tcg-target.inc.c 72.3 KB
Newer Older
B
balrog 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
/*
 * Tiny Code Generator for QEMU
 *
 * Copyright (c) 2008 Andrzej Zaborowski
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */
24

R
Richard Henderson 已提交
25
#include "elf.h"
R
Richard Henderson 已提交
26 27
#include "tcg-be-ldst.h"

28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
/* The __ARM_ARCH define is provided by gcc 4.8.  Construct it otherwise.  */
#ifndef __ARM_ARCH
# if defined(__ARM_ARCH_7__) || defined(__ARM_ARCH_7A__) \
     || defined(__ARM_ARCH_7R__) || defined(__ARM_ARCH_7M__) \
     || defined(__ARM_ARCH_7EM__)
#  define __ARM_ARCH 7
# elif defined(__ARM_ARCH_6__) || defined(__ARM_ARCH_6J__) \
       || defined(__ARM_ARCH_6Z__) || defined(__ARM_ARCH_6ZK__) \
       || defined(__ARM_ARCH_6K__) || defined(__ARM_ARCH_6T2__)
#  define __ARM_ARCH 6
# elif defined(__ARM_ARCH_5__) || defined(__ARM_ARCH_5E__) \
       || defined(__ARM_ARCH_5T__) || defined(__ARM_ARCH_5TE__) \
       || defined(__ARM_ARCH_5TEJ__)
#  define __ARM_ARCH 5
# else
#  define __ARM_ARCH 4
# endif
#endif

47 48
static int arm_arch = __ARM_ARCH;

49 50 51
#if defined(__ARM_ARCH_5T__) \
    || defined(__ARM_ARCH_5TE__) || defined(__ARM_ARCH_5TEJ__)
# define use_armv5t_instructions 1
52
#else
53
# define use_armv5t_instructions use_armv6_instructions
54 55
#endif

56 57
#define use_armv6_instructions  (__ARM_ARCH >= 6 || arm_arch >= 6)
#define use_armv7_instructions  (__ARM_ARCH >= 7 || arm_arch >= 7)
58

59 60 61 62
#ifndef use_idiv_instructions
bool use_idiv_instructions;
#endif

63 64 65 66 67 68 69
/* ??? Ought to think about changing CONFIG_SOFTMMU to always defined.  */
#ifdef CONFIG_SOFTMMU
# define USING_SOFTMMU 1
#else
# define USING_SOFTMMU 0
#endif

70
#ifdef CONFIG_DEBUG_TCG
71
static const char * const tcg_target_reg_names[TCG_TARGET_NB_REGS] = {
B
balrog 已提交
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86
    "%r0",
    "%r1",
    "%r2",
    "%r3",
    "%r4",
    "%r5",
    "%r6",
    "%r7",
    "%r8",
    "%r9",
    "%r10",
    "%r11",
    "%r12",
    "%r13",
    "%r14",
87
    "%pc",
B
balrog 已提交
88
};
89
#endif
B
balrog 已提交
90

91
static const int tcg_target_reg_alloc_order[] = {
B
balrog 已提交
92 93 94 95 96 97 98 99 100
    TCG_REG_R4,
    TCG_REG_R5,
    TCG_REG_R6,
    TCG_REG_R7,
    TCG_REG_R8,
    TCG_REG_R9,
    TCG_REG_R10,
    TCG_REG_R11,
    TCG_REG_R13,
101 102 103 104 105
    TCG_REG_R0,
    TCG_REG_R1,
    TCG_REG_R2,
    TCG_REG_R3,
    TCG_REG_R12,
B
balrog 已提交
106 107 108
    TCG_REG_R14,
};

109
static const int tcg_target_call_iarg_regs[4] = {
B
balrog 已提交
110 111
    TCG_REG_R0, TCG_REG_R1, TCG_REG_R2, TCG_REG_R3
};
112
static const int tcg_target_call_oarg_regs[2] = {
B
balrog 已提交
113 114 115
    TCG_REG_R0, TCG_REG_R1
};

116
#define TCG_REG_TMP  TCG_REG_R12
117

118
static inline void reloc_pc24(tcg_insn_unit *code_ptr, tcg_insn_unit *target)
119
{
120 121
    ptrdiff_t offset = (tcg_ptr_byte_diff(target, code_ptr) - 8) >> 2;
    *code_ptr = (*code_ptr & ~0xffffff) | (offset & 0xffffff);
122 123
}

124 125 126 127 128 129 130 131
static inline void reloc_pc24_atomic(tcg_insn_unit *code_ptr, tcg_insn_unit *target)
{
    ptrdiff_t offset = (tcg_ptr_byte_diff(target, code_ptr) - 8) >> 2;
    tcg_insn_unit insn = atomic_read(code_ptr);
    tcg_debug_assert(offset == sextract32(offset, 0, 24));
    atomic_set(code_ptr, deposit32(insn, 0, 24, offset));
}

132
static void patch_reloc(tcg_insn_unit *code_ptr, int type,
133
                        intptr_t value, intptr_t addend)
B
balrog 已提交
134
{
135 136
    tcg_debug_assert(type == R_ARM_PC24);
    tcg_debug_assert(addend == 0);
137
    reloc_pc24(code_ptr, (tcg_insn_unit *)value);
B
balrog 已提交
138 139
}

140 141 142 143
#define TCG_CT_CONST_ARM  0x100
#define TCG_CT_CONST_INV  0x200
#define TCG_CT_CONST_NEG  0x400
#define TCG_CT_CONST_ZERO 0x800
144

B
balrog 已提交
145
/* parse target specific constraints */
146
static int target_parse_constraint(TCGArgConstraint *ct, const char **pct_str)
B
balrog 已提交
147 148 149 150 151
{
    const char *ct_str;

    ct_str = *pct_str;
    switch (ct_str[0]) {
152
    case 'I':
153 154 155 156 157
        ct->ct |= TCG_CT_CONST_ARM;
        break;
    case 'K':
        ct->ct |= TCG_CT_CONST_INV;
        break;
158 159 160
    case 'N': /* The gcc constraint letter is L, already used here.  */
        ct->ct |= TCG_CT_CONST_NEG;
        break;
161 162 163
    case 'Z':
        ct->ct |= TCG_CT_CONST_ZERO;
        break;
164

B
balrog 已提交
165 166 167 168 169
    case 'r':
        ct->ct |= TCG_CT_REG;
        tcg_regset_set32(ct->u.regs, 0, (1 << TCG_TARGET_NB_REGS) - 1);
        break;

170 171
    /* qemu_ld address */
    case 'l':
B
balrog 已提交
172 173
        ct->ct |= TCG_CT_REG;
        tcg_regset_set32(ct->u.regs, 0, (1 << TCG_TARGET_NB_REGS) - 1);
174
#ifdef CONFIG_SOFTMMU
175
        /* r0-r2,lr will be overwritten when reading the tlb entry,
176
           so don't use these. */
B
balrog 已提交
177 178
        tcg_regset_reset_reg(ct->u.regs, TCG_REG_R0);
        tcg_regset_reset_reg(ct->u.regs, TCG_REG_R1);
179
        tcg_regset_reset_reg(ct->u.regs, TCG_REG_R2);
180
        tcg_regset_reset_reg(ct->u.regs, TCG_REG_R14);
181
#endif
182 183
        break;

184
    /* qemu_st address & data */
185
    case 's':
B
balrog 已提交
186 187
        ct->ct |= TCG_CT_REG;
        tcg_regset_set32(ct->u.regs, 0, (1 << TCG_TARGET_NB_REGS) - 1);
188 189
        /* r0-r2 will be overwritten when reading the tlb entry (softmmu only)
           and r0-r1 doing the byte swapping, so don't use these. */
B
balrog 已提交
190 191
        tcg_regset_reset_reg(ct->u.regs, TCG_REG_R0);
        tcg_regset_reset_reg(ct->u.regs, TCG_REG_R1);
192 193
#if defined(CONFIG_SOFTMMU)
        /* Avoid clashes with registers being used for helper args */
194
        tcg_regset_reset_reg(ct->u.regs, TCG_REG_R2);
195
#if TARGET_LONG_BITS == 64
196 197 198
        /* Avoid clashes with registers being used for helper args */
        tcg_regset_reset_reg(ct->u.regs, TCG_REG_R3);
#endif
199
        tcg_regset_reset_reg(ct->u.regs, TCG_REG_R14);
B
balrog 已提交
200
#endif
201
        break;
B
balrog 已提交
202 203 204 205 206 207 208 209 210 211

    default:
        return -1;
    }
    ct_str++;
    *pct_str = ct_str;

    return 0;
}

212 213 214 215 216 217 218 219 220
static inline uint32_t rotl(uint32_t val, int n)
{
  return (val << n) | (val >> (32 - n));
}

/* ARM immediates for ALU instructions are made of an unsigned 8-bit
   right-rotated by an even amount between 0 and 30. */
static inline int encode_imm(uint32_t imm)
{
L
Laurent Desnogues 已提交
221 222
    int shift;

223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239
    /* simple case, only lower bits */
    if ((imm & ~0xff) == 0)
        return 0;
    /* then try a simple even shift */
    shift = ctz32(imm) & ~1;
    if (((imm >> shift) & ~0xff) == 0)
        return 32 - shift;
    /* now try harder with rotations */
    if ((rotl(imm, 2) & ~0xff) == 0)
        return 2;
    if ((rotl(imm, 4) & ~0xff) == 0)
        return 4;
    if ((rotl(imm, 6) & ~0xff) == 0)
        return 6;
    /* imm can't be encoded */
    return -1;
}
240 241 242

static inline int check_fit_imm(uint32_t imm)
{
243
    return encode_imm(imm) >= 0;
244 245
}

B
balrog 已提交
246 247 248 249 250 251 252 253
/* Test if a constant matches the constraint.
 * TODO: define constraints for:
 *
 * ldr/str offset:   between -0xfff and 0xfff
 * ldrh/strh offset: between -0xff and 0xff
 * mov operand2:     values represented with x << (2 * y), x < 0x100
 * add, sub, eor...: ditto
 */
254
static inline int tcg_target_const_match(tcg_target_long val, TCGType type,
255
                                         const TCGArgConstraint *arg_ct)
B
balrog 已提交
256 257 258
{
    int ct;
    ct = arg_ct->ct;
259
    if (ct & TCG_CT_CONST) {
B
balrog 已提交
260
        return 1;
261
    } else if ((ct & TCG_CT_CONST_ARM) && check_fit_imm(val)) {
262
        return 1;
263 264
    } else if ((ct & TCG_CT_CONST_INV) && check_fit_imm(~val)) {
        return 1;
265 266
    } else if ((ct & TCG_CT_CONST_NEG) && check_fit_imm(-val)) {
        return 1;
267 268
    } else if ((ct & TCG_CT_CONST_ZERO) && val == 0) {
        return 1;
269
    } else {
B
balrog 已提交
270
        return 0;
271
    }
B
balrog 已提交
272 273
}

274 275
#define TO_CPSR (1 << 20)

276
typedef enum {
277 278 279 280 281 282 283 284 285 286 287 288 289 290 291
    ARITH_AND = 0x0 << 21,
    ARITH_EOR = 0x1 << 21,
    ARITH_SUB = 0x2 << 21,
    ARITH_RSB = 0x3 << 21,
    ARITH_ADD = 0x4 << 21,
    ARITH_ADC = 0x5 << 21,
    ARITH_SBC = 0x6 << 21,
    ARITH_RSC = 0x7 << 21,
    ARITH_TST = 0x8 << 21 | TO_CPSR,
    ARITH_CMP = 0xa << 21 | TO_CPSR,
    ARITH_CMN = 0xb << 21 | TO_CPSR,
    ARITH_ORR = 0xc << 21,
    ARITH_MOV = 0xd << 21,
    ARITH_BIC = 0xe << 21,
    ARITH_MVN = 0xf << 21,
292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310

    INSN_LDR_IMM   = 0x04100000,
    INSN_LDR_REG   = 0x06100000,
    INSN_STR_IMM   = 0x04000000,
    INSN_STR_REG   = 0x06000000,

    INSN_LDRH_IMM  = 0x005000b0,
    INSN_LDRH_REG  = 0x001000b0,
    INSN_LDRSH_IMM = 0x005000f0,
    INSN_LDRSH_REG = 0x001000f0,
    INSN_STRH_IMM  = 0x004000b0,
    INSN_STRH_REG  = 0x000000b0,

    INSN_LDRB_IMM  = 0x04500000,
    INSN_LDRB_REG  = 0x06500000,
    INSN_LDRSB_IMM = 0x005000d0,
    INSN_LDRSB_REG = 0x001000d0,
    INSN_STRB_IMM  = 0x04400000,
    INSN_STRB_REG  = 0x06400000,
311 312

    INSN_LDRD_IMM  = 0x004000d0,
313 314 315
    INSN_LDRD_REG  = 0x000000d0,
    INSN_STRD_IMM  = 0x004000f0,
    INSN_STRD_REG  = 0x000000f0,
P
Pranith Kumar 已提交
316 317 318 319

    INSN_DMB_ISH   = 0x5bf07ff5,
    INSN_DMB_MCR   = 0xba0f07ee,

320
} ARMInsn;
B
balrog 已提交
321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348

#define SHIFT_IMM_LSL(im)	(((im) << 7) | 0x00)
#define SHIFT_IMM_LSR(im)	(((im) << 7) | 0x20)
#define SHIFT_IMM_ASR(im)	(((im) << 7) | 0x40)
#define SHIFT_IMM_ROR(im)	(((im) << 7) | 0x60)
#define SHIFT_REG_LSL(rs)	(((rs) << 8) | 0x10)
#define SHIFT_REG_LSR(rs)	(((rs) << 8) | 0x30)
#define SHIFT_REG_ASR(rs)	(((rs) << 8) | 0x50)
#define SHIFT_REG_ROR(rs)	(((rs) << 8) | 0x70)

