Mercurial > hg > graal-jvmci-8
comparison jvmci/jdk.vm.ci.code/src/jdk/vm/ci/code/ValueUtil.java @ 22672:1bbd4a7c274b
Rename jdk.internal.jvmci to jdk.vm.ci
author | Tom Rodriguez <tom.rodriguez@oracle.com> |
---|---|
date | Thu, 08 Oct 2015 17:28:41 -0700 |
parents | jvmci/jdk.internal.jvmci.code/src/jdk/internal/jvmci/code/ValueUtil.java@6a7f2f656ed9 |
children | f48b657b550d |
comparison
equal
deleted
inserted
replaced
22671:97f30e4d0e95 | 22672:1bbd4a7c274b |
---|---|
1 /* | |
2 * Copyright (c) 2012, 2015, 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 jdk.vm.ci.code; | |
24 | |
25 import java.util.ArrayList; | |
26 import java.util.List; | |
27 | |
28 import jdk.vm.ci.meta.AllocatableValue; | |
29 import jdk.vm.ci.meta.JavaConstant; | |
30 import jdk.vm.ci.meta.JavaValue; | |
31 import jdk.vm.ci.meta.PlatformKind; | |
32 import jdk.vm.ci.meta.Value; | |
33 | |
34 /** | |
35 * Utility class for working with the {@link Value} class and its subclasses. | |
36 */ | |
37 public final class ValueUtil { | |
38 | |
39 public static boolean isIllegal(Value value) { | |
40 assert value != null; | |
41 return Value.ILLEGAL.equals(value); | |
42 } | |
43 | |
44 public static boolean isIllegalJavaValue(JavaValue value) { | |
45 assert value != null; | |
46 return Value.ILLEGAL.equals(value); | |
47 } | |
48 | |
49 public static boolean isLegal(Value value) { | |
50 return !isIllegal(value); | |
51 } | |
52 | |
53 public static boolean isVirtualObject(JavaValue value) { | |
54 assert value != null; | |
55 return value instanceof VirtualObject; | |
56 } | |
57 | |
58 public static VirtualObject asVirtualObject(JavaValue value) { | |
59 assert value != null; | |
60 return (VirtualObject) value; | |
61 } | |
62 | |
63 public static boolean isConstantJavaValue(JavaValue value) { | |
64 assert value != null; | |
65 return value instanceof JavaConstant; | |
66 } | |
67 | |
68 public static JavaConstant asConstantJavaValue(JavaValue value) { | |
69 assert value != null; | |
70 return (JavaConstant) value; | |
71 } | |
72 | |
73 public static boolean isAllocatableValue(Value value) { | |
74 assert value != null; | |
75 return value instanceof AllocatableValue; | |
76 } | |
77 | |
78 public static AllocatableValue asAllocatableValue(Value value) { | |
79 assert value != null; | |
80 return (AllocatableValue) value; | |
81 } | |
82 | |
83 public static boolean isStackSlot(Value value) { | |
84 assert value != null; | |
85 return value instanceof StackSlot; | |
86 } | |
87 | |
88 public static StackSlot asStackSlot(Value value) { | |
89 assert value != null; | |
90 return (StackSlot) value; | |
91 } | |
92 | |
93 public static boolean isStackSlotValue(Value value) { | |
94 assert value != null; | |
95 return value instanceof StackSlotValue; | |
96 } | |
97 | |
98 public static StackSlotValue asStackSlotValue(Value value) { | |
99 assert value != null; | |
100 return (StackSlotValue) value; | |
101 } | |
102 | |
103 public static boolean isVirtualStackSlot(Value value) { | |
104 assert value != null; | |
105 return value instanceof VirtualStackSlot; | |
106 } | |
107 | |
108 public static VirtualStackSlot asVirtualStackSlot(Value value) { | |
109 assert value != null; | |
110 return (VirtualStackSlot) value; | |
111 } | |
112 | |
113 public static boolean isRegister(Value value) { | |
114 assert value != null; | |
115 return value instanceof RegisterValue; | |
116 } | |
117 | |
118 public static Register asRegister(Value value) { | |
119 return asRegisterValue(value).getRegister(); | |
120 } | |
121 | |
122 public static RegisterValue asRegisterValue(Value value) { | |
123 assert value != null; | |
124 return (RegisterValue) value; | |
125 } | |
126 | |
127 public static Register asRegister(Value value, PlatformKind kind) { | |
128 if (value.getPlatformKind() != kind) { | |
129 throw new InternalError("needed: " + kind + " got: " + value.getPlatformKind()); | |
130 } else { | |
131 return asRegister(value); | |
132 } | |
133 } | |
134 | |
135 public static boolean sameRegister(Value v1, Value v2) { | |
136 return isRegister(v1) && isRegister(v2) && asRegister(v1).equals(asRegister(v2)); | |
137 } | |
138 | |
139 public static boolean sameRegister(Value v1, Value v2, Value v3) { | |
140 return sameRegister(v1, v2) && sameRegister(v1, v3); | |
141 } | |
142 | |
143 /** | |
144 * Checks if all the provided values are different physical registers. The parameters can be | |
145 * either {@link Register registers}, {@link Value values} or arrays of them. All values that | |
146 * are not {@link RegisterValue registers} are ignored. | |
147 */ | |
148 public static boolean differentRegisters(Object... values) { | |
149 List<Register> registers = collectRegisters(values, new ArrayList<Register>()); | |
150 for (int i = 1; i < registers.size(); i++) { | |
151 Register r1 = registers.get(i); | |
152 for (int j = 0; j < i; j++) { | |
153 Register r2 = registers.get(j); | |
154 if (r1.equals(r2)) { | |
155 return false; | |
156 } | |
157 } | |
158 } | |
159 return true; | |
160 } | |
161 | |
162 private static List<Register> collectRegisters(Object[] values, List<Register> registers) { | |
163 for (Object o : values) { | |
164 if (o instanceof Register) { | |
165 registers.add((Register) o); | |
166 } else if (o instanceof Value) { | |
167 if (isRegister((Value) o)) { | |
168 registers.add(asRegister((Value) o)); | |
169 } | |
170 } else if (o instanceof Object[]) { | |
171 collectRegisters((Object[]) o, registers); | |
172 } else { | |
173 throw new IllegalArgumentException("Not a Register or Value: " + o); | |
174 } | |
175 } | |
176 return registers; | |
177 } | |
178 | |
179 /** | |
180 * Subtract sets of registers (x - y). | |
181 * | |
182 * @param x a set of register to subtract from. | |
183 * @param y a set of registers to subtract. | |
184 * @return resulting set of registers (x - y). | |
185 */ | |
186 public static Value[] subtractRegisters(Value[] x, Value[] y) { | |
187 ArrayList<Value> result = new ArrayList<>(x.length); | |
188 for (Value i : x) { | |
189 boolean append = true; | |
190 for (Value j : y) { | |
191 if (ValueUtil.sameRegister(i, j)) { | |
192 append = false; | |
193 break; | |
194 } | |
195 } | |
196 if (append) { | |
197 result.add(i); | |
198 } | |
199 } | |
200 Value[] resultArray = new Value[result.size()]; | |
201 return result.toArray(resultArray); | |
202 } | |
203 } |