summaryrefslogtreecommitdiff
path: root/chapter22/src/test/java
diff options
context:
space:
mode:
Diffstat (limited to 'chapter22/src/test/java')
-rw-r--r--chapter22/src/test/java/com/seaofnodes/simple/Chapter20Test.java10
-rw-r--r--chapter22/src/test/java/com/seaofnodes/simple/Chapter21Test.java75
-rw-r--r--chapter22/src/test/java/com/seaofnodes/simple/Chapter22Test.java52
-rw-r--r--chapter22/src/test/java/com/seaofnodes/simple/TestC.java19
4 files changed, 106 insertions, 50 deletions
diff --git a/chapter22/src/test/java/com/seaofnodes/simple/Chapter20Test.java b/chapter22/src/test/java/com/seaofnodes/simple/Chapter20Test.java
index 5b33d1e7..baee39fe 100644
--- a/chapter22/src/test/java/com/seaofnodes/simple/Chapter20Test.java
+++ b/chapter22/src/test/java/com/seaofnodes/simple/Chapter20Test.java
@@ -63,7 +63,9 @@ val sqrt = { int x ->
guess = next;
}
};
-return sqrt(arg) + sqrt(arg+2);
+int truncate_me = (arg+2);
+int truncate = arg;
+return sqrt(truncate) + sqrt(truncate_me);
""";
testCPU(src,"x86_64_v2", "Win64" ,23,null);
testCPU(src,"riscv" , "SystemV",19,null);
@@ -92,7 +94,7 @@ return sqrt(arg) + sqrt(arg+2);
@Test
public void testArray1() throws IOException {
String src = Files.readString(Path.of("src/test/java/com/seaofnodes/simple/progs/array1.smp"));
- testCPU(src,"x86_64_v2", "SystemV",-1,"return .[];");
+ testCPU(src,"x86_64_v2", "SystemV",-1,"return (add,.[],(muli,.[]));");
testCPU(src,"riscv" , "SystemV", 8,"return (add,.[],(mul,.[],1000));");
testCPU(src,"arm" , "SystemV", 5,"return (add,.[],(mul,.[],1000));");
}
@@ -101,8 +103,8 @@ return sqrt(arg) + sqrt(arg+2);
@Test
public void testString() throws IOException {
String src = Files.readString(Path.of("src/test/java/com/seaofnodes/simple/progs/stringHash.smp"));
- testCPU(src,"x86_64_v2", "SystemV", 9,null);
- testCPU(src,"riscv" , "SystemV", 5,null);
+ testCPU(src,"x86_64_v2", "SystemV", 25,null);
+ testCPU(src,"riscv" , "SystemV", 8,null);
testCPU(src,"arm" , "SystemV", 6,null);
}
diff --git a/chapter22/src/test/java/com/seaofnodes/simple/Chapter21Test.java b/chapter22/src/test/java/com/seaofnodes/simple/Chapter21Test.java
index 0e8c05fe..8525ef76 100644
--- a/chapter22/src/test/java/com/seaofnodes/simple/Chapter21Test.java
+++ b/chapter22/src/test/java/com/seaofnodes/simple/Chapter21Test.java
@@ -40,15 +40,15 @@ public class Chapter21Test {
@Test public void testInfinite() {
String src = "struct S { int i; }; S !s = new S; while(1) s.i++;";
- testCPU(src,"x86_64_v2", "SystemV",0,"Stop[ ]");
- testCPU(src,"riscv" , "SystemV",0,"Stop[ ]");
- testCPU(src,"arm" , "SystemV",0,"Stop[ ]");
+ testCPU(src,"x86_64_v2", "SystemV",16,"return Top;");
+ testCPU(src,"riscv" , "SystemV",2,"return Top;");
+ testCPU(src,"arm" , "SystemV",2,"return Top;");
}
@Test
public void testArray1() throws IOException {
String src = Files.readString(Path.of("src/test/java/com/seaofnodes/simple/progs/array1.smp"));
- testCPU(src,"x86_64_v2", "SystemV",-1,"return .[];");
+ testCPU(src,"x86_64_v2", "SystemV",-1,"return (add,.[],(muli,.[]));");
testCPU(src,"riscv" , "SystemV", 7,"return (add,.[],(mul,.[],1000));");
testCPU(src,"arm" , "SystemV", 5,"return (add,.[],(mul,.[],1000));");
}
@@ -64,20 +64,20 @@ public class Chapter21Test {
@Test
public void testString() throws IOException {
String src = Files.readString(Path.of("src/test/java/com/seaofnodes/simple/progs/stringHash.smp"));
- testCPU(src,"x86_64_v2", "SystemV", 9,null);
- testCPU(src,"riscv" , "SystemV", 5,null);
+ testCPU(src,"x86_64_v2", "SystemV", 25,null);
+ testCPU(src,"riscv" , "SystemV", 8,null);
testCPU(src,"arm" , "SystemV", 6,null);
}
@Test public void testStringExport() throws IOException {
- TestC.run("stringHash", 9);
+ TestC.run("stringHash", 25);
}
@Test public void testLoop2() throws IOException {
String src = Files.readString(Path.of("src/test/java/com/seaofnodes/simple/progs/loop2.smp"));
- testCPU(src,"x86_64_v2", "Win64" ,0,"return (inc,Phi(Loop,0,inc));");
- testCPU(src,"riscv" , "SystemV",0,"return ( Phi(Loop,0,addi) + #1 );");
- testCPU(src,"arm" , "SystemV",0,"return (inc,Phi(Loop,0,inc));");
+ testCPU(src,"x86_64_v2", "Win64" ,0,"return (sari,(shli,(inc,Phi(Loop,0,sari))));");
+ testCPU(src,"riscv" , "SystemV",0,"return ( ( ( Phi(Loop,0,srai) + #1 ) << #32 ) >> #32 );");
+ testCPU(src,"arm" , "SystemV",0,"return (asri,(lsli,(inc,Phi(Loop,0,asri))));");
}
@Test public void testNewtonExport() throws IOException {
@@ -120,7 +120,7 @@ public class Chapter21Test {
String sprimes = sb.p("]").toString();
// Compile, link against native C; expect the above string of primes to be printed out by C
- TestC.run("sieve",sprimes, 257);
+ TestC.run("sieve",sprimes, 473);
// Evaluate on RISC5 emulator; expect return of an array of primes in
// the simulated heap.
@@ -150,14 +150,14 @@ public class Chapter21Test {
String fib = "[1, 1, 2, 3, 5, 8, 13, 21, 34, 55]";
TestC.run("fib", fib, 24);
- EvalRisc5 R5 = TestRisc5.build("fib", 9, 16, false);
+ EvalRisc5 R5 = TestRisc5.build("fib", 9, 17, false);
int trap = R5.step(100);
assertEquals(0,trap);
// Return register A0 holds fib(8)==55
assertEquals(55,R5.regs[riscv.A0]);
// arm
- EvalArm64 A5 = TestArm64.build("fib", 9, 16, false);
+ EvalArm64 A5 = TestArm64.build("fib", 9, 17, false);
int trap_arm = A5.step(100);
assertEquals(0,trap_arm);
// Return register X0 holds fib(8)==55
@@ -166,48 +166,49 @@ public class Chapter21Test {
@Test public void testPerson() throws IOException {
String person = "6\n";
+ // Todo: Need to fix it(switch to 4 byte pointers)
TestC.run("person", person, 0);
// 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;
+ // P0 = { age } // sizeof=4
+ int p0 = ps+4*4+0*4;
+ // P1 = { age } // sizeof=4
+ int p1 = ps+4*4+1*4;
+ // P2 = { age } // sizeof=4
+ int p2 = ps+4*4+2*4;
EvalRisc5 R5 = TestRisc5.build("person", ps, 0, false);
R5.regs[riscv.A1] = 1; // Index 1
R5.st4(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
+ R5.st4(ps+1*4,p0);
+ R5.st4(ps+2*4,p1);
+ R5.st4(ps+3*4,p2);
+ R5.st4(p0, 5); // age= 5
+ R5.st4(p1,17); // age=17
+ R5.st4(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));
+ assertEquals( 5+0,R5.ld4s(p0));
+ assertEquals(17+1,R5.ld4s(p1));
+ assertEquals(60+0,R5.ld4s(p2));
EvalArm64 A5 = TestArm64.build("person", ps, 0, false);
A5.regs[arm.X1] = 1; // Index 1
A5.st4(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
+ A5.st4(ps+1*4,p0);
+ A5.st4(ps+2*4,p1);
+ A5.st4(ps+3*4,p2);
+ A5.st4(p0, 5); // age= 5
+ A5.st4(p1,17); // age=17
+ A5.st4(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));
+ assertEquals( 5+0, A5.ld4s(p0));
+ assertEquals(17+1, A5.ld4s(p1));
+ assertEquals(60+0, A5.ld4s(p2));
}
@Test public void testArgCount() throws IOException {
diff --git a/chapter22/src/test/java/com/seaofnodes/simple/Chapter22Test.java b/chapter22/src/test/java/com/seaofnodes/simple/Chapter22Test.java
index 3bf379cd..4932ba3f 100644
--- a/chapter22/src/test/java/com/seaofnodes/simple/Chapter22Test.java
+++ b/chapter22/src/test/java/com/seaofnodes/simple/Chapter22Test.java
@@ -4,6 +4,9 @@ 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 org.junit.Ignore;
import org.junit.Test;
import static org.junit.Assert.*;
@@ -28,6 +31,55 @@ public class Chapter22Test {
assertEquals(stop, code._stop.toString());
}
+
+ // Int now is changed to 4 bytes.
+ @Test public void testPerson() throws IOException {
+ String person = "6\n";
+ // Todo: need to fix it and switch to 4 byte pointers
+ TestC.run("person", person, 0);
+
+ // 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=4
+ int p0 = ps+4*4+0*4;
+ // P1 = { age } // sizeof=4
+ int p1 = ps+4*4+1*4;
+ // P2 = { age } // sizeof=4
+ int p2 = ps+4*4+2*4;
+ EvalRisc5 R5 = TestRisc5.build("person", ps, 0, false);
+ R5.regs[riscv.A1] = 1; // Index 1
+ R5.st4(ps,3); // Length
+ R5.st4(ps+1*4,p0);
+ R5.st4(ps+2*4,p1);
+ R5.st4(ps+3*4,p2);
+ R5.st4(p0, 5); // age= 5
+ R5.st4(p1,17); // age=17
+ R5.st4(p2,60); // age=60
+
+ int trap = R5.step(100);
+ assertEquals(0,trap);
+ assertEquals( 5+0,R5.ld4s(p0));
+ assertEquals(17+1,R5.ld4s(p1));
+ assertEquals(60+0,R5.ld4s(p2));
+
+ EvalArm64 A5 = TestArm64.build("person", ps, 0, false);
+ A5.regs[arm.X1] = 1; // Index 1
+ A5.st4(ps, 3);
+ A5.st4(ps+1*4,p0);
+ A5.st4(ps+2*4,p1);
+ A5.st4(ps+3*4,p2);
+ A5.st4(p0, 5); // age= 5
+ A5.st4(p1,17); // age=17
+ A5.st4(p2,60); // age=60
+
+ int trap_arm = A5.step(100);
+ assertEquals(0,trap_arm);
+ assertEquals( 5+0, A5.ld4s(p0));
+ assertEquals(17+1, A5.ld4s(p1));
+ assertEquals(60+0, A5.ld4s(p2));
+ }
+
@Test
public void testCoRecur() {
String src = """
diff --git a/chapter22/src/test/java/com/seaofnodes/simple/TestC.java b/chapter22/src/test/java/com/seaofnodes/simple/TestC.java
index 41e646bf..761da0ba 100644
--- a/chapter22/src/test/java/com/seaofnodes/simple/TestC.java
+++ b/chapter22/src/test/java/com/seaofnodes/simple/TestC.java
@@ -16,8 +16,8 @@ public abstract class TestC {
public static final String CALL_CONVENTION = OS.startsWith("Windows") ? "Win64" : "SystemV";
public static final String CPU_PORT = switch( CPU ) {
- case "amd64" -> "x86_64_v2";
- default -> throw Utils.TODO("Map Yer CPU Port Here");
+ case "amd64" -> "x86_64_v2";
+ default -> throw Utils.TODO("Map Yer CPU Port Here");
};
public static void run( String file, int spills ) throws IOException { run(file,"",spills); }
@@ -49,12 +49,13 @@ public abstract class TestC {
params.add("gcc");
if( cfile!=null ) params.add(cfile); // Associated C driver, usually has a `main`
params.addAll(new String[] {
- obj,
- "-lm", // Picks up 'sqrt' for newtonFloat tests to compare
- "-g",
- "-o",
- bin,
- });
+ obj,
+ "-lm", // Picks up 'sqrt' for newtonFloat tests to compare
+ "-m32",
+ "-g",
+ "-o",
+ bin,
+ });
// Calling convention for C calls, if any
if( cfile!=null ) {
params.add("-D");
@@ -90,4 +91,4 @@ public abstract class TestC {
assertEquals("Expect spills:",spills,code._regAlloc._spillScaled,delta);
}
-}
+} \ No newline at end of file