tcg-target.c 53.2 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 25 26

#ifndef NDEBUG
static const char * const tcg_target_reg_names[TCG_TARGET_NB_REGS] = {
B
balrog 已提交
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
    "%r0",
    "%r1",
    "%r2",
    "%r3",
    "%r4",
    "%r5",
    "%r6",
    "%r7",
    "%r8",
    "%r9",
    "%r10",
    "%r11",
    "%r12",
    "%r13",
    "%r14",
};
43
#endif
B
balrog 已提交
44

45
static const int tcg_target_reg_alloc_order[] = {
B
balrog 已提交
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
    TCG_REG_R0,
    TCG_REG_R1,
    TCG_REG_R2,
    TCG_REG_R3,
    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_R12,
    TCG_REG_R13,
    TCG_REG_R14,
};

63
static const int tcg_target_call_iarg_regs[4] = {
B
balrog 已提交
64 65
    TCG_REG_R0, TCG_REG_R1, TCG_REG_R2, TCG_REG_R3
};
66
static const int tcg_target_call_oarg_regs[2] = {
B
balrog 已提交
67 68 69
    TCG_REG_R0, TCG_REG_R1
};

B
balrog 已提交
70
static void patch_reloc(uint8_t *code_ptr, int type,
B
balrog 已提交
71 72 73 74 75 76 77 78 79 80 81 82 83
                tcg_target_long value, tcg_target_long addend)
{
    switch (type) {
    case R_ARM_ABS32:
        *(uint32_t *) code_ptr = value;
        break;

    case R_ARM_CALL:
    case R_ARM_JUMP24:
    default:
        tcg_abort();

    case R_ARM_PC24:
B
balrog 已提交
84
        *(uint32_t *) code_ptr = ((*(uint32_t *) code_ptr) & 0xff000000) |
85
                (((value - ((tcg_target_long) code_ptr + 8)) >> 2) & 0xffffff);
B
balrog 已提交
86 87 88 89 90 91 92 93 94 95 96
        break;
    }
}

/* maximum number of register used for input function arguments */
static inline int tcg_target_get_call_iarg_regs_count(int flags)
{
    return 4;
}

/* parse target specific constraints */
97
static int target_parse_constraint(TCGArgConstraint *ct, const char **pct_str)
B
balrog 已提交
98 99 100 101 102
{
    const char *ct_str;

    ct_str = *pct_str;
    switch (ct_str[0]) {
103 104 105 106
    case 'I':
         ct->ct |= TCG_CT_CONST_ARM;
         break;

B
balrog 已提交
107 108 109 110 111 112 113 114 115 116 117 118
    case 'r':
#ifndef CONFIG_SOFTMMU
    case 'd':
    case 'D':
    case 'x':
    case 'X':
#endif
        ct->ct |= TCG_CT_REG;
        tcg_regset_set32(ct->u.regs, 0, (1 << TCG_TARGET_NB_REGS) - 1);
        break;

#ifdef CONFIG_SOFTMMU
119
    /* qemu_ld/st inputs (unless 'X', 'd' or 'D') */
B
balrog 已提交
120 121 122 123 124 125 126
    case 'x':
        ct->ct |= TCG_CT_REG;
        tcg_regset_set32(ct->u.regs, 0, (1 << TCG_TARGET_NB_REGS) - 1);
        tcg_regset_reset_reg(ct->u.regs, TCG_REG_R0);
        tcg_regset_reset_reg(ct->u.regs, TCG_REG_R1);
        break;

127 128 129 130 131 132 133 134
    /* qemu_ld64 data_reg */
    case 'd':
        ct->ct |= TCG_CT_REG;
        tcg_regset_set32(ct->u.regs, 0, (1 << TCG_TARGET_NB_REGS) - 1);
        /* r1 is still needed to load data_reg2, so don't use it.  */
        tcg_regset_reset_reg(ct->u.regs, TCG_REG_R1);
        break;

B
balrog 已提交
135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181
    /* qemu_ld/st64 data_reg2 */
    case 'D':
        ct->ct |= TCG_CT_REG;
        tcg_regset_set32(ct->u.regs, 0, (1 << TCG_TARGET_NB_REGS) - 1);
        /* r0, r1 and optionally r2 will be overwritten by the address
         * and the low word of data, so don't use these.  */
        tcg_regset_reset_reg(ct->u.regs, TCG_REG_R0);
        tcg_regset_reset_reg(ct->u.regs, TCG_REG_R1);
# if TARGET_LONG_BITS == 64
        tcg_regset_reset_reg(ct->u.regs, TCG_REG_R2);
# endif
        break;

# if TARGET_LONG_BITS == 64
    /* qemu_ld/st addr_reg2 */
    case 'X':
        ct->ct |= TCG_CT_REG;
        tcg_regset_set32(ct->u.regs, 0, (1 << TCG_TARGET_NB_REGS) - 1);
        /* r0 will be overwritten by the low word of base, so don't use it.  */
        tcg_regset_reset_reg(ct->u.regs, TCG_REG_R0);
        tcg_regset_reset_reg(ct->u.regs, TCG_REG_R1);
        break;
# endif
#endif

    case '1':
        ct->ct |= TCG_CT_REG;
        tcg_regset_set32(ct->u.regs, 0, (1 << TCG_TARGET_NB_REGS) - 1);
        tcg_regset_reset_reg(ct->u.regs, TCG_REG_R0);
        break;

    case '2':
        ct->ct |= TCG_CT_REG;
        tcg_regset_set32(ct->u.regs, 0, (1 << TCG_TARGET_NB_REGS) - 1);
        tcg_regset_reset_reg(ct->u.regs, TCG_REG_R0);
        tcg_regset_reset_reg(ct->u.regs, TCG_REG_R1);
        break;

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

    return 0;
}

182 183 184 185 186 187 188 189 190
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 已提交
191 192
    int shift;

193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209
    /* 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;
}
210 211 212

static inline int check_fit_imm(uint32_t imm)
{
213
    return encode_imm(imm) >= 0;
214 215
}

B
balrog 已提交
216 217 218 219 220 221 222 223 224 225 226 227 228 229 230
/* 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
 */
static inline int tcg_target_const_match(tcg_target_long val,
                const TCGArgConstraint *arg_ct)
{
    int ct;
    ct = arg_ct->ct;
    if (ct & TCG_CT_CONST)
        return 1;
231 232
    else if ((ct & TCG_CT_CONST_ARM) && check_fit_imm(val))
        return 1;
B
balrog 已提交
233 234 235 236 237 238 239 240 241 242 243 244 245
    else
        return 0;
}

enum arm_data_opc_e {
    ARITH_AND = 0x0,
    ARITH_EOR = 0x1,
    ARITH_SUB = 0x2,
    ARITH_RSB = 0x3,
    ARITH_ADD = 0x4,
    ARITH_ADC = 0x5,
    ARITH_SBC = 0x6,
    ARITH_RSC = 0x7,
P
pbrook 已提交
246
    ARITH_TST = 0x8,
B
balrog 已提交
247 248 249 250 251 252 253 254
    ARITH_CMP = 0xa,
    ARITH_CMN = 0xb,
    ARITH_ORR = 0xc,
    ARITH_MOV = 0xd,
    ARITH_BIC = 0xe,
    ARITH_MVN = 0xf,
};

P
pbrook 已提交
255 256
#define TO_CPSR(opc) \
  ((opc == ARITH_CMP || opc == ARITH_CMN || opc == ARITH_TST) << 20)
B
balrog 已提交
257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309

#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,
};

static const uint8_t tcg_cond_to_arm_cond[10] = {
    [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));
}

310 311
static inline void tcg_out_b_noaddr(TCGContext *s, int cond)
{
312
#ifdef HOST_WORDS_BIGENDIAN
313 314 315 316 317 318 319 320
    tcg_out8(s, (cond << 4) | 0x0a);
    s->code_ptr += 3;
#else
    s->code_ptr += 3;
    tcg_out8(s, (cond << 4) | 0x0a);
#endif
}

B
balrog 已提交
321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337
static inline void tcg_out_bl(TCGContext *s, int cond, int32_t offset)
{
    tcg_out32(s, (cond << 28) | 0x0b000000 |
                    (((offset - 8) >> 2) & 0x00ffffff));
}

static inline void tcg_out_dat_reg(TCGContext *s,
                int cond, int opc, int rd, int rn, int rm, int shift)
{
    tcg_out32(s, (cond << 28) | (0 << 25) | (opc << 21) | TO_CPSR(opc) |
                    (rn << 16) | (rd << 12) | shift | rm);
}

