summaryrefslogtreecommitdiff
path: root/chapter01/src/main/java/com
diff options
context:
space:
mode:
authordibyendumajumdar <mobile@majumdar.org.uk>2023-09-17 15:59:44 +0100
committerdibyendumajumdar <mobile@majumdar.org.uk>2023-09-17 15:59:44 +0100
commitadb2ae48c7e92e3a9024b043e476162a11ef9641 (patch)
treedf59f8bb3b1b3dbb41b76d97bffc0bf392f6cca3 /chapter01/src/main/java/com
parent4ea0d08fe97cdb051cf4bb175fec0672f07f3c84 (diff)
downloadSimple-adb2ae48c7e92e3a9024b043e476162a11ef9641.tar.gz
Simple-adb2ae48c7e92e3a9024b043e476162a11ef9641.zip
Initial definition of nodes that will be relevant for chapter 1.
Diffstat (limited to 'chapter01/src/main/java/com')
-rw-r--r--chapter01/src/main/java/com/seaofnodes/simple/node/ConstantNode.java16
-rw-r--r--chapter01/src/main/java/com/seaofnodes/simple/node/Control.java9
-rw-r--r--chapter01/src/main/java/com/seaofnodes/simple/node/Node.java94
-rw-r--r--chapter01/src/main/java/com/seaofnodes/simple/node/NodeIDGenerator.java19
-rw-r--r--chapter01/src/main/java/com/seaofnodes/simple/node/RegionNode.java11
-rw-r--r--chapter01/src/main/java/com/seaofnodes/simple/node/ReturnNode.java11
-rw-r--r--chapter01/src/main/java/com/seaofnodes/simple/node/StartNode.java11
7 files changed, 171 insertions, 0 deletions
diff --git a/chapter01/src/main/java/com/seaofnodes/simple/node/ConstantNode.java b/chapter01/src/main/java/com/seaofnodes/simple/node/ConstantNode.java
new file mode 100644
index 00000000..7f798301
--- /dev/null
+++ b/chapter01/src/main/java/com/seaofnodes/simple/node/ConstantNode.java
@@ -0,0 +1,16 @@
+package com.seaofnodes.simple.node;
+
+public class ConstantNode extends Node {
+
+ long _value;
+
+ public ConstantNode(int nid, long value) {
+ super(nid, OP_CONSTANT);
+ _value = value;
+ }
+
+ public ConstantNode(NodeIDGenerator idGenerator, long value) {
+ super(idGenerator, OP_CONSTANT);
+ _value = value;
+ }
+}
diff --git a/chapter01/src/main/java/com/seaofnodes/simple/node/Control.java b/chapter01/src/main/java/com/seaofnodes/simple/node/Control.java
new file mode 100644
index 00000000..48cf2b7a
--- /dev/null
+++ b/chapter01/src/main/java/com/seaofnodes/simple/node/Control.java
@@ -0,0 +1,9 @@
+package com.seaofnodes.simple.node;
+
+
+/**
+ * Control interface is a marker interface to be implemented
+ * by Control nodes.
+ */
+public interface Control {
+}
diff --git a/chapter01/src/main/java/com/seaofnodes/simple/node/Node.java b/chapter01/src/main/java/com/seaofnodes/simple/node/Node.java
new file mode 100644
index 00000000..0c6cdd3f
--- /dev/null
+++ b/chapter01/src/main/java/com/seaofnodes/simple/node/Node.java
@@ -0,0 +1,94 @@
+package com.seaofnodes.simple.node;
+
+import java.util.Arrays;
+import java.util.List;
+
+/**
+ * All Nodes in the Sea of Nodes IR inherit from the Node class.
+ * The Node class provides common functionality used by all subtypes.
+ * Subtypes of Node specialize by overriding methods.
+ */
+public class Node {
+
+ static final byte OP_START = 1;
+ static final byte OP_REGION = 2;
+ static final byte OP_RETURN = 3;
+ static final byte OP_CONSTANT = 4;
+
+ /**
+ * The opcode for this node
+ */
+ public final byte _opCode;
+
+ /**
+ * Each node has a unique ID within a compilation context
+ */
+ public final int _nid;
+
+ /**
+ * Inputs to the node.
+ *
+ * Generally fixed length, ordered, nulls allowed, no unused trailing space.
+ * The first input (offset 0) must be a Control node.
+ * @see Control
+ */
+ public final List<Node> _inputs;
+
+ protected Node(int nid, byte opCode, Node ...inputs)
+ {
+ _nid = nid;
+ _opCode = opCode;
+ _inputs = Arrays.asList(inputs);
+ }
+
+ protected Node(NodeIDGenerator idGenerator, byte opCode, Node ...inputs)
+ {
+ _nid = idGenerator.newNodeID();
+ _opCode = opCode;
+ _inputs = Arrays.asList(inputs);
+ }
+
+ /**
+ * Gets the ith input node
+ * @param i Offset of the input node
+ * @return Input node or null
+ */
+ public Node in(int i) { return _inputs != null ? _inputs.get(i) : null; }
+
+ public int numInputs() { return _inputs != null ? _inputs.size() : 0; }
+
+ /**
+ * Is this a control node, all control nodes are marked with
+ * the interface Control.
+ */
+ public boolean isControl() { return this instanceof Control; }
+
+ /**
+ * Hash is function of this nodes type, its _nid and
+ * inputs, or opcode + input_nids
+ */
+ @Override
+ public int hashCode() {
+ int sum = _opCode;
+ for (int i = 0; i < numInputs(); i++)
+ if (in(i) != null)
+ sum ^= in(i)._nid;
+ return sum;
+ }
+
+ /**
+ * Equals is opcode + input nids.
+ * Note that nodes are compared by reference.
+ */
+ @Override
+ public boolean equals(Object o) {
+ if (this == o) return true;
+ if (!(o instanceof Node n)) return false;
+ if (_opCode != n._opCode) return false;
+ if (numInputs() != n.numInputs()) return false;
+ for (int i = 0; i < numInputs(); i++)
+ if (in(i) != n.in(i))
+ return false;
+ return true;
+ }
+}
diff --git a/chapter01/src/main/java/com/seaofnodes/simple/node/NodeIDGenerator.java b/chapter01/src/main/java/com/seaofnodes/simple/node/NodeIDGenerator.java
new file mode 100644
index 00000000..96a292df
--- /dev/null
+++ b/chapter01/src/main/java/com/seaofnodes/simple/node/NodeIDGenerator.java
@@ -0,0 +1,19 @@
+package com.seaofnodes.simple.node;
+
+/**
+ * Nodes are given unique dense ids.
+ */
+public class NodeIDGenerator {
+ /**
+ * Counter for node IDs, do not hand out NID 0
+ * (Presumably) because it is used for the control node?
+ */
+ private int nodeIDCounter = 1;
+
+ public int newNodeID() {
+ if (nodeIDCounter > 100000) {
+ throw new RuntimeException("infinite node create loop");
+ }
+ return nodeIDCounter++;
+ }
+}
diff --git a/chapter01/src/main/java/com/seaofnodes/simple/node/RegionNode.java b/chapter01/src/main/java/com/seaofnodes/simple/node/RegionNode.java
new file mode 100644
index 00000000..b740deff
--- /dev/null
+++ b/chapter01/src/main/java/com/seaofnodes/simple/node/RegionNode.java
@@ -0,0 +1,11 @@
+package com.seaofnodes.simple.node;
+
+public class RegionNode extends Node implements Control {
+ public RegionNode(int nid, Node... inputs) {
+ super(nid, OP_REGION, inputs);
+ }
+
+ public RegionNode(NodeIDGenerator idGenerator, Node... inputs) {
+ super(idGenerator, OP_REGION, inputs);
+ }
+}
diff --git a/chapter01/src/main/java/com/seaofnodes/simple/node/ReturnNode.java b/chapter01/src/main/java/com/seaofnodes/simple/node/ReturnNode.java
new file mode 100644
index 00000000..655fd11f
--- /dev/null
+++ b/chapter01/src/main/java/com/seaofnodes/simple/node/ReturnNode.java
@@ -0,0 +1,11 @@
+package com.seaofnodes.simple.node;
+
+public class ReturnNode extends Node implements Control {
+ public ReturnNode(int nid, Node... inputs) {
+ super(nid, OP_RETURN, inputs);
+ }
+
+ public ReturnNode(NodeIDGenerator idGenerator, Node... inputs) {
+ super(idGenerator, OP_RETURN, inputs);
+ }
+}
diff --git a/chapter01/src/main/java/com/seaofnodes/simple/node/StartNode.java b/chapter01/src/main/java/com/seaofnodes/simple/node/StartNode.java
new file mode 100644
index 00000000..8117c4f6
--- /dev/null
+++ b/chapter01/src/main/java/com/seaofnodes/simple/node/StartNode.java
@@ -0,0 +1,11 @@
+package com.seaofnodes.simple.node;
+
+public class StartNode extends Node implements Control {
+ public StartNode(int nid, Node... inputs) {
+ super(nid, OP_START, inputs);
+ }
+
+ public StartNode(NodeIDGenerator idGenerator, Node... inputs) {
+ super(idGenerator, OP_START, inputs);
+ }
+}