summaryrefslogtreecommitdiff
path: root/chapter21/src/test/java
diff options
context:
space:
mode:
authorXmiliaH <45106915+XmiliaH@users.noreply.github.com>2025-03-25 18:44:41 +0100
committerGitHub <noreply@github.com>2025-03-25 10:44:41 -0700
commit5fd3508bd3c45ed83757b47caa7a24361bec71cb (patch)
tree456db64024c496b78fd16486a850b7d0f17cdbf1 /chapter21/src/test/java
parent1f87fba4ee6b874eed6c3652e3e8a25b5b0726aa (diff)
downloadSimple-5fd3508bd3c45ed83757b47caa7a24361bec71cb.tar.gz
Simple-5fd3508bd3c45ed83757b47caa7a24361bec71cb.zip
Call Simple function from C (#179)
* Call simple function from c * Small fix * Use different test values
Diffstat (limited to 'chapter21/src/test/java')
-rw-r--r--chapter21/src/test/java/com/seaofnodes/simple/Compile.java59
1 files changed, 50 insertions, 9 deletions
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));
}
}