comparison truffle/com.oracle.truffle.object.basic/src/com/oracle/truffle/object/basic/BasicLocations.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.object.basic/src/com/oracle/truffle/object/basic/BasicLocations.java@e9cbe1618733
children dc83cc1f94f2
comparison
equal deleted inserted replaced
21950:2a5011c7e641 21951:9c8c0937da41
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.basic;
24
25 import java.lang.invoke.*;
26
27 import com.oracle.truffle.api.*;
28 import com.oracle.truffle.api.object.*;
29 import com.oracle.truffle.object.*;
30 import com.oracle.truffle.object.LocationImpl.InternalLongLocation;
31
32 /**
33 * Property location.
34 *
35 * @see Shape
36 * @see Property
37 * @see DynamicObject
38 */
39 public abstract class BasicLocations {
40 static final int LONG_SIZE = 1;
41 static final int OBJECT_SIZE = 1;
42
43 public abstract static class ArrayLocation extends LocationImpl {
44 protected final int index;
45 protected final Location arrayLocation;
46
47 public ArrayLocation(int index, Location arrayLocation) {
48 this.index = index;
49 this.arrayLocation = arrayLocation;
50 }
51
52 protected final Object getArray(DynamicObject store, boolean condition) {
53 // non-null cast
54 return arrayLocation.get(store, condition);
55 }
56
57 @Override
58 public int hashCode() {
59 final int prime = 31;
60 int result = super.hashCode();
61 result = prime * result + index;
62 return result;
63 }
64
65 @Override
66 public boolean equals(Object obj) {
67 if (!super.equals(obj)) {
68 return false;
69 }
70 ArrayLocation other = (ArrayLocation) obj;
71 if (index != other.index) {
72 return false;
73 }
74 return true;
75 }
76
77 public final int getIndex() {
78 return index;
79 }
80
81 @Override
82 protected String getWhereString() {
83 return "[" + index + "]";
84 }
85 }
86
87 public abstract static class FieldLocation extends LocationImpl {
88 private final int index;
89
90 public FieldLocation(int index) {
91 this.index = index;
92 }
93
94 @Override
95 public int hashCode() {
96 final int prime = 31;
97 int result = super.hashCode();
98 result = prime * result + index;
99 return result;
100 }
101
102 @Override
103 public boolean equals(Object obj) {
104 if (!super.equals(obj)) {
105 return false;
106 }
107 FieldLocation other = (FieldLocation) obj;
108 if (index != other.index) {
109 return false;
110 }
111 return true;
112 }
113
114 public final int getIndex() {
115 return index;
116 }
117
118 @Override
119 protected String getWhereString() {
120 return "@" + index;
121 }
122 }
123
124 public abstract static class MethodHandleFieldLocation extends FieldLocation {
125 protected final MethodHandle getter;
126 protected final MethodHandle setter;
127
128 public MethodHandleFieldLocation(int index, MethodHandle getter, MethodHandle setter) {
129 super(index);
130 this.getter = getter;
131 this.setter = setter;
132 }
133 }
134
135 public static class ObjectArrayLocation extends ArrayLocation implements ObjectLocation {
136 public ObjectArrayLocation(int index, Location arrayLocation) {
137 super(index, arrayLocation);
138 }
139
140 @Override
141 public Object get(DynamicObject store, boolean condition) {
142 return ((Object[]) getArray(store, condition))[index];
143 }
144
145 @Override
146 public final void setInternal(DynamicObject store, Object value) throws IncompatibleLocationException {
147 ((Object[]) getArray(store, false))[index] = value;
148 }
149
150 @Override
151 public boolean canStore(Object value) {
152 return true;
153 }
154
155 public Class<? extends Object> getType() {
156 return Object.class;
157 }
158
159 public final boolean isNonNull() {
160 return false;
161 }
162
163 @Override
164 public int objectArrayCount() {
165 return OBJECT_SIZE;
166 }
167
168 @Override
169 public final void accept(LocationVisitor locationVisitor) {
170 locationVisitor.visitObjectArray(index, OBJECT_SIZE);
171 }
172 }
173
174 public static class ObjectFieldLocation extends MethodHandleFieldLocation implements ObjectLocation {
175
176 public ObjectFieldLocation(int index, MethodHandle getter, MethodHandle setter) {
177 super(index, getter, setter);
178 }
179
180 @Override
181 public Object get(DynamicObject store, boolean condition) {
182 try {
183 return getter.invokeExact(store);
184 } catch (Throwable e) {
185 CompilerDirectives.transferToInterpreter();
186 throw new IllegalStateException(e);
187 }
188 }
189
190 @Override
191 public final void setInternal(DynamicObject store, Object value) throws IncompatibleLocationException {
192 try {
193 setter.invokeExact(store, value);
194 } catch (Throwable e) {
195 CompilerDirectives.transferToInterpreter();
196 throw new IllegalStateException(e);
197 }
198 }
199
200 @Override
201 public boolean canStore(Object value) {
202 return true;
203 }
204
205 public Class<? extends Object> getType() {
206 return Object.class;
207 }
208
209 public boolean isNonNull() {
210 return false;
211 }
212
213 @Override
214 public int objectFieldCount() {
215 return OBJECT_SIZE;
216 }
217
218 @Override
219 public final void accept(LocationVisitor locationVisitor) {
220 locationVisitor.visitObjectField(getIndex(), OBJECT_SIZE);
221 }
222 }
223
224 public abstract static class SimpleObjectFieldLocation extends FieldLocation implements ObjectLocation {
225
226 public SimpleObjectFieldLocation(int index) {
227 super(index);
228 }
229
230 @Override
231 public abstract Object get(DynamicObject store, boolean condition);
232
233 @Override
234 public abstract void setInternal(DynamicObject store, Object value);
235
236 @Override
237 public boolean canStore(Object value) {
238 return true;
239 }
240
241 public Class<? extends Object> getType() {
242 return Object.class;
243 }
244
245 public boolean isNonNull() {
246 return false;
247 }
248
249 @Override
250 public int objectFieldCount() {
251 return OBJECT_SIZE;
252 }
253
254 @Override
255 public final void accept(LocationVisitor locationVisitor) {
256 locationVisitor.visitObjectField(getIndex(), OBJECT_SIZE);
257 }
258 }
259
260 public static class LongArrayLocation extends ArrayLocation implements InternalLongLocation {
261 protected final boolean allowInt;
262
263 public LongArrayLocation(int index, Location arrayLocation, boolean allowInt) {
264 super(index, arrayLocation);
265 this.allowInt = allowInt;
266 }
267
268 public LongArrayLocation(int index, Location arrayLocation) {
269 this(index, arrayLocation, false);
270 }
271
272 @Override
273 public final Object get(DynamicObject store, boolean condition) {
274 return getLong(store, condition);
275 }
276
277 @Override
278 public final void setInternal(DynamicObject store, Object value) throws IncompatibleLocationException {
279 if (canStore(value)) {
280 setLongInternal(store, ((Number) value).longValue());
281 } else {
282 throw incompatibleLocation();
283 }
284 }
285
286 @Override
287 public long getLong(DynamicObject store, boolean condition) {
288 return ((long[]) getArray(store, condition))[index];
289 }
290
291 public final void setLongInternal(DynamicObject store, long value) {
292 ((long[]) getArray(store, false))[index] = value;
293 }
294
295 @Override
296 public void setLong(DynamicObject store, long value, Shape shape) throws FinalLocationException {
297 setLongInternal(store, value);
298 }
299
300 @Override
301 public final void setLong(DynamicObject store, long value, Shape oldShape, Shape newShape) {
302 store.setShapeAndGrow(oldShape, newShape);
303 setLongInternal(store, value);
304 }
305
306 @Override
307 public final void setLong(DynamicObject store, long value) throws FinalLocationException {
308 setLong(store, value, null);
309 }
310
311 public final long getLong(DynamicObject store, Shape shape) {
312 return getLong(store, checkShape(store, shape));
313 }
314
315 @Override
316 public final boolean canStore(Object value) {
317 return value instanceof Long || (allowInt && value instanceof Integer);
318 }
319
320 public final Class<Long> getType() {
321 return long.class;
322 }
323
324 @Override
325 public int primitiveArrayCount() {
326 return LONG_SIZE;
327 }
328
329 @Override
330 public final void accept(LocationVisitor locationVisitor) {
331 locationVisitor.visitPrimitiveArray(getIndex(), LONG_SIZE);
332 }
333 }
334
335 public static class LongFieldLocation extends MethodHandleFieldLocation implements InternalLongLocation {
336 public LongFieldLocation(int index, MethodHandle getter, MethodHandle setter) {
337 super(index, getter, setter);
338 }
339
340 public static LongLocation create(InternalLongLocation longLocation, boolean allowInt) {
341 if ((!allowInt && (longLocation instanceof LongLocationDecorator)) || (longLocation instanceof LongLocationDecorator && ((LongLocationDecorator) longLocation).allowInt == allowInt)) {
342 return longLocation;
343 } else {
344 return new LongLocationDecorator(longLocation, allowInt);
345 }
346 }
347
348 @Override
349 public final Object get(DynamicObject store, boolean condition) {
350 return getLong(store, condition);
351 }
352
353 @Override
354 public final void setInternal(DynamicObject store, Object value) throws IncompatibleLocationException {
355 if (canStore(value)) {
356 setLongInternal(store, (long) value);
357 } else {
358 throw incompatibleLocation();
359 }
360 }
361
362 @Override
363 public final boolean canStore(Object value) {
364 return value instanceof Long;
365 }
366
367 @Override
368 public final void setLong(DynamicObject store, long value, Shape oldShape, Shape newShape) {
369 store.setShapeAndGrow(oldShape, newShape);
370 setLongInternal(store, value);
371 }
372
373 public long getLong(DynamicObject store, boolean condition) {
374 try {
375 return (long) getter.invokeExact(store);
376 } catch (Throwable e) {
377 CompilerDirectives.transferToInterpreter();
378 throw new IllegalStateException(e);
379 }
380 }
381
382 public void setLong(DynamicObject store, long value, Shape shape) {
383 setLongInternal(store, value);
384 }
385
386 public final void setLong(DynamicObject store, long value) throws FinalLocationException {
387 setLong(store, value, null);
388 }
389
390 public final void setLongInternal(DynamicObject store, long value) {
391 try {
392 setter.invokeExact(store, value);
393 } catch (Throwable e) {
394 CompilerDirectives.transferToInterpreter();
395 throw new IllegalStateException(e);
396 }
397 }
398
399 public final long getLong(DynamicObject store, Shape shape) {
400 return getLong(store, checkShape(store, shape));
401 }
402
403 @Override
404 public final int primitiveFieldCount() {
405 return LONG_SIZE;
406 }
407
408 public final Class<Long> getType() {
409 return long.class;
410 }
411
412 @Override
413 public final void accept(LocationVisitor locationVisitor) {
414 locationVisitor.visitPrimitiveField(getIndex(), LONG_SIZE);
415 }
416 }
417
418 public static class LongLocationDecorator extends PrimitiveLocationDecorator implements InternalLongLocation {
419 protected final boolean allowInt;
420
421 public LongLocationDecorator(InternalLongLocation longLocation, boolean allowInt) {
422 super(longLocation);
423 this.allowInt = allowInt;
424 }
425
426 @Override
427 public final Object get(DynamicObject store, boolean condition) {
428 return getLong(store, condition);
429 }
430
431 @Override
432 public final void setInternal(DynamicObject store, Object value) throws IncompatibleLocationException {
433 if (canStore(value)) {
434 setLongInternal(store, ((Number) value).longValue());
435 } else {
436 throw incompatibleLocation();
437 }
438 }
439
440 @Override
441 public final boolean canStore(Object value) {
442 return value instanceof Long || (allowInt && value instanceof Integer);
443 }
444
445 @Override
446 public final void setLong(DynamicObject store, long value, Shape oldShape, Shape newShape) {
447 store.setShapeAndGrow(oldShape, newShape);
448 setLongInternal(store, value);
449 }
450
451 public Class<Long> getType() {
452 return long.class;
453 }
454 }
455
456 public abstract static class SimpleLongFieldLocation extends FieldLocation implements InternalLongLocation {
457
458 public SimpleLongFieldLocation(int index) {
459 super(index);
460 }
461
462 @Override
463 public final Object get(DynamicObject store, boolean condition) {
464 return getLong(store, condition);
465 }
466
467 @Override
468 public final void setInternal(DynamicObject store, Object value) throws IncompatibleLocationException {
469 if (canStore(value)) {
470 setLongInternal(store, ((Number) value).longValue());
471 } else {
472 throw incompatibleLocation();
473 }
474 }
475
476 @Override
477 public final boolean canStore(Object value) {
478 return value instanceof Long;
479 }
480
481 @Override
482 public final void setLong(DynamicObject store, long value, Shape oldShape, Shape newShape) {
483 store.setShapeAndGrow(oldShape, newShape);
484 setLongInternal(store, value);
485 }
486
487 public abstract long getLong(DynamicObject store, boolean condition);
488
489 public final long getLong(DynamicObject store, Shape shape) {
490 return getLong(store, checkShape(store, shape));
491 }
492
493 public final void setLong(DynamicObject store, long value) {
494 setLong(store, value, null);
495 }
496
497 public void setLong(DynamicObject store, long value, Shape shape) {
498 setLongInternal(store, value);
499 }
500
501 public abstract void setLongInternal(DynamicObject store, long value);
502
503 @Override
504 public final int primitiveFieldCount() {
505 return LONG_SIZE;
506 }
507
508 public final Class<Long> getType() {
509 return long.class;
510 }
511
512 @Override
513 public final void accept(LocationVisitor locationVisitor) {
514 locationVisitor.visitPrimitiveField(getIndex(), LONG_SIZE);
515 }
516 }
517
518 public abstract static class PrimitiveLocationDecorator extends LocationImpl {
519 private final InternalLongLocation longLocation;
520
521 public PrimitiveLocationDecorator(InternalLongLocation longLocation) {
522 this.longLocation = longLocation;
523 }
524
525 public final long getLong(DynamicObject store, Shape shape) {
526 return longLocation.getLong(store, shape);
527 }
528
529 public final long getLong(DynamicObject store, boolean condition) {
530 return longLocation.getLong(store, condition);
531 }
532
533 public final void setLong(DynamicObject store, long value, Shape shape) throws FinalLocationException {
534 longLocation.setLong(store, value, shape);
535 }
536
537 public final void setLong(DynamicObject store, long value) throws FinalLocationException {
538 longLocation.setLong(store, value);
539 }
540
541 public final void setLongInternal(DynamicObject store, long value) {
542 longLocation.setLongInternal(store, value);
543 }
544
545 @Override
546 public final int primitiveFieldCount() {
547 return ((LocationImpl) longLocation).primitiveFieldCount();
548 }
549
550 @Override
551 public final int primitiveArrayCount() {
552 return ((LocationImpl) longLocation).primitiveArrayCount();
553 }
554
555 @Override
556 public final void accept(LocationVisitor locationVisitor) {
557 ((LocationImpl) longLocation).accept(locationVisitor);
558 }
559 }
560
561 public static class IntLocationDecorator extends PrimitiveLocationDecorator implements IntLocation {
562 public IntLocationDecorator(InternalLongLocation longLocation) {
563 super(longLocation);
564 }
565
566 @Override
567 public final Object get(DynamicObject store, boolean condition) {
568 return getInt(store, condition);
569 }
570
571 public int getInt(DynamicObject store, boolean condition) {
572 return (int) getLong(store, condition);
573 }
574
575 public void setInt(DynamicObject store, int value, Shape shape) throws FinalLocationException {
576 setLong(store, value, shape);
577 }
578
579 @Override
580 public final void setInt(DynamicObject store, int value) throws FinalLocationException {
581 setInt(store, value, null);
582 }
583
584 @Override
585 public final void setInternal(DynamicObject store, Object value) throws IncompatibleLocationException {
586 if (canStore(value)) {
587 setLongInternal(store, (int) value);
588 } else {
589 throw incompatibleLocation();
590 }
591 }
592
593 public final int getInt(DynamicObject store, Shape shape) {
594 return getInt(store, checkShape(store, shape));
595 }
596
597 @Override
598 public final boolean canStore(Object value) {
599 return value instanceof Integer;
600 }
601
602 @Override
603 public final void setInt(DynamicObject store, int value, Shape oldShape, Shape newShape) {
604 store.setShapeAndGrow(oldShape, newShape);
605 setLongInternal(store, value);
606 }
607
608 public Class<Integer> getType() {
609 return int.class;
610 }
611 }
612
613 public static class DoubleLocationDecorator extends PrimitiveLocationDecorator implements DoubleLocation {
614 private final boolean allowInt;
615
616 public DoubleLocationDecorator(InternalLongLocation longLocation, boolean allowInt) {
617 super(longLocation);
618 this.allowInt = allowInt;
619 }
620
621 @Override
622 public final Object get(DynamicObject store, boolean condition) {
623 return getDouble(store, condition);
624 }
625
626 public double getDouble(DynamicObject store, boolean condition) {
627 return Double.longBitsToDouble(getLong(store, condition));
628 }
629
630 public void setDouble(DynamicObject store, double value, Shape shape) {
631 setLongInternal(store, Double.doubleToRawLongBits(value));
632 }
633
634 public void setDouble(DynamicObject store, double value) {
635 setDouble(store, value, null);
636 }
637
638 @Override
639 public final void setInternal(DynamicObject store, Object value) throws IncompatibleLocationException {
640 if (canStore(value)) {
641 setDouble(store, ((Number) value).doubleValue(), null);
642 } else {
643 throw incompatibleLocation();
644 }
645 }
646
647 public final double getDouble(DynamicObject store, Shape shape) {
648 return getDouble(store, checkShape(store, shape));
649 }
650
651 @Override
652 public final boolean canStore(Object value) {
653 return value instanceof Double || (allowInt && value instanceof Integer);
654 }
655
656 @Override
657 public final void setDouble(DynamicObject store, double value, Shape oldShape, Shape newShape) {
658 store.setShapeAndGrow(oldShape, newShape);
659 setDouble(store, value, newShape);
660 }
661
662 public Class<Double> getType() {
663 return double.class;
664 }
665 }
666
667 public static class BooleanLocationDecorator extends PrimitiveLocationDecorator implements BooleanLocation {
668 public BooleanLocationDecorator(InternalLongLocation longLocation) {
669 super(longLocation);
670 }
671
672 @Override
673 public final Object get(DynamicObject store, boolean condition) {
674 return getBoolean(store, condition);
675 }
676
677 public boolean getBoolean(DynamicObject store, boolean condition) {
678 return getLong(store, condition) != 0;
679 }
680
681 public void setBoolean(DynamicObject store, boolean value, Shape shape) {
682 setLongInternal(store, value ? 1 : 0);
683 }
684
685 public void setBoolean(DynamicObject store, boolean value) {
686 setBoolean(store, value, null);
687 }
688
689 @Override
690 public final void setInternal(DynamicObject store, Object value) throws IncompatibleLocationException {
691 if (canStore(value)) {
692 setBoolean(store, (boolean) value, null);
693 } else {
694 throw incompatibleLocation();
695 }
696 }
697
698 public final boolean getBoolean(DynamicObject store, Shape shape) {
699 return getBoolean(store, checkShape(store, shape));
700 }
701
702 @Override
703 public final boolean canStore(Object value) {
704 return value instanceof Boolean;
705 }
706
707 @Override
708 public final void setBoolean(DynamicObject store, boolean value, Shape oldShape, Shape newShape) {
709 store.setShapeAndGrow(oldShape, newShape);
710 setBoolean(store, value, newShape);
711 }
712
713 public Class<Boolean> getType() {
714 return boolean.class;
715 }
716 }
717 }