comparison graal/com.oracle.max.cri/src/com/sun/cri/ci/CiVirtualObject.java @ 3733:e233f5660da4

Added Java files from Maxine project.
author Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
date Sat, 17 Dec 2011 19:59:18 +0100
parents
children bc8527f3071c
comparison
equal deleted inserted replaced
3732:3e2e8b8abdaf 3733:e233f5660da4
1 /*
2 * Copyright (c) 2010, 2011, 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.sun.cri.ci;
24
25 import com.sun.cri.ri.*;
26
27 /**
28 * An instance of this class represents an object whose allocation was removed by escape analysis. The information stored in the {@link CiVirtualObject} is used during
29 * deoptimization to recreate the object.
30 */
31 public final class CiVirtualObject extends CiValue {
32
33 private final RiType type;
34 private CiValue[] values;
35 private final int id;
36
37 /**
38 * Creates a new CiVirtualObject for the given type, with the given fields. If the type is an instance class then the values array needs to have one entry for each field, ordered in
39 * like the fields returned by {@link RiResolvedType#declaredFields()}. If the type is an array then the length of the values array determines the reallocated array length.
40 * @param type the type of the object whose allocation was removed during compilation. This can be either an instance of an array type.
41 * @param values an array containing all the values to be stored into the object when it is recreated.
42 * @param id a unique id that identifies the object within the debug information for one position in the compiled code.
43 * @return a new CiVirtualObject instance.
44 */
45 public static CiVirtualObject get(RiType type, CiValue[] values, int id) {
46 return new CiVirtualObject(type, values, id);
47 }
48
49 private CiVirtualObject(RiType type, CiValue[] values, int id) {
50 super(CiKind.Object);
51 this.type = type;
52 this.values = values;
53 this.id = id;
54 }
55
56 @Override
57 public String name() {
58 return "vobject";
59 }
60
61 /**
62 * @return the type of the object whose allocation was removed during compilation. This can be either an instance of an array type.
63 */
64 public RiType type() {
65 return type;
66 }
67
68 /**
69 * @return an array containing all the values to be stored into the object when it is recreated.
70 */
71 public CiValue[] values() {
72 return values;
73 }
74
75 /**
76 * @return the unique id that identifies the object within the debug information for one position in the compiled code.
77 */
78 public int id() {
79 return id;
80 }
81
82 /**
83 * Overwrites the current set of values with a new one.
84 * @param values an array containing all the values to be stored into the object when it is recreated.
85 */
86 public void setValues(CiValue[] values) {
87 this.values = values;
88 }
89
90 @Override
91 public int hashCode() {
92 return kind.ordinal() + type.hashCode();
93 }
94
95 @Override
96 public boolean equals(Object o) {
97 if (o == this) {
98 return true;
99 }
100 if (o instanceof CiVirtualObject) {
101 CiVirtualObject l = (CiVirtualObject) o;
102 if (l.type != type || l.values.length != values.length) {
103 return false;
104 }
105 for (int i = 0; i < values.length; i++) {
106 if (values[i] != l.values[i]) {
107 return false;
108 }
109 }
110 return true;
111 }
112 return false;
113 }
114
115 @Override
116 public boolean equalsIgnoringKind(CiValue o) {
117 return equals(o);
118 }
119
120 /**
121 * This is a helper class used to create virtual objects for a number of different JDK classes.
122 */
123 public static class CiVirtualObjectFactory {
124 private int nextId = 0;
125 private final RiRuntime runtime;
126
127 public CiVirtualObjectFactory(RiRuntime runtime) {
128 this.runtime = runtime;
129 }
130
131 public CiVirtualObject constantProxy(CiKind kind, CiValue objectValue, CiValue primitiveValue) {
132 CiConstant cKind = CiConstant.forObject(kind);
133 // TODO: here the ordering is hard coded... we should query RiType.fields() and act accordingly
134 return new CiVirtualObject(runtime.getType(CiConstant.class), new CiValue[] {cKind, primitiveValue, CiValue.IllegalValue, objectValue}, nextId++);
135 }
136
137 public CiValue proxy(CiValue ciValue) {
138 switch (ciValue.kind) {
139 case Boolean:
140 return new CiVirtualObject(runtime.getType(Boolean.class), new CiValue[] {ciValue}, nextId++);
141 case Byte:
142 return new CiVirtualObject(runtime.getType(Byte.class), new CiValue[] {ciValue}, nextId++);
143 case Char:
144 return new CiVirtualObject(runtime.getType(Character.class), new CiValue[] {ciValue}, nextId++);
145 case Double:
146 return new CiVirtualObject(runtime.getType(Double.class), new CiValue[] {ciValue, CiValue.IllegalValue}, nextId++);
147 case Float:
148 return new CiVirtualObject(runtime.getType(Float.class), new CiValue[] {ciValue}, nextId++);
149 case Int:
150 return new CiVirtualObject(runtime.getType(Integer.class), new CiValue[] {ciValue}, nextId++);
151 case Long:
152 return new CiVirtualObject(runtime.getType(Long.class), new CiValue[] {ciValue, CiValue.IllegalValue}, nextId++);
153 case Object:
154 return ciValue;
155 case Short:
156 return new CiVirtualObject(runtime.getType(Short.class), new CiValue[] {ciValue}, nextId++);
157 default:
158 assert false : ciValue.kind;
159 return null;
160 }
161 }
162
163 public CiVirtualObject arrayProxy(RiType arrayType, CiValue[] values) {
164 return new CiVirtualObject(arrayType, values, nextId++);
165 }
166
167 }
168 }