-
Notifications
You must be signed in to change notification settings - Fork 2
/
Compiler.cpp
426 lines (383 loc) · 11.1 KB
/
Compiler.cpp
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
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
#ifdef _DEBUG
#ifndef DBG_NEW
//#define DBG_NEW new ( _NORMAL_BLOCK , __FILE__ , __LINE__ )
//#define new DBG_NEW
#endif
#define _CRTDBG_MAP_ALLOC
#define _CRTDBG_MAP_ALLOC_NEW
#include <crtdbg.h>
#endif
#include <jetscript/Compiler.h>
#include <jetscript/Parser.h>
using namespace Jet;
CompilerContext::CompilerContext(void)
{
this->vararg = false;
this->isgenerator = false;
this->closures = 0;
this->parent = 0;
this->uuid = 0;
this->localindex = 0;
this->lastline = 0;
this->scope = new CompilerContext::Scope;
this->scope->level = 0;
this->scope->previous = this->scope->next = 0;
}
CompilerContext::~CompilerContext(void)
{
if (this->scope)
{
auto next = this->scope->next;
while (next)
{
auto tmp = next->next;
delete next;
next = tmp;
}
}
delete this->scope;
//delete functions
for (auto ii: this->functions)
delete ii.second;
}
void CompilerContext::PrintAssembly()
{
int index = 0;
for (unsigned int i = 0; i < this->out.size(); i++)
{
auto ins = this->out[i];
if (ins.type == InstructionType::Function)
{
printf("\n\nfunction %s definition\n%d arguments, %d locals, %d captures", ins.string, ins.a, ins.b, ins.c);
index = 0;
if (i+1 < this->out.size() && out[i+1].type == InstructionType::DebugLine)
{
++i;
printf(", %s line %.0lf", out[i].string, out[i].second);
}
continue;
}
else if (ins.type == InstructionType::Local)
{
printf("\nLocal\t%s", ins.string);
}
else if (ins.type == InstructionType::Capture)
{
printf("\nCapture\t%s", ins.string);
}
else if (ins.type == InstructionType::Label)
{
printf("\nLabel\t%s", ins.string);
}
else
{
if (ins.string)
printf("\n[%d]\t%-15s %-5.0lf %s", index++, Instructions[(int)ins.type], ins.second, ins.string);
else
printf("\n[%d]\t%-15s %-5d %.0lf", index++, Instructions[(int)ins.type], ins.first, ins.second);
}
if (i+1 < this->out.size() && out[i+1].type == InstructionType::DebugLine)
{
++i;
printf(" ; %s line %.0lf", out[i].string, out[i].second);
}
}
printf("\n");
}
std::vector<IntermediateInstruction> CompilerContext::Compile(BlockExpression* expr, const char* filename)
{
try
{
//may want to correct number of locals here
this->FunctionLabel("{Entry Point}", 0, 0, 0, this->vararg);
expr->Compile(this);
//add a return to signify end of global code
if (this->out[this->out.size()-1].type != InstructionType::Return)
{
this->Null();
this->Return();
}
this->Compile();
if (localindex > 255)
throw CompilerException(this->filename, this->lastline, "Too many locals: over 256 locals in function!");
if (closures > 255)
throw CompilerException(this->filename, this->lastline, "Too many closures: over 256 closures in function!");
//modify the entry point with number of locals
this->out[0].b = this->localindex;
this->out[0].c = this->closures;
this->out[0].d = this->vararg + this->isgenerator*2;
}
catch (CompilerException e)
{
//clean up the compiler
if (this->scope)
{
auto next = this->scope->next;
this->scope->next = 0;
while (next)
{
auto tmp = next->next;
delete next;
next = tmp;
}
}
this->scope->localvars.clear();
for (auto ii: this->functions)
delete ii.second;
this->functions.clear();
this->localindex = 0;
this->closures = 0;
throw e;
}
if (this->scope)
{
auto next = this->scope->next;
this->scope->next = 0;
while (next)
{
auto tmp = next->next;
delete next;
next = tmp;
}
this->scope->localvars.clear();
}
for (auto ii: this->functions)
delete ii.second;
this->functions.clear();
//add custom operators
this->localindex = 0;
this->closures = 0;
//this->PrintAssembly();
auto temp = std::move(this->out);
this->out.clear();
return std::move(temp);
}
bool CompilerContext::RegisterLocal(const std::string name)
{
//neeed to store locals in a contiguous array, even with different scopes
for (unsigned int i = 0; i < this->scope->localvars.size(); i++)
{
if (this->scope->localvars[i].name == name)
return false;
}
LocalVariable var;
var.local = this->localindex++;
var.name = name;
this->scope->localvars.push_back(var);
out.push_back(IntermediateInstruction(InstructionType::Local, var.name, 0));
return true;
}
void CompilerContext::BinaryOperation(TokenType operation)
{
switch (operation)
{
case TokenType::Plus:
case TokenType::AddAssign:
this->out.push_back(IntermediateInstruction(InstructionType::Add));
break;
case TokenType::Asterisk:
case TokenType::MultiplyAssign:
this->out.push_back(IntermediateInstruction(InstructionType::Mul));
break;
case TokenType::Minus:
case TokenType::SubtractAssign:
this->out.push_back(IntermediateInstruction(InstructionType::Sub));
break;
case TokenType::Slash:
case TokenType::DivideAssign:
this->out.push_back(IntermediateInstruction(InstructionType::Div));
break;
case TokenType::Modulo:
this->out.push_back(IntermediateInstruction(InstructionType::Modulus));
break;
case TokenType::Equals:
this->out.push_back(IntermediateInstruction(InstructionType::Eq));
break;
case TokenType::NotEqual:
this->out.push_back(IntermediateInstruction(InstructionType::NotEq));
break;
case TokenType::LessThan:
this->out.push_back(IntermediateInstruction(InstructionType::Lt));
break;
case TokenType::GreaterThan:
this->out.push_back(IntermediateInstruction(InstructionType::Gt));
break;
case TokenType::LessThanEqual:
this->out.push_back(IntermediateInstruction(InstructionType::LtE));
break;
case TokenType::GreaterThanEqual:
this->out.push_back(IntermediateInstruction(InstructionType::GtE));
break;
case TokenType::OrAssign:
case TokenType::BOr:
this->out.push_back(IntermediateInstruction(InstructionType::BOr));
break;
case TokenType::AndAssign:
case TokenType::BAnd:
this->out.push_back(IntermediateInstruction(InstructionType::BAnd));
break;
case TokenType::XorAssign:
case TokenType::Xor:
this->out.push_back(IntermediateInstruction(InstructionType::Xor));
break;
case TokenType::LeftShift:
this->out.push_back(IntermediateInstruction(InstructionType::LeftShift));
break;
case TokenType::RightShift:
this->out.push_back(IntermediateInstruction(InstructionType::RightShift));
break;
}
}
void CompilerContext::UnaryOperation(TokenType operation)
{
switch (operation)
{
case TokenType::Increment:
this->out.push_back(IntermediateInstruction(InstructionType::Incr));
break;
case TokenType::Decrement:
this->out.push_back(IntermediateInstruction(InstructionType::Decr));
break;
case TokenType::Minus:
this->out.push_back(IntermediateInstruction(InstructionType::Negate));
break;
case TokenType::BNot:
this->out.push_back(IntermediateInstruction(InstructionType::BNot));
break;
}
}
CompilerContext* CompilerContext::AddFunction(std::string name, unsigned int args, bool vararg)
{
//push instruction that sets the function
//todo, may need to have functions in other instruction code sets
CompilerContext* newfun = new CompilerContext();
//insert this into my list of functions
std::string fname = name+this->GetUUID();
newfun->arguments = args;
newfun->uuid = this->uuid;
newfun->parent = this;
newfun->vararg = vararg;
this->functions[fname] = newfun;
//store the function in the variable
this->LoadFunction(fname);
return newfun;
};
void CompilerContext::FinalizeFunction(CompilerContext* c)
{
this->uuid = c->uuid + 1;
//move upvalues
for (auto ii: this->functions)//check all children
{
for (auto& i: ii.second->captures)
{
if (i.second.uploaded == false)
{
i.second.uploaded = true;
//printf("closed %d %d\n", i.second.captureindex, i.second.localindex);
out.push_back(IntermediateInstruction(InstructionType::CInit,i.second.localindex/*ptr->localvars[i].local*/, i.second.captureindex/*ptr->localvars[i].capture*/));
}
}
}
}
void CompilerContext::Load(const std::string variable)
{
Scope* ptr = this->scope;
while (ptr)
{
//look for var in locals
for (unsigned int i = 0; i < ptr->localvars.size(); i++)
{
if (ptr->localvars[i].name == variable)
{
//printf("We found loading of a local var: %s at level %d, index %d\n", variable.c_str(), ptr->level, ptr->localvars[i].first);
out.push_back(IntermediateInstruction(InstructionType::LLoad, ptr->localvars[i].local, 0));//i, ptr->level));
return;//exit the loops we found it
}
}
ptr = ptr->previous;
}
int level = 0;
auto cur = this->parent;
auto prev = this;
while(cur)
{
ptr = cur->scope;
while (ptr)
{
//look for var in locals
for (unsigned int i = 0; i < ptr->localvars.size(); i++)
{
if (ptr->localvars[i].name == variable)
{
//printf("We found loading of a captured var: %s at level %d, index %d\n", variable.c_str(), level, ptr->localvars[i].first);
auto cpt = prev->captures.find(ptr->localvars[i].name);
if (cpt == prev->captures.end())
{
prev->captures[ptr->localvars[i].name] = Capture(level, ptr->localvars[i].local, prev->closures++);
cpt = prev->captures.find(ptr->localvars[i].name);
out.push_back(IntermediateInstruction(InstructionType::Capture, ptr->localvars[i].name, 0));
}
out.push_back(IntermediateInstruction(InstructionType::CLoad, cpt->second.captureindex/*ptr->localvars[i].capture*/, level));//i, ptr->level));
return;//exit the loops we found it
}
}
ptr = ptr->previous;
}
level--;
prev = cur;
cur = cur->parent;
}
out.push_back(IntermediateInstruction(InstructionType::Load, variable));
}
void CompilerContext::Store(const std::string variable)
{
//look up if I am a local or global
Scope* ptr = this->scope;
while (ptr)
{
//look for var in locals
for (unsigned int i = 0; i < ptr->localvars.size(); i++)
{
if (ptr->localvars[i].name == variable)
{
//printf("We found storing of a local var: %s at level %d, index %d\n", variable.c_str(), ptr->level, ptr->localvars[i].first);
out.push_back(IntermediateInstruction(InstructionType::LStore, ptr->localvars[i].local, 0));//i, ptr->level));
return;//exit the loops we found it
}
}
ptr = ptr->previous;
}
int level = 0;
auto cur = this->parent;
auto prev = this;
while(cur)
{
ptr = cur->scope;
while (ptr)
{
//look for var in locals
for (unsigned int i = 0; i < ptr->localvars.size(); i++)
{
if (ptr->localvars[i].name == variable)
{
//printf("We found storing of a captured var: %s at level %d, index %d\n", variable.c_str(), level, ptr->localvars[i].first);
//exit the loops we found it
auto cpt = prev->captures.find(ptr->localvars[i].name);
if (cpt == prev->captures.end())
{
prev->captures[ptr->localvars[i].name] = Capture(level, ptr->localvars[i].local, prev->closures++);
cpt = prev->captures.find(ptr->localvars[i].name);
out.push_back(IntermediateInstruction(InstructionType::Capture, ptr->localvars[i].name, 0));
}
out.push_back(IntermediateInstruction(InstructionType::CStore, cpt->second.captureindex /*ptr->localvars[i].capture*/, level));//i, ptr->level));
return;
}
}
ptr = ptr->previous;
}
level--;
prev = cur;
cur = cur->parent;
}
out.push_back(IntermediateInstruction(InstructionType::Store, variable));
}