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 #if TARGET_PHYS_ADDR_BITS <= 32
29 #define ADDEND_OFFSET 0
31 #define ADDEND_OFFSET 4
34 static const char * const tcg_target_reg_names[TCG_TARGET_NB_REGS] = {
69 static const int tcg_target_reg_alloc_order[] = {
104 static const int tcg_target_call_iarg_regs[] = {
115 static const int tcg_target_call_oarg_regs[2] = {
120 static const int tcg_target_callee_save_regs[] = {
121 TCG_REG_R13, /* sould r13 be saved? */
138 static uint32_t reloc_pc24_val (void *pc, tcg_target_long target)
140 tcg_target_long disp;
142 disp = target - (tcg_target_long) pc;
143 if ((disp << 6) >> 6 != disp)
146 return disp & 0x3fffffc;
149 static void reloc_pc24 (void *pc, tcg_target_long target)
151 *(uint32_t *) pc = (*(uint32_t *) pc & ~0x3fffffc)
152 | reloc_pc24_val (pc, target);
155 static uint16_t reloc_pc14_val (void *pc, tcg_target_long target)
157 tcg_target_long disp;
159 disp = target - (tcg_target_long) pc;
160 if (disp != (int16_t) disp)
163 return disp & 0xfffc;
166 static void reloc_pc14 (void *pc, tcg_target_long target)
168 *(uint32_t *) pc = (*(uint32_t *) pc & ~0xfffc)
169 | reloc_pc14_val (pc, target);
172 static void patch_reloc(uint8_t *code_ptr, int type,
173 tcg_target_long value, tcg_target_long addend)
178 reloc_pc14 (code_ptr, value);
181 reloc_pc24 (code_ptr, value);
188 /* maximum number of register used for input function arguments */
189 static int tcg_target_get_call_iarg_regs_count(int flags)
191 return sizeof (tcg_target_call_iarg_regs) / sizeof (tcg_target_call_iarg_regs[0]);
194 /* parse target specific constraints */
195 static int target_parse_constraint(TCGArgConstraint *ct, const char **pct_str)
201 case 'A': case 'B': case 'C': case 'D':
202 ct->ct |= TCG_CT_REG;
203 tcg_regset_set_reg(ct->u.regs, 3 + ct_str[0] - 'A');
206 ct->ct |= TCG_CT_REG;
207 tcg_regset_set32(ct->u.regs, 0, 0xffffffff);
209 case 'L': /* qemu_ld constraint */
210 ct->ct |= TCG_CT_REG;
211 tcg_regset_set32(ct->u.regs, 0, 0xffffffff);
212 tcg_regset_reset_reg(ct->u.regs, TCG_REG_R3);
213 tcg_regset_reset_reg(ct->u.regs, TCG_REG_R4);
215 case 'K': /* qemu_st[8..32] constraint */
216 ct->ct |= TCG_CT_REG;
217 tcg_regset_set32(ct->u.regs, 0, 0xffffffff);
218 tcg_regset_reset_reg(ct->u.regs, TCG_REG_R3);
219 tcg_regset_reset_reg(ct->u.regs, TCG_REG_R4);
220 tcg_regset_reset_reg(ct->u.regs, TCG_REG_R5);
221 #if TARGET_LONG_BITS == 64
222 tcg_regset_reset_reg(ct->u.regs, TCG_REG_R6);
225 case 'M': /* qemu_st64 constraint */
226 ct->ct |= TCG_CT_REG;
227 tcg_regset_set32(ct->u.regs, 0, 0xffffffff);
228 tcg_regset_reset_reg(ct->u.regs, TCG_REG_R3);
229 tcg_regset_reset_reg(ct->u.regs, TCG_REG_R4);
230 tcg_regset_reset_reg(ct->u.regs, TCG_REG_R5);
231 tcg_regset_reset_reg(ct->u.regs, TCG_REG_R6);
232 tcg_regset_reset_reg(ct->u.regs, TCG_REG_R7);
242 /* test if a constant matches the constraint */
243 static int tcg_target_const_match(tcg_target_long val,
244 const TCGArgConstraint *arg_ct)
249 if (ct & TCG_CT_CONST)
254 #define OPCD(opc) ((opc)<<26)
255 #define XO31(opc) (OPCD(31)|((opc)<<1))
256 #define XO19(opc) (OPCD(19)|((opc)<<1))
268 #define ADDI OPCD(14)
269 #define ADDIS OPCD(15)
271 #define ORIS OPCD(25)
272 #define XORI OPCD(26)
273 #define XORIS OPCD(27)
274 #define ANDI OPCD(28)
275 #define ANDIS OPCD(29)
276 #define MULLI OPCD( 7)
277 #define CMPLI OPCD(10)
278 #define CMPI OPCD(11)
280 #define LWZU OPCD(33)
281 #define STWU OPCD(37)
283 #define RLWINM OPCD(21)
285 #define BCLR XO19(16)
286 #define BCCTR XO19(528)
287 #define CRAND XO19(257)
289 #define EXTSB XO31(954)
290 #define EXTSH XO31(922)
291 #define ADD XO31(266)
292 #define ADDE XO31(138)
293 #define ADDC XO31( 10)
294 #define AND XO31( 28)
295 #define SUBF XO31( 40)
296 #define SUBFC XO31( 8)
297 #define SUBFE XO31(136)
299 #define XOR XO31(316)
300 #define MULLW XO31(235)
301 #define MULHWU XO31( 11)
302 #define DIVW XO31(491)
303 #define DIVWU XO31(459)
305 #define CMPL XO31( 32)
306 #define LHBRX XO31(790)
307 #define LWBRX XO31(534)
308 #define STHBRX XO31(918)
309 #define STWBRX XO31(662)
310 #define MFSPR XO31(339)
311 #define MTSPR XO31(467)
312 #define SRAWI XO31(824)
313 #define NEG XO31(104)
315 #define LBZX XO31( 87)
316 #define LHZX XO31(276)
317 #define LHAX XO31(343)
318 #define LWZX XO31( 23)
319 #define STBX XO31(215)
320 #define STHX XO31(407)
321 #define STWX XO31(151)
323 #define SPR(a,b) ((((a)<<5)|(b))<<11)
325 #define CTR SPR(9, 0)
327 #define SLW XO31( 24)
328 #define SRW XO31(536)
329 #define SRAW XO31(792)
332 #define STMW OPCD(47)
335 #define TRAP (TW | TO (31))
337 #define RT(r) ((r)<<21)
338 #define RS(r) ((r)<<21)
339 #define RA(r) ((r)<<16)
340 #define RB(r) ((r)<<11)
341 #define TO(t) ((t)<<21)
342 #define SH(s) ((s)<<11)
343 #define MB(b) ((b)<<6)
344 #define ME(e) ((e)<<1)
345 #define BO(o) ((o)<<21)
349 #define TAB(t,a,b) (RT(t) | RA(a) | RB(b))
350 #define SAB(s,a,b) (RS(s) | RA(a) | RB(b))
352 #define BF(n) ((n)<<23)
353 #define BI(n, c) (((c)+((n)*4))<<16)
354 #define BT(n, c) (((c)+((n)*4))<<21)
355 #define BA(n, c) (((c)+((n)*4))<<16)
356 #define BB(n, c) (((c)+((n)*4))<<11)
358 #define BO_COND_TRUE BO (12)
359 #define BO_COND_FALSE BO (4)
360 #define BO_ALWAYS BO (20)
369 static const uint32_t tcg_to_bc[10] = {
370 [TCG_COND_EQ] = BC | BI (7, CR_EQ) | BO_COND_TRUE,
371 [TCG_COND_NE] = BC | BI (7, CR_EQ) | BO_COND_FALSE,
372 [TCG_COND_LT] = BC | BI (7, CR_LT) | BO_COND_TRUE,
373 [TCG_COND_GE] = BC | BI (7, CR_LT) | BO_COND_FALSE,
374 [TCG_COND_LE] = BC | BI (7, CR_GT) | BO_COND_FALSE,
375 [TCG_COND_GT] = BC | BI (7, CR_GT) | BO_COND_TRUE,
376 [TCG_COND_LTU] = BC | BI (7, CR_LT) | BO_COND_TRUE,
377 [TCG_COND_GEU] = BC | BI (7, CR_LT) | BO_COND_FALSE,
378 [TCG_COND_LEU] = BC | BI (7, CR_GT) | BO_COND_FALSE,
379 [TCG_COND_GTU] = BC | BI (7, CR_GT) | BO_COND_TRUE,
382 static void tcg_out_mov(TCGContext *s, int ret, int arg)
384 tcg_out32 (s, OR | SAB (arg, ret, arg));
387 static void tcg_out_movi(TCGContext *s, TCGType type,
388 int ret, tcg_target_long arg)
390 if (arg == (int16_t) arg)
391 tcg_out32 (s, ADDI | RT (ret) | RA (0) | (arg & 0xffff));
393 tcg_out32 (s, ADDIS | RT (ret) | RA (0) | ((arg >> 16) & 0xffff));
395 tcg_out32 (s, ORI | RS (ret) | RA (ret) | (arg & 0xffff));
399 static void tcg_out_ldst (TCGContext *s, int ret, int addr,
400 int offset, int op1, int op2)
402 if (offset == (int16_t) offset)
403 tcg_out32 (s, op1 | RT (ret) | RA (addr) | (offset & 0xffff));
405 tcg_out_movi (s, TCG_TYPE_I32, 0, offset);
406 tcg_out32 (s, op2 | RT (ret) | RA (addr) | RB (0));
410 static void tcg_out_b (TCGContext *s, int mask, tcg_target_long target)
412 tcg_target_long disp;
414 disp = target - (tcg_target_long) s->code_ptr;
415 if ((disp << 6) >> 6 == disp)
416 tcg_out32 (s, B | disp | mask);
418 tcg_out_movi (s, TCG_TYPE_I32, 0, (tcg_target_long) target);
419 tcg_out32 (s, MTSPR | RS (0) | CTR);
420 tcg_out32 (s, BCCTR | BO_ALWAYS | mask);
424 #if defined(CONFIG_SOFTMMU)
425 extern void __ldb_mmu(void);
426 extern void __ldw_mmu(void);
427 extern void __ldl_mmu(void);
428 extern void __ldq_mmu(void);
430 extern void __stb_mmu(void);
431 extern void __stw_mmu(void);
432 extern void __stl_mmu(void);
433 extern void __stq_mmu(void);
435 static void *qemu_ld_helpers[4] = {
442 static void *qemu_st_helpers[4] = {
450 static void tcg_out_qemu_ld (TCGContext *s, const TCGArg *args, int opc)
452 int addr_reg, data_reg, data_reg2, r0, mem_index, s_bits, bswap;
453 #ifdef CONFIG_SOFTMMU
455 void *label1_ptr, *label2_ptr;
457 #if TARGET_LONG_BITS == 64
467 #if TARGET_LONG_BITS == 64
473 #ifdef CONFIG_SOFTMMU
478 tcg_out32 (s, (RLWINM
481 | SH (32 - (TARGET_PAGE_BITS - CPU_TLB_ENTRY_BITS))
482 | MB (32 - (CPU_TLB_BITS + CPU_TLB_ENTRY_BITS))
483 | ME (31 - CPU_TLB_ENTRY_BITS)
486 tcg_out32 (s, ADD | RT (r0) | RA (r0) | RB (TCG_AREG0));
490 | offsetof (CPUState, tlb_table[mem_index][0].addr_read)
493 tcg_out32 (s, (RLWINM
497 | MB ((32 - s_bits) & 31)
498 | ME (31 - TARGET_PAGE_BITS)
502 tcg_out32 (s, CMP | BF (7) | RA (r2) | RB (r1));
503 #if TARGET_LONG_BITS == 64
504 tcg_out32 (s, LWZ | RT (r1) | RA (r0) | 4);
505 tcg_out32 (s, CMP | BF (6) | RA (addr_reg2) | RB (r1));
506 tcg_out32 (s, CRAND | BT (7, CR_EQ) | BA (6, CR_EQ) | BB (7, CR_EQ));
509 label1_ptr = s->code_ptr;
511 tcg_out32 (s, BC | BI (7, CR_EQ) | BO_COND_TRUE);
515 #if TARGET_LONG_BITS == 32
516 tcg_out_mov (s, 3, addr_reg);
517 tcg_out_movi (s, TCG_TYPE_I32, 4, mem_index);
519 tcg_out_mov (s, 3, addr_reg2);
520 tcg_out_mov (s, 4, addr_reg);
521 tcg_out_movi (s, TCG_TYPE_I32, 5, mem_index);
524 tcg_out_b (s, LK, (tcg_target_long) qemu_ld_helpers[s_bits]);
527 tcg_out32 (s, EXTSB | RA (data_reg) | RS (3));
530 tcg_out32 (s, EXTSH | RA (data_reg) | RS (3));
536 tcg_out_mov (s, data_reg, 3);
540 if (data_reg2 == 4) {
541 tcg_out_mov (s, 0, 4);
542 tcg_out_mov (s, 4, 3);
543 tcg_out_mov (s, 3, 0);
546 tcg_out_mov (s, data_reg2, 3);
547 tcg_out_mov (s, 3, 4);
551 if (data_reg != 4) tcg_out_mov (s, data_reg, 4);
552 if (data_reg2 != 3) tcg_out_mov (s, data_reg2, 3);
556 label2_ptr = s->code_ptr;
559 /* label1: fast path */
561 reloc_pc14 (label1_ptr, (tcg_target_long) s->code_ptr);
564 /* r0 now contains &env->tlb_table[mem_index][index].addr_read */
568 | (ADDEND_OFFSET + offsetof (CPUTLBEntry, addend)
569 - offsetof (CPUTLBEntry, addr_read))
571 /* r0 = env->tlb_table[mem_index][index].addend */
572 tcg_out32 (s, ADD | RT (r0) | RA (r0) | RB (addr_reg));
573 /* r0 = env->tlb_table[mem_index][index].addend + addr */
575 #else /* !CONFIG_SOFTMMU */
579 #ifdef TARGET_WORDS_BIGENDIAN
587 tcg_out32 (s, LBZ | RT (data_reg) | RA (r0));
590 tcg_out32 (s, LBZ | RT (data_reg) | RA (r0));
591 tcg_out32 (s, EXTSB | RA (data_reg) | RS (data_reg));
594 if (bswap) tcg_out32 (s, LHBRX | RT (data_reg) | RB (r0));
595 else tcg_out32 (s, LHZ | RT (data_reg) | RA (r0));
599 tcg_out32 (s, LHBRX | RT (data_reg) | RB (r0));
600 tcg_out32 (s, EXTSH | RA (data_reg) | RS (data_reg));
602 else tcg_out32 (s, LHA | RT (data_reg) | RA (r0));
605 if (bswap) tcg_out32 (s, LWBRX | RT (data_reg) | RB (r0));
606 else tcg_out32 (s, LWZ | RT (data_reg)| RA (r0));
610 if (r0 == data_reg) {
611 tcg_out32 (s, LWBRX | RT (0) | RB (r0));
612 tcg_out32 (s, ADDI | RT (r0) | RA (r0) | 4);
613 tcg_out32 (s, LWBRX | RT (data_reg2) | RB (r0));
614 tcg_out_mov (s, data_reg, 0);
617 tcg_out32 (s, LWBRX | RT (data_reg) | RB (r0));
618 tcg_out32 (s, ADDI | RT (r0) | RA (r0) | 4);
619 tcg_out32 (s, LWBRX | RT (data_reg2) | RB (r0));
623 if (r0 == data_reg2) {
624 tcg_out32 (s, LWZ | RT (0) | RA (r0));
625 tcg_out32 (s, LWZ | RT (data_reg) | RA (r0) | 4);
626 tcg_out_mov (s, data_reg2, 0);
629 tcg_out32 (s, LWZ | RT (data_reg2) | RA (r0));
630 tcg_out32 (s, LWZ | RT (data_reg) | RA (r0) | 4);
636 #ifdef CONFIG_SOFTMMU
637 reloc_pc24 (label2_ptr, (tcg_target_long) s->code_ptr);
641 static void tcg_out_qemu_st (TCGContext *s, const TCGArg *args, int opc)
643 int addr_reg, r0, r1, data_reg, data_reg2, mem_index, bswap;
644 #ifdef CONFIG_SOFTMMU
646 void *label1_ptr, *label2_ptr;
648 #if TARGET_LONG_BITS == 64
658 #if TARGET_LONG_BITS == 64
663 #ifdef CONFIG_SOFTMMU
668 tcg_out32 (s, (RLWINM
671 | SH (32 - (TARGET_PAGE_BITS - CPU_TLB_ENTRY_BITS))
672 | MB (32 - (CPU_TLB_ENTRY_BITS + CPU_TLB_BITS))
673 | ME (31 - CPU_TLB_ENTRY_BITS)
676 tcg_out32 (s, ADD | RT (r0) | RA (r0) | RB (TCG_AREG0));
680 | offsetof (CPUState, tlb_table[mem_index][0].addr_write)
683 tcg_out32 (s, (RLWINM
687 | MB ((32 - opc) & 31)
688 | ME (31 - TARGET_PAGE_BITS)
692 tcg_out32 (s, CMP | (7 << 23) | RA (r2) | RB (r1));
693 #if TARGET_LONG_BITS == 64
694 tcg_out32 (s, LWZ | RT (r1) | RA (r0) | 4);
695 tcg_out32 (s, CMP | BF (6) | RA (addr_reg2) | RB (r1));
696 tcg_out32 (s, CRAND | BT (7, CR_EQ) | BA (6, CR_EQ) | BB (7, CR_EQ));
699 label1_ptr = s->code_ptr;
701 tcg_out32 (s, BC | BI (7, CR_EQ) | BO_COND_TRUE);
705 #if TARGET_LONG_BITS == 32
706 tcg_out_mov (s, 3, addr_reg);
709 tcg_out_mov (s, 3, addr_reg2);
710 tcg_out_mov (s, 4, addr_reg);
716 tcg_out32 (s, (RLWINM
724 tcg_out32 (s, (RLWINM
732 tcg_out_mov (s, ir, data_reg);
735 tcg_out_mov (s, 5, data_reg2);
736 tcg_out_mov (s, 6, data_reg);
742 tcg_out_movi (s, TCG_TYPE_I32, ir, mem_index);
743 tcg_out_b (s, LK, (tcg_target_long) qemu_st_helpers[opc]);
744 label2_ptr = s->code_ptr;
747 /* label1: fast path */
749 reloc_pc14 (label1_ptr, (tcg_target_long) s->code_ptr);
755 | (ADDEND_OFFSET + offsetof (CPUTLBEntry, addend)
756 - offsetof (CPUTLBEntry, addr_write))
758 /* r0 = env->tlb_table[mem_index][index].addend */
759 tcg_out32 (s, ADD | RT (r0) | RA (r0) | RB (addr_reg));
760 /* r0 = env->tlb_table[mem_index][index].addend + addr */
762 #else /* !CONFIG_SOFTMMU */
767 #ifdef TARGET_WORDS_BIGENDIAN
774 tcg_out32 (s, STB | RS (data_reg) | RA (r0));
777 if (bswap) tcg_out32 (s, STHBRX | RS (data_reg) | RA (0) | RB (r0));
778 else tcg_out32 (s, STH | RS (data_reg) | RA (r0));
781 if (bswap) tcg_out32 (s, STWBRX | RS (data_reg) | RA (0) | RB (r0));
782 else tcg_out32 (s, STW | RS (data_reg) | RA (r0));
786 tcg_out32 (s, ADDI | RT (r1) | RA (r0) | 4);
787 tcg_out32 (s, STWBRX | RS (data_reg) | RA (0) | RB (r0));
788 tcg_out32 (s, STWBRX | RS (data_reg2) | RA (0) | RB (r1));
791 tcg_out32 (s, STW | RS (data_reg2) | RA (r0));
792 tcg_out32 (s, STW | RS (data_reg) | RA (r0) | 4);
797 #ifdef CONFIG_SOFTMMU
798 reloc_pc24 (label2_ptr, (tcg_target_long) s->code_ptr);
802 void tcg_target_qemu_prologue (TCGContext *s)
809 + TCG_STATIC_CALL_ARGS_SIZE
810 + ARRAY_SIZE (tcg_target_callee_save_regs) * 4
812 frame_size = (frame_size + 15) & ~15;
814 tcg_out32 (s, MFSPR | RT (0) | LR);
815 tcg_out32 (s, STWU | RS (1) | RA (1) | (-frame_size & 0xffff));
816 for (i = 0; i < ARRAY_SIZE (tcg_target_callee_save_regs); ++i)
818 | RS (tcg_target_callee_save_regs[i])
820 | (i * 4 + 8 + TCG_STATIC_CALL_ARGS_SIZE)
823 tcg_out32 (s, STW | RS (0) | RA (1) | (frame_size - 4));
825 tcg_out32 (s, MTSPR | RS (3) | CTR);
826 tcg_out32 (s, BCCTR | BO_ALWAYS);
827 tb_ret_addr = s->code_ptr;
829 for (i = 0; i < ARRAY_SIZE (tcg_target_callee_save_regs); ++i)
831 | RT (tcg_target_callee_save_regs[i])
833 | (i * 4 + 8 + TCG_STATIC_CALL_ARGS_SIZE)
836 tcg_out32 (s, LWZ | RT (0) | RA (1) | (frame_size - 4));
837 tcg_out32 (s, MTSPR | RS (0) | LR);
838 tcg_out32 (s, ADDI | RT (1) | RA (1) | frame_size);
839 tcg_out32 (s, BCLR | BO_ALWAYS);
842 static void tcg_out_ld (TCGContext *s, TCGType type, int ret, int arg1,
843 tcg_target_long arg2)
845 tcg_out_ldst (s, ret, arg1, arg2, LWZ, LWZX);
848 static void tcg_out_st (TCGContext *s, TCGType type, int arg, int arg1,
849 tcg_target_long arg2)
851 tcg_out_ldst (s, arg, arg1, arg2, STW, STWX);
854 static void ppc_addi (TCGContext *s, int rt, int ra, tcg_target_long si)
859 if (si == (int16_t) si)
860 tcg_out32 (s, ADDI | RT (rt) | RA (ra) | (si & 0xffff));
862 uint16_t h = ((si >> 16) & 0xffff) + ((uint16_t) si >> 15);
863 tcg_out32 (s, ADDIS | RT (rt) | RA (ra) | h);
864 tcg_out32 (s, ADDI | RT (rt) | RA (rt) | (si & 0xffff));
868 static void tcg_out_addi(TCGContext *s, int reg, tcg_target_long val)
870 ppc_addi (s, reg, reg, val);
873 static void tcg_out_brcond(TCGContext *s, int cond,
874 TCGArg arg1, TCGArg arg2, int const_arg2,
877 TCGLabel *l = &s->labels[label_index];
885 if ((int16_t) arg2 == arg2) {
890 else if ((uint16_t) arg2 == arg2) {
905 if ((int16_t) arg2 == arg2) {
920 if ((uint16_t) arg2 == arg2) {
936 tcg_out32 (s, op | RA (arg1) | (arg2 & 0xffff));
939 tcg_out_movi (s, TCG_TYPE_I32, 0, arg2);
940 tcg_out32 (s, op | RA (arg1) | RB (0));
943 tcg_out32 (s, op | RA (arg1) | RB (arg2));
947 tcg_out32 (s, tcg_to_bc[cond] | reloc_pc14_val (s->code_ptr,
950 uint16_t val = *(uint16_t *) &s->code_ptr[2];
952 /* Thanks to Andrzej Zaborowski */
953 tcg_out32 (s, tcg_to_bc[cond] | (val & 0xfffc));
954 tcg_out_reloc (s, s->code_ptr - 4, R_PPC_REL14, label_index, 0);
958 /* brcond2 is taken verbatim from i386 tcg-target */
959 /* XXX: we implement it at the target level to avoid having to
960 handle cross basic blocks temporaries */
961 static void tcg_out_brcond2(TCGContext *s,
962 const TCGArg *args, const int *const_args)
965 label_next = gen_new_label();
968 tcg_out_brcond(s, TCG_COND_NE, args[0], args[2], const_args[2], label_next);
969 tcg_out_brcond(s, TCG_COND_EQ, args[1], args[3], const_args[3], args[5]);
972 tcg_out_brcond(s, TCG_COND_NE, args[0], args[2], const_args[2], args[5]);
973 tcg_out_brcond(s, TCG_COND_NE, args[1], args[3], const_args[3], args[5]);
976 tcg_out_brcond(s, TCG_COND_LT, args[1], args[3], const_args[3], args[5]);
977 tcg_out_brcond(s, TCG_COND_NE, args[1], args[3], const_args[3], label_next);
978 tcg_out_brcond(s, TCG_COND_LT, args[0], args[2], const_args[2], args[5]);
981 tcg_out_brcond(s, TCG_COND_LT, args[1], args[3], const_args[3], args[5]);
982 tcg_out_brcond(s, TCG_COND_NE, args[1], args[3], const_args[3], label_next);
983 tcg_out_brcond(s, TCG_COND_LE, args[0], args[2], const_args[2], args[5]);
986 tcg_out_brcond(s, TCG_COND_GT, args[1], args[3], const_args[3], args[5]);
987 tcg_out_brcond(s, TCG_COND_NE, args[1], args[3], const_args[3], label_next);
988 tcg_out_brcond(s, TCG_COND_GT, args[0], args[2], const_args[2], args[5]);
991 tcg_out_brcond(s, TCG_COND_GT, args[1], args[3], const_args[3], args[5]);
992 tcg_out_brcond(s, TCG_COND_NE, args[1], args[3], const_args[3], label_next);
993 tcg_out_brcond(s, TCG_COND_GE, args[0], args[2], const_args[2], args[5]);
996 tcg_out_brcond(s, TCG_COND_LTU, args[1], args[3], const_args[3], args[5]);
997 tcg_out_brcond(s, TCG_COND_NE, args[1], args[3], const_args[3], label_next);
998 tcg_out_brcond(s, TCG_COND_LTU, args[0], args[2], const_args[2], args[5]);
1001 tcg_out_brcond(s, TCG_COND_LTU, args[1], args[3], const_args[3], args[5]);
1002 tcg_out_brcond(s, TCG_COND_NE, args[1], args[3], const_args[3], label_next);
1003 tcg_out_brcond(s, TCG_COND_LEU, args[0], args[2], const_args[2], args[5]);
1006 tcg_out_brcond(s, TCG_COND_GTU, args[1], args[3], const_args[3], args[5]);
1007 tcg_out_brcond(s, TCG_COND_NE, args[1], args[3], const_args[3], label_next);
1008 tcg_out_brcond(s, TCG_COND_GTU, args[0], args[2], const_args[2], args[5]);
1011 tcg_out_brcond(s, TCG_COND_GTU, args[1], args[3], const_args[3], args[5]);
1012 tcg_out_brcond(s, TCG_COND_NE, args[1], args[3], const_args[3], label_next);
1013 tcg_out_brcond(s, TCG_COND_GEU, args[0], args[2], const_args[2], args[5]);
1018 tcg_out_label(s, label_next, (tcg_target_long)s->code_ptr);
1021 static uint64_t __attribute ((used)) ppc_udiv_helper (uint64_t a, uint32_t b)
1026 return (rem << 32) | (uint32_t) quo;
1029 static uint64_t __attribute ((used)) ppc_div_helper (int64_t a, int32_t b)
1034 return (rem << 32) | (uint32_t) quo;
1037 #define MAKE_TRAMPOLINE(name) \
1038 extern void name##_trampoline (void); \
1039 asm (#name "_trampoline:\n" \
1041 " addi 1,1,-112\n" \
1045 " bl ppc_" #name "_helper\n" \
1053 MAKE_TRAMPOLINE (div);
1054 MAKE_TRAMPOLINE (udiv);
1056 static void tcg_out_div2 (TCGContext *s, int uns)
1058 void *label1_ptr, *label2_ptr;
1061 tcg_out32 (s, CMPLI | BF (7) | RA (3));
1063 tcg_out32 (s, SRAWI | RS (4) | RA (0) | 31);
1064 tcg_out32 (s, CMPL | BF (7) | RA (3) | RB (4));
1067 label1_ptr = s->code_ptr;
1068 tcg_out32 (s, BC | BI (7, CR_EQ) | BO_COND_TRUE);
1070 tcg_out_b (s, LK, (tcg_target_long) (uns ? udiv_trampoline : div_trampoline));
1072 label2_ptr = s->code_ptr;
1075 reloc_pc14 (label1_ptr, (tcg_target_long) s->code_ptr);
1077 tcg_out32 (s, (uns ? DIVWU : DIVW) | TAB (6, 4, 5));
1078 tcg_out32 (s, MULLW | TAB (0, 6, 5));
1079 tcg_out32 (s, SUBF | TAB (3, 0, 4));
1081 reloc_pc24 (label2_ptr, (tcg_target_long) s->code_ptr);
1084 static void tcg_out_op(TCGContext *s, int opc, const TCGArg *args,
1085 const int *const_args)
1088 case INDEX_op_exit_tb:
1089 tcg_out_movi (s, TCG_TYPE_I32, TCG_REG_R3, args[0]);
1090 tcg_out_b (s, 0, (tcg_target_long) tb_ret_addr);
1092 case INDEX_op_goto_tb:
1093 if (s->tb_jmp_offset) {
1094 /* direct jump method */
1096 s->tb_jmp_offset[args[0]] = s->code_ptr - s->code_buf;
1102 s->tb_next_offset[args[0]] = s->code_ptr - s->code_buf;
1106 TCGLabel *l = &s->labels[args[0]];
1109 tcg_out_b (s, 0, l->u.value);
1112 uint32_t val = *(uint32_t *) s->code_ptr;
1114 /* Thanks to Andrzej Zaborowski */
1115 tcg_out32 (s, B | (val & 0x3fffffc));
1116 tcg_out_reloc (s, s->code_ptr - 4, R_PPC_REL24, args[0], 0);
1121 if (const_args[0]) {
1122 tcg_out_b (s, LK, args[0]);
1125 tcg_out32 (s, MTSPR | RS (args[0]) | LR);
1126 tcg_out32 (s, BCLR | BO_ALWAYS | LK);
1130 if (const_args[0]) {
1131 tcg_out_b (s, 0, args[0]);
1134 tcg_out32 (s, MTSPR | RS (args[0]) | CTR);
1135 tcg_out32 (s, BCCTR | BO_ALWAYS);
1138 case INDEX_op_movi_i32:
1139 tcg_out_movi(s, TCG_TYPE_I32, args[0], args[1]);
1141 case INDEX_op_ld8u_i32:
1142 tcg_out_ldst (s, args[0], args[1], args[2], LBZ, LBZX);
1144 case INDEX_op_ld8s_i32:
1145 tcg_out_ldst (s, args[0], args[1], args[2], LBZ, LBZX);
1146 tcg_out32 (s, EXTSB | RS (args[0]) | RA (args[0]));
1148 case INDEX_op_ld16u_i32:
1149 tcg_out_ldst (s, args[0], args[1], args[2], LHZ, LHZX);
1151 case INDEX_op_ld16s_i32:
1152 tcg_out_ldst (s, args[0], args[1], args[2], LHA, LHAX);
1154 case INDEX_op_ld_i32:
1155 tcg_out_ldst (s, args[0], args[1], args[2], LWZ, LWZX);
1157 case INDEX_op_st8_i32:
1158 tcg_out_ldst (s, args[0], args[1], args[2], STB, STBX);
1160 case INDEX_op_st16_i32:
1161 tcg_out_ldst (s, args[0], args[1], args[2], STH, STHX);
1163 case INDEX_op_st_i32:
1164 tcg_out_ldst (s, args[0], args[1], args[2], STW, STWX);
1167 case INDEX_op_add_i32:
1169 ppc_addi (s, args[0], args[1], args[2]);
1171 tcg_out32 (s, ADD | TAB (args[0], args[1], args[2]));
1173 case INDEX_op_sub_i32:
1175 ppc_addi (s, args[0], args[1], -args[2]);
1177 tcg_out32 (s, SUBF | TAB (args[0], args[2], args[1]));
1180 case INDEX_op_and_i32:
1181 if (const_args[2]) {
1183 tcg_out_movi (s, TCG_TYPE_I32, args[0], 0);
1185 if ((args[2] & 0xffff) == args[2])
1186 tcg_out32 (s, ANDI | RS (args[1]) | RA (args[0]) | args[2]);
1187 else if ((args[2] & 0xffff0000) == args[2])
1188 tcg_out32 (s, ANDIS | RS (args[1]) | RA (args[0])
1189 | ((args[2] >> 16) & 0xffff));
1190 else if (args[2] == 0xffffffff) {
1191 if (args[0] != args[1])
1192 tcg_out_mov (s, args[0], args[1]);
1195 tcg_out_movi (s, TCG_TYPE_I32, 0, args[2]);
1196 tcg_out32 (s, AND | SAB (args[1], args[0], 0));
1201 tcg_out32 (s, AND | SAB (args[1], args[0], args[2]));
1203 case INDEX_op_or_i32:
1204 if (const_args[2]) {
1206 if (args[2] & 0xffff) {
1207 tcg_out32 (s, ORI | RS (args[1]) | RA (args[0])
1208 | (args[2] & 0xffff));
1210 tcg_out32 (s, ORIS | RS (args[0]) | RA (args[0])
1211 | ((args[2] >> 16) & 0xffff));
1214 tcg_out32 (s, ORIS | RS (args[1]) | RA (args[0])
1215 | ((args[2] >> 16) & 0xffff));
1219 if (args[0] != args[1])
1220 tcg_out_mov (s, args[0], args[1]);
1224 tcg_out32 (s, OR | SAB (args[1], args[0], args[2]));
1226 case INDEX_op_xor_i32:
1227 if (const_args[2]) {
1229 if ((args[2] & 0xffff) == args[2])
1230 tcg_out32 (s, XORI | RS (args[1]) | RA (args[0])
1231 | (args[2] & 0xffff));
1232 else if ((args[2] & 0xffff0000) == args[2])
1233 tcg_out32 (s, XORIS | RS (args[1]) | RA (args[0])
1234 | ((args[2] >> 16) & 0xffff));
1236 tcg_out_movi (s, TCG_TYPE_I32, 0, args[2]);
1237 tcg_out32 (s, XOR | SAB (args[1], args[0], 0));
1241 if (args[0] != args[1])
1242 tcg_out_mov (s, args[0], args[1]);
1246 tcg_out32 (s, XOR | SAB (args[1], args[0], args[2]));
1249 case INDEX_op_mul_i32:
1250 if (const_args[2]) {
1251 if (args[2] == (int16_t) args[2])
1252 tcg_out32 (s, MULLI | RT (args[0]) | RA (args[1])
1253 | (args[2] & 0xffff));
1255 tcg_out_movi (s, TCG_TYPE_I32, 0, args[2]);
1256 tcg_out32 (s, MULLW | TAB (args[0], args[1], 0));
1260 tcg_out32 (s, MULLW | TAB (args[0], args[1], args[2]));
1262 case INDEX_op_mulu2_i32:
1263 if (args[0] == args[2] || args[0] == args[3]) {
1264 tcg_out32 (s, MULLW | TAB (0, args[2], args[3]));
1265 tcg_out32 (s, MULHWU | TAB (args[1], args[2], args[3]));
1266 tcg_out_mov (s, args[0], 0);
1269 tcg_out32 (s, MULLW | TAB (args[0], args[2], args[3]));
1270 tcg_out32 (s, MULHWU | TAB (args[1], args[2], args[3]));
1273 case INDEX_op_div2_i32:
1274 tcg_out_div2 (s, 0);
1276 case INDEX_op_divu2_i32:
1277 tcg_out_div2 (s, 1);
1280 case INDEX_op_shl_i32:
1281 if (const_args[2]) {
1283 tcg_out32 (s, (RLWINM
1292 tcg_out_mov (s, args[0], args[1]);
1295 tcg_out32 (s, SLW | SAB (args[1], args[0], args[2]));
1297 case INDEX_op_shr_i32:
1298 if (const_args[2]) {
1300 tcg_out32 (s, (RLWINM
1309 tcg_out_mov (s, args[0], args[1]);
1312 tcg_out32 (s, SRW | SAB (args[1], args[0], args[2]));
1314 case INDEX_op_sar_i32:
1316 tcg_out32 (s, SRAWI | RS (args[1]) | RA (args[0]) | SH (args[2]));
1318 tcg_out32 (s, SRAW | SAB (args[1], args[0], args[2]));
1321 case INDEX_op_add2_i32:
1322 if (args[0] == args[3] || args[0] == args[5]) {
1323 tcg_out32 (s, ADDC | TAB (0, args[2], args[4]));
1324 tcg_out32 (s, ADDE | TAB (args[1], args[3], args[5]));
1325 tcg_out_mov (s, args[0], 0);
1328 tcg_out32 (s, ADDC | TAB (args[0], args[2], args[4]));
1329 tcg_out32 (s, ADDE | TAB (args[1], args[3], args[5]));
1332 case INDEX_op_sub2_i32:
1333 if (args[0] == args[3] || args[0] == args[5]) {
1334 tcg_out32 (s, SUBFC | TAB (0, args[4], args[2]));
1335 tcg_out32 (s, SUBFE | TAB (args[1], args[5], args[3]));
1336 tcg_out_mov (s, args[0], 0);
1339 tcg_out32 (s, SUBFC | TAB (args[0], args[4], args[2]));
1340 tcg_out32 (s, SUBFE | TAB (args[1], args[5], args[3]));
1344 case INDEX_op_brcond_i32:
1349 args[3] = r1 is const
1350 args[4] = label_index
1352 tcg_out_brcond (s, args[2], args[0], args[1], const_args[1], args[3]);
1354 case INDEX_op_brcond2_i32:
1355 tcg_out_brcond2(s, args, const_args);
1358 case INDEX_op_neg_i32:
1359 tcg_out32 (s, NEG | RT (args[0]) | RA (args[1]));
1362 case INDEX_op_qemu_ld8u:
1363 tcg_out_qemu_ld(s, args, 0);
1365 case INDEX_op_qemu_ld8s:
1366 tcg_out_qemu_ld(s, args, 0 | 4);
1368 case INDEX_op_qemu_ld16u:
1369 tcg_out_qemu_ld(s, args, 1);
1371 case INDEX_op_qemu_ld16s:
1372 tcg_out_qemu_ld(s, args, 1 | 4);
1374 case INDEX_op_qemu_ld32u:
1375 tcg_out_qemu_ld(s, args, 2);
1377 case INDEX_op_qemu_ld64:
1378 tcg_out_qemu_ld(s, args, 3);
1380 case INDEX_op_qemu_st8:
1381 tcg_out_qemu_st(s, args, 0);
1383 case INDEX_op_qemu_st16:
1384 tcg_out_qemu_st(s, args, 1);
1386 case INDEX_op_qemu_st32:
1387 tcg_out_qemu_st(s, args, 2);
1389 case INDEX_op_qemu_st64:
1390 tcg_out_qemu_st(s, args, 3);
1394 tcg_dump_ops (s, stderr);
1399 static const TCGTargetOpDef ppc_op_defs[] = {
1400 { INDEX_op_exit_tb, { } },
1401 { INDEX_op_goto_tb, { } },
1402 { INDEX_op_call, { "ri" } },
1403 { INDEX_op_jmp, { "ri" } },
1404 { INDEX_op_br, { } },
1406 { INDEX_op_mov_i32, { "r", "r" } },
1407 { INDEX_op_movi_i32, { "r" } },
1408 { INDEX_op_ld8u_i32, { "r", "r" } },
1409 { INDEX_op_ld8s_i32, { "r", "r" } },
1410 { INDEX_op_ld16u_i32, { "r", "r" } },
1411 { INDEX_op_ld16s_i32, { "r", "r" } },
1412 { INDEX_op_ld_i32, { "r", "r" } },
1413 { INDEX_op_st8_i32, { "r", "r" } },
1414 { INDEX_op_st16_i32, { "r", "r" } },
1415 { INDEX_op_st_i32, { "r", "r" } },
1417 { INDEX_op_add_i32, { "r", "r", "ri" } },
1418 { INDEX_op_mul_i32, { "r", "r", "ri" } },
1419 { INDEX_op_mulu2_i32, { "r", "r", "r", "r" } },
1420 { INDEX_op_div2_i32, { "D", "A", "B", "1", "C" } },
1421 { INDEX_op_divu2_i32, { "D", "A", "B", "1", "C" } },
1422 { INDEX_op_sub_i32, { "r", "r", "ri" } },
1423 { INDEX_op_and_i32, { "r", "r", "ri" } },
1424 { INDEX_op_or_i32, { "r", "r", "ri" } },
1425 { INDEX_op_xor_i32, { "r", "r", "ri" } },
1427 { INDEX_op_shl_i32, { "r", "r", "ri" } },
1428 { INDEX_op_shr_i32, { "r", "r", "ri" } },
1429 { INDEX_op_sar_i32, { "r", "r", "ri" } },
1431 { INDEX_op_brcond_i32, { "r", "ri" } },
1433 { INDEX_op_add2_i32, { "r", "r", "r", "r", "r", "r" } },
1434 { INDEX_op_sub2_i32, { "r", "r", "r", "r", "r", "r" } },
1435 { INDEX_op_brcond2_i32, { "r", "r", "r", "r" } },
1437 { INDEX_op_neg_i32, { "r", "r" } },
1439 #if TARGET_LONG_BITS == 32
1440 { INDEX_op_qemu_ld8u, { "r", "L" } },
1441 { INDEX_op_qemu_ld8s, { "r", "L" } },
1442 { INDEX_op_qemu_ld16u, { "r", "L" } },
1443 { INDEX_op_qemu_ld16s, { "r", "L" } },
1444 { INDEX_op_qemu_ld32u, { "r", "L" } },
1445 { INDEX_op_qemu_ld32s, { "r", "L" } },
1446 { INDEX_op_qemu_ld64, { "r", "r", "L" } },
1448 { INDEX_op_qemu_st8, { "K", "K" } },
1449 { INDEX_op_qemu_st16, { "K", "K" } },
1450 { INDEX_op_qemu_st32, { "K", "K" } },
1451 { INDEX_op_qemu_st64, { "M", "M", "M" } },
1453 { INDEX_op_qemu_ld8u, { "r", "L", "L" } },
1454 { INDEX_op_qemu_ld8s, { "r", "L", "L" } },
1455 { INDEX_op_qemu_ld16u, { "r", "L", "L" } },
1456 { INDEX_op_qemu_ld16s, { "r", "L", "L" } },
1457 { INDEX_op_qemu_ld32u, { "r", "L", "L" } },
1458 { INDEX_op_qemu_ld32s, { "r", "L", "L" } },
1459 { INDEX_op_qemu_ld64, { "r", "L", "L", "L" } },
1461 { INDEX_op_qemu_st8, { "K", "K", "K" } },
1462 { INDEX_op_qemu_st16, { "K", "K", "K" } },
1463 { INDEX_op_qemu_st32, { "K", "K", "K" } },
1464 { INDEX_op_qemu_st64, { "M", "M", "M", "M" } },
1470 void tcg_target_init(TCGContext *s)
1472 tcg_regset_set32(tcg_target_available_regs[TCG_TYPE_I32], 0, 0xffffffff);
1473 tcg_regset_set32(tcg_target_call_clobber_regs, 0,
1482 (1 << TCG_REG_R10) |
1483 (1 << TCG_REG_R11) |
1487 tcg_regset_clear(s->reserved_regs);
1488 tcg_regset_set_reg(s->reserved_regs, TCG_REG_R0);
1489 tcg_regset_set_reg(s->reserved_regs, TCG_REG_R1);
1490 tcg_regset_set_reg(s->reserved_regs, TCG_REG_R2);
1492 tcg_add_target_add_op_defs(ppc_op_defs);