#include "assembler.h" using namespace std::string_literals; Asm::Args::Immediate32::Immediate32(const Asm::Args::Immediate64& imm64) { if (imm64.value() < 0x100000000) m_value = static_cast(imm64.value()); else throw std::runtime_error("Immediate32: Constructed from too big Immediate64"); } Asm::Args::Mem8Ptr64::Mem8Ptr64(const std::string& reg, int32_t offs): m_reg(reg), m_offs(offs) {} Asm::Args::Mem8Ptr64::Mem8Ptr64(const std::string& reg, const std::string& reg2, int32_t offs): m_reg(reg), m_reg2(reg2), m_offs(offs) {} std::string Asm::Args::Mem8Ptr64::reg() const { return "["s + m_reg + "]"s; } std::string Asm::Args::Mem8Ptr64::reg2() const { return "["s + m_reg2 + "]"s; } int32_t Asm::Args::Mem8Ptr64::offs() const { return m_offs; } Asm::Args::Mem32Ptr64::Mem32Ptr64(const std::string& reg, int32_t offs): m_reg(reg), m_offs(offs) {} Asm::Args::Mem32Ptr64::Mem32Ptr64(const std::string& reg, const std::string& reg2, int32_t offs): m_reg(reg), m_reg2(reg2), m_offs(offs) {} std::string Asm::Args::Mem32Ptr64::reg() const { return "["s + m_reg + "]"s; } std::string Asm::Args::Mem32Ptr64::reg2() const { return "["s + m_reg2 + "]"s; } int32_t Asm::Args::Mem32Ptr64::offs() const { return m_offs; } Asm::Args::Mem64Ptr64::Mem64Ptr64(const std::string& reg, int32_t offs): m_reg(reg), m_offs(offs) {} Asm::Args::Mem64Ptr64::Mem64Ptr64(const std::string& reg, const std::string& reg2, int32_t offs): m_reg(reg), m_reg2(reg2), m_offs(offs) {} std::string Asm::Args::Mem64Ptr64::reg() const { return "["s + m_reg + "]"s; } std::string Asm::Args::Mem64Ptr64::reg2() const { return "["s + m_reg2 + "]"s; } int32_t Asm::Args::Mem64Ptr64::offs() const { return m_offs; } namespace { std::unordered_map ops; } bool registerOp(const std::string& mnemonic, FactoryFunction f) { if (ops.contains(mnemonic)) { std::cerr << "Warning: mnemonic |" << mnemonic << "| already registered." << std::endl; return false; } //std::cout << "Registering mnemonic |" << mnemonic << "|." << std::endl; ops[mnemonic] = f; return true; } std::string mangleName(const std::string& s, const Asm::Args& args) { std::string result {s}; for (const auto& arg: args) { result += "_"s + arg.type().name(); } return result; } std::shared_ptr makeOp(const std::string& mnemonic, const Asm::Args& args) { std::string mangled{mangleName(mnemonic, args)}; const auto& i{ops.find(mangled)}; if (i == ops.end()) throw std::runtime_error("Instruction "s + mangled + " not implemented."); return i->second(args); } std::shared_ptr makeOp(const std::string& mnemonic) { Asm::Args dummy; // empty list of arguments return makeOp(mnemonic, dummy); } std::shared_ptr