diff options
| author | Cliff Click <cliffc@acm.org> | 2025-04-29 13:27:50 -0700 |
|---|---|---|
| committer | Cliff Click <cliffc@acm.org> | 2025-04-29 13:27:50 -0700 |
| commit | f505e8b554fed728c4952c06fbf2005d01540683 (patch) | |
| tree | 81ec096e775c949bfd656aa8d90d5146bf078c04 /chapter21/src/test | |
| parent | 562885524138d941c214dc66cd30fcd0da5e6269 (diff) | |
| parent | 7e1deb4cbd52c7ebbec78e2ceeb35875e83c6e3a (diff) | |
| download | Simple-f505e8b554fed728c4952c06fbf2005d01540683.tar.gz Simple-f505e8b554fed728c4952c06fbf2005d01540683.zip | |
Merge branch 'main' into ch22
Diffstat (limited to 'chapter21/src/test')
| -rw-r--r-- | chapter21/src/test/java/com/seaofnodes/simple/Chapter21Test.java | 9 | ||||
| -rw-r--r-- | chapter21/src/test/java/com/seaofnodes/simple/progs/jig.smp | 71 |
2 files changed, 68 insertions, 12 deletions
diff --git a/chapter21/src/test/java/com/seaofnodes/simple/Chapter21Test.java b/chapter21/src/test/java/com/seaofnodes/simple/Chapter21Test.java index ea23728c..c626c14e 100644 --- a/chapter21/src/test/java/com/seaofnodes/simple/Chapter21Test.java +++ b/chapter21/src/test/java/com/seaofnodes/simple/Chapter21Test.java @@ -15,9 +15,10 @@ public class Chapter21Test { @Test public void testJig() throws IOException { String src = Files.readString(Path.of("src/test/java/com/seaofnodes/simple/progs/jig.smp")); - testCPU(src,"x86_64_v2", "Win64" ,-1,null); - testCPU(src,"riscv" , "SystemV",-1,null); - testCPU(src,"arm" , "SystemV",-1,null); + CodeGen code = new CodeGen(src).driver("x86_64_v2","Win64","build/objs/jigS.o"); + //testCPU(src,"x86_64_v2", "Win64" ,-1,null); + //testCPU(src,"riscv" , "SystemV",-1,null); + //testCPU(src,"arm" , "SystemV",-1,null); } static void testCPU( String src, String cpu, String os, int spills, String stop ) { @@ -64,7 +65,7 @@ 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,"x86_64_v2", "SystemV", 9,null); testCPU(src,"riscv" , "SystemV", 5,null); testCPU(src,"arm" , "SystemV", 6,null); } diff --git a/chapter21/src/test/java/com/seaofnodes/simple/progs/jig.smp b/chapter21/src/test/java/com/seaofnodes/simple/progs/jig.smp index 00c2a161..b82b3f42 100644 --- a/chapter21/src/test/java/com/seaofnodes/simple/progs/jig.smp +++ b/chapter21/src/test/java/com/seaofnodes/simple/progs/jig.smp @@ -1,9 +1,64 @@ // -*- mode: java; -*- -struct s0 { - i16 v1; - u8 v2; -}; -s0 !l4Hi9BCnsd = new s0; -if( new s0.v2+arg ) - while(1) l4Hi9BCnsd=new s0; -while(new s0.v2+new s0.v1<=0) {} +// based on the top-down version from https://en.wikipedia.org/wiki/Merge_sort + +val merge_sort = { int[] a, int[] b, int n -> + copy_array (a, 0, n, b); + split_merge(a, 0, n, b); +}; + +val split_merge = { int[] b, int begin, int end, int[] a -> + if (end - begin <= 1) + return 0; + int middle = (end + begin) / 2; + split_merge(a, begin, middle, b); + split_merge(a, middle, end, b); + merge(b, begin, middle, end, a); + return 0; +}; + +val merge = { int[] b, int begin, int middle, int end, int[] a -> + int i = begin, j = middle; + + for (int k = begin; k < end; k++) { + // && and || + bool cond = false; + if (i < middle) { + if (j >= end) cond = true; + else if (a[i] <= a[j]) cond = true; + } + if (cond) b[k] = a[i++]; + else b[k] = a[j++]; + } +}; + +val copy_array = { int[] a, int begin, int end, int[] b -> + for (int k = begin; k < end; k++) + b[k] = a[k]; +}; + +val eq = { int[] a, int[] b, int n -> + int result = 1; + int i = 0; + while (i < n) { + if (a[i] != b[i]) { + result = 0; + break; + } + i = i + 1; + } + return result; +}; + +val main = { -> + int[] !a = new int[10]; + a[0] = 10; a[1] = 9; a[2] = 8; a[3] = 7; a[4] = 6; + a[5] = 5; a[6] = 4; a[7] = 3; a[8] = 2; a[9] = 1; + int[] !b = new int[10]; + b[0] = 0; b[1] = 0; b[2] = 0; b[3] = 0; b[4] = 0; + b[5] = 0; b[6] = 0; b[7] = 0; b[8] = 0; b[9] = 0; + int[] !expect = new int[10]; + expect[0] = 1; expect[1] = 2; expect[2] = 3; expect[3] = 4; expect[4] = 5; + expect[5] = 6; expect[6] = 7; expect[7] = 8; expect[8] = 9; expect[9] = 10; + merge_sort(a, b, 10); + return eq(a,expect,10); +}; |
