diff options
Diffstat (limited to 'asm')
-rw-r--r-- | asm/assembler.h | 18 | ||||
-rw-r--r-- | asm/intel64/encode.cpp | 24 | ||||
-rw-r--r-- | asm/intel64/mov.cpp | 6 | ||||
-rw-r--r-- | asm/intel64/xor.cpp | 15 |
4 files changed, 51 insertions, 12 deletions
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 <boost/endian/conversion.hpp> #include <any> @@ -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<int>(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<FlowGraph::DestroyScopeOp&>(*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<Asm::Args::Register8, Asm::Args::Register8>("mov"), [](const Asm::Args& args) -> std::shared_ptr<Op>{ return std::make_shared<Op_mov>(args); }) && + registerOp(mangleName<Asm::Args::Register32, Asm::Args::Register32>("mov"), [](const Asm::Args& args) -> std::shared_ptr<Op>{ + return std::make_shared<Op_mov>(args); + }) && + registerOp(mangleName<Asm::Args::Register64, Asm::Args::Register64>("mov"), [](const Asm::Args& args) -> std::shared_ptr<Op>{ + return std::make_shared<Op_mov>(args); + }) && registerOp(mangleName<Asm::Args::Register32, Asm::Args::Immediate32>("mov"), [](const Asm::Args& args) -> std::shared_ptr<Op>{ return std::make_shared<Op_mov>(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<uint8_t>{ 0x32 } + + machine_code = std::vector<uint8_t>{ 0x30 } + ModRM(std::any_cast<Asm::Args::Register8>(args[0]).name(), std::any_cast<Asm::Args::Register8>(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<uint8_t>{ 0x31 } + + ModRM(std::any_cast<Asm::Args::Register32>(args[0]).name(), std::any_cast<Asm::Args::Register32>(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<uint8_t>{ 0x31 } + + ModRM(std::any_cast<Asm::Args::Register64>(args[0]).name(), std::any_cast<Asm::Args::Register64>(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<Asm::Args::Register8, Asm::Args::Register8>("xor"), [](const Asm::Args& args) -> std::shared_ptr<Op>{ return std::make_shared<Op_xor>(args); + }) && + registerOp(mangleName<Asm::Args::Register32, Asm::Args::Register32>("xor"), [](const Asm::Args& args) -> std::shared_ptr<Op>{ + return std::make_shared<Op_xor>(args); + }) && + registerOp(mangleName<Asm::Args::Register64, Asm::Args::Register64>("xor"), [](const Asm::Args& args) -> std::shared_ptr<Op>{ + return std::make_shared<Op_xor>(args); }) }; |