Mercurial > hg > graal-compiler
comparison graal/com.oracle.truffle.api/src/com/oracle/truffle/api/nodes/NodeUtil.java @ 11848:099af41815ea
Truffle: add NodeUtil.printSourceAttributionTree() for debugging
author | Michael Van De Vanter <michael.van.de.vanter@oracle.com> |
---|---|
date | Mon, 30 Sep 2013 20:47:41 -0700 |
parents | c7769440afd8 |
children | e68922869732 |
comparison
equal
deleted
inserted
replaced
11843:372bacc13022 | 11848:099af41815ea |
---|---|
1 /* | 1 /* |
2 * Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved. | 2 * Copyright (c) 2012, 2013, Oracle and/or its affiliates. All rights reserved. |
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. | 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 * | 4 * |
5 * This code is free software; you can redistribute it and/or modify it | 5 * This code is free software; you can redistribute it and/or modify it |
6 * under the terms of the GNU General Public License version 2 only, as | 6 * under the terms of the GNU General Public License version 2 only, as |
7 * published by the Free Software Foundation. Oracle designates this | 7 * published by the Free Software Foundation. Oracle designates this |
29 import java.lang.reflect.*; | 29 import java.lang.reflect.*; |
30 import java.util.*; | 30 import java.util.*; |
31 | 31 |
32 import sun.misc.*; | 32 import sun.misc.*; |
33 | 33 |
34 import com.oracle.truffle.api.*; | |
34 import com.oracle.truffle.api.nodes.Node.Child; | 35 import com.oracle.truffle.api.nodes.Node.Child; |
35 import com.oracle.truffle.api.nodes.Node.Children; | 36 import com.oracle.truffle.api.nodes.Node.Children; |
36 | 37 |
37 /** | 38 /** |
38 * Utility class that manages the special access methods for node instances. | 39 * Utility class that manages the special access methods for node instances. |
635 printCompactTree(p, node, child, level + 1); | 636 printCompactTree(p, node, child, level + 1); |
636 } | 637 } |
637 p.flush(); | 638 p.flush(); |
638 } | 639 } |
639 | 640 |
641 public static String printSourceAttributionTree(Node node) { | |
642 StringWriter out = new StringWriter(); | |
643 printSourceAttributionTree(new PrintWriter(out), null, node, 1); | |
644 return out.toString(); | |
645 } | |
646 | |
647 public static void printSourceAttributionTree(OutputStream out, Node node) { | |
648 printSourceAttributionTree(new PrintWriter(out), null, node, 1); | |
649 } | |
650 | |
651 private static void printSourceAttributionTree(PrintWriter p, Node parent, Node node, int level) { | |
652 if (node == null) { | |
653 return; | |
654 } | |
655 if (parent == null) { | |
656 // Add some preliminary information before starting with the root node | |
657 final SourceSection sourceSection = node.getSourceSection(); | |
658 if (sourceSection != null) { | |
659 final String txt = sourceSection.getSource().getCode(); | |
660 p.println("Full source len=(" + txt.length() + ") txt=___" + txt + "___"); | |
661 p.println("AST source attribution:"); | |
662 } | |
663 } | |
664 final StringBuilder sb = new StringBuilder(); | |
665 for (int i = 0; i < level; i++) { | |
666 sb.append(" "); | |
667 } | |
668 | |
669 if (parent != null) { | |
670 String childName = ""; | |
671 NodeField[] fields = NodeClass.get(parent.getClass()).fields; | |
672 for (NodeField field : fields) { | |
673 Object value = field.loadValue(parent); | |
674 if (value == node) { | |
675 childName = field.getName(); | |
676 break; | |
677 } else if (value instanceof Node[]) { | |
678 int index = 0; | |
679 for (Node arrayNode : (Node[]) value) { | |
680 if (arrayNode == node) { | |
681 childName = field.getName() + "[" + index + "]"; | |
682 break; | |
683 } | |
684 index++; | |
685 } | |
686 } | |
687 } | |
688 sb.append(childName); | |
689 } | |
690 | |
691 sb.append(" (" + node.getClass().getSimpleName() + ") "); | |
692 sb.append(displaySourceAttribution(node)); | |
693 p.println(sb.toString()); | |
694 | |
695 for (Node child : node.getChildren()) { | |
696 printSourceAttributionTree(p, node, child, level + 1); | |
697 } | |
698 p.flush(); | |
699 } | |
700 | |
640 /** | 701 /** |
641 * Prints a human readable form of a {@link Node} AST to the given {@link PrintStream}. This | 702 * Prints a human readable form of a {@link Node} AST to the given {@link PrintStream}. This |
642 * print method does not check for cycles in the node structure. | 703 * print method does not check for cycles in the node structure. |
643 * | 704 * |
644 * @param out the stream to print to. | 705 * @param out the stream to print to. |
722 } | 783 } |
723 | 784 |
724 private static String nodeName(Node node) { | 785 private static String nodeName(Node node) { |
725 return node.getClass().getSimpleName(); | 786 return node.getClass().getSimpleName(); |
726 } | 787 } |
788 | |
789 private static String displaySourceAttribution(Node node) { | |
790 final SourceSection section = node.getSourceSection(); | |
791 if (section != null) { | |
792 final String srcText = section.getCode(); | |
793 final StringBuilder sb = new StringBuilder(); | |
794 sb.append("source: len=" + srcText.length()); | |
795 sb.append(" (" + section.getCharIndex() + "," + (section.getCharEndIndex() - 1) + ")"); | |
796 sb.append(" txt=___" + srcText + "___"); | |
797 return sb.toString(); | |
798 } | |
799 return ""; | |
800 } | |
727 } | 801 } |