summaryrefslogtreecommitdiffhomepage
path: root/asm/chunk.h
blob: 43cf54eaa0762425f08fe1e064157c28f429914d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
// Chunk data type, and derived types

#pragma once

#include <cstdint>
#include <string>
#include <vector>

// TODO: use it everywhere!
using OP_T = std::vector<uint8_t>;

class Chunk
{
public:
 virtual ~Chunk(){}
 virtual std::vector<uint8_t> getCode() = 0;
 virtual size_t size() = 0; ///< returns size in bytes
};

// can be added via multiple inheritance to chunks with addresses
struct AddressFeature
{
 std::string label;

 bool relativeAddressing{true};

 std::vector<uint8_t> machine_code;
 size_t addr_size;
 size_t addr_offs; ///< offset inside code

 std::vector<uint8_t> alternative_code;
 size_t alternative_size;
 size_t alternative_offs; ///< offset inside code
};

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(const 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;
};