tcg-target.c 81.6 KB
Newer Older
A
Aurelien Jarno 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
/*
 * Tiny Code Generator for QEMU
 *
 * Copyright (c) 2009-2010 Aurelien Jarno <aurelien@aurel32.net>
 * Based on i386/tcg-target.c - Copyright (c) 2008 Fabrice Bellard
 *
 * 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.
 */

/*
 * Register definitions
 */

#ifndef NDEBUG
static const char * const tcg_target_reg_names[TCG_TARGET_NB_REGS] = {
     "r0",  "r1",  "r2",  "r3",  "r4",  "r5",  "r6",  "r7",
     "r8",  "r9", "r10", "r11", "r12", "r13", "r14", "r15",
    "r16", "r17", "r18", "r19", "r20", "r21", "r22", "r23",
    "r24", "r25", "r26", "r27", "r28", "r29", "r30", "r31",
    "r32", "r33", "r34", "r35", "r36", "r37", "r38", "r39",
    "r40", "r41", "r42", "r43", "r44", "r45", "r46", "r47",
    "r48", "r49", "r50", "r51", "r52", "r53", "r54", "r55",
    "r56", "r57", "r58", "r59", "r60", "r61", "r62", "r63",
};
#endif

43 44 45 46 47
#ifdef CONFIG_USE_GUEST_BASE
#define TCG_GUEST_BASE_REG TCG_REG_R55
#else
#define TCG_GUEST_BASE_REG TCG_REG_R0
#endif
48 49 50
#ifndef GUEST_BASE
#define GUEST_BASE 0
#endif
51

A
Aurelien Jarno 已提交
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 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
/* Branch registers */
enum {
    TCG_REG_B0 = 0,
    TCG_REG_B1,
    TCG_REG_B2,
    TCG_REG_B3,
    TCG_REG_B4,
    TCG_REG_B5,
    TCG_REG_B6,
    TCG_REG_B7,
};

/* Floating point registers */
enum {
    TCG_REG_F0 = 0,
    TCG_REG_F1,
    TCG_REG_F2,
    TCG_REG_F3,
    TCG_REG_F4,
    TCG_REG_F5,
    TCG_REG_F6,
    TCG_REG_F7,
    TCG_REG_F8,
    TCG_REG_F9,
    TCG_REG_F10,
    TCG_REG_F11,
    TCG_REG_F12,
    TCG_REG_F13,
    TCG_REG_F14,
    TCG_REG_F15,
};

/* Predicate registers */
enum {
    TCG_REG_P0 = 0,
    TCG_REG_P1,
    TCG_REG_P2,
    TCG_REG_P3,
    TCG_REG_P4,
    TCG_REG_P5,
    TCG_REG_P6,
    TCG_REG_P7,
    TCG_REG_P8,
    TCG_REG_P9,
    TCG_REG_P10,
    TCG_REG_P11,
    TCG_REG_P12,
    TCG_REG_P13,
    TCG_REG_P14,
    TCG_REG_P15,
};

/* Application registers */
enum {
    TCG_REG_PFS = 64,
};

static const int tcg_target_reg_alloc_order[] = {
    TCG_REG_R35,
    TCG_REG_R36,
    TCG_REG_R37,
    TCG_REG_R38,
    TCG_REG_R39,
    TCG_REG_R40,
    TCG_REG_R41,
    TCG_REG_R42,
    TCG_REG_R43,
    TCG_REG_R44,
    TCG_REG_R45,
    TCG_REG_R46,
    TCG_REG_R47,
    TCG_REG_R48,
    TCG_REG_R49,
    TCG_REG_R50,
    TCG_REG_R51,
    TCG_REG_R52,
    TCG_REG_R53,
    TCG_REG_R54,
    TCG_REG_R55,
    TCG_REG_R14,
    TCG_REG_R15,
    TCG_REG_R16,
    TCG_REG_R17,
    TCG_REG_R18,
    TCG_REG_R19,
    TCG_REG_R20,
    TCG_REG_R21,
    TCG_REG_R22,
    TCG_REG_R23,
    TCG_REG_R24,
    TCG_REG_R25,
    TCG_REG_R26,
    TCG_REG_R27,
    TCG_REG_R28,
    TCG_REG_R29,
    TCG_REG_R30,
    TCG_REG_R31,
    TCG_REG_R56,
    TCG_REG_R57,
    TCG_REG_R58,
    TCG_REG_R59,
    TCG_REG_R60,
    TCG_REG_R61,
    TCG_REG_R62,
    TCG_REG_R63,
    TCG_REG_R8,
    TCG_REG_R9,
    TCG_REG_R10,
    TCG_REG_R11
};

static const int tcg_target_call_iarg_regs[8] = {
    TCG_REG_R56,
    TCG_REG_R57,
    TCG_REG_R58,
    TCG_REG_R59,
    TCG_REG_R60,
    TCG_REG_R61,
    TCG_REG_R62,
    TCG_REG_R63,
};

174 175
static const int tcg_target_call_oarg_regs[] = {
    TCG_REG_R8
A
Aurelien Jarno 已提交
176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221
};

/*
 * opcode formation
 */

/* bundle templates: stops (double bar in the IA64 manual) are marked with
   an uppercase letter. */
enum {
    mii = 0x00,
    miI = 0x01,
    mIi = 0x02,
    mII = 0x03,
    mlx = 0x04,
    mLX = 0x05,
    mmi = 0x08,
    mmI = 0x09,
    Mmi = 0x0a,
    MmI = 0x0b,
    mfi = 0x0c,
    mfI = 0x0d,
    mmf = 0x0e,
    mmF = 0x0f,
    mib = 0x10,
    miB = 0x11,
    mbb = 0x12,
    mbB = 0x13,
    bbb = 0x16,
    bbB = 0x17,
    mmb = 0x18,
    mmB = 0x19,
    mfb = 0x1c,
    mfB = 0x1d,
};

enum {
    OPC_ADD_A1                = 0x10000000000ull,
    OPC_AND_A1                = 0x10060000000ull,
    OPC_AND_A3                = 0x10160000000ull,
    OPC_ANDCM_A1              = 0x10068000000ull,
    OPC_ANDCM_A3              = 0x10168000000ull,
    OPC_ADDS_A4               = 0x10800000000ull,
    OPC_ADDL_A5               = 0x12000000000ull,
    OPC_ALLOC_M34             = 0x02c00000000ull,
    OPC_BR_DPTK_FEW_B1        = 0x08400000000ull,
    OPC_BR_SPTK_MANY_B1       = 0x08000001000ull,
222
    OPC_BR_CALL_SPNT_FEW_B3   = 0x0a200000000ull,
A
Aurelien Jarno 已提交
223 224 225 226
    OPC_BR_SPTK_MANY_B4       = 0x00100001000ull,
    OPC_BR_CALL_SPTK_MANY_B5  = 0x02100001000ull,
    OPC_BR_RET_SPTK_MANY_B4   = 0x00108001100ull,
    OPC_BRL_SPTK_MANY_X3      = 0x18000001000ull,
227
    OPC_BRL_CALL_SPNT_MANY_X4 = 0x1a200001000ull,
228
    OPC_BRL_CALL_SPTK_MANY_X4 = 0x1a000001000ull,
A
Aurelien Jarno 已提交
229 230 231 232 233 234
    OPC_CMP_LT_A6             = 0x18000000000ull,
    OPC_CMP_LTU_A6            = 0x1a000000000ull,
    OPC_CMP_EQ_A6             = 0x1c000000000ull,
    OPC_CMP4_LT_A6            = 0x18400000000ull,
    OPC_CMP4_LTU_A6           = 0x1a400000000ull,
    OPC_CMP4_EQ_A6            = 0x1c400000000ull,
235
    OPC_DEP_I14               = 0x0ae00000000ull,
R
Richard Henderson 已提交
236
    OPC_DEP_I15               = 0x08000000000ull,
A
Aurelien Jarno 已提交
237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265
    OPC_DEP_Z_I12             = 0x0a600000000ull,
    OPC_EXTR_I11              = 0x0a400002000ull,
    OPC_EXTR_U_I11            = 0x0a400000000ull,
    OPC_FCVT_FX_TRUNC_S1_F10  = 0x004d0000000ull,
    OPC_FCVT_FXU_TRUNC_S1_F10 = 0x004d8000000ull,
    OPC_FCVT_XF_F11           = 0x000e0000000ull,
    OPC_FMA_S1_F1             = 0x10400000000ull,
    OPC_FNMA_S1_F1            = 0x18400000000ull,
    OPC_FRCPA_S1_F6           = 0x00600000000ull,
    OPC_GETF_SIG_M19          = 0x08708000000ull,
    OPC_LD1_M1                = 0x08000000000ull,
    OPC_LD1_M3                = 0x0a000000000ull,
    OPC_LD2_M1                = 0x08040000000ull,
    OPC_LD2_M3                = 0x0a040000000ull,
    OPC_LD4_M1                = 0x08080000000ull,
    OPC_LD4_M3                = 0x0a080000000ull,
    OPC_LD8_M1                = 0x080c0000000ull,
    OPC_LD8_M3                = 0x0a0c0000000ull,
    OPC_MUX1_I3               = 0x0eca0000000ull,
    OPC_NOP_B9                = 0x04008000000ull,
    OPC_NOP_F16               = 0x00008000000ull,
    OPC_NOP_I18               = 0x00008000000ull,
    OPC_NOP_M48               = 0x00008000000ull,
    OPC_MOV_I21               = 0x00e00100000ull,
    OPC_MOV_RET_I21           = 0x00e00500000ull,
    OPC_MOV_I22               = 0x00188000000ull,
    OPC_MOV_I_I26             = 0x00150000000ull,
    OPC_MOVL_X2               = 0x0c000000000ull,
    OPC_OR_A1                 = 0x10070000000ull,
266
    OPC_OR_A3                 = 0x10170000000ull,
A
Aurelien Jarno 已提交
267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284
    OPC_SETF_EXP_M18          = 0x0c748000000ull,
    OPC_SETF_SIG_M18          = 0x0c708000000ull,
    OPC_SHL_I7                = 0x0f240000000ull,
    OPC_SHR_I5                = 0x0f220000000ull,
    OPC_SHR_U_I5              = 0x0f200000000ull,
    OPC_SHRP_I10              = 0x0ac00000000ull,
    OPC_SXT1_I29              = 0x000a0000000ull,
    OPC_SXT2_I29              = 0x000a8000000ull,
    OPC_SXT4_I29              = 0x000b0000000ull,
    OPC_ST1_M4                = 0x08c00000000ull,
    OPC_ST2_M4                = 0x08c40000000ull,
    OPC_ST4_M4                = 0x08c80000000ull,
    OPC_ST8_M4                = 0x08cc0000000ull,
    OPC_SUB_A1                = 0x10028000000ull,
    OPC_SUB_A3                = 0x10128000000ull,
    OPC_UNPACK4_L_I2          = 0x0f860000000ull,
    OPC_XMA_L_F2              = 0x1d000000000ull,
    OPC_XOR_A1                = 0x10078000000ull,
285
    OPC_XOR_A3                = 0x10178000000ull,
A
Aurelien Jarno 已提交
286 287 288
    OPC_ZXT1_I29              = 0x00080000000ull,
    OPC_ZXT2_I29              = 0x00088000000ull,
    OPC_ZXT4_I29              = 0x00090000000ull,
289 290 291

    INSN_NOP_M                = OPC_NOP_M48,  /* nop.m 0 */
    INSN_NOP_I                = OPC_NOP_I18,  /* nop.i 0 */
A
Aurelien Jarno 已提交
292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 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 349 350 351 352 353 354 355 356 357 358
};

static inline uint64_t tcg_opc_a1(int qp, uint64_t opc, int r1,
                                  int r2, int r3)
{
    return opc
           | ((r3 & 0x7f) << 20)
           | ((r2 & 0x7f) << 13)
           | ((r1 & 0x7f) << 6)
           | (qp & 0x3f);
}

static inline uint64_t tcg_opc_a3(int qp, uint64_t opc, int r1,
                                  uint64_t imm, int r3)
{
    return opc
           | ((imm & 0x80) << 29) /* s */
           | ((imm & 0x7f) << 13) /* imm7b */
           | ((r3 & 0x7f) << 20)
           | ((r1 & 0x7f) << 6)
           | (qp & 0x3f);
}

static inline uint64_t tcg_opc_a4(int qp, uint64_t opc, int r1,
                                  uint64_t imm, int r3)
{
    return opc
           | ((imm & 0x2000) << 23) /* s */
           | ((imm & 0x1f80) << 20) /* imm6d */
           | ((imm & 0x007f) << 13) /* imm7b */
           | ((r3 & 0x7f) << 20)
           | ((r1 & 0x7f) << 6)
           | (qp & 0x3f);
}

static inline uint64_t tcg_opc_a5(int qp, uint64_t opc, int r1,
                                  uint64_t imm, int r3)
{
    return opc
           | ((imm & 0x200000) << 15) /* s */
           | ((imm & 0x1f0000) <<  6) /* imm5c */
           | ((imm & 0x00ff80) << 20) /* imm9d */
           | ((imm & 0x00007f) << 13) /* imm7b */
           | ((r3 & 0x03) << 20)
           | ((r1 & 0x7f) << 6)
           | (qp & 0x3f);
}

static inline uint64_t tcg_opc_a6(int qp, uint64_t opc, int p1,
                                  int p2, int r2, int r3)
{
    return opc
           | ((p2 & 0x3f) << 27)
           | ((r3 & 0x7f) << 20)
           | ((r2 & 0x7f) << 13)
           | ((p1 & 0x3f) << 6)
           | (qp & 0x3f);
}

static inline uint64_t tcg_opc_b1(int qp, uint64_t opc, uint64_t imm)
{
    return opc
           | ((imm & 0x100000) << 16) /* s */
           | ((imm & 0x0fffff) << 13) /* imm20b */
           | (qp & 0x3f);
}

359 360 361 362 363 364 365 366 367
static inline uint64_t tcg_opc_b3(int qp, uint64_t opc, int b1, uint64_t imm)
{
    return opc
           | ((imm & 0x100000) << 16) /* s */
           | ((imm & 0x0fffff) << 13) /* imm20b */
           | ((b1 & 0x7) << 6)
           | (qp & 0x3f);
}

A
Aurelien Jarno 已提交
368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 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 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521
static inline uint64_t tcg_opc_b4(int qp, uint64_t opc, int b2)
{
    return opc
           | ((b2 & 0x7) << 13)
           | (qp & 0x3f);
}

static inline uint64_t tcg_opc_b5(int qp, uint64_t opc, int b1, int b2)
{
    return opc
           | ((b2 & 0x7) << 13)
           | ((b1 & 0x7) << 6)
           | (qp & 0x3f);
}


static inline uint64_t tcg_opc_b9(int qp, uint64_t opc, uint64_t imm)
{
    return opc
           | ((imm & 0x100000) << 16) /* i */
           | ((imm & 0x0fffff) << 6)  /* imm20a */
           | (qp & 0x3f);
}

static inline uint64_t tcg_opc_f1(int qp, uint64_t opc, int f1,
                                  int f3, int f4, int f2)
{
    return opc
           | ((f4 & 0x7f) << 27)
           | ((f3 & 0x7f) << 20)
           | ((f2 & 0x7f) << 13)
           | ((f1 & 0x7f) << 6)
           | (qp & 0x3f);
}