static inline void tcg_out_dat_reg2(TCGContext *s,
                int cond, int opc0, int opc1, int rd0, int rd1,
                int rn0, int rn1, int rm0, int rm1, int shift)
{
338 339 340 341 342 343 344 345 346 347 348 349 350
    if (rd0 == rn1 || rd0 == rm1) {
        tcg_out32(s, (cond << 28) | (0 << 25) | (opc0 << 21) | (1 << 20) |
                        (rn0 << 16) | (8 << 12) | shift | rm0);
        tcg_out32(s, (cond << 28) | (0 << 25) | (opc1 << 21) |
                        (rn1 << 16) | (rd1 << 12) | shift | rm1);
        tcg_out_dat_reg(s, cond, ARITH_MOV,
                        rd0, 0, TCG_REG_R8, SHIFT_IMM_LSL(0));
    } else {
        tcg_out32(s, (cond << 28) | (0 << 25) | (opc0 << 21) | (1 << 20) |
                        (rn0 << 16) | (rd0 << 12) | shift | rm0);
        tcg_out32(s, (cond << 28) | (0 << 25) | (opc1 << 21) |
                        (rn1 << 16) | (rd1 << 12) | shift | rm1);
    }
B
balrog 已提交
351 352 353 354 355
}

static inline void tcg_out_dat_imm(TCGContext *s,
                int cond, int opc, int rd, int rn, int im)
{
P
pbrook 已提交
356
    tcg_out32(s, (cond << 28) | (1 << 25) | (opc << 21) | TO_CPSR(opc) |
B
balrog 已提交
357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375
                    (rn << 16) | (rd << 12) | im);
}

static inline void tcg_out_movi32(TCGContext *s,
                int cond, int rd, int32_t arg)
{
    int offset = (uint32_t) arg - ((uint32_t) s->code_ptr + 8);

    /* TODO: This is very suboptimal, we can easily have a constant
     * pool somewhere after all the instructions.  */

    if (arg < 0 && arg > -0x100)
        return tcg_out_dat_imm(s, cond, ARITH_MVN, rd, 0, (~arg) & 0xff);

    if (offset < 0x100 && offset > -0x100)
        return offset >= 0 ?
                tcg_out_dat_imm(s, cond, ARITH_ADD, rd, 15, offset) :
                tcg_out_dat_imm(s, cond, ARITH_SUB, rd, 15, -offset);

376 377 378 379 380 381 382 383 384 385
#ifdef __ARM_ARCH_7A__
    /* use movw/movt */
    /* movw */
    tcg_out32(s, (cond << 28) | 0x03000000 | (rd << 12)
              | ((arg << 4) & 0x000f0000) | (arg & 0xfff));
    if (arg & 0xffff0000)
        /* movt */
        tcg_out32(s, (cond << 28) | 0x03400000 | (rd << 12)
                  | ((arg >> 12) & 0x000f0000) | ((arg >> 16) & 0xfff));
#else
B
balrog 已提交
386 387 388 389 390 391 392 393 394 395
    tcg_out_dat_imm(s, cond, ARITH_MOV, rd, 0, arg & 0xff);
    if (arg & 0x0000ff00)
        tcg_out_dat_imm(s, cond, ARITH_ORR, rd, rd,
                        ((arg >>  8) & 0xff) | 0xc00);
    if (arg & 0x00ff0000)
        tcg_out_dat_imm(s, cond, ARITH_ORR, rd, rd,
                        ((arg >> 16) & 0xff) | 0x800);
    if (arg & 0xff000000)
        tcg_out_dat_imm(s, cond, ARITH_ORR, rd, rd,
                        ((arg >> 24) & 0xff) | 0x400);
396
#endif
B
balrog 已提交
397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485
}

static inline void tcg_out_mul32(TCGContext *s,
                int cond, int rd, int rs, int rm)
{
    if (rd != rm)
        tcg_out32(s, (cond << 28) | (rd << 16) | (0 << 12) |
                        (rs << 8) | 0x90 | rm);
    else if (rd != rs)
        tcg_out32(s, (cond << 28) | (rd << 16) | (0 << 12) |
                        (rm << 8) | 0x90 | rs);
    else {
        tcg_out32(s, (cond << 28) | ( 8 << 16) | (0 << 12) |
                        (rs << 8) | 0x90 | rm);
        tcg_out_dat_reg(s, cond, ARITH_MOV,
                        rd, 0, 8, SHIFT_IMM_LSL(0));
    }
}

static inline void tcg_out_umull32(TCGContext *s,
                int cond, int rd0, int rd1, int rs, int rm)
{
    if (rd0 != rm && rd1 != rm)
        tcg_out32(s, (cond << 28) | 0x800090 |
                        (rd1 << 16) | (rd0 << 12) | (rs << 8) | rm);
    else if (rd0 != rs && rd1 != rs)
        tcg_out32(s, (cond << 28) | 0x800090 |
                        (rd1 << 16) | (rd0 << 12) | (rm << 8) | rs);
    else {
        tcg_out_dat_reg(s, cond, ARITH_MOV,
                        TCG_REG_R8, 0, rm, SHIFT_IMM_LSL(0));
        tcg_out32(s, (cond << 28) | 0x800098 |
                        (rd1 << 16) | (rd0 << 12) | (rs << 8));
    }
}

static inline void tcg_out_smull32(TCGContext *s,
                int cond, int rd0, int rd1, int rs, int rm)
{
    if (rd0 != rm && rd1 != rm)
        tcg_out32(s, (cond << 28) | 0xc00090 |
                        (rd1 << 16) | (rd0 << 12) | (rs << 8) | rm);
    else if (rd0 != rs && rd1 != rs)
        tcg_out32(s, (cond << 28) | 0xc00090 |
                        (rd1 << 16) | (rd0 << 12) | (rm << 8) | rs);
    else {
        tcg_out_dat_reg(s, cond, ARITH_MOV,
                        TCG_REG_R8, 0, rm, SHIFT_IMM_LSL(0));
        tcg_out32(s, (cond << 28) | 0xc00098 |
                        (rd1 << 16) | (rd0 << 12) | (rs << 8));
    }
}

static inline void tcg_out_ld32_12(TCGContext *s, int cond,
                int rd, int rn, tcg_target_long im)
{
    if (im >= 0)
        tcg_out32(s, (cond << 28) | 0x05900000 |
                        (rn << 16) | (rd << 12) | (im & 0xfff));
    else
        tcg_out32(s, (cond << 28) | 0x05100000 |
                        (rn << 16) | (rd << 12) | ((-im) & 0xfff));
}

static inline void tcg_out_st32_12(TCGContext *s, int cond,
                int rd, int rn, tcg_target_long im)
{
    if (im >= 0)
        tcg_out32(s, (cond << 28) | 0x05800000 |
                        (rn << 16) | (rd << 12) | (im & 0xfff));
    else
        tcg_out32(s, (cond << 28) | 0x05000000 |
                        (rn << 16) | (rd << 12) | ((-im) & 0xfff));
}

static inline void tcg_out_ld32_r(TCGContext *s, int cond,
                int rd, int rn, int rm)
{
    tcg_out32(s, (cond << 28) | 0x07900000 |
                    (rn << 16) | (rd << 12) | rm);
}

static inline void tcg_out_st32_r(TCGContext *s, int cond,
                int rd, int rn, int rm)
{
    tcg_out32(s, (cond << 28) | 0x07800000 |
                    (rn << 16) | (rd << 12) | rm);
}

P
pbrook 已提交
486 487 488 489 490 491 492 493 494 495 496 497 498 499 500
/* Register pre-increment with base writeback.  */
static inline void tcg_out_ld32_rwb(TCGContext *s, int cond,
                int rd, int rn, int rm)
{
    tcg_out32(s, (cond << 28) | 0x07b00000 |
                    (rn << 16) | (rd << 12) | rm);
}

static inline void tcg_out_st32_rwb(TCGContext *s, int cond,
                int rd, int rn, int rm)
{
    tcg_out32(s, (cond << 28) | 0x07a00000 |
                    (rn << 16) | (rd << 12) | rm);
}

B
balrog 已提交
501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645
static inline void tcg_out_ld16u_8(TCGContext *s, int cond,
                int rd, int rn, tcg_target_long im)
{
    if (im >= 0)
        tcg_out32(s, (cond << 28) | 0x01d000b0 |
                        (rn << 16) | (rd << 12) |
                        ((im & 0xf0) << 4) | (im & 0xf));
    else
        tcg_out32(s, (cond << 28) | 0x015000b0 |
                        (rn << 16) | (rd << 12) |
                        (((-im) & 0xf0) << 4) | ((-im) & 0xf));
}

