annotate truffle/com.oracle.truffle.api/src/com/oracle/truffle/api/nodes/NodeUtil.java @ 21966:5023b913e2ba

Help the partial evaluator / language developer by marking API methods as neverPartOfCompilation() when they are too complicated to be compiled.
author Christian Wimmer <christian.wimmer@oracle.com>
date Mon, 22 Jun 2015 10:16:27 -0700
parents 9c8c0937da41
children b2d1c8ff592a
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) {
21966
5023b913e2ba Help the partial evaluator / language developer by marking API methods as neverPartOfCompilation() when they are too complicated to be compiled.
Christian Wimmer <christian.wimmer@oracle.com>
parents: 21951
diff changeset
134 CompilerAsserts.neverPartOfCompilation();
11809
c7769440afd8 improve NodeUtil.cloneNode.
Andreas Woess <andreas.woess@jku.at>
parents: 11649
diff changeset
135 final Node clone = orig.copy();
20130
8dc73c226c63 Truffle: cache NodeClass lookup in Node.
Christian Humer <christian.humer@gmail.com>
parents: 20129
diff changeset
136 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
137
20129
5b7db8941fd7 Truffle: make NodeClass and NodeField a top-level class.
Christian Humer <christian.humer@gmail.com>
parents: 19974
diff changeset
138 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
139
20129
5b7db8941fd7 Truffle: make NodeClass and NodeField a top-level class.
Christian Humer <christian.humer@gmail.com>
parents: 19974
diff changeset
140 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
141 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
142 if (child != null) {
18750
8a758dce7d80 Truffle: added new deepCopy method to Node.
Christian Humer <christian.humer@gmail.com>
parents: 18510
diff changeset
143 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
144 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
145 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
146 }
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
147 }
20129
5b7db8941fd7 Truffle: make NodeClass and NodeField a top-level class.
Christian Humer <christian.humer@gmail.com>
parents: 19974
diff changeset
148 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
149 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
150 if (children != null) {
17275
846c059e3ecf Truffle: allow interface types in child fields
Andreas Woess <andreas.woess@jku.at>
parents: 16779
diff changeset
151 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
152 for (int i = 0; i < children.length; i++) {
11809
c7769440afd8 improve NodeUtil.cloneNode.
Andreas Woess <andreas.woess@jku.at>
parents: 11649
diff changeset
153 if (children[i] != null) {
18750
8a758dce7d80 Truffle: added new deepCopy method to Node.
Christian Humer <christian.humer@gmail.com>
parents: 18510
diff changeset
154 Node clonedChild = ((Node) children[i]).deepCopy();
11809
c7769440afd8 improve NodeUtil.cloneNode.
Andreas Woess <andreas.woess@jku.at>
parents: 11649
diff changeset
155 clonedChildren[i] = clonedChild;
20129
5b7db8941fd7 Truffle: make NodeClass and NodeField a top-level class.
Christian Humer <christian.humer@gmail.com>
parents: 19974
diff changeset
156 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
157 }
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
158 }
18904
6ba170cb6f53 Truffle: channel all NodeUtil field accesses through NodeField class
Andreas Woess <andreas.woess@jku.at>
parents: 18751
diff changeset
159 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
160 }
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
161 }
20129
5b7db8941fd7 Truffle: make NodeClass and NodeField a top-level class.
Christian Humer <christian.humer@gmail.com>
parents: 19974
diff changeset
162 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
163 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
164 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
165 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
166 }
cb4d5cc2b52b Truffle: clone ConditionProfile and BranchProfile node fields
Andreas Woess <andreas.woess@jku.at>
parents: 18485
diff changeset
167 }
18750
8a758dce7d80 Truffle: added new deepCopy method to Node.
Christian Humer <christian.humer@gmail.com>
parents: 18510
diff changeset
168 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
169 }
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
170
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
171 public static List<Node> findNodeChildren(Node node) {
21966
5023b913e2ba Help the partial evaluator / language developer by marking API methods as neverPartOfCompilation() when they are too complicated to be compiled.
Christian Wimmer <christian.wimmer@oracle.com>
parents: 21951
diff changeset
172 CompilerAsserts.neverPartOfCompilation();
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
173 List<Node> nodes = new ArrayList<>();
20130
8dc73c226c63 Truffle: cache NodeClass lookup in Node.
Christian Humer <christian.humer@gmail.com>
parents: 20129
diff changeset
174 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
175
20129
5b7db8941fd7 Truffle: make NodeClass and NodeField a top-level class.
Christian Humer <christian.humer@gmail.com>
parents: 19974
diff changeset
176 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
177 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
178 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
179 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
180 }
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
181 }
20129
5b7db8941fd7 Truffle: make NodeClass and NodeField a top-level class.
Christian Humer <christian.humer@gmail.com>
parents: 19974
diff changeset
182 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
183 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
184 if (children != null) {
17275
846c059e3ecf Truffle: allow interface types in child fields
Andreas Woess <andreas.woess@jku.at>
parents: 16779
diff changeset
185 for (Object child : children) {
10845
e9248ebb1d79 Truffle: ignore null children in NodeUtil.findNodeChildren; refactoring.
Andreas Woess <andreas.woess@jku.at>
parents: 10793
diff changeset
186 if (child != null) {
17275
846c059e3ecf Truffle: allow interface types in child fields
Andreas Woess <andreas.woess@jku.at>
parents: 16779
diff changeset
187 nodes.add((Node) child);
10845
e9248ebb1d79 Truffle: ignore null children in NodeUtil.findNodeChildren; refactoring.
Andreas Woess <andreas.woess@jku.at>
parents: 10793
diff changeset
188 }
e9248ebb1d79 Truffle: ignore null children in NodeUtil.findNodeChildren; refactoring.
Andreas Woess <andreas.woess@jku.at>
parents: 10793
diff changeset
189 }
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
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 }
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 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
194 }
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
195
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
196 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
197 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
198 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
199 }
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
200
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
201 public static boolean replaceChild(Node parent, Node oldChild, Node newChild) {
21966
5023b913e2ba Help the partial evaluator / language developer by marking API methods as neverPartOfCompilation() when they are too complicated to be compiled.
Christian Wimmer <christian.wimmer@oracle.com>
parents: 21951
diff changeset
202 CompilerAsserts.neverPartOfCompilation();
20130
8dc73c226c63 Truffle: cache NodeClass lookup in Node.
Christian Humer <christian.humer@gmail.com>
parents: 20129
diff changeset
203 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
204
20129
5b7db8941fd7 Truffle: make NodeClass and NodeField a top-level class.
Christian Humer <christian.humer@gmail.com>
parents: 19974
diff changeset
205 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
206 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
207 assert assertAssignable(nodeField, newChild);
6ba170cb6f53 Truffle: channel all NodeUtil field accesses through NodeField class
Andreas Woess <andreas.woess@jku.at>
parents: 18751
diff changeset
208 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
209 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
210 }
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
211 }
10742
99789440ce28 Truffle: Added assertions to replaceChild.
Christian Humer <christian.humer@gmail.com>
parents: 10704
diff changeset
212
20129
5b7db8941fd7 Truffle: make NodeClass and NodeField a top-level class.
Christian Humer <christian.humer@gmail.com>
parents: 19974
diff changeset
213 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
214 Object arrayObject = nodeField.getObject(parent);
10742
99789440ce28 Truffle: Added assertions to replaceChild.
Christian Humer <christian.humer@gmail.com>
parents: 10704
diff changeset
215 if (arrayObject != null) {
17275
846c059e3ecf Truffle: allow interface types in child fields
Andreas Woess <andreas.woess@jku.at>
parents: 16779
diff changeset
216 Object[] array = (Object[]) arrayObject;
10755
b221e31d7b0b Truffle: revise NodeUtil.replaceChild assertion
Andreas Woess <andreas.woess@jku.at>
parents: 10742
diff changeset
217 for (int i = 0; i < array.length; i++) {
b221e31d7b0b Truffle: revise NodeUtil.replaceChild assertion
Andreas Woess <andreas.woess@jku.at>
parents: 10742
diff changeset
218 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
219 assert assertAssignable(nodeField, newChild);
10755
b221e31d7b0b Truffle: revise NodeUtil.replaceChild assertion
Andreas Woess <andreas.woess@jku.at>
parents: 10742
diff changeset
220 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
221 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
222 }
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
223 }
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 }
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
226 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
227 }
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
228
20129
5b7db8941fd7 Truffle: make NodeClass and NodeField a top-level class.
Christian Humer <christian.humer@gmail.com>
parents: 19974
diff changeset
229 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
230 if (newValue == null) {
99789440ce28 Truffle: Added assertions to replaceChild.
Christian Humer <christian.humer@gmail.com>
parents: 10704
diff changeset
231 return true;
99789440ce28 Truffle: Added assertions to replaceChild.
Christian Humer <christian.humer@gmail.com>
parents: 10704
diff changeset
232 }
18904
6ba170cb6f53 Truffle: channel all NodeUtil field accesses through NodeField class
Andreas Woess <andreas.woess@jku.at>
parents: 18751
diff changeset
233 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
234 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
235 return true;
6ba170cb6f53 Truffle: channel all NodeUtil field accesses through NodeField class
Andreas Woess <andreas.woess@jku.at>
parents: 18751
diff changeset
236 } else {
6ba170cb6f53 Truffle: channel all NodeUtil field accesses through NodeField class
Andreas Woess <andreas.woess@jku.at>
parents: 18751
diff changeset
237 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
238 return false;
6ba170cb6f53 Truffle: channel all NodeUtil field accesses through NodeField class
Andreas Woess <andreas.woess@jku.at>
parents: 18751
diff changeset
239 }
6ba170cb6f53 Truffle: channel all NodeUtil field accesses through NodeField class
Andreas Woess <andreas.woess@jku.at>
parents: 18751
diff changeset
240 } 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
241 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
242 return true;
6ba170cb6f53 Truffle: channel all NodeUtil field accesses through NodeField class
Andreas Woess <andreas.woess@jku.at>
parents: 18751
diff changeset
243 } else {
6ba170cb6f53 Truffle: channel all NodeUtil field accesses through NodeField class
Andreas Woess <andreas.woess@jku.at>
parents: 18751
diff changeset
244 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
245 return false;
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 }
10755
b221e31d7b0b Truffle: revise NodeUtil.replaceChild assertion
Andreas Woess <andreas.woess@jku.at>
parents: 10742
diff changeset
248 throw new IllegalArgumentException();
10742
99789440ce28 Truffle: Added assertions to replaceChild.
Christian Humer <christian.humer@gmail.com>
parents: 10704
diff changeset
249 }
99789440ce28 Truffle: Added assertions to replaceChild.
Christian Humer <christian.humer@gmail.com>
parents: 10704
diff changeset
250
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
251 /**
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 * 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
253 *
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 * @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
255 */
20129
5b7db8941fd7 Truffle: make NodeClass and NodeField a top-level class.
Christian Humer <christian.humer@gmail.com>
parents: 19974
diff changeset
256 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
257 assert child != null;
20130
8dc73c226c63 Truffle: cache NodeClass lookup in Node.
Christian Humer <christian.humer@gmail.com>
parents: 20129
diff changeset
258 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
259
20129
5b7db8941fd7 Truffle: make NodeClass and NodeField a top-level class.
Christian Humer <christian.humer@gmail.com>
parents: 19974
diff changeset
260 for (NodeFieldAccessor field : parentNodeClass.getChildFields()) {
19012
f5b83e7b2b4c Truffle: simplify NodeUtil.findChildField
Andreas Woess <andreas.woess@jku.at>
parents: 19010
diff changeset
261 if (field.getObject(parent) == child) {
f5b83e7b2b4c Truffle: simplify NodeUtil.findChildField
Andreas Woess <andreas.woess@jku.at>
parents: 19010
diff changeset
262 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
263 }
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
264 }
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
20129
5b7db8941fd7 Truffle: make NodeClass and NodeField a top-level class.
Christian Humer <christian.humer@gmail.com>
parents: 19974
diff changeset
266 for (NodeFieldAccessor field : parentNodeClass.getChildrenFields()) {
19012
f5b83e7b2b4c Truffle: simplify NodeUtil.findChildField
Andreas Woess <andreas.woess@jku.at>
parents: 19010
diff changeset
267 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
268 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
269 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
270 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
271 if (array[i] == child) {
19012
f5b83e7b2b4c Truffle: simplify NodeUtil.findChildField
Andreas Woess <andreas.woess@jku.at>
parents: 19010
diff changeset
272 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
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 }
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 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
278 }
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 /**
19141
67d9e635102f Truffle/Instrumentation: refine checks for safe node replacement
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 19012
diff changeset
281 * 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
282 */
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
283 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
284 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
285 if (parent != null) {
20129
5b7db8941fd7 Truffle: make NodeClass and NodeField a top-level class.
Christian Humer <christian.humer@gmail.com>
parents: 19974
diff changeset
286 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
287 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
288 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
289 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
290 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
291 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
292 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
293 default:
67d9e635102f Truffle/Instrumentation: refine checks for safe node replacement
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 19012
diff changeset
294 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
295 }
67d9e635102f Truffle/Instrumentation: refine checks for safe node replacement
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 19012
diff changeset
296 }
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
297 }
19141
67d9e635102f Truffle/Instrumentation: refine checks for safe node replacement
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 19012
diff changeset
298 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
299 }
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
300
19607
73811d1b4cd0 Truffle: add NodeUtil.forEachChild
Andreas Woess <andreas.woess@oracle.com>
parents: 19141
diff changeset
301 /**
73811d1b4cd0 Truffle: add NodeUtil.forEachChild
Andreas Woess <andreas.woess@oracle.com>
parents: 19141
diff changeset
302 * 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
303 *
73811d1b4cd0 Truffle: add NodeUtil.forEachChild
Andreas Woess <andreas.woess@oracle.com>
parents: 19141
diff changeset
304 * @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
305 */
73811d1b4cd0 Truffle: add NodeUtil.forEachChild
Andreas Woess <andreas.woess@oracle.com>
parents: 19141
diff changeset
306 public static boolean forEachChild(Node parent, NodeVisitor visitor) {
21966
5023b913e2ba Help the partial evaluator / language developer by marking API methods as neverPartOfCompilation() when they are too complicated to be compiled.
Christian Wimmer <christian.wimmer@oracle.com>
parents: 21951
diff changeset
307 CompilerAsserts.neverPartOfCompilation();
19607
73811d1b4cd0 Truffle: add NodeUtil.forEachChild
Andreas Woess <andreas.woess@oracle.com>
parents: 19141
diff changeset
308 Objects.requireNonNull(visitor);
20130
8dc73c226c63 Truffle: cache NodeClass lookup in Node.
Christian Humer <christian.humer@gmail.com>
parents: 20129
diff changeset
309 NodeClass parentNodeClass = parent.getNodeClass();
19607
73811d1b4cd0 Truffle: add NodeUtil.forEachChild
Andreas Woess <andreas.woess@oracle.com>
parents: 19141
diff changeset
310
20129
5b7db8941fd7 Truffle: make NodeClass and NodeField a top-level class.
Christian Humer <christian.humer@gmail.com>
parents: 19974
diff changeset
311 for (NodeFieldAccessor field : parentNodeClass.getChildFields()) {
19607
73811d1b4cd0 Truffle: add NodeUtil.forEachChild
Andreas Woess <andreas.woess@oracle.com>
parents: 19141
diff changeset
312 Object child = field.getObject(parent);
73811d1b4cd0 Truffle: add NodeUtil.forEachChild
Andreas Woess <andreas.woess@oracle.com>
parents: 19141
diff changeset
313 if (child != null) {
73811d1b4cd0 Truffle: add NodeUtil.forEachChild
Andreas Woess <andreas.woess@oracle.com>
parents: 19141
diff changeset
314 if (!visitor.visit((Node) child)) {
73811d1b4cd0 Truffle: add NodeUtil.forEachChild
Andreas Woess <andreas.woess@oracle.com>
parents: 19141
diff changeset
315 return false;
73811d1b4cd0 Truffle: add NodeUtil.forEachChild
Andreas Woess <andreas.woess@oracle.com>
parents: 19141
diff changeset
316 }
73811d1b4cd0 Truffle: add NodeUtil.forEachChild
Andreas Woess <andreas.woess@oracle.com>
parents: 19141
diff changeset
317 }
73811d1b4cd0 Truffle: add NodeUtil.forEachChild
Andreas Woess <andreas.woess@oracle.com>
parents: 19141
diff changeset
318 }
73811d1b4cd0 Truffle: add NodeUtil.forEachChild
Andreas Woess <andreas.woess@oracle.com>
parents: 19141
diff changeset
319
20129
5b7db8941fd7 Truffle: make NodeClass and NodeField a top-level class.
Christian Humer <christian.humer@gmail.com>
parents: 19974
diff changeset
320 for (NodeFieldAccessor field : parentNodeClass.getChildrenFields()) {
19607
73811d1b4cd0 Truffle: add NodeUtil.forEachChild
Andreas Woess <andreas.woess@oracle.com>
parents: 19141
diff changeset
321 Object arrayObject = field.getObject(parent);
73811d1b4cd0 Truffle: add NodeUtil.forEachChild
Andreas Woess <andreas.woess@oracle.com>
parents: 19141
diff changeset
322 if (arrayObject != null) {
73811d1b4cd0 Truffle: add NodeUtil.forEachChild
Andreas Woess <andreas.woess@oracle.com>
parents: 19141
diff changeset
323 Object[] array = (Object[]) arrayObject;
73811d1b4cd0 Truffle: add NodeUtil.forEachChild
Andreas Woess <andreas.woess@oracle.com>
parents: 19141
diff changeset
324 for (int i = 0; i < array.length; i++) {
73811d1b4cd0 Truffle: add NodeUtil.forEachChild
Andreas Woess <andreas.woess@oracle.com>
parents: 19141
diff changeset
325 Object child = array[i];
73811d1b4cd0 Truffle: add NodeUtil.forEachChild
Andreas Woess <andreas.woess@oracle.com>
parents: 19141
diff changeset
326 if (child != null) {
73811d1b4cd0 Truffle: add NodeUtil.forEachChild
Andreas Woess <andreas.woess@oracle.com>
parents: 19141
diff changeset
327 if (!visitor.visit((Node) child)) {
73811d1b4cd0 Truffle: add NodeUtil.forEachChild
Andreas Woess <andreas.woess@oracle.com>
parents: 19141
diff changeset
328 return false;
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 }
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 }
73811d1b4cd0 Truffle: add NodeUtil.forEachChild
Andreas Woess <andreas.woess@oracle.com>
parents: 19141
diff changeset
334
73811d1b4cd0 Truffle: add NodeUtil.forEachChild
Andreas Woess <andreas.woess@oracle.com>
parents: 19141
diff changeset
335 return true;
73811d1b4cd0 Truffle: add NodeUtil.forEachChild
Andreas Woess <andreas.woess@oracle.com>
parents: 19141
diff changeset
336 }
73811d1b4cd0 Truffle: add NodeUtil.forEachChild
Andreas Woess <andreas.woess@oracle.com>
parents: 19141
diff changeset
337
20131
4b12d5355811 Truffle: do not use iterators for visitors.
Christian Humer <christian.humer@gmail.com>
parents: 20130
diff changeset
338 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
339 NodeClass parentNodeClass = parent.getNodeClass();
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 for (NodeFieldAccessor field : parentNodeClass.getChildFields()) {
4b12d5355811 Truffle: do not use iterators for visitors.
Christian Humer <christian.humer@gmail.com>
parents: 20130
diff changeset
342 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
343 return false;
4b12d5355811 Truffle: do not use iterators for visitors.
Christian Humer <christian.humer@gmail.com>
parents: 20130
diff changeset
344 }
4b12d5355811 Truffle: do not use iterators for visitors.
Christian Humer <christian.humer@gmail.com>
parents: 20130
diff changeset
345 }
4b12d5355811 Truffle: do not use iterators for visitors.
Christian Humer <christian.humer@gmail.com>
parents: 20130
diff changeset
346
4b12d5355811 Truffle: do not use iterators for visitors.
Christian Humer <christian.humer@gmail.com>
parents: 20130
diff changeset
347 for (NodeFieldAccessor field : parentNodeClass.getChildrenFields()) {
4b12d5355811 Truffle: do not use iterators for visitors.
Christian Humer <christian.humer@gmail.com>
parents: 20130
diff changeset
348 Object arrayObject = field.getObject(parent);
4b12d5355811 Truffle: do not use iterators for visitors.
Christian Humer <christian.humer@gmail.com>
parents: 20130
diff changeset
349 if (arrayObject == null) {
4b12d5355811 Truffle: do not use iterators for visitors.
Christian Humer <christian.humer@gmail.com>
parents: 20130
diff changeset
350 continue;
4b12d5355811 Truffle: do not use iterators for visitors.
Christian Humer <christian.humer@gmail.com>
parents: 20130
diff changeset
351 }
4b12d5355811 Truffle: do not use iterators for visitors.
Christian Humer <christian.humer@gmail.com>
parents: 20130
diff changeset
352 Object[] array = (Object[]) arrayObject;
4b12d5355811 Truffle: do not use iterators for visitors.
Christian Humer <christian.humer@gmail.com>
parents: 20130
diff changeset
353 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
354 if (!visitChild((Node) array[i], visitor)) {
4b12d5355811 Truffle: do not use iterators for visitors.
Christian Humer <christian.humer@gmail.com>
parents: 20130
diff changeset
355 return false;
4b12d5355811 Truffle: do not use iterators for visitors.
Christian Humer <christian.humer@gmail.com>
parents: 20130
diff changeset
356 }
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
4b12d5355811 Truffle: do not use iterators for visitors.
Christian Humer <christian.humer@gmail.com>
parents: 20130
diff changeset
360 return true;
4b12d5355811 Truffle: do not use iterators for visitors.
Christian Humer <christian.humer@gmail.com>
parents: 20130
diff changeset
361 }
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 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
364 if (child == null) {
4b12d5355811 Truffle: do not use iterators for visitors.
Christian Humer <christian.humer@gmail.com>
parents: 20130
diff changeset
365 return true;
4b12d5355811 Truffle: do not use iterators for visitors.
Christian Humer <christian.humer@gmail.com>
parents: 20130
diff changeset
366 }
4b12d5355811 Truffle: do not use iterators for visitors.
Christian Humer <christian.humer@gmail.com>
parents: 20130
diff changeset
367 if (!visitor.visit(child)) {
4b12d5355811 Truffle: do not use iterators for visitors.
Christian Humer <christian.humer@gmail.com>
parents: 20130
diff changeset
368 return false;
4b12d5355811 Truffle: do not use iterators for visitors.
Christian Humer <christian.humer@gmail.com>
parents: 20130
diff changeset
369 }
4b12d5355811 Truffle: do not use iterators for visitors.
Christian Humer <christian.humer@gmail.com>
parents: 20130
diff changeset
370 if (!forEachChildRecursive(child, visitor)) {
4b12d5355811 Truffle: do not use iterators for visitors.
Christian Humer <christian.humer@gmail.com>
parents: 20130
diff changeset
371 return false;
4b12d5355811 Truffle: do not use iterators for visitors.
Christian Humer <christian.humer@gmail.com>
parents: 20130
diff changeset
372 }
4b12d5355811 Truffle: do not use iterators for visitors.
Christian Humer <christian.humer@gmail.com>
parents: 20130
diff changeset
373 return true;
4b12d5355811 Truffle: do not use iterators for visitors.
Christian Humer <christian.humer@gmail.com>
parents: 20130
diff changeset
374 }
4b12d5355811 Truffle: do not use iterators for visitors.
Christian Humer <christian.humer@gmail.com>
parents: 20130
diff changeset
375
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
376 /** 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
377 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
378 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
379 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
380 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
381 }
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 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
383 }
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
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 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
386 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
387 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
388 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
389 }
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
390
11622
ccf047a30f18 Truffle: NodeUtil.getNthParent.
Chris Seaton <chris.seaton@oracle.com>
parents: 11279
diff changeset
391 /**
ccf047a30f18 Truffle: NodeUtil.getNthParent.
Chris Seaton <chris.seaton@oracle.com>
parents: 11279
diff changeset
392 * 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
393 * are less than n ancestors.
ccf047a30f18 Truffle: NodeUtil.getNthParent.
Chris Seaton <chris.seaton@oracle.com>
parents: 11279
diff changeset
394 */
ccf047a30f18 Truffle: NodeUtil.getNthParent.
Chris Seaton <chris.seaton@oracle.com>
parents: 11279
diff changeset
395 public static Node getNthParent(Node node, int n) {
ccf047a30f18 Truffle: NodeUtil.getNthParent.
Chris Seaton <chris.seaton@oracle.com>
parents: 11279
diff changeset
396 Node parent = node;
ccf047a30f18 Truffle: NodeUtil.getNthParent.
Chris Seaton <chris.seaton@oracle.com>
parents: 11279
diff changeset
397
ccf047a30f18 Truffle: NodeUtil.getNthParent.
Chris Seaton <chris.seaton@oracle.com>
parents: 11279
diff changeset
398 for (int i = 0; i < n; i++) {
ccf047a30f18 Truffle: NodeUtil.getNthParent.
Chris Seaton <chris.seaton@oracle.com>
parents: 11279
diff changeset
399 parent = parent.getParent();
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 if (parent == null) {
ccf047a30f18 Truffle: NodeUtil.getNthParent.
Chris Seaton <chris.seaton@oracle.com>
parents: 11279
diff changeset
402 return null;
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 }
ccf047a30f18 Truffle: NodeUtil.getNthParent.
Chris Seaton <chris.seaton@oracle.com>
parents: 11279
diff changeset
405
ccf047a30f18 Truffle: NodeUtil.getNthParent.
Chris Seaton <chris.seaton@oracle.com>
parents: 11279
diff changeset
406 return parent;
ccf047a30f18 Truffle: NodeUtil.getNthParent.
Chris Seaton <chris.seaton@oracle.com>
parents: 11279
diff changeset
407 }
ccf047a30f18 Truffle: NodeUtil.getNthParent.
Chris Seaton <chris.seaton@oracle.com>
parents: 11279
diff changeset
408
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
409 /** 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
410 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
411 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
412 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
413 } else {
7530
5e3d1a68664e applied mx eclipseformat to all Java files
Doug Simon <doug.simon@oracle.com>
parents: 7267
diff changeset
414 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
415 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
416 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
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 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
420 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
421 }
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
422 }
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
423 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
424 }
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
425
12666
d9c34e8337f4 Truffle: cleanup of NodeUtil#findParent; added NodeUtil#findAllParents
Christian Humer <christian.humer@gmail.com>
parents: 11950
diff changeset
426 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
427 Node parent = start.getParent();
d9c34e8337f4 Truffle: cleanup of NodeUtil#findParent; added NodeUtil#findAllParents
Christian Humer <christian.humer@gmail.com>
parents: 11950
diff changeset
428 if (parent == null) {
d9c34e8337f4 Truffle: cleanup of NodeUtil#findParent; added NodeUtil#findAllParents
Christian Humer <christian.humer@gmail.com>
parents: 11950
diff changeset
429 return null;
d9c34e8337f4 Truffle: cleanup of NodeUtil#findParent; added NodeUtil#findAllParents
Christian Humer <christian.humer@gmail.com>
parents: 11950
diff changeset
430 } else if (clazz.isInstance(parent)) {
d9c34e8337f4 Truffle: cleanup of NodeUtil#findParent; added NodeUtil#findAllParents
Christian Humer <christian.humer@gmail.com>
parents: 11950
diff changeset
431 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
432 } else {
12666
d9c34e8337f4 Truffle: cleanup of NodeUtil#findParent; added NodeUtil#findAllParents
Christian Humer <christian.humer@gmail.com>
parents: 11950
diff changeset
433 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
434 }
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
435 }
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
436
12666
d9c34e8337f4 Truffle: cleanup of NodeUtil#findParent; added NodeUtil#findAllParents
Christian Humer <christian.humer@gmail.com>
parents: 11950
diff changeset
437 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
438 List<T> parents = new ArrayList<>();
d9c34e8337f4 Truffle: cleanup of NodeUtil#findParent; added NodeUtil#findAllParents
Christian Humer <christian.humer@gmail.com>
parents: 11950
diff changeset
439 T parent = findParent(start, clazz);
d9c34e8337f4 Truffle: cleanup of NodeUtil#findParent; added NodeUtil#findAllParents
Christian Humer <christian.humer@gmail.com>
parents: 11950
diff changeset
440 while (parent != null) {
d9c34e8337f4 Truffle: cleanup of NodeUtil#findParent; added NodeUtil#findAllParents
Christian Humer <christian.humer@gmail.com>
parents: 11950
diff changeset
441 parents.add(parent);
d9c34e8337f4 Truffle: cleanup of NodeUtil#findParent; added NodeUtil#findAllParents
Christian Humer <christian.humer@gmail.com>
parents: 11950
diff changeset
442 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
443 }
12666
d9c34e8337f4 Truffle: cleanup of NodeUtil#findParent; added NodeUtil#findAllParents
Christian Humer <christian.humer@gmail.com>
parents: 11950
diff changeset
444 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
445 }
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
446
12666
d9c34e8337f4 Truffle: cleanup of NodeUtil#findParent; added NodeUtil#findAllParents
Christian Humer <christian.humer@gmail.com>
parents: 11950
diff changeset
447 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
448 List<Node> nodes = new ArrayList<>();
d9c34e8337f4 Truffle: cleanup of NodeUtil#findParent; added NodeUtil#findAllParents
Christian Humer <christian.humer@gmail.com>
parents: 11950
diff changeset
449 Node current = child;
d9c34e8337f4 Truffle: cleanup of NodeUtil#findParent; added NodeUtil#findAllParents
Christian Humer <christian.humer@gmail.com>
parents: 11950
diff changeset
450 while (current != null) {
d9c34e8337f4 Truffle: cleanup of NodeUtil#findParent; added NodeUtil#findAllParents
Christian Humer <christian.humer@gmail.com>
parents: 11950
diff changeset
451 nodes.add(current);
d9c34e8337f4 Truffle: cleanup of NodeUtil#findParent; added NodeUtil#findAllParents
Christian Humer <christian.humer@gmail.com>
parents: 11950
diff changeset
452 if (current == parent) {
d9c34e8337f4 Truffle: cleanup of NodeUtil#findParent; added NodeUtil#findAllParents
Christian Humer <christian.humer@gmail.com>
parents: 11950
diff changeset
453 return nodes;
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 current = current.getParent();
d9c34e8337f4 Truffle: cleanup of NodeUtil#findParent; added NodeUtil#findAllParents
Christian Humer <christian.humer@gmail.com>
parents: 11950
diff changeset
456 }
d9c34e8337f4 Truffle: cleanup of NodeUtil#findParent; added NodeUtil#findAllParents
Christian Humer <christian.humer@gmail.com>
parents: 11950
diff changeset
457 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
458 }
d9c34e8337f4 Truffle: cleanup of NodeUtil#findParent; added NodeUtil#findAllParents
Christian Humer <christian.humer@gmail.com>
parents: 11950
diff changeset
459
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
460 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
461 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
462 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
463 }
45ed86c4d127 Truffle: change NodeUtil.findFirstNodeInstance to include root in potential matches
Andreas Woess <andreas.woess@oracle.com>
parents: 21469
diff changeset
464 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
465 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
466 if (node != null) {
45ed86c4d127 Truffle: change NodeUtil.findFirstNodeInstance to include root in potential matches
Andreas Woess <andreas.woess@oracle.com>
parents: 21469
diff changeset
467 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
468 }
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
469 }
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 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
471 }
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
16465
456abab80eb5 Truffle: remove obsolete NodeUtil.findNodeInstancesInFunction (functionally equivalent to findAllNodeInstances)
Andreas Woess <andreas.woess@jku.at>
parents: 16464
diff changeset
473 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
474 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
475 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
476 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
477 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
478 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
479 }
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 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
481 }
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
482 });
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
483 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
484 }
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
485
10704
84b944726df2 Truffle: introduce NodeUtil.countNodes
Andreas Woess <andreas.woess@jku.at>
parents: 9754
diff changeset
486 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
487 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
488 }
e455fc531ec2 Truffle: Added API in NodeUtil to count nodes restricted to a Kind.
Christian Humer <christian.humer@gmail.com>
parents: 13983
diff changeset
489
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
490 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
491 NodeCounter counter = new NodeCounter(filter);
4b12d5355811 Truffle: do not use iterators for visitors.
Christian Humer <christian.humer@gmail.com>
parents: 20130
diff changeset
492 root.accept(counter);
4b12d5355811 Truffle: do not use iterators for visitors.
Christian Humer <christian.humer@gmail.com>
parents: 20130
diff changeset
493 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
494 }
6e12e0ace0d5 Truffle: add NodeUtil.countNodes overload that counts nodes of a specific type
Andreas Woess <andreas.woess@jku.at>
parents: 10756
diff changeset
495
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
496 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
497
20131
4b12d5355811 Truffle: do not use iterators for visitors.
Christian Humer <christian.humer@gmail.com>
parents: 20130
diff changeset
498 NodeCountFilter NO_FILTER = new NodeCountFilter() {
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 public boolean isCounted(Node node) {
4b12d5355811 Truffle: do not use iterators for visitors.
Christian Humer <christian.humer@gmail.com>
parents: 20130
diff changeset
501 return true;
4b12d5355811 Truffle: do not use iterators for visitors.
Christian Humer <christian.humer@gmail.com>
parents: 20130
diff changeset
502 }
4b12d5355811 Truffle: do not use iterators for visitors.
Christian Humer <christian.humer@gmail.com>
parents: 20130
diff changeset
503 };
4b12d5355811 Truffle: do not use iterators for visitors.
Christian Humer <christian.humer@gmail.com>
parents: 20130
diff changeset
504
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
505 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
506
10704
84b944726df2 Truffle: introduce NodeUtil.countNodes
Andreas Woess <andreas.woess@jku.at>
parents: 9754
diff changeset
507 }
84b944726df2 Truffle: introduce NodeUtil.countNodes
Andreas Woess <andreas.woess@jku.at>
parents: 9754
diff changeset
508
9754
98b004bf3985 Refactoring of NodeUtil to centralize the use of reflection
Christian Wimmer <christian.wimmer@oracle.com>
parents: 9538
diff changeset
509 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
510 StringWriter out = new StringWriter();
98b004bf3985 Refactoring of NodeUtil to centralize the use of reflection
Christian Wimmer <christian.wimmer@oracle.com>
parents: 9538
diff changeset
511 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
512 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
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 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
516 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
517 }
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
518
9754
98b004bf3985 Refactoring of NodeUtil to centralize the use of reflection
Christian Wimmer <christian.wimmer@oracle.com>
parents: 9538
diff changeset
519 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
520 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
521 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
522 }
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
523 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
524 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
525 }
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 if (parent == null) {
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 } else {
17275
846c059e3ecf Truffle: allow interface types in child fields
Andreas Woess <andreas.woess@jku.at>
parents: 16779
diff changeset
529 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
530 p.print(" = ");
10845
e9248ebb1d79 Truffle: ignore null children in NodeUtil.findNodeChildren; refactoring.
Andreas Woess <andreas.woess@jku.at>
parents: 10793
diff changeset
531 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
532 }
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
533
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 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
535 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
536 }
9754
98b004bf3985 Refactoring of NodeUtil to centralize the use of reflection
Christian Wimmer <christian.wimmer@oracle.com>
parents: 9538
diff changeset
537 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
538 }
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
539
11848
099af41815ea Truffle: add NodeUtil.printSourceAttributionTree() for debugging
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 11809
diff changeset
540 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
541 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
542 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
543 return out.toString();
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
099af41815ea Truffle: add NodeUtil.printSourceAttributionTree() for debugging
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 11809
diff changeset
546 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
547 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
548 }
099af41815ea Truffle: add NodeUtil.printSourceAttributionTree() for debugging
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 11809
diff changeset
549
21469
286aef83a9a7 Replacing PrintStream with PrintWriter in the simple language
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 20131
diff changeset
550 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
551 printSourceAttributionTree(out, null, node, 1);
286aef83a9a7 Replacing PrintStream with PrintWriter in the simple language
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 20131
diff changeset
552 }
286aef83a9a7 Replacing PrintStream with PrintWriter in the simple language
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 20131
diff changeset
553
11848
099af41815ea Truffle: add NodeUtil.printSourceAttributionTree() for debugging
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 11809
diff changeset
554 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
555 if (node == null) {
099af41815ea Truffle: add NodeUtil.printSourceAttributionTree() for debugging
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 11809
diff changeset
556 return;
099af41815ea Truffle: add NodeUtil.printSourceAttributionTree() for debugging
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 11809
diff changeset
557 }
099af41815ea Truffle: add NodeUtil.printSourceAttributionTree() for debugging
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 11809
diff changeset
558 if (parent == null) {
099af41815ea Truffle: add NodeUtil.printSourceAttributionTree() for debugging
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 11809
diff changeset
559 // 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
560 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
561 if (sourceSection != null) {
099af41815ea Truffle: add NodeUtil.printSourceAttributionTree() for debugging
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 11809
diff changeset
562 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
563 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
564 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
565 }
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 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
568 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
569 sb.append("| ");
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 if (parent != null) {
17275
846c059e3ecf Truffle: allow interface types in child fields
Andreas Woess <andreas.woess@jku.at>
parents: 16779
diff changeset
573 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
574 }
099af41815ea Truffle: add NodeUtil.printSourceAttributionTree() for debugging
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 11809
diff changeset
575
099af41815ea Truffle: add NodeUtil.printSourceAttributionTree() for debugging
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 11809
diff changeset
576 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
577
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
578 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
579
11848
099af41815ea Truffle: add NodeUtil.printSourceAttributionTree() for debugging
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 11809
diff changeset
580 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
581 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
582
099af41815ea Truffle: add NodeUtil.printSourceAttributionTree() for debugging
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 11809
diff changeset
583 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
584 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
585 }
099af41815ea Truffle: add NodeUtil.printSourceAttributionTree() for debugging
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 11809
diff changeset
586 p.flush();
099af41815ea Truffle: add NodeUtil.printSourceAttributionTree() for debugging
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 11809
diff changeset
587 }
099af41815ea Truffle: add NodeUtil.printSourceAttributionTree() for debugging
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 11809
diff changeset
588
17275
846c059e3ecf Truffle: allow interface types in child fields
Andreas Woess <andreas.woess@jku.at>
parents: 16779
diff changeset
589 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
590 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
591 for (NodeFieldAccessor field : fields) {
17275
846c059e3ecf Truffle: allow interface types in child fields
Andreas Woess <andreas.woess@jku.at>
parents: 16779
diff changeset
592 Object value = field.loadValue(parent);
846c059e3ecf Truffle: allow interface types in child fields
Andreas Woess <andreas.woess@jku.at>
parents: 16779
diff changeset
593 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
594 return field.getName();
846c059e3ecf Truffle: allow interface types in child fields
Andreas Woess <andreas.woess@jku.at>
parents: 16779
diff changeset
595 } else if (field.getKind() == NodeFieldKind.CHILDREN) {
846c059e3ecf Truffle: allow interface types in child fields
Andreas Woess <andreas.woess@jku.at>
parents: 16779
diff changeset
596 int index = 0;
846c059e3ecf Truffle: allow interface types in child fields
Andreas Woess <andreas.woess@jku.at>
parents: 16779
diff changeset
597 for (Object arrayNode : (Object[]) value) {
846c059e3ecf Truffle: allow interface types in child fields
Andreas Woess <andreas.woess@jku.at>
parents: 16779
diff changeset
598 if (arrayNode == node) {
846c059e3ecf Truffle: allow interface types in child fields
Andreas Woess <andreas.woess@jku.at>
parents: 16779
diff changeset
599 return field.getName() + "[" + index + "]";
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 index++;
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 }
846c059e3ecf Truffle: allow interface types in child fields
Andreas Woess <andreas.woess@jku.at>
parents: 16779
diff changeset
604 }
846c059e3ecf Truffle: allow interface types in child fields
Andreas Woess <andreas.woess@jku.at>
parents: 16779
diff changeset
605 return defaultName;
846c059e3ecf Truffle: allow interface types in child fields
Andreas Woess <andreas.woess@jku.at>
parents: 16779
diff changeset
606 }
846c059e3ecf Truffle: allow interface types in child fields
Andreas Woess <andreas.woess@jku.at>
parents: 16779
diff changeset
607
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
608 /**
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
609 * 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
610 * 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
611 * <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
612 * <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
613 * {@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
614 * <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
615 * <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
616 * </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
617 */
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 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
619 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
620 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
621 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
622 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
623 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
624 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
625 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
626 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
627 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
628 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
629 }
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 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
631 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
632 }
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
633 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
634 }
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
635
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
636 /**
9754
98b004bf3985 Refactoring of NodeUtil to centralize the use of reflection
Christian Wimmer <christian.wimmer@oracle.com>
parents: 9538
diff changeset
637 * 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
638 * 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
639 *
9754
98b004bf3985 Refactoring of NodeUtil to centralize the use of reflection
Christian Wimmer <christian.wimmer@oracle.com>
parents: 9538
diff changeset
640 * @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
641 * @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
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 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
644 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
645 }
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
646
9754
98b004bf3985 Refactoring of NodeUtil to centralize the use of reflection
Christian Wimmer <christian.wimmer@oracle.com>
parents: 9538
diff changeset
647 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
648 StringWriter out = new StringWriter();
98b004bf3985 Refactoring of NodeUtil to centralize the use of reflection
Christian Wimmer <christian.wimmer@oracle.com>
parents: 9538
diff changeset
649 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
650 return out.toString();
98b004bf3985 Refactoring of NodeUtil to centralize the use of reflection
Christian Wimmer <christian.wimmer@oracle.com>
parents: 9538
diff changeset
651 }
98b004bf3985 Refactoring of NodeUtil to centralize the use of reflection
Christian Wimmer <christian.wimmer@oracle.com>
parents: 9538
diff changeset
652
98b004bf3985 Refactoring of NodeUtil to centralize the use of reflection
Christian Wimmer <christian.wimmer@oracle.com>
parents: 9538
diff changeset
653 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
654 printTree(p, node, 1);
98b004bf3985 Refactoring of NodeUtil to centralize the use of reflection
Christian Wimmer <christian.wimmer@oracle.com>
parents: 9538
diff changeset
655 p.println();
98b004bf3985 Refactoring of NodeUtil to centralize the use of reflection
Christian Wimmer <christian.wimmer@oracle.com>
parents: 9538
diff changeset
656 p.flush();
98b004bf3985 Refactoring of NodeUtil to centralize the use of reflection
Christian Wimmer <christian.wimmer@oracle.com>
parents: 9538
diff changeset
657 }
98b004bf3985 Refactoring of NodeUtil to centralize the use of reflection
Christian Wimmer <christian.wimmer@oracle.com>
parents: 9538
diff changeset
658
98b004bf3985 Refactoring of NodeUtil to centralize the use of reflection
Christian Wimmer <christian.wimmer@oracle.com>
parents: 9538
diff changeset
659 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
660 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
661 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
662 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
663 }
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
664
10845
e9248ebb1d79 Truffle: ignore null children in NodeUtil.findNodeChildren; refactoring.
Andreas Woess <andreas.woess@jku.at>
parents: 10793
diff changeset
665 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
666
20129
5b7db8941fd7 Truffle: make NodeClass and NodeField a top-level class.
Christian Humer <christian.humer@gmail.com>
parents: 19974
diff changeset
667 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
668 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
669 p.print("(");
20130
8dc73c226c63 Truffle: cache NodeClass lookup in Node.
Christian Humer <christian.humer@gmail.com>
parents: 20129
diff changeset
670 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
671 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
672 childFields.add(field);
98b004bf3985 Refactoring of NodeUtil to centralize the use of reflection
Christian Wimmer <christian.wimmer@oracle.com>
parents: 9538
diff changeset
673 } 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
674 p.print(sep);
98b004bf3985 Refactoring of NodeUtil to centralize the use of reflection
Christian Wimmer <christian.wimmer@oracle.com>
parents: 9538
diff changeset
675 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
676
9754
98b004bf3985 Refactoring of NodeUtil to centralize the use of reflection
Christian Wimmer <christian.wimmer@oracle.com>
parents: 9538
diff changeset
677 p.print(field.getName());
98b004bf3985 Refactoring of NodeUtil to centralize the use of reflection
Christian Wimmer <christian.wimmer@oracle.com>
parents: 9538
diff changeset
678 p.print(" = ");
98b004bf3985 Refactoring of NodeUtil to centralize the use of reflection
Christian Wimmer <christian.wimmer@oracle.com>
parents: 9538
diff changeset
679 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
680 }
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 }
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
682 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
683
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
684 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
685 p.print(" {");
20129
5b7db8941fd7 Truffle: make NodeClass and NodeField a top-level class.
Christian Humer <christian.humer@gmail.com>
parents: 19974
diff changeset
686 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
687 printNewLine(p, level);
98b004bf3985 Refactoring of NodeUtil to centralize the use of reflection
Christian Wimmer <christian.wimmer@oracle.com>
parents: 9538
diff changeset
688 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
689
9754
98b004bf3985 Refactoring of NodeUtil to centralize the use of reflection
Christian Wimmer <christian.wimmer@oracle.com>
parents: 9538
diff changeset
690 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
691 if (value == null) {
98b004bf3985 Refactoring of NodeUtil to centralize the use of reflection
Christian Wimmer <christian.wimmer@oracle.com>
parents: 9538
diff changeset
692 p.print(" = null ");
98b004bf3985 Refactoring of NodeUtil to centralize the use of reflection
Christian Wimmer <christian.wimmer@oracle.com>
parents: 9538
diff changeset
693 } 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
694 p.print(" = ");
98b004bf3985 Refactoring of NodeUtil to centralize the use of reflection
Christian Wimmer <christian.wimmer@oracle.com>
parents: 9538
diff changeset
695 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
696 } else if (field.getKind() == NodeFieldKind.CHILDREN) {
16548
a3b0a2d61e62 extract method in NodeUtil
Christian Wirth <christian.wirth@oracle.com>
parents: 16513
diff changeset
697 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
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 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
701 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
702 }
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
703 }
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
704
16548
a3b0a2d61e62 extract method in NodeUtil
Christian Wirth <christian.wirth@oracle.com>
parents: 16513
diff changeset
705 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
706 String sep;
17275
846c059e3ecf Truffle: allow interface types in child fields
Andreas Woess <andreas.woess@jku.at>
parents: 16779
diff changeset
707 Object[] children = (Object[]) value;
16548
a3b0a2d61e62 extract method in NodeUtil
Christian Wirth <christian.wirth@oracle.com>
parents: 16513
diff changeset
708 p.print(" = [");
a3b0a2d61e62 extract method in NodeUtil
Christian Wirth <christian.wirth@oracle.com>
parents: 16513
diff changeset
709 sep = "";
17275
846c059e3ecf Truffle: allow interface types in child fields
Andreas Woess <andreas.woess@jku.at>
parents: 16779
diff changeset
710 for (Object child : children) {
16548
a3b0a2d61e62 extract method in NodeUtil
Christian Wirth <christian.wirth@oracle.com>
parents: 16513
diff changeset
711 p.print(sep);
a3b0a2d61e62 extract method in NodeUtil
Christian Wirth <christian.wirth@oracle.com>
parents: 16513
diff changeset
712 sep = ", ";
17275
846c059e3ecf Truffle: allow interface types in child fields
Andreas Woess <andreas.woess@jku.at>
parents: 16779
diff changeset
713 printTree(p, (Node) child, level + 1);
16548
a3b0a2d61e62 extract method in NodeUtil
Christian Wirth <christian.wirth@oracle.com>
parents: 16513
diff changeset
714 }
a3b0a2d61e62 extract method in NodeUtil
Christian Wirth <christian.wirth@oracle.com>
parents: 16513
diff changeset
715 p.print("]");
a3b0a2d61e62 extract method in NodeUtil
Christian Wirth <christian.wirth@oracle.com>
parents: 16513
diff changeset
716 }
a3b0a2d61e62 extract method in NodeUtil
Christian Wirth <christian.wirth@oracle.com>
parents: 16513
diff changeset
717
9754
98b004bf3985 Refactoring of NodeUtil to centralize the use of reflection
Christian Wimmer <christian.wimmer@oracle.com>
parents: 9538
diff changeset
718 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
719 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
720 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
721 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
722 }
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
723 }
10742
99789440ce28 Truffle: Added assertions to replaceChild.
Christian Humer <christian.humer@gmail.com>
parents: 10704
diff changeset
724
10845
e9248ebb1d79 Truffle: ignore null children in NodeUtil.findNodeChildren; refactoring.
Andreas Woess <andreas.woess@jku.at>
parents: 10793
diff changeset
725 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
726 return node.getClass().getSimpleName();
e9248ebb1d79 Truffle: ignore null children in NodeUtil.findNodeChildren; refactoring.
Andreas Woess <andreas.woess@jku.at>
parents: 10793
diff changeset
727 }
11848
099af41815ea Truffle: add NodeUtil.printSourceAttributionTree() for debugging
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 11809
diff changeset
728
099af41815ea Truffle: add NodeUtil.printSourceAttributionTree() for debugging
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 11809
diff changeset
729 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
730 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
731 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
732 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
733 }
11848
099af41815ea Truffle: add NodeUtil.printSourceAttributionTree() for debugging
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 11809
diff changeset
734 if (section != null) {
099af41815ea Truffle: add NodeUtil.printSourceAttributionTree() for debugging
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 11809
diff changeset
735 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
736 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
737 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
738 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
739 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
740 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
741 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
742 return sb.toString();
099af41815ea Truffle: add NodeUtil.printSourceAttributionTree() for debugging
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 11809
diff changeset
743 }
099af41815ea Truffle: add NodeUtil.printSourceAttributionTree() for debugging
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 11809
diff changeset
744 return "";
099af41815ea Truffle: add NodeUtil.printSourceAttributionTree() for debugging
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 11809
diff changeset
745 }
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
746
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 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
748 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
749 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
750 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
751 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
752 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
753 }
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 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
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 }
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 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
758 }
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
759
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
760 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
761 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
762 }
16464
76081918079d Truffle: move TraceRewrites code to NodeUtil
Andreas Woess <andreas.woess@jku.at>
parents: 16067
diff changeset
763
76081918079d Truffle: move TraceRewrites code to NodeUtil
Andreas Woess <andreas.woess@jku.at>
parents: 16067
diff changeset
764 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
765 if (TruffleOptions.TraceRewritesFilterFromCost != null) {
76081918079d Truffle: move TraceRewrites code to NodeUtil
Andreas Woess <andreas.woess@jku.at>
parents: 16067
diff changeset
766 if (filterByKind(oldNode, TruffleOptions.TraceRewritesFilterFromCost)) {
76081918079d Truffle: move TraceRewrites code to NodeUtil
Andreas Woess <andreas.woess@jku.at>
parents: 16067
diff changeset
767 return;
76081918079d Truffle: move TraceRewrites code to NodeUtil
Andreas Woess <andreas.woess@jku.at>
parents: 16067
diff changeset
768 }
76081918079d Truffle: move TraceRewrites code to NodeUtil
Andreas Woess <andreas.woess@jku.at>
parents: 16067
diff changeset
769 }
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 if (TruffleOptions.TraceRewritesFilterToCost != null) {
76081918079d Truffle: move TraceRewrites code to NodeUtil
Andreas Woess <andreas.woess@jku.at>
parents: 16067
diff changeset
772 if (filterByKind(newNode, TruffleOptions.TraceRewritesFilterToCost)) {
76081918079d Truffle: move TraceRewrites code to NodeUtil
Andreas Woess <andreas.woess@jku.at>
parents: 16067
diff changeset
773 return;
76081918079d Truffle: move TraceRewrites code to NodeUtil
Andreas Woess <andreas.woess@jku.at>
parents: 16067
diff changeset
774 }
76081918079d Truffle: move TraceRewrites code to NodeUtil
Andreas Woess <andreas.woess@jku.at>
parents: 16067
diff changeset
775 }
76081918079d Truffle: move TraceRewrites code to NodeUtil
Andreas Woess <andreas.woess@jku.at>
parents: 16067
diff changeset
776
76081918079d Truffle: move TraceRewrites code to NodeUtil
Andreas Woess <andreas.woess@jku.at>
parents: 16067
diff changeset
777 String filter = TruffleOptions.TraceRewritesFilterClass;
76081918079d Truffle: move TraceRewrites code to NodeUtil
Andreas Woess <andreas.woess@jku.at>
parents: 16067
diff changeset
778 Class<? extends Node> from = oldNode.getClass();
76081918079d Truffle: move TraceRewrites code to NodeUtil
Andreas Woess <andreas.woess@jku.at>
parents: 16067
diff changeset
779 Class<? extends Node> to = newNode.getClass();
76081918079d Truffle: move TraceRewrites code to NodeUtil
Andreas Woess <andreas.woess@jku.at>
parents: 16067
diff changeset
780 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
781 return;
76081918079d Truffle: move TraceRewrites code to NodeUtil
Andreas Woess <andreas.woess@jku.at>
parents: 16067
diff changeset
782 }
76081918079d Truffle: move TraceRewrites code to NodeUtil
Andreas Woess <andreas.woess@jku.at>
parents: 16067
diff changeset
783
76081918079d Truffle: move TraceRewrites code to NodeUtil
Andreas Woess <andreas.woess@jku.at>
parents: 16067
diff changeset
784 final SourceSection reportedSourceSection = oldNode.getEncapsulatingSourceSection();
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 PrintStream out = System.out;
76081918079d Truffle: move TraceRewrites code to NodeUtil
Andreas Woess <andreas.woess@jku.at>
parents: 16067
diff changeset
787 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
788 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
789 }
76081918079d Truffle: move TraceRewrites code to NodeUtil
Andreas Woess <andreas.woess@jku.at>
parents: 16067
diff changeset
790
76081918079d Truffle: move TraceRewrites code to NodeUtil
Andreas Woess <andreas.woess@jku.at>
parents: 16067
diff changeset
791 private static String formatNodeInfo(Node node) {
76081918079d Truffle: move TraceRewrites code to NodeUtil
Andreas Woess <andreas.woess@jku.at>
parents: 16067
diff changeset
792 String cost = "?";
76081918079d Truffle: move TraceRewrites code to NodeUtil
Andreas Woess <andreas.woess@jku.at>
parents: 16067
diff changeset
793 switch (node.getCost()) {
76081918079d Truffle: move TraceRewrites code to NodeUtil
Andreas Woess <andreas.woess@jku.at>
parents: 16067
diff changeset
794 case NONE:
76081918079d Truffle: move TraceRewrites code to NodeUtil
Andreas Woess <andreas.woess@jku.at>
parents: 16067
diff changeset
795 cost = "G";
76081918079d Truffle: move TraceRewrites code to NodeUtil
Andreas Woess <andreas.woess@jku.at>
parents: 16067
diff changeset
796 break;
76081918079d Truffle: move TraceRewrites code to NodeUtil
Andreas Woess <andreas.woess@jku.at>
parents: 16067
diff changeset
797 case MONOMORPHIC:
76081918079d Truffle: move TraceRewrites code to NodeUtil
Andreas Woess <andreas.woess@jku.at>
parents: 16067
diff changeset
798 cost = "M";
76081918079d Truffle: move TraceRewrites code to NodeUtil
Andreas Woess <andreas.woess@jku.at>
parents: 16067
diff changeset
799 break;
76081918079d Truffle: move TraceRewrites code to NodeUtil
Andreas Woess <andreas.woess@jku.at>
parents: 16067
diff changeset
800 case POLYMORPHIC:
76081918079d Truffle: move TraceRewrites code to NodeUtil
Andreas Woess <andreas.woess@jku.at>
parents: 16067
diff changeset
801 cost = "P";
76081918079d Truffle: move TraceRewrites code to NodeUtil
Andreas Woess <andreas.woess@jku.at>
parents: 16067
diff changeset
802 break;
76081918079d Truffle: move TraceRewrites code to NodeUtil
Andreas Woess <andreas.woess@jku.at>
parents: 16067
diff changeset
803 case MEGAMORPHIC:
76081918079d Truffle: move TraceRewrites code to NodeUtil
Andreas Woess <andreas.woess@jku.at>
parents: 16067
diff changeset
804 cost = "G";
76081918079d Truffle: move TraceRewrites code to NodeUtil
Andreas Woess <andreas.woess@jku.at>
parents: 16067
diff changeset
805 break;
76081918079d Truffle: move TraceRewrites code to NodeUtil
Andreas Woess <andreas.woess@jku.at>
parents: 16067
diff changeset
806 default:
76081918079d Truffle: move TraceRewrites code to NodeUtil
Andreas Woess <andreas.woess@jku.at>
parents: 16067
diff changeset
807 cost = "?";
76081918079d Truffle: move TraceRewrites code to NodeUtil
Andreas Woess <andreas.woess@jku.at>
parents: 16067
diff changeset
808 break;
76081918079d Truffle: move TraceRewrites code to NodeUtil
Andreas Woess <andreas.woess@jku.at>
parents: 16067
diff changeset
809 }
76081918079d Truffle: move TraceRewrites code to NodeUtil
Andreas Woess <andreas.woess@jku.at>
parents: 16067
diff changeset
810 return cost + " " + node.getClass().getSimpleName();
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 filterByKind(Node node, NodeCost cost) {
76081918079d Truffle: move TraceRewrites code to NodeUtil
Andreas Woess <andreas.woess@jku.at>
parents: 16067
diff changeset
814 return node.getCost() == cost;
76081918079d Truffle: move TraceRewrites code to NodeUtil
Andreas Woess <andreas.woess@jku.at>
parents: 16067
diff changeset
815 }
76081918079d Truffle: move TraceRewrites code to NodeUtil
Andreas Woess <andreas.woess@jku.at>
parents: 16067
diff changeset
816
76081918079d Truffle: move TraceRewrites code to NodeUtil
Andreas Woess <andreas.woess@jku.at>
parents: 16067
diff changeset
817 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
818 Class<?> currentFrom = from;
76081918079d Truffle: move TraceRewrites code to NodeUtil
Andreas Woess <andreas.woess@jku.at>
parents: 16067
diff changeset
819 while (currentFrom != null) {
76081918079d Truffle: move TraceRewrites code to NodeUtil
Andreas Woess <andreas.woess@jku.at>
parents: 16067
diff changeset
820 if (currentFrom.getName().contains(filter)) {
76081918079d Truffle: move TraceRewrites code to NodeUtil
Andreas Woess <andreas.woess@jku.at>
parents: 16067
diff changeset
821 return false;
76081918079d Truffle: move TraceRewrites code to NodeUtil
Andreas Woess <andreas.woess@jku.at>
parents: 16067
diff changeset
822 }
76081918079d Truffle: move TraceRewrites code to NodeUtil
Andreas Woess <andreas.woess@jku.at>
parents: 16067
diff changeset
823 currentFrom = currentFrom.getSuperclass();
76081918079d Truffle: move TraceRewrites code to NodeUtil
Andreas Woess <andreas.woess@jku.at>
parents: 16067
diff changeset
824 }
76081918079d Truffle: move TraceRewrites code to NodeUtil
Andreas Woess <andreas.woess@jku.at>
parents: 16067
diff changeset
825 return true;
76081918079d Truffle: move TraceRewrites code to NodeUtil
Andreas Woess <andreas.woess@jku.at>
parents: 16067
diff changeset
826 }
20131
4b12d5355811 Truffle: do not use iterators for visitors.
Christian Humer <christian.humer@gmail.com>
parents: 20130
diff changeset
827
4b12d5355811 Truffle: do not use iterators for visitors.
Christian Humer <christian.humer@gmail.com>
parents: 20130
diff changeset
828 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
829
4b12d5355811 Truffle: do not use iterators for visitors.
Christian Humer <christian.humer@gmail.com>
parents: 20130
diff changeset
830 public int count;
4b12d5355811 Truffle: do not use iterators for visitors.
Christian Humer <christian.humer@gmail.com>
parents: 20130
diff changeset
831 private final NodeCountFilter filter;
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 NodeCounter(NodeCountFilter filter) {
4b12d5355811 Truffle: do not use iterators for visitors.
Christian Humer <christian.humer@gmail.com>
parents: 20130
diff changeset
834 this.filter = filter;
4b12d5355811 Truffle: do not use iterators for visitors.
Christian Humer <christian.humer@gmail.com>
parents: 20130
diff changeset
835 }
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 public boolean visit(Node node) {
4b12d5355811 Truffle: do not use iterators for visitors.
Christian Humer <christian.humer@gmail.com>
parents: 20130
diff changeset
838 if (filter.isCounted(node)) {
4b12d5355811 Truffle: do not use iterators for visitors.
Christian Humer <christian.humer@gmail.com>
parents: 20130
diff changeset
839 count++;
4b12d5355811 Truffle: do not use iterators for visitors.
Christian Humer <christian.humer@gmail.com>
parents: 20130
diff changeset
840 }
4b12d5355811 Truffle: do not use iterators for visitors.
Christian Humer <christian.humer@gmail.com>
parents: 20130
diff changeset
841 return true;
4b12d5355811 Truffle: do not use iterators for visitors.
Christian Humer <christian.humer@gmail.com>
parents: 20130
diff changeset
842 }
4b12d5355811 Truffle: do not use iterators for visitors.
Christian Humer <christian.humer@gmail.com>
parents: 20130
diff changeset
843
4b12d5355811 Truffle: do not use iterators for visitors.
Christian Humer <christian.humer@gmail.com>
parents: 20130
diff changeset
844 }
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
845 }