From f86999e137f43372236f2dccd1fe3572a85c0dcd Mon Sep 17 00:00:00 2001 From: Roland Reichwein Date: Sat, 17 Oct 2020 14:16:46 +0200 Subject: Separate-out assembler --- intel.cpp | 136 -------------------------------------------------------------- 1 file changed, 136 deletions(-) (limited to 'intel.cpp') diff --git a/intel.cpp b/intel.cpp index 7ef6be3..9dac6c0 100644 --- a/intel.cpp +++ b/intel.cpp @@ -314,142 +314,6 @@ namespace { } // namespace -class Chunk -{ -public: - virtual ~Chunk(){} - virtual std::vector getCode() = 0; - virtual size_t size() = 0; ///< returns size in bytes -}; - -class Label: public Chunk -{ -public: - Label(const std::string& name) : m_name(name) {} - std::string name(){return m_name;} - std::vector getCode() override { return {}; } - size_t size() override { return 0; } - -private: - std::string m_name; -}; - -class Data: public Chunk -{ -public: - Data(std::vector data): m_data(data) {} - virtual ~Data(){} - - std::vector getCode() override - { - return m_data; - } - - size_t size() override - { - return m_data.size(); - } - -protected: - std::vector m_data; -}; - -class Segment: public std::vector> -{ - size_t getAddressOfLabel(const std::string& label) - { - size_t address{0}; - auto i{begin()}; - while (i != end()) { - Chunk& chunk{**i}; - address += chunk.size(); - if (typeid(chunk) == typeid(Label)) { - if (dynamic_cast(chunk).name() == label) { - return address; - } - } - } - - throw std::runtime_error("Bad label: "s + label); - } -}; - -class Op: public Chunk -{ -public: - virtual ~Op(){}; - virtual bool optimize() = 0; ///< returns true if changed -}; - -using AsmArgs = std::vector; // 0th element is mnemonic -using FactoryFunction = std::function(AsmArgs&)>; - -std::unordered_map ops; - -bool registerOp(const std::string& mnemonic, FactoryFunction f) -{ - if (ops.contains(mnemonic)) { - std::cout << "Warning: mnemonic |" << mnemonic << "| already registered." << std::endl; - return false; - } - - std::cout << "Registering mnemonic |" << mnemonic << "|." << std::endl; - - ops[mnemonic] = f; - - return true; -} - -class OpSimple: public Op -{ -public: - OpSimple(std::vector machine_code): machine_code(machine_code) {} - - 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; -}; - -class Op_nop: public OpSimple -{ -public: - Op_nop() : OpSimple({ 0x90 }) {} - -}; - -template -std::string mangleNameOne(const std::string& s) -{ - return s + "_" + typeid(T).name(); -} - -template -std::string mangleName(const std::string& s) -{ - if constexpr (sizeof...(Targs) == 0) - return mangleNameOne(s); - else - return mangleName(s + "_" + typeid(T).name()); -} - -bool registered { registerOp("nop", [](AsmArgs& args) -> std::shared_ptr{ - return std::make_shared(); - }) }; - class Assembler { std::unordered_map labels; ///< labels with their positions in instruction list -- cgit v1.2.3