2 * Tiny Code Generator for QEMU
4 * Copyright (c) 2008 Fabrice Bellard
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
25 #include "tcg-be-ldst.h"
27 #define TCG_CT_CONST_S16 0x100
28 #define TCG_CT_CONST_U16 0x200
29 #define TCG_CT_CONST_S32 0x400
30 #define TCG_CT_CONST_U32 0x800
31 #define TCG_CT_CONST_ZERO 0x1000
32 #define TCG_CT_CONST_MONE 0x2000
34 static uint8_t *tb_ret_addr;
36 #if TARGET_LONG_BITS == 32
49 static bool have_isa_2_06;
50 #define HAVE_ISA_2_06 have_isa_2_06
51 #define HAVE_ISEL have_isa_2_06
53 #ifdef CONFIG_USE_GUEST_BASE
54 #define TCG_GUEST_BASE_REG 30
56 #define TCG_GUEST_BASE_REG 0
60 static const char * const tcg_target_reg_names[TCG_TARGET_NB_REGS] = {
96 static const int tcg_target_reg_alloc_order[] = {
97 TCG_REG_R14, /* call saved registers */
115 TCG_REG_R12, /* call clobbered, non-arguments */
117 TCG_REG_R10, /* call clobbered, arguments */
127 static const int tcg_target_call_iarg_regs[] = {
138 static const int tcg_target_call_oarg_regs[] = {
142 static const int tcg_target_callee_save_regs[] = {
159 TCG_REG_R27, /* currently used for the global env */
166 static inline bool in_range_b(tcg_target_long target)
168 return target == sextract64(target, 0, 26);
171 static uint32_t reloc_pc24_val(void *pc, tcg_target_long target)
173 tcg_target_long disp;
175 disp = target - (tcg_target_long)pc;
176 assert(in_range_b(disp));
178 return disp & 0x3fffffc;
181 static void reloc_pc24(void *pc, tcg_target_long target)
183 *(uint32_t *)pc = (*(uint32_t *)pc & ~0x3fffffc)
184 | reloc_pc24_val(pc, target);
187 static uint16_t reloc_pc14_val(void *pc, tcg_target_long target)
189 tcg_target_long disp;
191 disp = target - (tcg_target_long)pc;
192 if (disp != (int16_t) disp) {
196 return disp & 0xfffc;
199 static void reloc_pc14(void *pc, tcg_target_long target)
201 *(uint32_t *)pc = (*(uint32_t *)pc & ~0xfffc) | reloc_pc14_val(pc, target);
204 static inline void tcg_out_b_noaddr(TCGContext *s, int insn)
206 unsigned retrans = *(uint32_t *)s->code_ptr & 0x3fffffc;
207 tcg_out32(s, insn | retrans);
210 static inline void tcg_out_bc_noaddr(TCGContext *s, int insn)
212 unsigned retrans = *(uint32_t *)s->code_ptr & 0xfffc;
213 tcg_out32(s, insn | retrans);
216 static void patch_reloc(uint8_t *code_ptr, int type,
217 intptr_t value, intptr_t addend)
222 reloc_pc14(code_ptr, value);
225 reloc_pc24(code_ptr, value);
232 /* parse target specific constraints */
233 static int target_parse_constraint(TCGArgConstraint *ct, const char **pct_str)
239 case 'A': case 'B': case 'C': case 'D':
240 ct->ct |= TCG_CT_REG;
241 tcg_regset_set_reg(ct->u.regs, 3 + ct_str[0] - 'A');
244 ct->ct |= TCG_CT_REG;
245 tcg_regset_set32(ct->u.regs, 0, 0xffffffff);
247 case 'L': /* qemu_ld constraint */
248 ct->ct |= TCG_CT_REG;
249 tcg_regset_set32(ct->u.regs, 0, 0xffffffff);
250 tcg_regset_reset_reg(ct->u.regs, TCG_REG_R3);
251 #ifdef CONFIG_SOFTMMU
252 tcg_regset_reset_reg(ct->u.regs, TCG_REG_R4);
253 tcg_regset_reset_reg(ct->u.regs, TCG_REG_R5);
256 case 'S': /* qemu_st constraint */
257 ct->ct |= TCG_CT_REG;
258 tcg_regset_set32(ct->u.regs, 0, 0xffffffff);
259 tcg_regset_reset_reg(ct->u.regs, TCG_REG_R3);
260 #ifdef CONFIG_SOFTMMU
261 tcg_regset_reset_reg(ct->u.regs, TCG_REG_R4);
262 tcg_regset_reset_reg(ct->u.regs, TCG_REG_R5);
263 tcg_regset_reset_reg(ct->u.regs, TCG_REG_R6);
267 ct->ct |= TCG_CT_CONST_S16;
270 ct->ct |= TCG_CT_CONST_U16;
273 ct->ct |= TCG_CT_CONST_MONE;
276 ct->ct |= TCG_CT_CONST_S32;
279 ct->ct |= TCG_CT_CONST_U32;
282 ct->ct |= TCG_CT_CONST_ZERO;
292 /* test if a constant matches the constraint */
293 static int tcg_target_const_match(tcg_target_long val, TCGType type,
294 const TCGArgConstraint *arg_ct)
297 if (ct & TCG_CT_CONST) {
301 /* The only 32-bit constraint we use aside from
302 TCG_CT_CONST is TCG_CT_CONST_S16. */
303 if (type == TCG_TYPE_I32) {
307 if ((ct & TCG_CT_CONST_S16) && val == (int16_t)val) {
309 } else if ((ct & TCG_CT_CONST_U16) && val == (uint16_t)val) {
311 } else if ((ct & TCG_CT_CONST_S32) && val == (int32_t)val) {
313 } else if ((ct & TCG_CT_CONST_U32) && val == (uint32_t)val) {
315 } else if ((ct & TCG_CT_CONST_ZERO) && val == 0) {
317 } else if ((ct & TCG_CT_CONST_MONE) && val == -1) {
323 #define OPCD(opc) ((opc)<<26)
324 #define XO19(opc) (OPCD(19)|((opc)<<1))
325 #define MD30(opc) (OPCD(30)|((opc)<<2))
326 #define MDS30(opc) (OPCD(30)|((opc)<<1))
327 #define XO31(opc) (OPCD(31)|((opc)<<1))
328 #define XO58(opc) (OPCD(58)|(opc))
329 #define XO62(opc) (OPCD(62)|(opc))
333 #define LBZ OPCD( 34)
334 #define LHZ OPCD( 40)
335 #define LHA OPCD( 42)
336 #define LWZ OPCD( 32)
337 #define STB OPCD( 38)
338 #define STH OPCD( 44)
339 #define STW OPCD( 36)
342 #define STDU XO62( 1)
343 #define STDX XO31(149)
346 #define LDX XO31( 21)
349 #define LWAX XO31(341)
351 #define ADDIC OPCD( 12)
352 #define ADDI OPCD( 14)
353 #define ADDIS OPCD( 15)
354 #define ORI OPCD( 24)
355 #define ORIS OPCD( 25)
356 #define XORI OPCD( 26)
357 #define XORIS OPCD( 27)
358 #define ANDI OPCD( 28)
359 #define ANDIS OPCD( 29)
360 #define MULLI OPCD( 7)
361 #define CMPLI OPCD( 10)
362 #define CMPI OPCD( 11)
363 #define SUBFIC OPCD( 8)
365 #define LWZU OPCD( 33)
366 #define STWU OPCD( 37)
368 #define RLWIMI OPCD( 20)
369 #define RLWINM OPCD( 21)
370 #define RLWNM OPCD( 23)
372 #define RLDICL MD30( 0)
373 #define RLDICR MD30( 1)
374 #define RLDIMI MD30( 3)
375 #define RLDCL MDS30( 8)
377 #define BCLR XO19( 16)
378 #define BCCTR XO19(528)
379 #define CRAND XO19(257)
380 #define CRANDC XO19(129)
381 #define CRNAND XO19(225)
382 #define CROR XO19(449)
383 #define CRNOR XO19( 33)
385 #define EXTSB XO31(954)
386 #define EXTSH XO31(922)
387 #define EXTSW XO31(986)
388 #define ADD XO31(266)
389 #define ADDE XO31(138)
390 #define ADDME XO31(234)
391 #define ADDZE XO31(202)
392 #define ADDC XO31( 10)
393 #define AND XO31( 28)
394 #define SUBF XO31( 40)
395 #define SUBFC XO31( 8)
396 #define SUBFE XO31(136)
397 #define SUBFME XO31(232)
398 #define SUBFZE XO31(200)
400 #define XOR XO31(316)
401 #define MULLW XO31(235)
402 #define MULHWU XO31( 11)
403 #define DIVW XO31(491)
404 #define DIVWU XO31(459)
406 #define CMPL XO31( 32)
407 #define LHBRX XO31(790)
408 #define LWBRX XO31(534)
409 #define LDBRX XO31(532)
410 #define STHBRX XO31(918)
411 #define STWBRX XO31(662)
412 #define STDBRX XO31(660)
413 #define MFSPR XO31(339)
414 #define MTSPR XO31(467)
415 #define SRAWI XO31(824)
416 #define NEG XO31(104)
417 #define MFCR XO31( 19)
418 #define MFOCRF (MFCR | (1u << 20))
419 #define NOR XO31(124)
420 #define CNTLZW XO31( 26)
421 #define CNTLZD XO31( 58)
422 #define ANDC XO31( 60)
423 #define ORC XO31(412)
424 #define EQV XO31(284)
425 #define NAND XO31(476)
426 #define ISEL XO31( 15)
428 #define MULLD XO31(233)
429 #define MULHD XO31( 73)
430 #define MULHDU XO31( 9)
431 #define DIVD XO31(489)
432 #define DIVDU XO31(457)
434 #define LBZX XO31( 87)
435 #define LHZX XO31(279)
436 #define LHAX XO31(343)
437 #define LWZX XO31( 23)
438 #define STBX XO31(215)
439 #define STHX XO31(407)
440 #define STWX XO31(151)
442 #define SPR(a, b) ((((a)<<5)|(b))<<11)
444 #define CTR SPR(9, 0)
446 #define SLW XO31( 24)
447 #define SRW XO31(536)
448 #define SRAW XO31(792)
450 #define SLD XO31( 27)
451 #define SRD XO31(539)
452 #define SRAD XO31(794)
453 #define SRADI XO31(413<<1)
456 #define TRAP (TW | TO(31))
458 #define RT(r) ((r)<<21)
459 #define RS(r) ((r)<<21)
460 #define RA(r) ((r)<<16)
461 #define RB(r) ((r)<<11)
462 #define TO(t) ((t)<<21)
463 #define SH(s) ((s)<<11)
464 #define MB(b) ((b)<<6)
465 #define ME(e) ((e)<<1)
466 #define BO(o) ((o)<<21)
467 #define MB64(b) ((b)<<5)
468 #define FXM(b) (1 << (19 - (b)))
472 #define TAB(t, a, b) (RT(t) | RA(a) | RB(b))
473 #define SAB(s, a, b) (RS(s) | RA(a) | RB(b))
474 #define TAI(s, a, i) (RT(s) | RA(a) | ((i) & 0xffff))
475 #define SAI(s, a, i) (RS(s) | RA(a) | ((i) & 0xffff))
477 #define BF(n) ((n)<<23)
478 #define BI(n, c) (((c)+((n)*4))<<16)
479 #define BT(n, c) (((c)+((n)*4))<<21)
480 #define BA(n, c) (((c)+((n)*4))<<16)
481 #define BB(n, c) (((c)+((n)*4))<<11)
482 #define BC_(n, c) (((c)+((n)*4))<<6)
484 #define BO_COND_TRUE BO(12)
485 #define BO_COND_FALSE BO( 4)
486 #define BO_ALWAYS BO(20)
495 static const uint32_t tcg_to_bc[] = {
496 [TCG_COND_EQ] = BC | BI(7, CR_EQ) | BO_COND_TRUE,
497 [TCG_COND_NE] = BC | BI(7, CR_EQ) | BO_COND_FALSE,
498 [TCG_COND_LT] = BC | BI(7, CR_LT) | BO_COND_TRUE,
499 [TCG_COND_GE] = BC | BI(7, CR_LT) | BO_COND_FALSE,
500 [TCG_COND_LE] = BC | BI(7, CR_GT) | BO_COND_FALSE,
501 [TCG_COND_GT] = BC | BI(7, CR_GT) | BO_COND_TRUE,
502 [TCG_COND_LTU] = BC | BI(7, CR_LT) | BO_COND_TRUE,
503 [TCG_COND_GEU] = BC | BI(7, CR_LT) | BO_COND_FALSE,
504 [TCG_COND_LEU] = BC | BI(7, CR_GT) | BO_COND_FALSE,
505 [TCG_COND_GTU] = BC | BI(7, CR_GT) | BO_COND_TRUE,
508 /* The low bit here is set if the RA and RB fields must be inverted. */
509 static const uint32_t tcg_to_isel[] = {
510 [TCG_COND_EQ] = ISEL | BC_(7, CR_EQ),
511 [TCG_COND_NE] = ISEL | BC_(7, CR_EQ) | 1,
512 [TCG_COND_LT] = ISEL | BC_(7, CR_LT),
513 [TCG_COND_GE] = ISEL | BC_(7, CR_LT) | 1,
514 [TCG_COND_LE] = ISEL | BC_(7, CR_GT) | 1,
515 [TCG_COND_GT] = ISEL | BC_(7, CR_GT),
516 [TCG_COND_LTU] = ISEL | BC_(7, CR_LT),
517 [TCG_COND_GEU] = ISEL | BC_(7, CR_LT) | 1,
518 [TCG_COND_LEU] = ISEL | BC_(7, CR_GT) | 1,
519 [TCG_COND_GTU] = ISEL | BC_(7, CR_GT),
522 static inline void tcg_out_mov(TCGContext *s, TCGType type,
523 TCGReg ret, TCGReg arg)
526 tcg_out32(s, OR | SAB(arg, ret, arg));
530 static inline void tcg_out_rld(TCGContext *s, int op, TCGReg ra, TCGReg rs,
533 sh = SH(sh & 0x1f) | (((sh >> 5) & 1) << 1);
534 mb = MB64((mb >> 5) | ((mb << 1) & 0x3f));
535 tcg_out32(s, op | RA(ra) | RS(rs) | sh | mb);
538 static inline void tcg_out_rlw(TCGContext *s, int op, TCGReg ra, TCGReg rs,
539 int sh, int mb, int me)
541 tcg_out32(s, op | RA(ra) | RS(rs) | SH(sh) | MB(mb) | ME(me));
544 static inline void tcg_out_ext32u(TCGContext *s, TCGReg dst, TCGReg src)
546 tcg_out_rld(s, RLDICL, dst, src, 0, 32);
549 static inline void tcg_out_shli64(TCGContext *s, TCGReg dst, TCGReg src, int c)
551 tcg_out_rld(s, RLDICR, dst, src, c, 63 - c);
554 static inline void tcg_out_shri64(TCGContext *s, TCGReg dst, TCGReg src, int c)
556 tcg_out_rld(s, RLDICL, dst, src, 64 - c, c);
559 static void tcg_out_movi32(TCGContext *s, TCGReg ret, int32_t arg)
561 if (arg == (int16_t) arg) {
562 tcg_out32(s, ADDI | TAI(ret, 0, arg));
564 tcg_out32(s, ADDIS | TAI(ret, 0, arg >> 16));
566 tcg_out32(s, ORI | SAI(ret, ret, arg));
571 static void tcg_out_movi(TCGContext *s, TCGType type, TCGReg ret,
574 if (type == TCG_TYPE_I32 || arg == (int32_t)arg) {
575 tcg_out_movi32(s, ret, arg);
576 } else if (arg == (uint32_t)arg && !(arg & 0x8000)) {
577 tcg_out32(s, ADDI | TAI(ret, 0, arg));
578 tcg_out32(s, ORIS | SAI(ret, ret, arg >> 16));
580 int32_t high = arg >> 32;
581 tcg_out_movi32(s, ret, high);
583 tcg_out_shli64(s, ret, ret, 32);
585 if (arg & 0xffff0000) {
586 tcg_out32(s, ORIS | SAI(ret, ret, arg >> 16));
589 tcg_out32(s, ORI | SAI(ret, ret, arg));
594 static bool mask_operand(uint32_t c, int *mb, int *me)
598 /* Accept a bit pattern like:
602 Keep track of the transitions. */
603 if (c == 0 || c == -1) {
609 if (test & (test - 1)) {
614 *mb = test ? clz32(test & -test) + 1 : 0;
618 static bool mask64_operand(uint64_t c, int *mb, int *me)
627 /* Accept 1..10..0. */
633 /* Accept 0..01..1. */
634 if (lsb == 1 && (c & (c + 1)) == 0) {
635 *mb = clz64(c + 1) + 1;
642 static void tcg_out_andi32(TCGContext *s, TCGReg dst, TCGReg src, uint32_t c)
646 if ((c & 0xffff) == c) {
647 tcg_out32(s, ANDI | SAI(src, dst, c));
649 } else if ((c & 0xffff0000) == c) {
650 tcg_out32(s, ANDIS | SAI(src, dst, c >> 16));
652 } else if (mask_operand(c, &mb, &me)) {
653 tcg_out_rlw(s, RLWINM, dst, src, 0, mb, me);
655 tcg_out_movi(s, TCG_TYPE_I32, TCG_REG_R0, c);
656 tcg_out32(s, AND | SAB(src, dst, TCG_REG_R0));
660 static void tcg_out_andi64(TCGContext *s, TCGReg dst, TCGReg src, uint64_t c)
664 if ((c & 0xffff) == c) {
665 tcg_out32(s, ANDI | SAI(src, dst, c));
667 } else if ((c & 0xffff0000) == c) {
668 tcg_out32(s, ANDIS | SAI(src, dst, c >> 16));
670 } else if (mask64_operand(c, &mb, &me)) {
672 tcg_out_rld(s, RLDICR, dst, src, 0, me);
674 tcg_out_rld(s, RLDICL, dst, src, 0, mb);
677 tcg_out_movi(s, TCG_TYPE_I64, TCG_REG_R0, c);
678 tcg_out32(s, AND | SAB(src, dst, TCG_REG_R0));
682 static void tcg_out_zori32(TCGContext *s, TCGReg dst, TCGReg src, uint32_t c,
683 int op_lo, int op_hi)
686 tcg_out32(s, op_hi | SAI(src, dst, c >> 16));
690 tcg_out32(s, op_lo | SAI(src, dst, c));
695 static void tcg_out_ori32(TCGContext *s, TCGReg dst, TCGReg src, uint32_t c)
697 tcg_out_zori32(s, dst, src, c, ORI, ORIS);
700 static void tcg_out_xori32(TCGContext *s, TCGReg dst, TCGReg src, uint32_t c)
702 tcg_out_zori32(s, dst, src, c, XORI, XORIS);
705 static void tcg_out_b(TCGContext *s, int mask, tcg_target_long target)
707 tcg_target_long disp;
709 disp = target - (tcg_target_long)s->code_ptr;
710 if (in_range_b(disp)) {
711 tcg_out32(s, B | (disp & 0x3fffffc) | mask);
713 tcg_out_movi(s, TCG_TYPE_I64, TCG_REG_R0, (tcg_target_long)target);
714 tcg_out32(s, MTSPR | RS(TCG_REG_R0) | CTR);
715 tcg_out32(s, BCCTR | BO_ALWAYS | mask);
719 static void tcg_out_call(TCGContext *s, tcg_target_long arg, int const_arg)
723 tcg_out_b(s, LK, arg);
725 tcg_out32(s, MTSPR | RS(arg) | LR);
726 tcg_out32(s, BCLR | BO_ALWAYS | LK);
733 /* Look through the descriptor. If the branch is in range, and we
734 don't have to spend too much effort on building the toc. */
735 intptr_t tgt = ((intptr_t *)arg)[0];
736 intptr_t toc = ((intptr_t *)arg)[1];
737 intptr_t diff = tgt - (intptr_t)s->code_ptr;
739 if (in_range_b(diff) && toc == (uint32_t)toc) {
740 tcg_out_movi(s, TCG_TYPE_I64, TCG_REG_R2, toc);
741 tcg_out_b(s, LK, tgt);
745 /* Fold the low bits of the constant into the addresses below. */
747 if (ofs + 8 < 0x8000) {
753 tcg_out_movi(s, TCG_TYPE_I64, reg, arg);
756 tcg_out32(s, LD | TAI(TCG_REG_R0, reg, ofs));
757 tcg_out32(s, MTSPR | RA(TCG_REG_R0) | CTR);
758 tcg_out32(s, LD | TAI(TCG_REG_R2, reg, ofs + 8));
759 tcg_out32(s, BCCTR | BO_ALWAYS | LK);
763 static void tcg_out_mem_long(TCGContext *s, int opi, int opx, TCGReg rt,
764 TCGReg base, tcg_target_long offset)
766 tcg_target_long orig = offset, l0, l1, extra = 0, align = 0;
767 TCGReg rs = TCG_REG_R2;
769 assert(rt != TCG_REG_R2 && base != TCG_REG_R2);
776 if (rt != TCG_REG_R0) {
783 case STB: case STH: case STW:
787 /* For unaligned, or very large offsets, use the indexed form. */
788 if (offset & align || offset != (int32_t)offset) {
789 tcg_out_movi(s, TCG_TYPE_I64, TCG_REG_R2, orig);
790 tcg_out32(s, opx | TAB(rt, base, TCG_REG_R2));
794 l0 = (int16_t)offset;
795 offset = (offset - l0) >> 16;
796 l1 = (int16_t)offset;
798 if (l1 < 0 && orig >= 0) {
800 l1 = (int16_t)(offset - 0x4000);
803 tcg_out32(s, ADDIS | TAI(rs, base, l1));
807 tcg_out32(s, ADDIS | TAI(rs, base, extra));
810 if (opi != ADDI || base != rt || l0 != 0) {
811 tcg_out32(s, opi | TAI(rt, base, l0));
815 static const uint32_t qemu_ldx_opc[16] = {
822 [MO_BSWAP | MO_UB] = LBZX,
823 [MO_BSWAP | MO_UW] = LHBRX,
824 [MO_BSWAP | MO_UL] = LWBRX,
825 [MO_BSWAP | MO_Q] = LDBRX,
828 static const uint32_t qemu_stx_opc[16] = {
833 [MO_BSWAP | MO_UB] = STBX,
834 [MO_BSWAP | MO_UW] = STHBRX,
835 [MO_BSWAP | MO_UL] = STWBRX,
836 [MO_BSWAP | MO_Q] = STDBRX,
839 static const uint32_t qemu_exts_opc[4] = {
840 EXTSB, EXTSH, EXTSW, 0
843 #if defined (CONFIG_SOFTMMU)
844 /* helper signature: helper_ld_mmu(CPUState *env, target_ulong addr,
845 * int mmu_idx, uintptr_t ra)
847 static const void * const qemu_ld_helpers[16] = {
848 [MO_UB] = helper_ret_ldub_mmu,
849 [MO_LEUW] = helper_le_lduw_mmu,
850 [MO_LEUL] = helper_le_ldul_mmu,
851 [MO_LEQ] = helper_le_ldq_mmu,
852 [MO_BEUW] = helper_be_lduw_mmu,
853 [MO_BEUL] = helper_be_ldul_mmu,
854 [MO_BEQ] = helper_be_ldq_mmu,
857 /* helper signature: helper_st_mmu(CPUState *env, target_ulong addr,
858 * uintxx_t val, int mmu_idx, uintptr_t ra)
860 static const void * const qemu_st_helpers[16] = {
861 [MO_UB] = helper_ret_stb_mmu,
862 [MO_LEUW] = helper_le_stw_mmu,
863 [MO_LEUL] = helper_le_stl_mmu,
864 [MO_LEQ] = helper_le_stq_mmu,
865 [MO_BEUW] = helper_be_stw_mmu,
866 [MO_BEUL] = helper_be_stl_mmu,
867 [MO_BEQ] = helper_be_stq_mmu,
870 /* Perform the TLB load and compare. Places the result of the comparison
871 in CR7, loads the addend of the TLB into R3, and returns the register
872 containing the guest address (zero-extended into R4). Clobbers R0 and R2. */
874 static TCGReg tcg_out_tlb_read(TCGContext *s, TCGMemOp s_bits, TCGReg addr_reg,
875 int mem_index, bool is_read)
879 ? offsetof(CPUArchState, tlb_table[mem_index][0].addr_read)
880 : offsetof(CPUArchState, tlb_table[mem_index][0].addr_write));
881 int add_off = offsetof(CPUArchState, tlb_table[mem_index][0].addend);
882 TCGReg base = TCG_AREG0;
884 /* Extract the page index, shifted into place for tlb index. */
885 if (TARGET_LONG_BITS == 32) {
886 /* Zero-extend the address into a place helpful for further use. */
887 tcg_out_ext32u(s, TCG_REG_R4, addr_reg);
888 addr_reg = TCG_REG_R4;
890 tcg_out_rld(s, RLDICL, TCG_REG_R3, addr_reg,
891 64 - TARGET_PAGE_BITS, 64 - CPU_TLB_BITS);
894 /* Compensate for very large offsets. */
895 if (add_off >= 0x8000) {
896 /* Most target env are smaller than 32k; none are larger than 64k.
897 Simplify the logic here merely to offset by 0x7ff0, giving us a
898 range just shy of 64k. Check this assumption. */
899 QEMU_BUILD_BUG_ON(offsetof(CPUArchState,
900 tlb_table[NB_MMU_MODES - 1][1])
902 tcg_out32(s, ADDI | TAI(TCG_REG_R2, base, 0x7ff0));
908 /* Extraction and shifting, part 2. */
909 if (TARGET_LONG_BITS == 32) {
910 tcg_out_rlw(s, RLWINM, TCG_REG_R3, addr_reg,
911 32 - (TARGET_PAGE_BITS - CPU_TLB_ENTRY_BITS),
912 32 - (CPU_TLB_BITS + CPU_TLB_ENTRY_BITS),
913 31 - CPU_TLB_ENTRY_BITS);
915 tcg_out_shli64(s, TCG_REG_R3, TCG_REG_R3, CPU_TLB_ENTRY_BITS);
918 tcg_out32(s, ADD | TAB(TCG_REG_R3, TCG_REG_R3, base));
920 /* Load the tlb comparator. */
921 tcg_out32(s, LD_ADDR | TAI(TCG_REG_R2, TCG_REG_R3, cmp_off));
923 /* Load the TLB addend for use on the fast path. Do this asap
924 to minimize any load use delay. */
925 tcg_out32(s, LD | TAI(TCG_REG_R3, TCG_REG_R3, add_off));
927 /* Clear the non-page, non-alignment bits from the address. */
928 if (TARGET_LONG_BITS == 32) {
929 tcg_out_rlw(s, RLWINM, TCG_REG_R0, addr_reg, 0,
930 (32 - s_bits) & 31, 31 - TARGET_PAGE_BITS);
931 } else if (!s_bits) {
932 tcg_out_rld(s, RLDICR, TCG_REG_R0, addr_reg, 0, 63 - TARGET_PAGE_BITS);
934 tcg_out_rld(s, RLDICL, TCG_REG_R0, addr_reg,
935 64 - TARGET_PAGE_BITS, TARGET_PAGE_BITS - s_bits);
936 tcg_out_rld(s, RLDICL, TCG_REG_R0, TCG_REG_R0, TARGET_PAGE_BITS, 0);
939 tcg_out32(s, CMP | BF(7) | RA(TCG_REG_R0) | RB(TCG_REG_R2) | CMP_L);
944 /* Record the context of a call to the out of line helper code for the slow
945 path for a load or store, so that we can later generate the correct
947 static void add_qemu_ldst_label(TCGContext *s, bool is_ld, TCGMemOp opc,
948 int data_reg, int addr_reg, int mem_index,
949 uint8_t *raddr, uint8_t *label_ptr)
951 TCGLabelQemuLdst *label = new_ldst_label(s);
953 label->is_ld = is_ld;
955 label->datalo_reg = data_reg;
956 label->addrlo_reg = addr_reg;
957 label->mem_index = mem_index;
958 label->raddr = raddr;
959 label->label_ptr[0] = label_ptr;
962 static void tcg_out_qemu_ld_slow_path(TCGContext *s, TCGLabelQemuLdst *lb)
964 TCGMemOp opc = lb->opc;
966 reloc_pc14(lb->label_ptr[0], (uintptr_t)s->code_ptr);
968 tcg_out_mov(s, TCG_TYPE_PTR, TCG_REG_R3, TCG_AREG0);
970 /* If the address needed to be zero-extended, we'll have already
971 placed it in R4. The only remaining case is 64-bit guest. */
972 tcg_out_mov(s, TCG_TYPE_I64, TCG_REG_R4, lb->addrlo_reg);
974 tcg_out_movi(s, TCG_TYPE_I32, TCG_REG_R5, lb->mem_index);
975 tcg_out32(s, MFSPR | RT(TCG_REG_R6) | LR);
977 tcg_out_call(s, (tcg_target_long)qemu_ld_helpers[opc & ~MO_SIGN], 1);
980 uint32_t insn = qemu_exts_opc[opc & MO_SIZE];
981 tcg_out32(s, insn | RA(lb->datalo_reg) | RS(TCG_REG_R3));
983 tcg_out_mov(s, TCG_TYPE_I64, lb->datalo_reg, TCG_REG_R3);
986 tcg_out_b(s, 0, (uintptr_t)lb->raddr);
989 static void tcg_out_qemu_st_slow_path(TCGContext *s, TCGLabelQemuLdst *lb)
991 TCGMemOp opc = lb->opc;
992 TCGMemOp s_bits = opc & MO_SIZE;
994 reloc_pc14(lb->label_ptr[0], (uintptr_t)s->code_ptr);
996 tcg_out_mov(s, TCG_TYPE_I64, TCG_REG_R3, TCG_AREG0);
998 /* If the address needed to be zero-extended, we'll have already
999 placed it in R4. The only remaining case is 64-bit guest. */
1000 tcg_out_mov(s, TCG_TYPE_I64, TCG_REG_R4, lb->addrlo_reg);
1002 tcg_out_rld(s, RLDICL, TCG_REG_R5, lb->datalo_reg,
1003 0, 64 - (1 << (3 + s_bits)));
1004 tcg_out_movi(s, TCG_TYPE_I32, TCG_REG_R6, lb->mem_index);
1005 tcg_out32(s, MFSPR | RT(TCG_REG_R7) | LR);
1007 tcg_out_call(s, (tcg_target_long)qemu_st_helpers[opc], 1);
1009 tcg_out_b(s, 0, (uintptr_t)lb->raddr);
1011 #endif /* SOFTMMU */
1013 static void tcg_out_qemu_ld(TCGContext *s, TCGReg data_reg, TCGReg addr_reg,
1014 TCGMemOp opc, int mem_index)
1018 TCGMemOp s_bits = opc & MO_SIZE;
1019 #ifdef CONFIG_SOFTMMU
1023 #ifdef CONFIG_SOFTMMU
1024 addr_reg = tcg_out_tlb_read(s, s_bits, addr_reg, mem_index, true);
1026 /* Load a pointer into the current opcode w/conditional branch-link. */
1027 label_ptr = s->code_ptr;
1028 tcg_out_bc_noaddr(s, BC | BI(7, CR_EQ) | BO_COND_FALSE | LK);
1031 #else /* !CONFIG_SOFTMMU */
1032 rbase = GUEST_BASE ? TCG_GUEST_BASE_REG : 0;
1033 if (TARGET_LONG_BITS == 32) {
1034 tcg_out_ext32u(s, TCG_REG_R2, addr_reg);
1035 addr_reg = TCG_REG_R2;
1039 insn = qemu_ldx_opc[opc];
1040 if (!HAVE_ISA_2_06 && insn == LDBRX) {
1041 tcg_out32(s, ADDI | TAI(TCG_REG_R0, addr_reg, 4));
1042 tcg_out32(s, LWBRX | TAB(data_reg, rbase, addr_reg));
1043 tcg_out32(s, LWBRX | TAB(TCG_REG_R0, rbase, TCG_REG_R0));
1044 tcg_out_rld(s, RLDIMI, data_reg, TCG_REG_R0, 32, 0);
1046 tcg_out32(s, insn | TAB(data_reg, rbase, addr_reg));
1048 insn = qemu_ldx_opc[opc & (MO_SIZE | MO_BSWAP)];
1049 tcg_out32(s, insn | TAB(data_reg, rbase, addr_reg));
1050 insn = qemu_exts_opc[s_bits];
1051 tcg_out32(s, insn | RA(data_reg) | RS(data_reg));
1054 #ifdef CONFIG_SOFTMMU
1055 add_qemu_ldst_label(s, true, opc, data_reg, addr_reg, mem_index,
1056 s->code_ptr, label_ptr);
1060 static void tcg_out_qemu_st(TCGContext *s, TCGReg data_reg, TCGReg addr_reg,
1061 TCGMemOp opc, int mem_index)
1065 #ifdef CONFIG_SOFTMMU
1069 #ifdef CONFIG_SOFTMMU
1070 addr_reg = tcg_out_tlb_read(s, opc & MO_SIZE, addr_reg, mem_index, false);
1072 /* Load a pointer into the current opcode w/conditional branch-link. */
1073 label_ptr = s->code_ptr;
1074 tcg_out_bc_noaddr(s, BC | BI(7, CR_EQ) | BO_COND_FALSE | LK);
1077 #else /* !CONFIG_SOFTMMU */
1078 rbase = GUEST_BASE ? TCG_GUEST_BASE_REG : 0;
1079 if (TARGET_LONG_BITS == 32) {
1080 tcg_out_ext32u(s, TCG_REG_R2, addr_reg);
1081 addr_reg = TCG_REG_R2;
1085 insn = qemu_stx_opc[opc];
1086 if (!HAVE_ISA_2_06 && insn == STDBRX) {
1087 tcg_out32(s, STWBRX | SAB(data_reg, rbase, addr_reg));
1088 tcg_out32(s, ADDI | TAI(TCG_REG_R2, addr_reg, 4));
1089 tcg_out_shri64(s, TCG_REG_R0, data_reg, 32);
1090 tcg_out32(s, STWBRX | SAB(TCG_REG_R0, rbase, TCG_REG_R2));
1092 tcg_out32(s, insn | SAB(data_reg, rbase, addr_reg));
1095 #ifdef CONFIG_SOFTMMU
1096 add_qemu_ldst_label(s, false, opc, data_reg, addr_reg, mem_index,
1097 s->code_ptr, label_ptr);
1101 #define FRAME_SIZE ((int) \
1102 ((8 /* back chain */ \
1105 + 8 /* compiler doubleword */ \
1106 + 8 /* link editor doubleword */ \
1107 + 8 /* TOC save area */ \
1108 + TCG_STATIC_CALL_ARGS_SIZE \
1109 + CPU_TEMP_BUF_NLONGS * sizeof(long) \
1110 + ARRAY_SIZE(tcg_target_callee_save_regs) * 8 \
1113 #define REG_SAVE_BOT (FRAME_SIZE - ARRAY_SIZE(tcg_target_callee_save_regs) * 8)
1115 static void tcg_target_qemu_prologue(TCGContext *s)
1119 tcg_set_frame(s, TCG_REG_CALL_STACK,
1120 REG_SAVE_BOT - CPU_TEMP_BUF_NLONGS * sizeof(long),
1121 CPU_TEMP_BUF_NLONGS * sizeof(long));
1124 /* First emit adhoc function descriptor */
1125 tcg_out64(s, (uint64_t)s->code_ptr + 24); /* entry point */
1126 s->code_ptr += 16; /* skip TOC and environment pointer */
1130 tcg_out32(s, MFSPR | RT(TCG_REG_R0) | LR);
1131 tcg_out32(s, STDU | SAI(TCG_REG_R1, TCG_REG_R1, -FRAME_SIZE));
1132 for (i = 0; i < ARRAY_SIZE(tcg_target_callee_save_regs); ++i) {
1133 tcg_out32(s, STD | SAI(tcg_target_callee_save_regs[i], 1,
1134 REG_SAVE_BOT + i * 8));
1136 tcg_out32(s, STD | SAI(TCG_REG_R0, TCG_REG_R1, FRAME_SIZE + 16));
1138 #ifdef CONFIG_USE_GUEST_BASE
1140 tcg_out_movi(s, TCG_TYPE_I64, TCG_GUEST_BASE_REG, GUEST_BASE);
1141 tcg_regset_set_reg(s->reserved_regs, TCG_GUEST_BASE_REG);
1145 tcg_out_mov(s, TCG_TYPE_PTR, TCG_AREG0, tcg_target_call_iarg_regs[0]);
1146 tcg_out32(s, MTSPR | RS(tcg_target_call_iarg_regs[1]) | CTR);
1147 tcg_out32(s, BCCTR | BO_ALWAYS);
1150 tb_ret_addr = s->code_ptr;
1152 for (i = 0; i < ARRAY_SIZE(tcg_target_callee_save_regs); ++i) {
1153 tcg_out32(s, LD | TAI(tcg_target_callee_save_regs[i], TCG_REG_R1,
1154 REG_SAVE_BOT + i * 8));
1156 tcg_out32(s, LD | TAI(TCG_REG_R0, TCG_REG_R1, FRAME_SIZE + 16));
1157 tcg_out32(s, MTSPR | RS(TCG_REG_R0) | LR);
1158 tcg_out32(s, ADDI | TAI(TCG_REG_R1, TCG_REG_R1, FRAME_SIZE));
1159 tcg_out32(s, BCLR | BO_ALWAYS);
1162 static inline void tcg_out_ld(TCGContext *s, TCGType type, TCGReg ret,
1163 TCGReg arg1, intptr_t arg2)
1167 if (type == TCG_TYPE_I32) {
1168 opi = LWZ, opx = LWZX;
1170 opi = LD, opx = LDX;
1172 tcg_out_mem_long(s, opi, opx, ret, arg1, arg2);
1175 static inline void tcg_out_st(TCGContext *s, TCGType type, TCGReg arg,
1176 TCGReg arg1, intptr_t arg2)
1180 if (type == TCG_TYPE_I32) {
1181 opi = STW, opx = STWX;
1183 opi = STD, opx = STDX;
1185 tcg_out_mem_long(s, opi, opx, arg, arg1, arg2);
1188 static void tcg_out_cmp(TCGContext *s, int cond, TCGArg arg1, TCGArg arg2,
1189 int const_arg2, int cr, TCGType type)
1194 /* Simplify the comparisons below wrt CMPI. */
1195 if (type == TCG_TYPE_I32) {
1196 arg2 = (int32_t)arg2;
1203 if ((int16_t) arg2 == arg2) {
1207 } else if ((uint16_t) arg2 == arg2) {
1222 if ((int16_t) arg2 == arg2) {
1237 if ((uint16_t) arg2 == arg2) {
1250 op |= BF(cr) | ((type == TCG_TYPE_I64) << 21);
1253 tcg_out32(s, op | RA(arg1) | (arg2 & 0xffff));
1256 tcg_out_movi(s, type, TCG_REG_R0, arg2);
1259 tcg_out32(s, op | RA(arg1) | RB(arg2));
1263 static void tcg_out_setcond_eq0(TCGContext *s, TCGType type,
1264 TCGReg dst, TCGReg src)
1266 tcg_out32(s, (type == TCG_TYPE_I64 ? CNTLZD : CNTLZW) | RS(src) | RA(dst));
1267 tcg_out_shri64(s, dst, dst, type == TCG_TYPE_I64 ? 6 : 5);
1270 static void tcg_out_setcond_ne0(TCGContext *s, TCGReg dst, TCGReg src)
1272 /* X != 0 implies X + -1 generates a carry. Extra addition
1273 trickery means: R = X-1 + ~X + C = X-1 + (-X+1) + C = C. */
1275 tcg_out32(s, ADDIC | TAI(dst, src, -1));
1276 tcg_out32(s, SUBFE | TAB(dst, dst, src));
1278 tcg_out32(s, ADDIC | TAI(TCG_REG_R0, src, -1));
1279 tcg_out32(s, SUBFE | TAB(dst, TCG_REG_R0, src));
1283 static TCGReg tcg_gen_setcond_xor(TCGContext *s, TCGReg arg1, TCGArg arg2,
1287 if ((uint32_t)arg2 == arg2) {
1288 tcg_out_xori32(s, TCG_REG_R0, arg1, arg2);
1290 tcg_out_movi(s, TCG_TYPE_I64, TCG_REG_R0, arg2);
1291 tcg_out32(s, XOR | SAB(arg1, TCG_REG_R0, TCG_REG_R0));
1294 tcg_out32(s, XOR | SAB(arg1, TCG_REG_R0, arg2));
1299 static void tcg_out_setcond(TCGContext *s, TCGType type, TCGCond cond,
1300 TCGArg arg0, TCGArg arg1, TCGArg arg2,
1305 /* Ignore high bits of a potential constant arg2. */
1306 if (type == TCG_TYPE_I32) {
1307 arg2 = (uint32_t)arg2;
1310 /* Handle common and trivial cases before handling anything else. */
1314 tcg_out_setcond_eq0(s, type, arg0, arg1);
1317 if (type == TCG_TYPE_I32) {
1318 tcg_out_ext32u(s, TCG_REG_R0, arg1);
1321 tcg_out_setcond_ne0(s, arg0, arg1);
1324 tcg_out32(s, NOR | SAB(arg1, arg0, arg1));
1328 /* Extract the sign bit. */
1329 tcg_out_rld(s, RLDICL, arg0, arg1,
1330 type == TCG_TYPE_I64 ? 1 : 33, 63);
1337 /* If we have ISEL, we can implement everything with 3 or 4 insns.
1338 All other cases below are also at least 3 insns, so speed up the
1339 code generator by not considering them and always using ISEL. */
1343 tcg_out_cmp(s, cond, arg1, arg2, const_arg2, 7, type);
1345 isel = tcg_to_isel[cond];
1347 tcg_out_movi(s, type, arg0, 1);
1349 /* arg0 = (bc ? 0 : 1) */
1350 tab = TAB(arg0, 0, arg0);
1353 /* arg0 = (bc ? 1 : 0) */
1354 tcg_out_movi(s, type, TCG_REG_R0, 0);
1355 tab = TAB(arg0, arg0, TCG_REG_R0);
1357 tcg_out32(s, isel | tab);
1363 arg1 = tcg_gen_setcond_xor(s, arg1, arg2, const_arg2);
1364 tcg_out_setcond_eq0(s, type, arg0, arg1);
1368 arg1 = tcg_gen_setcond_xor(s, arg1, arg2, const_arg2);
1369 /* Discard the high bits only once, rather than both inputs. */
1370 if (type == TCG_TYPE_I32) {
1371 tcg_out_ext32u(s, TCG_REG_R0, arg1);
1374 tcg_out_setcond_ne0(s, arg0, arg1);
1392 crop = CRNOR | BT(7, CR_EQ) | BA(7, CR_LT) | BB(7, CR_LT);
1398 crop = CRNOR | BT(7, CR_EQ) | BA(7, CR_GT) | BB(7, CR_GT);
1400 tcg_out_cmp(s, cond, arg1, arg2, const_arg2, 7, type);
1404 tcg_out32(s, MFOCRF | RT(TCG_REG_R0) | FXM(7));
1405 tcg_out_rlw(s, RLWINM, arg0, TCG_REG_R0, sh, 31, 31);
1413 static void tcg_out_bc(TCGContext *s, int bc, int label_index)
1415 TCGLabel *l = &s->labels[label_index];
1418 tcg_out32(s, bc | reloc_pc14_val(s->code_ptr, l->u.value));
1420 tcg_out_reloc(s, s->code_ptr, R_PPC_REL14, label_index, 0);
1421 tcg_out_bc_noaddr(s, bc);
1425 static void tcg_out_brcond(TCGContext *s, TCGCond cond,
1426 TCGArg arg1, TCGArg arg2, int const_arg2,
1427 int label_index, TCGType type)
1429 tcg_out_cmp(s, cond, arg1, arg2, const_arg2, 7, type);
1430 tcg_out_bc(s, tcg_to_bc[cond], label_index);
1433 static void tcg_out_movcond(TCGContext *s, TCGType type, TCGCond cond,
1434 TCGArg dest, TCGArg c1, TCGArg c2, TCGArg v1,
1435 TCGArg v2, bool const_c2)
1437 /* If for some reason both inputs are zero, don't produce bad code. */
1438 if (v1 == 0 && v2 == 0) {
1439 tcg_out_movi(s, type, dest, 0);
1443 tcg_out_cmp(s, cond, c1, c2, const_c2, 7, type);
1446 int isel = tcg_to_isel[cond];
1448 /* Swap the V operands if the operation indicates inversion. */
1455 /* V1 == 0 is handled by isel; V2 == 0 must be handled by hand. */
1457 tcg_out_movi(s, type, TCG_REG_R0, 0);
1459 tcg_out32(s, isel | TAB(dest, v1, v2));
1462 cond = tcg_invert_cond(cond);
1464 } else if (dest != v1) {
1466 tcg_out_movi(s, type, dest, 0);
1468 tcg_out_mov(s, type, dest, v1);
1471 /* Branch forward over one insn */
1472 tcg_out32(s, tcg_to_bc[cond] | 8);
1474 tcg_out_movi(s, type, dest, 0);
1476 tcg_out_mov(s, type, dest, v2);
1481 void ppc_tb_set_jmp_target(unsigned long jmp_addr, unsigned long addr)
1484 unsigned long patch_size;
1486 s.code_ptr = (uint8_t *) jmp_addr;
1487 tcg_out_b(&s, 0, addr);
1488 patch_size = s.code_ptr - (uint8_t *) jmp_addr;
1489 flush_icache_range(jmp_addr, jmp_addr + patch_size);
1492 static void tcg_out_op(TCGContext *s, TCGOpcode opc, const TCGArg *args,
1493 const int *const_args)
1499 case INDEX_op_exit_tb:
1500 tcg_out_movi(s, TCG_TYPE_I64, TCG_REG_R3, args[0]);
1501 tcg_out_b(s, 0, (tcg_target_long)tb_ret_addr);
1503 case INDEX_op_goto_tb:
1504 if (s->tb_jmp_offset) {
1505 /* Direct jump method. */
1506 s->tb_jmp_offset[args[0]] = s->code_ptr - s->code_buf;
1509 /* Indirect jump method. */
1512 s->tb_next_offset[args[0]] = s->code_ptr - s->code_buf;
1516 TCGLabel *l = &s->labels[args[0]];
1519 tcg_out_b(s, 0, l->u.value);
1521 tcg_out_reloc(s, s->code_ptr, R_PPC_REL24, args[0], 0);
1522 tcg_out_b_noaddr(s, B);
1527 tcg_out_call(s, args[0], const_args[0]);
1529 case INDEX_op_movi_i32:
1530 tcg_out_movi(s, TCG_TYPE_I32, args[0], args[1]);
1532 case INDEX_op_movi_i64:
1533 tcg_out_movi(s, TCG_TYPE_I64, args[0], args[1]);
1535 case INDEX_op_ld8u_i32:
1536 case INDEX_op_ld8u_i64:
1537 tcg_out_mem_long(s, LBZ, LBZX, args[0], args[1], args[2]);
1539 case INDEX_op_ld8s_i32:
1540 case INDEX_op_ld8s_i64:
1541 tcg_out_mem_long(s, LBZ, LBZX, args[0], args[1], args[2]);
1542 tcg_out32(s, EXTSB | RS(args[0]) | RA(args[0]));
1544 case INDEX_op_ld16u_i32:
1545 case INDEX_op_ld16u_i64:
1546 tcg_out_mem_long(s, LHZ, LHZX, args[0], args[1], args[2]);
1548 case INDEX_op_ld16s_i32:
1549 case INDEX_op_ld16s_i64:
1550 tcg_out_mem_long(s, LHA, LHAX, args[0], args[1], args[2]);
1552 case INDEX_op_ld_i32:
1553 case INDEX_op_ld32u_i64:
1554 tcg_out_mem_long(s, LWZ, LWZX, args[0], args[1], args[2]);
1556 case INDEX_op_ld32s_i64:
1557 tcg_out_mem_long(s, LWA, LWAX, args[0], args[1], args[2]);
1559 case INDEX_op_ld_i64:
1560 tcg_out_mem_long(s, LD, LDX, args[0], args[1], args[2]);
1562 case INDEX_op_st8_i32:
1563 case INDEX_op_st8_i64:
1564 tcg_out_mem_long(s, STB, STBX, args[0], args[1], args[2]);
1566 case INDEX_op_st16_i32:
1567 case INDEX_op_st16_i64:
1568 tcg_out_mem_long(s, STH, STHX, args[0], args[1], args[2]);
1570 case INDEX_op_st_i32:
1571 case INDEX_op_st32_i64:
1572 tcg_out_mem_long(s, STW, STWX, args[0], args[1], args[2]);
1574 case INDEX_op_st_i64:
1575 tcg_out_mem_long(s, STD, STDX, args[0], args[1], args[2]);
1578 case INDEX_op_add_i32:
1579 a0 = args[0], a1 = args[1], a2 = args[2];
1580 if (const_args[2]) {
1582 tcg_out_mem_long(s, ADDI, ADD, a0, a1, (int32_t)a2);
1584 tcg_out32(s, ADD | TAB(a0, a1, a2));
1587 case INDEX_op_sub_i32:
1588 a0 = args[0], a1 = args[1], a2 = args[2];
1589 if (const_args[1]) {
1590 if (const_args[2]) {
1591 tcg_out_movi(s, TCG_TYPE_I32, a0, a1 - a2);
1593 tcg_out32(s, SUBFIC | TAI(a0, a2, a1));
1595 } else if (const_args[2]) {
1599 tcg_out32(s, SUBF | TAB(a0, a2, a1));
1603 case INDEX_op_and_i32:
1604 a0 = args[0], a1 = args[1], a2 = args[2];
1605 if (const_args[2]) {
1606 tcg_out_andi32(s, a0, a1, a2);
1608 tcg_out32(s, AND | SAB(a1, a0, a2));
1611 case INDEX_op_and_i64:
1612 a0 = args[0], a1 = args[1], a2 = args[2];
1613 if (const_args[2]) {
1614 tcg_out_andi64(s, a0, a1, a2);
1616 tcg_out32(s, AND | SAB(a1, a0, a2));
1619 case INDEX_op_or_i64:
1620 case INDEX_op_or_i32:
1621 a0 = args[0], a1 = args[1], a2 = args[2];
1622 if (const_args[2]) {
1623 tcg_out_ori32(s, a0, a1, a2);
1625 tcg_out32(s, OR | SAB(a1, a0, a2));
1628 case INDEX_op_xor_i64:
1629 case INDEX_op_xor_i32:
1630 a0 = args[0], a1 = args[1], a2 = args[2];
1631 if (const_args[2]) {
1632 tcg_out_xori32(s, a0, a1, a2);
1634 tcg_out32(s, XOR | SAB(a1, a0, a2));
1637 case INDEX_op_andc_i32:
1638 a0 = args[0], a1 = args[1], a2 = args[2];
1639 if (const_args[2]) {
1640 tcg_out_andi32(s, a0, a1, ~a2);
1642 tcg_out32(s, ANDC | SAB(a1, a0, a2));
1645 case INDEX_op_andc_i64:
1646 a0 = args[0], a1 = args[1], a2 = args[2];
1647 if (const_args[2]) {
1648 tcg_out_andi64(s, a0, a1, ~a2);
1650 tcg_out32(s, ANDC | SAB(a1, a0, a2));
1653 case INDEX_op_orc_i32:
1654 if (const_args[2]) {
1655 tcg_out_ori32(s, args[0], args[1], ~args[2]);
1659 case INDEX_op_orc_i64:
1660 tcg_out32(s, ORC | SAB(args[1], args[0], args[2]));
1662 case INDEX_op_eqv_i32:
1663 if (const_args[2]) {
1664 tcg_out_xori32(s, args[0], args[1], ~args[2]);
1668 case INDEX_op_eqv_i64:
1669 tcg_out32(s, EQV | SAB(args[1], args[0], args[2]));
1671 case INDEX_op_nand_i32:
1672 case INDEX_op_nand_i64:
1673 tcg_out32(s, NAND | SAB(args[1], args[0], args[2]));
1675 case INDEX_op_nor_i32:
1676 case INDEX_op_nor_i64:
1677 tcg_out32(s, NOR | SAB(args[1], args[0], args[2]));
1680 case INDEX_op_mul_i32:
1681 a0 = args[0], a1 = args[1], a2 = args[2];
1682 if (const_args[2]) {
1683 tcg_out32(s, MULLI | TAI(a0, a1, a2));
1685 tcg_out32(s, MULLW | TAB(a0, a1, a2));
1689 case INDEX_op_div_i32:
1690 tcg_out32(s, DIVW | TAB(args[0], args[1], args[2]));
1693 case INDEX_op_divu_i32:
1694 tcg_out32(s, DIVWU | TAB(args[0], args[1], args[2]));
1697 case INDEX_op_shl_i32:
1698 if (const_args[2]) {
1699 tcg_out_rlw(s, RLWINM, args[0], args[1], args[2], 0, 31 - args[2]);
1701 tcg_out32(s, SLW | SAB(args[1], args[0], args[2]));
1704 case INDEX_op_shr_i32:
1705 if (const_args[2]) {
1706 tcg_out_rlw(s, RLWINM, args[0], args[1], 32 - args[2], args[2], 31);
1708 tcg_out32(s, SRW | SAB(args[1], args[0], args[2]));
1711 case INDEX_op_sar_i32:
1712 if (const_args[2]) {
1713 tcg_out32(s, SRAWI | RS(args[1]) | RA(args[0]) | SH(args[2]));
1715 tcg_out32(s, SRAW | SAB(args[1], args[0], args[2]));
1718 case INDEX_op_rotl_i32:
1719 if (const_args[2]) {
1720 tcg_out_rlw(s, RLWINM, args[0], args[1], args[2], 0, 31);
1722 tcg_out32(s, RLWNM | SAB(args[1], args[0], args[2])
1726 case INDEX_op_rotr_i32:
1727 if (const_args[2]) {
1728 tcg_out_rlw(s, RLWINM, args[0], args[1], 32 - args[2], 0, 31);
1730 tcg_out32(s, SUBFIC | TAI(TCG_REG_R0, args[2], 32));
1731 tcg_out32(s, RLWNM | SAB(args[1], args[0], TCG_REG_R0)
1736 case INDEX_op_brcond_i32:
1737 tcg_out_brcond(s, args[2], args[0], args[1], const_args[1],
1738 args[3], TCG_TYPE_I32);
1741 case INDEX_op_brcond_i64:
1742 tcg_out_brcond(s, args[2], args[0], args[1], const_args[1],
1743 args[3], TCG_TYPE_I64);
1746 case INDEX_op_neg_i32:
1747 case INDEX_op_neg_i64:
1748 tcg_out32(s, NEG | RT(args[0]) | RA(args[1]));
1751 case INDEX_op_not_i32:
1752 case INDEX_op_not_i64:
1753 tcg_out32(s, NOR | SAB(args[1], args[0], args[1]));
1756 case INDEX_op_add_i64:
1757 a0 = args[0], a1 = args[1], a2 = args[2];
1758 if (const_args[2]) {
1760 tcg_out_mem_long(s, ADDI, ADD, a0, a1, a2);
1762 tcg_out32(s, ADD | TAB(a0, a1, a2));
1765 case INDEX_op_sub_i64:
1766 a0 = args[0], a1 = args[1], a2 = args[2];
1767 if (const_args[1]) {
1768 if (const_args[2]) {
1769 tcg_out_movi(s, TCG_TYPE_I64, a0, a1 - a2);
1771 tcg_out32(s, SUBFIC | TAI(a0, a2, a1));
1773 } else if (const_args[2]) {
1777 tcg_out32(s, SUBF | TAB(a0, a2, a1));
1781 case INDEX_op_shl_i64:
1782 if (const_args[2]) {
1783 tcg_out_shli64(s, args[0], args[1], args[2]);
1785 tcg_out32(s, SLD | SAB(args[1], args[0], args[2]));
1788 case INDEX_op_shr_i64:
1789 if (const_args[2]) {
1790 tcg_out_shri64(s, args[0], args[1], args[2]);
1792 tcg_out32(s, SRD | SAB(args[1], args[0], args[2]));
1795 case INDEX_op_sar_i64:
1796 if (const_args[2]) {
1797 int sh = SH(args[2] & 0x1f) | (((args[2] >> 5) & 1) << 1);
1798 tcg_out32(s, SRADI | RA(args[0]) | RS(args[1]) | sh);
1800 tcg_out32(s, SRAD | SAB(args[1], args[0], args[2]));
1803 case INDEX_op_rotl_i64:
1804 if (const_args[2]) {
1805 tcg_out_rld(s, RLDICL, args[0], args[1], args[2], 0);
1807 tcg_out32(s, RLDCL | SAB(args[1], args[0], args[2]) | MB64(0));
1810 case INDEX_op_rotr_i64:
1811 if (const_args[2]) {
1812 tcg_out_rld(s, RLDICL, args[0], args[1], 64 - args[2], 0);
1814 tcg_out32(s, SUBFIC | TAI(TCG_REG_R0, args[2], 64));
1815 tcg_out32(s, RLDCL | SAB(args[1], args[0], TCG_REG_R0) | MB64(0));
1819 case INDEX_op_mul_i64:
1820 a0 = args[0], a1 = args[1], a2 = args[2];
1821 if (const_args[2]) {
1822 tcg_out32(s, MULLI | TAI(a0, a1, a2));
1824 tcg_out32(s, MULLD | TAB(a0, a1, a2));
1827 case INDEX_op_div_i64:
1828 tcg_out32(s, DIVD | TAB(args[0], args[1], args[2]));
1830 case INDEX_op_divu_i64:
1831 tcg_out32(s, DIVDU | TAB(args[0], args[1], args[2]));
1834 case INDEX_op_qemu_ld_i32:
1835 case INDEX_op_qemu_ld_i64:
1836 tcg_out_qemu_ld(s, args[0], args[1], args[2], args[3]);
1838 case INDEX_op_qemu_st_i32:
1839 case INDEX_op_qemu_st_i64:
1840 tcg_out_qemu_st(s, args[0], args[1], args[2], args[3]);
1843 case INDEX_op_ext8s_i32:
1844 case INDEX_op_ext8s_i64:
1847 case INDEX_op_ext16s_i32:
1848 case INDEX_op_ext16s_i64:
1851 case INDEX_op_ext32s_i64:
1855 tcg_out32(s, c | RS(args[1]) | RA(args[0]));
1858 case INDEX_op_setcond_i32:
1859 tcg_out_setcond(s, TCG_TYPE_I32, args[3], args[0], args[1], args[2],
1862 case INDEX_op_setcond_i64:
1863 tcg_out_setcond(s, TCG_TYPE_I64, args[3], args[0], args[1], args[2],
1867 case INDEX_op_bswap16_i32:
1868 case INDEX_op_bswap16_i64:
1869 a0 = args[0], a1 = args[1];
1872 /* a0 = (a1 r<< 24) & 0xff # 000c */
1873 tcg_out_rlw(s, RLWINM, a0, a1, 24, 24, 31);
1874 /* a0 = (a0 & ~0xff00) | (a1 r<< 8) & 0xff00 # 00dc */
1875 tcg_out_rlw(s, RLWIMI, a0, a1, 8, 16, 23);
1877 /* r0 = (a1 r<< 8) & 0xff00 # 00d0 */
1878 tcg_out_rlw(s, RLWINM, TCG_REG_R0, a1, 8, 16, 23);
1879 /* a0 = (a1 r<< 24) & 0xff # 000c */
1880 tcg_out_rlw(s, RLWINM, a0, a1, 24, 24, 31);
1881 /* a0 = a0 | r0 # 00dc */
1882 tcg_out32(s, OR | SAB(TCG_REG_R0, a0, a0));
1886 case INDEX_op_bswap32_i32:
1887 case INDEX_op_bswap32_i64:
1888 /* Stolen from gcc's builtin_bswap32 */
1890 a0 = args[0] == a1 ? TCG_REG_R0 : args[0];
1892 /* a1 = args[1] # abcd */
1893 /* a0 = rotate_left (a1, 8) # bcda */
1894 tcg_out_rlw(s, RLWINM, a0, a1, 8, 0, 31);
1895 /* a0 = (a0 & ~0xff000000) | ((a1 r<< 24) & 0xff000000) # dcda */
1896 tcg_out_rlw(s, RLWIMI, a0, a1, 24, 0, 7);
1897 /* a0 = (a0 & ~0x0000ff00) | ((a1 r<< 24) & 0x0000ff00) # dcba */
1898 tcg_out_rlw(s, RLWIMI, a0, a1, 24, 16, 23);
1900 if (a0 == TCG_REG_R0) {
1901 tcg_out_mov(s, TCG_TYPE_I64, args[0], a0);
1905 case INDEX_op_bswap64_i64:
1906 a0 = args[0], a1 = args[1], a2 = TCG_REG_R0;
1912 /* a1 = # abcd efgh */
1913 /* a0 = rl32(a1, 8) # 0000 fghe */
1914 tcg_out_rlw(s, RLWINM, a0, a1, 8, 0, 31);
1915 /* a0 = dep(a0, rl32(a1, 24), 0xff000000) # 0000 hghe */
1916 tcg_out_rlw(s, RLWIMI, a0, a1, 24, 0, 7);
1917 /* a0 = dep(a0, rl32(a1, 24), 0x0000ff00) # 0000 hgfe */
1918 tcg_out_rlw(s, RLWIMI, a0, a1, 24, 16, 23);
1920 /* a0 = rl64(a0, 32) # hgfe 0000 */
1921 /* a2 = rl64(a1, 32) # efgh abcd */
1922 tcg_out_rld(s, RLDICL, a0, a0, 32, 0);
1923 tcg_out_rld(s, RLDICL, a2, a1, 32, 0);
1925 /* a0 = dep(a0, rl32(a2, 8), 0xffffffff) # hgfe bcda */
1926 tcg_out_rlw(s, RLWIMI, a0, a2, 8, 0, 31);
1927 /* a0 = dep(a0, rl32(a2, 24), 0xff000000) # hgfe dcda */
1928 tcg_out_rlw(s, RLWIMI, a0, a2, 24, 0, 7);
1929 /* a0 = dep(a0, rl32(a2, 24), 0x0000ff00) # hgfe dcba */
1930 tcg_out_rlw(s, RLWIMI, a0, a2, 24, 16, 23);
1933 tcg_out_mov(s, TCG_TYPE_I64, args[0], a0);
1937 case INDEX_op_deposit_i32:
1938 if (const_args[2]) {
1939 uint32_t mask = ((2u << (args[4] - 1)) - 1) << args[3];
1940 tcg_out_andi32(s, args[0], args[0], ~mask);
1942 tcg_out_rlw(s, RLWIMI, args[0], args[2], args[3],
1943 32 - args[3] - args[4], 31 - args[3]);
1946 case INDEX_op_deposit_i64:
1947 if (const_args[2]) {
1948 uint64_t mask = ((2ull << (args[4] - 1)) - 1) << args[3];
1949 tcg_out_andi64(s, args[0], args[0], ~mask);
1951 tcg_out_rld(s, RLDIMI, args[0], args[2], args[3],
1952 64 - args[3] - args[4]);
1956 case INDEX_op_movcond_i32:
1957 tcg_out_movcond(s, TCG_TYPE_I32, args[5], args[0], args[1], args[2],
1958 args[3], args[4], const_args[2]);
1960 case INDEX_op_movcond_i64:
1961 tcg_out_movcond(s, TCG_TYPE_I64, args[5], args[0], args[1], args[2],
1962 args[3], args[4], const_args[2]);
1965 case INDEX_op_add2_i64:
1966 /* Note that the CA bit is defined based on the word size of the
1967 environment. So in 64-bit mode it's always carry-out of bit 63.
1968 The fallback code using deposit works just as well for 32-bit. */
1969 a0 = args[0], a1 = args[1];
1970 if (a0 == args[3] || (!const_args[5] && a0 == args[5])) {
1973 if (const_args[4]) {
1974 tcg_out32(s, ADDIC | TAI(a0, args[2], args[4]));
1976 tcg_out32(s, ADDC | TAB(a0, args[2], args[4]));
1978 if (const_args[5]) {
1979 tcg_out32(s, (args[5] ? ADDME : ADDZE) | RT(a1) | RA(args[3]));
1981 tcg_out32(s, ADDE | TAB(a1, args[3], args[5]));
1983 if (a0 != args[0]) {
1984 tcg_out_mov(s, TCG_TYPE_I64, args[0], a0);
1988 case INDEX_op_sub2_i64:
1989 a0 = args[0], a1 = args[1];
1990 if (a0 == args[5] || (!const_args[4] && a0 == args[4])) {
1993 if (const_args[2]) {
1994 tcg_out32(s, SUBFIC | TAI(a0, args[3], args[2]));
1996 tcg_out32(s, SUBFC | TAB(a0, args[3], args[2]));
1998 if (const_args[4]) {
1999 tcg_out32(s, (args[4] ? SUBFME : SUBFZE) | RT(a1) | RA(args[5]));
2001 tcg_out32(s, SUBFE | TAB(a1, args[5], args[4]));
2003 if (a0 != args[0]) {
2004 tcg_out_mov(s, TCG_TYPE_I64, args[0], a0);
2008 case INDEX_op_muluh_i64:
2009 tcg_out32(s, MULHDU | TAB(args[0], args[1], args[2]));
2011 case INDEX_op_mulsh_i64:
2012 tcg_out32(s, MULHD | TAB(args[0], args[1], args[2]));
2021 static const TCGTargetOpDef ppc_op_defs[] = {
2022 { INDEX_op_exit_tb, { } },
2023 { INDEX_op_goto_tb, { } },
2024 { INDEX_op_call, { "ri" } },
2025 { INDEX_op_br, { } },
2027 { INDEX_op_mov_i32, { "r", "r" } },
2028 { INDEX_op_mov_i64, { "r", "r" } },
2029 { INDEX_op_movi_i32, { "r" } },
2030 { INDEX_op_movi_i64, { "r" } },
2032 { INDEX_op_ld8u_i32, { "r", "r" } },
2033 { INDEX_op_ld8s_i32, { "r", "r" } },
2034 { INDEX_op_ld16u_i32, { "r", "r" } },
2035 { INDEX_op_ld16s_i32, { "r", "r" } },
2036 { INDEX_op_ld_i32, { "r", "r" } },
2037 { INDEX_op_ld_i64, { "r", "r" } },
2038 { INDEX_op_st8_i32, { "r", "r" } },
2039 { INDEX_op_st8_i64, { "r", "r" } },
2040 { INDEX_op_st16_i32, { "r", "r" } },
2041 { INDEX_op_st16_i64, { "r", "r" } },
2042 { INDEX_op_st_i32, { "r", "r" } },
2043 { INDEX_op_st_i64, { "r", "r" } },
2044 { INDEX_op_st32_i64, { "r", "r" } },
2046 { INDEX_op_ld8u_i64, { "r", "r" } },
2047 { INDEX_op_ld8s_i64, { "r", "r" } },
2048 { INDEX_op_ld16u_i64, { "r", "r" } },
2049 { INDEX_op_ld16s_i64, { "r", "r" } },
2050 { INDEX_op_ld32u_i64, { "r", "r" } },
2051 { INDEX_op_ld32s_i64, { "r", "r" } },
2053 { INDEX_op_add_i32, { "r", "r", "ri" } },
2054 { INDEX_op_mul_i32, { "r", "r", "rI" } },
2055 { INDEX_op_div_i32, { "r", "r", "r" } },
2056 { INDEX_op_divu_i32, { "r", "r", "r" } },
2057 { INDEX_op_sub_i32, { "r", "rI", "ri" } },
2058 { INDEX_op_and_i32, { "r", "r", "ri" } },
2059 { INDEX_op_or_i32, { "r", "r", "ri" } },
2060 { INDEX_op_xor_i32, { "r", "r", "ri" } },
2061 { INDEX_op_andc_i32, { "r", "r", "ri" } },
2062 { INDEX_op_orc_i32, { "r", "r", "ri" } },
2063 { INDEX_op_eqv_i32, { "r", "r", "ri" } },
2064 { INDEX_op_nand_i32, { "r", "r", "r" } },
2065 { INDEX_op_nor_i32, { "r", "r", "r" } },
2067 { INDEX_op_shl_i32, { "r", "r", "ri" } },
2068 { INDEX_op_shr_i32, { "r", "r", "ri" } },
2069 { INDEX_op_sar_i32, { "r", "r", "ri" } },
2070 { INDEX_op_rotl_i32, { "r", "r", "ri" } },
2071 { INDEX_op_rotr_i32, { "r", "r", "ri" } },
2073 { INDEX_op_brcond_i32, { "r", "ri" } },
2074 { INDEX_op_brcond_i64, { "r", "ri" } },
2076 { INDEX_op_neg_i32, { "r", "r" } },
2077 { INDEX_op_not_i32, { "r", "r" } },
2079 { INDEX_op_add_i64, { "r", "r", "rT" } },
2080 { INDEX_op_sub_i64, { "r", "rI", "rT" } },
2081 { INDEX_op_and_i64, { "r", "r", "ri" } },
2082 { INDEX_op_or_i64, { "r", "r", "rU" } },
2083 { INDEX_op_xor_i64, { "r", "r", "rU" } },
2084 { INDEX_op_andc_i64, { "r", "r", "ri" } },
2085 { INDEX_op_orc_i64, { "r", "r", "r" } },
2086 { INDEX_op_eqv_i64, { "r", "r", "r" } },
2087 { INDEX_op_nand_i64, { "r", "r", "r" } },
2088 { INDEX_op_nor_i64, { "r", "r", "r" } },
2090 { INDEX_op_shl_i64, { "r", "r", "ri" } },
2091 { INDEX_op_shr_i64, { "r", "r", "ri" } },
2092 { INDEX_op_sar_i64, { "r", "r", "ri" } },
2093 { INDEX_op_rotl_i64, { "r", "r", "ri" } },
2094 { INDEX_op_rotr_i64, { "r", "r", "ri" } },
2096 { INDEX_op_mul_i64, { "r", "r", "rI" } },
2097 { INDEX_op_div_i64, { "r", "r", "r" } },
2098 { INDEX_op_divu_i64, { "r", "r", "r" } },
2100 { INDEX_op_neg_i64, { "r", "r" } },
2101 { INDEX_op_not_i64, { "r", "r" } },
2103 { INDEX_op_qemu_ld_i32, { "r", "L" } },
2104 { INDEX_op_qemu_ld_i64, { "r", "L" } },
2105 { INDEX_op_qemu_st_i32, { "S", "S" } },
2106 { INDEX_op_qemu_st_i64, { "S", "S" } },
2108 { INDEX_op_ext8s_i32, { "r", "r" } },
2109 { INDEX_op_ext16s_i32, { "r", "r" } },
2110 { INDEX_op_ext8s_i64, { "r", "r" } },
2111 { INDEX_op_ext16s_i64, { "r", "r" } },
2112 { INDEX_op_ext32s_i64, { "r", "r" } },
2114 { INDEX_op_setcond_i32, { "r", "r", "ri" } },
2115 { INDEX_op_setcond_i64, { "r", "r", "ri" } },
2116 { INDEX_op_movcond_i32, { "r", "r", "ri", "rZ", "rZ" } },
2117 { INDEX_op_movcond_i64, { "r", "r", "ri", "rZ", "rZ" } },
2119 { INDEX_op_bswap16_i32, { "r", "r" } },
2120 { INDEX_op_bswap16_i64, { "r", "r" } },
2121 { INDEX_op_bswap32_i32, { "r", "r" } },
2122 { INDEX_op_bswap32_i64, { "r", "r" } },
2123 { INDEX_op_bswap64_i64, { "r", "r" } },
2125 { INDEX_op_deposit_i32, { "r", "0", "rZ" } },
2126 { INDEX_op_deposit_i64, { "r", "0", "rZ" } },
2128 { INDEX_op_add2_i64, { "r", "r", "r", "r", "rI", "rZM" } },
2129 { INDEX_op_sub2_i64, { "r", "r", "rI", "r", "rZM", "r" } },
2130 { INDEX_op_mulsh_i64, { "r", "r", "r" } },
2131 { INDEX_op_muluh_i64, { "r", "r", "r" } },
2136 static void tcg_target_init(TCGContext *s)
2138 unsigned long hwcap = qemu_getauxval(AT_HWCAP);
2139 if (hwcap & PPC_FEATURE_ARCH_2_06) {
2140 have_isa_2_06 = true;
2143 tcg_regset_set32(tcg_target_available_regs[TCG_TYPE_I32], 0, 0xffffffff);
2144 tcg_regset_set32(tcg_target_available_regs[TCG_TYPE_I64], 0, 0xffffffff);
2145 tcg_regset_set32(tcg_target_call_clobber_regs, 0,
2155 (1 << TCG_REG_R10) |
2156 (1 << TCG_REG_R11) |
2157 (1 << TCG_REG_R12));
2159 tcg_regset_clear(s->reserved_regs);
2160 tcg_regset_set_reg(s->reserved_regs, TCG_REG_R0); /* tcg temp */
2161 tcg_regset_set_reg(s->reserved_regs, TCG_REG_R1); /* stack pointer */
2162 tcg_regset_set_reg(s->reserved_regs, TCG_REG_R2); /* mem temp */
2164 tcg_regset_set_reg(s->reserved_regs, TCG_REG_R11); /* ??? */
2166 tcg_regset_set_reg(s->reserved_regs, TCG_REG_R13); /* thread pointer */
2168 tcg_add_target_add_op_defs(ppc_op_defs);
2173 DebugFrameFDEHeader fde;
2174 uint8_t fde_def_cfa[4];
2175 uint8_t fde_reg_ofs[ARRAY_SIZE(tcg_target_callee_save_regs) * 2 + 3];
2178 /* We're expecting a 2 byte uleb128 encoded value. */
2179 QEMU_BUILD_BUG_ON(FRAME_SIZE >= (1 << 14));
2181 #define ELF_HOST_MACHINE EM_PPC64
2183 static DebugFrame debug_frame = {
2184 .cie.len = sizeof(DebugFrameCIE)-4, /* length after .len member */
2187 .cie.code_align = 1,
2188 .cie.data_align = 0x78, /* sleb128 -8 */
2189 .cie.return_column = 65,
2191 /* Total FDE size does not include the "len" member. */
2192 .fde.len = sizeof(DebugFrame) - offsetof(DebugFrame, fde.cie_offset),
2195 12, 1, /* DW_CFA_def_cfa r1, ... */
2196 (FRAME_SIZE & 0x7f) | 0x80, /* ... uleb128 FRAME_SIZE */
2200 0x11, 65, 0x7e, /* DW_CFA_offset_extended_sf, lr, 16 */
2204 void tcg_register_jit(void *buf, size_t buf_size)
2206 uint8_t *p = &debug_frame.fde_reg_ofs[3];
2209 for (i = 0; i < ARRAY_SIZE(tcg_target_callee_save_regs); ++i, p += 2) {
2210 p[0] = 0x80 + tcg_target_callee_save_regs[i];
2211 p[1] = (FRAME_SIZE - (REG_SAVE_BOT + i * 8)) / 8;
2214 debug_frame.fde.func_start = (tcg_target_long) buf;
2215 debug_frame.fde.func_len = buf_size;
2217 tcg_register_jit_int(buf, buf_size, &debug_frame, sizeof(debug_frame));