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