static inline void tcg_out_st16u_8(TCGContext *s, int cond,
                int rd, int rn, tcg_target_long im)
{
    if (im >= 0)
        tcg_out32(s, (cond << 28) | 0x01c000b0 |
                        (rn << 16) | (rd << 12) |
                        ((im & 0xf0) << 4) | (im & 0xf));
    else
        tcg_out32(s, (cond << 28) | 0x014000b0 |
                        (rn << 16) | (rd << 12) |
                        (((-im) & 0xf0) << 4) | ((-im) & 0xf));
}

static inline void tcg_out_ld16u_r(TCGContext *s, int cond,
                int rd, int rn, int rm)
{
    tcg_out32(s, (cond << 28) | 0x019000b0 |
                    (rn << 16) | (rd << 12) | rm);
}

static inline void tcg_out_st16u_r(TCGContext *s, int cond,
                int rd, int rn, int rm)
{
    tcg_out32(s, (cond << 28) | 0x018000b0 |
                    (rn << 16) | (rd << 12) | rm);
}

static inline void tcg_out_ld16s_8(TCGContext *s, int cond,
                int rd, int rn, tcg_target_long im)
{
    if (im >= 0)
        tcg_out32(s, (cond << 28) | 0x01d000f0 |
                        (rn << 16) | (rd << 12) |
                        ((im & 0xf0) << 4) | (im & 0xf));
    else
        tcg_out32(s, (cond << 28) | 0x015000f0 |
                        (rn << 16) | (rd << 12) |
                        (((-im) & 0xf0) << 4) | ((-im) & 0xf));
}

static inline void tcg_out_st16s_8(TCGContext *s, int cond,
                int rd, int rn, tcg_target_long im)
{
    if (im >= 0)
        tcg_out32(s, (cond << 28) | 0x01c000f0 |
                        (rn << 16) | (rd << 12) |
                        ((im & 0xf0) << 4) | (im & 0xf));
    else
        tcg_out32(s, (cond << 28) | 0x014000f0 |
                        (rn << 16) | (rd << 12) |
                        (((-im) & 0xf0) << 4) | ((-im) & 0xf));
}

static inline void tcg_out_ld16s_r(TCGContext *s, int cond,
                int rd, int rn, int rm)
{
    tcg_out32(s, (cond << 28) | 0x019000f0 |
                    (rn << 16) | (rd << 12) | rm);
}

static inline void tcg_out_st16s_r(TCGContext *s, int cond,
                int rd, int rn, int rm)
{
    tcg_out32(s, (cond << 28) | 0x018000f0 |
                    (rn << 16) | (rd << 12) | rm);
}

static inline void tcg_out_ld8_12(TCGContext *s, int cond,
                int rd, int rn, tcg_target_long im)
{
    if (im >= 0)
        tcg_out32(s, (cond << 28) | 0x05d00000 |
                        (rn << 16) | (rd << 12) | (im & 0xfff));
    else
        tcg_out32(s, (cond << 28) | 0x05500000 |
                        (rn << 16) | (rd << 12) | ((-im) & 0xfff));
}

static inline void tcg_out_st8_12(TCGContext *s, int cond,
                int rd, int rn, tcg_target_long im)
{
    if (im >= 0)
        tcg_out32(s, (cond << 28) | 0x05c00000 |
                        (rn << 16) | (rd << 12) | (im & 0xfff));
    else
        tcg_out32(s, (cond << 28) | 0x05400000 |
                        (rn << 16) | (rd << 12) | ((-im) & 0xfff));
}

static inline void tcg_out_ld8_r(TCGContext *s, int cond,
                int rd, int rn, int rm)
{
    tcg_out32(s, (cond << 28) | 0x07d00000 |
                    (rn << 16) | (rd << 12) | rm);
}

static inline void tcg_out_st8_r(TCGContext *s, int cond,
                int rd, int rn, int rm)
{
    tcg_out32(s, (cond << 28) | 0x07c00000 |
                    (rn << 16) | (rd << 12) | rm);
}

static inline void tcg_out_ld8s_8(TCGContext *s, int cond,
                int rd, int rn, tcg_target_long im)
{
    if (im >= 0)
        tcg_out32(s, (cond << 28) | 0x01d000d0 |
                        (rn << 16) | (rd << 12) |
                        ((im & 0xf0) << 4) | (im & 0xf));
    else
        tcg_out32(s, (cond << 28) | 0x015000d0 |
                        (rn << 16) | (rd << 12) |
                        (((-im) & 0xf0) << 4) | ((-im) & 0xf));
}

static inline void tcg_out_st8s_8(TCGContext *s, int cond,
                int rd, int rn, tcg_target_long im)
{
    if (im >= 0)
        tcg_out32(s, (cond << 28) | 0x01c000d0 |
                        (rn << 16) | (rd << 12) |
                        ((im & 0xf0) << 4) | (im & 0xf));
    else
        tcg_out32(s, (cond << 28) | 0x014000d0 |
                        (rn << 16) | (rd << 12) |
                        (((-im) & 0xf0) << 4) | ((-im) & 0xf));
}

static inline void tcg_out_ld8s_r(TCGContext *s, int cond,
                int rd, int rn, int rm)
{
B
balrog 已提交
646
    tcg_out32(s, (cond << 28) | 0x019000d0 |
B
balrog 已提交
647 648 649 650 651 652
                    (rn << 16) | (rd << 12) | rm);
}

static inline void tcg_out_st8s_r(TCGContext *s, int cond,
                int rd, int rn, int rm)
{
B
balrog 已提交
653
    tcg_out32(s, (cond << 28) | 0x018000d0 |
B
balrog 已提交
654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 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 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817
                    (rn << 16) | (rd << 12) | rm);
}

static inline void tcg_out_ld32u(TCGContext *s, int cond,
                int rd, int rn, int32_t offset)
{
    if (offset > 0xfff || offset < -0xfff) {
        tcg_out_movi32(s, cond, TCG_REG_R8, offset);
        tcg_out_ld32_r(s, cond, rd, rn, TCG_REG_R8);
    } 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) {
        tcg_out_movi32(s, cond, TCG_REG_R8, offset);
        tcg_out_st32_r(s, cond, rd, rn, TCG_REG_R8);
    } 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) {
        tcg_out_movi32(s, cond, TCG_REG_R8, offset);
        tcg_out_ld16u_r(s, cond, rd, rn, TCG_REG_R8);
    } 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) {
        tcg_out_movi32(s, cond, TCG_REG_R8, offset);
        tcg_out_ld16s_r(s, cond, rd, rn, TCG_REG_R8);
    } else
        tcg_out_ld16s_8(s, cond, rd, rn, offset);
}

static inline void tcg_out_st16u(TCGContext *s, int cond,
                int rd, int rn, int32_t offset)
{
    if (offset > 0xff || offset < -0xff) {
        tcg_out_movi32(s, cond, TCG_REG_R8, offset);
        tcg_out_st16u_r(s, cond, rd, rn, TCG_REG_R8);
    } else
        tcg_out_st16u_8(s, cond, rd, rn, offset);
}

static inline void tcg_out_ld8u(TCGContext *s, int cond,
                int rd, int rn, int32_t offset)
{
    if (offset > 0xfff || offset < -0xfff) {
        tcg_out_movi32(s, cond, TCG_REG_R8, offset);
        tcg_out_ld8_r(s, cond, rd, rn, TCG_REG_R8);
    } 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) {
        tcg_out_movi32(s, cond, TCG_REG_R8, offset);
        tcg_out_ld8s_r(s, cond, rd, rn, TCG_REG_R8);
    } else
        tcg_out_ld8s_8(s, cond, rd, rn, offset);
}

static inline void tcg_out_st8u(TCGContext *s, int cond,
                int rd, int rn, int32_t offset)
{
    if (offset > 0xfff || offset < -0xfff) {
        tcg_out_movi32(s, cond, TCG_REG_R8, offset);
        tcg_out_st8_r(s, cond, rd, rn, TCG_REG_R8);
    } else
        tcg_out_st8_12(s, cond, rd, rn, offset);
}

