Mercurial > hg > truffle
comparison truffle/com.oracle.truffle.object/src/com/oracle/truffle/object/LayoutImpl.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/src/com/oracle/truffle/object/LayoutImpl.java@2c3666f44855 |
children | dc83cc1f94f2 |
comparison
equal
deleted
inserted
replaced
21950:2a5011c7e641 | 21951:9c8c0937da41 |
---|---|
1 /* | |
2 * Copyright (c) 2012, 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.object.*; | |
28 import com.oracle.truffle.api.object.Shape.Allocator; | |
29 import com.oracle.truffle.object.LocationImpl.EffectivelyFinalLocation; | |
30 import com.oracle.truffle.object.LocationImpl.TypedObjectLocation; | |
31 import com.oracle.truffle.object.Locations.ConstantLocation; | |
32 import com.oracle.truffle.object.Locations.DeclaredLocation; | |
33 import com.oracle.truffle.object.Locations.DualLocation; | |
34 import com.oracle.truffle.object.Locations.ValueLocation; | |
35 import com.oracle.truffle.object.ShapeImpl.BaseAllocator; | |
36 | |
37 public abstract class LayoutImpl extends Layout { | |
38 private static final int INT_TO_DOUBLE_FLAG = 1; | |
39 private static final int INT_TO_LONG_FLAG = 2; | |
40 | |
41 private final LayoutStrategy strategy; | |
42 private final Class<? extends DynamicObject> clazz; | |
43 private final int allowedImplicitCasts; | |
44 | |
45 protected LayoutImpl(EnumSet<ImplicitCast> allowedImplicitCasts, Class<? extends DynamicObjectImpl> clazz, LayoutStrategy strategy) { | |
46 this.strategy = strategy; | |
47 this.clazz = clazz; | |
48 | |
49 this.allowedImplicitCasts = (allowedImplicitCasts.contains(ImplicitCast.IntToDouble) ? INT_TO_DOUBLE_FLAG : 0) | (allowedImplicitCasts.contains(ImplicitCast.IntToLong) ? INT_TO_LONG_FLAG : 0); | |
50 } | |
51 | |
52 @Override | |
53 public abstract DynamicObject newInstance(Shape shape); | |
54 | |
55 @Override | |
56 public Class<? extends DynamicObject> getType() { | |
57 return clazz; | |
58 } | |
59 | |
60 @Override | |
61 public final Shape createShape(ObjectType operations, Object sharedData) { | |
62 return createShape(operations, sharedData, 0); | |
63 } | |
64 | |
65 @Override | |
66 public final Shape createShape(ObjectType operations) { | |
67 return createShape(operations, null); | |
68 } | |
69 | |
70 public boolean isAllowedIntToDouble() { | |
71 return (allowedImplicitCasts & INT_TO_DOUBLE_FLAG) != 0; | |
72 } | |
73 | |
74 public boolean isAllowedIntToLong() { | |
75 return (allowedImplicitCasts & INT_TO_LONG_FLAG) != 0; | |
76 } | |
77 | |
78 protected abstract boolean hasObjectExtensionArray(); | |
79 | |
80 protected abstract boolean hasPrimitiveExtensionArray(); | |
81 | |
82 protected abstract int getObjectFieldCount(); | |
83 | |
84 protected abstract int getPrimitiveFieldCount(); | |
85 | |
86 protected abstract Location getObjectArrayLocation(); | |
87 | |
88 protected abstract Location getPrimitiveArrayLocation(); | |
89 | |
90 protected abstract int objectFieldIndex(Location location); | |
91 | |
92 protected boolean isLocationAssignableFrom(Location destination, Location source) { | |
93 LayoutImpl layout = this; | |
94 if (destination.isFinal()) { | |
95 // allowed Final<X>Location => Final<X>Location | |
96 // allowed FinalIntLocation => Final{Int,Double}Location | |
97 // allowed: Final{Int,Double,TypedObject}Location => FinalObjectLocation | |
98 if (!source.isFinal()) { | |
99 return false; | |
100 } | |
101 } | |
102 | |
103 if (destination instanceof IntLocation) { | |
104 return (source instanceof IntLocation); | |
105 } else if (destination instanceof DoubleLocation) { | |
106 return (source instanceof DoubleLocation || (layout.isAllowedIntToDouble() && source instanceof IntLocation)); | |
107 } else if (destination instanceof LongLocation) { | |
108 return (source instanceof LongLocation || (layout.isAllowedIntToLong() && source instanceof IntLocation)); | |
109 } else if (destination instanceof BooleanLocation) { | |
110 return (source instanceof BooleanLocation); | |
111 } else if (destination instanceof TypedObjectLocation) { | |
112 return source instanceof TypedObjectLocation && ((TypedObjectLocation<?>) destination).getType().isAssignableFrom(((TypedObjectLocation<?>) source).getType()); | |
113 } else if (destination instanceof ValueLocation) { | |
114 return false; | |
115 } else { | |
116 assert destination instanceof ObjectLocation || destination instanceof DualLocation; | |
117 return true; | |
118 } | |
119 } | |
120 | |
121 protected Location existingLocationForValue(Object value, Location oldLocation, Shape oldShape) { | |
122 assert oldShape.getLayout() == this; | |
123 Location newLocation; | |
124 if (oldLocation instanceof IntLocation && value instanceof Integer) { | |
125 newLocation = oldLocation; | |
126 } else if (oldLocation instanceof DoubleLocation && (value instanceof Double || this.isAllowedIntToDouble() && value instanceof Integer)) { | |
127 newLocation = oldLocation; | |
128 } else if (oldLocation instanceof LongLocation && (value instanceof Long || this.isAllowedIntToLong() && value instanceof Long)) { | |
129 newLocation = oldLocation; | |
130 } else if (oldLocation instanceof DeclaredLocation) { | |
131 return oldShape.allocator().locationForValue(value, EnumSet.of(LocationModifier.Final, LocationModifier.NonNull)); | |
132 } else if (oldLocation instanceof ConstantLocation) { | |
133 return LocationImpl.valueEquals(oldLocation.get(null, false), value) ? oldLocation : new Locations.ConstantLocation(value); | |
134 } else if (oldLocation instanceof TypedObjectLocation && !((TypedObjectLocation<?>) oldLocation).getType().isAssignableFrom(value.getClass())) { | |
135 newLocation = (((TypedObjectLocation<?>) oldLocation).toUntypedLocation()); | |
136 } else if (oldLocation instanceof DualLocation) { | |
137 if (oldLocation.canStore(value)) { | |
138 newLocation = oldLocation; | |
139 } else { | |
140 newLocation = ((BaseAllocator) oldShape.allocator()).locationForValueUpcast(value, oldLocation); | |
141 } | |
142 } else if (oldLocation instanceof ObjectLocation) { | |
143 newLocation = oldLocation; | |
144 } else { | |
145 return oldShape.allocator().locationForValue(value, EnumSet.of(LocationModifier.NonNull)); | |
146 } | |
147 if (newLocation instanceof EffectivelyFinalLocation) { | |
148 newLocation = ((EffectivelyFinalLocation<?>) newLocation).toNonFinalLocation(); | |
149 } | |
150 return newLocation; | |
151 } | |
152 | |
153 /** | |
154 * Is this property an upcast of the other property? | |
155 * | |
156 * @param other the property being compared to | |
157 * @return true if this is a upcast of the other property, false otherwise | |
158 */ | |
159 public boolean isPropertyUpcastOf(Property thiz, Property other) { | |
160 if (thiz.getLocation() != null && other.getLocation() != null && other.getKey().equals(thiz.getKey()) && other.getFlags() == thiz.getFlags()) { | |
161 if (isLocationAssignableFrom(thiz.getLocation(), other.getLocation())) { | |
162 return true; | |
163 } | |
164 } | |
165 return false; | |
166 } | |
167 | |
168 @Override | |
169 public abstract Allocator createAllocator(); | |
170 | |
171 public LayoutStrategy getStrategy() { | |
172 return strategy; | |
173 } | |
174 } |