comparison graal/com.oracle.jvmci.code/src/com/oracle/jvmci/code/ValueUtil.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/ValueUtil.java@50b6f616b3c7
children f5b549811bac
comparison
equal deleted inserted replaced
21555:d12eaef9af72 21556:48c1ebd24120
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.jvmci.code;
24
25 import com.oracle.jvmci.meta.Kind;
26 import com.oracle.jvmci.meta.Value;
27 import com.oracle.jvmci.meta.AllocatableValue;
28 import com.oracle.jvmci.meta.JavaConstant;
29 import java.util.*;
30
31
32 /**
33 * Utility class for working with the {@link Value} class and its subclasses.
34 */
35 public final class ValueUtil {
36
37 public static boolean isIllegal(Value value) {
38 assert value != null;
39 return Value.ILLEGAL.equals(value);
40 }
41
42 public static boolean isLegal(Value value) {
43 return !isIllegal(value);
44 }
45
46 public static boolean isVirtualObject(Value value) {
47 assert value != null;
48 return value instanceof VirtualObject;
49 }
50
51 public static VirtualObject asVirtualObject(Value value) {
52 assert value != null;
53 return (VirtualObject) value;
54 }
55
56 public static boolean isConstant(Value value) {
57 assert value != null;
58 return value instanceof JavaConstant;
59 }
60
61 public static JavaConstant asConstant(Value value) {
62 assert value != null;
63 return (JavaConstant) value;
64 }
65
66 public static boolean isAllocatableValue(Value value) {
67 assert value != null;
68 return value instanceof AllocatableValue;
69 }
70
71 public static AllocatableValue asAllocatableValue(Value value) {
72 assert value != null;
73 return (AllocatableValue) value;
74 }
75
76 public static boolean isStackSlot(Value value) {
77 assert value != null;
78 return value instanceof StackSlot;
79 }
80
81 public static StackSlot asStackSlot(Value value) {
82 assert value != null;
83 return (StackSlot) value;
84 }
85
86 public static boolean isStackSlotValue(Value value) {
87 assert value != null;
88 return value instanceof StackSlotValue;
89 }
90
91 public static StackSlotValue asStackSlotValue(Value value) {
92 assert value != null;
93 return (StackSlotValue) value;
94 }
95
96 public static boolean isVirtualStackSlot(Value value) {
97 assert value != null;
98 return value instanceof VirtualStackSlot;
99 }
100
101 public static VirtualStackSlot asVirtualStackSlot(Value value) {
102 assert value != null;
103 return (VirtualStackSlot) value;
104 }
105
106 public static boolean isRegister(Value value) {
107 assert value != null;
108 return value instanceof RegisterValue;
109 }
110
111 public static Register asRegister(Value value) {
112 assert value != null;
113 return ((RegisterValue) value).getRegister();
114 }
115
116 public static Register asIntReg(Value value) {
117 if (value.getKind().getStackKind() != Kind.Int) {
118 throw new InternalError("needed Int got: " + value.getKind());
119 } else {
120 return asRegister(value);
121 }
122 }
123
124 public static Register asLongReg(Value value) {
125 if (value.getKind() != Kind.Long) {
126 throw new InternalError("needed Long got: " + value.getKind());
127 } else {
128 return asRegister(value);
129 }
130 }
131
132 public static Register asObjectReg(Value value) {
133 assert value.getKind() == Kind.Object : value.getKind();
134 return asRegister(value);
135 }
136
137 public static Register asFloatReg(Value value) {
138 assert value.getKind() == Kind.Float : value.getKind();
139 return asRegister(value);
140 }
141
142 public static Register asDoubleReg(Value value) {
143 assert value.getKind() == Kind.Double : value.getKind();
144 return asRegister(value);
145 }
146
147 public static boolean sameRegister(Value v1, Value v2) {
148 return isRegister(v1) && isRegister(v2) && asRegister(v1).equals(asRegister(v2));
149 }
150
151 public static boolean sameRegister(Value v1, Value v2, Value v3) {
152 return sameRegister(v1, v2) && sameRegister(v1, v3);
153 }
154
155 /**
156 * Checks if all the provided values are different physical registers. The parameters can be
157 * either {@link Register registers}, {@link Value values} or arrays of them. All values that
158 * are not {@link RegisterValue registers} are ignored.
159 */
160 public static boolean differentRegisters(Object... values) {
161 List<Register> registers = collectRegisters(values, new ArrayList<Register>());
162 for (int i = 1; i < registers.size(); i++) {
163 Register r1 = registers.get(i);
164 for (int j = 0; j < i; j++) {
165 Register r2 = registers.get(j);
166 if (r1.equals(r2)) {
167 return false;
168 }
169 }
170 }
171 return true;
172 }
173
174 private static List<Register> collectRegisters(Object[] values, List<Register> registers) {
175 for (Object o : values) {
176 if (o instanceof Register) {
177 registers.add((Register) o);
178 } else if (o instanceof Value) {
179 if (isRegister((Value) o)) {
180 registers.add(asRegister((Value) o));
181 }
182 } else if (o instanceof Object[]) {
183 collectRegisters((Object[]) o, registers);
184 } else {
185 throw new IllegalArgumentException("Not a Register or Value: " + o);
186 }
187 }
188 return registers;
189 }
190 }