Mercurial > hg > truffle
comparison truffle/com.oracle.truffle.tck/src/com/oracle/truffle/tck/ComplexNumbersB.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 class ComplexNumbersB implements TruffleObject { | |
39 | |
40 private final double[] reals; | |
41 private final double[] imags; | |
42 | |
43 public ComplexNumbersB(double[] reals, double[] imags) { | |
44 assert reals.length == imags.length; | |
45 this.reals = reals; | |
46 this.imags = imags; | |
47 } | |
48 | |
49 public double[] getData() { | |
50 double[] data = new double[reals.length * 2]; | |
51 for (int i = 0; i < reals.length; i++) { | |
52 data[i * 2] = reals[i]; | |
53 data[i * 2 + 1] = imags[i]; | |
54 } | |
55 return data; | |
56 } | |
57 | |
58 public ForeignAccess getForeignAccess() { | |
59 return ForeignAccess.create(new ComplexNumbersBForeignAccessFactory()); | |
60 } | |
61 | |
62 private static class ComplexNumbersBForeignAccessFactory implements Factory { | |
63 | |
64 public boolean canHandle(TruffleObject obj) { | |
65 return obj instanceof ComplexNumbersB; | |
66 } | |
67 | |
68 public CallTarget accessMessage(Message tree) { | |
69 if (Message.IS_NULL.equals(tree)) { | |
70 return Truffle.getRuntime().createCallTarget(RootNode.createConstantNode(false)); | |
71 } else if (Message.IS_EXECUTABLE.equals(tree)) { | |
72 return Truffle.getRuntime().createCallTarget(RootNode.createConstantNode(false)); | |
73 } else if (Message.IS_BOXED.equals(tree)) { | |
74 return Truffle.getRuntime().createCallTarget(RootNode.createConstantNode(false)); | |
75 } else if (Message.HAS_SIZE.equals(tree)) { | |
76 return Truffle.getRuntime().createCallTarget(RootNode.createConstantNode(true)); | |
77 } else if (Message.READ.equals(tree)) { | |
78 return Truffle.getRuntime().createCallTarget(new ComplexNumbersBReadNode()); | |
79 } else if (Message.WRITE.equals(tree)) { | |
80 return Truffle.getRuntime().createCallTarget(new ComplexNumbersBWriteNode()); | |
81 } else if (Message.GET_SIZE.equals(tree)) { | |
82 return Truffle.getRuntime().createCallTarget(new ComplexNumbersBSizeNode()); | |
83 } else { | |
84 throw new IllegalArgumentException(tree.toString() + " not supported"); | |
85 } | |
86 } | |
87 } | |
88 | |
89 private static class ComplexNumbersBWriteNode extends RootNode { | |
90 protected ComplexNumbersBWriteNode() { | |
91 super(TckLanguage.class, null, null); | |
92 } | |
93 | |
94 @Child private Node readReal; | |
95 @Child private Node readImag; | |
96 | |
97 @Override | |
98 public Object execute(VirtualFrame frame) { | |
99 ComplexNumbersB complexNumbers = (ComplexNumbersB) ForeignAccess.getReceiver(frame); | |
100 Number index = (Number) ForeignAccess.getArguments(frame).get(0); | |
101 TruffleObject value = (TruffleObject) ForeignAccess.getArguments(frame).get(1); | |
102 if (readReal == null || readImag == null) { | |
103 CompilerDirectives.transferToInterpreterAndInvalidate(); | |
104 this.readReal = insert(Message.READ.createNode()); | |
105 this.readImag = insert(Message.READ.createNode()); | |
106 } | |
107 Number realPart = (Number) ForeignAccess.execute(readReal, frame, value, new Object[]{ComplexNumber.REAL_IDENTIFIER}); | |
108 Number imagPart = (Number) ForeignAccess.execute(readImag, frame, value, new Object[]{ComplexNumber.IMAGINARY_IDENTIFIER}); | |
109 | |
110 complexNumbers.reals[index.intValue()] = realPart.doubleValue(); | |
111 complexNumbers.imags[index.intValue()] = imagPart.doubleValue(); | |
112 return value; | |
113 } | |
114 } | |
115 | |
116 private static class ComplexNumbersBReadNode extends RootNode { | |
117 protected ComplexNumbersBReadNode() { | |
118 super(TckLanguage.class, null, null); | |
119 } | |
120 | |
121 @Override | |
122 public Object execute(VirtualFrame frame) { | |
123 ComplexNumbersB complexNumbers = (ComplexNumbersB) ForeignAccess.getReceiver(frame); | |
124 Number index = (Number) ForeignAccess.getArguments(frame).get(0); | |
125 return new ComplexNumberBEntry(complexNumbers, index.intValue()); | |
126 } | |
127 | |
128 } | |
129 | |
130 private static class ComplexNumbersBSizeNode extends RootNode { | |
131 protected ComplexNumbersBSizeNode() { | |
132 super(TckLanguage.class, null, null); | |
133 } | |
134 | |
135 @Override | |
136 public Object execute(VirtualFrame frame) { | |
137 ComplexNumbersB complexNumbers = (ComplexNumbersB) ForeignAccess.getReceiver(frame); | |
138 assert complexNumbers.reals.length == complexNumbers.imags.length; | |
139 return complexNumbers.reals.length; | |
140 } | |
141 | |
142 } | |
143 | |
144 private static class ComplexNumberBEntry implements TruffleObject { | |
145 | |
146 private final ComplexNumbersB numbers; | |
147 private final int index; | |
148 | |
149 public ComplexNumberBEntry(ComplexNumbersB numbers, int index) { | |
150 this.numbers = numbers; | |
151 this.index = index; | |
152 } | |
153 | |
154 public ForeignAccess getForeignAccess() { | |
155 return ForeignAccess.create(new ComplexNumberBEntryForeignAccessFactory()); | |
156 } | |
157 | |
158 private static class ComplexNumberBEntryForeignAccessFactory implements Factory { | |
159 | |
160 public boolean canHandle(TruffleObject obj) { | |
161 return obj instanceof ComplexNumberBEntry; | |
162 } | |
163 | |
164 public CallTarget accessMessage(Message tree) { | |
165 if (Message.IS_NULL.equals(tree)) { | |
166 return Truffle.getRuntime().createCallTarget(RootNode.createConstantNode(false)); | |
167 } else if (Message.IS_EXECUTABLE.equals(tree)) { | |
168 return Truffle.getRuntime().createCallTarget(RootNode.createConstantNode(false)); | |
169 } else if (Message.IS_BOXED.equals(tree)) { | |
170 return Truffle.getRuntime().createCallTarget(RootNode.createConstantNode(false)); | |
171 } else if (Message.HAS_SIZE.equals(tree)) { | |
172 return Truffle.getRuntime().createCallTarget(RootNode.createConstantNode(false)); | |
173 } else if (Message.READ.equals(tree)) { | |
174 return Truffle.getRuntime().createCallTarget(new ComplexNumbersAEntryReadNode()); | |
175 } else if (Message.WRITE.equals(tree)) { | |
176 return Truffle.getRuntime().createCallTarget(new ComplexNumbersAEntryWriteNode()); | |
177 } else { | |
178 throw new IllegalArgumentException(tree.toString() + " not supported"); | |
179 } | |
180 } | |
181 | |
182 private static class ComplexNumbersAEntryReadNode extends RootNode { | |
183 protected ComplexNumbersAEntryReadNode() { | |
184 super(TckLanguage.class, null, null); | |
185 } | |
186 | |
187 @Child private Node readReal; | |
188 @Child private Node readImag; | |
189 | |
190 @Override | |
191 public Object execute(VirtualFrame frame) { | |
192 ComplexNumberBEntry complexNumber = (ComplexNumberBEntry) ForeignAccess.getReceiver(frame); | |
193 String name = (String) ForeignAccess.getArguments(frame).get(0); | |
194 if (name.equals(ComplexNumber.IMAGINARY_IDENTIFIER)) { | |
195 return complexNumber.numbers.imags[complexNumber.index]; | |
196 } else if (name.equals(ComplexNumber.REAL_IDENTIFIER)) { | |
197 return complexNumber.numbers.reals[complexNumber.index]; | |
198 } else { | |
199 throw new IllegalArgumentException(); | |
200 } | |
201 } | |
202 } | |
203 | |
204 private static class ComplexNumbersAEntryWriteNode extends RootNode { | |
205 protected ComplexNumbersAEntryWriteNode() { | |
206 super(TckLanguage.class, null, null); | |
207 } | |
208 | |
209 @Override | |
210 public Object execute(VirtualFrame frame) { | |
211 ComplexNumberBEntry complexNumber = (ComplexNumberBEntry) ForeignAccess.getReceiver(frame); | |
212 String name = (String) ForeignAccess.getArguments(frame).get(0); | |
213 Number value = (Number) ForeignAccess.getArguments(frame).get(1); | |
214 if (name.equals(ComplexNumber.IMAGINARY_IDENTIFIER)) { | |
215 complexNumber.numbers.imags[complexNumber.index] = value.doubleValue(); | |
216 } else if (name.equals(ComplexNumber.REAL_IDENTIFIER)) { | |
217 complexNumber.numbers.reals[complexNumber.index] = value.doubleValue(); | |
218 } else { | |
219 throw new IllegalArgumentException(); | |
220 } | |
221 return value; | |
222 } | |
223 | |
224 } | |
225 } | |
226 | |
227 } | |
228 | |
229 } |