forked from ragibson/miniJava-compiler
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCodeGenerator.java
More file actions
715 lines (629 loc) · 21.7 KB
/
CodeGenerator.java
File metadata and controls
715 lines (629 loc) · 21.7 KB
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
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
package miniJava.CodeGenerator;
//for code generator
import mJAM.Machine;
import mJAM.Machine.Op;
import mJAM.Machine.Prim;
import mJAM.ObjectFile;
import java.util.ArrayList;
import java.util.Stack;
//to run debugger on object code
import mJAM.Disassembler;
import mJAM.Interpreter;
import miniJava.ErrorReporter;
import miniJava.AbstractSyntaxTrees.AST;
import miniJava.AbstractSyntaxTrees.ArrayType;
import miniJava.AbstractSyntaxTrees.AssignStmt;
import miniJava.AbstractSyntaxTrees.BaseType;
import miniJava.AbstractSyntaxTrees.BinaryExpr;
import miniJava.AbstractSyntaxTrees.BlockStmt;
import miniJava.AbstractSyntaxTrees.BooleanLiteral;
import miniJava.AbstractSyntaxTrees.CallExpr;
import miniJava.AbstractSyntaxTrees.CallStmt;
import miniJava.AbstractSyntaxTrees.ClassDecl;
import miniJava.AbstractSyntaxTrees.ClassType;
import miniJava.AbstractSyntaxTrees.Expression;
import miniJava.AbstractSyntaxTrees.FieldDecl;
import miniJava.AbstractSyntaxTrees.IdRef;
import miniJava.AbstractSyntaxTrees.Identifier;
import miniJava.AbstractSyntaxTrees.IfStmt;
import miniJava.AbstractSyntaxTrees.IntLiteral;
import miniJava.AbstractSyntaxTrees.IxRef;
import miniJava.AbstractSyntaxTrees.LiteralExpr;
import miniJava.AbstractSyntaxTrees.MethodDecl;
import miniJava.AbstractSyntaxTrees.NewArrayExpr;
import miniJava.AbstractSyntaxTrees.NewObjectExpr;
import miniJava.AbstractSyntaxTrees.NullLiteral;
import miniJava.AbstractSyntaxTrees.Operator;
import miniJava.AbstractSyntaxTrees.Package;
import miniJava.AbstractSyntaxTrees.ParameterDecl;
import miniJava.AbstractSyntaxTrees.QualRef;
import miniJava.AbstractSyntaxTrees.RefExpr;
import miniJava.AbstractSyntaxTrees.Reference;
import miniJava.AbstractSyntaxTrees.ReturnStmt;
import miniJava.AbstractSyntaxTrees.Statement;
import miniJava.AbstractSyntaxTrees.StatementList;
import miniJava.AbstractSyntaxTrees.ThisRef;
import miniJava.AbstractSyntaxTrees.TypeKind;
import miniJava.AbstractSyntaxTrees.UnaryExpr;
import miniJava.AbstractSyntaxTrees.VarDecl;
import miniJava.AbstractSyntaxTrees.VarDeclStmt;
import miniJava.AbstractSyntaxTrees.Visitor;
import miniJava.AbstractSyntaxTrees.WhileStmt;
import miniJava.SyntacticAnalyzer.TokenKind;
public class CodeGenerator implements Visitor<Object, Object> {
ErrorReporter reporter;
int localsDisplacement; // offset[LB]
// offset[OB] handled on a per-object basis
int mainCodeAddr;
int staticFieldPushAddr;
int localAllocationPopCount;
int currentMethodNumParams;
boolean runInterpreter = false;
// List of (instruction address, MethodDecl) for function call patching
ArrayList<FunctionPatch> functionPatchList;
public CodeGenerator(ErrorReporter reporter) {
this.reporter = reporter;
Machine.initCodeGen();
functionPatchList = new ArrayList<FunctionPatch>();
}
public void generateCode(AST ast, String fileName) {
staticFieldPushAddr = Machine.nextInstrAddr();
Machine.emit(Op.PUSH, 0); // allocate space for static variables
Machine.emit(Op.LOADL, 0); // array length 0
Machine.emit(Prim.newarr); // empty String array argument
mainCodeAddr = Machine.nextInstrAddr(); // record instr addr where main is called // "main" is called
Machine.emit(Op.CALL, Machine.Reg.CB, 0); // static call main (address to be patched)
Machine.emit(Op.HALT, 0, 0, 0); // end execution
ast.visit(this, null);
/*
* write code to object code file (.mJAM)
*/
String objectCodeFileName = fileName.replace(".java", ".mJAM");
ObjectFile objF = new ObjectFile(objectCodeFileName);
System.out.print("Writing object code file " + objectCodeFileName + " ... ");
if (objF.write()) {
reporter.reportError("***Writing object code file FAILED");
return;
} else
System.out.println("SUCCEEDED");
/****************************************************************************************
* The pa4 code generator should write an object file as shown above at the end
* of code generation and exit(0) if there are no errors.
*
* During development of the code generator you may want to run the generated
* code directly after code generation using the mJAM debugger mode. You can
* adapt the following code for this (it assumes objectCodeFileName holds the
* name of the objectFile)
*
*/
// create asm file using disassembler
String asmCodeFileName = objectCodeFileName.replace(".mJAM", ".asm");
System.out.print("Writing assembly file " + asmCodeFileName + " ... ");
Disassembler d = new Disassembler(objectCodeFileName);
if (d.disassemble()) {
reporter.reportError("***Writing assembly file FAILED");
return;
} else
System.out.println("SUCCEEDED");
/*
* run code using debugger
*
*/
if (runInterpreter) {
System.out.println("Running code in debugger ... ");
Interpreter.debug(objectCodeFileName, asmCodeFileName);
System.out.println("*** mJAM execution completed");
}
}
@Override
public Object visitPackage(Package prog, Object arg) {
/*
* Generate Runtime Entity Descriptions for the program's Declarations
*
* ClassDecl # nonstatic fields
* FieldDecl
* static offset in global segment
* nonstatic offset in instance
* MethodDecl offset in code store of method start
* LocalDecl offset relative to LB for the value (can be negative for parameters)
*/
int staticFieldOffset = 0;
for (ClassDecl classDecl : prog.classDeclList) {
int instanceFieldOffset = 0;
for (FieldDecl fieldDecl : classDecl.fieldDeclList) {
if (fieldDecl.isStatic) {
fieldDecl.description = new RuntimeEntityDescription(staticFieldOffset++);
} else {
fieldDecl.description = new RuntimeEntityDescription(instanceFieldOffset++);
}
}
classDecl.description = new RuntimeEntityDescription(instanceFieldOffset);
}
Machine.patch(staticFieldPushAddr, staticFieldOffset);
Machine.patch(mainCodeAddr, Machine.nextInstrAddr());
// Ensure correctness and uniqueness of main method
int numMainMethods = 0;
boolean malformedMain = false;
for (ClassDecl cd : prog.classDeclList) {
for (MethodDecl m : cd.methodDeclList) {
if (m.name.equals("main")) {
numMainMethods++;
if (m.isPrivate || !m.isStatic || m.type.typeKind != TypeKind.VOID) {
malformedMain = true;
}
if (m.parameterDeclList.size() == 1) {
if (m.parameterDeclList.get(0).type instanceof ArrayType) {
ArrayType arrayType = (ArrayType) m.parameterDeclList.get(0).type;
if (arrayType.eltType instanceof ClassType
&& ((ClassType) arrayType.eltType).className.spelling.equals("String")) {
// correctly formed main method
functionPatchList.add(new FunctionPatch(mainCodeAddr, m));
} else {
malformedMain = true;
}
} else {
malformedMain = true;
}
} else {
malformedMain = true;
}
}
}
}
// add return to end of all void function and ensure return at end of all non-void functions
for (ClassDecl cd : prog.classDeclList) {
for (MethodDecl md : cd.methodDeclList) {
StatementList statementList = md.statementList;
if (statementList.size() == 0) {
md.statementList.add(new ReturnStmt(null, md.posn));
}
Statement lastStatement = statementList.get(statementList.size()-1);
if (md.type.typeKind != TypeKind.VOID) {
if (!(lastStatement instanceof ReturnStmt)) {
reporter.reportError("*** non-void method '" + md.name + "' does not return at end.");
}
} else {
md.statementList.add(new ReturnStmt(null, lastStatement.posn));
}
}
}
if (numMainMethods != 1 || malformedMain) {
reporter.reportError("*** program does not have a unique public static void main(String[] args) method.");
}
for (ClassDecl c : prog.classDeclList)
c.visit(this, null);
// final pass to patch function call addresses
for (FunctionPatch fp : functionPatchList) {
// System.out.println("Patching method " + fp.methodDecl.name + " at addr " + fp.codeAddr);
Machine.patch(fp.codeAddr, fp.methodDecl.description.memoryOffset);
}
return null;
}
@Override
public Object visitClassDecl(ClassDecl cd, Object arg) {
for (FieldDecl f : cd.fieldDeclList)
f.visit(this, null);
for (MethodDecl m : cd.methodDeclList)
m.visit(this, null);
return null;
}
@Override
public Object visitFieldDecl(FieldDecl fd, Object arg) {
fd.type.visit(this, null);
return null;
}
@Override
public Object visitMethodDecl(MethodDecl md, Object arg) {
localsDisplacement = 3;
currentMethodNumParams = md.parameterDeclList.size();
md.type.visit(this, null);
int parameterOffsetStart = -md.parameterDeclList.size();
for (ParameterDecl p : md.parameterDeclList) {
p.visit(this, null);
p.description = new RuntimeEntityDescription(parameterOffsetStart++);
}
// offset in code store of method start
md.description = new RuntimeEntityDescription(Machine.nextInstrAddr());
for (Statement s : md.statementList)
s.visit(this, null);
return null;
}
@Override
public Object visitParameterDecl(ParameterDecl pd, Object arg) {
pd.type.visit(this, null);
return null;
}
@Override
public Object visitVarDecl(VarDecl decl, Object arg) {
// System.out.println("Placing " + decl.name + " at offset " + localsDisplacement);
decl.description = new RuntimeEntityDescription(localsDisplacement++);
decl.type.visit(this, null);
return null;
}
@Override
public Object visitBaseType(BaseType type, Object arg) {
return null;
}
@Override
public Object visitClassType(ClassType type, Object arg) {
type.className.visit(this, null);
return null;
}
@Override
public Object visitArrayType(ArrayType type, Object arg) {
type.eltType.visit(this, null);
return null;
}
@Override
public Object visitBlockStmt(BlockStmt stmt, Object arg) {
// keep track of how many local variables were allocated
this.localAllocationPopCount = 0;
for (Statement s : stmt.sl)
s.visit(this, null);
if (this.localAllocationPopCount > 0) {
this.localsDisplacement -= this.localAllocationPopCount;
Machine.emit(Op.POP, this.localAllocationPopCount);
}
return null;
}
@Override
public Object visitVardeclStmt(VarDeclStmt stmt, Object arg) {
this.localAllocationPopCount++;
stmt.varDecl.visit(this, null);
stmt.initExp.visit(this, null);
return null;
}
@Override
public Object visitAssignStmt(AssignStmt stmt, Object arg) {
if (stmt.ref.decl.compilerHint == CompilerHint.STATIC_FIELD_ACCESS) {
stmt.val.visit(this, null);
Machine.emit(Op.STORE, Machine.Reg.SB, stmt.ref.decl.description.memoryOffset);
} else if (stmt.ref instanceof IdRef) {
IdRef idRef = (IdRef) stmt.ref;
if (idRef.decl instanceof FieldDecl) {
Machine.emit(Op.LOADA, Machine.Reg.OB, 0);
Machine.emit(Op.LOADL, idRef.id.decl.description.memoryOffset);
stmt.val.visit(this, null);
Machine.emit(Prim.fieldupd);
} else {
stmt.val.visit(this, null);
storeIdRef((IdRef) stmt.ref);
}
// Machine.emit(Op.STORE, Machine.Reg.LB, stmt.ref.decl.description.memoryOffset);
} else if (stmt.ref instanceof QualRef) {
visitQRefHelper((QualRef) stmt.ref);
stmt.val.visit(this, null);
Machine.emit(Prim.fieldupd);
} else if (stmt.ref instanceof IxRef) {
IxRef ixRef = (IxRef) stmt.ref;
ixRef.ref.visit(this, null);
ixRef.indexExpr.visit(this, null);
stmt.val.visit(this, null);
Machine.emit(Prim.arrayupd);
}
return null;
}
@Override
public Object visitCallStmt(CallStmt stmt, Object arg) {
for (Expression e : stmt.argList)
e.visit(this, null);
if (stmt.methodRef.decl.compilerHint == CompilerHint.DEFAULT_PRINTLN) {
assert stmt.argList.size() == 1;
Machine.emit(Prim.putintnl);
} else {
int callCodeAddr = Machine.nextInstrAddr();
if (((MethodDecl) stmt.methodRef.decl).isStatic) { // CALL
Machine.emit(Op.CALL, Machine.Reg.CB, 0);
functionPatchList.add(new FunctionPatch(callCodeAddr, (MethodDecl) stmt.methodRef.decl));
} else { // CALLI
stmt.methodRef.visit(this, null);
if (stmt.methodRef instanceof QualRef) {
QualRef methodQualRef = (QualRef) stmt.methodRef;
Reference object = methodQualRef.ref;
// Identifier function = methodQualRef.id;
object.visit(this, null);
} else {
// Implicit this.function();
visitThisRef(null, null);
}
// methodQualRef.visit(this, null);
callCodeAddr = Machine.nextInstrAddr();
Machine.emit(Op.CALLI, Machine.Reg.CB, 0);
functionPatchList.add(new FunctionPatch(callCodeAddr, (MethodDecl) stmt.methodRef.decl));
}
}
// Remove non-void return results from the stack if not assigned to variables
if (stmt.methodRef.decl.type.typeKind != TypeKind.VOID) {
Machine.emit(Op.POP, 1);
}
return null;
}
@Override
public Object visitReturnStmt(ReturnStmt stmt, Object arg) {
if (stmt.returnExpr != null)
stmt.returnExpr.visit(this, null);
Machine.emit(Op.RETURN, stmt.returnExpr == null ? 0 : 1, 0, currentMethodNumParams);
return null;
}
@Override
public Object visitIfStmt(IfStmt stmt, Object arg) {
stmt.cond.visit(this, null);
int codeAddrJump1 = Machine.nextInstrAddr();
Machine.emit(Op.JUMPIF, 0, Machine.Reg.CB, 0);
stmt.thenStmt.visit(this, null);
int codeAddrJump2 = Machine.nextInstrAddr();
Machine.emit(Op.JUMP, 0, Machine.Reg.CB, 0);
Machine.patch(codeAddrJump1, Machine.nextInstrAddr());
if (stmt.elseStmt != null) {
stmt.elseStmt.visit(this, null);
}
Machine.patch(codeAddrJump2, Machine.nextInstrAddr());
return null;
}
@Override
public Object visitWhileStmt(WhileStmt stmt, Object arg) {
int codeAddrJump1 = Machine.nextInstrAddr();
Machine.emit(Op.JUMP, 0, Machine.Reg.CB, 0);
stmt.body.visit(this, null);
int codeAddrJump2 = Machine.nextInstrAddr();
stmt.cond.visit(this, null);
Machine.emit(Op.JUMPIF, 1, Machine.Reg.CB, codeAddrJump1 + 1);
Machine.patch(codeAddrJump1, codeAddrJump2);
return null;
}
@Override
public Object visitUnaryExpr(UnaryExpr expr, Object arg) {
if (expr.operator.kind == TokenKind.MINUS) {
// convert unary -x into 0-x
Machine.emit(Op.LOADL, 0);
}
expr.expr.visit(this, null);
expr.operator.visit(this, null);
return null;
}
@Override
public Object visitBinaryExpr(BinaryExpr expr, Object arg) {
if (expr.operator.kind == TokenKind.AND) {
// short-circuiting AND
expr.left.visit(this, null);
Machine.emit(Op.LOAD, Machine.Reg.ST, -1);
int codeAddrSkipSecond = Machine.nextInstrAddr();
Machine.emit(Op.JUMPIF, 0, Machine.Reg.CB, 0);
expr.right.visit(this, null);
expr.operator.visit(this, null);
Machine.patch(codeAddrSkipSecond, Machine.nextInstrAddr());
} else if (expr.operator.kind == TokenKind.OR) {
// short-circuiting OR
expr.left.visit(this, null);
Machine.emit(Op.LOAD, Machine.Reg.ST, -1);
int codeAddrSkipSecond = Machine.nextInstrAddr();
Machine.emit(Op.JUMPIF, 1, Machine.Reg.CB, 0);
expr.right.visit(this, null);
expr.operator.visit(this, null);
Machine.patch(codeAddrSkipSecond, Machine.nextInstrAddr());
} else {
expr.left.visit(this, null);
expr.right.visit(this, null);
expr.operator.visit(this, null);
}
return null;
}
@Override
public Object visitRefExpr(RefExpr expr, Object arg) {
if (expr.ref.decl.compilerHint == CompilerHint.STATIC_FIELD_ACCESS) {
Machine.emit(Op.LOAD, Machine.Reg.SB, expr.ref.decl.description.memoryOffset);
} else if (expr.ref instanceof IdRef) {
expr.ref.visit(this, null);
} else if (expr.ref instanceof QualRef) {
expr.ref.visit(this, null);
} else if (expr.ref instanceof IxRef) {
IxRef ixRef = (IxRef) expr.ref;
ixRef.ref.visit(this, null);
ixRef.indexExpr.visit(this, null);
Machine.emit(Prim.arrayref);
} else if (expr.ref instanceof ThisRef) {
Machine.emit(Op.LOADA, Machine.Reg.OB, 0);
}
return null;
}
@Override
public Object visitCallExpr(CallExpr expr, Object arg) {
for (Expression e : expr.argList)
e.visit(this, null);
if (expr.functionRef.decl.compilerHint != CompilerHint.DEFAULT_PRINTLN) {
expr.functionRef.visit(this, null);
int callCodeAddr = Machine.nextInstrAddr();
if (((MethodDecl) expr.functionRef.decl).isStatic) { // CALL
Machine.emit(Op.CALL, Machine.Reg.CB, 0);
functionPatchList.add(new FunctionPatch(callCodeAddr, (MethodDecl) expr.functionRef.decl));
} else { // CALLI
// reload OB
if (expr.functionRef instanceof QualRef) {
QualRef qualRef = (QualRef) expr.functionRef;
qualRef.ref.visit(this, null);
} else {
Machine.emit(Op.LOADA, Machine.Reg.OB, 0);
}
callCodeAddr = Machine.nextInstrAddr();
Machine.emit(Op.CALLI, Machine.Reg.CB, 0);
functionPatchList.add(new FunctionPatch(callCodeAddr, (MethodDecl) expr.functionRef.decl));
}
}
return null;
}
@Override
public Object visitLiteralExpr(LiteralExpr expr, Object arg) {
expr.lit.visit(this, null);
return null;
}
@Override
public Object visitNewObjectExpr(NewObjectExpr expr, Object arg) {
// expr.classtype.visit(this, null);
Machine.emit(Op.LOADL, -1);
Machine.emit(Op.LOADL, expr.classtype.className.decl.description.memoryOffset);
Machine.emit(Prim.newobj);
return null;
}
@Override
public Object visitNewArrayExpr(NewArrayExpr expr, Object arg) {
// expr.eltType.visit(this, null);
expr.sizeExpr.visit(this, null);
Machine.emit(Prim.newarr);
return null;
}
@Override
public Object visitThisRef(ThisRef ref, Object arg) {
Machine.emit(Op.LOADA, Machine.Reg.OB, 0);
return null;
}
public void loadIdRef(IdRef ref) {
if (ref.decl instanceof FieldDecl) {
FieldDecl fieldDecl = (FieldDecl) ref.decl;
if (fieldDecl.isStatic) {
Machine.emit(Op.LOAD, Machine.Reg.SB, ref.id.decl.description.memoryOffset);
} else {
Machine.emit(Op.LOAD, Machine.Reg.OB, ref.id.decl.description.memoryOffset);
}
} else if (ref.id.decl.description != null) {
if (ref.id.decl.compilerHint == CompilerHint.STATIC_FIELD_ACCESS) {
Machine.emit(Op.LOAD, Machine.Reg.SB, ref.id.decl.description.memoryOffset);
} else if (!(ref.id.decl instanceof MethodDecl)) {
Machine.emit(Op.LOAD, Machine.Reg.LB, ref.id.decl.description.memoryOffset);
}
}
}
public void storeIdRef(IdRef ref) {
if (ref.decl instanceof FieldDecl) {
FieldDecl fieldDecl = (FieldDecl) ref.decl;
if (fieldDecl.isStatic) {
Machine.emit(Op.STORE, Machine.Reg.SB, ref.id.decl.description.memoryOffset);
} else {
}
} else {
if (ref.id.decl.compilerHint == CompilerHint.STATIC_FIELD_ACCESS) {
Machine.emit(Op.STORE, Machine.Reg.SB, ref.id.decl.description.memoryOffset);
} else {
Machine.emit(Op.STORE, Machine.Reg.LB, ref.id.decl.description.memoryOffset);
}
}
}
@Override
public Object visitIdRef(IdRef ref, Object arg) {
loadIdRef(ref);
return null;
}
public void visitQRefHelper(QualRef ref) {
// emits code that puts the final address and field index on the stack
// I.e. calling this method and then emitting fieldref will put the value of the
// QualRef on the stack
if (ref.id.decl.description != null) {
Stack<Integer> fieldOffsetStack = new Stack<Integer>();
fieldOffsetStack.push(ref.id.decl.description.memoryOffset);
// System.out.println("visitQRefHelper pushed " + fieldOffsetStack.peek() + " for reference " + ref.id.spelling);
while (ref.ref instanceof QualRef) {
ref = (QualRef) ref.ref;
fieldOffsetStack.push(ref.decl.description.memoryOffset);
// System.out.println("visitQRefHelper pushed " + fieldOffsetStack.peek() + " for reference " + ref.id.spelling);
}
ref.ref.visit(this, null);
// Machine.emit(Op.LOAD, Machine.Reg.LB, ref.ref.decl.description.memoryOffset);
int stackSize = fieldOffsetStack.size();
for (int i = 0; i < stackSize; i++) {
int fieldOffset = fieldOffsetStack.pop();
Machine.emit(Op.LOADL, fieldOffset);
// System.out.println("visitQRefHelper emitted LOADL " + fieldOffset);
if (i+1 < stackSize) {
Machine.emit(Prim.fieldref);
}
}
}
}
@Override
public Object visitQRef(QualRef ref, Object arg) {
if (ref.id.decl.compilerHint == CompilerHint.ARRAY_LENGTH) {
loadIdRef((IdRef) ref.ref);
// Machine.emit(Op.LOAD, Machine.Reg.LB, ref.ref.decl.description.memoryOffset);
Machine.emit(Prim.arraylen);
} else if (ref.id.decl.description != null) {
visitQRefHelper(ref);
Machine.emit(Prim.fieldref);
}
return null;
}
@Override
public Object visitIxRef(IxRef ref, Object arg) {
return null;
}
@Override
public Object visitIdentifier(Identifier id, Object arg) {
return null;
}
@Override
public Object visitOperator(Operator op, Object arg) {
switch (op.kind) {
case LESSTHAN:
Machine.emit(Prim.lt);
break;
case GREATERTHAN:
Machine.emit(Prim.gt);
break;
case EQUALS:
Machine.emit(Prim.eq);
break;
case LESSEQUAL:
Machine.emit(Prim.le);
break;
case GREATEREQUAL:
Machine.emit(Prim.ge);
break;
case NOTEQUAL:
Machine.emit(Prim.ne);
break;
case AND:
Machine.emit(Prim.and);
break;
case OR:
Machine.emit(Prim.or);
break;
case PLUS:
Machine.emit(Prim.add);
break;
case MINUS:
Machine.emit(Prim.sub);
break;
case TIMES:
Machine.emit(Prim.mult);
break;
case DIVIDE:
Machine.emit(Prim.div);
break;
case NOT:
Machine.emit(Prim.neg);
break;
default:
reporter.reportError("*** Encountered unknown operator " + op.kind + " in code generation.");
break;
}
return null;
}
@Override
public Object visitIntLiteral(IntLiteral num, Object arg) {
int intValue = Integer.parseInt(num.spelling);
Machine.emit(Op.LOADL, intValue);
return null;
}
@Override
public Object visitBooleanLiteral(BooleanLiteral bool, Object arg) {
if (bool.spelling.equals("true")) {
Machine.emit(Op.LOADL, Machine.trueRep);
} else if (bool.spelling.equals("false")) {
Machine.emit(Op.LOADL, Machine.falseRep);
}
return null;
}
@Override
public Object visitNullLiteral(NullLiteral nullLit, Object arg) {
Machine.emit(Op.LOADL, Machine.nullRep);
return null;
}
}