tcg-target.c 74.3 KB
Newer Older
A
Alexander Graf 已提交
1 2 3 4
/*
 * Tiny Code Generator for QEMU
 *
 * Copyright (c) 2009 Ulrich Hecht <uli@suse.de>
R
Richard Henderson 已提交
5 6
 * Copyright (c) 2009 Alexander Graf <agraf@suse.de>
 * Copyright (c) 2010 Richard Henderson <rth@twiddle.net>
A
Alexander Graf 已提交
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
 *
 * 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.
 */

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

29 30 31 32 33
/* We only support generating code for 64-bit mode.  */
#if TCG_TARGET_REG_BITS != 64
#error "unsupported code generation mode"
#endif

34 35
#include "elf.h"

R
Richard Henderson 已提交
36 37 38 39 40
/* ??? The translation blocks produced by TCG are generally small enough to
   be entirely reachable with a 16-bit displacement.  Leaving the option for
   a 32-bit displacement here Just In Case.  */
#define USE_LONG_BRANCHES 0

41 42 43 44
#define TCG_CT_CONST_MULI  0x100
#define TCG_CT_CONST_ORI   0x200
#define TCG_CT_CONST_XORI  0x400
#define TCG_CT_CONST_CMPI  0x800
45
#define TCG_CT_CONST_ADLI  0x1000
R
Richard Henderson 已提交
46 47 48 49 50 51 52 53

/* Several places within the instruction set 0 means "no register"
   rather than TCG_REG_R0.  */
#define TCG_REG_NONE    0

/* A scratch register that may be be used throughout the backend.  */
#define TCG_TMP0        TCG_REG_R14

54
#ifndef CONFIG_SOFTMMU
R
Richard Henderson 已提交
55 56 57 58 59 60 61 62 63 64
#define TCG_GUEST_BASE_REG TCG_REG_R13
#endif

/* All of the following instructions are prefixed with their instruction
   format, and are defined as 8- or 16-bit quantities, even when the two
   halves of the 16-bit quantity may appear 32 bits apart in the insn.
   This makes it easy to copy the values from the tables in Appendix B.  */
typedef enum S390Opcode {
    RIL_AFI     = 0xc209,
    RIL_AGFI    = 0xc208,
65
    RIL_ALFI    = 0xc20b,
R
Richard Henderson 已提交
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86
    RIL_ALGFI   = 0xc20a,
    RIL_BRASL   = 0xc005,
    RIL_BRCL    = 0xc004,
    RIL_CFI     = 0xc20d,
    RIL_CGFI    = 0xc20c,
    RIL_CLFI    = 0xc20f,
    RIL_CLGFI   = 0xc20e,
    RIL_IIHF    = 0xc008,
    RIL_IILF    = 0xc009,
    RIL_LARL    = 0xc000,
    RIL_LGFI    = 0xc001,
    RIL_LGRL    = 0xc408,
    RIL_LLIHF   = 0xc00e,
    RIL_LLILF   = 0xc00f,
    RIL_LRL     = 0xc40d,
    RIL_MSFI    = 0xc201,
    RIL_MSGFI   = 0xc200,
    RIL_NIHF    = 0xc00a,
    RIL_NILF    = 0xc00b,
    RIL_OIHF    = 0xc00c,
    RIL_OILF    = 0xc00d,
87
    RIL_SLFI    = 0xc205,
88
    RIL_SLGFI   = 0xc204,
R
Richard Henderson 已提交
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
    RIL_XIHF    = 0xc006,
    RIL_XILF    = 0xc007,

    RI_AGHI     = 0xa70b,
    RI_AHI      = 0xa70a,
    RI_BRC      = 0xa704,
    RI_IIHH     = 0xa500,
    RI_IIHL     = 0xa501,
    RI_IILH     = 0xa502,
    RI_IILL     = 0xa503,
    RI_LGHI     = 0xa709,
    RI_LLIHH    = 0xa50c,
    RI_LLIHL    = 0xa50d,
    RI_LLILH    = 0xa50e,
    RI_LLILL    = 0xa50f,
    RI_MGHI     = 0xa70d,
    RI_MHI      = 0xa70c,
    RI_NIHH     = 0xa504,
    RI_NIHL     = 0xa505,
    RI_NILH     = 0xa506,
    RI_NILL     = 0xa507,
    RI_OIHH     = 0xa508,
    RI_OIHL     = 0xa509,
    RI_OILH     = 0xa50a,
    RI_OILL     = 0xa50b,

    RIE_CGIJ    = 0xec7c,
    RIE_CGRJ    = 0xec64,
    RIE_CIJ     = 0xec7e,
    RIE_CLGRJ   = 0xec65,
    RIE_CLIJ    = 0xec7f,
    RIE_CLGIJ   = 0xec7d,
    RIE_CLRJ    = 0xec77,
    RIE_CRJ     = 0xec76,
123
    RIE_RISBG   = 0xec55,
R
Richard Henderson 已提交
124 125

    RRE_AGR     = 0xb908,
126 127 128
    RRE_ALGR    = 0xb90a,
    RRE_ALCR    = 0xb998,
    RRE_ALCGR   = 0xb988,
R
Richard Henderson 已提交
129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145
    RRE_CGR     = 0xb920,
    RRE_CLGR    = 0xb921,
    RRE_DLGR    = 0xb987,
    RRE_DLR     = 0xb997,
    RRE_DSGFR   = 0xb91d,
    RRE_DSGR    = 0xb90d,
    RRE_LGBR    = 0xb906,
    RRE_LCGR    = 0xb903,
    RRE_LGFR    = 0xb914,
    RRE_LGHR    = 0xb907,
    RRE_LGR     = 0xb904,
    RRE_LLGCR   = 0xb984,
    RRE_LLGFR   = 0xb916,
    RRE_LLGHR   = 0xb985,
    RRE_LRVR    = 0xb91f,
    RRE_LRVGR   = 0xb90f,
    RRE_LTGR    = 0xb902,
146
    RRE_MLGR    = 0xb986,
R
Richard Henderson 已提交
147 148 149 150 151
    RRE_MSGR    = 0xb90c,
    RRE_MSR     = 0xb252,
    RRE_NGR     = 0xb980,
    RRE_OGR     = 0xb981,
    RRE_SGR     = 0xb909,
152 153 154
    RRE_SLGR    = 0xb90b,
    RRE_SLBR    = 0xb999,
    RRE_SLBGR   = 0xb989,
R
Richard Henderson 已提交
155 156
    RRE_XGR     = 0xb982,

157 158 159
    RRF_LOCR    = 0xb9f2,
    RRF_LOCGR   = 0xb9e2,

R
Richard Henderson 已提交
160
    RR_AR       = 0x1a,
161
    RR_ALR      = 0x1e,
R
Richard Henderson 已提交
162 163 164 165 166 167 168 169 170 171 172
    RR_BASR     = 0x0d,
    RR_BCR      = 0x07,
    RR_CLR      = 0x15,
    RR_CR       = 0x19,
    RR_DR       = 0x1d,
    RR_LCR      = 0x13,
    RR_LR       = 0x18,
    RR_LTR      = 0x12,
    RR_NR       = 0x14,
    RR_OR       = 0x16,
    RR_SR       = 0x1b,
173
    RR_SLR      = 0x1f,
R
Richard Henderson 已提交
174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189
    RR_XR       = 0x17,

    RSY_RLL     = 0xeb1d,
    RSY_RLLG    = 0xeb1c,
    RSY_SLLG    = 0xeb0d,
    RSY_SRAG    = 0xeb0a,
    RSY_SRLG    = 0xeb0c,

    RS_SLL      = 0x89,
    RS_SRA      = 0x8a,
    RS_SRL      = 0x88,

    RXY_AG      = 0xe308,
    RXY_AY      = 0xe35a,
    RXY_CG      = 0xe320,
    RXY_CY      = 0xe359,
190
    RXY_LAY     = 0xe371,
R
Richard Henderson 已提交
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
    RXY_LB      = 0xe376,
    RXY_LG      = 0xe304,
    RXY_LGB     = 0xe377,
    RXY_LGF     = 0xe314,
    RXY_LGH     = 0xe315,
    RXY_LHY     = 0xe378,
    RXY_LLGC    = 0xe390,
    RXY_LLGF    = 0xe316,
    RXY_LLGH    = 0xe391,
    RXY_LMG     = 0xeb04,
    RXY_LRV     = 0xe31e,
    RXY_LRVG    = 0xe30f,
    RXY_LRVH    = 0xe31f,
    RXY_LY      = 0xe358,
    RXY_STCY    = 0xe372,
    RXY_STG     = 0xe324,
    RXY_STHY    = 0xe370,
    RXY_STMG    = 0xeb24,
    RXY_STRV    = 0xe33e,
    RXY_STRVG   = 0xe32f,
    RXY_STRVH   = 0xe33f,
    RXY_STY     = 0xe350,

    RX_A        = 0x5a,
    RX_C        = 0x59,
    RX_L        = 0x58,
217
    RX_LA       = 0x41,
R
Richard Henderson 已提交
218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233
    RX_LH       = 0x48,
    RX_ST       = 0x50,
    RX_STC      = 0x42,
    RX_STH      = 0x40,
} S390Opcode;

#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"
};
#endif

/* Since R6 is a potential argument register, choose it last of the
   call-saved registers.  Likewise prefer the call-clobbered registers
   in reverse order to maximize the chance of avoiding the arguments.  */
A
Alexander Graf 已提交
234
static const int tcg_target_reg_alloc_order[] = {
235
    /* Call saved registers.  */
R
Richard Henderson 已提交
236 237 238 239 240 241 242 243
    TCG_REG_R13,
    TCG_REG_R12,
    TCG_REG_R11,
    TCG_REG_R10,
    TCG_REG_R9,
    TCG_REG_R8,
    TCG_REG_R7,
    TCG_REG_R6,
244
    /* Call clobbered registers.  */
R
Richard Henderson 已提交
245 246 247
    TCG_REG_R14,
    TCG_REG_R0,
    TCG_REG_R1,
248
    /* Argument registers, in reverse order of allocation.  */
R
Richard Henderson 已提交
249 250 251 252
    TCG_REG_R5,
    TCG_REG_R4,
    TCG_REG_R3,
    TCG_REG_R2,
A
Alexander Graf 已提交
253 254 255
};

static const int tcg_target_call_iarg_regs[] = {
R
Richard Henderson 已提交
256 257 258 259 260
    TCG_REG_R2,
    TCG_REG_R3,
    TCG_REG_R4,
    TCG_REG_R5,
    TCG_REG_R6,
A
Alexander Graf 已提交
261 262 263
};

static const int tcg_target_call_oarg_regs[] = {
R
Richard Henderson 已提交
264 265 266 267 268 269 270 271 272 273 274 275 276 277
    TCG_REG_R2,
};

#define S390_CC_EQ      8
#define S390_CC_LT      4
#define S390_CC_GT      2
#define S390_CC_OV      1
#define S390_CC_NE      (S390_CC_LT | S390_CC_GT)
#define S390_CC_LE      (S390_CC_LT | S390_CC_EQ)
#define S390_CC_GE      (S390_CC_GT | S390_CC_EQ)
#define S390_CC_NEVER   0
#define S390_CC_ALWAYS  15

/* Condition codes that result from a COMPARE and COMPARE LOGICAL.  */
278
static const uint8_t tcg_cond_to_s390_cond[] = {
R
Richard Henderson 已提交
279 280 281 282 283 284 285 286 287 288 289 290 291 292 293
    [TCG_COND_EQ]  = S390_CC_EQ,
    [TCG_COND_NE]  = S390_CC_NE,
    [TCG_COND_LT]  = S390_CC_LT,
    [TCG_COND_LE]  = S390_CC_LE,
    [TCG_COND_GT]  = S390_CC_GT,
    [TCG_COND_GE]  = S390_CC_GE,
    [TCG_COND_LTU] = S390_CC_LT,
    [TCG_COND_LEU] = S390_CC_LE,
    [TCG_COND_GTU] = S390_CC_GT,
    [TCG_COND_GEU] = S390_CC_GE,
};

/* Condition codes that result from a LOAD AND TEST.  Here, we have no
   unsigned instruction variation, however since the test is vs zero we
   can re-map the outcomes appropriately.  */
294
static const uint8_t tcg_cond_to_ltr_cond[] = {
R
Richard Henderson 已提交
295 296 297 298 299 300 301 302 303 304 305 306 307
    [TCG_COND_EQ]  = S390_CC_EQ,
    [TCG_COND_NE]  = S390_CC_NE,
    [TCG_COND_LT]  = S390_CC_LT,
    [TCG_COND_LE]  = S390_CC_LE,
    [TCG_COND_GT]  = S390_CC_GT,
    [TCG_COND_GE]  = S390_CC_GE,
    [TCG_COND_LTU] = S390_CC_NEVER,
    [TCG_COND_LEU] = S390_CC_EQ,
    [TCG_COND_GTU] = S390_CC_NE,
    [TCG_COND_GEU] = S390_CC_ALWAYS,
};

#ifdef CONFIG_SOFTMMU
308 309 310 311 312 313 314 315 316 317 318 319 320
static void * const qemu_ld_helpers[16] = {
    [MO_UB]   = helper_ret_ldub_mmu,
    [MO_SB]   = helper_ret_ldsb_mmu,
    [MO_LEUW] = helper_le_lduw_mmu,
    [MO_LESW] = helper_le_ldsw_mmu,
    [MO_LEUL] = helper_le_ldul_mmu,
    [MO_LESL] = helper_le_ldsl_mmu,
    [MO_LEQ]  = helper_le_ldq_mmu,
    [MO_BEUW] = helper_be_lduw_mmu,
    [MO_BESW] = helper_be_ldsw_mmu,
    [MO_BEUL] = helper_be_ldul_mmu,
    [MO_BESL] = helper_be_ldsl_mmu,
    [MO_BEQ]  = helper_be_ldq_mmu,
321 322
};

