-
Notifications
You must be signed in to change notification settings - Fork 0
/
MonarchWalker.g
487 lines (436 loc) · 16.8 KB
/
MonarchWalker.g
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
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
tree grammar MonarchWalker;
options
{
tokenVocab=Monarch;
ASTLabelType=pANTLR3_BASE_TREE;
language=C;
output=AST;
}
scope Symbols
{
pANTLR3_HASH_TABLE table;
}
scope Function
{
LLVMValueRef ref;
}
scope BreakContinue
{
LLVMBasicBlockRef break_block;
LLVMBasicBlockRef continue_block;
pANTLR3_STRING name; // label
}
@includes
{
#include "Codegen.h"
}
@members
{
static void ANTLR3_CDECL VariableDelete(void *var){}
LLVMValueRef getOuter(pMonarchWalker ctx, LLVMValueRef function)
{
for (int i = (int)SCOPE_SIZE(Function) - 1; i > 0; i--)
{
SCOPE_TYPE(Function) scope = (SCOPE_TYPE(Function))SCOPE_INSTANCE(Function, i);
if (scope->ref == function)
{
scope = (SCOPE_TYPE(Function))SCOPE_INSTANCE(Function, i - 1);
return scope->ref;
}
}
return NULL;
}
LLVMBasicBlockRef getBreakBlock(pMonarchWalker ctx, pANTLR3_STRING name)
{
for (int i = (int)SCOPE_SIZE(BreakContinue) - 1; i >= 0; i--)
{
SCOPE_TYPE(BreakContinue) scope = (SCOPE_TYPE(BreakContinue))SCOPE_INSTANCE(BreakContinue, i);
if (!name || scope->name == name)
return scope->break_block;
}
return NULL;
}
LLVMBasicBlockRef getContinueBlock(pMonarchWalker ctx, pANTLR3_STRING name)
{
for (int i = (int)SCOPE_SIZE(BreakContinue) - 1; i >= 0; i--)
{
SCOPE_TYPE(BreakContinue) scope = (SCOPE_TYPE(BreakContinue))SCOPE_INSTANCE(BreakContinue, i);
if (!name || scope->name == name)
return scope->continue_block;
}
return NULL;
}
LLVMValueRef getSymbol(pMonarchWalker ctx, pANTLR3_STRING name)
{
for (int i = (int)SCOPE_SIZE(Symbols) - 1; i >= 0; i--)
{
SCOPE_TYPE(Symbols) symbols = (SCOPE_TYPE(Symbols))SCOPE_INSTANCE(Symbols, i);
void* symbol = symbols->table->get(symbols->table, name->chars);
if (symbol)
return (LLVMValueRef)symbol;
}
return NULL;
}
ANTLR3_BOOLEAN symbolDefined(pMonarchWalker ctx, pANTLR3_STRING name)
{
for (int i = (int)SCOPE_SIZE(Symbols) - 1; i >= 0; i--)
{
SCOPE_TYPE(Symbols) symbols = (SCOPE_TYPE(Symbols))SCOPE_INSTANCE(Symbols, i);
if (symbols->table->get(symbols->table, name->chars))
return ANTLR3_TRUE;
}
return ANTLR3_FALSE;
}
void addSymbol(pMonarchWalker ctx, LLVMValueRef value, const char* name)
{
SCOPE_TYPE(Symbols) symbols = SCOPE_TOP(Symbols);
symbols->table->put(symbols->table, (void*)name, value, VariableDelete);
}
void defineSymbol(pMonarchWalker ctx, pANTLR3_STRING name, LLVMTypeRef type)
{
if (!symbolDefined(ctx, name))
{
LLVMValueRef value = CreateValue((const char *)name->chars, type);
addSymbol(ctx, value, (const char*)name->chars);
}
}
void ANTLR3_CDECL freeTable(SCOPE_TYPE(Symbols) symbols)
{
symbols->table->free(symbols->table);
}
}
program
scope Symbols, Function; // global scope
@init
{
$Symbols::table = antlr3HashTableNew(11);
SCOPE_TOP(Symbols)->free = freeTable;
InitCodegen();
$Function::ref = CreateFunction("main", 0);
LLVMBasicBlockRef entry = CreateBlock($Function::ref, "entry");
BeginBlock(entry);
}
@after
{
BuildReturn($Function::ref);
TermCodegen($Function::ref);
}
: statements
;
arrayLiteral returns [LLVMValueRef value]
@init
{
ANTLR3_INT32 indx = 0;
}
: ^( ARRAY { $value = CreateArray(); } //!!ARL: Store off count to init size?
( ^( EXPR ( r=assignment_expression
{
LLVMValueRef lvalue = PutArray($value, ConstInt(indx++), GetType($r.value));
Assignment(lvalue, $r.value);
}
)* ) )? )
;
block[const char* name] returns [LLVMValueRef result, LLVMBasicBlockRef ref]
scope Symbols; // specify at higher levels instead?
@init
{
$Symbols::table = antlr3HashTableNew(11);
SCOPE_TOP(Symbols)->free = freeTable;
$ref = CreateBlock(SCOPE_TOP(Function)->ref, name);
BeginBlock($ref);
}
: ^( BLOCK r=statements ) { $result = $r.result; }
;
breakStatement
: ^( 'break' label=NameLiteral? )
{
JumpTo(getBreakBlock(ctx, $label ? $label.text : NULL));
// start a new basic block to catch any additional code that might get generated
// basic blocks cannot have multiple branches (e.g. if we break in an if statement
// a second branch will be added to endif).
BeginBlock(CreateBlock(SCOPE_TOP(Function)->ref, "unreachable"));
}
;
continueStatement
: ^( 'continue' label=NameLiteral? )
{
JumpTo(getContinueBlock(ctx, $label ? $label.text : NULL));
// start a new basic block to catch any additional code that might get generated
// basic blocks cannot have multiple branches (e.g. if we break in an if statement
// a second branch will be added to endif).
BeginBlock(CreateBlock(SCOPE_TOP(Function)->ref, "unreachable"));
}
;
caseClause
: ^( 'case' expression statements )
;
disruptiveStatement
: returnStatement
| breakStatement
| continueStatement
| throwStatement
;
//!!ARL: I'm tempted to leave this out entirely.
doStatement
: ^( DO_WHILE block["do"] cond=expression ) { DoWhile(SCOPE_TOP(Function)->ref, $cond.value, $block.ref); }
;
expressionStatement
: expression
;
expression returns [LLVMValueRef value]
: ^( EXPR first=assignment_expression assignment_expression* ) { $value = $first.value; }
;
// why is this its own rule?
constant_expression
: conditional_expression
;
assignment_expression returns [LLVMValueRef value]
options {backtrack=true;}
: ^( '=' r=assignment_expression lvalue[GetType($r.value)] ) { $value = Assignment($lvalue.value, $r.value); }
| ^( '+=' r=assignment_expression lvalue[GetType($r.value)] ) { LLVMValueRef tmp = AddValues($lvalue.value, $r.value); $value = Assignment($lvalue.value, tmp); }
| ^( '-=' r=assignment_expression lvalue[GetType($r.value)] ) { LLVMValueRef tmp = SubValues($lvalue.value, $r.value); $value = Assignment($lvalue.value, tmp); }
| ^( '*=' r=assignment_expression lvalue[GetType($r.value)] ) { LLVMValueRef tmp = MulValues($lvalue.value, $r.value); $value = Assignment($lvalue.value, tmp); }
| ^( '/=' r=assignment_expression lvalue[GetType($r.value)] ) { LLVMValueRef tmp = DivValues($lvalue.value, $r.value); $value = Assignment($lvalue.value, tmp); }
| conditional_expression { $value = $conditional_expression.value; }
;
lvalue[LLVMTypeRef _type] returns [LLVMValueRef value]
scope { LLVMTypeRef type; }
@init { $lvalue::type = $_type; }
: unary_expression[ANTLR3_TRUE] { $value = $unary_expression.value; }
;
conditional_expression returns [LLVMValueRef value]
@init
{
LLVMValueRef results[] = { NULL, NULL };
LLVMBasicBlockRef blocks[] = { NULL, NULL, NULL };
LLVMValueRef function = SCOPE_TOP(Function)->ref;
}
: logical_or_expression { $value = $logical_or_expression.value; }
| ^( '?' cond=logical_or_expression { blocks[2]=CreateBlock(function, "endif"); }
{ blocks[0]=CreateBlock(function, "iftrue"); BeginBlock(blocks[0]); } iftrue=expression { results[0]=$iftrue.value; JumpTo(blocks[2]); }
{ blocks[1]=CreateBlock(function, "iffalse"); BeginBlock(blocks[1]); } iffalse=conditional_expression { results[1]=$iffalse.value; JumpTo(blocks[2]); } )
{ $value = IfElse(function, $cond.value, results, blocks); }
;
logical_or_expression returns [LLVMValueRef value]
: logical_and_expression { $value = $logical_and_expression.value; }
| ^( '||' lhs=logical_or_expression rhs=logical_and_expression ){$lhs.value && $rhs.value}? { $value = HASEXCEPTION() ? NULL : LogicOr($lhs.value, $rhs.value); }
;
logical_and_expression returns [LLVMValueRef value]
: equality_expression { $value = $equality_expression.value; }
| ^( '&&' lhs=logical_and_expression rhs=equality_expression ){$lhs.value && $rhs.value}? { $value = HASEXCEPTION() ? NULL : LogicAnd($lhs.value, $rhs.value); }
;
equality_expression returns [LLVMValueRef value]
: relational_expression { $value = $relational_expression.value; }
| ^( '==' lhs=equality_expression rhs=relational_expression ){$lhs.value && $rhs.value}? { $value = HASEXCEPTION() ? NULL : CmpEQ($lhs.value, $rhs.value); }
| ^( '!=' lhs=equality_expression rhs=relational_expression ){$lhs.value && $rhs.value}? { $value = HASEXCEPTION() ? NULL : CmpNE($lhs.value, $rhs.value); }
;
relational_expression returns [LLVMValueRef value]
: additive_expression { $value = $additive_expression.value; }
| ^( '<' lhs=relational_expression rhs=additive_expression ){$lhs.value && $rhs.value}? { $value = HASEXCEPTION() ? NULL : CmpLT($lhs.value, $rhs.value); }
| ^( '>' lhs=relational_expression rhs=additive_expression ){$lhs.value && $rhs.value}? { $value = HASEXCEPTION() ? NULL : CmpGT($lhs.value, $rhs.value); }
| ^( '<=' lhs=relational_expression rhs=additive_expression ){$lhs.value && $rhs.value}? { $value = HASEXCEPTION() ? NULL : CmpLE($lhs.value, $rhs.value); }
| ^( '>=' lhs=relational_expression rhs=additive_expression ){$lhs.value && $rhs.value}? { $value = HASEXCEPTION() ? NULL : CmpGE($lhs.value, $rhs.value); }
;
additive_expression returns [LLVMValueRef value]
: multiplicative_expression { $value = $multiplicative_expression.value; }
| ^( '+' lhs=additive_expression rhs=multiplicative_expression ){$lhs.value && $rhs.value}? { $value = HASEXCEPTION() ? NULL : AddValues($lhs.value, $rhs.value); }
| ^( '-' lhs=additive_expression rhs=multiplicative_expression ){$lhs.value && $rhs.value}? { $value = HASEXCEPTION() ? NULL : SubValues($lhs.value, $rhs.value); }
;
multiplicative_expression returns [LLVMValueRef value]
: unary_expression[ANTLR3_FALSE] { $value = $unary_expression.value; }
| ^( '*' lhs=multiplicative_expression rhs=unary_expression[ANTLR3_FALSE] ){$lhs.value && $rhs.value}? { $value = HASEXCEPTION() ? NULL : MulValues($lhs.value, $rhs.value); }
| ^( '/' lhs=multiplicative_expression rhs=unary_expression[ANTLR3_FALSE] ){$lhs.value && $rhs.value}? { $value = HASEXCEPTION() ? NULL : DivValues($lhs.value, $rhs.value); }
| ^( '%' lhs=multiplicative_expression rhs=unary_expression[ANTLR3_FALSE] ){$lhs.value && $rhs.value}? { $value = HASEXCEPTION() ? NULL : ModValues($lhs.value, $rhs.value); }
;
unary_expression[ANTLR3_BOOLEAN lvalue] returns [LLVMValueRef value]
: postfix_expression[$lvalue] { $value = $postfix_expression.value; }
| '++' r=unary_expression[ANTLR3_TRUE] { $value = IncrementValue($r.value); }
| '--' r=unary_expression[ANTLR3_TRUE] { $value = DecrementValue($r.value); }
| '+' r=unary_expression[ANTLR3_FALSE] { $value = $r.value; }
| '-' r=unary_expression[ANTLR3_FALSE] { $value = NegateValue($r.value); }
| '!' r=unary_expression[ANTLR3_FALSE] { $value = InvertValue($r.value); }
| 'typeof' r=unary_expression[ANTLR3_FALSE]
| 'delete' r=unary_expression[ANTLR3_FALSE] { $value = DeleteValue($r.value); }
| 'print' r=unary_expression[ANTLR3_FALSE] { $value = PrintValue($r.value); }
;
postfix_expression[ANTLR3_BOOLEAN lvalue] returns [LLVMValueRef value]
@init
{
LLVMValueRef args[8]; //!!ARL: Store off count in parser for dynamic allocation
ANTLR3_UINT32 count = 0;
}
: primary_expression[$lvalue] { $value = $primary_expression.value; }
| ^( INDX array=postfix_expression[ANTLR3_FALSE] indx=expression )
{
if (SCOPE_SIZE(lvalue) > 0)
{
$value = PutArray($array.value, $indx.value, SCOPE_TOP(lvalue)->type);
}
else
{
$value = GetArray($array.value, $indx.value);
}
if (!$lvalue) $value = LoadValue($value);
}
| ^( CALL function=postfix_expression[ANTLR3_FALSE]
( ^( EXPR ( r=assignment_expression { args[count++] = $r.value; } )* ) )? )
{ $value = CallFunction($function.value, args, count); }
| ^( '.' postfix_expression[ANTLR3_FALSE] NameLiteral )
| ^( POSTINC postfix_expression[ANTLR3_FALSE] )
| ^( POSTDEC postfix_expression[ANTLR3_FALSE] )
;
//!!ARL: Don't need to pass lvalue bool if we can check rule scope size,
// however, we still need to avoid LoadValue when working with pre-incr/decr.
primary_expression[ANTLR3_BOOLEAN lvalue] returns [LLVMValueRef value]
@init { $value = NULL; }
: NameLiteral {$lvalue || symbolDefined(ctx, $NameLiteral.text)}?
{
if (HASEXCEPTION()) goto ruleprimary_expressionEx; // this should be generated automatically
if (SCOPE_SIZE(lvalue) > 0) defineSymbol(ctx, $NameLiteral.text, SCOPE_TOP(lvalue)->type);
$value = getSymbol(ctx, $NameLiteral.text);
if (!$lvalue && IsPointer($value)) $value = LoadValue($value);
}
| literal { $value = $literal.value; }
| ^( NESTED expression ) { $value = $expression.value; }
;
forStatement
scope Symbols, BreakContinue;
@init
{
$Symbols::table = antlr3HashTableNew(11);
SCOPE_TOP(Symbols)->free = freeTable;
LLVMValueRef function = SCOPE_TOP(Function)->ref;
LLVMBasicBlockRef blocks[] = {
CreateBlock(function, "for_init"),
CreateBlock(function, "for_cond"),
CreateBlock(function, "for_incr"),
NULL, // for_loop - created below
CreateBlock(function, "endfor")
};
$BreakContinue::continue_block = blocks[2]; // jump to increment on continue
$BreakContinue::break_block = blocks[4]; // jump to end on break
$BreakContinue::name = NULL; // pass thru?
}
: ^( 'for'
( { BeginBlock(blocks[0]); } ^( INIT expressionStatement ) )?
( { BeginBlock(blocks[1]); } ^( COND cond=expression ) )?
( { BeginBlock(blocks[2]); } ^( INCR expressionStatement ) )?
// ( ^( VAR var=NameLiteral obj=expression ) )?
( block["for_loop"] { blocks[3]=$block.ref; }
| { blocks[3]=CreateBlock(function, "for_loop"); BeginBlock(blocks[3]); } ^( STAT statement )
)
) { ForLoop(function, $cond.tree ? $cond.value : NULL, blocks); }
;
functionLiteral returns [LLVMValueRef value]
scope Symbols, Function;
@init
{
$Symbols::table = antlr3HashTableNew(11);
SCOPE_TOP(Symbols)->free = freeTable;
const char* function_name = "";
}
@after
{
BuildReturn($Function::ref);
ContinueFunction(getOuter(ctx, $Function::ref)); //!!ARL: Should maybe implement our own push/pop for functions (rather than using scopes).
}
: ^( FUNC ( name=NameLiteral { function_name = $name ? (const char *)$name.text->chars : ""; } )?
^( PARAMS args+=NameLiteral* )
{
ANTLR3_UINT32 count = $args ? $args->count : 0;
$Function::ref = CreateFunction(function_name, count);
for (int i = 0; i < count; i++)
{
pANTLR3_BASE_TREE arg = (pANTLR3_BASE_TREE)$args->get($args, i);
const char* name = (const char *)arg->getText(arg)->chars;
LLVMValueRef value = GetParam($Function::ref, i, name);
addSymbol(ctx, value, name);
}
addSymbol(ctx, $Function::ref, function_name); //!!ARL: Add at parent scope?
$value = $Function::ref;
}
block[function_name] )
;
ifStatement
@init
{
LLVMValueRef results[] = { NULL, NULL };
LLVMBasicBlockRef blocks[] = { NULL, NULL, NULL };
LLVMValueRef function = SCOPE_TOP(Function)->ref;
}
: ^( COND cond=expression { blocks[2]=CreateBlock(function, "endif"); }
( iftrue=block["iftrue"] { results[0]=$iftrue.result; blocks[0]=$iftrue.ref; JumpTo(blocks[2]); }
| { blocks[0]=CreateBlock(function, "iftrue"); BeginBlock(blocks[0]); } ^( STAT st=statement ) { results[0]=$st.result; JumpTo(blocks[2]); }
)
( iffalse=block["iffalse"] { results[1]=$iffalse.result; blocks[1]=$iffalse.ref; JumpTo(blocks[2]); }
| { blocks[1]=CreateBlock(function, "iffalse"); BeginBlock(blocks[1]); } ^( STAT sf=statement ) { results[1]=$sf.result; JumpTo(blocks[2]); }
)?
) { IfElse(function, $cond.value, results, blocks); }
;
literal returns [LLVMValueRef value]
: NumberLiteral
{
pANTLR3_STRING s = $NumberLiteral.text;
$value = ConstInt(s->toInt32(s));
}
| StringLiteral
{
pANTLR3_STRING s = $StringLiteral.text;
$value = ConstString((const char *)s->chars, s->len);
}
| 'true' { $value = ConstBool(1); } //!!ARL: Add these to the global symbol table instead?
| 'false' { $value = ConstBool(0); }
| 'null' { $value = ConstInt(0); }
| objectLiteral { $value = $objectLiteral.value; }
| arrayLiteral { $value = $arrayLiteral.value; }
| functionLiteral { $value = $functionLiteral.value; }
;
objectLiteral returns [LLVMValueRef value]
: ^( OBJECT vars=namedExpression* )
;
namedExpression
: ^( ':' NameLiteral constant_expression )
| ^( ':' StringLiteral constant_expression )
;
returnStatement
: ^( 'return' expression? )
;
// what about empty statements?
statement returns [LLVMValueRef result]
: ( expressionStatement { $result=NULL; }
| disruptiveStatement { $result=NULL; }
| tryStatement { $result=NULL; }
| ifStatement { $result=NULL; }
| switchStatement { $result=NULL; }
| whileStatement { $result=NULL; }
| forStatement { $result=NULL; }
| doStatement { $result=NULL; }
)
;
// only allow labels on switch, while, do and for?
labeledStatement returns [LLVMValueRef result]
: ^( STAT r=statement { $result=$r.result; }
( ^( LABEL NameLiteral ) )? )
;
statements returns [LLVMValueRef result]
: r=labeledStatement* { $result=$r.result; }
;
switchStatement
: ^( SWITCH expression caseClause*
( ^( DEFAULT statements ) )? )
;
throwStatement
: ^( 'throw' expression )
;
tryStatement
: ^( TRY block["try"] NameLiteral block["catch"] )
;
whileStatement
@init
{
LLVMValueRef function = SCOPE_TOP(Function)->ref;
LLVMBasicBlockRef cond_block = CreateBlock(function, "while_cond");
BeginBlock(cond_block);
}
: ^( WHILE cond=expression block["while_loop"] ) { While(function, $cond.value, cond_block, $block.ref); }
;