static inline void tcg_out_goto(TCGContext *s, int cond, uint32_t addr)
{
    int32_t val;

    val = addr - (tcg_target_long) s->code_ptr;
    if (val - 8 < 0x01fffffd && val - 8 > -0x01fffffd)
        tcg_out_b(s, cond, val);
    else {
#if 1
        tcg_abort();
#else
        if (cond == COND_AL) {
            tcg_out_ld32_12(s, COND_AL, 15, 15, -4);
            tcg_out32(s, addr); /* XXX: This is l->u.value, can we use it? */
        } else {
            tcg_out_movi32(s, cond, TCG_REG_R8, val - 8);
            tcg_out_dat_reg(s, cond, ARITH_ADD,
                            15, 15, TCG_REG_R8, SHIFT_IMM_LSL(0));
        }
#endif
    }
}

static inline void tcg_out_call(TCGContext *s, int cond, uint32_t addr)
{
    int32_t val;

#ifdef SAVE_LR
    tcg_out_dat_reg(s, cond, ARITH_MOV, TCG_REG_R8, 0, 14, SHIFT_IMM_LSL(0));
#endif

    val = addr - (tcg_target_long) s->code_ptr;
    if (val < 0x01fffffd && val > -0x01fffffd)
        tcg_out_bl(s, cond, val);
    else {
#if 1
        tcg_abort();
#else
        if (cond == COND_AL) {
            tcg_out_dat_imm(s, cond, ARITH_ADD, 14, 15, 4);
            tcg_out_ld32_12(s, COND_AL, 15, 15, -4);
            tcg_out32(s, addr); /* XXX: This is l->u.value, can we use it? */
        } else {
            tcg_out_movi32(s, cond, TCG_REG_R9, addr);
            tcg_out_dat_imm(s, cond, ARITH_MOV, 14, 0, 15);
            tcg_out_bx(s, cond, TCG_REG_R9);
        }
#endif
    }

#ifdef SAVE_LR
    tcg_out_dat_reg(s, cond, ARITH_MOV, 14, 0, TCG_REG_R8, SHIFT_IMM_LSL(0));
#endif
}

static inline void tcg_out_callr(TCGContext *s, int cond, int arg)
{
#ifdef SAVE_LR
    tcg_out_dat_reg(s, cond, ARITH_MOV, TCG_REG_R8, 0, 14, SHIFT_IMM_LSL(0));
#endif
    /* TODO: on ARMv5 and ARMv6 replace with tcg_out_blx(s, cond, arg);  */
    tcg_out_dat_reg(s, cond, ARITH_MOV, 14, 0, 15, SHIFT_IMM_LSL(0));
    tcg_out_bx(s, cond, arg);
#ifdef SAVE_LR
    tcg_out_dat_reg(s, cond, ARITH_MOV, 14, 0, TCG_REG_R8, SHIFT_IMM_LSL(0));
#endif
}

static inline void tcg_out_goto_label(TCGContext *s, int cond, int label_index)
{
    TCGLabel *l = &s->labels[label_index];

    if (l->has_value)
        tcg_out_goto(s, cond, l->u.value);
    else if (cond == COND_AL) {
        tcg_out_ld32_12(s, COND_AL, 15, 15, -4);
        tcg_out_reloc(s, s->code_ptr, R_ARM_ABS32, label_index, 31337);
        s->code_ptr += 4;
    } else {
        /* Probably this should be preferred even for COND_AL... */
        tcg_out_reloc(s, s->code_ptr, R_ARM_PC24, label_index, 31337);
818
        tcg_out_b_noaddr(s, cond);
B
balrog 已提交
819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873
    }
}

static void tcg_out_div_helper(TCGContext *s, int cond, const TCGArg *args,
                void *helper_div, void *helper_rem, int shift)
{
    int div_reg = args[0];
    int rem_reg = args[1];

    /* stmdb sp!, { r0 - r3, ip, lr } */
    /* (Note that we need an even number of registers as per EABI) */
    tcg_out32(s, (cond << 28) | 0x092d500f);

    tcg_out_dat_reg(s, cond, ARITH_MOV, 0, 0, args[2], SHIFT_IMM_LSL(0));
    tcg_out_dat_reg(s, cond, ARITH_MOV, 1, 0, args[3], SHIFT_IMM_LSL(0));
    tcg_out_dat_reg(s, cond, ARITH_MOV, 2, 0, args[4], SHIFT_IMM_LSL(0));
    tcg_out_dat_reg(s, cond, ARITH_MOV, 3, 0, 2, shift);

    tcg_out_call(s, cond, (uint32_t) helper_div);
    tcg_out_dat_reg(s, cond, ARITH_MOV, 8, 0, 0, SHIFT_IMM_LSL(0));

    /* ldmia sp, { r0 - r3, fp, lr } */
    tcg_out32(s, (cond << 28) | 0x089d500f);

    tcg_out_dat_reg(s, cond, ARITH_MOV, 0, 0, args[2], SHIFT_IMM_LSL(0));
    tcg_out_dat_reg(s, cond, ARITH_MOV, 1, 0, args[3], SHIFT_IMM_LSL(0));
    tcg_out_dat_reg(s, cond, ARITH_MOV, 2, 0, args[4], SHIFT_IMM_LSL(0));
    tcg_out_dat_reg(s, cond, ARITH_MOV, 3, 0, 2, shift);

    tcg_out_call(s, cond, (uint32_t) helper_rem);

    tcg_out_dat_reg(s, cond, ARITH_MOV, rem_reg, 0, 0, SHIFT_IMM_LSL(0));
    tcg_out_dat_reg(s, cond, ARITH_MOV, div_reg, 0, 8, SHIFT_IMM_LSL(0));

    /* ldr r0, [sp], #4 */
    if (rem_reg != 0 && div_reg != 0)
        tcg_out32(s, (cond << 28) | 0x04bd0004);
    /* ldr r1, [sp], #4 */
    if (rem_reg != 1 && div_reg != 1)
        tcg_out32(s, (cond << 28) | 0x04bd1004);
    /* ldr r2, [sp], #4 */
    if (rem_reg != 2 && div_reg != 2)
        tcg_out32(s, (cond << 28) | 0x04bd2004);
    /* ldr r3, [sp], #4 */
    if (rem_reg != 3 && div_reg != 3)
        tcg_out32(s, (cond << 28) | 0x04bd3004);
    /* ldr ip, [sp], #4 */
    if (rem_reg != 12 && div_reg != 12)
        tcg_out32(s, (cond << 28) | 0x04bdc004);
    /* ldr lr, [sp], #4 */
    if (rem_reg != 14 && div_reg != 14)
        tcg_out32(s, (cond << 28) | 0x04bde004);
}

#ifdef CONFIG_SOFTMMU
874 875

#include "../../softmmu_defs.h"
B
balrog 已提交
876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891

static void *qemu_ld_helpers[4] = {
    __ldb_mmu,
    __ldw_mmu,
    __ldl_mmu,
    __ldq_mmu,
};

static void *qemu_st_helpers[4] = {
    __stb_mmu,
    __stw_mmu,
    __stl_mmu,
    __stq_mmu,
};
#endif

P
pbrook 已提交
892 893
#define TLB_SHIFT	(CPU_TLB_ENTRY_BITS + CPU_TLB_BITS)

