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
31 #define LINKAGE_AREA_SIZE 52
34 #define LINKAGE_AREA_SIZE 8
39 #if TARGET_PHYS_ADDR_BITS <= 32
40 #define ADDEND_OFFSET 0
42 #define ADDEND_OFFSET 4
46 static const char * const tcg_target_reg_names
[TCG_TARGET_NB_REGS
] = {
82 static const int tcg_target_reg_alloc_order
[] = {
122 static const int tcg_target_call_iarg_regs
[] = {
133 static const int tcg_target_call_oarg_regs
[2] = {
138 static const int tcg_target_callee_save_regs
[] = {
162 static uint32_t reloc_pc24_val (void *pc
, tcg_target_long target
)
164 tcg_target_long disp
;
166 disp
= target
- (tcg_target_long
) pc
;
167 if ((disp
<< 6) >> 6 != disp
)
170 return disp
& 0x3fffffc;
173 static void reloc_pc24 (void *pc
, tcg_target_long target
)
175 *(uint32_t *) pc
= (*(uint32_t *) pc
& ~0x3fffffc)
176 | reloc_pc24_val (pc
, target
);
179 static uint16_t reloc_pc14_val (void *pc
, tcg_target_long target
)
181 tcg_target_long disp
;
183 disp
= target
- (tcg_target_long
) pc
;
184 if (disp
!= (int16_t) disp
)
187 return disp
& 0xfffc;
190 static void reloc_pc14 (void *pc
, tcg_target_long target
)
192 *(uint32_t *) pc
= (*(uint32_t *) pc
& ~0xfffc)
193 | reloc_pc14_val (pc
, target
);
196 static void patch_reloc(uint8_t *code_ptr
, int type
,
197 tcg_target_long value
, tcg_target_long addend
)
202 reloc_pc14 (code_ptr
, value
);
205 reloc_pc24 (code_ptr
, value
);
212 /* maximum number of register used for input function arguments */
213 static int tcg_target_get_call_iarg_regs_count(int flags
)
215 return ARRAY_SIZE (tcg_target_call_iarg_regs
);
218 /* parse target specific constraints */
219 static int target_parse_constraint(TCGArgConstraint
*ct
, const char **pct_str
)
225 case 'A': case 'B': case 'C': case 'D':
226 ct
->ct
|= TCG_CT_REG
;
227 tcg_regset_set_reg(ct
->u
.regs
, 3 + ct_str
[0] - 'A');
230 ct
->ct
|= TCG_CT_REG
;
231 tcg_regset_set32(ct
->u
.regs
, 0, 0xffffffff);
233 #ifdef CONFIG_SOFTMMU
234 case 'L': /* qemu_ld constraint */
235 ct
->ct
|= TCG_CT_REG
;
236 tcg_regset_set32(ct
->u
.regs
, 0, 0xffffffff);
237 tcg_regset_reset_reg(ct
->u
.regs
, TCG_REG_R3
);
238 tcg_regset_reset_reg(ct
->u
.regs
, TCG_REG_R4
);
240 case 'K': /* qemu_st[8..32] constraint */
241 ct
->ct
|= TCG_CT_REG
;
242 tcg_regset_set32(ct
->u
.regs
, 0, 0xffffffff);
243 tcg_regset_reset_reg(ct
->u
.regs
, TCG_REG_R3
);
244 tcg_regset_reset_reg(ct
->u
.regs
, TCG_REG_R4
);
245 tcg_regset_reset_reg(ct
->u
.regs
, TCG_REG_R5
);
246 #if TARGET_LONG_BITS == 64
247 tcg_regset_reset_reg(ct
->u
.regs
, TCG_REG_R6
);
250 case 'M': /* qemu_st64 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 tcg_regset_reset_reg(ct
->u
.regs
, TCG_REG_R7
);
262 ct
->ct
|= TCG_CT_REG
;
263 tcg_regset_set32(ct
->u
.regs
, 0, 0xffffffff);
266 ct
->ct
|= TCG_CT_REG
;
267 tcg_regset_set32(ct
->u
.regs
, 0, 0xffffffff);
268 tcg_regset_reset_reg(ct
->u
.regs
, TCG_REG_R3
);
279 /* test if a constant matches the constraint */
280 static int tcg_target_const_match(tcg_target_long val
,
281 const TCGArgConstraint
*arg_ct
)
286 if (ct
& TCG_CT_CONST
)
291 #define OPCD(opc) ((opc)<<26)
292 #define XO31(opc) (OPCD(31)|((opc)<<1))
293 #define XO19(opc) (OPCD(19)|((opc)<<1))
305 #define ADDI OPCD(14)
306 #define ADDIS OPCD(15)
308 #define ORIS OPCD(25)
309 #define XORI OPCD(26)
310 #define XORIS OPCD(27)
311 #define ANDI OPCD(28)
312 #define ANDIS OPCD(29)
313 #define MULLI OPCD( 7)
314 #define CMPLI OPCD(10)
315 #define CMPI OPCD(11)
317 #define LWZU OPCD(33)
318 #define STWU OPCD(37)
320 #define RLWINM OPCD(21)
322 #define BCLR XO19( 16)
323 #define BCCTR XO19(528)
324 #define CRAND XO19(257)
325 #define CRANDC XO19(129)
326 #define CRNAND XO19(225)
327 #define CROR XO19(449)
329 #define EXTSB XO31(954)
330 #define EXTSH XO31(922)
331 #define ADD XO31(266)
332 #define ADDE XO31(138)
333 #define ADDC XO31( 10)
334 #define AND XO31( 28)
335 #define SUBF XO31( 40)
336 #define SUBFC XO31( 8)
337 #define SUBFE XO31(136)
339 #define XOR XO31(316)
340 #define MULLW XO31(235)
341 #define MULHWU XO31( 11)
342 #define DIVW XO31(491)
343 #define DIVWU XO31(459)
345 #define CMPL XO31( 32)
346 #define LHBRX XO31(790)
347 #define LWBRX XO31(534)
348 #define STHBRX XO31(918)
349 #define STWBRX XO31(662)
350 #define MFSPR XO31(339)
351 #define MTSPR XO31(467)
352 #define SRAWI XO31(824)
353 #define NEG XO31(104)
355 #define LBZX XO31( 87)
356 #define LHZX XO31(276)
357 #define LHAX XO31(343)
358 #define LWZX XO31( 23)
359 #define STBX XO31(215)
360 #define STHX XO31(407)
361 #define STWX XO31(151)
363 #define SPR(a,b) ((((a)<<5)|(b))<<11)
365 #define CTR SPR(9, 0)
367 #define SLW XO31( 24)
368 #define SRW XO31(536)
369 #define SRAW XO31(792)
372 #define STMW OPCD(47)
375 #define TRAP (TW | TO (31))
377 #define RT(r) ((r)<<21)
378 #define RS(r) ((r)<<21)
379 #define RA(r) ((r)<<16)
380 #define RB(r) ((r)<<11)
381 #define TO(t) ((t)<<21)
382 #define SH(s) ((s)<<11)
383 #define MB(b) ((b)<<6)
384 #define ME(e) ((e)<<1)
385 #define BO(o) ((o)<<21)
389 #define TAB(t,a,b) (RT(t) | RA(a) | RB(b))
390 #define SAB(s,a,b) (RS(s) | RA(a) | RB(b))
392 #define BF(n) ((n)<<23)
393 #define BI(n, c) (((c)+((n)*4))<<16)
394 #define BT(n, c) (((c)+((n)*4))<<21)
395 #define BA(n, c) (((c)+((n)*4))<<16)
396 #define BB(n, c) (((c)+((n)*4))<<11)
398 #define BO_COND_TRUE BO (12)
399 #define BO_COND_FALSE BO (4)
400 #define BO_ALWAYS BO (20)
409 static const uint32_t tcg_to_bc
[10] = {
410 [TCG_COND_EQ
] = BC
| BI (7, CR_EQ
) | BO_COND_TRUE
,
411 [TCG_COND_NE
] = BC
| BI (7, CR_EQ
) | BO_COND_FALSE
,
412 [TCG_COND_LT
] = BC
| BI (7, CR_LT
) | BO_COND_TRUE
,
413 [TCG_COND_GE
] = BC
| BI (7, CR_LT
) | BO_COND_FALSE
,
414 [TCG_COND_LE
] = BC
| BI (7, CR_GT
) | BO_COND_FALSE
,
415 [TCG_COND_GT
] = BC
| BI (7, CR_GT
) | BO_COND_TRUE
,
416 [TCG_COND_LTU
] = BC
| BI (7, CR_LT
) | BO_COND_TRUE
,
417 [TCG_COND_GEU
] = BC
| BI (7, CR_LT
) | BO_COND_FALSE
,
418 [TCG_COND_LEU
] = BC
| BI (7, CR_GT
) | BO_COND_FALSE
,
419 [TCG_COND_GTU
] = BC
| BI (7, CR_GT
) | BO_COND_TRUE
,
422 static void tcg_out_mov(TCGContext
*s
, int ret
, int arg
)
424 tcg_out32 (s
, OR
| SAB (arg
, ret
, arg
));
427 static void tcg_out_movi(TCGContext
*s
, TCGType type
,
428 int ret
, tcg_target_long arg
)
430 if (arg
== (int16_t) arg
)
431 tcg_out32 (s
, ADDI
| RT (ret
) | RA (0) | (arg
& 0xffff));
433 tcg_out32 (s
, ADDIS
| RT (ret
) | RA (0) | ((arg
>> 16) & 0xffff));
435 tcg_out32 (s
, ORI
| RS (ret
) | RA (ret
) | (arg
& 0xffff));
439 static void tcg_out_ldst (TCGContext
*s
, int ret
, int addr
,
440 int offset
, int op1
, int op2
)
442 if (offset
== (int16_t) offset
)
443 tcg_out32 (s
, op1
| RT (ret
) | RA (addr
) | (offset
& 0xffff));
445 tcg_out_movi (s
, TCG_TYPE_I32
, 0, offset
);
446 tcg_out32 (s
, op2
| RT (ret
) | RA (addr
) | RB (0));
450 static void tcg_out_b (TCGContext
*s
, int mask
, tcg_target_long target
)
452 tcg_target_long disp
;
454 disp
= target
- (tcg_target_long
) s
->code_ptr
;
455 if ((disp
<< 6) >> 6 == disp
)
456 tcg_out32 (s
, B
| (disp
& 0x3fffffc) | mask
);
458 tcg_out_movi (s
, TCG_TYPE_I32
, 0, (tcg_target_long
) target
);
459 tcg_out32 (s
, MTSPR
| RS (0) | CTR
);
460 tcg_out32 (s
, BCCTR
| BO_ALWAYS
| mask
);
465 static void tcg_out_call (TCGContext
*s
, tcg_target_long arg
, int const_arg
)
471 tcg_out_movi (s
, TCG_TYPE_I32
, reg
, arg
);
475 tcg_out32 (s
, LWZ
| RT (0) | RA (reg
));
476 tcg_out32 (s
, MTSPR
| RA (0) | CTR
);
477 tcg_out32 (s
, LWZ
| RT (2) | RA (reg
) | 4);
478 tcg_out32 (s
, BCCTR
| BO_ALWAYS
| LK
);
482 #if defined(CONFIG_SOFTMMU)
484 #include "../../softmmu_defs.h"
486 static void *qemu_ld_helpers
[4] = {
493 static void *qemu_st_helpers
[4] = {
501 static void tcg_out_qemu_ld (TCGContext
*s
, const TCGArg
*args
, int opc
)
503 int addr_reg
, data_reg
, data_reg2
, r0
, r1
, mem_index
, s_bits
, bswap
;
504 #ifdef CONFIG_SOFTMMU
506 void *label1_ptr
, *label2_ptr
;
508 #if TARGET_LONG_BITS == 64
518 #if TARGET_LONG_BITS == 64
524 #ifdef CONFIG_SOFTMMU
529 tcg_out32 (s
, (RLWINM
532 | SH (32 - (TARGET_PAGE_BITS
- CPU_TLB_ENTRY_BITS
))
533 | MB (32 - (CPU_TLB_BITS
+ CPU_TLB_ENTRY_BITS
))
534 | ME (31 - CPU_TLB_ENTRY_BITS
)
537 tcg_out32 (s
, ADD
| RT (r0
) | RA (r0
) | RB (TCG_AREG0
));
541 | offsetof (CPUState
, tlb_table
[mem_index
][0].addr_read
)
544 tcg_out32 (s
, (RLWINM
548 | MB ((32 - s_bits
) & 31)
549 | ME (31 - TARGET_PAGE_BITS
)
553 tcg_out32 (s
, CMP
| BF (7) | RA (r2
) | RB (r1
));
554 #if TARGET_LONG_BITS == 64
555 tcg_out32 (s
, LWZ
| RT (r1
) | RA (r0
) | 4);
556 tcg_out32 (s
, CMP
| BF (6) | RA (addr_reg2
) | RB (r1
));
557 tcg_out32 (s
, CRAND
| BT (7, CR_EQ
) | BA (6, CR_EQ
) | BB (7, CR_EQ
));
560 label1_ptr
= s
->code_ptr
;
562 tcg_out32 (s
, BC
| BI (7, CR_EQ
) | BO_COND_TRUE
);
566 #if TARGET_LONG_BITS == 32
567 tcg_out_mov (s
, 3, addr_reg
);
568 tcg_out_movi (s
, TCG_TYPE_I32
, 4, mem_index
);
570 tcg_out_mov (s
, 3, addr_reg2
);
571 tcg_out_mov (s
, 4, addr_reg
);
572 tcg_out_movi (s
, TCG_TYPE_I32
, 5, mem_index
);
576 tcg_out_call (s
, (tcg_target_long
) qemu_ld_helpers
[s_bits
], 1);
578 tcg_out_b (s
, LK
, (tcg_target_long
) qemu_ld_helpers
[s_bits
]);
582 tcg_out32 (s
, EXTSB
| RA (data_reg
) | RS (3));
585 tcg_out32 (s
, EXTSH
| RA (data_reg
) | RS (3));
591 tcg_out_mov (s
, data_reg
, 3);
595 if (data_reg2
== 4) {
596 tcg_out_mov (s
, 0, 4);
597 tcg_out_mov (s
, 4, 3);
598 tcg_out_mov (s
, 3, 0);
601 tcg_out_mov (s
, data_reg2
, 3);
602 tcg_out_mov (s
, 3, 4);
606 if (data_reg
!= 4) tcg_out_mov (s
, data_reg
, 4);
607 if (data_reg2
!= 3) tcg_out_mov (s
, data_reg2
, 3);
611 label2_ptr
= s
->code_ptr
;
614 /* label1: fast path */
616 reloc_pc14 (label1_ptr
, (tcg_target_long
) s
->code_ptr
);
619 /* r0 now contains &env->tlb_table[mem_index][index].addr_read */
623 | (ADDEND_OFFSET
+ offsetof (CPUTLBEntry
, addend
)
624 - offsetof (CPUTLBEntry
, addr_read
))
626 /* r0 = env->tlb_table[mem_index][index].addend */
627 tcg_out32 (s
, ADD
| RT (r0
) | RA (r0
) | RB (addr_reg
));
628 /* r0 = env->tlb_table[mem_index][index].addend + addr */
630 #else /* !CONFIG_SOFTMMU */
635 #ifdef TARGET_WORDS_BIGENDIAN
643 tcg_out32 (s
, LBZ
| RT (data_reg
) | RA (r0
));
646 tcg_out32 (s
, LBZ
| RT (data_reg
) | RA (r0
));
647 tcg_out32 (s
, EXTSB
| RA (data_reg
) | RS (data_reg
));
650 if (bswap
) tcg_out32 (s
, LHBRX
| RT (data_reg
) | RB (r0
));
651 else tcg_out32 (s
, LHZ
| RT (data_reg
) | RA (r0
));
655 tcg_out32 (s
, LHBRX
| RT (data_reg
) | RB (r0
));
656 tcg_out32 (s
, EXTSH
| RA (data_reg
) | RS (data_reg
));
658 else tcg_out32 (s
, LHA
| RT (data_reg
) | RA (r0
));
661 if (bswap
) tcg_out32 (s
, LWBRX
| RT (data_reg
) | RB (r0
));
662 else tcg_out32 (s
, LWZ
| RT (data_reg
)| RA (r0
));
666 tcg_out32 (s
, ADDI
| RT (r1
) | RA (r0
) | 4);
667 tcg_out32 (s
, LWBRX
| RT (data_reg
) | RB (r0
));
668 tcg_out32 (s
, LWBRX
| RT (data_reg2
) | RB (r1
));
671 if (r0
== data_reg2
) {
672 tcg_out32 (s
, LWZ
| RT (0) | RA (r0
));
673 tcg_out32 (s
, LWZ
| RT (data_reg
) | RA (r0
) | 4);
674 tcg_out_mov (s
, data_reg2
, 0);
677 tcg_out32 (s
, LWZ
| RT (data_reg2
) | RA (r0
));
678 tcg_out32 (s
, LWZ
| RT (data_reg
) | RA (r0
) | 4);
684 #ifdef CONFIG_SOFTMMU
685 reloc_pc24 (label2_ptr
, (tcg_target_long
) s
->code_ptr
);
689 static void tcg_out_qemu_st (TCGContext
*s
, const TCGArg
*args
, int opc
)
691 int addr_reg
, r0
, r1
, data_reg
, data_reg2
, mem_index
, bswap
;
692 #ifdef CONFIG_SOFTMMU
694 void *label1_ptr
, *label2_ptr
;
696 #if TARGET_LONG_BITS == 64
706 #if TARGET_LONG_BITS == 64
711 #ifdef CONFIG_SOFTMMU
716 tcg_out32 (s
, (RLWINM
719 | SH (32 - (TARGET_PAGE_BITS
- CPU_TLB_ENTRY_BITS
))
720 | MB (32 - (CPU_TLB_ENTRY_BITS
+ CPU_TLB_BITS
))
721 | ME (31 - CPU_TLB_ENTRY_BITS
)
724 tcg_out32 (s
, ADD
| RT (r0
) | RA (r0
) | RB (TCG_AREG0
));
728 | offsetof (CPUState
, tlb_table
[mem_index
][0].addr_write
)
731 tcg_out32 (s
, (RLWINM
735 | MB ((32 - opc
) & 31)
736 | ME (31 - TARGET_PAGE_BITS
)
740 tcg_out32 (s
, CMP
| (7 << 23) | RA (r2
) | RB (r1
));
741 #if TARGET_LONG_BITS == 64
742 tcg_out32 (s
, LWZ
| RT (r1
) | RA (r0
) | 4);
743 tcg_out32 (s
, CMP
| BF (6) | RA (addr_reg2
) | RB (r1
));
744 tcg_out32 (s
, CRAND
| BT (7, CR_EQ
) | BA (6, CR_EQ
) | BB (7, CR_EQ
));
747 label1_ptr
= s
->code_ptr
;
749 tcg_out32 (s
, BC
| BI (7, CR_EQ
) | BO_COND_TRUE
);
753 #if TARGET_LONG_BITS == 32
754 tcg_out_mov (s
, 3, addr_reg
);
757 tcg_out_mov (s
, 3, addr_reg2
);
758 tcg_out_mov (s
, 4, addr_reg
);
759 #ifdef TCG_TARGET_CALL_ALIGN_ARGS
768 tcg_out32 (s
, (RLWINM
776 tcg_out32 (s
, (RLWINM
784 tcg_out_mov (s
, ir
, data_reg
);
787 #ifdef TCG_TARGET_CALL_ALIGN_ARGS
790 tcg_out_mov (s
, ir
++, data_reg2
);
791 tcg_out_mov (s
, ir
, data_reg
);
796 tcg_out_movi (s
, TCG_TYPE_I32
, ir
, mem_index
);
798 tcg_out_call (s
, (tcg_target_long
) qemu_st_helpers
[opc
], 1);
800 tcg_out_b (s
, LK
, (tcg_target_long
) qemu_st_helpers
[opc
]);
802 label2_ptr
= s
->code_ptr
;
805 /* label1: fast path */
807 reloc_pc14 (label1_ptr
, (tcg_target_long
) s
->code_ptr
);
813 | (ADDEND_OFFSET
+ offsetof (CPUTLBEntry
, addend
)
814 - offsetof (CPUTLBEntry
, addr_write
))
816 /* r0 = env->tlb_table[mem_index][index].addend */
817 tcg_out32 (s
, ADD
| RT (r0
) | RA (r0
) | RB (addr_reg
));
818 /* r0 = env->tlb_table[mem_index][index].addend + addr */
820 #else /* !CONFIG_SOFTMMU */
825 #ifdef TARGET_WORDS_BIGENDIAN
832 tcg_out32 (s
, STB
| RS (data_reg
) | RA (r0
));
835 if (bswap
) tcg_out32 (s
, STHBRX
| RS (data_reg
) | RA (0) | RB (r0
));
836 else tcg_out32 (s
, STH
| RS (data_reg
) | RA (r0
));
839 if (bswap
) tcg_out32 (s
, STWBRX
| RS (data_reg
) | RA (0) | RB (r0
));
840 else tcg_out32 (s
, STW
| RS (data_reg
) | RA (r0
));
844 tcg_out32 (s
, ADDI
| RT (r1
) | RA (r0
) | 4);
845 tcg_out32 (s
, STWBRX
| RS (data_reg
) | RA (0) | RB (r0
));
846 tcg_out32 (s
, STWBRX
| RS (data_reg2
) | RA (0) | RB (r1
));
849 tcg_out32 (s
, STW
| RS (data_reg2
) | RA (r0
));
850 tcg_out32 (s
, STW
| RS (data_reg
) | RA (r0
) | 4);
855 #ifdef CONFIG_SOFTMMU
856 reloc_pc24 (label2_ptr
, (tcg_target_long
) s
->code_ptr
);
860 void tcg_target_qemu_prologue (TCGContext
*s
)
866 + TCG_STATIC_CALL_ARGS_SIZE
867 + ARRAY_SIZE (tcg_target_callee_save_regs
) * 4
869 frame_size
= (frame_size
+ 15) & ~15;
875 /* First emit adhoc function descriptor */
876 addr
= (uint32_t) s
->code_ptr
+ 12;
877 tcg_out32 (s
, addr
); /* entry point */
878 s
->code_ptr
+= 8; /* skip TOC and environment pointer */
881 tcg_out32 (s
, MFSPR
| RT (0) | LR
);
882 tcg_out32 (s
, STWU
| RS (1) | RA (1) | (-frame_size
& 0xffff));
883 for (i
= 0; i
< ARRAY_SIZE (tcg_target_callee_save_regs
); ++i
)
885 | RS (tcg_target_callee_save_regs
[i
])
887 | (i
* 4 + LINKAGE_AREA_SIZE
+ TCG_STATIC_CALL_ARGS_SIZE
)
890 tcg_out32 (s
, STW
| RS (0) | RA (1) | (frame_size
+ LR_OFFSET
));
892 tcg_out32 (s
, MTSPR
| RS (3) | CTR
);
893 tcg_out32 (s
, BCCTR
| BO_ALWAYS
);
894 tb_ret_addr
= s
->code_ptr
;
896 for (i
= 0; i
< ARRAY_SIZE (tcg_target_callee_save_regs
); ++i
)
898 | RT (tcg_target_callee_save_regs
[i
])
900 | (i
* 4 + LINKAGE_AREA_SIZE
+ TCG_STATIC_CALL_ARGS_SIZE
)
903 tcg_out32 (s
, LWZ
| RT (0) | RA (1) | (frame_size
+ LR_OFFSET
));
904 tcg_out32 (s
, MTSPR
| RS (0) | LR
);
905 tcg_out32 (s
, ADDI
| RT (1) | RA (1) | frame_size
);
906 tcg_out32 (s
, BCLR
| BO_ALWAYS
);
909 static void tcg_out_ld (TCGContext
*s
, TCGType type
, int ret
, int arg1
,
910 tcg_target_long arg2
)
912 tcg_out_ldst (s
, ret
, arg1
, arg2
, LWZ
, LWZX
);
915 static void tcg_out_st (TCGContext
*s
, TCGType type
, int arg
, int arg1
,
916 tcg_target_long arg2
)
918 tcg_out_ldst (s
, arg
, arg1
, arg2
, STW
, STWX
);
921 static void ppc_addi (TCGContext
*s
, int rt
, int ra
, tcg_target_long si
)
926 if (si
== (int16_t) si
)
927 tcg_out32 (s
, ADDI
| RT (rt
) | RA (ra
) | (si
& 0xffff));
929 uint16_t h
= ((si
>> 16) & 0xffff) + ((uint16_t) si
>> 15);
930 tcg_out32 (s
, ADDIS
| RT (rt
) | RA (ra
) | h
);
931 tcg_out32 (s
, ADDI
| RT (rt
) | RA (rt
) | (si
& 0xffff));
935 static void tcg_out_addi(TCGContext
*s
, int reg
, tcg_target_long val
)
937 ppc_addi (s
, reg
, reg
, val
);
940 static void tcg_out_cmp (TCGContext
*s
, int cond
, TCGArg arg1
, TCGArg arg2
,
941 int const_arg2
, int cr
)
950 if ((int16_t) arg2
== arg2
) {
955 else if ((uint16_t) arg2
== arg2
) {
970 if ((int16_t) arg2
== arg2
) {
985 if ((uint16_t) arg2
== arg2
) {
1001 tcg_out32 (s
, op
| RA (arg1
) | (arg2
& 0xffff));
1004 tcg_out_movi (s
, TCG_TYPE_I32
, 0, arg2
);
1005 tcg_out32 (s
, op
| RA (arg1
) | RB (0));
1008 tcg_out32 (s
, op
| RA (arg1
) | RB (arg2
));
1013 static void tcg_out_bc (TCGContext
*s
, int bc
, int label_index
)
1015 TCGLabel
*l
= &s
->labels
[label_index
];
1018 tcg_out32 (s
, bc
| reloc_pc14_val (s
->code_ptr
, l
->u
.value
));
1020 uint16_t val
= *(uint16_t *) &s
->code_ptr
[2];
1022 /* Thanks to Andrzej Zaborowski */
1023 tcg_out32 (s
, bc
| (val
& 0xfffc));
1024 tcg_out_reloc (s
, s
->code_ptr
- 4, R_PPC_REL14
, label_index
, 0);
1028 static void tcg_out_brcond (TCGContext
*s
, int cond
,
1029 TCGArg arg1
, TCGArg arg2
, int const_arg2
,
1032 tcg_out_cmp (s
, cond
, arg1
, arg2
, const_arg2
, 7);
1033 tcg_out_bc (s
, tcg_to_bc
[cond
], label_index
);
1036 /* XXX: we implement it at the target level to avoid having to
1037 handle cross basic blocks temporaries */
1038 static void tcg_out_brcond2 (TCGContext
*s
, const TCGArg
*args
,
1039 const int *const_args
)
1041 int cond
= args
[4], label_index
= args
[5], op
;
1042 struct { int bit1
; int bit2
; int cond2
; } bits
[] = {
1043 [TCG_COND_LT
] = { CR_LT
, CR_LT
, TCG_COND_LT
},
1044 [TCG_COND_LE
] = { CR_LT
, CR_GT
, TCG_COND_LT
},
1045 [TCG_COND_GT
] = { CR_GT
, CR_GT
, TCG_COND_GT
},
1046 [TCG_COND_GE
] = { CR_GT
, CR_LT
, TCG_COND_GT
},
1047 [TCG_COND_LTU
] = { CR_LT
, CR_LT
, TCG_COND_LTU
},
1048 [TCG_COND_LEU
] = { CR_LT
, CR_GT
, TCG_COND_LTU
},
1049 [TCG_COND_GTU
] = { CR_GT
, CR_GT
, TCG_COND_GTU
},
1050 [TCG_COND_GEU
] = { CR_GT
, CR_LT
, TCG_COND_GTU
},
1051 }, *b
= &bits
[cond
];
1056 op
= (cond
== TCG_COND_EQ
) ? CRAND
: CRNAND
;
1057 tcg_out_cmp (s
, cond
, args
[0], args
[2], const_args
[2], 6);
1058 tcg_out_cmp (s
, cond
, args
[1], args
[3], const_args
[3], 7);
1059 tcg_out32 (s
, op
| BT (7, CR_EQ
) | BA (6, CR_EQ
) | BB (7, CR_EQ
));
1069 op
= (b
->bit1
!= b
->bit2
) ? CRANDC
: CRAND
;
1070 tcg_out_cmp (s
, b
->cond2
, args
[1], args
[3], const_args
[3], 5);
1071 tcg_out_cmp (s
, TCG_COND_EQ
, args
[1], args
[3], const_args
[3], 6);
1072 tcg_out_cmp (s
, cond
, args
[0], args
[2], const_args
[2], 7);
1073 tcg_out32 (s
, op
| BT (7, CR_EQ
) | BA (6, CR_EQ
) | BB (7, b
->bit2
));
1074 tcg_out32 (s
, CROR
| BT (7, CR_EQ
) | BA (5, b
->bit1
) | BB (7, CR_EQ
));
1080 tcg_out_bc (s
, (BC
| BI (7, CR_EQ
) | BO_COND_TRUE
), label_index
);
1083 void ppc_tb_set_jmp_target (unsigned long jmp_addr
, unsigned long addr
)
1086 long disp
= addr
- jmp_addr
;
1087 unsigned long patch_size
;
1089 ptr
= (uint32_t *)jmp_addr
;
1091 if ((disp
<< 6) >> 6 != disp
) {
1092 ptr
[0] = 0x3c000000 | (addr
>> 16); /* lis 0,addr@ha */
1093 ptr
[1] = 0x60000000 | (addr
& 0xffff); /* la 0,addr@l(0) */
1094 ptr
[2] = 0x7c0903a6; /* mtctr 0 */
1095 ptr
[3] = 0x4e800420; /* brctr */
1098 /* patch the branch destination */
1100 *ptr
= 0x48000000 | (disp
& 0x03fffffc); /* b disp */
1103 ptr
[0] = 0x60000000; /* nop */
1104 ptr
[1] = 0x60000000;
1105 ptr
[2] = 0x60000000;
1106 ptr
[3] = 0x60000000;
1111 flush_icache_range(jmp_addr
, jmp_addr
+ patch_size
);
1114 static void tcg_out_op(TCGContext
*s
, int opc
, const TCGArg
*args
,
1115 const int *const_args
)
1118 case INDEX_op_exit_tb
:
1119 tcg_out_movi (s
, TCG_TYPE_I32
, TCG_REG_R3
, args
[0]);
1120 tcg_out_b (s
, 0, (tcg_target_long
) tb_ret_addr
);
1122 case INDEX_op_goto_tb
:
1123 if (s
->tb_jmp_offset
) {
1124 /* direct jump method */
1126 s
->tb_jmp_offset
[args
[0]] = s
->code_ptr
- s
->code_buf
;
1132 s
->tb_next_offset
[args
[0]] = s
->code_ptr
- s
->code_buf
;
1136 TCGLabel
*l
= &s
->labels
[args
[0]];
1139 tcg_out_b (s
, 0, l
->u
.value
);
1142 uint32_t val
= *(uint32_t *) s
->code_ptr
;
1144 /* Thanks to Andrzej Zaborowski */
1145 tcg_out32 (s
, B
| (val
& 0x3fffffc));
1146 tcg_out_reloc (s
, s
->code_ptr
- 4, R_PPC_REL24
, args
[0], 0);
1152 tcg_out_call (s
, args
[0], const_args
[0]);
1154 if (const_args
[0]) {
1155 tcg_out_b (s
, LK
, args
[0]);
1158 tcg_out32 (s
, MTSPR
| RS (args
[0]) | LR
);
1159 tcg_out32 (s
, BCLR
| BO_ALWAYS
| LK
);
1164 if (const_args
[0]) {
1165 tcg_out_b (s
, 0, args
[0]);
1168 tcg_out32 (s
, MTSPR
| RS (args
[0]) | CTR
);
1169 tcg_out32 (s
, BCCTR
| BO_ALWAYS
);
1172 case INDEX_op_movi_i32
:
1173 tcg_out_movi(s
, TCG_TYPE_I32
, args
[0], args
[1]);
1175 case INDEX_op_ld8u_i32
:
1176 tcg_out_ldst (s
, args
[0], args
[1], args
[2], LBZ
, LBZX
);
1178 case INDEX_op_ld8s_i32
:
1179 tcg_out_ldst (s
, args
[0], args
[1], args
[2], LBZ
, LBZX
);
1180 tcg_out32 (s
, EXTSB
| RS (args
[0]) | RA (args
[0]));
1182 case INDEX_op_ld16u_i32
:
1183 tcg_out_ldst (s
, args
[0], args
[1], args
[2], LHZ
, LHZX
);
1185 case INDEX_op_ld16s_i32
:
1186 tcg_out_ldst (s
, args
[0], args
[1], args
[2], LHA
, LHAX
);
1188 case INDEX_op_ld_i32
:
1189 tcg_out_ldst (s
, args
[0], args
[1], args
[2], LWZ
, LWZX
);
1191 case INDEX_op_st8_i32
:
1192 tcg_out_ldst (s
, args
[0], args
[1], args
[2], STB
, STBX
);
1194 case INDEX_op_st16_i32
:
1195 tcg_out_ldst (s
, args
[0], args
[1], args
[2], STH
, STHX
);
1197 case INDEX_op_st_i32
:
1198 tcg_out_ldst (s
, args
[0], args
[1], args
[2], STW
, STWX
);
1201 case INDEX_op_add_i32
:
1203 ppc_addi (s
, args
[0], args
[1], args
[2]);
1205 tcg_out32 (s
, ADD
| TAB (args
[0], args
[1], args
[2]));
1207 case INDEX_op_sub_i32
:
1209 ppc_addi (s
, args
[0], args
[1], -args
[2]);
1211 tcg_out32 (s
, SUBF
| TAB (args
[0], args
[2], args
[1]));
1214 case INDEX_op_and_i32
:
1215 if (const_args
[2]) {
1216 if ((args
[2] & 0xffff) == args
[2])
1217 tcg_out32 (s
, ANDI
| RS (args
[1]) | RA (args
[0]) | args
[2]);
1218 else if ((args
[2] & 0xffff0000) == args
[2])
1219 tcg_out32 (s
, ANDIS
| RS (args
[1]) | RA (args
[0])
1220 | ((args
[2] >> 16) & 0xffff));
1222 tcg_out_movi (s
, TCG_TYPE_I32
, 0, args
[2]);
1223 tcg_out32 (s
, AND
| SAB (args
[1], args
[0], 0));
1227 tcg_out32 (s
, AND
| SAB (args
[1], args
[0], args
[2]));
1229 case INDEX_op_or_i32
:
1230 if (const_args
[2]) {
1231 if (args
[2] & 0xffff) {
1232 tcg_out32 (s
, ORI
| RS (args
[1]) | RA (args
[0])
1233 | (args
[2] & 0xffff));
1235 tcg_out32 (s
, ORIS
| RS (args
[0]) | RA (args
[0])
1236 | ((args
[2] >> 16) & 0xffff));
1239 tcg_out32 (s
, ORIS
| RS (args
[1]) | RA (args
[0])
1240 | ((args
[2] >> 16) & 0xffff));
1244 tcg_out32 (s
, OR
| SAB (args
[1], args
[0], args
[2]));
1246 case INDEX_op_xor_i32
:
1247 if (const_args
[2]) {
1248 if ((args
[2] & 0xffff) == args
[2])
1249 tcg_out32 (s
, XORI
| RS (args
[1]) | RA (args
[0])
1250 | (args
[2] & 0xffff));
1251 else if ((args
[2] & 0xffff0000) == args
[2])
1252 tcg_out32 (s
, XORIS
| RS (args
[1]) | RA (args
[0])
1253 | ((args
[2] >> 16) & 0xffff));
1255 tcg_out_movi (s
, TCG_TYPE_I32
, 0, args
[2]);
1256 tcg_out32 (s
, XOR
| SAB (args
[1], args
[0], 0));
1260 tcg_out32 (s
, XOR
| SAB (args
[1], args
[0], args
[2]));
1263 case INDEX_op_mul_i32
:
1264 if (const_args
[2]) {
1265 if (args
[2] == (int16_t) args
[2])
1266 tcg_out32 (s
, MULLI
| RT (args
[0]) | RA (args
[1])
1267 | (args
[2] & 0xffff));
1269 tcg_out_movi (s
, TCG_TYPE_I32
, 0, args
[2]);
1270 tcg_out32 (s
, MULLW
| TAB (args
[0], args
[1], 0));
1274 tcg_out32 (s
, MULLW
| TAB (args
[0], args
[1], args
[2]));
1277 case INDEX_op_div_i32
:
1278 tcg_out32 (s
, DIVW
| TAB (args
[0], args
[1], args
[2]));
1281 case INDEX_op_divu_i32
:
1282 tcg_out32 (s
, DIVWU
| TAB (args
[0], args
[1], args
[2]));
1285 case INDEX_op_rem_i32
:
1286 tcg_out32 (s
, DIVW
| TAB (0, args
[1], args
[2]));
1287 tcg_out32 (s
, MULLW
| TAB (0, 0, args
[2]));
1288 tcg_out32 (s
, SUBF
| TAB (args
[0], 0, args
[1]));
1291 case INDEX_op_remu_i32
:
1292 tcg_out32 (s
, DIVWU
| TAB (0, args
[1], args
[2]));
1293 tcg_out32 (s
, MULLW
| TAB (0, 0, args
[2]));
1294 tcg_out32 (s
, SUBF
| TAB (args
[0], 0, args
[1]));
1297 case INDEX_op_mulu2_i32
:
1298 if (args
[0] == args
[2] || args
[0] == args
[3]) {
1299 tcg_out32 (s
, MULLW
| TAB (0, args
[2], args
[3]));
1300 tcg_out32 (s
, MULHWU
| TAB (args
[1], args
[2], args
[3]));
1301 tcg_out_mov (s
, args
[0], 0);
1304 tcg_out32 (s
, MULLW
| TAB (args
[0], args
[2], args
[3]));
1305 tcg_out32 (s
, MULHWU
| TAB (args
[1], args
[2], args
[3]));
1309 case INDEX_op_shl_i32
:
1310 if (const_args
[2]) {
1311 tcg_out32 (s
, (RLWINM
1321 tcg_out32 (s
, SLW
| SAB (args
[1], args
[0], args
[2]));
1323 case INDEX_op_shr_i32
:
1324 if (const_args
[2]) {
1325 tcg_out32 (s
, (RLWINM
1335 tcg_out32 (s
, SRW
| SAB (args
[1], args
[0], args
[2]));
1337 case INDEX_op_sar_i32
:
1339 tcg_out32 (s
, SRAWI
| RS (args
[1]) | RA (args
[0]) | SH (args
[2]));
1341 tcg_out32 (s
, SRAW
| SAB (args
[1], args
[0], args
[2]));
1344 case INDEX_op_add2_i32
:
1345 if (args
[0] == args
[3] || args
[0] == args
[5]) {
1346 tcg_out32 (s
, ADDC
| TAB (0, args
[2], args
[4]));
1347 tcg_out32 (s
, ADDE
| TAB (args
[1], args
[3], args
[5]));
1348 tcg_out_mov (s
, args
[0], 0);
1351 tcg_out32 (s
, ADDC
| TAB (args
[0], args
[2], args
[4]));
1352 tcg_out32 (s
, ADDE
| TAB (args
[1], args
[3], args
[5]));
1355 case INDEX_op_sub2_i32
:
1356 if (args
[0] == args
[3] || args
[0] == args
[5]) {
1357 tcg_out32 (s
, SUBFC
| TAB (0, args
[4], args
[2]));
1358 tcg_out32 (s
, SUBFE
| TAB (args
[1], args
[5], args
[3]));
1359 tcg_out_mov (s
, args
[0], 0);
1362 tcg_out32 (s
, SUBFC
| TAB (args
[0], args
[4], args
[2]));
1363 tcg_out32 (s
, SUBFE
| TAB (args
[1], args
[5], args
[3]));
1367 case INDEX_op_brcond_i32
:
1372 args[3] = r1 is const
1373 args[4] = label_index
1375 tcg_out_brcond (s
, args
[2], args
[0], args
[1], const_args
[1], args
[3]);
1377 case INDEX_op_brcond2_i32
:
1378 tcg_out_brcond2(s
, args
, const_args
);
1381 case INDEX_op_neg_i32
:
1382 tcg_out32 (s
, NEG
| RT (args
[0]) | RA (args
[1]));
1385 case INDEX_op_qemu_ld8u
:
1386 tcg_out_qemu_ld(s
, args
, 0);
1388 case INDEX_op_qemu_ld8s
:
1389 tcg_out_qemu_ld(s
, args
, 0 | 4);
1391 case INDEX_op_qemu_ld16u
:
1392 tcg_out_qemu_ld(s
, args
, 1);
1394 case INDEX_op_qemu_ld16s
:
1395 tcg_out_qemu_ld(s
, args
, 1 | 4);
1397 case INDEX_op_qemu_ld32u
:
1398 tcg_out_qemu_ld(s
, args
, 2);
1400 case INDEX_op_qemu_ld64
:
1401 tcg_out_qemu_ld(s
, args
, 3);
1403 case INDEX_op_qemu_st8
:
1404 tcg_out_qemu_st(s
, args
, 0);
1406 case INDEX_op_qemu_st16
:
1407 tcg_out_qemu_st(s
, args
, 1);
1409 case INDEX_op_qemu_st32
:
1410 tcg_out_qemu_st(s
, args
, 2);
1412 case INDEX_op_qemu_st64
:
1413 tcg_out_qemu_st(s
, args
, 3);
1416 case INDEX_op_ext8s_i32
:
1417 tcg_out32 (s
, EXTSB
| RS (args
[1]) | RA (args
[0]));
1419 case INDEX_op_ext16s_i32
:
1420 tcg_out32 (s
, EXTSH
| RS (args
[1]) | RA (args
[0]));
1424 tcg_dump_ops (s
, stderr
);
1429 static const TCGTargetOpDef ppc_op_defs
[] = {
1430 { INDEX_op_exit_tb
, { } },
1431 { INDEX_op_goto_tb
, { } },
1432 { INDEX_op_call
, { "ri" } },
1433 { INDEX_op_jmp
, { "ri" } },
1434 { INDEX_op_br
, { } },
1436 { INDEX_op_mov_i32
, { "r", "r" } },
1437 { INDEX_op_movi_i32
, { "r" } },
1438 { INDEX_op_ld8u_i32
, { "r", "r" } },
1439 { INDEX_op_ld8s_i32
, { "r", "r" } },
1440 { INDEX_op_ld16u_i32
, { "r", "r" } },
1441 { INDEX_op_ld16s_i32
, { "r", "r" } },
1442 { INDEX_op_ld_i32
, { "r", "r" } },
1443 { INDEX_op_st8_i32
, { "r", "r" } },
1444 { INDEX_op_st16_i32
, { "r", "r" } },
1445 { INDEX_op_st_i32
, { "r", "r" } },
1447 { INDEX_op_add_i32
, { "r", "r", "ri" } },
1448 { INDEX_op_mul_i32
, { "r", "r", "ri" } },
1449 { INDEX_op_div_i32
, { "r", "r", "r" } },
1450 { INDEX_op_divu_i32
, { "r", "r", "r" } },
1451 { INDEX_op_rem_i32
, { "r", "r", "r" } },
1452 { INDEX_op_remu_i32
, { "r", "r", "r" } },
1453 { INDEX_op_mulu2_i32
, { "r", "r", "r", "r" } },
1454 { INDEX_op_sub_i32
, { "r", "r", "ri" } },
1455 { INDEX_op_and_i32
, { "r", "r", "ri" } },
1456 { INDEX_op_or_i32
, { "r", "r", "ri" } },
1457 { INDEX_op_xor_i32
, { "r", "r", "ri" } },
1459 { INDEX_op_shl_i32
, { "r", "r", "ri" } },
1460 { INDEX_op_shr_i32
, { "r", "r", "ri" } },
1461 { INDEX_op_sar_i32
, { "r", "r", "ri" } },
1463 { INDEX_op_brcond_i32
, { "r", "ri" } },
1465 { INDEX_op_add2_i32
, { "r", "r", "r", "r", "r", "r" } },
1466 { INDEX_op_sub2_i32
, { "r", "r", "r", "r", "r", "r" } },
1467 { INDEX_op_brcond2_i32
, { "r", "r", "r", "r" } },
1469 { INDEX_op_neg_i32
, { "r", "r" } },
1471 #if TARGET_LONG_BITS == 32
1472 { INDEX_op_qemu_ld8u
, { "r", "L" } },
1473 { INDEX_op_qemu_ld8s
, { "r", "L" } },
1474 { INDEX_op_qemu_ld16u
, { "r", "L" } },
1475 { INDEX_op_qemu_ld16s
, { "r", "L" } },
1476 { INDEX_op_qemu_ld32u
, { "r", "L" } },
1477 { INDEX_op_qemu_ld32s
, { "r", "L" } },
1478 { INDEX_op_qemu_ld64
, { "r", "r", "L" } },
1480 { INDEX_op_qemu_st8
, { "K", "K" } },
1481 { INDEX_op_qemu_st16
, { "K", "K" } },
1482 { INDEX_op_qemu_st32
, { "K", "K" } },
1483 { INDEX_op_qemu_st64
, { "M", "M", "M" } },
1485 { INDEX_op_qemu_ld8u
, { "r", "L", "L" } },
1486 { INDEX_op_qemu_ld8s
, { "r", "L", "L" } },
1487 { INDEX_op_qemu_ld16u
, { "r", "L", "L" } },
1488 { INDEX_op_qemu_ld16s
, { "r", "L", "L" } },
1489 { INDEX_op_qemu_ld32u
, { "r", "L", "L" } },
1490 { INDEX_op_qemu_ld32s
, { "r", "L", "L" } },
1491 { INDEX_op_qemu_ld64
, { "r", "L", "L", "L" } },
1493 { INDEX_op_qemu_st8
, { "K", "K", "K" } },
1494 { INDEX_op_qemu_st16
, { "K", "K", "K" } },
1495 { INDEX_op_qemu_st32
, { "K", "K", "K" } },
1496 { INDEX_op_qemu_st64
, { "M", "M", "M", "M" } },
1499 { INDEX_op_ext8s_i32
, { "r", "r" } },
1500 { INDEX_op_ext16s_i32
, { "r", "r" } },
1505 void tcg_target_init(TCGContext
*s
)
1507 tcg_regset_set32(tcg_target_available_regs
[TCG_TYPE_I32
], 0, 0xffffffff);
1508 tcg_regset_set32(tcg_target_call_clobber_regs
, 0,
1520 (1 << TCG_REG_R10
) |
1521 (1 << TCG_REG_R11
) |
1525 tcg_regset_clear(s
->reserved_regs
);
1526 tcg_regset_set_reg(s
->reserved_regs
, TCG_REG_R0
);
1527 tcg_regset_set_reg(s
->reserved_regs
, TCG_REG_R1
);
1529 tcg_regset_set_reg(s
->reserved_regs
, TCG_REG_R2
);
1532 tcg_add_target_add_op_defs(ppc_op_defs
);