-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtypes.h
353 lines (287 loc) · 9.58 KB
/
types.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
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
/*
* types.h
*
* Defines the Value, Constant, and Function types used in the interpreter
*/
#pragma once
#include "exception.h"
#include "instructions.h"
#include <cstdint>
#include <iostream>
#include <map>
#include <memory>
#include <string>
#include <vector>
#include "gc/gc.h"
#include "opt/opt_tag_ptr.h"
using namespace std;
using experimental::nullopt;
#define LOG(msg) { if (false) cerr << msg << endl; }
class Frame;
class Collectable;
class MachineCodeFunction;
struct Value : public Collectable {
// Abstract class for program values that can be stored on a frame's
// operand stack
virtual ~Value() {}
// instance function that returns type of value as a string
virtual string type() = 0;
// instance function that returns printable representation of this value's data
virtual string toString() = 0;
// instance function to determine whether this value is equal to another one
virtual bool equals(Value* other) = 0;
// helper function to dynamically cast value to a specific subclass type
// and raise an IllegalCastException if the cast fails
template <typename T>
T* cast() {
auto val = dynamic_cast<T*>(this);
if (val == NULL) {
throw IllegalCastException("cannot cast type " + this->type() + " to " + T::typeS);
}
return val;
}
};
struct Constant: public Value {
// Abstract class for constant program values
virtual ~Constant() {};
static const string typeS;
};
struct Function : public Value {
// Class for function type; produced by bytecode compiler
// functions defined within this function (but not inside nested functions)
vector<Function*> functions_;
// constants used by the instructions within this function (but not inside nested functions)
vector<tagptr_t> constants_;
// number of parameters to the function
int32_t parameter_count_;
// list of local variables
// note: the first parameter_count_ variables are the function's parameters
// in their order as given in the paraemter list
vector<string> local_vars_;
// list of local variables accessed by reference (LocalReference)
vector<string> local_reference_vars_;
// list of the names of non-global and non-local variables accessed by the function
vector<string> free_vars_;
// list of global variable and field names used inside the function
vector<string> names_;
// map of label indices to instruction indices
map<int, int> labels_;
// store a pointer to the compiled version
MachineCodeFunction* mcf = nullptr;
BcInstructionList instructions;
Function() {};
virtual ~Function() {};
Function(vector<Function*> functions_,
vector<tagptr_t> constants_,
int32_t parameter_count_,
vector<string> local_vars_,
vector<string> local_reference_vars_,
vector<string> free_vars_,
vector<string> names_,
BcInstructionList instructions):
functions_(functions_),
constants_(constants_),
parameter_count_(parameter_count_),
local_vars_(local_vars_),
local_reference_vars_(local_reference_vars_),
free_vars_(free_vars_),
names_(names_),
labels_(),
instructions(instructions) {};
Function(vector<Function*> functions_,
vector<tagptr_t> constants_,
int32_t parameter_count_,
vector<string> local_vars_,
vector<string> local_reference_vars_,
vector<string> free_vars_,
vector<string> names_,
map<int, int> labels_,
BcInstructionList instructions):
functions_(functions_),
constants_(constants_),
parameter_count_(parameter_count_),
local_vars_(local_vars_),
local_reference_vars_(local_reference_vars_),
free_vars_(free_vars_),
names_(names_),
labels_(labels_),
instructions(instructions) {};
string toString();
bool equals(Value* other);
static const string typeS;
string type() {
return "Function";
}
void follow(CollectedHeap& heap) override;
size_t getSize() override;
};
struct ValWrapper: public Value {
// Class for reference variables
tagptr_t ptr;
ValWrapper() {}
ValWrapper(tagptr_t ptr): ptr(ptr) {};
virtual ~ValWrapper() {}
static const string typeS;
string type() {
return "ValWrapper";
}
string toString();
bool equals(Value* other);
void follow(CollectedHeap& heap) override;
size_t getSize() override;
};
struct None : public Constant {
// Class for None type
virtual ~None() {}
static const string typeS;
string type() {
return "None";
}
string toString();
bool equals(Value* other);
void follow(CollectedHeap& heap) override;
size_t getSize() override;
};
struct Integer : public Constant {
// TODO: delete
// Class for integer type
int32_t value;
Integer(int32_t value) : value(value) {}
virtual ~Integer() {}
static const string typeS;
string type() {
return "Integer";
}
string toString();
bool equals(Value* other);
void follow(CollectedHeap& heap) override;
size_t getSize() override;
};
struct String : public Constant {
// TODO: delete
// Class for string type
string value;
String(string value): value(value) {};
virtual ~String() {};
static const string typeS;
string type() {
return "String";
}
string toString();
bool equals(Value* other);
void follow(CollectedHeap& heap) override;
size_t getSize() override;
};
struct Boolean : public Constant{
// TODO: delete
// Class for boolean type
bool value;
Boolean(bool value): value(value) {};
virtual ~Boolean() {};
static const string typeS;
string type() {
return "Boolean";
}
string toString();
bool equals(Value* other);
void follow(CollectedHeap& heap) override;
size_t getSize() override;
};
struct Record : public Constant {
// Class for record type (note that this is mutable)
map<string, tagptr_t> value;
tagptr_t get(string key);
void set(string key, tagptr_t value, CollectedHeap& collector);
virtual ~Record() {}
string toString();
bool equals(Value* other);
static const string typeS;
string type() {
return "Record";
}
void follow(CollectedHeap& heap) override;
size_t getSize() override;
};
struct Closure: public Constant {
// Class for closure type
// list of reference variables, in the same order as listed in the function's
// free_vars_ list
vector<ValWrapper*> refs;
// function that the closure is for
Function* func;
Closure(vector<ValWrapper*> refs, Function* func):
refs(refs), func(func) {};
virtual ~Closure() {}
static const string typeS;
string type() {
return "Closure";
}
string toString();
bool equals(Value* other);
void follow(CollectedHeap& heap) override;
size_t getSize() override;
};
class NativeFunction : public Function {
// Abstract class for native functions
public:
NativeFunction(vector<Function*> functions_,
vector<tagptr_t> constants_,
int32_t parameter_count_,
vector<string> local_vars_,
vector<string> local_reference_vars_,
vector<string> free_vars_,
vector<string> names_,
BcInstructionList instructions):
Function(functions_, constants_, parameter_count_,
local_vars_, local_reference_vars_, free_vars_,
names_, instructions) {};
virtual tagptr_t evalNativeFunction(Frame& currentFrame, CollectedHeap& ch) = 0;
};
class PrintNativeFunction : public NativeFunction {
// Class for print native function
public:
PrintNativeFunction(vector<Function*> functions_,
vector<tagptr_t> constants_,
int32_t parameter_count_,
vector<string> local_vars_,
vector<string> local_reference_vars_,
vector<string> free_vars_,
vector<string> names_,
BcInstructionList instructions):
NativeFunction(functions_, constants_, parameter_count_,
local_vars_, local_reference_vars_, free_vars_,
names_, instructions) {};
tagptr_t evalNativeFunction(Frame& currentFrame, CollectedHeap& ch);
};
class InputNativeFunction : public NativeFunction {
// Class for input native function
public:
InputNativeFunction(vector<Function*> functions_,
vector<tagptr_t> constants_,
int32_t parameter_count_,
vector<string> local_vars_,
vector<string> local_reference_vars_,
vector<string> free_vars_,
vector<string> names_,
BcInstructionList instructions):
NativeFunction(functions_, constants_, parameter_count_,
local_vars_, local_reference_vars_, free_vars_,
names_, instructions) {};
tagptr_t evalNativeFunction(Frame& currentFrame, CollectedHeap& ch);
};
class IntcastNativeFunction : public NativeFunction {
// Class for intcast native function
public:
IntcastNativeFunction(vector<Function*> functions_,
vector<tagptr_t> constants_,
int32_t parameter_count_,
vector<string> local_vars_,
vector<string> local_reference_vars_,
vector<string> free_vars_,
vector<string> names_,
BcInstructionList instructions):
NativeFunction(functions_, constants_, parameter_count_,
local_vars_, local_reference_vars_, free_vars_,
names_, instructions) {};
tagptr_t evalNativeFunction(Frame& currentFrame, CollectedHeap& ch);
};