Mercurial > hg > graal-compiler
comparison graal/com.oracle.truffle.object/src/com/oracle/truffle/object/Locations.java @ 18408:2c3666f44855
Truffle: initial commit of object API implementation
author | Andreas Woess <andreas.woess@jku.at> |
---|---|
date | Tue, 18 Nov 2014 23:19:43 +0100 |
parents | |
children | e9cbe1618733 |
comparison
equal
deleted
inserted
replaced
18407:f439fdb137a3 | 18408:2c3666f44855 |
---|---|
1 /* | |
2 * Copyright (c) 2013, 2014, 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.object; | |
24 | |
25 import java.util.*; | |
26 | |
27 import com.oracle.truffle.api.*; | |
28 import com.oracle.truffle.api.object.*; | |
29 | |
30 /** | |
31 * Property location. | |
32 * | |
33 * @see Location | |
34 * @see Shape | |
35 * @see Property | |
36 * @see DynamicObject | |
37 */ | |
38 public abstract class Locations { | |
39 public abstract static class ValueLocation extends LocationImpl { | |
40 | |
41 private final Object value; | |
42 | |
43 public ValueLocation(Object value) { | |
44 assert !(value instanceof Location); | |
45 this.value = value; | |
46 } | |
47 | |
48 @Override | |
49 public int hashCode() { | |
50 final int prime = 31; | |
51 int result = super.hashCode(); | |
52 result = prime * result + ((value == null) ? 0 : 0 /* value.hashCode() */); | |
53 return result; | |
54 } | |
55 | |
56 @Override | |
57 public boolean equals(Object obj) { | |
58 if (!super.equals(obj)) { | |
59 return false; | |
60 } | |
61 ValueLocation other = (ValueLocation) obj; | |
62 if (value == null) { | |
63 if (other.value != null) { | |
64 return false; | |
65 } | |
66 } else if (!value.equals(other.value)) { | |
67 return false; | |
68 } | |
69 return true; | |
70 } | |
71 | |
72 @Override | |
73 public final Object get(DynamicObject store, boolean condition) { | |
74 return value; | |
75 } | |
76 | |
77 @Override | |
78 public final void set(DynamicObject store, Object value, Shape shape) throws IncompatibleLocationException, FinalLocationException { | |
79 if (!canStoreFinal(store, value)) { | |
80 throw finalLocation(); | |
81 } | |
82 } | |
83 | |
84 @Override | |
85 protected boolean canStoreFinal(DynamicObject store, Object val) { | |
86 return valueEquals(this.value, val); | |
87 } | |
88 | |
89 @Override | |
90 public final void setInternal(DynamicObject store, Object value) throws IncompatibleLocationException { | |
91 if (!canStoreFinal(store, value)) { | |
92 CompilerDirectives.transferToInterpreter(); | |
93 throw new UnsupportedOperationException(); | |
94 } | |
95 } | |
96 | |
97 @Override | |
98 public String toString() { | |
99 return "=" + String.valueOf(value); | |
100 } | |
101 } | |
102 | |
103 public static final class ConstantLocation extends ValueLocation { | |
104 | |
105 public ConstantLocation(Object value) { | |
106 super(value); | |
107 } | |
108 | |
109 @Override | |
110 public boolean isConstant() { | |
111 return true; | |
112 } | |
113 } | |
114 | |
115 public static final class DeclaredLocation extends ValueLocation { | |
116 | |
117 public DeclaredLocation(Object value) { | |
118 super(value); | |
119 } | |
120 } | |
121 | |
122 public static class DualLocation extends LocationImpl implements TypedLocation { | |
123 protected final InternalLongLocation primitiveLocation; | |
124 protected final ObjectLocation objectLocation; | |
125 protected final LayoutImpl layout; | |
126 private final Class<?> type; | |
127 | |
128 public DualLocation(InternalLongLocation primitiveLocation, ObjectLocation objectLocation, LayoutImpl layout) { | |
129 this(primitiveLocation, objectLocation, layout, null); | |
130 } | |
131 | |
132 public DualLocation(InternalLongLocation primitiveLocation, ObjectLocation objectLocation, LayoutImpl layout, Class<?> type) { | |
133 this.primitiveLocation = primitiveLocation; | |
134 this.objectLocation = objectLocation; | |
135 this.layout = layout; | |
136 this.type = type; | |
137 } | |
138 | |
139 @Override | |
140 public Object get(DynamicObject store, boolean condition) { | |
141 if (type == Object.class) { | |
142 return objectLocation.get(store, condition); | |
143 } else { | |
144 long rawValue = primitiveLocation.getLong(store, condition); | |
145 if (type == int.class) { | |
146 return (int) rawValue; | |
147 } else if (type == long.class) { | |
148 return rawValue; | |
149 } else if (type == double.class) { | |
150 return Double.longBitsToDouble(rawValue); | |
151 } else if (type == boolean.class) { | |
152 return rawValue != 0; | |
153 } else { | |
154 CompilerDirectives.transferToInterpreter(); | |
155 throw new IllegalStateException(); | |
156 } | |
157 } | |
158 } | |
159 | |
160 @Override | |
161 public void setInternal(DynamicObject store, Object value) throws IncompatibleLocationException { | |
162 if (type == Object.class) { | |
163 ((LocationImpl) objectLocation).setInternal(store, value); | |
164 } else { | |
165 long rawValue; | |
166 if (type == int.class && value instanceof Integer) { | |
167 rawValue = (int) value; | |
168 } else if (type == long.class && value instanceof Long) { | |
169 rawValue = (long) value; | |
170 } else if (type == long.class && layout.isAllowedIntToLong() && value instanceof Integer) { | |
171 rawValue = (int) value; | |
172 } else if (type == double.class && value instanceof Double) { | |
173 rawValue = Double.doubleToRawLongBits((double) value); | |
174 } else if (type == double.class && layout.isAllowedIntToDouble() && value instanceof Integer) { | |
175 rawValue = Double.doubleToRawLongBits((int) value); | |
176 } else if (type == boolean.class && value instanceof Boolean) { | |
177 rawValue = (boolean) value ? 1 : 0; | |
178 } else { | |
179 throw incompatibleLocation(); | |
180 } | |
181 | |
182 primitiveLocation.setLongInternal(store, rawValue); | |
183 } | |
184 } | |
185 | |
186 @Override | |
187 public int primitiveFieldCount() { | |
188 return ((LocationImpl) primitiveLocation).primitiveFieldCount(); | |
189 } | |
190 | |
191 @Override | |
192 public int primitiveArrayCount() { | |
193 return ((LocationImpl) primitiveLocation).primitiveArrayCount(); | |
194 } | |
195 | |
196 @Override | |
197 public int objectFieldCount() { | |
198 return ((LocationImpl) objectLocation).objectFieldCount(); | |
199 } | |
200 | |
201 @Override | |
202 public int objectArrayCount() { | |
203 return ((LocationImpl) objectLocation).objectArrayCount(); | |
204 } | |
205 | |
206 @Override | |
207 public String toString() { | |
208 return objectLocation.toString() + "," + primitiveLocation.toString() + "," + type; | |
209 } | |
210 | |
211 @Override | |
212 public boolean equals(Object obj) { | |
213 if (!super.equals(obj)) { | |
214 return false; | |
215 } | |
216 DualLocation other = (DualLocation) obj; | |
217 return getObjectLocation().equals(other.getObjectLocation()) && primitiveLocation.equals(other.primitiveLocation) && layout.equals(other.layout) && Objects.equals(type, other.type); | |
218 } | |
219 | |
220 @Override | |
221 public int hashCode() { | |
222 final int prime = 31; | |
223 int result = super.hashCode(); | |
224 result = prime * result + (getObjectLocation() == null ? 0 : getObjectLocation().hashCode()); | |
225 result = prime * result + (primitiveLocation == null ? 0 : primitiveLocation.hashCode()); | |
226 result = prime * result + (type == null ? 0 : type.hashCode()); | |
227 return result; | |
228 } | |
229 | |
230 public ObjectLocation getObjectLocation() { | |
231 return objectLocation; | |
232 } | |
233 | |
234 public DualLocation changeType(Class<?> newType) { | |
235 return new DualLocation(primitiveLocation, objectLocation, layout, newType); | |
236 } | |
237 | |
238 public Class<?> getType() { | |
239 return type; | |
240 } | |
241 | |
242 public boolean isNonNull() { | |
243 return false; | |
244 } | |
245 | |
246 @Override | |
247 public boolean canStore(Object value) { | |
248 if (type == null) { | |
249 return false; | |
250 } else if (type == int.class) { | |
251 return value instanceof Integer; | |
252 } else if (type == long.class) { | |
253 return value instanceof Long || (layout.isAllowedIntToLong() && value instanceof Integer); | |
254 } else if (type == double.class) { | |
255 return value instanceof Double || (layout.isAllowedIntToDouble() && value instanceof Integer); | |
256 } else if (type == boolean.class) { | |
257 return value instanceof Boolean; | |
258 } else if (type == Object.class) { | |
259 return true; | |
260 } else { | |
261 throw new IllegalStateException(); | |
262 } | |
263 } | |
264 } | |
265 | |
266 public static class DeclaredDualLocation extends DualLocation { | |
267 private final Object defaultValue; | |
268 | |
269 public DeclaredDualLocation(InternalLongLocation primitiveLocation, ObjectLocation objectLocation, Object defaultValue, LayoutImpl layout) { | |
270 super(primitiveLocation, objectLocation, layout); | |
271 this.defaultValue = defaultValue; | |
272 } | |
273 | |
274 @Override | |
275 public Object get(DynamicObject store, boolean condition) { | |
276 return defaultValue; | |
277 } | |
278 | |
279 @Override | |
280 public void setInternal(DynamicObject store, Object value) throws IncompatibleLocationException { | |
281 if (valueEquals(defaultValue, value)) { | |
282 return; | |
283 } else { | |
284 throw incompatibleLocation(); | |
285 } | |
286 } | |
287 | |
288 @Override | |
289 public boolean equals(Object obj) { | |
290 return super.equals(obj) && Objects.equals(defaultValue, ((DeclaredDualLocation) obj).defaultValue); | |
291 } | |
292 | |
293 @Override | |
294 public int hashCode() { | |
295 return super.hashCode(); | |
296 } | |
297 | |
298 @Override | |
299 public DualLocation changeType(Class<?> newType) { | |
300 return new DualLocation(primitiveLocation, objectLocation, layout, newType); | |
301 } | |
302 | |
303 @Override | |
304 public boolean canStore(Object value) { | |
305 return valueEquals(defaultValue, value); | |
306 } | |
307 | |
308 @Override | |
309 public String toString() { | |
310 return objectLocation.toString() + "," + primitiveLocation.toString() + ",unset"; | |
311 } | |
312 } | |
313 } |