comparison graal/com.oracle.jvmci.code/src/com/oracle/jvmci/code/VirtualObject.java @ 21556:48c1ebd24120

renamed com.oracle.graal.api[meta|code] modules to com.oracle.jvmci.[meta|code] (JBS:GRAAL-53)
author Doug Simon <doug.simon@oracle.com>
date Wed, 27 May 2015 00:36:16 +0200
parents graal/com.oracle.graal.api.code/src/com/oracle/graal/api/code/VirtualObject.java@082417ac43e4
children f5b549811bac
comparison
equal deleted inserted replaced
21555:d12eaef9af72 21556:48c1ebd24120
1 /*
2 * Copyright (c) 2010, 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.jvmci.code;
24
25 import com.oracle.jvmci.meta.ResolvedJavaField;
26 import com.oracle.jvmci.meta.JavaValue;
27 import com.oracle.jvmci.meta.Kind;
28 import com.oracle.jvmci.meta.AbstractValue;
29 import com.oracle.jvmci.meta.Value;
30 import com.oracle.jvmci.meta.ResolvedJavaType;
31 import com.oracle.jvmci.meta.LIRKind;
32 import java.util.*;
33
34
35 /**
36 * An instance of this class represents an object whose allocation was removed by escape analysis.
37 * The information stored in the {@link VirtualObject} is used during deoptimization to recreate the
38 * object.
39 */
40 public final class VirtualObject extends AbstractValue implements JavaValue {
41
42 private final ResolvedJavaType type;
43 private Value[] values;
44 private final int id;
45
46 /**
47 * Creates a new {@link VirtualObject} for the given type, with the given fields. If
48 * {@code type} is an instance class then {@code values} provides the values for the fields
49 * returned by {@link ResolvedJavaType#getInstanceFields(boolean) getInstanceFields(true)}. If
50 * {@code type} is an array then the length of the values array determines the reallocated array
51 * length.
52 *
53 * @param type the type of the object whose allocation was removed during compilation. This can
54 * be either an instance of an array type.
55 * @param values an array containing all the values to be stored into the object when it is
56 * recreated
57 * @param id a unique id that identifies the object within the debug information for one
58 * position in the compiled code.
59 * @return a new {@link VirtualObject} instance.
60 */
61 public static VirtualObject get(ResolvedJavaType type, Value[] values, int id) {
62 return new VirtualObject(type, values, id);
63 }
64
65 private VirtualObject(ResolvedJavaType type, Value[] values, int id) {
66 super(LIRKind.reference(Kind.Object));
67 this.type = type;
68 this.values = values;
69 this.id = id;
70 }
71
72 private static StringBuilder appendValue(StringBuilder buf, Value value, Set<VirtualObject> visited) {
73 if (value instanceof VirtualObject) {
74 VirtualObject vo = (VirtualObject) value;
75 buf.append("vobject:").append(vo.type.toJavaName(false)).append(':').append(vo.id);
76 if (!visited.contains(vo)) {
77 visited.add(vo);
78 buf.append('{');
79 if (vo.values == null) {
80 buf.append("<uninitialized>");
81 } else {
82 if (vo.type.isArray()) {
83 for (int i = 0; i < vo.values.length; i++) {
84 if (i != 0) {
85 buf.append(',');
86 }
87 buf.append(i).append('=');
88 appendValue(buf, vo.values[i], visited);
89 }
90 } else {
91 ResolvedJavaField[] fields = vo.type.getInstanceFields(true);
92 assert fields.length == vo.values.length : vo.type + ", fields=" + Arrays.toString(fields) + ", values=" + Arrays.toString(vo.values);
93 for (int i = 0; i < vo.values.length; i++) {
94 if (i != 0) {
95 buf.append(',');
96 }
97 buf.append(fields[i].getName()).append('=');
98 appendValue(buf, vo.values[i], visited);
99 }
100 }
101 }
102 buf.append('}');
103 }
104 } else {
105 buf.append(value);
106 }
107 return buf;
108 }
109
110 @Override
111 public String toString() {
112 Set<VirtualObject> visited = Collections.newSetFromMap(new IdentityHashMap<VirtualObject, Boolean>());
113 return appendValue(new StringBuilder(), this, visited).toString();
114 }
115
116 /**
117 * Returns the type of the object whose allocation was removed during compilation. This can be
118 * either an instance of an array type.
119 */
120 public ResolvedJavaType getType() {
121 return type;
122 }
123
124 /**
125 * Returns an array containing all the values to be stored into the object when it is recreated.
126 */
127 public Value[] getValues() {
128 return values;
129 }
130
131 /**
132 * Returns the unique id that identifies the object within the debug information for one
133 * position in the compiled code.
134 */
135 public int getId() {
136 return id;
137 }
138
139 private static boolean checkValues(ResolvedJavaType type, Value[] values) {
140 if (values != null) {
141 if (!type.isArray()) {
142 ResolvedJavaField[] fields = type.getInstanceFields(true);
143 int fieldIndex = 0;
144 for (int i = 0; i < values.length; i++) {
145 ResolvedJavaField field = fields[fieldIndex++];
146 Kind valKind = values[i].getKind().getStackKind();
147 if (field.getKind() == Kind.Object) {
148 assert values[i].getLIRKind().isReference(0) : field + ": " + valKind + " != " + field.getKind();
149 } else {
150 if ((valKind == Kind.Double || valKind == Kind.Long) && field.getKind() == Kind.Int) {
151 assert fields[fieldIndex].getKind() == Kind.Int;
152 fieldIndex++;
153 } else {
154 assert valKind == field.getKind().getStackKind() : field + ": " + valKind + " != " + field.getKind();
155 }
156 }
157 }
158 assert fields.length == fieldIndex : type + ": fields=" + Arrays.toString(fields) + ", field values=" + Arrays.toString(values);
159 } else {
160 Kind componentKind = type.getComponentType().getKind().getStackKind();
161 if (componentKind == Kind.Object) {
162 for (int i = 0; i < values.length; i++) {
163 assert values[i].getLIRKind().isReference(0) : values[i].getKind() + " != " + componentKind;
164 }
165 } else {
166 for (int i = 0; i < values.length; i++) {
167 assert values[i].getKind() == componentKind || componentKind.getBitCount() >= values[i].getKind().getBitCount() ||
168 (componentKind == Kind.Int && values[i].getKind().getBitCount() >= Kind.Int.getBitCount()) : values[i].getKind() + " != " + componentKind;
169 }
170 }
171 }
172 }
173 return true;
174 }
175
176 /**
177 * Overwrites the current set of values with a new one.
178 *
179 * @param values an array containing all the values to be stored into the object when it is
180 * recreated.
181 */
182 public void setValues(Value[] values) {
183 assert checkValues(type, values);
184 this.values = values;
185 }
186
187 @Override
188 public int hashCode() {
189 return getLIRKind().hashCode() + type.hashCode();
190 }
191
192 @Override
193 public boolean equals(Object o) {
194 if (o == this) {
195 return true;
196 }
197 if (o instanceof VirtualObject) {
198 VirtualObject l = (VirtualObject) o;
199 if (!l.type.equals(type) || l.values.length != values.length) {
200 return false;
201 }
202 for (int i = 0; i < values.length; i++) {
203 /*
204 * Virtual objects can form cycles. Calling equals() could therefore lead to
205 * infinite recursion.
206 */
207 if (!same(values[i], l.values[i])) {
208 return false;
209 }
210 }
211 return true;
212 }
213 return false;
214 }
215
216 private static boolean same(Object o1, Object o2) {
217 return o1 == o2;
218 }
219 }