From 009e450626194299ee4b5ccb8463ac64e127fde6 Mon Sep 17 00:00:00 2001 From: Roland Reichwein Date: Fri, 13 Nov 2020 17:50:02 +0100 Subject: Added push and pop --- asm/intel64/all_ops.h | 2 ++ asm/intel64/idiv.cpp | 3 --- asm/intel64/pop.cpp | 30 ++++++++++++++++++++++++++++++ asm/intel64/pop.h | 31 +++++++++++++++++++++++++++++++ asm/intel64/push.cpp | 30 ++++++++++++++++++++++++++++++ asm/intel64/push.h | 31 +++++++++++++++++++++++++++++++ asm/intel64/trivials.cpp | 4 ++++ asm/intel64/trivials.h | 15 +++------------ 8 files changed, 131 insertions(+), 15 deletions(-) create mode 100644 asm/intel64/pop.cpp create mode 100644 asm/intel64/pop.h create mode 100644 asm/intel64/push.cpp create mode 100644 asm/intel64/push.h (limited to 'asm') diff --git a/asm/intel64/all_ops.h b/asm/intel64/all_ops.h index c41734c..779506d 100644 --- a/asm/intel64/all_ops.h +++ b/asm/intel64/all_ops.h @@ -10,6 +10,8 @@ #include "jmp.h" #include "mov.h" #include "mul.h" +#include "pop.h" +#include "push.h" #include "sub.h" #include "trivials.h" #include "xor.h" diff --git a/asm/intel64/idiv.cpp b/asm/intel64/idiv.cpp index c90724f..3ee17a3 100644 --- a/asm/intel64/idiv.cpp +++ b/asm/intel64/idiv.cpp @@ -31,9 +31,6 @@ bool registered { registerOp(mangleName("idiv"), [](Asm::Args& args) -> std::shared_ptr{ return std::make_shared(args); }) && - registerOp(mangleName("idiv"), [](Asm::Args& args) -> std::shared_ptr{ - return std::make_shared(args); - }) && registerOp(mangleName("idiv"), [](Asm::Args& args) -> std::shared_ptr{ return std::make_shared(args); }) diff --git a/asm/intel64/pop.cpp b/asm/intel64/pop.cpp new file mode 100644 index 0000000..8528b15 --- /dev/null +++ b/asm/intel64/pop.cpp @@ -0,0 +1,30 @@ +#include "pop.h" + +#include "codes.h" + +#include +#include + +#include + +using namespace std::string_literals; + +Op_pop::Op_pop(Asm::Args& args) +{ + if (args[0].type() == typeid(Asm::Args::Register64)) { // pop reg64 + machine_code = std::vector{ 0x58 } + RegNo(std::any_cast(args[0]).name()); + } else { + throw std::runtime_error("Unimplemented: pop "s + args[0].type().name()); + } +} + +namespace { + +bool registered { + registerOp(mangleName("pop"), [](Asm::Args& args) -> std::shared_ptr{ + return std::make_shared(args); + }) +}; + +} + diff --git a/asm/intel64/pop.h b/asm/intel64/pop.h new file mode 100644 index 0000000..3bcd421 --- /dev/null +++ b/asm/intel64/pop.h @@ -0,0 +1,31 @@ +// Pop value from stack + +#pragma once + +#include + +class Op_pop: public Op +{ +public: + Op_pop(Asm::Args& args); + +public: + std::vector getCode() override + { + return machine_code; + } + + size_t size() override + { + return machine_code.size(); + } + + bool optimize() override ///< returns true if changed + { + return false; + } + +protected: + std::vector machine_code; +}; + diff --git a/asm/intel64/push.cpp b/asm/intel64/push.cpp new file mode 100644 index 0000000..bad2c90 --- /dev/null +++ b/asm/intel64/push.cpp @@ -0,0 +1,30 @@ +#include "push.h" + +#include "codes.h" + +#include +#include + +#include + +using namespace std::string_literals; + +Op_push::Op_push(Asm::Args& args) +{ + if (args[0].type() == typeid(Asm::Args::Register64)) { // push reg64 + machine_code = std::vector{ 0x50 } + RegNo(std::any_cast(args[0]).name()); + } else { + throw std::runtime_error("Unimplemented: push "s + args[0].type().name()); + } +} + +namespace { + +bool registered { + registerOp(mangleName("push"), [](Asm::Args& args) -> std::shared_ptr{ + return std::make_shared(args); + }) +}; + +} + diff --git a/asm/intel64/push.h b/asm/intel64/push.h new file mode 100644 index 0000000..9525981 --- /dev/null +++ b/asm/intel64/push.h @@ -0,0 +1,31 @@ +// Push value onto stack + +#pragma once + +#include + +class Op_push: public Op +{ +public: + Op_push(Asm::Args& args); + +public: + std::vector getCode() override + { + return machine_code; + } + + size_t size() override + { + return machine_code.size(); + } + + bool optimize() override ///< returns true if changed + { + return false; + } + +protected: + std::vector machine_code; +}; + diff --git a/asm/intel64/trivials.cpp b/asm/intel64/trivials.cpp index dd89ecd..094d27f 100644 --- a/asm/intel64/trivials.cpp +++ b/asm/intel64/trivials.cpp @@ -2,6 +2,10 @@ #include +Op_nop::Op_nop(): OpSimple({ 0x90 }) {} +Op_ret::Op_ret(): OpSimple({ 0xC3 }) {} // near return; TODO: far return is 0xCB +Op_syscall::Op_syscall(): OpSimple({ 0x0F, 0x05 }) {} + namespace { bool registered { diff --git a/asm/intel64/trivials.h b/asm/intel64/trivials.h index 72ea3f8..898001e 100644 --- a/asm/intel64/trivials.h +++ b/asm/intel64/trivials.h @@ -5,20 +5,11 @@ #include // No Operation -class Op_nop: public OpSimple -{ -public: Op_nop() : OpSimple({ 0x90 }) {} -}; +class Op_nop: public OpSimple { public: Op_nop(); }; // Return from procedure -class Op_ret: public OpSimple -{ -public: Op_ret() : OpSimple({ 0xC3 }) {} // near return; TODO: far return is 0xCB -}; +class Op_ret: public OpSimple { public: Op_ret(); }; // Syscall -class Op_syscall: public OpSimple -{ -public: Op_syscall() : OpSimple({ 0x0F, 0x05 }) {} -}; +class Op_syscall: public OpSimple { public: Op_syscall(); }; -- cgit v1.2.3