diff options
| author | XmiliaH <45106915+XmiliaH@users.noreply.github.com> | 2025-03-25 18:44:41 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2025-03-25 10:44:41 -0700 |
| commit | 5fd3508bd3c45ed83757b47caa7a24361bec71cb (patch) | |
| tree | 456db64024c496b78fd16486a850b7d0f17cdbf1 /chapter21/src | |
| parent | 1f87fba4ee6b874eed6c3652e3e8a25b5b0726aa (diff) | |
| download | Simple-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')
| -rw-r--r-- | chapter21/src/main/java/com/seaofnodes/simple/codegen/ElfFile.java | 4 | ||||
| -rw-r--r-- | chapter21/src/test/java/com/seaofnodes/simple/Compile.java | 59 |
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)); } } |
