comparison graal/com.oracle.truffle.ruby.nodes/src/com/oracle/truffle/ruby/nodes/instrument/RubyASTPrinter.java @ 14094:3f27e57439ed

Truffle/Instrumentation: significant rearrangement (including moved class) and extension of the Truffle Instrumentation Framework. New interfaces include DebugContext (which can be attached to the ExecutionContext), through which access is provided to possibly language-specific (a) node instrumentation, (b) debug services manager, (c) notification when programs halt, (d) display of language values, and (e) display of variable identifiers.
author Michael Van De Vanter <michael.van.de.vanter@oracle.com>
date Mon, 03 Feb 2014 20:58:23 -0800
parents graal/com.oracle.truffle.ruby.nodes/src/com/oracle/truffle/ruby/nodes/debug/RubyASTPrinter.java@fb846424299f
children
comparison
equal deleted inserted replaced
13736:64fa70319890 14094:3f27e57439ed
1 /*
2 * Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved. This
3 * code is released under a tri EPL/GPL/LGPL license. You can use it,
4 * redistribute it and/or modify it under the terms of the:
5 *
6 * Eclipse Public License version 1.0
7 * GNU General Public License version 2
8 * GNU Lesser General Public License version 2.1
9 */
10 package com.oracle.truffle.ruby.nodes.instrument;
11
12 import java.io.*;
13 import java.util.*;
14
15 import com.oracle.truffle.api.*;
16 import com.oracle.truffle.api.debug.*;
17 import com.oracle.truffle.api.nodes.*;
18 import com.oracle.truffle.api.nodes.NodeUtil.NodeClass;
19 import com.oracle.truffle.api.nodes.NodeUtil.NodeField;
20 import com.oracle.truffle.api.nodes.NodeUtil.NodeFieldKind;
21 import com.oracle.truffle.api.nodes.instrument.*;
22 import com.oracle.truffle.ruby.nodes.*;
23 import com.oracle.truffle.ruby.nodes.call.*;
24 import com.oracle.truffle.ruby.nodes.literal.*;
25 import com.oracle.truffle.ruby.nodes.methods.*;
26
27 /**
28 * Printers for Truffle-internal AST information.
29 */
30 public final class RubyASTPrinter implements ASTPrinter {
31
32 public RubyASTPrinter() {
33 }
34
35 public void printTree(PrintWriter p, Node node, int maxDepth, Node markNode) {
36 printTree(p, node, maxDepth, markNode, 1);
37 p.println();
38 p.flush();
39 }
40
41 public String printTreeToString(Node node, int maxDepth, Node markNode) {
42 StringWriter out = new StringWriter();
43 printTree(new PrintWriter(out), node, maxDepth, markNode);
44 return out.toString();
45 }
46
47 public String printTreeToString(Node node, int maxDepth) {
48 return printTreeToString(node, maxDepth, null);
49 }
50
51 private static void printTree(PrintWriter p, Node node, int maxDepth, Node markNode, int level) {
52 if (node == null) {
53 p.print("null");
54 return;
55 }
56
57 p.print(nodeName(node));
58
59 String sep = "";
60 p.print("(");
61
62 final SourceSection src = node.getSourceSection();
63 if (src != null) {
64 if (!(src instanceof NullSourceSection)) {
65 p.print(src.getSource().getName() + ":" + src.getStartLine());
66 } else if (src instanceof CoreSourceSection) {
67 final CoreSourceSection coreSection = (CoreSourceSection) src;
68 p.print("core=\"" + (coreSection == null ? "?" : coreSection.toString()) + "\"");
69 }
70 }
71 if (node instanceof PhylumMarked) {
72 final PhylumMarked markedNode = (PhylumMarked) node;
73 String prefix = "";
74 for (NodePhylum phylum : markedNode.getPhylumMarks()) {
75 p.print(prefix);
76 prefix = ",";
77 p.print(phylum.toString());
78 }
79
80 }
81
82 ArrayList<NodeField> childFields = new ArrayList<>();
83
84 for (NodeField field : NodeClass.get(node.getClass()).getFields()) {
85 if (field.getKind() == NodeFieldKind.CHILD || field.getKind() == NodeFieldKind.CHILDREN) {
86 childFields.add(field);
87 } else if (field.getKind() == NodeFieldKind.DATA) {
88 // p.print(sep);
89 // sep = ", ";
90 //
91 // final String fieldName = field.getName();
92 // switch (fieldName) {
93 //
94 // }
95 // p.print(fieldName);
96 // p.print(" = ");
97 // p.print(field.loadValue(node));
98 }
99 }
100 p.print(")");
101
102 if (level <= maxDepth) {
103
104 if (childFields.size() != 0) {
105 p.print(" {");
106 for (NodeField field : childFields) {
107
108 Object value = field.loadValue(node);
109 if (value == null) {
110 printNewLine(p, level);
111 p.print(field.getName());
112 p.print(" = null ");
113 } else if (field.getKind() == NodeFieldKind.CHILD) {
114 final Node valueNode = (Node) value;
115 printNewLine(p, level, valueNode == markNode);
116 p.print(field.getName());
117 p.print(" = ");
118 printTree(p, valueNode, maxDepth, markNode, level + 1);
119 } else if (field.getKind() == NodeFieldKind.CHILDREN) {
120 printNewLine(p, level);
121 p.print(field.getName());
122 Node[] children = (Node[]) value;
123 p.print(" = [");
124 sep = "";
125 for (Node child : children) {
126 p.print(sep);
127 sep = ", ";
128 printTree(p, child, maxDepth, markNode, level + 1);
129 }
130 p.print("]");
131 } else {
132 printNewLine(p, level);
133 p.print(field.getName());
134 }
135 }
136 printNewLine(p, level - 1);
137 p.print("}");
138 }
139 }
140 }
141
142 private static void printNewLine(PrintWriter p, int level, boolean mark) {
143 p.println();
144 for (int i = 0; i < level; i++) {
145 if (mark && i == 0) {
146 p.print(" -->");
147 } else {
148 p.print(" ");
149 }
150 }
151 }
152
153 private static void printNewLine(PrintWriter p, int level) {
154 printNewLine(p, level, false);
155 }
156
157 private static String nodeName(Node node) {
158 String nodeVal = null;
159 if (node instanceof CallNode) {
160 final CallNode callNode = (CallNode) node;
161 nodeVal = callNode.getName();
162
163 } else if (node instanceof FixnumLiteralNode) {
164 final FixnumLiteralNode fixnum = (FixnumLiteralNode) node;
165 nodeVal = Integer.toString(fixnum.getValue());
166 } else if (node instanceof MethodDefinitionNode) {
167 final MethodDefinitionNode defNode = (MethodDefinitionNode) node;
168 nodeVal = defNode.getName();
169 }
170 String result = node.getClass().getSimpleName();
171 if (nodeVal != null) {
172 result = result + "[\"" + nodeVal + "\"]";
173 }
174 return result;
175 }
176
177 }