diff options
Diffstat (limited to 'asm/chunk.h')
-rw-r--r-- | asm/chunk.h | 79 |
1 files changed, 79 insertions, 0 deletions
diff --git a/asm/chunk.h b/asm/chunk.h new file mode 100644 index 0000000..723e711 --- /dev/null +++ b/asm/chunk.h @@ -0,0 +1,79 @@ +// Chunk data type, and derived types + +#pragma once + +#include <cstdint> +#include <string> +#include <vector> + +class Chunk +{ +public: + virtual ~Chunk(){} + virtual std::vector<uint8_t> 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<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 Op: public Chunk +{ +public: + virtual ~Op(){}; + virtual bool optimize() = 0; ///< returns true if changed +}; + +class OpSimple: public Op +{ +public: + OpSimple(std::vector<uint8_t> machine_code): machine_code(machine_code) {} + + std::vector<uint8_t> 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<uint8_t> machine_code; +}; + |