diff options
Diffstat (limited to 'chapter04')
| -rw-r--r-- | chapter04/Makefile | 9 | ||||
| -rw-r--r-- | chapter04/pom.xml | 3 | ||||
| -rw-r--r-- | chapter04/src/main/java/com/seaofnodes/simple/print/SimpleGraphAdapter.java | 38 |
3 files changed, 49 insertions, 1 deletions
diff --git a/chapter04/Makefile b/chapter04/Makefile index 7f55b343..e6c8f275 100644 --- a/chapter04/Makefile +++ b/chapter04/Makefile @@ -36,6 +36,10 @@ CLZDIR:= build/classes main_javas := $(wildcard $(SRC)/$(SIMPLE)/*java $(SRC)/$(SIMPLE)/*/*java) test_javas := $(wildcard $(TST)/$(SIMPLE)/*java $(TST)/$(SIMPLE)/*/*java) main_classes := $(patsubst $(SRC)/%java,$(CLZDIR)/main/%class,$(main_javas)) +# Compile the common adapter into this chapter without copying its sources. +GRAPH_DIR := $(firstword $(wildcard ../graph graph)) +graph_javas := $(wildcard $(GRAPH_DIR)/src/main/java/com/seaofnodes/graph/*.java) +main_javas += $(graph_javas) test_classes := $(patsubst $(TST)/%java,$(CLZDIR)/test/%class,$(test_javas)) test_cp := $(patsubst $(TST)/$(SIMPLE)/%.java,com.seaofnodes.simple.%,$(wildcard $(TST)/$(SIMPLE)/*Test.java)) classes = $(main_classes) $(test_classes) @@ -52,8 +56,11 @@ endif default: $(default_targets) +.PHONY: build +build: $(main_classes) + # Compile just the out-of-date files -$(main_classes): build/classes/main/%class: $(SRC)/%java +$(main_classes): $(CLZDIR)/main/%class: $(SRC)/%java $(graph_javas) @echo "compiling " $@ " because " $? @[ -d $(CLZDIR)/main ] || mkdir -p $(CLZDIR)/main @javac $(JAVAC_ARGS) -cp "$(CLZDIR)/main$(SEP)$(jars)" -sourcepath $(SRC) -d $(CLZDIR)/main $(main_javas) diff --git a/chapter04/pom.xml b/chapter04/pom.xml index 0fdebb14..5b9c7c6e 100644 --- a/chapter04/pom.xml +++ b/chapter04/pom.xml @@ -11,4 +11,7 @@ <artifactId>chapter04</artifactId> <packaging>jar</packaging> <name>Chapter 4</name> + <properties> + <simple.graph.directory>${project.basedir}/../graph</simple.graph.directory> + </properties> </project> diff --git a/chapter04/src/main/java/com/seaofnodes/simple/print/SimpleGraphAdapter.java b/chapter04/src/main/java/com/seaofnodes/simple/print/SimpleGraphAdapter.java new file mode 100644 index 00000000..0755613b --- /dev/null +++ b/chapter04/src/main/java/com/seaofnodes/simple/print/SimpleGraphAdapter.java @@ -0,0 +1,38 @@ +package com.seaofnodes.simple.print; + +import com.seaofnodes.graph.GraphAdapter; +import com.seaofnodes.graph.GraphSnapshot; +import com.seaofnodes.graph.GraphSnapshot.*; +import com.seaofnodes.simple.node.*; +import com.seaofnodes.simple.node.Node; +import java.util.ArrayList; + +/** Chapter 4's view of the IR; no knowledge of browser, transport or layout. */ +public class SimpleGraphAdapter extends GraphAdapter<Node> { + @Override protected int id(Node n) { return n._nid; } + @Override protected int nIns(Node n) { return n.nIns(); } + @Override protected Node in(Node n, int idx) { return n.in(idx); } + @Override protected int nOuts(Node n) { return n.nOuts(); } + @Override protected Node out(Node n, int idx) { return n.out(idx); } + + private ArrayList<Edge> edges(Node n) { + String[] names = n instanceof ScopeNode scope ? scope.reverseNames() : null; + var edges = new ArrayList<Edge>(); + for( int i = 0; i < n.nIns(); i++ ) { + Role role = n instanceof ScopeNode || n instanceof ConstantNode ? Role.ASSOC + : n instanceof ProjNode ? (n.isCFG() ? Role.CTRL : Role.DATA) + : i == 0 ? Role.CTRL : Role.DATA; + edges.add(new Edge(i, ref(n.in(i)), role, names == null ? null : names[i])); + } + return edges; + } + + @Override protected GraphSnapshot.Node desc(Node n) { + Projection proj = n instanceof ProjNode p + ? new Projection(n.nIns() == 0 ? 0 : ref(n.in(0)), p._idx) : null; + Kind kind = n instanceof ScopeNode ? Kind.SCOPE : n.isCFG() ? Kind.CTRL : Kind.DATA; + String label = n.label(); + return new GraphSnapshot.Node(n._nid, label == null ? n.getClass().getSimpleName() : label, + n._type == null ? null : n._type.toString(), kind, edges(n), proj); + } +} |
