2 * Tiny Code Generator for QEMU
4 * Copyright (c) 2008 Fabrice Bellard
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
25 #include "tcg-be-ldst.h"
27 #if defined _CALL_DARWIN || defined __APPLE__
28 #define TCG_TARGET_CALL_DARWIN
31 # define TCG_TARGET_CALL_ALIGN_ARGS 1
34 /* For some memory operations, we need a scratch that isn't R0. For the AIX
35 calling convention, we can re-use the TOC register since we'll be reloading
36 it at every call. Otherwise R12 will do nicely as neither a call-saved
37 register nor a parameter register. */
39 # define TCG_REG_TMP1 TCG_REG_R2
41 # define TCG_REG_TMP1 TCG_REG_R12
44 /* For the 64-bit target, we don't like the 5 insn sequence needed to build
45 full 64-bit addresses. Better to have a base register to which we can
46 apply a 32-bit displacement.
48 There are generally three items of interest:
49 (1) helper functions in the main executable,
50 (2) TranslationBlock data structures,
51 (3) the return address in the epilogue.
53 For user-only, we USE_STATIC_CODE_GEN_BUFFER, so the code_gen_buffer
54 will be inside the main executable, and thus near enough to make a
55 pointer to the epilogue be within 2GB of all helper functions.
57 For softmmu, we'll let the kernel choose the address of code_gen_buffer,
58 and odds are it'll be somewhere close to the main malloc arena, and so
59 a pointer to the epilogue will be within 2GB of the TranslationBlocks.
61 For --enable-pie, everything will be kinda near everything else,
62 somewhere in high memory.
64 Thus we choose to keep the return address in a call-saved register. */
65 #define TCG_REG_RA TCG_REG_R31
66 #define USE_REG_RA (TCG_TARGET_REG_BITS == 64)
68 /* Shorthand for size of a pointer. Avoid promotion to unsigned. */
69 #define SZP ((int)sizeof(void *))
71 /* Shorthand for size of a register. */
72 #define SZR (TCG_TARGET_REG_BITS / 8)
74 #define TCG_CT_CONST_S16 0x100
75 #define TCG_CT_CONST_U16 0x200
76 #define TCG_CT_CONST_S32 0x400
77 #define TCG_CT_CONST_U32 0x800
78 #define TCG_CT_CONST_ZERO 0x1000
79 #define TCG_CT_CONST_MONE 0x2000
80 #define TCG_CT_CONST_WSZ 0x4000
82 static tcg_insn_unit
*tb_ret_addr
;
89 #define HAVE_ISA_2_06 have_isa_2_06
90 #define HAVE_ISEL have_isa_2_06
92 #ifndef CONFIG_SOFTMMU
93 #define TCG_GUEST_BASE_REG 30
96 #ifdef CONFIG_DEBUG_TCG
97 static const char * const tcg_target_reg_names
[TCG_TARGET_NB_REGS
] = {
133 static const int tcg_target_reg_alloc_order
[] = {
134 TCG_REG_R14
, /* call saved registers */
152 TCG_REG_R12
, /* call clobbered, non-arguments */
156 TCG_REG_R10
, /* call clobbered, arguments */
166 static const int tcg_target_call_iarg_regs
[] = {
177 static const int tcg_target_call_oarg_regs
[] = {
182 static const int tcg_target_callee_save_regs
[] = {
183 #ifdef TCG_TARGET_CALL_DARWIN
199 TCG_REG_R27
, /* currently used for the global env */
206 static inline bool in_range_b(tcg_target_long target
)
208 return target
== sextract64(target
, 0, 26);
211 static uint32_t reloc_pc24_val(tcg_insn_unit
*pc
, tcg_insn_unit
*target
)
213 ptrdiff_t disp
= tcg_ptr_byte_diff(target
, pc
);
214 tcg_debug_assert(in_range_b(disp
));
215 return disp
& 0x3fffffc;
218 static void reloc_pc24(tcg_insn_unit
*pc
, tcg_insn_unit
*target
)
220 *pc
= (*pc
& ~0x3fffffc) | reloc_pc24_val(pc
, target
);
223 static uint16_t reloc_pc14_val(tcg_insn_unit
*pc
, tcg_insn_unit
*target
)
225 ptrdiff_t disp
= tcg_ptr_byte_diff(target
, pc
);
226 tcg_debug_assert(disp
== (int16_t) disp
);
227 return disp
& 0xfffc;
230 static void reloc_pc14(tcg_insn_unit
*pc
, tcg_insn_unit
*target
)
232 *pc
= (*pc
& ~0xfffc) | reloc_pc14_val(pc
, target
);
235 static inline void tcg_out_b_noaddr(TCGContext
*s
, int insn
)
237 unsigned retrans
= *s
->code_ptr
& 0x3fffffc;
238 tcg_out32(s
, insn
| retrans
);
241 static inline void tcg_out_bc_noaddr(TCGContext
*s
, int insn
)
243 unsigned retrans
= *s
->code_ptr
& 0xfffc;
244 tcg_out32(s
, insn
| retrans
);
247 static void patch_reloc(tcg_insn_unit
*code_ptr
, int type
,
248 intptr_t value
, intptr_t addend
)
250 tcg_insn_unit
*target
= (tcg_insn_unit
*)value
;
252 tcg_debug_assert(addend
== 0);
255 reloc_pc14(code_ptr
, target
);
258 reloc_pc24(code_ptr
, target
);
265 /* parse target specific constraints */
266 static const char *target_parse_constraint(TCGArgConstraint
*ct
,
267 const char *ct_str
, TCGType type
)
270 case 'A': case 'B': case 'C': case 'D':
271 ct
->ct
|= TCG_CT_REG
;
272 tcg_regset_set_reg(ct
->u
.regs
, 3 + ct_str
[0] - 'A');
275 ct
->ct
|= TCG_CT_REG
;
276 tcg_regset_set32(ct
->u
.regs
, 0, 0xffffffff);
278 case 'L': /* qemu_ld constraint */
279 ct
->ct
|= TCG_CT_REG
;
280 tcg_regset_set32(ct
->u
.regs
, 0, 0xffffffff);
281 tcg_regset_reset_reg(ct
->u
.regs
, TCG_REG_R3
);
282 #ifdef CONFIG_SOFTMMU
283 tcg_regset_reset_reg(ct
->u
.regs
, TCG_REG_R4
);
284 tcg_regset_reset_reg(ct
->u
.regs
, TCG_REG_R5
);
287 case 'S': /* qemu_st constraint */
288 ct
->ct
|= TCG_CT_REG
;
289 tcg_regset_set32(ct
->u
.regs
, 0, 0xffffffff);
290 tcg_regset_reset_reg(ct
->u
.regs
, TCG_REG_R3
);
291 #ifdef CONFIG_SOFTMMU
292 tcg_regset_reset_reg(ct
->u
.regs
, TCG_REG_R4
);
293 tcg_regset_reset_reg(ct
->u
.regs
, TCG_REG_R5
);
294 tcg_regset_reset_reg(ct
->u
.regs
, TCG_REG_R6
);
298 ct
->ct
|= TCG_CT_CONST_S16
;
301 ct
->ct
|= TCG_CT_CONST_U16
;
304 ct
->ct
|= TCG_CT_CONST_MONE
;
307 ct
->ct
|= TCG_CT_CONST_S32
;
310 ct
->ct
|= TCG_CT_CONST_U32
;
313 ct
->ct
|= TCG_CT_CONST_WSZ
;
316 ct
->ct
|= TCG_CT_CONST_ZERO
;
324 /* test if a constant matches the constraint */
325 static int tcg_target_const_match(tcg_target_long val
, TCGType type
,
326 const TCGArgConstraint
*arg_ct
)
329 if (ct
& TCG_CT_CONST
) {
333 /* The only 32-bit constraint we use aside from
334 TCG_CT_CONST is TCG_CT_CONST_S16. */
335 if (type
== TCG_TYPE_I32
) {
339 if ((ct
& TCG_CT_CONST_S16
) && val
== (int16_t)val
) {
341 } else if ((ct
& TCG_CT_CONST_U16
) && val
== (uint16_t)val
) {
343 } else if ((ct
& TCG_CT_CONST_S32
) && val
== (int32_t)val
) {
345 } else if ((ct
& TCG_CT_CONST_U32
) && val
== (uint32_t)val
) {
347 } else if ((ct
& TCG_CT_CONST_ZERO
) && val
== 0) {
349 } else if ((ct
& TCG_CT_CONST_MONE
) && val
== -1) {
351 } else if ((ct
& TCG_CT_CONST_WSZ
)
352 && val
== (type
== TCG_TYPE_I32
? 32 : 64)) {
358 #define OPCD(opc) ((opc)<<26)
359 #define XO19(opc) (OPCD(19)|((opc)<<1))
360 #define MD30(opc) (OPCD(30)|((opc)<<2))
361 #define MDS30(opc) (OPCD(30)|((opc)<<1))
362 #define XO31(opc) (OPCD(31)|((opc)<<1))
363 #define XO58(opc) (OPCD(58)|(opc))
364 #define XO62(opc) (OPCD(62)|(opc))
368 #define LBZ OPCD( 34)
369 #define LHZ OPCD( 40)
370 #define LHA OPCD( 42)
371 #define LWZ OPCD( 32)
372 #define STB OPCD( 38)
373 #define STH OPCD( 44)
374 #define STW OPCD( 36)
377 #define STDU XO62( 1)
378 #define STDX XO31(149)
381 #define LDX XO31( 21)
384 #define LWAX XO31(341)
386 #define ADDIC OPCD( 12)
387 #define ADDI OPCD( 14)
388 #define ADDIS OPCD( 15)
389 #define ORI OPCD( 24)
390 #define ORIS OPCD( 25)
391 #define XORI OPCD( 26)
392 #define XORIS OPCD( 27)
393 #define ANDI OPCD( 28)
394 #define ANDIS OPCD( 29)
395 #define MULLI OPCD( 7)
396 #define CMPLI OPCD( 10)
397 #define CMPI OPCD( 11)
398 #define SUBFIC OPCD( 8)
400 #define LWZU OPCD( 33)
401 #define STWU OPCD( 37)
403 #define RLWIMI OPCD( 20)
404 #define RLWINM OPCD( 21)
405 #define RLWNM OPCD( 23)
407 #define RLDICL MD30( 0)
408 #define RLDICR MD30( 1)
409 #define RLDIMI MD30( 3)
410 #define RLDCL MDS30( 8)
412 #define BCLR XO19( 16)
413 #define BCCTR XO19(528)
414 #define CRAND XO19(257)
415 #define CRANDC XO19(129)
416 #define CRNAND XO19(225)
417 #define CROR XO19(449)
418 #define CRNOR XO19( 33)
420 #define EXTSB XO31(954)
421 #define EXTSH XO31(922)
422 #define EXTSW XO31(986)
423 #define ADD XO31(266)
424 #define ADDE XO31(138)
425 #define ADDME XO31(234)
426 #define ADDZE XO31(202)
427 #define ADDC XO31( 10)
428 #define AND XO31( 28)
429 #define SUBF XO31( 40)
430 #define SUBFC XO31( 8)
431 #define SUBFE XO31(136)
432 #define SUBFME XO31(232)
433 #define SUBFZE XO31(200)
435 #define XOR XO31(316)
436 #define MULLW XO31(235)
437 #define MULHW XO31( 75)
438 #define MULHWU XO31( 11)
439 #define DIVW XO31(491)
440 #define DIVWU XO31(459)
442 #define CMPL XO31( 32)
443 #define LHBRX XO31(790)
444 #define LWBRX XO31(534)
445 #define LDBRX XO31(532)
446 #define STHBRX XO31(918)
447 #define STWBRX XO31(662)
448 #define STDBRX XO31(660)
449 #define MFSPR XO31(339)
450 #define MTSPR XO31(467)
451 #define SRAWI XO31(824)
452 #define NEG XO31(104)
453 #define MFCR XO31( 19)
454 #define MFOCRF (MFCR | (1u << 20))
455 #define NOR XO31(124)
456 #define CNTLZW XO31( 26)
457 #define CNTLZD XO31( 58)
458 #define CNTTZW XO31(538)
459 #define CNTTZD XO31(570)
460 #define CNTPOPW XO31(378)
461 #define CNTPOPD XO31(506)
462 #define ANDC XO31( 60)
463 #define ORC XO31(412)
464 #define EQV XO31(284)
465 #define NAND XO31(476)
466 #define ISEL XO31( 15)
468 #define MULLD XO31(233)
469 #define MULHD XO31( 73)
470 #define MULHDU XO31( 9)
471 #define DIVD XO31(489)
472 #define DIVDU XO31(457)
474 #define LBZX XO31( 87)
475 #define LHZX XO31(279)
476 #define LHAX XO31(343)
477 #define LWZX XO31( 23)
478 #define STBX XO31(215)
479 #define STHX XO31(407)
480 #define STWX XO31(151)
482 #define EIEIO XO31(854)
483 #define HWSYNC XO31(598)
484 #define LWSYNC (HWSYNC | (1u << 21))
486 #define SPR(a, b) ((((a)<<5)|(b))<<11)
488 #define CTR SPR(9, 0)
490 #define SLW XO31( 24)
491 #define SRW XO31(536)
492 #define SRAW XO31(792)
494 #define SLD XO31( 27)
495 #define SRD XO31(539)
496 #define SRAD XO31(794)
497 #define SRADI XO31(413<<1)
500 #define TRAP (TW | TO(31))
502 #define NOP ORI /* ori 0,0,0 */
504 #define RT(r) ((r)<<21)
505 #define RS(r) ((r)<<21)
506 #define RA(r) ((r)<<16)
507 #define RB(r) ((r)<<11)
508 #define TO(t) ((t)<<21)
509 #define SH(s) ((s)<<11)
510 #define MB(b) ((b)<<6)
511 #define ME(e) ((e)<<1)
512 #define BO(o) ((o)<<21)
513 #define MB64(b) ((b)<<5)
514 #define FXM(b) (1 << (19 - (b)))
518 #define TAB(t, a, b) (RT(t) | RA(a) | RB(b))
519 #define SAB(s, a, b) (RS(s) | RA(a) | RB(b))
520 #define TAI(s, a, i) (RT(s) | RA(a) | ((i) & 0xffff))
521 #define SAI(s, a, i) (RS(s) | RA(a) | ((i) & 0xffff))
523 #define BF(n) ((n)<<23)
524 #define BI(n, c) (((c)+((n)*4))<<16)
525 #define BT(n, c) (((c)+((n)*4))<<21)
526 #define BA(n, c) (((c)+((n)*4))<<16)
527 #define BB(n, c) (((c)+((n)*4))<<11)
528 #define BC_(n, c) (((c)+((n)*4))<<6)
530 #define BO_COND_TRUE BO(12)
531 #define BO_COND_FALSE BO( 4)
532 #define BO_ALWAYS BO(20)
541 static const uint32_t tcg_to_bc
[] = {
542 [TCG_COND_EQ
] = BC
| BI(7, CR_EQ
) | BO_COND_TRUE
,
543 [TCG_COND_NE
] = BC
| BI(7, CR_EQ
) | BO_COND_FALSE
,
544 [TCG_COND_LT
] = BC
| BI(7, CR_LT
) | BO_COND_TRUE
,
545 [TCG_COND_GE
] = BC
| BI(7, CR_LT
) | BO_COND_FALSE
,
546 [TCG_COND_LE
] = BC
| BI(7, CR_GT
) | BO_COND_FALSE
,
547 [TCG_COND_GT
] = BC
| BI(7, CR_GT
) | BO_COND_TRUE
,
548 [TCG_COND_LTU
] = BC
| BI(7, CR_LT
) | BO_COND_TRUE
,
549 [TCG_COND_GEU
] = BC
| BI(7, CR_LT
) | BO_COND_FALSE
,
550 [TCG_COND_LEU
] = BC
| BI(7, CR_GT
) | BO_COND_FALSE
,
551 [TCG_COND_GTU
] = BC
| BI(7, CR_GT
) | BO_COND_TRUE
,
554 /* The low bit here is set if the RA and RB fields must be inverted. */
555 static const uint32_t tcg_to_isel
[] = {
556 [TCG_COND_EQ
] = ISEL
| BC_(7, CR_EQ
),
557 [TCG_COND_NE
] = ISEL
| BC_(7, CR_EQ
) | 1,
558 [TCG_COND_LT
] = ISEL
| BC_(7, CR_LT
),
559 [TCG_COND_GE
] = ISEL
| BC_(7, CR_LT
) | 1,
560 [TCG_COND_LE
] = ISEL
| BC_(7, CR_GT
) | 1,
561 [TCG_COND_GT
] = ISEL
| BC_(7, CR_GT
),
562 [TCG_COND_LTU
] = ISEL
| BC_(7, CR_LT
),
563 [TCG_COND_GEU
] = ISEL
| BC_(7, CR_LT
) | 1,
564 [TCG_COND_LEU
] = ISEL
| BC_(7, CR_GT
) | 1,
565 [TCG_COND_GTU
] = ISEL
| BC_(7, CR_GT
),
568 static void tcg_out_mem_long(TCGContext
*s
, int opi
, int opx
, TCGReg rt
,
569 TCGReg base
, tcg_target_long offset
);
571 static void tcg_out_mov(TCGContext
*s
, TCGType type
, TCGReg ret
, TCGReg arg
)
573 tcg_debug_assert(TCG_TARGET_REG_BITS
== 64 || type
== TCG_TYPE_I32
);
575 tcg_out32(s
, OR
| SAB(arg
, ret
, arg
));
579 static inline void tcg_out_rld(TCGContext
*s
, int op
, TCGReg ra
, TCGReg rs
,
582 tcg_debug_assert(TCG_TARGET_REG_BITS
== 64);
583 sh
= SH(sh
& 0x1f) | (((sh
>> 5) & 1) << 1);
584 mb
= MB64((mb
>> 5) | ((mb
<< 1) & 0x3f));
585 tcg_out32(s
, op
| RA(ra
) | RS(rs
) | sh
| mb
);
588 static inline void tcg_out_rlw(TCGContext
*s
, int op
, TCGReg ra
, TCGReg rs
,
589 int sh
, int mb
, int me
)
591 tcg_out32(s
, op
| RA(ra
) | RS(rs
) | SH(sh
) | MB(mb
) | ME(me
));
594 static inline void tcg_out_ext32u(TCGContext
*s
, TCGReg dst
, TCGReg src
)
596 tcg_out_rld(s
, RLDICL
, dst
, src
, 0, 32);
599 static inline void tcg_out_shli32(TCGContext
*s
, TCGReg dst
, TCGReg src
, int c
)
601 tcg_out_rlw(s
, RLWINM
, dst
, src
, c
, 0, 31 - c
);
604 static inline void tcg_out_shli64(TCGContext
*s
, TCGReg dst
, TCGReg src
, int c
)
606 tcg_out_rld(s
, RLDICR
, dst
, src
, c
, 63 - c
);
609 static inline void tcg_out_shri32(TCGContext
*s
, TCGReg dst
, TCGReg src
, int c
)
611 tcg_out_rlw(s
, RLWINM
, dst
, src
, 32 - c
, c
, 31);
614 static inline void tcg_out_shri64(TCGContext
*s
, TCGReg dst
, TCGReg src
, int c
)
616 tcg_out_rld(s
, RLDICL
, dst
, src
, 64 - c
, c
);
619 static void tcg_out_movi32(TCGContext
*s
, TCGReg ret
, int32_t arg
)
621 if (arg
== (int16_t) arg
) {
622 tcg_out32(s
, ADDI
| TAI(ret
, 0, arg
));
624 tcg_out32(s
, ADDIS
| TAI(ret
, 0, arg
>> 16));
626 tcg_out32(s
, ORI
| SAI(ret
, ret
, arg
));
631 static void tcg_out_movi(TCGContext
*s
, TCGType type
, TCGReg ret
,
634 tcg_debug_assert(TCG_TARGET_REG_BITS
== 64 || type
== TCG_TYPE_I32
);
635 if (type
== TCG_TYPE_I32
|| arg
== (int32_t)arg
) {
636 tcg_out_movi32(s
, ret
, arg
);
637 } else if (arg
== (uint32_t)arg
&& !(arg
& 0x8000)) {
638 tcg_out32(s
, ADDI
| TAI(ret
, 0, arg
));
639 tcg_out32(s
, ORIS
| SAI(ret
, ret
, arg
>> 16));
644 intptr_t diff
= arg
- (intptr_t)tb_ret_addr
;
645 if (diff
== (int32_t)diff
) {
646 tcg_out_mem_long(s
, ADDI
, ADD
, ret
, TCG_REG_RA
, diff
);
651 high
= arg
>> 31 >> 1;
652 tcg_out_movi32(s
, ret
, high
);
654 tcg_out_shli64(s
, ret
, ret
, 32);
656 if (arg
& 0xffff0000) {
657 tcg_out32(s
, ORIS
| SAI(ret
, ret
, arg
>> 16));
660 tcg_out32(s
, ORI
| SAI(ret
, ret
, arg
));
665 static bool mask_operand(uint32_t c
, int *mb
, int *me
)
669 /* Accept a bit pattern like:
673 Keep track of the transitions. */
674 if (c
== 0 || c
== -1) {
680 if (test
& (test
- 1)) {
685 *mb
= test
? clz32(test
& -test
) + 1 : 0;
689 static bool mask64_operand(uint64_t c
, int *mb
, int *me
)
698 /* Accept 1..10..0. */
704 /* Accept 0..01..1. */
705 if (lsb
== 1 && (c
& (c
+ 1)) == 0) {
706 *mb
= clz64(c
+ 1) + 1;
713 static void tcg_out_andi32(TCGContext
*s
, TCGReg dst
, TCGReg src
, uint32_t c
)
717 if (mask_operand(c
, &mb
, &me
)) {
718 tcg_out_rlw(s
, RLWINM
, dst
, src
, 0, mb
, me
);
719 } else if ((c
& 0xffff) == c
) {
720 tcg_out32(s
, ANDI
| SAI(src
, dst
, c
));
722 } else if ((c
& 0xffff0000) == c
) {
723 tcg_out32(s
, ANDIS
| SAI(src
, dst
, c
>> 16));
726 tcg_out_movi(s
, TCG_TYPE_I32
, TCG_REG_R0
, c
);
727 tcg_out32(s
, AND
| SAB(src
, dst
, TCG_REG_R0
));
731 static void tcg_out_andi64(TCGContext
*s
, TCGReg dst
, TCGReg src
, uint64_t c
)
735 tcg_debug_assert(TCG_TARGET_REG_BITS
== 64);
736 if (mask64_operand(c
, &mb
, &me
)) {
738 tcg_out_rld(s
, RLDICR
, dst
, src
, 0, me
);
740 tcg_out_rld(s
, RLDICL
, dst
, src
, 0, mb
);
742 } else if ((c
& 0xffff) == c
) {
743 tcg_out32(s
, ANDI
| SAI(src
, dst
, c
));
745 } else if ((c
& 0xffff0000) == c
) {
746 tcg_out32(s
, ANDIS
| SAI(src
, dst
, c
>> 16));
749 tcg_out_movi(s
, TCG_TYPE_I64
, TCG_REG_R0
, c
);
750 tcg_out32(s
, AND
| SAB(src
, dst
, TCG_REG_R0
));
754 static void tcg_out_zori32(TCGContext
*s
, TCGReg dst
, TCGReg src
, uint32_t c
,
755 int op_lo
, int op_hi
)
758 tcg_out32(s
, op_hi
| SAI(src
, dst
, c
>> 16));
762 tcg_out32(s
, op_lo
| SAI(src
, dst
, c
));
767 static void tcg_out_ori32(TCGContext
*s
, TCGReg dst
, TCGReg src
, uint32_t c
)
769 tcg_out_zori32(s
, dst
, src
, c
, ORI
, ORIS
);
772 static void tcg_out_xori32(TCGContext
*s
, TCGReg dst
, TCGReg src
, uint32_t c
)
774 tcg_out_zori32(s
, dst
, src
, c
, XORI
, XORIS
);
777 static void tcg_out_b(TCGContext
*s
, int mask
, tcg_insn_unit
*target
)
779 ptrdiff_t disp
= tcg_pcrel_diff(s
, target
);
780 if (in_range_b(disp
)) {
781 tcg_out32(s
, B
| (disp
& 0x3fffffc) | mask
);
783 tcg_out_movi(s
, TCG_TYPE_PTR
, TCG_REG_R0
, (uintptr_t)target
);
784 tcg_out32(s
, MTSPR
| RS(TCG_REG_R0
) | CTR
);
785 tcg_out32(s
, BCCTR
| BO_ALWAYS
| mask
);
789 static void tcg_out_mem_long(TCGContext
*s
, int opi
, int opx
, TCGReg rt
,
790 TCGReg base
, tcg_target_long offset
)
792 tcg_target_long orig
= offset
, l0
, l1
, extra
= 0, align
= 0;
793 bool is_store
= false;
794 TCGReg rs
= TCG_REG_TMP1
;
801 if (rt
!= TCG_REG_R0
) {
809 case STB
: case STH
: case STW
:
814 /* For unaligned, or very large offsets, use the indexed form. */
815 if (offset
& align
|| offset
!= (int32_t)offset
) {
819 tcg_debug_assert(!is_store
|| rs
!= rt
);
820 tcg_out_movi(s
, TCG_TYPE_PTR
, rs
, orig
);
821 tcg_out32(s
, opx
| TAB(rt
, base
, rs
));
825 l0
= (int16_t)offset
;
826 offset
= (offset
- l0
) >> 16;
827 l1
= (int16_t)offset
;
829 if (l1
< 0 && orig
>= 0) {
831 l1
= (int16_t)(offset
- 0x4000);
834 tcg_out32(s
, ADDIS
| TAI(rs
, base
, l1
));
838 tcg_out32(s
, ADDIS
| TAI(rs
, base
, extra
));
841 if (opi
!= ADDI
|| base
!= rt
|| l0
!= 0) {
842 tcg_out32(s
, opi
| TAI(rt
, base
, l0
));
846 static inline void tcg_out_ld(TCGContext
*s
, TCGType type
, TCGReg ret
,
847 TCGReg arg1
, intptr_t arg2
)
851 tcg_debug_assert(TCG_TARGET_REG_BITS
== 64 || type
== TCG_TYPE_I32
);
852 if (type
== TCG_TYPE_I32
) {
853 opi
= LWZ
, opx
= LWZX
;
857 tcg_out_mem_long(s
, opi
, opx
, ret
, arg1
, arg2
);
860 static inline void tcg_out_st(TCGContext
*s
, TCGType type
, TCGReg arg
,
861 TCGReg arg1
, intptr_t arg2
)
865 tcg_debug_assert(TCG_TARGET_REG_BITS
== 64 || type
== TCG_TYPE_I32
);
866 if (type
== TCG_TYPE_I32
) {
867 opi
= STW
, opx
= STWX
;
869 opi
= STD
, opx
= STDX
;
871 tcg_out_mem_long(s
, opi
, opx
, arg
, arg1
, arg2
);
874 static inline bool tcg_out_sti(TCGContext
*s
, TCGType type
, TCGArg val
,
875 TCGReg base
, intptr_t ofs
)
880 static void tcg_out_cmp(TCGContext
*s
, int cond
, TCGArg arg1
, TCGArg arg2
,
881 int const_arg2
, int cr
, TCGType type
)
886 tcg_debug_assert(TCG_TARGET_REG_BITS
== 64 || type
== TCG_TYPE_I32
);
888 /* Simplify the comparisons below wrt CMPI. */
889 if (type
== TCG_TYPE_I32
) {
890 arg2
= (int32_t)arg2
;
897 if ((int16_t) arg2
== arg2
) {
901 } else if ((uint16_t) arg2
== arg2
) {
916 if ((int16_t) arg2
== arg2
) {
931 if ((uint16_t) arg2
== arg2
) {
944 op
|= BF(cr
) | ((type
== TCG_TYPE_I64
) << 21);
947 tcg_out32(s
, op
| RA(arg1
) | (arg2
& 0xffff));
950 tcg_out_movi(s
, type
, TCG_REG_R0
, arg2
);
953 tcg_out32(s
, op
| RA(arg1
) | RB(arg2
));
957 static void tcg_out_setcond_eq0(TCGContext
*s
, TCGType type
,
958 TCGReg dst
, TCGReg src
)
960 if (type
== TCG_TYPE_I32
) {
961 tcg_out32(s
, CNTLZW
| RS(src
) | RA(dst
));
962 tcg_out_shri32(s
, dst
, dst
, 5);
964 tcg_out32(s
, CNTLZD
| RS(src
) | RA(dst
));
965 tcg_out_shri64(s
, dst
, dst
, 6);
969 static void tcg_out_setcond_ne0(TCGContext
*s
, TCGReg dst
, TCGReg src
)
971 /* X != 0 implies X + -1 generates a carry. Extra addition
972 trickery means: R = X-1 + ~X + C = X-1 + (-X+1) + C = C. */
974 tcg_out32(s
, ADDIC
| TAI(dst
, src
, -1));
975 tcg_out32(s
, SUBFE
| TAB(dst
, dst
, src
));
977 tcg_out32(s
, ADDIC
| TAI(TCG_REG_R0
, src
, -1));
978 tcg_out32(s
, SUBFE
| TAB(dst
, TCG_REG_R0
, src
));
982 static TCGReg
tcg_gen_setcond_xor(TCGContext
*s
, TCGReg arg1
, TCGArg arg2
,
986 if ((uint32_t)arg2
== arg2
) {
987 tcg_out_xori32(s
, TCG_REG_R0
, arg1
, arg2
);
989 tcg_out_movi(s
, TCG_TYPE_I64
, TCG_REG_R0
, arg2
);
990 tcg_out32(s
, XOR
| SAB(arg1
, TCG_REG_R0
, TCG_REG_R0
));
993 tcg_out32(s
, XOR
| SAB(arg1
, TCG_REG_R0
, arg2
));
998 static void tcg_out_setcond(TCGContext
*s
, TCGType type
, TCGCond cond
,
999 TCGArg arg0
, TCGArg arg1
, TCGArg arg2
,
1004 tcg_debug_assert(TCG_TARGET_REG_BITS
== 64 || type
== TCG_TYPE_I32
);
1006 /* Ignore high bits of a potential constant arg2. */
1007 if (type
== TCG_TYPE_I32
) {
1008 arg2
= (uint32_t)arg2
;
1011 /* Handle common and trivial cases before handling anything else. */
1015 tcg_out_setcond_eq0(s
, type
, arg0
, arg1
);
1018 if (TCG_TARGET_REG_BITS
== 64 && type
== TCG_TYPE_I32
) {
1019 tcg_out_ext32u(s
, TCG_REG_R0
, arg1
);
1022 tcg_out_setcond_ne0(s
, arg0
, arg1
);
1025 tcg_out32(s
, NOR
| SAB(arg1
, arg0
, arg1
));
1029 /* Extract the sign bit. */
1030 if (type
== TCG_TYPE_I32
) {
1031 tcg_out_shri32(s
, arg0
, arg1
, 31);
1033 tcg_out_shri64(s
, arg0
, arg1
, 63);
1041 /* If we have ISEL, we can implement everything with 3 or 4 insns.
1042 All other cases below are also at least 3 insns, so speed up the
1043 code generator by not considering them and always using ISEL. */
1047 tcg_out_cmp(s
, cond
, arg1
, arg2
, const_arg2
, 7, type
);
1049 isel
= tcg_to_isel
[cond
];
1051 tcg_out_movi(s
, type
, arg0
, 1);
1053 /* arg0 = (bc ? 0 : 1) */
1054 tab
= TAB(arg0
, 0, arg0
);
1057 /* arg0 = (bc ? 1 : 0) */
1058 tcg_out_movi(s
, type
, TCG_REG_R0
, 0);
1059 tab
= TAB(arg0
, arg0
, TCG_REG_R0
);
1061 tcg_out32(s
, isel
| tab
);
1067 arg1
= tcg_gen_setcond_xor(s
, arg1
, arg2
, const_arg2
);
1068 tcg_out_setcond_eq0(s
, type
, arg0
, arg1
);
1072 arg1
= tcg_gen_setcond_xor(s
, arg1
, arg2
, const_arg2
);
1073 /* Discard the high bits only once, rather than both inputs. */
1074 if (TCG_TARGET_REG_BITS
== 64 && type
== TCG_TYPE_I32
) {
1075 tcg_out_ext32u(s
, TCG_REG_R0
, arg1
);
1078 tcg_out_setcond_ne0(s
, arg0
, arg1
);
1096 crop
= CRNOR
| BT(7, CR_EQ
) | BA(7, CR_LT
) | BB(7, CR_LT
);
1102 crop
= CRNOR
| BT(7, CR_EQ
) | BA(7, CR_GT
) | BB(7, CR_GT
);
1104 tcg_out_cmp(s
, cond
, arg1
, arg2
, const_arg2
, 7, type
);
1108 tcg_out32(s
, MFOCRF
| RT(TCG_REG_R0
) | FXM(7));
1109 tcg_out_rlw(s
, RLWINM
, arg0
, TCG_REG_R0
, sh
, 31, 31);
1117 static void tcg_out_bc(TCGContext
*s
, int bc
, TCGLabel
*l
)
1120 tcg_out32(s
, bc
| reloc_pc14_val(s
->code_ptr
, l
->u
.value_ptr
));
1122 tcg_out_reloc(s
, s
->code_ptr
, R_PPC_REL14
, l
, 0);
1123 tcg_out_bc_noaddr(s
, bc
);
1127 static void tcg_out_brcond(TCGContext
*s
, TCGCond cond
,
1128 TCGArg arg1
, TCGArg arg2
, int const_arg2
,
1129 TCGLabel
*l
, TCGType type
)
1131 tcg_out_cmp(s
, cond
, arg1
, arg2
, const_arg2
, 7, type
);
1132 tcg_out_bc(s
, tcg_to_bc
[cond
], l
);
1135 static void tcg_out_movcond(TCGContext
*s
, TCGType type
, TCGCond cond
,
1136 TCGArg dest
, TCGArg c1
, TCGArg c2
, TCGArg v1
,
1137 TCGArg v2
, bool const_c2
)
1139 /* If for some reason both inputs are zero, don't produce bad code. */
1140 if (v1
== 0 && v2
== 0) {
1141 tcg_out_movi(s
, type
, dest
, 0);
1145 tcg_out_cmp(s
, cond
, c1
, c2
, const_c2
, 7, type
);
1148 int isel
= tcg_to_isel
[cond
];
1150 /* Swap the V operands if the operation indicates inversion. */
1157 /* V1 == 0 is handled by isel; V2 == 0 must be handled by hand. */
1159 tcg_out_movi(s
, type
, TCG_REG_R0
, 0);
1161 tcg_out32(s
, isel
| TAB(dest
, v1
, v2
));
1164 cond
= tcg_invert_cond(cond
);
1166 } else if (dest
!= v1
) {
1168 tcg_out_movi(s
, type
, dest
, 0);
1170 tcg_out_mov(s
, type
, dest
, v1
);
1173 /* Branch forward over one insn */
1174 tcg_out32(s
, tcg_to_bc
[cond
] | 8);
1176 tcg_out_movi(s
, type
, dest
, 0);
1178 tcg_out_mov(s
, type
, dest
, v2
);
1183 static void tcg_out_cntxz(TCGContext
*s
, TCGType type
, uint32_t opc
,
1184 TCGArg a0
, TCGArg a1
, TCGArg a2
, bool const_a2
)
1186 if (const_a2
&& a2
== (type
== TCG_TYPE_I32
? 32 : 64)) {
1187 tcg_out32(s
, opc
| RA(a0
) | RS(a1
));
1189 tcg_out_cmp(s
, TCG_COND_EQ
, a1
, 0, 1, 7, type
);
1190 /* Note that the only other valid constant for a2 is 0. */
1192 tcg_out32(s
, opc
| RA(TCG_REG_R0
) | RS(a1
));
1193 tcg_out32(s
, tcg_to_isel
[TCG_COND_EQ
] | TAB(a0
, a2
, TCG_REG_R0
));
1194 } else if (!const_a2
&& a0
== a2
) {
1195 tcg_out32(s
, tcg_to_bc
[TCG_COND_EQ
] | 8);
1196 tcg_out32(s
, opc
| RA(a0
) | RS(a1
));
1198 tcg_out32(s
, opc
| RA(a0
) | RS(a1
));
1199 tcg_out32(s
, tcg_to_bc
[TCG_COND_NE
] | 8);
1201 tcg_out_movi(s
, type
, a0
, 0);
1203 tcg_out_mov(s
, type
, a0
, a2
);
1209 static void tcg_out_cmp2(TCGContext
*s
, const TCGArg
*args
,
1210 const int *const_args
)
1212 static const struct { uint8_t bit1
, bit2
; } bits
[] = {
1213 [TCG_COND_LT
] = { CR_LT
, CR_LT
},
1214 [TCG_COND_LE
] = { CR_LT
, CR_GT
},
1215 [TCG_COND_GT
] = { CR_GT
, CR_GT
},
1216 [TCG_COND_GE
] = { CR_GT
, CR_LT
},
1217 [TCG_COND_LTU
] = { CR_LT
, CR_LT
},
1218 [TCG_COND_LEU
] = { CR_LT
, CR_GT
},
1219 [TCG_COND_GTU
] = { CR_GT
, CR_GT
},
1220 [TCG_COND_GEU
] = { CR_GT
, CR_LT
},
1223 TCGCond cond
= args
[4], cond2
;
1224 TCGArg al
, ah
, bl
, bh
;
1225 int blconst
, bhconst
;
1232 blconst
= const_args
[2];
1233 bhconst
= const_args
[3];
1242 tcg_out_cmp(s
, cond
, al
, bl
, blconst
, 6, TCG_TYPE_I32
);
1243 tcg_out_cmp(s
, cond
, ah
, bh
, bhconst
, 7, TCG_TYPE_I32
);
1244 tcg_out32(s
, op
| BT(7, CR_EQ
) | BA(6, CR_EQ
) | BB(7, CR_EQ
));
1255 bit1
= bits
[cond
].bit1
;
1256 bit2
= bits
[cond
].bit2
;
1257 op
= (bit1
!= bit2
? CRANDC
: CRAND
);
1258 cond2
= tcg_unsigned_cond(cond
);
1260 tcg_out_cmp(s
, cond
, ah
, bh
, bhconst
, 6, TCG_TYPE_I32
);
1261 tcg_out_cmp(s
, cond2
, al
, bl
, blconst
, 7, TCG_TYPE_I32
);
1262 tcg_out32(s
, op
| BT(7, CR_EQ
) | BA(6, CR_EQ
) | BB(7, bit2
));
1263 tcg_out32(s
, CROR
| BT(7, CR_EQ
) | BA(6, bit1
) | BB(7, CR_EQ
));
1271 static void tcg_out_setcond2(TCGContext
*s
, const TCGArg
*args
,
1272 const int *const_args
)
1274 tcg_out_cmp2(s
, args
+ 1, const_args
+ 1);
1275 tcg_out32(s
, MFOCRF
| RT(TCG_REG_R0
) | FXM(7));
1276 tcg_out_rlw(s
, RLWINM
, args
[0], TCG_REG_R0
, 31, 31, 31);
1279 static void tcg_out_brcond2 (TCGContext
*s
, const TCGArg
*args
,
1280 const int *const_args
)
1282 tcg_out_cmp2(s
, args
, const_args
);
1283 tcg_out_bc(s
, BC
| BI(7, CR_EQ
) | BO_COND_TRUE
, arg_label(args
[5]));
1286 static void tcg_out_mb(TCGContext
*s
, TCGArg a0
)
1288 uint32_t insn
= HWSYNC
;
1290 if (a0
== TCG_MO_LD_LD
) {
1292 } else if (a0
== TCG_MO_ST_ST
) {
1298 #ifdef __powerpc64__
1299 void ppc_tb_set_jmp_target(uintptr_t jmp_addr
, uintptr_t addr
)
1301 tcg_insn_unit i1
, i2
;
1303 intptr_t diff
= addr
- jmp_addr
;
1305 if (in_range_b(diff
)) {
1306 i1
= B
| (diff
& 0x3fffffc);
1308 } else if (USE_REG_RA
) {
1310 diff
= addr
- (uintptr_t)tb_ret_addr
;
1312 hi
= (int32_t)(diff
- lo
);
1313 tcg_debug_assert(diff
== hi
+ lo
);
1314 i1
= ADDIS
| TAI(TCG_REG_TMP1
, TCG_REG_RA
, hi
>> 16);
1315 i2
= ADDI
| TAI(TCG_REG_TMP1
, TCG_REG_TMP1
, lo
);
1317 tcg_debug_assert(TCG_TARGET_REG_BITS
== 32 || addr
== (int32_t)addr
);
1318 i1
= ADDIS
| TAI(TCG_REG_TMP1
, 0, addr
>> 16);
1319 i2
= ORI
| SAI(TCG_REG_TMP1
, TCG_REG_TMP1
, addr
);
1321 #ifdef HOST_WORDS_BIGENDIAN
1322 pair
= (uint64_t)i1
<< 32 | i2
;
1324 pair
= (uint64_t)i2
<< 32 | i1
;
1327 atomic_set((uint64_t *)jmp_addr
, pair
);
1328 flush_icache_range(jmp_addr
, jmp_addr
+ 8);
1331 void ppc_tb_set_jmp_target(uintptr_t jmp_addr
, uintptr_t addr
)
1333 intptr_t diff
= addr
- jmp_addr
;
1334 tcg_debug_assert(in_range_b(diff
));
1335 atomic_set((uint32_t *)jmp_addr
, B
| (diff
& 0x3fffffc));
1336 flush_icache_range(jmp_addr
, jmp_addr
+ 4);
1340 static void tcg_out_call(TCGContext
*s
, tcg_insn_unit
*target
)
1343 /* Look through the descriptor. If the branch is in range, and we
1344 don't have to spend too much effort on building the toc. */
1345 void *tgt
= ((void **)target
)[0];
1346 uintptr_t toc
= ((uintptr_t *)target
)[1];
1347 intptr_t diff
= tcg_pcrel_diff(s
, tgt
);
1349 if (in_range_b(diff
) && toc
== (uint32_t)toc
) {
1350 tcg_out_movi(s
, TCG_TYPE_PTR
, TCG_REG_TMP1
, toc
);
1351 tcg_out_b(s
, LK
, tgt
);
1353 /* Fold the low bits of the constant into the addresses below. */
1354 intptr_t arg
= (intptr_t)target
;
1355 int ofs
= (int16_t)arg
;
1357 if (ofs
+ 8 < 0x8000) {
1362 tcg_out_movi(s
, TCG_TYPE_PTR
, TCG_REG_TMP1
, arg
);
1363 tcg_out_ld(s
, TCG_TYPE_PTR
, TCG_REG_R0
, TCG_REG_TMP1
, ofs
);
1364 tcg_out32(s
, MTSPR
| RA(TCG_REG_R0
) | CTR
);
1365 tcg_out_ld(s
, TCG_TYPE_PTR
, TCG_REG_R2
, TCG_REG_TMP1
, ofs
+ SZP
);
1366 tcg_out32(s
, BCCTR
| BO_ALWAYS
| LK
);
1368 #elif defined(_CALL_ELF) && _CALL_ELF == 2
1371 /* In the ELFv2 ABI, we have to set up r12 to contain the destination
1372 address, which the callee uses to compute its TOC address. */
1373 /* FIXME: when the branch is in range, we could avoid r12 load if we
1374 knew that the destination uses the same TOC, and what its local
1375 entry point offset is. */
1376 tcg_out_movi(s
, TCG_TYPE_PTR
, TCG_REG_R12
, (intptr_t)target
);
1378 diff
= tcg_pcrel_diff(s
, target
);
1379 if (in_range_b(diff
)) {
1380 tcg_out_b(s
, LK
, target
);
1382 tcg_out32(s
, MTSPR
| RS(TCG_REG_R12
) | CTR
);
1383 tcg_out32(s
, BCCTR
| BO_ALWAYS
| LK
);
1386 tcg_out_b(s
, LK
, target
);
1390 static const uint32_t qemu_ldx_opc
[16] = {
1397 [MO_BSWAP
| MO_UB
] = LBZX
,
1398 [MO_BSWAP
| MO_UW
] = LHBRX
,
1399 [MO_BSWAP
| MO_UL
] = LWBRX
,
1400 [MO_BSWAP
| MO_Q
] = LDBRX
,
1403 static const uint32_t qemu_stx_opc
[16] = {
1408 [MO_BSWAP
| MO_UB
] = STBX
,
1409 [MO_BSWAP
| MO_UW
] = STHBRX
,
1410 [MO_BSWAP
| MO_UL
] = STWBRX
,
1411 [MO_BSWAP
| MO_Q
] = STDBRX
,
1414 static const uint32_t qemu_exts_opc
[4] = {
1415 EXTSB
, EXTSH
, EXTSW
, 0
1418 #if defined (CONFIG_SOFTMMU)
1419 /* helper signature: helper_ld_mmu(CPUState *env, target_ulong addr,
1420 * int mmu_idx, uintptr_t ra)
1422 static void * const qemu_ld_helpers
[16] = {
1423 [MO_UB
] = helper_ret_ldub_mmu
,
1424 [MO_LEUW
] = helper_le_lduw_mmu
,
1425 [MO_LEUL
] = helper_le_ldul_mmu
,
1426 [MO_LEQ
] = helper_le_ldq_mmu
,
1427 [MO_BEUW
] = helper_be_lduw_mmu
,
1428 [MO_BEUL
] = helper_be_ldul_mmu
,
1429 [MO_BEQ
] = helper_be_ldq_mmu
,
1432 /* helper signature: helper_st_mmu(CPUState *env, target_ulong addr,
1433 * uintxx_t val, int mmu_idx, uintptr_t ra)
1435 static void * const qemu_st_helpers
[16] = {
1436 [MO_UB
] = helper_ret_stb_mmu
,
1437 [MO_LEUW
] = helper_le_stw_mmu
,
1438 [MO_LEUL
] = helper_le_stl_mmu
,
1439 [MO_LEQ
] = helper_le_stq_mmu
,
1440 [MO_BEUW
] = helper_be_stw_mmu
,
1441 [MO_BEUL
] = helper_be_stl_mmu
,
1442 [MO_BEQ
] = helper_be_stq_mmu
,
1445 /* Perform the TLB load and compare. Places the result of the comparison
1446 in CR7, loads the addend of the TLB into R3, and returns the register
1447 containing the guest address (zero-extended into R4). Clobbers R0 and R2. */
1449 static TCGReg
tcg_out_tlb_read(TCGContext
*s
, TCGMemOp opc
,
1450 TCGReg addrlo
, TCGReg addrhi
,
1451 int mem_index
, bool is_read
)
1455 ? offsetof(CPUArchState
, tlb_table
[mem_index
][0].addr_read
)
1456 : offsetof(CPUArchState
, tlb_table
[mem_index
][0].addr_write
));
1457 int add_off
= offsetof(CPUArchState
, tlb_table
[mem_index
][0].addend
);
1458 TCGReg base
= TCG_AREG0
;
1459 unsigned s_bits
= opc
& MO_SIZE
;
1460 unsigned a_bits
= get_alignment_bits(opc
);
1462 /* Extract the page index, shifted into place for tlb index. */
1463 if (TCG_TARGET_REG_BITS
== 64) {
1464 if (TARGET_LONG_BITS
== 32) {
1465 /* Zero-extend the address into a place helpful for further use. */
1466 tcg_out_ext32u(s
, TCG_REG_R4
, addrlo
);
1467 addrlo
= TCG_REG_R4
;
1469 tcg_out_rld(s
, RLDICL
, TCG_REG_R3
, addrlo
,
1470 64 - TARGET_PAGE_BITS
, 64 - CPU_TLB_BITS
);
1474 /* Compensate for very large offsets. */
1475 if (add_off
>= 0x8000) {
1476 /* Most target env are smaller than 32k; none are larger than 64k.
1477 Simplify the logic here merely to offset by 0x7ff0, giving us a
1478 range just shy of 64k. Check this assumption. */
1479 QEMU_BUILD_BUG_ON(offsetof(CPUArchState
,
1480 tlb_table
[NB_MMU_MODES
- 1][1])
1482 tcg_out32(s
, ADDI
| TAI(TCG_REG_TMP1
, base
, 0x7ff0));
1483 base
= TCG_REG_TMP1
;
1488 /* Extraction and shifting, part 2. */
1489 if (TCG_TARGET_REG_BITS
== 32 || TARGET_LONG_BITS
== 32) {
1490 tcg_out_rlw(s
, RLWINM
, TCG_REG_R3
, addrlo
,
1491 32 - (TARGET_PAGE_BITS
- CPU_TLB_ENTRY_BITS
),
1492 32 - (CPU_TLB_BITS
+ CPU_TLB_ENTRY_BITS
),
1493 31 - CPU_TLB_ENTRY_BITS
);
1495 tcg_out_shli64(s
, TCG_REG_R3
, TCG_REG_R3
, CPU_TLB_ENTRY_BITS
);
1498 tcg_out32(s
, ADD
| TAB(TCG_REG_R3
, TCG_REG_R3
, base
));
1500 /* Load the tlb comparator. */
1501 if (TCG_TARGET_REG_BITS
< TARGET_LONG_BITS
) {
1502 tcg_out_ld(s
, TCG_TYPE_I32
, TCG_REG_R4
, TCG_REG_R3
, cmp_off
);
1503 tcg_out_ld(s
, TCG_TYPE_I32
, TCG_REG_TMP1
, TCG_REG_R3
, cmp_off
+ 4);
1505 tcg_out_ld(s
, TCG_TYPE_TL
, TCG_REG_TMP1
, TCG_REG_R3
, cmp_off
);
1508 /* Load the TLB addend for use on the fast path. Do this asap
1509 to minimize any load use delay. */
1510 tcg_out_ld(s
, TCG_TYPE_PTR
, TCG_REG_R3
, TCG_REG_R3
, add_off
);
1512 /* Clear the non-page, non-alignment bits from the address */
1513 if (TCG_TARGET_REG_BITS
== 32) {
1514 /* We don't support unaligned accesses on 32-bits.
1515 * Preserve the bottom bits and thus trigger a comparison
1516 * failure on unaligned accesses.
1518 if (a_bits
< s_bits
) {
1521 tcg_out_rlw(s
, RLWINM
, TCG_REG_R0
, addrlo
, 0,
1522 (32 - a_bits
) & 31, 31 - TARGET_PAGE_BITS
);
1526 /* If the access is unaligned, we need to make sure we fail if we
1527 * cross a page boundary. The trick is to add the access size-1
1528 * to the address before masking the low bits. That will make the
1529 * address overflow to the next page if we cross a page boundary,
1530 * which will then force a mismatch of the TLB compare.
1532 if (a_bits
< s_bits
) {
1533 unsigned a_mask
= (1 << a_bits
) - 1;
1534 unsigned s_mask
= (1 << s_bits
) - 1;
1535 tcg_out32(s
, ADDI
| TAI(TCG_REG_R0
, t
, s_mask
- a_mask
));
1539 /* Mask the address for the requested alignment. */
1540 if (TARGET_LONG_BITS
== 32) {
1541 tcg_out_rlw(s
, RLWINM
, TCG_REG_R0
, t
, 0,
1542 (32 - a_bits
) & 31, 31 - TARGET_PAGE_BITS
);
1543 } else if (a_bits
== 0) {
1544 tcg_out_rld(s
, RLDICR
, TCG_REG_R0
, t
, 0, 63 - TARGET_PAGE_BITS
);
1546 tcg_out_rld(s
, RLDICL
, TCG_REG_R0
, t
,
1547 64 - TARGET_PAGE_BITS
, TARGET_PAGE_BITS
- a_bits
);
1548 tcg_out_rld(s
, RLDICL
, TCG_REG_R0
, TCG_REG_R0
, TARGET_PAGE_BITS
, 0);
1552 if (TCG_TARGET_REG_BITS
< TARGET_LONG_BITS
) {
1553 tcg_out_cmp(s
, TCG_COND_EQ
, TCG_REG_R0
, TCG_REG_TMP1
,
1554 0, 7, TCG_TYPE_I32
);
1555 tcg_out_cmp(s
, TCG_COND_EQ
, addrhi
, TCG_REG_R4
, 0, 6, TCG_TYPE_I32
);
1556 tcg_out32(s
, CRAND
| BT(7, CR_EQ
) | BA(6, CR_EQ
) | BB(7, CR_EQ
));
1558 tcg_out_cmp(s
, TCG_COND_EQ
, TCG_REG_R0
, TCG_REG_TMP1
,
1565 /* Record the context of a call to the out of line helper code for the slow
1566 path for a load or store, so that we can later generate the correct
1568 static void add_qemu_ldst_label(TCGContext
*s
, bool is_ld
, TCGMemOpIdx oi
,
1569 TCGReg datalo_reg
, TCGReg datahi_reg
,
1570 TCGReg addrlo_reg
, TCGReg addrhi_reg
,
1571 tcg_insn_unit
*raddr
, tcg_insn_unit
*lptr
)
1573 TCGLabelQemuLdst
*label
= new_ldst_label(s
);
1575 label
->is_ld
= is_ld
;
1577 label
->datalo_reg
= datalo_reg
;
1578 label
->datahi_reg
= datahi_reg
;
1579 label
->addrlo_reg
= addrlo_reg
;
1580 label
->addrhi_reg
= addrhi_reg
;
1581 label
->raddr
= raddr
;
1582 label
->label_ptr
[0] = lptr
;
1585 static void tcg_out_qemu_ld_slow_path(TCGContext
*s
, TCGLabelQemuLdst
*lb
)
1587 TCGMemOpIdx oi
= lb
->oi
;
1588 TCGMemOp opc
= get_memop(oi
);
1589 TCGReg hi
, lo
, arg
= TCG_REG_R3
;
1591 reloc_pc14(lb
->label_ptr
[0], s
->code_ptr
);
1593 tcg_out_mov(s
, TCG_TYPE_PTR
, arg
++, TCG_AREG0
);
1595 lo
= lb
->addrlo_reg
;
1596 hi
= lb
->addrhi_reg
;
1597 if (TCG_TARGET_REG_BITS
< TARGET_LONG_BITS
) {
1598 #ifdef TCG_TARGET_CALL_ALIGN_ARGS
1601 tcg_out_mov(s
, TCG_TYPE_I32
, arg
++, hi
);
1602 tcg_out_mov(s
, TCG_TYPE_I32
, arg
++, lo
);
1604 /* If the address needed to be zero-extended, we'll have already
1605 placed it in R4. The only remaining case is 64-bit guest. */
1606 tcg_out_mov(s
, TCG_TYPE_TL
, arg
++, lo
);
1609 tcg_out_movi(s
, TCG_TYPE_I32
, arg
++, oi
);
1610 tcg_out32(s
, MFSPR
| RT(arg
) | LR
);
1612 tcg_out_call(s
, qemu_ld_helpers
[opc
& (MO_BSWAP
| MO_SIZE
)]);
1614 lo
= lb
->datalo_reg
;
1615 hi
= lb
->datahi_reg
;
1616 if (TCG_TARGET_REG_BITS
== 32 && (opc
& MO_SIZE
) == MO_64
) {
1617 tcg_out_mov(s
, TCG_TYPE_I32
, lo
, TCG_REG_R4
);
1618 tcg_out_mov(s
, TCG_TYPE_I32
, hi
, TCG_REG_R3
);
1619 } else if (opc
& MO_SIGN
) {
1620 uint32_t insn
= qemu_exts_opc
[opc
& MO_SIZE
];
1621 tcg_out32(s
, insn
| RA(lo
) | RS(TCG_REG_R3
));
1623 tcg_out_mov(s
, TCG_TYPE_REG
, lo
, TCG_REG_R3
);
1626 tcg_out_b(s
, 0, lb
->raddr
);
1629 static void tcg_out_qemu_st_slow_path(TCGContext
*s
, TCGLabelQemuLdst
*lb
)
1631 TCGMemOpIdx oi
= lb
->oi
;
1632 TCGMemOp opc
= get_memop(oi
);
1633 TCGMemOp s_bits
= opc
& MO_SIZE
;
1634 TCGReg hi
, lo
, arg
= TCG_REG_R3
;
1636 reloc_pc14(lb
->label_ptr
[0], s
->code_ptr
);
1638 tcg_out_mov(s
, TCG_TYPE_PTR
, arg
++, TCG_AREG0
);
1640 lo
= lb
->addrlo_reg
;
1641 hi
= lb
->addrhi_reg
;
1642 if (TCG_TARGET_REG_BITS
< TARGET_LONG_BITS
) {
1643 #ifdef TCG_TARGET_CALL_ALIGN_ARGS
1646 tcg_out_mov(s
, TCG_TYPE_I32
, arg
++, hi
);
1647 tcg_out_mov(s
, TCG_TYPE_I32
, arg
++, lo
);
1649 /* If the address needed to be zero-extended, we'll have already
1650 placed it in R4. The only remaining case is 64-bit guest. */
1651 tcg_out_mov(s
, TCG_TYPE_TL
, arg
++, lo
);
1654 lo
= lb
->datalo_reg
;
1655 hi
= lb
->datahi_reg
;
1656 if (TCG_TARGET_REG_BITS
== 32) {
1659 #ifdef TCG_TARGET_CALL_ALIGN_ARGS
1662 tcg_out_mov(s
, TCG_TYPE_I32
, arg
++, hi
);
1665 tcg_out_mov(s
, TCG_TYPE_I32
, arg
++, lo
);
1668 tcg_out_rlw(s
, RLWINM
, arg
++, lo
, 0, 32 - (8 << s_bits
), 31);
1672 if (s_bits
== MO_64
) {
1673 tcg_out_mov(s
, TCG_TYPE_I64
, arg
++, lo
);
1675 tcg_out_rld(s
, RLDICL
, arg
++, lo
, 0, 64 - (8 << s_bits
));
1679 tcg_out_movi(s
, TCG_TYPE_I32
, arg
++, oi
);
1680 tcg_out32(s
, MFSPR
| RT(arg
) | LR
);
1682 tcg_out_call(s
, qemu_st_helpers
[opc
& (MO_BSWAP
| MO_SIZE
)]);
1684 tcg_out_b(s
, 0, lb
->raddr
);
1686 #endif /* SOFTMMU */
1688 static void tcg_out_qemu_ld(TCGContext
*s
, const TCGArg
*args
, bool is_64
)
1690 TCGReg datalo
, datahi
, addrlo
, rbase
;
1691 TCGReg addrhi
__attribute__((unused
));
1693 TCGMemOp opc
, s_bits
;
1694 #ifdef CONFIG_SOFTMMU
1696 tcg_insn_unit
*label_ptr
;
1700 datahi
= (TCG_TARGET_REG_BITS
== 32 && is_64
? *args
++ : 0);
1702 addrhi
= (TCG_TARGET_REG_BITS
< TARGET_LONG_BITS
? *args
++ : 0);
1704 opc
= get_memop(oi
);
1705 s_bits
= opc
& MO_SIZE
;
1707 #ifdef CONFIG_SOFTMMU
1708 mem_index
= get_mmuidx(oi
);
1709 addrlo
= tcg_out_tlb_read(s
, opc
, addrlo
, addrhi
, mem_index
, true);
1711 /* Load a pointer into the current opcode w/conditional branch-link. */
1712 label_ptr
= s
->code_ptr
;
1713 tcg_out_bc_noaddr(s
, BC
| BI(7, CR_EQ
) | BO_COND_FALSE
| LK
);
1716 #else /* !CONFIG_SOFTMMU */
1717 rbase
= guest_base
? TCG_GUEST_BASE_REG
: 0;
1718 if (TCG_TARGET_REG_BITS
> TARGET_LONG_BITS
) {
1719 tcg_out_ext32u(s
, TCG_REG_TMP1
, addrlo
);
1720 addrlo
= TCG_REG_TMP1
;
1724 if (TCG_TARGET_REG_BITS
== 32 && s_bits
== MO_64
) {
1725 if (opc
& MO_BSWAP
) {
1726 tcg_out32(s
, ADDI
| TAI(TCG_REG_R0
, addrlo
, 4));
1727 tcg_out32(s
, LWBRX
| TAB(datalo
, rbase
, addrlo
));
1728 tcg_out32(s
, LWBRX
| TAB(datahi
, rbase
, TCG_REG_R0
));
1729 } else if (rbase
!= 0) {
1730 tcg_out32(s
, ADDI
| TAI(TCG_REG_R0
, addrlo
, 4));
1731 tcg_out32(s
, LWZX
| TAB(datahi
, rbase
, addrlo
));
1732 tcg_out32(s
, LWZX
| TAB(datalo
, rbase
, TCG_REG_R0
));
1733 } else if (addrlo
== datahi
) {
1734 tcg_out32(s
, LWZ
| TAI(datalo
, addrlo
, 4));
1735 tcg_out32(s
, LWZ
| TAI(datahi
, addrlo
, 0));
1737 tcg_out32(s
, LWZ
| TAI(datahi
, addrlo
, 0));
1738 tcg_out32(s
, LWZ
| TAI(datalo
, addrlo
, 4));
1741 uint32_t insn
= qemu_ldx_opc
[opc
& (MO_BSWAP
| MO_SSIZE
)];
1742 if (!HAVE_ISA_2_06
&& insn
== LDBRX
) {
1743 tcg_out32(s
, ADDI
| TAI(TCG_REG_R0
, addrlo
, 4));
1744 tcg_out32(s
, LWBRX
| TAB(datalo
, rbase
, addrlo
));
1745 tcg_out32(s
, LWBRX
| TAB(TCG_REG_R0
, rbase
, TCG_REG_R0
));
1746 tcg_out_rld(s
, RLDIMI
, datalo
, TCG_REG_R0
, 32, 0);
1748 tcg_out32(s
, insn
| TAB(datalo
, rbase
, addrlo
));
1750 insn
= qemu_ldx_opc
[opc
& (MO_SIZE
| MO_BSWAP
)];
1751 tcg_out32(s
, insn
| TAB(datalo
, rbase
, addrlo
));
1752 insn
= qemu_exts_opc
[s_bits
];
1753 tcg_out32(s
, insn
| RA(datalo
) | RS(datalo
));
1757 #ifdef CONFIG_SOFTMMU
1758 add_qemu_ldst_label(s
, true, oi
, datalo
, datahi
, addrlo
, addrhi
,
1759 s
->code_ptr
, label_ptr
);
1763 static void tcg_out_qemu_st(TCGContext
*s
, const TCGArg
*args
, bool is_64
)
1765 TCGReg datalo
, datahi
, addrlo
, rbase
;
1766 TCGReg addrhi
__attribute__((unused
));
1768 TCGMemOp opc
, s_bits
;
1769 #ifdef CONFIG_SOFTMMU
1771 tcg_insn_unit
*label_ptr
;
1775 datahi
= (TCG_TARGET_REG_BITS
== 32 && is_64
? *args
++ : 0);
1777 addrhi
= (TCG_TARGET_REG_BITS
< TARGET_LONG_BITS
? *args
++ : 0);
1779 opc
= get_memop(oi
);
1780 s_bits
= opc
& MO_SIZE
;
1782 #ifdef CONFIG_SOFTMMU
1783 mem_index
= get_mmuidx(oi
);
1784 addrlo
= tcg_out_tlb_read(s
, opc
, addrlo
, addrhi
, mem_index
, false);
1786 /* Load a pointer into the current opcode w/conditional branch-link. */
1787 label_ptr
= s
->code_ptr
;
1788 tcg_out_bc_noaddr(s
, BC
| BI(7, CR_EQ
) | BO_COND_FALSE
| LK
);
1791 #else /* !CONFIG_SOFTMMU */
1792 rbase
= guest_base
? TCG_GUEST_BASE_REG
: 0;
1793 if (TCG_TARGET_REG_BITS
> TARGET_LONG_BITS
) {
1794 tcg_out_ext32u(s
, TCG_REG_TMP1
, addrlo
);
1795 addrlo
= TCG_REG_TMP1
;
1799 if (TCG_TARGET_REG_BITS
== 32 && s_bits
== MO_64
) {
1800 if (opc
& MO_BSWAP
) {
1801 tcg_out32(s
, ADDI
| TAI(TCG_REG_R0
, addrlo
, 4));
1802 tcg_out32(s
, STWBRX
| SAB(datalo
, rbase
, addrlo
));
1803 tcg_out32(s
, STWBRX
| SAB(datahi
, rbase
, TCG_REG_R0
));
1804 } else if (rbase
!= 0) {
1805 tcg_out32(s
, ADDI
| TAI(TCG_REG_R0
, addrlo
, 4));
1806 tcg_out32(s
, STWX
| SAB(datahi
, rbase
, addrlo
));
1807 tcg_out32(s
, STWX
| SAB(datalo
, rbase
, TCG_REG_R0
));
1809 tcg_out32(s
, STW
| TAI(datahi
, addrlo
, 0));
1810 tcg_out32(s
, STW
| TAI(datalo
, addrlo
, 4));
1813 uint32_t insn
= qemu_stx_opc
[opc
& (MO_BSWAP
| MO_SIZE
)];
1814 if (!HAVE_ISA_2_06
&& insn
== STDBRX
) {
1815 tcg_out32(s
, STWBRX
| SAB(datalo
, rbase
, addrlo
));
1816 tcg_out32(s
, ADDI
| TAI(TCG_REG_TMP1
, addrlo
, 4));
1817 tcg_out_shri64(s
, TCG_REG_R0
, datalo
, 32);
1818 tcg_out32(s
, STWBRX
| SAB(TCG_REG_R0
, rbase
, TCG_REG_TMP1
));
1820 tcg_out32(s
, insn
| SAB(datalo
, rbase
, addrlo
));
1824 #ifdef CONFIG_SOFTMMU
1825 add_qemu_ldst_label(s
, false, oi
, datalo
, datahi
, addrlo
, addrhi
,
1826 s
->code_ptr
, label_ptr
);
1830 /* Parameters for function call generation, used in tcg.c. */
1831 #define TCG_TARGET_STACK_ALIGN 16
1832 #define TCG_TARGET_EXTEND_ARGS 1
1835 # define LINK_AREA_SIZE (6 * SZR)
1836 # define LR_OFFSET (1 * SZR)
1837 # define TCG_TARGET_CALL_STACK_OFFSET (LINK_AREA_SIZE + 8 * SZR)
1838 #elif defined(TCG_TARGET_CALL_DARWIN)
1839 # define LINK_AREA_SIZE (6 * SZR)
1840 # define LR_OFFSET (2 * SZR)
1841 #elif TCG_TARGET_REG_BITS == 64
1842 # if defined(_CALL_ELF) && _CALL_ELF == 2
1843 # define LINK_AREA_SIZE (4 * SZR)
1844 # define LR_OFFSET (1 * SZR)
1846 #else /* TCG_TARGET_REG_BITS == 32 */
1847 # if defined(_CALL_SYSV)
1848 # define LINK_AREA_SIZE (2 * SZR)
1849 # define LR_OFFSET (1 * SZR)
1853 # error "Unhandled abi"
1855 #ifndef TCG_TARGET_CALL_STACK_OFFSET
1856 # define TCG_TARGET_CALL_STACK_OFFSET LINK_AREA_SIZE
1859 #define CPU_TEMP_BUF_SIZE (CPU_TEMP_BUF_NLONGS * (int)sizeof(long))
1860 #define REG_SAVE_SIZE ((int)ARRAY_SIZE(tcg_target_callee_save_regs) * SZR)
1862 #define FRAME_SIZE ((TCG_TARGET_CALL_STACK_OFFSET \
1863 + TCG_STATIC_CALL_ARGS_SIZE \
1864 + CPU_TEMP_BUF_SIZE \
1866 + TCG_TARGET_STACK_ALIGN - 1) \
1867 & -TCG_TARGET_STACK_ALIGN)
1869 #define REG_SAVE_BOT (FRAME_SIZE - REG_SAVE_SIZE)
1871 static void tcg_target_qemu_prologue(TCGContext
*s
)
1876 void **desc
= (void **)s
->code_ptr
;
1877 desc
[0] = desc
+ 2; /* entry point */
1878 desc
[1] = 0; /* environment pointer */
1879 s
->code_ptr
= (void *)(desc
+ 2); /* skip over descriptor */
1882 tcg_set_frame(s
, TCG_REG_CALL_STACK
, REG_SAVE_BOT
- CPU_TEMP_BUF_SIZE
,
1886 tcg_out32(s
, MFSPR
| RT(TCG_REG_R0
) | LR
);
1887 tcg_out32(s
, (SZR
== 8 ? STDU
: STWU
)
1888 | SAI(TCG_REG_R1
, TCG_REG_R1
, -FRAME_SIZE
));
1890 for (i
= 0; i
< ARRAY_SIZE(tcg_target_callee_save_regs
); ++i
) {
1891 tcg_out_st(s
, TCG_TYPE_REG
, tcg_target_callee_save_regs
[i
],
1892 TCG_REG_R1
, REG_SAVE_BOT
+ i
* SZR
);
1894 tcg_out_st(s
, TCG_TYPE_PTR
, TCG_REG_R0
, TCG_REG_R1
, FRAME_SIZE
+LR_OFFSET
);
1896 #ifndef CONFIG_SOFTMMU
1898 tcg_out_movi(s
, TCG_TYPE_PTR
, TCG_GUEST_BASE_REG
, guest_base
);
1899 tcg_regset_set_reg(s
->reserved_regs
, TCG_GUEST_BASE_REG
);
1903 tcg_out_mov(s
, TCG_TYPE_PTR
, TCG_AREG0
, tcg_target_call_iarg_regs
[0]);
1904 tcg_out32(s
, MTSPR
| RS(tcg_target_call_iarg_regs
[1]) | CTR
);
1908 /* Make the caller load the value as the TOC into R2. */
1909 tb_ret_addr
= s
->code_ptr
+ 2;
1910 desc
[1] = tb_ret_addr
;
1911 tcg_out_mov(s
, TCG_TYPE_PTR
, TCG_REG_RA
, TCG_REG_R2
);
1912 tcg_out32(s
, BCCTR
| BO_ALWAYS
);
1913 #elif defined(_CALL_ELF) && _CALL_ELF == 2
1914 /* Compute from the incoming R12 value. */
1915 tb_ret_addr
= s
->code_ptr
+ 2;
1916 tcg_out32(s
, ADDI
| TAI(TCG_REG_RA
, TCG_REG_R12
,
1917 tcg_ptr_byte_diff(tb_ret_addr
, s
->code_buf
)));
1918 tcg_out32(s
, BCCTR
| BO_ALWAYS
);
1920 /* Reserve max 5 insns for the constant load. */
1921 tb_ret_addr
= s
->code_ptr
+ 6;
1922 tcg_out_movi(s
, TCG_TYPE_PTR
, TCG_REG_RA
, (intptr_t)tb_ret_addr
);
1923 tcg_out32(s
, BCCTR
| BO_ALWAYS
);
1924 while (s
->code_ptr
< tb_ret_addr
) {
1929 tcg_out32(s
, BCCTR
| BO_ALWAYS
);
1930 tb_ret_addr
= s
->code_ptr
;
1934 tcg_debug_assert(tb_ret_addr
== s
->code_ptr
);
1936 tcg_out_ld(s
, TCG_TYPE_PTR
, TCG_REG_R0
, TCG_REG_R1
, FRAME_SIZE
+LR_OFFSET
);
1937 for (i
= 0; i
< ARRAY_SIZE(tcg_target_callee_save_regs
); ++i
) {
1938 tcg_out_ld(s
, TCG_TYPE_REG
, tcg_target_callee_save_regs
[i
],
1939 TCG_REG_R1
, REG_SAVE_BOT
+ i
* SZR
);
1941 tcg_out32(s
, MTSPR
| RS(TCG_REG_R0
) | LR
);
1942 tcg_out32(s
, ADDI
| TAI(TCG_REG_R1
, TCG_REG_R1
, FRAME_SIZE
));
1943 tcg_out32(s
, BCLR
| BO_ALWAYS
);
1946 static void tcg_out_op(TCGContext
*s
, TCGOpcode opc
, const TCGArg
*args
,
1947 const int *const_args
)
1953 case INDEX_op_exit_tb
:
1955 ptrdiff_t disp
= tcg_pcrel_diff(s
, tb_ret_addr
);
1957 /* Use a direct branch if we can, otherwise use the value in RA.
1958 Note that the direct branch is always backward, thus we need
1959 to account for the possibility of 5 insns from the movi. */
1960 if (!in_range_b(disp
- 20)) {
1961 tcg_out32(s
, MTSPR
| RS(TCG_REG_RA
) | CTR
);
1962 tcg_out_movi(s
, TCG_TYPE_PTR
, TCG_REG_R3
, args
[0]);
1963 tcg_out32(s
, BCCTR
| BO_ALWAYS
);
1967 tcg_out_movi(s
, TCG_TYPE_PTR
, TCG_REG_R3
, args
[0]);
1968 tcg_out_b(s
, 0, tb_ret_addr
);
1970 case INDEX_op_goto_tb
:
1971 tcg_debug_assert(s
->tb_jmp_insn_offset
);
1973 #ifdef __powerpc64__
1974 /* Ensure the next insns are 8-byte aligned. */
1975 if ((uintptr_t)s
->code_ptr
& 7) {
1978 s
->tb_jmp_insn_offset
[args
[0]] = tcg_current_code_size(s
);
1979 /* To be replaced by either a branch+nop or a load into TMP1. */
1981 tcg_out32(s
, MTSPR
| RS(TCG_REG_TMP1
) | CTR
);
1982 tcg_out32(s
, BCCTR
| BO_ALWAYS
);
1984 /* To be replaced by a branch. */
1987 s
->tb_jmp_reset_offset
[args
[0]] = tcg_current_code_size(s
);
1991 TCGLabel
*l
= arg_label(args
[0]);
1994 tcg_out_b(s
, 0, l
->u
.value_ptr
);
1996 tcg_out_reloc(s
, s
->code_ptr
, R_PPC_REL24
, l
, 0);
1997 tcg_out_b_noaddr(s
, B
);
2001 case INDEX_op_ld8u_i32
:
2002 case INDEX_op_ld8u_i64
:
2003 tcg_out_mem_long(s
, LBZ
, LBZX
, args
[0], args
[1], args
[2]);
2005 case INDEX_op_ld8s_i32
:
2006 case INDEX_op_ld8s_i64
:
2007 tcg_out_mem_long(s
, LBZ
, LBZX
, args
[0], args
[1], args
[2]);
2008 tcg_out32(s
, EXTSB
| RS(args
[0]) | RA(args
[0]));
2010 case INDEX_op_ld16u_i32
:
2011 case INDEX_op_ld16u_i64
:
2012 tcg_out_mem_long(s
, LHZ
, LHZX
, args
[0], args
[1], args
[2]);
2014 case INDEX_op_ld16s_i32
:
2015 case INDEX_op_ld16s_i64
:
2016 tcg_out_mem_long(s
, LHA
, LHAX
, args
[0], args
[1], args
[2]);
2018 case INDEX_op_ld_i32
:
2019 case INDEX_op_ld32u_i64
:
2020 tcg_out_mem_long(s
, LWZ
, LWZX
, args
[0], args
[1], args
[2]);
2022 case INDEX_op_ld32s_i64
:
2023 tcg_out_mem_long(s
, LWA
, LWAX
, args
[0], args
[1], args
[2]);
2025 case INDEX_op_ld_i64
:
2026 tcg_out_mem_long(s
, LD
, LDX
, args
[0], args
[1], args
[2]);
2028 case INDEX_op_st8_i32
:
2029 case INDEX_op_st8_i64
:
2030 tcg_out_mem_long(s
, STB
, STBX
, args
[0], args
[1], args
[2]);
2032 case INDEX_op_st16_i32
:
2033 case INDEX_op_st16_i64
:
2034 tcg_out_mem_long(s
, STH
, STHX
, args
[0], args
[1], args
[2]);
2036 case INDEX_op_st_i32
:
2037 case INDEX_op_st32_i64
:
2038 tcg_out_mem_long(s
, STW
, STWX
, args
[0], args
[1], args
[2]);
2040 case INDEX_op_st_i64
:
2041 tcg_out_mem_long(s
, STD
, STDX
, args
[0], args
[1], args
[2]);
2044 case INDEX_op_add_i32
:
2045 a0
= args
[0], a1
= args
[1], a2
= args
[2];
2046 if (const_args
[2]) {
2048 tcg_out_mem_long(s
, ADDI
, ADD
, a0
, a1
, (int32_t)a2
);
2050 tcg_out32(s
, ADD
| TAB(a0
, a1
, a2
));
2053 case INDEX_op_sub_i32
:
2054 a0
= args
[0], a1
= args
[1], a2
= args
[2];
2055 if (const_args
[1]) {
2056 if (const_args
[2]) {
2057 tcg_out_movi(s
, TCG_TYPE_I32
, a0
, a1
- a2
);
2059 tcg_out32(s
, SUBFIC
| TAI(a0
, a2
, a1
));
2061 } else if (const_args
[2]) {
2065 tcg_out32(s
, SUBF
| TAB(a0
, a2
, a1
));
2069 case INDEX_op_and_i32
:
2070 a0
= args
[0], a1
= args
[1], a2
= args
[2];
2071 if (const_args
[2]) {
2072 tcg_out_andi32(s
, a0
, a1
, a2
);
2074 tcg_out32(s
, AND
| SAB(a1
, a0
, a2
));
2077 case INDEX_op_and_i64
:
2078 a0
= args
[0], a1
= args
[1], a2
= args
[2];
2079 if (const_args
[2]) {
2080 tcg_out_andi64(s
, a0
, a1
, a2
);
2082 tcg_out32(s
, AND
| SAB(a1
, a0
, a2
));
2085 case INDEX_op_or_i64
:
2086 case INDEX_op_or_i32
:
2087 a0
= args
[0], a1
= args
[1], a2
= args
[2];
2088 if (const_args
[2]) {
2089 tcg_out_ori32(s
, a0
, a1
, a2
);
2091 tcg_out32(s
, OR
| SAB(a1
, a0
, a2
));
2094 case INDEX_op_xor_i64
:
2095 case INDEX_op_xor_i32
:
2096 a0
= args
[0], a1
= args
[1], a2
= args
[2];
2097 if (const_args
[2]) {
2098 tcg_out_xori32(s
, a0
, a1
, a2
);
2100 tcg_out32(s
, XOR
| SAB(a1
, a0
, a2
));
2103 case INDEX_op_andc_i32
:
2104 a0
= args
[0], a1
= args
[1], a2
= args
[2];
2105 if (const_args
[2]) {
2106 tcg_out_andi32(s
, a0
, a1
, ~a2
);
2108 tcg_out32(s
, ANDC
| SAB(a1
, a0
, a2
));
2111 case INDEX_op_andc_i64
:
2112 a0
= args
[0], a1
= args
[1], a2
= args
[2];
2113 if (const_args
[2]) {
2114 tcg_out_andi64(s
, a0
, a1
, ~a2
);
2116 tcg_out32(s
, ANDC
| SAB(a1
, a0
, a2
));
2119 case INDEX_op_orc_i32
:
2120 if (const_args
[2]) {
2121 tcg_out_ori32(s
, args
[0], args
[1], ~args
[2]);
2125 case INDEX_op_orc_i64
:
2126 tcg_out32(s
, ORC
| SAB(args
[1], args
[0], args
[2]));
2128 case INDEX_op_eqv_i32
:
2129 if (const_args
[2]) {
2130 tcg_out_xori32(s
, args
[0], args
[1], ~args
[2]);
2134 case INDEX_op_eqv_i64
:
2135 tcg_out32(s
, EQV
| SAB(args
[1], args
[0], args
[2]));
2137 case INDEX_op_nand_i32
:
2138 case INDEX_op_nand_i64
:
2139 tcg_out32(s
, NAND
| SAB(args
[1], args
[0], args
[2]));
2141 case INDEX_op_nor_i32
:
2142 case INDEX_op_nor_i64
:
2143 tcg_out32(s
, NOR
| SAB(args
[1], args
[0], args
[2]));
2146 case INDEX_op_clz_i32
:
2147 tcg_out_cntxz(s
, TCG_TYPE_I32
, CNTLZW
, args
[0], args
[1],
2148 args
[2], const_args
[2]);
2150 case INDEX_op_ctz_i32
:
2151 tcg_out_cntxz(s
, TCG_TYPE_I32
, CNTTZW
, args
[0], args
[1],
2152 args
[2], const_args
[2]);
2154 case INDEX_op_ctpop_i32
:
2155 tcg_out32(s
, CNTPOPW
| SAB(args
[1], args
[0], 0));
2158 case INDEX_op_clz_i64
:
2159 tcg_out_cntxz(s
, TCG_TYPE_I64
, CNTLZD
, args
[0], args
[1],
2160 args
[2], const_args
[2]);
2162 case INDEX_op_ctz_i64
:
2163 tcg_out_cntxz(s
, TCG_TYPE_I64
, CNTTZD
, args
[0], args
[1],
2164 args
[2], const_args
[2]);
2166 case INDEX_op_ctpop_i64
:
2167 tcg_out32(s
, CNTPOPD
| SAB(args
[1], args
[0], 0));
2170 case INDEX_op_mul_i32
:
2171 a0
= args
[0], a1
= args
[1], a2
= args
[2];
2172 if (const_args
[2]) {
2173 tcg_out32(s
, MULLI
| TAI(a0
, a1
, a2
));
2175 tcg_out32(s
, MULLW
| TAB(a0
, a1
, a2
));
2179 case INDEX_op_div_i32
:
2180 tcg_out32(s
, DIVW
| TAB(args
[0], args
[1], args
[2]));
2183 case INDEX_op_divu_i32
:
2184 tcg_out32(s
, DIVWU
| TAB(args
[0], args
[1], args
[2]));
2187 case INDEX_op_shl_i32
:
2188 if (const_args
[2]) {
2189 tcg_out_shli32(s
, args
[0], args
[1], args
[2]);
2191 tcg_out32(s
, SLW
| SAB(args
[1], args
[0], args
[2]));
2194 case INDEX_op_shr_i32
:
2195 if (const_args
[2]) {
2196 tcg_out_shri32(s
, args
[0], args
[1], args
[2]);
2198 tcg_out32(s
, SRW
| SAB(args
[1], args
[0], args
[2]));
2201 case INDEX_op_sar_i32
:
2202 if (const_args
[2]) {
2203 tcg_out32(s
, SRAWI
| RS(args
[1]) | RA(args
[0]) | SH(args
[2]));
2205 tcg_out32(s
, SRAW
| SAB(args
[1], args
[0], args
[2]));
2208 case INDEX_op_rotl_i32
:
2209 if (const_args
[2]) {
2210 tcg_out_rlw(s
, RLWINM
, args
[0], args
[1], args
[2], 0, 31);
2212 tcg_out32(s
, RLWNM
| SAB(args
[1], args
[0], args
[2])
2216 case INDEX_op_rotr_i32
:
2217 if (const_args
[2]) {
2218 tcg_out_rlw(s
, RLWINM
, args
[0], args
[1], 32 - args
[2], 0, 31);
2220 tcg_out32(s
, SUBFIC
| TAI(TCG_REG_R0
, args
[2], 32));
2221 tcg_out32(s
, RLWNM
| SAB(args
[1], args
[0], TCG_REG_R0
)
2226 case INDEX_op_brcond_i32
:
2227 tcg_out_brcond(s
, args
[2], args
[0], args
[1], const_args
[1],
2228 arg_label(args
[3]), TCG_TYPE_I32
);
2230 case INDEX_op_brcond_i64
:
2231 tcg_out_brcond(s
, args
[2], args
[0], args
[1], const_args
[1],
2232 arg_label(args
[3]), TCG_TYPE_I64
);
2234 case INDEX_op_brcond2_i32
:
2235 tcg_out_brcond2(s
, args
, const_args
);
2238 case INDEX_op_neg_i32
:
2239 case INDEX_op_neg_i64
:
2240 tcg_out32(s
, NEG
| RT(args
[0]) | RA(args
[1]));
2243 case INDEX_op_not_i32
:
2244 case INDEX_op_not_i64
:
2245 tcg_out32(s
, NOR
| SAB(args
[1], args
[0], args
[1]));
2248 case INDEX_op_add_i64
:
2249 a0
= args
[0], a1
= args
[1], a2
= args
[2];
2250 if (const_args
[2]) {
2252 tcg_out_mem_long(s
, ADDI
, ADD
, a0
, a1
, a2
);
2254 tcg_out32(s
, ADD
| TAB(a0
, a1
, a2
));
2257 case INDEX_op_sub_i64
:
2258 a0
= args
[0], a1
= args
[1], a2
= args
[2];
2259 if (const_args
[1]) {
2260 if (const_args
[2]) {
2261 tcg_out_movi(s
, TCG_TYPE_I64
, a0
, a1
- a2
);
2263 tcg_out32(s
, SUBFIC
| TAI(a0
, a2
, a1
));
2265 } else if (const_args
[2]) {
2269 tcg_out32(s
, SUBF
| TAB(a0
, a2
, a1
));
2273 case INDEX_op_shl_i64
:
2274 if (const_args
[2]) {
2275 tcg_out_shli64(s
, args
[0], args
[1], args
[2]);
2277 tcg_out32(s
, SLD
| SAB(args
[1], args
[0], args
[2]));
2280 case INDEX_op_shr_i64
:
2281 if (const_args
[2]) {
2282 tcg_out_shri64(s
, args
[0], args
[1], args
[2]);
2284 tcg_out32(s
, SRD
| SAB(args
[1], args
[0], args
[2]));
2287 case INDEX_op_sar_i64
:
2288 if (const_args
[2]) {
2289 int sh
= SH(args
[2] & 0x1f) | (((args
[2] >> 5) & 1) << 1);
2290 tcg_out32(s
, SRADI
| RA(args
[0]) | RS(args
[1]) | sh
);
2292 tcg_out32(s
, SRAD
| SAB(args
[1], args
[0], args
[2]));
2295 case INDEX_op_rotl_i64
:
2296 if (const_args
[2]) {
2297 tcg_out_rld(s
, RLDICL
, args
[0], args
[1], args
[2], 0);
2299 tcg_out32(s
, RLDCL
| SAB(args
[1], args
[0], args
[2]) | MB64(0));
2302 case INDEX_op_rotr_i64
:
2303 if (const_args
[2]) {
2304 tcg_out_rld(s
, RLDICL
, args
[0], args
[1], 64 - args
[2], 0);
2306 tcg_out32(s
, SUBFIC
| TAI(TCG_REG_R0
, args
[2], 64));
2307 tcg_out32(s
, RLDCL
| SAB(args
[1], args
[0], TCG_REG_R0
) | MB64(0));
2311 case INDEX_op_mul_i64
:
2312 a0
= args
[0], a1
= args
[1], a2
= args
[2];
2313 if (const_args
[2]) {
2314 tcg_out32(s
, MULLI
| TAI(a0
, a1
, a2
));
2316 tcg_out32(s
, MULLD
| TAB(a0
, a1
, a2
));
2319 case INDEX_op_div_i64
:
2320 tcg_out32(s
, DIVD
| TAB(args
[0], args
[1], args
[2]));
2322 case INDEX_op_divu_i64
:
2323 tcg_out32(s
, DIVDU
| TAB(args
[0], args
[1], args
[2]));
2326 case INDEX_op_qemu_ld_i32
:
2327 tcg_out_qemu_ld(s
, args
, false);
2329 case INDEX_op_qemu_ld_i64
:
2330 tcg_out_qemu_ld(s
, args
, true);
2332 case INDEX_op_qemu_st_i32
:
2333 tcg_out_qemu_st(s
, args
, false);
2335 case INDEX_op_qemu_st_i64
:
2336 tcg_out_qemu_st(s
, args
, true);
2339 case INDEX_op_ext8s_i32
:
2340 case INDEX_op_ext8s_i64
:
2343 case INDEX_op_ext16s_i32
:
2344 case INDEX_op_ext16s_i64
:
2347 case INDEX_op_ext_i32_i64
:
2348 case INDEX_op_ext32s_i64
:
2352 tcg_out32(s
, c
| RS(args
[1]) | RA(args
[0]));
2354 case INDEX_op_extu_i32_i64
:
2355 tcg_out_ext32u(s
, args
[0], args
[1]);
2358 case INDEX_op_setcond_i32
:
2359 tcg_out_setcond(s
, TCG_TYPE_I32
, args
[3], args
[0], args
[1], args
[2],
2362 case INDEX_op_setcond_i64
:
2363 tcg_out_setcond(s
, TCG_TYPE_I64
, args
[3], args
[0], args
[1], args
[2],
2366 case INDEX_op_setcond2_i32
:
2367 tcg_out_setcond2(s
, args
, const_args
);
2370 case INDEX_op_bswap16_i32
:
2371 case INDEX_op_bswap16_i64
:
2372 a0
= args
[0], a1
= args
[1];
2375 /* a0 = (a1 r<< 24) & 0xff # 000c */
2376 tcg_out_rlw(s
, RLWINM
, a0
, a1
, 24, 24, 31);
2377 /* a0 = (a0 & ~0xff00) | (a1 r<< 8) & 0xff00 # 00dc */
2378 tcg_out_rlw(s
, RLWIMI
, a0
, a1
, 8, 16, 23);
2380 /* r0 = (a1 r<< 8) & 0xff00 # 00d0 */
2381 tcg_out_rlw(s
, RLWINM
, TCG_REG_R0
, a1
, 8, 16, 23);
2382 /* a0 = (a1 r<< 24) & 0xff # 000c */
2383 tcg_out_rlw(s
, RLWINM
, a0
, a1
, 24, 24, 31);
2384 /* a0 = a0 | r0 # 00dc */
2385 tcg_out32(s
, OR
| SAB(TCG_REG_R0
, a0
, a0
));
2389 case INDEX_op_bswap32_i32
:
2390 case INDEX_op_bswap32_i64
:
2391 /* Stolen from gcc's builtin_bswap32 */
2393 a0
= args
[0] == a1
? TCG_REG_R0
: args
[0];
2395 /* a1 = args[1] # abcd */
2396 /* a0 = rotate_left (a1, 8) # bcda */
2397 tcg_out_rlw(s
, RLWINM
, a0
, a1
, 8, 0, 31);
2398 /* a0 = (a0 & ~0xff000000) | ((a1 r<< 24) & 0xff000000) # dcda */
2399 tcg_out_rlw(s
, RLWIMI
, a0
, a1
, 24, 0, 7);
2400 /* a0 = (a0 & ~0x0000ff00) | ((a1 r<< 24) & 0x0000ff00) # dcba */
2401 tcg_out_rlw(s
, RLWIMI
, a0
, a1
, 24, 16, 23);
2403 if (a0
== TCG_REG_R0
) {
2404 tcg_out_mov(s
, TCG_TYPE_REG
, args
[0], a0
);
2408 case INDEX_op_bswap64_i64
:
2409 a0
= args
[0], a1
= args
[1], a2
= TCG_REG_R0
;
2415 /* a1 = # abcd efgh */
2416 /* a0 = rl32(a1, 8) # 0000 fghe */
2417 tcg_out_rlw(s
, RLWINM
, a0
, a1
, 8, 0, 31);
2418 /* a0 = dep(a0, rl32(a1, 24), 0xff000000) # 0000 hghe */
2419 tcg_out_rlw(s
, RLWIMI
, a0
, a1
, 24, 0, 7);
2420 /* a0 = dep(a0, rl32(a1, 24), 0x0000ff00) # 0000 hgfe */
2421 tcg_out_rlw(s
, RLWIMI
, a0
, a1
, 24, 16, 23);
2423 /* a0 = rl64(a0, 32) # hgfe 0000 */
2424 /* a2 = rl64(a1, 32) # efgh abcd */
2425 tcg_out_rld(s
, RLDICL
, a0
, a0
, 32, 0);
2426 tcg_out_rld(s
, RLDICL
, a2
, a1
, 32, 0);
2428 /* a0 = dep(a0, rl32(a2, 8), 0xffffffff) # hgfe bcda */
2429 tcg_out_rlw(s
, RLWIMI
, a0
, a2
, 8, 0, 31);
2430 /* a0 = dep(a0, rl32(a2, 24), 0xff000000) # hgfe dcda */
2431 tcg_out_rlw(s
, RLWIMI
, a0
, a2
, 24, 0, 7);
2432 /* a0 = dep(a0, rl32(a2, 24), 0x0000ff00) # hgfe dcba */
2433 tcg_out_rlw(s
, RLWIMI
, a0
, a2
, 24, 16, 23);
2436 tcg_out_mov(s
, TCG_TYPE_REG
, args
[0], a0
);
2440 case INDEX_op_deposit_i32
:
2441 if (const_args
[2]) {
2442 uint32_t mask
= ((2u << (args
[4] - 1)) - 1) << args
[3];
2443 tcg_out_andi32(s
, args
[0], args
[0], ~mask
);
2445 tcg_out_rlw(s
, RLWIMI
, args
[0], args
[2], args
[3],
2446 32 - args
[3] - args
[4], 31 - args
[3]);
2449 case INDEX_op_deposit_i64
:
2450 if (const_args
[2]) {
2451 uint64_t mask
= ((2ull << (args
[4] - 1)) - 1) << args
[3];
2452 tcg_out_andi64(s
, args
[0], args
[0], ~mask
);
2454 tcg_out_rld(s
, RLDIMI
, args
[0], args
[2], args
[3],
2455 64 - args
[3] - args
[4]);
2459 case INDEX_op_extract_i32
:
2460 tcg_out_rlw(s
, RLWINM
, args
[0], args
[1],
2461 32 - args
[2], 32 - args
[3], 31);
2463 case INDEX_op_extract_i64
:
2464 tcg_out_rld(s
, RLDICL
, args
[0], args
[1], 64 - args
[2], 64 - args
[3]);
2467 case INDEX_op_movcond_i32
:
2468 tcg_out_movcond(s
, TCG_TYPE_I32
, args
[5], args
[0], args
[1], args
[2],
2469 args
[3], args
[4], const_args
[2]);
2471 case INDEX_op_movcond_i64
:
2472 tcg_out_movcond(s
, TCG_TYPE_I64
, args
[5], args
[0], args
[1], args
[2],
2473 args
[3], args
[4], const_args
[2]);
2476 #if TCG_TARGET_REG_BITS == 64
2477 case INDEX_op_add2_i64
:
2479 case INDEX_op_add2_i32
:
2481 /* Note that the CA bit is defined based on the word size of the
2482 environment. So in 64-bit mode it's always carry-out of bit 63.
2483 The fallback code using deposit works just as well for 32-bit. */
2484 a0
= args
[0], a1
= args
[1];
2485 if (a0
== args
[3] || (!const_args
[5] && a0
== args
[5])) {
2488 if (const_args
[4]) {
2489 tcg_out32(s
, ADDIC
| TAI(a0
, args
[2], args
[4]));
2491 tcg_out32(s
, ADDC
| TAB(a0
, args
[2], args
[4]));
2493 if (const_args
[5]) {
2494 tcg_out32(s
, (args
[5] ? ADDME
: ADDZE
) | RT(a1
) | RA(args
[3]));
2496 tcg_out32(s
, ADDE
| TAB(a1
, args
[3], args
[5]));
2498 if (a0
!= args
[0]) {
2499 tcg_out_mov(s
, TCG_TYPE_REG
, args
[0], a0
);
2503 #if TCG_TARGET_REG_BITS == 64
2504 case INDEX_op_sub2_i64
:
2506 case INDEX_op_sub2_i32
:
2508 a0
= args
[0], a1
= args
[1];
2509 if (a0
== args
[5] || (!const_args
[3] && a0
== args
[3])) {
2512 if (const_args
[2]) {
2513 tcg_out32(s
, SUBFIC
| TAI(a0
, args
[4], args
[2]));
2515 tcg_out32(s
, SUBFC
| TAB(a0
, args
[4], args
[2]));
2517 if (const_args
[3]) {
2518 tcg_out32(s
, (args
[3] ? SUBFME
: SUBFZE
) | RT(a1
) | RA(args
[5]));
2520 tcg_out32(s
, SUBFE
| TAB(a1
, args
[5], args
[3]));
2522 if (a0
!= args
[0]) {
2523 tcg_out_mov(s
, TCG_TYPE_REG
, args
[0], a0
);
2527 case INDEX_op_muluh_i32
:
2528 tcg_out32(s
, MULHWU
| TAB(args
[0], args
[1], args
[2]));
2530 case INDEX_op_mulsh_i32
:
2531 tcg_out32(s
, MULHW
| TAB(args
[0], args
[1], args
[2]));
2533 case INDEX_op_muluh_i64
:
2534 tcg_out32(s
, MULHDU
| TAB(args
[0], args
[1], args
[2]));
2536 case INDEX_op_mulsh_i64
:
2537 tcg_out32(s
, MULHD
| TAB(args
[0], args
[1], args
[2]));
2541 tcg_out_mb(s
, args
[0]);
2544 case INDEX_op_mov_i32
: /* Always emitted via tcg_out_mov. */
2545 case INDEX_op_mov_i64
:
2546 case INDEX_op_movi_i32
: /* Always emitted via tcg_out_movi. */
2547 case INDEX_op_movi_i64
:
2548 case INDEX_op_call
: /* Always emitted via tcg_out_call. */
2554 static const TCGTargetOpDef ppc_op_defs
[] = {
2555 { INDEX_op_exit_tb
, { } },
2556 { INDEX_op_goto_tb
, { } },
2557 { INDEX_op_br
, { } },
2559 { INDEX_op_ld8u_i32
, { "r", "r" } },
2560 { INDEX_op_ld8s_i32
, { "r", "r" } },
2561 { INDEX_op_ld16u_i32
, { "r", "r" } },
2562 { INDEX_op_ld16s_i32
, { "r", "r" } },
2563 { INDEX_op_ld_i32
, { "r", "r" } },
2565 { INDEX_op_st8_i32
, { "r", "r" } },
2566 { INDEX_op_st16_i32
, { "r", "r" } },
2567 { INDEX_op_st_i32
, { "r", "r" } },
2569 { INDEX_op_add_i32
, { "r", "r", "ri" } },
2570 { INDEX_op_mul_i32
, { "r", "r", "rI" } },
2571 { INDEX_op_div_i32
, { "r", "r", "r" } },
2572 { INDEX_op_divu_i32
, { "r", "r", "r" } },
2573 { INDEX_op_sub_i32
, { "r", "rI", "ri" } },
2574 { INDEX_op_and_i32
, { "r", "r", "ri" } },
2575 { INDEX_op_or_i32
, { "r", "r", "ri" } },
2576 { INDEX_op_xor_i32
, { "r", "r", "ri" } },
2577 { INDEX_op_andc_i32
, { "r", "r", "ri" } },
2578 { INDEX_op_orc_i32
, { "r", "r", "ri" } },
2579 { INDEX_op_eqv_i32
, { "r", "r", "ri" } },
2580 { INDEX_op_nand_i32
, { "r", "r", "r" } },
2581 { INDEX_op_nor_i32
, { "r", "r", "r" } },
2582 { INDEX_op_clz_i32
, { "r", "r", "rZW" } },
2583 { INDEX_op_ctz_i32
, { "r", "r", "rZW" } },
2584 { INDEX_op_ctpop_i32
, { "r", "r" } },
2586 { INDEX_op_shl_i32
, { "r", "r", "ri" } },
2587 { INDEX_op_shr_i32
, { "r", "r", "ri" } },
2588 { INDEX_op_sar_i32
, { "r", "r", "ri" } },
2589 { INDEX_op_rotl_i32
, { "r", "r", "ri" } },
2590 { INDEX_op_rotr_i32
, { "r", "r", "ri" } },
2592 { INDEX_op_neg_i32
, { "r", "r" } },
2593 { INDEX_op_not_i32
, { "r", "r" } },
2594 { INDEX_op_ext8s_i32
, { "r", "r" } },
2595 { INDEX_op_ext16s_i32
, { "r", "r" } },
2596 { INDEX_op_bswap16_i32
, { "r", "r" } },
2597 { INDEX_op_bswap32_i32
, { "r", "r" } },
2599 { INDEX_op_brcond_i32
, { "r", "ri" } },
2600 { INDEX_op_setcond_i32
, { "r", "r", "ri" } },
2601 { INDEX_op_movcond_i32
, { "r", "r", "ri", "rZ", "rZ" } },
2603 { INDEX_op_deposit_i32
, { "r", "0", "rZ" } },
2604 { INDEX_op_extract_i32
, { "r", "r" } },
2606 { INDEX_op_muluh_i32
, { "r", "r", "r" } },
2607 { INDEX_op_mulsh_i32
, { "r", "r", "r" } },
2609 #if TCG_TARGET_REG_BITS == 64
2610 { INDEX_op_ld8u_i64
, { "r", "r" } },
2611 { INDEX_op_ld8s_i64
, { "r", "r" } },
2612 { INDEX_op_ld16u_i64
, { "r", "r" } },
2613 { INDEX_op_ld16s_i64
, { "r", "r" } },
2614 { INDEX_op_ld32u_i64
, { "r", "r" } },
2615 { INDEX_op_ld32s_i64
, { "r", "r" } },
2616 { INDEX_op_ld_i64
, { "r", "r" } },
2618 { INDEX_op_st8_i64
, { "r", "r" } },
2619 { INDEX_op_st16_i64
, { "r", "r" } },
2620 { INDEX_op_st32_i64
, { "r", "r" } },
2621 { INDEX_op_st_i64
, { "r", "r" } },
2623 { INDEX_op_add_i64
, { "r", "r", "rT" } },
2624 { INDEX_op_sub_i64
, { "r", "rI", "rT" } },
2625 { INDEX_op_and_i64
, { "r", "r", "ri" } },
2626 { INDEX_op_or_i64
, { "r", "r", "rU" } },
2627 { INDEX_op_xor_i64
, { "r", "r", "rU" } },
2628 { INDEX_op_andc_i64
, { "r", "r", "ri" } },
2629 { INDEX_op_orc_i64
, { "r", "r", "r" } },
2630 { INDEX_op_eqv_i64
, { "r", "r", "r" } },
2631 { INDEX_op_nand_i64
, { "r", "r", "r" } },
2632 { INDEX_op_nor_i64
, { "r", "r", "r" } },
2633 { INDEX_op_clz_i64
, { "r", "r", "rZW" } },
2634 { INDEX_op_ctz_i64
, { "r", "r", "rZW" } },
2635 { INDEX_op_ctpop_i64
, { "r", "r" } },
2637 { INDEX_op_shl_i64
, { "r", "r", "ri" } },
2638 { INDEX_op_shr_i64
, { "r", "r", "ri" } },
2639 { INDEX_op_sar_i64
, { "r", "r", "ri" } },
2640 { INDEX_op_rotl_i64
, { "r", "r", "ri" } },
2641 { INDEX_op_rotr_i64
, { "r", "r", "ri" } },
2643 { INDEX_op_mul_i64
, { "r", "r", "rI" } },
2644 { INDEX_op_div_i64
, { "r", "r", "r" } },
2645 { INDEX_op_divu_i64
, { "r", "r", "r" } },
2647 { INDEX_op_neg_i64
, { "r", "r" } },
2648 { INDEX_op_not_i64
, { "r", "r" } },
2649 { INDEX_op_ext8s_i64
, { "r", "r" } },
2650 { INDEX_op_ext16s_i64
, { "r", "r" } },
2651 { INDEX_op_ext32s_i64
, { "r", "r" } },
2652 { INDEX_op_ext_i32_i64
, { "r", "r" } },
2653 { INDEX_op_extu_i32_i64
, { "r", "r" } },
2654 { INDEX_op_bswap16_i64
, { "r", "r" } },
2655 { INDEX_op_bswap32_i64
, { "r", "r" } },
2656 { INDEX_op_bswap64_i64
, { "r", "r" } },
2658 { INDEX_op_brcond_i64
, { "r", "ri" } },
2659 { INDEX_op_setcond_i64
, { "r", "r", "ri" } },
2660 { INDEX_op_movcond_i64
, { "r", "r", "ri", "rZ", "rZ" } },
2662 { INDEX_op_deposit_i64
, { "r", "0", "rZ" } },
2663 { INDEX_op_extract_i64
, { "r", "r" } },
2665 { INDEX_op_mulsh_i64
, { "r", "r", "r" } },
2666 { INDEX_op_muluh_i64
, { "r", "r", "r" } },
2669 #if TCG_TARGET_REG_BITS == 32
2670 { INDEX_op_brcond2_i32
, { "r", "r", "ri", "ri" } },
2671 { INDEX_op_setcond2_i32
, { "r", "r", "r", "ri", "ri" } },
2674 #if TCG_TARGET_REG_BITS == 64
2675 { INDEX_op_add2_i64
, { "r", "r", "r", "r", "rI", "rZM" } },
2676 { INDEX_op_sub2_i64
, { "r", "r", "rI", "rZM", "r", "r" } },
2678 { INDEX_op_add2_i32
, { "r", "r", "r", "r", "rI", "rZM" } },
2679 { INDEX_op_sub2_i32
, { "r", "r", "rI", "rZM", "r", "r" } },
2682 #if TCG_TARGET_REG_BITS == 64
2683 { INDEX_op_qemu_ld_i32
, { "r", "L" } },
2684 { INDEX_op_qemu_st_i32
, { "S", "S" } },
2685 { INDEX_op_qemu_ld_i64
, { "r", "L" } },
2686 { INDEX_op_qemu_st_i64
, { "S", "S" } },
2687 #elif TARGET_LONG_BITS == 32
2688 { INDEX_op_qemu_ld_i32
, { "r", "L" } },
2689 { INDEX_op_qemu_st_i32
, { "S", "S" } },
2690 { INDEX_op_qemu_ld_i64
, { "L", "L", "L" } },
2691 { INDEX_op_qemu_st_i64
, { "S", "S", "S" } },
2693 { INDEX_op_qemu_ld_i32
, { "r", "L", "L" } },
2694 { INDEX_op_qemu_st_i32
, { "S", "S", "S" } },
2695 { INDEX_op_qemu_ld_i64
, { "L", "L", "L", "L" } },
2696 { INDEX_op_qemu_st_i64
, { "S", "S", "S", "S" } },
2699 { INDEX_op_mb
, { } },
2703 static const TCGTargetOpDef
*tcg_target_op_def(TCGOpcode op
)
2705 int i
, n
= ARRAY_SIZE(ppc_op_defs
);
2707 for (i
= 0; i
< n
; ++i
) {
2708 if (ppc_op_defs
[i
].op
== op
) {
2709 return &ppc_op_defs
[i
];
2715 static void tcg_target_init(TCGContext
*s
)
2717 unsigned long hwcap
= qemu_getauxval(AT_HWCAP
);
2718 unsigned long hwcap2
= qemu_getauxval(AT_HWCAP2
);
2720 if (hwcap
& PPC_FEATURE_ARCH_2_06
) {
2721 have_isa_2_06
= true;
2723 #ifdef PPC_FEATURE2_ARCH_3_00
2724 if (hwcap2
& PPC_FEATURE2_ARCH_3_00
) {
2725 have_isa_3_00
= true;
2729 tcg_regset_set32(tcg_target_available_regs
[TCG_TYPE_I32
], 0, 0xffffffff);
2730 tcg_regset_set32(tcg_target_available_regs
[TCG_TYPE_I64
], 0, 0xffffffff);
2731 tcg_regset_set32(tcg_target_call_clobber_regs
, 0,
2741 (1 << TCG_REG_R10
) |
2742 (1 << TCG_REG_R11
) |
2743 (1 << TCG_REG_R12
));
2745 tcg_regset_clear(s
->reserved_regs
);
2746 tcg_regset_set_reg(s
->reserved_regs
, TCG_REG_R0
); /* tcg temp */
2747 tcg_regset_set_reg(s
->reserved_regs
, TCG_REG_R1
); /* stack pointer */
2748 #if defined(_CALL_SYSV)
2749 tcg_regset_set_reg(s
->reserved_regs
, TCG_REG_R2
); /* toc pointer */
2751 #if defined(_CALL_SYSV) || TCG_TARGET_REG_BITS == 64
2752 tcg_regset_set_reg(s
->reserved_regs
, TCG_REG_R13
); /* thread pointer */
2754 tcg_regset_set_reg(s
->reserved_regs
, TCG_REG_TMP1
); /* mem temp */
2756 tcg_regset_set_reg(s
->reserved_regs
, TCG_REG_RA
); /* return addr */
2763 DebugFrameFDEHeader fde
;
2764 uint8_t fde_def_cfa
[4];
2765 uint8_t fde_reg_ofs
[ARRAY_SIZE(tcg_target_callee_save_regs
) * 2 + 3];
2768 /* We're expecting a 2 byte uleb128 encoded value. */
2769 QEMU_BUILD_BUG_ON(FRAME_SIZE
>= (1 << 14));
2771 #if TCG_TARGET_REG_BITS == 64
2772 # define ELF_HOST_MACHINE EM_PPC64
2774 # define ELF_HOST_MACHINE EM_PPC
2777 static DebugFrame debug_frame
= {
2778 .cie
.len
= sizeof(DebugFrameCIE
)-4, /* length after .len member */
2781 .cie
.code_align
= 1,
2782 .cie
.data_align
= (-SZR
& 0x7f), /* sleb128 -SZR */
2783 .cie
.return_column
= 65,
2785 /* Total FDE size does not include the "len" member. */
2786 .fde
.len
= sizeof(DebugFrame
) - offsetof(DebugFrame
, fde
.cie_offset
),
2789 12, TCG_REG_R1
, /* DW_CFA_def_cfa r1, ... */
2790 (FRAME_SIZE
& 0x7f) | 0x80, /* ... uleb128 FRAME_SIZE */
2794 /* DW_CFA_offset_extended_sf, lr, LR_OFFSET */
2795 0x11, 65, (LR_OFFSET
/ -SZR
) & 0x7f,
2799 void tcg_register_jit(void *buf
, size_t buf_size
)
2801 uint8_t *p
= &debug_frame
.fde_reg_ofs
[3];
2804 for (i
= 0; i
< ARRAY_SIZE(tcg_target_callee_save_regs
); ++i
, p
+= 2) {
2805 p
[0] = 0x80 + tcg_target_callee_save_regs
[i
];
2806 p
[1] = (FRAME_SIZE
- (REG_SAVE_BOT
+ i
* SZR
)) / SZR
;
2809 debug_frame
.fde
.func_start
= (uintptr_t)buf
;
2810 debug_frame
.fde
.func_len
= buf_size
;
2812 tcg_register_jit_int(buf
, buf_size
, &debug_frame
, sizeof(debug_frame
));
2814 #endif /* __ELF__ */
2816 static size_t dcache_bsize
= 16;
2817 static size_t icache_bsize
= 16;
2819 void flush_icache_range(uintptr_t start
, uintptr_t stop
)
2821 uintptr_t p
, start1
, stop1
;
2822 size_t dsize
= dcache_bsize
;
2823 size_t isize
= icache_bsize
;
2825 start1
= start
& ~(dsize
- 1);
2826 stop1
= (stop
+ dsize
- 1) & ~(dsize
- 1);
2827 for (p
= start1
; p
< stop1
; p
+= dsize
) {
2828 asm volatile ("dcbst 0,%0" : : "r"(p
) : "memory");
2830 asm volatile ("sync" : : : "memory");
2832 start
&= start
& ~(isize
- 1);
2833 stop1
= (stop
+ isize
- 1) & ~(isize
- 1);
2834 for (p
= start1
; p
< stop1
; p
+= isize
) {
2835 asm volatile ("icbi 0,%0" : : "r"(p
) : "memory");
2837 asm volatile ("sync" : : : "memory");
2838 asm volatile ("isync" : : : "memory");
2842 #include <sys/systemcfg.h>
2844 static void __attribute__((constructor
)) tcg_cache_init(void)
2846 icache_bsize
= _system_configuration
.icache_line
;
2847 dcache_bsize
= _system_configuration
.dcache_line
;
2850 #elif defined __linux__
2851 static void __attribute__((constructor
)) tcg_cache_init(void)
2853 unsigned long dsize
= qemu_getauxval(AT_DCACHEBSIZE
);
2854 unsigned long isize
= qemu_getauxval(AT_ICACHEBSIZE
);
2856 if (dsize
== 0 || isize
== 0) {
2858 fprintf(stderr
, "getauxval AT_DCACHEBSIZE failed\n");
2861 fprintf(stderr
, "getauxval AT_ICACHEBSIZE failed\n");
2865 dcache_bsize
= dsize
;
2866 icache_bsize
= isize
;
2869 #elif defined __APPLE__
2870 #include <sys/sysctl.h>
2872 static void __attribute__((constructor
)) tcg_cache_init(void)
2876 int name
[2] = { CTL_HW
, HW_CACHELINE
};
2878 len
= sizeof(cacheline
);
2879 if (sysctl(name
, 2, &cacheline
, &len
, NULL
, 0)) {
2880 perror("sysctl CTL_HW HW_CACHELINE failed");
2883 dcache_bsize
= cacheline
;
2884 icache_bsize
= cacheline
;
2887 #elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
2888 #include <sys/sysctl.h>
2890 static void __attribute__((constructor
)) tcg_cache_init(void)
2895 if (sysctlbyname ("machdep.cacheline_size", &cacheline
, &len
, NULL
, 0)) {
2896 fprintf(stderr
, "sysctlbyname machdep.cacheline_size failed: %s\n",
2900 dcache_bsize
= cacheline
;
2901 icache_bsize
= cacheline
;