B
balrog 已提交
894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909
static inline void tcg_out_qemu_ld(TCGContext *s, int cond,
                const TCGArg *args, int opc)
{
    int addr_reg, data_reg, data_reg2;
#ifdef CONFIG_SOFTMMU
    int mem_index, s_bits;
# if TARGET_LONG_BITS == 64
    int addr_reg2;
# endif
    uint32_t *label_ptr;
#endif

    data_reg = *args++;
    if (opc == 3)
        data_reg2 = *args++;
    else
910
        data_reg2 = 0; /* suppress warning */
B
balrog 已提交
911 912
    addr_reg = *args++;
#ifdef CONFIG_SOFTMMU
913 914 915
# if TARGET_LONG_BITS == 64
    addr_reg2 = *args++;
# endif
B
balrog 已提交
916 917 918
    mem_index = *args;
    s_bits = opc & 3;

919
    /* Should generate something like the following:
P
pbrook 已提交
920
     *  shr r8, addr_reg, #TARGET_PAGE_BITS
921
     *  and r0, r8, #(CPU_TLB_SIZE - 1)   @ Assumption: CPU_TLB_BITS <= 8
P
pbrook 已提交
922
     *  add r0, env, r0 lsl #CPU_TLB_ENTRY_BITS
923 924 925 926
     */
#  if CPU_TLB_BITS > 8
#   error
#  endif
B
balrog 已提交
927
    tcg_out_dat_reg(s, COND_AL, ARITH_MOV,
P
pbrook 已提交
928
                    8, 0, addr_reg, SHIFT_IMM_LSR(TARGET_PAGE_BITS));
B
balrog 已提交
929 930 931 932
    tcg_out_dat_imm(s, COND_AL, ARITH_AND,
                    0, 8, CPU_TLB_SIZE - 1);
    tcg_out_dat_reg(s, COND_AL, ARITH_ADD,
                    0, TCG_AREG0, 0, SHIFT_IMM_LSL(CPU_TLB_ENTRY_BITS));
933 934 935 936 937 938 939
    /* In the
     *  ldr r1 [r0, #(offsetof(CPUState, tlb_table[mem_index][0].addr_read))]
     * below, the offset is likely to exceed 12 bits if mem_index != 0 and
     * not exceed otherwise, so use an
     *  add r0, r0, #(mem_index * sizeof *CPUState.tlb_table)
     * before.
     */
940 941 942 943
    if (mem_index)
        tcg_out_dat_imm(s, COND_AL, ARITH_ADD, 0, 0,
                        (mem_index << (TLB_SHIFT & 1)) |
                        ((16 - (TLB_SHIFT >> 1)) << 8));
B
balrog 已提交
944
    tcg_out_ld32_12(s, COND_AL, 1, 0,
945
                    offsetof(CPUState, tlb_table[0][0].addr_read));
B
balrog 已提交
946 947
    tcg_out_dat_reg(s, COND_AL, ARITH_CMP,
                    0, 1, 8, SHIFT_IMM_LSL(TARGET_PAGE_BITS));
P
pbrook 已提交
948 949 950 951
    /* Check alignment.  */
    if (s_bits)
        tcg_out_dat_imm(s, COND_EQ, ARITH_TST,
                        0, addr_reg, (1 << s_bits) - 1);
B
balrog 已提交
952 953 954 955
#  if TARGET_LONG_BITS == 64
    /* XXX: possibly we could use a block data load or writeback in
     * the first access.  */
    tcg_out_ld32_12(s, COND_EQ, 1, 0,
956
                    offsetof(CPUState, tlb_table[0][0].addr_read) + 4);
B
balrog 已提交
957 958 959 960
    tcg_out_dat_reg(s, COND_EQ, ARITH_CMP,
                    0, 1, addr_reg2, SHIFT_IMM_LSL(0));
#  endif
    tcg_out_ld32_12(s, COND_EQ, 1, 0,
961
                    offsetof(CPUState, tlb_table[0][0].addend));
B
balrog 已提交
962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980

    switch (opc) {
    case 0:
        tcg_out_ld8_r(s, COND_EQ, data_reg, addr_reg, 1);
        break;
    case 0 | 4:
        tcg_out_ld8s_r(s, COND_EQ, data_reg, addr_reg, 1);
        break;
    case 1:
        tcg_out_ld16u_r(s, COND_EQ, data_reg, addr_reg, 1);
        break;
    case 1 | 4:
        tcg_out_ld16s_r(s, COND_EQ, data_reg, addr_reg, 1);
        break;
    case 2:
    default:
        tcg_out_ld32_r(s, COND_EQ, data_reg, addr_reg, 1);
        break;
    case 3:
P
pbrook 已提交
981
        tcg_out_ld32_rwb(s, COND_EQ, data_reg, 1, addr_reg);
B
balrog 已提交
982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004
        tcg_out_ld32_12(s, COND_EQ, data_reg2, 1, 4);
        break;
    }

    label_ptr = (void *) s->code_ptr;
    tcg_out_b(s, COND_EQ, 8);

# ifdef SAVE_LR
    tcg_out_dat_reg(s, cond, ARITH_MOV, 8, 0, 14, SHIFT_IMM_LSL(0));
# endif

    /* TODO: move this code to where the constants pool will be */
    if (addr_reg)
        tcg_out_dat_reg(s, cond, ARITH_MOV,
                        0, 0, addr_reg, SHIFT_IMM_LSL(0));
# if TARGET_LONG_BITS == 32
    tcg_out_dat_imm(s, cond, ARITH_MOV, 1, 0, mem_index);
# else
    if (addr_reg2 != 1)
        tcg_out_dat_reg(s, cond, ARITH_MOV,
                        1, 0, addr_reg2, SHIFT_IMM_LSL(0));
    tcg_out_dat_imm(s, cond, ARITH_MOV, 2, 0, mem_index);
# endif
B
balrog 已提交
1005
    tcg_out_bl(s, cond, (tcg_target_long) qemu_ld_helpers[s_bits] -
B
balrog 已提交
1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029
                    (tcg_target_long) s->code_ptr);

    switch (opc) {
    case 0 | 4:
        tcg_out_dat_reg(s, cond, ARITH_MOV,
                        0, 0, 0, SHIFT_IMM_LSL(24));
        tcg_out_dat_reg(s, cond, ARITH_MOV,
                        data_reg, 0, 0, SHIFT_IMM_ASR(24));
        break;
    case 1 | 4:
        tcg_out_dat_reg(s, cond, ARITH_MOV,
                        0, 0, 0, SHIFT_IMM_LSL(16));
        tcg_out_dat_reg(s, cond, ARITH_MOV,
                        data_reg, 0, 0, SHIFT_IMM_ASR(16));
        break;
    case 0:
    case 1:
    case 2:
    default:
        if (data_reg)
            tcg_out_dat_reg(s, cond, ARITH_MOV,
                            data_reg, 0, 0, SHIFT_IMM_LSL(0));
        break;
    case 3:
1030 1031 1032
        if (data_reg != 0)
            tcg_out_dat_reg(s, cond, ARITH_MOV,
                            data_reg, 0, 0, SHIFT_IMM_LSL(0));
B
balrog 已提交
1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043
        if (data_reg2 != 1)
            tcg_out_dat_reg(s, cond, ARITH_MOV,
                            data_reg2, 0, 1, SHIFT_IMM_LSL(0));
        break;
    }

# ifdef SAVE_LR
    tcg_out_dat_reg(s, cond, ARITH_MOV, 14, 0, 8, SHIFT_IMM_LSL(0));
# endif

    *label_ptr += ((void *) s->code_ptr - (void *) label_ptr - 8) >> 2;
P
Paul Brook 已提交
1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059
#else /* !CONFIG_SOFTMMU */
    if (GUEST_BASE) {
        uint32_t offset = GUEST_BASE;
        int i;
        int rot;

        while (offset) {
            i = ctz32(offset) & ~1;
            rot = ((32 - i) << 7) & 0xf00;

            tcg_out_dat_imm(s, COND_AL, ARITH_ADD, 8, addr_reg,
                            ((offset >> i) & 0xff) | rot);
            addr_reg = 8;
            offset &= ~(0xff << i);
        }
    }
B
balrog 已提交
1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077
    switch (opc) {
    case 0:
        tcg_out_ld8_12(s, COND_AL, data_reg, addr_reg, 0);
        break;
    case 0 | 4:
        tcg_out_ld8s_8(s, COND_AL, data_reg, addr_reg, 0);
        break;
    case 1:
        tcg_out_ld16u_8(s, COND_AL, data_reg, addr_reg, 0);
        break;
    case 1 | 4:
        tcg_out_ld16s_8(s, COND_AL, data_reg, addr_reg, 0);
        break;
    case 2:
    default:
        tcg_out_ld32_12(s, COND_AL, data_reg, addr_reg, 0);
        break;
    case 3:
B
balrog 已提交
1078 1079
        /* TODO: use block load -
         * check that data_reg2 > data_reg or the other way */
A
aurel32 已提交
1080 1081 1082 1083 1084 1085 1086
        if (data_reg == addr_reg) {
            tcg_out_ld32_12(s, COND_AL, data_reg2, addr_reg, 4);
            tcg_out_ld32_12(s, COND_AL, data_reg, addr_reg, 0);
        } else {
            tcg_out_ld32_12(s, COND_AL, data_reg, addr_reg, 0);
            tcg_out_ld32_12(s, COND_AL, data_reg2, addr_reg, 4);
        }
B
balrog 已提交
1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107
        break;
    }
#endif
}

