summaryrefslogtreecommitdiffhomepage
path: root/asm/chunk.h
diff options
context:
space:
mode:
authorRoland Reichwein <mail@reichwein.it>2020-10-17 14:16:46 +0200
committerRoland Reichwein <mail@reichwein.it>2020-10-17 14:16:46 +0200
commitf86999e137f43372236f2dccd1fe3572a85c0dcd (patch)
treefedfeec810d22dde57073c5b51ecf1a4253a9c61 /asm/chunk.h
parent85e9768c6a083165ef8376d2924f5d82ce91d118 (diff)
Separate-out assembler
Diffstat (limited to 'asm/chunk.h')
-rw-r--r--asm/chunk.h79
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;
+};
+