-
Notifications
You must be signed in to change notification settings - Fork 0
/
ir.h
308 lines (239 loc) · 7.84 KB
/
ir.h
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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
#pragma once
#include <ciso646>
#include <cassert>
#include <cstdlib>
#include <cstdint>
#include <vector>
#include <set>
#include <iostream>
#include <iomanip>
/** General notes
-------------
If it is struct, any virtuality is not allowed, if it is class, anything is allowed.
*/
/** Slim instructions
Slim instructions are mere structs that offer no-cost nicer abstractions over the data in the code vector. They should not do anything but accessing the data (all data is constant) and act as the lowest level of the API.
A pointer to an instruction should also be used as a location handle for an analysis.
*/
struct Instruction {
typedef uint16_t BranchTarget;
enum class Opcode : uint16_t {
Push,
Add,
Sub,
Call,
Br,
};
size_t size() const;
Opcode const opcode;
static char const * opcodeToStr(Opcode opcode) {
switch (opcode) {
case Opcode::Push:
return "Push";
case Opcode::Add:
return "Add";
case Opcode::Sub:
return "Sub";
case Opcode::Call:
return "Call";
case Opcode::Br:
return "Br";
default:
return "???";
}
}
struct Push;
struct Add;
struct Sub;
struct Call;
struct Br;
protected:
Instruction(Opcode opcode) :
opcode(opcode) {}
private:
void * operator new (size_t) = delete;
void * operator new[](size_t) = delete;
void operator delete (void *) = delete;
void operator delete[](void *) = delete;
};
struct Instruction::Push : public Instruction {
uint16_t const index;
Push(uint16_t index) :
Instruction(Opcode::Push),
index(index) {}
friend std::ostream & operator << (std::ostream & stream, Push const & ins) {
stream << "PUSH " << ins.index;
return stream;
}
};
struct Instruction::Add : public Instruction {
Add() :
Instruction(Opcode::Add) {}
friend std::ostream & operator << (std::ostream & stream, Add const & ins) {
stream << "ADD";
return stream;
}
};
struct Instruction::Sub : public Instruction {
Sub() :
Instruction(Opcode::Sub) {}
friend std::ostream & operator << (std::ostream & stream, Sub const & ins) {
stream << "SUB";
return stream;
}
};
struct Instruction::Call : public Instruction {
uint8_t const arguments;
Call(uint8_t arguments) :
Instruction(Opcode::Call),
arguments(arguments) {}
friend std::ostream & operator << (std::ostream & stream, Call const & ins) {
stream << "CALL " << ins.arguments;
return stream;
}
};
struct Instruction::Br : public Instruction {
BranchTarget const target;
Br(BranchTarget target) :
Instruction(Opcode::Br),
target(target) {}
friend std::ostream & operator << (std::ostream & stream, Br const & ins) {
stream << "BR " << ins.target;
return stream;
}
};
// TODO this can be autogenerated
inline size_t Instruction::size() const {
switch (opcode) {
case Opcode::Push:
return sizeof(Instruction::Push);
case Opcode::Add:
return sizeof(Instruction::Add);
case Opcode::Sub:
return sizeof(Instruction::Sub);
case Opcode::Call:
return sizeof(Instruction::Call);
case Opcode::Br:
return sizeof(Instruction::Br);
default:
assert(false and "we have tested all instructions already");
}
}
class Code;
/** Constant pool.
Currently has only integers in it. In reality, integers will likely be immediate arguments to the instructions, and constant pool would contain more complex data such as SEXPs, symbols, and source code references.
The constant pool is shared amongst all versions of the code for particular function - the idea is that the first, least optimized version will likely require all constants (the most symbols, source refs and complex constants), while the optimized versions will most likely only use subsets.
*/
class ConstantPool {
public:
int operator [] (size_t index) const {
return values_[index];
}
size_t append(int value) {
// TODO this should of course check for duplicates
size_t result = values_.size();
values_.push_back(value);
return result;
}
private:
friend class Code;
ConstantPool() = default;
std::vector<int> values_;
std::set<Code*> versions_;
void attach(Code * version) {
versions_.insert(version);
}
void detach(Code * version) {
versions_.erase(version);
}
};
/** The code buffer.
This should be so much more than a simple vector as it is now.
TODO Revisit this and make more performant.
*/
class Code {
public:
/** First version constructor which also creates the constant pool.
*/
Code() :
pool(*new ConstantPool()) {
pool.attach(this);
}
template<typename INS>
INS const * at(size_t index) const {
return reinterpret_cast<INS const *>(buffer_.data() + index);
}
Instruction const * operator [] (size_t index) const {
return reinterpret_cast<Instruction const*>(buffer_.data() + index);
}
// TODO this should return the pointer to the instruction, to ease building but for that I need more clever code buffer
template<typename INS>
void append(INS const & ins) {
size_t from = buffer_.size();
buffer_.resize(from + sizeof(INS));
// memcpy so that we do not have to bother with copy constructors and assignments
memcpy(buffer_.data() + from, &ins, sizeof(INS));
}
class iterator {
public:
iterator(iterator const & from) = default;
iterator & operator = (iterator const & from) = default;
template<typename INS>
INS * at() const {
return reinterpret_cast<INS *>(current_);
}
Instruction & operator * () {
return *reinterpret_cast<Instruction *>(current_);
}
Instruction * operator -> () {
return reinterpret_cast<Instruction *>(current_);
}
iterator & operator ++ () {
current_ += at<Instruction>()->size();
return *this;
}
bool operator != (iterator const & other) const {
return current_ != other.current_;
}
private:
friend class Code;
iterator(char * start) :
current_(start) {}
char * current_;
};
iterator begin() {
return iterator(buffer_.data());
}
iterator end() {
return iterator(buffer_.data() + buffer_.size());
}
ConstantPool & pool;
private:
friend std::ostream & operator << (std::ostream & stream, Code & code) {
for (auto i = code.begin(), e = code.end(); i != e; ++i) {
stream << std::setw(5) << reinterpret_cast<char*>(i.at<Instruction>()) - code.buffer_.data() << " ";
switch (i->opcode) {
case Instruction::Opcode::Push:
stream << *i.at<Instruction::Push>();
break;
case Instruction::Opcode::Add:
stream << *i.at<Instruction::Add>();
break;
case Instruction::Opcode::Sub:
stream << *i.at<Instruction::Sub>();
break;
case Instruction::Opcode::Call:
stream << *i.at<Instruction::Call>();
break;
case Instruction::Opcode::Br:
stream << *i.at<Instruction::Br>();
break;
default:
stream << "??? " << static_cast<uint16_t>(i->opcode);
}
stream << std::endl;
}
return stream;
}
std::vector<char> buffer_;
};