blob: 584a597701d3feb9b46a59eb9f6f6a899cfcc0d1 [file] [log] [blame]
Dave Allison65fcc2c2014-04-28 13:45:27 -07001/*
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#include "assembler_thumb2.h"
18
Vladimir Marko80afd022015-05-19 18:08:00 +010019#include "base/bit_utils.h"
Dave Allison65fcc2c2014-04-28 13:45:27 -070020#include "base/logging.h"
21#include "entrypoints/quick/quick_entrypoints.h"
22#include "offsets.h"
23#include "thread.h"
Dave Allison65fcc2c2014-04-28 13:45:27 -070024
25namespace art {
26namespace arm {
27
Vladimir Marko6b756b52015-07-14 11:58:38 +010028void Thumb2Assembler::Fixup::PrepareDependents(Thumb2Assembler* assembler) {
29 // For each Fixup, it's easy to find the Fixups that it depends on as they are either
30 // the following or the preceding Fixups until we find the target. However, for fixup
31 // adjustment we need the reverse lookup, i.e. what Fixups depend on a given Fixup.
32 // This function creates a compact representation of this relationship, where we have
33 // all the dependents in a single array and Fixups reference their ranges by start
34 // index and count. (Instead of having a per-fixup vector.)
35
36 // Count the number of dependents of each Fixup.
37 const FixupId end_id = assembler->fixups_.size();
38 Fixup* fixups = assembler->fixups_.data();
39 for (FixupId fixup_id = 0u; fixup_id != end_id; ++fixup_id) {
40 uint32_t target = fixups[fixup_id].target_;
41 if (target > fixups[fixup_id].location_) {
42 for (FixupId id = fixup_id + 1u; id != end_id && fixups[id].location_ < target; ++id) {
43 fixups[id].dependents_count_ += 1u;
44 }
45 } else {
46 for (FixupId id = fixup_id; id != 0u && fixups[id - 1u].location_ >= target; --id) {
47 fixups[id - 1u].dependents_count_ += 1u;
48 }
49 }
50 }
51 // Assign index ranges in fixup_dependents_ to individual fixups. Record the end of the
52 // range in dependents_start_, we shall later decrement it as we fill in fixup_dependents_.
53 uint32_t number_of_dependents = 0u;
54 for (FixupId fixup_id = 0u; fixup_id != end_id; ++fixup_id) {
55 number_of_dependents += fixups[fixup_id].dependents_count_;
56 fixups[fixup_id].dependents_start_ = number_of_dependents;
57 }
58 if (number_of_dependents == 0u) {
59 return;
60 }
61 // Create and fill in the fixup_dependents_.
62 assembler->fixup_dependents_.reset(new FixupId[number_of_dependents]);
63 FixupId* dependents = assembler->fixup_dependents_.get();
64 for (FixupId fixup_id = 0u; fixup_id != end_id; ++fixup_id) {
65 uint32_t target = fixups[fixup_id].target_;
66 if (target > fixups[fixup_id].location_) {
67 for (FixupId id = fixup_id + 1u; id != end_id && fixups[id].location_ < target; ++id) {
68 fixups[id].dependents_start_ -= 1u;
69 dependents[fixups[id].dependents_start_] = fixup_id;
70 }
71 } else {
72 for (FixupId id = fixup_id; id != 0u && fixups[id - 1u].location_ >= target; --id) {
73 fixups[id - 1u].dependents_start_ -= 1u;
74 dependents[fixups[id - 1u].dependents_start_] = fixup_id;
75 }
76 }
77 }
78}
79
Vladimir Markocf93a5c2015-06-16 11:33:24 +000080void Thumb2Assembler::BindLabel(Label* label, uint32_t bound_pc) {
81 CHECK(!label->IsBound());
82
83 while (label->IsLinked()) {
84 FixupId fixup_id = label->Position(); // The id for linked Fixup.
85 Fixup* fixup = GetFixup(fixup_id); // Get the Fixup at this id.
86 fixup->Resolve(bound_pc); // Fixup can be resolved now.
Vladimir Markocf93a5c2015-06-16 11:33:24 +000087 uint32_t fixup_location = fixup->GetLocation();
88 uint16_t next = buffer_.Load<uint16_t>(fixup_location); // Get next in chain.
89 buffer_.Store<int16_t>(fixup_location, 0);
90 label->position_ = next; // Move to next.
91 }
92 label->BindTo(bound_pc);
93}
94
Andreas Gampe7cffc3b2015-10-19 21:31:53 -070095uint32_t Thumb2Assembler::BindLiterals() {
Vladimir Markocf93a5c2015-06-16 11:33:24 +000096 // We don't add the padding here, that's done only after adjusting the Fixup sizes.
97 uint32_t code_size = buffer_.Size();
98 for (Literal& lit : literals_) {
99 Label* label = lit.GetLabel();
100 BindLabel(label, code_size);
101 code_size += lit.GetSize();
102 }
Andreas Gampe7cffc3b2015-10-19 21:31:53 -0700103 return code_size;
104}
105
106void Thumb2Assembler::BindJumpTables(uint32_t code_size) {
107 for (JumpTable& table : jump_tables_) {
108 Label* label = table.GetLabel();
109 BindLabel(label, code_size);
110 code_size += table.GetSize();
111 }
Vladimir Markocf93a5c2015-06-16 11:33:24 +0000112}
113
114void Thumb2Assembler::AdjustFixupIfNeeded(Fixup* fixup, uint32_t* current_code_size,
115 std::deque<FixupId>* fixups_to_recalculate) {
116 uint32_t adjustment = fixup->AdjustSizeIfNeeded(*current_code_size);
117 if (adjustment != 0u) {
118 *current_code_size += adjustment;
Vladimir Marko6b756b52015-07-14 11:58:38 +0100119 for (FixupId dependent_id : fixup->Dependents(*this)) {
Vladimir Markocf93a5c2015-06-16 11:33:24 +0000120 Fixup* dependent = GetFixup(dependent_id);
121 dependent->IncreaseAdjustment(adjustment);
122 if (buffer_.Load<int16_t>(dependent->GetLocation()) == 0) {
123 buffer_.Store<int16_t>(dependent->GetLocation(), 1);
124 fixups_to_recalculate->push_back(dependent_id);
125 }
126 }
127 }
128}
129
130uint32_t Thumb2Assembler::AdjustFixups() {
Vladimir Marko6b756b52015-07-14 11:58:38 +0100131 Fixup::PrepareDependents(this);
Vladimir Markocf93a5c2015-06-16 11:33:24 +0000132 uint32_t current_code_size = buffer_.Size();
133 std::deque<FixupId> fixups_to_recalculate;
134 if (kIsDebugBuild) {
135 // We will use the placeholders in the buffer_ to mark whether the fixup has
136 // been added to the fixups_to_recalculate. Make sure we start with zeros.
137 for (Fixup& fixup : fixups_) {
138 CHECK_EQ(buffer_.Load<int16_t>(fixup.GetLocation()), 0);
139 }
140 }
141 for (Fixup& fixup : fixups_) {
142 AdjustFixupIfNeeded(&fixup, &current_code_size, &fixups_to_recalculate);
143 }
144 while (!fixups_to_recalculate.empty()) {
Vladimir Marko663c9342015-07-22 11:28:14 +0100145 do {
146 // Pop the fixup.
147 FixupId fixup_id = fixups_to_recalculate.front();
148 fixups_to_recalculate.pop_front();
149 Fixup* fixup = GetFixup(fixup_id);
150 DCHECK_NE(buffer_.Load<int16_t>(fixup->GetLocation()), 0);
151 buffer_.Store<int16_t>(fixup->GetLocation(), 0);
152 // See if it needs adjustment.
153 AdjustFixupIfNeeded(fixup, &current_code_size, &fixups_to_recalculate);
154 } while (!fixups_to_recalculate.empty());
155
Andreas Gampe7cffc3b2015-10-19 21:31:53 -0700156 if ((current_code_size & 2) != 0 && (!literals_.empty() || !jump_tables_.empty())) {
Vladimir Marko663c9342015-07-22 11:28:14 +0100157 // If we need to add padding before literals, this may just push some out of range,
158 // so recalculate all load literals. This makes up for the fact that we don't mark
159 // load literal as a dependency of all previous Fixups even though it actually is.
160 for (Fixup& fixup : fixups_) {
161 if (fixup.IsLoadLiteral()) {
162 AdjustFixupIfNeeded(&fixup, &current_code_size, &fixups_to_recalculate);
163 }
164 }
165 }
Vladimir Markocf93a5c2015-06-16 11:33:24 +0000166 }
167 if (kIsDebugBuild) {
168 // Check that no fixup is marked as being in fixups_to_recalculate anymore.
169 for (Fixup& fixup : fixups_) {
170 CHECK_EQ(buffer_.Load<int16_t>(fixup.GetLocation()), 0);
171 }
172 }
173
174 // Adjust literal pool labels for padding.
Roland Levillain14d90572015-07-16 10:52:26 +0100175 DCHECK_ALIGNED(current_code_size, 2);
Vladimir Markocf93a5c2015-06-16 11:33:24 +0000176 uint32_t literals_adjustment = current_code_size + (current_code_size & 2) - buffer_.Size();
177 if (literals_adjustment != 0u) {
178 for (Literal& literal : literals_) {
179 Label* label = literal.GetLabel();
180 DCHECK(label->IsBound());
181 int old_position = label->Position();
182 label->Reinitialize();
183 label->BindTo(old_position + literals_adjustment);
184 }
Andreas Gampe7cffc3b2015-10-19 21:31:53 -0700185 for (JumpTable& table : jump_tables_) {
186 Label* label = table.GetLabel();
187 DCHECK(label->IsBound());
188 int old_position = label->Position();
189 label->Reinitialize();
190 label->BindTo(old_position + literals_adjustment);
191 }
Vladimir Markocf93a5c2015-06-16 11:33:24 +0000192 }
193
194 return current_code_size;
195}
196
197void Thumb2Assembler::EmitFixups(uint32_t adjusted_code_size) {
198 // Move non-fixup code to its final place and emit fixups.
199 // Process fixups in reverse order so that we don't repeatedly move the same data.
200 size_t src_end = buffer_.Size();
201 size_t dest_end = adjusted_code_size;
202 buffer_.Resize(dest_end);
203 DCHECK_GE(dest_end, src_end);
204 for (auto i = fixups_.rbegin(), end = fixups_.rend(); i != end; ++i) {
205 Fixup* fixup = &*i;
206 if (fixup->GetOriginalSize() == fixup->GetSize()) {
207 // The size of this Fixup didn't change. To avoid moving the data
208 // in small chunks, emit the code to its original position.
209 fixup->Emit(&buffer_, adjusted_code_size);
210 fixup->Finalize(dest_end - src_end);
211 } else {
212 // Move the data between the end of the fixup and src_end to its final location.
213 size_t old_fixup_location = fixup->GetLocation();
214 size_t src_begin = old_fixup_location + fixup->GetOriginalSizeInBytes();
215 size_t data_size = src_end - src_begin;
216 size_t dest_begin = dest_end - data_size;
217 buffer_.Move(dest_begin, src_begin, data_size);
218 src_end = old_fixup_location;
219 dest_end = dest_begin - fixup->GetSizeInBytes();
220 // Finalize the Fixup and emit the data to the new location.
221 fixup->Finalize(dest_end - src_end);
222 fixup->Emit(&buffer_, adjusted_code_size);
223 }
224 }
225 CHECK_EQ(src_end, dest_end);
226}
227
228void Thumb2Assembler::EmitLiterals() {
229 if (!literals_.empty()) {
230 // Load literal instructions (LDR, LDRD, VLDR) require 4-byte alignment.
231 // We don't support byte and half-word literals.
232 uint32_t code_size = buffer_.Size();
Roland Levillain14d90572015-07-16 10:52:26 +0100233 DCHECK_ALIGNED(code_size, 2);
Vladimir Markocf93a5c2015-06-16 11:33:24 +0000234 if ((code_size & 2u) != 0u) {
235 Emit16(0);
236 }
237 for (Literal& literal : literals_) {
238 AssemblerBuffer::EnsureCapacity ensured(&buffer_);
239 DCHECK_EQ(static_cast<size_t>(literal.GetLabel()->Position()), buffer_.Size());
240 DCHECK(literal.GetSize() == 4u || literal.GetSize() == 8u);
241 for (size_t i = 0, size = literal.GetSize(); i != size; ++i) {
242 buffer_.Emit<uint8_t>(literal.GetData()[i]);
243 }
244 }
245 }
246}
247
Andreas Gampe7cffc3b2015-10-19 21:31:53 -0700248void Thumb2Assembler::EmitJumpTables() {
249 if (!jump_tables_.empty()) {
250 // Jump tables require 4 byte alignment. (We don't support byte and half-word jump tables.)
251 uint32_t code_size = buffer_.Size();
252 DCHECK_ALIGNED(code_size, 2);
253 if ((code_size & 2u) != 0u) {
254 Emit16(0);
255 }
256 for (JumpTable& table : jump_tables_) {
257 // Bulk ensure capacity, as this may be large.
258 size_t orig_size = buffer_.Size();
259 buffer_.ExtendCapacity(orig_size + table.GetSize());
260#ifndef NDEBUG
261 buffer_.has_ensured_capacity_ = true;
262#endif
263
264 DCHECK_EQ(static_cast<size_t>(table.GetLabel()->Position()), buffer_.Size());
265 int32_t anchor_position = table.GetAnchorLabel()->Position() + 4;
266
267 for (Label* target : table.GetData()) {
268 // Ensure that the label was tracked, so that it will have the right position.
269 DCHECK(std::find(tracked_labels_.begin(), tracked_labels_.end(), target) !=
270 tracked_labels_.end());
271
272 int32_t offset = target->Position() - anchor_position;
273 buffer_.Emit<int32_t>(offset);
274 }
275
276#ifndef NDEBUG
277 buffer_.has_ensured_capacity_ = false;
278#endif
279 size_t new_size = buffer_.Size();
280 DCHECK_LE(new_size - orig_size, table.GetSize());
281 }
282 }
283}
284
Vladimir Marko10ef6942015-10-22 15:25:54 +0100285void Thumb2Assembler::PatchCFI() {
286 if (cfi().NumberOfDelayedAdvancePCs() == 0u) {
287 return;
288 }
289
290 typedef DebugFrameOpCodeWriterForAssembler::DelayedAdvancePC DelayedAdvancePC;
291 const auto data = cfi().ReleaseStreamAndPrepareForDelayedAdvancePC();
292 const std::vector<uint8_t>& old_stream = data.first;
293 const std::vector<DelayedAdvancePC>& advances = data.second;
294
295 // Refill our data buffer with patched opcodes.
296 cfi().ReserveCFIStream(old_stream.size() + advances.size() + 16);
297 size_t stream_pos = 0;
298 for (const DelayedAdvancePC& advance : advances) {
299 DCHECK_GE(advance.stream_pos, stream_pos);
300 // Copy old data up to the point where advance was issued.
301 cfi().AppendRawData(old_stream, stream_pos, advance.stream_pos);
302 stream_pos = advance.stream_pos;
303 // Insert the advance command with its final offset.
304 size_t final_pc = GetAdjustedPosition(advance.pc);
305 cfi().AdvancePC(final_pc);
306 }
307 // Copy the final segment if any.
308 cfi().AppendRawData(old_stream, stream_pos, old_stream.size());
309}
310
Vladimir Markocf93a5c2015-06-16 11:33:24 +0000311inline int16_t Thumb2Assembler::BEncoding16(int32_t offset, Condition cond) {
Roland Levillain14d90572015-07-16 10:52:26 +0100312 DCHECK_ALIGNED(offset, 2);
Vladimir Markocf93a5c2015-06-16 11:33:24 +0000313 int16_t encoding = B15 | B14;
314 if (cond != AL) {
315 DCHECK(IsInt<9>(offset));
316 encoding |= B12 | (static_cast<int32_t>(cond) << 8) | ((offset >> 1) & 0xff);
317 } else {
318 DCHECK(IsInt<12>(offset));
319 encoding |= B13 | ((offset >> 1) & 0x7ff);
320 }
321 return encoding;
322}
323
324inline int32_t Thumb2Assembler::BEncoding32(int32_t offset, Condition cond) {
Roland Levillain14d90572015-07-16 10:52:26 +0100325 DCHECK_ALIGNED(offset, 2);
Vladimir Markocf93a5c2015-06-16 11:33:24 +0000326 int32_t s = (offset >> 31) & 1; // Sign bit.
327 int32_t encoding = B31 | B30 | B29 | B28 | B15 |
328 (s << 26) | // Sign bit goes to bit 26.
329 ((offset >> 1) & 0x7ff); // imm11 goes to bits 0-10.
330 if (cond != AL) {
331 DCHECK(IsInt<21>(offset));
332 // Encode cond, move imm6 from bits 12-17 to bits 16-21 and move J1 and J2.
333 encoding |= (static_cast<int32_t>(cond) << 22) | ((offset & 0x3f000) << (16 - 12)) |
334 ((offset & (1 << 19)) >> (19 - 13)) | // Extract J1 from bit 19 to bit 13.
335 ((offset & (1 << 18)) >> (18 - 11)); // Extract J2 from bit 18 to bit 11.
336 } else {
337 DCHECK(IsInt<25>(offset));
338 int32_t j1 = ((offset >> 23) ^ s ^ 1) & 1; // Calculate J1 from I1 extracted from bit 23.
339 int32_t j2 = ((offset >> 22)^ s ^ 1) & 1; // Calculate J2 from I2 extracted from bit 22.
340 // Move imm10 from bits 12-21 to bits 16-25 and add J1 and J2.
341 encoding |= B12 | ((offset & 0x3ff000) << (16 - 12)) |
342 (j1 << 13) | (j2 << 11);
343 }
344 return encoding;
345}
346
347inline int16_t Thumb2Assembler::CbxzEncoding16(Register rn, int32_t offset, Condition cond) {
348 DCHECK(!IsHighRegister(rn));
Roland Levillain14d90572015-07-16 10:52:26 +0100349 DCHECK_ALIGNED(offset, 2);
Vladimir Markocf93a5c2015-06-16 11:33:24 +0000350 DCHECK(IsUint<7>(offset));
351 DCHECK(cond == EQ || cond == NE);
352 return B15 | B13 | B12 | B8 | (cond == NE ? B11 : 0) | static_cast<int32_t>(rn) |
353 ((offset & 0x3e) << (3 - 1)) | // Move imm5 from bits 1-5 to bits 3-7.
354 ((offset & 0x40) << (9 - 6)); // Move i from bit 6 to bit 11
355}
356
357inline int16_t Thumb2Assembler::CmpRnImm8Encoding16(Register rn, int32_t value) {
358 DCHECK(!IsHighRegister(rn));
359 DCHECK(IsUint<8>(value));
360 return B13 | B11 | (rn << 8) | value;
361}
362
363inline int16_t Thumb2Assembler::AddRdnRmEncoding16(Register rdn, Register rm) {
364 // The high bit of rn is moved across 4-bit rm.
365 return B14 | B10 | (static_cast<int32_t>(rm) << 3) |
366 (static_cast<int32_t>(rdn) & 7) | ((static_cast<int32_t>(rdn) & 8) << 4);
367}
368
369inline int32_t Thumb2Assembler::MovwEncoding32(Register rd, int32_t value) {
370 DCHECK(IsUint<16>(value));
371 return B31 | B30 | B29 | B28 | B25 | B22 |
372 (static_cast<int32_t>(rd) << 8) |
373 ((value & 0xf000) << (16 - 12)) | // Move imm4 from bits 12-15 to bits 16-19.
374 ((value & 0x0800) << (26 - 11)) | // Move i from bit 11 to bit 26.
375 ((value & 0x0700) << (12 - 8)) | // Move imm3 from bits 8-10 to bits 12-14.
376 (value & 0xff); // Keep imm8 in bits 0-7.
377}
378
379inline int32_t Thumb2Assembler::MovtEncoding32(Register rd, int32_t value) {
380 DCHECK_EQ(value & 0xffff, 0);
381 int32_t movw_encoding = MovwEncoding32(rd, (value >> 16) & 0xffff);
382 return movw_encoding | B25 | B23;
383}
384
385inline int32_t Thumb2Assembler::MovModImmEncoding32(Register rd, int32_t value) {
386 uint32_t mod_imm = ModifiedImmediate(value);
387 DCHECK_NE(mod_imm, kInvalidModifiedImmediate);
388 return B31 | B30 | B29 | B28 | B22 | B19 | B18 | B17 | B16 |
389 (static_cast<int32_t>(rd) << 8) | static_cast<int32_t>(mod_imm);
390}
391
392inline int16_t Thumb2Assembler::LdrLitEncoding16(Register rt, int32_t offset) {
393 DCHECK(!IsHighRegister(rt));
Roland Levillain14d90572015-07-16 10:52:26 +0100394 DCHECK_ALIGNED(offset, 4);
Vladimir Markocf93a5c2015-06-16 11:33:24 +0000395 DCHECK(IsUint<10>(offset));
396 return B14 | B11 | (static_cast<int32_t>(rt) << 8) | (offset >> 2);
397}
398
399inline int32_t Thumb2Assembler::LdrLitEncoding32(Register rt, int32_t offset) {
400 // NOTE: We don't support negative offset, i.e. U=0 (B23).
401 return LdrRtRnImm12Encoding(rt, PC, offset);
402}
403
404inline int32_t Thumb2Assembler::LdrdEncoding32(Register rt, Register rt2, Register rn, int32_t offset) {
Roland Levillain14d90572015-07-16 10:52:26 +0100405 DCHECK_ALIGNED(offset, 4);
Vladimir Markocf93a5c2015-06-16 11:33:24 +0000406 CHECK(IsUint<10>(offset));
407 return B31 | B30 | B29 | B27 |
408 B24 /* P = 1 */ | B23 /* U = 1 */ | B22 | 0 /* W = 0 */ | B20 |
409 (static_cast<int32_t>(rn) << 16) | (static_cast<int32_t>(rt) << 12) |
410 (static_cast<int32_t>(rt2) << 8) | (offset >> 2);
411}
412
413inline int32_t Thumb2Assembler::VldrsEncoding32(SRegister sd, Register rn, int32_t offset) {
Roland Levillain14d90572015-07-16 10:52:26 +0100414 DCHECK_ALIGNED(offset, 4);
Vladimir Markocf93a5c2015-06-16 11:33:24 +0000415 CHECK(IsUint<10>(offset));
416 return B31 | B30 | B29 | B27 | B26 | B24 |
417 B23 /* U = 1 */ | B20 | B11 | B9 |
418 (static_cast<int32_t>(rn) << 16) |
419 ((static_cast<int32_t>(sd) & 0x01) << (22 - 0)) | // Move D from bit 0 to bit 22.
420 ((static_cast<int32_t>(sd) & 0x1e) << (12 - 1)) | // Move Vd from bits 1-4 to bits 12-15.
421 (offset >> 2);
422}
423
424inline int32_t Thumb2Assembler::VldrdEncoding32(DRegister dd, Register rn, int32_t offset) {
Roland Levillain14d90572015-07-16 10:52:26 +0100425 DCHECK_ALIGNED(offset, 4);
Vladimir Markocf93a5c2015-06-16 11:33:24 +0000426 CHECK(IsUint<10>(offset));
427 return B31 | B30 | B29 | B27 | B26 | B24 |
428 B23 /* U = 1 */ | B20 | B11 | B9 | B8 |
429 (rn << 16) |
430 ((static_cast<int32_t>(dd) & 0x10) << (22 - 4)) | // Move D from bit 4 to bit 22.
431 ((static_cast<int32_t>(dd) & 0x0f) << (12 - 0)) | // Move Vd from bits 0-3 to bits 12-15.
432 (offset >> 2);
433}
434
435inline int16_t Thumb2Assembler::LdrRtRnImm5Encoding16(Register rt, Register rn, int32_t offset) {
436 DCHECK(!IsHighRegister(rt));
437 DCHECK(!IsHighRegister(rn));
Roland Levillain14d90572015-07-16 10:52:26 +0100438 DCHECK_ALIGNED(offset, 4);
Vladimir Markocf93a5c2015-06-16 11:33:24 +0000439 DCHECK(IsUint<7>(offset));
440 return B14 | B13 | B11 |
441 (static_cast<int32_t>(rn) << 3) | static_cast<int32_t>(rt) |
442 (offset << (6 - 2)); // Move imm5 from bits 2-6 to bits 6-10.
443}
444
445int32_t Thumb2Assembler::Fixup::LoadWideOrFpEncoding(Register rbase, int32_t offset) const {
446 switch (type_) {
447 case kLoadLiteralWide:
448 return LdrdEncoding32(rn_, rt2_, rbase, offset);
449 case kLoadFPLiteralSingle:
450 return VldrsEncoding32(sd_, rbase, offset);
451 case kLoadFPLiteralDouble:
452 return VldrdEncoding32(dd_, rbase, offset);
453 default:
454 LOG(FATAL) << "Unexpected type: " << static_cast<int>(type_);
455 UNREACHABLE();
456 }
457}
458
459inline int32_t Thumb2Assembler::LdrRtRnImm12Encoding(Register rt, Register rn, int32_t offset) {
460 DCHECK(IsUint<12>(offset));
461 return B31 | B30 | B29 | B28 | B27 | B23 | B22 | B20 | (rn << 16) | (rt << 12) | offset;
462}
463
Andreas Gampe7cffc3b2015-10-19 21:31:53 -0700464inline int16_t Thumb2Assembler::AdrEncoding16(Register rd, int32_t offset) {
465 DCHECK(IsUint<10>(offset));
466 DCHECK(IsAligned<4>(offset));
467 DCHECK(!IsHighRegister(rd));
468 return B15 | B13 | (rd << 8) | (offset >> 2);
469}
470
471inline int32_t Thumb2Assembler::AdrEncoding32(Register rd, int32_t offset) {
472 DCHECK(IsUint<12>(offset));
473 // Bit 26: offset[11]
474 // Bits 14-12: offset[10-8]
475 // Bits 7-0: offset[7-0]
476 int32_t immediate_mask =
477 ((offset & (1 << 11)) << (26 - 11)) |
478 ((offset & (7 << 8)) << (12 - 8)) |
479 (offset & 0xFF);
480 return B31 | B30 | B29 | B28 | B25 | B19 | B18 | B17 | B16 | (rd << 8) | immediate_mask;
481}
482
Vladimir Markocf93a5c2015-06-16 11:33:24 +0000483void Thumb2Assembler::FinalizeCode() {
484 ArmAssembler::FinalizeCode();
Andreas Gampe7cffc3b2015-10-19 21:31:53 -0700485 uint32_t size_after_literals = BindLiterals();
486 BindJumpTables(size_after_literals);
Vladimir Markocf93a5c2015-06-16 11:33:24 +0000487 uint32_t adjusted_code_size = AdjustFixups();
488 EmitFixups(adjusted_code_size);
489 EmitLiterals();
Andreas Gampe7cffc3b2015-10-19 21:31:53 -0700490 FinalizeTrackedLabels();
491 EmitJumpTables();
Vladimir Marko10ef6942015-10-22 15:25:54 +0100492 PatchCFI();
Vladimir Markocf93a5c2015-06-16 11:33:24 +0000493}
494
Nicolas Geoffray5bd05a52015-10-13 09:48:30 +0100495bool Thumb2Assembler::ShifterOperandCanAlwaysHold(uint32_t immediate) {
496 return ArmAssembler::ModifiedImmediate(immediate) != kInvalidModifiedImmediate;
497}
498
Nicolas Geoffray3d1e7882015-02-03 13:59:52 +0000499bool Thumb2Assembler::ShifterOperandCanHold(Register rd ATTRIBUTE_UNUSED,
500 Register rn ATTRIBUTE_UNUSED,
Nicolas Geoffray3bcc8ea2014-11-28 15:00:02 +0000501 Opcode opcode,
502 uint32_t immediate,
503 ShifterOperand* shifter_op) {
504 shifter_op->type_ = ShifterOperand::kImmediate;
505 shifter_op->immed_ = immediate;
506 shifter_op->is_shift_ = false;
507 shifter_op->is_rotate_ = false;
508 switch (opcode) {
509 case ADD:
510 case SUB:
Nicolas Geoffray3bcc8ea2014-11-28 15:00:02 +0000511 if (immediate < (1 << 12)) { // Less than (or equal to) 12 bits can always be done.
512 return true;
513 }
514 return ArmAssembler::ModifiedImmediate(immediate) != kInvalidModifiedImmediate;
515
516 case MOV:
517 // TODO: Support less than or equal to 12bits.
518 return ArmAssembler::ModifiedImmediate(immediate) != kInvalidModifiedImmediate;
Vladimir Markod2b4ca22015-09-14 15:13:26 +0100519
Nicolas Geoffray3bcc8ea2014-11-28 15:00:02 +0000520 case MVN:
521 default:
522 return ArmAssembler::ModifiedImmediate(immediate) != kInvalidModifiedImmediate;
523 }
524}
525
Dave Allison65fcc2c2014-04-28 13:45:27 -0700526void Thumb2Assembler::and_(Register rd, Register rn, const ShifterOperand& so,
Vladimir Marko73cf0fb2015-07-30 15:07:22 +0100527 Condition cond, SetCc set_cc) {
528 EmitDataProcessing(cond, AND, set_cc, rn, rd, so);
Dave Allison65fcc2c2014-04-28 13:45:27 -0700529}
530
531
532void Thumb2Assembler::eor(Register rd, Register rn, const ShifterOperand& so,
Vladimir Marko73cf0fb2015-07-30 15:07:22 +0100533 Condition cond, SetCc set_cc) {
534 EmitDataProcessing(cond, EOR, set_cc, rn, rd, so);
Dave Allison65fcc2c2014-04-28 13:45:27 -0700535}
536
537
538void Thumb2Assembler::sub(Register rd, Register rn, const ShifterOperand& so,
Vladimir Marko73cf0fb2015-07-30 15:07:22 +0100539 Condition cond, SetCc set_cc) {
540 EmitDataProcessing(cond, SUB, set_cc, rn, rd, so);
Dave Allison65fcc2c2014-04-28 13:45:27 -0700541}
542
543
544void Thumb2Assembler::rsb(Register rd, Register rn, const ShifterOperand& so,
Vladimir Marko73cf0fb2015-07-30 15:07:22 +0100545 Condition cond, SetCc set_cc) {
546 EmitDataProcessing(cond, RSB, set_cc, rn, rd, so);
Dave Allison65fcc2c2014-04-28 13:45:27 -0700547}
548
549
550void Thumb2Assembler::add(Register rd, Register rn, const ShifterOperand& so,
Vladimir Marko73cf0fb2015-07-30 15:07:22 +0100551 Condition cond, SetCc set_cc) {
552 EmitDataProcessing(cond, ADD, set_cc, rn, rd, so);
Dave Allison65fcc2c2014-04-28 13:45:27 -0700553}
554
555
556void Thumb2Assembler::adc(Register rd, Register rn, const ShifterOperand& so,
Vladimir Marko73cf0fb2015-07-30 15:07:22 +0100557 Condition cond, SetCc set_cc) {
558 EmitDataProcessing(cond, ADC, set_cc, rn, rd, so);
Dave Allison65fcc2c2014-04-28 13:45:27 -0700559}
560
561
562void Thumb2Assembler::sbc(Register rd, Register rn, const ShifterOperand& so,
Vladimir Marko73cf0fb2015-07-30 15:07:22 +0100563 Condition cond, SetCc set_cc) {
564 EmitDataProcessing(cond, SBC, set_cc, rn, rd, so);
Dave Allison65fcc2c2014-04-28 13:45:27 -0700565}
566
567
568void Thumb2Assembler::rsc(Register rd, Register rn, const ShifterOperand& so,
Vladimir Marko73cf0fb2015-07-30 15:07:22 +0100569 Condition cond, SetCc set_cc) {
570 EmitDataProcessing(cond, RSC, set_cc, rn, rd, so);
Dave Allison65fcc2c2014-04-28 13:45:27 -0700571}
572
573
574void Thumb2Assembler::tst(Register rn, const ShifterOperand& so, Condition cond) {
575 CHECK_NE(rn, PC); // Reserve tst pc instruction for exception handler marker.
Vladimir Marko73cf0fb2015-07-30 15:07:22 +0100576 EmitDataProcessing(cond, TST, kCcSet, rn, R0, so);
Dave Allison65fcc2c2014-04-28 13:45:27 -0700577}
578
579
580void Thumb2Assembler::teq(Register rn, const ShifterOperand& so, Condition cond) {
581 CHECK_NE(rn, PC); // Reserve teq pc instruction for exception handler marker.
Vladimir Marko73cf0fb2015-07-30 15:07:22 +0100582 EmitDataProcessing(cond, TEQ, kCcSet, rn, R0, so);
Dave Allison65fcc2c2014-04-28 13:45:27 -0700583}
584
585
586void Thumb2Assembler::cmp(Register rn, const ShifterOperand& so, Condition cond) {
Vladimir Marko73cf0fb2015-07-30 15:07:22 +0100587 EmitDataProcessing(cond, CMP, kCcSet, rn, R0, so);
Dave Allison65fcc2c2014-04-28 13:45:27 -0700588}
589
590
591void Thumb2Assembler::cmn(Register rn, const ShifterOperand& so, Condition cond) {
Vladimir Marko73cf0fb2015-07-30 15:07:22 +0100592 EmitDataProcessing(cond, CMN, kCcSet, rn, R0, so);
Dave Allison65fcc2c2014-04-28 13:45:27 -0700593}
594
595
Vladimir Marko73cf0fb2015-07-30 15:07:22 +0100596void Thumb2Assembler::orr(Register rd, Register rn, const ShifterOperand& so,
597 Condition cond, SetCc set_cc) {
598 EmitDataProcessing(cond, ORR, set_cc, rn, rd, so);
Dave Allison65fcc2c2014-04-28 13:45:27 -0700599}
600
601
Vladimir Markod2b4ca22015-09-14 15:13:26 +0100602void Thumb2Assembler::orn(Register rd, Register rn, const ShifterOperand& so,
603 Condition cond, SetCc set_cc) {
604 EmitDataProcessing(cond, ORN, set_cc, rn, rd, so);
605}
606
607
Vladimir Marko73cf0fb2015-07-30 15:07:22 +0100608void Thumb2Assembler::mov(Register rd, const ShifterOperand& so,
609 Condition cond, SetCc set_cc) {
610 EmitDataProcessing(cond, MOV, set_cc, R0, rd, so);
Dave Allison65fcc2c2014-04-28 13:45:27 -0700611}
612
613
614void Thumb2Assembler::bic(Register rd, Register rn, const ShifterOperand& so,
Vladimir Marko73cf0fb2015-07-30 15:07:22 +0100615 Condition cond, SetCc set_cc) {
616 EmitDataProcessing(cond, BIC, set_cc, rn, rd, so);
Dave Allison65fcc2c2014-04-28 13:45:27 -0700617}
618
619
Vladimir Marko73cf0fb2015-07-30 15:07:22 +0100620void Thumb2Assembler::mvn(Register rd, const ShifterOperand& so,
621 Condition cond, SetCc set_cc) {
622 EmitDataProcessing(cond, MVN, set_cc, R0, rd, so);
Dave Allison65fcc2c2014-04-28 13:45:27 -0700623}
624
625
626void Thumb2Assembler::mul(Register rd, Register rn, Register rm, Condition cond) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700627 CheckCondition(cond);
628
Dave Allison65fcc2c2014-04-28 13:45:27 -0700629 if (rd == rm && !IsHighRegister(rd) && !IsHighRegister(rn) && !force_32bit_) {
630 // 16 bit.
631 int16_t encoding = B14 | B9 | B8 | B6 |
632 rn << 3 | rd;
633 Emit16(encoding);
634 } else {
635 // 32 bit.
Andreas Gampec8ccf682014-09-29 20:07:43 -0700636 uint32_t op1 = 0U /* 0b000 */;
637 uint32_t op2 = 0U /* 0b00 */;
Dave Allison65fcc2c2014-04-28 13:45:27 -0700638 int32_t encoding = B31 | B30 | B29 | B28 | B27 | B25 | B24 |
639 op1 << 20 |
640 B15 | B14 | B13 | B12 |
641 op2 << 4 |
642 static_cast<uint32_t>(rd) << 8 |
643 static_cast<uint32_t>(rn) << 16 |
644 static_cast<uint32_t>(rm);
645
646 Emit32(encoding);
647 }
648}
649
650
651void Thumb2Assembler::mla(Register rd, Register rn, Register rm, Register ra,
652 Condition cond) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700653 CheckCondition(cond);
654
Andreas Gampec8ccf682014-09-29 20:07:43 -0700655 uint32_t op1 = 0U /* 0b000 */;
656 uint32_t op2 = 0U /* 0b00 */;
Dave Allison65fcc2c2014-04-28 13:45:27 -0700657 int32_t encoding = B31 | B30 | B29 | B28 | B27 | B25 | B24 |
658 op1 << 20 |
659 op2 << 4 |
660 static_cast<uint32_t>(rd) << 8 |
661 static_cast<uint32_t>(ra) << 12 |
662 static_cast<uint32_t>(rn) << 16 |
663 static_cast<uint32_t>(rm);
664
665 Emit32(encoding);
666}
667
668
669void Thumb2Assembler::mls(Register rd, Register rn, Register rm, Register ra,
670 Condition cond) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700671 CheckCondition(cond);
672
Andreas Gampec8ccf682014-09-29 20:07:43 -0700673 uint32_t op1 = 0U /* 0b000 */;
674 uint32_t op2 = 01 /* 0b01 */;
Dave Allison65fcc2c2014-04-28 13:45:27 -0700675 int32_t encoding = B31 | B30 | B29 | B28 | B27 | B25 | B24 |
676 op1 << 20 |
677 op2 << 4 |
678 static_cast<uint32_t>(rd) << 8 |
679 static_cast<uint32_t>(ra) << 12 |
680 static_cast<uint32_t>(rn) << 16 |
681 static_cast<uint32_t>(rm);
682
683 Emit32(encoding);
684}
685
686
Zheng Xuc6667102015-05-15 16:08:45 +0800687void Thumb2Assembler::smull(Register rd_lo, Register rd_hi, Register rn,
688 Register rm, Condition cond) {
689 CheckCondition(cond);
690
691 uint32_t op1 = 0U /* 0b000; */;
692 uint32_t op2 = 0U /* 0b0000 */;
693 int32_t encoding = B31 | B30 | B29 | B28 | B27 | B25 | B24 | B23 |
694 op1 << 20 |
695 op2 << 4 |
696 static_cast<uint32_t>(rd_lo) << 12 |
697 static_cast<uint32_t>(rd_hi) << 8 |
698 static_cast<uint32_t>(rn) << 16 |
699 static_cast<uint32_t>(rm);
700
701 Emit32(encoding);
702}
703
704
Dave Allison65fcc2c2014-04-28 13:45:27 -0700705void Thumb2Assembler::umull(Register rd_lo, Register rd_hi, Register rn,
706 Register rm, Condition cond) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700707 CheckCondition(cond);
708
Andreas Gampec8ccf682014-09-29 20:07:43 -0700709 uint32_t op1 = 2U /* 0b010; */;
710 uint32_t op2 = 0U /* 0b0000 */;
Dave Allison65fcc2c2014-04-28 13:45:27 -0700711 int32_t encoding = B31 | B30 | B29 | B28 | B27 | B25 | B24 | B23 |
712 op1 << 20 |
713 op2 << 4 |
714 static_cast<uint32_t>(rd_lo) << 12 |
715 static_cast<uint32_t>(rd_hi) << 8 |
716 static_cast<uint32_t>(rn) << 16 |
717 static_cast<uint32_t>(rm);
718
719 Emit32(encoding);
720}
721
722
723void Thumb2Assembler::sdiv(Register rd, Register rn, Register rm, Condition cond) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700724 CheckCondition(cond);
725
Andreas Gampec8ccf682014-09-29 20:07:43 -0700726 uint32_t op1 = 1U /* 0b001 */;
727 uint32_t op2 = 15U /* 0b1111 */;
Dave Allison65fcc2c2014-04-28 13:45:27 -0700728 int32_t encoding = B31 | B30 | B29 | B28 | B27 | B25 | B24 | B23 | B20 |
729 op1 << 20 |
730 op2 << 4 |
731 0xf << 12 |
732 static_cast<uint32_t>(rd) << 8 |
733 static_cast<uint32_t>(rn) << 16 |
734 static_cast<uint32_t>(rm);
735
736 Emit32(encoding);
737}
738
739
740void Thumb2Assembler::udiv(Register rd, Register rn, Register rm, Condition cond) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700741 CheckCondition(cond);
742
Andreas Gampec8ccf682014-09-29 20:07:43 -0700743 uint32_t op1 = 1U /* 0b001 */;
744 uint32_t op2 = 15U /* 0b1111 */;
Dave Allison65fcc2c2014-04-28 13:45:27 -0700745 int32_t encoding = B31 | B30 | B29 | B28 | B27 | B25 | B24 | B23 | B21 | B20 |
746 op1 << 20 |
747 op2 << 4 |
748 0xf << 12 |
749 static_cast<uint32_t>(rd) << 8 |
750 static_cast<uint32_t>(rn) << 16 |
751 static_cast<uint32_t>(rm);
752
753 Emit32(encoding);
754}
755
756
Roland Levillain51d3fc42014-11-13 14:11:42 +0000757void Thumb2Assembler::sbfx(Register rd, Register rn, uint32_t lsb, uint32_t width, Condition cond) {
758 CheckCondition(cond);
759 CHECK_LE(lsb, 31U);
760 CHECK(1U <= width && width <= 32U) << width;
761 uint32_t widthminus1 = width - 1;
762 uint32_t imm2 = lsb & (B1 | B0); // Bits 0-1 of `lsb`.
763 uint32_t imm3 = (lsb & (B4 | B3 | B2)) >> 2; // Bits 2-4 of `lsb`.
764
765 uint32_t op = 20U /* 0b10100 */;
766 int32_t encoding = B31 | B30 | B29 | B28 | B25 |
767 op << 20 |
768 static_cast<uint32_t>(rn) << 16 |
769 imm3 << 12 |
770 static_cast<uint32_t>(rd) << 8 |
771 imm2 << 6 |
772 widthminus1;
773
774 Emit32(encoding);
775}
776
777
Roland Levillain981e4542014-11-14 11:47:14 +0000778void Thumb2Assembler::ubfx(Register rd, Register rn, uint32_t lsb, uint32_t width, Condition cond) {
779 CheckCondition(cond);
780 CHECK_LE(lsb, 31U);
781 CHECK(1U <= width && width <= 32U) << width;
782 uint32_t widthminus1 = width - 1;
783 uint32_t imm2 = lsb & (B1 | B0); // Bits 0-1 of `lsb`.
784 uint32_t imm3 = (lsb & (B4 | B3 | B2)) >> 2; // Bits 2-4 of `lsb`.
785
786 uint32_t op = 28U /* 0b11100 */;
787 int32_t encoding = B31 | B30 | B29 | B28 | B25 |
788 op << 20 |
789 static_cast<uint32_t>(rn) << 16 |
790 imm3 << 12 |
791 static_cast<uint32_t>(rd) << 8 |
792 imm2 << 6 |
793 widthminus1;
794
795 Emit32(encoding);
796}
797
798
Dave Allison65fcc2c2014-04-28 13:45:27 -0700799void Thumb2Assembler::ldr(Register rd, const Address& ad, Condition cond) {
800 EmitLoadStore(cond, true, false, false, false, rd, ad);
801}
802
803
804void Thumb2Assembler::str(Register rd, const Address& ad, Condition cond) {
805 EmitLoadStore(cond, false, false, false, false, rd, ad);
806}
807
808
809void Thumb2Assembler::ldrb(Register rd, const Address& ad, Condition cond) {
810 EmitLoadStore(cond, true, true, false, false, rd, ad);
811}
812
813
814void Thumb2Assembler::strb(Register rd, const Address& ad, Condition cond) {
815 EmitLoadStore(cond, false, true, false, false, rd, ad);
816}
817
818
819void Thumb2Assembler::ldrh(Register rd, const Address& ad, Condition cond) {
820 EmitLoadStore(cond, true, false, true, false, rd, ad);
821}
822
823
824void Thumb2Assembler::strh(Register rd, const Address& ad, Condition cond) {
825 EmitLoadStore(cond, false, false, true, false, rd, ad);
826}
827
828
829void Thumb2Assembler::ldrsb(Register rd, const Address& ad, Condition cond) {
830 EmitLoadStore(cond, true, true, false, true, rd, ad);
831}
832
833
834void Thumb2Assembler::ldrsh(Register rd, const Address& ad, Condition cond) {
835 EmitLoadStore(cond, true, false, true, true, rd, ad);
836}
837
838
839void Thumb2Assembler::ldrd(Register rd, const Address& ad, Condition cond) {
Roland Levillain4af147e2015-04-07 13:54:49 +0100840 ldrd(rd, Register(rd + 1), ad, cond);
841}
842
843
844void Thumb2Assembler::ldrd(Register rd, Register rd2, const Address& ad, Condition cond) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700845 CheckCondition(cond);
Roland Levillain4af147e2015-04-07 13:54:49 +0100846 // Encoding T1.
Dave Allison65fcc2c2014-04-28 13:45:27 -0700847 // This is different from other loads. The encoding is like ARM.
848 int32_t encoding = B31 | B30 | B29 | B27 | B22 | B20 |
849 static_cast<int32_t>(rd) << 12 |
Roland Levillain4af147e2015-04-07 13:54:49 +0100850 static_cast<int32_t>(rd2) << 8 |
Dave Allison65fcc2c2014-04-28 13:45:27 -0700851 ad.encodingThumbLdrdStrd();
852 Emit32(encoding);
853}
854
855
856void Thumb2Assembler::strd(Register rd, const Address& ad, Condition cond) {
Roland Levillain4af147e2015-04-07 13:54:49 +0100857 strd(rd, Register(rd + 1), ad, cond);
858}
859
860
861void Thumb2Assembler::strd(Register rd, Register rd2, const Address& ad, Condition cond) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700862 CheckCondition(cond);
Roland Levillain4af147e2015-04-07 13:54:49 +0100863 // Encoding T1.
Dave Allison65fcc2c2014-04-28 13:45:27 -0700864 // This is different from other loads. The encoding is like ARM.
865 int32_t encoding = B31 | B30 | B29 | B27 | B22 |
866 static_cast<int32_t>(rd) << 12 |
Roland Levillain4af147e2015-04-07 13:54:49 +0100867 static_cast<int32_t>(rd2) << 8 |
Dave Allison65fcc2c2014-04-28 13:45:27 -0700868 ad.encodingThumbLdrdStrd();
869 Emit32(encoding);
870}
871
872
873void Thumb2Assembler::ldm(BlockAddressMode am,
874 Register base,
875 RegList regs,
876 Condition cond) {
Vladimir Markoe8469c12014-11-26 18:09:30 +0000877 CHECK_NE(regs, 0u); // Do not use ldm if there's nothing to load.
878 if (IsPowerOfTwo(regs)) {
Dave Allison65fcc2c2014-04-28 13:45:27 -0700879 // Thumb doesn't support one reg in the list.
880 // Find the register number.
Vladimir Markoe8469c12014-11-26 18:09:30 +0000881 int reg = CTZ(static_cast<uint32_t>(regs));
Dave Allison65fcc2c2014-04-28 13:45:27 -0700882 CHECK_LT(reg, 16);
Dave Allison45fdb932014-06-25 12:37:10 -0700883 CHECK(am == DB_W); // Only writeback is supported.
Dave Allison65fcc2c2014-04-28 13:45:27 -0700884 ldr(static_cast<Register>(reg), Address(base, kRegisterSize, Address::PostIndex), cond);
885 } else {
886 EmitMultiMemOp(cond, am, true, base, regs);
887 }
888}
889
890
891void Thumb2Assembler::stm(BlockAddressMode am,
892 Register base,
893 RegList regs,
894 Condition cond) {
Vladimir Markoe8469c12014-11-26 18:09:30 +0000895 CHECK_NE(regs, 0u); // Do not use stm if there's nothing to store.
896 if (IsPowerOfTwo(regs)) {
Dave Allison65fcc2c2014-04-28 13:45:27 -0700897 // Thumb doesn't support one reg in the list.
898 // Find the register number.
Vladimir Markoe8469c12014-11-26 18:09:30 +0000899 int reg = CTZ(static_cast<uint32_t>(regs));
Dave Allison65fcc2c2014-04-28 13:45:27 -0700900 CHECK_LT(reg, 16);
Dave Allison45fdb932014-06-25 12:37:10 -0700901 CHECK(am == IA || am == IA_W);
902 Address::Mode strmode = am == IA ? Address::PreIndex : Address::Offset;
Dave Allison65fcc2c2014-04-28 13:45:27 -0700903 str(static_cast<Register>(reg), Address(base, -kRegisterSize, strmode), cond);
904 } else {
905 EmitMultiMemOp(cond, am, false, base, regs);
906 }
907}
908
909
910bool Thumb2Assembler::vmovs(SRegister sd, float s_imm, Condition cond) {
911 uint32_t imm32 = bit_cast<uint32_t, float>(s_imm);
912 if (((imm32 & ((1 << 19) - 1)) == 0) &&
913 ((((imm32 >> 25) & ((1 << 6) - 1)) == (1 << 5)) ||
914 (((imm32 >> 25) & ((1 << 6) - 1)) == ((1 << 5) -1)))) {
915 uint8_t imm8 = ((imm32 >> 31) << 7) | (((imm32 >> 29) & 1) << 6) |
916 ((imm32 >> 19) & ((1 << 6) -1));
917 EmitVFPsss(cond, B23 | B21 | B20 | ((imm8 >> 4)*B16) | (imm8 & 0xf),
918 sd, S0, S0);
919 return true;
920 }
921 return false;
922}
923
924
925bool Thumb2Assembler::vmovd(DRegister dd, double d_imm, Condition cond) {
926 uint64_t imm64 = bit_cast<uint64_t, double>(d_imm);
927 if (((imm64 & ((1LL << 48) - 1)) == 0) &&
928 ((((imm64 >> 54) & ((1 << 9) - 1)) == (1 << 8)) ||
929 (((imm64 >> 54) & ((1 << 9) - 1)) == ((1 << 8) -1)))) {
930 uint8_t imm8 = ((imm64 >> 63) << 7) | (((imm64 >> 61) & 1) << 6) |
931 ((imm64 >> 48) & ((1 << 6) -1));
932 EmitVFPddd(cond, B23 | B21 | B20 | ((imm8 >> 4)*B16) | B8 | (imm8 & 0xf),
933 dd, D0, D0);
934 return true;
935 }
936 return false;
937}
938
939
940void Thumb2Assembler::vmovs(SRegister sd, SRegister sm, Condition cond) {
941 EmitVFPsss(cond, B23 | B21 | B20 | B6, sd, S0, sm);
942}
943
944
945void Thumb2Assembler::vmovd(DRegister dd, DRegister dm, Condition cond) {
946 EmitVFPddd(cond, B23 | B21 | B20 | B6, dd, D0, dm);
947}
948
949
950void Thumb2Assembler::vadds(SRegister sd, SRegister sn, SRegister sm,
951 Condition cond) {
952 EmitVFPsss(cond, B21 | B20, sd, sn, sm);
953}
954
955
956void Thumb2Assembler::vaddd(DRegister dd, DRegister dn, DRegister dm,
957 Condition cond) {
958 EmitVFPddd(cond, B21 | B20, dd, dn, dm);
959}
960
961
962void Thumb2Assembler::vsubs(SRegister sd, SRegister sn, SRegister sm,
963 Condition cond) {
964 EmitVFPsss(cond, B21 | B20 | B6, sd, sn, sm);
965}
966
967
968void Thumb2Assembler::vsubd(DRegister dd, DRegister dn, DRegister dm,
969 Condition cond) {
970 EmitVFPddd(cond, B21 | B20 | B6, dd, dn, dm);
971}
972
973
974void Thumb2Assembler::vmuls(SRegister sd, SRegister sn, SRegister sm,
975 Condition cond) {
976 EmitVFPsss(cond, B21, sd, sn, sm);
977}
978
979
980void Thumb2Assembler::vmuld(DRegister dd, DRegister dn, DRegister dm,
981 Condition cond) {
982 EmitVFPddd(cond, B21, dd, dn, dm);
983}
984
985
986void Thumb2Assembler::vmlas(SRegister sd, SRegister sn, SRegister sm,
987 Condition cond) {
988 EmitVFPsss(cond, 0, sd, sn, sm);
989}
990
991
992void Thumb2Assembler::vmlad(DRegister dd, DRegister dn, DRegister dm,
993 Condition cond) {
994 EmitVFPddd(cond, 0, dd, dn, dm);
995}
996
997
998void Thumb2Assembler::vmlss(SRegister sd, SRegister sn, SRegister sm,
999 Condition cond) {
1000 EmitVFPsss(cond, B6, sd, sn, sm);
1001}
1002
1003
1004void Thumb2Assembler::vmlsd(DRegister dd, DRegister dn, DRegister dm,
1005 Condition cond) {
1006 EmitVFPddd(cond, B6, dd, dn, dm);
1007}
1008
1009
1010void Thumb2Assembler::vdivs(SRegister sd, SRegister sn, SRegister sm,
1011 Condition cond) {
1012 EmitVFPsss(cond, B23, sd, sn, sm);
1013}
1014
1015
1016void Thumb2Assembler::vdivd(DRegister dd, DRegister dn, DRegister dm,
1017 Condition cond) {
1018 EmitVFPddd(cond, B23, dd, dn, dm);
1019}
1020
1021
1022void Thumb2Assembler::vabss(SRegister sd, SRegister sm, Condition cond) {
1023 EmitVFPsss(cond, B23 | B21 | B20 | B7 | B6, sd, S0, sm);
1024}
1025
1026
1027void Thumb2Assembler::vabsd(DRegister dd, DRegister dm, Condition cond) {
1028 EmitVFPddd(cond, B23 | B21 | B20 | B7 | B6, dd, D0, dm);
1029}
1030
1031
1032void Thumb2Assembler::vnegs(SRegister sd, SRegister sm, Condition cond) {
1033 EmitVFPsss(cond, B23 | B21 | B20 | B16 | B6, sd, S0, sm);
1034}
1035
1036
1037void Thumb2Assembler::vnegd(DRegister dd, DRegister dm, Condition cond) {
1038 EmitVFPddd(cond, B23 | B21 | B20 | B16 | B6, dd, D0, dm);
1039}
1040
1041
1042void Thumb2Assembler::vsqrts(SRegister sd, SRegister sm, Condition cond) {
1043 EmitVFPsss(cond, B23 | B21 | B20 | B16 | B7 | B6, sd, S0, sm);
1044}
1045
1046void Thumb2Assembler::vsqrtd(DRegister dd, DRegister dm, Condition cond) {
1047 EmitVFPddd(cond, B23 | B21 | B20 | B16 | B7 | B6, dd, D0, dm);
1048}
1049
1050
1051void Thumb2Assembler::vcvtsd(SRegister sd, DRegister dm, Condition cond) {
1052 EmitVFPsd(cond, B23 | B21 | B20 | B18 | B17 | B16 | B8 | B7 | B6, sd, dm);
1053}
1054
1055
1056void Thumb2Assembler::vcvtds(DRegister dd, SRegister sm, Condition cond) {
1057 EmitVFPds(cond, B23 | B21 | B20 | B18 | B17 | B16 | B7 | B6, dd, sm);
1058}
1059
1060
1061void Thumb2Assembler::vcvtis(SRegister sd, SRegister sm, Condition cond) {
1062 EmitVFPsss(cond, B23 | B21 | B20 | B19 | B18 | B16 | B7 | B6, sd, S0, sm);
1063}
1064
1065
1066void Thumb2Assembler::vcvtid(SRegister sd, DRegister dm, Condition cond) {
1067 EmitVFPsd(cond, B23 | B21 | B20 | B19 | B18 | B16 | B8 | B7 | B6, sd, dm);
1068}
1069
1070
1071void Thumb2Assembler::vcvtsi(SRegister sd, SRegister sm, Condition cond) {
1072 EmitVFPsss(cond, B23 | B21 | B20 | B19 | B7 | B6, sd, S0, sm);
1073}
1074
1075
1076void Thumb2Assembler::vcvtdi(DRegister dd, SRegister sm, Condition cond) {
1077 EmitVFPds(cond, B23 | B21 | B20 | B19 | B8 | B7 | B6, dd, sm);
1078}
1079
1080
1081void Thumb2Assembler::vcvtus(SRegister sd, SRegister sm, Condition cond) {
1082 EmitVFPsss(cond, B23 | B21 | B20 | B19 | B18 | B7 | B6, sd, S0, sm);
1083}
1084
1085
1086void Thumb2Assembler::vcvtud(SRegister sd, DRegister dm, Condition cond) {
1087 EmitVFPsd(cond, B23 | B21 | B20 | B19 | B18 | B8 | B7 | B6, sd, dm);
1088}
1089
1090
1091void Thumb2Assembler::vcvtsu(SRegister sd, SRegister sm, Condition cond) {
1092 EmitVFPsss(cond, B23 | B21 | B20 | B19 | B6, sd, S0, sm);
1093}
1094
1095
1096void Thumb2Assembler::vcvtdu(DRegister dd, SRegister sm, Condition cond) {
1097 EmitVFPds(cond, B23 | B21 | B20 | B19 | B8 | B6, dd, sm);
1098}
1099
1100
1101void Thumb2Assembler::vcmps(SRegister sd, SRegister sm, Condition cond) {
1102 EmitVFPsss(cond, B23 | B21 | B20 | B18 | B6, sd, S0, sm);
1103}
1104
1105
1106void Thumb2Assembler::vcmpd(DRegister dd, DRegister dm, Condition cond) {
1107 EmitVFPddd(cond, B23 | B21 | B20 | B18 | B6, dd, D0, dm);
1108}
1109
1110
1111void Thumb2Assembler::vcmpsz(SRegister sd, Condition cond) {
1112 EmitVFPsss(cond, B23 | B21 | B20 | B18 | B16 | B6, sd, S0, S0);
1113}
1114
1115
1116void Thumb2Assembler::vcmpdz(DRegister dd, Condition cond) {
1117 EmitVFPddd(cond, B23 | B21 | B20 | B18 | B16 | B6, dd, D0, D0);
1118}
1119
1120void Thumb2Assembler::b(Label* label, Condition cond) {
agicsakie2142d252015-06-30 17:10:03 -07001121 DCHECK_EQ(next_condition_, AL);
Dave Allison65fcc2c2014-04-28 13:45:27 -07001122 EmitBranch(cond, label, false, false);
1123}
1124
1125
1126void Thumb2Assembler::bl(Label* label, Condition cond) {
1127 CheckCondition(cond);
1128 EmitBranch(cond, label, true, false);
1129}
1130
1131
1132void Thumb2Assembler::blx(Label* label) {
1133 EmitBranch(AL, label, true, true);
1134}
1135
1136
1137void Thumb2Assembler::MarkExceptionHandler(Label* label) {
Vladimir Marko73cf0fb2015-07-30 15:07:22 +01001138 EmitDataProcessing(AL, TST, kCcSet, PC, R0, ShifterOperand(0));
Dave Allison65fcc2c2014-04-28 13:45:27 -07001139 Label l;
1140 b(&l);
1141 EmitBranch(AL, label, false, false);
1142 Bind(&l);
1143}
1144
1145
1146void Thumb2Assembler::Emit32(int32_t value) {
1147 AssemblerBuffer::EnsureCapacity ensured(&buffer_);
1148 buffer_.Emit<int16_t>(value >> 16);
1149 buffer_.Emit<int16_t>(value & 0xffff);
1150}
1151
1152
1153void Thumb2Assembler::Emit16(int16_t value) {
1154 AssemblerBuffer::EnsureCapacity ensured(&buffer_);
1155 buffer_.Emit<int16_t>(value);
1156}
1157
1158
Vladimir Marko73cf0fb2015-07-30 15:07:22 +01001159bool Thumb2Assembler::Is32BitDataProcessing(Condition cond,
Dave Allison65fcc2c2014-04-28 13:45:27 -07001160 Opcode opcode,
Vladimir Marko73cf0fb2015-07-30 15:07:22 +01001161 SetCc set_cc,
Dave Allison65fcc2c2014-04-28 13:45:27 -07001162 Register rn,
1163 Register rd,
1164 const ShifterOperand& so) {
1165 if (force_32bit_) {
1166 return true;
1167 }
1168
Vladimir Marko5bc561c2014-12-16 17:41:59 +00001169 // Check special case for SP relative ADD and SUB immediate.
Vladimir Marko73cf0fb2015-07-30 15:07:22 +01001170 if ((opcode == ADD || opcode == SUB) && rn == SP && so.IsImmediate() && set_cc != kCcSet) {
Vladimir Marko5bc561c2014-12-16 17:41:59 +00001171 // If the immediate is in range, use 16 bit.
1172 if (rd == SP) {
1173 if (so.GetImmediate() < (1 << 9)) { // 9 bit immediate.
1174 return false;
Dave Allison65fcc2c2014-04-28 13:45:27 -07001175 }
Vladimir Marko5bc561c2014-12-16 17:41:59 +00001176 } else if (!IsHighRegister(rd) && opcode == ADD) {
1177 if (so.GetImmediate() < (1 << 10)) { // 10 bit immediate.
1178 return false;
Dave Allison65fcc2c2014-04-28 13:45:27 -07001179 }
1180 }
Vladimir Marko5bc561c2014-12-16 17:41:59 +00001181 }
Dave Allison65fcc2c2014-04-28 13:45:27 -07001182
Vladimir Marko73cf0fb2015-07-30 15:07:22 +01001183 bool can_contain_high_register =
1184 (opcode == CMP) ||
1185 (opcode == MOV && set_cc != kCcSet) ||
1186 ((opcode == ADD) && (rn == rd) && set_cc != kCcSet);
Vladimir Marko5bc561c2014-12-16 17:41:59 +00001187
1188 if (IsHighRegister(rd) || IsHighRegister(rn)) {
1189 if (!can_contain_high_register) {
Dave Allison65fcc2c2014-04-28 13:45:27 -07001190 return true;
1191 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001192
Vladimir Marko5bc561c2014-12-16 17:41:59 +00001193 // There are high register instructions available for this opcode.
1194 // However, there is no actual shift available, neither for ADD nor for MOV (ASR/LSR/LSL/ROR).
1195 if (so.IsShift() && (so.GetShift() == RRX || so.GetImmediate() != 0u)) {
1196 return true;
1197 }
1198
1199 // The ADD and MOV instructions that work with high registers don't have 16-bit
1200 // immediate variants.
1201 if (so.IsImmediate()) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001202 return true;
1203 }
Dave Allison65fcc2c2014-04-28 13:45:27 -07001204 }
1205
1206 if (so.IsRegister() && IsHighRegister(so.GetRegister()) && !can_contain_high_register) {
1207 return true;
1208 }
1209
Dave Allison65fcc2c2014-04-28 13:45:27 -07001210 bool rn_is_valid = true;
1211
1212 // Check for single operand instructions and ADD/SUB.
1213 switch (opcode) {
1214 case CMP:
1215 case MOV:
1216 case TST:
1217 case MVN:
1218 rn_is_valid = false; // There is no Rn for these instructions.
1219 break;
1220 case TEQ:
Vladimir Markod2b4ca22015-09-14 15:13:26 +01001221 case ORN:
Dave Allison65fcc2c2014-04-28 13:45:27 -07001222 return true;
Dave Allison65fcc2c2014-04-28 13:45:27 -07001223 case ADD:
1224 case SUB:
1225 break;
1226 default:
1227 if (so.IsRegister() && rd != rn) {
1228 return true;
1229 }
1230 }
1231
1232 if (so.IsImmediate()) {
Vladimir Marko73cf0fb2015-07-30 15:07:22 +01001233 if (opcode == RSB) {
1234 DCHECK(rn_is_valid);
1235 if (so.GetImmediate() != 0u) {
Dave Allison65fcc2c2014-04-28 13:45:27 -07001236 return true;
Vladimir Marko73cf0fb2015-07-30 15:07:22 +01001237 }
1238 } else if (rn_is_valid && rn != rd) {
1239 // The only thumb1 instructions with a register and an immediate are ADD and SUB
1240 // with a 3-bit immediate, and RSB with zero immediate.
1241 if (opcode == ADD || opcode == SUB) {
1242 if (!IsUint<3>(so.GetImmediate())) {
Dave Allison65fcc2c2014-04-28 13:45:27 -07001243 return true;
1244 }
Vladimir Marko73cf0fb2015-07-30 15:07:22 +01001245 } else {
1246 return true;
Dave Allison65fcc2c2014-04-28 13:45:27 -07001247 }
1248 } else {
1249 // ADD, SUB, CMP and MOV may be thumb1 only if the immediate is 8 bits.
1250 if (!(opcode == ADD || opcode == SUB || opcode == MOV || opcode == CMP)) {
1251 return true;
1252 } else {
Vladimir Marko73cf0fb2015-07-30 15:07:22 +01001253 if (!IsUint<8>(so.GetImmediate())) {
Dave Allison65fcc2c2014-04-28 13:45:27 -07001254 return true;
1255 }
1256 }
1257 }
Vladimir Marko73cf0fb2015-07-30 15:07:22 +01001258 } else {
1259 DCHECK(so.IsRegister());
1260 if (so.IsShift()) {
1261 // Shift operand - check if it is a MOV convertible to a 16-bit shift instruction.
1262 if (opcode != MOV) {
Zheng Xuc6667102015-05-15 16:08:45 +08001263 return true;
1264 }
Vladimir Marko73cf0fb2015-07-30 15:07:22 +01001265 // Check for MOV with an ROR/RRX. There is no 16-bit ROR immediate and no 16-bit RRX.
1266 if (so.GetShift() == ROR || so.GetShift() == RRX) {
1267 return true;
1268 }
1269 // 16-bit shifts set condition codes if and only if outside IT block,
1270 // i.e. if and only if cond == AL.
1271 if ((cond == AL) ? set_cc == kCcKeep : set_cc == kCcSet) {
1272 return true;
1273 }
1274 } else {
1275 // Register operand without shift.
1276 switch (opcode) {
1277 case ADD:
1278 // The 16-bit ADD that cannot contain high registers can set condition codes
1279 // if and only if outside IT block, i.e. if and only if cond == AL.
1280 if (!can_contain_high_register &&
1281 ((cond == AL) ? set_cc == kCcKeep : set_cc == kCcSet)) {
1282 return true;
1283 }
1284 break;
1285 case AND:
1286 case BIC:
1287 case EOR:
1288 case ORR:
1289 case MVN:
1290 case ADC:
1291 case SUB:
1292 case SBC:
1293 // These 16-bit opcodes set condition codes if and only if outside IT block,
1294 // i.e. if and only if cond == AL.
1295 if ((cond == AL) ? set_cc == kCcKeep : set_cc == kCcSet) {
1296 return true;
1297 }
1298 break;
1299 case RSB:
1300 case RSC:
1301 // No 16-bit RSB/RSC Rd, Rm, Rn. It would be equivalent to SUB/SBC Rd, Rn, Rm.
1302 return true;
1303 case CMP:
1304 default:
1305 break;
1306 }
Zheng Xuc6667102015-05-15 16:08:45 +08001307 }
1308 }
1309
Dave Allison65fcc2c2014-04-28 13:45:27 -07001310 // The instruction can be encoded in 16 bits.
1311 return false;
1312}
1313
1314
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001315void Thumb2Assembler::Emit32BitDataProcessing(Condition cond ATTRIBUTE_UNUSED,
Dave Allison65fcc2c2014-04-28 13:45:27 -07001316 Opcode opcode,
Vladimir Marko73cf0fb2015-07-30 15:07:22 +01001317 SetCc set_cc,
Dave Allison65fcc2c2014-04-28 13:45:27 -07001318 Register rn,
1319 Register rd,
1320 const ShifterOperand& so) {
Andreas Gampec8ccf682014-09-29 20:07:43 -07001321 uint8_t thumb_opcode = 255U /* 0b11111111 */;
Dave Allison65fcc2c2014-04-28 13:45:27 -07001322 switch (opcode) {
Andreas Gampec8ccf682014-09-29 20:07:43 -07001323 case AND: thumb_opcode = 0U /* 0b0000 */; break;
1324 case EOR: thumb_opcode = 4U /* 0b0100 */; break;
1325 case SUB: thumb_opcode = 13U /* 0b1101 */; break;
1326 case RSB: thumb_opcode = 14U /* 0b1110 */; break;
1327 case ADD: thumb_opcode = 8U /* 0b1000 */; break;
Andreas Gampe35c68e32014-09-30 08:39:37 -07001328 case ADC: thumb_opcode = 10U /* 0b1010 */; break;
Andreas Gampec8ccf682014-09-29 20:07:43 -07001329 case SBC: thumb_opcode = 11U /* 0b1011 */; break;
Dave Allison65fcc2c2014-04-28 13:45:27 -07001330 case RSC: break;
Vladimir Marko73cf0fb2015-07-30 15:07:22 +01001331 case TST: thumb_opcode = 0U /* 0b0000 */; DCHECK(set_cc == kCcSet); rd = PC; break;
1332 case TEQ: thumb_opcode = 4U /* 0b0100 */; DCHECK(set_cc == kCcSet); rd = PC; break;
1333 case CMP: thumb_opcode = 13U /* 0b1101 */; DCHECK(set_cc == kCcSet); rd = PC; break;
1334 case CMN: thumb_opcode = 8U /* 0b1000 */; DCHECK(set_cc == kCcSet); rd = PC; break;
Andreas Gampec8ccf682014-09-29 20:07:43 -07001335 case ORR: thumb_opcode = 2U /* 0b0010 */; break;
1336 case MOV: thumb_opcode = 2U /* 0b0010 */; rn = PC; break;
1337 case BIC: thumb_opcode = 1U /* 0b0001 */; break;
1338 case MVN: thumb_opcode = 3U /* 0b0011 */; rn = PC; break;
Vladimir Markod2b4ca22015-09-14 15:13:26 +01001339 case ORN: thumb_opcode = 3U /* 0b0011 */; break;
Dave Allison65fcc2c2014-04-28 13:45:27 -07001340 default:
1341 break;
1342 }
1343
Andreas Gampec8ccf682014-09-29 20:07:43 -07001344 if (thumb_opcode == 255U /* 0b11111111 */) {
Dave Allison65fcc2c2014-04-28 13:45:27 -07001345 LOG(FATAL) << "Invalid thumb2 opcode " << opcode;
Vladimir Markoe8469c12014-11-26 18:09:30 +00001346 UNREACHABLE();
Dave Allison65fcc2c2014-04-28 13:45:27 -07001347 }
1348
1349 int32_t encoding = 0;
1350 if (so.IsImmediate()) {
1351 // Check special cases.
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001352 if ((opcode == SUB || opcode == ADD) && (so.GetImmediate() < (1u << 12))) {
Vladimir Marko73cf0fb2015-07-30 15:07:22 +01001353 if (set_cc != kCcSet) {
Guillaume "Vermeille" Sanchezdc62c482015-03-11 14:30:31 +00001354 if (opcode == SUB) {
1355 thumb_opcode = 5U;
1356 } else if (opcode == ADD) {
1357 thumb_opcode = 0U;
1358 }
Dave Allison65fcc2c2014-04-28 13:45:27 -07001359 }
1360 uint32_t imm = so.GetImmediate();
Dave Allison65fcc2c2014-04-28 13:45:27 -07001361
1362 uint32_t i = (imm >> 11) & 1;
Andreas Gampec8ccf682014-09-29 20:07:43 -07001363 uint32_t imm3 = (imm >> 8) & 7U /* 0b111 */;
Dave Allison65fcc2c2014-04-28 13:45:27 -07001364 uint32_t imm8 = imm & 0xff;
1365
Guillaume "Vermeille" Sanchezdc62c482015-03-11 14:30:31 +00001366 encoding = B31 | B30 | B29 | B28 |
Vladimir Marko73cf0fb2015-07-30 15:07:22 +01001367 (set_cc == kCcSet ? B20 : B25) |
Guillaume "Vermeille" Sanchezdc62c482015-03-11 14:30:31 +00001368 thumb_opcode << 21 |
1369 rn << 16 |
1370 rd << 8 |
1371 i << 26 |
1372 imm3 << 12 |
1373 imm8;
Dave Allison65fcc2c2014-04-28 13:45:27 -07001374 } else {
1375 // Modified immediate.
Dave Allison45fdb932014-06-25 12:37:10 -07001376 uint32_t imm = ModifiedImmediate(so.encodingThumb());
Dave Allison65fcc2c2014-04-28 13:45:27 -07001377 if (imm == kInvalidModifiedImmediate) {
1378 LOG(FATAL) << "Immediate value cannot fit in thumb2 modified immediate";
Vladimir Markoe8469c12014-11-26 18:09:30 +00001379 UNREACHABLE();
Dave Allison65fcc2c2014-04-28 13:45:27 -07001380 }
1381 encoding = B31 | B30 | B29 | B28 |
1382 thumb_opcode << 21 |
Vladimir Marko73cf0fb2015-07-30 15:07:22 +01001383 (set_cc == kCcSet ? B20 : 0) |
Dave Allison65fcc2c2014-04-28 13:45:27 -07001384 rn << 16 |
1385 rd << 8 |
1386 imm;
1387 }
1388 } else if (so.IsRegister()) {
Guillaume "Vermeille" Sanchezdc62c482015-03-11 14:30:31 +00001389 // Register (possibly shifted)
1390 encoding = B31 | B30 | B29 | B27 | B25 |
1391 thumb_opcode << 21 |
Vladimir Marko73cf0fb2015-07-30 15:07:22 +01001392 (set_cc == kCcSet ? B20 : 0) |
Guillaume "Vermeille" Sanchezdc62c482015-03-11 14:30:31 +00001393 rn << 16 |
1394 rd << 8 |
1395 so.encodingThumb();
Dave Allison65fcc2c2014-04-28 13:45:27 -07001396 }
1397 Emit32(encoding);
1398}
1399
1400
1401void Thumb2Assembler::Emit16BitDataProcessing(Condition cond,
1402 Opcode opcode,
Vladimir Marko73cf0fb2015-07-30 15:07:22 +01001403 SetCc set_cc,
Dave Allison65fcc2c2014-04-28 13:45:27 -07001404 Register rn,
1405 Register rd,
1406 const ShifterOperand& so) {
1407 if (opcode == ADD || opcode == SUB) {
1408 Emit16BitAddSub(cond, opcode, set_cc, rn, rd, so);
1409 return;
1410 }
Andreas Gampec8ccf682014-09-29 20:07:43 -07001411 uint8_t thumb_opcode = 255U /* 0b11111111 */;
Dave Allison65fcc2c2014-04-28 13:45:27 -07001412 // Thumb1.
Andreas Gampec8ccf682014-09-29 20:07:43 -07001413 uint8_t dp_opcode = 1U /* 0b01 */;
Dave Allison65fcc2c2014-04-28 13:45:27 -07001414 uint8_t opcode_shift = 6;
1415 uint8_t rd_shift = 0;
1416 uint8_t rn_shift = 3;
1417 uint8_t immediate_shift = 0;
1418 bool use_immediate = false;
1419 uint8_t immediate = 0;
1420
1421 if (opcode == MOV && so.IsRegister() && so.IsShift()) {
1422 // Convert shifted mov operand2 into 16 bit opcodes.
1423 dp_opcode = 0;
1424 opcode_shift = 11;
1425
1426 use_immediate = true;
1427 immediate = so.GetImmediate();
1428 immediate_shift = 6;
1429
1430 rn = so.GetRegister();
1431
1432 switch (so.GetShift()) {
Vladimir Marko73cf0fb2015-07-30 15:07:22 +01001433 case LSL:
1434 DCHECK_LE(immediate, 31u);
1435 thumb_opcode = 0U /* 0b00 */;
Dave Allison65fcc2c2014-04-28 13:45:27 -07001436 break;
Vladimir Marko73cf0fb2015-07-30 15:07:22 +01001437 case LSR:
1438 DCHECK(1 <= immediate && immediate <= 32);
1439 immediate &= 31; // 32 is encoded as 0.
1440 thumb_opcode = 1U /* 0b01 */;
1441 break;
1442 case ASR:
1443 DCHECK(1 <= immediate && immediate <= 32);
1444 immediate &= 31; // 32 is encoded as 0.
1445 thumb_opcode = 2U /* 0b10 */;
1446 break;
1447 case ROR: // No 16-bit ROR immediate.
1448 case RRX: // No 16-bit RRX.
Dave Allison65fcc2c2014-04-28 13:45:27 -07001449 default:
Vladimir Marko73cf0fb2015-07-30 15:07:22 +01001450 LOG(FATAL) << "Unexpected shift: " << so.GetShift();
1451 UNREACHABLE();
Dave Allison65fcc2c2014-04-28 13:45:27 -07001452 }
1453 } else {
1454 if (so.IsImmediate()) {
1455 use_immediate = true;
1456 immediate = so.GetImmediate();
Andreas Gampe513ea0c2015-02-02 13:17:52 -08001457 } else {
Guillaume "Vermeille" Sanchezab4a2f52015-03-11 14:00:30 +00001458 CHECK(!(so.IsRegister() && so.IsShift() && so.GetSecondRegister() != kNoRegister))
1459 << "No register-shifted register instruction available in thumb";
Andreas Gampe513ea0c2015-02-02 13:17:52 -08001460 // Adjust rn and rd: only two registers will be emitted.
1461 switch (opcode) {
1462 case AND:
1463 case ORR:
1464 case EOR:
1465 case RSB:
1466 case ADC:
1467 case SBC:
1468 case BIC: {
Vladimir Marko73cf0fb2015-07-30 15:07:22 +01001469 // Sets condition codes if and only if outside IT block,
1470 // check that it complies with set_cc.
1471 DCHECK((cond == AL) ? set_cc != kCcKeep : set_cc != kCcSet);
Andreas Gampe513ea0c2015-02-02 13:17:52 -08001472 if (rn == rd) {
1473 rn = so.GetRegister();
1474 } else {
1475 CHECK_EQ(rd, so.GetRegister());
1476 }
1477 break;
1478 }
1479 case CMP:
1480 case CMN: {
1481 CHECK_EQ(rd, 0);
1482 rd = rn;
1483 rn = so.GetRegister();
1484 break;
1485 }
1486 case MVN: {
Vladimir Marko73cf0fb2015-07-30 15:07:22 +01001487 // Sets condition codes if and only if outside IT block,
1488 // check that it complies with set_cc.
1489 DCHECK((cond == AL) ? set_cc != kCcKeep : set_cc != kCcSet);
1490 CHECK_EQ(rn, 0);
1491 rn = so.GetRegister();
1492 break;
1493 }
1494 case TST:
1495 case TEQ: {
1496 DCHECK(set_cc == kCcSet);
Andreas Gampe513ea0c2015-02-02 13:17:52 -08001497 CHECK_EQ(rn, 0);
1498 rn = so.GetRegister();
1499 break;
1500 }
1501 default:
1502 break;
1503 }
Dave Allison65fcc2c2014-04-28 13:45:27 -07001504 }
1505
1506 switch (opcode) {
Andreas Gampec8ccf682014-09-29 20:07:43 -07001507 case AND: thumb_opcode = 0U /* 0b0000 */; break;
Andreas Gampe513ea0c2015-02-02 13:17:52 -08001508 case ORR: thumb_opcode = 12U /* 0b1100 */; break;
Andreas Gampec8ccf682014-09-29 20:07:43 -07001509 case EOR: thumb_opcode = 1U /* 0b0001 */; break;
Andreas Gampec8ccf682014-09-29 20:07:43 -07001510 case RSB: thumb_opcode = 9U /* 0b1001 */; break;
Andreas Gampec8ccf682014-09-29 20:07:43 -07001511 case ADC: thumb_opcode = 5U /* 0b0101 */; break;
1512 case SBC: thumb_opcode = 6U /* 0b0110 */; break;
Andreas Gampe513ea0c2015-02-02 13:17:52 -08001513 case BIC: thumb_opcode = 14U /* 0b1110 */; break;
1514 case TST: thumb_opcode = 8U /* 0b1000 */; CHECK(!use_immediate); break;
1515 case MVN: thumb_opcode = 15U /* 0b1111 */; CHECK(!use_immediate); break;
1516 case CMP: {
Vladimir Marko73cf0fb2015-07-30 15:07:22 +01001517 DCHECK(set_cc == kCcSet);
Dave Allison65fcc2c2014-04-28 13:45:27 -07001518 if (use_immediate) {
1519 // T2 encoding.
Andreas Gampe513ea0c2015-02-02 13:17:52 -08001520 dp_opcode = 0;
1521 opcode_shift = 11;
1522 thumb_opcode = 5U /* 0b101 */;
1523 rd_shift = 8;
1524 rn_shift = 8;
Vladimir Marko73cf0fb2015-07-30 15:07:22 +01001525 } else if (IsHighRegister(rd) || IsHighRegister(rn)) {
1526 // Special cmp for high registers.
1527 dp_opcode = 1U /* 0b01 */;
1528 opcode_shift = 7;
1529 // Put the top bit of rd into the bottom bit of the opcode.
1530 thumb_opcode = 10U /* 0b0001010 */ | static_cast<uint32_t>(rd) >> 3;
1531 rd = static_cast<Register>(static_cast<uint32_t>(rd) & 7U /* 0b111 */);
Dave Allison65fcc2c2014-04-28 13:45:27 -07001532 } else {
Andreas Gampec8ccf682014-09-29 20:07:43 -07001533 thumb_opcode = 10U /* 0b1010 */;
Dave Allison65fcc2c2014-04-28 13:45:27 -07001534 }
1535
1536 break;
Andreas Gampe513ea0c2015-02-02 13:17:52 -08001537 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001538 case CMN: {
Andreas Gampe513ea0c2015-02-02 13:17:52 -08001539 CHECK(!use_immediate);
Andreas Gampec8ccf682014-09-29 20:07:43 -07001540 thumb_opcode = 11U /* 0b1011 */;
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001541 break;
1542 }
Dave Allison65fcc2c2014-04-28 13:45:27 -07001543 case MOV:
1544 dp_opcode = 0;
1545 if (use_immediate) {
1546 // T2 encoding.
1547 opcode_shift = 11;
Andreas Gampec8ccf682014-09-29 20:07:43 -07001548 thumb_opcode = 4U /* 0b100 */;
Dave Allison65fcc2c2014-04-28 13:45:27 -07001549 rd_shift = 8;
1550 rn_shift = 8;
1551 } else {
1552 rn = so.GetRegister();
Vladimir Marko73cf0fb2015-07-30 15:07:22 +01001553 if (set_cc != kCcSet) {
Dave Allison65fcc2c2014-04-28 13:45:27 -07001554 // Special mov for high registers.
Andreas Gampec8ccf682014-09-29 20:07:43 -07001555 dp_opcode = 1U /* 0b01 */;
Dave Allison65fcc2c2014-04-28 13:45:27 -07001556 opcode_shift = 7;
1557 // Put the top bit of rd into the bottom bit of the opcode.
Andreas Gampec8ccf682014-09-29 20:07:43 -07001558 thumb_opcode = 12U /* 0b0001100 */ | static_cast<uint32_t>(rd) >> 3;
1559 rd = static_cast<Register>(static_cast<uint32_t>(rd) & 7U /* 0b111 */);
Dave Allison65fcc2c2014-04-28 13:45:27 -07001560 } else {
Vladimir Marko73cf0fb2015-07-30 15:07:22 +01001561 DCHECK(!IsHighRegister(rn));
1562 DCHECK(!IsHighRegister(rd));
Dave Allison65fcc2c2014-04-28 13:45:27 -07001563 thumb_opcode = 0;
1564 }
1565 }
1566 break;
Andreas Gampe513ea0c2015-02-02 13:17:52 -08001567
1568 case TEQ:
1569 case RSC:
Dave Allison65fcc2c2014-04-28 13:45:27 -07001570 default:
Andreas Gampe513ea0c2015-02-02 13:17:52 -08001571 LOG(FATAL) << "Invalid thumb1 opcode " << opcode;
Dave Allison65fcc2c2014-04-28 13:45:27 -07001572 break;
1573 }
1574 }
1575
Andreas Gampec8ccf682014-09-29 20:07:43 -07001576 if (thumb_opcode == 255U /* 0b11111111 */) {
Dave Allison65fcc2c2014-04-28 13:45:27 -07001577 LOG(FATAL) << "Invalid thumb1 opcode " << opcode;
Vladimir Markoe8469c12014-11-26 18:09:30 +00001578 UNREACHABLE();
Dave Allison65fcc2c2014-04-28 13:45:27 -07001579 }
1580
1581 int16_t encoding = dp_opcode << 14 |
1582 (thumb_opcode << opcode_shift) |
1583 rd << rd_shift |
1584 rn << rn_shift |
1585 (use_immediate ? (immediate << immediate_shift) : 0);
1586
1587 Emit16(encoding);
1588}
1589
1590
1591// ADD and SUB are complex enough to warrant their own emitter.
Vladimir Marko73cf0fb2015-07-30 15:07:22 +01001592void Thumb2Assembler::Emit16BitAddSub(Condition cond,
Dave Allison65fcc2c2014-04-28 13:45:27 -07001593 Opcode opcode,
Vladimir Marko73cf0fb2015-07-30 15:07:22 +01001594 SetCc set_cc,
Dave Allison65fcc2c2014-04-28 13:45:27 -07001595 Register rn,
1596 Register rd,
1597 const ShifterOperand& so) {
1598 uint8_t dp_opcode = 0;
1599 uint8_t opcode_shift = 6;
1600 uint8_t rd_shift = 0;
1601 uint8_t rn_shift = 3;
1602 uint8_t immediate_shift = 0;
1603 bool use_immediate = false;
Vladimir Markoac0341e2014-12-18 19:56:49 +00001604 uint32_t immediate = 0; // Should be at most 9 bits but keep the full immediate for CHECKs.
Vladimir Marko73cf0fb2015-07-30 15:07:22 +01001605 uint8_t thumb_opcode;
Dave Allison65fcc2c2014-04-28 13:45:27 -07001606
1607 if (so.IsImmediate()) {
1608 use_immediate = true;
1609 immediate = so.GetImmediate();
1610 }
1611
1612 switch (opcode) {
1613 case ADD:
1614 if (so.IsRegister()) {
1615 Register rm = so.GetRegister();
Vladimir Marko73cf0fb2015-07-30 15:07:22 +01001616 if (rn == rd && set_cc != kCcSet) {
Dave Allison65fcc2c2014-04-28 13:45:27 -07001617 // Can use T2 encoding (allows 4 bit registers)
Andreas Gampec8ccf682014-09-29 20:07:43 -07001618 dp_opcode = 1U /* 0b01 */;
Dave Allison65fcc2c2014-04-28 13:45:27 -07001619 opcode_shift = 10;
Andreas Gampec8ccf682014-09-29 20:07:43 -07001620 thumb_opcode = 1U /* 0b0001 */;
Dave Allison65fcc2c2014-04-28 13:45:27 -07001621 // Make Rn also contain the top bit of rd.
1622 rn = static_cast<Register>(static_cast<uint32_t>(rm) |
Andreas Gampec8ccf682014-09-29 20:07:43 -07001623 (static_cast<uint32_t>(rd) & 8U /* 0b1000 */) << 1);
1624 rd = static_cast<Register>(static_cast<uint32_t>(rd) & 7U /* 0b111 */);
Dave Allison65fcc2c2014-04-28 13:45:27 -07001625 } else {
1626 // T1.
Vladimir Marko73cf0fb2015-07-30 15:07:22 +01001627 DCHECK(!IsHighRegister(rd));
1628 DCHECK(!IsHighRegister(rn));
1629 DCHECK(!IsHighRegister(rm));
1630 // Sets condition codes if and only if outside IT block,
1631 // check that it complies with set_cc.
1632 DCHECK((cond == AL) ? set_cc != kCcKeep : set_cc != kCcSet);
Dave Allison65fcc2c2014-04-28 13:45:27 -07001633 opcode_shift = 9;
Andreas Gampec8ccf682014-09-29 20:07:43 -07001634 thumb_opcode = 12U /* 0b01100 */;
Dave Allison65fcc2c2014-04-28 13:45:27 -07001635 immediate = static_cast<uint32_t>(so.GetRegister());
1636 use_immediate = true;
1637 immediate_shift = 6;
1638 }
1639 } else {
1640 // Immediate.
1641 if (rd == SP && rn == SP) {
1642 // ADD sp, sp, #imm
Andreas Gampec8ccf682014-09-29 20:07:43 -07001643 dp_opcode = 2U /* 0b10 */;
1644 thumb_opcode = 3U /* 0b11 */;
Dave Allison65fcc2c2014-04-28 13:45:27 -07001645 opcode_shift = 12;
Vladimir Markoac0341e2014-12-18 19:56:49 +00001646 CHECK_LT(immediate, (1u << 9));
Roland Levillain14d90572015-07-16 10:52:26 +01001647 CHECK_ALIGNED(immediate, 4);
Dave Allison65fcc2c2014-04-28 13:45:27 -07001648
1649 // Remove rd and rn from instruction by orring it with immed and clearing bits.
1650 rn = R0;
1651 rd = R0;
1652 rd_shift = 0;
1653 rn_shift = 0;
1654 immediate >>= 2;
1655 } else if (rd != SP && rn == SP) {
1656 // ADD rd, SP, #imm
Andreas Gampec8ccf682014-09-29 20:07:43 -07001657 dp_opcode = 2U /* 0b10 */;
1658 thumb_opcode = 5U /* 0b101 */;
Dave Allison65fcc2c2014-04-28 13:45:27 -07001659 opcode_shift = 11;
Vladimir Markoac0341e2014-12-18 19:56:49 +00001660 CHECK_LT(immediate, (1u << 10));
Roland Levillain14d90572015-07-16 10:52:26 +01001661 CHECK_ALIGNED(immediate, 4);
Dave Allison65fcc2c2014-04-28 13:45:27 -07001662
1663 // Remove rn from instruction.
1664 rn = R0;
1665 rn_shift = 0;
1666 rd_shift = 8;
1667 immediate >>= 2;
1668 } else if (rn != rd) {
1669 // Must use T1.
1670 opcode_shift = 9;
Andreas Gampec8ccf682014-09-29 20:07:43 -07001671 thumb_opcode = 14U /* 0b01110 */;
Dave Allison65fcc2c2014-04-28 13:45:27 -07001672 immediate_shift = 6;
1673 } else {
1674 // T2 encoding.
1675 opcode_shift = 11;
Andreas Gampec8ccf682014-09-29 20:07:43 -07001676 thumb_opcode = 6U /* 0b110 */;
Dave Allison65fcc2c2014-04-28 13:45:27 -07001677 rd_shift = 8;
1678 rn_shift = 8;
1679 }
1680 }
1681 break;
1682
1683 case SUB:
1684 if (so.IsRegister()) {
Vladimir Marko73cf0fb2015-07-30 15:07:22 +01001685 // T1.
1686 Register rm = so.GetRegister();
1687 DCHECK(!IsHighRegister(rd));
1688 DCHECK(!IsHighRegister(rn));
1689 DCHECK(!IsHighRegister(rm));
1690 // Sets condition codes if and only if outside IT block,
1691 // check that it complies with set_cc.
1692 DCHECK((cond == AL) ? set_cc != kCcKeep : set_cc != kCcSet);
1693 opcode_shift = 9;
1694 thumb_opcode = 13U /* 0b01101 */;
1695 immediate = static_cast<uint32_t>(rm);
1696 use_immediate = true;
1697 immediate_shift = 6;
1698 } else {
1699 if (rd == SP && rn == SP) {
1700 // SUB sp, sp, #imm
1701 dp_opcode = 2U /* 0b10 */;
1702 thumb_opcode = 0x61 /* 0b1100001 */;
1703 opcode_shift = 7;
1704 CHECK_LT(immediate, (1u << 9));
1705 CHECK_ALIGNED(immediate, 4);
Dave Allison65fcc2c2014-04-28 13:45:27 -07001706
Vladimir Marko73cf0fb2015-07-30 15:07:22 +01001707 // Remove rd and rn from instruction by orring it with immed and clearing bits.
1708 rn = R0;
1709 rd = R0;
1710 rd_shift = 0;
1711 rn_shift = 0;
1712 immediate >>= 2;
1713 } else if (rn != rd) {
1714 // Must use T1.
1715 opcode_shift = 9;
1716 thumb_opcode = 15U /* 0b01111 */;
1717 immediate_shift = 6;
1718 } else {
1719 // T2 encoding.
1720 opcode_shift = 11;
1721 thumb_opcode = 7U /* 0b111 */;
1722 rd_shift = 8;
1723 rn_shift = 8;
1724 }
1725 }
Dave Allison65fcc2c2014-04-28 13:45:27 -07001726 break;
1727 default:
1728 LOG(FATAL) << "This opcode is not an ADD or SUB: " << opcode;
Vladimir Markoe8469c12014-11-26 18:09:30 +00001729 UNREACHABLE();
Dave Allison65fcc2c2014-04-28 13:45:27 -07001730 }
1731
1732 int16_t encoding = dp_opcode << 14 |
1733 (thumb_opcode << opcode_shift) |
1734 rd << rd_shift |
1735 rn << rn_shift |
1736 (use_immediate ? (immediate << immediate_shift) : 0);
1737
1738 Emit16(encoding);
1739}
1740
1741
1742void Thumb2Assembler::EmitDataProcessing(Condition cond,
1743 Opcode opcode,
Vladimir Marko73cf0fb2015-07-30 15:07:22 +01001744 SetCc set_cc,
Dave Allison65fcc2c2014-04-28 13:45:27 -07001745 Register rn,
1746 Register rd,
1747 const ShifterOperand& so) {
1748 CHECK_NE(rd, kNoRegister);
1749 CheckCondition(cond);
1750
1751 if (Is32BitDataProcessing(cond, opcode, set_cc, rn, rd, so)) {
1752 Emit32BitDataProcessing(cond, opcode, set_cc, rn, rd, so);
1753 } else {
1754 Emit16BitDataProcessing(cond, opcode, set_cc, rn, rd, so);
1755 }
1756}
1757
Vladimir Marko73cf0fb2015-07-30 15:07:22 +01001758void Thumb2Assembler::EmitShift(Register rd,
1759 Register rm,
1760 Shift shift,
1761 uint8_t amount,
1762 Condition cond,
1763 SetCc set_cc) {
Dave Allison45fdb932014-06-25 12:37:10 -07001764 CHECK_LT(amount, (1 << 5));
Vladimir Marko73cf0fb2015-07-30 15:07:22 +01001765 if ((IsHighRegister(rd) || IsHighRegister(rm) || shift == ROR || shift == RRX) ||
1766 ((cond == AL) ? set_cc == kCcKeep : set_cc == kCcSet)) {
Dave Allison45fdb932014-06-25 12:37:10 -07001767 uint16_t opcode = 0;
1768 switch (shift) {
Andreas Gampec8ccf682014-09-29 20:07:43 -07001769 case LSL: opcode = 0U /* 0b00 */; break;
1770 case LSR: opcode = 1U /* 0b01 */; break;
1771 case ASR: opcode = 2U /* 0b10 */; break;
1772 case ROR: opcode = 3U /* 0b11 */; break;
1773 case RRX: opcode = 3U /* 0b11 */; amount = 0; break;
Dave Allison45fdb932014-06-25 12:37:10 -07001774 default:
1775 LOG(FATAL) << "Unsupported thumb2 shift opcode";
Vladimir Markoe8469c12014-11-26 18:09:30 +00001776 UNREACHABLE();
Dave Allison45fdb932014-06-25 12:37:10 -07001777 }
1778 // 32 bit.
1779 int32_t encoding = B31 | B30 | B29 | B27 | B25 | B22 |
Vladimir Marko73cf0fb2015-07-30 15:07:22 +01001780 0xf << 16 | (set_cc == kCcSet ? B20 : 0);
Dave Allison45fdb932014-06-25 12:37:10 -07001781 uint32_t imm3 = amount >> 2;
Andreas Gampec8ccf682014-09-29 20:07:43 -07001782 uint32_t imm2 = amount & 3U /* 0b11 */;
Dave Allison45fdb932014-06-25 12:37:10 -07001783 encoding |= imm3 << 12 | imm2 << 6 | static_cast<int16_t>(rm) |
1784 static_cast<int16_t>(rd) << 8 | opcode << 4;
1785 Emit32(encoding);
1786 } else {
1787 // 16 bit shift
1788 uint16_t opcode = 0;
1789 switch (shift) {
Andreas Gampec8ccf682014-09-29 20:07:43 -07001790 case LSL: opcode = 0U /* 0b00 */; break;
1791 case LSR: opcode = 1U /* 0b01 */; break;
1792 case ASR: opcode = 2U /* 0b10 */; break;
Dave Allison45fdb932014-06-25 12:37:10 -07001793 default:
Vladimir Markoe8469c12014-11-26 18:09:30 +00001794 LOG(FATAL) << "Unsupported thumb2 shift opcode";
1795 UNREACHABLE();
Dave Allison45fdb932014-06-25 12:37:10 -07001796 }
1797 int16_t encoding = opcode << 11 | amount << 6 | static_cast<int16_t>(rm) << 3 |
1798 static_cast<int16_t>(rd);
1799 Emit16(encoding);
1800 }
1801}
1802
Vladimir Marko73cf0fb2015-07-30 15:07:22 +01001803void Thumb2Assembler::EmitShift(Register rd,
1804 Register rn,
1805 Shift shift,
1806 Register rm,
1807 Condition cond,
1808 SetCc set_cc) {
Dave Allison45fdb932014-06-25 12:37:10 -07001809 CHECK_NE(shift, RRX);
1810 bool must_be_32bit = false;
Vladimir Marko73cf0fb2015-07-30 15:07:22 +01001811 if (IsHighRegister(rd) || IsHighRegister(rm) || IsHighRegister(rn) || rd != rn ||
1812 ((cond == AL) ? set_cc == kCcKeep : set_cc == kCcSet)) {
Dave Allison45fdb932014-06-25 12:37:10 -07001813 must_be_32bit = true;
1814 }
1815
1816 if (must_be_32bit) {
1817 uint16_t opcode = 0;
1818 switch (shift) {
Andreas Gampec8ccf682014-09-29 20:07:43 -07001819 case LSL: opcode = 0U /* 0b00 */; break;
1820 case LSR: opcode = 1U /* 0b01 */; break;
1821 case ASR: opcode = 2U /* 0b10 */; break;
1822 case ROR: opcode = 3U /* 0b11 */; break;
Dave Allison45fdb932014-06-25 12:37:10 -07001823 default:
1824 LOG(FATAL) << "Unsupported thumb2 shift opcode";
Vladimir Markoe8469c12014-11-26 18:09:30 +00001825 UNREACHABLE();
Dave Allison45fdb932014-06-25 12:37:10 -07001826 }
1827 // 32 bit.
1828 int32_t encoding = B31 | B30 | B29 | B28 | B27 | B25 |
Vladimir Marko73cf0fb2015-07-30 15:07:22 +01001829 0xf << 12 | (set_cc == kCcSet ? B20 : 0);
Dave Allison45fdb932014-06-25 12:37:10 -07001830 encoding |= static_cast<int16_t>(rn) << 16 | static_cast<int16_t>(rm) |
1831 static_cast<int16_t>(rd) << 8 | opcode << 21;
1832 Emit32(encoding);
1833 } else {
1834 uint16_t opcode = 0;
1835 switch (shift) {
Andreas Gampec8ccf682014-09-29 20:07:43 -07001836 case LSL: opcode = 2U /* 0b0010 */; break;
1837 case LSR: opcode = 3U /* 0b0011 */; break;
1838 case ASR: opcode = 4U /* 0b0100 */; break;
Vladimir Marko73cf0fb2015-07-30 15:07:22 +01001839 case ROR: opcode = 7U /* 0b0111 */; break;
Dave Allison45fdb932014-06-25 12:37:10 -07001840 default:
Vladimir Markoe8469c12014-11-26 18:09:30 +00001841 LOG(FATAL) << "Unsupported thumb2 shift opcode";
1842 UNREACHABLE();
Dave Allison45fdb932014-06-25 12:37:10 -07001843 }
1844 int16_t encoding = B14 | opcode << 6 | static_cast<int16_t>(rm) << 3 |
1845 static_cast<int16_t>(rd);
1846 Emit16(encoding);
1847 }
1848}
1849
Vladimir Markocf93a5c2015-06-16 11:33:24 +00001850inline size_t Thumb2Assembler::Fixup::SizeInBytes(Size size) {
1851 switch (size) {
1852 case kBranch16Bit:
1853 return 2u;
1854 case kBranch32Bit:
1855 return 4u;
Dave Allison45fdb932014-06-25 12:37:10 -07001856
Vladimir Markocf93a5c2015-06-16 11:33:24 +00001857 case kCbxz16Bit:
1858 return 2u;
1859 case kCbxz32Bit:
1860 return 4u;
1861 case kCbxz48Bit:
1862 return 6u;
Dave Allison65fcc2c2014-04-28 13:45:27 -07001863
Vladimir Markocf93a5c2015-06-16 11:33:24 +00001864 case kLiteral1KiB:
1865 return 2u;
1866 case kLiteral4KiB:
1867 return 4u;
1868 case kLiteral64KiB:
1869 return 8u;
1870 case kLiteral1MiB:
1871 return 10u;
1872 case kLiteralFar:
1873 return 14u;
Dave Allison65fcc2c2014-04-28 13:45:27 -07001874
Andreas Gampe7cffc3b2015-10-19 21:31:53 -07001875 case kLiteralAddr1KiB:
1876 return 2u;
1877 case kLiteralAddr4KiB:
1878 return 4u;
1879 case kLiteralAddr64KiB:
1880 return 6u;
1881 case kLiteralAddrFar:
1882 return 10u;
1883
Vladimir Markocf93a5c2015-06-16 11:33:24 +00001884 case kLongOrFPLiteral1KiB:
1885 return 4u;
1886 case kLongOrFPLiteral256KiB:
1887 return 10u;
1888 case kLongOrFPLiteralFar:
1889 return 14u;
1890 }
1891 LOG(FATAL) << "Unexpected size: " << static_cast<int>(size);
1892 UNREACHABLE();
1893}
1894
1895inline uint32_t Thumb2Assembler::Fixup::GetOriginalSizeInBytes() const {
1896 return SizeInBytes(original_size_);
1897}
1898
1899inline uint32_t Thumb2Assembler::Fixup::GetSizeInBytes() const {
1900 return SizeInBytes(size_);
1901}
1902
1903inline size_t Thumb2Assembler::Fixup::LiteralPoolPaddingSize(uint32_t current_code_size) {
1904 // The code size must be a multiple of 2.
Roland Levillain14d90572015-07-16 10:52:26 +01001905 DCHECK_ALIGNED(current_code_size, 2);
Vladimir Markocf93a5c2015-06-16 11:33:24 +00001906 // If it isn't a multiple of 4, we need to add a 2-byte padding before the literal pool.
1907 return current_code_size & 2;
1908}
1909
1910inline int32_t Thumb2Assembler::Fixup::GetOffset(uint32_t current_code_size) const {
1911 static constexpr int32_t int32_min = std::numeric_limits<int32_t>::min();
1912 static constexpr int32_t int32_max = std::numeric_limits<int32_t>::max();
1913 DCHECK_LE(target_, static_cast<uint32_t>(int32_max));
1914 DCHECK_LE(location_, static_cast<uint32_t>(int32_max));
1915 DCHECK_LE(adjustment_, static_cast<uint32_t>(int32_max));
1916 int32_t diff = static_cast<int32_t>(target_) - static_cast<int32_t>(location_);
1917 if (target_ > location_) {
1918 DCHECK_LE(adjustment_, static_cast<uint32_t>(int32_max - diff));
1919 diff += static_cast<int32_t>(adjustment_);
Dave Allison65fcc2c2014-04-28 13:45:27 -07001920 } else {
Vladimir Markocf93a5c2015-06-16 11:33:24 +00001921 DCHECK_LE(int32_min + static_cast<int32_t>(adjustment_), diff);
1922 diff -= static_cast<int32_t>(adjustment_);
1923 }
1924 // The default PC adjustment for Thumb2 is 4 bytes.
1925 DCHECK_GE(diff, int32_min + 4);
1926 diff -= 4;
1927 // Add additional adjustment for instructions preceding the PC usage, padding
1928 // before the literal pool and rounding down the PC for literal loads.
1929 switch (GetSize()) {
1930 case kBranch16Bit:
1931 case kBranch32Bit:
1932 break;
1933
1934 case kCbxz16Bit:
1935 break;
1936 case kCbxz32Bit:
1937 case kCbxz48Bit:
1938 DCHECK_GE(diff, int32_min + 2);
1939 diff -= 2; // Extra CMP Rn, #0, 16-bit.
1940 break;
1941
1942 case kLiteral1KiB:
1943 case kLiteral4KiB:
1944 case kLongOrFPLiteral1KiB:
Andreas Gampe7cffc3b2015-10-19 21:31:53 -07001945 case kLiteralAddr1KiB:
1946 case kLiteralAddr4KiB:
Vladimir Markocf93a5c2015-06-16 11:33:24 +00001947 DCHECK(diff >= 0 || (GetSize() == kLiteral1KiB && diff == -2));
1948 diff += LiteralPoolPaddingSize(current_code_size);
1949 // Load literal instructions round down the PC+4 to a multiple of 4, so if the PC
1950 // isn't a multiple of 2, we need to adjust. Since we already adjusted for the target
1951 // being aligned, current PC alignment can be inferred from diff.
Roland Levillain14d90572015-07-16 10:52:26 +01001952 DCHECK_ALIGNED(diff, 2);
Vladimir Markocf93a5c2015-06-16 11:33:24 +00001953 diff = diff + (diff & 2);
1954 DCHECK_GE(diff, 0);
1955 break;
1956 case kLiteral1MiB:
1957 case kLiteral64KiB:
1958 case kLongOrFPLiteral256KiB:
Andreas Gampe7cffc3b2015-10-19 21:31:53 -07001959 case kLiteralAddr64KiB:
Vladimir Markocf93a5c2015-06-16 11:33:24 +00001960 DCHECK_GE(diff, 4); // The target must be at least 4 bytes after the ADD rX, PC.
1961 diff -= 4; // One extra 32-bit MOV.
1962 diff += LiteralPoolPaddingSize(current_code_size);
1963 break;
1964 case kLiteralFar:
1965 case kLongOrFPLiteralFar:
Andreas Gampe7cffc3b2015-10-19 21:31:53 -07001966 case kLiteralAddrFar:
Vladimir Markocf93a5c2015-06-16 11:33:24 +00001967 DCHECK_GE(diff, 8); // The target must be at least 4 bytes after the ADD rX, PC.
1968 diff -= 8; // Extra MOVW+MOVT; both 32-bit.
1969 diff += LiteralPoolPaddingSize(current_code_size);
1970 break;
1971 }
1972 return diff;
1973}
1974
1975inline size_t Thumb2Assembler::Fixup::IncreaseSize(Size new_size) {
1976 DCHECK_NE(target_, kUnresolved);
1977 Size old_size = size_;
1978 size_ = new_size;
1979 DCHECK_GT(SizeInBytes(new_size), SizeInBytes(old_size));
1980 size_t adjustment = SizeInBytes(new_size) - SizeInBytes(old_size);
1981 if (target_ > location_) {
1982 adjustment_ += adjustment;
1983 }
1984 return adjustment;
1985}
1986
1987uint32_t Thumb2Assembler::Fixup::AdjustSizeIfNeeded(uint32_t current_code_size) {
1988 uint32_t old_code_size = current_code_size;
1989 switch (GetSize()) {
1990 case kBranch16Bit:
1991 if (IsInt(cond_ != AL ? 9 : 12, GetOffset(current_code_size))) {
1992 break;
Vladimir Markof38caa62015-05-29 15:50:18 +01001993 }
Vladimir Markocf93a5c2015-06-16 11:33:24 +00001994 current_code_size += IncreaseSize(kBranch32Bit);
1995 FALLTHROUGH_INTENDED;
1996 case kBranch32Bit:
1997 // We don't support conditional branches beyond +-1MiB
1998 // or unconditional branches beyond +-16MiB.
1999 break;
2000
2001 case kCbxz16Bit:
2002 if (IsUint<7>(GetOffset(current_code_size))) {
2003 break;
2004 }
2005 current_code_size += IncreaseSize(kCbxz32Bit);
2006 FALLTHROUGH_INTENDED;
2007 case kCbxz32Bit:
2008 if (IsInt<9>(GetOffset(current_code_size))) {
2009 break;
2010 }
2011 current_code_size += IncreaseSize(kCbxz48Bit);
2012 FALLTHROUGH_INTENDED;
2013 case kCbxz48Bit:
2014 // We don't support conditional branches beyond +-1MiB.
2015 break;
2016
2017 case kLiteral1KiB:
2018 DCHECK(!IsHighRegister(rn_));
2019 if (IsUint<10>(GetOffset(current_code_size))) {
2020 break;
2021 }
2022 current_code_size += IncreaseSize(kLiteral4KiB);
2023 FALLTHROUGH_INTENDED;
2024 case kLiteral4KiB:
2025 if (IsUint<12>(GetOffset(current_code_size))) {
2026 break;
2027 }
2028 current_code_size += IncreaseSize(kLiteral64KiB);
2029 FALLTHROUGH_INTENDED;
2030 case kLiteral64KiB:
2031 // Can't handle high register which we can encounter by fall-through from kLiteral4KiB.
2032 if (!IsHighRegister(rn_) && IsUint<16>(GetOffset(current_code_size))) {
2033 break;
2034 }
2035 current_code_size += IncreaseSize(kLiteral1MiB);
2036 FALLTHROUGH_INTENDED;
2037 case kLiteral1MiB:
2038 if (IsUint<20>(GetOffset(current_code_size))) {
2039 break;
2040 }
2041 current_code_size += IncreaseSize(kLiteralFar);
2042 FALLTHROUGH_INTENDED;
2043 case kLiteralFar:
2044 // This encoding can reach any target.
2045 break;
2046
Andreas Gampe7cffc3b2015-10-19 21:31:53 -07002047 case kLiteralAddr1KiB:
2048 DCHECK(!IsHighRegister(rn_));
2049 if (IsUint<10>(GetOffset(current_code_size))) {
2050 break;
2051 }
2052 current_code_size += IncreaseSize(kLiteralAddr4KiB);
2053 FALLTHROUGH_INTENDED;
2054 case kLiteralAddr4KiB:
2055 if (IsUint<12>(GetOffset(current_code_size))) {
2056 break;
2057 }
2058 current_code_size += IncreaseSize(kLiteralAddr64KiB);
2059 FALLTHROUGH_INTENDED;
2060 case kLiteralAddr64KiB:
2061 if (IsUint<16>(GetOffset(current_code_size))) {
2062 break;
2063 }
2064 current_code_size += IncreaseSize(kLiteralAddrFar);
2065 FALLTHROUGH_INTENDED;
2066 case kLiteralAddrFar:
2067 // This encoding can reach any target.
2068 break;
2069
Vladimir Markocf93a5c2015-06-16 11:33:24 +00002070 case kLongOrFPLiteral1KiB:
2071 if (IsUint<10>(GetOffset(current_code_size))) {
2072 break;
2073 }
2074 current_code_size += IncreaseSize(kLongOrFPLiteral256KiB);
2075 FALLTHROUGH_INTENDED;
2076 case kLongOrFPLiteral256KiB:
2077 if (IsUint<18>(GetOffset(current_code_size))) {
2078 break;
2079 }
2080 current_code_size += IncreaseSize(kLongOrFPLiteralFar);
2081 FALLTHROUGH_INTENDED;
2082 case kLongOrFPLiteralFar:
2083 // This encoding can reach any target.
2084 break;
2085 }
2086 return current_code_size - old_code_size;
2087}
2088
2089void Thumb2Assembler::Fixup::Emit(AssemblerBuffer* buffer, uint32_t code_size) const {
2090 switch (GetSize()) {
2091 case kBranch16Bit: {
2092 DCHECK(type_ == kUnconditional || type_ == kConditional);
2093 DCHECK_EQ(type_ == kConditional, cond_ != AL);
2094 int16_t encoding = BEncoding16(GetOffset(code_size), cond_);
Vladimir Markof38caa62015-05-29 15:50:18 +01002095 buffer->Store<int16_t>(location_, encoding);
Vladimir Markocf93a5c2015-06-16 11:33:24 +00002096 break;
2097 }
2098 case kBranch32Bit: {
2099 DCHECK(type_ == kConditional || type_ == kUnconditional ||
2100 type_ == kUnconditionalLink || type_ == kUnconditionalLinkX);
2101 DCHECK_EQ(type_ == kConditional, cond_ != AL);
2102 int32_t encoding = BEncoding32(GetOffset(code_size), cond_);
2103 if (type_ == kUnconditionalLink) {
2104 DCHECK_NE(encoding & B12, 0);
2105 encoding |= B14;
2106 } else if (type_ == kUnconditionalLinkX) {
2107 DCHECK_NE(encoding & B12, 0);
2108 encoding ^= B14 | B12;
2109 }
2110 buffer->Store<int16_t>(location_, encoding >> 16);
2111 buffer->Store<int16_t>(location_ + 2u, static_cast<int16_t>(encoding & 0xffff));
2112 break;
2113 }
2114
2115 case kCbxz16Bit: {
2116 DCHECK(type_ == kCompareAndBranchXZero);
2117 int16_t encoding = CbxzEncoding16(rn_, GetOffset(code_size), cond_);
2118 buffer->Store<int16_t>(location_, encoding);
2119 break;
2120 }
2121 case kCbxz32Bit: {
2122 DCHECK(type_ == kCompareAndBranchXZero);
2123 DCHECK(cond_ == EQ || cond_ == NE);
2124 int16_t cmp_encoding = CmpRnImm8Encoding16(rn_, 0);
2125 int16_t b_encoding = BEncoding16(GetOffset(code_size), cond_);
2126 buffer->Store<int16_t>(location_, cmp_encoding);
2127 buffer->Store<int16_t>(location_ + 2, b_encoding);
2128 break;
2129 }
2130 case kCbxz48Bit: {
2131 DCHECK(type_ == kCompareAndBranchXZero);
2132 DCHECK(cond_ == EQ || cond_ == NE);
2133 int16_t cmp_encoding = CmpRnImm8Encoding16(rn_, 0);
2134 int32_t b_encoding = BEncoding32(GetOffset(code_size), cond_);
2135 buffer->Store<int16_t>(location_, cmp_encoding);
2136 buffer->Store<int16_t>(location_ + 2u, b_encoding >> 16);
2137 buffer->Store<int16_t>(location_ + 4u, static_cast<int16_t>(b_encoding & 0xffff));
2138 break;
2139 }
2140
2141 case kLiteral1KiB: {
2142 DCHECK(type_ == kLoadLiteralNarrow);
2143 int16_t encoding = LdrLitEncoding16(rn_, GetOffset(code_size));
2144 buffer->Store<int16_t>(location_, encoding);
2145 break;
2146 }
2147 case kLiteral4KiB: {
2148 DCHECK(type_ == kLoadLiteralNarrow);
2149 // GetOffset() uses PC+4 but load literal uses AlignDown(PC+4, 4). Adjust offset accordingly.
2150 int32_t encoding = LdrLitEncoding32(rn_, GetOffset(code_size));
2151 buffer->Store<int16_t>(location_, encoding >> 16);
2152 buffer->Store<int16_t>(location_ + 2u, static_cast<int16_t>(encoding & 0xffff));
2153 break;
2154 }
2155 case kLiteral64KiB: {
2156 DCHECK(type_ == kLoadLiteralNarrow);
2157 int32_t mov_encoding = MovwEncoding32(rn_, GetOffset(code_size));
2158 int16_t add_pc_encoding = AddRdnRmEncoding16(rn_, PC);
2159 int16_t ldr_encoding = LdrRtRnImm5Encoding16(rn_, rn_, 0);
2160 buffer->Store<int16_t>(location_, mov_encoding >> 16);
2161 buffer->Store<int16_t>(location_ + 2u, static_cast<int16_t>(mov_encoding & 0xffff));
2162 buffer->Store<int16_t>(location_ + 4u, add_pc_encoding);
2163 buffer->Store<int16_t>(location_ + 6u, ldr_encoding);
2164 break;
2165 }
2166 case kLiteral1MiB: {
2167 DCHECK(type_ == kLoadLiteralNarrow);
2168 int32_t offset = GetOffset(code_size);
2169 int32_t mov_encoding = MovModImmEncoding32(rn_, offset & ~0xfff);
2170 int16_t add_pc_encoding = AddRdnRmEncoding16(rn_, PC);
2171 int32_t ldr_encoding = LdrRtRnImm12Encoding(rn_, rn_, offset & 0xfff);
2172 buffer->Store<int16_t>(location_, mov_encoding >> 16);
2173 buffer->Store<int16_t>(location_ + 2u, static_cast<int16_t>(mov_encoding & 0xffff));
2174 buffer->Store<int16_t>(location_ + 4u, add_pc_encoding);
2175 buffer->Store<int16_t>(location_ + 6u, ldr_encoding >> 16);
2176 buffer->Store<int16_t>(location_ + 8u, static_cast<int16_t>(ldr_encoding & 0xffff));
2177 break;
2178 }
2179 case kLiteralFar: {
2180 DCHECK(type_ == kLoadLiteralNarrow);
2181 int32_t offset = GetOffset(code_size);
2182 int32_t movw_encoding = MovwEncoding32(rn_, offset & 0xffff);
2183 int32_t movt_encoding = MovtEncoding32(rn_, offset & ~0xffff);
2184 int16_t add_pc_encoding = AddRdnRmEncoding16(rn_, PC);
2185 int32_t ldr_encoding = LdrRtRnImm12Encoding(rn_, rn_, 0);
2186 buffer->Store<int16_t>(location_, movw_encoding >> 16);
2187 buffer->Store<int16_t>(location_ + 2u, static_cast<int16_t>(movw_encoding & 0xffff));
2188 buffer->Store<int16_t>(location_ + 4u, movt_encoding >> 16);
2189 buffer->Store<int16_t>(location_ + 6u, static_cast<int16_t>(movt_encoding & 0xffff));
2190 buffer->Store<int16_t>(location_ + 8u, add_pc_encoding);
2191 buffer->Store<int16_t>(location_ + 10u, ldr_encoding >> 16);
2192 buffer->Store<int16_t>(location_ + 12u, static_cast<int16_t>(ldr_encoding & 0xffff));
2193 break;
2194 }
2195
Andreas Gampe7cffc3b2015-10-19 21:31:53 -07002196 case kLiteralAddr1KiB: {
2197 DCHECK(type_ == kLoadLiteralAddr);
2198 int16_t encoding = AdrEncoding16(rn_, GetOffset(code_size));
2199 buffer->Store<int16_t>(location_, encoding);
2200 break;
2201 }
2202 case kLiteralAddr4KiB: {
2203 DCHECK(type_ == kLoadLiteralAddr);
2204 int32_t encoding = AdrEncoding32(rn_, GetOffset(code_size));
2205 buffer->Store<int16_t>(location_, encoding >> 16);
2206 buffer->Store<int16_t>(location_ + 2u, static_cast<int16_t>(encoding & 0xffff));
2207 break;
2208 }
2209 case kLiteralAddr64KiB: {
2210 DCHECK(type_ == kLoadLiteralAddr);
2211 int32_t mov_encoding = MovwEncoding32(rn_, GetOffset(code_size));
2212 int16_t add_pc_encoding = AddRdnRmEncoding16(rn_, PC);
2213 buffer->Store<int16_t>(location_, mov_encoding >> 16);
2214 buffer->Store<int16_t>(location_ + 2u, static_cast<int16_t>(mov_encoding & 0xffff));
2215 buffer->Store<int16_t>(location_ + 4u, add_pc_encoding);
2216 break;
2217 }
2218 case kLiteralAddrFar: {
2219 DCHECK(type_ == kLoadLiteralAddr);
2220 int32_t offset = GetOffset(code_size);
2221 int32_t movw_encoding = MovwEncoding32(rn_, offset & 0xffff);
2222 int32_t movt_encoding = MovtEncoding32(rn_, offset & ~0xffff);
2223 int16_t add_pc_encoding = AddRdnRmEncoding16(rn_, PC);
2224 buffer->Store<int16_t>(location_, movw_encoding >> 16);
2225 buffer->Store<int16_t>(location_ + 2u, static_cast<int16_t>(movw_encoding & 0xffff));
2226 buffer->Store<int16_t>(location_ + 4u, movt_encoding >> 16);
2227 buffer->Store<int16_t>(location_ + 6u, static_cast<int16_t>(movt_encoding & 0xffff));
2228 buffer->Store<int16_t>(location_ + 8u, add_pc_encoding);
2229 break;
2230 }
2231
Vladimir Markocf93a5c2015-06-16 11:33:24 +00002232 case kLongOrFPLiteral1KiB: {
2233 int32_t encoding = LoadWideOrFpEncoding(PC, GetOffset(code_size)); // DCHECKs type_.
2234 buffer->Store<int16_t>(location_, encoding >> 16);
2235 buffer->Store<int16_t>(location_ + 2u, static_cast<int16_t>(encoding & 0xffff));
2236 break;
2237 }
2238 case kLongOrFPLiteral256KiB: {
2239 int32_t offset = GetOffset(code_size);
2240 int32_t mov_encoding = MovModImmEncoding32(IP, offset & ~0x3ff);
2241 int16_t add_pc_encoding = AddRdnRmEncoding16(IP, PC);
2242 int32_t ldr_encoding = LoadWideOrFpEncoding(IP, offset & 0x3ff); // DCHECKs type_.
2243 buffer->Store<int16_t>(location_, mov_encoding >> 16);
2244 buffer->Store<int16_t>(location_ + 2u, static_cast<int16_t>(mov_encoding & 0xffff));
2245 buffer->Store<int16_t>(location_ + 4u, add_pc_encoding);
2246 buffer->Store<int16_t>(location_ + 6u, ldr_encoding >> 16);
2247 buffer->Store<int16_t>(location_ + 8u, static_cast<int16_t>(ldr_encoding & 0xffff));
2248 break;
2249 }
2250 case kLongOrFPLiteralFar: {
2251 int32_t offset = GetOffset(code_size);
2252 int32_t movw_encoding = MovwEncoding32(IP, offset & 0xffff);
2253 int32_t movt_encoding = MovtEncoding32(IP, offset & ~0xffff);
2254 int16_t add_pc_encoding = AddRdnRmEncoding16(IP, PC);
2255 int32_t ldr_encoding = LoadWideOrFpEncoding(IP, 0); // DCHECKs type_.
2256 buffer->Store<int16_t>(location_, movw_encoding >> 16);
2257 buffer->Store<int16_t>(location_ + 2u, static_cast<int16_t>(movw_encoding & 0xffff));
2258 buffer->Store<int16_t>(location_ + 4u, movt_encoding >> 16);
2259 buffer->Store<int16_t>(location_ + 6u, static_cast<int16_t>(movt_encoding & 0xffff));
2260 buffer->Store<int16_t>(location_ + 8u, add_pc_encoding);
2261 buffer->Store<int16_t>(location_ + 10u, ldr_encoding >> 16);
2262 buffer->Store<int16_t>(location_ + 12u, static_cast<int16_t>(ldr_encoding & 0xffff));
2263 break;
Dave Allison65fcc2c2014-04-28 13:45:27 -07002264 }
2265 }
2266}
2267
Dave Allison65fcc2c2014-04-28 13:45:27 -07002268uint16_t Thumb2Assembler::EmitCompareAndBranch(Register rn, uint16_t prev, bool n) {
Nicolas Geoffrayd56376c2015-05-21 12:32:34 +00002269 CHECK(IsLowRegister(rn));
Dave Allison65fcc2c2014-04-28 13:45:27 -07002270 uint32_t location = buffer_.Size();
2271
2272 // This is always unresolved as it must be a forward branch.
2273 Emit16(prev); // Previous link.
Vladimir Markocf93a5c2015-06-16 11:33:24 +00002274 return AddFixup(Fixup::CompareAndBranch(location, rn, n ? NE : EQ));
Dave Allison65fcc2c2014-04-28 13:45:27 -07002275}
2276
2277
2278// NOTE: this only support immediate offsets, not [rx,ry].
2279// TODO: support [rx,ry] instructions.
2280void Thumb2Assembler::EmitLoadStore(Condition cond,
2281 bool load,
2282 bool byte,
2283 bool half,
2284 bool is_signed,
2285 Register rd,
2286 const Address& ad) {
2287 CHECK_NE(rd, kNoRegister);
2288 CheckCondition(cond);
2289 bool must_be_32bit = force_32bit_;
2290 if (IsHighRegister(rd)) {
2291 must_be_32bit = true;
2292 }
2293
2294 Register rn = ad.GetRegister();
Dave Allison45fdb932014-06-25 12:37:10 -07002295 if (IsHighRegister(rn) && rn != SP && rn != PC) {
Dave Allison65fcc2c2014-04-28 13:45:27 -07002296 must_be_32bit = true;
2297 }
2298
2299 if (is_signed || ad.GetOffset() < 0 || ad.GetMode() != Address::Offset) {
2300 must_be_32bit = true;
2301 }
2302
Dave Allison45fdb932014-06-25 12:37:10 -07002303 if (ad.IsImmediate()) {
2304 // Immediate offset
2305 int32_t offset = ad.GetOffset();
Dave Allison65fcc2c2014-04-28 13:45:27 -07002306
Dave Allison45fdb932014-06-25 12:37:10 -07002307 // The 16 bit SP relative instruction can only have a 10 bit offset.
Dave Allison0bb9ade2014-06-26 17:57:36 -07002308 if (rn == SP && offset >= (1 << 10)) {
Dave Allison65fcc2c2014-04-28 13:45:27 -07002309 must_be_32bit = true;
2310 }
Dave Allison65fcc2c2014-04-28 13:45:27 -07002311
2312 if (byte) {
Dave Allison45fdb932014-06-25 12:37:10 -07002313 // 5 bit offset, no shift.
Dave Allison0bb9ade2014-06-26 17:57:36 -07002314 if (offset >= (1 << 5)) {
Dave Allison45fdb932014-06-25 12:37:10 -07002315 must_be_32bit = true;
2316 }
Dave Allison65fcc2c2014-04-28 13:45:27 -07002317 } else if (half) {
Dave Allison45fdb932014-06-25 12:37:10 -07002318 // 6 bit offset, shifted by 1.
Dave Allison0bb9ade2014-06-26 17:57:36 -07002319 if (offset >= (1 << 6)) {
Dave Allison45fdb932014-06-25 12:37:10 -07002320 must_be_32bit = true;
2321 }
Dave Allison65fcc2c2014-04-28 13:45:27 -07002322 } else {
Dave Allison45fdb932014-06-25 12:37:10 -07002323 // 7 bit offset, shifted by 2.
Dave Allison0bb9ade2014-06-26 17:57:36 -07002324 if (offset >= (1 << 7)) {
Dave Allison45fdb932014-06-25 12:37:10 -07002325 must_be_32bit = true;
Dave Allison65fcc2c2014-04-28 13:45:27 -07002326 }
2327 }
Dave Allison65fcc2c2014-04-28 13:45:27 -07002328
Dave Allison45fdb932014-06-25 12:37:10 -07002329 if (must_be_32bit) {
2330 int32_t encoding = B31 | B30 | B29 | B28 | B27 |
2331 (load ? B20 : 0) |
2332 (is_signed ? B24 : 0) |
2333 static_cast<uint32_t>(rd) << 12 |
2334 ad.encodingThumb(true) |
2335 (byte ? 0 : half ? B21 : B22);
2336 Emit32(encoding);
Dave Allison65fcc2c2014-04-28 13:45:27 -07002337 } else {
Dave Allison45fdb932014-06-25 12:37:10 -07002338 // 16 bit thumb1.
2339 uint8_t opA = 0;
2340 bool sp_relative = false;
Dave Allison65fcc2c2014-04-28 13:45:27 -07002341
2342 if (byte) {
Andreas Gampec8ccf682014-09-29 20:07:43 -07002343 opA = 7U /* 0b0111 */;
Dave Allison65fcc2c2014-04-28 13:45:27 -07002344 } else if (half) {
Andreas Gampec8ccf682014-09-29 20:07:43 -07002345 opA = 8U /* 0b1000 */;
Dave Allison65fcc2c2014-04-28 13:45:27 -07002346 } else {
Dave Allison45fdb932014-06-25 12:37:10 -07002347 if (rn == SP) {
Andreas Gampec8ccf682014-09-29 20:07:43 -07002348 opA = 9U /* 0b1001 */;
Dave Allison45fdb932014-06-25 12:37:10 -07002349 sp_relative = true;
2350 } else {
Andreas Gampec8ccf682014-09-29 20:07:43 -07002351 opA = 6U /* 0b0110 */;
Dave Allison45fdb932014-06-25 12:37:10 -07002352 }
Dave Allison65fcc2c2014-04-28 13:45:27 -07002353 }
Dave Allison45fdb932014-06-25 12:37:10 -07002354 int16_t encoding = opA << 12 |
2355 (load ? B11 : 0);
Dave Allison65fcc2c2014-04-28 13:45:27 -07002356
Dave Allison45fdb932014-06-25 12:37:10 -07002357 CHECK_GE(offset, 0);
2358 if (sp_relative) {
2359 // SP relative, 10 bit offset.
Dave Allison0bb9ade2014-06-26 17:57:36 -07002360 CHECK_LT(offset, (1 << 10));
Roland Levillain14d90572015-07-16 10:52:26 +01002361 CHECK_ALIGNED(offset, 4);
Dave Allison45fdb932014-06-25 12:37:10 -07002362 encoding |= rd << 8 | offset >> 2;
2363 } else {
2364 // No SP relative. The offset is shifted right depending on
2365 // the size of the load/store.
2366 encoding |= static_cast<uint32_t>(rd);
2367
2368 if (byte) {
2369 // 5 bit offset, no shift.
Dave Allison0bb9ade2014-06-26 17:57:36 -07002370 CHECK_LT(offset, (1 << 5));
Dave Allison45fdb932014-06-25 12:37:10 -07002371 } else if (half) {
2372 // 6 bit offset, shifted by 1.
Dave Allison0bb9ade2014-06-26 17:57:36 -07002373 CHECK_LT(offset, (1 << 6));
Roland Levillain14d90572015-07-16 10:52:26 +01002374 CHECK_ALIGNED(offset, 2);
Dave Allison45fdb932014-06-25 12:37:10 -07002375 offset >>= 1;
2376 } else {
2377 // 7 bit offset, shifted by 2.
Dave Allison0bb9ade2014-06-26 17:57:36 -07002378 CHECK_LT(offset, (1 << 7));
Roland Levillain14d90572015-07-16 10:52:26 +01002379 CHECK_ALIGNED(offset, 4);
Dave Allison45fdb932014-06-25 12:37:10 -07002380 offset >>= 2;
2381 }
2382 encoding |= rn << 3 | offset << 6;
2383 }
2384
2385 Emit16(encoding);
2386 }
2387 } else {
2388 // Register shift.
2389 if (ad.GetRegister() == PC) {
2390 // PC relative literal encoding.
2391 int32_t offset = ad.GetOffset();
Dave Allison0bb9ade2014-06-26 17:57:36 -07002392 if (must_be_32bit || offset < 0 || offset >= (1 << 10) || !load) {
Dave Allison45fdb932014-06-25 12:37:10 -07002393 int32_t up = B23;
2394 if (offset < 0) {
2395 offset = -offset;
2396 up = 0;
2397 }
2398 CHECK_LT(offset, (1 << 12));
2399 int32_t encoding = 0x1f << 27 | 0xf << 16 | B22 | (load ? B20 : 0) |
2400 offset | up |
2401 static_cast<uint32_t>(rd) << 12;
2402 Emit32(encoding);
2403 } else {
2404 // 16 bit literal load.
2405 CHECK_GE(offset, 0);
2406 CHECK_LT(offset, (1 << 10));
2407 int32_t encoding = B14 | (load ? B11 : 0) | static_cast<uint32_t>(rd) << 8 | offset >> 2;
2408 Emit16(encoding);
2409 }
2410 } else {
2411 if (ad.GetShiftCount() != 0) {
2412 // If there is a shift count this must be 32 bit.
2413 must_be_32bit = true;
2414 } else if (IsHighRegister(ad.GetRegisterOffset())) {
2415 must_be_32bit = true;
2416 }
2417
2418 if (must_be_32bit) {
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01002419 int32_t encoding = 0x1f << 27 | (load ? B20 : 0) | static_cast<uint32_t>(rd) << 12 |
Dave Allison45fdb932014-06-25 12:37:10 -07002420 ad.encodingThumb(true);
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01002421 if (half) {
2422 encoding |= B21;
2423 } else if (!byte) {
2424 encoding |= B22;
2425 }
Dave Allison45fdb932014-06-25 12:37:10 -07002426 Emit32(encoding);
2427 } else {
2428 // 16 bit register offset.
2429 int32_t encoding = B14 | B12 | (load ? B11 : 0) | static_cast<uint32_t>(rd) |
2430 ad.encodingThumb(false);
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01002431 if (byte) {
2432 encoding |= B10;
2433 } else if (half) {
2434 encoding |= B9;
2435 }
Dave Allison45fdb932014-06-25 12:37:10 -07002436 Emit16(encoding);
2437 }
2438 }
Dave Allison65fcc2c2014-04-28 13:45:27 -07002439 }
2440}
2441
2442
2443void Thumb2Assembler::EmitMultiMemOp(Condition cond,
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002444 BlockAddressMode bam,
Dave Allison65fcc2c2014-04-28 13:45:27 -07002445 bool load,
2446 Register base,
2447 RegList regs) {
2448 CHECK_NE(base, kNoRegister);
2449 CheckCondition(cond);
2450 bool must_be_32bit = force_32bit_;
2451
Vladimir Markoe8469c12014-11-26 18:09:30 +00002452 if (!must_be_32bit && base == SP && bam == (load ? IA_W : DB_W) &&
2453 (regs & 0xff00 & ~(1 << (load ? PC : LR))) == 0) {
2454 // Use 16-bit PUSH/POP.
2455 int16_t encoding = B15 | B13 | B12 | (load ? B11 : 0) | B10 |
2456 ((regs & (1 << (load ? PC : LR))) != 0 ? B8 : 0) | (regs & 0x00ff);
2457 Emit16(encoding);
2458 return;
2459 }
2460
Dave Allison65fcc2c2014-04-28 13:45:27 -07002461 if ((regs & 0xff00) != 0) {
2462 must_be_32bit = true;
2463 }
2464
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002465 bool w_bit = bam == IA_W || bam == DB_W || bam == DA_W || bam == IB_W;
Dave Allison65fcc2c2014-04-28 13:45:27 -07002466 // 16 bit always uses writeback.
2467 if (!w_bit) {
2468 must_be_32bit = true;
2469 }
2470
2471 if (must_be_32bit) {
2472 uint32_t op = 0;
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002473 switch (bam) {
Dave Allison65fcc2c2014-04-28 13:45:27 -07002474 case IA:
2475 case IA_W:
Andreas Gampec8ccf682014-09-29 20:07:43 -07002476 op = 1U /* 0b01 */;
Dave Allison65fcc2c2014-04-28 13:45:27 -07002477 break;
2478 case DB:
2479 case DB_W:
Andreas Gampec8ccf682014-09-29 20:07:43 -07002480 op = 2U /* 0b10 */;
Dave Allison65fcc2c2014-04-28 13:45:27 -07002481 break;
2482 case DA:
2483 case IB:
2484 case DA_W:
2485 case IB_W:
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002486 LOG(FATAL) << "LDM/STM mode not supported on thumb: " << bam;
Vladimir Markoe8469c12014-11-26 18:09:30 +00002487 UNREACHABLE();
Dave Allison65fcc2c2014-04-28 13:45:27 -07002488 }
2489 if (load) {
2490 // Cannot have SP in the list.
2491 CHECK_EQ((regs & (1 << SP)), 0);
2492 } else {
2493 // Cannot have PC or SP in the list.
2494 CHECK_EQ((regs & (1 << PC | 1 << SP)), 0);
2495 }
2496 int32_t encoding = B31 | B30 | B29 | B27 |
2497 (op << 23) |
2498 (load ? B20 : 0) |
2499 base << 16 |
2500 regs |
2501 (w_bit << 21);
2502 Emit32(encoding);
2503 } else {
2504 int16_t encoding = B15 | B14 |
2505 (load ? B11 : 0) |
2506 base << 8 |
2507 regs;
2508 Emit16(encoding);
2509 }
2510}
2511
Vladimir Markocf93a5c2015-06-16 11:33:24 +00002512void Thumb2Assembler::EmitBranch(Condition cond, Label* label, bool link, bool x) {
2513 bool use32bit = IsForced32Bit() || !CanRelocateBranches();
Dave Allison65fcc2c2014-04-28 13:45:27 -07002514 uint32_t pc = buffer_.Size();
Vladimir Markocf93a5c2015-06-16 11:33:24 +00002515 Fixup::Type branch_type;
Dave Allison65fcc2c2014-04-28 13:45:27 -07002516 if (cond == AL) {
2517 if (link) {
Vladimir Markocf93a5c2015-06-16 11:33:24 +00002518 use32bit = true;
Dave Allison65fcc2c2014-04-28 13:45:27 -07002519 if (x) {
Vladimir Markocf93a5c2015-06-16 11:33:24 +00002520 branch_type = Fixup::kUnconditionalLinkX; // BLX.
Dave Allison65fcc2c2014-04-28 13:45:27 -07002521 } else {
Vladimir Markocf93a5c2015-06-16 11:33:24 +00002522 branch_type = Fixup::kUnconditionalLink; // BX.
Dave Allison65fcc2c2014-04-28 13:45:27 -07002523 }
2524 } else {
Vladimir Markocf93a5c2015-06-16 11:33:24 +00002525 branch_type = Fixup::kUnconditional; // B.
Dave Allison65fcc2c2014-04-28 13:45:27 -07002526 }
2527 } else {
Vladimir Markocf93a5c2015-06-16 11:33:24 +00002528 branch_type = Fixup::kConditional; // B<cond>.
Dave Allison65fcc2c2014-04-28 13:45:27 -07002529 }
2530
Vladimir Markocf93a5c2015-06-16 11:33:24 +00002531 Fixup::Size size = use32bit ? Fixup::kBranch32Bit : Fixup::kBranch16Bit;
2532 FixupId branch_id = AddFixup(Fixup::Branch(pc, branch_type, size, cond));
2533
Dave Allison65fcc2c2014-04-28 13:45:27 -07002534 if (label->IsBound()) {
Vladimir Markocf93a5c2015-06-16 11:33:24 +00002535 // The branch is to a bound label which means that it's a backwards branch.
Vladimir Markocf93a5c2015-06-16 11:33:24 +00002536 GetFixup(branch_id)->Resolve(label->Position());
Vladimir Markocf93a5c2015-06-16 11:33:24 +00002537 Emit16(0);
Vladimir Markofbeb4ae2015-06-16 11:32:01 +00002538 } else {
Vladimir Markocf93a5c2015-06-16 11:33:24 +00002539 // Branch target is an unbound label. Add it to a singly-linked list maintained within
2540 // the code with the label serving as the head.
2541 Emit16(static_cast<uint16_t>(label->position_));
2542 label->LinkTo(branch_id);
Vladimir Markof38caa62015-05-29 15:50:18 +01002543 }
Vladimir Markocf93a5c2015-06-16 11:33:24 +00002544
2545 if (use32bit) {
2546 Emit16(0);
2547 }
2548 DCHECK_EQ(buffer_.Size() - pc, GetFixup(branch_id)->GetSizeInBytes());
Dave Allison65fcc2c2014-04-28 13:45:27 -07002549}
2550
2551
2552void Thumb2Assembler::clz(Register rd, Register rm, Condition cond) {
2553 CHECK_NE(rd, kNoRegister);
2554 CHECK_NE(rm, kNoRegister);
2555 CheckCondition(cond);
2556 CHECK_NE(rd, PC);
2557 CHECK_NE(rm, PC);
2558 int32_t encoding = B31 | B30 | B29 | B28 | B27 |
2559 B25 | B23 | B21 | B20 |
2560 static_cast<uint32_t>(rm) << 16 |
2561 0xf << 12 |
2562 static_cast<uint32_t>(rd) << 8 |
2563 B7 |
2564 static_cast<uint32_t>(rm);
2565 Emit32(encoding);
2566}
2567
2568
2569void Thumb2Assembler::movw(Register rd, uint16_t imm16, Condition cond) {
2570 CheckCondition(cond);
2571 bool must_be_32bit = force_32bit_;
2572 if (IsHighRegister(rd)|| imm16 >= 256u) {
2573 must_be_32bit = true;
2574 }
2575
2576 if (must_be_32bit) {
2577 // Use encoding T3.
Andreas Gampec8ccf682014-09-29 20:07:43 -07002578 uint32_t imm4 = (imm16 >> 12) & 15U /* 0b1111 */;
2579 uint32_t i = (imm16 >> 11) & 1U /* 0b1 */;
2580 uint32_t imm3 = (imm16 >> 8) & 7U /* 0b111 */;
Dave Allison65fcc2c2014-04-28 13:45:27 -07002581 uint32_t imm8 = imm16 & 0xff;
2582 int32_t encoding = B31 | B30 | B29 | B28 |
2583 B25 | B22 |
2584 static_cast<uint32_t>(rd) << 8 |
2585 i << 26 |
2586 imm4 << 16 |
2587 imm3 << 12 |
2588 imm8;
2589 Emit32(encoding);
2590 } else {
2591 int16_t encoding = B13 | static_cast<uint16_t>(rd) << 8 |
2592 imm16;
2593 Emit16(encoding);
2594 }
2595}
2596
2597
2598void Thumb2Assembler::movt(Register rd, uint16_t imm16, Condition cond) {
2599 CheckCondition(cond);
2600 // Always 32 bits.
Andreas Gampec8ccf682014-09-29 20:07:43 -07002601 uint32_t imm4 = (imm16 >> 12) & 15U /* 0b1111 */;
2602 uint32_t i = (imm16 >> 11) & 1U /* 0b1 */;
2603 uint32_t imm3 = (imm16 >> 8) & 7U /* 0b111 */;
Dave Allison65fcc2c2014-04-28 13:45:27 -07002604 uint32_t imm8 = imm16 & 0xff;
2605 int32_t encoding = B31 | B30 | B29 | B28 |
2606 B25 | B23 | B22 |
2607 static_cast<uint32_t>(rd) << 8 |
2608 i << 26 |
2609 imm4 << 16 |
2610 imm3 << 12 |
2611 imm8;
2612 Emit32(encoding);
2613}
2614
2615
Scott Wakeling9ee23f42015-07-23 10:44:35 +01002616void Thumb2Assembler::rbit(Register rd, Register rm, Condition cond) {
2617 CHECK_NE(rd, kNoRegister);
2618 CHECK_NE(rm, kNoRegister);
2619 CheckCondition(cond);
2620 CHECK_NE(rd, PC);
2621 CHECK_NE(rm, PC);
2622 CHECK_NE(rd, SP);
2623 CHECK_NE(rm, SP);
2624 int32_t encoding = B31 | B30 | B29 | B28 | B27 |
2625 B25 | B23 | B20 |
2626 static_cast<uint32_t>(rm) << 16 |
2627 0xf << 12 |
2628 static_cast<uint32_t>(rd) << 8 |
2629 B7 | B5 |
2630 static_cast<uint32_t>(rm);
2631 Emit32(encoding);
2632}
2633
2634
Dave Allison65fcc2c2014-04-28 13:45:27 -07002635void Thumb2Assembler::ldrex(Register rt, Register rn, uint16_t imm, Condition cond) {
2636 CHECK_NE(rn, kNoRegister);
2637 CHECK_NE(rt, kNoRegister);
2638 CheckCondition(cond);
Dave Allison65fcc2c2014-04-28 13:45:27 -07002639 CHECK_LT(imm, (1u << 10));
2640
2641 int32_t encoding = B31 | B30 | B29 | B27 | B22 | B20 |
2642 static_cast<uint32_t>(rn) << 16 |
2643 static_cast<uint32_t>(rt) << 12 |
2644 0xf << 8 |
2645 imm >> 2;
2646 Emit32(encoding);
2647}
2648
2649
2650void Thumb2Assembler::ldrex(Register rt, Register rn, Condition cond) {
2651 ldrex(rt, rn, 0, cond);
2652}
2653
2654
2655void Thumb2Assembler::strex(Register rd,
2656 Register rt,
2657 Register rn,
2658 uint16_t imm,
2659 Condition cond) {
2660 CHECK_NE(rn, kNoRegister);
2661 CHECK_NE(rd, kNoRegister);
2662 CHECK_NE(rt, kNoRegister);
2663 CheckCondition(cond);
2664 CHECK_LT(imm, (1u << 10));
2665
2666 int32_t encoding = B31 | B30 | B29 | B27 | B22 |
2667 static_cast<uint32_t>(rn) << 16 |
2668 static_cast<uint32_t>(rt) << 12 |
2669 static_cast<uint32_t>(rd) << 8 |
2670 imm >> 2;
2671 Emit32(encoding);
2672}
2673
2674
Calin Juravle52c48962014-12-16 17:02:57 +00002675void Thumb2Assembler::ldrexd(Register rt, Register rt2, Register rn, Condition cond) {
2676 CHECK_NE(rn, kNoRegister);
2677 CHECK_NE(rt, kNoRegister);
2678 CHECK_NE(rt2, kNoRegister);
2679 CHECK_NE(rt, rt2);
2680 CheckCondition(cond);
2681
2682 int32_t encoding = B31 | B30 | B29 | B27 | B23 | B22 | B20 |
2683 static_cast<uint32_t>(rn) << 16 |
2684 static_cast<uint32_t>(rt) << 12 |
2685 static_cast<uint32_t>(rt2) << 8 |
2686 B6 | B5 | B4 | B3 | B2 | B1 | B0;
2687 Emit32(encoding);
2688}
2689
2690
Dave Allison65fcc2c2014-04-28 13:45:27 -07002691void Thumb2Assembler::strex(Register rd,
2692 Register rt,
2693 Register rn,
2694 Condition cond) {
2695 strex(rd, rt, rn, 0, cond);
2696}
2697
2698
Calin Juravle52c48962014-12-16 17:02:57 +00002699void Thumb2Assembler::strexd(Register rd, Register rt, Register rt2, Register rn, Condition cond) {
2700 CHECK_NE(rd, kNoRegister);
2701 CHECK_NE(rn, kNoRegister);
2702 CHECK_NE(rt, kNoRegister);
2703 CHECK_NE(rt2, kNoRegister);
2704 CHECK_NE(rt, rt2);
2705 CHECK_NE(rd, rt);
2706 CHECK_NE(rd, rt2);
2707 CheckCondition(cond);
2708
2709 int32_t encoding = B31 | B30 | B29 | B27 | B23 | B22 |
2710 static_cast<uint32_t>(rn) << 16 |
2711 static_cast<uint32_t>(rt) << 12 |
2712 static_cast<uint32_t>(rt2) << 8 |
2713 B6 | B5 | B4 |
2714 static_cast<uint32_t>(rd);
2715 Emit32(encoding);
2716}
2717
2718
Dave Allison65fcc2c2014-04-28 13:45:27 -07002719void Thumb2Assembler::clrex(Condition cond) {
2720 CheckCondition(cond);
2721 int32_t encoding = B31 | B30 | B29 | B27 | B28 | B25 | B24 | B23 |
2722 B21 | B20 |
2723 0xf << 16 |
2724 B15 |
2725 0xf << 8 |
2726 B5 |
2727 0xf;
2728 Emit32(encoding);
2729}
2730
2731
2732void Thumb2Assembler::nop(Condition cond) {
2733 CheckCondition(cond);
Andreas Gampec8ccf682014-09-29 20:07:43 -07002734 uint16_t encoding = B15 | B13 | B12 |
Dave Allison65fcc2c2014-04-28 13:45:27 -07002735 B11 | B10 | B9 | B8;
Andreas Gampec8ccf682014-09-29 20:07:43 -07002736 Emit16(static_cast<int16_t>(encoding));
Dave Allison65fcc2c2014-04-28 13:45:27 -07002737}
2738
2739
2740void Thumb2Assembler::vmovsr(SRegister sn, Register rt, Condition cond) {
2741 CHECK_NE(sn, kNoSRegister);
2742 CHECK_NE(rt, kNoRegister);
2743 CHECK_NE(rt, SP);
2744 CHECK_NE(rt, PC);
2745 CheckCondition(cond);
2746 int32_t encoding = (static_cast<int32_t>(cond) << kConditionShift) |
2747 B27 | B26 | B25 |
2748 ((static_cast<int32_t>(sn) >> 1)*B16) |
2749 (static_cast<int32_t>(rt)*B12) | B11 | B9 |
2750 ((static_cast<int32_t>(sn) & 1)*B7) | B4;
2751 Emit32(encoding);
2752}
2753
2754
2755void Thumb2Assembler::vmovrs(Register rt, SRegister sn, Condition cond) {
2756 CHECK_NE(sn, kNoSRegister);
2757 CHECK_NE(rt, kNoRegister);
2758 CHECK_NE(rt, SP);
2759 CHECK_NE(rt, PC);
2760 CheckCondition(cond);
2761 int32_t encoding = (static_cast<int32_t>(cond) << kConditionShift) |
2762 B27 | B26 | B25 | B20 |
2763 ((static_cast<int32_t>(sn) >> 1)*B16) |
2764 (static_cast<int32_t>(rt)*B12) | B11 | B9 |
2765 ((static_cast<int32_t>(sn) & 1)*B7) | B4;
2766 Emit32(encoding);
2767}
2768
2769
2770void Thumb2Assembler::vmovsrr(SRegister sm, Register rt, Register rt2,
2771 Condition cond) {
2772 CHECK_NE(sm, kNoSRegister);
2773 CHECK_NE(sm, S31);
2774 CHECK_NE(rt, kNoRegister);
2775 CHECK_NE(rt, SP);
2776 CHECK_NE(rt, PC);
2777 CHECK_NE(rt2, kNoRegister);
2778 CHECK_NE(rt2, SP);
2779 CHECK_NE(rt2, PC);
2780 CheckCondition(cond);
2781 int32_t encoding = (static_cast<int32_t>(cond) << kConditionShift) |
2782 B27 | B26 | B22 |
2783 (static_cast<int32_t>(rt2)*B16) |
2784 (static_cast<int32_t>(rt)*B12) | B11 | B9 |
2785 ((static_cast<int32_t>(sm) & 1)*B5) | B4 |
2786 (static_cast<int32_t>(sm) >> 1);
2787 Emit32(encoding);
2788}
2789
2790
2791void Thumb2Assembler::vmovrrs(Register rt, Register rt2, SRegister sm,
2792 Condition cond) {
2793 CHECK_NE(sm, kNoSRegister);
2794 CHECK_NE(sm, S31);
2795 CHECK_NE(rt, kNoRegister);
2796 CHECK_NE(rt, SP);
2797 CHECK_NE(rt, PC);
2798 CHECK_NE(rt2, kNoRegister);
2799 CHECK_NE(rt2, SP);
2800 CHECK_NE(rt2, PC);
2801 CHECK_NE(rt, rt2);
2802 CheckCondition(cond);
2803 int32_t encoding = (static_cast<int32_t>(cond) << kConditionShift) |
2804 B27 | B26 | B22 | B20 |
2805 (static_cast<int32_t>(rt2)*B16) |
2806 (static_cast<int32_t>(rt)*B12) | B11 | B9 |
2807 ((static_cast<int32_t>(sm) & 1)*B5) | B4 |
2808 (static_cast<int32_t>(sm) >> 1);
2809 Emit32(encoding);
2810}
2811
2812
2813void Thumb2Assembler::vmovdrr(DRegister dm, Register rt, Register rt2,
2814 Condition cond) {
2815 CHECK_NE(dm, kNoDRegister);
2816 CHECK_NE(rt, kNoRegister);
2817 CHECK_NE(rt, SP);
2818 CHECK_NE(rt, PC);
2819 CHECK_NE(rt2, kNoRegister);
2820 CHECK_NE(rt2, SP);
2821 CHECK_NE(rt2, PC);
2822 CheckCondition(cond);
2823 int32_t encoding = (static_cast<int32_t>(cond) << kConditionShift) |
2824 B27 | B26 | B22 |
2825 (static_cast<int32_t>(rt2)*B16) |
2826 (static_cast<int32_t>(rt)*B12) | B11 | B9 | B8 |
2827 ((static_cast<int32_t>(dm) >> 4)*B5) | B4 |
2828 (static_cast<int32_t>(dm) & 0xf);
2829 Emit32(encoding);
2830}
2831
2832
2833void Thumb2Assembler::vmovrrd(Register rt, Register rt2, DRegister dm,
2834 Condition cond) {
2835 CHECK_NE(dm, kNoDRegister);
2836 CHECK_NE(rt, kNoRegister);
2837 CHECK_NE(rt, SP);
2838 CHECK_NE(rt, PC);
2839 CHECK_NE(rt2, kNoRegister);
2840 CHECK_NE(rt2, SP);
2841 CHECK_NE(rt2, PC);
2842 CHECK_NE(rt, rt2);
2843 CheckCondition(cond);
2844 int32_t encoding = (static_cast<int32_t>(cond) << kConditionShift) |
2845 B27 | B26 | B22 | B20 |
2846 (static_cast<int32_t>(rt2)*B16) |
2847 (static_cast<int32_t>(rt)*B12) | B11 | B9 | B8 |
2848 ((static_cast<int32_t>(dm) >> 4)*B5) | B4 |
2849 (static_cast<int32_t>(dm) & 0xf);
2850 Emit32(encoding);
2851}
2852
2853
2854void Thumb2Assembler::vldrs(SRegister sd, const Address& ad, Condition cond) {
2855 const Address& addr = static_cast<const Address&>(ad);
2856 CHECK_NE(sd, kNoSRegister);
2857 CheckCondition(cond);
2858 int32_t encoding = (static_cast<int32_t>(cond) << kConditionShift) |
2859 B27 | B26 | B24 | B20 |
2860 ((static_cast<int32_t>(sd) & 1)*B22) |
2861 ((static_cast<int32_t>(sd) >> 1)*B12) |
2862 B11 | B9 | addr.vencoding();
2863 Emit32(encoding);
2864}
2865
2866
2867void Thumb2Assembler::vstrs(SRegister sd, const Address& ad, Condition cond) {
2868 const Address& addr = static_cast<const Address&>(ad);
2869 CHECK_NE(static_cast<Register>(addr.encodingArm() & (0xf << kRnShift)), PC);
2870 CHECK_NE(sd, kNoSRegister);
2871 CheckCondition(cond);
2872 int32_t encoding = (static_cast<int32_t>(cond) << kConditionShift) |
2873 B27 | B26 | B24 |
2874 ((static_cast<int32_t>(sd) & 1)*B22) |
2875 ((static_cast<int32_t>(sd) >> 1)*B12) |
2876 B11 | B9 | addr.vencoding();
2877 Emit32(encoding);
2878}
2879
2880
2881void Thumb2Assembler::vldrd(DRegister dd, const Address& ad, Condition cond) {
2882 const Address& addr = static_cast<const Address&>(ad);
2883 CHECK_NE(dd, kNoDRegister);
2884 CheckCondition(cond);
2885 int32_t encoding = (static_cast<int32_t>(cond) << kConditionShift) |
2886 B27 | B26 | B24 | B20 |
2887 ((static_cast<int32_t>(dd) >> 4)*B22) |
2888 ((static_cast<int32_t>(dd) & 0xf)*B12) |
2889 B11 | B9 | B8 | addr.vencoding();
2890 Emit32(encoding);
2891}
2892
2893
2894void Thumb2Assembler::vstrd(DRegister dd, const Address& ad, Condition cond) {
2895 const Address& addr = static_cast<const Address&>(ad);
2896 CHECK_NE(static_cast<Register>(addr.encodingArm() & (0xf << kRnShift)), PC);
2897 CHECK_NE(dd, kNoDRegister);
2898 CheckCondition(cond);
2899 int32_t encoding = (static_cast<int32_t>(cond) << kConditionShift) |
2900 B27 | B26 | B24 |
2901 ((static_cast<int32_t>(dd) >> 4)*B22) |
2902 ((static_cast<int32_t>(dd) & 0xf)*B12) |
2903 B11 | B9 | B8 | addr.vencoding();
2904 Emit32(encoding);
2905}
2906
2907
2908void Thumb2Assembler::vpushs(SRegister reg, int nregs, Condition cond) {
2909 EmitVPushPop(static_cast<uint32_t>(reg), nregs, true, false, cond);
2910}
2911
2912
2913void Thumb2Assembler::vpushd(DRegister reg, int nregs, Condition cond) {
2914 EmitVPushPop(static_cast<uint32_t>(reg), nregs, true, true, cond);
2915}
2916
2917
2918void Thumb2Assembler::vpops(SRegister reg, int nregs, Condition cond) {
2919 EmitVPushPop(static_cast<uint32_t>(reg), nregs, false, false, cond);
2920}
2921
2922
2923void Thumb2Assembler::vpopd(DRegister reg, int nregs, Condition cond) {
2924 EmitVPushPop(static_cast<uint32_t>(reg), nregs, false, true, cond);
2925}
2926
2927
2928void Thumb2Assembler::EmitVPushPop(uint32_t reg, int nregs, bool push, bool dbl, Condition cond) {
2929 CheckCondition(cond);
2930
2931 uint32_t D;
2932 uint32_t Vd;
2933 if (dbl) {
2934 // Encoded as D:Vd.
2935 D = (reg >> 4) & 1;
Andreas Gampec8ccf682014-09-29 20:07:43 -07002936 Vd = reg & 15U /* 0b1111 */;
Dave Allison65fcc2c2014-04-28 13:45:27 -07002937 } else {
2938 // Encoded as Vd:D.
2939 D = reg & 1;
Andreas Gampec8ccf682014-09-29 20:07:43 -07002940 Vd = (reg >> 1) & 15U /* 0b1111 */;
Dave Allison65fcc2c2014-04-28 13:45:27 -07002941 }
2942 int32_t encoding = B27 | B26 | B21 | B19 | B18 | B16 |
2943 B11 | B9 |
2944 (dbl ? B8 : 0) |
2945 (push ? B24 : (B23 | B20)) |
Andreas Gampec8ccf682014-09-29 20:07:43 -07002946 14U /* 0b1110 */ << 28 |
Dave Allison65fcc2c2014-04-28 13:45:27 -07002947 nregs << (dbl ? 1 : 0) |
2948 D << 22 |
2949 Vd << 12;
2950 Emit32(encoding);
2951}
2952
2953
2954void Thumb2Assembler::EmitVFPsss(Condition cond, int32_t opcode,
2955 SRegister sd, SRegister sn, SRegister sm) {
2956 CHECK_NE(sd, kNoSRegister);
2957 CHECK_NE(sn, kNoSRegister);
2958 CHECK_NE(sm, kNoSRegister);
2959 CheckCondition(cond);
2960 int32_t encoding = (static_cast<int32_t>(cond) << kConditionShift) |
2961 B27 | B26 | B25 | B11 | B9 | opcode |
2962 ((static_cast<int32_t>(sd) & 1)*B22) |
2963 ((static_cast<int32_t>(sn) >> 1)*B16) |
2964 ((static_cast<int32_t>(sd) >> 1)*B12) |
2965 ((static_cast<int32_t>(sn) & 1)*B7) |
2966 ((static_cast<int32_t>(sm) & 1)*B5) |
2967 (static_cast<int32_t>(sm) >> 1);
2968 Emit32(encoding);
2969}
2970
2971
2972void Thumb2Assembler::EmitVFPddd(Condition cond, int32_t opcode,
2973 DRegister dd, DRegister dn, DRegister dm) {
2974 CHECK_NE(dd, kNoDRegister);
2975 CHECK_NE(dn, kNoDRegister);
2976 CHECK_NE(dm, kNoDRegister);
2977 CheckCondition(cond);
2978 int32_t encoding = (static_cast<int32_t>(cond) << kConditionShift) |
2979 B27 | B26 | B25 | B11 | B9 | B8 | opcode |
2980 ((static_cast<int32_t>(dd) >> 4)*B22) |
2981 ((static_cast<int32_t>(dn) & 0xf)*B16) |
2982 ((static_cast<int32_t>(dd) & 0xf)*B12) |
2983 ((static_cast<int32_t>(dn) >> 4)*B7) |
2984 ((static_cast<int32_t>(dm) >> 4)*B5) |
2985 (static_cast<int32_t>(dm) & 0xf);
2986 Emit32(encoding);
2987}
2988
2989
2990void Thumb2Assembler::EmitVFPsd(Condition cond, int32_t opcode,
2991 SRegister sd, DRegister dm) {
2992 CHECK_NE(sd, kNoSRegister);
2993 CHECK_NE(dm, kNoDRegister);
2994 CheckCondition(cond);
2995 int32_t encoding = (static_cast<int32_t>(cond) << kConditionShift) |
2996 B27 | B26 | B25 | B11 | B9 | opcode |
2997 ((static_cast<int32_t>(sd) & 1)*B22) |
2998 ((static_cast<int32_t>(sd) >> 1)*B12) |
2999 ((static_cast<int32_t>(dm) >> 4)*B5) |
3000 (static_cast<int32_t>(dm) & 0xf);
3001 Emit32(encoding);
3002}
3003
3004
3005void Thumb2Assembler::EmitVFPds(Condition cond, int32_t opcode,
3006 DRegister dd, SRegister sm) {
3007 CHECK_NE(dd, kNoDRegister);
3008 CHECK_NE(sm, kNoSRegister);
3009 CheckCondition(cond);
3010 int32_t encoding = (static_cast<int32_t>(cond) << kConditionShift) |
3011 B27 | B26 | B25 | B11 | B9 | opcode |
3012 ((static_cast<int32_t>(dd) >> 4)*B22) |
3013 ((static_cast<int32_t>(dd) & 0xf)*B12) |
3014 ((static_cast<int32_t>(sm) & 1)*B5) |
3015 (static_cast<int32_t>(sm) >> 1);
3016 Emit32(encoding);
3017}
3018
3019
3020void Thumb2Assembler::vmstat(Condition cond) { // VMRS APSR_nzcv, FPSCR.
Calin Juravleddb7df22014-11-25 20:56:51 +00003021 CHECK_NE(cond, kNoCondition);
Dave Allison65fcc2c2014-04-28 13:45:27 -07003022 CheckCondition(cond);
Calin Juravleddb7df22014-11-25 20:56:51 +00003023 int32_t encoding = (static_cast<int32_t>(cond) << kConditionShift) |
3024 B27 | B26 | B25 | B23 | B22 | B21 | B20 | B16 |
3025 (static_cast<int32_t>(PC)*B12) |
3026 B11 | B9 | B4;
3027 Emit32(encoding);
Dave Allison65fcc2c2014-04-28 13:45:27 -07003028}
3029
3030
3031void Thumb2Assembler::svc(uint32_t imm8) {
Andreas Gampeab1eb0d2015-02-13 19:23:55 -08003032 CHECK(IsUint<8>(imm8)) << imm8;
Dave Allison65fcc2c2014-04-28 13:45:27 -07003033 int16_t encoding = B15 | B14 | B12 |
3034 B11 | B10 | B9 | B8 |
3035 imm8;
3036 Emit16(encoding);
3037}
3038
3039
3040void Thumb2Assembler::bkpt(uint16_t imm8) {
Andreas Gampeab1eb0d2015-02-13 19:23:55 -08003041 CHECK(IsUint<8>(imm8)) << imm8;
Dave Allison65fcc2c2014-04-28 13:45:27 -07003042 int16_t encoding = B15 | B13 | B12 |
3043 B11 | B10 | B9 |
3044 imm8;
3045 Emit16(encoding);
3046}
3047
3048// Convert the given IT state to a mask bit given bit 0 of the first
3049// condition and a shift position.
3050static uint8_t ToItMask(ItState s, uint8_t firstcond0, uint8_t shift) {
3051 switch (s) {
3052 case kItOmitted: return 1 << shift;
3053 case kItThen: return firstcond0 << shift;
3054 case kItElse: return !firstcond0 << shift;
3055 }
3056 return 0;
3057}
3058
3059
3060// Set the IT condition in the given position for the given state. This is used
3061// to check that conditional instructions match the preceding IT statement.
3062void Thumb2Assembler::SetItCondition(ItState s, Condition cond, uint8_t index) {
3063 switch (s) {
3064 case kItOmitted: it_conditions_[index] = AL; break;
3065 case kItThen: it_conditions_[index] = cond; break;
3066 case kItElse:
3067 it_conditions_[index] = static_cast<Condition>(static_cast<uint8_t>(cond) ^ 1);
3068 break;
3069 }
3070}
3071
3072
3073void Thumb2Assembler::it(Condition firstcond, ItState i1, ItState i2, ItState i3) {
3074 CheckCondition(AL); // Not allowed in IT block.
3075 uint8_t firstcond0 = static_cast<uint8_t>(firstcond) & 1;
3076
3077 // All conditions to AL.
3078 for (uint8_t i = 0; i < 4; ++i) {
3079 it_conditions_[i] = AL;
3080 }
3081
3082 SetItCondition(kItThen, firstcond, 0);
3083 uint8_t mask = ToItMask(i1, firstcond0, 3);
3084 SetItCondition(i1, firstcond, 1);
3085
3086 if (i1 != kItOmitted) {
3087 mask |= ToItMask(i2, firstcond0, 2);
3088 SetItCondition(i2, firstcond, 2);
3089 if (i2 != kItOmitted) {
3090 mask |= ToItMask(i3, firstcond0, 1);
3091 SetItCondition(i3, firstcond, 3);
3092 if (i3 != kItOmitted) {
Andreas Gampec8ccf682014-09-29 20:07:43 -07003093 mask |= 1U /* 0b0001 */;
Dave Allison65fcc2c2014-04-28 13:45:27 -07003094 }
3095 }
3096 }
3097
3098 // Start at first condition.
3099 it_cond_index_ = 0;
3100 next_condition_ = it_conditions_[0];
3101 uint16_t encoding = B15 | B13 | B12 |
3102 B11 | B10 | B9 | B8 |
3103 firstcond << 4 |
3104 mask;
3105 Emit16(encoding);
3106}
3107
3108
3109void Thumb2Assembler::cbz(Register rn, Label* label) {
3110 CheckCondition(AL);
3111 if (label->IsBound()) {
3112 LOG(FATAL) << "cbz can only be used to branch forwards";
Vladimir Markoe8469c12014-11-26 18:09:30 +00003113 UNREACHABLE();
Nicolas Geoffrayd56376c2015-05-21 12:32:34 +00003114 } else if (IsHighRegister(rn)) {
3115 LOG(FATAL) << "cbz can only be used with low registers";
3116 UNREACHABLE();
Dave Allison65fcc2c2014-04-28 13:45:27 -07003117 } else {
3118 uint16_t branchid = EmitCompareAndBranch(rn, static_cast<uint16_t>(label->position_), false);
3119 label->LinkTo(branchid);
3120 }
3121}
3122
3123
3124void Thumb2Assembler::cbnz(Register rn, Label* label) {
3125 CheckCondition(AL);
3126 if (label->IsBound()) {
3127 LOG(FATAL) << "cbnz can only be used to branch forwards";
Vladimir Markoe8469c12014-11-26 18:09:30 +00003128 UNREACHABLE();
Nicolas Geoffrayd56376c2015-05-21 12:32:34 +00003129 } else if (IsHighRegister(rn)) {
3130 LOG(FATAL) << "cbnz can only be used with low registers";
3131 UNREACHABLE();
Dave Allison65fcc2c2014-04-28 13:45:27 -07003132 } else {
3133 uint16_t branchid = EmitCompareAndBranch(rn, static_cast<uint16_t>(label->position_), true);
3134 label->LinkTo(branchid);
3135 }
3136}
3137
3138
3139void Thumb2Assembler::blx(Register rm, Condition cond) {
3140 CHECK_NE(rm, kNoRegister);
3141 CheckCondition(cond);
3142 int16_t encoding = B14 | B10 | B9 | B8 | B7 | static_cast<int16_t>(rm) << 3;
3143 Emit16(encoding);
3144}
3145
3146
3147void Thumb2Assembler::bx(Register rm, Condition cond) {
3148 CHECK_NE(rm, kNoRegister);
3149 CheckCondition(cond);
3150 int16_t encoding = B14 | B10 | B9 | B8 | static_cast<int16_t>(rm) << 3;
3151 Emit16(encoding);
3152}
3153
3154
3155void Thumb2Assembler::Push(Register rd, Condition cond) {
3156 str(rd, Address(SP, -kRegisterSize, Address::PreIndex), cond);
3157}
3158
3159
3160void Thumb2Assembler::Pop(Register rd, Condition cond) {
3161 ldr(rd, Address(SP, kRegisterSize, Address::PostIndex), cond);
3162}
3163
3164
3165void Thumb2Assembler::PushList(RegList regs, Condition cond) {
3166 stm(DB_W, SP, regs, cond);
3167}
3168
3169
3170void Thumb2Assembler::PopList(RegList regs, Condition cond) {
3171 ldm(IA_W, SP, regs, cond);
3172}
3173
3174
3175void Thumb2Assembler::Mov(Register rd, Register rm, Condition cond) {
3176 if (cond != AL || rd != rm) {
3177 mov(rd, ShifterOperand(rm), cond);
3178 }
3179}
3180
3181
Dave Allison65fcc2c2014-04-28 13:45:27 -07003182void Thumb2Assembler::Bind(Label* label) {
Vladimir Markocf93a5c2015-06-16 11:33:24 +00003183 BindLabel(label, buffer_.Size());
Dave Allison65fcc2c2014-04-28 13:45:27 -07003184}
3185
3186
3187void Thumb2Assembler::Lsl(Register rd, Register rm, uint32_t shift_imm,
Vladimir Marko73cf0fb2015-07-30 15:07:22 +01003188 Condition cond, SetCc set_cc) {
Calin Juravle9aec02f2014-11-18 23:06:35 +00003189 CHECK_LE(shift_imm, 31u);
Dave Allison45fdb932014-06-25 12:37:10 -07003190 CheckCondition(cond);
Vladimir Marko73cf0fb2015-07-30 15:07:22 +01003191 EmitShift(rd, rm, LSL, shift_imm, cond, set_cc);
Dave Allison65fcc2c2014-04-28 13:45:27 -07003192}
3193
3194
3195void Thumb2Assembler::Lsr(Register rd, Register rm, uint32_t shift_imm,
Vladimir Marko73cf0fb2015-07-30 15:07:22 +01003196 Condition cond, SetCc set_cc) {
Calin Juravle9aec02f2014-11-18 23:06:35 +00003197 CHECK(1u <= shift_imm && shift_imm <= 32u);
Dave Allison65fcc2c2014-04-28 13:45:27 -07003198 if (shift_imm == 32) shift_imm = 0; // Comply to UAL syntax.
Dave Allison45fdb932014-06-25 12:37:10 -07003199 CheckCondition(cond);
Vladimir Marko73cf0fb2015-07-30 15:07:22 +01003200 EmitShift(rd, rm, LSR, shift_imm, cond, set_cc);
Dave Allison65fcc2c2014-04-28 13:45:27 -07003201}
3202
3203
3204void Thumb2Assembler::Asr(Register rd, Register rm, uint32_t shift_imm,
Vladimir Marko73cf0fb2015-07-30 15:07:22 +01003205 Condition cond, SetCc set_cc) {
Calin Juravle9aec02f2014-11-18 23:06:35 +00003206 CHECK(1u <= shift_imm && shift_imm <= 32u);
Dave Allison65fcc2c2014-04-28 13:45:27 -07003207 if (shift_imm == 32) shift_imm = 0; // Comply to UAL syntax.
Dave Allison45fdb932014-06-25 12:37:10 -07003208 CheckCondition(cond);
Vladimir Marko73cf0fb2015-07-30 15:07:22 +01003209 EmitShift(rd, rm, ASR, shift_imm, cond, set_cc);
Dave Allison65fcc2c2014-04-28 13:45:27 -07003210}
3211
3212
3213void Thumb2Assembler::Ror(Register rd, Register rm, uint32_t shift_imm,
Vladimir Marko73cf0fb2015-07-30 15:07:22 +01003214 Condition cond, SetCc set_cc) {
Calin Juravle9aec02f2014-11-18 23:06:35 +00003215 CHECK(1u <= shift_imm && shift_imm <= 31u);
Dave Allison45fdb932014-06-25 12:37:10 -07003216 CheckCondition(cond);
Vladimir Marko73cf0fb2015-07-30 15:07:22 +01003217 EmitShift(rd, rm, ROR, shift_imm, cond, set_cc);
Dave Allison65fcc2c2014-04-28 13:45:27 -07003218}
3219
3220
Vladimir Marko73cf0fb2015-07-30 15:07:22 +01003221void Thumb2Assembler::Rrx(Register rd, Register rm, Condition cond, SetCc set_cc) {
Dave Allison45fdb932014-06-25 12:37:10 -07003222 CheckCondition(cond);
Vladimir Markof9d741e2015-11-20 15:08:11 +00003223 EmitShift(rd, rm, RRX, 0, cond, set_cc);
Dave Allison45fdb932014-06-25 12:37:10 -07003224}
3225
3226
3227void Thumb2Assembler::Lsl(Register rd, Register rm, Register rn,
Vladimir Marko73cf0fb2015-07-30 15:07:22 +01003228 Condition cond, SetCc set_cc) {
Dave Allison45fdb932014-06-25 12:37:10 -07003229 CheckCondition(cond);
Vladimir Marko73cf0fb2015-07-30 15:07:22 +01003230 EmitShift(rd, rm, LSL, rn, cond, set_cc);
Dave Allison45fdb932014-06-25 12:37:10 -07003231}
3232
3233
3234void Thumb2Assembler::Lsr(Register rd, Register rm, Register rn,
Vladimir Marko73cf0fb2015-07-30 15:07:22 +01003235 Condition cond, SetCc set_cc) {
Dave Allison45fdb932014-06-25 12:37:10 -07003236 CheckCondition(cond);
Vladimir Marko73cf0fb2015-07-30 15:07:22 +01003237 EmitShift(rd, rm, LSR, rn, cond, set_cc);
Dave Allison45fdb932014-06-25 12:37:10 -07003238}
3239
3240
3241void Thumb2Assembler::Asr(Register rd, Register rm, Register rn,
Vladimir Marko73cf0fb2015-07-30 15:07:22 +01003242 Condition cond, SetCc set_cc) {
Dave Allison45fdb932014-06-25 12:37:10 -07003243 CheckCondition(cond);
Vladimir Marko73cf0fb2015-07-30 15:07:22 +01003244 EmitShift(rd, rm, ASR, rn, cond, set_cc);
Dave Allison45fdb932014-06-25 12:37:10 -07003245}
3246
3247
3248void Thumb2Assembler::Ror(Register rd, Register rm, Register rn,
Vladimir Marko73cf0fb2015-07-30 15:07:22 +01003249 Condition cond, SetCc set_cc) {
Dave Allison45fdb932014-06-25 12:37:10 -07003250 CheckCondition(cond);
Vladimir Marko73cf0fb2015-07-30 15:07:22 +01003251 EmitShift(rd, rm, ROR, rn, cond, set_cc);
Dave Allison65fcc2c2014-04-28 13:45:27 -07003252}
3253
3254
3255int32_t Thumb2Assembler::EncodeBranchOffset(int32_t offset, int32_t inst) {
3256 // The offset is off by 4 due to the way the ARM CPUs read PC.
3257 offset -= 4;
3258 offset >>= 1;
3259
3260 uint32_t value = 0;
3261 // There are two different encodings depending on the value of bit 12. In one case
3262 // intermediate values are calculated using the sign bit.
3263 if ((inst & B12) == B12) {
3264 // 25 bits of offset.
3265 uint32_t signbit = (offset >> 31) & 0x1;
3266 uint32_t i1 = (offset >> 22) & 0x1;
3267 uint32_t i2 = (offset >> 21) & 0x1;
3268 uint32_t imm10 = (offset >> 11) & 0x03ff;
3269 uint32_t imm11 = offset & 0x07ff;
3270 uint32_t j1 = (i1 ^ signbit) ? 0 : 1;
3271 uint32_t j2 = (i2 ^ signbit) ? 0 : 1;
3272 value = (signbit << 26) | (j1 << 13) | (j2 << 11) | (imm10 << 16) |
3273 imm11;
3274 // Remove the offset from the current encoding.
3275 inst &= ~(0x3ff << 16 | 0x7ff);
3276 } else {
3277 uint32_t signbit = (offset >> 31) & 0x1;
3278 uint32_t imm6 = (offset >> 11) & 0x03f;
3279 uint32_t imm11 = offset & 0x07ff;
3280 uint32_t j1 = (offset >> 19) & 1;
3281 uint32_t j2 = (offset >> 17) & 1;
3282 value = (signbit << 26) | (j1 << 13) | (j2 << 11) | (imm6 << 16) |
3283 imm11;
3284 // Remove the offset from the current encoding.
3285 inst &= ~(0x3f << 16 | 0x7ff);
3286 }
3287 // Mask out offset bits in current instruction.
3288 inst &= ~(B26 | B13 | B11);
3289 inst |= value;
3290 return inst;
3291}
3292
3293
3294int Thumb2Assembler::DecodeBranchOffset(int32_t instr) {
3295 int32_t imm32;
3296 if ((instr & B12) == B12) {
3297 uint32_t S = (instr >> 26) & 1;
3298 uint32_t J2 = (instr >> 11) & 1;
3299 uint32_t J1 = (instr >> 13) & 1;
3300 uint32_t imm10 = (instr >> 16) & 0x3FF;
3301 uint32_t imm11 = instr & 0x7FF;
3302
3303 uint32_t I1 = ~(J1 ^ S) & 1;
3304 uint32_t I2 = ~(J2 ^ S) & 1;
3305 imm32 = (S << 24) | (I1 << 23) | (I2 << 22) | (imm10 << 12) | (imm11 << 1);
3306 imm32 = (imm32 << 8) >> 8; // sign extend 24 bit immediate.
3307 } else {
3308 uint32_t S = (instr >> 26) & 1;
3309 uint32_t J2 = (instr >> 11) & 1;
3310 uint32_t J1 = (instr >> 13) & 1;
3311 uint32_t imm6 = (instr >> 16) & 0x3F;
3312 uint32_t imm11 = instr & 0x7FF;
3313
3314 imm32 = (S << 20) | (J2 << 19) | (J1 << 18) | (imm6 << 12) | (imm11 << 1);
3315 imm32 = (imm32 << 11) >> 11; // sign extend 21 bit immediate.
3316 }
3317 imm32 += 4;
3318 return imm32;
3319}
3320
Vladimir Markocf93a5c2015-06-16 11:33:24 +00003321uint32_t Thumb2Assembler::GetAdjustedPosition(uint32_t old_position) {
3322 // We can reconstruct the adjustment by going through all the fixups from the beginning
3323 // up to the old_position. Since we expect AdjustedPosition() to be called in a loop
3324 // with increasing old_position, we can use the data from last AdjustedPosition() to
3325 // continue where we left off and the whole loop should be O(m+n) where m is the number
3326 // of positions to adjust and n is the number of fixups.
3327 if (old_position < last_old_position_) {
3328 last_position_adjustment_ = 0u;
3329 last_old_position_ = 0u;
3330 last_fixup_id_ = 0u;
3331 }
3332 while (last_fixup_id_ != fixups_.size()) {
3333 Fixup* fixup = GetFixup(last_fixup_id_);
3334 if (fixup->GetLocation() >= old_position + last_position_adjustment_) {
3335 break;
3336 }
3337 if (fixup->GetSize() != fixup->GetOriginalSize()) {
3338 last_position_adjustment_ += fixup->GetSizeInBytes() - fixup->GetOriginalSizeInBytes();
3339 }
3340 ++last_fixup_id_;
3341 }
3342 last_old_position_ = old_position;
3343 return old_position + last_position_adjustment_;
3344}
3345
3346Literal* Thumb2Assembler::NewLiteral(size_t size, const uint8_t* data) {
3347 DCHECK(size == 4u || size == 8u) << size;
3348 literals_.emplace_back(size, data);
3349 return &literals_.back();
3350}
3351
3352void Thumb2Assembler::LoadLiteral(Register rt, Literal* literal) {
3353 DCHECK_EQ(literal->GetSize(), 4u);
3354 DCHECK(!literal->GetLabel()->IsBound());
3355 bool use32bit = IsForced32Bit() || IsHighRegister(rt);
3356 uint32_t location = buffer_.Size();
3357 Fixup::Size size = use32bit ? Fixup::kLiteral4KiB : Fixup::kLiteral1KiB;
3358 FixupId fixup_id = AddFixup(Fixup::LoadNarrowLiteral(location, rt, size));
3359 Emit16(static_cast<uint16_t>(literal->GetLabel()->position_));
3360 literal->GetLabel()->LinkTo(fixup_id);
3361 if (use32bit) {
3362 Emit16(0);
3363 }
3364 DCHECK_EQ(location + GetFixup(fixup_id)->GetSizeInBytes(), buffer_.Size());
3365}
3366
3367void Thumb2Assembler::LoadLiteral(Register rt, Register rt2, Literal* literal) {
3368 DCHECK_EQ(literal->GetSize(), 8u);
3369 DCHECK(!literal->GetLabel()->IsBound());
3370 uint32_t location = buffer_.Size();
3371 FixupId fixup_id =
3372 AddFixup(Fixup::LoadWideLiteral(location, rt, rt2, Fixup::kLongOrFPLiteral1KiB));
3373 Emit16(static_cast<uint16_t>(literal->GetLabel()->position_));
3374 literal->GetLabel()->LinkTo(fixup_id);
3375 Emit16(0);
3376 DCHECK_EQ(location + GetFixup(fixup_id)->GetSizeInBytes(), buffer_.Size());
3377}
3378
3379void Thumb2Assembler::LoadLiteral(SRegister sd, Literal* literal) {
3380 DCHECK_EQ(literal->GetSize(), 4u);
3381 DCHECK(!literal->GetLabel()->IsBound());
3382 uint32_t location = buffer_.Size();
3383 FixupId fixup_id = AddFixup(Fixup::LoadSingleLiteral(location, sd, Fixup::kLongOrFPLiteral1KiB));
3384 Emit16(static_cast<uint16_t>(literal->GetLabel()->position_));
3385 literal->GetLabel()->LinkTo(fixup_id);
3386 Emit16(0);
3387 DCHECK_EQ(location + GetFixup(fixup_id)->GetSizeInBytes(), buffer_.Size());
3388}
3389
3390void Thumb2Assembler::LoadLiteral(DRegister dd, Literal* literal) {
3391 DCHECK_EQ(literal->GetSize(), 8u);
3392 DCHECK(!literal->GetLabel()->IsBound());
3393 uint32_t location = buffer_.Size();
3394 FixupId fixup_id = AddFixup(Fixup::LoadDoubleLiteral(location, dd, Fixup::kLongOrFPLiteral1KiB));
3395 Emit16(static_cast<uint16_t>(literal->GetLabel()->position_));
3396 literal->GetLabel()->LinkTo(fixup_id);
3397 Emit16(0);
3398 DCHECK_EQ(location + GetFixup(fixup_id)->GetSizeInBytes(), buffer_.Size());
3399}
Dave Allison65fcc2c2014-04-28 13:45:27 -07003400
Dave Allison65fcc2c2014-04-28 13:45:27 -07003401
3402void Thumb2Assembler::AddConstant(Register rd, Register rn, int32_t value,
Vladimir Marko449b1092015-09-08 12:16:45 +01003403 Condition cond, SetCc set_cc) {
3404 if (value == 0 && set_cc != kCcSet) {
Dave Allison65fcc2c2014-04-28 13:45:27 -07003405 if (rd != rn) {
3406 mov(rd, ShifterOperand(rn), cond);
3407 }
3408 return;
3409 }
3410 // We prefer to select the shorter code sequence rather than selecting add for
3411 // positive values and sub for negatives ones, which would slightly improve
3412 // the readability of generated code for some constants.
3413 ShifterOperand shifter_op;
Nicolas Geoffray3bcc8ea2014-11-28 15:00:02 +00003414 if (ShifterOperandCanHold(rd, rn, ADD, value, &shifter_op)) {
Vladimir Marko449b1092015-09-08 12:16:45 +01003415 add(rd, rn, shifter_op, cond, set_cc);
Nicolas Geoffray3bcc8ea2014-11-28 15:00:02 +00003416 } else if (ShifterOperandCanHold(rd, rn, SUB, -value, &shifter_op)) {
Vladimir Marko449b1092015-09-08 12:16:45 +01003417 sub(rd, rn, shifter_op, cond, set_cc);
Dave Allison65fcc2c2014-04-28 13:45:27 -07003418 } else {
3419 CHECK(rn != IP);
Nicolas Geoffray3bcc8ea2014-11-28 15:00:02 +00003420 if (ShifterOperandCanHold(rd, rn, MVN, ~value, &shifter_op)) {
Vladimir Marko449b1092015-09-08 12:16:45 +01003421 mvn(IP, shifter_op, cond, kCcKeep);
3422 add(rd, rn, ShifterOperand(IP), cond, set_cc);
Nicolas Geoffray3bcc8ea2014-11-28 15:00:02 +00003423 } else if (ShifterOperandCanHold(rd, rn, MVN, ~(-value), &shifter_op)) {
Vladimir Marko449b1092015-09-08 12:16:45 +01003424 mvn(IP, shifter_op, cond, kCcKeep);
3425 sub(rd, rn, ShifterOperand(IP), cond, set_cc);
Dave Allison65fcc2c2014-04-28 13:45:27 -07003426 } else {
3427 movw(IP, Low16Bits(value), cond);
3428 uint16_t value_high = High16Bits(value);
3429 if (value_high != 0) {
3430 movt(IP, value_high, cond);
3431 }
Vladimir Marko449b1092015-09-08 12:16:45 +01003432 add(rd, rn, ShifterOperand(IP), cond, set_cc);
Dave Allison65fcc2c2014-04-28 13:45:27 -07003433 }
3434 }
3435}
3436
Andreas Gampe7cffc3b2015-10-19 21:31:53 -07003437void Thumb2Assembler::CmpConstant(Register rn, int32_t value, Condition cond) {
3438 // We prefer to select the shorter code sequence rather than selecting add for
3439 // positive values and sub for negatives ones, which would slightly improve
3440 // the readability of generated code for some constants.
3441 ShifterOperand shifter_op;
3442 if (ShifterOperandCanHold(kNoRegister, rn, CMP, value, &shifter_op)) {
3443 cmp(rn, shifter_op, cond);
3444 } else if (ShifterOperandCanHold(kNoRegister, rn, CMN, ~value, &shifter_op)) {
3445 cmn(rn, shifter_op, cond);
3446 } else {
3447 CHECK(rn != IP);
3448 movw(IP, Low16Bits(value), cond);
3449 uint16_t value_high = High16Bits(value);
3450 if (value_high != 0) {
3451 movt(IP, value_high, cond);
3452 }
3453 cmp(rn, ShifterOperand(IP), cond);
3454 }
3455}
Nicolas Geoffray3bcc8ea2014-11-28 15:00:02 +00003456
Dave Allison65fcc2c2014-04-28 13:45:27 -07003457void Thumb2Assembler::LoadImmediate(Register rd, int32_t value, Condition cond) {
3458 ShifterOperand shifter_op;
Nicolas Geoffray3bcc8ea2014-11-28 15:00:02 +00003459 if (ShifterOperandCanHold(rd, R0, MOV, value, &shifter_op)) {
Dave Allison65fcc2c2014-04-28 13:45:27 -07003460 mov(rd, shifter_op, cond);
Nicolas Geoffray3bcc8ea2014-11-28 15:00:02 +00003461 } else if (ShifterOperandCanHold(rd, R0, MVN, ~value, &shifter_op)) {
Dave Allison65fcc2c2014-04-28 13:45:27 -07003462 mvn(rd, shifter_op, cond);
3463 } else {
3464 movw(rd, Low16Bits(value), cond);
3465 uint16_t value_high = High16Bits(value);
3466 if (value_high != 0) {
3467 movt(rd, value_high, cond);
3468 }
3469 }
3470}
3471
Nicolas Geoffray3bcc8ea2014-11-28 15:00:02 +00003472
Dave Allison65fcc2c2014-04-28 13:45:27 -07003473// Implementation note: this method must emit at most one instruction when
3474// Address::CanHoldLoadOffsetThumb.
3475void Thumb2Assembler::LoadFromOffset(LoadOperandType type,
3476 Register reg,
3477 Register base,
3478 int32_t offset,
3479 Condition cond) {
3480 if (!Address::CanHoldLoadOffsetThumb(type, offset)) {
Roland Levillain775ef492014-11-04 17:43:11 +00003481 CHECK_NE(base, IP);
Dave Allison65fcc2c2014-04-28 13:45:27 -07003482 LoadImmediate(IP, offset, cond);
3483 add(IP, IP, ShifterOperand(base), cond);
3484 base = IP;
3485 offset = 0;
3486 }
3487 CHECK(Address::CanHoldLoadOffsetThumb(type, offset));
3488 switch (type) {
3489 case kLoadSignedByte:
3490 ldrsb(reg, Address(base, offset), cond);
3491 break;
3492 case kLoadUnsignedByte:
3493 ldrb(reg, Address(base, offset), cond);
3494 break;
3495 case kLoadSignedHalfword:
3496 ldrsh(reg, Address(base, offset), cond);
3497 break;
3498 case kLoadUnsignedHalfword:
3499 ldrh(reg, Address(base, offset), cond);
3500 break;
3501 case kLoadWord:
3502 ldr(reg, Address(base, offset), cond);
3503 break;
3504 case kLoadWordPair:
3505 ldrd(reg, Address(base, offset), cond);
3506 break;
3507 default:
3508 LOG(FATAL) << "UNREACHABLE";
Ian Rogers2c4257b2014-10-24 14:20:06 -07003509 UNREACHABLE();
Dave Allison65fcc2c2014-04-28 13:45:27 -07003510 }
3511}
3512
3513
3514// Implementation note: this method must emit at most one instruction when
3515// Address::CanHoldLoadOffsetThumb, as expected by JIT::GuardedLoadFromOffset.
3516void Thumb2Assembler::LoadSFromOffset(SRegister reg,
3517 Register base,
3518 int32_t offset,
3519 Condition cond) {
3520 if (!Address::CanHoldLoadOffsetThumb(kLoadSWord, offset)) {
3521 CHECK_NE(base, IP);
3522 LoadImmediate(IP, offset, cond);
3523 add(IP, IP, ShifterOperand(base), cond);
3524 base = IP;
3525 offset = 0;
3526 }
3527 CHECK(Address::CanHoldLoadOffsetThumb(kLoadSWord, offset));
3528 vldrs(reg, Address(base, offset), cond);
3529}
3530
3531
3532// Implementation note: this method must emit at most one instruction when
3533// Address::CanHoldLoadOffsetThumb, as expected by JIT::GuardedLoadFromOffset.
3534void Thumb2Assembler::LoadDFromOffset(DRegister reg,
3535 Register base,
3536 int32_t offset,
3537 Condition cond) {
3538 if (!Address::CanHoldLoadOffsetThumb(kLoadDWord, offset)) {
3539 CHECK_NE(base, IP);
3540 LoadImmediate(IP, offset, cond);
3541 add(IP, IP, ShifterOperand(base), cond);
3542 base = IP;
3543 offset = 0;
3544 }
3545 CHECK(Address::CanHoldLoadOffsetThumb(kLoadDWord, offset));
3546 vldrd(reg, Address(base, offset), cond);
3547}
3548
3549
3550// Implementation note: this method must emit at most one instruction when
3551// Address::CanHoldStoreOffsetThumb.
3552void Thumb2Assembler::StoreToOffset(StoreOperandType type,
3553 Register reg,
3554 Register base,
3555 int32_t offset,
3556 Condition cond) {
Roland Levillain775ef492014-11-04 17:43:11 +00003557 Register tmp_reg = kNoRegister;
Dave Allison65fcc2c2014-04-28 13:45:27 -07003558 if (!Address::CanHoldStoreOffsetThumb(type, offset)) {
Roland Levillain775ef492014-11-04 17:43:11 +00003559 CHECK_NE(base, IP);
Roland Levillain23f02f32015-08-25 18:23:20 +01003560 if ((reg != IP) &&
3561 ((type != kStoreWordPair) || (reg + 1 != IP))) {
Roland Levillain775ef492014-11-04 17:43:11 +00003562 tmp_reg = IP;
3563 } else {
Roland Levillain4af147e2015-04-07 13:54:49 +01003564 // Be careful not to use IP twice (for `reg` (or `reg` + 1 in
Roland Levillain23f02f32015-08-25 18:23:20 +01003565 // the case of a word-pair store) and `base`) to build the
3566 // Address object used by the store instruction(s) below.
3567 // Instead, save R5 on the stack (or R6 if R5 is already used by
3568 // `base`), use it as secondary temporary register, and restore
3569 // it after the store instruction has been emitted.
3570 tmp_reg = (base != R5) ? R5 : R6;
Roland Levillain775ef492014-11-04 17:43:11 +00003571 Push(tmp_reg);
3572 if (base == SP) {
3573 offset += kRegisterSize;
3574 }
3575 }
3576 LoadImmediate(tmp_reg, offset, cond);
Vladimir Marko73cf0fb2015-07-30 15:07:22 +01003577 add(tmp_reg, tmp_reg, ShifterOperand(base), AL);
Roland Levillain775ef492014-11-04 17:43:11 +00003578 base = tmp_reg;
Dave Allison65fcc2c2014-04-28 13:45:27 -07003579 offset = 0;
3580 }
3581 CHECK(Address::CanHoldStoreOffsetThumb(type, offset));
3582 switch (type) {
3583 case kStoreByte:
3584 strb(reg, Address(base, offset), cond);
3585 break;
3586 case kStoreHalfword:
3587 strh(reg, Address(base, offset), cond);
3588 break;
3589 case kStoreWord:
3590 str(reg, Address(base, offset), cond);
3591 break;
3592 case kStoreWordPair:
3593 strd(reg, Address(base, offset), cond);
3594 break;
3595 default:
3596 LOG(FATAL) << "UNREACHABLE";
Ian Rogers2c4257b2014-10-24 14:20:06 -07003597 UNREACHABLE();
Dave Allison65fcc2c2014-04-28 13:45:27 -07003598 }
Roland Levillain23f02f32015-08-25 18:23:20 +01003599 if ((tmp_reg != kNoRegister) && (tmp_reg != IP)) {
3600 CHECK((tmp_reg == R5) || (tmp_reg == R6));
Roland Levillain775ef492014-11-04 17:43:11 +00003601 Pop(tmp_reg);
3602 }
Dave Allison65fcc2c2014-04-28 13:45:27 -07003603}
3604
3605
3606// Implementation note: this method must emit at most one instruction when
3607// Address::CanHoldStoreOffsetThumb, as expected by JIT::GuardedStoreToOffset.
3608void Thumb2Assembler::StoreSToOffset(SRegister reg,
3609 Register base,
3610 int32_t offset,
3611 Condition cond) {
3612 if (!Address::CanHoldStoreOffsetThumb(kStoreSWord, offset)) {
3613 CHECK_NE(base, IP);
3614 LoadImmediate(IP, offset, cond);
3615 add(IP, IP, ShifterOperand(base), cond);
3616 base = IP;
3617 offset = 0;
3618 }
3619 CHECK(Address::CanHoldStoreOffsetThumb(kStoreSWord, offset));
3620 vstrs(reg, Address(base, offset), cond);
3621}
3622
3623
3624// Implementation note: this method must emit at most one instruction when
3625// Address::CanHoldStoreOffsetThumb, as expected by JIT::GuardedStoreSToOffset.
3626void Thumb2Assembler::StoreDToOffset(DRegister reg,
3627 Register base,
3628 int32_t offset,
3629 Condition cond) {
3630 if (!Address::CanHoldStoreOffsetThumb(kStoreDWord, offset)) {
3631 CHECK_NE(base, IP);
3632 LoadImmediate(IP, offset, cond);
3633 add(IP, IP, ShifterOperand(base), cond);
3634 base = IP;
3635 offset = 0;
3636 }
3637 CHECK(Address::CanHoldStoreOffsetThumb(kStoreDWord, offset));
3638 vstrd(reg, Address(base, offset), cond);
3639}
3640
3641
3642void Thumb2Assembler::MemoryBarrier(ManagedRegister mscratch) {
3643 CHECK_EQ(mscratch.AsArm().AsCoreRegister(), R12);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003644 dmb(SY);
3645}
3646
3647
3648void Thumb2Assembler::dmb(DmbOptions flavor) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003649 int32_t encoding = 0xf3bf8f50; // dmb in T1 encoding.
3650 Emit32(encoding | flavor);
Dave Allison65fcc2c2014-04-28 13:45:27 -07003651}
3652
3653
3654void Thumb2Assembler::CompareAndBranchIfZero(Register r, Label* label) {
Nicolas Geoffray2bcb4312015-07-01 12:22:56 +01003655 if (CanRelocateBranches() && IsLowRegister(r) && !label->IsBound()) {
Nicolas Geoffrayd56376c2015-05-21 12:32:34 +00003656 cbz(r, label);
3657 } else {
3658 cmp(r, ShifterOperand(0));
3659 b(label, EQ);
3660 }
3661}
3662
3663
Dave Allison65fcc2c2014-04-28 13:45:27 -07003664void Thumb2Assembler::CompareAndBranchIfNonZero(Register r, Label* label) {
Nicolas Geoffray2bcb4312015-07-01 12:22:56 +01003665 if (CanRelocateBranches() && IsLowRegister(r) && !label->IsBound()) {
Nicolas Geoffrayd126ba12015-05-20 11:25:27 +01003666 cbnz(r, label);
3667 } else {
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01003668 cmp(r, ShifterOperand(0));
3669 b(label, NE);
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01003670 }
Dave Allison65fcc2c2014-04-28 13:45:27 -07003671}
Andreas Gampe7cffc3b2015-10-19 21:31:53 -07003672
3673JumpTable* Thumb2Assembler::CreateJumpTable(std::vector<Label*>&& labels, Register base_reg) {
3674 jump_tables_.emplace_back(std::move(labels));
3675 JumpTable* table = &jump_tables_.back();
3676 DCHECK(!table->GetLabel()->IsBound());
3677
3678 bool use32bit = IsForced32Bit() || IsHighRegister(base_reg);
3679 uint32_t location = buffer_.Size();
3680 Fixup::Size size = use32bit ? Fixup::kLiteralAddr4KiB : Fixup::kLiteralAddr1KiB;
3681 FixupId fixup_id = AddFixup(Fixup::LoadLiteralAddress(location, base_reg, size));
3682 Emit16(static_cast<uint16_t>(table->GetLabel()->position_));
3683 table->GetLabel()->LinkTo(fixup_id);
3684 if (use32bit) {
3685 Emit16(0);
3686 }
3687 DCHECK_EQ(location + GetFixup(fixup_id)->GetSizeInBytes(), buffer_.Size());
3688
3689 return table;
3690}
3691
3692void Thumb2Assembler::EmitJumpTableDispatch(JumpTable* jump_table, Register displacement_reg) {
3693 CHECK(!IsForced32Bit()) << "Forced 32-bit dispatch not implemented yet";
3694 // 32-bit ADD doesn't support PC as an input, so we need a two-instruction sequence:
3695 // SUB ip, ip, #0
3696 // ADD pc, ip, reg
3697 // TODO: Implement.
3698
3699 // The anchor's position needs to be fixed up before we can compute offsets - so make it a tracked
3700 // label.
3701 BindTrackedLabel(jump_table->GetAnchorLabel());
3702
3703 add(PC, PC, ShifterOperand(displacement_reg));
3704}
3705
Dave Allison65fcc2c2014-04-28 13:45:27 -07003706} // namespace arm
3707} // namespace art