blob: 7e50c311da7f699f0f7abb1fa180c6e9bd1835e3 [file] [log] [blame]
Bill Buzbee00e1ec62014-02-27 23:44:13 +00001/*
2 * Copyright (C) 2014 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#ifndef ART_COMPILER_DEX_REG_STORAGE_H_
18#define ART_COMPILER_DEX_REG_STORAGE_H_
19
20
21namespace art {
22
23/*
buzbee091cc402014-03-31 10:14:40 -070024 * 16-bit representation of the physical register container holding a Dalvik value.
buzbeed111c6e2014-05-11 21:09:53 -070025 * The encoding allows up to 64 physical elements per storage class, and supports eight
buzbee091cc402014-03-31 10:14:40 -070026 * register container shapes.
Bill Buzbee00e1ec62014-02-27 23:44:13 +000027 *
buzbeed111c6e2014-05-11 21:09:53 -070028 * [V] [HHHHH] [SSS] [F] [LLLLLL]
Bill Buzbee00e1ec62014-02-27 23:44:13 +000029 *
buzbeed111c6e2014-05-11 21:09:53 -070030 * [LLLLLL]
buzbee091cc402014-03-31 10:14:40 -070031 * Physical register number for the low or solo register.
buzbeed111c6e2014-05-11 21:09:53 -070032 * 0..63
Bill Buzbee00e1ec62014-02-27 23:44:13 +000033 *
buzbee091cc402014-03-31 10:14:40 -070034 * [F]
35 * Describes type of the [LLLLL] register.
36 * 0: Core
37 * 1: Floating point
Bill Buzbee00e1ec62014-02-27 23:44:13 +000038 *
buzbee091cc402014-03-31 10:14:40 -070039 * [SSS]
40 * Shape of the register container.
41 * 000: Invalid
42 * 001: 32-bit solo register
43 * 010: 64-bit solo register
44 * 011: 64-bit pair consisting of two 32-bit solo registers
45 * 100: 128-bit solo register
46 * 101: 256-bit solo register
47 * 110: 512-bit solo register
48 * 111: 1024-bit solo register
Bill Buzbee00e1ec62014-02-27 23:44:13 +000049 *
buzbee091cc402014-03-31 10:14:40 -070050 * [HHHHH]
51 * Physical register number of the high register (valid only for register pair).
52 * 0..31
Bill Buzbee00e1ec62014-02-27 23:44:13 +000053 *
buzbee091cc402014-03-31 10:14:40 -070054 * [V]
55 * 0 -> Invalid
56 * 1 -> Valid
57 *
58 * Note that in all non-invalid cases, we can determine if the storage is floating point
buzbeed111c6e2014-05-11 21:09:53 -070059 * by testing bit 7. Note also that a register pair is effectively limited to a pair of
60 * physical register numbers in the 0..31 range.
buzbee091cc402014-03-31 10:14:40 -070061 *
62 * On some target architectures, the same underlying physical register container can be given
63 * different views. For example, Arm's 32-bit single-precision floating point registers
64 * s2 and s3 map to the low and high halves of double-precision d1. Similarly, X86's xmm3
65 * vector register can be viewed as 32-bit, 64-bit, 128-bit, etc. In these cases the use of
66 * one view will affect the other views. The RegStorage class does not concern itself
67 * with potential aliasing. That will be done using the associated RegisterInfo struct.
68 * Distinct RegStorage elements should be created for each view of a physical register
69 * container. The management of the aliased physical elements will be handled via RegisterInfo
70 * records.
Bill Buzbee00e1ec62014-02-27 23:44:13 +000071 */
72
73class RegStorage {
74 public:
75 enum RegStorageKind {
buzbee091cc402014-03-31 10:14:40 -070076 kValidMask = 0x8000,
77 kValid = 0x8000,
78 kInvalid = 0x0000,
buzbeed111c6e2014-05-11 21:09:53 -070079 kShapeMask = 0x0380,
80 k32BitSolo = 0x0080,
81 k64BitSolo = 0x0100,
82 k64BitPair = 0x0180,
83 k128BitSolo = 0x0200,
84 k256BitSolo = 0x0280,
85 k512BitSolo = 0x0300,
86 k1024BitSolo = 0x0380,
87 k64BitMask = 0x0300,
88 k64Bits = 0x0100,
89 kShapeTypeMask = 0x03c0,
90 kFloatingPoint = 0x0040,
buzbee091cc402014-03-31 10:14:40 -070091 kCoreRegister = 0x0000,
Bill Buzbee00e1ec62014-02-27 23:44:13 +000092 };
93
buzbeed111c6e2014-05-11 21:09:53 -070094 static const uint16_t kRegValMask = 0x03ff; // Num, type and shape.
95 static const uint16_t kRegTypeMask = 0x007f; // Num and type.
96 static const uint16_t kRegNumMask = 0x003f; // Num only.
97 static const uint16_t kHighRegNumMask = 0x001f; // 0..31 for high reg
buzbee091cc402014-03-31 10:14:40 -070098 static const uint16_t kMaxRegs = kRegValMask + 1;
buzbeed111c6e2014-05-11 21:09:53 -070099 // TODO: deprecate use of kInvalidRegVal and speed up GetReg(). Rely on valid bit instead.
100 static const uint16_t kInvalidRegVal = 0x03ff;
101 static const uint16_t kHighRegShift = 10;
102 static const uint16_t kHighRegMask = (kHighRegNumMask << kHighRegShift);
Bill Buzbee00e1ec62014-02-27 23:44:13 +0000103
buzbee091cc402014-03-31 10:14:40 -0700104 // Reg is [F][LLLLL], will override any existing shape and use rs_kind.
Bill Buzbee00e1ec62014-02-27 23:44:13 +0000105 RegStorage(RegStorageKind rs_kind, int reg) {
buzbee091cc402014-03-31 10:14:40 -0700106 DCHECK_NE(rs_kind, k64BitPair);
107 DCHECK_EQ(rs_kind & ~kShapeMask, 0);
108 reg_ = kValid | rs_kind | (reg & kRegTypeMask);
Bill Buzbee00e1ec62014-02-27 23:44:13 +0000109 }
110 RegStorage(RegStorageKind rs_kind, int low_reg, int high_reg) {
111 DCHECK_EQ(rs_kind, k64BitPair);
buzbee091cc402014-03-31 10:14:40 -0700112 DCHECK_EQ(low_reg & kFloatingPoint, high_reg & kFloatingPoint);
buzbeed111c6e2014-05-11 21:09:53 -0700113 DCHECK_LE(high_reg & kRegNumMask, kHighRegNumMask) << "High reg must be in 0..31";
114 reg_ = kValid | rs_kind | ((high_reg & kHighRegNumMask) << kHighRegShift) |
115 (low_reg & kRegTypeMask);
Bill Buzbee00e1ec62014-02-27 23:44:13 +0000116 }
buzbee091cc402014-03-31 10:14:40 -0700117 constexpr explicit RegStorage(uint16_t val) : reg_(val) {}
Bill Buzbee00e1ec62014-02-27 23:44:13 +0000118 RegStorage() : reg_(kInvalid) {}
Bill Buzbee00e1ec62014-02-27 23:44:13 +0000119
buzbee2700f7e2014-03-07 09:46:20 -0800120 bool operator==(const RegStorage rhs) const {
121 return (reg_ == rhs.GetRawBits());
122 }
123
124 bool operator!=(const RegStorage rhs) const {
125 return (reg_ != rhs.GetRawBits());
126 }
127
128 bool Valid() const {
buzbee091cc402014-03-31 10:14:40 -0700129 return ((reg_ & kValidMask) == kValid);
Bill Buzbee00e1ec62014-02-27 23:44:13 +0000130 }
131
132 bool Is32Bit() const {
buzbee091cc402014-03-31 10:14:40 -0700133 return ((reg_ & kShapeMask) == k32BitSolo);
Bill Buzbee00e1ec62014-02-27 23:44:13 +0000134 }
135
136 bool Is64Bit() const {
buzbee091cc402014-03-31 10:14:40 -0700137 return ((reg_ & k64BitMask) == k64Bits);
Bill Buzbee00e1ec62014-02-27 23:44:13 +0000138 }
139
Dmitry Petrochenko9ee801f2014-05-12 11:31:37 +0700140 bool Is64BitSolo() const {
141 return ((reg_ & kShapeMask) == k64BitSolo);
142 }
143
Bill Buzbee00e1ec62014-02-27 23:44:13 +0000144 bool IsPair() const {
buzbee091cc402014-03-31 10:14:40 -0700145 return ((reg_ & kShapeMask) == k64BitPair);
Bill Buzbee00e1ec62014-02-27 23:44:13 +0000146 }
147
buzbee091cc402014-03-31 10:14:40 -0700148 bool IsFloat() const {
149 DCHECK(Valid());
150 return ((reg_ & kFloatingPoint) == kFloatingPoint);
Bill Buzbee00e1ec62014-02-27 23:44:13 +0000151 }
152
buzbee091cc402014-03-31 10:14:40 -0700153 bool IsDouble() const {
154 DCHECK(Valid());
155 return (reg_ & (kFloatingPoint | k64BitMask)) == (kFloatingPoint | k64Bits);
156 }
157
158 bool IsSingle() const {
159 DCHECK(Valid());
160 return (reg_ & (kFloatingPoint | k64BitMask)) == kFloatingPoint;
161 }
162
163 static bool IsFloat(uint16_t reg) {
164 return ((reg & kFloatingPoint) == kFloatingPoint);
165 }
166
167 static bool IsDouble(uint16_t reg) {
168 return (reg & (kFloatingPoint | k64BitMask)) == (kFloatingPoint | k64Bits);
169 }
170
171 static bool IsSingle(uint16_t reg) {
172 return (reg & (kFloatingPoint | k64BitMask)) == kFloatingPoint;
Bill Buzbee00e1ec62014-02-27 23:44:13 +0000173 }
174
175 // Used to retrieve either the low register of a pair, or the only register.
176 int GetReg() const {
buzbee091cc402014-03-31 10:14:40 -0700177 DCHECK(!IsPair()) << "reg_ = 0x" << std::hex << reg_;
buzbee2700f7e2014-03-07 09:46:20 -0800178 return Valid() ? (reg_ & kRegValMask) : kInvalidRegVal;
Bill Buzbee00e1ec62014-02-27 23:44:13 +0000179 }
180
buzbee091cc402014-03-31 10:14:40 -0700181 // Sets shape, type and num of solo.
Bill Buzbee00e1ec62014-02-27 23:44:13 +0000182 void SetReg(int reg) {
buzbee2700f7e2014-03-07 09:46:20 -0800183 DCHECK(Valid());
buzbee091cc402014-03-31 10:14:40 -0700184 DCHECK(!IsPair());
Bill Buzbee00e1ec62014-02-27 23:44:13 +0000185 reg_ = (reg_ & ~kRegValMask) | reg;
buzbee2700f7e2014-03-07 09:46:20 -0800186 }
187
buzbee091cc402014-03-31 10:14:40 -0700188 // Set the reg number and type only, target remain 64-bit pair.
buzbee2700f7e2014-03-07 09:46:20 -0800189 void SetLowReg(int reg) {
190 DCHECK(IsPair());
buzbee091cc402014-03-31 10:14:40 -0700191 reg_ = (reg_ & ~kRegTypeMask) | (reg & kRegTypeMask);
buzbee2700f7e2014-03-07 09:46:20 -0800192 }
193
buzbee091cc402014-03-31 10:14:40 -0700194 // Retrieve the least significant register of a pair and return as 32-bit solo.
buzbee2700f7e2014-03-07 09:46:20 -0800195 int GetLowReg() const {
196 DCHECK(IsPair());
buzbee091cc402014-03-31 10:14:40 -0700197 return ((reg_ & kRegTypeMask) | k32BitSolo);
buzbee2700f7e2014-03-07 09:46:20 -0800198 }
199
200 // Create a stand-alone RegStorage from the low reg of a pair.
201 RegStorage GetLow() const {
202 DCHECK(IsPair());
buzbee091cc402014-03-31 10:14:40 -0700203 return RegStorage(k32BitSolo, reg_ & kRegTypeMask);
Bill Buzbee00e1ec62014-02-27 23:44:13 +0000204 }
205
206 // Retrieve the most significant register of a pair.
207 int GetHighReg() const {
208 DCHECK(IsPair());
buzbeed111c6e2014-05-11 21:09:53 -0700209 return k32BitSolo | ((reg_ & kHighRegMask) >> kHighRegShift) | (reg_ & kFloatingPoint);
Bill Buzbee00e1ec62014-02-27 23:44:13 +0000210 }
211
buzbee2700f7e2014-03-07 09:46:20 -0800212 // Create a stand-alone RegStorage from the high reg of a pair.
213 RegStorage GetHigh() const {
214 DCHECK(IsPair());
buzbee091cc402014-03-31 10:14:40 -0700215 return RegStorage(kValid | GetHighReg());
buzbee2700f7e2014-03-07 09:46:20 -0800216 }
217
Bill Buzbee00e1ec62014-02-27 23:44:13 +0000218 void SetHighReg(int reg) {
219 DCHECK(IsPair());
buzbeed111c6e2014-05-11 21:09:53 -0700220 reg_ = (reg_ & ~kHighRegMask) | ((reg & kHighRegNumMask) << kHighRegShift);
buzbee091cc402014-03-31 10:14:40 -0700221 }
222
223 // Return the register number of low or solo.
224 int GetRegNum() const {
225 return reg_ & kRegNumMask;
226 }
227
buzbee091cc402014-03-31 10:14:40 -0700228 // Is register number in 0..7?
229 bool Low8() const {
230 return GetRegNum() < 8;
231 }
232
233 // Is register number in 0..3?
234 bool Low4() const {
235 return GetRegNum() < 4;
Bill Buzbee00e1ec62014-02-27 23:44:13 +0000236 }
237
buzbee2700f7e2014-03-07 09:46:20 -0800238 // Combine 2 32-bit solo regs into a pair.
239 static RegStorage MakeRegPair(RegStorage low, RegStorage high) {
240 DCHECK(!low.IsPair());
241 DCHECK(low.Is32Bit());
242 DCHECK(!high.IsPair());
243 DCHECK(high.Is32Bit());
244 return RegStorage(k64BitPair, low.GetReg(), high.GetReg());
245 }
246
buzbee091cc402014-03-31 10:14:40 -0700247 static bool SameRegType(RegStorage reg1, RegStorage reg2) {
248 return (reg1.IsDouble() == reg2.IsDouble()) && (reg1.IsSingle() == reg2.IsSingle());
249 }
250
251 static bool SameRegType(int reg1, int reg2) {
252 return (IsDouble(reg1) == IsDouble(reg2)) && (IsSingle(reg1) == IsSingle(reg2));
253 }
254
buzbee2700f7e2014-03-07 09:46:20 -0800255 // Create a 32-bit solo.
256 static RegStorage Solo32(int reg_num) {
buzbee091cc402014-03-31 10:14:40 -0700257 return RegStorage(k32BitSolo, reg_num & kRegTypeMask);
258 }
259
260 // Create a floating point 32-bit solo.
261 static RegStorage FloatSolo32(int reg_num) {
262 return RegStorage(k32BitSolo, (reg_num & kRegNumMask) | kFloatingPoint);
buzbee2700f7e2014-03-07 09:46:20 -0800263 }
264
Mark Mendellfe945782014-05-22 09:52:36 -0400265 // Create a 128-bit solo.
266 static RegStorage Solo128(int reg_num) {
267 return RegStorage(k128BitSolo, reg_num & kRegTypeMask);
268 }
269
buzbee2700f7e2014-03-07 09:46:20 -0800270 // Create a 64-bit solo.
271 static RegStorage Solo64(int reg_num) {
buzbee091cc402014-03-31 10:14:40 -0700272 return RegStorage(k64BitSolo, reg_num & kRegTypeMask);
273 }
274
275 // Create a floating point 64-bit solo.
276 static RegStorage FloatSolo64(int reg_num) {
277 return RegStorage(k64BitSolo, (reg_num & kRegNumMask) | kFloatingPoint);
buzbee2700f7e2014-03-07 09:46:20 -0800278 }
279
280 static RegStorage InvalidReg() {
281 return RegStorage(kInvalid);
282 }
283
buzbee091cc402014-03-31 10:14:40 -0700284 static uint16_t RegNum(int raw_reg_bits) {
285 return raw_reg_bits & kRegNumMask;
286 }
287
Bill Buzbee00e1ec62014-02-27 23:44:13 +0000288 int GetRawBits() const {
289 return reg_;
290 }
291
buzbee091cc402014-03-31 10:14:40 -0700292 size_t StorageSize() {
293 switch (reg_ & kShapeMask) {
294 case kInvalid: return 0;
295 case k32BitSolo: return 4;
296 case k64BitSolo: return 8;
297 case k64BitPair: return 8; // Is this useful? Might want to disallow taking size of pair.
298 case k128BitSolo: return 16;
299 case k256BitSolo: return 32;
300 case k512BitSolo: return 64;
301 case k1024BitSolo: return 128;
Serban Constantinescued65c5e2014-05-22 15:10:18 +0100302 default: LOG(FATAL) << "Unexpected shape";
buzbee091cc402014-03-31 10:14:40 -0700303 }
304 return 0;
305 }
306
Bill Buzbee00e1ec62014-02-27 23:44:13 +0000307 private:
308 uint16_t reg_;
309};
310
311} // namespace art
312
313#endif // ART_COMPILER_DEX_REG_STORAGE_H_