From 1349c00b782eca3ea841bfa388301cb6fc908cc7 Mon Sep 17 00:00:00 2001 From: Roland Reichwein Date: Sun, 18 Oct 2020 20:39:56 +0200 Subject: Namespace Asm --- asm/assembler.cpp | 6 +++--- asm/assembler.h | 16 +++++++++------- asm/intel64/add.cpp | 22 +++++++++++----------- asm/intel64/add.h | 2 +- asm/intel64/int.cpp | 6 +++--- asm/intel64/int.h | 2 +- asm/intel64/jmp.cpp | 6 +++--- asm/intel64/jmp.h | 2 +- asm/intel64/mov.cpp | 8 ++++---- asm/intel64/mov.h | 2 +- asm/intel64/nop.cpp | 2 +- asm/intel64/ret.cpp | 2 +- asm/intel64/xor.cpp | 8 ++++---- asm/intel64/xor.h | 2 +- 14 files changed, 44 insertions(+), 42 deletions(-) (limited to 'asm') diff --git a/asm/assembler.cpp b/asm/assembler.cpp index 501c0a4..5d8a986 100644 --- a/asm/assembler.cpp +++ b/asm/assembler.cpp @@ -22,7 +22,7 @@ bool registerOp(const std::string& mnemonic, FactoryFunction f) return true; } -std::string mangleName(const std::string& s, AsmArgs& args) +std::string mangleName(const std::string& s, Asm::Args& args) { std::string result {s}; @@ -33,7 +33,7 @@ std::string mangleName(const std::string& s, AsmArgs& args) return result; } -std::shared_ptr makeOp(const std::string& mnemonic, AsmArgs& args) +std::shared_ptr makeOp(const std::string& mnemonic, Asm::Args& args) { std::string mangled{mangleName(mnemonic, args)}; @@ -46,7 +46,7 @@ std::shared_ptr makeOp(const std::string& mnemonic, AsmArgs& args) std::shared_ptr makeOp(const std::string& mnemonic) { - AsmArgs dummy; // empty list of arguments + Asm::Args dummy; // empty list of arguments return makeOp(mnemonic, dummy); } diff --git a/asm/assembler.h b/asm/assembler.h index 52c3da5..f301d60 100644 --- a/asm/assembler.h +++ b/asm/assembler.h @@ -12,13 +12,13 @@ #include #include -// TODO: namespace Asm, e.g. AsmArgs -> Asm::Args +namespace Asm { -class AsmArgs: public std::vector +class Args: public std::vector { public: - AsmArgs(){} - AsmArgs(const std::vector& args): std::vector(args){} + Args(){} + Args(const std::vector& args): std::vector(args){} class Immediate8 { @@ -88,14 +88,16 @@ public: }; -using FactoryFunction = std::function(AsmArgs&)>; +} // namespace Asm + +using FactoryFunction = std::function(Asm::Args&)>; // mnemonic: mnemonic including argument types bool registerOp(const std::string& mnemonic, FactoryFunction f); // Create Op from a registered mnemonic // mnemonic: just the mnemonic name -std::shared_ptr makeOp(const std::string& mnemonic, AsmArgs& args); +std::shared_ptr makeOp(const std::string& mnemonic, Asm::Args& args); // overload for empty list of arguments std::shared_ptr makeOp(const std::string& mnemonic); @@ -118,5 +120,5 @@ std::string mangleName(const std::string& s) return mangleName(s + "_" + typeid(T).name()); } -std::string mangleName(const std::string& s, AsmArgs& args); +std::string mangleName(const std::string& s, Asm::Args& args); diff --git a/asm/intel64/add.cpp b/asm/intel64/add.cpp index 2de2219..106ffec 100644 --- a/asm/intel64/add.cpp +++ b/asm/intel64/add.cpp @@ -7,18 +7,18 @@ using namespace std::string_literals; -Op_add::Op_add(AsmArgs& args) +Op_add::Op_add(Asm::Args& args) { - if (args[0].type() == typeid(AsmArgs::Register32) && - std::any_cast(args[0]).name() == "eax" && - args[1].type() == typeid(AsmArgs::Immediate32)) + 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 - machine_code = std::vector{ 0x05 } + std::any_cast(args[1]).getCode(); - } else if (args[0].type() == typeid(AsmArgs::Register64) && - std::any_cast(args[0]).name() == "rax" && - args[1].type() == typeid(AsmArgs::Immediate32)) + machine_code = std::vector{ 0x05 } + 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(); + 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()); } @@ -27,10 +27,10 @@ Op_add::Op_add(AsmArgs& args) namespace { bool registered { - registerOp(mangleName("add"), [](AsmArgs& args) -> std::shared_ptr{ + registerOp(mangleName("add"), [](Asm::Args& args) -> std::shared_ptr{ return std::make_shared(args); }) && - registerOp(mangleName("add"), [](AsmArgs& args) -> std::shared_ptr{ + registerOp(mangleName("add"), [](Asm::Args& args) -> std::shared_ptr{ return std::make_shared(args); }) }; diff --git a/asm/intel64/add.h b/asm/intel64/add.h index 4c6b589..1d55317 100644 --- a/asm/intel64/add.h +++ b/asm/intel64/add.h @@ -7,7 +7,7 @@ class Op_add: public Op { public: - Op_add(AsmArgs& args); + Op_add(Asm::Args& args); public: std::vector getCode() override diff --git a/asm/intel64/int.cpp b/asm/intel64/int.cpp index a7df338..de6c73b 100644 --- a/asm/intel64/int.cpp +++ b/asm/intel64/int.cpp @@ -2,11 +2,11 @@ #include -Op_int::Op_int(AsmArgs& args) +Op_int::Op_int(Asm::Args& args) { // At this point, the registration already ensured the number and types of args - AsmArgs::Immediate8 i {std::any_cast(args[0])}; + Asm::Args::Immediate8 i {std::any_cast(args[0])}; if (i.value() == 0) { // INT 0 machine_code = { 0xCE }; @@ -21,7 +21,7 @@ Op_int::Op_int(AsmArgs& args) namespace { -bool registered { registerOp(mangleName("int"), [](AsmArgs& args) -> std::shared_ptr{ +bool registered { registerOp(mangleName("int"), [](Asm::Args& args) -> std::shared_ptr{ return std::make_shared(args); }) }; diff --git a/asm/intel64/int.h b/asm/intel64/int.h index 7bd60c8..aee4962 100644 --- a/asm/intel64/int.h +++ b/asm/intel64/int.h @@ -7,7 +7,7 @@ class Op_int: public Op { public: - Op_int(AsmArgs& args); + Op_int(Asm::Args& args); public: std::vector getCode() override diff --git a/asm/intel64/jmp.cpp b/asm/intel64/jmp.cpp index 3899cac..8542127 100644 --- a/asm/intel64/jmp.cpp +++ b/asm/intel64/jmp.cpp @@ -61,7 +61,7 @@ namespace { bool registerOps() { bool result{true}; for (const auto& jumpOp: jumpOps) { - result &= registerOp(mangleName(jumpOp.name), [&](AsmArgs& args) -> std::shared_ptr{ + result &= registerOp(mangleName(jumpOp.name), [&](Asm::Args& args) -> std::shared_ptr{ return std::make_shared(jumpOp.name, args, jumpOp.jmp8, jumpOp.jmp32); }); } @@ -73,9 +73,9 @@ namespace { }; } -Op_jmp::Op_jmp(const std::string& name, AsmArgs& args, const OP_T& jmp8, const OP_T& jmp32) +Op_jmp::Op_jmp(const std::string& name, Asm::Args& args, const OP_T& jmp8, const OP_T& jmp32) { - label = std::any_cast(args[0]).name(); + label = std::any_cast(args[0]).name(); if (!jmp32.empty()) { // set machine_code machine_code = jmp32 + OP_T(size_t(4), uint8_t(0)); diff --git a/asm/intel64/jmp.h b/asm/intel64/jmp.h index db8a5a8..a7c7511 100644 --- a/asm/intel64/jmp.h +++ b/asm/intel64/jmp.h @@ -10,7 +10,7 @@ class Op_jmp: public Op, public AddressFeature { public: - Op_jmp(const std::string& name, AsmArgs& args, const OP_T& jmp8, const OP_T& jmp32); + Op_jmp(const std::string& name, Asm::Args& args, const OP_T& jmp8, const OP_T& jmp32); std::vector getCode() override { diff --git a/asm/intel64/mov.cpp b/asm/intel64/mov.cpp index 33589e9..40a48f8 100644 --- a/asm/intel64/mov.cpp +++ b/asm/intel64/mov.cpp @@ -9,12 +9,12 @@ using namespace std::string_literals; -Op_mov::Op_mov(AsmArgs& args) +Op_mov::Op_mov(Asm::Args& args) { - if (args[0].type() == typeid(AsmArgs::Register8) && args[1].type() == typeid(AsmArgs::Register8)) { // mov reg8, reg8 + if (args[0].type() == typeid(Asm::Args::Register8) && args[1].type() == typeid(Asm::Args::Register8)) { // mov reg8, reg8 // r/m8, r8: ModRM:r/m (w), ModRM:reg (r) machine_code = std::vector{ 0x88 } + - ModRM(std::any_cast(args[1]).name(), std::any_cast(args[0]).name()); + ModRM(std::any_cast(args[1]).name(), std::any_cast(args[0]).name()); } else { throw std::runtime_error("Unimplemented: mov "s + args[0].type().name() + " "s + args[1].type().name()); } @@ -23,7 +23,7 @@ Op_mov::Op_mov(AsmArgs& args) namespace { bool registered { - registerOp(mangleName("mov"), [](AsmArgs& args) -> std::shared_ptr{ + registerOp(mangleName("mov"), [](Asm::Args& args) -> std::shared_ptr{ return std::make_shared(args); }) }; diff --git a/asm/intel64/mov.h b/asm/intel64/mov.h index e1b2304..53afed4 100644 --- a/asm/intel64/mov.h +++ b/asm/intel64/mov.h @@ -7,7 +7,7 @@ class Op_mov: public Op { public: - Op_mov(AsmArgs& args); + Op_mov(Asm::Args& args); public: std::vector getCode() override diff --git a/asm/intel64/nop.cpp b/asm/intel64/nop.cpp index 450de46..2d99278 100644 --- a/asm/intel64/nop.cpp +++ b/asm/intel64/nop.cpp @@ -4,7 +4,7 @@ namespace { -bool registered { registerOp("nop", [](AsmArgs& args) -> std::shared_ptr{ +bool registered { registerOp("nop", [](Asm::Args& args) -> std::shared_ptr{ return std::make_shared(); }) }; diff --git a/asm/intel64/ret.cpp b/asm/intel64/ret.cpp index cd9ddd4..c42ad8a 100644 --- a/asm/intel64/ret.cpp +++ b/asm/intel64/ret.cpp @@ -4,7 +4,7 @@ namespace { -bool registered { registerOp("ret", [](AsmArgs& args) -> std::shared_ptr{ +bool registered { registerOp("ret", [](Asm::Args& args) -> std::shared_ptr{ return std::make_shared(); }) }; diff --git a/asm/intel64/xor.cpp b/asm/intel64/xor.cpp index c0dbb68..2aa4272 100644 --- a/asm/intel64/xor.cpp +++ b/asm/intel64/xor.cpp @@ -9,12 +9,12 @@ using namespace std::string_literals; -Op_xor::Op_xor(AsmArgs& args) +Op_xor::Op_xor(Asm::Args& args) { - if (args[0].type() == typeid(AsmArgs::Register8) && args[1].type() == typeid(AsmArgs::Register8)) { // xor reg8, reg8 + 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 } + - ModRM(std::any_cast(args[0]).name(), std::any_cast(args[1]).name()); + 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()); } @@ -23,7 +23,7 @@ Op_xor::Op_xor(AsmArgs& args) namespace { bool registered { - registerOp(mangleName("xor"), [](AsmArgs& args) -> std::shared_ptr{ + registerOp(mangleName("xor"), [](Asm::Args& args) -> std::shared_ptr{ return std::make_shared(args); }) }; diff --git a/asm/intel64/xor.h b/asm/intel64/xor.h index f00a657..7e07006 100644 --- a/asm/intel64/xor.h +++ b/asm/intel64/xor.h @@ -7,7 +7,7 @@ class Op_xor: public Op { public: - Op_xor(AsmArgs& args); + Op_xor(Asm::Args& args); public: std::vector getCode() override -- cgit v1.2.3