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 static tcg_insn_unit
*tb_ret_addr
;
29 #if defined _CALL_DARWIN || defined __APPLE__
30 #define TCG_TARGET_CALL_DARWIN
33 #ifdef TCG_TARGET_CALL_DARWIN
34 #define LINKAGE_AREA_SIZE 24
36 #elif defined _CALL_AIX
37 #define LINKAGE_AREA_SIZE 52
40 #define LINKAGE_AREA_SIZE 8
48 #ifdef CONFIG_USE_GUEST_BASE
49 #define TCG_GUEST_BASE_REG 30
51 #define TCG_GUEST_BASE_REG 0
55 static const char * const tcg_target_reg_names
[TCG_TARGET_NB_REGS
] = {
91 static const int tcg_target_reg_alloc_order
[] = {
106 #ifdef TCG_TARGET_CALL_DARWIN
117 #ifndef TCG_TARGET_CALL_DARWIN
130 static const int tcg_target_call_iarg_regs
[] = {
141 static const int tcg_target_call_oarg_regs
[2] = {
146 static const int tcg_target_callee_save_regs
[] = {
147 #ifdef TCG_TARGET_CALL_DARWIN
167 TCG_REG_R27
, /* currently used for the global env */
174 static inline bool in_range_b(tcg_target_long target
)
176 return target
== sextract32(target
, 0, 26);
179 static uint32_t reloc_pc24_val(tcg_insn_unit
*pc
, tcg_insn_unit
*target
)
181 ptrdiff_t disp
= tcg_ptr_byte_diff(target
, pc
);
182 assert(in_range_b(disp
));
183 return disp
& 0x3fffffc;
186 static void reloc_pc24(tcg_insn_unit
*pc
, tcg_insn_unit
*target
)
188 *pc
= (*pc
& ~0x3fffffc) | reloc_pc24_val(pc
, target
);
191 static uint16_t reloc_pc14_val(tcg_insn_unit
*pc
, tcg_insn_unit
*target
)
193 ptrdiff_t disp
= tcg_ptr_byte_diff(target
, pc
);
194 assert(disp
== (int16_t) disp
);
195 return disp
& 0xfffc;
198 static void reloc_pc14(tcg_insn_unit
*pc
, tcg_insn_unit
*target
)
200 *pc
= (*pc
& ~0xfffc) | reloc_pc14_val(pc
, target
);
203 static void patch_reloc(tcg_insn_unit
*code_ptr
, int type
,
204 intptr_t value
, intptr_t addend
)
206 tcg_insn_unit
*target
= (tcg_insn_unit
*)value
;
211 reloc_pc14(code_ptr
, target
);
214 reloc_pc24(code_ptr
, target
);
221 /* parse target specific constraints */
222 static int target_parse_constraint(TCGArgConstraint
*ct
, const char **pct_str
)
228 case 'A': case 'B': case 'C': case 'D':
229 ct
->ct
|= TCG_CT_REG
;
230 tcg_regset_set_reg(ct
->u
.regs
, 3 + ct_str
[0] - 'A');
233 ct
->ct
|= TCG_CT_REG
;
234 tcg_regset_set32(ct
->u
.regs
, 0, 0xffffffff);
236 #ifdef CONFIG_SOFTMMU
237 case 'L': /* qemu_ld constraint */
238 ct
->ct
|= TCG_CT_REG
;
239 tcg_regset_set32(ct
->u
.regs
, 0, 0xffffffff);
240 tcg_regset_reset_reg(ct
->u
.regs
, TCG_REG_R3
);
241 tcg_regset_reset_reg(ct
->u
.regs
, TCG_REG_R4
);
242 tcg_regset_reset_reg(ct
->u
.regs
, TCG_REG_R5
);
243 #if TARGET_LONG_BITS == 64
244 tcg_regset_reset_reg(ct
->u
.regs
, TCG_REG_R6
);
245 #ifdef TCG_TARGET_CALL_ALIGN_ARGS
246 tcg_regset_reset_reg(ct
->u
.regs
, TCG_REG_R7
);
250 case 'K': /* qemu_st[8..32] constraint */
251 ct
->ct
|= TCG_CT_REG
;
252 tcg_regset_set32(ct
->u
.regs
, 0, 0xffffffff);
253 tcg_regset_reset_reg(ct
->u
.regs
, TCG_REG_R3
);
254 tcg_regset_reset_reg(ct
->u
.regs
, TCG_REG_R4
);
255 tcg_regset_reset_reg(ct
->u
.regs
, TCG_REG_R5
);
256 tcg_regset_reset_reg(ct
->u
.regs
, TCG_REG_R6
);
257 #if TARGET_LONG_BITS == 64
258 tcg_regset_reset_reg(ct
->u
.regs
, TCG_REG_R7
);
259 #ifdef TCG_TARGET_CALL_ALIGN_ARGS
260 tcg_regset_reset_reg(ct
->u
.regs
, TCG_REG_R8
);
264 case 'M': /* qemu_st64 constraint */
265 ct
->ct
|= TCG_CT_REG
;
266 tcg_regset_set32(ct
->u
.regs
, 0, 0xffffffff);
267 tcg_regset_reset_reg(ct
->u
.regs
, TCG_REG_R3
);
268 tcg_regset_reset_reg(ct
->u
.regs
, TCG_REG_R4
);
269 tcg_regset_reset_reg(ct
->u
.regs
, TCG_REG_R5
);
270 tcg_regset_reset_reg(ct
->u
.regs
, TCG_REG_R6
);
271 tcg_regset_reset_reg(ct
->u
.regs
, TCG_REG_R7
);
272 tcg_regset_reset_reg(ct
->u
.regs
, TCG_REG_R8
);
273 #ifdef TCG_TARGET_CALL_ALIGN_ARGS
274 tcg_regset_reset_reg(ct
->u
.regs
, TCG_REG_R9
);
280 ct
->ct
|= TCG_CT_REG
;
281 tcg_regset_set32(ct
->u
.regs
, 0, 0xffffffff);
284 ct
->ct
|= TCG_CT_REG
;
285 tcg_regset_set32(ct
->u
.regs
, 0, 0xffffffff);
286 tcg_regset_reset_reg(ct
->u
.regs
, TCG_REG_R3
);
297 /* test if a constant matches the constraint */
298 static int tcg_target_const_match(tcg_target_long val
, TCGType type
,
299 const TCGArgConstraint
*arg_ct
)
304 if (ct
& TCG_CT_CONST
)
309 #define OPCD(opc) ((opc)<<26)
310 #define XO31(opc) (OPCD(31)|((opc)<<1))
311 #define XO19(opc) (OPCD(19)|((opc)<<1))
323 #define ADDIC OPCD(12)
324 #define ADDI OPCD(14)
325 #define ADDIS OPCD(15)
327 #define ORIS OPCD(25)
328 #define XORI OPCD(26)
329 #define XORIS OPCD(27)
330 #define ANDI OPCD(28)
331 #define ANDIS OPCD(29)
332 #define MULLI OPCD( 7)
333 #define CMPLI OPCD(10)
334 #define CMPI OPCD(11)
335 #define SUBFIC OPCD( 8)
337 #define LWZU OPCD(33)
338 #define STWU OPCD(37)
340 #define RLWIMI OPCD(20)
341 #define RLWINM OPCD(21)
342 #define RLWNM OPCD(23)
344 #define BCLR XO19( 16)
345 #define BCCTR XO19(528)
346 #define CRAND XO19(257)
347 #define CRANDC XO19(129)
348 #define CRNAND XO19(225)
349 #define CROR XO19(449)
350 #define CRNOR XO19( 33)
352 #define EXTSB XO31(954)
353 #define EXTSH XO31(922)
354 #define ADD XO31(266)
355 #define ADDE XO31(138)
356 #define ADDC XO31( 10)
357 #define AND XO31( 28)
358 #define SUBF XO31( 40)
359 #define SUBFC XO31( 8)
360 #define SUBFE XO31(136)
362 #define XOR XO31(316)
363 #define MULLW XO31(235)
364 #define MULHWU XO31( 11)
365 #define DIVW XO31(491)
366 #define DIVWU XO31(459)
368 #define CMPL XO31( 32)
369 #define LHBRX XO31(790)
370 #define LWBRX XO31(534)
371 #define STHBRX XO31(918)
372 #define STWBRX XO31(662)
373 #define MFSPR XO31(339)
374 #define MTSPR XO31(467)
375 #define SRAWI XO31(824)
376 #define NEG XO31(104)
377 #define MFCR XO31( 19)
378 #define CNTLZW XO31( 26)
379 #define NOR XO31(124)
380 #define ANDC XO31( 60)
381 #define ORC XO31(412)
382 #define EQV XO31(284)
383 #define NAND XO31(476)
384 #define ISEL XO31( 15)
386 #define LBZX XO31( 87)
387 #define LHZX XO31(279)
388 #define LHAX XO31(343)
389 #define LWZX XO31( 23)
390 #define STBX XO31(215)
391 #define STHX XO31(407)
392 #define STWX XO31(151)
394 #define SPR(a,b) ((((a)<<5)|(b))<<11)
396 #define CTR SPR(9, 0)
398 #define SLW XO31( 24)
399 #define SRW XO31(536)
400 #define SRAW XO31(792)
403 #define TRAP (TW | TO (31))
405 #define RT(r) ((r)<<21)
406 #define RS(r) ((r)<<21)
407 #define RA(r) ((r)<<16)
408 #define RB(r) ((r)<<11)
409 #define TO(t) ((t)<<21)
410 #define SH(s) ((s)<<11)
411 #define MB(b) ((b)<<6)
412 #define ME(e) ((e)<<1)
413 #define BO(o) ((o)<<21)
417 #define TAB(t,a,b) (RT(t) | RA(a) | RB(b))
418 #define SAB(s,a,b) (RS(s) | RA(a) | RB(b))
420 #define BF(n) ((n)<<23)
421 #define BI(n, c) (((c)+((n)*4))<<16)
422 #define BT(n, c) (((c)+((n)*4))<<21)
423 #define BA(n, c) (((c)+((n)*4))<<16)
424 #define BB(n, c) (((c)+((n)*4))<<11)
426 #define BO_COND_TRUE BO (12)
427 #define BO_COND_FALSE BO (4)
428 #define BO_ALWAYS BO (20)
437 static const uint32_t tcg_to_bc
[] = {
438 [TCG_COND_EQ
] = BC
| BI (7, CR_EQ
) | BO_COND_TRUE
,
439 [TCG_COND_NE
] = BC
| BI (7, CR_EQ
) | BO_COND_FALSE
,
440 [TCG_COND_LT
] = BC
| BI (7, CR_LT
) | BO_COND_TRUE
,
441 [TCG_COND_GE
] = BC
| BI (7, CR_LT
) | BO_COND_FALSE
,
442 [TCG_COND_LE
] = BC
| BI (7, CR_GT
) | BO_COND_FALSE
,
443 [TCG_COND_GT
] = BC
| BI (7, CR_GT
) | BO_COND_TRUE
,
444 [TCG_COND_LTU
] = BC
| BI (7, CR_LT
) | BO_COND_TRUE
,
445 [TCG_COND_GEU
] = BC
| BI (7, CR_LT
) | BO_COND_FALSE
,
446 [TCG_COND_LEU
] = BC
| BI (7, CR_GT
) | BO_COND_FALSE
,
447 [TCG_COND_GTU
] = BC
| BI (7, CR_GT
) | BO_COND_TRUE
,
450 static void tcg_out_mov(TCGContext
*s
, TCGType type
, TCGReg ret
, TCGReg arg
)
453 tcg_out32(s
, OR
| SAB(arg
, ret
, arg
));
457 static void tcg_out_movi(TCGContext
*s
, TCGType type
,
458 TCGReg ret
, tcg_target_long arg
)
460 if (arg
== (int16_t) arg
)
461 tcg_out32 (s
, ADDI
| RT (ret
) | RA (0) | (arg
& 0xffff));
463 tcg_out32 (s
, ADDIS
| RT (ret
) | RA (0) | ((arg
>> 16) & 0xffff));
465 tcg_out32 (s
, ORI
| RS (ret
) | RA (ret
) | (arg
& 0xffff));
469 static void tcg_out_ldst (TCGContext
*s
, int ret
, int addr
,
470 int offset
, int op1
, int op2
)
472 if (offset
== (int16_t) offset
)
473 tcg_out32 (s
, op1
| RT (ret
) | RA (addr
) | (offset
& 0xffff));
475 tcg_out_movi (s
, TCG_TYPE_I32
, 0, offset
);
476 tcg_out32 (s
, op2
| RT (ret
) | RA (addr
) | RB (0));
480 static void tcg_out_b(TCGContext
*s
, int mask
, tcg_insn_unit
*target
)
482 ptrdiff_t disp
= tcg_pcrel_diff(s
, target
);
483 if (in_range_b(disp
)) {
484 tcg_out32(s
, B
| (disp
& 0x3fffffc) | mask
);
486 tcg_out_movi(s
, TCG_TYPE_PTR
, TCG_REG_R0
, (uintptr_t)target
);
487 tcg_out32(s
, MTSPR
| RS(TCG_REG_R0
) | CTR
);
488 tcg_out32(s
, BCCTR
| BO_ALWAYS
| mask
);
492 static void tcg_out_call1(TCGContext
*s
, tcg_insn_unit
*target
, int lk
)
495 tcg_out_movi(s
, TCG_TYPE_PTR
, TCG_REG_R2
, (uintptr_t)target
);
496 tcg_out32(s
, LWZ
| RT(TCG_REG_R0
) | RA(reg
));
497 tcg_out32(s
, MTSPR
| RA(TCG_REG_R0
) | CTR
);
498 tcg_out32(s
, LWZ
| RT(TCG_REG_R2
) | RA(reg
) | 4);
499 tcg_out32(s
, BCCTR
| BO_ALWAYS
| lk
);
501 tcg_out_b(s
, lk
, target
);
505 static void tcg_out_call(TCGContext
*s
, tcg_insn_unit
*target
)
507 tcg_out_call1(s
, target
, LK
);
510 #if defined(CONFIG_SOFTMMU)
512 static void add_qemu_ldst_label (TCGContext
*s
,
520 tcg_insn_unit
*raddr
,
521 tcg_insn_unit
*label_ptr
)
523 TCGLabelQemuLdst
*label
= new_ldst_label(s
);
525 label
->is_ld
= is_ld
;
527 label
->datalo_reg
= data_reg
;
528 label
->datahi_reg
= data_reg2
;
529 label
->addrlo_reg
= addrlo_reg
;
530 label
->addrhi_reg
= addrhi_reg
;
531 label
->mem_index
= mem_index
;
532 label
->raddr
= raddr
;
533 label
->label_ptr
[0] = label_ptr
;
536 /* helper signature: helper_ret_ld_mmu(CPUState *env, target_ulong addr,
537 * int mmu_idx, uintptr_t ra)
539 static void * const qemu_ld_helpers
[16] = {
540 [MO_UB
] = helper_ret_ldub_mmu
,
541 [MO_LEUW
] = helper_le_lduw_mmu
,
542 [MO_LEUL
] = helper_le_ldul_mmu
,
543 [MO_LEQ
] = helper_le_ldq_mmu
,
544 [MO_BEUW
] = helper_be_lduw_mmu
,
545 [MO_BEUL
] = helper_be_ldul_mmu
,
546 [MO_BEQ
] = helper_be_ldq_mmu
,
549 /* helper signature: helper_ret_st_mmu(CPUState *env, target_ulong addr,
550 * uintxx_t val, int mmu_idx, uintptr_t ra)
552 static void * const qemu_st_helpers
[16] = {
553 [MO_UB
] = helper_ret_stb_mmu
,
554 [MO_LEUW
] = helper_le_stw_mmu
,
555 [MO_LEUL
] = helper_le_stl_mmu
,
556 [MO_LEQ
] = helper_le_stq_mmu
,
557 [MO_BEUW
] = helper_be_stw_mmu
,
558 [MO_BEUL
] = helper_be_stl_mmu
,
559 [MO_BEQ
] = helper_be_stq_mmu
,
562 static tcg_insn_unit
*ld_trampolines
[16];
563 static tcg_insn_unit
*st_trampolines
[16];
565 /* Perform the TLB load and compare. Branches to the slow path, placing the
566 address of the branch in *LABEL_PTR. Loads the addend of the TLB into R0.
567 Clobbers R1 and R2. */
569 static void tcg_out_tlb_check(TCGContext
*s
, TCGReg r0
, TCGReg r1
, TCGReg r2
,
570 TCGReg addrlo
, TCGReg addrhi
, TCGMemOp s_bits
,
571 int mem_index
, int is_load
,
572 tcg_insn_unit
**label_ptr
)
576 ? offsetof(CPUArchState
, tlb_table
[mem_index
][0].addr_read
)
577 : offsetof(CPUArchState
, tlb_table
[mem_index
][0].addr_write
));
578 int add_off
= offsetof(CPUArchState
, tlb_table
[mem_index
][0].addend
);
579 tcg_insn_unit retranst
;
580 TCGReg base
= TCG_AREG0
;
582 /* Extract the page index, shifted into place for tlb index. */
586 | SH(32 - (TARGET_PAGE_BITS
- CPU_TLB_ENTRY_BITS
))
587 | MB(32 - (CPU_TLB_BITS
+ CPU_TLB_ENTRY_BITS
))
588 | ME(31 - CPU_TLB_ENTRY_BITS
)));
590 /* Compensate for very large offsets. */
591 if (add_off
>= 0x8000) {
592 /* Most target env are smaller than 32k; none are larger than 64k.
593 Simplify the logic here merely to offset by 0x7ff0, giving us a
594 range just shy of 64k. Check this assumption. */
595 QEMU_BUILD_BUG_ON(offsetof(CPUArchState
,
596 tlb_table
[NB_MMU_MODES
- 1][1])
598 tcg_out32(s
, ADDI
| RT(r1
) | RA(base
) | 0x7ff0);
604 /* Clear the non-page, non-alignment bits from the address. */
609 | MB((32 - s_bits
) & 31)
610 | ME(31 - TARGET_PAGE_BITS
)));
612 tcg_out32(s
, ADD
| RT(r0
) | RA(r0
) | RB(base
));
615 /* Load the tlb comparator. */
616 tcg_out32(s
, LWZ
| RT(r1
) | RA(base
) | (cmp_off
& 0xffff));
618 tcg_out32(s
, CMP
| BF(7) | RA(r2
) | RB(r1
));
620 if (TARGET_LONG_BITS
== 64) {
621 tcg_out32(s
, LWZ
| RT(r1
) | RA(base
) | ((cmp_off
+ 4) & 0xffff));
624 /* Load the tlb addend for use on the fast path.
625 Do this asap to minimize load delay. */
626 tcg_out32(s
, LWZ
| RT(r0
) | RA(base
) | (add_off
& 0xffff));
628 if (TARGET_LONG_BITS
== 64) {
629 tcg_out32(s
, CMP
| BF(6) | RA(addrhi
) | RB(r1
));
630 tcg_out32(s
, CRAND
| BT(7, CR_EQ
) | BA(6, CR_EQ
) | BB(7, CR_EQ
));
633 /* Use a conditional branch-and-link so that we load a pointer to
634 somewhere within the current opcode, for passing on to the helper.
635 This address cannot be used for a tail call, but it's shorter
636 than forming an address from scratch. */
637 *label_ptr
= s
->code_ptr
;
638 retranst
= *s
->code_ptr
& 0xfffc;
639 tcg_out32(s
, BC
| BI(7, CR_EQ
) | retranst
| BO_COND_FALSE
| LK
);
643 static void tcg_out_qemu_ld(TCGContext
*s
, const TCGArg
*args
, bool is64
)
645 TCGReg addrlo
, datalo
, datahi
, rbase
, addrhi
__attribute__((unused
));
647 #ifdef CONFIG_SOFTMMU
649 tcg_insn_unit
*label_ptr
;
653 datahi
= (is64
? *args
++ : 0);
655 addrhi
= (TARGET_LONG_BITS
== 64 ? *args
++ : 0);
657 bswap
= opc
& MO_BSWAP
;
659 #ifdef CONFIG_SOFTMMU
661 tcg_out_tlb_check(s
, TCG_REG_R3
, TCG_REG_R4
, TCG_REG_R0
, addrlo
,
662 addrhi
, opc
& MO_SIZE
, mem_index
, 0, &label_ptr
);
664 #else /* !CONFIG_SOFTMMU */
665 rbase
= GUEST_BASE
? TCG_GUEST_BASE_REG
: 0;
668 switch (opc
& MO_SSIZE
) {
671 tcg_out32(s
, LBZX
| TAB(datalo
, rbase
, addrlo
));
674 tcg_out32(s
, LBZX
| TAB(datalo
, rbase
, addrlo
));
675 tcg_out32(s
, EXTSB
| RA(datalo
) | RS(datalo
));
678 tcg_out32(s
, (bswap
? LHBRX
: LHZX
) | TAB(datalo
, rbase
, addrlo
));
682 tcg_out32(s
, LHBRX
| TAB(datalo
, rbase
, addrlo
));
683 tcg_out32(s
, EXTSH
| RA(datalo
) | RS(datalo
));
685 tcg_out32(s
, LHAX
| TAB(datalo
, rbase
, addrlo
));
689 tcg_out32(s
, (bswap
? LWBRX
: LWZX
) | TAB(datalo
, rbase
, addrlo
));
693 tcg_out32(s
, ADDI
| RT(TCG_REG_R0
) | RA(addrlo
) | 4);
694 tcg_out32(s
, LWBRX
| TAB(datalo
, rbase
, addrlo
));
695 tcg_out32(s
, LWBRX
| TAB(datahi
, rbase
, TCG_REG_R0
));
696 } else if (rbase
!= 0) {
697 tcg_out32(s
, ADDI
| RT(TCG_REG_R0
) | RA(addrlo
) | 4);
698 tcg_out32(s
, LWZX
| TAB(datahi
, rbase
, addrlo
));
699 tcg_out32(s
, LWZX
| TAB(datalo
, rbase
, TCG_REG_R0
));
700 } else if (addrlo
== datahi
) {
701 tcg_out32(s
, LWZ
| RT(datalo
) | RA(addrlo
) | 4);
702 tcg_out32(s
, LWZ
| RT(datahi
) | RA(addrlo
));
704 tcg_out32(s
, LWZ
| RT(datahi
) | RA(addrlo
));
705 tcg_out32(s
, LWZ
| RT(datalo
) | RA(addrlo
) | 4);
709 #ifdef CONFIG_SOFTMMU
710 add_qemu_ldst_label(s
, true, opc
, datalo
, datahi
, addrlo
,
711 addrhi
, mem_index
, s
->code_ptr
, label_ptr
);
715 static void tcg_out_qemu_st(TCGContext
*s
, const TCGArg
*args
, bool is64
)
717 TCGReg addrlo
, datalo
, datahi
, rbase
, addrhi
__attribute__((unused
));
718 TCGMemOp opc
, bswap
, s_bits
;
719 #ifdef CONFIG_SOFTMMU
721 tcg_insn_unit
*label_ptr
;
725 datahi
= (is64
? *args
++ : 0);
727 addrhi
= (TARGET_LONG_BITS
== 64 ? *args
++ : 0);
729 bswap
= opc
& MO_BSWAP
;
730 s_bits
= opc
& MO_SIZE
;
732 #ifdef CONFIG_SOFTMMU
734 tcg_out_tlb_check(s
, TCG_REG_R3
, TCG_REG_R4
, TCG_REG_R0
, addrlo
,
735 addrhi
, s_bits
, mem_index
, 0, &label_ptr
);
737 #else /* !CONFIG_SOFTMMU */
738 rbase
= GUEST_BASE
? TCG_GUEST_BASE_REG
: 0;
743 tcg_out32(s
, STBX
| SAB(datalo
, rbase
, addrlo
));
746 tcg_out32(s
, (bswap
? STHBRX
: STHX
) | SAB(datalo
, rbase
, addrlo
));
750 tcg_out32(s
, (bswap
? STWBRX
: STWX
) | SAB(datalo
, rbase
, addrlo
));
754 tcg_out32(s
, ADDI
| RT(TCG_REG_R0
) | RA(addrlo
) | 4);
755 tcg_out32(s
, STWBRX
| SAB(datalo
, rbase
, addrlo
));
756 tcg_out32(s
, STWBRX
| SAB(datahi
, rbase
, TCG_REG_R0
));
757 } else if (rbase
!= 0) {
758 tcg_out32(s
, ADDI
| RT(TCG_REG_R0
) | RA(addrlo
) | 4);
759 tcg_out32(s
, STWX
| SAB(datahi
, rbase
, addrlo
));
760 tcg_out32(s
, STWX
| SAB(datalo
, rbase
, TCG_REG_R0
));
762 tcg_out32(s
, STW
| RS(datahi
) | RA(addrlo
));
763 tcg_out32(s
, STW
| RS(datalo
) | RA(addrlo
) | 4);
768 #ifdef CONFIG_SOFTMMU
769 add_qemu_ldst_label(s
, false, opc
, datalo
, datahi
, addrlo
, addrhi
,
770 mem_index
, s
->code_ptr
, label_ptr
);
774 #if defined(CONFIG_SOFTMMU)
775 static void tcg_out_qemu_ld_slow_path(TCGContext
*s
, TCGLabelQemuLdst
*l
)
777 TCGReg ir
, datalo
, datahi
;
778 TCGMemOp opc
= l
->opc
;
780 reloc_pc14(l
->label_ptr
[0], s
->code_ptr
);
783 if (TARGET_LONG_BITS
== 32) {
784 tcg_out_mov(s
, TCG_TYPE_I32
, ir
++, l
->addrlo_reg
);
786 #ifdef TCG_TARGET_CALL_ALIGN_ARGS
789 tcg_out_mov(s
, TCG_TYPE_I32
, ir
++, l
->addrhi_reg
);
790 tcg_out_mov(s
, TCG_TYPE_I32
, ir
++, l
->addrlo_reg
);
792 tcg_out_movi(s
, TCG_TYPE_I32
, ir
++, l
->mem_index
);
793 tcg_out32(s
, MFSPR
| RT(ir
++) | LR
);
794 tcg_out_b(s
, LK
, ld_trampolines
[opc
& ~MO_SIGN
]);
796 datalo
= l
->datalo_reg
;
797 switch (opc
& MO_SSIZE
) {
799 tcg_out32(s
, EXTSB
| RA(datalo
) | RS(TCG_REG_R3
));
802 tcg_out32(s
, EXTSH
| RA(datalo
) | RS(TCG_REG_R3
));
805 tcg_out_mov(s
, TCG_TYPE_I32
, datalo
, TCG_REG_R3
);
808 datahi
= l
->datahi_reg
;
809 if (datalo
!= TCG_REG_R3
) {
810 tcg_out_mov(s
, TCG_TYPE_I32
, datalo
, TCG_REG_R4
);
811 tcg_out_mov(s
, TCG_TYPE_I32
, datahi
, TCG_REG_R3
);
812 } else if (datahi
!= TCG_REG_R4
) {
813 tcg_out_mov(s
, TCG_TYPE_I32
, datahi
, TCG_REG_R3
);
814 tcg_out_mov(s
, TCG_TYPE_I32
, datalo
, TCG_REG_R4
);
816 tcg_out_mov(s
, TCG_TYPE_I32
, TCG_REG_R0
, TCG_REG_R4
);
817 tcg_out_mov(s
, TCG_TYPE_I32
, datahi
, TCG_REG_R3
);
818 tcg_out_mov(s
, TCG_TYPE_I32
, datalo
, TCG_REG_R0
);
822 tcg_out_b(s
, 0, l
->raddr
);
825 static void tcg_out_qemu_st_slow_path(TCGContext
*s
, TCGLabelQemuLdst
*l
)
828 TCGMemOp opc
= l
->opc
;
830 reloc_pc14(l
->label_ptr
[0], s
->code_ptr
);
833 if (TARGET_LONG_BITS
== 32) {
834 tcg_out_mov (s
, TCG_TYPE_I32
, ir
++, l
->addrlo_reg
);
836 #ifdef TCG_TARGET_CALL_ALIGN_ARGS
839 tcg_out_mov (s
, TCG_TYPE_I32
, ir
++, l
->addrhi_reg
);
840 tcg_out_mov (s
, TCG_TYPE_I32
, ir
++, l
->addrlo_reg
);
843 datalo
= l
->datalo_reg
;
844 switch (opc
& MO_SIZE
) {
846 tcg_out32(s
, (RLWINM
| RA (ir
) | RS (datalo
)
847 | SH (0) | MB (24) | ME (31)));
850 tcg_out32(s
, (RLWINM
| RA (ir
) | RS (datalo
)
851 | SH (0) | MB (16) | ME (31)));
854 tcg_out_mov(s
, TCG_TYPE_I32
, ir
, datalo
);
857 #ifdef TCG_TARGET_CALL_ALIGN_ARGS
860 tcg_out_mov(s
, TCG_TYPE_I32
, ir
++, l
->datahi_reg
);
861 tcg_out_mov(s
, TCG_TYPE_I32
, ir
, datalo
);
866 tcg_out_movi(s
, TCG_TYPE_I32
, ir
++, l
->mem_index
);
867 tcg_out32(s
, MFSPR
| RT(ir
++) | LR
);
868 tcg_out_b(s
, LK
, st_trampolines
[opc
]);
869 tcg_out_b(s
, 0, l
->raddr
);
873 #ifdef CONFIG_SOFTMMU
874 static void emit_ldst_trampoline(TCGContext
*s
, tcg_insn_unit
*ptr
)
876 tcg_out_mov(s
, TCG_TYPE_PTR
, TCG_REG_R3
, TCG_AREG0
);
877 tcg_out_call1(s
, ptr
, 0);
881 static void tcg_target_qemu_prologue (TCGContext
*s
)
887 + TCG_STATIC_CALL_ARGS_SIZE
888 + ARRAY_SIZE (tcg_target_callee_save_regs
) * 4
889 + CPU_TEMP_BUF_NLONGS
* sizeof(long)
891 frame_size
= (frame_size
+ 15) & ~15;
893 tcg_set_frame(s
, TCG_REG_CALL_STACK
, frame_size
894 - CPU_TEMP_BUF_NLONGS
* sizeof(long),
895 CPU_TEMP_BUF_NLONGS
* sizeof(long));
901 /* First emit adhoc function descriptor */
902 addr
= (uintptr_t)s
->code_ptr
+ 12;
903 tcg_out32(s
, addr
); /* entry point */
904 tcg_out32(s
, 0); /* toc */
905 tcg_out32(s
, 0); /* environment pointer */
908 tcg_out32 (s
, MFSPR
| RT (0) | LR
);
909 tcg_out32 (s
, STWU
| RS (1) | RA (1) | (-frame_size
& 0xffff));
910 for (i
= 0; i
< ARRAY_SIZE (tcg_target_callee_save_regs
); ++i
)
912 | RS (tcg_target_callee_save_regs
[i
])
914 | (i
* 4 + LINKAGE_AREA_SIZE
+ TCG_STATIC_CALL_ARGS_SIZE
)
917 tcg_out32 (s
, STW
| RS (0) | RA (1) | (frame_size
+ LR_OFFSET
));
919 #ifdef CONFIG_USE_GUEST_BASE
921 tcg_out_movi (s
, TCG_TYPE_I32
, TCG_GUEST_BASE_REG
, GUEST_BASE
);
922 tcg_regset_set_reg(s
->reserved_regs
, TCG_GUEST_BASE_REG
);
926 tcg_out_mov (s
, TCG_TYPE_PTR
, TCG_AREG0
, tcg_target_call_iarg_regs
[0]);
927 tcg_out32 (s
, MTSPR
| RS (tcg_target_call_iarg_regs
[1]) | CTR
);
928 tcg_out32 (s
, BCCTR
| BO_ALWAYS
);
929 tb_ret_addr
= s
->code_ptr
;
931 for (i
= 0; i
< ARRAY_SIZE (tcg_target_callee_save_regs
); ++i
)
933 | RT (tcg_target_callee_save_regs
[i
])
935 | (i
* 4 + LINKAGE_AREA_SIZE
+ TCG_STATIC_CALL_ARGS_SIZE
)
938 tcg_out32 (s
, LWZ
| RT (0) | RA (1) | (frame_size
+ LR_OFFSET
));
939 tcg_out32 (s
, MTSPR
| RS (0) | LR
);
940 tcg_out32 (s
, ADDI
| RT (1) | RA (1) | frame_size
);
941 tcg_out32 (s
, BCLR
| BO_ALWAYS
);
943 #ifdef CONFIG_SOFTMMU
944 for (i
= 0; i
< 16; ++i
) {
945 if (qemu_ld_helpers
[i
]) {
946 ld_trampolines
[i
] = s
->code_ptr
;
947 emit_ldst_trampoline(s
, qemu_ld_helpers
[i
]);
949 if (qemu_st_helpers
[i
]) {
950 st_trampolines
[i
] = s
->code_ptr
;
951 emit_ldst_trampoline(s
, qemu_st_helpers
[i
]);
957 static void tcg_out_ld(TCGContext
*s
, TCGType type
, TCGReg ret
, TCGReg arg1
,
960 tcg_out_ldst (s
, ret
, arg1
, arg2
, LWZ
, LWZX
);
963 static void tcg_out_st(TCGContext
*s
, TCGType type
, TCGReg arg
, TCGReg arg1
,
966 tcg_out_ldst (s
, arg
, arg1
, arg2
, STW
, STWX
);
969 static void ppc_addi (TCGContext
*s
, int rt
, int ra
, tcg_target_long si
)
974 if (si
== (int16_t) si
)
975 tcg_out32 (s
, ADDI
| RT (rt
) | RA (ra
) | (si
& 0xffff));
977 uint16_t h
= ((si
>> 16) & 0xffff) + ((uint16_t) si
>> 15);
978 tcg_out32 (s
, ADDIS
| RT (rt
) | RA (ra
) | h
);
979 tcg_out32 (s
, ADDI
| RT (rt
) | RA (rt
) | (si
& 0xffff));
983 static void tcg_out_cmp (TCGContext
*s
, int cond
, TCGArg arg1
, TCGArg arg2
,
984 int const_arg2
, int cr
)
993 if ((int16_t) arg2
== arg2
) {
998 else if ((uint16_t) arg2
== arg2
) {
1013 if ((int16_t) arg2
== arg2
) {
1028 if ((uint16_t) arg2
== arg2
) {
1044 tcg_out32 (s
, op
| RA (arg1
) | (arg2
& 0xffff));
1047 tcg_out_movi (s
, TCG_TYPE_I32
, 0, arg2
);
1048 tcg_out32 (s
, op
| RA (arg1
) | RB (0));
1051 tcg_out32 (s
, op
| RA (arg1
) | RB (arg2
));
1056 static void tcg_out_bc(TCGContext
*s
, int bc
, int label_index
)
1058 TCGLabel
*l
= &s
->labels
[label_index
];
1061 tcg_out32(s
, bc
| reloc_pc14_val(s
->code_ptr
, l
->u
.value_ptr
));
1063 /* Thanks to Andrzej Zaborowski */
1064 tcg_insn_unit retrans
= *s
->code_ptr
& 0xfffc;
1065 tcg_out_reloc(s
, s
->code_ptr
, R_PPC_REL14
, label_index
, 0);
1066 tcg_out32(s
, bc
| retrans
);
1070 static void tcg_out_cr7eq_from_cond (TCGContext
*s
, const TCGArg
*args
,
1071 const int *const_args
)
1073 TCGCond cond
= args
[4];
1075 struct { int bit1
; int bit2
; int cond2
; } bits
[] = {
1076 [TCG_COND_LT
] = { CR_LT
, CR_LT
, TCG_COND_LT
},
1077 [TCG_COND_LE
] = { CR_LT
, CR_GT
, TCG_COND_LT
},
1078 [TCG_COND_GT
] = { CR_GT
, CR_GT
, TCG_COND_GT
},
1079 [TCG_COND_GE
] = { CR_GT
, CR_LT
, TCG_COND_GT
},
1080 [TCG_COND_LTU
] = { CR_LT
, CR_LT
, TCG_COND_LTU
},
1081 [TCG_COND_LEU
] = { CR_LT
, CR_GT
, TCG_COND_LTU
},
1082 [TCG_COND_GTU
] = { CR_GT
, CR_GT
, TCG_COND_GTU
},
1083 [TCG_COND_GEU
] = { CR_GT
, CR_LT
, TCG_COND_GTU
},
1084 }, *b
= &bits
[cond
];
1089 op
= (cond
== TCG_COND_EQ
) ? CRAND
: CRNAND
;
1090 tcg_out_cmp (s
, cond
, args
[0], args
[2], const_args
[2], 6);
1091 tcg_out_cmp (s
, cond
, args
[1], args
[3], const_args
[3], 7);
1092 tcg_out32 (s
, op
| BT (7, CR_EQ
) | BA (6, CR_EQ
) | BB (7, CR_EQ
));
1102 op
= (b
->bit1
!= b
->bit2
) ? CRANDC
: CRAND
;
1103 tcg_out_cmp (s
, b
->cond2
, args
[1], args
[3], const_args
[3], 5);
1104 tcg_out_cmp (s
, tcg_unsigned_cond (cond
), args
[0], args
[2],
1106 tcg_out32 (s
, op
| BT (7, CR_EQ
) | BA (5, CR_EQ
) | BB (7, b
->bit2
));
1107 tcg_out32 (s
, CROR
| BT (7, CR_EQ
) | BA (5, b
->bit1
) | BB (7, CR_EQ
));
1114 static void tcg_out_setcond (TCGContext
*s
, TCGCond cond
, TCGArg arg0
,
1115 TCGArg arg1
, TCGArg arg2
, int const_arg2
)
1127 if ((uint16_t) arg2
== arg2
) {
1128 tcg_out32 (s
, XORI
| RS (arg1
) | RA (0) | arg2
);
1131 tcg_out_movi (s
, TCG_TYPE_I32
, 0, arg2
);
1132 tcg_out32 (s
, XOR
| SAB (arg1
, 0, 0));
1138 tcg_out32 (s
, XOR
| SAB (arg1
, 0, arg2
));
1140 tcg_out32 (s
, CNTLZW
| RS (arg
) | RA (0));
1141 tcg_out32 (s
, (RLWINM
1158 if ((uint16_t) arg2
== arg2
) {
1159 tcg_out32 (s
, XORI
| RS (arg1
) | RA (0) | arg2
);
1162 tcg_out_movi (s
, TCG_TYPE_I32
, 0, arg2
);
1163 tcg_out32 (s
, XOR
| SAB (arg1
, 0, 0));
1169 tcg_out32 (s
, XOR
| SAB (arg1
, 0, arg2
));
1172 if (arg
== arg1
&& arg1
== arg0
) {
1173 tcg_out32 (s
, ADDIC
| RT (0) | RA (arg
) | 0xffff);
1174 tcg_out32 (s
, SUBFE
| TAB (arg0
, 0, arg
));
1177 tcg_out32 (s
, ADDIC
| RT (arg0
) | RA (arg
) | 0xffff);
1178 tcg_out32 (s
, SUBFE
| TAB (arg0
, arg0
, arg
));
1197 crop
= CRNOR
| BT (7, CR_EQ
) | BA (7, CR_LT
) | BB (7, CR_LT
);
1203 crop
= CRNOR
| BT (7, CR_EQ
) | BA (7, CR_GT
) | BB (7, CR_GT
);
1205 tcg_out_cmp (s
, cond
, arg1
, arg2
, const_arg2
, 7);
1206 if (crop
) tcg_out32 (s
, crop
);
1207 tcg_out32 (s
, MFCR
| RT (0));
1208 tcg_out32 (s
, (RLWINM
1223 static void tcg_out_setcond2 (TCGContext
*s
, const TCGArg
*args
,
1224 const int *const_args
)
1226 tcg_out_cr7eq_from_cond (s
, args
+ 1, const_args
+ 1);
1227 tcg_out32 (s
, MFCR
| RT (0));
1228 tcg_out32 (s
, (RLWINM
1238 static void tcg_out_movcond (TCGContext
*s
, TCGCond cond
,
1240 TCGArg c1
, TCGArg c2
,
1241 TCGArg v1
, TCGArg v2
,
1244 tcg_out_cmp (s
, cond
, c1
, c2
, const_c2
, 7);
1247 /* At least here on 7747A bit twiddling hacks are outperformed
1248 by jumpy code (the testing was not scientific) */
1250 cond
= tcg_invert_cond (cond
);
1255 tcg_out_mov (s
, TCG_TYPE_I32
, dest
, v1
);
1258 /* Branch forward over one insn */
1259 tcg_out32 (s
, tcg_to_bc
[cond
] | 8);
1260 tcg_out_mov (s
, TCG_TYPE_I32
, dest
, v2
);
1263 /* isel version, "if (1)" above should be replaced once a way
1264 to figure out availability of isel on the underlying
1265 hardware is found */
1270 tab
= TAB (dest
, v1
, v2
);
1274 tab
= TAB (dest
, v2
, v1
);
1279 tab
= TAB (dest
, v1
, v2
);
1284 tab
= TAB (dest
, v2
, v1
);
1289 tab
= TAB (dest
, v2
, v1
);
1294 tab
= TAB (dest
, v1
, v2
);
1300 tcg_out32 (s
, ISEL
| tab
| ((bc
+ 28) << 6));
1304 static void tcg_out_brcond (TCGContext
*s
, TCGCond cond
,
1305 TCGArg arg1
, TCGArg arg2
, int const_arg2
,
1308 tcg_out_cmp (s
, cond
, arg1
, arg2
, const_arg2
, 7);
1309 tcg_out_bc (s
, tcg_to_bc
[cond
], label_index
);
1312 /* XXX: we implement it at the target level to avoid having to
1313 handle cross basic blocks temporaries */
1314 static void tcg_out_brcond2 (TCGContext
*s
, const TCGArg
*args
,
1315 const int *const_args
)
1317 tcg_out_cr7eq_from_cond (s
, args
, const_args
);
1318 tcg_out_bc (s
, (BC
| BI (7, CR_EQ
) | BO_COND_TRUE
), args
[5]);
1321 void ppc_tb_set_jmp_target (unsigned long jmp_addr
, unsigned long addr
)
1324 long disp
= addr
- jmp_addr
;
1325 unsigned long patch_size
;
1327 ptr
= (uint32_t *)jmp_addr
;
1329 if ((disp
<< 6) >> 6 != disp
) {
1330 ptr
[0] = 0x3c000000 | (addr
>> 16); /* lis 0,addr@ha */
1331 ptr
[1] = 0x60000000 | (addr
& 0xffff); /* la 0,addr@l(0) */
1332 ptr
[2] = 0x7c0903a6; /* mtctr 0 */
1333 ptr
[3] = 0x4e800420; /* brctr */
1336 /* patch the branch destination */
1338 *ptr
= 0x48000000 | (disp
& 0x03fffffc); /* b disp */
1341 ptr
[0] = 0x60000000; /* nop */
1342 ptr
[1] = 0x60000000;
1343 ptr
[2] = 0x60000000;
1344 ptr
[3] = 0x60000000;
1349 flush_icache_range(jmp_addr
, jmp_addr
+ patch_size
);
1352 static void tcg_out_op(TCGContext
*s
, TCGOpcode opc
, const TCGArg
*args
,
1353 const int *const_args
)
1356 case INDEX_op_exit_tb
:
1357 tcg_out_movi(s
, TCG_TYPE_I32
, TCG_REG_R3
, args
[0]);
1358 tcg_out_b(s
, 0, tb_ret_addr
);
1360 case INDEX_op_goto_tb
:
1361 if (s
->tb_jmp_offset
) {
1362 /* direct jump method */
1363 s
->tb_jmp_offset
[args
[0]] = tcg_current_code_size(s
);
1368 s
->tb_next_offset
[args
[0]] = tcg_current_code_size(s
);
1372 TCGLabel
*l
= &s
->labels
[args
[0]];
1375 tcg_out_b(s
, 0, l
->u
.value_ptr
);
1377 /* Thanks to Andrzej Zaborowski */
1378 tcg_insn_unit retrans
= *s
->code_ptr
& 0x3fffffc;
1379 tcg_out_reloc(s
, s
->code_ptr
, R_PPC_REL24
, args
[0], 0);
1380 tcg_out32(s
, B
| retrans
);
1384 case INDEX_op_ld8u_i32
:
1385 tcg_out_ldst (s
, args
[0], args
[1], args
[2], LBZ
, LBZX
);
1387 case INDEX_op_ld8s_i32
:
1388 tcg_out_ldst (s
, args
[0], args
[1], args
[2], LBZ
, LBZX
);
1389 tcg_out32 (s
, EXTSB
| RS (args
[0]) | RA (args
[0]));
1391 case INDEX_op_ld16u_i32
:
1392 tcg_out_ldst (s
, args
[0], args
[1], args
[2], LHZ
, LHZX
);
1394 case INDEX_op_ld16s_i32
:
1395 tcg_out_ldst (s
, args
[0], args
[1], args
[2], LHA
, LHAX
);
1397 case INDEX_op_ld_i32
:
1398 tcg_out_ldst (s
, args
[0], args
[1], args
[2], LWZ
, LWZX
);
1400 case INDEX_op_st8_i32
:
1401 tcg_out_ldst (s
, args
[0], args
[1], args
[2], STB
, STBX
);
1403 case INDEX_op_st16_i32
:
1404 tcg_out_ldst (s
, args
[0], args
[1], args
[2], STH
, STHX
);
1406 case INDEX_op_st_i32
:
1407 tcg_out_ldst (s
, args
[0], args
[1], args
[2], STW
, STWX
);
1410 case INDEX_op_add_i32
:
1412 ppc_addi (s
, args
[0], args
[1], args
[2]);
1414 tcg_out32 (s
, ADD
| TAB (args
[0], args
[1], args
[2]));
1416 case INDEX_op_sub_i32
:
1418 ppc_addi (s
, args
[0], args
[1], -args
[2]);
1420 tcg_out32 (s
, SUBF
| TAB (args
[0], args
[2], args
[1]));
1423 case INDEX_op_and_i32
:
1424 if (const_args
[2]) {
1430 tcg_out_movi (s
, TCG_TYPE_I32
, args
[0], 0);
1440 if ((t
& (t
- 1)) == 0) {
1443 if ((c
& 0x80000001) == 0x80000001) {
1458 tcg_out32 (s
, (RLWINM
1468 #endif /* !__PPU__ */
1470 if ((c
& 0xffff) == c
)
1471 tcg_out32 (s
, ANDI
| RS (args
[1]) | RA (args
[0]) | c
);
1472 else if ((c
& 0xffff0000) == c
)
1473 tcg_out32 (s
, ANDIS
| RS (args
[1]) | RA (args
[0])
1474 | ((c
>> 16) & 0xffff));
1476 tcg_out_movi (s
, TCG_TYPE_I32
, 0, c
);
1477 tcg_out32 (s
, AND
| SAB (args
[1], args
[0], 0));
1482 tcg_out32 (s
, AND
| SAB (args
[1], args
[0], args
[2]));
1484 case INDEX_op_or_i32
:
1485 if (const_args
[2]) {
1486 if (args
[2] & 0xffff) {
1487 tcg_out32 (s
, ORI
| RS (args
[1]) | RA (args
[0])
1488 | (args
[2] & 0xffff));
1490 tcg_out32 (s
, ORIS
| RS (args
[0]) | RA (args
[0])
1491 | ((args
[2] >> 16) & 0xffff));
1494 tcg_out32 (s
, ORIS
| RS (args
[1]) | RA (args
[0])
1495 | ((args
[2] >> 16) & 0xffff));
1499 tcg_out32 (s
, OR
| SAB (args
[1], args
[0], args
[2]));
1501 case INDEX_op_xor_i32
:
1502 if (const_args
[2]) {
1503 if ((args
[2] & 0xffff) == args
[2])
1504 tcg_out32 (s
, XORI
| RS (args
[1]) | RA (args
[0])
1505 | (args
[2] & 0xffff));
1506 else if ((args
[2] & 0xffff0000) == args
[2])
1507 tcg_out32 (s
, XORIS
| RS (args
[1]) | RA (args
[0])
1508 | ((args
[2] >> 16) & 0xffff));
1510 tcg_out_movi (s
, TCG_TYPE_I32
, 0, args
[2]);
1511 tcg_out32 (s
, XOR
| SAB (args
[1], args
[0], 0));
1515 tcg_out32 (s
, XOR
| SAB (args
[1], args
[0], args
[2]));
1517 case INDEX_op_andc_i32
:
1518 tcg_out32 (s
, ANDC
| SAB (args
[1], args
[0], args
[2]));
1520 case INDEX_op_orc_i32
:
1521 tcg_out32 (s
, ORC
| SAB (args
[1], args
[0], args
[2]));
1523 case INDEX_op_eqv_i32
:
1524 tcg_out32 (s
, EQV
| SAB (args
[1], args
[0], args
[2]));
1526 case INDEX_op_nand_i32
:
1527 tcg_out32 (s
, NAND
| SAB (args
[1], args
[0], args
[2]));
1529 case INDEX_op_nor_i32
:
1530 tcg_out32 (s
, NOR
| SAB (args
[1], args
[0], args
[2]));
1533 case INDEX_op_mul_i32
:
1534 if (const_args
[2]) {
1535 if (args
[2] == (int16_t) args
[2])
1536 tcg_out32 (s
, MULLI
| RT (args
[0]) | RA (args
[1])
1537 | (args
[2] & 0xffff));
1539 tcg_out_movi (s
, TCG_TYPE_I32
, 0, args
[2]);
1540 tcg_out32 (s
, MULLW
| TAB (args
[0], args
[1], 0));
1544 tcg_out32 (s
, MULLW
| TAB (args
[0], args
[1], args
[2]));
1547 case INDEX_op_div_i32
:
1548 tcg_out32 (s
, DIVW
| TAB (args
[0], args
[1], args
[2]));
1551 case INDEX_op_divu_i32
:
1552 tcg_out32 (s
, DIVWU
| TAB (args
[0], args
[1], args
[2]));
1555 case INDEX_op_mulu2_i32
:
1556 if (args
[0] == args
[2] || args
[0] == args
[3]) {
1557 tcg_out32 (s
, MULLW
| TAB (0, args
[2], args
[3]));
1558 tcg_out32 (s
, MULHWU
| TAB (args
[1], args
[2], args
[3]));
1559 tcg_out_mov (s
, TCG_TYPE_I32
, args
[0], 0);
1562 tcg_out32 (s
, MULLW
| TAB (args
[0], args
[2], args
[3]));
1563 tcg_out32 (s
, MULHWU
| TAB (args
[1], args
[2], args
[3]));
1567 case INDEX_op_shl_i32
:
1568 if (const_args
[2]) {
1569 tcg_out32 (s
, (RLWINM
1579 tcg_out32 (s
, SLW
| SAB (args
[1], args
[0], args
[2]));
1581 case INDEX_op_shr_i32
:
1582 if (const_args
[2]) {
1583 tcg_out32 (s
, (RLWINM
1593 tcg_out32 (s
, SRW
| SAB (args
[1], args
[0], args
[2]));
1595 case INDEX_op_sar_i32
:
1597 tcg_out32 (s
, SRAWI
| RS (args
[1]) | RA (args
[0]) | SH (args
[2]));
1599 tcg_out32 (s
, SRAW
| SAB (args
[1], args
[0], args
[2]));
1601 case INDEX_op_rotl_i32
:
1608 | (const_args
[2] ? RLWINM
| SH (args
[2])
1609 : RLWNM
| RB (args
[2]))
1614 case INDEX_op_rotr_i32
:
1615 if (const_args
[2]) {
1617 tcg_out_mov (s
, TCG_TYPE_I32
, args
[0], args
[1]);
1620 tcg_out32 (s
, RLWINM
1630 tcg_out32 (s
, SUBFIC
| RT (0) | RA (args
[2]) | 32);
1641 case INDEX_op_add2_i32
:
1642 if (args
[0] == args
[3] || args
[0] == args
[5]) {
1643 tcg_out32 (s
, ADDC
| TAB (0, args
[2], args
[4]));
1644 tcg_out32 (s
, ADDE
| TAB (args
[1], args
[3], args
[5]));
1645 tcg_out_mov (s
, TCG_TYPE_I32
, args
[0], 0);
1648 tcg_out32 (s
, ADDC
| TAB (args
[0], args
[2], args
[4]));
1649 tcg_out32 (s
, ADDE
| TAB (args
[1], args
[3], args
[5]));
1652 case INDEX_op_sub2_i32
:
1653 if (args
[0] == args
[3] || args
[0] == args
[5]) {
1654 tcg_out32 (s
, SUBFC
| TAB (0, args
[4], args
[2]));
1655 tcg_out32 (s
, SUBFE
| TAB (args
[1], args
[5], args
[3]));
1656 tcg_out_mov (s
, TCG_TYPE_I32
, args
[0], 0);
1659 tcg_out32 (s
, SUBFC
| TAB (args
[0], args
[4], args
[2]));
1660 tcg_out32 (s
, SUBFE
| TAB (args
[1], args
[5], args
[3]));
1664 case INDEX_op_brcond_i32
:
1669 args[3] = r1 is const
1670 args[4] = label_index
1672 tcg_out_brcond (s
, args
[2], args
[0], args
[1], const_args
[1], args
[3]);
1674 case INDEX_op_brcond2_i32
:
1675 tcg_out_brcond2(s
, args
, const_args
);
1678 case INDEX_op_neg_i32
:
1679 tcg_out32 (s
, NEG
| RT (args
[0]) | RA (args
[1]));
1682 case INDEX_op_not_i32
:
1683 tcg_out32 (s
, NOR
| SAB (args
[1], args
[0], args
[1]));
1686 case INDEX_op_qemu_ld_i32
:
1687 tcg_out_qemu_ld(s
, args
, 0);
1689 case INDEX_op_qemu_ld_i64
:
1690 tcg_out_qemu_ld(s
, args
, 1);
1692 case INDEX_op_qemu_st_i32
:
1693 tcg_out_qemu_st(s
, args
, 0);
1695 case INDEX_op_qemu_st_i64
:
1696 tcg_out_qemu_st(s
, args
, 1);
1699 case INDEX_op_ext8s_i32
:
1700 tcg_out32 (s
, EXTSB
| RS (args
[1]) | RA (args
[0]));
1702 case INDEX_op_ext8u_i32
:
1703 tcg_out32 (s
, RLWINM
1711 case INDEX_op_ext16s_i32
:
1712 tcg_out32 (s
, EXTSH
| RS (args
[1]) | RA (args
[0]));
1714 case INDEX_op_ext16u_i32
:
1715 tcg_out32 (s
, RLWINM
1724 case INDEX_op_setcond_i32
:
1725 tcg_out_setcond (s
, args
[3], args
[0], args
[1], args
[2], const_args
[2]);
1727 case INDEX_op_setcond2_i32
:
1728 tcg_out_setcond2 (s
, args
, const_args
);
1731 case INDEX_op_bswap16_i32
:
1732 /* Stolen from gcc's builtin_bswap16 */
1736 /* r0 = (a1 << 8) & 0xff00 # 00d0 */
1737 tcg_out32 (s
, RLWINM
1745 /* a0 = rotate_left (a1, 24) & 0xff # 000c */
1746 tcg_out32 (s
, RLWINM
1754 /* a0 = a0 | r0 # 00dc */
1755 tcg_out32 (s
, OR
| SAB (0, args
[0], args
[0]));
1758 case INDEX_op_bswap32_i32
:
1759 /* Stolen from gcc's builtin_bswap32 */
1763 /* a1 = args[1] # abcd */
1765 if (a0
== args
[1]) {
1769 /* a0 = rotate_left (a1, 8) # bcda */
1770 tcg_out32 (s
, RLWINM
1778 /* a0 = (a0 & ~0xff000000) | ((a1 << 24) & 0xff000000) # dcda */
1779 tcg_out32 (s
, RLWIMI
1787 /* a0 = (a0 & ~0x0000ff00) | ((a1 << 24) & 0x0000ff00) # dcba */
1788 tcg_out32 (s
, RLWIMI
1797 tcg_out_mov (s
, TCG_TYPE_I32
, args
[0], a0
);
1802 case INDEX_op_deposit_i32
:
1803 tcg_out32 (s
, RLWIMI
1807 | MB (32 - args
[3] - args
[4])
1812 case INDEX_op_movcond_i32
:
1813 tcg_out_movcond (s
, args
[5], args
[0],
1819 case INDEX_op_mov_i32
: /* Always emitted via tcg_out_mov. */
1820 case INDEX_op_movi_i32
: /* Always emitted via tcg_out_movi. */
1821 case INDEX_op_call
: /* Always emitted via tcg_out_call. */
1827 static const TCGTargetOpDef ppc_op_defs
[] = {
1828 { INDEX_op_exit_tb
, { } },
1829 { INDEX_op_goto_tb
, { } },
1830 { INDEX_op_br
, { } },
1832 { INDEX_op_ld8u_i32
, { "r", "r" } },
1833 { INDEX_op_ld8s_i32
, { "r", "r" } },
1834 { INDEX_op_ld16u_i32
, { "r", "r" } },
1835 { INDEX_op_ld16s_i32
, { "r", "r" } },
1836 { INDEX_op_ld_i32
, { "r", "r" } },
1837 { INDEX_op_st8_i32
, { "r", "r" } },
1838 { INDEX_op_st16_i32
, { "r", "r" } },
1839 { INDEX_op_st_i32
, { "r", "r" } },
1841 { INDEX_op_add_i32
, { "r", "r", "ri" } },
1842 { INDEX_op_mul_i32
, { "r", "r", "ri" } },
1843 { INDEX_op_div_i32
, { "r", "r", "r" } },
1844 { INDEX_op_divu_i32
, { "r", "r", "r" } },
1845 { INDEX_op_mulu2_i32
, { "r", "r", "r", "r" } },
1846 { INDEX_op_sub_i32
, { "r", "r", "ri" } },
1847 { INDEX_op_and_i32
, { "r", "r", "ri" } },
1848 { INDEX_op_or_i32
, { "r", "r", "ri" } },
1849 { INDEX_op_xor_i32
, { "r", "r", "ri" } },
1851 { INDEX_op_shl_i32
, { "r", "r", "ri" } },
1852 { INDEX_op_shr_i32
, { "r", "r", "ri" } },
1853 { INDEX_op_sar_i32
, { "r", "r", "ri" } },
1855 { INDEX_op_rotl_i32
, { "r", "r", "ri" } },
1856 { INDEX_op_rotr_i32
, { "r", "r", "ri" } },
1858 { INDEX_op_brcond_i32
, { "r", "ri" } },
1860 { INDEX_op_add2_i32
, { "r", "r", "r", "r", "r", "r" } },
1861 { INDEX_op_sub2_i32
, { "r", "r", "r", "r", "r", "r" } },
1862 { INDEX_op_brcond2_i32
, { "r", "r", "r", "r" } },
1864 { INDEX_op_neg_i32
, { "r", "r" } },
1865 { INDEX_op_not_i32
, { "r", "r" } },
1867 { INDEX_op_andc_i32
, { "r", "r", "r" } },
1868 { INDEX_op_orc_i32
, { "r", "r", "r" } },
1869 { INDEX_op_eqv_i32
, { "r", "r", "r" } },
1870 { INDEX_op_nand_i32
, { "r", "r", "r" } },
1871 { INDEX_op_nor_i32
, { "r", "r", "r" } },
1873 { INDEX_op_setcond_i32
, { "r", "r", "ri" } },
1874 { INDEX_op_setcond2_i32
, { "r", "r", "r", "ri", "ri" } },
1876 { INDEX_op_bswap16_i32
, { "r", "r" } },
1877 { INDEX_op_bswap32_i32
, { "r", "r" } },
1879 #if TARGET_LONG_BITS == 32
1880 { INDEX_op_qemu_ld_i32
, { "r", "L" } },
1881 { INDEX_op_qemu_ld_i64
, { "L", "L", "L" } },
1882 { INDEX_op_qemu_st_i32
, { "K", "K" } },
1883 { INDEX_op_qemu_st_i64
, { "M", "M", "M" } },
1885 { INDEX_op_qemu_ld_i32
, { "r", "L", "L" } },
1886 { INDEX_op_qemu_ld_i64
, { "L", "L", "L", "L" } },
1887 { INDEX_op_qemu_st_i32
, { "K", "K", "K" } },
1888 { INDEX_op_qemu_st_i64
, { "M", "M", "M", "M" } },
1891 { INDEX_op_ext8s_i32
, { "r", "r" } },
1892 { INDEX_op_ext8u_i32
, { "r", "r" } },
1893 { INDEX_op_ext16s_i32
, { "r", "r" } },
1894 { INDEX_op_ext16u_i32
, { "r", "r" } },
1896 { INDEX_op_deposit_i32
, { "r", "0", "r" } },
1897 { INDEX_op_movcond_i32
, { "r", "r", "ri", "r", "r" } },
1902 static void tcg_target_init(TCGContext
*s
)
1904 tcg_regset_set32(tcg_target_available_regs
[TCG_TYPE_I32
], 0, 0xffffffff);
1905 tcg_regset_set32(tcg_target_call_clobber_regs
, 0,
1907 #ifdef TCG_TARGET_CALL_DARWIN
1917 (1 << TCG_REG_R10
) |
1918 (1 << TCG_REG_R11
) |
1922 tcg_regset_clear(s
->reserved_regs
);
1923 tcg_regset_set_reg(s
->reserved_regs
, TCG_REG_R0
);
1924 tcg_regset_set_reg(s
->reserved_regs
, TCG_REG_R1
);
1925 #ifndef TCG_TARGET_CALL_DARWIN
1926 tcg_regset_set_reg(s
->reserved_regs
, TCG_REG_R2
);
1929 tcg_regset_set_reg(s
->reserved_regs
, TCG_REG_R13
);
1932 tcg_add_target_add_op_defs(ppc_op_defs
);