Skip to content

Commit b1522b8

Browse files
Part 1
Part 2 Part 3 Part 4 Temp disable stackvm AST revision to capture load/store array/field info
1 parent f027848 commit b1522b8

File tree

19 files changed

+420
-267
lines changed

19 files changed

+420
-267
lines changed

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

Lines changed: 44 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -309,11 +309,17 @@ private boolean compileExpr(AST.Expr expr) {
309309
case AST.NewExpr newExpr -> {
310310
return compileNewExpr(newExpr);
311311
}
312-
case AST.ArrayIndexExpr arrayIndexExpr -> {
313-
return compileArrayIndexExpr(arrayIndexExpr);
312+
case AST.InitExpr initExpr -> {
313+
return compileInitExpr(initExpr);
314314
}
315-
case AST.FieldExpr fieldExpr -> {
316-
return compileFieldExpr(fieldExpr);
315+
case AST.ArrayLoadExpr arrayLoadExpr -> {
316+
return compileArrayIndexExpr(arrayLoadExpr);
317+
}
318+
case AST.ArrayStoreExpr arrayStoreExpr -> {
319+
return compileArrayStoreExpr(arrayStoreExpr);
320+
}
321+
case AST.GetFieldExpr getFieldExpr -> {
322+
return compileFieldExpr(getFieldExpr);
317323
}
318324
case AST.SetFieldExpr setFieldExpr -> {
319325
return compileSetFieldExpr(setFieldExpr);
@@ -370,7 +376,7 @@ else if (t instanceof Type.TypeNullable ptr &&
370376
throw new CompilerException("Unexpected type: " + t);
371377
}
372378

373-
private boolean compileFieldExpr(AST.FieldExpr fieldExpr) {
379+
private boolean compileFieldExpr(AST.GetFieldExpr fieldExpr) {
374380
Type.TypeStruct typeStruct = getStructType(fieldExpr.object.type);
375381
int fieldIndex = typeStruct.getFieldIndex(fieldExpr.fieldName);
376382
if (fieldIndex < 0)
@@ -382,7 +388,7 @@ private boolean compileFieldExpr(AST.FieldExpr fieldExpr) {
382388
return true;
383389
}
384390

385-
private boolean compileArrayIndexExpr(AST.ArrayIndexExpr arrayIndexExpr) {
391+
private boolean compileArrayIndexExpr(AST.ArrayLoadExpr arrayIndexExpr) {
386392
compileExpr(arrayIndexExpr.array);
387393
boolean indexed = compileExpr(arrayIndexExpr.expr);
388394
if (indexed)
@@ -394,34 +400,50 @@ private boolean compileArrayIndexExpr(AST.ArrayIndexExpr arrayIndexExpr) {
394400
}
395401

396402
private boolean compileSetFieldExpr(AST.SetFieldExpr setFieldExpr) {
397-
Type.TypeStruct structType = (Type.TypeStruct) setFieldExpr.objectType;
403+
Type.TypeStruct structType = (Type.TypeStruct) setFieldExpr.object.type;
398404
int fieldIndex = structType.getFieldIndex(setFieldExpr.fieldName);
399405
if (fieldIndex == -1)
400406
throw new CompilerException("Field " + setFieldExpr.fieldName + " not found in struct " + structType.name);
401-
pushOperand(new Operand.LoadFieldOperand(top(), setFieldExpr.fieldName, fieldIndex));
407+
if (setFieldExpr instanceof AST.InitFieldExpr)
408+
pushOperand(top());
409+
else
410+
compileExpr(setFieldExpr.object);
411+
pushOperand(new Operand.LoadFieldOperand(pop(), setFieldExpr.fieldName, fieldIndex));
402412
boolean indexed = compileExpr(setFieldExpr.value);
403413
if (indexed)
404414
codeIndexedLoad();
405415
codeIndexedStore();
406416
return false;
407417
}
408418

419+
private boolean compileArrayStoreExpr(AST.ArrayStoreExpr arrayStoreExpr) {
420+
if (arrayStoreExpr instanceof AST.ArrayInitExpr)
421+
pushOperand(top()); // Array was created by new
422+
else
423+
compileExpr(arrayStoreExpr.array);
424+
boolean indexed = compileExpr(arrayStoreExpr.expr);
425+
if (indexed)
426+
codeIndexedLoad();
427+
Operand index = pop();
428+
Operand array = pop();
429+
pushOperand(new Operand.LoadIndexedOperand(array, index));
430+
indexed = compileExpr(arrayStoreExpr.value);
431+
if (indexed)
432+
codeIndexedLoad();
433+
codeIndexedStore();
434+
return false;
435+
}
436+
409437
private boolean compileNewExpr(AST.NewExpr newExpr) {
410438
codeNew(newExpr.type);
411-
if (newExpr.initExprList != null && !newExpr.initExprList.isEmpty()) {
412-
if (newExpr.type instanceof Type.TypeArray) {
413-
for (AST.Expr expr : newExpr.initExprList) {
414-
// Maybe have specific AST similar to how we have SetFieldExpr?
415-
boolean indexed = compileExpr(expr);
416-
if (indexed)
417-
codeIndexedLoad();
418-
codeStoreAppend();
419-
}
420-
}
421-
else if (newExpr.type instanceof Type.TypeStruct) {
422-
for (AST.Expr expr : newExpr.initExprList) {
423-
compileExpr(expr);
424-
}
439+
return false;
440+
}
441+
442+
private boolean compileInitExpr(AST.InitExpr initExpr) {
443+
compileExpr(initExpr.newExpr);
444+
if (initExpr.initExprList != null && !initExpr.initExprList.isEmpty()) {
445+
for (AST.Expr expr : initExpr.initExprList) {
446+
compileExpr(expr);
425447
}
426448
}
427449
return false;
@@ -640,15 +662,6 @@ else if (type instanceof Type.TypeStruct typeStruct)
640662
throw new CompilerException("Unexpected type: " + type);
641663
}
642664

643-
private void codeStoreAppend() {
644-
var operand = issa.read(pop());
645-
Operand.RegisterOperand arrayOperand = (Operand.RegisterOperand) issa.read(top());
646-
var insn = new Instruction.AStoreAppend(arrayOperand, operand);
647-
issa.recordUse(arrayOperand, insn);
648-
issa.recordUse(operand, insn);
649-
code(insn);
650-
}
651-
652665
private void codeNewArray(Type.TypeArray typeArray) {
653666
var temp = createTemp(typeArray);
654667
var target = (Operand.RegisterOperand) issa.write(temp);

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

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,8 @@ public abstract class Instruction {
2222
static final int I_NEW_STRUCT = 11;
2323
static final int I_ARRAY_STORE = 12;
2424
static final int I_ARRAY_LOAD = 13;
25-
static final int I_ARRAY_APPEND = 14;
26-
static final int I_FIELD_GET = 15;
27-
static final int I_FIELD_SET = 16;
25+
static final int I_FIELD_GET = 14;
26+
static final int I_FIELD_SET = 15;
2827

2928
public final int opcode;
3029
protected Operand.RegisterOperand def;
@@ -271,18 +270,6 @@ public StringBuilder toStr(StringBuilder sb) {
271270
}
272271
}
273272

274-
public static class AStoreAppend extends Instruction {
275-
public AStoreAppend(Operand.RegisterOperand array, Operand value) {
276-
super(I_ARRAY_APPEND, (Operand.RegisterOperand) null, array, value);
277-
}
278-
public Operand.RegisterOperand array() { return (Operand.RegisterOperand) uses[0]; }
279-
public Operand value() { return uses[1]; }
280-
@Override
281-
public StringBuilder toStr(StringBuilder sb) {
282-
return sb.append(uses[0]).append(".append(").append(uses[1]).append(")");
283-
}
284-
}
285-
286273
public static class ConditionalBranch extends Instruction {
287274
public final BasicBlock trueBlock;
288275
public final BasicBlock falseBlock;

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

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -421,8 +421,6 @@ else if (binaryInst.right() instanceof Operand.RegisterOperand registerOperand)
421421
var cell = valueLattice.get(newStructInst.destOperand().reg);
422422
changed = cell.setKind(V_VARYING);
423423
}
424-
case Instruction.AStoreAppend arrayAppendInst -> {
425-
}
426424
case Instruction.ArrayStore arrayStoreInst -> {
427425
}
428426
case Instruction.ArrayLoad arrayLoadInst -> {

optvm/src/main/java/com/compilerprogramming/ezlang/interpreter/Interpreter.java

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -205,19 +205,6 @@ else if (binaryInst.right() instanceof Operand.RegisterOperand registerOperand)
205205
case Instruction.NewStruct newStructInst -> {
206206
execStack.stack[base + newStructInst.destOperand().frameSlot()] = new Value.StructValue(newStructInst.type);
207207
}
208-
case Instruction.AStoreAppend arrayAppendInst -> {
209-
Value.ArrayValue arrayValue = (Value.ArrayValue) execStack.stack[base + arrayAppendInst.array().frameSlot()];
210-
if (arrayAppendInst.value() instanceof Operand.ConstantOperand constant) {
211-
arrayValue.values.add(new Value.IntegerValue(constant.value));
212-
}
213-
else if (arrayAppendInst.value() instanceof Operand.NullConstantOperand) {
214-
arrayValue.values.add(new Value.NullValue());
215-
}
216-
else if (arrayAppendInst.value() instanceof Operand.RegisterOperand registerOperand) {
217-
arrayValue.values.add(execStack.stack[base + registerOperand.frameSlot()]);
218-
}
219-
else throw new IllegalStateException();
220-
}
221208
case Instruction.ArrayStore arrayStoreInst -> {
222209
if (arrayStoreInst.arrayOperand() instanceof Operand.RegisterOperand arrayOperand) {
223210
Value.ArrayValue arrayValue = (Value.ArrayValue) execStack.stack[base + arrayOperand.frameSlot()];
@@ -241,7 +228,10 @@ else if (arrayStoreInst.sourceOperand() instanceof Operand.RegisterOperand regis
241228
value = execStack.stack[base + registerOperand.frameSlot()];
242229
}
243230
else throw new IllegalStateException();
244-
arrayValue.values.set(index, value);
231+
if (index == arrayValue.values.size())
232+
arrayValue.values.add(value);
233+
else
234+
arrayValue.values.set(index, value);
245235
} else throw new IllegalStateException();
246236
}
247237
case Instruction.ArrayLoad arrayLoadInst -> {

optvm/src/test/java/com/compilerprogramming/ezlang/compiler/TestCompiler.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -215,9 +215,9 @@ func foo()->[Int] {
215215
Assert.assertEquals("""
216216
L0:
217217
%t0 = New([Int])
218-
%t0.append(1)
219-
%t0.append(2)
220-
%t0.append(3)
218+
%t0[0] = 1
219+
%t0[1] = 2
220+
%t0[2] = 3
221221
ret %t0
222222
goto L1
223223
L1:
@@ -236,7 +236,7 @@ func foo(n: Int) -> [Int] {
236236
L0:
237237
arg n
238238
%t1 = New([Int])
239-
%t1.append(n)
239+
%t1[0] = n
240240
ret %t1
241241
goto L1
242242
L1:

optvm/src/test/java/com/compilerprogramming/ezlang/compiler/TestSSATransform.java

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1602,8 +1602,8 @@ func foo()->Int {
16021602
==========
16031603
L0:
16041604
%t2 = New([Int])
1605-
%t2.append(1)
1606-
%t2.append(2)
1605+
%t2[0] = 1
1606+
%t2[1] = 2
16071607
arr = %t2
16081608
arr[0] = 10
16091609
%t3 = arr[0]
@@ -1615,8 +1615,8 @@ func foo()->Int {
16151615
=========
16161616
L0:
16171617
%t2_0 = New([Int])
1618-
%t2_0.append(1)
1619-
%t2_0.append(2)
1618+
%t2_0[0] = 1
1619+
%t2_0[1] = 2
16201620
arr_0 = %t2_0
16211621
arr_0[0] = 10
16221622
%t3_0 = arr_0[0]
@@ -1628,8 +1628,8 @@ func foo()->Int {
16281628
=================
16291629
L0:
16301630
%t2_0 = New([Int])
1631-
%t2_0.append(1)
1632-
%t2_0.append(2)
1631+
%t2_0[0] = 1
1632+
%t2_0[1] = 2
16331633
arr_0 = %t2_0
16341634
arr_0[0] = 10
16351635
%t3_0 = arr_0[0]
@@ -2738,8 +2738,8 @@ func foo()->Int
27382738
%t1 = New([Foo?])
27392739
%t2 = New(Foo)
27402740
%t2.i = 1
2741-
%t1.append(%t2)
2742-
%t1.append(null)
2741+
%t1[0] = %t2
2742+
%t1[1] = null
27432743
f = %t1
27442744
%t3 = f[1]
27452745
%t4 = null==%t3
@@ -2762,8 +2762,8 @@ func foo()->Int
27622762
%t1_0 = New([Foo?])
27632763
%t2_0 = New(Foo)
27642764
%t2_0.i = 1
2765-
%t1_0.append(%t2_0)
2766-
%t1_0.append(null)
2765+
%t1_0[0] = %t2_0
2766+
%t1_0[1] = null
27672767
f_0 = %t1_0
27682768
%t3_0 = f_0[1]
27692769
%t4_0 = null==%t3_0
@@ -2787,8 +2787,8 @@ func foo()->Int
27872787
%t1_0 = New([Foo?])
27882788
%t2_0 = New(Foo)
27892789
%t2_0.i = 1
2790-
%t1_0.append(%t2_0)
2791-
%t1_0.append(null)
2790+
%t1_0[0] = %t2_0
2791+
%t1_0[1] = null
27922792
f_0 = %t1_0
27932793
%t3_0 = f_0[1]
27942794
%t4_0 = null==%t3_0

0 commit comments

Comments
 (0)