From 739297d8895b08a9ecd8e81b01b7ba8e8dc4a8ae Mon Sep 17 00:00:00 2001 From: Roland Reichwein Date: Sat, 21 Nov 2020 22:57:25 +0100 Subject: Bugfix push + pop --- asm/intel64/encode.cpp | 9 +++++++++ asm/intel64/mov.cpp | 29 +++++++++++++++++++++++++++++ asm/intel64/pop.cpp | 2 +- asm/intel64/push.cpp | 2 +- 4 files changed, 40 insertions(+), 2 deletions(-) (limited to 'asm/intel64') diff --git a/asm/intel64/encode.cpp b/asm/intel64/encode.cpp index 4e72933..62101f3 100644 --- a/asm/intel64/encode.cpp +++ b/asm/intel64/encode.cpp @@ -221,6 +221,15 @@ void Asm::toMachineCode(const FlowGraph::Graph& graph, Segment& segment) segment.push_back(makeLoadValue(graph.lastOp()->destination(), graph)); // TODO: Just get last operation result to eax for now segment.push_back(makeOp("xor", Asm::Args{{Asm::Args::Register64("rdi"), Asm::Args::Register64("rdi")}})); segment.push_back(makeOp("mov", Asm::Args{{Asm::Args::Register32("edi"), Asm::Args::Register32("eax")}})); + +#if 0 + segment.push_back(makeOp("xor", Asm::Args{{Asm::Args::Register64("rbp"), Asm::Args::Register64("rbp")}})); + segment.push_back(makeOp("dec", Asm::Args{{Asm::Args::Register64("rbp")}})); + segment.push_back(makeOp("mov", Asm::Args{{Asm::Args::Mem32Ptr64("rbp", -4), Asm::Args::Immediate32(3)}})); + segment.push_back(makeOp("mov", Asm::Args{{Asm::Args::Register32("edi"), Asm::Args::Mem32Ptr64("rbp", -4)}})); + //segment.push_back(makeOp("mov", Asm::Args{{Asm::Args::Register32("edi"), Asm::Args::Immediate32(3)}})); +#endif + segment.push_back(makeOp("mov", Asm::Args{{Asm::Args::Register64("rax"), Asm::Args::Immediate32(60)}})); // syscall 60: exit() segment.push_back(makeOp("syscall")); // rax: #syscall, rdi: exit code value } else if (typeid(node_deref) == typeid(FlowGraph::DataNode)) { diff --git a/asm/intel64/mov.cpp b/asm/intel64/mov.cpp index 5d09def..805675a 100644 --- a/asm/intel64/mov.cpp +++ b/asm/intel64/mov.cpp @@ -51,6 +51,23 @@ Op_mov::Op_mov(const Asm::Args& args) } else if (args[0].type() == typeid(Asm::Args::Register64) && args[1].type() == typeid(Asm::Args::Immediate32)) { // mov reg64, imm32 (sign-extended) machine_code = REX("W") + std::vector{ 0xC7 } + ModRM("/0", std::any_cast(args[0]).name()) + std::any_cast(args[1]).getCode(); + } else if (args[0].type() == typeid(Asm::Args::Mem8Ptr64) && args[1].type() == typeid(Asm::Args::Immediate8)) { // mov [reg64], imm8 + Asm::Args::Mem8Ptr64 ptr{std::any_cast(args[0])}; + machine_code = std::vector{ 0xC6 } + ModRM("/0", ptr.reg(), ptr.offs()) + std::any_cast(args[1]).getCode(); + + } else if (args[0].type() == typeid(Asm::Args::Mem32Ptr64) && args[1].type() == typeid(Asm::Args::Immediate32)) { // mov [reg64], imm32 + Asm::Args::Mem32Ptr64 ptr{std::any_cast(args[0])}; + machine_code = std::vector{ 0xC7 } + ModRM("/0", ptr.reg(), ptr.offs()) + std::any_cast(args[1]).getCode(); + + } else if (args[0].type() == typeid(Asm::Args::Mem64Ptr64) && args[1].type() == typeid(Asm::Args::Immediate32)) { // mov [reg64], imm32 (sign-extended to imm64) + Asm::Args::Mem64Ptr64 ptr{std::any_cast(args[0])}; + machine_code = REX("W") + std::vector{ 0xC7 } + ModRM("/0", ptr.reg(), ptr.offs()) + std::any_cast(args[1]).getCode(); + + } else if (args[0].type() == typeid(Asm::Args::Mem64Ptr64) && args[1].type() == typeid(Asm::Args::Immediate64)) { // mov [reg64], imm64 (cut to imm32) + Asm::Args::Mem64Ptr64 ptr{std::any_cast(args[0])}; + Asm::Args::Immediate32 imm32{std::any_cast(args[1])}; + machine_code = REX("W") + std::vector{ 0xC7 } + ModRM("/0", ptr.reg(), ptr.offs()) + imm32.getCode(); + } else { throw std::runtime_error("Unimplemented: mov "s + args[0].type().name() + " "s + args[1].type().name()); } @@ -88,6 +105,18 @@ bool registered { }) && registerOp(mangleName("mov"), [](const Asm::Args& args) -> std::shared_ptr{ return std::make_shared(args); + }) && + registerOp(mangleName("mov"), [](const Asm::Args& args) -> std::shared_ptr{ + return std::make_shared(args); + }) && + registerOp(mangleName("mov"), [](const Asm::Args& args) -> std::shared_ptr{ + return std::make_shared(args); + }) && + registerOp(mangleName("mov"), [](const Asm::Args& args) -> std::shared_ptr{ + return std::make_shared(args); + }) && + registerOp(mangleName("mov"), [](const Asm::Args& args) -> std::shared_ptr{ + return std::make_shared(args); }) }; diff --git a/asm/intel64/pop.cpp b/asm/intel64/pop.cpp index 6d8b734..6b396a2 100644 --- a/asm/intel64/pop.cpp +++ b/asm/intel64/pop.cpp @@ -12,7 +12,7 @@ using namespace std::string_literals; Op_pop::Op_pop(const Asm::Args& args) { if (args[0].type() == typeid(Asm::Args::Register64)) { // pop reg64 - machine_code = std::vector{ 0x58 } + RegNo(std::any_cast(args[0]).name()); + machine_code = std::vector{ static_cast(0x58 + RegNo(std::any_cast(args[0]).name())) }; } else { throw std::runtime_error("Unimplemented: pop "s + args[0].type().name()); } diff --git a/asm/intel64/push.cpp b/asm/intel64/push.cpp index 37acec9..9fb5955 100644 --- a/asm/intel64/push.cpp +++ b/asm/intel64/push.cpp @@ -12,7 +12,7 @@ using namespace std::string_literals; Op_push::Op_push(const Asm::Args& args) { if (args[0].type() == typeid(Asm::Args::Register64)) { // push reg64 - machine_code = std::vector{ 0x50 } + RegNo(std::any_cast(args[0]).name()); + machine_code = std::vector{ static_cast(0x50 + RegNo(std::any_cast(args[0]).name())) }; } else { throw std::runtime_error("Unimplemented: push "s + args[0].type().name()); } -- cgit v1.2.3