annotate graal/com.oracle.truffle.api/src/com/oracle/truffle/api/nodes/NodeUtil.java @ 18510:cb4d5cc2b52b

Truffle: clone ConditionProfile and BranchProfile node fields
author Andreas Woess <andreas.woess@jku.at>
date Tue, 25 Nov 2014 13:21:50 +0100
parents e3c95cbbb50c
children 8a758dce7d80
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 /*
16511
aee02665e505 Truffle: NodeUtil fix for displaying null SourceSections.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16067
diff changeset
2 * Copyright (c) 2012, 2014, 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 {
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
46
9754
98b004bf3985 Refactoring of NodeUtil to centralize the use of reflection
Christian Wimmer <christian.wimmer@oracle.com>
parents: 9538
diff changeset
47 /**
98b004bf3985 Refactoring of NodeUtil to centralize the use of reflection
Christian Wimmer <christian.wimmer@oracle.com>
parents: 9538
diff changeset
48 * 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
49 * accesses.
98b004bf3985 Refactoring of NodeUtil to centralize the use of reflection
Christian Wimmer <christian.wimmer@oracle.com>
parents: 9538
diff changeset
50 */
98b004bf3985 Refactoring of NodeUtil to centralize the use of reflection
Christian Wimmer <christian.wimmer@oracle.com>
parents: 9538
diff changeset
51 public interface FieldOffsetProvider {
98b004bf3985 Refactoring of NodeUtil to centralize the use of reflection
Christian Wimmer <christian.wimmer@oracle.com>
parents: 9538
diff changeset
52
98b004bf3985 Refactoring of NodeUtil to centralize the use of reflection
Christian Wimmer <christian.wimmer@oracle.com>
parents: 9538
diff changeset
53 long objectFieldOffset(Field field);
15834
fc6f8d143c68 Introduce method to customize type size
Christian Wimmer <christian.wimmer@oracle.com>
parents: 15093
diff changeset
54
fc6f8d143c68 Introduce method to customize type size
Christian Wimmer <christian.wimmer@oracle.com>
parents: 15093
diff changeset
55 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
56 }
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 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
59
98b004bf3985 Refactoring of NodeUtil to centralize the use of reflection
Christian Wimmer <christian.wimmer@oracle.com>
parents: 9538
diff changeset
60 @Override
98b004bf3985 Refactoring of NodeUtil to centralize the use of reflection
Christian Wimmer <christian.wimmer@oracle.com>
parents: 9538
diff changeset
61 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
62 return unsafe.objectFieldOffset(field);
98b004bf3985 Refactoring of NodeUtil to centralize the use of reflection
Christian Wimmer <christian.wimmer@oracle.com>
parents: 9538
diff changeset
63 }
15834
fc6f8d143c68 Introduce method to customize type size
Christian Wimmer <christian.wimmer@oracle.com>
parents: 15093
diff changeset
64
fc6f8d143c68 Introduce method to customize type size
Christian Wimmer <christian.wimmer@oracle.com>
parents: 15093
diff changeset
65 @Override
fc6f8d143c68 Introduce method to customize type size
Christian Wimmer <christian.wimmer@oracle.com>
parents: 15093
diff changeset
66 public int getTypeSize(Class<?> clazz) {
fc6f8d143c68 Introduce method to customize type size
Christian Wimmer <christian.wimmer@oracle.com>
parents: 15093
diff changeset
67 if (!clazz.isPrimitive()) {
fc6f8d143c68 Introduce method to customize type size
Christian Wimmer <christian.wimmer@oracle.com>
parents: 15093
diff changeset
68 return Unsafe.ARRAY_OBJECT_INDEX_SCALE;
fc6f8d143c68 Introduce method to customize type size
Christian Wimmer <christian.wimmer@oracle.com>
parents: 15093
diff changeset
69 } else if (clazz == int.class) {
fc6f8d143c68 Introduce method to customize type size
Christian Wimmer <christian.wimmer@oracle.com>
parents: 15093
diff changeset
70 return Unsafe.ARRAY_INT_INDEX_SCALE;
fc6f8d143c68 Introduce method to customize type size
Christian Wimmer <christian.wimmer@oracle.com>
parents: 15093
diff changeset
71 } else {
fc6f8d143c68 Introduce method to customize type size
Christian Wimmer <christian.wimmer@oracle.com>
parents: 15093
diff changeset
72 throw new UnsupportedOperationException("unsupported field type: " + clazz);
fc6f8d143c68 Introduce method to customize type size
Christian Wimmer <christian.wimmer@oracle.com>
parents: 15093
diff changeset
73 }
fc6f8d143c68 Introduce method to customize type size
Christian Wimmer <christian.wimmer@oracle.com>
parents: 15093
diff changeset
74 }
9754
98b004bf3985 Refactoring of NodeUtil to centralize the use of reflection
Christian Wimmer <christian.wimmer@oracle.com>
parents: 9538
diff changeset
75 };
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 public static enum NodeFieldKind {
98b004bf3985 Refactoring of NodeUtil to centralize the use of reflection
Christian Wimmer <christian.wimmer@oracle.com>
parents: 9538
diff changeset
78 /** 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
79 PARENT,
98b004bf3985 Refactoring of NodeUtil to centralize the use of reflection
Christian Wimmer <christian.wimmer@oracle.com>
parents: 9538
diff changeset
80 /** 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
81 CHILD,
98b004bf3985 Refactoring of NodeUtil to centralize the use of reflection
Christian Wimmer <christian.wimmer@oracle.com>
parents: 9538
diff changeset
82 /** 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
83 CHILDREN,
98b004bf3985 Refactoring of NodeUtil to centralize the use of reflection
Christian Wimmer <christian.wimmer@oracle.com>
parents: 9538
diff changeset
84 /** 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
85 DATA
98b004bf3985 Refactoring of NodeUtil to centralize the use of reflection
Christian Wimmer <christian.wimmer@oracle.com>
parents: 9538
diff changeset
86 }
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 * 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
90 */
98b004bf3985 Refactoring of NodeUtil to centralize the use of reflection
Christian Wimmer <christian.wimmer@oracle.com>
parents: 9538
diff changeset
91 public static final class NodeField {
98b004bf3985 Refactoring of NodeUtil to centralize the use of reflection
Christian Wimmer <christian.wimmer@oracle.com>
parents: 9538
diff changeset
92
98b004bf3985 Refactoring of NodeUtil to centralize the use of reflection
Christian Wimmer <christian.wimmer@oracle.com>
parents: 9538
diff changeset
93 private final NodeFieldKind kind;
98b004bf3985 Refactoring of NodeUtil to centralize the use of reflection
Christian Wimmer <christian.wimmer@oracle.com>
parents: 9538
diff changeset
94 private final Class<?> type;
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;
98b004bf3985 Refactoring of NodeUtil to centralize the use of reflection
Christian Wimmer <christian.wimmer@oracle.com>
parents: 9538
diff changeset
96 private long offset;
98b004bf3985 Refactoring of NodeUtil to centralize the use of reflection
Christian Wimmer <christian.wimmer@oracle.com>
parents: 9538
diff changeset
97
98b004bf3985 Refactoring of NodeUtil to centralize the use of reflection
Christian Wimmer <christian.wimmer@oracle.com>
parents: 9538
diff changeset
98 protected NodeField(NodeFieldKind kind, Class<?> type, String name, long offset) {
98b004bf3985 Refactoring of NodeUtil to centralize the use of reflection
Christian Wimmer <christian.wimmer@oracle.com>
parents: 9538
diff changeset
99 this.kind = kind;
98b004bf3985 Refactoring of NodeUtil to centralize the use of reflection
Christian Wimmer <christian.wimmer@oracle.com>
parents: 9538
diff changeset
100 this.type = type;
98b004bf3985 Refactoring of NodeUtil to centralize the use of reflection
Christian Wimmer <christian.wimmer@oracle.com>
parents: 9538
diff changeset
101 this.name = name;
98b004bf3985 Refactoring of NodeUtil to centralize the use of reflection
Christian Wimmer <christian.wimmer@oracle.com>
parents: 9538
diff changeset
102 this.offset = offset;
98b004bf3985 Refactoring of NodeUtil to centralize the use of reflection
Christian Wimmer <christian.wimmer@oracle.com>
parents: 9538
diff changeset
103 }
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
104
9754
98b004bf3985 Refactoring of NodeUtil to centralize the use of reflection
Christian Wimmer <christian.wimmer@oracle.com>
parents: 9538
diff changeset
105 public NodeFieldKind getKind() {
98b004bf3985 Refactoring of NodeUtil to centralize the use of reflection
Christian Wimmer <christian.wimmer@oracle.com>
parents: 9538
diff changeset
106 return kind;
98b004bf3985 Refactoring of NodeUtil to centralize the use of reflection
Christian Wimmer <christian.wimmer@oracle.com>
parents: 9538
diff changeset
107 }
98b004bf3985 Refactoring of NodeUtil to centralize the use of reflection
Christian Wimmer <christian.wimmer@oracle.com>
parents: 9538
diff changeset
108
98b004bf3985 Refactoring of NodeUtil to centralize the use of reflection
Christian Wimmer <christian.wimmer@oracle.com>
parents: 9538
diff changeset
109 public Class<?> getType() {
98b004bf3985 Refactoring of NodeUtil to centralize the use of reflection
Christian Wimmer <christian.wimmer@oracle.com>
parents: 9538
diff changeset
110 return type;
98b004bf3985 Refactoring of NodeUtil to centralize the use of reflection
Christian Wimmer <christian.wimmer@oracle.com>
parents: 9538
diff changeset
111 }
98b004bf3985 Refactoring of NodeUtil to centralize the use of reflection
Christian Wimmer <christian.wimmer@oracle.com>
parents: 9538
diff changeset
112
98b004bf3985 Refactoring of NodeUtil to centralize the use of reflection
Christian Wimmer <christian.wimmer@oracle.com>
parents: 9538
diff changeset
113 public String getName() {
98b004bf3985 Refactoring of NodeUtil to centralize the use of reflection
Christian Wimmer <christian.wimmer@oracle.com>
parents: 9538
diff changeset
114 return name;
98b004bf3985 Refactoring of NodeUtil to centralize the use of reflection
Christian Wimmer <christian.wimmer@oracle.com>
parents: 9538
diff changeset
115 }
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 public long getOffset() {
98b004bf3985 Refactoring of NodeUtil to centralize the use of reflection
Christian Wimmer <christian.wimmer@oracle.com>
parents: 9538
diff changeset
118 return offset;
98b004bf3985 Refactoring of NodeUtil to centralize the use of reflection
Christian Wimmer <christian.wimmer@oracle.com>
parents: 9538
diff changeset
119 }
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 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
122 if (type == boolean.class) {
98b004bf3985 Refactoring of NodeUtil to centralize the use of reflection
Christian Wimmer <christian.wimmer@oracle.com>
parents: 9538
diff changeset
123 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
124 } 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
125 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
126 } 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
127 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
128 } 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
129 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
130 } 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
131 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
132 } 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
133 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
134 } 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
135 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
136 } 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
137 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
138 } else {
98b004bf3985 Refactoring of NodeUtil to centralize the use of reflection
Christian Wimmer <christian.wimmer@oracle.com>
parents: 9538
diff changeset
139 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
140 }
98b004bf3985 Refactoring of NodeUtil to centralize the use of reflection
Christian Wimmer <christian.wimmer@oracle.com>
parents: 9538
diff changeset
141 }
14743
762c9aceb7d8 Add equals/hashCode methods
Christian Wimmer <christian.wimmer@oracle.com>
parents: 14569
diff changeset
142
762c9aceb7d8 Add equals/hashCode methods
Christian Wimmer <christian.wimmer@oracle.com>
parents: 14569
diff changeset
143 @Override
762c9aceb7d8 Add equals/hashCode methods
Christian Wimmer <christian.wimmer@oracle.com>
parents: 14569
diff changeset
144 public int hashCode() {
762c9aceb7d8 Add equals/hashCode methods
Christian Wimmer <christian.wimmer@oracle.com>
parents: 14569
diff changeset
145 return kind.hashCode() | type.hashCode() | name.hashCode() | ((Long) offset).hashCode();
762c9aceb7d8 Add equals/hashCode methods
Christian Wimmer <christian.wimmer@oracle.com>
parents: 14569
diff changeset
146 }
762c9aceb7d8 Add equals/hashCode methods
Christian Wimmer <christian.wimmer@oracle.com>
parents: 14569
diff changeset
147
762c9aceb7d8 Add equals/hashCode methods
Christian Wimmer <christian.wimmer@oracle.com>
parents: 14569
diff changeset
148 @Override
762c9aceb7d8 Add equals/hashCode methods
Christian Wimmer <christian.wimmer@oracle.com>
parents: 14569
diff changeset
149 public boolean equals(Object obj) {
762c9aceb7d8 Add equals/hashCode methods
Christian Wimmer <christian.wimmer@oracle.com>
parents: 14569
diff changeset
150 if (obj instanceof NodeField) {
762c9aceb7d8 Add equals/hashCode methods
Christian Wimmer <christian.wimmer@oracle.com>
parents: 14569
diff changeset
151 NodeField other = (NodeField) obj;
762c9aceb7d8 Add equals/hashCode methods
Christian Wimmer <christian.wimmer@oracle.com>
parents: 14569
diff changeset
152 return offset == other.offset && name.equals(other.name) && type.equals(other.type) && kind.equals(other.kind);
762c9aceb7d8 Add equals/hashCode methods
Christian Wimmer <christian.wimmer@oracle.com>
parents: 14569
diff changeset
153 }
762c9aceb7d8 Add equals/hashCode methods
Christian Wimmer <christian.wimmer@oracle.com>
parents: 14569
diff changeset
154 return false;
762c9aceb7d8 Add equals/hashCode methods
Christian Wimmer <christian.wimmer@oracle.com>
parents: 14569
diff changeset
155 }
9754
98b004bf3985 Refactoring of NodeUtil to centralize the use of reflection
Christian Wimmer <christian.wimmer@oracle.com>
parents: 9538
diff changeset
156 }
98b004bf3985 Refactoring of NodeUtil to centralize the use of reflection
Christian Wimmer <christian.wimmer@oracle.com>
parents: 9538
diff changeset
157
98b004bf3985 Refactoring of NodeUtil to centralize the use of reflection
Christian Wimmer <christian.wimmer@oracle.com>
parents: 9538
diff changeset
158 /**
98b004bf3985 Refactoring of NodeUtil to centralize the use of reflection
Christian Wimmer <christian.wimmer@oracle.com>
parents: 9538
diff changeset
159 * 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
160 * 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
161 */
98b004bf3985 Refactoring of NodeUtil to centralize the use of reflection
Christian Wimmer <christian.wimmer@oracle.com>
parents: 9538
diff changeset
162 public static final class NodeClass {
16466
d41922beb512 Truffle: use ClassValue for NodeClass
Andreas Woess <andreas.woess@jku.at>
parents: 16465
diff changeset
163 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
164 @SuppressWarnings("unchecked")
d41922beb512 Truffle: use ClassValue for NodeClass
Andreas Woess <andreas.woess@jku.at>
parents: 16465
diff changeset
165 @Override
16779
e52ad0d3b7d6 put some security sensitive actions into a privileged action
Doug Simon <doug.simon@oracle.com>
parents: 16674
diff changeset
166 protected NodeClass computeValue(final Class<?> clazz) {
16466
d41922beb512 Truffle: use ClassValue for NodeClass
Andreas Woess <andreas.woess@jku.at>
parents: 16465
diff changeset
167 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
168 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
169 public NodeClass run() {
e52ad0d3b7d6 put some security sensitive actions into a privileged action
Doug Simon <doug.simon@oracle.com>
parents: 16674
diff changeset
170 return new NodeClass((Class<? extends Node>) clazz, unsafeFieldOffsetProvider);
e52ad0d3b7d6 put some security sensitive actions into a privileged action
Doug Simon <doug.simon@oracle.com>
parents: 16674
diff changeset
171 }
e52ad0d3b7d6 put some security sensitive actions into a privileged action
Doug Simon <doug.simon@oracle.com>
parents: 16674
diff changeset
172 });
16466
d41922beb512 Truffle: use ClassValue for NodeClass
Andreas Woess <andreas.woess@jku.at>
parents: 16465
diff changeset
173 }
d41922beb512 Truffle: use ClassValue for NodeClass
Andreas Woess <andreas.woess@jku.at>
parents: 16465
diff changeset
174 };
7267
a4b84ba6dc2e Introduction of the Truffle API for efficient implementation of dynamic languages on top of the Graal VM. New projects com.oracle.truffle.api for the API definition and com.oracle.truffle.api.test for API tests and documentation.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
175
9754
98b004bf3985 Refactoring of NodeUtil to centralize the use of reflection
Christian Wimmer <christian.wimmer@oracle.com>
parents: 9538
diff changeset
176 // 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
177 private final NodeField[] fields;
98b004bf3985 Refactoring of NodeUtil to centralize the use of reflection
Christian Wimmer <christian.wimmer@oracle.com>
parents: 9538
diff changeset
178 // Separate arrays for the frequently accessed field offsets.
98b004bf3985 Refactoring of NodeUtil to centralize the use of reflection
Christian Wimmer <christian.wimmer@oracle.com>
parents: 9538
diff changeset
179 private final long parentOffset;
98b004bf3985 Refactoring of NodeUtil to centralize the use of reflection
Christian Wimmer <christian.wimmer@oracle.com>
parents: 9538
diff changeset
180 private final long[] childOffsets;
98b004bf3985 Refactoring of NodeUtil to centralize the use of reflection
Christian Wimmer <christian.wimmer@oracle.com>
parents: 9538
diff changeset
181 private final long[] childrenOffsets;
18510
cb4d5cc2b52b Truffle: clone ConditionProfile and BranchProfile node fields
Andreas Woess <andreas.woess@jku.at>
parents: 18485
diff changeset
182 private final long[] cloneableOffsets;
16467
17f7331dcc4f Truffle: move iterator to NodeClass
Andreas Woess <andreas.woess@jku.at>
parents: 16466
diff changeset
183 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
184
98b004bf3985 Refactoring of NodeUtil to centralize the use of reflection
Christian Wimmer <christian.wimmer@oracle.com>
parents: 9538
diff changeset
185 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
186 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
187 }
a4b84ba6dc2e Introduction of the Truffle API for efficient implementation of dynamic languages on top of the Graal VM. New projects com.oracle.truffle.api for the API definition and com.oracle.truffle.api.test for API tests and documentation.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
188
9754
98b004bf3985 Refactoring of NodeUtil to centralize the use of reflection
Christian Wimmer <christian.wimmer@oracle.com>
parents: 9538
diff changeset
189 public NodeClass(Class<? extends Node> clazz, FieldOffsetProvider fieldOffsetProvider) {
98b004bf3985 Refactoring of NodeUtil to centralize the use of reflection
Christian Wimmer <christian.wimmer@oracle.com>
parents: 9538
diff changeset
190 List<NodeField> fieldsList = new ArrayList<>();
17275
846c059e3ecf Truffle: allow interface types in child fields
Andreas Woess <andreas.woess@jku.at>
parents: 16779
diff changeset
191 long parentFieldOffset = -1;
9754
98b004bf3985 Refactoring of NodeUtil to centralize the use of reflection
Christian Wimmer <christian.wimmer@oracle.com>
parents: 9538
diff changeset
192 List<Long> childOffsetsList = new ArrayList<>();
98b004bf3985 Refactoring of NodeUtil to centralize the use of reflection
Christian Wimmer <christian.wimmer@oracle.com>
parents: 9538
diff changeset
193 List<Long> childrenOffsetsList = new ArrayList<>();
18510
cb4d5cc2b52b Truffle: clone ConditionProfile and BranchProfile node fields
Andreas Woess <andreas.woess@jku.at>
parents: 18485
diff changeset
194 List<Long> cloneableOffsetsList = new ArrayList<>();
9754
98b004bf3985 Refactoring of NodeUtil to centralize the use of reflection
Christian Wimmer <christian.wimmer@oracle.com>
parents: 9538
diff changeset
195
98b004bf3985 Refactoring of NodeUtil to centralize the use of reflection
Christian Wimmer <christian.wimmer@oracle.com>
parents: 9538
diff changeset
196 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
197 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
198 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
199 }
a4b84ba6dc2e Introduction of the Truffle API for efficient implementation of dynamic languages on top of the Graal VM. New projects com.oracle.truffle.api for the API definition and com.oracle.truffle.api.test for API tests and documentation.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
200
9754
98b004bf3985 Refactoring of NodeUtil to centralize the use of reflection
Christian Wimmer <christian.wimmer@oracle.com>
parents: 9538
diff changeset
201 NodeFieldKind kind;
17275
846c059e3ecf Truffle: allow interface types in child fields
Andreas Woess <andreas.woess@jku.at>
parents: 16779
diff changeset
202 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
203 assert Node.class.isAssignableFrom(field.getType());
9754
98b004bf3985 Refactoring of NodeUtil to centralize the use of reflection
Christian Wimmer <christian.wimmer@oracle.com>
parents: 9538
diff changeset
204 kind = NodeFieldKind.PARENT;
17275
846c059e3ecf Truffle: allow interface types in child fields
Andreas Woess <andreas.woess@jku.at>
parents: 16779
diff changeset
205 parentFieldOffset = fieldOffsetProvider.objectFieldOffset(field);
846c059e3ecf Truffle: allow interface types in child fields
Andreas Woess <andreas.woess@jku.at>
parents: 16779
diff changeset
206 } 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
207 checkChildField(field);
9754
98b004bf3985 Refactoring of NodeUtil to centralize the use of reflection
Christian Wimmer <christian.wimmer@oracle.com>
parents: 9538
diff changeset
208 kind = NodeFieldKind.CHILD;
98b004bf3985 Refactoring of NodeUtil to centralize the use of reflection
Christian Wimmer <christian.wimmer@oracle.com>
parents: 9538
diff changeset
209 childOffsetsList.add(fieldOffsetProvider.objectFieldOffset(field));
17275
846c059e3ecf Truffle: allow interface types in child fields
Andreas Woess <andreas.woess@jku.at>
parents: 16779
diff changeset
210 } 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
211 checkChildrenField(field);
9754
98b004bf3985 Refactoring of NodeUtil to centralize the use of reflection
Christian Wimmer <christian.wimmer@oracle.com>
parents: 9538
diff changeset
212 kind = NodeFieldKind.CHILDREN;
98b004bf3985 Refactoring of NodeUtil to centralize the use of reflection
Christian Wimmer <christian.wimmer@oracle.com>
parents: 9538
diff changeset
213 childrenOffsetsList.add(fieldOffsetProvider.objectFieldOffset(field));
18510
cb4d5cc2b52b Truffle: clone ConditionProfile and BranchProfile node fields
Andreas Woess <andreas.woess@jku.at>
parents: 18485
diff changeset
214 } else if (NodeCloneable.class.isAssignableFrom(field.getType())) {
cb4d5cc2b52b Truffle: clone ConditionProfile and BranchProfile node fields
Andreas Woess <andreas.woess@jku.at>
parents: 18485
diff changeset
215 kind = NodeFieldKind.DATA;
cb4d5cc2b52b Truffle: clone ConditionProfile and BranchProfile node fields
Andreas Woess <andreas.woess@jku.at>
parents: 18485
diff changeset
216 cloneableOffsetsList.add(fieldOffsetProvider.objectFieldOffset(field));
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
217 } else {
9754
98b004bf3985 Refactoring of NodeUtil to centralize the use of reflection
Christian Wimmer <christian.wimmer@oracle.com>
parents: 9538
diff changeset
218 kind = NodeFieldKind.DATA;
7267
a4b84ba6dc2e Introduction of the Truffle API for efficient implementation of dynamic languages on top of the Graal VM. New projects com.oracle.truffle.api for the API definition and com.oracle.truffle.api.test for API tests and documentation.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
219 }
9754
98b004bf3985 Refactoring of NodeUtil to centralize the use of reflection
Christian Wimmer <christian.wimmer@oracle.com>
parents: 9538
diff changeset
220 fieldsList.add(new NodeField(kind, field.getType(), field.getName(), fieldOffsetProvider.objectFieldOffset(field)));
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
221 }
17275
846c059e3ecf Truffle: allow interface types in child fields
Andreas Woess <andreas.woess@jku.at>
parents: 16779
diff changeset
222
846c059e3ecf Truffle: allow interface types in child fields
Andreas Woess <andreas.woess@jku.at>
parents: 16779
diff changeset
223 if (parentFieldOffset < 0) {
846c059e3ecf Truffle: allow interface types in child fields
Andreas Woess <andreas.woess@jku.at>
parents: 16779
diff changeset
224 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
225 }
846c059e3ecf Truffle: allow interface types in child fields
Andreas Woess <andreas.woess@jku.at>
parents: 16779
diff changeset
226
9754
98b004bf3985 Refactoring of NodeUtil to centralize the use of reflection
Christian Wimmer <christian.wimmer@oracle.com>
parents: 9538
diff changeset
227 this.fields = fieldsList.toArray(new NodeField[fieldsList.size()]);
17275
846c059e3ecf Truffle: allow interface types in child fields
Andreas Woess <andreas.woess@jku.at>
parents: 16779
diff changeset
228 this.parentOffset = parentFieldOffset;
9754
98b004bf3985 Refactoring of NodeUtil to centralize the use of reflection
Christian Wimmer <christian.wimmer@oracle.com>
parents: 9538
diff changeset
229 this.childOffsets = toLongArray(childOffsetsList);
98b004bf3985 Refactoring of NodeUtil to centralize the use of reflection
Christian Wimmer <christian.wimmer@oracle.com>
parents: 9538
diff changeset
230 this.childrenOffsets = toLongArray(childrenOffsetsList);
18510
cb4d5cc2b52b Truffle: clone ConditionProfile and BranchProfile node fields
Andreas Woess <andreas.woess@jku.at>
parents: 18485
diff changeset
231 this.cloneableOffsets = toLongArray(cloneableOffsetsList);
16467
17f7331dcc4f Truffle: move iterator to NodeClass
Andreas Woess <andreas.woess@jku.at>
parents: 16466
diff changeset
232 this.clazz = clazz;
9754
98b004bf3985 Refactoring of NodeUtil to centralize the use of reflection
Christian Wimmer <christian.wimmer@oracle.com>
parents: 9538
diff changeset
233 }
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
234
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
235 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
236 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
237 }
65a160d9d259 Truffle: add NodeInterface and require that all child fields be of this type
Andreas Woess <andreas.woess@jku.at>
parents: 18163
diff changeset
238
17275
846c059e3ecf Truffle: allow interface types in child fields
Andreas Woess <andreas.woess@jku.at>
parents: 16779
diff changeset
239 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
240 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
241 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
242 }
846c059e3ecf Truffle: allow interface types in child fields
Andreas Woess <andreas.woess@jku.at>
parents: 16779
diff changeset
243 if (Modifier.isFinal(field.getModifiers())) {
846c059e3ecf Truffle: allow interface types in child fields
Andreas Woess <andreas.woess@jku.at>
parents: 16779
diff changeset
244 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
245 }
846c059e3ecf Truffle: allow interface types in child fields
Andreas Woess <andreas.woess@jku.at>
parents: 16779
diff changeset
246 }
846c059e3ecf Truffle: allow interface types in child fields
Andreas Woess <andreas.woess@jku.at>
parents: 16779
diff changeset
247
846c059e3ecf Truffle: allow interface types in child fields
Andreas Woess <andreas.woess@jku.at>
parents: 16779
diff changeset
248 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
249 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
250 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
251 }
846c059e3ecf Truffle: allow interface types in child fields
Andreas Woess <andreas.woess@jku.at>
parents: 16779
diff changeset
252 if (!Modifier.isFinal(field.getModifiers())) {
846c059e3ecf Truffle: allow interface types in child fields
Andreas Woess <andreas.woess@jku.at>
parents: 16779
diff changeset
253 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
254 }
846c059e3ecf Truffle: allow interface types in child fields
Andreas Woess <andreas.woess@jku.at>
parents: 16779
diff changeset
255 }
846c059e3ecf Truffle: allow interface types in child fields
Andreas Woess <andreas.woess@jku.at>
parents: 16779
diff changeset
256
9754
98b004bf3985 Refactoring of NodeUtil to centralize the use of reflection
Christian Wimmer <christian.wimmer@oracle.com>
parents: 9538
diff changeset
257 public NodeField[] getFields() {
98b004bf3985 Refactoring of NodeUtil to centralize the use of reflection
Christian Wimmer <christian.wimmer@oracle.com>
parents: 9538
diff changeset
258 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
259 }
9258
07f8d136a05e Truffle API changes for the Frame API. Introduction of Assumptions class.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents: 7530
diff changeset
260
07f8d136a05e Truffle API changes for the Frame API. Introduction of Assumptions class.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents: 7530
diff changeset
261 public long getParentOffset() {
07f8d136a05e Truffle API changes for the Frame API. Introduction of Assumptions class.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents: 7530
diff changeset
262 return parentOffset;
07f8d136a05e Truffle API changes for the Frame API. Introduction of Assumptions class.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents: 7530
diff changeset
263 }
07f8d136a05e Truffle API changes for the Frame API. Introduction of Assumptions class.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents: 7530
diff changeset
264
9754
98b004bf3985 Refactoring of NodeUtil to centralize the use of reflection
Christian Wimmer <christian.wimmer@oracle.com>
parents: 9538
diff changeset
265 public long[] getChildOffsets() {
98b004bf3985 Refactoring of NodeUtil to centralize the use of reflection
Christian Wimmer <christian.wimmer@oracle.com>
parents: 9538
diff changeset
266 return childOffsets;
9258
07f8d136a05e Truffle API changes for the Frame API. Introduction of Assumptions class.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents: 7530
diff changeset
267 }
07f8d136a05e Truffle API changes for the Frame API. Introduction of Assumptions class.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents: 7530
diff changeset
268
9754
98b004bf3985 Refactoring of NodeUtil to centralize the use of reflection
Christian Wimmer <christian.wimmer@oracle.com>
parents: 9538
diff changeset
269 public long[] getChildrenOffsets() {
98b004bf3985 Refactoring of NodeUtil to centralize the use of reflection
Christian Wimmer <christian.wimmer@oracle.com>
parents: 9538
diff changeset
270 return childrenOffsets;
9258
07f8d136a05e Truffle API changes for the Frame API. Introduction of Assumptions class.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents: 7530
diff changeset
271 }
14743
762c9aceb7d8 Add equals/hashCode methods
Christian Wimmer <christian.wimmer@oracle.com>
parents: 14569
diff changeset
272
762c9aceb7d8 Add equals/hashCode methods
Christian Wimmer <christian.wimmer@oracle.com>
parents: 14569
diff changeset
273 @Override
762c9aceb7d8 Add equals/hashCode methods
Christian Wimmer <christian.wimmer@oracle.com>
parents: 14569
diff changeset
274 public int hashCode() {
762c9aceb7d8 Add equals/hashCode methods
Christian Wimmer <christian.wimmer@oracle.com>
parents: 14569
diff changeset
275 return Arrays.hashCode(fields) ^ Arrays.hashCode(childOffsets) ^ Arrays.hashCode(childrenOffsets) ^ ((Long) parentOffset).hashCode();
762c9aceb7d8 Add equals/hashCode methods
Christian Wimmer <christian.wimmer@oracle.com>
parents: 14569
diff changeset
276 }
762c9aceb7d8 Add equals/hashCode methods
Christian Wimmer <christian.wimmer@oracle.com>
parents: 14569
diff changeset
277
762c9aceb7d8 Add equals/hashCode methods
Christian Wimmer <christian.wimmer@oracle.com>
parents: 14569
diff changeset
278 @Override
762c9aceb7d8 Add equals/hashCode methods
Christian Wimmer <christian.wimmer@oracle.com>
parents: 14569
diff changeset
279 public boolean equals(Object obj) {
762c9aceb7d8 Add equals/hashCode methods
Christian Wimmer <christian.wimmer@oracle.com>
parents: 14569
diff changeset
280 if (obj instanceof NodeClass) {
762c9aceb7d8 Add equals/hashCode methods
Christian Wimmer <christian.wimmer@oracle.com>
parents: 14569
diff changeset
281 NodeClass other = (NodeClass) obj;
762c9aceb7d8 Add equals/hashCode methods
Christian Wimmer <christian.wimmer@oracle.com>
parents: 14569
diff changeset
282 return Arrays.equals(fields, other.fields) && Arrays.equals(childOffsets, other.childOffsets) && Arrays.equals(childrenOffsets, other.childrenOffsets) &&
762c9aceb7d8 Add equals/hashCode methods
Christian Wimmer <christian.wimmer@oracle.com>
parents: 14569
diff changeset
283 parentOffset == other.parentOffset;
762c9aceb7d8 Add equals/hashCode methods
Christian Wimmer <christian.wimmer@oracle.com>
parents: 14569
diff changeset
284 }
762c9aceb7d8 Add equals/hashCode methods
Christian Wimmer <christian.wimmer@oracle.com>
parents: 14569
diff changeset
285 return false;
762c9aceb7d8 Add equals/hashCode methods
Christian Wimmer <christian.wimmer@oracle.com>
parents: 14569
diff changeset
286 }
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
287
16467
17f7331dcc4f Truffle: move iterator to NodeClass
Andreas Woess <andreas.woess@jku.at>
parents: 16466
diff changeset
288 public Iterator<Node> makeIterator(Node node) {
17f7331dcc4f Truffle: move iterator to NodeClass
Andreas Woess <andreas.woess@jku.at>
parents: 16466
diff changeset
289 assert clazz.isInstance(node);
17f7331dcc4f Truffle: move iterator to NodeClass
Andreas Woess <andreas.woess@jku.at>
parents: 16466
diff changeset
290 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
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
16467
17f7331dcc4f Truffle: move iterator to NodeClass
Andreas Woess <andreas.woess@jku.at>
parents: 16466
diff changeset
293 private final class NodeIterator implements Iterator<Node> {
17f7331dcc4f Truffle: move iterator to NodeClass
Andreas Woess <andreas.woess@jku.at>
parents: 16466
diff changeset
294 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
295 private final int childrenCount;
8be5c68a779d Truffle: revert to previous iterator implementation, add test case
Andreas Woess <andreas.woess@jku.at>
parents: 16548
diff changeset
296 private int index;
16467
17f7331dcc4f Truffle: move iterator to NodeClass
Andreas Woess <andreas.woess@jku.at>
parents: 16466
diff changeset
297
17f7331dcc4f Truffle: move iterator to NodeClass
Andreas Woess <andreas.woess@jku.at>
parents: 16466
diff changeset
298 protected NodeIterator(Node node) {
17f7331dcc4f Truffle: move iterator to NodeClass
Andreas Woess <andreas.woess@jku.at>
parents: 16466
diff changeset
299 this.node = node;
16581
8be5c68a779d Truffle: revert to previous iterator implementation, add test case
Andreas Woess <andreas.woess@jku.at>
parents: 16548
diff changeset
300 this.index = 0;
8be5c68a779d Truffle: revert to previous iterator implementation, add test case
Andreas Woess <andreas.woess@jku.at>
parents: 16548
diff changeset
301 this.childrenCount = childrenCount();
8be5c68a779d Truffle: revert to previous iterator implementation, add test case
Andreas Woess <andreas.woess@jku.at>
parents: 16548
diff changeset
302 }
8be5c68a779d Truffle: revert to previous iterator implementation, add test case
Andreas Woess <andreas.woess@jku.at>
parents: 16548
diff changeset
303
8be5c68a779d Truffle: revert to previous iterator implementation, add test case
Andreas Woess <andreas.woess@jku.at>
parents: 16548
diff changeset
304 private int childrenCount() {
8be5c68a779d Truffle: revert to previous iterator implementation, add test case
Andreas Woess <andreas.woess@jku.at>
parents: 16548
diff changeset
305 int nodeCount = childOffsets.length;
8be5c68a779d Truffle: revert to previous iterator implementation, add test case
Andreas Woess <andreas.woess@jku.at>
parents: 16548
diff changeset
306 for (long fieldOffset : childrenOffsets) {
17275
846c059e3ecf Truffle: allow interface types in child fields
Andreas Woess <andreas.woess@jku.at>
parents: 16779
diff changeset
307 Object[] children = ((Object[]) unsafe.getObject(node, fieldOffset));
16581
8be5c68a779d Truffle: revert to previous iterator implementation, add test case
Andreas Woess <andreas.woess@jku.at>
parents: 16548
diff changeset
308 if (children != null) {
8be5c68a779d Truffle: revert to previous iterator implementation, add test case
Andreas Woess <andreas.woess@jku.at>
parents: 16548
diff changeset
309 nodeCount += children.length;
8be5c68a779d Truffle: revert to previous iterator implementation, add test case
Andreas Woess <andreas.woess@jku.at>
parents: 16548
diff changeset
310 }
8be5c68a779d Truffle: revert to previous iterator implementation, add test case
Andreas Woess <andreas.woess@jku.at>
parents: 16548
diff changeset
311 }
8be5c68a779d Truffle: revert to previous iterator implementation, add test case
Andreas Woess <andreas.woess@jku.at>
parents: 16548
diff changeset
312 return nodeCount;
8be5c68a779d Truffle: revert to previous iterator implementation, add test case
Andreas Woess <andreas.woess@jku.at>
parents: 16548
diff changeset
313 }
8be5c68a779d Truffle: revert to previous iterator implementation, add test case
Andreas Woess <andreas.woess@jku.at>
parents: 16548
diff changeset
314
8be5c68a779d Truffle: revert to previous iterator implementation, add test case
Andreas Woess <andreas.woess@jku.at>
parents: 16548
diff changeset
315 private Node nodeAt(int idx) {
8be5c68a779d Truffle: revert to previous iterator implementation, add test case
Andreas Woess <andreas.woess@jku.at>
parents: 16548
diff changeset
316 int nodeCount = childOffsets.length;
8be5c68a779d Truffle: revert to previous iterator implementation, add test case
Andreas Woess <andreas.woess@jku.at>
parents: 16548
diff changeset
317 if (idx < nodeCount) {
8be5c68a779d Truffle: revert to previous iterator implementation, add test case
Andreas Woess <andreas.woess@jku.at>
parents: 16548
diff changeset
318 return (Node) unsafe.getObject(node, childOffsets[idx]);
8be5c68a779d Truffle: revert to previous iterator implementation, add test case
Andreas Woess <andreas.woess@jku.at>
parents: 16548
diff changeset
319 } else {
8be5c68a779d Truffle: revert to previous iterator implementation, add test case
Andreas Woess <andreas.woess@jku.at>
parents: 16548
diff changeset
320 for (long fieldOffset : childrenOffsets) {
17275
846c059e3ecf Truffle: allow interface types in child fields
Andreas Woess <andreas.woess@jku.at>
parents: 16779
diff changeset
321 Object[] nodeArray = (Object[]) unsafe.getObject(node, fieldOffset);
16581
8be5c68a779d Truffle: revert to previous iterator implementation, add test case
Andreas Woess <andreas.woess@jku.at>
parents: 16548
diff changeset
322 if (idx < nodeCount + nodeArray.length) {
17275
846c059e3ecf Truffle: allow interface types in child fields
Andreas Woess <andreas.woess@jku.at>
parents: 16779
diff changeset
323 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
324 }
8be5c68a779d Truffle: revert to previous iterator implementation, add test case
Andreas Woess <andreas.woess@jku.at>
parents: 16548
diff changeset
325 nodeCount += nodeArray.length;
8be5c68a779d Truffle: revert to previous iterator implementation, add test case
Andreas Woess <andreas.woess@jku.at>
parents: 16548
diff changeset
326 }
8be5c68a779d Truffle: revert to previous iterator implementation, add test case
Andreas Woess <andreas.woess@jku.at>
parents: 16548
diff changeset
327 }
8be5c68a779d Truffle: revert to previous iterator implementation, add test case
Andreas Woess <andreas.woess@jku.at>
parents: 16548
diff changeset
328 return null;
16467
17f7331dcc4f Truffle: move iterator to NodeClass
Andreas Woess <andreas.woess@jku.at>
parents: 16466
diff changeset
329 }
17f7331dcc4f Truffle: move iterator to NodeClass
Andreas Woess <andreas.woess@jku.at>
parents: 16466
diff changeset
330
17f7331dcc4f Truffle: move iterator to NodeClass
Andreas Woess <andreas.woess@jku.at>
parents: 16466
diff changeset
331 private void forward() {
16581
8be5c68a779d Truffle: revert to previous iterator implementation, add test case
Andreas Woess <andreas.woess@jku.at>
parents: 16548
diff changeset
332 if (index < childrenCount) {
8be5c68a779d Truffle: revert to previous iterator implementation, add test case
Andreas Woess <andreas.woess@jku.at>
parents: 16548
diff changeset
333 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
334 }
a4b84ba6dc2e Introduction of the Truffle API for efficient implementation of dynamic languages on top of the Graal VM. New projects com.oracle.truffle.api for the API definition and com.oracle.truffle.api.test for API tests and documentation.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
335 }
16467
17f7331dcc4f Truffle: move iterator to NodeClass
Andreas Woess <andreas.woess@jku.at>
parents: 16466
diff changeset
336
17f7331dcc4f Truffle: move iterator to NodeClass
Andreas Woess <andreas.woess@jku.at>
parents: 16466
diff changeset
337 public boolean hasNext() {
16581
8be5c68a779d Truffle: revert to previous iterator implementation, add test case
Andreas Woess <andreas.woess@jku.at>
parents: 16548
diff changeset
338 return index < childrenCount;
16467
17f7331dcc4f Truffle: move iterator to NodeClass
Andreas Woess <andreas.woess@jku.at>
parents: 16466
diff changeset
339 }
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
340
16467
17f7331dcc4f Truffle: move iterator to NodeClass
Andreas Woess <andreas.woess@jku.at>
parents: 16466
diff changeset
341 public Node next() {
16581
8be5c68a779d Truffle: revert to previous iterator implementation, add test case
Andreas Woess <andreas.woess@jku.at>
parents: 16548
diff changeset
342 try {
8be5c68a779d Truffle: revert to previous iterator implementation, add test case
Andreas Woess <andreas.woess@jku.at>
parents: 16548
diff changeset
343 return nodeAt(index);
8be5c68a779d Truffle: revert to previous iterator implementation, add test case
Andreas Woess <andreas.woess@jku.at>
parents: 16548
diff changeset
344 } finally {
8be5c68a779d Truffle: revert to previous iterator implementation, add test case
Andreas Woess <andreas.woess@jku.at>
parents: 16548
diff changeset
345 forward();
16467
17f7331dcc4f Truffle: move iterator to NodeClass
Andreas Woess <andreas.woess@jku.at>
parents: 16466
diff changeset
346 }
17f7331dcc4f Truffle: move iterator to NodeClass
Andreas Woess <andreas.woess@jku.at>
parents: 16466
diff changeset
347 }
17f7331dcc4f Truffle: move iterator to NodeClass
Andreas Woess <andreas.woess@jku.at>
parents: 16466
diff changeset
348
17f7331dcc4f Truffle: move iterator to NodeClass
Andreas Woess <andreas.woess@jku.at>
parents: 16466
diff changeset
349 public void remove() {
17f7331dcc4f Truffle: move iterator to NodeClass
Andreas Woess <andreas.woess@jku.at>
parents: 16466
diff changeset
350 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
351 }
a4b84ba6dc2e Introduction of the Truffle API for efficient implementation of dynamic languages on top of the Graal VM. New projects com.oracle.truffle.api for the API definition and com.oracle.truffle.api.test for API tests and documentation.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
352 }
16467
17f7331dcc4f Truffle: move iterator to NodeClass
Andreas Woess <andreas.woess@jku.at>
parents: 16466
diff changeset
353 }
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
354
16467
17f7331dcc4f Truffle: move iterator to NodeClass
Andreas Woess <andreas.woess@jku.at>
parents: 16466
diff changeset
355 static Iterator<Node> makeIterator(Node node) {
17f7331dcc4f Truffle: move iterator to NodeClass
Andreas Woess <andreas.woess@jku.at>
parents: 16466
diff changeset
356 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
357 }
a4b84ba6dc2e Introduction of the Truffle API for efficient implementation of dynamic languages on top of the Graal VM. New projects com.oracle.truffle.api for the API definition and com.oracle.truffle.api.test for API tests and documentation.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
358
17399
5787218bad91 Truffle: implemented recursive node iterator and node streams for the graal runtime.
Christian Humer <christian.humer@gmail.com>
parents: 17275
diff changeset
359 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
360 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
361 }
5787218bad91 Truffle: implemented recursive node iterator and node streams for the graal runtime.
Christian Humer <christian.humer@gmail.com>
parents: 17275
diff changeset
362
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
363 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
364 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
365
5787218bad91 Truffle: implemented recursive node iterator and node streams for the graal runtime.
Christian Humer <christian.humer@gmail.com>
parents: 17275
diff changeset
366 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
367 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
368
5787218bad91 Truffle: implemented recursive node iterator and node streams for the graal runtime.
Christian Humer <christian.humer@gmail.com>
parents: 17275
diff changeset
369 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
370
5787218bad91 Truffle: implemented recursive node iterator and node streams for the graal runtime.
Christian Humer <christian.humer@gmail.com>
parents: 17275
diff changeset
371 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
372 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
373 }
5787218bad91 Truffle: implemented recursive node iterator and node streams for the graal runtime.
Christian Humer <christian.humer@gmail.com>
parents: 17275
diff changeset
374
5787218bad91 Truffle: implemented recursive node iterator and node streams for the graal runtime.
Christian Humer <christian.humer@gmail.com>
parents: 17275
diff changeset
375 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
376 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
377 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
378 }
5787218bad91 Truffle: implemented recursive node iterator and node streams for the graal runtime.
Christian Humer <christian.humer@gmail.com>
parents: 17275
diff changeset
379 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
380 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
381 }
5787218bad91 Truffle: implemented recursive node iterator and node streams for the graal runtime.
Christian Humer <christian.humer@gmail.com>
parents: 17275
diff changeset
382
5787218bad91 Truffle: implemented recursive node iterator and node streams for the graal runtime.
Christian Humer <christian.humer@gmail.com>
parents: 17275
diff changeset
383 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
384 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
385 }
5787218bad91 Truffle: implemented recursive node iterator and node streams for the graal runtime.
Christian Humer <christian.humer@gmail.com>
parents: 17275
diff changeset
386 });
5787218bad91 Truffle: implemented recursive node iterator and node streams for the graal runtime.
Christian Humer <christian.humer@gmail.com>
parents: 17275
diff changeset
387 }
5787218bad91 Truffle: implemented recursive node iterator and node streams for the graal runtime.
Christian Humer <christian.humer@gmail.com>
parents: 17275
diff changeset
388
5787218bad91 Truffle: implemented recursive node iterator and node streams for the graal runtime.
Christian Humer <christian.humer@gmail.com>
parents: 17275
diff changeset
389 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
390 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
391 }
5787218bad91 Truffle: implemented recursive node iterator and node streams for the graal runtime.
Christian Humer <christian.humer@gmail.com>
parents: 17275
diff changeset
392
5787218bad91 Truffle: implemented recursive node iterator and node streams for the graal runtime.
Christian Humer <christian.humer@gmail.com>
parents: 17275
diff changeset
393 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
394 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
395 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
396 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
397 }
5787218bad91 Truffle: implemented recursive node iterator and node streams for the graal runtime.
Christian Humer <christian.humer@gmail.com>
parents: 17275
diff changeset
398
5787218bad91 Truffle: implemented recursive node iterator and node streams for the graal runtime.
Christian Humer <christian.humer@gmail.com>
parents: 17275
diff changeset
399 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
400 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
401 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
402 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
403 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
404 }
5787218bad91 Truffle: implemented recursive node iterator and node streams for the graal runtime.
Christian Humer <christian.humer@gmail.com>
parents: 17275
diff changeset
405 }
5787218bad91 Truffle: implemented recursive node iterator and node streams for the graal runtime.
Christian Humer <christian.humer@gmail.com>
parents: 17275
diff changeset
406 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
407 }
5787218bad91 Truffle: implemented recursive node iterator and node streams for the graal runtime.
Christian Humer <christian.humer@gmail.com>
parents: 17275
diff changeset
408
5787218bad91 Truffle: implemented recursive node iterator and node streams for the graal runtime.
Christian Humer <christian.humer@gmail.com>
parents: 17275
diff changeset
409 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
410 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
411 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
412 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
413 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
414 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
415 } else {
5787218bad91 Truffle: implemented recursive node iterator and node streams for the graal runtime.
Christian Humer <christian.humer@gmail.com>
parents: 17275
diff changeset
416 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
417 }
5787218bad91 Truffle: implemented recursive node iterator and node streams for the graal runtime.
Christian Humer <christian.humer@gmail.com>
parents: 17275
diff changeset
418 }
5787218bad91 Truffle: implemented recursive node iterator and node streams for the graal runtime.
Christian Humer <christian.humer@gmail.com>
parents: 17275
diff changeset
419 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
420 }
5787218bad91 Truffle: implemented recursive node iterator and node streams for the graal runtime.
Christian Humer <christian.humer@gmail.com>
parents: 17275
diff changeset
421
5787218bad91 Truffle: implemented recursive node iterator and node streams for the graal runtime.
Christian Humer <christian.humer@gmail.com>
parents: 17275
diff changeset
422 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
423 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
424 }
5787218bad91 Truffle: implemented recursive node iterator and node streams for the graal runtime.
Christian Humer <christian.humer@gmail.com>
parents: 17275
diff changeset
425 }
5787218bad91 Truffle: implemented recursive node iterator and node streams for the graal runtime.
Christian Humer <christian.humer@gmail.com>
parents: 17275
diff changeset
426
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
427 private static long[] toLongArray(List<Long> list) {
a4b84ba6dc2e Introduction of the Truffle API for efficient implementation of dynamic languages on top of the Graal VM. New projects com.oracle.truffle.api for the API definition and com.oracle.truffle.api.test for API tests and documentation.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
428 long[] array = new long[list.size()];
a4b84ba6dc2e Introduction of the Truffle API for efficient implementation of dynamic languages on top of the Graal VM. New projects com.oracle.truffle.api for the API definition and com.oracle.truffle.api.test for API tests and documentation.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
429 for (int i = 0; i < list.size(); 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
430 array[i] = list.get(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
431 }
a4b84ba6dc2e Introduction of the Truffle API for efficient implementation of dynamic languages on top of the Graal VM. New projects com.oracle.truffle.api for the API definition and com.oracle.truffle.api.test for API tests and documentation.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
432 return array;
a4b84ba6dc2e Introduction of the Truffle API for efficient implementation of dynamic languages on top of the Graal VM. New projects com.oracle.truffle.api for the API definition and com.oracle.truffle.api.test for API tests and documentation.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
433 }
a4b84ba6dc2e Introduction of the Truffle API for efficient implementation of dynamic languages on top of the Graal VM. New projects com.oracle.truffle.api for the API definition and com.oracle.truffle.api.test for API tests and documentation.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
434
11636
136eaa90ef41 Truffle: make NodeUtil class final and its unsafe field private.
Andreas Woess <andreas.woess@jku.at>
parents: 11622
diff changeset
435 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
436
a4b84ba6dc2e Introduction of the Truffle API for efficient implementation of dynamic languages on top of the Graal VM. New projects com.oracle.truffle.api for the API definition and com.oracle.truffle.api.test for API tests and documentation.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
437 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
438 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
439 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
440 } 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
441 }
a4b84ba6dc2e Introduction of the Truffle API for efficient implementation of dynamic languages on top of the Graal VM. New projects com.oracle.truffle.api for the API definition and com.oracle.truffle.api.test for API tests and documentation.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
442 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
443 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
444 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
445 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
446 } 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
447 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
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 }
a4b84ba6dc2e Introduction of the Truffle API for efficient implementation of dynamic languages on top of the Graal VM. New projects com.oracle.truffle.api for the API definition and com.oracle.truffle.api.test for API tests and documentation.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
450
a4b84ba6dc2e Introduction of the Truffle API for efficient implementation of dynamic languages on top of the Graal VM. New projects com.oracle.truffle.api for the API definition and com.oracle.truffle.api.test for API tests and documentation.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
451 @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
452 public static <T extends Node> T cloneNode(T orig) {
11809
c7769440afd8 improve NodeUtil.cloneNode.
Andreas Woess <andreas.woess@jku.at>
parents: 11649
diff changeset
453 final Node clone = orig.copy();
c7769440afd8 improve NodeUtil.cloneNode.
Andreas Woess <andreas.woess@jku.at>
parents: 11649
diff changeset
454 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
455
a4b84ba6dc2e Introduction of the Truffle API for efficient implementation of dynamic languages on top of the Graal VM. New projects com.oracle.truffle.api for the API definition and com.oracle.truffle.api.test for API tests and documentation.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
456 unsafe.putObject(clone, nodeClass.parentOffset, 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
457
9754
98b004bf3985 Refactoring of NodeUtil to centralize the use of reflection
Christian Wimmer <christian.wimmer@oracle.com>
parents: 9538
diff changeset
458 for (long fieldOffset : nodeClass.childOffsets) {
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
459 Node child = (Node) unsafe.getObject(orig, fieldOffset);
a4b84ba6dc2e Introduction of the Truffle API for efficient implementation of dynamic languages on top of the Graal VM. New projects com.oracle.truffle.api for the API definition and com.oracle.truffle.api.test for API tests and documentation.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
460 if (child != 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
461 Node clonedChild = cloneNode(child);
a4b84ba6dc2e Introduction of the Truffle API for efficient implementation of dynamic languages on top of the Graal VM. New projects com.oracle.truffle.api for the API definition and com.oracle.truffle.api.test for API tests and documentation.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
462 unsafe.putObject(clonedChild, nodeClass.parentOffset, clone);
a4b84ba6dc2e Introduction of the Truffle API for efficient implementation of dynamic languages on top of the Graal VM. New projects com.oracle.truffle.api for the API definition and com.oracle.truffle.api.test for API tests and documentation.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
463 unsafe.putObject(clone, fieldOffset, clonedChild);
a4b84ba6dc2e Introduction of the Truffle API for efficient implementation of dynamic languages on top of the Graal VM. New projects com.oracle.truffle.api for the API definition and com.oracle.truffle.api.test for API tests and documentation.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
464 }
a4b84ba6dc2e Introduction of the Truffle API for efficient implementation of dynamic languages on top of the Graal VM. New projects com.oracle.truffle.api for the API definition and com.oracle.truffle.api.test for API tests and documentation.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
465 }
9754
98b004bf3985 Refactoring of NodeUtil to centralize the use of reflection
Christian Wimmer <christian.wimmer@oracle.com>
parents: 9538
diff changeset
466 for (long fieldOffset : nodeClass.childrenOffsets) {
17275
846c059e3ecf Truffle: allow interface types in child fields
Andreas Woess <andreas.woess@jku.at>
parents: 16779
diff changeset
467 Object[] children = (Object[]) unsafe.getObject(orig, fieldOffset);
7267
a4b84ba6dc2e Introduction of the Truffle API for efficient implementation of dynamic languages on top of the Graal VM. New projects com.oracle.truffle.api for the API definition and com.oracle.truffle.api.test for API tests and documentation.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
468 if (children != null) {
17275
846c059e3ecf Truffle: allow interface types in child fields
Andreas Woess <andreas.woess@jku.at>
parents: 16779
diff changeset
469 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
470 for (int i = 0; i < children.length; i++) {
11809
c7769440afd8 improve NodeUtil.cloneNode.
Andreas Woess <andreas.woess@jku.at>
parents: 11649
diff changeset
471 if (children[i] != null) {
17275
846c059e3ecf Truffle: allow interface types in child fields
Andreas Woess <andreas.woess@jku.at>
parents: 16779
diff changeset
472 Node clonedChild = cloneNode((Node) children[i]);
11809
c7769440afd8 improve NodeUtil.cloneNode.
Andreas Woess <andreas.woess@jku.at>
parents: 11649
diff changeset
473 clonedChildren[i] = clonedChild;
c7769440afd8 improve NodeUtil.cloneNode.
Andreas Woess <andreas.woess@jku.at>
parents: 11649
diff changeset
474 unsafe.putObject(clonedChild, nodeClass.parentOffset, 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
475 }
a4b84ba6dc2e Introduction of the Truffle API for efficient implementation of dynamic languages on top of the Graal VM. New projects com.oracle.truffle.api for the API definition and com.oracle.truffle.api.test for API tests and documentation.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
476 }
a4b84ba6dc2e Introduction of the Truffle API for efficient implementation of dynamic languages on top of the Graal VM. New projects com.oracle.truffle.api for the API definition and com.oracle.truffle.api.test for API tests and documentation.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
477 unsafe.putObject(clone, fieldOffset, clonedChildren);
a4b84ba6dc2e Introduction of the Truffle API for efficient implementation of dynamic languages on top of the Graal VM. New projects com.oracle.truffle.api for the API definition and com.oracle.truffle.api.test for API tests and documentation.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
478 }
a4b84ba6dc2e Introduction of the Truffle API for efficient implementation of dynamic languages on top of the Graal VM. New projects com.oracle.truffle.api for the API definition and com.oracle.truffle.api.test for API tests and documentation.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
479 }
18510
cb4d5cc2b52b Truffle: clone ConditionProfile and BranchProfile node fields
Andreas Woess <andreas.woess@jku.at>
parents: 18485
diff changeset
480 for (long fieldOffset : nodeClass.cloneableOffsets) {
cb4d5cc2b52b Truffle: clone ConditionProfile and BranchProfile node fields
Andreas Woess <andreas.woess@jku.at>
parents: 18485
diff changeset
481 Object cloneable = unsafe.getObject(clone, fieldOffset);
cb4d5cc2b52b Truffle: clone ConditionProfile and BranchProfile node fields
Andreas Woess <andreas.woess@jku.at>
parents: 18485
diff changeset
482 if (cloneable != null && cloneable == unsafe.getObject(orig, fieldOffset)) {
cb4d5cc2b52b Truffle: clone ConditionProfile and BranchProfile node fields
Andreas Woess <andreas.woess@jku.at>
parents: 18485
diff changeset
483 unsafe.putObject(clone, fieldOffset, ((NodeCloneable) cloneable).clone());
cb4d5cc2b52b Truffle: clone ConditionProfile and BranchProfile node fields
Andreas Woess <andreas.woess@jku.at>
parents: 18485
diff changeset
484 }
cb4d5cc2b52b Truffle: clone ConditionProfile and BranchProfile node fields
Andreas Woess <andreas.woess@jku.at>
parents: 18485
diff changeset
485 }
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
486 return (T) clone;
a4b84ba6dc2e Introduction of the Truffle API for efficient implementation of dynamic languages on top of the Graal VM. New projects com.oracle.truffle.api for the API definition and com.oracle.truffle.api.test for API tests and documentation.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
487 }
a4b84ba6dc2e Introduction of the Truffle API for efficient implementation of dynamic languages on top of the Graal VM. New projects com.oracle.truffle.api for the API definition and com.oracle.truffle.api.test for API tests and documentation.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
488
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
489 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
490 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
491 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
492
9754
98b004bf3985 Refactoring of NodeUtil to centralize the use of reflection
Christian Wimmer <christian.wimmer@oracle.com>
parents: 9538
diff changeset
493 for (long fieldOffset : nodeClass.childOffsets) {
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
494 Object child = unsafe.getObject(node, fieldOffset);
a4b84ba6dc2e Introduction of the Truffle API for efficient implementation of dynamic languages on top of the Graal VM. New projects com.oracle.truffle.api for the API definition and com.oracle.truffle.api.test for API tests and documentation.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
495 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
496 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
497 }
a4b84ba6dc2e Introduction of the Truffle API for efficient implementation of dynamic languages on top of the Graal VM. New projects com.oracle.truffle.api for the API definition and com.oracle.truffle.api.test for API tests and documentation.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
498 }
9754
98b004bf3985 Refactoring of NodeUtil to centralize the use of reflection
Christian Wimmer <christian.wimmer@oracle.com>
parents: 9538
diff changeset
499 for (long fieldOffset : nodeClass.childrenOffsets) {
17275
846c059e3ecf Truffle: allow interface types in child fields
Andreas Woess <andreas.woess@jku.at>
parents: 16779
diff changeset
500 Object[] children = (Object[]) unsafe.getObject(node, fieldOffset);
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
501 if (children != null) {
17275
846c059e3ecf Truffle: allow interface types in child fields
Andreas Woess <andreas.woess@jku.at>
parents: 16779
diff changeset
502 for (Object child : children) {
10845
e9248ebb1d79 Truffle: ignore null children in NodeUtil.findNodeChildren; refactoring.
Andreas Woess <andreas.woess@jku.at>
parents: 10793
diff changeset
503 if (child != null) {
17275
846c059e3ecf Truffle: allow interface types in child fields
Andreas Woess <andreas.woess@jku.at>
parents: 16779
diff changeset
504 nodes.add((Node) child);
10845
e9248ebb1d79 Truffle: ignore null children in NodeUtil.findNodeChildren; refactoring.
Andreas Woess <andreas.woess@jku.at>
parents: 10793
diff changeset
505 }
e9248ebb1d79 Truffle: ignore null children in NodeUtil.findNodeChildren; refactoring.
Andreas Woess <andreas.woess@jku.at>
parents: 10793
diff changeset
506 }
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
507 }
a4b84ba6dc2e Introduction of the Truffle API for efficient implementation of dynamic languages on top of the Graal VM. New projects com.oracle.truffle.api for the API definition and com.oracle.truffle.api.test for API tests and documentation.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
508 }
a4b84ba6dc2e Introduction of the Truffle API for efficient implementation of dynamic languages on top of the Graal VM. New projects com.oracle.truffle.api for the API definition and com.oracle.truffle.api.test for API tests and documentation.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
509
a4b84ba6dc2e Introduction of the Truffle API for efficient implementation of dynamic languages on top of the Graal VM. New projects com.oracle.truffle.api for the API definition and com.oracle.truffle.api.test for API tests and documentation.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
510 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
511 }
a4b84ba6dc2e Introduction of the Truffle API for efficient implementation of dynamic languages on top of the Graal VM. New projects com.oracle.truffle.api for the API definition and com.oracle.truffle.api.test for API tests and documentation.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
512
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
513 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
514 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
515
10755
b221e31d7b0b Truffle: revise NodeUtil.replaceChild assertion
Andreas Woess <andreas.woess@jku.at>
parents: 10742
diff changeset
516 for (long fieldOffset : nodeClass.getChildOffsets()) {
7267
a4b84ba6dc2e Introduction of the Truffle API for efficient implementation of dynamic languages on top of the Graal VM. New projects com.oracle.truffle.api for the API definition and com.oracle.truffle.api.test for API tests and documentation.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
517 if (unsafe.getObject(parent, fieldOffset) == oldChild) {
10755
b221e31d7b0b Truffle: revise NodeUtil.replaceChild assertion
Andreas Woess <andreas.woess@jku.at>
parents: 10742
diff changeset
518 assert assertAssignable(nodeClass, fieldOffset, 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
519 unsafe.putObject(parent, fieldOffset, 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
520 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
521 }
a4b84ba6dc2e Introduction of the Truffle API for efficient implementation of dynamic languages on top of the Graal VM. New projects com.oracle.truffle.api 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 }
10742
99789440ce28 Truffle: Added assertions to replaceChild.
Christian Humer <christian.humer@gmail.com>
parents: 10704
diff changeset
523
10755
b221e31d7b0b Truffle: revise NodeUtil.replaceChild assertion
Andreas Woess <andreas.woess@jku.at>
parents: 10742
diff changeset
524 for (long fieldOffset : nodeClass.getChildrenOffsets()) {
10742
99789440ce28 Truffle: Added assertions to replaceChild.
Christian Humer <christian.humer@gmail.com>
parents: 10704
diff changeset
525 Object arrayObject = unsafe.getObject(parent, fieldOffset);
99789440ce28 Truffle: Added assertions to replaceChild.
Christian Humer <christian.humer@gmail.com>
parents: 10704
diff changeset
526 if (arrayObject != null) {
17275
846c059e3ecf Truffle: allow interface types in child fields
Andreas Woess <andreas.woess@jku.at>
parents: 16779
diff changeset
527 Object[] array = (Object[]) arrayObject;
10755
b221e31d7b0b Truffle: revise NodeUtil.replaceChild assertion
Andreas Woess <andreas.woess@jku.at>
parents: 10742
diff changeset
528 for (int i = 0; i < array.length; i++) {
b221e31d7b0b Truffle: revise NodeUtil.replaceChild assertion
Andreas Woess <andreas.woess@jku.at>
parents: 10742
diff changeset
529 if (array[i] == oldChild) {
b221e31d7b0b Truffle: revise NodeUtil.replaceChild assertion
Andreas Woess <andreas.woess@jku.at>
parents: 10742
diff changeset
530 assert assertAssignable(nodeClass, fieldOffset, newChild);
b221e31d7b0b Truffle: revise NodeUtil.replaceChild assertion
Andreas Woess <andreas.woess@jku.at>
parents: 10742
diff changeset
531 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
532 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
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 }
a4b84ba6dc2e Introduction of the Truffle API for efficient implementation of dynamic languages on top of the Graal VM. New projects com.oracle.truffle.api 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 }
a4b84ba6dc2e Introduction of the Truffle API for efficient implementation of dynamic languages on top of the Graal VM. New projects com.oracle.truffle.api for the API definition and com.oracle.truffle.api.test for API tests and documentation.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
536 }
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
537 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
538 }
a4b84ba6dc2e Introduction of the Truffle API for efficient implementation of dynamic languages on top of the Graal VM. New projects com.oracle.truffle.api for the API definition and com.oracle.truffle.api.test for API tests and documentation.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
539
10755
b221e31d7b0b Truffle: revise NodeUtil.replaceChild assertion
Andreas Woess <andreas.woess@jku.at>
parents: 10742
diff changeset
540 private static boolean assertAssignable(NodeClass clazz, long fieldOffset, Object newValue) {
10742
99789440ce28 Truffle: Added assertions to replaceChild.
Christian Humer <christian.humer@gmail.com>
parents: 10704
diff changeset
541 if (newValue == null) {
99789440ce28 Truffle: Added assertions to replaceChild.
Christian Humer <christian.humer@gmail.com>
parents: 10704
diff changeset
542 return true;
99789440ce28 Truffle: Added assertions to replaceChild.
Christian Humer <christian.humer@gmail.com>
parents: 10704
diff changeset
543 }
10755
b221e31d7b0b Truffle: revise NodeUtil.replaceChild assertion
Andreas Woess <andreas.woess@jku.at>
parents: 10742
diff changeset
544 for (NodeField field : clazz.getFields()) {
b221e31d7b0b Truffle: revise NodeUtil.replaceChild assertion
Andreas Woess <andreas.woess@jku.at>
parents: 10742
diff changeset
545 if (field.getOffset() == fieldOffset) {
b221e31d7b0b Truffle: revise NodeUtil.replaceChild assertion
Andreas Woess <andreas.woess@jku.at>
parents: 10742
diff changeset
546 if (field.getKind() == NodeFieldKind.CHILD) {
b221e31d7b0b Truffle: revise NodeUtil.replaceChild assertion
Andreas Woess <andreas.woess@jku.at>
parents: 10742
diff changeset
547 if (field.getType().isAssignableFrom(newValue.getClass())) {
b221e31d7b0b Truffle: revise NodeUtil.replaceChild assertion
Andreas Woess <andreas.woess@jku.at>
parents: 10742
diff changeset
548 return true;
b221e31d7b0b Truffle: revise NodeUtil.replaceChild assertion
Andreas Woess <andreas.woess@jku.at>
parents: 10742
diff changeset
549 } else {
b221e31d7b0b Truffle: revise NodeUtil.replaceChild assertion
Andreas Woess <andreas.woess@jku.at>
parents: 10742
diff changeset
550 assert false : "Child class " + newValue.getClass().getName() + " is not assignable to field \"" + field.getName() + "\" of type " + field.getType().getName();
b221e31d7b0b Truffle: revise NodeUtil.replaceChild assertion
Andreas Woess <andreas.woess@jku.at>
parents: 10742
diff changeset
551 return false;
b221e31d7b0b Truffle: revise NodeUtil.replaceChild assertion
Andreas Woess <andreas.woess@jku.at>
parents: 10742
diff changeset
552 }
b221e31d7b0b Truffle: revise NodeUtil.replaceChild assertion
Andreas Woess <andreas.woess@jku.at>
parents: 10742
diff changeset
553 } else if (field.getKind() == NodeFieldKind.CHILDREN) {
b221e31d7b0b Truffle: revise NodeUtil.replaceChild assertion
Andreas Woess <andreas.woess@jku.at>
parents: 10742
diff changeset
554 if (field.getType().getComponentType().isAssignableFrom(newValue.getClass())) {
b221e31d7b0b Truffle: revise NodeUtil.replaceChild assertion
Andreas Woess <andreas.woess@jku.at>
parents: 10742
diff changeset
555 return true;
b221e31d7b0b Truffle: revise NodeUtil.replaceChild assertion
Andreas Woess <andreas.woess@jku.at>
parents: 10742
diff changeset
556 } else {
b221e31d7b0b Truffle: revise NodeUtil.replaceChild assertion
Andreas Woess <andreas.woess@jku.at>
parents: 10742
diff changeset
557 assert false : "Child class " + newValue.getClass().getName() + " is not assignable to field \"" + field.getName() + "\" of type " + field.getType().getName();
b221e31d7b0b Truffle: revise NodeUtil.replaceChild assertion
Andreas Woess <andreas.woess@jku.at>
parents: 10742
diff changeset
558 return false;
b221e31d7b0b Truffle: revise NodeUtil.replaceChild assertion
Andreas Woess <andreas.woess@jku.at>
parents: 10742
diff changeset
559 }
10742
99789440ce28 Truffle: Added assertions to replaceChild.
Christian Humer <christian.humer@gmail.com>
parents: 10704
diff changeset
560 }
99789440ce28 Truffle: Added assertions to replaceChild.
Christian Humer <christian.humer@gmail.com>
parents: 10704
diff changeset
561 }
99789440ce28 Truffle: Added assertions to replaceChild.
Christian Humer <christian.humer@gmail.com>
parents: 10704
diff changeset
562 }
10755
b221e31d7b0b Truffle: revise NodeUtil.replaceChild assertion
Andreas Woess <andreas.woess@jku.at>
parents: 10742
diff changeset
563 throw new IllegalArgumentException();
10742
99789440ce28 Truffle: Added assertions to replaceChild.
Christian Humer <christian.humer@gmail.com>
parents: 10704
diff changeset
564 }
99789440ce28 Truffle: Added assertions to replaceChild.
Christian Humer <christian.humer@gmail.com>
parents: 10704
diff changeset
565
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
566 /** 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
567 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
568 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
569 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
570 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
571 }
a4b84ba6dc2e Introduction of the Truffle API for efficient implementation of dynamic languages on top of the Graal VM. New projects com.oracle.truffle.api for the API definition and com.oracle.truffle.api.test for API tests and documentation.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
572 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
573 }
a4b84ba6dc2e Introduction of the Truffle API for efficient implementation of dynamic languages on top of the Graal VM. New projects com.oracle.truffle.api 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 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
576 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
577 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
578 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
579 }
a4b84ba6dc2e Introduction of the Truffle API for efficient implementation of dynamic languages on top of the Graal VM. New projects com.oracle.truffle.api for the API definition and com.oracle.truffle.api.test for API tests and documentation.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
580
11622
ccf047a30f18 Truffle: NodeUtil.getNthParent.
Chris Seaton <chris.seaton@oracle.com>
parents: 11279
diff changeset
581 /**
ccf047a30f18 Truffle: NodeUtil.getNthParent.
Chris Seaton <chris.seaton@oracle.com>
parents: 11279
diff changeset
582 * 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
583 * are less than n ancestors.
ccf047a30f18 Truffle: NodeUtil.getNthParent.
Chris Seaton <chris.seaton@oracle.com>
parents: 11279
diff changeset
584 */
ccf047a30f18 Truffle: NodeUtil.getNthParent.
Chris Seaton <chris.seaton@oracle.com>
parents: 11279
diff changeset
585 public static Node getNthParent(Node node, int n) {
ccf047a30f18 Truffle: NodeUtil.getNthParent.
Chris Seaton <chris.seaton@oracle.com>
parents: 11279
diff changeset
586 Node parent = node;
ccf047a30f18 Truffle: NodeUtil.getNthParent.
Chris Seaton <chris.seaton@oracle.com>
parents: 11279
diff changeset
587
ccf047a30f18 Truffle: NodeUtil.getNthParent.
Chris Seaton <chris.seaton@oracle.com>
parents: 11279
diff changeset
588 for (int i = 0; i < n; i++) {
ccf047a30f18 Truffle: NodeUtil.getNthParent.
Chris Seaton <chris.seaton@oracle.com>
parents: 11279
diff changeset
589 parent = parent.getParent();
ccf047a30f18 Truffle: NodeUtil.getNthParent.
Chris Seaton <chris.seaton@oracle.com>
parents: 11279
diff changeset
590
ccf047a30f18 Truffle: NodeUtil.getNthParent.
Chris Seaton <chris.seaton@oracle.com>
parents: 11279
diff changeset
591 if (parent == null) {
ccf047a30f18 Truffle: NodeUtil.getNthParent.
Chris Seaton <chris.seaton@oracle.com>
parents: 11279
diff changeset
592 return null;
ccf047a30f18 Truffle: NodeUtil.getNthParent.
Chris Seaton <chris.seaton@oracle.com>
parents: 11279
diff changeset
593 }
ccf047a30f18 Truffle: NodeUtil.getNthParent.
Chris Seaton <chris.seaton@oracle.com>
parents: 11279
diff changeset
594 }
ccf047a30f18 Truffle: NodeUtil.getNthParent.
Chris Seaton <chris.seaton@oracle.com>
parents: 11279
diff changeset
595
ccf047a30f18 Truffle: NodeUtil.getNthParent.
Chris Seaton <chris.seaton@oracle.com>
parents: 11279
diff changeset
596 return parent;
ccf047a30f18 Truffle: NodeUtil.getNthParent.
Chris Seaton <chris.seaton@oracle.com>
parents: 11279
diff changeset
597 }
ccf047a30f18 Truffle: NodeUtil.getNthParent.
Chris Seaton <chris.seaton@oracle.com>
parents: 11279
diff changeset
598
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
599 /** 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
600 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
601 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
602 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
603 } else {
7530
5e3d1a68664e applied mx eclipseformat to all Java files
Doug Simon <doug.simon@oracle.com>
parents: 7267
diff changeset
604 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
605 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
606 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
607 }
a4b84ba6dc2e Introduction of the Truffle API for efficient implementation of dynamic languages on top of the Graal VM. New projects com.oracle.truffle.api 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 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
610 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
611 }
a4b84ba6dc2e Introduction of the Truffle API for efficient implementation of dynamic languages on top of the Graal VM. New projects com.oracle.truffle.api for the API definition and com.oracle.truffle.api.test for API tests and documentation.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
612 }
a4b84ba6dc2e Introduction of the Truffle API for efficient implementation of dynamic languages on top of the Graal VM. New projects com.oracle.truffle.api for the API definition and com.oracle.truffle.api.test for API tests and documentation.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
613 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
614 }
a4b84ba6dc2e Introduction of the Truffle API for efficient implementation of dynamic languages on top of the Graal VM. New projects com.oracle.truffle.api for the API definition and com.oracle.truffle.api.test for API tests and documentation.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
615
12666
d9c34e8337f4 Truffle: cleanup of NodeUtil#findParent; added NodeUtil#findAllParents
Christian Humer <christian.humer@gmail.com>
parents: 11950
diff changeset
616 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
617 Node parent = start.getParent();
d9c34e8337f4 Truffle: cleanup of NodeUtil#findParent; added NodeUtil#findAllParents
Christian Humer <christian.humer@gmail.com>
parents: 11950
diff changeset
618 if (parent == null) {
d9c34e8337f4 Truffle: cleanup of NodeUtil#findParent; added NodeUtil#findAllParents
Christian Humer <christian.humer@gmail.com>
parents: 11950
diff changeset
619 return null;
d9c34e8337f4 Truffle: cleanup of NodeUtil#findParent; added NodeUtil#findAllParents
Christian Humer <christian.humer@gmail.com>
parents: 11950
diff changeset
620 } else if (clazz.isInstance(parent)) {
d9c34e8337f4 Truffle: cleanup of NodeUtil#findParent; added NodeUtil#findAllParents
Christian Humer <christian.humer@gmail.com>
parents: 11950
diff changeset
621 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
622 } else {
12666
d9c34e8337f4 Truffle: cleanup of NodeUtil#findParent; added NodeUtil#findAllParents
Christian Humer <christian.humer@gmail.com>
parents: 11950
diff changeset
623 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
624 }
a4b84ba6dc2e Introduction of the Truffle API for efficient implementation of dynamic languages on top of the Graal VM. New projects com.oracle.truffle.api 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
12666
d9c34e8337f4 Truffle: cleanup of NodeUtil#findParent; added NodeUtil#findAllParents
Christian Humer <christian.humer@gmail.com>
parents: 11950
diff changeset
627 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
628 List<T> parents = new ArrayList<>();
d9c34e8337f4 Truffle: cleanup of NodeUtil#findParent; added NodeUtil#findAllParents
Christian Humer <christian.humer@gmail.com>
parents: 11950
diff changeset
629 T parent = findParent(start, clazz);
d9c34e8337f4 Truffle: cleanup of NodeUtil#findParent; added NodeUtil#findAllParents
Christian Humer <christian.humer@gmail.com>
parents: 11950
diff changeset
630 while (parent != null) {
d9c34e8337f4 Truffle: cleanup of NodeUtil#findParent; added NodeUtil#findAllParents
Christian Humer <christian.humer@gmail.com>
parents: 11950
diff changeset
631 parents.add(parent);
d9c34e8337f4 Truffle: cleanup of NodeUtil#findParent; added NodeUtil#findAllParents
Christian Humer <christian.humer@gmail.com>
parents: 11950
diff changeset
632 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
633 }
12666
d9c34e8337f4 Truffle: cleanup of NodeUtil#findParent; added NodeUtil#findAllParents
Christian Humer <christian.humer@gmail.com>
parents: 11950
diff changeset
634 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
635 }
a4b84ba6dc2e Introduction of the Truffle API for efficient implementation of dynamic languages on top of the Graal VM. New projects com.oracle.truffle.api for the API definition and com.oracle.truffle.api.test for API tests and documentation.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
636
12666
d9c34e8337f4 Truffle: cleanup of NodeUtil#findParent; added NodeUtil#findAllParents
Christian Humer <christian.humer@gmail.com>
parents: 11950
diff changeset
637 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
638 List<Node> nodes = new ArrayList<>();
d9c34e8337f4 Truffle: cleanup of NodeUtil#findParent; added NodeUtil#findAllParents
Christian Humer <christian.humer@gmail.com>
parents: 11950
diff changeset
639 Node current = child;
d9c34e8337f4 Truffle: cleanup of NodeUtil#findParent; added NodeUtil#findAllParents
Christian Humer <christian.humer@gmail.com>
parents: 11950
diff changeset
640 while (current != null) {
d9c34e8337f4 Truffle: cleanup of NodeUtil#findParent; added NodeUtil#findAllParents
Christian Humer <christian.humer@gmail.com>
parents: 11950
diff changeset
641 nodes.add(current);
d9c34e8337f4 Truffle: cleanup of NodeUtil#findParent; added NodeUtil#findAllParents
Christian Humer <christian.humer@gmail.com>
parents: 11950
diff changeset
642 if (current == parent) {
d9c34e8337f4 Truffle: cleanup of NodeUtil#findParent; added NodeUtil#findAllParents
Christian Humer <christian.humer@gmail.com>
parents: 11950
diff changeset
643 return nodes;
d9c34e8337f4 Truffle: cleanup of NodeUtil#findParent; added NodeUtil#findAllParents
Christian Humer <christian.humer@gmail.com>
parents: 11950
diff changeset
644 }
d9c34e8337f4 Truffle: cleanup of NodeUtil#findParent; added NodeUtil#findAllParents
Christian Humer <christian.humer@gmail.com>
parents: 11950
diff changeset
645 current = current.getParent();
d9c34e8337f4 Truffle: cleanup of NodeUtil#findParent; added NodeUtil#findAllParents
Christian Humer <christian.humer@gmail.com>
parents: 11950
diff changeset
646 }
d9c34e8337f4 Truffle: cleanup of NodeUtil#findParent; added NodeUtil#findAllParents
Christian Humer <christian.humer@gmail.com>
parents: 11950
diff changeset
647 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
648 }
d9c34e8337f4 Truffle: cleanup of NodeUtil#findParent; added NodeUtil#findAllParents
Christian Humer <christian.humer@gmail.com>
parents: 11950
diff changeset
649
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
650 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
651 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
652 if (clazz.isInstance(childNode)) {
12666
d9c34e8337f4 Truffle: cleanup of NodeUtil#findParent; added NodeUtil#findAllParents
Christian Humer <christian.humer@gmail.com>
parents: 11950
diff changeset
653 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
654 } else {
9487
0285fbb8b05d NodeUtil.findFirstNodeInstance bug fix
Andreas Woess <andreas.woess@jku.at>
parents: 9486
diff changeset
655 T node = findFirstNodeInstance(childNode, clazz);
0285fbb8b05d NodeUtil.findFirstNodeInstance bug fix
Andreas Woess <andreas.woess@jku.at>
parents: 9486
diff changeset
656 if (node != null) {
0285fbb8b05d NodeUtil.findFirstNodeInstance bug fix
Andreas Woess <andreas.woess@jku.at>
parents: 9486
diff changeset
657 return node;
0285fbb8b05d NodeUtil.findFirstNodeInstance bug fix
Andreas Woess <andreas.woess@jku.at>
parents: 9486
diff changeset
658 }
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
659 }
a4b84ba6dc2e Introduction of the Truffle API for efficient implementation of dynamic languages on top of the Graal VM. New projects com.oracle.truffle.api for the API definition and com.oracle.truffle.api.test for API tests and documentation.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
660 }
a4b84ba6dc2e Introduction of the Truffle API for efficient implementation of dynamic languages on top of the Graal VM. New projects com.oracle.truffle.api for the API definition and com.oracle.truffle.api.test for API tests and documentation.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
661 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
662 }
a4b84ba6dc2e Introduction of the Truffle API for efficient implementation of dynamic languages on top of the Graal VM. New projects com.oracle.truffle.api for the API definition and com.oracle.truffle.api.test for API tests and documentation.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
663
16465
456abab80eb5 Truffle: remove obsolete NodeUtil.findNodeInstancesInFunction (functionally equivalent to findAllNodeInstances)
Andreas Woess <andreas.woess@jku.at>
parents: 16464
diff changeset
664 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
665 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
666 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
667 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
668 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
669 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
670 }
a4b84ba6dc2e Introduction of the Truffle API for efficient implementation of dynamic languages on top of the Graal VM. New projects com.oracle.truffle.api for the API definition and com.oracle.truffle.api.test for API tests and documentation.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
671 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
672 }
a4b84ba6dc2e Introduction of the Truffle API for efficient implementation of dynamic languages on top of the Graal VM. New projects com.oracle.truffle.api for the API definition and com.oracle.truffle.api.test for API tests and documentation.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
673 });
a4b84ba6dc2e Introduction of the Truffle API for efficient implementation of dynamic languages on top of the Graal VM. New projects com.oracle.truffle.api for the API definition and com.oracle.truffle.api.test for API tests and documentation.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
674 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
675 }
a4b84ba6dc2e Introduction of the Truffle API for efficient implementation of dynamic languages on top of the Graal VM. New projects com.oracle.truffle.api for the API definition and com.oracle.truffle.api.test for API tests and documentation.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
676
16465
456abab80eb5 Truffle: remove obsolete NodeUtil.findNodeInstancesInFunction (functionally equivalent to findAllNodeInstances)
Andreas Woess <andreas.woess@jku.at>
parents: 16464
diff changeset
677 /**
456abab80eb5 Truffle: remove obsolete NodeUtil.findNodeInstancesInFunction (functionally equivalent to findAllNodeInstances)
Andreas Woess <andreas.woess@jku.at>
parents: 16464
diff changeset
678 * 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
679 */
456abab80eb5 Truffle: remove obsolete NodeUtil.findNodeInstancesInFunction (functionally equivalent to findAllNodeInstances)
Andreas Woess <andreas.woess@jku.at>
parents: 16464
diff changeset
680 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
681 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
682 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
683 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
684 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
685 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
686 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
687 }
a4b84ba6dc2e Introduction of the Truffle API for efficient implementation of dynamic languages on top of the Graal VM. New projects com.oracle.truffle.api for the API definition and com.oracle.truffle.api.test for API tests and documentation.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
688 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
689 }
a4b84ba6dc2e Introduction of the Truffle API for efficient implementation of dynamic languages on top of the Graal VM. New projects com.oracle.truffle.api for the API definition and com.oracle.truffle.api.test for API tests and documentation.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
690 });
a4b84ba6dc2e Introduction of the Truffle API for efficient implementation of dynamic languages on top of the Graal VM. New projects com.oracle.truffle.api for the API definition and com.oracle.truffle.api.test for API tests and documentation.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
691 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
692 }
a4b84ba6dc2e Introduction of the Truffle API for efficient implementation of dynamic languages on top of the Graal VM. New projects com.oracle.truffle.api for the API definition and com.oracle.truffle.api.test for API tests and documentation.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
693
10704
84b944726df2 Truffle: introduce NodeUtil.countNodes
Andreas Woess <andreas.woess@jku.at>
parents: 9754
diff changeset
694 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
695 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
696 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
697 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
698 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
699 count++;
5787218bad91 Truffle: implemented recursive node iterator and node streams for the graal runtime.
Christian Humer <christian.humer@gmail.com>
parents: 17275
diff changeset
700 }
5787218bad91 Truffle: implemented recursive node iterator and node streams for the graal runtime.
Christian Humer <christian.humer@gmail.com>
parents: 17275
diff changeset
701 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
702 }
e455fc531ec2 Truffle: Added API in NodeUtil to count nodes restricted to a Kind.
Christian Humer <christian.humer@gmail.com>
parents: 13983
diff changeset
703
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
704 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
705 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
706 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
707 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
708 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
709 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
710 count++;
5787218bad91 Truffle: implemented recursive node iterator and node streams for the graal runtime.
Christian Humer <christian.humer@gmail.com>
parents: 17275
diff changeset
711 }
5787218bad91 Truffle: implemented recursive node iterator and node streams for the graal runtime.
Christian Humer <christian.humer@gmail.com>
parents: 17275
diff changeset
712 }
5787218bad91 Truffle: implemented recursive node iterator and node streams for the graal runtime.
Christian Humer <christian.humer@gmail.com>
parents: 17275
diff changeset
713 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
714 }
6e12e0ace0d5 Truffle: add NodeUtil.countNodes overload that counts nodes of a specific type
Andreas Woess <andreas.woess@jku.at>
parents: 10756
diff changeset
715
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
716 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
717
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
718 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
719
10704
84b944726df2 Truffle: introduce NodeUtil.countNodes
Andreas Woess <andreas.woess@jku.at>
parents: 9754
diff changeset
720 }
84b944726df2 Truffle: introduce NodeUtil.countNodes
Andreas Woess <andreas.woess@jku.at>
parents: 9754
diff changeset
721
9754
98b004bf3985 Refactoring of NodeUtil to centralize the use of reflection
Christian Wimmer <christian.wimmer@oracle.com>
parents: 9538
diff changeset
722 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
723 StringWriter out = new StringWriter();
98b004bf3985 Refactoring of NodeUtil to centralize the use of reflection
Christian Wimmer <christian.wimmer@oracle.com>
parents: 9538
diff changeset
724 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
725 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
726 }
a4b84ba6dc2e Introduction of the Truffle API for efficient implementation of dynamic languages on top of the Graal VM. New projects com.oracle.truffle.api for the API definition and com.oracle.truffle.api.test for API tests and documentation.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
727
9754
98b004bf3985 Refactoring of NodeUtil to centralize the use of reflection
Christian Wimmer <christian.wimmer@oracle.com>
parents: 9538
diff changeset
728 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
729 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
730 }
a4b84ba6dc2e Introduction of the Truffle API for efficient implementation of dynamic languages on top of the Graal VM. New projects com.oracle.truffle.api for the API definition and com.oracle.truffle.api.test for API tests and documentation.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
731
9754
98b004bf3985 Refactoring of NodeUtil to centralize the use of reflection
Christian Wimmer <christian.wimmer@oracle.com>
parents: 9538
diff changeset
732 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
733 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
734 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
735 }
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
736 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
737 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
738 }
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
739 if (parent == null) {
10845
e9248ebb1d79 Truffle: ignore null children in NodeUtil.findNodeChildren; refactoring.
Andreas Woess <andreas.woess@jku.at>
parents: 10793
diff changeset
740 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
741 } else {
17275
846c059e3ecf Truffle: allow interface types in child fields
Andreas Woess <andreas.woess@jku.at>
parents: 16779
diff changeset
742 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
743 p.print(" = ");
10845
e9248ebb1d79 Truffle: ignore null children in NodeUtil.findNodeChildren; refactoring.
Andreas Woess <andreas.woess@jku.at>
parents: 10793
diff changeset
744 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
745 }
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
746
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
747 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
748 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
749 }
9754
98b004bf3985 Refactoring of NodeUtil to centralize the use of reflection
Christian Wimmer <christian.wimmer@oracle.com>
parents: 9538
diff changeset
750 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
751 }
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
752
11848
099af41815ea Truffle: add NodeUtil.printSourceAttributionTree() for debugging
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 11809
diff changeset
753 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
754 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
755 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
756 return out.toString();
099af41815ea Truffle: add NodeUtil.printSourceAttributionTree() for debugging
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 11809
diff changeset
757 }
099af41815ea Truffle: add NodeUtil.printSourceAttributionTree() for debugging
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 11809
diff changeset
758
099af41815ea Truffle: add NodeUtil.printSourceAttributionTree() for debugging
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 11809
diff changeset
759 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
760 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
761 }
099af41815ea Truffle: add NodeUtil.printSourceAttributionTree() for debugging
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 11809
diff changeset
762
099af41815ea Truffle: add NodeUtil.printSourceAttributionTree() for debugging
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 11809
diff changeset
763 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
764 if (node == null) {
099af41815ea Truffle: add NodeUtil.printSourceAttributionTree() for debugging
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 11809
diff changeset
765 return;
099af41815ea Truffle: add NodeUtil.printSourceAttributionTree() for debugging
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 11809
diff changeset
766 }
099af41815ea Truffle: add NodeUtil.printSourceAttributionTree() for debugging
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 11809
diff changeset
767 if (parent == null) {
099af41815ea Truffle: add NodeUtil.printSourceAttributionTree() for debugging
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 11809
diff changeset
768 // 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
769 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
770 if (sourceSection != null) {
099af41815ea Truffle: add NodeUtil.printSourceAttributionTree() for debugging
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 11809
diff changeset
771 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
772 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
773 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
774 }
099af41815ea Truffle: add NodeUtil.printSourceAttributionTree() for debugging
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 11809
diff changeset
775 }
099af41815ea Truffle: add NodeUtil.printSourceAttributionTree() for debugging
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 11809
diff changeset
776 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
777 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
778 sb.append("| ");
11848
099af41815ea Truffle: add NodeUtil.printSourceAttributionTree() for debugging
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 11809
diff changeset
779 }
099af41815ea Truffle: add NodeUtil.printSourceAttributionTree() for debugging
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 11809
diff changeset
780
099af41815ea Truffle: add NodeUtil.printSourceAttributionTree() for debugging
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 11809
diff changeset
781 if (parent != null) {
17275
846c059e3ecf Truffle: allow interface types in child fields
Andreas Woess <andreas.woess@jku.at>
parents: 16779
diff changeset
782 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
783 }
099af41815ea Truffle: add NodeUtil.printSourceAttributionTree() for debugging
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 11809
diff changeset
784
099af41815ea Truffle: add NodeUtil.printSourceAttributionTree() for debugging
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 11809
diff changeset
785 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
786
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
787 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
788
11848
099af41815ea Truffle: add NodeUtil.printSourceAttributionTree() for debugging
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 11809
diff changeset
789 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
790 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
791
099af41815ea Truffle: add NodeUtil.printSourceAttributionTree() for debugging
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 11809
diff changeset
792 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
793 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
794 }
099af41815ea Truffle: add NodeUtil.printSourceAttributionTree() for debugging
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 11809
diff changeset
795 p.flush();
099af41815ea Truffle: add NodeUtil.printSourceAttributionTree() for debugging
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 11809
diff changeset
796 }
099af41815ea Truffle: add NodeUtil.printSourceAttributionTree() for debugging
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 11809
diff changeset
797
17275
846c059e3ecf Truffle: allow interface types in child fields
Andreas Woess <andreas.woess@jku.at>
parents: 16779
diff changeset
798 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
799 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
800 for (NodeField field : fields) {
846c059e3ecf Truffle: allow interface types in child fields
Andreas Woess <andreas.woess@jku.at>
parents: 16779
diff changeset
801 Object value = field.loadValue(parent);
846c059e3ecf Truffle: allow interface types in child fields
Andreas Woess <andreas.woess@jku.at>
parents: 16779
diff changeset
802 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
803 return field.getName();
846c059e3ecf Truffle: allow interface types in child fields
Andreas Woess <andreas.woess@jku.at>
parents: 16779
diff changeset
804 } else if (field.getKind() == NodeFieldKind.CHILDREN) {
846c059e3ecf Truffle: allow interface types in child fields
Andreas Woess <andreas.woess@jku.at>
parents: 16779
diff changeset
805 int index = 0;
846c059e3ecf Truffle: allow interface types in child fields
Andreas Woess <andreas.woess@jku.at>
parents: 16779
diff changeset
806 for (Object arrayNode : (Object[]) value) {
846c059e3ecf Truffle: allow interface types in child fields
Andreas Woess <andreas.woess@jku.at>
parents: 16779
diff changeset
807 if (arrayNode == node) {
846c059e3ecf Truffle: allow interface types in child fields
Andreas Woess <andreas.woess@jku.at>
parents: 16779
diff changeset
808 return field.getName() + "[" + index + "]";
846c059e3ecf Truffle: allow interface types in child fields
Andreas Woess <andreas.woess@jku.at>
parents: 16779
diff changeset
809 }
846c059e3ecf Truffle: allow interface types in child fields
Andreas Woess <andreas.woess@jku.at>
parents: 16779
diff changeset
810 index++;
846c059e3ecf Truffle: allow interface types in child fields
Andreas Woess <andreas.woess@jku.at>
parents: 16779
diff changeset
811 }
846c059e3ecf Truffle: allow interface types in child fields
Andreas Woess <andreas.woess@jku.at>
parents: 16779
diff changeset
812 }
846c059e3ecf Truffle: allow interface types in child fields
Andreas Woess <andreas.woess@jku.at>
parents: 16779
diff changeset
813 }
846c059e3ecf Truffle: allow interface types in child fields
Andreas Woess <andreas.woess@jku.at>
parents: 16779
diff changeset
814 return defaultName;
846c059e3ecf Truffle: allow interface types in child fields
Andreas Woess <andreas.woess@jku.at>
parents: 16779
diff changeset
815 }
846c059e3ecf Truffle: allow interface types in child fields
Andreas Woess <andreas.woess@jku.at>
parents: 16779
diff changeset
816
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
817 /**
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
818 * 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
819 * 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
820 * <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
821 * <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
822 * {@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
823 * <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
824 * <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
825 * </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
826 */
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
827 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
828 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
829 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
830 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
831 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
832 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
833 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
834 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
835 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
836 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
837 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
838 }
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
839 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
840 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
841 }
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
842 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
843 }
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
844
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
845 /**
9754
98b004bf3985 Refactoring of NodeUtil to centralize the use of reflection
Christian Wimmer <christian.wimmer@oracle.com>
parents: 9538
diff changeset
846 * 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
847 * 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
848 *
9754
98b004bf3985 Refactoring of NodeUtil to centralize the use of reflection
Christian Wimmer <christian.wimmer@oracle.com>
parents: 9538
diff changeset
849 * @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
850 * @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
851 */
9754
98b004bf3985 Refactoring of NodeUtil to centralize the use of reflection
Christian Wimmer <christian.wimmer@oracle.com>
parents: 9538
diff changeset
852 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
853 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
854 }
a4b84ba6dc2e Introduction of the Truffle API for efficient implementation of dynamic languages on top of the Graal VM. New projects com.oracle.truffle.api 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
9754
98b004bf3985 Refactoring of NodeUtil to centralize the use of reflection
Christian Wimmer <christian.wimmer@oracle.com>
parents: 9538
diff changeset
856 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
857 StringWriter out = new StringWriter();
98b004bf3985 Refactoring of NodeUtil to centralize the use of reflection
Christian Wimmer <christian.wimmer@oracle.com>
parents: 9538
diff changeset
858 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
859 return out.toString();
98b004bf3985 Refactoring of NodeUtil to centralize the use of reflection
Christian Wimmer <christian.wimmer@oracle.com>
parents: 9538
diff changeset
860 }
98b004bf3985 Refactoring of NodeUtil to centralize the use of reflection
Christian Wimmer <christian.wimmer@oracle.com>
parents: 9538
diff changeset
861
98b004bf3985 Refactoring of NodeUtil to centralize the use of reflection
Christian Wimmer <christian.wimmer@oracle.com>
parents: 9538
diff changeset
862 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
863 printTree(p, node, 1);
98b004bf3985 Refactoring of NodeUtil to centralize the use of reflection
Christian Wimmer <christian.wimmer@oracle.com>
parents: 9538
diff changeset
864 p.println();
98b004bf3985 Refactoring of NodeUtil to centralize the use of reflection
Christian Wimmer <christian.wimmer@oracle.com>
parents: 9538
diff changeset
865 p.flush();
98b004bf3985 Refactoring of NodeUtil to centralize the use of reflection
Christian Wimmer <christian.wimmer@oracle.com>
parents: 9538
diff changeset
866 }
98b004bf3985 Refactoring of NodeUtil to centralize the use of reflection
Christian Wimmer <christian.wimmer@oracle.com>
parents: 9538
diff changeset
867
98b004bf3985 Refactoring of NodeUtil to centralize the use of reflection
Christian Wimmer <christian.wimmer@oracle.com>
parents: 9538
diff changeset
868 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
869 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
870 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
871 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
872 }
a4b84ba6dc2e Introduction of the Truffle API for efficient implementation of dynamic languages on top of the Graal VM. New projects com.oracle.truffle.api for the API definition and com.oracle.truffle.api.test for API tests and documentation.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
873
10845
e9248ebb1d79 Truffle: ignore null children in NodeUtil.findNodeChildren; refactoring.
Andreas Woess <andreas.woess@jku.at>
parents: 10793
diff changeset
874 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
875
9754
98b004bf3985 Refactoring of NodeUtil to centralize the use of reflection
Christian Wimmer <christian.wimmer@oracle.com>
parents: 9538
diff changeset
876 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
877 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
878 p.print("(");
9754
98b004bf3985 Refactoring of NodeUtil to centralize the use of reflection
Christian Wimmer <christian.wimmer@oracle.com>
parents: 9538
diff changeset
879 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
880 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
881 childFields.add(field);
98b004bf3985 Refactoring of NodeUtil to centralize the use of reflection
Christian Wimmer <christian.wimmer@oracle.com>
parents: 9538
diff changeset
882 } 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
883 p.print(sep);
98b004bf3985 Refactoring of NodeUtil to centralize the use of reflection
Christian Wimmer <christian.wimmer@oracle.com>
parents: 9538
diff changeset
884 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
885
9754
98b004bf3985 Refactoring of NodeUtil to centralize the use of reflection
Christian Wimmer <christian.wimmer@oracle.com>
parents: 9538
diff changeset
886 p.print(field.getName());
98b004bf3985 Refactoring of NodeUtil to centralize the use of reflection
Christian Wimmer <christian.wimmer@oracle.com>
parents: 9538
diff changeset
887 p.print(" = ");
98b004bf3985 Refactoring of NodeUtil to centralize the use of reflection
Christian Wimmer <christian.wimmer@oracle.com>
parents: 9538
diff changeset
888 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
889 }
a4b84ba6dc2e Introduction of the Truffle API for efficient implementation of dynamic languages on top of the Graal VM. New projects com.oracle.truffle.api for the API definition and com.oracle.truffle.api.test for API tests and documentation.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
890 }
a4b84ba6dc2e Introduction of the Truffle API for efficient implementation of dynamic languages on top of the Graal VM. New projects com.oracle.truffle.api for the API definition and com.oracle.truffle.api.test for API tests and documentation.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
891 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
892
a4b84ba6dc2e Introduction of the Truffle API for efficient implementation of dynamic languages on top of the Graal VM. New projects com.oracle.truffle.api for the API definition and com.oracle.truffle.api.test for API tests and documentation.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
893 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
894 p.print(" {");
9754
98b004bf3985 Refactoring of NodeUtil to centralize the use of reflection
Christian Wimmer <christian.wimmer@oracle.com>
parents: 9538
diff changeset
895 for (NodeField field : childFields) {
98b004bf3985 Refactoring of NodeUtil to centralize the use of reflection
Christian Wimmer <christian.wimmer@oracle.com>
parents: 9538
diff changeset
896 printNewLine(p, level);
98b004bf3985 Refactoring of NodeUtil to centralize the use of reflection
Christian Wimmer <christian.wimmer@oracle.com>
parents: 9538
diff changeset
897 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
898
9754
98b004bf3985 Refactoring of NodeUtil to centralize the use of reflection
Christian Wimmer <christian.wimmer@oracle.com>
parents: 9538
diff changeset
899 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
900 if (value == null) {
98b004bf3985 Refactoring of NodeUtil to centralize the use of reflection
Christian Wimmer <christian.wimmer@oracle.com>
parents: 9538
diff changeset
901 p.print(" = null ");
98b004bf3985 Refactoring of NodeUtil to centralize the use of reflection
Christian Wimmer <christian.wimmer@oracle.com>
parents: 9538
diff changeset
902 } 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
903 p.print(" = ");
98b004bf3985 Refactoring of NodeUtil to centralize the use of reflection
Christian Wimmer <christian.wimmer@oracle.com>
parents: 9538
diff changeset
904 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
905 } else if (field.getKind() == NodeFieldKind.CHILDREN) {
16548
a3b0a2d61e62 extract method in NodeUtil
Christian Wirth <christian.wirth@oracle.com>
parents: 16513
diff changeset
906 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
907 }
a4b84ba6dc2e Introduction of the Truffle API for efficient implementation of dynamic languages on top of the Graal VM. New projects com.oracle.truffle.api for the API definition and com.oracle.truffle.api.test for API tests and documentation.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
908 }
a4b84ba6dc2e Introduction of the Truffle API for efficient implementation of dynamic languages on top of the Graal VM. New projects com.oracle.truffle.api for the API definition and com.oracle.truffle.api.test for API tests and documentation.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
909 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
910 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
911 }
a4b84ba6dc2e Introduction of the Truffle API for efficient implementation of dynamic languages on top of the Graal VM. New projects com.oracle.truffle.api for the API definition and com.oracle.truffle.api.test for API tests and documentation.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
912 }
a4b84ba6dc2e Introduction of the Truffle API for efficient implementation of dynamic languages on top of the Graal VM. New projects com.oracle.truffle.api for the API definition and com.oracle.truffle.api.test for API tests and documentation.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
913
16548
a3b0a2d61e62 extract method in NodeUtil
Christian Wirth <christian.wirth@oracle.com>
parents: 16513
diff changeset
914 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
915 String sep;
17275
846c059e3ecf Truffle: allow interface types in child fields
Andreas Woess <andreas.woess@jku.at>
parents: 16779
diff changeset
916 Object[] children = (Object[]) value;
16548
a3b0a2d61e62 extract method in NodeUtil
Christian Wirth <christian.wirth@oracle.com>
parents: 16513
diff changeset
917 p.print(" = [");
a3b0a2d61e62 extract method in NodeUtil
Christian Wirth <christian.wirth@oracle.com>
parents: 16513
diff changeset
918 sep = "";
17275
846c059e3ecf Truffle: allow interface types in child fields
Andreas Woess <andreas.woess@jku.at>
parents: 16779
diff changeset
919 for (Object child : children) {
16548
a3b0a2d61e62 extract method in NodeUtil
Christian Wirth <christian.wirth@oracle.com>
parents: 16513
diff changeset
920 p.print(sep);
a3b0a2d61e62 extract method in NodeUtil
Christian Wirth <christian.wirth@oracle.com>
parents: 16513
diff changeset
921 sep = ", ";
17275
846c059e3ecf Truffle: allow interface types in child fields
Andreas Woess <andreas.woess@jku.at>
parents: 16779
diff changeset
922 printTree(p, (Node) child, level + 1);
16548
a3b0a2d61e62 extract method in NodeUtil
Christian Wirth <christian.wirth@oracle.com>
parents: 16513
diff changeset
923 }
a3b0a2d61e62 extract method in NodeUtil
Christian Wirth <christian.wirth@oracle.com>
parents: 16513
diff changeset
924 p.print("]");
a3b0a2d61e62 extract method in NodeUtil
Christian Wirth <christian.wirth@oracle.com>
parents: 16513
diff changeset
925 }
a3b0a2d61e62 extract method in NodeUtil
Christian Wirth <christian.wirth@oracle.com>
parents: 16513
diff changeset
926
9754
98b004bf3985 Refactoring of NodeUtil to centralize the use of reflection
Christian Wimmer <christian.wimmer@oracle.com>
parents: 9538
diff changeset
927 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
928 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
929 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
930 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
931 }
a4b84ba6dc2e Introduction of the Truffle API for efficient implementation of dynamic languages on top of the Graal VM. New projects com.oracle.truffle.api for the API definition and com.oracle.truffle.api.test for API tests and documentation.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
932 }
10742
99789440ce28 Truffle: Added assertions to replaceChild.
Christian Humer <christian.humer@gmail.com>
parents: 10704
diff changeset
933
10845
e9248ebb1d79 Truffle: ignore null children in NodeUtil.findNodeChildren; refactoring.
Andreas Woess <andreas.woess@jku.at>
parents: 10793
diff changeset
934 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
935 return node.getClass().getSimpleName();
e9248ebb1d79 Truffle: ignore null children in NodeUtil.findNodeChildren; refactoring.
Andreas Woess <andreas.woess@jku.at>
parents: 10793
diff changeset
936 }
11848
099af41815ea Truffle: add NodeUtil.printSourceAttributionTree() for debugging
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 11809
diff changeset
937
099af41815ea Truffle: add NodeUtil.printSourceAttributionTree() for debugging
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 11809
diff changeset
938 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
939 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
940 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
941 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
942 }
11848
099af41815ea Truffle: add NodeUtil.printSourceAttributionTree() for debugging
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 11809
diff changeset
943 if (section != null) {
099af41815ea Truffle: add NodeUtil.printSourceAttributionTree() for debugging
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 11809
diff changeset
944 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
945 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
946 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
947 sb.append(" (" + section.getCharIndex() + "," + (section.getCharEndIndex() - 1) + ")");
16674
70f47dbbcabd Truffle/SourceAttribution: NodeUtil.displaySourceAttribution() outut revised for legibility.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16581
diff changeset
948 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
949 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
950 return sb.toString();
099af41815ea Truffle: add NodeUtil.printSourceAttributionTree() for debugging
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 11809
diff changeset
951 }
099af41815ea Truffle: add NodeUtil.printSourceAttributionTree() for debugging
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 11809
diff changeset
952 return "";
099af41815ea Truffle: add NodeUtil.printSourceAttributionTree() for debugging
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 11809
diff changeset
953 }
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
954
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
955 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
956 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
957 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
958 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
959 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
960 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
961 }
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
962 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
963 }
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
964 }
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
965 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
966 }
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
967
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
968 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
969 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
970 }
16464
76081918079d Truffle: move TraceRewrites code to NodeUtil
Andreas Woess <andreas.woess@jku.at>
parents: 16067
diff changeset
971
76081918079d Truffle: move TraceRewrites code to NodeUtil
Andreas Woess <andreas.woess@jku.at>
parents: 16067
diff changeset
972 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
973 if (TruffleOptions.TraceRewritesFilterFromCost != null) {
76081918079d Truffle: move TraceRewrites code to NodeUtil
Andreas Woess <andreas.woess@jku.at>
parents: 16067
diff changeset
974 if (filterByKind(oldNode, TruffleOptions.TraceRewritesFilterFromCost)) {
76081918079d Truffle: move TraceRewrites code to NodeUtil
Andreas Woess <andreas.woess@jku.at>
parents: 16067
diff changeset
975 return;
76081918079d Truffle: move TraceRewrites code to NodeUtil
Andreas Woess <andreas.woess@jku.at>
parents: 16067
diff changeset
976 }
76081918079d Truffle: move TraceRewrites code to NodeUtil
Andreas Woess <andreas.woess@jku.at>
parents: 16067
diff changeset
977 }
76081918079d Truffle: move TraceRewrites code to NodeUtil
Andreas Woess <andreas.woess@jku.at>
parents: 16067
diff changeset
978
76081918079d Truffle: move TraceRewrites code to NodeUtil
Andreas Woess <andreas.woess@jku.at>
parents: 16067
diff changeset
979 if (TruffleOptions.TraceRewritesFilterToCost != null) {
76081918079d Truffle: move TraceRewrites code to NodeUtil
Andreas Woess <andreas.woess@jku.at>
parents: 16067
diff changeset
980 if (filterByKind(newNode, TruffleOptions.TraceRewritesFilterToCost)) {
76081918079d Truffle: move TraceRewrites code to NodeUtil
Andreas Woess <andreas.woess@jku.at>
parents: 16067
diff changeset
981 return;
76081918079d Truffle: move TraceRewrites code to NodeUtil
Andreas Woess <andreas.woess@jku.at>
parents: 16067
diff changeset
982 }
76081918079d Truffle: move TraceRewrites code to NodeUtil
Andreas Woess <andreas.woess@jku.at>
parents: 16067
diff changeset
983 }
76081918079d Truffle: move TraceRewrites code to NodeUtil
Andreas Woess <andreas.woess@jku.at>
parents: 16067
diff changeset
984
76081918079d Truffle: move TraceRewrites code to NodeUtil
Andreas Woess <andreas.woess@jku.at>
parents: 16067
diff changeset
985 String filter = TruffleOptions.TraceRewritesFilterClass;
76081918079d Truffle: move TraceRewrites code to NodeUtil
Andreas Woess <andreas.woess@jku.at>
parents: 16067
diff changeset
986 Class<? extends Node> from = oldNode.getClass();
76081918079d Truffle: move TraceRewrites code to NodeUtil
Andreas Woess <andreas.woess@jku.at>
parents: 16067
diff changeset
987 Class<? extends Node> to = newNode.getClass();
76081918079d Truffle: move TraceRewrites code to NodeUtil
Andreas Woess <andreas.woess@jku.at>
parents: 16067
diff changeset
988 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
989 return;
76081918079d Truffle: move TraceRewrites code to NodeUtil
Andreas Woess <andreas.woess@jku.at>
parents: 16067
diff changeset
990 }
76081918079d Truffle: move TraceRewrites code to NodeUtil
Andreas Woess <andreas.woess@jku.at>
parents: 16067
diff changeset
991
76081918079d Truffle: move TraceRewrites code to NodeUtil
Andreas Woess <andreas.woess@jku.at>
parents: 16067
diff changeset
992 final SourceSection reportedSourceSection = oldNode.getEncapsulatingSourceSection();
76081918079d Truffle: move TraceRewrites code to NodeUtil
Andreas Woess <andreas.woess@jku.at>
parents: 16067
diff changeset
993
76081918079d Truffle: move TraceRewrites code to NodeUtil
Andreas Woess <andreas.woess@jku.at>
parents: 16067
diff changeset
994 PrintStream out = System.out;
76081918079d Truffle: move TraceRewrites code to NodeUtil
Andreas Woess <andreas.woess@jku.at>
parents: 16067
diff changeset
995 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
996 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
997 }
76081918079d Truffle: move TraceRewrites code to NodeUtil
Andreas Woess <andreas.woess@jku.at>
parents: 16067
diff changeset
998
76081918079d Truffle: move TraceRewrites code to NodeUtil
Andreas Woess <andreas.woess@jku.at>
parents: 16067
diff changeset
999 private static String formatNodeInfo(Node node) {
76081918079d Truffle: move TraceRewrites code to NodeUtil
Andreas Woess <andreas.woess@jku.at>
parents: 16067
diff changeset
1000 String cost = "?";
76081918079d Truffle: move TraceRewrites code to NodeUtil
Andreas Woess <andreas.woess@jku.at>
parents: 16067
diff changeset
1001 switch (node.getCost()) {
76081918079d Truffle: move TraceRewrites code to NodeUtil
Andreas Woess <andreas.woess@jku.at>
parents: 16067
diff changeset
1002 case NONE:
76081918079d Truffle: move TraceRewrites code to NodeUtil
Andreas Woess <andreas.woess@jku.at>
parents: 16067
diff changeset
1003 cost = "G";
76081918079d Truffle: move TraceRewrites code to NodeUtil
Andreas Woess <andreas.woess@jku.at>
parents: 16067
diff changeset
1004 break;
76081918079d Truffle: move TraceRewrites code to NodeUtil
Andreas Woess <andreas.woess@jku.at>
parents: 16067
diff changeset
1005 case MONOMORPHIC:
76081918079d Truffle: move TraceRewrites code to NodeUtil
Andreas Woess <andreas.woess@jku.at>
parents: 16067
diff changeset
1006 cost = "M";
76081918079d Truffle: move TraceRewrites code to NodeUtil
Andreas Woess <andreas.woess@jku.at>
parents: 16067
diff changeset
1007 break;
76081918079d Truffle: move TraceRewrites code to NodeUtil
Andreas Woess <andreas.woess@jku.at>
parents: 16067
diff changeset
1008 case POLYMORPHIC:
76081918079d Truffle: move TraceRewrites code to NodeUtil
Andreas Woess <andreas.woess@jku.at>
parents: 16067
diff changeset
1009 cost = "P";
76081918079d Truffle: move TraceRewrites code to NodeUtil
Andreas Woess <andreas.woess@jku.at>
parents: 16067
diff changeset
1010 break;
76081918079d Truffle: move TraceRewrites code to NodeUtil
Andreas Woess <andreas.woess@jku.at>
parents: 16067
diff changeset
1011 case MEGAMORPHIC:
76081918079d Truffle: move TraceRewrites code to NodeUtil
Andreas Woess <andreas.woess@jku.at>
parents: 16067
diff changeset
1012 cost = "G";
76081918079d Truffle: move TraceRewrites code to NodeUtil
Andreas Woess <andreas.woess@jku.at>
parents: 16067
diff changeset
1013 break;
76081918079d Truffle: move TraceRewrites code to NodeUtil
Andreas Woess <andreas.woess@jku.at>
parents: 16067
diff changeset
1014 default:
76081918079d Truffle: move TraceRewrites code to NodeUtil
Andreas Woess <andreas.woess@jku.at>
parents: 16067
diff changeset
1015 cost = "?";
76081918079d Truffle: move TraceRewrites code to NodeUtil
Andreas Woess <andreas.woess@jku.at>
parents: 16067
diff changeset
1016 break;
76081918079d Truffle: move TraceRewrites code to NodeUtil
Andreas Woess <andreas.woess@jku.at>
parents: 16067
diff changeset
1017 }
76081918079d Truffle: move TraceRewrites code to NodeUtil
Andreas Woess <andreas.woess@jku.at>
parents: 16067
diff changeset
1018 return cost + " " + node.getClass().getSimpleName();
76081918079d Truffle: move TraceRewrites code to NodeUtil
Andreas Woess <andreas.woess@jku.at>
parents: 16067
diff changeset
1019 }
76081918079d Truffle: move TraceRewrites code to NodeUtil
Andreas Woess <andreas.woess@jku.at>
parents: 16067
diff changeset
1020
76081918079d Truffle: move TraceRewrites code to NodeUtil
Andreas Woess <andreas.woess@jku.at>
parents: 16067
diff changeset
1021 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
1022 return node.getCost() == cost;
76081918079d Truffle: move TraceRewrites code to NodeUtil
Andreas Woess <andreas.woess@jku.at>
parents: 16067
diff changeset
1023 }
76081918079d Truffle: move TraceRewrites code to NodeUtil
Andreas Woess <andreas.woess@jku.at>
parents: 16067
diff changeset
1024
76081918079d Truffle: move TraceRewrites code to NodeUtil
Andreas Woess <andreas.woess@jku.at>
parents: 16067
diff changeset
1025 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
1026 Class<?> currentFrom = from;
76081918079d Truffle: move TraceRewrites code to NodeUtil
Andreas Woess <andreas.woess@jku.at>
parents: 16067
diff changeset
1027 while (currentFrom != null) {
76081918079d Truffle: move TraceRewrites code to NodeUtil
Andreas Woess <andreas.woess@jku.at>
parents: 16067
diff changeset
1028 if (currentFrom.getName().contains(filter)) {
76081918079d Truffle: move TraceRewrites code to NodeUtil
Andreas Woess <andreas.woess@jku.at>
parents: 16067
diff changeset
1029 return false;
76081918079d Truffle: move TraceRewrites code to NodeUtil
Andreas Woess <andreas.woess@jku.at>
parents: 16067
diff changeset
1030 }
76081918079d Truffle: move TraceRewrites code to NodeUtil
Andreas Woess <andreas.woess@jku.at>
parents: 16067
diff changeset
1031 currentFrom = currentFrom.getSuperclass();
76081918079d Truffle: move TraceRewrites code to NodeUtil
Andreas Woess <andreas.woess@jku.at>
parents: 16067
diff changeset
1032 }
76081918079d Truffle: move TraceRewrites code to NodeUtil
Andreas Woess <andreas.woess@jku.at>
parents: 16067
diff changeset
1033 return true;
76081918079d Truffle: move TraceRewrites code to NodeUtil
Andreas Woess <andreas.woess@jku.at>
parents: 16067
diff changeset
1034 }
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
1035 }