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