blob: d5cd59d481e230b9ad0e84c5240a3be5d37f867e [file] [log] [blame]
Elliott Hughes2faa5f12012-01-30 14:42:07 -08001/*
2 * Copyright (C) 2011 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 */
Carl Shapiroa5d5cfd2011-06-21 12:46:59 -070016
Ian Rogers2c8f6532011-09-02 17:16:34 -070017#include "assembler_arm.h"
18
Andreas Gampe7cffc3b2015-10-19 21:31:53 -070019#include <algorithm>
20
Vladimir Marko80afd022015-05-19 18:08:00 +010021#include "base/bit_utils.h"
Elliott Hughes07ed66b2012-12-12 18:34:25 -080022#include "base/logging.h"
Ian Rogers166db042013-07-26 12:05:57 -070023#include "entrypoints/quick/quick_entrypoints.h"
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070024#include "offsets.h"
Carl Shapiroe2d373e2011-07-25 15:20:06 -070025#include "thread.h"
Carl Shapiroa5d5cfd2011-06-21 12:46:59 -070026
Carl Shapiro6b6b5f02011-06-21 15:05:09 -070027namespace art {
Ian Rogers2c8f6532011-09-02 17:16:34 -070028namespace arm {
Carl Shapiroa5d5cfd2011-06-21 12:46:59 -070029
Dave Allison65fcc2c2014-04-28 13:45:27 -070030const char* kRegisterNames[] = {
Elliott Hughes1f359b02011-07-17 14:27:17 -070031 "r0", "r1", "r2", "r3", "r4", "r5", "r6", "r7", "r8", "r9", "r10",
32 "fp", "ip", "sp", "lr", "pc"
33};
Dave Allison65fcc2c2014-04-28 13:45:27 -070034
35const char* kConditionNames[] = {
36 "EQ", "NE", "CS", "CC", "MI", "PL", "VS", "VC", "HI", "LS", "GE", "LT", "GT",
37 "LE", "AL",
38};
39
Elliott Hughes1f359b02011-07-17 14:27:17 -070040std::ostream& operator<<(std::ostream& os, const Register& rhs) {
41 if (rhs >= R0 && rhs <= PC) {
42 os << kRegisterNames[rhs];
43 } else {
Ian Rogersb033c752011-07-20 12:22:35 -070044 os << "Register[" << static_cast<int>(rhs) << "]";
Elliott Hughes1f359b02011-07-17 14:27:17 -070045 }
46 return os;
47}
48
49
50std::ostream& operator<<(std::ostream& os, const SRegister& rhs) {
51 if (rhs >= S0 && rhs < kNumberOfSRegisters) {
Ian Rogersb033c752011-07-20 12:22:35 -070052 os << "s" << static_cast<int>(rhs);
Elliott Hughes1f359b02011-07-17 14:27:17 -070053 } else {
Ian Rogersb033c752011-07-20 12:22:35 -070054 os << "SRegister[" << static_cast<int>(rhs) << "]";
Elliott Hughes1f359b02011-07-17 14:27:17 -070055 }
56 return os;
57}
58
59
60std::ostream& operator<<(std::ostream& os, const DRegister& rhs) {
61 if (rhs >= D0 && rhs < kNumberOfDRegisters) {
Ian Rogersb033c752011-07-20 12:22:35 -070062 os << "d" << static_cast<int>(rhs);
Elliott Hughes1f359b02011-07-17 14:27:17 -070063 } else {
Ian Rogersb033c752011-07-20 12:22:35 -070064 os << "DRegister[" << static_cast<int>(rhs) << "]";
Elliott Hughes1f359b02011-07-17 14:27:17 -070065 }
66 return os;
67}
68
Elliott Hughes1f359b02011-07-17 14:27:17 -070069std::ostream& operator<<(std::ostream& os, const Condition& rhs) {
70 if (rhs >= EQ && rhs <= AL) {
71 os << kConditionNames[rhs];
72 } else {
Ian Rogersb033c752011-07-20 12:22:35 -070073 os << "Condition[" << static_cast<int>(rhs) << "]";
Elliott Hughes1f359b02011-07-17 14:27:17 -070074 }
75 return os;
76}
77
Nicolas Geoffray96f89a22014-07-11 10:57:49 +010078ShifterOperand::ShifterOperand(uint32_t immed)
79 : type_(kImmediate), rm_(kNoRegister), rs_(kNoRegister),
80 is_rotate_(false), is_shift_(false), shift_(kNoShift), rotate_(0), immed_(immed) {
81 CHECK(immed < (1u << 12) || ArmAssembler::ModifiedImmediate(immed) != kInvalidModifiedImmediate);
82}
Carl Shapiroa2e18e12011-06-21 18:57:55 -070083
84
Dave Allison65fcc2c2014-04-28 13:45:27 -070085uint32_t ShifterOperand::encodingArm() const {
86 CHECK(is_valid());
87 switch (type_) {
88 case kImmediate:
89 if (is_rotate_) {
90 return (rotate_ << kRotateShift) | (immed_ << kImmed8Shift);
91 } else {
92 return immed_;
Ian Rogersb033c752011-07-20 12:22:35 -070093 }
Dave Allison65fcc2c2014-04-28 13:45:27 -070094 case kRegister:
95 if (is_shift_) {
Andreas Gampe849cc5e2014-11-18 13:46:46 -080096 uint32_t shift_type;
97 switch (shift_) {
98 case arm::Shift::ROR:
99 shift_type = static_cast<uint32_t>(shift_);
100 CHECK_NE(immed_, 0U);
101 break;
102 case arm::Shift::RRX:
103 shift_type = static_cast<uint32_t>(arm::Shift::ROR); // Same encoding as ROR.
104 CHECK_EQ(immed_, 0U);
105 break;
106 default:
107 shift_type = static_cast<uint32_t>(shift_);
108 }
Dave Allison65fcc2c2014-04-28 13:45:27 -0700109 // Shifted immediate or register.
110 if (rs_ == kNoRegister) {
111 // Immediate shift.
112 return immed_ << kShiftImmShift |
Andreas Gampe849cc5e2014-11-18 13:46:46 -0800113 shift_type << kShiftShift |
Dave Allison65fcc2c2014-04-28 13:45:27 -0700114 static_cast<uint32_t>(rm_);
115 } else {
116 // Register shift.
117 return static_cast<uint32_t>(rs_) << kShiftRegisterShift |
Andreas Gampe849cc5e2014-11-18 13:46:46 -0800118 shift_type << kShiftShift | (1 << 4) |
Dave Allison65fcc2c2014-04-28 13:45:27 -0700119 static_cast<uint32_t>(rm_);
120 }
121 } else {
122 // Simple register
123 return static_cast<uint32_t>(rm_);
Ian Rogersb033c752011-07-20 12:22:35 -0700124 }
Dave Allison65fcc2c2014-04-28 13:45:27 -0700125 default:
126 // Can't get here.
127 LOG(FATAL) << "Invalid shifter operand for ARM";
128 return 0;
Ian Rogersb033c752011-07-20 12:22:35 -0700129 }
130}
131
Dave Allison45fdb932014-06-25 12:37:10 -0700132uint32_t ShifterOperand::encodingThumb() const {
133 switch (type_) {
134 case kImmediate:
135 return immed_;
136 case kRegister:
137 if (is_shift_) {
138 // Shifted immediate or register.
139 if (rs_ == kNoRegister) {
140 // Immediate shift.
141 if (shift_ == RRX) {
Vladimir Marko73cf0fb2015-07-30 15:07:22 +0100142 DCHECK_EQ(immed_, 0u);
Dave Allison45fdb932014-06-25 12:37:10 -0700143 // RRX is encoded as an ROR with imm 0.
144 return ROR << 4 | static_cast<uint32_t>(rm_);
Dave Allison65fcc2c2014-04-28 13:45:27 -0700145 } else {
Vladimir Marko73cf0fb2015-07-30 15:07:22 +0100146 DCHECK((1 <= immed_ && immed_ <= 31) ||
147 (immed_ == 0u && shift_ == LSL) ||
148 (immed_ == 32u && (shift_ == ASR || shift_ == LSR)));
149 uint32_t imm3 = (immed_ >> 2) & 7 /* 0b111*/;
Andreas Gampec8ccf682014-09-29 20:07:43 -0700150 uint32_t imm2 = immed_ & 3U /* 0b11 */;
Dave Allison45fdb932014-06-25 12:37:10 -0700151
152 return imm3 << 12 | imm2 << 6 | shift_ << 4 |
153 static_cast<uint32_t>(rm_);
Dave Allison65fcc2c2014-04-28 13:45:27 -0700154 }
155 } else {
Dave Allison45fdb932014-06-25 12:37:10 -0700156 LOG(FATAL) << "No register-shifted register instruction available in thumb";
157 return 0;
Dave Allison65fcc2c2014-04-28 13:45:27 -0700158 }
Dave Allison45fdb932014-06-25 12:37:10 -0700159 } else {
160 // Simple register
161 return static_cast<uint32_t>(rm_);
162 }
Dave Allison45fdb932014-06-25 12:37:10 -0700163 default:
164 // Can't get here.
165 LOG(FATAL) << "Invalid shifter operand for thumb";
Andreas Gampe65b798e2015-04-06 09:35:22 -0700166 UNREACHABLE();
Ian Rogersb033c752011-07-20 12:22:35 -0700167 }
Dave Allison65fcc2c2014-04-28 13:45:27 -0700168}
169
Dave Allison65fcc2c2014-04-28 13:45:27 -0700170uint32_t Address::encodingArm() const {
Andreas Gampeab1eb0d2015-02-13 19:23:55 -0800171 CHECK(IsAbsoluteUint<12>(offset_));
Dave Allison65fcc2c2014-04-28 13:45:27 -0700172 uint32_t encoding;
Dave Allison45fdb932014-06-25 12:37:10 -0700173 if (is_immed_offset_) {
174 if (offset_ < 0) {
175 encoding = (am_ ^ (1 << kUShift)) | -offset_; // Flip U to adjust sign.
176 } else {
177 encoding = am_ | offset_;
178 }
Dave Allison65fcc2c2014-04-28 13:45:27 -0700179 } else {
Dave Allison45fdb932014-06-25 12:37:10 -0700180 uint32_t shift = shift_;
181 if (shift == RRX) {
Andreas Gampe9f612ff2014-11-24 13:42:22 -0800182 CHECK_EQ(offset_, 0);
Dave Allison45fdb932014-06-25 12:37:10 -0700183 shift = ROR;
184 }
185 encoding = am_ | static_cast<uint32_t>(rm_) | shift << 5 | offset_ << 7 | B25;
Dave Allison65fcc2c2014-04-28 13:45:27 -0700186 }
187 encoding |= static_cast<uint32_t>(rn_) << kRnShift;
188 return encoding;
189}
Ian Rogersb033c752011-07-20 12:22:35 -0700190
Dave Allison65fcc2c2014-04-28 13:45:27 -0700191
Dave Allison45fdb932014-06-25 12:37:10 -0700192uint32_t Address::encodingThumb(bool is_32bit) const {
Dave Allison65fcc2c2014-04-28 13:45:27 -0700193 uint32_t encoding = 0;
Dave Allison45fdb932014-06-25 12:37:10 -0700194 if (is_immed_offset_) {
195 encoding = static_cast<uint32_t>(rn_) << 16;
196 // Check for the T3/T4 encoding.
197 // PUW must Offset for T3
198 // Convert ARM PU0W to PUW
199 // The Mode is in ARM encoding format which is:
200 // |P|U|0|W|
201 // we need this in thumb2 mode:
202 // |P|U|W|
Dave Allison65fcc2c2014-04-28 13:45:27 -0700203
Dave Allison45fdb932014-06-25 12:37:10 -0700204 uint32_t am = am_;
205 int32_t offset = offset_;
206 if (offset < 0) {
207 am ^= 1 << kUShift;
208 offset = -offset;
209 }
210 if (offset_ < 0 || (offset >= 0 && offset < 256 &&
Dave Allison65fcc2c2014-04-28 13:45:27 -0700211 am_ != Mode::Offset)) {
Dave Allison45fdb932014-06-25 12:37:10 -0700212 // T4 encoding.
213 uint32_t PUW = am >> 21; // Move down to bottom of word.
214 PUW = (PUW >> 1) | (PUW & 1); // Bits 3, 2 and 0.
215 // If P is 0 then W must be 1 (Different from ARM).
Andreas Gampec8ccf682014-09-29 20:07:43 -0700216 if ((PUW & 4U /* 0b100 */) == 0) {
217 PUW |= 1U /* 0b1 */;
Dave Allison65fcc2c2014-04-28 13:45:27 -0700218 }
Dave Allison45fdb932014-06-25 12:37:10 -0700219 encoding |= B11 | PUW << 8 | offset;
220 } else {
221 // T3 encoding (also sets op1 to 0b01).
222 encoding |= B23 | offset_;
223 }
Dave Allison65fcc2c2014-04-28 13:45:27 -0700224 } else {
Dave Allison45fdb932014-06-25 12:37:10 -0700225 // Register offset, possibly shifted.
226 // Need to choose between encoding T1 (16 bit) or T2.
227 // Only Offset mode is supported. Shift must be LSL and the count
228 // is only 2 bits.
229 CHECK_EQ(shift_, LSL);
230 CHECK_LE(offset_, 4);
231 CHECK_EQ(am_, Offset);
232 bool is_t2 = is_32bit;
233 if (ArmAssembler::IsHighRegister(rn_) || ArmAssembler::IsHighRegister(rm_)) {
234 is_t2 = true;
235 } else if (offset_ != 0) {
236 is_t2 = true;
237 }
238 if (is_t2) {
239 encoding = static_cast<uint32_t>(rn_) << 16 | static_cast<uint32_t>(rm_) |
240 offset_ << 4;
241 } else {
242 encoding = static_cast<uint32_t>(rn_) << 3 | static_cast<uint32_t>(rm_) << 6;
243 }
Dave Allison65fcc2c2014-04-28 13:45:27 -0700244 }
245 return encoding;
246}
247
248// This is very like the ARM encoding except the offset is 10 bits.
249uint32_t Address::encodingThumbLdrdStrd() const {
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -0800250 DCHECK(IsImmediate());
Dave Allison65fcc2c2014-04-28 13:45:27 -0700251 uint32_t encoding;
252 uint32_t am = am_;
253 // If P is 0 then W must be 1 (Different from ARM).
254 uint32_t PU1W = am_ >> 21; // Move down to bottom of word.
Andreas Gampec8ccf682014-09-29 20:07:43 -0700255 if ((PU1W & 8U /* 0b1000 */) == 0) {
Dave Allison65fcc2c2014-04-28 13:45:27 -0700256 am |= 1 << 21; // Set W bit.
257 }
258 if (offset_ < 0) {
259 int32_t off = -offset_;
260 CHECK_LT(off, 1024);
Roland Levillain14d90572015-07-16 10:52:26 +0100261 CHECK_ALIGNED(off, 4);
Dave Allison65fcc2c2014-04-28 13:45:27 -0700262 encoding = (am ^ (1 << kUShift)) | off >> 2; // Flip U to adjust sign.
263 } else {
264 CHECK_LT(offset_, 1024);
Roland Levillain14d90572015-07-16 10:52:26 +0100265 CHECK_ALIGNED(offset_, 4);
Dave Allison65fcc2c2014-04-28 13:45:27 -0700266 encoding = am | offset_ >> 2;
267 }
268 encoding |= static_cast<uint32_t>(rn_) << 16;
269 return encoding;
270}
271
272// Encoding for ARM addressing mode 3.
273uint32_t Address::encoding3() const {
274 const uint32_t offset_mask = (1 << 12) - 1;
275 uint32_t encoding = encodingArm();
276 uint32_t offset = encoding & offset_mask;
277 CHECK_LT(offset, 256u);
278 return (encoding & ~offset_mask) | ((offset & 0xf0) << 4) | (offset & 0xf);
279}
280
281// Encoding for vfp load/store addressing.
282uint32_t Address::vencoding() const {
Andreas Gampeab1eb0d2015-02-13 19:23:55 -0800283 CHECK(IsAbsoluteUint<10>(offset_)); // In the range -1020 to +1020.
284 CHECK_ALIGNED(offset_, 2); // Multiple of 4.
285
Dave Allison65fcc2c2014-04-28 13:45:27 -0700286 const uint32_t offset_mask = (1 << 12) - 1;
287 uint32_t encoding = encodingArm();
288 uint32_t offset = encoding & offset_mask;
Dave Allison65fcc2c2014-04-28 13:45:27 -0700289 CHECK((am_ == Offset) || (am_ == NegOffset));
Andreas Gampe277ccbd2014-11-03 21:36:10 -0800290 uint32_t vencoding_value = (encoding & (0xf << kRnShift)) | (offset >> 2);
Dave Allison65fcc2c2014-04-28 13:45:27 -0700291 if (am_ == Offset) {
Andreas Gampe277ccbd2014-11-03 21:36:10 -0800292 vencoding_value |= 1 << 23;
Dave Allison65fcc2c2014-04-28 13:45:27 -0700293 }
Andreas Gampe277ccbd2014-11-03 21:36:10 -0800294 return vencoding_value;
Dave Allison65fcc2c2014-04-28 13:45:27 -0700295}
296
297
298bool Address::CanHoldLoadOffsetArm(LoadOperandType type, int offset) {
Ian Rogersb033c752011-07-20 12:22:35 -0700299 switch (type) {
300 case kLoadSignedByte:
301 case kLoadSignedHalfword:
302 case kLoadUnsignedHalfword:
303 case kLoadWordPair:
Andreas Gampeab1eb0d2015-02-13 19:23:55 -0800304 return IsAbsoluteUint<8>(offset); // Addressing mode 3.
Ian Rogersb033c752011-07-20 12:22:35 -0700305 case kLoadUnsignedByte:
306 case kLoadWord:
Andreas Gampeab1eb0d2015-02-13 19:23:55 -0800307 return IsAbsoluteUint<12>(offset); // Addressing mode 2.
Ian Rogersb033c752011-07-20 12:22:35 -0700308 case kLoadSWord:
309 case kLoadDWord:
Andreas Gampeab1eb0d2015-02-13 19:23:55 -0800310 return IsAbsoluteUint<10>(offset); // VFP addressing mode.
Ian Rogersb033c752011-07-20 12:22:35 -0700311 default:
312 LOG(FATAL) << "UNREACHABLE";
Ian Rogers2c4257b2014-10-24 14:20:06 -0700313 UNREACHABLE();
Ian Rogersb033c752011-07-20 12:22:35 -0700314 }
315}
316
317
Dave Allison65fcc2c2014-04-28 13:45:27 -0700318bool Address::CanHoldStoreOffsetArm(StoreOperandType type, int offset) {
Ian Rogersb033c752011-07-20 12:22:35 -0700319 switch (type) {
320 case kStoreHalfword:
321 case kStoreWordPair:
Andreas Gampeab1eb0d2015-02-13 19:23:55 -0800322 return IsAbsoluteUint<8>(offset); // Addressing mode 3.
Ian Rogersb033c752011-07-20 12:22:35 -0700323 case kStoreByte:
324 case kStoreWord:
Andreas Gampeab1eb0d2015-02-13 19:23:55 -0800325 return IsAbsoluteUint<12>(offset); // Addressing mode 2.
Ian Rogersb033c752011-07-20 12:22:35 -0700326 case kStoreSWord:
327 case kStoreDWord:
Andreas Gampeab1eb0d2015-02-13 19:23:55 -0800328 return IsAbsoluteUint<10>(offset); // VFP addressing mode.
Ian Rogersb033c752011-07-20 12:22:35 -0700329 default:
330 LOG(FATAL) << "UNREACHABLE";
Ian Rogers2c4257b2014-10-24 14:20:06 -0700331 UNREACHABLE();
Ian Rogersb033c752011-07-20 12:22:35 -0700332 }
333}
334
Dave Allison65fcc2c2014-04-28 13:45:27 -0700335bool Address::CanHoldLoadOffsetThumb(LoadOperandType type, int offset) {
Ian Rogersb033c752011-07-20 12:22:35 -0700336 switch (type) {
337 case kLoadSignedByte:
Ian Rogersb033c752011-07-20 12:22:35 -0700338 case kLoadSignedHalfword:
Ian Rogersb033c752011-07-20 12:22:35 -0700339 case kLoadUnsignedHalfword:
Dave Allison65fcc2c2014-04-28 13:45:27 -0700340 case kLoadUnsignedByte:
Ian Rogersb033c752011-07-20 12:22:35 -0700341 case kLoadWord:
Andreas Gampeab1eb0d2015-02-13 19:23:55 -0800342 return IsAbsoluteUint<12>(offset);
Dave Allison65fcc2c2014-04-28 13:45:27 -0700343 case kLoadSWord:
344 case kLoadDWord:
Vladimir Marko6fd0ffe2015-11-19 21:13:52 +0000345 return IsAbsoluteUint<10>(offset) && (offset & 3) == 0; // VFP addressing mode.
Ian Rogersb033c752011-07-20 12:22:35 -0700346 case kLoadWordPair:
Vladimir Marko6fd0ffe2015-11-19 21:13:52 +0000347 return IsAbsoluteUint<10>(offset) && (offset & 3) == 0;
Ian Rogers2c4257b2014-10-24 14:20:06 -0700348 default:
Ian Rogersb033c752011-07-20 12:22:35 -0700349 LOG(FATAL) << "UNREACHABLE";
Ian Rogers2c4257b2014-10-24 14:20:06 -0700350 UNREACHABLE();
Ian Rogersb033c752011-07-20 12:22:35 -0700351 }
352}
353
Carl Shapiroe2d373e2011-07-25 15:20:06 -0700354
Dave Allison65fcc2c2014-04-28 13:45:27 -0700355bool Address::CanHoldStoreOffsetThumb(StoreOperandType type, int offset) {
Ian Rogersb033c752011-07-20 12:22:35 -0700356 switch (type) {
Ian Rogersb033c752011-07-20 12:22:35 -0700357 case kStoreHalfword:
Dave Allison65fcc2c2014-04-28 13:45:27 -0700358 case kStoreByte:
Ian Rogersb033c752011-07-20 12:22:35 -0700359 case kStoreWord:
Andreas Gampeab1eb0d2015-02-13 19:23:55 -0800360 return IsAbsoluteUint<12>(offset);
Dave Allison65fcc2c2014-04-28 13:45:27 -0700361 case kStoreSWord:
362 case kStoreDWord:
Vladimir Marko6fd0ffe2015-11-19 21:13:52 +0000363 return IsAbsoluteUint<10>(offset) && (offset & 3) == 0; // VFP addressing mode.
Ian Rogersb033c752011-07-20 12:22:35 -0700364 case kStoreWordPair:
Vladimir Marko6fd0ffe2015-11-19 21:13:52 +0000365 return IsAbsoluteUint<10>(offset) && (offset & 3) == 0;
Ian Rogers2c4257b2014-10-24 14:20:06 -0700366 default:
Ian Rogersb033c752011-07-20 12:22:35 -0700367 LOG(FATAL) << "UNREACHABLE";
Ian Rogers2c4257b2014-10-24 14:20:06 -0700368 UNREACHABLE();
Ian Rogersb033c752011-07-20 12:22:35 -0700369 }
370}
371
Dave Allison65fcc2c2014-04-28 13:45:27 -0700372void ArmAssembler::Pad(uint32_t bytes) {
373 AssemblerBuffer::EnsureCapacity ensured(&buffer_);
374 for (uint32_t i = 0; i < bytes; ++i) {
Ian Rogers13735952014-10-08 12:43:28 -0700375 buffer_.Emit<uint8_t>(0);
Carl Shapiroe2d373e2011-07-25 15:20:06 -0700376 }
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700377}
378
Dave Allison65fcc2c2014-04-28 13:45:27 -0700379static int LeadingZeros(uint32_t val) {
380 uint32_t alt;
381 int32_t n;
382 int32_t count;
383
384 count = 16;
385 n = 32;
386 do {
387 alt = val >> count;
388 if (alt != 0) {
389 n = n - count;
390 val = alt;
391 }
392 count >>= 1;
393 } while (count);
394 return n - val;
395}
396
397
398uint32_t ArmAssembler::ModifiedImmediate(uint32_t value) {
399 int32_t z_leading;
400 int32_t z_trailing;
401 uint32_t b0 = value & 0xff;
402
403 /* Note: case of value==0 must use 0:000:0:0000000 encoding */
404 if (value <= 0xFF)
405 return b0; // 0:000:a:bcdefgh.
406 if (value == ((b0 << 16) | b0))
407 return (0x1 << 12) | b0; /* 0:001:a:bcdefgh */
408 if (value == ((b0 << 24) | (b0 << 16) | (b0 << 8) | b0))
409 return (0x3 << 12) | b0; /* 0:011:a:bcdefgh */
410 b0 = (value >> 8) & 0xff;
411 if (value == ((b0 << 24) | (b0 << 8)))
412 return (0x2 << 12) | b0; /* 0:010:a:bcdefgh */
413 /* Can we do it with rotation? */
414 z_leading = LeadingZeros(value);
415 z_trailing = 32 - LeadingZeros(~value & (value - 1));
416 /* A run of eight or fewer active bits? */
417 if ((z_leading + z_trailing) < 24)
418 return kInvalidModifiedImmediate; /* No - bail */
419 /* left-justify the constant, discarding msb (known to be 1) */
420 value <<= z_leading + 1;
421 /* Create bcdefgh */
422 value >>= 25;
423
424 /* Put it all together */
425 uint32_t v = 8 + z_leading;
426
Andreas Gampec8ccf682014-09-29 20:07:43 -0700427 uint32_t i = (v & 16U /* 0b10000 */) >> 4;
428 uint32_t imm3 = (v >> 1) & 7U /* 0b111 */;
Dave Allison65fcc2c2014-04-28 13:45:27 -0700429 uint32_t a = v & 1;
430 return value | i << 26 | imm3 << 12 | a << 7;
431}
432
Andreas Gampe7cffc3b2015-10-19 21:31:53 -0700433void ArmAssembler::FinalizeTrackedLabels() {
434 if (!tracked_labels_.empty()) {
435 // This array should be sorted, as assembly is generated in linearized order. It isn't
436 // technically required, but GetAdjustedPosition() used in AdjustLabelPosition() can take
437 // advantage of it. So ensure that it's actually the case.
438 DCHECK(std::is_sorted(
439 tracked_labels_.begin(),
440 tracked_labels_.end(),
441 [](const Label* lhs, const Label* rhs) { return lhs->Position() < rhs->Position(); }));
442
443 Label* last_label = nullptr; // Track duplicates, we must not adjust twice.
444 for (Label* label : tracked_labels_) {
445 DCHECK_NE(label, last_label);
446 AdjustLabelPosition(label);
447 last_label = label;
448 }
449 }
450}
451
Ian Rogers2c8f6532011-09-02 17:16:34 -0700452} // namespace arm
Carl Shapiro6b6b5f02011-06-21 15:05:09 -0700453} // namespace art