13514
|
1 /*
|
|
2 * Copyright (c) 2013 Oracle and/or its affiliates. All rights reserved. This
|
|
3 * code is released under a tri EPL/GPL/LGPL license. You can use it,
|
|
4 * redistribute it and/or modify it under the terms of the:
|
|
5 *
|
|
6 * Eclipse Public License version 1.0
|
|
7 * GNU General Public License version 2
|
|
8 * GNU Lesser General Public License version 2.1
|
|
9 */
|
|
10 package com.oracle.truffle.ruby.runtime.objects;
|
|
11
|
|
12 import java.lang.reflect.*;
|
|
13 import java.util.*;
|
|
14 import java.util.Map.Entry;
|
|
15
|
|
16 import sun.misc.*;
|
|
17
|
|
18 import com.oracle.truffle.api.nodes.*;
|
|
19 import com.oracle.truffle.api.nodes.NodeUtil.*;
|
|
20 import com.oracle.truffle.ruby.runtime.*;
|
|
21
|
|
22 /**
|
|
23 * Maps names of instance variables to storage locations, which are either the offset of a primitive
|
|
24 * field in {@code RubyBasicObject}, or an index into an object array in {@code RubyBasicObject}.
|
|
25 * Object layouts are chained, with each having zero or one parents.
|
|
26 * <p>
|
|
27 * Object layouts are immutable, with the methods for adding new instance variables of generalizing
|
|
28 * the type of existing instance variables returning new object layouts.
|
|
29 */
|
|
30 public class ObjectLayout {
|
|
31
|
|
32 public static final ObjectLayout EMPTY = new ObjectLayout("(empty)");
|
|
33
|
|
34 private final String originHint;
|
|
35
|
|
36 private final ObjectLayout parent;
|
|
37
|
|
38 private final Map<String, StorageLocation> storageLocations = new HashMap<>();
|
|
39
|
|
40 private final int primitiveStorageLocationsUsed;
|
|
41 private final int objectStorageLocationsUsed;
|
|
42
|
|
43 public static final long FIRST_OFFSET = getFirstOffset();
|
|
44
|
|
45 private ObjectLayout(String originHint) {
|
|
46 this.originHint = originHint;
|
|
47 this.parent = null;
|
|
48 primitiveStorageLocationsUsed = 0;
|
|
49 objectStorageLocationsUsed = 0;
|
|
50 }
|
|
51
|
|
52 public ObjectLayout(String originHint, RubyContext context, ObjectLayout parent) {
|
|
53 this(originHint, context, parent, new HashMap<String, Class>());
|
|
54 }
|
|
55
|
|
56 public ObjectLayout(String originHint, RubyContext context, ObjectLayout parent, Map<String, Class> storageTypes) {
|
|
57 this.originHint = originHint;
|
|
58 this.parent = parent;
|
|
59
|
|
60 // Start our offsets from where the parent ends
|
|
61
|
|
62 int primitiveStorageLocationIndex;
|
|
63 int objectStorageLocationIndex;
|
|
64
|
|
65 if (parent == null) {
|
|
66 primitiveStorageLocationIndex = 0;
|
|
67 objectStorageLocationIndex = 0;
|
|
68 } else {
|
|
69 primitiveStorageLocationIndex = parent.primitiveStorageLocationsUsed;
|
|
70 objectStorageLocationIndex = parent.objectStorageLocationsUsed;
|
|
71 }
|
|
72
|
|
73 // Go through the variables we've been asked to store
|
|
74
|
|
75 for (Entry<String, Class> entry : storageTypes.entrySet()) {
|
|
76 // TODO(cs): what if parent has it, but we need a more general type?
|
|
77
|
|
78 final String name = entry.getKey();
|
|
79 final Class type = entry.getValue();
|
|
80
|
|
81 if (parent == null || parent.findStorageLocation(name) == null) {
|
|
82 boolean canStoreInPrimitive = false;
|
|
83 int primitivesNeeded = 0;
|
|
84
|
|
85 if (type == Integer.class) {
|
|
86 canStoreInPrimitive = true;
|
|
87 primitivesNeeded = 1;
|
|
88 } else if (type == Double.class) {
|
|
89 canStoreInPrimitive = true;
|
|
90 primitivesNeeded = 2;
|
|
91 }
|
|
92
|
|
93 if (canStoreInPrimitive && primitiveStorageLocationIndex + primitivesNeeded <= RubyBasicObject.PRIMITIVE_STORAGE_LOCATIONS_COUNT) {
|
|
94 final long offset = FIRST_OFFSET + Unsafe.ARRAY_INT_INDEX_SCALE * primitiveStorageLocationIndex;
|
|
95 final int mask = 1 << primitiveStorageLocationIndex;
|
|
96
|
|
97 StorageLocation newStorageLocation = null;
|
|
98
|
|
99 if (type == Integer.class) {
|
|
100 newStorageLocation = new FixnumStorageLocation(this, offset, mask);
|
|
101 } else if (type == Double.class) {
|
|
102 newStorageLocation = new FloatStorageLocation(this, offset, mask);
|
|
103 }
|
|
104
|
|
105 storageLocations.put(entry.getKey(), newStorageLocation);
|
|
106 primitiveStorageLocationIndex += primitivesNeeded;
|
|
107 } else {
|
|
108 if (canStoreInPrimitive && context.getConfiguration().getPrintSpiltInstanceVariables()) {
|
|
109 context.implementationMessage("instance variable %s of type %s spilt due to lack of space", name, type.getName());
|
|
110 }
|
|
111
|
|
112 final ObjectStorageLocation newStorageLocation = new ObjectStorageLocation(this, objectStorageLocationIndex);
|
|
113 storageLocations.put(entry.getKey(), newStorageLocation);
|
|
114 objectStorageLocationIndex++;
|
|
115 }
|
|
116 }
|
|
117 }
|
|
118
|
|
119 primitiveStorageLocationsUsed = primitiveStorageLocationIndex;
|
|
120 objectStorageLocationsUsed = objectStorageLocationIndex;
|
|
121 }
|
|
122
|
|
123 /**
|
|
124 * Create a new version of this layout, but with a different parent. The new parent probably
|
|
125 * comes from the same Ruby class as it did, but it's a new layout because layouts are
|
|
126 * immutable, so modifications to the superclass yields a new layout.
|
|
127 */
|
|
128 public ObjectLayout renew(RubyContext context, ObjectLayout newParent) {
|
|
129 return new ObjectLayout(originHint + ".renewed", context, newParent, getStorageTypes());
|
|
130 }
|
|
131
|
|
132 /**
|
|
133 * Create a new version of this layout but with a new variable.
|
|
134 */
|
|
135 public ObjectLayout withNewVariable(RubyContext context, String name, Class type) {
|
|
136 final Map<String, Class> storageTypes = getStorageTypes();
|
|
137 storageTypes.put(name, type);
|
|
138 return new ObjectLayout(originHint + ".withnew", context, parent, storageTypes);
|
|
139 }
|
|
140
|
|
141 /**
|
|
142 * Create a new version of this layout but with an existing variable generalized to support any
|
|
143 * type.
|
|
144 */
|
|
145 public ObjectLayout withGeneralisedVariable(RubyContext context, String name) {
|
|
146 return withNewVariable(context, name, Object.class);
|
|
147 }
|
|
148
|
|
149 /**
|
|
150 * Get a map of instance variable names to the type that they store.
|
|
151 */
|
|
152 public Map<String, Class> getStorageTypes() {
|
|
153 Map<String, Class> storageTypes = new HashMap<>();
|
|
154
|
|
155 for (Entry<String, StorageLocation> entry : storageLocations.entrySet()) {
|
|
156 final String name = entry.getKey();
|
|
157 final StorageLocation storageLocation = entry.getValue();
|
|
158
|
|
159 if (storageLocation.getStoredClass() != null) {
|
|
160 storageTypes.put(name, storageLocation.getStoredClass());
|
|
161 }
|
|
162 }
|
|
163
|
|
164 return storageTypes;
|
|
165 }
|
|
166
|
|
167 /**
|
|
168 * Get a map of instance variable names to the type that they store, but including both this
|
|
169 * layout and all parent layouts.
|
|
170 */
|
|
171 public Map<String, StorageLocation> getAllStorageLocations() {
|
|
172 final Map<String, StorageLocation> allStorageLocations = new HashMap<>();
|
|
173
|
|
174 allStorageLocations.putAll(storageLocations);
|
|
175
|
|
176 if (parent != null) {
|
|
177 allStorageLocations.putAll(parent.getAllStorageLocations());
|
|
178 }
|
|
179
|
|
180 return allStorageLocations;
|
|
181 }
|
|
182
|
|
183 /**
|
|
184 * Find a storage location from a name, including in parents.
|
|
185 */
|
|
186 public StorageLocation findStorageLocation(String name) {
|
|
187 final StorageLocation storageLocation = storageLocations.get(name);
|
|
188
|
|
189 if (storageLocation != null) {
|
|
190 return storageLocation;
|
|
191 }
|
|
192
|
|
193 if (parent == null) {
|
|
194 return null;
|
|
195 }
|
|
196
|
|
197 return parent.findStorageLocation(name);
|
|
198 }
|
|
199
|
|
200 public int getObjectStorageLocationsUsed() {
|
|
201 return objectStorageLocationsUsed;
|
|
202 }
|
|
203
|
|
204 /**
|
|
205 * Does this layout include another layout? That is, is that other layout somewhere in the chain
|
|
206 * of parents? We say 'include' because all of the variables in a parent layout are available in
|
|
207 * your layout as well.
|
|
208 */
|
|
209 public boolean contains(ObjectLayout other) {
|
|
210 ObjectLayout layout = this;
|
|
211
|
|
212 do {
|
|
213 if (other == layout) {
|
|
214 return true;
|
|
215 }
|
|
216
|
|
217 layout = layout.parent;
|
|
218 } while (layout != null);
|
|
219
|
|
220 return false;
|
|
221 }
|
|
222
|
|
223 public String getOriginHint() {
|
|
224 return originHint;
|
|
225 }
|
|
226
|
|
227 private static long getFirstOffset() {
|
|
228 try {
|
|
229 final Field fieldOffsetProviderField = NodeUtil.class.getDeclaredField("unsafeFieldOffsetProvider");
|
|
230 fieldOffsetProviderField.setAccessible(true);
|
|
231 final FieldOffsetProvider fieldOffsetProvider = (FieldOffsetProvider) fieldOffsetProviderField.get(null);
|
|
232
|
|
233 final Field firstPrimitiveField = RubyBasicObject.class.getDeclaredField("primitiveStorageLocation01");
|
|
234 return fieldOffsetProvider.objectFieldOffset(firstPrimitiveField);
|
|
235 } catch (NoSuchFieldException | IllegalAccessException e) {
|
|
236 throw new RuntimeException(e);
|
|
237 }
|
|
238 }
|
|
239
|
|
240 }
|