enum arm_cond_code_e {
    COND_EQ = 0x0,
    COND_NE = 0x1,
    COND_CS = 0x2,	/* Unsigned greater or equal */
    COND_CC = 0x3,	/* Unsigned less than */
    COND_MI = 0x4,	/* Negative */
    COND_PL = 0x5,	/* Zero or greater */
    COND_VS = 0x6,	/* Overflow */
    COND_VC = 0x7,	/* No overflow */
    COND_HI = 0x8,	/* Unsigned greater than */
    COND_LS = 0x9,	/* Unsigned less or equal */
    COND_GE = 0xa,
    COND_LT = 0xb,
    COND_GT = 0xc,
    COND_LE = 0xd,
    COND_AL = 0xe,
};

349
static const uint8_t tcg_cond_to_arm_cond[] = {
B
balrog 已提交
350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373
    [TCG_COND_EQ] = COND_EQ,
    [TCG_COND_NE] = COND_NE,
    [TCG_COND_LT] = COND_LT,
    [TCG_COND_GE] = COND_GE,
    [TCG_COND_LE] = COND_LE,
    [TCG_COND_GT] = COND_GT,
    /* unsigned */
    [TCG_COND_LTU] = COND_CC,
    [TCG_COND_GEU] = COND_CS,
    [TCG_COND_LEU] = COND_LS,
    [TCG_COND_GTU] = COND_HI,
};

static inline void tcg_out_bx(TCGContext *s, int cond, int rn)
{
    tcg_out32(s, (cond << 28) | 0x012fff10 | rn);
}

static inline void tcg_out_b(TCGContext *s, int cond, int32_t offset)
{
    tcg_out32(s, (cond << 28) | 0x0a000000 |
                    (((offset - 8) >> 2) & 0x00ffffff));
}

374 375
static inline void tcg_out_b_noaddr(TCGContext *s, int cond)
{
376 377
    /* We pay attention here to not modify the branch target by masking
       the corresponding bytes.  This ensure that caches and memory are
378
       kept coherent during retranslation. */
379
    tcg_out32(s, deposit32(*s->code_ptr, 24, 8, (cond << 4) | 0x0a));
380 381 382 383
}

static inline void tcg_out_bl_noaddr(TCGContext *s, int cond)
{
384 385
    /* We pay attention here to not modify the branch target by masking
       the corresponding bytes.  This ensure that caches and memory are
386
       kept coherent during retranslation. */
387
    tcg_out32(s, deposit32(*s->code_ptr, 24, 8, (cond << 4) | 0x0b));
388 389
}

B
balrog 已提交
390 391 392 393 394 395
static inline void tcg_out_bl(TCGContext *s, int cond, int32_t offset)
{
    tcg_out32(s, (cond << 28) | 0x0b000000 |
                    (((offset - 8) >> 2) & 0x00ffffff));
}

396 397 398 399 400
static inline void tcg_out_blx(TCGContext *s, int cond, int rn)
{
    tcg_out32(s, (cond << 28) | 0x012fff30 | rn);
}

401 402 403 404 405 406
static inline void tcg_out_blx_imm(TCGContext *s, int32_t offset)
{
    tcg_out32(s, 0xfa000000 | ((offset & 2) << 23) |
                (((offset - 8) >> 2) & 0x00ffffff));
}

B
balrog 已提交
407 408 409
static inline void tcg_out_dat_reg(TCGContext *s,
                int cond, int opc, int rd, int rn, int rm, int shift)
{
410
    tcg_out32(s, (cond << 28) | (0 << 25) | opc |
B
balrog 已提交
411 412 413
                    (rn << 16) | (rd << 12) | shift | rm);
}

414 415 416 417 418 419 420 421 422 423 424 425 426 427
static inline void tcg_out_nop(TCGContext *s)
{
    if (use_armv7_instructions) {
        /* Architected nop introduced in v6k.  */
        /* ??? This is an MSR (imm) 0,0,0 insn.  Anyone know if this
           also Just So Happened to do nothing on pre-v6k so that we
           don't need to conditionalize it?  */
        tcg_out32(s, 0xe320f000);
    } else {
        /* Prior to that the assembler uses mov r0, r0.  */
        tcg_out_dat_reg(s, COND_AL, ARITH_MOV, 0, 0, 0, SHIFT_IMM_LSL(0));
    }
}

428 429 430 431 432 433 434 435
static inline void tcg_out_mov_reg(TCGContext *s, int cond, int rd, int rm)
{
    /* Simple reg-reg move, optimising out the 'do nothing' case */
    if (rd != rm) {
        tcg_out_dat_reg(s, cond, ARITH_MOV, rd, 0, rm, SHIFT_IMM_LSL(0));
    }
}

B
balrog 已提交
436 437 438
static inline void tcg_out_dat_imm(TCGContext *s,
                int cond, int opc, int rd, int rn, int im)
{
439
    tcg_out32(s, (cond << 28) | (1 << 25) | opc |
B
balrog 已提交
440 441 442
                    (rn << 16) | (rd << 12) | im);
}

443
static void tcg_out_movi32(TCGContext *s, int cond, int rd, uint32_t arg)
B
balrog 已提交
444
{
445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467
    int rot, opc, rn;

    /* For armv7, make sure not to use movw+movt when mov/mvn would do.
       Speed things up by only checking when movt would be required.
       Prior to armv7, have one go at fully rotated immediates before
       doing the decomposition thing below.  */
    if (!use_armv7_instructions || (arg & 0xffff0000)) {
        rot = encode_imm(arg);
        if (rot >= 0) {
            tcg_out_dat_imm(s, cond, ARITH_MOV, rd, 0,
                            rotl(arg, rot) | (rot << 7));
            return;
        }
        rot = encode_imm(~arg);
        if (rot >= 0) {
            tcg_out_dat_imm(s, cond, ARITH_MVN, rd, 0,
                            rotl(~arg, rot) | (rot << 7));
            return;
        }
    }

    /* Use movw + movt.  */
    if (use_armv7_instructions) {
468 469 470
        /* movw */
        tcg_out32(s, (cond << 28) | 0x03000000 | (rd << 12)
                  | ((arg << 4) & 0x000f0000) | (arg & 0xfff));
471
        if (arg & 0xffff0000) {
472 473 474 475
            /* movt */
            tcg_out32(s, (cond << 28) | 0x03400000 | (rd << 12)
                      | ((arg >> 12) & 0x000f0000) | ((arg >> 16) & 0xfff));
        }
476 477
        return;
    }
478

479 480 481 482 483 484 485 486
    /* TODO: This is very suboptimal, we can easily have a constant
       pool somewhere after all the instructions.  */
    opc = ARITH_MOV;
    rn = 0;
    /* If we have lots of leading 1's, we can shorten the sequence by
       beginning with mvn and then clearing higher bits with eor.  */
    if (clz32(~arg) > clz32(arg)) {
        opc = ARITH_MVN, arg = ~arg;
487
    }
488 489 490 491 492 493 494 495 496
    do {
        int i = ctz32(arg) & ~1;
        rot = ((32 - i) << 7) & 0xf00;
        tcg_out_dat_imm(s, cond, opc, rd, rn, ((arg >> i) & 0xff) | rot);
        arg &= ~(0xff << i);

        opc = ARITH_EOR;
        rn = rd;
    } while (arg);
B
balrog 已提交
497 498
}

499 500 501 502 503 504 505 506
static inline void tcg_out_dat_rI(TCGContext *s, int cond, int opc, TCGArg dst,
                                  TCGArg lhs, TCGArg rhs, int rhs_is_const)
{
    /* Emit either the reg,imm or reg,reg form of a data-processing insn.
     * rhs must satisfy the "rI" constraint.
     */
    if (rhs_is_const) {
        int rot = encode_imm(rhs);
507
        tcg_debug_assert(rot >= 0);
508 509 510 511 512 513
        tcg_out_dat_imm(s, cond, opc, dst, lhs, rotl(rhs, rot) | (rot << 7));
    } else {
        tcg_out_dat_reg(s, cond, opc, dst, lhs, rhs, SHIFT_IMM_LSL(0));
    }
}

514 515 516 517 518 519 520 521 522 523 524 525
static void tcg_out_dat_rIK(TCGContext *s, int cond, int opc, int opinv,
                            TCGReg dst, TCGReg lhs, TCGArg rhs,
                            bool rhs_is_const)
{
    /* Emit either the reg,imm or reg,reg form of a data-processing insn.
     * rhs must satisfy the "rIK" constraint.
     */
    if (rhs_is_const) {
        int rot = encode_imm(rhs);
        if (rot < 0) {
            rhs = ~rhs;
            rot = encode_imm(rhs);
526
            tcg_debug_assert(rot >= 0);
527 528 529 530 531 532 533 534
            opc = opinv;
        }
        tcg_out_dat_imm(s, cond, opc, dst, lhs, rotl(rhs, rot) | (rot << 7));
    } else {
        tcg_out_dat_reg(s, cond, opc, dst, lhs, rhs, SHIFT_IMM_LSL(0));
    }
}

535 536 537 538 539 540 541 542 543 544 545 546
static void tcg_out_dat_rIN(TCGContext *s, int cond, int opc, int opneg,
                            TCGArg dst, TCGArg lhs, TCGArg rhs,
                            bool rhs_is_const)
{
    /* Emit either the reg,imm or reg,reg form of a data-processing insn.
     * rhs must satisfy the "rIN" constraint.
     */
    if (rhs_is_const) {
        int rot = encode_imm(rhs);
        if (rot < 0) {
            rhs = -rhs;
            rot = encode_imm(rhs);
547
            tcg_debug_assert(rot >= 0);
548 549 550 551 552 553 554 555
            opc = opneg;
        }
        tcg_out_dat_imm(s, cond, opc, dst, lhs, rotl(rhs, rot) | (rot << 7));
    } else {
        tcg_out_dat_reg(s, cond, opc, dst, lhs, rhs, SHIFT_IMM_LSL(0));
    }
}

556 557
static inline void tcg_out_mul32(TCGContext *s, int cond, TCGReg rd,
                                 TCGReg rn, TCGReg rm)
B
balrog 已提交
558
{
559 560 561 562 563 564 565 566 567 568
    /* if ArchVersion() < 6 && d == n then UNPREDICTABLE;  */
    if (!use_armv6_instructions && rd == rn) {
        if (rd == rm) {
            /* rd == rn == rm; copy an input to tmp first.  */
            tcg_out_mov_reg(s, cond, TCG_REG_TMP, rn);
            rm = rn = TCG_REG_TMP;
        } else {
            rn = rm;
            rm = rd;
        }
B
balrog 已提交
569
    }
570 571
    /* mul */
    tcg_out32(s, (cond << 28) | 0x90 | (rd << 16) | (rm << 8) | rn);
B
balrog 已提交
572 573
}

574 575
static inline void tcg_out_umull32(TCGContext *s, int cond, TCGReg rd0,
                                   TCGReg rd1, TCGReg rn, TCGReg rm)
B
balrog 已提交
576
{
577 578 579 580 581 582 583 584 585 586
    /* if ArchVersion() < 6 && (dHi == n || dLo == n) then UNPREDICTABLE;  */
    if (!use_armv6_instructions && (rd0 == rn || rd1 == rn)) {
        if (rd0 == rm || rd1 == rm) {
            tcg_out_mov_reg(s, cond, TCG_REG_TMP, rn);
            rn = TCG_REG_TMP;
        } else {
            TCGReg t = rn;
            rn = rm;
            rm = t;
        }
B
balrog 已提交
587
    }
588 589 590
    /* umull */
    tcg_out32(s, (cond << 28) | 0x00800090 |
              (rd1 << 16) | (rd0 << 12) | (rm << 8) | rn);
B
balrog 已提交
591 592
}

593 594
static inline void tcg_out_smull32(TCGContext *s, int cond, TCGReg rd0,
                                   TCGReg rd1, TCGReg rn, TCGReg rm)
B
balrog 已提交
595
{
596 597 598 599 600 601 602 603 604 605
    /* if ArchVersion() < 6 && (dHi == n || dLo == n) then UNPREDICTABLE;  */
    if (!use_armv6_instructions && (rd0 == rn || rd1 == rn)) {
        if (rd0 == rm || rd1 == rm) {
            tcg_out_mov_reg(s, cond, TCG_REG_TMP, rn);
            rn = TCG_REG_TMP;
        } else {
            TCGReg t = rn;
            rn = rm;
            rm = t;
        }
B
balrog 已提交
606
    }
607 608 609
    /* smull */
    tcg_out32(s, (cond << 28) | 0x00c00090 |
              (rd1 << 16) | (rd0 << 12) | (rm << 8) | rn);
B
balrog 已提交
610 611
}

612 613 614 615 616 617 618 619 620 621
static inline void tcg_out_sdiv(TCGContext *s, int cond, int rd, int rn, int rm)
{
    tcg_out32(s, 0x0710f010 | (cond << 28) | (rd << 16) | rn | (rm << 8));
}

static inline void tcg_out_udiv(TCGContext *s, int cond, int rd, int rn, int rm)
{
    tcg_out32(s, 0x0730f010 | (cond << 28) | (rd << 16) | rn | (rm << 8));
}

A
Aurelien Jarno 已提交
622 623 624 625 626 627 628
static inline void tcg_out_ext8s(TCGContext *s, int cond,
                                 int rd, int rn)
{
    if (use_armv6_instructions) {
        /* sxtb */
        tcg_out32(s, 0x06af0070 | (cond << 28) | (rd << 12) | rn);
    } else {
629
        tcg_out_dat_reg(s, cond, ARITH_MOV,
A
Aurelien Jarno 已提交
630
                        rd, 0, rn, SHIFT_IMM_LSL(24));
631
        tcg_out_dat_reg(s, cond, ARITH_MOV,
A
Aurelien Jarno 已提交
632 633 634 635
                        rd, 0, rd, SHIFT_IMM_ASR(24));
    }
}

636 637 638 639 640 641
static inline void tcg_out_ext8u(TCGContext *s, int cond,
                                 int rd, int rn)
{
    tcg_out_dat_imm(s, cond, ARITH_AND, rd, rn, 0xff);
}

