changeset 2548:3e960f8a6c52

Merge.
author Thomas Wuerthinger <thomas@wuerthinger.net>
date Wed, 27 Apr 2011 21:57:03 +0200
parents b6cd17226aad (current diff) 7a0e1bd2bb64 (diff)
children c12715ca2cc7
files
diffstat 7 files changed, 287 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- /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 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<classpath>
+	<classpathentry kind="src" path="src"/>
+	<classpathentry kind="src" path="test"/>
+	<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.6"/>
+	<classpathentry kind="con" path="org.eclipse.jdt.junit.JUNIT_CONTAINER/4"/>
+	<classpathentry kind="output" path="bin"/>
+</classpath>
--- /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 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<projectDescription>
+	<name>GraalGraph</name>
+	<comment></comment>
+	<projects>
+	</projects>
+	<buildSpec>
+		<buildCommand>
+			<name>org.eclipse.jdt.core.javabuilder</name>
+			<arguments>
+			</arguments>
+		</buildCommand>
+	</buildSpec>
+	<natures>
+		<nature>org.eclipse.jdt.core.javanature</nature>
+	</natures>
+</projectDescription>
--- /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
--- /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<Node> nodes;
+	private int nextId;
+	
+	public Graph() {
+		nodes = new ArrayList<Node>();
+	}
+
+	public synchronized int nextId(Node node) {
+		int id = nextId++;
+		nodes.add(id, node);
+		return id;
+	}
+	
+	public Collection<Node> getNodes(){
+		return Collections.unmodifiableCollection(nodes);
+	}
+	
+	public Node local(Node node) {
+		if(node.getGraph() == this)
+			return node;
+		return node.cloneNode(this);
+	}
+}
--- /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<Node> usages;
+	private final ArrayList<Node> 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<Node>();
+		this.usages = new ArrayList<Node>();
+	}
+	
+	public Node(int inputs, int successors, Graph graph) {
+		this(nullNodes(inputs, graph), nullNodes(successors, graph), graph);
+	}
+	
+	public class NodeArray implements Iterable<Node>{
+		private final Node[] nodes;
+		
+		public NodeArray(Node[] nodes) {
+			this.nodes = nodes;
+		}
+		
+		@Override
+		public Iterator<Node> 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<Node> getPredecessors() {
+		return Collections.unmodifiableCollection(predecessors);
+	}
+	
+	public Collection<Node> 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;
+	}
+}
--- /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);
+	}
+
+}
--- /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);
+		}
+		
+	}
+}