static inline uint64_t tcg_opc_f2(int qp, uint64_t opc, int f1,
                                  int f3, int f4, int f2)
{
    return opc
           | ((f4 & 0x7f) << 27)
           | ((f3 & 0x7f) << 20)
           | ((f2 & 0x7f) << 13)
           | ((f1 & 0x7f) << 6)
           | (qp & 0x3f);
}

static inline uint64_t tcg_opc_f6(int qp, uint64_t opc, int f1,
                                  int p2, int f2, int f3)
{
    return opc
           | ((p2 & 0x3f) << 27)
           | ((f3 & 0x7f) << 20)
           | ((f2 & 0x7f) << 13)
           | ((f1 & 0x7f) << 6)
           | (qp & 0x3f);
}

static inline uint64_t tcg_opc_f10(int qp, uint64_t opc, int f1, int f2)
{
    return opc
           | ((f2 & 0x7f) << 13)
           | ((f1 & 0x7f) << 6)
           | (qp & 0x3f);
}

static inline uint64_t tcg_opc_f11(int qp, uint64_t opc, int f1, int f2)
{
    return opc
           | ((f2 & 0x7f) << 13)
           | ((f1 & 0x7f) << 6)
           | (qp & 0x3f);
}

static inline uint64_t tcg_opc_f16(int qp, uint64_t opc, uint64_t imm)
{
    return opc
           | ((imm & 0x100000) << 16) /* i */
           | ((imm & 0x0fffff) << 6)  /* imm20a */
           | (qp & 0x3f);
}

static inline uint64_t tcg_opc_i2(int qp, uint64_t opc, int r1,
                                  int r2, int r3)
{
    return opc
           | ((r3 & 0x7f) << 20)
           | ((r2 & 0x7f) << 13)
           | ((r1 & 0x7f) << 6)
           | (qp & 0x3f);
}

static inline uint64_t tcg_opc_i3(int qp, uint64_t opc, int r1,
                                  int r2, int mbtype)
{
    return opc
           | ((mbtype & 0x0f) << 20)
           | ((r2 & 0x7f) << 13)
           | ((r1 & 0x7f) << 6)
           | (qp & 0x3f);
}

static inline uint64_t tcg_opc_i5(int qp, uint64_t opc, int r1,
                                  int r3, int r2)
{
    return opc
           | ((r3 & 0x7f) << 20)
           | ((r2 & 0x7f) << 13)
           | ((r1 & 0x7f) << 6)
           | (qp & 0x3f);
}

static inline uint64_t tcg_opc_i7(int qp, uint64_t opc, int r1,
                                  int r2, int r3)
{
    return opc
           | ((r3 & 0x7f) << 20)
           | ((r2 & 0x7f) << 13)
           | ((r1 & 0x7f) << 6)
           | (qp & 0x3f);
}

static inline uint64_t tcg_opc_i10(int qp, uint64_t opc, int r1,
                                   int r2, int r3, uint64_t count)
{
    return opc
           | ((count & 0x3f) << 27)
           | ((r3 & 0x7f) << 20)
           | ((r2 & 0x7f) << 13)
           | ((r1 & 0x7f) << 6)
           | (qp & 0x3f);
}

static inline uint64_t tcg_opc_i11(int qp, uint64_t opc, int r1,
                                   int r3, uint64_t pos, uint64_t len)
{
    return opc
           | ((len & 0x3f) << 27)
           | ((r3 & 0x7f) << 20)
           | ((pos & 0x3f) << 14)
           | ((r1 & 0x7f) << 6)
           | (qp & 0x3f);
}

static inline uint64_t tcg_opc_i12(int qp, uint64_t opc, int r1,
                                   int r2, uint64_t pos, uint64_t len)
{
    return opc
           | ((len & 0x3f) << 27)
           | ((pos & 0x3f) << 20)
           | ((r2 & 0x7f) << 13)
           | ((r1 & 0x7f) << 6)
           | (qp & 0x3f);
}

522 523 524 525 526 527 528 529 530 531 532 533
static inline uint64_t tcg_opc_i14(int qp, uint64_t opc, int r1, uint64_t imm,
                                   int r3, uint64_t pos, uint64_t len)
{
    return opc
           | ((imm & 0x01) << 36)
           | ((len & 0x3f) << 27)
           | ((r3 & 0x7f) << 20)
           | ((pos & 0x3f) << 14)
           | ((r1 & 0x7f) << 6)
           | (qp & 0x3f);
}

R
Richard Henderson 已提交
534 535 536 537 538 539 540 541 542 543 544 545
static inline uint64_t tcg_opc_i15(int qp, uint64_t opc, int r1, int r2,
                                   int r3, uint64_t pos, uint64_t len)
{
    return opc
           | ((pos & 0x3f) << 31)
           | ((len & 0x0f) << 27)
           | ((r3 & 0x7f) << 20)
           | ((r2 & 0x7f) << 13)
           | ((r1 & 0x7f) << 6)
           | (qp & 0x3f);
}

A
Aurelien Jarno 已提交
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
static inline uint64_t tcg_opc_i18(int qp, uint64_t opc, uint64_t imm)
{
    return opc
           | ((imm & 0x100000) << 16) /* i */
           | ((imm & 0x0fffff) << 6)  /* imm20a */
           | (qp & 0x3f);
}

static inline uint64_t tcg_opc_i21(int qp, uint64_t opc, int b1,
                                   int r2, uint64_t imm)
{
    return opc
           | ((imm & 0x1ff) << 24)
           | ((r2 & 0x7f) << 13)
           | ((b1 & 0x7) << 6)
           | (qp & 0x3f);
}

static inline uint64_t tcg_opc_i22(int qp, uint64_t opc, int r1, int b2)
{
    return opc
           | ((b2 & 0x7) << 13)
           | ((r1 & 0x7f) << 6)
           | (qp & 0x3f);
}

static inline uint64_t tcg_opc_i26(int qp, uint64_t opc, int ar3, int r2)
{
    return opc
           | ((ar3 & 0x7f) << 20)
           | ((r2 & 0x7f) << 13)
           | (qp & 0x3f);
}

static inline uint64_t tcg_opc_i29(int qp, uint64_t opc, int r1, int r3)
{
    return opc
           | ((r3 & 0x7f) << 20)
           | ((r1 & 0x7f) << 6)
           | (qp & 0x3f);
}

static inline uint64_t tcg_opc_l2(uint64_t imm)
{
    return (imm & 0x7fffffffffc00000ull) >> 22;
}

static inline uint64_t tcg_opc_l3(uint64_t imm)
{
    return (imm & 0x07fffffffff00000ull) >> 18;
}

598 599
#define tcg_opc_l4  tcg_opc_l3

A
Aurelien Jarno 已提交
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 646 647 648 649 650 651 652 653 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
static inline uint64_t tcg_opc_m1(int qp, uint64_t opc, int r1, int r3)
{
    return opc
           | ((r3 & 0x7f) << 20)
           | ((r1 & 0x7f) << 6)
           | (qp & 0x3f);
}

static inline uint64_t tcg_opc_m3(int qp, uint64_t opc, int r1,
                                  int r3, uint64_t imm)
{
    return opc
           | ((imm & 0x100) << 28) /* s */
           | ((imm & 0x080) << 20) /* i */
           | ((imm & 0x07f) << 13) /* imm7b */
           | ((r3 & 0x7f) << 20)
           | ((r1 & 0x7f) << 6)
           | (qp & 0x3f);
}

static inline uint64_t tcg_opc_m4(int qp, uint64_t opc, int r2, int r3)
{
    return opc
           | ((r3 & 0x7f) << 20)
           | ((r2 & 0x7f) << 13)
           | (qp & 0x3f);
}

static inline uint64_t tcg_opc_m18(int qp, uint64_t opc, int f1, int r2)
{
    return opc
           | ((r2 & 0x7f) << 13)
           | ((f1 & 0x7f) << 6)
           | (qp & 0x3f);
}

static inline uint64_t tcg_opc_m19(int qp, uint64_t opc, int r1, int f2)
{
    return opc
           | ((f2 & 0x7f) << 13)
           | ((r1 & 0x7f) << 6)
           | (qp & 0x3f);
}

static inline uint64_t tcg_opc_m34(int qp, uint64_t opc, int r1,
                                   int sof, int sol, int sor)
{
    return opc
           | ((sor & 0x0f) << 27)
           | ((sol & 0x7f) << 20)
           | ((sof & 0x7f) << 13)
           | ((r1 & 0x7f) << 6)
           | (qp & 0x3f);
}

static inline uint64_t tcg_opc_m48(int qp, uint64_t opc, uint64_t imm)
{
    return opc
           | ((imm & 0x100000) << 16) /* i */
           | ((imm & 0x0fffff) << 6)  /* imm20a */
           | (qp & 0x3f);
}

static inline uint64_t tcg_opc_x2(int qp, uint64_t opc,
                                  int r1, uint64_t imm)
{
    return opc
           | ((imm & 0x8000000000000000ull) >> 27) /* i */
           |  (imm & 0x0000000000200000ull)        /* ic */
           | ((imm & 0x00000000001f0000ull) << 6)  /* imm5c */
           | ((imm & 0x000000000000ff80ull) << 20) /* imm9d */
           | ((imm & 0x000000000000007full) << 13) /* imm7b */
           | ((r1 & 0x7f) << 6)
           | (qp & 0x3f);
}

static inline uint64_t tcg_opc_x3(int qp, uint64_t opc, uint64_t imm)
{
    return opc
           | ((imm & 0x0800000000000000ull) >> 23) /* i */
           | ((imm & 0x00000000000fffffull) << 13) /* imm20b */
           | (qp & 0x3f);
}

684 685 686 687 688 689 690 691 692
static inline uint64_t tcg_opc_x4(int qp, uint64_t opc, int b1, uint64_t imm)
{
    return opc
           | ((imm & 0x0800000000000000ull) >> 23) /* i */
           | ((imm & 0x00000000000fffffull) << 13) /* imm20b */
           | ((b1 & 0x7) << 6)
           | (qp & 0x3f);
}

A
Aurelien Jarno 已提交
693 694

/*
695
 * Relocations - Note that we never encode branches elsewhere than slot 2.
A
Aurelien Jarno 已提交
696 697
 */

698
static void reloc_pcrel21b_slot2(tcg_insn_unit *pc, tcg_insn_unit *target)
A
Aurelien Jarno 已提交
699
{
700
    uint64_t imm = target - pc;
A
Aurelien Jarno 已提交
701

702 703 704
    pc->hi = (pc->hi & 0xf700000fffffffffull)
             | ((imm & 0x100000) << 39)  /* s */
             | ((imm & 0x0fffff) << 36); /* imm20b */
A
Aurelien Jarno 已提交
705 706
}

707
static uint64_t get_reloc_pcrel21b_slot2(tcg_insn_unit *pc)
A
Aurelien Jarno 已提交
708
{
709
    int64_t high = pc->hi;
A
Aurelien Jarno 已提交
710

711 712
    return ((high >> 39) & 0x100000) + /* s */
           ((high >> 36) & 0x0fffff);  /* imm20b */
A
Aurelien Jarno 已提交
713 714
}

715
static void patch_reloc(tcg_insn_unit *code_ptr, int type,
716
                        intptr_t value, intptr_t addend)
A
Aurelien Jarno 已提交
717
{
718 719 720
    assert(addend == 0);
    assert(type == R_IA64_PCREL21B);
    reloc_pcrel21b_slot2(code_ptr, (tcg_insn_unit *)value);
A
Aurelien Jarno 已提交
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
}

/*
 * Constraints
 */

/* parse target specific constraints */
static int target_parse_constraint(TCGArgConstraint *ct, const char **pct_str)
{
    const char *ct_str;

    ct_str = *pct_str;
    switch(ct_str[0]) {
    case 'r':
        ct->ct |= TCG_CT_REG;
        tcg_regset_set(ct->u.regs, 0xffffffffffffffffull);
        break;
    case 'I':
        ct->ct |= TCG_CT_CONST_S22;
        break;
    case 'S':
        ct->ct |= TCG_CT_REG;
        tcg_regset_set(ct->u.regs, 0xffffffffffffffffull);
#if defined(CONFIG_SOFTMMU)
        tcg_regset_reset_reg(ct->u.regs, TCG_REG_R56);
        tcg_regset_reset_reg(ct->u.regs, TCG_REG_R57);
747
        tcg_regset_reset_reg(ct->u.regs, TCG_REG_R58);
A
Aurelien Jarno 已提交
748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764
#endif
        break;
    case 'Z':
        /* We are cheating a bit here, using the fact that the register
           r0 is also the register number 0. Hence there is no need
           to check for const_args in each instruction. */
        ct->ct |= TCG_CT_CONST_ZERO;
        break;
    default:
        return -1;
    }
    ct_str++;
    *pct_str = ct_str;
    return 0;
}

/* test if a constant matches the constraint */
765
static inline int tcg_target_const_match(tcg_target_long val, TCGType type,
A
Aurelien Jarno 已提交
766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783
                                         const TCGArgConstraint *arg_ct)
{
    int ct;
    ct = arg_ct->ct;
    if (ct & TCG_CT_CONST)
        return 1;
    else if ((ct & TCG_CT_CONST_ZERO) && val == 0)
        return 1;
    else if ((ct & TCG_CT_CONST_S22) && val == ((int32_t)val << 10) >> 10)
        return 1;
    else
        return 0;
}

/*
 * Code generation
 */

784
static tcg_insn_unit *tb_ret_addr;
A
Aurelien Jarno 已提交
785 786 787 788 789 790 791 792 793 794

static inline void tcg_out_bundle(TCGContext *s, int template,
                                  uint64_t slot0, uint64_t slot1,
                                  uint64_t slot2)
{
    template &= 0x1f;          /* 5 bits */
    slot0 &= 0x1ffffffffffull; /* 41 bits */
    slot1 &= 0x1ffffffffffull; /* 41 bits */
    slot2 &= 0x1ffffffffffull; /* 41 bits */

795 796 797 798
    *s->code_ptr++ = (tcg_insn_unit){
        (slot1 << 46) | (slot0 << 5) | template,
        (slot2 << 23) | (slot1 >> 18)
    };
A
Aurelien Jarno 已提交
799 800
}

801 802 803 804 805
static inline uint64_t tcg_opc_mov_a(int qp, TCGReg dst, TCGReg src)
{
    return tcg_opc_a4(qp, OPC_ADDS_A4, dst, 0, src);
}

806
static inline void tcg_out_mov(TCGContext *s, TCGType type,
807
                               TCGReg ret, TCGReg arg)
A
Aurelien Jarno 已提交
808 809
{
    tcg_out_bundle(s, mmI,
810 811
                   INSN_NOP_M,
                   INSN_NOP_M,
812
                   tcg_opc_mov_a(TCG_REG_P0, ret, arg));
A
Aurelien Jarno 已提交
813 814
}

815 816 817 818 819 820
static inline uint64_t tcg_opc_movi_a(int qp, TCGReg dst, int64_t src)
{
    assert(src == sextract64(src, 0, 22));
    return tcg_opc_a5(qp, OPC_ADDL_A5, dst, src, TCG_REG_R0);
}