A
Aurelien Jarno 已提交
642 643 644 645 646 647 648
static inline void tcg_out_ext16s(TCGContext *s, int cond,
                                  int rd, int rn)
{
    if (use_armv6_instructions) {
        /* sxth */
        tcg_out32(s, 0x06bf0070 | (cond << 28) | (rd << 12) | rn);
    } else {
649
        tcg_out_dat_reg(s, cond, ARITH_MOV,
A
Aurelien Jarno 已提交
650
                        rd, 0, rn, SHIFT_IMM_LSL(16));
651
        tcg_out_dat_reg(s, cond, ARITH_MOV,
A
Aurelien Jarno 已提交
652 653 654 655 656 657 658 659 660 661 662
                        rd, 0, rd, SHIFT_IMM_ASR(16));
    }
}

static inline void tcg_out_ext16u(TCGContext *s, int cond,
                                  int rd, int rn)
{
    if (use_armv6_instructions) {
        /* uxth */
        tcg_out32(s, 0x06ff0070 | (cond << 28) | (rd << 12) | rn);
    } else {
663
        tcg_out_dat_reg(s, cond, ARITH_MOV,
A
Aurelien Jarno 已提交
664
                        rd, 0, rn, SHIFT_IMM_LSL(16));
665
        tcg_out_dat_reg(s, cond, ARITH_MOV,
A
Aurelien Jarno 已提交
666 667 668 669
                        rd, 0, rd, SHIFT_IMM_LSR(16));
    }
}

670 671 672 673 674 675 676
static inline void tcg_out_bswap16s(TCGContext *s, int cond, int rd, int rn)
{
    if (use_armv6_instructions) {
        /* revsh */
        tcg_out32(s, 0x06ff0fb0 | (cond << 28) | (rd << 12) | rn);
    } else {
        tcg_out_dat_reg(s, cond, ARITH_MOV,
677
                        TCG_REG_TMP, 0, rn, SHIFT_IMM_LSL(24));
678
        tcg_out_dat_reg(s, cond, ARITH_MOV,
679
                        TCG_REG_TMP, 0, TCG_REG_TMP, SHIFT_IMM_ASR(16));
680
        tcg_out_dat_reg(s, cond, ARITH_ORR,
681
                        rd, TCG_REG_TMP, rn, SHIFT_IMM_LSR(8));
682 683 684
    }
}

A
Aurelien Jarno 已提交
685 686 687 688 689 690 691
static inline void tcg_out_bswap16(TCGContext *s, int cond, int rd, int rn)
{
    if (use_armv6_instructions) {
        /* rev16 */
        tcg_out32(s, 0x06bf0fb0 | (cond << 28) | (rd << 12) | rn);
    } else {
        tcg_out_dat_reg(s, cond, ARITH_MOV,
692
                        TCG_REG_TMP, 0, rn, SHIFT_IMM_LSL(24));
A
Aurelien Jarno 已提交
693
        tcg_out_dat_reg(s, cond, ARITH_MOV,
694
                        TCG_REG_TMP, 0, TCG_REG_TMP, SHIFT_IMM_LSR(16));
A
Aurelien Jarno 已提交
695
        tcg_out_dat_reg(s, cond, ARITH_ORR,
696
                        rd, TCG_REG_TMP, rn, SHIFT_IMM_LSR(8));
A
Aurelien Jarno 已提交
697 698 699
    }
}

700 701 702 703 704 705 706 707 708
/* swap the two low bytes assuming that the two high input bytes and the
   two high output bit can hold any value. */
static inline void tcg_out_bswap16st(TCGContext *s, int cond, int rd, int rn)
{
    if (use_armv6_instructions) {
        /* rev16 */
        tcg_out32(s, 0x06bf0fb0 | (cond << 28) | (rd << 12) | rn);
    } else {
        tcg_out_dat_reg(s, cond, ARITH_MOV,
709 710
                        TCG_REG_TMP, 0, rn, SHIFT_IMM_LSR(8));
        tcg_out_dat_imm(s, cond, ARITH_AND, TCG_REG_TMP, TCG_REG_TMP, 0xff);
711
        tcg_out_dat_reg(s, cond, ARITH_ORR,
712
                        rd, TCG_REG_TMP, rn, SHIFT_IMM_LSL(8));
713 714 715
    }
}

A
Aurelien Jarno 已提交
716 717 718 719 720 721 722
static inline void tcg_out_bswap32(TCGContext *s, int cond, int rd, int rn)
{
    if (use_armv6_instructions) {
        /* rev */
        tcg_out32(s, 0x06bf0f30 | (cond << 28) | (rd << 12) | rn);
    } else {
        tcg_out_dat_reg(s, cond, ARITH_EOR,
723
                        TCG_REG_TMP, rn, rn, SHIFT_IMM_ROR(16));
A
Aurelien Jarno 已提交
724
        tcg_out_dat_imm(s, cond, ARITH_BIC,
725
                        TCG_REG_TMP, TCG_REG_TMP, 0xff | 0x800);
A
Aurelien Jarno 已提交
726 727 728
        tcg_out_dat_reg(s, cond, ARITH_MOV,
                        rd, 0, rn, SHIFT_IMM_ROR(8));
        tcg_out_dat_reg(s, cond, ARITH_EOR,
729
                        rd, rd, TCG_REG_TMP, SHIFT_IMM_LSR(8));
A
Aurelien Jarno 已提交
730 731 732
    }
}

733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754
bool tcg_target_deposit_valid(int ofs, int len)
{
    /* ??? Without bfi, we could improve over generic code by combining
       the right-shift from a non-zero ofs with the orr.  We do run into
       problems when rd == rs, and the mask generated from ofs+len doesn't
       fit into an immediate.  We would have to be careful not to pessimize
       wrt the optimizations performed on the expanded code.  */
    return use_armv7_instructions;
}

static inline void tcg_out_deposit(TCGContext *s, int cond, TCGReg rd,
                                   TCGArg a1, int ofs, int len, bool const_a1)
{
    if (const_a1) {
        /* bfi becomes bfc with rn == 15.  */
        a1 = 15;
    }
    /* bfi/bfc */
    tcg_out32(s, 0x07c00010 | (cond << 28) | (rd << 12) | a1
              | (ofs << 7) | ((ofs + len - 1) << 16));
}

755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777
/* Note that this routine is used for both LDR and LDRH formats, so we do
   not wish to include an immediate shift at this point.  */
static void tcg_out_memop_r(TCGContext *s, int cond, ARMInsn opc, TCGReg rt,
                            TCGReg rn, TCGReg rm, bool u, bool p, bool w)
{
    tcg_out32(s, (cond << 28) | opc | (u << 23) | (p << 24)
              | (w << 21) | (rn << 16) | (rt << 12) | rm);
}

static void tcg_out_memop_8(TCGContext *s, int cond, ARMInsn opc, TCGReg rt,
                            TCGReg rn, int imm8, bool p, bool w)
{
    bool u = 1;
    if (imm8 < 0) {
        imm8 = -imm8;
        u = 0;
    }
    tcg_out32(s, (cond << 28) | opc | (u << 23) | (p << 24) | (w << 21) |
              (rn << 16) | (rt << 12) | ((imm8 & 0xf0) << 4) | (imm8 & 0xf));
}

static void tcg_out_memop_12(TCGContext *s, int cond, ARMInsn opc, TCGReg rt,
                             TCGReg rn, int imm12, bool p, bool w)
B
balrog 已提交
778
{
779 780 781 782 783 784 785 786 787 788 789 790 791
    bool u = 1;
    if (imm12 < 0) {
        imm12 = -imm12;
        u = 0;
    }
    tcg_out32(s, (cond << 28) | opc | (u << 23) | (p << 24) | (w << 21) |
              (rn << 16) | (rt << 12) | imm12);
}

static inline void tcg_out_ld32_12(TCGContext *s, int cond, TCGReg rt,
                                   TCGReg rn, int imm12)
{
    tcg_out_memop_12(s, cond, INSN_LDR_IMM, rt, rn, imm12, 1, 0);
B
balrog 已提交
792 793
}

794 795
static inline void tcg_out_st32_12(TCGContext *s, int cond, TCGReg rt,
                                   TCGReg rn, int imm12)
B
balrog 已提交
796
{
797
    tcg_out_memop_12(s, cond, INSN_STR_IMM, rt, rn, imm12, 1, 0);
B
balrog 已提交
798 799
}

800 801
static inline void tcg_out_ld32_r(TCGContext *s, int cond, TCGReg rt,
                                  TCGReg rn, TCGReg rm)
B
balrog 已提交
802
{
803
    tcg_out_memop_r(s, cond, INSN_LDR_REG, rt, rn, rm, 1, 1, 0);
B
balrog 已提交
804 805
}

806 807
static inline void tcg_out_st32_r(TCGContext *s, int cond, TCGReg rt,
                                  TCGReg rn, TCGReg rm)
B
balrog 已提交
808
{
809
    tcg_out_memop_r(s, cond, INSN_STR_REG, rt, rn, rm, 1, 1, 0);
B
balrog 已提交
810 811
}

812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835
static inline void tcg_out_ldrd_8(TCGContext *s, int cond, TCGReg rt,
                                   TCGReg rn, int imm8)
{
    tcg_out_memop_8(s, cond, INSN_LDRD_IMM, rt, rn, imm8, 1, 0);
}

static inline void tcg_out_ldrd_r(TCGContext *s, int cond, TCGReg rt,
                                  TCGReg rn, TCGReg rm)
{
    tcg_out_memop_r(s, cond, INSN_LDRD_REG, rt, rn, rm, 1, 1, 0);
}

static inline void tcg_out_strd_8(TCGContext *s, int cond, TCGReg rt,
                                   TCGReg rn, int imm8)
{
    tcg_out_memop_8(s, cond, INSN_STRD_IMM, rt, rn, imm8, 1, 0);
}

static inline void tcg_out_strd_r(TCGContext *s, int cond, TCGReg rt,
                                  TCGReg rn, TCGReg rm)
{
    tcg_out_memop_r(s, cond, INSN_STRD_REG, rt, rn, rm, 1, 1, 0);
}

P
pbrook 已提交
836
/* Register pre-increment with base writeback.  */
837 838
static inline void tcg_out_ld32_rwb(TCGContext *s, int cond, TCGReg rt,
                                    TCGReg rn, TCGReg rm)
P
pbrook 已提交
839
{
840
    tcg_out_memop_r(s, cond, INSN_LDR_REG, rt, rn, rm, 1, 1, 1);
P
pbrook 已提交
841 842
}

843 844
static inline void tcg_out_st32_rwb(TCGContext *s, int cond, TCGReg rt,
                                    TCGReg rn, TCGReg rm)
P
pbrook 已提交
845
{
846
    tcg_out_memop_r(s, cond, INSN_STR_REG, rt, rn, rm, 1, 1, 1);
P
pbrook 已提交
847 848
}

849 850
static inline void tcg_out_ld16u_8(TCGContext *s, int cond, TCGReg rt,
                                   TCGReg rn, int imm8)
B
balrog 已提交
851
{
852
    tcg_out_memop_8(s, cond, INSN_LDRH_IMM, rt, rn, imm8, 1, 0);
B
balrog 已提交
853 854
}

855 856
static inline void tcg_out_st16_8(TCGContext *s, int cond, TCGReg rt,
                                  TCGReg rn, int imm8)
B
balrog 已提交
857
{
858
    tcg_out_memop_8(s, cond, INSN_STRH_IMM, rt, rn, imm8, 1, 0);
B
balrog 已提交
859 860
}

861 862
static inline void tcg_out_ld16u_r(TCGContext *s, int cond, TCGReg rt,
                                   TCGReg rn, TCGReg rm)
B
balrog 已提交
863
{
864
    tcg_out_memop_r(s, cond, INSN_LDRH_REG, rt, rn, rm, 1, 1, 0);
B
balrog 已提交
865 866
}

867 868
static inline void tcg_out_st16_r(TCGContext *s, int cond, TCGReg rt,
                                  TCGReg rn, TCGReg rm)
B
balrog 已提交
869
{
870
    tcg_out_memop_r(s, cond, INSN_STRH_REG, rt, rn, rm, 1, 1, 0);
B
balrog 已提交
871 872
}

873 874
static inline void tcg_out_ld16s_8(TCGContext *s, int cond, TCGReg rt,
                                   TCGReg rn, int imm8)
B
balrog 已提交
875
{
876
    tcg_out_memop_8(s, cond, INSN_LDRSH_IMM, rt, rn, imm8, 1, 0);
B
balrog 已提交
877 878
}

879 880
static inline void tcg_out_ld16s_r(TCGContext *s, int cond, TCGReg rt,
                                   TCGReg rn, TCGReg rm)
B
balrog 已提交
881
{
882
    tcg_out_memop_r(s, cond, INSN_LDRSH_REG, rt, rn, rm, 1, 1, 0);
B
balrog 已提交
883 884
}

885 886
static inline void tcg_out_ld8_12(TCGContext *s, int cond, TCGReg rt,
                                  TCGReg rn, int imm12)
B
balrog 已提交
887
{
888
    tcg_out_memop_12(s, cond, INSN_LDRB_IMM, rt, rn, imm12, 1, 0);
B
balrog 已提交
889 890
}

891 892
static inline void tcg_out_st8_12(TCGContext *s, int cond, TCGReg rt,
                                  TCGReg rn, int imm12)
B
balrog 已提交
893
{
894
    tcg_out_memop_12(s, cond, INSN_STRB_IMM, rt, rn, imm12, 1, 0);
B
balrog 已提交
895 896
}

897 898
static inline void tcg_out_ld8_r(TCGContext *s, int cond, TCGReg rt,
                                 TCGReg rn, TCGReg rm)
B
balrog 已提交
899
{
900
    tcg_out_memop_r(s, cond, INSN_LDRB_REG, rt, rn, rm, 1, 1, 0);
B
balrog 已提交
901 902
}

903 904
static inline void tcg_out_st8_r(TCGContext *s, int cond, TCGReg rt,
                                 TCGReg rn, TCGReg rm)
