blob: e262134e6e9930d47600688d8676ee9dc09cb340 [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
19#include "base/logging.h"
20#include "entrypoints/quick/quick_entrypoints.h"
21#include "offsets.h"
22#include "thread.h"
23#include "utils.h"
24
25namespace art {
26namespace arm {
27
Nicolas Geoffray3bcc8ea2014-11-28 15:00:02 +000028bool Thumb2Assembler::ShifterOperandCanHold(Register rd,
29 Register rn,
30 Opcode opcode,
31 uint32_t immediate,
32 ShifterOperand* shifter_op) {
33 shifter_op->type_ = ShifterOperand::kImmediate;
34 shifter_op->immed_ = immediate;
35 shifter_op->is_shift_ = false;
36 shifter_op->is_rotate_ = false;
37 switch (opcode) {
38 case ADD:
39 case SUB:
40 if (rn == SP) {
41 if (rd == SP) {
42 return immediate < (1 << 9); // 9 bits allowed.
43 } else {
44 return immediate < (1 << 12); // 12 bits.
45 }
46 }
47 if (immediate < (1 << 12)) { // Less than (or equal to) 12 bits can always be done.
48 return true;
49 }
50 return ArmAssembler::ModifiedImmediate(immediate) != kInvalidModifiedImmediate;
51
52 case MOV:
53 // TODO: Support less than or equal to 12bits.
54 return ArmAssembler::ModifiedImmediate(immediate) != kInvalidModifiedImmediate;
55 case MVN:
56 default:
57 return ArmAssembler::ModifiedImmediate(immediate) != kInvalidModifiedImmediate;
58 }
59}
60
Dave Allison65fcc2c2014-04-28 13:45:27 -070061void Thumb2Assembler::and_(Register rd, Register rn, const ShifterOperand& so,
62 Condition cond) {
63 EmitDataProcessing(cond, AND, 0, rn, rd, so);
64}
65
66
67void Thumb2Assembler::eor(Register rd, Register rn, const ShifterOperand& so,
68 Condition cond) {
69 EmitDataProcessing(cond, EOR, 0, rn, rd, so);
70}
71
72
73void Thumb2Assembler::sub(Register rd, Register rn, const ShifterOperand& so,
74 Condition cond) {
75 EmitDataProcessing(cond, SUB, 0, rn, rd, so);
76}
77
78
79void Thumb2Assembler::rsb(Register rd, Register rn, const ShifterOperand& so,
80 Condition cond) {
81 EmitDataProcessing(cond, RSB, 0, rn, rd, so);
82}
83
84
85void Thumb2Assembler::rsbs(Register rd, Register rn, const ShifterOperand& so,
86 Condition cond) {
87 EmitDataProcessing(cond, RSB, 1, rn, rd, so);
88}
89
90
91void Thumb2Assembler::add(Register rd, Register rn, const ShifterOperand& so,
92 Condition cond) {
93 EmitDataProcessing(cond, ADD, 0, rn, rd, so);
94}
95
96
97void Thumb2Assembler::adds(Register rd, Register rn, const ShifterOperand& so,
98 Condition cond) {
99 EmitDataProcessing(cond, ADD, 1, rn, rd, so);
100}
101
102
103void Thumb2Assembler::subs(Register rd, Register rn, const ShifterOperand& so,
104 Condition cond) {
105 EmitDataProcessing(cond, SUB, 1, rn, rd, so);
106}
107
108
109void Thumb2Assembler::adc(Register rd, Register rn, const ShifterOperand& so,
110 Condition cond) {
111 EmitDataProcessing(cond, ADC, 0, rn, rd, so);
112}
113
114
115void Thumb2Assembler::sbc(Register rd, Register rn, const ShifterOperand& so,
116 Condition cond) {
117 EmitDataProcessing(cond, SBC, 0, rn, rd, so);
118}
119
120
121void Thumb2Assembler::rsc(Register rd, Register rn, const ShifterOperand& so,
122 Condition cond) {
123 EmitDataProcessing(cond, RSC, 0, rn, rd, so);
124}
125
126
127void Thumb2Assembler::tst(Register rn, const ShifterOperand& so, Condition cond) {
128 CHECK_NE(rn, PC); // Reserve tst pc instruction for exception handler marker.
129 EmitDataProcessing(cond, TST, 1, rn, R0, so);
130}
131
132
133void Thumb2Assembler::teq(Register rn, const ShifterOperand& so, Condition cond) {
134 CHECK_NE(rn, PC); // Reserve teq pc instruction for exception handler marker.
135 EmitDataProcessing(cond, TEQ, 1, rn, R0, so);
136}
137
138
139void Thumb2Assembler::cmp(Register rn, const ShifterOperand& so, Condition cond) {
140 EmitDataProcessing(cond, CMP, 1, rn, R0, so);
141}
142
143
144void Thumb2Assembler::cmn(Register rn, const ShifterOperand& so, Condition cond) {
145 EmitDataProcessing(cond, CMN, 1, rn, R0, so);
146}
147
148
149void Thumb2Assembler::orr(Register rd, Register rn,
150 const ShifterOperand& so, Condition cond) {
151 EmitDataProcessing(cond, ORR, 0, rn, rd, so);
152}
153
154
155void Thumb2Assembler::orrs(Register rd, Register rn,
156 const ShifterOperand& so, Condition cond) {
157 EmitDataProcessing(cond, ORR, 1, rn, rd, so);
158}
159
160
161void Thumb2Assembler::mov(Register rd, const ShifterOperand& so, Condition cond) {
162 EmitDataProcessing(cond, MOV, 0, R0, rd, so);
163}
164
165
166void Thumb2Assembler::movs(Register rd, const ShifterOperand& so, Condition cond) {
167 EmitDataProcessing(cond, MOV, 1, R0, rd, so);
168}
169
170
171void Thumb2Assembler::bic(Register rd, Register rn, const ShifterOperand& so,
172 Condition cond) {
173 EmitDataProcessing(cond, BIC, 0, rn, rd, so);
174}
175
176
177void Thumb2Assembler::mvn(Register rd, const ShifterOperand& so, Condition cond) {
178 EmitDataProcessing(cond, MVN, 0, R0, rd, so);
179}
180
181
182void Thumb2Assembler::mvns(Register rd, const ShifterOperand& so, Condition cond) {
183 EmitDataProcessing(cond, MVN, 1, R0, rd, so);
184}
185
186
187void Thumb2Assembler::mul(Register rd, Register rn, Register rm, Condition cond) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700188 CheckCondition(cond);
189
Dave Allison65fcc2c2014-04-28 13:45:27 -0700190 if (rd == rm && !IsHighRegister(rd) && !IsHighRegister(rn) && !force_32bit_) {
191 // 16 bit.
192 int16_t encoding = B14 | B9 | B8 | B6 |
193 rn << 3 | rd;
194 Emit16(encoding);
195 } else {
196 // 32 bit.
Andreas Gampec8ccf682014-09-29 20:07:43 -0700197 uint32_t op1 = 0U /* 0b000 */;
198 uint32_t op2 = 0U /* 0b00 */;
Dave Allison65fcc2c2014-04-28 13:45:27 -0700199 int32_t encoding = B31 | B30 | B29 | B28 | B27 | B25 | B24 |
200 op1 << 20 |
201 B15 | B14 | B13 | B12 |
202 op2 << 4 |
203 static_cast<uint32_t>(rd) << 8 |
204 static_cast<uint32_t>(rn) << 16 |
205 static_cast<uint32_t>(rm);
206
207 Emit32(encoding);
208 }
209}
210
211
212void Thumb2Assembler::mla(Register rd, Register rn, Register rm, Register ra,
213 Condition cond) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700214 CheckCondition(cond);
215
Andreas Gampec8ccf682014-09-29 20:07:43 -0700216 uint32_t op1 = 0U /* 0b000 */;
217 uint32_t op2 = 0U /* 0b00 */;
Dave Allison65fcc2c2014-04-28 13:45:27 -0700218 int32_t encoding = B31 | B30 | B29 | B28 | B27 | B25 | B24 |
219 op1 << 20 |
220 op2 << 4 |
221 static_cast<uint32_t>(rd) << 8 |
222 static_cast<uint32_t>(ra) << 12 |
223 static_cast<uint32_t>(rn) << 16 |
224 static_cast<uint32_t>(rm);
225
226 Emit32(encoding);
227}
228
229
230void Thumb2Assembler::mls(Register rd, Register rn, Register rm, Register ra,
231 Condition cond) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700232 CheckCondition(cond);
233
Andreas Gampec8ccf682014-09-29 20:07:43 -0700234 uint32_t op1 = 0U /* 0b000 */;
235 uint32_t op2 = 01 /* 0b01 */;
Dave Allison65fcc2c2014-04-28 13:45:27 -0700236 int32_t encoding = B31 | B30 | B29 | B28 | B27 | B25 | B24 |
237 op1 << 20 |
238 op2 << 4 |
239 static_cast<uint32_t>(rd) << 8 |
240 static_cast<uint32_t>(ra) << 12 |
241 static_cast<uint32_t>(rn) << 16 |
242 static_cast<uint32_t>(rm);
243
244 Emit32(encoding);
245}
246
247
248void Thumb2Assembler::umull(Register rd_lo, Register rd_hi, Register rn,
249 Register rm, Condition cond) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700250 CheckCondition(cond);
251
Andreas Gampec8ccf682014-09-29 20:07:43 -0700252 uint32_t op1 = 2U /* 0b010; */;
253 uint32_t op2 = 0U /* 0b0000 */;
Dave Allison65fcc2c2014-04-28 13:45:27 -0700254 int32_t encoding = B31 | B30 | B29 | B28 | B27 | B25 | B24 | B23 |
255 op1 << 20 |
256 op2 << 4 |
257 static_cast<uint32_t>(rd_lo) << 12 |
258 static_cast<uint32_t>(rd_hi) << 8 |
259 static_cast<uint32_t>(rn) << 16 |
260 static_cast<uint32_t>(rm);
261
262 Emit32(encoding);
263}
264
265
266void Thumb2Assembler::sdiv(Register rd, Register rn, Register rm, Condition cond) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700267 CheckCondition(cond);
268
Andreas Gampec8ccf682014-09-29 20:07:43 -0700269 uint32_t op1 = 1U /* 0b001 */;
270 uint32_t op2 = 15U /* 0b1111 */;
Dave Allison65fcc2c2014-04-28 13:45:27 -0700271 int32_t encoding = B31 | B30 | B29 | B28 | B27 | B25 | B24 | B23 | B20 |
272 op1 << 20 |
273 op2 << 4 |
274 0xf << 12 |
275 static_cast<uint32_t>(rd) << 8 |
276 static_cast<uint32_t>(rn) << 16 |
277 static_cast<uint32_t>(rm);
278
279 Emit32(encoding);
280}
281
282
283void Thumb2Assembler::udiv(Register rd, Register rn, Register rm, Condition cond) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700284 CheckCondition(cond);
285
Andreas Gampec8ccf682014-09-29 20:07:43 -0700286 uint32_t op1 = 1U /* 0b001 */;
287 uint32_t op2 = 15U /* 0b1111 */;
Dave Allison65fcc2c2014-04-28 13:45:27 -0700288 int32_t encoding = B31 | B30 | B29 | B28 | B27 | B25 | B24 | B23 | B21 | B20 |
289 op1 << 20 |
290 op2 << 4 |
291 0xf << 12 |
292 static_cast<uint32_t>(rd) << 8 |
293 static_cast<uint32_t>(rn) << 16 |
294 static_cast<uint32_t>(rm);
295
296 Emit32(encoding);
297}
298
299
Roland Levillain51d3fc42014-11-13 14:11:42 +0000300void Thumb2Assembler::sbfx(Register rd, Register rn, uint32_t lsb, uint32_t width, Condition cond) {
301 CheckCondition(cond);
302 CHECK_LE(lsb, 31U);
303 CHECK(1U <= width && width <= 32U) << width;
304 uint32_t widthminus1 = width - 1;
305 uint32_t imm2 = lsb & (B1 | B0); // Bits 0-1 of `lsb`.
306 uint32_t imm3 = (lsb & (B4 | B3 | B2)) >> 2; // Bits 2-4 of `lsb`.
307
308 uint32_t op = 20U /* 0b10100 */;
309 int32_t encoding = B31 | B30 | B29 | B28 | B25 |
310 op << 20 |
311 static_cast<uint32_t>(rn) << 16 |
312 imm3 << 12 |
313 static_cast<uint32_t>(rd) << 8 |
314 imm2 << 6 |
315 widthminus1;
316
317 Emit32(encoding);
318}
319
320
Roland Levillain981e4542014-11-14 11:47:14 +0000321void Thumb2Assembler::ubfx(Register rd, Register rn, uint32_t lsb, uint32_t width, Condition cond) {
322 CheckCondition(cond);
323 CHECK_LE(lsb, 31U);
324 CHECK(1U <= width && width <= 32U) << width;
325 uint32_t widthminus1 = width - 1;
326 uint32_t imm2 = lsb & (B1 | B0); // Bits 0-1 of `lsb`.
327 uint32_t imm3 = (lsb & (B4 | B3 | B2)) >> 2; // Bits 2-4 of `lsb`.
328
329 uint32_t op = 28U /* 0b11100 */;
330 int32_t encoding = B31 | B30 | B29 | B28 | B25 |
331 op << 20 |
332 static_cast<uint32_t>(rn) << 16 |
333 imm3 << 12 |
334 static_cast<uint32_t>(rd) << 8 |
335 imm2 << 6 |
336 widthminus1;
337
338 Emit32(encoding);
339}
340
341
Dave Allison65fcc2c2014-04-28 13:45:27 -0700342void Thumb2Assembler::ldr(Register rd, const Address& ad, Condition cond) {
343 EmitLoadStore(cond, true, false, false, false, rd, ad);
344}
345
346
347void Thumb2Assembler::str(Register rd, const Address& ad, Condition cond) {
348 EmitLoadStore(cond, false, false, false, false, rd, ad);
349}
350
351
352void Thumb2Assembler::ldrb(Register rd, const Address& ad, Condition cond) {
353 EmitLoadStore(cond, true, true, false, false, rd, ad);
354}
355
356
357void Thumb2Assembler::strb(Register rd, const Address& ad, Condition cond) {
358 EmitLoadStore(cond, false, true, false, false, rd, ad);
359}
360
361
362void Thumb2Assembler::ldrh(Register rd, const Address& ad, Condition cond) {
363 EmitLoadStore(cond, true, false, true, false, rd, ad);
364}
365
366
367void Thumb2Assembler::strh(Register rd, const Address& ad, Condition cond) {
368 EmitLoadStore(cond, false, false, true, false, rd, ad);
369}
370
371
372void Thumb2Assembler::ldrsb(Register rd, const Address& ad, Condition cond) {
373 EmitLoadStore(cond, true, true, false, true, rd, ad);
374}
375
376
377void Thumb2Assembler::ldrsh(Register rd, const Address& ad, Condition cond) {
378 EmitLoadStore(cond, true, false, true, true, rd, ad);
379}
380
381
382void Thumb2Assembler::ldrd(Register rd, const Address& ad, Condition cond) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700383 CheckCondition(cond);
Dave Allison65fcc2c2014-04-28 13:45:27 -0700384 CHECK_EQ(rd % 2, 0);
385 // This is different from other loads. The encoding is like ARM.
386 int32_t encoding = B31 | B30 | B29 | B27 | B22 | B20 |
387 static_cast<int32_t>(rd) << 12 |
388 (static_cast<int32_t>(rd) + 1) << 8 |
389 ad.encodingThumbLdrdStrd();
390 Emit32(encoding);
391}
392
393
394void Thumb2Assembler::strd(Register rd, const Address& ad, Condition cond) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700395 CheckCondition(cond);
Dave Allison65fcc2c2014-04-28 13:45:27 -0700396 CHECK_EQ(rd % 2, 0);
397 // This is different from other loads. The encoding is like ARM.
398 int32_t encoding = B31 | B30 | B29 | B27 | B22 |
399 static_cast<int32_t>(rd) << 12 |
400 (static_cast<int32_t>(rd) + 1) << 8 |
401 ad.encodingThumbLdrdStrd();
402 Emit32(encoding);
403}
404
405
406void Thumb2Assembler::ldm(BlockAddressMode am,
407 Register base,
408 RegList regs,
409 Condition cond) {
Vladimir Markoe8469c12014-11-26 18:09:30 +0000410 CHECK_NE(regs, 0u); // Do not use ldm if there's nothing to load.
411 if (IsPowerOfTwo(regs)) {
Dave Allison65fcc2c2014-04-28 13:45:27 -0700412 // Thumb doesn't support one reg in the list.
413 // Find the register number.
Vladimir Markoe8469c12014-11-26 18:09:30 +0000414 int reg = CTZ(static_cast<uint32_t>(regs));
Dave Allison65fcc2c2014-04-28 13:45:27 -0700415 CHECK_LT(reg, 16);
Dave Allison45fdb932014-06-25 12:37:10 -0700416 CHECK(am == DB_W); // Only writeback is supported.
Dave Allison65fcc2c2014-04-28 13:45:27 -0700417 ldr(static_cast<Register>(reg), Address(base, kRegisterSize, Address::PostIndex), cond);
418 } else {
419 EmitMultiMemOp(cond, am, true, base, regs);
420 }
421}
422
423
424void Thumb2Assembler::stm(BlockAddressMode am,
425 Register base,
426 RegList regs,
427 Condition cond) {
Vladimir Markoe8469c12014-11-26 18:09:30 +0000428 CHECK_NE(regs, 0u); // Do not use stm if there's nothing to store.
429 if (IsPowerOfTwo(regs)) {
Dave Allison65fcc2c2014-04-28 13:45:27 -0700430 // Thumb doesn't support one reg in the list.
431 // Find the register number.
Vladimir Markoe8469c12014-11-26 18:09:30 +0000432 int reg = CTZ(static_cast<uint32_t>(regs));
Dave Allison65fcc2c2014-04-28 13:45:27 -0700433 CHECK_LT(reg, 16);
Dave Allison45fdb932014-06-25 12:37:10 -0700434 CHECK(am == IA || am == IA_W);
435 Address::Mode strmode = am == IA ? Address::PreIndex : Address::Offset;
Dave Allison65fcc2c2014-04-28 13:45:27 -0700436 str(static_cast<Register>(reg), Address(base, -kRegisterSize, strmode), cond);
437 } else {
438 EmitMultiMemOp(cond, am, false, base, regs);
439 }
440}
441
442
443bool Thumb2Assembler::vmovs(SRegister sd, float s_imm, Condition cond) {
444 uint32_t imm32 = bit_cast<uint32_t, float>(s_imm);
445 if (((imm32 & ((1 << 19) - 1)) == 0) &&
446 ((((imm32 >> 25) & ((1 << 6) - 1)) == (1 << 5)) ||
447 (((imm32 >> 25) & ((1 << 6) - 1)) == ((1 << 5) -1)))) {
448 uint8_t imm8 = ((imm32 >> 31) << 7) | (((imm32 >> 29) & 1) << 6) |
449 ((imm32 >> 19) & ((1 << 6) -1));
450 EmitVFPsss(cond, B23 | B21 | B20 | ((imm8 >> 4)*B16) | (imm8 & 0xf),
451 sd, S0, S0);
452 return true;
453 }
454 return false;
455}
456
457
458bool Thumb2Assembler::vmovd(DRegister dd, double d_imm, Condition cond) {
459 uint64_t imm64 = bit_cast<uint64_t, double>(d_imm);
460 if (((imm64 & ((1LL << 48) - 1)) == 0) &&
461 ((((imm64 >> 54) & ((1 << 9) - 1)) == (1 << 8)) ||
462 (((imm64 >> 54) & ((1 << 9) - 1)) == ((1 << 8) -1)))) {
463 uint8_t imm8 = ((imm64 >> 63) << 7) | (((imm64 >> 61) & 1) << 6) |
464 ((imm64 >> 48) & ((1 << 6) -1));
465 EmitVFPddd(cond, B23 | B21 | B20 | ((imm8 >> 4)*B16) | B8 | (imm8 & 0xf),
466 dd, D0, D0);
467 return true;
468 }
469 return false;
470}
471
472
473void Thumb2Assembler::vmovs(SRegister sd, SRegister sm, Condition cond) {
474 EmitVFPsss(cond, B23 | B21 | B20 | B6, sd, S0, sm);
475}
476
477
478void Thumb2Assembler::vmovd(DRegister dd, DRegister dm, Condition cond) {
479 EmitVFPddd(cond, B23 | B21 | B20 | B6, dd, D0, dm);
480}
481
482
483void Thumb2Assembler::vadds(SRegister sd, SRegister sn, SRegister sm,
484 Condition cond) {
485 EmitVFPsss(cond, B21 | B20, sd, sn, sm);
486}
487
488
489void Thumb2Assembler::vaddd(DRegister dd, DRegister dn, DRegister dm,
490 Condition cond) {
491 EmitVFPddd(cond, B21 | B20, dd, dn, dm);
492}
493
494
495void Thumb2Assembler::vsubs(SRegister sd, SRegister sn, SRegister sm,
496 Condition cond) {
497 EmitVFPsss(cond, B21 | B20 | B6, sd, sn, sm);
498}
499
500
501void Thumb2Assembler::vsubd(DRegister dd, DRegister dn, DRegister dm,
502 Condition cond) {
503 EmitVFPddd(cond, B21 | B20 | B6, dd, dn, dm);
504}
505
506
507void Thumb2Assembler::vmuls(SRegister sd, SRegister sn, SRegister sm,
508 Condition cond) {
509 EmitVFPsss(cond, B21, sd, sn, sm);
510}
511
512
513void Thumb2Assembler::vmuld(DRegister dd, DRegister dn, DRegister dm,
514 Condition cond) {
515 EmitVFPddd(cond, B21, dd, dn, dm);
516}
517
518
519void Thumb2Assembler::vmlas(SRegister sd, SRegister sn, SRegister sm,
520 Condition cond) {
521 EmitVFPsss(cond, 0, sd, sn, sm);
522}
523
524
525void Thumb2Assembler::vmlad(DRegister dd, DRegister dn, DRegister dm,
526 Condition cond) {
527 EmitVFPddd(cond, 0, dd, dn, dm);
528}
529
530
531void Thumb2Assembler::vmlss(SRegister sd, SRegister sn, SRegister sm,
532 Condition cond) {
533 EmitVFPsss(cond, B6, sd, sn, sm);
534}
535
536
537void Thumb2Assembler::vmlsd(DRegister dd, DRegister dn, DRegister dm,
538 Condition cond) {
539 EmitVFPddd(cond, B6, dd, dn, dm);
540}
541
542
543void Thumb2Assembler::vdivs(SRegister sd, SRegister sn, SRegister sm,
544 Condition cond) {
545 EmitVFPsss(cond, B23, sd, sn, sm);
546}
547
548
549void Thumb2Assembler::vdivd(DRegister dd, DRegister dn, DRegister dm,
550 Condition cond) {
551 EmitVFPddd(cond, B23, dd, dn, dm);
552}
553
554
555void Thumb2Assembler::vabss(SRegister sd, SRegister sm, Condition cond) {
556 EmitVFPsss(cond, B23 | B21 | B20 | B7 | B6, sd, S0, sm);
557}
558
559
560void Thumb2Assembler::vabsd(DRegister dd, DRegister dm, Condition cond) {
561 EmitVFPddd(cond, B23 | B21 | B20 | B7 | B6, dd, D0, dm);
562}
563
564
565void Thumb2Assembler::vnegs(SRegister sd, SRegister sm, Condition cond) {
566 EmitVFPsss(cond, B23 | B21 | B20 | B16 | B6, sd, S0, sm);
567}
568
569
570void Thumb2Assembler::vnegd(DRegister dd, DRegister dm, Condition cond) {
571 EmitVFPddd(cond, B23 | B21 | B20 | B16 | B6, dd, D0, dm);
572}
573
574
575void Thumb2Assembler::vsqrts(SRegister sd, SRegister sm, Condition cond) {
576 EmitVFPsss(cond, B23 | B21 | B20 | B16 | B7 | B6, sd, S0, sm);
577}
578
579void Thumb2Assembler::vsqrtd(DRegister dd, DRegister dm, Condition cond) {
580 EmitVFPddd(cond, B23 | B21 | B20 | B16 | B7 | B6, dd, D0, dm);
581}
582
583
584void Thumb2Assembler::vcvtsd(SRegister sd, DRegister dm, Condition cond) {
585 EmitVFPsd(cond, B23 | B21 | B20 | B18 | B17 | B16 | B8 | B7 | B6, sd, dm);
586}
587
588
589void Thumb2Assembler::vcvtds(DRegister dd, SRegister sm, Condition cond) {
590 EmitVFPds(cond, B23 | B21 | B20 | B18 | B17 | B16 | B7 | B6, dd, sm);
591}
592
593
594void Thumb2Assembler::vcvtis(SRegister sd, SRegister sm, Condition cond) {
595 EmitVFPsss(cond, B23 | B21 | B20 | B19 | B18 | B16 | B7 | B6, sd, S0, sm);
596}
597
598
599void Thumb2Assembler::vcvtid(SRegister sd, DRegister dm, Condition cond) {
600 EmitVFPsd(cond, B23 | B21 | B20 | B19 | B18 | B16 | B8 | B7 | B6, sd, dm);
601}
602
603
604void Thumb2Assembler::vcvtsi(SRegister sd, SRegister sm, Condition cond) {
605 EmitVFPsss(cond, B23 | B21 | B20 | B19 | B7 | B6, sd, S0, sm);
606}
607
608
609void Thumb2Assembler::vcvtdi(DRegister dd, SRegister sm, Condition cond) {
610 EmitVFPds(cond, B23 | B21 | B20 | B19 | B8 | B7 | B6, dd, sm);
611}
612
613
614void Thumb2Assembler::vcvtus(SRegister sd, SRegister sm, Condition cond) {
615 EmitVFPsss(cond, B23 | B21 | B20 | B19 | B18 | B7 | B6, sd, S0, sm);
616}
617
618
619void Thumb2Assembler::vcvtud(SRegister sd, DRegister dm, Condition cond) {
620 EmitVFPsd(cond, B23 | B21 | B20 | B19 | B18 | B8 | B7 | B6, sd, dm);
621}
622
623
624void Thumb2Assembler::vcvtsu(SRegister sd, SRegister sm, Condition cond) {
625 EmitVFPsss(cond, B23 | B21 | B20 | B19 | B6, sd, S0, sm);
626}
627
628
629void Thumb2Assembler::vcvtdu(DRegister dd, SRegister sm, Condition cond) {
630 EmitVFPds(cond, B23 | B21 | B20 | B19 | B8 | B6, dd, sm);
631}
632
633
634void Thumb2Assembler::vcmps(SRegister sd, SRegister sm, Condition cond) {
635 EmitVFPsss(cond, B23 | B21 | B20 | B18 | B6, sd, S0, sm);
636}
637
638
639void Thumb2Assembler::vcmpd(DRegister dd, DRegister dm, Condition cond) {
640 EmitVFPddd(cond, B23 | B21 | B20 | B18 | B6, dd, D0, dm);
641}
642
643
644void Thumb2Assembler::vcmpsz(SRegister sd, Condition cond) {
645 EmitVFPsss(cond, B23 | B21 | B20 | B18 | B16 | B6, sd, S0, S0);
646}
647
648
649void Thumb2Assembler::vcmpdz(DRegister dd, Condition cond) {
650 EmitVFPddd(cond, B23 | B21 | B20 | B18 | B16 | B6, dd, D0, D0);
651}
652
653void Thumb2Assembler::b(Label* label, Condition cond) {
654 EmitBranch(cond, label, false, false);
655}
656
657
658void Thumb2Assembler::bl(Label* label, Condition cond) {
659 CheckCondition(cond);
660 EmitBranch(cond, label, true, false);
661}
662
663
664void Thumb2Assembler::blx(Label* label) {
665 EmitBranch(AL, label, true, true);
666}
667
668
669void Thumb2Assembler::MarkExceptionHandler(Label* label) {
670 EmitDataProcessing(AL, TST, 1, PC, R0, ShifterOperand(0));
671 Label l;
672 b(&l);
673 EmitBranch(AL, label, false, false);
674 Bind(&l);
675}
676
677
678void Thumb2Assembler::Emit32(int32_t value) {
679 AssemblerBuffer::EnsureCapacity ensured(&buffer_);
680 buffer_.Emit<int16_t>(value >> 16);
681 buffer_.Emit<int16_t>(value & 0xffff);
682}
683
684
685void Thumb2Assembler::Emit16(int16_t value) {
686 AssemblerBuffer::EnsureCapacity ensured(&buffer_);
687 buffer_.Emit<int16_t>(value);
688}
689
690
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700691bool Thumb2Assembler::Is32BitDataProcessing(Condition cond ATTRIBUTE_UNUSED,
Dave Allison65fcc2c2014-04-28 13:45:27 -0700692 Opcode opcode,
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700693 bool set_cc ATTRIBUTE_UNUSED,
Dave Allison65fcc2c2014-04-28 13:45:27 -0700694 Register rn,
695 Register rd,
696 const ShifterOperand& so) {
697 if (force_32bit_) {
698 return true;
699 }
700
Vladimir Marko5bc561c2014-12-16 17:41:59 +0000701 // Check special case for SP relative ADD and SUB immediate.
702 if ((opcode == ADD || opcode == SUB) && rn == SP && so.IsImmediate()) {
703 // If the immediate is in range, use 16 bit.
704 if (rd == SP) {
705 if (so.GetImmediate() < (1 << 9)) { // 9 bit immediate.
706 return false;
Dave Allison65fcc2c2014-04-28 13:45:27 -0700707 }
Vladimir Marko5bc561c2014-12-16 17:41:59 +0000708 } else if (!IsHighRegister(rd) && opcode == ADD) {
709 if (so.GetImmediate() < (1 << 10)) { // 10 bit immediate.
710 return false;
Dave Allison65fcc2c2014-04-28 13:45:27 -0700711 }
712 }
Vladimir Marko5bc561c2014-12-16 17:41:59 +0000713 }
Dave Allison65fcc2c2014-04-28 13:45:27 -0700714
Vladimir Marko5bc561c2014-12-16 17:41:59 +0000715 bool can_contain_high_register = (opcode == MOV)
716 || ((opcode == ADD) && (rn == rd));
717
718 if (IsHighRegister(rd) || IsHighRegister(rn)) {
719 if (!can_contain_high_register) {
Dave Allison65fcc2c2014-04-28 13:45:27 -0700720 return true;
721 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100722
Vladimir Marko5bc561c2014-12-16 17:41:59 +0000723 // There are high register instructions available for this opcode.
724 // However, there is no actual shift available, neither for ADD nor for MOV (ASR/LSR/LSL/ROR).
725 if (so.IsShift() && (so.GetShift() == RRX || so.GetImmediate() != 0u)) {
726 return true;
727 }
728
729 // The ADD and MOV instructions that work with high registers don't have 16-bit
730 // immediate variants.
731 if (so.IsImmediate()) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100732 return true;
733 }
Dave Allison65fcc2c2014-04-28 13:45:27 -0700734 }
735
736 if (so.IsRegister() && IsHighRegister(so.GetRegister()) && !can_contain_high_register) {
737 return true;
738 }
739
740 // Check for MOV with an ROR.
741 if (opcode == MOV && so.IsRegister() && so.IsShift() && so.GetShift() == ROR) {
742 if (so.GetImmediate() != 0) {
743 return true;
744 }
745 }
746
747 bool rn_is_valid = true;
748
749 // Check for single operand instructions and ADD/SUB.
750 switch (opcode) {
751 case CMP:
752 case MOV:
753 case TST:
754 case MVN:
755 rn_is_valid = false; // There is no Rn for these instructions.
756 break;
757 case TEQ:
758 return true;
759 break;
760 case ADD:
761 case SUB:
762 break;
763 default:
764 if (so.IsRegister() && rd != rn) {
765 return true;
766 }
767 }
768
769 if (so.IsImmediate()) {
770 if (rn_is_valid && rn != rd) {
771 // The only thumb1 instruction with a register and an immediate are ADD and SUB. The
772 // immediate must be 3 bits.
773 if (opcode != ADD && opcode != SUB) {
774 return true;
775 } else {
776 // Check that the immediate is 3 bits for ADD and SUB.
777 if (so.GetImmediate() >= 8) {
778 return true;
779 }
780 }
781 } else {
782 // ADD, SUB, CMP and MOV may be thumb1 only if the immediate is 8 bits.
783 if (!(opcode == ADD || opcode == SUB || opcode == MOV || opcode == CMP)) {
784 return true;
785 } else {
786 if (so.GetImmediate() > 255) {
787 return true;
788 }
789 }
790 }
791 }
792
793 // The instruction can be encoded in 16 bits.
794 return false;
795}
796
797
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700798void Thumb2Assembler::Emit32BitDataProcessing(Condition cond ATTRIBUTE_UNUSED,
Dave Allison65fcc2c2014-04-28 13:45:27 -0700799 Opcode opcode,
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700800 bool set_cc,
Dave Allison65fcc2c2014-04-28 13:45:27 -0700801 Register rn,
802 Register rd,
803 const ShifterOperand& so) {
Andreas Gampec8ccf682014-09-29 20:07:43 -0700804 uint8_t thumb_opcode = 255U /* 0b11111111 */;
Dave Allison65fcc2c2014-04-28 13:45:27 -0700805 switch (opcode) {
Andreas Gampec8ccf682014-09-29 20:07:43 -0700806 case AND: thumb_opcode = 0U /* 0b0000 */; break;
807 case EOR: thumb_opcode = 4U /* 0b0100 */; break;
808 case SUB: thumb_opcode = 13U /* 0b1101 */; break;
809 case RSB: thumb_opcode = 14U /* 0b1110 */; break;
810 case ADD: thumb_opcode = 8U /* 0b1000 */; break;
Andreas Gampe35c68e32014-09-30 08:39:37 -0700811 case ADC: thumb_opcode = 10U /* 0b1010 */; break;
Andreas Gampec8ccf682014-09-29 20:07:43 -0700812 case SBC: thumb_opcode = 11U /* 0b1011 */; break;
Dave Allison65fcc2c2014-04-28 13:45:27 -0700813 case RSC: break;
Andreas Gampec8ccf682014-09-29 20:07:43 -0700814 case TST: thumb_opcode = 0U /* 0b0000 */; set_cc = true; rd = PC; break;
815 case TEQ: thumb_opcode = 4U /* 0b0100 */; set_cc = true; rd = PC; break;
816 case CMP: thumb_opcode = 13U /* 0b1101 */; set_cc = true; rd = PC; break;
817 case CMN: thumb_opcode = 8U /* 0b1000 */; set_cc = true; rd = PC; break;
818 case ORR: thumb_opcode = 2U /* 0b0010 */; break;
819 case MOV: thumb_opcode = 2U /* 0b0010 */; rn = PC; break;
820 case BIC: thumb_opcode = 1U /* 0b0001 */; break;
821 case MVN: thumb_opcode = 3U /* 0b0011 */; rn = PC; break;
Dave Allison65fcc2c2014-04-28 13:45:27 -0700822 default:
823 break;
824 }
825
Andreas Gampec8ccf682014-09-29 20:07:43 -0700826 if (thumb_opcode == 255U /* 0b11111111 */) {
Dave Allison65fcc2c2014-04-28 13:45:27 -0700827 LOG(FATAL) << "Invalid thumb2 opcode " << opcode;
Vladimir Markoe8469c12014-11-26 18:09:30 +0000828 UNREACHABLE();
Dave Allison65fcc2c2014-04-28 13:45:27 -0700829 }
830
831 int32_t encoding = 0;
832 if (so.IsImmediate()) {
833 // Check special cases.
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100834 if ((opcode == SUB || opcode == ADD) && (so.GetImmediate() < (1u << 12))) {
Dave Allison65fcc2c2014-04-28 13:45:27 -0700835 if (opcode == SUB) {
Andreas Gampec8ccf682014-09-29 20:07:43 -0700836 thumb_opcode = 5U /* 0b0101 */;
Dave Allison65fcc2c2014-04-28 13:45:27 -0700837 } else {
838 thumb_opcode = 0;
839 }
840 uint32_t imm = so.GetImmediate();
Dave Allison65fcc2c2014-04-28 13:45:27 -0700841
842 uint32_t i = (imm >> 11) & 1;
Andreas Gampec8ccf682014-09-29 20:07:43 -0700843 uint32_t imm3 = (imm >> 8) & 7U /* 0b111 */;
Dave Allison65fcc2c2014-04-28 13:45:27 -0700844 uint32_t imm8 = imm & 0xff;
845
846 encoding = B31 | B30 | B29 | B28 | B25 |
Dave Allison65fcc2c2014-04-28 13:45:27 -0700847 thumb_opcode << 21 |
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100848 rn << 16 |
Dave Allison65fcc2c2014-04-28 13:45:27 -0700849 rd << 8 |
850 i << 26 |
851 imm3 << 12 |
852 imm8;
853 } else {
854 // Modified immediate.
Dave Allison45fdb932014-06-25 12:37:10 -0700855 uint32_t imm = ModifiedImmediate(so.encodingThumb());
Dave Allison65fcc2c2014-04-28 13:45:27 -0700856 if (imm == kInvalidModifiedImmediate) {
857 LOG(FATAL) << "Immediate value cannot fit in thumb2 modified immediate";
Vladimir Markoe8469c12014-11-26 18:09:30 +0000858 UNREACHABLE();
Dave Allison65fcc2c2014-04-28 13:45:27 -0700859 }
860 encoding = B31 | B30 | B29 | B28 |
861 thumb_opcode << 21 |
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700862 (set_cc ? 1 : 0) << 20 |
Dave Allison65fcc2c2014-04-28 13:45:27 -0700863 rn << 16 |
864 rd << 8 |
865 imm;
866 }
867 } else if (so.IsRegister()) {
868 // Register (possibly shifted)
869 encoding = B31 | B30 | B29 | B27 | B25 |
870 thumb_opcode << 21 |
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700871 (set_cc ? 1 : 0) << 20 |
Dave Allison65fcc2c2014-04-28 13:45:27 -0700872 rn << 16 |
873 rd << 8 |
Dave Allison45fdb932014-06-25 12:37:10 -0700874 so.encodingThumb();
Dave Allison65fcc2c2014-04-28 13:45:27 -0700875 }
876 Emit32(encoding);
877}
878
879
880void Thumb2Assembler::Emit16BitDataProcessing(Condition cond,
881 Opcode opcode,
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700882 bool set_cc,
Dave Allison65fcc2c2014-04-28 13:45:27 -0700883 Register rn,
884 Register rd,
885 const ShifterOperand& so) {
886 if (opcode == ADD || opcode == SUB) {
887 Emit16BitAddSub(cond, opcode, set_cc, rn, rd, so);
888 return;
889 }
Andreas Gampec8ccf682014-09-29 20:07:43 -0700890 uint8_t thumb_opcode = 255U /* 0b11111111 */;
Dave Allison65fcc2c2014-04-28 13:45:27 -0700891 // Thumb1.
Andreas Gampec8ccf682014-09-29 20:07:43 -0700892 uint8_t dp_opcode = 1U /* 0b01 */;
Dave Allison65fcc2c2014-04-28 13:45:27 -0700893 uint8_t opcode_shift = 6;
894 uint8_t rd_shift = 0;
895 uint8_t rn_shift = 3;
896 uint8_t immediate_shift = 0;
897 bool use_immediate = false;
898 uint8_t immediate = 0;
899
900 if (opcode == MOV && so.IsRegister() && so.IsShift()) {
901 // Convert shifted mov operand2 into 16 bit opcodes.
902 dp_opcode = 0;
903 opcode_shift = 11;
904
905 use_immediate = true;
906 immediate = so.GetImmediate();
907 immediate_shift = 6;
908
909 rn = so.GetRegister();
910
911 switch (so.GetShift()) {
Andreas Gampec8ccf682014-09-29 20:07:43 -0700912 case LSL: thumb_opcode = 0U /* 0b00 */; break;
913 case LSR: thumb_opcode = 1U /* 0b01 */; break;
914 case ASR: thumb_opcode = 2U /* 0b10 */; break;
Dave Allison65fcc2c2014-04-28 13:45:27 -0700915 case ROR:
916 // ROR doesn't allow immediates.
Andreas Gampec8ccf682014-09-29 20:07:43 -0700917 thumb_opcode = 7U /* 0b111 */;
918 dp_opcode = 1U /* 0b01 */;
Dave Allison65fcc2c2014-04-28 13:45:27 -0700919 opcode_shift = 6;
920 use_immediate = false;
921 break;
922 case RRX: break;
923 default:
924 break;
925 }
926 } else {
927 if (so.IsImmediate()) {
928 use_immediate = true;
929 immediate = so.GetImmediate();
930 }
931
932 switch (opcode) {
Andreas Gampec8ccf682014-09-29 20:07:43 -0700933 case AND: thumb_opcode = 0U /* 0b0000 */; break;
934 case EOR: thumb_opcode = 1U /* 0b0001 */; break;
Dave Allison65fcc2c2014-04-28 13:45:27 -0700935 case SUB: break;
Andreas Gampec8ccf682014-09-29 20:07:43 -0700936 case RSB: thumb_opcode = 9U /* 0b1001 */; break;
Dave Allison65fcc2c2014-04-28 13:45:27 -0700937 case ADD: break;
Andreas Gampec8ccf682014-09-29 20:07:43 -0700938 case ADC: thumb_opcode = 5U /* 0b0101 */; break;
939 case SBC: thumb_opcode = 6U /* 0b0110 */; break;
Dave Allison65fcc2c2014-04-28 13:45:27 -0700940 case RSC: break;
Andreas Gampec8ccf682014-09-29 20:07:43 -0700941 case TST: thumb_opcode = 8U /* 0b1000 */; rn = so.GetRegister(); break;
Dave Allison65fcc2c2014-04-28 13:45:27 -0700942 case TEQ: break;
943 case CMP:
944 if (use_immediate) {
945 // T2 encoding.
946 dp_opcode = 0;
947 opcode_shift = 11;
Andreas Gampec8ccf682014-09-29 20:07:43 -0700948 thumb_opcode = 5U /* 0b101 */;
Dave Allison65fcc2c2014-04-28 13:45:27 -0700949 rd_shift = 8;
950 rn_shift = 8;
951 } else {
Andreas Gampec8ccf682014-09-29 20:07:43 -0700952 thumb_opcode = 10U /* 0b1010 */;
Nicolas Geoffray8d486732014-07-16 16:23:40 +0100953 rd = rn;
Dave Allison65fcc2c2014-04-28 13:45:27 -0700954 rn = so.GetRegister();
955 }
956
957 break;
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100958 case CMN: {
Andreas Gampec8ccf682014-09-29 20:07:43 -0700959 thumb_opcode = 11U /* 0b1011 */;
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100960 rd = rn;
961 rn = so.GetRegister();
962 break;
963 }
Andreas Gampec8ccf682014-09-29 20:07:43 -0700964 case ORR: thumb_opcode = 12U /* 0b1100 */; break;
Dave Allison65fcc2c2014-04-28 13:45:27 -0700965 case MOV:
966 dp_opcode = 0;
967 if (use_immediate) {
968 // T2 encoding.
969 opcode_shift = 11;
Andreas Gampec8ccf682014-09-29 20:07:43 -0700970 thumb_opcode = 4U /* 0b100 */;
Dave Allison65fcc2c2014-04-28 13:45:27 -0700971 rd_shift = 8;
972 rn_shift = 8;
973 } else {
974 rn = so.GetRegister();
975 if (IsHighRegister(rn) || IsHighRegister(rd)) {
976 // Special mov for high registers.
Andreas Gampec8ccf682014-09-29 20:07:43 -0700977 dp_opcode = 1U /* 0b01 */;
Dave Allison65fcc2c2014-04-28 13:45:27 -0700978 opcode_shift = 7;
979 // Put the top bit of rd into the bottom bit of the opcode.
Andreas Gampec8ccf682014-09-29 20:07:43 -0700980 thumb_opcode = 12U /* 0b0001100 */ | static_cast<uint32_t>(rd) >> 3;
981 rd = static_cast<Register>(static_cast<uint32_t>(rd) & 7U /* 0b111 */);
Dave Allison65fcc2c2014-04-28 13:45:27 -0700982 } else {
983 thumb_opcode = 0;
984 }
985 }
986 break;
Andreas Gampec8ccf682014-09-29 20:07:43 -0700987 case BIC: thumb_opcode = 14U /* 0b1110 */; break;
988 case MVN: thumb_opcode = 15U /* 0b1111 */; rn = so.GetRegister(); break;
Dave Allison65fcc2c2014-04-28 13:45:27 -0700989 default:
990 break;
991 }
992 }
993
Andreas Gampec8ccf682014-09-29 20:07:43 -0700994 if (thumb_opcode == 255U /* 0b11111111 */) {
Dave Allison65fcc2c2014-04-28 13:45:27 -0700995 LOG(FATAL) << "Invalid thumb1 opcode " << opcode;
Vladimir Markoe8469c12014-11-26 18:09:30 +0000996 UNREACHABLE();
Dave Allison65fcc2c2014-04-28 13:45:27 -0700997 }
998
999 int16_t encoding = dp_opcode << 14 |
1000 (thumb_opcode << opcode_shift) |
1001 rd << rd_shift |
1002 rn << rn_shift |
1003 (use_immediate ? (immediate << immediate_shift) : 0);
1004
1005 Emit16(encoding);
1006}
1007
1008
1009// ADD and SUB are complex enough to warrant their own emitter.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001010void Thumb2Assembler::Emit16BitAddSub(Condition cond ATTRIBUTE_UNUSED,
Dave Allison65fcc2c2014-04-28 13:45:27 -07001011 Opcode opcode,
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001012 bool set_cc ATTRIBUTE_UNUSED,
Dave Allison65fcc2c2014-04-28 13:45:27 -07001013 Register rn,
1014 Register rd,
1015 const ShifterOperand& so) {
1016 uint8_t dp_opcode = 0;
1017 uint8_t opcode_shift = 6;
1018 uint8_t rd_shift = 0;
1019 uint8_t rn_shift = 3;
1020 uint8_t immediate_shift = 0;
1021 bool use_immediate = false;
1022 uint8_t immediate = 0;
1023 uint8_t thumb_opcode;;
1024
1025 if (so.IsImmediate()) {
1026 use_immediate = true;
1027 immediate = so.GetImmediate();
1028 }
1029
1030 switch (opcode) {
1031 case ADD:
1032 if (so.IsRegister()) {
1033 Register rm = so.GetRegister();
1034 if (rn == rd) {
1035 // Can use T2 encoding (allows 4 bit registers)
Andreas Gampec8ccf682014-09-29 20:07:43 -07001036 dp_opcode = 1U /* 0b01 */;
Dave Allison65fcc2c2014-04-28 13:45:27 -07001037 opcode_shift = 10;
Andreas Gampec8ccf682014-09-29 20:07:43 -07001038 thumb_opcode = 1U /* 0b0001 */;
Dave Allison65fcc2c2014-04-28 13:45:27 -07001039 // Make Rn also contain the top bit of rd.
1040 rn = static_cast<Register>(static_cast<uint32_t>(rm) |
Andreas Gampec8ccf682014-09-29 20:07:43 -07001041 (static_cast<uint32_t>(rd) & 8U /* 0b1000 */) << 1);
1042 rd = static_cast<Register>(static_cast<uint32_t>(rd) & 7U /* 0b111 */);
Dave Allison65fcc2c2014-04-28 13:45:27 -07001043 } else {
1044 // T1.
1045 opcode_shift = 9;
Andreas Gampec8ccf682014-09-29 20:07:43 -07001046 thumb_opcode = 12U /* 0b01100 */;
Dave Allison65fcc2c2014-04-28 13:45:27 -07001047 immediate = static_cast<uint32_t>(so.GetRegister());
1048 use_immediate = true;
1049 immediate_shift = 6;
1050 }
1051 } else {
1052 // Immediate.
1053 if (rd == SP && rn == SP) {
1054 // ADD sp, sp, #imm
Andreas Gampec8ccf682014-09-29 20:07:43 -07001055 dp_opcode = 2U /* 0b10 */;
1056 thumb_opcode = 3U /* 0b11 */;
Dave Allison65fcc2c2014-04-28 13:45:27 -07001057 opcode_shift = 12;
1058 CHECK_LT(immediate, (1 << 9));
Andreas Gampec8ccf682014-09-29 20:07:43 -07001059 CHECK_EQ((immediate & 3 /* 0b11 */), 0);
Dave Allison65fcc2c2014-04-28 13:45:27 -07001060
1061 // Remove rd and rn from instruction by orring it with immed and clearing bits.
1062 rn = R0;
1063 rd = R0;
1064 rd_shift = 0;
1065 rn_shift = 0;
1066 immediate >>= 2;
1067 } else if (rd != SP && rn == SP) {
1068 // ADD rd, SP, #imm
Andreas Gampec8ccf682014-09-29 20:07:43 -07001069 dp_opcode = 2U /* 0b10 */;
1070 thumb_opcode = 5U /* 0b101 */;
Dave Allison65fcc2c2014-04-28 13:45:27 -07001071 opcode_shift = 11;
1072 CHECK_LT(immediate, (1 << 10));
Andreas Gampec8ccf682014-09-29 20:07:43 -07001073 CHECK_EQ((immediate & 3 /* 0b11 */), 0);
Dave Allison65fcc2c2014-04-28 13:45:27 -07001074
1075 // Remove rn from instruction.
1076 rn = R0;
1077 rn_shift = 0;
1078 rd_shift = 8;
1079 immediate >>= 2;
1080 } else if (rn != rd) {
1081 // Must use T1.
1082 opcode_shift = 9;
Andreas Gampec8ccf682014-09-29 20:07:43 -07001083 thumb_opcode = 14U /* 0b01110 */;
Dave Allison65fcc2c2014-04-28 13:45:27 -07001084 immediate_shift = 6;
1085 } else {
1086 // T2 encoding.
1087 opcode_shift = 11;
Andreas Gampec8ccf682014-09-29 20:07:43 -07001088 thumb_opcode = 6U /* 0b110 */;
Dave Allison65fcc2c2014-04-28 13:45:27 -07001089 rd_shift = 8;
1090 rn_shift = 8;
1091 }
1092 }
1093 break;
1094
1095 case SUB:
1096 if (so.IsRegister()) {
1097 // T1.
1098 opcode_shift = 9;
Andreas Gampec8ccf682014-09-29 20:07:43 -07001099 thumb_opcode = 13U /* 0b01101 */;
Dave Allison65fcc2c2014-04-28 13:45:27 -07001100 immediate = static_cast<uint32_t>(so.GetRegister());
1101 use_immediate = true;
1102 immediate_shift = 6;
1103 } else {
1104 if (rd == SP && rn == SP) {
1105 // SUB sp, sp, #imm
Andreas Gampec8ccf682014-09-29 20:07:43 -07001106 dp_opcode = 2U /* 0b10 */;
1107 thumb_opcode = 0x61 /* 0b1100001 */;
Dave Allison65fcc2c2014-04-28 13:45:27 -07001108 opcode_shift = 7;
1109 CHECK_LT(immediate, (1 << 9));
Andreas Gampec8ccf682014-09-29 20:07:43 -07001110 CHECK_EQ((immediate & 3 /* 0b11 */), 0);
Dave Allison65fcc2c2014-04-28 13:45:27 -07001111
1112 // Remove rd and rn from instruction by orring it with immed and clearing bits.
1113 rn = R0;
1114 rd = R0;
1115 rd_shift = 0;
1116 rn_shift = 0;
1117 immediate >>= 2;
1118 } else if (rn != rd) {
1119 // Must use T1.
1120 opcode_shift = 9;
Andreas Gampec8ccf682014-09-29 20:07:43 -07001121 thumb_opcode = 15U /* 0b01111 */;
Dave Allison65fcc2c2014-04-28 13:45:27 -07001122 immediate_shift = 6;
1123 } else {
1124 // T2 encoding.
1125 opcode_shift = 11;
Andreas Gampec8ccf682014-09-29 20:07:43 -07001126 thumb_opcode = 7U /* 0b111 */;
Dave Allison65fcc2c2014-04-28 13:45:27 -07001127 rd_shift = 8;
1128 rn_shift = 8;
1129 }
1130 }
1131 break;
1132 default:
1133 LOG(FATAL) << "This opcode is not an ADD or SUB: " << opcode;
Vladimir Markoe8469c12014-11-26 18:09:30 +00001134 UNREACHABLE();
Dave Allison65fcc2c2014-04-28 13:45:27 -07001135 }
1136
1137 int16_t encoding = dp_opcode << 14 |
1138 (thumb_opcode << opcode_shift) |
1139 rd << rd_shift |
1140 rn << rn_shift |
1141 (use_immediate ? (immediate << immediate_shift) : 0);
1142
1143 Emit16(encoding);
1144}
1145
1146
1147void Thumb2Assembler::EmitDataProcessing(Condition cond,
1148 Opcode opcode,
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001149 bool set_cc,
Dave Allison65fcc2c2014-04-28 13:45:27 -07001150 Register rn,
1151 Register rd,
1152 const ShifterOperand& so) {
1153 CHECK_NE(rd, kNoRegister);
1154 CheckCondition(cond);
1155
1156 if (Is32BitDataProcessing(cond, opcode, set_cc, rn, rd, so)) {
1157 Emit32BitDataProcessing(cond, opcode, set_cc, rn, rd, so);
1158 } else {
1159 Emit16BitDataProcessing(cond, opcode, set_cc, rn, rd, so);
1160 }
1161}
1162
Dave Allison45fdb932014-06-25 12:37:10 -07001163void Thumb2Assembler::EmitShift(Register rd, Register rm, Shift shift, uint8_t amount, bool setcc) {
1164 CHECK_LT(amount, (1 << 5));
1165 if (IsHighRegister(rd) || IsHighRegister(rm) || shift == ROR || shift == RRX) {
1166 uint16_t opcode = 0;
1167 switch (shift) {
Andreas Gampec8ccf682014-09-29 20:07:43 -07001168 case LSL: opcode = 0U /* 0b00 */; break;
1169 case LSR: opcode = 1U /* 0b01 */; break;
1170 case ASR: opcode = 2U /* 0b10 */; break;
1171 case ROR: opcode = 3U /* 0b11 */; break;
1172 case RRX: opcode = 3U /* 0b11 */; amount = 0; break;
Dave Allison45fdb932014-06-25 12:37:10 -07001173 default:
1174 LOG(FATAL) << "Unsupported thumb2 shift opcode";
Vladimir Markoe8469c12014-11-26 18:09:30 +00001175 UNREACHABLE();
Dave Allison45fdb932014-06-25 12:37:10 -07001176 }
1177 // 32 bit.
1178 int32_t encoding = B31 | B30 | B29 | B27 | B25 | B22 |
1179 0xf << 16 | (setcc ? B20 : 0);
1180 uint32_t imm3 = amount >> 2;
Andreas Gampec8ccf682014-09-29 20:07:43 -07001181 uint32_t imm2 = amount & 3U /* 0b11 */;
Dave Allison45fdb932014-06-25 12:37:10 -07001182 encoding |= imm3 << 12 | imm2 << 6 | static_cast<int16_t>(rm) |
1183 static_cast<int16_t>(rd) << 8 | opcode << 4;
1184 Emit32(encoding);
1185 } else {
1186 // 16 bit shift
1187 uint16_t opcode = 0;
1188 switch (shift) {
Andreas Gampec8ccf682014-09-29 20:07:43 -07001189 case LSL: opcode = 0U /* 0b00 */; break;
1190 case LSR: opcode = 1U /* 0b01 */; break;
1191 case ASR: opcode = 2U /* 0b10 */; break;
Dave Allison45fdb932014-06-25 12:37:10 -07001192 default:
Vladimir Markoe8469c12014-11-26 18:09:30 +00001193 LOG(FATAL) << "Unsupported thumb2 shift opcode";
1194 UNREACHABLE();
Dave Allison45fdb932014-06-25 12:37:10 -07001195 }
1196 int16_t encoding = opcode << 11 | amount << 6 | static_cast<int16_t>(rm) << 3 |
1197 static_cast<int16_t>(rd);
1198 Emit16(encoding);
1199 }
1200}
1201
1202void Thumb2Assembler::EmitShift(Register rd, Register rn, Shift shift, Register rm, bool setcc) {
1203 CHECK_NE(shift, RRX);
1204 bool must_be_32bit = false;
1205 if (IsHighRegister(rd) || IsHighRegister(rm) || IsHighRegister(rn) || rd != rn) {
1206 must_be_32bit = true;
1207 }
1208
1209 if (must_be_32bit) {
1210 uint16_t opcode = 0;
1211 switch (shift) {
Andreas Gampec8ccf682014-09-29 20:07:43 -07001212 case LSL: opcode = 0U /* 0b00 */; break;
1213 case LSR: opcode = 1U /* 0b01 */; break;
1214 case ASR: opcode = 2U /* 0b10 */; break;
1215 case ROR: opcode = 3U /* 0b11 */; break;
Dave Allison45fdb932014-06-25 12:37:10 -07001216 default:
1217 LOG(FATAL) << "Unsupported thumb2 shift opcode";
Vladimir Markoe8469c12014-11-26 18:09:30 +00001218 UNREACHABLE();
Dave Allison45fdb932014-06-25 12:37:10 -07001219 }
1220 // 32 bit.
1221 int32_t encoding = B31 | B30 | B29 | B28 | B27 | B25 |
1222 0xf << 12 | (setcc ? B20 : 0);
1223 encoding |= static_cast<int16_t>(rn) << 16 | static_cast<int16_t>(rm) |
1224 static_cast<int16_t>(rd) << 8 | opcode << 21;
1225 Emit32(encoding);
1226 } else {
1227 uint16_t opcode = 0;
1228 switch (shift) {
Andreas Gampec8ccf682014-09-29 20:07:43 -07001229 case LSL: opcode = 2U /* 0b0010 */; break;
1230 case LSR: opcode = 3U /* 0b0011 */; break;
1231 case ASR: opcode = 4U /* 0b0100 */; break;
Dave Allison45fdb932014-06-25 12:37:10 -07001232 default:
Vladimir Markoe8469c12014-11-26 18:09:30 +00001233 LOG(FATAL) << "Unsupported thumb2 shift opcode";
1234 UNREACHABLE();
Dave Allison45fdb932014-06-25 12:37:10 -07001235 }
1236 int16_t encoding = B14 | opcode << 6 | static_cast<int16_t>(rm) << 3 |
1237 static_cast<int16_t>(rd);
1238 Emit16(encoding);
1239 }
1240}
1241
1242
Dave Allison65fcc2c2014-04-28 13:45:27 -07001243
1244void Thumb2Assembler::Branch::Emit(AssemblerBuffer* buffer) const {
1245 bool link = type_ == kUnconditionalLinkX || type_ == kUnconditionalLink;
1246 bool x = type_ == kUnconditionalX || type_ == kUnconditionalLinkX;
1247 int32_t offset = target_ - location_;
1248
1249 if (size_ == k32Bit) {
1250 int32_t encoding = B31 | B30 | B29 | B28 | B15;
1251 if (link) {
1252 // BL or BLX immediate.
1253 encoding |= B14;
1254 if (!x) {
1255 encoding |= B12;
1256 } else {
1257 // Bottom bit of offset must be 0.
1258 CHECK_EQ((offset & 1), 0);
1259 }
1260 } else {
1261 if (x) {
1262 LOG(FATAL) << "Invalid use of BX";
Vladimir Markoe8469c12014-11-26 18:09:30 +00001263 UNREACHABLE();
Dave Allison65fcc2c2014-04-28 13:45:27 -07001264 } else {
1265 if (cond_ == AL) {
1266 // Can use the T4 encoding allowing a 24 bit offset.
1267 if (!x) {
1268 encoding |= B12;
1269 }
1270 } else {
1271 // Must be T3 encoding with a 20 bit offset.
1272 encoding |= cond_ << 22;
1273 }
1274 }
1275 }
1276 encoding = Thumb2Assembler::EncodeBranchOffset(offset, encoding);
1277 buffer->Store<int16_t>(location_, static_cast<int16_t>(encoding >> 16));
1278 buffer->Store<int16_t>(location_+2, static_cast<int16_t>(encoding & 0xffff));
1279 } else {
1280 if (IsCompareAndBranch()) {
1281 offset -= 4;
1282 uint16_t i = (offset >> 6) & 1;
Andreas Gampec8ccf682014-09-29 20:07:43 -07001283 uint16_t imm5 = (offset >> 1) & 31U /* 0b11111 */;
Dave Allison65fcc2c2014-04-28 13:45:27 -07001284 int16_t encoding = B15 | B13 | B12 |
1285 (type_ == kCompareAndBranchNonZero ? B11 : 0) |
1286 static_cast<uint32_t>(rn_) |
1287 B8 |
1288 i << 9 |
1289 imm5 << 3;
1290 buffer->Store<int16_t>(location_, encoding);
1291 } else {
1292 offset -= 4; // Account for PC offset.
1293 int16_t encoding;
1294 // 16 bit.
1295 if (cond_ == AL) {
1296 encoding = B15 | B14 | B13 |
1297 ((offset >> 1) & 0x7ff);
1298 } else {
1299 encoding = B15 | B14 | B12 |
1300 cond_ << 8 | ((offset >> 1) & 0xff);
1301 }
1302 buffer->Store<int16_t>(location_, encoding);
1303 }
1304 }
1305}
1306
1307
1308uint16_t Thumb2Assembler::EmitCompareAndBranch(Register rn, uint16_t prev, bool n) {
1309 uint32_t location = buffer_.Size();
1310
1311 // This is always unresolved as it must be a forward branch.
1312 Emit16(prev); // Previous link.
1313 return AddBranch(n ? Branch::kCompareAndBranchNonZero : Branch::kCompareAndBranchZero,
1314 location, rn);
1315}
1316
1317
1318// NOTE: this only support immediate offsets, not [rx,ry].
1319// TODO: support [rx,ry] instructions.
1320void Thumb2Assembler::EmitLoadStore(Condition cond,
1321 bool load,
1322 bool byte,
1323 bool half,
1324 bool is_signed,
1325 Register rd,
1326 const Address& ad) {
1327 CHECK_NE(rd, kNoRegister);
1328 CheckCondition(cond);
1329 bool must_be_32bit = force_32bit_;
1330 if (IsHighRegister(rd)) {
1331 must_be_32bit = true;
1332 }
1333
1334 Register rn = ad.GetRegister();
Dave Allison45fdb932014-06-25 12:37:10 -07001335 if (IsHighRegister(rn) && rn != SP && rn != PC) {
Dave Allison65fcc2c2014-04-28 13:45:27 -07001336 must_be_32bit = true;
1337 }
1338
1339 if (is_signed || ad.GetOffset() < 0 || ad.GetMode() != Address::Offset) {
1340 must_be_32bit = true;
1341 }
1342
Dave Allison45fdb932014-06-25 12:37:10 -07001343 if (ad.IsImmediate()) {
1344 // Immediate offset
1345 int32_t offset = ad.GetOffset();
Dave Allison65fcc2c2014-04-28 13:45:27 -07001346
Dave Allison45fdb932014-06-25 12:37:10 -07001347 // The 16 bit SP relative instruction can only have a 10 bit offset.
Dave Allison0bb9ade2014-06-26 17:57:36 -07001348 if (rn == SP && offset >= (1 << 10)) {
Dave Allison65fcc2c2014-04-28 13:45:27 -07001349 must_be_32bit = true;
1350 }
Dave Allison65fcc2c2014-04-28 13:45:27 -07001351
1352 if (byte) {
Dave Allison45fdb932014-06-25 12:37:10 -07001353 // 5 bit offset, no shift.
Dave Allison0bb9ade2014-06-26 17:57:36 -07001354 if (offset >= (1 << 5)) {
Dave Allison45fdb932014-06-25 12:37:10 -07001355 must_be_32bit = true;
1356 }
Dave Allison65fcc2c2014-04-28 13:45:27 -07001357 } else if (half) {
Dave Allison45fdb932014-06-25 12:37:10 -07001358 // 6 bit offset, shifted by 1.
Dave Allison0bb9ade2014-06-26 17:57:36 -07001359 if (offset >= (1 << 6)) {
Dave Allison45fdb932014-06-25 12:37:10 -07001360 must_be_32bit = true;
1361 }
Dave Allison65fcc2c2014-04-28 13:45:27 -07001362 } else {
Dave Allison45fdb932014-06-25 12:37:10 -07001363 // 7 bit offset, shifted by 2.
Dave Allison0bb9ade2014-06-26 17:57:36 -07001364 if (offset >= (1 << 7)) {
Dave Allison45fdb932014-06-25 12:37:10 -07001365 must_be_32bit = true;
Dave Allison65fcc2c2014-04-28 13:45:27 -07001366 }
1367 }
Dave Allison65fcc2c2014-04-28 13:45:27 -07001368
Dave Allison45fdb932014-06-25 12:37:10 -07001369 if (must_be_32bit) {
1370 int32_t encoding = B31 | B30 | B29 | B28 | B27 |
1371 (load ? B20 : 0) |
1372 (is_signed ? B24 : 0) |
1373 static_cast<uint32_t>(rd) << 12 |
1374 ad.encodingThumb(true) |
1375 (byte ? 0 : half ? B21 : B22);
1376 Emit32(encoding);
Dave Allison65fcc2c2014-04-28 13:45:27 -07001377 } else {
Dave Allison45fdb932014-06-25 12:37:10 -07001378 // 16 bit thumb1.
1379 uint8_t opA = 0;
1380 bool sp_relative = false;
Dave Allison65fcc2c2014-04-28 13:45:27 -07001381
1382 if (byte) {
Andreas Gampec8ccf682014-09-29 20:07:43 -07001383 opA = 7U /* 0b0111 */;
Dave Allison65fcc2c2014-04-28 13:45:27 -07001384 } else if (half) {
Andreas Gampec8ccf682014-09-29 20:07:43 -07001385 opA = 8U /* 0b1000 */;
Dave Allison65fcc2c2014-04-28 13:45:27 -07001386 } else {
Dave Allison45fdb932014-06-25 12:37:10 -07001387 if (rn == SP) {
Andreas Gampec8ccf682014-09-29 20:07:43 -07001388 opA = 9U /* 0b1001 */;
Dave Allison45fdb932014-06-25 12:37:10 -07001389 sp_relative = true;
1390 } else {
Andreas Gampec8ccf682014-09-29 20:07:43 -07001391 opA = 6U /* 0b0110 */;
Dave Allison45fdb932014-06-25 12:37:10 -07001392 }
Dave Allison65fcc2c2014-04-28 13:45:27 -07001393 }
Dave Allison45fdb932014-06-25 12:37:10 -07001394 int16_t encoding = opA << 12 |
1395 (load ? B11 : 0);
Dave Allison65fcc2c2014-04-28 13:45:27 -07001396
Dave Allison45fdb932014-06-25 12:37:10 -07001397 CHECK_GE(offset, 0);
1398 if (sp_relative) {
1399 // SP relative, 10 bit offset.
Dave Allison0bb9ade2014-06-26 17:57:36 -07001400 CHECK_LT(offset, (1 << 10));
Andreas Gampec8ccf682014-09-29 20:07:43 -07001401 CHECK_EQ((offset & 3 /* 0b11 */), 0);
Dave Allison45fdb932014-06-25 12:37:10 -07001402 encoding |= rd << 8 | offset >> 2;
1403 } else {
1404 // No SP relative. The offset is shifted right depending on
1405 // the size of the load/store.
1406 encoding |= static_cast<uint32_t>(rd);
1407
1408 if (byte) {
1409 // 5 bit offset, no shift.
Dave Allison0bb9ade2014-06-26 17:57:36 -07001410 CHECK_LT(offset, (1 << 5));
Dave Allison45fdb932014-06-25 12:37:10 -07001411 } else if (half) {
1412 // 6 bit offset, shifted by 1.
Dave Allison0bb9ade2014-06-26 17:57:36 -07001413 CHECK_LT(offset, (1 << 6));
Andreas Gampec8ccf682014-09-29 20:07:43 -07001414 CHECK_EQ((offset & 1 /* 0b1 */), 0);
Dave Allison45fdb932014-06-25 12:37:10 -07001415 offset >>= 1;
1416 } else {
1417 // 7 bit offset, shifted by 2.
Dave Allison0bb9ade2014-06-26 17:57:36 -07001418 CHECK_LT(offset, (1 << 7));
Andreas Gampec8ccf682014-09-29 20:07:43 -07001419 CHECK_EQ((offset & 3 /* 0b11 */), 0);
Dave Allison45fdb932014-06-25 12:37:10 -07001420 offset >>= 2;
1421 }
1422 encoding |= rn << 3 | offset << 6;
1423 }
1424
1425 Emit16(encoding);
1426 }
1427 } else {
1428 // Register shift.
1429 if (ad.GetRegister() == PC) {
1430 // PC relative literal encoding.
1431 int32_t offset = ad.GetOffset();
Dave Allison0bb9ade2014-06-26 17:57:36 -07001432 if (must_be_32bit || offset < 0 || offset >= (1 << 10) || !load) {
Dave Allison45fdb932014-06-25 12:37:10 -07001433 int32_t up = B23;
1434 if (offset < 0) {
1435 offset = -offset;
1436 up = 0;
1437 }
1438 CHECK_LT(offset, (1 << 12));
1439 int32_t encoding = 0x1f << 27 | 0xf << 16 | B22 | (load ? B20 : 0) |
1440 offset | up |
1441 static_cast<uint32_t>(rd) << 12;
1442 Emit32(encoding);
1443 } else {
1444 // 16 bit literal load.
1445 CHECK_GE(offset, 0);
1446 CHECK_LT(offset, (1 << 10));
1447 int32_t encoding = B14 | (load ? B11 : 0) | static_cast<uint32_t>(rd) << 8 | offset >> 2;
1448 Emit16(encoding);
1449 }
1450 } else {
1451 if (ad.GetShiftCount() != 0) {
1452 // If there is a shift count this must be 32 bit.
1453 must_be_32bit = true;
1454 } else if (IsHighRegister(ad.GetRegisterOffset())) {
1455 must_be_32bit = true;
1456 }
1457
1458 if (must_be_32bit) {
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01001459 int32_t encoding = 0x1f << 27 | (load ? B20 : 0) | static_cast<uint32_t>(rd) << 12 |
Dave Allison45fdb932014-06-25 12:37:10 -07001460 ad.encodingThumb(true);
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01001461 if (half) {
1462 encoding |= B21;
1463 } else if (!byte) {
1464 encoding |= B22;
1465 }
Dave Allison45fdb932014-06-25 12:37:10 -07001466 Emit32(encoding);
1467 } else {
1468 // 16 bit register offset.
1469 int32_t encoding = B14 | B12 | (load ? B11 : 0) | static_cast<uint32_t>(rd) |
1470 ad.encodingThumb(false);
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01001471 if (byte) {
1472 encoding |= B10;
1473 } else if (half) {
1474 encoding |= B9;
1475 }
Dave Allison45fdb932014-06-25 12:37:10 -07001476 Emit16(encoding);
1477 }
1478 }
Dave Allison65fcc2c2014-04-28 13:45:27 -07001479 }
1480}
1481
1482
1483void Thumb2Assembler::EmitMultiMemOp(Condition cond,
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001484 BlockAddressMode bam,
Dave Allison65fcc2c2014-04-28 13:45:27 -07001485 bool load,
1486 Register base,
1487 RegList regs) {
1488 CHECK_NE(base, kNoRegister);
1489 CheckCondition(cond);
1490 bool must_be_32bit = force_32bit_;
1491
Vladimir Markoe8469c12014-11-26 18:09:30 +00001492 if (!must_be_32bit && base == SP && bam == (load ? IA_W : DB_W) &&
1493 (regs & 0xff00 & ~(1 << (load ? PC : LR))) == 0) {
1494 // Use 16-bit PUSH/POP.
1495 int16_t encoding = B15 | B13 | B12 | (load ? B11 : 0) | B10 |
1496 ((regs & (1 << (load ? PC : LR))) != 0 ? B8 : 0) | (regs & 0x00ff);
1497 Emit16(encoding);
1498 return;
1499 }
1500
Dave Allison65fcc2c2014-04-28 13:45:27 -07001501 if ((regs & 0xff00) != 0) {
1502 must_be_32bit = true;
1503 }
1504
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001505 bool w_bit = bam == IA_W || bam == DB_W || bam == DA_W || bam == IB_W;
Dave Allison65fcc2c2014-04-28 13:45:27 -07001506 // 16 bit always uses writeback.
1507 if (!w_bit) {
1508 must_be_32bit = true;
1509 }
1510
1511 if (must_be_32bit) {
1512 uint32_t op = 0;
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001513 switch (bam) {
Dave Allison65fcc2c2014-04-28 13:45:27 -07001514 case IA:
1515 case IA_W:
Andreas Gampec8ccf682014-09-29 20:07:43 -07001516 op = 1U /* 0b01 */;
Dave Allison65fcc2c2014-04-28 13:45:27 -07001517 break;
1518 case DB:
1519 case DB_W:
Andreas Gampec8ccf682014-09-29 20:07:43 -07001520 op = 2U /* 0b10 */;
Dave Allison65fcc2c2014-04-28 13:45:27 -07001521 break;
1522 case DA:
1523 case IB:
1524 case DA_W:
1525 case IB_W:
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001526 LOG(FATAL) << "LDM/STM mode not supported on thumb: " << bam;
Vladimir Markoe8469c12014-11-26 18:09:30 +00001527 UNREACHABLE();
Dave Allison65fcc2c2014-04-28 13:45:27 -07001528 }
1529 if (load) {
1530 // Cannot have SP in the list.
1531 CHECK_EQ((regs & (1 << SP)), 0);
1532 } else {
1533 // Cannot have PC or SP in the list.
1534 CHECK_EQ((regs & (1 << PC | 1 << SP)), 0);
1535 }
1536 int32_t encoding = B31 | B30 | B29 | B27 |
1537 (op << 23) |
1538 (load ? B20 : 0) |
1539 base << 16 |
1540 regs |
1541 (w_bit << 21);
1542 Emit32(encoding);
1543 } else {
1544 int16_t encoding = B15 | B14 |
1545 (load ? B11 : 0) |
1546 base << 8 |
1547 regs;
1548 Emit16(encoding);
1549 }
1550}
1551
1552
1553void Thumb2Assembler::EmitBranch(Condition cond, Label* label, bool link, bool x) {
1554 uint32_t pc = buffer_.Size();
1555 Branch::Type branch_type;
1556 if (cond == AL) {
1557 if (link) {
1558 if (x) {
1559 branch_type = Branch::kUnconditionalLinkX; // BLX.
1560 } else {
1561 branch_type = Branch::kUnconditionalLink; // BX.
1562 }
1563 } else {
1564 branch_type = Branch::kUnconditional; // B.
1565 }
1566 } else {
1567 branch_type = Branch::kConditional; // B<cond>.
1568 }
1569
1570 if (label->IsBound()) {
1571 Branch::Size size = AddBranch(branch_type, pc, label->Position(), cond); // Resolved branch.
1572
1573 // The branch is to a bound label which means that it's a backwards branch. We know the
1574 // current size of it so we can emit the appropriate space. Note that if it's a 16 bit
1575 // branch the size may change if it so happens that other branches change size that change
1576 // the distance to the target and that distance puts this branch over the limit for 16 bits.
1577 if (size == Branch::k16Bit) {
Nicolas Geoffray8d486732014-07-16 16:23:40 +01001578 DCHECK(!force_32bit_branches_);
Dave Allison65fcc2c2014-04-28 13:45:27 -07001579 Emit16(0); // Space for a 16 bit branch.
1580 } else {
1581 Emit32(0); // Space for a 32 bit branch.
1582 }
1583 } else {
1584 // Branch is to an unbound label. Emit space for it.
1585 uint16_t branch_id = AddBranch(branch_type, pc, cond); // Unresolved branch.
Nicolas Geoffray8d486732014-07-16 16:23:40 +01001586 if (force_32bit_branches_ || force_32bit_) {
Dave Allison65fcc2c2014-04-28 13:45:27 -07001587 Emit16(static_cast<uint16_t>(label->position_)); // Emit current label link.
1588 Emit16(0); // another 16 bits.
1589 } else {
1590 Emit16(static_cast<uint16_t>(label->position_)); // Emit current label link.
1591 }
1592 label->LinkTo(branch_id); // Link to the branch ID.
1593 }
1594}
1595
1596
1597void Thumb2Assembler::clz(Register rd, Register rm, Condition cond) {
1598 CHECK_NE(rd, kNoRegister);
1599 CHECK_NE(rm, kNoRegister);
1600 CheckCondition(cond);
1601 CHECK_NE(rd, PC);
1602 CHECK_NE(rm, PC);
1603 int32_t encoding = B31 | B30 | B29 | B28 | B27 |
1604 B25 | B23 | B21 | B20 |
1605 static_cast<uint32_t>(rm) << 16 |
1606 0xf << 12 |
1607 static_cast<uint32_t>(rd) << 8 |
1608 B7 |
1609 static_cast<uint32_t>(rm);
1610 Emit32(encoding);
1611}
1612
1613
1614void Thumb2Assembler::movw(Register rd, uint16_t imm16, Condition cond) {
1615 CheckCondition(cond);
1616 bool must_be_32bit = force_32bit_;
1617 if (IsHighRegister(rd)|| imm16 >= 256u) {
1618 must_be_32bit = true;
1619 }
1620
1621 if (must_be_32bit) {
1622 // Use encoding T3.
Andreas Gampec8ccf682014-09-29 20:07:43 -07001623 uint32_t imm4 = (imm16 >> 12) & 15U /* 0b1111 */;
1624 uint32_t i = (imm16 >> 11) & 1U /* 0b1 */;
1625 uint32_t imm3 = (imm16 >> 8) & 7U /* 0b111 */;
Dave Allison65fcc2c2014-04-28 13:45:27 -07001626 uint32_t imm8 = imm16 & 0xff;
1627 int32_t encoding = B31 | B30 | B29 | B28 |
1628 B25 | B22 |
1629 static_cast<uint32_t>(rd) << 8 |
1630 i << 26 |
1631 imm4 << 16 |
1632 imm3 << 12 |
1633 imm8;
1634 Emit32(encoding);
1635 } else {
1636 int16_t encoding = B13 | static_cast<uint16_t>(rd) << 8 |
1637 imm16;
1638 Emit16(encoding);
1639 }
1640}
1641
1642
1643void Thumb2Assembler::movt(Register rd, uint16_t imm16, Condition cond) {
1644 CheckCondition(cond);
1645 // Always 32 bits.
Andreas Gampec8ccf682014-09-29 20:07:43 -07001646 uint32_t imm4 = (imm16 >> 12) & 15U /* 0b1111 */;
1647 uint32_t i = (imm16 >> 11) & 1U /* 0b1 */;
1648 uint32_t imm3 = (imm16 >> 8) & 7U /* 0b111 */;
Dave Allison65fcc2c2014-04-28 13:45:27 -07001649 uint32_t imm8 = imm16 & 0xff;
1650 int32_t encoding = B31 | B30 | B29 | B28 |
1651 B25 | B23 | B22 |
1652 static_cast<uint32_t>(rd) << 8 |
1653 i << 26 |
1654 imm4 << 16 |
1655 imm3 << 12 |
1656 imm8;
1657 Emit32(encoding);
1658}
1659
1660
1661void Thumb2Assembler::ldrex(Register rt, Register rn, uint16_t imm, Condition cond) {
1662 CHECK_NE(rn, kNoRegister);
1663 CHECK_NE(rt, kNoRegister);
1664 CheckCondition(cond);
1665 CHECK_NE(rn, kNoRegister);
1666 CHECK_NE(rt, kNoRegister);
1667 CheckCondition(cond);
1668 CHECK_LT(imm, (1u << 10));
1669
1670 int32_t encoding = B31 | B30 | B29 | B27 | B22 | B20 |
1671 static_cast<uint32_t>(rn) << 16 |
1672 static_cast<uint32_t>(rt) << 12 |
1673 0xf << 8 |
1674 imm >> 2;
1675 Emit32(encoding);
1676}
1677
1678
1679void Thumb2Assembler::ldrex(Register rt, Register rn, Condition cond) {
1680 ldrex(rt, rn, 0, cond);
1681}
1682
1683
1684void Thumb2Assembler::strex(Register rd,
1685 Register rt,
1686 Register rn,
1687 uint16_t imm,
1688 Condition cond) {
1689 CHECK_NE(rn, kNoRegister);
1690 CHECK_NE(rd, kNoRegister);
1691 CHECK_NE(rt, kNoRegister);
1692 CheckCondition(cond);
1693 CHECK_LT(imm, (1u << 10));
1694
1695 int32_t encoding = B31 | B30 | B29 | B27 | B22 |
1696 static_cast<uint32_t>(rn) << 16 |
1697 static_cast<uint32_t>(rt) << 12 |
1698 static_cast<uint32_t>(rd) << 8 |
1699 imm >> 2;
1700 Emit32(encoding);
1701}
1702
1703
1704void Thumb2Assembler::strex(Register rd,
1705 Register rt,
1706 Register rn,
1707 Condition cond) {
1708 strex(rd, rt, rn, 0, cond);
1709}
1710
1711
1712void Thumb2Assembler::clrex(Condition cond) {
1713 CheckCondition(cond);
1714 int32_t encoding = B31 | B30 | B29 | B27 | B28 | B25 | B24 | B23 |
1715 B21 | B20 |
1716 0xf << 16 |
1717 B15 |
1718 0xf << 8 |
1719 B5 |
1720 0xf;
1721 Emit32(encoding);
1722}
1723
1724
1725void Thumb2Assembler::nop(Condition cond) {
1726 CheckCondition(cond);
Andreas Gampec8ccf682014-09-29 20:07:43 -07001727 uint16_t encoding = B15 | B13 | B12 |
Dave Allison65fcc2c2014-04-28 13:45:27 -07001728 B11 | B10 | B9 | B8;
Andreas Gampec8ccf682014-09-29 20:07:43 -07001729 Emit16(static_cast<int16_t>(encoding));
Dave Allison65fcc2c2014-04-28 13:45:27 -07001730}
1731
1732
1733void Thumb2Assembler::vmovsr(SRegister sn, Register rt, Condition cond) {
1734 CHECK_NE(sn, kNoSRegister);
1735 CHECK_NE(rt, kNoRegister);
1736 CHECK_NE(rt, SP);
1737 CHECK_NE(rt, PC);
1738 CheckCondition(cond);
1739 int32_t encoding = (static_cast<int32_t>(cond) << kConditionShift) |
1740 B27 | B26 | B25 |
1741 ((static_cast<int32_t>(sn) >> 1)*B16) |
1742 (static_cast<int32_t>(rt)*B12) | B11 | B9 |
1743 ((static_cast<int32_t>(sn) & 1)*B7) | B4;
1744 Emit32(encoding);
1745}
1746
1747
1748void Thumb2Assembler::vmovrs(Register rt, SRegister sn, Condition cond) {
1749 CHECK_NE(sn, kNoSRegister);
1750 CHECK_NE(rt, kNoRegister);
1751 CHECK_NE(rt, SP);
1752 CHECK_NE(rt, PC);
1753 CheckCondition(cond);
1754 int32_t encoding = (static_cast<int32_t>(cond) << kConditionShift) |
1755 B27 | B26 | B25 | B20 |
1756 ((static_cast<int32_t>(sn) >> 1)*B16) |
1757 (static_cast<int32_t>(rt)*B12) | B11 | B9 |
1758 ((static_cast<int32_t>(sn) & 1)*B7) | B4;
1759 Emit32(encoding);
1760}
1761
1762
1763void Thumb2Assembler::vmovsrr(SRegister sm, Register rt, Register rt2,
1764 Condition cond) {
1765 CHECK_NE(sm, kNoSRegister);
1766 CHECK_NE(sm, S31);
1767 CHECK_NE(rt, kNoRegister);
1768 CHECK_NE(rt, SP);
1769 CHECK_NE(rt, PC);
1770 CHECK_NE(rt2, kNoRegister);
1771 CHECK_NE(rt2, SP);
1772 CHECK_NE(rt2, PC);
1773 CheckCondition(cond);
1774 int32_t encoding = (static_cast<int32_t>(cond) << kConditionShift) |
1775 B27 | B26 | B22 |
1776 (static_cast<int32_t>(rt2)*B16) |
1777 (static_cast<int32_t>(rt)*B12) | B11 | B9 |
1778 ((static_cast<int32_t>(sm) & 1)*B5) | B4 |
1779 (static_cast<int32_t>(sm) >> 1);
1780 Emit32(encoding);
1781}
1782
1783
1784void Thumb2Assembler::vmovrrs(Register rt, Register rt2, SRegister sm,
1785 Condition cond) {
1786 CHECK_NE(sm, kNoSRegister);
1787 CHECK_NE(sm, S31);
1788 CHECK_NE(rt, kNoRegister);
1789 CHECK_NE(rt, SP);
1790 CHECK_NE(rt, PC);
1791 CHECK_NE(rt2, kNoRegister);
1792 CHECK_NE(rt2, SP);
1793 CHECK_NE(rt2, PC);
1794 CHECK_NE(rt, rt2);
1795 CheckCondition(cond);
1796 int32_t encoding = (static_cast<int32_t>(cond) << kConditionShift) |
1797 B27 | B26 | B22 | B20 |
1798 (static_cast<int32_t>(rt2)*B16) |
1799 (static_cast<int32_t>(rt)*B12) | B11 | B9 |
1800 ((static_cast<int32_t>(sm) & 1)*B5) | B4 |
1801 (static_cast<int32_t>(sm) >> 1);
1802 Emit32(encoding);
1803}
1804
1805
1806void Thumb2Assembler::vmovdrr(DRegister dm, Register rt, Register rt2,
1807 Condition cond) {
1808 CHECK_NE(dm, kNoDRegister);
1809 CHECK_NE(rt, kNoRegister);
1810 CHECK_NE(rt, SP);
1811 CHECK_NE(rt, PC);
1812 CHECK_NE(rt2, kNoRegister);
1813 CHECK_NE(rt2, SP);
1814 CHECK_NE(rt2, PC);
1815 CheckCondition(cond);
1816 int32_t encoding = (static_cast<int32_t>(cond) << kConditionShift) |
1817 B27 | B26 | B22 |
1818 (static_cast<int32_t>(rt2)*B16) |
1819 (static_cast<int32_t>(rt)*B12) | B11 | B9 | B8 |
1820 ((static_cast<int32_t>(dm) >> 4)*B5) | B4 |
1821 (static_cast<int32_t>(dm) & 0xf);
1822 Emit32(encoding);
1823}
1824
1825
1826void Thumb2Assembler::vmovrrd(Register rt, Register rt2, DRegister dm,
1827 Condition cond) {
1828 CHECK_NE(dm, kNoDRegister);
1829 CHECK_NE(rt, kNoRegister);
1830 CHECK_NE(rt, SP);
1831 CHECK_NE(rt, PC);
1832 CHECK_NE(rt2, kNoRegister);
1833 CHECK_NE(rt2, SP);
1834 CHECK_NE(rt2, PC);
1835 CHECK_NE(rt, rt2);
1836 CheckCondition(cond);
1837 int32_t encoding = (static_cast<int32_t>(cond) << kConditionShift) |
1838 B27 | B26 | B22 | B20 |
1839 (static_cast<int32_t>(rt2)*B16) |
1840 (static_cast<int32_t>(rt)*B12) | B11 | B9 | B8 |
1841 ((static_cast<int32_t>(dm) >> 4)*B5) | B4 |
1842 (static_cast<int32_t>(dm) & 0xf);
1843 Emit32(encoding);
1844}
1845
1846
1847void Thumb2Assembler::vldrs(SRegister sd, const Address& ad, Condition cond) {
1848 const Address& addr = static_cast<const Address&>(ad);
1849 CHECK_NE(sd, kNoSRegister);
1850 CheckCondition(cond);
1851 int32_t encoding = (static_cast<int32_t>(cond) << kConditionShift) |
1852 B27 | B26 | B24 | B20 |
1853 ((static_cast<int32_t>(sd) & 1)*B22) |
1854 ((static_cast<int32_t>(sd) >> 1)*B12) |
1855 B11 | B9 | addr.vencoding();
1856 Emit32(encoding);
1857}
1858
1859
1860void Thumb2Assembler::vstrs(SRegister sd, const Address& ad, Condition cond) {
1861 const Address& addr = static_cast<const Address&>(ad);
1862 CHECK_NE(static_cast<Register>(addr.encodingArm() & (0xf << kRnShift)), PC);
1863 CHECK_NE(sd, kNoSRegister);
1864 CheckCondition(cond);
1865 int32_t encoding = (static_cast<int32_t>(cond) << kConditionShift) |
1866 B27 | B26 | B24 |
1867 ((static_cast<int32_t>(sd) & 1)*B22) |
1868 ((static_cast<int32_t>(sd) >> 1)*B12) |
1869 B11 | B9 | addr.vencoding();
1870 Emit32(encoding);
1871}
1872
1873
1874void Thumb2Assembler::vldrd(DRegister dd, const Address& ad, Condition cond) {
1875 const Address& addr = static_cast<const Address&>(ad);
1876 CHECK_NE(dd, kNoDRegister);
1877 CheckCondition(cond);
1878 int32_t encoding = (static_cast<int32_t>(cond) << kConditionShift) |
1879 B27 | B26 | B24 | B20 |
1880 ((static_cast<int32_t>(dd) >> 4)*B22) |
1881 ((static_cast<int32_t>(dd) & 0xf)*B12) |
1882 B11 | B9 | B8 | addr.vencoding();
1883 Emit32(encoding);
1884}
1885
1886
1887void Thumb2Assembler::vstrd(DRegister dd, const Address& ad, Condition cond) {
1888 const Address& addr = static_cast<const Address&>(ad);
1889 CHECK_NE(static_cast<Register>(addr.encodingArm() & (0xf << kRnShift)), PC);
1890 CHECK_NE(dd, kNoDRegister);
1891 CheckCondition(cond);
1892 int32_t encoding = (static_cast<int32_t>(cond) << kConditionShift) |
1893 B27 | B26 | B24 |
1894 ((static_cast<int32_t>(dd) >> 4)*B22) |
1895 ((static_cast<int32_t>(dd) & 0xf)*B12) |
1896 B11 | B9 | B8 | addr.vencoding();
1897 Emit32(encoding);
1898}
1899
1900
1901void Thumb2Assembler::vpushs(SRegister reg, int nregs, Condition cond) {
1902 EmitVPushPop(static_cast<uint32_t>(reg), nregs, true, false, cond);
1903}
1904
1905
1906void Thumb2Assembler::vpushd(DRegister reg, int nregs, Condition cond) {
1907 EmitVPushPop(static_cast<uint32_t>(reg), nregs, true, true, cond);
1908}
1909
1910
1911void Thumb2Assembler::vpops(SRegister reg, int nregs, Condition cond) {
1912 EmitVPushPop(static_cast<uint32_t>(reg), nregs, false, false, cond);
1913}
1914
1915
1916void Thumb2Assembler::vpopd(DRegister reg, int nregs, Condition cond) {
1917 EmitVPushPop(static_cast<uint32_t>(reg), nregs, false, true, cond);
1918}
1919
1920
1921void Thumb2Assembler::EmitVPushPop(uint32_t reg, int nregs, bool push, bool dbl, Condition cond) {
1922 CheckCondition(cond);
1923
1924 uint32_t D;
1925 uint32_t Vd;
1926 if (dbl) {
1927 // Encoded as D:Vd.
1928 D = (reg >> 4) & 1;
Andreas Gampec8ccf682014-09-29 20:07:43 -07001929 Vd = reg & 15U /* 0b1111 */;
Dave Allison65fcc2c2014-04-28 13:45:27 -07001930 } else {
1931 // Encoded as Vd:D.
1932 D = reg & 1;
Andreas Gampec8ccf682014-09-29 20:07:43 -07001933 Vd = (reg >> 1) & 15U /* 0b1111 */;
Dave Allison65fcc2c2014-04-28 13:45:27 -07001934 }
1935 int32_t encoding = B27 | B26 | B21 | B19 | B18 | B16 |
1936 B11 | B9 |
1937 (dbl ? B8 : 0) |
1938 (push ? B24 : (B23 | B20)) |
Andreas Gampec8ccf682014-09-29 20:07:43 -07001939 14U /* 0b1110 */ << 28 |
Dave Allison65fcc2c2014-04-28 13:45:27 -07001940 nregs << (dbl ? 1 : 0) |
1941 D << 22 |
1942 Vd << 12;
1943 Emit32(encoding);
1944}
1945
1946
1947void Thumb2Assembler::EmitVFPsss(Condition cond, int32_t opcode,
1948 SRegister sd, SRegister sn, SRegister sm) {
1949 CHECK_NE(sd, kNoSRegister);
1950 CHECK_NE(sn, kNoSRegister);
1951 CHECK_NE(sm, kNoSRegister);
1952 CheckCondition(cond);
1953 int32_t encoding = (static_cast<int32_t>(cond) << kConditionShift) |
1954 B27 | B26 | B25 | B11 | B9 | opcode |
1955 ((static_cast<int32_t>(sd) & 1)*B22) |
1956 ((static_cast<int32_t>(sn) >> 1)*B16) |
1957 ((static_cast<int32_t>(sd) >> 1)*B12) |
1958 ((static_cast<int32_t>(sn) & 1)*B7) |
1959 ((static_cast<int32_t>(sm) & 1)*B5) |
1960 (static_cast<int32_t>(sm) >> 1);
1961 Emit32(encoding);
1962}
1963
1964
1965void Thumb2Assembler::EmitVFPddd(Condition cond, int32_t opcode,
1966 DRegister dd, DRegister dn, DRegister dm) {
1967 CHECK_NE(dd, kNoDRegister);
1968 CHECK_NE(dn, kNoDRegister);
1969 CHECK_NE(dm, kNoDRegister);
1970 CheckCondition(cond);
1971 int32_t encoding = (static_cast<int32_t>(cond) << kConditionShift) |
1972 B27 | B26 | B25 | B11 | B9 | B8 | opcode |
1973 ((static_cast<int32_t>(dd) >> 4)*B22) |
1974 ((static_cast<int32_t>(dn) & 0xf)*B16) |
1975 ((static_cast<int32_t>(dd) & 0xf)*B12) |
1976 ((static_cast<int32_t>(dn) >> 4)*B7) |
1977 ((static_cast<int32_t>(dm) >> 4)*B5) |
1978 (static_cast<int32_t>(dm) & 0xf);
1979 Emit32(encoding);
1980}
1981
1982
1983void Thumb2Assembler::EmitVFPsd(Condition cond, int32_t opcode,
1984 SRegister sd, DRegister dm) {
1985 CHECK_NE(sd, kNoSRegister);
1986 CHECK_NE(dm, kNoDRegister);
1987 CheckCondition(cond);
1988 int32_t encoding = (static_cast<int32_t>(cond) << kConditionShift) |
1989 B27 | B26 | B25 | B11 | B9 | opcode |
1990 ((static_cast<int32_t>(sd) & 1)*B22) |
1991 ((static_cast<int32_t>(sd) >> 1)*B12) |
1992 ((static_cast<int32_t>(dm) >> 4)*B5) |
1993 (static_cast<int32_t>(dm) & 0xf);
1994 Emit32(encoding);
1995}
1996
1997
1998void Thumb2Assembler::EmitVFPds(Condition cond, int32_t opcode,
1999 DRegister dd, SRegister sm) {
2000 CHECK_NE(dd, kNoDRegister);
2001 CHECK_NE(sm, kNoSRegister);
2002 CheckCondition(cond);
2003 int32_t encoding = (static_cast<int32_t>(cond) << kConditionShift) |
2004 B27 | B26 | B25 | B11 | B9 | opcode |
2005 ((static_cast<int32_t>(dd) >> 4)*B22) |
2006 ((static_cast<int32_t>(dd) & 0xf)*B12) |
2007 ((static_cast<int32_t>(sm) & 1)*B5) |
2008 (static_cast<int32_t>(sm) >> 1);
2009 Emit32(encoding);
2010}
2011
2012
2013void Thumb2Assembler::vmstat(Condition cond) { // VMRS APSR_nzcv, FPSCR.
Calin Juravleddb7df22014-11-25 20:56:51 +00002014 CHECK_NE(cond, kNoCondition);
Dave Allison65fcc2c2014-04-28 13:45:27 -07002015 CheckCondition(cond);
Calin Juravleddb7df22014-11-25 20:56:51 +00002016 int32_t encoding = (static_cast<int32_t>(cond) << kConditionShift) |
2017 B27 | B26 | B25 | B23 | B22 | B21 | B20 | B16 |
2018 (static_cast<int32_t>(PC)*B12) |
2019 B11 | B9 | B4;
2020 Emit32(encoding);
Dave Allison65fcc2c2014-04-28 13:45:27 -07002021}
2022
2023
2024void Thumb2Assembler::svc(uint32_t imm8) {
2025 CHECK(IsUint(8, imm8)) << imm8;
2026 int16_t encoding = B15 | B14 | B12 |
2027 B11 | B10 | B9 | B8 |
2028 imm8;
2029 Emit16(encoding);
2030}
2031
2032
2033void Thumb2Assembler::bkpt(uint16_t imm8) {
2034 CHECK(IsUint(8, imm8)) << imm8;
2035 int16_t encoding = B15 | B13 | B12 |
2036 B11 | B10 | B9 |
2037 imm8;
2038 Emit16(encoding);
2039}
2040
2041// Convert the given IT state to a mask bit given bit 0 of the first
2042// condition and a shift position.
2043static uint8_t ToItMask(ItState s, uint8_t firstcond0, uint8_t shift) {
2044 switch (s) {
2045 case kItOmitted: return 1 << shift;
2046 case kItThen: return firstcond0 << shift;
2047 case kItElse: return !firstcond0 << shift;
2048 }
2049 return 0;
2050}
2051
2052
2053// Set the IT condition in the given position for the given state. This is used
2054// to check that conditional instructions match the preceding IT statement.
2055void Thumb2Assembler::SetItCondition(ItState s, Condition cond, uint8_t index) {
2056 switch (s) {
2057 case kItOmitted: it_conditions_[index] = AL; break;
2058 case kItThen: it_conditions_[index] = cond; break;
2059 case kItElse:
2060 it_conditions_[index] = static_cast<Condition>(static_cast<uint8_t>(cond) ^ 1);
2061 break;
2062 }
2063}
2064
2065
2066void Thumb2Assembler::it(Condition firstcond, ItState i1, ItState i2, ItState i3) {
2067 CheckCondition(AL); // Not allowed in IT block.
2068 uint8_t firstcond0 = static_cast<uint8_t>(firstcond) & 1;
2069
2070 // All conditions to AL.
2071 for (uint8_t i = 0; i < 4; ++i) {
2072 it_conditions_[i] = AL;
2073 }
2074
2075 SetItCondition(kItThen, firstcond, 0);
2076 uint8_t mask = ToItMask(i1, firstcond0, 3);
2077 SetItCondition(i1, firstcond, 1);
2078
2079 if (i1 != kItOmitted) {
2080 mask |= ToItMask(i2, firstcond0, 2);
2081 SetItCondition(i2, firstcond, 2);
2082 if (i2 != kItOmitted) {
2083 mask |= ToItMask(i3, firstcond0, 1);
2084 SetItCondition(i3, firstcond, 3);
2085 if (i3 != kItOmitted) {
Andreas Gampec8ccf682014-09-29 20:07:43 -07002086 mask |= 1U /* 0b0001 */;
Dave Allison65fcc2c2014-04-28 13:45:27 -07002087 }
2088 }
2089 }
2090
2091 // Start at first condition.
2092 it_cond_index_ = 0;
2093 next_condition_ = it_conditions_[0];
2094 uint16_t encoding = B15 | B13 | B12 |
2095 B11 | B10 | B9 | B8 |
2096 firstcond << 4 |
2097 mask;
2098 Emit16(encoding);
2099}
2100
2101
2102void Thumb2Assembler::cbz(Register rn, Label* label) {
2103 CheckCondition(AL);
2104 if (label->IsBound()) {
2105 LOG(FATAL) << "cbz can only be used to branch forwards";
Vladimir Markoe8469c12014-11-26 18:09:30 +00002106 UNREACHABLE();
Dave Allison65fcc2c2014-04-28 13:45:27 -07002107 } else {
2108 uint16_t branchid = EmitCompareAndBranch(rn, static_cast<uint16_t>(label->position_), false);
2109 label->LinkTo(branchid);
2110 }
2111}
2112
2113
2114void Thumb2Assembler::cbnz(Register rn, Label* label) {
2115 CheckCondition(AL);
2116 if (label->IsBound()) {
2117 LOG(FATAL) << "cbnz can only be used to branch forwards";
Vladimir Markoe8469c12014-11-26 18:09:30 +00002118 UNREACHABLE();
Dave Allison65fcc2c2014-04-28 13:45:27 -07002119 } else {
2120 uint16_t branchid = EmitCompareAndBranch(rn, static_cast<uint16_t>(label->position_), true);
2121 label->LinkTo(branchid);
2122 }
2123}
2124
2125
2126void Thumb2Assembler::blx(Register rm, Condition cond) {
2127 CHECK_NE(rm, kNoRegister);
2128 CheckCondition(cond);
2129 int16_t encoding = B14 | B10 | B9 | B8 | B7 | static_cast<int16_t>(rm) << 3;
2130 Emit16(encoding);
2131}
2132
2133
2134void Thumb2Assembler::bx(Register rm, Condition cond) {
2135 CHECK_NE(rm, kNoRegister);
2136 CheckCondition(cond);
2137 int16_t encoding = B14 | B10 | B9 | B8 | static_cast<int16_t>(rm) << 3;
2138 Emit16(encoding);
2139}
2140
2141
2142void Thumb2Assembler::Push(Register rd, Condition cond) {
2143 str(rd, Address(SP, -kRegisterSize, Address::PreIndex), cond);
2144}
2145
2146
2147void Thumb2Assembler::Pop(Register rd, Condition cond) {
2148 ldr(rd, Address(SP, kRegisterSize, Address::PostIndex), cond);
2149}
2150
2151
2152void Thumb2Assembler::PushList(RegList regs, Condition cond) {
2153 stm(DB_W, SP, regs, cond);
2154}
2155
2156
2157void Thumb2Assembler::PopList(RegList regs, Condition cond) {
2158 ldm(IA_W, SP, regs, cond);
2159}
2160
2161
2162void Thumb2Assembler::Mov(Register rd, Register rm, Condition cond) {
2163 if (cond != AL || rd != rm) {
2164 mov(rd, ShifterOperand(rm), cond);
2165 }
2166}
2167
2168
2169// A branch has changed size. Make a hole for it.
2170void Thumb2Assembler::MakeHoleForBranch(uint32_t location, uint32_t delta) {
2171 // Move the contents of the buffer using: Move(newposition, oldposition)
2172 AssemblerBuffer::EnsureCapacity ensured(&buffer_);
2173 buffer_.Move(location + delta, location);
2174}
2175
2176
2177void Thumb2Assembler::Bind(Label* label) {
2178 CHECK(!label->IsBound());
2179 uint32_t bound_pc = buffer_.Size();
2180 std::vector<Branch*> changed_branches;
2181
2182 while (label->IsLinked()) {
2183 uint16_t position = label->Position(); // Branch id for linked branch.
2184 Branch* branch = GetBranch(position); // Get the branch at this id.
2185 bool changed = branch->Resolve(bound_pc); // Branch can be resolved now.
2186 uint32_t branch_location = branch->GetLocation();
2187 uint16_t next = buffer_.Load<uint16_t>(branch_location); // Get next in chain.
2188 if (changed) {
Nicolas Geoffray8d486732014-07-16 16:23:40 +01002189 DCHECK(!force_32bit_branches_);
Dave Allison65fcc2c2014-04-28 13:45:27 -07002190 MakeHoleForBranch(branch->GetLocation(), 2);
2191 if (branch->IsCompareAndBranch()) {
2192 // A cbz/cbnz instruction has changed size. There is no valid encoding for
2193 // a 32 bit cbz/cbnz so we need to change this to an instruction pair:
2194 // cmp rn, #0
2195 // b<eq|ne> target
2196 bool n = branch->GetType() == Branch::kCompareAndBranchNonZero;
2197 Condition cond = n ? NE : EQ;
2198 branch->Move(2); // Move the branch forward by 2 bytes.
2199 branch->ResetTypeAndCondition(Branch::kConditional, cond);
2200 branch->ResetSize(Branch::k16Bit);
2201
2202 // Now add a compare instruction in the place the branch was.
Andreas Gampe277ccbd2014-11-03 21:36:10 -08002203 buffer_.Store<int16_t>(branch_location,
2204 B13 | B11 | static_cast<int16_t>(branch->GetRegister()) << 8);
Dave Allison65fcc2c2014-04-28 13:45:27 -07002205
2206 // Since have moved made a hole in the code we need to reload the
2207 // current pc.
2208 bound_pc = buffer_.Size();
2209
2210 // Now resolve the newly added branch.
2211 changed = branch->Resolve(bound_pc);
2212 if (changed) {
2213 MakeHoleForBranch(branch->GetLocation(), 2);
2214 changed_branches.push_back(branch);
2215 }
2216 } else {
2217 changed_branches.push_back(branch);
2218 }
2219 }
2220 label->position_ = next; // Move to next.
2221 }
2222 label->BindTo(bound_pc);
2223
2224 // Now relocate any changed branches. Do this until there are no more changes.
2225 std::vector<Branch*> branches_to_process = changed_branches;
2226 while (branches_to_process.size() != 0) {
2227 changed_branches.clear();
2228 for (auto& changed_branch : branches_to_process) {
2229 for (auto& branch : branches_) {
2230 bool changed = branch->Relocate(changed_branch->GetLocation(), 2);
2231 if (changed) {
2232 changed_branches.push_back(branch);
2233 }
2234 }
2235 branches_to_process = changed_branches;
2236 }
2237 }
2238}
2239
2240
2241void Thumb2Assembler::EmitBranches() {
2242 for (auto& branch : branches_) {
2243 branch->Emit(&buffer_);
2244 }
2245}
2246
2247
2248void Thumb2Assembler::Lsl(Register rd, Register rm, uint32_t shift_imm,
Dave Allison45fdb932014-06-25 12:37:10 -07002249 bool setcc, Condition cond) {
Calin Juravle9aec02f2014-11-18 23:06:35 +00002250 CHECK_LE(shift_imm, 31u);
Dave Allison45fdb932014-06-25 12:37:10 -07002251 CheckCondition(cond);
2252 EmitShift(rd, rm, LSL, shift_imm, setcc);
Dave Allison65fcc2c2014-04-28 13:45:27 -07002253}
2254
2255
2256void Thumb2Assembler::Lsr(Register rd, Register rm, uint32_t shift_imm,
Dave Allison45fdb932014-06-25 12:37:10 -07002257 bool setcc, Condition cond) {
Calin Juravle9aec02f2014-11-18 23:06:35 +00002258 CHECK(1u <= shift_imm && shift_imm <= 32u);
Dave Allison65fcc2c2014-04-28 13:45:27 -07002259 if (shift_imm == 32) shift_imm = 0; // Comply to UAL syntax.
Dave Allison45fdb932014-06-25 12:37:10 -07002260 CheckCondition(cond);
2261 EmitShift(rd, rm, LSR, shift_imm, setcc);
Dave Allison65fcc2c2014-04-28 13:45:27 -07002262}
2263
2264
2265void Thumb2Assembler::Asr(Register rd, Register rm, uint32_t shift_imm,
Dave Allison45fdb932014-06-25 12:37:10 -07002266 bool setcc, Condition cond) {
Calin Juravle9aec02f2014-11-18 23:06:35 +00002267 CHECK(1u <= shift_imm && shift_imm <= 32u);
Dave Allison65fcc2c2014-04-28 13:45:27 -07002268 if (shift_imm == 32) shift_imm = 0; // Comply to UAL syntax.
Dave Allison45fdb932014-06-25 12:37:10 -07002269 CheckCondition(cond);
2270 EmitShift(rd, rm, ASR, shift_imm, setcc);
Dave Allison65fcc2c2014-04-28 13:45:27 -07002271}
2272
2273
2274void Thumb2Assembler::Ror(Register rd, Register rm, uint32_t shift_imm,
Dave Allison45fdb932014-06-25 12:37:10 -07002275 bool setcc, Condition cond) {
Calin Juravle9aec02f2014-11-18 23:06:35 +00002276 CHECK(1u <= shift_imm && shift_imm <= 31u);
Dave Allison45fdb932014-06-25 12:37:10 -07002277 CheckCondition(cond);
2278 EmitShift(rd, rm, ROR, shift_imm, setcc);
Dave Allison65fcc2c2014-04-28 13:45:27 -07002279}
2280
2281
Dave Allison45fdb932014-06-25 12:37:10 -07002282void Thumb2Assembler::Rrx(Register rd, Register rm, bool setcc, Condition cond) {
2283 CheckCondition(cond);
2284 EmitShift(rd, rm, RRX, rm, setcc);
2285}
2286
2287
2288void Thumb2Assembler::Lsl(Register rd, Register rm, Register rn,
2289 bool setcc, Condition cond) {
2290 CheckCondition(cond);
2291 EmitShift(rd, rm, LSL, rn, setcc);
2292}
2293
2294
2295void Thumb2Assembler::Lsr(Register rd, Register rm, Register rn,
2296 bool setcc, Condition cond) {
2297 CheckCondition(cond);
2298 EmitShift(rd, rm, LSR, rn, setcc);
2299}
2300
2301
2302void Thumb2Assembler::Asr(Register rd, Register rm, Register rn,
2303 bool setcc, Condition cond) {
2304 CheckCondition(cond);
2305 EmitShift(rd, rm, ASR, rn, setcc);
2306}
2307
2308
2309void Thumb2Assembler::Ror(Register rd, Register rm, Register rn,
2310 bool setcc, Condition cond) {
2311 CheckCondition(cond);
2312 EmitShift(rd, rm, ROR, rn, setcc);
Dave Allison65fcc2c2014-04-28 13:45:27 -07002313}
2314
2315
2316int32_t Thumb2Assembler::EncodeBranchOffset(int32_t offset, int32_t inst) {
2317 // The offset is off by 4 due to the way the ARM CPUs read PC.
2318 offset -= 4;
2319 offset >>= 1;
2320
2321 uint32_t value = 0;
2322 // There are two different encodings depending on the value of bit 12. In one case
2323 // intermediate values are calculated using the sign bit.
2324 if ((inst & B12) == B12) {
2325 // 25 bits of offset.
2326 uint32_t signbit = (offset >> 31) & 0x1;
2327 uint32_t i1 = (offset >> 22) & 0x1;
2328 uint32_t i2 = (offset >> 21) & 0x1;
2329 uint32_t imm10 = (offset >> 11) & 0x03ff;
2330 uint32_t imm11 = offset & 0x07ff;
2331 uint32_t j1 = (i1 ^ signbit) ? 0 : 1;
2332 uint32_t j2 = (i2 ^ signbit) ? 0 : 1;
2333 value = (signbit << 26) | (j1 << 13) | (j2 << 11) | (imm10 << 16) |
2334 imm11;
2335 // Remove the offset from the current encoding.
2336 inst &= ~(0x3ff << 16 | 0x7ff);
2337 } else {
2338 uint32_t signbit = (offset >> 31) & 0x1;
2339 uint32_t imm6 = (offset >> 11) & 0x03f;
2340 uint32_t imm11 = offset & 0x07ff;
2341 uint32_t j1 = (offset >> 19) & 1;
2342 uint32_t j2 = (offset >> 17) & 1;
2343 value = (signbit << 26) | (j1 << 13) | (j2 << 11) | (imm6 << 16) |
2344 imm11;
2345 // Remove the offset from the current encoding.
2346 inst &= ~(0x3f << 16 | 0x7ff);
2347 }
2348 // Mask out offset bits in current instruction.
2349 inst &= ~(B26 | B13 | B11);
2350 inst |= value;
2351 return inst;
2352}
2353
2354
2355int Thumb2Assembler::DecodeBranchOffset(int32_t instr) {
2356 int32_t imm32;
2357 if ((instr & B12) == B12) {
2358 uint32_t S = (instr >> 26) & 1;
2359 uint32_t J2 = (instr >> 11) & 1;
2360 uint32_t J1 = (instr >> 13) & 1;
2361 uint32_t imm10 = (instr >> 16) & 0x3FF;
2362 uint32_t imm11 = instr & 0x7FF;
2363
2364 uint32_t I1 = ~(J1 ^ S) & 1;
2365 uint32_t I2 = ~(J2 ^ S) & 1;
2366 imm32 = (S << 24) | (I1 << 23) | (I2 << 22) | (imm10 << 12) | (imm11 << 1);
2367 imm32 = (imm32 << 8) >> 8; // sign extend 24 bit immediate.
2368 } else {
2369 uint32_t S = (instr >> 26) & 1;
2370 uint32_t J2 = (instr >> 11) & 1;
2371 uint32_t J1 = (instr >> 13) & 1;
2372 uint32_t imm6 = (instr >> 16) & 0x3F;
2373 uint32_t imm11 = instr & 0x7FF;
2374
2375 imm32 = (S << 20) | (J2 << 19) | (J1 << 18) | (imm6 << 12) | (imm11 << 1);
2376 imm32 = (imm32 << 11) >> 11; // sign extend 21 bit immediate.
2377 }
2378 imm32 += 4;
2379 return imm32;
2380}
2381
2382
2383void Thumb2Assembler::AddConstant(Register rd, int32_t value, Condition cond) {
2384 AddConstant(rd, rd, value, cond);
2385}
2386
2387
2388void Thumb2Assembler::AddConstant(Register rd, Register rn, int32_t value,
2389 Condition cond) {
2390 if (value == 0) {
2391 if (rd != rn) {
2392 mov(rd, ShifterOperand(rn), cond);
2393 }
2394 return;
2395 }
2396 // We prefer to select the shorter code sequence rather than selecting add for
2397 // positive values and sub for negatives ones, which would slightly improve
2398 // the readability of generated code for some constants.
2399 ShifterOperand shifter_op;
Nicolas Geoffray3bcc8ea2014-11-28 15:00:02 +00002400 if (ShifterOperandCanHold(rd, rn, ADD, value, &shifter_op)) {
Dave Allison65fcc2c2014-04-28 13:45:27 -07002401 add(rd, rn, shifter_op, cond);
Nicolas Geoffray3bcc8ea2014-11-28 15:00:02 +00002402 } else if (ShifterOperandCanHold(rd, rn, SUB, -value, &shifter_op)) {
Dave Allison65fcc2c2014-04-28 13:45:27 -07002403 sub(rd, rn, shifter_op, cond);
2404 } else {
2405 CHECK(rn != IP);
Nicolas Geoffray3bcc8ea2014-11-28 15:00:02 +00002406 if (ShifterOperandCanHold(rd, rn, MVN, ~value, &shifter_op)) {
Dave Allison65fcc2c2014-04-28 13:45:27 -07002407 mvn(IP, shifter_op, cond);
2408 add(rd, rn, ShifterOperand(IP), cond);
Nicolas Geoffray3bcc8ea2014-11-28 15:00:02 +00002409 } else if (ShifterOperandCanHold(rd, rn, MVN, ~(-value), &shifter_op)) {
Dave Allison65fcc2c2014-04-28 13:45:27 -07002410 mvn(IP, shifter_op, cond);
2411 sub(rd, rn, ShifterOperand(IP), cond);
2412 } else {
2413 movw(IP, Low16Bits(value), cond);
2414 uint16_t value_high = High16Bits(value);
2415 if (value_high != 0) {
2416 movt(IP, value_high, cond);
2417 }
2418 add(rd, rn, ShifterOperand(IP), cond);
2419 }
2420 }
2421}
2422
2423
2424void Thumb2Assembler::AddConstantSetFlags(Register rd, Register rn, int32_t value,
2425 Condition cond) {
2426 ShifterOperand shifter_op;
Nicolas Geoffray3bcc8ea2014-11-28 15:00:02 +00002427 if (ShifterOperandCanHold(rd, rn, ADD, value, &shifter_op)) {
Dave Allison65fcc2c2014-04-28 13:45:27 -07002428 adds(rd, rn, shifter_op, cond);
Nicolas Geoffray3bcc8ea2014-11-28 15:00:02 +00002429 } else if (ShifterOperandCanHold(rd, rn, ADD, -value, &shifter_op)) {
Dave Allison65fcc2c2014-04-28 13:45:27 -07002430 subs(rd, rn, shifter_op, cond);
2431 } else {
2432 CHECK(rn != IP);
Nicolas Geoffray3bcc8ea2014-11-28 15:00:02 +00002433 if (ShifterOperandCanHold(rd, rn, MVN, ~value, &shifter_op)) {
Dave Allison65fcc2c2014-04-28 13:45:27 -07002434 mvn(IP, shifter_op, cond);
2435 adds(rd, rn, ShifterOperand(IP), cond);
Nicolas Geoffray3bcc8ea2014-11-28 15:00:02 +00002436 } else if (ShifterOperandCanHold(rd, rn, MVN, ~(-value), &shifter_op)) {
Dave Allison65fcc2c2014-04-28 13:45:27 -07002437 mvn(IP, shifter_op, cond);
2438 subs(rd, rn, ShifterOperand(IP), cond);
2439 } else {
2440 movw(IP, Low16Bits(value), cond);
2441 uint16_t value_high = High16Bits(value);
2442 if (value_high != 0) {
2443 movt(IP, value_high, cond);
2444 }
2445 adds(rd, rn, ShifterOperand(IP), cond);
2446 }
2447 }
2448}
2449
Nicolas Geoffray3bcc8ea2014-11-28 15:00:02 +00002450
Dave Allison65fcc2c2014-04-28 13:45:27 -07002451void Thumb2Assembler::LoadImmediate(Register rd, int32_t value, Condition cond) {
2452 ShifterOperand shifter_op;
Nicolas Geoffray3bcc8ea2014-11-28 15:00:02 +00002453 if (ShifterOperandCanHold(rd, R0, MOV, value, &shifter_op)) {
Dave Allison65fcc2c2014-04-28 13:45:27 -07002454 mov(rd, shifter_op, cond);
Nicolas Geoffray3bcc8ea2014-11-28 15:00:02 +00002455 } else if (ShifterOperandCanHold(rd, R0, MVN, ~value, &shifter_op)) {
Dave Allison65fcc2c2014-04-28 13:45:27 -07002456 mvn(rd, shifter_op, cond);
2457 } else {
2458 movw(rd, Low16Bits(value), cond);
2459 uint16_t value_high = High16Bits(value);
2460 if (value_high != 0) {
2461 movt(rd, value_high, cond);
2462 }
2463 }
2464}
2465
Nicolas Geoffray3bcc8ea2014-11-28 15:00:02 +00002466
Dave Allison65fcc2c2014-04-28 13:45:27 -07002467// Implementation note: this method must emit at most one instruction when
2468// Address::CanHoldLoadOffsetThumb.
2469void Thumb2Assembler::LoadFromOffset(LoadOperandType type,
2470 Register reg,
2471 Register base,
2472 int32_t offset,
2473 Condition cond) {
2474 if (!Address::CanHoldLoadOffsetThumb(type, offset)) {
Roland Levillain775ef492014-11-04 17:43:11 +00002475 CHECK_NE(base, IP);
Dave Allison65fcc2c2014-04-28 13:45:27 -07002476 LoadImmediate(IP, offset, cond);
2477 add(IP, IP, ShifterOperand(base), cond);
2478 base = IP;
2479 offset = 0;
2480 }
2481 CHECK(Address::CanHoldLoadOffsetThumb(type, offset));
2482 switch (type) {
2483 case kLoadSignedByte:
2484 ldrsb(reg, Address(base, offset), cond);
2485 break;
2486 case kLoadUnsignedByte:
2487 ldrb(reg, Address(base, offset), cond);
2488 break;
2489 case kLoadSignedHalfword:
2490 ldrsh(reg, Address(base, offset), cond);
2491 break;
2492 case kLoadUnsignedHalfword:
2493 ldrh(reg, Address(base, offset), cond);
2494 break;
2495 case kLoadWord:
2496 ldr(reg, Address(base, offset), cond);
2497 break;
2498 case kLoadWordPair:
2499 ldrd(reg, Address(base, offset), cond);
2500 break;
2501 default:
2502 LOG(FATAL) << "UNREACHABLE";
Ian Rogers2c4257b2014-10-24 14:20:06 -07002503 UNREACHABLE();
Dave Allison65fcc2c2014-04-28 13:45:27 -07002504 }
2505}
2506
2507
2508// Implementation note: this method must emit at most one instruction when
2509// Address::CanHoldLoadOffsetThumb, as expected by JIT::GuardedLoadFromOffset.
2510void Thumb2Assembler::LoadSFromOffset(SRegister reg,
2511 Register base,
2512 int32_t offset,
2513 Condition cond) {
2514 if (!Address::CanHoldLoadOffsetThumb(kLoadSWord, offset)) {
2515 CHECK_NE(base, IP);
2516 LoadImmediate(IP, offset, cond);
2517 add(IP, IP, ShifterOperand(base), cond);
2518 base = IP;
2519 offset = 0;
2520 }
2521 CHECK(Address::CanHoldLoadOffsetThumb(kLoadSWord, offset));
2522 vldrs(reg, Address(base, offset), cond);
2523}
2524
2525
2526// Implementation note: this method must emit at most one instruction when
2527// Address::CanHoldLoadOffsetThumb, as expected by JIT::GuardedLoadFromOffset.
2528void Thumb2Assembler::LoadDFromOffset(DRegister reg,
2529 Register base,
2530 int32_t offset,
2531 Condition cond) {
2532 if (!Address::CanHoldLoadOffsetThumb(kLoadDWord, offset)) {
2533 CHECK_NE(base, IP);
2534 LoadImmediate(IP, offset, cond);
2535 add(IP, IP, ShifterOperand(base), cond);
2536 base = IP;
2537 offset = 0;
2538 }
2539 CHECK(Address::CanHoldLoadOffsetThumb(kLoadDWord, offset));
2540 vldrd(reg, Address(base, offset), cond);
2541}
2542
2543
2544// Implementation note: this method must emit at most one instruction when
2545// Address::CanHoldStoreOffsetThumb.
2546void Thumb2Assembler::StoreToOffset(StoreOperandType type,
2547 Register reg,
2548 Register base,
2549 int32_t offset,
2550 Condition cond) {
Roland Levillain775ef492014-11-04 17:43:11 +00002551 Register tmp_reg = kNoRegister;
Dave Allison65fcc2c2014-04-28 13:45:27 -07002552 if (!Address::CanHoldStoreOffsetThumb(type, offset)) {
Roland Levillain775ef492014-11-04 17:43:11 +00002553 CHECK_NE(base, IP);
2554 if (reg != IP) {
2555 tmp_reg = IP;
2556 } else {
2557 // Be careful not to use IP twice (for `reg` and to build the
2558 // Address object used by the store instruction(s) below).
2559 // Instead, save R5 on the stack (or R6 if R5 is not available),
2560 // use it as secondary temporary register, and restore it after
2561 // the store instruction has been emitted.
2562 tmp_reg = base != R5 ? R5 : R6;
2563 Push(tmp_reg);
2564 if (base == SP) {
2565 offset += kRegisterSize;
2566 }
2567 }
2568 LoadImmediate(tmp_reg, offset, cond);
2569 add(tmp_reg, tmp_reg, ShifterOperand(base), cond);
2570 base = tmp_reg;
Dave Allison65fcc2c2014-04-28 13:45:27 -07002571 offset = 0;
2572 }
2573 CHECK(Address::CanHoldStoreOffsetThumb(type, offset));
2574 switch (type) {
2575 case kStoreByte:
2576 strb(reg, Address(base, offset), cond);
2577 break;
2578 case kStoreHalfword:
2579 strh(reg, Address(base, offset), cond);
2580 break;
2581 case kStoreWord:
2582 str(reg, Address(base, offset), cond);
2583 break;
2584 case kStoreWordPair:
2585 strd(reg, Address(base, offset), cond);
2586 break;
2587 default:
2588 LOG(FATAL) << "UNREACHABLE";
Ian Rogers2c4257b2014-10-24 14:20:06 -07002589 UNREACHABLE();
Dave Allison65fcc2c2014-04-28 13:45:27 -07002590 }
Roland Levillain775ef492014-11-04 17:43:11 +00002591 if (tmp_reg != kNoRegister && tmp_reg != IP) {
2592 DCHECK(tmp_reg == R5 || tmp_reg == R6);
2593 Pop(tmp_reg);
2594 }
Dave Allison65fcc2c2014-04-28 13:45:27 -07002595}
2596
2597
2598// Implementation note: this method must emit at most one instruction when
2599// Address::CanHoldStoreOffsetThumb, as expected by JIT::GuardedStoreToOffset.
2600void Thumb2Assembler::StoreSToOffset(SRegister reg,
2601 Register base,
2602 int32_t offset,
2603 Condition cond) {
2604 if (!Address::CanHoldStoreOffsetThumb(kStoreSWord, offset)) {
2605 CHECK_NE(base, IP);
2606 LoadImmediate(IP, offset, cond);
2607 add(IP, IP, ShifterOperand(base), cond);
2608 base = IP;
2609 offset = 0;
2610 }
2611 CHECK(Address::CanHoldStoreOffsetThumb(kStoreSWord, offset));
2612 vstrs(reg, Address(base, offset), cond);
2613}
2614
2615
2616// Implementation note: this method must emit at most one instruction when
2617// Address::CanHoldStoreOffsetThumb, as expected by JIT::GuardedStoreSToOffset.
2618void Thumb2Assembler::StoreDToOffset(DRegister reg,
2619 Register base,
2620 int32_t offset,
2621 Condition cond) {
2622 if (!Address::CanHoldStoreOffsetThumb(kStoreDWord, offset)) {
2623 CHECK_NE(base, IP);
2624 LoadImmediate(IP, offset, cond);
2625 add(IP, IP, ShifterOperand(base), cond);
2626 base = IP;
2627 offset = 0;
2628 }
2629 CHECK(Address::CanHoldStoreOffsetThumb(kStoreDWord, offset));
2630 vstrd(reg, Address(base, offset), cond);
2631}
2632
2633
2634void Thumb2Assembler::MemoryBarrier(ManagedRegister mscratch) {
2635 CHECK_EQ(mscratch.AsArm().AsCoreRegister(), R12);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002636 dmb(SY);
2637}
2638
2639
2640void Thumb2Assembler::dmb(DmbOptions flavor) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002641 int32_t encoding = 0xf3bf8f50; // dmb in T1 encoding.
2642 Emit32(encoding | flavor);
Dave Allison65fcc2c2014-04-28 13:45:27 -07002643}
2644
2645
2646void Thumb2Assembler::CompareAndBranchIfZero(Register r, Label* label) {
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01002647 if (force_32bit_branches_) {
2648 cmp(r, ShifterOperand(0));
2649 b(label, EQ);
2650 } else {
2651 cbz(r, label);
2652 }
Dave Allison65fcc2c2014-04-28 13:45:27 -07002653}
2654
2655
2656void Thumb2Assembler::CompareAndBranchIfNonZero(Register r, Label* label) {
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01002657 if (force_32bit_branches_) {
2658 cmp(r, ShifterOperand(0));
2659 b(label, NE);
2660 } else {
2661 cbnz(r, label);
2662 }
Dave Allison65fcc2c2014-04-28 13:45:27 -07002663}
2664} // namespace arm
2665} // namespace art