MCPcopy Create free account
hub / github.com/SeaOfNodes/Simple / TestC

Class TestC

chapter22/src/test/java/com/seaofnodes/simple/TestC.java:12–106  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

10// Call a C driver program, which calls a Simple-generated .o, and self-checks.
11// Expects GCC from the default CLI, compiles C, compiles Simple, links and runs.
12public abstract class TestC {
13
14 public static final String OS = System.getProperty("os.name");
15 public static final String CPU = System.getProperty("os.arch");
16
17 public static final String CALL_CONVENTION = OS.startsWith("Windows") ? "Win64" : "SystemV";
18 public static final String CPU_PORT = switch( CPU ) {
19 case "amd64" -> "x86_64_v2";
20 default -> throw Utils.TODO("Map Yer CPU Port Here");
21 };
22
23 public static void run( String file, int spills ) throws IOException { run(file,"",spills); }
24
25 public static void run( String file, String expected, int spills ) throws IOException {
26 run("src/test/java/com/seaofnodes/simple/progs",file,expected,spills);
27 }
28
29 // Compile and run a simple program
30 public static void run( String dir, String file, String expected, int spills ) throws IOException {
31 // Files
32 String cfile = dir+"/"+file+".c" ;
33 String sfile = dir+"/"+file+".smp";
34 String efile = "build/objs/"+file;
35
36 // Compile and export Simple
37 String src = Files.readString(Path.of(sfile));
38 run(src,CALL_CONVENTION,"",cfile,efile,"S",expected,spills);
39 }
40
41 public static void run( String src, String simple_conv, String c_conv, String cfile, String efile, String xtn, String expected, int spills ) throws IOException {
42 String bin = efile+xtn;
43 String obj = bin+".o";
44 String exe = OS.startsWith("Windows") ? bin+".exe" : bin;
45 // Compile simple, emit ELF
46 CodeGen code = new CodeGen(src).driver( CPU_PORT, simple_conv, obj);
47 String result = gcc(obj, c_conv, cfile, false, exe );
48 assertEquals(expected,result);
49
50 // Allocation quality not degraded
51 int delta = spills>>3;
52 if( delta==0 ) delta = 1;
53 if( spills != -1 )
54 assertEquals("Expect spills:",spills,code._regAlloc._spillScaled,delta);
55 }
56
57 public static String gcc( String obj, String c_conv, String cfile, boolean stdin, String... args ) throws IOException {
58
59 // Compile the C program. Compiling code and constants in the low
60 // 2Gig. Pointers are 64b BUT since always in the low 2G all the
61 // high bits are zero - and Simple code can be emitted treating
62 // pointers as 4bytes.
63 var params = new Ary<>(String.class);
64 params.add("gcc");
65 if( cfile!=null ) params.add(cfile); // Associated C driver, usually has a `main`
66 params.addAll(new String[] {
67 obj,
68 "-lm", // Picks up 'sqrt' for newtonFloat tests to compare
69 "-g",

Callers

nothing calls this directly

Calls 1

TODOMethod · 0.95

Tested by

no test coverage detected