B
balrog 已提交
905
{
906
    tcg_out_memop_r(s, cond, INSN_STRB_REG, rt, rn, rm, 1, 1, 0);
B
balrog 已提交
907 908
}

909 910
static inline void tcg_out_ld8s_8(TCGContext *s, int cond, TCGReg rt,
                                  TCGReg rn, int imm8)
B
balrog 已提交
911
{
912
    tcg_out_memop_8(s, cond, INSN_LDRSB_IMM, rt, rn, imm8, 1, 0);
B
balrog 已提交
913 914
}

915 916
static inline void tcg_out_ld8s_r(TCGContext *s, int cond, TCGReg rt,
                                  TCGReg rn, TCGReg rm)
B
balrog 已提交
917
{
918
    tcg_out_memop_r(s, cond, INSN_LDRSB_REG, rt, rn, rm, 1, 1, 0);
B
balrog 已提交
919 920 921 922 923 924
}

static inline void tcg_out_ld32u(TCGContext *s, int cond,
                int rd, int rn, int32_t offset)
{
    if (offset > 0xfff || offset < -0xfff) {
925 926
        tcg_out_movi32(s, cond, TCG_REG_TMP, offset);
        tcg_out_ld32_r(s, cond, rd, rn, TCG_REG_TMP);
B
balrog 已提交
927 928 929 930 931 932 933 934
    } else
        tcg_out_ld32_12(s, cond, rd, rn, offset);
}

static inline void tcg_out_st32(TCGContext *s, int cond,
                int rd, int rn, int32_t offset)
{
    if (offset > 0xfff || offset < -0xfff) {
935 936
        tcg_out_movi32(s, cond, TCG_REG_TMP, offset);
        tcg_out_st32_r(s, cond, rd, rn, TCG_REG_TMP);
B
balrog 已提交
937 938 939 940 941 942 943 944
    } else
        tcg_out_st32_12(s, cond, rd, rn, offset);
}

static inline void tcg_out_ld16u(TCGContext *s, int cond,
                int rd, int rn, int32_t offset)
{
    if (offset > 0xff || offset < -0xff) {
945 946
        tcg_out_movi32(s, cond, TCG_REG_TMP, offset);
        tcg_out_ld16u_r(s, cond, rd, rn, TCG_REG_TMP);
B
balrog 已提交
947 948 949 950 951 952 953 954
    } else
        tcg_out_ld16u_8(s, cond, rd, rn, offset);
}

static inline void tcg_out_ld16s(TCGContext *s, int cond,
                int rd, int rn, int32_t offset)
{
    if (offset > 0xff || offset < -0xff) {
955 956
        tcg_out_movi32(s, cond, TCG_REG_TMP, offset);
        tcg_out_ld16s_r(s, cond, rd, rn, TCG_REG_TMP);
B
balrog 已提交
957 958 959 960
    } else
        tcg_out_ld16s_8(s, cond, rd, rn, offset);
}

961
static inline void tcg_out_st16(TCGContext *s, int cond,
B
balrog 已提交
962 963 964
                int rd, int rn, int32_t offset)
{
    if (offset > 0xff || offset < -0xff) {
965 966
        tcg_out_movi32(s, cond, TCG_REG_TMP, offset);
        tcg_out_st16_r(s, cond, rd, rn, TCG_REG_TMP);
B
balrog 已提交
967
    } else
968
        tcg_out_st16_8(s, cond, rd, rn, offset);
B
balrog 已提交
969 970 971 972 973 974
}

static inline void tcg_out_ld8u(TCGContext *s, int cond,
                int rd, int rn, int32_t offset)
{
    if (offset > 0xfff || offset < -0xfff) {
975 976
        tcg_out_movi32(s, cond, TCG_REG_TMP, offset);
        tcg_out_ld8_r(s, cond, rd, rn, TCG_REG_TMP);
B
balrog 已提交
977 978 979 980 981 982 983 984
    } else
        tcg_out_ld8_12(s, cond, rd, rn, offset);
}

static inline void tcg_out_ld8s(TCGContext *s, int cond,
                int rd, int rn, int32_t offset)
{
    if (offset > 0xff || offset < -0xff) {
985 986
        tcg_out_movi32(s, cond, TCG_REG_TMP, offset);
        tcg_out_ld8s_r(s, cond, rd, rn, TCG_REG_TMP);
B
balrog 已提交
987 988 989 990
    } else
        tcg_out_ld8s_8(s, cond, rd, rn, offset);
}

991
static inline void tcg_out_st8(TCGContext *s, int cond,
B
balrog 已提交
992 993 994
                int rd, int rn, int32_t offset)
{
    if (offset > 0xfff || offset < -0xfff) {
995 996
        tcg_out_movi32(s, cond, TCG_REG_TMP, offset);
        tcg_out_st8_r(s, cond, rd, rn, TCG_REG_TMP);
B
balrog 已提交
997 998 999 1000
    } else
        tcg_out_st8_12(s, cond, rd, rn, offset);
}

1001 1002 1003
/* The _goto case is normally between TBs within the same code buffer, and
 * with the code buffer limited to 16MB we wouldn't need the long case.
 * But we also use it for the tail-call to the qemu_ld/st helpers, which does.
1004
 */
1005
static inline void tcg_out_goto(TCGContext *s, int cond, tcg_insn_unit *addr)
B
balrog 已提交
1006
{
1007 1008
    intptr_t addri = (intptr_t)addr;
    ptrdiff_t disp = tcg_pcrel_diff(s, addr);
B
balrog 已提交
1009

1010
    if ((addri & 1) == 0 && disp - 8 < 0x01fffffd && disp - 8 > -0x01fffffd) {
1011 1012
        tcg_out_b(s, cond, disp);
        return;
1013 1014
    }

1015
    tcg_out_movi32(s, cond, TCG_REG_TMP, addri);
1016 1017 1018
    if (use_armv5t_instructions) {
        tcg_out_bx(s, cond, TCG_REG_TMP);
    } else {
1019
        if (addri & 1) {
1020
            tcg_abort();
B
balrog 已提交
1021
        }
1022
        tcg_out_mov_reg(s, cond, TCG_REG_PC, TCG_REG_TMP);
B
balrog 已提交
1023 1024 1025
    }
}

1026 1027
/* The call case is mostly used for helpers - so it's not unreasonable
 * for them to be beyond branch range */
1028
static void tcg_out_call(TCGContext *s, tcg_insn_unit *addr)
B
balrog 已提交
1029
{
1030 1031
    intptr_t addri = (intptr_t)addr;
    ptrdiff_t disp = tcg_pcrel_diff(s, addr);
B
balrog 已提交
1032

1033 1034
    if (disp - 8 < 0x02000000 && disp - 8 >= -0x02000000) {
        if (addri & 1) {
1035
            /* Use BLX if the target is in Thumb mode */
1036
            if (!use_armv5t_instructions) {
1037 1038
                tcg_abort();
            }
1039
            tcg_out_blx_imm(s, disp);
1040
        } else {
1041
            tcg_out_bl(s, COND_AL, disp);
1042
        }
1043
    } else if (use_armv7_instructions) {
1044
        tcg_out_movi32(s, COND_AL, TCG_REG_TMP, addri);
1045
        tcg_out_blx(s, COND_AL, TCG_REG_TMP);
1046
    } else {
1047 1048
        tcg_out_dat_imm(s, COND_AL, ARITH_ADD, TCG_REG_R14, TCG_REG_PC, 4);
        tcg_out_ld32_12(s, COND_AL, TCG_REG_PC, TCG_REG_PC, -4);
1049
        tcg_out32(s, addri);
B
balrog 已提交
1050 1051 1052
    }
}

1053 1054 1055 1056 1057 1058 1059 1060 1061 1062
void arm_tb_set_jmp_target(uintptr_t jmp_addr, uintptr_t addr)
{
    tcg_insn_unit *code_ptr = (tcg_insn_unit *)jmp_addr;
    tcg_insn_unit *target = (tcg_insn_unit *)addr;

    /* we could use a ldr pc, [pc, #-4] kind of branch and avoid the flush */
    reloc_pc24_atomic(code_ptr, target);
    flush_icache_range(jmp_addr, jmp_addr + 4);
}

1063
static inline void tcg_out_goto_label(TCGContext *s, int cond, TCGLabel *l)
B
balrog 已提交
1064
{
1065
    if (l->has_value) {
1066
        tcg_out_goto(s, cond, l->u.value_ptr);
B
balrog 已提交
1067
    } else {
1068
        tcg_out_reloc(s, s->code_ptr, R_ARM_PC24, l, 0);
1069
        tcg_out_b_noaddr(s, cond);
B
balrog 已提交
1070 1071 1072
    }
}

P
Pranith Kumar 已提交
1073 1074 1075 1076 1077 1078 1079 1080 1081
static inline void tcg_out_mb(TCGContext *s, TCGArg a0)
{
    if (use_armv7_instructions) {
        tcg_out32(s, INSN_DMB_ISH);
    } else if (use_armv6_instructions) {
        tcg_out32(s, INSN_DMB_MCR);
    }
}

B
balrog 已提交
1082
#ifdef CONFIG_SOFTMMU
1083 1084 1085
/* helper signature: helper_ret_ld_mmu(CPUState *env, target_ulong addr,
 *                                     int mmu_idx, uintptr_t ra)
 */
1086
static void * const qemu_ld_helpers[16] = {
1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100
    [MO_UB]   = helper_ret_ldub_mmu,
    [MO_SB]   = helper_ret_ldsb_mmu,

    [MO_LEUW] = helper_le_lduw_mmu,
    [MO_LEUL] = helper_le_ldul_mmu,
    [MO_LEQ]  = helper_le_ldq_mmu,
    [MO_LESW] = helper_le_ldsw_mmu,
    [MO_LESL] = helper_le_ldul_mmu,

    [MO_BEUW] = helper_be_lduw_mmu,
    [MO_BEUL] = helper_be_ldul_mmu,
    [MO_BEQ]  = helper_be_ldq_mmu,
    [MO_BESW] = helper_be_ldsw_mmu,
    [MO_BESL] = helper_be_ldul_mmu,
1101 1102
};

1103 1104 1105
/* helper signature: helper_ret_st_mmu(CPUState *env, target_ulong addr,
 *                                     uintxx_t val, int mmu_idx, uintptr_t ra)
 */
1106
static void * const qemu_st_helpers[16] = {
1107 1108 1109 1110 1111 1112 1113
    [MO_UB]   = helper_ret_stb_mmu,
    [MO_LEUW] = helper_le_stw_mmu,
    [MO_LEUL] = helper_le_stl_mmu,
    [MO_LEQ]  = helper_le_stq_mmu,
    [MO_BEUW] = helper_be_stw_mmu,
    [MO_BEUL] = helper_be_stl_mmu,
    [MO_BEQ]  = helper_be_stq_mmu,
1114
};
1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125

/* Helper routines for marshalling helper function arguments into
 * the correct registers and stack.
 * argreg is where we want to put this argument, arg is the argument itself.
 * Return value is the updated argreg ready for the next call.
 * Note that argreg 0..3 is real registers, 4+ on stack.
 *
 * We provide routines for arguments which are: immediate, 32 bit
 * value in register, 16 and 8 bit values in register (which must be zero
 * extended before use) and 64 bit value in a lo:hi register pair.
 */
1126 1127 1128 1129 1130 1131 1132 1133
#define DEFINE_TCG_OUT_ARG(NAME, ARGTYPE, MOV_ARG, EXT_ARG)                \
static TCGReg NAME(TCGContext *s, TCGReg argreg, ARGTYPE arg)              \
{                                                                          \
    if (argreg < 4) {                                                      \
        MOV_ARG(s, COND_AL, argreg, arg);                                  \
    } else {                                                               \
        int ofs = (argreg - 4) * 4;                                        \
        EXT_ARG;                                                           \
1134
        tcg_debug_assert(ofs + 4 <= TCG_STATIC_CALL_ARGS_SIZE);            \
1135 1136 1137 1138 1139 1140
        tcg_out_st32_12(s, COND_AL, arg, TCG_REG_CALL_STACK, ofs);         \
    }                                                                      \
    return argreg + 1;                                                     \
}

DEFINE_TCG_OUT_ARG(tcg_out_arg_imm32, uint32_t, tcg_out_movi32,
1141
    (tcg_out_movi32(s, COND_AL, TCG_REG_TMP, arg), arg = TCG_REG_TMP))
1142
DEFINE_TCG_OUT_ARG(tcg_out_arg_reg8, TCGReg, tcg_out_ext8u,
1143
    (tcg_out_ext8u(s, COND_AL, TCG_REG_TMP, arg), arg = TCG_REG_TMP))
1144
DEFINE_TCG_OUT_ARG(tcg_out_arg_reg16, TCGReg, tcg_out_ext16u,
1145
    (tcg_out_ext16u(s, COND_AL, TCG_REG_TMP, arg), arg = TCG_REG_TMP))
1146 1147 1148 1149
DEFINE_TCG_OUT_ARG(tcg_out_arg_reg32, TCGReg, tcg_out_mov_reg, )

static TCGReg tcg_out_arg_reg64(TCGContext *s, TCGReg argreg,
                                TCGReg arglo, TCGReg arghi)
1150 1151 1152 1153 1154 1155 1156
{
    /* 64 bit arguments must go in even/odd register pairs
     * and in 8-aligned stack slots.
     */
    if (argreg & 1) {
        argreg++;
    }
1157 1158 1159 1160 1161 1162 1163 1164 1165 1166
    if (use_armv6_instructions && argreg >= 4
        && (arglo & 1) == 0 && arghi == arglo + 1) {
        tcg_out_strd_8(s, COND_AL, arglo,
                       TCG_REG_CALL_STACK, (argreg - 4) * 4);
        return argreg + 2;
    } else {
        argreg = tcg_out_arg_reg32(s, argreg, arglo);
        argreg = tcg_out_arg_reg32(s, argreg, arghi);
        return argreg;
    }
1167
}
B
balrog 已提交
1168

