tcg-target.inc.c 82.1 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
#ifndef CONFIG_SOFTMMU
44 45 46
#define TCG_GUEST_BASE_REG TCG_REG_R55
#endif

A
Aurelien Jarno 已提交
47 48 49 50 51 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
/* 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,
};

169 170
static const int tcg_target_call_oarg_regs[] = {
    TCG_REG_R8
A
Aurelien Jarno 已提交
171 172 173 174 175 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
};

/*
 * 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,
217
    OPC_BR_CALL_SPNT_FEW_B3   = 0x0a200000000ull,
A
Aurelien Jarno 已提交
218 219 220 221
    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,
222
    OPC_BRL_CALL_SPNT_MANY_X4 = 0x1a200001000ull,
223
    OPC_BRL_CALL_SPTK_MANY_X4 = 0x1a000001000ull,
A
Aurelien Jarno 已提交
224 225 226 227 228 229
    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,
230
    OPC_DEP_I14               = 0x0ae00000000ull,
R
Richard Henderson 已提交
231
    OPC_DEP_I15               = 0x08000000000ull,
A
Aurelien Jarno 已提交
232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260
    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,
261
    OPC_OR_A3                 = 0x10170000000ull,
A
Aurelien Jarno 已提交
262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279
    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,
280
    OPC_XOR_A3                = 0x10178000000ull,
A
Aurelien Jarno 已提交
281 282 283
    OPC_ZXT1_I29              = 0x00080000000ull,
    OPC_ZXT2_I29              = 0x00088000000ull,
    OPC_ZXT4_I29              = 0x00090000000ull,
284 285 286

    INSN_NOP_M                = OPC_NOP_M48,  /* nop.m 0 */
    INSN_NOP_I                = OPC_NOP_I18,  /* nop.i 0 */
A
Aurelien Jarno 已提交
287 288 289 290 291 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
};

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);
}

354 355 356 357 358 359 360 361 362
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 已提交
363 364 365 366 367 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
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);
}

517 518 519 520 521 522 523 524 525 526 527 528
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 已提交
529 530 531 532 533 534 535 536 537 538 539 540
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 已提交
541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592
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;
}

593 594
#define tcg_opc_l4  tcg_opc_l3

A
Aurelien Jarno 已提交
595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 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
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);
}

679 680 681 682 683 684 685 686 687
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 已提交
688 689

/*
690
 * Relocations - Note that we never encode branches elsewhere than slot 2.
A
Aurelien Jarno 已提交
691 692
 */

693
static void reloc_pcrel21b_slot2(tcg_insn_unit *pc, tcg_insn_unit *target)
A
Aurelien Jarno 已提交
694
{
695
    uint64_t imm = target - pc;
A
Aurelien Jarno 已提交
696

697 698 699
    pc->hi = (pc->hi & 0xf700000fffffffffull)
             | ((imm & 0x100000) << 39)  /* s */
             | ((imm & 0x0fffff) << 36); /* imm20b */
A
Aurelien Jarno 已提交
700 701
}

702
static uint64_t get_reloc_pcrel21b_slot2(tcg_insn_unit *pc)
A
Aurelien Jarno 已提交
703
{
704
    int64_t high = pc->hi;
A
Aurelien Jarno 已提交
705

706 707
    return ((high >> 39) & 0x100000) + /* s */
           ((high >> 36) & 0x0fffff);  /* imm20b */
A
Aurelien Jarno 已提交
708 709
}

710
static void patch_reloc(tcg_insn_unit *code_ptr, int type,
711
                        intptr_t value, intptr_t addend)
A
Aurelien Jarno 已提交
712
{
713 714
    tcg_debug_assert(addend == 0);
    tcg_debug_assert(type == R_IA64_PCREL21B);
715
    reloc_pcrel21b_slot2(code_ptr, (tcg_insn_unit *)value);
A
Aurelien Jarno 已提交
716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741
}

/*
 * 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);
742
        tcg_regset_reset_reg(ct->u.regs, TCG_REG_R58);
A
Aurelien Jarno 已提交
743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759
#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 */
760
static inline int tcg_target_const_match(tcg_target_long val, TCGType type,
A
Aurelien Jarno 已提交
761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778
                                         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
 */

779
static tcg_insn_unit *tb_ret_addr;
A
Aurelien Jarno 已提交
780 781 782 783 784 785 786 787 788 789

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 */

790 791 792 793
    *s->code_ptr++ = (tcg_insn_unit){
        (slot1 << 46) | (slot0 << 5) | template,
        (slot2 << 23) | (slot1 >> 18)
    };
A
Aurelien Jarno 已提交
794 795
}

796 797 798 799 800
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);
}

801
static inline void tcg_out_mov(TCGContext *s, TCGType type,
802
                               TCGReg ret, TCGReg arg)
A
Aurelien Jarno 已提交
803 804
{
    tcg_out_bundle(s, mmI,
805 806
                   INSN_NOP_M,
                   INSN_NOP_M,
807
                   tcg_opc_mov_a(TCG_REG_P0, ret, arg));
A
Aurelien Jarno 已提交
808 809
}

810 811
static inline uint64_t tcg_opc_movi_a(int qp, TCGReg dst, int64_t src)
{
812
    tcg_debug_assert(src == sextract64(src, 0, 22));
813 814 815
    return tcg_opc_a5(qp, OPC_ADDL_A5, dst, src, TCG_REG_R0);
}

A
Aurelien Jarno 已提交
816
static inline void tcg_out_movi(TCGContext *s, TCGType type,
817
                                TCGReg reg, tcg_target_long arg)
