annotate truffle/com.oracle.truffle.api.dsl.test/src/com/oracle/truffle/api/dsl/test/examples/ExampleNode.java @ 21951:9c8c0937da41

Moving all sources into truffle subdirectory
author Jaroslav Tulach <jaroslav.tulach@oracle.com>
date Wed, 17 Jun 2015 10:58:08 +0200
parents graal/com.oracle.truffle.api.dsl.test/src/com/oracle/truffle/api/dsl/test/examples/ExampleNode.java@a79a3e467245
children 5bc7f7b867ab
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
19301
a79a3e467245 Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
1 /*
a79a3e467245 Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
2 * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
a79a3e467245 Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
a79a3e467245 Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
4 *
a79a3e467245 Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
5 * This code is free software; you can redistribute it and/or modify it
a79a3e467245 Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
6 * under the terms of the GNU General Public License version 2 only, as
a79a3e467245 Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
7 * published by the Free Software Foundation.
a79a3e467245 Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
8 *
a79a3e467245 Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
9 * This code is distributed in the hope that it will be useful, but WITHOUT
a79a3e467245 Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
a79a3e467245 Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
a79a3e467245 Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
12 * version 2 for more details (a copy is included in the LICENSE file that
a79a3e467245 Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
13 * accompanied this code).
a79a3e467245 Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
14 *
a79a3e467245 Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
15 * You should have received a copy of the GNU General Public License version
a79a3e467245 Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
16 * 2 along with this work; if not, write to the Free Software Foundation,
a79a3e467245 Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
a79a3e467245 Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
18 *
a79a3e467245 Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
a79a3e467245 Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
20 * or visit www.oracle.com if you need additional information or have any
a79a3e467245 Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
21 * questions.
a79a3e467245 Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
22 */
a79a3e467245 Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
23 package com.oracle.truffle.api.dsl.test.examples;
a79a3e467245 Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
24
a79a3e467245 Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
25 import com.oracle.truffle.api.*;
a79a3e467245 Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
26 import com.oracle.truffle.api.dsl.*;
a79a3e467245 Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
27 import com.oracle.truffle.api.dsl.internal.*;
a79a3e467245 Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
28 import com.oracle.truffle.api.frame.*;
a79a3e467245 Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
29 import com.oracle.truffle.api.nodes.*;
a79a3e467245 Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
30
a79a3e467245 Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
31 @TypeSystemReference(ExampleTypes.class)
a79a3e467245 Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
32 @NodeChild(value = "args", type = ExampleNode[].class)
a79a3e467245 Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
33 public abstract class ExampleNode extends Node {
a79a3e467245 Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
34
a79a3e467245 Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
35 public Object execute(@SuppressWarnings("unused") VirtualFrame frame) {
a79a3e467245 Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
36 // will get implemented by the DSL.
a79a3e467245 Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
37 throw new UnsupportedOperationException();
a79a3e467245 Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
38 }
a79a3e467245 Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
39
a79a3e467245 Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
40 @Override
a79a3e467245 Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
41 public String toString() {
a79a3e467245 Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
42 if (this instanceof SpecializedNode) {
a79a3e467245 Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
43 return ((SpecializedNode) this).getSpecializationNode().toString();
a79a3e467245 Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
44 } else {
a79a3e467245 Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
45 return super.toString();
a79a3e467245 Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
46 }
a79a3e467245 Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
47 }
a79a3e467245 Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
48
a79a3e467245 Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
49 public static CallTarget createTarget(ExampleNode node) {
a79a3e467245 Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
50 return Truffle.getRuntime().createCallTarget(new ExampleRootNode(node));
a79a3e467245 Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
51 }
a79a3e467245 Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
52
a79a3e467245 Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
53 @SuppressWarnings("unchecked")
a79a3e467245 Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
54 public static <T> T getNode(CallTarget target) {
a79a3e467245 Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
55 return (T) ((ExampleRootNode) ((RootCallTarget) target).getRootNode()).child;
a79a3e467245 Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
56 }
a79a3e467245 Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
57
a79a3e467245 Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
58 public static ExampleNode[] createArguments(int count) {
a79a3e467245 Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
59 ExampleNode[] nodes = new ExampleNode[count];
a79a3e467245 Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
60 for (int i = 0; i < count; i++) {
a79a3e467245 Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
61 nodes[i] = new ExampleArgumentNode(i);
a79a3e467245 Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
62 }
a79a3e467245 Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
63 return nodes;
a79a3e467245 Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
64 }
a79a3e467245 Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
65
a79a3e467245 Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
66 private static class ExampleRootNode extends RootNode {
a79a3e467245 Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
67
a79a3e467245 Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
68 @Child ExampleNode child;
a79a3e467245 Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
69
a79a3e467245 Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
70 public ExampleRootNode(ExampleNode child) {
a79a3e467245 Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
71 this.child = child;
a79a3e467245 Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
72 }
a79a3e467245 Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
73
a79a3e467245 Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
74 @Override
a79a3e467245 Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
75 public Object execute(VirtualFrame frame) {
a79a3e467245 Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
76 return child.execute(frame);
a79a3e467245 Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
77 }
a79a3e467245 Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
78
a79a3e467245 Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
79 }
a79a3e467245 Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
80
a79a3e467245 Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
81 private static class ExampleArgumentNode extends ExampleNode {
a79a3e467245 Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
82
a79a3e467245 Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
83 private final int index;
a79a3e467245 Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
84
a79a3e467245 Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
85 public ExampleArgumentNode(int index) {
a79a3e467245 Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
86 this.index = index;
a79a3e467245 Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
87 }
a79a3e467245 Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
88
a79a3e467245 Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
89 @Override
a79a3e467245 Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
90 public Object execute(VirtualFrame frame) {
a79a3e467245 Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
91 Object[] arguments = frame.getArguments();
a79a3e467245 Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
92 if (index < arguments.length) {
a79a3e467245 Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
93 return arguments[index];
a79a3e467245 Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
94 }
a79a3e467245 Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
95 return null;
a79a3e467245 Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
96 }
a79a3e467245 Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
97 }
a79a3e467245 Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
98
a79a3e467245 Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
99 public static CallTarget createDummyTarget(int argumentIndex) {
a79a3e467245 Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
100 return Truffle.getRuntime().createCallTarget(new DummyCallRootNode(argumentIndex));
a79a3e467245 Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
101 }
a79a3e467245 Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
102
a79a3e467245 Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
103 private static class DummyCallRootNode extends RootNode {
a79a3e467245 Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
104
a79a3e467245 Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
105 private final int argumentIndex;
a79a3e467245 Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
106
a79a3e467245 Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
107 public DummyCallRootNode(int argumentIndex) {
a79a3e467245 Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
108 this.argumentIndex = argumentIndex;
a79a3e467245 Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
109 }
a79a3e467245 Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
110
a79a3e467245 Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
111 @Override
a79a3e467245 Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
112 public Object execute(VirtualFrame frame) {
a79a3e467245 Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
113 return frame.getArguments()[argumentIndex];
a79a3e467245 Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
114 }
a79a3e467245 Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
115
a79a3e467245 Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
116 @Override
a79a3e467245 Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
117 public String toString() {
a79a3e467245 Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
118 return "DummyRootNode[arg = " + argumentIndex + "]";
a79a3e467245 Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
119 }
a79a3e467245 Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
120
a79a3e467245 Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
121 }
a79a3e467245 Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
122
a79a3e467245 Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
123 }