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 /* ??? The translation blocks produced by TCG are generally small enough to
28 be entirely reachable with a 16-bit displacement. Leaving the option for
29 a 32-bit displacement here Just In Case. */
30 #define USE_LONG_BRANCHES 0
32 #define TCG_CT_CONST_32 0x0100
33 #define TCG_CT_CONST_NEG 0x0200
34 #define TCG_CT_CONST_ADDI 0x0400
35 #define TCG_CT_CONST_MULI 0x0800
36 #define TCG_CT_CONST_ANDI 0x1000
37 #define TCG_CT_CONST_ORI 0x2000
38 #define TCG_CT_CONST_XORI 0x4000
39 #define TCG_CT_CONST_CMPI 0x8000
41 /* Several places within the instruction set 0 means "no register"
42 rather than TCG_REG_R0. */
43 #define TCG_REG_NONE 0
45 /* A scratch register that may be be used throughout the backend. */
46 #define TCG_TMP0 TCG_REG_R14
48 #ifdef CONFIG_USE_GUEST_BASE
49 #define TCG_GUEST_BASE_REG TCG_REG_R13
51 #define TCG_GUEST_BASE_REG TCG_REG_R0
59 /* All of the following instructions are prefixed with their instruction
60 format, and are defined as 8- or 16-bit quantities, even when the two
61 halves of the 16-bit quantity may appear 32 bits apart in the insn.
62 This makes it easy to copy the values from the tables in Appendix B. */
63 typedef enum S390Opcode
{
207 #define LD_SIGNED 0x04
208 #define LD_UINT8 0x00
209 #define LD_INT8 (LD_UINT8 | LD_SIGNED)
210 #define LD_UINT16 0x01
211 #define LD_INT16 (LD_UINT16 | LD_SIGNED)
212 #define LD_UINT32 0x02
213 #define LD_INT32 (LD_UINT32 | LD_SIGNED)
214 #define LD_UINT64 0x03
215 #define LD_INT64 (LD_UINT64 | LD_SIGNED)
218 static const char * const tcg_target_reg_names
[TCG_TARGET_NB_REGS
] = {
219 "%r0", "%r1", "%r2", "%r3", "%r4", "%r5", "%r6", "%r7",
220 "%r8", "%r9", "%r10" "%r11" "%r12" "%r13" "%r14" "%r15"
224 /* Since R6 is a potential argument register, choose it last of the
225 call-saved registers. Likewise prefer the call-clobbered registers
226 in reverse order to maximize the chance of avoiding the arguments. */
227 static const int tcg_target_reg_alloc_order
[] = {
245 static const int tcg_target_call_iarg_regs
[] = {
253 static const int tcg_target_call_oarg_regs
[] = {
255 #if TCG_TARGET_REG_BITS == 32
264 #define S390_CC_NE (S390_CC_LT | S390_CC_GT)
265 #define S390_CC_LE (S390_CC_LT | S390_CC_EQ)
266 #define S390_CC_GE (S390_CC_GT | S390_CC_EQ)
267 #define S390_CC_NEVER 0
268 #define S390_CC_ALWAYS 15
270 /* Condition codes that result from a COMPARE and COMPARE LOGICAL. */
271 static const uint8_t tcg_cond_to_s390_cond
[10] = {
272 [TCG_COND_EQ
] = S390_CC_EQ
,
273 [TCG_COND_NE
] = S390_CC_NE
,
274 [TCG_COND_LT
] = S390_CC_LT
,
275 [TCG_COND_LE
] = S390_CC_LE
,
276 [TCG_COND_GT
] = S390_CC_GT
,
277 [TCG_COND_GE
] = S390_CC_GE
,
278 [TCG_COND_LTU
] = S390_CC_LT
,
279 [TCG_COND_LEU
] = S390_CC_LE
,
280 [TCG_COND_GTU
] = S390_CC_GT
,
281 [TCG_COND_GEU
] = S390_CC_GE
,
284 /* Condition codes that result from a LOAD AND TEST. Here, we have no
285 unsigned instruction variation, however since the test is vs zero we
286 can re-map the outcomes appropriately. */
287 static const uint8_t tcg_cond_to_ltr_cond
[10] = {
288 [TCG_COND_EQ
] = S390_CC_EQ
,
289 [TCG_COND_NE
] = S390_CC_NE
,
290 [TCG_COND_LT
] = S390_CC_LT
,
291 [TCG_COND_LE
] = S390_CC_LE
,
292 [TCG_COND_GT
] = S390_CC_GT
,
293 [TCG_COND_GE
] = S390_CC_GE
,
294 [TCG_COND_LTU
] = S390_CC_NEVER
,
295 [TCG_COND_LEU
] = S390_CC_EQ
,
296 [TCG_COND_GTU
] = S390_CC_NE
,
297 [TCG_COND_GEU
] = S390_CC_ALWAYS
,
300 #ifdef CONFIG_SOFTMMU
302 #include "../../softmmu_defs.h"
304 #ifdef CONFIG_TCG_PASS_AREG0
305 /* helper signature: helper_ld_mmu(CPUState *env, target_ulong addr,
307 static const void * const qemu_ld_helpers
[4] = {
314 /* helper signature: helper_st_mmu(CPUState *env, target_ulong addr,
315 uintxx_t val, int mmu_idx) */
316 static const void * const qemu_st_helpers
[4] = {
323 /* legacy helper signature: __ld_mmu(target_ulong addr, int
325 static void *qemu_ld_helpers
[4] = {
332 /* legacy helper signature: __st_mmu(target_ulong addr, uintxx_t val,
334 static void *qemu_st_helpers
[4] = {
343 static uint8_t *tb_ret_addr
;
345 /* A list of relevant facilities used by this translator. Some of these
346 are required for proper operation, and these are checked at startup. */
348 #define FACILITY_ZARCH_ACTIVE (1ULL << (63 - 2))
349 #define FACILITY_LONG_DISP (1ULL << (63 - 18))
350 #define FACILITY_EXT_IMM (1ULL << (63 - 21))
351 #define FACILITY_GEN_INST_EXT (1ULL << (63 - 34))
353 static uint64_t facilities
;
355 static void patch_reloc(uint8_t *code_ptr
, int type
,
356 tcg_target_long value
, tcg_target_long addend
)
358 tcg_target_long code_ptr_tl
= (tcg_target_long
)code_ptr
;
359 tcg_target_long pcrel2
;
361 /* ??? Not the usual definition of "addend". */
362 pcrel2
= (value
- (code_ptr_tl
+ addend
)) >> 1;
366 assert(pcrel2
== (int16_t)pcrel2
);
367 *(int16_t *)code_ptr
= pcrel2
;
370 assert(pcrel2
== (int32_t)pcrel2
);
371 *(int32_t *)code_ptr
= pcrel2
;
379 static int tcg_target_get_call_iarg_regs_count(int flags
)
381 return sizeof(tcg_target_call_iarg_regs
) / sizeof(int);
384 /* parse target specific constraints */
385 static int target_parse_constraint(TCGArgConstraint
*ct
, const char **pct_str
)
387 const char *ct_str
= *pct_str
;
390 case 'r': /* all registers */
391 ct
->ct
|= TCG_CT_REG
;
392 tcg_regset_set32(ct
->u
.regs
, 0, 0xffff);
394 case 'R': /* not R0 */
395 ct
->ct
|= TCG_CT_REG
;
396 tcg_regset_set32(ct
->u
.regs
, 0, 0xffff);
397 tcg_regset_reset_reg(ct
->u
.regs
, TCG_REG_R0
);
399 case 'L': /* qemu_ld/st constraint */
400 ct
->ct
|= TCG_CT_REG
;
401 tcg_regset_set32(ct
->u
.regs
, 0, 0xffff);
402 tcg_regset_reset_reg (ct
->u
.regs
, TCG_REG_R2
);
403 tcg_regset_reset_reg (ct
->u
.regs
, TCG_REG_R3
);
405 case 'a': /* force R2 for division */
406 ct
->ct
|= TCG_CT_REG
;
407 tcg_regset_clear(ct
->u
.regs
);
408 tcg_regset_set_reg(ct
->u
.regs
, TCG_REG_R2
);
410 case 'b': /* force R3 for division */
411 ct
->ct
|= TCG_CT_REG
;
412 tcg_regset_clear(ct
->u
.regs
);
413 tcg_regset_set_reg(ct
->u
.regs
, TCG_REG_R3
);
415 case 'N': /* force immediate negate */
416 ct
->ct
|= TCG_CT_CONST_NEG
;
418 case 'W': /* force 32-bit ("word") immediate */
419 ct
->ct
|= TCG_CT_CONST_32
;
422 ct
->ct
|= TCG_CT_CONST_ADDI
;
425 ct
->ct
|= TCG_CT_CONST_MULI
;
428 ct
->ct
|= TCG_CT_CONST_ANDI
;
431 ct
->ct
|= TCG_CT_CONST_ORI
;
434 ct
->ct
|= TCG_CT_CONST_XORI
;
437 ct
->ct
|= TCG_CT_CONST_CMPI
;
448 /* Immediates to be used with logical AND. This is an optimization only,
449 since a full 64-bit immediate AND can always be performed with 4 sequential
450 NI[LH][LH] instructions. What we're looking for is immediates that we
451 can load efficiently, and the immediate load plus the reg-reg AND is
452 smaller than the sequential NI's. */
454 static int tcg_match_andi(int ct
, tcg_target_ulong val
)
458 if (facilities
& FACILITY_EXT_IMM
) {
459 if (ct
& TCG_CT_CONST_32
) {
460 /* All 32-bit ANDs can be performed with 1 48-bit insn. */
464 /* Zero-extensions. */
465 if (val
== 0xff || val
== 0xffff || val
== 0xffffffff) {
469 if (ct
& TCG_CT_CONST_32
) {
471 } else if (val
== 0xffffffff) {
476 /* Try all 32-bit insns that can perform it in one go. */
477 for (i
= 0; i
< 4; i
++) {
478 tcg_target_ulong mask
= ~(0xffffull
<< i
*16);
479 if ((val
& mask
) == mask
) {
484 /* Look for 16-bit values performing the mask. These are better
485 to load with LLI[LH][LH]. */
486 for (i
= 0; i
< 4; i
++) {
487 tcg_target_ulong mask
= 0xffffull
<< i
*16;
488 if ((val
& mask
) == val
) {
493 /* Look for 32-bit values performing the 64-bit mask. These
494 are better to load with LLI[LH]F, or if extended immediates
495 not available, with a pair of LLI insns. */
496 if ((ct
& TCG_CT_CONST_32
) == 0) {
497 if (val
<= 0xffffffff || (val
& 0xffffffff) == 0) {
505 /* Immediates to be used with logical OR. This is an optimization only,
506 since a full 64-bit immediate OR can always be performed with 4 sequential
507 OI[LH][LH] instructions. What we're looking for is immediates that we
508 can load efficiently, and the immediate load plus the reg-reg OR is
509 smaller than the sequential OI's. */
511 static int tcg_match_ori(int ct
, tcg_target_long val
)
513 if (facilities
& FACILITY_EXT_IMM
) {
514 if (ct
& TCG_CT_CONST_32
) {
515 /* All 32-bit ORs can be performed with 1 48-bit insn. */
520 /* Look for negative values. These are best to load with LGHI. */
522 if (val
== (int16_t)val
) {
525 if (facilities
& FACILITY_EXT_IMM
) {
526 if (val
== (int32_t)val
) {
535 /* Immediates to be used with logical XOR. This is almost, but not quite,
536 only an optimization. XOR with immediate is only supported with the
537 extended-immediate facility. That said, there are a few patterns for
538 which it is better to load the value into a register first. */
540 static int tcg_match_xori(int ct
, tcg_target_long val
)
542 if ((facilities
& FACILITY_EXT_IMM
) == 0) {
546 if (ct
& TCG_CT_CONST_32
) {
547 /* All 32-bit XORs can be performed with 1 48-bit insn. */
551 /* Look for negative values. These are best to load with LGHI. */
552 if (val
< 0 && val
== (int32_t)val
) {
559 /* Imediates to be used with comparisons. */
561 static int tcg_match_cmpi(int ct
, tcg_target_long val
)
563 if (facilities
& FACILITY_EXT_IMM
) {
564 /* The COMPARE IMMEDIATE instruction is available. */
565 if (ct
& TCG_CT_CONST_32
) {
566 /* We have a 32-bit immediate and can compare against anything. */
569 /* ??? We have no insight here into whether the comparison is
570 signed or unsigned. The COMPARE IMMEDIATE insn uses a 32-bit
571 signed immediate, and the COMPARE LOGICAL IMMEDIATE insn uses
572 a 32-bit unsigned immediate. If we were to use the (semi)
573 obvious "val == (int32_t)val" we would be enabling unsigned
574 comparisons vs very large numbers. The only solution is to
575 take the intersection of the ranges. */
576 /* ??? Another possible solution is to simply lie and allow all
577 constants here and force the out-of-range values into a temp
578 register in tgen_cmp when we have knowledge of the actual
579 comparison code in use. */
580 return val
>= 0 && val
<= 0x7fffffff;
583 /* Only the LOAD AND TEST instruction is available. */
588 /* Test if a constant matches the constraint. */
589 static int tcg_target_const_match(tcg_target_long val
,
590 const TCGArgConstraint
*arg_ct
)
594 if (ct
& TCG_CT_CONST
) {
598 /* Handle the modifiers. */
599 if (ct
& TCG_CT_CONST_NEG
) {
602 if (ct
& TCG_CT_CONST_32
) {
606 /* The following are mutually exclusive. */
607 if (ct
& TCG_CT_CONST_ADDI
) {
608 /* Immediates that may be used with add. If we have the
609 extended-immediates facility then we have ADD IMMEDIATE
610 with signed and unsigned 32-bit, otherwise we have only
611 ADD HALFWORD IMMEDIATE with a signed 16-bit. */
612 if (facilities
& FACILITY_EXT_IMM
) {
613 return val
== (int32_t)val
|| val
== (uint32_t)val
;
615 return val
== (int16_t)val
;
617 } else if (ct
& TCG_CT_CONST_MULI
) {
618 /* Immediates that may be used with multiply. If we have the
619 general-instruction-extensions, then we have MULTIPLY SINGLE
620 IMMEDIATE with a signed 32-bit, otherwise we have only
621 MULTIPLY HALFWORD IMMEDIATE, with a signed 16-bit. */
622 if (facilities
& FACILITY_GEN_INST_EXT
) {
623 return val
== (int32_t)val
;
625 return val
== (int16_t)val
;
627 } else if (ct
& TCG_CT_CONST_ANDI
) {
628 return tcg_match_andi(ct
, val
);
629 } else if (ct
& TCG_CT_CONST_ORI
) {
630 return tcg_match_ori(ct
, val
);
631 } else if (ct
& TCG_CT_CONST_XORI
) {
632 return tcg_match_xori(ct
, val
);
633 } else if (ct
& TCG_CT_CONST_CMPI
) {
634 return tcg_match_cmpi(ct
, val
);
640 /* Emit instructions according to the given instruction format. */
642 static void tcg_out_insn_RR(TCGContext
*s
, S390Opcode op
, TCGReg r1
, TCGReg r2
)
644 tcg_out16(s
, (op
<< 8) | (r1
<< 4) | r2
);
647 static void tcg_out_insn_RRE(TCGContext
*s
, S390Opcode op
,
648 TCGReg r1
, TCGReg r2
)
650 tcg_out32(s
, (op
<< 16) | (r1
<< 4) | r2
);
653 static void tcg_out_insn_RI(TCGContext
*s
, S390Opcode op
, TCGReg r1
, int i2
)
655 tcg_out32(s
, (op
<< 16) | (r1
<< 20) | (i2
& 0xffff));
658 static void tcg_out_insn_RIL(TCGContext
*s
, S390Opcode op
, TCGReg r1
, int i2
)
660 tcg_out16(s
, op
| (r1
<< 4));
664 static void tcg_out_insn_RS(TCGContext
*s
, S390Opcode op
, TCGReg r1
,
665 TCGReg b2
, TCGReg r3
, int disp
)
667 tcg_out32(s
, (op
<< 24) | (r1
<< 20) | (r3
<< 16) | (b2
<< 12)
671 static void tcg_out_insn_RSY(TCGContext
*s
, S390Opcode op
, TCGReg r1
,
672 TCGReg b2
, TCGReg r3
, int disp
)
674 tcg_out16(s
, (op
& 0xff00) | (r1
<< 4) | r3
);
675 tcg_out32(s
, (op
& 0xff) | (b2
<< 28)
676 | ((disp
& 0xfff) << 16) | ((disp
& 0xff000) >> 4));
679 #define tcg_out_insn_RX tcg_out_insn_RS
680 #define tcg_out_insn_RXY tcg_out_insn_RSY
682 /* Emit an opcode with "type-checking" of the format. */
683 #define tcg_out_insn(S, FMT, OP, ...) \
684 glue(tcg_out_insn_,FMT)(S, glue(glue(FMT,_),OP), ## __VA_ARGS__)
687 /* emit 64-bit shifts */
688 static void tcg_out_sh64(TCGContext
* s
, S390Opcode op
, TCGReg dest
,
689 TCGReg src
, TCGReg sh_reg
, int sh_imm
)
691 tcg_out_insn_RSY(s
, op
, dest
, sh_reg
, src
, sh_imm
);
694 /* emit 32-bit shifts */
695 static void tcg_out_sh32(TCGContext
* s
, S390Opcode op
, TCGReg dest
,
696 TCGReg sh_reg
, int sh_imm
)
698 tcg_out_insn_RS(s
, op
, dest
, sh_reg
, 0, sh_imm
);
701 static void tcg_out_mov(TCGContext
*s
, TCGType type
, TCGReg dst
, TCGReg src
)
704 if (type
== TCG_TYPE_I32
) {
705 tcg_out_insn(s
, RR
, LR
, dst
, src
);
707 tcg_out_insn(s
, RRE
, LGR
, dst
, src
);
712 /* load a register with an immediate value */
713 static void tcg_out_movi(TCGContext
*s
, TCGType type
,
714 TCGReg ret
, tcg_target_long sval
)
716 static const S390Opcode lli_insns
[4] = {
717 RI_LLILL
, RI_LLILH
, RI_LLIHL
, RI_LLIHH
720 tcg_target_ulong uval
= sval
;
723 if (type
== TCG_TYPE_I32
) {
724 uval
= (uint32_t)sval
;
725 sval
= (int32_t)sval
;
728 /* Try all 32-bit insns that can load it in one go. */
729 if (sval
>= -0x8000 && sval
< 0x8000) {
730 tcg_out_insn(s
, RI
, LGHI
, ret
, sval
);
734 for (i
= 0; i
< 4; i
++) {
735 tcg_target_long mask
= 0xffffull
<< i
*16;
736 if ((uval
& mask
) == uval
) {
737 tcg_out_insn_RI(s
, lli_insns
[i
], ret
, uval
>> i
*16);
742 /* Try all 48-bit insns that can load it in one go. */
743 if (facilities
& FACILITY_EXT_IMM
) {
744 if (sval
== (int32_t)sval
) {
745 tcg_out_insn(s
, RIL
, LGFI
, ret
, sval
);
748 if (uval
<= 0xffffffff) {
749 tcg_out_insn(s
, RIL
, LLILF
, ret
, uval
);
752 if ((uval
& 0xffffffff) == 0) {
753 tcg_out_insn(s
, RIL
, LLIHF
, ret
, uval
>> 31 >> 1);
758 /* Try for PC-relative address load. */
759 if ((sval
& 1) == 0) {
760 intptr_t off
= (sval
- (intptr_t)s
->code_ptr
) >> 1;
761 if (off
== (int32_t)off
) {
762 tcg_out_insn(s
, RIL
, LARL
, ret
, off
);
767 /* If extended immediates are not present, then we may have to issue
768 several instructions to load the low 32 bits. */
769 if (!(facilities
& FACILITY_EXT_IMM
)) {
770 /* A 32-bit unsigned value can be loaded in 2 insns. And given
771 that the lli_insns loop above did not succeed, we know that
772 both insns are required. */
773 if (uval
<= 0xffffffff) {
774 tcg_out_insn(s
, RI
, LLILL
, ret
, uval
);
775 tcg_out_insn(s
, RI
, IILH
, ret
, uval
>> 16);
779 /* If all high bits are set, the value can be loaded in 2 or 3 insns.
780 We first want to make sure that all the high bits get set. With
781 luck the low 16-bits can be considered negative to perform that for
782 free, otherwise we load an explicit -1. */
783 if (sval
>> 31 >> 1 == -1) {
785 tcg_out_insn(s
, RI
, LGHI
, ret
, uval
);
787 tcg_out_insn(s
, RI
, LGHI
, ret
, -1);
788 tcg_out_insn(s
, RI
, IILL
, ret
, uval
);
790 tcg_out_insn(s
, RI
, IILH
, ret
, uval
>> 16);
795 /* If we get here, both the high and low parts have non-zero bits. */
797 /* Recurse to load the lower 32-bits. */
798 tcg_out_movi(s
, TCG_TYPE_I32
, ret
, sval
);
800 /* Insert data into the high 32-bits. */
801 uval
= uval
>> 31 >> 1;
802 if (facilities
& FACILITY_EXT_IMM
) {
803 if (uval
< 0x10000) {
804 tcg_out_insn(s
, RI
, IIHL
, ret
, uval
);
805 } else if ((uval
& 0xffff) == 0) {
806 tcg_out_insn(s
, RI
, IIHH
, ret
, uval
>> 16);
808 tcg_out_insn(s
, RIL
, IIHF
, ret
, uval
);
812 tcg_out_insn(s
, RI
, IIHL
, ret
, uval
);
814 if (uval
& 0xffff0000) {
815 tcg_out_insn(s
, RI
, IIHH
, ret
, uval
>> 16);
821 /* Emit a load/store type instruction. Inputs are:
822 DATA: The register to be loaded or stored.
823 BASE+OFS: The effective address.
824 OPC_RX: If the operation has an RX format opcode (e.g. STC), otherwise 0.
825 OPC_RXY: The RXY format opcode for the operation (e.g. STCY). */
827 static void tcg_out_mem(TCGContext
*s
, S390Opcode opc_rx
, S390Opcode opc_rxy
,
828 TCGReg data
, TCGReg base
, TCGReg index
,
831 if (ofs
< -0x80000 || ofs
>= 0x80000) {
832 /* Combine the low 16 bits of the offset with the actual load insn;
833 the high 48 bits must come from an immediate load. */
834 tcg_out_movi(s
, TCG_TYPE_PTR
, TCG_TMP0
, ofs
& ~0xffff);
837 /* If we were already given an index register, add it in. */
838 if (index
!= TCG_REG_NONE
) {
839 tcg_out_insn(s
, RRE
, AGR
, TCG_TMP0
, index
);
844 if (opc_rx
&& ofs
>= 0 && ofs
< 0x1000) {
845 tcg_out_insn_RX(s
, opc_rx
, data
, base
, index
, ofs
);
847 tcg_out_insn_RXY(s
, opc_rxy
, data
, base
, index
, ofs
);
852 /* load data without address translation or endianness conversion */
853 static inline void tcg_out_ld(TCGContext
*s
, TCGType type
, TCGReg data
,
854 TCGReg base
, tcg_target_long ofs
)
856 if (type
== TCG_TYPE_I32
) {
857 tcg_out_mem(s
, RX_L
, RXY_LY
, data
, base
, TCG_REG_NONE
, ofs
);
859 tcg_out_mem(s
, 0, RXY_LG
, data
, base
, TCG_REG_NONE
, ofs
);
863 static inline void tcg_out_st(TCGContext
*s
, TCGType type
, TCGReg data
,
864 TCGReg base
, tcg_target_long ofs
)
866 if (type
== TCG_TYPE_I32
) {
867 tcg_out_mem(s
, RX_ST
, RXY_STY
, data
, base
, TCG_REG_NONE
, ofs
);
869 tcg_out_mem(s
, 0, RXY_STG
, data
, base
, TCG_REG_NONE
, ofs
);
873 /* load data from an absolute host address */
874 static void tcg_out_ld_abs(TCGContext
*s
, TCGType type
, TCGReg dest
, void *abs
)
876 tcg_target_long addr
= (tcg_target_long
)abs
;
878 if (facilities
& FACILITY_GEN_INST_EXT
) {
879 tcg_target_long disp
= (addr
- (tcg_target_long
)s
->code_ptr
) >> 1;
880 if (disp
== (int32_t)disp
) {
881 if (type
== TCG_TYPE_I32
) {
882 tcg_out_insn(s
, RIL
, LRL
, dest
, disp
);
884 tcg_out_insn(s
, RIL
, LGRL
, dest
, disp
);
890 tcg_out_movi(s
, TCG_TYPE_PTR
, dest
, addr
& ~0xffff);
891 tcg_out_ld(s
, type
, dest
, dest
, addr
& 0xffff);
894 static void tgen_ext8s(TCGContext
*s
, TCGType type
, TCGReg dest
, TCGReg src
)
896 if (facilities
& FACILITY_EXT_IMM
) {
897 tcg_out_insn(s
, RRE
, LGBR
, dest
, src
);
901 if (type
== TCG_TYPE_I32
) {
903 tcg_out_sh32(s
, RS_SLL
, dest
, TCG_REG_NONE
, 24);
905 tcg_out_sh64(s
, RSY_SLLG
, dest
, src
, TCG_REG_NONE
, 24);
907 tcg_out_sh32(s
, RS_SRA
, dest
, TCG_REG_NONE
, 24);
909 tcg_out_sh64(s
, RSY_SLLG
, dest
, src
, TCG_REG_NONE
, 56);
910 tcg_out_sh64(s
, RSY_SRAG
, dest
, dest
, TCG_REG_NONE
, 56);
914 static void tgen_ext8u(TCGContext
*s
, TCGType type
, TCGReg dest
, TCGReg src
)
916 if (facilities
& FACILITY_EXT_IMM
) {
917 tcg_out_insn(s
, RRE
, LLGCR
, dest
, src
);
922 tcg_out_movi(s
, type
, TCG_TMP0
, 0xff);
925 tcg_out_movi(s
, type
, dest
, 0xff);
927 if (type
== TCG_TYPE_I32
) {
928 tcg_out_insn(s
, RR
, NR
, dest
, src
);
930 tcg_out_insn(s
, RRE
, NGR
, dest
, src
);
934 static void tgen_ext16s(TCGContext
*s
, TCGType type
, TCGReg dest
, TCGReg src
)
936 if (facilities
& FACILITY_EXT_IMM
) {
937 tcg_out_insn(s
, RRE
, LGHR
, dest
, src
);
941 if (type
== TCG_TYPE_I32
) {
943 tcg_out_sh32(s
, RS_SLL
, dest
, TCG_REG_NONE
, 16);
945 tcg_out_sh64(s
, RSY_SLLG
, dest
, src
, TCG_REG_NONE
, 16);
947 tcg_out_sh32(s
, RS_SRA
, dest
, TCG_REG_NONE
, 16);
949 tcg_out_sh64(s
, RSY_SLLG
, dest
, src
, TCG_REG_NONE
, 48);
950 tcg_out_sh64(s
, RSY_SRAG
, dest
, dest
, TCG_REG_NONE
, 48);
954 static void tgen_ext16u(TCGContext
*s
, TCGType type
, TCGReg dest
, TCGReg src
)
956 if (facilities
& FACILITY_EXT_IMM
) {
957 tcg_out_insn(s
, RRE
, LLGHR
, dest
, src
);
962 tcg_out_movi(s
, type
, TCG_TMP0
, 0xffff);
965 tcg_out_movi(s
, type
, dest
, 0xffff);
967 if (type
== TCG_TYPE_I32
) {
968 tcg_out_insn(s
, RR
, NR
, dest
, src
);
970 tcg_out_insn(s
, RRE
, NGR
, dest
, src
);
974 static inline void tgen_ext32s(TCGContext
*s
, TCGReg dest
, TCGReg src
)
976 tcg_out_insn(s
, RRE
, LGFR
, dest
, src
);
979 static inline void tgen_ext32u(TCGContext
*s
, TCGReg dest
, TCGReg src
)
981 tcg_out_insn(s
, RRE
, LLGFR
, dest
, src
);
984 static inline void tgen32_addi(TCGContext
*s
, TCGReg dest
, int32_t val
)
986 if (val
== (int16_t)val
) {
987 tcg_out_insn(s
, RI
, AHI
, dest
, val
);
989 tcg_out_insn(s
, RIL
, AFI
, dest
, val
);
993 static inline void tgen64_addi(TCGContext
*s
, TCGReg dest
, int64_t val
)
995 if (val
== (int16_t)val
) {
996 tcg_out_insn(s
, RI
, AGHI
, dest
, val
);
997 } else if (val
== (int32_t)val
) {
998 tcg_out_insn(s
, RIL
, AGFI
, dest
, val
);
999 } else if (val
== (uint32_t)val
) {
1000 tcg_out_insn(s
, RIL
, ALGFI
, dest
, val
);
1007 static void tgen64_andi(TCGContext
*s
, TCGReg dest
, tcg_target_ulong val
)
1009 static const S390Opcode ni_insns
[4] = {
1010 RI_NILL
, RI_NILH
, RI_NIHL
, RI_NIHH
1012 static const S390Opcode nif_insns
[2] = {
1018 /* Look for no-op. */
1023 /* Look for the zero-extensions. */
1024 if (val
== 0xffffffff) {
1025 tgen_ext32u(s
, dest
, dest
);
1029 if (facilities
& FACILITY_EXT_IMM
) {
1031 tgen_ext8u(s
, TCG_TYPE_I64
, dest
, dest
);
1034 if (val
== 0xffff) {
1035 tgen_ext16u(s
, TCG_TYPE_I64
, dest
, dest
);
1039 /* Try all 32-bit insns that can perform it in one go. */
1040 for (i
= 0; i
< 4; i
++) {
1041 tcg_target_ulong mask
= ~(0xffffull
<< i
*16);
1042 if ((val
& mask
) == mask
) {
1043 tcg_out_insn_RI(s
, ni_insns
[i
], dest
, val
>> i
*16);
1048 /* Try all 48-bit insns that can perform it in one go. */
1049 if (facilities
& FACILITY_EXT_IMM
) {
1050 for (i
= 0; i
< 2; i
++) {
1051 tcg_target_ulong mask
= ~(0xffffffffull
<< i
*32);
1052 if ((val
& mask
) == mask
) {
1053 tcg_out_insn_RIL(s
, nif_insns
[i
], dest
, val
>> i
*32);
1059 /* Perform the AND via sequential modifications to the high and low
1060 parts. Do this via recursion to handle 16-bit vs 32-bit masks in
1062 tgen64_andi(s
, dest
, val
| 0xffffffff00000000ull
);
1063 tgen64_andi(s
, dest
, val
| 0x00000000ffffffffull
);
1065 /* With no extended-immediate facility, just emit the sequence. */
1066 for (i
= 0; i
< 4; i
++) {
1067 tcg_target_ulong mask
= 0xffffull
<< i
*16;
1068 if ((val
& mask
) != mask
) {
1069 tcg_out_insn_RI(s
, ni_insns
[i
], dest
, val
>> i
*16);
1075 static void tgen64_ori(TCGContext
*s
, TCGReg dest
, tcg_target_ulong val
)
1077 static const S390Opcode oi_insns
[4] = {
1078 RI_OILL
, RI_OILH
, RI_OIHL
, RI_OIHH
1080 static const S390Opcode nif_insns
[2] = {
1086 /* Look for no-op. */
1091 if (facilities
& FACILITY_EXT_IMM
) {
1092 /* Try all 32-bit insns that can perform it in one go. */
1093 for (i
= 0; i
< 4; i
++) {
1094 tcg_target_ulong mask
= (0xffffull
<< i
*16);
1095 if ((val
& mask
) != 0 && (val
& ~mask
) == 0) {
1096 tcg_out_insn_RI(s
, oi_insns
[i
], dest
, val
>> i
*16);
1101 /* Try all 48-bit insns that can perform it in one go. */
1102 for (i
= 0; i
< 2; i
++) {
1103 tcg_target_ulong mask
= (0xffffffffull
<< i
*32);
1104 if ((val
& mask
) != 0 && (val
& ~mask
) == 0) {
1105 tcg_out_insn_RIL(s
, nif_insns
[i
], dest
, val
>> i
*32);
1110 /* Perform the OR via sequential modifications to the high and
1111 low parts. Do this via recursion to handle 16-bit vs 32-bit
1112 masks in each half. */
1113 tgen64_ori(s
, dest
, val
& 0x00000000ffffffffull
);
1114 tgen64_ori(s
, dest
, val
& 0xffffffff00000000ull
);
1116 /* With no extended-immediate facility, we don't need to be so
1117 clever. Just iterate over the insns and mask in the constant. */
1118 for (i
= 0; i
< 4; i
++) {
1119 tcg_target_ulong mask
= (0xffffull
<< i
*16);
1120 if ((val
& mask
) != 0) {
1121 tcg_out_insn_RI(s
, oi_insns
[i
], dest
, val
>> i
*16);
1127 static void tgen64_xori(TCGContext
*s
, TCGReg dest
, tcg_target_ulong val
)
1129 /* Perform the xor by parts. */
1130 if (val
& 0xffffffff) {
1131 tcg_out_insn(s
, RIL
, XILF
, dest
, val
);
1133 if (val
> 0xffffffff) {
1134 tcg_out_insn(s
, RIL
, XIHF
, dest
, val
>> 31 >> 1);
1138 static int tgen_cmp(TCGContext
*s
, TCGType type
, TCGCond c
, TCGReg r1
,
1139 TCGArg c2
, int c2const
)
1141 bool is_unsigned
= (c
> TCG_COND_GT
);
1144 if (type
== TCG_TYPE_I32
) {
1145 tcg_out_insn(s
, RR
, LTR
, r1
, r1
);
1147 tcg_out_insn(s
, RRE
, LTGR
, r1
, r1
);
1149 return tcg_cond_to_ltr_cond
[c
];
1152 if (type
== TCG_TYPE_I32
) {
1153 tcg_out_insn(s
, RIL
, CLFI
, r1
, c2
);
1155 tcg_out_insn(s
, RIL
, CLGFI
, r1
, c2
);
1158 if (type
== TCG_TYPE_I32
) {
1159 tcg_out_insn(s
, RIL
, CFI
, r1
, c2
);
1161 tcg_out_insn(s
, RIL
, CGFI
, r1
, c2
);
1167 if (type
== TCG_TYPE_I32
) {
1168 tcg_out_insn(s
, RR
, CLR
, r1
, c2
);
1170 tcg_out_insn(s
, RRE
, CLGR
, r1
, c2
);
1173 if (type
== TCG_TYPE_I32
) {
1174 tcg_out_insn(s
, RR
, CR
, r1
, c2
);
1176 tcg_out_insn(s
, RRE
, CGR
, r1
, c2
);
1180 return tcg_cond_to_s390_cond
[c
];
1183 static void tgen_setcond(TCGContext
*s
, TCGType type
, TCGCond c
,
1184 TCGReg dest
, TCGReg r1
, TCGArg c2
, int c2const
)
1186 int cc
= tgen_cmp(s
, type
, c
, r1
, c2
, c2const
);
1188 /* Emit: r1 = 1; if (cc) goto over; r1 = 0; over: */
1189 tcg_out_movi(s
, type
, dest
, 1);
1190 tcg_out_insn(s
, RI
, BRC
, cc
, (4 + 4) >> 1);
1191 tcg_out_movi(s
, type
, dest
, 0);
1194 static void tgen_gotoi(TCGContext
*s
, int cc
, tcg_target_long dest
)
1196 tcg_target_long off
= (dest
- (tcg_target_long
)s
->code_ptr
) >> 1;
1197 if (off
> -0x8000 && off
< 0x7fff) {
1198 tcg_out_insn(s
, RI
, BRC
, cc
, off
);
1199 } else if (off
== (int32_t)off
) {
1200 tcg_out_insn(s
, RIL
, BRCL
, cc
, off
);
1202 tcg_out_movi(s
, TCG_TYPE_PTR
, TCG_TMP0
, dest
);
1203 tcg_out_insn(s
, RR
, BCR
, cc
, TCG_TMP0
);
1207 static void tgen_branch(TCGContext
*s
, int cc
, int labelno
)
1209 TCGLabel
* l
= &s
->labels
[labelno
];
1211 tgen_gotoi(s
, cc
, l
->u
.value
);
1212 } else if (USE_LONG_BRANCHES
) {
1213 tcg_out16(s
, RIL_BRCL
| (cc
<< 4));
1214 tcg_out_reloc(s
, s
->code_ptr
, R_390_PC32DBL
, labelno
, -2);
1217 tcg_out16(s
, RI_BRC
| (cc
<< 4));
1218 tcg_out_reloc(s
, s
->code_ptr
, R_390_PC16DBL
, labelno
, -2);
1223 static void tgen_compare_branch(TCGContext
*s
, S390Opcode opc
, int cc
,
1224 TCGReg r1
, TCGReg r2
, int labelno
)
1226 TCGLabel
* l
= &s
->labels
[labelno
];
1227 tcg_target_long off
;
1230 off
= (l
->u
.value
- (tcg_target_long
)s
->code_ptr
) >> 1;
1232 /* We need to keep the offset unchanged for retranslation. */
1233 off
= ((int16_t *)s
->code_ptr
)[1];
1234 tcg_out_reloc(s
, s
->code_ptr
+ 2, R_390_PC16DBL
, labelno
, -2);
1237 tcg_out16(s
, (opc
& 0xff00) | (r1
<< 4) | r2
);
1239 tcg_out16(s
, cc
<< 12 | (opc
& 0xff));
1242 static void tgen_compare_imm_branch(TCGContext
*s
, S390Opcode opc
, int cc
,
1243 TCGReg r1
, int i2
, int labelno
)
1245 TCGLabel
* l
= &s
->labels
[labelno
];
1246 tcg_target_long off
;
1249 off
= (l
->u
.value
- (tcg_target_long
)s
->code_ptr
) >> 1;
1251 /* We need to keep the offset unchanged for retranslation. */
1252 off
= ((int16_t *)s
->code_ptr
)[1];
1253 tcg_out_reloc(s
, s
->code_ptr
+ 2, R_390_PC16DBL
, labelno
, -2);
1256 tcg_out16(s
, (opc
& 0xff00) | (r1
<< 4) | cc
);
1258 tcg_out16(s
, (i2
<< 8) | (opc
& 0xff));
1261 static void tgen_brcond(TCGContext
*s
, TCGType type
, TCGCond c
,
1262 TCGReg r1
, TCGArg c2
, int c2const
, int labelno
)
1266 if (facilities
& FACILITY_GEN_INST_EXT
) {
1267 bool is_unsigned
= (c
> TCG_COND_GT
);
1271 cc
= tcg_cond_to_s390_cond
[c
];
1274 opc
= (type
== TCG_TYPE_I32
1275 ? (is_unsigned
? RIE_CLRJ
: RIE_CRJ
)
1276 : (is_unsigned
? RIE_CLGRJ
: RIE_CGRJ
));
1277 tgen_compare_branch(s
, opc
, cc
, r1
, c2
, labelno
);
1281 /* COMPARE IMMEDIATE AND BRANCH RELATIVE has an 8-bit immediate field.
1282 If the immediate we've been given does not fit that range, we'll
1283 fall back to separate compare and branch instructions using the
1284 larger comparison range afforded by COMPARE IMMEDIATE. */
1285 if (type
== TCG_TYPE_I32
) {
1288 in_range
= (uint32_t)c2
== (uint8_t)c2
;
1291 in_range
= (int32_t)c2
== (int8_t)c2
;
1296 in_range
= (uint64_t)c2
== (uint8_t)c2
;
1299 in_range
= (int64_t)c2
== (int8_t)c2
;
1303 tgen_compare_imm_branch(s
, opc
, cc
, r1
, c2
, labelno
);
1308 cc
= tgen_cmp(s
, type
, c
, r1
, c2
, c2const
);
1309 tgen_branch(s
, cc
, labelno
);
1312 static void tgen_calli(TCGContext
*s
, tcg_target_long dest
)
1314 tcg_target_long off
= (dest
- (tcg_target_long
)s
->code_ptr
) >> 1;
1315 if (off
== (int32_t)off
) {
1316 tcg_out_insn(s
, RIL
, BRASL
, TCG_REG_R14
, off
);
1318 tcg_out_movi(s
, TCG_TYPE_PTR
, TCG_TMP0
, dest
);
1319 tcg_out_insn(s
, RR
, BASR
, TCG_REG_R14
, TCG_TMP0
);
1323 static void tcg_out_qemu_ld_direct(TCGContext
*s
, int opc
, TCGReg data
,
1324 TCGReg base
, TCGReg index
, int disp
)
1326 #ifdef TARGET_WORDS_BIGENDIAN
1327 const int bswap
= 0;
1329 const int bswap
= 1;
1333 tcg_out_insn(s
, RXY
, LLGC
, data
, base
, index
, disp
);
1336 tcg_out_insn(s
, RXY
, LGB
, data
, base
, index
, disp
);
1340 /* swapped unsigned halfword load with upper bits zeroed */
1341 tcg_out_insn(s
, RXY
, LRVH
, data
, base
, index
, disp
);
1342 tgen_ext16u(s
, TCG_TYPE_I64
, data
, data
);
1344 tcg_out_insn(s
, RXY
, LLGH
, data
, base
, index
, disp
);
1349 /* swapped sign-extended halfword load */
1350 tcg_out_insn(s
, RXY
, LRVH
, data
, base
, index
, disp
);
1351 tgen_ext16s(s
, TCG_TYPE_I64
, data
, data
);
1353 tcg_out_insn(s
, RXY
, LGH
, data
, base
, index
, disp
);
1358 /* swapped unsigned int load with upper bits zeroed */
1359 tcg_out_insn(s
, RXY
, LRV
, data
, base
, index
, disp
);
1360 tgen_ext32u(s
, data
, data
);
1362 tcg_out_insn(s
, RXY
, LLGF
, data
, base
, index
, disp
);
1367 /* swapped sign-extended int load */
1368 tcg_out_insn(s
, RXY
, LRV
, data
, base
, index
, disp
);
1369 tgen_ext32s(s
, data
, data
);
1371 tcg_out_insn(s
, RXY
, LGF
, data
, base
, index
, disp
);
1376 tcg_out_insn(s
, RXY
, LRVG
, data
, base
, index
, disp
);
1378 tcg_out_insn(s
, RXY
, LG
, data
, base
, index
, disp
);
1386 static void tcg_out_qemu_st_direct(TCGContext
*s
, int opc
, TCGReg data
,
1387 TCGReg base
, TCGReg index
, int disp
)
1389 #ifdef TARGET_WORDS_BIGENDIAN
1390 const int bswap
= 0;
1392 const int bswap
= 1;
1396 if (disp
>= 0 && disp
< 0x1000) {
1397 tcg_out_insn(s
, RX
, STC
, data
, base
, index
, disp
);
1399 tcg_out_insn(s
, RXY
, STCY
, data
, base
, index
, disp
);
1404 tcg_out_insn(s
, RXY
, STRVH
, data
, base
, index
, disp
);
1405 } else if (disp
>= 0 && disp
< 0x1000) {
1406 tcg_out_insn(s
, RX
, STH
, data
, base
, index
, disp
);
1408 tcg_out_insn(s
, RXY
, STHY
, data
, base
, index
, disp
);
1413 tcg_out_insn(s
, RXY
, STRV
, data
, base
, index
, disp
);
1414 } else if (disp
>= 0 && disp
< 0x1000) {
1415 tcg_out_insn(s
, RX
, ST
, data
, base
, index
, disp
);
1417 tcg_out_insn(s
, RXY
, STY
, data
, base
, index
, disp
);
1422 tcg_out_insn(s
, RXY
, STRVG
, data
, base
, index
, disp
);
1424 tcg_out_insn(s
, RXY
, STG
, data
, base
, index
, disp
);
1432 #if defined(CONFIG_SOFTMMU)
1433 static void tgen64_andi_tmp(TCGContext
*s
, TCGReg dest
, tcg_target_ulong val
)
1435 if (tcg_match_andi(0, val
)) {
1436 tcg_out_movi(s
, TCG_TYPE_I64
, TCG_TMP0
, val
);
1437 tcg_out_insn(s
, RRE
, NGR
, dest
, TCG_TMP0
);
1439 tgen64_andi(s
, dest
, val
);
1443 static void tcg_prepare_qemu_ldst(TCGContext
* s
, TCGReg data_reg
,
1444 TCGReg addr_reg
, int mem_index
, int opc
,
1445 uint16_t **label2_ptr_p
, int is_store
)
1447 const TCGReg arg0
= TCG_REG_R2
;
1448 const TCGReg arg1
= TCG_REG_R3
;
1449 int s_bits
= opc
& 3;
1450 uint16_t *label1_ptr
;
1451 tcg_target_long ofs
;
1453 if (TARGET_LONG_BITS
== 32) {
1454 tgen_ext32u(s
, arg0
, addr_reg
);
1456 tcg_out_mov(s
, TCG_TYPE_I64
, arg0
, addr_reg
);
1459 tcg_out_sh64(s
, RSY_SRLG
, arg1
, addr_reg
, TCG_REG_NONE
,
1460 TARGET_PAGE_BITS
- CPU_TLB_ENTRY_BITS
);
1462 tgen64_andi_tmp(s
, arg0
, TARGET_PAGE_MASK
| ((1 << s_bits
) - 1));
1463 tgen64_andi_tmp(s
, arg1
, (CPU_TLB_SIZE
- 1) << CPU_TLB_ENTRY_BITS
);
1466 ofs
= offsetof(CPUArchState
, tlb_table
[mem_index
][0].addr_write
);
1468 ofs
= offsetof(CPUArchState
, tlb_table
[mem_index
][0].addr_read
);
1470 assert(ofs
< 0x80000);
1472 if (TARGET_LONG_BITS
== 32) {
1473 tcg_out_mem(s
, RX_C
, RXY_CY
, arg0
, arg1
, TCG_AREG0
, ofs
);
1475 tcg_out_mem(s
, 0, RXY_CG
, arg0
, arg1
, TCG_AREG0
, ofs
);
1478 if (TARGET_LONG_BITS
== 32) {
1479 tgen_ext32u(s
, arg0
, addr_reg
);
1481 tcg_out_mov(s
, TCG_TYPE_I64
, arg0
, addr_reg
);
1484 label1_ptr
= (uint16_t*)s
->code_ptr
;
1486 /* je label1 (offset will be patched in later) */
1487 tcg_out_insn(s
, RI
, BRC
, S390_CC_EQ
, 0);
1489 /* call load/store helper */
1491 /* Make sure to zero-extend the value to the full register
1492 for the calling convention. */
1495 tgen_ext8u(s
, TCG_TYPE_I64
, arg1
, data_reg
);
1498 tgen_ext16u(s
, TCG_TYPE_I64
, arg1
, data_reg
);
1501 tgen_ext32u(s
, arg1
, data_reg
);
1504 tcg_out_mov(s
, TCG_TYPE_I64
, arg1
, data_reg
);
1509 tcg_out_movi(s
, TCG_TYPE_I32
, TCG_REG_R4
, mem_index
);
1510 #ifdef CONFIG_TCG_PASS_AREG0
1511 /* XXX/FIXME: suboptimal */
1512 tcg_out_mov(s
, TCG_TYPE_I32
, tcg_target_call_iarg_regs
[2],
1513 tcg_target_call_iarg_regs
[1]);
1514 tcg_out_mov(s
, TCG_TYPE_TL
, tcg_target_call_iarg_regs
[1],
1515 tcg_target_call_iarg_regs
[0]);
1516 tcg_out_mov(s
, TCG_TYPE_PTR
, tcg_target_call_iarg_regs
[0],
1519 tgen_calli(s
, (tcg_target_ulong
)qemu_st_helpers
[s_bits
]);
1521 tcg_out_movi(s
, TCG_TYPE_I32
, arg1
, mem_index
);
1522 #ifdef CONFIG_TCG_PASS_AREG0
1523 /* XXX/FIXME: suboptimal */
1524 tcg_out_mov(s
, TCG_TYPE_I32
, tcg_target_call_iarg_regs
[3],
1525 tcg_target_call_iarg_regs
[2]);
1526 tcg_out_mov(s
, TCG_TYPE_I64
, tcg_target_call_iarg_regs
[2],
1527 tcg_target_call_iarg_regs
[1]);
1528 tcg_out_mov(s
, TCG_TYPE_TL
, tcg_target_call_iarg_regs
[1],
1529 tcg_target_call_iarg_regs
[0]);
1530 tcg_out_mov(s
, TCG_TYPE_PTR
, tcg_target_call_iarg_regs
[0],
1533 tgen_calli(s
, (tcg_target_ulong
)qemu_ld_helpers
[s_bits
]);
1535 /* sign extension */
1538 tgen_ext8s(s
, TCG_TYPE_I64
, data_reg
, arg0
);
1541 tgen_ext16s(s
, TCG_TYPE_I64
, data_reg
, arg0
);
1544 tgen_ext32s(s
, data_reg
, arg0
);
1547 /* unsigned -> just copy */
1548 tcg_out_mov(s
, TCG_TYPE_I64
, data_reg
, arg0
);
1553 /* jump to label2 (end) */
1554 *label2_ptr_p
= (uint16_t*)s
->code_ptr
;
1556 tcg_out_insn(s
, RI
, BRC
, S390_CC_ALWAYS
, 0);
1558 /* this is label1, patch branch */
1559 *(label1_ptr
+ 1) = ((unsigned long)s
->code_ptr
-
1560 (unsigned long)label1_ptr
) >> 1;
1562 ofs
= offsetof(CPUArchState
, tlb_table
[mem_index
][0].addend
);
1563 assert(ofs
< 0x80000);
1565 tcg_out_mem(s
, 0, RXY_AG
, arg0
, arg1
, TCG_AREG0
, ofs
);
1568 static void tcg_finish_qemu_ldst(TCGContext
* s
, uint16_t *label2_ptr
)
1571 *(label2_ptr
+ 1) = ((unsigned long)s
->code_ptr
-
1572 (unsigned long)label2_ptr
) >> 1;
1575 static void tcg_prepare_user_ldst(TCGContext
*s
, TCGReg
*addr_reg
,
1576 TCGReg
*index_reg
, tcg_target_long
*disp
)
1578 if (TARGET_LONG_BITS
== 32) {
1579 tgen_ext32u(s
, TCG_TMP0
, *addr_reg
);
1580 *addr_reg
= TCG_TMP0
;
1582 if (GUEST_BASE
< 0x80000) {
1583 *index_reg
= TCG_REG_NONE
;
1586 *index_reg
= TCG_GUEST_BASE_REG
;
1590 #endif /* CONFIG_SOFTMMU */
1592 /* load data with address translation (if applicable)
1593 and endianness conversion */
1594 static void tcg_out_qemu_ld(TCGContext
* s
, const TCGArg
* args
, int opc
)
1596 TCGReg addr_reg
, data_reg
;
1597 #if defined(CONFIG_SOFTMMU)
1599 uint16_t *label2_ptr
;
1602 tcg_target_long disp
;
1608 #if defined(CONFIG_SOFTMMU)
1611 tcg_prepare_qemu_ldst(s
, data_reg
, addr_reg
, mem_index
,
1612 opc
, &label2_ptr
, 0);
1614 tcg_out_qemu_ld_direct(s
, opc
, data_reg
, TCG_REG_R2
, TCG_REG_NONE
, 0);
1616 tcg_finish_qemu_ldst(s
, label2_ptr
);
1618 tcg_prepare_user_ldst(s
, &addr_reg
, &index_reg
, &disp
);
1619 tcg_out_qemu_ld_direct(s
, opc
, data_reg
, addr_reg
, index_reg
, disp
);
1623 static void tcg_out_qemu_st(TCGContext
* s
, const TCGArg
* args
, int opc
)
1625 TCGReg addr_reg
, data_reg
;
1626 #if defined(CONFIG_SOFTMMU)
1628 uint16_t *label2_ptr
;
1631 tcg_target_long disp
;
1637 #if defined(CONFIG_SOFTMMU)
1640 tcg_prepare_qemu_ldst(s
, data_reg
, addr_reg
, mem_index
,
1641 opc
, &label2_ptr
, 1);
1643 tcg_out_qemu_st_direct(s
, opc
, data_reg
, TCG_REG_R2
, TCG_REG_NONE
, 0);
1645 tcg_finish_qemu_ldst(s
, label2_ptr
);
1647 tcg_prepare_user_ldst(s
, &addr_reg
, &index_reg
, &disp
);
1648 tcg_out_qemu_st_direct(s
, opc
, data_reg
, addr_reg
, index_reg
, disp
);
1652 #if TCG_TARGET_REG_BITS == 64
1653 # define OP_32_64(x) \
1654 case glue(glue(INDEX_op_,x),_i32): \
1655 case glue(glue(INDEX_op_,x),_i64)
1657 # define OP_32_64(x) \
1658 case glue(glue(INDEX_op_,x),_i32)
1661 static inline void tcg_out_op(TCGContext
*s
, TCGOpcode opc
,
1662 const TCGArg
*args
, const int *const_args
)
1667 case INDEX_op_exit_tb
:
1669 tcg_out_movi(s
, TCG_TYPE_PTR
, TCG_REG_R2
, args
[0]);
1670 tgen_gotoi(s
, S390_CC_ALWAYS
, (unsigned long)tb_ret_addr
);
1673 case INDEX_op_goto_tb
:
1674 if (s
->tb_jmp_offset
) {
1677 /* load address stored at s->tb_next + args[0] */
1678 tcg_out_ld_abs(s
, TCG_TYPE_PTR
, TCG_TMP0
, s
->tb_next
+ args
[0]);
1680 tcg_out_insn(s
, RR
, BCR
, S390_CC_ALWAYS
, TCG_TMP0
);
1682 s
->tb_next_offset
[args
[0]] = s
->code_ptr
- s
->code_buf
;
1686 if (const_args
[0]) {
1687 tgen_calli(s
, args
[0]);
1689 tcg_out_insn(s
, RR
, BASR
, TCG_REG_R14
, args
[0]);
1693 case INDEX_op_mov_i32
:
1694 tcg_out_mov(s
, TCG_TYPE_I32
, args
[0], args
[1]);
1696 case INDEX_op_movi_i32
:
1697 tcg_out_movi(s
, TCG_TYPE_I32
, args
[0], args
[1]);
1701 /* ??? LLC (RXY format) is only present with the extended-immediate
1702 facility, whereas LLGC is always present. */
1703 tcg_out_mem(s
, 0, RXY_LLGC
, args
[0], args
[1], TCG_REG_NONE
, args
[2]);
1707 /* ??? LB is no smaller than LGB, so no point to using it. */
1708 tcg_out_mem(s
, 0, RXY_LGB
, args
[0], args
[1], TCG_REG_NONE
, args
[2]);
1712 /* ??? LLH (RXY format) is only present with the extended-immediate
1713 facility, whereas LLGH is always present. */
1714 tcg_out_mem(s
, 0, RXY_LLGH
, args
[0], args
[1], TCG_REG_NONE
, args
[2]);
1717 case INDEX_op_ld16s_i32
:
1718 tcg_out_mem(s
, RX_LH
, RXY_LHY
, args
[0], args
[1], TCG_REG_NONE
, args
[2]);
1721 case INDEX_op_ld_i32
:
1722 tcg_out_ld(s
, TCG_TYPE_I32
, args
[0], args
[1], args
[2]);
1726 tcg_out_mem(s
, RX_STC
, RXY_STCY
, args
[0], args
[1],
1727 TCG_REG_NONE
, args
[2]);
1731 tcg_out_mem(s
, RX_STH
, RXY_STHY
, args
[0], args
[1],
1732 TCG_REG_NONE
, args
[2]);
1735 case INDEX_op_st_i32
:
1736 tcg_out_st(s
, TCG_TYPE_I32
, args
[0], args
[1], args
[2]);
1739 case INDEX_op_add_i32
:
1740 if (const_args
[2]) {
1741 tgen32_addi(s
, args
[0], args
[2]);
1743 tcg_out_insn(s
, RR
, AR
, args
[0], args
[2]);
1746 case INDEX_op_sub_i32
:
1747 if (const_args
[2]) {
1748 tgen32_addi(s
, args
[0], -args
[2]);
1750 tcg_out_insn(s
, RR
, SR
, args
[0], args
[2]);
1754 case INDEX_op_and_i32
:
1755 if (const_args
[2]) {
1756 tgen64_andi(s
, args
[0], args
[2] | 0xffffffff00000000ull
);
1758 tcg_out_insn(s
, RR
, NR
, args
[0], args
[2]);
1761 case INDEX_op_or_i32
:
1762 if (const_args
[2]) {
1763 tgen64_ori(s
, args
[0], args
[2] & 0xffffffff);
1765 tcg_out_insn(s
, RR
, OR
, args
[0], args
[2]);
1768 case INDEX_op_xor_i32
:
1769 if (const_args
[2]) {
1770 tgen64_xori(s
, args
[0], args
[2] & 0xffffffff);
1772 tcg_out_insn(s
, RR
, XR
, args
[0], args
[2]);
1776 case INDEX_op_neg_i32
:
1777 tcg_out_insn(s
, RR
, LCR
, args
[0], args
[1]);
1780 case INDEX_op_mul_i32
:
1781 if (const_args
[2]) {
1782 if ((int32_t)args
[2] == (int16_t)args
[2]) {
1783 tcg_out_insn(s
, RI
, MHI
, args
[0], args
[2]);
1785 tcg_out_insn(s
, RIL
, MSFI
, args
[0], args
[2]);
1788 tcg_out_insn(s
, RRE
, MSR
, args
[0], args
[2]);
1792 case INDEX_op_div2_i32
:
1793 tcg_out_insn(s
, RR
, DR
, TCG_REG_R2
, args
[4]);
1795 case INDEX_op_divu2_i32
:
1796 tcg_out_insn(s
, RRE
, DLR
, TCG_REG_R2
, args
[4]);
1799 case INDEX_op_shl_i32
:
1802 if (const_args
[2]) {
1803 tcg_out_sh32(s
, op
, args
[0], TCG_REG_NONE
, args
[2]);
1805 tcg_out_sh32(s
, op
, args
[0], args
[2], 0);
1808 case INDEX_op_shr_i32
:
1811 case INDEX_op_sar_i32
:
1815 case INDEX_op_rotl_i32
:
1816 /* ??? Using tcg_out_sh64 here for the format; it is a 32-bit rol. */
1817 if (const_args
[2]) {
1818 tcg_out_sh64(s
, RSY_RLL
, args
[0], args
[1], TCG_REG_NONE
, args
[2]);
1820 tcg_out_sh64(s
, RSY_RLL
, args
[0], args
[1], args
[2], 0);
1823 case INDEX_op_rotr_i32
:
1824 if (const_args
[2]) {
1825 tcg_out_sh64(s
, RSY_RLL
, args
[0], args
[1],
1826 TCG_REG_NONE
, (32 - args
[2]) & 31);
1828 tcg_out_insn(s
, RR
, LCR
, TCG_TMP0
, args
[2]);
1829 tcg_out_sh64(s
, RSY_RLL
, args
[0], args
[1], TCG_TMP0
, 0);
1833 case INDEX_op_ext8s_i32
:
1834 tgen_ext8s(s
, TCG_TYPE_I32
, args
[0], args
[1]);
1836 case INDEX_op_ext16s_i32
:
1837 tgen_ext16s(s
, TCG_TYPE_I32
, args
[0], args
[1]);
1839 case INDEX_op_ext8u_i32
:
1840 tgen_ext8u(s
, TCG_TYPE_I32
, args
[0], args
[1]);
1842 case INDEX_op_ext16u_i32
:
1843 tgen_ext16u(s
, TCG_TYPE_I32
, args
[0], args
[1]);
1847 /* The TCG bswap definition requires bits 0-47 already be zero.
1848 Thus we don't need the G-type insns to implement bswap16_i64. */
1849 tcg_out_insn(s
, RRE
, LRVR
, args
[0], args
[1]);
1850 tcg_out_sh32(s
, RS_SRL
, args
[0], TCG_REG_NONE
, 16);
1853 tcg_out_insn(s
, RRE
, LRVR
, args
[0], args
[1]);
1857 tgen_branch(s
, S390_CC_ALWAYS
, args
[0]);
1860 case INDEX_op_brcond_i32
:
1861 tgen_brcond(s
, TCG_TYPE_I32
, args
[2], args
[0],
1862 args
[1], const_args
[1], args
[3]);
1864 case INDEX_op_setcond_i32
:
1865 tgen_setcond(s
, TCG_TYPE_I32
, args
[3], args
[0], args
[1],
1866 args
[2], const_args
[2]);
1869 case INDEX_op_qemu_ld8u
:
1870 tcg_out_qemu_ld(s
, args
, LD_UINT8
);
1872 case INDEX_op_qemu_ld8s
:
1873 tcg_out_qemu_ld(s
, args
, LD_INT8
);
1875 case INDEX_op_qemu_ld16u
:
1876 tcg_out_qemu_ld(s
, args
, LD_UINT16
);
1878 case INDEX_op_qemu_ld16s
:
1879 tcg_out_qemu_ld(s
, args
, LD_INT16
);
1881 case INDEX_op_qemu_ld32
:
1882 /* ??? Technically we can use a non-extending instruction. */
1883 tcg_out_qemu_ld(s
, args
, LD_UINT32
);
1885 case INDEX_op_qemu_ld64
:
1886 tcg_out_qemu_ld(s
, args
, LD_UINT64
);
1889 case INDEX_op_qemu_st8
:
1890 tcg_out_qemu_st(s
, args
, LD_UINT8
);
1892 case INDEX_op_qemu_st16
:
1893 tcg_out_qemu_st(s
, args
, LD_UINT16
);
1895 case INDEX_op_qemu_st32
:
1896 tcg_out_qemu_st(s
, args
, LD_UINT32
);
1898 case INDEX_op_qemu_st64
:
1899 tcg_out_qemu_st(s
, args
, LD_UINT64
);
1902 #if TCG_TARGET_REG_BITS == 64
1903 case INDEX_op_mov_i64
:
1904 tcg_out_mov(s
, TCG_TYPE_I64
, args
[0], args
[1]);
1906 case INDEX_op_movi_i64
:
1907 tcg_out_movi(s
, TCG_TYPE_I64
, args
[0], args
[1]);
1910 case INDEX_op_ld16s_i64
:
1911 tcg_out_mem(s
, 0, RXY_LGH
, args
[0], args
[1], TCG_REG_NONE
, args
[2]);
1913 case INDEX_op_ld32u_i64
:
1914 tcg_out_mem(s
, 0, RXY_LLGF
, args
[0], args
[1], TCG_REG_NONE
, args
[2]);
1916 case INDEX_op_ld32s_i64
:
1917 tcg_out_mem(s
, 0, RXY_LGF
, args
[0], args
[1], TCG_REG_NONE
, args
[2]);
1919 case INDEX_op_ld_i64
:
1920 tcg_out_ld(s
, TCG_TYPE_I64
, args
[0], args
[1], args
[2]);
1923 case INDEX_op_st32_i64
:
1924 tcg_out_st(s
, TCG_TYPE_I32
, args
[0], args
[1], args
[2]);
1926 case INDEX_op_st_i64
:
1927 tcg_out_st(s
, TCG_TYPE_I64
, args
[0], args
[1], args
[2]);
1930 case INDEX_op_add_i64
:
1931 if (const_args
[2]) {
1932 tgen64_addi(s
, args
[0], args
[2]);
1934 tcg_out_insn(s
, RRE
, AGR
, args
[0], args
[2]);
1937 case INDEX_op_sub_i64
:
1938 if (const_args
[2]) {
1939 tgen64_addi(s
, args
[0], -args
[2]);
1941 tcg_out_insn(s
, RRE
, SGR
, args
[0], args
[2]);
1945 case INDEX_op_and_i64
:
1946 if (const_args
[2]) {
1947 tgen64_andi(s
, args
[0], args
[2]);
1949 tcg_out_insn(s
, RRE
, NGR
, args
[0], args
[2]);
1952 case INDEX_op_or_i64
:
1953 if (const_args
[2]) {
1954 tgen64_ori(s
, args
[0], args
[2]);
1956 tcg_out_insn(s
, RRE
, OGR
, args
[0], args
[2]);
1959 case INDEX_op_xor_i64
:
1960 if (const_args
[2]) {
1961 tgen64_xori(s
, args
[0], args
[2]);
1963 tcg_out_insn(s
, RRE
, XGR
, args
[0], args
[2]);
1967 case INDEX_op_neg_i64
:
1968 tcg_out_insn(s
, RRE
, LCGR
, args
[0], args
[1]);
1970 case INDEX_op_bswap64_i64
:
1971 tcg_out_insn(s
, RRE
, LRVGR
, args
[0], args
[1]);
1974 case INDEX_op_mul_i64
:
1975 if (const_args
[2]) {
1976 if (args
[2] == (int16_t)args
[2]) {
1977 tcg_out_insn(s
, RI
, MGHI
, args
[0], args
[2]);
1979 tcg_out_insn(s
, RIL
, MSGFI
, args
[0], args
[2]);
1982 tcg_out_insn(s
, RRE
, MSGR
, args
[0], args
[2]);
1986 case INDEX_op_div2_i64
:
1987 /* ??? We get an unnecessary sign-extension of the dividend
1988 into R3 with this definition, but as we do in fact always
1989 produce both quotient and remainder using INDEX_op_div_i64
1990 instead requires jumping through even more hoops. */
1991 tcg_out_insn(s
, RRE
, DSGR
, TCG_REG_R2
, args
[4]);
1993 case INDEX_op_divu2_i64
:
1994 tcg_out_insn(s
, RRE
, DLGR
, TCG_REG_R2
, args
[4]);
1997 case INDEX_op_shl_i64
:
2000 if (const_args
[2]) {
2001 tcg_out_sh64(s
, op
, args
[0], args
[1], TCG_REG_NONE
, args
[2]);
2003 tcg_out_sh64(s
, op
, args
[0], args
[1], args
[2], 0);
2006 case INDEX_op_shr_i64
:
2009 case INDEX_op_sar_i64
:
2013 case INDEX_op_rotl_i64
:
2014 if (const_args
[2]) {
2015 tcg_out_sh64(s
, RSY_RLLG
, args
[0], args
[1],
2016 TCG_REG_NONE
, args
[2]);
2018 tcg_out_sh64(s
, RSY_RLLG
, args
[0], args
[1], args
[2], 0);
2021 case INDEX_op_rotr_i64
:
2022 if (const_args
[2]) {
2023 tcg_out_sh64(s
, RSY_RLLG
, args
[0], args
[1],
2024 TCG_REG_NONE
, (64 - args
[2]) & 63);
2026 /* We can use the smaller 32-bit negate because only the
2027 low 6 bits are examined for the rotate. */
2028 tcg_out_insn(s
, RR
, LCR
, TCG_TMP0
, args
[2]);
2029 tcg_out_sh64(s
, RSY_RLLG
, args
[0], args
[1], TCG_TMP0
, 0);
2033 case INDEX_op_ext8s_i64
:
2034 tgen_ext8s(s
, TCG_TYPE_I64
, args
[0], args
[1]);
2036 case INDEX_op_ext16s_i64
:
2037 tgen_ext16s(s
, TCG_TYPE_I64
, args
[0], args
[1]);
2039 case INDEX_op_ext32s_i64
:
2040 tgen_ext32s(s
, args
[0], args
[1]);
2042 case INDEX_op_ext8u_i64
:
2043 tgen_ext8u(s
, TCG_TYPE_I64
, args
[0], args
[1]);
2045 case INDEX_op_ext16u_i64
:
2046 tgen_ext16u(s
, TCG_TYPE_I64
, args
[0], args
[1]);
2048 case INDEX_op_ext32u_i64
:
2049 tgen_ext32u(s
, args
[0], args
[1]);
2052 case INDEX_op_brcond_i64
:
2053 tgen_brcond(s
, TCG_TYPE_I64
, args
[2], args
[0],
2054 args
[1], const_args
[1], args
[3]);
2056 case INDEX_op_setcond_i64
:
2057 tgen_setcond(s
, TCG_TYPE_I64
, args
[3], args
[0], args
[1],
2058 args
[2], const_args
[2]);
2061 case INDEX_op_qemu_ld32u
:
2062 tcg_out_qemu_ld(s
, args
, LD_UINT32
);
2064 case INDEX_op_qemu_ld32s
:
2065 tcg_out_qemu_ld(s
, args
, LD_INT32
);
2067 #endif /* TCG_TARGET_REG_BITS == 64 */
2070 /* This one is obsolete and never emitted. */
2075 fprintf(stderr
,"unimplemented opc 0x%x\n",opc
);
2080 static const TCGTargetOpDef s390_op_defs
[] = {
2081 { INDEX_op_exit_tb
, { } },
2082 { INDEX_op_goto_tb
, { } },
2083 { INDEX_op_call
, { "ri" } },
2084 { INDEX_op_jmp
, { "ri" } },
2085 { INDEX_op_br
, { } },
2087 { INDEX_op_mov_i32
, { "r", "r" } },
2088 { INDEX_op_movi_i32
, { "r" } },
2090 { INDEX_op_ld8u_i32
, { "r", "r" } },
2091 { INDEX_op_ld8s_i32
, { "r", "r" } },
2092 { INDEX_op_ld16u_i32
, { "r", "r" } },
2093 { INDEX_op_ld16s_i32
, { "r", "r" } },
2094 { INDEX_op_ld_i32
, { "r", "r" } },
2095 { INDEX_op_st8_i32
, { "r", "r" } },
2096 { INDEX_op_st16_i32
, { "r", "r" } },
2097 { INDEX_op_st_i32
, { "r", "r" } },
2099 { INDEX_op_add_i32
, { "r", "0", "rWI" } },
2100 { INDEX_op_sub_i32
, { "r", "0", "rWNI" } },
2101 { INDEX_op_mul_i32
, { "r", "0", "rK" } },
2103 { INDEX_op_div2_i32
, { "b", "a", "0", "1", "r" } },
2104 { INDEX_op_divu2_i32
, { "b", "a", "0", "1", "r" } },
2106 { INDEX_op_and_i32
, { "r", "0", "rWA" } },
2107 { INDEX_op_or_i32
, { "r", "0", "rWO" } },
2108 { INDEX_op_xor_i32
, { "r", "0", "rWX" } },
2110 { INDEX_op_neg_i32
, { "r", "r" } },
2112 { INDEX_op_shl_i32
, { "r", "0", "Ri" } },
2113 { INDEX_op_shr_i32
, { "r", "0", "Ri" } },
2114 { INDEX_op_sar_i32
, { "r", "0", "Ri" } },
2116 { INDEX_op_rotl_i32
, { "r", "r", "Ri" } },
2117 { INDEX_op_rotr_i32
, { "r", "r", "Ri" } },
2119 { INDEX_op_ext8s_i32
, { "r", "r" } },
2120 { INDEX_op_ext8u_i32
, { "r", "r" } },
2121 { INDEX_op_ext16s_i32
, { "r", "r" } },
2122 { INDEX_op_ext16u_i32
, { "r", "r" } },
2124 { INDEX_op_bswap16_i32
, { "r", "r" } },
2125 { INDEX_op_bswap32_i32
, { "r", "r" } },
2127 { INDEX_op_brcond_i32
, { "r", "rWC" } },
2128 { INDEX_op_setcond_i32
, { "r", "r", "rWC" } },
2130 { INDEX_op_qemu_ld8u
, { "r", "L" } },
2131 { INDEX_op_qemu_ld8s
, { "r", "L" } },
2132 { INDEX_op_qemu_ld16u
, { "r", "L" } },
2133 { INDEX_op_qemu_ld16s
, { "r", "L" } },
2134 { INDEX_op_qemu_ld32
, { "r", "L" } },
2135 { INDEX_op_qemu_ld64
, { "r", "L" } },
2137 { INDEX_op_qemu_st8
, { "L", "L" } },
2138 { INDEX_op_qemu_st16
, { "L", "L" } },
2139 { INDEX_op_qemu_st32
, { "L", "L" } },
2140 { INDEX_op_qemu_st64
, { "L", "L" } },
2142 #if defined(__s390x__)
2143 { INDEX_op_mov_i64
, { "r", "r" } },
2144 { INDEX_op_movi_i64
, { "r" } },
2146 { INDEX_op_ld8u_i64
, { "r", "r" } },
2147 { INDEX_op_ld8s_i64
, { "r", "r" } },
2148 { INDEX_op_ld16u_i64
, { "r", "r" } },
2149 { INDEX_op_ld16s_i64
, { "r", "r" } },
2150 { INDEX_op_ld32u_i64
, { "r", "r" } },
2151 { INDEX_op_ld32s_i64
, { "r", "r" } },
2152 { INDEX_op_ld_i64
, { "r", "r" } },
2154 { INDEX_op_st8_i64
, { "r", "r" } },
2155 { INDEX_op_st16_i64
, { "r", "r" } },
2156 { INDEX_op_st32_i64
, { "r", "r" } },
2157 { INDEX_op_st_i64
, { "r", "r" } },
2159 { INDEX_op_add_i64
, { "r", "0", "rI" } },
2160 { INDEX_op_sub_i64
, { "r", "0", "rNI" } },
2161 { INDEX_op_mul_i64
, { "r", "0", "rK" } },
2163 { INDEX_op_div2_i64
, { "b", "a", "0", "1", "r" } },
2164 { INDEX_op_divu2_i64
, { "b", "a", "0", "1", "r" } },
2166 { INDEX_op_and_i64
, { "r", "0", "rA" } },
2167 { INDEX_op_or_i64
, { "r", "0", "rO" } },
2168 { INDEX_op_xor_i64
, { "r", "0", "rX" } },
2170 { INDEX_op_neg_i64
, { "r", "r" } },
2172 { INDEX_op_shl_i64
, { "r", "r", "Ri" } },
2173 { INDEX_op_shr_i64
, { "r", "r", "Ri" } },
2174 { INDEX_op_sar_i64
, { "r", "r", "Ri" } },
2176 { INDEX_op_rotl_i64
, { "r", "r", "Ri" } },
2177 { INDEX_op_rotr_i64
, { "r", "r", "Ri" } },
2179 { INDEX_op_ext8s_i64
, { "r", "r" } },
2180 { INDEX_op_ext8u_i64
, { "r", "r" } },
2181 { INDEX_op_ext16s_i64
, { "r", "r" } },
2182 { INDEX_op_ext16u_i64
, { "r", "r" } },
2183 { INDEX_op_ext32s_i64
, { "r", "r" } },
2184 { INDEX_op_ext32u_i64
, { "r", "r" } },
2186 { INDEX_op_bswap16_i64
, { "r", "r" } },
2187 { INDEX_op_bswap32_i64
, { "r", "r" } },
2188 { INDEX_op_bswap64_i64
, { "r", "r" } },
2190 { INDEX_op_brcond_i64
, { "r", "rC" } },
2191 { INDEX_op_setcond_i64
, { "r", "r", "rC" } },
2193 { INDEX_op_qemu_ld32u
, { "r", "L" } },
2194 { INDEX_op_qemu_ld32s
, { "r", "L" } },
2200 /* ??? Linux kernels provide an AUXV entry AT_HWCAP that provides most of
2201 this information. However, getting at that entry is not easy this far
2202 away from main. Our options are: start searching from environ, but
2203 that fails as soon as someone does a setenv in between. Read the data
2204 from /proc/self/auxv. Or do the probing ourselves. The only thing
2205 extra that AT_HWCAP gives us is HWCAP_S390_HIGH_GPRS, which indicates
2206 that the kernel saves all 64-bits of the registers around traps while
2207 in 31-bit mode. But this is true of all "recent" kernels (ought to dig
2208 back and see from when this might not be true). */
2212 static volatile sig_atomic_t got_sigill
;
2214 static void sigill_handler(int sig
)
2219 static void query_facilities(void)
2221 struct sigaction sa_old
, sa_new
;
2222 register int r0
__asm__("0");
2223 register void *r1
__asm__("1");
2226 memset(&sa_new
, 0, sizeof(sa_new
));
2227 sa_new
.sa_handler
= sigill_handler
;
2228 sigaction(SIGILL
, &sa_new
, &sa_old
);
2230 /* First, try STORE FACILITY LIST EXTENDED. If this is present, then
2231 we need not do any more probing. Unfortunately, this itself is an
2232 extension and the original STORE FACILITY LIST instruction is
2233 kernel-only, storing its results at absolute address 200. */
2236 asm volatile(".word 0xb2b0,0x1000"
2237 : "=r"(r0
) : "0"(0), "r"(r1
) : "memory", "cc");
2240 /* STORE FACILITY EXTENDED is not available. Probe for one of each
2241 kind of instruction that we're interested in. */
2242 /* ??? Possibly some of these are in practice never present unless
2243 the store-facility-extended facility is also present. But since
2244 that isn't documented it's just better to probe for each. */
2246 /* Test for z/Architecture. Required even in 31-bit mode. */
2249 asm volatile(".word 0xb908,0x0000" : "=r"(r0
) : : "cc");
2251 facilities
|= FACILITY_ZARCH_ACTIVE
;
2254 /* Test for long displacement. */
2258 asm volatile(".word 0xe300,0x1000,0x0058"
2259 : "=r"(r0
) : "r"(r1
) : "cc");
2261 facilities
|= FACILITY_LONG_DISP
;
2264 /* Test for extended immediates. */
2267 asm volatile(".word 0xc209,0x0000,0x0000" : : : "cc");
2269 facilities
|= FACILITY_EXT_IMM
;
2272 /* Test for general-instructions-extension. */
2275 asm volatile(".word 0xc201,0x0000,0x0001");
2277 facilities
|= FACILITY_GEN_INST_EXT
;
2281 sigaction(SIGILL
, &sa_old
, NULL
);
2283 /* The translator currently uses these extensions unconditionally.
2284 Pruning this back to the base ESA/390 architecture doesn't seem
2285 worthwhile, since even the KVM target requires z/Arch. */
2287 if ((facilities
& FACILITY_ZARCH_ACTIVE
) == 0) {
2288 fprintf(stderr
, "TCG: z/Arch facility is required.\n");
2289 fprintf(stderr
, "TCG: Boot with a 64-bit enabled kernel.\n");
2292 if ((facilities
& FACILITY_LONG_DISP
) == 0) {
2293 fprintf(stderr
, "TCG: long-displacement facility is required.\n");
2297 /* So far there's just enough support for 31-bit mode to let the
2298 compile succeed. This is good enough to run QEMU with KVM. */
2299 if (sizeof(void *) != 8) {
2300 fprintf(stderr
, "TCG: 31-bit mode is not supported.\n");
2309 static void tcg_target_init(TCGContext
*s
)
2311 #if !defined(CONFIG_USER_ONLY)
2313 if ((1 << CPU_TLB_ENTRY_BITS
) != sizeof(CPUTLBEntry
)) {
2320 tcg_regset_set32(tcg_target_available_regs
[TCG_TYPE_I32
], 0, 0xffff);
2321 tcg_regset_set32(tcg_target_available_regs
[TCG_TYPE_I64
], 0, 0xffff);
2323 tcg_regset_clear(tcg_target_call_clobber_regs
);
2324 tcg_regset_set_reg(tcg_target_call_clobber_regs
, TCG_REG_R0
);
2325 tcg_regset_set_reg(tcg_target_call_clobber_regs
, TCG_REG_R1
);
2326 tcg_regset_set_reg(tcg_target_call_clobber_regs
, TCG_REG_R2
);
2327 tcg_regset_set_reg(tcg_target_call_clobber_regs
, TCG_REG_R3
);
2328 tcg_regset_set_reg(tcg_target_call_clobber_regs
, TCG_REG_R4
);
2329 tcg_regset_set_reg(tcg_target_call_clobber_regs
, TCG_REG_R5
);
2330 /* The return register can be considered call-clobbered. */
2331 tcg_regset_set_reg(tcg_target_call_clobber_regs
, TCG_REG_R14
);
2333 tcg_regset_clear(s
->reserved_regs
);
2334 tcg_regset_set_reg(s
->reserved_regs
, TCG_TMP0
);
2335 /* XXX many insns can't be used with R0, so we better avoid it for now */
2336 tcg_regset_set_reg(s
->reserved_regs
, TCG_REG_R0
);
2337 tcg_regset_set_reg(s
->reserved_regs
, TCG_REG_CALL_STACK
);
2339 tcg_add_target_add_op_defs(s390_op_defs
);
2340 tcg_set_frame(s
, TCG_AREG0
, offsetof(CPUArchState
, temp_buf
),
2341 CPU_TEMP_BUF_NLONGS
* sizeof(long));
2344 static void tcg_target_qemu_prologue(TCGContext
*s
)
2346 /* stmg %r6,%r15,48(%r15) (save registers) */
2347 tcg_out_insn(s
, RXY
, STMG
, TCG_REG_R6
, TCG_REG_R15
, TCG_REG_R15
, 48);
2349 /* aghi %r15,-160 (stack frame) */
2350 tcg_out_insn(s
, RI
, AGHI
, TCG_REG_R15
, -160);
2352 if (GUEST_BASE
>= 0x80000) {
2353 tcg_out_movi(s
, TCG_TYPE_PTR
, TCG_GUEST_BASE_REG
, GUEST_BASE
);
2354 tcg_regset_set_reg(s
->reserved_regs
, TCG_GUEST_BASE_REG
);
2357 tcg_out_mov(s
, TCG_TYPE_PTR
, TCG_AREG0
, tcg_target_call_iarg_regs
[0]);
2358 /* br %r3 (go to TB) */
2359 tcg_out_insn(s
, RR
, BCR
, S390_CC_ALWAYS
, tcg_target_call_iarg_regs
[1]);
2361 tb_ret_addr
= s
->code_ptr
;
2363 /* lmg %r6,%r15,208(%r15) (restore registers) */
2364 tcg_out_insn(s
, RXY
, LMG
, TCG_REG_R6
, TCG_REG_R15
, TCG_REG_R15
, 208);
2366 /* br %r14 (return) */
2367 tcg_out_insn(s
, RR
, BCR
, S390_CC_ALWAYS
, TCG_REG_R14
);