Mercurial > hg > truffle
comparison truffle/com.oracle.truffle.tck/src/com/oracle/truffle/tck/ComplexNumbersA.java @ 22495:aeba89e1d8da
Add ComplexNumber sequence tests
author | Matthias Grimmer <grimmer@ssw.jku.at> |
---|---|
date | Fri, 11 Dec 2015 15:20:27 +0100 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
22489:28227895fa35 | 22495:aeba89e1d8da |
---|---|
1 /* | |
2 * Copyright (c) 2015, 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. Oracle designates this | |
8 * particular file as subject to the "Classpath" exception as provided | |
9 * by Oracle in the LICENSE file that accompanied this code. | |
10 * | |
11 * This code is distributed in the hope that it will be useful, but WITHOUT | |
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | |
14 * version 2 for more details (a copy is included in the LICENSE file that | |
15 * accompanied this code). | |
16 * | |
17 * You should have received a copy of the GNU General Public License version | |
18 * 2 along with this work; if not, write to the Free Software Foundation, | |
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. | |
20 * | |
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA | |
22 * or visit www.oracle.com if you need additional information or have any | |
23 * questions. | |
24 */ | |
25 package com.oracle.truffle.tck; | |
26 | |
27 import com.oracle.truffle.api.CallTarget; | |
28 import com.oracle.truffle.api.CompilerDirectives; | |
29 import com.oracle.truffle.api.Truffle; | |
30 import com.oracle.truffle.api.frame.VirtualFrame; | |
31 import com.oracle.truffle.api.interop.ForeignAccess; | |
32 import com.oracle.truffle.api.interop.ForeignAccess.Factory; | |
33 import com.oracle.truffle.api.interop.Message; | |
34 import com.oracle.truffle.api.interop.TruffleObject; | |
35 import com.oracle.truffle.api.nodes.Node; | |
36 import com.oracle.truffle.api.nodes.RootNode; | |
37 | |
38 public final class ComplexNumbersA implements TruffleObject { | |
39 | |
40 private final double[] data; | |
41 | |
42 public ComplexNumbersA(double[] data) { | |
43 assert data.length % 2 == 0; | |
44 this.data = data; | |
45 } | |
46 | |
47 public double[] getData() { | |
48 return data; | |
49 } | |
50 | |
51 public ForeignAccess getForeignAccess() { | |
52 return ForeignAccess.create(new ComplexNumbersAForeignAccessFactory()); | |
53 } | |
54 | |
55 private static class ComplexNumbersAForeignAccessFactory implements Factory { | |
56 | |
57 public boolean canHandle(TruffleObject obj) { | |
58 return obj instanceof ComplexNumbersA; | |
59 } | |
60 | |
61 public CallTarget accessMessage(Message tree) { | |
62 if (Message.IS_NULL.equals(tree)) { | |
63 return Truffle.getRuntime().createCallTarget(RootNode.createConstantNode(false)); | |
64 } else if (Message.IS_EXECUTABLE.equals(tree)) { | |
65 return Truffle.getRuntime().createCallTarget(RootNode.createConstantNode(false)); | |
66 } else if (Message.IS_BOXED.equals(tree)) { | |
67 return Truffle.getRuntime().createCallTarget(RootNode.createConstantNode(false)); | |
68 } else if (Message.HAS_SIZE.equals(tree)) { | |
69 return Truffle.getRuntime().createCallTarget(RootNode.createConstantNode(true)); | |
70 } else if (Message.READ.equals(tree)) { | |
71 return Truffle.getRuntime().createCallTarget(new ComplexNumbersAReadNode()); | |
72 } else if (Message.WRITE.equals(tree)) { | |
73 return Truffle.getRuntime().createCallTarget(new ComplexNumbersAWriteNode()); | |
74 } else if (Message.GET_SIZE.equals(tree)) { | |
75 return Truffle.getRuntime().createCallTarget(new ComplexNumbersASizeNode()); | |
76 } else { | |
77 throw new IllegalArgumentException(tree.toString() + " not supported"); | |
78 } | |
79 } | |
80 } | |
81 | |
82 private static class ComplexNumbersAWriteNode extends RootNode { | |
83 protected ComplexNumbersAWriteNode() { | |
84 super(TckLanguage.class, null, null); | |
85 } | |
86 | |
87 @Child private Node readReal; | |
88 @Child private Node readImag; | |
89 | |
90 @Override | |
91 public Object execute(VirtualFrame frame) { | |
92 ComplexNumbersA complexNumbers = (ComplexNumbersA) ForeignAccess.getReceiver(frame); | |
93 Number index = (Number) ForeignAccess.getArguments(frame).get(0); | |
94 TruffleObject value = (TruffleObject) ForeignAccess.getArguments(frame).get(1); | |
95 if (readReal == null || readImag == null) { | |
96 CompilerDirectives.transferToInterpreterAndInvalidate(); | |
97 this.readReal = insert(Message.READ.createNode()); | |
98 this.readImag = insert(Message.READ.createNode()); | |
99 } | |
100 Number realPart = (Number) ForeignAccess.execute(readReal, frame, value, new Object[]{ComplexNumber.REAL_IDENTIFIER}); | |
101 Number imagPart = (Number) ForeignAccess.execute(readImag, frame, value, new Object[]{ComplexNumber.IMAGINARY_IDENTIFIER}); | |
102 | |
103 complexNumbers.data[index.intValue() * 2] = realPart.doubleValue(); | |
104 complexNumbers.data[index.intValue() * 2 + 1] = imagPart.doubleValue(); | |
105 return value; | |
106 } | |
107 } | |
108 | |
109 private static class ComplexNumbersAReadNode extends RootNode { | |
110 protected ComplexNumbersAReadNode() { | |
111 super(TckLanguage.class, null, null); | |
112 } | |
113 | |
114 @Override | |
115 public Object execute(VirtualFrame frame) { | |
116 ComplexNumbersA complexNumbers = (ComplexNumbersA) ForeignAccess.getReceiver(frame); | |
117 Number index = (Number) ForeignAccess.getArguments(frame).get(0); | |
118 return new ComplexNumberAEntry(complexNumbers, index.intValue()); | |
119 } | |
120 } | |
121 | |
122 private static class ComplexNumbersASizeNode extends RootNode { | |
123 protected ComplexNumbersASizeNode() { | |
124 super(TckLanguage.class, null, null); | |
125 } | |
126 | |
127 @Override | |
128 public Object execute(VirtualFrame frame) { | |
129 ComplexNumbersA complexNumbers = (ComplexNumbersA) ForeignAccess.getReceiver(frame); | |
130 return complexNumbers.data.length / 2; | |
131 } | |
132 | |
133 } | |
134 | |
135 private static class ComplexNumberAEntry implements TruffleObject { | |
136 | |
137 private final ComplexNumbersA numbers; | |
138 private final int index; | |
139 | |
140 public ComplexNumberAEntry(ComplexNumbersA numbers, int index) { | |
141 this.numbers = numbers; | |
142 this.index = index; | |
143 } | |
144 | |
145 public ForeignAccess getForeignAccess() { | |
146 return ForeignAccess.create(new ComplexNumberAEntryForeignAccessFactory()); | |
147 } | |
148 | |
149 private static class ComplexNumberAEntryForeignAccessFactory implements Factory { | |
150 | |
151 public boolean canHandle(TruffleObject obj) { | |
152 return obj instanceof ComplexNumberAEntry; | |
153 } | |
154 | |
155 public CallTarget accessMessage(Message tree) { | |
156 if (Message.IS_NULL.equals(tree)) { | |
157 return Truffle.getRuntime().createCallTarget(RootNode.createConstantNode(false)); | |
158 } else if (Message.IS_EXECUTABLE.equals(tree)) { | |
159 return Truffle.getRuntime().createCallTarget(RootNode.createConstantNode(false)); | |
160 } else if (Message.IS_BOXED.equals(tree)) { | |
161 return Truffle.getRuntime().createCallTarget(RootNode.createConstantNode(false)); | |
162 } else if (Message.HAS_SIZE.equals(tree)) { | |
163 return Truffle.getRuntime().createCallTarget(RootNode.createConstantNode(false)); | |
164 } else if (Message.READ.equals(tree)) { | |
165 return Truffle.getRuntime().createCallTarget(new ComplexNumbersAEntryReadNode()); | |
166 } else if (Message.WRITE.equals(tree)) { | |
167 return Truffle.getRuntime().createCallTarget(new ComplexNumbersAEntryWriteNode()); | |
168 } else { | |
169 throw new IllegalArgumentException(tree.toString() + " not supported"); | |
170 } | |
171 } | |
172 | |
173 private static class ComplexNumbersAEntryReadNode extends RootNode { | |
174 protected ComplexNumbersAEntryReadNode() { | |
175 super(TckLanguage.class, null, null); | |
176 } | |
177 | |
178 @Child private Node readReal; | |
179 @Child private Node readImag; | |
180 | |
181 @Override | |
182 public Object execute(VirtualFrame frame) { | |
183 ComplexNumberAEntry complexNumber = (ComplexNumberAEntry) ForeignAccess.getReceiver(frame); | |
184 String name = (String) ForeignAccess.getArguments(frame).get(0); | |
185 if (name.equals(ComplexNumber.IMAGINARY_IDENTIFIER)) { | |
186 return complexNumber.numbers.data[complexNumber.index * 2 + 1]; | |
187 } else if (name.equals(ComplexNumber.REAL_IDENTIFIER)) { | |
188 return complexNumber.numbers.data[complexNumber.index * 2]; | |
189 } else { | |
190 throw new IllegalArgumentException(); | |
191 } | |
192 } | |
193 } | |
194 | |
195 private static class ComplexNumbersAEntryWriteNode extends RootNode { | |
196 protected ComplexNumbersAEntryWriteNode() { | |
197 super(TckLanguage.class, null, null); | |
198 } | |
199 | |
200 @Override | |
201 public Object execute(VirtualFrame frame) { | |
202 ComplexNumberAEntry complexNumber = (ComplexNumberAEntry) ForeignAccess.getReceiver(frame); | |
203 String name = (String) ForeignAccess.getArguments(frame).get(0); | |
204 Number value = (Number) ForeignAccess.getArguments(frame).get(1); | |
205 if (name.equals(ComplexNumber.IMAGINARY_IDENTIFIER)) { | |
206 complexNumber.numbers.data[complexNumber.index * 2 + 1] = value.doubleValue(); | |
207 } else if (name.equals(ComplexNumber.REAL_IDENTIFIER)) { | |
208 complexNumber.numbers.data[complexNumber.index * 2] = value.doubleValue(); | |
209 } else { | |
210 throw new IllegalArgumentException(); | |
211 } | |
212 return value; | |
213 } | |
214 | |
215 } | |
216 } | |
217 | |
218 } | |
219 } |