2 * Tiny Code Generator for QEMU
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-be-ldst.h"
29 /* We only support generating code for 64-bit mode. */
30 #if TCG_TARGET_REG_BITS != 64
31 #error "unsupported code generation mode"
36 /* ??? The translation blocks produced by TCG are generally small enough to
37 be entirely reachable with a 16-bit displacement. Leaving the option for
38 a 32-bit displacement here Just In Case. */
39 #define USE_LONG_BRANCHES 0
41 #define TCG_CT_CONST_MULI 0x100
42 #define TCG_CT_CONST_ORI 0x200
43 #define TCG_CT_CONST_XORI 0x400
44 #define TCG_CT_CONST_CMPI 0x800
46 /* Several places within the instruction set 0 means "no register"
47 rather than TCG_REG_R0. */
48 #define TCG_REG_NONE 0
50 /* A scratch register that may be be used throughout the backend. */
51 #define TCG_TMP0 TCG_REG_R14
53 #ifdef CONFIG_USE_GUEST_BASE
54 #define TCG_GUEST_BASE_REG TCG_REG_R13
56 #define TCG_GUEST_BASE_REG TCG_REG_R0
64 /* All of the following instructions are prefixed with their instruction
65 format, and are defined as 8- or 16-bit quantities, even when the two
66 halves of the 16-bit quantity may appear 32 bits apart in the insn.
67 This makes it easy to copy the values from the tables in Appendix B. */
68 typedef enum S390Opcode {
231 static const char * const tcg_target_reg_names[TCG_TARGET_NB_REGS] = {
232 "%r0", "%r1", "%r2", "%r3", "%r4", "%r5", "%r6", "%r7",
233 "%r8", "%r9", "%r10" "%r11" "%r12" "%r13" "%r14" "%r15"
237 /* Since R6 is a potential argument register, choose it last of the
238 call-saved registers. Likewise prefer the call-clobbered registers
239 in reverse order to maximize the chance of avoiding the arguments. */
240 static const int tcg_target_reg_alloc_order[] = {
241 /* Call saved registers. */
250 /* Call clobbered registers. */
254 /* Argument registers, in reverse order of allocation. */
261 static const int tcg_target_call_iarg_regs[] = {
269 static const int tcg_target_call_oarg_regs[] = {
277 #define S390_CC_NE (S390_CC_LT | S390_CC_GT)
278 #define S390_CC_LE (S390_CC_LT | S390_CC_EQ)
279 #define S390_CC_GE (S390_CC_GT | S390_CC_EQ)
280 #define S390_CC_NEVER 0
281 #define S390_CC_ALWAYS 15
283 /* Condition codes that result from a COMPARE and COMPARE LOGICAL. */
284 static const uint8_t tcg_cond_to_s390_cond[] = {
285 [TCG_COND_EQ] = S390_CC_EQ,
286 [TCG_COND_NE] = S390_CC_NE,
287 [TCG_COND_LT] = S390_CC_LT,
288 [TCG_COND_LE] = S390_CC_LE,
289 [TCG_COND_GT] = S390_CC_GT,
290 [TCG_COND_GE] = S390_CC_GE,
291 [TCG_COND_LTU] = S390_CC_LT,
292 [TCG_COND_LEU] = S390_CC_LE,
293 [TCG_COND_GTU] = S390_CC_GT,
294 [TCG_COND_GEU] = S390_CC_GE,
297 /* Condition codes that result from a LOAD AND TEST. Here, we have no
298 unsigned instruction variation, however since the test is vs zero we
299 can re-map the outcomes appropriately. */
300 static const uint8_t tcg_cond_to_ltr_cond[] = {
301 [TCG_COND_EQ] = S390_CC_EQ,
302 [TCG_COND_NE] = S390_CC_NE,
303 [TCG_COND_LT] = S390_CC_LT,
304 [TCG_COND_LE] = S390_CC_LE,
305 [TCG_COND_GT] = S390_CC_GT,
306 [TCG_COND_GE] = S390_CC_GE,
307 [TCG_COND_LTU] = S390_CC_NEVER,
308 [TCG_COND_LEU] = S390_CC_EQ,
309 [TCG_COND_GTU] = S390_CC_NE,
310 [TCG_COND_GEU] = S390_CC_ALWAYS,
313 #ifdef CONFIG_SOFTMMU
314 static void * const qemu_ld_helpers[16] = {
315 [MO_UB] = helper_ret_ldub_mmu,
316 [MO_SB] = helper_ret_ldsb_mmu,
317 [MO_LEUW] = helper_le_lduw_mmu,
318 [MO_LESW] = helper_le_ldsw_mmu,
319 [MO_LEUL] = helper_le_ldul_mmu,
320 [MO_LESL] = helper_le_ldsl_mmu,
321 [MO_LEQ] = helper_le_ldq_mmu,
322 [MO_BEUW] = helper_be_lduw_mmu,
323 [MO_BESW] = helper_be_ldsw_mmu,
324 [MO_BEUL] = helper_be_ldul_mmu,
325 [MO_BESL] = helper_be_ldsl_mmu,
326 [MO_BEQ] = helper_be_ldq_mmu,
329 static void * const qemu_st_helpers[16] = {
330 [MO_UB] = helper_ret_stb_mmu,
331 [MO_LEUW] = helper_le_stw_mmu,
332 [MO_LEUL] = helper_le_stl_mmu,
333 [MO_LEQ] = helper_le_stq_mmu,
334 [MO_BEUW] = helper_be_stw_mmu,
335 [MO_BEUL] = helper_be_stl_mmu,
336 [MO_BEQ] = helper_be_stq_mmu,
340 static tcg_insn_unit *tb_ret_addr;
342 /* A list of relevant facilities used by this translator. Some of these
343 are required for proper operation, and these are checked at startup. */
345 #define FACILITY_ZARCH_ACTIVE (1ULL << (63 - 2))
346 #define FACILITY_LONG_DISP (1ULL << (63 - 18))
347 #define FACILITY_EXT_IMM (1ULL << (63 - 21))
348 #define FACILITY_GEN_INST_EXT (1ULL << (63 - 34))
349 #define FACILITY_LOAD_ON_COND (1ULL << (63 - 45))
351 static uint64_t facilities;
353 static void patch_reloc(tcg_insn_unit *code_ptr, int type,
354 intptr_t value, intptr_t addend)
356 intptr_t pcrel2 = (tcg_insn_unit *)value - (code_ptr - 1);
357 assert(addend == -2);
361 assert(pcrel2 == (int16_t)pcrel2);
362 tcg_patch16(code_ptr, pcrel2);
365 assert(pcrel2 == (int32_t)pcrel2);
366 tcg_patch32(code_ptr, pcrel2);
374 /* parse target specific constraints */
375 static int target_parse_constraint(TCGArgConstraint *ct, const char **pct_str)
377 const char *ct_str = *pct_str;
380 case 'r': /* all registers */
381 ct->ct |= TCG_CT_REG;
382 tcg_regset_set32(ct->u.regs, 0, 0xffff);
384 case 'R': /* not R0 */
385 ct->ct |= TCG_CT_REG;
386 tcg_regset_set32(ct->u.regs, 0, 0xffff);
387 tcg_regset_reset_reg(ct->u.regs, TCG_REG_R0);
389 case 'L': /* qemu_ld/st constraint */
390 ct->ct |= TCG_CT_REG;
391 tcg_regset_set32(ct->u.regs, 0, 0xffff);
392 tcg_regset_reset_reg (ct->u.regs, TCG_REG_R2);
393 tcg_regset_reset_reg (ct->u.regs, TCG_REG_R3);
394 tcg_regset_reset_reg (ct->u.regs, TCG_REG_R4);
396 case 'a': /* force R2 for division */
397 ct->ct |= TCG_CT_REG;
398 tcg_regset_clear(ct->u.regs);
399 tcg_regset_set_reg(ct->u.regs, TCG_REG_R2);
401 case 'b': /* force R3 for division */
402 ct->ct |= TCG_CT_REG;
403 tcg_regset_clear(ct->u.regs);
404 tcg_regset_set_reg(ct->u.regs, TCG_REG_R3);
407 ct->ct |= TCG_CT_CONST_MULI;
410 ct->ct |= TCG_CT_CONST_ORI;
413 ct->ct |= TCG_CT_CONST_XORI;
416 ct->ct |= TCG_CT_CONST_CMPI;
427 /* Immediates to be used with logical OR. This is an optimization only,
428 since a full 64-bit immediate OR can always be performed with 4 sequential
429 OI[LH][LH] instructions. What we're looking for is immediates that we
430 can load efficiently, and the immediate load plus the reg-reg OR is
431 smaller than the sequential OI's. */
433 static int tcg_match_ori(TCGType type, tcg_target_long val)
435 if (facilities & FACILITY_EXT_IMM) {
436 if (type == TCG_TYPE_I32) {
437 /* All 32-bit ORs can be performed with 1 48-bit insn. */
442 /* Look for negative values. These are best to load with LGHI. */
444 if (val == (int16_t)val) {
447 if (facilities & FACILITY_EXT_IMM) {
448 if (val == (int32_t)val) {
457 /* Immediates to be used with logical XOR. This is almost, but not quite,
458 only an optimization. XOR with immediate is only supported with the
459 extended-immediate facility. That said, there are a few patterns for
460 which it is better to load the value into a register first. */
462 static int tcg_match_xori(TCGType type, tcg_target_long val)
464 if ((facilities & FACILITY_EXT_IMM) == 0) {
468 if (type == TCG_TYPE_I32) {
469 /* All 32-bit XORs can be performed with 1 48-bit insn. */
473 /* Look for negative values. These are best to load with LGHI. */
474 if (val < 0 && val == (int32_t)val) {
481 /* Imediates to be used with comparisons. */
483 static int tcg_match_cmpi(TCGType type, tcg_target_long val)
485 if (facilities & FACILITY_EXT_IMM) {
486 /* The COMPARE IMMEDIATE instruction is available. */
487 if (type == TCG_TYPE_I32) {
488 /* We have a 32-bit immediate and can compare against anything. */
491 /* ??? We have no insight here into whether the comparison is
492 signed or unsigned. The COMPARE IMMEDIATE insn uses a 32-bit
493 signed immediate, and the COMPARE LOGICAL IMMEDIATE insn uses
494 a 32-bit unsigned immediate. If we were to use the (semi)
495 obvious "val == (int32_t)val" we would be enabling unsigned
496 comparisons vs very large numbers. The only solution is to
497 take the intersection of the ranges. */
498 /* ??? Another possible solution is to simply lie and allow all
499 constants here and force the out-of-range values into a temp
500 register in tgen_cmp when we have knowledge of the actual
501 comparison code in use. */
502 return val >= 0 && val <= 0x7fffffff;
505 /* Only the LOAD AND TEST instruction is available. */
510 /* Test if a constant matches the constraint. */
511 static int tcg_target_const_match(tcg_target_long val, TCGType type,
512 const TCGArgConstraint *arg_ct)
516 if (ct & TCG_CT_CONST) {
520 if (type == TCG_TYPE_I32) {
524 /* The following are mutually exclusive. */
525 if (ct & TCG_CT_CONST_MULI) {
526 /* Immediates that may be used with multiply. If we have the
527 general-instruction-extensions, then we have MULTIPLY SINGLE
528 IMMEDIATE with a signed 32-bit, otherwise we have only
529 MULTIPLY HALFWORD IMMEDIATE, with a signed 16-bit. */
530 if (facilities & FACILITY_GEN_INST_EXT) {
531 return val == (int32_t)val;
533 return val == (int16_t)val;
535 } else if (ct & TCG_CT_CONST_ORI) {
536 return tcg_match_ori(type, val);
537 } else if (ct & TCG_CT_CONST_XORI) {
538 return tcg_match_xori(type, val);
539 } else if (ct & TCG_CT_CONST_CMPI) {
540 return tcg_match_cmpi(type, val);
546 /* Emit instructions according to the given instruction format. */
548 static void tcg_out_insn_RR(TCGContext *s, S390Opcode op, TCGReg r1, TCGReg r2)
550 tcg_out16(s, (op << 8) | (r1 << 4) | r2);
553 static void tcg_out_insn_RRE(TCGContext *s, S390Opcode op,
554 TCGReg r1, TCGReg r2)
556 tcg_out32(s, (op << 16) | (r1 << 4) | r2);
559 static void tcg_out_insn_RRF(TCGContext *s, S390Opcode op,
560 TCGReg r1, TCGReg r2, int m3)
562 tcg_out32(s, (op << 16) | (m3 << 12) | (r1 << 4) | r2);
565 static void tcg_out_insn_RI(TCGContext *s, S390Opcode op, TCGReg r1, int i2)
567 tcg_out32(s, (op << 16) | (r1 << 20) | (i2 & 0xffff));
570 static void tcg_out_insn_RIL(TCGContext *s, S390Opcode op, TCGReg r1, int i2)
572 tcg_out16(s, op | (r1 << 4));
576 static void tcg_out_insn_RS(TCGContext *s, S390Opcode op, TCGReg r1,
577 TCGReg b2, TCGReg r3, int disp)
579 tcg_out32(s, (op << 24) | (r1 << 20) | (r3 << 16) | (b2 << 12)
583 static void tcg_out_insn_RSY(TCGContext *s, S390Opcode op, TCGReg r1,
584 TCGReg b2, TCGReg r3, int disp)
586 tcg_out16(s, (op & 0xff00) | (r1 << 4) | r3);
587 tcg_out32(s, (op & 0xff) | (b2 << 28)
588 | ((disp & 0xfff) << 16) | ((disp & 0xff000) >> 4));
591 #define tcg_out_insn_RX tcg_out_insn_RS
592 #define tcg_out_insn_RXY tcg_out_insn_RSY
594 /* Emit an opcode with "type-checking" of the format. */
595 #define tcg_out_insn(S, FMT, OP, ...) \
596 glue(tcg_out_insn_,FMT)(S, glue(glue(FMT,_),OP), ## __VA_ARGS__)
599 /* emit 64-bit shifts */
600 static void tcg_out_sh64(TCGContext* s, S390Opcode op, TCGReg dest,
601 TCGReg src, TCGReg sh_reg, int sh_imm)
603 tcg_out_insn_RSY(s, op, dest, sh_reg, src, sh_imm);
606 /* emit 32-bit shifts */
607 static void tcg_out_sh32(TCGContext* s, S390Opcode op, TCGReg dest,
608 TCGReg sh_reg, int sh_imm)
610 tcg_out_insn_RS(s, op, dest, sh_reg, 0, sh_imm);
613 static void tcg_out_mov(TCGContext *s, TCGType type, TCGReg dst, TCGReg src)
616 if (type == TCG_TYPE_I32) {
617 tcg_out_insn(s, RR, LR, dst, src);
619 tcg_out_insn(s, RRE, LGR, dst, src);
624 /* load a register with an immediate value */
625 static void tcg_out_movi(TCGContext *s, TCGType type,
626 TCGReg ret, tcg_target_long sval)
628 static const S390Opcode lli_insns[4] = {
629 RI_LLILL, RI_LLILH, RI_LLIHL, RI_LLIHH
632 tcg_target_ulong uval = sval;
635 if (type == TCG_TYPE_I32) {
636 uval = (uint32_t)sval;
637 sval = (int32_t)sval;
640 /* Try all 32-bit insns that can load it in one go. */
641 if (sval >= -0x8000 && sval < 0x8000) {
642 tcg_out_insn(s, RI, LGHI, ret, sval);
646 for (i = 0; i < 4; i++) {
647 tcg_target_long mask = 0xffffull << i*16;
648 if ((uval & mask) == uval) {
649 tcg_out_insn_RI(s, lli_insns[i], ret, uval >> i*16);
654 /* Try all 48-bit insns that can load it in one go. */
655 if (facilities & FACILITY_EXT_IMM) {
656 if (sval == (int32_t)sval) {
657 tcg_out_insn(s, RIL, LGFI, ret, sval);
660 if (uval <= 0xffffffff) {
661 tcg_out_insn(s, RIL, LLILF, ret, uval);
664 if ((uval & 0xffffffff) == 0) {
665 tcg_out_insn(s, RIL, LLIHF, ret, uval >> 31 >> 1);
670 /* Try for PC-relative address load. */
671 if ((sval & 1) == 0) {
672 ptrdiff_t off = tcg_pcrel_diff(s, (void *)sval) >> 1;
673 if (off == (int32_t)off) {
674 tcg_out_insn(s, RIL, LARL, ret, off);
679 /* If extended immediates are not present, then we may have to issue
680 several instructions to load the low 32 bits. */
681 if (!(facilities & FACILITY_EXT_IMM)) {
682 /* A 32-bit unsigned value can be loaded in 2 insns. And given
683 that the lli_insns loop above did not succeed, we know that
684 both insns are required. */
685 if (uval <= 0xffffffff) {
686 tcg_out_insn(s, RI, LLILL, ret, uval);
687 tcg_out_insn(s, RI, IILH, ret, uval >> 16);
691 /* If all high bits are set, the value can be loaded in 2 or 3 insns.
692 We first want to make sure that all the high bits get set. With
693 luck the low 16-bits can be considered negative to perform that for
694 free, otherwise we load an explicit -1. */
695 if (sval >> 31 >> 1 == -1) {
697 tcg_out_insn(s, RI, LGHI, ret, uval);
699 tcg_out_insn(s, RI, LGHI, ret, -1);
700 tcg_out_insn(s, RI, IILL, ret, uval);
702 tcg_out_insn(s, RI, IILH, ret, uval >> 16);
707 /* If we get here, both the high and low parts have non-zero bits. */
709 /* Recurse to load the lower 32-bits. */
710 tcg_out_movi(s, TCG_TYPE_I64, ret, uval & 0xffffffff);
712 /* Insert data into the high 32-bits. */
713 uval = uval >> 31 >> 1;
714 if (facilities & FACILITY_EXT_IMM) {
715 if (uval < 0x10000) {
716 tcg_out_insn(s, RI, IIHL, ret, uval);
717 } else if ((uval & 0xffff) == 0) {
718 tcg_out_insn(s, RI, IIHH, ret, uval >> 16);
720 tcg_out_insn(s, RIL, IIHF, ret, uval);
724 tcg_out_insn(s, RI, IIHL, ret, uval);
726 if (uval & 0xffff0000) {
727 tcg_out_insn(s, RI, IIHH, ret, uval >> 16);
733 /* Emit a load/store type instruction. Inputs are:
734 DATA: The register to be loaded or stored.
735 BASE+OFS: The effective address.
736 OPC_RX: If the operation has an RX format opcode (e.g. STC), otherwise 0.
737 OPC_RXY: The RXY format opcode for the operation (e.g. STCY). */
739 static void tcg_out_mem(TCGContext *s, S390Opcode opc_rx, S390Opcode opc_rxy,
740 TCGReg data, TCGReg base, TCGReg index,
743 if (ofs < -0x80000 || ofs >= 0x80000) {
744 /* Combine the low 20 bits of the offset with the actual load insn;
745 the high 44 bits must come from an immediate load. */
746 tcg_target_long low = ((ofs & 0xfffff) ^ 0x80000) - 0x80000;
747 tcg_out_movi(s, TCG_TYPE_PTR, TCG_TMP0, ofs - low);
750 /* If we were already given an index register, add it in. */
751 if (index != TCG_REG_NONE) {
752 tcg_out_insn(s, RRE, AGR, TCG_TMP0, index);
757 if (opc_rx && ofs >= 0 && ofs < 0x1000) {
758 tcg_out_insn_RX(s, opc_rx, data, base, index, ofs);
760 tcg_out_insn_RXY(s, opc_rxy, data, base, index, ofs);
765 /* load data without address translation or endianness conversion */
766 static inline void tcg_out_ld(TCGContext *s, TCGType type, TCGReg data,
767 TCGReg base, intptr_t ofs)
769 if (type == TCG_TYPE_I32) {
770 tcg_out_mem(s, RX_L, RXY_LY, data, base, TCG_REG_NONE, ofs);
772 tcg_out_mem(s, 0, RXY_LG, data, base, TCG_REG_NONE, ofs);
776 static inline void tcg_out_st(TCGContext *s, TCGType type, TCGReg data,
777 TCGReg base, intptr_t ofs)
779 if (type == TCG_TYPE_I32) {
780 tcg_out_mem(s, RX_ST, RXY_STY, data, base, TCG_REG_NONE, ofs);
782 tcg_out_mem(s, 0, RXY_STG, data, base, TCG_REG_NONE, ofs);
786 /* load data from an absolute host address */
787 static void tcg_out_ld_abs(TCGContext *s, TCGType type, TCGReg dest, void *abs)
789 intptr_t addr = (intptr_t)abs;
791 if ((facilities & FACILITY_GEN_INST_EXT) && !(addr & 1)) {
792 ptrdiff_t disp = tcg_pcrel_diff(s, abs) >> 1;
793 if (disp == (int32_t)disp) {
794 if (type == TCG_TYPE_I32) {
795 tcg_out_insn(s, RIL, LRL, dest, disp);
797 tcg_out_insn(s, RIL, LGRL, dest, disp);
803 tcg_out_movi(s, TCG_TYPE_PTR, dest, addr & ~0xffff);
804 tcg_out_ld(s, type, dest, dest, addr & 0xffff);
807 static inline void tcg_out_risbg(TCGContext *s, TCGReg dest, TCGReg src,
808 int msb, int lsb, int ofs, int z)
811 tcg_out16(s, (RIE_RISBG & 0xff00) | (dest << 4) | src);
812 tcg_out16(s, (msb << 8) | (z << 7) | lsb);
813 tcg_out16(s, (ofs << 8) | (RIE_RISBG & 0xff));
816 static void tgen_ext8s(TCGContext *s, TCGType type, TCGReg dest, TCGReg src)
818 if (facilities & FACILITY_EXT_IMM) {
819 tcg_out_insn(s, RRE, LGBR, dest, src);
823 if (type == TCG_TYPE_I32) {
825 tcg_out_sh32(s, RS_SLL, dest, TCG_REG_NONE, 24);
827 tcg_out_sh64(s, RSY_SLLG, dest, src, TCG_REG_NONE, 24);
829 tcg_out_sh32(s, RS_SRA, dest, TCG_REG_NONE, 24);
831 tcg_out_sh64(s, RSY_SLLG, dest, src, TCG_REG_NONE, 56);
832 tcg_out_sh64(s, RSY_SRAG, dest, dest, TCG_REG_NONE, 56);
836 static void tgen_ext8u(TCGContext *s, TCGType type, TCGReg dest, TCGReg src)
838 if (facilities & FACILITY_EXT_IMM) {
839 tcg_out_insn(s, RRE, LLGCR, dest, src);
844 tcg_out_movi(s, type, TCG_TMP0, 0xff);
847 tcg_out_movi(s, type, dest, 0xff);
849 if (type == TCG_TYPE_I32) {
850 tcg_out_insn(s, RR, NR, dest, src);
852 tcg_out_insn(s, RRE, NGR, dest, src);
856 static void tgen_ext16s(TCGContext *s, TCGType type, TCGReg dest, TCGReg src)
858 if (facilities & FACILITY_EXT_IMM) {
859 tcg_out_insn(s, RRE, LGHR, dest, src);
863 if (type == TCG_TYPE_I32) {
865 tcg_out_sh32(s, RS_SLL, dest, TCG_REG_NONE, 16);
867 tcg_out_sh64(s, RSY_SLLG, dest, src, TCG_REG_NONE, 16);
869 tcg_out_sh32(s, RS_SRA, dest, TCG_REG_NONE, 16);
871 tcg_out_sh64(s, RSY_SLLG, dest, src, TCG_REG_NONE, 48);
872 tcg_out_sh64(s, RSY_SRAG, dest, dest, TCG_REG_NONE, 48);
876 static void tgen_ext16u(TCGContext *s, TCGType type, TCGReg dest, TCGReg src)
878 if (facilities & FACILITY_EXT_IMM) {
879 tcg_out_insn(s, RRE, LLGHR, dest, src);
884 tcg_out_movi(s, type, TCG_TMP0, 0xffff);
887 tcg_out_movi(s, type, dest, 0xffff);
889 if (type == TCG_TYPE_I32) {
890 tcg_out_insn(s, RR, NR, dest, src);
892 tcg_out_insn(s, RRE, NGR, dest, src);
896 static inline void tgen_ext32s(TCGContext *s, TCGReg dest, TCGReg src)
898 tcg_out_insn(s, RRE, LGFR, dest, src);
901 static inline void tgen_ext32u(TCGContext *s, TCGReg dest, TCGReg src)
903 tcg_out_insn(s, RRE, LLGFR, dest, src);
906 /* Accept bit patterns like these:
911 Copied from gcc sources. */
912 static inline bool risbg_mask(uint64_t c)
915 /* We don't change the number of transitions by inverting,
916 so make sure we start with the LSB zero. */
920 /* Reject all zeros or all ones. */
924 /* Find the first transition. */
926 /* Invert to look for a second transition. */
928 /* Erase the first transition. */
930 /* Find the second transition, if any. */
932 /* Match if all the bits are 1's, or if c is zero. */
936 static void tgen_andi_risbg(TCGContext *s, TCGReg out, TCGReg in, uint64_t val)
939 if ((val & 0x8000000000000001ull) == 0x8000000000000001ull) {
940 /* Achieve wraparound by swapping msb and lsb. */
941 msb = 64 - ctz64(~val);
942 lsb = clz64(~val) - 1;
945 lsb = 63 - ctz64(val);
947 tcg_out_risbg(s, out, in, msb, lsb, 0, 1);
950 static void tgen_andi(TCGContext *s, TCGType type, TCGReg dest, uint64_t val)
952 static const S390Opcode ni_insns[4] = {
953 RI_NILL, RI_NILH, RI_NIHL, RI_NIHH
955 static const S390Opcode nif_insns[2] = {
958 uint64_t valid = (type == TCG_TYPE_I32 ? 0xffffffffull : -1ull);
961 /* Look for the zero-extensions. */
962 if ((val & valid) == 0xffffffff) {
963 tgen_ext32u(s, dest, dest);
966 if (facilities & FACILITY_EXT_IMM) {
967 if ((val & valid) == 0xff) {
968 tgen_ext8u(s, TCG_TYPE_I64, dest, dest);
971 if ((val & valid) == 0xffff) {
972 tgen_ext16u(s, TCG_TYPE_I64, dest, dest);
977 /* Try all 32-bit insns that can perform it in one go. */
978 for (i = 0; i < 4; i++) {
979 tcg_target_ulong mask = ~(0xffffull << i*16);
980 if (((val | ~valid) & mask) == mask) {
981 tcg_out_insn_RI(s, ni_insns[i], dest, val >> i*16);
986 /* Try all 48-bit insns that can perform it in one go. */
987 if (facilities & FACILITY_EXT_IMM) {
988 for (i = 0; i < 2; i++) {
989 tcg_target_ulong mask = ~(0xffffffffull << i*32);
990 if (((val | ~valid) & mask) == mask) {
991 tcg_out_insn_RIL(s, nif_insns[i], dest, val >> i*32);
996 if ((facilities & FACILITY_GEN_INST_EXT) && risbg_mask(val)) {
997 tgen_andi_risbg(s, dest, dest, val);
1001 /* Fall back to loading the constant. */
1002 tcg_out_movi(s, type, TCG_TMP0, val);
1003 if (type == TCG_TYPE_I32) {
1004 tcg_out_insn(s, RR, NR, dest, TCG_TMP0);
1006 tcg_out_insn(s, RRE, NGR, dest, TCG_TMP0);
1010 static void tgen64_ori(TCGContext *s, TCGReg dest, tcg_target_ulong val)
1012 static const S390Opcode oi_insns[4] = {
1013 RI_OILL, RI_OILH, RI_OIHL, RI_OIHH
1015 static const S390Opcode nif_insns[2] = {
1021 /* Look for no-op. */
1026 if (facilities & FACILITY_EXT_IMM) {
1027 /* Try all 32-bit insns that can perform it in one go. */
1028 for (i = 0; i < 4; i++) {
1029 tcg_target_ulong mask = (0xffffull << i*16);
1030 if ((val & mask) != 0 && (val & ~mask) == 0) {
1031 tcg_out_insn_RI(s, oi_insns[i], dest, val >> i*16);
1036 /* Try all 48-bit insns that can perform it in one go. */
1037 for (i = 0; i < 2; i++) {
1038 tcg_target_ulong mask = (0xffffffffull << i*32);
1039 if ((val & mask) != 0 && (val & ~mask) == 0) {
1040 tcg_out_insn_RIL(s, nif_insns[i], dest, val >> i*32);
1045 /* Perform the OR via sequential modifications to the high and
1046 low parts. Do this via recursion to handle 16-bit vs 32-bit
1047 masks in each half. */
1048 tgen64_ori(s, dest, val & 0x00000000ffffffffull);
1049 tgen64_ori(s, dest, val & 0xffffffff00000000ull);
1051 /* With no extended-immediate facility, we don't need to be so
1052 clever. Just iterate over the insns and mask in the constant. */
1053 for (i = 0; i < 4; i++) {
1054 tcg_target_ulong mask = (0xffffull << i*16);
1055 if ((val & mask) != 0) {
1056 tcg_out_insn_RI(s, oi_insns[i], dest, val >> i*16);
1062 static void tgen64_xori(TCGContext *s, TCGReg dest, tcg_target_ulong val)
1064 /* Perform the xor by parts. */
1065 if (val & 0xffffffff) {
1066 tcg_out_insn(s, RIL, XILF, dest, val);
1068 if (val > 0xffffffff) {
1069 tcg_out_insn(s, RIL, XIHF, dest, val >> 31 >> 1);
1073 static int tgen_cmp(TCGContext *s, TCGType type, TCGCond c, TCGReg r1,
1074 TCGArg c2, int c2const)
1076 bool is_unsigned = is_unsigned_cond(c);
1079 if (type == TCG_TYPE_I32) {
1080 tcg_out_insn(s, RR, LTR, r1, r1);
1082 tcg_out_insn(s, RRE, LTGR, r1, r1);
1084 return tcg_cond_to_ltr_cond[c];
1087 if (type == TCG_TYPE_I32) {
1088 tcg_out_insn(s, RIL, CLFI, r1, c2);
1090 tcg_out_insn(s, RIL, CLGFI, r1, c2);
1093 if (type == TCG_TYPE_I32) {
1094 tcg_out_insn(s, RIL, CFI, r1, c2);
1096 tcg_out_insn(s, RIL, CGFI, r1, c2);
1102 if (type == TCG_TYPE_I32) {
1103 tcg_out_insn(s, RR, CLR, r1, c2);
1105 tcg_out_insn(s, RRE, CLGR, r1, c2);
1108 if (type == TCG_TYPE_I32) {
1109 tcg_out_insn(s, RR, CR, r1, c2);
1111 tcg_out_insn(s, RRE, CGR, r1, c2);
1115 return tcg_cond_to_s390_cond[c];
1118 static void tgen_setcond(TCGContext *s, TCGType type, TCGCond c,
1119 TCGReg dest, TCGReg c1, TCGArg c2, int c2const)
1121 int cc = tgen_cmp(s, type, c, c1, c2, c2const);
1123 /* Emit: r1 = 1; if (cc) goto over; r1 = 0; over: */
1124 tcg_out_movi(s, type, dest, 1);
1125 tcg_out_insn(s, RI, BRC, cc, (4 + 4) >> 1);
1126 tcg_out_movi(s, type, dest, 0);
1129 static void tgen_movcond(TCGContext *s, TCGType type, TCGCond c, TCGReg dest,
1130 TCGReg c1, TCGArg c2, int c2const, TCGReg r3)
1133 if (facilities & FACILITY_LOAD_ON_COND) {
1134 cc = tgen_cmp(s, type, c, c1, c2, c2const);
1135 tcg_out_insn(s, RRF, LOCGR, dest, r3, cc);
1137 c = tcg_invert_cond(c);
1138 cc = tgen_cmp(s, type, c, c1, c2, c2const);
1140 /* Emit: if (cc) goto over; dest = r3; over: */
1141 tcg_out_insn(s, RI, BRC, cc, (4 + 4) >> 1);
1142 tcg_out_insn(s, RRE, LGR, dest, r3);
1146 bool tcg_target_deposit_valid(int ofs, int len)
1148 return (facilities & FACILITY_GEN_INST_EXT) != 0;
1151 static void tgen_deposit(TCGContext *s, TCGReg dest, TCGReg src,
1154 int lsb = (63 - ofs);
1155 int msb = lsb - (len - 1);
1156 tcg_out_risbg(s, dest, src, msb, lsb, ofs, 0);
1159 static void tgen_gotoi(TCGContext *s, int cc, tcg_insn_unit *dest)
1161 ptrdiff_t off = dest - s->code_ptr;
1162 if (off == (int16_t)off) {
1163 tcg_out_insn(s, RI, BRC, cc, off);
1164 } else if (off == (int32_t)off) {
1165 tcg_out_insn(s, RIL, BRCL, cc, off);
1167 tcg_out_movi(s, TCG_TYPE_PTR, TCG_TMP0, (uintptr_t)dest);
1168 tcg_out_insn(s, RR, BCR, cc, TCG_TMP0);
1172 static void tgen_branch(TCGContext *s, int cc, int labelno)
1174 TCGLabel* l = &s->labels[labelno];
1176 tgen_gotoi(s, cc, l->u.value_ptr);
1177 } else if (USE_LONG_BRANCHES) {
1178 tcg_out16(s, RIL_BRCL | (cc << 4));
1179 tcg_out_reloc(s, s->code_ptr, R_390_PC32DBL, labelno, -2);
1182 tcg_out16(s, RI_BRC | (cc << 4));
1183 tcg_out_reloc(s, s->code_ptr, R_390_PC16DBL, labelno, -2);
1188 static void tgen_compare_branch(TCGContext *s, S390Opcode opc, int cc,
1189 TCGReg r1, TCGReg r2, int labelno)
1191 TCGLabel* l = &s->labels[labelno];
1195 off = l->u.value_ptr - s->code_ptr;
1197 /* We need to keep the offset unchanged for retranslation. */
1198 off = s->code_ptr[1];
1199 tcg_out_reloc(s, s->code_ptr + 1, R_390_PC16DBL, labelno, -2);
1202 tcg_out16(s, (opc & 0xff00) | (r1 << 4) | r2);
1204 tcg_out16(s, cc << 12 | (opc & 0xff));
1207 static void tgen_compare_imm_branch(TCGContext *s, S390Opcode opc, int cc,
1208 TCGReg r1, int i2, int labelno)
1210 TCGLabel* l = &s->labels[labelno];
1211 tcg_target_long off;
1214 off = l->u.value_ptr - s->code_ptr;
1216 /* We need to keep the offset unchanged for retranslation. */
1217 off = s->code_ptr[1];
1218 tcg_out_reloc(s, s->code_ptr + 1, R_390_PC16DBL, labelno, -2);
1221 tcg_out16(s, (opc & 0xff00) | (r1 << 4) | cc);
1223 tcg_out16(s, (i2 << 8) | (opc & 0xff));
1226 static void tgen_brcond(TCGContext *s, TCGType type, TCGCond c,
1227 TCGReg r1, TCGArg c2, int c2const, int labelno)
1231 if (facilities & FACILITY_GEN_INST_EXT) {
1232 bool is_unsigned = is_unsigned_cond(c);
1236 cc = tcg_cond_to_s390_cond[c];
1239 opc = (type == TCG_TYPE_I32
1240 ? (is_unsigned ? RIE_CLRJ : RIE_CRJ)
1241 : (is_unsigned ? RIE_CLGRJ : RIE_CGRJ));
1242 tgen_compare_branch(s, opc, cc, r1, c2, labelno);
1246 /* COMPARE IMMEDIATE AND BRANCH RELATIVE has an 8-bit immediate field.
1247 If the immediate we've been given does not fit that range, we'll
1248 fall back to separate compare and branch instructions using the
1249 larger comparison range afforded by COMPARE IMMEDIATE. */
1250 if (type == TCG_TYPE_I32) {
1253 in_range = (uint32_t)c2 == (uint8_t)c2;
1256 in_range = (int32_t)c2 == (int8_t)c2;
1261 in_range = (uint64_t)c2 == (uint8_t)c2;
1264 in_range = (int64_t)c2 == (int8_t)c2;
1268 tgen_compare_imm_branch(s, opc, cc, r1, c2, labelno);
1273 cc = tgen_cmp(s, type, c, r1, c2, c2const);
1274 tgen_branch(s, cc, labelno);
1277 static void tcg_out_call(TCGContext *s, tcg_insn_unit *dest)
1279 ptrdiff_t off = dest - s->code_ptr;
1280 if (off == (int32_t)off) {
1281 tcg_out_insn(s, RIL, BRASL, TCG_REG_R14, off);
1283 tcg_out_movi(s, TCG_TYPE_PTR, TCG_TMP0, (uintptr_t)dest);
1284 tcg_out_insn(s, RR, BASR, TCG_REG_R14, TCG_TMP0);
1288 static void tcg_out_qemu_ld_direct(TCGContext *s, TCGMemOp opc, TCGReg data,
1289 TCGReg base, TCGReg index, int disp)
1293 tcg_out_insn(s, RXY, LLGC, data, base, index, disp);
1296 tcg_out_insn(s, RXY, LGB, data, base, index, disp);
1299 case MO_UW | MO_BSWAP:
1300 /* swapped unsigned halfword load with upper bits zeroed */
1301 tcg_out_insn(s, RXY, LRVH, data, base, index, disp);
1302 tgen_ext16u(s, TCG_TYPE_I64, data, data);
1305 tcg_out_insn(s, RXY, LLGH, data, base, index, disp);
1308 case MO_SW | MO_BSWAP:
1309 /* swapped sign-extended halfword load */
1310 tcg_out_insn(s, RXY, LRVH, data, base, index, disp);
1311 tgen_ext16s(s, TCG_TYPE_I64, data, data);
1314 tcg_out_insn(s, RXY, LGH, data, base, index, disp);
1317 case MO_UL | MO_BSWAP:
1318 /* swapped unsigned int load with upper bits zeroed */
1319 tcg_out_insn(s, RXY, LRV, data, base, index, disp);
1320 tgen_ext32u(s, data, data);
1323 tcg_out_insn(s, RXY, LLGF, data, base, index, disp);
1326 case MO_SL | MO_BSWAP:
1327 /* swapped sign-extended int load */
1328 tcg_out_insn(s, RXY, LRV, data, base, index, disp);
1329 tgen_ext32s(s, data, data);
1332 tcg_out_insn(s, RXY, LGF, data, base, index, disp);
1335 case MO_Q | MO_BSWAP:
1336 tcg_out_insn(s, RXY, LRVG, data, base, index, disp);
1339 tcg_out_insn(s, RXY, LG, data, base, index, disp);
1347 static void tcg_out_qemu_st_direct(TCGContext *s, TCGMemOp opc, TCGReg data,
1348 TCGReg base, TCGReg index, int disp)
1352 if (disp >= 0 && disp < 0x1000) {
1353 tcg_out_insn(s, RX, STC, data, base, index, disp);
1355 tcg_out_insn(s, RXY, STCY, data, base, index, disp);
1359 case MO_UW | MO_BSWAP:
1360 tcg_out_insn(s, RXY, STRVH, data, base, index, disp);
1363 if (disp >= 0 && disp < 0x1000) {
1364 tcg_out_insn(s, RX, STH, data, base, index, disp);
1366 tcg_out_insn(s, RXY, STHY, data, base, index, disp);
1370 case MO_UL | MO_BSWAP:
1371 tcg_out_insn(s, RXY, STRV, data, base, index, disp);
1374 if (disp >= 0 && disp < 0x1000) {
1375 tcg_out_insn(s, RX, ST, data, base, index, disp);
1377 tcg_out_insn(s, RXY, STY, data, base, index, disp);
1381 case MO_Q | MO_BSWAP:
1382 tcg_out_insn(s, RXY, STRVG, data, base, index, disp);
1385 tcg_out_insn(s, RXY, STG, data, base, index, disp);
1393 #if defined(CONFIG_SOFTMMU)
1394 /* We're expecting to use a 20-bit signed offset on the tlb memory ops.
1395 Using the offset of the second entry in the last tlb table ensures
1396 that we can index all of the elements of the first entry. */
1397 QEMU_BUILD_BUG_ON(offsetof(CPUArchState, tlb_table[NB_MMU_MODES - 1][1])
1400 /* Load and compare a TLB entry, leaving the flags set. Loads the TLB
1401 addend into R2. Returns a register with the santitized guest address. */
1402 static TCGReg tcg_out_tlb_read(TCGContext* s, TCGReg addr_reg, TCGMemOp opc,
1403 int mem_index, bool is_ld)
1405 TCGMemOp s_bits = opc & MO_SIZE;
1406 uint64_t tlb_mask = TARGET_PAGE_MASK | ((1 << s_bits) - 1);
1409 if (facilities & FACILITY_GEN_INST_EXT) {
1410 tcg_out_risbg(s, TCG_REG_R2, addr_reg,
1411 64 - CPU_TLB_BITS - CPU_TLB_ENTRY_BITS,
1412 63 - CPU_TLB_ENTRY_BITS,
1413 64 + CPU_TLB_ENTRY_BITS - TARGET_PAGE_BITS, 1);
1414 tgen_andi_risbg(s, TCG_REG_R3, addr_reg, tlb_mask);
1416 tcg_out_sh64(s, RSY_SRLG, TCG_REG_R2, addr_reg, TCG_REG_NONE,
1417 TARGET_PAGE_BITS - CPU_TLB_ENTRY_BITS);
1418 tcg_out_movi(s, TCG_TYPE_TL, TCG_REG_R3, addr_reg);
1419 tgen_andi(s, TCG_TYPE_I64, TCG_REG_R2,
1420 (CPU_TLB_SIZE - 1) << CPU_TLB_ENTRY_BITS);
1421 tgen_andi(s, TCG_TYPE_TL, TCG_REG_R3, tlb_mask);
1425 ofs = offsetof(CPUArchState, tlb_table[mem_index][0].addr_read);
1427 ofs = offsetof(CPUArchState, tlb_table[mem_index][0].addr_write);
1429 if (TARGET_LONG_BITS == 32) {
1430 tcg_out_mem(s, RX_C, RXY_CY, TCG_REG_R3, TCG_REG_R2, TCG_AREG0, ofs);
1432 tcg_out_mem(s, 0, RXY_CG, TCG_REG_R3, TCG_REG_R2, TCG_AREG0, ofs);
1435 ofs = offsetof(CPUArchState, tlb_table[mem_index][0].addend);
1436 tcg_out_mem(s, 0, RXY_LG, TCG_REG_R2, TCG_REG_R2, TCG_AREG0, ofs);
1438 if (TARGET_LONG_BITS == 32) {
1439 tgen_ext32u(s, TCG_REG_R3, addr_reg);
1445 static void add_qemu_ldst_label(TCGContext *s, bool is_ld, TCGMemOp opc,
1446 TCGReg data, TCGReg addr, int mem_index,
1447 tcg_insn_unit *raddr, tcg_insn_unit *label_ptr)
1449 TCGLabelQemuLdst *label = new_ldst_label(s);
1451 label->is_ld = is_ld;
1453 label->datalo_reg = data;
1454 label->addrlo_reg = addr;
1455 label->mem_index = mem_index;
1456 label->raddr = raddr;
1457 label->label_ptr[0] = label_ptr;
1460 static void tcg_out_qemu_ld_slow_path(TCGContext *s, TCGLabelQemuLdst *lb)
1462 TCGReg addr_reg = lb->addrlo_reg;
1463 TCGReg data_reg = lb->datalo_reg;
1464 TCGMemOp opc = lb->opc;
1466 patch_reloc(lb->label_ptr[0], R_390_PC16DBL, (intptr_t)s->code_ptr, -2);
1468 tcg_out_mov(s, TCG_TYPE_PTR, TCG_REG_R2, TCG_AREG0);
1469 if (TARGET_LONG_BITS == 64) {
1470 tcg_out_mov(s, TCG_TYPE_I64, TCG_REG_R3, addr_reg);
1472 tcg_out_movi(s, TCG_TYPE_I32, TCG_REG_R4, lb->mem_index);
1473 tcg_out_movi(s, TCG_TYPE_PTR, TCG_REG_R5, (uintptr_t)lb->raddr);
1474 tcg_out_call(s, qemu_ld_helpers[opc]);
1475 tcg_out_mov(s, TCG_TYPE_I64, data_reg, TCG_REG_R2);
1477 tgen_gotoi(s, S390_CC_ALWAYS, lb->raddr);
1480 static void tcg_out_qemu_st_slow_path(TCGContext *s, TCGLabelQemuLdst *lb)
1482 TCGReg addr_reg = lb->addrlo_reg;
1483 TCGReg data_reg = lb->datalo_reg;
1484 TCGMemOp opc = lb->opc;
1486 patch_reloc(lb->label_ptr[0], R_390_PC16DBL, (intptr_t)s->code_ptr, -2);
1488 tcg_out_mov(s, TCG_TYPE_PTR, TCG_REG_R2, TCG_AREG0);
1489 if (TARGET_LONG_BITS == 64) {
1490 tcg_out_mov(s, TCG_TYPE_I64, TCG_REG_R3, addr_reg);
1492 switch (opc & MO_SIZE) {
1494 tgen_ext8u(s, TCG_TYPE_I64, TCG_REG_R4, data_reg);
1497 tgen_ext16u(s, TCG_TYPE_I64, TCG_REG_R4, data_reg);
1500 tgen_ext32u(s, TCG_REG_R4, data_reg);
1503 tcg_out_mov(s, TCG_TYPE_I64, TCG_REG_R4, data_reg);
1508 tcg_out_movi(s, TCG_TYPE_I32, TCG_REG_R5, lb->mem_index);
1509 tcg_out_movi(s, TCG_TYPE_PTR, TCG_REG_R6, (uintptr_t)lb->raddr);
1510 tcg_out_call(s, qemu_st_helpers[opc]);
1512 tgen_gotoi(s, S390_CC_ALWAYS, lb->raddr);
1515 static void tcg_prepare_user_ldst(TCGContext *s, TCGReg *addr_reg,
1516 TCGReg *index_reg, tcg_target_long *disp)
1518 if (TARGET_LONG_BITS == 32) {
1519 tgen_ext32u(s, TCG_TMP0, *addr_reg);
1520 *addr_reg = TCG_TMP0;
1522 if (GUEST_BASE < 0x80000) {
1523 *index_reg = TCG_REG_NONE;
1526 *index_reg = TCG_GUEST_BASE_REG;
1530 #endif /* CONFIG_SOFTMMU */
1532 static void tcg_out_qemu_ld(TCGContext* s, TCGReg data_reg, TCGReg addr_reg,
1533 TCGMemOp opc, int mem_index)
1535 #ifdef CONFIG_SOFTMMU
1536 tcg_insn_unit *label_ptr;
1539 base_reg = tcg_out_tlb_read(s, addr_reg, opc, mem_index, 1);
1541 label_ptr = s->code_ptr + 1;
1542 tcg_out_insn(s, RI, BRC, S390_CC_NE, 0);
1544 tcg_out_qemu_ld_direct(s, opc, data_reg, base_reg, TCG_REG_R2, 0);
1546 add_qemu_ldst_label(s, 1, opc, data_reg, addr_reg, mem_index,
1547 s->code_ptr, label_ptr);
1550 tcg_target_long disp;
1552 tcg_prepare_user_ldst(s, &addr_reg, &index_reg, &disp);
1553 tcg_out_qemu_ld_direct(s, opc, data_reg, addr_reg, index_reg, disp);
1557 static void tcg_out_qemu_st(TCGContext* s, TCGReg data_reg, TCGReg addr_reg,
1558 TCGMemOp opc, int mem_index)
1560 #ifdef CONFIG_SOFTMMU
1561 tcg_insn_unit *label_ptr;
1564 base_reg = tcg_out_tlb_read(s, addr_reg, opc, mem_index, 0);
1566 label_ptr = s->code_ptr + 1;
1567 tcg_out_insn(s, RI, BRC, S390_CC_NE, 0);
1569 tcg_out_qemu_st_direct(s, opc, data_reg, base_reg, TCG_REG_R2, 0);
1571 add_qemu_ldst_label(s, 0, opc, data_reg, addr_reg, mem_index,
1572 s->code_ptr, label_ptr);
1575 tcg_target_long disp;
1577 tcg_prepare_user_ldst(s, &addr_reg, &index_reg, &disp);
1578 tcg_out_qemu_st_direct(s, opc, data_reg, addr_reg, index_reg, disp);
1582 # define OP_32_64(x) \
1583 case glue(glue(INDEX_op_,x),_i32): \
1584 case glue(glue(INDEX_op_,x),_i64)
1586 static inline void tcg_out_op(TCGContext *s, TCGOpcode opc,
1587 const TCGArg *args, const int *const_args)
1593 case INDEX_op_exit_tb:
1595 tcg_out_movi(s, TCG_TYPE_PTR, TCG_REG_R2, args[0]);
1596 tgen_gotoi(s, S390_CC_ALWAYS, tb_ret_addr);
1599 case INDEX_op_goto_tb:
1600 if (s->tb_jmp_offset) {
1603 /* load address stored at s->tb_next + args[0] */
1604 tcg_out_ld_abs(s, TCG_TYPE_PTR, TCG_TMP0, s->tb_next + args[0]);
1606 tcg_out_insn(s, RR, BCR, S390_CC_ALWAYS, TCG_TMP0);
1608 s->tb_next_offset[args[0]] = tcg_current_code_size(s);
1612 /* ??? LLC (RXY format) is only present with the extended-immediate
1613 facility, whereas LLGC is always present. */
1614 tcg_out_mem(s, 0, RXY_LLGC, args[0], args[1], TCG_REG_NONE, args[2]);
1618 /* ??? LB is no smaller than LGB, so no point to using it. */
1619 tcg_out_mem(s, 0, RXY_LGB, args[0], args[1], TCG_REG_NONE, args[2]);
1623 /* ??? LLH (RXY format) is only present with the extended-immediate
1624 facility, whereas LLGH is always present. */
1625 tcg_out_mem(s, 0, RXY_LLGH, args[0], args[1], TCG_REG_NONE, args[2]);
1628 case INDEX_op_ld16s_i32:
1629 tcg_out_mem(s, RX_LH, RXY_LHY, args[0], args[1], TCG_REG_NONE, args[2]);
1632 case INDEX_op_ld_i32:
1633 tcg_out_ld(s, TCG_TYPE_I32, args[0], args[1], args[2]);
1637 tcg_out_mem(s, RX_STC, RXY_STCY, args[0], args[1],
1638 TCG_REG_NONE, args[2]);
1642 tcg_out_mem(s, RX_STH, RXY_STHY, args[0], args[1],
1643 TCG_REG_NONE, args[2]);
1646 case INDEX_op_st_i32:
1647 tcg_out_st(s, TCG_TYPE_I32, args[0], args[1], args[2]);
1650 case INDEX_op_add_i32:
1651 a0 = args[0], a1 = args[1], a2 = (int32_t)args[2];
1652 if (const_args[2]) {
1655 if (a2 == (int16_t)a2) {
1656 tcg_out_insn(s, RI, AHI, a0, a2);
1659 if (facilities & FACILITY_EXT_IMM) {
1660 tcg_out_insn(s, RIL, AFI, a0, a2);
1664 tcg_out_mem(s, RX_LA, RXY_LAY, a0, a1, TCG_REG_NONE, a2);
1665 } else if (a0 == a1) {
1666 tcg_out_insn(s, RR, AR, a0, a2);
1668 tcg_out_insn(s, RX, LA, a0, a1, a2, 0);
1671 case INDEX_op_sub_i32:
1672 a0 = args[0], a1 = args[1], a2 = (int32_t)args[2];
1673 if (const_args[2]) {
1677 tcg_out_insn(s, RR, SR, args[0], args[2]);
1680 case INDEX_op_and_i32:
1681 if (const_args[2]) {
1682 tgen_andi(s, TCG_TYPE_I32, args[0], args[2]);
1684 tcg_out_insn(s, RR, NR, args[0], args[2]);
1687 case INDEX_op_or_i32:
1688 if (const_args[2]) {
1689 tgen64_ori(s, args[0], args[2] & 0xffffffff);
1691 tcg_out_insn(s, RR, OR, args[0], args[2]);
1694 case INDEX_op_xor_i32:
1695 if (const_args[2]) {
1696 tgen64_xori(s, args[0], args[2] & 0xffffffff);
1698 tcg_out_insn(s, RR, XR, args[0], args[2]);
1702 case INDEX_op_neg_i32:
1703 tcg_out_insn(s, RR, LCR, args[0], args[1]);
1706 case INDEX_op_mul_i32:
1707 if (const_args[2]) {
1708 if ((int32_t)args[2] == (int16_t)args[2]) {
1709 tcg_out_insn(s, RI, MHI, args[0], args[2]);
1711 tcg_out_insn(s, RIL, MSFI, args[0], args[2]);
1714 tcg_out_insn(s, RRE, MSR, args[0], args[2]);
1718 case INDEX_op_div2_i32:
1719 tcg_out_insn(s, RR, DR, TCG_REG_R2, args[4]);
1721 case INDEX_op_divu2_i32:
1722 tcg_out_insn(s, RRE, DLR, TCG_REG_R2, args[4]);
1725 case INDEX_op_shl_i32:
1728 if (const_args[2]) {
1729 tcg_out_sh32(s, op, args[0], TCG_REG_NONE, args[2]);
1731 tcg_out_sh32(s, op, args[0], args[2], 0);
1734 case INDEX_op_shr_i32:
1737 case INDEX_op_sar_i32:
1741 case INDEX_op_rotl_i32:
1742 /* ??? Using tcg_out_sh64 here for the format; it is a 32-bit rol. */
1743 if (const_args[2]) {
1744 tcg_out_sh64(s, RSY_RLL, args[0], args[1], TCG_REG_NONE, args[2]);
1746 tcg_out_sh64(s, RSY_RLL, args[0], args[1], args[2], 0);
1749 case INDEX_op_rotr_i32:
1750 if (const_args[2]) {
1751 tcg_out_sh64(s, RSY_RLL, args[0], args[1],
1752 TCG_REG_NONE, (32 - args[2]) & 31);
1754 tcg_out_insn(s, RR, LCR, TCG_TMP0, args[2]);
1755 tcg_out_sh64(s, RSY_RLL, args[0], args[1], TCG_TMP0, 0);
1759 case INDEX_op_ext8s_i32:
1760 tgen_ext8s(s, TCG_TYPE_I32, args[0], args[1]);
1762 case INDEX_op_ext16s_i32:
1763 tgen_ext16s(s, TCG_TYPE_I32, args[0], args[1]);
1765 case INDEX_op_ext8u_i32:
1766 tgen_ext8u(s, TCG_TYPE_I32, args[0], args[1]);
1768 case INDEX_op_ext16u_i32:
1769 tgen_ext16u(s, TCG_TYPE_I32, args[0], args[1]);
1773 /* The TCG bswap definition requires bits 0-47 already be zero.
1774 Thus we don't need the G-type insns to implement bswap16_i64. */
1775 tcg_out_insn(s, RRE, LRVR, args[0], args[1]);
1776 tcg_out_sh32(s, RS_SRL, args[0], TCG_REG_NONE, 16);
1779 tcg_out_insn(s, RRE, LRVR, args[0], args[1]);
1782 case INDEX_op_add2_i32:
1783 /* ??? Make use of ALFI. */
1784 tcg_out_insn(s, RR, ALR, args[0], args[4]);
1785 tcg_out_insn(s, RRE, ALCR, args[1], args[5]);
1787 case INDEX_op_sub2_i32:
1788 /* ??? Make use of SLFI. */
1789 tcg_out_insn(s, RR, SLR, args[0], args[4]);
1790 tcg_out_insn(s, RRE, SLBR, args[1], args[5]);
1794 tgen_branch(s, S390_CC_ALWAYS, args[0]);
1797 case INDEX_op_brcond_i32:
1798 tgen_brcond(s, TCG_TYPE_I32, args[2], args[0],
1799 args[1], const_args[1], args[3]);
1801 case INDEX_op_setcond_i32:
1802 tgen_setcond(s, TCG_TYPE_I32, args[3], args[0], args[1],
1803 args[2], const_args[2]);
1805 case INDEX_op_movcond_i32:
1806 tgen_movcond(s, TCG_TYPE_I32, args[5], args[0], args[1],
1807 args[2], const_args[2], args[3]);
1810 case INDEX_op_qemu_ld_i32:
1811 /* ??? Technically we can use a non-extending instruction. */
1812 case INDEX_op_qemu_ld_i64:
1813 tcg_out_qemu_ld(s, args[0], args[1], args[2], args[3]);
1815 case INDEX_op_qemu_st_i32:
1816 case INDEX_op_qemu_st_i64:
1817 tcg_out_qemu_st(s, args[0], args[1], args[2], args[3]);
1820 case INDEX_op_ld16s_i64:
1821 tcg_out_mem(s, 0, RXY_LGH, args[0], args[1], TCG_REG_NONE, args[2]);
1823 case INDEX_op_ld32u_i64:
1824 tcg_out_mem(s, 0, RXY_LLGF, args[0], args[1], TCG_REG_NONE, args[2]);
1826 case INDEX_op_ld32s_i64:
1827 tcg_out_mem(s, 0, RXY_LGF, args[0], args[1], TCG_REG_NONE, args[2]);
1829 case INDEX_op_ld_i64:
1830 tcg_out_ld(s, TCG_TYPE_I64, args[0], args[1], args[2]);
1833 case INDEX_op_st32_i64:
1834 tcg_out_st(s, TCG_TYPE_I32, args[0], args[1], args[2]);
1836 case INDEX_op_st_i64:
1837 tcg_out_st(s, TCG_TYPE_I64, args[0], args[1], args[2]);
1840 case INDEX_op_add_i64:
1841 a0 = args[0], a1 = args[1], a2 = args[2];
1842 if (const_args[2]) {
1845 if (a2 == (int16_t)a2) {
1846 tcg_out_insn(s, RI, AGHI, a0, a2);
1849 if (facilities & FACILITY_EXT_IMM) {
1850 if (a2 == (int32_t)a2) {
1851 tcg_out_insn(s, RIL, AGFI, a0, a2);
1853 } else if (a2 == (uint32_t)a2) {
1854 tcg_out_insn(s, RIL, ALGFI, a0, a2);
1856 } else if (-a2 == (uint32_t)-a2) {
1857 tcg_out_insn(s, RIL, SLGFI, a0, -a2);
1862 tcg_out_mem(s, RX_LA, RXY_LAY, a0, a1, TCG_REG_NONE, a2);
1863 } else if (a0 == a1) {
1864 tcg_out_insn(s, RRE, AGR, a0, a2);
1866 tcg_out_insn(s, RX, LA, a0, a1, a2, 0);
1869 case INDEX_op_sub_i64:
1870 a0 = args[0], a1 = args[1], a2 = args[2];
1871 if (const_args[2]) {
1875 tcg_out_insn(s, RRE, SGR, args[0], args[2]);
1879 case INDEX_op_and_i64:
1880 if (const_args[2]) {
1881 tgen_andi(s, TCG_TYPE_I64, args[0], args[2]);
1883 tcg_out_insn(s, RRE, NGR, args[0], args[2]);
1886 case INDEX_op_or_i64:
1887 if (const_args[2]) {
1888 tgen64_ori(s, args[0], args[2]);
1890 tcg_out_insn(s, RRE, OGR, args[0], args[2]);
1893 case INDEX_op_xor_i64:
1894 if (const_args[2]) {
1895 tgen64_xori(s, args[0], args[2]);
1897 tcg_out_insn(s, RRE, XGR, args[0], args[2]);
1901 case INDEX_op_neg_i64:
1902 tcg_out_insn(s, RRE, LCGR, args[0], args[1]);
1904 case INDEX_op_bswap64_i64:
1905 tcg_out_insn(s, RRE, LRVGR, args[0], args[1]);
1908 case INDEX_op_mul_i64:
1909 if (const_args[2]) {
1910 if (args[2] == (int16_t)args[2]) {
1911 tcg_out_insn(s, RI, MGHI, args[0], args[2]);
1913 tcg_out_insn(s, RIL, MSGFI, args[0], args[2]);
1916 tcg_out_insn(s, RRE, MSGR, args[0], args[2]);
1920 case INDEX_op_div2_i64:
1921 /* ??? We get an unnecessary sign-extension of the dividend
1922 into R3 with this definition, but as we do in fact always
1923 produce both quotient and remainder using INDEX_op_div_i64
1924 instead requires jumping through even more hoops. */
1925 tcg_out_insn(s, RRE, DSGR, TCG_REG_R2, args[4]);
1927 case INDEX_op_divu2_i64:
1928 tcg_out_insn(s, RRE, DLGR, TCG_REG_R2, args[4]);
1930 case INDEX_op_mulu2_i64:
1931 tcg_out_insn(s, RRE, MLGR, TCG_REG_R2, args[3]);
1934 case INDEX_op_shl_i64:
1937 if (const_args[2]) {
1938 tcg_out_sh64(s, op, args[0], args[1], TCG_REG_NONE, args[2]);
1940 tcg_out_sh64(s, op, args[0], args[1], args[2], 0);
1943 case INDEX_op_shr_i64:
1946 case INDEX_op_sar_i64:
1950 case INDEX_op_rotl_i64:
1951 if (const_args[2]) {
1952 tcg_out_sh64(s, RSY_RLLG, args[0], args[1],
1953 TCG_REG_NONE, args[2]);
1955 tcg_out_sh64(s, RSY_RLLG, args[0], args[1], args[2], 0);
1958 case INDEX_op_rotr_i64:
1959 if (const_args[2]) {
1960 tcg_out_sh64(s, RSY_RLLG, args[0], args[1],
1961 TCG_REG_NONE, (64 - args[2]) & 63);
1963 /* We can use the smaller 32-bit negate because only the
1964 low 6 bits are examined for the rotate. */
1965 tcg_out_insn(s, RR, LCR, TCG_TMP0, args[2]);
1966 tcg_out_sh64(s, RSY_RLLG, args[0], args[1], TCG_TMP0, 0);
1970 case INDEX_op_ext8s_i64:
1971 tgen_ext8s(s, TCG_TYPE_I64, args[0], args[1]);
1973 case INDEX_op_ext16s_i64:
1974 tgen_ext16s(s, TCG_TYPE_I64, args[0], args[1]);
1976 case INDEX_op_ext32s_i64:
1977 tgen_ext32s(s, args[0], args[1]);
1979 case INDEX_op_ext8u_i64:
1980 tgen_ext8u(s, TCG_TYPE_I64, args[0], args[1]);
1982 case INDEX_op_ext16u_i64:
1983 tgen_ext16u(s, TCG_TYPE_I64, args[0], args[1]);
1985 case INDEX_op_ext32u_i64:
1986 tgen_ext32u(s, args[0], args[1]);
1989 case INDEX_op_add2_i64:
1990 /* ??? Make use of ALGFI and SLGFI. */
1991 tcg_out_insn(s, RRE, ALGR, args[0], args[4]);
1992 tcg_out_insn(s, RRE, ALCGR, args[1], args[5]);
1994 case INDEX_op_sub2_i64:
1995 /* ??? Make use of ALGFI and SLGFI. */
1996 tcg_out_insn(s, RRE, SLGR, args[0], args[4]);
1997 tcg_out_insn(s, RRE, SLBGR, args[1], args[5]);
2000 case INDEX_op_brcond_i64:
2001 tgen_brcond(s, TCG_TYPE_I64, args[2], args[0],
2002 args[1], const_args[1], args[3]);
2004 case INDEX_op_setcond_i64:
2005 tgen_setcond(s, TCG_TYPE_I64, args[3], args[0], args[1],
2006 args[2], const_args[2]);
2008 case INDEX_op_movcond_i64:
2009 tgen_movcond(s, TCG_TYPE_I64, args[5], args[0], args[1],
2010 args[2], const_args[2], args[3]);
2014 tgen_deposit(s, args[0], args[2], args[3], args[4]);
2017 case INDEX_op_mov_i32: /* Always emitted via tcg_out_mov. */
2018 case INDEX_op_mov_i64:
2019 case INDEX_op_movi_i32: /* Always emitted via tcg_out_movi. */
2020 case INDEX_op_movi_i64:
2021 case INDEX_op_call: /* Always emitted via tcg_out_call. */
2027 static const TCGTargetOpDef s390_op_defs[] = {
2028 { INDEX_op_exit_tb, { } },
2029 { INDEX_op_goto_tb, { } },
2030 { INDEX_op_br, { } },
2032 { INDEX_op_ld8u_i32, { "r", "r" } },
2033 { INDEX_op_ld8s_i32, { "r", "r" } },
2034 { INDEX_op_ld16u_i32, { "r", "r" } },
2035 { INDEX_op_ld16s_i32, { "r", "r" } },
2036 { INDEX_op_ld_i32, { "r", "r" } },
2037 { INDEX_op_st8_i32, { "r", "r" } },
2038 { INDEX_op_st16_i32, { "r", "r" } },
2039 { INDEX_op_st_i32, { "r", "r" } },
2041 { INDEX_op_add_i32, { "r", "r", "ri" } },
2042 { INDEX_op_sub_i32, { "r", "0", "ri" } },
2043 { INDEX_op_mul_i32, { "r", "0", "rK" } },
2045 { INDEX_op_div2_i32, { "b", "a", "0", "1", "r" } },
2046 { INDEX_op_divu2_i32, { "b", "a", "0", "1", "r" } },
2048 { INDEX_op_and_i32, { "r", "0", "ri" } },
2049 { INDEX_op_or_i32, { "r", "0", "rO" } },
2050 { INDEX_op_xor_i32, { "r", "0", "rX" } },
2052 { INDEX_op_neg_i32, { "r", "r" } },
2054 { INDEX_op_shl_i32, { "r", "0", "Ri" } },
2055 { INDEX_op_shr_i32, { "r", "0", "Ri" } },
2056 { INDEX_op_sar_i32, { "r", "0", "Ri" } },
2058 { INDEX_op_rotl_i32, { "r", "r", "Ri" } },
2059 { INDEX_op_rotr_i32, { "r", "r", "Ri" } },
2061 { INDEX_op_ext8s_i32, { "r", "r" } },
2062 { INDEX_op_ext8u_i32, { "r", "r" } },
2063 { INDEX_op_ext16s_i32, { "r", "r" } },
2064 { INDEX_op_ext16u_i32, { "r", "r" } },
2066 { INDEX_op_bswap16_i32, { "r", "r" } },
2067 { INDEX_op_bswap32_i32, { "r", "r" } },
2069 { INDEX_op_add2_i32, { "r", "r", "0", "1", "r", "r" } },
2070 { INDEX_op_sub2_i32, { "r", "r", "0", "1", "r", "r" } },
2072 { INDEX_op_brcond_i32, { "r", "rC" } },
2073 { INDEX_op_setcond_i32, { "r", "r", "rC" } },
2074 { INDEX_op_movcond_i32, { "r", "r", "rC", "r", "0" } },
2075 { INDEX_op_deposit_i32, { "r", "0", "r" } },
2077 { INDEX_op_qemu_ld_i32, { "r", "L" } },
2078 { INDEX_op_qemu_ld_i64, { "r", "L" } },
2079 { INDEX_op_qemu_st_i32, { "L", "L" } },
2080 { INDEX_op_qemu_st_i64, { "L", "L" } },
2082 { INDEX_op_ld8u_i64, { "r", "r" } },
2083 { INDEX_op_ld8s_i64, { "r", "r" } },
2084 { INDEX_op_ld16u_i64, { "r", "r" } },
2085 { INDEX_op_ld16s_i64, { "r", "r" } },
2086 { INDEX_op_ld32u_i64, { "r", "r" } },
2087 { INDEX_op_ld32s_i64, { "r", "r" } },
2088 { INDEX_op_ld_i64, { "r", "r" } },
2090 { INDEX_op_st8_i64, { "r", "r" } },
2091 { INDEX_op_st16_i64, { "r", "r" } },
2092 { INDEX_op_st32_i64, { "r", "r" } },
2093 { INDEX_op_st_i64, { "r", "r" } },
2095 { INDEX_op_add_i64, { "r", "r", "ri" } },
2096 { INDEX_op_sub_i64, { "r", "0", "ri" } },
2097 { INDEX_op_mul_i64, { "r", "0", "rK" } },
2099 { INDEX_op_div2_i64, { "b", "a", "0", "1", "r" } },
2100 { INDEX_op_divu2_i64, { "b", "a", "0", "1", "r" } },
2101 { INDEX_op_mulu2_i64, { "b", "a", "0", "r" } },
2103 { INDEX_op_and_i64, { "r", "0", "ri" } },
2104 { INDEX_op_or_i64, { "r", "0", "rO" } },
2105 { INDEX_op_xor_i64, { "r", "0", "rX" } },
2107 { INDEX_op_neg_i64, { "r", "r" } },
2109 { INDEX_op_shl_i64, { "r", "r", "Ri" } },
2110 { INDEX_op_shr_i64, { "r", "r", "Ri" } },
2111 { INDEX_op_sar_i64, { "r", "r", "Ri" } },
2113 { INDEX_op_rotl_i64, { "r", "r", "Ri" } },
2114 { INDEX_op_rotr_i64, { "r", "r", "Ri" } },
2116 { INDEX_op_ext8s_i64, { "r", "r" } },
2117 { INDEX_op_ext8u_i64, { "r", "r" } },
2118 { INDEX_op_ext16s_i64, { "r", "r" } },
2119 { INDEX_op_ext16u_i64, { "r", "r" } },
2120 { INDEX_op_ext32s_i64, { "r", "r" } },
2121 { INDEX_op_ext32u_i64, { "r", "r" } },
2123 { INDEX_op_bswap16_i64, { "r", "r" } },
2124 { INDEX_op_bswap32_i64, { "r", "r" } },
2125 { INDEX_op_bswap64_i64, { "r", "r" } },
2127 { INDEX_op_add2_i64, { "r", "r", "0", "1", "r", "r" } },
2128 { INDEX_op_sub2_i64, { "r", "r", "0", "1", "r", "r" } },
2130 { INDEX_op_brcond_i64, { "r", "rC" } },
2131 { INDEX_op_setcond_i64, { "r", "r", "rC" } },
2132 { INDEX_op_movcond_i64, { "r", "r", "rC", "r", "0" } },
2133 { INDEX_op_deposit_i64, { "r", "0", "r" } },
2138 static void query_facilities(void)
2140 unsigned long hwcap = qemu_getauxval(AT_HWCAP);
2142 /* Is STORE FACILITY LIST EXTENDED available? Honestly, I believe this
2143 is present on all 64-bit systems, but let's check for it anyway. */
2144 if (hwcap & HWCAP_S390_STFLE) {
2145 register int r0 __asm__("0");
2146 register void *r1 __asm__("1");
2150 asm volatile(".word 0xb2b0,0x1000"
2151 : "=r"(r0) : "0"(0), "r"(r1) : "memory", "cc");
2155 static void tcg_target_init(TCGContext *s)
2159 tcg_regset_set32(tcg_target_available_regs[TCG_TYPE_I32], 0, 0xffff);
2160 tcg_regset_set32(tcg_target_available_regs[TCG_TYPE_I64], 0, 0xffff);
2162 tcg_regset_clear(tcg_target_call_clobber_regs);
2163 tcg_regset_set_reg(tcg_target_call_clobber_regs, TCG_REG_R0);
2164 tcg_regset_set_reg(tcg_target_call_clobber_regs, TCG_REG_R1);
2165 tcg_regset_set_reg(tcg_target_call_clobber_regs, TCG_REG_R2);
2166 tcg_regset_set_reg(tcg_target_call_clobber_regs, TCG_REG_R3);
2167 tcg_regset_set_reg(tcg_target_call_clobber_regs, TCG_REG_R4);
2168 tcg_regset_set_reg(tcg_target_call_clobber_regs, TCG_REG_R5);
2169 /* The r6 register is technically call-saved, but it's also a parameter
2170 register, so it can get killed by setup for the qemu_st helper. */
2171 tcg_regset_set_reg(tcg_target_call_clobber_regs, TCG_REG_R6);
2172 /* The return register can be considered call-clobbered. */
2173 tcg_regset_set_reg(tcg_target_call_clobber_regs, TCG_REG_R14);
2175 tcg_regset_clear(s->reserved_regs);
2176 tcg_regset_set_reg(s->reserved_regs, TCG_TMP0);
2177 /* XXX many insns can't be used with R0, so we better avoid it for now */
2178 tcg_regset_set_reg(s->reserved_regs, TCG_REG_R0);
2179 tcg_regset_set_reg(s->reserved_regs, TCG_REG_CALL_STACK);
2181 tcg_add_target_add_op_defs(s390_op_defs);
2184 #define FRAME_SIZE ((int)(TCG_TARGET_CALL_STACK_OFFSET \
2185 + TCG_STATIC_CALL_ARGS_SIZE \
2186 + CPU_TEMP_BUF_NLONGS * sizeof(long)))
2188 static void tcg_target_qemu_prologue(TCGContext *s)
2190 /* stmg %r6,%r15,48(%r15) (save registers) */
2191 tcg_out_insn(s, RXY, STMG, TCG_REG_R6, TCG_REG_R15, TCG_REG_R15, 48);
2193 /* aghi %r15,-frame_size */
2194 tcg_out_insn(s, RI, AGHI, TCG_REG_R15, -FRAME_SIZE);
2196 tcg_set_frame(s, TCG_REG_CALL_STACK,
2197 TCG_STATIC_CALL_ARGS_SIZE + TCG_TARGET_CALL_STACK_OFFSET,
2198 CPU_TEMP_BUF_NLONGS * sizeof(long));
2200 if (GUEST_BASE >= 0x80000) {
2201 tcg_out_movi(s, TCG_TYPE_PTR, TCG_GUEST_BASE_REG, GUEST_BASE);
2202 tcg_regset_set_reg(s->reserved_regs, TCG_GUEST_BASE_REG);
2205 tcg_out_mov(s, TCG_TYPE_PTR, TCG_AREG0, tcg_target_call_iarg_regs[0]);
2206 /* br %r3 (go to TB) */
2207 tcg_out_insn(s, RR, BCR, S390_CC_ALWAYS, tcg_target_call_iarg_regs[1]);
2209 tb_ret_addr = s->code_ptr;
2211 /* lmg %r6,%r15,fs+48(%r15) (restore registers) */
2212 tcg_out_insn(s, RXY, LMG, TCG_REG_R6, TCG_REG_R15, TCG_REG_R15,
2215 /* br %r14 (return) */
2216 tcg_out_insn(s, RR, BCR, S390_CC_ALWAYS, TCG_REG_R14);
2221 DebugFrameFDEHeader fde;
2222 uint8_t fde_def_cfa[4];
2223 uint8_t fde_reg_ofs[18];
2226 /* We're expecting a 2 byte uleb128 encoded value. */
2227 QEMU_BUILD_BUG_ON(FRAME_SIZE >= (1 << 14));
2229 #define ELF_HOST_MACHINE EM_S390
2231 static DebugFrame debug_frame = {
2232 .cie.len = sizeof(DebugFrameCIE)-4, /* length after .len member */
2235 .cie.code_align = 1,
2236 .cie.data_align = 8, /* sleb128 8 */
2237 .cie.return_column = TCG_REG_R14,
2239 /* Total FDE size does not include the "len" member. */
2240 .fde.len = sizeof(DebugFrame) - offsetof(DebugFrame, fde.cie_offset),
2243 12, TCG_REG_CALL_STACK, /* DW_CFA_def_cfa %r15, ... */
2244 (FRAME_SIZE & 0x7f) | 0x80, /* ... uleb128 FRAME_SIZE */
2248 0x86, 6, /* DW_CFA_offset, %r6, 48 */
2249 0x87, 7, /* DW_CFA_offset, %r7, 56 */
2250 0x88, 8, /* DW_CFA_offset, %r8, 64 */
2251 0x89, 9, /* DW_CFA_offset, %r92, 72 */
2252 0x8a, 10, /* DW_CFA_offset, %r10, 80 */
2253 0x8b, 11, /* DW_CFA_offset, %r11, 88 */
2254 0x8c, 12, /* DW_CFA_offset, %r12, 96 */
2255 0x8d, 13, /* DW_CFA_offset, %r13, 104 */
2256 0x8e, 14, /* DW_CFA_offset, %r14, 112 */
2260 void tcg_register_jit(void *buf, size_t buf_size)
2262 debug_frame.fde.func_start = (uintptr_t)buf;
2263 debug_frame.fde.func_len = buf_size;
2265 tcg_register_jit_int(buf, buf_size, &debug_frame, sizeof(debug_frame));