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
82 static const int tcg_target_call_oarg_regs[2] = {
83 TCG_REG_R0, TCG_REG_R1
86 #define TCG_REG_TMP TCG_REG_R12
87 #define TCG_VEC_TMP TCG_REG_Q15
88 #ifndef CONFIG_SOFTMMU
89 #define TCG_REG_GUEST_BASE TCG_REG_R11
95 COND_CS = 0x2, /* Unsigned greater or equal */
96 COND_CC = 0x3, /* Unsigned less than */
97 COND_MI = 0x4, /* Negative */
98 COND_PL = 0x5, /* Zero or greater */
99 COND_VS = 0x6, /* Overflow */
100 COND_VC = 0x7, /* No overflow */
101 COND_HI = 0x8, /* Unsigned greater than */
102 COND_LS = 0x9, /* Unsigned less or equal */
110 #define TO_CPSR (1 << 20)
112 #define SHIFT_IMM_LSL(im) (((im) << 7) | 0x00)
113 #define SHIFT_IMM_LSR(im) (((im) << 7) | 0x20)
114 #define SHIFT_IMM_ASR(im) (((im) << 7) | 0x40)
115 #define SHIFT_IMM_ROR(im) (((im) << 7) | 0x60)
116 #define SHIFT_REG_LSL(rs) (((rs) << 8) | 0x10)
117 #define SHIFT_REG_LSR(rs) (((rs) << 8) | 0x30)
118 #define SHIFT_REG_ASR(rs) (((rs) << 8) | 0x50)
119 #define SHIFT_REG_ROR(rs) (((rs) << 8) | 0x70)
122 ARITH_AND = 0x0 << 21,
123 ARITH_EOR = 0x1 << 21,
124 ARITH_SUB = 0x2 << 21,
125 ARITH_RSB = 0x3 << 21,
126 ARITH_ADD = 0x4 << 21,
127 ARITH_ADC = 0x5 << 21,
128 ARITH_SBC = 0x6 << 21,
129 ARITH_RSC = 0x7 << 21,
130 ARITH_TST = 0x8 << 21 | TO_CPSR,
131 ARITH_CMP = 0xa << 21 | TO_CPSR,
132 ARITH_CMN = 0xb << 21 | TO_CPSR,
133 ARITH_ORR = 0xc << 21,
134 ARITH_MOV = 0xd << 21,
135 ARITH_BIC = 0xe << 21,
136 ARITH_MVN = 0xf << 21,
138 INSN_CLZ = 0x016f0f10,
139 INSN_RBIT = 0x06ff0f30,
141 INSN_LDMIA = 0x08b00000,
142 INSN_STMDB = 0x09200000,
144 INSN_LDR_IMM = 0x04100000,
145 INSN_LDR_REG = 0x06100000,
146 INSN_STR_IMM = 0x04000000,
147 INSN_STR_REG = 0x06000000,
149 INSN_LDRH_IMM = 0x005000b0,
150 INSN_LDRH_REG = 0x001000b0,
151 INSN_LDRSH_IMM = 0x005000f0,
152 INSN_LDRSH_REG = 0x001000f0,
153 INSN_STRH_IMM = 0x004000b0,
154 INSN_STRH_REG = 0x000000b0,
156 INSN_LDRB_IMM = 0x04500000,
157 INSN_LDRB_REG = 0x06500000,
158 INSN_LDRSB_IMM = 0x005000d0,
159 INSN_LDRSB_REG = 0x001000d0,
160 INSN_STRB_IMM = 0x04400000,
161 INSN_STRB_REG = 0x06400000,
163 INSN_LDRD_IMM = 0x004000d0,
164 INSN_LDRD_REG = 0x000000d0,
165 INSN_STRD_IMM = 0x004000f0,
166 INSN_STRD_REG = 0x000000f0,
168 INSN_DMB_ISH = 0xf57ff05b,
169 INSN_DMB_MCR = 0xee070fba,
171 /* Architected nop introduced in v6k. */
172 /* ??? This is an MSR (imm) 0,0,0 insn. Anyone know if this
173 also Just So Happened to do nothing on pre-v6k so that we
174 don't need to conditionalize it? */
175 INSN_NOP_v6k = 0xe320f000,
176 /* Otherwise the assembler uses mov r0,r0 */
177 INSN_NOP_v4 = (COND_AL << 28) | ARITH_MOV,
179 INSN_VADD = 0xf2000800,
180 INSN_VAND = 0xf2000110,
181 INSN_VBIC = 0xf2100110,
182 INSN_VEOR = 0xf3000110,
183 INSN_VORN = 0xf2300110,
184 INSN_VORR = 0xf2200110,
185 INSN_VSUB = 0xf3000800,
186 INSN_VMUL = 0xf2000910,
187 INSN_VQADD = 0xf2000010,
188 INSN_VQADD_U = 0xf3000010,
189 INSN_VQSUB = 0xf2000210,
190 INSN_VQSUB_U = 0xf3000210,
191 INSN_VMAX = 0xf2000600,
192 INSN_VMAX_U = 0xf3000600,
193 INSN_VMIN = 0xf2000610,
194 INSN_VMIN_U = 0xf3000610,
196 INSN_VABS = 0xf3b10300,
197 INSN_VMVN = 0xf3b00580,
198 INSN_VNEG = 0xf3b10380,
200 INSN_VCEQ0 = 0xf3b10100,
201 INSN_VCGT0 = 0xf3b10000,
202 INSN_VCGE0 = 0xf3b10080,
203 INSN_VCLE0 = 0xf3b10180,
204 INSN_VCLT0 = 0xf3b10200,
206 INSN_VCEQ = 0xf3000810,
207 INSN_VCGE = 0xf2000310,
208 INSN_VCGT = 0xf2000300,
209 INSN_VCGE_U = 0xf3000310,
210 INSN_VCGT_U = 0xf3000300,
212 INSN_VSHLI = 0xf2800510, /* VSHL (immediate) */
213 INSN_VSARI = 0xf2800010, /* VSHR.S */
214 INSN_VSHRI = 0xf3800010, /* VSHR.U */
215 INSN_VSLI = 0xf3800510,
216 INSN_VSHL_S = 0xf2000400, /* VSHL.S (register) */
217 INSN_VSHL_U = 0xf3000400, /* VSHL.U (register) */
219 INSN_VBSL = 0xf3100110,
220 INSN_VBIT = 0xf3200110,
221 INSN_VBIF = 0xf3300110,
223 INSN_VTST = 0xf2000810,
225 INSN_VDUP_G = 0xee800b10, /* VDUP (ARM core register) */
226 INSN_VDUP_S = 0xf3b00c00, /* VDUP (scalar) */
227 INSN_VLDR_D = 0xed100b00, /* VLDR.64 */
228 INSN_VLD1 = 0xf4200000, /* VLD1 (multiple single elements) */
229 INSN_VLD1R = 0xf4a00c00, /* VLD1 (single element to all lanes) */
230 INSN_VST1 = 0xf4000000, /* VST1 (multiple single elements) */
231 INSN_VMOVI = 0xf2800010, /* VMOV (immediate) */
234 #define INSN_NOP (use_armv7_instructions ? INSN_NOP_v6k : INSN_NOP_v4)
236 static const uint8_t tcg_cond_to_arm_cond[] = {
237 [TCG_COND_EQ] = COND_EQ,
238 [TCG_COND_NE] = COND_NE,
239 [TCG_COND_LT] = COND_LT,
240 [TCG_COND_GE] = COND_GE,
241 [TCG_COND_LE] = COND_LE,
242 [TCG_COND_GT] = COND_GT,
244 [TCG_COND_LTU] = COND_CC,
245 [TCG_COND_GEU] = COND_CS,
246 [TCG_COND_LEU] = COND_LS,
247 [TCG_COND_GTU] = COND_HI,
250 static int encode_imm(uint32_t imm);
252 /* TCG private relocation type: add with pc+imm8 */
255 /* TCG private relocation type: vldr with imm8 << 2 */
256 #define R_ARM_PC11 12
258 static bool reloc_pc24(tcg_insn_unit *src_rw, const tcg_insn_unit *target)
260 const tcg_insn_unit *src_rx = tcg_splitwx_to_rx(src_rw);
261 ptrdiff_t offset = (tcg_ptr_byte_diff(target, src_rx) - 8) >> 2;
263 if (offset == sextract32(offset, 0, 24)) {
264 *src_rw = deposit32(*src_rw, 0, 24, offset);
270 static bool reloc_pc13(tcg_insn_unit *src_rw, const tcg_insn_unit *target)
272 const tcg_insn_unit *src_rx = tcg_splitwx_to_rx(src_rw);
273 ptrdiff_t offset = tcg_ptr_byte_diff(target, src_rx) - 8;
275 if (offset >= -0xfff && offset <= 0xfff) {
276 tcg_insn_unit insn = *src_rw;
277 bool u = (offset >= 0);
281 insn = deposit32(insn, 23, 1, u);
282 insn = deposit32(insn, 0, 12, offset);
289 static bool reloc_pc11(tcg_insn_unit *src_rw, const tcg_insn_unit *target)
291 const tcg_insn_unit *src_rx = tcg_splitwx_to_rx(src_rw);
292 ptrdiff_t offset = (tcg_ptr_byte_diff(target, src_rx) - 8) / 4;
294 if (offset >= -0xff && offset <= 0xff) {
295 tcg_insn_unit insn = *src_rw;
296 bool u = (offset >= 0);
300 insn = deposit32(insn, 23, 1, u);
301 insn = deposit32(insn, 0, 8, offset);
308 static bool reloc_pc8(tcg_insn_unit *src_rw, const tcg_insn_unit *target)
310 const tcg_insn_unit *src_rx = tcg_splitwx_to_rx(src_rw);
311 ptrdiff_t offset = tcg_ptr_byte_diff(target, src_rx) - 8;
312 int imm12 = encode_imm(offset);
315 *src_rw = deposit32(*src_rw, 0, 12, imm12);
321 static bool patch_reloc(tcg_insn_unit *code_ptr, int type,
322 intptr_t value, intptr_t addend)
324 tcg_debug_assert(addend == 0);
327 return reloc_pc24(code_ptr, (const tcg_insn_unit *)value);
329 return reloc_pc13(code_ptr, (const tcg_insn_unit *)value);
331 return reloc_pc11(code_ptr, (const tcg_insn_unit *)value);
333 return reloc_pc8(code_ptr, (const tcg_insn_unit *)value);
335 g_assert_not_reached();
339 #define TCG_CT_CONST_ARM 0x100
340 #define TCG_CT_CONST_INV 0x200
341 #define TCG_CT_CONST_NEG 0x400
342 #define TCG_CT_CONST_ZERO 0x800
343 #define TCG_CT_CONST_ORRI 0x1000
344 #define TCG_CT_CONST_ANDI 0x2000
346 #define ALL_GENERAL_REGS 0xffffu
347 #define ALL_VECTOR_REGS 0xffff0000u
350 * r0-r2 will be overwritten when reading the tlb entry (softmmu only)
351 * and r0-r1 doing the byte swapping, so don't use these.
352 * r3 is removed for softmmu to avoid clashes with helper arguments.
354 #ifdef CONFIG_SOFTMMU
355 #define ALL_QLOAD_REGS \
356 (ALL_GENERAL_REGS & ~((1 << TCG_REG_R0) | (1 << TCG_REG_R1) | \
357 (1 << TCG_REG_R2) | (1 << TCG_REG_R3) | \
359 #define ALL_QSTORE_REGS \
360 (ALL_GENERAL_REGS & ~((1 << TCG_REG_R0) | (1 << TCG_REG_R1) | \
361 (1 << TCG_REG_R2) | (1 << TCG_REG_R14) | \
362 ((TARGET_LONG_BITS == 64) << TCG_REG_R3)))
364 #define ALL_QLOAD_REGS ALL_GENERAL_REGS
365 #define ALL_QSTORE_REGS \
366 (ALL_GENERAL_REGS & ~((1 << TCG_REG_R0) | (1 << TCG_REG_R1)))
370 * ARM immediates for ALU instructions are made of an unsigned 8-bit
371 * right-rotated by an even amount between 0 and 30.
373 * Return < 0 if @imm cannot be encoded, else the entire imm12 field.
375 static int encode_imm(uint32_t imm)
379 /* Simple case, no rotation required. */
380 if ((imm & ~0xff) == 0) {
384 /* Next, try a simple even shift. */
385 rot = ctz32(imm) & ~1;
388 if ((imm8 & ~0xff) == 0) {
393 * Finally, try harder with rotations.
394 * The ctz test above will have taken care of rotates >= 8.
396 for (rot = 2; rot < 8; rot += 2) {
397 imm8 = rol32(imm, rot);
398 if ((imm8 & ~0xff) == 0) {
402 /* Fail: imm cannot be encoded. */
406 /* Note that rot is even, and we discard bit 0 by shifting by 7. */
407 return rot << 7 | imm8;
410 static int encode_imm_nofail(uint32_t imm)
412 int ret = encode_imm(imm);
413 tcg_debug_assert(ret >= 0);
417 static bool check_fit_imm(uint32_t imm)
419 return encode_imm(imm) >= 0;
422 /* Return true if v16 is a valid 16-bit shifted immediate. */
423 static bool is_shimm16(uint16_t v16, int *cmode, int *imm8)
425 if (v16 == (v16 & 0xff)) {
429 } else if (v16 == (v16 & 0xff00)) {
437 /* Return true if v32 is a valid 32-bit shifted immediate. */
438 static bool is_shimm32(uint32_t v32, int *cmode, int *imm8)
440 if (v32 == (v32 & 0xff)) {
444 } else if (v32 == (v32 & 0xff00)) {
446 *imm8 = (v32 >> 8) & 0xff;
448 } else if (v32 == (v32 & 0xff0000)) {
450 *imm8 = (v32 >> 16) & 0xff;
452 } else if (v32 == (v32 & 0xff000000)) {
460 /* Return true if v32 is a valid 32-bit shifting ones immediate. */
461 static bool is_soimm32(uint32_t v32, int *cmode, int *imm8)
463 if ((v32 & 0xffff00ff) == 0xff) {
465 *imm8 = (v32 >> 8) & 0xff;
467 } else if ((v32 & 0xff00ffff) == 0xffff) {
469 *imm8 = (v32 >> 16) & 0xff;
476 * Return non-zero if v32 can be formed by MOVI+ORR.
477 * Place the parameters for MOVI in (cmode, imm8).
478 * Return the cmode for ORR; the imm8 can be had via extraction from v32.
480 static int is_shimm32_pair(uint32_t v32, int *cmode, int *imm8)
484 for (i = 6; i > 0; i -= 2) {
485 /* Mask out one byte we can add with ORR. */
486 uint32_t tmp = v32 & ~(0xffu << (i * 4));
487 if (is_shimm32(tmp, cmode, imm8) ||
488 is_soimm32(tmp, cmode, imm8)) {
495 /* Return true if V is a valid 16-bit or 32-bit shifted immediate. */
496 static bool is_shimm1632(uint32_t v32, int *cmode, int *imm8)
498 if (v32 == deposit32(v32, 16, 16, v32)) {
499 return is_shimm16(v32, cmode, imm8);
501 return is_shimm32(v32, cmode, imm8);
505 /* Test if a constant matches the constraint.
506 * TODO: define constraints for:
508 * ldr/str offset: between -0xfff and 0xfff
509 * ldrh/strh offset: between -0xff and 0xff
510 * mov operand2: values represented with x << (2 * y), x < 0x100
511 * add, sub, eor...: ditto
513 static bool tcg_target_const_match(int64_t val, TCGType type, int ct)
515 if (ct & TCG_CT_CONST) {
517 } else if ((ct & TCG_CT_CONST_ARM) && check_fit_imm(val)) {
519 } else if ((ct & TCG_CT_CONST_INV) && check_fit_imm(~val)) {
521 } else if ((ct & TCG_CT_CONST_NEG) && check_fit_imm(-val)) {
523 } else if ((ct & TCG_CT_CONST_ZERO) && val == 0) {
527 switch (ct & (TCG_CT_CONST_ORRI | TCG_CT_CONST_ANDI)) {
530 case TCG_CT_CONST_ANDI:
533 case TCG_CT_CONST_ORRI:
534 if (val == deposit64(val, 32, 32, val)) {
536 return is_shimm1632(val, &cmode, &imm8);
540 /* Both bits should not be set for the same insn. */
541 g_assert_not_reached();
547 static void tcg_out_b_imm(TCGContext *s, ARMCond cond, int32_t offset)
549 tcg_out32(s, (cond << 28) | 0x0a000000 |
550 (((offset - 8) >> 2) & 0x00ffffff));
553 static void tcg_out_bl_imm(TCGContext *s, ARMCond cond, int32_t offset)
555 tcg_out32(s, (cond << 28) | 0x0b000000 |
556 (((offset - 8) >> 2) & 0x00ffffff));
559 static void tcg_out_blx_reg(TCGContext *s, ARMCond cond, TCGReg rn)
561 tcg_out32(s, (cond << 28) | 0x012fff30 | rn);
564 static void tcg_out_blx_imm(TCGContext *s, int32_t offset)
566 tcg_out32(s, 0xfa000000 | ((offset & 2) << 23) |
567 (((offset - 8) >> 2) & 0x00ffffff));
570 static void tcg_out_dat_reg(TCGContext *s, ARMCond cond, ARMInsn opc,
571 TCGReg rd, TCGReg rn, TCGReg rm, int shift)
573 tcg_out32(s, (cond << 28) | (0 << 25) | opc |
574 (rn << 16) | (rd << 12) | shift | rm);
577 static void tcg_out_mov_reg(TCGContext *s, ARMCond cond, TCGReg rd, TCGReg rm)
579 /* Simple reg-reg move, optimising out the 'do nothing' case */
581 tcg_out_dat_reg(s, cond, ARITH_MOV, rd, 0, rm, SHIFT_IMM_LSL(0));
585 static void tcg_out_bx_reg(TCGContext *s, ARMCond cond, TCGReg rn)
587 tcg_out32(s, (cond << 28) | 0x012fff10 | rn);
590 static void tcg_out_b_reg(TCGContext *s, ARMCond cond, TCGReg rn)
593 * Unless the C portion of QEMU is compiled as thumb, we don't need
594 * true BX semantics; merely a branch to an address held in a register.
596 tcg_out_bx_reg(s, cond, rn);
599 static void tcg_out_dat_imm(TCGContext *s, ARMCond cond, ARMInsn opc,
600 TCGReg rd, TCGReg rn, int im)
602 tcg_out32(s, (cond << 28) | (1 << 25) | opc |
603 (rn << 16) | (rd << 12) | im);
606 static void tcg_out_ldstm(TCGContext *s, ARMCond cond, ARMInsn opc,
607 TCGReg rn, uint16_t mask)
609 tcg_out32(s, (cond << 28) | opc | (rn << 16) | mask);
612 /* Note that this routine is used for both LDR and LDRH formats, so we do
613 not wish to include an immediate shift at this point. */
614 static void tcg_out_memop_r(TCGContext *s, ARMCond cond, ARMInsn opc, TCGReg rt,
615 TCGReg rn, TCGReg rm, bool u, bool p, bool w)
617 tcg_out32(s, (cond << 28) | opc | (u << 23) | (p << 24)
618 | (w << 21) | (rn << 16) | (rt << 12) | rm);
621 static void tcg_out_memop_8(TCGContext *s, ARMCond cond, ARMInsn opc, TCGReg rt,
622 TCGReg rn, int imm8, bool p, bool w)
629 tcg_out32(s, (cond << 28) | opc | (u << 23) | (p << 24) | (w << 21) |
630 (rn << 16) | (rt << 12) | ((imm8 & 0xf0) << 4) | (imm8 & 0xf));
633 static void tcg_out_memop_12(TCGContext *s, ARMCond cond, ARMInsn opc,
634 TCGReg rt, TCGReg rn, int imm12, bool p, bool w)
641 tcg_out32(s, (cond << 28) | opc | (u << 23) | (p << 24) | (w << 21) |
642 (rn << 16) | (rt << 12) | imm12);
645 static void tcg_out_ld32_12(TCGContext *s, ARMCond cond, TCGReg rt,
646 TCGReg rn, int imm12)
648 tcg_out_memop_12(s, cond, INSN_LDR_IMM, rt, rn, imm12, 1, 0);
651 static void tcg_out_st32_12(TCGContext *s, ARMCond cond, TCGReg rt,
652 TCGReg rn, int imm12)
654 tcg_out_memop_12(s, cond, INSN_STR_IMM, rt, rn, imm12, 1, 0);
657 static void tcg_out_ld32_r(TCGContext *s, ARMCond cond, TCGReg rt,
658 TCGReg rn, TCGReg rm)
660 tcg_out_memop_r(s, cond, INSN_LDR_REG, rt, rn, rm, 1, 1, 0);
663 static void tcg_out_st32_r(TCGContext *s, ARMCond cond, TCGReg rt,
664 TCGReg rn, TCGReg rm)
666 tcg_out_memop_r(s, cond, INSN_STR_REG, rt, rn, rm, 1, 1, 0);
669 static void tcg_out_ldrd_8(TCGContext *s, ARMCond cond, TCGReg rt,
672 tcg_out_memop_8(s, cond, INSN_LDRD_IMM, rt, rn, imm8, 1, 0);
675 static void tcg_out_ldrd_r(TCGContext *s, ARMCond cond, TCGReg rt,
676 TCGReg rn, TCGReg rm)
678 tcg_out_memop_r(s, cond, INSN_LDRD_REG, rt, rn, rm, 1, 1, 0);
681 static void __attribute__((unused))
682 tcg_out_ldrd_rwb(TCGContext *s, ARMCond cond, TCGReg rt, TCGReg rn, TCGReg rm)
684 tcg_out_memop_r(s, cond, INSN_LDRD_REG, rt, rn, rm, 1, 1, 1);
687 static void tcg_out_strd_8(TCGContext *s, ARMCond cond, TCGReg rt,
690 tcg_out_memop_8(s, cond, INSN_STRD_IMM, rt, rn, imm8, 1, 0);
693 static void tcg_out_strd_r(TCGContext *s, ARMCond cond, TCGReg rt,
694 TCGReg rn, TCGReg rm)
696 tcg_out_memop_r(s, cond, INSN_STRD_REG, rt, rn, rm, 1, 1, 0);
699 /* Register pre-increment with base writeback. */
700 static void tcg_out_ld32_rwb(TCGContext *s, ARMCond cond, TCGReg rt,
701 TCGReg rn, TCGReg rm)
703 tcg_out_memop_r(s, cond, INSN_LDR_REG, rt, rn, rm, 1, 1, 1);
706 static void tcg_out_st32_rwb(TCGContext *s, ARMCond cond, TCGReg rt,
707 TCGReg rn, TCGReg rm)
709 tcg_out_memop_r(s, cond, INSN_STR_REG, rt, rn, rm, 1, 1, 1);
712 static void tcg_out_ld16u_8(TCGContext *s, ARMCond cond, TCGReg rt,
715 tcg_out_memop_8(s, cond, INSN_LDRH_IMM, rt, rn, imm8, 1, 0);
718 static void tcg_out_st16_8(TCGContext *s, ARMCond cond, TCGReg rt,
721 tcg_out_memop_8(s, cond, INSN_STRH_IMM, rt, rn, imm8, 1, 0);
724 static void tcg_out_ld16u_r(TCGContext *s, ARMCond cond, TCGReg rt,
725 TCGReg rn, TCGReg rm)
727 tcg_out_memop_r(s, cond, INSN_LDRH_REG, rt, rn, rm, 1, 1, 0);
730 static void tcg_out_st16_r(TCGContext *s, ARMCond cond, TCGReg rt,
731 TCGReg rn, TCGReg rm)
733 tcg_out_memop_r(s, cond, INSN_STRH_REG, rt, rn, rm, 1, 1, 0);
736 static void tcg_out_ld16s_8(TCGContext *s, ARMCond cond, TCGReg rt,
739 tcg_out_memop_8(s, cond, INSN_LDRSH_IMM, rt, rn, imm8, 1, 0);
742 static void tcg_out_ld16s_r(TCGContext *s, ARMCond cond, TCGReg rt,
743 TCGReg rn, TCGReg rm)
745 tcg_out_memop_r(s, cond, INSN_LDRSH_REG, rt, rn, rm, 1, 1, 0);
748 static void tcg_out_ld8_12(TCGContext *s, ARMCond cond, TCGReg rt,
749 TCGReg rn, int imm12)
751 tcg_out_memop_12(s, cond, INSN_LDRB_IMM, rt, rn, imm12, 1, 0);
754 static void tcg_out_st8_12(TCGContext *s, ARMCond cond, TCGReg rt,
755 TCGReg rn, int imm12)
757 tcg_out_memop_12(s, cond, INSN_STRB_IMM, rt, rn, imm12, 1, 0);
760 static void tcg_out_ld8_r(TCGContext *s, ARMCond cond, TCGReg rt,
761 TCGReg rn, TCGReg rm)
763 tcg_out_memop_r(s, cond, INSN_LDRB_REG, rt, rn, rm, 1, 1, 0);
766 static void tcg_out_st8_r(TCGContext *s, ARMCond cond, TCGReg rt,
767 TCGReg rn, TCGReg rm)
769 tcg_out_memop_r(s, cond, INSN_STRB_REG, rt, rn, rm, 1, 1, 0);
772 static void tcg_out_ld8s_8(TCGContext *s, ARMCond cond, TCGReg rt,
775 tcg_out_memop_8(s, cond, INSN_LDRSB_IMM, rt, rn, imm8, 1, 0);
778 static void tcg_out_ld8s_r(TCGContext *s, ARMCond cond, TCGReg rt,
779 TCGReg rn, TCGReg rm)
781 tcg_out_memop_r(s, cond, INSN_LDRSB_REG, rt, rn, rm, 1, 1, 0);
784 static void tcg_out_movi_pool(TCGContext *s, ARMCond cond,
785 TCGReg rd, uint32_t arg)
787 new_pool_label(s, arg, R_ARM_PC13, s->code_ptr, 0);
788 tcg_out_ld32_12(s, cond, rd, TCG_REG_PC, 0);
791 static void tcg_out_movi32(TCGContext *s, ARMCond cond,
792 TCGReg rd, uint32_t arg)
794 int imm12, diff, opc, sh1, sh2;
795 uint32_t tt0, tt1, tt2;
797 /* Check a single MOV/MVN before anything else. */
798 imm12 = encode_imm(arg);
800 tcg_out_dat_imm(s, cond, ARITH_MOV, rd, 0, imm12);
803 imm12 = encode_imm(~arg);
805 tcg_out_dat_imm(s, cond, ARITH_MVN, rd, 0, imm12);
809 /* Check for a pc-relative address. This will usually be the TB,
810 or within the TB, which is immediately before the code block. */
811 diff = tcg_pcrel_diff(s, (void *)arg) - 8;
813 imm12 = encode_imm(diff);
815 tcg_out_dat_imm(s, cond, ARITH_ADD, rd, TCG_REG_PC, imm12);
819 imm12 = encode_imm(-diff);
821 tcg_out_dat_imm(s, cond, ARITH_SUB, rd, TCG_REG_PC, imm12);
826 /* Use movw + movt. */
827 if (use_armv7_instructions) {
829 tcg_out32(s, (cond << 28) | 0x03000000 | (rd << 12)
830 | ((arg << 4) & 0x000f0000) | (arg & 0xfff));
831 if (arg & 0xffff0000) {
833 tcg_out32(s, (cond << 28) | 0x03400000 | (rd << 12)
834 | ((arg >> 12) & 0x000f0000) | ((arg >> 16) & 0xfff));
839 /* Look for sequences of two insns. If we have lots of 1's, we can
840 shorten the sequence by beginning with mvn and then clearing
841 higher bits with eor. */
844 if (ctpop32(arg) > 16) {
848 sh1 = ctz32(tt0) & ~1;
849 tt1 = tt0 & ~(0xff << sh1);
850 sh2 = ctz32(tt1) & ~1;
851 tt2 = tt1 & ~(0xff << sh2);
855 rot = ((32 - sh1) << 7) & 0xf00;
856 tcg_out_dat_imm(s, cond, opc, rd, 0, ((tt0 >> sh1) & 0xff) | rot);
857 rot = ((32 - sh2) << 7) & 0xf00;
858 tcg_out_dat_imm(s, cond, ARITH_EOR, rd, rd,
859 ((tt0 >> sh2) & 0xff) | rot);
863 /* Otherwise, drop it into the constant pool. */
864 tcg_out_movi_pool(s, cond, rd, arg);
868 * Emit either the reg,imm or reg,reg form of a data-processing insn.
869 * rhs must satisfy the "rI" constraint.
871 static void tcg_out_dat_rI(TCGContext *s, ARMCond cond, ARMInsn opc,
872 TCGReg dst, TCGReg lhs, TCGArg rhs, int rhs_is_const)
875 tcg_out_dat_imm(s, cond, opc, dst, lhs, encode_imm_nofail(rhs));
877 tcg_out_dat_reg(s, cond, opc, dst, lhs, rhs, SHIFT_IMM_LSL(0));
882 * Emit either the reg,imm or reg,reg form of a data-processing insn.
883 * rhs must satisfy the "rIK" constraint.
885 static void tcg_out_dat_rIK(TCGContext *s, ARMCond cond, ARMInsn opc,
886 ARMInsn opinv, TCGReg dst, TCGReg lhs, TCGArg rhs,
890 int imm12 = encode_imm(rhs);
892 imm12 = encode_imm_nofail(~rhs);
895 tcg_out_dat_imm(s, cond, opc, dst, lhs, imm12);
897 tcg_out_dat_reg(s, cond, opc, dst, lhs, rhs, SHIFT_IMM_LSL(0));
901 static void tcg_out_dat_rIN(TCGContext *s, ARMCond cond, ARMInsn opc,
902 ARMInsn opneg, TCGReg dst, TCGReg lhs, TCGArg rhs,
905 /* Emit either the reg,imm or reg,reg form of a data-processing insn.
906 * rhs must satisfy the "rIN" constraint.
909 int imm12 = encode_imm(rhs);
911 imm12 = encode_imm_nofail(-rhs);
914 tcg_out_dat_imm(s, cond, opc, dst, lhs, imm12);
916 tcg_out_dat_reg(s, cond, opc, dst, lhs, rhs, SHIFT_IMM_LSL(0));
920 static void tcg_out_mul32(TCGContext *s, ARMCond cond, TCGReg rd,
921 TCGReg rn, TCGReg rm)
924 tcg_out32(s, (cond << 28) | 0x90 | (rd << 16) | (rm << 8) | rn);
927 static void tcg_out_umull32(TCGContext *s, ARMCond cond, TCGReg rd0,
928 TCGReg rd1, TCGReg rn, TCGReg rm)
931 tcg_out32(s, (cond << 28) | 0x00800090 |
932 (rd1 << 16) | (rd0 << 12) | (rm << 8) | rn);
935 static void tcg_out_smull32(TCGContext *s, ARMCond cond, TCGReg rd0,
936 TCGReg rd1, TCGReg rn, TCGReg rm)
939 tcg_out32(s, (cond << 28) | 0x00c00090 |
940 (rd1 << 16) | (rd0 << 12) | (rm << 8) | rn);
943 static void tcg_out_sdiv(TCGContext *s, ARMCond cond,
944 TCGReg rd, TCGReg rn, TCGReg rm)
946 tcg_out32(s, 0x0710f010 | (cond << 28) | (rd << 16) | rn | (rm << 8));
949 static void tcg_out_udiv(TCGContext *s, ARMCond cond,
950 TCGReg rd, TCGReg rn, TCGReg rm)
952 tcg_out32(s, 0x0730f010 | (cond << 28) | (rd << 16) | rn | (rm << 8));
955 static void tcg_out_ext8s(TCGContext *s, ARMCond cond, TCGReg rd, TCGReg rn)
958 tcg_out32(s, 0x06af0070 | (cond << 28) | (rd << 12) | rn);
961 static void __attribute__((unused))
962 tcg_out_ext8u(TCGContext *s, ARMCond cond, TCGReg rd, TCGReg rn)
964 tcg_out_dat_imm(s, cond, ARITH_AND, rd, rn, 0xff);
967 static void tcg_out_ext16s(TCGContext *s, ARMCond cond, TCGReg rd, TCGReg rn)
970 tcg_out32(s, 0x06bf0070 | (cond << 28) | (rd << 12) | rn);
973 static void tcg_out_ext16u(TCGContext *s, ARMCond cond, TCGReg rd, TCGReg rn)
976 tcg_out32(s, 0x06ff0070 | (cond << 28) | (rd << 12) | rn);
979 static void tcg_out_bswap16(TCGContext *s, ARMCond cond,
980 TCGReg rd, TCGReg rn, int flags)
982 if (flags & TCG_BSWAP_OS) {
984 tcg_out32(s, 0x06ff0fb0 | (cond << 28) | (rd << 12) | rn);
989 tcg_out32(s, 0x06bf0fb0 | (cond << 28) | (rd << 12) | rn);
990 if ((flags & (TCG_BSWAP_IZ | TCG_BSWAP_OZ)) == TCG_BSWAP_OZ) {
992 tcg_out32(s, 0x06ff0070 | (cond << 28) | (rd << 12) | rd);
996 static void tcg_out_bswap32(TCGContext *s, ARMCond cond, TCGReg rd, TCGReg rn)
999 tcg_out32(s, 0x06bf0f30 | (cond << 28) | (rd << 12) | rn);
1002 static void tcg_out_deposit(TCGContext *s, ARMCond cond, TCGReg rd,
1003 TCGArg a1, int ofs, int len, bool const_a1)
1006 /* bfi becomes bfc with rn == 15. */
1010 tcg_out32(s, 0x07c00010 | (cond << 28) | (rd << 12) | a1
1011 | (ofs << 7) | ((ofs + len - 1) << 16));
1014 static void tcg_out_extract(TCGContext *s, ARMCond cond, TCGReg rd,
1015 TCGReg rn, int ofs, int len)
1018 tcg_out32(s, 0x07e00050 | (cond << 28) | (rd << 12) | rn
1019 | (ofs << 7) | ((len - 1) << 16));
1022 static void tcg_out_sextract(TCGContext *s, ARMCond cond, TCGReg rd,
1023 TCGReg rn, int ofs, int len)
1026 tcg_out32(s, 0x07a00050 | (cond << 28) | (rd << 12) | rn
1027 | (ofs << 7) | ((len - 1) << 16));
1030 static void tcg_out_ld32u(TCGContext *s, ARMCond cond,
1031 TCGReg rd, TCGReg rn, int32_t offset)
1033 if (offset > 0xfff || offset < -0xfff) {
1034 tcg_out_movi32(s, cond, TCG_REG_TMP, offset);
1035 tcg_out_ld32_r(s, cond, rd, rn, TCG_REG_TMP);
1037 tcg_out_ld32_12(s, cond, rd, rn, offset);
1040 static void tcg_out_st32(TCGContext *s, ARMCond cond,
1041 TCGReg rd, TCGReg rn, int32_t offset)
1043 if (offset > 0xfff || offset < -0xfff) {
1044 tcg_out_movi32(s, cond, TCG_REG_TMP, offset);
1045 tcg_out_st32_r(s, cond, rd, rn, TCG_REG_TMP);
1047 tcg_out_st32_12(s, cond, rd, rn, offset);
1050 static void tcg_out_ld16u(TCGContext *s, ARMCond cond,
1051 TCGReg rd, TCGReg rn, int32_t offset)
1053 if (offset > 0xff || offset < -0xff) {
1054 tcg_out_movi32(s, cond, TCG_REG_TMP, offset);
1055 tcg_out_ld16u_r(s, cond, rd, rn, TCG_REG_TMP);
1057 tcg_out_ld16u_8(s, cond, rd, rn, offset);
1060 static void tcg_out_ld16s(TCGContext *s, ARMCond cond,
1061 TCGReg rd, TCGReg rn, int32_t offset)
1063 if (offset > 0xff || offset < -0xff) {
1064 tcg_out_movi32(s, cond, TCG_REG_TMP, offset);
1065 tcg_out_ld16s_r(s, cond, rd, rn, TCG_REG_TMP);
1067 tcg_out_ld16s_8(s, cond, rd, rn, offset);
1070 static void tcg_out_st16(TCGContext *s, ARMCond cond,
1071 TCGReg rd, TCGReg rn, int32_t offset)
1073 if (offset > 0xff || offset < -0xff) {
1074 tcg_out_movi32(s, cond, TCG_REG_TMP, offset);
1075 tcg_out_st16_r(s, cond, rd, rn, TCG_REG_TMP);
1077 tcg_out_st16_8(s, cond, rd, rn, offset);
1080 static void tcg_out_ld8u(TCGContext *s, ARMCond cond,
1081 TCGReg rd, TCGReg rn, int32_t offset)
1083 if (offset > 0xfff || offset < -0xfff) {
1084 tcg_out_movi32(s, cond, TCG_REG_TMP, offset);
1085 tcg_out_ld8_r(s, cond, rd, rn, TCG_REG_TMP);
1087 tcg_out_ld8_12(s, cond, rd, rn, offset);
1090 static void tcg_out_ld8s(TCGContext *s, ARMCond cond,
1091 TCGReg rd, TCGReg rn, int32_t offset)
1093 if (offset > 0xff || offset < -0xff) {
1094 tcg_out_movi32(s, cond, TCG_REG_TMP, offset);
1095 tcg_out_ld8s_r(s, cond, rd, rn, TCG_REG_TMP);
1097 tcg_out_ld8s_8(s, cond, rd, rn, offset);
1100 static void tcg_out_st8(TCGContext *s, ARMCond cond,
1101 TCGReg rd, TCGReg rn, int32_t offset)
1103 if (offset > 0xfff || offset < -0xfff) {
1104 tcg_out_movi32(s, cond, TCG_REG_TMP, offset);
1105 tcg_out_st8_r(s, cond, rd, rn, TCG_REG_TMP);
1107 tcg_out_st8_12(s, cond, rd, rn, offset);
1111 * The _goto case is normally between TBs within the same code buffer, and
1112 * with the code buffer limited to 16MB we wouldn't need the long case.
1113 * But we also use it for the tail-call to the qemu_ld/st helpers, which does.
1115 static void tcg_out_goto(TCGContext *s, ARMCond cond, const tcg_insn_unit *addr)
1117 intptr_t addri = (intptr_t)addr;
1118 ptrdiff_t disp = tcg_pcrel_diff(s, addr);
1119 bool arm_mode = !(addri & 1);
1121 if (arm_mode && disp - 8 < 0x01fffffd && disp - 8 > -0x01fffffd) {
1122 tcg_out_b_imm(s, cond, disp);
1126 /* LDR is interworking from v5t. */
1127 tcg_out_movi_pool(s, cond, TCG_REG_PC, addri);
1131 * The call case is mostly used for helpers - so it's not unreasonable
1132 * for them to be beyond branch range.
1134 static void tcg_out_call(TCGContext *s, const tcg_insn_unit *addr)
1136 intptr_t addri = (intptr_t)addr;
1137 ptrdiff_t disp = tcg_pcrel_diff(s, addr);
1138 bool arm_mode = !(addri & 1);
1140 if (disp - 8 < 0x02000000 && disp - 8 >= -0x02000000) {
1142 tcg_out_bl_imm(s, COND_AL, disp);
1144 tcg_out_blx_imm(s, disp);
1149 tcg_out_movi32(s, COND_AL, TCG_REG_TMP, addri);
1150 tcg_out_blx_reg(s, COND_AL, TCG_REG_TMP);
1153 static void tcg_out_goto_label(TCGContext *s, ARMCond cond, TCGLabel *l)
1156 tcg_out_goto(s, cond, l->u.value_ptr);
1158 tcg_out_reloc(s, s->code_ptr, R_ARM_PC24, l, 0);
1159 tcg_out_b_imm(s, cond, 0);
1163 static void tcg_out_mb(TCGContext *s, TCGArg a0)
1165 if (use_armv7_instructions) {
1166 tcg_out32(s, INSN_DMB_ISH);
1168 tcg_out32(s, INSN_DMB_MCR);
1172 static TCGCond tcg_out_cmp2(TCGContext *s, const TCGArg *args,
1173 const int *const_args)
1175 TCGReg al = args[0];
1176 TCGReg ah = args[1];
1177 TCGArg bl = args[2];
1178 TCGArg bh = args[3];
1179 TCGCond cond = args[4];
1180 int const_bl = const_args[2];
1181 int const_bh = const_args[3];
1190 /* We perform a conditional comparision. If the high half is
1191 equal, then overwrite the flags with the comparison of the
1192 low half. The resulting flags cover the whole. */
1193 tcg_out_dat_rI(s, COND_AL, ARITH_CMP, 0, ah, bh, const_bh);
1194 tcg_out_dat_rI(s, COND_EQ, ARITH_CMP, 0, al, bl, const_bl);
1199 /* We perform a double-word subtraction and examine the result.
1200 We do not actually need the result of the subtract, so the
1201 low part "subtract" is a compare. For the high half we have
1202 no choice but to compute into a temporary. */
1203 tcg_out_dat_rI(s, COND_AL, ARITH_CMP, 0, al, bl, const_bl);
1204 tcg_out_dat_rI(s, COND_AL, ARITH_SBC | TO_CPSR,
1205 TCG_REG_TMP, ah, bh, const_bh);
1210 /* Similar, but with swapped arguments, via reversed subtract. */
1211 tcg_out_dat_rI(s, COND_AL, ARITH_RSB | TO_CPSR,
1212 TCG_REG_TMP, al, bl, const_bl);
1213 tcg_out_dat_rI(s, COND_AL, ARITH_RSC | TO_CPSR,
1214 TCG_REG_TMP, ah, bh, const_bh);
1215 return tcg_swap_cond(cond);
1218 g_assert_not_reached();
1223 * Note that TCGReg references Q-registers.
1224 * Q-regno = 2 * D-regno, so shift left by 1 whlie inserting.
1226 static uint32_t encode_vd(TCGReg rd)
1228 tcg_debug_assert(rd >= TCG_REG_Q0);
1229 return (extract32(rd, 3, 1) << 22) | (extract32(rd, 0, 3) << 13);
1232 static uint32_t encode_vn(TCGReg rn)
1234 tcg_debug_assert(rn >= TCG_REG_Q0);
1235 return (extract32(rn, 3, 1) << 7) | (extract32(rn, 0, 3) << 17);
1238 static uint32_t encode_vm(TCGReg rm)
1240 tcg_debug_assert(rm >= TCG_REG_Q0);
1241 return (extract32(rm, 3, 1) << 5) | (extract32(rm, 0, 3) << 1);
1244 static void tcg_out_vreg2(TCGContext *s, ARMInsn insn, int q, int vece,
1247 tcg_out32(s, insn | (vece << 18) | (q << 6) |
1248 encode_vd(d) | encode_vm(m));
1251 static void tcg_out_vreg3(TCGContext *s, ARMInsn insn, int q, int vece,
1252 TCGReg d, TCGReg n, TCGReg m)
1254 tcg_out32(s, insn | (vece << 20) | (q << 6) |
1255 encode_vd(d) | encode_vn(n) | encode_vm(m));
1258 static void tcg_out_vmovi(TCGContext *s, TCGReg rd,
1259 int q, int op, int cmode, uint8_t imm8)
1261 tcg_out32(s, INSN_VMOVI | encode_vd(rd) | (q << 6) | (op << 5)
1262 | (cmode << 8) | extract32(imm8, 0, 4)
1263 | (extract32(imm8, 4, 3) << 16)
1264 | (extract32(imm8, 7, 1) << 24));
1267 static void tcg_out_vshifti(TCGContext *s, ARMInsn insn, int q,
1268 TCGReg rd, TCGReg rm, int l_imm6)
1270 tcg_out32(s, insn | (q << 6) | encode_vd(rd) | encode_vm(rm) |
1271 (extract32(l_imm6, 6, 1) << 7) |
1272 (extract32(l_imm6, 0, 6) << 16));
1275 static void tcg_out_vldst(TCGContext *s, ARMInsn insn,
1276 TCGReg rd, TCGReg rn, int offset)
1279 if (check_fit_imm(offset) || check_fit_imm(-offset)) {
1280 tcg_out_dat_rIN(s, COND_AL, ARITH_ADD, ARITH_SUB,
1281 TCG_REG_TMP, rn, offset, true);
1283 tcg_out_movi(s, TCG_TYPE_PTR, TCG_REG_TMP, offset);
1284 tcg_out_dat_reg(s, COND_AL, ARITH_ADD,
1285 TCG_REG_TMP, TCG_REG_TMP, rn, 0);
1289 tcg_out32(s, insn | (rn << 16) | encode_vd(rd) | 0xf);
1292 #ifdef CONFIG_SOFTMMU
1293 /* helper signature: helper_ret_ld_mmu(CPUState *env, target_ulong addr,
1294 * int mmu_idx, uintptr_t ra)
1296 static void * const qemu_ld_helpers[MO_SSIZE + 1] = {
1297 [MO_UB] = helper_ret_ldub_mmu,
1298 [MO_SB] = helper_ret_ldsb_mmu,
1300 [MO_UW] = helper_be_lduw_mmu,
1301 [MO_UL] = helper_be_ldul_mmu,
1302 [MO_UQ] = helper_be_ldq_mmu,
1303 [MO_SW] = helper_be_ldsw_mmu,
1304 [MO_SL] = helper_be_ldul_mmu,
1306 [MO_UW] = helper_le_lduw_mmu,
1307 [MO_UL] = helper_le_ldul_mmu,
1308 [MO_UQ] = helper_le_ldq_mmu,
1309 [MO_SW] = helper_le_ldsw_mmu,
1310 [MO_SL] = helper_le_ldul_mmu,
1314 /* helper signature: helper_ret_st_mmu(CPUState *env, target_ulong addr,
1315 * uintxx_t val, int mmu_idx, uintptr_t ra)
1317 static void * const qemu_st_helpers[MO_SIZE + 1] = {
1318 [MO_8] = helper_ret_stb_mmu,
1320 [MO_16] = helper_be_stw_mmu,
1321 [MO_32] = helper_be_stl_mmu,
1322 [MO_64] = helper_be_stq_mmu,
1324 [MO_16] = helper_le_stw_mmu,
1325 [MO_32] = helper_le_stl_mmu,
1326 [MO_64] = helper_le_stq_mmu,
1330 /* Helper routines for marshalling helper function arguments into
1331 * the correct registers and stack.
1332 * argreg is where we want to put this argument, arg is the argument itself.
1333 * Return value is the updated argreg ready for the next call.
1334 * Note that argreg 0..3 is real registers, 4+ on stack.
1336 * We provide routines for arguments which are: immediate, 32 bit
1337 * value in register, 16 and 8 bit values in register (which must be zero
1338 * extended before use) and 64 bit value in a lo:hi register pair.
1340 #define DEFINE_TCG_OUT_ARG(NAME, ARGTYPE, MOV_ARG, EXT_ARG) \
1341 static TCGReg NAME(TCGContext *s, TCGReg argreg, ARGTYPE arg) \
1344 MOV_ARG(s, COND_AL, argreg, arg); \
1346 int ofs = (argreg - 4) * 4; \
1348 tcg_debug_assert(ofs + 4 <= TCG_STATIC_CALL_ARGS_SIZE); \
1349 tcg_out_st32_12(s, COND_AL, arg, TCG_REG_CALL_STACK, ofs); \
1351 return argreg + 1; \
1354 DEFINE_TCG_OUT_ARG(tcg_out_arg_imm32, uint32_t, tcg_out_movi32,
1355 (tcg_out_movi32(s, COND_AL, TCG_REG_TMP, arg), arg = TCG_REG_TMP))
1356 DEFINE_TCG_OUT_ARG(tcg_out_arg_reg8, TCGReg, tcg_out_ext8u,
1357 (tcg_out_ext8u(s, COND_AL, TCG_REG_TMP, arg), arg = TCG_REG_TMP))
1358 DEFINE_TCG_OUT_ARG(tcg_out_arg_reg16, TCGReg, tcg_out_ext16u,
1359 (tcg_out_ext16u(s, COND_AL, TCG_REG_TMP, arg), arg = TCG_REG_TMP))
1360 DEFINE_TCG_OUT_ARG(tcg_out_arg_reg32, TCGReg, tcg_out_mov_reg, )
1362 static TCGReg tcg_out_arg_reg64(TCGContext *s, TCGReg argreg,
1363 TCGReg arglo, TCGReg arghi)
1365 /* 64 bit arguments must go in even/odd register pairs
1366 * and in 8-aligned stack slots.
1371 if (argreg >= 4 && (arglo & 1) == 0 && arghi == arglo + 1) {
1372 tcg_out_strd_8(s, COND_AL, arglo,
1373 TCG_REG_CALL_STACK, (argreg - 4) * 4);
1376 argreg = tcg_out_arg_reg32(s, argreg, arglo);
1377 argreg = tcg_out_arg_reg32(s, argreg, arghi);
1382 #define TLB_SHIFT (CPU_TLB_ENTRY_BITS + CPU_TLB_BITS)
1384 /* We expect to use an 9-bit sign-magnitude negative offset from ENV. */
1385 QEMU_BUILD_BUG_ON(TLB_MASK_TABLE_OFS(0) > 0);
1386 QEMU_BUILD_BUG_ON(TLB_MASK_TABLE_OFS(0) < -256);
1388 /* These offsets are built into the LDRD below. */
1389 QEMU_BUILD_BUG_ON(offsetof(CPUTLBDescFast, mask) != 0);
1390 QEMU_BUILD_BUG_ON(offsetof(CPUTLBDescFast, table) != 4);
1392 /* Load and compare a TLB entry, leaving the flags set. Returns the register
1393 containing the addend of the tlb entry. Clobbers R0, R1, R2, TMP. */
1395 static TCGReg tcg_out_tlb_read(TCGContext *s, TCGReg addrlo, TCGReg addrhi,
1396 MemOp opc, int mem_index, bool is_load)
1398 int cmp_off = (is_load ? offsetof(CPUTLBEntry, addr_read)
1399 : offsetof(CPUTLBEntry, addr_write));
1400 int fast_off = TLB_MASK_TABLE_OFS(mem_index);
1401 unsigned s_mask = (1 << (opc & MO_SIZE)) - 1;
1402 unsigned a_mask = (1 << get_alignment_bits(opc)) - 1;
1405 /* Load env_tlb(env)->f[mmu_idx].{mask,table} into {r0,r1}. */
1406 tcg_out_ldrd_8(s, COND_AL, TCG_REG_R0, TCG_AREG0, fast_off);
1408 /* Extract the tlb index from the address into R0. */
1409 tcg_out_dat_reg(s, COND_AL, ARITH_AND, TCG_REG_R0, TCG_REG_R0, addrlo,
1410 SHIFT_IMM_LSR(TARGET_PAGE_BITS - CPU_TLB_ENTRY_BITS));
1413 * Add the tlb_table pointer, creating the CPUTLBEntry address in R1.
1414 * Load the tlb comparator into R2/R3 and the fast path addend into R1.
1417 if (TARGET_LONG_BITS == 64) {
1418 tcg_out_ldrd_rwb(s, COND_AL, TCG_REG_R2, TCG_REG_R1, TCG_REG_R0);
1420 tcg_out_ld32_rwb(s, COND_AL, TCG_REG_R2, TCG_REG_R1, TCG_REG_R0);
1423 tcg_out_dat_reg(s, COND_AL, ARITH_ADD,
1424 TCG_REG_R1, TCG_REG_R1, TCG_REG_R0, 0);
1425 if (TARGET_LONG_BITS == 64) {
1426 tcg_out_ldrd_8(s, COND_AL, TCG_REG_R2, TCG_REG_R1, cmp_off);
1428 tcg_out_ld32_12(s, COND_AL, TCG_REG_R2, TCG_REG_R1, cmp_off);
1432 /* Load the tlb addend. */
1433 tcg_out_ld32_12(s, COND_AL, TCG_REG_R1, TCG_REG_R1,
1434 offsetof(CPUTLBEntry, addend));
1437 * Check alignment, check comparators.
1438 * Do this in 2-4 insns. Use MOVW for v7, if possible,
1439 * to reduce the number of sequential conditional instructions.
1440 * Almost all guests have at least 4k pages, which means that we need
1441 * to clear at least 9 bits even for an 8-byte memory, which means it
1442 * isn't worth checking for an immediate operand for BIC.
1444 * For unaligned accesses, test the page of the last unit of alignment.
1445 * This leaves the least significant alignment bits unchanged, and of
1446 * course must be zero.
1449 if (a_mask < s_mask) {
1450 t_addr = TCG_REG_R0;
1451 tcg_out_dat_imm(s, COND_AL, ARITH_ADD, t_addr,
1452 addrlo, s_mask - a_mask);
1454 if (use_armv7_instructions && TARGET_PAGE_BITS <= 16) {
1455 tcg_out_movi32(s, COND_AL, TCG_REG_TMP, ~(TARGET_PAGE_MASK | a_mask));
1456 tcg_out_dat_reg(s, COND_AL, ARITH_BIC, TCG_REG_TMP,
1457 t_addr, TCG_REG_TMP, 0);
1458 tcg_out_dat_reg(s, COND_AL, ARITH_CMP, 0, TCG_REG_R2, TCG_REG_TMP, 0);
1461 tcg_debug_assert(a_mask <= 0xff);
1462 tcg_out_dat_imm(s, COND_AL, ARITH_TST, 0, addrlo, a_mask);
1464 tcg_out_dat_reg(s, COND_AL, ARITH_MOV, TCG_REG_TMP, 0, t_addr,
1465 SHIFT_IMM_LSR(TARGET_PAGE_BITS));
1466 tcg_out_dat_reg(s, (a_mask ? COND_EQ : COND_AL), ARITH_CMP,
1467 0, TCG_REG_R2, TCG_REG_TMP,
1468 SHIFT_IMM_LSL(TARGET_PAGE_BITS));
1471 if (TARGET_LONG_BITS == 64) {
1472 tcg_out_dat_reg(s, COND_EQ, ARITH_CMP, 0, TCG_REG_R3, addrhi, 0);
1478 /* Record the context of a call to the out of line helper code for the slow
1479 path for a load or store, so that we can later generate the correct
1481 static void add_qemu_ldst_label(TCGContext *s, bool is_ld, MemOpIdx oi,
1482 TCGReg datalo, TCGReg datahi, TCGReg addrlo,
1483 TCGReg addrhi, tcg_insn_unit *raddr,
1484 tcg_insn_unit *label_ptr)
1486 TCGLabelQemuLdst *label = new_ldst_label(s);
1488 label->is_ld = is_ld;
1490 label->datalo_reg = datalo;
1491 label->datahi_reg = datahi;
1492 label->addrlo_reg = addrlo;
1493 label->addrhi_reg = addrhi;
1494 label->raddr = tcg_splitwx_to_rx(raddr);
1495 label->label_ptr[0] = label_ptr;
1498 static bool tcg_out_qemu_ld_slow_path(TCGContext *s, TCGLabelQemuLdst *lb)
1500 TCGReg argreg, datalo, datahi;
1501 MemOpIdx oi = lb->oi;
1502 MemOp opc = get_memop(oi);
1504 if (!reloc_pc24(lb->label_ptr[0], tcg_splitwx_to_rx(s->code_ptr))) {
1508 argreg = tcg_out_arg_reg32(s, TCG_REG_R0, TCG_AREG0);
1509 if (TARGET_LONG_BITS == 64) {
1510 argreg = tcg_out_arg_reg64(s, argreg, lb->addrlo_reg, lb->addrhi_reg);
1512 argreg = tcg_out_arg_reg32(s, argreg, lb->addrlo_reg);
1514 argreg = tcg_out_arg_imm32(s, argreg, oi);
1515 argreg = tcg_out_arg_reg32(s, argreg, TCG_REG_R14);
1517 /* Use the canonical unsigned helpers and minimize icache usage. */
1518 tcg_out_call(s, qemu_ld_helpers[opc & MO_SIZE]);
1520 datalo = lb->datalo_reg;
1521 datahi = lb->datahi_reg;
1522 switch (opc & MO_SSIZE) {
1524 tcg_out_ext8s(s, COND_AL, datalo, TCG_REG_R0);
1527 tcg_out_ext16s(s, COND_AL, datalo, TCG_REG_R0);
1530 tcg_out_mov_reg(s, COND_AL, datalo, TCG_REG_R0);
1533 if (datalo != TCG_REG_R1) {
1534 tcg_out_mov_reg(s, COND_AL, datalo, TCG_REG_R0);
1535 tcg_out_mov_reg(s, COND_AL, datahi, TCG_REG_R1);
1536 } else if (datahi != TCG_REG_R0) {
1537 tcg_out_mov_reg(s, COND_AL, datahi, TCG_REG_R1);
1538 tcg_out_mov_reg(s, COND_AL, datalo, TCG_REG_R0);
1540 tcg_out_mov_reg(s, COND_AL, TCG_REG_TMP, TCG_REG_R0);
1541 tcg_out_mov_reg(s, COND_AL, datahi, TCG_REG_R1);
1542 tcg_out_mov_reg(s, COND_AL, datalo, TCG_REG_TMP);
1547 tcg_out_goto(s, COND_AL, lb->raddr);
1551 static bool tcg_out_qemu_st_slow_path(TCGContext *s, TCGLabelQemuLdst *lb)
1553 TCGReg argreg, datalo, datahi;
1554 MemOpIdx oi = lb->oi;
1555 MemOp opc = get_memop(oi);
1557 if (!reloc_pc24(lb->label_ptr[0], tcg_splitwx_to_rx(s->code_ptr))) {
1561 argreg = TCG_REG_R0;
1562 argreg = tcg_out_arg_reg32(s, argreg, TCG_AREG0);
1563 if (TARGET_LONG_BITS == 64) {
1564 argreg = tcg_out_arg_reg64(s, argreg, lb->addrlo_reg, lb->addrhi_reg);
1566 argreg = tcg_out_arg_reg32(s, argreg, lb->addrlo_reg);
1569 datalo = lb->datalo_reg;
1570 datahi = lb->datahi_reg;
1571 switch (opc & MO_SIZE) {
1573 argreg = tcg_out_arg_reg8(s, argreg, datalo);
1576 argreg = tcg_out_arg_reg16(s, argreg, datalo);
1580 argreg = tcg_out_arg_reg32(s, argreg, datalo);
1583 argreg = tcg_out_arg_reg64(s, argreg, datalo, datahi);
1587 argreg = tcg_out_arg_imm32(s, argreg, oi);
1588 argreg = tcg_out_arg_reg32(s, argreg, TCG_REG_R14);
1590 /* Tail-call to the helper, which will return to the fast path. */
1591 tcg_out_goto(s, COND_AL, qemu_st_helpers[opc & MO_SIZE]);
1596 static void tcg_out_test_alignment(TCGContext *s, bool is_ld, TCGReg addrlo,
1597 TCGReg addrhi, unsigned a_bits)
1599 unsigned a_mask = (1 << a_bits) - 1;
1600 TCGLabelQemuLdst *label = new_ldst_label(s);
1602 label->is_ld = is_ld;
1603 label->addrlo_reg = addrlo;
1604 label->addrhi_reg = addrhi;
1606 /* We are expecting a_bits to max out at 7, and can easily support 8. */
1607 tcg_debug_assert(a_mask <= 0xff);
1608 /* tst addr, #mask */
1609 tcg_out_dat_imm(s, COND_AL, ARITH_TST, 0, addrlo, a_mask);
1611 /* blne slow_path */
1612 label->label_ptr[0] = s->code_ptr;
1613 tcg_out_bl_imm(s, COND_NE, 0);
1615 label->raddr = tcg_splitwx_to_rx(s->code_ptr);
1618 static bool tcg_out_fail_alignment(TCGContext *s, TCGLabelQemuLdst *l)
1620 if (!reloc_pc24(l->label_ptr[0], tcg_splitwx_to_rx(s->code_ptr))) {
1624 if (TARGET_LONG_BITS == 64) {
1625 /* 64-bit target address is aligned into R2:R3. */
1626 if (l->addrhi_reg != TCG_REG_R2) {
1627 tcg_out_mov(s, TCG_TYPE_I32, TCG_REG_R2, l->addrlo_reg);
1628 tcg_out_mov(s, TCG_TYPE_I32, TCG_REG_R3, l->addrhi_reg);
1629 } else if (l->addrlo_reg != TCG_REG_R3) {
1630 tcg_out_mov(s, TCG_TYPE_I32, TCG_REG_R3, l->addrhi_reg);
1631 tcg_out_mov(s, TCG_TYPE_I32, TCG_REG_R2, l->addrlo_reg);
1633 tcg_out_mov(s, TCG_TYPE_I32, TCG_REG_R1, TCG_REG_R2);
1634 tcg_out_mov(s, TCG_TYPE_I32, TCG_REG_R2, TCG_REG_R3);
1635 tcg_out_mov(s, TCG_TYPE_I32, TCG_REG_R3, TCG_REG_R1);
1638 tcg_out_mov(s, TCG_TYPE_I32, TCG_REG_R1, l->addrlo_reg);
1640 tcg_out_mov(s, TCG_TYPE_PTR, TCG_REG_R0, TCG_AREG0);
1643 * Tail call to the helper, with the return address back inline,
1644 * just for the clarity of the debugging traceback -- the helper
1645 * cannot return. We have used BLNE to arrive here, so LR is
1648 tcg_out_goto(s, COND_AL, (const void *)
1649 (l->is_ld ? helper_unaligned_ld : helper_unaligned_st));
1653 static bool tcg_out_qemu_ld_slow_path(TCGContext *s, TCGLabelQemuLdst *l)
1655 return tcg_out_fail_alignment(s, l);
1658 static bool tcg_out_qemu_st_slow_path(TCGContext *s, TCGLabelQemuLdst *l)
1660 return tcg_out_fail_alignment(s, l);
1662 #endif /* SOFTMMU */
1664 static void tcg_out_qemu_ld_index(TCGContext *s, MemOp opc,
1665 TCGReg datalo, TCGReg datahi,
1666 TCGReg addrlo, TCGReg addend,
1667 bool scratch_addend)
1669 /* Byte swapping is left to middle-end expansion. */
1670 tcg_debug_assert((opc & MO_BSWAP) == 0);
1672 switch (opc & MO_SSIZE) {
1674 tcg_out_ld8_r(s, COND_AL, datalo, addrlo, addend);
1677 tcg_out_ld8s_r(s, COND_AL, datalo, addrlo, addend);
1680 tcg_out_ld16u_r(s, COND_AL, datalo, addrlo, addend);
1683 tcg_out_ld16s_r(s, COND_AL, datalo, addrlo, addend);
1686 tcg_out_ld32_r(s, COND_AL, datalo, addrlo, addend);
1689 /* LDRD requires alignment; double-check that. */
1690 if (get_alignment_bits(opc) >= MO_64
1691 && (datalo & 1) == 0 && datahi == datalo + 1) {
1693 * Rm (the second address op) must not overlap Rt or Rt + 1.
1694 * Since datalo is aligned, we can simplify the test via alignment.
1695 * Flip the two address arguments if that works.
1697 if ((addend & ~1) != datalo) {
1698 tcg_out_ldrd_r(s, COND_AL, datalo, addrlo, addend);
1701 if ((addrlo & ~1) != datalo) {
1702 tcg_out_ldrd_r(s, COND_AL, datalo, addend, addrlo);
1706 if (scratch_addend) {
1707 tcg_out_ld32_rwb(s, COND_AL, datalo, addend, addrlo);
1708 tcg_out_ld32_12(s, COND_AL, datahi, addend, 4);
1710 tcg_out_dat_reg(s, COND_AL, ARITH_ADD, TCG_REG_TMP,
1711 addend, addrlo, SHIFT_IMM_LSL(0));
1712 tcg_out_ld32_12(s, COND_AL, datalo, TCG_REG_TMP, 0);
1713 tcg_out_ld32_12(s, COND_AL, datahi, TCG_REG_TMP, 4);
1717 g_assert_not_reached();
1721 #ifndef CONFIG_SOFTMMU
1722 static void tcg_out_qemu_ld_direct(TCGContext *s, MemOp opc, TCGReg datalo,
1723 TCGReg datahi, TCGReg addrlo)
1725 /* Byte swapping is left to middle-end expansion. */
1726 tcg_debug_assert((opc & MO_BSWAP) == 0);
1728 switch (opc & MO_SSIZE) {
1730 tcg_out_ld8_12(s, COND_AL, datalo, addrlo, 0);
1733 tcg_out_ld8s_8(s, COND_AL, datalo, addrlo, 0);
1736 tcg_out_ld16u_8(s, COND_AL, datalo, addrlo, 0);
1739 tcg_out_ld16s_8(s, COND_AL, datalo, addrlo, 0);
1742 tcg_out_ld32_12(s, COND_AL, datalo, addrlo, 0);
1745 /* LDRD requires alignment; double-check that. */
1746 if (get_alignment_bits(opc) >= MO_64
1747 && (datalo & 1) == 0 && datahi == datalo + 1) {
1748 tcg_out_ldrd_8(s, COND_AL, datalo, addrlo, 0);
1749 } else if (datalo == addrlo) {
1750 tcg_out_ld32_12(s, COND_AL, datahi, addrlo, 4);
1751 tcg_out_ld32_12(s, COND_AL, datalo, addrlo, 0);
1753 tcg_out_ld32_12(s, COND_AL, datalo, addrlo, 0);
1754 tcg_out_ld32_12(s, COND_AL, datahi, addrlo, 4);
1758 g_assert_not_reached();
1763 static void tcg_out_qemu_ld(TCGContext *s, const TCGArg *args, bool is64)
1765 TCGReg addrlo, datalo, datahi, addrhi __attribute__((unused));
1768 #ifdef CONFIG_SOFTMMU
1771 tcg_insn_unit *label_ptr;
1777 datahi = (is64 ? *args++ : 0);
1779 addrhi = (TARGET_LONG_BITS == 64 ? *args++ : 0);
1781 opc = get_memop(oi);
1783 #ifdef CONFIG_SOFTMMU
1784 mem_index = get_mmuidx(oi);
1785 addend = tcg_out_tlb_read(s, addrlo, addrhi, opc, mem_index, 1);
1787 /* This a conditional BL only to load a pointer within this opcode into LR
1788 for the slow path. We will not be using the value for a tail call. */
1789 label_ptr = s->code_ptr;
1790 tcg_out_bl_imm(s, COND_NE, 0);
1792 tcg_out_qemu_ld_index(s, opc, datalo, datahi, addrlo, addend, true);
1794 add_qemu_ldst_label(s, true, oi, datalo, datahi, addrlo, addrhi,
1795 s->code_ptr, label_ptr);
1796 #else /* !CONFIG_SOFTMMU */
1797 a_bits = get_alignment_bits(opc);
1799 tcg_out_test_alignment(s, true, addrlo, addrhi, a_bits);
1802 tcg_out_qemu_ld_index(s, opc, datalo, datahi,
1803 addrlo, TCG_REG_GUEST_BASE, false);
1805 tcg_out_qemu_ld_direct(s, opc, datalo, datahi, addrlo);
1810 static void tcg_out_qemu_st_index(TCGContext *s, ARMCond cond, MemOp opc,
1811 TCGReg datalo, TCGReg datahi,
1812 TCGReg addrlo, TCGReg addend,
1813 bool scratch_addend)
1815 /* Byte swapping is left to middle-end expansion. */
1816 tcg_debug_assert((opc & MO_BSWAP) == 0);
1818 switch (opc & MO_SIZE) {
1820 tcg_out_st8_r(s, cond, datalo, addrlo, addend);
1823 tcg_out_st16_r(s, cond, datalo, addrlo, addend);
1826 tcg_out_st32_r(s, cond, datalo, addrlo, addend);
1829 /* STRD requires alignment; double-check that. */
1830 if (get_alignment_bits(opc) >= MO_64
1831 && (datalo & 1) == 0 && datahi == datalo + 1) {
1832 tcg_out_strd_r(s, cond, datalo, addrlo, addend);
1833 } else if (scratch_addend) {
1834 tcg_out_st32_rwb(s, cond, datalo, addend, addrlo);
1835 tcg_out_st32_12(s, cond, datahi, addend, 4);
1837 tcg_out_dat_reg(s, cond, ARITH_ADD, TCG_REG_TMP,
1838 addend, addrlo, SHIFT_IMM_LSL(0));
1839 tcg_out_st32_12(s, cond, datalo, TCG_REG_TMP, 0);
1840 tcg_out_st32_12(s, cond, datahi, TCG_REG_TMP, 4);
1844 g_assert_not_reached();
1848 #ifndef CONFIG_SOFTMMU
1849 static void tcg_out_qemu_st_direct(TCGContext *s, MemOp opc, TCGReg datalo,
1850 TCGReg datahi, TCGReg addrlo)
1852 /* Byte swapping is left to middle-end expansion. */
1853 tcg_debug_assert((opc & MO_BSWAP) == 0);
1855 switch (opc & MO_SIZE) {
1857 tcg_out_st8_12(s, COND_AL, datalo, addrlo, 0);
1860 tcg_out_st16_8(s, COND_AL, datalo, addrlo, 0);
1863 tcg_out_st32_12(s, COND_AL, datalo, addrlo, 0);
1866 /* STRD requires alignment; double-check that. */
1867 if (get_alignment_bits(opc) >= MO_64
1868 && (datalo & 1) == 0 && datahi == datalo + 1) {
1869 tcg_out_strd_8(s, COND_AL, datalo, addrlo, 0);
1871 tcg_out_st32_12(s, COND_AL, datalo, addrlo, 0);
1872 tcg_out_st32_12(s, COND_AL, datahi, addrlo, 4);
1876 g_assert_not_reached();
1881 static void tcg_out_qemu_st(TCGContext *s, const TCGArg *args, bool is64)
1883 TCGReg addrlo, datalo, datahi, addrhi __attribute__((unused));
1886 #ifdef CONFIG_SOFTMMU
1889 tcg_insn_unit *label_ptr;
1895 datahi = (is64 ? *args++ : 0);
1897 addrhi = (TARGET_LONG_BITS == 64 ? *args++ : 0);
1899 opc = get_memop(oi);
1901 #ifdef CONFIG_SOFTMMU
1902 mem_index = get_mmuidx(oi);
1903 addend = tcg_out_tlb_read(s, addrlo, addrhi, opc, mem_index, 0);
1905 tcg_out_qemu_st_index(s, COND_EQ, opc, datalo, datahi,
1906 addrlo, addend, true);
1908 /* The conditional call must come last, as we're going to return here. */
1909 label_ptr = s->code_ptr;
1910 tcg_out_bl_imm(s, COND_NE, 0);
1912 add_qemu_ldst_label(s, false, oi, datalo, datahi, addrlo, addrhi,
1913 s->code_ptr, label_ptr);
1914 #else /* !CONFIG_SOFTMMU */
1915 a_bits = get_alignment_bits(opc);
1917 tcg_out_test_alignment(s, false, addrlo, addrhi, a_bits);
1920 tcg_out_qemu_st_index(s, COND_AL, opc, datalo, datahi,
1921 addrlo, TCG_REG_GUEST_BASE, false);
1923 tcg_out_qemu_st_direct(s, opc, datalo, datahi, addrlo);
1928 static void tcg_out_epilogue(TCGContext *s);
1930 static void tcg_out_op(TCGContext *s, TCGOpcode opc,
1931 const TCGArg args[TCG_MAX_OP_ARGS],
1932 const int const_args[TCG_MAX_OP_ARGS])
1934 TCGArg a0, a1, a2, a3, a4, a5;
1938 case INDEX_op_exit_tb:
1939 tcg_out_movi(s, TCG_TYPE_PTR, TCG_REG_R0, args[0]);
1940 tcg_out_epilogue(s);
1942 case INDEX_op_goto_tb:
1944 /* Indirect jump method */
1945 intptr_t ptr, dif, dil;
1946 TCGReg base = TCG_REG_PC;
1948 tcg_debug_assert(s->tb_jmp_insn_offset == 0);
1949 ptr = (intptr_t)tcg_splitwx_to_rx(s->tb_jmp_target_addr + args[0]);
1950 dif = tcg_pcrel_diff(s, (void *)ptr) - 8;
1951 dil = sextract32(dif, 0, 12);
1953 /* The TB is close, but outside the 12 bits addressable by
1954 the load. We can extend this to 20 bits with a sub of a
1955 shifted immediate from pc. In the vastly unlikely event
1956 the code requires more than 1MB, we'll use 2 insns and
1959 tcg_out_movi32(s, COND_AL, base, ptr - dil);
1961 tcg_out_ld32_12(s, COND_AL, TCG_REG_PC, base, dil);
1962 set_jmp_reset_offset(s, args[0]);
1965 case INDEX_op_goto_ptr:
1966 tcg_out_b_reg(s, COND_AL, args[0]);
1969 tcg_out_goto_label(s, COND_AL, arg_label(args[0]));
1972 case INDEX_op_ld8u_i32:
1973 tcg_out_ld8u(s, COND_AL, args[0], args[1], args[2]);
1975 case INDEX_op_ld8s_i32:
1976 tcg_out_ld8s(s, COND_AL, args[0], args[1], args[2]);
1978 case INDEX_op_ld16u_i32:
1979 tcg_out_ld16u(s, COND_AL, args[0], args[1], args[2]);
1981 case INDEX_op_ld16s_i32:
1982 tcg_out_ld16s(s, COND_AL, args[0], args[1], args[2]);
1984 case INDEX_op_ld_i32:
1985 tcg_out_ld32u(s, COND_AL, args[0], args[1], args[2]);
1987 case INDEX_op_st8_i32:
1988 tcg_out_st8(s, COND_AL, args[0], args[1], args[2]);
1990 case INDEX_op_st16_i32:
1991 tcg_out_st16(s, COND_AL, args[0], args[1], args[2]);
1993 case INDEX_op_st_i32:
1994 tcg_out_st32(s, COND_AL, args[0], args[1], args[2]);
1997 case INDEX_op_movcond_i32:
1998 /* Constraints mean that v2 is always in the same register as dest,
1999 * so we only need to do "if condition passed, move v1 to dest".
2001 tcg_out_dat_rIN(s, COND_AL, ARITH_CMP, ARITH_CMN, 0,
2002 args[1], args[2], const_args[2]);
2003 tcg_out_dat_rIK(s, tcg_cond_to_arm_cond[args[5]], ARITH_MOV,
2004 ARITH_MVN, args[0], 0, args[3], const_args[3]);
2006 case INDEX_op_add_i32:
2007 tcg_out_dat_rIN(s, COND_AL, ARITH_ADD, ARITH_SUB,
2008 args[0], args[1], args[2], const_args[2]);
2010 case INDEX_op_sub_i32:
2011 if (const_args[1]) {
2012 if (const_args[2]) {
2013 tcg_out_movi32(s, COND_AL, args[0], args[1] - args[2]);
2015 tcg_out_dat_rI(s, COND_AL, ARITH_RSB,
2016 args[0], args[2], args[1], 1);
2019 tcg_out_dat_rIN(s, COND_AL, ARITH_SUB, ARITH_ADD,
2020 args[0], args[1], args[2], const_args[2]);
2023 case INDEX_op_and_i32:
2024 tcg_out_dat_rIK(s, COND_AL, ARITH_AND, ARITH_BIC,
2025 args[0], args[1], args[2], const_args[2]);
2027 case INDEX_op_andc_i32:
2028 tcg_out_dat_rIK(s, COND_AL, ARITH_BIC, ARITH_AND,
2029 args[0], args[1], args[2], const_args[2]);
2031 case INDEX_op_or_i32:
2034 case INDEX_op_xor_i32:
2038 tcg_out_dat_rI(s, COND_AL, c, args[0], args[1], args[2], const_args[2]);
2040 case INDEX_op_add2_i32:
2041 a0 = args[0], a1 = args[1], a2 = args[2];
2042 a3 = args[3], a4 = args[4], a5 = args[5];
2043 if (a0 == a3 || (a0 == a5 && !const_args[5])) {
2046 tcg_out_dat_rIN(s, COND_AL, ARITH_ADD | TO_CPSR, ARITH_SUB | TO_CPSR,
2047 a0, a2, a4, const_args[4]);
2048 tcg_out_dat_rIK(s, COND_AL, ARITH_ADC, ARITH_SBC,
2049 a1, a3, a5, const_args[5]);
2050 tcg_out_mov_reg(s, COND_AL, args[0], a0);
2052 case INDEX_op_sub2_i32:
2053 a0 = args[0], a1 = args[1], a2 = args[2];
2054 a3 = args[3], a4 = args[4], a5 = args[5];
2055 if ((a0 == a3 && !const_args[3]) || (a0 == a5 && !const_args[5])) {
2058 if (const_args[2]) {
2059 if (const_args[4]) {
2060 tcg_out_movi32(s, COND_AL, a0, a4);
2063 tcg_out_dat_rI(s, COND_AL, ARITH_RSB | TO_CPSR, a0, a4, a2, 1);
2065 tcg_out_dat_rIN(s, COND_AL, ARITH_SUB | TO_CPSR,
2066 ARITH_ADD | TO_CPSR, a0, a2, a4, const_args[4]);
2068 if (const_args[3]) {
2069 if (const_args[5]) {
2070 tcg_out_movi32(s, COND_AL, a1, a5);
2073 tcg_out_dat_rI(s, COND_AL, ARITH_RSC, a1, a5, a3, 1);
2075 tcg_out_dat_rIK(s, COND_AL, ARITH_SBC, ARITH_ADC,
2076 a1, a3, a5, const_args[5]);
2078 tcg_out_mov_reg(s, COND_AL, args[0], a0);
2080 case INDEX_op_neg_i32:
2081 tcg_out_dat_imm(s, COND_AL, ARITH_RSB, args[0], args[1], 0);
2083 case INDEX_op_not_i32:
2084 tcg_out_dat_reg(s, COND_AL,
2085 ARITH_MVN, args[0], 0, args[1], SHIFT_IMM_LSL(0));
2087 case INDEX_op_mul_i32:
2088 tcg_out_mul32(s, COND_AL, args[0], args[1], args[2]);
2090 case INDEX_op_mulu2_i32:
2091 tcg_out_umull32(s, COND_AL, args[0], args[1], args[2], args[3]);
2093 case INDEX_op_muls2_i32:
2094 tcg_out_smull32(s, COND_AL, args[0], args[1], args[2], args[3]);
2096 /* XXX: Perhaps args[2] & 0x1f is wrong */
2097 case INDEX_op_shl_i32:
2099 SHIFT_IMM_LSL(args[2] & 0x1f) : SHIFT_REG_LSL(args[2]);
2101 case INDEX_op_shr_i32:
2102 c = const_args[2] ? (args[2] & 0x1f) ? SHIFT_IMM_LSR(args[2] & 0x1f) :
2103 SHIFT_IMM_LSL(0) : SHIFT_REG_LSR(args[2]);
2105 case INDEX_op_sar_i32:
2106 c = const_args[2] ? (args[2] & 0x1f) ? SHIFT_IMM_ASR(args[2] & 0x1f) :
2107 SHIFT_IMM_LSL(0) : SHIFT_REG_ASR(args[2]);
2109 case INDEX_op_rotr_i32:
2110 c = const_args[2] ? (args[2] & 0x1f) ? SHIFT_IMM_ROR(args[2] & 0x1f) :
2111 SHIFT_IMM_LSL(0) : SHIFT_REG_ROR(args[2]);
2114 tcg_out_dat_reg(s, COND_AL, ARITH_MOV, args[0], 0, args[1], c);
2117 case INDEX_op_rotl_i32:
2118 if (const_args[2]) {
2119 tcg_out_dat_reg(s, COND_AL, ARITH_MOV, args[0], 0, args[1],
2120 ((0x20 - args[2]) & 0x1f) ?
2121 SHIFT_IMM_ROR((0x20 - args[2]) & 0x1f) :
2124 tcg_out_dat_imm(s, COND_AL, ARITH_RSB, TCG_REG_TMP, args[2], 0x20);
2125 tcg_out_dat_reg(s, COND_AL, ARITH_MOV, args[0], 0, args[1],
2126 SHIFT_REG_ROR(TCG_REG_TMP));
2130 case INDEX_op_ctz_i32:
2131 tcg_out_dat_reg(s, COND_AL, INSN_RBIT, TCG_REG_TMP, 0, args[1], 0);
2135 case INDEX_op_clz_i32:
2141 if (c && a2 == 32) {
2142 tcg_out_dat_reg(s, COND_AL, INSN_CLZ, a0, 0, a1, 0);
2145 tcg_out_dat_imm(s, COND_AL, ARITH_CMP, 0, a1, 0);
2146 tcg_out_dat_reg(s, COND_NE, INSN_CLZ, a0, 0, a1, 0);
2147 if (c || a0 != a2) {
2148 tcg_out_dat_rIK(s, COND_EQ, ARITH_MOV, ARITH_MVN, a0, 0, a2, c);
2152 case INDEX_op_brcond_i32:
2153 tcg_out_dat_rIN(s, COND_AL, ARITH_CMP, ARITH_CMN, 0,
2154 args[0], args[1], const_args[1]);
2155 tcg_out_goto_label(s, tcg_cond_to_arm_cond[args[2]],
2156 arg_label(args[3]));
2158 case INDEX_op_setcond_i32:
2159 tcg_out_dat_rIN(s, COND_AL, ARITH_CMP, ARITH_CMN, 0,
2160 args[1], args[2], const_args[2]);
2161 tcg_out_dat_imm(s, tcg_cond_to_arm_cond[args[3]],
2162 ARITH_MOV, args[0], 0, 1);
2163 tcg_out_dat_imm(s, tcg_cond_to_arm_cond[tcg_invert_cond(args[3])],
2164 ARITH_MOV, args[0], 0, 0);
2167 case INDEX_op_brcond2_i32:
2168 c = tcg_out_cmp2(s, args, const_args);
2169 tcg_out_goto_label(s, tcg_cond_to_arm_cond[c], arg_label(args[5]));
2171 case INDEX_op_setcond2_i32:
2172 c = tcg_out_cmp2(s, args + 1, const_args + 1);
2173 tcg_out_dat_imm(s, tcg_cond_to_arm_cond[c], ARITH_MOV, args[0], 0, 1);
2174 tcg_out_dat_imm(s, tcg_cond_to_arm_cond[tcg_invert_cond(c)],
2175 ARITH_MOV, args[0], 0, 0);
2178 case INDEX_op_qemu_ld_i32:
2179 tcg_out_qemu_ld(s, args, 0);
2181 case INDEX_op_qemu_ld_i64:
2182 tcg_out_qemu_ld(s, args, 1);
2184 case INDEX_op_qemu_st_i32:
2185 tcg_out_qemu_st(s, args, 0);
2187 case INDEX_op_qemu_st_i64:
2188 tcg_out_qemu_st(s, args, 1);
2191 case INDEX_op_bswap16_i32:
2192 tcg_out_bswap16(s, COND_AL, args[0], args[1], args[2]);
2194 case INDEX_op_bswap32_i32:
2195 tcg_out_bswap32(s, COND_AL, args[0], args[1]);
2198 case INDEX_op_ext8s_i32:
2199 tcg_out_ext8s(s, COND_AL, args[0], args[1]);
2201 case INDEX_op_ext16s_i32:
2202 tcg_out_ext16s(s, COND_AL, args[0], args[1]);
2204 case INDEX_op_ext16u_i32:
2205 tcg_out_ext16u(s, COND_AL, args[0], args[1]);
2208 case INDEX_op_deposit_i32:
2209 tcg_out_deposit(s, COND_AL, args[0], args[2],
2210 args[3], args[4], const_args[2]);
2212 case INDEX_op_extract_i32:
2213 tcg_out_extract(s, COND_AL, args[0], args[1], args[2], args[3]);
2215 case INDEX_op_sextract_i32:
2216 tcg_out_sextract(s, COND_AL, args[0], args[1], args[2], args[3]);
2218 case INDEX_op_extract2_i32:
2219 /* ??? These optimization vs zero should be generic. */
2220 /* ??? But we can't substitute 2 for 1 in the opcode stream yet. */
2221 if (const_args[1]) {
2222 if (const_args[2]) {
2223 tcg_out_movi(s, TCG_TYPE_REG, args[0], 0);
2225 tcg_out_dat_reg(s, COND_AL, ARITH_MOV, args[0], 0,
2226 args[2], SHIFT_IMM_LSL(32 - args[3]));
2228 } else if (const_args[2]) {
2229 tcg_out_dat_reg(s, COND_AL, ARITH_MOV, args[0], 0,
2230 args[1], SHIFT_IMM_LSR(args[3]));
2232 /* We can do extract2 in 2 insns, vs the 3 required otherwise. */
2233 tcg_out_dat_reg(s, COND_AL, ARITH_MOV, TCG_REG_TMP, 0,
2234 args[2], SHIFT_IMM_LSL(32 - args[3]));
2235 tcg_out_dat_reg(s, COND_AL, ARITH_ORR, args[0], TCG_REG_TMP,
2236 args[1], SHIFT_IMM_LSR(args[3]));
2240 case INDEX_op_div_i32:
2241 tcg_out_sdiv(s, COND_AL, args[0], args[1], args[2]);
2243 case INDEX_op_divu_i32:
2244 tcg_out_udiv(s, COND_AL, args[0], args[1], args[2]);
2248 tcg_out_mb(s, args[0]);
2251 case INDEX_op_mov_i32: /* Always emitted via tcg_out_mov. */
2252 case INDEX_op_call: /* Always emitted via tcg_out_call. */
2258 static TCGConstraintSetIndex tcg_target_op_def(TCGOpcode op)
2261 case INDEX_op_goto_ptr:
2264 case INDEX_op_ld8u_i32:
2265 case INDEX_op_ld8s_i32:
2266 case INDEX_op_ld16u_i32:
2267 case INDEX_op_ld16s_i32:
2268 case INDEX_op_ld_i32:
2269 case INDEX_op_neg_i32:
2270 case INDEX_op_not_i32:
2271 case INDEX_op_bswap16_i32:
2272 case INDEX_op_bswap32_i32:
2273 case INDEX_op_ext8s_i32:
2274 case INDEX_op_ext16s_i32:
2275 case INDEX_op_ext16u_i32:
2276 case INDEX_op_extract_i32:
2277 case INDEX_op_sextract_i32:
2278 return C_O1_I1(r, r);
2280 case INDEX_op_st8_i32:
2281 case INDEX_op_st16_i32:
2282 case INDEX_op_st_i32:
2283 return C_O0_I2(r, r);
2285 case INDEX_op_add_i32:
2286 case INDEX_op_sub_i32:
2287 case INDEX_op_setcond_i32:
2288 return C_O1_I2(r, r, rIN);
2290 case INDEX_op_and_i32:
2291 case INDEX_op_andc_i32:
2292 case INDEX_op_clz_i32:
2293 case INDEX_op_ctz_i32:
2294 return C_O1_I2(r, r, rIK);
2296 case INDEX_op_mul_i32:
2297 case INDEX_op_div_i32:
2298 case INDEX_op_divu_i32:
2299 return C_O1_I2(r, r, r);
2301 case INDEX_op_mulu2_i32:
2302 case INDEX_op_muls2_i32:
2303 return C_O2_I2(r, r, r, r);
2305 case INDEX_op_or_i32:
2306 case INDEX_op_xor_i32:
2307 return C_O1_I2(r, r, rI);
2309 case INDEX_op_shl_i32:
2310 case INDEX_op_shr_i32:
2311 case INDEX_op_sar_i32:
2312 case INDEX_op_rotl_i32:
2313 case INDEX_op_rotr_i32:
2314 return C_O1_I2(r, r, ri);
2316 case INDEX_op_brcond_i32:
2317 return C_O0_I2(r, rIN);
2318 case INDEX_op_deposit_i32:
2319 return C_O1_I2(r, 0, rZ);
2320 case INDEX_op_extract2_i32:
2321 return C_O1_I2(r, rZ, rZ);
2322 case INDEX_op_movcond_i32:
2323 return C_O1_I4(r, r, rIN, rIK, 0);
2324 case INDEX_op_add2_i32:
2325 return C_O2_I4(r, r, r, r, rIN, rIK);
2326 case INDEX_op_sub2_i32:
2327 return C_O2_I4(r, r, rI, rI, rIN, rIK);
2328 case INDEX_op_brcond2_i32:
2329 return C_O0_I4(r, r, rI, rI);
2330 case INDEX_op_setcond2_i32:
2331 return C_O1_I4(r, r, r, rI, rI);
2333 case INDEX_op_qemu_ld_i32:
2334 return TARGET_LONG_BITS == 32 ? C_O1_I1(r, l) : C_O1_I2(r, l, l);
2335 case INDEX_op_qemu_ld_i64:
2336 return TARGET_LONG_BITS == 32 ? C_O2_I1(r, r, l) : C_O2_I2(r, r, l, l);
2337 case INDEX_op_qemu_st_i32:
2338 return TARGET_LONG_BITS == 32 ? C_O0_I2(s, s) : C_O0_I3(s, s, s);
2339 case INDEX_op_qemu_st_i64:
2340 return TARGET_LONG_BITS == 32 ? C_O0_I3(s, s, s) : C_O0_I4(s, s, s, s);
2342 case INDEX_op_st_vec:
2343 return C_O0_I2(w, r);
2344 case INDEX_op_ld_vec:
2345 case INDEX_op_dupm_vec:
2346 return C_O1_I1(w, r);
2347 case INDEX_op_dup_vec:
2348 return C_O1_I1(w, wr);
2349 case INDEX_op_abs_vec:
2350 case INDEX_op_neg_vec:
2351 case INDEX_op_not_vec:
2352 case INDEX_op_shli_vec:
2353 case INDEX_op_shri_vec:
2354 case INDEX_op_sari_vec:
2355 return C_O1_I1(w, w);
2356 case INDEX_op_dup2_vec:
2357 case INDEX_op_add_vec:
2358 case INDEX_op_mul_vec:
2359 case INDEX_op_smax_vec:
2360 case INDEX_op_smin_vec:
2361 case INDEX_op_ssadd_vec:
2362 case INDEX_op_sssub_vec:
2363 case INDEX_op_sub_vec:
2364 case INDEX_op_umax_vec:
2365 case INDEX_op_umin_vec:
2366 case INDEX_op_usadd_vec:
2367 case INDEX_op_ussub_vec:
2368 case INDEX_op_xor_vec:
2369 case INDEX_op_arm_sshl_vec:
2370 case INDEX_op_arm_ushl_vec:
2371 return C_O1_I2(w, w, w);
2372 case INDEX_op_arm_sli_vec:
2373 return C_O1_I2(w, 0, w);
2374 case INDEX_op_or_vec:
2375 case INDEX_op_andc_vec:
2376 return C_O1_I2(w, w, wO);
2377 case INDEX_op_and_vec:
2378 case INDEX_op_orc_vec:
2379 return C_O1_I2(w, w, wV);
2380 case INDEX_op_cmp_vec:
2381 return C_O1_I2(w, w, wZ);
2382 case INDEX_op_bitsel_vec:
2383 return C_O1_I3(w, w, w, w);
2385 g_assert_not_reached();
2389 static void tcg_target_init(TCGContext *s)
2392 * Only probe for the platform and capabilities if we haven't already
2393 * determined maximum values at compile time.
2395 #if !defined(use_idiv_instructions) || !defined(use_neon_instructions)
2397 unsigned long hwcap = qemu_getauxval(AT_HWCAP);
2398 #ifndef use_idiv_instructions
2399 use_idiv_instructions = (hwcap & HWCAP_ARM_IDIVA) != 0;
2401 #ifndef use_neon_instructions
2402 use_neon_instructions = (hwcap & HWCAP_ARM_NEON) != 0;
2407 if (__ARM_ARCH < 7) {
2408 const char *pl = (const char *)qemu_getauxval(AT_PLATFORM);
2409 if (pl != NULL && pl[0] == 'v' && pl[1] >= '4' && pl[1] <= '9') {
2410 arm_arch = pl[1] - '0';
2414 error_report("TCG: ARMv%d is unsupported; exiting", arm_arch);
2419 tcg_target_available_regs[TCG_TYPE_I32] = ALL_GENERAL_REGS;
2421 tcg_target_call_clobber_regs = 0;
2422 tcg_regset_set_reg(tcg_target_call_clobber_regs, TCG_REG_R0);
2423 tcg_regset_set_reg(tcg_target_call_clobber_regs, TCG_REG_R1);
2424 tcg_regset_set_reg(tcg_target_call_clobber_regs, TCG_REG_R2);
2425 tcg_regset_set_reg(tcg_target_call_clobber_regs, TCG_REG_R3);
2426 tcg_regset_set_reg(tcg_target_call_clobber_regs, TCG_REG_R12);
2427 tcg_regset_set_reg(tcg_target_call_clobber_regs, TCG_REG_R14);
2429 if (use_neon_instructions) {
2430 tcg_target_available_regs[TCG_TYPE_V64] = ALL_VECTOR_REGS;
2431 tcg_target_available_regs[TCG_TYPE_V128] = ALL_VECTOR_REGS;
2433 tcg_regset_set_reg(tcg_target_call_clobber_regs, TCG_REG_Q0);
2434 tcg_regset_set_reg(tcg_target_call_clobber_regs, TCG_REG_Q1);
2435 tcg_regset_set_reg(tcg_target_call_clobber_regs, TCG_REG_Q2);
2436 tcg_regset_set_reg(tcg_target_call_clobber_regs, TCG_REG_Q3);
2437 tcg_regset_set_reg(tcg_target_call_clobber_regs, TCG_REG_Q8);
2438 tcg_regset_set_reg(tcg_target_call_clobber_regs, TCG_REG_Q9);
2439 tcg_regset_set_reg(tcg_target_call_clobber_regs, TCG_REG_Q10);
2440 tcg_regset_set_reg(tcg_target_call_clobber_regs, TCG_REG_Q11);
2441 tcg_regset_set_reg(tcg_target_call_clobber_regs, TCG_REG_Q12);
2442 tcg_regset_set_reg(tcg_target_call_clobber_regs, TCG_REG_Q13);
2443 tcg_regset_set_reg(tcg_target_call_clobber_regs, TCG_REG_Q14);
2444 tcg_regset_set_reg(tcg_target_call_clobber_regs, TCG_REG_Q15);
2447 s->reserved_regs = 0;
2448 tcg_regset_set_reg(s->reserved_regs, TCG_REG_CALL_STACK);
2449 tcg_regset_set_reg(s->reserved_regs, TCG_REG_TMP);
2450 tcg_regset_set_reg(s->reserved_regs, TCG_REG_PC);
2451 tcg_regset_set_reg(s->reserved_regs, TCG_VEC_TMP);
2454 static void tcg_out_ld(TCGContext *s, TCGType type, TCGReg arg,
2455 TCGReg arg1, intptr_t arg2)
2459 tcg_out_ld32u(s, COND_AL, arg, arg1, arg2);
2462 /* regs 1; size 8; align 8 */
2463 tcg_out_vldst(s, INSN_VLD1 | 0x7d0, arg, arg1, arg2);
2467 * We have only 8-byte alignment for the stack per the ABI.
2468 * Rather than dynamically re-align the stack, it's easier
2469 * to simply not request alignment beyond that. So:
2470 * regs 2; size 8; align 8
2472 tcg_out_vldst(s, INSN_VLD1 | 0xad0, arg, arg1, arg2);
2475 g_assert_not_reached();
2479 static void tcg_out_st(TCGContext *s, TCGType type, TCGReg arg,
2480 TCGReg arg1, intptr_t arg2)
2484 tcg_out_st32(s, COND_AL, arg, arg1, arg2);
2487 /* regs 1; size 8; align 8 */
2488 tcg_out_vldst(s, INSN_VST1 | 0x7d0, arg, arg1, arg2);
2491 /* See tcg_out_ld re alignment: regs 2; size 8; align 8 */
2492 tcg_out_vldst(s, INSN_VST1 | 0xad0, arg, arg1, arg2);
2495 g_assert_not_reached();
2499 static bool tcg_out_sti(TCGContext *s, TCGType type, TCGArg val,
2500 TCGReg base, intptr_t ofs)
2505 static bool tcg_out_mov(TCGContext *s, TCGType type, TCGReg ret, TCGReg arg)
2512 if (ret < TCG_REG_Q0 && arg < TCG_REG_Q0) {
2513 tcg_out_mov_reg(s, COND_AL, ret, arg);
2520 /* "VMOV D,N" is an alias for "VORR D,N,N". */
2521 tcg_out_vreg3(s, INSN_VORR, type - TCG_TYPE_V64, 0, ret, arg, arg);
2525 g_assert_not_reached();
2529 static void tcg_out_movi(TCGContext *s, TCGType type,
2530 TCGReg ret, tcg_target_long arg)
2532 tcg_debug_assert(type == TCG_TYPE_I32);
2533 tcg_debug_assert(ret < TCG_REG_Q0);
2534 tcg_out_movi32(s, COND_AL, ret, arg);
2537 /* Type is always V128, with I64 elements. */
2538 static void tcg_out_dup2_vec(TCGContext *s, TCGReg rd, TCGReg rl, TCGReg rh)
2540 /* Move high element into place first. */
2542 tcg_out_vreg3(s, INSN_VORR | (1 << 12), 0, 0, rd, rh, rh);
2543 /* Move low element into place; tcg_out_mov will check for nop. */
2544 tcg_out_mov(s, TCG_TYPE_V64, rd, rl);
2547 static bool tcg_out_dup_vec(TCGContext *s, TCGType type, unsigned vece,
2548 TCGReg rd, TCGReg rs)
2550 int q = type - TCG_TYPE_V64;
2552 if (vece == MO_64) {
2553 if (type == TCG_TYPE_V128) {
2554 tcg_out_dup2_vec(s, rd, rs, rs);
2556 tcg_out_mov(s, TCG_TYPE_V64, rd, rs);
2558 } else if (rs < TCG_REG_Q0) {
2559 int b = (vece == MO_8);
2560 int e = (vece == MO_16);
2561 tcg_out32(s, INSN_VDUP_G | (b << 22) | (q << 21) | (e << 5) |
2562 encode_vn(rd) | (rs << 12));
2564 int imm4 = 1 << vece;
2565 tcg_out32(s, INSN_VDUP_S | (imm4 << 16) | (q << 6) |
2566 encode_vd(rd) | encode_vm(rs));
2571 static bool tcg_out_dupm_vec(TCGContext *s, TCGType type, unsigned vece,
2572 TCGReg rd, TCGReg base, intptr_t offset)
2574 if (vece == MO_64) {
2575 tcg_out_ld(s, TCG_TYPE_V64, rd, base, offset);
2576 if (type == TCG_TYPE_V128) {
2577 tcg_out_dup2_vec(s, rd, rd, rd);
2580 int q = type - TCG_TYPE_V64;
2581 tcg_out_vldst(s, INSN_VLD1R | (vece << 6) | (q << 5),
2587 static void tcg_out_dupi_vec(TCGContext *s, TCGType type, unsigned vece,
2588 TCGReg rd, int64_t v64)
2590 int q = type - TCG_TYPE_V64;
2593 /* Test all bytes equal first. */
2595 tcg_out_vmovi(s, rd, q, 0, 0xe, v64);
2600 * Test all bytes 0x00 or 0xff second. This can match cases that
2601 * might otherwise take 2 or 3 insns for MO_16 or MO_32 below.
2603 for (i = imm8 = 0; i < 8; i++) {
2604 uint8_t byte = v64 >> (i * 8);
2607 } else if (byte != 0) {
2611 tcg_out_vmovi(s, rd, q, 1, 0xe, imm8);
2616 * Tests for various replications. For each element width, if we
2617 * cannot find an expansion there's no point checking a larger
2618 * width because we already know by replication it cannot match.
2620 if (vece == MO_16) {
2623 if (is_shimm16(v16, &cmode, &imm8)) {
2624 tcg_out_vmovi(s, rd, q, 0, cmode, imm8);
2627 if (is_shimm16(~v16, &cmode, &imm8)) {
2628 tcg_out_vmovi(s, rd, q, 1, cmode, imm8);
2633 * Otherwise, all remaining constants can be loaded in two insns:
2634 * rd = v16 & 0xff, rd |= v16 & 0xff00.
2636 tcg_out_vmovi(s, rd, q, 0, 0x8, v16 & 0xff);
2637 tcg_out_vmovi(s, rd, q, 0, 0xb, v16 >> 8); /* VORRI */
2641 if (vece == MO_32) {
2644 if (is_shimm32(v32, &cmode, &imm8) ||
2645 is_soimm32(v32, &cmode, &imm8)) {
2646 tcg_out_vmovi(s, rd, q, 0, cmode, imm8);
2649 if (is_shimm32(~v32, &cmode, &imm8) ||
2650 is_soimm32(~v32, &cmode, &imm8)) {
2651 tcg_out_vmovi(s, rd, q, 1, cmode, imm8);
2656 * Restrict the set of constants to those we can load with
2657 * two instructions. Others we load from the pool.
2659 i = is_shimm32_pair(v32, &cmode, &imm8);
2661 tcg_out_vmovi(s, rd, q, 0, cmode, imm8);
2662 tcg_out_vmovi(s, rd, q, 0, i | 1, extract32(v32, i * 4, 8));
2665 i = is_shimm32_pair(~v32, &cmode, &imm8);
2667 tcg_out_vmovi(s, rd, q, 1, cmode, imm8);
2668 tcg_out_vmovi(s, rd, q, 1, i | 1, extract32(~v32, i * 4, 8));
2674 * As a last resort, load from the constant pool.
2676 if (!q || vece == MO_64) {
2677 new_pool_l2(s, R_ARM_PC11, s->code_ptr, 0, v64, v64 >> 32);
2678 /* VLDR Dd, [pc + offset] */
2679 tcg_out32(s, INSN_VLDR_D | encode_vd(rd) | (0xf << 16));
2681 tcg_out_dup2_vec(s, rd, rd, rd);
2684 new_pool_label(s, (uint32_t)v64, R_ARM_PC8, s->code_ptr, 0);
2685 /* add tmp, pc, offset */
2686 tcg_out_dat_imm(s, COND_AL, ARITH_ADD, TCG_REG_TMP, TCG_REG_PC, 0);
2687 tcg_out_dupm_vec(s, type, MO_32, rd, TCG_REG_TMP, 0);
2691 static const ARMInsn vec_cmp_insn[16] = {
2692 [TCG_COND_EQ] = INSN_VCEQ,
2693 [TCG_COND_GT] = INSN_VCGT,
2694 [TCG_COND_GE] = INSN_VCGE,
2695 [TCG_COND_GTU] = INSN_VCGT_U,
2696 [TCG_COND_GEU] = INSN_VCGE_U,
2699 static const ARMInsn vec_cmp0_insn[16] = {
2700 [TCG_COND_EQ] = INSN_VCEQ0,
2701 [TCG_COND_GT] = INSN_VCGT0,
2702 [TCG_COND_GE] = INSN_VCGE0,
2703 [TCG_COND_LT] = INSN_VCLT0,
2704 [TCG_COND_LE] = INSN_VCLE0,
2707 static void tcg_out_vec_op(TCGContext *s, TCGOpcode opc,
2708 unsigned vecl, unsigned vece,
2709 const TCGArg args[TCG_MAX_OP_ARGS],
2710 const int const_args[TCG_MAX_OP_ARGS])
2712 TCGType type = vecl + TCG_TYPE_V64;
2714 TCGArg a0, a1, a2, a3;
2722 case INDEX_op_ld_vec:
2723 tcg_out_ld(s, type, a0, a1, a2);
2725 case INDEX_op_st_vec:
2726 tcg_out_st(s, type, a0, a1, a2);
2728 case INDEX_op_dupm_vec:
2729 tcg_out_dupm_vec(s, type, vece, a0, a1, a2);
2731 case INDEX_op_dup2_vec:
2732 tcg_out_dup2_vec(s, a0, a1, a2);
2734 case INDEX_op_abs_vec:
2735 tcg_out_vreg2(s, INSN_VABS, q, vece, a0, a1);
2737 case INDEX_op_neg_vec:
2738 tcg_out_vreg2(s, INSN_VNEG, q, vece, a0, a1);
2740 case INDEX_op_not_vec:
2741 tcg_out_vreg2(s, INSN_VMVN, q, 0, a0, a1);
2743 case INDEX_op_add_vec:
2744 tcg_out_vreg3(s, INSN_VADD, q, vece, a0, a1, a2);
2746 case INDEX_op_mul_vec:
2747 tcg_out_vreg3(s, INSN_VMUL, q, vece, a0, a1, a2);
2749 case INDEX_op_smax_vec:
2750 tcg_out_vreg3(s, INSN_VMAX, q, vece, a0, a1, a2);
2752 case INDEX_op_smin_vec:
2753 tcg_out_vreg3(s, INSN_VMIN, q, vece, a0, a1, a2);
2755 case INDEX_op_sub_vec:
2756 tcg_out_vreg3(s, INSN_VSUB, q, vece, a0, a1, a2);
2758 case INDEX_op_ssadd_vec:
2759 tcg_out_vreg3(s, INSN_VQADD, q, vece, a0, a1, a2);
2761 case INDEX_op_sssub_vec:
2762 tcg_out_vreg3(s, INSN_VQSUB, q, vece, a0, a1, a2);
2764 case INDEX_op_umax_vec:
2765 tcg_out_vreg3(s, INSN_VMAX_U, q, vece, a0, a1, a2);
2767 case INDEX_op_umin_vec:
2768 tcg_out_vreg3(s, INSN_VMIN_U, q, vece, a0, a1, a2);
2770 case INDEX_op_usadd_vec:
2771 tcg_out_vreg3(s, INSN_VQADD_U, q, vece, a0, a1, a2);
2773 case INDEX_op_ussub_vec:
2774 tcg_out_vreg3(s, INSN_VQSUB_U, q, vece, a0, a1, a2);
2776 case INDEX_op_xor_vec:
2777 tcg_out_vreg3(s, INSN_VEOR, q, 0, a0, a1, a2);
2779 case INDEX_op_arm_sshl_vec:
2781 * Note that Vm is the data and Vn is the shift count,
2782 * therefore the arguments appear reversed.
2784 tcg_out_vreg3(s, INSN_VSHL_S, q, vece, a0, a2, a1);
2786 case INDEX_op_arm_ushl_vec:
2788 tcg_out_vreg3(s, INSN_VSHL_U, q, vece, a0, a2, a1);
2790 case INDEX_op_shli_vec:
2791 tcg_out_vshifti(s, INSN_VSHLI, q, a0, a1, a2 + (8 << vece));
2793 case INDEX_op_shri_vec:
2794 tcg_out_vshifti(s, INSN_VSHRI, q, a0, a1, (16 << vece) - a2);
2796 case INDEX_op_sari_vec:
2797 tcg_out_vshifti(s, INSN_VSARI, q, a0, a1, (16 << vece) - a2);
2799 case INDEX_op_arm_sli_vec:
2800 tcg_out_vshifti(s, INSN_VSLI, q, a0, a2, args[3] + (8 << vece));
2803 case INDEX_op_andc_vec:
2804 if (!const_args[2]) {
2805 tcg_out_vreg3(s, INSN_VBIC, q, 0, a0, a1, a2);
2810 case INDEX_op_and_vec:
2811 if (const_args[2]) {
2812 is_shimm1632(~a2, &cmode, &imm8);
2814 tcg_out_vmovi(s, a0, q, 1, cmode | 1, imm8); /* VBICI */
2817 tcg_out_vmovi(s, a0, q, 1, cmode, imm8); /* VMVNI */
2820 tcg_out_vreg3(s, INSN_VAND, q, 0, a0, a1, a2);
2823 case INDEX_op_orc_vec:
2824 if (!const_args[2]) {
2825 tcg_out_vreg3(s, INSN_VORN, q, 0, a0, a1, a2);
2830 case INDEX_op_or_vec:
2831 if (const_args[2]) {
2832 is_shimm1632(a2, &cmode, &imm8);
2834 tcg_out_vmovi(s, a0, q, 0, cmode | 1, imm8); /* VORRI */
2837 tcg_out_vmovi(s, a0, q, 0, cmode, imm8); /* VMOVI */
2840 tcg_out_vreg3(s, INSN_VORR, q, 0, a0, a1, a2);
2843 case INDEX_op_cmp_vec:
2845 TCGCond cond = args[3];
2847 if (cond == TCG_COND_NE) {
2848 if (const_args[2]) {
2849 tcg_out_vreg3(s, INSN_VTST, q, vece, a0, a1, a1);
2851 tcg_out_vreg3(s, INSN_VCEQ, q, vece, a0, a1, a2);
2852 tcg_out_vreg2(s, INSN_VMVN, q, 0, a0, a0);
2857 if (const_args[2]) {
2858 insn = vec_cmp0_insn[cond];
2860 tcg_out_vreg2(s, insn, q, vece, a0, a1);
2863 tcg_out_dupi_vec(s, type, MO_8, TCG_VEC_TMP, 0);
2866 insn = vec_cmp_insn[cond];
2869 t = a1, a1 = a2, a2 = t;
2870 cond = tcg_swap_cond(cond);
2871 insn = vec_cmp_insn[cond];
2872 tcg_debug_assert(insn != 0);
2874 tcg_out_vreg3(s, insn, q, vece, a0, a1, a2);
2879 case INDEX_op_bitsel_vec:
2882 tcg_out_vreg3(s, INSN_VBIT, q, 0, a0, a2, a1);
2883 } else if (a0 == a2) {
2884 tcg_out_vreg3(s, INSN_VBIF, q, 0, a0, a3, a1);
2886 tcg_out_mov(s, type, a0, a1);
2887 tcg_out_vreg3(s, INSN_VBSL, q, 0, a0, a2, a3);
2891 case INDEX_op_mov_vec: /* Always emitted via tcg_out_mov. */
2892 case INDEX_op_dup_vec: /* Always emitted via tcg_out_dup_vec. */
2894 g_assert_not_reached();
2898 int tcg_can_emit_vec_op(TCGOpcode opc, TCGType type, unsigned vece)
2901 case INDEX_op_add_vec:
2902 case INDEX_op_sub_vec:
2903 case INDEX_op_and_vec:
2904 case INDEX_op_andc_vec:
2905 case INDEX_op_or_vec:
2906 case INDEX_op_orc_vec:
2907 case INDEX_op_xor_vec:
2908 case INDEX_op_not_vec:
2909 case INDEX_op_shli_vec:
2910 case INDEX_op_shri_vec:
2911 case INDEX_op_sari_vec:
2912 case INDEX_op_ssadd_vec:
2913 case INDEX_op_sssub_vec:
2914 case INDEX_op_usadd_vec:
2915 case INDEX_op_ussub_vec:
2916 case INDEX_op_bitsel_vec:
2918 case INDEX_op_abs_vec:
2919 case INDEX_op_cmp_vec:
2920 case INDEX_op_mul_vec:
2921 case INDEX_op_neg_vec:
2922 case INDEX_op_smax_vec:
2923 case INDEX_op_smin_vec:
2924 case INDEX_op_umax_vec:
2925 case INDEX_op_umin_vec:
2926 return vece < MO_64;
2927 case INDEX_op_shlv_vec:
2928 case INDEX_op_shrv_vec:
2929 case INDEX_op_sarv_vec:
2930 case INDEX_op_rotli_vec:
2931 case INDEX_op_rotlv_vec:
2932 case INDEX_op_rotrv_vec:
2939 void tcg_expand_vec_op(TCGOpcode opc, TCGType type, unsigned vece,
2943 TCGv_vec v0, v1, v2, t1, t2, c1;
2947 v0 = temp_tcgv_vec(arg_temp(a0));
2948 v1 = temp_tcgv_vec(arg_temp(va_arg(va, TCGArg)));
2949 a2 = va_arg(va, TCGArg);
2953 case INDEX_op_shlv_vec:
2955 * Merely propagate shlv_vec to arm_ushl_vec.
2956 * In this way we don't set TCG_TARGET_HAS_shv_vec
2957 * because everything is done via expansion.
2959 v2 = temp_tcgv_vec(arg_temp(a2));
2960 vec_gen_3(INDEX_op_arm_ushl_vec, type, vece, tcgv_vec_arg(v0),
2961 tcgv_vec_arg(v1), tcgv_vec_arg(v2));
2964 case INDEX_op_shrv_vec:
2965 case INDEX_op_sarv_vec:
2966 /* Right shifts are negative left shifts for NEON. */
2967 v2 = temp_tcgv_vec(arg_temp(a2));
2968 t1 = tcg_temp_new_vec(type);
2969 tcg_gen_neg_vec(vece, t1, v2);
2970 if (opc == INDEX_op_shrv_vec) {
2971 opc = INDEX_op_arm_ushl_vec;
2973 opc = INDEX_op_arm_sshl_vec;
2975 vec_gen_3(opc, type, vece, tcgv_vec_arg(v0),
2976 tcgv_vec_arg(v1), tcgv_vec_arg(t1));
2977 tcg_temp_free_vec(t1);
2980 case INDEX_op_rotli_vec:
2981 t1 = tcg_temp_new_vec(type);
2982 tcg_gen_shri_vec(vece, t1, v1, -a2 & ((8 << vece) - 1));
2983 vec_gen_4(INDEX_op_arm_sli_vec, type, vece,
2984 tcgv_vec_arg(v0), tcgv_vec_arg(t1), tcgv_vec_arg(v1), a2);
2985 tcg_temp_free_vec(t1);
2988 case INDEX_op_rotlv_vec:
2989 v2 = temp_tcgv_vec(arg_temp(a2));
2990 t1 = tcg_temp_new_vec(type);
2991 c1 = tcg_constant_vec(type, vece, 8 << vece);
2992 tcg_gen_sub_vec(vece, t1, v2, c1);
2993 /* Right shifts are negative left shifts for NEON. */
2994 vec_gen_3(INDEX_op_arm_ushl_vec, type, vece, tcgv_vec_arg(t1),
2995 tcgv_vec_arg(v1), tcgv_vec_arg(t1));
2996 vec_gen_3(INDEX_op_arm_ushl_vec, type, vece, tcgv_vec_arg(v0),
2997 tcgv_vec_arg(v1), tcgv_vec_arg(v2));
2998 tcg_gen_or_vec(vece, v0, v0, t1);
2999 tcg_temp_free_vec(t1);
3002 case INDEX_op_rotrv_vec:
3003 v2 = temp_tcgv_vec(arg_temp(a2));
3004 t1 = tcg_temp_new_vec(type);
3005 t2 = tcg_temp_new_vec(type);
3006 c1 = tcg_constant_vec(type, vece, 8 << vece);
3007 tcg_gen_neg_vec(vece, t1, v2);
3008 tcg_gen_sub_vec(vece, t2, c1, v2);
3009 /* Right shifts are negative left shifts for NEON. */
3010 vec_gen_3(INDEX_op_arm_ushl_vec, type, vece, tcgv_vec_arg(t1),
3011 tcgv_vec_arg(v1), tcgv_vec_arg(t1));
3012 vec_gen_3(INDEX_op_arm_ushl_vec, type, vece, tcgv_vec_arg(t2),
3013 tcgv_vec_arg(v1), tcgv_vec_arg(t2));
3014 tcg_gen_or_vec(vece, v0, t1, t2);
3015 tcg_temp_free_vec(t1);
3016 tcg_temp_free_vec(t2);
3020 g_assert_not_reached();
3024 static void tcg_out_nop_fill(tcg_insn_unit *p, int count)
3027 for (i = 0; i < count; ++i) {
3032 /* Compute frame size via macros, to share between tcg_target_qemu_prologue
3033 and tcg_register_jit. */
3035 #define PUSH_SIZE ((11 - 4 + 1 + 1) * sizeof(tcg_target_long))
3037 #define FRAME_SIZE \
3039 + TCG_STATIC_CALL_ARGS_SIZE \
3040 + CPU_TEMP_BUF_NLONGS * sizeof(long) \
3041 + TCG_TARGET_STACK_ALIGN - 1) \
3042 & -TCG_TARGET_STACK_ALIGN)
3044 #define STACK_ADDEND (FRAME_SIZE - PUSH_SIZE)
3046 static void tcg_target_qemu_prologue(TCGContext *s)
3048 /* Calling convention requires us to save r4-r11 and lr. */
3049 /* stmdb sp!, { r4 - r11, lr } */
3050 tcg_out_ldstm(s, COND_AL, INSN_STMDB, TCG_REG_CALL_STACK,
3051 (1 << TCG_REG_R4) | (1 << TCG_REG_R5) | (1 << TCG_REG_R6) |
3052 (1 << TCG_REG_R7) | (1 << TCG_REG_R8) | (1 << TCG_REG_R9) |
3053 (1 << TCG_REG_R10) | (1 << TCG_REG_R11) | (1 << TCG_REG_R14));
3055 /* Reserve callee argument and tcg temp space. */
3056 tcg_out_dat_rI(s, COND_AL, ARITH_SUB, TCG_REG_CALL_STACK,
3057 TCG_REG_CALL_STACK, STACK_ADDEND, 1);
3058 tcg_set_frame(s, TCG_REG_CALL_STACK, TCG_STATIC_CALL_ARGS_SIZE,
3059 CPU_TEMP_BUF_NLONGS * sizeof(long));
3061 tcg_out_mov(s, TCG_TYPE_PTR, TCG_AREG0, tcg_target_call_iarg_regs[0]);
3063 #ifndef CONFIG_SOFTMMU
3065 tcg_out_movi(s, TCG_TYPE_PTR, TCG_REG_GUEST_BASE, guest_base);
3066 tcg_regset_set_reg(s->reserved_regs, TCG_REG_GUEST_BASE);
3070 tcg_out_b_reg(s, COND_AL, tcg_target_call_iarg_regs[1]);
3073 * Return path for goto_ptr. Set return value to 0, a-la exit_tb,
3074 * and fall through to the rest of the epilogue.
3076 tcg_code_gen_epilogue = tcg_splitwx_to_rx(s->code_ptr);
3077 tcg_out_movi(s, TCG_TYPE_PTR, TCG_REG_R0, 0);
3078 tcg_out_epilogue(s);
3081 static void tcg_out_epilogue(TCGContext *s)
3083 /* Release local stack frame. */
3084 tcg_out_dat_rI(s, COND_AL, ARITH_ADD, TCG_REG_CALL_STACK,
3085 TCG_REG_CALL_STACK, STACK_ADDEND, 1);
3087 /* ldmia sp!, { r4 - r11, pc } */
3088 tcg_out_ldstm(s, COND_AL, INSN_LDMIA, TCG_REG_CALL_STACK,
3089 (1 << TCG_REG_R4) | (1 << TCG_REG_R5) | (1 << TCG_REG_R6) |
3090 (1 << TCG_REG_R7) | (1 << TCG_REG_R8) | (1 << TCG_REG_R9) |
3091 (1 << TCG_REG_R10) | (1 << TCG_REG_R11) | (1 << TCG_REG_PC));
3096 uint8_t fde_def_cfa[4];
3097 uint8_t fde_reg_ofs[18];
3100 #define ELF_HOST_MACHINE EM_ARM
3102 /* We're expecting a 2 byte uleb128 encoded value. */
3103 QEMU_BUILD_BUG_ON(FRAME_SIZE >= (1 << 14));
3105 static const DebugFrame debug_frame = {
3106 .h.cie.len = sizeof(DebugFrameCIE)-4, /* length after .len member */
3109 .h.cie.code_align = 1,
3110 .h.cie.data_align = 0x7c, /* sleb128 -4 */
3111 .h.cie.return_column = 14,
3113 /* Total FDE size does not include the "len" member. */
3114 .h.fde.len = sizeof(DebugFrame) - offsetof(DebugFrame, h.fde.cie_offset),
3117 12, 13, /* DW_CFA_def_cfa sp, ... */
3118 (FRAME_SIZE & 0x7f) | 0x80, /* ... uleb128 FRAME_SIZE */
3122 /* The following must match the stmdb in the prologue. */
3123 0x8e, 1, /* DW_CFA_offset, lr, -4 */
3124 0x8b, 2, /* DW_CFA_offset, r11, -8 */
3125 0x8a, 3, /* DW_CFA_offset, r10, -12 */
3126 0x89, 4, /* DW_CFA_offset, r9, -16 */
3127 0x88, 5, /* DW_CFA_offset, r8, -20 */
3128 0x87, 6, /* DW_CFA_offset, r7, -24 */
3129 0x86, 7, /* DW_CFA_offset, r6, -28 */
3130 0x85, 8, /* DW_CFA_offset, r5, -32 */
3131 0x84, 9, /* DW_CFA_offset, r4, -36 */
3135 void tcg_register_jit(const void *buf, size_t buf_size)
3137 tcg_register_jit_int(buf, buf_size, &debug_frame, sizeof(debug_frame));