static inline void tcg_out_qemu_st(TCGContext *s, int cond,
                const TCGArg *args, int opc)
{
    int addr_reg, data_reg, data_reg2;
#ifdef CONFIG_SOFTMMU
    int mem_index, s_bits;
# if TARGET_LONG_BITS == 64
    int addr_reg2;
# endif
    uint32_t *label_ptr;
#endif

    data_reg = *args++;
    if (opc == 3)
        data_reg2 = *args++;
    else
1108
        data_reg2 = 0; /* suppress warning */
B
balrog 已提交
1109 1110
    addr_reg = *args++;
#ifdef CONFIG_SOFTMMU
1111 1112 1113
# if TARGET_LONG_BITS == 64
    addr_reg2 = *args++;
# endif
B
balrog 已提交
1114 1115 1116
    mem_index = *args;
    s_bits = opc & 3;

1117
    /* Should generate something like the following:
P
pbrook 已提交
1118
     *  shr r8, addr_reg, #TARGET_PAGE_BITS
1119
     *  and r0, r8, #(CPU_TLB_SIZE - 1)   @ Assumption: CPU_TLB_BITS <= 8
P
pbrook 已提交
1120
     *  add r0, env, r0 lsl #CPU_TLB_ENTRY_BITS
1121
     */
B
balrog 已提交
1122
    tcg_out_dat_reg(s, COND_AL, ARITH_MOV,
P
pbrook 已提交
1123
                    8, 0, addr_reg, SHIFT_IMM_LSR(TARGET_PAGE_BITS));
B
balrog 已提交
1124 1125 1126 1127
    tcg_out_dat_imm(s, COND_AL, ARITH_AND,
                    0, 8, CPU_TLB_SIZE - 1);
    tcg_out_dat_reg(s, COND_AL, ARITH_ADD,
                    0, TCG_AREG0, 0, SHIFT_IMM_LSL(CPU_TLB_ENTRY_BITS));
1128 1129 1130 1131 1132 1133 1134
    /* In the
     *  ldr r1 [r0, #(offsetof(CPUState, tlb_table[mem_index][0].addr_write))]
     * below, the offset is likely to exceed 12 bits if mem_index != 0 and
     * not exceed otherwise, so use an
     *  add r0, r0, #(mem_index * sizeof *CPUState.tlb_table)
     * before.
     */
1135 1136 1137 1138
    if (mem_index)
        tcg_out_dat_imm(s, COND_AL, ARITH_ADD, 0, 0,
                        (mem_index << (TLB_SHIFT & 1)) |
                        ((16 - (TLB_SHIFT >> 1)) << 8));
B
balrog 已提交
1139
    tcg_out_ld32_12(s, COND_AL, 1, 0,
1140
                    offsetof(CPUState, tlb_table[0][0].addr_write));
B
balrog 已提交
1141 1142
    tcg_out_dat_reg(s, COND_AL, ARITH_CMP,
                    0, 1, 8, SHIFT_IMM_LSL(TARGET_PAGE_BITS));
P
pbrook 已提交
1143 1144 1145 1146
    /* Check alignment.  */
    if (s_bits)
        tcg_out_dat_imm(s, COND_EQ, ARITH_TST,
                        0, addr_reg, (1 << s_bits) - 1);
B
balrog 已提交
1147 1148 1149 1150
#  if TARGET_LONG_BITS == 64
    /* XXX: possibly we could use a block data load or writeback in
     * the first access.  */
    tcg_out_ld32_12(s, COND_EQ, 1, 0,
1151
                    offsetof(CPUState, tlb_table[0][0].addr_write)
B
balrog 已提交
1152 1153 1154 1155 1156
                    + 4);
    tcg_out_dat_reg(s, COND_EQ, ARITH_CMP,
                    0, 1, addr_reg2, SHIFT_IMM_LSL(0));
#  endif
    tcg_out_ld32_12(s, COND_EQ, 1, 0,
1157
                    offsetof(CPUState, tlb_table[0][0].addend));
B
balrog 已提交
1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176

    switch (opc) {
    case 0:
        tcg_out_st8_r(s, COND_EQ, data_reg, addr_reg, 1);
        break;
    case 0 | 4:
        tcg_out_st8s_r(s, COND_EQ, data_reg, addr_reg, 1);
        break;
    case 1:
        tcg_out_st16u_r(s, COND_EQ, data_reg, addr_reg, 1);
        break;
    case 1 | 4:
        tcg_out_st16s_r(s, COND_EQ, data_reg, addr_reg, 1);
        break;
    case 2:
    default:
        tcg_out_st32_r(s, COND_EQ, data_reg, addr_reg, 1);
        break;
    case 3:
P
pbrook 已提交
1177
        tcg_out_st32_rwb(s, COND_EQ, data_reg, 1, addr_reg);
B
balrog 已提交
1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240
        tcg_out_st32_12(s, COND_EQ, data_reg2, 1, 4);
        break;
    }

    label_ptr = (void *) s->code_ptr;
    tcg_out_b(s, COND_EQ, 8);

    /* TODO: move this code to where the constants pool will be */
    if (addr_reg)
        tcg_out_dat_reg(s, cond, ARITH_MOV,
                        0, 0, addr_reg, SHIFT_IMM_LSL(0));
# if TARGET_LONG_BITS == 32
    switch (opc) {
    case 0:
        tcg_out_dat_imm(s, cond, ARITH_AND, 1, data_reg, 0xff);
        tcg_out_dat_imm(s, cond, ARITH_MOV, 2, 0, mem_index);
        break;
    case 1:
        tcg_out_dat_reg(s, cond, ARITH_MOV,
                        1, 0, data_reg, SHIFT_IMM_LSL(16));
        tcg_out_dat_reg(s, cond, ARITH_MOV,
                        1, 0, 1, SHIFT_IMM_LSR(16));
        tcg_out_dat_imm(s, cond, ARITH_MOV, 2, 0, mem_index);
        break;
    case 2:
        if (data_reg != 1)
            tcg_out_dat_reg(s, cond, ARITH_MOV,
                            1, 0, data_reg, SHIFT_IMM_LSL(0));
        tcg_out_dat_imm(s, cond, ARITH_MOV, 2, 0, mem_index);
        break;
    case 3:
        if (data_reg != 1)
            tcg_out_dat_reg(s, cond, ARITH_MOV,
                            1, 0, data_reg, SHIFT_IMM_LSL(0));
        if (data_reg2 != 2)
            tcg_out_dat_reg(s, cond, ARITH_MOV,
                            2, 0, data_reg2, SHIFT_IMM_LSL(0));
        tcg_out_dat_imm(s, cond, ARITH_MOV, 3, 0, mem_index);
        break;
    }
# else
    if (addr_reg2 != 1)
        tcg_out_dat_reg(s, cond, ARITH_MOV,
                        1, 0, addr_reg2, SHIFT_IMM_LSL(0));
    switch (opc) {
    case 0:
        tcg_out_dat_imm(s, cond, ARITH_AND, 2, data_reg, 0xff);
        tcg_out_dat_imm(s, cond, ARITH_MOV, 3, 0, mem_index);
        break;
    case 1:
        tcg_out_dat_reg(s, cond, ARITH_MOV,
                        2, 0, data_reg, SHIFT_IMM_LSL(16));
        tcg_out_dat_reg(s, cond, ARITH_MOV,
                        2, 0, 2, SHIFT_IMM_LSR(16));
        tcg_out_dat_imm(s, cond, ARITH_MOV, 3, 0, mem_index);
        break;
    case 2:
        if (data_reg != 2)
            tcg_out_dat_reg(s, cond, ARITH_MOV,
                            2, 0, data_reg, SHIFT_IMM_LSL(0));
        tcg_out_dat_imm(s, cond, ARITH_MOV, 3, 0, mem_index);
        break;
    case 3:
1241 1242
        tcg_out_dat_imm(s, cond, ARITH_MOV, 8, 0, mem_index);
        tcg_out32(s, (cond << 28) | 0x052d8010); /* str r8, [sp, #-0x10]! */
B
balrog 已提交
1243 1244 1245 1246 1247 1248 1249 1250 1251 1252
        if (data_reg != 2)
            tcg_out_dat_reg(s, cond, ARITH_MOV,
                            2, 0, data_reg, SHIFT_IMM_LSL(0));
        if (data_reg2 != 3)
            tcg_out_dat_reg(s, cond, ARITH_MOV,
                            3, 0, data_reg2, SHIFT_IMM_LSL(0));
        break;
    }
# endif

1253 1254 1255 1256
# ifdef SAVE_LR
    tcg_out_dat_reg(s, cond, ARITH_MOV, 8, 0, 14, SHIFT_IMM_LSL(0));
# endif

B
balrog 已提交
1257
    tcg_out_bl(s, cond, (tcg_target_long) qemu_st_helpers[s_bits] -
B
balrog 已提交
1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268
                    (tcg_target_long) s->code_ptr);
# if TARGET_LONG_BITS == 64
    if (opc == 3)
        tcg_out_dat_imm(s, cond, ARITH_ADD, 13, 13, 0x10);
# endif

# ifdef SAVE_LR
    tcg_out_dat_reg(s, cond, ARITH_MOV, 14, 0, 8, SHIFT_IMM_LSL(0));
# endif

    *label_ptr += ((void *) s->code_ptr - (void *) label_ptr - 8) >> 2;
P
Paul Brook 已提交
1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284
#else /* !CONFIG_SOFTMMU */
    if (GUEST_BASE) {
        uint32_t offset = GUEST_BASE;
        int i;
        int rot;

        while (offset) {
            i = ctz32(offset) & ~1;
            rot = ((32 - i) << 7) & 0xf00;

            tcg_out_dat_imm(s, COND_AL, ARITH_ADD, 8, addr_reg,
                            ((offset >> i) & 0xff) | rot);
            addr_reg = 8;
            offset &= ~(0xff << i);
        }
    }
B
balrog 已提交
1285 1286 1287 1288 1289
    switch (opc) {
    case 0:
        tcg_out_st8_12(s, COND_AL, data_reg, addr_reg, 0);
        break;
    case 0 | 4:
B
balrog 已提交
1290
        tcg_out_st8s_8(s, COND_AL, data_reg, addr_reg, 0);
B
balrog 已提交
1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302
        break;
    case 1:
        tcg_out_st16u_8(s, COND_AL, data_reg, addr_reg, 0);
        break;
    case 1 | 4:
        tcg_out_st16s_8(s, COND_AL, data_reg, addr_reg, 0);
        break;
    case 2:
    default:
        tcg_out_st32_12(s, COND_AL, data_reg, addr_reg, 0);
        break;
    case 3:
B
balrog 已提交
1303 1304
        /* TODO: use block store -
         * check that data_reg2 > data_reg or the other way */
B
balrog 已提交
1305 1306 1307 1308 1309 1310 1311 1312 1313
        tcg_out_st32_12(s, COND_AL, data_reg, addr_reg, 0);
        tcg_out_st32_12(s, COND_AL, data_reg2, addr_reg, 4);
        break;
    }
#endif
}