A
Aurelien Jarno 已提交
821
static inline void tcg_out_movi(TCGContext *s, TCGType type,
822
                                TCGReg reg, tcg_target_long arg)
A
Aurelien Jarno 已提交
823 824
{
    tcg_out_bundle(s, mLX,
825
                   INSN_NOP_M,
A
Aurelien Jarno 已提交
826 827 828 829
                   tcg_opc_l2 (arg),
                   tcg_opc_x2 (TCG_REG_P0, OPC_MOVL_X2, reg, arg));
}

830
static void tcg_out_br(TCGContext *s, TCGLabel *l)
A
Aurelien Jarno 已提交
831
{
832
    uint64_t imm;
A
Aurelien Jarno 已提交
833

834 835 836
    /* We pay attention here to not modify the branch target by reading
       the existing value and using it again. This ensure that caches and
       memory are kept coherent during retranslation. */
A
Aurelien Jarno 已提交
837
    if (l->has_value) {
838
        imm = l->u.value_ptr -  s->code_ptr;
A
Aurelien Jarno 已提交
839
    } else {
840
        imm = get_reloc_pcrel21b_slot2(s->code_ptr);
841
        tcg_out_reloc(s, s->code_ptr, R_IA64_PCREL21B, l, 0);
A
Aurelien Jarno 已提交
842
    }
843 844 845 846 847

    tcg_out_bundle(s, mmB,
                   INSN_NOP_M,
                   INSN_NOP_M,
                   tcg_opc_b1(TCG_REG_P0, OPC_BR_SPTK_MANY_B1, imm));
A
Aurelien Jarno 已提交
848 849
}

850
static inline void tcg_out_call(TCGContext *s, tcg_insn_unit *desc)
851
{
852 853
    uintptr_t func = desc->lo, gp = desc->hi, disp;

854 855 856
    /* Look through the function descriptor.  */
    tcg_out_bundle(s, mlx,
                   INSN_NOP_M,
857 858 859
                   tcg_opc_l2 (gp),
                   tcg_opc_x2 (TCG_REG_P0, OPC_MOVL_X2, TCG_REG_R1, gp));
    disp = (tcg_insn_unit *)func - s->code_ptr;
860 861 862 863 864 865 866
    tcg_out_bundle(s, mLX,
                   INSN_NOP_M,
                   tcg_opc_l4 (disp),
                   tcg_opc_x4 (TCG_REG_P0, OPC_BRL_CALL_SPTK_MANY_X4,
                               TCG_REG_B0, disp));
}

A
Aurelien Jarno 已提交
867 868
static void tcg_out_exit_tb(TCGContext *s, tcg_target_long arg)
{
869
    uint64_t imm, opc1;
A
Aurelien Jarno 已提交
870

871 872 873 874 875 876 877
    /* At least arg == 0 is a common operation.  */
    if (arg == sextract64(arg, 0, 22)) {
        opc1 = tcg_opc_movi_a(TCG_REG_P0, TCG_REG_R8, arg);
    } else {
        tcg_out_movi(s, TCG_TYPE_PTR, TCG_REG_R8, arg);
        opc1 = INSN_NOP_M;
    }
A
Aurelien Jarno 已提交
878

879
    imm = tb_ret_addr - s->code_ptr;
A
Aurelien Jarno 已提交
880 881

    tcg_out_bundle(s, mLX,
882
                   opc1,
A
Aurelien Jarno 已提交
883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898
                   tcg_opc_l3 (imm),
                   tcg_opc_x3 (TCG_REG_P0, OPC_BRL_SPTK_MANY_X3, imm));
}

static inline void tcg_out_goto_tb(TCGContext *s, TCGArg arg)
{
    if (s->tb_jmp_offset) {
        /* direct jump method */
        tcg_abort();
    } else {
        /* indirect jump method */
        tcg_out_movi(s, TCG_TYPE_PTR, TCG_REG_R2,
                     (tcg_target_long)(s->tb_next + arg));
        tcg_out_bundle(s, MmI,
                       tcg_opc_m1 (TCG_REG_P0, OPC_LD8_M1,
                                   TCG_REG_R2, TCG_REG_R2),
899
                       INSN_NOP_M,
A
Aurelien Jarno 已提交
900 901 902
                       tcg_opc_i21(TCG_REG_P0, OPC_MOV_I21, TCG_REG_B6,
                                   TCG_REG_R2, 0));
        tcg_out_bundle(s, mmB,
903 904
                       INSN_NOP_M,
                       INSN_NOP_M,
A
Aurelien Jarno 已提交
905 906 907
                       tcg_opc_b4 (TCG_REG_P0, OPC_BR_SPTK_MANY_B4,
                                   TCG_REG_B6));
    }
908
    s->tb_next_offset[arg] = tcg_current_code_size(s);
A
Aurelien Jarno 已提交
909 910 911 912 913
}

static inline void tcg_out_jmp(TCGContext *s, TCGArg addr)
{
    tcg_out_bundle(s, mmI,
914 915
                   INSN_NOP_M,
                   INSN_NOP_M,
A
Aurelien Jarno 已提交
916 917
                   tcg_opc_i21(TCG_REG_P0, OPC_MOV_I21, TCG_REG_B6, addr, 0));
    tcg_out_bundle(s, mmB,
918 919
                   INSN_NOP_M,
                   INSN_NOP_M,
A
Aurelien Jarno 已提交
920 921 922 923 924 925 926 927 928 929 930
                   tcg_opc_b4(TCG_REG_P0, OPC_BR_SPTK_MANY_B4, TCG_REG_B6));
}

static inline void tcg_out_ld_rel(TCGContext *s, uint64_t opc_m4, TCGArg arg,
                                  TCGArg arg1, tcg_target_long arg2)
{
    if (arg2 == ((int16_t)arg2 >> 2) << 2) {
        tcg_out_bundle(s, MmI,
                       tcg_opc_a4(TCG_REG_P0, OPC_ADDS_A4,
                                  TCG_REG_R2, arg2, arg1),
                       tcg_opc_m1 (TCG_REG_P0, opc_m4, arg, TCG_REG_R2),
931
                       INSN_NOP_I);
A
Aurelien Jarno 已提交
932 933 934 935 936 937
    } else {
        tcg_out_movi(s, TCG_TYPE_PTR, TCG_REG_R2, arg2);
        tcg_out_bundle(s, MmI,
                       tcg_opc_a1 (TCG_REG_P0, OPC_ADD_A1,
                                   TCG_REG_R2, TCG_REG_R2, arg1),
                       tcg_opc_m1 (TCG_REG_P0, opc_m4, arg, TCG_REG_R2),
938
                       INSN_NOP_I);
A
Aurelien Jarno 已提交
939 940 941 942 943 944 945 946 947 948 949
    }
}

static inline void tcg_out_st_rel(TCGContext *s, uint64_t opc_m4, TCGArg arg,
                                  TCGArg arg1, tcg_target_long arg2)
{
    if (arg2 == ((int16_t)arg2 >> 2) << 2) {
        tcg_out_bundle(s, MmI,
                       tcg_opc_a4(TCG_REG_P0, OPC_ADDS_A4,
                                  TCG_REG_R2, arg2, arg1),
                       tcg_opc_m4 (TCG_REG_P0, opc_m4, arg, TCG_REG_R2),
950
                       INSN_NOP_I);
A
Aurelien Jarno 已提交
951 952 953 954 955 956
    } else {
        tcg_out_movi(s, TCG_TYPE_PTR, TCG_REG_R2, arg2);
        tcg_out_bundle(s, MmI,
                       tcg_opc_a1 (TCG_REG_P0, OPC_ADD_A1,
                                   TCG_REG_R2, TCG_REG_R2, arg1),
                       tcg_opc_m4 (TCG_REG_P0, opc_m4, arg, TCG_REG_R2),
957
                       INSN_NOP_I);
A
Aurelien Jarno 已提交
958 959 960
    }
}

961
static inline void tcg_out_ld(TCGContext *s, TCGType type, TCGReg arg,
962
                              TCGReg arg1, intptr_t arg2)
A
Aurelien Jarno 已提交
963 964 965 966 967 968 969 970
{
    if (type == TCG_TYPE_I32) {
        tcg_out_ld_rel(s, OPC_LD4_M1, arg, arg1, arg2);
    } else {
        tcg_out_ld_rel(s, OPC_LD8_M1, arg, arg1, arg2);
    }
}

971
static inline void tcg_out_st(TCGContext *s, TCGType type, TCGReg arg,
972
                              TCGReg arg1, intptr_t arg2)
A
Aurelien Jarno 已提交
973 974 975 976 977 978 979 980
{
    if (type == TCG_TYPE_I32) {
        tcg_out_st_rel(s, OPC_ST4_M4, arg, arg1, arg2);
    } else {
        tcg_out_st_rel(s, OPC_ST8_M4, arg, arg1, arg2);
    }
}

981 982 983
static inline void tcg_out_alu(TCGContext *s, uint64_t opc_a1, uint64_t opc_a3,
                               TCGReg ret, TCGArg arg1, int const_arg1,
                               TCGArg arg2, int const_arg2)
A
Aurelien Jarno 已提交
984
{
985
    uint64_t opc1 = 0, opc2 = 0, opc3 = 0;
A
Aurelien Jarno 已提交
986 987

    if (const_arg2 && arg2 != 0) {
988
        opc2 = tcg_opc_movi_a(TCG_REG_P0, TCG_REG_R3, arg2);
A
Aurelien Jarno 已提交
989 990
        arg2 = TCG_REG_R3;
    }
991 992 993 994
    if (const_arg1 && arg1 != 0) {
        if (opc_a3 && arg1 == (int8_t)arg1) {
            opc3 = tcg_opc_a3(TCG_REG_P0, opc_a3, ret, arg1, arg2);
        } else {
995
            opc1 = tcg_opc_movi_a(TCG_REG_P0, TCG_REG_R2, arg1);
996 997 998 999 1000 1001
            arg1 = TCG_REG_R2;
        }
    }
    if (opc3 == 0) {
        opc3 = tcg_opc_a1(TCG_REG_P0, opc_a1, ret, arg1, arg2);
    }
A
Aurelien Jarno 已提交
1002

1003 1004 1005
    tcg_out_bundle(s, (opc1 || opc2 ? mII : miI),
                   opc1 ? opc1 : INSN_NOP_M,
                   opc2 ? opc2 : INSN_NOP_I,
1006
                   opc3);
A
Aurelien Jarno 已提交
1007 1008
}

1009 1010 1011 1012 1013 1014 1015 1016 1017
static inline void tcg_out_add(TCGContext *s, TCGReg ret, TCGReg arg1,
                               TCGArg arg2, int const_arg2)
{
    if (const_arg2 && arg2 == sextract64(arg2, 0, 14)) {
        tcg_out_bundle(s, mmI,
                       INSN_NOP_M,
                       INSN_NOP_M,
                       tcg_opc_a4(TCG_REG_P0, OPC_ADDS_A4, ret, arg2, arg1));
    } else {
1018
        tcg_out_alu(s, OPC_ADD_A1, 0, ret, arg1, 0, arg2, const_arg2);
1019 1020 1021
    }
}

1022 1023 1024
static inline void tcg_out_sub(TCGContext *s, TCGReg ret, TCGArg arg1,
                               int const_arg1, TCGArg arg2, int const_arg2)
{
1025
    if (!const_arg1 && const_arg2 && -arg2 == sextract64(-arg2, 0, 14)) {
1026 1027 1028 1029 1030
        tcg_out_bundle(s, mmI,
                       INSN_NOP_M,
                       INSN_NOP_M,
                       tcg_opc_a4(TCG_REG_P0, OPC_ADDS_A4, ret, -arg2, arg1));
    } else {
1031 1032
        tcg_out_alu(s, OPC_SUB_A1, OPC_SUB_A3, ret,
                    arg1, const_arg1, arg2, const_arg2);
1033 1034 1035
    }
}

A
Aurelien Jarno 已提交
1036 1037 1038 1039 1040
static inline void tcg_out_eqv(TCGContext *s, TCGArg ret,
                               TCGArg arg1, int const_arg1,
                               TCGArg arg2, int const_arg2)
{
    tcg_out_bundle(s, mII,
1041
                   INSN_NOP_M,
A
Aurelien Jarno 已提交
1042 1043 1044 1045 1046 1047 1048 1049 1050
                   tcg_opc_a1 (TCG_REG_P0, OPC_XOR_A1, ret, arg1, arg2),
                   tcg_opc_a3 (TCG_REG_P0, OPC_ANDCM_A3, ret, -1, ret));
}

static inline void tcg_out_nand(TCGContext *s, TCGArg ret,
                                TCGArg arg1, int const_arg1,
                                TCGArg arg2, int const_arg2)
{
    tcg_out_bundle(s, mII,
1051
                   INSN_NOP_M,
A
Aurelien Jarno 已提交
1052 1053 1054 1055 1056 1057 1058 1059 1060
                   tcg_opc_a1 (TCG_REG_P0, OPC_AND_A1, ret, arg1, arg2),
                   tcg_opc_a3 (TCG_REG_P0, OPC_ANDCM_A3, ret, -1, ret));
}

static inline void tcg_out_nor(TCGContext *s, TCGArg ret,
                               TCGArg arg1, int const_arg1,
                               TCGArg arg2, int const_arg2)
{
    tcg_out_bundle(s, mII,
1061
                   INSN_NOP_M,
A
Aurelien Jarno 已提交
1062 1063 1064 1065 1066 1067 1068 1069 1070
                   tcg_opc_a1 (TCG_REG_P0, OPC_OR_A1, ret, arg1, arg2),
                   tcg_opc_a3 (TCG_REG_P0, OPC_ANDCM_A3, ret, -1, ret));
}

static inline void tcg_out_orc(TCGContext *s, TCGArg ret,
                               TCGArg arg1, int const_arg1,
                               TCGArg arg2, int const_arg2)
{
    tcg_out_bundle(s, mII,
1071
                   INSN_NOP_M,
A
Aurelien Jarno 已提交
1072 1073 1074 1075 1076 1077 1078 1079 1080 1081
                   tcg_opc_a3 (TCG_REG_P0, OPC_ANDCM_A3, TCG_REG_R2, -1, arg2),
                   tcg_opc_a1 (TCG_REG_P0, OPC_OR_A1, ret, arg1, TCG_REG_R2));
}

static inline void tcg_out_mul(TCGContext *s, TCGArg ret,
                               TCGArg arg1, TCGArg arg2)
{
    tcg_out_bundle(s, mmI,
                   tcg_opc_m18(TCG_REG_P0, OPC_SETF_SIG_M18, TCG_REG_F6, arg1),
                   tcg_opc_m18(TCG_REG_P0, OPC_SETF_SIG_M18, TCG_REG_F7, arg2),
1082
                   INSN_NOP_I);
A
Aurelien Jarno 已提交
1083
    tcg_out_bundle(s, mmF,
1084 1085
                   INSN_NOP_M,
                   INSN_NOP_M,
A
Aurelien Jarno 已提交
1086 1087 1088 1089
                   tcg_opc_f2 (TCG_REG_P0, OPC_XMA_L_F2, TCG_REG_F6, TCG_REG_F6,
                               TCG_REG_F7, TCG_REG_F0));
    tcg_out_bundle(s, miI,
                   tcg_opc_m19(TCG_REG_P0, OPC_GETF_SIG_M19, ret, TCG_REG_F6),
1090 1091
                   INSN_NOP_I,
                   INSN_NOP_I);
A
Aurelien Jarno 已提交
1092 1093 1094 1095 1096 1097 1098
}

