diff options
| author | Cliff Click <cliffc@acm.org> | 2026-09-17 15:34:28 -0700 |
|---|---|---|
| committer | Cliff Click <cliffc@acm.org> | 2026-09-17 15:34:28 -0700 |
| commit | 42681f41318796afc58bc14ce04620b7c5c7eb18 (patch) | |
| tree | 6c8a90f441a4a0edd2b4e5f3a8293d45b9c55834 /chapter24/src/test/java | |
| parent | 5eb00d3306e6e8738b8033c5ba8401770f9d7ffd (diff) | |
| download | Simple-42681f41318796afc58bc14ce04620b7c5c7eb18.tar.gz Simple-42681f41318796afc58bc14ce04620b7c5c7eb18.zip | |
Parser fix OR temps
Diffstat (limited to 'chapter24/src/test/java')
| -rw-r--r-- | chapter24/src/test/java/com/seaofnodes/simple/Chapter23Test.java | 42 |
1 files changed, 42 insertions, 0 deletions
diff --git a/chapter24/src/test/java/com/seaofnodes/simple/Chapter23Test.java b/chapter24/src/test/java/com/seaofnodes/simple/Chapter23Test.java index 94e13a87..400f6b52 100644 --- a/chapter24/src/test/java/com/seaofnodes/simple/Chapter23Test.java +++ b/chapter24/src/test/java/com/seaofnodes/simple/Chapter23Test.java @@ -13,6 +13,48 @@ import static org.junit.Assert.*; public class Chapter23Test { @Test + public void testOrRhsLoopLocal() throws IOException { + checkOrRhsLoop(""" + struct S { int f; }; + int x = 0; + int result = (arg & 1) || (new S { + while (x < 3) x++; + f = ++x; + }).f; + return x * 10 + result; + """); + } + + @Test + public void testOrRhsLoopMemory() throws IOException { + checkOrRhsLoop(""" + struct S { int f; }; + S !s = new S; + int result = (arg & 1) || (new S { + while (s.f < 3) s.f++; + s.f++; + f = s.f; + }).f; + return s.f * 10 + result; + """); + } + + private static void checkOrRhsLoop(String src) throws IOException { + CodeGen code = new CodeGen(src).driver("riscv", "SystemV", null); + for (int arg : new int[]{0, 1, 2, 3}) { + byte[] image = new byte[1 << 20]; + System.arraycopy(code._encoding.bits(), 0, image, 0, code._encoding._bits.size()); + EvalRisc5 r5 = new EvalRisc5(image, 1 << 16); + r5.regs[riscv.A0] = arg; + assertEquals("Execution must finish", 0, r5.step(1000)); + // False LHS runs the loop and final increment, returning 4. + // True LHS skips all RHS effects and returns the LHS value, 1. + assertEquals("OR result and side effects for arg=" + arg, + (arg & 1) == 0 ? 44 : 1, r5.regs[riscv.A0]); + } + } + + @Test public void testJig() throws IOException { //String src = Files.readString(Path.of("src/test/java/com/seaofnodes/simple/progs/jig.smp")); String src = Files.readString(Path.of("docs/examples/BubbleSort.smp")); |