A
Aurelien Jarno 已提交
818 819
{
    tcg_out_bundle(s, mLX,
820
                   INSN_NOP_M,
A
Aurelien Jarno 已提交
821 822 823 824
                   tcg_opc_l2 (arg),
                   tcg_opc_x2 (TCG_REG_P0, OPC_MOVL_X2, reg, arg));
}

825
static void tcg_out_br(TCGContext *s, TCGLabel *l)
A
Aurelien Jarno 已提交
826
{
827
    uint64_t imm;
A
Aurelien Jarno 已提交
828

829 830 831
    /* 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 已提交
832
    if (l->has_value) {
833
        imm = l->u.value_ptr -  s->code_ptr;
A
Aurelien Jarno 已提交
834
    } else {
835
        imm = get_reloc_pcrel21b_slot2(s->code_ptr);
836
        tcg_out_reloc(s, s->code_ptr, R_IA64_PCREL21B, l, 0);
A
Aurelien Jarno 已提交
837
    }
838 839 840 841 842

    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 已提交
843 844
}

845
static inline void tcg_out_call(TCGContext *s, tcg_insn_unit *desc)
846
{
847 848
    uintptr_t func = desc->lo, gp = desc->hi, disp;

849 850 851
    /* Look through the function descriptor.  */
    tcg_out_bundle(s, mlx,
                   INSN_NOP_M,
852 853 854
                   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;
855 856 857 858 859 860 861
    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 已提交
862 863
static void tcg_out_exit_tb(TCGContext *s, tcg_target_long arg)
{
864
    uint64_t imm, opc1;
A
Aurelien Jarno 已提交
865

866 867 868 869 870 871 872
    /* 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 已提交
873

874
    imm = tb_ret_addr - s->code_ptr;
A
Aurelien Jarno 已提交
875 876

    tcg_out_bundle(s, mLX,
877
                   opc1,
A
Aurelien Jarno 已提交
878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893
                   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),
894
                       INSN_NOP_M,
A
Aurelien Jarno 已提交
895 896 897
                       tcg_opc_i21(TCG_REG_P0, OPC_MOV_I21, TCG_REG_B6,
                                   TCG_REG_R2, 0));
        tcg_out_bundle(s, mmB,
898 899
                       INSN_NOP_M,
                       INSN_NOP_M,
A
Aurelien Jarno 已提交
900 901 902
                       tcg_opc_b4 (TCG_REG_P0, OPC_BR_SPTK_MANY_B4,
                                   TCG_REG_B6));
    }
903
    s->tb_next_offset[arg] = tcg_current_code_size(s);
A
Aurelien Jarno 已提交
904 905 906 907 908
}

static inline void tcg_out_jmp(TCGContext *s, TCGArg addr)
{
    tcg_out_bundle(s, mmI,
909 910
                   INSN_NOP_M,
                   INSN_NOP_M,
A
Aurelien Jarno 已提交
911 912
                   tcg_opc_i21(TCG_REG_P0, OPC_MOV_I21, TCG_REG_B6, addr, 0));
    tcg_out_bundle(s, mmB,
913 914
                   INSN_NOP_M,
                   INSN_NOP_M,
A
Aurelien Jarno 已提交
915 916 917 918 919 920 921 922 923 924 925
                   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),
926
                       INSN_NOP_I);
A
Aurelien Jarno 已提交
927 928 929 930 931 932
    } 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),
933
                       INSN_NOP_I);
A
Aurelien Jarno 已提交
934 935 936 937 938 939 940 941 942 943 944
    }
}

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),
945
                       INSN_NOP_I);
A
Aurelien Jarno 已提交
946 947 948 949 950 951
    } 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),
952
                       INSN_NOP_I);
A
Aurelien Jarno 已提交
953 954 955
    }
}

956
static inline void tcg_out_ld(TCGContext *s, TCGType type, TCGReg arg,
957
                              TCGReg arg1, intptr_t arg2)
A
Aurelien Jarno 已提交
958 959 960 961 962 963 964 965
{
    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);
    }
}

966
static inline void tcg_out_st(TCGContext *s, TCGType type, TCGReg arg,
967
                              TCGReg arg1, intptr_t arg2)
A
Aurelien Jarno 已提交
968 969 970 971 972 973 974 975
{
    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);
    }
}

976 977 978
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 已提交
979
{
980
    uint64_t opc1 = 0, opc2 = 0, opc3 = 0;
A
Aurelien Jarno 已提交
981 982

    if (const_arg2 && arg2 != 0) {
983
        opc2 = tcg_opc_movi_a(TCG_REG_P0, TCG_REG_R3, arg2);
A
Aurelien Jarno 已提交
984 985
        arg2 = TCG_REG_R3;
    }
986 987 988 989
    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 {
990
            opc1 = tcg_opc_movi_a(TCG_REG_P0, TCG_REG_R2, arg1);
991 992 993 994 995 996
            arg1 = TCG_REG_R2;
        }
    }
    if (opc3 == 0) {
        opc3 = tcg_opc_a1(TCG_REG_P0, opc_a1, ret, arg1, arg2);
    }
A
Aurelien Jarno 已提交
997

998 999 1000
    tcg_out_bundle(s, (opc1 || opc2 ? mII : miI),
                   opc1 ? opc1 : INSN_NOP_M,
                   opc2 ? opc2 : INSN_NOP_I,
1001
                   opc3);
A
Aurelien Jarno 已提交
1002 1003
}

1004 1005 1006 1007 1008 1009 1010 1011 1012
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 {
1013
        tcg_out_alu(s, OPC_ADD_A1, 0, ret, arg1, 0, arg2, const_arg2);
1014 1015 1016
    }
}

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

A
Aurelien Jarno 已提交
1031 1032 1033 1034 1035
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,
1036
                   INSN_NOP_M,
A
Aurelien Jarno 已提交
1037 1038 1039 1040 1041 1042 1043 1044 1045
                   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,
1046
                   INSN_NOP_M,
A
Aurelien Jarno 已提交
1047 1048 1049 1050 1051 1052 1053 1054 1055
                   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,
1056
                   INSN_NOP_M,
A
Aurelien Jarno 已提交
1057 1058 1059 1060 1061 1062 1063 1064 1065
                   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,
1066
                   INSN_NOP_M,
A
Aurelien Jarno 已提交
1067 1068 1069 1070 1071 1072 1073 1074 1075 1076
                   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),
1077
                   INSN_NOP_I);
A
Aurelien Jarno 已提交
1078
    tcg_out_bundle(s, mmF,
1079 1080
                   INSN_NOP_M,
                   INSN_NOP_M,
A
Aurelien Jarno 已提交
1081 1082 1083 1084
                   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),
1085 1086
                   INSN_NOP_I,
                   INSN_NOP_I);
A
Aurelien Jarno 已提交
1087 1088 1089 1090 1091 1092 1093
}

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,
1094 1095
                       INSN_NOP_M,
                       INSN_NOP_I,
A
Aurelien Jarno 已提交
1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112
                       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,
1113 1114
                       INSN_NOP_M,
                       INSN_NOP_I,
A
Aurelien Jarno 已提交
1115 1116 1117 1118
                       tcg_opc_i11(TCG_REG_P0, OPC_EXTR_I11,
                                   ret, arg1, arg2, 63 - arg2));
    } else {
        tcg_out_bundle(s, miI,
1119 1120
                       INSN_NOP_M,
                       INSN_NOP_I,
A
Aurelien Jarno 已提交
1121 1122 1123 1124 1125 1126 1127 1128 1129
                       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,
1130 1131
                       INSN_NOP_M,
                       INSN_NOP_I,
A
Aurelien Jarno 已提交
1132 1133 1134 1135
                       tcg_opc_i12(TCG_REG_P0, OPC_DEP_Z_I12, ret,
                                   arg1, 63 - arg2, 31 - arg2));
    } else {
        tcg_out_bundle(s, mII,
1136
                       INSN_NOP_M,
A
Aurelien Jarno 已提交
1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148
                       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,
1149 1150
                       INSN_NOP_M,
                       INSN_NOP_I,
A
Aurelien Jarno 已提交
1151 1152 1153 1154
                       tcg_opc_i12(TCG_REG_P0, OPC_DEP_Z_I12, ret,
                                   arg1, 63 - arg2, 63 - arg2));
    } else {
        tcg_out_bundle(s, miI,
1155 1156
                       INSN_NOP_M,
                       INSN_NOP_I,
A
Aurelien Jarno 已提交
1157 1158 1159 1160 1161 1162 1163 1164 1165 1166
                       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,
1167 1168
                       INSN_NOP_M,
                       INSN_NOP_I,
A
Aurelien Jarno 已提交
1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185
                       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,
1186 1187
                       INSN_NOP_M,
                       INSN_NOP_I,
A
Aurelien Jarno 已提交
1188 1189 1190 1191
                       tcg_opc_i11(TCG_REG_P0, OPC_EXTR_U_I11, ret,
                                   arg1, arg2, 63 - arg2));
    } else {
        tcg_out_bundle(s, miI,
1192 1193
                       INSN_NOP_M,
                       INSN_NOP_I,
A
Aurelien Jarno 已提交
1194 1195 1196 1197 1198 1199 1200 1201 1202 1203
                       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,
1204
                       INSN_NOP_M,
A
Aurelien Jarno 已提交
1205 1206 1207 1208 1209 1210
                       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,
1211
                       INSN_NOP_M,
A
Aurelien Jarno 已提交
1212 1213 1214 1215 1216
                       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,
1217
                       INSN_NOP_M,
A
Aurelien Jarno 已提交
1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229
                       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,
1230 1231
                       INSN_NOP_M,
                       INSN_NOP_I,
A
Aurelien Jarno 已提交
1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242
                       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,
1243 1244
                       INSN_NOP_M,
                       INSN_NOP_I,
A
Aurelien Jarno 已提交
1245 1246 1247 1248 1249 1250 1251 1252 1253 1254
                       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,
1255
                       INSN_NOP_M,
A
Aurelien Jarno 已提交
1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275
                       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,
1276 1277
                       INSN_NOP_M,
                       INSN_NOP_I,
A
Aurelien Jarno 已提交
1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288
                       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,
1289 1290
                       INSN_NOP_M,
                       INSN_NOP_I,
A
Aurelien Jarno 已提交
1291 1292 1293 1294 1295
                       tcg_opc_a1 (TCG_REG_P0, OPC_OR_A1, ret,
                                   TCG_REG_R2, TCG_REG_R3));
    }
}

1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309
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 已提交
1310 1311 1312 1313
static inline void tcg_out_ext(TCGContext *s, uint64_t opc_i29,
                               TCGArg ret, TCGArg arg)
{
    tcg_out_bundle(s, miI,
1314 1315
                   INSN_NOP_M,
                   INSN_NOP_I,
A
Aurelien Jarno 已提交
1316 1317 1318
                   tcg_opc_i29(TCG_REG_P0, opc_i29, ret, arg));
}

1319 1320 1321 1322 1323
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 已提交
1324 1325 1326
static inline void tcg_out_bswap16(TCGContext *s, TCGArg ret, TCGArg arg)
{
    tcg_out_bundle(s, mII,
1327
                   INSN_NOP_M,
A
Aurelien Jarno 已提交
1328
                   tcg_opc_i12(TCG_REG_P0, OPC_DEP_Z_I12, ret, arg, 15, 15),
1329
                   tcg_opc_bswap64_i(TCG_REG_P0, ret, ret));
A
Aurelien Jarno 已提交
1330 1331 1332 1333 1334
}

static inline void tcg_out_bswap32(TCGContext *s, TCGArg ret, TCGArg arg)
{
    tcg_out_bundle(s, mII,
1335
                   INSN_NOP_M,
A
Aurelien Jarno 已提交
1336
                   tcg_opc_i12(TCG_REG_P0, OPC_DEP_Z_I12, ret, arg, 31, 31),
1337
                   tcg_opc_bswap64_i(TCG_REG_P0, ret, ret));
A
Aurelien Jarno 已提交
1338 1339 1340 1341
}

static inline void tcg_out_bswap64(TCGContext *s, TCGArg ret, TCGArg arg)
{
1342
    tcg_out_bundle(s, miI,
1343 1344
                   INSN_NOP_M,
                   INSN_NOP_I,
1345
                   tcg_opc_bswap64_i(TCG_REG_P0, ret, arg));
A
Aurelien Jarno 已提交
1346 1347
}

R
Richard Henderson 已提交
1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364
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.  */
1365
            i1 = tcg_opc_movi_a(TCG_REG_P0, TCG_REG_R2, a2);
R
Richard Henderson 已提交
1366 1367 1368 1369 1370 1371 1372
            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),
1373 1374
                   INSN_NOP_M,
                   i1 ? i1 : INSN_NOP_I,
R
Richard Henderson 已提交
1375 1376 1377
                   i2);
}