323 324 325 326 327 328 329 330
static void * const qemu_st_helpers[16] = {
    [MO_UB]   = helper_ret_stb_mmu,
    [MO_LEUW] = helper_le_stw_mmu,
    [MO_LEUL] = helper_le_stl_mmu,
    [MO_LEQ]  = helper_le_stq_mmu,
    [MO_BEUW] = helper_be_stw_mmu,
    [MO_BEUL] = helper_be_stl_mmu,
    [MO_BEQ]  = helper_be_stq_mmu,
331 332
};
#endif
R
Richard Henderson 已提交
333

334
static tcg_insn_unit *tb_ret_addr;
R
Richard Henderson 已提交
335 336 337 338 339 340 341 342

/* A list of relevant facilities used by this translator.  Some of these
   are required for proper operation, and these are checked at startup.  */

#define FACILITY_ZARCH_ACTIVE	(1ULL << (63 - 2))
#define FACILITY_LONG_DISP	(1ULL << (63 - 18))
#define FACILITY_EXT_IMM	(1ULL << (63 - 21))
#define FACILITY_GEN_INST_EXT	(1ULL << (63 - 34))
343
#define FACILITY_LOAD_ON_COND   (1ULL << (63 - 45))
R
Richard Henderson 已提交
344 345

static uint64_t facilities;
A
Alexander Graf 已提交
346

347
static void patch_reloc(tcg_insn_unit *code_ptr, int type,
348
                        intptr_t value, intptr_t addend)
A
Alexander Graf 已提交
349
{
350 351
    intptr_t pcrel2 = (tcg_insn_unit *)value - (code_ptr - 1);
    assert(addend == -2);
R
Richard Henderson 已提交
352 353 354 355

    switch (type) {
    case R_390_PC16DBL:
        assert(pcrel2 == (int16_t)pcrel2);
356
        tcg_patch16(code_ptr, pcrel2);
R
Richard Henderson 已提交
357 358 359
        break;
    case R_390_PC32DBL:
        assert(pcrel2 == (int32_t)pcrel2);
360
        tcg_patch32(code_ptr, pcrel2);
R
Richard Henderson 已提交
361 362 363 364 365
        break;
    default:
        tcg_abort();
        break;
    }
A
Alexander Graf 已提交
366 367 368 369 370
}

/* parse target specific constraints */
static int target_parse_constraint(TCGArgConstraint *ct, const char **pct_str)
{
R
Richard Henderson 已提交
371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387
    const char *ct_str = *pct_str;

    switch (ct_str[0]) {
    case 'r':                  /* all registers */
        ct->ct |= TCG_CT_REG;
        tcg_regset_set32(ct->u.regs, 0, 0xffff);
        break;
    case 'R':                  /* not R0 */
        ct->ct |= TCG_CT_REG;
        tcg_regset_set32(ct->u.regs, 0, 0xffff);
        tcg_regset_reset_reg(ct->u.regs, TCG_REG_R0);
        break;
    case 'L':                  /* qemu_ld/st constraint */
        ct->ct |= TCG_CT_REG;
        tcg_regset_set32(ct->u.regs, 0, 0xffff);
        tcg_regset_reset_reg (ct->u.regs, TCG_REG_R2);
        tcg_regset_reset_reg (ct->u.regs, TCG_REG_R3);
388
        tcg_regset_reset_reg (ct->u.regs, TCG_REG_R4);
R
Richard Henderson 已提交
389 390 391 392 393 394 395 396 397 398 399
        break;
    case 'a':                  /* force R2 for division */
        ct->ct |= TCG_CT_REG;
        tcg_regset_clear(ct->u.regs);
        tcg_regset_set_reg(ct->u.regs, TCG_REG_R2);
        break;
    case 'b':                  /* force R3 for division */
        ct->ct |= TCG_CT_REG;
        tcg_regset_clear(ct->u.regs);
        tcg_regset_set_reg(ct->u.regs, TCG_REG_R3);
        break;
400 401 402
    case 'A':
        ct->ct |= TCG_CT_CONST_ADLI;
        break;
R
Richard Henderson 已提交
403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420
    case 'K':
        ct->ct |= TCG_CT_CONST_MULI;
        break;
    case 'O':
        ct->ct |= TCG_CT_CONST_ORI;
        break;
    case 'X':
        ct->ct |= TCG_CT_CONST_XORI;
        break;
    case 'C':
        ct->ct |= TCG_CT_CONST_CMPI;
        break;
    default:
        return -1;
    }
    ct_str++;
    *pct_str = ct_str;

A
Alexander Graf 已提交
421 422 423
    return 0;
}

R
Richard Henderson 已提交
424 425 426 427 428 429
/* Immediates to be used with logical OR.  This is an optimization only,
   since a full 64-bit immediate OR can always be performed with 4 sequential
   OI[LH][LH] instructions.  What we're looking for is immediates that we
   can load efficiently, and the immediate load plus the reg-reg OR is
   smaller than the sequential OI's.  */

430
static int tcg_match_ori(TCGType type, tcg_target_long val)
R
Richard Henderson 已提交
431 432
{
    if (facilities & FACILITY_EXT_IMM) {
433
        if (type == TCG_TYPE_I32) {
R
Richard Henderson 已提交
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
            /* All 32-bit ORs can be performed with 1 48-bit insn.  */
            return 1;
        }
    }

    /* Look for negative values.  These are best to load with LGHI.  */
    if (val < 0) {
        if (val == (int16_t)val) {
            return 0;
        }
        if (facilities & FACILITY_EXT_IMM) {
            if (val == (int32_t)val) {
                return 0;
            }
        }
    }

    return 1;
}

/* Immediates to be used with logical XOR.  This is almost, but not quite,
   only an optimization.  XOR with immediate is only supported with the
   extended-immediate facility.  That said, there are a few patterns for
   which it is better to load the value into a register first.  */

459
static int tcg_match_xori(TCGType type, tcg_target_long val)
R
Richard Henderson 已提交
460 461 462 463 464
{
    if ((facilities & FACILITY_EXT_IMM) == 0) {
        return 0;
    }

465
    if (type == TCG_TYPE_I32) {
R
Richard Henderson 已提交
466 467 468 469 470 471 472 473 474 475 476 477 478 479
        /* All 32-bit XORs can be performed with 1 48-bit insn.  */
        return 1;
    }

    /* Look for negative values.  These are best to load with LGHI.  */
    if (val < 0 && val == (int32_t)val) {
        return 0;
    }

    return 1;
}

/* Imediates to be used with comparisons.  */

480
static int tcg_match_cmpi(TCGType type, tcg_target_long val)
R
Richard Henderson 已提交
481 482 483
{
    if (facilities & FACILITY_EXT_IMM) {
        /* The COMPARE IMMEDIATE instruction is available.  */
484
        if (type == TCG_TYPE_I32) {
R
Richard Henderson 已提交
485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506
            /* We have a 32-bit immediate and can compare against anything.  */
            return 1;
        } else {
            /* ??? We have no insight here into whether the comparison is
               signed or unsigned.  The COMPARE IMMEDIATE insn uses a 32-bit
               signed immediate, and the COMPARE LOGICAL IMMEDIATE insn uses
               a 32-bit unsigned immediate.  If we were to use the (semi)
               obvious "val == (int32_t)val" we would be enabling unsigned
               comparisons vs very large numbers.  The only solution is to
               take the intersection of the ranges.  */
            /* ??? Another possible solution is to simply lie and allow all
               constants here and force the out-of-range values into a temp
               register in tgen_cmp when we have knowledge of the actual
               comparison code in use.  */
            return val >= 0 && val <= 0x7fffffff;
        }
    } else {
        /* Only the LOAD AND TEST instruction is available.  */
        return val == 0;
    }
}

507 508 509 510 511 512 513 514 515 516 517 518 519 520
/* Immediates to be used with add2/sub2.  */

static int tcg_match_add2i(TCGType type, tcg_target_long val)
{
    if (facilities & FACILITY_EXT_IMM) {
        if (type == TCG_TYPE_I32) {
            return 1;
        } else if (val >= -0xffffffffll && val <= 0xffffffffll) {
            return 1;
        }
    }
    return 0;
}

A
Alexander Graf 已提交
521
/* Test if a constant matches the constraint. */
522
static int tcg_target_const_match(tcg_target_long val, TCGType type,
R
Richard Henderson 已提交
523
                                  const TCGArgConstraint *arg_ct)
A
Alexander Graf 已提交
524
{
R
Richard Henderson 已提交
525 526 527 528 529 530
    int ct = arg_ct->ct;

    if (ct & TCG_CT_CONST) {
        return 1;
    }

531
    if (type == TCG_TYPE_I32) {
R
Richard Henderson 已提交
532 533 534 535
        val = (int32_t)val;
    }

    /* The following are mutually exclusive.  */
536
    if (ct & TCG_CT_CONST_MULI) {
R
Richard Henderson 已提交
537 538 539 540 541 542 543 544 545
        /* Immediates that may be used with multiply.  If we have the
           general-instruction-extensions, then we have MULTIPLY SINGLE
           IMMEDIATE with a signed 32-bit, otherwise we have only
           MULTIPLY HALFWORD IMMEDIATE, with a signed 16-bit.  */
        if (facilities & FACILITY_GEN_INST_EXT) {
            return val == (int32_t)val;
        } else {
            return val == (int16_t)val;
        }
546 547
    } else if (ct & TCG_CT_CONST_ADLI) {
        return tcg_match_add2i(type, val);
R
Richard Henderson 已提交
548
    } else if (ct & TCG_CT_CONST_ORI) {
549
        return tcg_match_ori(type, val);
R
Richard Henderson 已提交
550
    } else if (ct & TCG_CT_CONST_XORI) {
551
        return tcg_match_xori(type, val);
R
Richard Henderson 已提交
552
    } else if (ct & TCG_CT_CONST_CMPI) {
553
        return tcg_match_cmpi(type, val);
R
Richard Henderson 已提交
554 555
    }

A
Alexander Graf 已提交
556 557 558
    return 0;
}

R
Richard Henderson 已提交
559 560 561 562 563 564 565 566 567 568 569 570 571
/* Emit instructions according to the given instruction format.  */

static void tcg_out_insn_RR(TCGContext *s, S390Opcode op, TCGReg r1, TCGReg r2)
{
    tcg_out16(s, (op << 8) | (r1 << 4) | r2);
}

static void tcg_out_insn_RRE(TCGContext *s, S390Opcode op,
                             TCGReg r1, TCGReg r2)
{
    tcg_out32(s, (op << 16) | (r1 << 4) | r2);
}

572 573 574 575 576 577
static void tcg_out_insn_RRF(TCGContext *s, S390Opcode op,
                             TCGReg r1, TCGReg r2, int m3)
{
    tcg_out32(s, (op << 16) | (m3 << 12) | (r1 << 4) | r2);
}

R
Richard Henderson 已提交
578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636
static void tcg_out_insn_RI(TCGContext *s, S390Opcode op, TCGReg r1, int i2)
{
    tcg_out32(s, (op << 16) | (r1 << 20) | (i2 & 0xffff));
}

static void tcg_out_insn_RIL(TCGContext *s, S390Opcode op, TCGReg r1, int i2)
{
    tcg_out16(s, op | (r1 << 4));
    tcg_out32(s, i2);
}

static void tcg_out_insn_RS(TCGContext *s, S390Opcode op, TCGReg r1,
                            TCGReg b2, TCGReg r3, int disp)
{
    tcg_out32(s, (op << 24) | (r1 << 20) | (r3 << 16) | (b2 << 12)
              | (disp & 0xfff));
}

static void tcg_out_insn_RSY(TCGContext *s, S390Opcode op, TCGReg r1,
                             TCGReg b2, TCGReg r3, int disp)
{
    tcg_out16(s, (op & 0xff00) | (r1 << 4) | r3);
    tcg_out32(s, (op & 0xff) | (b2 << 28)
              | ((disp & 0xfff) << 16) | ((disp & 0xff000) >> 4));
}

#define tcg_out_insn_RX   tcg_out_insn_RS
#define tcg_out_insn_RXY  tcg_out_insn_RSY

