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
|
package com.seaofnodes.simple;
import com.seaofnodes.simple.codegen.CodeGen;
import com.seaofnodes.simple.codegen.ElfReader;
import com.seaofnodes.simple.codegen.ParseAll;
import com.seaofnodes.simple.type.TypeInteger;
import com.seaofnodes.simple.util.Ary;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.concurrent.TimeUnit;
import org.junit.Ignore;
import org.junit.Test;
import static org.junit.Assert.*;
public class Chapter25Test {
private static final String SYS_MODDIR = "src/main/smp";
private static final String SYS_BLDDIR = "build/objs/lib_"+TestC.CPU_ABI;
private static final String RELEASE_SYS_BLDDIR = TestC.RELEASE_SYS_DIR;
@Test
public void testPostfixFieldUpdate() {
String src = """
struct V {
u32 !len = 0;
i64[] !buf;
new V = { i64[] b -> buf=b; };
val grow = { i64 n -> self; };
val add = { i64 x -> grow(1).buf[len++] = x; self; };
};
return new V(new i64[1]).add(7).len;
""";
CodeGen code = new CodeGen(src).driver(CodeGen.Phase.TypeCheck);
assertEquals("1",Eval2.eval(code,0));
}
@Test
public void testStringEscapes() {
String src = "u8[~] s=\"A\\n\\t\\r\\\\\\\"B\"; " +
"return s#==7 && s[0]=='A' && s[1]==10 && s[2]==9 && s[3]==13 && " +
"s[4]==92 && s[5]==34 && s[6]=='B';";
CodeGen code = new CodeGen(src).driver(CodeGen.Phase.TypeCheck);
assertEquals("1",Eval2.eval(code,0));
}
@Test @Ignore
public void testModule0() throws IOException {
String MODDIR = "src/test/java/com/seaofnodes/simple/test0";
String BLDDIR = "build/objs/test0";
String a_obj = BLDDIR+"/A.o";
String ab_obj = BLDDIR+"/A/B.o";
// Remove any prior results so the test runs from scratch.
delELFiles(new File(BLDDIR));
writeB(MODDIR,5);
// Compile MODDIR/A.smp into MODDIR/A.o
// Since A refers to B also:
// Compile MODDIR/A/B.smp into MODDIR/A/B.o
CodeGen code1 = new CodeGen(MODDIR, BLDDIR,null,
"A",null,123L,TypeInteger.BOT);
code1.driver(CodeGen.Phase.Export,TestC.CPU_PORT,TestC.CALL_CONVENTION);
// Verify produces A.o, A/B.o
File a_file = new File( a_obj);
File ab_file = new File(ab_obj);
assertTrue( a_file.exists() );
assertTrue( ab_file.exists() );
long a_msec1 = a_file.lastModified();
long ab_msec1 = ab_file.lastModified();
// Link and execute: arg is true, so compute "5+1" as the exit code
String rez1 = TestC.gcc("A", 1.2, a_obj, ab_obj);
assertEquals("exec exit code: 6",rez1);
// Compile again A, expecting both A.o and A/B.o to be up-to-date and not compiled
CodeGen code2 = new CodeGen(MODDIR, BLDDIR, null,
"A",null,123L,TypeInteger.BOT);
code2.driver(CodeGen.Phase.Export,TestC.CPU_PORT,TestC.CALL_CONVENTION);
assertTrue( a_file.exists() );
assertTrue( ab_file.exists() );
long a_msec2 = a_file.lastModified();
long ab_msec2 = ab_file.lastModified();
assertEquals( a_msec1, a_msec2 );
assertEquals( ab_msec1, ab_msec2 );
// Touch A.smp and recompile. A/B.o should not recompile.
new File(MODDIR+"/A.smp").setLastModified(System.currentTimeMillis());
CodeGen code3 = new CodeGen(MODDIR, BLDDIR,null,
"A",null,123L,TypeInteger.BOT);
code3.driver(CodeGen.Phase.Export,TestC.CPU_PORT,TestC.CALL_CONVENTION);
long a_msec3 = a_file.lastModified();
long ab_msec3 = ab_file.lastModified();
assertTrue ( a_msec1 < a_msec3 );
assertEquals( ab_msec1, ab_msec3 );
// Link and execute: arg is true, so compute "5+1" as the exit code
String rez3 = TestC.gcc("A", 1.2, a_obj, ab_obj);
assertEquals("exec exit code: 6",rez3);
// Modify B.smp and recompile A/B.o; it should recompile and A.o should not.
writeB(MODDIR,7);
CodeGen code4 = new CodeGen(MODDIR, BLDDIR,null,
"A/B",null,123L,TypeInteger.BOT);
code4.driver(CodeGen.Phase.Export,TestC.CPU_PORT,TestC.CALL_CONVENTION);
long a_msec4 = a_file.lastModified();
long ab_msec4 = ab_file.lastModified();
assertEquals( a_msec3, a_msec4 );
assertTrue ( ab_msec3 < ab_msec4 );
// Link and execute: uses stale A.o, so remains '6' not '7+1' == 8
String rez4 = TestC.gcc("A", 1.2, a_obj, ab_obj);
assertEquals("exec exit code: 6",rez4);
// Recompile A.o, it should recompile despite not being touched because
// it depends on A/B.o which recompiled in the prior step.
CodeGen code5 = new CodeGen(MODDIR, BLDDIR,null,
"A",null,123L,TypeInteger.BOT);
code5.driver(CodeGen.Phase.Export,TestC.CPU_PORT,TestC.CALL_CONVENTION);
long a_msec5 = a_file.lastModified();
long ab_msec5 = ab_file.lastModified();
assertTrue ( a_msec4 < a_msec5 );
assertEquals( ab_msec4, ab_msec5 );
// Link and execute: updates A.o from B.o inlining, without compiling B.o
String rez5 = TestC.gcc("A", 1.2, a_obj, ab_obj);
assertEquals("exec exit code: 8",rez5);
// Reset for next time
writeB(MODDIR,5);
}
private void writeB(String MODDIR, int x) throws IOException {
var bsmp = new FileWriter(MODDIR+"/A/B.smp");
bsmp.write("val x="+x+";\n");
bsmp.close();
}
// Recursive search (TODO: gzip, archives) and delete all .o files
private void delELFiles( File dir) {
if( dir.isDirectory() )
for( File f : dir.listFiles() )
delELFiles(f);
else if( dir.getName().endsWith(".o") )
dir.delete();
}
private File buildTestSys(boolean clean) {
if( clean )
delELFiles(new File(SYS_BLDDIR));
File sys_file = new File(SYS_BLDDIR+"/sys.o" );
if( sys_file.exists() )
return sys_file;
// Compile SYS_MODDIR/sys.smp into SYS_BLDDIR/sys.o. Chapter25
// compiler tests intentionally depend on this freshly built sys.o.
CodeGen code1 = new CodeGen(SYS_MODDIR, SYS_BLDDIR,null,
"sys",null,123L,TypeInteger.BOT);
code1.driver(CodeGen.Phase.Export,TestC.CPU_PORT,TestC.CALL_CONVENTION,false,false,0);
assertTrue( sys_file.exists() );
return sys_file;
}
@Test
public void testSys() {
delELFiles(new File(SYS_BLDDIR));
CodeGen code1 = new CodeGen(SYS_MODDIR, SYS_BLDDIR,null,
"sys",null,123L,TypeInteger.BOT);
code1.driver(CodeGen.Phase.Export,TestC.CPU_PORT,TestC.CALL_CONVENTION,false,false,0);
// Verify produces sys.o, sys/io.o
File sys_file = new File(SYS_BLDDIR+"/sys.o" );
assertTrue( sys_file.exists() );
// Can read the ELF files
ElfReader sys_elf = ElfReader.load( sys_file, null);
sys_elf.loadSimple(code1);
// Elf files are sane
// Sys depends on io, libc, aryi64, aryu8
assertEquals(6,sys_elf._deps.length);
assertSame("sys/aryu8" ,sys_elf._deps[0]);
assertSame("sys/io" ,sys_elf._deps[1]);
assertSame("sys/aryi64",sys_elf._deps[2]);
assertSame("sys/Ary" ,sys_elf._deps[3]);
assertSame("sys/Scan" ,sys_elf._deps[4]);
assertSame("sys/libc" ,sys_elf._deps[5]);
assertSame("class:sys" ,sys_elf._clz._name);
}
@Test
public void testHelloWorld() throws IOException {
File sys_file = buildTestSys(false);
assertTrue("Missing "+sys_file+"; testHelloWorld depends on testSys building it", sys_file.exists());
String expected = "Hello, World!\n";
String prog = "return sys.io.p(\""+expected+"\") - "+expected.length()+";";
TestC.run(prog,"helloWorld",new Ary<>(new String[]{SYS_BLDDIR}),
TestC.CALL_CONVENTION, null, null, expected,0);
}
@Test
public void testHelloWorldDriver() throws Exception {
File sys_file = new File(RELEASE_SYS_BLDDIR+"/sys.o");
assertTrue("Missing "+sys_file+"; run make release or make tests_sys first", sys_file.exists());
Simple.main(new String[]{"-L",RELEASE_SYS_BLDDIR,"--norun","docs/examples/A_helloWorld.smp"});
}
@Test
public void testHelloWorldDriverLibFile() throws Exception {
File sys_file = new File(RELEASE_SYS_BLDDIR+"/sys.o");
assertTrue("Missing "+sys_file+"; run make release or make tests_sys first", sys_file.exists());
Simple.main(new String[]{"-L",sys_file.toString(),"docs/examples/A_helloWorld.smp"});
}
@Test
public void testHelloWorldNoInline() throws Exception {
File sys_file = buildTestSys(false);
assertTrue("Missing "+sys_file+"; testHelloWorldNoInline depends on testSys building it", sys_file.exists());
String base = "helloWorldNoInline";
String expected = "Hello, World!\n";
String prog = "return sys.io.p_noInline(\""+expected+"\") - "+expected.length()+";";
CodeGen code = new CodeGen(null,"build/objs",new Ary<>(new String[]{SYS_BLDDIR}),
base,prog,123L,TypeInteger.BOT);
code.driver(TestC.CPU_PORT,TestC.CALL_CONVENTION,false,true);
String obj = "build/objs/"+base+".o";
String exe = "build/objs/"+base+(TestC.OS.startsWith("Windows") ? ".exe" : "");
String syms = run(new String[]{"nm",obj});
assertTrue(syms, syms.contains(" U sys.io.p_noInline"));
String out = run(new String[]{"gcc",obj,sys_file.toString(),"-lm","-g","-o",exe});
assertEquals("",out);
String rez = run(new String[]{exe});
assertEquals(expected,rez);
}
private static String run(String[] cmd) throws Exception {
Process p = new ProcessBuilder(cmd).redirectErrorStream(true).start();
boolean normal = p.waitFor(5, TimeUnit.SECONDS);
String out = new String(p.getInputStream().readAllBytes());
assertTrue(out, normal);
assertEquals(out,0,p.exitValue());
return out;
}
@Test
public void testRedirectedRead() throws IOException {
File sys_file = buildTestSys(false);
assertTrue("Missing "+sys_file+"; testRedirectedRead depends on testSys building it", sys_file.exists());
String src = """
u8[] buf = new u8[10];
i64 ptr = buf;
int rez = sys.libc.read(0,ptr,buf#);
return rez < buf# ? 0 : sys.libc._exit(-2);
""";
TestC.run(src,"redirectedRead",new Ary<>(new String[]{SYS_BLDDIR}),
TestC.CALL_CONVENTION, null, null,
"abc", "", -1);
}
@Test
public void testBubbles() throws IOException {
File sys_file = buildTestSys(false);
assertTrue("Missing "+sys_file+"; testBubbles depends on testSys building it", sys_file.exists());
String src = Files.readString( Path.of("docs/examples/BubbleSort.smp"));
TestC.run(src,"BubbleSort",new Ary<>(new String[]{SYS_BLDDIR}),
TestC.CALL_CONVENTION, null, null,
"[3, 2,-17, 999 ] ", "[-17, 2, 3, 999]", -1);
}
}
|