Mercurial > hg > graal-compiler
comparison graal/com.oracle.max.cri/src/com/sun/cri/ci/CiAddress.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 /** | |
26 * Represents an address in target machine memory, specified via some combination of a base register, an index register, | |
27 * a displacement and a scale. Note that the base and index registers may be {@link CiVariable variable}, that is as yet | |
28 * unassigned to target machine registers. | |
29 */ | |
30 public final class CiAddress extends CiValue { | |
31 | |
32 /** | |
33 * A sentinel value used as a place holder in an instruction stream for an address that will be patched. | |
34 */ | |
35 public static final CiAddress Placeholder = new CiAddress(CiKind.Illegal, CiRegister.None.asValue()); | |
36 | |
37 /** | |
38 * Base register that defines the start of the address computation; always present. | |
39 */ | |
40 public final CiValue base; | |
41 /** | |
42 * Optional index register, the value of which (possibly scaled by {@link #scale}) is added to {@link #base}. | |
43 * If not present, is denoted by {@link CiValue#IllegalValue}. | |
44 */ | |
45 public final CiValue index; | |
46 /** | |
47 * Scaling factor for indexing, dependent on target operand size. | |
48 */ | |
49 public final Scale scale; | |
50 /** | |
51 * Optional additive displacement. | |
52 */ | |
53 public final int displacement; | |
54 | |
55 /** | |
56 * Creates a {@code CiAddress} with given base register, no scaling and no displacement. | |
57 * @param kind the kind of the value being addressed | |
58 * @param base the base register | |
59 */ | |
60 public CiAddress(CiKind kind, CiValue base) { | |
61 this(kind, base, IllegalValue, Scale.Times1, 0); | |
62 } | |
63 | |
64 /** | |
65 * Creates a {@code CiAddress} with given base register, no scaling and a given displacement. | |
66 * @param kind the kind of the value being addressed | |
67 * @param base the base register | |
68 * @param displacement the displacement | |
69 */ | |
70 public CiAddress(CiKind kind, CiValue base, int displacement) { | |
71 this(kind, base, IllegalValue, Scale.Times1, displacement); | |
72 } | |
73 | |
74 /** | |
75 * Creates a {@code CiAddress} with given base and offset registers, no scaling and no displacement. | |
76 * @param kind the kind of the value being addressed | |
77 * @param base the base register | |
78 * @param offset the offset register | |
79 */ | |
80 public CiAddress(CiKind kind, CiValue base, CiValue offset) { | |
81 this(kind, base, offset, Scale.Times1, 0); | |
82 } | |
83 | |
84 /** | |
85 * Creates a {@code CiAddress} with given base and index registers, scaling and displacement. | |
86 * This is the most general constructor.. | |
87 * @param kind the kind of the value being addressed | |
88 * @param base the base register | |
89 * @param index the index register | |
90 * @param scale the scaling factor | |
91 * @param displacement the displacement | |
92 */ | |
93 public CiAddress(CiKind kind, CiValue base, CiValue index, Scale scale, int displacement) { | |
94 super(kind); | |
95 | |
96 if (index.isConstant()) { | |
97 long longIndex = ((CiConstant) index).asLong(); | |
98 long longDisp = displacement + longIndex * scale.value; | |
99 if ((int) longIndex != longIndex || (int) longDisp != longDisp) { | |
100 throw new Error("integer overflow when computing constant displacement"); | |
101 } | |
102 displacement = (int) longDisp; | |
103 index = IllegalValue; | |
104 scale = Scale.Times1; | |
105 } | |
106 assert base.isIllegal() || base.isVariableOrRegister(); | |
107 assert index.isIllegal() || index.isVariableOrRegister(); | |
108 | |
109 this.base = base; | |
110 this.index = index; | |
111 this.scale = scale; | |
112 this.displacement = displacement; | |
113 } | |
114 | |
115 /** | |
116 * A scaling factor used in complex addressing modes such as those supported by x86 platforms. | |
117 */ | |
118 public enum Scale { | |
119 Times1(1, 0), | |
120 Times2(2, 1), | |
121 Times4(4, 2), | |
122 Times8(8, 3); | |
123 | |
124 Scale(int value, int log2) { | |
125 this.value = value; | |
126 this.log2 = log2; | |
127 } | |
128 | |
129 /** | |
130 * The value (or multiplier) of this scale. | |
131 */ | |
132 public final int value; | |
133 | |
134 /** | |
135 * The {@linkplain #value value} of this scale log 2. | |
136 */ | |
137 public final int log2; | |
138 | |
139 public static Scale fromInt(int scale) { | |
140 // Checkstyle: stop | |
141 switch (scale) { | |
142 case 1: return Times1; | |
143 case 2: return Times2; | |
144 case 4: return Times4; | |
145 case 8: return Times8; | |
146 default: throw new IllegalArgumentException(String.valueOf(scale)); | |
147 } | |
148 // Checkstyle: resume | |
149 } | |
150 | |
151 public static Scale fromShift(int shift) { | |
152 return fromInt(1 << shift); | |
153 } | |
154 } | |
155 | |
156 /** | |
157 * If the base register is a {@link CiRegisterValue} returns the associated {@link CiRegister} | |
158 * otherwise raises an exception.. | |
159 * @return the base {@link CiRegister} | |
160 * @exception Error if {@code base} is not a {@link CiRegisterValue} | |
161 */ | |
162 public CiRegister base() { | |
163 return base.asRegister(); | |
164 } | |
165 | |
166 /** | |
167 * If the index register is a {@link CiRegisterValue} returns the associated {@link CiRegister} | |
168 * otherwise raises an exception.. | |
169 * @return the base {@link CiRegister} | |
170 * @exception Error if {@code index} is not a {@link CiRegisterValue} | |
171 */ | |
172 public CiRegister index() { | |
173 return index.asRegister(); | |
174 } | |
175 | |
176 /** | |
177 * Encodes the possible addressing modes as a simple value. | |
178 */ | |
179 public enum Format { | |
180 BASE, | |
181 BASE_DISP, | |
182 BASE_INDEX, | |
183 BASE_INDEX_DISP, | |
184 PLACEHOLDER; | |
185 } | |
186 | |
187 /** | |
188 * Returns the {@link Format encoded addressing mode} that this {@code CiAddress} represents. | |
189 * @return the encoded addressing mode | |
190 */ | |
191 public Format format() { | |
192 if (this == Placeholder) { | |
193 return Format.PLACEHOLDER; | |
194 } | |
195 assert base.isLegal(); | |
196 if (index.isLegal()) { | |
197 if (displacement != 0) { | |
198 return Format.BASE_INDEX_DISP; | |
199 } else { | |
200 return Format.BASE_INDEX; | |
201 } | |
202 } else { | |
203 if (displacement != 0) { | |
204 return Format.BASE_DISP; | |
205 } else { | |
206 return Format.BASE; | |
207 } | |
208 } | |
209 } | |
210 | |
211 private static String s(CiValue location) { | |
212 if (location.isRegister()) { | |
213 return location.asRegister().name; | |
214 } | |
215 assert location.isVariable(); | |
216 return "v" + ((CiVariable) location).index; | |
217 } | |
218 | |
219 private static String signed(int i) { | |
220 if (i >= 0) { | |
221 return "+" + i; | |
222 } | |
223 return String.valueOf(i); | |
224 } | |
225 | |
226 @Override | |
227 public String name() { | |
228 // Checkstyle: stop | |
229 switch (format()) { | |
230 case BASE : return "[" + s(base) + "]"; | |
231 case BASE_DISP : return "[" + s(base) + signed(displacement) + "]"; | |
232 case BASE_INDEX : return "[" + s(base) + "+" + s(index) + "]"; | |
233 case BASE_INDEX_DISP : return "[" + s(base) + "+(" + s(index) + "*" + scale.value + ")" + signed(displacement) + "]"; | |
234 case PLACEHOLDER : return "[<placeholder>]"; | |
235 default : throw new IllegalArgumentException("unknown format: " + format()); | |
236 } | |
237 // Checkstyle: resume | |
238 } | |
239 | |
240 @Override | |
241 public boolean equals(Object obj) { | |
242 if (obj instanceof CiAddress) { | |
243 CiAddress addr = (CiAddress) obj; | |
244 return kind == addr.kind && displacement == addr.displacement && base.equals(addr.base) && scale == addr.scale && index.equals(addr.index); | |
245 } | |
246 return false; | |
247 } | |
248 | |
249 @Override | |
250 public boolean equalsIgnoringKind(CiValue o) { | |
251 if (o instanceof CiAddress) { | |
252 CiAddress addr = (CiAddress) o; | |
253 return displacement == addr.displacement && base.equalsIgnoringKind(addr.base) && scale == addr.scale && index.equalsIgnoringKind(addr.index); | |
254 } | |
255 return false; | |
256 } | |
257 | |
258 @Override | |
259 public int hashCode() { | |
260 return (base.hashCode() << 4) | kind.ordinal(); | |
261 } | |
262 } |