P
pbrook 已提交
1169 1170
#define TLB_SHIFT	(CPU_TLB_ENTRY_BITS + CPU_TLB_BITS)

1171 1172 1173 1174 1175 1176 1177 1178 1179
/* We're expecting to use an 8-bit immediate and to mask.  */
QEMU_BUILD_BUG_ON(CPU_TLB_BITS > 8);

/* We're expecting to use an 8-bit immediate add + 8-bit ldrd offset.
   Using the offset of the second entry in the last tlb table ensures
   that we can index all of the elements of the first entry.  */
QEMU_BUILD_BUG_ON(offsetof(CPUArchState, tlb_table[NB_MMU_MODES - 1][1])
                  > 0xffff);

1180 1181
/* Load and compare a TLB entry, leaving the flags set.  Returns the register
   containing the addend of the tlb entry.  Clobbers R0, R1, R2, TMP.  */
B
balrog 已提交
1182

1183
static TCGReg tcg_out_tlb_read(TCGContext *s, TCGReg addrlo, TCGReg addrhi,
1184
                               TCGMemOp opc, int mem_index, bool is_load)
1185
{
1186
    TCGReg base = TCG_AREG0;
1187 1188 1189 1190 1191
    int cmp_off =
        (is_load
         ? offsetof(CPUArchState, tlb_table[mem_index][0].addr_read)
         : offsetof(CPUArchState, tlb_table[mem_index][0].addr_write));
    int add_off = offsetof(CPUArchState, tlb_table[mem_index][0].addend);
1192 1193
    unsigned s_bits = opc & MO_SIZE;
    unsigned a_bits = get_alignment_bits(opc);
1194

1195
    /* Should generate something like the following:
1196
     *   shr    tmp, addrlo, #TARGET_PAGE_BITS                    (1)
1197
     *   add    r2, env, #high
1198 1199
     *   and    r0, tmp, #(CPU_TLB_SIZE - 1)                      (2)
     *   add    r2, r2, r0, lsl #CPU_TLB_ENTRY_BITS               (3)
1200
     *   ldr    r0, [r2, #cmp]                                    (4)
1201 1202
     *   tst    addrlo, #s_mask
     *   ldr    r2, [r2, #add]                                    (5)
1203
     *   cmpeq  r0, tmp, lsl #TARGET_PAGE_BITS
1204
     */
1205
    tcg_out_dat_reg(s, COND_AL, ARITH_MOV, TCG_REG_TMP,
1206
                    0, addrlo, SHIFT_IMM_LSR(TARGET_PAGE_BITS));
1207

1208
    /* We checked that the offset is contained within 16 bits above.  */
1209
    if (add_off > 0xfff || (use_armv6_instructions && cmp_off > 0xff)) {
1210
        tcg_out_dat_imm(s, COND_AL, ARITH_ADD, TCG_REG_R2, base,
1211
                        (24 << 7) | (cmp_off >> 8));
1212
        base = TCG_REG_R2;
1213 1214
        add_off -= cmp_off & 0xff00;
        cmp_off &= 0xff;
1215 1216
    }

B
balrog 已提交
1217
    tcg_out_dat_imm(s, COND_AL, ARITH_AND,
1218
                    TCG_REG_R0, TCG_REG_TMP, CPU_TLB_SIZE - 1);
1219
    tcg_out_dat_reg(s, COND_AL, ARITH_ADD, TCG_REG_R2, base,
1220
                    TCG_REG_R0, SHIFT_IMM_LSL(CPU_TLB_ENTRY_BITS));
1221

1222 1223 1224 1225
    /* Load the tlb comparator.  Use ldrd if needed and available,
       but due to how the pointer needs setting up, ldm isn't useful.
       Base arm5 doesn't have ldrd, but armv5te does.  */
    if (use_armv6_instructions && TARGET_LONG_BITS == 64) {
1226
        tcg_out_ldrd_8(s, COND_AL, TCG_REG_R0, TCG_REG_R2, cmp_off);
1227
    } else {
1228
        tcg_out_ld32_12(s, COND_AL, TCG_REG_R0, TCG_REG_R2, cmp_off);
1229
        if (TARGET_LONG_BITS == 64) {
1230
            tcg_out_ld32_12(s, COND_AL, TCG_REG_R1, TCG_REG_R2, cmp_off + 4);
1231
        }
1232
    }
1233

1234 1235 1236 1237 1238 1239 1240
    /* Check alignment.  We don't support inline unaligned acceses,
       but we can easily support overalignment checks.  */
    if (a_bits < s_bits) {
        a_bits = s_bits;
    }
    if (a_bits) {
        tcg_out_dat_imm(s, COND_AL, ARITH_TST, 0, addrlo, (1 << a_bits) - 1);
1241 1242
    }

1243 1244 1245
    /* Load the tlb addend.  */
    tcg_out_ld32_12(s, COND_AL, TCG_REG_R2, TCG_REG_R2, add_off);

1246 1247 1248
    tcg_out_dat_reg(s, (s_bits ? COND_EQ : COND_AL), ARITH_CMP, 0,
                    TCG_REG_R0, TCG_REG_TMP, SHIFT_IMM_LSL(TARGET_PAGE_BITS));

1249 1250 1251 1252
    if (TARGET_LONG_BITS == 64) {
        tcg_out_dat_reg(s, COND_EQ, ARITH_CMP, 0,
                        TCG_REG_R1, addrhi, SHIFT_IMM_LSL(0));
    }
1253

1254
    return TCG_REG_R2;
1255
}
1256 1257 1258 1259

/* Record the context of a call to the out of line helper code for the slow
   path for a load or store, so that we can later generate the correct
   helper code.  */
1260
static void add_qemu_ldst_label(TCGContext *s, bool is_ld, TCGMemOpIdx oi,
1261
                                TCGReg datalo, TCGReg datahi, TCGReg addrlo,
1262 1263
                                TCGReg addrhi, tcg_insn_unit *raddr,
                                tcg_insn_unit *label_ptr)
1264
{
R
Richard Henderson 已提交
1265
    TCGLabelQemuLdst *label = new_ldst_label(s);
1266 1267

    label->is_ld = is_ld;
1268
    label->oi = oi;
1269 1270 1271 1272
    label->datalo_reg = datalo;
    label->datahi_reg = datahi;
    label->addrlo_reg = addrlo;
    label->addrhi_reg = addrhi;
1273 1274 1275 1276 1277 1278
    label->raddr = raddr;
    label->label_ptr[0] = label_ptr;
}

static void tcg_out_qemu_ld_slow_path(TCGContext *s, TCGLabelQemuLdst *lb)
{
1279
    TCGReg argreg, datalo, datahi;
1280 1281
    TCGMemOpIdx oi = lb->oi;
    TCGMemOp opc = get_memop(oi);
1282
    void *func;
1283

1284
    reloc_pc24(lb->label_ptr[0], s->code_ptr);
1285 1286 1287 1288 1289 1290 1291

    argreg = tcg_out_arg_reg32(s, TCG_REG_R0, TCG_AREG0);
    if (TARGET_LONG_BITS == 64) {
        argreg = tcg_out_arg_reg64(s, argreg, lb->addrlo_reg, lb->addrhi_reg);
    } else {
        argreg = tcg_out_arg_reg32(s, argreg, lb->addrlo_reg);
    }
1292
    argreg = tcg_out_arg_imm32(s, argreg, oi);
1293 1294 1295 1296 1297 1298
    argreg = tcg_out_arg_reg32(s, argreg, TCG_REG_R14);

    /* For armv6 we can use the canonical unsigned helpers and minimize
       icache usage.  For pre-armv6, use the signed helpers since we do
       not have a single insn sign-extend.  */
    if (use_armv6_instructions) {
1299
        func = qemu_ld_helpers[opc & (MO_BSWAP | MO_SIZE)];
1300
    } else {
1301
        func = qemu_ld_helpers[opc & (MO_BSWAP | MO_SSIZE)];
1302 1303
        if (opc & MO_SIGN) {
            opc = MO_UL;
1304 1305 1306
        }
    }
    tcg_out_call(s, func);
1307

1308 1309
    datalo = lb->datalo_reg;
    datahi = lb->datahi_reg;
1310
    switch (opc & MO_SSIZE) {
1311
    case MO_SB:
1312
        tcg_out_ext8s(s, COND_AL, datalo, TCG_REG_R0);
1313
        break;
1314
    case MO_SW:
1315
        tcg_out_ext16s(s, COND_AL, datalo, TCG_REG_R0);
1316 1317
        break;
    default:
1318
        tcg_out_mov_reg(s, COND_AL, datalo, TCG_REG_R0);
1319
        break;
1320
    case MO_Q:
1321 1322 1323 1324 1325 1326
        if (datalo != TCG_REG_R1) {
            tcg_out_mov_reg(s, COND_AL, datalo, TCG_REG_R0);
            tcg_out_mov_reg(s, COND_AL, datahi, TCG_REG_R1);
        } else if (datahi != TCG_REG_R0) {
            tcg_out_mov_reg(s, COND_AL, datahi, TCG_REG_R1);
            tcg_out_mov_reg(s, COND_AL, datalo, TCG_REG_R0);
1327 1328
        } else {
            tcg_out_mov_reg(s, COND_AL, TCG_REG_TMP, TCG_REG_R0);
1329 1330
            tcg_out_mov_reg(s, COND_AL, datahi, TCG_REG_R1);
            tcg_out_mov_reg(s, COND_AL, datalo, TCG_REG_TMP);
1331
        }
1332 1333 1334
        break;
    }

1335
    tcg_out_goto(s, COND_AL, lb->raddr);
1336 1337 1338 1339
}

static void tcg_out_qemu_st_slow_path(TCGContext *s, TCGLabelQemuLdst *lb)
{
1340
    TCGReg argreg, datalo, datahi;
1341 1342
    TCGMemOpIdx oi = lb->oi;
    TCGMemOp opc = get_memop(oi);
1343

1344
    reloc_pc24(lb->label_ptr[0], s->code_ptr);
1345 1346 1347 1348 1349 1350 1351 1352 1353

    argreg = TCG_REG_R0;
    argreg = tcg_out_arg_reg32(s, argreg, TCG_AREG0);
    if (TARGET_LONG_BITS == 64) {
        argreg = tcg_out_arg_reg64(s, argreg, lb->addrlo_reg, lb->addrhi_reg);
    } else {
        argreg = tcg_out_arg_reg32(s, argreg, lb->addrlo_reg);
    }

1354 1355
    datalo = lb->datalo_reg;
    datahi = lb->datahi_reg;
1356
    switch (opc & MO_SIZE) {
1357
    case MO_8:
1358
        argreg = tcg_out_arg_reg8(s, argreg, datalo);
1359
        break;
1360
    case MO_16:
1361
        argreg = tcg_out_arg_reg16(s, argreg, datalo);
1362
        break;
1363 1364
    case MO_32:
    default:
1365
        argreg = tcg_out_arg_reg32(s, argreg, datalo);
1366
        break;
1367
    case MO_64:
1368
        argreg = tcg_out_arg_reg64(s, argreg, datalo, datahi);
1369 1370 1371
        break;
    }

1372
    argreg = tcg_out_arg_imm32(s, argreg, oi);
1373
    argreg = tcg_out_arg_reg32(s, argreg, TCG_REG_R14);
1374

1375
    /* Tail-call to the helper, which will return to the fast path.  */
1376
    tcg_out_goto(s, COND_AL, qemu_st_helpers[opc & (MO_BSWAP | MO_SIZE)]);
1377
}
1378 1379
#endif /* SOFTMMU */

1380 1381 1382
static inline void tcg_out_qemu_ld_index(TCGContext *s, TCGMemOp opc,
                                         TCGReg datalo, TCGReg datahi,
                                         TCGReg addrlo, TCGReg addend)
1383
{
1384
    TCGMemOp bswap = opc & MO_BSWAP;
1385

1386 1387
    switch (opc & MO_SSIZE) {
    case MO_UB:
1388
        tcg_out_ld8_r(s, COND_AL, datalo, addrlo, addend);
B
balrog 已提交
1389
        break;
1390
    case MO_SB:
1391
        tcg_out_ld8s_r(s, COND_AL, datalo, addrlo, addend);
B
balrog 已提交
1392
        break;
1393
    case MO_UW:
1394
        tcg_out_ld16u_r(s, COND_AL, datalo, addrlo, addend);
1395
        if (bswap) {
1396
            tcg_out_bswap16(s, COND_AL, datalo, datalo);
1397
        }
B
balrog 已提交
1398
        break;
1399
    case MO_SW:
1400
        if (bswap) {
1401 1402
            tcg_out_ld16u_r(s, COND_AL, datalo, addrlo, addend);
            tcg_out_bswap16s(s, COND_AL, datalo, datalo);
1403
        } else {
1404
            tcg_out_ld16s_r(s, COND_AL, datalo, addrlo, addend);
1405
        }
B
balrog 已提交
1406
        break;
1407
    case MO_UL:
B
balrog 已提交
1408
    default:
1409
        tcg_out_ld32_r(s, COND_AL, datalo, addrlo, addend);
1410
        if (bswap) {
1411
            tcg_out_bswap32(s, COND_AL, datalo, datalo);
1412
        }
B
balrog 已提交
1413
        break;
1414
    case MO_Q:
1415
        {
1416 1417
            TCGReg dl = (bswap ? datahi : datalo);
            TCGReg dh = (bswap ? datalo : datahi);
1418

1419 1420 1421
            /* Avoid ldrd for user-only emulation, to handle unaligned.  */
            if (USING_SOFTMMU && use_armv6_instructions
                && (dl & 1) == 0 && dh == dl + 1) {
1422
                tcg_out_ldrd_r(s, COND_AL, dl, addrlo, addend);
1423
            } else if (dl != addend) {
1424
                tcg_out_ld32_rwb(s, COND_AL, dl, addend, addrlo);
1425 1426 1427
                tcg_out_ld32_12(s, COND_AL, dh, addend, 4);
            } else {
                tcg_out_dat_reg(s, COND_AL, ARITH_ADD, TCG_REG_TMP,
1428
                                addend, addrlo, SHIFT_IMM_LSL(0));
1429 1430 1431 1432 1433
                tcg_out_ld32_12(s, COND_AL, dl, TCG_REG_TMP, 0);
                tcg_out_ld32_12(s, COND_AL, dh, TCG_REG_TMP, 4);
            }
            if (bswap) {
                tcg_out_bswap32(s, COND_AL, dl, dl);
1434
                tcg_out_bswap32(s, COND_AL, dh, dh);
1435
            }
1436
        }
B
balrog 已提交
1437 1438
        break;
    }
1439
}
B
balrog 已提交
1440

