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
|
package com.seaofnodes.simple;
import com.seaofnodes.simple.util.Utils;
import com.seaofnodes.simple.codegen.CodeGen;
import com.seaofnodes.simple.node.cpus.arm.arm;
import com.seaofnodes.simple.node.cpus.riscv.riscv;
import java.io.IOException;
import org.junit.Ignore;
import org.junit.Test;
import static org.junit.Assert.*;
public class Chapter22Test {
@Test
public void testJig() throws IOException {
String src = """
struct s0 {
bool v1;
i16 v2;
int v3;
i8 v4;
byte v5;
};
while(new s0.v3)
while(new s0.v5<<new s0.v4) {}
if(0) {
if(0) {
flt !P5ZUD4=new s0.v2;
}
while(0) {}
}
return new s0.v1;
""";
testCPU(src,"x86_64_v2", "win64" ,-1,null);
testCPU(src,"riscv" , "SystemV",-1,null);
testCPU(src,"arm" , "SystemV",-1,null);
}
static CodeGen testCPU( String src, String cpu, String os, int spills, String stop ) {
CodeGen code = new CodeGen(src).driver(CodeGen.Phase.Encoding,cpu,os);
int delta = spills>>3;
if( delta==0 ) delta = 1;
if( spills != -1 && !CodeGen.iterSeedOverridden() )
assertEquals("Expect spills:",spills,code._regAlloc._spillScaled,delta);
if( stop != null )
assertEquals(stop, code.print());
return code;
}
static int testCPUSize( String src, String cpu, String os, int spills ) {
return testCPU(src,cpu,os,spills, "return 0;" )._encoding._bits.size();
}
// Should not fold away
@Test public void testSextFail() throws IOException {
String src = """
struct _Person { i32 age;};
_Person !p = new _Person;
p.age = (arg<<17)>>17;
return 0;
""";
assertEquals(82, testCPUSize(src, "x86_64_v2","win64", 3 ));
assertEquals(96, testCPUSize(src, "riscv", "SystemV", 4 ));
assertEquals(96, testCPUSize(src, "arm" , "SystemV", 4 ));
// do assertEquals here
EvalRisc5 R5 = TestRisc5.build( src, "sext_str_not_fold_away", 0, 4, false);
int trap = R5.step(100);
assertEquals(0,trap);
EvalArm64 A5 = TestArm64.build("sext_str_not_fold_away", src, 0, 4, false);
trap = A5.step(100);
assertEquals(0,trap);
}
// Should not fold away
@Test public void testSextFail2() throws IOException {
String src = """
struct _Person { i32 age;};
_Person !p = new _Person;
p.age = (arg<<48)>>48;
return 0;
""";
EvalRisc5 R5 = TestRisc5.build( src, "sext_str_not_fold_away_2", 0, 4, false);
int trap = R5.step(100);
assertEquals(0,trap);
EvalArm64 A5 = TestArm64.build("sext_str_not_fold_away_2", src, 0, 3, false);
int trap_arm = A5.step(100);
assertEquals(0,trap_arm);
assertEquals(82, testCPUSize(src, "x86_64_v2","win64",3 ));
assertEquals(96, testCPUSize(src, "riscv", "SystemV",4 ));
assertEquals(96, testCPUSize(src, "arm", "SystemV",4 ));
}
// Should fold away
@Test public void testSextSuccess() throws IOException {
String src = """
// Should fold away sign extend
struct _Person { i8 age;};
_Person !p = new _Person;
p.age = (arg<<48)>>48;
return 0;
""";
EvalRisc5 R5 = TestRisc5.build( src, "sext_str_fold_away", 0, 5, false);
int trap = R5.step(100);
assertEquals(0,trap);
EvalArm64 A5 = TestArm64.build("sext_str_fold_away", src, 0, 5, false);
int trap_arm = A5.step(100);
assertEquals(0,trap_arm);
assertEquals(72, testCPUSize(src, "x86_64_v2","win64",3 ));
assertEquals(92, testCPUSize(src, "riscv", "SystemV",5 ));
assertEquals(92, testCPUSize(src, "arm", "SystemV",5));
// do assertEquals here
}
// Int now is changed to 4 bytes.
@Test public void testPerson() throws IOException {
String src =
"""
struct Person { // Exports a field typed singleton+final *class:Person{age:i32}
i32 age;
};
val fcn = { Person?[] ps, int x -> // exports a field with a constant fcn ptr; exports the fcn also
if( ps[x] )
ps[x].age++;
};
""";
String person = "6\n";
TestC.run(src, "person2", null, TestC.CALL_CONVENTION, "", TestC.C_DRIVERS_DIR+"person.c", person, 2);
// Memory layout starting at PS:
int ps = 1<<16; // Person array pointer starts at heap start
// Person[3] = { len,pad,P0,P1,P2 }; // sizeof = 4*8
// P0 = { age } // sizeof=8
int p0 = ps+4*8+0*8;
// P1 = { age } // sizeof=8
int p1 = ps+4*8+1*8;
// P2 = { age } // sizeof=8
int p2 = ps+4*8+2*8;
EvalRisc5 R5 = TestRisc5.build( src, "fcn", ps, 2, false);
R5.regs[riscv.A1] = 1; // Index 1
R5.st8(ps,3); // Length
R5.st8(ps+1*8,p0);
R5.st8(ps+2*8,p1);
R5.st8(ps+3*8,p2);
R5.st8(p0, 5); // age= 5
R5.st8(p1,17); // age=17
R5.st8(p2,60); // age=60
int trap = R5.step(100);
assertEquals(0,trap);
assertEquals( 5+0,R5.ld8(p0));
assertEquals(17+1,R5.ld8(p1));
assertEquals(60+0,R5.ld8(p2));
EvalArm64 A5 = TestArm64.build("fcn", src, ps, 2, false);
A5.regs[arm.X1] = 1; // Index 1
A5.st8(ps, 3);
A5.st8(ps+1*8,p0);
A5.st8(ps+2*8,p1);
A5.st8(ps+3*8,p2);
A5.st8(p0, 5); // age= 5
A5.st8(p1,17); // age=17
A5.st8(p2,60); // age=60
int trap_arm = A5.step(100);
assertEquals(0,trap_arm);
assertEquals( 5+0, A5.ld8(p0));
assertEquals(17+1, A5.ld8(p1));
assertEquals(60+0, A5.ld8(p2));
}
@Test
public void testCoRecur() {
String src = """
val x = 5; // aa.az; // Error to self-define forward ref
struct _A { Test._B? b; Test._C? c; i64 ax; val az = x*2; new _A = { i64 x -> ax=x; }; };
struct _B { Test._A? a; Test._C? c; f32 bx; val bz = x*3; new _B = { f32 x, Test._A? aa -> bx=x; a=aa; }; };
struct _C { Test._A? a; Test._B? b; f64 cx; val cz = x*x; new _C = { f64 x, Test._A? aa, Test._B? bb -> cx=x; a=aa; b=bb; }; };
_A !aa = new _A(17);
_B !bb = new _B(3.14f, aa);
_C !cc = new _C(2.73, aa, bb);
aa.b = bb;
aa.c = cc;
bb.c = cc;
return cc.cz;
""";
CodeGen code = new CodeGen(src).parse().opto().typeCheck();
assertEquals("return 25;", code.print());
assertEquals("25", Eval2.eval(code, 0));
}
@Test
public void testHelloWorld() throws IOException {
String src = """
// fd buf len -> len
{ i32 i64 u32 -> u32 } write = "C";
// Write byte array to stdout
val p = { u8[~] str ->
i64 ptr = str; // cast array base to i64
return write(1,ptr,str#);
};
p("Hello, World!");
return 0;
""";
TestC.runSF(src,"helloWorld", "Hello, World!",2);
// Evaluate on RISC5 emulator
EvalRisc5 R5 = TestRisc5.build( src, "helloWorld", 0, 5, false);
int trap = R5.step(100);
assertEquals(0,trap);
assertEquals(0,R5.regs[riscv.A0]);
assertEquals("Hello, World!",R5._stdout.toString());
// Evaluate on ARM emulator
EvalArm64 arm = TestArm64.build("helloWorld", src,0, 5, false);
trap = arm.step(100);
assertEquals(0,trap);
assertEquals(0,arm.regs[0]);
assertEquals("Hello, World!",arm._stdout.toString());
}
@Test
public void testStrLoad() {
CodeGen code = new CodeGen("return \"abc\"[1];");
code.parse().opto();
assertEquals("return .[];", code.print());
}
@Test
public void testFinalArray() {
String src = """
int N=4;
i32[] !is = new i32[N];
for( int i=0; i<N; i++ )
is[i] = i*i;
val _sum = { i32[~] is -> // final array
int sum=0;
for( int i=0; i<is#; i++ )
sum += is[i];
return sum;
};
return _sum(is);
""";
CodeGen code = new CodeGen(src).parse().opto().typeCheck();
assertEquals("return Phi(Loop,0,(Phi_sum+.[]));", code.print());
assertEquals("14", Eval2.eval(code, 0));
}
@Test @Ignore
public void testEcho() throws IOException {
String src =
"""
// Echo stdin to stdout.
return sys.io.p( sys.io.stdin() );
""";
TestC.runSF(src,"echo", "",0);
// Evaluate on RISC5 emulator
EvalRisc5 R5 = TestRisc5.build( src, "echo", 0, 2, false);
int trap = R5.step(100);
assertEquals(0,trap);
assertEquals(0,R5.regs[riscv.A0]);
// Evaluate on ARM emulator
EvalArm64 arm = TestArm64.build("echo", src, 0, 2, false);
trap = arm.step(100);
assertEquals(0,trap);
assertEquals(0,arm.regs[0]);
}
}
|