#include "add.h" #include "codes.h" #include #include using namespace std::string_literals; Op_add::Op_add(const Asm::Args& args) { if (args[0].type() == typeid(Asm::Args::Register32) && std::any_cast(args[0]).name() == "eax" && args[1].type() == typeid(Asm::Args::Immediate32)) { // add eax, imm32 (before "add reg32, imm32"! It's shorter.) machine_code = std::vector{ 0x05 } + std::any_cast(args[1]).getCode(); } else if (args[0].type() == typeid(Asm::Args::Register32) && args[1].type() == typeid(Asm::Args::Immediate32)) { // add reg32, imm32 machine_code = std::vector{ 0x81 } + ModRM("/0", std::any_cast(args[0]).name()) + std::any_cast(args[1]).getCode(); } else if (args[0].type() == typeid(Asm::Args::Register64) && std::any_cast(args[0]).name() == "rax" && args[1].type() == typeid(Asm::Args::Immediate32)) { // add rax, imm32 machine_code = REX("W") + std::vector{ 0x05 } + std::any_cast(args[1]).getCode(); } else { throw std::runtime_error("Unimplemented: add "s + args[0].type().name() + " "s + args[1].type().name()); } } namespace { bool registered { registerOp(mangleName("add"), [](const Asm::Args& args) -> std::shared_ptr{ return std::make_shared(args); }) && registerOp(mangleName("add"), [](const Asm::Args& args) -> std::shared_ptr{ return std::make_shared(args); }) }; }