A
Aurelien Jarno 已提交
1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419
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 已提交
1420
static inline void tcg_out_brcond(TCGContext *s, TCGCond cond, TCGReg arg1,
1421
                                  TCGReg arg2, TCGLabel *l, int cmp4)
A
Aurelien Jarno 已提交
1422
{
1423
    uint64_t imm;
A
Aurelien Jarno 已提交
1424

1425 1426 1427
    /* 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 已提交
1428
    if (l->has_value) {
1429
        imm = l->u.value_ptr - s->code_ptr;
A
Aurelien Jarno 已提交
1430
    } else {
1431
        imm = get_reloc_pcrel21b_slot2(s->code_ptr);
1432
        tcg_out_reloc(s, s->code_ptr, R_IA64_PCREL21B, l, 0);
A
Aurelien Jarno 已提交
1433
    }
1434 1435 1436 1437 1438

    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 已提交
1439 1440 1441 1442 1443 1444 1445
}

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),
1446 1447
                   tcg_opc_movi_a(TCG_REG_P6, ret, 1),
                   tcg_opc_movi_a(TCG_REG_P7, ret, 0));
A
Aurelien Jarno 已提交
1448 1449
}

1450 1451 1452 1453 1454 1455 1456 1457
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) {
1458
        opc1 = tcg_opc_movi_a(TCG_REG_P6, ret, v1);
1459
    } else if (ret == v1) {
1460
        opc1 = INSN_NOP_M;
1461
    } else {
1462
        opc1 = tcg_opc_mov_a(TCG_REG_P6, ret, v1);
1463 1464
    }
    if (const_v2) {
1465
        opc2 = tcg_opc_movi_a(TCG_REG_P7, ret, v2);
1466
    } else if (ret == v2) {
1467
        opc2 = INSN_NOP_I;
1468
    } else {
1469
        opc2 = tcg_opc_mov_a(TCG_REG_P7, ret, v2);
1470 1471 1472 1473 1474 1475 1476 1477
    }

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

A
Aurelien Jarno 已提交
1478
#if defined(CONFIG_SOFTMMU)
1479 1480 1481 1482
/* 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 已提交
1483
/* Load and compare a TLB entry, and return the result in (p6, p7).
1484
   R2 is loaded with the addend TLB entry.
1485
   R57 is loaded with the address, zero extented on 32-bit targets.
1486 1487
   R1, R3 are clobbered, leaving R56 free for...
   BSWAP_1, BSWAP_2 and I-slot insns for swapping data for store.  */
1488
static inline void tcg_out_qemu_tlb(TCGContext *s, TCGReg addr_reg,
1489 1490
                                    TCGMemOp s_bits, int off_rw, int off_add,
                                    uint64_t bswap1, uint64_t bswap2)
1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517
{
     /*
        .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 已提交
1518
                               addr_reg, TARGET_PAGE_BITS, CPU_TLB_BITS - 1),
1519 1520
                   tcg_opc_ext_i(TCG_REG_P0,
                                 TARGET_LONG_BITS == 32 ? MO_UL : MO_Q,
1521 1522
                                 TCG_REG_R57, addr_reg));
    tcg_out_bundle(s, miI,
A
Aurelien Jarno 已提交
1523
                   tcg_opc_a1 (TCG_REG_P0, OPC_ADD_A1, TCG_REG_R2,
1524 1525 1526 1527 1528 1529 1530 1531 1532 1533
                               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),
1534 1535
                   tcg_opc_m3 (TCG_REG_P0,
                               (TARGET_LONG_BITS == 32
1536 1537
                                ? OPC_LD4_M3 : OPC_LD8_M3), TCG_REG_R3,
                               TCG_REG_R2, off_add - off_rw),
1538
                   bswap1);
1539
    tcg_out_bundle(s, mmI,
1540
                   tcg_opc_m1 (TCG_REG_P0, OPC_LD8_M1, TCG_REG_R2, TCG_REG_R2),
A
Aurelien Jarno 已提交
1541
                   tcg_opc_a6 (TCG_REG_P0, OPC_CMP_EQ_A6, TCG_REG_P6,
1542
                               TCG_REG_P7, TCG_REG_R1, TCG_REG_R3),
1543
                   bswap2);
A
Aurelien Jarno 已提交
1544 1545
}

1546 1547 1548
typedef struct TCGLabelQemuLdst {
    bool is_ld;
    TCGMemOp size;
1549
    tcg_insn_unit *label_ptr;     /* label pointers to be updated */
1550
    struct TCGLabelQemuLdst *next;
1551 1552 1553
} TCGLabelQemuLdst;

typedef struct TCGBackendData {
1554
    TCGLabelQemuLdst *labels;
1555 1556 1557 1558
} TCGBackendData;

static inline void tcg_out_tb_init(TCGContext *s)
{
1559
    s->be->labels = NULL;
1560 1561 1562
}

static void add_qemu_ldst_label(TCGContext *s, bool is_ld, TCGMemOp opc,
1563
                                tcg_insn_unit *label_ptr)
1564 1565
{
    TCGBackendData *be = s->be;
1566
    TCGLabelQemuLdst *l = tcg_malloc(sizeof(*l));
1567 1568 1569 1570

    l->is_ld = is_ld;
    l->size = opc & MO_SIZE;
    l->label_ptr = label_ptr;
1571 1572
    l->next = be->labels;
    be->labels = l;
1573 1574
}

1575
static bool tcg_out_tb_finalize(TCGContext *s)
1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586
{
    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,
    };
1587
    tcg_insn_unit *thunks[8] = { };
1588
    TCGLabelQemuLdst *l;
1589

1590
    for (l = s->be->labels; l != NULL; l = l->next) {
1591
        long x = l->is_ld * 4 + l->size;
1592
        tcg_insn_unit *dest = thunks[x];
1593 1594 1595 1596

        /* 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
V
Veres Lajos 已提交
1597
           using incoming parameter register numbers, not outgoing.  */
1598 1599 1600
        if (dest == NULL) {
            uintptr_t *desc = (uintptr_t *)helpers[x];
            uintptr_t func = desc[0], gp = desc[1], disp;
1601

1602
            thunks[x] = dest = s->code_ptr;
1603 1604 1605

            tcg_out_bundle(s, mlx,
                           INSN_NOP_M,
1606
                           tcg_opc_l2 (gp),
1607
                           tcg_opc_x2 (TCG_REG_P0, OPC_MOVL_X2,
1608
                                       TCG_REG_R1, gp));
1609 1610 1611 1612 1613 1614
            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));
1615
            disp = (tcg_insn_unit *)func - s->code_ptr;
1616 1617 1618 1619 1620 1621
            tcg_out_bundle(s, mLX,
                           INSN_NOP_M,
                           tcg_opc_l3 (disp),
                           tcg_opc_x3 (TCG_REG_P0, OPC_BRL_SPTK_MANY_X3, disp));
        }