static uint8_t *tb_ret_addr;

B
balrog 已提交
1314
static inline void tcg_out_op(TCGContext *s, int opc,
B
balrog 已提交
1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329
                const TCGArg *args, const int *const_args)
{
    int c;

    switch (opc) {
    case INDEX_op_exit_tb:
#ifdef SAVE_LR
        if (args[0] >> 8)
            tcg_out_ld32_12(s, COND_AL, TCG_REG_R0, 15, 0);
        else
            tcg_out_dat_imm(s, COND_AL, ARITH_MOV, TCG_REG_R0, 0, args[0]);
        tcg_out_dat_reg(s, COND_AL, ARITH_MOV, 15, 0, 14, SHIFT_IMM_LSL(0));
        if (args[0] >> 8)
            tcg_out32(s, args[0]);
#else
1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341
        {
            uint8_t *ld_ptr = s->code_ptr;
            if (args[0] >> 8)
                tcg_out_ld32_12(s, COND_AL, 0, 15, 0);
            else
                tcg_out_dat_imm(s, COND_AL, ARITH_MOV, 0, 0, args[0]);
            tcg_out_goto(s, COND_AL, (tcg_target_ulong) tb_ret_addr);
            if (args[0] >> 8) {
                *ld_ptr = (uint8_t) (s->code_ptr - ld_ptr) - 8;
                tcg_out32(s, args[0]);
            }
        }
B
balrog 已提交
1342 1343 1344 1345 1346
#endif
        break;
    case INDEX_op_goto_tb:
        if (s->tb_jmp_offset) {
            /* Direct jump method */
1347
#if defined(USE_DIRECT_JUMP)
B
balrog 已提交
1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436
            s->tb_jmp_offset[args[0]] = s->code_ptr - s->code_buf;
            tcg_out_b(s, COND_AL, 8);
#else
            tcg_out_ld32_12(s, COND_AL, 15, 15, -4);
            s->tb_jmp_offset[args[0]] = s->code_ptr - s->code_buf;
            tcg_out32(s, 0);
#endif
        } else {
            /* Indirect jump method */
#if 1
            c = (int) (s->tb_next + args[0]) - ((int) s->code_ptr + 8);
            if (c > 0xfff || c < -0xfff) {
                tcg_out_movi32(s, COND_AL, TCG_REG_R0,
                                (tcg_target_long) (s->tb_next + args[0]));
                tcg_out_ld32_12(s, COND_AL, 15, TCG_REG_R0, 0);
            } else
                tcg_out_ld32_12(s, COND_AL, 15, 15, c);
#else
            tcg_out_ld32_12(s, COND_AL, TCG_REG_R0, 15, 0);
            tcg_out_ld32_12(s, COND_AL, 15, TCG_REG_R0, 0);
            tcg_out32(s, (tcg_target_long) (s->tb_next + args[0]));
#endif
        }
        s->tb_next_offset[args[0]] = s->code_ptr - s->code_buf;
        break;
    case INDEX_op_call:
        if (const_args[0])
            tcg_out_call(s, COND_AL, args[0]);
        else
            tcg_out_callr(s, COND_AL, args[0]);
        break;
    case INDEX_op_jmp:
        if (const_args[0])
            tcg_out_goto(s, COND_AL, args[0]);
        else
            tcg_out_bx(s, COND_AL, args[0]);
        break;
    case INDEX_op_br:
        tcg_out_goto_label(s, COND_AL, args[0]);
        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:
        tcg_out_st8u(s, COND_AL, args[0], args[1], args[2]);
        break;
    case INDEX_op_st16_i32:
        tcg_out_st16u(s, COND_AL, args[0], args[1], args[2]);
        break;
    case INDEX_op_st_i32:
        tcg_out_st32(s, COND_AL, args[0], args[1], args[2]);
        break;

    case INDEX_op_mov_i32:
        tcg_out_dat_reg(s, COND_AL, ARITH_MOV,
                        args[0], 0, args[1], SHIFT_IMM_LSL(0));
        break;
    case INDEX_op_movi_i32:
        tcg_out_movi32(s, COND_AL, args[0], args[1]);
        break;
    case INDEX_op_add_i32:
        c = ARITH_ADD;
        goto gen_arith;
    case INDEX_op_sub_i32:
        c = ARITH_SUB;
        goto gen_arith;
    case INDEX_op_and_i32:
        c = ARITH_AND;
        goto gen_arith;
    case INDEX_op_or_i32:
        c = ARITH_ORR;
        goto gen_arith;
    case INDEX_op_xor_i32:
        c = ARITH_EOR;
        /* Fall through.  */
    gen_arith:
1437 1438 1439
        if (const_args[2]) {
            int rot;
            rot = encode_imm(args[2]);
1440
            tcg_out_dat_imm(s, COND_AL, c,
1441 1442
                            args[0], args[1], rotl(args[2], rot) | (rot << 7));
        } else
1443 1444
            tcg_out_dat_reg(s, COND_AL, c,
                            args[0], args[1], args[2], SHIFT_IMM_LSL(0));
B
balrog 已提交
1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455
        break;
    case INDEX_op_add2_i32:
        tcg_out_dat_reg2(s, COND_AL, ARITH_ADD, ARITH_ADC,
                        args[0], args[1], args[2], args[3],
                        args[4], args[5], SHIFT_IMM_LSL(0));
        break;
    case INDEX_op_sub2_i32:
        tcg_out_dat_reg2(s, COND_AL, ARITH_SUB, ARITH_SBC,
                        args[0], args[1], args[2], args[3],
                        args[4], args[5], SHIFT_IMM_LSL(0));
        break;
B
balrog 已提交
1456 1457 1458
    case INDEX_op_neg_i32:
        tcg_out_dat_imm(s, COND_AL, ARITH_RSB, args[0], args[1], 0);
        break;
L
Laurent Desnogues 已提交
1459 1460 1461 1462
    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 已提交
1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534
    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;
    case INDEX_op_div2_i32:
        tcg_out_div_helper(s, COND_AL, args,
                        tcg_helper_div_i64, tcg_helper_rem_i64,
                        SHIFT_IMM_ASR(31));
        break;
    case INDEX_op_divu2_i32:
        tcg_out_div_helper(s, COND_AL, args,
                        tcg_helper_divu_i64, tcg_helper_remu_i64,
                        SHIFT_IMM_LSR(31));
        break;
    /* 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]);
        /* Fall through.  */
    gen_shift32:
        tcg_out_dat_reg(s, COND_AL, ARITH_MOV, args[0], 0, args[1], c);
        break;

    case INDEX_op_brcond_i32:
        tcg_out_dat_reg(s, COND_AL, ARITH_CMP, 0,
                        args[0], args[1], SHIFT_IMM_LSL(0));
        tcg_out_goto_label(s, tcg_cond_to_arm_cond[args[2]], args[3]);
        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,
         */
        tcg_out_dat_reg(s, COND_AL, ARITH_CMP, 0,
                        args[1], args[3], SHIFT_IMM_LSL(0));
        tcg_out_dat_reg(s, COND_EQ, ARITH_CMP, 0,
                        args[0], args[2], SHIFT_IMM_LSL(0));
        tcg_out_goto_label(s, tcg_cond_to_arm_cond[args[4]], args[5]);
        break;

    case INDEX_op_qemu_ld8u:
        tcg_out_qemu_ld(s, COND_AL, args, 0);
        break;
    case INDEX_op_qemu_ld8s:
        tcg_out_qemu_ld(s, COND_AL, args, 0 | 4);
        break;
    case INDEX_op_qemu_ld16u:
        tcg_out_qemu_ld(s, COND_AL, args, 1);
        break;
    case INDEX_op_qemu_ld16s:
        tcg_out_qemu_ld(s, COND_AL, args, 1 | 4);
        break;
    case INDEX_op_qemu_ld32u:
        tcg_out_qemu_ld(s, COND_AL, args, 2);
        break;
    case INDEX_op_qemu_ld64:
        tcg_out_qemu_ld(s, COND_AL, args, 3);
        break;
B
balrog 已提交
1535

B
balrog 已提交
1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586
    case INDEX_op_qemu_st8:
        tcg_out_qemu_st(s, COND_AL, args, 0);
        break;
    case INDEX_op_qemu_st16:
        tcg_out_qemu_st(s, COND_AL, args, 1);
        break;
    case INDEX_op_qemu_st32:
        tcg_out_qemu_st(s, COND_AL, args, 2);
        break;
    case INDEX_op_qemu_st64:
        tcg_out_qemu_st(s, COND_AL, args, 3);
        break;

    case INDEX_op_ext8s_i32:
        tcg_out_dat_reg(s, COND_AL, ARITH_MOV,
                        args[0], 0, args[1], SHIFT_IMM_LSL(24));
        tcg_out_dat_reg(s, COND_AL, ARITH_MOV,
                        args[0], 0, args[0], SHIFT_IMM_ASR(24));
        break;
    case INDEX_op_ext16s_i32:
        tcg_out_dat_reg(s, COND_AL, ARITH_MOV,
                        args[0], 0, args[1], SHIFT_IMM_LSL(16));
        tcg_out_dat_reg(s, COND_AL, ARITH_MOV,
                        args[0], 0, args[0], SHIFT_IMM_ASR(16));
        break;

    default:
        tcg_abort();
    }
}

