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
|
// Nodes in flow graph: Abstract Operations
#pragma once
#include "data.h"
#include "storage.h"
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) {}
std::vector<Data>& operands() { return mOperands; }
virtual ~Node() {}; // force class to be polymorphic (e.g. in a container)
private:
std::vector<Data> mOperands;
};
// Memory on Heap: new and delete
class AllocateDynamic: public Node
{
public:
AllocateDynamic(Data& location, Data& size): m_location(location), m_size(size) {}
private:
Data m_location; // in/out: Pointer
Data m_size; // in: Size
};
class DeallocateDynamic: public Node
{
public:
DeallocateDynamic(Data& location) : m_location(location) {} // in
private:
Data m_location; // in: Pointer
};
Data MakeConstantInt(int i);
Data MakeLocalPointer(const std::string& name);
Data MakeLocalSize(const std::string& name);
Data MakeTemporaryInt(LocalScope& scope);
class MemCopy: public Node
{
public:
MemCopy(Data& destination, Data& source, Data& size): m_destination(destination), m_source(source), m_size(size) {}
private:
Data m_destination; // Pointer
Data m_source; // Pointer
Data m_size; // in bytes
};
class Move: public Node
{
public:
Move(Data& destination, Data& source): m_destination(destination), m_source(source) {}
private:
Data m_destination;
Data m_source;
};
enum class JumpVariant
{
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):
m_variant(variant),
m_source0(source0),
m_source1(source1),
m_destination(destination)
{}
private:
JumpVariant m_variant;
Data m_source0;
Data m_source1;
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): m_destination(destination), m_arguments(arguments) {}
private:
std::shared_ptr<Node> m_destination;
std::vector<Data> m_arguments;
};
// Return from Subroutine
class Return: public Node
{
public:
Return(std::vector<Data> returnValues): m_returnValues(returnValues) {}
private:
std::vector<Data> m_returnValues;
};
enum class UnaryOperationType {
Increment,
Decrement,
Negate
};
class UnaryOperation: public Node
{
public:
UnaryOperation(UnaryOperationType type, Data& destination, Data& source): m_type(type), m_destination(destination), m_source(source) {}
private:
UnaryOperationType m_type;
Data m_destination;
Data m_source;
};
enum class BinaryOperationType {
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;
};
} // namespace FlowGraph
|