Skip to content

Commit 109fdc2

Browse files
Update SON backend to match chapter 22 of Simple
Update SON backend to match chapter 22 of Simple
2 parents 89c0fe2 + 650b4ef commit 109fdc2

File tree

178 files changed

+4426
-2638
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

178 files changed

+4426
-2638
lines changed

optvm/src/main/java/com/compilerprogramming/ezlang/compiler/CompiledFunction.java

Lines changed: 28 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
import com.compilerprogramming.ezlang.parser.AST;
55
import com.compilerprogramming.ezlang.types.Scope;
66
import com.compilerprogramming.ezlang.types.Symbol;
7-
import com.compilerprogramming.ezlang.types.Type;
7+
import com.compilerprogramming.ezlang.types.EZType;
88
import com.compilerprogramming.ezlang.types.TypeDictionary;
99

1010
import java.util.*;
@@ -17,7 +17,7 @@ public class CompiledFunction {
1717
public BasicBlock currentBlock;
1818
private BasicBlock currentBreakTarget;
1919
private BasicBlock currentContinueTarget;
20-
public Type.TypeFunction functionType;
20+
public EZType.EZTypeFunction functionType;
2121
public final RegisterPool registerPool;
2222
private final TypeDictionary typeDictionary;
2323

@@ -39,7 +39,7 @@ public class CompiledFunction {
3939

4040
public CompiledFunction(Symbol.FunctionTypeSymbol functionSymbol, TypeDictionary typeDictionary, EnumSet<Options> options) {
4141
AST.FuncDecl funcDecl = (AST.FuncDecl) functionSymbol.functionDecl;
42-
this.functionType = (Type.TypeFunction) functionSymbol.type;
42+
this.functionType = (EZType.EZTypeFunction) functionSymbol.type;
4343
this.registerPool = new RegisterPool();
4444
// Incremental SSA is an optional feature
4545
this.issa = (options != null && options.contains(Options.ISSA)) ? new IncrementalSSABraun(this) : new NoopIncrementalSSA();
@@ -60,8 +60,8 @@ public CompiledFunction(Symbol.FunctionTypeSymbol functionSymbol, TypeDictionary
6060
public CompiledFunction(Symbol.FunctionTypeSymbol functionSymbol, TypeDictionary typeDictionary) {
6161
this(functionSymbol,typeDictionary,null);
6262
}
63-
public CompiledFunction(Type.TypeFunction functionType, TypeDictionary typeDictionary) {
64-
this.functionType = (Type.TypeFunction) functionType;
63+
public CompiledFunction(EZType.EZTypeFunction functionType, TypeDictionary typeDictionary) {
64+
this.functionType = (EZType.EZTypeFunction) functionType;
6565
this.registerPool = new RegisterPool();
6666
this.issa = new NoopIncrementalSSA(); this.BID = 0;
6767
this.entry = this.currentBlock = createBlock();
@@ -325,7 +325,7 @@ private boolean compileExpr(AST.Expr expr) {
325325
private boolean compileCallExpr(AST.CallExpr callExpr) {
326326
compileExpr(callExpr.callee);
327327
var callee = pop();
328-
Type.TypeFunction calleeType = null;
328+
EZType.EZTypeFunction calleeType = null;
329329
if (callee instanceof Operand.LocalFunctionOperand functionOperand)
330330
calleeType = functionOperand.functionType;
331331
else throw new CompilerException("Cannot call a non function type");
@@ -347,28 +347,28 @@ private boolean compileCallExpr(AST.CallExpr callExpr) {
347347
for (int i = 0; i < args.size(); i++)
348348
pop();
349349
Operand.TempRegisterOperand ret = null;
350-
if (callExpr.callee.type instanceof Type.TypeFunction tf &&
351-
!(tf.returnType instanceof Type.TypeVoid)) {
350+
if (callExpr.callee.type instanceof EZType.EZTypeFunction tf &&
351+
!(tf.returnType instanceof EZType.EZTypeVoid)) {
352352
ret = createTemp(tf.returnType);
353353
}
354354
codeCall(returnStackPos, ret, calleeType, args.toArray(new Operand.RegisterOperand[args.size()]));
355355
return false;
356356
}
357357

358-
private Type.TypeStruct getStructType(Type t) {
359-
if (t instanceof Type.TypeStruct typeStruct) {
358+
private EZType.EZTypeStruct getStructType(EZType t) {
359+
if (t instanceof EZType.EZTypeStruct typeStruct) {
360360
return typeStruct;
361361
}
362-
else if (t instanceof Type.TypeNullable ptr &&
363-
ptr.baseType instanceof Type.TypeStruct typeStruct) {
362+
else if (t instanceof EZType.EZTypeNullable ptr &&
363+
ptr.baseType instanceof EZType.EZTypeStruct typeStruct) {
364364
return typeStruct;
365365
}
366366
else
367367
throw new CompilerException("Unexpected type: " + t);
368368
}
369369

370370
private boolean compileFieldExpr(AST.GetFieldExpr fieldExpr) {
371-
Type.TypeStruct typeStruct = getStructType(fieldExpr.object.type);
371+
EZType.EZTypeStruct typeStruct = getStructType(fieldExpr.object.type);
372372
int fieldIndex = typeStruct.getFieldIndex(fieldExpr.fieldName);
373373
if (fieldIndex < 0)
374374
throw new CompilerException("Field " + fieldExpr.fieldName + " not found");
@@ -391,7 +391,7 @@ private boolean compileArrayIndexExpr(AST.ArrayLoadExpr arrayIndexExpr) {
391391
}
392392

393393
private boolean compileSetFieldExpr(AST.SetFieldExpr setFieldExpr) {
394-
Type.TypeStruct structType = (Type.TypeStruct) setFieldExpr.object.type;
394+
EZType.EZTypeStruct structType = (EZType.EZTypeStruct) setFieldExpr.object.type;
395395
int fieldIndex = structType.getFieldIndex(setFieldExpr.fieldName);
396396
if (fieldIndex == -1)
397397
throw new CompilerException("Field " + setFieldExpr.fieldName + " not found in struct " + structType.name);
@@ -441,7 +441,7 @@ private boolean compileInitExpr(AST.InitExpr initExpr) {
441441
}
442442

443443
private boolean compileSymbolExpr(AST.NameExpr symbolExpr) {
444-
if (symbolExpr.type instanceof Type.TypeFunction functionType)
444+
if (symbolExpr.type instanceof EZType.EZTypeFunction functionType)
445445
pushOperand(new Operand.LocalFunctionOperand(functionType));
446446
else {
447447
Symbol.VarSymbol varSymbol = (Symbol.VarSymbol) symbolExpr.symbol;
@@ -549,29 +549,29 @@ private boolean compileUnaryExpr(AST.UnaryExpr unaryExpr) {
549549
}
550550

551551
private boolean compileConstantExpr(AST.LiteralExpr constantExpr) {
552-
if (constantExpr.type instanceof Type.TypeInteger)
552+
if (constantExpr.type instanceof EZType.EZTypeInteger)
553553
pushConstant(constantExpr.value.num.intValue(), constantExpr.type);
554-
else if (constantExpr.type instanceof Type.TypeNull)
554+
else if (constantExpr.type instanceof EZType.EZTypeNull)
555555
pushNullConstant(constantExpr.type);
556556
else throw new CompilerException("Invalid constant type");
557557
return false;
558558
}
559559

560-
private void pushConstant(long value, Type type) {
560+
private void pushConstant(long value, EZType type) {
561561
pushOperand(new Operand.ConstantOperand(value, type));
562562
}
563563

564-
private void pushNullConstant(Type type) {
564+
private void pushNullConstant(EZType type) {
565565
pushOperand(new Operand.NullConstantOperand(type));
566566
}
567567

568-
private Operand.TempRegisterOperand createTemp(Type type) {
568+
private Operand.TempRegisterOperand createTemp(EZType type) {
569569
var tempRegister = new Operand.TempRegisterOperand(registerPool.newTempReg(type));
570570
pushOperand(tempRegister);
571571
return tempRegister;
572572
}
573573

574-
Type typeOfOperand(Operand operand) {
574+
EZType typeOfOperand(Operand operand) {
575575
if (operand instanceof Operand.ConstantOperand constant)
576576
return constant.type;
577577
else if (operand instanceof Operand.NullConstantOperand nullConstantOperand)
@@ -582,7 +582,7 @@ else if (operand instanceof Operand.RegisterOperand registerOperand)
582582
}
583583

584584
private Operand.TempRegisterOperand createTempAndMove(Operand src) {
585-
Type type = typeOfOperand(src);
585+
EZType type = typeOfOperand(src);
586586
var temp = createTemp(type);
587587
codeMove(src, temp);
588588
return temp;
@@ -644,16 +644,16 @@ else if (indexed instanceof Operand.LoadFieldOperand loadFieldOperand)
644644
codeMove(value, indexed);
645645
}
646646

647-
private void codeNew(Type type, AST.Expr len, AST.Expr initVal) {
648-
if (type instanceof Type.TypeArray typeArray)
647+
private void codeNew(EZType type, AST.Expr len, AST.Expr initVal) {
648+
if (type instanceof EZType.EZTypeArray typeArray)
649649
codeNewArray(typeArray, len, initVal);
650-
else if (type instanceof Type.TypeStruct typeStruct)
650+
else if (type instanceof EZType.EZTypeStruct typeStruct)
651651
codeNewStruct(typeStruct);
652652
else
653653
throw new CompilerException("Unexpected type: " + type);
654654
}
655655

656-
private void codeNewArray(Type.TypeArray typeArray, AST.Expr len, AST.Expr initVal) {
656+
private void codeNewArray(EZType.EZTypeArray typeArray, AST.Expr len, AST.Expr initVal) {
657657
var temp = createTemp(typeArray);
658658
Operand lenOperand = null;
659659
Operand initValOperand = null;
@@ -685,7 +685,7 @@ private void codeNewArray(Type.TypeArray typeArray, AST.Expr len, AST.Expr initV
685685
code(insn);
686686
}
687687

688-
private void codeNewStruct(Type.TypeStruct typeStruct) {
688+
private void codeNewStruct(EZType.EZTypeStruct typeStruct) {
689689
var temp = createTemp(typeStruct);
690690
var target = (Operand.RegisterOperand) issa.write(temp);
691691
var insn = new Instruction.NewStruct(typeStruct, target);
@@ -729,7 +729,7 @@ private void codeCBR(BasicBlock block, Operand condition, BasicBlock trueBlock,
729729

730730
private void codeCall(int newBase,
731731
Operand.RegisterOperand targetOperand,
732-
Type.TypeFunction calleeType,
732+
EZType.EZTypeFunction calleeType,
733733
Operand.RegisterOperand ...arguments) {
734734
if (targetOperand != null)
735735
targetOperand = (Operand.RegisterOperand) issa.write(targetOperand);

optvm/src/main/java/com/compilerprogramming/ezlang/compiler/Compiler.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
import com.compilerprogramming.ezlang.semantic.SemaAssignTypes;
66
import com.compilerprogramming.ezlang.semantic.SemaDefineTypes;
77
import com.compilerprogramming.ezlang.types.Symbol;
8-
import com.compilerprogramming.ezlang.types.Type;
8+
import com.compilerprogramming.ezlang.types.EZType;
99
import com.compilerprogramming.ezlang.types.TypeDictionary;
1010

1111
import java.util.EnumSet;
@@ -15,7 +15,7 @@ public class Compiler {
1515
private void compile(TypeDictionary typeDictionary, EnumSet<Options> options) {
1616
for (Symbol symbol: typeDictionary.getLocalSymbols()) {
1717
if (symbol instanceof Symbol.FunctionTypeSymbol functionSymbol) {
18-
Type.TypeFunction functionType = (Type.TypeFunction) functionSymbol.type;
18+
EZType.EZTypeFunction functionType = (EZType.EZTypeFunction) functionSymbol.type;
1919
var function = new CompiledFunction(functionSymbol, typeDictionary, options);
2020
if (options.contains(Options.DUMP_INITIAL_IR))
2121
function.dumpIR(false, "Initial IR");

optvm/src/main/java/com/compilerprogramming/ezlang/compiler/Instruction.java

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package com.compilerprogramming.ezlang.compiler;
22

33
import com.compilerprogramming.ezlang.exceptions.CompilerException;
4-
import com.compilerprogramming.ezlang.types.Type;
4+
import com.compilerprogramming.ezlang.types.EZType;
55

66
import java.util.ArrayList;
77
import java.util.Collections;
@@ -139,16 +139,16 @@ public StringBuilder toStr(StringBuilder sb) {
139139
}
140140

141141
public static class NewArray extends Instruction {
142-
public final Type.TypeArray type;
143-
public NewArray(Type.TypeArray type, Operand.RegisterOperand destOperand) {
142+
public final EZType.EZTypeArray type;
143+
public NewArray(EZType.EZTypeArray type, Operand.RegisterOperand destOperand) {
144144
super(I_NEW_ARRAY, destOperand);
145145
this.type = type;
146146
}
147-
public NewArray(Type.TypeArray type, Operand.RegisterOperand destOperand, Operand len) {
147+
public NewArray(EZType.EZTypeArray type, Operand.RegisterOperand destOperand, Operand len) {
148148
super(I_NEW_ARRAY, destOperand, len);
149149
this.type = type;
150150
}
151-
public NewArray(Type.TypeArray type, Operand.RegisterOperand destOperand, Operand len, Operand initValue) {
151+
public NewArray(EZType.EZTypeArray type, Operand.RegisterOperand destOperand, Operand len, Operand initValue) {
152152
super(I_NEW_ARRAY, destOperand, len, initValue);
153153
this.type = type;
154154
}
@@ -170,8 +170,8 @@ public StringBuilder toStr(StringBuilder sb) {
170170
}
171171

172172
public static class NewStruct extends Instruction {
173-
public final Type.TypeStruct type;
174-
public NewStruct(Type.TypeStruct type, Operand.RegisterOperand destOperand) {
173+
public final EZType.EZTypeStruct type;
174+
public NewStruct(EZType.EZTypeStruct type, Operand.RegisterOperand destOperand) {
175175
super(I_NEW_STRUCT, destOperand);
176176
this.type = type;
177177
}
@@ -324,9 +324,9 @@ public StringBuilder toStr(StringBuilder sb) {
324324
}
325325

326326
public static class Call extends Instruction {
327-
public final Type.TypeFunction callee;
327+
public final EZType.EZTypeFunction callee;
328328
public final int newbase;
329-
public Call(int newbase, Operand.RegisterOperand returnOperand, Type.TypeFunction callee, Operand.RegisterOperand... args) {
329+
public Call(int newbase, Operand.RegisterOperand returnOperand, EZType.EZTypeFunction callee, Operand.RegisterOperand... args) {
330330
super(I_CALL, returnOperand, args);
331331
this.callee = callee;
332332
this.newbase = newbase;

optvm/src/main/java/com/compilerprogramming/ezlang/compiler/Operand.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
package com.compilerprogramming.ezlang.compiler;
22

33
import com.compilerprogramming.ezlang.types.Symbol;
4-
import com.compilerprogramming.ezlang.types.Type;
4+
import com.compilerprogramming.ezlang.types.EZType;
55

66
public class Operand {
77

8-
Type type;
8+
EZType type;
99

1010
public static class ConstantOperand extends Operand {
1111
public final long value;
12-
public ConstantOperand(long value, Type type) {
12+
public ConstantOperand(long value, EZType type) {
1313
this.value = value;
1414
this.type = type;
1515
}
@@ -20,7 +20,7 @@ public String toString() {
2020
}
2121

2222
public static class NullConstantOperand extends Operand {
23-
public NullConstantOperand(Type type) {
23+
public NullConstantOperand(EZType type) {
2424
this.type = type;
2525
}
2626
@Override
@@ -61,8 +61,8 @@ public RegisterOperand copy(Register register) {
6161
}
6262

6363
public static class LocalFunctionOperand extends Operand {
64-
public final Type.TypeFunction functionType;
65-
public LocalFunctionOperand(Type.TypeFunction functionType) {
64+
public final EZType.EZTypeFunction functionType;
65+
public LocalFunctionOperand(EZType.EZTypeFunction functionType) {
6666
this.functionType = functionType;
6767
}
6868
@Override

optvm/src/main/java/com/compilerprogramming/ezlang/compiler/Register.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package com.compilerprogramming.ezlang.compiler;
22

3-
import com.compilerprogramming.ezlang.types.Type;
3+
import com.compilerprogramming.ezlang.types.EZType;
44

55
/**
66
* Represents a Virtual Register in the abstract machine.
@@ -44,18 +44,18 @@ public class Register {
4444
/**
4545
* The type of the register
4646
*/
47-
public final Type type;
47+
public final EZType type;
4848
/**
4949
* The location of this register relative to the base
5050
* of the executing function. Multiple registers may share the same
5151
* frame slot because of different non-overlapping life times.
5252
*/
5353
protected int frameSlot;
5454

55-
public Register(int id, String name, Type type) {
55+
public Register(int id, String name, EZType type) {
5656
this(id,name,type,id); // Initially frame slot is set to the unique ID
5757
}
58-
protected Register(int id, String name, Type type, int frameSlot) {
58+
protected Register(int id, String name, EZType type, int frameSlot) {
5959
this.id = id;
6060
this.name = name;
6161
this.type = type;

optvm/src/main/java/com/compilerprogramming/ezlang/compiler/RegisterPool.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package com.compilerprogramming.ezlang.compiler;
22

3-
import com.compilerprogramming.ezlang.types.Type;
3+
import com.compilerprogramming.ezlang.types.EZType;
44

55
import java.util.ArrayList;
66

@@ -18,20 +18,20 @@ public class RegisterPool {
1818
public Register getReg(int regNumber) {
1919
return registers.get(regNumber);
2020
}
21-
public Register newReg(String baseName, Type type) {
21+
public Register newReg(String baseName, EZType type) {
2222
var id = registers.size();
2323
var reg = new Register(id, baseName, type);
2424
registers.add(reg);
2525
return reg;
2626
}
27-
public Register newTempReg(Type type) {
27+
public Register newTempReg(EZType type) {
2828
var id = registers.size();
2929
var name = "%t"+id;
3030
var reg = new Register(id, name, type);
3131
registers.add(reg);
3232
return reg;
3333
}
34-
public Register newTempReg(String baseName, Type type) {
34+
public Register newTempReg(String baseName, EZType type) {
3535
var id = registers.size();
3636
var name = baseName+"_"+id;
3737
var reg = new Register(id, name, type);

optvm/src/main/java/com/compilerprogramming/ezlang/compiler/SparseConditionalConstantPropagation.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package com.compilerprogramming.ezlang.compiler;
22

33
import com.compilerprogramming.ezlang.exceptions.CompilerException;
4-
import com.compilerprogramming.ezlang.types.Type;
4+
import com.compilerprogramming.ezlang.types.EZType;
55

66
import java.util.*;
77

@@ -361,7 +361,7 @@ private boolean evalInstruction(Instruction instruction) {
361361
} else throw new IllegalStateException();
362362
}
363363
case Instruction.Call callInst -> {
364-
if (!(callInst.callee.returnType instanceof Type.TypeVoid)) {
364+
if (!(callInst.callee.returnType instanceof EZType.EZTypeVoid)) {
365365
var cell = valueLattice.get(callInst.returnOperand().reg);
366366
changed = cell.setKind(V_VARYING);
367367
}

0 commit comments

Comments
 (0)