diff options
Diffstat (limited to 'intel.cpp')
-rw-r--r-- | intel.cpp | 82 |
1 files changed, 62 insertions, 20 deletions
@@ -155,7 +155,7 @@ namespace { if (sl[1].value == "eax") { // ADD EAX, imm32 return { { std::vector<uint8_t>{ 0x05 } +imm32(sl[2].value), {} } }; } else if (sl[1].value == "rax") { // ADD RAX, imm32 - return { { REX("W") + std::vector<uint8_t>{ 0x05 } +imm32(sl[2].value), {} } }; + return { { REX("W") + std::vector<uint8_t>{ 0x05 } +imm32(sl[2].value), {} } }; } } @@ -231,11 +231,6 @@ namespace { throw std::runtime_error("Unknown command: "s + sl[0].value); }}, - // No Operation - { "nop", [](const std::vector<Token>& sl) -> InstructionCodeList { - return {{ std::vector<uint8_t>{ 0x90 }, {}}}; - }}, - // Return from procedure { "ret", [](const std::vector<Token>& sl) -> InstructionCodeList { return {{ std::vector<uint8_t>{ 0xC3 }, {}}}; // near return; TODO: far return is 0xCB @@ -319,13 +314,70 @@ namespace { } // namespace +class Chunk +{ +public: + virtual ~Chunk(){} + virtual std::vector<uint8_t> getCode() = 0; + virtual size_t size() = 0; ///< returns size in bytes +}; -class Op +class Label: public Chunk +{ +public: + Label(const std::string& name) : m_name(name) {} + std::string name(){return m_name;} + std::vector<uint8_t> getCode() override { return {}; } + size_t size() override { return 0; } + +private: + std::string m_name; +}; + +class Data: public Chunk +{ +public: + Data(std::vector<uint8_t> data): m_data(data) {} + virtual ~Data(){} + + std::vector<uint8_t> getCode() override + { + return m_data; + } + + size_t size() override + { + return m_data.size(); + } + +protected: + std::vector<uint8_t> m_data; +}; + +class Segment: public std::vector<std::shared_ptr<Chunk>> +{ + 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<Label&>(chunk).name() == label) { + return address; + } + } + } + + throw std::runtime_error("Bad label: "s + label); + } +}; + +class Op: public Chunk { public: virtual ~Op(){}; - virtual std::vector<uint8_t> getMachineCode() = 0; - virtual size_t size() = 0; ///< returns size in bytes virtual bool optimize() = 0; ///< returns true if changed }; @@ -353,7 +405,7 @@ class OpSimple: public Op public: OpSimple(std::vector<uint8_t> machine_code): machine_code(machine_code) {} - std::vector<uint8_t> getMachineCode() override + std::vector<uint8_t> getCode() override { return machine_code; } @@ -372,16 +424,6 @@ protected: std::vector<uint8_t> machine_code; }; -class Op_Label: public OpSimple -{ -public: - Op_Label(const std::string& name) : OpSimple({}), m_name(name) {} - std::string name(){return m_name;} - -private: - std::string m_name; -}; - class Op_nop: public OpSimple { public: |