# HG changeset patch # User Andreas Woess # Date 1379186316 -7200 # Node ID 126e20d365636d58d8d5a184a9c57cbaf2e0d8cd # Parent 136eaa90ef41405833eace0304ab33a29cd4f1d4 Truffle IGV printer: add edge labels. diff -r 136eaa90ef41 -r 126e20d36563 graal/com.oracle.truffle.api/src/com/oracle/truffle/api/nodes/GraphPrintVisitor.java --- a/graal/com.oracle.truffle.api/src/com/oracle/truffle/api/nodes/GraphPrintVisitor.java Sat Sep 14 20:49:30 2013 +0200 +++ b/graal/com.oracle.truffle.api/src/com/oracle/truffle/api/nodes/GraphPrintVisitor.java Sat Sep 14 21:18:36 2013 +0200 @@ -218,8 +218,10 @@ } } setNodeProperty(node, "class", node.getClass().getSimpleName()); - readNodeProperties((Node) node); - copyDebugProperties(node); + if (node instanceof Node) { + readNodeProperties((Node) node); + copyDebugProperties((Node) node); + } } } @@ -252,12 +254,10 @@ propElem.setTextContent(String.valueOf(value)); } - private void copyDebugProperties(Object node) { - if (node instanceof Node) { - Map debugProperties = ((Node) node).getDebugProperties(); - for (Map.Entry property : debugProperties.entrySet()) { - setNodeProperty(node, property.getKey(), property.getValue()); - } + private void copyDebugProperties(Node node) { + Map debugProperties = node.getDebugProperties(); + for (Map.Entry property : debugProperties.entrySet()) { + setNodeProperty(node, property.getKey(), property.getValue()); } } @@ -274,7 +274,7 @@ } } - protected void connectNodes(Object a, Object b) { + protected void connectNodes(Object a, Object b, String label) { if (nodeMap.get(a) == null || nodeMap.get(b) == null) { return; } @@ -294,6 +294,9 @@ edgeElem.setAttribute("from", fromId); edgeElem.setAttribute("to", toId); edgeElem.setAttribute("index", String.valueOf(count)); + if (label != null) { + edgeElem.setAttribute("label", label); + } edgesElement.appendChild(edgeElem); edgeList.add(edgeElem); } @@ -304,45 +307,60 @@ } // if node is visited once again, skip - if (getElementByObject(node) == null || NodeUtil.findAnnotation(node.getClass(), GraphDuplicate.class) != null) { - visitAny(node); + if (getElementByObject(node) != null && NodeUtil.findAnnotation(node.getClass(), GraphDuplicate.class) == null) { + return this; + } + + // respect node's custom handler + if (NodeUtil.findAnnotation(node.getClass(), CustomGraphPrintHandler.class) != null) { + Class customHandlerClass = NodeUtil.findAnnotation(node.getClass(), CustomGraphPrintHandler.class).handler(); + try { + GraphPrintHandler customHandler = customHandlerClass.newInstance(); + customHandler.visit(node, new GraphPrintAdapter()); + } catch (InstantiationException | IllegalAccessException e) { + assert false : e; + } + } else if (NodeUtil.findAnnotation(node.getClass(), NullGraphPrintHandler.class) != null) { + // ignore + } else { + // default handler + createElementForNode(node); + + if (node instanceof Node) { + for (Map.Entry child : findNamedNodeChildren((Node) node).entrySet()) { + visit(child.getValue()); + connectNodes(node, child.getValue(), child.getKey()); + } + } } return this; } - private void visitAny(Object node) { - // respect node's custom handler - if (NodeUtil.findAnnotation(node.getClass(), NullGraphPrintHandler.class) != null) { - return; - } - if (NodeUtil.findAnnotation(node.getClass(), CustomGraphPrintHandler.class) != null) { - Class gpHandlerClass = NodeUtil.findAnnotation(node.getClass(), CustomGraphPrintHandler.class).handler(); - try { - GraphPrintHandler gpHandler = gpHandlerClass.newInstance(); - gpHandler.visit(node, new GraphPrintAdapter()); - } catch (InstantiationException e) { - assert false; - } catch (IllegalAccessException e) { - assert false; + private static LinkedHashMap findNamedNodeChildren(Node node) { + LinkedHashMap nodes = new LinkedHashMap<>(); + NodeClass nodeClass = NodeClass.get(node.getClass()); + + for (NodeField field : nodeClass.getFields()) { + NodeFieldKind kind = field.getKind(); + if (kind == NodeFieldKind.CHILD || kind == NodeFieldKind.CHILDREN) { + Object value = field.loadValue(node); + if (value != null) { + if (kind == NodeFieldKind.CHILD) { + nodes.put(field.getName(), (Node) value); + } else if (kind == NodeFieldKind.CHILDREN) { + Object[] children = (Object[]) value; + for (int i = 0; i < children.length; i++) { + if (children[i] != null) { + nodes.put(field.getName() + "[" + i + "]", (Node) children[i]); + } + } + } + } } - return; } - // default handler - createElementForNode(node); - - List children = NodeUtil.findNodeChildren((Node) node); - for (Object child : children) { - if (child == null) { - continue; - } else if (child instanceof Node) { - visit(child); - } else { - continue; - } - connectNodes(node, child); - } + return nodes; } public class GraphPrintAdapter { @@ -356,7 +374,7 @@ } public void connectNodes(Object node, Object child) { - GraphPrintVisitor.this.connectNodes(node, child); + GraphPrintVisitor.this.connectNodes(node, child, null); } public void setNodeProperty(Object node, String propertyName, Object value) {