summaryrefslogtreecommitdiff
path: root/chapter21/src/test/java/com/seaofnodes
diff options
context:
space:
mode:
authorCliff Click <cliffc@acm.org>2026-09-28 15:42:57 -0700
committerCliff Click <cliffc@acm.org>2026-09-28 15:42:57 -0700
commit75390f477ac7d4568ac02f97358d146ff9879d6e (patch)
treea6dc2226357ecb03359414b51f3de87034892aca /chapter21/src/test/java/com/seaofnodes
parent586c8f65f2d0c0e1f73aadd0aa8d850aa653bddd (diff)
downloadSimple-75390f477ac7d4568ac02f97358d146ff9879d6e.tar.gz
Simple-75390f477ac7d4568ac02f97358d146ff9879d6e.zip
Backport FunPtr
Helps keep functions alive via ptr only (no calls). A few RISC 5 TFP encoding fixes.
Diffstat (limited to 'chapter21/src/test/java/com/seaofnodes')
-rw-r--r--chapter21/src/test/java/com/seaofnodes/simple/Chapter21Test.java21
1 files changed, 21 insertions, 0 deletions
diff --git a/chapter21/src/test/java/com/seaofnodes/simple/Chapter21Test.java b/chapter21/src/test/java/com/seaofnodes/simple/Chapter21Test.java
index 0cabcfbc..72041bd7 100644
--- a/chapter21/src/test/java/com/seaofnodes/simple/Chapter21Test.java
+++ b/chapter21/src/test/java/com/seaofnodes/simple/Chapter21Test.java
@@ -11,6 +11,27 @@ import org.junit.Test;
import static org.junit.Assert.*;
public class Chapter21Test {
+ @Test public void testReturnedFunctionEncoding() throws IOException {
+ String src = "if(arg) return {->42;}; return {->43;};";
+ CodeGen code = new CodeGen(src).driver("riscv","SystemV",null);
+ for( int arg : new int[]{0,1} ) {
+ byte[] image = new byte[1<<16];
+ byte[] bits = code._encoding.bits();
+ System.arraycopy(bits,0,image,0,bits.length);
+ EvalRisc5 cpu = new EvalRisc5(image,1<<15);
+ cpu._pc = code._encoding._opStart[code.link(code._main)._nid];
+ cpu.regs[riscv.A0] = arg;
+ assertEquals(0,cpu.step(100));
+ assertEquals(0,cpu._pc);
+ cpu._pc = (int)cpu.regs[riscv.A0];
+ assertEquals(0,cpu.step(100));
+ assertEquals(0,cpu._pc);
+ assertEquals(arg==0 ? 43 : 42,cpu.regs[riscv.A0]);
+ }
+ // Anonymous bodies need code, but no exported ELF name.
+ new CodeGen(src).driver(TestC.CPU_PORT,TestC.CALL_CONVENTION,"build/objs/anonymous.o");
+ }
+
@Test public void testArmFloatMemory() throws IOException {
for( String type : new String[]{"f32","f64"} ) {
String src = type+"[] !a=new "+type+"[3]; a[arg]=1.25; return a[1];";