-
Notifications
You must be signed in to change notification settings - Fork 54
/
Compiler.js
603 lines (537 loc) · 28 KB
/
Compiler.js
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
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
// Compiler from parse tree to bytecode.
'use strict';
define(["Bytecode", "Node", "inst", "PascalError"],
function (Bytecode, Node, inst, PascalError) {
var Compiler = function () {
// This is a stack of lists of addresses of unconditional jumps (UJP) instructions
// that should go to the end of the function/procedure in an Exit statement.
// Each outer element represents a nested function/procedure we're compiling.
// The inner list is an unordered list of addresses to update when we get to
// the end of the function/procedure and know its last address.
this.exitInstructions = [];
};
// Given a parse tree, return the bytecode object.
Compiler.prototype.compile = function (root) {
var bytecode = new Bytecode(root.symbolTable.native);
// Start at the root and recurse.
this._generateBytecode(bytecode, root, null);
// Generate top-level calling code.
bytecode.setStartAddress();
bytecode.add(inst.MST, 0, 0, "start of program -----------------");
bytecode.add(inst.CUP, 0, root.symbol.address, "call main program");
bytecode.add(inst.STP, 0, 0, "program end");
return bytecode;
};
// Adds the node to the bytecode.
Compiler.prototype._generateBytecode = function (bytecode, node, symbolTable) {
switch (node.nodeType) {
case Node.IDENTIFIER:
var name = node.token.value;
var symbolLookup = node.symbolLookup;
if (symbolLookup.symbol.byReference) {
// Symbol is by reference. Must get its address first.
bytecode.add(inst.LVA, symbolLookup.level,
symbolLookup.symbol.address, "address of " + name);
bytecode.add(inst.LDI, symbolLookup.symbol.type.typeCode,
0, "value of " + name);
} else {
// Here we could call _generateAddressBytecode() followed by an inst.LDI,
// but loading the value directly is more efficient.
if (symbolLookup.symbol.type.nodeType === Node.SIMPLE_TYPE) {
var opcode;
switch (symbolLookup.symbol.type.typeCode) {
case inst.A:
opcode = inst.LVA;
break;
case inst.B:
opcode = inst.LVB;
break;
case inst.C:
opcode = inst.LVC;
break;
case inst.I:
opcode = inst.LVI;
break;
case inst.R:
opcode = inst.LVR;
break;
case inst.S:
// A string is not a character, but there's no opcode
// for loading a string. Re-use LVC.
opcode = inst.LVC;
break;
default:
throw new PascalError(node.token, "can't make code to get " +
symbolLookup.symbol.type.print());
}
bytecode.add(opcode, symbolLookup.level,
symbolLookup.symbol.address, "value of " + name);
} else {
// This is a more complex type, and apparently it's being
// passed by value, so we push the entire thing onto the stack.
var size = symbolLookup.symbol.type.getTypeSize();
// For large parameters it would be more
// space-efficient (but slower) to have a loop.
for (var i = 0; i < size; i++) {
bytecode.add(inst.LVI, symbolLookup.level,
symbolLookup.symbol.address + i,
"value of " + name + " at index " + i);
}
}
}
break;
case Node.NUMBER:
var v = node.getNumber();
var cindex = bytecode.addConstant(v);
// See if we're an integer or real.
var typeCode;
if ((v | 0) === v) {
typeCode = inst.I;
} else {
typeCode = inst.R;
}
bytecode.add(inst.LDC, typeCode, cindex, "constant value " + v);
break;
case Node.STRING:
var v = node.token.value;
var cindex = bytecode.addConstant(v);
bytecode.add(inst.LDC, inst.S, cindex, "string '" + v + "'");
break;
case Node.BOOLEAN:
var v = node.token.value;
bytecode.add(inst.LDC, inst.B, node.getBoolean() ? 1 : 0, "boolean " + v);
break;
case Node.POINTER:
// This can only be nil.
var cindex = bytecode.addConstant(0);
bytecode.add(inst.LDC, inst.A, cindex, "nil pointer");
break;
case Node.PROGRAM:
case Node.PROCEDURE:
case Node.FUNCTION:
var isFunction = node.nodeType === Node.FUNCTION;
var name = node.name.token.value;
// Begin a new frame for exit statements.
this._beginExitFrame();
// Generate each procedure and function.
for (var i = 0; i < node.declarations.length; i++) {
var declaration = node.declarations[i];
if (declaration.nodeType === Node.PROCEDURE ||
declaration.nodeType === Node.FUNCTION) {
this._generateBytecode(bytecode, declaration, node.symbolTable);
}
}
// Generate code for entry to block.
node.symbol.address = bytecode.getNextAddress();
var frameSize = inst.MARK_SIZE + node.symbolTable.totalVariableSize +
node.symbolTable.totalParameterSize;
bytecode.add(inst.ENT, 0, frameSize, "start of " + name + " -----------------");
// Generate code for typed constants.
for (var i = 0; i < node.declarations.length; i++) {
var declaration = node.declarations[i];
if (declaration.nodeType === Node.TYPED_CONST) {
this._generateBytecode(bytecode, declaration, node.symbolTable);
}
}
// Generate code for block.
this._generateBytecode(bytecode, node.block, node.symbolTable);
// End the frame for exit statements.
var ujpAddresses = this._endExitFrame();
var rtnAddress = bytecode.getNextAddress();
bytecode.add(inst.RTN, isFunction ? node.expressionType.
returnType.getSimpleTypeCode() : inst.P, 0, "end of " + name);
// Update all of the UJP statements to point to RTN.
for (var i = 0; i < ujpAddresses.length; i++) {
bytecode.setOperand2(ujpAddresses[i], rtnAddress);
}
break;
case Node.USES:
case Node.VAR:
case Node.PARAMETER:
case Node.CONST:
case Node.ARRAY_TYPE:
case Node.TYPE:
// Nothing.
break;
case Node.BLOCK:
for (var i = 0; i < node.statements.length; i++) {
this._generateBytecode(bytecode, node.statements[i], symbolTable);
}
break;
case Node.CAST:
this._generateBytecode(bytecode, node.expression, symbolTable);
var fromType = node.expression.expressionType;
var toType = node.type;
if (fromType.isSimpleType(inst.I) && toType.isSimpleType(inst.R)) {
bytecode.add(inst.FLT, 0, 0, "cast to float");
} else {
throw new PascalError(node.token, "don't know how to compile a cast from " +
fromType.print() + " to " + toType.print());
}
break;
case Node.ASSIGNMENT:
// Push address of LHS onto stack.
this._generateAddressBytecode(bytecode, node.lhs, symbolTable);
// Push RHS onto stack.
this._generateBytecode(bytecode, node.rhs, symbolTable);
// We don't look at the type code when executing, but might as
// well set it anyway.
var storeTypeCode = node.rhs.expressionType.getSimpleTypeCode();
bytecode.add(inst.STI, storeTypeCode, 0, "store into " + node.lhs.print());
break;
case Node.PROCEDURE_CALL:
case Node.FUNCTION_CALL:
var isFunction = node.nodeType === Node.FUNCTION_CALL;
var declType = isFunction ? "function" : "procedure";
var symbolLookup = node.name.symbolLookup;
var symbol = symbolLookup.symbol;
if (!symbol.isNative) {
bytecode.add(inst.MST, symbolLookup.level, 0, "set up mark for " + declType);
}
// Push arguments.
for (var i = 0; i < node.argumentList.length; i++) {
var argument = node.argumentList[i];
if (argument.byReference) {
this._generateAddressBytecode(bytecode, argument, symbolTable);
} else {
this._generateBytecode(bytecode, argument, symbolTable);
}
}
// See if this is a user procedure/function or native procedure/function.
if (symbol.isNative) {
// The CSP index is stored in the address field.
var index = symbol.address;
bytecode.add(inst.CSP, node.argumentList.length, index,
"call system " + declType + " " + symbol.name);
} else {
// Call procedure/function.
var parameterSize = symbol.type.getTotalParameterSize();
bytecode.add(inst.CUP, parameterSize, symbol.address,
"call " + node.name.print());
}
break;
case Node.REPEAT:
var topOfLoop = bytecode.getNextAddress();
bytecode.addComment(topOfLoop, "top of repeat loop");
this._generateBytecode(bytecode, node.block, symbolTable);
this._generateBytecode(bytecode, node.expression, symbolTable);
bytecode.add(inst.FJP, 0, topOfLoop, "jump to top of repeat");
break;
case Node.FOR:
// Assign start value.
var varNode = node.variable;
this._generateAddressBytecode(bytecode, varNode, symbolTable);
this._generateBytecode(bytecode, node.fromExpr, symbolTable);
bytecode.add(inst.STI, 0, 0, "store into " + varNode.print());
// Comparison.
var topOfLoop = bytecode.getNextAddress();
this._generateBytecode(bytecode, varNode, symbolTable);
this._generateBytecode(bytecode, node.toExpr, symbolTable);
bytecode.add(node.downto ? inst.LES : inst.GRT,
inst.I, 0, "see if we're done with the loop");
var jumpInstruction = bytecode.getNextAddress();
bytecode.add(inst.TJP, 0, 0, "yes, jump to end");
// Body.
this._generateBytecode(bytecode, node.body, symbolTable);
// Increment/decrement variable.
this._generateAddressBytecode(bytecode, varNode, symbolTable);
this._generateBytecode(bytecode, varNode, symbolTable);
if (node.downto) {
bytecode.add(inst.DEC, inst.I, 0, "decrement loop variable");
} else {
bytecode.add(inst.INC, inst.I, 0, "increment loop variable");
}
bytecode.add(inst.STI, 0, 0, "store into " + varNode.print());
// Jump back to top.
bytecode.add(inst.UJP, 0, topOfLoop, "jump to top of loop");
var endOfLoop = bytecode.getNextAddress();
// Fix up earlier jump.
bytecode.setOperand2(jumpInstruction, endOfLoop);
break;
case Node.IF:
var hasElse = node.elseStatement !== null;
// Do comparison.
this._generateBytecode(bytecode, node.expression, symbolTable);
var skipThenInstruction = bytecode.getNextAddress();
bytecode.add(inst.FJP, 0, 0, "false, jump " + (hasElse ? "to else" : "past body"));
// Then block.
this._generateBytecode(bytecode, node.thenStatement, symbolTable);
var skipElseInstruction = -1;
if (hasElse) {
skipElseInstruction = bytecode.getNextAddress();
bytecode.add(inst.UJP, 0, 0, "jump past else");
}
// Else block.
var falseAddress = bytecode.getNextAddress();
if (hasElse) {
this._generateBytecode(bytecode, node.elseStatement, symbolTable);
}
// Fix up earlier jumps.
bytecode.setOperand2(skipThenInstruction, falseAddress);
if (hasElse !== -1) {
var endOfIf = bytecode.getNextAddress();
bytecode.setOperand2(skipElseInstruction, endOfIf);
}
break;
case Node.EXIT:
// Return from procedure or function. We don't yet have the address
// of the last instruction in this function, so we keep track of these
// in an array and deal with them at the end.
var address = bytecode.getNextAddress();
bytecode.add(inst.UJP, 0, 0, "return from function/procedure");
this._addExitInstruction(address);
break;
case Node.WHILE:
// Generate the expression test.
var topOfLoop = bytecode.getNextAddress();
bytecode.addComment(topOfLoop, "top of while loop");
this._generateBytecode(bytecode, node.expression, symbolTable);
// Jump over the statement if the expression was false.
var jumpInstruction = bytecode.getNextAddress();
bytecode.add(inst.FJP, 0, 0, "if false, exit while loop");
// Generate the statement.
this._generateBytecode(bytecode, node.statement, symbolTable);
bytecode.add(inst.UJP, 0, topOfLoop, "jump to top of while loop");
// Fix up earlier jump.
var endOfLoop = bytecode.getNextAddress();
bytecode.setOperand2(jumpInstruction, endOfLoop);
break;
case Node.TYPED_CONST:
// These are just initialized variables. Copy the values to their stack
// location.
var constAddress = bytecode.addTypedConstants(node.rawData.data);
for (var i = 0; i < node.rawData.length; i++) {
var typeCode = node.rawData.simpleTypeCodes[i];
bytecode.add(inst.LDA, 0, node.symbol.address + i,
"address of " + node.name.print() +
" on stack (element " + i + ")");
// It's absurd to create this many constants, one for each
// address in the const pool, but I don't see another
// straightforward way to do it. Creating an ad-hoc loop is
// hard because I don't know where I'd store the loop
// variable. Even if I could store it on the stack where we
// are, how would I pop it off at the end of the loop? We
// don't have a POP instruction.
var cindex = bytecode.addConstant(constAddress + i);
bytecode.add(inst.LDC, inst.A, cindex, "address of " +
node.name.print() + " in const area (element " + i + ")");
bytecode.add(inst.LDI, typeCode, 0, "value of element");
bytecode.add(inst.STI, typeCode, 0, "write value");
}
break;
case Node.NOT:
this._generateBytecode(bytecode, node.expression, symbolTable);
bytecode.add(inst.NOT, 0, 0, "logical not");
break;
case Node.NEGATIVE:
this._generateBytecode(bytecode, node.expression, symbolTable);
if (node.expression.expressionType.isSimpleType(inst.R)) {
bytecode.add(inst.NGR, 0, 0, "real sign inversion");
} else {
bytecode.add(inst.NGI, 0, 0, "integer sign inversion");
}
break;
case Node.ADDITION:
this._generateNumericBinaryBytecode(bytecode, node, symbolTable,
"add", inst.ADI, inst.ADR);
break;
case Node.SUBTRACTION:
this._generateNumericBinaryBytecode(bytecode, node, symbolTable,
"subtract", inst.SBI, inst.SBR);
break;
case Node.MULTIPLICATION:
this._generateNumericBinaryBytecode(bytecode, node, symbolTable,
"multiply", inst.MPI, inst.MPR);
break;
case Node.DIVISION:
this._generateNumericBinaryBytecode(bytecode, node, symbolTable,
"divide", null, inst.DVR);
break;
case Node.FIELD_DESIGNATOR:
this._generateAddressBytecode(bytecode, node, symbolTable);
bytecode.add(inst.LDI, node.expressionType.getSimpleTypeCode(), 0,
"load value of record field");
break;
case Node.ARRAY:
// Array lookup.
this._generateAddressBytecode(bytecode, node, symbolTable);
bytecode.add(inst.LDI, node.expressionType.getSimpleTypeCode(), 0,
"load value of array element");
break;
case Node.ADDRESS_OF:
this._generateAddressBytecode(bytecode, node.variable, symbolTable);
break;
case Node.DEREFERENCE:
this._generateBytecode(bytecode, node.variable, symbolTable);
bytecode.add(inst.LDI, node.expressionType.getSimpleTypeCode(), 0,
"load value pointed to by pointer");
break;
case Node.EQUALITY:
this._generateComparisonBinaryBytecode(bytecode, node, symbolTable,
"equals", inst.EQU);
break;
case Node.INEQUALITY:
this._generateComparisonBinaryBytecode(bytecode, node, symbolTable,
"not equals", inst.NEQ);
break;
case Node.LESS_THAN:
this._generateComparisonBinaryBytecode(bytecode, node, symbolTable,
"less than", inst.LES);
break;
case Node.GREATER_THAN:
this._generateComparisonBinaryBytecode(bytecode, node, symbolTable,
"greater than", inst.GRT);
break;
case Node.LESS_THAN_OR_EQUAL_TO:
this._generateComparisonBinaryBytecode(bytecode, node, symbolTable,
"less than or equal to", inst.LEQ);
break;
case Node.GREATER_THAN_OR_EQUAL_TO:
this._generateComparisonBinaryBytecode(bytecode, node, symbolTable,
"greater than or equal to", inst.GEQ);
break;
case Node.AND:
this._generateComparisonBinaryBytecode(bytecode, node, symbolTable,
"and", inst.AND);
break;
case Node.OR:
this._generateComparisonBinaryBytecode(bytecode, node, symbolTable,
"or", inst.IOR);
break;
case Node.INTEGER_DIVISION:
this._generateNumericBinaryBytecode(bytecode, node, symbolTable,
"divide", inst.DVI, null);
break;
case Node.MOD:
this._generateNumericBinaryBytecode(bytecode, node, symbolTable,
"mod", inst.MOD, null);
break;
default:
throw new PascalError(null, "can't compile unknown node " + node.nodeType);
}
};
// Generates code to do math on two operands.
Compiler.prototype._generateNumericBinaryBytecode = function (bytecode, node,
symbolTable, opName, integerOpcode, realOpcode) {
this._generateBytecode(bytecode, node.lhs, symbolTable);
this._generateBytecode(bytecode, node.rhs, symbolTable);
if (node.expressionType.nodeType === Node.SIMPLE_TYPE) {
switch (node.expressionType.typeCode) {
case inst.I:
if (integerOpcode === null) {
throw new PascalError(node.token, "can't " + opName + " integers");
}
bytecode.add(integerOpcode, 0, 0, opName + " integers");
break;
case inst.R:
if (realOpcode === null) {
throw new PascalError(node.token, "can't " + opName + " reals");
}
bytecode.add(realOpcode, 0, 0, opName + " reals");
break;
default:
throw new PascalError(node.token, "can't " + opName + " operands of type " +
inst.typeCodeToName(node.expressionType.typeCode));
}
} else {
throw new PascalError(node.token, "can't " + opName +
" operands of type " + node.expressionType.print());
}
};
// Generates code to compare two operands.
Compiler.prototype._generateComparisonBinaryBytecode = function (bytecode, node,
symbolTable, opName, opcode) {
this._generateBytecode(bytecode, node.lhs, symbolTable);
this._generateBytecode(bytecode, node.rhs, symbolTable);
var opType = node.lhs.expressionType;
if (opType.nodeType === Node.SIMPLE_TYPE) {
bytecode.add(opcode, opType.typeCode, 0, opName);
} else {
throw new PascalError(node.token, "can't do " + opName +
" operands of type " + opType.print());
}
};
// Adds the address of the node to the bytecode.
Compiler.prototype._generateAddressBytecode = function(bytecode, node, symbolTable) {
switch (node.nodeType) {
case Node.IDENTIFIER:
var symbolLookup = node.symbolLookup;
var i;
if (symbolLookup.symbol.byReference) {
// By reference, the address is all we need.
i = inst.LVA;
} else {
// Load its address.
i = inst.LDA;
}
bytecode.add(i, symbolLookup.level,
symbolLookup.symbol.address, "address of " + node.print());
break;
case Node.ARRAY:
var arrayType = node.variable.expressionType;
// We compute the strides of the nested arrays as we go.
var strides = [];
// Start with the array's element size.
strides.push(arrayType.elementType.getTypeSize());
for (var i = 0; i < node.indices.length; i++) {
// Generate value of index.
this._generateBytecode(bytecode, node.indices[i], symbolTable);
// Subtract lower bound.
var low = arrayType.ranges[i].getRangeLowBound();
var cindex = bytecode.addConstant(low);
bytecode.add(inst.LDC, inst.I, cindex, "lower bound " + low);
bytecode.add(inst.SBI, 0, 0, "subtract lower bound");
// Add new stride.
var size = arrayType.ranges[i].getRangeSize();
strides.push(strides[strides.length - 1]*size);
// This would be a good place to do a runtime bounds check since
// we have the index and the size. The top of the stack should be
// non-negative and less than size.
}
// Pop the last stride, we don't need it. It represents the size of the
// entire array.
strides.pop();
// Look up address of array.
this._generateAddressBytecode(bytecode, node.variable, symbolTable);
for (var i = 0; i < node.indices.length; i++) {
// Compute address of the slice or element.
var stride = strides.pop();
bytecode.add(inst.IXA, 0, stride,
"address of array " +
((i === node.indices.length - 1) ? "element" : "slice") +
" (size " + stride + ")");
}
break;
case Node.FIELD_DESIGNATOR:
var recordType = node.variable.expressionType;
// Look up address of record.
this._generateAddressBytecode(bytecode, node.variable, symbolTable);
// Add the offset of the field.
var cindex = bytecode.addConstant(node.field.offset);
bytecode.add(inst.LDC, inst.I, cindex,
"offset of field \"" + node.field.name.print() + "\"");
bytecode.add(inst.ADI, 0, 0, "add offset to record address");
break;
case Node.DEREFERENCE:
// Just push the value of the pointer.
this._generateBytecode(bytecode, node.variable, symbolTable);
break;
default:
throw new PascalError(null, "unknown LHS node " + node.print());
}
};
// Start a frame for a function/procedure.
Compiler.prototype._beginExitFrame = function () {
this.exitInstructions.push([]);
};
// Add an address of an instruction to update once we know the end of the function.
Compiler.prototype._addExitInstruction = function (address) {
this.exitInstructions[this.exitInstructions.length - 1].push(address);
};
// End a frame for a function/procedure, returning a list of addresses of UJP functions
// to update.
Compiler.prototype._endExitFrame = function () {
return this.exitInstructions.pop();
};
return Compiler;
});