1441 1442 1443 1444 1445
static inline void tcg_out_qemu_ld_direct(TCGContext *s, TCGMemOp opc,
                                          TCGReg datalo, TCGReg datahi,
                                          TCGReg addrlo)
{
    TCGMemOp bswap = opc & MO_BSWAP;
P
Paul Brook 已提交
1446

1447 1448
    switch (opc & MO_SSIZE) {
    case MO_UB:
1449
        tcg_out_ld8_12(s, COND_AL, datalo, addrlo, 0);
B
balrog 已提交
1450
        break;
1451
    case MO_SB:
1452
        tcg_out_ld8s_8(s, COND_AL, datalo, addrlo, 0);
B
balrog 已提交
1453
        break;
1454
    case MO_UW:
1455
        tcg_out_ld16u_8(s, COND_AL, datalo, addrlo, 0);
1456
        if (bswap) {
1457
            tcg_out_bswap16(s, COND_AL, datalo, datalo);
1458
        }
B
balrog 已提交
1459
        break;
1460
    case MO_SW:
1461
        if (bswap) {
1462 1463
            tcg_out_ld16u_8(s, COND_AL, datalo, addrlo, 0);
            tcg_out_bswap16s(s, COND_AL, datalo, datalo);
1464
        } else {
1465
            tcg_out_ld16s_8(s, COND_AL, datalo, addrlo, 0);
1466
        }
B
balrog 已提交
1467
        break;
1468
    case MO_UL:
B
balrog 已提交
1469
    default:
1470
        tcg_out_ld32_12(s, COND_AL, datalo, addrlo, 0);
1471
        if (bswap) {
1472
            tcg_out_bswap32(s, COND_AL, datalo, datalo);
1473
        }
B
balrog 已提交
1474
        break;
1475
    case MO_Q:
1476 1477 1478 1479
        {
            TCGReg dl = (bswap ? datahi : datalo);
            TCGReg dh = (bswap ? datalo : datahi);

1480 1481 1482
            /* Avoid ldrd for user-only emulation, to handle unaligned.  */
            if (USING_SOFTMMU && use_armv6_instructions
                && (dl & 1) == 0 && dh == dl + 1) {
1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494
                tcg_out_ldrd_8(s, COND_AL, dl, addrlo, 0);
            } else if (dl == addrlo) {
                tcg_out_ld32_12(s, COND_AL, dh, addrlo, bswap ? 0 : 4);
                tcg_out_ld32_12(s, COND_AL, dl, addrlo, bswap ? 4 : 0);
            } else {
                tcg_out_ld32_12(s, COND_AL, dl, addrlo, bswap ? 4 : 0);
                tcg_out_ld32_12(s, COND_AL, dh, addrlo, bswap ? 0 : 4);
            }
            if (bswap) {
                tcg_out_bswap32(s, COND_AL, dl, dl);
                tcg_out_bswap32(s, COND_AL, dh, dh);
            }
A
aurel32 已提交
1495
        }
B
balrog 已提交
1496 1497 1498 1499
        break;
    }
}

1500
static void tcg_out_qemu_ld(TCGContext *s, const TCGArg *args, bool is64)
B
balrog 已提交
1501
{
1502
    TCGReg addrlo, datalo, datahi, addrhi __attribute__((unused));
1503
    TCGMemOpIdx oi;
1504
    TCGMemOp opc;
B
balrog 已提交
1505
#ifdef CONFIG_SOFTMMU
1506
    int mem_index;
1507
    TCGReg addend;
1508
    tcg_insn_unit *label_ptr;
B
balrog 已提交
1509
#endif
1510

1511
    datalo = *args++;
1512
    datahi = (is64 ? *args++ : 0);
1513 1514
    addrlo = *args++;
    addrhi = (TARGET_LONG_BITS == 64 ? *args++ : 0);
1515 1516
    oi = *args++;
    opc = get_memop(oi);
B
balrog 已提交
1517

1518
#ifdef CONFIG_SOFTMMU
1519
    mem_index = get_mmuidx(oi);
1520
    addend = tcg_out_tlb_read(s, addrlo, addrhi, opc, mem_index, 1);
1521 1522 1523 1524 1525 1526 1527

    /* This a conditional BL only to load a pointer within this opcode into LR
       for the slow path.  We will not be using the value for a tail call.  */
    label_ptr = s->code_ptr;
    tcg_out_bl_noaddr(s, COND_NE);

    tcg_out_qemu_ld_index(s, opc, datalo, datahi, addrlo, addend);
B
balrog 已提交
1528

1529 1530
    add_qemu_ldst_label(s, true, oi, datalo, datahi, addrlo, addrhi,
                        s->code_ptr, label_ptr);
1531
#else /* !CONFIG_SOFTMMU */
1532 1533
    if (guest_base) {
        tcg_out_movi(s, TCG_TYPE_PTR, TCG_REG_TMP, guest_base);
1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547
        tcg_out_qemu_ld_index(s, opc, datalo, datahi, addrlo, TCG_REG_TMP);
    } else {
        tcg_out_qemu_ld_direct(s, opc, datalo, datahi, addrlo);
    }
#endif
}

static inline void tcg_out_qemu_st_index(TCGContext *s, int cond, TCGMemOp opc,
                                         TCGReg datalo, TCGReg datahi,
                                         TCGReg addrlo, TCGReg addend)
{
    TCGMemOp bswap = opc & MO_BSWAP;

    switch (opc & MO_SIZE) {
1548
    case MO_8:
1549
        tcg_out_st8_r(s, cond, datalo, addrlo, addend);
B
balrog 已提交
1550
        break;
1551
    case MO_16:
1552
        if (bswap) {
1553 1554
            tcg_out_bswap16st(s, cond, TCG_REG_R0, datalo);
            tcg_out_st16_r(s, cond, TCG_REG_R0, addrlo, addend);
1555
        } else {
1556
            tcg_out_st16_r(s, cond, datalo, addrlo, addend);
1557
        }
B
balrog 已提交
1558
        break;
1559
    case MO_32:
B
balrog 已提交
1560
    default:
1561
        if (bswap) {
1562 1563
            tcg_out_bswap32(s, cond, TCG_REG_R0, datalo);
            tcg_out_st32_r(s, cond, TCG_REG_R0, addrlo, addend);
1564
        } else {
1565
            tcg_out_st32_r(s, cond, datalo, addrlo, addend);
1566
        }
B
balrog 已提交
1567
        break;
1568
    case MO_64:
1569
        /* Avoid strd for user-only emulation, to handle unaligned.  */
1570
        if (bswap) {
1571 1572 1573 1574
            tcg_out_bswap32(s, cond, TCG_REG_R0, datahi);
            tcg_out_st32_rwb(s, cond, TCG_REG_R0, addend, addrlo);
            tcg_out_bswap32(s, cond, TCG_REG_R0, datalo);
            tcg_out_st32_12(s, cond, TCG_REG_R0, addend, 4);
1575
        } else if (USING_SOFTMMU && use_armv6_instructions
1576
                   && (datalo & 1) == 0 && datahi == datalo + 1) {
1577
            tcg_out_strd_r(s, cond, datalo, addrlo, addend);
1578
        } else {
1579 1580
            tcg_out_st32_rwb(s, cond, datalo, addend, addrlo);
            tcg_out_st32_12(s, cond, datahi, addend, 4);
1581
        }
B
balrog 已提交
1582 1583
        break;
    }
1584
}
B
balrog 已提交
1585

1586 1587 1588 1589 1590
static inline void tcg_out_qemu_st_direct(TCGContext *s, TCGMemOp opc,
                                          TCGReg datalo, TCGReg datahi,
                                          TCGReg addrlo)
{
    TCGMemOp bswap = opc & MO_BSWAP;
1591

1592
    switch (opc & MO_SIZE) {
1593
    case MO_8:
1594
        tcg_out_st8_12(s, COND_AL, datalo, addrlo, 0);
B
balrog 已提交
1595
        break;
1596
    case MO_16:
1597
        if (bswap) {
1598 1599
            tcg_out_bswap16st(s, COND_AL, TCG_REG_R0, datalo);
            tcg_out_st16_8(s, COND_AL, TCG_REG_R0, addrlo, 0);
1600
        } else {
1601
            tcg_out_st16_8(s, COND_AL, datalo, addrlo, 0);
1602
        }
B
balrog 已提交
1603
        break;
1604
    case MO_32:
B
balrog 已提交
1605
    default:
1606
        if (bswap) {
1607 1608
            tcg_out_bswap32(s, COND_AL, TCG_REG_R0, datalo);
            tcg_out_st32_12(s, COND_AL, TCG_REG_R0, addrlo, 0);
1609
        } else {
1610
            tcg_out_st32_12(s, COND_AL, datalo, addrlo, 0);
1611
        }
B
balrog 已提交
1612
        break;
1613
    case MO_64:
1614
        /* Avoid strd for user-only emulation, to handle unaligned.  */
1615
        if (bswap) {
1616 1617 1618 1619
            tcg_out_bswap32(s, COND_AL, TCG_REG_R0, datahi);
            tcg_out_st32_12(s, COND_AL, TCG_REG_R0, addrlo, 0);
            tcg_out_bswap32(s, COND_AL, TCG_REG_R0, datalo);
            tcg_out_st32_12(s, COND_AL, TCG_REG_R0, addrlo, 4);
1620
        } else if (USING_SOFTMMU && use_armv6_instructions
1621 1622
                   && (datalo & 1) == 0 && datahi == datalo + 1) {
            tcg_out_strd_8(s, COND_AL, datalo, addrlo, 0);
1623
        } else {
1624 1625
            tcg_out_st32_12(s, COND_AL, datalo, addrlo, 0);
            tcg_out_st32_12(s, COND_AL, datahi, addrlo, 4);
1626
        }
B
balrog 已提交
1627 1628
        break;
    }
1629 1630 1631 1632 1633
}

static void tcg_out_qemu_st(TCGContext *s, const TCGArg *args, bool is64)
{
    TCGReg addrlo, datalo, datahi, addrhi __attribute__((unused));
1634
    TCGMemOpIdx oi;
1635 1636 1637 1638
    TCGMemOp opc;
#ifdef CONFIG_SOFTMMU
    int mem_index;
    TCGReg addend;
1639
    tcg_insn_unit *label_ptr;
1640 1641 1642 1643 1644 1645
#endif

    datalo = *args++;
    datahi = (is64 ? *args++ : 0);
    addrlo = *args++;
    addrhi = (TARGET_LONG_BITS == 64 ? *args++ : 0);
1646 1647
    oi = *args++;
    opc = get_memop(oi);
1648 1649

#ifdef CONFIG_SOFTMMU
1650
    mem_index = get_mmuidx(oi);
1651
    addend = tcg_out_tlb_read(s, addrlo, addrhi, opc, mem_index, 0);
1652 1653 1654 1655 1656 1657 1658

    tcg_out_qemu_st_index(s, COND_EQ, opc, datalo, datahi, addrlo, addend);

    /* The conditional call must come last, as we're going to return here.  */
    label_ptr = s->code_ptr;
    tcg_out_bl_noaddr(s, COND_NE);

1659 1660
    add_qemu_ldst_label(s, false, oi, datalo, datahi, addrlo, addrhi,
                        s->code_ptr, label_ptr);
1661
#else /* !CONFIG_SOFTMMU */
1662 1663
    if (guest_base) {
        tcg_out_movi(s, TCG_TYPE_PTR, TCG_REG_TMP, guest_base);
1664 1665 1666 1667 1668
        tcg_out_qemu_st_index(s, COND_AL, opc, datalo,
                              datahi, addrlo, TCG_REG_TMP);
    } else {
        tcg_out_qemu_st_direct(s, opc, datalo, datahi, addrlo);
    }
B
balrog 已提交
1669 1670 1671
#endif
}

1672
static tcg_insn_unit *tb_ret_addr;
B
balrog 已提交
1673

