From: <th...@us...> - 2009-04-01 00:08:02
|
Revision: 6143 http://jython.svn.sourceforge.net/jython/?rev=6143&view=rev Author: thobes Date: 2009-04-01 00:07:49 +0000 (Wed, 01 Apr 2009) Log Message: ----------- Checking in the ssa compiler, it's almost starting to be able to do stuff now. Modified Paths: -------------- trunk/sandbox/tobias/compiler/src/org/python/compiler/advanced/sea/NodeSeaGenerator.java trunk/sandbox/tobias/compiler/src/org/python/compiler/advanced/sea/SeaScope.java trunk/sandbox/tobias/compiler/src/org/python/compiler/advanced/sea/ValueCarrier.java trunk/sandbox/tobias/compiler/src/org/python/compiler/sea/GraphBuilder.java trunk/sandbox/tobias/compiler/src/org/python/compiler/sea/StateCarrier.java trunk/sandbox/tobias/compiler/src/org/python/compiler/sea/SuperGraph.java trunk/sandbox/tobias/compiler/src/org/python/compiler/sea/SupergraphVisitor.java trunk/sandbox/tobias/compiler/src/org/thobe/compiler/sea/ArrayNode.java trunk/sandbox/tobias/compiler/src/org/thobe/compiler/sea/ArrayValue.java trunk/sandbox/tobias/compiler/src/org/thobe/compiler/sea/CodeGenerationTraverser.java trunk/sandbox/tobias/compiler/src/org/thobe/compiler/sea/ExceptionValue.java trunk/sandbox/tobias/compiler/src/org/thobe/compiler/sea/Graph.java trunk/sandbox/tobias/compiler/src/org/thobe/compiler/sea/GraphBuilder.java trunk/sandbox/tobias/compiler/src/org/thobe/compiler/sea/GraphTraverser.java trunk/sandbox/tobias/compiler/src/org/thobe/compiler/sea/GraphVisitor.java trunk/sandbox/tobias/compiler/src/org/thobe/compiler/sea/IfNode.java trunk/sandbox/tobias/compiler/src/org/thobe/compiler/sea/InvocationNode.java trunk/sandbox/tobias/compiler/src/org/thobe/compiler/sea/LoadNode.java trunk/sandbox/tobias/compiler/src/org/thobe/compiler/sea/Node.java trunk/sandbox/tobias/compiler/src/org/thobe/compiler/sea/PhiNode.java trunk/sandbox/tobias/compiler/src/org/thobe/compiler/sea/Selection.java trunk/sandbox/tobias/compiler/src/org/thobe/compiler/sea/SerializationTraverser.java trunk/sandbox/tobias/compiler/src/org/thobe/compiler/sea/StoreNode.java trunk/sandbox/tobias/compiler/src/org/thobe/compiler/sea/SwitchNode.java trunk/sandbox/tobias/compiler/src/org/thobe/compiler/sea/ThrowNode.java trunk/sandbox/tobias/compiler/src/org/thobe/compiler/sea/TransformationTraverser.java trunk/sandbox/tobias/compiler/src/org/thobe/compiler/sea/Value.java trunk/sandbox/tobias/compiler/src/org/thobe/compiler/sea/ValueNode.java trunk/sandbox/tobias/compiler/src/org/thobe/compiler/sea/VariableFactory.java trunk/sandbox/tobias/compiler/test/org/python/compiler/sea/output/GraphvizOutput.java Added Paths: ----------- trunk/sandbox/tobias/compiler/src/org/thobe/compiler/sea/GraphReferenceNode.java trunk/sandbox/tobias/compiler/src/org/thobe/compiler/sea/NodeSuccession.java trunk/sandbox/tobias/compiler/src/org/thobe/compiler/sea/ReturnNode.java trunk/sandbox/tobias/compiler/src/org/thobe/compiler/sea/Variable.java trunk/sandbox/tobias/compiler/test/org/python/compiler/sea/output/BuildGraph.java Removed Paths: ------------- trunk/sandbox/tobias/compiler/src/org/python/compiler/sea/Variable.java trunk/sandbox/tobias/compiler/src/org/thobe/compiler/sea/Continuation.java trunk/sandbox/tobias/compiler/src/org/thobe/compiler/sea/OpNode.java Modified: trunk/sandbox/tobias/compiler/src/org/python/compiler/advanced/sea/NodeSeaGenerator.java =================================================================== --- trunk/sandbox/tobias/compiler/src/org/python/compiler/advanced/sea/NodeSeaGenerator.java 2009-03-31 23:53:50 UTC (rev 6142) +++ trunk/sandbox/tobias/compiler/src/org/python/compiler/advanced/sea/NodeSeaGenerator.java 2009-04-01 00:07:49 UTC (rev 6143) @@ -70,9 +70,9 @@ import org.python.compiler.sea.SelectionCallback; import org.python.compiler.sea.StateCarrier; import org.python.compiler.sea.UnrollerCallback; -import org.python.compiler.sea.Variable; import org.thobe.compiler.sea.ExceptionValue; import org.thobe.compiler.sea.Value; +import org.thobe.compiler.sea.Variable; /** * Intermediate code generator that generates a Sea of Nodes type of SSA IR. @@ -112,7 +112,7 @@ public Value loadConstant(Constant token) { final Value constant = constants.get(token); if (constant == null) { - throw new IllegalArgumentException("No such constant: " + token); + throw new IllegalArgumentException("No such constant: " + token + " in " + constants); } return constant; } @@ -408,7 +408,7 @@ @Override public Value visitLambda(Lambda node) throws Exception { linenumber(node); - generate.returnValue(evaluate(node.getInternalBody())); + generate.returnPythonValue(evaluate(node.getInternalBody())); return generate.graph(); } @@ -721,7 +721,7 @@ @Override public Value visitReturn(Return node) throws Exception { linenumber(node); - generate.returnValue(evaluate(node.getInternalValue())); + generate.returnPythonValue(evaluate(node.getInternalValue())); return null; } Modified: trunk/sandbox/tobias/compiler/src/org/python/compiler/advanced/sea/SeaScope.java =================================================================== --- trunk/sandbox/tobias/compiler/src/org/python/compiler/advanced/sea/SeaScope.java 2009-03-31 23:53:50 UTC (rev 6142) +++ trunk/sandbox/tobias/compiler/src/org/python/compiler/advanced/sea/SeaScope.java 2009-04-01 00:07:49 UTC (rev 6143) @@ -120,9 +120,9 @@ }; } - public static SeaScope forFunction(String name, String[] parameters, - String[] locals, String[] globals, final String[] free, - String[] cell, boolean hasStarImport) { + public static SeaScope forFunction(String name, final String[] parameters, + final String[] locals, final String[] globals, final String[] free, + final String[] cell, boolean hasStarImport) { return new SeaScope() { @Override public GraphBuilder builder(SuperGraph ocean, CompilerFlags flags) { @@ -137,8 +137,13 @@ } public void populate(VariableFactory factory) { - // TODO Auto-generated method stub - + for (String var : parameters) { + factory.createParameter(var); + } + for (String var : locals) { + factory.createLocalVariable(var); + } + // TODO: more stuff } @Override @@ -233,7 +238,7 @@ } @Override - public void returnValue(Value value) { + public void returnPythonValue(Value value) { scope.returnValue(this, value); } Modified: trunk/sandbox/tobias/compiler/src/org/python/compiler/advanced/sea/ValueCarrier.java =================================================================== --- trunk/sandbox/tobias/compiler/src/org/python/compiler/advanced/sea/ValueCarrier.java 2009-03-31 23:53:50 UTC (rev 6142) +++ trunk/sandbox/tobias/compiler/src/org/python/compiler/advanced/sea/ValueCarrier.java 2009-04-01 00:07:49 UTC (rev 6143) @@ -1,7 +1,7 @@ package org.python.compiler.advanced.sea; import org.thobe.compiler.sea.Value; -import org.python.compiler.sea.Variable; +import org.thobe.compiler.sea.Variable; abstract class ValueCarrier { private final Variable[] closure; Modified: trunk/sandbox/tobias/compiler/src/org/python/compiler/sea/GraphBuilder.java =================================================================== --- trunk/sandbox/tobias/compiler/src/org/python/compiler/sea/GraphBuilder.java 2009-03-31 23:53:50 UTC (rev 6142) +++ trunk/sandbox/tobias/compiler/src/org/python/compiler/sea/GraphBuilder.java 2009-04-01 00:07:49 UTC (rev 6143) @@ -6,12 +6,13 @@ import org.python.core.CodeFlag; import org.python.core.CompilerFlags; import org.thobe.compiler.sea.ArrayValue; -import org.thobe.compiler.sea.Continuation; +import org.thobe.compiler.sea.NodeSuccession; import org.thobe.compiler.sea.ExceptionValue; import org.thobe.compiler.sea.NamespacePopulator; import org.thobe.compiler.sea.InvocationType; import org.thobe.compiler.sea.Selection; import org.thobe.compiler.sea.Value; +import org.thobe.compiler.sea.Variable; public abstract class GraphBuilder extends org.thobe.compiler.sea.GraphBuilder { private final CallStrategy call; @@ -79,8 +80,7 @@ * @param line the line number to set. */ public final void linenumber(int line) { - // TODO Auto-generated method stub - + // FIXME: implement this somehow } // --- Handle --- @@ -125,7 +125,7 @@ * @return a Python string with the supplied content. */ public Value string(String string) { - // TODO Auto-generated method stub + // TODO: implement this return null; } @@ -155,36 +155,36 @@ // phi verify that each value comes from different paths? // Does it have to? Selection select = something();// TODO: design API like: selection(types.isTrue(), result); - replaceContinuation(select.onTrue()); + replaceSuccession(select.onTrue()); Value on_true = schedule(selection.onTrue()); // schedule artificial node - Continuation after_true = replaceContinuation(select.onFalse()); + NodeSuccession after_true = replaceSuccession(select.onFalse()); Value on_false = schedule(selection.onFalse()); // schedule artificial node - Continuation after_false = currentContinuation(); - replaceContinuation(merge(after_true, after_false)); - return super.phiOrNull(on_true, on_false); + NodeSuccession after_false = currentSuccession(); + replaceSuccession(merge(after_true, after_false)); + return phiOrNull(on_true, on_false); } public void loop(LoopCallback loop) throws Exception { // TODO: this has issues for the same reasons as selection does. - final Continuation start = currentContinuation(); - final Continuation after = newContinuation(); + final NodeSuccession start = currentSuccession(); + final NodeSuccession after = newSuccession(); StateCarrier state = loop.head(); - replaceContinuation(state.onTrue()); + replaceSuccession(state.onTrue()); loop.body(state.payload(), new LoopHandle() { @Override public void breakLoop() { - nextContinuation(after); + nextSuccession(after); } @Override public void continueLoop() { - nextContinuation(start); + nextSuccession(start); } }); - replaceContinuation(state.onFalse()); + replaceSuccession(state.onFalse()); loop.orelse(); - nextContinuation(after); - replaceContinuation(after); + nextSuccession(after); + replaceSuccession(after); } public void tryBlock(BlockCallback block) { @@ -724,7 +724,7 @@ // --- Exit --- - public abstract void returnValue(Value evaluate); + public abstract void returnPythonValue(Value evaluate); public abstract Value yeildValue(Value value); Modified: trunk/sandbox/tobias/compiler/src/org/python/compiler/sea/StateCarrier.java =================================================================== --- trunk/sandbox/tobias/compiler/src/org/python/compiler/sea/StateCarrier.java 2009-03-31 23:53:50 UTC (rev 6142) +++ trunk/sandbox/tobias/compiler/src/org/python/compiler/sea/StateCarrier.java 2009-04-01 00:07:49 UTC (rev 6143) @@ -1,6 +1,6 @@ package org.python.compiler.sea; -import org.thobe.compiler.sea.Continuation; +import org.thobe.compiler.sea.NodeSuccession; import org.thobe.compiler.sea.Value; public class StateCarrier { @@ -15,12 +15,12 @@ return payload; } - Continuation onTrue() { + NodeSuccession onTrue() { // TODO Auto-generated method stub return null; } - Continuation onFalse() { + NodeSuccession onFalse() { // TODO Auto-generated method stub return null; } Modified: trunk/sandbox/tobias/compiler/src/org/python/compiler/sea/SuperGraph.java =================================================================== --- trunk/sandbox/tobias/compiler/src/org/python/compiler/sea/SuperGraph.java 2009-03-31 23:53:50 UTC (rev 6142) +++ trunk/sandbox/tobias/compiler/src/org/python/compiler/sea/SuperGraph.java 2009-04-01 00:07:49 UTC (rev 6143) @@ -7,8 +7,8 @@ import org.thobe.compiler.sea.VariableFactory; public class SuperGraph { - private final Graph constants = null; - private final Graph module = null; + private Graph constants; + private Graph module; public GraphBuilder constants() { // TODO: capture this graph... @@ -28,7 +28,7 @@ } @Override - public void returnValue(Value evaluate) { + public void returnPythonValue(Value evaluate) { throw new UnsupportedOperationException(); } @@ -40,7 +40,8 @@ } public void accept(NodeSorter sort, SupergraphVisitor visitor) { - // TODO Auto-generated method stub - + // TODO: replace this method + constants.serialize(visitor.visitGraph()); + module.serialize(visitor.visitGraph()); } } Modified: trunk/sandbox/tobias/compiler/src/org/python/compiler/sea/SupergraphVisitor.java =================================================================== --- trunk/sandbox/tobias/compiler/src/org/python/compiler/sea/SupergraphVisitor.java 2009-03-31 23:53:50 UTC (rev 6142) +++ trunk/sandbox/tobias/compiler/src/org/python/compiler/sea/SupergraphVisitor.java 2009-04-01 00:07:49 UTC (rev 6143) @@ -1,5 +1,7 @@ package org.python.compiler.sea; +import org.thobe.compiler.sea.GraphVisitor; + public interface SupergraphVisitor { - + GraphVisitor visitGraph(); } Deleted: trunk/sandbox/tobias/compiler/src/org/python/compiler/sea/Variable.java =================================================================== --- trunk/sandbox/tobias/compiler/src/org/python/compiler/sea/Variable.java 2009-03-31 23:53:50 UTC (rev 6142) +++ trunk/sandbox/tobias/compiler/src/org/python/compiler/sea/Variable.java 2009-04-01 00:07:49 UTC (rev 6143) @@ -1,5 +0,0 @@ -package org.python.compiler.sea; - -public class Variable { - -} Modified: trunk/sandbox/tobias/compiler/src/org/thobe/compiler/sea/ArrayNode.java =================================================================== --- trunk/sandbox/tobias/compiler/src/org/thobe/compiler/sea/ArrayNode.java 2009-03-31 23:53:50 UTC (rev 6142) +++ trunk/sandbox/tobias/compiler/src/org/thobe/compiler/sea/ArrayNode.java 2009-04-01 00:07:49 UTC (rev 6143) @@ -1,8 +1,26 @@ package org.thobe.compiler.sea; -public class ArrayNode extends ValueNode { +import java.util.Arrays; + +class ArrayNode extends ValueNode { + private final Value[] content; + ArrayNode(Value[] content) { - // TODO Auto-generated constructor stub + this.content = content; } + @Override + public Value result() { + throw new UnsupportedOperationException("Not implemented"); + } + + @Override + ArrayNode accept(GraphTraverser traverser) { + return traverser.array(this, content); + } + + @Override + public String toString() { + return "Array:[" + Arrays.toString(content) + "]"; + } } Modified: trunk/sandbox/tobias/compiler/src/org/thobe/compiler/sea/ArrayValue.java =================================================================== --- trunk/sandbox/tobias/compiler/src/org/thobe/compiler/sea/ArrayValue.java 2009-03-31 23:53:50 UTC (rev 6142) +++ trunk/sandbox/tobias/compiler/src/org/thobe/compiler/sea/ArrayValue.java 2009-04-01 00:07:49 UTC (rev 6143) @@ -1,7 +1,12 @@ package org.thobe.compiler.sea; public class ArrayValue extends Value { + private final ArrayNode from; + ArrayValue(ArrayNode from) { + this.from = from; + } + public Value[] array(int size) { // TODO Auto-generated method stub return null; @@ -11,4 +16,9 @@ // TODO Auto-generated method stub return null; } + + @Override + public String toString() { + return "value_of(" + from + ")"; + } } Modified: trunk/sandbox/tobias/compiler/src/org/thobe/compiler/sea/CodeGenerationTraverser.java =================================================================== --- trunk/sandbox/tobias/compiler/src/org/thobe/compiler/sea/CodeGenerationTraverser.java 2009-03-31 23:53:50 UTC (rev 6142) +++ trunk/sandbox/tobias/compiler/src/org/thobe/compiler/sea/CodeGenerationTraverser.java 2009-04-01 00:07:49 UTC (rev 6143) @@ -1,6 +1,6 @@ package org.thobe.compiler.sea; -class CodeGenerationTraverser extends GraphTraverser { +abstract class CodeGenerationTraverser extends GraphTraverser { public CodeGenerationTraverser(GraphVisitor visitor) { } Deleted: trunk/sandbox/tobias/compiler/src/org/thobe/compiler/sea/Continuation.java =================================================================== --- trunk/sandbox/tobias/compiler/src/org/thobe/compiler/sea/Continuation.java 2009-03-31 23:53:50 UTC (rev 6142) +++ trunk/sandbox/tobias/compiler/src/org/thobe/compiler/sea/Continuation.java 2009-04-01 00:07:49 UTC (rev 6143) @@ -1,5 +0,0 @@ -package org.thobe.compiler.sea; - -public class Continuation { - -} Modified: trunk/sandbox/tobias/compiler/src/org/thobe/compiler/sea/ExceptionValue.java =================================================================== --- trunk/sandbox/tobias/compiler/src/org/thobe/compiler/sea/ExceptionValue.java 2009-03-31 23:53:50 UTC (rev 6142) +++ trunk/sandbox/tobias/compiler/src/org/thobe/compiler/sea/ExceptionValue.java 2009-04-01 00:07:49 UTC (rev 6143) @@ -1,5 +1,13 @@ package org.thobe.compiler.sea; public class ExceptionValue extends Value { + ExceptionValue() { + throw new UnsupportedOperationException( + "How should exceptions be created?"); + } + @Override + public String toString() { + return "ExceptionValue[how did you get this?]"; + } } Modified: trunk/sandbox/tobias/compiler/src/org/thobe/compiler/sea/Graph.java =================================================================== --- trunk/sandbox/tobias/compiler/src/org/thobe/compiler/sea/Graph.java 2009-03-31 23:53:50 UTC (rev 6142) +++ trunk/sandbox/tobias/compiler/src/org/thobe/compiler/sea/Graph.java 2009-04-01 00:07:49 UTC (rev 6143) @@ -1,18 +1,92 @@ package org.thobe.compiler.sea; public final class Graph { - private Node entry; + private final Node entry; - public void compile(GraphVisitor visitor) { - entry.accept(new CodeGenerationTraverser(visitor)); + Graph(Node entry) { + this.entry = entry; } - public void transform(GraphVisitor visitor) { - entry.accept(new TransformationTraverser(visitor)); + public void serialize(final GraphVisitor visitor) { + entry.accept(new GraphTraverser() { + @Override + ArrayNode array(ArrayNode original, Value[] content) { + visitor.node(original); + return original; + } + + @Override + InvocationNode invoke(InvocationNode original, + InvocationType invocation, Value[] arguments) { + visitor.node(original); + return original; + } + + @Override + LoadNode load(LoadNode original, Variable variable) { + visitor.node(original); + return original; + } + + @Override + PhiNode phi(PhiNode original, Node[] array) { + visitor.node(original); + return original; + } + + @Override + ThrowNode raiseException(ThrowNode original, + ExceptionValue exception) { + visitor.node(original); + return original; + } + + @Override + ReturnNode returnValue(ReturnNode original, Value value) { + visitor.node(original); + return original; + } + + @Override + SelectTraverser select(final SwitchNode original, Value select) { + visitor.node(original); + return new SelectTraverser() { + @Override + void onCase(Integer key, Node node) { + // TODO Auto-generated method stub + + } + + @Override + void otherwise(Node node) { + // TODO Auto-generated method stub + + } + + @Override + SwitchNode done() { + return original; + } + }; + } + + @Override + IfNode selection(IfNode original, Value predicate, + Node trueSuccessor, Node falseSuccessor) { + visitor.node(original); + return original; + } + + @Override + StoreNode store(StoreNode original, Variable variable, Value value) { + visitor.node(original); + return original; + } + }); } public byte[] serialize() { - SerializationTraverser traverser = new SerializationTraverser(); + SerializationTraverser traverser = null;//new SerializationTraverser(); entry.accept(traverser); return traverser.serialization(); } Modified: trunk/sandbox/tobias/compiler/src/org/thobe/compiler/sea/GraphBuilder.java =================================================================== --- trunk/sandbox/tobias/compiler/src/org/thobe/compiler/sea/GraphBuilder.java 2009-03-31 23:53:50 UTC (rev 6142) +++ trunk/sandbox/tobias/compiler/src/org/thobe/compiler/sea/GraphBuilder.java 2009-04-01 00:07:49 UTC (rev 6143) @@ -1,54 +1,71 @@ package org.thobe.compiler.sea; -import org.python.compiler.sea.Variable; +public abstract class GraphBuilder { + private final GraphReferenceNode entry; + private NodeSuccession succession; -public abstract class GraphBuilder { protected GraphBuilder(NamespacePopulator populator) { - populator.populate(new VariableFactory(){}); + populator.populate(new VariableFactory() { + }); + entry = new GraphReferenceNode(); + succession = entry.succession(); } - protected InvocationNode invoke(InvocationType invocation, + public final Graph build() { + return new Graph(entry); + } + + protected ValueNode invoke(InvocationType invocation, Value... arguments) { return new InvocationNode(invocation, arguments); } - protected ArrayNode array(Value[] content) { + protected ValueNode array(Value[] content) { return new ArrayNode(content); } protected <N extends Node> N schedule(N node) { - // TODO Auto-generated method stub + succession = succession.setNext(node); return node; } protected <V extends Value> V schedule(V value) { - // TODO Auto-generated method stub + schedule(ValueNode.repeat(value)); return value; } - protected Continuation replaceContinuation(Continuation progression) { - // TODO Auto-generated method stub - return null; + protected NodeSuccession replaceSuccession(NodeSuccession succession) { + try { + return this.succession; + } finally { + this.succession = succession; + } } - protected Continuation currentContinuation() { - // TODO Auto-generated method stub - return null; + protected NodeSuccession currentSuccession() { + return succession; } - protected void nextContinuation(Continuation start) { + protected void nextSuccession(NodeSuccession start) { // TODO Auto-generated method stub } - protected Continuation newContinuation() { + protected NodeSuccession newSuccession() { // TODO Auto-generated method stub return null; } - protected Continuation merge(Continuation... progressions) { - // TODO Auto-generated method stub - return null; + protected NodeSuccession merge(final NodeSuccession... previous) { + return new NodeSuccession(){ + @Override + NodeSuccession setNext(Node node) { + for (NodeSuccession prev : previous) { + prev.setNext(node); + } + return node.succession(); + } + }; } protected Value phi(Value... values) { @@ -79,6 +96,10 @@ } } + protected final Node returnValue(Value value) { + return new ReturnNode(value); + } + protected final ValueNode loadVariable(Variable variable) { return new LoadNode(variable); } Added: trunk/sandbox/tobias/compiler/src/org/thobe/compiler/sea/GraphReferenceNode.java =================================================================== --- trunk/sandbox/tobias/compiler/src/org/thobe/compiler/sea/GraphReferenceNode.java (rev 0) +++ trunk/sandbox/tobias/compiler/src/org/thobe/compiler/sea/GraphReferenceNode.java 2009-04-01 00:07:49 UTC (rev 6143) @@ -0,0 +1,26 @@ +package org.thobe.compiler.sea; + +class GraphReferenceNode extends Node { + Node start; + + @Override + Node accept(GraphTraverser traverser) { + return start.accept(traverser); + } + + @Override + NodeSuccession succession() { + return new NodeSuccession() { + @Override + NodeSuccession setNext(Node node) { + start = node; + return node.succession(); + } + }; + } + + @Override + public String toString() { + return start != null ? start.toString() : "NullGraph"; + } +} Modified: trunk/sandbox/tobias/compiler/src/org/thobe/compiler/sea/GraphTraverser.java =================================================================== --- trunk/sandbox/tobias/compiler/src/org/thobe/compiler/sea/GraphTraverser.java 2009-03-31 23:53:50 UTC (rev 6142) +++ trunk/sandbox/tobias/compiler/src/org/thobe/compiler/sea/GraphTraverser.java 2009-04-01 00:07:49 UTC (rev 6143) @@ -1,5 +1,32 @@ package org.thobe.compiler.sea; -class GraphTraverser { +abstract class GraphTraverser { + abstract class SelectTraverser { + abstract void onCase(Integer key, Node node); + abstract void otherwise(Node node); + + abstract SwitchNode done(); + } + + abstract SelectTraverser select(SwitchNode original, Value select); + + abstract IfNode selection(IfNode original, Value predicate, + Node trueSuccessor, Node falseSuccessor); + + abstract ReturnNode returnValue(ReturnNode original, Value value); + + abstract StoreNode store(StoreNode original, Variable variable, Value value); + + abstract ThrowNode raiseException(ThrowNode original, + ExceptionValue exception); + + abstract InvocationNode invoke(InvocationNode original, + InvocationType invocation, Value[] arguments); + + abstract LoadNode load(LoadNode original, Variable variable); + + abstract PhiNode phi(PhiNode original, Node[] array); + + abstract ArrayNode array(ArrayNode original, Value[] content); } Modified: trunk/sandbox/tobias/compiler/src/org/thobe/compiler/sea/GraphVisitor.java =================================================================== --- trunk/sandbox/tobias/compiler/src/org/thobe/compiler/sea/GraphVisitor.java 2009-03-31 23:53:50 UTC (rev 6142) +++ trunk/sandbox/tobias/compiler/src/org/thobe/compiler/sea/GraphVisitor.java 2009-04-01 00:07:49 UTC (rev 6143) @@ -1,20 +1,21 @@ package org.thobe.compiler.sea; public interface GraphVisitor { + void node(Node node); /** * Allocate a location to store a local variable. * * @param name The name of the local variable. * @return a new location for storing the local variable. */ - VariableLocation allocateLocal(String name, ValueType type); + //VariableLocation allocateLocal(String name, ValueType type); /** * Allocate a location to store a temporary variable. * * @return a new location for storing the temporary variable. */ - VariableLocation allocateTemporary(); + //VariableLocation allocateTemporary(); - void deallocate(VariableLocation location); + //void deallocate(VariableLocation location); } Modified: trunk/sandbox/tobias/compiler/src/org/thobe/compiler/sea/IfNode.java =================================================================== --- trunk/sandbox/tobias/compiler/src/org/thobe/compiler/sea/IfNode.java 2009-03-31 23:53:50 UTC (rev 6142) +++ trunk/sandbox/tobias/compiler/src/org/thobe/compiler/sea/IfNode.java 2009-04-01 00:07:49 UTC (rev 6143) @@ -3,13 +3,34 @@ class IfNode extends Node { private Node trueSuccessor; private Node falseSuccessor; + private final Value predicate; IfNode(Value predicate) { + this.predicate = predicate; } void setOnTrue(Node next) { + trueSuccessor = next; } void setOnFalse(Node next) { + falseSuccessor = next; } + + @Override + IfNode accept(GraphTraverser traverser) { + return traverser.selection(this, predicate, trueSuccessor, + falseSuccessor); + } + + @Override + NodeSuccession succession() { + return NodeSuccession.DUMMY; + } + + @Override + public String toString() { + return "IfNode[" + predicate + " ? " + trueSuccessor + " : " + + falseSuccessor + "]"; + } } Modified: trunk/sandbox/tobias/compiler/src/org/thobe/compiler/sea/InvocationNode.java =================================================================== --- trunk/sandbox/tobias/compiler/src/org/thobe/compiler/sea/InvocationNode.java 2009-03-31 23:53:50 UTC (rev 6142) +++ trunk/sandbox/tobias/compiler/src/org/thobe/compiler/sea/InvocationNode.java 2009-04-01 00:07:49 UTC (rev 6143) @@ -1,9 +1,40 @@ package org.thobe.compiler.sea; -public class InvocationNode extends ValueNode { +class InvocationNode extends ValueNode { + private final InvocationType invocation; + private final Value[] arguments; + private final Value result = new Value() { + @Override + public String toString() { + return "value_of(" + InvocationNode.this + ")"; + } + }; InvocationNode(InvocationType invocation, Value[] arguments) { - // TODO Auto-generated constructor stub + this.invocation = invocation; + this.arguments = arguments; } + @Override + public Value result() { + return result; + } + + @Override + InvocationNode accept(GraphTraverser traverser) { + return traverser.invoke(this, invocation, arguments); + } + + @Override + public String toString() { + StringBuilder args = new StringBuilder(invocation.toString()); + String sep = "("; + for (Value arg : arguments) { + args.append(sep); + args.append(arg.toString()); + sep = ", "; + } + args.append(")"); + return "InvocationNode[" + args + "]"; + } } Modified: trunk/sandbox/tobias/compiler/src/org/thobe/compiler/sea/LoadNode.java =================================================================== --- trunk/sandbox/tobias/compiler/src/org/thobe/compiler/sea/LoadNode.java 2009-03-31 23:53:50 UTC (rev 6142) +++ trunk/sandbox/tobias/compiler/src/org/thobe/compiler/sea/LoadNode.java 2009-04-01 00:07:49 UTC (rev 6143) @@ -1,11 +1,32 @@ package org.thobe.compiler.sea; -import org.python.compiler.sea.Variable; +class LoadNode extends ValueNode { + private final Variable variable; -public class LoadNode extends ValueNode { LoadNode(Variable variable) { - // TODO Auto-generated constructor stub + this.variable = variable; } private StoreNode defined; + private final Value result = new Value() { + @Override + public String toString() { + return "value_of(" + LoadNode.this + ")"; + } + }; + + @Override + public Value result() { + return result; + } + + @Override + LoadNode accept(GraphTraverser traverser) { + return traverser.load(this, variable); + } + + @Override + public String toString() { + return "LoadNode[" + variable + "]"; + } } Modified: trunk/sandbox/tobias/compiler/src/org/thobe/compiler/sea/Node.java =================================================================== --- trunk/sandbox/tobias/compiler/src/org/thobe/compiler/sea/Node.java 2009-03-31 23:53:50 UTC (rev 6142) +++ trunk/sandbox/tobias/compiler/src/org/thobe/compiler/sea/Node.java 2009-04-01 00:07:49 UTC (rev 6143) @@ -1,10 +1,8 @@ package org.thobe.compiler.sea; public abstract class Node { - - public void accept(GraphTraverser serializationTraverser) { - // TODO Auto-generated method stub - - } - + abstract Node accept(GraphTraverser traverser); + abstract NodeSuccession succession(); + @Override + abstract public String toString(); } Copied: trunk/sandbox/tobias/compiler/src/org/thobe/compiler/sea/NodeSuccession.java (from rev 6138, trunk/sandbox/tobias/compiler/src/org/thobe/compiler/sea/Continuation.java) =================================================================== --- trunk/sandbox/tobias/compiler/src/org/thobe/compiler/sea/NodeSuccession.java (rev 0) +++ trunk/sandbox/tobias/compiler/src/org/thobe/compiler/sea/NodeSuccession.java 2009-04-01 00:07:49 UTC (rev 6143) @@ -0,0 +1,19 @@ +package org.thobe.compiler.sea; + +public abstract class NodeSuccession { + public static final NodeSuccession DUMMY = new NodeSuccession() { + @Override + NodeSuccession setNext(Node node) { + throw new IllegalStateException( + "Trying to set next node for node with no single succession."); + } + }; + public static final NodeSuccession NOTHING = new NodeSuccession() { + @Override + NodeSuccession setNext(Node node) { + return this; + } + }; + + abstract NodeSuccession setNext(Node node); +} Deleted: trunk/sandbox/tobias/compiler/src/org/thobe/compiler/sea/OpNode.java =================================================================== --- trunk/sandbox/tobias/compiler/src/org/thobe/compiler/sea/OpNode.java 2009-03-31 23:53:50 UTC (rev 6142) +++ trunk/sandbox/tobias/compiler/src/org/thobe/compiler/sea/OpNode.java 2009-04-01 00:07:49 UTC (rev 6143) @@ -1,5 +0,0 @@ -package org.thobe.compiler.sea; - -class OpNode extends Node { - Value[] parameters; -} Modified: trunk/sandbox/tobias/compiler/src/org/thobe/compiler/sea/PhiNode.java =================================================================== --- trunk/sandbox/tobias/compiler/src/org/thobe/compiler/sea/PhiNode.java 2009-03-31 23:53:50 UTC (rev 6142) +++ trunk/sandbox/tobias/compiler/src/org/thobe/compiler/sea/PhiNode.java 2009-04-01 00:07:49 UTC (rev 6143) @@ -6,7 +6,39 @@ PhiNode(Value[] values) { // TODO Auto-generated constructor stub } + private List<Node> components; private final Value result = new Value() { + @Override + public String toString() { + return "phi(" + parts() + ")"; + } }; + + @Override + public Value result() { + return result; + } + + @Override + PhiNode accept(GraphTraverser traverser) { + return traverser.phi(this, + components.toArray(new Node[components.size()])); + } + + private StringBuilder parts() { + StringBuilder parts = new StringBuilder(); + String sep = ""; + for (Node part : components) { + parts.append(sep); + parts.append(part); + sep = ", "; + } + return parts; + } + + @Override + public String toString() { + return "PhiNode[" + parts() + "]"; + } } Added: trunk/sandbox/tobias/compiler/src/org/thobe/compiler/sea/ReturnNode.java =================================================================== --- trunk/sandbox/tobias/compiler/src/org/thobe/compiler/sea/ReturnNode.java (rev 0) +++ trunk/sandbox/tobias/compiler/src/org/thobe/compiler/sea/ReturnNode.java 2009-04-01 00:07:49 UTC (rev 6143) @@ -0,0 +1,24 @@ +package org.thobe.compiler.sea; + +class ReturnNode extends Node { + private final Value value; + + ReturnNode(Value value) { + this.value = value; + } + + @Override + ReturnNode accept(GraphTraverser traverser) { + return traverser.returnValue(this, value); + } + + @Override + NodeSuccession succession() { + return NodeSuccession.NOTHING; + } + + @Override + public String toString() { + return "ReturnNode[" + value + "]"; + } +} Modified: trunk/sandbox/tobias/compiler/src/org/thobe/compiler/sea/Selection.java =================================================================== --- trunk/sandbox/tobias/compiler/src/org/thobe/compiler/sea/Selection.java 2009-03-31 23:53:50 UTC (rev 6142) +++ trunk/sandbox/tobias/compiler/src/org/thobe/compiler/sea/Selection.java 2009-04-01 00:07:49 UTC (rev 6143) @@ -2,12 +2,12 @@ public class Selection { - public Continuation onTrue() { + public NodeSuccession onTrue() { // TODO Auto-generated method stub return null; } - public Continuation onFalse() { + public NodeSuccession onFalse() { // TODO Auto-generated method stub return null; } Modified: trunk/sandbox/tobias/compiler/src/org/thobe/compiler/sea/SerializationTraverser.java =================================================================== --- trunk/sandbox/tobias/compiler/src/org/thobe/compiler/sea/SerializationTraverser.java 2009-03-31 23:53:50 UTC (rev 6142) +++ trunk/sandbox/tobias/compiler/src/org/thobe/compiler/sea/SerializationTraverser.java 2009-04-01 00:07:49 UTC (rev 6143) @@ -1,6 +1,6 @@ package org.thobe.compiler.sea; -class SerializationTraverser extends GraphTraverser { +abstract class SerializationTraverser extends GraphTraverser { public SerializationTraverser() { throw new UnsupportedOperationException( "Graph serialization is not implemented."); Modified: trunk/sandbox/tobias/compiler/src/org/thobe/compiler/sea/StoreNode.java =================================================================== --- trunk/sandbox/tobias/compiler/src/org/thobe/compiler/sea/StoreNode.java 2009-03-31 23:53:50 UTC (rev 6142) +++ trunk/sandbox/tobias/compiler/src/org/thobe/compiler/sea/StoreNode.java 2009-04-01 00:07:49 UTC (rev 6143) @@ -1,10 +1,33 @@ package org.thobe.compiler.sea; -import org.python.compiler.sea.Variable; +class StoreNode extends Node { + private Node next; + private final Variable variable; + private final Value value; -public class StoreNode extends OpNode { - StoreNode(Variable variable, Value value) { - // TODO Auto-generated constructor stub + this.variable = variable; + this.value = value; } + + @Override + StoreNode accept(GraphTraverser traverser) { + return traverser.store(this, variable, value); + } + + @Override + NodeSuccession succession() { + return new NodeSuccession() { + @Override + NodeSuccession setNext(Node node) { + next = node; + return node.succession(); + } + }; + } + + @Override + public String toString() { + return "StoreNode[" + variable + " <- " + value + "]"; + } } Modified: trunk/sandbox/tobias/compiler/src/org/thobe/compiler/sea/SwitchNode.java =================================================================== --- trunk/sandbox/tobias/compiler/src/org/thobe/compiler/sea/SwitchNode.java 2009-03-31 23:53:50 UTC (rev 6142) +++ trunk/sandbox/tobias/compiler/src/org/thobe/compiler/sea/SwitchNode.java 2009-04-01 00:07:49 UTC (rev 6143) @@ -1,13 +1,50 @@ package org.thobe.compiler.sea; import java.util.Map; +import java.util.SortedMap; +import java.util.TreeMap; class SwitchNode extends Node { private Node defaultSuccessor; - private Map<Integer, Node> successors; + private final SortedMap<Integer, Node> successors = new TreeMap<Integer, Node>(); + private final Value select; + SwitchNode(Value select) { + this.select = select; } + void addSuccessor(int on, Node next) { successors.put(on, next); } + + @Override + SwitchNode accept(GraphTraverser traverser) { + GraphTraverser.SelectTraverser selector = traverser.select(this, select); + for (Map.Entry<Integer, Node> entry : successors.entrySet()) { + selector.onCase(entry.getKey(), entry.getValue()); + } + if (defaultSuccessor != null) { + selector.otherwise(defaultSuccessor); + } + return selector.done(); + } + + @Override + NodeSuccession succession() { + return NodeSuccession.DUMMY; + } + + @Override + public String toString() { + StringBuilder targets = new StringBuilder(); + String sep = ": "; + for (Map.Entry<Integer, Node> entry : successors.entrySet()) { + targets.append(sep); + targets.append(entry.getKey().toString()); + targets.append(" -> "); + targets.append(entry.getValue().toString()); + sep = ", "; + } + return "SwitchNode[" + targets + "]"; + } } Modified: trunk/sandbox/tobias/compiler/src/org/thobe/compiler/sea/ThrowNode.java =================================================================== --- trunk/sandbox/tobias/compiler/src/org/thobe/compiler/sea/ThrowNode.java 2009-03-31 23:53:50 UTC (rev 6142) +++ trunk/sandbox/tobias/compiler/src/org/thobe/compiler/sea/ThrowNode.java 2009-04-01 00:07:49 UTC (rev 6143) @@ -1,9 +1,24 @@ package org.thobe.compiler.sea; -public class ThrowNode extends Node { +class ThrowNode extends Node { + private final ExceptionValue exception; ThrowNode(ExceptionValue exception) { - // TODO Auto-generated constructor stub + this.exception = exception; } + @Override + ThrowNode accept(GraphTraverser traverser) { + return traverser.raiseException(this, exception); + } + + @Override + NodeSuccession succession() { + return NodeSuccession.DUMMY; + } + + @Override + public String toString() { + return "ThrowNode[" + exception + "]"; + } } Modified: trunk/sandbox/tobias/compiler/src/org/thobe/compiler/sea/TransformationTraverser.java =================================================================== --- trunk/sandbox/tobias/compiler/src/org/thobe/compiler/sea/TransformationTraverser.java 2009-03-31 23:53:50 UTC (rev 6142) +++ trunk/sandbox/tobias/compiler/src/org/thobe/compiler/sea/TransformationTraverser.java 2009-04-01 00:07:49 UTC (rev 6143) @@ -1,6 +1,6 @@ package org.thobe.compiler.sea; -class TransformationTraverser extends GraphTraverser { +abstract class TransformationTraverser extends GraphTraverser { public TransformationTraverser(GraphVisitor visitor) { // TODO Auto-generated constructor stub Modified: trunk/sandbox/tobias/compiler/src/org/thobe/compiler/sea/Value.java =================================================================== --- trunk/sandbox/tobias/compiler/src/org/thobe/compiler/sea/Value.java 2009-03-31 23:53:50 UTC (rev 6142) +++ trunk/sandbox/tobias/compiler/src/org/thobe/compiler/sea/Value.java 2009-04-01 00:07:49 UTC (rev 6143) @@ -1,7 +1,9 @@ package org.thobe.compiler.sea; +public abstract class Value { + Value() { + } -public class Value { public static Value integer(int integer) { // TODO Auto-generated method stub return null; @@ -12,4 +14,6 @@ return null; } + @Override + public abstract String toString(); } Modified: trunk/sandbox/tobias/compiler/src/org/thobe/compiler/sea/ValueNode.java =================================================================== --- trunk/sandbox/tobias/compiler/src/org/thobe/compiler/sea/ValueNode.java 2009-03-31 23:53:50 UTC (rev 6142) +++ trunk/sandbox/tobias/compiler/src/org/thobe/compiler/sea/ValueNode.java 2009-04-01 00:07:49 UTC (rev 6143) @@ -1,12 +1,44 @@ package org.thobe.compiler.sea; -public class ValueNode extends OpNode { +public abstract class ValueNode extends Node { + static ValueNode repeat(final Value value) { + return new ValueNode() { + @Override + public Value result() { + return value; + } + + @Override + ValueNode accept(GraphTraverser traverser) { + throw new UnsupportedOperationException("Needs fixing!"); + } + + @Override + public String toString() { + return "RepeatedValueNode[" + value + "]"; + } + }; + } + + private Node next; + ValueNode() { // TODO Auto-generated constructor stub } - public Value result() { - // TODO Auto-generated method stub - return null; + public abstract Value result(); + + @Override + abstract ValueNode accept(GraphTraverser traverser); + + @Override + NodeSuccession succession() { + return new NodeSuccession() { + @Override + NodeSuccession setNext(Node node) { + next = node; + return node.succession(); + } + }; } } Copied: trunk/sandbox/tobias/compiler/src/org/thobe/compiler/sea/Variable.java (from rev 6138, trunk/sandbox/tobias/compiler/src/org/python/compiler/sea/Variable.java) =================================================================== --- trunk/sandbox/tobias/compiler/src/org/thobe/compiler/sea/Variable.java (rev 0) +++ trunk/sandbox/tobias/compiler/src/org/thobe/compiler/sea/Variable.java 2009-04-01 00:07:49 UTC (rev 6143) @@ -0,0 +1,5 @@ +package org.thobe.compiler.sea; + +public class Variable { + +} Modified: trunk/sandbox/tobias/compiler/src/org/thobe/compiler/sea/VariableFactory.java =================================================================== --- trunk/sandbox/tobias/compiler/src/org/thobe/compiler/sea/VariableFactory.java 2009-03-31 23:53:50 UTC (rev 6142) +++ trunk/sandbox/tobias/compiler/src/org/thobe/compiler/sea/VariableFactory.java 2009-04-01 00:07:49 UTC (rev 6143) @@ -12,4 +12,9 @@ } + public void createParameter(String param) { + // TODO Auto-generated method stub + + } + } Added: trunk/sandbox/tobias/compiler/test/org/python/compiler/sea/output/BuildGraph.java =================================================================== --- trunk/sandbox/tobias/compiler/test/org/python/compiler/sea/output/BuildGraph.java (rev 0) +++ trunk/sandbox/tobias/compiler/test/org/python/compiler/sea/output/BuildGraph.java 2009-04-01 00:07:49 UTC (rev 6143) @@ -0,0 +1,34 @@ +package org.python.compiler.sea.output; + +import org.python.compiler.sea.PythonOperation; +import org.thobe.compiler.sea.GraphBuilder; +import org.thobe.compiler.sea.GraphVisitor; +import org.thobe.compiler.sea.NamespacePopulator; +import org.thobe.compiler.sea.Node; +import org.thobe.compiler.sea.ValueNode; +import org.thobe.compiler.sea.VariableFactory; + +public final class BuildGraph extends GraphBuilder { + protected BuildGraph(NamespacePopulator populator) { + super(populator); + } + + public static void main(String[] args) { + BuildGraph builder = new BuildGraph(new NamespacePopulator() { + public void populate(VariableFactory factory) { + factory.createLocalVariable("x"); + factory.createLocalVariable("y"); + } + }); + ValueNode load_x = builder.loadVariable(builder.variable("x")); + ValueNode load_y = builder.loadVariable(builder.variable("y")); + ValueNode add = builder.schedule(builder.invoke( + PythonOperation.BINARY_ADD, load_x.result(), load_y.result())); + builder.schedule(builder.returnValue(add.result())); + builder.build().serialize(new GraphVisitor() { + public void node(Node node) { + System.out.println(node); + } + }); + } +} Modified: trunk/sandbox/tobias/compiler/test/org/python/compiler/sea/output/GraphvizOutput.java =================================================================== --- trunk/sandbox/tobias/compiler/test/org/python/compiler/sea/output/GraphvizOutput.java 2009-03-31 23:53:50 UTC (rev 6142) +++ trunk/sandbox/tobias/compiler/test/org/python/compiler/sea/output/GraphvizOutput.java 2009-04-01 00:07:49 UTC (rev 6143) @@ -8,16 +8,30 @@ import org.python.core.CompileMode; import org.python.core.CompilerFlags; import org.python.core.ParserFacade; +import org.thobe.compiler.sea.GraphVisitor; +import org.thobe.compiler.sea.Node; public class GraphvizOutput { public static void print(CompileMode mode, String source) throws Exception { CompilerFlags flags = new CompilerFlags(); mod ast = ParserFacade.parse(source, mode, GraphvizOutput.class.getName(), flags); + print(ast, flags); + } + + private static void print(mod ast, CompilerFlags flags) throws Exception { NodeSeaBundle bundle = new NodeSeaBundle(); CompilerDirector.compile(bundle, new Preferences(), flags, true, false, ast); bundle.accept(new SupergraphVisitor() { + public GraphVisitor visitGraph() { + System.out.println("graph:"); + return new GraphVisitor() { + public void node(Node node) { + System.out.println(" " + node); + } + }; + } }); } This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |