summaryrefslogtreecommitdiff
path: root/chapter22
diff options
context:
space:
mode:
authorCliff Click <cliffc@acm.org>2025-05-03 15:55:38 -0700
committerCliff Click <cliffc@acm.org>2025-05-03 15:55:38 -0700
commit2bedee089e65fa4d6a843e7497fc8bc5dc4be992 (patch)
tree426fd5e1db359f41e3497562f0c8708764c9f6ca /chapter22
parent1c9d3f51994d8bd8fadadbde2d1992018909081d (diff)
downloadSimple-2bedee089e65fa4d6a843e7497fc8bc5dc4be992.tar.gz
Simple-2bedee089e65fa4d6a843e7497fc8bc5dc4be992.zip
back to i64 default
Sorting out 4b pointers first. Back to i64 ints. Everything compiles except the Person test which explicitly deals with pointer sizes.
Diffstat (limited to 'chapter22')
-rw-r--r--chapter22/src/main/java/com/seaofnodes/simple/Parser.java6
-rw-r--r--chapter22/src/main/java/com/seaofnodes/simple/node/cpus/riscv/ImmRISC.java2
-rw-r--r--chapter22/src/test/java/com/seaofnodes/simple/Chapter09Test.java6
-rw-r--r--chapter22/src/test/java/com/seaofnodes/simple/Chapter11Test.java4
-rw-r--r--chapter22/src/test/java/com/seaofnodes/simple/Chapter13Test.java2
-rw-r--r--chapter22/src/test/java/com/seaofnodes/simple/Chapter20Test.java6
-rw-r--r--chapter22/src/test/java/com/seaofnodes/simple/Chapter21Test.java21
-rw-r--r--chapter22/src/test/java/com/seaofnodes/simple/TestC.java35
8 files changed, 44 insertions, 38 deletions
diff --git a/chapter22/src/main/java/com/seaofnodes/simple/Parser.java b/chapter22/src/main/java/com/seaofnodes/simple/Parser.java
index d551a72a..9d313046 100644
--- a/chapter22/src/main/java/com/seaofnodes/simple/Parser.java
+++ b/chapter22/src/main/java/com/seaofnodes/simple/Parser.java
@@ -115,7 +115,7 @@ public class Parser {
put("i32" ,TypeInteger.I32);
put("i64" ,TypeInteger.BOT);
put("i8" ,TypeInteger.I8 );
- put("int" ,TypeInteger.I32);
+ put("int" ,TypeInteger.BOT);
put("u1" ,TypeInteger.U1 );
put("u16" ,TypeInteger.U16);
put("u32" ,TypeInteger.U32);
@@ -135,7 +135,7 @@ public class Parser {
_scope.define(ScopeNode.CTRL, Type.CONTROL , false, null, _lexer);
_scope.define(ScopeNode.MEM0, TypeMem.BOT , false, null, _lexer);
- _scope.define(ScopeNode.ARG0, TypeInteger.I32, false, null, _lexer);
+ _scope.define(ScopeNode.ARG0, TypeInteger.BOT, false, null, _lexer);
ctrl(XCTRL);
_scope.mem(new MemMergeNode(false));
@@ -159,7 +159,7 @@ public class Parser {
// default main).
FunNode main = _code.link(_code._main);
StopNode stop = _code._stop;
- if( main.ret().expr()._type==Type.TOP ) {
+ if( main.ret().expr()._type==Type.TOP && main.uctrl()==null ) {
// Kill an empty default main; so it does not attempt to put a
// "main" in any final ELF file
main.setDef(1,XCTRL); // Delete default start input
diff --git a/chapter22/src/main/java/com/seaofnodes/simple/node/cpus/riscv/ImmRISC.java b/chapter22/src/main/java/com/seaofnodes/simple/node/cpus/riscv/ImmRISC.java
index 589bf550..9e7f456a 100644
--- a/chapter22/src/main/java/com/seaofnodes/simple/node/cpus/riscv/ImmRISC.java
+++ b/chapter22/src/main/java/com/seaofnodes/simple/node/cpus/riscv/ImmRISC.java
@@ -40,7 +40,7 @@ abstract public class ImmRISC extends MachConcreteNode implements MachNode {
@Override public void encoding( Encoding enc ) {
short dst = enc.reg(this );
short src = enc.reg(in(1));
- enc.add4(riscv.i_type(opcode(), dst, func3(), src, _imm12));
+ enc.add4(riscv.i_type(opcode(), dst, func3(), src, _imm12 & 0xFFF));
}
// General form: "addi rd = rs1 + imm"
diff --git a/chapter22/src/test/java/com/seaofnodes/simple/Chapter09Test.java b/chapter22/src/test/java/com/seaofnodes/simple/Chapter09Test.java
index a4aade16..d4d02aa9 100644
--- a/chapter22/src/test/java/com/seaofnodes/simple/Chapter09Test.java
+++ b/chapter22/src/test/java/com/seaofnodes/simple/Chapter09Test.java
@@ -128,7 +128,7 @@ while(v1+arg) {
}
""");
code.parse().opto();
- assertEquals("Stop[ ]", code.print());
+ assertEquals("return Top;", code.print());
}
@Test
@@ -222,7 +222,7 @@ while(1) {
}
""");
code.parse().opto();
- assertEquals("Stop[ ]", code.print());
+ assertEquals("return Top;", code.print());
}
@Test
@@ -272,7 +272,7 @@ while(1)
return v1+v0;
""");
code.parse().opto();
- assertEquals("Stop[ ]", code.print());
+ assertEquals("return Top;", code.print());
}
@Test
diff --git a/chapter22/src/test/java/com/seaofnodes/simple/Chapter11Test.java b/chapter22/src/test/java/com/seaofnodes/simple/Chapter11Test.java
index 1fb9f75a..f6539ac4 100644
--- a/chapter22/src/test/java/com/seaofnodes/simple/Chapter11Test.java
+++ b/chapter22/src/test/java/com/seaofnodes/simple/Chapter11Test.java
@@ -435,7 +435,7 @@ while(1)
arg = arg+ptr.v;
""");
code.parse().opto();
- assertEquals("Stop[ ]", code.print());
+ assertEquals("return Top;", code.print());
}
@Test
@@ -527,7 +527,7 @@ while(0>=0) {
return 0;
""");
code.parse().opto();
- assertEquals("Stop[ ]", code.print());
+ assertEquals("return Top;", code.print());
}
@Test
diff --git a/chapter22/src/test/java/com/seaofnodes/simple/Chapter13Test.java b/chapter22/src/test/java/com/seaofnodes/simple/Chapter13Test.java
index a4644917..4f5c30c1 100644
--- a/chapter22/src/test/java/com/seaofnodes/simple/Chapter13Test.java
+++ b/chapter22/src/test/java/com/seaofnodes/simple/Chapter13Test.java
@@ -33,7 +33,7 @@ while( arg ) {
return head.next.i;
""");
try { code.parse().opto().typeCheck(); fail(); }
- catch( Exception e ) { assertEquals("Might be null accessing 'next'",e.getMessage()); }
+ catch( Exception e ) { assertEquals("Might be null accessing 'i'",e.getMessage()); }
}
@Test
diff --git a/chapter22/src/test/java/com/seaofnodes/simple/Chapter20Test.java b/chapter22/src/test/java/com/seaofnodes/simple/Chapter20Test.java
index e14c323f..f0b31065 100644
--- a/chapter22/src/test/java/com/seaofnodes/simple/Chapter20Test.java
+++ b/chapter22/src/test/java/com/seaofnodes/simple/Chapter20Test.java
@@ -93,7 +93,7 @@ return sqrt(arg) + sqrt(cast_int);
@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 (add,.[],(muli,.[]));");
+ testCPU(src,"x86_64_v2", "SystemV",-1,"return .[];");
testCPU(src,"riscv" , "SystemV", 8,"return (add,.[],(mul,.[],1000));");
testCPU(src,"arm" , "SystemV", 5,"return (add,.[],(mul,.[],1000));");
}
@@ -102,8 +102,8 @@ return sqrt(arg) + sqrt(cast_int);
@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", 25,null);
- testCPU(src,"riscv" , "SystemV", 8,null);
+ testCPU(src,"x86_64_v2", "SystemV", 9,null);
+ testCPU(src,"riscv" , "SystemV", 5,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 8525ef76..4210fc56 100644
--- a/chapter22/src/test/java/com/seaofnodes/simple/Chapter21Test.java
+++ b/chapter22/src/test/java/com/seaofnodes/simple/Chapter21Test.java
@@ -6,7 +6,6 @@ import com.seaofnodes.simple.node.cpus.riscv.riscv;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
-import org.junit.Ignore;
import org.junit.Test;
import static org.junit.Assert.*;
@@ -39,8 +38,8 @@ 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",16,"return Top;");
+ String src = "struct S { int i; }; S !s = new S; while(1) s.i++; return s.i;";
+ testCPU(src,"x86_64_v2", "SystemV",0,"return Top;");
testCPU(src,"riscv" , "SystemV",2,"return Top;");
testCPU(src,"arm" , "SystemV",2,"return Top;");
}
@@ -48,7 +47,7 @@ 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",-1,"return (add,.[],(muli,.[]));");
+ testCPU(src,"x86_64_v2", "SystemV",-1,"return .[];");
testCPU(src,"riscv" , "SystemV", 7,"return (add,.[],(mul,.[],1000));");
testCPU(src,"arm" , "SystemV", 5,"return (add,.[],(mul,.[],1000));");
}
@@ -64,20 +63,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", 25,null);
- testCPU(src,"riscv" , "SystemV", 8,null);
+ testCPU(src,"x86_64_v2", "SystemV", 9,null);
+ testCPU(src,"riscv" , "SystemV", 5,null);
testCPU(src,"arm" , "SystemV", 6,null);
}
@Test public void testStringExport() throws IOException {
- TestC.run("stringHash", 25);
+ TestC.run("stringHash", 9);
}
@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 (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))));");
+ 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));");
}
@Test public void testNewtonExport() throws IOException {
@@ -120,7 +119,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, 473);
+ TestC.run("sieve",sprimes, 257);
// Evaluate on RISC5 emulator; expect return of an array of primes in
// the simulated heap.
diff --git a/chapter22/src/test/java/com/seaofnodes/simple/TestC.java b/chapter22/src/test/java/com/seaofnodes/simple/TestC.java
index 761da0ba..2e68a029 100644
--- a/chapter22/src/test/java/com/seaofnodes/simple/TestC.java
+++ b/chapter22/src/test/java/com/seaofnodes/simple/TestC.java
@@ -1,10 +1,10 @@
package com.seaofnodes.simple;
import com.seaofnodes.simple.codegen.CodeGen;
+import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
-import java.io.InputStream;
-import java.io.IOException;
+import java.util.concurrent.TimeUnit;
import static org.junit.Assert.*;
// Call a C driver program, which calls a Simple-generated .o, and self-checks.
@@ -44,14 +44,16 @@ public abstract class TestC {
// Compile simple, emit ELF
CodeGen code = new CodeGen(src).driver( CPU_PORT, simple_conv, obj);
- // Compile the C program
+ // Compile the C program. Compiling code and constants in the low
+ // 2Gig. Pointers are 64b BUT since always in the low 2G all the
+ // high bits are zero - and Simple code can be emitted treating
+ // pointers as 4bytes.
var params = new Ary<>(String.class);
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
- "-m32",
"-g",
"-o",
bin,
@@ -63,25 +65,30 @@ public abstract class TestC {
}
Process gcc = new ProcessBuilder(params.asAry()).redirectErrorStream(true).start();
- byte error;
- try { error = (byte)gcc.waitFor(); } catch( InterruptedException e ) { throw new IOException("interrupted"); }
+ int exit;
+ try {
+ boolean normal = gcc.waitFor(2, TimeUnit.SECONDS);
+ exit = normal ? gcc.exitValue() : -1; // no exit???
+ } catch( InterruptedException e ) {
+ throw new IOException("interrupted");
+ }
String result = new String(gcc.getInputStream().readAllBytes());
- if( error!=0 ) {
- System.err.println("gcc error code: "+error);
+ if( exit!=0 ) {
+ System.err.println("gcc error code: "+exit);
System.err.println(result);
}
- assertEquals( 0, error );
+ assertEquals( 0, exit );
//assertTrue(result.isEmpty()); // No data in error stream
// Execute results
Process smp = new ProcessBuilder(bin).redirectErrorStream(true).start();
- try { error = (byte)smp.waitFor(); } catch( InterruptedException e ) { throw new IOException("interrupted"); }
+ try { exit = (byte)smp.waitFor(); } catch( InterruptedException e ) { throw new IOException("interrupted"); }
result = new String(smp.getInputStream().readAllBytes());
- if( error!=0 ) {
- System.err.println("exec error code: "+error);
+ if( exit!=0 ) {
+ System.err.println("exec exit code: "+exit);
System.err.println(result);
}
- assertEquals( 0, error );
+ assertEquals( 0, exit );
assertEquals(expected,result);
// Allocation quality not degraded
@@ -91,4 +98,4 @@ public abstract class TestC {
assertEquals("Expect spills:",spills,code._regAlloc._spillScaled,delta);
}
-} \ No newline at end of file
+}