From a3b4cd4fdd4340c952eaa00bca9bebf817b901ae Mon Sep 17 00:00:00 2001 From: Roland Reichwein Date: Sun, 15 Nov 2020 18:39:01 +0100 Subject: Fixed unit tests, prepared hierarchical evaluation via stack (WIP) --- asm/assembler.h | 18 ++++++++++++++++++ asm/intel64/encode.cpp | 24 ++++++++++++++---------- asm/intel64/mov.cpp | 6 ++++++ asm/intel64/xor.cpp | 15 +++++++++++++-- 4 files changed, 51 insertions(+), 12 deletions(-) (limited to 'asm') diff --git a/asm/assembler.h b/asm/assembler.h index aa886b5..bd291b8 100644 --- a/asm/assembler.h +++ b/asm/assembler.h @@ -4,6 +4,8 @@ #include "chunk.h" +#include "../minicc.h" + #include #include @@ -93,6 +95,22 @@ public: std::string m_name; }; + // 64 bit Ptr to 32 bit Memory + class Mem32Ptr64 + { + public: + Mem32Ptr64(const std::string& reg, int32_t offs = 0): m_reg(reg), m_offs(offs) {} + Mem32Ptr64(const std::string& reg, const std::string& reg2 = ""s, int32_t offs = 0): m_reg(reg), m_reg2(reg2), m_offs(offs) {} + std::string reg() { return m_reg; } + std::string reg2() { return m_reg2; } + int32_t offs() { return m_offs; } + + private: + std::string m_reg; + std::string m_reg2; + int32_t m_offs; + }; + class Label { public: diff --git a/asm/intel64/encode.cpp b/asm/intel64/encode.cpp index 123dff2..03a7897 100644 --- a/asm/intel64/encode.cpp +++ b/asm/intel64/encode.cpp @@ -37,7 +37,7 @@ void Asm::toMachineCode(const FlowGraph::Graph& graph, Segment& segment) std::runtime_error("Bad value for operand 1: Constant expected"); } - if (op.type() == FlowGraph::UnaryOperationType::Store) { + if (op.type() == FlowGraph::UnaryOperationType::Negate) { Asm::Args args1{{Asm::Args::Register32("edi"), Asm::Args::Immediate32(immediate1)}}; segment.push_back(makeOp("mov", args1)); } else @@ -84,16 +84,14 @@ void Asm::toMachineCode(const FlowGraph::Graph& graph, Segment& segment) std::runtime_error("Bad value for operand 2: Constant expected"); } - Asm::Args args1{{Asm::Args::Register32("edi"), Asm::Args::Immediate32(immediate1)}}; - segment.push_back(makeOp("mov", args1)); + segment.push_back(makeOp("mov", Asm::Args{{Asm::Args::Register32("edi"), Asm::Args::Immediate32(immediate1)}})); - Asm::Args args2{{Asm::Args::Register32("edi"), Asm::Args::Immediate32(immediate2)}}; - - if (op.type() == FlowGraph::BinaryOperationType::Add) - segment.push_back(makeOp("add", args2)); - else if (op.type() == FlowGraph::BinaryOperationType::Multiply) - segment.push_back(makeOp("mul", args2)); - else + if (op.type() == FlowGraph::BinaryOperationType::Add) { + segment.push_back(makeOp("add", Asm::Args{{Asm::Args::Register32("edi"), Asm::Args::Immediate32(immediate2)}})); + } else if (op.type() == FlowGraph::BinaryOperationType::Multiply) { + segment.push_back(makeOp("mov", Asm::Args{{Asm::Args::Register32("ebx"), Asm::Args::Immediate32(immediate2)}})); + segment.push_back(makeOp("mul", Asm::Args{{Asm::Args::Register32("ebx")}})); + } else throw std::runtime_error("ICE: Asm: Unsupported binary operation type: "s + std::to_string(static_cast(op.type()))); } else if (typeid(node_deref) == typeid(FlowGraph::CreateScopeOp)) { @@ -104,6 +102,12 @@ void Asm::toMachineCode(const FlowGraph::Graph& graph, Segment& segment) } else if (typeid(node_deref) == typeid(FlowGraph::DestroyScopeOp)) { //FlowGraph::DestroyScopeOp& op {dynamic_cast(*node)}; segment.push_back(makeOp("pop", Asm::Args{{Asm::Args::Register64("rbp")}})); + + // Move + 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")}})); + } else if (typeid(node_deref) == typeid(FlowGraph::DataNode)) { + // ignore: Immediate data is used in subsequent nodes } else { throw std::runtime_error("ICE: Encoding: Unsupported node: "s + demangle(typeid(node_deref))); } diff --git a/asm/intel64/mov.cpp b/asm/intel64/mov.cpp index 5b224c1..6426238 100644 --- a/asm/intel64/mov.cpp +++ b/asm/intel64/mov.cpp @@ -43,6 +43,12 @@ 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); }) && diff --git a/asm/intel64/xor.cpp b/asm/intel64/xor.cpp index aba6fb5..1628558 100644 --- a/asm/intel64/xor.cpp +++ b/asm/intel64/xor.cpp @@ -12,9 +12,14 @@ using namespace std::string_literals; Op_xor::Op_xor(const Asm::Args& args) { if (args[0].type() == typeid(Asm::Args::Register8) && args[1].type() == typeid(Asm::Args::Register8)) { // xor reg8, reg8 - // r8, r/m8: ModRM:reg (w), ModRM:r/m (r) - machine_code = std::vector{ 0x32 } + + machine_code = std::vector{ 0x30 } + ModRM(std::any_cast(args[0]).name(), std::any_cast(args[1]).name()); + } else if (args[0].type() == typeid(Asm::Args::Register32) && args[1].type() == typeid(Asm::Args::Register32)) { // xor reg32, reg32 + machine_code = std::vector{ 0x31 } + + ModRM(std::any_cast(args[0]).name(), std::any_cast(args[1]).name()); + } else if (args[0].type() == typeid(Asm::Args::Register64) && args[1].type() == typeid(Asm::Args::Register64)) { // xor reg64, reg64 + machine_code = REX("W") + std::vector{ 0x31 } + + ModRM(std::any_cast(args[0]).name(), std::any_cast(args[1]).name()); } else { throw std::runtime_error("Unimplemented: xor "s + args[0].type().name() + " "s + args[1].type().name()); } @@ -25,6 +30,12 @@ namespace { bool registered { registerOp(mangleName("xor"), [](const Asm::Args& args) -> std::shared_ptr{ return std::make_shared(args); + }) && + registerOp(mangleName("xor"), [](const Asm::Args& args) -> std::shared_ptr{ + return std::make_shared(args); + }) && + registerOp(mangleName("xor"), [](const Asm::Args& args) -> std::shared_ptr{ + return std::make_shared(args); }) }; -- cgit v1.2.3