1674
static inline void tcg_out_op(TCGContext *s, TCGOpcode opc,
B
balrog 已提交
1675 1676
                const TCGArg *args, const int *const_args)
{
1677
    TCGArg a0, a1, a2, a3, a4, a5;
B
balrog 已提交
1678 1679 1680 1681
    int c;

    switch (opc) {
    case INDEX_op_exit_tb:
1682 1683
        tcg_out_movi32(s, COND_AL, TCG_REG_R0, args[0]);
        tcg_out_goto(s, COND_AL, tb_ret_addr);
B
balrog 已提交
1684 1685
        break;
    case INDEX_op_goto_tb:
1686
        if (s->tb_jmp_insn_offset) {
B
balrog 已提交
1687
            /* Direct jump method */
1688
            s->tb_jmp_insn_offset[args[0]] = tcg_current_code_size(s);
1689
            tcg_out_b_noaddr(s, COND_AL);
B
balrog 已提交
1690 1691
        } else {
            /* Indirect jump method */
1692
            intptr_t ptr = (intptr_t)(s->tb_jmp_target_addr + args[0]);
1693 1694
            tcg_out_movi32(s, COND_AL, TCG_REG_R0, ptr & ~0xfff);
            tcg_out_ld32_12(s, COND_AL, TCG_REG_PC, TCG_REG_R0, ptr & 0xfff);
B
balrog 已提交
1695
        }
1696
        s->tb_jmp_reset_offset[args[0]] = tcg_current_code_size(s);
B
balrog 已提交
1697 1698
        break;
    case INDEX_op_br:
1699
        tcg_out_goto_label(s, COND_AL, arg_label(args[0]));
B
balrog 已提交
1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717
        break;

    case INDEX_op_ld8u_i32:
        tcg_out_ld8u(s, COND_AL, args[0], args[1], args[2]);
        break;
    case INDEX_op_ld8s_i32:
        tcg_out_ld8s(s, COND_AL, args[0], args[1], args[2]);
        break;
    case INDEX_op_ld16u_i32:
        tcg_out_ld16u(s, COND_AL, args[0], args[1], args[2]);
        break;
    case INDEX_op_ld16s_i32:
        tcg_out_ld16s(s, COND_AL, args[0], args[1], args[2]);
        break;
    case INDEX_op_ld_i32:
        tcg_out_ld32u(s, COND_AL, args[0], args[1], args[2]);
        break;
    case INDEX_op_st8_i32:
1718
        tcg_out_st8(s, COND_AL, args[0], args[1], args[2]);
B
balrog 已提交
1719 1720
        break;
    case INDEX_op_st16_i32:
1721
        tcg_out_st16(s, COND_AL, args[0], args[1], args[2]);
B
balrog 已提交
1722 1723 1724 1725 1726
        break;
    case INDEX_op_st_i32:
        tcg_out_st32(s, COND_AL, args[0], args[1], args[2]);
        break;

P
Peter Maydell 已提交
1727 1728 1729 1730
    case INDEX_op_movcond_i32:
        /* Constraints mean that v2 is always in the same register as dest,
         * so we only need to do "if condition passed, move v1 to dest".
         */
1731 1732 1733 1734
        tcg_out_dat_rIN(s, COND_AL, ARITH_CMP, ARITH_CMN, 0,
                        args[1], args[2], const_args[2]);
        tcg_out_dat_rIK(s, tcg_cond_to_arm_cond[args[5]], ARITH_MOV,
                        ARITH_MVN, args[0], 0, args[3], const_args[3]);
P
Peter Maydell 已提交
1735
        break;
B
balrog 已提交
1736
    case INDEX_op_add_i32:
1737 1738 1739
        tcg_out_dat_rIN(s, COND_AL, ARITH_ADD, ARITH_SUB,
                        args[0], args[1], args[2], const_args[2]);
        break;
B
balrog 已提交
1740
    case INDEX_op_sub_i32:
1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751
        if (const_args[1]) {
            if (const_args[2]) {
                tcg_out_movi32(s, COND_AL, args[0], args[1] - args[2]);
            } else {
                tcg_out_dat_rI(s, COND_AL, ARITH_RSB,
                               args[0], args[2], args[1], 1);
            }
        } else {
            tcg_out_dat_rIN(s, COND_AL, ARITH_SUB, ARITH_ADD,
                            args[0], args[1], args[2], const_args[2]);
        }
1752
        break;
B
balrog 已提交
1753
    case INDEX_op_and_i32:
1754 1755 1756
        tcg_out_dat_rIK(s, COND_AL, ARITH_AND, ARITH_BIC,
                        args[0], args[1], args[2], const_args[2]);
        break;
A
Aurelien Jarno 已提交
1757
    case INDEX_op_andc_i32:
1758 1759 1760
        tcg_out_dat_rIK(s, COND_AL, ARITH_BIC, ARITH_AND,
                        args[0], args[1], args[2], const_args[2]);
        break;
B
balrog 已提交
1761 1762 1763 1764 1765 1766 1767
    case INDEX_op_or_i32:
        c = ARITH_ORR;
        goto gen_arith;
    case INDEX_op_xor_i32:
        c = ARITH_EOR;
        /* Fall through.  */
    gen_arith:
1768
        tcg_out_dat_rI(s, COND_AL, c, args[0], args[1], args[2], const_args[2]);
B
balrog 已提交
1769 1770
        break;
    case INDEX_op_add2_i32:
1771 1772 1773
        a0 = args[0], a1 = args[1], a2 = args[2];
        a3 = args[3], a4 = args[4], a5 = args[5];
        if (a0 == a3 || (a0 == a5 && !const_args[5])) {
1774
            a0 = TCG_REG_TMP;
1775 1776 1777 1778 1779 1780
        }
        tcg_out_dat_rIN(s, COND_AL, ARITH_ADD | TO_CPSR, ARITH_SUB | TO_CPSR,
                        a0, a2, a4, const_args[4]);
        tcg_out_dat_rIK(s, COND_AL, ARITH_ADC, ARITH_SBC,
                        a1, a3, a5, const_args[5]);
        tcg_out_mov_reg(s, COND_AL, args[0], a0);
B
balrog 已提交
1781 1782
        break;
    case INDEX_op_sub2_i32:
1783 1784 1785
        a0 = args[0], a1 = args[1], a2 = args[2];
        a3 = args[3], a4 = args[4], a5 = args[5];
        if ((a0 == a3 && !const_args[3]) || (a0 == a5 && !const_args[5])) {
1786
            a0 = TCG_REG_TMP;
1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808
        }
        if (const_args[2]) {
            if (const_args[4]) {
                tcg_out_movi32(s, COND_AL, a0, a4);
                a4 = a0;
            }
            tcg_out_dat_rI(s, COND_AL, ARITH_RSB | TO_CPSR, a0, a4, a2, 1);
        } else {
            tcg_out_dat_rIN(s, COND_AL, ARITH_SUB | TO_CPSR,
                            ARITH_ADD | TO_CPSR, a0, a2, a4, const_args[4]);
        }
        if (const_args[3]) {
            if (const_args[5]) {
                tcg_out_movi32(s, COND_AL, a1, a5);
                a5 = a1;
            }
            tcg_out_dat_rI(s, COND_AL, ARITH_RSC, a1, a5, a3, 1);
        } else {
            tcg_out_dat_rIK(s, COND_AL, ARITH_SBC, ARITH_ADC,
                            a1, a3, a5, const_args[5]);
        }
        tcg_out_mov_reg(s, COND_AL, args[0], a0);
B
balrog 已提交
1809
        break;
B
balrog 已提交
1810 1811 1812
    case INDEX_op_neg_i32:
        tcg_out_dat_imm(s, COND_AL, ARITH_RSB, args[0], args[1], 0);
        break;
L
Laurent Desnogues 已提交
1813 1814 1815 1816
    case INDEX_op_not_i32:
        tcg_out_dat_reg(s, COND_AL,
                        ARITH_MVN, args[0], 0, args[1], SHIFT_IMM_LSL(0));
        break;
B
balrog 已提交
1817 1818 1819 1820 1821 1822
    case INDEX_op_mul_i32:
        tcg_out_mul32(s, COND_AL, args[0], args[1], args[2]);
        break;
    case INDEX_op_mulu2_i32:
        tcg_out_umull32(s, COND_AL, args[0], args[1], args[2], args[3]);
        break;
R
Richard Henderson 已提交
1823 1824 1825
    case INDEX_op_muls2_i32:
        tcg_out_smull32(s, COND_AL, args[0], args[1], args[2], args[3]);
        break;
B
balrog 已提交
1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837
    /* XXX: Perhaps args[2] & 0x1f is wrong */
    case INDEX_op_shl_i32:
        c = const_args[2] ?
                SHIFT_IMM_LSL(args[2] & 0x1f) : SHIFT_REG_LSL(args[2]);
        goto gen_shift32;
    case INDEX_op_shr_i32:
        c = const_args[2] ? (args[2] & 0x1f) ? SHIFT_IMM_LSR(args[2] & 0x1f) :
                SHIFT_IMM_LSL(0) : SHIFT_REG_LSR(args[2]);
        goto gen_shift32;
    case INDEX_op_sar_i32:
        c = const_args[2] ? (args[2] & 0x1f) ? SHIFT_IMM_ASR(args[2] & 0x1f) :
                SHIFT_IMM_LSL(0) : SHIFT_REG_ASR(args[2]);
A
Aurelien Jarno 已提交
1838 1839 1840 1841
        goto gen_shift32;
    case INDEX_op_rotr_i32:
        c = const_args[2] ? (args[2] & 0x1f) ? SHIFT_IMM_ROR(args[2] & 0x1f) :
                SHIFT_IMM_LSL(0) : SHIFT_REG_ROR(args[2]);
B
balrog 已提交
1842 1843 1844 1845 1846
        /* Fall through.  */
    gen_shift32:
        tcg_out_dat_reg(s, COND_AL, ARITH_MOV, args[0], 0, args[1], c);
        break;

A
Aurelien Jarno 已提交
1847 1848 1849 1850 1851 1852 1853
    case INDEX_op_rotl_i32:
        if (const_args[2]) {
            tcg_out_dat_reg(s, COND_AL, ARITH_MOV, args[0], 0, args[1],
                            ((0x20 - args[2]) & 0x1f) ?
                            SHIFT_IMM_ROR((0x20 - args[2]) & 0x1f) :
                            SHIFT_IMM_LSL(0));
        } else {
1854
            tcg_out_dat_imm(s, COND_AL, ARITH_RSB, TCG_REG_TMP, args[2], 0x20);
A
Aurelien Jarno 已提交
1855
            tcg_out_dat_reg(s, COND_AL, ARITH_MOV, args[0], 0, args[1],
1856
                            SHIFT_REG_ROR(TCG_REG_TMP));
A
Aurelien Jarno 已提交
1857 1858 1859
        }
        break;

B
balrog 已提交
1860
    case INDEX_op_brcond_i32:
1861
        tcg_out_dat_rIN(s, COND_AL, ARITH_CMP, ARITH_CMN, 0,
1862
                       args[0], args[1], const_args[1]);
1863 1864
        tcg_out_goto_label(s, tcg_cond_to_arm_cond[args[2]],
                           arg_label(args[3]));
B
balrog 已提交
1865 1866 1867 1868 1869 1870 1871 1872 1873 1874
        break;
    case INDEX_op_brcond2_i32:
        /* The resulting conditions are:
         * TCG_COND_EQ    -->  a0 == a2 && a1 == a3,
         * TCG_COND_NE    --> (a0 != a2 && a1 == a3) ||  a1 != a3,
         * TCG_COND_LT(U) --> (a0 <  a2 && a1 == a3) ||  a1 <  a3,
         * TCG_COND_GE(U) --> (a0 >= a2 && a1 == a3) || (a1 >= a3 && a1 != a3),
         * TCG_COND_LE(U) --> (a0 <= a2 && a1 == a3) || (a1 <= a3 && a1 != a3),
         * TCG_COND_GT(U) --> (a0 >  a2 && a1 == a3) ||  a1 >  a3,
         */
1875 1876 1877 1878
        tcg_out_dat_rIN(s, COND_AL, ARITH_CMP, ARITH_CMN, 0,
                        args[1], args[3], const_args[3]);
        tcg_out_dat_rIN(s, COND_EQ, ARITH_CMP, ARITH_CMN, 0,
                        args[0], args[2], const_args[2]);
1879 1880
        tcg_out_goto_label(s, tcg_cond_to_arm_cond[args[4]],
                           arg_label(args[5]));
B
balrog 已提交
1881
        break;
A
Aurelien Jarno 已提交
1882
    case INDEX_op_setcond_i32:
1883 1884
        tcg_out_dat_rIN(s, COND_AL, ARITH_CMP, ARITH_CMN, 0,
                        args[1], args[2], const_args[2]);
A
Aurelien Jarno 已提交
1885 1886 1887 1888 1889
        tcg_out_dat_imm(s, tcg_cond_to_arm_cond[args[3]],
                        ARITH_MOV, args[0], 0, 1);
        tcg_out_dat_imm(s, tcg_cond_to_arm_cond[tcg_invert_cond(args[3])],
                        ARITH_MOV, args[0], 0, 0);
        break;
A
Aurelien Jarno 已提交
1890 1891
    case INDEX_op_setcond2_i32:
        /* See brcond2_i32 comment */
1892 1893 1894 1895
        tcg_out_dat_rIN(s, COND_AL, ARITH_CMP, ARITH_CMN, 0,
                        args[2], args[4], const_args[4]);
        tcg_out_dat_rIN(s, COND_EQ, ARITH_CMP, ARITH_CMN, 0,
                        args[1], args[3], const_args[3]);
A
Aurelien Jarno 已提交
1896 1897 1898 1899
        tcg_out_dat_imm(s, tcg_cond_to_arm_cond[args[5]],
                        ARITH_MOV, args[0], 0, 1);
        tcg_out_dat_imm(s, tcg_cond_to_arm_cond[tcg_invert_cond(args[5])],
                        ARITH_MOV, args[0], 0, 0);
A
Andrzej Zaborowski 已提交
1900
        break;
B
balrog 已提交
1901

1902 1903
    case INDEX_op_qemu_ld_i32:
        tcg_out_qemu_ld(s, args, 0);
B
balrog 已提交
1904
        break;
1905 1906
    case INDEX_op_qemu_ld_i64:
        tcg_out_qemu_ld(s, args, 1);
B
balrog 已提交
1907
        break;
1908 1909
    case INDEX_op_qemu_st_i32:
        tcg_out_qemu_st(s, args, 0);
B
balrog 已提交
1910
        break;
1911 1912
    case INDEX_op_qemu_st_i64:
        tcg_out_qemu_st(s, args, 1);
B
balrog 已提交
1913 1914
        break;

A
Aurelien Jarno 已提交
1915 1916 1917 1918 1919 1920 1921
    case INDEX_op_bswap16_i32:
        tcg_out_bswap16(s, COND_AL, args[0], args[1]);
        break;
    case INDEX_op_bswap32_i32:
        tcg_out_bswap32(s, COND_AL, args[0], args[1]);
        break;

B
balrog 已提交
1922
    case INDEX_op_ext8s_i32:
A
Aurelien Jarno 已提交
1923
        tcg_out_ext8s(s, COND_AL, args[0], args[1]);
B
balrog 已提交
1924 1925
        break;
    case INDEX_op_ext16s_i32:
A
Aurelien Jarno 已提交
1926 1927 1928 1929
        tcg_out_ext16s(s, COND_AL, args[0], args[1]);
        break;
    case INDEX_op_ext16u_i32:
        tcg_out_ext16u(s, COND_AL, args[0], args[1]);
B
balrog 已提交
1930 1931
        break;

1932 1933 1934 1935 1936
    case INDEX_op_deposit_i32:
        tcg_out_deposit(s, COND_AL, args[0], args[2],
                        args[3], args[4], const_args[2]);
        break;

1937 1938 1939 1940 1941 1942 1943
    case INDEX_op_div_i32:
        tcg_out_sdiv(s, COND_AL, args[0], args[1], args[2]);
        break;
    case INDEX_op_divu_i32:
        tcg_out_udiv(s, COND_AL, args[0], args[1], args[2]);
        break;

P
Pranith Kumar 已提交
1944 1945 1946 1947
    case INDEX_op_mb:
        tcg_out_mb(s, args[0]);
        break;

1948 1949 1950
    case INDEX_op_mov_i32:  /* Always emitted via tcg_out_mov.  */
    case INDEX_op_movi_i32: /* Always emitted via tcg_out_movi.  */
    case INDEX_op_call:     /* Always emitted via tcg_out_call.  */
B
balrog 已提交
1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970
    default:
        tcg_abort();
    }
}