static inline void tcg_out_sar_i32(TCGContext *s, TCGArg ret, TCGArg arg1,
                                   TCGArg arg2, int const_arg2)
{
    if (const_arg2) {
        tcg_out_bundle(s, miI,
1099 1100
                       INSN_NOP_M,
                       INSN_NOP_I,
A
Aurelien Jarno 已提交
1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117
                       tcg_opc_i11(TCG_REG_P0, OPC_EXTR_I11,
                                   ret, arg1, arg2, 31 - arg2));
    } else {
        tcg_out_bundle(s, mII,
                       tcg_opc_a3 (TCG_REG_P0, OPC_AND_A3,
                                   TCG_REG_R3, 0x1f, arg2),
                       tcg_opc_i29(TCG_REG_P0, OPC_SXT4_I29, TCG_REG_R2, arg1),
                       tcg_opc_i5 (TCG_REG_P0, OPC_SHR_I5, ret,
                                   TCG_REG_R2, TCG_REG_R3));
    }
}

static inline void tcg_out_sar_i64(TCGContext *s, TCGArg ret, TCGArg arg1,
                                   TCGArg arg2, int const_arg2)
{
    if (const_arg2) {
        tcg_out_bundle(s, miI,
1118 1119
                       INSN_NOP_M,
                       INSN_NOP_I,
A
Aurelien Jarno 已提交
1120 1121 1122 1123
                       tcg_opc_i11(TCG_REG_P0, OPC_EXTR_I11,
                                   ret, arg1, arg2, 63 - arg2));
    } else {
        tcg_out_bundle(s, miI,
1124 1125
                       INSN_NOP_M,
                       INSN_NOP_I,
A
Aurelien Jarno 已提交
1126 1127 1128 1129 1130 1131 1132 1133 1134
                       tcg_opc_i5 (TCG_REG_P0, OPC_SHR_I5, ret, arg1, arg2));
    }
}

static inline void tcg_out_shl_i32(TCGContext *s, TCGArg ret, TCGArg arg1,
                                   TCGArg arg2, int const_arg2)
{
    if (const_arg2) {
        tcg_out_bundle(s, miI,
1135 1136
                       INSN_NOP_M,
                       INSN_NOP_I,
A
Aurelien Jarno 已提交
1137 1138 1139 1140
                       tcg_opc_i12(TCG_REG_P0, OPC_DEP_Z_I12, ret,
                                   arg1, 63 - arg2, 31 - arg2));
    } else {
        tcg_out_bundle(s, mII,
1141
                       INSN_NOP_M,
A
Aurelien Jarno 已提交
1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153
                       tcg_opc_a3 (TCG_REG_P0, OPC_AND_A3, TCG_REG_R2,
                                   0x1f, arg2),
                       tcg_opc_i7 (TCG_REG_P0, OPC_SHL_I7, ret,
                                   arg1, TCG_REG_R2));
    }
}

static inline void tcg_out_shl_i64(TCGContext *s, TCGArg ret, TCGArg arg1,
                                   TCGArg arg2, int const_arg2)
{
    if (const_arg2) {
        tcg_out_bundle(s, miI,
1154 1155
                       INSN_NOP_M,
                       INSN_NOP_I,
A
Aurelien Jarno 已提交
1156 1157 1158 1159
                       tcg_opc_i12(TCG_REG_P0, OPC_DEP_Z_I12, ret,
                                   arg1, 63 - arg2, 63 - arg2));
    } else {
        tcg_out_bundle(s, miI,
1160 1161
                       INSN_NOP_M,
                       INSN_NOP_I,
A
Aurelien Jarno 已提交
1162 1163 1164 1165 1166 1167 1168 1169 1170 1171
                       tcg_opc_i7 (TCG_REG_P0, OPC_SHL_I7, ret,
                                   arg1, arg2));
    }
}

static inline void tcg_out_shr_i32(TCGContext *s, TCGArg ret, TCGArg arg1,
                                   TCGArg arg2, int const_arg2)
{
    if (const_arg2) {
        tcg_out_bundle(s, miI,
1172 1173
                       INSN_NOP_M,
                       INSN_NOP_I,
A
Aurelien Jarno 已提交
1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190
                       tcg_opc_i11(TCG_REG_P0, OPC_EXTR_U_I11, ret,
                                   arg1, arg2, 31 - arg2));
    } else {
        tcg_out_bundle(s, mII,
                       tcg_opc_a3 (TCG_REG_P0, OPC_AND_A3, TCG_REG_R3,
                                   0x1f, arg2),
                       tcg_opc_i29(TCG_REG_P0, OPC_ZXT4_I29, TCG_REG_R2, arg1),
                       tcg_opc_i5 (TCG_REG_P0, OPC_SHR_U_I5, ret,
                                   TCG_REG_R2, TCG_REG_R3));
    }
}

static inline void tcg_out_shr_i64(TCGContext *s, TCGArg ret, TCGArg arg1,
                                   TCGArg arg2, int const_arg2)
{
    if (const_arg2) {
        tcg_out_bundle(s, miI,
1191 1192
                       INSN_NOP_M,
                       INSN_NOP_I,
A
Aurelien Jarno 已提交
1193 1194 1195 1196
                       tcg_opc_i11(TCG_REG_P0, OPC_EXTR_U_I11, ret,
                                   arg1, arg2, 63 - arg2));
    } else {
        tcg_out_bundle(s, miI,
1197 1198
                       INSN_NOP_M,
                       INSN_NOP_I,
A
Aurelien Jarno 已提交
1199 1200 1201 1202 1203 1204 1205 1206 1207 1208
                       tcg_opc_i5 (TCG_REG_P0, OPC_SHR_U_I5, ret,
                                   arg1, arg2));
    }
}

static inline void tcg_out_rotl_i32(TCGContext *s, TCGArg ret, TCGArg arg1,
                                    TCGArg arg2, int const_arg2)
{
    if (const_arg2) {
        tcg_out_bundle(s, mII,
1209
                       INSN_NOP_M,
A
Aurelien Jarno 已提交
1210 1211 1212 1213 1214 1215
                       tcg_opc_i2 (TCG_REG_P0, OPC_UNPACK4_L_I2,
                                   TCG_REG_R2, arg1, arg1),
                       tcg_opc_i11(TCG_REG_P0, OPC_EXTR_U_I11, ret,
                                   TCG_REG_R2, 32 - arg2, 31));
    } else {
        tcg_out_bundle(s, miI,
1216
                       INSN_NOP_M,
A
Aurelien Jarno 已提交
1217 1218 1219 1220 1221
                       tcg_opc_i2 (TCG_REG_P0, OPC_UNPACK4_L_I2,
                                   TCG_REG_R2, arg1, arg1),
                       tcg_opc_a3 (TCG_REG_P0, OPC_AND_A3, TCG_REG_R3,
                                   0x1f, arg2));
        tcg_out_bundle(s, mII,
1222
                       INSN_NOP_M,
A
Aurelien Jarno 已提交
1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234
                       tcg_opc_a3 (TCG_REG_P0, OPC_SUB_A3, TCG_REG_R3,
                                   0x20, TCG_REG_R3),
                       tcg_opc_i5 (TCG_REG_P0, OPC_SHR_U_I5, ret,
                                   TCG_REG_R2, TCG_REG_R3));
    }
}

static inline void tcg_out_rotl_i64(TCGContext *s, TCGArg ret, TCGArg arg1,
                                    TCGArg arg2, int const_arg2)
{
    if (const_arg2) {
        tcg_out_bundle(s, miI,
1235 1236
                       INSN_NOP_M,
                       INSN_NOP_I,
A
Aurelien Jarno 已提交
1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247
                       tcg_opc_i10(TCG_REG_P0, OPC_SHRP_I10, ret, arg1,
                                   arg1, 0x40 - arg2));
    } else {
        tcg_out_bundle(s, mII,
                       tcg_opc_a3 (TCG_REG_P0, OPC_SUB_A3, TCG_REG_R2,
                                   0x40, arg2),
                       tcg_opc_i7 (TCG_REG_P0, OPC_SHL_I7, TCG_REG_R3,
                                   arg1, arg2),
                       tcg_opc_i5 (TCG_REG_P0, OPC_SHR_U_I5, TCG_REG_R2,
                                   arg1, TCG_REG_R2));
        tcg_out_bundle(s, miI,
1248 1249
                       INSN_NOP_M,
                       INSN_NOP_I,
A
Aurelien Jarno 已提交
1250 1251 1252 1253 1254 1255 1256 1257 1258 1259
                       tcg_opc_a1 (TCG_REG_P0, OPC_OR_A1, ret,
                                   TCG_REG_R2, TCG_REG_R3));
    }
}

static inline void tcg_out_rotr_i32(TCGContext *s, TCGArg ret, TCGArg arg1,
                                    TCGArg arg2, int const_arg2)
{
    if (const_arg2) {
        tcg_out_bundle(s, mII,
1260
                       INSN_NOP_M,
A
Aurelien Jarno 已提交
1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280
                       tcg_opc_i2 (TCG_REG_P0, OPC_UNPACK4_L_I2,
                                   TCG_REG_R2, arg1, arg1),
                       tcg_opc_i11(TCG_REG_P0, OPC_EXTR_U_I11, ret,
                                   TCG_REG_R2, arg2, 31));
    } else {
        tcg_out_bundle(s, mII,
                       tcg_opc_a3 (TCG_REG_P0, OPC_AND_A3, TCG_REG_R3,
                                   0x1f, arg2),
                       tcg_opc_i2 (TCG_REG_P0, OPC_UNPACK4_L_I2,
                                   TCG_REG_R2, arg1, arg1),
                       tcg_opc_i5 (TCG_REG_P0, OPC_SHR_U_I5, ret,
                                   TCG_REG_R2, TCG_REG_R3));
    }
}

static inline void tcg_out_rotr_i64(TCGContext *s, TCGArg ret, TCGArg arg1,
                                    TCGArg arg2, int const_arg2)
{
    if (const_arg2) {
        tcg_out_bundle(s, miI,
1281 1282
                       INSN_NOP_M,
                       INSN_NOP_I,
A
Aurelien Jarno 已提交
1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293
                       tcg_opc_i10(TCG_REG_P0, OPC_SHRP_I10, ret, arg1,
                                   arg1, arg2));
    } else {
        tcg_out_bundle(s, mII,
                       tcg_opc_a3 (TCG_REG_P0, OPC_SUB_A3, TCG_REG_R2,
                                   0x40, arg2),
                       tcg_opc_i5 (TCG_REG_P0, OPC_SHR_U_I5, TCG_REG_R3,
                                   arg1, arg2),
                       tcg_opc_i7 (TCG_REG_P0, OPC_SHL_I7, TCG_REG_R2,
                                   arg1, TCG_REG_R2));
        tcg_out_bundle(s, miI,
1294 1295
                       INSN_NOP_M,
                       INSN_NOP_I,
A
Aurelien Jarno 已提交
1296 1297 1298 1299 1300
                       tcg_opc_a1 (TCG_REG_P0, OPC_OR_A1, ret,
                                   TCG_REG_R2, TCG_REG_R3));
    }
}

1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314
static const uint64_t opc_ext_i29[8] = {
    OPC_ZXT1_I29, OPC_ZXT2_I29, OPC_ZXT4_I29, 0,
    OPC_SXT1_I29, OPC_SXT2_I29, OPC_SXT4_I29, 0
};

static inline uint64_t tcg_opc_ext_i(int qp, TCGMemOp opc, TCGReg d, TCGReg s)
{
    if ((opc & MO_SIZE) == MO_64) {
        return tcg_opc_mov_a(qp, d, s);
    } else {
        return tcg_opc_i29(qp, opc_ext_i29[opc & MO_SSIZE], d, s);
    }
}

A
Aurelien Jarno 已提交
1315 1316 1317 1318
static inline void tcg_out_ext(TCGContext *s, uint64_t opc_i29,
                               TCGArg ret, TCGArg arg)
{
    tcg_out_bundle(s, miI,
1319 1320
                   INSN_NOP_M,
                   INSN_NOP_I,
A
Aurelien Jarno 已提交
1321 1322 1323
                   tcg_opc_i29(TCG_REG_P0, opc_i29, ret, arg));
}

1324 1325 1326 1327 1328
static inline uint64_t tcg_opc_bswap64_i(int qp, TCGReg d, TCGReg s)
{
    return tcg_opc_i3(qp, OPC_MUX1_I3, d, s, 0xb);
}

A
Aurelien Jarno 已提交
1329 1330 1331
static inline void tcg_out_bswap16(TCGContext *s, TCGArg ret, TCGArg arg)
{
    tcg_out_bundle(s, mII,
1332
                   INSN_NOP_M,
A
Aurelien Jarno 已提交
1333
                   tcg_opc_i12(TCG_REG_P0, OPC_DEP_Z_I12, ret, arg, 15, 15),
1334
                   tcg_opc_bswap64_i(TCG_REG_P0, ret, ret));
A
Aurelien Jarno 已提交
1335 1336 1337 1338 1339
}

static inline void tcg_out_bswap32(TCGContext *s, TCGArg ret, TCGArg arg)
{
    tcg_out_bundle(s, mII,
1340
                   INSN_NOP_M,
A
Aurelien Jarno 已提交
1341
                   tcg_opc_i12(TCG_REG_P0, OPC_DEP_Z_I12, ret, arg, 31, 31),
1342
                   tcg_opc_bswap64_i(TCG_REG_P0, ret, ret));
A
Aurelien Jarno 已提交
1343 1344 1345 1346
}

static inline void tcg_out_bswap64(TCGContext *s, TCGArg ret, TCGArg arg)
{
1347
    tcg_out_bundle(s, miI,
1348 1349
                   INSN_NOP_M,
                   INSN_NOP_I,
1350
                   tcg_opc_bswap64_i(TCG_REG_P0, ret, arg));
A
Aurelien Jarno 已提交
1351 1352
}

R
Richard Henderson 已提交
1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369
static inline void tcg_out_deposit(TCGContext *s, TCGArg ret, TCGArg a1,
                                   TCGArg a2, int const_a2, int pos, int len)
{
    uint64_t i1 = 0, i2 = 0;
    int cpos = 63 - pos, lm1 = len - 1;

    if (const_a2) {
        /* Truncate the value of a constant a2 to the width of the field.  */
        int mask = (1u << len) - 1;
        a2 &= mask;

        if (a2 == 0 || a2 == mask) {
            /* 1-bit signed constant inserted into register.  */
            i2 = tcg_opc_i14(TCG_REG_P0, OPC_DEP_I14, ret, a2, a1, cpos, lm1);
        } else {
            /* Otherwise, load any constant into a temporary.  Do this into
               the first I slot to help out with cross-unit delays.  */
1370
            i1 = tcg_opc_movi_a(TCG_REG_P0, TCG_REG_R2, a2);
R
Richard Henderson 已提交
1371 1372 1373 1374 1375 1376 1377
            a2 = TCG_REG_R2;
        }
    }
    if (i2 == 0) {
        i2 = tcg_opc_i15(TCG_REG_P0, OPC_DEP_I15, ret, a2, a1, cpos, lm1);
    }
    tcg_out_bundle(s, (i1 ? mII : miI),
1378 1379
                   INSN_NOP_M,
                   i1 ? i1 : INSN_NOP_I,
R
Richard Henderson 已提交
1380 1381 1382
                   i2);
}

