xref: /openbmc/qemu/tcg/ppc/tcg-target.c.inc (revision 835fde4a)
1/*
2 * Tiny Code Generator for QEMU
3 *
4 * Copyright (c) 2008 Fabrice Bellard
5 *
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:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
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
22 * THE SOFTWARE.
23 */
24
25#include "elf.h"
26#include "../tcg-pool.c.inc"
27
28#if defined _CALL_DARWIN || defined __APPLE__
29#define TCG_TARGET_CALL_DARWIN
30#endif
31#ifdef _CALL_SYSV
32# define TCG_TARGET_CALL_ALIGN_ARGS   1
33#endif
34
35/* For some memory operations, we need a scratch that isn't R0.  For the AIX
36   calling convention, we can re-use the TOC register since we'll be reloading
37   it at every call.  Otherwise R12 will do nicely as neither a call-saved
38   register nor a parameter register.  */
39#ifdef _CALL_AIX
40# define TCG_REG_TMP1   TCG_REG_R2
41#else
42# define TCG_REG_TMP1   TCG_REG_R12
43#endif
44
45#define TCG_VEC_TMP1    TCG_REG_V0
46#define TCG_VEC_TMP2    TCG_REG_V1
47
48#define TCG_REG_TB     TCG_REG_R31
49#define USE_REG_TB     (TCG_TARGET_REG_BITS == 64)
50
51/* Shorthand for size of a pointer.  Avoid promotion to unsigned.  */
52#define SZP  ((int)sizeof(void *))
53
54/* Shorthand for size of a register.  */
55#define SZR  (TCG_TARGET_REG_BITS / 8)
56
57#define TCG_CT_CONST_S16  0x100
58#define TCG_CT_CONST_U16  0x200
59#define TCG_CT_CONST_S32  0x400
60#define TCG_CT_CONST_U32  0x800
61#define TCG_CT_CONST_ZERO 0x1000
62#define TCG_CT_CONST_MONE 0x2000
63#define TCG_CT_CONST_WSZ  0x4000
64
65#define ALL_GENERAL_REGS  0xffffffffu
66#define ALL_VECTOR_REGS   0xffffffff00000000ull
67
68#ifdef CONFIG_SOFTMMU
69#define ALL_QLOAD_REGS \
70    (ALL_GENERAL_REGS & \
71     ~((1 << TCG_REG_R3) | (1 << TCG_REG_R4) | (1 << TCG_REG_R5)))
72#define ALL_QSTORE_REGS \
73    (ALL_GENERAL_REGS & ~((1 << TCG_REG_R3) | (1 << TCG_REG_R4) | \
74                          (1 << TCG_REG_R5) | (1 << TCG_REG_R6)))
75#else
76#define ALL_QLOAD_REGS  (ALL_GENERAL_REGS & ~(1 << TCG_REG_R3))
77#define ALL_QSTORE_REGS ALL_QLOAD_REGS
78#endif
79
80TCGPowerISA have_isa;
81static bool have_isel;
82bool have_altivec;
83bool have_vsx;
84
85#ifndef CONFIG_SOFTMMU
86#define TCG_GUEST_BASE_REG 30
87#endif
88
89#ifdef CONFIG_DEBUG_TCG
90static const char tcg_target_reg_names[TCG_TARGET_NB_REGS][4] = {
91    "r0",  "r1",  "r2",  "r3",  "r4",  "r5",  "r6",  "r7",
92    "r8",  "r9",  "r10", "r11", "r12", "r13", "r14", "r15",
93    "r16", "r17", "r18", "r19", "r20", "r21", "r22", "r23",
94    "r24", "r25", "r26", "r27", "r28", "r29", "r30", "r31",
95    "v0",  "v1",  "v2",  "v3",  "v4",  "v5",  "v6",  "v7",
96    "v8",  "v9",  "v10", "v11", "v12", "v13", "v14", "v15",
97    "v16", "v17", "v18", "v19", "v20", "v21", "v22", "v23",
98    "v24", "v25", "v26", "v27", "v28", "v29", "v30", "v31",
99};
100#endif
101
102static const int tcg_target_reg_alloc_order[] = {
103    TCG_REG_R14,  /* call saved registers */
104    TCG_REG_R15,
105    TCG_REG_R16,
106    TCG_REG_R17,
107    TCG_REG_R18,
108    TCG_REG_R19,
109    TCG_REG_R20,
110    TCG_REG_R21,
111    TCG_REG_R22,
112    TCG_REG_R23,
113    TCG_REG_R24,
114    TCG_REG_R25,
115    TCG_REG_R26,
116    TCG_REG_R27,
117    TCG_REG_R28,
118    TCG_REG_R29,
119    TCG_REG_R30,
120    TCG_REG_R31,
121    TCG_REG_R12,  /* call clobbered, non-arguments */
122    TCG_REG_R11,
123    TCG_REG_R2,
124    TCG_REG_R13,
125    TCG_REG_R10,  /* call clobbered, arguments */
126    TCG_REG_R9,
127    TCG_REG_R8,
128    TCG_REG_R7,
129    TCG_REG_R6,
130    TCG_REG_R5,
131    TCG_REG_R4,
132    TCG_REG_R3,
133
134    /* V0 and V1 reserved as temporaries; V20 - V31 are call-saved */
135    TCG_REG_V2,   /* call clobbered, vectors */
136    TCG_REG_V3,
137    TCG_REG_V4,
138    TCG_REG_V5,
139    TCG_REG_V6,
140    TCG_REG_V7,
141    TCG_REG_V8,
142    TCG_REG_V9,
143    TCG_REG_V10,
144    TCG_REG_V11,
145    TCG_REG_V12,
146    TCG_REG_V13,
147    TCG_REG_V14,
148    TCG_REG_V15,
149    TCG_REG_V16,
150    TCG_REG_V17,
151    TCG_REG_V18,
152    TCG_REG_V19,
153};
154
155static const int tcg_target_call_iarg_regs[] = {
156    TCG_REG_R3,
157    TCG_REG_R4,
158    TCG_REG_R5,
159    TCG_REG_R6,
160    TCG_REG_R7,
161    TCG_REG_R8,
162    TCG_REG_R9,
163    TCG_REG_R10
164};
165
166static const int tcg_target_call_oarg_regs[] = {
167    TCG_REG_R3,
168    TCG_REG_R4
169};
170
171static const int tcg_target_callee_save_regs[] = {
172#ifdef TCG_TARGET_CALL_DARWIN
173    TCG_REG_R11,
174#endif
175    TCG_REG_R14,
176    TCG_REG_R15,
177    TCG_REG_R16,
178    TCG_REG_R17,
179    TCG_REG_R18,
180    TCG_REG_R19,
181    TCG_REG_R20,
182    TCG_REG_R21,
183    TCG_REG_R22,
184    TCG_REG_R23,
185    TCG_REG_R24,
186    TCG_REG_R25,
187    TCG_REG_R26,
188    TCG_REG_R27, /* currently used for the global env */
189    TCG_REG_R28,
190    TCG_REG_R29,
191    TCG_REG_R30,
192    TCG_REG_R31
193};
194
195static inline bool in_range_b(tcg_target_long target)
196{
197    return target == sextract64(target, 0, 26);
198}
199
200static uint32_t reloc_pc24_val(const tcg_insn_unit *pc,
201			       const tcg_insn_unit *target)
202{
203    ptrdiff_t disp = tcg_ptr_byte_diff(target, pc);
204    tcg_debug_assert(in_range_b(disp));
205    return disp & 0x3fffffc;
206}
207
208static bool reloc_pc24(tcg_insn_unit *src_rw, const tcg_insn_unit *target)
209{
210    const tcg_insn_unit *src_rx = tcg_splitwx_to_rx(src_rw);
211    ptrdiff_t disp = tcg_ptr_byte_diff(target, src_rx);
212
213    if (in_range_b(disp)) {
214        *src_rw = (*src_rw & ~0x3fffffc) | (disp & 0x3fffffc);
215        return true;
216    }
217    return false;
218}
219
220static uint16_t reloc_pc14_val(const tcg_insn_unit *pc,
221			       const tcg_insn_unit *target)
222{
223    ptrdiff_t disp = tcg_ptr_byte_diff(target, pc);
224    tcg_debug_assert(disp == (int16_t) disp);
225    return disp & 0xfffc;
226}
227
228static bool reloc_pc14(tcg_insn_unit *src_rw, const tcg_insn_unit *target)
229{
230    const tcg_insn_unit *src_rx = tcg_splitwx_to_rx(src_rw);
231    ptrdiff_t disp = tcg_ptr_byte_diff(target, src_rx);
232
233    if (disp == (int16_t) disp) {
234        *src_rw = (*src_rw & ~0xfffc) | (disp & 0xfffc);
235        return true;
236    }
237    return false;
238}
239
240/* test if a constant matches the constraint */
241static int tcg_target_const_match(tcg_target_long val, TCGType type,
242                                  const TCGArgConstraint *arg_ct)
243{
244    int ct = arg_ct->ct;
245    if (ct & TCG_CT_CONST) {
246        return 1;
247    }
248
249    /* The only 32-bit constraint we use aside from
250       TCG_CT_CONST is TCG_CT_CONST_S16.  */
251    if (type == TCG_TYPE_I32) {
252        val = (int32_t)val;
253    }
254
255    if ((ct & TCG_CT_CONST_S16) && val == (int16_t)val) {
256        return 1;
257    } else if ((ct & TCG_CT_CONST_U16) && val == (uint16_t)val) {
258        return 1;
259    } else if ((ct & TCG_CT_CONST_S32) && val == (int32_t)val) {
260        return 1;
261    } else if ((ct & TCG_CT_CONST_U32) && val == (uint32_t)val) {
262        return 1;
263    } else if ((ct & TCG_CT_CONST_ZERO) && val == 0) {
264        return 1;
265    } else if ((ct & TCG_CT_CONST_MONE) && val == -1) {
266        return 1;
267    } else if ((ct & TCG_CT_CONST_WSZ)
268               && val == (type == TCG_TYPE_I32 ? 32 : 64)) {
269        return 1;
270    }
271    return 0;
272}
273
274#define OPCD(opc) ((opc)<<26)
275#define XO19(opc) (OPCD(19)|((opc)<<1))
276#define MD30(opc) (OPCD(30)|((opc)<<2))
277#define MDS30(opc) (OPCD(30)|((opc)<<1))
278#define XO31(opc) (OPCD(31)|((opc)<<1))
279#define XO58(opc) (OPCD(58)|(opc))
280#define XO62(opc) (OPCD(62)|(opc))
281#define VX4(opc)  (OPCD(4)|(opc))
282
283#define B      OPCD( 18)
284#define BC     OPCD( 16)
285#define LBZ    OPCD( 34)
286#define LHZ    OPCD( 40)
287#define LHA    OPCD( 42)
288#define LWZ    OPCD( 32)
289#define LWZUX  XO31( 55)
290#define STB    OPCD( 38)
291#define STH    OPCD( 44)
292#define STW    OPCD( 36)
293
294#define STD    XO62(  0)
295#define STDU   XO62(  1)
296#define STDX   XO31(149)
297
298#define LD     XO58(  0)
299#define LDX    XO31( 21)
300#define LDU    XO58(  1)
301#define LDUX   XO31( 53)
302#define LWA    XO58(  2)
303#define LWAX   XO31(341)
304
305#define ADDIC  OPCD( 12)
306#define ADDI   OPCD( 14)
307#define ADDIS  OPCD( 15)
308#define ORI    OPCD( 24)
309#define ORIS   OPCD( 25)
310#define XORI   OPCD( 26)
311#define XORIS  OPCD( 27)
312#define ANDI   OPCD( 28)
313#define ANDIS  OPCD( 29)
314#define MULLI  OPCD(  7)
315#define CMPLI  OPCD( 10)
316#define CMPI   OPCD( 11)
317#define SUBFIC OPCD( 8)
318
319#define LWZU   OPCD( 33)
320#define STWU   OPCD( 37)
321
322#define RLWIMI OPCD( 20)
323#define RLWINM OPCD( 21)
324#define RLWNM  OPCD( 23)
325
326#define RLDICL MD30(  0)
327#define RLDICR MD30(  1)
328#define RLDIMI MD30(  3)
329#define RLDCL  MDS30( 8)
330
331#define BCLR   XO19( 16)
332#define BCCTR  XO19(528)
333#define CRAND  XO19(257)
334#define CRANDC XO19(129)
335#define CRNAND XO19(225)
336#define CROR   XO19(449)
337#define CRNOR  XO19( 33)
338
339#define EXTSB  XO31(954)
340#define EXTSH  XO31(922)
341#define EXTSW  XO31(986)
342#define ADD    XO31(266)
343#define ADDE   XO31(138)
344#define ADDME  XO31(234)
345#define ADDZE  XO31(202)
346#define ADDC   XO31( 10)
347#define AND    XO31( 28)
348#define SUBF   XO31( 40)
349#define SUBFC  XO31(  8)
350#define SUBFE  XO31(136)
351#define SUBFME XO31(232)
352#define SUBFZE XO31(200)
353#define OR     XO31(444)
354#define XOR    XO31(316)
355#define MULLW  XO31(235)
356#define MULHW  XO31( 75)
357#define MULHWU XO31( 11)
358#define DIVW   XO31(491)
359#define DIVWU  XO31(459)
360#define CMP    XO31(  0)
361#define CMPL   XO31( 32)
362#define LHBRX  XO31(790)
363#define LWBRX  XO31(534)
364#define LDBRX  XO31(532)
365#define STHBRX XO31(918)
366#define STWBRX XO31(662)
367#define STDBRX XO31(660)
368#define MFSPR  XO31(339)
369#define MTSPR  XO31(467)
370#define SRAWI  XO31(824)
371#define NEG    XO31(104)
372#define MFCR   XO31( 19)
373#define MFOCRF (MFCR | (1u << 20))
374#define NOR    XO31(124)
375#define CNTLZW XO31( 26)
376#define CNTLZD XO31( 58)
377#define CNTTZW XO31(538)
378#define CNTTZD XO31(570)
379#define CNTPOPW XO31(378)
380#define CNTPOPD XO31(506)
381#define ANDC   XO31( 60)
382#define ORC    XO31(412)
383#define EQV    XO31(284)
384#define NAND   XO31(476)
385#define ISEL   XO31( 15)
386
387#define MULLD  XO31(233)
388#define MULHD  XO31( 73)
389#define MULHDU XO31(  9)
390#define DIVD   XO31(489)
391#define DIVDU  XO31(457)
392
393#define LBZX   XO31( 87)
394#define LHZX   XO31(279)
395#define LHAX   XO31(343)
396#define LWZX   XO31( 23)
397#define STBX   XO31(215)
398#define STHX   XO31(407)
399#define STWX   XO31(151)
400
401#define EIEIO  XO31(854)
402#define HWSYNC XO31(598)
403#define LWSYNC (HWSYNC | (1u << 21))
404
405#define SPR(a, b) ((((a)<<5)|(b))<<11)
406#define LR     SPR(8, 0)
407#define CTR    SPR(9, 0)
408
409#define SLW    XO31( 24)
410#define SRW    XO31(536)
411#define SRAW   XO31(792)
412
413#define SLD    XO31( 27)
414#define SRD    XO31(539)
415#define SRAD   XO31(794)
416#define SRADI  XO31(413<<1)
417
418#define TW     XO31( 4)
419#define TRAP   (TW | TO(31))
420
421#define NOP    ORI  /* ori 0,0,0 */
422
423#define LVX        XO31(103)
424#define LVEBX      XO31(7)
425#define LVEHX      XO31(39)
426#define LVEWX      XO31(71)
427#define LXSDX      (XO31(588) | 1)  /* v2.06, force tx=1 */
428#define LXVDSX     (XO31(332) | 1)  /* v2.06, force tx=1 */
429#define LXSIWZX    (XO31(12) | 1)   /* v2.07, force tx=1 */
430#define LXV        (OPCD(61) | 8 | 1)  /* v3.00, force tx=1 */
431#define LXSD       (OPCD(57) | 2)   /* v3.00 */
432#define LXVWSX     (XO31(364) | 1)  /* v3.00, force tx=1 */
433
434#define STVX       XO31(231)
435#define STVEWX     XO31(199)
436#define STXSDX     (XO31(716) | 1)  /* v2.06, force sx=1 */
437#define STXSIWX    (XO31(140) | 1)  /* v2.07, force sx=1 */
438#define STXV       (OPCD(61) | 8 | 5) /* v3.00, force sx=1 */
439#define STXSD      (OPCD(61) | 2)   /* v3.00 */
440
441#define VADDSBS    VX4(768)
442#define VADDUBS    VX4(512)
443#define VADDUBM    VX4(0)
444#define VADDSHS    VX4(832)
445#define VADDUHS    VX4(576)
446#define VADDUHM    VX4(64)
447#define VADDSWS    VX4(896)
448#define VADDUWS    VX4(640)
449#define VADDUWM    VX4(128)
450#define VADDUDM    VX4(192)       /* v2.07 */
451
452#define VSUBSBS    VX4(1792)
453#define VSUBUBS    VX4(1536)
454#define VSUBUBM    VX4(1024)
455#define VSUBSHS    VX4(1856)
456#define VSUBUHS    VX4(1600)
457#define VSUBUHM    VX4(1088)
458#define VSUBSWS    VX4(1920)
459#define VSUBUWS    VX4(1664)
460#define VSUBUWM    VX4(1152)
461#define VSUBUDM    VX4(1216)      /* v2.07 */
462
463#define VNEGW      (VX4(1538) | (6 << 16))  /* v3.00 */
464#define VNEGD      (VX4(1538) | (7 << 16))  /* v3.00 */
465
466#define VMAXSB     VX4(258)
467#define VMAXSH     VX4(322)
468#define VMAXSW     VX4(386)
469#define VMAXSD     VX4(450)       /* v2.07 */
470#define VMAXUB     VX4(2)
471#define VMAXUH     VX4(66)
472#define VMAXUW     VX4(130)
473#define VMAXUD     VX4(194)       /* v2.07 */
474#define VMINSB     VX4(770)
475#define VMINSH     VX4(834)
476#define VMINSW     VX4(898)
477#define VMINSD     VX4(962)       /* v2.07 */
478#define VMINUB     VX4(514)
479#define VMINUH     VX4(578)
480#define VMINUW     VX4(642)
481#define VMINUD     VX4(706)       /* v2.07 */
482
483#define VCMPEQUB   VX4(6)
484#define VCMPEQUH   VX4(70)
485#define VCMPEQUW   VX4(134)
486#define VCMPEQUD   VX4(199)       /* v2.07 */
487#define VCMPGTSB   VX4(774)
488#define VCMPGTSH   VX4(838)
489#define VCMPGTSW   VX4(902)
490#define VCMPGTSD   VX4(967)       /* v2.07 */
491#define VCMPGTUB   VX4(518)
492#define VCMPGTUH   VX4(582)
493#define VCMPGTUW   VX4(646)
494#define VCMPGTUD   VX4(711)       /* v2.07 */
495#define VCMPNEB    VX4(7)         /* v3.00 */
496#define VCMPNEH    VX4(71)        /* v3.00 */
497#define VCMPNEW    VX4(135)       /* v3.00 */
498
499#define VSLB       VX4(260)
500#define VSLH       VX4(324)
501#define VSLW       VX4(388)
502#define VSLD       VX4(1476)      /* v2.07 */
503#define VSRB       VX4(516)
504#define VSRH       VX4(580)
505#define VSRW       VX4(644)
506#define VSRD       VX4(1732)      /* v2.07 */
507#define VSRAB      VX4(772)
508#define VSRAH      VX4(836)
509#define VSRAW      VX4(900)
510#define VSRAD      VX4(964)       /* v2.07 */
511#define VRLB       VX4(4)
512#define VRLH       VX4(68)
513#define VRLW       VX4(132)
514#define VRLD       VX4(196)       /* v2.07 */
515
516#define VMULEUB    VX4(520)
517#define VMULEUH    VX4(584)
518#define VMULEUW    VX4(648)       /* v2.07 */
519#define VMULOUB    VX4(8)
520#define VMULOUH    VX4(72)
521#define VMULOUW    VX4(136)       /* v2.07 */
522#define VMULUWM    VX4(137)       /* v2.07 */
523#define VMULLD     VX4(457)       /* v3.10 */
524#define VMSUMUHM   VX4(38)
525
526#define VMRGHB     VX4(12)
527#define VMRGHH     VX4(76)
528#define VMRGHW     VX4(140)
529#define VMRGLB     VX4(268)
530#define VMRGLH     VX4(332)
531#define VMRGLW     VX4(396)
532
533#define VPKUHUM    VX4(14)
534#define VPKUWUM    VX4(78)
535
536#define VAND       VX4(1028)
537#define VANDC      VX4(1092)
538#define VNOR       VX4(1284)
539#define VOR        VX4(1156)
540#define VXOR       VX4(1220)
541#define VEQV       VX4(1668)      /* v2.07 */
542#define VNAND      VX4(1412)      /* v2.07 */
543#define VORC       VX4(1348)      /* v2.07 */
544
545#define VSPLTB     VX4(524)
546#define VSPLTH     VX4(588)
547#define VSPLTW     VX4(652)
548#define VSPLTISB   VX4(780)
549#define VSPLTISH   VX4(844)
550#define VSPLTISW   VX4(908)
551
552#define VSLDOI     VX4(44)
553
554#define XXPERMDI   (OPCD(60) | (10 << 3) | 7)  /* v2.06, force ax=bx=tx=1 */
555#define XXSEL      (OPCD(60) | (3 << 4) | 0xf) /* v2.06, force ax=bx=cx=tx=1 */
556#define XXSPLTIB   (OPCD(60) | (360 << 1) | 1) /* v3.00, force tx=1 */
557
558#define MFVSRD     (XO31(51) | 1)   /* v2.07, force sx=1 */
559#define MFVSRWZ    (XO31(115) | 1)  /* v2.07, force sx=1 */
560#define MTVSRD     (XO31(179) | 1)  /* v2.07, force tx=1 */
561#define MTVSRWZ    (XO31(243) | 1)  /* v2.07, force tx=1 */
562#define MTVSRDD    (XO31(435) | 1)  /* v3.00, force tx=1 */
563#define MTVSRWS    (XO31(403) | 1)  /* v3.00, force tx=1 */
564
565#define RT(r) ((r)<<21)
566#define RS(r) ((r)<<21)
567#define RA(r) ((r)<<16)
568#define RB(r) ((r)<<11)
569#define TO(t) ((t)<<21)
570#define SH(s) ((s)<<11)
571#define MB(b) ((b)<<6)
572#define ME(e) ((e)<<1)
573#define BO(o) ((o)<<21)
574#define MB64(b) ((b)<<5)
575#define FXM(b) (1 << (19 - (b)))
576
577#define VRT(r)  (((r) & 31) << 21)
578#define VRA(r)  (((r) & 31) << 16)
579#define VRB(r)  (((r) & 31) << 11)
580#define VRC(r)  (((r) & 31) <<  6)
581
582#define LK    1
583
584#define TAB(t, a, b) (RT(t) | RA(a) | RB(b))
585#define SAB(s, a, b) (RS(s) | RA(a) | RB(b))
586#define TAI(s, a, i) (RT(s) | RA(a) | ((i) & 0xffff))
587#define SAI(s, a, i) (RS(s) | RA(a) | ((i) & 0xffff))
588
589#define BF(n)    ((n)<<23)
590#define BI(n, c) (((c)+((n)*4))<<16)
591#define BT(n, c) (((c)+((n)*4))<<21)
592#define BA(n, c) (((c)+((n)*4))<<16)
593#define BB(n, c) (((c)+((n)*4))<<11)
594#define BC_(n, c) (((c)+((n)*4))<<6)
595
596#define BO_COND_TRUE  BO(12)
597#define BO_COND_FALSE BO( 4)
598#define BO_ALWAYS     BO(20)
599
600enum {
601    CR_LT,
602    CR_GT,
603    CR_EQ,
604    CR_SO
605};
606
607static const uint32_t tcg_to_bc[] = {
608    [TCG_COND_EQ]  = BC | BI(7, CR_EQ) | BO_COND_TRUE,
609    [TCG_COND_NE]  = BC | BI(7, CR_EQ) | BO_COND_FALSE,
610    [TCG_COND_LT]  = BC | BI(7, CR_LT) | BO_COND_TRUE,
611    [TCG_COND_GE]  = BC | BI(7, CR_LT) | BO_COND_FALSE,
612    [TCG_COND_LE]  = BC | BI(7, CR_GT) | BO_COND_FALSE,
613    [TCG_COND_GT]  = BC | BI(7, CR_GT) | BO_COND_TRUE,
614    [TCG_COND_LTU] = BC | BI(7, CR_LT) | BO_COND_TRUE,
615    [TCG_COND_GEU] = BC | BI(7, CR_LT) | BO_COND_FALSE,
616    [TCG_COND_LEU] = BC | BI(7, CR_GT) | BO_COND_FALSE,
617    [TCG_COND_GTU] = BC | BI(7, CR_GT) | BO_COND_TRUE,
618};
619
620/* The low bit here is set if the RA and RB fields must be inverted.  */
621static const uint32_t tcg_to_isel[] = {
622    [TCG_COND_EQ]  = ISEL | BC_(7, CR_EQ),
623    [TCG_COND_NE]  = ISEL | BC_(7, CR_EQ) | 1,
624    [TCG_COND_LT]  = ISEL | BC_(7, CR_LT),
625    [TCG_COND_GE]  = ISEL | BC_(7, CR_LT) | 1,
626    [TCG_COND_LE]  = ISEL | BC_(7, CR_GT) | 1,
627    [TCG_COND_GT]  = ISEL | BC_(7, CR_GT),
628    [TCG_COND_LTU] = ISEL | BC_(7, CR_LT),
629    [TCG_COND_GEU] = ISEL | BC_(7, CR_LT) | 1,
630    [TCG_COND_LEU] = ISEL | BC_(7, CR_GT) | 1,
631    [TCG_COND_GTU] = ISEL | BC_(7, CR_GT),
632};
633
634static bool patch_reloc(tcg_insn_unit *code_ptr, int type,
635                        intptr_t value, intptr_t addend)
636{
637    const tcg_insn_unit *target;
638    int16_t lo;
639    int32_t hi;
640
641    value += addend;
642    target = (const tcg_insn_unit *)value;
643
644    switch (type) {
645    case R_PPC_REL14:
646        return reloc_pc14(code_ptr, target);
647    case R_PPC_REL24:
648        return reloc_pc24(code_ptr, target);
649    case R_PPC_ADDR16:
650        /*
651         * We are (slightly) abusing this relocation type.  In particular,
652         * assert that the low 2 bits are zero, and do not modify them.
653         * That way we can use this with LD et al that have opcode bits
654         * in the low 2 bits of the insn.
655         */
656        if ((value & 3) || value != (int16_t)value) {
657            return false;
658        }
659        *code_ptr = (*code_ptr & ~0xfffc) | (value & 0xfffc);
660        break;
661    case R_PPC_ADDR32:
662        /*
663         * We are abusing this relocation type.  Again, this points to
664         * a pair of insns, lis + load.  This is an absolute address
665         * relocation for PPC32 so the lis cannot be removed.
666         */
667        lo = value;
668        hi = value - lo;
669        if (hi + lo != value) {
670            return false;
671        }
672        code_ptr[0] = deposit32(code_ptr[0], 0, 16, hi >> 16);
673        code_ptr[1] = deposit32(code_ptr[1], 0, 16, lo);
674        break;
675    default:
676        g_assert_not_reached();
677    }
678    return true;
679}
680
681static void tcg_out_mem_long(TCGContext *s, int opi, int opx, TCGReg rt,
682                             TCGReg base, tcg_target_long offset);
683
684static bool tcg_out_mov(TCGContext *s, TCGType type, TCGReg ret, TCGReg arg)
685{
686    if (ret == arg) {
687        return true;
688    }
689    switch (type) {
690    case TCG_TYPE_I64:
691        tcg_debug_assert(TCG_TARGET_REG_BITS == 64);
692        /* fallthru */
693    case TCG_TYPE_I32:
694        if (ret < TCG_REG_V0) {
695            if (arg < TCG_REG_V0) {
696                tcg_out32(s, OR | SAB(arg, ret, arg));
697                break;
698            } else if (have_isa_2_07) {
699                tcg_out32(s, (type == TCG_TYPE_I32 ? MFVSRWZ : MFVSRD)
700                          | VRT(arg) | RA(ret));
701                break;
702            } else {
703                /* Altivec does not support vector->integer moves.  */
704                return false;
705            }
706        } else if (arg < TCG_REG_V0) {
707            if (have_isa_2_07) {
708                tcg_out32(s, (type == TCG_TYPE_I32 ? MTVSRWZ : MTVSRD)
709                          | VRT(ret) | RA(arg));
710                break;
711            } else {
712                /* Altivec does not support integer->vector moves.  */
713                return false;
714            }
715        }
716        /* fallthru */
717    case TCG_TYPE_V64:
718    case TCG_TYPE_V128:
719        tcg_debug_assert(ret >= TCG_REG_V0 && arg >= TCG_REG_V0);
720        tcg_out32(s, VOR | VRT(ret) | VRA(arg) | VRB(arg));
721        break;
722    default:
723        g_assert_not_reached();
724    }
725    return true;
726}
727
728static inline void tcg_out_rld(TCGContext *s, int op, TCGReg ra, TCGReg rs,
729                               int sh, int mb)
730{
731    tcg_debug_assert(TCG_TARGET_REG_BITS == 64);
732    sh = SH(sh & 0x1f) | (((sh >> 5) & 1) << 1);
733    mb = MB64((mb >> 5) | ((mb << 1) & 0x3f));
734    tcg_out32(s, op | RA(ra) | RS(rs) | sh | mb);
735}
736
737static inline void tcg_out_rlw(TCGContext *s, int op, TCGReg ra, TCGReg rs,
738                               int sh, int mb, int me)
739{
740    tcg_out32(s, op | RA(ra) | RS(rs) | SH(sh) | MB(mb) | ME(me));
741}
742
743static inline void tcg_out_ext32u(TCGContext *s, TCGReg dst, TCGReg src)
744{
745    tcg_out_rld(s, RLDICL, dst, src, 0, 32);
746}
747
748static inline void tcg_out_shli32(TCGContext *s, TCGReg dst, TCGReg src, int c)
749{
750    tcg_out_rlw(s, RLWINM, dst, src, c, 0, 31 - c);
751}
752
753static inline void tcg_out_shli64(TCGContext *s, TCGReg dst, TCGReg src, int c)
754{
755    tcg_out_rld(s, RLDICR, dst, src, c, 63 - c);
756}
757
758static inline void tcg_out_shri32(TCGContext *s, TCGReg dst, TCGReg src, int c)
759{
760    tcg_out_rlw(s, RLWINM, dst, src, 32 - c, c, 31);
761}
762
763static inline void tcg_out_shri64(TCGContext *s, TCGReg dst, TCGReg src, int c)
764{
765    tcg_out_rld(s, RLDICL, dst, src, 64 - c, c);
766}
767
768/* Emit a move into ret of arg, if it can be done in one insn.  */
769static bool tcg_out_movi_one(TCGContext *s, TCGReg ret, tcg_target_long arg)
770{
771    if (arg == (int16_t)arg) {
772        tcg_out32(s, ADDI | TAI(ret, 0, arg));
773        return true;
774    }
775    if (arg == (int32_t)arg && (arg & 0xffff) == 0) {
776        tcg_out32(s, ADDIS | TAI(ret, 0, arg >> 16));
777        return true;
778    }
779    return false;
780}
781
782static void tcg_out_movi_int(TCGContext *s, TCGType type, TCGReg ret,
783                             tcg_target_long arg, bool in_prologue)
784{
785    intptr_t tb_diff;
786    tcg_target_long tmp;
787    int shift;
788
789    tcg_debug_assert(TCG_TARGET_REG_BITS == 64 || type == TCG_TYPE_I32);
790
791    if (TCG_TARGET_REG_BITS == 64 && type == TCG_TYPE_I32) {
792        arg = (int32_t)arg;
793    }
794
795    /* Load 16-bit immediates with one insn.  */
796    if (tcg_out_movi_one(s, ret, arg)) {
797        return;
798    }
799
800    /* Load addresses within the TB with one insn.  */
801    tb_diff = tcg_tbrel_diff(s, (void *)arg);
802    if (!in_prologue && USE_REG_TB && tb_diff == (int16_t)tb_diff) {
803        tcg_out32(s, ADDI | TAI(ret, TCG_REG_TB, tb_diff));
804        return;
805    }
806
807    /* Load 32-bit immediates with two insns.  Note that we've already
808       eliminated bare ADDIS, so we know both insns are required.  */
809    if (TCG_TARGET_REG_BITS == 32 || arg == (int32_t)arg) {
810        tcg_out32(s, ADDIS | TAI(ret, 0, arg >> 16));
811        tcg_out32(s, ORI | SAI(ret, ret, arg));
812        return;
813    }
814    if (arg == (uint32_t)arg && !(arg & 0x8000)) {
815        tcg_out32(s, ADDI | TAI(ret, 0, arg));
816        tcg_out32(s, ORIS | SAI(ret, ret, arg >> 16));
817        return;
818    }
819
820    /* Load masked 16-bit value.  */
821    if (arg > 0 && (arg & 0x8000)) {
822        tmp = arg | 0x7fff;
823        if ((tmp & (tmp + 1)) == 0) {
824            int mb = clz64(tmp + 1) + 1;
825            tcg_out32(s, ADDI | TAI(ret, 0, arg));
826            tcg_out_rld(s, RLDICL, ret, ret, 0, mb);
827            return;
828        }
829    }
830
831    /* Load common masks with 2 insns.  */
832    shift = ctz64(arg);
833    tmp = arg >> shift;
834    if (tmp == (int16_t)tmp) {
835        tcg_out32(s, ADDI | TAI(ret, 0, tmp));
836        tcg_out_shli64(s, ret, ret, shift);
837        return;
838    }
839    shift = clz64(arg);
840    if (tcg_out_movi_one(s, ret, arg << shift)) {
841        tcg_out_shri64(s, ret, ret, shift);
842        return;
843    }
844
845    /* Load addresses within 2GB of TB with 2 (or rarely 3) insns.  */
846    if (!in_prologue && USE_REG_TB && tb_diff == (int32_t)tb_diff) {
847        tcg_out_mem_long(s, ADDI, ADD, ret, TCG_REG_TB, tb_diff);
848        return;
849    }
850
851    /* Use the constant pool, if possible.  */
852    if (!in_prologue && USE_REG_TB) {
853        new_pool_label(s, arg, R_PPC_ADDR16, s->code_ptr,
854                       tcg_tbrel_diff(s, NULL));
855        tcg_out32(s, LD | TAI(ret, TCG_REG_TB, 0));
856        return;
857    }
858
859    tmp = arg >> 31 >> 1;
860    tcg_out_movi(s, TCG_TYPE_I32, ret, tmp);
861    if (tmp) {
862        tcg_out_shli64(s, ret, ret, 32);
863    }
864    if (arg & 0xffff0000) {
865        tcg_out32(s, ORIS | SAI(ret, ret, arg >> 16));
866    }
867    if (arg & 0xffff) {
868        tcg_out32(s, ORI | SAI(ret, ret, arg));
869    }
870}
871
872static void tcg_out_dupi_vec(TCGContext *s, TCGType type, unsigned vece,
873                             TCGReg ret, int64_t val)
874{
875    uint32_t load_insn;
876    int rel, low;
877    intptr_t add;
878
879    switch (vece) {
880    case MO_8:
881        low = (int8_t)val;
882        if (low >= -16 && low < 16) {
883            tcg_out32(s, VSPLTISB | VRT(ret) | ((val & 31) << 16));
884            return;
885        }
886        if (have_isa_3_00) {
887            tcg_out32(s, XXSPLTIB | VRT(ret) | ((val & 0xff) << 11));
888            return;
889        }
890        break;
891
892    case MO_16:
893        low = (int16_t)val;
894        if (low >= -16 && low < 16) {
895            tcg_out32(s, VSPLTISH | VRT(ret) | ((val & 31) << 16));
896            return;
897        }
898        break;
899
900    case MO_32:
901        low = (int32_t)val;
902        if (low >= -16 && low < 16) {
903            tcg_out32(s, VSPLTISW | VRT(ret) | ((val & 31) << 16));
904            return;
905        }
906        break;
907    }
908
909    /*
910     * Otherwise we must load the value from the constant pool.
911     */
912    if (USE_REG_TB) {
913        rel = R_PPC_ADDR16;
914        add = tcg_tbrel_diff(s, NULL);
915    } else {
916        rel = R_PPC_ADDR32;
917        add = 0;
918    }
919
920    if (have_vsx) {
921        load_insn = type == TCG_TYPE_V64 ? LXSDX : LXVDSX;
922        load_insn |= VRT(ret) | RB(TCG_REG_TMP1);
923        if (TCG_TARGET_REG_BITS == 64) {
924            new_pool_label(s, val, rel, s->code_ptr, add);
925        } else {
926            new_pool_l2(s, rel, s->code_ptr, add, val >> 32, val);
927        }
928    } else {
929        load_insn = LVX | VRT(ret) | RB(TCG_REG_TMP1);
930        if (TCG_TARGET_REG_BITS == 64) {
931            new_pool_l2(s, rel, s->code_ptr, add, val, val);
932        } else {
933            new_pool_l4(s, rel, s->code_ptr, add,
934                        val >> 32, val, val >> 32, val);
935        }
936    }
937
938    if (USE_REG_TB) {
939        tcg_out32(s, ADDI | TAI(TCG_REG_TMP1, 0, 0));
940        load_insn |= RA(TCG_REG_TB);
941    } else {
942        tcg_out32(s, ADDIS | TAI(TCG_REG_TMP1, 0, 0));
943        tcg_out32(s, ADDI | TAI(TCG_REG_TMP1, TCG_REG_TMP1, 0));
944    }
945    tcg_out32(s, load_insn);
946}
947
948static void tcg_out_movi(TCGContext *s, TCGType type, TCGReg ret,
949                         tcg_target_long arg)
950{
951    switch (type) {
952    case TCG_TYPE_I32:
953    case TCG_TYPE_I64:
954        tcg_debug_assert(ret < TCG_REG_V0);
955        tcg_out_movi_int(s, type, ret, arg, false);
956        break;
957
958    default:
959        g_assert_not_reached();
960    }
961}
962
963static bool mask_operand(uint32_t c, int *mb, int *me)
964{
965    uint32_t lsb, test;
966
967    /* Accept a bit pattern like:
968           0....01....1
969           1....10....0
970           0..01..10..0
971       Keep track of the transitions.  */
972    if (c == 0 || c == -1) {
973        return false;
974    }
975    test = c;
976    lsb = test & -test;
977    test += lsb;
978    if (test & (test - 1)) {
979        return false;
980    }
981
982    *me = clz32(lsb);
983    *mb = test ? clz32(test & -test) + 1 : 0;
984    return true;
985}
986
987static bool mask64_operand(uint64_t c, int *mb, int *me)
988{
989    uint64_t lsb;
990
991    if (c == 0) {
992        return false;
993    }
994
995    lsb = c & -c;
996    /* Accept 1..10..0.  */
997    if (c == -lsb) {
998        *mb = 0;
999        *me = clz64(lsb);
1000        return true;
1001    }
1002    /* Accept 0..01..1.  */
1003    if (lsb == 1 && (c & (c + 1)) == 0) {
1004        *mb = clz64(c + 1) + 1;
1005        *me = 63;
1006        return true;
1007    }
1008    return false;
1009}
1010
1011static void tcg_out_andi32(TCGContext *s, TCGReg dst, TCGReg src, uint32_t c)
1012{
1013    int mb, me;
1014
1015    if (mask_operand(c, &mb, &me)) {
1016        tcg_out_rlw(s, RLWINM, dst, src, 0, mb, me);
1017    } else if ((c & 0xffff) == c) {
1018        tcg_out32(s, ANDI | SAI(src, dst, c));
1019        return;
1020    } else if ((c & 0xffff0000) == c) {
1021        tcg_out32(s, ANDIS | SAI(src, dst, c >> 16));
1022        return;
1023    } else {
1024        tcg_out_movi(s, TCG_TYPE_I32, TCG_REG_R0, c);
1025        tcg_out32(s, AND | SAB(src, dst, TCG_REG_R0));
1026    }
1027}
1028
1029static void tcg_out_andi64(TCGContext *s, TCGReg dst, TCGReg src, uint64_t c)
1030{
1031    int mb, me;
1032
1033    tcg_debug_assert(TCG_TARGET_REG_BITS == 64);
1034    if (mask64_operand(c, &mb, &me)) {
1035        if (mb == 0) {
1036            tcg_out_rld(s, RLDICR, dst, src, 0, me);
1037        } else {
1038            tcg_out_rld(s, RLDICL, dst, src, 0, mb);
1039        }
1040    } else if ((c & 0xffff) == c) {
1041        tcg_out32(s, ANDI | SAI(src, dst, c));
1042        return;
1043    } else if ((c & 0xffff0000) == c) {
1044        tcg_out32(s, ANDIS | SAI(src, dst, c >> 16));
1045        return;
1046    } else {
1047        tcg_out_movi(s, TCG_TYPE_I64, TCG_REG_R0, c);
1048        tcg_out32(s, AND | SAB(src, dst, TCG_REG_R0));
1049    }
1050}
1051
1052static void tcg_out_zori32(TCGContext *s, TCGReg dst, TCGReg src, uint32_t c,
1053                           int op_lo, int op_hi)
1054{
1055    if (c >> 16) {
1056        tcg_out32(s, op_hi | SAI(src, dst, c >> 16));
1057        src = dst;
1058    }
1059    if (c & 0xffff) {
1060        tcg_out32(s, op_lo | SAI(src, dst, c));
1061        src = dst;
1062    }
1063}
1064
1065static void tcg_out_ori32(TCGContext *s, TCGReg dst, TCGReg src, uint32_t c)
1066{
1067    tcg_out_zori32(s, dst, src, c, ORI, ORIS);
1068}
1069
1070static void tcg_out_xori32(TCGContext *s, TCGReg dst, TCGReg src, uint32_t c)
1071{
1072    tcg_out_zori32(s, dst, src, c, XORI, XORIS);
1073}
1074
1075static void tcg_out_b(TCGContext *s, int mask, const tcg_insn_unit *target)
1076{
1077    ptrdiff_t disp = tcg_pcrel_diff(s, target);
1078    if (in_range_b(disp)) {
1079        tcg_out32(s, B | (disp & 0x3fffffc) | mask);
1080    } else {
1081        tcg_out_movi(s, TCG_TYPE_PTR, TCG_REG_R0, (uintptr_t)target);
1082        tcg_out32(s, MTSPR | RS(TCG_REG_R0) | CTR);
1083        tcg_out32(s, BCCTR | BO_ALWAYS | mask);
1084    }
1085}
1086
1087static void tcg_out_mem_long(TCGContext *s, int opi, int opx, TCGReg rt,
1088                             TCGReg base, tcg_target_long offset)
1089{
1090    tcg_target_long orig = offset, l0, l1, extra = 0, align = 0;
1091    bool is_int_store = false;
1092    TCGReg rs = TCG_REG_TMP1;
1093
1094    switch (opi) {
1095    case LD: case LWA:
1096        align = 3;
1097        /* FALLTHRU */
1098    default:
1099        if (rt > TCG_REG_R0 && rt < TCG_REG_V0) {
1100            rs = rt;
1101            break;
1102        }
1103        break;
1104    case LXSD:
1105    case STXSD:
1106        align = 3;
1107        break;
1108    case LXV:
1109    case STXV:
1110        align = 15;
1111        break;
1112    case STD:
1113        align = 3;
1114        /* FALLTHRU */
1115    case STB: case STH: case STW:
1116        is_int_store = true;
1117        break;
1118    }
1119
1120    /* For unaligned, or very large offsets, use the indexed form.  */
1121    if (offset & align || offset != (int32_t)offset || opi == 0) {
1122        if (rs == base) {
1123            rs = TCG_REG_R0;
1124        }
1125        tcg_debug_assert(!is_int_store || rs != rt);
1126        tcg_out_movi(s, TCG_TYPE_PTR, rs, orig);
1127        tcg_out32(s, opx | TAB(rt & 31, base, rs));
1128        return;
1129    }
1130
1131    l0 = (int16_t)offset;
1132    offset = (offset - l0) >> 16;
1133    l1 = (int16_t)offset;
1134
1135    if (l1 < 0 && orig >= 0) {
1136        extra = 0x4000;
1137        l1 = (int16_t)(offset - 0x4000);
1138    }
1139    if (l1) {
1140        tcg_out32(s, ADDIS | TAI(rs, base, l1));
1141        base = rs;
1142    }
1143    if (extra) {
1144        tcg_out32(s, ADDIS | TAI(rs, base, extra));
1145        base = rs;
1146    }
1147    if (opi != ADDI || base != rt || l0 != 0) {
1148        tcg_out32(s, opi | TAI(rt & 31, base, l0));
1149    }
1150}
1151
1152static void tcg_out_vsldoi(TCGContext *s, TCGReg ret,
1153                           TCGReg va, TCGReg vb, int shb)
1154{
1155    tcg_out32(s, VSLDOI | VRT(ret) | VRA(va) | VRB(vb) | (shb << 6));
1156}
1157
1158static void tcg_out_ld(TCGContext *s, TCGType type, TCGReg ret,
1159                       TCGReg base, intptr_t offset)
1160{
1161    int shift;
1162
1163    switch (type) {
1164    case TCG_TYPE_I32:
1165        if (ret < TCG_REG_V0) {
1166            tcg_out_mem_long(s, LWZ, LWZX, ret, base, offset);
1167            break;
1168        }
1169        if (have_isa_2_07 && have_vsx) {
1170            tcg_out_mem_long(s, 0, LXSIWZX, ret, base, offset);
1171            break;
1172        }
1173        tcg_debug_assert((offset & 3) == 0);
1174        tcg_out_mem_long(s, 0, LVEWX, ret, base, offset);
1175        shift = (offset - 4) & 0xc;
1176        if (shift) {
1177            tcg_out_vsldoi(s, ret, ret, ret, shift);
1178        }
1179        break;
1180    case TCG_TYPE_I64:
1181        if (ret < TCG_REG_V0) {
1182            tcg_debug_assert(TCG_TARGET_REG_BITS == 64);
1183            tcg_out_mem_long(s, LD, LDX, ret, base, offset);
1184            break;
1185        }
1186        /* fallthru */
1187    case TCG_TYPE_V64:
1188        tcg_debug_assert(ret >= TCG_REG_V0);
1189        if (have_vsx) {
1190            tcg_out_mem_long(s, have_isa_3_00 ? LXSD : 0, LXSDX,
1191                             ret, base, offset);
1192            break;
1193        }
1194        tcg_debug_assert((offset & 7) == 0);
1195        tcg_out_mem_long(s, 0, LVX, ret, base, offset & -16);
1196        if (offset & 8) {
1197            tcg_out_vsldoi(s, ret, ret, ret, 8);
1198        }
1199        break;
1200    case TCG_TYPE_V128:
1201        tcg_debug_assert(ret >= TCG_REG_V0);
1202        tcg_debug_assert((offset & 15) == 0);
1203        tcg_out_mem_long(s, have_isa_3_00 ? LXV : 0,
1204                         LVX, ret, base, offset);
1205        break;
1206    default:
1207        g_assert_not_reached();
1208    }
1209}
1210
1211static void tcg_out_st(TCGContext *s, TCGType type, TCGReg arg,
1212                              TCGReg base, intptr_t offset)
1213{
1214    int shift;
1215
1216    switch (type) {
1217    case TCG_TYPE_I32:
1218        if (arg < TCG_REG_V0) {
1219            tcg_out_mem_long(s, STW, STWX, arg, base, offset);
1220            break;
1221        }
1222        if (have_isa_2_07 && have_vsx) {
1223            tcg_out_mem_long(s, 0, STXSIWX, arg, base, offset);
1224            break;
1225        }
1226        assert((offset & 3) == 0);
1227        tcg_debug_assert((offset & 3) == 0);
1228        shift = (offset - 4) & 0xc;
1229        if (shift) {
1230            tcg_out_vsldoi(s, TCG_VEC_TMP1, arg, arg, shift);
1231            arg = TCG_VEC_TMP1;
1232        }
1233        tcg_out_mem_long(s, 0, STVEWX, arg, base, offset);
1234        break;
1235    case TCG_TYPE_I64:
1236        if (arg < TCG_REG_V0) {
1237            tcg_debug_assert(TCG_TARGET_REG_BITS == 64);
1238            tcg_out_mem_long(s, STD, STDX, arg, base, offset);
1239            break;
1240        }
1241        /* fallthru */
1242    case TCG_TYPE_V64:
1243        tcg_debug_assert(arg >= TCG_REG_V0);
1244        if (have_vsx) {
1245            tcg_out_mem_long(s, have_isa_3_00 ? STXSD : 0,
1246                             STXSDX, arg, base, offset);
1247            break;
1248        }
1249        tcg_debug_assert((offset & 7) == 0);
1250        if (offset & 8) {
1251            tcg_out_vsldoi(s, TCG_VEC_TMP1, arg, arg, 8);
1252            arg = TCG_VEC_TMP1;
1253        }
1254        tcg_out_mem_long(s, 0, STVEWX, arg, base, offset);
1255        tcg_out_mem_long(s, 0, STVEWX, arg, base, offset + 4);
1256        break;
1257    case TCG_TYPE_V128:
1258        tcg_debug_assert(arg >= TCG_REG_V0);
1259        tcg_out_mem_long(s, have_isa_3_00 ? STXV : 0,
1260                         STVX, arg, base, offset);
1261        break;
1262    default:
1263        g_assert_not_reached();
1264    }
1265}
1266
1267static inline bool tcg_out_sti(TCGContext *s, TCGType type, TCGArg val,
1268                               TCGReg base, intptr_t ofs)
1269{
1270    return false;
1271}
1272
1273static void tcg_out_cmp(TCGContext *s, int cond, TCGArg arg1, TCGArg arg2,
1274                        int const_arg2, int cr, TCGType type)
1275{
1276    int imm;
1277    uint32_t op;
1278
1279    tcg_debug_assert(TCG_TARGET_REG_BITS == 64 || type == TCG_TYPE_I32);
1280
1281    /* Simplify the comparisons below wrt CMPI.  */
1282    if (type == TCG_TYPE_I32) {
1283        arg2 = (int32_t)arg2;
1284    }
1285
1286    switch (cond) {
1287    case TCG_COND_EQ:
1288    case TCG_COND_NE:
1289        if (const_arg2) {
1290            if ((int16_t) arg2 == arg2) {
1291                op = CMPI;
1292                imm = 1;
1293                break;
1294            } else if ((uint16_t) arg2 == arg2) {
1295                op = CMPLI;
1296                imm = 1;
1297                break;
1298            }
1299        }
1300        op = CMPL;
1301        imm = 0;
1302        break;
1303
1304    case TCG_COND_LT:
1305    case TCG_COND_GE:
1306    case TCG_COND_LE:
1307    case TCG_COND_GT:
1308        if (const_arg2) {
1309            if ((int16_t) arg2 == arg2) {
1310                op = CMPI;
1311                imm = 1;
1312                break;
1313            }
1314        }
1315        op = CMP;
1316        imm = 0;
1317        break;
1318
1319    case TCG_COND_LTU:
1320    case TCG_COND_GEU:
1321    case TCG_COND_LEU:
1322    case TCG_COND_GTU:
1323        if (const_arg2) {
1324            if ((uint16_t) arg2 == arg2) {
1325                op = CMPLI;
1326                imm = 1;
1327                break;
1328            }
1329        }
1330        op = CMPL;
1331        imm = 0;
1332        break;
1333
1334    default:
1335        tcg_abort();
1336    }
1337    op |= BF(cr) | ((type == TCG_TYPE_I64) << 21);
1338
1339    if (imm) {
1340        tcg_out32(s, op | RA(arg1) | (arg2 & 0xffff));
1341    } else {
1342        if (const_arg2) {
1343            tcg_out_movi(s, type, TCG_REG_R0, arg2);
1344            arg2 = TCG_REG_R0;
1345        }
1346        tcg_out32(s, op | RA(arg1) | RB(arg2));
1347    }
1348}
1349
1350static void tcg_out_setcond_eq0(TCGContext *s, TCGType type,
1351                                TCGReg dst, TCGReg src)
1352{
1353    if (type == TCG_TYPE_I32) {
1354        tcg_out32(s, CNTLZW | RS(src) | RA(dst));
1355        tcg_out_shri32(s, dst, dst, 5);
1356    } else {
1357        tcg_out32(s, CNTLZD | RS(src) | RA(dst));
1358        tcg_out_shri64(s, dst, dst, 6);
1359    }
1360}
1361
1362static void tcg_out_setcond_ne0(TCGContext *s, TCGReg dst, TCGReg src)
1363{
1364    /* X != 0 implies X + -1 generates a carry.  Extra addition
1365       trickery means: R = X-1 + ~X + C = X-1 + (-X+1) + C = C.  */
1366    if (dst != src) {
1367        tcg_out32(s, ADDIC | TAI(dst, src, -1));
1368        tcg_out32(s, SUBFE | TAB(dst, dst, src));
1369    } else {
1370        tcg_out32(s, ADDIC | TAI(TCG_REG_R0, src, -1));
1371        tcg_out32(s, SUBFE | TAB(dst, TCG_REG_R0, src));
1372    }
1373}
1374
1375static TCGReg tcg_gen_setcond_xor(TCGContext *s, TCGReg arg1, TCGArg arg2,
1376                                  bool const_arg2)
1377{
1378    if (const_arg2) {
1379        if ((uint32_t)arg2 == arg2) {
1380            tcg_out_xori32(s, TCG_REG_R0, arg1, arg2);
1381        } else {
1382            tcg_out_movi(s, TCG_TYPE_I64, TCG_REG_R0, arg2);
1383            tcg_out32(s, XOR | SAB(arg1, TCG_REG_R0, TCG_REG_R0));
1384        }
1385    } else {
1386        tcg_out32(s, XOR | SAB(arg1, TCG_REG_R0, arg2));
1387    }
1388    return TCG_REG_R0;
1389}
1390
1391static void tcg_out_setcond(TCGContext *s, TCGType type, TCGCond cond,
1392                            TCGArg arg0, TCGArg arg1, TCGArg arg2,
1393                            int const_arg2)
1394{
1395    int crop, sh;
1396
1397    tcg_debug_assert(TCG_TARGET_REG_BITS == 64 || type == TCG_TYPE_I32);
1398
1399    /* Ignore high bits of a potential constant arg2.  */
1400    if (type == TCG_TYPE_I32) {
1401        arg2 = (uint32_t)arg2;
1402    }
1403
1404    /* Handle common and trivial cases before handling anything else.  */
1405    if (arg2 == 0) {
1406        switch (cond) {
1407        case TCG_COND_EQ:
1408            tcg_out_setcond_eq0(s, type, arg0, arg1);
1409            return;
1410        case TCG_COND_NE:
1411            if (TCG_TARGET_REG_BITS == 64 && type == TCG_TYPE_I32) {
1412                tcg_out_ext32u(s, TCG_REG_R0, arg1);
1413                arg1 = TCG_REG_R0;
1414            }
1415            tcg_out_setcond_ne0(s, arg0, arg1);
1416            return;
1417        case TCG_COND_GE:
1418            tcg_out32(s, NOR | SAB(arg1, arg0, arg1));
1419            arg1 = arg0;
1420            /* FALLTHRU */
1421        case TCG_COND_LT:
1422            /* Extract the sign bit.  */
1423            if (type == TCG_TYPE_I32) {
1424                tcg_out_shri32(s, arg0, arg1, 31);
1425            } else {
1426                tcg_out_shri64(s, arg0, arg1, 63);
1427            }
1428            return;
1429        default:
1430            break;
1431        }
1432    }
1433
1434    /* If we have ISEL, we can implement everything with 3 or 4 insns.
1435       All other cases below are also at least 3 insns, so speed up the
1436       code generator by not considering them and always using ISEL.  */
1437    if (have_isel) {
1438        int isel, tab;
1439
1440        tcg_out_cmp(s, cond, arg1, arg2, const_arg2, 7, type);
1441
1442        isel = tcg_to_isel[cond];
1443
1444        tcg_out_movi(s, type, arg0, 1);
1445        if (isel & 1) {
1446            /* arg0 = (bc ? 0 : 1) */
1447            tab = TAB(arg0, 0, arg0);
1448            isel &= ~1;
1449        } else {
1450            /* arg0 = (bc ? 1 : 0) */
1451            tcg_out_movi(s, type, TCG_REG_R0, 0);
1452            tab = TAB(arg0, arg0, TCG_REG_R0);
1453        }
1454        tcg_out32(s, isel | tab);
1455        return;
1456    }
1457
1458    switch (cond) {
1459    case TCG_COND_EQ:
1460        arg1 = tcg_gen_setcond_xor(s, arg1, arg2, const_arg2);
1461        tcg_out_setcond_eq0(s, type, arg0, arg1);
1462        return;
1463
1464    case TCG_COND_NE:
1465        arg1 = tcg_gen_setcond_xor(s, arg1, arg2, const_arg2);
1466        /* Discard the high bits only once, rather than both inputs.  */
1467        if (TCG_TARGET_REG_BITS == 64 && type == TCG_TYPE_I32) {
1468            tcg_out_ext32u(s, TCG_REG_R0, arg1);
1469            arg1 = TCG_REG_R0;
1470        }
1471        tcg_out_setcond_ne0(s, arg0, arg1);
1472        return;
1473
1474    case TCG_COND_GT:
1475    case TCG_COND_GTU:
1476        sh = 30;
1477        crop = 0;
1478        goto crtest;
1479
1480    case TCG_COND_LT:
1481    case TCG_COND_LTU:
1482        sh = 29;
1483        crop = 0;
1484        goto crtest;
1485
1486    case TCG_COND_GE:
1487    case TCG_COND_GEU:
1488        sh = 31;
1489        crop = CRNOR | BT(7, CR_EQ) | BA(7, CR_LT) | BB(7, CR_LT);
1490        goto crtest;
1491
1492    case TCG_COND_LE:
1493    case TCG_COND_LEU:
1494        sh = 31;
1495        crop = CRNOR | BT(7, CR_EQ) | BA(7, CR_GT) | BB(7, CR_GT);
1496    crtest:
1497        tcg_out_cmp(s, cond, arg1, arg2, const_arg2, 7, type);
1498        if (crop) {
1499            tcg_out32(s, crop);
1500        }
1501        tcg_out32(s, MFOCRF | RT(TCG_REG_R0) | FXM(7));
1502        tcg_out_rlw(s, RLWINM, arg0, TCG_REG_R0, sh, 31, 31);
1503        break;
1504
1505    default:
1506        tcg_abort();
1507    }
1508}
1509
1510static void tcg_out_bc(TCGContext *s, int bc, TCGLabel *l)
1511{
1512    if (l->has_value) {
1513        bc |= reloc_pc14_val(tcg_splitwx_to_rx(s->code_ptr), l->u.value_ptr);
1514    } else {
1515        tcg_out_reloc(s, s->code_ptr, R_PPC_REL14, l, 0);
1516    }
1517    tcg_out32(s, bc);
1518}
1519
1520static void tcg_out_brcond(TCGContext *s, TCGCond cond,
1521                           TCGArg arg1, TCGArg arg2, int const_arg2,
1522                           TCGLabel *l, TCGType type)
1523{
1524    tcg_out_cmp(s, cond, arg1, arg2, const_arg2, 7, type);
1525    tcg_out_bc(s, tcg_to_bc[cond], l);
1526}
1527
1528static void tcg_out_movcond(TCGContext *s, TCGType type, TCGCond cond,
1529                            TCGArg dest, TCGArg c1, TCGArg c2, TCGArg v1,
1530                            TCGArg v2, bool const_c2)
1531{
1532    /* If for some reason both inputs are zero, don't produce bad code.  */
1533    if (v1 == 0 && v2 == 0) {
1534        tcg_out_movi(s, type, dest, 0);
1535        return;
1536    }
1537
1538    tcg_out_cmp(s, cond, c1, c2, const_c2, 7, type);
1539
1540    if (have_isel) {
1541        int isel = tcg_to_isel[cond];
1542
1543        /* Swap the V operands if the operation indicates inversion.  */
1544        if (isel & 1) {
1545            int t = v1;
1546            v1 = v2;
1547            v2 = t;
1548            isel &= ~1;
1549        }
1550        /* V1 == 0 is handled by isel; V2 == 0 must be handled by hand.  */
1551        if (v2 == 0) {
1552            tcg_out_movi(s, type, TCG_REG_R0, 0);
1553        }
1554        tcg_out32(s, isel | TAB(dest, v1, v2));
1555    } else {
1556        if (dest == v2) {
1557            cond = tcg_invert_cond(cond);
1558            v2 = v1;
1559        } else if (dest != v1) {
1560            if (v1 == 0) {
1561                tcg_out_movi(s, type, dest, 0);
1562            } else {
1563                tcg_out_mov(s, type, dest, v1);
1564            }
1565        }
1566        /* Branch forward over one insn */
1567        tcg_out32(s, tcg_to_bc[cond] | 8);
1568        if (v2 == 0) {
1569            tcg_out_movi(s, type, dest, 0);
1570        } else {
1571            tcg_out_mov(s, type, dest, v2);
1572        }
1573    }
1574}
1575
1576static void tcg_out_cntxz(TCGContext *s, TCGType type, uint32_t opc,
1577                          TCGArg a0, TCGArg a1, TCGArg a2, bool const_a2)
1578{
1579    if (const_a2 && a2 == (type == TCG_TYPE_I32 ? 32 : 64)) {
1580        tcg_out32(s, opc | RA(a0) | RS(a1));
1581    } else {
1582        tcg_out_cmp(s, TCG_COND_EQ, a1, 0, 1, 7, type);
1583        /* Note that the only other valid constant for a2 is 0.  */
1584        if (have_isel) {
1585            tcg_out32(s, opc | RA(TCG_REG_R0) | RS(a1));
1586            tcg_out32(s, tcg_to_isel[TCG_COND_EQ] | TAB(a0, a2, TCG_REG_R0));
1587        } else if (!const_a2 && a0 == a2) {
1588            tcg_out32(s, tcg_to_bc[TCG_COND_EQ] | 8);
1589            tcg_out32(s, opc | RA(a0) | RS(a1));
1590        } else {
1591            tcg_out32(s, opc | RA(a0) | RS(a1));
1592            tcg_out32(s, tcg_to_bc[TCG_COND_NE] | 8);
1593            if (const_a2) {
1594                tcg_out_movi(s, type, a0, 0);
1595            } else {
1596                tcg_out_mov(s, type, a0, a2);
1597            }
1598        }
1599    }
1600}
1601
1602static void tcg_out_cmp2(TCGContext *s, const TCGArg *args,
1603                         const int *const_args)
1604{
1605    static const struct { uint8_t bit1, bit2; } bits[] = {
1606        [TCG_COND_LT ] = { CR_LT, CR_LT },
1607        [TCG_COND_LE ] = { CR_LT, CR_GT },
1608        [TCG_COND_GT ] = { CR_GT, CR_GT },
1609        [TCG_COND_GE ] = { CR_GT, CR_LT },
1610        [TCG_COND_LTU] = { CR_LT, CR_LT },
1611        [TCG_COND_LEU] = { CR_LT, CR_GT },
1612        [TCG_COND_GTU] = { CR_GT, CR_GT },
1613        [TCG_COND_GEU] = { CR_GT, CR_LT },
1614    };
1615
1616    TCGCond cond = args[4], cond2;
1617    TCGArg al, ah, bl, bh;
1618    int blconst, bhconst;
1619    int op, bit1, bit2;
1620
1621    al = args[0];
1622    ah = args[1];
1623    bl = args[2];
1624    bh = args[3];
1625    blconst = const_args[2];
1626    bhconst = const_args[3];
1627
1628    switch (cond) {
1629    case TCG_COND_EQ:
1630        op = CRAND;
1631        goto do_equality;
1632    case TCG_COND_NE:
1633        op = CRNAND;
1634    do_equality:
1635        tcg_out_cmp(s, cond, al, bl, blconst, 6, TCG_TYPE_I32);
1636        tcg_out_cmp(s, cond, ah, bh, bhconst, 7, TCG_TYPE_I32);
1637        tcg_out32(s, op | BT(7, CR_EQ) | BA(6, CR_EQ) | BB(7, CR_EQ));
1638        break;
1639
1640    case TCG_COND_LT:
1641    case TCG_COND_LE:
1642    case TCG_COND_GT:
1643    case TCG_COND_GE:
1644    case TCG_COND_LTU:
1645    case TCG_COND_LEU:
1646    case TCG_COND_GTU:
1647    case TCG_COND_GEU:
1648        bit1 = bits[cond].bit1;
1649        bit2 = bits[cond].bit2;
1650        op = (bit1 != bit2 ? CRANDC : CRAND);
1651        cond2 = tcg_unsigned_cond(cond);
1652
1653        tcg_out_cmp(s, cond, ah, bh, bhconst, 6, TCG_TYPE_I32);
1654        tcg_out_cmp(s, cond2, al, bl, blconst, 7, TCG_TYPE_I32);
1655        tcg_out32(s, op | BT(7, CR_EQ) | BA(6, CR_EQ) | BB(7, bit2));
1656        tcg_out32(s, CROR | BT(7, CR_EQ) | BA(6, bit1) | BB(7, CR_EQ));
1657        break;
1658
1659    default:
1660        tcg_abort();
1661    }
1662}
1663
1664static void tcg_out_setcond2(TCGContext *s, const TCGArg *args,
1665                             const int *const_args)
1666{
1667    tcg_out_cmp2(s, args + 1, const_args + 1);
1668    tcg_out32(s, MFOCRF | RT(TCG_REG_R0) | FXM(7));
1669    tcg_out_rlw(s, RLWINM, args[0], TCG_REG_R0, 31, 31, 31);
1670}
1671
1672static void tcg_out_brcond2 (TCGContext *s, const TCGArg *args,
1673                             const int *const_args)
1674{
1675    tcg_out_cmp2(s, args, const_args);
1676    tcg_out_bc(s, BC | BI(7, CR_EQ) | BO_COND_TRUE, arg_label(args[5]));
1677}
1678
1679static void tcg_out_mb(TCGContext *s, TCGArg a0)
1680{
1681    uint32_t insn = HWSYNC;
1682    a0 &= TCG_MO_ALL;
1683    if (a0 == TCG_MO_LD_LD) {
1684        insn = LWSYNC;
1685    } else if (a0 == TCG_MO_ST_ST) {
1686        insn = EIEIO;
1687    }
1688    tcg_out32(s, insn);
1689}
1690
1691void tb_target_set_jmp_target(uintptr_t tc_ptr, uintptr_t jmp_rx,
1692                              uintptr_t jmp_rw, uintptr_t addr)
1693{
1694    if (TCG_TARGET_REG_BITS == 64) {
1695        tcg_insn_unit i1, i2;
1696        intptr_t tb_diff = addr - tc_ptr;
1697        intptr_t br_diff = addr - (jmp_rx + 4);
1698        uint64_t pair;
1699
1700        /* This does not exercise the range of the branch, but we do
1701           still need to be able to load the new value of TCG_REG_TB.
1702           But this does still happen quite often.  */
1703        if (tb_diff == (int16_t)tb_diff) {
1704            i1 = ADDI | TAI(TCG_REG_TB, TCG_REG_TB, tb_diff);
1705            i2 = B | (br_diff & 0x3fffffc);
1706        } else {
1707            intptr_t lo = (int16_t)tb_diff;
1708            intptr_t hi = (int32_t)(tb_diff - lo);
1709            assert(tb_diff == hi + lo);
1710            i1 = ADDIS | TAI(TCG_REG_TB, TCG_REG_TB, hi >> 16);
1711            i2 = ADDI | TAI(TCG_REG_TB, TCG_REG_TB, lo);
1712        }
1713#ifdef HOST_WORDS_BIGENDIAN
1714        pair = (uint64_t)i1 << 32 | i2;
1715#else
1716        pair = (uint64_t)i2 << 32 | i1;
1717#endif
1718
1719        /* As per the enclosing if, this is ppc64.  Avoid the _Static_assert
1720           within qatomic_set that would fail to build a ppc32 host.  */
1721        qatomic_set__nocheck((uint64_t *)jmp_rw, pair);
1722        flush_idcache_range(jmp_rx, jmp_rw, 8);
1723    } else {
1724        intptr_t diff = addr - jmp_rx;
1725        tcg_debug_assert(in_range_b(diff));
1726        qatomic_set((uint32_t *)jmp_rw, B | (diff & 0x3fffffc));
1727        flush_idcache_range(jmp_rx, jmp_rw, 4);
1728    }
1729}
1730
1731static void tcg_out_call(TCGContext *s, const tcg_insn_unit *target)
1732{
1733#ifdef _CALL_AIX
1734    /* Look through the descriptor.  If the branch is in range, and we
1735       don't have to spend too much effort on building the toc.  */
1736    const void *tgt = ((const void * const *)target)[0];
1737    uintptr_t toc = ((const uintptr_t *)target)[1];
1738    intptr_t diff = tcg_pcrel_diff(s, tgt);
1739
1740    if (in_range_b(diff) && toc == (uint32_t)toc) {
1741        tcg_out_movi(s, TCG_TYPE_PTR, TCG_REG_TMP1, toc);
1742        tcg_out_b(s, LK, tgt);
1743    } else {
1744        /* Fold the low bits of the constant into the addresses below.  */
1745        intptr_t arg = (intptr_t)target;
1746        int ofs = (int16_t)arg;
1747
1748        if (ofs + 8 < 0x8000) {
1749            arg -= ofs;
1750        } else {
1751            ofs = 0;
1752        }
1753        tcg_out_movi(s, TCG_TYPE_PTR, TCG_REG_TMP1, arg);
1754        tcg_out_ld(s, TCG_TYPE_PTR, TCG_REG_R0, TCG_REG_TMP1, ofs);
1755        tcg_out32(s, MTSPR | RA(TCG_REG_R0) | CTR);
1756        tcg_out_ld(s, TCG_TYPE_PTR, TCG_REG_R2, TCG_REG_TMP1, ofs + SZP);
1757        tcg_out32(s, BCCTR | BO_ALWAYS | LK);
1758    }
1759#elif defined(_CALL_ELF) && _CALL_ELF == 2
1760    intptr_t diff;
1761
1762    /* In the ELFv2 ABI, we have to set up r12 to contain the destination
1763       address, which the callee uses to compute its TOC address.  */
1764    /* FIXME: when the branch is in range, we could avoid r12 load if we
1765       knew that the destination uses the same TOC, and what its local
1766       entry point offset is.  */
1767    tcg_out_movi(s, TCG_TYPE_PTR, TCG_REG_R12, (intptr_t)target);
1768
1769    diff = tcg_pcrel_diff(s, target);
1770    if (in_range_b(diff)) {
1771        tcg_out_b(s, LK, target);
1772    } else {
1773        tcg_out32(s, MTSPR | RS(TCG_REG_R12) | CTR);
1774        tcg_out32(s, BCCTR | BO_ALWAYS | LK);
1775    }
1776#else
1777    tcg_out_b(s, LK, target);
1778#endif
1779}
1780
1781static const uint32_t qemu_ldx_opc[16] = {
1782    [MO_UB] = LBZX,
1783    [MO_UW] = LHZX,
1784    [MO_UL] = LWZX,
1785    [MO_Q]  = LDX,
1786    [MO_SW] = LHAX,
1787    [MO_SL] = LWAX,
1788    [MO_BSWAP | MO_UB] = LBZX,
1789    [MO_BSWAP | MO_UW] = LHBRX,
1790    [MO_BSWAP | MO_UL] = LWBRX,
1791    [MO_BSWAP | MO_Q]  = LDBRX,
1792};
1793
1794static const uint32_t qemu_stx_opc[16] = {
1795    [MO_UB] = STBX,
1796    [MO_UW] = STHX,
1797    [MO_UL] = STWX,
1798    [MO_Q]  = STDX,
1799    [MO_BSWAP | MO_UB] = STBX,
1800    [MO_BSWAP | MO_UW] = STHBRX,
1801    [MO_BSWAP | MO_UL] = STWBRX,
1802    [MO_BSWAP | MO_Q]  = STDBRX,
1803};
1804
1805static const uint32_t qemu_exts_opc[4] = {
1806    EXTSB, EXTSH, EXTSW, 0
1807};
1808
1809#if defined (CONFIG_SOFTMMU)
1810#include "../tcg-ldst.c.inc"
1811
1812/* helper signature: helper_ld_mmu(CPUState *env, target_ulong addr,
1813 *                                 int mmu_idx, uintptr_t ra)
1814 */
1815static void * const qemu_ld_helpers[16] = {
1816    [MO_UB]   = helper_ret_ldub_mmu,
1817    [MO_LEUW] = helper_le_lduw_mmu,
1818    [MO_LEUL] = helper_le_ldul_mmu,
1819    [MO_LEQ]  = helper_le_ldq_mmu,
1820    [MO_BEUW] = helper_be_lduw_mmu,
1821    [MO_BEUL] = helper_be_ldul_mmu,
1822    [MO_BEQ]  = helper_be_ldq_mmu,
1823};
1824
1825/* helper signature: helper_st_mmu(CPUState *env, target_ulong addr,
1826 *                                 uintxx_t val, int mmu_idx, uintptr_t ra)
1827 */
1828static void * const qemu_st_helpers[16] = {
1829    [MO_UB]   = helper_ret_stb_mmu,
1830    [MO_LEUW] = helper_le_stw_mmu,
1831    [MO_LEUL] = helper_le_stl_mmu,
1832    [MO_LEQ]  = helper_le_stq_mmu,
1833    [MO_BEUW] = helper_be_stw_mmu,
1834    [MO_BEUL] = helper_be_stl_mmu,
1835    [MO_BEQ]  = helper_be_stq_mmu,
1836};
1837
1838/* We expect to use a 16-bit negative offset from ENV.  */
1839QEMU_BUILD_BUG_ON(TLB_MASK_TABLE_OFS(0) > 0);
1840QEMU_BUILD_BUG_ON(TLB_MASK_TABLE_OFS(0) < -32768);
1841
1842/* Perform the TLB load and compare.  Places the result of the comparison
1843   in CR7, loads the addend of the TLB into R3, and returns the register
1844   containing the guest address (zero-extended into R4).  Clobbers R0 and R2. */
1845
1846static TCGReg tcg_out_tlb_read(TCGContext *s, MemOp opc,
1847                               TCGReg addrlo, TCGReg addrhi,
1848                               int mem_index, bool is_read)
1849{
1850    int cmp_off
1851        = (is_read
1852           ? offsetof(CPUTLBEntry, addr_read)
1853           : offsetof(CPUTLBEntry, addr_write));
1854    int fast_off = TLB_MASK_TABLE_OFS(mem_index);
1855    int mask_off = fast_off + offsetof(CPUTLBDescFast, mask);
1856    int table_off = fast_off + offsetof(CPUTLBDescFast, table);
1857    unsigned s_bits = opc & MO_SIZE;
1858    unsigned a_bits = get_alignment_bits(opc);
1859
1860    /* Load tlb_mask[mmu_idx] and tlb_table[mmu_idx].  */
1861    tcg_out_ld(s, TCG_TYPE_PTR, TCG_REG_R3, TCG_AREG0, mask_off);
1862    tcg_out_ld(s, TCG_TYPE_PTR, TCG_REG_R4, TCG_AREG0, table_off);
1863
1864    /* Extract the page index, shifted into place for tlb index.  */
1865    if (TCG_TARGET_REG_BITS == 32) {
1866        tcg_out_shri32(s, TCG_REG_TMP1, addrlo,
1867                       TARGET_PAGE_BITS - CPU_TLB_ENTRY_BITS);
1868    } else {
1869        tcg_out_shri64(s, TCG_REG_TMP1, addrlo,
1870                       TARGET_PAGE_BITS - CPU_TLB_ENTRY_BITS);
1871    }
1872    tcg_out32(s, AND | SAB(TCG_REG_R3, TCG_REG_R3, TCG_REG_TMP1));
1873
1874    /* Load the TLB comparator.  */
1875    if (cmp_off == 0 && TCG_TARGET_REG_BITS >= TARGET_LONG_BITS) {
1876        uint32_t lxu = (TCG_TARGET_REG_BITS == 32 || TARGET_LONG_BITS == 32
1877                        ? LWZUX : LDUX);
1878        tcg_out32(s, lxu | TAB(TCG_REG_TMP1, TCG_REG_R3, TCG_REG_R4));
1879    } else {
1880        tcg_out32(s, ADD | TAB(TCG_REG_R3, TCG_REG_R3, TCG_REG_R4));
1881        if (TCG_TARGET_REG_BITS < TARGET_LONG_BITS) {
1882            tcg_out_ld(s, TCG_TYPE_I32, TCG_REG_TMP1, TCG_REG_R3, cmp_off + 4);
1883            tcg_out_ld(s, TCG_TYPE_I32, TCG_REG_R4, TCG_REG_R3, cmp_off);
1884        } else {
1885            tcg_out_ld(s, TCG_TYPE_TL, TCG_REG_TMP1, TCG_REG_R3, cmp_off);
1886        }
1887    }
1888
1889    /* Load the TLB addend for use on the fast path.  Do this asap
1890       to minimize any load use delay.  */
1891    tcg_out_ld(s, TCG_TYPE_PTR, TCG_REG_R3, TCG_REG_R3,
1892               offsetof(CPUTLBEntry, addend));
1893
1894    /* Clear the non-page, non-alignment bits from the address */
1895    if (TCG_TARGET_REG_BITS == 32) {
1896        /* We don't support unaligned accesses on 32-bits.
1897         * Preserve the bottom bits and thus trigger a comparison
1898         * failure on unaligned accesses.
1899         */
1900        if (a_bits < s_bits) {
1901            a_bits = s_bits;
1902        }
1903        tcg_out_rlw(s, RLWINM, TCG_REG_R0, addrlo, 0,
1904                    (32 - a_bits) & 31, 31 - TARGET_PAGE_BITS);
1905    } else {
1906        TCGReg t = addrlo;
1907
1908        /* If the access is unaligned, we need to make sure we fail if we
1909         * cross a page boundary.  The trick is to add the access size-1
1910         * to the address before masking the low bits.  That will make the
1911         * address overflow to the next page if we cross a page boundary,
1912         * which will then force a mismatch of the TLB compare.
1913         */
1914        if (a_bits < s_bits) {
1915            unsigned a_mask = (1 << a_bits) - 1;
1916            unsigned s_mask = (1 << s_bits) - 1;
1917            tcg_out32(s, ADDI | TAI(TCG_REG_R0, t, s_mask - a_mask));
1918            t = TCG_REG_R0;
1919        }
1920
1921        /* Mask the address for the requested alignment.  */
1922        if (TARGET_LONG_BITS == 32) {
1923            tcg_out_rlw(s, RLWINM, TCG_REG_R0, t, 0,
1924                        (32 - a_bits) & 31, 31 - TARGET_PAGE_BITS);
1925            /* Zero-extend the address for use in the final address.  */
1926            tcg_out_ext32u(s, TCG_REG_R4, addrlo);
1927            addrlo = TCG_REG_R4;
1928        } else if (a_bits == 0) {
1929            tcg_out_rld(s, RLDICR, TCG_REG_R0, t, 0, 63 - TARGET_PAGE_BITS);
1930        } else {
1931            tcg_out_rld(s, RLDICL, TCG_REG_R0, t,
1932                        64 - TARGET_PAGE_BITS, TARGET_PAGE_BITS - a_bits);
1933            tcg_out_rld(s, RLDICL, TCG_REG_R0, TCG_REG_R0, TARGET_PAGE_BITS, 0);
1934        }
1935    }
1936
1937    if (TCG_TARGET_REG_BITS < TARGET_LONG_BITS) {
1938        tcg_out_cmp(s, TCG_COND_EQ, TCG_REG_R0, TCG_REG_TMP1,
1939                    0, 7, TCG_TYPE_I32);
1940        tcg_out_cmp(s, TCG_COND_EQ, addrhi, TCG_REG_R4, 0, 6, TCG_TYPE_I32);
1941        tcg_out32(s, CRAND | BT(7, CR_EQ) | BA(6, CR_EQ) | BB(7, CR_EQ));
1942    } else {
1943        tcg_out_cmp(s, TCG_COND_EQ, TCG_REG_R0, TCG_REG_TMP1,
1944                    0, 7, TCG_TYPE_TL);
1945    }
1946
1947    return addrlo;
1948}
1949
1950/* Record the context of a call to the out of line helper code for the slow
1951   path for a load or store, so that we can later generate the correct
1952   helper code.  */
1953static void add_qemu_ldst_label(TCGContext *s, bool is_ld, TCGMemOpIdx oi,
1954                                TCGReg datalo_reg, TCGReg datahi_reg,
1955                                TCGReg addrlo_reg, TCGReg addrhi_reg,
1956                                tcg_insn_unit *raddr, tcg_insn_unit *lptr)
1957{
1958    TCGLabelQemuLdst *label = new_ldst_label(s);
1959
1960    label->is_ld = is_ld;
1961    label->oi = oi;
1962    label->datalo_reg = datalo_reg;
1963    label->datahi_reg = datahi_reg;
1964    label->addrlo_reg = addrlo_reg;
1965    label->addrhi_reg = addrhi_reg;
1966    label->raddr = tcg_splitwx_to_rx(raddr);
1967    label->label_ptr[0] = lptr;
1968}
1969
1970static bool tcg_out_qemu_ld_slow_path(TCGContext *s, TCGLabelQemuLdst *lb)
1971{
1972    TCGMemOpIdx oi = lb->oi;
1973    MemOp opc = get_memop(oi);
1974    TCGReg hi, lo, arg = TCG_REG_R3;
1975
1976    if (!reloc_pc14(lb->label_ptr[0], tcg_splitwx_to_rx(s->code_ptr))) {
1977        return false;
1978    }
1979
1980    tcg_out_mov(s, TCG_TYPE_PTR, arg++, TCG_AREG0);
1981
1982    lo = lb->addrlo_reg;
1983    hi = lb->addrhi_reg;
1984    if (TCG_TARGET_REG_BITS < TARGET_LONG_BITS) {
1985#ifdef TCG_TARGET_CALL_ALIGN_ARGS
1986        arg |= 1;
1987#endif
1988        tcg_out_mov(s, TCG_TYPE_I32, arg++, hi);
1989        tcg_out_mov(s, TCG_TYPE_I32, arg++, lo);
1990    } else {
1991        /* If the address needed to be zero-extended, we'll have already
1992           placed it in R4.  The only remaining case is 64-bit guest.  */
1993        tcg_out_mov(s, TCG_TYPE_TL, arg++, lo);
1994    }
1995
1996    tcg_out_movi(s, TCG_TYPE_I32, arg++, oi);
1997    tcg_out32(s, MFSPR | RT(arg) | LR);
1998
1999    tcg_out_call(s, qemu_ld_helpers[opc & (MO_BSWAP | MO_SIZE)]);
2000
2001    lo = lb->datalo_reg;
2002    hi = lb->datahi_reg;
2003    if (TCG_TARGET_REG_BITS == 32 && (opc & MO_SIZE) == MO_64) {
2004        tcg_out_mov(s, TCG_TYPE_I32, lo, TCG_REG_R4);
2005        tcg_out_mov(s, TCG_TYPE_I32, hi, TCG_REG_R3);
2006    } else if (opc & MO_SIGN) {
2007        uint32_t insn = qemu_exts_opc[opc & MO_SIZE];
2008        tcg_out32(s, insn | RA(lo) | RS(TCG_REG_R3));
2009    } else {
2010        tcg_out_mov(s, TCG_TYPE_REG, lo, TCG_REG_R3);
2011    }
2012
2013    tcg_out_b(s, 0, lb->raddr);
2014    return true;
2015}
2016
2017static bool tcg_out_qemu_st_slow_path(TCGContext *s, TCGLabelQemuLdst *lb)
2018{
2019    TCGMemOpIdx oi = lb->oi;
2020    MemOp opc = get_memop(oi);
2021    MemOp s_bits = opc & MO_SIZE;
2022    TCGReg hi, lo, arg = TCG_REG_R3;
2023
2024    if (!reloc_pc14(lb->label_ptr[0], tcg_splitwx_to_rx(s->code_ptr))) {
2025        return false;
2026    }
2027
2028    tcg_out_mov(s, TCG_TYPE_PTR, arg++, TCG_AREG0);
2029
2030    lo = lb->addrlo_reg;
2031    hi = lb->addrhi_reg;
2032    if (TCG_TARGET_REG_BITS < TARGET_LONG_BITS) {
2033#ifdef TCG_TARGET_CALL_ALIGN_ARGS
2034        arg |= 1;
2035#endif
2036        tcg_out_mov(s, TCG_TYPE_I32, arg++, hi);
2037        tcg_out_mov(s, TCG_TYPE_I32, arg++, lo);
2038    } else {
2039        /* If the address needed to be zero-extended, we'll have already
2040           placed it in R4.  The only remaining case is 64-bit guest.  */
2041        tcg_out_mov(s, TCG_TYPE_TL, arg++, lo);
2042    }
2043
2044    lo = lb->datalo_reg;
2045    hi = lb->datahi_reg;
2046    if (TCG_TARGET_REG_BITS == 32) {
2047        switch (s_bits) {
2048        case MO_64:
2049#ifdef TCG_TARGET_CALL_ALIGN_ARGS
2050            arg |= 1;
2051#endif
2052            tcg_out_mov(s, TCG_TYPE_I32, arg++, hi);
2053            /* FALLTHRU */
2054        case MO_32:
2055            tcg_out_mov(s, TCG_TYPE_I32, arg++, lo);
2056            break;
2057        default:
2058            tcg_out_rlw(s, RLWINM, arg++, lo, 0, 32 - (8 << s_bits), 31);
2059            break;
2060        }
2061    } else {
2062        if (s_bits == MO_64) {
2063            tcg_out_mov(s, TCG_TYPE_I64, arg++, lo);
2064        } else {
2065            tcg_out_rld(s, RLDICL, arg++, lo, 0, 64 - (8 << s_bits));
2066        }
2067    }
2068
2069    tcg_out_movi(s, TCG_TYPE_I32, arg++, oi);
2070    tcg_out32(s, MFSPR | RT(arg) | LR);
2071
2072    tcg_out_call(s, qemu_st_helpers[opc & (MO_BSWAP | MO_SIZE)]);
2073
2074    tcg_out_b(s, 0, lb->raddr);
2075    return true;
2076}
2077#endif /* SOFTMMU */
2078
2079static void tcg_out_qemu_ld(TCGContext *s, const TCGArg *args, bool is_64)
2080{
2081    TCGReg datalo, datahi, addrlo, rbase;
2082    TCGReg addrhi __attribute__((unused));
2083    TCGMemOpIdx oi;
2084    MemOp opc, s_bits;
2085#ifdef CONFIG_SOFTMMU
2086    int mem_index;
2087    tcg_insn_unit *label_ptr;
2088#endif
2089
2090    datalo = *args++;
2091    datahi = (TCG_TARGET_REG_BITS == 32 && is_64 ? *args++ : 0);
2092    addrlo = *args++;
2093    addrhi = (TCG_TARGET_REG_BITS < TARGET_LONG_BITS ? *args++ : 0);
2094    oi = *args++;
2095    opc = get_memop(oi);
2096    s_bits = opc & MO_SIZE;
2097
2098#ifdef CONFIG_SOFTMMU
2099    mem_index = get_mmuidx(oi);
2100    addrlo = tcg_out_tlb_read(s, opc, addrlo, addrhi, mem_index, true);
2101
2102    /* Load a pointer into the current opcode w/conditional branch-link. */
2103    label_ptr = s->code_ptr;
2104    tcg_out32(s, BC | BI(7, CR_EQ) | BO_COND_FALSE | LK);
2105
2106    rbase = TCG_REG_R3;
2107#else  /* !CONFIG_SOFTMMU */
2108    rbase = guest_base ? TCG_GUEST_BASE_REG : 0;
2109    if (TCG_TARGET_REG_BITS > TARGET_LONG_BITS) {
2110        tcg_out_ext32u(s, TCG_REG_TMP1, addrlo);
2111        addrlo = TCG_REG_TMP1;
2112    }
2113#endif
2114
2115    if (TCG_TARGET_REG_BITS == 32 && s_bits == MO_64) {
2116        if (opc & MO_BSWAP) {
2117            tcg_out32(s, ADDI | TAI(TCG_REG_R0, addrlo, 4));
2118            tcg_out32(s, LWBRX | TAB(datalo, rbase, addrlo));
2119            tcg_out32(s, LWBRX | TAB(datahi, rbase, TCG_REG_R0));
2120        } else if (rbase != 0) {
2121            tcg_out32(s, ADDI | TAI(TCG_REG_R0, addrlo, 4));
2122            tcg_out32(s, LWZX | TAB(datahi, rbase, addrlo));
2123            tcg_out32(s, LWZX | TAB(datalo, rbase, TCG_REG_R0));
2124        } else if (addrlo == datahi) {
2125            tcg_out32(s, LWZ | TAI(datalo, addrlo, 4));
2126            tcg_out32(s, LWZ | TAI(datahi, addrlo, 0));
2127        } else {
2128            tcg_out32(s, LWZ | TAI(datahi, addrlo, 0));
2129            tcg_out32(s, LWZ | TAI(datalo, addrlo, 4));
2130        }
2131    } else {
2132        uint32_t insn = qemu_ldx_opc[opc & (MO_BSWAP | MO_SSIZE)];
2133        if (!have_isa_2_06 && insn == LDBRX) {
2134            tcg_out32(s, ADDI | TAI(TCG_REG_R0, addrlo, 4));
2135            tcg_out32(s, LWBRX | TAB(datalo, rbase, addrlo));
2136            tcg_out32(s, LWBRX | TAB(TCG_REG_R0, rbase, TCG_REG_R0));
2137            tcg_out_rld(s, RLDIMI, datalo, TCG_REG_R0, 32, 0);
2138        } else if (insn) {
2139            tcg_out32(s, insn | TAB(datalo, rbase, addrlo));
2140        } else {
2141            insn = qemu_ldx_opc[opc & (MO_SIZE | MO_BSWAP)];
2142            tcg_out32(s, insn | TAB(datalo, rbase, addrlo));
2143            insn = qemu_exts_opc[s_bits];
2144            tcg_out32(s, insn | RA(datalo) | RS(datalo));
2145        }
2146    }
2147
2148#ifdef CONFIG_SOFTMMU
2149    add_qemu_ldst_label(s, true, oi, datalo, datahi, addrlo, addrhi,
2150                        s->code_ptr, label_ptr);
2151#endif
2152}
2153
2154static void tcg_out_qemu_st(TCGContext *s, const TCGArg *args, bool is_64)
2155{
2156    TCGReg datalo, datahi, addrlo, rbase;
2157    TCGReg addrhi __attribute__((unused));
2158    TCGMemOpIdx oi;
2159    MemOp opc, s_bits;
2160#ifdef CONFIG_SOFTMMU
2161    int mem_index;
2162    tcg_insn_unit *label_ptr;
2163#endif
2164
2165    datalo = *args++;
2166    datahi = (TCG_TARGET_REG_BITS == 32 && is_64 ? *args++ : 0);
2167    addrlo = *args++;
2168    addrhi = (TCG_TARGET_REG_BITS < TARGET_LONG_BITS ? *args++ : 0);
2169    oi = *args++;
2170    opc = get_memop(oi);
2171    s_bits = opc & MO_SIZE;
2172
2173#ifdef CONFIG_SOFTMMU
2174    mem_index = get_mmuidx(oi);
2175    addrlo = tcg_out_tlb_read(s, opc, addrlo, addrhi, mem_index, false);
2176
2177    /* Load a pointer into the current opcode w/conditional branch-link. */
2178    label_ptr = s->code_ptr;
2179    tcg_out32(s, BC | BI(7, CR_EQ) | BO_COND_FALSE | LK);
2180
2181    rbase = TCG_REG_R3;
2182#else  /* !CONFIG_SOFTMMU */
2183    rbase = guest_base ? TCG_GUEST_BASE_REG : 0;
2184    if (TCG_TARGET_REG_BITS > TARGET_LONG_BITS) {
2185        tcg_out_ext32u(s, TCG_REG_TMP1, addrlo);
2186        addrlo = TCG_REG_TMP1;
2187    }
2188#endif
2189
2190    if (TCG_TARGET_REG_BITS == 32 && s_bits == MO_64) {
2191        if (opc & MO_BSWAP) {
2192            tcg_out32(s, ADDI | TAI(TCG_REG_R0, addrlo, 4));
2193            tcg_out32(s, STWBRX | SAB(datalo, rbase, addrlo));
2194            tcg_out32(s, STWBRX | SAB(datahi, rbase, TCG_REG_R0));
2195        } else if (rbase != 0) {
2196            tcg_out32(s, ADDI | TAI(TCG_REG_R0, addrlo, 4));
2197            tcg_out32(s, STWX | SAB(datahi, rbase, addrlo));
2198            tcg_out32(s, STWX | SAB(datalo, rbase, TCG_REG_R0));
2199        } else {
2200            tcg_out32(s, STW | TAI(datahi, addrlo, 0));
2201            tcg_out32(s, STW | TAI(datalo, addrlo, 4));
2202        }
2203    } else {
2204        uint32_t insn = qemu_stx_opc[opc & (MO_BSWAP | MO_SIZE)];
2205        if (!have_isa_2_06 && insn == STDBRX) {
2206            tcg_out32(s, STWBRX | SAB(datalo, rbase, addrlo));
2207            tcg_out32(s, ADDI | TAI(TCG_REG_TMP1, addrlo, 4));
2208            tcg_out_shri64(s, TCG_REG_R0, datalo, 32);
2209            tcg_out32(s, STWBRX | SAB(TCG_REG_R0, rbase, TCG_REG_TMP1));
2210        } else {
2211            tcg_out32(s, insn | SAB(datalo, rbase, addrlo));
2212        }
2213    }
2214
2215#ifdef CONFIG_SOFTMMU
2216    add_qemu_ldst_label(s, false, oi, datalo, datahi, addrlo, addrhi,
2217                        s->code_ptr, label_ptr);
2218#endif
2219}
2220
2221static void tcg_out_nop_fill(tcg_insn_unit *p, int count)
2222{
2223    int i;
2224    for (i = 0; i < count; ++i) {
2225        p[i] = NOP;
2226    }
2227}
2228
2229/* Parameters for function call generation, used in tcg.c.  */
2230#define TCG_TARGET_STACK_ALIGN       16
2231#define TCG_TARGET_EXTEND_ARGS       1
2232
2233#ifdef _CALL_AIX
2234# define LINK_AREA_SIZE                (6 * SZR)
2235# define LR_OFFSET                     (1 * SZR)
2236# define TCG_TARGET_CALL_STACK_OFFSET  (LINK_AREA_SIZE + 8 * SZR)
2237#elif defined(TCG_TARGET_CALL_DARWIN)
2238# define LINK_AREA_SIZE                (6 * SZR)
2239# define LR_OFFSET                     (2 * SZR)
2240#elif TCG_TARGET_REG_BITS == 64
2241# if defined(_CALL_ELF) && _CALL_ELF == 2
2242#  define LINK_AREA_SIZE               (4 * SZR)
2243#  define LR_OFFSET                    (1 * SZR)
2244# endif
2245#else /* TCG_TARGET_REG_BITS == 32 */
2246# if defined(_CALL_SYSV)
2247#  define LINK_AREA_SIZE               (2 * SZR)
2248#  define LR_OFFSET                    (1 * SZR)
2249# endif
2250#endif
2251#ifndef LR_OFFSET
2252# error "Unhandled abi"
2253#endif
2254#ifndef TCG_TARGET_CALL_STACK_OFFSET
2255# define TCG_TARGET_CALL_STACK_OFFSET  LINK_AREA_SIZE
2256#endif
2257
2258#define CPU_TEMP_BUF_SIZE  (CPU_TEMP_BUF_NLONGS * (int)sizeof(long))
2259#define REG_SAVE_SIZE      ((int)ARRAY_SIZE(tcg_target_callee_save_regs) * SZR)
2260
2261#define FRAME_SIZE ((TCG_TARGET_CALL_STACK_OFFSET   \
2262                     + TCG_STATIC_CALL_ARGS_SIZE    \
2263                     + CPU_TEMP_BUF_SIZE            \
2264                     + REG_SAVE_SIZE                \
2265                     + TCG_TARGET_STACK_ALIGN - 1)  \
2266                    & -TCG_TARGET_STACK_ALIGN)
2267
2268#define REG_SAVE_BOT (FRAME_SIZE - REG_SAVE_SIZE)
2269
2270static void tcg_target_qemu_prologue(TCGContext *s)
2271{
2272    int i;
2273
2274#ifdef _CALL_AIX
2275    const void **desc = (const void **)s->code_ptr;
2276    desc[0] = tcg_splitwx_to_rx(desc + 2);  /* entry point */
2277    desc[1] = 0;                            /* environment pointer */
2278    s->code_ptr = (void *)(desc + 2);       /* skip over descriptor */
2279#endif
2280
2281    tcg_set_frame(s, TCG_REG_CALL_STACK, REG_SAVE_BOT - CPU_TEMP_BUF_SIZE,
2282                  CPU_TEMP_BUF_SIZE);
2283
2284    /* Prologue */
2285    tcg_out32(s, MFSPR | RT(TCG_REG_R0) | LR);
2286    tcg_out32(s, (SZR == 8 ? STDU : STWU)
2287              | SAI(TCG_REG_R1, TCG_REG_R1, -FRAME_SIZE));
2288
2289    for (i = 0; i < ARRAY_SIZE(tcg_target_callee_save_regs); ++i) {
2290        tcg_out_st(s, TCG_TYPE_REG, tcg_target_callee_save_regs[i],
2291                   TCG_REG_R1, REG_SAVE_BOT + i * SZR);
2292    }
2293    tcg_out_st(s, TCG_TYPE_PTR, TCG_REG_R0, TCG_REG_R1, FRAME_SIZE+LR_OFFSET);
2294
2295#ifndef CONFIG_SOFTMMU
2296    if (guest_base) {
2297        tcg_out_movi_int(s, TCG_TYPE_PTR, TCG_GUEST_BASE_REG, guest_base, true);
2298        tcg_regset_set_reg(s->reserved_regs, TCG_GUEST_BASE_REG);
2299    }
2300#endif
2301
2302    tcg_out_mov(s, TCG_TYPE_PTR, TCG_AREG0, tcg_target_call_iarg_regs[0]);
2303    tcg_out32(s, MTSPR | RS(tcg_target_call_iarg_regs[1]) | CTR);
2304    if (USE_REG_TB) {
2305        tcg_out_mov(s, TCG_TYPE_PTR, TCG_REG_TB, tcg_target_call_iarg_regs[1]);
2306    }
2307    tcg_out32(s, BCCTR | BO_ALWAYS);
2308
2309    /* Epilogue */
2310    tcg_code_gen_epilogue = tcg_splitwx_to_rx(s->code_ptr);
2311
2312    tcg_out_ld(s, TCG_TYPE_PTR, TCG_REG_R0, TCG_REG_R1, FRAME_SIZE+LR_OFFSET);
2313    for (i = 0; i < ARRAY_SIZE(tcg_target_callee_save_regs); ++i) {
2314        tcg_out_ld(s, TCG_TYPE_REG, tcg_target_callee_save_regs[i],
2315                   TCG_REG_R1, REG_SAVE_BOT + i * SZR);
2316    }
2317    tcg_out32(s, MTSPR | RS(TCG_REG_R0) | LR);
2318    tcg_out32(s, ADDI | TAI(TCG_REG_R1, TCG_REG_R1, FRAME_SIZE));
2319    tcg_out32(s, BCLR | BO_ALWAYS);
2320}
2321
2322static void tcg_out_op(TCGContext *s, TCGOpcode opc,
2323                       const TCGArg args[TCG_MAX_OP_ARGS],
2324                       const int const_args[TCG_MAX_OP_ARGS])
2325{
2326    TCGArg a0, a1, a2;
2327    int c;
2328
2329    switch (opc) {
2330    case INDEX_op_exit_tb:
2331        tcg_out_movi(s, TCG_TYPE_PTR, TCG_REG_R3, args[0]);
2332        tcg_out_b(s, 0, tcg_code_gen_epilogue);
2333        break;
2334    case INDEX_op_goto_tb:
2335        if (s->tb_jmp_insn_offset) {
2336            /* Direct jump. */
2337            if (TCG_TARGET_REG_BITS == 64) {
2338                /* Ensure the next insns are 8-byte aligned. */
2339                if ((uintptr_t)s->code_ptr & 7) {
2340                    tcg_out32(s, NOP);
2341                }
2342                s->tb_jmp_insn_offset[args[0]] = tcg_current_code_size(s);
2343                tcg_out32(s, ADDIS | TAI(TCG_REG_TB, TCG_REG_TB, 0));
2344                tcg_out32(s, ADDI | TAI(TCG_REG_TB, TCG_REG_TB, 0));
2345            } else {
2346                s->tb_jmp_insn_offset[args[0]] = tcg_current_code_size(s);
2347                tcg_out32(s, B);
2348                s->tb_jmp_reset_offset[args[0]] = tcg_current_code_size(s);
2349                break;
2350            }
2351        } else {
2352            /* Indirect jump. */
2353            tcg_debug_assert(s->tb_jmp_insn_offset == NULL);
2354            tcg_out_ld(s, TCG_TYPE_PTR, TCG_REG_TB, 0,
2355                       (intptr_t)(s->tb_jmp_insn_offset + args[0]));
2356        }
2357        tcg_out32(s, MTSPR | RS(TCG_REG_TB) | CTR);
2358        tcg_out32(s, BCCTR | BO_ALWAYS);
2359        set_jmp_reset_offset(s, args[0]);
2360        if (USE_REG_TB) {
2361            /* For the unlinked case, need to reset TCG_REG_TB.  */
2362            tcg_out_mem_long(s, ADDI, ADD, TCG_REG_TB, TCG_REG_TB,
2363                             -tcg_current_code_size(s));
2364        }
2365        break;
2366    case INDEX_op_goto_ptr:
2367        tcg_out32(s, MTSPR | RS(args[0]) | CTR);
2368        if (USE_REG_TB) {
2369            tcg_out_mov(s, TCG_TYPE_PTR, TCG_REG_TB, args[0]);
2370        }
2371        tcg_out32(s, ADDI | TAI(TCG_REG_R3, 0, 0));
2372        tcg_out32(s, BCCTR | BO_ALWAYS);
2373        break;
2374    case INDEX_op_br:
2375        {
2376            TCGLabel *l = arg_label(args[0]);
2377            uint32_t insn = B;
2378
2379            if (l->has_value) {
2380                insn |= reloc_pc24_val(tcg_splitwx_to_rx(s->code_ptr),
2381                                       l->u.value_ptr);
2382            } else {
2383                tcg_out_reloc(s, s->code_ptr, R_PPC_REL24, l, 0);
2384            }
2385            tcg_out32(s, insn);
2386        }
2387        break;
2388    case INDEX_op_ld8u_i32:
2389    case INDEX_op_ld8u_i64:
2390        tcg_out_mem_long(s, LBZ, LBZX, args[0], args[1], args[2]);
2391        break;
2392    case INDEX_op_ld8s_i32:
2393    case INDEX_op_ld8s_i64:
2394        tcg_out_mem_long(s, LBZ, LBZX, args[0], args[1], args[2]);
2395        tcg_out32(s, EXTSB | RS(args[0]) | RA(args[0]));
2396        break;
2397    case INDEX_op_ld16u_i32:
2398    case INDEX_op_ld16u_i64:
2399        tcg_out_mem_long(s, LHZ, LHZX, args[0], args[1], args[2]);
2400        break;
2401    case INDEX_op_ld16s_i32:
2402    case INDEX_op_ld16s_i64:
2403        tcg_out_mem_long(s, LHA, LHAX, args[0], args[1], args[2]);
2404        break;
2405    case INDEX_op_ld_i32:
2406    case INDEX_op_ld32u_i64:
2407        tcg_out_mem_long(s, LWZ, LWZX, args[0], args[1], args[2]);
2408        break;
2409    case INDEX_op_ld32s_i64:
2410        tcg_out_mem_long(s, LWA, LWAX, args[0], args[1], args[2]);
2411        break;
2412    case INDEX_op_ld_i64:
2413        tcg_out_mem_long(s, LD, LDX, args[0], args[1], args[2]);
2414        break;
2415    case INDEX_op_st8_i32:
2416    case INDEX_op_st8_i64:
2417        tcg_out_mem_long(s, STB, STBX, args[0], args[1], args[2]);
2418        break;
2419    case INDEX_op_st16_i32:
2420    case INDEX_op_st16_i64:
2421        tcg_out_mem_long(s, STH, STHX, args[0], args[1], args[2]);
2422        break;
2423    case INDEX_op_st_i32:
2424    case INDEX_op_st32_i64:
2425        tcg_out_mem_long(s, STW, STWX, args[0], args[1], args[2]);
2426        break;
2427    case INDEX_op_st_i64:
2428        tcg_out_mem_long(s, STD, STDX, args[0], args[1], args[2]);
2429        break;
2430
2431    case INDEX_op_add_i32:
2432        a0 = args[0], a1 = args[1], a2 = args[2];
2433        if (const_args[2]) {
2434        do_addi_32:
2435            tcg_out_mem_long(s, ADDI, ADD, a0, a1, (int32_t)a2);
2436        } else {
2437            tcg_out32(s, ADD | TAB(a0, a1, a2));
2438        }
2439        break;
2440    case INDEX_op_sub_i32:
2441        a0 = args[0], a1 = args[1], a2 = args[2];
2442        if (const_args[1]) {
2443            if (const_args[2]) {
2444                tcg_out_movi(s, TCG_TYPE_I32, a0, a1 - a2);
2445            } else {
2446                tcg_out32(s, SUBFIC | TAI(a0, a2, a1));
2447            }
2448        } else if (const_args[2]) {
2449            a2 = -a2;
2450            goto do_addi_32;
2451        } else {
2452            tcg_out32(s, SUBF | TAB(a0, a2, a1));
2453        }
2454        break;
2455
2456    case INDEX_op_and_i32:
2457        a0 = args[0], a1 = args[1], a2 = args[2];
2458        if (const_args[2]) {
2459            tcg_out_andi32(s, a0, a1, a2);
2460        } else {
2461            tcg_out32(s, AND | SAB(a1, a0, a2));
2462        }
2463        break;
2464    case INDEX_op_and_i64:
2465        a0 = args[0], a1 = args[1], a2 = args[2];
2466        if (const_args[2]) {
2467            tcg_out_andi64(s, a0, a1, a2);
2468        } else {
2469            tcg_out32(s, AND | SAB(a1, a0, a2));
2470        }
2471        break;
2472    case INDEX_op_or_i64:
2473    case INDEX_op_or_i32:
2474        a0 = args[0], a1 = args[1], a2 = args[2];
2475        if (const_args[2]) {
2476            tcg_out_ori32(s, a0, a1, a2);
2477        } else {
2478            tcg_out32(s, OR | SAB(a1, a0, a2));
2479        }
2480        break;
2481    case INDEX_op_xor_i64:
2482    case INDEX_op_xor_i32:
2483        a0 = args[0], a1 = args[1], a2 = args[2];
2484        if (const_args[2]) {
2485            tcg_out_xori32(s, a0, a1, a2);
2486        } else {
2487            tcg_out32(s, XOR | SAB(a1, a0, a2));
2488        }
2489        break;
2490    case INDEX_op_andc_i32:
2491        a0 = args[0], a1 = args[1], a2 = args[2];
2492        if (const_args[2]) {
2493            tcg_out_andi32(s, a0, a1, ~a2);
2494        } else {
2495            tcg_out32(s, ANDC | SAB(a1, a0, a2));
2496        }
2497        break;
2498    case INDEX_op_andc_i64:
2499        a0 = args[0], a1 = args[1], a2 = args[2];
2500        if (const_args[2]) {
2501            tcg_out_andi64(s, a0, a1, ~a2);
2502        } else {
2503            tcg_out32(s, ANDC | SAB(a1, a0, a2));
2504        }
2505        break;
2506    case INDEX_op_orc_i32:
2507        if (const_args[2]) {
2508            tcg_out_ori32(s, args[0], args[1], ~args[2]);
2509            break;
2510        }
2511        /* FALLTHRU */
2512    case INDEX_op_orc_i64:
2513        tcg_out32(s, ORC | SAB(args[1], args[0], args[2]));
2514        break;
2515    case INDEX_op_eqv_i32:
2516        if (const_args[2]) {
2517            tcg_out_xori32(s, args[0], args[1], ~args[2]);
2518            break;
2519        }
2520        /* FALLTHRU */
2521    case INDEX_op_eqv_i64:
2522        tcg_out32(s, EQV | SAB(args[1], args[0], args[2]));
2523        break;
2524    case INDEX_op_nand_i32:
2525    case INDEX_op_nand_i64:
2526        tcg_out32(s, NAND | SAB(args[1], args[0], args[2]));
2527        break;
2528    case INDEX_op_nor_i32:
2529    case INDEX_op_nor_i64:
2530        tcg_out32(s, NOR | SAB(args[1], args[0], args[2]));
2531        break;
2532
2533    case INDEX_op_clz_i32:
2534        tcg_out_cntxz(s, TCG_TYPE_I32, CNTLZW, args[0], args[1],
2535                      args[2], const_args[2]);
2536        break;
2537    case INDEX_op_ctz_i32:
2538        tcg_out_cntxz(s, TCG_TYPE_I32, CNTTZW, args[0], args[1],
2539                      args[2], const_args[2]);
2540        break;
2541    case INDEX_op_ctpop_i32:
2542        tcg_out32(s, CNTPOPW | SAB(args[1], args[0], 0));
2543        break;
2544
2545    case INDEX_op_clz_i64:
2546        tcg_out_cntxz(s, TCG_TYPE_I64, CNTLZD, args[0], args[1],
2547                      args[2], const_args[2]);
2548        break;
2549    case INDEX_op_ctz_i64:
2550        tcg_out_cntxz(s, TCG_TYPE_I64, CNTTZD, args[0], args[1],
2551                      args[2], const_args[2]);
2552        break;
2553    case INDEX_op_ctpop_i64:
2554        tcg_out32(s, CNTPOPD | SAB(args[1], args[0], 0));
2555        break;
2556
2557    case INDEX_op_mul_i32:
2558        a0 = args[0], a1 = args[1], a2 = args[2];
2559        if (const_args[2]) {
2560            tcg_out32(s, MULLI | TAI(a0, a1, a2));
2561        } else {
2562            tcg_out32(s, MULLW | TAB(a0, a1, a2));
2563        }
2564        break;
2565
2566    case INDEX_op_div_i32:
2567        tcg_out32(s, DIVW | TAB(args[0], args[1], args[2]));
2568        break;
2569
2570    case INDEX_op_divu_i32:
2571        tcg_out32(s, DIVWU | TAB(args[0], args[1], args[2]));
2572        break;
2573
2574    case INDEX_op_shl_i32:
2575        if (const_args[2]) {
2576            /* Limit immediate shift count lest we create an illegal insn.  */
2577            tcg_out_shli32(s, args[0], args[1], args[2] & 31);
2578        } else {
2579            tcg_out32(s, SLW | SAB(args[1], args[0], args[2]));
2580        }
2581        break;
2582    case INDEX_op_shr_i32:
2583        if (const_args[2]) {
2584            /* Limit immediate shift count lest we create an illegal insn.  */
2585            tcg_out_shri32(s, args[0], args[1], args[2] & 31);
2586        } else {
2587            tcg_out32(s, SRW | SAB(args[1], args[0], args[2]));
2588        }
2589        break;
2590    case INDEX_op_sar_i32:
2591        if (const_args[2]) {
2592            /* Limit immediate shift count lest we create an illegal insn.  */
2593            tcg_out32(s, SRAWI | RS(args[1]) | RA(args[0]) | SH(args[2] & 31));
2594        } else {
2595            tcg_out32(s, SRAW | SAB(args[1], args[0], args[2]));
2596        }
2597        break;
2598    case INDEX_op_rotl_i32:
2599        if (const_args[2]) {
2600            tcg_out_rlw(s, RLWINM, args[0], args[1], args[2], 0, 31);
2601        } else {
2602            tcg_out32(s, RLWNM | SAB(args[1], args[0], args[2])
2603                         | MB(0) | ME(31));
2604        }
2605        break;
2606    case INDEX_op_rotr_i32:
2607        if (const_args[2]) {
2608            tcg_out_rlw(s, RLWINM, args[0], args[1], 32 - args[2], 0, 31);
2609        } else {
2610            tcg_out32(s, SUBFIC | TAI(TCG_REG_R0, args[2], 32));
2611            tcg_out32(s, RLWNM | SAB(args[1], args[0], TCG_REG_R0)
2612                         | MB(0) | ME(31));
2613        }
2614        break;
2615
2616    case INDEX_op_brcond_i32:
2617        tcg_out_brcond(s, args[2], args[0], args[1], const_args[1],
2618                       arg_label(args[3]), TCG_TYPE_I32);
2619        break;
2620    case INDEX_op_brcond_i64:
2621        tcg_out_brcond(s, args[2], args[0], args[1], const_args[1],
2622                       arg_label(args[3]), TCG_TYPE_I64);
2623        break;
2624    case INDEX_op_brcond2_i32:
2625        tcg_out_brcond2(s, args, const_args);
2626        break;
2627
2628    case INDEX_op_neg_i32:
2629    case INDEX_op_neg_i64:
2630        tcg_out32(s, NEG | RT(args[0]) | RA(args[1]));
2631        break;
2632
2633    case INDEX_op_not_i32:
2634    case INDEX_op_not_i64:
2635        tcg_out32(s, NOR | SAB(args[1], args[0], args[1]));
2636        break;
2637
2638    case INDEX_op_add_i64:
2639        a0 = args[0], a1 = args[1], a2 = args[2];
2640        if (const_args[2]) {
2641        do_addi_64:
2642            tcg_out_mem_long(s, ADDI, ADD, a0, a1, a2);
2643        } else {
2644            tcg_out32(s, ADD | TAB(a0, a1, a2));
2645        }
2646        break;
2647    case INDEX_op_sub_i64:
2648        a0 = args[0], a1 = args[1], a2 = args[2];
2649        if (const_args[1]) {
2650            if (const_args[2]) {
2651                tcg_out_movi(s, TCG_TYPE_I64, a0, a1 - a2);
2652            } else {
2653                tcg_out32(s, SUBFIC | TAI(a0, a2, a1));
2654            }
2655        } else if (const_args[2]) {
2656            a2 = -a2;
2657            goto do_addi_64;
2658        } else {
2659            tcg_out32(s, SUBF | TAB(a0, a2, a1));
2660        }
2661        break;
2662
2663    case INDEX_op_shl_i64:
2664        if (const_args[2]) {
2665            /* Limit immediate shift count lest we create an illegal insn.  */
2666            tcg_out_shli64(s, args[0], args[1], args[2] & 63);
2667        } else {
2668            tcg_out32(s, SLD | SAB(args[1], args[0], args[2]));
2669        }
2670        break;
2671    case INDEX_op_shr_i64:
2672        if (const_args[2]) {
2673            /* Limit immediate shift count lest we create an illegal insn.  */
2674            tcg_out_shri64(s, args[0], args[1], args[2] & 63);
2675        } else {
2676            tcg_out32(s, SRD | SAB(args[1], args[0], args[2]));
2677        }
2678        break;
2679    case INDEX_op_sar_i64:
2680        if (const_args[2]) {
2681            int sh = SH(args[2] & 0x1f) | (((args[2] >> 5) & 1) << 1);
2682            tcg_out32(s, SRADI | RA(args[0]) | RS(args[1]) | sh);
2683        } else {
2684            tcg_out32(s, SRAD | SAB(args[1], args[0], args[2]));
2685        }
2686        break;
2687    case INDEX_op_rotl_i64:
2688        if (const_args[2]) {
2689            tcg_out_rld(s, RLDICL, args[0], args[1], args[2], 0);
2690        } else {
2691            tcg_out32(s, RLDCL | SAB(args[1], args[0], args[2]) | MB64(0));
2692        }
2693        break;
2694    case INDEX_op_rotr_i64:
2695        if (const_args[2]) {
2696            tcg_out_rld(s, RLDICL, args[0], args[1], 64 - args[2], 0);
2697        } else {
2698            tcg_out32(s, SUBFIC | TAI(TCG_REG_R0, args[2], 64));
2699            tcg_out32(s, RLDCL | SAB(args[1], args[0], TCG_REG_R0) | MB64(0));
2700        }
2701        break;
2702
2703    case INDEX_op_mul_i64:
2704        a0 = args[0], a1 = args[1], a2 = args[2];
2705        if (const_args[2]) {
2706            tcg_out32(s, MULLI | TAI(a0, a1, a2));
2707        } else {
2708            tcg_out32(s, MULLD | TAB(a0, a1, a2));
2709        }
2710        break;
2711    case INDEX_op_div_i64:
2712        tcg_out32(s, DIVD | TAB(args[0], args[1], args[2]));
2713        break;
2714    case INDEX_op_divu_i64:
2715        tcg_out32(s, DIVDU | TAB(args[0], args[1], args[2]));
2716        break;
2717
2718    case INDEX_op_qemu_ld_i32:
2719        tcg_out_qemu_ld(s, args, false);
2720        break;
2721    case INDEX_op_qemu_ld_i64:
2722        tcg_out_qemu_ld(s, args, true);
2723        break;
2724    case INDEX_op_qemu_st_i32:
2725        tcg_out_qemu_st(s, args, false);
2726        break;
2727    case INDEX_op_qemu_st_i64:
2728        tcg_out_qemu_st(s, args, true);
2729        break;
2730
2731    case INDEX_op_ext8s_i32:
2732    case INDEX_op_ext8s_i64:
2733        c = EXTSB;
2734        goto gen_ext;
2735    case INDEX_op_ext16s_i32:
2736    case INDEX_op_ext16s_i64:
2737        c = EXTSH;
2738        goto gen_ext;
2739    case INDEX_op_ext_i32_i64:
2740    case INDEX_op_ext32s_i64:
2741        c = EXTSW;
2742        goto gen_ext;
2743    gen_ext:
2744        tcg_out32(s, c | RS(args[1]) | RA(args[0]));
2745        break;
2746    case INDEX_op_extu_i32_i64:
2747        tcg_out_ext32u(s, args[0], args[1]);
2748        break;
2749
2750    case INDEX_op_setcond_i32:
2751        tcg_out_setcond(s, TCG_TYPE_I32, args[3], args[0], args[1], args[2],
2752                        const_args[2]);
2753        break;
2754    case INDEX_op_setcond_i64:
2755        tcg_out_setcond(s, TCG_TYPE_I64, args[3], args[0], args[1], args[2],
2756                        const_args[2]);
2757        break;
2758    case INDEX_op_setcond2_i32:
2759        tcg_out_setcond2(s, args, const_args);
2760        break;
2761
2762    case INDEX_op_bswap16_i32:
2763    case INDEX_op_bswap16_i64:
2764        a0 = args[0], a1 = args[1];
2765        /* a1 = abcd */
2766        if (a0 != a1) {
2767            /* a0 = (a1 r<< 24) & 0xff # 000c */
2768            tcg_out_rlw(s, RLWINM, a0, a1, 24, 24, 31);
2769            /* a0 = (a0 & ~0xff00) | (a1 r<< 8) & 0xff00 # 00dc */
2770            tcg_out_rlw(s, RLWIMI, a0, a1, 8, 16, 23);
2771        } else {
2772            /* r0 = (a1 r<< 8) & 0xff00 # 00d0 */
2773            tcg_out_rlw(s, RLWINM, TCG_REG_R0, a1, 8, 16, 23);
2774            /* a0 = (a1 r<< 24) & 0xff # 000c */
2775            tcg_out_rlw(s, RLWINM, a0, a1, 24, 24, 31);
2776            /* a0 = a0 | r0 # 00dc */
2777            tcg_out32(s, OR | SAB(TCG_REG_R0, a0, a0));
2778        }
2779        break;
2780
2781    case INDEX_op_bswap32_i32:
2782    case INDEX_op_bswap32_i64:
2783        /* Stolen from gcc's builtin_bswap32 */
2784        a1 = args[1];
2785        a0 = args[0] == a1 ? TCG_REG_R0 : args[0];
2786
2787        /* a1 = args[1] # abcd */
2788        /* a0 = rotate_left (a1, 8) # bcda */
2789        tcg_out_rlw(s, RLWINM, a0, a1, 8, 0, 31);
2790        /* a0 = (a0 & ~0xff000000) | ((a1 r<< 24) & 0xff000000) # dcda */
2791        tcg_out_rlw(s, RLWIMI, a0, a1, 24, 0, 7);
2792        /* a0 = (a0 & ~0x0000ff00) | ((a1 r<< 24) & 0x0000ff00) # dcba */
2793        tcg_out_rlw(s, RLWIMI, a0, a1, 24, 16, 23);
2794
2795        if (a0 == TCG_REG_R0) {
2796            tcg_out_mov(s, TCG_TYPE_REG, args[0], a0);
2797        }
2798        break;
2799
2800    case INDEX_op_bswap64_i64:
2801        a0 = args[0], a1 = args[1], a2 = TCG_REG_R0;
2802        if (a0 == a1) {
2803            a0 = TCG_REG_R0;
2804            a2 = a1;
2805        }
2806
2807        /* a1 = # abcd efgh */
2808        /* a0 = rl32(a1, 8) # 0000 fghe */
2809        tcg_out_rlw(s, RLWINM, a0, a1, 8, 0, 31);
2810        /* a0 = dep(a0, rl32(a1, 24), 0xff000000) # 0000 hghe */
2811        tcg_out_rlw(s, RLWIMI, a0, a1, 24, 0, 7);
2812        /* a0 = dep(a0, rl32(a1, 24), 0x0000ff00) # 0000 hgfe */
2813        tcg_out_rlw(s, RLWIMI, a0, a1, 24, 16, 23);
2814
2815        /* a0 = rl64(a0, 32) # hgfe 0000 */
2816        /* a2 = rl64(a1, 32) # efgh abcd */
2817        tcg_out_rld(s, RLDICL, a0, a0, 32, 0);
2818        tcg_out_rld(s, RLDICL, a2, a1, 32, 0);
2819
2820        /* a0 = dep(a0, rl32(a2, 8), 0xffffffff)  # hgfe bcda */
2821        tcg_out_rlw(s, RLWIMI, a0, a2, 8, 0, 31);
2822        /* a0 = dep(a0, rl32(a2, 24), 0xff000000) # hgfe dcda */
2823        tcg_out_rlw(s, RLWIMI, a0, a2, 24, 0, 7);
2824        /* a0 = dep(a0, rl32(a2, 24), 0x0000ff00) # hgfe dcba */
2825        tcg_out_rlw(s, RLWIMI, a0, a2, 24, 16, 23);
2826
2827        if (a0 == 0) {
2828            tcg_out_mov(s, TCG_TYPE_REG, args[0], a0);
2829        }
2830        break;
2831
2832    case INDEX_op_deposit_i32:
2833        if (const_args[2]) {
2834            uint32_t mask = ((2u << (args[4] - 1)) - 1) << args[3];
2835            tcg_out_andi32(s, args[0], args[0], ~mask);
2836        } else {
2837            tcg_out_rlw(s, RLWIMI, args[0], args[2], args[3],
2838                        32 - args[3] - args[4], 31 - args[3]);
2839        }
2840        break;
2841    case INDEX_op_deposit_i64:
2842        if (const_args[2]) {
2843            uint64_t mask = ((2ull << (args[4] - 1)) - 1) << args[3];
2844            tcg_out_andi64(s, args[0], args[0], ~mask);
2845        } else {
2846            tcg_out_rld(s, RLDIMI, args[0], args[2], args[3],
2847                        64 - args[3] - args[4]);
2848        }
2849        break;
2850
2851    case INDEX_op_extract_i32:
2852        tcg_out_rlw(s, RLWINM, args[0], args[1],
2853                    32 - args[2], 32 - args[3], 31);
2854        break;
2855    case INDEX_op_extract_i64:
2856        tcg_out_rld(s, RLDICL, args[0], args[1], 64 - args[2], 64 - args[3]);
2857        break;
2858
2859    case INDEX_op_movcond_i32:
2860        tcg_out_movcond(s, TCG_TYPE_I32, args[5], args[0], args[1], args[2],
2861                        args[3], args[4], const_args[2]);
2862        break;
2863    case INDEX_op_movcond_i64:
2864        tcg_out_movcond(s, TCG_TYPE_I64, args[5], args[0], args[1], args[2],
2865                        args[3], args[4], const_args[2]);
2866        break;
2867
2868#if TCG_TARGET_REG_BITS == 64
2869    case INDEX_op_add2_i64:
2870#else
2871    case INDEX_op_add2_i32:
2872#endif
2873        /* Note that the CA bit is defined based on the word size of the
2874           environment.  So in 64-bit mode it's always carry-out of bit 63.
2875           The fallback code using deposit works just as well for 32-bit.  */
2876        a0 = args[0], a1 = args[1];
2877        if (a0 == args[3] || (!const_args[5] && a0 == args[5])) {
2878            a0 = TCG_REG_R0;
2879        }
2880        if (const_args[4]) {
2881            tcg_out32(s, ADDIC | TAI(a0, args[2], args[4]));
2882        } else {
2883            tcg_out32(s, ADDC | TAB(a0, args[2], args[4]));
2884        }
2885        if (const_args[5]) {
2886            tcg_out32(s, (args[5] ? ADDME : ADDZE) | RT(a1) | RA(args[3]));
2887        } else {
2888            tcg_out32(s, ADDE | TAB(a1, args[3], args[5]));
2889        }
2890        if (a0 != args[0]) {
2891            tcg_out_mov(s, TCG_TYPE_REG, args[0], a0);
2892        }
2893        break;
2894
2895#if TCG_TARGET_REG_BITS == 64
2896    case INDEX_op_sub2_i64:
2897#else
2898    case INDEX_op_sub2_i32:
2899#endif
2900        a0 = args[0], a1 = args[1];
2901        if (a0 == args[5] || (!const_args[3] && a0 == args[3])) {
2902            a0 = TCG_REG_R0;
2903        }
2904        if (const_args[2]) {
2905            tcg_out32(s, SUBFIC | TAI(a0, args[4], args[2]));
2906        } else {
2907            tcg_out32(s, SUBFC | TAB(a0, args[4], args[2]));
2908        }
2909        if (const_args[3]) {
2910            tcg_out32(s, (args[3] ? SUBFME : SUBFZE) | RT(a1) | RA(args[5]));
2911        } else {
2912            tcg_out32(s, SUBFE | TAB(a1, args[5], args[3]));
2913        }
2914        if (a0 != args[0]) {
2915            tcg_out_mov(s, TCG_TYPE_REG, args[0], a0);
2916        }
2917        break;
2918
2919    case INDEX_op_muluh_i32:
2920        tcg_out32(s, MULHWU | TAB(args[0], args[1], args[2]));
2921        break;
2922    case INDEX_op_mulsh_i32:
2923        tcg_out32(s, MULHW | TAB(args[0], args[1], args[2]));
2924        break;
2925    case INDEX_op_muluh_i64:
2926        tcg_out32(s, MULHDU | TAB(args[0], args[1], args[2]));
2927        break;
2928    case INDEX_op_mulsh_i64:
2929        tcg_out32(s, MULHD | TAB(args[0], args[1], args[2]));
2930        break;
2931
2932    case INDEX_op_mb:
2933        tcg_out_mb(s, args[0]);
2934        break;
2935
2936    case INDEX_op_mov_i32:   /* Always emitted via tcg_out_mov.  */
2937    case INDEX_op_mov_i64:
2938    case INDEX_op_call:      /* Always emitted via tcg_out_call.  */
2939    default:
2940        tcg_abort();
2941    }
2942}
2943
2944int tcg_can_emit_vec_op(TCGOpcode opc, TCGType type, unsigned vece)
2945{
2946    switch (opc) {
2947    case INDEX_op_and_vec:
2948    case INDEX_op_or_vec:
2949    case INDEX_op_xor_vec:
2950    case INDEX_op_andc_vec:
2951    case INDEX_op_not_vec:
2952        return 1;
2953    case INDEX_op_orc_vec:
2954        return have_isa_2_07;
2955    case INDEX_op_add_vec:
2956    case INDEX_op_sub_vec:
2957    case INDEX_op_smax_vec:
2958    case INDEX_op_smin_vec:
2959    case INDEX_op_umax_vec:
2960    case INDEX_op_umin_vec:
2961    case INDEX_op_shlv_vec:
2962    case INDEX_op_shrv_vec:
2963    case INDEX_op_sarv_vec:
2964    case INDEX_op_rotlv_vec:
2965        return vece <= MO_32 || have_isa_2_07;
2966    case INDEX_op_ssadd_vec:
2967    case INDEX_op_sssub_vec:
2968    case INDEX_op_usadd_vec:
2969    case INDEX_op_ussub_vec:
2970        return vece <= MO_32;
2971    case INDEX_op_cmp_vec:
2972    case INDEX_op_shli_vec:
2973    case INDEX_op_shri_vec:
2974    case INDEX_op_sari_vec:
2975    case INDEX_op_rotli_vec:
2976        return vece <= MO_32 || have_isa_2_07 ? -1 : 0;
2977    case INDEX_op_neg_vec:
2978        return vece >= MO_32 && have_isa_3_00;
2979    case INDEX_op_mul_vec:
2980        switch (vece) {
2981        case MO_8:
2982        case MO_16:
2983            return -1;
2984        case MO_32:
2985            return have_isa_2_07 ? 1 : -1;
2986        case MO_64:
2987            return have_isa_3_10;
2988        }
2989        return 0;
2990    case INDEX_op_bitsel_vec:
2991        return have_vsx;
2992    case INDEX_op_rotrv_vec:
2993        return -1;
2994    default:
2995        return 0;
2996    }
2997}
2998
2999static bool tcg_out_dup_vec(TCGContext *s, TCGType type, unsigned vece,
3000                            TCGReg dst, TCGReg src)
3001{
3002    tcg_debug_assert(dst >= TCG_REG_V0);
3003
3004    /* Splat from integer reg allowed via constraints for v3.00.  */
3005    if (src < TCG_REG_V0) {
3006        tcg_debug_assert(have_isa_3_00);
3007        switch (vece) {
3008        case MO_64:
3009            tcg_out32(s, MTVSRDD | VRT(dst) | RA(src) | RB(src));
3010            return true;
3011        case MO_32:
3012            tcg_out32(s, MTVSRWS | VRT(dst) | RA(src));
3013            return true;
3014        default:
3015            /* Fail, so that we fall back on either dupm or mov+dup.  */
3016            return false;
3017        }
3018    }
3019
3020    /*
3021     * Recall we use (or emulate) VSX integer loads, so the integer is
3022     * right justified within the left (zero-index) double-word.
3023     */
3024    switch (vece) {
3025    case MO_8:
3026        tcg_out32(s, VSPLTB | VRT(dst) | VRB(src) | (7 << 16));
3027        break;
3028    case MO_16:
3029        tcg_out32(s, VSPLTH | VRT(dst) | VRB(src) | (3 << 16));
3030        break;
3031    case MO_32:
3032        tcg_out32(s, VSPLTW | VRT(dst) | VRB(src) | (1 << 16));
3033        break;
3034    case MO_64:
3035        if (have_vsx) {
3036            tcg_out32(s, XXPERMDI | VRT(dst) | VRA(src) | VRB(src));
3037            break;
3038        }
3039        tcg_out_vsldoi(s, TCG_VEC_TMP1, src, src, 8);
3040        tcg_out_vsldoi(s, dst, TCG_VEC_TMP1, src, 8);
3041        break;
3042    default:
3043        g_assert_not_reached();
3044    }
3045    return true;
3046}
3047
3048static bool tcg_out_dupm_vec(TCGContext *s, TCGType type, unsigned vece,
3049                             TCGReg out, TCGReg base, intptr_t offset)
3050{
3051    int elt;
3052
3053    tcg_debug_assert(out >= TCG_REG_V0);
3054    switch (vece) {
3055    case MO_8:
3056        if (have_isa_3_00) {
3057            tcg_out_mem_long(s, LXV, LVX, out, base, offset & -16);
3058        } else {
3059            tcg_out_mem_long(s, 0, LVEBX, out, base, offset);
3060        }
3061        elt = extract32(offset, 0, 4);
3062#ifndef HOST_WORDS_BIGENDIAN
3063        elt ^= 15;
3064#endif
3065        tcg_out32(s, VSPLTB | VRT(out) | VRB(out) | (elt << 16));
3066        break;
3067    case MO_16:
3068        tcg_debug_assert((offset & 1) == 0);
3069        if (have_isa_3_00) {
3070            tcg_out_mem_long(s, LXV | 8, LVX, out, base, offset & -16);
3071        } else {
3072            tcg_out_mem_long(s, 0, LVEHX, out, base, offset);
3073        }
3074        elt = extract32(offset, 1, 3);
3075#ifndef HOST_WORDS_BIGENDIAN
3076        elt ^= 7;
3077#endif
3078        tcg_out32(s, VSPLTH | VRT(out) | VRB(out) | (elt << 16));
3079        break;
3080    case MO_32:
3081        if (have_isa_3_00) {
3082            tcg_out_mem_long(s, 0, LXVWSX, out, base, offset);
3083            break;
3084        }
3085        tcg_debug_assert((offset & 3) == 0);
3086        tcg_out_mem_long(s, 0, LVEWX, out, base, offset);
3087        elt = extract32(offset, 2, 2);
3088#ifndef HOST_WORDS_BIGENDIAN
3089        elt ^= 3;
3090#endif
3091        tcg_out32(s, VSPLTW | VRT(out) | VRB(out) | (elt << 16));
3092        break;
3093    case MO_64:
3094        if (have_vsx) {
3095            tcg_out_mem_long(s, 0, LXVDSX, out, base, offset);
3096            break;
3097        }
3098        tcg_debug_assert((offset & 7) == 0);
3099        tcg_out_mem_long(s, 0, LVX, out, base, offset & -16);
3100        tcg_out_vsldoi(s, TCG_VEC_TMP1, out, out, 8);
3101        elt = extract32(offset, 3, 1);
3102#ifndef HOST_WORDS_BIGENDIAN
3103        elt = !elt;
3104#endif
3105        if (elt) {
3106            tcg_out_vsldoi(s, out, out, TCG_VEC_TMP1, 8);
3107        } else {
3108            tcg_out_vsldoi(s, out, TCG_VEC_TMP1, out, 8);
3109        }
3110        break;
3111    default:
3112        g_assert_not_reached();
3113    }
3114    return true;
3115}
3116
3117static void tcg_out_vec_op(TCGContext *s, TCGOpcode opc,
3118                           unsigned vecl, unsigned vece,
3119                           const TCGArg args[TCG_MAX_OP_ARGS],
3120                           const int const_args[TCG_MAX_OP_ARGS])
3121{
3122    static const uint32_t
3123        add_op[4] = { VADDUBM, VADDUHM, VADDUWM, VADDUDM },
3124        sub_op[4] = { VSUBUBM, VSUBUHM, VSUBUWM, VSUBUDM },
3125        mul_op[4] = { 0, 0, VMULUWM, VMULLD },
3126        neg_op[4] = { 0, 0, VNEGW, VNEGD },
3127        eq_op[4]  = { VCMPEQUB, VCMPEQUH, VCMPEQUW, VCMPEQUD },
3128        ne_op[4]  = { VCMPNEB, VCMPNEH, VCMPNEW, 0 },
3129        gts_op[4] = { VCMPGTSB, VCMPGTSH, VCMPGTSW, VCMPGTSD },
3130        gtu_op[4] = { VCMPGTUB, VCMPGTUH, VCMPGTUW, VCMPGTUD },
3131        ssadd_op[4] = { VADDSBS, VADDSHS, VADDSWS, 0 },
3132        usadd_op[4] = { VADDUBS, VADDUHS, VADDUWS, 0 },
3133        sssub_op[4] = { VSUBSBS, VSUBSHS, VSUBSWS, 0 },
3134        ussub_op[4] = { VSUBUBS, VSUBUHS, VSUBUWS, 0 },
3135        umin_op[4] = { VMINUB, VMINUH, VMINUW, VMINUD },
3136        smin_op[4] = { VMINSB, VMINSH, VMINSW, VMINSD },
3137        umax_op[4] = { VMAXUB, VMAXUH, VMAXUW, VMAXUD },
3138        smax_op[4] = { VMAXSB, VMAXSH, VMAXSW, VMAXSD },
3139        shlv_op[4] = { VSLB, VSLH, VSLW, VSLD },
3140        shrv_op[4] = { VSRB, VSRH, VSRW, VSRD },
3141        sarv_op[4] = { VSRAB, VSRAH, VSRAW, VSRAD },
3142        mrgh_op[4] = { VMRGHB, VMRGHH, VMRGHW, 0 },
3143        mrgl_op[4] = { VMRGLB, VMRGLH, VMRGLW, 0 },
3144        muleu_op[4] = { VMULEUB, VMULEUH, VMULEUW, 0 },
3145        mulou_op[4] = { VMULOUB, VMULOUH, VMULOUW, 0 },
3146        pkum_op[4] = { VPKUHUM, VPKUWUM, 0, 0 },
3147        rotl_op[4] = { VRLB, VRLH, VRLW, VRLD };
3148
3149    TCGType type = vecl + TCG_TYPE_V64;
3150    TCGArg a0 = args[0], a1 = args[1], a2 = args[2];
3151    uint32_t insn;
3152
3153    switch (opc) {
3154    case INDEX_op_ld_vec:
3155        tcg_out_ld(s, type, a0, a1, a2);
3156        return;
3157    case INDEX_op_st_vec:
3158        tcg_out_st(s, type, a0, a1, a2);
3159        return;
3160    case INDEX_op_dupm_vec:
3161        tcg_out_dupm_vec(s, type, vece, a0, a1, a2);
3162        return;
3163
3164    case INDEX_op_add_vec:
3165        insn = add_op[vece];
3166        break;
3167    case INDEX_op_sub_vec:
3168        insn = sub_op[vece];
3169        break;
3170    case INDEX_op_neg_vec:
3171        insn = neg_op[vece];
3172        a2 = a1;
3173        a1 = 0;
3174        break;
3175    case INDEX_op_mul_vec:
3176        insn = mul_op[vece];
3177        break;
3178    case INDEX_op_ssadd_vec:
3179        insn = ssadd_op[vece];
3180        break;
3181    case INDEX_op_sssub_vec:
3182        insn = sssub_op[vece];
3183        break;
3184    case INDEX_op_usadd_vec:
3185        insn = usadd_op[vece];
3186        break;
3187    case INDEX_op_ussub_vec:
3188        insn = ussub_op[vece];
3189        break;
3190    case INDEX_op_smin_vec:
3191        insn = smin_op[vece];
3192        break;
3193    case INDEX_op_umin_vec:
3194        insn = umin_op[vece];
3195        break;
3196    case INDEX_op_smax_vec:
3197        insn = smax_op[vece];
3198        break;
3199    case INDEX_op_umax_vec:
3200        insn = umax_op[vece];
3201        break;
3202    case INDEX_op_shlv_vec:
3203        insn = shlv_op[vece];
3204        break;
3205    case INDEX_op_shrv_vec:
3206        insn = shrv_op[vece];
3207        break;
3208    case INDEX_op_sarv_vec:
3209        insn = sarv_op[vece];
3210        break;
3211    case INDEX_op_and_vec:
3212        insn = VAND;
3213        break;
3214    case INDEX_op_or_vec:
3215        insn = VOR;
3216        break;
3217    case INDEX_op_xor_vec:
3218        insn = VXOR;
3219        break;
3220    case INDEX_op_andc_vec:
3221        insn = VANDC;
3222        break;
3223    case INDEX_op_not_vec:
3224        insn = VNOR;
3225        a2 = a1;
3226        break;
3227    case INDEX_op_orc_vec:
3228        insn = VORC;
3229        break;
3230
3231    case INDEX_op_cmp_vec:
3232        switch (args[3]) {
3233        case TCG_COND_EQ:
3234            insn = eq_op[vece];
3235            break;
3236        case TCG_COND_NE:
3237            insn = ne_op[vece];
3238            break;
3239        case TCG_COND_GT:
3240            insn = gts_op[vece];
3241            break;
3242        case TCG_COND_GTU:
3243            insn = gtu_op[vece];
3244            break;
3245        default:
3246            g_assert_not_reached();
3247        }
3248        break;
3249
3250    case INDEX_op_bitsel_vec:
3251        tcg_out32(s, XXSEL | VRT(a0) | VRC(a1) | VRB(a2) | VRA(args[3]));
3252        return;
3253
3254    case INDEX_op_dup2_vec:
3255        assert(TCG_TARGET_REG_BITS == 32);
3256        /* With inputs a1 = xLxx, a2 = xHxx  */
3257        tcg_out32(s, VMRGHW | VRT(a0) | VRA(a2) | VRB(a1));  /* a0  = xxHL */
3258        tcg_out_vsldoi(s, TCG_VEC_TMP1, a0, a0, 8);          /* tmp = HLxx */
3259        tcg_out_vsldoi(s, a0, a0, TCG_VEC_TMP1, 8);          /* a0  = HLHL */
3260        return;
3261
3262    case INDEX_op_ppc_mrgh_vec:
3263        insn = mrgh_op[vece];
3264        break;
3265    case INDEX_op_ppc_mrgl_vec:
3266        insn = mrgl_op[vece];
3267        break;
3268    case INDEX_op_ppc_muleu_vec:
3269        insn = muleu_op[vece];
3270        break;
3271    case INDEX_op_ppc_mulou_vec:
3272        insn = mulou_op[vece];
3273        break;
3274    case INDEX_op_ppc_pkum_vec:
3275        insn = pkum_op[vece];
3276        break;
3277    case INDEX_op_rotlv_vec:
3278        insn = rotl_op[vece];
3279        break;
3280    case INDEX_op_ppc_msum_vec:
3281        tcg_debug_assert(vece == MO_16);
3282        tcg_out32(s, VMSUMUHM | VRT(a0) | VRA(a1) | VRB(a2) | VRC(args[3]));
3283        return;
3284
3285    case INDEX_op_mov_vec:  /* Always emitted via tcg_out_mov.  */
3286    case INDEX_op_dup_vec:  /* Always emitted via tcg_out_dup_vec.  */
3287    default:
3288        g_assert_not_reached();
3289    }
3290
3291    tcg_debug_assert(insn != 0);
3292    tcg_out32(s, insn | VRT(a0) | VRA(a1) | VRB(a2));
3293}
3294
3295static void expand_vec_shi(TCGType type, unsigned vece, TCGv_vec v0,
3296                           TCGv_vec v1, TCGArg imm, TCGOpcode opci)
3297{
3298    TCGv_vec t1;
3299
3300    if (vece == MO_32) {
3301        /*
3302         * Only 5 bits are significant, and VSPLTISB can represent -16..15.
3303         * So using negative numbers gets us the 4th bit easily.
3304         */
3305        imm = sextract32(imm, 0, 5);
3306    } else {
3307        imm &= (8 << vece) - 1;
3308    }
3309
3310    /* Splat w/bytes for xxspltib when 2.07 allows MO_64. */
3311    t1 = tcg_constant_vec(type, MO_8, imm);
3312    vec_gen_3(opci, type, vece, tcgv_vec_arg(v0),
3313              tcgv_vec_arg(v1), tcgv_vec_arg(t1));
3314}
3315
3316static void expand_vec_cmp(TCGType type, unsigned vece, TCGv_vec v0,
3317                           TCGv_vec v1, TCGv_vec v2, TCGCond cond)
3318{
3319    bool need_swap = false, need_inv = false;
3320
3321    tcg_debug_assert(vece <= MO_32 || have_isa_2_07);
3322
3323    switch (cond) {
3324    case TCG_COND_EQ:
3325    case TCG_COND_GT:
3326    case TCG_COND_GTU:
3327        break;
3328    case TCG_COND_NE:
3329        if (have_isa_3_00 && vece <= MO_32) {
3330            break;
3331        }
3332        /* fall through */
3333    case TCG_COND_LE:
3334    case TCG_COND_LEU:
3335        need_inv = true;
3336        break;
3337    case TCG_COND_LT:
3338    case TCG_COND_LTU:
3339        need_swap = true;
3340        break;
3341    case TCG_COND_GE:
3342    case TCG_COND_GEU:
3343        need_swap = need_inv = true;
3344        break;
3345    default:
3346        g_assert_not_reached();
3347    }
3348
3349    if (need_inv) {
3350        cond = tcg_invert_cond(cond);
3351    }
3352    if (need_swap) {
3353        TCGv_vec t1;
3354        t1 = v1, v1 = v2, v2 = t1;
3355        cond = tcg_swap_cond(cond);
3356    }
3357
3358    vec_gen_4(INDEX_op_cmp_vec, type, vece, tcgv_vec_arg(v0),
3359              tcgv_vec_arg(v1), tcgv_vec_arg(v2), cond);
3360
3361    if (need_inv) {
3362        tcg_gen_not_vec(vece, v0, v0);
3363    }
3364}
3365
3366static void expand_vec_mul(TCGType type, unsigned vece, TCGv_vec v0,
3367                           TCGv_vec v1, TCGv_vec v2)
3368{
3369    TCGv_vec t1 = tcg_temp_new_vec(type);
3370    TCGv_vec t2 = tcg_temp_new_vec(type);
3371    TCGv_vec c0, c16;
3372
3373    switch (vece) {
3374    case MO_8:
3375    case MO_16:
3376        vec_gen_3(INDEX_op_ppc_muleu_vec, type, vece, tcgv_vec_arg(t1),
3377                  tcgv_vec_arg(v1), tcgv_vec_arg(v2));
3378        vec_gen_3(INDEX_op_ppc_mulou_vec, type, vece, tcgv_vec_arg(t2),
3379                  tcgv_vec_arg(v1), tcgv_vec_arg(v2));
3380        vec_gen_3(INDEX_op_ppc_mrgh_vec, type, vece + 1, tcgv_vec_arg(v0),
3381                  tcgv_vec_arg(t1), tcgv_vec_arg(t2));
3382        vec_gen_3(INDEX_op_ppc_mrgl_vec, type, vece + 1, tcgv_vec_arg(t1),
3383                  tcgv_vec_arg(t1), tcgv_vec_arg(t2));
3384        vec_gen_3(INDEX_op_ppc_pkum_vec, type, vece, tcgv_vec_arg(v0),
3385                  tcgv_vec_arg(v0), tcgv_vec_arg(t1));
3386	break;
3387
3388    case MO_32:
3389        tcg_debug_assert(!have_isa_2_07);
3390        /*
3391         * Only 5 bits are significant, and VSPLTISB can represent -16..15.
3392         * So using -16 is a quick way to represent 16.
3393         */
3394        c16 = tcg_constant_vec(type, MO_8, -16);
3395        c0 = tcg_constant_vec(type, MO_8, 0);
3396
3397        vec_gen_3(INDEX_op_rotlv_vec, type, MO_32, tcgv_vec_arg(t1),
3398                  tcgv_vec_arg(v2), tcgv_vec_arg(c16));
3399        vec_gen_3(INDEX_op_ppc_mulou_vec, type, MO_16, tcgv_vec_arg(t2),
3400                  tcgv_vec_arg(v1), tcgv_vec_arg(v2));
3401        vec_gen_4(INDEX_op_ppc_msum_vec, type, MO_16, tcgv_vec_arg(t1),
3402                  tcgv_vec_arg(v1), tcgv_vec_arg(t1), tcgv_vec_arg(c0));
3403        vec_gen_3(INDEX_op_shlv_vec, type, MO_32, tcgv_vec_arg(t1),
3404                  tcgv_vec_arg(t1), tcgv_vec_arg(c16));
3405        tcg_gen_add_vec(MO_32, v0, t1, t2);
3406        break;
3407
3408    default:
3409        g_assert_not_reached();
3410    }
3411    tcg_temp_free_vec(t1);
3412    tcg_temp_free_vec(t2);
3413}
3414
3415void tcg_expand_vec_op(TCGOpcode opc, TCGType type, unsigned vece,
3416                       TCGArg a0, ...)
3417{
3418    va_list va;
3419    TCGv_vec v0, v1, v2, t0;
3420    TCGArg a2;
3421
3422    va_start(va, a0);
3423    v0 = temp_tcgv_vec(arg_temp(a0));
3424    v1 = temp_tcgv_vec(arg_temp(va_arg(va, TCGArg)));
3425    a2 = va_arg(va, TCGArg);
3426
3427    switch (opc) {
3428    case INDEX_op_shli_vec:
3429        expand_vec_shi(type, vece, v0, v1, a2, INDEX_op_shlv_vec);
3430        break;
3431    case INDEX_op_shri_vec:
3432        expand_vec_shi(type, vece, v0, v1, a2, INDEX_op_shrv_vec);
3433        break;
3434    case INDEX_op_sari_vec:
3435        expand_vec_shi(type, vece, v0, v1, a2, INDEX_op_sarv_vec);
3436        break;
3437    case INDEX_op_rotli_vec:
3438        expand_vec_shi(type, vece, v0, v1, a2, INDEX_op_rotlv_vec);
3439        break;
3440    case INDEX_op_cmp_vec:
3441        v2 = temp_tcgv_vec(arg_temp(a2));
3442        expand_vec_cmp(type, vece, v0, v1, v2, va_arg(va, TCGArg));
3443        break;
3444    case INDEX_op_mul_vec:
3445        v2 = temp_tcgv_vec(arg_temp(a2));
3446        expand_vec_mul(type, vece, v0, v1, v2);
3447        break;
3448    case INDEX_op_rotlv_vec:
3449        v2 = temp_tcgv_vec(arg_temp(a2));
3450        t0 = tcg_temp_new_vec(type);
3451        tcg_gen_neg_vec(vece, t0, v2);
3452        tcg_gen_rotlv_vec(vece, v0, v1, t0);
3453        tcg_temp_free_vec(t0);
3454        break;
3455    default:
3456        g_assert_not_reached();
3457    }
3458    va_end(va);
3459}
3460
3461static TCGConstraintSetIndex tcg_target_op_def(TCGOpcode op)
3462{
3463    switch (op) {
3464    case INDEX_op_goto_ptr:
3465        return C_O0_I1(r);
3466
3467    case INDEX_op_ld8u_i32:
3468    case INDEX_op_ld8s_i32:
3469    case INDEX_op_ld16u_i32:
3470    case INDEX_op_ld16s_i32:
3471    case INDEX_op_ld_i32:
3472    case INDEX_op_ctpop_i32:
3473    case INDEX_op_neg_i32:
3474    case INDEX_op_not_i32:
3475    case INDEX_op_ext8s_i32:
3476    case INDEX_op_ext16s_i32:
3477    case INDEX_op_bswap16_i32:
3478    case INDEX_op_bswap32_i32:
3479    case INDEX_op_extract_i32:
3480    case INDEX_op_ld8u_i64:
3481    case INDEX_op_ld8s_i64:
3482    case INDEX_op_ld16u_i64:
3483    case INDEX_op_ld16s_i64:
3484    case INDEX_op_ld32u_i64:
3485    case INDEX_op_ld32s_i64:
3486    case INDEX_op_ld_i64:
3487    case INDEX_op_ctpop_i64:
3488    case INDEX_op_neg_i64:
3489    case INDEX_op_not_i64:
3490    case INDEX_op_ext8s_i64:
3491    case INDEX_op_ext16s_i64:
3492    case INDEX_op_ext32s_i64:
3493    case INDEX_op_ext_i32_i64:
3494    case INDEX_op_extu_i32_i64:
3495    case INDEX_op_bswap16_i64:
3496    case INDEX_op_bswap32_i64:
3497    case INDEX_op_bswap64_i64:
3498    case INDEX_op_extract_i64:
3499        return C_O1_I1(r, r);
3500
3501    case INDEX_op_st8_i32:
3502    case INDEX_op_st16_i32:
3503    case INDEX_op_st_i32:
3504    case INDEX_op_st8_i64:
3505    case INDEX_op_st16_i64:
3506    case INDEX_op_st32_i64:
3507    case INDEX_op_st_i64:
3508        return C_O0_I2(r, r);
3509
3510    case INDEX_op_add_i32:
3511    case INDEX_op_and_i32:
3512    case INDEX_op_or_i32:
3513    case INDEX_op_xor_i32:
3514    case INDEX_op_andc_i32:
3515    case INDEX_op_orc_i32:
3516    case INDEX_op_eqv_i32:
3517    case INDEX_op_shl_i32:
3518    case INDEX_op_shr_i32:
3519    case INDEX_op_sar_i32:
3520    case INDEX_op_rotl_i32:
3521    case INDEX_op_rotr_i32:
3522    case INDEX_op_setcond_i32:
3523    case INDEX_op_and_i64:
3524    case INDEX_op_andc_i64:
3525    case INDEX_op_shl_i64:
3526    case INDEX_op_shr_i64:
3527    case INDEX_op_sar_i64:
3528    case INDEX_op_rotl_i64:
3529    case INDEX_op_rotr_i64:
3530    case INDEX_op_setcond_i64:
3531        return C_O1_I2(r, r, ri);
3532
3533    case INDEX_op_mul_i32:
3534    case INDEX_op_mul_i64:
3535        return C_O1_I2(r, r, rI);
3536
3537    case INDEX_op_div_i32:
3538    case INDEX_op_divu_i32:
3539    case INDEX_op_nand_i32:
3540    case INDEX_op_nor_i32:
3541    case INDEX_op_muluh_i32:
3542    case INDEX_op_mulsh_i32:
3543    case INDEX_op_orc_i64:
3544    case INDEX_op_eqv_i64:
3545    case INDEX_op_nand_i64:
3546    case INDEX_op_nor_i64:
3547    case INDEX_op_div_i64:
3548    case INDEX_op_divu_i64:
3549    case INDEX_op_mulsh_i64:
3550    case INDEX_op_muluh_i64:
3551        return C_O1_I2(r, r, r);
3552
3553    case INDEX_op_sub_i32:
3554        return C_O1_I2(r, rI, ri);
3555    case INDEX_op_add_i64:
3556        return C_O1_I2(r, r, rT);
3557    case INDEX_op_or_i64:
3558    case INDEX_op_xor_i64:
3559        return C_O1_I2(r, r, rU);
3560    case INDEX_op_sub_i64:
3561        return C_O1_I2(r, rI, rT);
3562    case INDEX_op_clz_i32:
3563    case INDEX_op_ctz_i32:
3564    case INDEX_op_clz_i64:
3565    case INDEX_op_ctz_i64:
3566        return C_O1_I2(r, r, rZW);
3567
3568    case INDEX_op_brcond_i32:
3569    case INDEX_op_brcond_i64:
3570        return C_O0_I2(r, ri);
3571
3572    case INDEX_op_movcond_i32:
3573    case INDEX_op_movcond_i64:
3574        return C_O1_I4(r, r, ri, rZ, rZ);
3575    case INDEX_op_deposit_i32:
3576    case INDEX_op_deposit_i64:
3577        return C_O1_I2(r, 0, rZ);
3578    case INDEX_op_brcond2_i32:
3579        return C_O0_I4(r, r, ri, ri);
3580    case INDEX_op_setcond2_i32:
3581        return C_O1_I4(r, r, r, ri, ri);
3582    case INDEX_op_add2_i64:
3583    case INDEX_op_add2_i32:
3584        return C_O2_I4(r, r, r, r, rI, rZM);
3585    case INDEX_op_sub2_i64:
3586    case INDEX_op_sub2_i32:
3587        return C_O2_I4(r, r, rI, rZM, r, r);
3588
3589    case INDEX_op_qemu_ld_i32:
3590        return (TCG_TARGET_REG_BITS == 64 || TARGET_LONG_BITS == 32
3591                ? C_O1_I1(r, L)
3592                : C_O1_I2(r, L, L));
3593
3594    case INDEX_op_qemu_st_i32:
3595        return (TCG_TARGET_REG_BITS == 64 || TARGET_LONG_BITS == 32
3596                ? C_O0_I2(S, S)
3597                : C_O0_I3(S, S, S));
3598
3599    case INDEX_op_qemu_ld_i64:
3600        return (TCG_TARGET_REG_BITS == 64 ? C_O1_I1(r, L)
3601                : TARGET_LONG_BITS == 32 ? C_O2_I1(L, L, L)
3602                : C_O2_I2(L, L, L, L));
3603
3604    case INDEX_op_qemu_st_i64:
3605        return (TCG_TARGET_REG_BITS == 64 ? C_O0_I2(S, S)
3606                : TARGET_LONG_BITS == 32 ? C_O0_I3(S, S, S)
3607                : C_O0_I4(S, S, S, S));
3608
3609    case INDEX_op_add_vec:
3610    case INDEX_op_sub_vec:
3611    case INDEX_op_mul_vec:
3612    case INDEX_op_and_vec:
3613    case INDEX_op_or_vec:
3614    case INDEX_op_xor_vec:
3615    case INDEX_op_andc_vec:
3616    case INDEX_op_orc_vec:
3617    case INDEX_op_cmp_vec:
3618    case INDEX_op_ssadd_vec:
3619    case INDEX_op_sssub_vec:
3620    case INDEX_op_usadd_vec:
3621    case INDEX_op_ussub_vec:
3622    case INDEX_op_smax_vec:
3623    case INDEX_op_smin_vec:
3624    case INDEX_op_umax_vec:
3625    case INDEX_op_umin_vec:
3626    case INDEX_op_shlv_vec:
3627    case INDEX_op_shrv_vec:
3628    case INDEX_op_sarv_vec:
3629    case INDEX_op_rotlv_vec:
3630    case INDEX_op_rotrv_vec:
3631    case INDEX_op_ppc_mrgh_vec:
3632    case INDEX_op_ppc_mrgl_vec:
3633    case INDEX_op_ppc_muleu_vec:
3634    case INDEX_op_ppc_mulou_vec:
3635    case INDEX_op_ppc_pkum_vec:
3636    case INDEX_op_dup2_vec:
3637        return C_O1_I2(v, v, v);
3638
3639    case INDEX_op_not_vec:
3640    case INDEX_op_neg_vec:
3641        return C_O1_I1(v, v);
3642
3643    case INDEX_op_dup_vec:
3644        return have_isa_3_00 ? C_O1_I1(v, vr) : C_O1_I1(v, v);
3645
3646    case INDEX_op_ld_vec:
3647    case INDEX_op_dupm_vec:
3648        return C_O1_I1(v, r);
3649
3650    case INDEX_op_st_vec:
3651        return C_O0_I2(v, r);
3652
3653    case INDEX_op_bitsel_vec:
3654    case INDEX_op_ppc_msum_vec:
3655        return C_O1_I3(v, v, v, v);
3656
3657    default:
3658        g_assert_not_reached();
3659    }
3660}
3661
3662static void tcg_target_init(TCGContext *s)
3663{
3664    unsigned long hwcap = qemu_getauxval(AT_HWCAP);
3665    unsigned long hwcap2 = qemu_getauxval(AT_HWCAP2);
3666
3667    have_isa = tcg_isa_base;
3668    if (hwcap & PPC_FEATURE_ARCH_2_06) {
3669        have_isa = tcg_isa_2_06;
3670    }
3671#ifdef PPC_FEATURE2_ARCH_2_07
3672    if (hwcap2 & PPC_FEATURE2_ARCH_2_07) {
3673        have_isa = tcg_isa_2_07;
3674    }
3675#endif
3676#ifdef PPC_FEATURE2_ARCH_3_00
3677    if (hwcap2 & PPC_FEATURE2_ARCH_3_00) {
3678        have_isa = tcg_isa_3_00;
3679    }
3680#endif
3681#ifdef PPC_FEATURE2_ARCH_3_10
3682    if (hwcap2 & PPC_FEATURE2_ARCH_3_10) {
3683        have_isa = tcg_isa_3_10;
3684    }
3685#endif
3686
3687#ifdef PPC_FEATURE2_HAS_ISEL
3688    /* Prefer explicit instruction from the kernel. */
3689    have_isel = (hwcap2 & PPC_FEATURE2_HAS_ISEL) != 0;
3690#else
3691    /* Fall back to knowing Power7 (2.06) has ISEL. */
3692    have_isel = have_isa_2_06;
3693#endif
3694
3695    if (hwcap & PPC_FEATURE_HAS_ALTIVEC) {
3696        have_altivec = true;
3697        /* We only care about the portion of VSX that overlaps Altivec. */
3698        if (hwcap & PPC_FEATURE_HAS_VSX) {
3699            have_vsx = true;
3700        }
3701    }
3702
3703    tcg_target_available_regs[TCG_TYPE_I32] = 0xffffffff;
3704    tcg_target_available_regs[TCG_TYPE_I64] = 0xffffffff;
3705    if (have_altivec) {
3706        tcg_target_available_regs[TCG_TYPE_V64] = 0xffffffff00000000ull;
3707        tcg_target_available_regs[TCG_TYPE_V128] = 0xffffffff00000000ull;
3708    }
3709
3710    tcg_target_call_clobber_regs = 0;
3711    tcg_regset_set_reg(tcg_target_call_clobber_regs, TCG_REG_R0);
3712    tcg_regset_set_reg(tcg_target_call_clobber_regs, TCG_REG_R2);
3713    tcg_regset_set_reg(tcg_target_call_clobber_regs, TCG_REG_R3);
3714    tcg_regset_set_reg(tcg_target_call_clobber_regs, TCG_REG_R4);
3715    tcg_regset_set_reg(tcg_target_call_clobber_regs, TCG_REG_R5);
3716    tcg_regset_set_reg(tcg_target_call_clobber_regs, TCG_REG_R6);
3717    tcg_regset_set_reg(tcg_target_call_clobber_regs, TCG_REG_R7);
3718    tcg_regset_set_reg(tcg_target_call_clobber_regs, TCG_REG_R8);
3719    tcg_regset_set_reg(tcg_target_call_clobber_regs, TCG_REG_R9);
3720    tcg_regset_set_reg(tcg_target_call_clobber_regs, TCG_REG_R10);
3721    tcg_regset_set_reg(tcg_target_call_clobber_regs, TCG_REG_R11);
3722    tcg_regset_set_reg(tcg_target_call_clobber_regs, TCG_REG_R12);
3723
3724    tcg_regset_set_reg(tcg_target_call_clobber_regs, TCG_REG_V0);
3725    tcg_regset_set_reg(tcg_target_call_clobber_regs, TCG_REG_V1);
3726    tcg_regset_set_reg(tcg_target_call_clobber_regs, TCG_REG_V2);
3727    tcg_regset_set_reg(tcg_target_call_clobber_regs, TCG_REG_V3);
3728    tcg_regset_set_reg(tcg_target_call_clobber_regs, TCG_REG_V4);
3729    tcg_regset_set_reg(tcg_target_call_clobber_regs, TCG_REG_V5);
3730    tcg_regset_set_reg(tcg_target_call_clobber_regs, TCG_REG_V6);
3731    tcg_regset_set_reg(tcg_target_call_clobber_regs, TCG_REG_V7);
3732    tcg_regset_set_reg(tcg_target_call_clobber_regs, TCG_REG_V8);
3733    tcg_regset_set_reg(tcg_target_call_clobber_regs, TCG_REG_V9);
3734    tcg_regset_set_reg(tcg_target_call_clobber_regs, TCG_REG_V10);
3735    tcg_regset_set_reg(tcg_target_call_clobber_regs, TCG_REG_V11);
3736    tcg_regset_set_reg(tcg_target_call_clobber_regs, TCG_REG_V12);
3737    tcg_regset_set_reg(tcg_target_call_clobber_regs, TCG_REG_V13);
3738    tcg_regset_set_reg(tcg_target_call_clobber_regs, TCG_REG_V14);
3739    tcg_regset_set_reg(tcg_target_call_clobber_regs, TCG_REG_V15);
3740    tcg_regset_set_reg(tcg_target_call_clobber_regs, TCG_REG_V16);
3741    tcg_regset_set_reg(tcg_target_call_clobber_regs, TCG_REG_V17);
3742    tcg_regset_set_reg(tcg_target_call_clobber_regs, TCG_REG_V18);
3743    tcg_regset_set_reg(tcg_target_call_clobber_regs, TCG_REG_V19);
3744
3745    s->reserved_regs = 0;
3746    tcg_regset_set_reg(s->reserved_regs, TCG_REG_R0); /* tcg temp */
3747    tcg_regset_set_reg(s->reserved_regs, TCG_REG_R1); /* stack pointer */
3748#if defined(_CALL_SYSV)
3749    tcg_regset_set_reg(s->reserved_regs, TCG_REG_R2); /* toc pointer */
3750#endif
3751#if defined(_CALL_SYSV) || TCG_TARGET_REG_BITS == 64
3752    tcg_regset_set_reg(s->reserved_regs, TCG_REG_R13); /* thread pointer */
3753#endif
3754    tcg_regset_set_reg(s->reserved_regs, TCG_REG_TMP1); /* mem temp */
3755    tcg_regset_set_reg(s->reserved_regs, TCG_VEC_TMP1);
3756    tcg_regset_set_reg(s->reserved_regs, TCG_VEC_TMP2);
3757    if (USE_REG_TB) {
3758        tcg_regset_set_reg(s->reserved_regs, TCG_REG_TB);  /* tb->tc_ptr */
3759    }
3760}
3761
3762#ifdef __ELF__
3763typedef struct {
3764    DebugFrameCIE cie;
3765    DebugFrameFDEHeader fde;
3766    uint8_t fde_def_cfa[4];
3767    uint8_t fde_reg_ofs[ARRAY_SIZE(tcg_target_callee_save_regs) * 2 + 3];
3768} DebugFrame;
3769
3770/* We're expecting a 2 byte uleb128 encoded value.  */
3771QEMU_BUILD_BUG_ON(FRAME_SIZE >= (1 << 14));
3772
3773#if TCG_TARGET_REG_BITS == 64
3774# define ELF_HOST_MACHINE EM_PPC64
3775#else
3776# define ELF_HOST_MACHINE EM_PPC
3777#endif
3778
3779static DebugFrame debug_frame = {
3780    .cie.len = sizeof(DebugFrameCIE)-4, /* length after .len member */
3781    .cie.id = -1,
3782    .cie.version = 1,
3783    .cie.code_align = 1,
3784    .cie.data_align = (-SZR & 0x7f),         /* sleb128 -SZR */
3785    .cie.return_column = 65,
3786
3787    /* Total FDE size does not include the "len" member.  */
3788    .fde.len = sizeof(DebugFrame) - offsetof(DebugFrame, fde.cie_offset),
3789
3790    .fde_def_cfa = {
3791        12, TCG_REG_R1,                 /* DW_CFA_def_cfa r1, ... */
3792        (FRAME_SIZE & 0x7f) | 0x80,     /* ... uleb128 FRAME_SIZE */
3793        (FRAME_SIZE >> 7)
3794    },
3795    .fde_reg_ofs = {
3796        /* DW_CFA_offset_extended_sf, lr, LR_OFFSET */
3797        0x11, 65, (LR_OFFSET / -SZR) & 0x7f,
3798    }
3799};
3800
3801void tcg_register_jit(const void *buf, size_t buf_size)
3802{
3803    uint8_t *p = &debug_frame.fde_reg_ofs[3];
3804    int i;
3805
3806    for (i = 0; i < ARRAY_SIZE(tcg_target_callee_save_regs); ++i, p += 2) {
3807        p[0] = 0x80 + tcg_target_callee_save_regs[i];
3808        p[1] = (FRAME_SIZE - (REG_SAVE_BOT + i * SZR)) / SZR;
3809    }
3810
3811    debug_frame.fde.func_start = (uintptr_t)buf;
3812    debug_frame.fde.func_len = buf_size;
3813
3814    tcg_register_jit_int(buf, buf_size, &debug_frame, sizeof(debug_frame));
3815}
3816#endif /* __ELF__ */
3817