1622
        reloc_pcrel21b_slot2(l->label_ptr, dest);
1623 1624 1625 1626 1627 1628 1629 1630

        /* Test for (pending) buffer overflow.  The assumption is that any
           one operation beginning below the high water mark cannot overrun
           the buffer completely.  Thus we can test for overflow after
           generating code without having to check during generation.  */
        if (unlikely((void *)s->code_ptr > s->code_gen_highwater)) {
            return false;
        }
1631
    }
1632
    return true;
1633
}
A
Aurelien Jarno 已提交
1634

1635
static inline void tcg_out_qemu_ld(TCGContext *s, const TCGArg *args)
A
Aurelien Jarno 已提交
1636
{
1637 1638 1639 1640
    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;
1641
    TCGMemOpIdx oi;
1642
    TCGMemOp opc, s_bits;
1643
    uint64_t fin1, fin2;
1644
    tcg_insn_unit *label_ptr;
A
Aurelien Jarno 已提交
1645

1646 1647
    data_reg = args[0];
    addr_reg = args[1];
1648 1649 1650
    oi = args[2];
    opc = get_memop(oi);
    mem_index = get_mmuidx(oi);
1651
    s_bits = opc & MO_SIZE;
A
Aurelien Jarno 已提交
1652 1653 1654

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

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

1661
    fin2 = 0;
1662
    if (opc & MO_BSWAP) {
1663
        fin1 = tcg_opc_bswap64_i(TCG_REG_P0, data_reg, TCG_REG_R8);
1664 1665
        if (s_bits < MO_64) {
            int shift = 64 - (8 << s_bits);
1666 1667 1668
            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);
1669
        }
1670 1671
    } else {
        fin1 = tcg_opc_ext_i(TCG_REG_P0, opc, data_reg, TCG_REG_R8);
1672 1673
    }

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

    add_qemu_ldst_label(s, 1, opc, label_ptr);
