comparison graal/com.oracle.truffle.api.dsl.test/src/com/oracle/truffle/api/dsl/test/examples/ExampleNode.java @ 19301:a79a3e467245

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