diff options
Diffstat (limited to 'chapter21/src/test/java/com')
5 files changed, 70 insertions, 64 deletions
diff --git a/chapter21/src/test/java/com/seaofnodes/simple/Chapter15Test.java b/chapter21/src/test/java/com/seaofnodes/simple/Chapter15Test.java index e011392d..a850b1b8 100644 --- a/chapter21/src/test/java/com/seaofnodes/simple/Chapter15Test.java +++ b/chapter21/src/test/java/com/seaofnodes/simple/Chapter15Test.java @@ -214,7 +214,8 @@ return ary[1] * 1000 + ary[3]; // 1 * 1000 + 6 @Test public void sieveOEratosthenes() throws IOException { - CodeGen code = new CodeGen(Files.readString(Path.of("src/test/java/com/seaofnodes/simple/progs/sieve.smp"))); + String src = Files.readString(Path.of("src/test/java/com/seaofnodes/simple/progs/sieve.smp")); + CodeGen code = new CodeGen(src+"return sieve(arg);"); code.parse().opto(); assertEquals("return [int];", code.print()); assertEquals("int[ 2,3,5,7,11,13,17,19]",Eval2.eval(code, 20)); diff --git a/chapter21/src/test/java/com/seaofnodes/simple/Chapter21Test.java b/chapter21/src/test/java/com/seaofnodes/simple/Chapter21Test.java index dd38215c..3f764a4c 100644 --- a/chapter21/src/test/java/com/seaofnodes/simple/Chapter21Test.java +++ b/chapter21/src/test/java/com/seaofnodes/simple/Chapter21Test.java @@ -29,12 +29,6 @@ public class Chapter21Test { assertEquals(stop, code._stop.toString()); } - private static void testAllCPUs( String src, int spills, String stop ) { - testCPU(src,"x86_64_v2", "SystemV",spills,stop); - testCPU(src,"riscv" , "SystemV",spills,stop); - testCPU(src,"arm" , "SystemV",spills,stop); - } - @Test public void testBasic1() { String src = "return arg | 2;"; @@ -44,6 +38,22 @@ public class Chapter21Test { } @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",7,"return .[];"); + testCPU(src,"riscv" , "SystemV",7,"return (add,.[],(mul,.[],1000));"); + testCPU(src,"arm" , "SystemV",5,"return (add,.[],(mul,.[],1000));"); + } + + @Test + public void testFib() throws IOException { + String src = Files.readString(Path.of("src/test/java/com/seaofnodes/simple/progs/fib.smp")); + testCPU(src,"x86_64_v2", "SystemV",24,null); + testCPU(src,"riscv" , "SystemV",16,null); + testCPU(src,"arm" , "SystemV",16,null); + } + + @Test public void testNewtonFloat() throws IOException { String src = Files.readString(Path.of("src/test/java/com/seaofnodes/simple/progs/newtonFloat.smp")) + "flt farg = arg; return test_sqrt(farg) + test_sqrt(farg+2.0);"; @@ -52,7 +62,7 @@ public class Chapter21Test { testCPU(src,"arm" , "SystemV",19,null); } - @Test public void testNewtonExport() throws Exception { + @Test public void testNewtonExport() throws IOException { String result = """ 0 0.000000 1 1.000000 @@ -76,36 +86,11 @@ public class Chapter21Test { testCPU(src,"arm" , "SystemV", 6,null); } - @Test - public void testStringExport() throws Exception { TestC.run("stringHash"); } + @Test public void testStringExport() throws IOException { TestC.run("stringHash"); } - @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",7,"return .[];"); - testCPU(src,"riscv" , "SystemV",7,"return (add,.[],(mul,.[],1000));"); - testCPU(src,"arm" , "SystemV",5,"return (add,.[],(mul,.[],1000));"); - } - - @Test - public void testFib() throws IOException { - String src = Files.readString(Path.of("src/test/java/com/seaofnodes/simple/progs/fib.smp")); - testCPU(src,"x86_64_v2", "SystemV",24,null); - testCPU(src,"riscv" , "SystemV",16,null); - testCPU(src,"arm" , "SystemV",16,null); - } - - - // Read test case from disk. - // TODO: exportELF not handling external calls yet - @Ignore @Test - public void testPersons() throws IOException { - String src = Files.readString(Path.of("src/test/java/com/seaofnodes/simple/progs/Person.smp")); - CodeGen code = new CodeGen(src); - code.parse().opto().typeCheck().instSelect( "x86_64_v2", "SystemV").GCM().localSched().regAlloc().encode().exportELF("build/objs/Persons.o"); - //testCPU(src,"x86_64_v2", "Win64" ,3,null); - //testCPU(src,"riscv" , "SystemV",1,null); - //testCPU(src,"arm" , "SystemV",1,null); + @Test public void testSieve() throws IOException { + String primes = "[2, 3, ]"; + TestC.run("sieve",primes); } } diff --git a/chapter21/src/test/java/com/seaofnodes/simple/TestC.java b/chapter21/src/test/java/com/seaofnodes/simple/TestC.java index 121ace03..1cf1f36a 100644 --- a/chapter21/src/test/java/com/seaofnodes/simple/TestC.java +++ b/chapter21/src/test/java/com/seaofnodes/simple/TestC.java @@ -20,9 +20,9 @@ public abstract class TestC { default -> throw Utils.TODO("Map Yer CPU Port Here"); }; - public static void run( String file ) throws Exception { run(file,""); } + public static void run( String file ) throws IOException { run(file,""); } - public static void run( String file, String expected ) throws Exception { + public static void run( String file, String expected ) throws IOException { run("src/test/java/com/seaofnodes/simple/progs",file,expected); } diff --git a/chapter21/src/test/java/com/seaofnodes/simple/progs/sieve.c b/chapter21/src/test/java/com/seaofnodes/simple/progs/sieve.c new file mode 100644 index 00000000..8c28aea8 --- /dev/null +++ b/chapter21/src/test/java/com/seaofnodes/simple/progs/sieve.c @@ -0,0 +1,18 @@ +#include <stdio.h> +#include <stdbool.h> + +typedef struct AryInt { + unsigned int len; + unsigned int data[]; +} AryInt; + +extern AryInt sieve(int N); + +int main() { + AryInt ps = sieve(10); + printf("["); + for( int i=0; i<ps.len; i++ ) + printf("%d, ",ps.data[i]); + printf("]"); + return 0; +} diff --git a/chapter21/src/test/java/com/seaofnodes/simple/progs/sieve.smp b/chapter21/src/test/java/com/seaofnodes/simple/progs/sieve.smp index 0b6f56ed..c5c3824a 100644 --- a/chapter21/src/test/java/com/seaofnodes/simple/progs/sieve.smp +++ b/chapter21/src/test/java/com/seaofnodes/simple/progs/sieve.smp @@ -1,29 +1,31 @@ // -*- mode: java; -*- // The main Sieve array -bool[] !ary = new bool[arg]; -// The primes less than arg -int[] !primes = new int[arg>>1]; -// Number of primes so far, searching at index p -int nprimes = 0, p=2; -// Find primes while p^2 < arg -while( p*p < arg ) { - // skip marked non-primes - while( ary[p] ) p++; - // p is now a prime - primes[nprimes++] = p; - // Mark out the rest non-primes - for( int i = p + p; i < ary#; i+= p ) - ary[i] = true; - p++; -} - -// Now just collect the remaining primes, no more marking -for( ; p < arg; p++ ) - if( !ary[p] ) +val sieve = { int N -> + bool[] !ary = new bool[N]; + // The primes less than N + int[] !primes = new int[N>>1]; + // Number of primes so far, searching at index p + int nprimes = 0, p=2; + // Find primes while p^2 < N + while( p*p < N ) { + // skip marked non-primes + while( ary[p] ) p++; + // p is now a prime primes[nprimes++] = p; + // Mark out the rest non-primes + for( int i = p + p; i < ary#; i+= p ) + ary[i] = true; + p++; + } + + // Now just collect the remaining primes, no more marking + for( ; p < N; p++ ) + if( !ary[p] ) + primes[nprimes++] = p; -// Copy/shrink the result array -int[] !rez = new int[nprimes]; -for( int j=0; j < nprimes; j++ ) - rez[j] = primes[j]; -return rez; + // Copy/shrink the result array + int[] !rez = new int[nprimes]; + for( int j=0; j < nprimes; j++ ) + rez[j] = primes[j]; + return rez; +}; |
