summaryrefslogtreecommitdiff
path: root/chapter21/src
diff options
context:
space:
mode:
Diffstat (limited to 'chapter21/src')
-rw-r--r--chapter21/src/main/java/com/seaofnodes/simple/codegen/ElfFile.java4
-rw-r--r--chapter21/src/test/java/com/seaofnodes/simple/Compile.java59
2 files changed, 52 insertions, 11 deletions
diff --git a/chapter21/src/main/java/com/seaofnodes/simple/codegen/ElfFile.java b/chapter21/src/main/java/com/seaofnodes/simple/codegen/ElfFile.java
index 5bf83542..6c8a1fc1 100644
--- a/chapter21/src/main/java/com/seaofnodes/simple/codegen/ElfFile.java
+++ b/chapter21/src/main/java/com/seaofnodes/simple/codegen/ElfFile.java
@@ -283,7 +283,7 @@ public class ElfFile {
// populate function symbols
encodeFunctions(symbols, text);
// populate big constants
- encodeConstants(symbols, rdata);
+ // encodeConstants(symbols, rdata);
// create .text relocations
DataSection text_rela = new DataSection(".rela.text", 4 /* SHT_RELA */);
@@ -301,7 +301,7 @@ public class ElfFile {
write8(text_rela._contents, -4);
}
// relocations to constants
- for (Map.Entry<Node,Type> e : _code._encoding._bigCons.entrySet()) {
+ if (false) for (Map.Entry<Node,Type> e : _code._encoding._bigCons.entrySet()) {
int nid = e.getKey()._nid;
int sym_id = _bigCons.get(e.getValue())._index;
int offset = _code._encoding._opStart[nid] + _code._encoding._opLen[nid] - 4;
diff --git a/chapter21/src/test/java/com/seaofnodes/simple/Compile.java b/chapter21/src/test/java/com/seaofnodes/simple/Compile.java
index 5286029a..dfe30a27 100644
--- a/chapter21/src/test/java/com/seaofnodes/simple/Compile.java
+++ b/chapter21/src/test/java/com/seaofnodes/simple/Compile.java
@@ -1,27 +1,68 @@
package com.seaofnodes.simple;
import com.seaofnodes.simple.codegen.CodeGen;
+import com.seaofnodes.simple.node.FunNode;
import org.junit.Ignore;
import org.junit.Test;
import java.io.IOException;
+import java.nio.charset.StandardCharsets;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.util.ArrayList;
+import static org.junit.Assert.*;
import static com.seaofnodes.simple.Main.PORTS;
public class Compile {
- @Test
- @Ignore
- public void testCompile() throws IOException, InterruptedException {
+ private static Process run(String simple, String c) throws IOException, InterruptedException {
boolean USE_WSL = System.getProperty("os.name").startsWith("Windows");
- CodeGen code = new CodeGen("return 13;");
+ CodeGen code = new CodeGen(simple);
code.parse().opto().typeCheck().instSelect(PORTS,"x86_64_v2", "SystemV").GCM().localSched().regAlloc().encode();
+ if (c != null) for (var n:code._start._outputs) if (n instanceof FunNode f && f._name.equals("main")) f._name = "simple_main";
code.exportELF("test.o");
- Process p = Runtime.getRuntime().exec(USE_WSL ? new String[]{"wsl.exe", "gcc", "-o", "test", "test.o"}:new String[]{"gcc", "-o", "test", "test.o"});
- assert p.waitFor() == 0;
- p = Runtime.getRuntime().exec(USE_WSL ? new String[]{"wsl.exe", "./test"}:new String[]{"./test"});
- System.out.println(p.waitFor());
- assert p.waitFor() == 13;
+ var params = new ArrayList<String>();
+ if (USE_WSL) params.add("wsl.exe");
+ params.add("gcc");
+ params.add("-o");
+ params.add("test");
+ if (c != null) {
+ Files.writeString(Path.of("test.c"), c);
+ params.add("test.c");
+ }
+ params.add("test.o");
+ Process p = Runtime.getRuntime().exec(params.toArray(String[]::new));
+ assertEquals(0, p.waitFor());
+ return Runtime.getRuntime().exec(USE_WSL ? new String[]{"wsl.exe", "./test"}:new String[]{"./test"});
+ }
+
+ @Test
+ @Ignore
+ public void testCompile() throws IOException, InterruptedException {
+ var p = run("""
+ val testMy = { flt x ->
+ flt guess = x;
+ while( 1 ) {
+ flt next = (x/guess + guess)/2;
+ if( next == guess ) return guess;
+ guess = next;
+ }
+ };
+ ""","""
+
+ #include <stdio.h>
+
+ extern double testMy(double);
+
+ int main(int argc, char** argv) {
+ printf("%f\\n", testMy(2.0));
+ return 0;
+ }
+
+ """);
+ assertEquals(0, p.waitFor());
+ assertEquals("1.414214\n", new String(p.getInputStream().readAllBytes(), StandardCharsets.UTF_8));
}
}