A
Aurelien Jarno 已提交
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
static inline uint64_t tcg_opc_cmp_a(int qp, TCGCond cond, TCGArg arg1,
                                     TCGArg arg2, int cmp4)
{
    uint64_t opc_eq_a6, opc_lt_a6, opc_ltu_a6;

    if (cmp4) {
        opc_eq_a6 = OPC_CMP4_EQ_A6;
        opc_lt_a6 = OPC_CMP4_LT_A6;
        opc_ltu_a6 = OPC_CMP4_LTU_A6;
    } else {
        opc_eq_a6 = OPC_CMP_EQ_A6;
        opc_lt_a6 = OPC_CMP_LT_A6;
        opc_ltu_a6 = OPC_CMP_LTU_A6;
    }

    switch (cond) {
    case TCG_COND_EQ:
        return tcg_opc_a6 (qp, opc_eq_a6,  TCG_REG_P6, TCG_REG_P7, arg1, arg2);
    case TCG_COND_NE:
        return tcg_opc_a6 (qp, opc_eq_a6,  TCG_REG_P7, TCG_REG_P6, arg1, arg2);
    case TCG_COND_LT:
        return tcg_opc_a6 (qp, opc_lt_a6,  TCG_REG_P6, TCG_REG_P7, arg1, arg2);
    case TCG_COND_LTU:
        return tcg_opc_a6 (qp, opc_ltu_a6, TCG_REG_P6, TCG_REG_P7, arg1, arg2);
    case TCG_COND_GE:
        return tcg_opc_a6 (qp, opc_lt_a6,  TCG_REG_P7, TCG_REG_P6, arg1, arg2);
    case TCG_COND_GEU:
        return tcg_opc_a6 (qp, opc_ltu_a6, TCG_REG_P7, TCG_REG_P6, arg1, arg2);
    case TCG_COND_LE:
        return tcg_opc_a6 (qp, opc_lt_a6,  TCG_REG_P7, TCG_REG_P6, arg2, arg1);
    case TCG_COND_LEU:
        return tcg_opc_a6 (qp, opc_ltu_a6, TCG_REG_P7, TCG_REG_P6, arg2, arg1);
    case TCG_COND_GT:
        return tcg_opc_a6 (qp, opc_lt_a6,  TCG_REG_P6, TCG_REG_P7, arg2, arg1);
    case TCG_COND_GTU:
        return tcg_opc_a6 (qp, opc_ltu_a6, TCG_REG_P6, TCG_REG_P7, arg2, arg1);
    default:
        tcg_abort();
        break;
    }
}

R
Richard Henderson 已提交
1425
static inline void tcg_out_brcond(TCGContext *s, TCGCond cond, TCGReg arg1,
1426
                                  TCGReg arg2, TCGLabel *l, int cmp4)
A
Aurelien Jarno 已提交
1427
{
1428
    uint64_t imm;
A
Aurelien Jarno 已提交
1429

1430 1431 1432
    /* We pay attention here to not modify the branch target by reading
       the existing value and using it again. This ensure that caches and
       memory are kept coherent during retranslation. */
A
Aurelien Jarno 已提交
1433
    if (l->has_value) {
1434
        imm = l->u.value_ptr - s->code_ptr;
A
Aurelien Jarno 已提交
1435
    } else {
1436
        imm = get_reloc_pcrel21b_slot2(s->code_ptr);
1437
        tcg_out_reloc(s, s->code_ptr, R_IA64_PCREL21B, l, 0);
A
Aurelien Jarno 已提交
1438
    }
1439 1440 1441 1442 1443

    tcg_out_bundle(s, miB,
                   INSN_NOP_M,
                   tcg_opc_cmp_a(TCG_REG_P0, cond, arg1, arg2, cmp4),
                   tcg_opc_b1(TCG_REG_P6, OPC_BR_DPTK_FEW_B1, imm));
A
Aurelien Jarno 已提交
1444 1445 1446 1447 1448 1449 1450
}

static inline void tcg_out_setcond(TCGContext *s, TCGCond cond, TCGArg ret,
                                   TCGArg arg1, TCGArg arg2, int cmp4)
{
    tcg_out_bundle(s, MmI,
                   tcg_opc_cmp_a(TCG_REG_P0, cond, arg1, arg2, cmp4),
1451 1452
                   tcg_opc_movi_a(TCG_REG_P6, ret, 1),
                   tcg_opc_movi_a(TCG_REG_P7, ret, 0));
A
Aurelien Jarno 已提交
1453 1454
}

1455 1456 1457 1458 1459 1460 1461 1462
static inline void tcg_out_movcond(TCGContext *s, TCGCond cond, TCGArg ret,
                                   TCGArg c1, TCGArg c2,
                                   TCGArg v1, int const_v1,
                                   TCGArg v2, int const_v2, int cmp4)
{
    uint64_t opc1, opc2;

    if (const_v1) {
1463
        opc1 = tcg_opc_movi_a(TCG_REG_P6, ret, v1);
1464
    } else if (ret == v1) {
1465
        opc1 = INSN_NOP_M;
1466
    } else {
1467
        opc1 = tcg_opc_mov_a(TCG_REG_P6, ret, v1);
1468 1469
    }
    if (const_v2) {
1470
        opc2 = tcg_opc_movi_a(TCG_REG_P7, ret, v2);
1471
    } else if (ret == v2) {
1472
        opc2 = INSN_NOP_I;
1473
    } else {
1474
        opc2 = tcg_opc_mov_a(TCG_REG_P7, ret, v2);
1475 1476 1477 1478 1479 1480 1481 1482
    }

    tcg_out_bundle(s, MmI,
                   tcg_opc_cmp_a(TCG_REG_P0, cond, c1, c2, cmp4),
                   opc1,
                   opc2);
}

A
Aurelien Jarno 已提交
1483
#if defined(CONFIG_SOFTMMU)
1484 1485 1486 1487
/* We're expecting to use an signed 22-bit immediate add.  */
QEMU_BUILD_BUG_ON(offsetof(CPUArchState, tlb_table[NB_MMU_MODES - 1][1])
                  > 0x1fffff)

A
Aurelien Jarno 已提交
1488
/* Load and compare a TLB entry, and return the result in (p6, p7).
1489
   R2 is loaded with the addend TLB entry.
1490
   R57 is loaded with the address, zero extented on 32-bit targets.
1491 1492
   R1, R3 are clobbered, leaving R56 free for...
   BSWAP_1, BSWAP_2 and I-slot insns for swapping data for store.  */
1493
static inline void tcg_out_qemu_tlb(TCGContext *s, TCGReg addr_reg,
1494 1495
                                    TCGMemOp s_bits, int off_rw, int off_add,
                                    uint64_t bswap1, uint64_t bswap2)
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
{
     /*
        .mii
        mov	r2 = off_rw
        extr.u	r3 = addr_reg, ...		# extract tlb page
        zxt4	r57 = addr_reg                  # or mov for 64-bit guest
        ;;
        .mii
        addl	r2 = r2, areg0
        shl	r3 = r3, cteb                   # via dep.z
        dep	r1 = 0, r57, ...                # zero page ofs, keep align
        ;;
        .mmi
        add	r2 = r2, r3
        ;;
        ld4	r3 = [r2], off_add-off_rw	# or ld8 for 64-bit guest
        nop
        ;;
        .mmi
        nop
        cmp.eq	p6, p7 = r3, r58
        nop
        ;;
      */
    tcg_out_bundle(s, miI,
                   tcg_opc_movi_a(TCG_REG_P0, TCG_REG_R2, off_rw),
                   tcg_opc_i11(TCG_REG_P0, OPC_EXTR_U_I11, TCG_REG_R3,
A
Aurelien Jarno 已提交
1523
                               addr_reg, TARGET_PAGE_BITS, CPU_TLB_BITS - 1),
1524 1525
                   tcg_opc_ext_i(TCG_REG_P0,
                                 TARGET_LONG_BITS == 32 ? MO_UL : MO_Q,
1526 1527
                                 TCG_REG_R57, addr_reg));
    tcg_out_bundle(s, miI,
A
Aurelien Jarno 已提交
1528
                   tcg_opc_a1 (TCG_REG_P0, OPC_ADD_A1, TCG_REG_R2,
1529 1530 1531 1532 1533 1534 1535 1536 1537 1538
                               TCG_REG_R2, TCG_AREG0),
                   tcg_opc_i12(TCG_REG_P0, OPC_DEP_Z_I12, TCG_REG_R3,
                               TCG_REG_R3, 63 - CPU_TLB_ENTRY_BITS,
                               63 - CPU_TLB_ENTRY_BITS),
                   tcg_opc_i14(TCG_REG_P0, OPC_DEP_I14, TCG_REG_R1, 0,
                               TCG_REG_R57, 63 - s_bits,
                               TARGET_PAGE_BITS - s_bits - 1));
    tcg_out_bundle(s, MmI,
                   tcg_opc_a1 (TCG_REG_P0, OPC_ADD_A1,
                               TCG_REG_R2, TCG_REG_R2, TCG_REG_R3),
1539 1540
                   tcg_opc_m3 (TCG_REG_P0,
                               (TARGET_LONG_BITS == 32
1541 1542
                                ? OPC_LD4_M3 : OPC_LD8_M3), TCG_REG_R3,
                               TCG_REG_R2, off_add - off_rw),
1543
                   bswap1);
1544
    tcg_out_bundle(s, mmI,
1545
                   tcg_opc_m1 (TCG_REG_P0, OPC_LD8_M1, TCG_REG_R2, TCG_REG_R2),
A
Aurelien Jarno 已提交
1546
                   tcg_opc_a6 (TCG_REG_P0, OPC_CMP_EQ_A6, TCG_REG_P6,
1547
                               TCG_REG_P7, TCG_REG_R1, TCG_REG_R3),
1548
                   bswap2);
A
Aurelien Jarno 已提交
1549 1550
}

1551 1552 1553
typedef struct TCGLabelQemuLdst {
    bool is_ld;
    TCGMemOp size;
1554
    tcg_insn_unit *label_ptr;     /* label pointers to be updated */
1555
    struct TCGLabelQemuLdst *next;
1556 1557 1558
} TCGLabelQemuLdst;

typedef struct TCGBackendData {
1559
    TCGLabelQemuLdst *labels;
1560 1561 1562 1563
} TCGBackendData;

static inline void tcg_out_tb_init(TCGContext *s)
{
1564
    s->be->labels = NULL;
1565 1566 1567
}

static void add_qemu_ldst_label(TCGContext *s, bool is_ld, TCGMemOp opc,
1568
                                tcg_insn_unit *label_ptr)
1569 1570
{
    TCGBackendData *be = s->be;
1571
    TCGLabelQemuLdst *l = tcg_malloc(sizeof(*l));
1572 1573 1574 1575

    l->is_ld = is_ld;
    l->size = opc & MO_SIZE;
    l->label_ptr = label_ptr;
1576 1577
    l->next = be->labels;
    be->labels = l;
1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591
}

static void tcg_out_tb_finalize(TCGContext *s)
{
    static const void * const helpers[8] = {
        helper_ret_stb_mmu,
        helper_le_stw_mmu,
        helper_le_stl_mmu,
        helper_le_stq_mmu,
        helper_ret_ldub_mmu,
        helper_le_lduw_mmu,
        helper_le_ldul_mmu,
        helper_le_ldq_mmu,
    };
1592
    tcg_insn_unit *thunks[8] = { };
1593
    TCGLabelQemuLdst *l;
1594

1595
    for (l = s->be->labels; l != NULL; l = l->next) {
1596
        long x = l->is_ld * 4 + l->size;
1597
        tcg_insn_unit *dest = thunks[x];
1598 1599 1600 1601 1602

        /* The out-of-line thunks are all the same; load the return address
           from B0, load the GP, and branch to the code.  Note that we are
           always post-call, so the register window has rolled, so we're
           using incomming parameter register numbers, not outgoing.  */
1603 1604 1605
        if (dest == NULL) {
            uintptr_t *desc = (uintptr_t *)helpers[x];
            uintptr_t func = desc[0], gp = desc[1], disp;
1606

1607
            thunks[x] = dest = s->code_ptr;
1608 1609 1610

            tcg_out_bundle(s, mlx,
                           INSN_NOP_M,
1611
                           tcg_opc_l2 (gp),
1612
                           tcg_opc_x2 (TCG_REG_P0, OPC_MOVL_X2,
1613
                                       TCG_REG_R1, gp));
1614 1615 1616 1617 1618 1619
            tcg_out_bundle(s, mii,
                           INSN_NOP_M,
                           INSN_NOP_I,
                           tcg_opc_i22(TCG_REG_P0, OPC_MOV_I22,
                                       l->is_ld ? TCG_REG_R35 : TCG_REG_R36,
                                       TCG_REG_B0));
1620
            disp = (tcg_insn_unit *)func - s->code_ptr;
1621 1622 1623 1624 1625 1626
            tcg_out_bundle(s, mLX,
                           INSN_NOP_M,
                           tcg_opc_l3 (disp),
                           tcg_opc_x3 (TCG_REG_P0, OPC_BRL_SPTK_MANY_X3, disp));
        }

1627
        reloc_pcrel21b_slot2(l->label_ptr, dest);
1628 1629
    }
}
A
Aurelien Jarno 已提交
1630

