xref: /openbmc/qemu/tcg/arm/tcg-target.c.inc (revision 63070ce368e1a2d430b9022a9db46f1817628efc)
1/*
2 * Tiny Code Generator for QEMU
3 *
4 * Copyright (c) 2008 Andrzej Zaborowski
5 *
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:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
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
22 * THE SOFTWARE.
23 */
24
25#include "elf.h"
26
27int arm_arch = __ARM_ARCH;
28
29#ifndef use_idiv_instructions
30bool use_idiv_instructions;
31#endif
32#ifndef use_neon_instructions
33bool use_neon_instructions;
34#endif
35
36/* Used for function call generation. */
37#define TCG_TARGET_STACK_ALIGN          8
38#define TCG_TARGET_CALL_STACK_OFFSET    0
39#define TCG_TARGET_CALL_ARG_I32         TCG_CALL_ARG_NORMAL
40#define TCG_TARGET_CALL_ARG_I64         TCG_CALL_ARG_EVEN
41#define TCG_TARGET_CALL_ARG_I128        TCG_CALL_ARG_EVEN
42#define TCG_TARGET_CALL_RET_I128        TCG_CALL_RET_BY_REF
43
44#ifdef CONFIG_DEBUG_TCG
45static const char * const tcg_target_reg_names[TCG_TARGET_NB_REGS] = {
46    "%r0",  "%r1",  "%r2",  "%r3",  "%r4",  "%r5",  "%r6",  "%r7",
47    "%r8",  "%r9",  "%r10", "%r11", "%r12", "%sp",  "%r14", "%pc",
48    "%q0",  "%q1",  "%q2",  "%q3",  "%q4",  "%q5",  "%q6",  "%q7",
49    "%q8",  "%q9",  "%q10", "%q11", "%q12", "%q13", "%q14", "%q15",
50};
51#endif
52
53static const int tcg_target_reg_alloc_order[] = {
54    TCG_REG_R4,
55    TCG_REG_R5,
56    TCG_REG_R6,
57    TCG_REG_R7,
58    TCG_REG_R8,
59    TCG_REG_R9,
60    TCG_REG_R10,
61    TCG_REG_R11,
62    TCG_REG_R13,
63    TCG_REG_R0,
64    TCG_REG_R1,
65    TCG_REG_R2,
66    TCG_REG_R3,
67    TCG_REG_R12,
68    TCG_REG_R14,
69
70    TCG_REG_Q0,
71    TCG_REG_Q1,
72    TCG_REG_Q2,
73    TCG_REG_Q3,
74    /* Q4 - Q7 are call-saved, and skipped. */
75    TCG_REG_Q8,
76    TCG_REG_Q9,
77    TCG_REG_Q10,
78    TCG_REG_Q11,
79    TCG_REG_Q12,
80    TCG_REG_Q13,
81    TCG_REG_Q14,
82    TCG_REG_Q15,
83};
84
85static const int tcg_target_call_iarg_regs[4] = {
86    TCG_REG_R0, TCG_REG_R1, TCG_REG_R2, TCG_REG_R3
87};
88
89static TCGReg tcg_target_call_oarg_reg(TCGCallReturnKind kind, int slot)
90{
91    tcg_debug_assert(kind == TCG_CALL_RET_NORMAL);
92    tcg_debug_assert(slot >= 0 && slot <= 3);
93    return TCG_REG_R0 + slot;
94}
95
96#define TCG_REG_TMP  TCG_REG_R12
97#define TCG_VEC_TMP  TCG_REG_Q15
98#define TCG_REG_GUEST_BASE  TCG_REG_R11
99
100typedef enum {
101    COND_EQ = 0x0,
102    COND_NE = 0x1,
103    COND_CS = 0x2,	/* Unsigned greater or equal */
104    COND_CC = 0x3,	/* Unsigned less than */
105    COND_MI = 0x4,	/* Negative */
106    COND_PL = 0x5,	/* Zero or greater */
107    COND_VS = 0x6,	/* Overflow */
108    COND_VC = 0x7,	/* No overflow */
109    COND_HI = 0x8,	/* Unsigned greater than */
110    COND_LS = 0x9,	/* Unsigned less or equal */
111    COND_GE = 0xa,
112    COND_LT = 0xb,
113    COND_GT = 0xc,
114    COND_LE = 0xd,
115    COND_AL = 0xe,
116} ARMCond;
117
118#define TO_CPSR (1 << 20)
119
120#define SHIFT_IMM_LSL(im)	(((im) << 7) | 0x00)
121#define SHIFT_IMM_LSR(im)	(((im) << 7) | 0x20)
122#define SHIFT_IMM_ASR(im)	(((im) << 7) | 0x40)
123#define SHIFT_IMM_ROR(im)	(((im) << 7) | 0x60)
124#define SHIFT_REG_LSL(rs)	(((rs) << 8) | 0x10)
125#define SHIFT_REG_LSR(rs)	(((rs) << 8) | 0x30)
126#define SHIFT_REG_ASR(rs)	(((rs) << 8) | 0x50)
127#define SHIFT_REG_ROR(rs)	(((rs) << 8) | 0x70)
128
129typedef enum {
130    ARITH_AND = 0x0 << 21,
131    ARITH_EOR = 0x1 << 21,
132    ARITH_SUB = 0x2 << 21,
133    ARITH_RSB = 0x3 << 21,
134    ARITH_ADD = 0x4 << 21,
135    ARITH_ADC = 0x5 << 21,
136    ARITH_SBC = 0x6 << 21,
137    ARITH_RSC = 0x7 << 21,
138    ARITH_TST = 0x8 << 21 | TO_CPSR,
139    ARITH_CMP = 0xa << 21 | TO_CPSR,
140    ARITH_CMN = 0xb << 21 | TO_CPSR,
141    ARITH_ORR = 0xc << 21,
142    ARITH_MOV = 0xd << 21,
143    ARITH_BIC = 0xe << 21,
144    ARITH_MVN = 0xf << 21,
145
146    INSN_B         = 0x0a000000,
147
148    INSN_CLZ       = 0x016f0f10,
149    INSN_RBIT      = 0x06ff0f30,
150
151    INSN_LDMIA     = 0x08b00000,
152    INSN_STMDB     = 0x09200000,
153
154    INSN_LDR_IMM   = 0x04100000,
155    INSN_LDR_REG   = 0x06100000,
156    INSN_STR_IMM   = 0x04000000,
157    INSN_STR_REG   = 0x06000000,
158
159    INSN_LDRH_IMM  = 0x005000b0,
160    INSN_LDRH_REG  = 0x001000b0,
161    INSN_LDRSH_IMM = 0x005000f0,
162    INSN_LDRSH_REG = 0x001000f0,
163    INSN_STRH_IMM  = 0x004000b0,
164    INSN_STRH_REG  = 0x000000b0,
165
166    INSN_LDRB_IMM  = 0x04500000,
167    INSN_LDRB_REG  = 0x06500000,
168    INSN_LDRSB_IMM = 0x005000d0,
169    INSN_LDRSB_REG = 0x001000d0,
170    INSN_STRB_IMM  = 0x04400000,
171    INSN_STRB_REG  = 0x06400000,
172
173    INSN_LDRD_IMM  = 0x004000d0,
174    INSN_LDRD_REG  = 0x000000d0,
175    INSN_STRD_IMM  = 0x004000f0,
176    INSN_STRD_REG  = 0x000000f0,
177
178    INSN_DMB_ISH   = 0xf57ff05b,
179    INSN_DMB_MCR   = 0xee070fba,
180
181    INSN_MSRI_CPSR = 0x0360f000,
182
183    /* Architected nop introduced in v6k.  */
184    /* ??? This is an MSR (imm) 0,0,0 insn.  Anyone know if this
185       also Just So Happened to do nothing on pre-v6k so that we
186       don't need to conditionalize it?  */
187    INSN_NOP_v6k   = 0xe320f000,
188    /* Otherwise the assembler uses mov r0,r0 */
189    INSN_NOP_v4    = (COND_AL << 28) | ARITH_MOV,
190
191    INSN_VADD      = 0xf2000800,
192    INSN_VAND      = 0xf2000110,
193    INSN_VBIC      = 0xf2100110,
194    INSN_VEOR      = 0xf3000110,
195    INSN_VORN      = 0xf2300110,
196    INSN_VORR      = 0xf2200110,
197    INSN_VSUB      = 0xf3000800,
198    INSN_VMUL      = 0xf2000910,
199    INSN_VQADD     = 0xf2000010,
200    INSN_VQADD_U   = 0xf3000010,
201    INSN_VQSUB     = 0xf2000210,
202    INSN_VQSUB_U   = 0xf3000210,
203    INSN_VMAX      = 0xf2000600,
204    INSN_VMAX_U    = 0xf3000600,
205    INSN_VMIN      = 0xf2000610,
206    INSN_VMIN_U    = 0xf3000610,
207
208    INSN_VABS      = 0xf3b10300,
209    INSN_VMVN      = 0xf3b00580,
210    INSN_VNEG      = 0xf3b10380,
211
212    INSN_VCEQ0     = 0xf3b10100,
213    INSN_VCGT0     = 0xf3b10000,
214    INSN_VCGE0     = 0xf3b10080,
215    INSN_VCLE0     = 0xf3b10180,
216    INSN_VCLT0     = 0xf3b10200,
217
218    INSN_VCEQ      = 0xf3000810,
219    INSN_VCGE      = 0xf2000310,
220    INSN_VCGT      = 0xf2000300,
221    INSN_VCGE_U    = 0xf3000310,
222    INSN_VCGT_U    = 0xf3000300,
223
224    INSN_VSHLI     = 0xf2800510,  /* VSHL (immediate) */
225    INSN_VSARI     = 0xf2800010,  /* VSHR.S */
226    INSN_VSHRI     = 0xf3800010,  /* VSHR.U */
227    INSN_VSLI      = 0xf3800510,
228    INSN_VSHL_S    = 0xf2000400,  /* VSHL.S (register) */
229    INSN_VSHL_U    = 0xf3000400,  /* VSHL.U (register) */
230
231    INSN_VBSL      = 0xf3100110,
232    INSN_VBIT      = 0xf3200110,
233    INSN_VBIF      = 0xf3300110,
234
235    INSN_VTST      = 0xf2000810,
236
237    INSN_VDUP_G    = 0xee800b10,  /* VDUP (ARM core register) */
238    INSN_VDUP_S    = 0xf3b00c00,  /* VDUP (scalar) */
239    INSN_VLDR_D    = 0xed100b00,  /* VLDR.64 */
240    INSN_VLD1      = 0xf4200000,  /* VLD1 (multiple single elements) */
241    INSN_VLD1R     = 0xf4a00c00,  /* VLD1 (single element to all lanes) */
242    INSN_VST1      = 0xf4000000,  /* VST1 (multiple single elements) */
243    INSN_VMOVI     = 0xf2800010,  /* VMOV (immediate) */
244} ARMInsn;
245
246#define INSN_NOP   (use_armv7_instructions ? INSN_NOP_v6k : INSN_NOP_v4)
247
248static const uint8_t tcg_cond_to_arm_cond[] = {
249    [TCG_COND_EQ] = COND_EQ,
250    [TCG_COND_NE] = COND_NE,
251    [TCG_COND_LT] = COND_LT,
252    [TCG_COND_GE] = COND_GE,
253    [TCG_COND_LE] = COND_LE,
254    [TCG_COND_GT] = COND_GT,
255    /* unsigned */
256    [TCG_COND_LTU] = COND_CC,
257    [TCG_COND_GEU] = COND_CS,
258    [TCG_COND_LEU] = COND_LS,
259    [TCG_COND_GTU] = COND_HI,
260};
261
262static int encode_imm(uint32_t imm);
263
264/* TCG private relocation type: add with pc+imm8 */
265#define R_ARM_PC8  11
266
267/* TCG private relocation type: vldr with imm8 << 2 */
268#define R_ARM_PC11 12
269
270static bool reloc_pc24(tcg_insn_unit *src_rw, const tcg_insn_unit *target)
271{
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) >> 2;
274
275    if (offset == sextract32(offset, 0, 24)) {
276        *src_rw = deposit32(*src_rw, 0, 24, offset);
277        return true;
278    }
279    return false;
280}
281
282static bool reloc_pc13(tcg_insn_unit *src_rw, const tcg_insn_unit *target)
283{
284    const tcg_insn_unit *src_rx = tcg_splitwx_to_rx(src_rw);
285    ptrdiff_t offset = tcg_ptr_byte_diff(target, src_rx) - 8;
286
287    if (offset >= -0xfff && offset <= 0xfff) {
288        tcg_insn_unit insn = *src_rw;
289        bool u = (offset >= 0);
290        if (!u) {
291            offset = -offset;
292        }
293        insn = deposit32(insn, 23, 1, u);
294        insn = deposit32(insn, 0, 12, offset);
295        *src_rw = insn;
296        return true;
297    }
298    return false;
299}
300
301static bool reloc_pc11(tcg_insn_unit *src_rw, const tcg_insn_unit *target)
302{
303    const tcg_insn_unit *src_rx = tcg_splitwx_to_rx(src_rw);
304    ptrdiff_t offset = (tcg_ptr_byte_diff(target, src_rx) - 8) / 4;
305
306    if (offset >= -0xff && offset <= 0xff) {
307        tcg_insn_unit insn = *src_rw;
308        bool u = (offset >= 0);
309        if (!u) {
310            offset = -offset;
311        }
312        insn = deposit32(insn, 23, 1, u);
313        insn = deposit32(insn, 0, 8, offset);
314        *src_rw = insn;
315        return true;
316    }
317    return false;
318}
319
320static bool reloc_pc8(tcg_insn_unit *src_rw, const tcg_insn_unit *target)
321{
322    const tcg_insn_unit *src_rx = tcg_splitwx_to_rx(src_rw);
323    ptrdiff_t offset = tcg_ptr_byte_diff(target, src_rx) - 8;
324    int imm12 = encode_imm(offset);
325
326    if (imm12 >= 0) {
327        *src_rw = deposit32(*src_rw, 0, 12, imm12);
328        return true;
329    }
330    return false;
331}
332
333static bool patch_reloc(tcg_insn_unit *code_ptr, int type,
334                        intptr_t value, intptr_t addend)
335{
336    tcg_debug_assert(addend == 0);
337    switch (type) {
338    case R_ARM_PC24:
339        return reloc_pc24(code_ptr, (const tcg_insn_unit *)value);
340    case R_ARM_PC13:
341        return reloc_pc13(code_ptr, (const tcg_insn_unit *)value);
342    case R_ARM_PC11:
343        return reloc_pc11(code_ptr, (const tcg_insn_unit *)value);
344    case R_ARM_PC8:
345        return reloc_pc8(code_ptr, (const tcg_insn_unit *)value);
346    default:
347        g_assert_not_reached();
348    }
349}
350
351#define TCG_CT_CONST_ARM  0x100
352#define TCG_CT_CONST_INV  0x200
353#define TCG_CT_CONST_NEG  0x400
354#define TCG_CT_CONST_ZERO 0x800
355#define TCG_CT_CONST_ORRI 0x1000
356#define TCG_CT_CONST_ANDI 0x2000
357
358#define ALL_GENERAL_REGS  0xffffu
359#define ALL_VECTOR_REGS   0xffff0000u
360
361/*
362 * r0-r3 will be overwritten when reading the tlb entry (system-mode only);
363 * r14 will be overwritten by the BLNE branching to the slow path.
364 */
365#define ALL_QLDST_REGS \
366    (ALL_GENERAL_REGS & ~((tcg_use_softmmu ? 0xf : 0) | (1 << TCG_REG_R14)))
367
368/*
369 * ARM immediates for ALU instructions are made of an unsigned 8-bit
370 * right-rotated by an even amount between 0 and 30.
371 *
372 * Return < 0 if @imm cannot be encoded, else the entire imm12 field.
373 */
374static int encode_imm(uint32_t imm)
375{
376    uint32_t rot, imm8;
377
378    /* Simple case, no rotation required. */
379    if ((imm & ~0xff) == 0) {
380        return imm;
381    }
382
383    /* Next, try a simple even shift.  */
384    rot = ctz32(imm) & ~1;
385    imm8 = imm >> rot;
386    rot = 32 - rot;
387    if ((imm8 & ~0xff) == 0) {
388        goto found;
389    }
390
391    /*
392     * Finally, try harder with rotations.
393     * The ctz test above will have taken care of rotates >= 8.
394     */
395    for (rot = 2; rot < 8; rot += 2) {
396        imm8 = rol32(imm, rot);
397        if ((imm8 & ~0xff) == 0) {
398            goto found;
399        }
400    }
401    /* Fail: imm cannot be encoded. */
402    return -1;
403
404 found:
405    /* Note that rot is even, and we discard bit 0 by shifting by 7. */
406    return rot << 7 | imm8;
407}
408
409static int encode_imm_nofail(uint32_t imm)
410{
411    int ret = encode_imm(imm);
412    tcg_debug_assert(ret >= 0);
413    return ret;
414}
415
416static bool check_fit_imm(uint32_t imm)
417{
418    return encode_imm(imm) >= 0;
419}
420
421/* Return true if v16 is a valid 16-bit shifted immediate.  */
422static bool is_shimm16(uint16_t v16, int *cmode, int *imm8)
423{
424    if (v16 == (v16 & 0xff)) {
425        *cmode = 0x8;
426        *imm8 = v16 & 0xff;
427        return true;
428    } else if (v16 == (v16 & 0xff00)) {
429        *cmode = 0xa;
430        *imm8 = v16 >> 8;
431        return true;
432    }
433    return false;
434}
435
436/* Return true if v32 is a valid 32-bit shifted immediate.  */
437static bool is_shimm32(uint32_t v32, int *cmode, int *imm8)
438{
439    if (v32 == (v32 & 0xff)) {
440        *cmode = 0x0;
441        *imm8 = v32 & 0xff;
442        return true;
443    } else if (v32 == (v32 & 0xff00)) {
444        *cmode = 0x2;
445        *imm8 = (v32 >> 8) & 0xff;
446        return true;
447    } else if (v32 == (v32 & 0xff0000)) {
448        *cmode = 0x4;
449        *imm8 = (v32 >> 16) & 0xff;
450        return true;
451    } else if (v32 == (v32 & 0xff000000)) {
452        *cmode = 0x6;
453        *imm8 = v32 >> 24;
454        return true;
455    }
456    return false;
457}
458
459/* Return true if v32 is a valid 32-bit shifting ones immediate.  */
460static bool is_soimm32(uint32_t v32, int *cmode, int *imm8)
461{
462    if ((v32 & 0xffff00ff) == 0xff) {
463        *cmode = 0xc;
464        *imm8 = (v32 >> 8) & 0xff;
465        return true;
466    } else if ((v32 & 0xff00ffff) == 0xffff) {
467        *cmode = 0xd;
468        *imm8 = (v32 >> 16) & 0xff;
469        return true;
470    }
471    return false;
472}
473
474/*
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.
478 */
479static int is_shimm32_pair(uint32_t v32, int *cmode, int *imm8)
480{
481    int i;
482
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)) {
488            break;
489        }
490    }
491    return i;
492}
493
494/* Return true if V is a valid 16-bit or 32-bit shifted immediate.  */
495static bool is_shimm1632(uint32_t v32, int *cmode, int *imm8)
496{
497    if (v32 == deposit32(v32, 16, 16, v32)) {
498        return is_shimm16(v32, cmode, imm8);
499    } else {
500        return is_shimm32(v32, cmode, imm8);
501    }
502}
503
504/* Test if a constant matches the constraint.
505 * TODO: define constraints for:
506 *
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
511 */
512static bool tcg_target_const_match(int64_t val, int ct,
513                                   TCGType type, TCGCond cond, int vece)
514{
515    if (ct & TCG_CT_CONST) {
516        return 1;
517    } else if ((ct & TCG_CT_CONST_ARM) && check_fit_imm(val)) {
518        return 1;
519    } else if ((ct & TCG_CT_CONST_INV) && check_fit_imm(~val)) {
520        return 1;
521    } else if ((ct & TCG_CT_CONST_NEG) && check_fit_imm(-val)) {
522        return 1;
523    } else if ((ct & TCG_CT_CONST_ZERO) && val == 0) {
524        return 1;
525    }
526
527    switch (ct & (TCG_CT_CONST_ORRI | TCG_CT_CONST_ANDI)) {
528    case 0:
529        break;
530    case TCG_CT_CONST_ANDI:
531        val = ~val;
532        /* fallthru */
533    case TCG_CT_CONST_ORRI:
534        if (val == deposit64(val, 32, 32, val)) {
535            int cmode, imm8;
536            return is_shimm1632(val, &cmode, &imm8);
537        }
538        break;
539    default:
540        /* Both bits should not be set for the same insn.  */
541        g_assert_not_reached();
542    }
543
544    return 0;
545}
546
547static void tcg_out_b_imm(TCGContext *s, ARMCond cond, int32_t offset)
548{
549    tcg_out32(s, (cond << 28) | INSN_B |
550                    (((offset - 8) >> 2) & 0x00ffffff));
551}
552
553static void tcg_out_bl_imm(TCGContext *s, ARMCond cond, int32_t offset)
554{
555    tcg_out32(s, (cond << 28) | 0x0b000000 |
556                    (((offset - 8) >> 2) & 0x00ffffff));
557}
558
559static void tcg_out_blx_reg(TCGContext *s, ARMCond cond, TCGReg rn)
560{
561    tcg_out32(s, (cond << 28) | 0x012fff30 | rn);
562}
563
564static void tcg_out_blx_imm(TCGContext *s, int32_t offset)
565{
566    tcg_out32(s, 0xfa000000 | ((offset & 2) << 23) |
567                (((offset - 8) >> 2) & 0x00ffffff));
568}
569
570static void tcg_out_dat_reg(TCGContext *s, ARMCond cond, ARMInsn opc,
571                            TCGReg rd, TCGReg rn, TCGReg rm, int shift)
572{
573    tcg_out32(s, (cond << 28) | (0 << 25) | opc |
574                    (rn << 16) | (rd << 12) | shift | rm);
575}
576
577static void tcg_out_mov_reg(TCGContext *s, ARMCond cond, TCGReg rd, TCGReg rm)
578{
579    /* Simple reg-reg move, optimising out the 'do nothing' case */
580    if (rd != rm) {
581        tcg_out_dat_reg(s, cond, ARITH_MOV, rd, 0, rm, SHIFT_IMM_LSL(0));
582    }
583}
584
585static void tcg_out_bx_reg(TCGContext *s, ARMCond cond, TCGReg rn)
586{
587    tcg_out32(s, (cond << 28) | 0x012fff10 | rn);
588}
589
590static void tcg_out_b_reg(TCGContext *s, ARMCond cond, TCGReg rn)
591{
592    /*
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.
595     */
596    tcg_out_bx_reg(s, cond, rn);
597}
598
599static void tcg_out_dat_imm(TCGContext *s, ARMCond cond, ARMInsn opc,
600                            TCGReg rd, TCGReg rn, int im)
601{
602    tcg_out32(s, (cond << 28) | (1 << 25) | opc |
603                    (rn << 16) | (rd << 12) | im);
604}
605
606static void tcg_out_ldstm(TCGContext *s, ARMCond cond, ARMInsn opc,
607                          TCGReg rn, uint16_t mask)
608{
609    tcg_out32(s, (cond << 28) | opc | (rn << 16) | mask);
610}
611
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.  */
614static void tcg_out_memop_r(TCGContext *s, ARMCond cond, ARMInsn opc, TCGReg rt,
615                            TCGReg rn, TCGReg rm, bool u, bool p, bool w)
616{
617    tcg_out32(s, (cond << 28) | opc | (u << 23) | (p << 24)
618              | (w << 21) | (rn << 16) | (rt << 12) | rm);
619}
620
621static void tcg_out_memop_8(TCGContext *s, ARMCond cond, ARMInsn opc, TCGReg rt,
622                            TCGReg rn, int imm8, bool p, bool w)
623{
624    bool u = 1;
625    if (imm8 < 0) {
626        imm8 = -imm8;
627        u = 0;
628    }
629    tcg_out32(s, (cond << 28) | opc | (u << 23) | (p << 24) | (w << 21) |
630              (rn << 16) | (rt << 12) | ((imm8 & 0xf0) << 4) | (imm8 & 0xf));
631}
632
633static void tcg_out_memop_12(TCGContext *s, ARMCond cond, ARMInsn opc,
634                             TCGReg rt, TCGReg rn, int imm12, bool p, bool w)
635{
636    bool u = 1;
637    if (imm12 < 0) {
638        imm12 = -imm12;
639        u = 0;
640    }
641    tcg_out32(s, (cond << 28) | opc | (u << 23) | (p << 24) | (w << 21) |
642              (rn << 16) | (rt << 12) | imm12);
643}
644
645static void tcg_out_ld32_12(TCGContext *s, ARMCond cond, TCGReg rt,
646                            TCGReg rn, int imm12)
647{
648    tcg_out_memop_12(s, cond, INSN_LDR_IMM, rt, rn, imm12, 1, 0);
649}
650
651static void tcg_out_st32_12(TCGContext *s, ARMCond cond, TCGReg rt,
652                            TCGReg rn, int imm12)
653{
654    tcg_out_memop_12(s, cond, INSN_STR_IMM, rt, rn, imm12, 1, 0);
655}
656
657static void tcg_out_ld32_r(TCGContext *s, ARMCond cond, TCGReg rt,
658                           TCGReg rn, TCGReg rm)
659{
660    tcg_out_memop_r(s, cond, INSN_LDR_REG, rt, rn, rm, 1, 1, 0);
661}
662
663static void tcg_out_st32_r(TCGContext *s, ARMCond cond, TCGReg rt,
664                           TCGReg rn, TCGReg rm)
665{
666    tcg_out_memop_r(s, cond, INSN_STR_REG, rt, rn, rm, 1, 1, 0);
667}
668
669static void tcg_out_ldrd_8(TCGContext *s, ARMCond cond, TCGReg rt,
670                           TCGReg rn, int imm8)
671{
672    tcg_out_memop_8(s, cond, INSN_LDRD_IMM, rt, rn, imm8, 1, 0);
673}
674
675static void tcg_out_ldrd_r(TCGContext *s, ARMCond cond, TCGReg rt,
676                           TCGReg rn, TCGReg rm)
677{
678    tcg_out_memop_r(s, cond, INSN_LDRD_REG, rt, rn, rm, 1, 1, 0);
679}
680
681static void tcg_out_strd_8(TCGContext *s, ARMCond cond, TCGReg rt,
682                           TCGReg rn, int imm8)
683{
684    tcg_out_memop_8(s, cond, INSN_STRD_IMM, rt, rn, imm8, 1, 0);
685}
686
687static void tcg_out_strd_r(TCGContext *s, ARMCond cond, TCGReg rt,
688                           TCGReg rn, TCGReg rm)
689{
690    tcg_out_memop_r(s, cond, INSN_STRD_REG, rt, rn, rm, 1, 1, 0);
691}
692
693/* Register pre-increment with base writeback.  */
694static void tcg_out_ld32_rwb(TCGContext *s, ARMCond cond, TCGReg rt,
695                             TCGReg rn, TCGReg rm)
696{
697    tcg_out_memop_r(s, cond, INSN_LDR_REG, rt, rn, rm, 1, 1, 1);
698}
699
700static void tcg_out_st32_rwb(TCGContext *s, ARMCond cond, TCGReg rt,
701                             TCGReg rn, TCGReg rm)
702{
703    tcg_out_memop_r(s, cond, INSN_STR_REG, rt, rn, rm, 1, 1, 1);
704}
705
706static void tcg_out_ld16u_8(TCGContext *s, ARMCond cond, TCGReg rt,
707                            TCGReg rn, int imm8)
708{
709    tcg_out_memop_8(s, cond, INSN_LDRH_IMM, rt, rn, imm8, 1, 0);
710}
711
712static void tcg_out_st16_8(TCGContext *s, ARMCond cond, TCGReg rt,
713                           TCGReg rn, int imm8)
714{
715    tcg_out_memop_8(s, cond, INSN_STRH_IMM, rt, rn, imm8, 1, 0);
716}
717
718static void tcg_out_ld16u_r(TCGContext *s, ARMCond cond, TCGReg rt,
719                            TCGReg rn, TCGReg rm)
720{
721    tcg_out_memop_r(s, cond, INSN_LDRH_REG, rt, rn, rm, 1, 1, 0);
722}
723
724static void tcg_out_st16_r(TCGContext *s, ARMCond cond, TCGReg rt,
725                           TCGReg rn, TCGReg rm)
726{
727    tcg_out_memop_r(s, cond, INSN_STRH_REG, rt, rn, rm, 1, 1, 0);
728}
729
730static void tcg_out_ld16s_8(TCGContext *s, ARMCond cond, TCGReg rt,
731                            TCGReg rn, int imm8)
732{
733    tcg_out_memop_8(s, cond, INSN_LDRSH_IMM, rt, rn, imm8, 1, 0);
734}
735
736static void tcg_out_ld16s_r(TCGContext *s, ARMCond cond, TCGReg rt,
737                            TCGReg rn, TCGReg rm)
738{
739    tcg_out_memop_r(s, cond, INSN_LDRSH_REG, rt, rn, rm, 1, 1, 0);
740}
741
742static void tcg_out_ld8_12(TCGContext *s, ARMCond cond, TCGReg rt,
743                           TCGReg rn, int imm12)
744{
745    tcg_out_memop_12(s, cond, INSN_LDRB_IMM, rt, rn, imm12, 1, 0);
746}
747
748static void tcg_out_st8_12(TCGContext *s, ARMCond cond, TCGReg rt,
749                           TCGReg rn, int imm12)
750{
751    tcg_out_memop_12(s, cond, INSN_STRB_IMM, rt, rn, imm12, 1, 0);
752}
753
754static void tcg_out_ld8_r(TCGContext *s, ARMCond cond, TCGReg rt,
755                          TCGReg rn, TCGReg rm)
756{
757    tcg_out_memop_r(s, cond, INSN_LDRB_REG, rt, rn, rm, 1, 1, 0);
758}
759
760static void tcg_out_st8_r(TCGContext *s, ARMCond cond, TCGReg rt,
761                          TCGReg rn, TCGReg rm)
762{
763    tcg_out_memop_r(s, cond, INSN_STRB_REG, rt, rn, rm, 1, 1, 0);
764}
765
766static void tcg_out_ld8s_8(TCGContext *s, ARMCond cond, TCGReg rt,
767                           TCGReg rn, int imm8)
768{
769    tcg_out_memop_8(s, cond, INSN_LDRSB_IMM, rt, rn, imm8, 1, 0);
770}
771
772static void tcg_out_ld8s_r(TCGContext *s, ARMCond cond, TCGReg rt,
773                           TCGReg rn, TCGReg rm)
774{
775    tcg_out_memop_r(s, cond, INSN_LDRSB_REG, rt, rn, rm, 1, 1, 0);
776}
777
778static void tcg_out_movi_pool(TCGContext *s, ARMCond cond,
779                              TCGReg rd, uint32_t arg)
780{
781    new_pool_label(s, arg, R_ARM_PC13, s->code_ptr, 0);
782    tcg_out_ld32_12(s, cond, rd, TCG_REG_PC, 0);
783}
784
785static void tcg_out_movi32(TCGContext *s, ARMCond cond,
786                           TCGReg rd, uint32_t arg)
787{
788    int imm12, diff, opc, sh1, sh2;
789    uint32_t tt0, tt1, tt2;
790
791    /* Check a single MOV/MVN before anything else.  */
792    imm12 = encode_imm(arg);
793    if (imm12 >= 0) {
794        tcg_out_dat_imm(s, cond, ARITH_MOV, rd, 0, imm12);
795        return;
796    }
797    imm12 = encode_imm(~arg);
798    if (imm12 >= 0) {
799        tcg_out_dat_imm(s, cond, ARITH_MVN, rd, 0, imm12);
800        return;
801    }
802
803    /* Check for a pc-relative address.  This will usually be the TB,
804       or within the TB, which is immediately before the code block.  */
805    diff = tcg_pcrel_diff(s, (void *)arg) - 8;
806    if (diff >= 0) {
807        imm12 = encode_imm(diff);
808        if (imm12 >= 0) {
809            tcg_out_dat_imm(s, cond, ARITH_ADD, rd, TCG_REG_PC, imm12);
810            return;
811        }
812    } else {
813        imm12 = encode_imm(-diff);
814        if (imm12 >= 0) {
815            tcg_out_dat_imm(s, cond, ARITH_SUB, rd, TCG_REG_PC, imm12);
816            return;
817        }
818    }
819
820    /* Use movw + movt.  */
821    if (use_armv7_instructions) {
822        /* movw */
823        tcg_out32(s, (cond << 28) | 0x03000000 | (rd << 12)
824                  | ((arg << 4) & 0x000f0000) | (arg & 0xfff));
825        if (arg & 0xffff0000) {
826            /* movt */
827            tcg_out32(s, (cond << 28) | 0x03400000 | (rd << 12)
828                      | ((arg >> 12) & 0x000f0000) | ((arg >> 16) & 0xfff));
829        }
830        return;
831    }
832
833    /* Look for sequences of two insns.  If we have lots of 1's, we can
834       shorten the sequence by beginning with mvn and then clearing
835       higher bits with eor.  */
836    tt0 = arg;
837    opc = ARITH_MOV;
838    if (ctpop32(arg) > 16) {
839        tt0 = ~arg;
840        opc = ARITH_MVN;
841    }
842    sh1 = ctz32(tt0) & ~1;
843    tt1 = tt0 & ~(0xff << sh1);
844    sh2 = ctz32(tt1) & ~1;
845    tt2 = tt1 & ~(0xff << sh2);
846    if (tt2 == 0) {
847        int rot;
848
849        rot = ((32 - sh1) << 7) & 0xf00;
850        tcg_out_dat_imm(s, cond, opc, rd,  0, ((tt0 >> sh1) & 0xff) | rot);
851        rot = ((32 - sh2) << 7) & 0xf00;
852        tcg_out_dat_imm(s, cond, ARITH_EOR, rd, rd,
853                        ((tt0 >> sh2) & 0xff) | rot);
854        return;
855    }
856
857    /* Otherwise, drop it into the constant pool.  */
858    tcg_out_movi_pool(s, cond, rd, arg);
859}
860
861/*
862 * Emit either the reg,imm or reg,reg form of a data-processing insn.
863 * rhs must satisfy the "rI" constraint.
864 */
865static void tcg_out_dat_rI(TCGContext *s, ARMCond cond, ARMInsn opc,
866                           TCGReg dst, TCGReg lhs, TCGArg rhs, int rhs_is_const)
867{
868    if (rhs_is_const) {
869        tcg_out_dat_imm(s, cond, opc, dst, lhs, encode_imm_nofail(rhs));
870    } else {
871        tcg_out_dat_reg(s, cond, opc, dst, lhs, rhs, SHIFT_IMM_LSL(0));
872    }
873}
874
875/*
876 * Emit either the reg,imm or reg,reg form of a data-processing insn.
877 * rhs must satisfy the "rIK" constraint.
878 */
879static void tcg_out_dat_IK(TCGContext *s, ARMCond cond, ARMInsn opc,
880                            ARMInsn opinv, TCGReg dst, TCGReg lhs, TCGArg rhs)
881{
882    int imm12 = encode_imm(rhs);
883    if (imm12 < 0) {
884        imm12 = encode_imm_nofail(~rhs);
885        opc = opinv;
886    }
887    tcg_out_dat_imm(s, cond, opc, dst, lhs, imm12);
888}
889
890static void tcg_out_dat_rIK(TCGContext *s, ARMCond cond, ARMInsn opc,
891                            ARMInsn opinv, TCGReg dst, TCGReg lhs, TCGArg rhs,
892                            bool rhs_is_const)
893{
894    if (rhs_is_const) {
895        tcg_out_dat_IK(s, cond, opc, opinv, dst, lhs, rhs);
896    } else {
897        tcg_out_dat_reg(s, cond, opc, dst, lhs, rhs, SHIFT_IMM_LSL(0));
898    }
899}
900
901static void tcg_out_dat_IN(TCGContext *s, ARMCond cond, ARMInsn opc,
902                           ARMInsn opneg, TCGReg dst, TCGReg lhs, TCGArg rhs)
903{
904    int imm12 = encode_imm(rhs);
905    if (imm12 < 0) {
906        imm12 = encode_imm_nofail(-rhs);
907        opc = opneg;
908    }
909    tcg_out_dat_imm(s, cond, opc, dst, lhs, imm12);
910}
911
912static void tcg_out_dat_rIN(TCGContext *s, ARMCond cond, ARMInsn opc,
913                            ARMInsn opneg, TCGReg dst, TCGReg lhs, TCGArg rhs,
914                            bool rhs_is_const)
915{
916    /* Emit either the reg,imm or reg,reg form of a data-processing insn.
917     * rhs must satisfy the "rIN" constraint.
918     */
919    if (rhs_is_const) {
920        tcg_out_dat_IN(s, cond, opc, opneg, dst, lhs, rhs);
921    } else {
922        tcg_out_dat_reg(s, cond, opc, dst, lhs, rhs, SHIFT_IMM_LSL(0));
923    }
924}
925
926static void tcg_out_ext8s(TCGContext *s, TCGType t, TCGReg rd, TCGReg rn)
927{
928    /* sxtb */
929    tcg_out32(s, 0x06af0070 | (COND_AL << 28) | (rd << 12) | rn);
930}
931
932static void tcg_out_ext8u(TCGContext *s, TCGReg rd, TCGReg rn)
933{
934    tcg_out_dat_imm(s, COND_AL, ARITH_AND, rd, rn, 0xff);
935}
936
937static void tcg_out_ext16s(TCGContext *s, TCGType t, TCGReg rd, TCGReg rn)
938{
939    /* sxth */
940    tcg_out32(s, 0x06bf0070 | (COND_AL << 28) | (rd << 12) | rn);
941}
942
943static void tcg_out_ext16u(TCGContext *s, TCGReg rd, TCGReg rn)
944{
945    /* uxth */
946    tcg_out32(s, 0x06ff0070 | (COND_AL << 28) | (rd << 12) | rn);
947}
948
949static void tcg_out_ext32s(TCGContext *s, TCGReg rd, TCGReg rn)
950{
951    g_assert_not_reached();
952}
953
954static void tcg_out_ext32u(TCGContext *s, TCGReg rd, TCGReg rn)
955{
956    g_assert_not_reached();
957}
958
959static void tcg_out_exts_i32_i64(TCGContext *s, TCGReg rd, TCGReg rn)
960{
961    g_assert_not_reached();
962}
963
964static void tcg_out_extu_i32_i64(TCGContext *s, TCGReg rd, TCGReg rn)
965{
966    g_assert_not_reached();
967}
968
969static void tcg_out_extrl_i64_i32(TCGContext *s, TCGReg rd, TCGReg rn)
970{
971    g_assert_not_reached();
972}
973
974static void tgen_deposit(TCGContext *s, TCGType type, TCGReg a0, TCGReg a1,
975                         TCGReg a2, unsigned ofs, unsigned len)
976{
977    /* bfi/bfc */
978    tcg_out32(s, 0x07c00010 | (COND_AL << 28) | (a0 << 12) | a1
979              | (ofs << 7) | ((ofs + len - 1) << 16));
980}
981
982static void tgen_depositi(TCGContext *s, TCGType type, TCGReg a0, TCGReg a1,
983                          tcg_target_long a2, unsigned ofs, unsigned len)
984{
985    /* bfi becomes bfc with rn == 15.  */
986    tgen_deposit(s, type, a0, a1, 15, ofs, len);
987}
988
989static const TCGOutOpDeposit outop_deposit = {
990    .base.static_constraint = C_O1_I2(r, 0, rZ),
991    .out_rrr = tgen_deposit,
992    .out_rri = tgen_depositi,
993};
994
995static void tgen_extract(TCGContext *s, TCGType type, TCGReg rd, TCGReg rn,
996                         unsigned ofs, unsigned len)
997{
998    /* According to gcc, AND can be faster. */
999    if (ofs == 0 && len <= 8) {
1000        tcg_out_dat_imm(s, COND_AL, ARITH_AND, rd, rn,
1001                        encode_imm_nofail((1 << len) - 1));
1002        return;
1003    }
1004
1005    if (use_armv7_instructions) {
1006        /* ubfx */
1007        tcg_out32(s, 0x07e00050 | (COND_AL << 28) | (rd << 12) | rn
1008                  | (ofs << 7) | ((len - 1) << 16));
1009        return;
1010    }
1011
1012    assert(ofs % 8 == 0);
1013    switch (len) {
1014    case 8:
1015        /* uxtb */
1016        tcg_out32(s, 0x06ef0070 | (COND_AL << 28) |
1017                  (rd << 12) | (ofs << 7) | rn);
1018        break;
1019    case 16:
1020        /* uxth */
1021        tcg_out32(s, 0x06ff0070 | (COND_AL << 28) |
1022                  (rd << 12) | (ofs << 7) | rn);
1023        break;
1024    default:
1025        g_assert_not_reached();
1026    }
1027}
1028
1029static const TCGOutOpExtract outop_extract = {
1030    .base.static_constraint = C_O1_I1(r, r),
1031    .out_rr = tgen_extract,
1032};
1033
1034static void tgen_sextract(TCGContext *s, TCGType type, TCGReg rd, TCGReg rn,
1035                          unsigned ofs, unsigned len)
1036{
1037    if (use_armv7_instructions) {
1038        /* sbfx */
1039        tcg_out32(s, 0x07a00050 | (COND_AL << 28) | (rd << 12) | rn
1040                  | (ofs << 7) | ((len - 1) << 16));
1041        return;
1042    }
1043
1044    assert(ofs % 8 == 0);
1045    switch (len) {
1046    case 8:
1047        /* sxtb */
1048        tcg_out32(s, 0x06af0070 | (COND_AL << 28) |
1049                  (rd << 12) | (ofs << 7) | rn);
1050        break;
1051    case 16:
1052        /* sxth */
1053        tcg_out32(s, 0x06bf0070 | (COND_AL << 28) |
1054                  (rd << 12) | (ofs << 7) | rn);
1055        break;
1056    default:
1057        g_assert_not_reached();
1058    }
1059}
1060
1061static const TCGOutOpExtract outop_sextract = {
1062    .base.static_constraint = C_O1_I1(r, r),
1063    .out_rr = tgen_sextract,
1064};
1065
1066
1067static void tcg_out_ld32u(TCGContext *s, ARMCond cond,
1068                          TCGReg rd, TCGReg rn, int32_t offset)
1069{
1070    if (offset > 0xfff || offset < -0xfff) {
1071        tcg_out_movi32(s, cond, TCG_REG_TMP, offset);
1072        tcg_out_ld32_r(s, cond, rd, rn, TCG_REG_TMP);
1073    } else
1074        tcg_out_ld32_12(s, cond, rd, rn, offset);
1075}
1076
1077static void tcg_out_st32(TCGContext *s, ARMCond cond,
1078                         TCGReg rd, TCGReg rn, int32_t offset)
1079{
1080    if (offset > 0xfff || offset < -0xfff) {
1081        tcg_out_movi32(s, cond, TCG_REG_TMP, offset);
1082        tcg_out_st32_r(s, cond, rd, rn, TCG_REG_TMP);
1083    } else
1084        tcg_out_st32_12(s, cond, rd, rn, offset);
1085}
1086
1087/*
1088 * The _goto case is normally between TBs within the same code buffer, and
1089 * with the code buffer limited to 16MB we wouldn't need the long case.
1090 * But we also use it for the tail-call to the qemu_ld/st helpers, which does.
1091 */
1092static void tcg_out_goto(TCGContext *s, ARMCond cond, const tcg_insn_unit *addr)
1093{
1094    intptr_t addri = (intptr_t)addr;
1095    ptrdiff_t disp = tcg_pcrel_diff(s, addr);
1096    bool arm_mode = !(addri & 1);
1097
1098    if (arm_mode && disp - 8 < 0x01fffffd && disp - 8 > -0x01fffffd) {
1099        tcg_out_b_imm(s, cond, disp);
1100        return;
1101    }
1102
1103    /* LDR is interworking from v5t. */
1104    tcg_out_movi_pool(s, cond, TCG_REG_PC, addri);
1105}
1106
1107/*
1108 * The call case is mostly used for helpers - so it's not unreasonable
1109 * for them to be beyond branch range.
1110 */
1111static void tcg_out_call_int(TCGContext *s, const tcg_insn_unit *addr)
1112{
1113    intptr_t addri = (intptr_t)addr;
1114    ptrdiff_t disp = tcg_pcrel_diff(s, addr);
1115    bool arm_mode = !(addri & 1);
1116
1117    if (disp - 8 < 0x02000000 && disp - 8 >= -0x02000000) {
1118        if (arm_mode) {
1119            tcg_out_bl_imm(s, COND_AL, disp);
1120        } else {
1121            tcg_out_blx_imm(s, disp);
1122        }
1123        return;
1124    }
1125
1126    tcg_out_movi32(s, COND_AL, TCG_REG_TMP, addri);
1127    tcg_out_blx_reg(s, COND_AL, TCG_REG_TMP);
1128}
1129
1130static void tcg_out_call(TCGContext *s, const tcg_insn_unit *addr,
1131                         const TCGHelperInfo *info)
1132{
1133    tcg_out_call_int(s, addr);
1134}
1135
1136static void tcg_out_goto_label(TCGContext *s, ARMCond cond, TCGLabel *l)
1137{
1138    if (l->has_value) {
1139        tcg_out_goto(s, cond, l->u.value_ptr);
1140    } else {
1141        tcg_out_reloc(s, s->code_ptr, R_ARM_PC24, l, 0);
1142        tcg_out_b_imm(s, cond, 0);
1143    }
1144}
1145
1146static void tcg_out_br(TCGContext *s, TCGLabel *l)
1147{
1148    tcg_out_goto_label(s, COND_AL, l);
1149}
1150
1151static void tcg_out_mb(TCGContext *s, unsigned a0)
1152{
1153    if (use_armv7_instructions) {
1154        tcg_out32(s, INSN_DMB_ISH);
1155    } else {
1156        tcg_out32(s, INSN_DMB_MCR);
1157    }
1158}
1159
1160static TCGCond tgen_cmp(TCGContext *s, TCGCond cond, TCGReg a, TCGReg b)
1161{
1162    if (is_tst_cond(cond)) {
1163        tcg_out_dat_reg(s, COND_AL, ARITH_TST, 0, a, b, SHIFT_IMM_LSL(0));
1164        return tcg_tst_eqne_cond(cond);
1165    }
1166    tcg_out_dat_reg(s, COND_AL, ARITH_CMP, 0, a, b, SHIFT_IMM_LSL(0));
1167    return cond;
1168}
1169
1170static TCGCond tgen_cmpi(TCGContext *s, TCGCond cond, TCGReg a, TCGArg b)
1171{
1172    int imm12;
1173
1174    if (!is_tst_cond(cond)) {
1175        tcg_out_dat_IN(s, COND_AL, ARITH_CMP, ARITH_CMN, 0, a, b);
1176        return cond;
1177    }
1178
1179    /*
1180     * The compare constraints allow rIN, but TST does not support N.
1181     * Be prepared to load the constant into a scratch register.
1182     */
1183    imm12 = encode_imm(b);
1184    if (imm12 >= 0) {
1185        tcg_out_dat_imm(s, COND_AL, ARITH_TST, 0, a, imm12);
1186    } else {
1187        tcg_out_movi32(s, COND_AL, TCG_REG_TMP, b);
1188        tcg_out_dat_reg(s, COND_AL, ARITH_TST, 0,
1189                        a, TCG_REG_TMP, SHIFT_IMM_LSL(0));
1190    }
1191    return tcg_tst_eqne_cond(cond);
1192}
1193
1194static TCGCond tcg_out_cmp(TCGContext *s, TCGCond cond, TCGReg a,
1195                           TCGArg b, int b_const)
1196{
1197    if (b_const) {
1198        return tgen_cmpi(s, cond, a, b);
1199    } else {
1200        return tgen_cmp(s, cond, a, b);
1201    }
1202}
1203
1204static TCGCond tcg_out_cmp2(TCGContext *s, TCGCond cond, TCGReg al, TCGReg ah,
1205                            TCGArg bl, bool const_bl, TCGArg bh, bool const_bh)
1206{
1207    switch (cond) {
1208    case TCG_COND_EQ:
1209    case TCG_COND_NE:
1210    case TCG_COND_LTU:
1211    case TCG_COND_LEU:
1212    case TCG_COND_GTU:
1213    case TCG_COND_GEU:
1214        /*
1215         * We perform a conditional comparison.  If the high half is
1216         * equal, then overwrite the flags with the comparison of the
1217         * low half.  The resulting flags cover the whole.
1218         */
1219        tcg_out_dat_rI(s, COND_AL, ARITH_CMP, 0, ah, bh, const_bh);
1220        tcg_out_dat_rI(s, COND_EQ, ARITH_CMP, 0, al, bl, const_bl);
1221        return cond;
1222
1223    case TCG_COND_TSTEQ:
1224    case TCG_COND_TSTNE:
1225        /* Similar, but with TST instead of CMP. */
1226        tcg_out_dat_rI(s, COND_AL, ARITH_TST, 0, ah, bh, const_bh);
1227        tcg_out_dat_rI(s, COND_EQ, ARITH_TST, 0, al, bl, const_bl);
1228        return tcg_tst_eqne_cond(cond);
1229
1230    case TCG_COND_LT:
1231    case TCG_COND_GE:
1232        /* We perform a double-word subtraction and examine the result.
1233           We do not actually need the result of the subtract, so the
1234           low part "subtract" is a compare.  For the high half we have
1235           no choice but to compute into a temporary.  */
1236        tcg_out_dat_rI(s, COND_AL, ARITH_CMP, 0, al, bl, const_bl);
1237        tcg_out_dat_rI(s, COND_AL, ARITH_SBC | TO_CPSR,
1238                       TCG_REG_TMP, ah, bh, const_bh);
1239        return cond;
1240
1241    case TCG_COND_LE:
1242    case TCG_COND_GT:
1243        /* Similar, but with swapped arguments, via reversed subtract.  */
1244        tcg_out_dat_rI(s, COND_AL, ARITH_RSB | TO_CPSR,
1245                       TCG_REG_TMP, al, bl, const_bl);
1246        tcg_out_dat_rI(s, COND_AL, ARITH_RSC | TO_CPSR,
1247                       TCG_REG_TMP, ah, bh, const_bh);
1248        return tcg_swap_cond(cond);
1249
1250    default:
1251        g_assert_not_reached();
1252    }
1253}
1254
1255/*
1256 * Note that TCGReg references Q-registers.
1257 * Q-regno = 2 * D-regno, so shift left by 1 while inserting.
1258 */
1259static uint32_t encode_vd(TCGReg rd)
1260{
1261    tcg_debug_assert(rd >= TCG_REG_Q0);
1262    return (extract32(rd, 3, 1) << 22) | (extract32(rd, 0, 3) << 13);
1263}
1264
1265static uint32_t encode_vn(TCGReg rn)
1266{
1267    tcg_debug_assert(rn >= TCG_REG_Q0);
1268    return (extract32(rn, 3, 1) << 7) | (extract32(rn, 0, 3) << 17);
1269}
1270
1271static uint32_t encode_vm(TCGReg rm)
1272{
1273    tcg_debug_assert(rm >= TCG_REG_Q0);
1274    return (extract32(rm, 3, 1) << 5) | (extract32(rm, 0, 3) << 1);
1275}
1276
1277static void tcg_out_vreg2(TCGContext *s, ARMInsn insn, int q, int vece,
1278                          TCGReg d, TCGReg m)
1279{
1280    tcg_out32(s, insn | (vece << 18) | (q << 6) |
1281              encode_vd(d) | encode_vm(m));
1282}
1283
1284static void tcg_out_vreg3(TCGContext *s, ARMInsn insn, int q, int vece,
1285                          TCGReg d, TCGReg n, TCGReg m)
1286{
1287    tcg_out32(s, insn | (vece << 20) | (q << 6) |
1288              encode_vd(d) | encode_vn(n) | encode_vm(m));
1289}
1290
1291static void tcg_out_vmovi(TCGContext *s, TCGReg rd,
1292                          int q, int op, int cmode, uint8_t imm8)
1293{
1294    tcg_out32(s, INSN_VMOVI | encode_vd(rd) | (q << 6) | (op << 5)
1295              | (cmode << 8) | extract32(imm8, 0, 4)
1296              | (extract32(imm8, 4, 3) << 16)
1297              | (extract32(imm8, 7, 1) << 24));
1298}
1299
1300static void tcg_out_vshifti(TCGContext *s, ARMInsn insn, int q,
1301                            TCGReg rd, TCGReg rm, int l_imm6)
1302{
1303    tcg_out32(s, insn | (q << 6) | encode_vd(rd) | encode_vm(rm) |
1304              (extract32(l_imm6, 6, 1) << 7) |
1305              (extract32(l_imm6, 0, 6) << 16));
1306}
1307
1308static void tcg_out_vldst(TCGContext *s, ARMInsn insn,
1309                          TCGReg rd, TCGReg rn, int offset)
1310{
1311    if (offset != 0) {
1312        if (check_fit_imm(offset) || check_fit_imm(-offset)) {
1313            tcg_out_dat_rIN(s, COND_AL, ARITH_ADD, ARITH_SUB,
1314                            TCG_REG_TMP, rn, offset, true);
1315        } else {
1316            tcg_out_movi(s, TCG_TYPE_PTR, TCG_REG_TMP, offset);
1317            tcg_out_dat_reg(s, COND_AL, ARITH_ADD,
1318                            TCG_REG_TMP, TCG_REG_TMP, rn, 0);
1319        }
1320        rn = TCG_REG_TMP;
1321    }
1322    tcg_out32(s, insn | (rn << 16) | encode_vd(rd) | 0xf);
1323}
1324
1325typedef struct {
1326    ARMCond cond;
1327    TCGReg base;
1328    int index;
1329    bool index_scratch;
1330    TCGAtomAlign aa;
1331} HostAddress;
1332
1333bool tcg_target_has_memory_bswap(MemOp memop)
1334{
1335    return false;
1336}
1337
1338static TCGReg ldst_ra_gen(TCGContext *s, const TCGLabelQemuLdst *l, int arg)
1339{
1340    /* We arrive at the slow path via "BLNE", so R14 contains l->raddr. */
1341    return TCG_REG_R14;
1342}
1343
1344static const TCGLdstHelperParam ldst_helper_param = {
1345    .ra_gen = ldst_ra_gen,
1346    .ntmp = 1,
1347    .tmp = { TCG_REG_TMP },
1348};
1349
1350static bool tcg_out_qemu_ld_slow_path(TCGContext *s, TCGLabelQemuLdst *lb)
1351{
1352    MemOp opc = get_memop(lb->oi);
1353
1354    if (!reloc_pc24(lb->label_ptr[0], tcg_splitwx_to_rx(s->code_ptr))) {
1355        return false;
1356    }
1357
1358    tcg_out_ld_helper_args(s, lb, &ldst_helper_param);
1359    tcg_out_call_int(s, qemu_ld_helpers[opc & MO_SIZE]);
1360    tcg_out_ld_helper_ret(s, lb, false, &ldst_helper_param);
1361
1362    tcg_out_goto(s, COND_AL, lb->raddr);
1363    return true;
1364}
1365
1366static bool tcg_out_qemu_st_slow_path(TCGContext *s, TCGLabelQemuLdst *lb)
1367{
1368    MemOp opc = get_memop(lb->oi);
1369
1370    if (!reloc_pc24(lb->label_ptr[0], tcg_splitwx_to_rx(s->code_ptr))) {
1371        return false;
1372    }
1373
1374    tcg_out_st_helper_args(s, lb, &ldst_helper_param);
1375
1376    /* Tail-call to the helper, which will return to the fast path.  */
1377    tcg_out_goto(s, COND_AL, qemu_st_helpers[opc & MO_SIZE]);
1378    return true;
1379}
1380
1381/* We expect to use an 9-bit sign-magnitude negative offset from ENV.  */
1382#define MIN_TLB_MASK_TABLE_OFS  -256
1383
1384static TCGLabelQemuLdst *prepare_host_addr(TCGContext *s, HostAddress *h,
1385                                           TCGReg addr, MemOpIdx oi, bool is_ld)
1386{
1387    TCGLabelQemuLdst *ldst = NULL;
1388    MemOp opc = get_memop(oi);
1389    unsigned a_mask;
1390
1391    if (tcg_use_softmmu) {
1392        *h = (HostAddress){
1393            .cond = COND_AL,
1394            .base = addr,
1395            .index = TCG_REG_R1,
1396            .index_scratch = true,
1397        };
1398    } else {
1399        *h = (HostAddress){
1400            .cond = COND_AL,
1401            .base = addr,
1402            .index = guest_base ? TCG_REG_GUEST_BASE : -1,
1403            .index_scratch = false,
1404        };
1405    }
1406
1407    h->aa = atom_and_align_for_opc(s, opc, MO_ATOM_IFALIGN, false);
1408    a_mask = (1 << h->aa.align) - 1;
1409
1410    if (tcg_use_softmmu) {
1411        int mem_index = get_mmuidx(oi);
1412        int cmp_off = is_ld ? offsetof(CPUTLBEntry, addr_read)
1413                            : offsetof(CPUTLBEntry, addr_write);
1414        int fast_off = tlb_mask_table_ofs(s, mem_index);
1415        unsigned s_mask = (1 << (opc & MO_SIZE)) - 1;
1416        TCGReg t_addr;
1417
1418        ldst = new_ldst_label(s);
1419        ldst->is_ld = is_ld;
1420        ldst->oi = oi;
1421        ldst->addr_reg = addr;
1422
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);
1427
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, addr,
1430                        SHIFT_IMM_LSR(TARGET_PAGE_BITS - CPU_TLB_ENTRY_BITS));
1431
1432        /*
1433         * Add the tlb_table pointer, creating the CPUTLBEntry address in R1.
1434         * Load the tlb comparator into R2 and the fast path addend into R1.
1435         */
1436        if (cmp_off == 0) {
1437            tcg_out_ld32_rwb(s, COND_AL, TCG_REG_R2, TCG_REG_R1, TCG_REG_R0);
1438        } else {
1439            tcg_out_dat_reg(s, COND_AL, ARITH_ADD,
1440                            TCG_REG_R1, TCG_REG_R1, TCG_REG_R0, 0);
1441            tcg_out_ld32_12(s, COND_AL, TCG_REG_R2, TCG_REG_R1, cmp_off);
1442        }
1443
1444        /* Load the tlb addend.  */
1445        tcg_out_ld32_12(s, COND_AL, TCG_REG_R1, TCG_REG_R1,
1446                        offsetof(CPUTLBEntry, addend));
1447
1448        /*
1449         * Check alignment, check comparators.
1450         * Do this in 2-4 insns.  Use MOVW for v7, if possible,
1451         * to reduce the number of sequential conditional instructions.
1452         * Almost all guests have at least 4k pages, which means that we need
1453         * to clear at least 9 bits even for an 8-byte memory, which means it
1454         * isn't worth checking for an immediate operand for BIC.
1455         *
1456         * For unaligned accesses, test the page of the last unit of alignment.
1457         * This leaves the least significant alignment bits unchanged, and of
1458         * course must be zero.
1459         */
1460        t_addr = addr;
1461        if (a_mask < s_mask) {
1462            t_addr = TCG_REG_R0;
1463            tcg_out_dat_imm(s, COND_AL, ARITH_ADD, t_addr,
1464                            addr, s_mask - a_mask);
1465        }
1466        if (use_armv7_instructions && TARGET_PAGE_BITS <= 16) {
1467            tcg_out_movi32(s, COND_AL, TCG_REG_TMP, ~(TARGET_PAGE_MASK | a_mask));
1468            tcg_out_dat_reg(s, COND_AL, ARITH_BIC, TCG_REG_TMP,
1469                            t_addr, TCG_REG_TMP, 0);
1470            tcg_out_dat_reg(s, COND_AL, ARITH_CMP, 0,
1471                            TCG_REG_R2, TCG_REG_TMP, 0);
1472        } else {
1473            if (a_mask) {
1474                tcg_debug_assert(a_mask <= 0xff);
1475                tcg_out_dat_imm(s, COND_AL, ARITH_TST, 0, addr, a_mask);
1476            }
1477            tcg_out_dat_reg(s, COND_AL, ARITH_MOV, TCG_REG_TMP, 0, t_addr,
1478                            SHIFT_IMM_LSR(TARGET_PAGE_BITS));
1479            tcg_out_dat_reg(s, (a_mask ? COND_EQ : COND_AL), ARITH_CMP,
1480                            0, TCG_REG_R2, TCG_REG_TMP,
1481                            SHIFT_IMM_LSL(TARGET_PAGE_BITS));
1482        }
1483    } else if (a_mask) {
1484        ldst = new_ldst_label(s);
1485        ldst->is_ld = is_ld;
1486        ldst->oi = oi;
1487        ldst->addr_reg = addr;
1488
1489        /* We are expecting alignment to max out at 7 */
1490        tcg_debug_assert(a_mask <= 0xff);
1491        /* tst addr, #mask */
1492        tcg_out_dat_imm(s, COND_AL, ARITH_TST, 0, addr, a_mask);
1493    }
1494
1495    return ldst;
1496}
1497
1498static void tcg_out_qemu_ld_direct(TCGContext *s, MemOp opc, TCGReg datalo,
1499                                   TCGReg datahi, HostAddress h)
1500{
1501    TCGReg base;
1502
1503    /* Byte swapping is left to middle-end expansion. */
1504    tcg_debug_assert((opc & MO_BSWAP) == 0);
1505
1506    switch (opc & MO_SSIZE) {
1507    case MO_UB:
1508        if (h.index < 0) {
1509            tcg_out_ld8_12(s, h.cond, datalo, h.base, 0);
1510        } else {
1511            tcg_out_ld8_r(s, h.cond, datalo, h.base, h.index);
1512        }
1513        break;
1514    case MO_SB:
1515        if (h.index < 0) {
1516            tcg_out_ld8s_8(s, h.cond, datalo, h.base, 0);
1517        } else {
1518            tcg_out_ld8s_r(s, h.cond, datalo, h.base, h.index);
1519        }
1520        break;
1521    case MO_UW:
1522        if (h.index < 0) {
1523            tcg_out_ld16u_8(s, h.cond, datalo, h.base, 0);
1524        } else {
1525            tcg_out_ld16u_r(s, h.cond, datalo, h.base, h.index);
1526        }
1527        break;
1528    case MO_SW:
1529        if (h.index < 0) {
1530            tcg_out_ld16s_8(s, h.cond, datalo, h.base, 0);
1531        } else {
1532            tcg_out_ld16s_r(s, h.cond, datalo, h.base, h.index);
1533        }
1534        break;
1535    case MO_UL:
1536        if (h.index < 0) {
1537            tcg_out_ld32_12(s, h.cond, datalo, h.base, 0);
1538        } else {
1539            tcg_out_ld32_r(s, h.cond, datalo, h.base, h.index);
1540        }
1541        break;
1542    case MO_UQ:
1543        /* We used pair allocation for datalo, so already should be aligned. */
1544        tcg_debug_assert((datalo & 1) == 0);
1545        tcg_debug_assert(datahi == datalo + 1);
1546        /* LDRD requires alignment; double-check that. */
1547        if (memop_alignment_bits(opc) >= MO_64) {
1548            if (h.index < 0) {
1549                tcg_out_ldrd_8(s, h.cond, datalo, h.base, 0);
1550                break;
1551            }
1552            /*
1553             * Rm (the second address op) must not overlap Rt or Rt + 1.
1554             * Since datalo is aligned, we can simplify the test via alignment.
1555             * Flip the two address arguments if that works.
1556             */
1557            if ((h.index & ~1) != datalo) {
1558                tcg_out_ldrd_r(s, h.cond, datalo, h.base, h.index);
1559                break;
1560            }
1561            if ((h.base & ~1) != datalo) {
1562                tcg_out_ldrd_r(s, h.cond, datalo, h.index, h.base);
1563                break;
1564            }
1565        }
1566        if (h.index < 0) {
1567            base = h.base;
1568            if (datalo == h.base) {
1569                tcg_out_mov_reg(s, h.cond, TCG_REG_TMP, base);
1570                base = TCG_REG_TMP;
1571            }
1572        } else if (h.index_scratch) {
1573            tcg_out_ld32_rwb(s, h.cond, datalo, h.index, h.base);
1574            tcg_out_ld32_12(s, h.cond, datahi, h.index, 4);
1575            break;
1576        } else {
1577            tcg_out_dat_reg(s, h.cond, ARITH_ADD, TCG_REG_TMP,
1578                            h.base, h.index, SHIFT_IMM_LSL(0));
1579            base = TCG_REG_TMP;
1580        }
1581        tcg_out_ld32_12(s, h.cond, datalo, base, 0);
1582        tcg_out_ld32_12(s, h.cond, datahi, base, 4);
1583        break;
1584    default:
1585        g_assert_not_reached();
1586    }
1587}
1588
1589static void tgen_qemu_ld(TCGContext *s, TCGType type, TCGReg data,
1590                         TCGReg addr, MemOpIdx oi)
1591{
1592    MemOp opc = get_memop(oi);
1593    TCGLabelQemuLdst *ldst;
1594    HostAddress h;
1595
1596    ldst = prepare_host_addr(s, &h, addr, oi, true);
1597    if (ldst) {
1598        ldst->type = type;
1599        ldst->datalo_reg = data;
1600        ldst->datahi_reg = -1;
1601
1602        /*
1603         * This a conditional BL only to load a pointer within this
1604         * opcode into LR for the slow path.  We will not be using
1605         * the value for a tail call.
1606         */
1607        ldst->label_ptr[0] = s->code_ptr;
1608        tcg_out_bl_imm(s, COND_NE, 0);
1609    }
1610
1611    tcg_out_qemu_ld_direct(s, opc, data, -1, h);
1612
1613    if (ldst) {
1614        ldst->raddr = tcg_splitwx_to_rx(s->code_ptr);
1615    }
1616}
1617
1618static const TCGOutOpQemuLdSt outop_qemu_ld = {
1619    .base.static_constraint = C_O1_I1(r, q),
1620    .out = tgen_qemu_ld,
1621};
1622
1623static void tgen_qemu_ld2(TCGContext *s, TCGType type, TCGReg datalo,
1624                          TCGReg datahi, TCGReg addr, MemOpIdx oi)
1625{
1626    MemOp opc = get_memop(oi);
1627    TCGLabelQemuLdst *ldst;
1628    HostAddress h;
1629
1630    ldst = prepare_host_addr(s, &h, addr, oi, true);
1631    if (ldst) {
1632        ldst->type = type;
1633        ldst->datalo_reg = datalo;
1634        ldst->datahi_reg = datahi;
1635
1636        /*
1637         * This a conditional BL only to load a pointer within this
1638         * opcode into LR for the slow path.  We will not be using
1639         * the value for a tail call.
1640         */
1641        ldst->label_ptr[0] = s->code_ptr;
1642        tcg_out_bl_imm(s, COND_NE, 0);
1643    }
1644
1645    tcg_out_qemu_ld_direct(s, opc, datalo, datahi, h);
1646
1647    if (ldst) {
1648        ldst->raddr = tcg_splitwx_to_rx(s->code_ptr);
1649    }
1650}
1651
1652static const TCGOutOpQemuLdSt2 outop_qemu_ld2 = {
1653    .base.static_constraint = C_O2_I1(e, p, q),
1654    .out = tgen_qemu_ld2,
1655};
1656
1657static void tcg_out_qemu_st_direct(TCGContext *s, MemOp opc, TCGReg datalo,
1658                                   TCGReg datahi, HostAddress h)
1659{
1660    /* Byte swapping is left to middle-end expansion. */
1661    tcg_debug_assert((opc & MO_BSWAP) == 0);
1662
1663    switch (opc & MO_SIZE) {
1664    case MO_8:
1665        if (h.index < 0) {
1666            tcg_out_st8_12(s, h.cond, datalo, h.base, 0);
1667        } else {
1668            tcg_out_st8_r(s, h.cond, datalo, h.base, h.index);
1669        }
1670        break;
1671    case MO_16:
1672        if (h.index < 0) {
1673            tcg_out_st16_8(s, h.cond, datalo, h.base, 0);
1674        } else {
1675            tcg_out_st16_r(s, h.cond, datalo, h.base, h.index);
1676        }
1677        break;
1678    case MO_32:
1679        if (h.index < 0) {
1680            tcg_out_st32_12(s, h.cond, datalo, h.base, 0);
1681        } else {
1682            tcg_out_st32_r(s, h.cond, datalo, h.base, h.index);
1683        }
1684        break;
1685    case MO_64:
1686        /* We used pair allocation for datalo, so already should be aligned. */
1687        tcg_debug_assert((datalo & 1) == 0);
1688        tcg_debug_assert(datahi == datalo + 1);
1689        /* STRD requires alignment; double-check that. */
1690        if (memop_alignment_bits(opc) >= MO_64) {
1691            if (h.index < 0) {
1692                tcg_out_strd_8(s, h.cond, datalo, h.base, 0);
1693            } else {
1694                tcg_out_strd_r(s, h.cond, datalo, h.base, h.index);
1695            }
1696        } else if (h.index < 0) {
1697            tcg_out_st32_12(s, h.cond, datalo, h.base, 0);
1698            tcg_out_st32_12(s, h.cond, datahi, h.base, 4);
1699        } else if (h.index_scratch) {
1700            tcg_out_st32_rwb(s, h.cond, datalo, h.index, h.base);
1701            tcg_out_st32_12(s, h.cond, datahi, h.index, 4);
1702        } else {
1703            tcg_out_dat_reg(s, h.cond, ARITH_ADD, TCG_REG_TMP,
1704                            h.base, h.index, SHIFT_IMM_LSL(0));
1705            tcg_out_st32_12(s, h.cond, datalo, TCG_REG_TMP, 0);
1706            tcg_out_st32_12(s, h.cond, datahi, TCG_REG_TMP, 4);
1707        }
1708        break;
1709    default:
1710        g_assert_not_reached();
1711    }
1712}
1713
1714static void tgen_qemu_st(TCGContext *s, TCGType type, TCGReg data,
1715                         TCGReg addr, MemOpIdx oi)
1716{
1717    MemOp opc = get_memop(oi);
1718    TCGLabelQemuLdst *ldst;
1719    HostAddress h;
1720
1721    ldst = prepare_host_addr(s, &h, addr, oi, false);
1722    if (ldst) {
1723        ldst->type = type;
1724        ldst->datalo_reg = data;
1725        ldst->datahi_reg = -1;
1726
1727        h.cond = COND_EQ;
1728        tcg_out_qemu_st_direct(s, opc, data, -1, h);
1729
1730        /* The conditional call is last, as we're going to return here. */
1731        ldst->label_ptr[0] = s->code_ptr;
1732        tcg_out_bl_imm(s, COND_NE, 0);
1733        ldst->raddr = tcg_splitwx_to_rx(s->code_ptr);
1734    } else {
1735        tcg_out_qemu_st_direct(s, opc, data, -1, h);
1736    }
1737}
1738
1739static const TCGOutOpQemuLdSt outop_qemu_st = {
1740    .base.static_constraint = C_O0_I2(q, q),
1741    .out = tgen_qemu_st,
1742};
1743
1744static void tgen_qemu_st2(TCGContext *s, TCGType type, TCGReg datalo,
1745                          TCGReg datahi, TCGReg addr, MemOpIdx oi)
1746{
1747    MemOp opc = get_memop(oi);
1748    TCGLabelQemuLdst *ldst;
1749    HostAddress h;
1750
1751    ldst = prepare_host_addr(s, &h, addr, oi, false);
1752    if (ldst) {
1753        ldst->type = type;
1754        ldst->datalo_reg = datalo;
1755        ldst->datahi_reg = datahi;
1756
1757        h.cond = COND_EQ;
1758        tcg_out_qemu_st_direct(s, opc, datalo, datahi, h);
1759
1760        /* The conditional call is last, as we're going to return here. */
1761        ldst->label_ptr[0] = s->code_ptr;
1762        tcg_out_bl_imm(s, COND_NE, 0);
1763        ldst->raddr = tcg_splitwx_to_rx(s->code_ptr);
1764    } else {
1765        tcg_out_qemu_st_direct(s, opc, datalo, datahi, h);
1766    }
1767}
1768
1769static const TCGOutOpQemuLdSt2 outop_qemu_st2 = {
1770    .base.static_constraint = C_O0_I3(Q, p, q),
1771    .out = tgen_qemu_st2,
1772};
1773
1774static void tcg_out_epilogue(TCGContext *s);
1775
1776static void tcg_out_exit_tb(TCGContext *s, uintptr_t arg)
1777{
1778    tcg_out_movi(s, TCG_TYPE_PTR, TCG_REG_R0, arg);
1779    tcg_out_epilogue(s);
1780}
1781
1782static void tcg_out_goto_tb(TCGContext *s, int which)
1783{
1784    uintptr_t i_addr;
1785    intptr_t i_disp;
1786
1787    /* Direct branch will be patched by tb_target_set_jmp_target. */
1788    set_jmp_insn_offset(s, which);
1789    tcg_out32(s, INSN_NOP);
1790
1791    /* When branch is out of range, fall through to indirect. */
1792    i_addr = get_jmp_target_addr(s, which);
1793    i_disp = tcg_pcrel_diff(s, (void *)i_addr) - 8;
1794    tcg_debug_assert(i_disp < 0);
1795    if (i_disp >= -0xfff) {
1796        tcg_out_ld32_12(s, COND_AL, TCG_REG_PC, TCG_REG_PC, i_disp);
1797    } else {
1798        /*
1799         * The TB is close, but outside the 12 bits addressable by
1800         * the load.  We can extend this to 20 bits with a sub of a
1801         * shifted immediate from pc.
1802         */
1803        int h = -i_disp;
1804        int l = -(h & 0xfff);
1805
1806        h = encode_imm_nofail(h + l);
1807        tcg_out_dat_imm(s, COND_AL, ARITH_SUB, TCG_REG_R0, TCG_REG_PC, h);
1808        tcg_out_ld32_12(s, COND_AL, TCG_REG_PC, TCG_REG_R0, l);
1809    }
1810    set_jmp_reset_offset(s, which);
1811}
1812
1813static void tcg_out_goto_ptr(TCGContext *s, TCGReg a0)
1814{
1815    tcg_out_b_reg(s, COND_AL, a0);
1816}
1817
1818void tb_target_set_jmp_target(const TranslationBlock *tb, int n,
1819                              uintptr_t jmp_rx, uintptr_t jmp_rw)
1820{
1821    uintptr_t addr = tb->jmp_target_addr[n];
1822    ptrdiff_t offset = addr - (jmp_rx + 8);
1823    tcg_insn_unit insn;
1824
1825    /* Either directly branch, or fall through to indirect branch. */
1826    if (offset == sextract64(offset, 0, 26)) {
1827        /* B <addr> */
1828        insn = deposit32((COND_AL << 28) | INSN_B, 0, 24, offset >> 2);
1829    } else {
1830        insn = INSN_NOP;
1831    }
1832
1833    qatomic_set((uint32_t *)jmp_rw, insn);
1834    flush_idcache_range(jmp_rx, jmp_rw, 4);
1835}
1836
1837
1838static void tgen_add(TCGContext *s, TCGType type,
1839                     TCGReg a0, TCGReg a1, TCGReg a2)
1840{
1841    tcg_out_dat_reg(s, COND_AL, ARITH_ADD, a0, a1, a2, SHIFT_IMM_LSL(0));
1842}
1843
1844static void tgen_addi(TCGContext *s, TCGType type,
1845                      TCGReg a0, TCGReg a1, tcg_target_long a2)
1846{
1847    tcg_out_dat_IN(s, COND_AL, ARITH_ADD, ARITH_SUB, a0, a1, a2);
1848}
1849
1850static const TCGOutOpBinary outop_add = {
1851    .base.static_constraint = C_O1_I2(r, r, rIN),
1852    .out_rrr = tgen_add,
1853    .out_rri = tgen_addi,
1854};
1855
1856static void tgen_addco(TCGContext *s, TCGType type,
1857                       TCGReg a0, TCGReg a1, TCGReg a2)
1858{
1859    tcg_out_dat_reg(s, COND_AL, ARITH_ADD | TO_CPSR,
1860                    a0, a1, a2, SHIFT_IMM_LSL(0));
1861}
1862
1863static void tgen_addco_imm(TCGContext *s, TCGType type,
1864                           TCGReg a0, TCGReg a1, tcg_target_long a2)
1865{
1866    tcg_out_dat_IN(s, COND_AL, ARITH_ADD | TO_CPSR, ARITH_SUB | TO_CPSR,
1867                   a0, a1, a2);
1868}
1869
1870static const TCGOutOpBinary outop_addco = {
1871    .base.static_constraint = C_O1_I2(r, r, rIN),
1872    .out_rrr = tgen_addco,
1873    .out_rri = tgen_addco_imm,
1874};
1875
1876static void tgen_addci(TCGContext *s, TCGType type,
1877                       TCGReg a0, TCGReg a1, TCGReg a2)
1878{
1879    tcg_out_dat_reg(s, COND_AL, ARITH_ADC, a0, a1, a2, SHIFT_IMM_LSL(0));
1880}
1881
1882static void tgen_addci_imm(TCGContext *s, TCGType type,
1883                           TCGReg a0, TCGReg a1, tcg_target_long a2)
1884{
1885    tcg_out_dat_IK(s, COND_AL, ARITH_ADC, ARITH_SBC, a0, a1, a2);
1886}
1887
1888static const TCGOutOpAddSubCarry outop_addci = {
1889    .base.static_constraint = C_O1_I2(r, r, rIK),
1890    .out_rrr = tgen_addci,
1891    .out_rri = tgen_addci_imm,
1892};
1893
1894static void tgen_addcio(TCGContext *s, TCGType type,
1895                        TCGReg a0, TCGReg a1, TCGReg a2)
1896{
1897    tcg_out_dat_reg(s, COND_AL, ARITH_ADC | TO_CPSR,
1898                    a0, a1, a2, SHIFT_IMM_LSL(0));
1899}
1900
1901static void tgen_addcio_imm(TCGContext *s, TCGType type,
1902                            TCGReg a0, TCGReg a1, tcg_target_long a2)
1903{
1904    tcg_out_dat_IK(s, COND_AL, ARITH_ADC | TO_CPSR, ARITH_SBC | TO_CPSR,
1905                   a0, a1, a2);
1906}
1907
1908static const TCGOutOpBinary outop_addcio = {
1909    .base.static_constraint = C_O1_I2(r, r, rIK),
1910    .out_rrr = tgen_addcio,
1911    .out_rri = tgen_addcio_imm,
1912};
1913
1914/* Set C to @c; NZVQ all set to 0. */
1915static void tcg_out_movi_apsr_c(TCGContext *s, bool c)
1916{
1917    int imm12 = encode_imm_nofail(c << 29);
1918    tcg_out32(s, (COND_AL << 28) | INSN_MSRI_CPSR | 0x80000 | imm12);
1919}
1920
1921static void tcg_out_set_carry(TCGContext *s)
1922{
1923    tcg_out_movi_apsr_c(s, 1);
1924}
1925
1926static void tgen_and(TCGContext *s, TCGType type,
1927                     TCGReg a0, TCGReg a1, TCGReg a2)
1928{
1929    tcg_out_dat_reg(s, COND_AL, ARITH_AND, a0, a1, a2, SHIFT_IMM_LSL(0));
1930}
1931
1932static void tgen_andi(TCGContext *s, TCGType type,
1933                      TCGReg a0, TCGReg a1, tcg_target_long a2)
1934{
1935    tcg_out_dat_IK(s, COND_AL, ARITH_AND, ARITH_BIC, a0, a1, a2);
1936}
1937
1938static const TCGOutOpBinary outop_and = {
1939    .base.static_constraint = C_O1_I2(r, r, rIK),
1940    .out_rrr = tgen_and,
1941    .out_rri = tgen_andi,
1942};
1943
1944static void tgen_andc(TCGContext *s, TCGType type,
1945                      TCGReg a0, TCGReg a1, TCGReg a2)
1946{
1947    tcg_out_dat_reg(s, COND_AL, ARITH_BIC, a0, a1, a2, SHIFT_IMM_LSL(0));
1948}
1949
1950static const TCGOutOpBinary outop_andc = {
1951    .base.static_constraint = C_O1_I2(r, r, r),
1952    .out_rrr = tgen_andc,
1953};
1954
1955static void tgen_clz(TCGContext *s, TCGType type,
1956                     TCGReg a0, TCGReg a1, TCGReg a2)
1957{
1958    tcg_out_dat_imm(s, COND_AL, ARITH_CMP, 0, a1, 0);
1959    tcg_out_dat_reg(s, COND_NE, INSN_CLZ, a0, 0, a1, 0);
1960    tcg_out_mov_reg(s, COND_EQ, a0, a2);
1961}
1962
1963static void tgen_clzi(TCGContext *s, TCGType type,
1964                      TCGReg a0, TCGReg a1, tcg_target_long a2)
1965{
1966    if (a2 == 32) {
1967        tcg_out_dat_reg(s, COND_AL, INSN_CLZ, a0, 0, a1, 0);
1968    } else {
1969        tcg_out_dat_imm(s, COND_AL, ARITH_CMP, 0, a1, 0);
1970        tcg_out_dat_reg(s, COND_NE, INSN_CLZ, a0, 0, a1, 0);
1971        tcg_out_movi32(s, COND_EQ, a0, a2);
1972    }
1973}
1974
1975static const TCGOutOpBinary outop_clz = {
1976    .base.static_constraint = C_O1_I2(r, r, rIK),
1977    .out_rrr = tgen_clz,
1978    .out_rri = tgen_clzi,
1979};
1980
1981static const TCGOutOpUnary outop_ctpop = {
1982    .base.static_constraint = C_NotImplemented,
1983};
1984
1985static void tgen_ctz(TCGContext *s, TCGType type,
1986                     TCGReg a0, TCGReg a1, TCGReg a2)
1987{
1988    tcg_out_dat_reg(s, COND_AL, INSN_RBIT, TCG_REG_TMP, 0, a1, 0);
1989    tgen_clz(s, TCG_TYPE_I32, a0, TCG_REG_TMP, a2);
1990}
1991
1992static void tgen_ctzi(TCGContext *s, TCGType type,
1993                      TCGReg a0, TCGReg a1, tcg_target_long a2)
1994{
1995    tcg_out_dat_reg(s, COND_AL, INSN_RBIT, TCG_REG_TMP, 0, a1, 0);
1996    tgen_clzi(s, TCG_TYPE_I32, a0, TCG_REG_TMP, a2);
1997}
1998
1999static TCGConstraintSetIndex cset_ctz(TCGType type, unsigned flags)
2000{
2001    return use_armv7_instructions ? C_O1_I2(r, r, rIK) : C_NotImplemented;
2002}
2003
2004static const TCGOutOpBinary outop_ctz = {
2005    .base.static_constraint = C_Dynamic,
2006    .base.dynamic_constraint = cset_ctz,
2007    .out_rrr = tgen_ctz,
2008    .out_rri = tgen_ctzi,
2009};
2010
2011static TCGConstraintSetIndex cset_idiv(TCGType type, unsigned flags)
2012{
2013    return use_idiv_instructions ? C_O1_I2(r, r, r) : C_NotImplemented;
2014}
2015
2016static void tgen_divs(TCGContext *s, TCGType type,
2017                      TCGReg a0, TCGReg a1, TCGReg a2)
2018{
2019    /* sdiv */
2020    tcg_out32(s, 0x0710f010 | (COND_AL << 28) | (a0 << 16) | a1 | (a2 << 8));
2021}
2022
2023static const TCGOutOpBinary outop_divs = {
2024    .base.static_constraint = C_Dynamic,
2025    .base.dynamic_constraint = cset_idiv,
2026    .out_rrr = tgen_divs,
2027};
2028
2029static const TCGOutOpDivRem outop_divs2 = {
2030    .base.static_constraint = C_NotImplemented,
2031};
2032
2033static void tgen_divu(TCGContext *s, TCGType type,
2034                      TCGReg a0, TCGReg a1, TCGReg a2)
2035{
2036    /* udiv */
2037    tcg_out32(s, 0x0730f010 | (COND_AL << 28) | (a0 << 16) | a1 | (a2 << 8));
2038}
2039
2040static const TCGOutOpBinary outop_divu = {
2041    .base.static_constraint = C_Dynamic,
2042    .base.dynamic_constraint = cset_idiv,
2043    .out_rrr = tgen_divu,
2044};
2045
2046static const TCGOutOpDivRem outop_divu2 = {
2047    .base.static_constraint = C_NotImplemented,
2048};
2049
2050static const TCGOutOpBinary outop_eqv = {
2051    .base.static_constraint = C_NotImplemented,
2052};
2053
2054static void tgen_mul(TCGContext *s, TCGType type,
2055                     TCGReg a0, TCGReg a1, TCGReg a2)
2056{
2057    /* mul */
2058    tcg_out32(s, (COND_AL << 28) | 0x90 | (a0 << 16) | (a1 << 8) | a2);
2059}
2060
2061static const TCGOutOpBinary outop_mul = {
2062    .base.static_constraint = C_O1_I2(r, r, r),
2063    .out_rrr = tgen_mul,
2064};
2065
2066static void tgen_muls2(TCGContext *s, TCGType type,
2067                       TCGReg rd0, TCGReg rd1, TCGReg rn, TCGReg rm)
2068{
2069    /* smull */
2070    tcg_out32(s, (COND_AL << 28) | 0x00c00090 |
2071              (rd1 << 16) | (rd0 << 12) | (rm << 8) | rn);
2072}
2073
2074static const TCGOutOpMul2 outop_muls2 = {
2075    .base.static_constraint = C_O2_I2(r, r, r, r),
2076    .out_rrrr = tgen_muls2,
2077};
2078
2079static const TCGOutOpBinary outop_mulsh = {
2080    .base.static_constraint = C_NotImplemented,
2081};
2082
2083static void tgen_mulu2(TCGContext *s, TCGType type,
2084                       TCGReg rd0, TCGReg rd1, TCGReg rn, TCGReg rm)
2085{
2086    /* umull */
2087    tcg_out32(s, (COND_AL << 28) | 0x00800090 |
2088              (rd1 << 16) | (rd0 << 12) | (rm << 8) | rn);
2089}
2090
2091static const TCGOutOpMul2 outop_mulu2 = {
2092    .base.static_constraint = C_O2_I2(r, r, r, r),
2093    .out_rrrr = tgen_mulu2,
2094};
2095
2096static const TCGOutOpBinary outop_muluh = {
2097    .base.static_constraint = C_NotImplemented,
2098};
2099
2100static const TCGOutOpBinary outop_nand = {
2101    .base.static_constraint = C_NotImplemented,
2102};
2103
2104static const TCGOutOpBinary outop_nor = {
2105    .base.static_constraint = C_NotImplemented,
2106};
2107
2108static void tgen_or(TCGContext *s, TCGType type,
2109                     TCGReg a0, TCGReg a1, TCGReg a2)
2110{
2111    tcg_out_dat_reg(s, COND_AL, ARITH_ORR, a0, a1, a2, SHIFT_IMM_LSL(0));
2112}
2113
2114static void tgen_ori(TCGContext *s, TCGType type,
2115                     TCGReg a0, TCGReg a1, tcg_target_long a2)
2116{
2117    tcg_out_dat_imm(s, COND_AL, ARITH_ORR, a0, a1, encode_imm_nofail(a2));
2118}
2119
2120static const TCGOutOpBinary outop_or = {
2121    .base.static_constraint = C_O1_I2(r, r, rI),
2122    .out_rrr = tgen_or,
2123    .out_rri = tgen_ori,
2124};
2125
2126static const TCGOutOpBinary outop_orc = {
2127    .base.static_constraint = C_NotImplemented,
2128};
2129
2130static const TCGOutOpBinary outop_rems = {
2131    .base.static_constraint = C_NotImplemented,
2132};
2133
2134static const TCGOutOpBinary outop_remu = {
2135    .base.static_constraint = C_NotImplemented,
2136};
2137
2138static const TCGOutOpBinary outop_rotl = {
2139    .base.static_constraint = C_NotImplemented,
2140};
2141
2142static void tgen_rotr(TCGContext *s, TCGType type,
2143                      TCGReg a0, TCGReg a1, TCGReg a2)
2144{
2145    tcg_out_dat_reg(s, COND_AL, ARITH_MOV, a0, 0, a1, SHIFT_REG_ROR(a2));
2146}
2147
2148static void tgen_rotri(TCGContext *s, TCGType type,
2149                       TCGReg a0, TCGReg a1, tcg_target_long a2)
2150{
2151    tcg_out_dat_reg(s, COND_AL, ARITH_MOV, a0, 0, a1, SHIFT_IMM_ROR(a2 & 0x1f));
2152}
2153
2154static const TCGOutOpBinary outop_rotr = {
2155    .base.static_constraint = C_O1_I2(r, r, ri),
2156    .out_rrr = tgen_rotr,
2157    .out_rri = tgen_rotri,
2158};
2159
2160static void tgen_sar(TCGContext *s, TCGType type,
2161                     TCGReg a0, TCGReg a1, TCGReg a2)
2162{
2163    tcg_out_dat_reg(s, COND_AL, ARITH_MOV, a0, 0, a1, SHIFT_REG_ASR(a2));
2164}
2165
2166static void tgen_sari(TCGContext *s, TCGType type,
2167                      TCGReg a0, TCGReg a1, tcg_target_long a2)
2168{
2169    tcg_out_dat_reg(s, COND_AL, ARITH_MOV, a0, 0, a1,
2170                    SHIFT_IMM_ASR(a2 & 0x1f));
2171}
2172
2173static const TCGOutOpBinary outop_sar = {
2174    .base.static_constraint = C_O1_I2(r, r, ri),
2175    .out_rrr = tgen_sar,
2176    .out_rri = tgen_sari,
2177};
2178
2179static void tgen_shl(TCGContext *s, TCGType type,
2180                     TCGReg a0, TCGReg a1, TCGReg a2)
2181{
2182    tcg_out_dat_reg(s, COND_AL, ARITH_MOV, a0, 0, a1, SHIFT_REG_LSL(a2));
2183}
2184
2185static void tgen_shli(TCGContext *s, TCGType type,
2186                      TCGReg a0, TCGReg a1, tcg_target_long a2)
2187{
2188    tcg_out_dat_reg(s, COND_AL, ARITH_MOV, a0, 0, a1,
2189                    SHIFT_IMM_LSL(a2 & 0x1f));
2190}
2191
2192static const TCGOutOpBinary outop_shl = {
2193    .base.static_constraint = C_O1_I2(r, r, ri),
2194    .out_rrr = tgen_shl,
2195    .out_rri = tgen_shli,
2196};
2197
2198static void tgen_shr(TCGContext *s, TCGType type,
2199                     TCGReg a0, TCGReg a1, TCGReg a2)
2200{
2201    tcg_out_dat_reg(s, COND_AL, ARITH_MOV, a0, 0, a1, SHIFT_REG_LSR(a2));
2202}
2203
2204static void tgen_shri(TCGContext *s, TCGType type,
2205                      TCGReg a0, TCGReg a1, tcg_target_long a2)
2206{
2207    tcg_out_dat_reg(s, COND_AL, ARITH_MOV, a0, 0, a1,
2208                    SHIFT_IMM_LSR(a2 & 0x1f));
2209}
2210
2211static const TCGOutOpBinary outop_shr = {
2212    .base.static_constraint = C_O1_I2(r, r, ri),
2213    .out_rrr = tgen_shr,
2214    .out_rri = tgen_shri,
2215};
2216
2217static void tgen_sub(TCGContext *s, TCGType type,
2218                     TCGReg a0, TCGReg a1, TCGReg a2)
2219{
2220    tcg_out_dat_reg(s, COND_AL, ARITH_SUB, a0, a1, a2, SHIFT_IMM_LSL(0));
2221}
2222
2223static void tgen_subfi(TCGContext *s, TCGType type,
2224                       TCGReg a0, tcg_target_long a1, TCGReg a2)
2225{
2226    tcg_out_dat_imm(s, COND_AL, ARITH_RSB, a0, a2, encode_imm_nofail(a1));
2227}
2228
2229static const TCGOutOpSubtract outop_sub = {
2230    .base.static_constraint = C_O1_I2(r, rI, r),
2231    .out_rrr = tgen_sub,
2232    .out_rir = tgen_subfi,
2233};
2234
2235static void tgen_subbo_rrr(TCGContext *s, TCGType type,
2236                           TCGReg a0, TCGReg a1, TCGReg a2)
2237{
2238    tcg_out_dat_reg(s, COND_AL, ARITH_SUB | TO_CPSR,
2239                    a0, a1, a2, SHIFT_IMM_LSL(0));
2240}
2241
2242static void tgen_subbo_rri(TCGContext *s, TCGType type,
2243                           TCGReg a0, TCGReg a1, tcg_target_long a2)
2244{
2245    tcg_out_dat_IN(s, COND_AL, ARITH_SUB | TO_CPSR, ARITH_ADD | TO_CPSR,
2246                   a0, a1, a2);
2247}
2248
2249static void tgen_subbo_rir(TCGContext *s, TCGType type,
2250                           TCGReg a0, tcg_target_long a1, TCGReg a2)
2251{
2252    tcg_out_dat_imm(s, COND_AL, ARITH_RSB | TO_CPSR,
2253                    a0, a2, encode_imm_nofail(a1));
2254}
2255
2256static void tgen_subbo_rii(TCGContext *s, TCGType type,
2257                           TCGReg a0, tcg_target_long a1, tcg_target_long a2)
2258{
2259    tcg_out_movi(s, TCG_TYPE_I32, TCG_REG_TMP, a2);
2260    tgen_subbo_rir(s, TCG_TYPE_I32, a0, a1, TCG_REG_TMP);
2261}
2262
2263static const TCGOutOpAddSubCarry outop_subbo = {
2264    .base.static_constraint = C_O1_I2(r, rI, rIN),
2265    .out_rrr = tgen_subbo_rrr,
2266    .out_rri = tgen_subbo_rri,
2267    .out_rir = tgen_subbo_rir,
2268    .out_rii = tgen_subbo_rii,
2269};
2270
2271static void tgen_subbi_rrr(TCGContext *s, TCGType type,
2272                           TCGReg a0, TCGReg a1, TCGReg a2)
2273{
2274    tcg_out_dat_reg(s, COND_AL, ARITH_SBC,
2275                    a0, a1, a2, SHIFT_IMM_LSL(0));
2276}
2277
2278static void tgen_subbi_rri(TCGContext *s, TCGType type,
2279                           TCGReg a0, TCGReg a1, tcg_target_long a2)
2280{
2281    tcg_out_dat_IK(s, COND_AL, ARITH_SBC, ARITH_ADC, a0, a1, a2);
2282}
2283
2284static void tgen_subbi_rir(TCGContext *s, TCGType type,
2285                           TCGReg a0, tcg_target_long a1, TCGReg a2)
2286{
2287    tcg_out_dat_imm(s, COND_AL, ARITH_RSC, a0, a2, encode_imm_nofail(a1));
2288}
2289
2290static void tgen_subbi_rii(TCGContext *s, TCGType type,
2291                           TCGReg a0, tcg_target_long a1, tcg_target_long a2)
2292{
2293    tcg_out_movi(s, TCG_TYPE_I32, TCG_REG_TMP, a2);
2294    tgen_subbi_rir(s, TCG_TYPE_I32, a0, a1, TCG_REG_TMP);
2295}
2296
2297static const TCGOutOpAddSubCarry outop_subbi = {
2298    .base.static_constraint = C_O1_I2(r, rI, rIK),
2299    .out_rrr = tgen_subbi_rrr,
2300    .out_rri = tgen_subbi_rri,
2301    .out_rir = tgen_subbi_rir,
2302    .out_rii = tgen_subbi_rii,
2303};
2304
2305static void tgen_subbio_rrr(TCGContext *s, TCGType type,
2306                            TCGReg a0, TCGReg a1, TCGReg a2)
2307{
2308    tcg_out_dat_reg(s, COND_AL, ARITH_SBC | TO_CPSR,
2309                    a0, a1, a2, SHIFT_IMM_LSL(0));
2310}
2311
2312static void tgen_subbio_rri(TCGContext *s, TCGType type,
2313                            TCGReg a0, TCGReg a1, tcg_target_long a2)
2314{
2315    tcg_out_dat_IK(s, COND_AL, ARITH_SBC | TO_CPSR, ARITH_ADC | TO_CPSR,
2316                   a0, a1, a2);
2317}
2318
2319static void tgen_subbio_rir(TCGContext *s, TCGType type,
2320                            TCGReg a0, tcg_target_long a1, TCGReg a2)
2321{
2322    tcg_out_dat_imm(s, COND_AL, ARITH_RSC | TO_CPSR,
2323                    a0, a2, encode_imm_nofail(a1));
2324}
2325
2326static void tgen_subbio_rii(TCGContext *s, TCGType type,
2327                            TCGReg a0, tcg_target_long a1, tcg_target_long a2)
2328{
2329    tcg_out_movi(s, TCG_TYPE_I32, TCG_REG_TMP, a2);
2330    tgen_subbio_rir(s, TCG_TYPE_I32, a0, a1, TCG_REG_TMP);
2331}
2332
2333static const TCGOutOpAddSubCarry outop_subbio = {
2334    .base.static_constraint = C_O1_I2(r, rI, rIK),
2335    .out_rrr = tgen_subbio_rrr,
2336    .out_rri = tgen_subbio_rri,
2337    .out_rir = tgen_subbio_rir,
2338    .out_rii = tgen_subbio_rii,
2339};
2340
2341static void tcg_out_set_borrow(TCGContext *s)
2342{
2343    tcg_out_movi_apsr_c(s, 0);  /* borrow = !carry */
2344}
2345
2346static void tgen_xor(TCGContext *s, TCGType type,
2347                     TCGReg a0, TCGReg a1, TCGReg a2)
2348{
2349    tcg_out_dat_reg(s, COND_AL, ARITH_EOR, a0, a1, a2, SHIFT_IMM_LSL(0));
2350}
2351
2352static void tgen_xori(TCGContext *s, TCGType type,
2353                      TCGReg a0, TCGReg a1, tcg_target_long a2)
2354{
2355    tcg_out_dat_imm(s, COND_AL, ARITH_EOR, a0, a1, encode_imm_nofail(a2));
2356}
2357
2358static const TCGOutOpBinary outop_xor = {
2359    .base.static_constraint = C_O1_I2(r, r, rI),
2360    .out_rrr = tgen_xor,
2361    .out_rri = tgen_xori,
2362};
2363
2364static void tgen_bswap16(TCGContext *s, TCGType type,
2365                         TCGReg rd, TCGReg rn, unsigned flags)
2366{
2367    if (flags & TCG_BSWAP_OS) {
2368        /* revsh */
2369        tcg_out32(s, 0x06ff0fb0 | (COND_AL << 28) | (rd << 12) | rn);
2370        return;
2371    }
2372
2373    /* rev16 */
2374    tcg_out32(s, 0x06bf0fb0 | (COND_AL << 28) | (rd << 12) | rn);
2375    if ((flags & (TCG_BSWAP_IZ | TCG_BSWAP_OZ)) == TCG_BSWAP_OZ) {
2376        tcg_out_ext16u(s, rd, rd);
2377    }
2378}
2379
2380static const TCGOutOpBswap outop_bswap16 = {
2381    .base.static_constraint = C_O1_I1(r, r),
2382    .out_rr = tgen_bswap16,
2383};
2384
2385static void tgen_bswap32(TCGContext *s, TCGType type,
2386                         TCGReg rd, TCGReg rn, unsigned flags)
2387{
2388    /* rev */
2389    tcg_out32(s, 0x06bf0f30 | (COND_AL << 28) | (rd << 12) | rn);
2390}
2391
2392static const TCGOutOpBswap outop_bswap32 = {
2393    .base.static_constraint = C_O1_I1(r, r),
2394    .out_rr = tgen_bswap32,
2395};
2396
2397static const TCGOutOpUnary outop_bswap64 = {
2398    .base.static_constraint = C_NotImplemented,
2399};
2400
2401static void tgen_neg(TCGContext *s, TCGType type, TCGReg a0, TCGReg a1)
2402{
2403    tgen_subfi(s, type, a0, 0, a1);
2404}
2405
2406static const TCGOutOpUnary outop_neg = {
2407    .base.static_constraint = C_O1_I1(r, r),
2408    .out_rr = tgen_neg,
2409};
2410
2411static void tgen_not(TCGContext *s, TCGType type, TCGReg a0, TCGReg a1)
2412{
2413    tcg_out_dat_reg(s, COND_AL, ARITH_MVN, a0, 0, a1, SHIFT_IMM_LSL(0));
2414}
2415
2416static const TCGOutOpUnary outop_not = {
2417    .base.static_constraint = C_O1_I1(r, r),
2418    .out_rr = tgen_not,
2419};
2420
2421static void tgen_brcond(TCGContext *s, TCGType type, TCGCond cond,
2422                        TCGReg a0, TCGReg a1, TCGLabel *l)
2423{
2424    cond = tgen_cmp(s, cond, a0, a1);
2425    tcg_out_goto_label(s, tcg_cond_to_arm_cond[cond], l);
2426}
2427
2428static void tgen_brcondi(TCGContext *s, TCGType type, TCGCond cond,
2429                         TCGReg a0, tcg_target_long a1, TCGLabel *l)
2430{
2431    cond = tgen_cmpi(s, cond, a0, a1);
2432    tcg_out_goto_label(s, tcg_cond_to_arm_cond[cond], l);
2433}
2434
2435static const TCGOutOpBrcond outop_brcond = {
2436    .base.static_constraint = C_O0_I2(r, rIN),
2437    .out_rr = tgen_brcond,
2438    .out_ri = tgen_brcondi,
2439};
2440
2441static void finish_setcond(TCGContext *s, TCGCond cond, TCGReg ret, bool neg)
2442{
2443    tcg_out_movi32(s, tcg_cond_to_arm_cond[tcg_invert_cond(cond)], ret, 0);
2444    tcg_out_movi32(s, tcg_cond_to_arm_cond[cond], ret, neg ? -1 : 1);
2445}
2446
2447static void tgen_setcond(TCGContext *s, TCGType type, TCGCond cond,
2448                         TCGReg a0, TCGReg a1, TCGReg a2)
2449{
2450    cond = tgen_cmp(s, cond, a1, a2);
2451    finish_setcond(s, cond, a0, false);
2452}
2453
2454static void tgen_setcondi(TCGContext *s, TCGType type, TCGCond cond,
2455                          TCGReg a0, TCGReg a1, tcg_target_long a2)
2456{
2457    cond = tgen_cmpi(s, cond, a1, a2);
2458    finish_setcond(s, cond, a0, false);
2459}
2460
2461static const TCGOutOpSetcond outop_setcond = {
2462    .base.static_constraint = C_O1_I2(r, r, rIN),
2463    .out_rrr = tgen_setcond,
2464    .out_rri = tgen_setcondi,
2465};
2466
2467static void tgen_negsetcond(TCGContext *s, TCGType type, TCGCond cond,
2468                            TCGReg a0, TCGReg a1, TCGReg a2)
2469{
2470    cond = tgen_cmp(s, cond, a1, a2);
2471    finish_setcond(s, cond, a0, true);
2472}
2473
2474static void tgen_negsetcondi(TCGContext *s, TCGType type, TCGCond cond,
2475                             TCGReg a0, TCGReg a1, tcg_target_long a2)
2476{
2477    cond = tgen_cmpi(s, cond, a1, a2);
2478    finish_setcond(s, cond, a0, true);
2479}
2480
2481static const TCGOutOpSetcond outop_negsetcond = {
2482    .base.static_constraint = C_O1_I2(r, r, rIN),
2483    .out_rrr = tgen_negsetcond,
2484    .out_rri = tgen_negsetcondi,
2485};
2486
2487static void tgen_movcond(TCGContext *s, TCGType type, TCGCond cond,
2488                         TCGReg ret, TCGReg c1, TCGArg c2, bool const_c2,
2489                         TCGArg vt, bool const_vt, TCGArg vf, bool consf_vf)
2490{
2491    cond = tcg_out_cmp(s, cond, c1, c2, const_c2);
2492    tcg_out_dat_rIK(s, tcg_cond_to_arm_cond[cond], ARITH_MOV, ARITH_MVN,
2493                    ret, 0, vt, const_vt);
2494}
2495
2496static const TCGOutOpMovcond outop_movcond = {
2497    .base.static_constraint = C_O1_I4(r, r, rIN, rIK, 0),
2498    .out = tgen_movcond,
2499};
2500
2501static void tgen_brcond2(TCGContext *s, TCGCond cond, TCGReg al, TCGReg ah,
2502                         TCGArg bl, bool const_bl, TCGArg bh, bool const_bh,
2503                         TCGLabel *l)
2504{
2505    cond = tcg_out_cmp2(s, cond, al, ah, bl, const_bl, bh, const_bh);
2506    tcg_out_goto_label(s, tcg_cond_to_arm_cond[cond], l);
2507}
2508
2509static const TCGOutOpBrcond2 outop_brcond2 = {
2510    .base.static_constraint = C_O0_I4(r, r, rI, rI),
2511    .out = tgen_brcond2,
2512};
2513
2514static void tgen_setcond2(TCGContext *s, TCGCond cond, TCGReg ret,
2515                          TCGReg al, TCGReg ah,
2516                          TCGArg bl, bool const_bl,
2517                          TCGArg bh, bool const_bh)
2518{
2519    cond = tcg_out_cmp2(s, cond, al, ah, bl, const_bl, bh, const_bh);
2520    finish_setcond(s, cond, ret, false);
2521}
2522
2523static const TCGOutOpSetcond2 outop_setcond2 = {
2524    .base.static_constraint = C_O1_I4(r, r, r, rI, rI),
2525    .out = tgen_setcond2,
2526};
2527
2528static void tgen_extract2(TCGContext *s, TCGType type, TCGReg a0,
2529                          TCGReg a1, TCGReg a2, unsigned shr)
2530{
2531    /* We can do extract2 in 2 insns, vs the 3 required otherwise.  */
2532    tgen_shli(s, TCG_TYPE_I32, TCG_REG_TMP, a2, 32 - shr);
2533    tcg_out_dat_reg(s, COND_AL, ARITH_ORR, a0, TCG_REG_TMP,
2534                    a1, SHIFT_IMM_LSR(shr));
2535}
2536
2537static const TCGOutOpExtract2 outop_extract2 = {
2538    .base.static_constraint = C_O1_I2(r, r, r),
2539    .out_rrr = tgen_extract2,
2540};
2541
2542static void tgen_ld8u(TCGContext *s, TCGType type, TCGReg rd,
2543                      TCGReg rn, ptrdiff_t offset)
2544{
2545    if (offset > 0xfff || offset < -0xfff) {
2546        tcg_out_movi32(s, COND_AL, TCG_REG_TMP, offset);
2547        tcg_out_ld8_r(s, COND_AL, rd, rn, TCG_REG_TMP);
2548    } else {
2549        tcg_out_ld8_12(s, COND_AL, rd, rn, offset);
2550    }
2551}
2552
2553static const TCGOutOpLoad outop_ld8u = {
2554    .base.static_constraint = C_O1_I1(r, r),
2555    .out = tgen_ld8u,
2556};
2557
2558static void tgen_ld8s(TCGContext *s, TCGType type, TCGReg rd,
2559                      TCGReg rn, ptrdiff_t offset)
2560{
2561    if (offset > 0xff || offset < -0xff) {
2562        tcg_out_movi32(s, COND_AL, TCG_REG_TMP, offset);
2563        tcg_out_ld8s_r(s, COND_AL, rd, rn, TCG_REG_TMP);
2564    } else {
2565        tcg_out_ld8s_8(s, COND_AL, rd, rn, offset);
2566    }
2567}
2568
2569static const TCGOutOpLoad outop_ld8s = {
2570    .base.static_constraint = C_O1_I1(r, r),
2571    .out = tgen_ld8s,
2572};
2573
2574static void tgen_ld16u(TCGContext *s, TCGType type, TCGReg rd,
2575                       TCGReg rn, ptrdiff_t offset)
2576{
2577    if (offset > 0xff || offset < -0xff) {
2578        tcg_out_movi32(s, COND_AL, TCG_REG_TMP, offset);
2579        tcg_out_ld16u_r(s, COND_AL, rd, rn, TCG_REG_TMP);
2580    } else {
2581        tcg_out_ld16u_8(s, COND_AL, rd, rn, offset);
2582    }
2583}
2584
2585static const TCGOutOpLoad outop_ld16u = {
2586    .base.static_constraint = C_O1_I1(r, r),
2587    .out = tgen_ld16u,
2588};
2589
2590static void tgen_ld16s(TCGContext *s, TCGType type, TCGReg rd,
2591                       TCGReg rn, ptrdiff_t offset)
2592{
2593    if (offset > 0xff || offset < -0xff) {
2594        tcg_out_movi32(s, COND_AL, TCG_REG_TMP, offset);
2595        tcg_out_ld16s_r(s, COND_AL, rd, rn, TCG_REG_TMP);
2596    } else {
2597        tcg_out_ld16s_8(s, COND_AL, rd, rn, offset);
2598    }
2599}
2600
2601static const TCGOutOpLoad outop_ld16s = {
2602    .base.static_constraint = C_O1_I1(r, r),
2603    .out = tgen_ld16s,
2604};
2605
2606static void tgen_st8(TCGContext *s, TCGType type, TCGReg rd,
2607                     TCGReg rn, ptrdiff_t offset)
2608{
2609    if (offset > 0xfff || offset < -0xfff) {
2610        tcg_out_movi32(s, COND_AL, TCG_REG_TMP, offset);
2611        tcg_out_st8_r(s, COND_AL, rd, rn, TCG_REG_TMP);
2612    } else {
2613        tcg_out_st8_12(s, COND_AL, rd, rn, offset);
2614    }
2615}
2616
2617static const TCGOutOpStore outop_st8 = {
2618    .base.static_constraint = C_O0_I2(r, r),
2619    .out_r = tgen_st8,
2620};
2621
2622static void tgen_st16(TCGContext *s, TCGType type, TCGReg rd,
2623                      TCGReg rn, ptrdiff_t offset)
2624{
2625    if (offset > 0xff || offset < -0xff) {
2626        tcg_out_movi32(s, COND_AL, TCG_REG_TMP, offset);
2627        tcg_out_st16_r(s, COND_AL, rd, rn, TCG_REG_TMP);
2628    } else {
2629        tcg_out_st16_8(s, COND_AL, rd, rn, offset);
2630    }
2631}
2632
2633static const TCGOutOpStore outop_st16 = {
2634    .base.static_constraint = C_O0_I2(r, r),
2635    .out_r = tgen_st16,
2636};
2637
2638static const TCGOutOpStore outop_st = {
2639    .base.static_constraint = C_O0_I2(r, r),
2640    .out_r = tcg_out_st,
2641};
2642
2643static TCGConstraintSetIndex
2644tcg_target_op_def(TCGOpcode op, TCGType type, unsigned flags)
2645{
2646    switch (op) {
2647    case INDEX_op_st_vec:
2648        return C_O0_I2(w, r);
2649    case INDEX_op_ld_vec:
2650    case INDEX_op_dupm_vec:
2651        return C_O1_I1(w, r);
2652    case INDEX_op_dup_vec:
2653        return C_O1_I1(w, wr);
2654    case INDEX_op_abs_vec:
2655    case INDEX_op_neg_vec:
2656    case INDEX_op_not_vec:
2657    case INDEX_op_shli_vec:
2658    case INDEX_op_shri_vec:
2659    case INDEX_op_sari_vec:
2660        return C_O1_I1(w, w);
2661    case INDEX_op_dup2_vec:
2662    case INDEX_op_add_vec:
2663    case INDEX_op_mul_vec:
2664    case INDEX_op_smax_vec:
2665    case INDEX_op_smin_vec:
2666    case INDEX_op_ssadd_vec:
2667    case INDEX_op_sssub_vec:
2668    case INDEX_op_sub_vec:
2669    case INDEX_op_umax_vec:
2670    case INDEX_op_umin_vec:
2671    case INDEX_op_usadd_vec:
2672    case INDEX_op_ussub_vec:
2673    case INDEX_op_xor_vec:
2674    case INDEX_op_arm_sshl_vec:
2675    case INDEX_op_arm_ushl_vec:
2676        return C_O1_I2(w, w, w);
2677    case INDEX_op_arm_sli_vec:
2678        return C_O1_I2(w, 0, w);
2679    case INDEX_op_or_vec:
2680    case INDEX_op_andc_vec:
2681        return C_O1_I2(w, w, wO);
2682    case INDEX_op_and_vec:
2683    case INDEX_op_orc_vec:
2684        return C_O1_I2(w, w, wV);
2685    case INDEX_op_cmp_vec:
2686        return C_O1_I2(w, w, wZ);
2687    case INDEX_op_bitsel_vec:
2688        return C_O1_I3(w, w, w, w);
2689    default:
2690        return C_NotImplemented;
2691    }
2692}
2693
2694static void tcg_target_init(TCGContext *s)
2695{
2696    /*
2697     * Only probe for the platform and capabilities if we haven't already
2698     * determined maximum values at compile time.
2699     */
2700#if !defined(use_idiv_instructions) || !defined(use_neon_instructions)
2701    {
2702        unsigned long hwcap = qemu_getauxval(AT_HWCAP);
2703#ifndef use_idiv_instructions
2704        use_idiv_instructions = (hwcap & HWCAP_ARM_IDIVA) != 0;
2705#endif
2706#ifndef use_neon_instructions
2707        use_neon_instructions = (hwcap & HWCAP_ARM_NEON) != 0;
2708#endif
2709    }
2710#endif
2711
2712    if (__ARM_ARCH < 7) {
2713        const char *pl = (const char *)qemu_getauxval(AT_PLATFORM);
2714        if (pl != NULL && pl[0] == 'v' && pl[1] >= '4' && pl[1] <= '9') {
2715            arm_arch = pl[1] - '0';
2716        }
2717
2718        if (arm_arch < 6) {
2719            error_report("TCG: ARMv%d is unsupported; exiting", arm_arch);
2720            exit(EXIT_FAILURE);
2721        }
2722    }
2723
2724    tcg_target_available_regs[TCG_TYPE_I32] = ALL_GENERAL_REGS;
2725
2726    tcg_target_call_clobber_regs = 0;
2727    tcg_regset_set_reg(tcg_target_call_clobber_regs, TCG_REG_R0);
2728    tcg_regset_set_reg(tcg_target_call_clobber_regs, TCG_REG_R1);
2729    tcg_regset_set_reg(tcg_target_call_clobber_regs, TCG_REG_R2);
2730    tcg_regset_set_reg(tcg_target_call_clobber_regs, TCG_REG_R3);
2731    tcg_regset_set_reg(tcg_target_call_clobber_regs, TCG_REG_R12);
2732    tcg_regset_set_reg(tcg_target_call_clobber_regs, TCG_REG_R14);
2733
2734    if (use_neon_instructions) {
2735        tcg_target_available_regs[TCG_TYPE_V64]  = ALL_VECTOR_REGS;
2736        tcg_target_available_regs[TCG_TYPE_V128] = ALL_VECTOR_REGS;
2737
2738        tcg_regset_set_reg(tcg_target_call_clobber_regs, TCG_REG_Q0);
2739        tcg_regset_set_reg(tcg_target_call_clobber_regs, TCG_REG_Q1);
2740        tcg_regset_set_reg(tcg_target_call_clobber_regs, TCG_REG_Q2);
2741        tcg_regset_set_reg(tcg_target_call_clobber_regs, TCG_REG_Q3);
2742        tcg_regset_set_reg(tcg_target_call_clobber_regs, TCG_REG_Q8);
2743        tcg_regset_set_reg(tcg_target_call_clobber_regs, TCG_REG_Q9);
2744        tcg_regset_set_reg(tcg_target_call_clobber_regs, TCG_REG_Q10);
2745        tcg_regset_set_reg(tcg_target_call_clobber_regs, TCG_REG_Q11);
2746        tcg_regset_set_reg(tcg_target_call_clobber_regs, TCG_REG_Q12);
2747        tcg_regset_set_reg(tcg_target_call_clobber_regs, TCG_REG_Q13);
2748        tcg_regset_set_reg(tcg_target_call_clobber_regs, TCG_REG_Q14);
2749        tcg_regset_set_reg(tcg_target_call_clobber_regs, TCG_REG_Q15);
2750    }
2751
2752    s->reserved_regs = 0;
2753    tcg_regset_set_reg(s->reserved_regs, TCG_REG_CALL_STACK);
2754    tcg_regset_set_reg(s->reserved_regs, TCG_REG_TMP);
2755    tcg_regset_set_reg(s->reserved_regs, TCG_REG_PC);
2756    tcg_regset_set_reg(s->reserved_regs, TCG_VEC_TMP);
2757}
2758
2759static void tcg_out_ld(TCGContext *s, TCGType type, TCGReg arg,
2760                       TCGReg arg1, intptr_t arg2)
2761{
2762    switch (type) {
2763    case TCG_TYPE_I32:
2764        tcg_out_ld32u(s, COND_AL, arg, arg1, arg2);
2765        return;
2766    case TCG_TYPE_V64:
2767        /* regs 1; size 8; align 8 */
2768        tcg_out_vldst(s, INSN_VLD1 | 0x7d0, arg, arg1, arg2);
2769        return;
2770    case TCG_TYPE_V128:
2771        /*
2772         * We have only 8-byte alignment for the stack per the ABI.
2773         * Rather than dynamically re-align the stack, it's easier
2774         * to simply not request alignment beyond that.  So:
2775         * regs 2; size 8; align 8
2776         */
2777        tcg_out_vldst(s, INSN_VLD1 | 0xad0, arg, arg1, arg2);
2778        return;
2779    default:
2780        g_assert_not_reached();
2781    }
2782}
2783
2784static void tcg_out_st(TCGContext *s, TCGType type, TCGReg arg,
2785                       TCGReg arg1, intptr_t arg2)
2786{
2787    switch (type) {
2788    case TCG_TYPE_I32:
2789        tcg_out_st32(s, COND_AL, arg, arg1, arg2);
2790        return;
2791    case TCG_TYPE_V64:
2792        /* regs 1; size 8; align 8 */
2793        tcg_out_vldst(s, INSN_VST1 | 0x7d0, arg, arg1, arg2);
2794        return;
2795    case TCG_TYPE_V128:
2796        /* See tcg_out_ld re alignment: regs 2; size 8; align 8 */
2797        tcg_out_vldst(s, INSN_VST1 | 0xad0, arg, arg1, arg2);
2798        return;
2799    default:
2800        g_assert_not_reached();
2801    }
2802}
2803
2804static bool tcg_out_sti(TCGContext *s, TCGType type, TCGArg val,
2805                        TCGReg base, intptr_t ofs)
2806{
2807    return false;
2808}
2809
2810static bool tcg_out_mov(TCGContext *s, TCGType type, TCGReg ret, TCGReg arg)
2811{
2812    if (ret == arg) {
2813        return true;
2814    }
2815    switch (type) {
2816    case TCG_TYPE_I32:
2817        if (ret < TCG_REG_Q0 && arg < TCG_REG_Q0) {
2818            tcg_out_mov_reg(s, COND_AL, ret, arg);
2819            return true;
2820        }
2821        return false;
2822
2823    case TCG_TYPE_V64:
2824    case TCG_TYPE_V128:
2825        /* "VMOV D,N" is an alias for "VORR D,N,N". */
2826        tcg_out_vreg3(s, INSN_VORR, type - TCG_TYPE_V64, 0, ret, arg, arg);
2827        return true;
2828
2829    default:
2830        g_assert_not_reached();
2831    }
2832}
2833
2834static void tcg_out_movi(TCGContext *s, TCGType type,
2835                         TCGReg ret, tcg_target_long arg)
2836{
2837    tcg_debug_assert(type == TCG_TYPE_I32);
2838    tcg_debug_assert(ret < TCG_REG_Q0);
2839    tcg_out_movi32(s, COND_AL, ret, arg);
2840}
2841
2842static bool tcg_out_xchg(TCGContext *s, TCGType type, TCGReg r1, TCGReg r2)
2843{
2844    return false;
2845}
2846
2847static void tcg_out_addi_ptr(TCGContext *s, TCGReg rd, TCGReg rs,
2848                             tcg_target_long imm)
2849{
2850    int enc, opc = ARITH_ADD;
2851
2852    /* All of the easiest immediates to encode are positive. */
2853    if (imm < 0) {
2854        imm = -imm;
2855        opc = ARITH_SUB;
2856    }
2857    enc = encode_imm(imm);
2858    if (enc >= 0) {
2859        tcg_out_dat_imm(s, COND_AL, opc, rd, rs, enc);
2860    } else {
2861        tcg_out_movi32(s, COND_AL, TCG_REG_TMP, imm);
2862        tcg_out_dat_reg(s, COND_AL, opc, rd, rs,
2863                        TCG_REG_TMP, SHIFT_IMM_LSL(0));
2864    }
2865}
2866
2867/* Type is always V128, with I64 elements.  */
2868static void tcg_out_dup2_vec(TCGContext *s, TCGReg rd, TCGReg rl, TCGReg rh)
2869{
2870    /* Move high element into place first. */
2871    /* VMOV Dd+1, Ds */
2872    tcg_out_vreg3(s, INSN_VORR | (1 << 12), 0, 0, rd, rh, rh);
2873    /* Move low element into place; tcg_out_mov will check for nop. */
2874    tcg_out_mov(s, TCG_TYPE_V64, rd, rl);
2875}
2876
2877static bool tcg_out_dup_vec(TCGContext *s, TCGType type, unsigned vece,
2878                            TCGReg rd, TCGReg rs)
2879{
2880    int q = type - TCG_TYPE_V64;
2881
2882    if (vece == MO_64) {
2883        if (type == TCG_TYPE_V128) {
2884            tcg_out_dup2_vec(s, rd, rs, rs);
2885        } else {
2886            tcg_out_mov(s, TCG_TYPE_V64, rd, rs);
2887        }
2888    } else if (rs < TCG_REG_Q0) {
2889        int b = (vece == MO_8);
2890        int e = (vece == MO_16);
2891        tcg_out32(s, INSN_VDUP_G | (b << 22) | (q << 21) | (e << 5) |
2892                  encode_vn(rd) | (rs << 12));
2893    } else {
2894        int imm4 = 1 << vece;
2895        tcg_out32(s, INSN_VDUP_S | (imm4 << 16) | (q << 6) |
2896                  encode_vd(rd) | encode_vm(rs));
2897    }
2898    return true;
2899}
2900
2901static bool tcg_out_dupm_vec(TCGContext *s, TCGType type, unsigned vece,
2902                             TCGReg rd, TCGReg base, intptr_t offset)
2903{
2904    if (vece == MO_64) {
2905        tcg_out_ld(s, TCG_TYPE_V64, rd, base, offset);
2906        if (type == TCG_TYPE_V128) {
2907            tcg_out_dup2_vec(s, rd, rd, rd);
2908        }
2909    } else {
2910        int q = type - TCG_TYPE_V64;
2911        tcg_out_vldst(s, INSN_VLD1R | (vece << 6) | (q << 5),
2912                      rd, base, offset);
2913    }
2914    return true;
2915}
2916
2917static void tcg_out_dupi_vec(TCGContext *s, TCGType type, unsigned vece,
2918                             TCGReg rd, int64_t v64)
2919{
2920    int q = type - TCG_TYPE_V64;
2921    int cmode, imm8, i;
2922
2923    /* Test all bytes equal first.  */
2924    if (vece == MO_8) {
2925        tcg_out_vmovi(s, rd, q, 0, 0xe, v64);
2926        return;
2927    }
2928
2929    /*
2930     * Test all bytes 0x00 or 0xff second.  This can match cases that
2931     * might otherwise take 2 or 3 insns for MO_16 or MO_32 below.
2932     */
2933    for (i = imm8 = 0; i < 8; i++) {
2934        uint8_t byte = v64 >> (i * 8);
2935        if (byte == 0xff) {
2936            imm8 |= 1 << i;
2937        } else if (byte != 0) {
2938            goto fail_bytes;
2939        }
2940    }
2941    tcg_out_vmovi(s, rd, q, 1, 0xe, imm8);
2942    return;
2943 fail_bytes:
2944
2945    /*
2946     * Tests for various replications.  For each element width, if we
2947     * cannot find an expansion there's no point checking a larger
2948     * width because we already know by replication it cannot match.
2949     */
2950    if (vece == MO_16) {
2951        uint16_t v16 = v64;
2952
2953        if (is_shimm16(v16, &cmode, &imm8)) {
2954            tcg_out_vmovi(s, rd, q, 0, cmode, imm8);
2955            return;
2956        }
2957        if (is_shimm16(~v16, &cmode, &imm8)) {
2958            tcg_out_vmovi(s, rd, q, 1, cmode, imm8);
2959            return;
2960        }
2961
2962        /*
2963         * Otherwise, all remaining constants can be loaded in two insns:
2964         * rd = v16 & 0xff, rd |= v16 & 0xff00.
2965         */
2966        tcg_out_vmovi(s, rd, q, 0, 0x8, v16 & 0xff);
2967        tcg_out_vmovi(s, rd, q, 0, 0xb, v16 >> 8);   /* VORRI */
2968        return;
2969    }
2970
2971    if (vece == MO_32) {
2972        uint32_t v32 = v64;
2973
2974        if (is_shimm32(v32, &cmode, &imm8) ||
2975            is_soimm32(v32, &cmode, &imm8)) {
2976            tcg_out_vmovi(s, rd, q, 0, cmode, imm8);
2977            return;
2978        }
2979        if (is_shimm32(~v32, &cmode, &imm8) ||
2980            is_soimm32(~v32, &cmode, &imm8)) {
2981            tcg_out_vmovi(s, rd, q, 1, cmode, imm8);
2982            return;
2983        }
2984
2985        /*
2986         * Restrict the set of constants to those we can load with
2987         * two instructions.  Others we load from the pool.
2988         */
2989        i = is_shimm32_pair(v32, &cmode, &imm8);
2990        if (i) {
2991            tcg_out_vmovi(s, rd, q, 0, cmode, imm8);
2992            tcg_out_vmovi(s, rd, q, 0, i | 1, extract32(v32, i * 4, 8));
2993            return;
2994        }
2995        i = is_shimm32_pair(~v32, &cmode, &imm8);
2996        if (i) {
2997            tcg_out_vmovi(s, rd, q, 1, cmode, imm8);
2998            tcg_out_vmovi(s, rd, q, 1, i | 1, extract32(~v32, i * 4, 8));
2999            return;
3000        }
3001    }
3002
3003    /*
3004     * As a last resort, load from the constant pool.
3005     */
3006    if (!q || vece == MO_64) {
3007        new_pool_l2(s, R_ARM_PC11, s->code_ptr, 0, v64, v64 >> 32);
3008        /* VLDR Dd, [pc + offset] */
3009        tcg_out32(s, INSN_VLDR_D | encode_vd(rd) | (0xf << 16));
3010        if (q) {
3011            tcg_out_dup2_vec(s, rd, rd, rd);
3012        }
3013    } else {
3014        new_pool_label(s, (uint32_t)v64, R_ARM_PC8, s->code_ptr, 0);
3015        /* add tmp, pc, offset */
3016        tcg_out_dat_imm(s, COND_AL, ARITH_ADD, TCG_REG_TMP, TCG_REG_PC, 0);
3017        tcg_out_dupm_vec(s, type, MO_32, rd, TCG_REG_TMP, 0);
3018    }
3019}
3020
3021static const ARMInsn vec_cmp_insn[16] = {
3022    [TCG_COND_EQ] = INSN_VCEQ,
3023    [TCG_COND_GT] = INSN_VCGT,
3024    [TCG_COND_GE] = INSN_VCGE,
3025    [TCG_COND_GTU] = INSN_VCGT_U,
3026    [TCG_COND_GEU] = INSN_VCGE_U,
3027};
3028
3029static const ARMInsn vec_cmp0_insn[16] = {
3030    [TCG_COND_EQ] = INSN_VCEQ0,
3031    [TCG_COND_GT] = INSN_VCGT0,
3032    [TCG_COND_GE] = INSN_VCGE0,
3033    [TCG_COND_LT] = INSN_VCLT0,
3034    [TCG_COND_LE] = INSN_VCLE0,
3035};
3036
3037static void tcg_out_vec_op(TCGContext *s, TCGOpcode opc,
3038                           unsigned vecl, unsigned vece,
3039                           const TCGArg args[TCG_MAX_OP_ARGS],
3040                           const int const_args[TCG_MAX_OP_ARGS])
3041{
3042    TCGType type = vecl + TCG_TYPE_V64;
3043    unsigned q = vecl;
3044    TCGArg a0, a1, a2, a3;
3045    int cmode, imm8;
3046
3047    a0 = args[0];
3048    a1 = args[1];
3049    a2 = args[2];
3050
3051    switch (opc) {
3052    case INDEX_op_ld_vec:
3053        tcg_out_ld(s, type, a0, a1, a2);
3054        return;
3055    case INDEX_op_st_vec:
3056        tcg_out_st(s, type, a0, a1, a2);
3057        return;
3058    case INDEX_op_dupm_vec:
3059        tcg_out_dupm_vec(s, type, vece, a0, a1, a2);
3060        return;
3061    case INDEX_op_dup2_vec:
3062        tcg_out_dup2_vec(s, a0, a1, a2);
3063        return;
3064    case INDEX_op_abs_vec:
3065        tcg_out_vreg2(s, INSN_VABS, q, vece, a0, a1);
3066        return;
3067    case INDEX_op_neg_vec:
3068        tcg_out_vreg2(s, INSN_VNEG, q, vece, a0, a1);
3069        return;
3070    case INDEX_op_not_vec:
3071        tcg_out_vreg2(s, INSN_VMVN, q, 0, a0, a1);
3072        return;
3073    case INDEX_op_add_vec:
3074        tcg_out_vreg3(s, INSN_VADD, q, vece, a0, a1, a2);
3075        return;
3076    case INDEX_op_mul_vec:
3077        tcg_out_vreg3(s, INSN_VMUL, q, vece, a0, a1, a2);
3078        return;
3079    case INDEX_op_smax_vec:
3080        tcg_out_vreg3(s, INSN_VMAX, q, vece, a0, a1, a2);
3081        return;
3082    case INDEX_op_smin_vec:
3083        tcg_out_vreg3(s, INSN_VMIN, q, vece, a0, a1, a2);
3084        return;
3085    case INDEX_op_sub_vec:
3086        tcg_out_vreg3(s, INSN_VSUB, q, vece, a0, a1, a2);
3087        return;
3088    case INDEX_op_ssadd_vec:
3089        tcg_out_vreg3(s, INSN_VQADD, q, vece, a0, a1, a2);
3090        return;
3091    case INDEX_op_sssub_vec:
3092        tcg_out_vreg3(s, INSN_VQSUB, q, vece, a0, a1, a2);
3093        return;
3094    case INDEX_op_umax_vec:
3095        tcg_out_vreg3(s, INSN_VMAX_U, q, vece, a0, a1, a2);
3096        return;
3097    case INDEX_op_umin_vec:
3098        tcg_out_vreg3(s, INSN_VMIN_U, q, vece, a0, a1, a2);
3099        return;
3100    case INDEX_op_usadd_vec:
3101        tcg_out_vreg3(s, INSN_VQADD_U, q, vece, a0, a1, a2);
3102        return;
3103    case INDEX_op_ussub_vec:
3104        tcg_out_vreg3(s, INSN_VQSUB_U, q, vece, a0, a1, a2);
3105        return;
3106    case INDEX_op_xor_vec:
3107        tcg_out_vreg3(s, INSN_VEOR, q, 0, a0, a1, a2);
3108        return;
3109    case INDEX_op_arm_sshl_vec:
3110        /*
3111         * Note that Vm is the data and Vn is the shift count,
3112         * therefore the arguments appear reversed.
3113         */
3114        tcg_out_vreg3(s, INSN_VSHL_S, q, vece, a0, a2, a1);
3115        return;
3116    case INDEX_op_arm_ushl_vec:
3117        /* See above. */
3118        tcg_out_vreg3(s, INSN_VSHL_U, q, vece, a0, a2, a1);
3119        return;
3120    case INDEX_op_shli_vec:
3121        tcg_out_vshifti(s, INSN_VSHLI, q, a0, a1, a2 + (8 << vece));
3122        return;
3123    case INDEX_op_shri_vec:
3124        tcg_out_vshifti(s, INSN_VSHRI, q, a0, a1, (16 << vece) - a2);
3125        return;
3126    case INDEX_op_sari_vec:
3127        tcg_out_vshifti(s, INSN_VSARI, q, a0, a1, (16 << vece) - a2);
3128        return;
3129    case INDEX_op_arm_sli_vec:
3130        tcg_out_vshifti(s, INSN_VSLI, q, a0, a2, args[3] + (8 << vece));
3131        return;
3132
3133    case INDEX_op_andc_vec:
3134        if (!const_args[2]) {
3135            tcg_out_vreg3(s, INSN_VBIC, q, 0, a0, a1, a2);
3136            return;
3137        }
3138        a2 = ~a2;
3139        /* fall through */
3140    case INDEX_op_and_vec:
3141        if (const_args[2]) {
3142            is_shimm1632(~a2, &cmode, &imm8);
3143            if (a0 == a1) {
3144                tcg_out_vmovi(s, a0, q, 1, cmode | 1, imm8); /* VBICI */
3145                return;
3146            }
3147            tcg_out_vmovi(s, a0, q, 1, cmode, imm8); /* VMVNI */
3148            a2 = a0;
3149        }
3150        tcg_out_vreg3(s, INSN_VAND, q, 0, a0, a1, a2);
3151        return;
3152
3153    case INDEX_op_orc_vec:
3154        if (!const_args[2]) {
3155            tcg_out_vreg3(s, INSN_VORN, q, 0, a0, a1, a2);
3156            return;
3157        }
3158        a2 = ~a2;
3159        /* fall through */
3160    case INDEX_op_or_vec:
3161        if (const_args[2]) {
3162            is_shimm1632(a2, &cmode, &imm8);
3163            if (a0 == a1) {
3164                tcg_out_vmovi(s, a0, q, 0, cmode | 1, imm8); /* VORRI */
3165                return;
3166            }
3167            tcg_out_vmovi(s, a0, q, 0, cmode, imm8); /* VMOVI */
3168            a2 = a0;
3169        }
3170        tcg_out_vreg3(s, INSN_VORR, q, 0, a0, a1, a2);
3171        return;
3172
3173    case INDEX_op_cmp_vec:
3174        {
3175            TCGCond cond = args[3];
3176            ARMInsn insn;
3177
3178            switch (cond) {
3179            case TCG_COND_NE:
3180                if (const_args[2]) {
3181                    tcg_out_vreg3(s, INSN_VTST, q, vece, a0, a1, a1);
3182                } else {
3183                    tcg_out_vreg3(s, INSN_VCEQ, q, vece, a0, a1, a2);
3184                    tcg_out_vreg2(s, INSN_VMVN, q, 0, a0, a0);
3185                }
3186                break;
3187
3188            case TCG_COND_TSTNE:
3189            case TCG_COND_TSTEQ:
3190                if (const_args[2]) {
3191                    /* (x & 0) == 0 */
3192                    tcg_out_dupi_vec(s, type, MO_8, a0,
3193                                     -(cond == TCG_COND_TSTEQ));
3194                    break;
3195                }
3196                tcg_out_vreg3(s, INSN_VTST, q, vece, a0, a1, a2);
3197                if (cond == TCG_COND_TSTEQ) {
3198                    tcg_out_vreg2(s, INSN_VMVN, q, 0, a0, a0);
3199                }
3200                break;
3201
3202            default:
3203                if (const_args[2]) {
3204                    insn = vec_cmp0_insn[cond];
3205                    if (insn) {
3206                        tcg_out_vreg2(s, insn, q, vece, a0, a1);
3207                        return;
3208                    }
3209                    tcg_out_dupi_vec(s, type, MO_8, TCG_VEC_TMP, 0);
3210                    a2 = TCG_VEC_TMP;
3211                }
3212                insn = vec_cmp_insn[cond];
3213                if (insn == 0) {
3214                    TCGArg t;
3215                    t = a1, a1 = a2, a2 = t;
3216                    cond = tcg_swap_cond(cond);
3217                    insn = vec_cmp_insn[cond];
3218                    tcg_debug_assert(insn != 0);
3219                }
3220                tcg_out_vreg3(s, insn, q, vece, a0, a1, a2);
3221                break;
3222            }
3223        }
3224        return;
3225
3226    case INDEX_op_bitsel_vec:
3227        a3 = args[3];
3228        if (a0 == a3) {
3229            tcg_out_vreg3(s, INSN_VBIT, q, 0, a0, a2, a1);
3230        } else if (a0 == a2) {
3231            tcg_out_vreg3(s, INSN_VBIF, q, 0, a0, a3, a1);
3232        } else {
3233            tcg_out_mov(s, type, a0, a1);
3234            tcg_out_vreg3(s, INSN_VBSL, q, 0, a0, a2, a3);
3235        }
3236        return;
3237
3238    case INDEX_op_mov_vec:  /* Always emitted via tcg_out_mov.  */
3239    case INDEX_op_dup_vec:  /* Always emitted via tcg_out_dup_vec.  */
3240    default:
3241        g_assert_not_reached();
3242    }
3243}
3244
3245int tcg_can_emit_vec_op(TCGOpcode opc, TCGType type, unsigned vece)
3246{
3247    switch (opc) {
3248    case INDEX_op_add_vec:
3249    case INDEX_op_sub_vec:
3250    case INDEX_op_and_vec:
3251    case INDEX_op_andc_vec:
3252    case INDEX_op_or_vec:
3253    case INDEX_op_orc_vec:
3254    case INDEX_op_xor_vec:
3255    case INDEX_op_not_vec:
3256    case INDEX_op_shli_vec:
3257    case INDEX_op_shri_vec:
3258    case INDEX_op_sari_vec:
3259    case INDEX_op_ssadd_vec:
3260    case INDEX_op_sssub_vec:
3261    case INDEX_op_usadd_vec:
3262    case INDEX_op_ussub_vec:
3263    case INDEX_op_bitsel_vec:
3264        return 1;
3265    case INDEX_op_abs_vec:
3266    case INDEX_op_cmp_vec:
3267    case INDEX_op_mul_vec:
3268    case INDEX_op_neg_vec:
3269    case INDEX_op_smax_vec:
3270    case INDEX_op_smin_vec:
3271    case INDEX_op_umax_vec:
3272    case INDEX_op_umin_vec:
3273        return vece < MO_64;
3274    case INDEX_op_shlv_vec:
3275    case INDEX_op_shrv_vec:
3276    case INDEX_op_sarv_vec:
3277    case INDEX_op_rotli_vec:
3278    case INDEX_op_rotlv_vec:
3279    case INDEX_op_rotrv_vec:
3280        return -1;
3281    default:
3282        return 0;
3283    }
3284}
3285
3286void tcg_expand_vec_op(TCGOpcode opc, TCGType type, unsigned vece,
3287                       TCGArg a0, ...)
3288{
3289    va_list va;
3290    TCGv_vec v0, v1, v2, t1, t2, c1;
3291    TCGArg a2;
3292
3293    va_start(va, a0);
3294    v0 = temp_tcgv_vec(arg_temp(a0));
3295    v1 = temp_tcgv_vec(arg_temp(va_arg(va, TCGArg)));
3296    a2 = va_arg(va, TCGArg);
3297    va_end(va);
3298
3299    switch (opc) {
3300    case INDEX_op_shlv_vec:
3301        /*
3302         * Merely propagate shlv_vec to arm_ushl_vec.
3303         * In this way we don't set TCG_TARGET_HAS_shv_vec
3304         * because everything is done via expansion.
3305         */
3306        v2 = temp_tcgv_vec(arg_temp(a2));
3307        vec_gen_3(INDEX_op_arm_ushl_vec, type, vece, tcgv_vec_arg(v0),
3308                  tcgv_vec_arg(v1), tcgv_vec_arg(v2));
3309        break;
3310
3311    case INDEX_op_shrv_vec:
3312    case INDEX_op_sarv_vec:
3313        /* Right shifts are negative left shifts for NEON.  */
3314        v2 = temp_tcgv_vec(arg_temp(a2));
3315        t1 = tcg_temp_new_vec(type);
3316        tcg_gen_neg_vec(vece, t1, v2);
3317        if (opc == INDEX_op_shrv_vec) {
3318            opc = INDEX_op_arm_ushl_vec;
3319        } else {
3320            opc = INDEX_op_arm_sshl_vec;
3321        }
3322        vec_gen_3(opc, type, vece, tcgv_vec_arg(v0),
3323                  tcgv_vec_arg(v1), tcgv_vec_arg(t1));
3324        tcg_temp_free_vec(t1);
3325        break;
3326
3327    case INDEX_op_rotli_vec:
3328        t1 = tcg_temp_new_vec(type);
3329        tcg_gen_shri_vec(vece, t1, v1, -a2 & ((8 << vece) - 1));
3330        vec_gen_4(INDEX_op_arm_sli_vec, type, vece,
3331                  tcgv_vec_arg(v0), tcgv_vec_arg(t1), tcgv_vec_arg(v1), a2);
3332        tcg_temp_free_vec(t1);
3333        break;
3334
3335    case INDEX_op_rotlv_vec:
3336        v2 = temp_tcgv_vec(arg_temp(a2));
3337        t1 = tcg_temp_new_vec(type);
3338        c1 = tcg_constant_vec(type, vece, 8 << vece);
3339        tcg_gen_sub_vec(vece, t1, v2, c1);
3340        /* Right shifts are negative left shifts for NEON.  */
3341        vec_gen_3(INDEX_op_arm_ushl_vec, type, vece, tcgv_vec_arg(t1),
3342                  tcgv_vec_arg(v1), tcgv_vec_arg(t1));
3343        vec_gen_3(INDEX_op_arm_ushl_vec, type, vece, tcgv_vec_arg(v0),
3344                  tcgv_vec_arg(v1), tcgv_vec_arg(v2));
3345        tcg_gen_or_vec(vece, v0, v0, t1);
3346        tcg_temp_free_vec(t1);
3347        break;
3348
3349    case INDEX_op_rotrv_vec:
3350        v2 = temp_tcgv_vec(arg_temp(a2));
3351        t1 = tcg_temp_new_vec(type);
3352        t2 = tcg_temp_new_vec(type);
3353        c1 = tcg_constant_vec(type, vece, 8 << vece);
3354        tcg_gen_neg_vec(vece, t1, v2);
3355        tcg_gen_sub_vec(vece, t2, c1, v2);
3356        /* Right shifts are negative left shifts for NEON.  */
3357        vec_gen_3(INDEX_op_arm_ushl_vec, type, vece, tcgv_vec_arg(t1),
3358                  tcgv_vec_arg(v1), tcgv_vec_arg(t1));
3359        vec_gen_3(INDEX_op_arm_ushl_vec, type, vece, tcgv_vec_arg(t2),
3360                  tcgv_vec_arg(v1), tcgv_vec_arg(t2));
3361        tcg_gen_or_vec(vece, v0, t1, t2);
3362        tcg_temp_free_vec(t1);
3363        tcg_temp_free_vec(t2);
3364        break;
3365
3366    default:
3367        g_assert_not_reached();
3368    }
3369}
3370
3371static void tcg_out_nop_fill(tcg_insn_unit *p, int count)
3372{
3373    int i;
3374    for (i = 0; i < count; ++i) {
3375        p[i] = INSN_NOP;
3376    }
3377}
3378
3379/* Compute frame size via macros, to share between tcg_target_qemu_prologue
3380   and tcg_register_jit.  */
3381
3382#define PUSH_SIZE  ((11 - 4 + 1 + 1) * sizeof(tcg_target_long))
3383
3384#define FRAME_SIZE \
3385    ((PUSH_SIZE \
3386      + TCG_STATIC_CALL_ARGS_SIZE \
3387      + CPU_TEMP_BUF_NLONGS * sizeof(long) \
3388      + TCG_TARGET_STACK_ALIGN - 1) \
3389     & -TCG_TARGET_STACK_ALIGN)
3390
3391#define STACK_ADDEND  (FRAME_SIZE - PUSH_SIZE)
3392
3393static void tcg_target_qemu_prologue(TCGContext *s)
3394{
3395    /* Calling convention requires us to save r4-r11 and lr.  */
3396    /* stmdb sp!, { r4 - r11, lr } */
3397    tcg_out_ldstm(s, COND_AL, INSN_STMDB, TCG_REG_CALL_STACK,
3398                  (1 << TCG_REG_R4) | (1 << TCG_REG_R5) | (1 << TCG_REG_R6) |
3399                  (1 << TCG_REG_R7) | (1 << TCG_REG_R8) | (1 << TCG_REG_R9) |
3400                  (1 << TCG_REG_R10) | (1 << TCG_REG_R11) | (1 << TCG_REG_R14));
3401
3402    /* Reserve callee argument and tcg temp space.  */
3403    tcg_out_dat_rI(s, COND_AL, ARITH_SUB, TCG_REG_CALL_STACK,
3404                   TCG_REG_CALL_STACK, STACK_ADDEND, 1);
3405    tcg_set_frame(s, TCG_REG_CALL_STACK, TCG_STATIC_CALL_ARGS_SIZE,
3406                  CPU_TEMP_BUF_NLONGS * sizeof(long));
3407
3408    tcg_out_mov(s, TCG_TYPE_PTR, TCG_AREG0, tcg_target_call_iarg_regs[0]);
3409
3410    if (!tcg_use_softmmu && guest_base) {
3411        tcg_out_movi(s, TCG_TYPE_PTR, TCG_REG_GUEST_BASE, guest_base);
3412        tcg_regset_set_reg(s->reserved_regs, TCG_REG_GUEST_BASE);
3413    }
3414
3415    tcg_out_b_reg(s, COND_AL, tcg_target_call_iarg_regs[1]);
3416
3417    /*
3418     * Return path for goto_ptr. Set return value to 0, a-la exit_tb,
3419     * and fall through to the rest of the epilogue.
3420     */
3421    tcg_code_gen_epilogue = tcg_splitwx_to_rx(s->code_ptr);
3422    tcg_out_movi(s, TCG_TYPE_PTR, TCG_REG_R0, 0);
3423    tcg_out_epilogue(s);
3424}
3425
3426static void tcg_out_epilogue(TCGContext *s)
3427{
3428    /* Release local stack frame.  */
3429    tcg_out_dat_rI(s, COND_AL, ARITH_ADD, TCG_REG_CALL_STACK,
3430                   TCG_REG_CALL_STACK, STACK_ADDEND, 1);
3431
3432    /* ldmia sp!, { r4 - r11, pc } */
3433    tcg_out_ldstm(s, COND_AL, INSN_LDMIA, TCG_REG_CALL_STACK,
3434                  (1 << TCG_REG_R4) | (1 << TCG_REG_R5) | (1 << TCG_REG_R6) |
3435                  (1 << TCG_REG_R7) | (1 << TCG_REG_R8) | (1 << TCG_REG_R9) |
3436                  (1 << TCG_REG_R10) | (1 << TCG_REG_R11) | (1 << TCG_REG_PC));
3437}
3438
3439static void tcg_out_tb_start(TCGContext *s)
3440{
3441    /* nothing to do */
3442}
3443
3444typedef struct {
3445    DebugFrameHeader h;
3446    uint8_t fde_def_cfa[4];
3447    uint8_t fde_reg_ofs[18];
3448} DebugFrame;
3449
3450#define ELF_HOST_MACHINE EM_ARM
3451
3452/* We're expecting a 2 byte uleb128 encoded value.  */
3453QEMU_BUILD_BUG_ON(FRAME_SIZE >= (1 << 14));
3454
3455static const DebugFrame debug_frame = {
3456    .h.cie.len = sizeof(DebugFrameCIE)-4, /* length after .len member */
3457    .h.cie.id = -1,
3458    .h.cie.version = 1,
3459    .h.cie.code_align = 1,
3460    .h.cie.data_align = 0x7c,             /* sleb128 -4 */
3461    .h.cie.return_column = 14,
3462
3463    /* Total FDE size does not include the "len" member.  */
3464    .h.fde.len = sizeof(DebugFrame) - offsetof(DebugFrame, h.fde.cie_offset),
3465
3466    .fde_def_cfa = {
3467        12, 13,                         /* DW_CFA_def_cfa sp, ... */
3468        (FRAME_SIZE & 0x7f) | 0x80,     /* ... uleb128 FRAME_SIZE */
3469        (FRAME_SIZE >> 7)
3470    },
3471    .fde_reg_ofs = {
3472        /* The following must match the stmdb in the prologue.  */
3473        0x8e, 1,                        /* DW_CFA_offset, lr, -4 */
3474        0x8b, 2,                        /* DW_CFA_offset, r11, -8 */
3475        0x8a, 3,                        /* DW_CFA_offset, r10, -12 */
3476        0x89, 4,                        /* DW_CFA_offset, r9, -16 */
3477        0x88, 5,                        /* DW_CFA_offset, r8, -20 */
3478        0x87, 6,                        /* DW_CFA_offset, r7, -24 */
3479        0x86, 7,                        /* DW_CFA_offset, r6, -28 */
3480        0x85, 8,                        /* DW_CFA_offset, r5, -32 */
3481        0x84, 9,                        /* DW_CFA_offset, r4, -36 */
3482    }
3483};
3484
3485void tcg_register_jit(const void *buf, size_t buf_size)
3486{
3487    tcg_register_jit_int(buf, buf_size, &debug_frame, sizeof(debug_frame));
3488}
3489