1688 1689 1690 1691 1692 1693 1694

    /* 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 已提交
1695 1696
}

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

1709 1710
    data_reg = args[0];
    addr_reg = args[1];
1711 1712 1713
    oi = args[2];
    opc = get_memop(oi);
    mem_index = get_mmuidx(oi);
1714
    s_bits = opc & MO_SIZE;
A
Aurelien Jarno 已提交
1715

1716 1717 1718 1719
    /* 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;
1720
    if (opc & MO_BSWAP) {
1721
        pre1 = tcg_opc_bswap64_i(TCG_REG_P0, TCG_REG_R58, data_reg);
1722 1723
        if (s_bits < MO_64) {
            int shift = 64 - (8 << s_bits);
1724 1725
            pre2 = tcg_opc_i11(TCG_REG_P0, OPC_EXTR_U_I11,
                               TCG_REG_R58, TCG_REG_R58, shift, 63 - shift);
1726
        }
1727 1728 1729
    } 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);
1730 1731
    }

1732
    tcg_out_qemu_tlb(s, addr_reg, s_bits,
1733
                     offsetof(CPUArchState, tlb_table[mem_index][0].addr_write),
1734
                     offsetof(CPUArchState, tlb_table[mem_index][0].addend),
1735
                     pre1, pre2);
A
Aurelien Jarno 已提交
1736 1737

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

    add_qemu_ldst_label(s, 0, opc, label_ptr);
A
Aurelien Jarno 已提交
1752 1753 1754
}

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

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

1765 1766 1767
    data_reg = args[0];
    addr_reg = args[1];
    opc = args[2];
1768 1769
    s_bits = opc & MO_SIZE;
    bswap = opc & MO_BSWAP;
A
Aurelien Jarno 已提交
1770 1771

#if TARGET_LONG_BITS == 32
1772
    if (guest_base != 0) {
1773
        tcg_out_bundle(s, mII,
1774
                       INSN_NOP_M,
1775 1776 1777 1778 1779 1780
                       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,
1781
                       INSN_NOP_M,
1782 1783
                       tcg_opc_i29(TCG_REG_P0, OPC_ZXT4_I29,
                                   TCG_REG_R2, addr_reg),
1784
                       INSN_NOP_I);
1785
    }
A
Aurelien Jarno 已提交
1786

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

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

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

1889 1890 1891
    data_reg = args[0];
    addr_reg = args[1];
    opc = args[2];
1892 1893
    s_bits = opc & MO_SIZE;
    bswap = opc & MO_BSWAP;
A
Aurelien Jarno 已提交
1894 1895

#if TARGET_LONG_BITS == 32
1896
    if (guest_base != 0) {
1897
        tcg_out_bundle(s, mII,
1898
                       INSN_NOP_M,
1899 1900 1901 1902 1903 1904
                       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,
1905
                       INSN_NOP_M,
1906
                       tcg_opc_i29(TCG_REG_P0, OPC_ZXT4_I29,
1907
                                   TCG_REG_R2, addr_reg),
1908
                       INSN_NOP_I);
1909 1910
    }

A
Aurelien Jarno 已提交
1911
    if (bswap) {
1912
        if (s_bits == MO_16) {
A
Aurelien Jarno 已提交
1913
            tcg_out_bundle(s, mII,
1914
                           INSN_NOP_M,
A
Aurelien Jarno 已提交
1915 1916
                           tcg_opc_i12(TCG_REG_P0, OPC_DEP_Z_I12,
                                       TCG_REG_R3, data_reg, 15, 15),
1917 1918
                           tcg_opc_bswap64_i(TCG_REG_P0,
                                             TCG_REG_R3, TCG_REG_R3));
A
Aurelien Jarno 已提交
1919
            data_reg = TCG_REG_R3;
1920
        } else if (s_bits == MO_32) {
A
Aurelien Jarno 已提交
1921
            tcg_out_bundle(s, mII,
1922
                           INSN_NOP_M,
A
Aurelien Jarno 已提交
1923 1924
                           tcg_opc_i12(TCG_REG_P0, OPC_DEP_Z_I12,
                                       TCG_REG_R3, data_reg, 31, 31),
1925 1926
                           tcg_opc_bswap64_i(TCG_REG_P0,
                                             TCG_REG_R3, TCG_REG_R3));
A
Aurelien Jarno 已提交
1927
            data_reg = TCG_REG_R3;
1928
        } else if (s_bits == MO_64) {
A
Aurelien Jarno 已提交
1929
            tcg_out_bundle(s, miI,
1930 1931
                           INSN_NOP_M,
                           INSN_NOP_I,
1932
                           tcg_opc_bswap64_i(TCG_REG_P0, TCG_REG_R3, data_reg));
A
Aurelien Jarno 已提交
1933 1934 1935 1936
            data_reg = TCG_REG_R3;
        }
    }
    tcg_out_bundle(s, mmI,
1937
                   tcg_opc_m4 (TCG_REG_P0, opc_st_m4[s_bits],
A
Aurelien Jarno 已提交
1938
                               data_reg, TCG_REG_R2),
1939 1940
                   INSN_NOP_M,
                   INSN_NOP_I);
A
Aurelien Jarno 已提交
1941
#else
1942
    if (guest_base != 0) {
1943 1944 1945 1946
        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 {
1947
        add_guest_base = INSN_NOP_M;
1948 1949
    }

1950
    if (!bswap) {
1951
        tcg_out_bundle(s, (guest_base ? MmI : mmI),
1952
                       add_guest_base,
1953
                       tcg_opc_m4 (TCG_REG_P0, opc_st_m4[s_bits],
1954
                                   data_reg, addr_reg),
1955
                       INSN_NOP_I);
A
Aurelien Jarno 已提交
1956
    } else {
1957
        if (s_bits == MO_16) {
A
Aurelien Jarno 已提交
1958
            tcg_out_bundle(s, mII,
1959
                           add_guest_base,
A
Aurelien Jarno 已提交
1960 1961
                           tcg_opc_i12(TCG_REG_P0, OPC_DEP_Z_I12,
                                       TCG_REG_R3, data_reg, 15, 15),
1962 1963
                           tcg_opc_bswap64_i(TCG_REG_P0,
                                             TCG_REG_R3, TCG_REG_R3));
A
Aurelien Jarno 已提交
1964
            data_reg = TCG_REG_R3;
1965
        } else if (s_bits == MO_32) {
A
Aurelien Jarno 已提交
1966
            tcg_out_bundle(s, mII,
1967
                           add_guest_base,
A
Aurelien Jarno 已提交
1968 1969
                           tcg_opc_i12(TCG_REG_P0, OPC_DEP_Z_I12,
                                       TCG_REG_R3, data_reg, 31, 31),
1970 1971
                           tcg_opc_bswap64_i(TCG_REG_P0,
                                             TCG_REG_R3, TCG_REG_R3));
A
Aurelien Jarno 已提交
1972
            data_reg = TCG_REG_R3;
1973
        } else if (s_bits == MO_64) {
A
Aurelien Jarno 已提交
1974
            tcg_out_bundle(s, miI,
1975
                           add_guest_base,
1976
                           INSN_NOP_I,
1977
                           tcg_opc_bswap64_i(TCG_REG_P0, TCG_REG_R3, data_reg));
A
Aurelien Jarno 已提交
1978 1979 1980
            data_reg = TCG_REG_R3;
        }
        tcg_out_bundle(s, miI,
1981
                       tcg_opc_m4 (TCG_REG_P0, opc_st_m4[s_bits],
1982
                                   data_reg, addr_reg),
1983 1984
                       INSN_NOP_I,
                       INSN_NOP_I);
A
Aurelien Jarno 已提交
1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998
    }
#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:
1999
        tcg_out_br(s, arg_label(args[0]));
A
Aurelien Jarno 已提交
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 2048 2049 2050 2051
        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:
2052
        tcg_out_add(s, args[0], args[1], args[2], const_args[2]);
A
Aurelien Jarno 已提交
2053 2054 2055
        break;
    case INDEX_op_sub_i32:
    case INDEX_op_sub_i64:
2056
        tcg_out_sub(s, args[0], args[1], const_args[1], args[2], const_args[2]);
A
Aurelien Jarno 已提交
2057 2058 2059 2060
        break;

    case INDEX_op_and_i32:
    case INDEX_op_and_i64:
2061 2062 2063
        /* 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 已提交
2064 2065 2066
        break;
    case INDEX_op_andc_i32:
    case INDEX_op_andc_i64:
2067 2068
        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 已提交
2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086
        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:
2087 2088 2089
        /* 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 已提交
2090 2091 2092 2093 2094 2095 2096 2097
        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:
2098 2099 2100
        /* 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 已提交
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
        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;
2155
    case INDEX_op_ext_i32_i64:
A
Aurelien Jarno 已提交
2156 2157 2158
    case INDEX_op_ext32s_i64:
        tcg_out_ext(s, OPC_SXT4_I29, args[0], args[1]);
        break;
2159
    case INDEX_op_extu_i32_i64:
A
Aurelien Jarno 已提交
2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175
    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 已提交
2176 2177 2178 2179 2180 2181
    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 已提交
2182
    case INDEX_op_brcond_i32:
2183
        tcg_out_brcond(s, args[2], args[0], args[1], arg_label(args[3]), 1);
A
Aurelien Jarno 已提交
2184 2185
        break;
    case INDEX_op_brcond_i64:
2186
        tcg_out_brcond(s, args[2], args[0], args[1], arg_label(args[3]), 0);
A
Aurelien Jarno 已提交
2187 2188 2189 2190 2191 2192 2193
        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;
2194 2195 2196 2197 2198 2199 2200 2201
    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 已提交
2202

2203 2204
    case INDEX_op_qemu_ld_i32:
        tcg_out_qemu_ld(s, args);
A
Aurelien Jarno 已提交
2205
        break;
2206 2207
    case INDEX_op_qemu_ld_i64:
        tcg_out_qemu_ld(s, args);
A
Aurelien Jarno 已提交
2208
        break;
2209 2210
    case INDEX_op_qemu_st_i32:
        tcg_out_qemu_st(s, args);
A
Aurelien Jarno 已提交
2211
        break;
2212 2213
    case INDEX_op_qemu_st_i64:
        tcg_out_qemu_st(s, args);
A
Aurelien Jarno 已提交
2214 2215
        break;

2216 2217 2218 2219 2220
    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 已提交
2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239
    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" } },

2240
    { INDEX_op_add_i32, { "r", "rZ", "rI" } },
A
Aurelien Jarno 已提交
2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267
    { 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 已提交
2268
    { INDEX_op_brcond_i32, { "rZ", "rZ" } },
A
Aurelien Jarno 已提交
2269
    { INDEX_op_setcond_i32, { "r", "rZ", "rZ" } },
2270
    { INDEX_op_movcond_i32, { "r", "rZ", "rZ", "rI", "rI" } },
A
Aurelien Jarno 已提交
2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283

    { 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" } },

2284
    { INDEX_op_add_i64, { "r", "rZ", "rI" } },
A
Aurelien Jarno 已提交
2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309
    { 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"} },
2310 2311
    { INDEX_op_ext_i32_i64, { "r", "rZ" } },
    { INDEX_op_extu_i32_i64, { "r", "rZ" } },
A
Aurelien Jarno 已提交
2312 2313 2314 2315 2316

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

R
Richard Henderson 已提交
2317
    { INDEX_op_brcond_i64, { "rZ", "rZ" } },
A
Aurelien Jarno 已提交
2318
    { INDEX_op_setcond_i64, { "r", "rZ", "rZ" } },
2319
    { INDEX_op_movcond_i64, { "r", "rZ", "rZ", "rI", "rI" } },
A
Aurelien Jarno 已提交
2320

R
Richard Henderson 已提交
2321 2322 2323
    { INDEX_op_deposit_i32, { "r", "rZ", "ri" } },
    { INDEX_op_deposit_i64, { "r", "rZ", "ri" } },

2324 2325 2326 2327
    { 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 已提交
2328 2329 2330 2331 2332

    { -1 },
};

/* Generate global QEMU prologue and epilogue code */
2333
static void tcg_target_qemu_prologue(TCGContext *s)
A
Aurelien Jarno 已提交
2334 2335 2336 2337
{
    int frame_size;

    /* reserve some stack space */
B
Blue Swirl 已提交
2338 2339
    frame_size = TCG_STATIC_CALL_ARGS_SIZE +
                 CPU_TEMP_BUF_NLONGS * sizeof(long);
A
Aurelien Jarno 已提交
2340 2341
    frame_size = (frame_size + TCG_TARGET_STACK_ALIGN - 1) &
                 ~(TCG_TARGET_STACK_ALIGN - 1);
B
Blue Swirl 已提交
2342 2343
    tcg_set_frame(s, TCG_REG_CALL_STACK, TCG_STATIC_CALL_ARGS_SIZE,
                  CPU_TEMP_BUF_NLONGS * sizeof(long));
A
Aurelien Jarno 已提交
2344 2345

    /* First emit adhoc function descriptor */
2346 2347 2348 2349 2350
    *s->code_ptr = (tcg_insn_unit){
        (uint64_t)(s->code_ptr + 1), /* entry point */
        0                            /* skip gp */
    };
    s->code_ptr++;
A
Aurelien Jarno 已提交
2351 2352

    /* prologue */
A
Aurelien Jarno 已提交
2353
    tcg_out_bundle(s, miI,
A
Aurelien Jarno 已提交
2354
                   tcg_opc_m34(TCG_REG_P0, OPC_ALLOC_M34,
A
Aurelien Jarno 已提交
2355
                               TCG_REG_R34, 32, 24, 0),
R
Richard Henderson 已提交
2356
                   INSN_NOP_I,
A
Aurelien Jarno 已提交
2357
                   tcg_opc_i21(TCG_REG_P0, OPC_MOV_I21,
A
Aurelien Jarno 已提交
2358
                               TCG_REG_B6, TCG_REG_R33, 0));
2359

2360
    /* ??? If guest_base < 0x200000, we could load the register via
2361
       an ADDL in the M slot of the next bundle.  */
2362
    if (guest_base != 0) {
2363
        tcg_out_bundle(s, mlx,
2364
                       INSN_NOP_M,
2365
                       tcg_opc_l2(guest_base),
2366
                       tcg_opc_x2 (TCG_REG_P0, OPC_MOVL_X2,
2367
                                   TCG_GUEST_BASE_REG, guest_base));
2368 2369 2370
        tcg_regset_set_reg(s->reserved_regs, TCG_GUEST_BASE_REG);
    }

A
Aurelien Jarno 已提交
2371 2372 2373
    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 已提交
2374
                   tcg_opc_i22(TCG_REG_P0, OPC_MOV_I22,
R
Richard Henderson 已提交
2375
                               TCG_REG_R33, TCG_REG_B0),
A
Aurelien Jarno 已提交
2376 2377 2378 2379 2380
                   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,
2381
                   INSN_NOP_M,
A
Aurelien Jarno 已提交
2382
                   tcg_opc_i21(TCG_REG_P0, OPC_MOV_I21,
R
Richard Henderson 已提交
2383
                               TCG_REG_B0, TCG_REG_R33, 0),
A
Aurelien Jarno 已提交
2384 2385 2386
                   tcg_opc_a4 (TCG_REG_P0, OPC_ADDS_A4,
                               TCG_REG_R12, frame_size, TCG_REG_R12));
    tcg_out_bundle(s, miB,
2387
                   INSN_NOP_M,
A
Aurelien Jarno 已提交
2388
                   tcg_opc_i26(TCG_REG_P0, OPC_MOV_I_I26,
A
Aurelien Jarno 已提交
2389
                               TCG_REG_PFS, TCG_REG_R34),
A
Aurelien Jarno 已提交
2390 2391 2392 2393
                   tcg_opc_b4 (TCG_REG_P0, OPC_BR_RET_SPTK_MANY_B4,
                               TCG_REG_B0));
}

