annotate graal/com.oracle.truffle.api/src/com/oracle/truffle/api/nodes/NodeUtil.java @ 19607:73811d1b4cd0

Truffle: add NodeUtil.forEachChild
author Andreas Woess <andreas.woess@oracle.com>
date Fri, 27 Feb 2015 14:57:27 +0100
parents 67d9e635102f
children f682b9e6ca07
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.*;
16779
e52ad0d3b7d6 put some security sensitive actions into a privileged action
Doug Simon <doug.simon@oracle.com>
parents: 16674
diff changeset
30 import java.security.*;
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
31 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
32
a4b84ba6dc2e Introduction of the Truffle API for efficient implementation of dynamic languages on top of the Graal VM. New projects com.oracle.truffle.api 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 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
34
11848
099af41815ea Truffle: add NodeUtil.printSourceAttributionTree() for debugging
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 11809
diff changeset
35 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
36 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
37 import com.oracle.truffle.api.instrument.ProbeNode.WrapperNode;
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
38 import com.oracle.truffle.api.nodes.Node.Child;
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 import com.oracle.truffle.api.nodes.Node.Children;
16067
915ebb306fcc Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 15834
diff changeset
40 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
41
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
42 /**
a4b84ba6dc2e Introduction of the Truffle API for efficient implementation of dynamic languages on top of the Graal VM. New projects com.oracle.truffle.api for the API definition and com.oracle.truffle.api.test for API tests and documentation.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
43 * 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
44 */
11636
136eaa90ef41 Truffle: make NodeUtil class final and its unsafe field private.
Andreas Woess <andreas.woess@jku.at>
parents: 11622
diff changeset
45 public final class NodeUtil {
18905
c46e268fd091 Truffle: add (and switch to) safe node field access implementation using reflection
Andreas Woess <andreas.woess@jku.at>
parents: 18904
diff changeset
46 private static final boolean USE_UNSAFE = Boolean.getBoolean("truffle.unsafe");
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
47
9754
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 * 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
50 * accesses.
98b004bf3985 Refactoring of NodeUtil to centralize the use of reflection
Christian Wimmer <christian.wimmer@oracle.com>
parents: 9538
diff changeset
51 */
98b004bf3985 Refactoring of NodeUtil to centralize the use of reflection
Christian Wimmer <christian.wimmer@oracle.com>
parents: 9538
diff changeset
52 public interface FieldOffsetProvider {
98b004bf3985 Refactoring of NodeUtil to centralize the use of reflection
Christian Wimmer <christian.wimmer@oracle.com>
parents: 9538
diff changeset
53
98b004bf3985 Refactoring of NodeUtil to centralize the use of reflection
Christian Wimmer <christian.wimmer@oracle.com>
parents: 9538
diff changeset
54 long objectFieldOffset(Field field);
15834
fc6f8d143c68 Introduce method to customize type size
Christian Wimmer <christian.wimmer@oracle.com>
parents: 15093
diff changeset
55
fc6f8d143c68 Introduce method to customize type size
Christian Wimmer <christian.wimmer@oracle.com>
parents: 15093
diff changeset
56 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
57 }
98b004bf3985 Refactoring of NodeUtil to centralize the use of reflection
Christian Wimmer <christian.wimmer@oracle.com>
parents: 9538
diff changeset
58
98b004bf3985 Refactoring of NodeUtil to centralize the use of reflection
Christian Wimmer <christian.wimmer@oracle.com>
parents: 9538
diff changeset
59 private static final FieldOffsetProvider unsafeFieldOffsetProvider = new FieldOffsetProvider() {
98b004bf3985 Refactoring of NodeUtil to centralize the use of reflection
Christian Wimmer <christian.wimmer@oracle.com>
parents: 9538
diff changeset
60
98b004bf3985 Refactoring of NodeUtil to centralize the use of reflection
Christian Wimmer <christian.wimmer@oracle.com>
parents: 9538
diff changeset
61 @Override
98b004bf3985 Refactoring of NodeUtil to centralize the use of reflection
Christian Wimmer <christian.wimmer@oracle.com>
parents: 9538
diff changeset
62 public long objectFieldOffset(Field field) {
98b004bf3985 Refactoring of NodeUtil to centralize the use of reflection
Christian Wimmer <christian.wimmer@oracle.com>
parents: 9538
diff changeset
63 return unsafe.objectFieldOffset(field);
98b004bf3985 Refactoring of NodeUtil to centralize the use of reflection
Christian Wimmer <christian.wimmer@oracle.com>
parents: 9538
diff changeset
64 }
15834
fc6f8d143c68 Introduce method to customize type size
Christian Wimmer <christian.wimmer@oracle.com>
parents: 15093
diff changeset
65
fc6f8d143c68 Introduce method to customize type size
Christian Wimmer <christian.wimmer@oracle.com>
parents: 15093
diff changeset
66 @Override
fc6f8d143c68 Introduce method to customize type size
Christian Wimmer <christian.wimmer@oracle.com>
parents: 15093
diff changeset
67 public int getTypeSize(Class<?> clazz) {
fc6f8d143c68 Introduce method to customize type size
Christian Wimmer <christian.wimmer@oracle.com>
parents: 15093
diff changeset
68 if (!clazz.isPrimitive()) {
fc6f8d143c68 Introduce method to customize type size
Christian Wimmer <christian.wimmer@oracle.com>
parents: 15093
diff changeset
69 return Unsafe.ARRAY_OBJECT_INDEX_SCALE;
fc6f8d143c68 Introduce method to customize type size
Christian Wimmer <christian.wimmer@oracle.com>
parents: 15093
diff changeset
70 } else if (clazz == int.class) {
fc6f8d143c68 Introduce method to customize type size
Christian Wimmer <christian.wimmer@oracle.com>
parents: 15093
diff changeset
71 return Unsafe.ARRAY_INT_INDEX_SCALE;
fc6f8d143c68 Introduce method to customize type size
Christian Wimmer <christian.wimmer@oracle.com>
parents: 15093
diff changeset
72 } else {
fc6f8d143c68 Introduce method to customize type size
Christian Wimmer <christian.wimmer@oracle.com>
parents: 15093
diff changeset
73 throw new UnsupportedOperationException("unsupported field type: " + clazz);
fc6f8d143c68 Introduce method to customize type size
Christian Wimmer <christian.wimmer@oracle.com>
parents: 15093
diff changeset
74 }
fc6f8d143c68 Introduce method to customize type size
Christian Wimmer <christian.wimmer@oracle.com>
parents: 15093
diff changeset
75 }
9754
98b004bf3985 Refactoring of NodeUtil to centralize the use of reflection
Christian Wimmer <christian.wimmer@oracle.com>
parents: 9538
diff changeset
76 };
98b004bf3985 Refactoring of NodeUtil to centralize the use of reflection
Christian Wimmer <christian.wimmer@oracle.com>
parents: 9538
diff changeset
77
98b004bf3985 Refactoring of NodeUtil to centralize the use of reflection
Christian Wimmer <christian.wimmer@oracle.com>
parents: 9538
diff changeset
78 public static enum NodeFieldKind {
98b004bf3985 Refactoring of NodeUtil to centralize the use of reflection
Christian Wimmer <christian.wimmer@oracle.com>
parents: 9538
diff changeset
79 /** The single {@link Node#getParent() parent} field. */
98b004bf3985 Refactoring of NodeUtil to centralize the use of reflection
Christian Wimmer <christian.wimmer@oracle.com>
parents: 9538
diff changeset
80 PARENT,
98b004bf3985 Refactoring of NodeUtil to centralize the use of reflection
Christian Wimmer <christian.wimmer@oracle.com>
parents: 9538
diff changeset
81 /** A field annotated with {@link Child}. */
98b004bf3985 Refactoring of NodeUtil to centralize the use of reflection
Christian Wimmer <christian.wimmer@oracle.com>
parents: 9538
diff changeset
82 CHILD,
98b004bf3985 Refactoring of NodeUtil to centralize the use of reflection
Christian Wimmer <christian.wimmer@oracle.com>
parents: 9538
diff changeset
83 /** A field annotated with {@link Children}. */
98b004bf3985 Refactoring of NodeUtil to centralize the use of reflection
Christian Wimmer <christian.wimmer@oracle.com>
parents: 9538
diff changeset
84 CHILDREN,
98b004bf3985 Refactoring of NodeUtil to centralize the use of reflection
Christian Wimmer <christian.wimmer@oracle.com>
parents: 9538
diff changeset
85 /** A normal non-child data field of the node. */
98b004bf3985 Refactoring of NodeUtil to centralize the use of reflection
Christian Wimmer <christian.wimmer@oracle.com>
parents: 9538
diff changeset
86 DATA
98b004bf3985 Refactoring of NodeUtil to centralize the use of reflection
Christian Wimmer <christian.wimmer@oracle.com>
parents: 9538
diff changeset
87 }
98b004bf3985 Refactoring of NodeUtil to centralize the use of reflection
Christian Wimmer <christian.wimmer@oracle.com>
parents: 9538
diff changeset
88
98b004bf3985 Refactoring of NodeUtil to centralize the use of reflection
Christian Wimmer <christian.wimmer@oracle.com>
parents: 9538
diff changeset
89 /**
98b004bf3985 Refactoring of NodeUtil to centralize the use of reflection
Christian Wimmer <christian.wimmer@oracle.com>
parents: 9538
diff changeset
90 * Information about a field in a {@link Node} class.
98b004bf3985 Refactoring of NodeUtil to centralize the use of reflection
Christian Wimmer <christian.wimmer@oracle.com>
parents: 9538
diff changeset
91 */
18905
c46e268fd091 Truffle: add (and switch to) safe node field access implementation using reflection
Andreas Woess <andreas.woess@jku.at>
parents: 18904
diff changeset
92 public abstract static class NodeField {
9754
98b004bf3985 Refactoring of NodeUtil to centralize the use of reflection
Christian Wimmer <christian.wimmer@oracle.com>
parents: 9538
diff changeset
93
98b004bf3985 Refactoring of NodeUtil to centralize the use of reflection
Christian Wimmer <christian.wimmer@oracle.com>
parents: 9538
diff changeset
94 private final NodeFieldKind kind;
98b004bf3985 Refactoring of NodeUtil to centralize the use of reflection
Christian Wimmer <christian.wimmer@oracle.com>
parents: 9538
diff changeset
95 private final String name;
18905
c46e268fd091 Truffle: add (and switch to) safe node field access implementation using reflection
Andreas Woess <andreas.woess@jku.at>
parents: 18904
diff changeset
96 protected final Class<?> type;
c46e268fd091 Truffle: add (and switch to) safe node field access implementation using reflection
Andreas Woess <andreas.woess@jku.at>
parents: 18904
diff changeset
97 protected final long offset;
9754
98b004bf3985 Refactoring of NodeUtil to centralize the use of reflection
Christian Wimmer <christian.wimmer@oracle.com>
parents: 9538
diff changeset
98
18905
c46e268fd091 Truffle: add (and switch to) safe node field access implementation using reflection
Andreas Woess <andreas.woess@jku.at>
parents: 18904
diff changeset
99 protected NodeField(NodeFieldKind kind, Field field) {
9754
98b004bf3985 Refactoring of NodeUtil to centralize the use of reflection
Christian Wimmer <christian.wimmer@oracle.com>
parents: 9538
diff changeset
100 this.kind = kind;
18904
6ba170cb6f53 Truffle: channel all NodeUtil field accesses through NodeField class
Andreas Woess <andreas.woess@jku.at>
parents: 18751
diff changeset
101 this.type = field.getType();
6ba170cb6f53 Truffle: channel all NodeUtil field accesses through NodeField class
Andreas Woess <andreas.woess@jku.at>
parents: 18751
diff changeset
102 this.name = field.getName();
6ba170cb6f53 Truffle: channel all NodeUtil field accesses through NodeField class
Andreas Woess <andreas.woess@jku.at>
parents: 18751
diff changeset
103 this.offset = unsafeFieldOffsetProvider.objectFieldOffset(field);
6ba170cb6f53 Truffle: channel all NodeUtil field accesses through NodeField class
Andreas Woess <andreas.woess@jku.at>
parents: 18751
diff changeset
104 }
6ba170cb6f53 Truffle: channel all NodeUtil field accesses through NodeField class
Andreas Woess <andreas.woess@jku.at>
parents: 18751
diff changeset
105
6ba170cb6f53 Truffle: channel all NodeUtil field accesses through NodeField class
Andreas Woess <andreas.woess@jku.at>
parents: 18751
diff changeset
106 protected static NodeField create(NodeFieldKind kind, Field field) {
18905
c46e268fd091 Truffle: add (and switch to) safe node field access implementation using reflection
Andreas Woess <andreas.woess@jku.at>
parents: 18904
diff changeset
107 if (USE_UNSAFE) {
c46e268fd091 Truffle: add (and switch to) safe node field access implementation using reflection
Andreas Woess <andreas.woess@jku.at>
parents: 18904
diff changeset
108 return new UnsafeNodeField(kind, field);
c46e268fd091 Truffle: add (and switch to) safe node field access implementation using reflection
Andreas Woess <andreas.woess@jku.at>
parents: 18904
diff changeset
109 } else {
c46e268fd091 Truffle: add (and switch to) safe node field access implementation using reflection
Andreas Woess <andreas.woess@jku.at>
parents: 18904
diff changeset
110 return new ReflectionNodeField(kind, field);
c46e268fd091 Truffle: add (and switch to) safe node field access implementation using reflection
Andreas Woess <andreas.woess@jku.at>
parents: 18904
diff changeset
111 }
9754
98b004bf3985 Refactoring of NodeUtil to centralize the use of reflection
Christian Wimmer <christian.wimmer@oracle.com>
parents: 9538
diff changeset
112 }
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
113
9754
98b004bf3985 Refactoring of NodeUtil to centralize the use of reflection
Christian Wimmer <christian.wimmer@oracle.com>
parents: 9538
diff changeset
114 public NodeFieldKind getKind() {
98b004bf3985 Refactoring of NodeUtil to centralize the use of reflection
Christian Wimmer <christian.wimmer@oracle.com>
parents: 9538
diff changeset
115 return kind;
98b004bf3985 Refactoring of NodeUtil to centralize the use of reflection
Christian Wimmer <christian.wimmer@oracle.com>
parents: 9538
diff changeset
116 }
98b004bf3985 Refactoring of NodeUtil to centralize the use of reflection
Christian Wimmer <christian.wimmer@oracle.com>
parents: 9538
diff changeset
117
98b004bf3985 Refactoring of NodeUtil to centralize the use of reflection
Christian Wimmer <christian.wimmer@oracle.com>
parents: 9538
diff changeset
118 public Class<?> getType() {
98b004bf3985 Refactoring of NodeUtil to centralize the use of reflection
Christian Wimmer <christian.wimmer@oracle.com>
parents: 9538
diff changeset
119 return type;
98b004bf3985 Refactoring of NodeUtil to centralize the use of reflection
Christian Wimmer <christian.wimmer@oracle.com>
parents: 9538
diff changeset
120 }
98b004bf3985 Refactoring of NodeUtil to centralize the use of reflection
Christian Wimmer <christian.wimmer@oracle.com>
parents: 9538
diff changeset
121
98b004bf3985 Refactoring of NodeUtil to centralize the use of reflection
Christian Wimmer <christian.wimmer@oracle.com>
parents: 9538
diff changeset
122 public String getName() {
98b004bf3985 Refactoring of NodeUtil to centralize the use of reflection
Christian Wimmer <christian.wimmer@oracle.com>
parents: 9538
diff changeset
123 return name;
98b004bf3985 Refactoring of NodeUtil to centralize the use of reflection
Christian Wimmer <christian.wimmer@oracle.com>
parents: 9538
diff changeset
124 }
98b004bf3985 Refactoring of NodeUtil to centralize the use of reflection
Christian Wimmer <christian.wimmer@oracle.com>
parents: 9538
diff changeset
125
98b004bf3985 Refactoring of NodeUtil to centralize the use of reflection
Christian Wimmer <christian.wimmer@oracle.com>
parents: 9538
diff changeset
126 public long getOffset() {
98b004bf3985 Refactoring of NodeUtil to centralize the use of reflection
Christian Wimmer <christian.wimmer@oracle.com>
parents: 9538
diff changeset
127 return offset;
98b004bf3985 Refactoring of NodeUtil to centralize the use of reflection
Christian Wimmer <christian.wimmer@oracle.com>
parents: 9538
diff changeset
128 }
98b004bf3985 Refactoring of NodeUtil to centralize the use of reflection
Christian Wimmer <christian.wimmer@oracle.com>
parents: 9538
diff changeset
129
18905
c46e268fd091 Truffle: add (and switch to) safe node field access implementation using reflection
Andreas Woess <andreas.woess@jku.at>
parents: 18904
diff changeset
130 public abstract void putObject(Node receiver, Object value);
c46e268fd091 Truffle: add (and switch to) safe node field access implementation using reflection
Andreas Woess <andreas.woess@jku.at>
parents: 18904
diff changeset
131
c46e268fd091 Truffle: add (and switch to) safe node field access implementation using reflection
Andreas Woess <andreas.woess@jku.at>
parents: 18904
diff changeset
132 public abstract Object getObject(Node receiver);
c46e268fd091 Truffle: add (and switch to) safe node field access implementation using reflection
Andreas Woess <andreas.woess@jku.at>
parents: 18904
diff changeset
133
c46e268fd091 Truffle: add (and switch to) safe node field access implementation using reflection
Andreas Woess <andreas.woess@jku.at>
parents: 18904
diff changeset
134 public abstract Object loadValue(Node node);
c46e268fd091 Truffle: add (and switch to) safe node field access implementation using reflection
Andreas Woess <andreas.woess@jku.at>
parents: 18904
diff changeset
135
c46e268fd091 Truffle: add (and switch to) safe node field access implementation using reflection
Andreas Woess <andreas.woess@jku.at>
parents: 18904
diff changeset
136 @Override
c46e268fd091 Truffle: add (and switch to) safe node field access implementation using reflection
Andreas Woess <andreas.woess@jku.at>
parents: 18904
diff changeset
137 public int hashCode() {
c46e268fd091 Truffle: add (and switch to) safe node field access implementation using reflection
Andreas Woess <andreas.woess@jku.at>
parents: 18904
diff changeset
138 return kind.hashCode() | type.hashCode() | name.hashCode() | ((Long) offset).hashCode();
c46e268fd091 Truffle: add (and switch to) safe node field access implementation using reflection
Andreas Woess <andreas.woess@jku.at>
parents: 18904
diff changeset
139 }
c46e268fd091 Truffle: add (and switch to) safe node field access implementation using reflection
Andreas Woess <andreas.woess@jku.at>
parents: 18904
diff changeset
140
c46e268fd091 Truffle: add (and switch to) safe node field access implementation using reflection
Andreas Woess <andreas.woess@jku.at>
parents: 18904
diff changeset
141 @Override
c46e268fd091 Truffle: add (and switch to) safe node field access implementation using reflection
Andreas Woess <andreas.woess@jku.at>
parents: 18904
diff changeset
142 public boolean equals(Object obj) {
c46e268fd091 Truffle: add (and switch to) safe node field access implementation using reflection
Andreas Woess <andreas.woess@jku.at>
parents: 18904
diff changeset
143 if (obj instanceof NodeField) {
c46e268fd091 Truffle: add (and switch to) safe node field access implementation using reflection
Andreas Woess <andreas.woess@jku.at>
parents: 18904
diff changeset
144 NodeField other = (NodeField) obj;
c46e268fd091 Truffle: add (and switch to) safe node field access implementation using reflection
Andreas Woess <andreas.woess@jku.at>
parents: 18904
diff changeset
145 return offset == other.offset && name.equals(other.name) && type.equals(other.type) && kind.equals(other.kind);
c46e268fd091 Truffle: add (and switch to) safe node field access implementation using reflection
Andreas Woess <andreas.woess@jku.at>
parents: 18904
diff changeset
146 }
c46e268fd091 Truffle: add (and switch to) safe node field access implementation using reflection
Andreas Woess <andreas.woess@jku.at>
parents: 18904
diff changeset
147 return false;
c46e268fd091 Truffle: add (and switch to) safe node field access implementation using reflection
Andreas Woess <andreas.woess@jku.at>
parents: 18904
diff changeset
148 }
c46e268fd091 Truffle: add (and switch to) safe node field access implementation using reflection
Andreas Woess <andreas.woess@jku.at>
parents: 18904
diff changeset
149 }
c46e268fd091 Truffle: add (and switch to) safe node field access implementation using reflection
Andreas Woess <andreas.woess@jku.at>
parents: 18904
diff changeset
150
c46e268fd091 Truffle: add (and switch to) safe node field access implementation using reflection
Andreas Woess <andreas.woess@jku.at>
parents: 18904
diff changeset
151 private static final class UnsafeNodeField extends NodeField {
c46e268fd091 Truffle: add (and switch to) safe node field access implementation using reflection
Andreas Woess <andreas.woess@jku.at>
parents: 18904
diff changeset
152 protected UnsafeNodeField(NodeFieldKind kind, Field field) {
c46e268fd091 Truffle: add (and switch to) safe node field access implementation using reflection
Andreas Woess <andreas.woess@jku.at>
parents: 18904
diff changeset
153 super(kind, field);
c46e268fd091 Truffle: add (and switch to) safe node field access implementation using reflection
Andreas Woess <andreas.woess@jku.at>
parents: 18904
diff changeset
154 }
c46e268fd091 Truffle: add (and switch to) safe node field access implementation using reflection
Andreas Woess <andreas.woess@jku.at>
parents: 18904
diff changeset
155
c46e268fd091 Truffle: add (and switch to) safe node field access implementation using reflection
Andreas Woess <andreas.woess@jku.at>
parents: 18904
diff changeset
156 @Override
c46e268fd091 Truffle: add (and switch to) safe node field access implementation using reflection
Andreas Woess <andreas.woess@jku.at>
parents: 18904
diff changeset
157 public void putObject(Node receiver, Object value) {
18904
6ba170cb6f53 Truffle: channel all NodeUtil field accesses through NodeField class
Andreas Woess <andreas.woess@jku.at>
parents: 18751
diff changeset
158 assert !type.isPrimitive() && value == null || type.isInstance(value);
18751
e55e18c1f40d Truffle: add putObject to node reflection.
Christian Humer <christian.humer@gmail.com>
parents: 18750
diff changeset
159 unsafe.putObject(receiver, offset, value);
e55e18c1f40d Truffle: add putObject to node reflection.
Christian Humer <christian.humer@gmail.com>
parents: 18750
diff changeset
160 }
e55e18c1f40d Truffle: add putObject to node reflection.
Christian Humer <christian.humer@gmail.com>
parents: 18750
diff changeset
161
18905
c46e268fd091 Truffle: add (and switch to) safe node field access implementation using reflection
Andreas Woess <andreas.woess@jku.at>
parents: 18904
diff changeset
162 @Override
c46e268fd091 Truffle: add (and switch to) safe node field access implementation using reflection
Andreas Woess <andreas.woess@jku.at>
parents: 18904
diff changeset
163 public Object getObject(Node receiver) {
18904
6ba170cb6f53 Truffle: channel all NodeUtil field accesses through NodeField class
Andreas Woess <andreas.woess@jku.at>
parents: 18751
diff changeset
164 assert !type.isPrimitive();
6ba170cb6f53 Truffle: channel all NodeUtil field accesses through NodeField class
Andreas Woess <andreas.woess@jku.at>
parents: 18751
diff changeset
165 return unsafe.getObject(receiver, offset);
6ba170cb6f53 Truffle: channel all NodeUtil field accesses through NodeField class
Andreas Woess <andreas.woess@jku.at>
parents: 18751
diff changeset
166 }
6ba170cb6f53 Truffle: channel all NodeUtil field accesses through NodeField class
Andreas Woess <andreas.woess@jku.at>
parents: 18751
diff changeset
167
18905
c46e268fd091 Truffle: add (and switch to) safe node field access implementation using reflection
Andreas Woess <andreas.woess@jku.at>
parents: 18904
diff changeset
168 @Override
9754
98b004bf3985 Refactoring of NodeUtil to centralize the use of reflection
Christian Wimmer <christian.wimmer@oracle.com>
parents: 9538
diff changeset
169 public Object loadValue(Node node) {
98b004bf3985 Refactoring of NodeUtil to centralize the use of reflection
Christian Wimmer <christian.wimmer@oracle.com>
parents: 9538
diff changeset
170 if (type == boolean.class) {
98b004bf3985 Refactoring of NodeUtil to centralize the use of reflection
Christian Wimmer <christian.wimmer@oracle.com>
parents: 9538
diff changeset
171 return unsafe.getBoolean(node, offset);
98b004bf3985 Refactoring of NodeUtil to centralize the use of reflection
Christian Wimmer <christian.wimmer@oracle.com>
parents: 9538
diff changeset
172 } else if (type == byte.class) {
98b004bf3985 Refactoring of NodeUtil to centralize the use of reflection
Christian Wimmer <christian.wimmer@oracle.com>
parents: 9538
diff changeset
173 return unsafe.getByte(node, offset);
98b004bf3985 Refactoring of NodeUtil to centralize the use of reflection
Christian Wimmer <christian.wimmer@oracle.com>
parents: 9538
diff changeset
174 } else if (type == short.class) {
98b004bf3985 Refactoring of NodeUtil to centralize the use of reflection
Christian Wimmer <christian.wimmer@oracle.com>
parents: 9538
diff changeset
175 return unsafe.getShort(node, offset);
98b004bf3985 Refactoring of NodeUtil to centralize the use of reflection
Christian Wimmer <christian.wimmer@oracle.com>
parents: 9538
diff changeset
176 } else if (type == char.class) {
98b004bf3985 Refactoring of NodeUtil to centralize the use of reflection
Christian Wimmer <christian.wimmer@oracle.com>
parents: 9538
diff changeset
177 return unsafe.getChar(node, offset);
98b004bf3985 Refactoring of NodeUtil to centralize the use of reflection
Christian Wimmer <christian.wimmer@oracle.com>
parents: 9538
diff changeset
178 } else if (type == int.class) {
98b004bf3985 Refactoring of NodeUtil to centralize the use of reflection
Christian Wimmer <christian.wimmer@oracle.com>
parents: 9538
diff changeset
179 return unsafe.getInt(node, offset);
98b004bf3985 Refactoring of NodeUtil to centralize the use of reflection
Christian Wimmer <christian.wimmer@oracle.com>
parents: 9538
diff changeset
180 } else if (type == long.class) {
98b004bf3985 Refactoring of NodeUtil to centralize the use of reflection
Christian Wimmer <christian.wimmer@oracle.com>
parents: 9538
diff changeset
181 return unsafe.getLong(node, offset);
98b004bf3985 Refactoring of NodeUtil to centralize the use of reflection
Christian Wimmer <christian.wimmer@oracle.com>
parents: 9538
diff changeset
182 } else if (type == float.class) {
98b004bf3985 Refactoring of NodeUtil to centralize the use of reflection
Christian Wimmer <christian.wimmer@oracle.com>
parents: 9538
diff changeset
183 return unsafe.getFloat(node, offset);
98b004bf3985 Refactoring of NodeUtil to centralize the use of reflection
Christian Wimmer <christian.wimmer@oracle.com>
parents: 9538
diff changeset
184 } else if (type == double.class) {
98b004bf3985 Refactoring of NodeUtil to centralize the use of reflection
Christian Wimmer <christian.wimmer@oracle.com>
parents: 9538
diff changeset
185 return unsafe.getDouble(node, offset);
98b004bf3985 Refactoring of NodeUtil to centralize the use of reflection
Christian Wimmer <christian.wimmer@oracle.com>
parents: 9538
diff changeset
186 } else {
98b004bf3985 Refactoring of NodeUtil to centralize the use of reflection
Christian Wimmer <christian.wimmer@oracle.com>
parents: 9538
diff changeset
187 return unsafe.getObject(node, offset);
98b004bf3985 Refactoring of NodeUtil to centralize the use of reflection
Christian Wimmer <christian.wimmer@oracle.com>
parents: 9538
diff changeset
188 }
98b004bf3985 Refactoring of NodeUtil to centralize the use of reflection
Christian Wimmer <christian.wimmer@oracle.com>
parents: 9538
diff changeset
189 }
18905
c46e268fd091 Truffle: add (and switch to) safe node field access implementation using reflection
Andreas Woess <andreas.woess@jku.at>
parents: 18904
diff changeset
190 }
c46e268fd091 Truffle: add (and switch to) safe node field access implementation using reflection
Andreas Woess <andreas.woess@jku.at>
parents: 18904
diff changeset
191
c46e268fd091 Truffle: add (and switch to) safe node field access implementation using reflection
Andreas Woess <andreas.woess@jku.at>
parents: 18904
diff changeset
192 private static final class ReflectionNodeField extends NodeField {
c46e268fd091 Truffle: add (and switch to) safe node field access implementation using reflection
Andreas Woess <andreas.woess@jku.at>
parents: 18904
diff changeset
193 private final Field field;
c46e268fd091 Truffle: add (and switch to) safe node field access implementation using reflection
Andreas Woess <andreas.woess@jku.at>
parents: 18904
diff changeset
194
c46e268fd091 Truffle: add (and switch to) safe node field access implementation using reflection
Andreas Woess <andreas.woess@jku.at>
parents: 18904
diff changeset
195 protected ReflectionNodeField(NodeFieldKind kind, Field field) {
c46e268fd091 Truffle: add (and switch to) safe node field access implementation using reflection
Andreas Woess <andreas.woess@jku.at>
parents: 18904
diff changeset
196 super(kind, field);
c46e268fd091 Truffle: add (and switch to) safe node field access implementation using reflection
Andreas Woess <andreas.woess@jku.at>
parents: 18904
diff changeset
197 this.field = field;
c46e268fd091 Truffle: add (and switch to) safe node field access implementation using reflection
Andreas Woess <andreas.woess@jku.at>
parents: 18904
diff changeset
198 field.setAccessible(true);
c46e268fd091 Truffle: add (and switch to) safe node field access implementation using reflection
Andreas Woess <andreas.woess@jku.at>
parents: 18904
diff changeset
199 }
14743
762c9aceb7d8 Add equals/hashCode methods
Christian Wimmer <christian.wimmer@oracle.com>
parents: 14569
diff changeset
200
762c9aceb7d8 Add equals/hashCode methods
Christian Wimmer <christian.wimmer@oracle.com>
parents: 14569
diff changeset
201 @Override
18905
c46e268fd091 Truffle: add (and switch to) safe node field access implementation using reflection
Andreas Woess <andreas.woess@jku.at>
parents: 18904
diff changeset
202 public void putObject(Node receiver, Object value) {
c46e268fd091 Truffle: add (and switch to) safe node field access implementation using reflection
Andreas Woess <andreas.woess@jku.at>
parents: 18904
diff changeset
203 assert !type.isPrimitive() && value == null || type.isInstance(value);
c46e268fd091 Truffle: add (and switch to) safe node field access implementation using reflection
Andreas Woess <andreas.woess@jku.at>
parents: 18904
diff changeset
204 try {
c46e268fd091 Truffle: add (and switch to) safe node field access implementation using reflection
Andreas Woess <andreas.woess@jku.at>
parents: 18904
diff changeset
205 field.set(receiver, value);
c46e268fd091 Truffle: add (and switch to) safe node field access implementation using reflection
Andreas Woess <andreas.woess@jku.at>
parents: 18904
diff changeset
206 } catch (IllegalAccessException e) {
c46e268fd091 Truffle: add (and switch to) safe node field access implementation using reflection
Andreas Woess <andreas.woess@jku.at>
parents: 18904
diff changeset
207 throw new AssertionError(e);
c46e268fd091 Truffle: add (and switch to) safe node field access implementation using reflection
Andreas Woess <andreas.woess@jku.at>
parents: 18904
diff changeset
208 }
14743
762c9aceb7d8 Add equals/hashCode methods
Christian Wimmer <christian.wimmer@oracle.com>
parents: 14569
diff changeset
209 }
762c9aceb7d8 Add equals/hashCode methods
Christian Wimmer <christian.wimmer@oracle.com>
parents: 14569
diff changeset
210
762c9aceb7d8 Add equals/hashCode methods
Christian Wimmer <christian.wimmer@oracle.com>
parents: 14569
diff changeset
211 @Override
18905
c46e268fd091 Truffle: add (and switch to) safe node field access implementation using reflection
Andreas Woess <andreas.woess@jku.at>
parents: 18904
diff changeset
212 public Object getObject(Node receiver) {
c46e268fd091 Truffle: add (and switch to) safe node field access implementation using reflection
Andreas Woess <andreas.woess@jku.at>
parents: 18904
diff changeset
213 assert !type.isPrimitive();
c46e268fd091 Truffle: add (and switch to) safe node field access implementation using reflection
Andreas Woess <andreas.woess@jku.at>
parents: 18904
diff changeset
214 try {
c46e268fd091 Truffle: add (and switch to) safe node field access implementation using reflection
Andreas Woess <andreas.woess@jku.at>
parents: 18904
diff changeset
215 return field.get(receiver);
c46e268fd091 Truffle: add (and switch to) safe node field access implementation using reflection
Andreas Woess <andreas.woess@jku.at>
parents: 18904
diff changeset
216 } catch (IllegalAccessException e) {
c46e268fd091 Truffle: add (and switch to) safe node field access implementation using reflection
Andreas Woess <andreas.woess@jku.at>
parents: 18904
diff changeset
217 throw new AssertionError(e);
14743
762c9aceb7d8 Add equals/hashCode methods
Christian Wimmer <christian.wimmer@oracle.com>
parents: 14569
diff changeset
218 }
18905
c46e268fd091 Truffle: add (and switch to) safe node field access implementation using reflection
Andreas Woess <andreas.woess@jku.at>
parents: 18904
diff changeset
219 }
c46e268fd091 Truffle: add (and switch to) safe node field access implementation using reflection
Andreas Woess <andreas.woess@jku.at>
parents: 18904
diff changeset
220
c46e268fd091 Truffle: add (and switch to) safe node field access implementation using reflection
Andreas Woess <andreas.woess@jku.at>
parents: 18904
diff changeset
221 @Override
c46e268fd091 Truffle: add (and switch to) safe node field access implementation using reflection
Andreas Woess <andreas.woess@jku.at>
parents: 18904
diff changeset
222 public Object loadValue(Node node) {
c46e268fd091 Truffle: add (and switch to) safe node field access implementation using reflection
Andreas Woess <andreas.woess@jku.at>
parents: 18904
diff changeset
223 try {
c46e268fd091 Truffle: add (and switch to) safe node field access implementation using reflection
Andreas Woess <andreas.woess@jku.at>
parents: 18904
diff changeset
224 if (type == boolean.class) {
c46e268fd091 Truffle: add (and switch to) safe node field access implementation using reflection
Andreas Woess <andreas.woess@jku.at>
parents: 18904
diff changeset
225 return field.getBoolean(node);
c46e268fd091 Truffle: add (and switch to) safe node field access implementation using reflection
Andreas Woess <andreas.woess@jku.at>
parents: 18904
diff changeset
226 } else if (type == byte.class) {
c46e268fd091 Truffle: add (and switch to) safe node field access implementation using reflection
Andreas Woess <andreas.woess@jku.at>
parents: 18904
diff changeset
227 return field.getByte(node);
c46e268fd091 Truffle: add (and switch to) safe node field access implementation using reflection
Andreas Woess <andreas.woess@jku.at>
parents: 18904
diff changeset
228 } else if (type == short.class) {
c46e268fd091 Truffle: add (and switch to) safe node field access implementation using reflection
Andreas Woess <andreas.woess@jku.at>
parents: 18904
diff changeset
229 return field.getShort(node);
c46e268fd091 Truffle: add (and switch to) safe node field access implementation using reflection
Andreas Woess <andreas.woess@jku.at>
parents: 18904
diff changeset
230 } else if (type == char.class) {
c46e268fd091 Truffle: add (and switch to) safe node field access implementation using reflection
Andreas Woess <andreas.woess@jku.at>
parents: 18904
diff changeset
231 return field.getChar(node);
c46e268fd091 Truffle: add (and switch to) safe node field access implementation using reflection
Andreas Woess <andreas.woess@jku.at>
parents: 18904
diff changeset
232 } else if (type == int.class) {
c46e268fd091 Truffle: add (and switch to) safe node field access implementation using reflection
Andreas Woess <andreas.woess@jku.at>
parents: 18904
diff changeset
233 return field.getInt(node);
c46e268fd091 Truffle: add (and switch to) safe node field access implementation using reflection
Andreas Woess <andreas.woess@jku.at>
parents: 18904
diff changeset
234 } else if (type == long.class) {
c46e268fd091 Truffle: add (and switch to) safe node field access implementation using reflection
Andreas Woess <andreas.woess@jku.at>
parents: 18904
diff changeset
235 return field.getLong(node);
c46e268fd091 Truffle: add (and switch to) safe node field access implementation using reflection
Andreas Woess <andreas.woess@jku.at>
parents: 18904
diff changeset
236 } else if (type == float.class) {
c46e268fd091 Truffle: add (and switch to) safe node field access implementation using reflection
Andreas Woess <andreas.woess@jku.at>
parents: 18904
diff changeset
237 return field.getFloat(node);
c46e268fd091 Truffle: add (and switch to) safe node field access implementation using reflection
Andreas Woess <andreas.woess@jku.at>
parents: 18904
diff changeset
238 } else if (type == double.class) {
c46e268fd091 Truffle: add (and switch to) safe node field access implementation using reflection
Andreas Woess <andreas.woess@jku.at>
parents: 18904
diff changeset
239 return field.getDouble(node);
c46e268fd091 Truffle: add (and switch to) safe node field access implementation using reflection
Andreas Woess <andreas.woess@jku.at>
parents: 18904
diff changeset
240 } else {
c46e268fd091 Truffle: add (and switch to) safe node field access implementation using reflection
Andreas Woess <andreas.woess@jku.at>
parents: 18904
diff changeset
241 return field.get(node);
c46e268fd091 Truffle: add (and switch to) safe node field access implementation using reflection
Andreas Woess <andreas.woess@jku.at>
parents: 18904
diff changeset
242 }
c46e268fd091 Truffle: add (and switch to) safe node field access implementation using reflection
Andreas Woess <andreas.woess@jku.at>
parents: 18904
diff changeset
243 } catch (IllegalAccessException e) {
c46e268fd091 Truffle: add (and switch to) safe node field access implementation using reflection
Andreas Woess <andreas.woess@jku.at>
parents: 18904
diff changeset
244 throw new AssertionError(e);
c46e268fd091 Truffle: add (and switch to) safe node field access implementation using reflection
Andreas Woess <andreas.woess@jku.at>
parents: 18904
diff changeset
245 }
14743
762c9aceb7d8 Add equals/hashCode methods
Christian Wimmer <christian.wimmer@oracle.com>
parents: 14569
diff changeset
246 }
9754
98b004bf3985 Refactoring of NodeUtil to centralize the use of reflection
Christian Wimmer <christian.wimmer@oracle.com>
parents: 9538
diff changeset
247 }
98b004bf3985 Refactoring of NodeUtil to centralize the use of reflection
Christian Wimmer <christian.wimmer@oracle.com>
parents: 9538
diff changeset
248
98b004bf3985 Refactoring of NodeUtil to centralize the use of reflection
Christian Wimmer <christian.wimmer@oracle.com>
parents: 9538
diff changeset
249 /**
98b004bf3985 Refactoring of NodeUtil to centralize the use of reflection
Christian Wimmer <christian.wimmer@oracle.com>
parents: 9538
diff changeset
250 * Information about a {@link Node} class. A single instance of this class is allocated for
98b004bf3985 Refactoring of NodeUtil to centralize the use of reflection
Christian Wimmer <christian.wimmer@oracle.com>
parents: 9538
diff changeset
251 * every subclass of {@link Node} that is used.
98b004bf3985 Refactoring of NodeUtil to centralize the use of reflection
Christian Wimmer <christian.wimmer@oracle.com>
parents: 9538
diff changeset
252 */
98b004bf3985 Refactoring of NodeUtil to centralize the use of reflection
Christian Wimmer <christian.wimmer@oracle.com>
parents: 9538
diff changeset
253 public static final class NodeClass {
16466
d41922beb512 Truffle: use ClassValue for NodeClass
Andreas Woess <andreas.woess@jku.at>
parents: 16465
diff changeset
254 private static final ClassValue<NodeClass> nodeClasses = new ClassValue<NodeClass>() {
d41922beb512 Truffle: use ClassValue for NodeClass
Andreas Woess <andreas.woess@jku.at>
parents: 16465
diff changeset
255 @SuppressWarnings("unchecked")
d41922beb512 Truffle: use ClassValue for NodeClass
Andreas Woess <andreas.woess@jku.at>
parents: 16465
diff changeset
256 @Override
16779
e52ad0d3b7d6 put some security sensitive actions into a privileged action
Doug Simon <doug.simon@oracle.com>
parents: 16674
diff changeset
257 protected NodeClass computeValue(final Class<?> clazz) {
16466
d41922beb512 Truffle: use ClassValue for NodeClass
Andreas Woess <andreas.woess@jku.at>
parents: 16465
diff changeset
258 assert Node.class.isAssignableFrom(clazz);
16779
e52ad0d3b7d6 put some security sensitive actions into a privileged action
Doug Simon <doug.simon@oracle.com>
parents: 16674
diff changeset
259 return AccessController.doPrivileged(new PrivilegedAction<NodeClass>() {
e52ad0d3b7d6 put some security sensitive actions into a privileged action
Doug Simon <doug.simon@oracle.com>
parents: 16674
diff changeset
260 public NodeClass run() {
18904
6ba170cb6f53 Truffle: channel all NodeUtil field accesses through NodeField class
Andreas Woess <andreas.woess@jku.at>
parents: 18751
diff changeset
261 return new NodeClass((Class<? extends Node>) clazz);
16779
e52ad0d3b7d6 put some security sensitive actions into a privileged action
Doug Simon <doug.simon@oracle.com>
parents: 16674
diff changeset
262 }
e52ad0d3b7d6 put some security sensitive actions into a privileged action
Doug Simon <doug.simon@oracle.com>
parents: 16674
diff changeset
263 });
16466
d41922beb512 Truffle: use ClassValue for NodeClass
Andreas Woess <andreas.woess@jku.at>
parents: 16465
diff changeset
264 }
d41922beb512 Truffle: use ClassValue for NodeClass
Andreas Woess <andreas.woess@jku.at>
parents: 16465
diff changeset
265 };
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
266
9754
98b004bf3985 Refactoring of NodeUtil to centralize the use of reflection
Christian Wimmer <christian.wimmer@oracle.com>
parents: 9538
diff changeset
267 // The comprehensive list of all fields.
98b004bf3985 Refactoring of NodeUtil to centralize the use of reflection
Christian Wimmer <christian.wimmer@oracle.com>
parents: 9538
diff changeset
268 private final NodeField[] fields;
18904
6ba170cb6f53 Truffle: channel all NodeUtil field accesses through NodeField class
Andreas Woess <andreas.woess@jku.at>
parents: 18751
diff changeset
269 // Separate arrays for the frequently accessed fields.
6ba170cb6f53 Truffle: channel all NodeUtil field accesses through NodeField class
Andreas Woess <andreas.woess@jku.at>
parents: 18751
diff changeset
270 private final NodeField parentField;
6ba170cb6f53 Truffle: channel all NodeUtil field accesses through NodeField class
Andreas Woess <andreas.woess@jku.at>
parents: 18751
diff changeset
271 private final NodeField[] childFields;
6ba170cb6f53 Truffle: channel all NodeUtil field accesses through NodeField class
Andreas Woess <andreas.woess@jku.at>
parents: 18751
diff changeset
272 private final NodeField[] childrenFields;
6ba170cb6f53 Truffle: channel all NodeUtil field accesses through NodeField class
Andreas Woess <andreas.woess@jku.at>
parents: 18751
diff changeset
273 private final NodeField[] cloneableFields;
6ba170cb6f53 Truffle: channel all NodeUtil field accesses through NodeField class
Andreas Woess <andreas.woess@jku.at>
parents: 18751
diff changeset
274
16467
17f7331dcc4f Truffle: move iterator to NodeClass
Andreas Woess <andreas.woess@jku.at>
parents: 16466
diff changeset
275 private final Class<? extends Node> clazz;
9754
98b004bf3985 Refactoring of NodeUtil to centralize the use of reflection
Christian Wimmer <christian.wimmer@oracle.com>
parents: 9538
diff changeset
276
98b004bf3985 Refactoring of NodeUtil to centralize the use of reflection
Christian Wimmer <christian.wimmer@oracle.com>
parents: 9538
diff changeset
277 public static NodeClass get(Class<? extends Node> clazz) {
16466
d41922beb512 Truffle: use ClassValue for NodeClass
Andreas Woess <andreas.woess@jku.at>
parents: 16465
diff changeset
278 return nodeClasses.get(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
279 }
a4b84ba6dc2e Introduction of the Truffle API for efficient implementation of dynamic languages on top of the Graal VM. New projects com.oracle.truffle.api for the API definition and com.oracle.truffle.api.test for API tests and documentation.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
280
18904
6ba170cb6f53 Truffle: channel all NodeUtil field accesses through NodeField class
Andreas Woess <andreas.woess@jku.at>
parents: 18751
diff changeset
281 public NodeClass(Class<? extends Node> clazz) {
9754
98b004bf3985 Refactoring of NodeUtil to centralize the use of reflection
Christian Wimmer <christian.wimmer@oracle.com>
parents: 9538
diff changeset
282 List<NodeField> fieldsList = new ArrayList<>();
18904
6ba170cb6f53 Truffle: channel all NodeUtil field accesses through NodeField class
Andreas Woess <andreas.woess@jku.at>
parents: 18751
diff changeset
283 NodeField parentFieldTmp = null;
6ba170cb6f53 Truffle: channel all NodeUtil field accesses through NodeField class
Andreas Woess <andreas.woess@jku.at>
parents: 18751
diff changeset
284 List<NodeField> childFieldList = new ArrayList<>();
6ba170cb6f53 Truffle: channel all NodeUtil field accesses through NodeField class
Andreas Woess <andreas.woess@jku.at>
parents: 18751
diff changeset
285 List<NodeField> childrenFieldList = new ArrayList<>();
6ba170cb6f53 Truffle: channel all NodeUtil field accesses through NodeField class
Andreas Woess <andreas.woess@jku.at>
parents: 18751
diff changeset
286 List<NodeField> cloneableFieldList = new ArrayList<>();
9754
98b004bf3985 Refactoring of NodeUtil to centralize the use of reflection
Christian Wimmer <christian.wimmer@oracle.com>
parents: 9538
diff changeset
287
98b004bf3985 Refactoring of NodeUtil to centralize the use of reflection
Christian Wimmer <christian.wimmer@oracle.com>
parents: 9538
diff changeset
288 for (Field field : getAllFields(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
289 if (Modifier.isStatic(field.getModifiers()) || field.isSynthetic()) {
a4b84ba6dc2e Introduction of the Truffle API for efficient implementation of dynamic languages on top of the Graal VM. New projects com.oracle.truffle.api for the API definition and com.oracle.truffle.api.test for API tests and documentation.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
290 continue;
a4b84ba6dc2e Introduction of the Truffle API for efficient implementation of dynamic languages on top of the Graal VM. New projects com.oracle.truffle.api for the API definition and com.oracle.truffle.api.test for API tests and documentation.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
291 }
a4b84ba6dc2e Introduction of the Truffle API for efficient implementation of dynamic languages on top of the Graal VM. New projects com.oracle.truffle.api for the API definition and com.oracle.truffle.api.test for API tests and documentation.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
292
18904
6ba170cb6f53 Truffle: channel all NodeUtil field accesses through NodeField class
Andreas Woess <andreas.woess@jku.at>
parents: 18751
diff changeset
293 NodeField nodeField;
17275
846c059e3ecf Truffle: allow interface types in child fields
Andreas Woess <andreas.woess@jku.at>
parents: 16779
diff changeset
294 if (field.getDeclaringClass() == Node.class && field.getName().equals("parent")) {
846c059e3ecf Truffle: allow interface types in child fields
Andreas Woess <andreas.woess@jku.at>
parents: 16779
diff changeset
295 assert Node.class.isAssignableFrom(field.getType());
18904
6ba170cb6f53 Truffle: channel all NodeUtil field accesses through NodeField class
Andreas Woess <andreas.woess@jku.at>
parents: 18751
diff changeset
296 nodeField = NodeField.create(NodeFieldKind.PARENT, field);
6ba170cb6f53 Truffle: channel all NodeUtil field accesses through NodeField class
Andreas Woess <andreas.woess@jku.at>
parents: 18751
diff changeset
297 parentFieldTmp = nodeField;
17275
846c059e3ecf Truffle: allow interface types in child fields
Andreas Woess <andreas.woess@jku.at>
parents: 16779
diff changeset
298 } else if (field.getAnnotation(Child.class) != null) {
846c059e3ecf Truffle: allow interface types in child fields
Andreas Woess <andreas.woess@jku.at>
parents: 16779
diff changeset
299 checkChildField(field);
18904
6ba170cb6f53 Truffle: channel all NodeUtil field accesses through NodeField class
Andreas Woess <andreas.woess@jku.at>
parents: 18751
diff changeset
300 nodeField = NodeField.create(NodeFieldKind.CHILD, field);
6ba170cb6f53 Truffle: channel all NodeUtil field accesses through NodeField class
Andreas Woess <andreas.woess@jku.at>
parents: 18751
diff changeset
301 childFieldList.add(nodeField);
17275
846c059e3ecf Truffle: allow interface types in child fields
Andreas Woess <andreas.woess@jku.at>
parents: 16779
diff changeset
302 } else if (field.getAnnotation(Children.class) != null) {
846c059e3ecf Truffle: allow interface types in child fields
Andreas Woess <andreas.woess@jku.at>
parents: 16779
diff changeset
303 checkChildrenField(field);
18904
6ba170cb6f53 Truffle: channel all NodeUtil field accesses through NodeField class
Andreas Woess <andreas.woess@jku.at>
parents: 18751
diff changeset
304 nodeField = NodeField.create(NodeFieldKind.CHILDREN, field);
6ba170cb6f53 Truffle: channel all NodeUtil field accesses through NodeField class
Andreas Woess <andreas.woess@jku.at>
parents: 18751
diff changeset
305 childrenFieldList.add(nodeField);
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
306 } else {
18904
6ba170cb6f53 Truffle: channel all NodeUtil field accesses through NodeField class
Andreas Woess <andreas.woess@jku.at>
parents: 18751
diff changeset
307 nodeField = NodeField.create(NodeFieldKind.DATA, field);
6ba170cb6f53 Truffle: channel all NodeUtil field accesses through NodeField class
Andreas Woess <andreas.woess@jku.at>
parents: 18751
diff changeset
308 if (NodeCloneable.class.isAssignableFrom(field.getType())) {
6ba170cb6f53 Truffle: channel all NodeUtil field accesses through NodeField class
Andreas Woess <andreas.woess@jku.at>
parents: 18751
diff changeset
309 cloneableFieldList.add(nodeField);
6ba170cb6f53 Truffle: channel all NodeUtil field accesses through NodeField class
Andreas Woess <andreas.woess@jku.at>
parents: 18751
diff changeset
310 }
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
311 }
18904
6ba170cb6f53 Truffle: channel all NodeUtil field accesses through NodeField class
Andreas Woess <andreas.woess@jku.at>
parents: 18751
diff changeset
312 fieldsList.add(nodeField);
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
313 }
17275
846c059e3ecf Truffle: allow interface types in child fields
Andreas Woess <andreas.woess@jku.at>
parents: 16779
diff changeset
314
18904
6ba170cb6f53 Truffle: channel all NodeUtil field accesses through NodeField class
Andreas Woess <andreas.woess@jku.at>
parents: 18751
diff changeset
315 if (parentFieldTmp == null) {
17275
846c059e3ecf Truffle: allow interface types in child fields
Andreas Woess <andreas.woess@jku.at>
parents: 16779
diff changeset
316 throw new AssertionError("parent field not found");
846c059e3ecf Truffle: allow interface types in child fields
Andreas Woess <andreas.woess@jku.at>
parents: 16779
diff changeset
317 }
846c059e3ecf Truffle: allow interface types in child fields
Andreas Woess <andreas.woess@jku.at>
parents: 16779
diff changeset
318
9754
98b004bf3985 Refactoring of NodeUtil to centralize the use of reflection
Christian Wimmer <christian.wimmer@oracle.com>
parents: 9538
diff changeset
319 this.fields = fieldsList.toArray(new NodeField[fieldsList.size()]);
18904
6ba170cb6f53 Truffle: channel all NodeUtil field accesses through NodeField class
Andreas Woess <andreas.woess@jku.at>
parents: 18751
diff changeset
320 this.parentField = parentFieldTmp;
6ba170cb6f53 Truffle: channel all NodeUtil field accesses through NodeField class
Andreas Woess <andreas.woess@jku.at>
parents: 18751
diff changeset
321 this.childFields = childFieldList.toArray(new NodeField[childFieldList.size()]);
6ba170cb6f53 Truffle: channel all NodeUtil field accesses through NodeField class
Andreas Woess <andreas.woess@jku.at>
parents: 18751
diff changeset
322 this.childrenFields = childrenFieldList.toArray(new NodeField[childrenFieldList.size()]);
6ba170cb6f53 Truffle: channel all NodeUtil field accesses through NodeField class
Andreas Woess <andreas.woess@jku.at>
parents: 18751
diff changeset
323 this.cloneableFields = cloneableFieldList.toArray(new NodeField[cloneableFieldList.size()]);
16467
17f7331dcc4f Truffle: move iterator to NodeClass
Andreas Woess <andreas.woess@jku.at>
parents: 16466
diff changeset
324 this.clazz = clazz;
9754
98b004bf3985 Refactoring of NodeUtil to centralize the use of reflection
Christian Wimmer <christian.wimmer@oracle.com>
parents: 9538
diff changeset
325 }
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
326
18332
65a160d9d259 Truffle: add NodeInterface and require that all child fields be of this type
Andreas Woess <andreas.woess@jku.at>
parents: 18163
diff changeset
327 private static boolean isNodeType(Class<?> clazz) {
65a160d9d259 Truffle: add NodeInterface and require that all child fields be of this type
Andreas Woess <andreas.woess@jku.at>
parents: 18163
diff changeset
328 return Node.class.isAssignableFrom(clazz) || (clazz.isInterface() && NodeInterface.class.isAssignableFrom(clazz));
65a160d9d259 Truffle: add NodeInterface and require that all child fields be of this type
Andreas Woess <andreas.woess@jku.at>
parents: 18163
diff changeset
329 }
65a160d9d259 Truffle: add NodeInterface and require that all child fields be of this type
Andreas Woess <andreas.woess@jku.at>
parents: 18163
diff changeset
330
17275
846c059e3ecf Truffle: allow interface types in child fields
Andreas Woess <andreas.woess@jku.at>
parents: 16779
diff changeset
331 private static void checkChildField(Field field) {
18332
65a160d9d259 Truffle: add NodeInterface and require that all child fields be of this type
Andreas Woess <andreas.woess@jku.at>
parents: 18163
diff changeset
332 if (!isNodeType(field.getType())) {
65a160d9d259 Truffle: add NodeInterface and require that all child fields be of this type
Andreas Woess <andreas.woess@jku.at>
parents: 18163
diff changeset
333 throw new AssertionError("@Child field type must be a subclass of Node or an interface extending NodeInterface (" + field + ")");
17275
846c059e3ecf Truffle: allow interface types in child fields
Andreas Woess <andreas.woess@jku.at>
parents: 16779
diff changeset
334 }
846c059e3ecf Truffle: allow interface types in child fields
Andreas Woess <andreas.woess@jku.at>
parents: 16779
diff changeset
335 if (Modifier.isFinal(field.getModifiers())) {
846c059e3ecf Truffle: allow interface types in child fields
Andreas Woess <andreas.woess@jku.at>
parents: 16779
diff changeset
336 throw new AssertionError("@Child field must not be final (" + field + ")");
846c059e3ecf Truffle: allow interface types in child fields
Andreas Woess <andreas.woess@jku.at>
parents: 16779
diff changeset
337 }
846c059e3ecf Truffle: allow interface types in child fields
Andreas Woess <andreas.woess@jku.at>
parents: 16779
diff changeset
338 }
846c059e3ecf Truffle: allow interface types in child fields
Andreas Woess <andreas.woess@jku.at>
parents: 16779
diff changeset
339
846c059e3ecf Truffle: allow interface types in child fields
Andreas Woess <andreas.woess@jku.at>
parents: 16779
diff changeset
340 private static void checkChildrenField(Field field) {
18332
65a160d9d259 Truffle: add NodeInterface and require that all child fields be of this type
Andreas Woess <andreas.woess@jku.at>
parents: 18163
diff changeset
341 if (!(field.getType().isArray() && isNodeType(field.getType().getComponentType()))) {
65a160d9d259 Truffle: add NodeInterface and require that all child fields be of this type
Andreas Woess <andreas.woess@jku.at>
parents: 18163
diff changeset
342 throw new AssertionError("@Children field type must be an array of a subclass of Node or an interface extending NodeInterface (" + field + ")");
17275
846c059e3ecf Truffle: allow interface types in child fields
Andreas Woess <andreas.woess@jku.at>
parents: 16779
diff changeset
343 }
846c059e3ecf Truffle: allow interface types in child fields
Andreas Woess <andreas.woess@jku.at>
parents: 16779
diff changeset
344 if (!Modifier.isFinal(field.getModifiers())) {
846c059e3ecf Truffle: allow interface types in child fields
Andreas Woess <andreas.woess@jku.at>
parents: 16779
diff changeset
345 throw new AssertionError("@Children field must be final (" + field + ")");
846c059e3ecf Truffle: allow interface types in child fields
Andreas Woess <andreas.woess@jku.at>
parents: 16779
diff changeset
346 }
846c059e3ecf Truffle: allow interface types in child fields
Andreas Woess <andreas.woess@jku.at>
parents: 16779
diff changeset
347 }
846c059e3ecf Truffle: allow interface types in child fields
Andreas Woess <andreas.woess@jku.at>
parents: 16779
diff changeset
348
9754
98b004bf3985 Refactoring of NodeUtil to centralize the use of reflection
Christian Wimmer <christian.wimmer@oracle.com>
parents: 9538
diff changeset
349 public NodeField[] getFields() {
98b004bf3985 Refactoring of NodeUtil to centralize the use of reflection
Christian Wimmer <christian.wimmer@oracle.com>
parents: 9538
diff changeset
350 return fields;
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
351 }
9258
07f8d136a05e Truffle API changes for the Frame API. Introduction of Assumptions class.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents: 7530
diff changeset
352
18904
6ba170cb6f53 Truffle: channel all NodeUtil field accesses through NodeField class
Andreas Woess <andreas.woess@jku.at>
parents: 18751
diff changeset
353 public NodeField getParentField() {
6ba170cb6f53 Truffle: channel all NodeUtil field accesses through NodeField class
Andreas Woess <andreas.woess@jku.at>
parents: 18751
diff changeset
354 return parentField;
9258
07f8d136a05e Truffle API changes for the Frame API. Introduction of Assumptions class.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents: 7530
diff changeset
355 }
07f8d136a05e Truffle API changes for the Frame API. Introduction of Assumptions class.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents: 7530
diff changeset
356
18904
6ba170cb6f53 Truffle: channel all NodeUtil field accesses through NodeField class
Andreas Woess <andreas.woess@jku.at>
parents: 18751
diff changeset
357 public NodeField[] getChildFields() {
6ba170cb6f53 Truffle: channel all NodeUtil field accesses through NodeField class
Andreas Woess <andreas.woess@jku.at>
parents: 18751
diff changeset
358 return childFields;
9258
07f8d136a05e Truffle API changes for the Frame API. Introduction of Assumptions class.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents: 7530
diff changeset
359 }
07f8d136a05e Truffle API changes for the Frame API. Introduction of Assumptions class.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents: 7530
diff changeset
360
18904
6ba170cb6f53 Truffle: channel all NodeUtil field accesses through NodeField class
Andreas Woess <andreas.woess@jku.at>
parents: 18751
diff changeset
361 public NodeField[] getChildrenFields() {
6ba170cb6f53 Truffle: channel all NodeUtil field accesses through NodeField class
Andreas Woess <andreas.woess@jku.at>
parents: 18751
diff changeset
362 return childrenFields;
9258
07f8d136a05e Truffle API changes for the Frame API. Introduction of Assumptions class.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents: 7530
diff changeset
363 }
14743
762c9aceb7d8 Add equals/hashCode methods
Christian Wimmer <christian.wimmer@oracle.com>
parents: 14569
diff changeset
364
762c9aceb7d8 Add equals/hashCode methods
Christian Wimmer <christian.wimmer@oracle.com>
parents: 14569
diff changeset
365 @Override
762c9aceb7d8 Add equals/hashCode methods
Christian Wimmer <christian.wimmer@oracle.com>
parents: 14569
diff changeset
366 public int hashCode() {
18904
6ba170cb6f53 Truffle: channel all NodeUtil field accesses through NodeField class
Andreas Woess <andreas.woess@jku.at>
parents: 18751
diff changeset
367 return clazz.hashCode();
14743
762c9aceb7d8 Add equals/hashCode methods
Christian Wimmer <christian.wimmer@oracle.com>
parents: 14569
diff changeset
368 }
762c9aceb7d8 Add equals/hashCode methods
Christian Wimmer <christian.wimmer@oracle.com>
parents: 14569
diff changeset
369
762c9aceb7d8 Add equals/hashCode methods
Christian Wimmer <christian.wimmer@oracle.com>
parents: 14569
diff changeset
370 @Override
762c9aceb7d8 Add equals/hashCode methods
Christian Wimmer <christian.wimmer@oracle.com>
parents: 14569
diff changeset
371 public boolean equals(Object obj) {
762c9aceb7d8 Add equals/hashCode methods
Christian Wimmer <christian.wimmer@oracle.com>
parents: 14569
diff changeset
372 if (obj instanceof NodeClass) {
762c9aceb7d8 Add equals/hashCode methods
Christian Wimmer <christian.wimmer@oracle.com>
parents: 14569
diff changeset
373 NodeClass other = (NodeClass) obj;
18904
6ba170cb6f53 Truffle: channel all NodeUtil field accesses through NodeField class
Andreas Woess <andreas.woess@jku.at>
parents: 18751
diff changeset
374 return clazz.equals(other.clazz);
14743
762c9aceb7d8 Add equals/hashCode methods
Christian Wimmer <christian.wimmer@oracle.com>
parents: 14569
diff changeset
375 }
762c9aceb7d8 Add equals/hashCode methods
Christian Wimmer <christian.wimmer@oracle.com>
parents: 14569
diff changeset
376 return false;
762c9aceb7d8 Add equals/hashCode methods
Christian Wimmer <christian.wimmer@oracle.com>
parents: 14569
diff changeset
377 }
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
16467
17f7331dcc4f Truffle: move iterator to NodeClass
Andreas Woess <andreas.woess@jku.at>
parents: 16466
diff changeset
379 public Iterator<Node> makeIterator(Node node) {
17f7331dcc4f Truffle: move iterator to NodeClass
Andreas Woess <andreas.woess@jku.at>
parents: 16466
diff changeset
380 assert clazz.isInstance(node);
17f7331dcc4f Truffle: move iterator to NodeClass
Andreas Woess <andreas.woess@jku.at>
parents: 16466
diff changeset
381 return new NodeIterator(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
382 }
a4b84ba6dc2e Introduction of the Truffle API for efficient implementation of dynamic languages on top of the Graal VM. New projects com.oracle.truffle.api 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
16467
17f7331dcc4f Truffle: move iterator to NodeClass
Andreas Woess <andreas.woess@jku.at>
parents: 16466
diff changeset
384 private final class NodeIterator implements Iterator<Node> {
17f7331dcc4f Truffle: move iterator to NodeClass
Andreas Woess <andreas.woess@jku.at>
parents: 16466
diff changeset
385 private final Node node;
16581
8be5c68a779d Truffle: revert to previous iterator implementation, add test case
Andreas Woess <andreas.woess@jku.at>
parents: 16548
diff changeset
386 private final int childrenCount;
8be5c68a779d Truffle: revert to previous iterator implementation, add test case
Andreas Woess <andreas.woess@jku.at>
parents: 16548
diff changeset
387 private int index;
16467
17f7331dcc4f Truffle: move iterator to NodeClass
Andreas Woess <andreas.woess@jku.at>
parents: 16466
diff changeset
388
17f7331dcc4f Truffle: move iterator to NodeClass
Andreas Woess <andreas.woess@jku.at>
parents: 16466
diff changeset
389 protected NodeIterator(Node node) {
17f7331dcc4f Truffle: move iterator to NodeClass
Andreas Woess <andreas.woess@jku.at>
parents: 16466
diff changeset
390 this.node = node;
16581
8be5c68a779d Truffle: revert to previous iterator implementation, add test case
Andreas Woess <andreas.woess@jku.at>
parents: 16548
diff changeset
391 this.index = 0;
8be5c68a779d Truffle: revert to previous iterator implementation, add test case
Andreas Woess <andreas.woess@jku.at>
parents: 16548
diff changeset
392 this.childrenCount = childrenCount();
8be5c68a779d Truffle: revert to previous iterator implementation, add test case
Andreas Woess <andreas.woess@jku.at>
parents: 16548
diff changeset
393 }
8be5c68a779d Truffle: revert to previous iterator implementation, add test case
Andreas Woess <andreas.woess@jku.at>
parents: 16548
diff changeset
394
8be5c68a779d Truffle: revert to previous iterator implementation, add test case
Andreas Woess <andreas.woess@jku.at>
parents: 16548
diff changeset
395 private int childrenCount() {
18904
6ba170cb6f53 Truffle: channel all NodeUtil field accesses through NodeField class
Andreas Woess <andreas.woess@jku.at>
parents: 18751
diff changeset
396 int nodeCount = childFields.length;
6ba170cb6f53 Truffle: channel all NodeUtil field accesses through NodeField class
Andreas Woess <andreas.woess@jku.at>
parents: 18751
diff changeset
397 for (NodeField childrenField : childrenFields) {
6ba170cb6f53 Truffle: channel all NodeUtil field accesses through NodeField class
Andreas Woess <andreas.woess@jku.at>
parents: 18751
diff changeset
398 Object[] children = ((Object[]) childrenField.getObject(node));
16581
8be5c68a779d Truffle: revert to previous iterator implementation, add test case
Andreas Woess <andreas.woess@jku.at>
parents: 16548
diff changeset
399 if (children != null) {
8be5c68a779d Truffle: revert to previous iterator implementation, add test case
Andreas Woess <andreas.woess@jku.at>
parents: 16548
diff changeset
400 nodeCount += children.length;
8be5c68a779d Truffle: revert to previous iterator implementation, add test case
Andreas Woess <andreas.woess@jku.at>
parents: 16548
diff changeset
401 }
8be5c68a779d Truffle: revert to previous iterator implementation, add test case
Andreas Woess <andreas.woess@jku.at>
parents: 16548
diff changeset
402 }
8be5c68a779d Truffle: revert to previous iterator implementation, add test case
Andreas Woess <andreas.woess@jku.at>
parents: 16548
diff changeset
403 return nodeCount;
8be5c68a779d Truffle: revert to previous iterator implementation, add test case
Andreas Woess <andreas.woess@jku.at>
parents: 16548
diff changeset
404 }
8be5c68a779d Truffle: revert to previous iterator implementation, add test case
Andreas Woess <andreas.woess@jku.at>
parents: 16548
diff changeset
405
8be5c68a779d Truffle: revert to previous iterator implementation, add test case
Andreas Woess <andreas.woess@jku.at>
parents: 16548
diff changeset
406 private Node nodeAt(int idx) {
18904
6ba170cb6f53 Truffle: channel all NodeUtil field accesses through NodeField class
Andreas Woess <andreas.woess@jku.at>
parents: 18751
diff changeset
407 int nodeCount = childFields.length;
16581
8be5c68a779d Truffle: revert to previous iterator implementation, add test case
Andreas Woess <andreas.woess@jku.at>
parents: 16548
diff changeset
408 if (idx < nodeCount) {
18904
6ba170cb6f53 Truffle: channel all NodeUtil field accesses through NodeField class
Andreas Woess <andreas.woess@jku.at>
parents: 18751
diff changeset
409 return (Node) childFields[idx].getObject(node);
16581
8be5c68a779d Truffle: revert to previous iterator implementation, add test case
Andreas Woess <andreas.woess@jku.at>
parents: 16548
diff changeset
410 } else {
18904
6ba170cb6f53 Truffle: channel all NodeUtil field accesses through NodeField class
Andreas Woess <andreas.woess@jku.at>
parents: 18751
diff changeset
411 for (NodeField childrenField : childrenFields) {
6ba170cb6f53 Truffle: channel all NodeUtil field accesses through NodeField class
Andreas Woess <andreas.woess@jku.at>
parents: 18751
diff changeset
412 Object[] nodeArray = (Object[]) childrenField.getObject(node);
16581
8be5c68a779d Truffle: revert to previous iterator implementation, add test case
Andreas Woess <andreas.woess@jku.at>
parents: 16548
diff changeset
413 if (idx < nodeCount + nodeArray.length) {
17275
846c059e3ecf Truffle: allow interface types in child fields
Andreas Woess <andreas.woess@jku.at>
parents: 16779
diff changeset
414 return (Node) nodeArray[idx - nodeCount];
16581
8be5c68a779d Truffle: revert to previous iterator implementation, add test case
Andreas Woess <andreas.woess@jku.at>
parents: 16548
diff changeset
415 }
8be5c68a779d Truffle: revert to previous iterator implementation, add test case
Andreas Woess <andreas.woess@jku.at>
parents: 16548
diff changeset
416 nodeCount += nodeArray.length;
8be5c68a779d Truffle: revert to previous iterator implementation, add test case
Andreas Woess <andreas.woess@jku.at>
parents: 16548
diff changeset
417 }
8be5c68a779d Truffle: revert to previous iterator implementation, add test case
Andreas Woess <andreas.woess@jku.at>
parents: 16548
diff changeset
418 }
8be5c68a779d Truffle: revert to previous iterator implementation, add test case
Andreas Woess <andreas.woess@jku.at>
parents: 16548
diff changeset
419 return null;
16467
17f7331dcc4f Truffle: move iterator to NodeClass
Andreas Woess <andreas.woess@jku.at>
parents: 16466
diff changeset
420 }
17f7331dcc4f Truffle: move iterator to NodeClass
Andreas Woess <andreas.woess@jku.at>
parents: 16466
diff changeset
421
17f7331dcc4f Truffle: move iterator to NodeClass
Andreas Woess <andreas.woess@jku.at>
parents: 16466
diff changeset
422 private void forward() {
16581
8be5c68a779d Truffle: revert to previous iterator implementation, add test case
Andreas Woess <andreas.woess@jku.at>
parents: 16548
diff changeset
423 if (index < childrenCount) {
8be5c68a779d Truffle: revert to previous iterator implementation, add test case
Andreas Woess <andreas.woess@jku.at>
parents: 16548
diff changeset
424 index++;
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
425 }
a4b84ba6dc2e Introduction of the Truffle API for efficient implementation of dynamic languages on top of the Graal VM. New projects com.oracle.truffle.api for the API definition and com.oracle.truffle.api.test for API tests and documentation.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
426 }
16467
17f7331dcc4f Truffle: move iterator to NodeClass
Andreas Woess <andreas.woess@jku.at>
parents: 16466
diff changeset
427
17f7331dcc4f Truffle: move iterator to NodeClass
Andreas Woess <andreas.woess@jku.at>
parents: 16466
diff changeset
428 public boolean hasNext() {
16581
8be5c68a779d Truffle: revert to previous iterator implementation, add test case
Andreas Woess <andreas.woess@jku.at>
parents: 16548
diff changeset
429 return index < childrenCount;
16467
17f7331dcc4f Truffle: move iterator to NodeClass
Andreas Woess <andreas.woess@jku.at>
parents: 16466
diff changeset
430 }
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
431
16467
17f7331dcc4f Truffle: move iterator to NodeClass
Andreas Woess <andreas.woess@jku.at>
parents: 16466
diff changeset
432 public Node next() {
16581
8be5c68a779d Truffle: revert to previous iterator implementation, add test case
Andreas Woess <andreas.woess@jku.at>
parents: 16548
diff changeset
433 try {
8be5c68a779d Truffle: revert to previous iterator implementation, add test case
Andreas Woess <andreas.woess@jku.at>
parents: 16548
diff changeset
434 return nodeAt(index);
8be5c68a779d Truffle: revert to previous iterator implementation, add test case
Andreas Woess <andreas.woess@jku.at>
parents: 16548
diff changeset
435 } finally {
8be5c68a779d Truffle: revert to previous iterator implementation, add test case
Andreas Woess <andreas.woess@jku.at>
parents: 16548
diff changeset
436 forward();
16467
17f7331dcc4f Truffle: move iterator to NodeClass
Andreas Woess <andreas.woess@jku.at>
parents: 16466
diff changeset
437 }
17f7331dcc4f Truffle: move iterator to NodeClass
Andreas Woess <andreas.woess@jku.at>
parents: 16466
diff changeset
438 }
17f7331dcc4f Truffle: move iterator to NodeClass
Andreas Woess <andreas.woess@jku.at>
parents: 16466
diff changeset
439
17f7331dcc4f Truffle: move iterator to NodeClass
Andreas Woess <andreas.woess@jku.at>
parents: 16466
diff changeset
440 public void remove() {
17f7331dcc4f Truffle: move iterator to NodeClass
Andreas Woess <andreas.woess@jku.at>
parents: 16466
diff changeset
441 throw new UnsupportedOperationException();
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
442 }
a4b84ba6dc2e Introduction of the Truffle API for efficient implementation of dynamic languages on top of the Graal VM. New projects com.oracle.truffle.api 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 }
16467
17f7331dcc4f Truffle: move iterator to NodeClass
Andreas Woess <andreas.woess@jku.at>
parents: 16466
diff changeset
444 }
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
16467
17f7331dcc4f Truffle: move iterator to NodeClass
Andreas Woess <andreas.woess@jku.at>
parents: 16466
diff changeset
446 static Iterator<Node> makeIterator(Node node) {
17f7331dcc4f Truffle: move iterator to NodeClass
Andreas Woess <andreas.woess@jku.at>
parents: 16466
diff changeset
447 return NodeClass.get(node.getClass()).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
448 }
a4b84ba6dc2e Introduction of the Truffle API for efficient implementation of dynamic languages on top of the Graal VM. New projects com.oracle.truffle.api for the API definition and com.oracle.truffle.api.test for API tests and documentation.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
449
17399
5787218bad91 Truffle: implemented recursive node iterator and node streams for the graal runtime.
Christian Humer <christian.humer@gmail.com>
parents: 17275
diff changeset
450 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
451 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
452 }
5787218bad91 Truffle: implemented recursive node iterator and node streams for the graal runtime.
Christian Humer <christian.humer@gmail.com>
parents: 17275
diff changeset
453
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
454 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
455 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
456
5787218bad91 Truffle: implemented recursive node iterator and node streams for the graal runtime.
Christian Humer <christian.humer@gmail.com>
parents: 17275
diff changeset
457 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
458 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
459
5787218bad91 Truffle: implemented recursive node iterator and node streams for the graal runtime.
Christian Humer <christian.humer@gmail.com>
parents: 17275
diff changeset
460 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
461
5787218bad91 Truffle: implemented recursive node iterator and node streams for the graal runtime.
Christian Humer <christian.humer@gmail.com>
parents: 17275
diff changeset
462 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
463 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
464 }
5787218bad91 Truffle: implemented recursive node iterator and node streams for the graal runtime.
Christian Humer <christian.humer@gmail.com>
parents: 17275
diff changeset
465
5787218bad91 Truffle: implemented recursive node iterator and node streams for the graal runtime.
Christian Humer <christian.humer@gmail.com>
parents: 17275
diff changeset
466 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
467 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
468 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
469 }
5787218bad91 Truffle: implemented recursive node iterator and node streams for the graal runtime.
Christian Humer <christian.humer@gmail.com>
parents: 17275
diff changeset
470 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
471 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
472 }
5787218bad91 Truffle: implemented recursive node iterator and node streams for the graal runtime.
Christian Humer <christian.humer@gmail.com>
parents: 17275
diff changeset
473
5787218bad91 Truffle: implemented recursive node iterator and node streams for the graal runtime.
Christian Humer <christian.humer@gmail.com>
parents: 17275
diff changeset
474 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
475 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
476 }
5787218bad91 Truffle: implemented recursive node iterator and node streams for the graal runtime.
Christian Humer <christian.humer@gmail.com>
parents: 17275
diff changeset
477 });
5787218bad91 Truffle: implemented recursive node iterator and node streams for the graal runtime.
Christian Humer <christian.humer@gmail.com>
parents: 17275
diff changeset
478 }
5787218bad91 Truffle: implemented recursive node iterator and node streams for the graal runtime.
Christian Humer <christian.humer@gmail.com>
parents: 17275
diff changeset
479
5787218bad91 Truffle: implemented recursive node iterator and node streams for the graal runtime.
Christian Humer <christian.humer@gmail.com>
parents: 17275
diff changeset
480 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
481 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
482 }
5787218bad91 Truffle: implemented recursive node iterator and node streams for the graal runtime.
Christian Humer <christian.humer@gmail.com>
parents: 17275
diff changeset
483
5787218bad91 Truffle: implemented recursive node iterator and node streams for the graal runtime.
Christian Humer <christian.humer@gmail.com>
parents: 17275
diff changeset
484 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
485 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
486 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
487 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
488 }
5787218bad91 Truffle: implemented recursive node iterator and node streams for the graal runtime.
Christian Humer <christian.humer@gmail.com>
parents: 17275
diff changeset
489
5787218bad91 Truffle: implemented recursive node iterator and node streams for the graal runtime.
Christian Humer <christian.humer@gmail.com>
parents: 17275
diff changeset
490 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
491 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
492 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
493 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
494 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
495 }
5787218bad91 Truffle: implemented recursive node iterator and node streams for the graal runtime.
Christian Humer <christian.humer@gmail.com>
parents: 17275
diff changeset
496 }
5787218bad91 Truffle: implemented recursive node iterator and node streams for the graal runtime.
Christian Humer <christian.humer@gmail.com>
parents: 17275
diff changeset
497 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
498 }
5787218bad91 Truffle: implemented recursive node iterator and node streams for the graal runtime.
Christian Humer <christian.humer@gmail.com>
parents: 17275
diff changeset
499
5787218bad91 Truffle: implemented recursive node iterator and node streams for the graal runtime.
Christian Humer <christian.humer@gmail.com>
parents: 17275
diff changeset
500 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
501 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
502 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
503 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
504 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
505 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
506 } else {
5787218bad91 Truffle: implemented recursive node iterator and node streams for the graal runtime.
Christian Humer <christian.humer@gmail.com>
parents: 17275
diff changeset
507 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
508 }
5787218bad91 Truffle: implemented recursive node iterator and node streams for the graal runtime.
Christian Humer <christian.humer@gmail.com>
parents: 17275
diff changeset
509 }
5787218bad91 Truffle: implemented recursive node iterator and node streams for the graal runtime.
Christian Humer <christian.humer@gmail.com>
parents: 17275
diff changeset
510 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
511 }
5787218bad91 Truffle: implemented recursive node iterator and node streams for the graal runtime.
Christian Humer <christian.humer@gmail.com>
parents: 17275
diff changeset
512
5787218bad91 Truffle: implemented recursive node iterator and node streams for the graal runtime.
Christian Humer <christian.humer@gmail.com>
parents: 17275
diff changeset
513 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
514 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
515 }
5787218bad91 Truffle: implemented recursive node iterator and node streams for the graal runtime.
Christian Humer <christian.humer@gmail.com>
parents: 17275
diff changeset
516 }
5787218bad91 Truffle: implemented recursive node iterator and node streams for the graal runtime.
Christian Humer <christian.humer@gmail.com>
parents: 17275
diff changeset
517
11636
136eaa90ef41 Truffle: make NodeUtil class final and its unsafe field private.
Andreas Woess <andreas.woess@jku.at>
parents: 11622
diff changeset
518 private static final Unsafe unsafe = getUnsafe();
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
519
a4b84ba6dc2e Introduction of the Truffle API for efficient implementation of dynamic languages on top of the Graal VM. New projects com.oracle.truffle.api for the API definition and com.oracle.truffle.api.test for API tests and documentation.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
520 private static Unsafe getUnsafe() {
a4b84ba6dc2e Introduction of the Truffle API for efficient implementation of dynamic languages on top of the Graal VM. New projects com.oracle.truffle.api for the API definition and com.oracle.truffle.api.test for API tests and documentation.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
521 try {
a4b84ba6dc2e Introduction of the Truffle API for efficient implementation of dynamic languages on top of the Graal VM. New projects com.oracle.truffle.api for the API definition and com.oracle.truffle.api.test for API tests and documentation.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
522 return Unsafe.getUnsafe();
a4b84ba6dc2e Introduction of the Truffle API for efficient implementation of dynamic languages on top of the Graal VM. New projects com.oracle.truffle.api for the API definition and com.oracle.truffle.api.test for API tests and documentation.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
523 } catch (SecurityException e) {
a4b84ba6dc2e Introduction of the Truffle API for efficient implementation of dynamic languages on top of the Graal VM. New projects com.oracle.truffle.api for the API definition and com.oracle.truffle.api.test for API tests and documentation.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
524 }
a4b84ba6dc2e Introduction of the Truffle API for efficient implementation of dynamic languages on top of the Graal VM. New projects com.oracle.truffle.api for the API definition and com.oracle.truffle.api.test for API tests and documentation.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
525 try {
a4b84ba6dc2e Introduction of the Truffle API for efficient implementation of dynamic languages on top of the Graal VM. New projects com.oracle.truffle.api for the API definition and com.oracle.truffle.api.test for API tests and documentation.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
526 Field theUnsafeInstance = Unsafe.class.getDeclaredField("theUnsafe");
a4b84ba6dc2e Introduction of the Truffle API for efficient implementation of dynamic languages on top of the Graal VM. New projects com.oracle.truffle.api for the API definition and com.oracle.truffle.api.test for API tests and documentation.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
527 theUnsafeInstance.setAccessible(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
528 return (Unsafe) theUnsafeInstance.get(Unsafe.class);
a4b84ba6dc2e Introduction of the Truffle API for efficient implementation of dynamic languages on top of the Graal VM. New projects com.oracle.truffle.api for the API definition and com.oracle.truffle.api.test for API tests and documentation.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
529 } catch (Exception e) {
a4b84ba6dc2e Introduction of the Truffle API for efficient implementation of dynamic languages on top of the Graal VM. New projects com.oracle.truffle.api for the API definition and com.oracle.truffle.api.test for API tests and documentation.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
530 throw new RuntimeException("exception while trying to get Unsafe.theUnsafe via reflection:", e);
a4b84ba6dc2e Introduction of the Truffle API for efficient implementation of dynamic languages on top of the Graal VM. New projects com.oracle.truffle.api for the API definition and com.oracle.truffle.api.test for API tests and documentation.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
531 }
a4b84ba6dc2e Introduction of the Truffle API for efficient implementation of dynamic languages on top of the Graal VM. New projects com.oracle.truffle.api for the API definition and com.oracle.truffle.api.test for API tests and documentation.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
532 }
a4b84ba6dc2e Introduction of the Truffle API for efficient implementation of dynamic languages on top of the Graal VM. New projects com.oracle.truffle.api for the API definition and com.oracle.truffle.api.test for API tests and documentation.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
533
a4b84ba6dc2e Introduction of the Truffle API for efficient implementation of dynamic languages on top of the Graal VM. New projects com.oracle.truffle.api for the API definition and com.oracle.truffle.api.test for API tests and documentation.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
534 @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
535 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
536 return (T) orig.deepCopy();
8a758dce7d80 Truffle: added new deepCopy method to Node.
Christian Humer <christian.humer@gmail.com>
parents: 18510
diff changeset
537 }
8a758dce7d80 Truffle: added new deepCopy method to Node.
Christian Humer <christian.humer@gmail.com>
parents: 18510
diff changeset
538
8a758dce7d80 Truffle: added new deepCopy method to Node.
Christian Humer <christian.humer@gmail.com>
parents: 18510
diff changeset
539 static Node deepCopyImpl(Node orig) {
11809
c7769440afd8 improve NodeUtil.cloneNode.
Andreas Woess <andreas.woess@jku.at>
parents: 11649
diff changeset
540 final Node clone = orig.copy();
c7769440afd8 improve NodeUtil.cloneNode.
Andreas Woess <andreas.woess@jku.at>
parents: 11649
diff changeset
541 NodeClass nodeClass = NodeClass.get(clone.getClass());
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
542
18904
6ba170cb6f53 Truffle: channel all NodeUtil field accesses through NodeField class
Andreas Woess <andreas.woess@jku.at>
parents: 18751
diff changeset
543 nodeClass.parentField.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
544
18904
6ba170cb6f53 Truffle: channel all NodeUtil field accesses through NodeField class
Andreas Woess <andreas.woess@jku.at>
parents: 18751
diff changeset
545 for (NodeField childField : nodeClass.childFields) {
6ba170cb6f53 Truffle: channel all NodeUtil field accesses through NodeField class
Andreas Woess <andreas.woess@jku.at>
parents: 18751
diff changeset
546 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
547 if (child != null) {
18750
8a758dce7d80 Truffle: added new deepCopy method to Node.
Christian Humer <christian.humer@gmail.com>
parents: 18510
diff changeset
548 Node clonedChild = child.deepCopy();
18904
6ba170cb6f53 Truffle: channel all NodeUtil field accesses through NodeField class
Andreas Woess <andreas.woess@jku.at>
parents: 18751
diff changeset
549 nodeClass.parentField.putObject(clonedChild, clone);
6ba170cb6f53 Truffle: channel all NodeUtil field accesses through NodeField class
Andreas Woess <andreas.woess@jku.at>
parents: 18751
diff changeset
550 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
551 }
a4b84ba6dc2e Introduction of the Truffle API for efficient implementation of dynamic languages on top of the Graal VM. New projects com.oracle.truffle.api for the API definition and com.oracle.truffle.api.test for API tests and documentation.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
552 }
18904
6ba170cb6f53 Truffle: channel all NodeUtil field accesses through NodeField class
Andreas Woess <andreas.woess@jku.at>
parents: 18751
diff changeset
553 for (NodeField childrenField : nodeClass.childrenFields) {
6ba170cb6f53 Truffle: channel all NodeUtil field accesses through NodeField class
Andreas Woess <andreas.woess@jku.at>
parents: 18751
diff changeset
554 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
555 if (children != null) {
17275
846c059e3ecf Truffle: allow interface types in child fields
Andreas Woess <andreas.woess@jku.at>
parents: 16779
diff changeset
556 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
557 for (int i = 0; i < children.length; i++) {
11809
c7769440afd8 improve NodeUtil.cloneNode.
Andreas Woess <andreas.woess@jku.at>
parents: 11649
diff changeset
558 if (children[i] != null) {
18750
8a758dce7d80 Truffle: added new deepCopy method to Node.
Christian Humer <christian.humer@gmail.com>
parents: 18510
diff changeset
559 Node clonedChild = ((Node) children[i]).deepCopy();
11809
c7769440afd8 improve NodeUtil.cloneNode.
Andreas Woess <andreas.woess@jku.at>
parents: 11649
diff changeset
560 clonedChildren[i] = clonedChild;
18904
6ba170cb6f53 Truffle: channel all NodeUtil field accesses through NodeField class
Andreas Woess <andreas.woess@jku.at>
parents: 18751
diff changeset
561 nodeClass.parentField.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
562 }
a4b84ba6dc2e Introduction of the Truffle API for efficient implementation of dynamic languages on top of the Graal VM. New projects com.oracle.truffle.api for the API definition and com.oracle.truffle.api.test for API tests and documentation.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
563 }
18904
6ba170cb6f53 Truffle: channel all NodeUtil field accesses through NodeField class
Andreas Woess <andreas.woess@jku.at>
parents: 18751
diff changeset
564 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
565 }
a4b84ba6dc2e Introduction of the Truffle API for efficient implementation of dynamic languages on top of the Graal VM. New projects com.oracle.truffle.api for the API definition and com.oracle.truffle.api.test for API tests and documentation.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
566 }
18904
6ba170cb6f53 Truffle: channel all NodeUtil field accesses through NodeField class
Andreas Woess <andreas.woess@jku.at>
parents: 18751
diff changeset
567 for (NodeField cloneableField : nodeClass.cloneableFields) {
6ba170cb6f53 Truffle: channel all NodeUtil field accesses through NodeField class
Andreas Woess <andreas.woess@jku.at>
parents: 18751
diff changeset
568 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
569 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
570 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
571 }
cb4d5cc2b52b Truffle: clone ConditionProfile and BranchProfile node fields
Andreas Woess <andreas.woess@jku.at>
parents: 18485
diff changeset
572 }
18750
8a758dce7d80 Truffle: added new deepCopy method to Node.
Christian Humer <christian.humer@gmail.com>
parents: 18510
diff changeset
573 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
574 }
a4b84ba6dc2e Introduction of the Truffle API for efficient implementation of dynamic languages on top of the Graal VM. New projects com.oracle.truffle.api for the API definition and com.oracle.truffle.api.test for API tests and documentation.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
575
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
576 public static List<Node> findNodeChildren(Node node) {
e2965e5cd474 Fixed Truffle child iterator should only iterate children which are annotated with @Child or @Children.
Christian Humer <christian.humer@gmail.com>
parents: 9487
diff changeset
577 List<Node> nodes = new ArrayList<>();
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
578 NodeClass nodeClass = NodeClass.get(node.getClass());
a4b84ba6dc2e Introduction of the Truffle API for efficient implementation of dynamic languages on top of the Graal VM. New projects com.oracle.truffle.api for the API definition and com.oracle.truffle.api.test for API tests and documentation.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
579
18904
6ba170cb6f53 Truffle: channel all NodeUtil field accesses through NodeField class
Andreas Woess <andreas.woess@jku.at>
parents: 18751
diff changeset
580 for (NodeField nodeField : nodeClass.childFields) {
6ba170cb6f53 Truffle: channel all NodeUtil field accesses through NodeField class
Andreas Woess <andreas.woess@jku.at>
parents: 18751
diff changeset
581 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
582 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
583 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
584 }
a4b84ba6dc2e Introduction of the Truffle API for efficient implementation of dynamic languages on top of the Graal VM. New projects com.oracle.truffle.api for the API definition and com.oracle.truffle.api.test for API tests and documentation.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
585 }
18904
6ba170cb6f53 Truffle: channel all NodeUtil field accesses through NodeField class
Andreas Woess <andreas.woess@jku.at>
parents: 18751
diff changeset
586 for (NodeField nodeField : nodeClass.childrenFields) {
6ba170cb6f53 Truffle: channel all NodeUtil field accesses through NodeField class
Andreas Woess <andreas.woess@jku.at>
parents: 18751
diff changeset
587 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
588 if (children != null) {
17275
846c059e3ecf Truffle: allow interface types in child fields
Andreas Woess <andreas.woess@jku.at>
parents: 16779
diff changeset
589 for (Object child : children) {
10845
e9248ebb1d79 Truffle: ignore null children in NodeUtil.findNodeChildren; refactoring.
Andreas Woess <andreas.woess@jku.at>
parents: 10793
diff changeset
590 if (child != null) {
17275
846c059e3ecf Truffle: allow interface types in child fields
Andreas Woess <andreas.woess@jku.at>
parents: 16779
diff changeset
591 nodes.add((Node) child);
10845
e9248ebb1d79 Truffle: ignore null children in NodeUtil.findNodeChildren; refactoring.
Andreas Woess <andreas.woess@jku.at>
parents: 10793
diff changeset
592 }
e9248ebb1d79 Truffle: ignore null children in NodeUtil.findNodeChildren; refactoring.
Andreas Woess <andreas.woess@jku.at>
parents: 10793
diff changeset
593 }
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
594 }
a4b84ba6dc2e Introduction of the Truffle API for efficient implementation of dynamic languages on top of the Graal VM. New projects com.oracle.truffle.api for the API definition and com.oracle.truffle.api.test for API tests and documentation.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
595 }
a4b84ba6dc2e Introduction of the Truffle API for efficient implementation of dynamic languages on top of the Graal VM. New projects com.oracle.truffle.api for the API definition and com.oracle.truffle.api.test for API tests and documentation.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
596
a4b84ba6dc2e Introduction of the Truffle API for efficient implementation of dynamic languages on top of the Graal VM. New projects com.oracle.truffle.api for the API definition and com.oracle.truffle.api.test for API tests and documentation.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
597 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
598 }
a4b84ba6dc2e Introduction of the Truffle API for efficient implementation of dynamic languages on top of the Graal VM. New projects com.oracle.truffle.api for the API definition and com.oracle.truffle.api.test for API tests and documentation.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
599
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
600 public static boolean replaceChild(Node parent, Node oldChild, Node newChild) {
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
601 NodeClass nodeClass = NodeClass.get(parent.getClass());
a4b84ba6dc2e Introduction of the Truffle API for efficient implementation of dynamic languages on top of the Graal VM. New projects com.oracle.truffle.api for the API definition and com.oracle.truffle.api.test for API tests and documentation.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
602
18904
6ba170cb6f53 Truffle: channel all NodeUtil field accesses through NodeField class
Andreas Woess <andreas.woess@jku.at>
parents: 18751
diff changeset
603 for (NodeField nodeField : nodeClass.getChildFields()) {
6ba170cb6f53 Truffle: channel all NodeUtil field accesses through NodeField class
Andreas Woess <andreas.woess@jku.at>
parents: 18751
diff changeset
604 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
605 assert assertAssignable(nodeField, newChild);
6ba170cb6f53 Truffle: channel all NodeUtil field accesses through NodeField class
Andreas Woess <andreas.woess@jku.at>
parents: 18751
diff changeset
606 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
607 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
608 }
a4b84ba6dc2e Introduction of the Truffle API for efficient implementation of dynamic languages on top of the Graal VM. New projects com.oracle.truffle.api for the API definition and com.oracle.truffle.api.test for API tests and documentation.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
609 }
10742
99789440ce28 Truffle: Added assertions to replaceChild.
Christian Humer <christian.humer@gmail.com>
parents: 10704
diff changeset
610
18904
6ba170cb6f53 Truffle: channel all NodeUtil field accesses through NodeField class
Andreas Woess <andreas.woess@jku.at>
parents: 18751
diff changeset
611 for (NodeField nodeField : nodeClass.getChildrenFields()) {
6ba170cb6f53 Truffle: channel all NodeUtil field accesses through NodeField class
Andreas Woess <andreas.woess@jku.at>
parents: 18751
diff changeset
612 Object arrayObject = nodeField.getObject(parent);
10742
99789440ce28 Truffle: Added assertions to replaceChild.
Christian Humer <christian.humer@gmail.com>
parents: 10704
diff changeset
613 if (arrayObject != null) {
17275
846c059e3ecf Truffle: allow interface types in child fields
Andreas Woess <andreas.woess@jku.at>
parents: 16779
diff changeset
614 Object[] array = (Object[]) arrayObject;
10755
b221e31d7b0b Truffle: revise NodeUtil.replaceChild assertion
Andreas Woess <andreas.woess@jku.at>
parents: 10742
diff changeset
615 for (int i = 0; i < array.length; i++) {
b221e31d7b0b Truffle: revise NodeUtil.replaceChild assertion
Andreas Woess <andreas.woess@jku.at>
parents: 10742
diff changeset
616 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
617 assert assertAssignable(nodeField, newChild);
10755
b221e31d7b0b Truffle: revise NodeUtil.replaceChild assertion
Andreas Woess <andreas.woess@jku.at>
parents: 10742
diff changeset
618 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
619 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
620 }
a4b84ba6dc2e Introduction of the Truffle API for efficient implementation of dynamic languages on top of the Graal VM. New projects com.oracle.truffle.api for the API definition and com.oracle.truffle.api.test for API tests and documentation.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
621 }
a4b84ba6dc2e Introduction of the Truffle API for efficient implementation of dynamic languages on top of the Graal VM. New projects com.oracle.truffle.api for the API definition and com.oracle.truffle.api.test for API tests and documentation.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
622 }
a4b84ba6dc2e Introduction of the Truffle API for efficient implementation of dynamic languages on top of the Graal VM. New projects com.oracle.truffle.api for the API definition and com.oracle.truffle.api.test for API tests and documentation.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
623 }
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
624 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
625 }
a4b84ba6dc2e Introduction of the Truffle API for efficient implementation of dynamic languages on top of the Graal VM. New projects com.oracle.truffle.api for the API definition and com.oracle.truffle.api.test for API tests and documentation.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
626
18904
6ba170cb6f53 Truffle: channel all NodeUtil field accesses through NodeField class
Andreas Woess <andreas.woess@jku.at>
parents: 18751
diff changeset
627 private static boolean assertAssignable(NodeField field, Object newValue) {
10742
99789440ce28 Truffle: Added assertions to replaceChild.
Christian Humer <christian.humer@gmail.com>
parents: 10704
diff changeset
628 if (newValue == null) {
99789440ce28 Truffle: Added assertions to replaceChild.
Christian Humer <christian.humer@gmail.com>
parents: 10704
diff changeset
629 return true;
99789440ce28 Truffle: Added assertions to replaceChild.
Christian Humer <christian.humer@gmail.com>
parents: 10704
diff changeset
630 }
18904
6ba170cb6f53 Truffle: channel all NodeUtil field accesses through NodeField class
Andreas Woess <andreas.woess@jku.at>
parents: 18751
diff changeset
631 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
632 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
633 return true;
6ba170cb6f53 Truffle: channel all NodeUtil field accesses through NodeField class
Andreas Woess <andreas.woess@jku.at>
parents: 18751
diff changeset
634 } else {
6ba170cb6f53 Truffle: channel all NodeUtil field accesses through NodeField class
Andreas Woess <andreas.woess@jku.at>
parents: 18751
diff changeset
635 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
636 return false;
6ba170cb6f53 Truffle: channel all NodeUtil field accesses through NodeField class
Andreas Woess <andreas.woess@jku.at>
parents: 18751
diff changeset
637 }
6ba170cb6f53 Truffle: channel all NodeUtil field accesses through NodeField class
Andreas Woess <andreas.woess@jku.at>
parents: 18751
diff changeset
638 } 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
639 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
640 return true;
6ba170cb6f53 Truffle: channel all NodeUtil field accesses through NodeField class
Andreas Woess <andreas.woess@jku.at>
parents: 18751
diff changeset
641 } else {
6ba170cb6f53 Truffle: channel all NodeUtil field accesses through NodeField class
Andreas Woess <andreas.woess@jku.at>
parents: 18751
diff changeset
642 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
643 return false;
10742
99789440ce28 Truffle: Added assertions to replaceChild.
Christian Humer <christian.humer@gmail.com>
parents: 10704
diff changeset
644 }
99789440ce28 Truffle: Added assertions to replaceChild.
Christian Humer <christian.humer@gmail.com>
parents: 10704
diff changeset
645 }
10755
b221e31d7b0b Truffle: revise NodeUtil.replaceChild assertion
Andreas Woess <andreas.woess@jku.at>
parents: 10742
diff changeset
646 throw new IllegalArgumentException();
10742
99789440ce28 Truffle: Added assertions to replaceChild.
Christian Humer <christian.humer@gmail.com>
parents: 10704
diff changeset
647 }
99789440ce28 Truffle: Added assertions to replaceChild.
Christian Humer <christian.humer@gmail.com>
parents: 10704
diff changeset
648
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
649 /**
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
650 * 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
651 *
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
652 * @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
653 */
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
654 public static NodeField findChildField(Node parent, Node child) {
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
655 assert child != 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
656 NodeClass parentNodeClass = NodeClass.get(parent.getClass());
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
657
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
658 for (NodeField field : parentNodeClass.getChildFields()) {
19012
f5b83e7b2b4c Truffle: simplify NodeUtil.findChildField
Andreas Woess <andreas.woess@jku.at>
parents: 19010
diff changeset
659 if (field.getObject(parent) == child) {
f5b83e7b2b4c Truffle: simplify NodeUtil.findChildField
Andreas Woess <andreas.woess@jku.at>
parents: 19010
diff changeset
660 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
661 }
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
662 }
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
663
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
664 for (NodeField field : parentNodeClass.getChildrenFields()) {
19012
f5b83e7b2b4c Truffle: simplify NodeUtil.findChildField
Andreas Woess <andreas.woess@jku.at>
parents: 19010
diff changeset
665 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
666 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
667 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
668 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
669 if (array[i] == child) {
19012
f5b83e7b2b4c Truffle: simplify NodeUtil.findChildField
Andreas Woess <andreas.woess@jku.at>
parents: 19010
diff changeset
670 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
671 }
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
672 }
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
673 }
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
674 }
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
675 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
676 }
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
677
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
678 /**
19141
67d9e635102f Truffle/Instrumentation: refine checks for safe node replacement
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 19012
diff changeset
679 * 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
680 */
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
681 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
682 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
683 if (parent != null) {
67d9e635102f Truffle/Instrumentation: refine checks for safe node replacement
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 19012
diff changeset
684 final NodeField field = findChildField(parent, oldChild);
67d9e635102f Truffle/Instrumentation: refine checks for safe node replacement
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 19012
diff changeset
685 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
686 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
687 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
688 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
689 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
690 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
691 default:
67d9e635102f Truffle/Instrumentation: refine checks for safe node replacement
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 19012
diff changeset
692 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
693 }
67d9e635102f Truffle/Instrumentation: refine checks for safe node replacement
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 19012
diff changeset
694 }
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
695 }
19141
67d9e635102f Truffle/Instrumentation: refine checks for safe node replacement
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 19012
diff changeset
696 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
697 }
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
698
19607
73811d1b4cd0 Truffle: add NodeUtil.forEachChild
Andreas Woess <andreas.woess@oracle.com>
parents: 19141
diff changeset
699 /**
73811d1b4cd0 Truffle: add NodeUtil.forEachChild
Andreas Woess <andreas.woess@oracle.com>
parents: 19141
diff changeset
700 * 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
701 *
73811d1b4cd0 Truffle: add NodeUtil.forEachChild
Andreas Woess <andreas.woess@oracle.com>
parents: 19141
diff changeset
702 * @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
703 */
73811d1b4cd0 Truffle: add NodeUtil.forEachChild
Andreas Woess <andreas.woess@oracle.com>
parents: 19141
diff changeset
704 public static boolean forEachChild(Node parent, NodeVisitor visitor) {
73811d1b4cd0 Truffle: add NodeUtil.forEachChild
Andreas Woess <andreas.woess@oracle.com>
parents: 19141
diff changeset
705 Objects.requireNonNull(visitor);
73811d1b4cd0 Truffle: add NodeUtil.forEachChild
Andreas Woess <andreas.woess@oracle.com>
parents: 19141
diff changeset
706 NodeClass parentNodeClass = NodeClass.get(parent.getClass());
73811d1b4cd0 Truffle: add NodeUtil.forEachChild
Andreas Woess <andreas.woess@oracle.com>
parents: 19141
diff changeset
707
73811d1b4cd0 Truffle: add NodeUtil.forEachChild
Andreas Woess <andreas.woess@oracle.com>
parents: 19141
diff changeset
708 for (NodeField field : parentNodeClass.getChildFields()) {
73811d1b4cd0 Truffle: add NodeUtil.forEachChild
Andreas Woess <andreas.woess@oracle.com>
parents: 19141
diff changeset
709 Object child = field.getObject(parent);
73811d1b4cd0 Truffle: add NodeUtil.forEachChild
Andreas Woess <andreas.woess@oracle.com>
parents: 19141
diff changeset
710 if (child != null) {
73811d1b4cd0 Truffle: add NodeUtil.forEachChild
Andreas Woess <andreas.woess@oracle.com>
parents: 19141
diff changeset
711 if (!visitor.visit((Node) child)) {
73811d1b4cd0 Truffle: add NodeUtil.forEachChild
Andreas Woess <andreas.woess@oracle.com>
parents: 19141
diff changeset
712 return false;
73811d1b4cd0 Truffle: add NodeUtil.forEachChild
Andreas Woess <andreas.woess@oracle.com>
parents: 19141
diff changeset
713 }
73811d1b4cd0 Truffle: add NodeUtil.forEachChild
Andreas Woess <andreas.woess@oracle.com>
parents: 19141
diff changeset
714 }
73811d1b4cd0 Truffle: add NodeUtil.forEachChild
Andreas Woess <andreas.woess@oracle.com>
parents: 19141
diff changeset
715 }
73811d1b4cd0 Truffle: add NodeUtil.forEachChild
Andreas Woess <andreas.woess@oracle.com>
parents: 19141
diff changeset
716
73811d1b4cd0 Truffle: add NodeUtil.forEachChild
Andreas Woess <andreas.woess@oracle.com>
parents: 19141
diff changeset
717 for (NodeField field : parentNodeClass.getChildrenFields()) {
73811d1b4cd0 Truffle: add NodeUtil.forEachChild
Andreas Woess <andreas.woess@oracle.com>
parents: 19141
diff changeset
718 Object arrayObject = field.getObject(parent);
73811d1b4cd0 Truffle: add NodeUtil.forEachChild
Andreas Woess <andreas.woess@oracle.com>
parents: 19141
diff changeset
719 if (arrayObject != null) {
73811d1b4cd0 Truffle: add NodeUtil.forEachChild
Andreas Woess <andreas.woess@oracle.com>
parents: 19141
diff changeset
720 Object[] array = (Object[]) arrayObject;
73811d1b4cd0 Truffle: add NodeUtil.forEachChild
Andreas Woess <andreas.woess@oracle.com>
parents: 19141
diff changeset
721 for (int i = 0; i < array.length; i++) {
73811d1b4cd0 Truffle: add NodeUtil.forEachChild
Andreas Woess <andreas.woess@oracle.com>
parents: 19141
diff changeset
722 Object child = array[i];
73811d1b4cd0 Truffle: add NodeUtil.forEachChild
Andreas Woess <andreas.woess@oracle.com>
parents: 19141
diff changeset
723 if (child != null) {
73811d1b4cd0 Truffle: add NodeUtil.forEachChild
Andreas Woess <andreas.woess@oracle.com>
parents: 19141
diff changeset
724 if (!visitor.visit((Node) child)) {
73811d1b4cd0 Truffle: add NodeUtil.forEachChild
Andreas Woess <andreas.woess@oracle.com>
parents: 19141
diff changeset
725 return false;
73811d1b4cd0 Truffle: add NodeUtil.forEachChild
Andreas Woess <andreas.woess@oracle.com>
parents: 19141
diff changeset
726 }
73811d1b4cd0 Truffle: add NodeUtil.forEachChild
Andreas Woess <andreas.woess@oracle.com>
parents: 19141
diff changeset
727 }
73811d1b4cd0 Truffle: add NodeUtil.forEachChild
Andreas Woess <andreas.woess@oracle.com>
parents: 19141
diff changeset
728 }
73811d1b4cd0 Truffle: add NodeUtil.forEachChild
Andreas Woess <andreas.woess@oracle.com>
parents: 19141
diff changeset
729 }
73811d1b4cd0 Truffle: add NodeUtil.forEachChild
Andreas Woess <andreas.woess@oracle.com>
parents: 19141
diff changeset
730 }
73811d1b4cd0 Truffle: add NodeUtil.forEachChild
Andreas Woess <andreas.woess@oracle.com>
parents: 19141
diff changeset
731
73811d1b4cd0 Truffle: add NodeUtil.forEachChild
Andreas Woess <andreas.woess@oracle.com>
parents: 19141
diff changeset
732 return true;
73811d1b4cd0 Truffle: add NodeUtil.forEachChild
Andreas Woess <andreas.woess@oracle.com>
parents: 19141
diff changeset
733 }
73811d1b4cd0 Truffle: add NodeUtil.forEachChild
Andreas Woess <andreas.woess@oracle.com>
parents: 19141
diff changeset
734
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
735 /** Returns all declared fields in the class hierarchy. */
9754
98b004bf3985 Refactoring of NodeUtil to centralize the use of reflection
Christian Wimmer <christian.wimmer@oracle.com>
parents: 9538
diff changeset
736 private 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
737 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
738 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
739 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
740 }
a4b84ba6dc2e Introduction of the Truffle API for efficient implementation of dynamic languages on top of the Graal VM. New projects com.oracle.truffle.api for the API definition and com.oracle.truffle.api.test for API tests and documentation.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
741 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
742 }
a4b84ba6dc2e Introduction of the Truffle API for efficient implementation of dynamic languages on top of the Graal VM. New projects com.oracle.truffle.api for the API definition and com.oracle.truffle.api.test for API tests and documentation.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
743
a4b84ba6dc2e Introduction of the Truffle API for efficient implementation of dynamic languages on top of the Graal VM. New projects com.oracle.truffle.api for the API definition and com.oracle.truffle.api.test for API tests and documentation.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
744 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
745 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
746 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
747 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
748 }
a4b84ba6dc2e Introduction of the Truffle API for efficient implementation of dynamic languages on top of the Graal VM. New projects com.oracle.truffle.api for the API definition and com.oracle.truffle.api.test for API tests and documentation.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
749
11622
ccf047a30f18 Truffle: NodeUtil.getNthParent.
Chris Seaton <chris.seaton@oracle.com>
parents: 11279
diff changeset
750 /**
ccf047a30f18 Truffle: NodeUtil.getNthParent.
Chris Seaton <chris.seaton@oracle.com>
parents: 11279
diff changeset
751 * 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
752 * are less than n ancestors.
ccf047a30f18 Truffle: NodeUtil.getNthParent.
Chris Seaton <chris.seaton@oracle.com>
parents: 11279
diff changeset
753 */
ccf047a30f18 Truffle: NodeUtil.getNthParent.
Chris Seaton <chris.seaton@oracle.com>
parents: 11279
diff changeset
754 public static Node getNthParent(Node node, int n) {
ccf047a30f18 Truffle: NodeUtil.getNthParent.
Chris Seaton <chris.seaton@oracle.com>
parents: 11279
diff changeset
755 Node parent = node;
ccf047a30f18 Truffle: NodeUtil.getNthParent.
Chris Seaton <chris.seaton@oracle.com>
parents: 11279
diff changeset
756
ccf047a30f18 Truffle: NodeUtil.getNthParent.
Chris Seaton <chris.seaton@oracle.com>
parents: 11279
diff changeset
757 for (int i = 0; i < n; i++) {
ccf047a30f18 Truffle: NodeUtil.getNthParent.
Chris Seaton <chris.seaton@oracle.com>
parents: 11279
diff changeset
758 parent = parent.getParent();
ccf047a30f18 Truffle: NodeUtil.getNthParent.
Chris Seaton <chris.seaton@oracle.com>
parents: 11279
diff changeset
759
ccf047a30f18 Truffle: NodeUtil.getNthParent.
Chris Seaton <chris.seaton@oracle.com>
parents: 11279
diff changeset
760 if (parent == null) {
ccf047a30f18 Truffle: NodeUtil.getNthParent.
Chris Seaton <chris.seaton@oracle.com>
parents: 11279
diff changeset
761 return null;
ccf047a30f18 Truffle: NodeUtil.getNthParent.
Chris Seaton <chris.seaton@oracle.com>
parents: 11279
diff changeset
762 }
ccf047a30f18 Truffle: NodeUtil.getNthParent.
Chris Seaton <chris.seaton@oracle.com>
parents: 11279
diff changeset
763 }
ccf047a30f18 Truffle: NodeUtil.getNthParent.
Chris Seaton <chris.seaton@oracle.com>
parents: 11279
diff changeset
764
ccf047a30f18 Truffle: NodeUtil.getNthParent.
Chris Seaton <chris.seaton@oracle.com>
parents: 11279
diff changeset
765 return parent;
ccf047a30f18 Truffle: NodeUtil.getNthParent.
Chris Seaton <chris.seaton@oracle.com>
parents: 11279
diff changeset
766 }
ccf047a30f18 Truffle: NodeUtil.getNthParent.
Chris Seaton <chris.seaton@oracle.com>
parents: 11279
diff changeset
767
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
768 /** 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
769 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
770 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
771 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
772 } else {
7530
5e3d1a68664e applied mx eclipseformat to all Java files
Doug Simon <doug.simon@oracle.com>
parents: 7267
diff changeset
773 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
774 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
775 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
776 }
a4b84ba6dc2e Introduction of the Truffle API for efficient implementation of dynamic languages on top of the Graal VM. New projects com.oracle.truffle.api for the API definition and com.oracle.truffle.api.test for API tests and documentation.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
777 }
a4b84ba6dc2e Introduction of the Truffle API for efficient implementation of dynamic languages on top of the Graal VM. New projects com.oracle.truffle.api for the API definition and com.oracle.truffle.api.test for API tests and documentation.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
778 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
779 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
780 }
a4b84ba6dc2e Introduction of the Truffle API for efficient implementation of dynamic languages on top of the Graal VM. New projects com.oracle.truffle.api for the API definition and com.oracle.truffle.api.test for API tests and documentation.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
781 }
a4b84ba6dc2e Introduction of the Truffle API for efficient implementation of dynamic languages on top of the Graal VM. New projects com.oracle.truffle.api for the API definition and com.oracle.truffle.api.test for API tests and documentation.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
782 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
783 }
a4b84ba6dc2e Introduction of the Truffle API for efficient implementation of dynamic languages on top of the Graal VM. New projects com.oracle.truffle.api for the API definition and com.oracle.truffle.api.test for API tests and documentation.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
784
12666
d9c34e8337f4 Truffle: cleanup of NodeUtil#findParent; added NodeUtil#findAllParents
Christian Humer <christian.humer@gmail.com>
parents: 11950
diff changeset
785 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
786 Node parent = start.getParent();
d9c34e8337f4 Truffle: cleanup of NodeUtil#findParent; added NodeUtil#findAllParents
Christian Humer <christian.humer@gmail.com>
parents: 11950
diff changeset
787 if (parent == null) {
d9c34e8337f4 Truffle: cleanup of NodeUtil#findParent; added NodeUtil#findAllParents
Christian Humer <christian.humer@gmail.com>
parents: 11950
diff changeset
788 return null;
d9c34e8337f4 Truffle: cleanup of NodeUtil#findParent; added NodeUtil#findAllParents
Christian Humer <christian.humer@gmail.com>
parents: 11950
diff changeset
789 } else if (clazz.isInstance(parent)) {
d9c34e8337f4 Truffle: cleanup of NodeUtil#findParent; added NodeUtil#findAllParents
Christian Humer <christian.humer@gmail.com>
parents: 11950
diff changeset
790 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
791 } else {
12666
d9c34e8337f4 Truffle: cleanup of NodeUtil#findParent; added NodeUtil#findAllParents
Christian Humer <christian.humer@gmail.com>
parents: 11950
diff changeset
792 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
793 }
a4b84ba6dc2e Introduction of the Truffle API for efficient implementation of dynamic languages on top of the Graal VM. New projects com.oracle.truffle.api for the API definition and com.oracle.truffle.api.test for API tests and documentation.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
794 }
a4b84ba6dc2e Introduction of the Truffle API for efficient implementation of dynamic languages on top of the Graal VM. New projects com.oracle.truffle.api for the API definition and com.oracle.truffle.api.test for API tests and documentation.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
795
12666
d9c34e8337f4 Truffle: cleanup of NodeUtil#findParent; added NodeUtil#findAllParents
Christian Humer <christian.humer@gmail.com>
parents: 11950
diff changeset
796 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
797 List<T> parents = new ArrayList<>();
d9c34e8337f4 Truffle: cleanup of NodeUtil#findParent; added NodeUtil#findAllParents
Christian Humer <christian.humer@gmail.com>
parents: 11950
diff changeset
798 T parent = findParent(start, clazz);
d9c34e8337f4 Truffle: cleanup of NodeUtil#findParent; added NodeUtil#findAllParents
Christian Humer <christian.humer@gmail.com>
parents: 11950
diff changeset
799 while (parent != null) {
d9c34e8337f4 Truffle: cleanup of NodeUtil#findParent; added NodeUtil#findAllParents
Christian Humer <christian.humer@gmail.com>
parents: 11950
diff changeset
800 parents.add(parent);
d9c34e8337f4 Truffle: cleanup of NodeUtil#findParent; added NodeUtil#findAllParents
Christian Humer <christian.humer@gmail.com>
parents: 11950
diff changeset
801 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
802 }
12666
d9c34e8337f4 Truffle: cleanup of NodeUtil#findParent; added NodeUtil#findAllParents
Christian Humer <christian.humer@gmail.com>
parents: 11950
diff changeset
803 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
804 }
a4b84ba6dc2e Introduction of the Truffle API for efficient implementation of dynamic languages on top of the Graal VM. New projects com.oracle.truffle.api for the API definition and com.oracle.truffle.api.test for API tests and documentation.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
805
12666
d9c34e8337f4 Truffle: cleanup of NodeUtil#findParent; added NodeUtil#findAllParents
Christian Humer <christian.humer@gmail.com>
parents: 11950
diff changeset
806 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
807 List<Node> nodes = new ArrayList<>();
d9c34e8337f4 Truffle: cleanup of NodeUtil#findParent; added NodeUtil#findAllParents
Christian Humer <christian.humer@gmail.com>
parents: 11950
diff changeset
808 Node current = child;
d9c34e8337f4 Truffle: cleanup of NodeUtil#findParent; added NodeUtil#findAllParents
Christian Humer <christian.humer@gmail.com>
parents: 11950
diff changeset
809 while (current != null) {
d9c34e8337f4 Truffle: cleanup of NodeUtil#findParent; added NodeUtil#findAllParents
Christian Humer <christian.humer@gmail.com>
parents: 11950
diff changeset
810 nodes.add(current);
d9c34e8337f4 Truffle: cleanup of NodeUtil#findParent; added NodeUtil#findAllParents
Christian Humer <christian.humer@gmail.com>
parents: 11950
diff changeset
811 if (current == parent) {
d9c34e8337f4 Truffle: cleanup of NodeUtil#findParent; added NodeUtil#findAllParents
Christian Humer <christian.humer@gmail.com>
parents: 11950
diff changeset
812 return nodes;
d9c34e8337f4 Truffle: cleanup of NodeUtil#findParent; added NodeUtil#findAllParents
Christian Humer <christian.humer@gmail.com>
parents: 11950
diff changeset
813 }
d9c34e8337f4 Truffle: cleanup of NodeUtil#findParent; added NodeUtil#findAllParents
Christian Humer <christian.humer@gmail.com>
parents: 11950
diff changeset
814 current = current.getParent();
d9c34e8337f4 Truffle: cleanup of NodeUtil#findParent; added NodeUtil#findAllParents
Christian Humer <christian.humer@gmail.com>
parents: 11950
diff changeset
815 }
d9c34e8337f4 Truffle: cleanup of NodeUtil#findParent; added NodeUtil#findAllParents
Christian Humer <christian.humer@gmail.com>
parents: 11950
diff changeset
816 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
817 }
d9c34e8337f4 Truffle: cleanup of NodeUtil#findParent; added NodeUtil#findAllParents
Christian Humer <christian.humer@gmail.com>
parents: 11950
diff changeset
818
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
819 public static <T> T findFirstNodeInstance(Node root, Class<T> clazz) {
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
820 for (Node childNode : findNodeChildren(root)) {
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
821 if (clazz.isInstance(childNode)) {
12666
d9c34e8337f4 Truffle: cleanup of NodeUtil#findParent; added NodeUtil#findAllParents
Christian Humer <christian.humer@gmail.com>
parents: 11950
diff changeset
822 return clazz.cast(childNode);
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
823 } else {
9487
0285fbb8b05d NodeUtil.findFirstNodeInstance bug fix
Andreas Woess <andreas.woess@jku.at>
parents: 9486
diff changeset
824 T node = findFirstNodeInstance(childNode, clazz);
0285fbb8b05d NodeUtil.findFirstNodeInstance bug fix
Andreas Woess <andreas.woess@jku.at>
parents: 9486
diff changeset
825 if (node != null) {
0285fbb8b05d NodeUtil.findFirstNodeInstance bug fix
Andreas Woess <andreas.woess@jku.at>
parents: 9486
diff changeset
826 return node;
0285fbb8b05d NodeUtil.findFirstNodeInstance bug fix
Andreas Woess <andreas.woess@jku.at>
parents: 9486
diff changeset
827 }
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
828 }
a4b84ba6dc2e Introduction of the Truffle API for efficient implementation of dynamic languages on top of the Graal VM. New projects com.oracle.truffle.api for the API definition and com.oracle.truffle.api.test for API tests and documentation.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
829 }
a4b84ba6dc2e Introduction of the Truffle API for efficient implementation of dynamic languages on top of the Graal VM. New projects com.oracle.truffle.api for the API definition and com.oracle.truffle.api.test for API tests and documentation.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
830 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
831 }
a4b84ba6dc2e Introduction of the Truffle API for efficient implementation of dynamic languages on top of the Graal VM. New projects com.oracle.truffle.api for the API definition and com.oracle.truffle.api.test for API tests and documentation.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
832
16465
456abab80eb5 Truffle: remove obsolete NodeUtil.findNodeInstancesInFunction (functionally equivalent to findAllNodeInstances)
Andreas Woess <andreas.woess@jku.at>
parents: 16464
diff changeset
833 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
834 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
835 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
836 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
837 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
838 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
839 }
a4b84ba6dc2e Introduction of the Truffle API for efficient implementation of dynamic languages on top of the Graal VM. New projects com.oracle.truffle.api for the API definition and com.oracle.truffle.api.test for API tests and documentation.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
840 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
841 }
a4b84ba6dc2e Introduction of the Truffle API for efficient implementation of dynamic languages on top of the Graal VM. New projects com.oracle.truffle.api for the API definition and com.oracle.truffle.api.test for API tests and documentation.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
842 });
a4b84ba6dc2e Introduction of the Truffle API for efficient implementation of dynamic languages on top of the Graal VM. New projects com.oracle.truffle.api for the API definition and com.oracle.truffle.api.test for API tests and documentation.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
843 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
844 }
a4b84ba6dc2e Introduction of the Truffle API for efficient implementation of dynamic languages on top of the Graal VM. New projects com.oracle.truffle.api 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
16465
456abab80eb5 Truffle: remove obsolete NodeUtil.findNodeInstancesInFunction (functionally equivalent to findAllNodeInstances)
Andreas Woess <andreas.woess@jku.at>
parents: 16464
diff changeset
846 /**
456abab80eb5 Truffle: remove obsolete NodeUtil.findNodeInstancesInFunction (functionally equivalent to findAllNodeInstances)
Andreas Woess <andreas.woess@jku.at>
parents: 16464
diff changeset
847 * Like {@link #findAllNodeInstances(Node, Class)} but do not visit children of found nodes.
456abab80eb5 Truffle: remove obsolete NodeUtil.findNodeInstancesInFunction (functionally equivalent to findAllNodeInstances)
Andreas Woess <andreas.woess@jku.at>
parents: 16464
diff changeset
848 */
456abab80eb5 Truffle: remove obsolete NodeUtil.findNodeInstancesInFunction (functionally equivalent to findAllNodeInstances)
Andreas Woess <andreas.woess@jku.at>
parents: 16464
diff changeset
849 public static <T> List<T> findNodeInstancesShallow(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
850 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
851 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
852 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
853 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
854 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
855 return false;
a4b84ba6dc2e Introduction of the Truffle API for efficient implementation of dynamic languages on top of the Graal VM. New projects com.oracle.truffle.api for the API definition and com.oracle.truffle.api.test for API tests and documentation.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
856 }
a4b84ba6dc2e Introduction of the Truffle API for efficient implementation of dynamic languages on top of the Graal VM. New projects com.oracle.truffle.api for the API definition and com.oracle.truffle.api.test for API tests and documentation.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
857 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
858 }
a4b84ba6dc2e Introduction of the Truffle API for efficient implementation of dynamic languages on top of the Graal VM. New projects com.oracle.truffle.api for the API definition and com.oracle.truffle.api.test for API tests and documentation.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
859 });
a4b84ba6dc2e Introduction of the Truffle API for efficient implementation of dynamic languages on top of the Graal VM. New projects com.oracle.truffle.api for the API definition and com.oracle.truffle.api.test for API tests and documentation.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
860 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
861 }
a4b84ba6dc2e Introduction of the Truffle API for efficient implementation of dynamic languages on top of the Graal VM. New projects com.oracle.truffle.api for the API definition and com.oracle.truffle.api.test for API tests and documentation.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
862
10704
84b944726df2 Truffle: introduce NodeUtil.countNodes
Andreas Woess <andreas.woess@jku.at>
parents: 9754
diff changeset
863 public static int countNodes(Node root) {
17399
5787218bad91 Truffle: implemented recursive node iterator and node streams for the graal runtime.
Christian Humer <christian.humer@gmail.com>
parents: 17275
diff changeset
864 Iterator<Node> nodeIterator = makeRecursiveIterator(root);
5787218bad91 Truffle: implemented recursive node iterator and node streams for the graal runtime.
Christian Humer <christian.humer@gmail.com>
parents: 17275
diff changeset
865 int count = 0;
5787218bad91 Truffle: implemented recursive node iterator and node streams for the graal runtime.
Christian Humer <christian.humer@gmail.com>
parents: 17275
diff changeset
866 while (nodeIterator.hasNext()) {
5787218bad91 Truffle: implemented recursive node iterator and node streams for the graal runtime.
Christian Humer <christian.humer@gmail.com>
parents: 17275
diff changeset
867 nodeIterator.next();
5787218bad91 Truffle: implemented recursive node iterator and node streams for the graal runtime.
Christian Humer <christian.humer@gmail.com>
parents: 17275
diff changeset
868 count++;
5787218bad91 Truffle: implemented recursive node iterator and node streams for the graal runtime.
Christian Humer <christian.humer@gmail.com>
parents: 17275
diff changeset
869 }
5787218bad91 Truffle: implemented recursive node iterator and node streams for the graal runtime.
Christian Humer <christian.humer@gmail.com>
parents: 17275
diff changeset
870 return count;
13995
e455fc531ec2 Truffle: Added API in NodeUtil to count nodes restricted to a Kind.
Christian Humer <christian.humer@gmail.com>
parents: 13983
diff changeset
871 }
e455fc531ec2 Truffle: Added API in NodeUtil to count nodes restricted to a Kind.
Christian Humer <christian.humer@gmail.com>
parents: 13983
diff changeset
872
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
873 public static int countNodes(Node root, NodeCountFilter filter) {
17399
5787218bad91 Truffle: implemented recursive node iterator and node streams for the graal runtime.
Christian Humer <christian.humer@gmail.com>
parents: 17275
diff changeset
874 Iterator<Node> nodeIterator = makeRecursiveIterator(root);
5787218bad91 Truffle: implemented recursive node iterator and node streams for the graal runtime.
Christian Humer <christian.humer@gmail.com>
parents: 17275
diff changeset
875 int count = 0;
5787218bad91 Truffle: implemented recursive node iterator and node streams for the graal runtime.
Christian Humer <christian.humer@gmail.com>
parents: 17275
diff changeset
876 while (nodeIterator.hasNext()) {
5787218bad91 Truffle: implemented recursive node iterator and node streams for the graal runtime.
Christian Humer <christian.humer@gmail.com>
parents: 17275
diff changeset
877 Node node = nodeIterator.next();
5787218bad91 Truffle: implemented recursive node iterator and node streams for the graal runtime.
Christian Humer <christian.humer@gmail.com>
parents: 17275
diff changeset
878 if (node != null && filter.isCounted(node)) {
5787218bad91 Truffle: implemented recursive node iterator and node streams for the graal runtime.
Christian Humer <christian.humer@gmail.com>
parents: 17275
diff changeset
879 count++;
5787218bad91 Truffle: implemented recursive node iterator and node streams for the graal runtime.
Christian Humer <christian.humer@gmail.com>
parents: 17275
diff changeset
880 }
5787218bad91 Truffle: implemented recursive node iterator and node streams for the graal runtime.
Christian Humer <christian.humer@gmail.com>
parents: 17275
diff changeset
881 }
5787218bad91 Truffle: implemented recursive node iterator and node streams for the graal runtime.
Christian Humer <christian.humer@gmail.com>
parents: 17275
diff changeset
882 return 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
883 }
6e12e0ace0d5 Truffle: add NodeUtil.countNodes overload that counts nodes of a specific type
Andreas Woess <andreas.woess@jku.at>
parents: 10756
diff changeset
884
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
885 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
886
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
887 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
888
10704
84b944726df2 Truffle: introduce NodeUtil.countNodes
Andreas Woess <andreas.woess@jku.at>
parents: 9754
diff changeset
889 }
84b944726df2 Truffle: introduce NodeUtil.countNodes
Andreas Woess <andreas.woess@jku.at>
parents: 9754
diff changeset
890
9754
98b004bf3985 Refactoring of NodeUtil to centralize the use of reflection
Christian Wimmer <christian.wimmer@oracle.com>
parents: 9538
diff changeset
891 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
892 StringWriter out = new StringWriter();
98b004bf3985 Refactoring of NodeUtil to centralize the use of reflection
Christian Wimmer <christian.wimmer@oracle.com>
parents: 9538
diff changeset
893 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
894 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
895 }
a4b84ba6dc2e Introduction of the Truffle API for efficient implementation of dynamic languages on top of the Graal VM. New projects com.oracle.truffle.api for the API definition and com.oracle.truffle.api.test for API tests and documentation.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
896
9754
98b004bf3985 Refactoring of NodeUtil to centralize the use of reflection
Christian Wimmer <christian.wimmer@oracle.com>
parents: 9538
diff changeset
897 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
898 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
899 }
a4b84ba6dc2e Introduction of the Truffle API for efficient implementation of dynamic languages on top of the Graal VM. New projects com.oracle.truffle.api for the API definition and com.oracle.truffle.api.test for API tests and documentation.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
900
9754
98b004bf3985 Refactoring of NodeUtil to centralize the use of reflection
Christian Wimmer <christian.wimmer@oracle.com>
parents: 9538
diff changeset
901 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
902 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
903 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
904 }
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
905 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
906 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
907 }
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
908 if (parent == null) {
10845
e9248ebb1d79 Truffle: ignore null children in NodeUtil.findNodeChildren; refactoring.
Andreas Woess <andreas.woess@jku.at>
parents: 10793
diff changeset
909 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
910 } else {
17275
846c059e3ecf Truffle: allow interface types in child fields
Andreas Woess <andreas.woess@jku.at>
parents: 16779
diff changeset
911 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
912 p.print(" = ");
10845
e9248ebb1d79 Truffle: ignore null children in NodeUtil.findNodeChildren; refactoring.
Andreas Woess <andreas.woess@jku.at>
parents: 10793
diff changeset
913 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
914 }
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
915
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
916 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
917 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
918 }
9754
98b004bf3985 Refactoring of NodeUtil to centralize the use of reflection
Christian Wimmer <christian.wimmer@oracle.com>
parents: 9538
diff changeset
919 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
920 }
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
921
11848
099af41815ea Truffle: add NodeUtil.printSourceAttributionTree() for debugging
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 11809
diff changeset
922 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
923 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
924 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
925 return out.toString();
099af41815ea Truffle: add NodeUtil.printSourceAttributionTree() for debugging
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 11809
diff changeset
926 }
099af41815ea Truffle: add NodeUtil.printSourceAttributionTree() for debugging
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 11809
diff changeset
927
099af41815ea Truffle: add NodeUtil.printSourceAttributionTree() for debugging
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 11809
diff changeset
928 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
929 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
930 }
099af41815ea Truffle: add NodeUtil.printSourceAttributionTree() for debugging
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 11809
diff changeset
931
099af41815ea Truffle: add NodeUtil.printSourceAttributionTree() for debugging
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 11809
diff changeset
932 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
933 if (node == null) {
099af41815ea Truffle: add NodeUtil.printSourceAttributionTree() for debugging
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 11809
diff changeset
934 return;
099af41815ea Truffle: add NodeUtil.printSourceAttributionTree() for debugging
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 11809
diff changeset
935 }
099af41815ea Truffle: add NodeUtil.printSourceAttributionTree() for debugging
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 11809
diff changeset
936 if (parent == null) {
099af41815ea Truffle: add NodeUtil.printSourceAttributionTree() for debugging
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 11809
diff changeset
937 // 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
938 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
939 if (sourceSection != null) {
099af41815ea Truffle: add NodeUtil.printSourceAttributionTree() for debugging
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 11809
diff changeset
940 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
941 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
942 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
943 }
099af41815ea Truffle: add NodeUtil.printSourceAttributionTree() for debugging
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 11809
diff changeset
944 }
099af41815ea Truffle: add NodeUtil.printSourceAttributionTree() for debugging
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 11809
diff changeset
945 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
946 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
947 sb.append("| ");
11848
099af41815ea Truffle: add NodeUtil.printSourceAttributionTree() for debugging
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 11809
diff changeset
948 }
099af41815ea Truffle: add NodeUtil.printSourceAttributionTree() for debugging
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 11809
diff changeset
949
099af41815ea Truffle: add NodeUtil.printSourceAttributionTree() for debugging
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 11809
diff changeset
950 if (parent != null) {
17275
846c059e3ecf Truffle: allow interface types in child fields
Andreas Woess <andreas.woess@jku.at>
parents: 16779
diff changeset
951 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
952 }
099af41815ea Truffle: add NodeUtil.printSourceAttributionTree() for debugging
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 11809
diff changeset
953
099af41815ea Truffle: add NodeUtil.printSourceAttributionTree() for debugging
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 11809
diff changeset
954 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
955
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
956 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
957
11848
099af41815ea Truffle: add NodeUtil.printSourceAttributionTree() for debugging
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 11809
diff changeset
958 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
959 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
960
099af41815ea Truffle: add NodeUtil.printSourceAttributionTree() for debugging
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 11809
diff changeset
961 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
962 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
963 }
099af41815ea Truffle: add NodeUtil.printSourceAttributionTree() for debugging
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 11809
diff changeset
964 p.flush();
099af41815ea Truffle: add NodeUtil.printSourceAttributionTree() for debugging
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 11809
diff changeset
965 }
099af41815ea Truffle: add NodeUtil.printSourceAttributionTree() for debugging
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 11809
diff changeset
966
17275
846c059e3ecf Truffle: allow interface types in child fields
Andreas Woess <andreas.woess@jku.at>
parents: 16779
diff changeset
967 private static String getNodeFieldName(Node parent, Node node, String defaultName) {
846c059e3ecf Truffle: allow interface types in child fields
Andreas Woess <andreas.woess@jku.at>
parents: 16779
diff changeset
968 NodeField[] fields = NodeClass.get(parent.getClass()).fields;
846c059e3ecf Truffle: allow interface types in child fields
Andreas Woess <andreas.woess@jku.at>
parents: 16779
diff changeset
969 for (NodeField field : fields) {
846c059e3ecf Truffle: allow interface types in child fields
Andreas Woess <andreas.woess@jku.at>
parents: 16779
diff changeset
970 Object value = field.loadValue(parent);
846c059e3ecf Truffle: allow interface types in child fields
Andreas Woess <andreas.woess@jku.at>
parents: 16779
diff changeset
971 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
972 return field.getName();
846c059e3ecf Truffle: allow interface types in child fields
Andreas Woess <andreas.woess@jku.at>
parents: 16779
diff changeset
973 } else if (field.getKind() == NodeFieldKind.CHILDREN) {
846c059e3ecf Truffle: allow interface types in child fields
Andreas Woess <andreas.woess@jku.at>
parents: 16779
diff changeset
974 int index = 0;
846c059e3ecf Truffle: allow interface types in child fields
Andreas Woess <andreas.woess@jku.at>
parents: 16779
diff changeset
975 for (Object arrayNode : (Object[]) value) {
846c059e3ecf Truffle: allow interface types in child fields
Andreas Woess <andreas.woess@jku.at>
parents: 16779
diff changeset
976 if (arrayNode == node) {
846c059e3ecf Truffle: allow interface types in child fields
Andreas Woess <andreas.woess@jku.at>
parents: 16779
diff changeset
977 return field.getName() + "[" + index + "]";
846c059e3ecf Truffle: allow interface types in child fields
Andreas Woess <andreas.woess@jku.at>
parents: 16779
diff changeset
978 }
846c059e3ecf Truffle: allow interface types in child fields
Andreas Woess <andreas.woess@jku.at>
parents: 16779
diff changeset
979 index++;
846c059e3ecf Truffle: allow interface types in child fields
Andreas Woess <andreas.woess@jku.at>
parents: 16779
diff changeset
980 }
846c059e3ecf Truffle: allow interface types in child fields
Andreas Woess <andreas.woess@jku.at>
parents: 16779
diff changeset
981 }
846c059e3ecf Truffle: allow interface types in child fields
Andreas Woess <andreas.woess@jku.at>
parents: 16779
diff changeset
982 }
846c059e3ecf Truffle: allow interface types in child fields
Andreas Woess <andreas.woess@jku.at>
parents: 16779
diff changeset
983 return defaultName;
846c059e3ecf Truffle: allow interface types in child fields
Andreas Woess <andreas.woess@jku.at>
parents: 16779
diff changeset
984 }
846c059e3ecf Truffle: allow interface types in child fields
Andreas Woess <andreas.woess@jku.at>
parents: 16779
diff changeset
985
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
986 /**
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
987 * 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
988 * 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
989 * <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
990 * <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
991 * {@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
992 * <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
993 * <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
994 * </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
995 */
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
996 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
997 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
998 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
999 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
1000 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
1001 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
1002 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
1003 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
1004 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
1005 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
1006 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
1007 }
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
1008 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
1009 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
1010 }
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
1011 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
1012 }
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
1013
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
1014 /**
9754
98b004bf3985 Refactoring of NodeUtil to centralize the use of reflection
Christian Wimmer <christian.wimmer@oracle.com>
parents: 9538
diff changeset
1015 * 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
1016 * 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
1017 *
9754
98b004bf3985 Refactoring of NodeUtil to centralize the use of reflection
Christian Wimmer <christian.wimmer@oracle.com>
parents: 9538
diff changeset
1018 * @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
1019 * @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
1020 */
9754
98b004bf3985 Refactoring of NodeUtil to centralize the use of reflection
Christian Wimmer <christian.wimmer@oracle.com>
parents: 9538
diff changeset
1021 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
1022 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
1023 }
a4b84ba6dc2e Introduction of the Truffle API for efficient implementation of dynamic languages on top of the Graal VM. New projects com.oracle.truffle.api for the API definition and com.oracle.truffle.api.test for API tests and documentation.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
1024
9754
98b004bf3985 Refactoring of NodeUtil to centralize the use of reflection
Christian Wimmer <christian.wimmer@oracle.com>
parents: 9538
diff changeset
1025 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
1026 StringWriter out = new StringWriter();
98b004bf3985 Refactoring of NodeUtil to centralize the use of reflection
Christian Wimmer <christian.wimmer@oracle.com>
parents: 9538
diff changeset
1027 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
1028 return out.toString();
98b004bf3985 Refactoring of NodeUtil to centralize the use of reflection
Christian Wimmer <christian.wimmer@oracle.com>
parents: 9538
diff changeset
1029 }
98b004bf3985 Refactoring of NodeUtil to centralize the use of reflection
Christian Wimmer <christian.wimmer@oracle.com>
parents: 9538
diff changeset
1030
98b004bf3985 Refactoring of NodeUtil to centralize the use of reflection
Christian Wimmer <christian.wimmer@oracle.com>
parents: 9538
diff changeset
1031 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
1032 printTree(p, node, 1);
98b004bf3985 Refactoring of NodeUtil to centralize the use of reflection
Christian Wimmer <christian.wimmer@oracle.com>
parents: 9538
diff changeset
1033 p.println();
98b004bf3985 Refactoring of NodeUtil to centralize the use of reflection
Christian Wimmer <christian.wimmer@oracle.com>
parents: 9538
diff changeset
1034 p.flush();
98b004bf3985 Refactoring of NodeUtil to centralize the use of reflection
Christian Wimmer <christian.wimmer@oracle.com>
parents: 9538
diff changeset
1035 }
98b004bf3985 Refactoring of NodeUtil to centralize the use of reflection
Christian Wimmer <christian.wimmer@oracle.com>
parents: 9538
diff changeset
1036
98b004bf3985 Refactoring of NodeUtil to centralize the use of reflection
Christian Wimmer <christian.wimmer@oracle.com>
parents: 9538
diff changeset
1037 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
1038 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
1039 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
1040 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
1041 }
a4b84ba6dc2e Introduction of the Truffle API for efficient implementation of dynamic languages on top of the Graal VM. New projects com.oracle.truffle.api for the API definition and com.oracle.truffle.api.test for API tests and documentation.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
1042
10845
e9248ebb1d79 Truffle: ignore null children in NodeUtil.findNodeChildren; refactoring.
Andreas Woess <andreas.woess@jku.at>
parents: 10793
diff changeset
1043 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
1044
9754
98b004bf3985 Refactoring of NodeUtil to centralize the use of reflection
Christian Wimmer <christian.wimmer@oracle.com>
parents: 9538
diff changeset
1045 ArrayList<NodeField> childFields = new ArrayList<>();
98b004bf3985 Refactoring of NodeUtil to centralize the use of reflection
Christian Wimmer <christian.wimmer@oracle.com>
parents: 9538
diff changeset
1046 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
1047 p.print("(");
9754
98b004bf3985 Refactoring of NodeUtil to centralize the use of reflection
Christian Wimmer <christian.wimmer@oracle.com>
parents: 9538
diff changeset
1048 for (NodeField field : NodeClass.get(node.getClass()).fields) {
98b004bf3985 Refactoring of NodeUtil to centralize the use of reflection
Christian Wimmer <christian.wimmer@oracle.com>
parents: 9538
diff changeset
1049 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
1050 childFields.add(field);
98b004bf3985 Refactoring of NodeUtil to centralize the use of reflection
Christian Wimmer <christian.wimmer@oracle.com>
parents: 9538
diff changeset
1051 } 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
1052 p.print(sep);
98b004bf3985 Refactoring of NodeUtil to centralize the use of reflection
Christian Wimmer <christian.wimmer@oracle.com>
parents: 9538
diff changeset
1053 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
1054
9754
98b004bf3985 Refactoring of NodeUtil to centralize the use of reflection
Christian Wimmer <christian.wimmer@oracle.com>
parents: 9538
diff changeset
1055 p.print(field.getName());
98b004bf3985 Refactoring of NodeUtil to centralize the use of reflection
Christian Wimmer <christian.wimmer@oracle.com>
parents: 9538
diff changeset
1056 p.print(" = ");
98b004bf3985 Refactoring of NodeUtil to centralize the use of reflection
Christian Wimmer <christian.wimmer@oracle.com>
parents: 9538
diff changeset
1057 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
1058 }
a4b84ba6dc2e Introduction of the Truffle API for efficient implementation of dynamic languages on top of the Graal VM. New projects com.oracle.truffle.api for the API definition and com.oracle.truffle.api.test for API tests and documentation.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
1059 }
a4b84ba6dc2e Introduction of the Truffle API for efficient implementation of dynamic languages on top of the Graal VM. New projects com.oracle.truffle.api for the API definition and com.oracle.truffle.api.test for API tests and documentation.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
1060 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
1061
a4b84ba6dc2e Introduction of the Truffle API for efficient implementation of dynamic languages on top of the Graal VM. New projects com.oracle.truffle.api for the API definition and com.oracle.truffle.api.test for API tests and documentation.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
1062 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
1063 p.print(" {");
9754
98b004bf3985 Refactoring of NodeUtil to centralize the use of reflection
Christian Wimmer <christian.wimmer@oracle.com>
parents: 9538
diff changeset
1064 for (NodeField field : childFields) {
98b004bf3985 Refactoring of NodeUtil to centralize the use of reflection
Christian Wimmer <christian.wimmer@oracle.com>
parents: 9538
diff changeset
1065 printNewLine(p, level);
98b004bf3985 Refactoring of NodeUtil to centralize the use of reflection
Christian Wimmer <christian.wimmer@oracle.com>
parents: 9538
diff changeset
1066 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
1067
9754
98b004bf3985 Refactoring of NodeUtil to centralize the use of reflection
Christian Wimmer <christian.wimmer@oracle.com>
parents: 9538
diff changeset
1068 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
1069 if (value == null) {
98b004bf3985 Refactoring of NodeUtil to centralize the use of reflection
Christian Wimmer <christian.wimmer@oracle.com>
parents: 9538
diff changeset
1070 p.print(" = null ");
98b004bf3985 Refactoring of NodeUtil to centralize the use of reflection
Christian Wimmer <christian.wimmer@oracle.com>
parents: 9538
diff changeset
1071 } 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
1072 p.print(" = ");
98b004bf3985 Refactoring of NodeUtil to centralize the use of reflection
Christian Wimmer <christian.wimmer@oracle.com>
parents: 9538
diff changeset
1073 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
1074 } else if (field.getKind() == NodeFieldKind.CHILDREN) {
16548
a3b0a2d61e62 extract method in NodeUtil
Christian Wirth <christian.wirth@oracle.com>
parents: 16513
diff changeset
1075 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
1076 }
a4b84ba6dc2e Introduction of the Truffle API for efficient implementation of dynamic languages on top of the Graal VM. New projects com.oracle.truffle.api for the API definition and com.oracle.truffle.api.test for API tests and documentation.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
1077 }
a4b84ba6dc2e Introduction of the Truffle API for efficient implementation of dynamic languages on top of the Graal VM. New projects com.oracle.truffle.api for the API definition and com.oracle.truffle.api.test for API tests and documentation.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
1078 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
1079 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
1080 }
a4b84ba6dc2e Introduction of the Truffle API for efficient implementation of dynamic languages on top of the Graal VM. New projects com.oracle.truffle.api for the API definition and com.oracle.truffle.api.test for API tests and documentation.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
1081 }
a4b84ba6dc2e Introduction of the Truffle API for efficient implementation of dynamic languages on top of the Graal VM. New projects com.oracle.truffle.api for the API definition and com.oracle.truffle.api.test for API tests and documentation.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
1082
16548
a3b0a2d61e62 extract method in NodeUtil
Christian Wirth <christian.wirth@oracle.com>
parents: 16513
diff changeset
1083 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
1084 String sep;
17275
846c059e3ecf Truffle: allow interface types in child fields
Andreas Woess <andreas.woess@jku.at>
parents: 16779
diff changeset
1085 Object[] children = (Object[]) value;
16548
a3b0a2d61e62 extract method in NodeUtil
Christian Wirth <christian.wirth@oracle.com>
parents: 16513
diff changeset
1086 p.print(" = [");
a3b0a2d61e62 extract method in NodeUtil
Christian Wirth <christian.wirth@oracle.com>
parents: 16513
diff changeset
1087 sep = "";
17275
846c059e3ecf Truffle: allow interface types in child fields
Andreas Woess <andreas.woess@jku.at>
parents: 16779
diff changeset
1088 for (Object child : children) {
16548
a3b0a2d61e62 extract method in NodeUtil
Christian Wirth <christian.wirth@oracle.com>
parents: 16513
diff changeset
1089 p.print(sep);
a3b0a2d61e62 extract method in NodeUtil
Christian Wirth <christian.wirth@oracle.com>
parents: 16513
diff changeset
1090 sep = ", ";
17275
846c059e3ecf Truffle: allow interface types in child fields
Andreas Woess <andreas.woess@jku.at>
parents: 16779
diff changeset
1091 printTree(p, (Node) child, level + 1);
16548
a3b0a2d61e62 extract method in NodeUtil
Christian Wirth <christian.wirth@oracle.com>
parents: 16513
diff changeset
1092 }
a3b0a2d61e62 extract method in NodeUtil
Christian Wirth <christian.wirth@oracle.com>
parents: 16513
diff changeset
1093 p.print("]");
a3b0a2d61e62 extract method in NodeUtil
Christian Wirth <christian.wirth@oracle.com>
parents: 16513
diff changeset
1094 }
a3b0a2d61e62 extract method in NodeUtil
Christian Wirth <christian.wirth@oracle.com>
parents: 16513
diff changeset
1095
9754
98b004bf3985 Refactoring of NodeUtil to centralize the use of reflection
Christian Wimmer <christian.wimmer@oracle.com>
parents: 9538
diff changeset
1096 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
1097 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
1098 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
1099 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
1100 }
a4b84ba6dc2e Introduction of the Truffle API for efficient implementation of dynamic languages on top of the Graal VM. New projects com.oracle.truffle.api for the API definition and com.oracle.truffle.api.test for API tests and documentation.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
1101 }
10742
99789440ce28 Truffle: Added assertions to replaceChild.
Christian Humer <christian.humer@gmail.com>
parents: 10704
diff changeset
1102
10845
e9248ebb1d79 Truffle: ignore null children in NodeUtil.findNodeChildren; refactoring.
Andreas Woess <andreas.woess@jku.at>
parents: 10793
diff changeset
1103 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
1104 return node.getClass().getSimpleName();
e9248ebb1d79 Truffle: ignore null children in NodeUtil.findNodeChildren; refactoring.
Andreas Woess <andreas.woess@jku.at>
parents: 10793
diff changeset
1105 }
11848
099af41815ea Truffle: add NodeUtil.printSourceAttributionTree() for debugging
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 11809
diff changeset
1106
099af41815ea Truffle: add NodeUtil.printSourceAttributionTree() for debugging
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 11809
diff changeset
1107 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
1108 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
1109 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
1110 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
1111 }
11848
099af41815ea Truffle: add NodeUtil.printSourceAttributionTree() for debugging
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 11809
diff changeset
1112 if (section != null) {
099af41815ea Truffle: add NodeUtil.printSourceAttributionTree() for debugging
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 11809
diff changeset
1113 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
1114 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
1115 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
1116 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
1117 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
1118 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
1119 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
1120 return sb.toString();
099af41815ea Truffle: add NodeUtil.printSourceAttributionTree() for debugging
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 11809
diff changeset
1121 }
099af41815ea Truffle: add NodeUtil.printSourceAttributionTree() for debugging
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 11809
diff changeset
1122 return "";
099af41815ea Truffle: add NodeUtil.printSourceAttributionTree() for debugging
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 11809
diff changeset
1123 }
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
1124
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
1125 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
1126 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
1127 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
1128 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
1129 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
1130 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
1131 }
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
1132 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
1133 }
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
1134 }
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
1135 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
1136 }
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
1137
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
1138 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
1139 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
1140 }
16464
76081918079d Truffle: move TraceRewrites code to NodeUtil
Andreas Woess <andreas.woess@jku.at>
parents: 16067
diff changeset
1141
76081918079d Truffle: move TraceRewrites code to NodeUtil
Andreas Woess <andreas.woess@jku.at>
parents: 16067
diff changeset
1142 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
1143 if (TruffleOptions.TraceRewritesFilterFromCost != null) {
76081918079d Truffle: move TraceRewrites code to NodeUtil
Andreas Woess <andreas.woess@jku.at>
parents: 16067
diff changeset
1144 if (filterByKind(oldNode, TruffleOptions.TraceRewritesFilterFromCost)) {
76081918079d Truffle: move TraceRewrites code to NodeUtil
Andreas Woess <andreas.woess@jku.at>
parents: 16067
diff changeset
1145 return;
76081918079d Truffle: move TraceRewrites code to NodeUtil
Andreas Woess <andreas.woess@jku.at>
parents: 16067
diff changeset
1146 }
76081918079d Truffle: move TraceRewrites code to NodeUtil
Andreas Woess <andreas.woess@jku.at>
parents: 16067
diff changeset
1147 }
76081918079d Truffle: move TraceRewrites code to NodeUtil
Andreas Woess <andreas.woess@jku.at>
parents: 16067
diff changeset
1148
76081918079d Truffle: move TraceRewrites code to NodeUtil
Andreas Woess <andreas.woess@jku.at>
parents: 16067
diff changeset
1149 if (TruffleOptions.TraceRewritesFilterToCost != null) {
76081918079d Truffle: move TraceRewrites code to NodeUtil
Andreas Woess <andreas.woess@jku.at>
parents: 16067
diff changeset
1150 if (filterByKind(newNode, TruffleOptions.TraceRewritesFilterToCost)) {
76081918079d Truffle: move TraceRewrites code to NodeUtil
Andreas Woess <andreas.woess@jku.at>
parents: 16067
diff changeset
1151 return;
76081918079d Truffle: move TraceRewrites code to NodeUtil
Andreas Woess <andreas.woess@jku.at>
parents: 16067
diff changeset
1152 }
76081918079d Truffle: move TraceRewrites code to NodeUtil
Andreas Woess <andreas.woess@jku.at>
parents: 16067
diff changeset
1153 }
76081918079d Truffle: move TraceRewrites code to NodeUtil
Andreas Woess <andreas.woess@jku.at>
parents: 16067
diff changeset
1154
76081918079d Truffle: move TraceRewrites code to NodeUtil
Andreas Woess <andreas.woess@jku.at>
parents: 16067
diff changeset
1155 String filter = TruffleOptions.TraceRewritesFilterClass;
76081918079d Truffle: move TraceRewrites code to NodeUtil
Andreas Woess <andreas.woess@jku.at>
parents: 16067
diff changeset
1156 Class<? extends Node> from = oldNode.getClass();
76081918079d Truffle: move TraceRewrites code to NodeUtil
Andreas Woess <andreas.woess@jku.at>
parents: 16067
diff changeset
1157 Class<? extends Node> to = newNode.getClass();
76081918079d Truffle: move TraceRewrites code to NodeUtil
Andreas Woess <andreas.woess@jku.at>
parents: 16067
diff changeset
1158 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
1159 return;
76081918079d Truffle: move TraceRewrites code to NodeUtil
Andreas Woess <andreas.woess@jku.at>
parents: 16067
diff changeset
1160 }
76081918079d Truffle: move TraceRewrites code to NodeUtil
Andreas Woess <andreas.woess@jku.at>
parents: 16067
diff changeset
1161
76081918079d Truffle: move TraceRewrites code to NodeUtil
Andreas Woess <andreas.woess@jku.at>
parents: 16067
diff changeset
1162 final SourceSection reportedSourceSection = oldNode.getEncapsulatingSourceSection();
76081918079d Truffle: move TraceRewrites code to NodeUtil
Andreas Woess <andreas.woess@jku.at>
parents: 16067
diff changeset
1163
76081918079d Truffle: move TraceRewrites code to NodeUtil
Andreas Woess <andreas.woess@jku.at>
parents: 16067
diff changeset
1164 PrintStream out = System.out;
76081918079d Truffle: move TraceRewrites code to NodeUtil
Andreas Woess <andreas.woess@jku.at>
parents: 16067
diff changeset
1165 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
1166 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
1167 }
76081918079d Truffle: move TraceRewrites code to NodeUtil
Andreas Woess <andreas.woess@jku.at>
parents: 16067
diff changeset
1168
76081918079d Truffle: move TraceRewrites code to NodeUtil
Andreas Woess <andreas.woess@jku.at>
parents: 16067
diff changeset
1169 private static String formatNodeInfo(Node node) {
76081918079d Truffle: move TraceRewrites code to NodeUtil
Andreas Woess <andreas.woess@jku.at>
parents: 16067
diff changeset
1170 String cost = "?";
76081918079d Truffle: move TraceRewrites code to NodeUtil
Andreas Woess <andreas.woess@jku.at>
parents: 16067
diff changeset
1171 switch (node.getCost()) {
76081918079d Truffle: move TraceRewrites code to NodeUtil
Andreas Woess <andreas.woess@jku.at>
parents: 16067
diff changeset
1172 case NONE:
76081918079d Truffle: move TraceRewrites code to NodeUtil
Andreas Woess <andreas.woess@jku.at>
parents: 16067
diff changeset
1173 cost = "G";
76081918079d Truffle: move TraceRewrites code to NodeUtil
Andreas Woess <andreas.woess@jku.at>
parents: 16067
diff changeset
1174 break;
76081918079d Truffle: move TraceRewrites code to NodeUtil
Andreas Woess <andreas.woess@jku.at>
parents: 16067
diff changeset
1175 case MONOMORPHIC:
76081918079d Truffle: move TraceRewrites code to NodeUtil
Andreas Woess <andreas.woess@jku.at>
parents: 16067
diff changeset
1176 cost = "M";
76081918079d Truffle: move TraceRewrites code to NodeUtil
Andreas Woess <andreas.woess@jku.at>
parents: 16067
diff changeset
1177 break;
76081918079d Truffle: move TraceRewrites code to NodeUtil
Andreas Woess <andreas.woess@jku.at>
parents: 16067
diff changeset
1178 case POLYMORPHIC:
76081918079d Truffle: move TraceRewrites code to NodeUtil
Andreas Woess <andreas.woess@jku.at>
parents: 16067
diff changeset
1179 cost = "P";
76081918079d Truffle: move TraceRewrites code to NodeUtil
Andreas Woess <andreas.woess@jku.at>
parents: 16067
diff changeset
1180 break;
76081918079d Truffle: move TraceRewrites code to NodeUtil
Andreas Woess <andreas.woess@jku.at>
parents: 16067
diff changeset
1181 case MEGAMORPHIC:
76081918079d Truffle: move TraceRewrites code to NodeUtil
Andreas Woess <andreas.woess@jku.at>
parents: 16067
diff changeset
1182 cost = "G";
76081918079d Truffle: move TraceRewrites code to NodeUtil
Andreas Woess <andreas.woess@jku.at>
parents: 16067
diff changeset
1183 break;
76081918079d Truffle: move TraceRewrites code to NodeUtil
Andreas Woess <andreas.woess@jku.at>
parents: 16067
diff changeset
1184 default:
76081918079d Truffle: move TraceRewrites code to NodeUtil
Andreas Woess <andreas.woess@jku.at>
parents: 16067
diff changeset
1185 cost = "?";
76081918079d Truffle: move TraceRewrites code to NodeUtil
Andreas Woess <andreas.woess@jku.at>
parents: 16067
diff changeset
1186 break;
76081918079d Truffle: move TraceRewrites code to NodeUtil
Andreas Woess <andreas.woess@jku.at>
parents: 16067
diff changeset
1187 }
76081918079d Truffle: move TraceRewrites code to NodeUtil
Andreas Woess <andreas.woess@jku.at>
parents: 16067
diff changeset
1188 return cost + " " + node.getClass().getSimpleName();
76081918079d Truffle: move TraceRewrites code to NodeUtil
Andreas Woess <andreas.woess@jku.at>
parents: 16067
diff changeset
1189 }
76081918079d Truffle: move TraceRewrites code to NodeUtil
Andreas Woess <andreas.woess@jku.at>
parents: 16067
diff changeset
1190
76081918079d Truffle: move TraceRewrites code to NodeUtil
Andreas Woess <andreas.woess@jku.at>
parents: 16067
diff changeset
1191 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
1192 return node.getCost() == cost;
76081918079d Truffle: move TraceRewrites code to NodeUtil
Andreas Woess <andreas.woess@jku.at>
parents: 16067
diff changeset
1193 }
76081918079d Truffle: move TraceRewrites code to NodeUtil
Andreas Woess <andreas.woess@jku.at>
parents: 16067
diff changeset
1194
76081918079d Truffle: move TraceRewrites code to NodeUtil
Andreas Woess <andreas.woess@jku.at>
parents: 16067
diff changeset
1195 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
1196 Class<?> currentFrom = from;
76081918079d Truffle: move TraceRewrites code to NodeUtil
Andreas Woess <andreas.woess@jku.at>
parents: 16067
diff changeset
1197 while (currentFrom != null) {
76081918079d Truffle: move TraceRewrites code to NodeUtil
Andreas Woess <andreas.woess@jku.at>
parents: 16067
diff changeset
1198 if (currentFrom.getName().contains(filter)) {
76081918079d Truffle: move TraceRewrites code to NodeUtil
Andreas Woess <andreas.woess@jku.at>
parents: 16067
diff changeset
1199 return false;
76081918079d Truffle: move TraceRewrites code to NodeUtil
Andreas Woess <andreas.woess@jku.at>
parents: 16067
diff changeset
1200 }
76081918079d Truffle: move TraceRewrites code to NodeUtil
Andreas Woess <andreas.woess@jku.at>
parents: 16067
diff changeset
1201 currentFrom = currentFrom.getSuperclass();
76081918079d Truffle: move TraceRewrites code to NodeUtil
Andreas Woess <andreas.woess@jku.at>
parents: 16067
diff changeset
1202 }
76081918079d Truffle: move TraceRewrites code to NodeUtil
Andreas Woess <andreas.woess@jku.at>
parents: 16067
diff changeset
1203 return true;
76081918079d Truffle: move TraceRewrites code to NodeUtil
Andreas Woess <andreas.woess@jku.at>
parents: 16067
diff changeset
1204 }
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
1205 }