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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
|
// Nodes in flow graph: Abstract Operations
#pragma once
#include "data.h"
#include "storage.h"
#include <memory>
namespace FlowGraph {
// Node in Graph: Building block of the graph
// Abstracts actions of program flow, yet machine independent,
// to be converted later into machine code
// Basic elements:
// - Subroutine calls
// - Arithmetic/logical operations
// Arguments (Data instances) will be provided explicitly from outside
class Node
{
public:
Node(){}
Node(std::vector<Data> operands): mOperands(operands) {}
virtual ~Node() {}; // force class to be polymorphic (e.g. in a container)
std::vector<Data>& operands() { return mOperands; }
Data& destination(); // best-effort return of result/destination; else throw
private:
std::vector<Data> mOperands;
};
// Memory on Heap: new and delete
class AllocateDynamic: public Node
{
public:
AllocateDynamic(Data& location, Data& size):
Node(std::vector<Data>({location, size})) // in/out: Pointer; in: Size
{}
};
class DeallocateDynamic: public Node
{
public:
DeallocateDynamic(Data& location):
Node(std::vector<Data>({location})) // in: Pointer
{}
};
Data MakeConstantInt(int i);
Data MakeLocalInt(std::shared_ptr<FlowGraph::LocalScope> scope, const std::string& name);
Data MakeLocalPointer(std::shared_ptr<FlowGraph::LocalScope> scope, const std::string& name);
Data MakeTemporaryInt(std::shared_ptr<LocalScope> scope);
class MemCopy: public Node
{
public:
MemCopy(Data& destination, Data& source, Data& size): // Pointer, Pointer, size in bytes
Node(std::vector<Data>({destination, source, size}))
{}
};
class Move: public Node
{
public:
Move(Data& destination, Data& source):
Node(std::vector<Data>({destination, source}))
{}
};
enum class JumpVariant: int
{
Unconditional,
GT,
LT,
GE,
LE,
EQ,
NE
};
// Unconditional Jump, conditional Jump
class Jump: public Node
{
public:
Jump(JumpVariant variant, Data& source0, Data& source1, std::shared_ptr<Node> destination):
Node(std::vector<Data>({source0, source1})),
m_variant(variant),
m_destination(destination)
{}
JumpVariant variant() { return m_variant; }
private:
JumpVariant m_variant;
std::shared_ptr<Node> m_destination; // successor on branch
};
// Call Subroutine
class Call: public Node
{
public:
Call(std::shared_ptr<Node> destination, std::vector<Data> arguments):
Node(arguments),
m_destination(destination)
{}
private:
std::shared_ptr<Node> m_destination;
};
// Return from Subroutine
class Return: public Node
{
public:
Return(std::vector<Data> returnValues):
Node(returnValues)
{}
};
enum class UnaryOperationType: int
{
LogicalNot,
BitwiseNot,
Minus,
};
class UnaryOperation: public Node
{
public:
UnaryOperation(UnaryOperationType type, Data& destination, Data& source):
Node(std::vector<Data>({destination, source})),
m_type(type)
{}
UnaryOperationType type() { return m_type; }
private:
UnaryOperationType m_type;
};
// Just take a value e.g. Immediate and store it for later use.
// Should be optimized out later.
class DataNode: public Node
{
public:
DataNode(Data& value):
Node(std::vector<Data>({value}))
{}
};
enum class BinaryOperationType: int
{
Add,
Subtract,
Multiply,
Divide,
Modulo,
ShiftRight,
ShiftLeft,
BitwiseAnd,
BitwiseOr,
BitwiseXor,
BitwiseNot,
LogicalAnd,
LogicalOr,
LogicalNot
};
class BinaryOperation: public Node
{
public:
BinaryOperation(BinaryOperationType type, Data& destination, Data& source0, Data& source1):
Node(std::vector<Data>({destination, source0, source1})),
m_type(type)
{}
BinaryOperationType type() {return m_type;}
private:
BinaryOperationType m_type;
};
// Open a new scope, with stack frame
class CreateScopeOp: public Node
{
public:
CreateScopeOp();
std::shared_ptr<LocalScope> scope();
private:
std::shared_ptr<LocalScope> m_scope;
};
// Close current scope, closing stack frame
class DestroyScopeOp: public Node
{
public:
DestroyScopeOp(std::shared_ptr<LocalScope> scope);
private:
std::shared_ptr<LocalScope> m_scope;
};
} // namespace FlowGraph
|