# HG changeset patch # User Thomas Wuerthinger # Date 1303934223 -7200 # Node ID 3e960f8a6c52082c45df29d5dc73008d7bf92bd5 # Parent b6cd17226aad8a08df0d51114c8552b39d38d511# Parent 7a0e1bd2bb64753b794c88cff466a94cf96183f3 Merge. diff -r b6cd17226aad -r 3e960f8a6c52 graal/GraalGraph/.classpath --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/graal/GraalGraph/.classpath Wed Apr 27 21:57:03 2011 +0200 @@ -0,0 +1,8 @@ + + + + + + + + diff -r b6cd17226aad -r 3e960f8a6c52 graal/GraalGraph/.project --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/graal/GraalGraph/.project Wed Apr 27 21:57:03 2011 +0200 @@ -0,0 +1,17 @@ + + + GraalGraph + + + + + + org.eclipse.jdt.core.javabuilder + + + + + + org.eclipse.jdt.core.javanature + + diff -r b6cd17226aad -r 3e960f8a6c52 graal/GraalGraph/.settings/org.eclipse.jdt.core.prefs --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/graal/GraalGraph/.settings/org.eclipse.jdt.core.prefs Wed Apr 27 21:57:03 2011 +0200 @@ -0,0 +1,12 @@ +#Wed Apr 27 16:04:34 CEST 2011 +eclipse.preferences.version=1 +org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled +org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.6 +org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve +org.eclipse.jdt.core.compiler.compliance=1.6 +org.eclipse.jdt.core.compiler.debug.lineNumber=generate +org.eclipse.jdt.core.compiler.debug.localVariable=generate +org.eclipse.jdt.core.compiler.debug.sourceFile=generate +org.eclipse.jdt.core.compiler.problem.assertIdentifier=error +org.eclipse.jdt.core.compiler.problem.enumIdentifier=error +org.eclipse.jdt.core.compiler.source=1.6 diff -r b6cd17226aad -r 3e960f8a6c52 graal/GraalGraph/src/com/oracle/graal/graph/Graph.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/graal/GraalGraph/src/com/oracle/graal/graph/Graph.java Wed Apr 27 21:57:03 2011 +0200 @@ -0,0 +1,34 @@ +package com.oracle.graal.graph; + +import java.util.ArrayList; +import java.util.Collection; +import java.util.Collections; + +/** + * @author Gilles Duboscq + * + */ +public class Graph { + private final ArrayList nodes; + private int nextId; + + public Graph() { + nodes = new ArrayList(); + } + + public synchronized int nextId(Node node) { + int id = nextId++; + nodes.add(id, node); + return id; + } + + public Collection getNodes(){ + return Collections.unmodifiableCollection(nodes); + } + + public Node local(Node node) { + if(node.getGraph() == this) + return node; + return node.cloneNode(this); + } +} diff -r b6cd17226aad -r 3e960f8a6c52 graal/GraalGraph/src/com/oracle/graal/graph/Node.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/graal/GraalGraph/src/com/oracle/graal/graph/Node.java Wed Apr 27 21:57:03 2011 +0200 @@ -0,0 +1,164 @@ +package com.oracle.graal.graph; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collection; +import java.util.Collections; +import java.util.Iterator; + +/** + * @author Gilles Duboscq + * + */ +public abstract class Node { + private final Graph graph; + private final int id; + private final NodeArray inputs; + private final NodeArray successors; + private final ArrayList usages; + private final ArrayList predecessors; + + public Node(Node[] inputs, Node[] successors, Graph graph) { + this.graph = graph; + if(graph != null) { + this.id = graph.nextId(this); //this pointer escaping in a constructor.. + }else { + this.id = -1; + } + this.inputs = new NodeArray(inputs); + this.successors = new NodeArray(successors); + this.predecessors = new ArrayList(); + this.usages = new ArrayList(); + } + + public Node(int inputs, int successors, Graph graph) { + this(nullNodes(inputs, graph), nullNodes(successors, graph), graph); + } + + public class NodeArray implements Iterable{ + private final Node[] nodes; + + public NodeArray(Node[] nodes) { + this.nodes = nodes; + } + + @Override + public Iterator iterator() { + return Arrays.asList(this.nodes).iterator(); + } + + public Node set(int index, Node node) { + if(node.graph != Node.this.graph) { + // fail ? + } + Node old = nodes[index]; + nodes[index] = node; + if(Node.this.inputs == this) { // :-/ + old.usages.remove(Node.this); + node.usages.add(Node.this); + }else /*if(Node.this.successors == this)*/{ + old.predecessors.remove(Node.this); + node.predecessors.add(Node.this); + } + + return old; + } + + public boolean contains(Node n) { + for(int i = 0; i < nodes.length; i++) + if(nodes[i] == n) //equals? + return true; + return false; + } + + public boolean replace(Node toReplace, Node replacement) { + for(int i = 0; i < nodes.length; i++) { + if(nodes[i] == toReplace) { // equals? + this.set(i, replacement); + return true; //replace only one occurrence + } + } + return false; + } + + public Node[] asArray() { + Node[] copy = new Node[nodes.length]; + System.arraycopy(nodes, 0, copy, 0, nodes.length); + return copy; + } + } + + public Collection getPredecessors() { + return Collections.unmodifiableCollection(predecessors); + } + + public Collection getUsages() { + return Collections.unmodifiableCollection(usages); + } + + public NodeArray getInputs() { + return inputs; + } + + public NodeArray getSuccessors() { + return successors; + } + + public int getId() { + return id; + } + + public Graph getGraph() { + return graph; + } + + public void replace(Node other) { + if(other.graph != this.graph) { + other = other.cloneNode(this.graph); + } + Node[] myInputs = inputs.nodes; + for(int i = 0; i < myInputs.length; i++) { + other.inputs.set(i, myInputs[i]); + } + for(Node usage : usages) { + usage.inputs.replace(this, other); + } + + Node[] mySuccessors = successors.nodes; + for(int i = 0; i < mySuccessors.length; i++) { + other.successors.set(i, mySuccessors[i]); + } + for(Node predecessor : predecessors) { + predecessor.successors.replace(this, other); + } + } + + public abstract Node cloneNode(Graph into); + + @Override + public boolean equals(Object obj) { + if(obj == this) + return true; + if(obj.getClass() == this.getClass()) { + Node other = (Node)obj; + if(other.id == this.id && other.graph == this.graph) + return true; + } + return false; + } + + protected Node getInput(int index) { + return this.inputs.nodes[index]; + } + + protected Node getSuccessor(int index) { + return this.successors.nodes[index]; + } + + private static Node[] nullNodes(int number, Graph graph) { + Node[] nodes = new Node[number]; + for(int i = 0; i < number; i++) + nodes[i] = new NullNode(0, 0, graph); + return nodes; + } +} diff -r b6cd17226aad -r 3e960f8a6c52 graal/GraalGraph/src/com/oracle/graal/graph/NullNode.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/graal/GraalGraph/src/com/oracle/graal/graph/NullNode.java Wed Apr 27 21:57:03 2011 +0200 @@ -0,0 +1,18 @@ +package com.oracle.graal.graph; + +/** + * @author Gilles Duboscq + * + */ +public class NullNode extends Node { + + public NullNode(int inputs, int successors, Graph graph) { + super(inputs, successors, graph); + } + + @Override + public NullNode cloneNode(Graph into) { + return new NullNode(0, 0, into); + } + +} diff -r b6cd17226aad -r 3e960f8a6c52 graal/GraalGraph/test/com/oracle/graal/graph/NodeTest.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/graal/GraalGraph/test/com/oracle/graal/graph/NodeTest.java Wed Apr 27 21:57:03 2011 +0200 @@ -0,0 +1,34 @@ +package com.oracle.graal.graph; + +import static org.junit.Assert.*; + +import org.junit.Test; + +public class NodeTest { + + @Test + public void testReplace() { + DummyNode n1 = new DummyNode(2, 1, null); + + Graph g1 = new Graph(); + + DummyNode n2 = new DummyNode(1, 1, g1); + } + + private static class DummyNode extends Node{ + + public DummyNode(int inputs, int successors, Graph graph) { + super(inputs, successors, graph); + } + + public DummyNode(Node[] inputs, Node[] successors, Graph graph) { + super(inputs, successors, graph); + } + + @Override + public Node cloneNode(Graph into) { + return new DummyNode(this.getInputs().asArray(), this.getSuccessors().asArray(), into); + } + + } +}