static const TCGTargetOpDef arm_op_defs[] = {
    { INDEX_op_exit_tb, { } },
    { INDEX_op_goto_tb, { } },
    { INDEX_op_br, { } },

    { INDEX_op_ld8u_i32, { "r", "r" } },
    { INDEX_op_ld8s_i32, { "r", "r" } },
    { INDEX_op_ld16u_i32, { "r", "r" } },
    { INDEX_op_ld16s_i32, { "r", "r" } },
    { INDEX_op_ld_i32, { "r", "r" } },
    { INDEX_op_st8_i32, { "r", "r" } },
    { INDEX_op_st16_i32, { "r", "r" } },
    { INDEX_op_st_i32, { "r", "r" } },

    /* TODO: "r", "r", "ri" */
1971
    { INDEX_op_add_i32, { "r", "r", "rIN" } },
1972
    { INDEX_op_sub_i32, { "r", "rI", "rIN" } },
B
balrog 已提交
1973 1974
    { INDEX_op_mul_i32, { "r", "r", "r" } },
    { INDEX_op_mulu2_i32, { "r", "r", "r", "r" } },
R
Richard Henderson 已提交
1975
    { INDEX_op_muls2_i32, { "r", "r", "r", "r" } },
1976 1977
    { INDEX_op_and_i32, { "r", "r", "rIK" } },
    { INDEX_op_andc_i32, { "r", "r", "rIK" } },
1978 1979
    { INDEX_op_or_i32, { "r", "r", "rI" } },
    { INDEX_op_xor_i32, { "r", "r", "rI" } },
B
balrog 已提交
1980
    { INDEX_op_neg_i32, { "r", "r" } },
L
Laurent Desnogues 已提交
1981
    { INDEX_op_not_i32, { "r", "r" } },
B
balrog 已提交
1982 1983 1984 1985

    { INDEX_op_shl_i32, { "r", "r", "ri" } },
    { INDEX_op_shr_i32, { "r", "r", "ri" } },
    { INDEX_op_sar_i32, { "r", "r", "ri" } },
A
Aurelien Jarno 已提交
1986 1987
    { INDEX_op_rotl_i32, { "r", "r", "ri" } },
    { INDEX_op_rotr_i32, { "r", "r", "ri" } },
B
balrog 已提交
1988

1989 1990 1991
    { INDEX_op_brcond_i32, { "r", "rIN" } },
    { INDEX_op_setcond_i32, { "r", "r", "rIN" } },
    { INDEX_op_movcond_i32, { "r", "r", "rIN", "rIK", "0" } },
B
balrog 已提交
1992

1993 1994
    { INDEX_op_add2_i32, { "r", "r", "r", "r", "rIN", "rIK" } },
    { INDEX_op_sub2_i32, { "r", "r", "rI", "rI", "rIN", "rIK" } },
1995 1996
    { INDEX_op_brcond2_i32, { "r", "r", "rIN", "rIN" } },
    { INDEX_op_setcond2_i32, { "r", "r", "r", "rIN", "rIN" } },
B
balrog 已提交
1997

1998
#if TARGET_LONG_BITS == 32
1999 2000 2001 2002
    { INDEX_op_qemu_ld_i32, { "r", "l" } },
    { INDEX_op_qemu_ld_i64, { "r", "r", "l" } },
    { INDEX_op_qemu_st_i32, { "s", "s" } },
    { INDEX_op_qemu_st_i64, { "s", "s", "s" } },
2003
#else
2004 2005 2006 2007
    { INDEX_op_qemu_ld_i32, { "r", "l", "l" } },
    { INDEX_op_qemu_ld_i64, { "r", "r", "l", "l" } },
    { INDEX_op_qemu_st_i32, { "s", "s", "s" } },
    { INDEX_op_qemu_st_i64, { "s", "s", "s", "s" } },
2008
#endif
B
balrog 已提交
2009

A
Aurelien Jarno 已提交
2010 2011 2012
    { INDEX_op_bswap16_i32, { "r", "r" } },
    { INDEX_op_bswap32_i32, { "r", "r" } },

B
balrog 已提交
2013 2014
    { INDEX_op_ext8s_i32, { "r", "r" } },
    { INDEX_op_ext16s_i32, { "r", "r" } },
A
Aurelien Jarno 已提交
2015
    { INDEX_op_ext16u_i32, { "r", "r" } },
B
balrog 已提交
2016

2017 2018
    { INDEX_op_deposit_i32, { "r", "0", "rZ" } },

2019 2020 2021
    { INDEX_op_div_i32, { "r", "r", "r" } },
    { INDEX_op_divu_i32, { "r", "r", "r" } },

P
Pranith Kumar 已提交
2022
    { INDEX_op_mb, { } },
B
balrog 已提交
2023 2024 2025
    { -1 },
};

2026
static void tcg_target_init(TCGContext *s)
B
balrog 已提交
2027
{
2028 2029
    /* Only probe for the platform and capabilities if we havn't already
       determined maximum values at compile time.  */
R
Richard Henderson 已提交
2030
#ifndef use_idiv_instructions
2031
    {
R
Richard Henderson 已提交
2032
        unsigned long hwcap = qemu_getauxval(AT_HWCAP);
2033 2034
        use_idiv_instructions = (hwcap & HWCAP_ARM_IDIVA) != 0;
    }
R
Richard Henderson 已提交
2035
#endif
2036
    if (__ARM_ARCH < 7) {
R
Richard Henderson 已提交
2037
        const char *pl = (const char *)qemu_getauxval(AT_PLATFORM);
2038 2039 2040 2041
        if (pl != NULL && pl[0] == 'v' && pl[1] >= '4' && pl[1] <= '9') {
            arm_arch = pl[1] - '0';
        }
    }
2042

2043
    tcg_regset_set32(tcg_target_available_regs[TCG_TYPE_I32], 0, 0xffff);
B
balrog 已提交
2044
    tcg_regset_set32(tcg_target_call_clobber_regs, 0,
2045 2046 2047 2048 2049 2050
                     (1 << TCG_REG_R0) |
                     (1 << TCG_REG_R1) |
                     (1 << TCG_REG_R2) |
                     (1 << TCG_REG_R3) |
                     (1 << TCG_REG_R12) |
                     (1 << TCG_REG_R14));
B
balrog 已提交
2051 2052 2053

    tcg_regset_clear(s->reserved_regs);
    tcg_regset_set_reg(s->reserved_regs, TCG_REG_CALL_STACK);
2054
    tcg_regset_set_reg(s->reserved_regs, TCG_REG_TMP);
2055
    tcg_regset_set_reg(s->reserved_regs, TCG_REG_PC);
B
balrog 已提交
2056 2057 2058 2059

    tcg_add_target_add_op_defs(arm_op_defs);
}

2060
static inline void tcg_out_ld(TCGContext *s, TCGType type, TCGReg arg,
2061
                              TCGReg arg1, intptr_t arg2)
B
balrog 已提交
2062 2063 2064 2065
{
    tcg_out_ld32u(s, COND_AL, arg, arg1, arg2);
}

2066
static inline void tcg_out_st(TCGContext *s, TCGType type, TCGReg arg,
2067
                              TCGReg arg1, intptr_t arg2)
B
balrog 已提交
2068 2069 2070 2071
{
    tcg_out_st32(s, COND_AL, arg, arg1, arg2);
}

2072 2073 2074 2075 2076 2077
static inline bool tcg_out_sti(TCGContext *s, TCGType type, TCGArg val,
                               TCGReg base, intptr_t ofs)
{
    return false;
}

2078 2079
static inline void tcg_out_mov(TCGContext *s, TCGType type,
                               TCGReg ret, TCGReg arg)
B
balrog 已提交
2080 2081 2082 2083 2084
{
    tcg_out_dat_reg(s, COND_AL, ARITH_MOV, ret, 0, arg, SHIFT_IMM_LSL(0));
}

static inline void tcg_out_movi(TCGContext *s, TCGType type,
2085
                                TCGReg ret, tcg_target_long arg)
B
balrog 已提交
2086 2087 2088 2089
{
    tcg_out_movi32(s, COND_AL, ret, arg);
}

2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101
/* Compute frame size via macros, to share between tcg_target_qemu_prologue
   and tcg_register_jit.  */

#define PUSH_SIZE  ((11 - 4 + 1 + 1) * sizeof(tcg_target_long))

#define FRAME_SIZE \
    ((PUSH_SIZE \
      + TCG_STATIC_CALL_ARGS_SIZE \
      + CPU_TEMP_BUF_NLONGS * sizeof(long) \
      + TCG_TARGET_STACK_ALIGN - 1) \
     & -TCG_TARGET_STACK_ALIGN)

2102
static void tcg_target_qemu_prologue(TCGContext *s)
B
balrog 已提交
2103
{
2104
    int stack_addend;
2105 2106 2107 2108

    /* Calling convention requires us to save r4-r11 and lr.  */
    /* stmdb sp!, { r4 - r11, lr } */
    tcg_out32(s, (COND_AL << 28) | 0x092d4ff0);
B
Blue Swirl 已提交
2109

2110 2111
    /* Reserve callee argument and tcg temp space.  */
    stack_addend = FRAME_SIZE - PUSH_SIZE;
2112 2113

    tcg_out_dat_rI(s, COND_AL, ARITH_SUB, TCG_REG_CALL_STACK,
2114
                   TCG_REG_CALL_STACK, stack_addend, 1);
2115 2116
    tcg_set_frame(s, TCG_REG_CALL_STACK, TCG_STATIC_CALL_ARGS_SIZE,
                  CPU_TEMP_BUF_NLONGS * sizeof(long));
2117

B
Blue Swirl 已提交
2118
    tcg_out_mov(s, TCG_TYPE_PTR, TCG_AREG0, tcg_target_call_iarg_regs[0]);
B
balrog 已提交
2119

B
Blue Swirl 已提交
2120
    tcg_out_bx(s, COND_AL, tcg_target_call_iarg_regs[1]);
B
balrog 已提交
2121 2122
    tb_ret_addr = s->code_ptr;

2123 2124
    /* Epilogue.  We branch here via tb_ret_addr.  */
    tcg_out_dat_rI(s, COND_AL, ARITH_ADD, TCG_REG_CALL_STACK,
2125
                   TCG_REG_CALL_STACK, stack_addend, 1);
2126 2127 2128

    /* ldmia sp!, { r4 - r11, pc } */
    tcg_out32(s, (COND_AL << 28) | 0x08bd8ff0);
B
balrog 已提交
2129
}
2130 2131

typedef struct {
2132
    DebugFrameHeader h;
2133 2134 2135 2136 2137 2138 2139 2140 2141
    uint8_t fde_def_cfa[4];
    uint8_t fde_reg_ofs[18];
} DebugFrame;

#define ELF_HOST_MACHINE EM_ARM

/* We're expecting a 2 byte uleb128 encoded value.  */
QEMU_BUILD_BUG_ON(FRAME_SIZE >= (1 << 14));

2142 2143 2144 2145 2146 2147 2148
static const DebugFrame debug_frame = {
    .h.cie.len = sizeof(DebugFrameCIE)-4, /* length after .len member */
    .h.cie.id = -1,
    .h.cie.version = 1,
    .h.cie.code_align = 1,
    .h.cie.data_align = 0x7c,             /* sleb128 -4 */
    .h.cie.return_column = 14,
2149 2150

    /* Total FDE size does not include the "len" member.  */
2151
    .h.fde.len = sizeof(DebugFrame) - offsetof(DebugFrame, h.fde.cie_offset),
2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175

    .fde_def_cfa = {
        12, 13,                         /* DW_CFA_def_cfa sp, ... */
        (FRAME_SIZE & 0x7f) | 0x80,     /* ... uleb128 FRAME_SIZE */
        (FRAME_SIZE >> 7)
    },
    .fde_reg_ofs = {
        /* The following must match the stmdb in the prologue.  */
        0x8e, 1,                        /* DW_CFA_offset, lr, -4 */
        0x8b, 2,                        /* DW_CFA_offset, r11, -8 */
        0x8a, 3,                        /* DW_CFA_offset, r10, -12 */
        0x89, 4,                        /* DW_CFA_offset, r9, -16 */
        0x88, 5,                        /* DW_CFA_offset, r8, -20 */
        0x87, 6,                        /* DW_CFA_offset, r7, -24 */
        0x86, 7,                        /* DW_CFA_offset, r6, -28 */
        0x85, 8,                        /* DW_CFA_offset, r5, -32 */
        0x84, 9,                        /* DW_CFA_offset, r4, -36 */
    }
};

void tcg_register_jit(void *buf, size_t buf_size)
{
    tcg_register_jit_int(buf, buf_size, &debug_frame, sizeof(debug_frame));
}