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 #define TCG_CT_CONST_U32 0x100
27 static uint8_t *tb_ret_addr;
31 #if TARGET_PHYS_ADDR_BITS == 32
37 #if TARGET_LONG_BITS == 32
46 static const char * const tcg_target_reg_names[TCG_TARGET_NB_REGS] = {
82 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] = {
132 static const int tcg_target_callee_save_regs[] = {
146 /* TCG_REG_R27, */ /* currently used for the global env, so no
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 << 38) >> 38 != 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 ARRAY_SIZE (tcg_target_call_iarg_regs);
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 case 'L': /* qemu_ld 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 #ifdef CONFIG_SOFTMMU
230 tcg_regset_reset_reg (ct->u.regs, TCG_REG_R4);
233 case 'S': /* qemu_st constraint */
234 ct->ct |= TCG_CT_REG;
235 tcg_regset_set32 (ct->u.regs, 0, 0xffffffff);
236 tcg_regset_reset_reg (ct->u.regs, TCG_REG_R3);
237 #ifdef CONFIG_SOFTMMU
238 tcg_regset_reset_reg (ct->u.regs, TCG_REG_R4);
239 tcg_regset_reset_reg (ct->u.regs, TCG_REG_R5);
243 ct->ct |= TCG_CT_CONST_U32;
253 /* test if a constant matches the constraint */
254 static int tcg_target_const_match (tcg_target_long val,
255 const TCGArgConstraint *arg_ct)
260 if (ct & TCG_CT_CONST)
262 else if ((ct & TCG_CT_CONST_U32) && (val == (uint32_t) val))
267 #define OPCD(opc) ((opc)<<26)
268 #define XO19(opc) (OPCD(19)|((opc)<<1))
269 #define XO30(opc) (OPCD(30)|((opc)<<2))
270 #define XO31(opc) (OPCD(31)|((opc)<<1))
271 #define XO58(opc) (OPCD(58)|(opc))
272 #define XO62(opc) (OPCD(62)|(opc))
276 #define LBZ OPCD( 34)
277 #define LHZ OPCD( 40)
278 #define LHA OPCD( 42)
279 #define LWZ OPCD( 32)
280 #define STB OPCD( 38)
281 #define STH OPCD( 44)
282 #define STW OPCD( 36)
285 #define STDU XO62( 1)
286 #define STDX XO31(149)
289 #define LDX XO31( 21)
292 #define LWAX XO31(341)
294 #define ADDI OPCD( 14)
295 #define ADDIS OPCD( 15)
296 #define ORI OPCD( 24)
297 #define ORIS OPCD( 25)
298 #define XORI OPCD( 26)
299 #define XORIS OPCD( 27)
300 #define ANDI OPCD( 28)
301 #define ANDIS OPCD( 29)
302 #define MULLI OPCD( 7)
303 #define CMPLI OPCD( 10)
304 #define CMPI OPCD( 11)
306 #define LWZU OPCD( 33)
307 #define STWU OPCD( 37)
309 #define RLWINM OPCD( 21)
311 #define RLDICL XO30( 0)
312 #define RLDICR XO30( 1)
313 #define RLDIMI XO30( 3)
315 #define BCLR XO19( 16)
316 #define BCCTR XO19(528)
317 #define CRAND XO19(257)
318 #define CRANDC XO19(129)
319 #define CRNAND XO19(225)
320 #define CROR XO19(449)
322 #define EXTSB XO31(954)
323 #define EXTSH XO31(922)
324 #define EXTSW XO31(986)
325 #define ADD XO31(266)
326 #define ADDE XO31(138)
327 #define ADDC XO31( 10)
328 #define AND XO31( 28)
329 #define SUBF XO31( 40)
330 #define SUBFC XO31( 8)
331 #define SUBFE XO31(136)
333 #define XOR XO31(316)
334 #define MULLW XO31(235)
335 #define MULHWU XO31( 11)
336 #define DIVW XO31(491)
337 #define DIVWU XO31(459)
339 #define CMPL XO31( 32)
340 #define LHBRX XO31(790)
341 #define LWBRX XO31(534)
342 #define STHBRX XO31(918)
343 #define STWBRX XO31(662)
344 #define MFSPR XO31(339)
345 #define MTSPR XO31(467)
346 #define SRAWI XO31(824)
347 #define NEG XO31(104)
349 #define MULLD XO31(233)
350 #define MULHD XO31( 73)
351 #define MULHDU XO31( 9)
352 #define DIVD XO31(489)
353 #define DIVDU XO31(457)
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)
371 #define SLD XO31( 27)
372 #define SRD XO31(539)
373 #define SRAD XO31(794)
374 #define SRADI XO31(413<<1)
376 #define LMW OPCD( 46)
377 #define STMW OPCD( 47)
380 #define TRAP (TW | TO (31))
382 #define RT(r) ((r)<<21)
383 #define RS(r) ((r)<<21)
384 #define RA(r) ((r)<<16)
385 #define RB(r) ((r)<<11)
386 #define TO(t) ((t)<<21)
387 #define SH(s) ((s)<<11)
388 #define MB(b) ((b)<<6)
389 #define ME(e) ((e)<<1)
390 #define BO(o) ((o)<<21)
391 #define MB64(b) ((b)<<5)
395 #define TAB(t,a,b) (RT(t) | RA(a) | RB(b))
396 #define SAB(s,a,b) (RS(s) | RA(a) | RB(b))
398 #define BF(n) ((n)<<23)
399 #define BI(n, c) (((c)+((n)*4))<<16)
400 #define BT(n, c) (((c)+((n)*4))<<21)
401 #define BA(n, c) (((c)+((n)*4))<<16)
402 #define BB(n, c) (((c)+((n)*4))<<11)
404 #define BO_COND_TRUE BO (12)
405 #define BO_COND_FALSE BO ( 4)
406 #define BO_ALWAYS BO (20)
415 static const uint32_t tcg_to_bc[10] = {
416 [TCG_COND_EQ] = BC | BI (7, CR_EQ) | BO_COND_TRUE,
417 [TCG_COND_NE] = BC | BI (7, CR_EQ) | BO_COND_FALSE,
418 [TCG_COND_LT] = BC | BI (7, CR_LT) | BO_COND_TRUE,
419 [TCG_COND_GE] = BC | BI (7, CR_LT) | BO_COND_FALSE,
420 [TCG_COND_LE] = BC | BI (7, CR_GT) | BO_COND_FALSE,
421 [TCG_COND_GT] = BC | BI (7, CR_GT) | BO_COND_TRUE,
422 [TCG_COND_LTU] = BC | BI (7, CR_LT) | BO_COND_TRUE,
423 [TCG_COND_GEU] = BC | BI (7, CR_LT) | BO_COND_FALSE,
424 [TCG_COND_LEU] = BC | BI (7, CR_GT) | BO_COND_FALSE,
425 [TCG_COND_GTU] = BC | BI (7, CR_GT) | BO_COND_TRUE,
428 static void tcg_out_mov (TCGContext *s, int ret, int arg)
430 tcg_out32 (s, OR | SAB (arg, ret, arg));
433 static void tcg_out_rld (TCGContext *s, int op, int ra, int rs, int sh, int mb)
435 sh = SH (sh & 0x1f) | (((sh >> 5) & 1) << 1);
436 mb = MB64 ((mb >> 5) | ((mb << 1) & 0x3f));
437 tcg_out32 (s, op | RA (ra) | RS (rs) | sh | mb);
440 static void tcg_out_movi32 (TCGContext *s, int ret, int32_t arg)
442 if (arg == (int16_t) arg)
443 tcg_out32 (s, ADDI | RT (ret) | RA (0) | (arg & 0xffff));
445 tcg_out32 (s, ADDIS | RT (ret) | RA (0) | ((arg >> 16) & 0xffff));
447 tcg_out32 (s, ORI | RS (ret) | RA (ret) | (arg & 0xffff));
451 static void tcg_out_movi (TCGContext *s, TCGType type,
452 int ret, tcg_target_long arg)
456 if (type == TCG_TYPE_I32 || arg == arg32) {
457 tcg_out_movi32 (s, ret, arg32);
460 if ((uint64_t) arg >> 32) {
461 uint16_t h16 = arg >> 16;
464 tcg_out_movi32 (s, ret, arg >> 32);
465 tcg_out_rld (s, RLDICR, ret, ret, 32, 31);
466 if (h16) tcg_out32 (s, ORIS | RS (ret) | RA (ret) | h16);
467 if (l16) tcg_out32 (s, ORI | RS (ret) | RA (ret) | l16);
470 tcg_out_movi32 (s, ret, arg32);
472 tcg_out_rld (s, RLDICL, ret, ret, 0, 32);
477 static void tcg_out_call (TCGContext *s, tcg_target_long arg, int const_arg)
483 tcg_out_movi (s, TCG_TYPE_I64, reg, arg);
487 tcg_out32 (s, LD | RT (0) | RA (reg));
488 tcg_out32 (s, MTSPR | RA (0) | CTR);
489 tcg_out32 (s, LD | RT (11) | RA (reg) | 16);
490 tcg_out32 (s, LD | RT (2) | RA (reg) | 8);
491 tcg_out32 (s, BCCTR | BO_ALWAYS | LK);
494 static void tcg_out_ldst (TCGContext *s, int ret, int addr,
495 int offset, int op1, int op2)
497 if (offset == (int16_t) offset)
498 tcg_out32 (s, op1 | RT (ret) | RA (addr) | (offset & 0xffff));
500 tcg_out_movi (s, TCG_TYPE_I64, 0, offset);
501 tcg_out32 (s, op2 | RT (ret) | RA (addr) | RB (0));
505 static void tcg_out_ldsta (TCGContext *s, int ret, int addr,
506 int offset, int op1, int op2)
508 if (offset == (int16_t) (offset & ~3))
509 tcg_out32 (s, op1 | RT (ret) | RA (addr) | (offset & 0xffff));
511 tcg_out_movi (s, TCG_TYPE_I64, 0, offset);
512 tcg_out32 (s, op2 | RT (ret) | RA (addr) | RB (0));
516 static void tcg_out_b (TCGContext *s, int mask, tcg_target_long target)
518 tcg_target_long disp;
520 disp = target - (tcg_target_long) s->code_ptr;
521 if ((disp << 38) >> 38 == disp)
522 tcg_out32 (s, B | (disp & 0x3fffffc) | mask);
524 tcg_out_movi (s, TCG_TYPE_I64, 0, (tcg_target_long) target);
525 tcg_out32 (s, MTSPR | RS (0) | CTR);
526 tcg_out32 (s, BCCTR | BO_ALWAYS | mask);
530 #if defined (CONFIG_SOFTMMU)
532 #include "../../softmmu_defs.h"
534 static void *qemu_ld_helpers[4] = {
541 static void *qemu_st_helpers[4] = {
548 static void tcg_out_tlb_read (TCGContext *s, int r0, int r1, int r2,
549 int addr_reg, int s_bits, int offset)
551 #if TARGET_LONG_BITS == 32
552 tcg_out_rld (s, RLDICL, addr_reg, addr_reg, 0, 32);
554 tcg_out32 (s, (RLWINM
557 | SH (32 - (TARGET_PAGE_BITS - CPU_TLB_ENTRY_BITS))
558 | MB (32 - (CPU_TLB_BITS + CPU_TLB_ENTRY_BITS))
559 | ME (31 - CPU_TLB_ENTRY_BITS)
562 tcg_out32 (s, ADD | RT (r0) | RA (r0) | RB (TCG_AREG0));
563 tcg_out32 (s, (LWZU | RT (r1) | RA (r0) | offset));
564 tcg_out32 (s, (RLWINM
568 | MB ((32 - s_bits) & 31)
569 | ME (31 - TARGET_PAGE_BITS)
573 tcg_out_rld (s, RLDICL, r0, addr_reg,
574 64 - TARGET_PAGE_BITS,
576 tcg_out_rld (s, RLDICR, r0, r0,
578 63 - CPU_TLB_ENTRY_BITS);
580 tcg_out32 (s, ADD | TAB (r0, r0, TCG_AREG0));
581 tcg_out32 (s, LD_ADDR | RT (r1) | RA (r0) | offset);
584 tcg_out_rld (s, RLDICR, r2, addr_reg, 0, 63 - TARGET_PAGE_BITS);
587 tcg_out_rld (s, RLDICL, r2, addr_reg,
588 64 - TARGET_PAGE_BITS,
589 TARGET_PAGE_BITS - s_bits);
590 tcg_out_rld (s, RLDICL, r2, r2, TARGET_PAGE_BITS, 0);
596 static void tcg_out_qemu_ld (TCGContext *s, const TCGArg *args, int opc)
598 int addr_reg, data_reg, r0, r1, mem_index, s_bits, bswap;
599 #ifdef CONFIG_SOFTMMU
601 void *label1_ptr, *label2_ptr;
609 #ifdef CONFIG_SOFTMMU
614 tcg_out_tlb_read (s, r0, r1, r2, addr_reg, s_bits,
615 offsetof (CPUState, tlb_table[mem_index][0].addr_read));
617 tcg_out32 (s, CMP | BF (7) | RA (r2) | RB (r1) | CMP_L);
619 label1_ptr = s->code_ptr;
621 tcg_out32 (s, BC | BI (7, CR_EQ) | BO_COND_TRUE);
625 tcg_out_mov (s, 3, addr_reg);
626 tcg_out_movi (s, TCG_TYPE_I64, 4, mem_index);
628 tcg_out_call (s, (tcg_target_long) qemu_ld_helpers[s_bits], 1);
632 tcg_out32 (s, EXTSB | RA (data_reg) | RS (3));
635 tcg_out32 (s, EXTSH | RA (data_reg) | RS (3));
638 tcg_out32 (s, EXTSW | RA (data_reg) | RS (3));
645 tcg_out_mov (s, data_reg, 3);
648 label2_ptr = s->code_ptr;
651 /* label1: fast path */
653 reloc_pc14 (label1_ptr, (tcg_target_long) s->code_ptr);
656 /* r0 now contains &env->tlb_table[mem_index][index].addr_read */
657 tcg_out32 (s, (LD_ADDEND
660 | (offsetof (CPUTLBEntry, addend)
661 - offsetof (CPUTLBEntry, addr_read))
663 /* r0 = env->tlb_table[mem_index][index].addend */
664 tcg_out32 (s, ADD | RT (r0) | RA (r0) | RB (addr_reg));
665 /* r0 = env->tlb_table[mem_index][index].addend + addr */
667 #else /* !CONFIG_SOFTMMU */
668 #if TARGET_LONG_BITS == 32
669 tcg_out_rld (s, RLDICL, addr_reg, addr_reg, 0, 32);
675 #ifdef TARGET_WORDS_BIGENDIAN
683 tcg_out32 (s, LBZ | RT (data_reg) | RA (r0));
686 tcg_out32 (s, LBZ | RT (data_reg) | RA (r0));
687 tcg_out32 (s, EXTSB | RA (data_reg) | RS (data_reg));
690 if (bswap) tcg_out32 (s, LHBRX | RT (data_reg) | RB (r0));
691 else tcg_out32 (s, LHZ | RT (data_reg) | RA (r0));
695 tcg_out32 (s, LHBRX | RT (data_reg) | RB (r0));
696 tcg_out32 (s, EXTSH | RA (data_reg) | RS (data_reg));
698 else tcg_out32 (s, LHA | RT (data_reg) | RA (r0));
701 if (bswap) tcg_out32 (s, LWBRX | RT (data_reg) | RB (r0));
702 else tcg_out32 (s, LWZ | RT (data_reg)| RA (r0));
706 tcg_out32 (s, LWBRX | RT (data_reg) | RB (r0));
707 tcg_out32 (s, EXTSW | RA (data_reg) | RS (data_reg));
709 else tcg_out32 (s, LWA | RT (data_reg)| RA (r0));
713 tcg_out_movi32 (s, 0, 4);
714 tcg_out32 (s, LWBRX | RT (data_reg) | RB (r0));
715 tcg_out32 (s, LWBRX | RT ( r1) | RA (r0));
716 tcg_out_rld (s, RLDIMI, data_reg, r1, 32, 0);
718 else tcg_out32 (s, LD | RT (data_reg) | RA (r0));
722 #ifdef CONFIG_SOFTMMU
723 reloc_pc24 (label2_ptr, (tcg_target_long) s->code_ptr);
727 static void tcg_out_qemu_st (TCGContext *s, const TCGArg *args, int opc)
729 int addr_reg, r0, r1, data_reg, mem_index, bswap;
730 #ifdef CONFIG_SOFTMMU
732 void *label1_ptr, *label2_ptr;
739 #ifdef CONFIG_SOFTMMU
744 tcg_out_tlb_read (s, r0, r1, r2, addr_reg, opc,
745 offsetof (CPUState, tlb_table[mem_index][0].addr_write));
747 tcg_out32 (s, CMP | BF (7) | RA (r2) | RB (r1) | CMP_L);
749 label1_ptr = s->code_ptr;
751 tcg_out32 (s, BC | BI (7, CR_EQ) | BO_COND_TRUE);
755 tcg_out_mov (s, 3, addr_reg);
756 tcg_out_rld (s, RLDICL, 4, data_reg, 0, 64 - (1 << (3 + opc)));
757 tcg_out_movi (s, TCG_TYPE_I64, 5, mem_index);
759 tcg_out_call (s, (tcg_target_long) qemu_st_helpers[opc], 1);
761 label2_ptr = s->code_ptr;
764 /* label1: fast path */
766 reloc_pc14 (label1_ptr, (tcg_target_long) s->code_ptr);
769 tcg_out32 (s, (LD_ADDEND
772 | (offsetof (CPUTLBEntry, addend)
773 - offsetof (CPUTLBEntry, addr_write))
775 /* r0 = env->tlb_table[mem_index][index].addend */
776 tcg_out32 (s, ADD | RT (r0) | RA (r0) | RB (addr_reg));
777 /* r0 = env->tlb_table[mem_index][index].addend + addr */
779 #else /* !CONFIG_SOFTMMU */
780 #if TARGET_LONG_BITS == 32
781 tcg_out_rld (s, RLDICL, addr_reg, addr_reg, 0, 32);
787 #ifdef TARGET_WORDS_BIGENDIAN
794 tcg_out32 (s, STB | RS (data_reg) | RA (r0));
797 if (bswap) tcg_out32 (s, STHBRX | RS (data_reg) | RA (0) | RB (r0));
798 else tcg_out32 (s, STH | RS (data_reg) | RA (r0));
801 if (bswap) tcg_out32 (s, STWBRX | RS (data_reg) | RA (0) | RB (r0));
802 else tcg_out32 (s, STW | RS (data_reg) | RA (r0));
806 tcg_out32 (s, STWBRX | RS (data_reg) | RA (0) | RB (r0));
807 tcg_out32 (s, ADDI | RT (r1) | RA (r0) | 4);
808 tcg_out_rld (s, RLDICL, 0, data_reg, 32, 0);
809 tcg_out32 (s, STWBRX | RS (0) | RA (0) | RB (r1));
811 else tcg_out32 (s, STD | RS (data_reg) | RA (r0));
815 #ifdef CONFIG_SOFTMMU
816 reloc_pc24 (label2_ptr, (tcg_target_long) s->code_ptr);
820 void tcg_target_qemu_prologue (TCGContext *s)
829 + 8 /* compiler doubleword */
830 + 8 /* link editor doubleword */
831 + 8 /* TOC save area */
832 + TCG_STATIC_CALL_ARGS_SIZE
833 + ARRAY_SIZE (tcg_target_callee_save_regs) * 8
835 frame_size = (frame_size + 15) & ~15;
837 /* First emit adhoc function descriptor */
838 addr = (uint64_t) s->code_ptr + 24;
839 tcg_out32 (s, addr >> 32); tcg_out32 (s, addr); /* entry point */
840 s->code_ptr += 16; /* skip TOC and environment pointer */
843 tcg_out32 (s, MFSPR | RT (0) | LR);
844 tcg_out32 (s, STDU | RS (1) | RA (1) | (-frame_size & 0xffff));
845 for (i = 0; i < ARRAY_SIZE (tcg_target_callee_save_regs); ++i)
847 | RS (tcg_target_callee_save_regs[i])
849 | (i * 8 + 48 + TCG_STATIC_CALL_ARGS_SIZE)
852 tcg_out32 (s, STD | RS (0) | RA (1) | (frame_size + 16));
854 tcg_out32 (s, MTSPR | RS (3) | CTR);
855 tcg_out32 (s, BCCTR | BO_ALWAYS);
858 tb_ret_addr = s->code_ptr;
860 for (i = 0; i < ARRAY_SIZE (tcg_target_callee_save_regs); ++i)
862 | RT (tcg_target_callee_save_regs[i])
864 | (i * 8 + 48 + TCG_STATIC_CALL_ARGS_SIZE)
867 tcg_out32 (s, LD | RT (0) | RA (1) | (frame_size + 16));
868 tcg_out32 (s, MTSPR | RS (0) | LR);
869 tcg_out32 (s, ADDI | RT (1) | RA (1) | frame_size);
870 tcg_out32 (s, BCLR | BO_ALWAYS);
873 static void tcg_out_ld (TCGContext *s, TCGType type, int ret, int arg1,
874 tcg_target_long arg2)
876 if (type == TCG_TYPE_I32)
877 tcg_out_ldst (s, ret, arg1, arg2, LWZ, LWZX);
879 tcg_out_ldsta (s, ret, arg1, arg2, LD, LDX);
882 static void tcg_out_st (TCGContext *s, TCGType type, int arg, int arg1,
883 tcg_target_long arg2)
885 if (type == TCG_TYPE_I32)
886 tcg_out_ldst (s, arg, arg1, arg2, STW, STWX);
888 tcg_out_ldsta (s, arg, arg1, arg2, STD, STDX);
891 static void ppc_addi32 (TCGContext *s, int rt, int ra, tcg_target_long si)
896 if (si == (int16_t) si)
897 tcg_out32 (s, ADDI | RT (rt) | RA (ra) | (si & 0xffff));
899 uint16_t h = ((si >> 16) & 0xffff) + ((uint16_t) si >> 15);
900 tcg_out32 (s, ADDIS | RT (rt) | RA (ra) | h);
901 tcg_out32 (s, ADDI | RT (rt) | RA (rt) | (si & 0xffff));
905 static void ppc_addi64 (TCGContext *s, int rt, int ra, tcg_target_long si)
907 /* XXX: suboptimal */
908 if (si == (int16_t) si
909 || ((((uint64_t) si >> 31) == 0) && (si & 0x8000) == 0))
910 ppc_addi32 (s, rt, ra, si);
912 tcg_out_movi (s, TCG_TYPE_I64, 0, si);
913 tcg_out32 (s, ADD | RT (rt) | RA (ra));
917 static void tcg_out_addi (TCGContext *s, int reg, tcg_target_long val)
919 ppc_addi64 (s, reg, reg, val);
922 static void tcg_out_cmp (TCGContext *s, int cond, TCGArg arg1, TCGArg arg2,
923 int const_arg2, int cr, int arch64)
932 if ((int16_t) arg2 == arg2) {
937 else if ((uint16_t) arg2 == arg2) {
952 if ((int16_t) arg2 == arg2) {
967 if ((uint16_t) arg2 == arg2) {
980 op |= BF (cr) | (arch64 << 21);
983 tcg_out32 (s, op | RA (arg1) | (arg2 & 0xffff));
986 tcg_out_movi (s, TCG_TYPE_I64, 0, arg2);
987 tcg_out32 (s, op | RA (arg1) | RB (0));
990 tcg_out32 (s, op | RA (arg1) | RB (arg2));
995 static void tcg_out_bc (TCGContext *s, int bc, int label_index)
997 TCGLabel *l = &s->labels[label_index];
1000 tcg_out32 (s, bc | reloc_pc14_val (s->code_ptr, l->u.value));
1002 uint16_t val = *(uint16_t *) &s->code_ptr[2];
1004 /* Thanks to Andrzej Zaborowski */
1005 tcg_out32 (s, bc | (val & 0xfffc));
1006 tcg_out_reloc (s, s->code_ptr - 4, R_PPC_REL14, label_index, 0);
1010 static void tcg_out_brcond (TCGContext *s, int cond,
1011 TCGArg arg1, TCGArg arg2, int const_arg2,
1012 int label_index, int arch64)
1014 tcg_out_cmp (s, cond, arg1, arg2, const_arg2, 7, arch64);
1015 tcg_out_bc (s, tcg_to_bc[cond], label_index);
1018 void ppc_tb_set_jmp_target (unsigned long jmp_addr, unsigned long addr)
1021 unsigned long patch_size;
1023 s.code_ptr = (uint8_t *) jmp_addr;
1024 tcg_out_b (&s, 0, addr);
1025 patch_size = s.code_ptr - (uint8_t *) jmp_addr;
1026 flush_icache_range (jmp_addr, jmp_addr + patch_size);
1029 static void tcg_out_op (TCGContext *s, int opc, const TCGArg *args,
1030 const int *const_args)
1035 case INDEX_op_exit_tb:
1036 tcg_out_movi (s, TCG_TYPE_I64, TCG_REG_R3, args[0]);
1037 tcg_out_b (s, 0, (tcg_target_long) tb_ret_addr);
1039 case INDEX_op_goto_tb:
1040 if (s->tb_jmp_offset) {
1041 /* direct jump method */
1043 s->tb_jmp_offset[args[0]] = s->code_ptr - s->code_buf;
1049 s->tb_next_offset[args[0]] = s->code_ptr - s->code_buf;
1053 TCGLabel *l = &s->labels[args[0]];
1056 tcg_out_b (s, 0, l->u.value);
1059 uint32_t val = *(uint32_t *) s->code_ptr;
1061 /* Thanks to Andrzej Zaborowski */
1062 tcg_out32 (s, B | (val & 0x3fffffc));
1063 tcg_out_reloc (s, s->code_ptr - 4, R_PPC_REL24, args[0], 0);
1068 tcg_out_call (s, args[0], const_args[0]);
1071 if (const_args[0]) {
1072 tcg_out_b (s, 0, args[0]);
1075 tcg_out32 (s, MTSPR | RS (args[0]) | CTR);
1076 tcg_out32 (s, BCCTR | BO_ALWAYS);
1079 case INDEX_op_movi_i32:
1080 tcg_out_movi (s, TCG_TYPE_I32, args[0], args[1]);
1082 case INDEX_op_movi_i64:
1083 tcg_out_movi (s, TCG_TYPE_I64, args[0], args[1]);
1085 case INDEX_op_ld8u_i32:
1086 case INDEX_op_ld8u_i64:
1087 tcg_out_ldst (s, args[0], args[1], args[2], LBZ, LBZX);
1089 case INDEX_op_ld8s_i32:
1090 case INDEX_op_ld8s_i64:
1091 tcg_out_ldst (s, args[0], args[1], args[2], LBZ, LBZX);
1092 tcg_out32 (s, EXTSB | RS (args[0]) | RA (args[0]));
1094 case INDEX_op_ld16u_i32:
1095 case INDEX_op_ld16u_i64:
1096 tcg_out_ldst (s, args[0], args[1], args[2], LHZ, LHZX);
1098 case INDEX_op_ld16s_i32:
1099 case INDEX_op_ld16s_i64:
1100 tcg_out_ldst (s, args[0], args[1], args[2], LHA, LHAX);
1102 case INDEX_op_ld_i32:
1103 case INDEX_op_ld32u_i64:
1104 tcg_out_ldst (s, args[0], args[1], args[2], LWZ, LWZX);
1106 case INDEX_op_ld32s_i64:
1107 tcg_out_ldsta (s, args[0], args[1], args[2], LWA, LWAX);
1109 case INDEX_op_ld_i64:
1110 tcg_out_ldsta (s, args[0], args[1], args[2], LD, LDX);
1112 case INDEX_op_st8_i32:
1113 case INDEX_op_st8_i64:
1114 tcg_out_ldst (s, args[0], args[1], args[2], STB, STBX);
1116 case INDEX_op_st16_i32:
1117 case INDEX_op_st16_i64:
1118 tcg_out_ldst (s, args[0], args[1], args[2], STH, STHX);
1120 case INDEX_op_st_i32:
1121 case INDEX_op_st32_i64:
1122 tcg_out_ldst (s, args[0], args[1], args[2], STW, STWX);
1124 case INDEX_op_st_i64:
1125 tcg_out_ldsta (s, args[0], args[1], args[2], STD, STDX);
1128 case INDEX_op_add_i32:
1130 ppc_addi32 (s, args[0], args[1], args[2]);
1132 tcg_out32 (s, ADD | TAB (args[0], args[1], args[2]));
1134 case INDEX_op_sub_i32:
1136 ppc_addi32 (s, args[0], args[1], -args[2]);
1138 tcg_out32 (s, SUBF | TAB (args[0], args[2], args[1]));
1141 case INDEX_op_and_i64:
1142 case INDEX_op_and_i32:
1143 if (const_args[2]) {
1144 if ((args[2] & 0xffff) == args[2])
1145 tcg_out32 (s, ANDI | RS (args[1]) | RA (args[0]) | args[2]);
1146 else if ((args[2] & 0xffff0000) == args[2])
1147 tcg_out32 (s, ANDIS | RS (args[1]) | RA (args[0])
1148 | ((args[2] >> 16) & 0xffff));
1150 tcg_out_movi (s, (opc == INDEX_op_and_i32
1154 tcg_out32 (s, AND | SAB (args[1], args[0], 0));
1158 tcg_out32 (s, AND | SAB (args[1], args[0], args[2]));
1160 case INDEX_op_or_i64:
1161 case INDEX_op_or_i32:
1162 if (const_args[2]) {
1163 if (args[2] & 0xffff) {
1164 tcg_out32 (s, ORI | RS (args[1]) | RA (args[0])
1165 | (args[2] & 0xffff));
1167 tcg_out32 (s, ORIS | RS (args[0]) | RA (args[0])
1168 | ((args[2] >> 16) & 0xffff));
1171 tcg_out32 (s, ORIS | RS (args[1]) | RA (args[0])
1172 | ((args[2] >> 16) & 0xffff));
1176 tcg_out32 (s, OR | SAB (args[1], args[0], args[2]));
1178 case INDEX_op_xor_i64:
1179 case INDEX_op_xor_i32:
1180 if (const_args[2]) {
1181 if ((args[2] & 0xffff) == args[2])
1182 tcg_out32 (s, XORI | RS (args[1]) | RA (args[0])
1183 | (args[2] & 0xffff));
1184 else if ((args[2] & 0xffff0000) == args[2])
1185 tcg_out32 (s, XORIS | RS (args[1]) | RA (args[0])
1186 | ((args[2] >> 16) & 0xffff));
1188 tcg_out_movi (s, (opc == INDEX_op_and_i32
1192 tcg_out32 (s, XOR | SAB (args[1], args[0], 0));
1196 tcg_out32 (s, XOR | SAB (args[1], args[0], args[2]));
1199 case INDEX_op_mul_i32:
1200 if (const_args[2]) {
1201 if (args[2] == (int16_t) args[2])
1202 tcg_out32 (s, MULLI | RT (args[0]) | RA (args[1])
1203 | (args[2] & 0xffff));
1205 tcg_out_movi (s, TCG_TYPE_I32, 0, args[2]);
1206 tcg_out32 (s, MULLW | TAB (args[0], args[1], 0));
1210 tcg_out32 (s, MULLW | TAB (args[0], args[1], args[2]));
1213 case INDEX_op_div_i32:
1214 tcg_out32 (s, DIVW | TAB (args[0], args[1], args[2]));
1217 case INDEX_op_divu_i32:
1218 tcg_out32 (s, DIVWU | TAB (args[0], args[1], args[2]));
1221 case INDEX_op_rem_i32:
1222 tcg_out32 (s, DIVW | TAB (0, args[1], args[2]));
1223 tcg_out32 (s, MULLW | TAB (0, 0, args[2]));
1224 tcg_out32 (s, SUBF | TAB (args[0], 0, args[1]));
1227 case INDEX_op_remu_i32:
1228 tcg_out32 (s, DIVWU | TAB (0, args[1], args[2]));
1229 tcg_out32 (s, MULLW | TAB (0, 0, args[2]));
1230 tcg_out32 (s, SUBF | TAB (args[0], 0, args[1]));
1233 case INDEX_op_shl_i32:
1234 if (const_args[2]) {
1235 tcg_out32 (s, (RLWINM
1245 tcg_out32 (s, SLW | SAB (args[1], args[0], args[2]));
1247 case INDEX_op_shr_i32:
1248 if (const_args[2]) {
1249 tcg_out32 (s, (RLWINM
1259 tcg_out32 (s, SRW | SAB (args[1], args[0], args[2]));
1261 case INDEX_op_sar_i32:
1263 tcg_out32 (s, SRAWI | RS (args[1]) | RA (args[0]) | SH (args[2]));
1265 tcg_out32 (s, SRAW | SAB (args[1], args[0], args[2]));
1268 case INDEX_op_brcond_i32:
1269 tcg_out_brcond (s, args[2], args[0], args[1], const_args[1], args[3], 0);
1272 case INDEX_op_brcond_i64:
1273 tcg_out_brcond (s, args[2], args[0], args[1], const_args[1], args[3], 1);
1276 case INDEX_op_neg_i32:
1277 case INDEX_op_neg_i64:
1278 tcg_out32 (s, NEG | RT (args[0]) | RA (args[1]));
1281 case INDEX_op_add_i64:
1283 ppc_addi64 (s, args[0], args[1], args[2]);
1285 tcg_out32 (s, ADD | TAB (args[0], args[1], args[2]));
1287 case INDEX_op_sub_i64:
1289 ppc_addi64 (s, args[0], args[1], -args[2]);
1291 tcg_out32 (s, SUBF | TAB (args[0], args[2], args[1]));
1294 case INDEX_op_shl_i64:
1296 tcg_out_rld (s, RLDICR, args[0], args[1], args[2], 63 - args[2]);
1298 tcg_out32 (s, SLD | SAB (args[1], args[0], args[2]));
1300 case INDEX_op_shr_i64:
1302 tcg_out_rld (s, RLDICL, args[0], args[1], 64 - args[2], args[2]);
1304 tcg_out32 (s, SRD | SAB (args[1], args[0], args[2]));
1306 case INDEX_op_sar_i64:
1307 if (const_args[2]) {
1308 int sh = SH (args[2] & 0x1f) | (((args[2] >> 5) & 1) << 1);
1309 tcg_out32 (s, SRADI | RA (args[0]) | RS (args[1]) | sh);
1312 tcg_out32 (s, SRAD | SAB (args[1], args[0], args[2]));
1315 case INDEX_op_mul_i64:
1316 tcg_out32 (s, MULLD | TAB (args[0], args[1], args[2]));
1318 case INDEX_op_div_i64:
1319 tcg_out32 (s, DIVD | TAB (args[0], args[1], args[2]));
1321 case INDEX_op_divu_i64:
1322 tcg_out32 (s, DIVDU | TAB (args[0], args[1], args[2]));
1324 case INDEX_op_rem_i64:
1325 tcg_out32 (s, DIVD | TAB (0, args[1], args[2]));
1326 tcg_out32 (s, MULLD | TAB (0, 0, args[2]));
1327 tcg_out32 (s, SUBF | TAB (args[0], 0, args[1]));
1329 case INDEX_op_remu_i64:
1330 tcg_out32 (s, DIVDU | TAB (0, args[1], args[2]));
1331 tcg_out32 (s, MULLD | TAB (0, 0, args[2]));
1332 tcg_out32 (s, SUBF | TAB (args[0], 0, args[1]));
1335 case INDEX_op_qemu_ld8u:
1336 tcg_out_qemu_ld (s, args, 0);
1338 case INDEX_op_qemu_ld8s:
1339 tcg_out_qemu_ld (s, args, 0 | 4);
1341 case INDEX_op_qemu_ld16u:
1342 tcg_out_qemu_ld (s, args, 1);
1344 case INDEX_op_qemu_ld16s:
1345 tcg_out_qemu_ld (s, args, 1 | 4);
1347 case INDEX_op_qemu_ld32u:
1348 tcg_out_qemu_ld (s, args, 2);
1350 case INDEX_op_qemu_ld32s:
1351 tcg_out_qemu_ld (s, args, 2 | 4);
1353 case INDEX_op_qemu_ld64:
1354 tcg_out_qemu_ld (s, args, 3);
1356 case INDEX_op_qemu_st8:
1357 tcg_out_qemu_st (s, args, 0);
1359 case INDEX_op_qemu_st16:
1360 tcg_out_qemu_st (s, args, 1);
1362 case INDEX_op_qemu_st32:
1363 tcg_out_qemu_st (s, args, 2);
1365 case INDEX_op_qemu_st64:
1366 tcg_out_qemu_st (s, args, 3);
1369 case INDEX_op_ext8s_i32:
1370 case INDEX_op_ext8s_i64:
1373 case INDEX_op_ext16s_i32:
1374 case INDEX_op_ext16s_i64:
1377 case INDEX_op_ext32s_i64:
1381 tcg_out32 (s, c | RS (args[1]) | RA (args[0]));
1385 tcg_dump_ops (s, stderr);
1390 static const TCGTargetOpDef ppc_op_defs[] = {
1391 { INDEX_op_exit_tb, { } },
1392 { INDEX_op_goto_tb, { } },
1393 { INDEX_op_call, { "ri" } },
1394 { INDEX_op_jmp, { "ri" } },
1395 { INDEX_op_br, { } },
1397 { INDEX_op_mov_i32, { "r", "r" } },
1398 { INDEX_op_mov_i64, { "r", "r" } },
1399 { INDEX_op_movi_i32, { "r" } },
1400 { INDEX_op_movi_i64, { "r" } },
1402 { INDEX_op_ld8u_i32, { "r", "r" } },
1403 { INDEX_op_ld8s_i32, { "r", "r" } },
1404 { INDEX_op_ld16u_i32, { "r", "r" } },
1405 { INDEX_op_ld16s_i32, { "r", "r" } },
1406 { INDEX_op_ld_i32, { "r", "r" } },
1407 { INDEX_op_ld_i64, { "r", "r" } },
1408 { INDEX_op_st8_i32, { "r", "r" } },
1409 { INDEX_op_st8_i64, { "r", "r" } },
1410 { INDEX_op_st16_i32, { "r", "r" } },
1411 { INDEX_op_st16_i64, { "r", "r" } },
1412 { INDEX_op_st_i32, { "r", "r" } },
1413 { INDEX_op_st_i64, { "r", "r" } },
1414 { INDEX_op_st32_i64, { "r", "r" } },
1416 { INDEX_op_ld8u_i64, { "r", "r" } },
1417 { INDEX_op_ld8s_i64, { "r", "r" } },
1418 { INDEX_op_ld16u_i64, { "r", "r" } },
1419 { INDEX_op_ld16s_i64, { "r", "r" } },
1420 { INDEX_op_ld32u_i64, { "r", "r" } },
1421 { INDEX_op_ld32s_i64, { "r", "r" } },
1422 { INDEX_op_ld_i64, { "r", "r" } },
1424 { INDEX_op_add_i32, { "r", "r", "ri" } },
1425 { INDEX_op_mul_i32, { "r", "r", "ri" } },
1426 { INDEX_op_div_i32, { "r", "r", "r" } },
1427 { INDEX_op_divu_i32, { "r", "r", "r" } },
1428 { INDEX_op_rem_i32, { "r", "r", "r" } },
1429 { INDEX_op_remu_i32, { "r", "r", "r" } },
1430 { INDEX_op_sub_i32, { "r", "r", "ri" } },
1431 { INDEX_op_and_i32, { "r", "r", "ri" } },
1432 { INDEX_op_or_i32, { "r", "r", "ri" } },
1433 { INDEX_op_xor_i32, { "r", "r", "ri" } },
1435 { INDEX_op_shl_i32, { "r", "r", "ri" } },
1436 { INDEX_op_shr_i32, { "r", "r", "ri" } },
1437 { INDEX_op_sar_i32, { "r", "r", "ri" } },
1439 { INDEX_op_brcond_i32, { "r", "ri" } },
1440 { INDEX_op_brcond_i64, { "r", "ri" } },
1442 { INDEX_op_neg_i32, { "r", "r" } },
1444 { INDEX_op_add_i64, { "r", "r", "ri" } },
1445 { INDEX_op_sub_i64, { "r", "r", "ri" } },
1446 { INDEX_op_and_i64, { "r", "r", "rZ" } },
1447 { INDEX_op_or_i64, { "r", "r", "rZ" } },
1448 { INDEX_op_xor_i64, { "r", "r", "rZ" } },
1450 { INDEX_op_shl_i64, { "r", "r", "ri" } },
1451 { INDEX_op_shr_i64, { "r", "r", "ri" } },
1452 { INDEX_op_sar_i64, { "r", "r", "ri" } },
1454 { INDEX_op_mul_i64, { "r", "r", "r" } },
1455 { INDEX_op_div_i64, { "r", "r", "r" } },
1456 { INDEX_op_divu_i64, { "r", "r", "r" } },
1457 { INDEX_op_rem_i64, { "r", "r", "r" } },
1458 { INDEX_op_remu_i64, { "r", "r", "r" } },
1460 { INDEX_op_neg_i64, { "r", "r" } },
1462 { INDEX_op_qemu_ld8u, { "r", "L" } },
1463 { INDEX_op_qemu_ld8s, { "r", "L" } },
1464 { INDEX_op_qemu_ld16u, { "r", "L" } },
1465 { INDEX_op_qemu_ld16s, { "r", "L" } },
1466 { INDEX_op_qemu_ld32u, { "r", "L" } },
1467 { INDEX_op_qemu_ld32s, { "r", "L" } },
1468 { INDEX_op_qemu_ld64, { "r", "L" } },
1470 { INDEX_op_qemu_st8, { "S", "S" } },
1471 { INDEX_op_qemu_st16, { "S", "S" } },
1472 { INDEX_op_qemu_st32, { "S", "S" } },
1473 { INDEX_op_qemu_st64, { "S", "S", "S" } },
1475 { INDEX_op_ext8s_i32, { "r", "r" } },
1476 { INDEX_op_ext16s_i32, { "r", "r" } },
1477 { INDEX_op_ext8s_i64, { "r", "r" } },
1478 { INDEX_op_ext16s_i64, { "r", "r" } },
1479 { INDEX_op_ext32s_i64, { "r", "r" } },
1484 void tcg_target_init (TCGContext *s)
1486 tcg_regset_set32 (tcg_target_available_regs[TCG_TYPE_I32], 0, 0xffffffff);
1487 tcg_regset_set32 (tcg_target_available_regs[TCG_TYPE_I64], 0, 0xffffffff);
1488 tcg_regset_set32 (tcg_target_call_clobber_regs, 0,
1497 (1 << TCG_REG_R10) |
1498 (1 << TCG_REG_R11) |
1502 tcg_regset_clear (s->reserved_regs);
1503 tcg_regset_set_reg (s->reserved_regs, TCG_REG_R0);
1504 tcg_regset_set_reg (s->reserved_regs, TCG_REG_R1);
1505 tcg_regset_set_reg (s->reserved_regs, TCG_REG_R2);
1506 tcg_regset_set_reg (s->reserved_regs, TCG_REG_R13);
1508 tcg_add_target_add_op_defs (ppc_op_defs);