1631
static inline void tcg_out_qemu_ld(TCGContext *s, const TCGArg *args)
A
Aurelien Jarno 已提交
1632
{
1633 1634 1635 1636
    static const uint64_t opc_ld_m1[4] = {
        OPC_LD1_M1, OPC_LD2_M1, OPC_LD4_M1, OPC_LD8_M1
    };
    int addr_reg, data_reg, mem_index;
1637
    TCGMemOpIdx oi;
1638
    TCGMemOp opc, s_bits;
1639
    uint64_t fin1, fin2;
1640
    tcg_insn_unit *label_ptr;
A
Aurelien Jarno 已提交
1641

1642 1643
    data_reg = args[0];
    addr_reg = args[1];
1644 1645 1646
    oi = args[2];
    opc = get_memop(oi);
    mem_index = get_mmuidx(oi);
1647
    s_bits = opc & MO_SIZE;
A
Aurelien Jarno 已提交
1648 1649 1650

    /* Read the TLB entry */
    tcg_out_qemu_tlb(s, addr_reg, s_bits,
1651
                     offsetof(CPUArchState, tlb_table[mem_index][0].addr_read),
1652 1653
                     offsetof(CPUArchState, tlb_table[mem_index][0].addend),
                     INSN_NOP_I, INSN_NOP_I);
A
Aurelien Jarno 已提交
1654 1655

    /* P6 is the fast path, and P7 the slow path */
1656

1657
    fin2 = 0;
1658
    if (opc & MO_BSWAP) {
1659
        fin1 = tcg_opc_bswap64_i(TCG_REG_P0, data_reg, TCG_REG_R8);
1660 1661
        if (s_bits < MO_64) {
            int shift = 64 - (8 << s_bits);
1662 1663 1664
            fin2 = (opc & MO_SIGN ? OPC_EXTR_I11 : OPC_EXTR_U_I11);
            fin2 = tcg_opc_i11(TCG_REG_P0, fin2,
                               data_reg, data_reg, shift, 63 - shift);
1665
        }
1666 1667
    } else {
        fin1 = tcg_opc_ext_i(TCG_REG_P0, opc, data_reg, TCG_REG_R8);
1668 1669
    }

1670
    tcg_out_bundle(s, mmI,
1671
                   tcg_opc_mov_a(TCG_REG_P7, TCG_REG_R56, TCG_AREG0),
1672 1673
                   tcg_opc_a1 (TCG_REG_P6, OPC_ADD_A1, TCG_REG_R2,
                               TCG_REG_R2, TCG_REG_R57),
1674
                   tcg_opc_movi_a(TCG_REG_P7, TCG_REG_R58, oi));
1675
    label_ptr = s->code_ptr;
1676
    tcg_out_bundle(s, miB,
1677 1678
                   tcg_opc_m1 (TCG_REG_P6, opc_ld_m1[s_bits],
                               TCG_REG_R8, TCG_REG_R2),
1679 1680
                   INSN_NOP_I,
                   tcg_opc_b3 (TCG_REG_P7, OPC_BR_CALL_SPNT_FEW_B3, TCG_REG_B0,
1681
                               get_reloc_pcrel21b_slot2(label_ptr)));
1682 1683

    add_qemu_ldst_label(s, 1, opc, label_ptr);
1684 1685 1686 1687 1688 1689 1690

    /* Note that we always use LE helper functions, so the bswap insns
       here for the fast path also apply to the slow path.  */
    tcg_out_bundle(s, (fin2 ? mII : miI),
                   INSN_NOP_M,
                   fin1,
                   fin2 ? fin2 : INSN_NOP_I);
A
Aurelien Jarno 已提交
1691 1692
}

1693
static inline void tcg_out_qemu_st(TCGContext *s, const TCGArg *args)
A
Aurelien Jarno 已提交
1694
{
1695 1696 1697
    static const uint64_t opc_st_m4[4] = {
        OPC_ST1_M4, OPC_ST2_M4, OPC_ST4_M4, OPC_ST8_M4
    };
1698
    TCGReg addr_reg, data_reg;
1699
    int mem_index;
1700
    uint64_t pre1, pre2;
1701
    TCGMemOpIdx oi;
1702
    TCGMemOp opc, s_bits;
1703
    tcg_insn_unit *label_ptr;
A
Aurelien Jarno 已提交
1704

1705 1706
    data_reg = args[0];
    addr_reg = args[1];
1707 1708 1709
    oi = args[2];
    opc = get_memop(oi);
    mem_index = get_mmuidx(oi);
1710
    s_bits = opc & MO_SIZE;
A
Aurelien Jarno 已提交
1711

1712 1713 1714 1715
    /* Note that we always use LE helper functions, so the bswap insns
       that are here for the fast path also apply to the slow path,
       and move the data into the argument register.  */
    pre2 = INSN_NOP_I;
1716
    if (opc & MO_BSWAP) {
1717
        pre1 = tcg_opc_bswap64_i(TCG_REG_P0, TCG_REG_R58, data_reg);
1718 1719
        if (s_bits < MO_64) {
            int shift = 64 - (8 << s_bits);
1720 1721
            pre2 = tcg_opc_i11(TCG_REG_P0, OPC_EXTR_U_I11,
                               TCG_REG_R58, TCG_REG_R58, shift, 63 - shift);
1722
        }
1723 1724 1725
    } else {
        /* Just move the data into place for the slow path.  */
        pre1 = tcg_opc_ext_i(TCG_REG_P0, opc, TCG_REG_R58, data_reg);
1726 1727
    }

1728
    tcg_out_qemu_tlb(s, addr_reg, s_bits,
1729
                     offsetof(CPUArchState, tlb_table[mem_index][0].addr_write),
1730
                     offsetof(CPUArchState, tlb_table[mem_index][0].addend),
1731
                     pre1, pre2);
A
Aurelien Jarno 已提交
1732 1733

    /* P6 is the fast path, and P7 the slow path */
1734
    tcg_out_bundle(s, mmI,
1735
                   tcg_opc_mov_a(TCG_REG_P7, TCG_REG_R56, TCG_AREG0),
1736 1737
                   tcg_opc_a1 (TCG_REG_P6, OPC_ADD_A1, TCG_REG_R2,
                               TCG_REG_R2, TCG_REG_R57),
1738
                   tcg_opc_movi_a(TCG_REG_P7, TCG_REG_R59, oi));
1739
    label_ptr = s->code_ptr;
1740
    tcg_out_bundle(s, miB,
1741
                   tcg_opc_m4 (TCG_REG_P6, opc_st_m4[s_bits],
1742
                               TCG_REG_R58, TCG_REG_R2),
1743 1744
                   INSN_NOP_I,
                   tcg_opc_b3 (TCG_REG_P7, OPC_BR_CALL_SPNT_FEW_B3, TCG_REG_B0,
1745
                               get_reloc_pcrel21b_slot2(label_ptr)));
1746 1747

    add_qemu_ldst_label(s, 0, opc, label_ptr);
A
Aurelien Jarno 已提交
1748 1749 1750
}

#else /* !CONFIG_SOFTMMU */
1751
# include "tcg-be-null.h"
A
Aurelien Jarno 已提交
1752

1753
static inline void tcg_out_qemu_ld(TCGContext *s, const TCGArg *args)
A
Aurelien Jarno 已提交
1754
{
1755 1756 1757
    static uint64_t const opc_ld_m1[4] = {
        OPC_LD1_M1, OPC_LD2_M1, OPC_LD4_M1, OPC_LD8_M1
    };
1758
    int addr_reg, data_reg;
1759
    TCGMemOp opc, s_bits, bswap;
A
Aurelien Jarno 已提交
1760

1761 1762 1763
    data_reg = args[0];
    addr_reg = args[1];
    opc = args[2];
1764 1765
    s_bits = opc & MO_SIZE;
    bswap = opc & MO_BSWAP;
A
Aurelien Jarno 已提交
1766 1767

#if TARGET_LONG_BITS == 32
1768 1769
    if (GUEST_BASE != 0) {
        tcg_out_bundle(s, mII,
1770
                       INSN_NOP_M,
1771 1772 1773 1774 1775 1776
                       tcg_opc_i29(TCG_REG_P0, OPC_ZXT4_I29,
                                   TCG_REG_R3, addr_reg),
                       tcg_opc_a1 (TCG_REG_P0, OPC_ADD_A1, TCG_REG_R2,
                                   TCG_GUEST_BASE_REG, TCG_REG_R3));
    } else {
        tcg_out_bundle(s, miI,
1777
                       INSN_NOP_M,
1778 1779
                       tcg_opc_i29(TCG_REG_P0, OPC_ZXT4_I29,
                                   TCG_REG_R2, addr_reg),
1780
                       INSN_NOP_I);
1781
    }
A
Aurelien Jarno 已提交
1782

1783 1784
    if (!bswap) {
        if (!(opc & MO_SIGN)) {
A
Aurelien Jarno 已提交
1785 1786 1787
            tcg_out_bundle(s, miI,
                           tcg_opc_m1 (TCG_REG_P0, opc_ld_m1[s_bits],
                                       data_reg, TCG_REG_R2),
1788 1789
                           INSN_NOP_I,
                           INSN_NOP_I);
A
Aurelien Jarno 已提交
1790 1791 1792 1793
        } else {
            tcg_out_bundle(s, mII,
                           tcg_opc_m1 (TCG_REG_P0, opc_ld_m1[s_bits],
                                       data_reg, TCG_REG_R2),
1794
                           INSN_NOP_I,
1795
                           tcg_opc_ext_i(TCG_REG_P0, opc, data_reg, data_reg));
A
Aurelien Jarno 已提交
1796
        }
1797
    } else if (s_bits == MO_64) {
A
Aurelien Jarno 已提交
1798 1799 1800
            tcg_out_bundle(s, mII,
                           tcg_opc_m1 (TCG_REG_P0, opc_ld_m1[s_bits],
                                       data_reg, TCG_REG_R2),
1801
                           INSN_NOP_I,
1802
                           tcg_opc_bswap64_i(TCG_REG_P0, data_reg, data_reg));
A
Aurelien Jarno 已提交
1803
    } else {
1804
        if (s_bits == MO_16) {
A
Aurelien Jarno 已提交
1805 1806 1807
            tcg_out_bundle(s, mII,
                           tcg_opc_m1 (TCG_REG_P0, opc_ld_m1[s_bits],
                                       data_reg, TCG_REG_R2),
1808
                           INSN_NOP_I,
A
Aurelien Jarno 已提交
1809 1810 1811 1812 1813 1814
                           tcg_opc_i12(TCG_REG_P0, OPC_DEP_Z_I12,
                                      data_reg, data_reg, 15, 15));
        } else {
            tcg_out_bundle(s, mII,
                           tcg_opc_m1 (TCG_REG_P0, opc_ld_m1[s_bits],
                                       data_reg, TCG_REG_R2),
1815
                           INSN_NOP_I,
A
Aurelien Jarno 已提交
1816 1817 1818
                           tcg_opc_i12(TCG_REG_P0, OPC_DEP_Z_I12,
                                      data_reg, data_reg, 31, 31));
        }
1819
        if (!(opc & MO_SIGN)) {
A
Aurelien Jarno 已提交
1820
            tcg_out_bundle(s, miI,
1821 1822
                           INSN_NOP_M,
                           INSN_NOP_I,
1823
                           tcg_opc_bswap64_i(TCG_REG_P0, data_reg, data_reg));
A
Aurelien Jarno 已提交
1824 1825
        } else {
            tcg_out_bundle(s, mII,
1826
                           INSN_NOP_M,
1827
                           tcg_opc_bswap64_i(TCG_REG_P0, data_reg, data_reg),
1828
                           tcg_opc_ext_i(TCG_REG_P0, opc, data_reg, data_reg));
A
Aurelien Jarno 已提交
1829 1830 1831
        }
    }
#else
1832 1833 1834 1835 1836 1837
    if (GUEST_BASE != 0) {
        tcg_out_bundle(s, MmI,
                       tcg_opc_a1 (TCG_REG_P0, OPC_ADD_A1, TCG_REG_R2,
                                   TCG_GUEST_BASE_REG, addr_reg),
                       tcg_opc_m1 (TCG_REG_P0, opc_ld_m1[s_bits],
                                   data_reg, TCG_REG_R2),
1838
                       INSN_NOP_I);
1839 1840
    } else {
        tcg_out_bundle(s, mmI,
1841
                       INSN_NOP_M,
1842 1843
                       tcg_opc_m1 (TCG_REG_P0, opc_ld_m1[s_bits],
                                   data_reg, addr_reg),
1844
                       INSN_NOP_I);
1845
    }
A
Aurelien Jarno 已提交
1846

1847
    if (bswap && s_bits == MO_16) {
A
Aurelien Jarno 已提交
1848
        tcg_out_bundle(s, mII,
1849
                       INSN_NOP_M,
A
Aurelien Jarno 已提交
1850 1851
                       tcg_opc_i12(TCG_REG_P0, OPC_DEP_Z_I12,
                                   data_reg, data_reg, 15, 15),
1852
                       tcg_opc_bswap64_i(TCG_REG_P0, data_reg, data_reg));
1853
    } else if (bswap && s_bits == MO_32) {
A
Aurelien Jarno 已提交
1854
        tcg_out_bundle(s, mII,
1855
                       INSN_NOP_M,
A
Aurelien Jarno 已提交
1856 1857
                       tcg_opc_i12(TCG_REG_P0, OPC_DEP_Z_I12,
                                   data_reg, data_reg, 31, 31),
1858
                       tcg_opc_bswap64_i(TCG_REG_P0, data_reg, data_reg));
1859
    } else if (bswap && s_bits == MO_64) {
A
Aurelien Jarno 已提交
1860
        tcg_out_bundle(s, miI,
1861 1862
                       INSN_NOP_M,
                       INSN_NOP_I,
1863
                       tcg_opc_bswap64_i(TCG_REG_P0, data_reg, data_reg));
A
Aurelien Jarno 已提交
1864
    }
1865
    if (opc & MO_SIGN) {
A
Aurelien Jarno 已提交
1866
        tcg_out_bundle(s, miI,
1867 1868
                       INSN_NOP_M,
                       INSN_NOP_I,
1869
                       tcg_opc_ext_i(TCG_REG_P0, opc, data_reg, data_reg));
A
Aurelien Jarno 已提交
1870 1871 1872 1873
    }
#endif
}

