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 static uint8_t *tb_ret_addr
;
28 #define LINKAGE_AREA_SIZE 24
29 #define BACK_CHAIN_OFFSET 8
31 #define LINKAGE_AREA_SIZE 8
32 #define BACK_CHAIN_OFFSET 4
36 #if TARGET_PHYS_ADDR_BITS <= 32
37 #define ADDEND_OFFSET 0
39 #define ADDEND_OFFSET 4
42 static const char * const tcg_target_reg_names
[TCG_TARGET_NB_REGS
] = {
77 static const int tcg_target_reg_alloc_order
[] = {
117 static const int tcg_target_call_iarg_regs
[] = {
128 static const int tcg_target_call_oarg_regs
[2] = {
133 static const int tcg_target_callee_save_regs
[] = {
154 static uint32_t reloc_pc24_val (void *pc
, tcg_target_long target
)
156 tcg_target_long disp
;
158 disp
= target
- (tcg_target_long
) pc
;
159 if ((disp
<< 6) >> 6 != disp
)
162 return disp
& 0x3fffffc;
165 static void reloc_pc24 (void *pc
, tcg_target_long target
)
167 *(uint32_t *) pc
= (*(uint32_t *) pc
& ~0x3fffffc)
168 | reloc_pc24_val (pc
, target
);
171 static uint16_t reloc_pc14_val (void *pc
, tcg_target_long target
)
173 tcg_target_long disp
;
175 disp
= target
- (tcg_target_long
) pc
;
176 if (disp
!= (int16_t) disp
)
179 return disp
& 0xfffc;
182 static void reloc_pc14 (void *pc
, tcg_target_long target
)
184 *(uint32_t *) pc
= (*(uint32_t *) pc
& ~0xfffc)
185 | reloc_pc14_val (pc
, target
);
188 static void patch_reloc(uint8_t *code_ptr
, int type
,
189 tcg_target_long value
, tcg_target_long addend
)
194 reloc_pc14 (code_ptr
, value
);
197 reloc_pc24 (code_ptr
, value
);
204 /* maximum number of register used for input function arguments */
205 static int tcg_target_get_call_iarg_regs_count(int flags
)
207 return sizeof (tcg_target_call_iarg_regs
) / sizeof (tcg_target_call_iarg_regs
[0]);
210 /* parse target specific constraints */
211 static int target_parse_constraint(TCGArgConstraint
*ct
, const char **pct_str
)
217 case 'A': case 'B': case 'C': case 'D':
218 ct
->ct
|= TCG_CT_REG
;
219 tcg_regset_set_reg(ct
->u
.regs
, 3 + ct_str
[0] - 'A');
222 ct
->ct
|= TCG_CT_REG
;
223 tcg_regset_set32(ct
->u
.regs
, 0, 0xffffffff);
225 #ifdef CONFIG_SOFTMMU
226 case 'L': /* qemu_ld constraint */
227 ct
->ct
|= TCG_CT_REG
;
228 tcg_regset_set32(ct
->u
.regs
, 0, 0xffffffff);
229 tcg_regset_reset_reg(ct
->u
.regs
, TCG_REG_R3
);
230 tcg_regset_reset_reg(ct
->u
.regs
, TCG_REG_R4
);
232 case 'K': /* qemu_st[8..32] constraint */
233 ct
->ct
|= TCG_CT_REG
;
234 tcg_regset_set32(ct
->u
.regs
, 0, 0xffffffff);
235 tcg_regset_reset_reg(ct
->u
.regs
, TCG_REG_R3
);
236 tcg_regset_reset_reg(ct
->u
.regs
, TCG_REG_R4
);
237 tcg_regset_reset_reg(ct
->u
.regs
, TCG_REG_R5
);
238 #if TARGET_LONG_BITS == 64
239 tcg_regset_reset_reg(ct
->u
.regs
, TCG_REG_R6
);
242 case 'M': /* qemu_st64 constraint */
243 ct
->ct
|= TCG_CT_REG
;
244 tcg_regset_set32(ct
->u
.regs
, 0, 0xffffffff);
245 tcg_regset_reset_reg(ct
->u
.regs
, TCG_REG_R3
);
246 tcg_regset_reset_reg(ct
->u
.regs
, TCG_REG_R4
);
247 tcg_regset_reset_reg(ct
->u
.regs
, TCG_REG_R5
);
248 tcg_regset_reset_reg(ct
->u
.regs
, TCG_REG_R6
);
249 tcg_regset_reset_reg(ct
->u
.regs
, TCG_REG_R7
);
254 ct
->ct
|= TCG_CT_REG
;
255 tcg_regset_set32(ct
->u
.regs
, 0, 0xffffffff);
258 ct
->ct
|= TCG_CT_REG
;
259 tcg_regset_set32(ct
->u
.regs
, 0, 0xffffffff);
260 tcg_regset_reset_reg(ct
->u
.regs
, TCG_REG_R3
);
271 /* test if a constant matches the constraint */
272 static int tcg_target_const_match(tcg_target_long val
,
273 const TCGArgConstraint
*arg_ct
)
278 if (ct
& TCG_CT_CONST
)
283 #define OPCD(opc) ((opc)<<26)
284 #define XO31(opc) (OPCD(31)|((opc)<<1))
285 #define XO19(opc) (OPCD(19)|((opc)<<1))
297 #define ADDI OPCD(14)
298 #define ADDIS OPCD(15)
300 #define ORIS OPCD(25)
301 #define XORI OPCD(26)
302 #define XORIS OPCD(27)
303 #define ANDI OPCD(28)
304 #define ANDIS OPCD(29)
305 #define MULLI OPCD( 7)
306 #define CMPLI OPCD(10)
307 #define CMPI OPCD(11)
309 #define LWZU OPCD(33)
310 #define STWU OPCD(37)
312 #define RLWINM OPCD(21)
314 #define BCLR XO19( 16)
315 #define BCCTR XO19(528)
316 #define CRAND XO19(257)
317 #define CRANDC XO19(129)
318 #define CRNAND XO19(225)
319 #define CROR XO19(449)
321 #define EXTSB XO31(954)
322 #define EXTSH XO31(922)
323 #define ADD XO31(266)
324 #define ADDE XO31(138)
325 #define ADDC XO31( 10)
326 #define AND XO31( 28)
327 #define SUBF XO31( 40)
328 #define SUBFC XO31( 8)
329 #define SUBFE XO31(136)
331 #define XOR XO31(316)
332 #define MULLW XO31(235)
333 #define MULHWU XO31( 11)
334 #define DIVW XO31(491)
335 #define DIVWU XO31(459)
337 #define CMPL XO31( 32)
338 #define LHBRX XO31(790)
339 #define LWBRX XO31(534)
340 #define STHBRX XO31(918)
341 #define STWBRX XO31(662)
342 #define MFSPR XO31(339)
343 #define MTSPR XO31(467)
344 #define SRAWI XO31(824)
345 #define NEG XO31(104)
347 #define LBZX XO31( 87)
348 #define LHZX XO31(276)
349 #define LHAX XO31(343)
350 #define LWZX XO31( 23)
351 #define STBX XO31(215)
352 #define STHX XO31(407)
353 #define STWX XO31(151)
355 #define SPR(a,b) ((((a)<<5)|(b))<<11)
357 #define CTR SPR(9, 0)
359 #define SLW XO31( 24)
360 #define SRW XO31(536)
361 #define SRAW XO31(792)
364 #define STMW OPCD(47)
367 #define TRAP (TW | TO (31))
369 #define RT(r) ((r)<<21)
370 #define RS(r) ((r)<<21)
371 #define RA(r) ((r)<<16)
372 #define RB(r) ((r)<<11)
373 #define TO(t) ((t)<<21)
374 #define SH(s) ((s)<<11)
375 #define MB(b) ((b)<<6)
376 #define ME(e) ((e)<<1)
377 #define BO(o) ((o)<<21)
381 #define TAB(t,a,b) (RT(t) | RA(a) | RB(b))
382 #define SAB(s,a,b) (RS(s) | RA(a) | RB(b))
384 #define BF(n) ((n)<<23)
385 #define BI(n, c) (((c)+((n)*4))<<16)
386 #define BT(n, c) (((c)+((n)*4))<<21)
387 #define BA(n, c) (((c)+((n)*4))<<16)
388 #define BB(n, c) (((c)+((n)*4))<<11)
390 #define BO_COND_TRUE BO (12)
391 #define BO_COND_FALSE BO (4)
392 #define BO_ALWAYS BO (20)
401 static const uint32_t tcg_to_bc
[10] = {
402 [TCG_COND_EQ
] = BC
| BI (7, CR_EQ
) | BO_COND_TRUE
,
403 [TCG_COND_NE
] = BC
| BI (7, CR_EQ
) | BO_COND_FALSE
,
404 [TCG_COND_LT
] = BC
| BI (7, CR_LT
) | BO_COND_TRUE
,
405 [TCG_COND_GE
] = BC
| BI (7, CR_LT
) | BO_COND_FALSE
,
406 [TCG_COND_LE
] = BC
| BI (7, CR_GT
) | BO_COND_FALSE
,
407 [TCG_COND_GT
] = BC
| BI (7, CR_GT
) | BO_COND_TRUE
,
408 [TCG_COND_LTU
] = BC
| BI (7, CR_LT
) | BO_COND_TRUE
,
409 [TCG_COND_GEU
] = BC
| BI (7, CR_LT
) | BO_COND_FALSE
,
410 [TCG_COND_LEU
] = BC
| BI (7, CR_GT
) | BO_COND_FALSE
,
411 [TCG_COND_GTU
] = BC
| BI (7, CR_GT
) | BO_COND_TRUE
,
414 static void tcg_out_mov(TCGContext
*s
, int ret
, int arg
)
416 tcg_out32 (s
, OR
| SAB (arg
, ret
, arg
));
419 static void tcg_out_movi(TCGContext
*s
, TCGType type
,
420 int ret
, tcg_target_long arg
)
422 if (arg
== (int16_t) arg
)
423 tcg_out32 (s
, ADDI
| RT (ret
) | RA (0) | (arg
& 0xffff));
425 tcg_out32 (s
, ADDIS
| RT (ret
) | RA (0) | ((arg
>> 16) & 0xffff));
427 tcg_out32 (s
, ORI
| RS (ret
) | RA (ret
) | (arg
& 0xffff));
431 static void tcg_out_ldst (TCGContext
*s
, int ret
, int addr
,
432 int offset
, int op1
, int op2
)
434 if (offset
== (int16_t) offset
)
435 tcg_out32 (s
, op1
| RT (ret
) | RA (addr
) | (offset
& 0xffff));
437 tcg_out_movi (s
, TCG_TYPE_I32
, 0, offset
);
438 tcg_out32 (s
, op2
| RT (ret
) | RA (addr
) | RB (0));
442 static void tcg_out_b (TCGContext
*s
, int mask
, tcg_target_long target
)
444 tcg_target_long disp
;
446 disp
= target
- (tcg_target_long
) s
->code_ptr
;
447 if ((disp
<< 6) >> 6 == disp
)
448 tcg_out32 (s
, B
| (disp
& 0x3fffffc) | mask
);
450 tcg_out_movi (s
, TCG_TYPE_I32
, 0, (tcg_target_long
) target
);
451 tcg_out32 (s
, MTSPR
| RS (0) | CTR
);
452 tcg_out32 (s
, BCCTR
| BO_ALWAYS
| mask
);
456 #if defined(CONFIG_SOFTMMU)
458 #include "../../softmmu_defs.h"
460 static void *qemu_ld_helpers
[4] = {
467 static void *qemu_st_helpers
[4] = {
475 static void tcg_out_qemu_ld (TCGContext
*s
, const TCGArg
*args
, int opc
)
477 int addr_reg
, data_reg
, data_reg2
, r0
, r1
, mem_index
, s_bits
, bswap
;
478 #ifdef CONFIG_SOFTMMU
480 void *label1_ptr
, *label2_ptr
;
482 #if TARGET_LONG_BITS == 64
492 #if TARGET_LONG_BITS == 64
498 #ifdef CONFIG_SOFTMMU
503 tcg_out32 (s
, (RLWINM
506 | SH (32 - (TARGET_PAGE_BITS
- CPU_TLB_ENTRY_BITS
))
507 | MB (32 - (CPU_TLB_BITS
+ CPU_TLB_ENTRY_BITS
))
508 | ME (31 - CPU_TLB_ENTRY_BITS
)
511 tcg_out32 (s
, ADD
| RT (r0
) | RA (r0
) | RB (TCG_AREG0
));
515 | offsetof (CPUState
, tlb_table
[mem_index
][0].addr_read
)
518 tcg_out32 (s
, (RLWINM
522 | MB ((32 - s_bits
) & 31)
523 | ME (31 - TARGET_PAGE_BITS
)
527 tcg_out32 (s
, CMP
| BF (7) | RA (r2
) | RB (r1
));
528 #if TARGET_LONG_BITS == 64
529 tcg_out32 (s
, LWZ
| RT (r1
) | RA (r0
) | 4);
530 tcg_out32 (s
, CMP
| BF (6) | RA (addr_reg2
) | RB (r1
));
531 tcg_out32 (s
, CRAND
| BT (7, CR_EQ
) | BA (6, CR_EQ
) | BB (7, CR_EQ
));
534 label1_ptr
= s
->code_ptr
;
536 tcg_out32 (s
, BC
| BI (7, CR_EQ
) | BO_COND_TRUE
);
540 #if TARGET_LONG_BITS == 32
541 tcg_out_mov (s
, 3, addr_reg
);
542 tcg_out_movi (s
, TCG_TYPE_I32
, 4, mem_index
);
544 tcg_out_mov (s
, 3, addr_reg2
);
545 tcg_out_mov (s
, 4, addr_reg
);
546 tcg_out_movi (s
, TCG_TYPE_I32
, 5, mem_index
);
549 tcg_out_b (s
, LK
, (tcg_target_long
) qemu_ld_helpers
[s_bits
]);
552 tcg_out32 (s
, EXTSB
| RA (data_reg
) | RS (3));
555 tcg_out32 (s
, EXTSH
| RA (data_reg
) | RS (3));
561 tcg_out_mov (s
, data_reg
, 3);
565 if (data_reg2
== 4) {
566 tcg_out_mov (s
, 0, 4);
567 tcg_out_mov (s
, 4, 3);
568 tcg_out_mov (s
, 3, 0);
571 tcg_out_mov (s
, data_reg2
, 3);
572 tcg_out_mov (s
, 3, 4);
576 if (data_reg
!= 4) tcg_out_mov (s
, data_reg
, 4);
577 if (data_reg2
!= 3) tcg_out_mov (s
, data_reg2
, 3);
581 label2_ptr
= s
->code_ptr
;
584 /* label1: fast path */
586 reloc_pc14 (label1_ptr
, (tcg_target_long
) s
->code_ptr
);
589 /* r0 now contains &env->tlb_table[mem_index][index].addr_read */
593 | (ADDEND_OFFSET
+ offsetof (CPUTLBEntry
, addend
)
594 - offsetof (CPUTLBEntry
, addr_read
))
596 /* r0 = env->tlb_table[mem_index][index].addend */
597 tcg_out32 (s
, ADD
| RT (r0
) | RA (r0
) | RB (addr_reg
));
598 /* r0 = env->tlb_table[mem_index][index].addend + addr */
600 #else /* !CONFIG_SOFTMMU */
605 #ifdef TARGET_WORDS_BIGENDIAN
613 tcg_out32 (s
, LBZ
| RT (data_reg
) | RA (r0
));
616 tcg_out32 (s
, LBZ
| RT (data_reg
) | RA (r0
));
617 tcg_out32 (s
, EXTSB
| RA (data_reg
) | RS (data_reg
));
620 if (bswap
) tcg_out32 (s
, LHBRX
| RT (data_reg
) | RB (r0
));
621 else tcg_out32 (s
, LHZ
| RT (data_reg
) | RA (r0
));
625 tcg_out32 (s
, LHBRX
| RT (data_reg
) | RB (r0
));
626 tcg_out32 (s
, EXTSH
| RA (data_reg
) | RS (data_reg
));
628 else tcg_out32 (s
, LHA
| RT (data_reg
) | RA (r0
));
631 if (bswap
) tcg_out32 (s
, LWBRX
| RT (data_reg
) | RB (r0
));
632 else tcg_out32 (s
, LWZ
| RT (data_reg
)| RA (r0
));
636 tcg_out32 (s
, ADDI
| RT (r1
) | RA (r0
) | 4);
637 tcg_out32 (s
, LWBRX
| RT (data_reg
) | RB (r0
));
638 tcg_out32 (s
, LWBRX
| RT (data_reg2
) | RB (r1
));
641 if (r0
== data_reg2
) {
642 tcg_out32 (s
, LWZ
| RT (0) | RA (r0
));
643 tcg_out32 (s
, LWZ
| RT (data_reg
) | RA (r0
) | 4);
644 tcg_out_mov (s
, data_reg2
, 0);
647 tcg_out32 (s
, LWZ
| RT (data_reg2
) | RA (r0
));
648 tcg_out32 (s
, LWZ
| RT (data_reg
) | RA (r0
) | 4);
654 #ifdef CONFIG_SOFTMMU
655 reloc_pc24 (label2_ptr
, (tcg_target_long
) s
->code_ptr
);
659 static void tcg_out_qemu_st (TCGContext
*s
, const TCGArg
*args
, int opc
)
661 int addr_reg
, r0
, r1
, data_reg
, data_reg2
, mem_index
, bswap
;
662 #ifdef CONFIG_SOFTMMU
664 void *label1_ptr
, *label2_ptr
;
666 #if TARGET_LONG_BITS == 64
676 #if TARGET_LONG_BITS == 64
681 #ifdef CONFIG_SOFTMMU
686 tcg_out32 (s
, (RLWINM
689 | SH (32 - (TARGET_PAGE_BITS
- CPU_TLB_ENTRY_BITS
))
690 | MB (32 - (CPU_TLB_ENTRY_BITS
+ CPU_TLB_BITS
))
691 | ME (31 - CPU_TLB_ENTRY_BITS
)
694 tcg_out32 (s
, ADD
| RT (r0
) | RA (r0
) | RB (TCG_AREG0
));
698 | offsetof (CPUState
, tlb_table
[mem_index
][0].addr_write
)
701 tcg_out32 (s
, (RLWINM
705 | MB ((32 - opc
) & 31)
706 | ME (31 - TARGET_PAGE_BITS
)
710 tcg_out32 (s
, CMP
| (7 << 23) | RA (r2
) | RB (r1
));
711 #if TARGET_LONG_BITS == 64
712 tcg_out32 (s
, LWZ
| RT (r1
) | RA (r0
) | 4);
713 tcg_out32 (s
, CMP
| BF (6) | RA (addr_reg2
) | RB (r1
));
714 tcg_out32 (s
, CRAND
| BT (7, CR_EQ
) | BA (6, CR_EQ
) | BB (7, CR_EQ
));
717 label1_ptr
= s
->code_ptr
;
719 tcg_out32 (s
, BC
| BI (7, CR_EQ
) | BO_COND_TRUE
);
723 #if TARGET_LONG_BITS == 32
724 tcg_out_mov (s
, 3, addr_reg
);
727 tcg_out_mov (s
, 3, addr_reg2
);
728 tcg_out_mov (s
, 4, addr_reg
);
729 #ifdef TCG_TARGET_CALL_ALIGN_ARGS
738 tcg_out32 (s
, (RLWINM
746 tcg_out32 (s
, (RLWINM
754 tcg_out_mov (s
, ir
, data_reg
);
757 #ifdef TCG_TARGET_CALL_ALIGN_ARGS
760 tcg_out_mov (s
, ir
++, data_reg2
);
761 tcg_out_mov (s
, ir
, data_reg
);
766 tcg_out_movi (s
, TCG_TYPE_I32
, ir
, mem_index
);
767 tcg_out_b (s
, LK
, (tcg_target_long
) qemu_st_helpers
[opc
]);
768 label2_ptr
= s
->code_ptr
;
771 /* label1: fast path */
773 reloc_pc14 (label1_ptr
, (tcg_target_long
) s
->code_ptr
);
779 | (ADDEND_OFFSET
+ offsetof (CPUTLBEntry
, addend
)
780 - offsetof (CPUTLBEntry
, addr_write
))
782 /* r0 = env->tlb_table[mem_index][index].addend */
783 tcg_out32 (s
, ADD
| RT (r0
) | RA (r0
) | RB (addr_reg
));
784 /* r0 = env->tlb_table[mem_index][index].addend + addr */
786 #else /* !CONFIG_SOFTMMU */
791 #ifdef TARGET_WORDS_BIGENDIAN
798 tcg_out32 (s
, STB
| RS (data_reg
) | RA (r0
));
801 if (bswap
) tcg_out32 (s
, STHBRX
| RS (data_reg
) | RA (0) | RB (r0
));
802 else tcg_out32 (s
, STH
| RS (data_reg
) | RA (r0
));
805 if (bswap
) tcg_out32 (s
, STWBRX
| RS (data_reg
) | RA (0) | RB (r0
));
806 else tcg_out32 (s
, STW
| RS (data_reg
) | RA (r0
));
810 tcg_out32 (s
, ADDI
| RT (r1
) | RA (r0
) | 4);
811 tcg_out32 (s
, STWBRX
| RS (data_reg
) | RA (0) | RB (r0
));
812 tcg_out32 (s
, STWBRX
| RS (data_reg2
) | RA (0) | RB (r1
));
815 tcg_out32 (s
, STW
| RS (data_reg2
) | RA (r0
));
816 tcg_out32 (s
, STW
| RS (data_reg
) | RA (r0
) | 4);
821 #ifdef CONFIG_SOFTMMU
822 reloc_pc24 (label2_ptr
, (tcg_target_long
) s
->code_ptr
);
826 void tcg_target_qemu_prologue (TCGContext
*s
)
832 + TCG_STATIC_CALL_ARGS_SIZE
833 + ARRAY_SIZE (tcg_target_callee_save_regs
) * 4
835 frame_size
= (frame_size
+ 15) & ~15;
837 tcg_out32 (s
, MFSPR
| RT (0) | LR
);
838 tcg_out32 (s
, STWU
| RS (1) | RA (1) | (-frame_size
& 0xffff));
839 for (i
= 0; i
< ARRAY_SIZE (tcg_target_callee_save_regs
); ++i
)
841 | RS (tcg_target_callee_save_regs
[i
])
843 | (i
* 4 + LINKAGE_AREA_SIZE
+ TCG_STATIC_CALL_ARGS_SIZE
)
846 tcg_out32 (s
, STW
| RS (0) | RA (1) | (frame_size
+ BACK_CHAIN_OFFSET
));
848 tcg_out32 (s
, MTSPR
| RS (3) | CTR
);
849 tcg_out32 (s
, BCCTR
| BO_ALWAYS
);
850 tb_ret_addr
= s
->code_ptr
;
852 for (i
= 0; i
< ARRAY_SIZE (tcg_target_callee_save_regs
); ++i
)
854 | RT (tcg_target_callee_save_regs
[i
])
856 | (i
* 4 + LINKAGE_AREA_SIZE
+ TCG_STATIC_CALL_ARGS_SIZE
)
859 tcg_out32 (s
, LWZ
| RT (0) | RA (1) | (frame_size
+ BACK_CHAIN_OFFSET
));
860 tcg_out32 (s
, MTSPR
| RS (0) | LR
);
861 tcg_out32 (s
, ADDI
| RT (1) | RA (1) | frame_size
);
862 tcg_out32 (s
, BCLR
| BO_ALWAYS
);
865 static void tcg_out_ld (TCGContext
*s
, TCGType type
, int ret
, int arg1
,
866 tcg_target_long arg2
)
868 tcg_out_ldst (s
, ret
, arg1
, arg2
, LWZ
, LWZX
);
871 static void tcg_out_st (TCGContext
*s
, TCGType type
, int arg
, int arg1
,
872 tcg_target_long arg2
)
874 tcg_out_ldst (s
, arg
, arg1
, arg2
, STW
, STWX
);
877 static void ppc_addi (TCGContext
*s
, int rt
, int ra
, tcg_target_long si
)
882 if (si
== (int16_t) si
)
883 tcg_out32 (s
, ADDI
| RT (rt
) | RA (ra
) | (si
& 0xffff));
885 uint16_t h
= ((si
>> 16) & 0xffff) + ((uint16_t) si
>> 15);
886 tcg_out32 (s
, ADDIS
| RT (rt
) | RA (ra
) | h
);
887 tcg_out32 (s
, ADDI
| RT (rt
) | RA (rt
) | (si
& 0xffff));
891 static void tcg_out_addi(TCGContext
*s
, int reg
, tcg_target_long val
)
893 ppc_addi (s
, reg
, reg
, val
);
896 static void tcg_out_cmp (TCGContext
*s
, int cond
, TCGArg arg1
, TCGArg arg2
,
897 int const_arg2
, int cr
)
906 if ((int16_t) arg2
== arg2
) {
911 else if ((uint16_t) arg2
== arg2
) {
926 if ((int16_t) arg2
== arg2
) {
941 if ((uint16_t) arg2
== arg2
) {
957 tcg_out32 (s
, op
| RA (arg1
) | (arg2
& 0xffff));
960 tcg_out_movi (s
, TCG_TYPE_I32
, 0, arg2
);
961 tcg_out32 (s
, op
| RA (arg1
) | RB (0));
964 tcg_out32 (s
, op
| RA (arg1
) | RB (arg2
));
969 static void tcg_out_bc (TCGContext
*s
, int bc
, int label_index
)
971 TCGLabel
*l
= &s
->labels
[label_index
];
974 tcg_out32 (s
, bc
| reloc_pc14_val (s
->code_ptr
, l
->u
.value
));
976 uint16_t val
= *(uint16_t *) &s
->code_ptr
[2];
978 /* Thanks to Andrzej Zaborowski */
979 tcg_out32 (s
, bc
| (val
& 0xfffc));
980 tcg_out_reloc (s
, s
->code_ptr
- 4, R_PPC_REL14
, label_index
, 0);
984 static void tcg_out_brcond (TCGContext
*s
, int cond
,
985 TCGArg arg1
, TCGArg arg2
, int const_arg2
,
988 tcg_out_cmp (s
, cond
, arg1
, arg2
, const_arg2
, 7);
989 tcg_out_bc (s
, tcg_to_bc
[cond
], label_index
);
992 /* XXX: we implement it at the target level to avoid having to
993 handle cross basic blocks temporaries */
994 static void tcg_out_brcond2 (TCGContext
*s
, const TCGArg
*args
,
995 const int *const_args
)
997 int cond
= args
[4], label_index
= args
[5], op
;
998 struct { int bit1
; int bit2
; int cond2
; } bits
[] = {
999 [TCG_COND_LT
] = { CR_LT
, CR_LT
, TCG_COND_LT
},
1000 [TCG_COND_LE
] = { CR_LT
, CR_GT
, TCG_COND_LT
},
1001 [TCG_COND_GT
] = { CR_GT
, CR_GT
, TCG_COND_GT
},
1002 [TCG_COND_GE
] = { CR_GT
, CR_LT
, TCG_COND_GT
},
1003 [TCG_COND_LTU
] = { CR_LT
, CR_LT
, TCG_COND_LTU
},
1004 [TCG_COND_LEU
] = { CR_LT
, CR_GT
, TCG_COND_LTU
},
1005 [TCG_COND_GTU
] = { CR_GT
, CR_GT
, TCG_COND_GTU
},
1006 [TCG_COND_GEU
] = { CR_GT
, CR_LT
, TCG_COND_GTU
},
1007 }, *b
= &bits
[cond
];
1012 op
= (cond
== TCG_COND_EQ
) ? CRAND
: CRNAND
;
1013 tcg_out_cmp (s
, cond
, args
[0], args
[2], const_args
[2], 6);
1014 tcg_out_cmp (s
, cond
, args
[1], args
[3], const_args
[3], 7);
1015 tcg_out32 (s
, op
| BT (7, CR_EQ
) | BA (6, CR_EQ
) | BB (7, CR_EQ
));
1025 op
= (b
->bit1
!= b
->bit2
) ? CRANDC
: CRAND
;
1026 tcg_out_cmp (s
, b
->cond2
, args
[1], args
[3], const_args
[3], 5);
1027 tcg_out_cmp (s
, TCG_COND_EQ
, args
[1], args
[3], const_args
[3], 6);
1028 tcg_out_cmp (s
, cond
, args
[0], args
[2], const_args
[2], 7);
1029 tcg_out32 (s
, op
| BT (7, CR_EQ
) | BA (6, CR_EQ
) | BB (7, b
->bit2
));
1030 tcg_out32 (s
, CROR
| BT (7, CR_EQ
) | BA (5, b
->bit1
) | BB (7, CR_EQ
));
1036 tcg_out_bc (s
, (BC
| BI (7, CR_EQ
) | BO_COND_TRUE
), label_index
);
1039 void ppc_tb_set_jmp_target (unsigned long jmp_addr
, unsigned long addr
)
1042 long disp
= addr
- jmp_addr
;
1043 unsigned long patch_size
;
1045 ptr
= (uint32_t *)jmp_addr
;
1047 if ((disp
<< 6) >> 6 != disp
) {
1048 ptr
[0] = 0x3c000000 | (addr
>> 16); /* lis 0,addr@ha */
1049 ptr
[1] = 0x60000000 | (addr
& 0xffff); /* la 0,addr@l(0) */
1050 ptr
[2] = 0x7c0903a6; /* mtctr 0 */
1051 ptr
[3] = 0x4e800420; /* brctr */
1054 /* patch the branch destination */
1056 *ptr
= 0x48000000 | (disp
& 0x03fffffc); /* b disp */
1059 ptr
[0] = 0x60000000; /* nop */
1060 ptr
[1] = 0x60000000;
1061 ptr
[2] = 0x60000000;
1062 ptr
[3] = 0x60000000;
1067 flush_icache_range(jmp_addr
, jmp_addr
+ patch_size
);
1070 static void tcg_out_op(TCGContext
*s
, int opc
, const TCGArg
*args
,
1071 const int *const_args
)
1074 case INDEX_op_exit_tb
:
1075 tcg_out_movi (s
, TCG_TYPE_I32
, TCG_REG_R3
, args
[0]);
1076 tcg_out_b (s
, 0, (tcg_target_long
) tb_ret_addr
);
1078 case INDEX_op_goto_tb
:
1079 if (s
->tb_jmp_offset
) {
1080 /* direct jump method */
1082 s
->tb_jmp_offset
[args
[0]] = s
->code_ptr
- s
->code_buf
;
1088 s
->tb_next_offset
[args
[0]] = s
->code_ptr
- s
->code_buf
;
1092 TCGLabel
*l
= &s
->labels
[args
[0]];
1095 tcg_out_b (s
, 0, l
->u
.value
);
1098 uint32_t val
= *(uint32_t *) s
->code_ptr
;
1100 /* Thanks to Andrzej Zaborowski */
1101 tcg_out32 (s
, B
| (val
& 0x3fffffc));
1102 tcg_out_reloc (s
, s
->code_ptr
- 4, R_PPC_REL24
, args
[0], 0);
1107 if (const_args
[0]) {
1108 tcg_out_b (s
, LK
, args
[0]);
1111 tcg_out32 (s
, MTSPR
| RS (args
[0]) | LR
);
1112 tcg_out32 (s
, BCLR
| BO_ALWAYS
| LK
);
1116 if (const_args
[0]) {
1117 tcg_out_b (s
, 0, args
[0]);
1120 tcg_out32 (s
, MTSPR
| RS (args
[0]) | CTR
);
1121 tcg_out32 (s
, BCCTR
| BO_ALWAYS
);
1124 case INDEX_op_movi_i32
:
1125 tcg_out_movi(s
, TCG_TYPE_I32
, args
[0], args
[1]);
1127 case INDEX_op_ld8u_i32
:
1128 tcg_out_ldst (s
, args
[0], args
[1], args
[2], LBZ
, LBZX
);
1130 case INDEX_op_ld8s_i32
:
1131 tcg_out_ldst (s
, args
[0], args
[1], args
[2], LBZ
, LBZX
);
1132 tcg_out32 (s
, EXTSB
| RS (args
[0]) | RA (args
[0]));
1134 case INDEX_op_ld16u_i32
:
1135 tcg_out_ldst (s
, args
[0], args
[1], args
[2], LHZ
, LHZX
);
1137 case INDEX_op_ld16s_i32
:
1138 tcg_out_ldst (s
, args
[0], args
[1], args
[2], LHA
, LHAX
);
1140 case INDEX_op_ld_i32
:
1141 tcg_out_ldst (s
, args
[0], args
[1], args
[2], LWZ
, LWZX
);
1143 case INDEX_op_st8_i32
:
1144 tcg_out_ldst (s
, args
[0], args
[1], args
[2], STB
, STBX
);
1146 case INDEX_op_st16_i32
:
1147 tcg_out_ldst (s
, args
[0], args
[1], args
[2], STH
, STHX
);
1149 case INDEX_op_st_i32
:
1150 tcg_out_ldst (s
, args
[0], args
[1], args
[2], STW
, STWX
);
1153 case INDEX_op_add_i32
:
1155 ppc_addi (s
, args
[0], args
[1], args
[2]);
1157 tcg_out32 (s
, ADD
| TAB (args
[0], args
[1], args
[2]));
1159 case INDEX_op_sub_i32
:
1161 ppc_addi (s
, args
[0], args
[1], -args
[2]);
1163 tcg_out32 (s
, SUBF
| TAB (args
[0], args
[2], args
[1]));
1166 case INDEX_op_and_i32
:
1167 if (const_args
[2]) {
1168 if ((args
[2] & 0xffff) == args
[2])
1169 tcg_out32 (s
, ANDI
| RS (args
[1]) | RA (args
[0]) | args
[2]);
1170 else if ((args
[2] & 0xffff0000) == args
[2])
1171 tcg_out32 (s
, ANDIS
| RS (args
[1]) | RA (args
[0])
1172 | ((args
[2] >> 16) & 0xffff));
1174 tcg_out_movi (s
, TCG_TYPE_I32
, 0, args
[2]);
1175 tcg_out32 (s
, AND
| SAB (args
[1], args
[0], 0));
1179 tcg_out32 (s
, AND
| SAB (args
[1], args
[0], args
[2]));
1181 case INDEX_op_or_i32
:
1182 if (const_args
[2]) {
1183 if (args
[2] & 0xffff) {
1184 tcg_out32 (s
, ORI
| RS (args
[1]) | RA (args
[0])
1185 | (args
[2] & 0xffff));
1187 tcg_out32 (s
, ORIS
| RS (args
[0]) | RA (args
[0])
1188 | ((args
[2] >> 16) & 0xffff));
1191 tcg_out32 (s
, ORIS
| RS (args
[1]) | RA (args
[0])
1192 | ((args
[2] >> 16) & 0xffff));
1196 tcg_out32 (s
, OR
| SAB (args
[1], args
[0], args
[2]));
1198 case INDEX_op_xor_i32
:
1199 if (const_args
[2]) {
1200 if ((args
[2] & 0xffff) == args
[2])
1201 tcg_out32 (s
, XORI
| RS (args
[1]) | RA (args
[0])
1202 | (args
[2] & 0xffff));
1203 else if ((args
[2] & 0xffff0000) == args
[2])
1204 tcg_out32 (s
, XORIS
| RS (args
[1]) | RA (args
[0])
1205 | ((args
[2] >> 16) & 0xffff));
1207 tcg_out_movi (s
, TCG_TYPE_I32
, 0, args
[2]);
1208 tcg_out32 (s
, XOR
| SAB (args
[1], args
[0], 0));
1212 tcg_out32 (s
, XOR
| SAB (args
[1], args
[0], args
[2]));
1215 case INDEX_op_mul_i32
:
1216 if (const_args
[2]) {
1217 if (args
[2] == (int16_t) args
[2])
1218 tcg_out32 (s
, MULLI
| RT (args
[0]) | RA (args
[1])
1219 | (args
[2] & 0xffff));
1221 tcg_out_movi (s
, TCG_TYPE_I32
, 0, args
[2]);
1222 tcg_out32 (s
, MULLW
| TAB (args
[0], args
[1], 0));
1226 tcg_out32 (s
, MULLW
| TAB (args
[0], args
[1], args
[2]));
1229 case INDEX_op_div_i32
:
1230 tcg_out32 (s
, DIVW
| TAB (args
[0], args
[1], args
[2]));
1233 case INDEX_op_divu_i32
:
1234 tcg_out32 (s
, DIVWU
| TAB (args
[0], args
[1], args
[2]));
1237 case INDEX_op_rem_i32
:
1238 tcg_out32 (s
, DIVW
| TAB (0, args
[1], args
[2]));
1239 tcg_out32 (s
, MULLW
| TAB (0, 0, args
[2]));
1240 tcg_out32 (s
, SUBF
| TAB (args
[0], 0, args
[1]));
1243 case INDEX_op_remu_i32
:
1244 tcg_out32 (s
, DIVWU
| TAB (0, args
[1], args
[2]));
1245 tcg_out32 (s
, MULLW
| TAB (0, 0, args
[2]));
1246 tcg_out32 (s
, SUBF
| TAB (args
[0], 0, args
[1]));
1249 case INDEX_op_mulu2_i32
:
1250 if (args
[0] == args
[2] || args
[0] == args
[3]) {
1251 tcg_out32 (s
, MULLW
| TAB (0, args
[2], args
[3]));
1252 tcg_out32 (s
, MULHWU
| TAB (args
[1], args
[2], args
[3]));
1253 tcg_out_mov (s
, args
[0], 0);
1256 tcg_out32 (s
, MULLW
| TAB (args
[0], args
[2], args
[3]));
1257 tcg_out32 (s
, MULHWU
| TAB (args
[1], args
[2], args
[3]));
1261 case INDEX_op_shl_i32
:
1262 if (const_args
[2]) {
1263 tcg_out32 (s
, (RLWINM
1273 tcg_out32 (s
, SLW
| SAB (args
[1], args
[0], args
[2]));
1275 case INDEX_op_shr_i32
:
1276 if (const_args
[2]) {
1277 tcg_out32 (s
, (RLWINM
1287 tcg_out32 (s
, SRW
| SAB (args
[1], args
[0], args
[2]));
1289 case INDEX_op_sar_i32
:
1291 tcg_out32 (s
, SRAWI
| RS (args
[1]) | RA (args
[0]) | SH (args
[2]));
1293 tcg_out32 (s
, SRAW
| SAB (args
[1], args
[0], args
[2]));
1296 case INDEX_op_add2_i32
:
1297 if (args
[0] == args
[3] || args
[0] == args
[5]) {
1298 tcg_out32 (s
, ADDC
| TAB (0, args
[2], args
[4]));
1299 tcg_out32 (s
, ADDE
| TAB (args
[1], args
[3], args
[5]));
1300 tcg_out_mov (s
, args
[0], 0);
1303 tcg_out32 (s
, ADDC
| TAB (args
[0], args
[2], args
[4]));
1304 tcg_out32 (s
, ADDE
| TAB (args
[1], args
[3], args
[5]));
1307 case INDEX_op_sub2_i32
:
1308 if (args
[0] == args
[3] || args
[0] == args
[5]) {
1309 tcg_out32 (s
, SUBFC
| TAB (0, args
[4], args
[2]));
1310 tcg_out32 (s
, SUBFE
| TAB (args
[1], args
[5], args
[3]));
1311 tcg_out_mov (s
, args
[0], 0);
1314 tcg_out32 (s
, SUBFC
| TAB (args
[0], args
[4], args
[2]));
1315 tcg_out32 (s
, SUBFE
| TAB (args
[1], args
[5], args
[3]));
1319 case INDEX_op_brcond_i32
:
1324 args[3] = r1 is const
1325 args[4] = label_index
1327 tcg_out_brcond (s
, args
[2], args
[0], args
[1], const_args
[1], args
[3]);
1329 case INDEX_op_brcond2_i32
:
1330 tcg_out_brcond2(s
, args
, const_args
);
1333 case INDEX_op_neg_i32
:
1334 tcg_out32 (s
, NEG
| RT (args
[0]) | RA (args
[1]));
1337 case INDEX_op_qemu_ld8u
:
1338 tcg_out_qemu_ld(s
, args
, 0);
1340 case INDEX_op_qemu_ld8s
:
1341 tcg_out_qemu_ld(s
, args
, 0 | 4);
1343 case INDEX_op_qemu_ld16u
:
1344 tcg_out_qemu_ld(s
, args
, 1);
1346 case INDEX_op_qemu_ld16s
:
1347 tcg_out_qemu_ld(s
, args
, 1 | 4);
1349 case INDEX_op_qemu_ld32u
:
1350 tcg_out_qemu_ld(s
, args
, 2);
1352 case INDEX_op_qemu_ld64
:
1353 tcg_out_qemu_ld(s
, args
, 3);
1355 case INDEX_op_qemu_st8
:
1356 tcg_out_qemu_st(s
, args
, 0);
1358 case INDEX_op_qemu_st16
:
1359 tcg_out_qemu_st(s
, args
, 1);
1361 case INDEX_op_qemu_st32
:
1362 tcg_out_qemu_st(s
, args
, 2);
1364 case INDEX_op_qemu_st64
:
1365 tcg_out_qemu_st(s
, args
, 3);
1368 case INDEX_op_ext8s_i32
:
1369 tcg_out32 (s
, EXTSB
| RS (args
[1]) | RA (args
[0]));
1371 case INDEX_op_ext16s_i32
:
1372 tcg_out32 (s
, EXTSH
| RS (args
[1]) | RA (args
[0]));
1376 tcg_dump_ops (s
, stderr
);
1381 static const TCGTargetOpDef ppc_op_defs
[] = {
1382 { INDEX_op_exit_tb
, { } },
1383 { INDEX_op_goto_tb
, { } },
1384 { INDEX_op_call
, { "ri" } },
1385 { INDEX_op_jmp
, { "ri" } },
1386 { INDEX_op_br
, { } },
1388 { INDEX_op_mov_i32
, { "r", "r" } },
1389 { INDEX_op_movi_i32
, { "r" } },
1390 { INDEX_op_ld8u_i32
, { "r", "r" } },
1391 { INDEX_op_ld8s_i32
, { "r", "r" } },
1392 { INDEX_op_ld16u_i32
, { "r", "r" } },
1393 { INDEX_op_ld16s_i32
, { "r", "r" } },
1394 { INDEX_op_ld_i32
, { "r", "r" } },
1395 { INDEX_op_st8_i32
, { "r", "r" } },
1396 { INDEX_op_st16_i32
, { "r", "r" } },
1397 { INDEX_op_st_i32
, { "r", "r" } },
1399 { INDEX_op_add_i32
, { "r", "r", "ri" } },
1400 { INDEX_op_mul_i32
, { "r", "r", "ri" } },
1401 { INDEX_op_div_i32
, { "r", "r", "r" } },
1402 { INDEX_op_divu_i32
, { "r", "r", "r" } },
1403 { INDEX_op_rem_i32
, { "r", "r", "r" } },
1404 { INDEX_op_remu_i32
, { "r", "r", "r" } },
1405 { INDEX_op_mulu2_i32
, { "r", "r", "r", "r" } },
1406 { INDEX_op_sub_i32
, { "r", "r", "ri" } },
1407 { INDEX_op_and_i32
, { "r", "r", "ri" } },
1408 { INDEX_op_or_i32
, { "r", "r", "ri" } },
1409 { INDEX_op_xor_i32
, { "r", "r", "ri" } },
1411 { INDEX_op_shl_i32
, { "r", "r", "ri" } },
1412 { INDEX_op_shr_i32
, { "r", "r", "ri" } },
1413 { INDEX_op_sar_i32
, { "r", "r", "ri" } },
1415 { INDEX_op_brcond_i32
, { "r", "ri" } },
1417 { INDEX_op_add2_i32
, { "r", "r", "r", "r", "r", "r" } },
1418 { INDEX_op_sub2_i32
, { "r", "r", "r", "r", "r", "r" } },
1419 { INDEX_op_brcond2_i32
, { "r", "r", "r", "r" } },
1421 { INDEX_op_neg_i32
, { "r", "r" } },
1423 #if TARGET_LONG_BITS == 32
1424 { INDEX_op_qemu_ld8u
, { "r", "L" } },
1425 { INDEX_op_qemu_ld8s
, { "r", "L" } },
1426 { INDEX_op_qemu_ld16u
, { "r", "L" } },
1427 { INDEX_op_qemu_ld16s
, { "r", "L" } },
1428 { INDEX_op_qemu_ld32u
, { "r", "L" } },
1429 { INDEX_op_qemu_ld32s
, { "r", "L" } },
1430 { INDEX_op_qemu_ld64
, { "r", "r", "L" } },
1432 { INDEX_op_qemu_st8
, { "K", "K" } },
1433 { INDEX_op_qemu_st16
, { "K", "K" } },
1434 { INDEX_op_qemu_st32
, { "K", "K" } },
1435 { INDEX_op_qemu_st64
, { "M", "M", "M" } },
1437 { INDEX_op_qemu_ld8u
, { "r", "L", "L" } },
1438 { INDEX_op_qemu_ld8s
, { "r", "L", "L" } },
1439 { INDEX_op_qemu_ld16u
, { "r", "L", "L" } },
1440 { INDEX_op_qemu_ld16s
, { "r", "L", "L" } },
1441 { INDEX_op_qemu_ld32u
, { "r", "L", "L" } },
1442 { INDEX_op_qemu_ld32s
, { "r", "L", "L" } },
1443 { INDEX_op_qemu_ld64
, { "r", "L", "L", "L" } },
1445 { INDEX_op_qemu_st8
, { "K", "K", "K" } },
1446 { INDEX_op_qemu_st16
, { "K", "K", "K" } },
1447 { INDEX_op_qemu_st32
, { "K", "K", "K" } },
1448 { INDEX_op_qemu_st64
, { "M", "M", "M", "M" } },
1451 { INDEX_op_ext8s_i32
, { "r", "r" } },
1452 { INDEX_op_ext16s_i32
, { "r", "r" } },
1457 void tcg_target_init(TCGContext
*s
)
1459 tcg_regset_set32(tcg_target_available_regs
[TCG_TYPE_I32
], 0, 0xffffffff);
1460 tcg_regset_set32(tcg_target_call_clobber_regs
, 0,
1472 (1 << TCG_REG_R10
) |
1473 (1 << TCG_REG_R11
) |
1477 tcg_regset_clear(s
->reserved_regs
);
1478 tcg_regset_set_reg(s
->reserved_regs
, TCG_REG_R0
);
1479 tcg_regset_set_reg(s
->reserved_regs
, TCG_REG_R1
);
1481 tcg_regset_set_reg(s
->reserved_regs
, TCG_REG_R2
);
1484 tcg_add_target_add_op_defs(ppc_op_defs
);