/* Emit an opcode with "type-checking" of the format.  */
#define tcg_out_insn(S, FMT, OP, ...) \
    glue(tcg_out_insn_,FMT)(S, glue(glue(FMT,_),OP), ## __VA_ARGS__)


/* emit 64-bit shifts */
static void tcg_out_sh64(TCGContext* s, S390Opcode op, TCGReg dest,
                         TCGReg src, TCGReg sh_reg, int sh_imm)
{
    tcg_out_insn_RSY(s, op, dest, sh_reg, src, sh_imm);
}

/* emit 32-bit shifts */
static void tcg_out_sh32(TCGContext* s, S390Opcode op, TCGReg dest,
                         TCGReg sh_reg, int sh_imm)
{
    tcg_out_insn_RS(s, op, dest, sh_reg, 0, sh_imm);
}

static void tcg_out_mov(TCGContext *s, TCGType type, TCGReg dst, TCGReg src)
{
    if (src != dst) {
        if (type == TCG_TYPE_I32) {
            tcg_out_insn(s, RR, LR, dst, src);
        } else {
            tcg_out_insn(s, RRE, LGR, dst, src);
        }
    }
}

A
Alexander Graf 已提交
637
/* load a register with an immediate value */
R
Richard Henderson 已提交
638 639
static void tcg_out_movi(TCGContext *s, TCGType type,
                         TCGReg ret, tcg_target_long sval)
A
Alexander Graf 已提交
640
{
R
Richard Henderson 已提交
641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684
    static const S390Opcode lli_insns[4] = {
        RI_LLILL, RI_LLILH, RI_LLIHL, RI_LLIHH
    };

    tcg_target_ulong uval = sval;
    int i;

    if (type == TCG_TYPE_I32) {
        uval = (uint32_t)sval;
        sval = (int32_t)sval;
    }

    /* Try all 32-bit insns that can load it in one go.  */
    if (sval >= -0x8000 && sval < 0x8000) {
        tcg_out_insn(s, RI, LGHI, ret, sval);
        return;
    }

    for (i = 0; i < 4; i++) {
        tcg_target_long mask = 0xffffull << i*16;
        if ((uval & mask) == uval) {
            tcg_out_insn_RI(s, lli_insns[i], ret, uval >> i*16);
            return;
        }
    }

    /* Try all 48-bit insns that can load it in one go.  */
    if (facilities & FACILITY_EXT_IMM) {
        if (sval == (int32_t)sval) {
            tcg_out_insn(s, RIL, LGFI, ret, sval);
            return;
        }
        if (uval <= 0xffffffff) {
            tcg_out_insn(s, RIL, LLILF, ret, uval);
            return;
        }
        if ((uval & 0xffffffff) == 0) {
            tcg_out_insn(s, RIL, LLIHF, ret, uval >> 31 >> 1);
            return;
        }
    }

    /* Try for PC-relative address load.  */
    if ((sval & 1) == 0) {
685
        ptrdiff_t off = tcg_pcrel_diff(s, (void *)sval) >> 1;
R
Richard Henderson 已提交
686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722
        if (off == (int32_t)off) {
            tcg_out_insn(s, RIL, LARL, ret, off);
            return;
        }
    }

    /* If extended immediates are not present, then we may have to issue
       several instructions to load the low 32 bits.  */
    if (!(facilities & FACILITY_EXT_IMM)) {
        /* A 32-bit unsigned value can be loaded in 2 insns.  And given
           that the lli_insns loop above did not succeed, we know that
           both insns are required.  */
        if (uval <= 0xffffffff) {
            tcg_out_insn(s, RI, LLILL, ret, uval);
            tcg_out_insn(s, RI, IILH, ret, uval >> 16);
            return;
        }

        /* If all high bits are set, the value can be loaded in 2 or 3 insns.
           We first want to make sure that all the high bits get set.  With
           luck the low 16-bits can be considered negative to perform that for
           free, otherwise we load an explicit -1.  */
        if (sval >> 31 >> 1 == -1) {
            if (uval & 0x8000) {
                tcg_out_insn(s, RI, LGHI, ret, uval);
            } else {
                tcg_out_insn(s, RI, LGHI, ret, -1);
                tcg_out_insn(s, RI, IILL, ret, uval);
            }
            tcg_out_insn(s, RI, IILH, ret, uval >> 16);
            return;
        }
    }

    /* If we get here, both the high and low parts have non-zero bits.  */

    /* Recurse to load the lower 32-bits.  */
R
Richard Henderson 已提交
723
    tcg_out_movi(s, TCG_TYPE_I64, ret, uval & 0xffffffff);
R
Richard Henderson 已提交
724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756

    /* Insert data into the high 32-bits.  */
    uval = uval >> 31 >> 1;
    if (facilities & FACILITY_EXT_IMM) {
        if (uval < 0x10000) {
            tcg_out_insn(s, RI, IIHL, ret, uval);
        } else if ((uval & 0xffff) == 0) {
            tcg_out_insn(s, RI, IIHH, ret, uval >> 16);
        } else {
            tcg_out_insn(s, RIL, IIHF, ret, uval);
        }
    } else {
        if (uval & 0xffff) {
            tcg_out_insn(s, RI, IIHL, ret, uval);
        }
        if (uval & 0xffff0000) {
            tcg_out_insn(s, RI, IIHH, ret, uval >> 16);
        }
    }
}


/* Emit a load/store type instruction.  Inputs are:
   DATA:     The register to be loaded or stored.
   BASE+OFS: The effective address.
   OPC_RX:   If the operation has an RX format opcode (e.g. STC), otherwise 0.
   OPC_RXY:  The RXY format opcode for the operation (e.g. STCY).  */

static void tcg_out_mem(TCGContext *s, S390Opcode opc_rx, S390Opcode opc_rxy,
                        TCGReg data, TCGReg base, TCGReg index,
                        tcg_target_long ofs)
{
    if (ofs < -0x80000 || ofs >= 0x80000) {
757 758 759 760 761
        /* Combine the low 20 bits of the offset with the actual load insn;
           the high 44 bits must come from an immediate load.  */
        tcg_target_long low = ((ofs & 0xfffff) ^ 0x80000) - 0x80000;
        tcg_out_movi(s, TCG_TYPE_PTR, TCG_TMP0, ofs - low);
        ofs = low;
R
Richard Henderson 已提交
762 763 764 765 766 767 768 769 770 771 772 773 774

        /* If we were already given an index register, add it in.  */
        if (index != TCG_REG_NONE) {
            tcg_out_insn(s, RRE, AGR, TCG_TMP0, index);
        }
        index = TCG_TMP0;
    }

    if (opc_rx && ofs >= 0 && ofs < 0x1000) {
        tcg_out_insn_RX(s, opc_rx, data, base, index, ofs);
    } else {
        tcg_out_insn_RXY(s, opc_rxy, data, base, index, ofs);
    }
A
Alexander Graf 已提交
775 776
}

R
Richard Henderson 已提交
777

A
Alexander Graf 已提交
778
/* load data without address translation or endianness conversion */
R
Richard Henderson 已提交
779
static inline void tcg_out_ld(TCGContext *s, TCGType type, TCGReg data,
780
                              TCGReg base, intptr_t ofs)
A
Alexander Graf 已提交
781
{
R
Richard Henderson 已提交
782 783 784 785 786
    if (type == TCG_TYPE_I32) {
        tcg_out_mem(s, RX_L, RXY_LY, data, base, TCG_REG_NONE, ofs);
    } else {
        tcg_out_mem(s, 0, RXY_LG, data, base, TCG_REG_NONE, ofs);
    }
A
Alexander Graf 已提交
787 788
}

R
Richard Henderson 已提交
789
static inline void tcg_out_st(TCGContext *s, TCGType type, TCGReg data,
790
                              TCGReg base, intptr_t ofs)
A
Alexander Graf 已提交
791
{
R
Richard Henderson 已提交
792 793 794 795 796 797 798 799 800 801
    if (type == TCG_TYPE_I32) {
        tcg_out_mem(s, RX_ST, RXY_STY, data, base, TCG_REG_NONE, ofs);
    } else {
        tcg_out_mem(s, 0, RXY_STG, data, base, TCG_REG_NONE, ofs);
    }
}

/* load data from an absolute host address */
static void tcg_out_ld_abs(TCGContext *s, TCGType type, TCGReg dest, void *abs)
{
802
    intptr_t addr = (intptr_t)abs;
R
Richard Henderson 已提交
803

804 805
    if ((facilities & FACILITY_GEN_INST_EXT) && !(addr & 1)) {
        ptrdiff_t disp = tcg_pcrel_diff(s, abs) >> 1;
R
Richard Henderson 已提交
806 807 808 809 810 811 812 813 814 815 816 817 818 819
        if (disp == (int32_t)disp) {
            if (type == TCG_TYPE_I32) {
                tcg_out_insn(s, RIL, LRL, dest, disp);
            } else {
                tcg_out_insn(s, RIL, LGRL, dest, disp);
            }
            return;
        }
    }

    tcg_out_movi(s, TCG_TYPE_PTR, dest, addr & ~0xffff);
    tcg_out_ld(s, type, dest, dest, addr & 0xffff);
}

820 821 822 823 824 825 826 827 828
static inline void tcg_out_risbg(TCGContext *s, TCGReg dest, TCGReg src,
                                 int msb, int lsb, int ofs, int z)
{
    /* Format RIE-f */
    tcg_out16(s, (RIE_RISBG & 0xff00) | (dest << 4) | src);
    tcg_out16(s, (msb << 8) | (z << 7) | lsb);
    tcg_out16(s, (ofs << 8) | (RIE_RISBG & 0xff));
}

R
Richard Henderson 已提交
829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918
static void tgen_ext8s(TCGContext *s, TCGType type, TCGReg dest, TCGReg src)
{
    if (facilities & FACILITY_EXT_IMM) {
        tcg_out_insn(s, RRE, LGBR, dest, src);
        return;
    }

    if (type == TCG_TYPE_I32) {
        if (dest == src) {
            tcg_out_sh32(s, RS_SLL, dest, TCG_REG_NONE, 24);
        } else {
            tcg_out_sh64(s, RSY_SLLG, dest, src, TCG_REG_NONE, 24);
        }
        tcg_out_sh32(s, RS_SRA, dest, TCG_REG_NONE, 24);
    } else {
        tcg_out_sh64(s, RSY_SLLG, dest, src, TCG_REG_NONE, 56);
        tcg_out_sh64(s, RSY_SRAG, dest, dest, TCG_REG_NONE, 56);
    }
}

static void tgen_ext8u(TCGContext *s, TCGType type, TCGReg dest, TCGReg src)
{
    if (facilities & FACILITY_EXT_IMM) {
        tcg_out_insn(s, RRE, LLGCR, dest, src);
        return;
    }

    if (dest == src) {
        tcg_out_movi(s, type, TCG_TMP0, 0xff);
        src = TCG_TMP0;
    } else {
        tcg_out_movi(s, type, dest, 0xff);
    }
    if (type == TCG_TYPE_I32) {
        tcg_out_insn(s, RR, NR, dest, src);
    } else {
        tcg_out_insn(s, RRE, NGR, dest, src);
    }
}

static void tgen_ext16s(TCGContext *s, TCGType type, TCGReg dest, TCGReg src)
{
    if (facilities & FACILITY_EXT_IMM) {
        tcg_out_insn(s, RRE, LGHR, dest, src);
        return;
    }

    if (type == TCG_TYPE_I32) {
        if (dest == src) {
            tcg_out_sh32(s, RS_SLL, dest, TCG_REG_NONE, 16);
        } else {
            tcg_out_sh64(s, RSY_SLLG, dest, src, TCG_REG_NONE, 16);
        }
        tcg_out_sh32(s, RS_SRA, dest, TCG_REG_NONE, 16);
    } else {
        tcg_out_sh64(s, RSY_SLLG, dest, src, TCG_REG_NONE, 48);
        tcg_out_sh64(s, RSY_SRAG, dest, dest, TCG_REG_NONE, 48);
    }
}

static void tgen_ext16u(TCGContext *s, TCGType type, TCGReg dest, TCGReg src)
{
    if (facilities & FACILITY_EXT_IMM) {
        tcg_out_insn(s, RRE, LLGHR, dest, src);
        return;
    }

    if (dest == src) {
        tcg_out_movi(s, type, TCG_TMP0, 0xffff);
        src = TCG_TMP0;
    } else {
        tcg_out_movi(s, type, dest, 0xffff);
    }
    if (type == TCG_TYPE_I32) {
        tcg_out_insn(s, RR, NR, dest, src);
    } else {
        tcg_out_insn(s, RRE, NGR, dest, src);
    }
}

static inline void tgen_ext32s(TCGContext *s, TCGReg dest, TCGReg src)
{
    tcg_out_insn(s, RRE, LGFR, dest, src);
}

static inline void tgen_ext32u(TCGContext *s, TCGReg dest, TCGReg src)
{
    tcg_out_insn(s, RRE, LLGFR, dest, src);
}

919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948
/* Accept bit patterns like these:
    0....01....1
    1....10....0
    1..10..01..1
    0..01..10..0
   Copied from gcc sources.  */
static inline bool risbg_mask(uint64_t c)
{
    uint64_t lsb;
    /* We don't change the number of transitions by inverting,
       so make sure we start with the LSB zero.  */
    if (c & 1) {
        c = ~c;
    }
    /* Reject all zeros or all ones.  */
    if (c == 0) {
        return false;
    }
    /* Find the first transition.  */
    lsb = c & -c;
    /* Invert to look for a second transition.  */
    c = ~c;
    /* Erase the first transition.  */
    c &= -lsb;
    /* Find the second transition, if any.  */
    lsb = c & -c;
    /* Match if all the bits are 1's, or if c is zero.  */
    return c == -lsb;
}

949 950 951 952 953 954 955 956 957 958 959 960 961 962
static void tgen_andi_risbg(TCGContext *s, TCGReg out, TCGReg in, uint64_t val)
{
    int msb, lsb;
    if ((val & 0x8000000000000001ull) == 0x8000000000000001ull) {
        /* Achieve wraparound by swapping msb and lsb.  */
        msb = 64 - ctz64(~val);
        lsb = clz64(~val) - 1;
    } else {
        msb = clz64(val);
        lsb = 63 - ctz64(val);
    }
    tcg_out_risbg(s, out, in, msb, lsb, 0, 1);
}

963
static void tgen_andi(TCGContext *s, TCGType type, TCGReg dest, uint64_t val)
R
Richard Henderson 已提交
964 965 966 967 968 969 970
{
    static const S390Opcode ni_insns[4] = {
        RI_NILL, RI_NILH, RI_NIHL, RI_NIHH
    };
    static const S390Opcode nif_insns[2] = {
        RIL_NILF, RIL_NIHF
    };
971
    uint64_t valid = (type == TCG_TYPE_I32 ? 0xffffffffull : -1ull);
R
Richard Henderson 已提交
972 973 974
    int i;

    /* Look for the zero-extensions.  */
975
    if ((val & valid) == 0xffffffff) {
R
Richard Henderson 已提交
976 977 978 979
        tgen_ext32u(s, dest, dest);
        return;
    }
    if (facilities & FACILITY_EXT_IMM) {
980
        if ((val & valid) == 0xff) {
R
Richard Henderson 已提交
981 982 983
            tgen_ext8u(s, TCG_TYPE_I64, dest, dest);
            return;
        }
984
        if ((val & valid) == 0xffff) {
R
Richard Henderson 已提交
985 986 987
            tgen_ext16u(s, TCG_TYPE_I64, dest, dest);
            return;
        }
988
    }
R
Richard Henderson 已提交
989

990 991 992 993 994 995
    /* Try all 32-bit insns that can perform it in one go.  */
    for (i = 0; i < 4; i++) {
        tcg_target_ulong mask = ~(0xffffull << i*16);
        if (((val | ~valid) & mask) == mask) {
            tcg_out_insn_RI(s, ni_insns[i], dest, val >> i*16);
            return;
R
Richard Henderson 已提交
996
        }
997
    }
R
Richard Henderson 已提交
998

999 1000 1001 1002 1003 1004 1005
    /* Try all 48-bit insns that can perform it in one go.  */
    if (facilities & FACILITY_EXT_IMM) {
        for (i = 0; i < 2; i++) {
            tcg_target_ulong mask = ~(0xffffffffull << i*32);
            if (((val | ~valid) & mask) == mask) {
                tcg_out_insn_RIL(s, nif_insns[i], dest, val >> i*32);
                return;
R
Richard Henderson 已提交
1006 1007
            }
        }
1008
    }
1009
    if ((facilities & FACILITY_GEN_INST_EXT) && risbg_mask(val)) {
1010
        tgen_andi_risbg(s, dest, dest, val);
1011 1012
        return;
    }
R
Richard Henderson 已提交
1013

1014 1015 1016 1017
    /* Fall back to loading the constant.  */
    tcg_out_movi(s, type, TCG_TMP0, val);
    if (type == TCG_TYPE_I32) {
        tcg_out_insn(s, RR, NR, dest, TCG_TMP0);
R
Richard Henderson 已提交
1018
    } else {
1019
        tcg_out_insn(s, RRE, NGR, dest, TCG_TMP0);
R
Richard Henderson 已提交
1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088
    }
}

static void tgen64_ori(TCGContext *s, TCGReg dest, tcg_target_ulong val)
{
    static const S390Opcode oi_insns[4] = {
        RI_OILL, RI_OILH, RI_OIHL, RI_OIHH
    };
    static const S390Opcode nif_insns[2] = {
        RIL_OILF, RIL_OIHF
    };

    int i;

    /* Look for no-op.  */
    if (val == 0) {
        return;
    }

    if (facilities & FACILITY_EXT_IMM) {
        /* Try all 32-bit insns that can perform it in one go.  */
        for (i = 0; i < 4; i++) {
            tcg_target_ulong mask = (0xffffull << i*16);
            if ((val & mask) != 0 && (val & ~mask) == 0) {
                tcg_out_insn_RI(s, oi_insns[i], dest, val >> i*16);
                return;
            }
        }

        /* Try all 48-bit insns that can perform it in one go.  */
        for (i = 0; i < 2; i++) {
            tcg_target_ulong mask = (0xffffffffull << i*32);
            if ((val & mask) != 0 && (val & ~mask) == 0) {
                tcg_out_insn_RIL(s, nif_insns[i], dest, val >> i*32);
                return;
            }
        }

        /* Perform the OR via sequential modifications to the high and
           low parts.  Do this via recursion to handle 16-bit vs 32-bit
           masks in each half.  */
        tgen64_ori(s, dest, val & 0x00000000ffffffffull);
        tgen64_ori(s, dest, val & 0xffffffff00000000ull);
    } else {
        /* With no extended-immediate facility, we don't need to be so
           clever.  Just iterate over the insns and mask in the constant.  */
        for (i = 0; i < 4; i++) {
            tcg_target_ulong mask = (0xffffull << i*16);
            if ((val & mask) != 0) {
                tcg_out_insn_RI(s, oi_insns[i], dest, val >> i*16);
            }
        }
    }
}

static void tgen64_xori(TCGContext *s, TCGReg dest, tcg_target_ulong val)
{
    /* Perform the xor by parts.  */
    if (val & 0xffffffff) {
        tcg_out_insn(s, RIL, XILF, dest, val);
    }
    if (val > 0xffffffff) {
        tcg_out_insn(s, RIL, XIHF, dest, val >> 31 >> 1);
    }
}

static int tgen_cmp(TCGContext *s, TCGType type, TCGCond c, TCGReg r1,
                    TCGArg c2, int c2const)
{
R
Richard Henderson 已提交
1089
    bool is_unsigned = is_unsigned_cond(c);
R
Richard Henderson 已提交
1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130
    if (c2const) {
        if (c2 == 0) {
            if (type == TCG_TYPE_I32) {
                tcg_out_insn(s, RR, LTR, r1, r1);
            } else {
                tcg_out_insn(s, RRE, LTGR, r1, r1);
            }
            return tcg_cond_to_ltr_cond[c];
        } else {
            if (is_unsigned) {
                if (type == TCG_TYPE_I32) {
                    tcg_out_insn(s, RIL, CLFI, r1, c2);
                } else {
                    tcg_out_insn(s, RIL, CLGFI, r1, c2);
                }
            } else {
                if (type == TCG_TYPE_I32) {
                    tcg_out_insn(s, RIL, CFI, r1, c2);
                } else {
                    tcg_out_insn(s, RIL, CGFI, r1, c2);
                }
            }
        }
    } else {
        if (is_unsigned) {
            if (type == TCG_TYPE_I32) {
                tcg_out_insn(s, RR, CLR, r1, c2);
            } else {
                tcg_out_insn(s, RRE, CLGR, r1, c2);
            }
        } else {
            if (type == TCG_TYPE_I32) {
                tcg_out_insn(s, RR, CR, r1, c2);
            } else {
                tcg_out_insn(s, RRE, CGR, r1, c2);
            }
        }
    }
    return tcg_cond_to_s390_cond[c];
}

R
Richard Henderson 已提交
1131
static void tgen_setcond(TCGContext *s, TCGType type, TCGCond cond,
1132
                         TCGReg dest, TCGReg c1, TCGArg c2, int c2const)
R
Richard Henderson 已提交
1133
{
R
Richard Henderson 已提交
1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207
    int cc;

    switch (cond) {
    case TCG_COND_GTU:
    case TCG_COND_GT:
    do_greater:
        /* The result of a compare has CC=2 for GT and CC=3 unused.
           ADD LOGICAL WITH CARRY considers (CC & 2) the carry bit.  */
        tgen_cmp(s, type, cond, c1, c2, c2const);
        tcg_out_movi(s, type, dest, 0);
        tcg_out_insn(s, RRE, ALCGR, dest, dest);
        return;

    case TCG_COND_GEU:
    do_geu:
        /* We need "real" carry semantics, so use SUBTRACT LOGICAL
           instead of COMPARE LOGICAL.  This needs an extra move.  */
        tcg_out_mov(s, type, TCG_TMP0, c1);
        if (c2const) {
            tcg_out_movi(s, TCG_TYPE_I64, dest, 0);
            if (type == TCG_TYPE_I32) {
                tcg_out_insn(s, RIL, SLFI, TCG_TMP0, c2);
            } else {
                tcg_out_insn(s, RIL, SLGFI, TCG_TMP0, c2);
            }
        } else {
            if (type == TCG_TYPE_I32) {
                tcg_out_insn(s, RR, SLR, TCG_TMP0, c2);
            } else {
                tcg_out_insn(s, RRE, SLGR, TCG_TMP0, c2);
            }
            tcg_out_movi(s, TCG_TYPE_I64, dest, 0);
        }
        tcg_out_insn(s, RRE, ALCGR, dest, dest);
        return;

    case TCG_COND_LEU:
    case TCG_COND_LTU:
    case TCG_COND_LT:
        /* Swap operands so that we can use GEU/GTU/GT.  */
        if (c2const) {
            tcg_out_movi(s, type, TCG_TMP0, c2);
            c2 = c1;
            c2const = 0;
            c1 = TCG_TMP0;
        } else {
            TCGReg t = c1;
            c1 = c2;
            c2 = t;
        }
        if (cond == TCG_COND_LEU) {
            goto do_geu;
        }
        cond = tcg_swap_cond(cond);
        goto do_greater;

    case TCG_COND_NE:
        /* X != 0 is X > 0.  */
        if (c2const && c2 == 0) {
            cond = TCG_COND_GTU;
            goto do_greater;
        }
        break;

    case TCG_COND_EQ:
        /* X == 0 is X <= 0 is 0 >= X.  */
        if (c2const && c2 == 0) {
            tcg_out_movi(s, TCG_TYPE_I64, TCG_TMP0, 0);
            c2 = c1;
            c2const = 0;
            c1 = TCG_TMP0;
            goto do_geu;
        }
        break;
R
Richard Henderson 已提交
1208

R
Richard Henderson 已提交
1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224
    default:
        break;
    }

    cc = tgen_cmp(s, type, cond, c1, c2, c2const);
    if (facilities & FACILITY_LOAD_ON_COND) {
        /* Emit: d = 0, t = 1, d = (cc ? t : d).  */
        tcg_out_movi(s, TCG_TYPE_I64, dest, 0);
        tcg_out_movi(s, TCG_TYPE_I64, TCG_TMP0, 1);
        tcg_out_insn(s, RRF, LOCGR, dest, TCG_TMP0, cc);
    } else {
        /* Emit: d = 1; if (cc) goto over; d = 0; over:  */
        tcg_out_movi(s, type, dest, 1);
        tcg_out_insn(s, RI, BRC, cc, (4 + 4) >> 1);
        tcg_out_movi(s, type, dest, 0);
    }
R
Richard Henderson 已提交
1225 1226
}

1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243
static void tgen_movcond(TCGContext *s, TCGType type, TCGCond c, TCGReg dest,
                         TCGReg c1, TCGArg c2, int c2const, TCGReg r3)
{
    int cc;
    if (facilities & FACILITY_LOAD_ON_COND) {
        cc = tgen_cmp(s, type, c, c1, c2, c2const);
        tcg_out_insn(s, RRF, LOCGR, dest, r3, cc);
    } else {
        c = tcg_invert_cond(c);
        cc = tgen_cmp(s, type, c, c1, c2, c2const);

        /* Emit: if (cc) goto over; dest = r3; over:  */
        tcg_out_insn(s, RI, BRC, cc, (4 + 4) >> 1);
        tcg_out_insn(s, RRE, LGR, dest, r3);
    }
}

1244 1245 1246 1247 1248 1249 1250 1251 1252 1253
bool tcg_target_deposit_valid(int ofs, int len)
{
    return (facilities & FACILITY_GEN_INST_EXT) != 0;
}

static void tgen_deposit(TCGContext *s, TCGReg dest, TCGReg src,
                         int ofs, int len)
{
    int lsb = (63 - ofs);
    int msb = lsb - (len - 1);
1254
    tcg_out_risbg(s, dest, src, msb, lsb, ofs, 0);
1255 1256
}

1257
static void tgen_gotoi(TCGContext *s, int cc, tcg_insn_unit *dest)
R
Richard Henderson 已提交
1258
{
1259 1260
    ptrdiff_t off = dest - s->code_ptr;
    if (off == (int16_t)off) {
R
Richard Henderson 已提交
1261 1262 1263 1264
        tcg_out_insn(s, RI, BRC, cc, off);
    } else if (off == (int32_t)off) {
        tcg_out_insn(s, RIL, BRCL, cc, off);
    } else {
1265
        tcg_out_movi(s, TCG_TYPE_PTR, TCG_TMP0, (uintptr_t)dest);
R
Richard Henderson 已提交
1266 1267 1268 1269
        tcg_out_insn(s, RR, BCR, cc, TCG_TMP0);
    }
}

1270
static void tgen_branch(TCGContext *s, int cc, TCGLabel *l)
R
Richard Henderson 已提交
1271 1272
{
    if (l->has_value) {
1273
        tgen_gotoi(s, cc, l->u.value_ptr);
R
Richard Henderson 已提交
1274 1275
    } else if (USE_LONG_BRANCHES) {
        tcg_out16(s, RIL_BRCL | (cc << 4));
1276
        tcg_out_reloc(s, s->code_ptr, R_390_PC32DBL, l, -2);
1277
        s->code_ptr += 2;
R
Richard Henderson 已提交
1278 1279
    } else {
        tcg_out16(s, RI_BRC | (cc << 4));
1280
        tcg_out_reloc(s, s->code_ptr, R_390_PC16DBL, l, -2);
1281
        s->code_ptr += 1;
R
Richard Henderson 已提交
1282 1283 1284 1285
    }
}

static void tgen_compare_branch(TCGContext *s, S390Opcode opc, int cc,
1286
                                TCGReg r1, TCGReg r2, TCGLabel *l)
R
Richard Henderson 已提交
1287
{
1288
    intptr_t off;
R
Richard Henderson 已提交
1289 1290

    if (l->has_value) {
1291
        off = l->u.value_ptr - s->code_ptr;
R
Richard Henderson 已提交
1292 1293
    } else {
        /* We need to keep the offset unchanged for retranslation.  */
1294
        off = s->code_ptr[1];
1295
        tcg_out_reloc(s, s->code_ptr + 1, R_390_PC16DBL, l, -2);
R
Richard Henderson 已提交
1296 1297 1298 1299 1300 1301 1302 1303
    }

    tcg_out16(s, (opc & 0xff00) | (r1 << 4) | r2);
    tcg_out16(s, off);
    tcg_out16(s, cc << 12 | (opc & 0xff));
}

static void tgen_compare_imm_branch(TCGContext *s, S390Opcode opc, int cc,
1304
                                    TCGReg r1, int i2, TCGLabel *l)
R
Richard Henderson 已提交
1305 1306 1307 1308
{
    tcg_target_long off;

    if (l->has_value) {
1309
        off = l->u.value_ptr - s->code_ptr;
R
Richard Henderson 已提交
1310 1311
    } else {
        /* We need to keep the offset unchanged for retranslation.  */
1312
        off = s->code_ptr[1];
1313
        tcg_out_reloc(s, s->code_ptr + 1, R_390_PC16DBL, l, -2);
R
Richard Henderson 已提交
1314 1315 1316 1317 1318 1319 1320 1321
    }

    tcg_out16(s, (opc & 0xff00) | (r1 << 4) | cc);
    tcg_out16(s, off);
    tcg_out16(s, (i2 << 8) | (opc & 0xff));
}

static void tgen_brcond(TCGContext *s, TCGType type, TCGCond c,
1322
                        TCGReg r1, TCGArg c2, int c2const, TCGLabel *l)
R
Richard Henderson 已提交
1323 1324 1325 1326
{
    int cc;

    if (facilities & FACILITY_GEN_INST_EXT) {
1327
        bool is_unsigned = is_unsigned_cond(c);
R
Richard Henderson 已提交
1328 1329 1330 1331 1332 1333 1334 1335 1336
        bool in_range;
        S390Opcode opc;

        cc = tcg_cond_to_s390_cond[c];

        if (!c2const) {
            opc = (type == TCG_TYPE_I32
                   ? (is_unsigned ? RIE_CLRJ : RIE_CRJ)
                   : (is_unsigned ? RIE_CLGRJ : RIE_CGRJ));
1337
            tgen_compare_branch(s, opc, cc, r1, c2, l);
R
Richard Henderson 已提交
1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362
            return;
        }

        /* COMPARE IMMEDIATE AND BRANCH RELATIVE has an 8-bit immediate field.
           If the immediate we've been given does not fit that range, we'll
           fall back to separate compare and branch instructions using the
           larger comparison range afforded by COMPARE IMMEDIATE.  */
        if (type == TCG_TYPE_I32) {
            if (is_unsigned) {
                opc = RIE_CLIJ;
                in_range = (uint32_t)c2 == (uint8_t)c2;
            } else {
                opc = RIE_CIJ;
                in_range = (int32_t)c2 == (int8_t)c2;
            }
        } else {
            if (is_unsigned) {
                opc = RIE_CLGIJ;
                in_range = (uint64_t)c2 == (uint8_t)c2;
            } else {
                opc = RIE_CGIJ;
                in_range = (int64_t)c2 == (int8_t)c2;
            }
        }
        if (in_range) {
1363
            tgen_compare_imm_branch(s, opc, cc, r1, c2, l);
R
Richard Henderson 已提交
1364 1365 1366 1367 1368
            return;
        }
    }

    cc = tgen_cmp(s, type, c, r1, c2, c2const);
1369
    tgen_branch(s, cc, l);
R
Richard Henderson 已提交
1370 1371
}

1372
static void tcg_out_call(TCGContext *s, tcg_insn_unit *dest)
R
Richard Henderson 已提交
1373
{
1374
    ptrdiff_t off = dest - s->code_ptr;
R
Richard Henderson 已提交
1375 1376 1377
    if (off == (int32_t)off) {
        tcg_out_insn(s, RIL, BRASL, TCG_REG_R14, off);
    } else {
1378
        tcg_out_movi(s, TCG_TYPE_PTR, TCG_TMP0, (uintptr_t)dest);
R
Richard Henderson 已提交
1379 1380 1381 1382
        tcg_out_insn(s, RR, BASR, TCG_REG_R14, TCG_TMP0);
    }
}

1383
static void tcg_out_qemu_ld_direct(TCGContext *s, TCGMemOp opc, TCGReg data,
R
Richard Henderson 已提交
1384 1385
                                   TCGReg base, TCGReg index, int disp)
{
1386
    switch (opc & (MO_SSIZE | MO_BSWAP)) {
1387
    case MO_UB:
R
Richard Henderson 已提交
1388 1389
        tcg_out_insn(s, RXY, LLGC, data, base, index, disp);
        break;
1390
    case MO_SB:
R
Richard Henderson 已提交
1391 1392
        tcg_out_insn(s, RXY, LGB, data, base, index, disp);
        break;
1393 1394 1395 1396 1397 1398

    case MO_UW | MO_BSWAP:
        /* swapped unsigned halfword load with upper bits zeroed */
        tcg_out_insn(s, RXY, LRVH, data, base, index, disp);
        tgen_ext16u(s, TCG_TYPE_I64, data, data);
        break;
1399
    case MO_UW:
1400 1401 1402 1403 1404 1405 1406
        tcg_out_insn(s, RXY, LLGH, data, base, index, disp);
        break;

    case MO_SW | MO_BSWAP:
        /* swapped sign-extended halfword load */
        tcg_out_insn(s, RXY, LRVH, data, base, index, disp);
        tgen_ext16s(s, TCG_TYPE_I64, data, data);
R
Richard Henderson 已提交
1407
        break;
1408
    case MO_SW:
1409 1410 1411 1412 1413 1414 1415
        tcg_out_insn(s, RXY, LGH, data, base, index, disp);
        break;

    case MO_UL | MO_BSWAP:
        /* swapped unsigned int load with upper bits zeroed */
        tcg_out_insn(s, RXY, LRV, data, base, index, disp);
        tgen_ext32u(s, data, data);
R
Richard Henderson 已提交
1416
        break;
1417
    case MO_UL:
1418 1419 1420 1421 1422 1423 1424
        tcg_out_insn(s, RXY, LLGF, data, base, index, disp);
        break;

    case MO_SL | MO_BSWAP:
        /* swapped sign-extended int load */
        tcg_out_insn(s, RXY, LRV, data, base, index, disp);
        tgen_ext32s(s, data, data);
R
Richard Henderson 已提交
1425
        break;
1426
    case MO_SL:
1427 1428 1429 1430 1431
        tcg_out_insn(s, RXY, LGF, data, base, index, disp);
        break;

    case MO_Q | MO_BSWAP:
        tcg_out_insn(s, RXY, LRVG, data, base, index, disp);
R
Richard Henderson 已提交
1432
        break;
1433
    case MO_Q:
1434
        tcg_out_insn(s, RXY, LG, data, base, index, disp);
R
Richard Henderson 已提交
1435
        break;
1436

R
Richard Henderson 已提交
1437 1438 1439 1440 1441
    default:
        tcg_abort();
    }
}

1442
static void tcg_out_qemu_st_direct(TCGContext *s, TCGMemOp opc, TCGReg data,
R
Richard Henderson 已提交
1443 1444
                                   TCGReg base, TCGReg index, int disp)
{
1445
    switch (opc & (MO_SIZE | MO_BSWAP)) {
1446
    case MO_UB:
R
Richard Henderson 已提交
1447 1448 1449 1450 1451 1452
        if (disp >= 0 && disp < 0x1000) {
            tcg_out_insn(s, RX, STC, data, base, index, disp);
        } else {
            tcg_out_insn(s, RXY, STCY, data, base, index, disp);
        }
        break;
1453 1454 1455 1456

    case MO_UW | MO_BSWAP:
        tcg_out_insn(s, RXY, STRVH, data, base, index, disp);
        break;
1457
    case MO_UW:
1458
        if (disp >= 0 && disp < 0x1000) {
R
Richard Henderson 已提交
1459 1460 1461 1462 1463
            tcg_out_insn(s, RX, STH, data, base, index, disp);
        } else {
            tcg_out_insn(s, RXY, STHY, data, base, index, disp);
        }
        break;
1464 1465 1466 1467

    case MO_UL | MO_BSWAP:
        tcg_out_insn(s, RXY, STRV, data, base, index, disp);
        break;
1468
    case MO_UL:
1469
        if (disp >= 0 && disp < 0x1000) {
R
Richard Henderson 已提交
1470 1471 1472 1473 1474
            tcg_out_insn(s, RX, ST, data, base, index, disp);
        } else {
            tcg_out_insn(s, RXY, STY, data, base, index, disp);
        }
        break;
1475 1476 1477 1478

    case MO_Q | MO_BSWAP:
        tcg_out_insn(s, RXY, STRVG, data, base, index, disp);
        break;
1479
    case MO_Q:
1480
        tcg_out_insn(s, RXY, STG, data, base, index, disp);
R
Richard Henderson 已提交
1481
        break;
1482

R
Richard Henderson 已提交
1483 1484 1485 1486 1487 1488
    default:
        tcg_abort();
    }
}

#if defined(CONFIG_SOFTMMU)
1489 1490 1491 1492 1493 1494 1495 1496 1497 1498
/* We're expecting to use a 20-bit signed offset on the tlb memory ops.
   Using the offset of the second entry in the last tlb table ensures
   that we can index all of the elements of the first entry.  */
QEMU_BUILD_BUG_ON(offsetof(CPUArchState, tlb_table[NB_MMU_MODES - 1][1])
                  > 0x7ffff);

/* Load and compare a TLB entry, leaving the flags set.  Loads the TLB
   addend into R2.  Returns a register with the santitized guest address.  */
static TCGReg tcg_out_tlb_read(TCGContext* s, TCGReg addr_reg, TCGMemOp opc,
                               int mem_index, bool is_ld)
R
Richard Henderson 已提交
1499
{
1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513
    int s_mask = (1 << (opc & MO_SIZE)) - 1;
    int ofs, a_off;
    uint64_t tlb_mask;

    /* For aligned accesses, we check the first byte and include the alignment
       bits within the address.  For unaligned access, we check that we don't
       cross pages using the address of the last byte of the access.  */
    if ((opc & MO_AMASK) == MO_ALIGN || s_mask == 0) {
        a_off = 0;
        tlb_mask = TARGET_PAGE_MASK | s_mask;
    } else {
        a_off = s_mask;
        tlb_mask = TARGET_PAGE_MASK;
    }
1514

1515 1516 1517 1518 1519
    if (facilities & FACILITY_GEN_INST_EXT) {
        tcg_out_risbg(s, TCG_REG_R2, addr_reg,
                      64 - CPU_TLB_BITS - CPU_TLB_ENTRY_BITS,
                      63 - CPU_TLB_ENTRY_BITS,
                      64 + CPU_TLB_ENTRY_BITS - TARGET_PAGE_BITS, 1);
1520 1521 1522 1523 1524 1525
        if (a_off) {
            tcg_out_insn(s, RX, LA, TCG_REG_R3, addr_reg, TCG_REG_NONE, a_off);
            tgen_andi(s, TCG_TYPE_TL, TCG_REG_R3, tlb_mask);
        } else {
            tgen_andi_risbg(s, TCG_REG_R3, addr_reg, tlb_mask);
        }
R
Richard Henderson 已提交
1526
    } else {
1527 1528
        tcg_out_sh64(s, RSY_SRLG, TCG_REG_R2, addr_reg, TCG_REG_NONE,
                     TARGET_PAGE_BITS - CPU_TLB_ENTRY_BITS);
1529
        tcg_out_insn(s, RX, LA, TCG_REG_R3, addr_reg, TCG_REG_NONE, a_off);
1530 1531 1532
        tgen_andi(s, TCG_TYPE_I64, TCG_REG_R2,
                  (CPU_TLB_SIZE - 1) << CPU_TLB_ENTRY_BITS);
        tgen_andi(s, TCG_TYPE_TL, TCG_REG_R3, tlb_mask);
R
Richard Henderson 已提交
1533 1534
    }

1535
    if (is_ld) {
1536
        ofs = offsetof(CPUArchState, tlb_table[mem_index][0].addr_read);
1537 1538
    } else {
        ofs = offsetof(CPUArchState, tlb_table[mem_index][0].addr_write);
R
Richard Henderson 已提交
1539 1540
    }
    if (TARGET_LONG_BITS == 32) {
1541
        tcg_out_mem(s, RX_C, RXY_CY, TCG_REG_R3, TCG_REG_R2, TCG_AREG0, ofs);
R
Richard Henderson 已提交
1542
    } else {
1543
        tcg_out_mem(s, 0, RXY_CG, TCG_REG_R3, TCG_REG_R2, TCG_AREG0, ofs);
R
Richard Henderson 已提交
1544 1545
    }

1546 1547 1548
    ofs = offsetof(CPUArchState, tlb_table[mem_index][0].addend);
    tcg_out_mem(s, 0, RXY_LG, TCG_REG_R2, TCG_REG_R2, TCG_AREG0, ofs);

R
Richard Henderson 已提交
1549
    if (TARGET_LONG_BITS == 32) {
1550 1551
        tgen_ext32u(s, TCG_REG_R3, addr_reg);
        return TCG_REG_R3;
R
Richard Henderson 已提交
1552
    }
1553 1554
    return addr_reg;
}
R
Richard Henderson 已提交
1555

1556 1557
static void add_qemu_ldst_label(TCGContext *s, bool is_ld, TCGMemOpIdx oi,
                                TCGReg data, TCGReg addr,
1558 1559 1560 1561 1562
                                tcg_insn_unit *raddr, tcg_insn_unit *label_ptr)
{
    TCGLabelQemuLdst *label = new_ldst_label(s);

    label->is_ld = is_ld;
1563
    label->oi = oi;
1564 1565 1566 1567 1568
    label->datalo_reg = data;
    label->addrlo_reg = addr;
    label->raddr = raddr;
    label->label_ptr[0] = label_ptr;
}
R
Richard Henderson 已提交
1569

1570 1571 1572 1573
static void tcg_out_qemu_ld_slow_path(TCGContext *s, TCGLabelQemuLdst *lb)
{
    TCGReg addr_reg = lb->addrlo_reg;
    TCGReg data_reg = lb->datalo_reg;
1574 1575
    TCGMemOpIdx oi = lb->oi;
    TCGMemOp opc = get_memop(oi);
R
Richard Henderson 已提交
1576

1577
    patch_reloc(lb->label_ptr[0], R_390_PC16DBL, (intptr_t)s->code_ptr, -2);
R
Richard Henderson 已提交
1578

1579 1580 1581 1582
    tcg_out_mov(s, TCG_TYPE_PTR, TCG_REG_R2, TCG_AREG0);
    if (TARGET_LONG_BITS == 64) {
        tcg_out_mov(s, TCG_TYPE_I64, TCG_REG_R3, addr_reg);
    }
1583
    tcg_out_movi(s, TCG_TYPE_I32, TCG_REG_R4, oi);
1584
    tcg_out_movi(s, TCG_TYPE_PTR, TCG_REG_R5, (uintptr_t)lb->raddr);
1585
    tcg_out_call(s, qemu_ld_helpers[opc & (MO_BSWAP | MO_SSIZE)]);
1586
    tcg_out_mov(s, TCG_TYPE_I64, data_reg, TCG_REG_R2);
1587

1588
    tgen_gotoi(s, S390_CC_ALWAYS, lb->raddr);
R
Richard Henderson 已提交
1589 1590
}

1591
static void tcg_out_qemu_st_slow_path(TCGContext *s, TCGLabelQemuLdst *lb)
R
Richard Henderson 已提交
1592
{
1593 1594
    TCGReg addr_reg = lb->addrlo_reg;
    TCGReg data_reg = lb->datalo_reg;
1595 1596
    TCGMemOpIdx oi = lb->oi;
    TCGMemOp opc = get_memop(oi);
1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619

    patch_reloc(lb->label_ptr[0], R_390_PC16DBL, (intptr_t)s->code_ptr, -2);

    tcg_out_mov(s, TCG_TYPE_PTR, TCG_REG_R2, TCG_AREG0);
    if (TARGET_LONG_BITS == 64) {
        tcg_out_mov(s, TCG_TYPE_I64, TCG_REG_R3, addr_reg);
    }
    switch (opc & MO_SIZE) {
    case MO_UB:
        tgen_ext8u(s, TCG_TYPE_I64, TCG_REG_R4, data_reg);
        break;
    case MO_UW:
        tgen_ext16u(s, TCG_TYPE_I64, TCG_REG_R4, data_reg);
        break;
    case MO_UL:
        tgen_ext32u(s, TCG_REG_R4, data_reg);
        break;
    case MO_Q:
        tcg_out_mov(s, TCG_TYPE_I64, TCG_REG_R4, data_reg);
        break;
    default:
        tcg_abort();
    }
1620
    tcg_out_movi(s, TCG_TYPE_I32, TCG_REG_R5, oi);
1621
    tcg_out_movi(s, TCG_TYPE_PTR, TCG_REG_R6, (uintptr_t)lb->raddr);
1622
    tcg_out_call(s, qemu_st_helpers[opc & (MO_BSWAP | MO_SIZE)]);
1623 1624

    tgen_gotoi(s, S390_CC_ALWAYS, lb->raddr);
R
Richard Henderson 已提交
1625 1626 1627 1628 1629 1630 1631 1632 1633
}
#else
static void tcg_prepare_user_ldst(TCGContext *s, TCGReg *addr_reg,
                                  TCGReg *index_reg, tcg_target_long *disp)
{
    if (TARGET_LONG_BITS == 32) {
        tgen_ext32u(s, TCG_TMP0, *addr_reg);
        *addr_reg = TCG_TMP0;
    }
1634
    if (guest_base < 0x80000) {
R
Richard Henderson 已提交
1635
        *index_reg = TCG_REG_NONE;
1636
        *disp = guest_base;
R
Richard Henderson 已提交
1637 1638 1639 1640 1641 1642 1643
    } else {
        *index_reg = TCG_GUEST_BASE_REG;
        *disp = 0;
    }
}
#endif /* CONFIG_SOFTMMU */

1644
static void tcg_out_qemu_ld(TCGContext* s, TCGReg data_reg, TCGReg addr_reg,
1645
                            TCGMemOpIdx oi)
R
Richard Henderson 已提交
1646
{
1647
    TCGMemOp opc = get_memop(oi);
1648
#ifdef CONFIG_SOFTMMU
1649
    unsigned mem_index = get_mmuidx(oi);
1650 1651 1652 1653 1654
    tcg_insn_unit *label_ptr;
    TCGReg base_reg;

    base_reg = tcg_out_tlb_read(s, addr_reg, opc, mem_index, 1);

1655 1656 1657 1658
    /* We need to keep the offset unchanged for retranslation.  */
    tcg_out16(s, RI_BRC | (S390_CC_NE << 4));
    label_ptr = s->code_ptr;
    s->code_ptr += 1;
1659 1660

    tcg_out_qemu_ld_direct(s, opc, data_reg, base_reg, TCG_REG_R2, 0);
R
Richard Henderson 已提交
1661

1662
    add_qemu_ldst_label(s, 1, oi, data_reg, addr_reg, s->code_ptr, label_ptr);
R
Richard Henderson 已提交
1663
#else
1664 1665 1666
    TCGReg index_reg;
    tcg_target_long disp;

R
Richard Henderson 已提交
1667 1668 1669 1670 1671
    tcg_prepare_user_ldst(s, &addr_reg, &index_reg, &disp);
    tcg_out_qemu_ld_direct(s, opc, data_reg, addr_reg, index_reg, disp);
#endif
}

1672
static void tcg_out_qemu_st(TCGContext* s, TCGReg data_reg, TCGReg addr_reg,
1673
                            TCGMemOpIdx oi)
R
Richard Henderson 已提交
1674
{
1675
    TCGMemOp opc = get_memop(oi);
1676
#ifdef CONFIG_SOFTMMU
1677
    unsigned mem_index = get_mmuidx(oi);
1678 1679 1680 1681 1682
    tcg_insn_unit *label_ptr;
    TCGReg base_reg;

    base_reg = tcg_out_tlb_read(s, addr_reg, opc, mem_index, 0);

1683 1684 1685 1686
    /* We need to keep the offset unchanged for retranslation.  */
    tcg_out16(s, RI_BRC | (S390_CC_NE << 4));
    label_ptr = s->code_ptr;
    s->code_ptr += 1;
1687 1688

    tcg_out_qemu_st_direct(s, opc, data_reg, base_reg, TCG_REG_R2, 0);
R
Richard Henderson 已提交
1689

1690
    add_qemu_ldst_label(s, 0, oi, data_reg, addr_reg, s->code_ptr, label_ptr);
R
Richard Henderson 已提交
1691
#else
1692 1693 1694
    TCGReg index_reg;
    tcg_target_long disp;

R
Richard Henderson 已提交
1695 1696 1697
    tcg_prepare_user_ldst(s, &addr_reg, &index_reg, &disp);
    tcg_out_qemu_st_direct(s, opc, data_reg, addr_reg, index_reg, disp);
#endif
A
Alexander Graf 已提交
1698 1699
}

R
Richard Henderson 已提交
1700 1701 1702 1703
# define OP_32_64(x) \
        case glue(glue(INDEX_op_,x),_i32): \
        case glue(glue(INDEX_op_,x),_i64)

1704
static inline void tcg_out_op(TCGContext *s, TCGOpcode opc,
A
Alexander Graf 已提交
1705 1706
                const TCGArg *args, const int *const_args)
{
R
Richard Henderson 已提交
1707
    S390Opcode op;
1708
    TCGArg a0, a1, a2;
R
Richard Henderson 已提交
1709 1710 1711 1712 1713

    switch (opc) {
    case INDEX_op_exit_tb:
        /* return value */
        tcg_out_movi(s, TCG_TYPE_PTR, TCG_REG_R2, args[0]);
1714
        tgen_gotoi(s, S390_CC_ALWAYS, tb_ret_addr);
R
Richard Henderson 已提交
1715 1716 1717 1718
        break;

    case INDEX_op_goto_tb:
        if (s->tb_jmp_offset) {
1719 1720 1721
            tcg_out16(s, RIL_BRCL | (S390_CC_ALWAYS << 4));
            s->tb_jmp_offset[args[0]] = tcg_current_code_size(s);
            s->code_ptr += 2;
R
Richard Henderson 已提交
1722 1723 1724 1725 1726 1727
        } else {
            /* load address stored at s->tb_next + args[0] */
            tcg_out_ld_abs(s, TCG_TYPE_PTR, TCG_TMP0, s->tb_next + args[0]);
            /* and go there */
            tcg_out_insn(s, RR, BCR, S390_CC_ALWAYS, TCG_TMP0);
        }
1728
        s->tb_next_offset[args[0]] = tcg_current_code_size(s);
R
Richard Henderson 已提交
1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770
        break;

    OP_32_64(ld8u):
        /* ??? LLC (RXY format) is only present with the extended-immediate
           facility, whereas LLGC is always present.  */
        tcg_out_mem(s, 0, RXY_LLGC, args[0], args[1], TCG_REG_NONE, args[2]);
        break;

    OP_32_64(ld8s):
        /* ??? LB is no smaller than LGB, so no point to using it.  */
        tcg_out_mem(s, 0, RXY_LGB, args[0], args[1], TCG_REG_NONE, args[2]);
        break;

    OP_32_64(ld16u):
        /* ??? LLH (RXY format) is only present with the extended-immediate
           facility, whereas LLGH is always present.  */
        tcg_out_mem(s, 0, RXY_LLGH, args[0], args[1], TCG_REG_NONE, args[2]);
        break;

    case INDEX_op_ld16s_i32:
        tcg_out_mem(s, RX_LH, RXY_LHY, args[0], args[1], TCG_REG_NONE, args[2]);
        break;

    case INDEX_op_ld_i32:
        tcg_out_ld(s, TCG_TYPE_I32, args[0], args[1], args[2]);
        break;

    OP_32_64(st8):
        tcg_out_mem(s, RX_STC, RXY_STCY, args[0], args[1],
                    TCG_REG_NONE, args[2]);
        break;

    OP_32_64(st16):
        tcg_out_mem(s, RX_STH, RXY_STHY, args[0], args[1],
                    TCG_REG_NONE, args[2]);
        break;

    case INDEX_op_st_i32:
        tcg_out_st(s, TCG_TYPE_I32, args[0], args[1], args[2]);
        break;

    case INDEX_op_add_i32:
1771
        a0 = args[0], a1 = args[1], a2 = (int32_t)args[2];
R
Richard Henderson 已提交
1772
        if (const_args[2]) {
1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786
        do_addi_32:
            if (a0 == a1) {
                if (a2 == (int16_t)a2) {
                    tcg_out_insn(s, RI, AHI, a0, a2);
                    break;
                }
                if (facilities & FACILITY_EXT_IMM) {
                    tcg_out_insn(s, RIL, AFI, a0, a2);
                    break;
                }
            }
            tcg_out_mem(s, RX_LA, RXY_LAY, a0, a1, TCG_REG_NONE, a2);
        } else if (a0 == a1) {
            tcg_out_insn(s, RR, AR, a0, a2);
R
Richard Henderson 已提交
1787
        } else {
1788
            tcg_out_insn(s, RX, LA, a0, a1, a2, 0);
R
Richard Henderson 已提交
1789 1790 1791
        }
        break;
    case INDEX_op_sub_i32:
1792
        a0 = args[0], a1 = args[1], a2 = (int32_t)args[2];
R
Richard Henderson 已提交
1793
        if (const_args[2]) {
1794 1795
            a2 = -a2;
            goto do_addi_32;
R
Richard Henderson 已提交
1796
        }
1797
        tcg_out_insn(s, RR, SR, args[0], args[2]);
R
Richard Henderson 已提交
1798 1799 1800 1801
        break;

    case INDEX_op_and_i32:
        if (const_args[2]) {
1802
            tgen_andi(s, TCG_TYPE_I32, args[0], args[2]);
R
Richard Henderson 已提交
1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901
        } else {
            tcg_out_insn(s, RR, NR, args[0], args[2]);
        }
        break;
    case INDEX_op_or_i32:
        if (const_args[2]) {
            tgen64_ori(s, args[0], args[2] & 0xffffffff);
        } else {
            tcg_out_insn(s, RR, OR, args[0], args[2]);
        }
        break;
    case INDEX_op_xor_i32:
        if (const_args[2]) {
            tgen64_xori(s, args[0], args[2] & 0xffffffff);
        } else {
            tcg_out_insn(s, RR, XR, args[0], args[2]);
        }
        break;

    case INDEX_op_neg_i32:
        tcg_out_insn(s, RR, LCR, args[0], args[1]);
        break;

    case INDEX_op_mul_i32:
        if (const_args[2]) {
            if ((int32_t)args[2] == (int16_t)args[2]) {
                tcg_out_insn(s, RI, MHI, args[0], args[2]);
            } else {
                tcg_out_insn(s, RIL, MSFI, args[0], args[2]);
            }
        } else {
            tcg_out_insn(s, RRE, MSR, args[0], args[2]);
        }
        break;

    case INDEX_op_div2_i32:
        tcg_out_insn(s, RR, DR, TCG_REG_R2, args[4]);
        break;
    case INDEX_op_divu2_i32:
        tcg_out_insn(s, RRE, DLR, TCG_REG_R2, args[4]);
        break;

    case INDEX_op_shl_i32:
        op = RS_SLL;
    do_shift32:
        if (const_args[2]) {
            tcg_out_sh32(s, op, args[0], TCG_REG_NONE, args[2]);
        } else {
            tcg_out_sh32(s, op, args[0], args[2], 0);
        }
        break;
    case INDEX_op_shr_i32:
        op = RS_SRL;
        goto do_shift32;
    case INDEX_op_sar_i32:
        op = RS_SRA;
        goto do_shift32;

    case INDEX_op_rotl_i32:
        /* ??? Using tcg_out_sh64 here for the format; it is a 32-bit rol.  */
        if (const_args[2]) {
            tcg_out_sh64(s, RSY_RLL, args[0], args[1], TCG_REG_NONE, args[2]);
        } else {
            tcg_out_sh64(s, RSY_RLL, args[0], args[1], args[2], 0);
        }
        break;
    case INDEX_op_rotr_i32:
        if (const_args[2]) {
            tcg_out_sh64(s, RSY_RLL, args[0], args[1],
                         TCG_REG_NONE, (32 - args[2]) & 31);
        } else {
            tcg_out_insn(s, RR, LCR, TCG_TMP0, args[2]);
            tcg_out_sh64(s, RSY_RLL, args[0], args[1], TCG_TMP0, 0);
        }
        break;

    case INDEX_op_ext8s_i32:
        tgen_ext8s(s, TCG_TYPE_I32, args[0], args[1]);
        break;
    case INDEX_op_ext16s_i32:
        tgen_ext16s(s, TCG_TYPE_I32, args[0], args[1]);
        break;
    case INDEX_op_ext8u_i32:
        tgen_ext8u(s, TCG_TYPE_I32, args[0], args[1]);
        break;
    case INDEX_op_ext16u_i32:
        tgen_ext16u(s, TCG_TYPE_I32, args[0], args[1]);
        break;

    OP_32_64(bswap16):
        /* The TCG bswap definition requires bits 0-47 already be zero.
           Thus we don't need the G-type insns to implement bswap16_i64.  */
        tcg_out_insn(s, RRE, LRVR, args[0], args[1]);
        tcg_out_sh32(s, RS_SRL, args[0], TCG_REG_NONE, 16);
        break;
    OP_32_64(bswap32):
        tcg_out_insn(s, RRE, LRVR, args[0], args[1]);
        break;

1902
    case INDEX_op_add2_i32:
1903 1904 1905 1906 1907
        if (const_args[4]) {
            tcg_out_insn(s, RIL, ALFI, args[0], args[4]);
        } else {
            tcg_out_insn(s, RR, ALR, args[0], args[4]);
        }
1908 1909 1910
        tcg_out_insn(s, RRE, ALCR, args[1], args[5]);
        break;
    case INDEX_op_sub2_i32:
1911 1912 1913 1914 1915
        if (const_args[4]) {
            tcg_out_insn(s, RIL, SLFI, args[0], args[4]);
        } else {
            tcg_out_insn(s, RR, SLR, args[0], args[4]);
        }
1916 1917 1918
        tcg_out_insn(s, RRE, SLBR, args[1], args[5]);
        break;

R
Richard Henderson 已提交
1919
    case INDEX_op_br:
1920
        tgen_branch(s, S390_CC_ALWAYS, arg_label(args[0]));
R
Richard Henderson 已提交
1921 1922 1923 1924
        break;

    case INDEX_op_brcond_i32:
        tgen_brcond(s, TCG_TYPE_I32, args[2], args[0],
1925
                    args[1], const_args[1], arg_label(args[3]));
R
Richard Henderson 已提交
1926 1927 1928 1929 1930
        break;
    case INDEX_op_setcond_i32:
        tgen_setcond(s, TCG_TYPE_I32, args[3], args[0], args[1],
                     args[2], const_args[2]);
        break;
1931 1932 1933 1934
    case INDEX_op_movcond_i32:
        tgen_movcond(s, TCG_TYPE_I32, args[5], args[0], args[1],
                     args[2], const_args[2], args[3]);
        break;
R
Richard Henderson 已提交
1935

1936
    case INDEX_op_qemu_ld_i32:
R
Richard Henderson 已提交
1937
        /* ??? Technically we can use a non-extending instruction.  */
1938
    case INDEX_op_qemu_ld_i64:
1939
        tcg_out_qemu_ld(s, args[0], args[1], args[2]);
R
Richard Henderson 已提交
1940
        break;
1941 1942
    case INDEX_op_qemu_st_i32:
    case INDEX_op_qemu_st_i64:
1943
        tcg_out_qemu_st(s, args[0], args[1], args[2]);
R
Richard Henderson 已提交
1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966
        break;

    case INDEX_op_ld16s_i64:
        tcg_out_mem(s, 0, RXY_LGH, args[0], args[1], TCG_REG_NONE, args[2]);
        break;
    case INDEX_op_ld32u_i64:
        tcg_out_mem(s, 0, RXY_LLGF, args[0], args[1], TCG_REG_NONE, args[2]);
        break;
    case INDEX_op_ld32s_i64:
        tcg_out_mem(s, 0, RXY_LGF, args[0], args[1], TCG_REG_NONE, args[2]);
        break;
    case INDEX_op_ld_i64:
        tcg_out_ld(s, TCG_TYPE_I64, args[0], args[1], args[2]);
        break;

    case INDEX_op_st32_i64:
        tcg_out_st(s, TCG_TYPE_I32, args[0], args[1], args[2]);
        break;
    case INDEX_op_st_i64:
        tcg_out_st(s, TCG_TYPE_I64, args[0], args[1], args[2]);
        break;

    case INDEX_op_add_i64:
1967
        a0 = args[0], a1 = args[1], a2 = args[2];
R
Richard Henderson 已提交
1968
        if (const_args[2]) {
1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990
        do_addi_64:
            if (a0 == a1) {
                if (a2 == (int16_t)a2) {
                    tcg_out_insn(s, RI, AGHI, a0, a2);
                    break;
                }
                if (facilities & FACILITY_EXT_IMM) {
                    if (a2 == (int32_t)a2) {
                        tcg_out_insn(s, RIL, AGFI, a0, a2);
                        break;
                    } else if (a2 == (uint32_t)a2) {
                        tcg_out_insn(s, RIL, ALGFI, a0, a2);
                        break;
                    } else if (-a2 == (uint32_t)-a2) {
                        tcg_out_insn(s, RIL, SLGFI, a0, -a2);
                        break;
                    }
                }
            }
            tcg_out_mem(s, RX_LA, RXY_LAY, a0, a1, TCG_REG_NONE, a2);
        } else if (a0 == a1) {
            tcg_out_insn(s, RRE, AGR, a0, a2);
R
Richard Henderson 已提交
1991
        } else {
1992
            tcg_out_insn(s, RX, LA, a0, a1, a2, 0);
R
Richard Henderson 已提交
1993 1994 1995
        }
        break;
    case INDEX_op_sub_i64:
1996
        a0 = args[0], a1 = args[1], a2 = args[2];
R
Richard Henderson 已提交
1997
        if (const_args[2]) {
1998 1999
            a2 = -a2;
            goto do_addi_64;
R
Richard Henderson 已提交
2000 2001 2002 2003 2004 2005 2006
        } else {
            tcg_out_insn(s, RRE, SGR, args[0], args[2]);
        }
        break;

    case INDEX_op_and_i64:
        if (const_args[2]) {
2007
            tgen_andi(s, TCG_TYPE_I64, args[0], args[2]);
R
Richard Henderson 已提交
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 2052 2053 2054 2055
        } else {
            tcg_out_insn(s, RRE, NGR, args[0], args[2]);
        }
        break;
    case INDEX_op_or_i64:
        if (const_args[2]) {
            tgen64_ori(s, args[0], args[2]);
        } else {
            tcg_out_insn(s, RRE, OGR, args[0], args[2]);
        }
        break;
    case INDEX_op_xor_i64:
        if (const_args[2]) {
            tgen64_xori(s, args[0], args[2]);
        } else {
            tcg_out_insn(s, RRE, XGR, args[0], args[2]);
        }
        break;

    case INDEX_op_neg_i64:
        tcg_out_insn(s, RRE, LCGR, args[0], args[1]);
        break;
    case INDEX_op_bswap64_i64:
        tcg_out_insn(s, RRE, LRVGR, args[0], args[1]);
        break;

    case INDEX_op_mul_i64:
        if (const_args[2]) {
            if (args[2] == (int16_t)args[2]) {
                tcg_out_insn(s, RI, MGHI, args[0], args[2]);
            } else {
                tcg_out_insn(s, RIL, MSGFI, args[0], args[2]);
            }
        } else {
            tcg_out_insn(s, RRE, MSGR, args[0], args[2]);
        }
        break;

    case INDEX_op_div2_i64:
        /* ??? We get an unnecessary sign-extension of the dividend
           into R3 with this definition, but as we do in fact always
           produce both quotient and remainder using INDEX_op_div_i64
           instead requires jumping through even more hoops.  */
        tcg_out_insn(s, RRE, DSGR, TCG_REG_R2, args[4]);
        break;
    case INDEX_op_divu2_i64:
        tcg_out_insn(s, RRE, DLGR, TCG_REG_R2, args[4]);
        break;
2056 2057 2058
    case INDEX_op_mulu2_i64:
        tcg_out_insn(s, RRE, MLGR, TCG_REG_R2, args[3]);
        break;
R
Richard Henderson 已提交
2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101

    case INDEX_op_shl_i64:
        op = RSY_SLLG;
    do_shift64:
        if (const_args[2]) {
            tcg_out_sh64(s, op, args[0], args[1], TCG_REG_NONE, args[2]);
        } else {
            tcg_out_sh64(s, op, args[0], args[1], args[2], 0);
        }
        break;
    case INDEX_op_shr_i64:
        op = RSY_SRLG;
        goto do_shift64;
    case INDEX_op_sar_i64:
        op = RSY_SRAG;
        goto do_shift64;

    case INDEX_op_rotl_i64:
        if (const_args[2]) {
            tcg_out_sh64(s, RSY_RLLG, args[0], args[1],
                         TCG_REG_NONE, args[2]);
        } else {
            tcg_out_sh64(s, RSY_RLLG, args[0], args[1], args[2], 0);
        }
        break;
    case INDEX_op_rotr_i64:
        if (const_args[2]) {
            tcg_out_sh64(s, RSY_RLLG, args[0], args[1],
                         TCG_REG_NONE, (64 - args[2]) & 63);
        } else {
            /* We can use the smaller 32-bit negate because only the
               low 6 bits are examined for the rotate.  */
            tcg_out_insn(s, RR, LCR, TCG_TMP0, args[2]);
            tcg_out_sh64(s, RSY_RLLG, args[0], args[1], TCG_TMP0, 0);
        }
        break;

    case INDEX_op_ext8s_i64:
        tgen_ext8s(s, TCG_TYPE_I64, args[0], args[1]);
        break;
    case INDEX_op_ext16s_i64:
        tgen_ext16s(s, TCG_TYPE_I64, args[0], args[1]);
        break;
2102
    case INDEX_op_ext_i32_i64:
R
Richard Henderson 已提交
2103 2104 2105 2106 2107 2108 2109 2110 2111
    case INDEX_op_ext32s_i64:
        tgen_ext32s(s, args[0], args[1]);
        break;
    case INDEX_op_ext8u_i64:
        tgen_ext8u(s, TCG_TYPE_I64, args[0], args[1]);
        break;
    case INDEX_op_ext16u_i64:
        tgen_ext16u(s, TCG_TYPE_I64, args[0], args[1]);
        break;
2112
    case INDEX_op_extu_i32_i64:
R
Richard Henderson 已提交
2113 2114 2115 2116
    case INDEX_op_ext32u_i64:
        tgen_ext32u(s, args[0], args[1]);
        break;

2117
    case INDEX_op_add2_i64:
2118 2119 2120 2121 2122 2123 2124 2125 2126
        if (const_args[4]) {
            if ((int64_t)args[4] >= 0) {
                tcg_out_insn(s, RIL, ALGFI, args[0], args[4]);
            } else {
                tcg_out_insn(s, RIL, SLGFI, args[0], -args[4]);
            }
        } else {
            tcg_out_insn(s, RRE, ALGR, args[0], args[4]);
        }
2127 2128 2129
        tcg_out_insn(s, RRE, ALCGR, args[1], args[5]);
        break;
    case INDEX_op_sub2_i64:
2130 2131 2132 2133 2134 2135 2136 2137 2138
        if (const_args[4]) {
            if ((int64_t)args[4] >= 0) {
                tcg_out_insn(s, RIL, SLGFI, args[0], args[4]);
            } else {
                tcg_out_insn(s, RIL, ALGFI, args[0], -args[4]);
            }
        } else {
            tcg_out_insn(s, RRE, SLGR, args[0], args[4]);
        }
2139 2140 2141
        tcg_out_insn(s, RRE, SLBGR, args[1], args[5]);
        break;

R
Richard Henderson 已提交
2142 2143
    case INDEX_op_brcond_i64:
        tgen_brcond(s, TCG_TYPE_I64, args[2], args[0],
2144
                    args[1], const_args[1], arg_label(args[3]));
R
Richard Henderson 已提交
2145 2146 2147 2148 2149
        break;
    case INDEX_op_setcond_i64:
        tgen_setcond(s, TCG_TYPE_I64, args[3], args[0], args[1],
                     args[2], const_args[2]);
        break;
2150 2151 2152 2153
    case INDEX_op_movcond_i64:
        tgen_movcond(s, TCG_TYPE_I64, args[5], args[0], args[1],
                     args[2], const_args[2], args[3]);
        break;
R
Richard Henderson 已提交
2154

2155 2156 2157 2158
    OP_32_64(deposit):
        tgen_deposit(s, args[0], args[2], args[3], args[4]);
        break;

2159 2160 2161 2162 2163
    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.  */
R
Richard Henderson 已提交
2164 2165 2166
    default:
        tcg_abort();
    }
A
Alexander Graf 已提交
2167 2168
}

R
Richard Henderson 已提交
2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182
static const TCGTargetOpDef s390_op_defs[] = {
    { INDEX_op_exit_tb, { } },
    { INDEX_op_goto_tb, { } },
    { INDEX_op_br, { } },

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

2183 2184
    { INDEX_op_add_i32, { "r", "r", "ri" } },
    { INDEX_op_sub_i32, { "r", "0", "ri" } },
R
Richard Henderson 已提交
2185 2186 2187 2188 2189
    { INDEX_op_mul_i32, { "r", "0", "rK" } },

    { INDEX_op_div2_i32, { "b", "a", "0", "1", "r" } },
    { INDEX_op_divu2_i32, { "b", "a", "0", "1", "r" } },

2190
    { INDEX_op_and_i32, { "r", "0", "ri" } },
2191 2192
    { INDEX_op_or_i32, { "r", "0", "rO" } },
    { INDEX_op_xor_i32, { "r", "0", "rX" } },
R
Richard Henderson 已提交
2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210

    { INDEX_op_neg_i32, { "r", "r" } },

    { INDEX_op_shl_i32, { "r", "0", "Ri" } },
    { INDEX_op_shr_i32, { "r", "0", "Ri" } },
    { INDEX_op_sar_i32, { "r", "0", "Ri" } },

    { INDEX_op_rotl_i32, { "r", "r", "Ri" } },
    { INDEX_op_rotr_i32, { "r", "r", "Ri" } },

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

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

2211 2212
    { INDEX_op_add2_i32, { "r", "r", "0", "1", "rA", "r" } },
    { INDEX_op_sub2_i32, { "r", "r", "0", "1", "rA", "r" } },
2213

2214 2215 2216
    { INDEX_op_brcond_i32, { "r", "rC" } },
    { INDEX_op_setcond_i32, { "r", "r", "rC" } },
    { INDEX_op_movcond_i32, { "r", "r", "rC", "r", "0" } },
2217
    { INDEX_op_deposit_i32, { "r", "0", "r" } },
R
Richard Henderson 已提交
2218

2219 2220 2221 2222
    { INDEX_op_qemu_ld_i32, { "r", "L" } },
    { INDEX_op_qemu_ld_i64, { "r", "L" } },
    { INDEX_op_qemu_st_i32, { "L", "L" } },
    { INDEX_op_qemu_st_i64, { "L", "L" } },
R
Richard Henderson 已提交
2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236

    { 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, { "r", "r" } },
    { INDEX_op_st16_i64, { "r", "r" } },
    { INDEX_op_st32_i64, { "r", "r" } },
    { INDEX_op_st_i64, { "r", "r" } },

2237 2238
    { INDEX_op_add_i64, { "r", "r", "ri" } },
    { INDEX_op_sub_i64, { "r", "0", "ri" } },
R
Richard Henderson 已提交
2239 2240 2241 2242
    { INDEX_op_mul_i64, { "r", "0", "rK" } },

    { INDEX_op_div2_i64, { "b", "a", "0", "1", "r" } },
    { INDEX_op_divu2_i64, { "b", "a", "0", "1", "r" } },
2243
    { INDEX_op_mulu2_i64, { "b", "a", "0", "r" } },
R
Richard Henderson 已提交
2244

2245
    { INDEX_op_and_i64, { "r", "0", "ri" } },
R
Richard Henderson 已提交
2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264
    { INDEX_op_or_i64, { "r", "0", "rO" } },
    { INDEX_op_xor_i64, { "r", "0", "rX" } },

    { INDEX_op_neg_i64, { "r", "r" } },

    { INDEX_op_shl_i64, { "r", "r", "Ri" } },
    { INDEX_op_shr_i64, { "r", "r", "Ri" } },
    { INDEX_op_sar_i64, { "r", "r", "Ri" } },

    { INDEX_op_rotl_i64, { "r", "r", "Ri" } },
    { INDEX_op_rotr_i64, { "r", "r", "Ri" } },

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

2265 2266 2267
    { INDEX_op_ext_i32_i64, { "r", "r" } },
    { INDEX_op_extu_i32_i64, { "r", "r" } },

R
Richard Henderson 已提交
2268 2269 2270 2271
    { INDEX_op_bswap16_i64, { "r", "r" } },
    { INDEX_op_bswap32_i64, { "r", "r" } },
    { INDEX_op_bswap64_i64, { "r", "r" } },

2272 2273
    { INDEX_op_add2_i64, { "r", "r", "0", "1", "rA", "r" } },
    { INDEX_op_sub2_i64, { "r", "r", "0", "1", "rA", "r" } },
2274

R
Richard Henderson 已提交
2275 2276
    { INDEX_op_brcond_i64, { "r", "rC" } },
    { INDEX_op_setcond_i64, { "r", "r", "rC" } },
2277
    { INDEX_op_movcond_i64, { "r", "r", "rC", "r", "0" } },
2278
    { INDEX_op_deposit_i64, { "r", "0", "r" } },
R
Richard Henderson 已提交
2279 2280 2281 2282 2283 2284

    { -1 },
};

static void query_facilities(void)
{
2285
    unsigned long hwcap = qemu_getauxval(AT_HWCAP);
R
Richard Henderson 已提交
2286

2287 2288 2289 2290 2291
    /* Is STORE FACILITY LIST EXTENDED available?  Honestly, I believe this
       is present on all 64-bit systems, but let's check for it anyway.  */
    if (hwcap & HWCAP_S390_STFLE) {
        register int r0 __asm__("0");
        register void *r1 __asm__("1");
R
Richard Henderson 已提交
2292

2293 2294 2295 2296
        /* stfle 0(%r1) */
        r1 = &facilities;
        asm volatile(".word 0xb2b0,0x1000"
                     : "=r"(r0) : "0"(0), "r"(r1) : "memory", "cc");
R
Richard Henderson 已提交
2297 2298 2299 2300
    }
}

static void tcg_target_init(TCGContext *s)
A
Alexander Graf 已提交
2301
{
R
Richard Henderson 已提交
2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313
    query_facilities();

    tcg_regset_set32(tcg_target_available_regs[TCG_TYPE_I32], 0, 0xffff);
    tcg_regset_set32(tcg_target_available_regs[TCG_TYPE_I64], 0, 0xffff);

    tcg_regset_clear(tcg_target_call_clobber_regs);
    tcg_regset_set_reg(tcg_target_call_clobber_regs, TCG_REG_R0);
    tcg_regset_set_reg(tcg_target_call_clobber_regs, TCG_REG_R1);
    tcg_regset_set_reg(tcg_target_call_clobber_regs, TCG_REG_R2);
    tcg_regset_set_reg(tcg_target_call_clobber_regs, TCG_REG_R3);
    tcg_regset_set_reg(tcg_target_call_clobber_regs, TCG_REG_R4);
    tcg_regset_set_reg(tcg_target_call_clobber_regs, TCG_REG_R5);
2314 2315 2316
    /* The r6 register is technically call-saved, but it's also a parameter
       register, so it can get killed by setup for the qemu_st helper.  */
    tcg_regset_set_reg(tcg_target_call_clobber_regs, TCG_REG_R6);
R
Richard Henderson 已提交
2317 2318 2319 2320 2321 2322 2323 2324 2325 2326
    /* The return register can be considered call-clobbered.  */
    tcg_regset_set_reg(tcg_target_call_clobber_regs, TCG_REG_R14);

    tcg_regset_clear(s->reserved_regs);
    tcg_regset_set_reg(s->reserved_regs, TCG_TMP0);
    /* XXX many insns can't be used with R0, so we better avoid it for now */
    tcg_regset_set_reg(s->reserved_regs, TCG_REG_R0);
    tcg_regset_set_reg(s->reserved_regs, TCG_REG_CALL_STACK);

    tcg_add_target_add_op_defs(s390_op_defs);
A
Alexander Graf 已提交
2327 2328
}

2329 2330 2331 2332
#define FRAME_SIZE  ((int)(TCG_TARGET_CALL_STACK_OFFSET          \
                           + TCG_STATIC_CALL_ARGS_SIZE           \
                           + CPU_TEMP_BUF_NLONGS * sizeof(long)))