1874
static inline void tcg_out_qemu_st(TCGContext *s, const TCGArg *args)
A
Aurelien Jarno 已提交
1875
{
1876 1877 1878
    static uint64_t const opc_st_m4[4] = {
        OPC_ST1_M4, OPC_ST2_M4, OPC_ST4_M4, OPC_ST8_M4
    };
1879
    int addr_reg, data_reg;
1880 1881 1882
#if TARGET_LONG_BITS == 64
    uint64_t add_guest_base;
#endif
1883
    TCGMemOp opc, s_bits, bswap;
A
Aurelien Jarno 已提交
1884

1885 1886 1887
    data_reg = args[0];
    addr_reg = args[1];
    opc = args[2];
1888 1889
    s_bits = opc & MO_SIZE;
    bswap = opc & MO_BSWAP;
A
Aurelien Jarno 已提交
1890 1891

#if TARGET_LONG_BITS == 32
1892 1893
    if (GUEST_BASE != 0) {
        tcg_out_bundle(s, mII,
1894
                       INSN_NOP_M,
1895 1896 1897 1898 1899 1900
                       tcg_opc_i29(TCG_REG_P0, OPC_ZXT4_I29,
                                   TCG_REG_R3, addr_reg),
                       tcg_opc_a1 (TCG_REG_P0, OPC_ADD_A1, TCG_REG_R2,
                                   TCG_GUEST_BASE_REG, TCG_REG_R3));
    } else {
        tcg_out_bundle(s, miI,
1901
                       INSN_NOP_M,
1902
                       tcg_opc_i29(TCG_REG_P0, OPC_ZXT4_I29,
1903
                                   TCG_REG_R2, addr_reg),
1904
                       INSN_NOP_I);
1905 1906
    }

A
Aurelien Jarno 已提交
1907
    if (bswap) {
1908
        if (s_bits == MO_16) {
A
Aurelien Jarno 已提交
1909
            tcg_out_bundle(s, mII,
1910
                           INSN_NOP_M,
A
Aurelien Jarno 已提交
1911 1912
                           tcg_opc_i12(TCG_REG_P0, OPC_DEP_Z_I12,
                                       TCG_REG_R3, data_reg, 15, 15),
1913 1914
                           tcg_opc_bswap64_i(TCG_REG_P0,
                                             TCG_REG_R3, TCG_REG_R3));
A
Aurelien Jarno 已提交
1915
            data_reg = TCG_REG_R3;
1916
        } else if (s_bits == MO_32) {
A
Aurelien Jarno 已提交
1917
            tcg_out_bundle(s, mII,
1918
                           INSN_NOP_M,
A
Aurelien Jarno 已提交
1919 1920
                           tcg_opc_i12(TCG_REG_P0, OPC_DEP_Z_I12,
                                       TCG_REG_R3, data_reg, 31, 31),
1921 1922
                           tcg_opc_bswap64_i(TCG_REG_P0,
                                             TCG_REG_R3, TCG_REG_R3));
A
Aurelien Jarno 已提交
1923
            data_reg = TCG_REG_R3;
1924
        } else if (s_bits == MO_64) {
A
Aurelien Jarno 已提交
1925
            tcg_out_bundle(s, miI,
1926 1927
                           INSN_NOP_M,
                           INSN_NOP_I,
1928
                           tcg_opc_bswap64_i(TCG_REG_P0, TCG_REG_R3, data_reg));
A
Aurelien Jarno 已提交
1929 1930 1931 1932
            data_reg = TCG_REG_R3;
        }
    }
    tcg_out_bundle(s, mmI,
1933
                   tcg_opc_m4 (TCG_REG_P0, opc_st_m4[s_bits],
A
Aurelien Jarno 已提交
1934
                               data_reg, TCG_REG_R2),
1935 1936
                   INSN_NOP_M,
                   INSN_NOP_I);
A
Aurelien Jarno 已提交
1937
#else
1938 1939 1940 1941 1942
    if (GUEST_BASE != 0) {
        add_guest_base = tcg_opc_a1 (TCG_REG_P0, OPC_ADD_A1, TCG_REG_R2,
                                     TCG_GUEST_BASE_REG, addr_reg);
        addr_reg = TCG_REG_R2;
    } else {
1943
        add_guest_base = INSN_NOP_M;
1944 1945
    }

1946
    if (!bswap) {
1947 1948
        tcg_out_bundle(s, (GUEST_BASE ? MmI : mmI),
                       add_guest_base,
1949
                       tcg_opc_m4 (TCG_REG_P0, opc_st_m4[s_bits],
1950
                                   data_reg, addr_reg),
1951
                       INSN_NOP_I);
A
Aurelien Jarno 已提交
1952
    } else {
1953
        if (s_bits == MO_16) {
A
Aurelien Jarno 已提交
1954
            tcg_out_bundle(s, mII,
1955
                           add_guest_base,
A
Aurelien Jarno 已提交
1956 1957
                           tcg_opc_i12(TCG_REG_P0, OPC_DEP_Z_I12,
                                       TCG_REG_R3, data_reg, 15, 15),
1958 1959
                           tcg_opc_bswap64_i(TCG_REG_P0,
                                             TCG_REG_R3, TCG_REG_R3));
A
Aurelien Jarno 已提交
1960
            data_reg = TCG_REG_R3;
1961
        } else if (s_bits == MO_32) {
A
Aurelien Jarno 已提交
1962
            tcg_out_bundle(s, mII,
1963
                           add_guest_base,
A
Aurelien Jarno 已提交
1964 1965
                           tcg_opc_i12(TCG_REG_P0, OPC_DEP_Z_I12,
                                       TCG_REG_R3, data_reg, 31, 31),
1966 1967
                           tcg_opc_bswap64_i(TCG_REG_P0,
                                             TCG_REG_R3, TCG_REG_R3));
A
Aurelien Jarno 已提交
1968
            data_reg = TCG_REG_R3;
1969
        } else if (s_bits == MO_64) {
A
Aurelien Jarno 已提交
1970
            tcg_out_bundle(s, miI,
1971
                           add_guest_base,
1972
                           INSN_NOP_I,
1973
                           tcg_opc_bswap64_i(TCG_REG_P0, TCG_REG_R3, data_reg));
A
Aurelien Jarno 已提交
1974 1975 1976
            data_reg = TCG_REG_R3;
        }
        tcg_out_bundle(s, miI,
1977
                       tcg_opc_m4 (TCG_REG_P0, opc_st_m4[s_bits],
1978
                                   data_reg, addr_reg),
1979 1980
                       INSN_NOP_I,
                       INSN_NOP_I);
A
Aurelien Jarno 已提交
1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994
    }
#endif
}

#endif

static inline void tcg_out_op(TCGContext *s, TCGOpcode opc,
                              const TCGArg *args, const int *const_args)
{
    switch(opc) {
    case INDEX_op_exit_tb:
        tcg_out_exit_tb(s, args[0]);
        break;
    case INDEX_op_br:
1995
        tcg_out_br(s, arg_label(args[0]));
A
Aurelien Jarno 已提交
1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047
        break;
    case INDEX_op_goto_tb:
        tcg_out_goto_tb(s, args[0]);
        break;

    case INDEX_op_ld8u_i32:
    case INDEX_op_ld8u_i64:
        tcg_out_ld_rel(s, OPC_LD1_M1, args[0], args[1], args[2]);
        break;
    case INDEX_op_ld8s_i32:
    case INDEX_op_ld8s_i64:
        tcg_out_ld_rel(s, OPC_LD1_M1, args[0], args[1], args[2]);
        tcg_out_ext(s, OPC_SXT1_I29, args[0], args[0]);
        break;
    case INDEX_op_ld16u_i32:
    case INDEX_op_ld16u_i64:
        tcg_out_ld_rel(s, OPC_LD2_M1, args[0], args[1], args[2]);
        break;
    case INDEX_op_ld16s_i32:
    case INDEX_op_ld16s_i64:
        tcg_out_ld_rel(s, OPC_LD2_M1, args[0], args[1], args[2]);
        tcg_out_ext(s, OPC_SXT2_I29, args[0], args[0]);
        break;
    case INDEX_op_ld_i32:
    case INDEX_op_ld32u_i64:
        tcg_out_ld_rel(s, OPC_LD4_M1, args[0], args[1], args[2]);
        break;
    case INDEX_op_ld32s_i64:
        tcg_out_ld_rel(s, OPC_LD4_M1, args[0], args[1], args[2]);
        tcg_out_ext(s, OPC_SXT4_I29, args[0], args[0]);
        break;
    case INDEX_op_ld_i64:
        tcg_out_ld_rel(s, OPC_LD8_M1, args[0], args[1], args[2]);
        break;
    case INDEX_op_st8_i32:
    case INDEX_op_st8_i64:
        tcg_out_st_rel(s, OPC_ST1_M4, args[0], args[1], args[2]);
        break;
    case INDEX_op_st16_i32:
    case INDEX_op_st16_i64:
        tcg_out_st_rel(s, OPC_ST2_M4, args[0], args[1], args[2]);
        break;
    case INDEX_op_st_i32:
    case INDEX_op_st32_i64:
        tcg_out_st_rel(s, OPC_ST4_M4, args[0], args[1], args[2]);
        break;
    case INDEX_op_st_i64:
        tcg_out_st_rel(s, OPC_ST8_M4, args[0], args[1], args[2]);
        break;

    case INDEX_op_add_i32:
    case INDEX_op_add_i64:
2048
        tcg_out_add(s, args[0], args[1], args[2], const_args[2]);
A
Aurelien Jarno 已提交
2049 2050 2051
        break;
    case INDEX_op_sub_i32:
    case INDEX_op_sub_i64:
2052
        tcg_out_sub(s, args[0], args[1], const_args[1], args[2], const_args[2]);
A
Aurelien Jarno 已提交
2053 2054 2055 2056
        break;

    case INDEX_op_and_i32:
    case INDEX_op_and_i64:
2057 2058 2059
        /* TCG expects arg2 constant; A3 expects arg1 constant.  Swap.  */
        tcg_out_alu(s, OPC_AND_A1, OPC_AND_A3, args[0],
                    args[2], const_args[2], args[1], const_args[1]);
A
Aurelien Jarno 已提交
2060 2061 2062
        break;
    case INDEX_op_andc_i32:
    case INDEX_op_andc_i64:
2063 2064
        tcg_out_alu(s, OPC_ANDCM_A1, OPC_ANDCM_A3, args[0],
                    args[1], const_args[1], args[2], const_args[2]);
A
Aurelien Jarno 已提交
2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082
        break;
    case INDEX_op_eqv_i32:
    case INDEX_op_eqv_i64:
        tcg_out_eqv(s, args[0], args[1], const_args[1],
                    args[2], const_args[2]);
        break;
    case INDEX_op_nand_i32:
    case INDEX_op_nand_i64:
        tcg_out_nand(s, args[0], args[1], const_args[1],
                     args[2], const_args[2]);
        break;
    case INDEX_op_nor_i32:
    case INDEX_op_nor_i64:
        tcg_out_nor(s, args[0], args[1], const_args[1],
                    args[2], const_args[2]);
        break;
    case INDEX_op_or_i32:
    case INDEX_op_or_i64:
2083 2084 2085
        /* TCG expects arg2 constant; A3 expects arg1 constant.  Swap.  */
        tcg_out_alu(s, OPC_OR_A1, OPC_OR_A3, args[0],
                    args[2], const_args[2], args[1], const_args[1]);
A
Aurelien Jarno 已提交
2086 2087 2088 2089 2090 2091 2092 2093
        break;
    case INDEX_op_orc_i32:
    case INDEX_op_orc_i64:
        tcg_out_orc(s, args[0], args[1], const_args[1],
                    args[2], const_args[2]);
        break;
    case INDEX_op_xor_i32:
    case INDEX_op_xor_i64:
2094 2095 2096
        /* TCG expects arg2 constant; A3 expects arg1 constant.  Swap.  */
        tcg_out_alu(s, OPC_XOR_A1, OPC_XOR_A3, args[0],
                    args[2], const_args[2], args[1], const_args[1]);
A
Aurelien Jarno 已提交
2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169
        break;

    case INDEX_op_mul_i32:
    case INDEX_op_mul_i64:
        tcg_out_mul(s, args[0], args[1], args[2]);
        break;

    case INDEX_op_sar_i32:
        tcg_out_sar_i32(s, args[0], args[1], args[2], const_args[2]);
        break;
    case INDEX_op_sar_i64:
        tcg_out_sar_i64(s, args[0], args[1], args[2], const_args[2]);
        break;
    case INDEX_op_shl_i32:
        tcg_out_shl_i32(s, args[0], args[1], args[2], const_args[2]);
        break;
    case INDEX_op_shl_i64:
        tcg_out_shl_i64(s, args[0], args[1], args[2], const_args[2]);
        break;
    case INDEX_op_shr_i32:
        tcg_out_shr_i32(s, args[0], args[1], args[2], const_args[2]);
        break;
    case INDEX_op_shr_i64:
        tcg_out_shr_i64(s, args[0], args[1], args[2], const_args[2]);
        break;
    case INDEX_op_rotl_i32:
        tcg_out_rotl_i32(s, args[0], args[1], args[2], const_args[2]);
        break;
    case INDEX_op_rotl_i64:
        tcg_out_rotl_i64(s, args[0], args[1], args[2], const_args[2]);
        break;
    case INDEX_op_rotr_i32:
        tcg_out_rotr_i32(s, args[0], args[1], args[2], const_args[2]);
        break;
    case INDEX_op_rotr_i64:
        tcg_out_rotr_i64(s, args[0], args[1], args[2], const_args[2]);
        break;

    case INDEX_op_ext8s_i32:
    case INDEX_op_ext8s_i64:
        tcg_out_ext(s, OPC_SXT1_I29, args[0], args[1]);
        break;
    case INDEX_op_ext8u_i32:
    case INDEX_op_ext8u_i64:
        tcg_out_ext(s, OPC_ZXT1_I29, args[0], args[1]);
        break;
    case INDEX_op_ext16s_i32:
    case INDEX_op_ext16s_i64:
        tcg_out_ext(s, OPC_SXT2_I29, args[0], args[1]);
        break;
    case INDEX_op_ext16u_i32:
    case INDEX_op_ext16u_i64:
        tcg_out_ext(s, OPC_ZXT2_I29, args[0], args[1]);
        break;
    case INDEX_op_ext32s_i64:
        tcg_out_ext(s, OPC_SXT4_I29, args[0], args[1]);
        break;
    case INDEX_op_ext32u_i64:
        tcg_out_ext(s, OPC_ZXT4_I29, args[0], args[1]);
        break;

    case INDEX_op_bswap16_i32:
    case INDEX_op_bswap16_i64:
        tcg_out_bswap16(s, args[0], args[1]);
        break;
    case INDEX_op_bswap32_i32:
    case INDEX_op_bswap32_i64:
        tcg_out_bswap32(s, args[0], args[1]);
        break;
    case INDEX_op_bswap64_i64:
        tcg_out_bswap64(s, args[0], args[1]);
        break;

R
Richard Henderson 已提交
2170 2171 2172 2173 2174 2175
    case INDEX_op_deposit_i32:
    case INDEX_op_deposit_i64:
        tcg_out_deposit(s, args[0], args[1], args[2], const_args[2],
                        args[3], args[4]);
        break;

A
Aurelien Jarno 已提交
2176
    case INDEX_op_brcond_i32:
2177
        tcg_out_brcond(s, args[2], args[0], args[1], arg_label(args[3]), 1);
A
Aurelien Jarno 已提交
2178 2179
        break;
    case INDEX_op_brcond_i64:
2180
        tcg_out_brcond(s, args[2], args[0], args[1], arg_label(args[3]), 0);
A
Aurelien Jarno 已提交
2181 2182 2183 2184 2185 2186 2187
        break;
    case INDEX_op_setcond_i32:
        tcg_out_setcond(s, args[3], args[0], args[1], args[2], 1);
        break;
    case INDEX_op_setcond_i64:
        tcg_out_setcond(s, args[3], args[0], args[1], args[2], 0);
        break;
2188 2189 2190 2191 2192 2193 2194 2195
    case INDEX_op_movcond_i32:
        tcg_out_movcond(s, args[5], args[0], args[1], args[2],
                        args[3], const_args[3], args[4], const_args[4], 1);
        break;
    case INDEX_op_movcond_i64:
        tcg_out_movcond(s, args[5], args[0], args[1], args[2],
                        args[3], const_args[3], args[4], const_args[4], 0);
        break;
A
Aurelien Jarno 已提交
2196

2197 2198
    case INDEX_op_qemu_ld_i32:
        tcg_out_qemu_ld(s, args);
A
Aurelien Jarno 已提交
2199
        break;
2200 2201
    case INDEX_op_qemu_ld_i64:
        tcg_out_qemu_ld(s, args);
A
Aurelien Jarno 已提交
2202
        break;
2203 2204
    case INDEX_op_qemu_st_i32:
        tcg_out_qemu_st(s, args);
A
Aurelien Jarno 已提交
2205
        break;
2206 2207
    case INDEX_op_qemu_st_i64:
        tcg_out_qemu_st(s, args);
A
Aurelien Jarno 已提交
2208 2209
        break;

2210 2211 2212 2213 2214
    case INDEX_op_mov_i32:  /* Always emitted via tcg_out_mov.  */
    case INDEX_op_mov_i64:
    case INDEX_op_movi_i32: /* Always emitted via tcg_out_movi.  */
    case INDEX_op_movi_i64:
    case INDEX_op_call:     /* Always emitted via tcg_out_call.  */
A
Aurelien Jarno 已提交
2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233
    default:
        tcg_abort();
    }
}

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

    { 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, { "rZ", "r" } },
    { INDEX_op_st16_i32, { "rZ", "r" } },
    { INDEX_op_st_i32, { "rZ", "r" } },

2234
    { INDEX_op_add_i32, { "r", "rZ", "rI" } },