static const TCGTargetOpDef arm_op_defs[] = {
    { INDEX_op_exit_tb, { } },
    { INDEX_op_goto_tb, { } },
    { INDEX_op_call, { "ri" } },
    { INDEX_op_jmp, { "ri" } },
    { INDEX_op_br, { } },

    { INDEX_op_mov_i32, { "r", "r" } },
    { INDEX_op_movi_i32, { "r" } },

    { 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" */
1587 1588
    { INDEX_op_add_i32, { "r", "r", "rI" } },
    { INDEX_op_sub_i32, { "r", "r", "rI" } },
B
balrog 已提交
1589 1590 1591 1592
    { INDEX_op_mul_i32, { "r", "r", "r" } },
    { INDEX_op_mulu2_i32, { "r", "r", "r", "r" } },
    { INDEX_op_div2_i32, { "r", "r", "r", "1", "2" } },
    { INDEX_op_divu2_i32, { "r", "r", "r", "1", "2" } },
1593 1594 1595
    { INDEX_op_and_i32, { "r", "r", "rI" } },
    { INDEX_op_or_i32, { "r", "r", "rI" } },
    { INDEX_op_xor_i32, { "r", "r", "rI" } },
B
balrog 已提交
1596
    { INDEX_op_neg_i32, { "r", "r" } },
L
Laurent Desnogues 已提交
1597
    { INDEX_op_not_i32, { "r", "r" } },
B
balrog 已提交
1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614

    { INDEX_op_shl_i32, { "r", "r", "ri" } },
    { INDEX_op_shr_i32, { "r", "r", "ri" } },
    { INDEX_op_sar_i32, { "r", "r", "ri" } },

    { INDEX_op_brcond_i32, { "r", "r" } },

    /* TODO: "r", "r", "r", "r", "ri", "ri" */
    { INDEX_op_add2_i32, { "r", "r", "r", "r", "r", "r" } },
    { INDEX_op_sub2_i32, { "r", "r", "r", "r", "r", "r" } },
    { INDEX_op_brcond2_i32, { "r", "r", "r", "r" } },

    { INDEX_op_qemu_ld8u, { "r", "x", "X" } },
    { INDEX_op_qemu_ld8s, { "r", "x", "X" } },
    { INDEX_op_qemu_ld16u, { "r", "x", "X" } },
    { INDEX_op_qemu_ld16s, { "r", "x", "X" } },
    { INDEX_op_qemu_ld32u, { "r", "x", "X" } },
1615
    { INDEX_op_qemu_ld64, { "d", "r", "x", "X" } },
B
balrog 已提交
1616

P
pbrook 已提交
1617 1618 1619 1620
    { INDEX_op_qemu_st8, { "x", "x", "X" } },
    { INDEX_op_qemu_st16, { "x", "x", "X" } },
    { INDEX_op_qemu_st32, { "x", "x", "X" } },
    { INDEX_op_qemu_st64, { "x", "D", "x", "X" } },
B
balrog 已提交
1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661

    { INDEX_op_ext8s_i32, { "r", "r" } },
    { INDEX_op_ext16s_i32, { "r", "r" } },

    { -1 },
};

void tcg_target_init(TCGContext *s)
{
    /* fail safe */
    if ((1 << CPU_TLB_ENTRY_BITS) != sizeof(CPUTLBEntry))
        tcg_abort();

    tcg_regset_set32(tcg_target_available_regs[TCG_TYPE_I32], 0,
                    ((2 << TCG_REG_R14) - 1) & ~(1 << TCG_REG_R8));
    tcg_regset_set32(tcg_target_call_clobber_regs, 0,
                    ((2 << TCG_REG_R3) - 1) |
                    (1 << TCG_REG_R12) | (1 << TCG_REG_R14));

    tcg_regset_clear(s->reserved_regs);
#ifdef SAVE_LR
    tcg_regset_set_reg(s->reserved_regs, TCG_REG_R14);
#endif
    tcg_regset_set_reg(s->reserved_regs, TCG_REG_CALL_STACK);
    tcg_regset_set_reg(s->reserved_regs, TCG_REG_R8);

    tcg_add_target_add_op_defs(arm_op_defs);
}

static inline void tcg_out_ld(TCGContext *s, TCGType type, int arg,
                int arg1, tcg_target_long arg2)
{
    tcg_out_ld32u(s, COND_AL, arg, arg1, arg2);
}

static inline void tcg_out_st(TCGContext *s, TCGType type, int arg,
                int arg1, tcg_target_long arg2)
{
    tcg_out_st32(s, COND_AL, arg, arg1, arg2);
}

P
Paul Brook 已提交
1662
static void tcg_out_addi(TCGContext *s, int reg, tcg_target_long val)
B
balrog 已提交
1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698
{
    if (val > 0)
        if (val < 0x100)
            tcg_out_dat_imm(s, COND_AL, ARITH_ADD, reg, reg, val);
        else
            tcg_abort();
    else if (val < 0) {
        if (val > -0x100)
            tcg_out_dat_imm(s, COND_AL, ARITH_SUB, reg, reg, -val);
        else
            tcg_abort();
    }
}

static inline void tcg_out_mov(TCGContext *s, int ret, int arg)
{
    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,
                int ret, tcg_target_long arg)
{
    tcg_out_movi32(s, COND_AL, ret, arg);
}

void tcg_target_qemu_prologue(TCGContext *s)
{
    /* stmdb sp!, { r9 - r11, lr } */
    tcg_out32(s, (COND_AL << 28) | 0x092d4e00);

    tcg_out_bx(s, COND_AL, TCG_REG_R0);
    tb_ret_addr = s->code_ptr;

    /* ldmia sp!, { r9 - r11, pc } */
    tcg_out32(s, (COND_AL << 28) | 0x08bd8e00);
}