2 * Tiny Code Generator for QEMU
4 * Copyright (c) 2009 Ulrich Hecht <uli@suse.de>
5 * Copyright (c) 2009 Alexander Graf <agraf@suse.de>
6 * Copyright (c) 2010 Richard Henderson <rth@twiddle.net>
8 * Permission is hereby granted, free of charge, to any person obtaining a copy
9 * of this software and associated documentation files (the "Software"), to deal
10 * in the Software without restriction, including without limitation the rights
11 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12 * copies of the Software, and to permit persons to whom the Software is
13 * furnished to do so, subject to the following conditions:
15 * The above copyright notice and this permission notice shall be included in
16 * all copies or substantial portions of the Software.
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
21 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
27 #include "../tcg-ldst.c.inc"
28 #include "../tcg-pool.c.inc"
31 #define TCG_CT_CONST_S16 (1 << 8)
32 #define TCG_CT_CONST_S32 (1 << 9)
33 #define TCG_CT_CONST_S33 (1 << 10)
34 #define TCG_CT_CONST_ZERO (1 << 11)
35 #define TCG_CT_CONST_P32 (1 << 12)
36 #define TCG_CT_CONST_INV (1 << 13)
37 #define TCG_CT_CONST_INVRISBG (1 << 14)
39 #define ALL_GENERAL_REGS MAKE_64BIT_MASK(0, 16)
40 #define ALL_VECTOR_REGS MAKE_64BIT_MASK(32, 32)
42 /* Several places within the instruction set 0 means "no register"
43 rather than TCG_REG_R0. */
44 #define TCG_REG_NONE 0
46 /* A scratch register that may be be used throughout the backend. */
47 #define TCG_TMP0 TCG_REG_R1
49 #define TCG_GUEST_BASE_REG TCG_REG_R13
51 /* All of the following instructions are prefixed with their instruction
52 format, and are defined as 8- or 16-bit quantities, even when the two
53 halves of the 16-bit quantity may appear 32 bits apart in the insn.
54 This makes it easy to copy the values from the tables in Appendix B. */
55 typedef enum S390Opcode {
127 RIEg_LOCGHI = 0xec46,
165 RRFa_MSGRKC = 0xb9ed,
187 RRFam_SELGR = 0xb9e3,
191 RRFc_POPCNT = 0xb9e1,
275 VRRc_VCEQ = 0xe7f8, /* we leave the m5 cs field 0 */
276 VRRc_VCH = 0xe7fb, /* " */
277 VRRc_VCHL = 0xe7f9, /* " */
278 VRRc_VERLLV = 0xe773,
280 VRRc_VESRAV = 0xe77a,
281 VRRc_VESRLV = 0xe778,
294 VRRc_VPKS = 0xe797, /* we leave the m5 cs field 0 */
319 #ifdef CONFIG_DEBUG_TCG
320 static const char * const tcg_target_reg_names[TCG_TARGET_NB_REGS] = {
321 "%r0", "%r1", "%r2", "%r3", "%r4", "%r5", "%r6", "%r7",
322 "%r8", "%r9", "%r10", "%r11", "%r12", "%r13", "%r14", "%r15",
323 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
324 "%v0", "%v1", "%v2", "%v3", "%v4", "%v5", "%v6", "%v7",
325 "%v8", "%v9", "%v10", "%v11", "%v12", "%v13", "%v14", "%v15",
326 "%v16", "%v17", "%v18", "%v19", "%v20", "%v21", "%v22", "%v23",
327 "%v24", "%v25", "%v26", "%v27", "%v28", "%v29", "%v30", "%v31",
331 /* Since R6 is a potential argument register, choose it last of the
332 call-saved registers. Likewise prefer the call-clobbered registers
333 in reverse order to maximize the chance of avoiding the arguments. */
334 static const int tcg_target_reg_alloc_order[] = {
335 /* Call saved registers. */
344 /* Call clobbered registers. */
348 /* Argument registers, in reverse order of allocation. */
354 /* V8-V15 are call saved, and omitted. */
381 static const int tcg_target_call_iarg_regs[] = {
389 static TCGReg tcg_target_call_oarg_reg(TCGCallReturnKind kind, int slot)
391 tcg_debug_assert(kind == TCG_CALL_RET_NORMAL);
392 tcg_debug_assert(slot == 0);
400 #define S390_CC_NE (S390_CC_LT | S390_CC_GT)
401 #define S390_CC_LE (S390_CC_LT | S390_CC_EQ)
402 #define S390_CC_GE (S390_CC_GT | S390_CC_EQ)
403 #define S390_CC_NEVER 0
404 #define S390_CC_ALWAYS 15
406 /* Condition codes that result from a COMPARE and COMPARE LOGICAL. */
407 static const uint8_t tcg_cond_to_s390_cond[] = {
408 [TCG_COND_EQ] = S390_CC_EQ,
409 [TCG_COND_NE] = S390_CC_NE,
410 [TCG_COND_LT] = S390_CC_LT,
411 [TCG_COND_LE] = S390_CC_LE,
412 [TCG_COND_GT] = S390_CC_GT,
413 [TCG_COND_GE] = S390_CC_GE,
414 [TCG_COND_LTU] = S390_CC_LT,
415 [TCG_COND_LEU] = S390_CC_LE,
416 [TCG_COND_GTU] = S390_CC_GT,
417 [TCG_COND_GEU] = S390_CC_GE,
420 /* Condition codes that result from a LOAD AND TEST. Here, we have no
421 unsigned instruction variation, however since the test is vs zero we
422 can re-map the outcomes appropriately. */
423 static const uint8_t tcg_cond_to_ltr_cond[] = {
424 [TCG_COND_EQ] = S390_CC_EQ,
425 [TCG_COND_NE] = S390_CC_NE,
426 [TCG_COND_LT] = S390_CC_LT,
427 [TCG_COND_LE] = S390_CC_LE,
428 [TCG_COND_GT] = S390_CC_GT,
429 [TCG_COND_GE] = S390_CC_GE,
430 [TCG_COND_LTU] = S390_CC_NEVER,
431 [TCG_COND_LEU] = S390_CC_EQ,
432 [TCG_COND_GTU] = S390_CC_NE,
433 [TCG_COND_GEU] = S390_CC_ALWAYS,
436 static const tcg_insn_unit *tb_ret_addr;
437 uint64_t s390_facilities[3];
439 static inline bool is_general_reg(TCGReg r)
441 return r <= TCG_REG_R15;
444 static inline bool is_vector_reg(TCGReg r)
446 return r >= TCG_REG_V0 && r <= TCG_REG_V31;
449 static bool patch_reloc(tcg_insn_unit *src_rw, int type,
450 intptr_t value, intptr_t addend)
452 const tcg_insn_unit *src_rx = tcg_splitwx_to_rx(src_rw);
457 pcrel2 = (tcg_insn_unit *)value - src_rx;
461 if (pcrel2 == (int16_t)pcrel2) {
462 tcg_patch16(src_rw, pcrel2);
467 if (pcrel2 == (int32_t)pcrel2) {
468 tcg_patch32(src_rw, pcrel2);
473 if (value == sextract64(value, 0, 20)) {
474 old = *(uint32_t *)src_rw & 0xf00000ff;
475 old |= ((value & 0xfff) << 16) | ((value & 0xff000) >> 4);
476 tcg_patch32(src_rw, old);
481 g_assert_not_reached();
486 static int is_const_p16(uint64_t val)
488 for (int i = 0; i < 4; ++i) {
489 uint64_t mask = 0xffffull << (i * 16);
490 if ((val & ~mask) == 0) {
497 static int is_const_p32(uint64_t val)
499 if ((val & 0xffffffff00000000ull) == 0) {
502 if ((val & 0x00000000ffffffffull) == 0) {
509 * Accept bit patterns like these:
514 * Copied from gcc sources.
516 static bool risbg_mask(uint64_t c)
519 /* We don't change the number of transitions by inverting,
520 so make sure we start with the LSB zero. */
524 /* Reject all zeros or all ones. */
528 /* Find the first transition. */
530 /* Invert to look for a second transition. */
532 /* Erase the first transition. */
534 /* Find the second transition, if any. */
536 /* Match if all the bits are 1's, or if c is zero. */
540 /* Test if a constant matches the constraint. */
541 static bool tcg_target_const_match(int64_t val, TCGType type, int ct, int vece)
543 if (ct & TCG_CT_CONST) {
547 if (type == TCG_TYPE_I32) {
551 /* The following are mutually exclusive. */
552 if (ct & TCG_CT_CONST_S16) {
553 return val == (int16_t)val;
554 } else if (ct & TCG_CT_CONST_S32) {
555 return val == (int32_t)val;
556 } else if (ct & TCG_CT_CONST_S33) {
557 return val >= -0xffffffffll && val <= 0xffffffffll;
558 } else if (ct & TCG_CT_CONST_ZERO) {
562 if (ct & TCG_CT_CONST_INV) {
566 * Note that is_const_p16 is a subset of is_const_p32,
567 * so we don't need both constraints.
569 if ((ct & TCG_CT_CONST_P32) && is_const_p32(val) >= 0) {
572 if ((ct & TCG_CT_CONST_INVRISBG) && risbg_mask(~val)) {
579 /* Emit instructions according to the given instruction format. */
581 static void tcg_out_insn_RR(TCGContext *s, S390Opcode op, TCGReg r1, TCGReg r2)
583 tcg_out16(s, (op << 8) | (r1 << 4) | r2);
586 static void tcg_out_insn_RRE(TCGContext *s, S390Opcode op,
587 TCGReg r1, TCGReg r2)
589 tcg_out32(s, (op << 16) | (r1 << 4) | r2);
592 /* RRF-a without the m4 field */
593 static void tcg_out_insn_RRFa(TCGContext *s, S390Opcode op,
594 TCGReg r1, TCGReg r2, TCGReg r3)
596 tcg_out32(s, (op << 16) | (r3 << 12) | (r1 << 4) | r2);
599 /* RRF-a with the m4 field */
600 static void tcg_out_insn_RRFam(TCGContext *s, S390Opcode op,
601 TCGReg r1, TCGReg r2, TCGReg r3, int m4)
603 tcg_out32(s, (op << 16) | (r3 << 12) | (m4 << 8) | (r1 << 4) | r2);
606 static void tcg_out_insn_RRFc(TCGContext *s, S390Opcode op,
607 TCGReg r1, TCGReg r2, int m3)
609 tcg_out32(s, (op << 16) | (m3 << 12) | (r1 << 4) | r2);
612 static void tcg_out_insn_RI(TCGContext *s, S390Opcode op, TCGReg r1, int i2)
614 tcg_out32(s, (op << 16) | (r1 << 20) | (i2 & 0xffff));
617 static void tcg_out_insn_RIEg(TCGContext *s, S390Opcode op, TCGReg r1,
620 tcg_out16(s, (op & 0xff00) | (r1 << 4) | m3);
621 tcg_out32(s, (i2 << 16) | (op & 0xff));
624 static void tcg_out_insn_RIL(TCGContext *s, S390Opcode op, TCGReg r1, int i2)
626 tcg_out16(s, op | (r1 << 4));
630 static void tcg_out_insn_RS(TCGContext *s, S390Opcode op, TCGReg r1,
631 TCGReg b2, TCGReg r3, int disp)
633 tcg_out32(s, (op << 24) | (r1 << 20) | (r3 << 16) | (b2 << 12)
637 static void tcg_out_insn_RSY(TCGContext *s, S390Opcode op, TCGReg r1,
638 TCGReg b2, TCGReg r3, int disp)
640 tcg_out16(s, (op & 0xff00) | (r1 << 4) | r3);
641 tcg_out32(s, (op & 0xff) | (b2 << 28)
642 | ((disp & 0xfff) << 16) | ((disp & 0xff000) >> 4));
645 #define tcg_out_insn_RX tcg_out_insn_RS
646 #define tcg_out_insn_RXY tcg_out_insn_RSY
648 static int RXB(TCGReg v1, TCGReg v2, TCGReg v3, TCGReg v4)
651 * Shift bit 4 of each regno to its corresponding bit of RXB.
652 * RXB itself begins at bit 8 of the instruction so 8 - 4 = 4
653 * is the left-shift of the 4th operand.
655 return ((v1 & 0x10) << (4 + 3))
656 | ((v2 & 0x10) << (4 + 2))
657 | ((v3 & 0x10) << (4 + 1))
658 | ((v4 & 0x10) << (4 + 0));
661 static void tcg_out_insn_VRIa(TCGContext *s, S390Opcode op,
662 TCGReg v1, uint16_t i2, int m3)
664 tcg_debug_assert(is_vector_reg(v1));
665 tcg_out16(s, (op & 0xff00) | ((v1 & 0xf) << 4));
667 tcg_out16(s, (op & 0x00ff) | RXB(v1, 0, 0, 0) | (m3 << 12));
670 static void tcg_out_insn_VRIb(TCGContext *s, S390Opcode op,
671 TCGReg v1, uint8_t i2, uint8_t i3, int m4)
673 tcg_debug_assert(is_vector_reg(v1));
674 tcg_out16(s, (op & 0xff00) | ((v1 & 0xf) << 4));
675 tcg_out16(s, (i2 << 8) | (i3 & 0xff));
676 tcg_out16(s, (op & 0x00ff) | RXB(v1, 0, 0, 0) | (m4 << 12));
679 static void tcg_out_insn_VRIc(TCGContext *s, S390Opcode op,
680 TCGReg v1, uint16_t i2, TCGReg v3, int m4)
682 tcg_debug_assert(is_vector_reg(v1));
683 tcg_debug_assert(is_vector_reg(v3));
684 tcg_out16(s, (op & 0xff00) | ((v1 & 0xf) << 4) | (v3 & 0xf));
686 tcg_out16(s, (op & 0x00ff) | RXB(v1, v3, 0, 0) | (m4 << 12));
689 static void tcg_out_insn_VRRa(TCGContext *s, S390Opcode op,
690 TCGReg v1, TCGReg v2, int m3)
692 tcg_debug_assert(is_vector_reg(v1));
693 tcg_debug_assert(is_vector_reg(v2));
694 tcg_out16(s, (op & 0xff00) | ((v1 & 0xf) << 4) | (v2 & 0xf));
695 tcg_out32(s, (op & 0x00ff) | RXB(v1, v2, 0, 0) | (m3 << 12));
698 static void tcg_out_insn_VRRc(TCGContext *s, S390Opcode op,
699 TCGReg v1, TCGReg v2, TCGReg v3, int m4)
701 tcg_debug_assert(is_vector_reg(v1));
702 tcg_debug_assert(is_vector_reg(v2));
703 tcg_debug_assert(is_vector_reg(v3));
704 tcg_out16(s, (op & 0xff00) | ((v1 & 0xf) << 4) | (v2 & 0xf));
705 tcg_out16(s, v3 << 12);
706 tcg_out16(s, (op & 0x00ff) | RXB(v1, v2, v3, 0) | (m4 << 12));
709 static void tcg_out_insn_VRRe(TCGContext *s, S390Opcode op,
710 TCGReg v1, TCGReg v2, TCGReg v3, TCGReg v4)
712 tcg_debug_assert(is_vector_reg(v1));
713 tcg_debug_assert(is_vector_reg(v2));
714 tcg_debug_assert(is_vector_reg(v3));
715 tcg_debug_assert(is_vector_reg(v4));
716 tcg_out16(s, (op & 0xff00) | ((v1 & 0xf) << 4) | (v2 & 0xf));
717 tcg_out16(s, v3 << 12);
718 tcg_out16(s, (op & 0x00ff) | RXB(v1, v2, v3, v4) | (v4 << 12));
721 static void tcg_out_insn_VRRf(TCGContext *s, S390Opcode op,
722 TCGReg v1, TCGReg r2, TCGReg r3)
724 tcg_debug_assert(is_vector_reg(v1));
725 tcg_debug_assert(is_general_reg(r2));
726 tcg_debug_assert(is_general_reg(r3));
727 tcg_out16(s, (op & 0xff00) | ((v1 & 0xf) << 4) | r2);
728 tcg_out16(s, r3 << 12);
729 tcg_out16(s, (op & 0x00ff) | RXB(v1, 0, 0, 0));
732 static void tcg_out_insn_VRSa(TCGContext *s, S390Opcode op, TCGReg v1,
733 intptr_t d2, TCGReg b2, TCGReg v3, int m4)
735 tcg_debug_assert(is_vector_reg(v1));
736 tcg_debug_assert(d2 >= 0 && d2 <= 0xfff);
737 tcg_debug_assert(is_general_reg(b2));
738 tcg_debug_assert(is_vector_reg(v3));
739 tcg_out16(s, (op & 0xff00) | ((v1 & 0xf) << 4) | (v3 & 0xf));
740 tcg_out16(s, b2 << 12 | d2);
741 tcg_out16(s, (op & 0x00ff) | RXB(v1, v3, 0, 0) | (m4 << 12));
744 static void tcg_out_insn_VRSb(TCGContext *s, S390Opcode op, TCGReg v1,
745 intptr_t d2, TCGReg b2, TCGReg r3, int m4)
747 tcg_debug_assert(is_vector_reg(v1));
748 tcg_debug_assert(d2 >= 0 && d2 <= 0xfff);
749 tcg_debug_assert(is_general_reg(b2));
750 tcg_debug_assert(is_general_reg(r3));
751 tcg_out16(s, (op & 0xff00) | ((v1 & 0xf) << 4) | r3);
752 tcg_out16(s, b2 << 12 | d2);
753 tcg_out16(s, (op & 0x00ff) | RXB(v1, 0, 0, 0) | (m4 << 12));
756 static void tcg_out_insn_VRSc(TCGContext *s, S390Opcode op, TCGReg r1,
757 intptr_t d2, TCGReg b2, TCGReg v3, int m4)
759 tcg_debug_assert(is_general_reg(r1));
760 tcg_debug_assert(d2 >= 0 && d2 <= 0xfff);
761 tcg_debug_assert(is_general_reg(b2));
762 tcg_debug_assert(is_vector_reg(v3));
763 tcg_out16(s, (op & 0xff00) | (r1 << 4) | (v3 & 0xf));
764 tcg_out16(s, b2 << 12 | d2);
765 tcg_out16(s, (op & 0x00ff) | RXB(0, v3, 0, 0) | (m4 << 12));
768 static void tcg_out_insn_VRX(TCGContext *s, S390Opcode op, TCGReg v1,
769 TCGReg b2, TCGReg x2, intptr_t d2, int m3)
771 tcg_debug_assert(is_vector_reg(v1));
772 tcg_debug_assert(d2 >= 0 && d2 <= 0xfff);
773 tcg_debug_assert(is_general_reg(x2));
774 tcg_debug_assert(is_general_reg(b2));
775 tcg_out16(s, (op & 0xff00) | ((v1 & 0xf) << 4) | x2);
776 tcg_out16(s, (b2 << 12) | d2);
777 tcg_out16(s, (op & 0x00ff) | RXB(v1, 0, 0, 0) | (m3 << 12));
780 /* Emit an opcode with "type-checking" of the format. */
781 #define tcg_out_insn(S, FMT, OP, ...) \
782 glue(tcg_out_insn_,FMT)(S, glue(glue(FMT,_),OP), ## __VA_ARGS__)
785 /* emit 64-bit shifts */
786 static void tcg_out_sh64(TCGContext* s, S390Opcode op, TCGReg dest,
787 TCGReg src, TCGReg sh_reg, int sh_imm)
789 tcg_out_insn_RSY(s, op, dest, sh_reg, src, sh_imm);
792 /* emit 32-bit shifts */
793 static void tcg_out_sh32(TCGContext* s, S390Opcode op, TCGReg dest,
794 TCGReg sh_reg, int sh_imm)
796 tcg_out_insn_RS(s, op, dest, sh_reg, 0, sh_imm);
799 static bool tcg_out_mov(TCGContext *s, TCGType type, TCGReg dst, TCGReg src)
806 if (likely(is_general_reg(dst) && is_general_reg(src))) {
807 tcg_out_insn(s, RR, LR, dst, src);
813 if (likely(is_general_reg(dst))) {
814 if (likely(is_general_reg(src))) {
815 tcg_out_insn(s, RRE, LGR, dst, src);
817 tcg_out_insn(s, VRSc, VLGV, dst, 0, 0, src, 3);
820 } else if (is_general_reg(src)) {
821 tcg_out_insn(s, VRSb, VLVG, dst, 0, 0, src, 3);
828 tcg_out_insn(s, VRRa, VLR, dst, src, 0);
832 g_assert_not_reached();
837 static const S390Opcode li_insns[4] = {
838 RI_LLILL, RI_LLILH, RI_LLIHL, RI_LLIHH
840 static const S390Opcode oi_insns[4] = {
841 RI_OILL, RI_OILH, RI_OIHL, RI_OIHH
843 static const S390Opcode lif_insns[2] = {
844 RIL_LLILF, RIL_LLIHF,
847 /* load a register with an immediate value */
848 static void tcg_out_movi(TCGContext *s, TCGType type,
849 TCGReg ret, tcg_target_long sval)
851 tcg_target_ulong uval = sval;
855 if (type == TCG_TYPE_I32) {
856 uval = (uint32_t)sval;
857 sval = (int32_t)sval;
860 /* Try all 32-bit insns that can load it in one go. */
861 if (sval >= -0x8000 && sval < 0x8000) {
862 tcg_out_insn(s, RI, LGHI, ret, sval);
866 i = is_const_p16(uval);
868 tcg_out_insn_RI(s, li_insns[i], ret, uval >> (i * 16));
872 /* Try all 48-bit insns that can load it in one go. */
873 if (sval == (int32_t)sval) {
874 tcg_out_insn(s, RIL, LGFI, ret, sval);
878 i = is_const_p32(uval);
880 tcg_out_insn_RIL(s, lif_insns[i], ret, uval >> (i * 32));
884 /* Try for PC-relative address load. For odd addresses, add one. */
885 pc_off = tcg_pcrel_diff(s, (void *)sval) >> 1;
886 if (pc_off == (int32_t)pc_off) {
887 tcg_out_insn(s, RIL, LARL, ret, pc_off);
889 tcg_out_insn(s, RI, AGHI, ret, 1);
894 /* Otherwise, load it by parts. */
895 i = is_const_p16((uint32_t)uval);
897 tcg_out_insn_RI(s, li_insns[i], ret, uval >> (i * 16));
899 tcg_out_insn(s, RIL, LLILF, ret, uval);
902 i = is_const_p16(uval);
904 tcg_out_insn_RI(s, oi_insns[i + 2], ret, uval >> (i * 16));
906 tcg_out_insn(s, RIL, OIHF, ret, uval);
910 /* Emit a load/store type instruction. Inputs are:
911 DATA: The register to be loaded or stored.
912 BASE+OFS: The effective address.
913 OPC_RX: If the operation has an RX format opcode (e.g. STC), otherwise 0.
914 OPC_RXY: The RXY format opcode for the operation (e.g. STCY). */
916 static void tcg_out_mem(TCGContext *s, S390Opcode opc_rx, S390Opcode opc_rxy,
917 TCGReg data, TCGReg base, TCGReg index,
920 if (ofs < -0x80000 || ofs >= 0x80000) {
921 /* Combine the low 20 bits of the offset with the actual load insn;
922 the high 44 bits must come from an immediate load. */
923 tcg_target_long low = ((ofs & 0xfffff) ^ 0x80000) - 0x80000;
924 tcg_out_movi(s, TCG_TYPE_PTR, TCG_TMP0, ofs - low);
927 /* If we were already given an index register, add it in. */
928 if (index != TCG_REG_NONE) {
929 tcg_out_insn(s, RRE, AGR, TCG_TMP0, index);
934 if (opc_rx && ofs >= 0 && ofs < 0x1000) {
935 tcg_out_insn_RX(s, opc_rx, data, base, index, ofs);
937 tcg_out_insn_RXY(s, opc_rxy, data, base, index, ofs);
941 static void tcg_out_vrx_mem(TCGContext *s, S390Opcode opc_vrx,
942 TCGReg data, TCGReg base, TCGReg index,
943 tcg_target_long ofs, int m3)
945 if (ofs < 0 || ofs >= 0x1000) {
946 if (ofs >= -0x80000 && ofs < 0x80000) {
947 tcg_out_insn(s, RXY, LAY, TCG_TMP0, base, index, ofs);
949 index = TCG_REG_NONE;
952 tcg_out_movi(s, TCG_TYPE_PTR, TCG_TMP0, ofs);
953 if (index != TCG_REG_NONE) {
954 tcg_out_insn(s, RRE, AGR, TCG_TMP0, index);
960 tcg_out_insn_VRX(s, opc_vrx, data, base, index, ofs, m3);
963 /* load data without address translation or endianness conversion */
964 static void tcg_out_ld(TCGContext *s, TCGType type, TCGReg data,
965 TCGReg base, intptr_t ofs)
969 if (likely(is_general_reg(data))) {
970 tcg_out_mem(s, RX_L, RXY_LY, data, base, TCG_REG_NONE, ofs);
973 tcg_out_vrx_mem(s, VRX_VLLEZ, data, base, TCG_REG_NONE, ofs, MO_32);
977 if (likely(is_general_reg(data))) {
978 tcg_out_mem(s, 0, RXY_LG, data, base, TCG_REG_NONE, ofs);
984 tcg_out_vrx_mem(s, VRX_VLLEZ, data, base, TCG_REG_NONE, ofs, MO_64);
988 /* Hint quadword aligned. */
989 tcg_out_vrx_mem(s, VRX_VL, data, base, TCG_REG_NONE, ofs, 4);
993 g_assert_not_reached();
997 static void tcg_out_st(TCGContext *s, TCGType type, TCGReg data,
998 TCGReg base, intptr_t ofs)
1002 if (likely(is_general_reg(data))) {
1003 tcg_out_mem(s, RX_ST, RXY_STY, data, base, TCG_REG_NONE, ofs);
1005 tcg_out_vrx_mem(s, VRX_VSTEF, data, base, TCG_REG_NONE, ofs, 1);
1010 if (likely(is_general_reg(data))) {
1011 tcg_out_mem(s, 0, RXY_STG, data, base, TCG_REG_NONE, ofs);
1017 tcg_out_vrx_mem(s, VRX_VSTEG, data, base, TCG_REG_NONE, ofs, 0);
1021 /* Hint quadword aligned. */
1022 tcg_out_vrx_mem(s, VRX_VST, data, base, TCG_REG_NONE, ofs, 4);
1026 g_assert_not_reached();
1030 static inline bool tcg_out_sti(TCGContext *s, TCGType type, TCGArg val,
1031 TCGReg base, intptr_t ofs)
1036 static bool tcg_out_xchg(TCGContext *s, TCGType type, TCGReg r1, TCGReg r2)
1041 static void tcg_out_addi_ptr(TCGContext *s, TCGReg rd, TCGReg rs,
1042 tcg_target_long imm)
1044 /* This function is only used for passing structs by reference. */
1045 tcg_out_mem(s, RX_LA, RXY_LAY, rd, rs, TCG_REG_NONE, imm);
1048 static inline void tcg_out_risbg(TCGContext *s, TCGReg dest, TCGReg src,
1049 int msb, int lsb, int ofs, int z)
1052 tcg_out16(s, (RIEf_RISBG & 0xff00) | (dest << 4) | src);
1053 tcg_out16(s, (msb << 8) | (z << 7) | lsb);
1054 tcg_out16(s, (ofs << 8) | (RIEf_RISBG & 0xff));
1057 static void tcg_out_ext8s(TCGContext *s, TCGType type, TCGReg dest, TCGReg src)
1059 tcg_out_insn(s, RRE, LGBR, dest, src);
1062 static void tcg_out_ext8u(TCGContext *s, TCGReg dest, TCGReg src)
1064 tcg_out_insn(s, RRE, LLGCR, dest, src);
1067 static void tcg_out_ext16s(TCGContext *s, TCGType type, TCGReg dest, TCGReg src)
1069 tcg_out_insn(s, RRE, LGHR, dest, src);
1072 static void tcg_out_ext16u(TCGContext *s, TCGReg dest, TCGReg src)
1074 tcg_out_insn(s, RRE, LLGHR, dest, src);
1077 static void tcg_out_ext32s(TCGContext *s, TCGReg dest, TCGReg src)
1079 tcg_out_insn(s, RRE, LGFR, dest, src);
1082 static void tcg_out_ext32u(TCGContext *s, TCGReg dest, TCGReg src)
1084 tcg_out_insn(s, RRE, LLGFR, dest, src);
1087 static void tcg_out_exts_i32_i64(TCGContext *s, TCGReg dest, TCGReg src)
1089 tcg_out_ext32s(s, dest, src);
1092 static void tcg_out_extu_i32_i64(TCGContext *s, TCGReg dest, TCGReg src)
1094 tcg_out_ext32u(s, dest, src);
1097 static void tcg_out_extrl_i64_i32(TCGContext *s, TCGReg dest, TCGReg src)
1099 tcg_out_mov(s, TCG_TYPE_I32, dest, src);
1102 static void tgen_andi_risbg(TCGContext *s, TCGReg out, TCGReg in, uint64_t val)
1105 if ((val & 0x8000000000000001ull) == 0x8000000000000001ull) {
1106 /* Achieve wraparound by swapping msb and lsb. */
1107 msb = 64 - ctz64(~val);
1108 lsb = clz64(~val) - 1;
1111 lsb = 63 - ctz64(val);
1113 tcg_out_risbg(s, out, in, msb, lsb, 0, 1);
1116 static void tgen_andi(TCGContext *s, TCGType type, TCGReg dest, uint64_t val)
1118 static const S390Opcode ni_insns[4] = {
1119 RI_NILL, RI_NILH, RI_NIHL, RI_NIHH
1121 static const S390Opcode nif_insns[2] = {
1124 uint64_t valid = (type == TCG_TYPE_I32 ? 0xffffffffull : -1ull);
1127 /* Look for the zero-extensions. */
1128 if ((val & valid) == 0xffffffff) {
1129 tcg_out_ext32u(s, dest, dest);
1132 if ((val & valid) == 0xff) {
1133 tcg_out_ext8u(s, dest, dest);
1136 if ((val & valid) == 0xffff) {
1137 tcg_out_ext16u(s, dest, dest);
1141 i = is_const_p16(~val & valid);
1143 tcg_out_insn_RI(s, ni_insns[i], dest, val >> (i * 16));
1147 i = is_const_p32(~val & valid);
1148 tcg_debug_assert(i == 0 || type != TCG_TYPE_I32);
1150 tcg_out_insn_RIL(s, nif_insns[i], dest, val >> (i * 32));
1154 if (risbg_mask(val)) {
1155 tgen_andi_risbg(s, dest, dest, val);
1159 g_assert_not_reached();
1162 static void tgen_ori(TCGContext *s, TCGReg dest, uint64_t val)
1164 static const S390Opcode oif_insns[2] = {
1170 i = is_const_p16(val);
1172 tcg_out_insn_RI(s, oi_insns[i], dest, val >> (i * 16));
1176 i = is_const_p32(val);
1178 tcg_out_insn_RIL(s, oif_insns[i], dest, val >> (i * 32));
1182 g_assert_not_reached();
1185 static void tgen_xori(TCGContext *s, TCGReg dest, uint64_t val)
1187 switch (is_const_p32(val)) {
1189 tcg_out_insn(s, RIL, XILF, dest, val);
1192 tcg_out_insn(s, RIL, XIHF, dest, val >> 32);
1195 g_assert_not_reached();
1199 static int tgen_cmp2(TCGContext *s, TCGType type, TCGCond c, TCGReg r1,
1200 TCGArg c2, bool c2const, bool need_carry, int *inv_cc)
1202 bool is_unsigned = is_unsigned_cond(c);
1203 TCGCond inv_c = tcg_invert_cond(c);
1208 if (!(is_unsigned && need_carry)) {
1209 if (type == TCG_TYPE_I32) {
1210 tcg_out_insn(s, RR, LTR, r1, r1);
1212 tcg_out_insn(s, RRE, LTGR, r1, r1);
1214 *inv_cc = tcg_cond_to_ltr_cond[inv_c];
1215 return tcg_cond_to_ltr_cond[c];
1219 if (!is_unsigned && c2 == (int16_t)c2) {
1220 op = (type == TCG_TYPE_I32 ? RI_CHI : RI_CGHI);
1221 tcg_out_insn_RI(s, op, r1, c2);
1225 if (type == TCG_TYPE_I32) {
1226 op = (is_unsigned ? RIL_CLFI : RIL_CFI);
1227 tcg_out_insn_RIL(s, op, r1, c2);
1232 * Constraints are for a signed 33-bit operand, which is a
1233 * convenient superset of this signed/unsigned test.
1235 if (c2 == (is_unsigned ? (TCGArg)(uint32_t)c2 : (TCGArg)(int32_t)c2)) {
1236 op = (is_unsigned ? RIL_CLGFI : RIL_CGFI);
1237 tcg_out_insn_RIL(s, op, r1, c2);
1241 /* Load everything else into a register. */
1242 tcg_out_movi(s, TCG_TYPE_I64, TCG_TMP0, c2);
1246 if (type == TCG_TYPE_I32) {
1247 op = (is_unsigned ? RR_CLR : RR_CR);
1248 tcg_out_insn_RR(s, op, r1, c2);
1250 op = (is_unsigned ? RRE_CLGR : RRE_CGR);
1251 tcg_out_insn_RRE(s, op, r1, c2);
1255 *inv_cc = tcg_cond_to_s390_cond[inv_c];
1256 return tcg_cond_to_s390_cond[c];
1259 static int tgen_cmp(TCGContext *s, TCGType type, TCGCond c, TCGReg r1,
1260 TCGArg c2, bool c2const, bool need_carry)
1263 return tgen_cmp2(s, type, c, r1, c2, c2const, need_carry, &inv_cc);
1266 static void tgen_setcond(TCGContext *s, TCGType type, TCGCond cond,
1267 TCGReg dest, TCGReg c1, TCGArg c2,
1268 bool c2const, bool neg)
1272 /* With LOC2, we can always emit the minimum 3 insns. */
1273 if (HAVE_FACILITY(LOAD_ON_COND2)) {
1274 /* Emit: d = 0, d = (cc ? 1 : d). */
1275 cc = tgen_cmp(s, type, cond, c1, c2, c2const, false);
1276 tcg_out_movi(s, TCG_TYPE_I64, dest, 0);
1277 tcg_out_insn(s, RIEg, LOCGHI, dest, neg ? -1 : 1, cc);
1286 /* Swap operands so that we can use LEU/GTU/GT/LE. */
1291 cond = tcg_swap_cond(cond);
1300 /* X != 0 is X > 0. */
1301 if (c2const && c2 == 0) {
1302 cond = TCG_COND_GTU;
1311 * The result of a compare has CC=2 for GT and CC=3 unused.
1312 * ADD LOGICAL WITH CARRY considers (CC & 2) the carry bit.
1314 tgen_cmp(s, type, cond, c1, c2, c2const, true);
1315 tcg_out_movi(s, type, dest, 0);
1316 tcg_out_insn(s, RRE, ALCGR, dest, dest);
1318 if (type == TCG_TYPE_I32) {
1319 tcg_out_insn(s, RR, LCR, dest, dest);
1321 tcg_out_insn(s, RRE, LCGR, dest, dest);
1327 /* X == 0 is X <= 0. */
1328 if (c2const && c2 == 0) {
1329 cond = TCG_COND_LEU;
1338 * As above, but we're looking for borrow, or !carry.
1339 * The second insn computes d - d - borrow, or -1 for true
1340 * and 0 for false. So we must mask to 1 bit afterward.
1342 tgen_cmp(s, type, cond, c1, c2, c2const, true);
1343 tcg_out_insn(s, RRE, SLBGR, dest, dest);
1345 tgen_andi(s, type, dest, 1);
1350 g_assert_not_reached();
1353 cc = tgen_cmp(s, type, cond, c1, c2, c2const, false);
1354 /* Emit: d = 0, t = 1, d = (cc ? t : d). */
1355 tcg_out_movi(s, TCG_TYPE_I64, dest, 0);
1356 tcg_out_movi(s, TCG_TYPE_I64, TCG_TMP0, neg ? -1 : 1);
1357 tcg_out_insn(s, RRFc, LOCGR, dest, TCG_TMP0, cc);
1360 static void tgen_movcond_int(TCGContext *s, TCGType type, TCGReg dest,
1361 TCGArg v3, int v3const, TCGReg v4,
1368 if (HAVE_FACILITY(LOAD_ON_COND2)) {
1369 /* Emit: if (cc) dest = v3. */
1370 tcg_out_insn(s, RIEg, LOCGHI, dest, v3, cc);
1373 tcg_out_insn(s, RI, LGHI, TCG_TMP0, v3);
1376 /* LGR+LOCGHI is larger than LGHI+LOCGR. */
1377 tcg_out_insn(s, RI, LGHI, dest, v3);
1382 if (HAVE_FACILITY(MISC_INSN_EXT3)) {
1383 /* Emit: dest = cc ? v3 : v4. */
1384 tcg_out_insn(s, RRFam, SELGR, dest, v3, v4, cc);
1390 tcg_out_mov(s, type, dest, v3);
1396 /* Emit: if (cc) dest = src. */
1397 tcg_out_insn(s, RRFc, LOCGR, dest, src, cc);
1400 static void tgen_movcond(TCGContext *s, TCGType type, TCGCond c, TCGReg dest,
1401 TCGReg c1, TCGArg c2, int c2const,
1402 TCGArg v3, int v3const, TCGReg v4)
1406 cc = tgen_cmp2(s, type, c, c1, c2, c2const, false, &inv_cc);
1407 tgen_movcond_int(s, type, dest, v3, v3const, v4, cc, inv_cc);
1410 static void tgen_clz(TCGContext *s, TCGReg dest, TCGReg a1,
1411 TCGArg a2, int a2const)
1413 /* Since this sets both R and R+1, we have no choice but to store the
1414 result into R0, allowing R1 == TCG_TMP0 to be clobbered as well. */
1415 QEMU_BUILD_BUG_ON(TCG_TMP0 != TCG_REG_R1);
1416 tcg_out_insn(s, RRE, FLOGR, TCG_REG_R0, a1);
1418 if (a2const && a2 == 64) {
1419 tcg_out_mov(s, TCG_TYPE_I64, dest, TCG_REG_R0);
1424 * Conditions from FLOGR are:
1425 * 2 -> one bit found
1426 * 8 -> no one bit found
1428 tgen_movcond_int(s, TCG_TYPE_I64, dest, a2, a2const, TCG_REG_R0, 8, 2);
1431 static void tgen_ctpop(TCGContext *s, TCGType type, TCGReg dest, TCGReg src)
1433 /* With MIE3, and bit 0 of m4 set, we get the complete result. */
1434 if (HAVE_FACILITY(MISC_INSN_EXT3)) {
1435 if (type == TCG_TYPE_I32) {
1436 tcg_out_ext32u(s, dest, src);
1439 tcg_out_insn(s, RRFc, POPCNT, dest, src, 8);
1443 /* Without MIE3, each byte gets the count of bits for the byte. */
1444 tcg_out_insn(s, RRFc, POPCNT, dest, src, 0);
1446 /* Multiply to sum each byte at the top of the word. */
1447 if (type == TCG_TYPE_I32) {
1448 tcg_out_insn(s, RIL, MSFI, dest, 0x01010101);
1449 tcg_out_sh32(s, RS_SRL, dest, TCG_REG_NONE, 24);
1451 tcg_out_movi(s, TCG_TYPE_I64, TCG_TMP0, 0x0101010101010101ull);
1452 tcg_out_insn(s, RRE, MSGR, dest, TCG_TMP0);
1453 tcg_out_sh64(s, RSY_SRLG, dest, dest, TCG_REG_NONE, 56);
1457 static void tgen_deposit(TCGContext *s, TCGReg dest, TCGReg src,
1458 int ofs, int len, int z)
1460 int lsb = (63 - ofs);
1461 int msb = lsb - (len - 1);
1462 tcg_out_risbg(s, dest, src, msb, lsb, ofs, z);
1465 static void tgen_extract(TCGContext *s, TCGReg dest, TCGReg src,
1468 tcg_out_risbg(s, dest, src, 64 - len, 63, 64 - ofs, 1);
1471 static void tgen_gotoi(TCGContext *s, int cc, const tcg_insn_unit *dest)
1473 ptrdiff_t off = tcg_pcrel_diff(s, dest) >> 1;
1474 if (off == (int16_t)off) {
1475 tcg_out_insn(s, RI, BRC, cc, off);
1476 } else if (off == (int32_t)off) {
1477 tcg_out_insn(s, RIL, BRCL, cc, off);
1479 tcg_out_movi(s, TCG_TYPE_PTR, TCG_TMP0, (uintptr_t)dest);
1480 tcg_out_insn(s, RR, BCR, cc, TCG_TMP0);
1484 static void tgen_branch(TCGContext *s, int cc, TCGLabel *l)
1487 tgen_gotoi(s, cc, l->u.value_ptr);
1489 tcg_out16(s, RI_BRC | (cc << 4));
1490 tcg_out_reloc(s, s->code_ptr, R_390_PC16DBL, l, 2);
1495 static void tgen_compare_branch(TCGContext *s, S390Opcode opc, int cc,
1496 TCGReg r1, TCGReg r2, TCGLabel *l)
1498 tcg_out_reloc(s, s->code_ptr + 1, R_390_PC16DBL, l, 2);
1500 tcg_out16(s, (opc & 0xff00) | (r1 << 4) | r2);
1502 tcg_out16(s, cc << 12 | (opc & 0xff));
1505 static void tgen_compare_imm_branch(TCGContext *s, S390Opcode opc, int cc,
1506 TCGReg r1, int i2, TCGLabel *l)
1508 tcg_out_reloc(s, s->code_ptr + 1, R_390_PC16DBL, l, 2);
1510 tcg_out16(s, (opc & 0xff00) | (r1 << 4) | cc);
1512 tcg_out16(s, (i2 << 8) | (opc & 0xff));
1515 static void tgen_brcond(TCGContext *s, TCGType type, TCGCond c,
1516 TCGReg r1, TCGArg c2, int c2const, TCGLabel *l)
1519 bool is_unsigned = is_unsigned_cond(c);
1523 cc = tcg_cond_to_s390_cond[c];
1526 opc = (type == TCG_TYPE_I32
1527 ? (is_unsigned ? RIEb_CLRJ : RIEb_CRJ)
1528 : (is_unsigned ? RIEb_CLGRJ : RIEb_CGRJ));
1529 tgen_compare_branch(s, opc, cc, r1, c2, l);
1534 * COMPARE IMMEDIATE AND BRANCH RELATIVE has an 8-bit immediate field.
1535 * If the immediate we've been given does not fit that range, we'll
1536 * fall back to separate compare and branch instructions using the
1537 * larger comparison range afforded by COMPARE IMMEDIATE.
1539 if (type == TCG_TYPE_I32) {
1542 in_range = (uint32_t)c2 == (uint8_t)c2;
1545 in_range = (int32_t)c2 == (int8_t)c2;
1550 in_range = (uint64_t)c2 == (uint8_t)c2;
1553 in_range = (int64_t)c2 == (int8_t)c2;
1557 tgen_compare_imm_branch(s, opc, cc, r1, c2, l);
1561 cc = tgen_cmp(s, type, c, r1, c2, c2const, false);
1562 tgen_branch(s, cc, l);
1565 static void tcg_out_call_int(TCGContext *s, const tcg_insn_unit *dest)
1567 ptrdiff_t off = tcg_pcrel_diff(s, dest) >> 1;
1568 if (off == (int32_t)off) {
1569 tcg_out_insn(s, RIL, BRASL, TCG_REG_R14, off);
1571 tcg_out_movi(s, TCG_TYPE_PTR, TCG_TMP0, (uintptr_t)dest);
1572 tcg_out_insn(s, RR, BASR, TCG_REG_R14, TCG_TMP0);
1576 static void tcg_out_call(TCGContext *s, const tcg_insn_unit *dest,
1577 const TCGHelperInfo *info)
1579 tcg_out_call_int(s, dest);
1589 bool tcg_target_has_memory_bswap(MemOp memop)
1593 if ((memop & MO_SIZE) <= MO_64) {
1598 * Reject 16-byte memop with 16-byte atomicity,
1599 * but do allow a pair of 64-bit operations.
1601 aa = atom_and_align_for_opc(tcg_ctx, memop, MO_ATOM_IFALIGN, true);
1602 return aa.atom <= MO_64;
1605 static void tcg_out_qemu_ld_direct(TCGContext *s, MemOp opc, TCGReg data,
1608 switch (opc & (MO_SSIZE | MO_BSWAP)) {
1610 tcg_out_insn(s, RXY, LLGC, data, h.base, h.index, h.disp);
1613 tcg_out_insn(s, RXY, LGB, data, h.base, h.index, h.disp);
1616 case MO_UW | MO_BSWAP:
1617 /* swapped unsigned halfword load with upper bits zeroed */
1618 tcg_out_insn(s, RXY, LRVH, data, h.base, h.index, h.disp);
1619 tcg_out_ext16u(s, data, data);
1622 tcg_out_insn(s, RXY, LLGH, data, h.base, h.index, h.disp);
1625 case MO_SW | MO_BSWAP:
1626 /* swapped sign-extended halfword load */
1627 tcg_out_insn(s, RXY, LRVH, data, h.base, h.index, h.disp);
1628 tcg_out_ext16s(s, TCG_TYPE_REG, data, data);
1631 tcg_out_insn(s, RXY, LGH, data, h.base, h.index, h.disp);
1634 case MO_UL | MO_BSWAP:
1635 /* swapped unsigned int load with upper bits zeroed */
1636 tcg_out_insn(s, RXY, LRV, data, h.base, h.index, h.disp);
1637 tcg_out_ext32u(s, data, data);
1640 tcg_out_insn(s, RXY, LLGF, data, h.base, h.index, h.disp);
1643 case MO_SL | MO_BSWAP:
1644 /* swapped sign-extended int load */
1645 tcg_out_insn(s, RXY, LRV, data, h.base, h.index, h.disp);
1646 tcg_out_ext32s(s, data, data);
1649 tcg_out_insn(s, RXY, LGF, data, h.base, h.index, h.disp);
1652 case MO_UQ | MO_BSWAP:
1653 tcg_out_insn(s, RXY, LRVG, data, h.base, h.index, h.disp);
1656 tcg_out_insn(s, RXY, LG, data, h.base, h.index, h.disp);
1660 g_assert_not_reached();
1664 static void tcg_out_qemu_st_direct(TCGContext *s, MemOp opc, TCGReg data,
1667 switch (opc & (MO_SIZE | MO_BSWAP)) {
1669 if (h.disp >= 0 && h.disp < 0x1000) {
1670 tcg_out_insn(s, RX, STC, data, h.base, h.index, h.disp);
1672 tcg_out_insn(s, RXY, STCY, data, h.base, h.index, h.disp);
1676 case MO_UW | MO_BSWAP:
1677 tcg_out_insn(s, RXY, STRVH, data, h.base, h.index, h.disp);
1680 if (h.disp >= 0 && h.disp < 0x1000) {
1681 tcg_out_insn(s, RX, STH, data, h.base, h.index, h.disp);
1683 tcg_out_insn(s, RXY, STHY, data, h.base, h.index, h.disp);
1687 case MO_UL | MO_BSWAP:
1688 tcg_out_insn(s, RXY, STRV, data, h.base, h.index, h.disp);
1691 if (h.disp >= 0 && h.disp < 0x1000) {
1692 tcg_out_insn(s, RX, ST, data, h.base, h.index, h.disp);
1694 tcg_out_insn(s, RXY, STY, data, h.base, h.index, h.disp);
1698 case MO_UQ | MO_BSWAP:
1699 tcg_out_insn(s, RXY, STRVG, data, h.base, h.index, h.disp);
1702 tcg_out_insn(s, RXY, STG, data, h.base, h.index, h.disp);
1706 g_assert_not_reached();
1710 static const TCGLdstHelperParam ldst_helper_param = {
1711 .ntmp = 1, .tmp = { TCG_TMP0 }
1714 static bool tcg_out_qemu_ld_slow_path(TCGContext *s, TCGLabelQemuLdst *lb)
1716 MemOp opc = get_memop(lb->oi);
1718 if (!patch_reloc(lb->label_ptr[0], R_390_PC16DBL,
1719 (intptr_t)tcg_splitwx_to_rx(s->code_ptr), 2)) {
1723 tcg_out_ld_helper_args(s, lb, &ldst_helper_param);
1724 tcg_out_call_int(s, qemu_ld_helpers[opc & MO_SIZE]);
1725 tcg_out_ld_helper_ret(s, lb, false, &ldst_helper_param);
1727 tgen_gotoi(s, S390_CC_ALWAYS, lb->raddr);
1731 static bool tcg_out_qemu_st_slow_path(TCGContext *s, TCGLabelQemuLdst *lb)
1733 MemOp opc = get_memop(lb->oi);
1735 if (!patch_reloc(lb->label_ptr[0], R_390_PC16DBL,
1736 (intptr_t)tcg_splitwx_to_rx(s->code_ptr), 2)) {
1740 tcg_out_st_helper_args(s, lb, &ldst_helper_param);
1741 tcg_out_call_int(s, qemu_st_helpers[opc & MO_SIZE]);
1743 tgen_gotoi(s, S390_CC_ALWAYS, lb->raddr);
1747 /* We're expecting to use a 20-bit negative offset on the tlb memory ops. */
1748 #define MIN_TLB_MASK_TABLE_OFS -(1 << 19)
1751 * For system-mode, perform the TLB load and compare.
1752 * For user-mode, perform any required alignment tests.
1753 * In both cases, return a TCGLabelQemuLdst structure if the slow path
1754 * is required and fill in @h with the host address for the fast path.
1756 static TCGLabelQemuLdst *prepare_host_addr(TCGContext *s, HostAddress *h,
1757 TCGReg addr_reg, MemOpIdx oi,
1760 TCGType addr_type = s->addr_type;
1761 TCGLabelQemuLdst *ldst = NULL;
1762 MemOp opc = get_memop(oi);
1763 MemOp s_bits = opc & MO_SIZE;
1766 h->aa = atom_and_align_for_opc(s, opc, MO_ATOM_IFALIGN, s_bits == MO_128);
1767 a_mask = (1 << h->aa.align) - 1;
1769 if (tcg_use_softmmu) {
1770 unsigned s_mask = (1 << s_bits) - 1;
1771 int mem_index = get_mmuidx(oi);
1772 int fast_off = tlb_mask_table_ofs(s, mem_index);
1773 int mask_off = fast_off + offsetof(CPUTLBDescFast, mask);
1774 int table_off = fast_off + offsetof(CPUTLBDescFast, table);
1778 ldst = new_ldst_label(s);
1779 ldst->is_ld = is_ld;
1781 ldst->addrlo_reg = addr_reg;
1783 tcg_out_sh64(s, RSY_SRLG, TCG_TMP0, addr_reg, TCG_REG_NONE,
1784 s->page_bits - CPU_TLB_ENTRY_BITS);
1786 tcg_out_insn(s, RXY, NG, TCG_TMP0, TCG_AREG0, TCG_REG_NONE, mask_off);
1787 tcg_out_insn(s, RXY, AG, TCG_TMP0, TCG_AREG0, TCG_REG_NONE, table_off);
1790 * For aligned accesses, we check the first byte and include the
1791 * alignment bits within the address. For unaligned access, we
1792 * check that we don't cross pages using the address of the last
1793 * byte of the access.
1795 a_off = (a_mask >= s_mask ? 0 : s_mask - a_mask);
1796 tlb_mask = (uint64_t)s->page_mask | a_mask;
1798 tgen_andi_risbg(s, TCG_REG_R0, addr_reg, tlb_mask);
1800 tcg_out_insn(s, RX, LA, TCG_REG_R0, addr_reg, TCG_REG_NONE, a_off);
1801 tgen_andi(s, addr_type, TCG_REG_R0, tlb_mask);
1805 ofs = offsetof(CPUTLBEntry, addr_read);
1807 ofs = offsetof(CPUTLBEntry, addr_write);
1809 if (addr_type == TCG_TYPE_I32) {
1810 ofs += HOST_BIG_ENDIAN * 4;
1811 tcg_out_insn(s, RX, C, TCG_REG_R0, TCG_TMP0, TCG_REG_NONE, ofs);
1813 tcg_out_insn(s, RXY, CG, TCG_REG_R0, TCG_TMP0, TCG_REG_NONE, ofs);
1816 tcg_out16(s, RI_BRC | (S390_CC_NE << 4));
1817 ldst->label_ptr[0] = s->code_ptr++;
1819 h->index = TCG_TMP0;
1820 tcg_out_insn(s, RXY, LG, h->index, TCG_TMP0, TCG_REG_NONE,
1821 offsetof(CPUTLBEntry, addend));
1823 if (addr_type == TCG_TYPE_I32) {
1824 tcg_out_insn(s, RRE, ALGFR, h->index, addr_reg);
1825 h->base = TCG_REG_NONE;
1832 ldst = new_ldst_label(s);
1833 ldst->is_ld = is_ld;
1835 ldst->addrlo_reg = addr_reg;
1837 /* We are expecting a_bits to max out at 7, much lower than TMLL. */
1838 tcg_debug_assert(a_mask <= 0xffff);
1839 tcg_out_insn(s, RI, TMLL, addr_reg, a_mask);
1841 tcg_out16(s, RI_BRC | (7 << 4)); /* CC in {1,2,3} */
1842 ldst->label_ptr[0] = s->code_ptr++;
1846 if (addr_type == TCG_TYPE_I32) {
1847 tcg_out_ext32u(s, TCG_TMP0, addr_reg);
1850 if (guest_base < 0x80000) {
1851 h->index = TCG_REG_NONE;
1852 h->disp = guest_base;
1854 h->index = TCG_GUEST_BASE_REG;
1862 static void tcg_out_qemu_ld(TCGContext* s, TCGReg data_reg, TCGReg addr_reg,
1863 MemOpIdx oi, TCGType data_type)
1865 TCGLabelQemuLdst *ldst;
1868 ldst = prepare_host_addr(s, &h, addr_reg, oi, true);
1869 tcg_out_qemu_ld_direct(s, get_memop(oi), data_reg, h);
1872 ldst->type = data_type;
1873 ldst->datalo_reg = data_reg;
1874 ldst->raddr = tcg_splitwx_to_rx(s->code_ptr);
1878 static void tcg_out_qemu_st(TCGContext* s, TCGReg data_reg, TCGReg addr_reg,
1879 MemOpIdx oi, TCGType data_type)
1881 TCGLabelQemuLdst *ldst;
1884 ldst = prepare_host_addr(s, &h, addr_reg, oi, false);
1885 tcg_out_qemu_st_direct(s, get_memop(oi), data_reg, h);
1888 ldst->type = data_type;
1889 ldst->datalo_reg = data_reg;
1890 ldst->raddr = tcg_splitwx_to_rx(s->code_ptr);
1894 static void tcg_out_qemu_ldst_i128(TCGContext *s, TCGReg datalo, TCGReg datahi,
1895 TCGReg addr_reg, MemOpIdx oi, bool is_ld)
1897 TCGLabel *l1 = NULL, *l2 = NULL;
1898 TCGLabelQemuLdst *ldst;
1904 ldst = prepare_host_addr(s, &h, addr_reg, oi, is_ld);
1906 use_pair = h.aa.atom < MO_128;
1907 need_bswap = get_memop(oi) & MO_BSWAP;
1911 * Atomicity requires we use LPQ. If we've already checked for
1912 * 16-byte alignment, that's all we need. If we arrive with
1913 * lesser alignment, we have determined that less than 16-byte
1914 * alignment can be satisfied with two 8-byte loads.
1916 if (h.aa.align < MO_128) {
1918 l1 = gen_new_label();
1919 l2 = gen_new_label();
1921 tcg_out_insn(s, RI, TMLL, addr_reg, 15);
1922 tgen_branch(s, 7, l1); /* CC in {1,2,3} */
1925 tcg_debug_assert(!need_bswap);
1926 tcg_debug_assert(datalo & 1);
1927 tcg_debug_assert(datahi == datalo - 1);
1928 insn = is_ld ? RXY_LPQ : RXY_STPQ;
1929 tcg_out_insn_RXY(s, insn, datahi, h.base, h.index, h.disp);
1932 tgen_branch(s, S390_CC_ALWAYS, l2);
1933 tcg_out_label(s, l1);
1940 d1 = datalo, d2 = datahi;
1941 insn = is_ld ? RXY_LRVG : RXY_STRVG;
1943 d1 = datahi, d2 = datalo;
1944 insn = is_ld ? RXY_LG : RXY_STG;
1947 if (h.base == d1 || h.index == d1) {
1948 tcg_out_insn(s, RXY, LAY, TCG_TMP0, h.base, h.index, h.disp);
1950 h.index = TCG_REG_NONE;
1953 tcg_out_insn_RXY(s, insn, d1, h.base, h.index, h.disp);
1954 tcg_out_insn_RXY(s, insn, d2, h.base, h.index, h.disp + 8);
1957 tcg_out_label(s, l2);
1961 ldst->type = TCG_TYPE_I128;
1962 ldst->datalo_reg = datalo;
1963 ldst->datahi_reg = datahi;
1964 ldst->raddr = tcg_splitwx_to_rx(s->code_ptr);
1968 static void tcg_out_exit_tb(TCGContext *s, uintptr_t a0)
1970 /* Reuse the zeroing that exists for goto_ptr. */
1972 tgen_gotoi(s, S390_CC_ALWAYS, tcg_code_gen_epilogue);
1974 tcg_out_movi(s, TCG_TYPE_PTR, TCG_REG_R2, a0);
1975 tgen_gotoi(s, S390_CC_ALWAYS, tb_ret_addr);
1979 static void tcg_out_goto_tb(TCGContext *s, int which)
1982 * Branch displacement must be aligned for atomic patching;
1983 * see if we need to add extra nop before branch
1985 if (!QEMU_PTR_IS_ALIGNED(s->code_ptr + 1, 4)) {
1988 tcg_out16(s, RIL_BRCL | (S390_CC_ALWAYS << 4));
1989 set_jmp_insn_offset(s, which);
1991 set_jmp_reset_offset(s, which);
1994 void tb_target_set_jmp_target(const TranslationBlock *tb, int n,
1995 uintptr_t jmp_rx, uintptr_t jmp_rw)
1997 if (!HAVE_FACILITY(GEN_INST_EXT)) {
2000 /* patch the branch destination */
2001 uintptr_t addr = tb->jmp_target_addr[n];
2002 intptr_t disp = addr - (jmp_rx - 2);
2003 qatomic_set((int32_t *)jmp_rw, disp / 2);
2004 /* no need to flush icache explicitly */
2007 # define OP_32_64(x) \
2008 case glue(glue(INDEX_op_,x),_i32): \
2009 case glue(glue(INDEX_op_,x),_i64)
2011 static inline void tcg_out_op(TCGContext *s, TCGOpcode opc,
2012 const TCGArg args[TCG_MAX_OP_ARGS],
2013 const int const_args[TCG_MAX_OP_ARGS])
2019 case INDEX_op_goto_ptr:
2021 tcg_out_insn(s, RR, BCR, S390_CC_ALWAYS, a0);
2025 /* ??? LLC (RXY format) is only present with the extended-immediate
2026 facility, whereas LLGC is always present. */
2027 tcg_out_mem(s, 0, RXY_LLGC, args[0], args[1], TCG_REG_NONE, args[2]);
2031 /* ??? LB is no smaller than LGB, so no point to using it. */
2032 tcg_out_mem(s, 0, RXY_LGB, args[0], args[1], TCG_REG_NONE, args[2]);
2036 /* ??? LLH (RXY format) is only present with the extended-immediate
2037 facility, whereas LLGH is always present. */
2038 tcg_out_mem(s, 0, RXY_LLGH, args[0], args[1], TCG_REG_NONE, args[2]);
2041 case INDEX_op_ld16s_i32:
2042 tcg_out_mem(s, RX_LH, RXY_LHY, args[0], args[1], TCG_REG_NONE, args[2]);
2045 case INDEX_op_ld_i32:
2046 tcg_out_ld(s, TCG_TYPE_I32, args[0], args[1], args[2]);
2050 tcg_out_mem(s, RX_STC, RXY_STCY, args[0], args[1],
2051 TCG_REG_NONE, args[2]);
2055 tcg_out_mem(s, RX_STH, RXY_STHY, args[0], args[1],
2056 TCG_REG_NONE, args[2]);
2059 case INDEX_op_st_i32:
2060 tcg_out_st(s, TCG_TYPE_I32, args[0], args[1], args[2]);
2063 case INDEX_op_add_i32:
2064 a0 = args[0], a1 = args[1], a2 = (int32_t)args[2];
2065 if (const_args[2]) {
2068 if (a2 == (int16_t)a2) {
2069 tcg_out_insn(s, RI, AHI, a0, a2);
2072 tcg_out_insn(s, RIL, AFI, a0, a2);
2075 tcg_out_mem(s, RX_LA, RXY_LAY, a0, a1, TCG_REG_NONE, a2);
2076 } else if (a0 == a1) {
2077 tcg_out_insn(s, RR, AR, a0, a2);
2079 tcg_out_insn(s, RX, LA, a0, a1, a2, 0);
2082 case INDEX_op_sub_i32:
2083 a0 = args[0], a1 = args[1], a2 = (int32_t)args[2];
2084 if (const_args[2]) {
2087 } else if (a0 == a1) {
2088 tcg_out_insn(s, RR, SR, a0, a2);
2090 tcg_out_insn(s, RRFa, SRK, a0, a1, a2);
2094 case INDEX_op_and_i32:
2095 a0 = args[0], a1 = args[1], a2 = (uint32_t)args[2];
2096 if (const_args[2]) {
2097 tcg_out_mov(s, TCG_TYPE_I32, a0, a1);
2098 tgen_andi(s, TCG_TYPE_I32, a0, a2);
2099 } else if (a0 == a1) {
2100 tcg_out_insn(s, RR, NR, a0, a2);
2102 tcg_out_insn(s, RRFa, NRK, a0, a1, a2);
2105 case INDEX_op_or_i32:
2106 a0 = args[0], a1 = args[1], a2 = (uint32_t)args[2];
2107 if (const_args[2]) {
2108 tcg_out_mov(s, TCG_TYPE_I32, a0, a1);
2109 tgen_ori(s, a0, a2);
2110 } else if (a0 == a1) {
2111 tcg_out_insn(s, RR, OR, a0, a2);
2113 tcg_out_insn(s, RRFa, ORK, a0, a1, a2);
2116 case INDEX_op_xor_i32:
2117 a0 = args[0], a1 = args[1], a2 = (uint32_t)args[2];
2118 if (const_args[2]) {
2119 tcg_out_mov(s, TCG_TYPE_I32, a0, a1);
2120 tcg_out_insn(s, RIL, XILF, a0, a2);
2121 } else if (a0 == a1) {
2122 tcg_out_insn(s, RR, XR, args[0], args[2]);
2124 tcg_out_insn(s, RRFa, XRK, a0, a1, a2);
2128 case INDEX_op_andc_i32:
2129 a0 = args[0], a1 = args[1], a2 = (uint32_t)args[2];
2130 if (const_args[2]) {
2131 tcg_out_mov(s, TCG_TYPE_I32, a0, a1);
2132 tgen_andi(s, TCG_TYPE_I32, a0, (uint32_t)~a2);
2134 tcg_out_insn(s, RRFa, NCRK, a0, a1, a2);
2137 case INDEX_op_orc_i32:
2138 a0 = args[0], a1 = args[1], a2 = (uint32_t)args[2];
2139 if (const_args[2]) {
2140 tcg_out_mov(s, TCG_TYPE_I32, a0, a1);
2141 tgen_ori(s, a0, (uint32_t)~a2);
2143 tcg_out_insn(s, RRFa, OCRK, a0, a1, a2);
2146 case INDEX_op_eqv_i32:
2147 a0 = args[0], a1 = args[1], a2 = (uint32_t)args[2];
2148 if (const_args[2]) {
2149 tcg_out_mov(s, TCG_TYPE_I32, a0, a1);
2150 tcg_out_insn(s, RIL, XILF, a0, ~a2);
2152 tcg_out_insn(s, RRFa, NXRK, a0, a1, a2);
2155 case INDEX_op_nand_i32:
2156 tcg_out_insn(s, RRFa, NNRK, args[0], args[1], args[2]);
2158 case INDEX_op_nor_i32:
2159 tcg_out_insn(s, RRFa, NORK, args[0], args[1], args[2]);
2162 case INDEX_op_neg_i32:
2163 tcg_out_insn(s, RR, LCR, args[0], args[1]);
2165 case INDEX_op_not_i32:
2166 tcg_out_insn(s, RRFa, NORK, args[0], args[1], args[1]);
2169 case INDEX_op_mul_i32:
2170 a0 = args[0], a1 = args[1], a2 = (int32_t)args[2];
2171 if (const_args[2]) {
2172 tcg_out_mov(s, TCG_TYPE_I32, a0, a1);
2173 if (a2 == (int16_t)a2) {
2174 tcg_out_insn(s, RI, MHI, a0, a2);
2176 tcg_out_insn(s, RIL, MSFI, a0, a2);
2178 } else if (a0 == a1) {
2179 tcg_out_insn(s, RRE, MSR, a0, a2);
2181 tcg_out_insn(s, RRFa, MSRKC, a0, a1, a2);
2185 case INDEX_op_div2_i32:
2186 tcg_debug_assert(args[0] == args[2]);
2187 tcg_debug_assert(args[1] == args[3]);
2188 tcg_debug_assert((args[1] & 1) == 0);
2189 tcg_debug_assert(args[0] == args[1] + 1);
2190 tcg_out_insn(s, RR, DR, args[1], args[4]);
2192 case INDEX_op_divu2_i32:
2193 tcg_debug_assert(args[0] == args[2]);
2194 tcg_debug_assert(args[1] == args[3]);
2195 tcg_debug_assert((args[1] & 1) == 0);
2196 tcg_debug_assert(args[0] == args[1] + 1);
2197 tcg_out_insn(s, RRE, DLR, args[1], args[4]);
2200 case INDEX_op_shl_i32:
2204 a0 = args[0], a1 = args[1], a2 = (int32_t)args[2];
2206 if (const_args[2]) {
2207 tcg_out_sh32(s, op, a0, TCG_REG_NONE, a2);
2209 tcg_out_sh32(s, op, a0, a2, 0);
2212 /* Using tcg_out_sh64 here for the format; it is a 32-bit shift. */
2213 if (const_args[2]) {
2214 tcg_out_sh64(s, op2, a0, a1, TCG_REG_NONE, a2);
2216 tcg_out_sh64(s, op2, a0, a1, a2, 0);
2220 case INDEX_op_shr_i32:
2224 case INDEX_op_sar_i32:
2229 case INDEX_op_rotl_i32:
2230 /* ??? Using tcg_out_sh64 here for the format; it is a 32-bit rol. */
2231 if (const_args[2]) {
2232 tcg_out_sh64(s, RSY_RLL, args[0], args[1], TCG_REG_NONE, args[2]);
2234 tcg_out_sh64(s, RSY_RLL, args[0], args[1], args[2], 0);
2237 case INDEX_op_rotr_i32:
2238 if (const_args[2]) {
2239 tcg_out_sh64(s, RSY_RLL, args[0], args[1],
2240 TCG_REG_NONE, (32 - args[2]) & 31);
2242 tcg_out_insn(s, RR, LCR, TCG_TMP0, args[2]);
2243 tcg_out_sh64(s, RSY_RLL, args[0], args[1], TCG_TMP0, 0);
2247 case INDEX_op_bswap16_i32:
2248 a0 = args[0], a1 = args[1], a2 = args[2];
2249 tcg_out_insn(s, RRE, LRVR, a0, a1);
2250 if (a2 & TCG_BSWAP_OS) {
2251 tcg_out_sh32(s, RS_SRA, a0, TCG_REG_NONE, 16);
2253 tcg_out_sh32(s, RS_SRL, a0, TCG_REG_NONE, 16);
2256 case INDEX_op_bswap16_i64:
2257 a0 = args[0], a1 = args[1], a2 = args[2];
2258 tcg_out_insn(s, RRE, LRVGR, a0, a1);
2259 if (a2 & TCG_BSWAP_OS) {
2260 tcg_out_sh64(s, RSY_SRAG, a0, a0, TCG_REG_NONE, 48);
2262 tcg_out_sh64(s, RSY_SRLG, a0, a0, TCG_REG_NONE, 48);
2266 case INDEX_op_bswap32_i32:
2267 tcg_out_insn(s, RRE, LRVR, args[0], args[1]);
2269 case INDEX_op_bswap32_i64:
2270 a0 = args[0], a1 = args[1], a2 = args[2];
2271 tcg_out_insn(s, RRE, LRVR, a0, a1);
2272 if (a2 & TCG_BSWAP_OS) {
2273 tcg_out_ext32s(s, a0, a0);
2274 } else if ((a2 & (TCG_BSWAP_IZ | TCG_BSWAP_OZ)) == TCG_BSWAP_OZ) {
2275 tcg_out_ext32u(s, a0, a0);
2279 case INDEX_op_add2_i32:
2280 if (const_args[4]) {
2281 tcg_out_insn(s, RIL, ALFI, args[0], args[4]);
2283 tcg_out_insn(s, RR, ALR, args[0], args[4]);
2285 tcg_out_insn(s, RRE, ALCR, args[1], args[5]);
2287 case INDEX_op_sub2_i32:
2288 if (const_args[4]) {
2289 tcg_out_insn(s, RIL, SLFI, args[0], args[4]);
2291 tcg_out_insn(s, RR, SLR, args[0], args[4]);
2293 tcg_out_insn(s, RRE, SLBR, args[1], args[5]);
2297 tgen_branch(s, S390_CC_ALWAYS, arg_label(args[0]));
2300 case INDEX_op_brcond_i32:
2301 tgen_brcond(s, TCG_TYPE_I32, args[2], args[0],
2302 args[1], const_args[1], arg_label(args[3]));
2304 case INDEX_op_setcond_i32:
2305 tgen_setcond(s, TCG_TYPE_I32, args[3], args[0], args[1],
2306 args[2], const_args[2], false);
2308 case INDEX_op_negsetcond_i32:
2309 tgen_setcond(s, TCG_TYPE_I32, args[3], args[0], args[1],
2310 args[2], const_args[2], true);
2312 case INDEX_op_movcond_i32:
2313 tgen_movcond(s, TCG_TYPE_I32, args[5], args[0], args[1],
2314 args[2], const_args[2], args[3], const_args[3], args[4]);
2317 case INDEX_op_qemu_ld_a32_i32:
2318 case INDEX_op_qemu_ld_a64_i32:
2319 tcg_out_qemu_ld(s, args[0], args[1], args[2], TCG_TYPE_I32);
2321 case INDEX_op_qemu_ld_a32_i64:
2322 case INDEX_op_qemu_ld_a64_i64:
2323 tcg_out_qemu_ld(s, args[0], args[1], args[2], TCG_TYPE_I64);
2325 case INDEX_op_qemu_st_a32_i32:
2326 case INDEX_op_qemu_st_a64_i32:
2327 tcg_out_qemu_st(s, args[0], args[1], args[2], TCG_TYPE_I32);
2329 case INDEX_op_qemu_st_a32_i64:
2330 case INDEX_op_qemu_st_a64_i64:
2331 tcg_out_qemu_st(s, args[0], args[1], args[2], TCG_TYPE_I64);
2333 case INDEX_op_qemu_ld_a32_i128:
2334 case INDEX_op_qemu_ld_a64_i128:
2335 tcg_out_qemu_ldst_i128(s, args[0], args[1], args[2], args[3], true);
2337 case INDEX_op_qemu_st_a32_i128:
2338 case INDEX_op_qemu_st_a64_i128:
2339 tcg_out_qemu_ldst_i128(s, args[0], args[1], args[2], args[3], false);
2342 case INDEX_op_ld16s_i64:
2343 tcg_out_mem(s, 0, RXY_LGH, args[0], args[1], TCG_REG_NONE, args[2]);
2345 case INDEX_op_ld32u_i64:
2346 tcg_out_mem(s, 0, RXY_LLGF, args[0], args[1], TCG_REG_NONE, args[2]);
2348 case INDEX_op_ld32s_i64:
2349 tcg_out_mem(s, 0, RXY_LGF, args[0], args[1], TCG_REG_NONE, args[2]);
2351 case INDEX_op_ld_i64:
2352 tcg_out_ld(s, TCG_TYPE_I64, args[0], args[1], args[2]);
2355 case INDEX_op_st32_i64:
2356 tcg_out_st(s, TCG_TYPE_I32, args[0], args[1], args[2]);
2358 case INDEX_op_st_i64:
2359 tcg_out_st(s, TCG_TYPE_I64, args[0], args[1], args[2]);
2362 case INDEX_op_add_i64:
2363 a0 = args[0], a1 = args[1], a2 = args[2];
2364 if (const_args[2]) {
2367 if (a2 == (int16_t)a2) {
2368 tcg_out_insn(s, RI, AGHI, a0, a2);
2371 if (a2 == (int32_t)a2) {
2372 tcg_out_insn(s, RIL, AGFI, a0, a2);
2375 if (a2 == (uint32_t)a2) {
2376 tcg_out_insn(s, RIL, ALGFI, a0, a2);
2379 if (-a2 == (uint32_t)-a2) {
2380 tcg_out_insn(s, RIL, SLGFI, a0, -a2);
2384 tcg_out_mem(s, RX_LA, RXY_LAY, a0, a1, TCG_REG_NONE, a2);
2385 } else if (a0 == a1) {
2386 tcg_out_insn(s, RRE, AGR, a0, a2);
2388 tcg_out_insn(s, RX, LA, a0, a1, a2, 0);
2391 case INDEX_op_sub_i64:
2392 a0 = args[0], a1 = args[1], a2 = args[2];
2393 if (const_args[2]) {
2397 tcg_out_insn(s, RRFa, SGRK, a0, a1, a2);
2401 case INDEX_op_and_i64:
2402 a0 = args[0], a1 = args[1], a2 = args[2];
2403 if (const_args[2]) {
2404 tcg_out_mov(s, TCG_TYPE_I64, a0, a1);
2405 tgen_andi(s, TCG_TYPE_I64, args[0], args[2]);
2407 tcg_out_insn(s, RRFa, NGRK, a0, a1, a2);
2410 case INDEX_op_or_i64:
2411 a0 = args[0], a1 = args[1], a2 = args[2];
2412 if (const_args[2]) {
2413 tcg_out_mov(s, TCG_TYPE_I64, a0, a1);
2414 tgen_ori(s, a0, a2);
2416 tcg_out_insn(s, RRFa, OGRK, a0, a1, a2);
2419 case INDEX_op_xor_i64:
2420 a0 = args[0], a1 = args[1], a2 = args[2];
2421 if (const_args[2]) {
2422 tcg_out_mov(s, TCG_TYPE_I64, a0, a1);
2423 tgen_xori(s, a0, a2);
2425 tcg_out_insn(s, RRFa, XGRK, a0, a1, a2);
2429 case INDEX_op_andc_i64:
2430 a0 = args[0], a1 = args[1], a2 = args[2];
2431 if (const_args[2]) {
2432 tcg_out_mov(s, TCG_TYPE_I64, a0, a1);
2433 tgen_andi(s, TCG_TYPE_I64, a0, ~a2);
2435 tcg_out_insn(s, RRFa, NCGRK, a0, a1, a2);
2438 case INDEX_op_orc_i64:
2439 a0 = args[0], a1 = args[1], a2 = args[2];
2440 if (const_args[2]) {
2441 tcg_out_mov(s, TCG_TYPE_I64, a0, a1);
2442 tgen_ori(s, a0, ~a2);
2444 tcg_out_insn(s, RRFa, OCGRK, a0, a1, a2);
2447 case INDEX_op_eqv_i64:
2448 a0 = args[0], a1 = args[1], a2 = args[2];
2449 if (const_args[2]) {
2450 tcg_out_mov(s, TCG_TYPE_I64, a0, a1);
2451 tgen_xori(s, a0, ~a2);
2453 tcg_out_insn(s, RRFa, NXGRK, a0, a1, a2);
2456 case INDEX_op_nand_i64:
2457 tcg_out_insn(s, RRFa, NNGRK, args[0], args[1], args[2]);
2459 case INDEX_op_nor_i64:
2460 tcg_out_insn(s, RRFa, NOGRK, args[0], args[1], args[2]);
2463 case INDEX_op_neg_i64:
2464 tcg_out_insn(s, RRE, LCGR, args[0], args[1]);
2466 case INDEX_op_not_i64:
2467 tcg_out_insn(s, RRFa, NOGRK, args[0], args[1], args[1]);
2469 case INDEX_op_bswap64_i64:
2470 tcg_out_insn(s, RRE, LRVGR, args[0], args[1]);
2473 case INDEX_op_mul_i64:
2474 a0 = args[0], a1 = args[1], a2 = args[2];
2475 if (const_args[2]) {
2476 tcg_out_mov(s, TCG_TYPE_I64, a0, a1);
2477 if (a2 == (int16_t)a2) {
2478 tcg_out_insn(s, RI, MGHI, a0, a2);
2480 tcg_out_insn(s, RIL, MSGFI, a0, a2);
2482 } else if (a0 == a1) {
2483 tcg_out_insn(s, RRE, MSGR, a0, a2);
2485 tcg_out_insn(s, RRFa, MSGRKC, a0, a1, a2);
2489 case INDEX_op_div2_i64:
2491 * ??? We get an unnecessary sign-extension of the dividend
2492 * into op0 with this definition, but as we do in fact always
2493 * produce both quotient and remainder using INDEX_op_div_i64
2494 * instead requires jumping through even more hoops.
2496 tcg_debug_assert(args[0] == args[2]);
2497 tcg_debug_assert(args[1] == args[3]);
2498 tcg_debug_assert((args[1] & 1) == 0);
2499 tcg_debug_assert(args[0] == args[1] + 1);
2500 tcg_out_insn(s, RRE, DSGR, args[1], args[4]);
2502 case INDEX_op_divu2_i64:
2503 tcg_debug_assert(args[0] == args[2]);
2504 tcg_debug_assert(args[1] == args[3]);
2505 tcg_debug_assert((args[1] & 1) == 0);
2506 tcg_debug_assert(args[0] == args[1] + 1);
2507 tcg_out_insn(s, RRE, DLGR, args[1], args[4]);
2509 case INDEX_op_mulu2_i64:
2510 tcg_debug_assert(args[0] == args[2]);
2511 tcg_debug_assert((args[1] & 1) == 0);
2512 tcg_debug_assert(args[0] == args[1] + 1);
2513 tcg_out_insn(s, RRE, MLGR, args[1], args[3]);
2515 case INDEX_op_muls2_i64:
2516 tcg_debug_assert((args[1] & 1) == 0);
2517 tcg_debug_assert(args[0] == args[1] + 1);
2518 tcg_out_insn(s, RRFa, MGRK, args[1], args[2], args[3]);
2521 case INDEX_op_shl_i64:
2524 if (const_args[2]) {
2525 tcg_out_sh64(s, op, args[0], args[1], TCG_REG_NONE, args[2]);
2527 tcg_out_sh64(s, op, args[0], args[1], args[2], 0);
2530 case INDEX_op_shr_i64:
2533 case INDEX_op_sar_i64:
2537 case INDEX_op_rotl_i64:
2538 if (const_args[2]) {
2539 tcg_out_sh64(s, RSY_RLLG, args[0], args[1],
2540 TCG_REG_NONE, args[2]);
2542 tcg_out_sh64(s, RSY_RLLG, args[0], args[1], args[2], 0);
2545 case INDEX_op_rotr_i64:
2546 if (const_args[2]) {
2547 tcg_out_sh64(s, RSY_RLLG, args[0], args[1],
2548 TCG_REG_NONE, (64 - args[2]) & 63);
2550 /* We can use the smaller 32-bit negate because only the
2551 low 6 bits are examined for the rotate. */
2552 tcg_out_insn(s, RR, LCR, TCG_TMP0, args[2]);
2553 tcg_out_sh64(s, RSY_RLLG, args[0], args[1], TCG_TMP0, 0);
2557 case INDEX_op_add2_i64:
2558 if (const_args[4]) {
2559 if ((int64_t)args[4] >= 0) {
2560 tcg_out_insn(s, RIL, ALGFI, args[0], args[4]);
2562 tcg_out_insn(s, RIL, SLGFI, args[0], -args[4]);
2565 tcg_out_insn(s, RRE, ALGR, args[0], args[4]);
2567 tcg_out_insn(s, RRE, ALCGR, args[1], args[5]);
2569 case INDEX_op_sub2_i64:
2570 if (const_args[4]) {
2571 if ((int64_t)args[4] >= 0) {
2572 tcg_out_insn(s, RIL, SLGFI, args[0], args[4]);
2574 tcg_out_insn(s, RIL, ALGFI, args[0], -args[4]);
2577 tcg_out_insn(s, RRE, SLGR, args[0], args[4]);
2579 tcg_out_insn(s, RRE, SLBGR, args[1], args[5]);
2582 case INDEX_op_brcond_i64:
2583 tgen_brcond(s, TCG_TYPE_I64, args[2], args[0],
2584 args[1], const_args[1], arg_label(args[3]));
2586 case INDEX_op_setcond_i64:
2587 tgen_setcond(s, TCG_TYPE_I64, args[3], args[0], args[1],
2588 args[2], const_args[2], false);
2590 case INDEX_op_negsetcond_i64:
2591 tgen_setcond(s, TCG_TYPE_I64, args[3], args[0], args[1],
2592 args[2], const_args[2], true);
2594 case INDEX_op_movcond_i64:
2595 tgen_movcond(s, TCG_TYPE_I64, args[5], args[0], args[1],
2596 args[2], const_args[2], args[3], const_args[3], args[4]);
2600 a0 = args[0], a1 = args[1], a2 = args[2];
2601 if (const_args[1]) {
2602 tgen_deposit(s, a0, a2, args[3], args[4], 1);
2604 /* Since we can't support "0Z" as a constraint, we allow a1 in
2605 any register. Fix things up as if a matching constraint. */
2607 TCGType type = (opc == INDEX_op_deposit_i64);
2609 tcg_out_mov(s, type, TCG_TMP0, a2);
2612 tcg_out_mov(s, type, a0, a1);
2614 tgen_deposit(s, a0, a2, args[3], args[4], 0);
2619 tgen_extract(s, args[0], args[1], args[2], args[3]);
2622 case INDEX_op_clz_i64:
2623 tgen_clz(s, args[0], args[1], args[2], const_args[2]);
2626 case INDEX_op_ctpop_i32:
2627 tgen_ctpop(s, TCG_TYPE_I32, args[0], args[1]);
2629 case INDEX_op_ctpop_i64:
2630 tgen_ctpop(s, TCG_TYPE_I64, args[0], args[1]);
2634 /* The host memory model is quite strong, we simply need to
2635 serialize the instruction stream. */
2636 if (args[0] & TCG_MO_ST_LD) {
2637 /* fast-bcr-serialization facility (45) is present */
2638 tcg_out_insn(s, RR, BCR, 14, 0);
2642 case INDEX_op_mov_i32: /* Always emitted via tcg_out_mov. */
2643 case INDEX_op_mov_i64:
2644 case INDEX_op_call: /* Always emitted via tcg_out_call. */
2645 case INDEX_op_exit_tb: /* Always emitted via tcg_out_exit_tb. */
2646 case INDEX_op_goto_tb: /* Always emitted via tcg_out_goto_tb. */
2647 case INDEX_op_ext8s_i32: /* Always emitted via tcg_reg_alloc_op. */
2648 case INDEX_op_ext8s_i64:
2649 case INDEX_op_ext8u_i32:
2650 case INDEX_op_ext8u_i64:
2651 case INDEX_op_ext16s_i32:
2652 case INDEX_op_ext16s_i64:
2653 case INDEX_op_ext16u_i32:
2654 case INDEX_op_ext16u_i64:
2655 case INDEX_op_ext32s_i64:
2656 case INDEX_op_ext32u_i64:
2657 case INDEX_op_ext_i32_i64:
2658 case INDEX_op_extu_i32_i64:
2659 case INDEX_op_extrl_i64_i32:
2661 g_assert_not_reached();
2665 static bool tcg_out_dup_vec(TCGContext *s, TCGType type, unsigned vece,
2666 TCGReg dst, TCGReg src)
2668 if (is_general_reg(src)) {
2669 /* Replicate general register into two MO_64. */
2670 tcg_out_insn(s, VRRf, VLVGP, dst, src, src);
2671 if (vece == MO_64) {
2678 * Recall that the "standard" integer, within a vector, is the
2679 * rightmost element of the leftmost doubleword, a-la VLLEZ.
2681 tcg_out_insn(s, VRIc, VREP, dst, (8 >> vece) - 1, src, vece);
2685 static bool tcg_out_dupm_vec(TCGContext *s, TCGType type, unsigned vece,
2686 TCGReg dst, TCGReg base, intptr_t offset)
2688 tcg_out_vrx_mem(s, VRX_VLREP, dst, base, TCG_REG_NONE, offset, vece);
2692 static void tcg_out_dupi_vec(TCGContext *s, TCGType type, unsigned vece,
2693 TCGReg dst, int64_t val)
2695 int i, mask, msb, lsb;
2697 /* Look for int16_t elements. */
2698 if (vece <= MO_16 ||
2699 (vece == MO_32 ? (int32_t)val : val) == (int16_t)val) {
2700 tcg_out_insn(s, VRIa, VREPI, dst, val, vece);
2704 /* Look for bit masks. */
2705 if (vece == MO_32) {
2706 if (risbg_mask((int32_t)val)) {
2707 /* Handle wraparound by swapping msb and lsb. */
2708 if ((val & 0x80000001u) == 0x80000001u) {
2709 msb = 32 - ctz32(~val);
2710 lsb = clz32(~val) - 1;
2713 lsb = 31 - ctz32(val);
2715 tcg_out_insn(s, VRIb, VGM, dst, msb, lsb, MO_32);
2719 if (risbg_mask(val)) {
2720 /* Handle wraparound by swapping msb and lsb. */
2721 if ((val & 0x8000000000000001ull) == 0x8000000000000001ull) {
2722 /* Handle wraparound by swapping msb and lsb. */
2723 msb = 64 - ctz64(~val);
2724 lsb = clz64(~val) - 1;
2727 lsb = 63 - ctz64(val);
2729 tcg_out_insn(s, VRIb, VGM, dst, msb, lsb, MO_64);
2734 /* Look for all bytes 0x00 or 0xff. */
2735 for (i = mask = 0; i < 8; i++) {
2736 uint8_t byte = val >> (i * 8);
2739 } else if (byte != 0) {
2744 tcg_out_insn(s, VRIa, VGBM, dst, mask * 0x0101, 0);
2748 /* Otherwise, stuff it in the constant pool. */
2749 tcg_out_insn(s, RIL, LARL, TCG_TMP0, 0);
2750 new_pool_label(s, val, R_390_PC32DBL, s->code_ptr - 2, 2);
2751 tcg_out_insn(s, VRX, VLREP, dst, TCG_TMP0, TCG_REG_NONE, 0, MO_64);
2754 static void tcg_out_vec_op(TCGContext *s, TCGOpcode opc,
2755 unsigned vecl, unsigned vece,
2756 const TCGArg args[TCG_MAX_OP_ARGS],
2757 const int const_args[TCG_MAX_OP_ARGS])
2759 TCGType type = vecl + TCG_TYPE_V64;
2760 TCGArg a0 = args[0], a1 = args[1], a2 = args[2];
2763 case INDEX_op_ld_vec:
2764 tcg_out_ld(s, type, a0, a1, a2);
2766 case INDEX_op_st_vec:
2767 tcg_out_st(s, type, a0, a1, a2);
2769 case INDEX_op_dupm_vec:
2770 tcg_out_dupm_vec(s, type, vece, a0, a1, a2);
2773 case INDEX_op_abs_vec:
2774 tcg_out_insn(s, VRRa, VLP, a0, a1, vece);
2776 case INDEX_op_neg_vec:
2777 tcg_out_insn(s, VRRa, VLC, a0, a1, vece);
2779 case INDEX_op_not_vec:
2780 tcg_out_insn(s, VRRc, VNO, a0, a1, a1, 0);
2783 case INDEX_op_add_vec:
2784 tcg_out_insn(s, VRRc, VA, a0, a1, a2, vece);
2786 case INDEX_op_sub_vec:
2787 tcg_out_insn(s, VRRc, VS, a0, a1, a2, vece);
2789 case INDEX_op_and_vec:
2790 tcg_out_insn(s, VRRc, VN, a0, a1, a2, 0);
2792 case INDEX_op_andc_vec:
2793 tcg_out_insn(s, VRRc, VNC, a0, a1, a2, 0);
2795 case INDEX_op_mul_vec:
2796 tcg_out_insn(s, VRRc, VML, a0, a1, a2, vece);
2798 case INDEX_op_or_vec:
2799 tcg_out_insn(s, VRRc, VO, a0, a1, a2, 0);
2801 case INDEX_op_orc_vec:
2802 tcg_out_insn(s, VRRc, VOC, a0, a1, a2, 0);
2804 case INDEX_op_xor_vec:
2805 tcg_out_insn(s, VRRc, VX, a0, a1, a2, 0);
2807 case INDEX_op_nand_vec:
2808 tcg_out_insn(s, VRRc, VNN, a0, a1, a2, 0);
2810 case INDEX_op_nor_vec:
2811 tcg_out_insn(s, VRRc, VNO, a0, a1, a2, 0);
2813 case INDEX_op_eqv_vec:
2814 tcg_out_insn(s, VRRc, VNX, a0, a1, a2, 0);
2817 case INDEX_op_shli_vec:
2818 tcg_out_insn(s, VRSa, VESL, a0, a2, TCG_REG_NONE, a1, vece);
2820 case INDEX_op_shri_vec:
2821 tcg_out_insn(s, VRSa, VESRL, a0, a2, TCG_REG_NONE, a1, vece);
2823 case INDEX_op_sari_vec:
2824 tcg_out_insn(s, VRSa, VESRA, a0, a2, TCG_REG_NONE, a1, vece);
2826 case INDEX_op_rotli_vec:
2827 tcg_out_insn(s, VRSa, VERLL, a0, a2, TCG_REG_NONE, a1, vece);
2829 case INDEX_op_shls_vec:
2830 tcg_out_insn(s, VRSa, VESL, a0, 0, a2, a1, vece);
2832 case INDEX_op_shrs_vec:
2833 tcg_out_insn(s, VRSa, VESRL, a0, 0, a2, a1, vece);
2835 case INDEX_op_sars_vec:
2836 tcg_out_insn(s, VRSa, VESRA, a0, 0, a2, a1, vece);
2838 case INDEX_op_rotls_vec:
2839 tcg_out_insn(s, VRSa, VERLL, a0, 0, a2, a1, vece);
2841 case INDEX_op_shlv_vec:
2842 tcg_out_insn(s, VRRc, VESLV, a0, a1, a2, vece);
2844 case INDEX_op_shrv_vec:
2845 tcg_out_insn(s, VRRc, VESRLV, a0, a1, a2, vece);
2847 case INDEX_op_sarv_vec:
2848 tcg_out_insn(s, VRRc, VESRAV, a0, a1, a2, vece);
2850 case INDEX_op_rotlv_vec:
2851 tcg_out_insn(s, VRRc, VERLLV, a0, a1, a2, vece);
2854 case INDEX_op_smin_vec:
2855 tcg_out_insn(s, VRRc, VMN, a0, a1, a2, vece);
2857 case INDEX_op_smax_vec:
2858 tcg_out_insn(s, VRRc, VMX, a0, a1, a2, vece);
2860 case INDEX_op_umin_vec:
2861 tcg_out_insn(s, VRRc, VMNL, a0, a1, a2, vece);
2863 case INDEX_op_umax_vec:
2864 tcg_out_insn(s, VRRc, VMXL, a0, a1, a2, vece);
2867 case INDEX_op_bitsel_vec:
2868 tcg_out_insn(s, VRRe, VSEL, a0, a2, args[3], a1);
2871 case INDEX_op_cmp_vec:
2872 switch ((TCGCond)args[3]) {
2874 tcg_out_insn(s, VRRc, VCEQ, a0, a1, a2, vece);
2877 tcg_out_insn(s, VRRc, VCH, a0, a1, a2, vece);
2880 tcg_out_insn(s, VRRc, VCHL, a0, a1, a2, vece);
2883 g_assert_not_reached();
2887 case INDEX_op_s390_vuph_vec:
2888 tcg_out_insn(s, VRRa, VUPH, a0, a1, vece);
2890 case INDEX_op_s390_vupl_vec:
2891 tcg_out_insn(s, VRRa, VUPL, a0, a1, vece);
2893 case INDEX_op_s390_vpks_vec:
2894 tcg_out_insn(s, VRRc, VPKS, a0, a1, a2, vece);
2897 case INDEX_op_mov_vec: /* Always emitted via tcg_out_mov. */
2898 case INDEX_op_dup_vec: /* Always emitted via tcg_out_dup_vec. */
2900 g_assert_not_reached();
2904 int tcg_can_emit_vec_op(TCGOpcode opc, TCGType type, unsigned vece)
2907 case INDEX_op_abs_vec:
2908 case INDEX_op_add_vec:
2909 case INDEX_op_and_vec:
2910 case INDEX_op_andc_vec:
2911 case INDEX_op_bitsel_vec:
2912 case INDEX_op_eqv_vec:
2913 case INDEX_op_nand_vec:
2914 case INDEX_op_neg_vec:
2915 case INDEX_op_nor_vec:
2916 case INDEX_op_not_vec:
2917 case INDEX_op_or_vec:
2918 case INDEX_op_orc_vec:
2919 case INDEX_op_rotli_vec:
2920 case INDEX_op_rotls_vec:
2921 case INDEX_op_rotlv_vec:
2922 case INDEX_op_sari_vec:
2923 case INDEX_op_sars_vec:
2924 case INDEX_op_sarv_vec:
2925 case INDEX_op_shli_vec:
2926 case INDEX_op_shls_vec:
2927 case INDEX_op_shlv_vec:
2928 case INDEX_op_shri_vec:
2929 case INDEX_op_shrs_vec:
2930 case INDEX_op_shrv_vec:
2931 case INDEX_op_smax_vec:
2932 case INDEX_op_smin_vec:
2933 case INDEX_op_sub_vec:
2934 case INDEX_op_umax_vec:
2935 case INDEX_op_umin_vec:
2936 case INDEX_op_xor_vec:
2938 case INDEX_op_cmp_vec:
2939 case INDEX_op_cmpsel_vec:
2940 case INDEX_op_rotrv_vec:
2942 case INDEX_op_mul_vec:
2943 return vece < MO_64;
2944 case INDEX_op_ssadd_vec:
2945 case INDEX_op_sssub_vec:
2946 return vece < MO_64 ? -1 : 0;
2952 static bool expand_vec_cmp_noinv(TCGType type, unsigned vece, TCGv_vec v0,
2953 TCGv_vec v1, TCGv_vec v2, TCGCond cond)
2955 bool need_swap = false, need_inv = false;
2973 need_swap = need_inv = true;
2976 g_assert_not_reached();
2980 cond = tcg_invert_cond(cond);
2984 t1 = v1, v1 = v2, v2 = t1;
2985 cond = tcg_swap_cond(cond);
2988 vec_gen_4(INDEX_op_cmp_vec, type, vece, tcgv_vec_arg(v0),
2989 tcgv_vec_arg(v1), tcgv_vec_arg(v2), cond);
2994 static void expand_vec_cmp(TCGType type, unsigned vece, TCGv_vec v0,
2995 TCGv_vec v1, TCGv_vec v2, TCGCond cond)
2997 if (expand_vec_cmp_noinv(type, vece, v0, v1, v2, cond)) {
2998 tcg_gen_not_vec(vece, v0, v0);
3002 static void expand_vec_cmpsel(TCGType type, unsigned vece, TCGv_vec v0,
3003 TCGv_vec c1, TCGv_vec c2,
3004 TCGv_vec v3, TCGv_vec v4, TCGCond cond)
3006 TCGv_vec t = tcg_temp_new_vec(type);
3008 if (expand_vec_cmp_noinv(type, vece, t, c1, c2, cond)) {
3009 /* Invert the sense of the compare by swapping arguments. */
3010 tcg_gen_bitsel_vec(vece, v0, t, v4, v3);
3012 tcg_gen_bitsel_vec(vece, v0, t, v3, v4);
3014 tcg_temp_free_vec(t);
3017 static void expand_vec_sat(TCGType type, unsigned vece, TCGv_vec v0,
3018 TCGv_vec v1, TCGv_vec v2, TCGOpcode add_sub_opc)
3020 TCGv_vec h1 = tcg_temp_new_vec(type);
3021 TCGv_vec h2 = tcg_temp_new_vec(type);
3022 TCGv_vec l1 = tcg_temp_new_vec(type);
3023 TCGv_vec l2 = tcg_temp_new_vec(type);
3025 tcg_debug_assert (vece < MO_64);
3027 /* Unpack with sign-extension. */
3028 vec_gen_2(INDEX_op_s390_vuph_vec, type, vece,
3029 tcgv_vec_arg(h1), tcgv_vec_arg(v1));
3030 vec_gen_2(INDEX_op_s390_vuph_vec, type, vece,
3031 tcgv_vec_arg(h2), tcgv_vec_arg(v2));
3033 vec_gen_2(INDEX_op_s390_vupl_vec, type, vece,
3034 tcgv_vec_arg(l1), tcgv_vec_arg(v1));
3035 vec_gen_2(INDEX_op_s390_vupl_vec, type, vece,
3036 tcgv_vec_arg(l2), tcgv_vec_arg(v2));
3038 /* Arithmetic on a wider element size. */
3039 vec_gen_3(add_sub_opc, type, vece + 1, tcgv_vec_arg(h1),
3040 tcgv_vec_arg(h1), tcgv_vec_arg(h2));
3041 vec_gen_3(add_sub_opc, type, vece + 1, tcgv_vec_arg(l1),
3042 tcgv_vec_arg(l1), tcgv_vec_arg(l2));
3044 /* Pack with saturation. */
3045 vec_gen_3(INDEX_op_s390_vpks_vec, type, vece + 1,
3046 tcgv_vec_arg(v0), tcgv_vec_arg(h1), tcgv_vec_arg(l1));
3048 tcg_temp_free_vec(h1);
3049 tcg_temp_free_vec(h2);
3050 tcg_temp_free_vec(l1);
3051 tcg_temp_free_vec(l2);
3054 void tcg_expand_vec_op(TCGOpcode opc, TCGType type, unsigned vece,
3058 TCGv_vec v0, v1, v2, v3, v4, t0;
3061 v0 = temp_tcgv_vec(arg_temp(a0));
3062 v1 = temp_tcgv_vec(arg_temp(va_arg(va, TCGArg)));
3063 v2 = temp_tcgv_vec(arg_temp(va_arg(va, TCGArg)));
3066 case INDEX_op_cmp_vec:
3067 expand_vec_cmp(type, vece, v0, v1, v2, va_arg(va, TCGArg));
3070 case INDEX_op_cmpsel_vec:
3071 v3 = temp_tcgv_vec(arg_temp(va_arg(va, TCGArg)));
3072 v4 = temp_tcgv_vec(arg_temp(va_arg(va, TCGArg)));
3073 expand_vec_cmpsel(type, vece, v0, v1, v2, v3, v4, va_arg(va, TCGArg));
3076 case INDEX_op_rotrv_vec:
3077 t0 = tcg_temp_new_vec(type);
3078 tcg_gen_neg_vec(vece, t0, v2);
3079 tcg_gen_rotlv_vec(vece, v0, v1, t0);
3080 tcg_temp_free_vec(t0);
3083 case INDEX_op_ssadd_vec:
3084 expand_vec_sat(type, vece, v0, v1, v2, INDEX_op_add_vec);
3086 case INDEX_op_sssub_vec:
3087 expand_vec_sat(type, vece, v0, v1, v2, INDEX_op_sub_vec);
3091 g_assert_not_reached();
3096 static TCGConstraintSetIndex tcg_target_op_def(TCGOpcode op)
3099 case INDEX_op_goto_ptr:
3102 case INDEX_op_ld8u_i32:
3103 case INDEX_op_ld8u_i64:
3104 case INDEX_op_ld8s_i32:
3105 case INDEX_op_ld8s_i64:
3106 case INDEX_op_ld16u_i32:
3107 case INDEX_op_ld16u_i64:
3108 case INDEX_op_ld16s_i32:
3109 case INDEX_op_ld16s_i64:
3110 case INDEX_op_ld_i32:
3111 case INDEX_op_ld32u_i64:
3112 case INDEX_op_ld32s_i64:
3113 case INDEX_op_ld_i64:
3114 return C_O1_I1(r, r);
3116 case INDEX_op_st8_i32:
3117 case INDEX_op_st8_i64:
3118 case INDEX_op_st16_i32:
3119 case INDEX_op_st16_i64:
3120 case INDEX_op_st_i32:
3121 case INDEX_op_st32_i64:
3122 case INDEX_op_st_i64:
3123 return C_O0_I2(r, r);
3125 case INDEX_op_add_i32:
3126 case INDEX_op_add_i64:
3127 case INDEX_op_shl_i64:
3128 case INDEX_op_shr_i64:
3129 case INDEX_op_sar_i64:
3130 case INDEX_op_rotl_i32:
3131 case INDEX_op_rotl_i64:
3132 case INDEX_op_rotr_i32:
3133 case INDEX_op_rotr_i64:
3134 case INDEX_op_setcond_i32:
3135 case INDEX_op_negsetcond_i32:
3136 return C_O1_I2(r, r, ri);
3137 case INDEX_op_setcond_i64:
3138 case INDEX_op_negsetcond_i64:
3139 return C_O1_I2(r, r, rA);
3141 case INDEX_op_clz_i64:
3142 return C_O1_I2(r, r, rI);
3144 case INDEX_op_sub_i32:
3145 case INDEX_op_sub_i64:
3146 case INDEX_op_and_i32:
3147 case INDEX_op_or_i32:
3148 case INDEX_op_xor_i32:
3149 return C_O1_I2(r, r, ri);
3150 case INDEX_op_and_i64:
3151 return C_O1_I2(r, r, rNKR);
3152 case INDEX_op_or_i64:
3153 case INDEX_op_xor_i64:
3154 return C_O1_I2(r, r, rK);
3156 case INDEX_op_andc_i32:
3157 case INDEX_op_orc_i32:
3158 case INDEX_op_eqv_i32:
3159 return C_O1_I2(r, r, ri);
3160 case INDEX_op_andc_i64:
3161 return C_O1_I2(r, r, rKR);
3162 case INDEX_op_orc_i64:
3163 case INDEX_op_eqv_i64:
3164 return C_O1_I2(r, r, rNK);
3166 case INDEX_op_nand_i32:
3167 case INDEX_op_nand_i64:
3168 case INDEX_op_nor_i32:
3169 case INDEX_op_nor_i64:
3170 return C_O1_I2(r, r, r);
3172 case INDEX_op_mul_i32:
3173 return (HAVE_FACILITY(MISC_INSN_EXT2)
3175 : C_O1_I2(r, 0, ri));
3176 case INDEX_op_mul_i64:
3177 return (HAVE_FACILITY(MISC_INSN_EXT2)
3179 : C_O1_I2(r, 0, rJ));
3181 case INDEX_op_shl_i32:
3182 case INDEX_op_shr_i32:
3183 case INDEX_op_sar_i32:
3184 return C_O1_I2(r, r, ri);
3186 case INDEX_op_brcond_i32:
3187 return C_O0_I2(r, ri);
3188 case INDEX_op_brcond_i64:
3189 return C_O0_I2(r, rA);
3191 case INDEX_op_bswap16_i32:
3192 case INDEX_op_bswap16_i64:
3193 case INDEX_op_bswap32_i32:
3194 case INDEX_op_bswap32_i64:
3195 case INDEX_op_bswap64_i64:
3196 case INDEX_op_neg_i32:
3197 case INDEX_op_neg_i64:
3198 case INDEX_op_not_i32:
3199 case INDEX_op_not_i64:
3200 case INDEX_op_ext8s_i32:
3201 case INDEX_op_ext8s_i64:
3202 case INDEX_op_ext8u_i32:
3203 case INDEX_op_ext8u_i64:
3204 case INDEX_op_ext16s_i32:
3205 case INDEX_op_ext16s_i64:
3206 case INDEX_op_ext16u_i32:
3207 case INDEX_op_ext16u_i64:
3208 case INDEX_op_ext32s_i64:
3209 case INDEX_op_ext32u_i64:
3210 case INDEX_op_ext_i32_i64:
3211 case INDEX_op_extu_i32_i64:
3212 case INDEX_op_extract_i32:
3213 case INDEX_op_extract_i64:
3214 case INDEX_op_ctpop_i32:
3215 case INDEX_op_ctpop_i64:
3216 return C_O1_I1(r, r);
3218 case INDEX_op_qemu_ld_a32_i32:
3219 case INDEX_op_qemu_ld_a64_i32:
3220 case INDEX_op_qemu_ld_a32_i64:
3221 case INDEX_op_qemu_ld_a64_i64:
3222 return C_O1_I1(r, r);
3223 case INDEX_op_qemu_st_a32_i64:
3224 case INDEX_op_qemu_st_a64_i64:
3225 case INDEX_op_qemu_st_a32_i32:
3226 case INDEX_op_qemu_st_a64_i32:
3227 return C_O0_I2(r, r);
3228 case INDEX_op_qemu_ld_a32_i128:
3229 case INDEX_op_qemu_ld_a64_i128:
3230 return C_O2_I1(o, m, r);
3231 case INDEX_op_qemu_st_a32_i128:
3232 case INDEX_op_qemu_st_a64_i128:
3233 return C_O0_I3(o, m, r);
3235 case INDEX_op_deposit_i32:
3236 case INDEX_op_deposit_i64:
3237 return C_O1_I2(r, rZ, r);
3239 case INDEX_op_movcond_i32:
3240 return C_O1_I4(r, r, ri, rI, r);
3241 case INDEX_op_movcond_i64:
3242 return C_O1_I4(r, r, rA, rI, r);
3244 case INDEX_op_div2_i32:
3245 case INDEX_op_div2_i64:
3246 case INDEX_op_divu2_i32:
3247 case INDEX_op_divu2_i64:
3248 return C_O2_I3(o, m, 0, 1, r);
3250 case INDEX_op_mulu2_i64:
3251 return C_O2_I2(o, m, 0, r);
3252 case INDEX_op_muls2_i64:
3253 return C_O2_I2(o, m, r, r);
3255 case INDEX_op_add2_i32:
3256 case INDEX_op_sub2_i32:
3257 return C_N1_O1_I4(r, r, 0, 1, ri, r);
3259 case INDEX_op_add2_i64:
3260 case INDEX_op_sub2_i64:
3261 return C_N1_O1_I4(r, r, 0, 1, rA, r);
3263 case INDEX_op_st_vec:
3264 return C_O0_I2(v, r);
3265 case INDEX_op_ld_vec:
3266 case INDEX_op_dupm_vec:
3267 return C_O1_I1(v, r);
3268 case INDEX_op_dup_vec:
3269 return C_O1_I1(v, vr);
3270 case INDEX_op_abs_vec:
3271 case INDEX_op_neg_vec:
3272 case INDEX_op_not_vec:
3273 case INDEX_op_rotli_vec:
3274 case INDEX_op_sari_vec:
3275 case INDEX_op_shli_vec:
3276 case INDEX_op_shri_vec:
3277 case INDEX_op_s390_vuph_vec:
3278 case INDEX_op_s390_vupl_vec:
3279 return C_O1_I1(v, v);
3280 case INDEX_op_add_vec:
3281 case INDEX_op_sub_vec:
3282 case INDEX_op_and_vec:
3283 case INDEX_op_andc_vec:
3284 case INDEX_op_or_vec:
3285 case INDEX_op_orc_vec:
3286 case INDEX_op_xor_vec:
3287 case INDEX_op_nand_vec:
3288 case INDEX_op_nor_vec:
3289 case INDEX_op_eqv_vec:
3290 case INDEX_op_cmp_vec:
3291 case INDEX_op_mul_vec:
3292 case INDEX_op_rotlv_vec:
3293 case INDEX_op_rotrv_vec:
3294 case INDEX_op_shlv_vec:
3295 case INDEX_op_shrv_vec:
3296 case INDEX_op_sarv_vec:
3297 case INDEX_op_smax_vec:
3298 case INDEX_op_smin_vec:
3299 case INDEX_op_umax_vec:
3300 case INDEX_op_umin_vec:
3301 case INDEX_op_s390_vpks_vec:
3302 return C_O1_I2(v, v, v);
3303 case INDEX_op_rotls_vec:
3304 case INDEX_op_shls_vec:
3305 case INDEX_op_shrs_vec:
3306 case INDEX_op_sars_vec:
3307 return C_O1_I2(v, v, r);
3308 case INDEX_op_bitsel_vec:
3309 return C_O1_I3(v, v, v, v);
3312 g_assert_not_reached();
3317 * Mainline glibc added HWCAP_S390_VX before it was kernel abi.
3318 * Some distros have fixed this up locally, others have not.
3320 #ifndef HWCAP_S390_VXRS
3321 #define HWCAP_S390_VXRS 2048
3324 static void query_s390_facilities(void)
3326 unsigned long hwcap = qemu_getauxval(AT_HWCAP);
3329 /* Is STORE FACILITY LIST EXTENDED available? Honestly, I believe this
3330 is present on all 64-bit systems, but let's check for it anyway. */
3331 if (hwcap & HWCAP_S390_STFLE) {
3332 register int r0 __asm__("0") = ARRAY_SIZE(s390_facilities) - 1;
3333 register void *r1 __asm__("1") = s390_facilities;
3336 asm volatile(".word 0xb2b0,0x1000"
3337 : "=r"(r0) : "r"(r0), "r"(r1) : "memory", "cc");
3341 * Use of vector registers requires os support beyond the facility bit.
3342 * If the kernel does not advertise support, disable the facility bits.
3343 * There is nothing else we currently care about in the 3rd word, so
3344 * disable VECTOR with one store.
3346 if (!(hwcap & HWCAP_S390_VXRS)) {
3347 s390_facilities[2] = 0;
3351 * Minimum supported cpu revision is z196.
3352 * Check for all required facilities.
3353 * ZARCH_ACTIVE is done via preprocessor check for 64-bit.
3355 if (!HAVE_FACILITY(LONG_DISP)) {
3356 which = "long-displacement";
3359 if (!HAVE_FACILITY(EXT_IMM)) {
3360 which = "extended-immediate";
3363 if (!HAVE_FACILITY(GEN_INST_EXT)) {
3364 which = "general-instructions-extension";
3368 * Facility 45 is a big bin that contains: distinct-operands,
3369 * fast-BCR-serialization, high-word, population-count,
3370 * interlocked-access-1, and load/store-on-condition-1
3372 if (!HAVE_FACILITY(45)) {
3379 error_report("%s: missing required facility %s", __func__, which);
3383 static void tcg_target_init(TCGContext *s)
3385 query_s390_facilities();
3387 tcg_target_available_regs[TCG_TYPE_I32] = 0xffff;
3388 tcg_target_available_regs[TCG_TYPE_I64] = 0xffff;
3389 if (HAVE_FACILITY(VECTOR)) {
3390 tcg_target_available_regs[TCG_TYPE_V64] = 0xffffffff00000000ull;
3391 tcg_target_available_regs[TCG_TYPE_V128] = 0xffffffff00000000ull;
3394 tcg_target_call_clobber_regs = 0;
3395 tcg_regset_set_reg(tcg_target_call_clobber_regs, TCG_REG_R0);
3396 tcg_regset_set_reg(tcg_target_call_clobber_regs, TCG_REG_R1);
3397 tcg_regset_set_reg(tcg_target_call_clobber_regs, TCG_REG_R2);
3398 tcg_regset_set_reg(tcg_target_call_clobber_regs, TCG_REG_R3);
3399 tcg_regset_set_reg(tcg_target_call_clobber_regs, TCG_REG_R4);
3400 tcg_regset_set_reg(tcg_target_call_clobber_regs, TCG_REG_R5);
3401 /* The r6 register is technically call-saved, but it's also a parameter
3402 register, so it can get killed by setup for the qemu_st helper. */
3403 tcg_regset_set_reg(tcg_target_call_clobber_regs, TCG_REG_R6);
3404 /* The return register can be considered call-clobbered. */
3405 tcg_regset_set_reg(tcg_target_call_clobber_regs, TCG_REG_R14);
3407 tcg_regset_set_reg(tcg_target_call_clobber_regs, TCG_REG_V0);
3408 tcg_regset_set_reg(tcg_target_call_clobber_regs, TCG_REG_V1);
3409 tcg_regset_set_reg(tcg_target_call_clobber_regs, TCG_REG_V2);
3410 tcg_regset_set_reg(tcg_target_call_clobber_regs, TCG_REG_V3);
3411 tcg_regset_set_reg(tcg_target_call_clobber_regs, TCG_REG_V4);
3412 tcg_regset_set_reg(tcg_target_call_clobber_regs, TCG_REG_V5);
3413 tcg_regset_set_reg(tcg_target_call_clobber_regs, TCG_REG_V6);
3414 tcg_regset_set_reg(tcg_target_call_clobber_regs, TCG_REG_V7);
3415 tcg_regset_set_reg(tcg_target_call_clobber_regs, TCG_REG_V16);
3416 tcg_regset_set_reg(tcg_target_call_clobber_regs, TCG_REG_V17);
3417 tcg_regset_set_reg(tcg_target_call_clobber_regs, TCG_REG_V18);
3418 tcg_regset_set_reg(tcg_target_call_clobber_regs, TCG_REG_V19);
3419 tcg_regset_set_reg(tcg_target_call_clobber_regs, TCG_REG_V20);
3420 tcg_regset_set_reg(tcg_target_call_clobber_regs, TCG_REG_V21);
3421 tcg_regset_set_reg(tcg_target_call_clobber_regs, TCG_REG_V22);
3422 tcg_regset_set_reg(tcg_target_call_clobber_regs, TCG_REG_V23);
3423 tcg_regset_set_reg(tcg_target_call_clobber_regs, TCG_REG_V24);
3424 tcg_regset_set_reg(tcg_target_call_clobber_regs, TCG_REG_V25);
3425 tcg_regset_set_reg(tcg_target_call_clobber_regs, TCG_REG_V26);
3426 tcg_regset_set_reg(tcg_target_call_clobber_regs, TCG_REG_V27);
3427 tcg_regset_set_reg(tcg_target_call_clobber_regs, TCG_REG_V28);
3428 tcg_regset_set_reg(tcg_target_call_clobber_regs, TCG_REG_V29);
3429 tcg_regset_set_reg(tcg_target_call_clobber_regs, TCG_REG_V30);
3430 tcg_regset_set_reg(tcg_target_call_clobber_regs, TCG_REG_V31);
3432 s->reserved_regs = 0;
3433 tcg_regset_set_reg(s->reserved_regs, TCG_TMP0);
3434 /* XXX many insns can't be used with R0, so we better avoid it for now */
3435 tcg_regset_set_reg(s->reserved_regs, TCG_REG_R0);
3436 tcg_regset_set_reg(s->reserved_regs, TCG_REG_CALL_STACK);
3439 #define FRAME_SIZE ((int)(TCG_TARGET_CALL_STACK_OFFSET \
3440 + TCG_STATIC_CALL_ARGS_SIZE \
3441 + CPU_TEMP_BUF_NLONGS * sizeof(long)))
3443 static void tcg_target_qemu_prologue(TCGContext *s)
3445 /* stmg %r6,%r15,48(%r15) (save registers) */
3446 tcg_out_insn(s, RXY, STMG, TCG_REG_R6, TCG_REG_R15, TCG_REG_R15, 48);
3448 /* aghi %r15,-frame_size */
3449 tcg_out_insn(s, RI, AGHI, TCG_REG_R15, -FRAME_SIZE);
3451 tcg_set_frame(s, TCG_REG_CALL_STACK,
3452 TCG_STATIC_CALL_ARGS_SIZE + TCG_TARGET_CALL_STACK_OFFSET,
3453 CPU_TEMP_BUF_NLONGS * sizeof(long));
3455 if (!tcg_use_softmmu && guest_base >= 0x80000) {
3456 tcg_out_movi(s, TCG_TYPE_PTR, TCG_GUEST_BASE_REG, guest_base);
3457 tcg_regset_set_reg(s->reserved_regs, TCG_GUEST_BASE_REG);
3460 tcg_out_mov(s, TCG_TYPE_PTR, TCG_AREG0, tcg_target_call_iarg_regs[0]);
3462 /* br %r3 (go to TB) */
3463 tcg_out_insn(s, RR, BCR, S390_CC_ALWAYS, tcg_target_call_iarg_regs[1]);
3466 * Return path for goto_ptr. Set return value to 0, a-la exit_tb,
3467 * and fall through to the rest of the epilogue.
3469 tcg_code_gen_epilogue = tcg_splitwx_to_rx(s->code_ptr);
3470 tcg_out_movi(s, TCG_TYPE_PTR, TCG_REG_R2, 0);
3473 tb_ret_addr = tcg_splitwx_to_rx(s->code_ptr);
3475 /* lmg %r6,%r15,fs+48(%r15) (restore registers) */
3476 tcg_out_insn(s, RXY, LMG, TCG_REG_R6, TCG_REG_R15, TCG_REG_R15,
3479 /* br %r14 (return) */
3480 tcg_out_insn(s, RR, BCR, S390_CC_ALWAYS, TCG_REG_R14);
3483 static void tcg_out_tb_start(TCGContext *s)
3488 static void tcg_out_nop_fill(tcg_insn_unit *p, int count)
3490 memset(p, 0x07, count * sizeof(tcg_insn_unit));
3495 uint8_t fde_def_cfa[4];
3496 uint8_t fde_reg_ofs[18];
3499 /* We're expecting a 2 byte uleb128 encoded value. */
3500 QEMU_BUILD_BUG_ON(FRAME_SIZE >= (1 << 14));
3502 #define ELF_HOST_MACHINE EM_S390
3504 static const DebugFrame debug_frame = {
3505 .h.cie.len = sizeof(DebugFrameCIE)-4, /* length after .len member */
3508 .h.cie.code_align = 1,
3509 .h.cie.data_align = 8, /* sleb128 8 */
3510 .h.cie.return_column = TCG_REG_R14,
3512 /* Total FDE size does not include the "len" member. */
3513 .h.fde.len = sizeof(DebugFrame) - offsetof(DebugFrame, h.fde.cie_offset),
3516 12, TCG_REG_CALL_STACK, /* DW_CFA_def_cfa %r15, ... */
3517 (FRAME_SIZE & 0x7f) | 0x80, /* ... uleb128 FRAME_SIZE */
3521 0x86, 6, /* DW_CFA_offset, %r6, 48 */
3522 0x87, 7, /* DW_CFA_offset, %r7, 56 */
3523 0x88, 8, /* DW_CFA_offset, %r8, 64 */
3524 0x89, 9, /* DW_CFA_offset, %r92, 72 */
3525 0x8a, 10, /* DW_CFA_offset, %r10, 80 */
3526 0x8b, 11, /* DW_CFA_offset, %r11, 88 */
3527 0x8c, 12, /* DW_CFA_offset, %r12, 96 */
3528 0x8d, 13, /* DW_CFA_offset, %r13, 104 */
3529 0x8e, 14, /* DW_CFA_offset, %r14, 112 */
3533 void tcg_register_jit(const void *buf, size_t buf_size)
3535 tcg_register_jit_int(buf, buf_size, &debug_frame, sizeof(debug_frame));