A
Aurelien Jarno 已提交
2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261
    { INDEX_op_sub_i32, { "r", "rI", "rI" } },

    { INDEX_op_and_i32, { "r", "rI", "rI" } },
    { INDEX_op_andc_i32, { "r", "rI", "rI" } },
    { INDEX_op_eqv_i32, { "r", "rZ", "rZ" } },
    { INDEX_op_nand_i32, { "r", "rZ", "rZ" } },
    { INDEX_op_nor_i32, { "r", "rZ", "rZ" } },
    { INDEX_op_or_i32, { "r", "rI", "rI" } },
    { INDEX_op_orc_i32, { "r", "rZ", "rZ" } },
    { INDEX_op_xor_i32, { "r", "rI", "rI" } },

    { INDEX_op_mul_i32, { "r", "rZ", "rZ" } },

    { INDEX_op_sar_i32, { "r", "rZ", "ri" } },
    { INDEX_op_shl_i32, { "r", "rZ", "ri" } },
    { INDEX_op_shr_i32, { "r", "rZ", "ri" } },
    { INDEX_op_rotl_i32, { "r", "rZ", "ri" } },
    { INDEX_op_rotr_i32, { "r", "rZ", "ri" } },

    { INDEX_op_ext8s_i32, { "r", "rZ"} },
    { INDEX_op_ext8u_i32, { "r", "rZ"} },
    { INDEX_op_ext16s_i32, { "r", "rZ"} },
    { INDEX_op_ext16u_i32, { "r", "rZ"} },

    { INDEX_op_bswap16_i32, { "r", "rZ" } },
    { INDEX_op_bswap32_i32, { "r", "rZ" } },

R
Richard Henderson 已提交
2262
    { INDEX_op_brcond_i32, { "rZ", "rZ" } },
A
Aurelien Jarno 已提交
2263
    { INDEX_op_setcond_i32, { "r", "rZ", "rZ" } },
2264
    { INDEX_op_movcond_i32, { "r", "rZ", "rZ", "rI", "rI" } },
A
Aurelien Jarno 已提交
2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277

    { INDEX_op_ld8u_i64, { "r", "r" } },
    { INDEX_op_ld8s_i64, { "r", "r" } },
    { INDEX_op_ld16u_i64, { "r", "r" } },
    { INDEX_op_ld16s_i64, { "r", "r" } },
    { INDEX_op_ld32u_i64, { "r", "r" } },
    { INDEX_op_ld32s_i64, { "r", "r" } },
    { INDEX_op_ld_i64, { "r", "r" } },
    { INDEX_op_st8_i64, { "rZ", "r" } },
    { INDEX_op_st16_i64, { "rZ", "r" } },
    { INDEX_op_st32_i64, { "rZ", "r" } },
    { INDEX_op_st_i64, { "rZ", "r" } },

2278
    { INDEX_op_add_i64, { "r", "rZ", "rI" } },
A
Aurelien Jarno 已提交
2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308
    { INDEX_op_sub_i64, { "r", "rI", "rI" } },

    { INDEX_op_and_i64, { "r", "rI", "rI" } },
    { INDEX_op_andc_i64, { "r", "rI", "rI" } },
    { INDEX_op_eqv_i64, { "r", "rZ", "rZ" } },
    { INDEX_op_nand_i64, { "r", "rZ", "rZ" } },
    { INDEX_op_nor_i64, { "r", "rZ", "rZ" } },
    { INDEX_op_or_i64, { "r", "rI", "rI" } },
    { INDEX_op_orc_i64, { "r", "rZ", "rZ" } },
    { INDEX_op_xor_i64, { "r", "rI", "rI" } },

    { INDEX_op_mul_i64, { "r", "rZ", "rZ" } },

    { INDEX_op_sar_i64, { "r", "rZ", "ri" } },
    { INDEX_op_shl_i64, { "r", "rZ", "ri" } },
    { INDEX_op_shr_i64, { "r", "rZ", "ri" } },
    { INDEX_op_rotl_i64, { "r", "rZ", "ri" } },
    { INDEX_op_rotr_i64, { "r", "rZ", "ri" } },

    { INDEX_op_ext8s_i64, { "r", "rZ"} },
    { INDEX_op_ext8u_i64, { "r", "rZ"} },
    { INDEX_op_ext16s_i64, { "r", "rZ"} },
    { INDEX_op_ext16u_i64, { "r", "rZ"} },
    { INDEX_op_ext32s_i64, { "r", "rZ"} },
    { INDEX_op_ext32u_i64, { "r", "rZ"} },

    { INDEX_op_bswap16_i64, { "r", "rZ" } },
    { INDEX_op_bswap32_i64, { "r", "rZ" } },
    { INDEX_op_bswap64_i64, { "r", "rZ" } },

R
Richard Henderson 已提交
2309
    { INDEX_op_brcond_i64, { "rZ", "rZ" } },
A
Aurelien Jarno 已提交
2310
    { INDEX_op_setcond_i64, { "r", "rZ", "rZ" } },
2311
    { INDEX_op_movcond_i64, { "r", "rZ", "rZ", "rI", "rI" } },
A
Aurelien Jarno 已提交
2312

R
Richard Henderson 已提交
2313 2314 2315
    { INDEX_op_deposit_i32, { "r", "rZ", "ri" } },
    { INDEX_op_deposit_i64, { "r", "rZ", "ri" } },

2316 2317 2318 2319
    { INDEX_op_qemu_ld_i32, { "r", "r" } },
    { INDEX_op_qemu_ld_i64, { "r", "r" } },
    { INDEX_op_qemu_st_i32, { "SZ", "r" } },
    { INDEX_op_qemu_st_i64, { "SZ", "r" } },
A
Aurelien Jarno 已提交
2320 2321 2322 2323 2324

    { -1 },
};

/* Generate global QEMU prologue and epilogue code */
2325
static void tcg_target_qemu_prologue(TCGContext *s)
A
Aurelien Jarno 已提交
2326 2327 2328 2329
{
    int frame_size;

    /* reserve some stack space */
B
Blue Swirl 已提交
2330 2331
    frame_size = TCG_STATIC_CALL_ARGS_SIZE +
                 CPU_TEMP_BUF_NLONGS * sizeof(long);
A
Aurelien Jarno 已提交
2332 2333
    frame_size = (frame_size + TCG_TARGET_STACK_ALIGN - 1) &
                 ~(TCG_TARGET_STACK_ALIGN - 1);
B
Blue Swirl 已提交
2334 2335
    tcg_set_frame(s, TCG_REG_CALL_STACK, TCG_STATIC_CALL_ARGS_SIZE,
                  CPU_TEMP_BUF_NLONGS * sizeof(long));
A
Aurelien Jarno 已提交
2336 2337

    /* First emit adhoc function descriptor */
2338 2339 2340 2341 2342
    *s->code_ptr = (tcg_insn_unit){
        (uint64_t)(s->code_ptr + 1), /* entry point */
        0                            /* skip gp */
    };
    s->code_ptr++;
A
Aurelien Jarno 已提交
2343 2344

    /* prologue */
A
Aurelien Jarno 已提交
2345
    tcg_out_bundle(s, miI,
A
Aurelien Jarno 已提交
2346
                   tcg_opc_m34(TCG_REG_P0, OPC_ALLOC_M34,
A
Aurelien Jarno 已提交
2347
                               TCG_REG_R34, 32, 24, 0),
R
Richard Henderson 已提交
2348
                   INSN_NOP_I,
A
Aurelien Jarno 已提交
2349
                   tcg_opc_i21(TCG_REG_P0, OPC_MOV_I21,
A
Aurelien Jarno 已提交
2350
                               TCG_REG_B6, TCG_REG_R33, 0));
2351 2352 2353 2354 2355

    /* ??? If GUEST_BASE < 0x200000, we could load the register via
       an ADDL in the M slot of the next bundle.  */
    if (GUEST_BASE != 0) {
        tcg_out_bundle(s, mlx,
2356
                       INSN_NOP_M,
2357 2358 2359 2360 2361 2362
                       tcg_opc_l2 (GUEST_BASE),
                       tcg_opc_x2 (TCG_REG_P0, OPC_MOVL_X2,
                                   TCG_GUEST_BASE_REG, GUEST_BASE));
        tcg_regset_set_reg(s->reserved_regs, TCG_GUEST_BASE_REG);
    }

A
Aurelien Jarno 已提交
2363 2364 2365
    tcg_out_bundle(s, miB,
                   tcg_opc_a4 (TCG_REG_P0, OPC_ADDS_A4,
                               TCG_REG_R12, -frame_size, TCG_REG_R12),
A
Aurelien Jarno 已提交
2366
                   tcg_opc_i22(TCG_REG_P0, OPC_MOV_I22,
R
Richard Henderson 已提交
2367
                               TCG_REG_R33, TCG_REG_B0),
A
Aurelien Jarno 已提交
2368 2369 2370 2371 2372
                   tcg_opc_b4 (TCG_REG_P0, OPC_BR_SPTK_MANY_B4, TCG_REG_B6));

    /* epilogue */
    tb_ret_addr = s->code_ptr;
    tcg_out_bundle(s, miI,
2373
                   INSN_NOP_M,
A
Aurelien Jarno 已提交
2374
                   tcg_opc_i21(TCG_REG_P0, OPC_MOV_I21,
R
Richard Henderson 已提交
2375
                               TCG_REG_B0, TCG_REG_R33, 0),
A
Aurelien Jarno 已提交
2376 2377 2378
                   tcg_opc_a4 (TCG_REG_P0, OPC_ADDS_A4,
                               TCG_REG_R12, frame_size, TCG_REG_R12));
    tcg_out_bundle(s, miB,
2379
                   INSN_NOP_M,
A
Aurelien Jarno 已提交
2380
                   tcg_opc_i26(TCG_REG_P0, OPC_MOV_I_I26,
A
Aurelien Jarno 已提交
2381
                               TCG_REG_PFS, TCG_REG_R34),
A
Aurelien Jarno 已提交
2382 2383 2384 2385
                   tcg_opc_b4 (TCG_REG_P0, OPC_BR_RET_SPTK_MANY_B4,
                               TCG_REG_B0));
}

2386
static void tcg_target_init(TCGContext *s)
A
Aurelien Jarno 已提交
2387 2388 2389 2390 2391 2392
{
    tcg_regset_set(tcg_target_available_regs[TCG_TYPE_I32],
                   0xffffffffffffffffull);
    tcg_regset_set(tcg_target_available_regs[TCG_TYPE_I64],
                   0xffffffffffffffffull);

2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425
    tcg_regset_clear(tcg_target_call_clobber_regs);
    tcg_regset_set_reg(tcg_target_call_clobber_regs, TCG_REG_R8);
    tcg_regset_set_reg(tcg_target_call_clobber_regs, TCG_REG_R9);
    tcg_regset_set_reg(tcg_target_call_clobber_regs, TCG_REG_R10);
    tcg_regset_set_reg(tcg_target_call_clobber_regs, TCG_REG_R11);
    tcg_regset_set_reg(tcg_target_call_clobber_regs, TCG_REG_R14);
    tcg_regset_set_reg(tcg_target_call_clobber_regs, TCG_REG_R15);
    tcg_regset_set_reg(tcg_target_call_clobber_regs, TCG_REG_R16);
    tcg_regset_set_reg(tcg_target_call_clobber_regs, TCG_REG_R17);
    tcg_regset_set_reg(tcg_target_call_clobber_regs, TCG_REG_R18);
    tcg_regset_set_reg(tcg_target_call_clobber_regs, TCG_REG_R19);
    tcg_regset_set_reg(tcg_target_call_clobber_regs, TCG_REG_R20);
    tcg_regset_set_reg(tcg_target_call_clobber_regs, TCG_REG_R21);
    tcg_regset_set_reg(tcg_target_call_clobber_regs, TCG_REG_R22);
    tcg_regset_set_reg(tcg_target_call_clobber_regs, TCG_REG_R23);
    tcg_regset_set_reg(tcg_target_call_clobber_regs, TCG_REG_R24);
    tcg_regset_set_reg(tcg_target_call_clobber_regs, TCG_REG_R25);
    tcg_regset_set_reg(tcg_target_call_clobber_regs, TCG_REG_R26);
    tcg_regset_set_reg(tcg_target_call_clobber_regs, TCG_REG_R27);
    tcg_regset_set_reg(tcg_target_call_clobber_regs, TCG_REG_R28);
    tcg_regset_set_reg(tcg_target_call_clobber_regs, TCG_REG_R29);
    tcg_regset_set_reg(tcg_target_call_clobber_regs, TCG_REG_R30);
    tcg_regset_set_reg(tcg_target_call_clobber_regs, TCG_REG_R31);
    tcg_regset_set_reg(tcg_target_call_clobber_regs, TCG_REG_R56);
    tcg_regset_set_reg(tcg_target_call_clobber_regs, TCG_REG_R57);
    tcg_regset_set_reg(tcg_target_call_clobber_regs, TCG_REG_R58);
    tcg_regset_set_reg(tcg_target_call_clobber_regs, TCG_REG_R59);
    tcg_regset_set_reg(tcg_target_call_clobber_regs, TCG_REG_R60);
    tcg_regset_set_reg(tcg_target_call_clobber_regs, TCG_REG_R61);
    tcg_regset_set_reg(tcg_target_call_clobber_regs, TCG_REG_R62);
    tcg_regset_set_reg(tcg_target_call_clobber_regs, TCG_REG_R63);

    tcg_regset_clear(s->reserved_regs);
A
Aurelien Jarno 已提交
2426 2427 2428 2429 2430
    tcg_regset_set_reg(s->reserved_regs, TCG_REG_R0);   /* zero register */
    tcg_regset_set_reg(s->reserved_regs, TCG_REG_R1);   /* global pointer */
    tcg_regset_set_reg(s->reserved_regs, TCG_REG_R2);   /* internal use */
    tcg_regset_set_reg(s->reserved_regs, TCG_REG_R3);   /* internal use */
    tcg_regset_set_reg(s->reserved_regs, TCG_REG_R12);  /* stack pointer */
2431
    tcg_regset_set_reg(s->reserved_regs, TCG_REG_R13);  /* thread pointer */
R
Richard Henderson 已提交
2432
    tcg_regset_set_reg(s->reserved_regs, TCG_REG_R33);  /* return address */
A
Aurelien Jarno 已提交
2433
    tcg_regset_set_reg(s->reserved_regs, TCG_REG_R34);  /* PFS */
A
Aurelien Jarno 已提交
2434

R
Richard Henderson 已提交
2435
    /* The following 4 are not in use, are call-saved, but *not* saved
2436 2437 2438 2439 2440 2441
       by the prologue.  Therefore we cannot use them without modifying
       the prologue.  There doesn't seem to be any good reason to use
       these as opposed to the windowed registers.  */
    tcg_regset_set_reg(s->reserved_regs, TCG_REG_R4);
    tcg_regset_set_reg(s->reserved_regs, TCG_REG_R5);
    tcg_regset_set_reg(s->reserved_regs, TCG_REG_R6);
R
Richard Henderson 已提交
2442
    tcg_regset_set_reg(s->reserved_regs, TCG_REG_R7);
2443

A
Aurelien Jarno 已提交
2444 2445
    tcg_add_target_add_op_defs(ia64_op_defs);
}