From 28209703c72fb7e9955961dbdb84e24a0f871acd Mon Sep 17 00:00:00 2001 From: Cliff Click Date: Mon, 10 Aug 2026 11:11:18 -0700 Subject: Chapter 25: Modules --- .../java/com/seaofnodes/simple/Chapter22Test.java | 141 +++++++++++---------- 1 file changed, 72 insertions(+), 69 deletions(-) (limited to 'src/test/java/com/seaofnodes/simple/Chapter22Test.java') diff --git a/src/test/java/com/seaofnodes/simple/Chapter22Test.java b/src/test/java/com/seaofnodes/simple/Chapter22Test.java index 78b9e7a1..91b8310d 100644 --- a/src/test/java/com/seaofnodes/simple/Chapter22Test.java +++ b/src/test/java/com/seaofnodes/simple/Chapter22Test.java @@ -1,12 +1,10 @@ package com.seaofnodes.simple; +import com.seaofnodes.simple.util.Utils; import com.seaofnodes.simple.codegen.CodeGen; -import java.io.IOException; -import java.nio.file.Files; -import java.nio.file.Path; - 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.*; @@ -15,25 +13,25 @@ public class Chapter22Test { @Test public void testJig() throws IOException { - String src = -""" -val fib = {int n -> - int temp=0; - int f1=1; - int f2=1; - int i=n; - while( i>1 ){ - temp = f1+f2; - f1=f2; - f2=temp; - i=i-1; - } - return f2; + String src = """ +struct s0 { + bool v1; + i16 v2; + int v3; + i8 v4; + byte v5; }; - -fib(10); +while(new s0.v3) + while(new s0.v5<>3; if( delta==0 ) delta = 1; - if( spills != -1 ) + if( spills != -1 && !CodeGen.iterSeedOverridden() ) assertEquals("Expect spills:",spills,code._regAlloc._spillScaled,delta); if( stop != null ) - assertEquals(stop, code._stop.toString()); + assertEquals(stop, code.print()); return code; } - static int testCPUSize( String src, String cpu, String os, int spills, String stop ) { - return testCPU(src,cpu,os,spills,stop)._encoding._bits.size(); + 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; +struct _Person { i32 age;}; +_Person !p = new _Person; p.age = (arg<<17)>>17; return 0; """; - assertEquals(46, testCPUSize(src, "x86_64_v2","Win64",2,"return 0;")); - assertEquals(60, testCPUSize(src, "riscv","SystemV",4,"return 0;")); - assertEquals(56, testCPUSize(src, "arm","SystemV",4,"return 0;")); + assertEquals(83, 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("sext_str_not_fold_away", src,0, 4, false); + EvalRisc5 R5 = TestRisc5.build( src, "sext_str_not_fold_away", 0, 4, false); int trap = R5.step(100); assertEquals(0,trap); @@ -80,14 +78,13 @@ return 0; // Should not fold away @Test public void testSextFail2() throws IOException { String src = """ - - struct Person { i32 age;}; - Person !p = new Person; + struct _Person { i32 age;}; + _Person !p = new _Person; p.age = (arg<<48)>>48; return 0; """; - EvalRisc5 R5 = TestRisc5.build("sext_str_not_fold_away_2", src, 0, 4, false); + EvalRisc5 R5 = TestRisc5.build( src, "sext_str_not_fold_away_2", 0, 4, false); int trap = R5.step(100); assertEquals(0,trap); @@ -95,9 +92,9 @@ return 0; int trap_arm = A5.step(100); assertEquals(0,trap_arm); - assertEquals(46, testCPUSize(src, "x86_64_v2","Win64",2,"return 0;")); - assertEquals(60, testCPUSize(src, "riscv","SystemV",4,"return 0;")); - assertEquals(56, testCPUSize(src, "arm","SystemV",4,"return 0;")); + assertEquals(83, testCPUSize(src, "x86_64_v2","win64",3 )); + assertEquals(96, testCPUSize(src, "riscv", "SystemV",4 )); + assertEquals(96, testCPUSize(src, "arm", "SystemV",4 )); } @@ -105,13 +102,13 @@ return 0; @Test public void testSextSuccess() throws IOException { String src = """ // Should fold away sign extend -struct Person { i8 age;}; -Person !p = new Person; +struct _Person { i8 age;}; +_Person !p = new _Person; p.age = (arg<<48)>>48; return 0; - """; +"""; - EvalRisc5 R5 = TestRisc5.build("sext_str_fold_away", src, 0, 5, false); + EvalRisc5 R5 = TestRisc5.build( src, "sext_str_fold_away", 0, 5, false); int trap = R5.step(100); assertEquals(0,trap); @@ -119,9 +116,9 @@ return 0; int trap_arm = A5.step(100); assertEquals(0,trap_arm); - assertEquals(41, testCPUSize(src, "x86_64_v2","Win64",2,"return 0;")); - assertEquals(56, testCPUSize(src, "riscv","SystemV",5,"return 0;")); - assertEquals(52, testCPUSize(src, "arm","SystemV",5,"return 0;")); + assertEquals(71, testCPUSize(src, "x86_64_v2","win64",3 )); + assertEquals(92, testCPUSize(src, "riscv", "SystemV",5 )); + assertEquals(92, testCPUSize(src, "arm", "SystemV",5 )); // do assertEquals here } @@ -130,17 +127,17 @@ return 0; @Test public void testPerson() throws IOException { String src = """ -struct Person { +struct Person { // Exports a field typed singleton+final *class:Person{age:i32} i32 age; }; -val fcn = { Person?[] ps, int x -> +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, "person", null, person, 0); + 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 @@ -151,7 +148,7 @@ val fcn = { Person?[] ps, int x -> int p1 = ps+4*8+1*8; // P2 = { age } // sizeof=8 int p2 = ps+4*8+2*8; - EvalRisc5 R5 = TestRisc5.build("person", src, ps, 0, false); + EvalRisc5 R5 = TestRisc5.build( src, "fcn", ps, 0, false); R5.regs[riscv.A1] = 1; // Index 1 R5.st8(ps,3); // Length R5.st8(ps+1*8,p0); @@ -167,7 +164,7 @@ val fcn = { Person?[] ps, int x -> assertEquals(17+1,R5.ld8(p1)); assertEquals(60+0,R5.ld8(p2)); - EvalArm64 A5 = TestArm64.build("person", src, ps, 0, false); + EvalArm64 A5 = TestArm64.build("fcn", src, ps, 0, false); A5.regs[arm.X1] = 1; // Index 1 A5.st8(ps, 3); A5.st8(ps+1*8,p0); @@ -187,41 +184,47 @@ val fcn = { Person?[] ps, int x -> @Test public void testCoRecur() { String src = """ -struct A { B? b; C? c; i64 ax; val az = x*2; }; -struct B { A? a; C? c; f32 bx; val bz = x*3; }; -struct C { A? a; B? b; f64 cx; val cz = x*x; }; -A !aa = new A{ ax=17; }; -B !bb = new B{ bx=3.14; a = aa; }; -C !cc = new C{ cx=2.73; a = aa; b = bb; }; +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; -val x = 5; // aa.az; // Error to self-define forward ref return cc.cz; """; CodeGen code = new CodeGen(src).parse().opto().typeCheck(); - assertEquals("return 25;", code._stop.toString()); + assertEquals("return 25;", code.print()); assertEquals("25", Eval2.eval(code, 0)); } @Test public void testHelloWorld() throws IOException { - String src = -""" -sys.io.p("Hello, World!"); + 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.run(src,TestC.CALL_CONVENTION,null, null,null,"build/objs/helloWorld","","Hello, World!",0); + TestC.runSF(src,"helloWorld", "Hello, World!",2); // Evaluate on RISC5 emulator - EvalRisc5 R5 = TestRisc5.build("helloWorld", src, 0, 2, false); + 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, 2, false); + EvalArm64 arm = TestArm64.build("helloWorld", src,0, 5, false); trap = arm.step(100); assertEquals(0,trap); assertEquals(0,arm.regs[0]); @@ -235,16 +238,16 @@ int N=4; i32[] !is = new i32[N]; for( int i=0; i // final array +val _sum = { i32[~] is -> // final array int sum=0; for( int i=0; i