2 * Tiny Code Generator for QEMU
4 * Copyright (c) 2008 Andrzej Zaborowski
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
26 #include "../tcg-ldst.c.inc"
27 #include "../tcg-pool.c.inc"
29 int arm_arch = __ARM_ARCH;
31 #ifndef use_idiv_instructions
32 bool use_idiv_instructions;
34 #ifndef use_neon_instructions
35 bool use_neon_instructions;
38 #ifdef CONFIG_DEBUG_TCG
39 static const char * const tcg_target_reg_names[TCG_TARGET_NB_REGS] = {
40 "%r0", "%r1", "%r2", "%r3", "%r4", "%r5", "%r6", "%r7",
41 "%r8", "%r9", "%r10", "%r11", "%r12", "%sp", "%r14", "%pc",
42 "%q0", "%q1", "%q2", "%q3", "%q4", "%q5", "%q6", "%q7",
43 "%q8", "%q9", "%q10", "%q11", "%q12", "%q13", "%q14", "%q15",
47 static const int tcg_target_reg_alloc_order[] = {
68 /* Q4 - Q7 are call-saved, and skipped. */
79 static const int tcg_target_call_iarg_regs[4] = {
80 TCG_REG_R0, TCG_REG_R1, TCG_REG_R2, TCG_REG_R3
83 static TCGReg tcg_target_call_oarg_reg(TCGCallReturnKind kind, int slot)
85 tcg_debug_assert(kind == TCG_CALL_RET_NORMAL);
86 tcg_debug_assert(slot >= 0 && slot <= 3);
87 return TCG_REG_R0 + slot;
90 #define TCG_REG_TMP TCG_REG_R12
91 #define TCG_VEC_TMP TCG_REG_Q15
92 #ifndef CONFIG_SOFTMMU
93 #define TCG_REG_GUEST_BASE TCG_REG_R11
99 COND_CS = 0x2, /* Unsigned greater or equal */
100 COND_CC = 0x3, /* Unsigned less than */
101 COND_MI = 0x4, /* Negative */
102 COND_PL = 0x5, /* Zero or greater */
103 COND_VS = 0x6, /* Overflow */
104 COND_VC = 0x7, /* No overflow */
105 COND_HI = 0x8, /* Unsigned greater than */
106 COND_LS = 0x9, /* Unsigned less or equal */
114 #define TO_CPSR (1 << 20)
116 #define SHIFT_IMM_LSL(im) (((im) << 7) | 0x00)
117 #define SHIFT_IMM_LSR(im) (((im) << 7) | 0x20)
118 #define SHIFT_IMM_ASR(im) (((im) << 7) | 0x40)
119 #define SHIFT_IMM_ROR(im) (((im) << 7) | 0x60)
120 #define SHIFT_REG_LSL(rs) (((rs) << 8) | 0x10)
121 #define SHIFT_REG_LSR(rs) (((rs) << 8) | 0x30)
122 #define SHIFT_REG_ASR(rs) (((rs) << 8) | 0x50)
123 #define SHIFT_REG_ROR(rs) (((rs) << 8) | 0x70)
126 ARITH_AND = 0x0 << 21,
127 ARITH_EOR = 0x1 << 21,
128 ARITH_SUB = 0x2 << 21,
129 ARITH_RSB = 0x3 << 21,
130 ARITH_ADD = 0x4 << 21,
131 ARITH_ADC = 0x5 << 21,
132 ARITH_SBC = 0x6 << 21,
133 ARITH_RSC = 0x7 << 21,
134 ARITH_TST = 0x8 << 21 | TO_CPSR,
135 ARITH_CMP = 0xa << 21 | TO_CPSR,
136 ARITH_CMN = 0xb << 21 | TO_CPSR,
137 ARITH_ORR = 0xc << 21,
138 ARITH_MOV = 0xd << 21,
139 ARITH_BIC = 0xe << 21,
140 ARITH_MVN = 0xf << 21,
144 INSN_CLZ = 0x016f0f10,
145 INSN_RBIT = 0x06ff0f30,
147 INSN_LDMIA = 0x08b00000,
148 INSN_STMDB = 0x09200000,
150 INSN_LDR_IMM = 0x04100000,
151 INSN_LDR_REG = 0x06100000,
152 INSN_STR_IMM = 0x04000000,
153 INSN_STR_REG = 0x06000000,
155 INSN_LDRH_IMM = 0x005000b0,
156 INSN_LDRH_REG = 0x001000b0,
157 INSN_LDRSH_IMM = 0x005000f0,
158 INSN_LDRSH_REG = 0x001000f0,
159 INSN_STRH_IMM = 0x004000b0,
160 INSN_STRH_REG = 0x000000b0,
162 INSN_LDRB_IMM = 0x04500000,
163 INSN_LDRB_REG = 0x06500000,
164 INSN_LDRSB_IMM = 0x005000d0,
165 INSN_LDRSB_REG = 0x001000d0,
166 INSN_STRB_IMM = 0x04400000,
167 INSN_STRB_REG = 0x06400000,
169 INSN_LDRD_IMM = 0x004000d0,
170 INSN_LDRD_REG = 0x000000d0,
171 INSN_STRD_IMM = 0x004000f0,
172 INSN_STRD_REG = 0x000000f0,
174 INSN_DMB_ISH = 0xf57ff05b,
175 INSN_DMB_MCR = 0xee070fba,
177 /* Architected nop introduced in v6k. */
178 /* ??? This is an MSR (imm) 0,0,0 insn. Anyone know if this
179 also Just So Happened to do nothing on pre-v6k so that we
180 don't need to conditionalize it? */
181 INSN_NOP_v6k = 0xe320f000,
182 /* Otherwise the assembler uses mov r0,r0 */
183 INSN_NOP_v4 = (COND_AL << 28) | ARITH_MOV,
185 INSN_VADD = 0xf2000800,
186 INSN_VAND = 0xf2000110,
187 INSN_VBIC = 0xf2100110,
188 INSN_VEOR = 0xf3000110,
189 INSN_VORN = 0xf2300110,
190 INSN_VORR = 0xf2200110,
191 INSN_VSUB = 0xf3000800,
192 INSN_VMUL = 0xf2000910,
193 INSN_VQADD = 0xf2000010,
194 INSN_VQADD_U = 0xf3000010,
195 INSN_VQSUB = 0xf2000210,
196 INSN_VQSUB_U = 0xf3000210,
197 INSN_VMAX = 0xf2000600,
198 INSN_VMAX_U = 0xf3000600,
199 INSN_VMIN = 0xf2000610,
200 INSN_VMIN_U = 0xf3000610,
202 INSN_VABS = 0xf3b10300,
203 INSN_VMVN = 0xf3b00580,
204 INSN_VNEG = 0xf3b10380,
206 INSN_VCEQ0 = 0xf3b10100,
207 INSN_VCGT0 = 0xf3b10000,
208 INSN_VCGE0 = 0xf3b10080,
209 INSN_VCLE0 = 0xf3b10180,
210 INSN_VCLT0 = 0xf3b10200,
212 INSN_VCEQ = 0xf3000810,
213 INSN_VCGE = 0xf2000310,
214 INSN_VCGT = 0xf2000300,
215 INSN_VCGE_U = 0xf3000310,
216 INSN_VCGT_U = 0xf3000300,
218 INSN_VSHLI = 0xf2800510, /* VSHL (immediate) */
219 INSN_VSARI = 0xf2800010, /* VSHR.S */
220 INSN_VSHRI = 0xf3800010, /* VSHR.U */
221 INSN_VSLI = 0xf3800510,
222 INSN_VSHL_S = 0xf2000400, /* VSHL.S (register) */
223 INSN_VSHL_U = 0xf3000400, /* VSHL.U (register) */
225 INSN_VBSL = 0xf3100110,
226 INSN_VBIT = 0xf3200110,
227 INSN_VBIF = 0xf3300110,
229 INSN_VTST = 0xf2000810,
231 INSN_VDUP_G = 0xee800b10, /* VDUP (ARM core register) */
232 INSN_VDUP_S = 0xf3b00c00, /* VDUP (scalar) */
233 INSN_VLDR_D = 0xed100b00, /* VLDR.64 */
234 INSN_VLD1 = 0xf4200000, /* VLD1 (multiple single elements) */
235 INSN_VLD1R = 0xf4a00c00, /* VLD1 (single element to all lanes) */
236 INSN_VST1 = 0xf4000000, /* VST1 (multiple single elements) */
237 INSN_VMOVI = 0xf2800010, /* VMOV (immediate) */
240 #define INSN_NOP (use_armv7_instructions ? INSN_NOP_v6k : INSN_NOP_v4)
242 static const uint8_t tcg_cond_to_arm_cond[] = {
243 [TCG_COND_EQ] = COND_EQ,
244 [TCG_COND_NE] = COND_NE,
245 [TCG_COND_LT] = COND_LT,
246 [TCG_COND_GE] = COND_GE,
247 [TCG_COND_LE] = COND_LE,
248 [TCG_COND_GT] = COND_GT,
250 [TCG_COND_LTU] = COND_CC,
251 [TCG_COND_GEU] = COND_CS,
252 [TCG_COND_LEU] = COND_LS,
253 [TCG_COND_GTU] = COND_HI,
256 static int encode_imm(uint32_t imm);
258 /* TCG private relocation type: add with pc+imm8 */
261 /* TCG private relocation type: vldr with imm8 << 2 */
262 #define R_ARM_PC11 12
264 static bool reloc_pc24(tcg_insn_unit *src_rw, const tcg_insn_unit *target)
266 const tcg_insn_unit *src_rx = tcg_splitwx_to_rx(src_rw);
267 ptrdiff_t offset = (tcg_ptr_byte_diff(target, src_rx) - 8) >> 2;
269 if (offset == sextract32(offset, 0, 24)) {
270 *src_rw = deposit32(*src_rw, 0, 24, offset);
276 static bool reloc_pc13(tcg_insn_unit *src_rw, const tcg_insn_unit *target)
278 const tcg_insn_unit *src_rx = tcg_splitwx_to_rx(src_rw);
279 ptrdiff_t offset = tcg_ptr_byte_diff(target, src_rx) - 8;
281 if (offset >= -0xfff && offset <= 0xfff) {
282 tcg_insn_unit insn = *src_rw;
283 bool u = (offset >= 0);
287 insn = deposit32(insn, 23, 1, u);
288 insn = deposit32(insn, 0, 12, offset);
295 static bool reloc_pc11(tcg_insn_unit *src_rw, const tcg_insn_unit *target)
297 const tcg_insn_unit *src_rx = tcg_splitwx_to_rx(src_rw);
298 ptrdiff_t offset = (tcg_ptr_byte_diff(target, src_rx) - 8) / 4;
300 if (offset >= -0xff && offset <= 0xff) {
301 tcg_insn_unit insn = *src_rw;
302 bool u = (offset >= 0);
306 insn = deposit32(insn, 23, 1, u);
307 insn = deposit32(insn, 0, 8, offset);
314 static bool reloc_pc8(tcg_insn_unit *src_rw, const tcg_insn_unit *target)
316 const tcg_insn_unit *src_rx = tcg_splitwx_to_rx(src_rw);
317 ptrdiff_t offset = tcg_ptr_byte_diff(target, src_rx) - 8;
318 int imm12 = encode_imm(offset);
321 *src_rw = deposit32(*src_rw, 0, 12, imm12);
327 static bool patch_reloc(tcg_insn_unit *code_ptr, int type,
328 intptr_t value, intptr_t addend)
330 tcg_debug_assert(addend == 0);
333 return reloc_pc24(code_ptr, (const tcg_insn_unit *)value);
335 return reloc_pc13(code_ptr, (const tcg_insn_unit *)value);
337 return reloc_pc11(code_ptr, (const tcg_insn_unit *)value);
339 return reloc_pc8(code_ptr, (const tcg_insn_unit *)value);
341 g_assert_not_reached();
345 #define TCG_CT_CONST_ARM 0x100
346 #define TCG_CT_CONST_INV 0x200
347 #define TCG_CT_CONST_NEG 0x400
348 #define TCG_CT_CONST_ZERO 0x800
349 #define TCG_CT_CONST_ORRI 0x1000
350 #define TCG_CT_CONST_ANDI 0x2000
352 #define ALL_GENERAL_REGS 0xffffu
353 #define ALL_VECTOR_REGS 0xffff0000u
356 * r0-r3 will be overwritten when reading the tlb entry (system-mode only);
357 * r14 will be overwritten by the BLNE branching to the slow path.
359 #ifdef CONFIG_SOFTMMU
360 #define ALL_QLDST_REGS \
361 (ALL_GENERAL_REGS & ~((1 << TCG_REG_R0) | (1 << TCG_REG_R1) | \
362 (1 << TCG_REG_R2) | (1 << TCG_REG_R3) | \
365 #define ALL_QLDST_REGS (ALL_GENERAL_REGS & ~(1 << TCG_REG_R14))
369 * ARM immediates for ALU instructions are made of an unsigned 8-bit
370 * right-rotated by an even amount between 0 and 30.
372 * Return < 0 if @imm cannot be encoded, else the entire imm12 field.
374 static int encode_imm(uint32_t imm)
378 /* Simple case, no rotation required. */
379 if ((imm & ~0xff) == 0) {
383 /* Next, try a simple even shift. */
384 rot = ctz32(imm) & ~1;
387 if ((imm8 & ~0xff) == 0) {
392 * Finally, try harder with rotations.
393 * The ctz test above will have taken care of rotates >= 8.
395 for (rot = 2; rot < 8; rot += 2) {
396 imm8 = rol32(imm, rot);
397 if ((imm8 & ~0xff) == 0) {
401 /* Fail: imm cannot be encoded. */
405 /* Note that rot is even, and we discard bit 0 by shifting by 7. */
406 return rot << 7 | imm8;
409 static int encode_imm_nofail(uint32_t imm)
411 int ret = encode_imm(imm);
412 tcg_debug_assert(ret >= 0);
416 static bool check_fit_imm(uint32_t imm)
418 return encode_imm(imm) >= 0;
421 /* Return true if v16 is a valid 16-bit shifted immediate. */
422 static bool is_shimm16(uint16_t v16, int *cmode, int *imm8)
424 if (v16 == (v16 & 0xff)) {
428 } else if (v16 == (v16 & 0xff00)) {
436 /* Return true if v32 is a valid 32-bit shifted immediate. */
437 static bool is_shimm32(uint32_t v32, int *cmode, int *imm8)
439 if (v32 == (v32 & 0xff)) {
443 } else if (v32 == (v32 & 0xff00)) {
445 *imm8 = (v32 >> 8) & 0xff;
447 } else if (v32 == (v32 & 0xff0000)) {
449 *imm8 = (v32 >> 16) & 0xff;
451 } else if (v32 == (v32 & 0xff000000)) {
459 /* Return true if v32 is a valid 32-bit shifting ones immediate. */
460 static bool is_soimm32(uint32_t v32, int *cmode, int *imm8)
462 if ((v32 & 0xffff00ff) == 0xff) {
464 *imm8 = (v32 >> 8) & 0xff;
466 } else if ((v32 & 0xff00ffff) == 0xffff) {
468 *imm8 = (v32 >> 16) & 0xff;
475 * Return non-zero if v32 can be formed by MOVI+ORR.
476 * Place the parameters for MOVI in (cmode, imm8).
477 * Return the cmode for ORR; the imm8 can be had via extraction from v32.
479 static int is_shimm32_pair(uint32_t v32, int *cmode, int *imm8)
483 for (i = 6; i > 0; i -= 2) {
484 /* Mask out one byte we can add with ORR. */
485 uint32_t tmp = v32 & ~(0xffu << (i * 4));
486 if (is_shimm32(tmp, cmode, imm8) ||
487 is_soimm32(tmp, cmode, imm8)) {
494 /* Return true if V is a valid 16-bit or 32-bit shifted immediate. */
495 static bool is_shimm1632(uint32_t v32, int *cmode, int *imm8)
497 if (v32 == deposit32(v32, 16, 16, v32)) {
498 return is_shimm16(v32, cmode, imm8);
500 return is_shimm32(v32, cmode, imm8);
504 /* Test if a constant matches the constraint.
505 * TODO: define constraints for:
507 * ldr/str offset: between -0xfff and 0xfff
508 * ldrh/strh offset: between -0xff and 0xff
509 * mov operand2: values represented with x << (2 * y), x < 0x100
510 * add, sub, eor...: ditto
512 static bool tcg_target_const_match(int64_t val, TCGType type, int ct, int vece)
514 if (ct & TCG_CT_CONST) {
516 } else if ((ct & TCG_CT_CONST_ARM) && check_fit_imm(val)) {
518 } else if ((ct & TCG_CT_CONST_INV) && check_fit_imm(~val)) {
520 } else if ((ct & TCG_CT_CONST_NEG) && check_fit_imm(-val)) {
522 } else if ((ct & TCG_CT_CONST_ZERO) && val == 0) {
526 switch (ct & (TCG_CT_CONST_ORRI | TCG_CT_CONST_ANDI)) {
529 case TCG_CT_CONST_ANDI:
532 case TCG_CT_CONST_ORRI:
533 if (val == deposit64(val, 32, 32, val)) {
535 return is_shimm1632(val, &cmode, &imm8);
539 /* Both bits should not be set for the same insn. */
540 g_assert_not_reached();
546 static void tcg_out_b_imm(TCGContext *s, ARMCond cond, int32_t offset)
548 tcg_out32(s, (cond << 28) | INSN_B |
549 (((offset - 8) >> 2) & 0x00ffffff));
552 static void tcg_out_bl_imm(TCGContext *s, ARMCond cond, int32_t offset)
554 tcg_out32(s, (cond << 28) | 0x0b000000 |
555 (((offset - 8) >> 2) & 0x00ffffff));
558 static void tcg_out_blx_reg(TCGContext *s, ARMCond cond, TCGReg rn)
560 tcg_out32(s, (cond << 28) | 0x012fff30 | rn);
563 static void tcg_out_blx_imm(TCGContext *s, int32_t offset)
565 tcg_out32(s, 0xfa000000 | ((offset & 2) << 23) |
566 (((offset - 8) >> 2) & 0x00ffffff));
569 static void tcg_out_dat_reg(TCGContext *s, ARMCond cond, ARMInsn opc,
570 TCGReg rd, TCGReg rn, TCGReg rm, int shift)
572 tcg_out32(s, (cond << 28) | (0 << 25) | opc |
573 (rn << 16) | (rd << 12) | shift | rm);
576 static void tcg_out_mov_reg(TCGContext *s, ARMCond cond, TCGReg rd, TCGReg rm)
578 /* Simple reg-reg move, optimising out the 'do nothing' case */
580 tcg_out_dat_reg(s, cond, ARITH_MOV, rd, 0, rm, SHIFT_IMM_LSL(0));
584 static void tcg_out_bx_reg(TCGContext *s, ARMCond cond, TCGReg rn)
586 tcg_out32(s, (cond << 28) | 0x012fff10 | rn);
589 static void tcg_out_b_reg(TCGContext *s, ARMCond cond, TCGReg rn)
592 * Unless the C portion of QEMU is compiled as thumb, we don't need
593 * true BX semantics; merely a branch to an address held in a register.
595 tcg_out_bx_reg(s, cond, rn);
598 static void tcg_out_dat_imm(TCGContext *s, ARMCond cond, ARMInsn opc,
599 TCGReg rd, TCGReg rn, int im)
601 tcg_out32(s, (cond << 28) | (1 << 25) | opc |
602 (rn << 16) | (rd << 12) | im);
605 static void tcg_out_ldstm(TCGContext *s, ARMCond cond, ARMInsn opc,
606 TCGReg rn, uint16_t mask)
608 tcg_out32(s, (cond << 28) | opc | (rn << 16) | mask);
611 /* Note that this routine is used for both LDR and LDRH formats, so we do
612 not wish to include an immediate shift at this point. */
613 static void tcg_out_memop_r(TCGContext *s, ARMCond cond, ARMInsn opc, TCGReg rt,
614 TCGReg rn, TCGReg rm, bool u, bool p, bool w)
616 tcg_out32(s, (cond << 28) | opc | (u << 23) | (p << 24)
617 | (w << 21) | (rn << 16) | (rt << 12) | rm);
620 static void tcg_out_memop_8(TCGContext *s, ARMCond cond, ARMInsn opc, TCGReg rt,
621 TCGReg rn, int imm8, bool p, bool w)
628 tcg_out32(s, (cond << 28) | opc | (u << 23) | (p << 24) | (w << 21) |
629 (rn << 16) | (rt << 12) | ((imm8 & 0xf0) << 4) | (imm8 & 0xf));
632 static void tcg_out_memop_12(TCGContext *s, ARMCond cond, ARMInsn opc,
633 TCGReg rt, TCGReg rn, int imm12, bool p, bool w)
640 tcg_out32(s, (cond << 28) | opc | (u << 23) | (p << 24) | (w << 21) |
641 (rn << 16) | (rt << 12) | imm12);
644 static void tcg_out_ld32_12(TCGContext *s, ARMCond cond, TCGReg rt,
645 TCGReg rn, int imm12)
647 tcg_out_memop_12(s, cond, INSN_LDR_IMM, rt, rn, imm12, 1, 0);
650 static void tcg_out_st32_12(TCGContext *s, ARMCond cond, TCGReg rt,
651 TCGReg rn, int imm12)
653 tcg_out_memop_12(s, cond, INSN_STR_IMM, rt, rn, imm12, 1, 0);
656 static void tcg_out_ld32_r(TCGContext *s, ARMCond cond, TCGReg rt,
657 TCGReg rn, TCGReg rm)
659 tcg_out_memop_r(s, cond, INSN_LDR_REG, rt, rn, rm, 1, 1, 0);
662 static void tcg_out_st32_r(TCGContext *s, ARMCond cond, TCGReg rt,
663 TCGReg rn, TCGReg rm)
665 tcg_out_memop_r(s, cond, INSN_STR_REG, rt, rn, rm, 1, 1, 0);
668 static void tcg_out_ldrd_8(TCGContext *s, ARMCond cond, TCGReg rt,
671 tcg_out_memop_8(s, cond, INSN_LDRD_IMM, rt, rn, imm8, 1, 0);
674 static void tcg_out_ldrd_r(TCGContext *s, ARMCond cond, TCGReg rt,
675 TCGReg rn, TCGReg rm)
677 tcg_out_memop_r(s, cond, INSN_LDRD_REG, rt, rn, rm, 1, 1, 0);
680 static void __attribute__((unused))
681 tcg_out_ldrd_rwb(TCGContext *s, ARMCond cond, TCGReg rt, TCGReg rn, TCGReg rm)
683 tcg_out_memop_r(s, cond, INSN_LDRD_REG, rt, rn, rm, 1, 1, 1);
686 static void __attribute__((unused))
687 tcg_out_strd_8(TCGContext *s, ARMCond cond, TCGReg rt, TCGReg rn, int imm8)
689 tcg_out_memop_8(s, cond, INSN_STRD_IMM, rt, rn, imm8, 1, 0);
692 static void tcg_out_strd_r(TCGContext *s, ARMCond cond, TCGReg rt,
693 TCGReg rn, TCGReg rm)
695 tcg_out_memop_r(s, cond, INSN_STRD_REG, rt, rn, rm, 1, 1, 0);
698 /* Register pre-increment with base writeback. */
699 static void tcg_out_ld32_rwb(TCGContext *s, ARMCond cond, TCGReg rt,
700 TCGReg rn, TCGReg rm)
702 tcg_out_memop_r(s, cond, INSN_LDR_REG, rt, rn, rm, 1, 1, 1);
705 static void tcg_out_st32_rwb(TCGContext *s, ARMCond cond, TCGReg rt,
706 TCGReg rn, TCGReg rm)
708 tcg_out_memop_r(s, cond, INSN_STR_REG, rt, rn, rm, 1, 1, 1);
711 static void tcg_out_ld16u_8(TCGContext *s, ARMCond cond, TCGReg rt,
714 tcg_out_memop_8(s, cond, INSN_LDRH_IMM, rt, rn, imm8, 1, 0);
717 static void tcg_out_st16_8(TCGContext *s, ARMCond cond, TCGReg rt,
720 tcg_out_memop_8(s, cond, INSN_STRH_IMM, rt, rn, imm8, 1, 0);
723 static void tcg_out_ld16u_r(TCGContext *s, ARMCond cond, TCGReg rt,
724 TCGReg rn, TCGReg rm)
726 tcg_out_memop_r(s, cond, INSN_LDRH_REG, rt, rn, rm, 1, 1, 0);
729 static void tcg_out_st16_r(TCGContext *s, ARMCond cond, TCGReg rt,
730 TCGReg rn, TCGReg rm)
732 tcg_out_memop_r(s, cond, INSN_STRH_REG, rt, rn, rm, 1, 1, 0);
735 static void tcg_out_ld16s_8(TCGContext *s, ARMCond cond, TCGReg rt,
738 tcg_out_memop_8(s, cond, INSN_LDRSH_IMM, rt, rn, imm8, 1, 0);
741 static void tcg_out_ld16s_r(TCGContext *s, ARMCond cond, TCGReg rt,
742 TCGReg rn, TCGReg rm)
744 tcg_out_memop_r(s, cond, INSN_LDRSH_REG, rt, rn, rm, 1, 1, 0);
747 static void tcg_out_ld8_12(TCGContext *s, ARMCond cond, TCGReg rt,
748 TCGReg rn, int imm12)
750 tcg_out_memop_12(s, cond, INSN_LDRB_IMM, rt, rn, imm12, 1, 0);
753 static void tcg_out_st8_12(TCGContext *s, ARMCond cond, TCGReg rt,
754 TCGReg rn, int imm12)
756 tcg_out_memop_12(s, cond, INSN_STRB_IMM, rt, rn, imm12, 1, 0);
759 static void tcg_out_ld8_r(TCGContext *s, ARMCond cond, TCGReg rt,
760 TCGReg rn, TCGReg rm)
762 tcg_out_memop_r(s, cond, INSN_LDRB_REG, rt, rn, rm, 1, 1, 0);
765 static void tcg_out_st8_r(TCGContext *s, ARMCond cond, TCGReg rt,
766 TCGReg rn, TCGReg rm)
768 tcg_out_memop_r(s, cond, INSN_STRB_REG, rt, rn, rm, 1, 1, 0);
771 static void tcg_out_ld8s_8(TCGContext *s, ARMCond cond, TCGReg rt,
774 tcg_out_memop_8(s, cond, INSN_LDRSB_IMM, rt, rn, imm8, 1, 0);
777 static void tcg_out_ld8s_r(TCGContext *s, ARMCond cond, TCGReg rt,
778 TCGReg rn, TCGReg rm)
780 tcg_out_memop_r(s, cond, INSN_LDRSB_REG, rt, rn, rm, 1, 1, 0);
783 static void tcg_out_movi_pool(TCGContext *s, ARMCond cond,
784 TCGReg rd, uint32_t arg)
786 new_pool_label(s, arg, R_ARM_PC13, s->code_ptr, 0);
787 tcg_out_ld32_12(s, cond, rd, TCG_REG_PC, 0);
790 static void tcg_out_movi32(TCGContext *s, ARMCond cond,
791 TCGReg rd, uint32_t arg)
793 int imm12, diff, opc, sh1, sh2;
794 uint32_t tt0, tt1, tt2;
796 /* Check a single MOV/MVN before anything else. */
797 imm12 = encode_imm(arg);
799 tcg_out_dat_imm(s, cond, ARITH_MOV, rd, 0, imm12);
802 imm12 = encode_imm(~arg);
804 tcg_out_dat_imm(s, cond, ARITH_MVN, rd, 0, imm12);
808 /* Check for a pc-relative address. This will usually be the TB,
809 or within the TB, which is immediately before the code block. */
810 diff = tcg_pcrel_diff(s, (void *)arg) - 8;
812 imm12 = encode_imm(diff);
814 tcg_out_dat_imm(s, cond, ARITH_ADD, rd, TCG_REG_PC, imm12);
818 imm12 = encode_imm(-diff);
820 tcg_out_dat_imm(s, cond, ARITH_SUB, rd, TCG_REG_PC, imm12);
825 /* Use movw + movt. */
826 if (use_armv7_instructions) {
828 tcg_out32(s, (cond << 28) | 0x03000000 | (rd << 12)
829 | ((arg << 4) & 0x000f0000) | (arg & 0xfff));
830 if (arg & 0xffff0000) {
832 tcg_out32(s, (cond << 28) | 0x03400000 | (rd << 12)
833 | ((arg >> 12) & 0x000f0000) | ((arg >> 16) & 0xfff));
838 /* Look for sequences of two insns. If we have lots of 1's, we can
839 shorten the sequence by beginning with mvn and then clearing
840 higher bits with eor. */
843 if (ctpop32(arg) > 16) {
847 sh1 = ctz32(tt0) & ~1;
848 tt1 = tt0 & ~(0xff << sh1);
849 sh2 = ctz32(tt1) & ~1;
850 tt2 = tt1 & ~(0xff << sh2);
854 rot = ((32 - sh1) << 7) & 0xf00;
855 tcg_out_dat_imm(s, cond, opc, rd, 0, ((tt0 >> sh1) & 0xff) | rot);
856 rot = ((32 - sh2) << 7) & 0xf00;
857 tcg_out_dat_imm(s, cond, ARITH_EOR, rd, rd,
858 ((tt0 >> sh2) & 0xff) | rot);
862 /* Otherwise, drop it into the constant pool. */
863 tcg_out_movi_pool(s, cond, rd, arg);
867 * Emit either the reg,imm or reg,reg form of a data-processing insn.
868 * rhs must satisfy the "rI" constraint.
870 static void tcg_out_dat_rI(TCGContext *s, ARMCond cond, ARMInsn opc,
871 TCGReg dst, TCGReg lhs, TCGArg rhs, int rhs_is_const)
874 tcg_out_dat_imm(s, cond, opc, dst, lhs, encode_imm_nofail(rhs));
876 tcg_out_dat_reg(s, cond, opc, dst, lhs, rhs, SHIFT_IMM_LSL(0));
881 * Emit either the reg,imm or reg,reg form of a data-processing insn.
882 * rhs must satisfy the "rIK" constraint.
884 static void tcg_out_dat_rIK(TCGContext *s, ARMCond cond, ARMInsn opc,
885 ARMInsn opinv, TCGReg dst, TCGReg lhs, TCGArg rhs,
889 int imm12 = encode_imm(rhs);
891 imm12 = encode_imm_nofail(~rhs);
894 tcg_out_dat_imm(s, cond, opc, dst, lhs, imm12);
896 tcg_out_dat_reg(s, cond, opc, dst, lhs, rhs, SHIFT_IMM_LSL(0));
900 static void tcg_out_dat_rIN(TCGContext *s, ARMCond cond, ARMInsn opc,
901 ARMInsn opneg, TCGReg dst, TCGReg lhs, TCGArg rhs,
904 /* Emit either the reg,imm or reg,reg form of a data-processing insn.
905 * rhs must satisfy the "rIN" constraint.
908 int imm12 = encode_imm(rhs);
910 imm12 = encode_imm_nofail(-rhs);
913 tcg_out_dat_imm(s, cond, opc, dst, lhs, imm12);
915 tcg_out_dat_reg(s, cond, opc, dst, lhs, rhs, SHIFT_IMM_LSL(0));
919 static void tcg_out_mul32(TCGContext *s, ARMCond cond, TCGReg rd,
920 TCGReg rn, TCGReg rm)
923 tcg_out32(s, (cond << 28) | 0x90 | (rd << 16) | (rm << 8) | rn);
926 static void tcg_out_umull32(TCGContext *s, ARMCond cond, TCGReg rd0,
927 TCGReg rd1, TCGReg rn, TCGReg rm)
930 tcg_out32(s, (cond << 28) | 0x00800090 |
931 (rd1 << 16) | (rd0 << 12) | (rm << 8) | rn);
934 static void tcg_out_smull32(TCGContext *s, ARMCond cond, TCGReg rd0,
935 TCGReg rd1, TCGReg rn, TCGReg rm)
938 tcg_out32(s, (cond << 28) | 0x00c00090 |
939 (rd1 << 16) | (rd0 << 12) | (rm << 8) | rn);
942 static void tcg_out_sdiv(TCGContext *s, ARMCond cond,
943 TCGReg rd, TCGReg rn, TCGReg rm)
945 tcg_out32(s, 0x0710f010 | (cond << 28) | (rd << 16) | rn | (rm << 8));
948 static void tcg_out_udiv(TCGContext *s, ARMCond cond,
949 TCGReg rd, TCGReg rn, TCGReg rm)
951 tcg_out32(s, 0x0730f010 | (cond << 28) | (rd << 16) | rn | (rm << 8));
954 static void tcg_out_ext8s(TCGContext *s, TCGType t, TCGReg rd, TCGReg rn)
957 tcg_out32(s, 0x06af0070 | (COND_AL << 28) | (rd << 12) | rn);
960 static void tcg_out_ext8u(TCGContext *s, TCGReg rd, TCGReg rn)
962 tcg_out_dat_imm(s, COND_AL, ARITH_AND, rd, rn, 0xff);
965 static void tcg_out_ext16s(TCGContext *s, TCGType t, TCGReg rd, TCGReg rn)
968 tcg_out32(s, 0x06bf0070 | (COND_AL << 28) | (rd << 12) | rn);
971 static void tcg_out_ext16u(TCGContext *s, TCGReg rd, TCGReg rn)
974 tcg_out32(s, 0x06ff0070 | (COND_AL << 28) | (rd << 12) | rn);
977 static void tcg_out_ext32s(TCGContext *s, TCGReg rd, TCGReg rn)
979 g_assert_not_reached();
982 static void tcg_out_ext32u(TCGContext *s, TCGReg rd, TCGReg rn)
984 g_assert_not_reached();
987 static void tcg_out_exts_i32_i64(TCGContext *s, TCGReg rd, TCGReg rn)
989 g_assert_not_reached();
992 static void tcg_out_extu_i32_i64(TCGContext *s, TCGReg rd, TCGReg rn)
994 g_assert_not_reached();
997 static void tcg_out_extrl_i64_i32(TCGContext *s, TCGReg rd, TCGReg rn)
999 g_assert_not_reached();
1002 static void tcg_out_bswap16(TCGContext *s, ARMCond cond,
1003 TCGReg rd, TCGReg rn, int flags)
1005 if (flags & TCG_BSWAP_OS) {
1007 tcg_out32(s, 0x06ff0fb0 | (cond << 28) | (rd << 12) | rn);
1012 tcg_out32(s, 0x06bf0fb0 | (cond << 28) | (rd << 12) | rn);
1013 if ((flags & (TCG_BSWAP_IZ | TCG_BSWAP_OZ)) == TCG_BSWAP_OZ) {
1015 tcg_out32(s, 0x06ff0070 | (cond << 28) | (rd << 12) | rd);
1019 static void tcg_out_bswap32(TCGContext *s, ARMCond cond, TCGReg rd, TCGReg rn)
1022 tcg_out32(s, 0x06bf0f30 | (cond << 28) | (rd << 12) | rn);
1025 static void tcg_out_deposit(TCGContext *s, ARMCond cond, TCGReg rd,
1026 TCGArg a1, int ofs, int len, bool const_a1)
1029 /* bfi becomes bfc with rn == 15. */
1033 tcg_out32(s, 0x07c00010 | (cond << 28) | (rd << 12) | a1
1034 | (ofs << 7) | ((ofs + len - 1) << 16));
1037 static void tcg_out_extract(TCGContext *s, ARMCond cond, TCGReg rd,
1038 TCGReg rn, int ofs, int len)
1041 tcg_out32(s, 0x07e00050 | (cond << 28) | (rd << 12) | rn
1042 | (ofs << 7) | ((len - 1) << 16));
1045 static void tcg_out_sextract(TCGContext *s, ARMCond cond, TCGReg rd,
1046 TCGReg rn, int ofs, int len)
1049 tcg_out32(s, 0x07a00050 | (cond << 28) | (rd << 12) | rn
1050 | (ofs << 7) | ((len - 1) << 16));
1053 static void tcg_out_ld32u(TCGContext *s, ARMCond cond,
1054 TCGReg rd, TCGReg rn, int32_t offset)
1056 if (offset > 0xfff || offset < -0xfff) {
1057 tcg_out_movi32(s, cond, TCG_REG_TMP, offset);
1058 tcg_out_ld32_r(s, cond, rd, rn, TCG_REG_TMP);
1060 tcg_out_ld32_12(s, cond, rd, rn, offset);
1063 static void tcg_out_st32(TCGContext *s, ARMCond cond,
1064 TCGReg rd, TCGReg rn, int32_t offset)
1066 if (offset > 0xfff || offset < -0xfff) {
1067 tcg_out_movi32(s, cond, TCG_REG_TMP, offset);
1068 tcg_out_st32_r(s, cond, rd, rn, TCG_REG_TMP);
1070 tcg_out_st32_12(s, cond, rd, rn, offset);
1073 static void tcg_out_ld16u(TCGContext *s, ARMCond cond,
1074 TCGReg rd, TCGReg rn, int32_t offset)
1076 if (offset > 0xff || offset < -0xff) {
1077 tcg_out_movi32(s, cond, TCG_REG_TMP, offset);
1078 tcg_out_ld16u_r(s, cond, rd, rn, TCG_REG_TMP);
1080 tcg_out_ld16u_8(s, cond, rd, rn, offset);
1083 static void tcg_out_ld16s(TCGContext *s, ARMCond cond,
1084 TCGReg rd, TCGReg rn, int32_t offset)
1086 if (offset > 0xff || offset < -0xff) {
1087 tcg_out_movi32(s, cond, TCG_REG_TMP, offset);
1088 tcg_out_ld16s_r(s, cond, rd, rn, TCG_REG_TMP);
1090 tcg_out_ld16s_8(s, cond, rd, rn, offset);
1093 static void tcg_out_st16(TCGContext *s, ARMCond cond,
1094 TCGReg rd, TCGReg rn, int32_t offset)
1096 if (offset > 0xff || offset < -0xff) {
1097 tcg_out_movi32(s, cond, TCG_REG_TMP, offset);
1098 tcg_out_st16_r(s, cond, rd, rn, TCG_REG_TMP);
1100 tcg_out_st16_8(s, cond, rd, rn, offset);
1103 static void tcg_out_ld8u(TCGContext *s, ARMCond cond,
1104 TCGReg rd, TCGReg rn, int32_t offset)
1106 if (offset > 0xfff || offset < -0xfff) {
1107 tcg_out_movi32(s, cond, TCG_REG_TMP, offset);
1108 tcg_out_ld8_r(s, cond, rd, rn, TCG_REG_TMP);
1110 tcg_out_ld8_12(s, cond, rd, rn, offset);
1113 static void tcg_out_ld8s(TCGContext *s, ARMCond cond,
1114 TCGReg rd, TCGReg rn, int32_t offset)
1116 if (offset > 0xff || offset < -0xff) {
1117 tcg_out_movi32(s, cond, TCG_REG_TMP, offset);
1118 tcg_out_ld8s_r(s, cond, rd, rn, TCG_REG_TMP);
1120 tcg_out_ld8s_8(s, cond, rd, rn, offset);
1123 static void tcg_out_st8(TCGContext *s, ARMCond cond,
1124 TCGReg rd, TCGReg rn, int32_t offset)
1126 if (offset > 0xfff || offset < -0xfff) {
1127 tcg_out_movi32(s, cond, TCG_REG_TMP, offset);
1128 tcg_out_st8_r(s, cond, rd, rn, TCG_REG_TMP);
1130 tcg_out_st8_12(s, cond, rd, rn, offset);
1134 * The _goto case is normally between TBs within the same code buffer, and
1135 * with the code buffer limited to 16MB we wouldn't need the long case.
1136 * But we also use it for the tail-call to the qemu_ld/st helpers, which does.
1138 static void tcg_out_goto(TCGContext *s, ARMCond cond, const tcg_insn_unit *addr)
1140 intptr_t addri = (intptr_t)addr;
1141 ptrdiff_t disp = tcg_pcrel_diff(s, addr);
1142 bool arm_mode = !(addri & 1);
1144 if (arm_mode && disp - 8 < 0x01fffffd && disp - 8 > -0x01fffffd) {
1145 tcg_out_b_imm(s, cond, disp);
1149 /* LDR is interworking from v5t. */
1150 tcg_out_movi_pool(s, cond, TCG_REG_PC, addri);
1154 * The call case is mostly used for helpers - so it's not unreasonable
1155 * for them to be beyond branch range.
1157 static void tcg_out_call_int(TCGContext *s, const tcg_insn_unit *addr)
1159 intptr_t addri = (intptr_t)addr;
1160 ptrdiff_t disp = tcg_pcrel_diff(s, addr);
1161 bool arm_mode = !(addri & 1);
1163 if (disp - 8 < 0x02000000 && disp - 8 >= -0x02000000) {
1165 tcg_out_bl_imm(s, COND_AL, disp);
1167 tcg_out_blx_imm(s, disp);
1172 tcg_out_movi32(s, COND_AL, TCG_REG_TMP, addri);
1173 tcg_out_blx_reg(s, COND_AL, TCG_REG_TMP);
1176 static void tcg_out_call(TCGContext *s, const tcg_insn_unit *addr,
1177 const TCGHelperInfo *info)
1179 tcg_out_call_int(s, addr);
1182 static void tcg_out_goto_label(TCGContext *s, ARMCond cond, TCGLabel *l)
1185 tcg_out_goto(s, cond, l->u.value_ptr);
1187 tcg_out_reloc(s, s->code_ptr, R_ARM_PC24, l, 0);
1188 tcg_out_b_imm(s, cond, 0);
1192 static void tcg_out_mb(TCGContext *s, TCGArg a0)
1194 if (use_armv7_instructions) {
1195 tcg_out32(s, INSN_DMB_ISH);
1197 tcg_out32(s, INSN_DMB_MCR);
1201 static TCGCond tcg_out_cmp2(TCGContext *s, const TCGArg *args,
1202 const int *const_args)
1204 TCGReg al = args[0];
1205 TCGReg ah = args[1];
1206 TCGArg bl = args[2];
1207 TCGArg bh = args[3];
1208 TCGCond cond = args[4];
1209 int const_bl = const_args[2];
1210 int const_bh = const_args[3];
1220 * We perform a conditional comparison. If the high half is
1221 * equal, then overwrite the flags with the comparison of the
1222 * low half. The resulting flags cover the whole.
1224 tcg_out_dat_rI(s, COND_AL, ARITH_CMP, 0, ah, bh, const_bh);
1225 tcg_out_dat_rI(s, COND_EQ, ARITH_CMP, 0, al, bl, const_bl);
1230 /* We perform a double-word subtraction and examine the result.
1231 We do not actually need the result of the subtract, so the
1232 low part "subtract" is a compare. For the high half we have
1233 no choice but to compute into a temporary. */
1234 tcg_out_dat_rI(s, COND_AL, ARITH_CMP, 0, al, bl, const_bl);
1235 tcg_out_dat_rI(s, COND_AL, ARITH_SBC | TO_CPSR,
1236 TCG_REG_TMP, ah, bh, const_bh);
1241 /* Similar, but with swapped arguments, via reversed subtract. */
1242 tcg_out_dat_rI(s, COND_AL, ARITH_RSB | TO_CPSR,
1243 TCG_REG_TMP, al, bl, const_bl);
1244 tcg_out_dat_rI(s, COND_AL, ARITH_RSC | TO_CPSR,
1245 TCG_REG_TMP, ah, bh, const_bh);
1246 return tcg_swap_cond(cond);
1249 g_assert_not_reached();
1254 * Note that TCGReg references Q-registers.
1255 * Q-regno = 2 * D-regno, so shift left by 1 while inserting.
1257 static uint32_t encode_vd(TCGReg rd)
1259 tcg_debug_assert(rd >= TCG_REG_Q0);
1260 return (extract32(rd, 3, 1) << 22) | (extract32(rd, 0, 3) << 13);
1263 static uint32_t encode_vn(TCGReg rn)
1265 tcg_debug_assert(rn >= TCG_REG_Q0);
1266 return (extract32(rn, 3, 1) << 7) | (extract32(rn, 0, 3) << 17);
1269 static uint32_t encode_vm(TCGReg rm)
1271 tcg_debug_assert(rm >= TCG_REG_Q0);
1272 return (extract32(rm, 3, 1) << 5) | (extract32(rm, 0, 3) << 1);
1275 static void tcg_out_vreg2(TCGContext *s, ARMInsn insn, int q, int vece,
1278 tcg_out32(s, insn | (vece << 18) | (q << 6) |
1279 encode_vd(d) | encode_vm(m));
1282 static void tcg_out_vreg3(TCGContext *s, ARMInsn insn, int q, int vece,
1283 TCGReg d, TCGReg n, TCGReg m)
1285 tcg_out32(s, insn | (vece << 20) | (q << 6) |
1286 encode_vd(d) | encode_vn(n) | encode_vm(m));
1289 static void tcg_out_vmovi(TCGContext *s, TCGReg rd,
1290 int q, int op, int cmode, uint8_t imm8)
1292 tcg_out32(s, INSN_VMOVI | encode_vd(rd) | (q << 6) | (op << 5)
1293 | (cmode << 8) | extract32(imm8, 0, 4)
1294 | (extract32(imm8, 4, 3) << 16)
1295 | (extract32(imm8, 7, 1) << 24));
1298 static void tcg_out_vshifti(TCGContext *s, ARMInsn insn, int q,
1299 TCGReg rd, TCGReg rm, int l_imm6)
1301 tcg_out32(s, insn | (q << 6) | encode_vd(rd) | encode_vm(rm) |
1302 (extract32(l_imm6, 6, 1) << 7) |
1303 (extract32(l_imm6, 0, 6) << 16));
1306 static void tcg_out_vldst(TCGContext *s, ARMInsn insn,
1307 TCGReg rd, TCGReg rn, int offset)
1310 if (check_fit_imm(offset) || check_fit_imm(-offset)) {
1311 tcg_out_dat_rIN(s, COND_AL, ARITH_ADD, ARITH_SUB,
1312 TCG_REG_TMP, rn, offset, true);
1314 tcg_out_movi(s, TCG_TYPE_PTR, TCG_REG_TMP, offset);
1315 tcg_out_dat_reg(s, COND_AL, ARITH_ADD,
1316 TCG_REG_TMP, TCG_REG_TMP, rn, 0);
1320 tcg_out32(s, insn | (rn << 16) | encode_vd(rd) | 0xf);
1331 bool tcg_target_has_memory_bswap(MemOp memop)
1336 static TCGReg ldst_ra_gen(TCGContext *s, const TCGLabelQemuLdst *l, int arg)
1338 /* We arrive at the slow path via "BLNE", so R14 contains l->raddr. */
1342 static const TCGLdstHelperParam ldst_helper_param = {
1343 .ra_gen = ldst_ra_gen,
1345 .tmp = { TCG_REG_TMP },
1348 static bool tcg_out_qemu_ld_slow_path(TCGContext *s, TCGLabelQemuLdst *lb)
1350 MemOp opc = get_memop(lb->oi);
1352 if (!reloc_pc24(lb->label_ptr[0], tcg_splitwx_to_rx(s->code_ptr))) {
1356 tcg_out_ld_helper_args(s, lb, &ldst_helper_param);
1357 tcg_out_call_int(s, qemu_ld_helpers[opc & MO_SIZE]);
1358 tcg_out_ld_helper_ret(s, lb, false, &ldst_helper_param);
1360 tcg_out_goto(s, COND_AL, lb->raddr);
1364 static bool tcg_out_qemu_st_slow_path(TCGContext *s, TCGLabelQemuLdst *lb)
1366 MemOp opc = get_memop(lb->oi);
1368 if (!reloc_pc24(lb->label_ptr[0], tcg_splitwx_to_rx(s->code_ptr))) {
1372 tcg_out_st_helper_args(s, lb, &ldst_helper_param);
1374 /* Tail-call to the helper, which will return to the fast path. */
1375 tcg_out_goto(s, COND_AL, qemu_st_helpers[opc & MO_SIZE]);
1379 /* We expect to use an 9-bit sign-magnitude negative offset from ENV. */
1380 #define MIN_TLB_MASK_TABLE_OFS -256
1382 static TCGLabelQemuLdst *prepare_host_addr(TCGContext *s, HostAddress *h,
1383 TCGReg addrlo, TCGReg addrhi,
1384 MemOpIdx oi, bool is_ld)
1386 TCGLabelQemuLdst *ldst = NULL;
1387 MemOp opc = get_memop(oi);
1390 #ifdef CONFIG_SOFTMMU
1394 .index = TCG_REG_R1,
1395 .index_scratch = true,
1401 .index = guest_base ? TCG_REG_GUEST_BASE : -1,
1402 .index_scratch = false,
1406 h->aa = atom_and_align_for_opc(s, opc, MO_ATOM_IFALIGN, false);
1407 a_mask = (1 << h->aa.align) - 1;
1409 #ifdef CONFIG_SOFTMMU
1410 int mem_index = get_mmuidx(oi);
1411 int cmp_off = is_ld ? offsetof(CPUTLBEntry, addr_read)
1412 : offsetof(CPUTLBEntry, addr_write);
1413 int fast_off = tlb_mask_table_ofs(s, mem_index);
1414 unsigned s_mask = (1 << (opc & MO_SIZE)) - 1;
1417 ldst = new_ldst_label(s);
1418 ldst->is_ld = is_ld;
1420 ldst->addrlo_reg = addrlo;
1421 ldst->addrhi_reg = addrhi;
1423 /* Load cpu->neg.tlb.f[mmu_idx].{mask,table} into {r0,r1}. */
1424 QEMU_BUILD_BUG_ON(offsetof(CPUTLBDescFast, mask) != 0);
1425 QEMU_BUILD_BUG_ON(offsetof(CPUTLBDescFast, table) != 4);
1426 tcg_out_ldrd_8(s, COND_AL, TCG_REG_R0, TCG_AREG0, fast_off);
1428 /* Extract the tlb index from the address into R0. */
1429 tcg_out_dat_reg(s, COND_AL, ARITH_AND, TCG_REG_R0, TCG_REG_R0, addrlo,
1430 SHIFT_IMM_LSR(s->page_bits - CPU_TLB_ENTRY_BITS));
1433 * Add the tlb_table pointer, creating the CPUTLBEntry address in R1.
1434 * Load the tlb comparator into R2/R3 and the fast path addend into R1.
1436 QEMU_BUILD_BUG_ON(HOST_BIG_ENDIAN);
1438 if (s->addr_type == TCG_TYPE_I32) {
1439 tcg_out_ld32_rwb(s, COND_AL, TCG_REG_R2, TCG_REG_R1, TCG_REG_R0);
1441 tcg_out_ldrd_rwb(s, COND_AL, TCG_REG_R2, TCG_REG_R1, TCG_REG_R0);
1444 tcg_out_dat_reg(s, COND_AL, ARITH_ADD,
1445 TCG_REG_R1, TCG_REG_R1, TCG_REG_R0, 0);
1446 if (s->addr_type == TCG_TYPE_I32) {
1447 tcg_out_ld32_12(s, COND_AL, TCG_REG_R2, TCG_REG_R1, cmp_off);
1449 tcg_out_ldrd_8(s, COND_AL, TCG_REG_R2, TCG_REG_R1, cmp_off);
1453 /* Load the tlb addend. */
1454 tcg_out_ld32_12(s, COND_AL, TCG_REG_R1, TCG_REG_R1,
1455 offsetof(CPUTLBEntry, addend));
1458 * Check alignment, check comparators.
1459 * Do this in 2-4 insns. Use MOVW for v7, if possible,
1460 * to reduce the number of sequential conditional instructions.
1461 * Almost all guests have at least 4k pages, which means that we need
1462 * to clear at least 9 bits even for an 8-byte memory, which means it
1463 * isn't worth checking for an immediate operand for BIC.
1465 * For unaligned accesses, test the page of the last unit of alignment.
1466 * This leaves the least significant alignment bits unchanged, and of
1467 * course must be zero.
1470 if (a_mask < s_mask) {
1471 t_addr = TCG_REG_R0;
1472 tcg_out_dat_imm(s, COND_AL, ARITH_ADD, t_addr,
1473 addrlo, s_mask - a_mask);
1475 if (use_armv7_instructions && s->page_bits <= 16) {
1476 tcg_out_movi32(s, COND_AL, TCG_REG_TMP, ~(s->page_mask | a_mask));
1477 tcg_out_dat_reg(s, COND_AL, ARITH_BIC, TCG_REG_TMP,
1478 t_addr, TCG_REG_TMP, 0);
1479 tcg_out_dat_reg(s, COND_AL, ARITH_CMP, 0, TCG_REG_R2, TCG_REG_TMP, 0);
1482 tcg_debug_assert(a_mask <= 0xff);
1483 tcg_out_dat_imm(s, COND_AL, ARITH_TST, 0, addrlo, a_mask);
1485 tcg_out_dat_reg(s, COND_AL, ARITH_MOV, TCG_REG_TMP, 0, t_addr,
1486 SHIFT_IMM_LSR(s->page_bits));
1487 tcg_out_dat_reg(s, (a_mask ? COND_EQ : COND_AL), ARITH_CMP,
1488 0, TCG_REG_R2, TCG_REG_TMP,
1489 SHIFT_IMM_LSL(s->page_bits));
1492 if (s->addr_type != TCG_TYPE_I32) {
1493 tcg_out_dat_reg(s, COND_EQ, ARITH_CMP, 0, TCG_REG_R3, addrhi, 0);
1497 ldst = new_ldst_label(s);
1498 ldst->is_ld = is_ld;
1500 ldst->addrlo_reg = addrlo;
1501 ldst->addrhi_reg = addrhi;
1503 /* We are expecting alignment to max out at 7 */
1504 tcg_debug_assert(a_mask <= 0xff);
1505 /* tst addr, #mask */
1506 tcg_out_dat_imm(s, COND_AL, ARITH_TST, 0, addrlo, a_mask);
1513 static void tcg_out_qemu_ld_direct(TCGContext *s, MemOp opc, TCGReg datalo,
1514 TCGReg datahi, HostAddress h)
1518 /* Byte swapping is left to middle-end expansion. */
1519 tcg_debug_assert((opc & MO_BSWAP) == 0);
1521 switch (opc & MO_SSIZE) {
1524 tcg_out_ld8_12(s, h.cond, datalo, h.base, 0);
1526 tcg_out_ld8_r(s, h.cond, datalo, h.base, h.index);
1531 tcg_out_ld8s_8(s, h.cond, datalo, h.base, 0);
1533 tcg_out_ld8s_r(s, h.cond, datalo, h.base, h.index);
1538 tcg_out_ld16u_8(s, h.cond, datalo, h.base, 0);
1540 tcg_out_ld16u_r(s, h.cond, datalo, h.base, h.index);
1545 tcg_out_ld16s_8(s, h.cond, datalo, h.base, 0);
1547 tcg_out_ld16s_r(s, h.cond, datalo, h.base, h.index);
1552 tcg_out_ld32_12(s, h.cond, datalo, h.base, 0);
1554 tcg_out_ld32_r(s, h.cond, datalo, h.base, h.index);
1558 /* We used pair allocation for datalo, so already should be aligned. */
1559 tcg_debug_assert((datalo & 1) == 0);
1560 tcg_debug_assert(datahi == datalo + 1);
1561 /* LDRD requires alignment; double-check that. */
1562 if (get_alignment_bits(opc) >= MO_64) {
1564 tcg_out_ldrd_8(s, h.cond, datalo, h.base, 0);
1568 * Rm (the second address op) must not overlap Rt or Rt + 1.
1569 * Since datalo is aligned, we can simplify the test via alignment.
1570 * Flip the two address arguments if that works.
1572 if ((h.index & ~1) != datalo) {
1573 tcg_out_ldrd_r(s, h.cond, datalo, h.base, h.index);
1576 if ((h.base & ~1) != datalo) {
1577 tcg_out_ldrd_r(s, h.cond, datalo, h.index, h.base);
1583 if (datalo == h.base) {
1584 tcg_out_mov_reg(s, h.cond, TCG_REG_TMP, base);
1587 } else if (h.index_scratch) {
1588 tcg_out_ld32_rwb(s, h.cond, datalo, h.index, h.base);
1589 tcg_out_ld32_12(s, h.cond, datahi, h.index, 4);
1592 tcg_out_dat_reg(s, h.cond, ARITH_ADD, TCG_REG_TMP,
1593 h.base, h.index, SHIFT_IMM_LSL(0));
1596 tcg_out_ld32_12(s, h.cond, datalo, base, 0);
1597 tcg_out_ld32_12(s, h.cond, datahi, base, 4);
1600 g_assert_not_reached();
1604 static void tcg_out_qemu_ld(TCGContext *s, TCGReg datalo, TCGReg datahi,
1605 TCGReg addrlo, TCGReg addrhi,
1606 MemOpIdx oi, TCGType data_type)
1608 MemOp opc = get_memop(oi);
1609 TCGLabelQemuLdst *ldst;
1612 ldst = prepare_host_addr(s, &h, addrlo, addrhi, oi, true);
1614 ldst->type = data_type;
1615 ldst->datalo_reg = datalo;
1616 ldst->datahi_reg = datahi;
1619 * This a conditional BL only to load a pointer within this
1620 * opcode into LR for the slow path. We will not be using
1621 * the value for a tail call.
1623 ldst->label_ptr[0] = s->code_ptr;
1624 tcg_out_bl_imm(s, COND_NE, 0);
1626 tcg_out_qemu_ld_direct(s, opc, datalo, datahi, h);
1627 ldst->raddr = tcg_splitwx_to_rx(s->code_ptr);
1629 tcg_out_qemu_ld_direct(s, opc, datalo, datahi, h);
1633 static void tcg_out_qemu_st_direct(TCGContext *s, MemOp opc, TCGReg datalo,
1634 TCGReg datahi, HostAddress h)
1636 /* Byte swapping is left to middle-end expansion. */
1637 tcg_debug_assert((opc & MO_BSWAP) == 0);
1639 switch (opc & MO_SIZE) {
1642 tcg_out_st8_12(s, h.cond, datalo, h.base, 0);
1644 tcg_out_st8_r(s, h.cond, datalo, h.base, h.index);
1649 tcg_out_st16_8(s, h.cond, datalo, h.base, 0);
1651 tcg_out_st16_r(s, h.cond, datalo, h.base, h.index);
1656 tcg_out_st32_12(s, h.cond, datalo, h.base, 0);
1658 tcg_out_st32_r(s, h.cond, datalo, h.base, h.index);
1662 /* We used pair allocation for datalo, so already should be aligned. */
1663 tcg_debug_assert((datalo & 1) == 0);
1664 tcg_debug_assert(datahi == datalo + 1);
1665 /* STRD requires alignment; double-check that. */
1666 if (get_alignment_bits(opc) >= MO_64) {
1668 tcg_out_strd_8(s, h.cond, datalo, h.base, 0);
1670 tcg_out_strd_r(s, h.cond, datalo, h.base, h.index);
1672 } else if (h.index_scratch) {
1673 tcg_out_st32_rwb(s, h.cond, datalo, h.index, h.base);
1674 tcg_out_st32_12(s, h.cond, datahi, h.index, 4);
1676 tcg_out_dat_reg(s, h.cond, ARITH_ADD, TCG_REG_TMP,
1677 h.base, h.index, SHIFT_IMM_LSL(0));
1678 tcg_out_st32_12(s, h.cond, datalo, TCG_REG_TMP, 0);
1679 tcg_out_st32_12(s, h.cond, datahi, TCG_REG_TMP, 4);
1683 g_assert_not_reached();
1687 static void tcg_out_qemu_st(TCGContext *s, TCGReg datalo, TCGReg datahi,
1688 TCGReg addrlo, TCGReg addrhi,
1689 MemOpIdx oi, TCGType data_type)
1691 MemOp opc = get_memop(oi);
1692 TCGLabelQemuLdst *ldst;
1695 ldst = prepare_host_addr(s, &h, addrlo, addrhi, oi, false);
1697 ldst->type = data_type;
1698 ldst->datalo_reg = datalo;
1699 ldst->datahi_reg = datahi;
1702 tcg_out_qemu_st_direct(s, opc, datalo, datahi, h);
1704 /* The conditional call is last, as we're going to return here. */
1705 ldst->label_ptr[0] = s->code_ptr;
1706 tcg_out_bl_imm(s, COND_NE, 0);
1707 ldst->raddr = tcg_splitwx_to_rx(s->code_ptr);
1709 tcg_out_qemu_st_direct(s, opc, datalo, datahi, h);
1713 static void tcg_out_epilogue(TCGContext *s);
1715 static void tcg_out_exit_tb(TCGContext *s, uintptr_t arg)
1717 tcg_out_movi(s, TCG_TYPE_PTR, TCG_REG_R0, arg);
1718 tcg_out_epilogue(s);
1721 static void tcg_out_goto_tb(TCGContext *s, int which)
1726 /* Direct branch will be patched by tb_target_set_jmp_target. */
1727 set_jmp_insn_offset(s, which);
1728 tcg_out32(s, INSN_NOP);
1730 /* When branch is out of range, fall through to indirect. */
1731 i_addr = get_jmp_target_addr(s, which);
1732 i_disp = tcg_pcrel_diff(s, (void *)i_addr) - 8;
1733 tcg_debug_assert(i_disp < 0);
1734 if (i_disp >= -0xfff) {
1735 tcg_out_ld32_12(s, COND_AL, TCG_REG_PC, TCG_REG_PC, i_disp);
1738 * The TB is close, but outside the 12 bits addressable by
1739 * the load. We can extend this to 20 bits with a sub of a
1740 * shifted immediate from pc.
1745 h = encode_imm_nofail(h - l);
1746 tcg_out_dat_imm(s, COND_AL, ARITH_SUB, TCG_REG_R0, TCG_REG_PC, h);
1747 tcg_out_ld32_12(s, COND_AL, TCG_REG_PC, TCG_REG_R0, l);
1749 set_jmp_reset_offset(s, which);
1752 void tb_target_set_jmp_target(const TranslationBlock *tb, int n,
1753 uintptr_t jmp_rx, uintptr_t jmp_rw)
1755 uintptr_t addr = tb->jmp_target_addr[n];
1756 ptrdiff_t offset = addr - (jmp_rx + 8);
1759 /* Either directly branch, or fall through to indirect branch. */
1760 if (offset == sextract64(offset, 0, 26)) {
1762 insn = deposit32((COND_AL << 28) | INSN_B, 0, 24, offset >> 2);
1767 qatomic_set((uint32_t *)jmp_rw, insn);
1768 flush_idcache_range(jmp_rx, jmp_rw, 4);
1771 static void tcg_out_op(TCGContext *s, TCGOpcode opc,
1772 const TCGArg args[TCG_MAX_OP_ARGS],
1773 const int const_args[TCG_MAX_OP_ARGS])
1775 TCGArg a0, a1, a2, a3, a4, a5;
1779 case INDEX_op_goto_ptr:
1780 tcg_out_b_reg(s, COND_AL, args[0]);
1783 tcg_out_goto_label(s, COND_AL, arg_label(args[0]));
1786 case INDEX_op_ld8u_i32:
1787 tcg_out_ld8u(s, COND_AL, args[0], args[1], args[2]);
1789 case INDEX_op_ld8s_i32:
1790 tcg_out_ld8s(s, COND_AL, args[0], args[1], args[2]);
1792 case INDEX_op_ld16u_i32:
1793 tcg_out_ld16u(s, COND_AL, args[0], args[1], args[2]);
1795 case INDEX_op_ld16s_i32:
1796 tcg_out_ld16s(s, COND_AL, args[0], args[1], args[2]);
1798 case INDEX_op_ld_i32:
1799 tcg_out_ld32u(s, COND_AL, args[0], args[1], args[2]);
1801 case INDEX_op_st8_i32:
1802 tcg_out_st8(s, COND_AL, args[0], args[1], args[2]);
1804 case INDEX_op_st16_i32:
1805 tcg_out_st16(s, COND_AL, args[0], args[1], args[2]);
1807 case INDEX_op_st_i32:
1808 tcg_out_st32(s, COND_AL, args[0], args[1], args[2]);
1811 case INDEX_op_movcond_i32:
1812 /* Constraints mean that v2 is always in the same register as dest,
1813 * so we only need to do "if condition passed, move v1 to dest".
1815 tcg_out_dat_rIN(s, COND_AL, ARITH_CMP, ARITH_CMN, 0,
1816 args[1], args[2], const_args[2]);
1817 tcg_out_dat_rIK(s, tcg_cond_to_arm_cond[args[5]], ARITH_MOV,
1818 ARITH_MVN, args[0], 0, args[3], const_args[3]);
1820 case INDEX_op_add_i32:
1821 tcg_out_dat_rIN(s, COND_AL, ARITH_ADD, ARITH_SUB,
1822 args[0], args[1], args[2], const_args[2]);
1824 case INDEX_op_sub_i32:
1825 if (const_args[1]) {
1826 if (const_args[2]) {
1827 tcg_out_movi32(s, COND_AL, args[0], args[1] - args[2]);
1829 tcg_out_dat_rI(s, COND_AL, ARITH_RSB,
1830 args[0], args[2], args[1], 1);
1833 tcg_out_dat_rIN(s, COND_AL, ARITH_SUB, ARITH_ADD,
1834 args[0], args[1], args[2], const_args[2]);
1837 case INDEX_op_and_i32:
1838 tcg_out_dat_rIK(s, COND_AL, ARITH_AND, ARITH_BIC,
1839 args[0], args[1], args[2], const_args[2]);
1841 case INDEX_op_andc_i32:
1842 tcg_out_dat_rIK(s, COND_AL, ARITH_BIC, ARITH_AND,
1843 args[0], args[1], args[2], const_args[2]);
1845 case INDEX_op_or_i32:
1848 case INDEX_op_xor_i32:
1852 tcg_out_dat_rI(s, COND_AL, c, args[0], args[1], args[2], const_args[2]);
1854 case INDEX_op_add2_i32:
1855 a0 = args[0], a1 = args[1], a2 = args[2];
1856 a3 = args[3], a4 = args[4], a5 = args[5];
1857 if (a0 == a3 || (a0 == a5 && !const_args[5])) {
1860 tcg_out_dat_rIN(s, COND_AL, ARITH_ADD | TO_CPSR, ARITH_SUB | TO_CPSR,
1861 a0, a2, a4, const_args[4]);
1862 tcg_out_dat_rIK(s, COND_AL, ARITH_ADC, ARITH_SBC,
1863 a1, a3, a5, const_args[5]);
1864 tcg_out_mov_reg(s, COND_AL, args[0], a0);
1866 case INDEX_op_sub2_i32:
1867 a0 = args[0], a1 = args[1], a2 = args[2];
1868 a3 = args[3], a4 = args[4], a5 = args[5];
1869 if ((a0 == a3 && !const_args[3]) || (a0 == a5 && !const_args[5])) {
1872 if (const_args[2]) {
1873 if (const_args[4]) {
1874 tcg_out_movi32(s, COND_AL, a0, a4);
1877 tcg_out_dat_rI(s, COND_AL, ARITH_RSB | TO_CPSR, a0, a4, a2, 1);
1879 tcg_out_dat_rIN(s, COND_AL, ARITH_SUB | TO_CPSR,
1880 ARITH_ADD | TO_CPSR, a0, a2, a4, const_args[4]);
1882 if (const_args[3]) {
1883 if (const_args[5]) {
1884 tcg_out_movi32(s, COND_AL, a1, a5);
1887 tcg_out_dat_rI(s, COND_AL, ARITH_RSC, a1, a5, a3, 1);
1889 tcg_out_dat_rIK(s, COND_AL, ARITH_SBC, ARITH_ADC,
1890 a1, a3, a5, const_args[5]);
1892 tcg_out_mov_reg(s, COND_AL, args[0], a0);
1894 case INDEX_op_neg_i32:
1895 tcg_out_dat_imm(s, COND_AL, ARITH_RSB, args[0], args[1], 0);
1897 case INDEX_op_not_i32:
1898 tcg_out_dat_reg(s, COND_AL,
1899 ARITH_MVN, args[0], 0, args[1], SHIFT_IMM_LSL(0));
1901 case INDEX_op_mul_i32:
1902 tcg_out_mul32(s, COND_AL, args[0], args[1], args[2]);
1904 case INDEX_op_mulu2_i32:
1905 tcg_out_umull32(s, COND_AL, args[0], args[1], args[2], args[3]);
1907 case INDEX_op_muls2_i32:
1908 tcg_out_smull32(s, COND_AL, args[0], args[1], args[2], args[3]);
1910 /* XXX: Perhaps args[2] & 0x1f is wrong */
1911 case INDEX_op_shl_i32:
1913 SHIFT_IMM_LSL(args[2] & 0x1f) : SHIFT_REG_LSL(args[2]);
1915 case INDEX_op_shr_i32:
1916 c = const_args[2] ? (args[2] & 0x1f) ? SHIFT_IMM_LSR(args[2] & 0x1f) :
1917 SHIFT_IMM_LSL(0) : SHIFT_REG_LSR(args[2]);
1919 case INDEX_op_sar_i32:
1920 c = const_args[2] ? (args[2] & 0x1f) ? SHIFT_IMM_ASR(args[2] & 0x1f) :
1921 SHIFT_IMM_LSL(0) : SHIFT_REG_ASR(args[2]);
1923 case INDEX_op_rotr_i32:
1924 c = const_args[2] ? (args[2] & 0x1f) ? SHIFT_IMM_ROR(args[2] & 0x1f) :
1925 SHIFT_IMM_LSL(0) : SHIFT_REG_ROR(args[2]);
1928 tcg_out_dat_reg(s, COND_AL, ARITH_MOV, args[0], 0, args[1], c);
1931 case INDEX_op_rotl_i32:
1932 if (const_args[2]) {
1933 tcg_out_dat_reg(s, COND_AL, ARITH_MOV, args[0], 0, args[1],
1934 ((0x20 - args[2]) & 0x1f) ?
1935 SHIFT_IMM_ROR((0x20 - args[2]) & 0x1f) :
1938 tcg_out_dat_imm(s, COND_AL, ARITH_RSB, TCG_REG_TMP, args[2], 0x20);
1939 tcg_out_dat_reg(s, COND_AL, ARITH_MOV, args[0], 0, args[1],
1940 SHIFT_REG_ROR(TCG_REG_TMP));
1944 case INDEX_op_ctz_i32:
1945 tcg_out_dat_reg(s, COND_AL, INSN_RBIT, TCG_REG_TMP, 0, args[1], 0);
1949 case INDEX_op_clz_i32:
1955 if (c && a2 == 32) {
1956 tcg_out_dat_reg(s, COND_AL, INSN_CLZ, a0, 0, a1, 0);
1959 tcg_out_dat_imm(s, COND_AL, ARITH_CMP, 0, a1, 0);
1960 tcg_out_dat_reg(s, COND_NE, INSN_CLZ, a0, 0, a1, 0);
1961 if (c || a0 != a2) {
1962 tcg_out_dat_rIK(s, COND_EQ, ARITH_MOV, ARITH_MVN, a0, 0, a2, c);
1966 case INDEX_op_brcond_i32:
1967 tcg_out_dat_rIN(s, COND_AL, ARITH_CMP, ARITH_CMN, 0,
1968 args[0], args[1], const_args[1]);
1969 tcg_out_goto_label(s, tcg_cond_to_arm_cond[args[2]],
1970 arg_label(args[3]));
1972 case INDEX_op_setcond_i32:
1973 tcg_out_dat_rIN(s, COND_AL, ARITH_CMP, ARITH_CMN, 0,
1974 args[1], args[2], const_args[2]);
1975 tcg_out_dat_imm(s, tcg_cond_to_arm_cond[args[3]],
1976 ARITH_MOV, args[0], 0, 1);
1977 tcg_out_dat_imm(s, tcg_cond_to_arm_cond[tcg_invert_cond(args[3])],
1978 ARITH_MOV, args[0], 0, 0);
1980 case INDEX_op_negsetcond_i32:
1981 tcg_out_dat_rIN(s, COND_AL, ARITH_CMP, ARITH_CMN, 0,
1982 args[1], args[2], const_args[2]);
1983 tcg_out_dat_imm(s, tcg_cond_to_arm_cond[args[3]],
1984 ARITH_MVN, args[0], 0, 0);
1985 tcg_out_dat_imm(s, tcg_cond_to_arm_cond[tcg_invert_cond(args[3])],
1986 ARITH_MOV, args[0], 0, 0);
1989 case INDEX_op_brcond2_i32:
1990 c = tcg_out_cmp2(s, args, const_args);
1991 tcg_out_goto_label(s, tcg_cond_to_arm_cond[c], arg_label(args[5]));
1993 case INDEX_op_setcond2_i32:
1994 c = tcg_out_cmp2(s, args + 1, const_args + 1);
1995 tcg_out_dat_imm(s, tcg_cond_to_arm_cond[c], ARITH_MOV, args[0], 0, 1);
1996 tcg_out_dat_imm(s, tcg_cond_to_arm_cond[tcg_invert_cond(c)],
1997 ARITH_MOV, args[0], 0, 0);
2000 case INDEX_op_qemu_ld_a32_i32:
2001 tcg_out_qemu_ld(s, args[0], -1, args[1], -1, args[2], TCG_TYPE_I32);
2003 case INDEX_op_qemu_ld_a64_i32:
2004 tcg_out_qemu_ld(s, args[0], -1, args[1], args[2],
2005 args[3], TCG_TYPE_I32);
2007 case INDEX_op_qemu_ld_a32_i64:
2008 tcg_out_qemu_ld(s, args[0], args[1], args[2], -1,
2009 args[3], TCG_TYPE_I64);
2011 case INDEX_op_qemu_ld_a64_i64:
2012 tcg_out_qemu_ld(s, args[0], args[1], args[2], args[3],
2013 args[4], TCG_TYPE_I64);
2016 case INDEX_op_qemu_st_a32_i32:
2017 tcg_out_qemu_st(s, args[0], -1, args[1], -1, args[2], TCG_TYPE_I32);
2019 case INDEX_op_qemu_st_a64_i32:
2020 tcg_out_qemu_st(s, args[0], -1, args[1], args[2],
2021 args[3], TCG_TYPE_I32);
2023 case INDEX_op_qemu_st_a32_i64:
2024 tcg_out_qemu_st(s, args[0], args[1], args[2], -1,
2025 args[3], TCG_TYPE_I64);
2027 case INDEX_op_qemu_st_a64_i64:
2028 tcg_out_qemu_st(s, args[0], args[1], args[2], args[3],
2029 args[4], TCG_TYPE_I64);
2032 case INDEX_op_bswap16_i32:
2033 tcg_out_bswap16(s, COND_AL, args[0], args[1], args[2]);
2035 case INDEX_op_bswap32_i32:
2036 tcg_out_bswap32(s, COND_AL, args[0], args[1]);
2039 case INDEX_op_deposit_i32:
2040 tcg_out_deposit(s, COND_AL, args[0], args[2],
2041 args[3], args[4], const_args[2]);
2043 case INDEX_op_extract_i32:
2044 tcg_out_extract(s, COND_AL, args[0], args[1], args[2], args[3]);
2046 case INDEX_op_sextract_i32:
2047 tcg_out_sextract(s, COND_AL, args[0], args[1], args[2], args[3]);
2049 case INDEX_op_extract2_i32:
2050 /* ??? These optimization vs zero should be generic. */
2051 /* ??? But we can't substitute 2 for 1 in the opcode stream yet. */
2052 if (const_args[1]) {
2053 if (const_args[2]) {
2054 tcg_out_movi(s, TCG_TYPE_REG, args[0], 0);
2056 tcg_out_dat_reg(s, COND_AL, ARITH_MOV, args[0], 0,
2057 args[2], SHIFT_IMM_LSL(32 - args[3]));
2059 } else if (const_args[2]) {
2060 tcg_out_dat_reg(s, COND_AL, ARITH_MOV, args[0], 0,
2061 args[1], SHIFT_IMM_LSR(args[3]));
2063 /* We can do extract2 in 2 insns, vs the 3 required otherwise. */
2064 tcg_out_dat_reg(s, COND_AL, ARITH_MOV, TCG_REG_TMP, 0,
2065 args[2], SHIFT_IMM_LSL(32 - args[3]));
2066 tcg_out_dat_reg(s, COND_AL, ARITH_ORR, args[0], TCG_REG_TMP,
2067 args[1], SHIFT_IMM_LSR(args[3]));
2071 case INDEX_op_div_i32:
2072 tcg_out_sdiv(s, COND_AL, args[0], args[1], args[2]);
2074 case INDEX_op_divu_i32:
2075 tcg_out_udiv(s, COND_AL, args[0], args[1], args[2]);
2079 tcg_out_mb(s, args[0]);
2082 case INDEX_op_mov_i32: /* Always emitted via tcg_out_mov. */
2083 case INDEX_op_call: /* Always emitted via tcg_out_call. */
2084 case INDEX_op_exit_tb: /* Always emitted via tcg_out_exit_tb. */
2085 case INDEX_op_goto_tb: /* Always emitted via tcg_out_goto_tb. */
2086 case INDEX_op_ext8s_i32: /* Always emitted via tcg_reg_alloc_op. */
2087 case INDEX_op_ext8u_i32:
2088 case INDEX_op_ext16s_i32:
2089 case INDEX_op_ext16u_i32:
2091 g_assert_not_reached();
2095 static TCGConstraintSetIndex tcg_target_op_def(TCGOpcode op)
2098 case INDEX_op_goto_ptr:
2101 case INDEX_op_ld8u_i32:
2102 case INDEX_op_ld8s_i32:
2103 case INDEX_op_ld16u_i32:
2104 case INDEX_op_ld16s_i32:
2105 case INDEX_op_ld_i32:
2106 case INDEX_op_neg_i32:
2107 case INDEX_op_not_i32:
2108 case INDEX_op_bswap16_i32:
2109 case INDEX_op_bswap32_i32:
2110 case INDEX_op_ext8s_i32:
2111 case INDEX_op_ext16s_i32:
2112 case INDEX_op_ext16u_i32:
2113 case INDEX_op_extract_i32:
2114 case INDEX_op_sextract_i32:
2115 return C_O1_I1(r, r);
2117 case INDEX_op_st8_i32:
2118 case INDEX_op_st16_i32:
2119 case INDEX_op_st_i32:
2120 return C_O0_I2(r, r);
2122 case INDEX_op_add_i32:
2123 case INDEX_op_sub_i32:
2124 case INDEX_op_setcond_i32:
2125 case INDEX_op_negsetcond_i32:
2126 return C_O1_I2(r, r, rIN);
2128 case INDEX_op_and_i32:
2129 case INDEX_op_andc_i32:
2130 case INDEX_op_clz_i32:
2131 case INDEX_op_ctz_i32:
2132 return C_O1_I2(r, r, rIK);
2134 case INDEX_op_mul_i32:
2135 case INDEX_op_div_i32:
2136 case INDEX_op_divu_i32:
2137 return C_O1_I2(r, r, r);
2139 case INDEX_op_mulu2_i32:
2140 case INDEX_op_muls2_i32:
2141 return C_O2_I2(r, r, r, r);
2143 case INDEX_op_or_i32:
2144 case INDEX_op_xor_i32:
2145 return C_O1_I2(r, r, rI);
2147 case INDEX_op_shl_i32:
2148 case INDEX_op_shr_i32:
2149 case INDEX_op_sar_i32:
2150 case INDEX_op_rotl_i32:
2151 case INDEX_op_rotr_i32:
2152 return C_O1_I2(r, r, ri);
2154 case INDEX_op_brcond_i32:
2155 return C_O0_I2(r, rIN);
2156 case INDEX_op_deposit_i32:
2157 return C_O1_I2(r, 0, rZ);
2158 case INDEX_op_extract2_i32:
2159 return C_O1_I2(r, rZ, rZ);
2160 case INDEX_op_movcond_i32:
2161 return C_O1_I4(r, r, rIN, rIK, 0);
2162 case INDEX_op_add2_i32:
2163 return C_O2_I4(r, r, r, r, rIN, rIK);
2164 case INDEX_op_sub2_i32:
2165 return C_O2_I4(r, r, rI, rI, rIN, rIK);
2166 case INDEX_op_brcond2_i32:
2167 return C_O0_I4(r, r, rI, rI);
2168 case INDEX_op_setcond2_i32:
2169 return C_O1_I4(r, r, r, rI, rI);
2171 case INDEX_op_qemu_ld_a32_i32:
2172 return C_O1_I1(r, q);
2173 case INDEX_op_qemu_ld_a64_i32:
2174 return C_O1_I2(r, q, q);
2175 case INDEX_op_qemu_ld_a32_i64:
2176 return C_O2_I1(e, p, q);
2177 case INDEX_op_qemu_ld_a64_i64:
2178 return C_O2_I2(e, p, q, q);
2179 case INDEX_op_qemu_st_a32_i32:
2180 return C_O0_I2(q, q);
2181 case INDEX_op_qemu_st_a64_i32:
2182 return C_O0_I3(q, q, q);
2183 case INDEX_op_qemu_st_a32_i64:
2184 return C_O0_I3(Q, p, q);
2185 case INDEX_op_qemu_st_a64_i64:
2186 return C_O0_I4(Q, p, q, q);
2188 case INDEX_op_st_vec:
2189 return C_O0_I2(w, r);
2190 case INDEX_op_ld_vec:
2191 case INDEX_op_dupm_vec:
2192 return C_O1_I1(w, r);
2193 case INDEX_op_dup_vec:
2194 return C_O1_I1(w, wr);
2195 case INDEX_op_abs_vec:
2196 case INDEX_op_neg_vec:
2197 case INDEX_op_not_vec:
2198 case INDEX_op_shli_vec:
2199 case INDEX_op_shri_vec:
2200 case INDEX_op_sari_vec:
2201 return C_O1_I1(w, w);
2202 case INDEX_op_dup2_vec:
2203 case INDEX_op_add_vec:
2204 case INDEX_op_mul_vec:
2205 case INDEX_op_smax_vec:
2206 case INDEX_op_smin_vec:
2207 case INDEX_op_ssadd_vec:
2208 case INDEX_op_sssub_vec:
2209 case INDEX_op_sub_vec:
2210 case INDEX_op_umax_vec:
2211 case INDEX_op_umin_vec:
2212 case INDEX_op_usadd_vec:
2213 case INDEX_op_ussub_vec:
2214 case INDEX_op_xor_vec:
2215 case INDEX_op_arm_sshl_vec:
2216 case INDEX_op_arm_ushl_vec:
2217 return C_O1_I2(w, w, w);
2218 case INDEX_op_arm_sli_vec:
2219 return C_O1_I2(w, 0, w);
2220 case INDEX_op_or_vec:
2221 case INDEX_op_andc_vec:
2222 return C_O1_I2(w, w, wO);
2223 case INDEX_op_and_vec:
2224 case INDEX_op_orc_vec:
2225 return C_O1_I2(w, w, wV);
2226 case INDEX_op_cmp_vec:
2227 return C_O1_I2(w, w, wZ);
2228 case INDEX_op_bitsel_vec:
2229 return C_O1_I3(w, w, w, w);
2231 g_assert_not_reached();
2235 static void tcg_target_init(TCGContext *s)
2238 * Only probe for the platform and capabilities if we haven't already
2239 * determined maximum values at compile time.
2241 #if !defined(use_idiv_instructions) || !defined(use_neon_instructions)
2243 unsigned long hwcap = qemu_getauxval(AT_HWCAP);
2244 #ifndef use_idiv_instructions
2245 use_idiv_instructions = (hwcap & HWCAP_ARM_IDIVA) != 0;
2247 #ifndef use_neon_instructions
2248 use_neon_instructions = (hwcap & HWCAP_ARM_NEON) != 0;
2253 if (__ARM_ARCH < 7) {
2254 const char *pl = (const char *)qemu_getauxval(AT_PLATFORM);
2255 if (pl != NULL && pl[0] == 'v' && pl[1] >= '4' && pl[1] <= '9') {
2256 arm_arch = pl[1] - '0';
2260 error_report("TCG: ARMv%d is unsupported; exiting", arm_arch);
2265 tcg_target_available_regs[TCG_TYPE_I32] = ALL_GENERAL_REGS;
2267 tcg_target_call_clobber_regs = 0;
2268 tcg_regset_set_reg(tcg_target_call_clobber_regs, TCG_REG_R0);
2269 tcg_regset_set_reg(tcg_target_call_clobber_regs, TCG_REG_R1);
2270 tcg_regset_set_reg(tcg_target_call_clobber_regs, TCG_REG_R2);
2271 tcg_regset_set_reg(tcg_target_call_clobber_regs, TCG_REG_R3);
2272 tcg_regset_set_reg(tcg_target_call_clobber_regs, TCG_REG_R12);
2273 tcg_regset_set_reg(tcg_target_call_clobber_regs, TCG_REG_R14);
2275 if (use_neon_instructions) {
2276 tcg_target_available_regs[TCG_TYPE_V64] = ALL_VECTOR_REGS;
2277 tcg_target_available_regs[TCG_TYPE_V128] = ALL_VECTOR_REGS;
2279 tcg_regset_set_reg(tcg_target_call_clobber_regs, TCG_REG_Q0);
2280 tcg_regset_set_reg(tcg_target_call_clobber_regs, TCG_REG_Q1);
2281 tcg_regset_set_reg(tcg_target_call_clobber_regs, TCG_REG_Q2);
2282 tcg_regset_set_reg(tcg_target_call_clobber_regs, TCG_REG_Q3);
2283 tcg_regset_set_reg(tcg_target_call_clobber_regs, TCG_REG_Q8);
2284 tcg_regset_set_reg(tcg_target_call_clobber_regs, TCG_REG_Q9);
2285 tcg_regset_set_reg(tcg_target_call_clobber_regs, TCG_REG_Q10);
2286 tcg_regset_set_reg(tcg_target_call_clobber_regs, TCG_REG_Q11);
2287 tcg_regset_set_reg(tcg_target_call_clobber_regs, TCG_REG_Q12);
2288 tcg_regset_set_reg(tcg_target_call_clobber_regs, TCG_REG_Q13);
2289 tcg_regset_set_reg(tcg_target_call_clobber_regs, TCG_REG_Q14);
2290 tcg_regset_set_reg(tcg_target_call_clobber_regs, TCG_REG_Q15);
2293 s->reserved_regs = 0;
2294 tcg_regset_set_reg(s->reserved_regs, TCG_REG_CALL_STACK);
2295 tcg_regset_set_reg(s->reserved_regs, TCG_REG_TMP);
2296 tcg_regset_set_reg(s->reserved_regs, TCG_REG_PC);
2297 tcg_regset_set_reg(s->reserved_regs, TCG_VEC_TMP);
2300 static void tcg_out_ld(TCGContext *s, TCGType type, TCGReg arg,
2301 TCGReg arg1, intptr_t arg2)
2305 tcg_out_ld32u(s, COND_AL, arg, arg1, arg2);
2308 /* regs 1; size 8; align 8 */
2309 tcg_out_vldst(s, INSN_VLD1 | 0x7d0, arg, arg1, arg2);
2313 * We have only 8-byte alignment for the stack per the ABI.
2314 * Rather than dynamically re-align the stack, it's easier
2315 * to simply not request alignment beyond that. So:
2316 * regs 2; size 8; align 8
2318 tcg_out_vldst(s, INSN_VLD1 | 0xad0, arg, arg1, arg2);
2321 g_assert_not_reached();
2325 static void tcg_out_st(TCGContext *s, TCGType type, TCGReg arg,
2326 TCGReg arg1, intptr_t arg2)
2330 tcg_out_st32(s, COND_AL, arg, arg1, arg2);
2333 /* regs 1; size 8; align 8 */
2334 tcg_out_vldst(s, INSN_VST1 | 0x7d0, arg, arg1, arg2);
2337 /* See tcg_out_ld re alignment: regs 2; size 8; align 8 */
2338 tcg_out_vldst(s, INSN_VST1 | 0xad0, arg, arg1, arg2);
2341 g_assert_not_reached();
2345 static bool tcg_out_sti(TCGContext *s, TCGType type, TCGArg val,
2346 TCGReg base, intptr_t ofs)
2351 static bool tcg_out_mov(TCGContext *s, TCGType type, TCGReg ret, TCGReg arg)
2358 if (ret < TCG_REG_Q0 && arg < TCG_REG_Q0) {
2359 tcg_out_mov_reg(s, COND_AL, ret, arg);
2366 /* "VMOV D,N" is an alias for "VORR D,N,N". */
2367 tcg_out_vreg3(s, INSN_VORR, type - TCG_TYPE_V64, 0, ret, arg, arg);
2371 g_assert_not_reached();
2375 static void tcg_out_movi(TCGContext *s, TCGType type,
2376 TCGReg ret, tcg_target_long arg)
2378 tcg_debug_assert(type == TCG_TYPE_I32);
2379 tcg_debug_assert(ret < TCG_REG_Q0);
2380 tcg_out_movi32(s, COND_AL, ret, arg);
2383 static bool tcg_out_xchg(TCGContext *s, TCGType type, TCGReg r1, TCGReg r2)
2388 static void tcg_out_addi_ptr(TCGContext *s, TCGReg rd, TCGReg rs,
2389 tcg_target_long imm)
2391 int enc, opc = ARITH_ADD;
2393 /* All of the easiest immediates to encode are positive. */
2398 enc = encode_imm(imm);
2400 tcg_out_dat_imm(s, COND_AL, opc, rd, rs, enc);
2402 tcg_out_movi32(s, COND_AL, TCG_REG_TMP, imm);
2403 tcg_out_dat_reg(s, COND_AL, opc, rd, rs,
2404 TCG_REG_TMP, SHIFT_IMM_LSL(0));
2408 /* Type is always V128, with I64 elements. */
2409 static void tcg_out_dup2_vec(TCGContext *s, TCGReg rd, TCGReg rl, TCGReg rh)
2411 /* Move high element into place first. */
2413 tcg_out_vreg3(s, INSN_VORR | (1 << 12), 0, 0, rd, rh, rh);
2414 /* Move low element into place; tcg_out_mov will check for nop. */
2415 tcg_out_mov(s, TCG_TYPE_V64, rd, rl);
2418 static bool tcg_out_dup_vec(TCGContext *s, TCGType type, unsigned vece,
2419 TCGReg rd, TCGReg rs)
2421 int q = type - TCG_TYPE_V64;
2423 if (vece == MO_64) {
2424 if (type == TCG_TYPE_V128) {
2425 tcg_out_dup2_vec(s, rd, rs, rs);
2427 tcg_out_mov(s, TCG_TYPE_V64, rd, rs);
2429 } else if (rs < TCG_REG_Q0) {
2430 int b = (vece == MO_8);
2431 int e = (vece == MO_16);
2432 tcg_out32(s, INSN_VDUP_G | (b << 22) | (q << 21) | (e << 5) |
2433 encode_vn(rd) | (rs << 12));
2435 int imm4 = 1 << vece;
2436 tcg_out32(s, INSN_VDUP_S | (imm4 << 16) | (q << 6) |
2437 encode_vd(rd) | encode_vm(rs));
2442 static bool tcg_out_dupm_vec(TCGContext *s, TCGType type, unsigned vece,
2443 TCGReg rd, TCGReg base, intptr_t offset)
2445 if (vece == MO_64) {
2446 tcg_out_ld(s, TCG_TYPE_V64, rd, base, offset);
2447 if (type == TCG_TYPE_V128) {
2448 tcg_out_dup2_vec(s, rd, rd, rd);
2451 int q = type - TCG_TYPE_V64;
2452 tcg_out_vldst(s, INSN_VLD1R | (vece << 6) | (q << 5),
2458 static void tcg_out_dupi_vec(TCGContext *s, TCGType type, unsigned vece,
2459 TCGReg rd, int64_t v64)
2461 int q = type - TCG_TYPE_V64;
2464 /* Test all bytes equal first. */
2466 tcg_out_vmovi(s, rd, q, 0, 0xe, v64);
2471 * Test all bytes 0x00 or 0xff second. This can match cases that
2472 * might otherwise take 2 or 3 insns for MO_16 or MO_32 below.
2474 for (i = imm8 = 0; i < 8; i++) {
2475 uint8_t byte = v64 >> (i * 8);
2478 } else if (byte != 0) {
2482 tcg_out_vmovi(s, rd, q, 1, 0xe, imm8);
2487 * Tests for various replications. For each element width, if we
2488 * cannot find an expansion there's no point checking a larger
2489 * width because we already know by replication it cannot match.
2491 if (vece == MO_16) {
2494 if (is_shimm16(v16, &cmode, &imm8)) {
2495 tcg_out_vmovi(s, rd, q, 0, cmode, imm8);
2498 if (is_shimm16(~v16, &cmode, &imm8)) {
2499 tcg_out_vmovi(s, rd, q, 1, cmode, imm8);
2504 * Otherwise, all remaining constants can be loaded in two insns:
2505 * rd = v16 & 0xff, rd |= v16 & 0xff00.
2507 tcg_out_vmovi(s, rd, q, 0, 0x8, v16 & 0xff);
2508 tcg_out_vmovi(s, rd, q, 0, 0xb, v16 >> 8); /* VORRI */
2512 if (vece == MO_32) {
2515 if (is_shimm32(v32, &cmode, &imm8) ||
2516 is_soimm32(v32, &cmode, &imm8)) {
2517 tcg_out_vmovi(s, rd, q, 0, cmode, imm8);
2520 if (is_shimm32(~v32, &cmode, &imm8) ||
2521 is_soimm32(~v32, &cmode, &imm8)) {
2522 tcg_out_vmovi(s, rd, q, 1, cmode, imm8);
2527 * Restrict the set of constants to those we can load with
2528 * two instructions. Others we load from the pool.
2530 i = is_shimm32_pair(v32, &cmode, &imm8);
2532 tcg_out_vmovi(s, rd, q, 0, cmode, imm8);
2533 tcg_out_vmovi(s, rd, q, 0, i | 1, extract32(v32, i * 4, 8));
2536 i = is_shimm32_pair(~v32, &cmode, &imm8);
2538 tcg_out_vmovi(s, rd, q, 1, cmode, imm8);
2539 tcg_out_vmovi(s, rd, q, 1, i | 1, extract32(~v32, i * 4, 8));
2545 * As a last resort, load from the constant pool.
2547 if (!q || vece == MO_64) {
2548 new_pool_l2(s, R_ARM_PC11, s->code_ptr, 0, v64, v64 >> 32);
2549 /* VLDR Dd, [pc + offset] */
2550 tcg_out32(s, INSN_VLDR_D | encode_vd(rd) | (0xf << 16));
2552 tcg_out_dup2_vec(s, rd, rd, rd);
2555 new_pool_label(s, (uint32_t)v64, R_ARM_PC8, s->code_ptr, 0);
2556 /* add tmp, pc, offset */
2557 tcg_out_dat_imm(s, COND_AL, ARITH_ADD, TCG_REG_TMP, TCG_REG_PC, 0);
2558 tcg_out_dupm_vec(s, type, MO_32, rd, TCG_REG_TMP, 0);
2562 static const ARMInsn vec_cmp_insn[16] = {
2563 [TCG_COND_EQ] = INSN_VCEQ,
2564 [TCG_COND_GT] = INSN_VCGT,
2565 [TCG_COND_GE] = INSN_VCGE,
2566 [TCG_COND_GTU] = INSN_VCGT_U,
2567 [TCG_COND_GEU] = INSN_VCGE_U,
2570 static const ARMInsn vec_cmp0_insn[16] = {
2571 [TCG_COND_EQ] = INSN_VCEQ0,
2572 [TCG_COND_GT] = INSN_VCGT0,
2573 [TCG_COND_GE] = INSN_VCGE0,
2574 [TCG_COND_LT] = INSN_VCLT0,
2575 [TCG_COND_LE] = INSN_VCLE0,
2578 static void tcg_out_vec_op(TCGContext *s, TCGOpcode opc,
2579 unsigned vecl, unsigned vece,
2580 const TCGArg args[TCG_MAX_OP_ARGS],
2581 const int const_args[TCG_MAX_OP_ARGS])
2583 TCGType type = vecl + TCG_TYPE_V64;
2585 TCGArg a0, a1, a2, a3;
2593 case INDEX_op_ld_vec:
2594 tcg_out_ld(s, type, a0, a1, a2);
2596 case INDEX_op_st_vec:
2597 tcg_out_st(s, type, a0, a1, a2);
2599 case INDEX_op_dupm_vec:
2600 tcg_out_dupm_vec(s, type, vece, a0, a1, a2);
2602 case INDEX_op_dup2_vec:
2603 tcg_out_dup2_vec(s, a0, a1, a2);
2605 case INDEX_op_abs_vec:
2606 tcg_out_vreg2(s, INSN_VABS, q, vece, a0, a1);
2608 case INDEX_op_neg_vec:
2609 tcg_out_vreg2(s, INSN_VNEG, q, vece, a0, a1);
2611 case INDEX_op_not_vec:
2612 tcg_out_vreg2(s, INSN_VMVN, q, 0, a0, a1);
2614 case INDEX_op_add_vec:
2615 tcg_out_vreg3(s, INSN_VADD, q, vece, a0, a1, a2);
2617 case INDEX_op_mul_vec:
2618 tcg_out_vreg3(s, INSN_VMUL, q, vece, a0, a1, a2);
2620 case INDEX_op_smax_vec:
2621 tcg_out_vreg3(s, INSN_VMAX, q, vece, a0, a1, a2);
2623 case INDEX_op_smin_vec:
2624 tcg_out_vreg3(s, INSN_VMIN, q, vece, a0, a1, a2);
2626 case INDEX_op_sub_vec:
2627 tcg_out_vreg3(s, INSN_VSUB, q, vece, a0, a1, a2);
2629 case INDEX_op_ssadd_vec:
2630 tcg_out_vreg3(s, INSN_VQADD, q, vece, a0, a1, a2);
2632 case INDEX_op_sssub_vec:
2633 tcg_out_vreg3(s, INSN_VQSUB, q, vece, a0, a1, a2);
2635 case INDEX_op_umax_vec:
2636 tcg_out_vreg3(s, INSN_VMAX_U, q, vece, a0, a1, a2);
2638 case INDEX_op_umin_vec:
2639 tcg_out_vreg3(s, INSN_VMIN_U, q, vece, a0, a1, a2);
2641 case INDEX_op_usadd_vec:
2642 tcg_out_vreg3(s, INSN_VQADD_U, q, vece, a0, a1, a2);
2644 case INDEX_op_ussub_vec:
2645 tcg_out_vreg3(s, INSN_VQSUB_U, q, vece, a0, a1, a2);
2647 case INDEX_op_xor_vec:
2648 tcg_out_vreg3(s, INSN_VEOR, q, 0, a0, a1, a2);
2650 case INDEX_op_arm_sshl_vec:
2652 * Note that Vm is the data and Vn is the shift count,
2653 * therefore the arguments appear reversed.
2655 tcg_out_vreg3(s, INSN_VSHL_S, q, vece, a0, a2, a1);
2657 case INDEX_op_arm_ushl_vec:
2659 tcg_out_vreg3(s, INSN_VSHL_U, q, vece, a0, a2, a1);
2661 case INDEX_op_shli_vec:
2662 tcg_out_vshifti(s, INSN_VSHLI, q, a0, a1, a2 + (8 << vece));
2664 case INDEX_op_shri_vec:
2665 tcg_out_vshifti(s, INSN_VSHRI, q, a0, a1, (16 << vece) - a2);
2667 case INDEX_op_sari_vec:
2668 tcg_out_vshifti(s, INSN_VSARI, q, a0, a1, (16 << vece) - a2);
2670 case INDEX_op_arm_sli_vec:
2671 tcg_out_vshifti(s, INSN_VSLI, q, a0, a2, args[3] + (8 << vece));
2674 case INDEX_op_andc_vec:
2675 if (!const_args[2]) {
2676 tcg_out_vreg3(s, INSN_VBIC, q, 0, a0, a1, a2);
2681 case INDEX_op_and_vec:
2682 if (const_args[2]) {
2683 is_shimm1632(~a2, &cmode, &imm8);
2685 tcg_out_vmovi(s, a0, q, 1, cmode | 1, imm8); /* VBICI */
2688 tcg_out_vmovi(s, a0, q, 1, cmode, imm8); /* VMVNI */
2691 tcg_out_vreg3(s, INSN_VAND, q, 0, a0, a1, a2);
2694 case INDEX_op_orc_vec:
2695 if (!const_args[2]) {
2696 tcg_out_vreg3(s, INSN_VORN, q, 0, a0, a1, a2);
2701 case INDEX_op_or_vec:
2702 if (const_args[2]) {
2703 is_shimm1632(a2, &cmode, &imm8);
2705 tcg_out_vmovi(s, a0, q, 0, cmode | 1, imm8); /* VORRI */
2708 tcg_out_vmovi(s, a0, q, 0, cmode, imm8); /* VMOVI */
2711 tcg_out_vreg3(s, INSN_VORR, q, 0, a0, a1, a2);
2714 case INDEX_op_cmp_vec:
2716 TCGCond cond = args[3];
2718 if (cond == TCG_COND_NE) {
2719 if (const_args[2]) {
2720 tcg_out_vreg3(s, INSN_VTST, q, vece, a0, a1, a1);
2722 tcg_out_vreg3(s, INSN_VCEQ, q, vece, a0, a1, a2);
2723 tcg_out_vreg2(s, INSN_VMVN, q, 0, a0, a0);
2728 if (const_args[2]) {
2729 insn = vec_cmp0_insn[cond];
2731 tcg_out_vreg2(s, insn, q, vece, a0, a1);
2734 tcg_out_dupi_vec(s, type, MO_8, TCG_VEC_TMP, 0);
2737 insn = vec_cmp_insn[cond];
2740 t = a1, a1 = a2, a2 = t;
2741 cond = tcg_swap_cond(cond);
2742 insn = vec_cmp_insn[cond];
2743 tcg_debug_assert(insn != 0);
2745 tcg_out_vreg3(s, insn, q, vece, a0, a1, a2);
2750 case INDEX_op_bitsel_vec:
2753 tcg_out_vreg3(s, INSN_VBIT, q, 0, a0, a2, a1);
2754 } else if (a0 == a2) {
2755 tcg_out_vreg3(s, INSN_VBIF, q, 0, a0, a3, a1);
2757 tcg_out_mov(s, type, a0, a1);
2758 tcg_out_vreg3(s, INSN_VBSL, q, 0, a0, a2, a3);
2762 case INDEX_op_mov_vec: /* Always emitted via tcg_out_mov. */
2763 case INDEX_op_dup_vec: /* Always emitted via tcg_out_dup_vec. */
2765 g_assert_not_reached();
2769 int tcg_can_emit_vec_op(TCGOpcode opc, TCGType type, unsigned vece)
2772 case INDEX_op_add_vec:
2773 case INDEX_op_sub_vec:
2774 case INDEX_op_and_vec:
2775 case INDEX_op_andc_vec:
2776 case INDEX_op_or_vec:
2777 case INDEX_op_orc_vec:
2778 case INDEX_op_xor_vec:
2779 case INDEX_op_not_vec:
2780 case INDEX_op_shli_vec:
2781 case INDEX_op_shri_vec:
2782 case INDEX_op_sari_vec:
2783 case INDEX_op_ssadd_vec:
2784 case INDEX_op_sssub_vec:
2785 case INDEX_op_usadd_vec:
2786 case INDEX_op_ussub_vec:
2787 case INDEX_op_bitsel_vec:
2789 case INDEX_op_abs_vec:
2790 case INDEX_op_cmp_vec:
2791 case INDEX_op_mul_vec:
2792 case INDEX_op_neg_vec:
2793 case INDEX_op_smax_vec:
2794 case INDEX_op_smin_vec:
2795 case INDEX_op_umax_vec:
2796 case INDEX_op_umin_vec:
2797 return vece < MO_64;
2798 case INDEX_op_shlv_vec:
2799 case INDEX_op_shrv_vec:
2800 case INDEX_op_sarv_vec:
2801 case INDEX_op_rotli_vec:
2802 case INDEX_op_rotlv_vec:
2803 case INDEX_op_rotrv_vec:
2810 void tcg_expand_vec_op(TCGOpcode opc, TCGType type, unsigned vece,
2814 TCGv_vec v0, v1, v2, t1, t2, c1;
2818 v0 = temp_tcgv_vec(arg_temp(a0));
2819 v1 = temp_tcgv_vec(arg_temp(va_arg(va, TCGArg)));
2820 a2 = va_arg(va, TCGArg);
2824 case INDEX_op_shlv_vec:
2826 * Merely propagate shlv_vec to arm_ushl_vec.
2827 * In this way we don't set TCG_TARGET_HAS_shv_vec
2828 * because everything is done via expansion.
2830 v2 = temp_tcgv_vec(arg_temp(a2));
2831 vec_gen_3(INDEX_op_arm_ushl_vec, type, vece, tcgv_vec_arg(v0),
2832 tcgv_vec_arg(v1), tcgv_vec_arg(v2));
2835 case INDEX_op_shrv_vec:
2836 case INDEX_op_sarv_vec:
2837 /* Right shifts are negative left shifts for NEON. */
2838 v2 = temp_tcgv_vec(arg_temp(a2));
2839 t1 = tcg_temp_new_vec(type);
2840 tcg_gen_neg_vec(vece, t1, v2);
2841 if (opc == INDEX_op_shrv_vec) {
2842 opc = INDEX_op_arm_ushl_vec;
2844 opc = INDEX_op_arm_sshl_vec;
2846 vec_gen_3(opc, type, vece, tcgv_vec_arg(v0),
2847 tcgv_vec_arg(v1), tcgv_vec_arg(t1));
2848 tcg_temp_free_vec(t1);
2851 case INDEX_op_rotli_vec:
2852 t1 = tcg_temp_new_vec(type);
2853 tcg_gen_shri_vec(vece, t1, v1, -a2 & ((8 << vece) - 1));
2854 vec_gen_4(INDEX_op_arm_sli_vec, type, vece,
2855 tcgv_vec_arg(v0), tcgv_vec_arg(t1), tcgv_vec_arg(v1), a2);
2856 tcg_temp_free_vec(t1);
2859 case INDEX_op_rotlv_vec:
2860 v2 = temp_tcgv_vec(arg_temp(a2));
2861 t1 = tcg_temp_new_vec(type);
2862 c1 = tcg_constant_vec(type, vece, 8 << vece);
2863 tcg_gen_sub_vec(vece, t1, v2, c1);
2864 /* Right shifts are negative left shifts for NEON. */
2865 vec_gen_3(INDEX_op_arm_ushl_vec, type, vece, tcgv_vec_arg(t1),
2866 tcgv_vec_arg(v1), tcgv_vec_arg(t1));
2867 vec_gen_3(INDEX_op_arm_ushl_vec, type, vece, tcgv_vec_arg(v0),
2868 tcgv_vec_arg(v1), tcgv_vec_arg(v2));
2869 tcg_gen_or_vec(vece, v0, v0, t1);
2870 tcg_temp_free_vec(t1);
2873 case INDEX_op_rotrv_vec:
2874 v2 = temp_tcgv_vec(arg_temp(a2));
2875 t1 = tcg_temp_new_vec(type);
2876 t2 = tcg_temp_new_vec(type);
2877 c1 = tcg_constant_vec(type, vece, 8 << vece);
2878 tcg_gen_neg_vec(vece, t1, v2);
2879 tcg_gen_sub_vec(vece, t2, c1, v2);
2880 /* Right shifts are negative left shifts for NEON. */
2881 vec_gen_3(INDEX_op_arm_ushl_vec, type, vece, tcgv_vec_arg(t1),
2882 tcgv_vec_arg(v1), tcgv_vec_arg(t1));
2883 vec_gen_3(INDEX_op_arm_ushl_vec, type, vece, tcgv_vec_arg(t2),
2884 tcgv_vec_arg(v1), tcgv_vec_arg(t2));
2885 tcg_gen_or_vec(vece, v0, t1, t2);
2886 tcg_temp_free_vec(t1);
2887 tcg_temp_free_vec(t2);
2891 g_assert_not_reached();
2895 static void tcg_out_nop_fill(tcg_insn_unit *p, int count)
2898 for (i = 0; i < count; ++i) {
2903 /* Compute frame size via macros, to share between tcg_target_qemu_prologue
2904 and tcg_register_jit. */
2906 #define PUSH_SIZE ((11 - 4 + 1 + 1) * sizeof(tcg_target_long))
2908 #define FRAME_SIZE \
2910 + TCG_STATIC_CALL_ARGS_SIZE \
2911 + CPU_TEMP_BUF_NLONGS * sizeof(long) \
2912 + TCG_TARGET_STACK_ALIGN - 1) \
2913 & -TCG_TARGET_STACK_ALIGN)
2915 #define STACK_ADDEND (FRAME_SIZE - PUSH_SIZE)
2917 static void tcg_target_qemu_prologue(TCGContext *s)
2919 /* Calling convention requires us to save r4-r11 and lr. */
2920 /* stmdb sp!, { r4 - r11, lr } */
2921 tcg_out_ldstm(s, COND_AL, INSN_STMDB, TCG_REG_CALL_STACK,
2922 (1 << TCG_REG_R4) | (1 << TCG_REG_R5) | (1 << TCG_REG_R6) |
2923 (1 << TCG_REG_R7) | (1 << TCG_REG_R8) | (1 << TCG_REG_R9) |
2924 (1 << TCG_REG_R10) | (1 << TCG_REG_R11) | (1 << TCG_REG_R14));
2926 /* Reserve callee argument and tcg temp space. */
2927 tcg_out_dat_rI(s, COND_AL, ARITH_SUB, TCG_REG_CALL_STACK,
2928 TCG_REG_CALL_STACK, STACK_ADDEND, 1);
2929 tcg_set_frame(s, TCG_REG_CALL_STACK, TCG_STATIC_CALL_ARGS_SIZE,
2930 CPU_TEMP_BUF_NLONGS * sizeof(long));
2932 tcg_out_mov(s, TCG_TYPE_PTR, TCG_AREG0, tcg_target_call_iarg_regs[0]);
2934 #ifndef CONFIG_SOFTMMU
2936 tcg_out_movi(s, TCG_TYPE_PTR, TCG_REG_GUEST_BASE, guest_base);
2937 tcg_regset_set_reg(s->reserved_regs, TCG_REG_GUEST_BASE);
2941 tcg_out_b_reg(s, COND_AL, tcg_target_call_iarg_regs[1]);
2944 * Return path for goto_ptr. Set return value to 0, a-la exit_tb,
2945 * and fall through to the rest of the epilogue.
2947 tcg_code_gen_epilogue = tcg_splitwx_to_rx(s->code_ptr);
2948 tcg_out_movi(s, TCG_TYPE_PTR, TCG_REG_R0, 0);
2949 tcg_out_epilogue(s);
2952 static void tcg_out_epilogue(TCGContext *s)
2954 /* Release local stack frame. */
2955 tcg_out_dat_rI(s, COND_AL, ARITH_ADD, TCG_REG_CALL_STACK,
2956 TCG_REG_CALL_STACK, STACK_ADDEND, 1);
2958 /* ldmia sp!, { r4 - r11, pc } */
2959 tcg_out_ldstm(s, COND_AL, INSN_LDMIA, TCG_REG_CALL_STACK,
2960 (1 << TCG_REG_R4) | (1 << TCG_REG_R5) | (1 << TCG_REG_R6) |
2961 (1 << TCG_REG_R7) | (1 << TCG_REG_R8) | (1 << TCG_REG_R9) |
2962 (1 << TCG_REG_R10) | (1 << TCG_REG_R11) | (1 << TCG_REG_PC));
2965 static void tcg_out_tb_start(TCGContext *s)
2972 uint8_t fde_def_cfa[4];
2973 uint8_t fde_reg_ofs[18];
2976 #define ELF_HOST_MACHINE EM_ARM
2978 /* We're expecting a 2 byte uleb128 encoded value. */
2979 QEMU_BUILD_BUG_ON(FRAME_SIZE >= (1 << 14));
2981 static const DebugFrame debug_frame = {
2982 .h.cie.len = sizeof(DebugFrameCIE)-4, /* length after .len member */
2985 .h.cie.code_align = 1,
2986 .h.cie.data_align = 0x7c, /* sleb128 -4 */
2987 .h.cie.return_column = 14,
2989 /* Total FDE size does not include the "len" member. */
2990 .h.fde.len = sizeof(DebugFrame) - offsetof(DebugFrame, h.fde.cie_offset),
2993 12, 13, /* DW_CFA_def_cfa sp, ... */
2994 (FRAME_SIZE & 0x7f) | 0x80, /* ... uleb128 FRAME_SIZE */
2998 /* The following must match the stmdb in the prologue. */
2999 0x8e, 1, /* DW_CFA_offset, lr, -4 */
3000 0x8b, 2, /* DW_CFA_offset, r11, -8 */
3001 0x8a, 3, /* DW_CFA_offset, r10, -12 */
3002 0x89, 4, /* DW_CFA_offset, r9, -16 */
3003 0x88, 5, /* DW_CFA_offset, r8, -20 */
3004 0x87, 6, /* DW_CFA_offset, r7, -24 */
3005 0x86, 7, /* DW_CFA_offset, r6, -28 */
3006 0x85, 8, /* DW_CFA_offset, r5, -32 */
3007 0x84, 9, /* DW_CFA_offset, r4, -36 */
3011 void tcg_register_jit(const void *buf, size_t buf_size)
3013 tcg_register_jit_int(buf, buf_size, &debug_frame, sizeof(debug_frame));