R
Richard Henderson 已提交
2333
static void tcg_target_qemu_prologue(TCGContext *s)
A
Alexander Graf 已提交
2334
{
R
Richard Henderson 已提交
2335 2336 2337
    /* stmg %r6,%r15,48(%r15) (save registers) */
    tcg_out_insn(s, RXY, STMG, TCG_REG_R6, TCG_REG_R15, TCG_REG_R15, 48);

2338
    /* aghi %r15,-frame_size */
2339
    tcg_out_insn(s, RI, AGHI, TCG_REG_R15, -FRAME_SIZE);
2340 2341 2342 2343

    tcg_set_frame(s, TCG_REG_CALL_STACK,
                  TCG_STATIC_CALL_ARGS_SIZE + TCG_TARGET_CALL_STACK_OFFSET,
                  CPU_TEMP_BUF_NLONGS * sizeof(long));
R
Richard Henderson 已提交
2344

L
Laurent Vivier 已提交
2345
#ifndef CONFIG_SOFTMMU
2346 2347
    if (guest_base >= 0x80000) {
        tcg_out_movi(s, TCG_TYPE_PTR, TCG_GUEST_BASE_REG, guest_base);
R
Richard Henderson 已提交
2348 2349
        tcg_regset_set_reg(s->reserved_regs, TCG_GUEST_BASE_REG);
    }
L
Laurent Vivier 已提交
2350
#endif
R
Richard Henderson 已提交
2351

B
Blue Swirl 已提交
2352 2353 2354
    tcg_out_mov(s, TCG_TYPE_PTR, TCG_AREG0, tcg_target_call_iarg_regs[0]);
    /* br %r3 (go to TB) */
    tcg_out_insn(s, RR, BCR, S390_CC_ALWAYS, tcg_target_call_iarg_regs[1]);
R
Richard Henderson 已提交
2355 2356 2357

    tb_ret_addr = s->code_ptr;

2358 2359
    /* lmg %r6,%r15,fs+48(%r15) (restore registers) */
    tcg_out_insn(s, RXY, LMG, TCG_REG_R6, TCG_REG_R15, TCG_REG_R15,
2360
                 FRAME_SIZE + 48);
R
Richard Henderson 已提交
2361 2362 2363

    /* br %r14 (return) */
    tcg_out_insn(s, RR, BCR, S390_CC_ALWAYS, TCG_REG_R14);
A
Alexander Graf 已提交
2364
}
2365 2366