2394
static void tcg_target_init(TCGContext *s)
A
Aurelien Jarno 已提交
2395 2396 2397 2398 2399 2400
{
    tcg_regset_set(tcg_target_available_regs[TCG_TYPE_I32],
                   0xffffffffffffffffull);
    tcg_regset_set(tcg_target_available_regs[TCG_TYPE_I64],
                   0xffffffffffffffffull);

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 2426 2427 2428 2429 2430 2431 2432 2433
    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 已提交
2434 2435 2436 2437 2438
    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 */
2439
    tcg_regset_set_reg(s->reserved_regs, TCG_REG_R13);  /* thread pointer */
R
Richard Henderson 已提交
2440
    tcg_regset_set_reg(s->reserved_regs, TCG_REG_R33);  /* return address */
A
Aurelien Jarno 已提交
2441
    tcg_regset_set_reg(s->reserved_regs, TCG_REG_R34);  /* PFS */
A
Aurelien Jarno 已提交
2442

R
Richard Henderson 已提交
2443
    /* The following 4 are not in use, are call-saved, but *not* saved
2444 2445 2446 2447 2448 2449
       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 已提交
2450
    tcg_regset_set_reg(s->reserved_regs, TCG_REG_R7);
2451

A
Aurelien Jarno 已提交
2452 2453
    tcg_add_target_add_op_defs(ia64_op_defs);
}