Skip to content

Commit 6c9ca8e

Browse files
Merge pull request #48 from CompilerProgramming/son21
Sync with final ch21
2 parents 59b5d14 + c86989f commit 6c9ca8e

File tree

4 files changed

+98
-13
lines changed
  • parser/src/main/java/com/compilerprogramming/ezlang/parser
  • seaofnodes/src

4 files changed

+98
-13
lines changed

parser/src/main/java/com/compilerprogramming/ezlang/parser/AST.java

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -545,8 +545,14 @@ public void accept(ASTVisitor visitor) {
545545
*/
546546
public static class NewExpr extends Expr {
547547
public final TypeExpr typeExpr;
548+
public final long len; // temp hack, as this needs to be an expression rather than constant
548549
public NewExpr(TypeExpr typeExpr) {
549550
this.typeExpr = typeExpr;
551+
this.len = 0;
552+
}
553+
public NewExpr(TypeExpr typeExpr, long len) {
554+
this.typeExpr = typeExpr;
555+
this.len = len;
550556
}
551557
@Override
552558
public StringBuilder toStr(StringBuilder sb) {
@@ -572,8 +578,13 @@ public static class InitExpr extends Expr {
572578
public final NewExpr newExpr;
573579
public final List<Expr> initExprList;
574580
public InitExpr(NewExpr newExpr, List<Expr> initExprList) {
575-
this.newExpr = newExpr;
576581
this.initExprList = initExprList;
582+
// For arrays we compute length based on number of elements
583+
// This is not actually correct - see https://github.com/CompilerProgramming/ez-lang/issues/47
584+
if (initExprList.size() != newExpr.len)
585+
this.newExpr = new NewExpr(newExpr.typeExpr,initExprList.size());
586+
else
587+
this.newExpr = newExpr;
577588
}
578589
@Override
579590
public StringBuilder toStr(StringBuilder sb) {

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -554,7 +554,7 @@ private Node compileNewExpr(AST.NewExpr newExpr) {
554554
Type type = newExpr.type;
555555
if (type instanceof Type.TypeArray typeArray) {
556556
SONTypeMemPtr tarray = (SONTypeMemPtr) TYPES.get(typeArray.name());
557-
return newArray(tarray._obj,ZERO);
557+
return newArray(tarray._obj,newExpr.len==0?ZERO:con(newExpr.len));
558558
}
559559
else if (type instanceof Type.TypeStruct typeStruct) {
560560
SONTypeMemPtr tptr = (SONTypeMemPtr) TYPES.get(typeStruct.name());
@@ -686,6 +686,7 @@ private Node compileCallExpr(AST.CallExpr callExpr) {
686686

687687
// Post-call setup
688688
CallEndNode cend = (CallEndNode)new CallEndNode(call).peephole();
689+
call.peephole(); // Rerun peeps after CallEnd, allows early inlining
689690
// Control from CallEnd
690691
ctrl(new CProjNode(cend,0,ScopeNode.CTRL).peephole());
691692
// Memory from CallEnd
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
// -*- mode: java; -*-
2+
// based on the top-down version from https://en.wikipedia.org/wiki/Merge_sort
3+
4+
val merge_sort = { int[] a, int[] b, int n ->
5+
copy_array (a, 0, n, b);
6+
split_merge(a, 0, n, b);
7+
};
8+
9+
val split_merge = { int[] b, int begin, int end, int[] a ->
10+
if (end - begin <= 1)
11+
return 0;
12+
int middle = (end + begin) / 2;
13+
split_merge(a, begin, middle, b);
14+
split_merge(a, middle, end, b);
15+
merge(b, begin, middle, end, a);
16+
return 0;
17+
};
18+
19+
val merge = { int[] b, int begin, int middle, int end, int[] a ->
20+
int i = begin, j = middle;
21+
22+
for (int k = begin; k < end; k++) {
23+
// && and ||
24+
bool cond = false;
25+
if (i < middle) {
26+
if (j >= end) cond = true;
27+
else if (a[i] <= a[j]) cond = true;
28+
}
29+
if (cond) b[k] = a[i++];
30+
else b[k] = a[j++];
31+
}
32+
};
33+
34+
val copy_array = { int[] a, int begin, int end, int[] b ->
35+
for (int k = begin; k < end; k++)
36+
b[k] = a[k];
37+
};
38+
39+
val eq = { int[] a, int[] b, int n ->
40+
int result = 1;
41+
int i = 0;
42+
while (i < n) {
43+
if (a[i] != b[i]) {
44+
result = 0;
45+
break;
46+
}
47+
i = i + 1;
48+
}
49+
return result;
50+
};
51+
52+
val main = { ->
53+
int[] !a = new int[10];
54+
a[0] = 10; a[1] = 9; a[2] = 8; a[3] = 7; a[4] = 6;
55+
a[5] = 5; a[6] = 4; a[7] = 3; a[8] = 2; a[9] = 1;
56+
int[] !b = new int[10];
57+
b[0] = 0; b[1] = 0; b[2] = 0; b[3] = 0; b[4] = 0;
58+
b[5] = 0; b[6] = 0; b[7] = 0; b[8] = 0; b[9] = 0;
59+
int[] !expect = new int[10];
60+
expect[0] = 1; expect[1] = 2; expect[2] = 3; expect[3] = 4; expect[4] = 5;
61+
expect[5] = 6; expect[6] = 7; expect[7] = 8; expect[8] = 9; expect[9] = 10;
62+
merge_sort(a, b, 10);
63+
return eq(a,expect,10);
64+
};

seaofnodes/src/test/java/com/compilerprogramming/ezlang/compiler/Simple.java

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,12 @@ public class Simple {
1616
static final int DUMP_AFTER_PARSE = 1<<0;
1717
static final int DUMP_AFTER_OPTO = 1<<1;
1818
static final int DUMP_AFTER_TYPE_CHECK = 1<<2;
19-
static final int DUMP_AFTER_INSTR_SELECT = 1<<3;
20-
static final int DUMP_AFTER_GCM = 1<<4;
21-
static final int DUMP_AFTER_LOCAL_SCHED = 1<<5;
22-
static final int DUMP_AFTER_REG_ALLOC = 1<<6;
23-
static final int DUMP_AFTER_ENCODE = 1<<7;
19+
static final int DUMP_AFTER_LOOP_TREE = 1<<3;
20+
static final int DUMP_AFTER_INSTR_SELECT = 1<<4;
21+
static final int DUMP_AFTER_GCM = 1<<5;
22+
static final int DUMP_AFTER_LOCAL_SCHED = 1<<6;
23+
static final int DUMP_AFTER_REG_ALLOC = 1<<7;
24+
static final int DUMP_AFTER_ENCODE = 1<<8;
2425

2526
static final int DUMP_FINAL = 1<<16;
2627

@@ -88,12 +89,13 @@ static void dump(CodeGen code, int dump, int pass) {
8889
case DUMP_AFTER_PARSE -> "01-parse.dot";
8990
case DUMP_AFTER_OPTO -> "02-opto.dot";
9091
case DUMP_AFTER_TYPE_CHECK -> "03-type_check.dot";
91-
case DUMP_AFTER_INSTR_SELECT -> "04-instr_select.dot";
92-
case DUMP_AFTER_GCM -> "05-gcm.dot";
93-
case DUMP_AFTER_LOCAL_SCHED -> "06-local_sched.dot";
94-
case DUMP_AFTER_REG_ALLOC -> "07-reg_allos.dot";
95-
case DUMP_AFTER_ENCODE -> "08-local_sched.dot";
96-
case DUMP_FINAL -> "09-final.dot";
92+
case DUMP_AFTER_LOOP_TREE -> "04-loop_tree.dot";
93+
case DUMP_AFTER_INSTR_SELECT -> "05-instr_select.dot";
94+
case DUMP_AFTER_GCM -> "06-gcm.dot";
95+
case DUMP_AFTER_LOCAL_SCHED -> "07-local_sched.dot";
96+
case DUMP_AFTER_REG_ALLOC -> "08-reg_allos.dot";
97+
case DUMP_AFTER_ENCODE -> "09-local_sched.dot";
98+
case DUMP_FINAL -> "10-final.dot";
9799
default -> throw Utils.TODO();
98100
};
99101

@@ -110,6 +112,7 @@ static void dump(CodeGen code, int dump, int pass) {
110112
case DUMP_AFTER_PARSE -> "After Parse:";
111113
case DUMP_AFTER_OPTO -> "After OPTO:";
112114
case DUMP_AFTER_TYPE_CHECK -> "After Type Check:";
115+
case DUMP_AFTER_LOOP_TREE -> "After Loop Tree:";
113116
case DUMP_AFTER_INSTR_SELECT -> "After Instruction Selection:";
114117
case DUMP_AFTER_GCM -> "After GCM:";
115118
case DUMP_AFTER_LOCAL_SCHED -> "After Local Scheduling:";
@@ -134,6 +137,8 @@ static void print_compilation_times(CodeGen code) {
134137
System.out.println(String.format("Optimization Time: %.3f sec", t));
135138
total += t = code._tTypeCheck / 1e3;
136139
System.out.println(String.format("Type Checking Time: %.3f sec", t));
140+
total += t = code._tLoopTree / 1e3;
141+
System.out.println(String.format("Loop Tree Time: %.3f sec", t));
137142
total += t = code._tInsSel / 1e3;
138143
System.out.println(String.format("Instruction Selection Time: %.3f sec", t));
139144
total += t = code._tGCM / 1e3;
@@ -180,6 +185,7 @@ public static void main(String[] args) throws Exception {
180185
case "--dump-after-parse": dump |= DUMP_AFTER_PARSE; break;
181186
case "--dump-after-opto": dump |= DUMP_AFTER_OPTO; break;
182187
case "--dump-after-type-check": dump |= DUMP_AFTER_TYPE_CHECK; break;
188+
case "--dump-after-loop-tree": dump |= DUMP_AFTER_LOOP_TREE; break;
183189
case "--dump-after-instr-select": dump |= DUMP_AFTER_INSTR_SELECT; break;
184190
case "--dump-after-gcm": dump |= DUMP_AFTER_GCM; break;
185191
case "--dump-after-local-sched": dump |= DUMP_AFTER_LOCAL_SCHED; break;
@@ -250,6 +256,9 @@ public static void main(String[] args) throws Exception {
250256
code.typeCheck();
251257
dump(code, dump, DUMP_AFTER_TYPE_CHECK);
252258

259+
code.loopTree();
260+
dump(code, dump, DUMP_AFTER_LOOP_TREE);
261+
253262
if (do_codegen) {
254263
code.instSelect(cpu, abi);
255264
dump(code, dump, DUMP_AFTER_INSTR_SELECT);

0 commit comments

Comments
 (0)