1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
|
import com.seaofnodes.simple.codegen.CodeGen;
import com.seaofnodes.simple.node.*;
import java.nio.file.*;
import java.util.regex.Pattern;
/** Run from the repo root with Chapter 21's built classes on the classpath. */
public class BackendDump {
static String js(String s) {
return "\""+s.replace("\\","\\\\").replace("\"","\\\"")
.replace("\r", "").replace("\n","\\n")+"\"";
}
public static void main(String[] args) throws Exception {
Path deck=Path.of("docs/2026_REBASE");
String source=Files.readString(deck.resolve("demos/backend.smp"));
CodeGen code=new CodeGen(source).parse().opto().typeCheck().loopTree()
.instSelect("x86_64_v2","SystemV");
String selected=code.toString();
// Every row from the whole graph, in printer order. Show concrete Java
// classes and every input slot; omit types and redundant use lists.
StringBuilder compact=new StringBuilder("id node inputs\n");
var rows=Pattern.compile("(?m)^\\s*(\\d+) ").matcher(selected);
int count=0;
while(rows.find()) {
Node n=code.f(Integer.parseInt(rows.group(1)));
String name=n.getClass().getSimpleName();
if(n instanceof ParmNode || n instanceof CProjNode) name+=":"+n.label();
compact.append(String.format("%2d %-16s",n._nid,name));
for(int i=0;i<n.nIns();i++) compact.append(' ').append(n.in(i)==null?"_":n.in(i)._nid);
compact.append('\n');
count++;
}
code.GCM().localSched().regAlloc();
String assembly=code.asm();
code.encode();
String encoded=code.asm();
Files.writeString(deck.resolve("assets/backend-selected.txt"),selected);
Files.writeString(deck.resolve("assets/backend-registers.txt"),assembly);
Files.writeString(deck.resolve("assets/backend-encoded.txt"),encoded);
Files.writeString(deck.resolve("backend.js"),
"/* Generated by tools/BackendDump.java; Chapter 21, x86-64 System V. */\n"+
"window.REBASE.backend = {\n source: "+js(source)+",\n selected: "+js(compact.toString())+
",\n assembly: "+js(assembly)+",\n encoded: "+js(encoded)+"\n};\n");
System.out.println("Captured "+count+" selected nodes, allocated assembly, and encoded assembly.");
}
}
|