typedef struct {
2367
    DebugFrameHeader h;
2368 2369 2370 2371 2372 2373 2374 2375 2376
    uint8_t fde_def_cfa[4];
    uint8_t fde_reg_ofs[18];
} DebugFrame;

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

#define ELF_HOST_MACHINE  EM_S390

2377 2378 2379 2380 2381 2382 2383
static const DebugFrame debug_frame = {
    .h.cie.len = sizeof(DebugFrameCIE)-4, /* length after .len member */
    .h.cie.id = -1,
    .h.cie.version = 1,
    .h.cie.code_align = 1,
    .h.cie.data_align = 8,                /* sleb128 8 */
    .h.cie.return_column = TCG_REG_R14,
2384 2385

    /* Total FDE size does not include the "len" member.  */
2386
    .h.fde.len = sizeof(DebugFrame) - offsetof(DebugFrame, h.fde.cie_offset),
2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409

    .fde_def_cfa = {
        12, TCG_REG_CALL_STACK,         /* DW_CFA_def_cfa %r15, ... */
        (FRAME_SIZE & 0x7f) | 0x80,     /* ... uleb128 FRAME_SIZE */
        (FRAME_SIZE >> 7)
    },
    .fde_reg_ofs = {
        0x86, 6,                        /* DW_CFA_offset, %r6, 48 */
        0x87, 7,                        /* DW_CFA_offset, %r7, 56 */
        0x88, 8,                        /* DW_CFA_offset, %r8, 64 */
        0x89, 9,                        /* DW_CFA_offset, %r92, 72 */
        0x8a, 10,                       /* DW_CFA_offset, %r10, 80 */
        0x8b, 11,                       /* DW_CFA_offset, %r11, 88 */
        0x8c, 12,                       /* DW_CFA_offset, %r12, 96 */
        0x8d, 13,                       /* DW_CFA_offset, %r13, 104 */
        0x8e, 14,                       /* DW_CFA_offset, %r14, 112 */
    }
};

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