Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -309,11 +309,17 @@ private boolean compileExpr(AST.Expr expr) {
case AST.NewExpr newExpr -> {
return compileNewExpr(newExpr);
}
case AST.ArrayIndexExpr arrayIndexExpr -> {
return compileArrayIndexExpr(arrayIndexExpr);
case AST.InitExpr initExpr -> {
return compileInitExpr(initExpr);
}
case AST.FieldExpr fieldExpr -> {
return compileFieldExpr(fieldExpr);
case AST.ArrayLoadExpr arrayLoadExpr -> {
return compileArrayIndexExpr(arrayLoadExpr);
}
case AST.ArrayStoreExpr arrayStoreExpr -> {
return compileArrayStoreExpr(arrayStoreExpr);
}
case AST.GetFieldExpr getFieldExpr -> {
return compileFieldExpr(getFieldExpr);
}
case AST.SetFieldExpr setFieldExpr -> {
return compileSetFieldExpr(setFieldExpr);
Expand Down Expand Up @@ -370,7 +376,7 @@ else if (t instanceof Type.TypeNullable ptr &&
throw new CompilerException("Unexpected type: " + t);
}

private boolean compileFieldExpr(AST.FieldExpr fieldExpr) {
private boolean compileFieldExpr(AST.GetFieldExpr fieldExpr) {
Type.TypeStruct typeStruct = getStructType(fieldExpr.object.type);
int fieldIndex = typeStruct.getFieldIndex(fieldExpr.fieldName);
if (fieldIndex < 0)
Expand All @@ -382,7 +388,7 @@ private boolean compileFieldExpr(AST.FieldExpr fieldExpr) {
return true;
}

private boolean compileArrayIndexExpr(AST.ArrayIndexExpr arrayIndexExpr) {
private boolean compileArrayIndexExpr(AST.ArrayLoadExpr arrayIndexExpr) {
compileExpr(arrayIndexExpr.array);
boolean indexed = compileExpr(arrayIndexExpr.expr);
if (indexed)
Expand All @@ -394,34 +400,50 @@ private boolean compileArrayIndexExpr(AST.ArrayIndexExpr arrayIndexExpr) {
}

private boolean compileSetFieldExpr(AST.SetFieldExpr setFieldExpr) {
Type.TypeStruct structType = (Type.TypeStruct) setFieldExpr.objectType;
Type.TypeStruct structType = (Type.TypeStruct) setFieldExpr.object.type;
int fieldIndex = structType.getFieldIndex(setFieldExpr.fieldName);
if (fieldIndex == -1)
throw new CompilerException("Field " + setFieldExpr.fieldName + " not found in struct " + structType.name);
pushOperand(new Operand.LoadFieldOperand(top(), setFieldExpr.fieldName, fieldIndex));
if (setFieldExpr instanceof AST.InitFieldExpr)
pushOperand(top());
else
compileExpr(setFieldExpr.object);
pushOperand(new Operand.LoadFieldOperand(pop(), setFieldExpr.fieldName, fieldIndex));
boolean indexed = compileExpr(setFieldExpr.value);
if (indexed)
codeIndexedLoad();
codeIndexedStore();
return false;
}

private boolean compileArrayStoreExpr(AST.ArrayStoreExpr arrayStoreExpr) {
if (arrayStoreExpr instanceof AST.ArrayInitExpr)
pushOperand(top()); // Array was created by new
else
compileExpr(arrayStoreExpr.array);
boolean indexed = compileExpr(arrayStoreExpr.expr);
if (indexed)
codeIndexedLoad();
Operand index = pop();
Operand array = pop();
pushOperand(new Operand.LoadIndexedOperand(array, index));
indexed = compileExpr(arrayStoreExpr.value);
if (indexed)
codeIndexedLoad();
codeIndexedStore();
return false;
}

private boolean compileNewExpr(AST.NewExpr newExpr) {
codeNew(newExpr.type);
if (newExpr.initExprList != null && !newExpr.initExprList.isEmpty()) {
if (newExpr.type instanceof Type.TypeArray) {
for (AST.Expr expr : newExpr.initExprList) {
// Maybe have specific AST similar to how we have SetFieldExpr?
boolean indexed = compileExpr(expr);
if (indexed)
codeIndexedLoad();
codeStoreAppend();
}
}
else if (newExpr.type instanceof Type.TypeStruct) {
for (AST.Expr expr : newExpr.initExprList) {
compileExpr(expr);
}
return false;
}

private boolean compileInitExpr(AST.InitExpr initExpr) {
compileExpr(initExpr.newExpr);
if (initExpr.initExprList != null && !initExpr.initExprList.isEmpty()) {
for (AST.Expr expr : initExpr.initExprList) {
compileExpr(expr);
}
}
return false;
Expand Down Expand Up @@ -640,15 +662,6 @@ else if (type instanceof Type.TypeStruct typeStruct)
throw new CompilerException("Unexpected type: " + type);
}

private void codeStoreAppend() {
var operand = issa.read(pop());
Operand.RegisterOperand arrayOperand = (Operand.RegisterOperand) issa.read(top());
var insn = new Instruction.AStoreAppend(arrayOperand, operand);
issa.recordUse(arrayOperand, insn);
issa.recordUse(operand, insn);
code(insn);
}

private void codeNewArray(Type.TypeArray typeArray) {
var temp = createTemp(typeArray);
var target = (Operand.RegisterOperand) issa.write(temp);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,8 @@ public abstract class Instruction {
static final int I_NEW_STRUCT = 11;
static final int I_ARRAY_STORE = 12;
static final int I_ARRAY_LOAD = 13;
static final int I_ARRAY_APPEND = 14;
static final int I_FIELD_GET = 15;
static final int I_FIELD_SET = 16;
static final int I_FIELD_GET = 14;
static final int I_FIELD_SET = 15;

public final int opcode;
protected Operand.RegisterOperand def;
Expand Down Expand Up @@ -271,18 +270,6 @@ public StringBuilder toStr(StringBuilder sb) {
}
}

public static class AStoreAppend extends Instruction {
public AStoreAppend(Operand.RegisterOperand array, Operand value) {
super(I_ARRAY_APPEND, (Operand.RegisterOperand) null, array, value);
}
public Operand.RegisterOperand array() { return (Operand.RegisterOperand) uses[0]; }
public Operand value() { return uses[1]; }
@Override
public StringBuilder toStr(StringBuilder sb) {
return sb.append(uses[0]).append(".append(").append(uses[1]).append(")");
}
}

public static class ConditionalBranch extends Instruction {
public final BasicBlock trueBlock;
public final BasicBlock falseBlock;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -421,8 +421,6 @@ else if (binaryInst.right() instanceof Operand.RegisterOperand registerOperand)
var cell = valueLattice.get(newStructInst.destOperand().reg);
changed = cell.setKind(V_VARYING);
}
case Instruction.AStoreAppend arrayAppendInst -> {
}
case Instruction.ArrayStore arrayStoreInst -> {
}
case Instruction.ArrayLoad arrayLoadInst -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -205,19 +205,6 @@ else if (binaryInst.right() instanceof Operand.RegisterOperand registerOperand)
case Instruction.NewStruct newStructInst -> {
execStack.stack[base + newStructInst.destOperand().frameSlot()] = new Value.StructValue(newStructInst.type);
}
case Instruction.AStoreAppend arrayAppendInst -> {
Value.ArrayValue arrayValue = (Value.ArrayValue) execStack.stack[base + arrayAppendInst.array().frameSlot()];
if (arrayAppendInst.value() instanceof Operand.ConstantOperand constant) {
arrayValue.values.add(new Value.IntegerValue(constant.value));
}
else if (arrayAppendInst.value() instanceof Operand.NullConstantOperand) {
arrayValue.values.add(new Value.NullValue());
}
else if (arrayAppendInst.value() instanceof Operand.RegisterOperand registerOperand) {
arrayValue.values.add(execStack.stack[base + registerOperand.frameSlot()]);
}
else throw new IllegalStateException();
}
case Instruction.ArrayStore arrayStoreInst -> {
if (arrayStoreInst.arrayOperand() instanceof Operand.RegisterOperand arrayOperand) {
Value.ArrayValue arrayValue = (Value.ArrayValue) execStack.stack[base + arrayOperand.frameSlot()];
Expand All @@ -241,7 +228,10 @@ else if (arrayStoreInst.sourceOperand() instanceof Operand.RegisterOperand regis
value = execStack.stack[base + registerOperand.frameSlot()];
}
else throw new IllegalStateException();
arrayValue.values.set(index, value);
if (index == arrayValue.values.size())
arrayValue.values.add(value);
else
arrayValue.values.set(index, value);
} else throw new IllegalStateException();
}
case Instruction.ArrayLoad arrayLoadInst -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -215,9 +215,9 @@ func foo()->[Int] {
Assert.assertEquals("""
L0:
%t0 = New([Int])
%t0.append(1)
%t0.append(2)
%t0.append(3)
%t0[0] = 1
%t0[1] = 2
%t0[2] = 3
ret %t0
goto L1
L1:
Expand All @@ -236,7 +236,7 @@ func foo(n: Int) -> [Int] {
L0:
arg n
%t1 = New([Int])
%t1.append(n)
%t1[0] = n
ret %t1
goto L1
L1:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1602,8 +1602,8 @@ func foo()->Int {
==========
L0:
%t2 = New([Int])
%t2.append(1)
%t2.append(2)
%t2[0] = 1
%t2[1] = 2
arr = %t2
arr[0] = 10
%t3 = arr[0]
Expand All @@ -1615,8 +1615,8 @@ func foo()->Int {
=========
L0:
%t2_0 = New([Int])
%t2_0.append(1)
%t2_0.append(2)
%t2_0[0] = 1
%t2_0[1] = 2
arr_0 = %t2_0
arr_0[0] = 10
%t3_0 = arr_0[0]
Expand All @@ -1628,8 +1628,8 @@ func foo()->Int {
=================
L0:
%t2_0 = New([Int])
%t2_0.append(1)
%t2_0.append(2)
%t2_0[0] = 1
%t2_0[1] = 2
arr_0 = %t2_0
arr_0[0] = 10
%t3_0 = arr_0[0]
Expand Down Expand Up @@ -2738,8 +2738,8 @@ func foo()->Int
%t1 = New([Foo?])
%t2 = New(Foo)
%t2.i = 1
%t1.append(%t2)
%t1.append(null)
%t1[0] = %t2
%t1[1] = null
f = %t1
%t3 = f[1]
%t4 = null==%t3
Expand All @@ -2762,8 +2762,8 @@ func foo()->Int
%t1_0 = New([Foo?])
%t2_0 = New(Foo)
%t2_0.i = 1
%t1_0.append(%t2_0)
%t1_0.append(null)
%t1_0[0] = %t2_0
%t1_0[1] = null
f_0 = %t1_0
%t3_0 = f_0[1]
%t4_0 = null==%t3_0
Expand All @@ -2787,8 +2787,8 @@ func foo()->Int
%t1_0 = New([Foo?])
%t2_0 = New(Foo)
%t2_0.i = 1
%t1_0.append(%t2_0)
%t1_0.append(null)
%t1_0[0] = %t2_0
%t1_0[1] = null
f_0 = %t1_0
%t3_0 = f_0[1]
%t4_0 = null==%t3_0
Expand Down
Loading
Loading