annotate truffle/com.oracle.truffle.api/src/com/oracle/truffle/api/nodes/NodeUtil.java @ 21951:9c8c0937da41

Moving all sources into truffle subdirectory
author Jaroslav Tulach <jaroslav.tulach@oracle.com>
date Wed, 17 Jun 2015 10:58:08 +0200
parents graal/com.oracle.truffle.api/src/com/oracle/truffle/api/nodes/NodeUtil.java@f4cd6b1c2efc
children 5023b913e2ba
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
7267
a4b84ba6dc2e Introduction of the Truffle API for efficient implementation of dynamic languages on top of the Graal VM. New projects com.oracle.truffle.api for the API definition and com.oracle.truffle.api.test for API tests and documentation.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
1 /*
18984
0f462015296f Truffle: Additions to NodeUtil to support optional "safe" AST node replacement (and diagnostics when unsafe)
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 18905
diff changeset
2 * Copyright (c) 2012, 2015, Oracle and/or its affiliates. All rights reserved.
7267
a4b84ba6dc2e Introduction of the Truffle API for efficient implementation of dynamic languages on top of the Graal VM. New projects com.oracle.truffle.api for the API definition and com.oracle.truffle.api.test for API tests and documentation.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
a4b84ba6dc2e Introduction of the Truffle API for efficient implementation of dynamic languages on top of the Graal VM. New projects com.oracle.truffle.api for the API definition and com.oracle.truffle.api.test for API tests and documentation.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
4 *
a4b84ba6dc2e Introduction of the Truffle API for efficient implementation of dynamic languages on top of the Graal VM. New projects com.oracle.truffle.api for the API definition and com.oracle.truffle.api.test for API tests and documentation.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
5 * This code is free software; you can redistribute it and/or modify it
a4b84ba6dc2e Introduction of the Truffle API for efficient implementation of dynamic languages on top of the Graal VM. New projects com.oracle.truffle.api for the API definition and com.oracle.truffle.api.test for API tests and documentation.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
6 * under the terms of the GNU General Public License version 2 only, as
11279
494b818b527c Adding "Classpath" exception to the classes in the com.oracle.truffle.api and the com.oracle.truffle.api.dsl package.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents: 10845
diff changeset
7 * published by the Free Software Foundation. Oracle designates this
494b818b527c Adding "Classpath" exception to the classes in the com.oracle.truffle.api and the com.oracle.truffle.api.dsl package.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents: 10845
diff changeset
8 * particular file as subject to the "Classpath" exception as provided
494b818b527c Adding "Classpath" exception to the classes in the com.oracle.truffle.api and the com.oracle.truffle.api.dsl package.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents: 10845
diff changeset
9 * by Oracle in the LICENSE file that accompanied this code.
7267
a4b84ba6dc2e Introduction of the Truffle API for efficient implementation of dynamic languages on top of the Graal VM. New projects com.oracle.truffle.api for the API definition and com.oracle.truffle.api.test for API tests and documentation.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
10 *
a4b84ba6dc2e Introduction of the Truffle API for efficient implementation of dynamic languages on top of the Graal VM. New projects com.oracle.truffle.api for the API definition and com.oracle.truffle.api.test for API tests and documentation.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
11 * This code is distributed in the hope that it will be useful, but WITHOUT
a4b84ba6dc2e Introduction of the Truffle API for efficient implementation of dynamic languages on top of the Graal VM. New projects com.oracle.truffle.api for the API definition and com.oracle.truffle.api.test for API tests and documentation.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
a4b84ba6dc2e Introduction of the Truffle API for efficient implementation of dynamic languages on top of the Graal VM. New projects com.oracle.truffle.api for the API definition and com.oracle.truffle.api.test for API tests and documentation.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
a4b84ba6dc2e Introduction of the Truffle API for efficient implementation of dynamic languages on top of the Graal VM. New projects com.oracle.truffle.api for the API definition and com.oracle.truffle.api.test for API tests and documentation.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
14 * version 2 for more details (a copy is included in the LICENSE file that
a4b84ba6dc2e Introduction of the Truffle API for efficient implementation of dynamic languages on top of the Graal VM. New projects com.oracle.truffle.api for the API definition and com.oracle.truffle.api.test for API tests and documentation.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
15 * accompanied this code).
a4b84ba6dc2e Introduction of the Truffle API for efficient implementation of dynamic languages on top of the Graal VM. New projects com.oracle.truffle.api for the API definition and com.oracle.truffle.api.test for API tests and documentation.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
16 *
a4b84ba6dc2e Introduction of the Truffle API for efficient implementation of dynamic languages on top of the Graal VM. New projects com.oracle.truffle.api for the API definition and com.oracle.truffle.api.test for API tests and documentation.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
17 * You should have received a copy of the GNU General Public License version
a4b84ba6dc2e Introduction of the Truffle API for efficient implementation of dynamic languages on top of the Graal VM. New projects com.oracle.truffle.api for the API definition and com.oracle.truffle.api.test for API tests and documentation.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
18 * 2 along with this work; if not, write to the Free Software Foundation,
a4b84ba6dc2e Introduction of the Truffle API for efficient implementation of dynamic languages on top of the Graal VM. New projects com.oracle.truffle.api for the API definition and com.oracle.truffle.api.test for API tests and documentation.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
a4b84ba6dc2e Introduction of the Truffle API for efficient implementation of dynamic languages on top of the Graal VM. New projects com.oracle.truffle.api for the API definition and com.oracle.truffle.api.test for API tests and documentation.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
20 *
a4b84ba6dc2e Introduction of the Truffle API for efficient implementation of dynamic languages on top of the Graal VM. New projects com.oracle.truffle.api for the API definition and com.oracle.truffle.api.test for API tests and documentation.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
a4b84ba6dc2e Introduction of the Truffle API for efficient implementation of dynamic languages on top of the Graal VM. New projects com.oracle.truffle.api for the API definition and com.oracle.truffle.api.test for API tests and documentation.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
22 * or visit www.oracle.com if you need additional information or have any
a4b84ba6dc2e Introduction of the Truffle API for efficient implementation of dynamic languages on top of the Graal VM. New projects com.oracle.truffle.api for the API definition and com.oracle.truffle.api.test for API tests and documentation.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
23 * questions.
a4b84ba6dc2e Introduction of the Truffle API for efficient implementation of dynamic languages on top of the Graal VM. New projects com.oracle.truffle.api for the API definition and com.oracle.truffle.api.test for API tests and documentation.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
24 */
a4b84ba6dc2e Introduction of the Truffle API for efficient implementation of dynamic languages on top of the Graal VM. New projects com.oracle.truffle.api for the API definition and com.oracle.truffle.api.test for API tests and documentation.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
25 package com.oracle.truffle.api.nodes;
a4b84ba6dc2e Introduction of the Truffle API for efficient implementation of dynamic languages on top of the Graal VM. New projects com.oracle.truffle.api for the API definition and com.oracle.truffle.api.test for API tests and documentation.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
26
a4b84ba6dc2e Introduction of the Truffle API for efficient implementation of dynamic languages on top of the Graal VM. New projects com.oracle.truffle.api for the API definition and com.oracle.truffle.api.test for API tests and documentation.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
27 import java.io.*;
a4b84ba6dc2e Introduction of the Truffle API for efficient implementation of dynamic languages on top of the Graal VM. New projects com.oracle.truffle.api for the API definition and com.oracle.truffle.api.test for API tests and documentation.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
28 import java.lang.annotation.*;
a4b84ba6dc2e Introduction of the Truffle API for efficient implementation of dynamic languages on top of the Graal VM. New projects com.oracle.truffle.api for the API definition and com.oracle.truffle.api.test for API tests and documentation.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
29 import java.lang.reflect.*;
a4b84ba6dc2e Introduction of the Truffle API for efficient implementation of dynamic languages on top of the Graal VM. New projects com.oracle.truffle.api for the API definition and com.oracle.truffle.api.test for API tests and documentation.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
30 import java.util.*;
a4b84ba6dc2e Introduction of the Truffle API for efficient implementation of dynamic languages on top of the Graal VM. New projects com.oracle.truffle.api for the API definition and com.oracle.truffle.api.test for API tests and documentation.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
31
a4b84ba6dc2e Introduction of the Truffle API for efficient implementation of dynamic languages on top of the Graal VM. New projects com.oracle.truffle.api for the API definition and com.oracle.truffle.api.test for API tests and documentation.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
32 import sun.misc.*;
a4b84ba6dc2e Introduction of the Truffle API for efficient implementation of dynamic languages on top of the Graal VM. New projects com.oracle.truffle.api for the API definition and com.oracle.truffle.api.test for API tests and documentation.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
33
11848
099af41815ea Truffle: add NodeUtil.printSourceAttributionTree() for debugging
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 11809
diff changeset
34 import com.oracle.truffle.api.*;
18485
e3c95cbbb50c Truffle Instrumentation: major API revision, based around the Probe and Instrument classes; add Instrumentable API for language implementors, with most details automated; reimplemented to handle AST splitting automatically; more JUnit tests.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 18332
diff changeset
35 import com.oracle.truffle.api.instrument.*;
e3c95cbbb50c Truffle Instrumentation: major API revision, based around the Probe and Instrument classes; add Instrumentable API for language implementors, with most details automated; reimplemented to handle AST splitting automatically; more JUnit tests.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 18332
diff changeset
36 import com.oracle.truffle.api.instrument.ProbeNode.WrapperNode;
20129
5b7db8941fd7 Truffle: make NodeClass and NodeField a top-level class.
Christian Humer <christian.humer@gmail.com>
parents: 19974
diff changeset
37 import com.oracle.truffle.api.nodes.NodeFieldAccessor.NodeFieldKind;
16067
915ebb306fcc Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 15834
diff changeset
38 import com.oracle.truffle.api.source.*;
9537
e2965e5cd474 Fixed Truffle child iterator should only iterate children which are annotated with @Child or @Children.
Christian Humer <christian.humer@gmail.com>
parents: 9487
diff changeset
39
7267
a4b84ba6dc2e Introduction of the Truffle API for efficient implementation of dynamic languages on top of the Graal VM. New projects com.oracle.truffle.api for the API definition and com.oracle.truffle.api.test for API tests and documentation.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
40 /**
a4b84ba6dc2e Introduction of the Truffle API for efficient implementation of dynamic languages on top of the Graal VM. New projects com.oracle.truffle.api for the API definition and com.oracle.truffle.api.test for API tests and documentation.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
41 * Utility class that manages the special access methods for node instances.
a4b84ba6dc2e Introduction of the Truffle API for efficient implementation of dynamic languages on top of the Graal VM. New projects com.oracle.truffle.api for the API definition and com.oracle.truffle.api.test for API tests and documentation.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
42 */
11636
136eaa90ef41 Truffle: make NodeUtil class final and its unsafe field private.
Andreas Woess <andreas.woess@jku.at>
parents: 11622
diff changeset
43 public final class NodeUtil {
7267
a4b84ba6dc2e Introduction of the Truffle API for efficient implementation of dynamic languages on top of the Graal VM. New projects com.oracle.truffle.api for the API definition and com.oracle.truffle.api.test for API tests and documentation.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
44
9754
98b004bf3985 Refactoring of NodeUtil to centralize the use of reflection
Christian Wimmer <christian.wimmer@oracle.com>
parents: 9538
diff changeset
45 /**
98b004bf3985 Refactoring of NodeUtil to centralize the use of reflection
Christian Wimmer <christian.wimmer@oracle.com>
parents: 9538
diff changeset
46 * Interface that allows the customization of field offsets used for {@link Unsafe} field
98b004bf3985 Refactoring of NodeUtil to centralize the use of reflection
Christian Wimmer <christian.wimmer@oracle.com>
parents: 9538
diff changeset
47 * accesses.
98b004bf3985 Refactoring of NodeUtil to centralize the use of reflection
Christian Wimmer <christian.wimmer@oracle.com>
parents: 9538
diff changeset
48 */
98b004bf3985 Refactoring of NodeUtil to centralize the use of reflection
Christian Wimmer <christian.wimmer@oracle.com>
parents: 9538
diff changeset
49 public interface FieldOffsetProvider {
98b004bf3985 Refactoring of NodeUtil to centralize the use of reflection
Christian Wimmer <christian.wimmer@oracle.com>
parents: 9538
diff changeset
50
98b004bf3985 Refactoring of NodeUtil to centralize the use of reflection
Christian Wimmer <christian.wimmer@oracle.com>
parents: 9538
diff changeset
51 long objectFieldOffset(Field field);
15834
fc6f8d143c68 Introduce method to customize type size
Christian Wimmer <christian.wimmer@oracle.com>
parents: 15093
diff changeset
52
fc6f8d143c68 Introduce method to customize type size
Christian Wimmer <christian.wimmer@oracle.com>
parents: 15093
diff changeset
53 int getTypeSize(Class<?> clazz);
9754
98b004bf3985 Refactoring of NodeUtil to centralize the use of reflection
Christian Wimmer <christian.wimmer@oracle.com>
parents: 9538
diff changeset
54 }
98b004bf3985 Refactoring of NodeUtil to centralize the use of reflection
Christian Wimmer <christian.wimmer@oracle.com>
parents: 9538
diff changeset
55
16467
17f7331dcc4f Truffle: move iterator to NodeClass
Andreas Woess <andreas.woess@jku.at>
parents: 16466
diff changeset
56 static Iterator<Node> makeIterator(Node node) {
20130
8dc73c226c63 Truffle: cache NodeClass lookup in Node.
Christian Humer <christian.humer@gmail.com>
parents: 20129
diff changeset
57 return node.getNodeClass().makeIterator(node);
7267
a4b84ba6dc2e Introduction of the Truffle API for efficient implementation of dynamic languages on top of the Graal VM. New projects com.oracle.truffle.api for the API definition and com.oracle.truffle.api.test for API tests and documentation.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
58 }
a4b84ba6dc2e Introduction of the Truffle API for efficient implementation of dynamic languages on top of the Graal VM. New projects com.oracle.truffle.api for the API definition and com.oracle.truffle.api.test for API tests and documentation.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
59
17399
5787218bad91 Truffle: implemented recursive node iterator and node streams for the graal runtime.
Christian Humer <christian.humer@gmail.com>
parents: 17275
diff changeset
60 public static Iterator<Node> makeRecursiveIterator(Node node) {
5787218bad91 Truffle: implemented recursive node iterator and node streams for the graal runtime.
Christian Humer <christian.humer@gmail.com>
parents: 17275
diff changeset
61 return new RecursiveNodeIterator(node);
5787218bad91 Truffle: implemented recursive node iterator and node streams for the graal runtime.
Christian Humer <christian.humer@gmail.com>
parents: 17275
diff changeset
62 }
5787218bad91 Truffle: implemented recursive node iterator and node streams for the graal runtime.
Christian Humer <christian.humer@gmail.com>
parents: 17275
diff changeset
63
18163
c88ab4f1f04a re-enabled Checkstyle with the release of 6.0 that supports Java 8; fixed existing Checkstyle warnings
Doug Simon <doug.simon@oracle.com>
parents: 17399
diff changeset
64 private static final class RecursiveNodeIterator implements Iterator<Node> {
17399
5787218bad91 Truffle: implemented recursive node iterator and node streams for the graal runtime.
Christian Humer <christian.humer@gmail.com>
parents: 17275
diff changeset
65 private final List<Iterator<Node>> iteratorStack = new ArrayList<>();
5787218bad91 Truffle: implemented recursive node iterator and node streams for the graal runtime.
Christian Humer <christian.humer@gmail.com>
parents: 17275
diff changeset
66
5787218bad91 Truffle: implemented recursive node iterator and node streams for the graal runtime.
Christian Humer <christian.humer@gmail.com>
parents: 17275
diff changeset
67 public RecursiveNodeIterator(final Node node) {
5787218bad91 Truffle: implemented recursive node iterator and node streams for the graal runtime.
Christian Humer <christian.humer@gmail.com>
parents: 17275
diff changeset
68 iteratorStack.add(new Iterator<Node>() {
5787218bad91 Truffle: implemented recursive node iterator and node streams for the graal runtime.
Christian Humer <christian.humer@gmail.com>
parents: 17275
diff changeset
69
5787218bad91 Truffle: implemented recursive node iterator and node streams for the graal runtime.
Christian Humer <christian.humer@gmail.com>
parents: 17275
diff changeset
70 private boolean visited;
5787218bad91 Truffle: implemented recursive node iterator and node streams for the graal runtime.
Christian Humer <christian.humer@gmail.com>
parents: 17275
diff changeset
71
5787218bad91 Truffle: implemented recursive node iterator and node streams for the graal runtime.
Christian Humer <christian.humer@gmail.com>
parents: 17275
diff changeset
72 public void remove() {
5787218bad91 Truffle: implemented recursive node iterator and node streams for the graal runtime.
Christian Humer <christian.humer@gmail.com>
parents: 17275
diff changeset
73 throw new UnsupportedOperationException();
5787218bad91 Truffle: implemented recursive node iterator and node streams for the graal runtime.
Christian Humer <christian.humer@gmail.com>
parents: 17275
diff changeset
74 }
5787218bad91 Truffle: implemented recursive node iterator and node streams for the graal runtime.
Christian Humer <christian.humer@gmail.com>
parents: 17275
diff changeset
75
5787218bad91 Truffle: implemented recursive node iterator and node streams for the graal runtime.
Christian Humer <christian.humer@gmail.com>
parents: 17275
diff changeset
76 public Node next() {
5787218bad91 Truffle: implemented recursive node iterator and node streams for the graal runtime.
Christian Humer <christian.humer@gmail.com>
parents: 17275
diff changeset
77 if (visited) {
5787218bad91 Truffle: implemented recursive node iterator and node streams for the graal runtime.
Christian Humer <christian.humer@gmail.com>
parents: 17275
diff changeset
78 throw new NoSuchElementException();
5787218bad91 Truffle: implemented recursive node iterator and node streams for the graal runtime.
Christian Humer <christian.humer@gmail.com>
parents: 17275
diff changeset
79 }
5787218bad91 Truffle: implemented recursive node iterator and node streams for the graal runtime.
Christian Humer <christian.humer@gmail.com>
parents: 17275
diff changeset
80 visited = true;
5787218bad91 Truffle: implemented recursive node iterator and node streams for the graal runtime.
Christian Humer <christian.humer@gmail.com>
parents: 17275
diff changeset
81 return node;
5787218bad91 Truffle: implemented recursive node iterator and node streams for the graal runtime.
Christian Humer <christian.humer@gmail.com>
parents: 17275
diff changeset
82 }
5787218bad91 Truffle: implemented recursive node iterator and node streams for the graal runtime.
Christian Humer <christian.humer@gmail.com>
parents: 17275
diff changeset
83
5787218bad91 Truffle: implemented recursive node iterator and node streams for the graal runtime.
Christian Humer <christian.humer@gmail.com>
parents: 17275
diff changeset
84 public boolean hasNext() {
5787218bad91 Truffle: implemented recursive node iterator and node streams for the graal runtime.
Christian Humer <christian.humer@gmail.com>
parents: 17275
diff changeset
85 return !visited;
5787218bad91 Truffle: implemented recursive node iterator and node streams for the graal runtime.
Christian Humer <christian.humer@gmail.com>
parents: 17275
diff changeset
86 }
5787218bad91 Truffle: implemented recursive node iterator and node streams for the graal runtime.
Christian Humer <christian.humer@gmail.com>
parents: 17275
diff changeset
87 });
5787218bad91 Truffle: implemented recursive node iterator and node streams for the graal runtime.
Christian Humer <christian.humer@gmail.com>
parents: 17275
diff changeset
88 }
5787218bad91 Truffle: implemented recursive node iterator and node streams for the graal runtime.
Christian Humer <christian.humer@gmail.com>
parents: 17275
diff changeset
89
5787218bad91 Truffle: implemented recursive node iterator and node streams for the graal runtime.
Christian Humer <christian.humer@gmail.com>
parents: 17275
diff changeset
90 public boolean hasNext() {
5787218bad91 Truffle: implemented recursive node iterator and node streams for the graal runtime.
Christian Humer <christian.humer@gmail.com>
parents: 17275
diff changeset
91 return peekIterator() != null;
5787218bad91 Truffle: implemented recursive node iterator and node streams for the graal runtime.
Christian Humer <christian.humer@gmail.com>
parents: 17275
diff changeset
92 }
5787218bad91 Truffle: implemented recursive node iterator and node streams for the graal runtime.
Christian Humer <christian.humer@gmail.com>
parents: 17275
diff changeset
93
5787218bad91 Truffle: implemented recursive node iterator and node streams for the graal runtime.
Christian Humer <christian.humer@gmail.com>
parents: 17275
diff changeset
94 public Node next() {
5787218bad91 Truffle: implemented recursive node iterator and node streams for the graal runtime.
Christian Humer <christian.humer@gmail.com>
parents: 17275
diff changeset
95 Iterator<Node> iterator = peekIterator();
5787218bad91 Truffle: implemented recursive node iterator and node streams for the graal runtime.
Christian Humer <christian.humer@gmail.com>
parents: 17275
diff changeset
96 if (iterator == null) {
5787218bad91 Truffle: implemented recursive node iterator and node streams for the graal runtime.
Christian Humer <christian.humer@gmail.com>
parents: 17275
diff changeset
97 throw new NoSuchElementException();
5787218bad91 Truffle: implemented recursive node iterator and node streams for the graal runtime.
Christian Humer <christian.humer@gmail.com>
parents: 17275
diff changeset
98 }
5787218bad91 Truffle: implemented recursive node iterator and node streams for the graal runtime.
Christian Humer <christian.humer@gmail.com>
parents: 17275
diff changeset
99
5787218bad91 Truffle: implemented recursive node iterator and node streams for the graal runtime.
Christian Humer <christian.humer@gmail.com>
parents: 17275
diff changeset
100 Node node = iterator.next();
5787218bad91 Truffle: implemented recursive node iterator and node streams for the graal runtime.
Christian Humer <christian.humer@gmail.com>
parents: 17275
diff changeset
101 if (node != null) {
5787218bad91 Truffle: implemented recursive node iterator and node streams for the graal runtime.
Christian Humer <christian.humer@gmail.com>
parents: 17275
diff changeset
102 Iterator<Node> childIterator = makeIterator(node);
5787218bad91 Truffle: implemented recursive node iterator and node streams for the graal runtime.
Christian Humer <christian.humer@gmail.com>
parents: 17275
diff changeset
103 if (childIterator.hasNext()) {
5787218bad91 Truffle: implemented recursive node iterator and node streams for the graal runtime.
Christian Humer <christian.humer@gmail.com>
parents: 17275
diff changeset
104 iteratorStack.add(childIterator);
5787218bad91 Truffle: implemented recursive node iterator and node streams for the graal runtime.
Christian Humer <christian.humer@gmail.com>
parents: 17275
diff changeset
105 }
5787218bad91 Truffle: implemented recursive node iterator and node streams for the graal runtime.
Christian Humer <christian.humer@gmail.com>
parents: 17275
diff changeset
106 }
5787218bad91 Truffle: implemented recursive node iterator and node streams for the graal runtime.
Christian Humer <christian.humer@gmail.com>
parents: 17275
diff changeset
107 return node;
5787218bad91 Truffle: implemented recursive node iterator and node streams for the graal runtime.
Christian Humer <christian.humer@gmail.com>
parents: 17275
diff changeset
108 }
5787218bad91 Truffle: implemented recursive node iterator and node streams for the graal runtime.
Christian Humer <christian.humer@gmail.com>
parents: 17275
diff changeset
109
5787218bad91 Truffle: implemented recursive node iterator and node streams for the graal runtime.
Christian Humer <christian.humer@gmail.com>
parents: 17275
diff changeset
110 private Iterator<Node> peekIterator() {
5787218bad91 Truffle: implemented recursive node iterator and node streams for the graal runtime.
Christian Humer <christian.humer@gmail.com>
parents: 17275
diff changeset
111 int tos = iteratorStack.size() - 1;
5787218bad91 Truffle: implemented recursive node iterator and node streams for the graal runtime.
Christian Humer <christian.humer@gmail.com>
parents: 17275
diff changeset
112 while (tos >= 0) {
5787218bad91 Truffle: implemented recursive node iterator and node streams for the graal runtime.
Christian Humer <christian.humer@gmail.com>
parents: 17275
diff changeset
113 Iterator<Node> iterable = iteratorStack.get(tos);
5787218bad91 Truffle: implemented recursive node iterator and node streams for the graal runtime.
Christian Humer <christian.humer@gmail.com>
parents: 17275
diff changeset
114 if (iterable.hasNext()) {
5787218bad91 Truffle: implemented recursive node iterator and node streams for the graal runtime.
Christian Humer <christian.humer@gmail.com>
parents: 17275
diff changeset
115 return iterable;
5787218bad91 Truffle: implemented recursive node iterator and node streams for the graal runtime.
Christian Humer <christian.humer@gmail.com>
parents: 17275
diff changeset
116 } else {
5787218bad91 Truffle: implemented recursive node iterator and node streams for the graal runtime.
Christian Humer <christian.humer@gmail.com>
parents: 17275
diff changeset
117 iteratorStack.remove(tos--);
5787218bad91 Truffle: implemented recursive node iterator and node streams for the graal runtime.
Christian Humer <christian.humer@gmail.com>
parents: 17275
diff changeset
118 }
5787218bad91 Truffle: implemented recursive node iterator and node streams for the graal runtime.
Christian Humer <christian.humer@gmail.com>
parents: 17275
diff changeset
119 }
5787218bad91 Truffle: implemented recursive node iterator and node streams for the graal runtime.
Christian Humer <christian.humer@gmail.com>
parents: 17275
diff changeset
120 return null;
5787218bad91 Truffle: implemented recursive node iterator and node streams for the graal runtime.
Christian Humer <christian.humer@gmail.com>
parents: 17275
diff changeset
121 }
5787218bad91 Truffle: implemented recursive node iterator and node streams for the graal runtime.
Christian Humer <christian.humer@gmail.com>
parents: 17275
diff changeset
122
5787218bad91 Truffle: implemented recursive node iterator and node streams for the graal runtime.
Christian Humer <christian.humer@gmail.com>
parents: 17275
diff changeset
123 public void remove() {
5787218bad91 Truffle: implemented recursive node iterator and node streams for the graal runtime.
Christian Humer <christian.humer@gmail.com>
parents: 17275
diff changeset
124 throw new UnsupportedOperationException();
5787218bad91 Truffle: implemented recursive node iterator and node streams for the graal runtime.
Christian Humer <christian.humer@gmail.com>
parents: 17275
diff changeset
125 }
5787218bad91 Truffle: implemented recursive node iterator and node streams for the graal runtime.
Christian Humer <christian.humer@gmail.com>
parents: 17275
diff changeset
126 }
5787218bad91 Truffle: implemented recursive node iterator and node streams for the graal runtime.
Christian Humer <christian.humer@gmail.com>
parents: 17275
diff changeset
127
7267
a4b84ba6dc2e Introduction of the Truffle API for efficient implementation of dynamic languages on top of the Graal VM. New projects com.oracle.truffle.api for the API definition and com.oracle.truffle.api.test for API tests and documentation.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
128 @SuppressWarnings("unchecked")
a4b84ba6dc2e Introduction of the Truffle API for efficient implementation of dynamic languages on top of the Graal VM. New projects com.oracle.truffle.api for the API definition and com.oracle.truffle.api.test for API tests and documentation.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
129 public static <T extends Node> T cloneNode(T orig) {
18750
8a758dce7d80 Truffle: added new deepCopy method to Node.
Christian Humer <christian.humer@gmail.com>
parents: 18510
diff changeset
130 return (T) orig.deepCopy();
8a758dce7d80 Truffle: added new deepCopy method to Node.
Christian Humer <christian.humer@gmail.com>
parents: 18510
diff changeset
131 }
8a758dce7d80 Truffle: added new deepCopy method to Node.
Christian Humer <christian.humer@gmail.com>
parents: 18510
diff changeset
132
8a758dce7d80 Truffle: added new deepCopy method to Node.
Christian Humer <christian.humer@gmail.com>
parents: 18510
diff changeset
133 static Node deepCopyImpl(Node orig) {
11809
c7769440afd8 improve NodeUtil.cloneNode.
Andreas Woess <andreas.woess@jku.at>
parents: 11649
diff changeset
134 final Node clone = orig.copy();
20130
8dc73c226c63 Truffle: cache NodeClass lookup in Node.
Christian Humer <christian.humer@gmail.com>
parents: 20129
diff changeset
135 NodeClass nodeClass = clone.getNodeClass();
7267
a4b84ba6dc2e Introduction of the Truffle API for efficient implementation of dynamic languages on top of the Graal VM. New projects com.oracle.truffle.api for the API definition and com.oracle.truffle.api.test for API tests and documentation.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
136
20129
5b7db8941fd7 Truffle: make NodeClass and NodeField a top-level class.
Christian Humer <christian.humer@gmail.com>
parents: 19974
diff changeset
137 nodeClass.getParentField().putObject(clone, null);
7267
a4b84ba6dc2e Introduction of the Truffle API for efficient implementation of dynamic languages on top of the Graal VM. New projects com.oracle.truffle.api for the API definition and com.oracle.truffle.api.test for API tests and documentation.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
138
20129
5b7db8941fd7 Truffle: make NodeClass and NodeField a top-level class.
Christian Humer <christian.humer@gmail.com>
parents: 19974
diff changeset
139 for (NodeFieldAccessor childField : nodeClass.getChildFields()) {
18904
6ba170cb6f53 Truffle: channel all NodeUtil field accesses through NodeField class
Andreas Woess <andreas.woess@jku.at>
parents: 18751
diff changeset
140 Node child = (Node) childField.getObject(orig);
7267
a4b84ba6dc2e Introduction of the Truffle API for efficient implementation of dynamic languages on top of the Graal VM. New projects com.oracle.truffle.api for the API definition and com.oracle.truffle.api.test for API tests and documentation.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
141 if (child != null) {
18750
8a758dce7d80 Truffle: added new deepCopy method to Node.
Christian Humer <christian.humer@gmail.com>
parents: 18510
diff changeset
142 Node clonedChild = child.deepCopy();
20129
5b7db8941fd7 Truffle: make NodeClass and NodeField a top-level class.
Christian Humer <christian.humer@gmail.com>
parents: 19974
diff changeset
143 nodeClass.getParentField().putObject(clonedChild, clone);
18904
6ba170cb6f53 Truffle: channel all NodeUtil field accesses through NodeField class
Andreas Woess <andreas.woess@jku.at>
parents: 18751
diff changeset
144 childField.putObject(clone, clonedChild);
7267
a4b84ba6dc2e Introduction of the Truffle API for efficient implementation of dynamic languages on top of the Graal VM. New projects com.oracle.truffle.api for the API definition and com.oracle.truffle.api.test for API tests and documentation.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
145 }
a4b84ba6dc2e Introduction of the Truffle API for efficient implementation of dynamic languages on top of the Graal VM. New projects com.oracle.truffle.api for the API definition and com.oracle.truffle.api.test for API tests and documentation.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
146 }
20129
5b7db8941fd7 Truffle: make NodeClass and NodeField a top-level class.
Christian Humer <christian.humer@gmail.com>
parents: 19974
diff changeset
147 for (NodeFieldAccessor childrenField : nodeClass.getChildrenFields()) {
18904
6ba170cb6f53 Truffle: channel all NodeUtil field accesses through NodeField class
Andreas Woess <andreas.woess@jku.at>
parents: 18751
diff changeset
148 Object[] children = (Object[]) childrenField.getObject(orig);
7267
a4b84ba6dc2e Introduction of the Truffle API for efficient implementation of dynamic languages on top of the Graal VM. New projects com.oracle.truffle.api for the API definition and com.oracle.truffle.api.test for API tests and documentation.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
149 if (children != null) {
17275
846c059e3ecf Truffle: allow interface types in child fields
Andreas Woess <andreas.woess@jku.at>
parents: 16779
diff changeset
150 Object[] clonedChildren = (Object[]) Array.newInstance(children.getClass().getComponentType(), children.length);
7267
a4b84ba6dc2e Introduction of the Truffle API for efficient implementation of dynamic languages on top of the Graal VM. New projects com.oracle.truffle.api for the API definition and com.oracle.truffle.api.test for API tests and documentation.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
151 for (int i = 0; i < children.length; i++) {
11809
c7769440afd8 improve NodeUtil.cloneNode.
Andreas Woess <andreas.woess@jku.at>
parents: 11649
diff changeset
152 if (children[i] != null) {
18750
8a758dce7d80 Truffle: added new deepCopy method to Node.
Christian Humer <christian.humer@gmail.com>
parents: 18510
diff changeset
153 Node clonedChild = ((Node) children[i]).deepCopy();
11809
c7769440afd8 improve NodeUtil.cloneNode.
Andreas Woess <andreas.woess@jku.at>
parents: 11649
diff changeset
154 clonedChildren[i] = clonedChild;
20129
5b7db8941fd7 Truffle: make NodeClass and NodeField a top-level class.
Christian Humer <christian.humer@gmail.com>
parents: 19974
diff changeset
155 nodeClass.getParentField().putObject(clonedChild, clone);
7267
a4b84ba6dc2e Introduction of the Truffle API for efficient implementation of dynamic languages on top of the Graal VM. New projects com.oracle.truffle.api for the API definition and com.oracle.truffle.api.test for API tests and documentation.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
156 }
a4b84ba6dc2e Introduction of the Truffle API for efficient implementation of dynamic languages on top of the Graal VM. New projects com.oracle.truffle.api for the API definition and com.oracle.truffle.api.test for API tests and documentation.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
157 }
18904
6ba170cb6f53 Truffle: channel all NodeUtil field accesses through NodeField class
Andreas Woess <andreas.woess@jku.at>
parents: 18751
diff changeset
158 childrenField.putObject(clone, clonedChildren);
7267
a4b84ba6dc2e Introduction of the Truffle API for efficient implementation of dynamic languages on top of the Graal VM. New projects com.oracle.truffle.api for the API definition and com.oracle.truffle.api.test for API tests and documentation.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
159 }
a4b84ba6dc2e Introduction of the Truffle API for efficient implementation of dynamic languages on top of the Graal VM. New projects com.oracle.truffle.api for the API definition and com.oracle.truffle.api.test for API tests and documentation.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
160 }
20129
5b7db8941fd7 Truffle: make NodeClass and NodeField a top-level class.
Christian Humer <christian.humer@gmail.com>
parents: 19974
diff changeset
161 for (NodeFieldAccessor cloneableField : nodeClass.getCloneableFields()) {
18904
6ba170cb6f53 Truffle: channel all NodeUtil field accesses through NodeField class
Andreas Woess <andreas.woess@jku.at>
parents: 18751
diff changeset
162 Object cloneable = cloneableField.getObject(clone);
6ba170cb6f53 Truffle: channel all NodeUtil field accesses through NodeField class
Andreas Woess <andreas.woess@jku.at>
parents: 18751
diff changeset
163 if (cloneable != null && cloneable == cloneableField.getObject(orig)) {
6ba170cb6f53 Truffle: channel all NodeUtil field accesses through NodeField class
Andreas Woess <andreas.woess@jku.at>
parents: 18751
diff changeset
164 cloneableField.putObject(clone, ((NodeCloneable) cloneable).clone());
18510
cb4d5cc2b52b Truffle: clone ConditionProfile and BranchProfile node fields
Andreas Woess <andreas.woess@jku.at>
parents: 18485
diff changeset
165 }
cb4d5cc2b52b Truffle: clone ConditionProfile and BranchProfile node fields
Andreas Woess <andreas.woess@jku.at>
parents: 18485
diff changeset
166 }
18750
8a758dce7d80 Truffle: added new deepCopy method to Node.
Christian Humer <christian.humer@gmail.com>
parents: 18510
diff changeset
167 return clone;
7267
a4b84ba6dc2e Introduction of the Truffle API for efficient implementation of dynamic languages on top of the Graal VM. New projects com.oracle.truffle.api for the API definition and com.oracle.truffle.api.test for API tests and documentation.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
168 }
a4b84ba6dc2e Introduction of the Truffle API for efficient implementation of dynamic languages on top of the Graal VM. New projects com.oracle.truffle.api for the API definition and com.oracle.truffle.api.test for API tests and documentation.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
169
9537
e2965e5cd474 Fixed Truffle child iterator should only iterate children which are annotated with @Child or @Children.
Christian Humer <christian.humer@gmail.com>
parents: 9487
diff changeset
170 public static List<Node> findNodeChildren(Node node) {
e2965e5cd474 Fixed Truffle child iterator should only iterate children which are annotated with @Child or @Children.
Christian Humer <christian.humer@gmail.com>
parents: 9487
diff changeset
171 List<Node> nodes = new ArrayList<>();
20130
8dc73c226c63 Truffle: cache NodeClass lookup in Node.
Christian Humer <christian.humer@gmail.com>
parents: 20129
diff changeset
172 NodeClass nodeClass = node.getNodeClass();
7267
a4b84ba6dc2e Introduction of the Truffle API for efficient implementation of dynamic languages on top of the Graal VM. New projects com.oracle.truffle.api for the API definition and com.oracle.truffle.api.test for API tests and documentation.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
173
20129
5b7db8941fd7 Truffle: make NodeClass and NodeField a top-level class.
Christian Humer <christian.humer@gmail.com>
parents: 19974
diff changeset
174 for (NodeFieldAccessor nodeField : nodeClass.getChildFields()) {
18904
6ba170cb6f53 Truffle: channel all NodeUtil field accesses through NodeField class
Andreas Woess <andreas.woess@jku.at>
parents: 18751
diff changeset
175 Object child = nodeField.getObject(node);
7267
a4b84ba6dc2e Introduction of the Truffle API for efficient implementation of dynamic languages on top of the Graal VM. New projects com.oracle.truffle.api for the API definition and com.oracle.truffle.api.test for API tests and documentation.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
176 if (child != null) {
9537
e2965e5cd474 Fixed Truffle child iterator should only iterate children which are annotated with @Child or @Children.
Christian Humer <christian.humer@gmail.com>
parents: 9487
diff changeset
177 nodes.add((Node) child);
7267
a4b84ba6dc2e Introduction of the Truffle API for efficient implementation of dynamic languages on top of the Graal VM. New projects com.oracle.truffle.api for the API definition and com.oracle.truffle.api.test for API tests and documentation.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
178 }
a4b84ba6dc2e Introduction of the Truffle API for efficient implementation of dynamic languages on top of the Graal VM. New projects com.oracle.truffle.api for the API definition and com.oracle.truffle.api.test for API tests and documentation.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
179 }
20129
5b7db8941fd7 Truffle: make NodeClass and NodeField a top-level class.
Christian Humer <christian.humer@gmail.com>
parents: 19974
diff changeset
180 for (NodeFieldAccessor nodeField : nodeClass.getChildrenFields()) {
18904
6ba170cb6f53 Truffle: channel all NodeUtil field accesses through NodeField class
Andreas Woess <andreas.woess@jku.at>
parents: 18751
diff changeset
181 Object[] children = (Object[]) nodeField.getObject(node);
7267
a4b84ba6dc2e Introduction of the Truffle API for efficient implementation of dynamic languages on top of the Graal VM. New projects com.oracle.truffle.api for the API definition and com.oracle.truffle.api.test for API tests and documentation.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
182 if (children != null) {
17275
846c059e3ecf Truffle: allow interface types in child fields
Andreas Woess <andreas.woess@jku.at>
parents: 16779
diff changeset
183 for (Object child : children) {
10845
e9248ebb1d79 Truffle: ignore null children in NodeUtil.findNodeChildren; refactoring.
Andreas Woess <andreas.woess@jku.at>
parents: 10793
diff changeset
184 if (child != null) {
17275
846c059e3ecf Truffle: allow interface types in child fields
Andreas Woess <andreas.woess@jku.at>
parents: 16779
diff changeset
185 nodes.add((Node) child);
10845
e9248ebb1d79 Truffle: ignore null children in NodeUtil.findNodeChildren; refactoring.
Andreas Woess <andreas.woess@jku.at>
parents: 10793
diff changeset
186 }
e9248ebb1d79 Truffle: ignore null children in NodeUtil.findNodeChildren; refactoring.
Andreas Woess <andreas.woess@jku.at>
parents: 10793
diff changeset
187 }
7267
a4b84ba6dc2e Introduction of the Truffle API for efficient implementation of dynamic languages on top of the Graal VM. New projects com.oracle.truffle.api for the API definition and com.oracle.truffle.api.test for API tests and documentation.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
188 }
a4b84ba6dc2e Introduction of the Truffle API for efficient implementation of dynamic languages on top of the Graal VM. New projects com.oracle.truffle.api for the API definition and com.oracle.truffle.api.test for API tests and documentation.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
189 }
a4b84ba6dc2e Introduction of the Truffle API for efficient implementation of dynamic languages on top of the Graal VM. New projects com.oracle.truffle.api for the API definition and com.oracle.truffle.api.test for API tests and documentation.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
190
a4b84ba6dc2e Introduction of the Truffle API for efficient implementation of dynamic languages on top of the Graal VM. New projects com.oracle.truffle.api for the API definition and com.oracle.truffle.api.test for API tests and documentation.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
191 return nodes;
a4b84ba6dc2e Introduction of the Truffle API for efficient implementation of dynamic languages on top of the Graal VM. New projects com.oracle.truffle.api for the API definition and com.oracle.truffle.api.test for API tests and documentation.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
192 }
a4b84ba6dc2e Introduction of the Truffle API for efficient implementation of dynamic languages on top of the Graal VM. New projects com.oracle.truffle.api for the API definition and com.oracle.truffle.api.test for API tests and documentation.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
193
19758
f682b9e6ca07 Truffle: expose non-atomic replace API to Truffle DSL to avoid multiple RootNode lookups for multiple replaces in one atomic block.
Christian Humer <christian.humer@gmail.com>
parents: 19607
diff changeset
194 public static <T extends Node> T nonAtomicReplace(Node oldNode, T newNode, CharSequence reason) {
f682b9e6ca07 Truffle: expose non-atomic replace API to Truffle DSL to avoid multiple RootNode lookups for multiple replaces in one atomic block.
Christian Humer <christian.humer@gmail.com>
parents: 19607
diff changeset
195 oldNode.replaceHelper(newNode, reason);
f682b9e6ca07 Truffle: expose non-atomic replace API to Truffle DSL to avoid multiple RootNode lookups for multiple replaces in one atomic block.
Christian Humer <christian.humer@gmail.com>
parents: 19607
diff changeset
196 return newNode;
f682b9e6ca07 Truffle: expose non-atomic replace API to Truffle DSL to avoid multiple RootNode lookups for multiple replaces in one atomic block.
Christian Humer <christian.humer@gmail.com>
parents: 19607
diff changeset
197 }
f682b9e6ca07 Truffle: expose non-atomic replace API to Truffle DSL to avoid multiple RootNode lookups for multiple replaces in one atomic block.
Christian Humer <christian.humer@gmail.com>
parents: 19607
diff changeset
198
11950
be0a33a631fa Truffle: fix node rewrite issue that can occur when a currently executing node is replaced in a recursive call.
Andreas Woess <andreas.woess@jku.at>
parents: 11907
diff changeset
199 public static boolean replaceChild(Node parent, Node oldChild, Node newChild) {
20130
8dc73c226c63 Truffle: cache NodeClass lookup in Node.
Christian Humer <christian.humer@gmail.com>
parents: 20129
diff changeset
200 NodeClass nodeClass = parent.getNodeClass();
7267
a4b84ba6dc2e Introduction of the Truffle API for efficient implementation of dynamic languages on top of the Graal VM. New projects com.oracle.truffle.api for the API definition and com.oracle.truffle.api.test for API tests and documentation.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
201
20129
5b7db8941fd7 Truffle: make NodeClass and NodeField a top-level class.
Christian Humer <christian.humer@gmail.com>
parents: 19974
diff changeset
202 for (NodeFieldAccessor nodeField : nodeClass.getChildFields()) {
18904
6ba170cb6f53 Truffle: channel all NodeUtil field accesses through NodeField class
Andreas Woess <andreas.woess@jku.at>
parents: 18751
diff changeset
203 if (nodeField.getObject(parent) == oldChild) {
6ba170cb6f53 Truffle: channel all NodeUtil field accesses through NodeField class
Andreas Woess <andreas.woess@jku.at>
parents: 18751
diff changeset
204 assert assertAssignable(nodeField, newChild);
6ba170cb6f53 Truffle: channel all NodeUtil field accesses through NodeField class
Andreas Woess <andreas.woess@jku.at>
parents: 18751
diff changeset
205 nodeField.putObject(parent, newChild);
11950
be0a33a631fa Truffle: fix node rewrite issue that can occur when a currently executing node is replaced in a recursive call.
Andreas Woess <andreas.woess@jku.at>
parents: 11907
diff changeset
206 return true;
7267
a4b84ba6dc2e Introduction of the Truffle API for efficient implementation of dynamic languages on top of the Graal VM. New projects com.oracle.truffle.api for the API definition and com.oracle.truffle.api.test for API tests and documentation.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
207 }
a4b84ba6dc2e Introduction of the Truffle API for efficient implementation of dynamic languages on top of the Graal VM. New projects com.oracle.truffle.api for the API definition and com.oracle.truffle.api.test for API tests and documentation.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
208 }
10742
99789440ce28 Truffle: Added assertions to replaceChild.
Christian Humer <christian.humer@gmail.com>
parents: 10704
diff changeset
209
20129
5b7db8941fd7 Truffle: make NodeClass and NodeField a top-level class.
Christian Humer <christian.humer@gmail.com>
parents: 19974
diff changeset
210 for (NodeFieldAccessor nodeField : nodeClass.getChildrenFields()) {
18904
6ba170cb6f53 Truffle: channel all NodeUtil field accesses through NodeField class
Andreas Woess <andreas.woess@jku.at>
parents: 18751
diff changeset
211 Object arrayObject = nodeField.getObject(parent);
10742
99789440ce28 Truffle: Added assertions to replaceChild.
Christian Humer <christian.humer@gmail.com>
parents: 10704
diff changeset
212 if (arrayObject != null) {
17275
846c059e3ecf Truffle: allow interface types in child fields
Andreas Woess <andreas.woess@jku.at>
parents: 16779
diff changeset
213 Object[] array = (Object[]) arrayObject;
10755
b221e31d7b0b Truffle: revise NodeUtil.replaceChild assertion
Andreas Woess <andreas.woess@jku.at>
parents: 10742
diff changeset
214 for (int i = 0; i < array.length; i++) {
b221e31d7b0b Truffle: revise NodeUtil.replaceChild assertion
Andreas Woess <andreas.woess@jku.at>
parents: 10742
diff changeset
215 if (array[i] == oldChild) {
18904
6ba170cb6f53 Truffle: channel all NodeUtil field accesses through NodeField class
Andreas Woess <andreas.woess@jku.at>
parents: 18751
diff changeset
216 assert assertAssignable(nodeField, newChild);
10755
b221e31d7b0b Truffle: revise NodeUtil.replaceChild assertion
Andreas Woess <andreas.woess@jku.at>
parents: 10742
diff changeset
217 array[i] = newChild;
11950
be0a33a631fa Truffle: fix node rewrite issue that can occur when a currently executing node is replaced in a recursive call.
Andreas Woess <andreas.woess@jku.at>
parents: 11907
diff changeset
218 return true;
7267
a4b84ba6dc2e Introduction of the Truffle API for efficient implementation of dynamic languages on top of the Graal VM. New projects com.oracle.truffle.api for the API definition and com.oracle.truffle.api.test for API tests and documentation.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
219 }
a4b84ba6dc2e Introduction of the Truffle API for efficient implementation of dynamic languages on top of the Graal VM. New projects com.oracle.truffle.api for the API definition and com.oracle.truffle.api.test for API tests and documentation.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
220 }
a4b84ba6dc2e Introduction of the Truffle API for efficient implementation of dynamic languages on top of the Graal VM. New projects com.oracle.truffle.api for the API definition and com.oracle.truffle.api.test for API tests and documentation.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
221 }
a4b84ba6dc2e Introduction of the Truffle API for efficient implementation of dynamic languages on top of the Graal VM. New projects com.oracle.truffle.api for the API definition and com.oracle.truffle.api.test for API tests and documentation.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
222 }
11950
be0a33a631fa Truffle: fix node rewrite issue that can occur when a currently executing node is replaced in a recursive call.
Andreas Woess <andreas.woess@jku.at>
parents: 11907
diff changeset
223 return false;
7267
a4b84ba6dc2e Introduction of the Truffle API for efficient implementation of dynamic languages on top of the Graal VM. New projects com.oracle.truffle.api for the API definition and com.oracle.truffle.api.test for API tests and documentation.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
224 }
a4b84ba6dc2e Introduction of the Truffle API for efficient implementation of dynamic languages on top of the Graal VM. New projects com.oracle.truffle.api for the API definition and com.oracle.truffle.api.test for API tests and documentation.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
225
20129
5b7db8941fd7 Truffle: make NodeClass and NodeField a top-level class.
Christian Humer <christian.humer@gmail.com>
parents: 19974
diff changeset
226 private static boolean assertAssignable(NodeFieldAccessor field, Object newValue) {
10742
99789440ce28 Truffle: Added assertions to replaceChild.
Christian Humer <christian.humer@gmail.com>
parents: 10704
diff changeset
227 if (newValue == null) {
99789440ce28 Truffle: Added assertions to replaceChild.
Christian Humer <christian.humer@gmail.com>
parents: 10704
diff changeset
228 return true;
99789440ce28 Truffle: Added assertions to replaceChild.
Christian Humer <christian.humer@gmail.com>
parents: 10704
diff changeset
229 }
18904
6ba170cb6f53 Truffle: channel all NodeUtil field accesses through NodeField class
Andreas Woess <andreas.woess@jku.at>
parents: 18751
diff changeset
230 if (field.getKind() == NodeFieldKind.CHILD) {
6ba170cb6f53 Truffle: channel all NodeUtil field accesses through NodeField class
Andreas Woess <andreas.woess@jku.at>
parents: 18751
diff changeset
231 if (field.getType().isAssignableFrom(newValue.getClass())) {
6ba170cb6f53 Truffle: channel all NodeUtil field accesses through NodeField class
Andreas Woess <andreas.woess@jku.at>
parents: 18751
diff changeset
232 return true;
6ba170cb6f53 Truffle: channel all NodeUtil field accesses through NodeField class
Andreas Woess <andreas.woess@jku.at>
parents: 18751
diff changeset
233 } else {
6ba170cb6f53 Truffle: channel all NodeUtil field accesses through NodeField class
Andreas Woess <andreas.woess@jku.at>
parents: 18751
diff changeset
234 assert false : "Child class " + newValue.getClass().getName() + " is not assignable to field \"" + field.getName() + "\" of type " + field.getType().getName();
6ba170cb6f53 Truffle: channel all NodeUtil field accesses through NodeField class
Andreas Woess <andreas.woess@jku.at>
parents: 18751
diff changeset
235 return false;
6ba170cb6f53 Truffle: channel all NodeUtil field accesses through NodeField class
Andreas Woess <andreas.woess@jku.at>
parents: 18751
diff changeset
236 }
6ba170cb6f53 Truffle: channel all NodeUtil field accesses through NodeField class
Andreas Woess <andreas.woess@jku.at>
parents: 18751
diff changeset
237 } else if (field.getKind() == NodeFieldKind.CHILDREN) {
6ba170cb6f53 Truffle: channel all NodeUtil field accesses through NodeField class
Andreas Woess <andreas.woess@jku.at>
parents: 18751
diff changeset
238 if (field.getType().getComponentType().isAssignableFrom(newValue.getClass())) {
6ba170cb6f53 Truffle: channel all NodeUtil field accesses through NodeField class
Andreas Woess <andreas.woess@jku.at>
parents: 18751
diff changeset
239 return true;
6ba170cb6f53 Truffle: channel all NodeUtil field accesses through NodeField class
Andreas Woess <andreas.woess@jku.at>
parents: 18751
diff changeset
240 } else {
6ba170cb6f53 Truffle: channel all NodeUtil field accesses through NodeField class
Andreas Woess <andreas.woess@jku.at>
parents: 18751
diff changeset
241 assert false : "Child class " + newValue.getClass().getName() + " is not assignable to field \"" + field.getName() + "\" of type " + field.getType().getName();
6ba170cb6f53 Truffle: channel all NodeUtil field accesses through NodeField class
Andreas Woess <andreas.woess@jku.at>
parents: 18751
diff changeset
242 return false;
10742
99789440ce28 Truffle: Added assertions to replaceChild.
Christian Humer <christian.humer@gmail.com>
parents: 10704
diff changeset
243 }
99789440ce28 Truffle: Added assertions to replaceChild.
Christian Humer <christian.humer@gmail.com>
parents: 10704
diff changeset
244 }
10755
b221e31d7b0b Truffle: revise NodeUtil.replaceChild assertion
Andreas Woess <andreas.woess@jku.at>
parents: 10742
diff changeset
245 throw new IllegalArgumentException();
10742
99789440ce28 Truffle: Added assertions to replaceChild.
Christian Humer <christian.humer@gmail.com>
parents: 10704
diff changeset
246 }
99789440ce28 Truffle: Added assertions to replaceChild.
Christian Humer <christian.humer@gmail.com>
parents: 10704
diff changeset
247
18984
0f462015296f Truffle: Additions to NodeUtil to support optional "safe" AST node replacement (and diagnostics when unsafe)
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 18905
diff changeset
248 /**
0f462015296f Truffle: Additions to NodeUtil to support optional "safe" AST node replacement (and diagnostics when unsafe)
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 18905
diff changeset
249 * Finds the field in a parent node, if any, that holds a specified child node.
0f462015296f Truffle: Additions to NodeUtil to support optional "safe" AST node replacement (and diagnostics when unsafe)
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 18905
diff changeset
250 *
0f462015296f Truffle: Additions to NodeUtil to support optional "safe" AST node replacement (and diagnostics when unsafe)
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 18905
diff changeset
251 * @return the field (possibly an array) holding the child, {@code null} if not found.
0f462015296f Truffle: Additions to NodeUtil to support optional "safe" AST node replacement (and diagnostics when unsafe)
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 18905
diff changeset
252 */
20129
5b7db8941fd7 Truffle: make NodeClass and NodeField a top-level class.
Christian Humer <christian.humer@gmail.com>
parents: 19974
diff changeset
253 public static NodeFieldAccessor findChildField(Node parent, Node child) {
18984
0f462015296f Truffle: Additions to NodeUtil to support optional "safe" AST node replacement (and diagnostics when unsafe)
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 18905
diff changeset
254 assert child != null;
20130
8dc73c226c63 Truffle: cache NodeClass lookup in Node.
Christian Humer <christian.humer@gmail.com>
parents: 20129
diff changeset
255 NodeClass parentNodeClass = parent.getNodeClass();
18984
0f462015296f Truffle: Additions to NodeUtil to support optional "safe" AST node replacement (and diagnostics when unsafe)
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 18905
diff changeset
256
20129
5b7db8941fd7 Truffle: make NodeClass and NodeField a top-level class.
Christian Humer <christian.humer@gmail.com>
parents: 19974
diff changeset
257 for (NodeFieldAccessor field : parentNodeClass.getChildFields()) {
19012
f5b83e7b2b4c Truffle: simplify NodeUtil.findChildField
Andreas Woess <andreas.woess@jku.at>
parents: 19010
diff changeset
258 if (field.getObject(parent) == child) {
f5b83e7b2b4c Truffle: simplify NodeUtil.findChildField
Andreas Woess <andreas.woess@jku.at>
parents: 19010
diff changeset
259 return field;
18984
0f462015296f Truffle: Additions to NodeUtil to support optional "safe" AST node replacement (and diagnostics when unsafe)
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 18905
diff changeset
260 }
0f462015296f Truffle: Additions to NodeUtil to support optional "safe" AST node replacement (and diagnostics when unsafe)
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 18905
diff changeset
261 }
0f462015296f Truffle: Additions to NodeUtil to support optional "safe" AST node replacement (and diagnostics when unsafe)
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 18905
diff changeset
262
20129
5b7db8941fd7 Truffle: make NodeClass and NodeField a top-level class.
Christian Humer <christian.humer@gmail.com>
parents: 19974
diff changeset
263 for (NodeFieldAccessor field : parentNodeClass.getChildrenFields()) {
19012
f5b83e7b2b4c Truffle: simplify NodeUtil.findChildField
Andreas Woess <andreas.woess@jku.at>
parents: 19010
diff changeset
264 Object arrayObject = field.getObject(parent);
18984
0f462015296f Truffle: Additions to NodeUtil to support optional "safe" AST node replacement (and diagnostics when unsafe)
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 18905
diff changeset
265 if (arrayObject != null) {
0f462015296f Truffle: Additions to NodeUtil to support optional "safe" AST node replacement (and diagnostics when unsafe)
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 18905
diff changeset
266 Object[] array = (Object[]) arrayObject;
0f462015296f Truffle: Additions to NodeUtil to support optional "safe" AST node replacement (and diagnostics when unsafe)
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 18905
diff changeset
267 for (int i = 0; i < array.length; i++) {
0f462015296f Truffle: Additions to NodeUtil to support optional "safe" AST node replacement (and diagnostics when unsafe)
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 18905
diff changeset
268 if (array[i] == child) {
19012
f5b83e7b2b4c Truffle: simplify NodeUtil.findChildField
Andreas Woess <andreas.woess@jku.at>
parents: 19010
diff changeset
269 return field;
18984
0f462015296f Truffle: Additions to NodeUtil to support optional "safe" AST node replacement (and diagnostics when unsafe)
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 18905
diff changeset
270 }
0f462015296f Truffle: Additions to NodeUtil to support optional "safe" AST node replacement (and diagnostics when unsafe)
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 18905
diff changeset
271 }
0f462015296f Truffle: Additions to NodeUtil to support optional "safe" AST node replacement (and diagnostics when unsafe)
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 18905
diff changeset
272 }
0f462015296f Truffle: Additions to NodeUtil to support optional "safe" AST node replacement (and diagnostics when unsafe)
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 18905
diff changeset
273 }
0f462015296f Truffle: Additions to NodeUtil to support optional "safe" AST node replacement (and diagnostics when unsafe)
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 18905
diff changeset
274 return null;
0f462015296f Truffle: Additions to NodeUtil to support optional "safe" AST node replacement (and diagnostics when unsafe)
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 18905
diff changeset
275 }
0f462015296f Truffle: Additions to NodeUtil to support optional "safe" AST node replacement (and diagnostics when unsafe)
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 18905
diff changeset
276
0f462015296f Truffle: Additions to NodeUtil to support optional "safe" AST node replacement (and diagnostics when unsafe)
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 18905
diff changeset
277 /**
19141
67d9e635102f Truffle/Instrumentation: refine checks for safe node replacement
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 19012
diff changeset
278 * Determines whether a proposed child replacement would be safe: structurally and type.
18984
0f462015296f Truffle: Additions to NodeUtil to support optional "safe" AST node replacement (and diagnostics when unsafe)
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 18905
diff changeset
279 */
0f462015296f Truffle: Additions to NodeUtil to support optional "safe" AST node replacement (and diagnostics when unsafe)
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 18905
diff changeset
280 public static boolean isReplacementSafe(Node parent, Node oldChild, Node newChild) {
0f462015296f Truffle: Additions to NodeUtil to support optional "safe" AST node replacement (and diagnostics when unsafe)
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 18905
diff changeset
281 assert newChild != null;
19141
67d9e635102f Truffle/Instrumentation: refine checks for safe node replacement
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 19012
diff changeset
282 if (parent != null) {
20129
5b7db8941fd7 Truffle: make NodeClass and NodeField a top-level class.
Christian Humer <christian.humer@gmail.com>
parents: 19974
diff changeset
283 final NodeFieldAccessor field = findChildField(parent, oldChild);
19141
67d9e635102f Truffle/Instrumentation: refine checks for safe node replacement
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 19012
diff changeset
284 if (field != null) {
67d9e635102f Truffle/Instrumentation: refine checks for safe node replacement
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 19012
diff changeset
285 switch (field.getKind()) {
67d9e635102f Truffle/Instrumentation: refine checks for safe node replacement
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 19012
diff changeset
286 case CHILD:
67d9e635102f Truffle/Instrumentation: refine checks for safe node replacement
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 19012
diff changeset
287 return field.getType().isAssignableFrom(newChild.getClass());
67d9e635102f Truffle/Instrumentation: refine checks for safe node replacement
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 19012
diff changeset
288 case CHILDREN:
67d9e635102f Truffle/Instrumentation: refine checks for safe node replacement
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 19012
diff changeset
289 return field.getType().getComponentType().isAssignableFrom(newChild.getClass());
67d9e635102f Truffle/Instrumentation: refine checks for safe node replacement
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 19012
diff changeset
290 default:
67d9e635102f Truffle/Instrumentation: refine checks for safe node replacement
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 19012
diff changeset
291 throw new IllegalStateException();
67d9e635102f Truffle/Instrumentation: refine checks for safe node replacement
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 19012
diff changeset
292 }
67d9e635102f Truffle/Instrumentation: refine checks for safe node replacement
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 19012
diff changeset
293 }
18984
0f462015296f Truffle: Additions to NodeUtil to support optional "safe" AST node replacement (and diagnostics when unsafe)
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 18905
diff changeset
294 }
19141
67d9e635102f Truffle/Instrumentation: refine checks for safe node replacement
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 19012
diff changeset
295 return false;
18984
0f462015296f Truffle: Additions to NodeUtil to support optional "safe" AST node replacement (and diagnostics when unsafe)
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 18905
diff changeset
296 }
0f462015296f Truffle: Additions to NodeUtil to support optional "safe" AST node replacement (and diagnostics when unsafe)
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 18905
diff changeset
297
19607
73811d1b4cd0 Truffle: add NodeUtil.forEachChild
Andreas Woess <andreas.woess@oracle.com>
parents: 19141
diff changeset
298 /**
73811d1b4cd0 Truffle: add NodeUtil.forEachChild
Andreas Woess <andreas.woess@oracle.com>
parents: 19141
diff changeset
299 * Executes a closure for every non-null child of the parent node.
73811d1b4cd0 Truffle: add NodeUtil.forEachChild
Andreas Woess <andreas.woess@oracle.com>
parents: 19141
diff changeset
300 *
73811d1b4cd0 Truffle: add NodeUtil.forEachChild
Andreas Woess <andreas.woess@oracle.com>
parents: 19141
diff changeset
301 * @return {@code true} if all children were visited, {@code false} otherwise
73811d1b4cd0 Truffle: add NodeUtil.forEachChild
Andreas Woess <andreas.woess@oracle.com>
parents: 19141
diff changeset
302 */
73811d1b4cd0 Truffle: add NodeUtil.forEachChild
Andreas Woess <andreas.woess@oracle.com>
parents: 19141
diff changeset
303 public static boolean forEachChild(Node parent, NodeVisitor visitor) {
73811d1b4cd0 Truffle: add NodeUtil.forEachChild
Andreas Woess <andreas.woess@oracle.com>
parents: 19141
diff changeset
304 Objects.requireNonNull(visitor);
20130
8dc73c226c63 Truffle: cache NodeClass lookup in Node.
Christian Humer <christian.humer@gmail.com>
parents: 20129
diff changeset
305 NodeClass parentNodeClass = parent.getNodeClass();
19607
73811d1b4cd0 Truffle: add NodeUtil.forEachChild
Andreas Woess <andreas.woess@oracle.com>
parents: 19141
diff changeset
306
20129
5b7db8941fd7 Truffle: make NodeClass and NodeField a top-level class.
Christian Humer <christian.humer@gmail.com>
parents: 19974
diff changeset
307 for (NodeFieldAccessor field : parentNodeClass.getChildFields()) {
19607
73811d1b4cd0 Truffle: add NodeUtil.forEachChild
Andreas Woess <andreas.woess@oracle.com>
parents: 19141
diff changeset
308 Object child = field.getObject(parent);
73811d1b4cd0 Truffle: add NodeUtil.forEachChild
Andreas Woess <andreas.woess@oracle.com>
parents: 19141
diff changeset
309 if (child != null) {
73811d1b4cd0 Truffle: add NodeUtil.forEachChild
Andreas Woess <andreas.woess@oracle.com>
parents: 19141
diff changeset
310 if (!visitor.visit((Node) child)) {
73811d1b4cd0 Truffle: add NodeUtil.forEachChild
Andreas Woess <andreas.woess@oracle.com>
parents: 19141
diff changeset
311 return false;
73811d1b4cd0 Truffle: add NodeUtil.forEachChild
Andreas Woess <andreas.woess@oracle.com>
parents: 19141
diff changeset
312 }
73811d1b4cd0 Truffle: add NodeUtil.forEachChild
Andreas Woess <andreas.woess@oracle.com>
parents: 19141
diff changeset
313 }
73811d1b4cd0 Truffle: add NodeUtil.forEachChild
Andreas Woess <andreas.woess@oracle.com>
parents: 19141
diff changeset
314 }
73811d1b4cd0 Truffle: add NodeUtil.forEachChild
Andreas Woess <andreas.woess@oracle.com>
parents: 19141
diff changeset
315
20129
5b7db8941fd7 Truffle: make NodeClass and NodeField a top-level class.
Christian Humer <christian.humer@gmail.com>
parents: 19974
diff changeset
316 for (NodeFieldAccessor field : parentNodeClass.getChildrenFields()) {
19607
73811d1b4cd0 Truffle: add NodeUtil.forEachChild
Andreas Woess <andreas.woess@oracle.com>
parents: 19141
diff changeset
317 Object arrayObject = field.getObject(parent);
73811d1b4cd0 Truffle: add NodeUtil.forEachChild
Andreas Woess <andreas.woess@oracle.com>
parents: 19141
diff changeset
318 if (arrayObject != null) {
73811d1b4cd0 Truffle: add NodeUtil.forEachChild
Andreas Woess <andreas.woess@oracle.com>
parents: 19141
diff changeset
319 Object[] array = (Object[]) arrayObject;
73811d1b4cd0 Truffle: add NodeUtil.forEachChild
Andreas Woess <andreas.woess@oracle.com>
parents: 19141
diff changeset
320 for (int i = 0; i < array.length; i++) {
73811d1b4cd0 Truffle: add NodeUtil.forEachChild
Andreas Woess <andreas.woess@oracle.com>
parents: 19141
diff changeset
321 Object child = array[i];
73811d1b4cd0 Truffle: add NodeUtil.forEachChild
Andreas Woess <andreas.woess@oracle.com>
parents: 19141
diff changeset
322 if (child != null) {
73811d1b4cd0 Truffle: add NodeUtil.forEachChild
Andreas Woess <andreas.woess@oracle.com>
parents: 19141
diff changeset
323 if (!visitor.visit((Node) child)) {
73811d1b4cd0 Truffle: add NodeUtil.forEachChild
Andreas Woess <andreas.woess@oracle.com>
parents: 19141
diff changeset
324 return false;
73811d1b4cd0 Truffle: add NodeUtil.forEachChild
Andreas Woess <andreas.woess@oracle.com>
parents: 19141
diff changeset
325 }
73811d1b4cd0 Truffle: add NodeUtil.forEachChild
Andreas Woess <andreas.woess@oracle.com>
parents: 19141
diff changeset
326 }
73811d1b4cd0 Truffle: add NodeUtil.forEachChild
Andreas Woess <andreas.woess@oracle.com>
parents: 19141
diff changeset
327 }
73811d1b4cd0 Truffle: add NodeUtil.forEachChild
Andreas Woess <andreas.woess@oracle.com>
parents: 19141
diff changeset
328 }
73811d1b4cd0 Truffle: add NodeUtil.forEachChild
Andreas Woess <andreas.woess@oracle.com>
parents: 19141
diff changeset
329 }
73811d1b4cd0 Truffle: add NodeUtil.forEachChild
Andreas Woess <andreas.woess@oracle.com>
parents: 19141
diff changeset
330
73811d1b4cd0 Truffle: add NodeUtil.forEachChild
Andreas Woess <andreas.woess@oracle.com>
parents: 19141
diff changeset
331 return true;
73811d1b4cd0 Truffle: add NodeUtil.forEachChild
Andreas Woess <andreas.woess@oracle.com>
parents: 19141
diff changeset
332 }
73811d1b4cd0 Truffle: add NodeUtil.forEachChild
Andreas Woess <andreas.woess@oracle.com>
parents: 19141
diff changeset
333
20131
4b12d5355811 Truffle: do not use iterators for visitors.
Christian Humer <christian.humer@gmail.com>
parents: 20130
diff changeset
334 static boolean forEachChildRecursive(Node parent, NodeVisitor visitor) {
4b12d5355811 Truffle: do not use iterators for visitors.
Christian Humer <christian.humer@gmail.com>
parents: 20130
diff changeset
335 NodeClass parentNodeClass = parent.getNodeClass();
4b12d5355811 Truffle: do not use iterators for visitors.
Christian Humer <christian.humer@gmail.com>
parents: 20130
diff changeset
336
4b12d5355811 Truffle: do not use iterators for visitors.
Christian Humer <christian.humer@gmail.com>
parents: 20130
diff changeset
337 for (NodeFieldAccessor field : parentNodeClass.getChildFields()) {
4b12d5355811 Truffle: do not use iterators for visitors.
Christian Humer <christian.humer@gmail.com>
parents: 20130
diff changeset
338 if (!visitChild((Node) field.getObject(parent), visitor)) {
4b12d5355811 Truffle: do not use iterators for visitors.
Christian Humer <christian.humer@gmail.com>
parents: 20130
diff changeset
339 return false;
4b12d5355811 Truffle: do not use iterators for visitors.
Christian Humer <christian.humer@gmail.com>
parents: 20130
diff changeset
340 }
4b12d5355811 Truffle: do not use iterators for visitors.
Christian Humer <christian.humer@gmail.com>
parents: 20130
diff changeset
341 }
4b12d5355811 Truffle: do not use iterators for visitors.
Christian Humer <christian.humer@gmail.com>
parents: 20130
diff changeset
342
4b12d5355811 Truffle: do not use iterators for visitors.
Christian Humer <christian.humer@gmail.com>
parents: 20130
diff changeset
343 for (NodeFieldAccessor field : parentNodeClass.getChildrenFields()) {
4b12d5355811 Truffle: do not use iterators for visitors.
Christian Humer <christian.humer@gmail.com>
parents: 20130
diff changeset
344 Object arrayObject = field.getObject(parent);
4b12d5355811 Truffle: do not use iterators for visitors.
Christian Humer <christian.humer@gmail.com>
parents: 20130
diff changeset
345 if (arrayObject == null) {
4b12d5355811 Truffle: do not use iterators for visitors.
Christian Humer <christian.humer@gmail.com>
parents: 20130
diff changeset
346 continue;
4b12d5355811 Truffle: do not use iterators for visitors.
Christian Humer <christian.humer@gmail.com>
parents: 20130
diff changeset
347 }
4b12d5355811 Truffle: do not use iterators for visitors.
Christian Humer <christian.humer@gmail.com>
parents: 20130
diff changeset
348 Object[] array = (Object[]) arrayObject;
4b12d5355811 Truffle: do not use iterators for visitors.
Christian Humer <christian.humer@gmail.com>
parents: 20130
diff changeset
349 for (int i = 0; i < array.length; i++) {
4b12d5355811 Truffle: do not use iterators for visitors.
Christian Humer <christian.humer@gmail.com>
parents: 20130
diff changeset
350 if (!visitChild((Node) array[i], visitor)) {
4b12d5355811 Truffle: do not use iterators for visitors.
Christian Humer <christian.humer@gmail.com>
parents: 20130
diff changeset
351 return false;
4b12d5355811 Truffle: do not use iterators for visitors.
Christian Humer <christian.humer@gmail.com>
parents: 20130
diff changeset
352 }
4b12d5355811 Truffle: do not use iterators for visitors.
Christian Humer <christian.humer@gmail.com>
parents: 20130
diff changeset
353 }
4b12d5355811 Truffle: do not use iterators for visitors.
Christian Humer <christian.humer@gmail.com>
parents: 20130
diff changeset
354 }
4b12d5355811 Truffle: do not use iterators for visitors.
Christian Humer <christian.humer@gmail.com>
parents: 20130
diff changeset
355
4b12d5355811 Truffle: do not use iterators for visitors.
Christian Humer <christian.humer@gmail.com>
parents: 20130
diff changeset
356 return true;
4b12d5355811 Truffle: do not use iterators for visitors.
Christian Humer <christian.humer@gmail.com>
parents: 20130
diff changeset
357 }
4b12d5355811 Truffle: do not use iterators for visitors.
Christian Humer <christian.humer@gmail.com>
parents: 20130
diff changeset
358
4b12d5355811 Truffle: do not use iterators for visitors.
Christian Humer <christian.humer@gmail.com>
parents: 20130
diff changeset
359 private static boolean visitChild(Node child, NodeVisitor visitor) {
4b12d5355811 Truffle: do not use iterators for visitors.
Christian Humer <christian.humer@gmail.com>
parents: 20130
diff changeset
360 if (child == null) {
4b12d5355811 Truffle: do not use iterators for visitors.
Christian Humer <christian.humer@gmail.com>
parents: 20130
diff changeset
361 return true;
4b12d5355811 Truffle: do not use iterators for visitors.
Christian Humer <christian.humer@gmail.com>
parents: 20130
diff changeset
362 }
4b12d5355811 Truffle: do not use iterators for visitors.
Christian Humer <christian.humer@gmail.com>
parents: 20130
diff changeset
363 if (!visitor.visit(child)) {
4b12d5355811 Truffle: do not use iterators for visitors.
Christian Humer <christian.humer@gmail.com>
parents: 20130
diff changeset
364 return false;
4b12d5355811 Truffle: do not use iterators for visitors.
Christian Humer <christian.humer@gmail.com>
parents: 20130
diff changeset
365 }
4b12d5355811 Truffle: do not use iterators for visitors.
Christian Humer <christian.humer@gmail.com>
parents: 20130
diff changeset
366 if (!forEachChildRecursive(child, visitor)) {
4b12d5355811 Truffle: do not use iterators for visitors.
Christian Humer <christian.humer@gmail.com>
parents: 20130
diff changeset
367 return false;
4b12d5355811 Truffle: do not use iterators for visitors.
Christian Humer <christian.humer@gmail.com>
parents: 20130
diff changeset
368 }
4b12d5355811 Truffle: do not use iterators for visitors.
Christian Humer <christian.humer@gmail.com>
parents: 20130
diff changeset
369 return true;
4b12d5355811 Truffle: do not use iterators for visitors.
Christian Humer <christian.humer@gmail.com>
parents: 20130
diff changeset
370 }
4b12d5355811 Truffle: do not use iterators for visitors.
Christian Humer <christian.humer@gmail.com>
parents: 20130
diff changeset
371
7267
a4b84ba6dc2e Introduction of the Truffle API for efficient implementation of dynamic languages on top of the Graal VM. New projects com.oracle.truffle.api for the API definition and com.oracle.truffle.api.test for API tests and documentation.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
372 /** Returns all declared fields in the class hierarchy. */
20129
5b7db8941fd7 Truffle: make NodeClass and NodeField a top-level class.
Christian Humer <christian.humer@gmail.com>
parents: 19974
diff changeset
373 static Field[] getAllFields(Class<? extends Object> clazz) {
7267
a4b84ba6dc2e Introduction of the Truffle API for efficient implementation of dynamic languages on top of the Graal VM. New projects com.oracle.truffle.api for the API definition and com.oracle.truffle.api.test for API tests and documentation.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
374 Field[] declaredFields = clazz.getDeclaredFields();
a4b84ba6dc2e Introduction of the Truffle API for efficient implementation of dynamic languages on top of the Graal VM. New projects com.oracle.truffle.api for the API definition and com.oracle.truffle.api.test for API tests and documentation.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
375 if (clazz.getSuperclass() != null) {
a4b84ba6dc2e Introduction of the Truffle API for efficient implementation of dynamic languages on top of the Graal VM. New projects com.oracle.truffle.api for the API definition and com.oracle.truffle.api.test for API tests and documentation.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
376 return concat(getAllFields(clazz.getSuperclass()), declaredFields);
a4b84ba6dc2e Introduction of the Truffle API for efficient implementation of dynamic languages on top of the Graal VM. New projects com.oracle.truffle.api for the API definition and com.oracle.truffle.api.test for API tests and documentation.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
377 }
a4b84ba6dc2e Introduction of the Truffle API for efficient implementation of dynamic languages on top of the Graal VM. New projects com.oracle.truffle.api for the API definition and com.oracle.truffle.api.test for API tests and documentation.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
378 return declaredFields;
a4b84ba6dc2e Introduction of the Truffle API for efficient implementation of dynamic languages on top of the Graal VM. New projects com.oracle.truffle.api for the API definition and com.oracle.truffle.api.test for API tests and documentation.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
379 }
a4b84ba6dc2e Introduction of the Truffle API for efficient implementation of dynamic languages on top of the Graal VM. New projects com.oracle.truffle.api for the API definition and com.oracle.truffle.api.test for API tests and documentation.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
380
a4b84ba6dc2e Introduction of the Truffle API for efficient implementation of dynamic languages on top of the Graal VM. New projects com.oracle.truffle.api for the API definition and com.oracle.truffle.api.test for API tests and documentation.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
381 public static <T> T[] concat(T[] first, T[] second) {
a4b84ba6dc2e Introduction of the Truffle API for efficient implementation of dynamic languages on top of the Graal VM. New projects com.oracle.truffle.api for the API definition and com.oracle.truffle.api.test for API tests and documentation.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
382 T[] result = Arrays.copyOf(first, first.length + second.length);
a4b84ba6dc2e Introduction of the Truffle API for efficient implementation of dynamic languages on top of the Graal VM. New projects com.oracle.truffle.api for the API definition and com.oracle.truffle.api.test for API tests and documentation.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
383 System.arraycopy(second, 0, result, first.length, second.length);
a4b84ba6dc2e Introduction of the Truffle API for efficient implementation of dynamic languages on top of the Graal VM. New projects com.oracle.truffle.api for the API definition and com.oracle.truffle.api.test for API tests and documentation.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
384 return result;
a4b84ba6dc2e Introduction of the Truffle API for efficient implementation of dynamic languages on top of the Graal VM. New projects com.oracle.truffle.api for the API definition and com.oracle.truffle.api.test for API tests and documentation.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
385 }
a4b84ba6dc2e Introduction of the Truffle API for efficient implementation of dynamic languages on top of the Graal VM. New projects com.oracle.truffle.api for the API definition and com.oracle.truffle.api.test for API tests and documentation.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
386
11622
ccf047a30f18 Truffle: NodeUtil.getNthParent.
Chris Seaton <chris.seaton@oracle.com>
parents: 11279
diff changeset
387 /**
ccf047a30f18 Truffle: NodeUtil.getNthParent.
Chris Seaton <chris.seaton@oracle.com>
parents: 11279
diff changeset
388 * Get the nth parent of a node, where the 0th parent is the node itself. Returns null if there
ccf047a30f18 Truffle: NodeUtil.getNthParent.
Chris Seaton <chris.seaton@oracle.com>
parents: 11279
diff changeset
389 * are less than n ancestors.
ccf047a30f18 Truffle: NodeUtil.getNthParent.
Chris Seaton <chris.seaton@oracle.com>
parents: 11279
diff changeset
390 */
ccf047a30f18 Truffle: NodeUtil.getNthParent.
Chris Seaton <chris.seaton@oracle.com>
parents: 11279
diff changeset
391 public static Node getNthParent(Node node, int n) {
ccf047a30f18 Truffle: NodeUtil.getNthParent.
Chris Seaton <chris.seaton@oracle.com>
parents: 11279
diff changeset
392 Node parent = node;
ccf047a30f18 Truffle: NodeUtil.getNthParent.
Chris Seaton <chris.seaton@oracle.com>
parents: 11279
diff changeset
393
ccf047a30f18 Truffle: NodeUtil.getNthParent.
Chris Seaton <chris.seaton@oracle.com>
parents: 11279
diff changeset
394 for (int i = 0; i < n; i++) {
ccf047a30f18 Truffle: NodeUtil.getNthParent.
Chris Seaton <chris.seaton@oracle.com>
parents: 11279
diff changeset
395 parent = parent.getParent();
ccf047a30f18 Truffle: NodeUtil.getNthParent.
Chris Seaton <chris.seaton@oracle.com>
parents: 11279
diff changeset
396
ccf047a30f18 Truffle: NodeUtil.getNthParent.
Chris Seaton <chris.seaton@oracle.com>
parents: 11279
diff changeset
397 if (parent == null) {
ccf047a30f18 Truffle: NodeUtil.getNthParent.
Chris Seaton <chris.seaton@oracle.com>
parents: 11279
diff changeset
398 return null;
ccf047a30f18 Truffle: NodeUtil.getNthParent.
Chris Seaton <chris.seaton@oracle.com>
parents: 11279
diff changeset
399 }
ccf047a30f18 Truffle: NodeUtil.getNthParent.
Chris Seaton <chris.seaton@oracle.com>
parents: 11279
diff changeset
400 }
ccf047a30f18 Truffle: NodeUtil.getNthParent.
Chris Seaton <chris.seaton@oracle.com>
parents: 11279
diff changeset
401
ccf047a30f18 Truffle: NodeUtil.getNthParent.
Chris Seaton <chris.seaton@oracle.com>
parents: 11279
diff changeset
402 return parent;
ccf047a30f18 Truffle: NodeUtil.getNthParent.
Chris Seaton <chris.seaton@oracle.com>
parents: 11279
diff changeset
403 }
ccf047a30f18 Truffle: NodeUtil.getNthParent.
Chris Seaton <chris.seaton@oracle.com>
parents: 11279
diff changeset
404
7267
a4b84ba6dc2e Introduction of the Truffle API for efficient implementation of dynamic languages on top of the Graal VM. New projects com.oracle.truffle.api for the API definition and com.oracle.truffle.api.test for API tests and documentation.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
405 /** find annotation in class/interface hierarchy. */
7530
5e3d1a68664e applied mx eclipseformat to all Java files
Doug Simon <doug.simon@oracle.com>
parents: 7267
diff changeset
406 public static <T extends Annotation> T findAnnotation(Class<?> clazz, Class<T> annotationClass) {
7267
a4b84ba6dc2e Introduction of the Truffle API for efficient implementation of dynamic languages on top of the Graal VM. New projects com.oracle.truffle.api for the API definition and com.oracle.truffle.api.test for API tests and documentation.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
407 if (clazz.getAnnotation(annotationClass) != null) {
a4b84ba6dc2e Introduction of the Truffle API for efficient implementation of dynamic languages on top of the Graal VM. New projects com.oracle.truffle.api for the API definition and com.oracle.truffle.api.test for API tests and documentation.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
408 return clazz.getAnnotation(annotationClass);
a4b84ba6dc2e Introduction of the Truffle API for efficient implementation of dynamic languages on top of the Graal VM. New projects com.oracle.truffle.api for the API definition and com.oracle.truffle.api.test for API tests and documentation.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
409 } else {
7530
5e3d1a68664e applied mx eclipseformat to all Java files
Doug Simon <doug.simon@oracle.com>
parents: 7267
diff changeset
410 for (Class<?> intf : clazz.getInterfaces()) {
7267
a4b84ba6dc2e Introduction of the Truffle API for efficient implementation of dynamic languages on top of the Graal VM. New projects com.oracle.truffle.api for the API definition and com.oracle.truffle.api.test for API tests and documentation.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
411 if (intf.getAnnotation(annotationClass) != null) {
a4b84ba6dc2e Introduction of the Truffle API for efficient implementation of dynamic languages on top of the Graal VM. New projects com.oracle.truffle.api for the API definition and com.oracle.truffle.api.test for API tests and documentation.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
412 return intf.getAnnotation(annotationClass);
a4b84ba6dc2e Introduction of the Truffle API for efficient implementation of dynamic languages on top of the Graal VM. New projects com.oracle.truffle.api for the API definition and com.oracle.truffle.api.test for API tests and documentation.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
413 }
a4b84ba6dc2e Introduction of the Truffle API for efficient implementation of dynamic languages on top of the Graal VM. New projects com.oracle.truffle.api for the API definition and com.oracle.truffle.api.test for API tests and documentation.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
414 }
a4b84ba6dc2e Introduction of the Truffle API for efficient implementation of dynamic languages on top of the Graal VM. New projects com.oracle.truffle.api for the API definition and com.oracle.truffle.api.test for API tests and documentation.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
415 if (clazz.getSuperclass() != null) {
a4b84ba6dc2e Introduction of the Truffle API for efficient implementation of dynamic languages on top of the Graal VM. New projects com.oracle.truffle.api for the API definition and com.oracle.truffle.api.test for API tests and documentation.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
416 return findAnnotation(clazz.getSuperclass(), annotationClass);
a4b84ba6dc2e Introduction of the Truffle API for efficient implementation of dynamic languages on top of the Graal VM. New projects com.oracle.truffle.api for the API definition and com.oracle.truffle.api.test for API tests and documentation.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
417 }
a4b84ba6dc2e Introduction of the Truffle API for efficient implementation of dynamic languages on top of the Graal VM. New projects com.oracle.truffle.api for the API definition and com.oracle.truffle.api.test for API tests and documentation.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
418 }
a4b84ba6dc2e Introduction of the Truffle API for efficient implementation of dynamic languages on top of the Graal VM. New projects com.oracle.truffle.api for the API definition and com.oracle.truffle.api.test for API tests and documentation.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
419 return null;
a4b84ba6dc2e Introduction of the Truffle API for efficient implementation of dynamic languages on top of the Graal VM. New projects com.oracle.truffle.api for the API definition and com.oracle.truffle.api.test for API tests and documentation.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
420 }
a4b84ba6dc2e Introduction of the Truffle API for efficient implementation of dynamic languages on top of the Graal VM. New projects com.oracle.truffle.api for the API definition and com.oracle.truffle.api.test for API tests and documentation.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
421
12666
d9c34e8337f4 Truffle: cleanup of NodeUtil#findParent; added NodeUtil#findAllParents
Christian Humer <christian.humer@gmail.com>
parents: 11950
diff changeset
422 public static <T> T findParent(Node start, Class<T> clazz) {
d9c34e8337f4 Truffle: cleanup of NodeUtil#findParent; added NodeUtil#findAllParents
Christian Humer <christian.humer@gmail.com>
parents: 11950
diff changeset
423 Node parent = start.getParent();
d9c34e8337f4 Truffle: cleanup of NodeUtil#findParent; added NodeUtil#findAllParents
Christian Humer <christian.humer@gmail.com>
parents: 11950
diff changeset
424 if (parent == null) {
d9c34e8337f4 Truffle: cleanup of NodeUtil#findParent; added NodeUtil#findAllParents
Christian Humer <christian.humer@gmail.com>
parents: 11950
diff changeset
425 return null;
d9c34e8337f4 Truffle: cleanup of NodeUtil#findParent; added NodeUtil#findAllParents
Christian Humer <christian.humer@gmail.com>
parents: 11950
diff changeset
426 } else if (clazz.isInstance(parent)) {
d9c34e8337f4 Truffle: cleanup of NodeUtil#findParent; added NodeUtil#findAllParents
Christian Humer <christian.humer@gmail.com>
parents: 11950
diff changeset
427 return clazz.cast(parent);
7267
a4b84ba6dc2e Introduction of the Truffle API for efficient implementation of dynamic languages on top of the Graal VM. New projects com.oracle.truffle.api for the API definition and com.oracle.truffle.api.test for API tests and documentation.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
428 } else {
12666
d9c34e8337f4 Truffle: cleanup of NodeUtil#findParent; added NodeUtil#findAllParents
Christian Humer <christian.humer@gmail.com>
parents: 11950
diff changeset
429 return findParent(parent, clazz);
7267
a4b84ba6dc2e Introduction of the Truffle API for efficient implementation of dynamic languages on top of the Graal VM. New projects com.oracle.truffle.api for the API definition and com.oracle.truffle.api.test for API tests and documentation.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
430 }
a4b84ba6dc2e Introduction of the Truffle API for efficient implementation of dynamic languages on top of the Graal VM. New projects com.oracle.truffle.api for the API definition and com.oracle.truffle.api.test for API tests and documentation.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
431 }
a4b84ba6dc2e Introduction of the Truffle API for efficient implementation of dynamic languages on top of the Graal VM. New projects com.oracle.truffle.api for the API definition and com.oracle.truffle.api.test for API tests and documentation.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
432
12666
d9c34e8337f4 Truffle: cleanup of NodeUtil#findParent; added NodeUtil#findAllParents
Christian Humer <christian.humer@gmail.com>
parents: 11950
diff changeset
433 public static <T> List<T> findAllParents(Node start, Class<T> clazz) {
d9c34e8337f4 Truffle: cleanup of NodeUtil#findParent; added NodeUtil#findAllParents
Christian Humer <christian.humer@gmail.com>
parents: 11950
diff changeset
434 List<T> parents = new ArrayList<>();
d9c34e8337f4 Truffle: cleanup of NodeUtil#findParent; added NodeUtil#findAllParents
Christian Humer <christian.humer@gmail.com>
parents: 11950
diff changeset
435 T parent = findParent(start, clazz);
d9c34e8337f4 Truffle: cleanup of NodeUtil#findParent; added NodeUtil#findAllParents
Christian Humer <christian.humer@gmail.com>
parents: 11950
diff changeset
436 while (parent != null) {
d9c34e8337f4 Truffle: cleanup of NodeUtil#findParent; added NodeUtil#findAllParents
Christian Humer <christian.humer@gmail.com>
parents: 11950
diff changeset
437 parents.add(parent);
d9c34e8337f4 Truffle: cleanup of NodeUtil#findParent; added NodeUtil#findAllParents
Christian Humer <christian.humer@gmail.com>
parents: 11950
diff changeset
438 parent = findParent((Node) parent, clazz);
7267
a4b84ba6dc2e Introduction of the Truffle API for efficient implementation of dynamic languages on top of the Graal VM. New projects com.oracle.truffle.api for the API definition and com.oracle.truffle.api.test for API tests and documentation.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
439 }
12666
d9c34e8337f4 Truffle: cleanup of NodeUtil#findParent; added NodeUtil#findAllParents
Christian Humer <christian.humer@gmail.com>
parents: 11950
diff changeset
440 return parents;
7267
a4b84ba6dc2e Introduction of the Truffle API for efficient implementation of dynamic languages on top of the Graal VM. New projects com.oracle.truffle.api for the API definition and com.oracle.truffle.api.test for API tests and documentation.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
441 }
a4b84ba6dc2e Introduction of the Truffle API for efficient implementation of dynamic languages on top of the Graal VM. New projects com.oracle.truffle.api for the API definition and com.oracle.truffle.api.test for API tests and documentation.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
442
12666
d9c34e8337f4 Truffle: cleanup of NodeUtil#findParent; added NodeUtil#findAllParents
Christian Humer <christian.humer@gmail.com>
parents: 11950
diff changeset
443 public static List<Node> collectNodes(Node parent, Node child) {
d9c34e8337f4 Truffle: cleanup of NodeUtil#findParent; added NodeUtil#findAllParents
Christian Humer <christian.humer@gmail.com>
parents: 11950
diff changeset
444 List<Node> nodes = new ArrayList<>();
d9c34e8337f4 Truffle: cleanup of NodeUtil#findParent; added NodeUtil#findAllParents
Christian Humer <christian.humer@gmail.com>
parents: 11950
diff changeset
445 Node current = child;
d9c34e8337f4 Truffle: cleanup of NodeUtil#findParent; added NodeUtil#findAllParents
Christian Humer <christian.humer@gmail.com>
parents: 11950
diff changeset
446 while (current != null) {
d9c34e8337f4 Truffle: cleanup of NodeUtil#findParent; added NodeUtil#findAllParents
Christian Humer <christian.humer@gmail.com>
parents: 11950
diff changeset
447 nodes.add(current);
d9c34e8337f4 Truffle: cleanup of NodeUtil#findParent; added NodeUtil#findAllParents
Christian Humer <christian.humer@gmail.com>
parents: 11950
diff changeset
448 if (current == parent) {
d9c34e8337f4 Truffle: cleanup of NodeUtil#findParent; added NodeUtil#findAllParents
Christian Humer <christian.humer@gmail.com>
parents: 11950
diff changeset
449 return nodes;
d9c34e8337f4 Truffle: cleanup of NodeUtil#findParent; added NodeUtil#findAllParents
Christian Humer <christian.humer@gmail.com>
parents: 11950
diff changeset
450 }
d9c34e8337f4 Truffle: cleanup of NodeUtil#findParent; added NodeUtil#findAllParents
Christian Humer <christian.humer@gmail.com>
parents: 11950
diff changeset
451 current = current.getParent();
d9c34e8337f4 Truffle: cleanup of NodeUtil#findParent; added NodeUtil#findAllParents
Christian Humer <christian.humer@gmail.com>
parents: 11950
diff changeset
452 }
d9c34e8337f4 Truffle: cleanup of NodeUtil#findParent; added NodeUtil#findAllParents
Christian Humer <christian.humer@gmail.com>
parents: 11950
diff changeset
453 throw new IllegalArgumentException("Node " + parent + " is not a parent of " + child + ".");
d9c34e8337f4 Truffle: cleanup of NodeUtil#findParent; added NodeUtil#findAllParents
Christian Humer <christian.humer@gmail.com>
parents: 11950
diff changeset
454 }
d9c34e8337f4 Truffle: cleanup of NodeUtil#findParent; added NodeUtil#findAllParents
Christian Humer <christian.humer@gmail.com>
parents: 11950
diff changeset
455
9537
e2965e5cd474 Fixed Truffle child iterator should only iterate children which are annotated with @Child or @Children.
Christian Humer <christian.humer@gmail.com>
parents: 9487
diff changeset
456 public static <T> T findFirstNodeInstance(Node root, Class<T> clazz) {
21650
45ed86c4d127 Truffle: change NodeUtil.findFirstNodeInstance to include root in potential matches
Andreas Woess <andreas.woess@oracle.com>
parents: 21469
diff changeset
457 if (clazz.isInstance(root)) {
45ed86c4d127 Truffle: change NodeUtil.findFirstNodeInstance to include root in potential matches
Andreas Woess <andreas.woess@oracle.com>
parents: 21469
diff changeset
458 return clazz.cast(root);
45ed86c4d127 Truffle: change NodeUtil.findFirstNodeInstance to include root in potential matches
Andreas Woess <andreas.woess@oracle.com>
parents: 21469
diff changeset
459 }
45ed86c4d127 Truffle: change NodeUtil.findFirstNodeInstance to include root in potential matches
Andreas Woess <andreas.woess@oracle.com>
parents: 21469
diff changeset
460 for (Node child : root.getChildren()) {
45ed86c4d127 Truffle: change NodeUtil.findFirstNodeInstance to include root in potential matches
Andreas Woess <andreas.woess@oracle.com>
parents: 21469
diff changeset
461 T node = findFirstNodeInstance(child, clazz);
45ed86c4d127 Truffle: change NodeUtil.findFirstNodeInstance to include root in potential matches
Andreas Woess <andreas.woess@oracle.com>
parents: 21469
diff changeset
462 if (node != null) {
45ed86c4d127 Truffle: change NodeUtil.findFirstNodeInstance to include root in potential matches
Andreas Woess <andreas.woess@oracle.com>
parents: 21469
diff changeset
463 return node;
7267
a4b84ba6dc2e Introduction of the Truffle API for efficient implementation of dynamic languages on top of the Graal VM. New projects com.oracle.truffle.api for the API definition and com.oracle.truffle.api.test for API tests and documentation.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
464 }
a4b84ba6dc2e Introduction of the Truffle API for efficient implementation of dynamic languages on top of the Graal VM. New projects com.oracle.truffle.api for the API definition and com.oracle.truffle.api.test for API tests and documentation.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
465 }
a4b84ba6dc2e Introduction of the Truffle API for efficient implementation of dynamic languages on top of the Graal VM. New projects com.oracle.truffle.api for the API definition and com.oracle.truffle.api.test for API tests and documentation.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
466 return null;
a4b84ba6dc2e Introduction of the Truffle API for efficient implementation of dynamic languages on top of the Graal VM. New projects com.oracle.truffle.api for the API definition and com.oracle.truffle.api.test for API tests and documentation.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
467 }
a4b84ba6dc2e Introduction of the Truffle API for efficient implementation of dynamic languages on top of the Graal VM. New projects com.oracle.truffle.api for the API definition and com.oracle.truffle.api.test for API tests and documentation.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
468
16465
456abab80eb5 Truffle: remove obsolete NodeUtil.findNodeInstancesInFunction (functionally equivalent to findAllNodeInstances)
Andreas Woess <andreas.woess@jku.at>
parents: 16464
diff changeset
469 public static <T> List<T> findAllNodeInstances(final Node root, final Class<T> clazz) {
7267
a4b84ba6dc2e Introduction of the Truffle API for efficient implementation of dynamic languages on top of the Graal VM. New projects com.oracle.truffle.api for the API definition and com.oracle.truffle.api.test for API tests and documentation.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
470 final List<T> nodeList = new ArrayList<>();
a4b84ba6dc2e Introduction of the Truffle API for efficient implementation of dynamic languages on top of the Graal VM. New projects com.oracle.truffle.api for the API definition and com.oracle.truffle.api.test for API tests and documentation.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
471 root.accept(new NodeVisitor() {
a4b84ba6dc2e Introduction of the Truffle API for efficient implementation of dynamic languages on top of the Graal VM. New projects com.oracle.truffle.api for the API definition and com.oracle.truffle.api.test for API tests and documentation.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
472 public boolean visit(Node node) {
a4b84ba6dc2e Introduction of the Truffle API for efficient implementation of dynamic languages on top of the Graal VM. New projects com.oracle.truffle.api for the API definition and com.oracle.truffle.api.test for API tests and documentation.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
473 if (clazz.isInstance(node)) {
16465
456abab80eb5 Truffle: remove obsolete NodeUtil.findNodeInstancesInFunction (functionally equivalent to findAllNodeInstances)
Andreas Woess <andreas.woess@jku.at>
parents: 16464
diff changeset
474 nodeList.add(clazz.cast(node));
7267
a4b84ba6dc2e Introduction of the Truffle API for efficient implementation of dynamic languages on top of the Graal VM. New projects com.oracle.truffle.api for the API definition and com.oracle.truffle.api.test for API tests and documentation.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
475 }
a4b84ba6dc2e Introduction of the Truffle API for efficient implementation of dynamic languages on top of the Graal VM. New projects com.oracle.truffle.api for the API definition and com.oracle.truffle.api.test for API tests and documentation.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
476 return true;
a4b84ba6dc2e Introduction of the Truffle API for efficient implementation of dynamic languages on top of the Graal VM. New projects com.oracle.truffle.api for the API definition and com.oracle.truffle.api.test for API tests and documentation.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
477 }
a4b84ba6dc2e Introduction of the Truffle API for efficient implementation of dynamic languages on top of the Graal VM. New projects com.oracle.truffle.api for the API definition and com.oracle.truffle.api.test for API tests and documentation.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
478 });
a4b84ba6dc2e Introduction of the Truffle API for efficient implementation of dynamic languages on top of the Graal VM. New projects com.oracle.truffle.api for the API definition and com.oracle.truffle.api.test for API tests and documentation.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
479 return nodeList;
a4b84ba6dc2e Introduction of the Truffle API for efficient implementation of dynamic languages on top of the Graal VM. New projects com.oracle.truffle.api for the API definition and com.oracle.truffle.api.test for API tests and documentation.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
480 }
a4b84ba6dc2e Introduction of the Truffle API for efficient implementation of dynamic languages on top of the Graal VM. New projects com.oracle.truffle.api for the API definition and com.oracle.truffle.api.test for API tests and documentation.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
481
10704
84b944726df2 Truffle: introduce NodeUtil.countNodes
Andreas Woess <andreas.woess@jku.at>
parents: 9754
diff changeset
482 public static int countNodes(Node root) {
20131
4b12d5355811 Truffle: do not use iterators for visitors.
Christian Humer <christian.humer@gmail.com>
parents: 20130
diff changeset
483 return countNodes(root, NodeCountFilter.NO_FILTER);
13995
e455fc531ec2 Truffle: Added API in NodeUtil to count nodes restricted to a Kind.
Christian Humer <christian.humer@gmail.com>
parents: 13983
diff changeset
484 }
e455fc531ec2 Truffle: Added API in NodeUtil to count nodes restricted to a Kind.
Christian Humer <christian.humer@gmail.com>
parents: 13983
diff changeset
485
14569
5e04917e6616 Truffle: renamed NodeFilter to NodeCountFilter and changed it to have inclusive implementation semantics.
Christian Humer <christian.humer@gmail.com>
parents: 14565
diff changeset
486 public static int countNodes(Node root, NodeCountFilter filter) {
20131
4b12d5355811 Truffle: do not use iterators for visitors.
Christian Humer <christian.humer@gmail.com>
parents: 20130
diff changeset
487 NodeCounter counter = new NodeCounter(filter);
4b12d5355811 Truffle: do not use iterators for visitors.
Christian Humer <christian.humer@gmail.com>
parents: 20130
diff changeset
488 root.accept(counter);
4b12d5355811 Truffle: do not use iterators for visitors.
Christian Humer <christian.humer@gmail.com>
parents: 20130
diff changeset
489 return counter.count;
10793
6e12e0ace0d5 Truffle: add NodeUtil.countNodes overload that counts nodes of a specific type
Andreas Woess <andreas.woess@jku.at>
parents: 10756
diff changeset
490 }
6e12e0ace0d5 Truffle: add NodeUtil.countNodes overload that counts nodes of a specific type
Andreas Woess <andreas.woess@jku.at>
parents: 10756
diff changeset
491
14569
5e04917e6616 Truffle: renamed NodeFilter to NodeCountFilter and changed it to have inclusive implementation semantics.
Christian Humer <christian.humer@gmail.com>
parents: 14565
diff changeset
492 public interface NodeCountFilter {
14565
9c01fabfb167 Truffle: Removed deprecated API; Added NodeFilter to customize filter when counting nodes.
Christian Humer <christian.humer@gmail.com>
parents: 14564
diff changeset
493
20131
4b12d5355811 Truffle: do not use iterators for visitors.
Christian Humer <christian.humer@gmail.com>
parents: 20130
diff changeset
494 NodeCountFilter NO_FILTER = new NodeCountFilter() {
4b12d5355811 Truffle: do not use iterators for visitors.
Christian Humer <christian.humer@gmail.com>
parents: 20130
diff changeset
495
4b12d5355811 Truffle: do not use iterators for visitors.
Christian Humer <christian.humer@gmail.com>
parents: 20130
diff changeset
496 public boolean isCounted(Node node) {
4b12d5355811 Truffle: do not use iterators for visitors.
Christian Humer <christian.humer@gmail.com>
parents: 20130
diff changeset
497 return true;
4b12d5355811 Truffle: do not use iterators for visitors.
Christian Humer <christian.humer@gmail.com>
parents: 20130
diff changeset
498 }
4b12d5355811 Truffle: do not use iterators for visitors.
Christian Humer <christian.humer@gmail.com>
parents: 20130
diff changeset
499 };
4b12d5355811 Truffle: do not use iterators for visitors.
Christian Humer <christian.humer@gmail.com>
parents: 20130
diff changeset
500
14569
5e04917e6616 Truffle: renamed NodeFilter to NodeCountFilter and changed it to have inclusive implementation semantics.
Christian Humer <christian.humer@gmail.com>
parents: 14565
diff changeset
501 boolean isCounted(Node node);
14565
9c01fabfb167 Truffle: Removed deprecated API; Added NodeFilter to customize filter when counting nodes.
Christian Humer <christian.humer@gmail.com>
parents: 14564
diff changeset
502
10704
84b944726df2 Truffle: introduce NodeUtil.countNodes
Andreas Woess <andreas.woess@jku.at>
parents: 9754
diff changeset
503 }
84b944726df2 Truffle: introduce NodeUtil.countNodes
Andreas Woess <andreas.woess@jku.at>
parents: 9754
diff changeset
504
9754
98b004bf3985 Refactoring of NodeUtil to centralize the use of reflection
Christian Wimmer <christian.wimmer@oracle.com>
parents: 9538
diff changeset
505 public static String printCompactTreeToString(Node node) {
98b004bf3985 Refactoring of NodeUtil to centralize the use of reflection
Christian Wimmer <christian.wimmer@oracle.com>
parents: 9538
diff changeset
506 StringWriter out = new StringWriter();
98b004bf3985 Refactoring of NodeUtil to centralize the use of reflection
Christian Wimmer <christian.wimmer@oracle.com>
parents: 9538
diff changeset
507 printCompactTree(new PrintWriter(out), null, node, 1);
98b004bf3985 Refactoring of NodeUtil to centralize the use of reflection
Christian Wimmer <christian.wimmer@oracle.com>
parents: 9538
diff changeset
508 return out.toString();
7267
a4b84ba6dc2e Introduction of the Truffle API for efficient implementation of dynamic languages on top of the Graal VM. New projects com.oracle.truffle.api for the API definition and com.oracle.truffle.api.test for API tests and documentation.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
509 }
a4b84ba6dc2e Introduction of the Truffle API for efficient implementation of dynamic languages on top of the Graal VM. New projects com.oracle.truffle.api for the API definition and com.oracle.truffle.api.test for API tests and documentation.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
510
9754
98b004bf3985 Refactoring of NodeUtil to centralize the use of reflection
Christian Wimmer <christian.wimmer@oracle.com>
parents: 9538
diff changeset
511 public static void printCompactTree(OutputStream out, Node node) {
98b004bf3985 Refactoring of NodeUtil to centralize the use of reflection
Christian Wimmer <christian.wimmer@oracle.com>
parents: 9538
diff changeset
512 printCompactTree(new PrintWriter(out), null, node, 1);
7267
a4b84ba6dc2e Introduction of the Truffle API for efficient implementation of dynamic languages on top of the Graal VM. New projects com.oracle.truffle.api for the API definition and com.oracle.truffle.api.test for API tests and documentation.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
513 }
a4b84ba6dc2e Introduction of the Truffle API for efficient implementation of dynamic languages on top of the Graal VM. New projects com.oracle.truffle.api for the API definition and com.oracle.truffle.api.test for API tests and documentation.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
514
9754
98b004bf3985 Refactoring of NodeUtil to centralize the use of reflection
Christian Wimmer <christian.wimmer@oracle.com>
parents: 9538
diff changeset
515 private static void printCompactTree(PrintWriter p, Node parent, Node node, int level) {
9538
e6fe35d64b71 Implemented a method to produce a compact string representation of the truffle tree which just shows the nodes and their children but no data fields.
Christian Humer <christian.humer@gmail.com>
parents: 9537
diff changeset
516 if (node == null) {
e6fe35d64b71 Implemented a method to produce a compact string representation of the truffle tree which just shows the nodes and their children but no data fields.
Christian Humer <christian.humer@gmail.com>
parents: 9537
diff changeset
517 return;
e6fe35d64b71 Implemented a method to produce a compact string representation of the truffle tree which just shows the nodes and their children but no data fields.
Christian Humer <christian.humer@gmail.com>
parents: 9537
diff changeset
518 }
e6fe35d64b71 Implemented a method to produce a compact string representation of the truffle tree which just shows the nodes and their children but no data fields.
Christian Humer <christian.humer@gmail.com>
parents: 9537
diff changeset
519 for (int i = 0; i < level; i++) {
e6fe35d64b71 Implemented a method to produce a compact string representation of the truffle tree which just shows the nodes and their children but no data fields.
Christian Humer <christian.humer@gmail.com>
parents: 9537
diff changeset
520 p.print(" ");
e6fe35d64b71 Implemented a method to produce a compact string representation of the truffle tree which just shows the nodes and their children but no data fields.
Christian Humer <christian.humer@gmail.com>
parents: 9537
diff changeset
521 }
e6fe35d64b71 Implemented a method to produce a compact string representation of the truffle tree which just shows the nodes and their children but no data fields.
Christian Humer <christian.humer@gmail.com>
parents: 9537
diff changeset
522 if (parent == null) {
10845
e9248ebb1d79 Truffle: ignore null children in NodeUtil.findNodeChildren; refactoring.
Andreas Woess <andreas.woess@jku.at>
parents: 10793
diff changeset
523 p.println(nodeName(node));
9538
e6fe35d64b71 Implemented a method to produce a compact string representation of the truffle tree which just shows the nodes and their children but no data fields.
Christian Humer <christian.humer@gmail.com>
parents: 9537
diff changeset
524 } else {
17275
846c059e3ecf Truffle: allow interface types in child fields
Andreas Woess <andreas.woess@jku.at>
parents: 16779
diff changeset
525 p.print(getNodeFieldName(parent, node, "unknownField"));
9538
e6fe35d64b71 Implemented a method to produce a compact string representation of the truffle tree which just shows the nodes and their children but no data fields.
Christian Humer <christian.humer@gmail.com>
parents: 9537
diff changeset
526 p.print(" = ");
10845
e9248ebb1d79 Truffle: ignore null children in NodeUtil.findNodeChildren; refactoring.
Andreas Woess <andreas.woess@jku.at>
parents: 10793
diff changeset
527 p.println(nodeName(node));
9538
e6fe35d64b71 Implemented a method to produce a compact string representation of the truffle tree which just shows the nodes and their children but no data fields.
Christian Humer <christian.humer@gmail.com>
parents: 9537
diff changeset
528 }
e6fe35d64b71 Implemented a method to produce a compact string representation of the truffle tree which just shows the nodes and their children but no data fields.
Christian Humer <christian.humer@gmail.com>
parents: 9537
diff changeset
529
e6fe35d64b71 Implemented a method to produce a compact string representation of the truffle tree which just shows the nodes and their children but no data fields.
Christian Humer <christian.humer@gmail.com>
parents: 9537
diff changeset
530 for (Node child : node.getChildren()) {
e6fe35d64b71 Implemented a method to produce a compact string representation of the truffle tree which just shows the nodes and their children but no data fields.
Christian Humer <christian.humer@gmail.com>
parents: 9537
diff changeset
531 printCompactTree(p, node, child, level + 1);
e6fe35d64b71 Implemented a method to produce a compact string representation of the truffle tree which just shows the nodes and their children but no data fields.
Christian Humer <christian.humer@gmail.com>
parents: 9537
diff changeset
532 }
9754
98b004bf3985 Refactoring of NodeUtil to centralize the use of reflection
Christian Wimmer <christian.wimmer@oracle.com>
parents: 9538
diff changeset
533 p.flush();
9538
e6fe35d64b71 Implemented a method to produce a compact string representation of the truffle tree which just shows the nodes and their children but no data fields.
Christian Humer <christian.humer@gmail.com>
parents: 9537
diff changeset
534 }
e6fe35d64b71 Implemented a method to produce a compact string representation of the truffle tree which just shows the nodes and their children but no data fields.
Christian Humer <christian.humer@gmail.com>
parents: 9537
diff changeset
535
11848
099af41815ea Truffle: add NodeUtil.printSourceAttributionTree() for debugging
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 11809
diff changeset
536 public static String printSourceAttributionTree(Node node) {
099af41815ea Truffle: add NodeUtil.printSourceAttributionTree() for debugging
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 11809
diff changeset
537 StringWriter out = new StringWriter();
099af41815ea Truffle: add NodeUtil.printSourceAttributionTree() for debugging
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 11809
diff changeset
538 printSourceAttributionTree(new PrintWriter(out), null, node, 1);
099af41815ea Truffle: add NodeUtil.printSourceAttributionTree() for debugging
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 11809
diff changeset
539 return out.toString();
099af41815ea Truffle: add NodeUtil.printSourceAttributionTree() for debugging
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 11809
diff changeset
540 }
099af41815ea Truffle: add NodeUtil.printSourceAttributionTree() for debugging
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 11809
diff changeset
541
099af41815ea Truffle: add NodeUtil.printSourceAttributionTree() for debugging
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 11809
diff changeset
542 public static void printSourceAttributionTree(OutputStream out, Node node) {
099af41815ea Truffle: add NodeUtil.printSourceAttributionTree() for debugging
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 11809
diff changeset
543 printSourceAttributionTree(new PrintWriter(out), null, node, 1);
099af41815ea Truffle: add NodeUtil.printSourceAttributionTree() for debugging
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 11809
diff changeset
544 }
099af41815ea Truffle: add NodeUtil.printSourceAttributionTree() for debugging
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 11809
diff changeset
545
21469
286aef83a9a7 Replacing PrintStream with PrintWriter in the simple language
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 20131
diff changeset
546 public static void printSourceAttributionTree(PrintWriter out, Node node) {
286aef83a9a7 Replacing PrintStream with PrintWriter in the simple language
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 20131
diff changeset
547 printSourceAttributionTree(out, null, node, 1);
286aef83a9a7 Replacing PrintStream with PrintWriter in the simple language
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 20131
diff changeset
548 }
286aef83a9a7 Replacing PrintStream with PrintWriter in the simple language
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 20131
diff changeset
549
11848
099af41815ea Truffle: add NodeUtil.printSourceAttributionTree() for debugging
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 11809
diff changeset
550 private static void printSourceAttributionTree(PrintWriter p, Node parent, Node node, int level) {
099af41815ea Truffle: add NodeUtil.printSourceAttributionTree() for debugging
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 11809
diff changeset
551 if (node == null) {
099af41815ea Truffle: add NodeUtil.printSourceAttributionTree() for debugging
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 11809
diff changeset
552 return;
099af41815ea Truffle: add NodeUtil.printSourceAttributionTree() for debugging
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 11809
diff changeset
553 }
099af41815ea Truffle: add NodeUtil.printSourceAttributionTree() for debugging
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 11809
diff changeset
554 if (parent == null) {
099af41815ea Truffle: add NodeUtil.printSourceAttributionTree() for debugging
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 11809
diff changeset
555 // Add some preliminary information before starting with the root node
099af41815ea Truffle: add NodeUtil.printSourceAttributionTree() for debugging
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 11809
diff changeset
556 final SourceSection sourceSection = node.getSourceSection();
099af41815ea Truffle: add NodeUtil.printSourceAttributionTree() for debugging
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 11809
diff changeset
557 if (sourceSection != null) {
099af41815ea Truffle: add NodeUtil.printSourceAttributionTree() for debugging
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 11809
diff changeset
558 final String txt = sourceSection.getSource().getCode();
11907
873da100d113 Truffle: another minor tweak to NodeUtil.printSourceAttributionTree
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 11850
diff changeset
559 p.println("Full source len=(" + txt.length() + ") ___" + txt + "___");
11848
099af41815ea Truffle: add NodeUtil.printSourceAttributionTree() for debugging
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 11809
diff changeset
560 p.println("AST source attribution:");
099af41815ea Truffle: add NodeUtil.printSourceAttributionTree() for debugging
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 11809
diff changeset
561 }
099af41815ea Truffle: add NodeUtil.printSourceAttributionTree() for debugging
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 11809
diff changeset
562 }
099af41815ea Truffle: add NodeUtil.printSourceAttributionTree() for debugging
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 11809
diff changeset
563 final StringBuilder sb = new StringBuilder();
099af41815ea Truffle: add NodeUtil.printSourceAttributionTree() for debugging
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 11809
diff changeset
564 for (int i = 0; i < level; i++) {
11850
e68922869732 Truffle: minor legibility improvement in NodeUtil.printSourceAttributionTree()
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 11848
diff changeset
565 sb.append("| ");
11848
099af41815ea Truffle: add NodeUtil.printSourceAttributionTree() for debugging
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 11809
diff changeset
566 }
099af41815ea Truffle: add NodeUtil.printSourceAttributionTree() for debugging
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 11809
diff changeset
567
099af41815ea Truffle: add NodeUtil.printSourceAttributionTree() for debugging
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 11809
diff changeset
568 if (parent != null) {
17275
846c059e3ecf Truffle: allow interface types in child fields
Andreas Woess <andreas.woess@jku.at>
parents: 16779
diff changeset
569 sb.append(getNodeFieldName(parent, node, ""));
11848
099af41815ea Truffle: add NodeUtil.printSourceAttributionTree() for debugging
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 11809
diff changeset
570 }
099af41815ea Truffle: add NodeUtil.printSourceAttributionTree() for debugging
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 11809
diff changeset
571
099af41815ea Truffle: add NodeUtil.printSourceAttributionTree() for debugging
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 11809
diff changeset
572 sb.append(" (" + node.getClass().getSimpleName() + ") ");
18485
e3c95cbbb50c Truffle Instrumentation: major API revision, based around the Probe and Instrument classes; add Instrumentable API for language implementors, with most details automated; reimplemented to handle AST splitting automatically; more JUnit tests.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 18332
diff changeset
573
e3c95cbbb50c Truffle Instrumentation: major API revision, based around the Probe and Instrument classes; add Instrumentable API for language implementors, with most details automated; reimplemented to handle AST splitting automatically; more JUnit tests.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 18332
diff changeset
574 sb.append(printSyntaxTags(node));
e3c95cbbb50c Truffle Instrumentation: major API revision, based around the Probe and Instrument classes; add Instrumentable API for language implementors, with most details automated; reimplemented to handle AST splitting automatically; more JUnit tests.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 18332
diff changeset
575
11848
099af41815ea Truffle: add NodeUtil.printSourceAttributionTree() for debugging
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 11809
diff changeset
576 sb.append(displaySourceAttribution(node));
099af41815ea Truffle: add NodeUtil.printSourceAttributionTree() for debugging
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 11809
diff changeset
577 p.println(sb.toString());
099af41815ea Truffle: add NodeUtil.printSourceAttributionTree() for debugging
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 11809
diff changeset
578
099af41815ea Truffle: add NodeUtil.printSourceAttributionTree() for debugging
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 11809
diff changeset
579 for (Node child : node.getChildren()) {
099af41815ea Truffle: add NodeUtil.printSourceAttributionTree() for debugging
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 11809
diff changeset
580 printSourceAttributionTree(p, node, child, level + 1);
099af41815ea Truffle: add NodeUtil.printSourceAttributionTree() for debugging
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 11809
diff changeset
581 }
099af41815ea Truffle: add NodeUtil.printSourceAttributionTree() for debugging
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 11809
diff changeset
582 p.flush();
099af41815ea Truffle: add NodeUtil.printSourceAttributionTree() for debugging
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 11809
diff changeset
583 }
099af41815ea Truffle: add NodeUtil.printSourceAttributionTree() for debugging
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 11809
diff changeset
584
17275
846c059e3ecf Truffle: allow interface types in child fields
Andreas Woess <andreas.woess@jku.at>
parents: 16779
diff changeset
585 private static String getNodeFieldName(Node parent, Node node, String defaultName) {
20130
8dc73c226c63 Truffle: cache NodeClass lookup in Node.
Christian Humer <christian.humer@gmail.com>
parents: 20129
diff changeset
586 NodeFieldAccessor[] fields = parent.getNodeClass().getFields();
20129
5b7db8941fd7 Truffle: make NodeClass and NodeField a top-level class.
Christian Humer <christian.humer@gmail.com>
parents: 19974
diff changeset
587 for (NodeFieldAccessor field : fields) {
17275
846c059e3ecf Truffle: allow interface types in child fields
Andreas Woess <andreas.woess@jku.at>
parents: 16779
diff changeset
588 Object value = field.loadValue(parent);
846c059e3ecf Truffle: allow interface types in child fields
Andreas Woess <andreas.woess@jku.at>
parents: 16779
diff changeset
589 if (field.getKind() == NodeFieldKind.CHILD && value == node) {
846c059e3ecf Truffle: allow interface types in child fields
Andreas Woess <andreas.woess@jku.at>
parents: 16779
diff changeset
590 return field.getName();
846c059e3ecf Truffle: allow interface types in child fields
Andreas Woess <andreas.woess@jku.at>
parents: 16779
diff changeset
591 } else if (field.getKind() == NodeFieldKind.CHILDREN) {
846c059e3ecf Truffle: allow interface types in child fields
Andreas Woess <andreas.woess@jku.at>
parents: 16779
diff changeset
592 int index = 0;
846c059e3ecf Truffle: allow interface types in child fields
Andreas Woess <andreas.woess@jku.at>
parents: 16779
diff changeset
593 for (Object arrayNode : (Object[]) value) {
846c059e3ecf Truffle: allow interface types in child fields
Andreas Woess <andreas.woess@jku.at>
parents: 16779
diff changeset
594 if (arrayNode == node) {
846c059e3ecf Truffle: allow interface types in child fields
Andreas Woess <andreas.woess@jku.at>
parents: 16779
diff changeset
595 return field.getName() + "[" + index + "]";
846c059e3ecf Truffle: allow interface types in child fields
Andreas Woess <andreas.woess@jku.at>
parents: 16779
diff changeset
596 }
846c059e3ecf Truffle: allow interface types in child fields
Andreas Woess <andreas.woess@jku.at>
parents: 16779
diff changeset
597 index++;
846c059e3ecf Truffle: allow interface types in child fields
Andreas Woess <andreas.woess@jku.at>
parents: 16779
diff changeset
598 }
846c059e3ecf Truffle: allow interface types in child fields
Andreas Woess <andreas.woess@jku.at>
parents: 16779
diff changeset
599 }
846c059e3ecf Truffle: allow interface types in child fields
Andreas Woess <andreas.woess@jku.at>
parents: 16779
diff changeset
600 }
846c059e3ecf Truffle: allow interface types in child fields
Andreas Woess <andreas.woess@jku.at>
parents: 16779
diff changeset
601 return defaultName;
846c059e3ecf Truffle: allow interface types in child fields
Andreas Woess <andreas.woess@jku.at>
parents: 16779
diff changeset
602 }
846c059e3ecf Truffle: allow interface types in child fields
Andreas Woess <andreas.woess@jku.at>
parents: 16779
diff changeset
603
7267
a4b84ba6dc2e Introduction of the Truffle API for efficient implementation of dynamic languages on top of the Graal VM. New projects com.oracle.truffle.api for the API definition and com.oracle.truffle.api.test for API tests and documentation.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
604 /**
18485
e3c95cbbb50c Truffle Instrumentation: major API revision, based around the Probe and Instrument classes; add Instrumentable API for language implementors, with most details automated; reimplemented to handle AST splitting automatically; more JUnit tests.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 18332
diff changeset
605 * Returns a string listing the {@linkplain SyntaxTag syntax tags}, if any, associated with a
e3c95cbbb50c Truffle Instrumentation: major API revision, based around the Probe and Instrument classes; add Instrumentable API for language implementors, with most details automated; reimplemented to handle AST splitting automatically; more JUnit tests.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 18332
diff changeset
606 * node:
e3c95cbbb50c Truffle Instrumentation: major API revision, based around the Probe and Instrument classes; add Instrumentable API for language implementors, with most details automated; reimplemented to handle AST splitting automatically; more JUnit tests.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 18332
diff changeset
607 * <ul>
e3c95cbbb50c Truffle Instrumentation: major API revision, based around the Probe and Instrument classes; add Instrumentable API for language implementors, with most details automated; reimplemented to handle AST splitting automatically; more JUnit tests.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 18332
diff changeset
608 * <li>"[{@linkplain StandardSyntaxTag#STATEMENT STATEMENT},
e3c95cbbb50c Truffle Instrumentation: major API revision, based around the Probe and Instrument classes; add Instrumentable API for language implementors, with most details automated; reimplemented to handle AST splitting automatically; more JUnit tests.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 18332
diff changeset
609 * {@linkplain StandardSyntaxTag#ASSIGNMENT ASSIGNMENT}]" if tags have been applied;</li>
e3c95cbbb50c Truffle Instrumentation: major API revision, based around the Probe and Instrument classes; add Instrumentable API for language implementors, with most details automated; reimplemented to handle AST splitting automatically; more JUnit tests.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 18332
diff changeset
610 * <li>"[]" if the node supports tags, but none are present; and</li>
e3c95cbbb50c Truffle Instrumentation: major API revision, based around the Probe and Instrument classes; add Instrumentable API for language implementors, with most details automated; reimplemented to handle AST splitting automatically; more JUnit tests.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 18332
diff changeset
611 * <li>"" if the node does not support tags.</li>
e3c95cbbb50c Truffle Instrumentation: major API revision, based around the Probe and Instrument classes; add Instrumentable API for language implementors, with most details automated; reimplemented to handle AST splitting automatically; more JUnit tests.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 18332
diff changeset
612 * </ul>
e3c95cbbb50c Truffle Instrumentation: major API revision, based around the Probe and Instrument classes; add Instrumentable API for language implementors, with most details automated; reimplemented to handle AST splitting automatically; more JUnit tests.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 18332
diff changeset
613 */
e3c95cbbb50c Truffle Instrumentation: major API revision, based around the Probe and Instrument classes; add Instrumentable API for language implementors, with most details automated; reimplemented to handle AST splitting automatically; more JUnit tests.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 18332
diff changeset
614 public static String printSyntaxTags(final Object node) {
e3c95cbbb50c Truffle Instrumentation: major API revision, based around the Probe and Instrument classes; add Instrumentable API for language implementors, with most details automated; reimplemented to handle AST splitting automatically; more JUnit tests.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 18332
diff changeset
615 if (node instanceof WrapperNode) {
e3c95cbbb50c Truffle Instrumentation: major API revision, based around the Probe and Instrument classes; add Instrumentable API for language implementors, with most details automated; reimplemented to handle AST splitting automatically; more JUnit tests.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 18332
diff changeset
616 final Probe probe = ((WrapperNode) node).getProbe();
e3c95cbbb50c Truffle Instrumentation: major API revision, based around the Probe and Instrument classes; add Instrumentable API for language implementors, with most details automated; reimplemented to handle AST splitting automatically; more JUnit tests.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 18332
diff changeset
617 final Collection<SyntaxTag> syntaxTags = probe.getSyntaxTags();
e3c95cbbb50c Truffle Instrumentation: major API revision, based around the Probe and Instrument classes; add Instrumentable API for language implementors, with most details automated; reimplemented to handle AST splitting automatically; more JUnit tests.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 18332
diff changeset
618 final StringBuilder sb = new StringBuilder();
e3c95cbbb50c Truffle Instrumentation: major API revision, based around the Probe and Instrument classes; add Instrumentable API for language implementors, with most details automated; reimplemented to handle AST splitting automatically; more JUnit tests.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 18332
diff changeset
619 String prefix = "";
e3c95cbbb50c Truffle Instrumentation: major API revision, based around the Probe and Instrument classes; add Instrumentable API for language implementors, with most details automated; reimplemented to handle AST splitting automatically; more JUnit tests.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 18332
diff changeset
620 sb.append("[");
e3c95cbbb50c Truffle Instrumentation: major API revision, based around the Probe and Instrument classes; add Instrumentable API for language implementors, with most details automated; reimplemented to handle AST splitting automatically; more JUnit tests.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 18332
diff changeset
621 for (SyntaxTag tag : syntaxTags) {
e3c95cbbb50c Truffle Instrumentation: major API revision, based around the Probe and Instrument classes; add Instrumentable API for language implementors, with most details automated; reimplemented to handle AST splitting automatically; more JUnit tests.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 18332
diff changeset
622 sb.append(prefix);
e3c95cbbb50c Truffle Instrumentation: major API revision, based around the Probe and Instrument classes; add Instrumentable API for language implementors, with most details automated; reimplemented to handle AST splitting automatically; more JUnit tests.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 18332
diff changeset
623 prefix = ",";
e3c95cbbb50c Truffle Instrumentation: major API revision, based around the Probe and Instrument classes; add Instrumentable API for language implementors, with most details automated; reimplemented to handle AST splitting automatically; more JUnit tests.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 18332
diff changeset
624 sb.append(tag.toString());
e3c95cbbb50c Truffle Instrumentation: major API revision, based around the Probe and Instrument classes; add Instrumentable API for language implementors, with most details automated; reimplemented to handle AST splitting automatically; more JUnit tests.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 18332
diff changeset
625 }
e3c95cbbb50c Truffle Instrumentation: major API revision, based around the Probe and Instrument classes; add Instrumentable API for language implementors, with most details automated; reimplemented to handle AST splitting automatically; more JUnit tests.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 18332
diff changeset
626 sb.append("]");
e3c95cbbb50c Truffle Instrumentation: major API revision, based around the Probe and Instrument classes; add Instrumentable API for language implementors, with most details automated; reimplemented to handle AST splitting automatically; more JUnit tests.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 18332
diff changeset
627 return sb.toString();
e3c95cbbb50c Truffle Instrumentation: major API revision, based around the Probe and Instrument classes; add Instrumentable API for language implementors, with most details automated; reimplemented to handle AST splitting automatically; more JUnit tests.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 18332
diff changeset
628 }
e3c95cbbb50c Truffle Instrumentation: major API revision, based around the Probe and Instrument classes; add Instrumentable API for language implementors, with most details automated; reimplemented to handle AST splitting automatically; more JUnit tests.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 18332
diff changeset
629 return "";
e3c95cbbb50c Truffle Instrumentation: major API revision, based around the Probe and Instrument classes; add Instrumentable API for language implementors, with most details automated; reimplemented to handle AST splitting automatically; more JUnit tests.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 18332
diff changeset
630 }
e3c95cbbb50c Truffle Instrumentation: major API revision, based around the Probe and Instrument classes; add Instrumentable API for language implementors, with most details automated; reimplemented to handle AST splitting automatically; more JUnit tests.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 18332
diff changeset
631
e3c95cbbb50c Truffle Instrumentation: major API revision, based around the Probe and Instrument classes; add Instrumentable API for language implementors, with most details automated; reimplemented to handle AST splitting automatically; more JUnit tests.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 18332
diff changeset
632 /**
9754
98b004bf3985 Refactoring of NodeUtil to centralize the use of reflection
Christian Wimmer <christian.wimmer@oracle.com>
parents: 9538
diff changeset
633 * Prints a human readable form of a {@link Node} AST to the given {@link PrintStream}. This
98b004bf3985 Refactoring of NodeUtil to centralize the use of reflection
Christian Wimmer <christian.wimmer@oracle.com>
parents: 9538
diff changeset
634 * print method does not check for cycles in the node structure.
15090
07e7aae05983 Truffle: context sensitive inlining cleanup
Christian Humer <christian.humer@gmail.com>
parents: 15089
diff changeset
635 *
9754
98b004bf3985 Refactoring of NodeUtil to centralize the use of reflection
Christian Wimmer <christian.wimmer@oracle.com>
parents: 9538
diff changeset
636 * @param out the stream to print to.
98b004bf3985 Refactoring of NodeUtil to centralize the use of reflection
Christian Wimmer <christian.wimmer@oracle.com>
parents: 9538
diff changeset
637 * @param node the root node to write
7267
a4b84ba6dc2e Introduction of the Truffle API for efficient implementation of dynamic languages on top of the Graal VM. New projects com.oracle.truffle.api for the API definition and com.oracle.truffle.api.test for API tests and documentation.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
638 */
9754
98b004bf3985 Refactoring of NodeUtil to centralize the use of reflection
Christian Wimmer <christian.wimmer@oracle.com>
parents: 9538
diff changeset
639 public static void printTree(OutputStream out, Node node) {
98b004bf3985 Refactoring of NodeUtil to centralize the use of reflection
Christian Wimmer <christian.wimmer@oracle.com>
parents: 9538
diff changeset
640 printTree(new PrintWriter(out), node);
7267
a4b84ba6dc2e Introduction of the Truffle API for efficient implementation of dynamic languages on top of the Graal VM. New projects com.oracle.truffle.api for the API definition and com.oracle.truffle.api.test for API tests and documentation.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
641 }
a4b84ba6dc2e Introduction of the Truffle API for efficient implementation of dynamic languages on top of the Graal VM. New projects com.oracle.truffle.api for the API definition and com.oracle.truffle.api.test for API tests and documentation.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
642
9754
98b004bf3985 Refactoring of NodeUtil to centralize the use of reflection
Christian Wimmer <christian.wimmer@oracle.com>
parents: 9538
diff changeset
643 public static String printTreeToString(Node node) {
98b004bf3985 Refactoring of NodeUtil to centralize the use of reflection
Christian Wimmer <christian.wimmer@oracle.com>
parents: 9538
diff changeset
644 StringWriter out = new StringWriter();
98b004bf3985 Refactoring of NodeUtil to centralize the use of reflection
Christian Wimmer <christian.wimmer@oracle.com>
parents: 9538
diff changeset
645 printTree(new PrintWriter(out), node);
98b004bf3985 Refactoring of NodeUtil to centralize the use of reflection
Christian Wimmer <christian.wimmer@oracle.com>
parents: 9538
diff changeset
646 return out.toString();
98b004bf3985 Refactoring of NodeUtil to centralize the use of reflection
Christian Wimmer <christian.wimmer@oracle.com>
parents: 9538
diff changeset
647 }
98b004bf3985 Refactoring of NodeUtil to centralize the use of reflection
Christian Wimmer <christian.wimmer@oracle.com>
parents: 9538
diff changeset
648
98b004bf3985 Refactoring of NodeUtil to centralize the use of reflection
Christian Wimmer <christian.wimmer@oracle.com>
parents: 9538
diff changeset
649 public static void printTree(PrintWriter p, Node node) {
98b004bf3985 Refactoring of NodeUtil to centralize the use of reflection
Christian Wimmer <christian.wimmer@oracle.com>
parents: 9538
diff changeset
650 printTree(p, node, 1);
98b004bf3985 Refactoring of NodeUtil to centralize the use of reflection
Christian Wimmer <christian.wimmer@oracle.com>
parents: 9538
diff changeset
651 p.println();
98b004bf3985 Refactoring of NodeUtil to centralize the use of reflection
Christian Wimmer <christian.wimmer@oracle.com>
parents: 9538
diff changeset
652 p.flush();
98b004bf3985 Refactoring of NodeUtil to centralize the use of reflection
Christian Wimmer <christian.wimmer@oracle.com>
parents: 9538
diff changeset
653 }
98b004bf3985 Refactoring of NodeUtil to centralize the use of reflection
Christian Wimmer <christian.wimmer@oracle.com>
parents: 9538
diff changeset
654
98b004bf3985 Refactoring of NodeUtil to centralize the use of reflection
Christian Wimmer <christian.wimmer@oracle.com>
parents: 9538
diff changeset
655 private static void printTree(PrintWriter p, Node node, int level) {
7267
a4b84ba6dc2e Introduction of the Truffle API for efficient implementation of dynamic languages on top of the Graal VM. New projects com.oracle.truffle.api for the API definition and com.oracle.truffle.api.test for API tests and documentation.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
656 if (node == null) {
a4b84ba6dc2e Introduction of the Truffle API for efficient implementation of dynamic languages on top of the Graal VM. New projects com.oracle.truffle.api for the API definition and com.oracle.truffle.api.test for API tests and documentation.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
657 p.print("null");
a4b84ba6dc2e Introduction of the Truffle API for efficient implementation of dynamic languages on top of the Graal VM. New projects com.oracle.truffle.api for the API definition and com.oracle.truffle.api.test for API tests and documentation.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
658 return;
a4b84ba6dc2e Introduction of the Truffle API for efficient implementation of dynamic languages on top of the Graal VM. New projects com.oracle.truffle.api for the API definition and com.oracle.truffle.api.test for API tests and documentation.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
659 }
a4b84ba6dc2e Introduction of the Truffle API for efficient implementation of dynamic languages on top of the Graal VM. New projects com.oracle.truffle.api for the API definition and com.oracle.truffle.api.test for API tests and documentation.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
660
10845
e9248ebb1d79 Truffle: ignore null children in NodeUtil.findNodeChildren; refactoring.
Andreas Woess <andreas.woess@jku.at>
parents: 10793
diff changeset
661 p.print(nodeName(node));
7267
a4b84ba6dc2e Introduction of the Truffle API for efficient implementation of dynamic languages on top of the Graal VM. New projects com.oracle.truffle.api for the API definition and com.oracle.truffle.api.test for API tests and documentation.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
662
20129
5b7db8941fd7 Truffle: make NodeClass and NodeField a top-level class.
Christian Humer <christian.humer@gmail.com>
parents: 19974
diff changeset
663 ArrayList<NodeFieldAccessor> childFields = new ArrayList<>();
9754
98b004bf3985 Refactoring of NodeUtil to centralize the use of reflection
Christian Wimmer <christian.wimmer@oracle.com>
parents: 9538
diff changeset
664 String sep = "";
7267
a4b84ba6dc2e Introduction of the Truffle API for efficient implementation of dynamic languages on top of the Graal VM. New projects com.oracle.truffle.api for the API definition and com.oracle.truffle.api.test for API tests and documentation.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
665 p.print("(");
20130
8dc73c226c63 Truffle: cache NodeClass lookup in Node.
Christian Humer <christian.humer@gmail.com>
parents: 20129
diff changeset
666 for (NodeFieldAccessor field : NodeClass.get(node).getFields()) {
9754
98b004bf3985 Refactoring of NodeUtil to centralize the use of reflection
Christian Wimmer <christian.wimmer@oracle.com>
parents: 9538
diff changeset
667 if (field.getKind() == NodeFieldKind.CHILD || field.getKind() == NodeFieldKind.CHILDREN) {
98b004bf3985 Refactoring of NodeUtil to centralize the use of reflection
Christian Wimmer <christian.wimmer@oracle.com>
parents: 9538
diff changeset
668 childFields.add(field);
98b004bf3985 Refactoring of NodeUtil to centralize the use of reflection
Christian Wimmer <christian.wimmer@oracle.com>
parents: 9538
diff changeset
669 } else if (field.getKind() == NodeFieldKind.DATA) {
98b004bf3985 Refactoring of NodeUtil to centralize the use of reflection
Christian Wimmer <christian.wimmer@oracle.com>
parents: 9538
diff changeset
670 p.print(sep);
98b004bf3985 Refactoring of NodeUtil to centralize the use of reflection
Christian Wimmer <christian.wimmer@oracle.com>
parents: 9538
diff changeset
671 sep = ", ";
7267
a4b84ba6dc2e Introduction of the Truffle API for efficient implementation of dynamic languages on top of the Graal VM. New projects com.oracle.truffle.api for the API definition and com.oracle.truffle.api.test for API tests and documentation.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
672
9754
98b004bf3985 Refactoring of NodeUtil to centralize the use of reflection
Christian Wimmer <christian.wimmer@oracle.com>
parents: 9538
diff changeset
673 p.print(field.getName());
98b004bf3985 Refactoring of NodeUtil to centralize the use of reflection
Christian Wimmer <christian.wimmer@oracle.com>
parents: 9538
diff changeset
674 p.print(" = ");
98b004bf3985 Refactoring of NodeUtil to centralize the use of reflection
Christian Wimmer <christian.wimmer@oracle.com>
parents: 9538
diff changeset
675 p.print(field.loadValue(node));
7267
a4b84ba6dc2e Introduction of the Truffle API for efficient implementation of dynamic languages on top of the Graal VM. New projects com.oracle.truffle.api for the API definition and com.oracle.truffle.api.test for API tests and documentation.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
676 }
a4b84ba6dc2e Introduction of the Truffle API for efficient implementation of dynamic languages on top of the Graal VM. New projects com.oracle.truffle.api for the API definition and com.oracle.truffle.api.test for API tests and documentation.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
677 }
a4b84ba6dc2e Introduction of the Truffle API for efficient implementation of dynamic languages on top of the Graal VM. New projects com.oracle.truffle.api for the API definition and com.oracle.truffle.api.test for API tests and documentation.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
678 p.print(")");
a4b84ba6dc2e Introduction of the Truffle API for efficient implementation of dynamic languages on top of the Graal VM. New projects com.oracle.truffle.api for the API definition and com.oracle.truffle.api.test for API tests and documentation.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
679
a4b84ba6dc2e Introduction of the Truffle API for efficient implementation of dynamic languages on top of the Graal VM. New projects com.oracle.truffle.api for the API definition and com.oracle.truffle.api.test for API tests and documentation.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
680 if (childFields.size() != 0) {
a4b84ba6dc2e Introduction of the Truffle API for efficient implementation of dynamic languages on top of the Graal VM. New projects com.oracle.truffle.api for the API definition and com.oracle.truffle.api.test for API tests and documentation.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
681 p.print(" {");
20129
5b7db8941fd7 Truffle: make NodeClass and NodeField a top-level class.
Christian Humer <christian.humer@gmail.com>
parents: 19974
diff changeset
682 for (NodeFieldAccessor field : childFields) {
9754
98b004bf3985 Refactoring of NodeUtil to centralize the use of reflection
Christian Wimmer <christian.wimmer@oracle.com>
parents: 9538
diff changeset
683 printNewLine(p, level);
98b004bf3985 Refactoring of NodeUtil to centralize the use of reflection
Christian Wimmer <christian.wimmer@oracle.com>
parents: 9538
diff changeset
684 p.print(field.getName());
7267
a4b84ba6dc2e Introduction of the Truffle API for efficient implementation of dynamic languages on top of the Graal VM. New projects com.oracle.truffle.api for the API definition and com.oracle.truffle.api.test for API tests and documentation.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
685
9754
98b004bf3985 Refactoring of NodeUtil to centralize the use of reflection
Christian Wimmer <christian.wimmer@oracle.com>
parents: 9538
diff changeset
686 Object value = field.loadValue(node);
98b004bf3985 Refactoring of NodeUtil to centralize the use of reflection
Christian Wimmer <christian.wimmer@oracle.com>
parents: 9538
diff changeset
687 if (value == null) {
98b004bf3985 Refactoring of NodeUtil to centralize the use of reflection
Christian Wimmer <christian.wimmer@oracle.com>
parents: 9538
diff changeset
688 p.print(" = null ");
98b004bf3985 Refactoring of NodeUtil to centralize the use of reflection
Christian Wimmer <christian.wimmer@oracle.com>
parents: 9538
diff changeset
689 } else if (field.getKind() == NodeFieldKind.CHILD) {
98b004bf3985 Refactoring of NodeUtil to centralize the use of reflection
Christian Wimmer <christian.wimmer@oracle.com>
parents: 9538
diff changeset
690 p.print(" = ");
98b004bf3985 Refactoring of NodeUtil to centralize the use of reflection
Christian Wimmer <christian.wimmer@oracle.com>
parents: 9538
diff changeset
691 printTree(p, (Node) value, level + 1);
98b004bf3985 Refactoring of NodeUtil to centralize the use of reflection
Christian Wimmer <christian.wimmer@oracle.com>
parents: 9538
diff changeset
692 } else if (field.getKind() == NodeFieldKind.CHILDREN) {
16548
a3b0a2d61e62 extract method in NodeUtil
Christian Wirth <christian.wirth@oracle.com>
parents: 16513
diff changeset
693 printChildren(p, level, value);
7267
a4b84ba6dc2e Introduction of the Truffle API for efficient implementation of dynamic languages on top of the Graal VM. New projects com.oracle.truffle.api for the API definition and com.oracle.truffle.api.test for API tests and documentation.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
694 }
a4b84ba6dc2e Introduction of the Truffle API for efficient implementation of dynamic languages on top of the Graal VM. New projects com.oracle.truffle.api for the API definition and com.oracle.truffle.api.test for API tests and documentation.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
695 }
a4b84ba6dc2e Introduction of the Truffle API for efficient implementation of dynamic languages on top of the Graal VM. New projects com.oracle.truffle.api for the API definition and com.oracle.truffle.api.test for API tests and documentation.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
696 printNewLine(p, level - 1);
a4b84ba6dc2e Introduction of the Truffle API for efficient implementation of dynamic languages on top of the Graal VM. New projects com.oracle.truffle.api for the API definition and com.oracle.truffle.api.test for API tests and documentation.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
697 p.print("}");
a4b84ba6dc2e Introduction of the Truffle API for efficient implementation of dynamic languages on top of the Graal VM. New projects com.oracle.truffle.api for the API definition and com.oracle.truffle.api.test for API tests and documentation.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
698 }
a4b84ba6dc2e Introduction of the Truffle API for efficient implementation of dynamic languages on top of the Graal VM. New projects com.oracle.truffle.api for the API definition and com.oracle.truffle.api.test for API tests and documentation.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
699 }
a4b84ba6dc2e Introduction of the Truffle API for efficient implementation of dynamic languages on top of the Graal VM. New projects com.oracle.truffle.api for the API definition and com.oracle.truffle.api.test for API tests and documentation.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
700
16548
a3b0a2d61e62 extract method in NodeUtil
Christian Wirth <christian.wirth@oracle.com>
parents: 16513
diff changeset
701 private static void printChildren(PrintWriter p, int level, Object value) {
a3b0a2d61e62 extract method in NodeUtil
Christian Wirth <christian.wirth@oracle.com>
parents: 16513
diff changeset
702 String sep;
17275
846c059e3ecf Truffle: allow interface types in child fields
Andreas Woess <andreas.woess@jku.at>
parents: 16779
diff changeset
703 Object[] children = (Object[]) value;
16548
a3b0a2d61e62 extract method in NodeUtil
Christian Wirth <christian.wirth@oracle.com>
parents: 16513
diff changeset
704 p.print(" = [");
a3b0a2d61e62 extract method in NodeUtil
Christian Wirth <christian.wirth@oracle.com>
parents: 16513
diff changeset
705 sep = "";
17275
846c059e3ecf Truffle: allow interface types in child fields
Andreas Woess <andreas.woess@jku.at>
parents: 16779
diff changeset
706 for (Object child : children) {
16548
a3b0a2d61e62 extract method in NodeUtil
Christian Wirth <christian.wirth@oracle.com>
parents: 16513
diff changeset
707 p.print(sep);
a3b0a2d61e62 extract method in NodeUtil
Christian Wirth <christian.wirth@oracle.com>
parents: 16513
diff changeset
708 sep = ", ";
17275
846c059e3ecf Truffle: allow interface types in child fields
Andreas Woess <andreas.woess@jku.at>
parents: 16779
diff changeset
709 printTree(p, (Node) child, level + 1);
16548
a3b0a2d61e62 extract method in NodeUtil
Christian Wirth <christian.wirth@oracle.com>
parents: 16513
diff changeset
710 }
a3b0a2d61e62 extract method in NodeUtil
Christian Wirth <christian.wirth@oracle.com>
parents: 16513
diff changeset
711 p.print("]");
a3b0a2d61e62 extract method in NodeUtil
Christian Wirth <christian.wirth@oracle.com>
parents: 16513
diff changeset
712 }
a3b0a2d61e62 extract method in NodeUtil
Christian Wirth <christian.wirth@oracle.com>
parents: 16513
diff changeset
713
9754
98b004bf3985 Refactoring of NodeUtil to centralize the use of reflection
Christian Wimmer <christian.wimmer@oracle.com>
parents: 9538
diff changeset
714 private static void printNewLine(PrintWriter p, int level) {
7267
a4b84ba6dc2e Introduction of the Truffle API for efficient implementation of dynamic languages on top of the Graal VM. New projects com.oracle.truffle.api for the API definition and com.oracle.truffle.api.test for API tests and documentation.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
715 p.println();
a4b84ba6dc2e Introduction of the Truffle API for efficient implementation of dynamic languages on top of the Graal VM. New projects com.oracle.truffle.api for the API definition and com.oracle.truffle.api.test for API tests and documentation.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
716 for (int i = 0; i < level; i++) {
a4b84ba6dc2e Introduction of the Truffle API for efficient implementation of dynamic languages on top of the Graal VM. New projects com.oracle.truffle.api for the API definition and com.oracle.truffle.api.test for API tests and documentation.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
717 p.print(" ");
a4b84ba6dc2e Introduction of the Truffle API for efficient implementation of dynamic languages on top of the Graal VM. New projects com.oracle.truffle.api for the API definition and com.oracle.truffle.api.test for API tests and documentation.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
718 }
a4b84ba6dc2e Introduction of the Truffle API for efficient implementation of dynamic languages on top of the Graal VM. New projects com.oracle.truffle.api for the API definition and com.oracle.truffle.api.test for API tests and documentation.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
719 }
10742
99789440ce28 Truffle: Added assertions to replaceChild.
Christian Humer <christian.humer@gmail.com>
parents: 10704
diff changeset
720
10845
e9248ebb1d79 Truffle: ignore null children in NodeUtil.findNodeChildren; refactoring.
Andreas Woess <andreas.woess@jku.at>
parents: 10793
diff changeset
721 private static String nodeName(Node node) {
e9248ebb1d79 Truffle: ignore null children in NodeUtil.findNodeChildren; refactoring.
Andreas Woess <andreas.woess@jku.at>
parents: 10793
diff changeset
722 return node.getClass().getSimpleName();
e9248ebb1d79 Truffle: ignore null children in NodeUtil.findNodeChildren; refactoring.
Andreas Woess <andreas.woess@jku.at>
parents: 10793
diff changeset
723 }
11848
099af41815ea Truffle: add NodeUtil.printSourceAttributionTree() for debugging
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 11809
diff changeset
724
099af41815ea Truffle: add NodeUtil.printSourceAttributionTree() for debugging
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 11809
diff changeset
725 private static String displaySourceAttribution(Node node) {
099af41815ea Truffle: add NodeUtil.printSourceAttributionTree() for debugging
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 11809
diff changeset
726 final SourceSection section = node.getSourceSection();
16511
aee02665e505 Truffle: NodeUtil fix for displaying null SourceSections.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16067
diff changeset
727 if (section instanceof NullSourceSection) {
aee02665e505 Truffle: NodeUtil fix for displaying null SourceSections.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16067
diff changeset
728 return "source: " + section.getShortDescription();
aee02665e505 Truffle: NodeUtil fix for displaying null SourceSections.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16067
diff changeset
729 }
11848
099af41815ea Truffle: add NodeUtil.printSourceAttributionTree() for debugging
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 11809
diff changeset
730 if (section != null) {
099af41815ea Truffle: add NodeUtil.printSourceAttributionTree() for debugging
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 11809
diff changeset
731 final String srcText = section.getCode();
099af41815ea Truffle: add NodeUtil.printSourceAttributionTree() for debugging
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 11809
diff changeset
732 final StringBuilder sb = new StringBuilder();
16674
70f47dbbcabd Truffle/SourceAttribution: NodeUtil.displaySourceAttribution() outut revised for legibility.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16581
diff changeset
733 sb.append("source:");
11848
099af41815ea Truffle: add NodeUtil.printSourceAttributionTree() for debugging
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 11809
diff changeset
734 sb.append(" (" + section.getCharIndex() + "," + (section.getCharEndIndex() - 1) + ")");
19010
3bf612703773 Truffle: print the line number in the source attribution tree.
Chris Seaton <chris.seaton@oracle.com>
parents: 18984
diff changeset
735 sb.append(" line=" + section.getLineLocation().getLineNumber());
16674
70f47dbbcabd Truffle/SourceAttribution: NodeUtil.displaySourceAttribution() outut revised for legibility.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16581
diff changeset
736 sb.append(" len=" + srcText.length());
70f47dbbcabd Truffle/SourceAttribution: NodeUtil.displaySourceAttribution() outut revised for legibility.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16581
diff changeset
737 sb.append(" text=\"" + srcText + "\"");
11848
099af41815ea Truffle: add NodeUtil.printSourceAttributionTree() for debugging
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 11809
diff changeset
738 return sb.toString();
099af41815ea Truffle: add NodeUtil.printSourceAttributionTree() for debugging
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 11809
diff changeset
739 }
099af41815ea Truffle: add NodeUtil.printSourceAttributionTree() for debugging
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 11809
diff changeset
740 return "";
099af41815ea Truffle: add NodeUtil.printSourceAttributionTree() for debugging
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 11809
diff changeset
741 }
11950
be0a33a631fa Truffle: fix node rewrite issue that can occur when a currently executing node is replaced in a recursive call.
Andreas Woess <andreas.woess@jku.at>
parents: 11907
diff changeset
742
be0a33a631fa Truffle: fix node rewrite issue that can occur when a currently executing node is replaced in a recursive call.
Andreas Woess <andreas.woess@jku.at>
parents: 11907
diff changeset
743 public static boolean verify(Node root) {
be0a33a631fa Truffle: fix node rewrite issue that can occur when a currently executing node is replaced in a recursive call.
Andreas Woess <andreas.woess@jku.at>
parents: 11907
diff changeset
744 Iterable<Node> children = root.getChildren();
be0a33a631fa Truffle: fix node rewrite issue that can occur when a currently executing node is replaced in a recursive call.
Andreas Woess <andreas.woess@jku.at>
parents: 11907
diff changeset
745 for (Node child : children) {
be0a33a631fa Truffle: fix node rewrite issue that can occur when a currently executing node is replaced in a recursive call.
Andreas Woess <andreas.woess@jku.at>
parents: 11907
diff changeset
746 if (child != null) {
be0a33a631fa Truffle: fix node rewrite issue that can occur when a currently executing node is replaced in a recursive call.
Andreas Woess <andreas.woess@jku.at>
parents: 11907
diff changeset
747 if (child.getParent() != root) {
be0a33a631fa Truffle: fix node rewrite issue that can occur when a currently executing node is replaced in a recursive call.
Andreas Woess <andreas.woess@jku.at>
parents: 11907
diff changeset
748 throw new AssertionError(toStringWithClass(child) + ": actual parent=" + toStringWithClass(child.getParent()) + " expected parent=" + toStringWithClass(root));
be0a33a631fa Truffle: fix node rewrite issue that can occur when a currently executing node is replaced in a recursive call.
Andreas Woess <andreas.woess@jku.at>
parents: 11907
diff changeset
749 }
be0a33a631fa Truffle: fix node rewrite issue that can occur when a currently executing node is replaced in a recursive call.
Andreas Woess <andreas.woess@jku.at>
parents: 11907
diff changeset
750 verify(child);
be0a33a631fa Truffle: fix node rewrite issue that can occur when a currently executing node is replaced in a recursive call.
Andreas Woess <andreas.woess@jku.at>
parents: 11907
diff changeset
751 }
be0a33a631fa Truffle: fix node rewrite issue that can occur when a currently executing node is replaced in a recursive call.
Andreas Woess <andreas.woess@jku.at>
parents: 11907
diff changeset
752 }
be0a33a631fa Truffle: fix node rewrite issue that can occur when a currently executing node is replaced in a recursive call.
Andreas Woess <andreas.woess@jku.at>
parents: 11907
diff changeset
753 return true;
be0a33a631fa Truffle: fix node rewrite issue that can occur when a currently executing node is replaced in a recursive call.
Andreas Woess <andreas.woess@jku.at>
parents: 11907
diff changeset
754 }
be0a33a631fa Truffle: fix node rewrite issue that can occur when a currently executing node is replaced in a recursive call.
Andreas Woess <andreas.woess@jku.at>
parents: 11907
diff changeset
755
be0a33a631fa Truffle: fix node rewrite issue that can occur when a currently executing node is replaced in a recursive call.
Andreas Woess <andreas.woess@jku.at>
parents: 11907
diff changeset
756 private static String toStringWithClass(Object obj) {
be0a33a631fa Truffle: fix node rewrite issue that can occur when a currently executing node is replaced in a recursive call.
Andreas Woess <andreas.woess@jku.at>
parents: 11907
diff changeset
757 return obj == null ? "null" : obj + "(" + obj.getClass().getName() + ")";
be0a33a631fa Truffle: fix node rewrite issue that can occur when a currently executing node is replaced in a recursive call.
Andreas Woess <andreas.woess@jku.at>
parents: 11907
diff changeset
758 }
16464
76081918079d Truffle: move TraceRewrites code to NodeUtil
Andreas Woess <andreas.woess@jku.at>
parents: 16067
diff changeset
759
76081918079d Truffle: move TraceRewrites code to NodeUtil
Andreas Woess <andreas.woess@jku.at>
parents: 16067
diff changeset
760 static void traceRewrite(Node oldNode, Node newNode, CharSequence reason) {
76081918079d Truffle: move TraceRewrites code to NodeUtil
Andreas Woess <andreas.woess@jku.at>
parents: 16067
diff changeset
761 if (TruffleOptions.TraceRewritesFilterFromCost != null) {
76081918079d Truffle: move TraceRewrites code to NodeUtil
Andreas Woess <andreas.woess@jku.at>
parents: 16067
diff changeset
762 if (filterByKind(oldNode, TruffleOptions.TraceRewritesFilterFromCost)) {
76081918079d Truffle: move TraceRewrites code to NodeUtil
Andreas Woess <andreas.woess@jku.at>
parents: 16067
diff changeset
763 return;
76081918079d Truffle: move TraceRewrites code to NodeUtil
Andreas Woess <andreas.woess@jku.at>
parents: 16067
diff changeset
764 }
76081918079d Truffle: move TraceRewrites code to NodeUtil
Andreas Woess <andreas.woess@jku.at>
parents: 16067
diff changeset
765 }
76081918079d Truffle: move TraceRewrites code to NodeUtil
Andreas Woess <andreas.woess@jku.at>
parents: 16067
diff changeset
766
76081918079d Truffle: move TraceRewrites code to NodeUtil
Andreas Woess <andreas.woess@jku.at>
parents: 16067
diff changeset
767 if (TruffleOptions.TraceRewritesFilterToCost != null) {
76081918079d Truffle: move TraceRewrites code to NodeUtil
Andreas Woess <andreas.woess@jku.at>
parents: 16067
diff changeset
768 if (filterByKind(newNode, TruffleOptions.TraceRewritesFilterToCost)) {
76081918079d Truffle: move TraceRewrites code to NodeUtil
Andreas Woess <andreas.woess@jku.at>
parents: 16067
diff changeset
769 return;
76081918079d Truffle: move TraceRewrites code to NodeUtil
Andreas Woess <andreas.woess@jku.at>
parents: 16067
diff changeset
770 }
76081918079d Truffle: move TraceRewrites code to NodeUtil
Andreas Woess <andreas.woess@jku.at>
parents: 16067
diff changeset
771 }
76081918079d Truffle: move TraceRewrites code to NodeUtil
Andreas Woess <andreas.woess@jku.at>
parents: 16067
diff changeset
772
76081918079d Truffle: move TraceRewrites code to NodeUtil
Andreas Woess <andreas.woess@jku.at>
parents: 16067
diff changeset
773 String filter = TruffleOptions.TraceRewritesFilterClass;
76081918079d Truffle: move TraceRewrites code to NodeUtil
Andreas Woess <andreas.woess@jku.at>
parents: 16067
diff changeset
774 Class<? extends Node> from = oldNode.getClass();
76081918079d Truffle: move TraceRewrites code to NodeUtil
Andreas Woess <andreas.woess@jku.at>
parents: 16067
diff changeset
775 Class<? extends Node> to = newNode.getClass();
76081918079d Truffle: move TraceRewrites code to NodeUtil
Andreas Woess <andreas.woess@jku.at>
parents: 16067
diff changeset
776 if (filter != null && (filterByContainsClassName(from, filter) || filterByContainsClassName(to, filter))) {
76081918079d Truffle: move TraceRewrites code to NodeUtil
Andreas Woess <andreas.woess@jku.at>
parents: 16067
diff changeset
777 return;
76081918079d Truffle: move TraceRewrites code to NodeUtil
Andreas Woess <andreas.woess@jku.at>
parents: 16067
diff changeset
778 }
76081918079d Truffle: move TraceRewrites code to NodeUtil
Andreas Woess <andreas.woess@jku.at>
parents: 16067
diff changeset
779
76081918079d Truffle: move TraceRewrites code to NodeUtil
Andreas Woess <andreas.woess@jku.at>
parents: 16067
diff changeset
780 final SourceSection reportedSourceSection = oldNode.getEncapsulatingSourceSection();
76081918079d Truffle: move TraceRewrites code to NodeUtil
Andreas Woess <andreas.woess@jku.at>
parents: 16067
diff changeset
781
76081918079d Truffle: move TraceRewrites code to NodeUtil
Andreas Woess <andreas.woess@jku.at>
parents: 16067
diff changeset
782 PrintStream out = System.out;
76081918079d Truffle: move TraceRewrites code to NodeUtil
Andreas Woess <andreas.woess@jku.at>
parents: 16067
diff changeset
783 out.printf("[truffle] rewrite %-50s |From %-40s |To %-40s |Reason %s%s%n", oldNode.toString(), formatNodeInfo(oldNode), formatNodeInfo(newNode),
76081918079d Truffle: move TraceRewrites code to NodeUtil
Andreas Woess <andreas.woess@jku.at>
parents: 16067
diff changeset
784 reason != null && reason.length() > 0 ? reason : "unknown", reportedSourceSection != null ? " at " + reportedSourceSection.getShortDescription() : "");
76081918079d Truffle: move TraceRewrites code to NodeUtil
Andreas Woess <andreas.woess@jku.at>
parents: 16067
diff changeset
785 }
76081918079d Truffle: move TraceRewrites code to NodeUtil
Andreas Woess <andreas.woess@jku.at>
parents: 16067
diff changeset
786
76081918079d Truffle: move TraceRewrites code to NodeUtil
Andreas Woess <andreas.woess@jku.at>
parents: 16067
diff changeset
787 private static String formatNodeInfo(Node node) {
76081918079d Truffle: move TraceRewrites code to NodeUtil
Andreas Woess <andreas.woess@jku.at>
parents: 16067
diff changeset
788 String cost = "?";
76081918079d Truffle: move TraceRewrites code to NodeUtil
Andreas Woess <andreas.woess@jku.at>
parents: 16067
diff changeset
789 switch (node.getCost()) {
76081918079d Truffle: move TraceRewrites code to NodeUtil
Andreas Woess <andreas.woess@jku.at>
parents: 16067
diff changeset
790 case NONE:
76081918079d Truffle: move TraceRewrites code to NodeUtil
Andreas Woess <andreas.woess@jku.at>
parents: 16067
diff changeset
791 cost = "G";
76081918079d Truffle: move TraceRewrites code to NodeUtil
Andreas Woess <andreas.woess@jku.at>
parents: 16067
diff changeset
792 break;
76081918079d Truffle: move TraceRewrites code to NodeUtil
Andreas Woess <andreas.woess@jku.at>
parents: 16067
diff changeset
793 case MONOMORPHIC:
76081918079d Truffle: move TraceRewrites code to NodeUtil
Andreas Woess <andreas.woess@jku.at>
parents: 16067
diff changeset
794 cost = "M";
76081918079d Truffle: move TraceRewrites code to NodeUtil
Andreas Woess <andreas.woess@jku.at>
parents: 16067
diff changeset
795 break;
76081918079d Truffle: move TraceRewrites code to NodeUtil
Andreas Woess <andreas.woess@jku.at>
parents: 16067
diff changeset
796 case POLYMORPHIC:
76081918079d Truffle: move TraceRewrites code to NodeUtil
Andreas Woess <andreas.woess@jku.at>
parents: 16067
diff changeset
797 cost = "P";
76081918079d Truffle: move TraceRewrites code to NodeUtil
Andreas Woess <andreas.woess@jku.at>
parents: 16067
diff changeset
798 break;
76081918079d Truffle: move TraceRewrites code to NodeUtil
Andreas Woess <andreas.woess@jku.at>
parents: 16067
diff changeset
799 case MEGAMORPHIC:
76081918079d Truffle: move TraceRewrites code to NodeUtil
Andreas Woess <andreas.woess@jku.at>
parents: 16067
diff changeset
800 cost = "G";
76081918079d Truffle: move TraceRewrites code to NodeUtil
Andreas Woess <andreas.woess@jku.at>
parents: 16067
diff changeset
801 break;
76081918079d Truffle: move TraceRewrites code to NodeUtil
Andreas Woess <andreas.woess@jku.at>
parents: 16067
diff changeset
802 default:
76081918079d Truffle: move TraceRewrites code to NodeUtil
Andreas Woess <andreas.woess@jku.at>
parents: 16067
diff changeset
803 cost = "?";
76081918079d Truffle: move TraceRewrites code to NodeUtil
Andreas Woess <andreas.woess@jku.at>
parents: 16067
diff changeset
804 break;
76081918079d Truffle: move TraceRewrites code to NodeUtil
Andreas Woess <andreas.woess@jku.at>
parents: 16067
diff changeset
805 }
76081918079d Truffle: move TraceRewrites code to NodeUtil
Andreas Woess <andreas.woess@jku.at>
parents: 16067
diff changeset
806 return cost + " " + node.getClass().getSimpleName();
76081918079d Truffle: move TraceRewrites code to NodeUtil
Andreas Woess <andreas.woess@jku.at>
parents: 16067
diff changeset
807 }
76081918079d Truffle: move TraceRewrites code to NodeUtil
Andreas Woess <andreas.woess@jku.at>
parents: 16067
diff changeset
808
76081918079d Truffle: move TraceRewrites code to NodeUtil
Andreas Woess <andreas.woess@jku.at>
parents: 16067
diff changeset
809 private static boolean filterByKind(Node node, NodeCost cost) {
76081918079d Truffle: move TraceRewrites code to NodeUtil
Andreas Woess <andreas.woess@jku.at>
parents: 16067
diff changeset
810 return node.getCost() == cost;
76081918079d Truffle: move TraceRewrites code to NodeUtil
Andreas Woess <andreas.woess@jku.at>
parents: 16067
diff changeset
811 }
76081918079d Truffle: move TraceRewrites code to NodeUtil
Andreas Woess <andreas.woess@jku.at>
parents: 16067
diff changeset
812
76081918079d Truffle: move TraceRewrites code to NodeUtil
Andreas Woess <andreas.woess@jku.at>
parents: 16067
diff changeset
813 private static boolean filterByContainsClassName(Class<? extends Node> from, String filter) {
76081918079d Truffle: move TraceRewrites code to NodeUtil
Andreas Woess <andreas.woess@jku.at>
parents: 16067
diff changeset
814 Class<?> currentFrom = from;
76081918079d Truffle: move TraceRewrites code to NodeUtil
Andreas Woess <andreas.woess@jku.at>
parents: 16067
diff changeset
815 while (currentFrom != null) {
76081918079d Truffle: move TraceRewrites code to NodeUtil
Andreas Woess <andreas.woess@jku.at>
parents: 16067
diff changeset
816 if (currentFrom.getName().contains(filter)) {
76081918079d Truffle: move TraceRewrites code to NodeUtil
Andreas Woess <andreas.woess@jku.at>
parents: 16067
diff changeset
817 return false;
76081918079d Truffle: move TraceRewrites code to NodeUtil
Andreas Woess <andreas.woess@jku.at>
parents: 16067
diff changeset
818 }
76081918079d Truffle: move TraceRewrites code to NodeUtil
Andreas Woess <andreas.woess@jku.at>
parents: 16067
diff changeset
819 currentFrom = currentFrom.getSuperclass();
76081918079d Truffle: move TraceRewrites code to NodeUtil
Andreas Woess <andreas.woess@jku.at>
parents: 16067
diff changeset
820 }
76081918079d Truffle: move TraceRewrites code to NodeUtil
Andreas Woess <andreas.woess@jku.at>
parents: 16067
diff changeset
821 return true;
76081918079d Truffle: move TraceRewrites code to NodeUtil
Andreas Woess <andreas.woess@jku.at>
parents: 16067
diff changeset
822 }
20131
4b12d5355811 Truffle: do not use iterators for visitors.
Christian Humer <christian.humer@gmail.com>
parents: 20130
diff changeset
823
4b12d5355811 Truffle: do not use iterators for visitors.
Christian Humer <christian.humer@gmail.com>
parents: 20130
diff changeset
824 private static final class NodeCounter implements NodeVisitor {
4b12d5355811 Truffle: do not use iterators for visitors.
Christian Humer <christian.humer@gmail.com>
parents: 20130
diff changeset
825
4b12d5355811 Truffle: do not use iterators for visitors.
Christian Humer <christian.humer@gmail.com>
parents: 20130
diff changeset
826 public int count;
4b12d5355811 Truffle: do not use iterators for visitors.
Christian Humer <christian.humer@gmail.com>
parents: 20130
diff changeset
827 private final NodeCountFilter filter;
4b12d5355811 Truffle: do not use iterators for visitors.
Christian Humer <christian.humer@gmail.com>
parents: 20130
diff changeset
828
4b12d5355811 Truffle: do not use iterators for visitors.
Christian Humer <christian.humer@gmail.com>
parents: 20130
diff changeset
829 public NodeCounter(NodeCountFilter filter) {
4b12d5355811 Truffle: do not use iterators for visitors.
Christian Humer <christian.humer@gmail.com>
parents: 20130
diff changeset
830 this.filter = filter;
4b12d5355811 Truffle: do not use iterators for visitors.
Christian Humer <christian.humer@gmail.com>
parents: 20130
diff changeset
831 }
4b12d5355811 Truffle: do not use iterators for visitors.
Christian Humer <christian.humer@gmail.com>
parents: 20130
diff changeset
832
4b12d5355811 Truffle: do not use iterators for visitors.
Christian Humer <christian.humer@gmail.com>
parents: 20130
diff changeset
833 public boolean visit(Node node) {
4b12d5355811 Truffle: do not use iterators for visitors.
Christian Humer <christian.humer@gmail.com>
parents: 20130
diff changeset
834 if (filter.isCounted(node)) {
4b12d5355811 Truffle: do not use iterators for visitors.
Christian Humer <christian.humer@gmail.com>
parents: 20130
diff changeset
835 count++;
4b12d5355811 Truffle: do not use iterators for visitors.
Christian Humer <christian.humer@gmail.com>
parents: 20130
diff changeset
836 }
4b12d5355811 Truffle: do not use iterators for visitors.
Christian Humer <christian.humer@gmail.com>
parents: 20130
diff changeset
837 return true;
4b12d5355811 Truffle: do not use iterators for visitors.
Christian Humer <christian.humer@gmail.com>
parents: 20130
diff changeset
838 }
4b12d5355811 Truffle: do not use iterators for visitors.
Christian Humer <christian.humer@gmail.com>
parents: 20130
diff changeset
839
4b12d5355811 Truffle: do not use iterators for visitors.
Christian Humer <christian.humer@gmail.com>
parents: 20130
diff changeset
840 }
7267
a4b84ba6dc2e Introduction of the Truffle API for efficient implementation of dynamic languages on top of the Graal VM. New projects com.oracle.truffle.api for the API definition and com.oracle.truffle.api.test for API tests and documentation.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
841 }