2 * Tiny Code Generator for QEMU
4 * Copyright (c) 2008 Fabrice Bellard
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
26 #include "../tcg-pool.c.inc"
29 * Standardize on the _CALL_FOO symbols used by GCC:
30 * Apple XCode does not define _CALL_DARWIN.
31 * Clang defines _CALL_ELF (64-bit) but not _CALL_SYSV (32-bit).
33 #if !defined(_CALL_SYSV) && \
34 !defined(_CALL_DARWIN) && \
35 !defined(_CALL_AIX) && \
37 # if defined(__APPLE__)
39 # elif defined(__ELF__) && TCG_TARGET_REG_BITS == 32
47 # define TCG_TARGET_CALL_ALIGN_ARGS 1
50 /* For some memory operations, we need a scratch that isn't R0. For the AIX
51 calling convention, we can re-use the TOC register since we'll be reloading
52 it at every call. Otherwise R12 will do nicely as neither a call-saved
53 register nor a parameter register. */
55 # define TCG_REG_TMP1 TCG_REG_R2
57 # define TCG_REG_TMP1 TCG_REG_R12
60 #define TCG_VEC_TMP1 TCG_REG_V0
61 #define TCG_VEC_TMP2 TCG_REG_V1
63 #define TCG_REG_TB TCG_REG_R31
64 #define USE_REG_TB (TCG_TARGET_REG_BITS == 64)
66 /* Shorthand for size of a pointer. Avoid promotion to unsigned. */
67 #define SZP ((int)sizeof(void *))
69 /* Shorthand for size of a register. */
70 #define SZR (TCG_TARGET_REG_BITS / 8)
72 #define TCG_CT_CONST_S16 0x100
73 #define TCG_CT_CONST_U16 0x200
74 #define TCG_CT_CONST_S32 0x400
75 #define TCG_CT_CONST_U32 0x800
76 #define TCG_CT_CONST_ZERO 0x1000
77 #define TCG_CT_CONST_MONE 0x2000
78 #define TCG_CT_CONST_WSZ 0x4000
80 #define ALL_GENERAL_REGS 0xffffffffu
81 #define ALL_VECTOR_REGS 0xffffffff00000000ull
84 #define ALL_QLOAD_REGS \
86 ~((1 << TCG_REG_R3) | (1 << TCG_REG_R4) | (1 << TCG_REG_R5)))
87 #define ALL_QSTORE_REGS \
88 (ALL_GENERAL_REGS & ~((1 << TCG_REG_R3) | (1 << TCG_REG_R4) | \
89 (1 << TCG_REG_R5) | (1 << TCG_REG_R6)))
91 #define ALL_QLOAD_REGS (ALL_GENERAL_REGS & ~(1 << TCG_REG_R3))
92 #define ALL_QSTORE_REGS ALL_QLOAD_REGS
96 static bool have_isel;
100 #ifndef CONFIG_SOFTMMU
101 #define TCG_GUEST_BASE_REG 30
104 #ifdef CONFIG_DEBUG_TCG
105 static const char tcg_target_reg_names[TCG_TARGET_NB_REGS][4] = {
106 "r0", "r1", "r2", "r3", "r4", "r5", "r6", "r7",
107 "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15",
108 "r16", "r17", "r18", "r19", "r20", "r21", "r22", "r23",
109 "r24", "r25", "r26", "r27", "r28", "r29", "r30", "r31",
110 "v0", "v1", "v2", "v3", "v4", "v5", "v6", "v7",
111 "v8", "v9", "v10", "v11", "v12", "v13", "v14", "v15",
112 "v16", "v17", "v18", "v19", "v20", "v21", "v22", "v23",
113 "v24", "v25", "v26", "v27", "v28", "v29", "v30", "v31",
117 static const int tcg_target_reg_alloc_order[] = {
118 TCG_REG_R14, /* call saved registers */
136 TCG_REG_R12, /* call clobbered, non-arguments */
140 TCG_REG_R10, /* call clobbered, arguments */
149 /* V0 and V1 reserved as temporaries; V20 - V31 are call-saved */
150 TCG_REG_V2, /* call clobbered, vectors */
170 static const int tcg_target_call_iarg_regs[] = {
181 static const int tcg_target_call_oarg_regs[] = {
186 static const int tcg_target_callee_save_regs[] = {
203 TCG_REG_R27, /* currently used for the global env */
210 static inline bool in_range_b(tcg_target_long target)
212 return target == sextract64(target, 0, 26);
215 static uint32_t reloc_pc24_val(const tcg_insn_unit *pc,
216 const tcg_insn_unit *target)
218 ptrdiff_t disp = tcg_ptr_byte_diff(target, pc);
219 tcg_debug_assert(in_range_b(disp));
220 return disp & 0x3fffffc;
223 static bool reloc_pc24(tcg_insn_unit *src_rw, const tcg_insn_unit *target)
225 const tcg_insn_unit *src_rx = tcg_splitwx_to_rx(src_rw);
226 ptrdiff_t disp = tcg_ptr_byte_diff(target, src_rx);
228 if (in_range_b(disp)) {
229 *src_rw = (*src_rw & ~0x3fffffc) | (disp & 0x3fffffc);
235 static uint16_t reloc_pc14_val(const tcg_insn_unit *pc,
236 const tcg_insn_unit *target)
238 ptrdiff_t disp = tcg_ptr_byte_diff(target, pc);
239 tcg_debug_assert(disp == (int16_t) disp);
240 return disp & 0xfffc;
243 static bool reloc_pc14(tcg_insn_unit *src_rw, const tcg_insn_unit *target)
245 const tcg_insn_unit *src_rx = tcg_splitwx_to_rx(src_rw);
246 ptrdiff_t disp = tcg_ptr_byte_diff(target, src_rx);
248 if (disp == (int16_t) disp) {
249 *src_rw = (*src_rw & ~0xfffc) | (disp & 0xfffc);
255 /* test if a constant matches the constraint */
256 static bool tcg_target_const_match(int64_t val, TCGType type, int ct)
258 if (ct & TCG_CT_CONST) {
262 /* The only 32-bit constraint we use aside from
263 TCG_CT_CONST is TCG_CT_CONST_S16. */
264 if (type == TCG_TYPE_I32) {
268 if ((ct & TCG_CT_CONST_S16) && val == (int16_t)val) {
270 } else if ((ct & TCG_CT_CONST_U16) && val == (uint16_t)val) {
272 } else if ((ct & TCG_CT_CONST_S32) && val == (int32_t)val) {
274 } else if ((ct & TCG_CT_CONST_U32) && val == (uint32_t)val) {
276 } else if ((ct & TCG_CT_CONST_ZERO) && val == 0) {
278 } else if ((ct & TCG_CT_CONST_MONE) && val == -1) {
280 } else if ((ct & TCG_CT_CONST_WSZ)
281 && val == (type == TCG_TYPE_I32 ? 32 : 64)) {
287 #define OPCD(opc) ((opc)<<26)
288 #define XO19(opc) (OPCD(19)|((opc)<<1))
289 #define MD30(opc) (OPCD(30)|((opc)<<2))
290 #define MDS30(opc) (OPCD(30)|((opc)<<1))
291 #define XO31(opc) (OPCD(31)|((opc)<<1))
292 #define XO58(opc) (OPCD(58)|(opc))
293 #define XO62(opc) (OPCD(62)|(opc))
294 #define VX4(opc) (OPCD(4)|(opc))
298 #define LBZ OPCD( 34)
299 #define LHZ OPCD( 40)
300 #define LHA OPCD( 42)
301 #define LWZ OPCD( 32)
302 #define LWZUX XO31( 55)
303 #define STB OPCD( 38)
304 #define STH OPCD( 44)
305 #define STW OPCD( 36)
308 #define STDU XO62( 1)
309 #define STDX XO31(149)
312 #define LDX XO31( 21)
314 #define LDUX XO31( 53)
316 #define LWAX XO31(341)
318 #define ADDIC OPCD( 12)
319 #define ADDI OPCD( 14)
320 #define ADDIS OPCD( 15)
321 #define ORI OPCD( 24)
322 #define ORIS OPCD( 25)
323 #define XORI OPCD( 26)
324 #define XORIS OPCD( 27)
325 #define ANDI OPCD( 28)
326 #define ANDIS OPCD( 29)
327 #define MULLI OPCD( 7)
328 #define CMPLI OPCD( 10)
329 #define CMPI OPCD( 11)
330 #define SUBFIC OPCD( 8)
332 #define LWZU OPCD( 33)
333 #define STWU OPCD( 37)
335 #define RLWIMI OPCD( 20)
336 #define RLWINM OPCD( 21)
337 #define RLWNM OPCD( 23)
339 #define RLDICL MD30( 0)
340 #define RLDICR MD30( 1)
341 #define RLDIMI MD30( 3)
342 #define RLDCL MDS30( 8)
344 #define BCLR XO19( 16)
345 #define BCCTR XO19(528)
346 #define CRAND XO19(257)
347 #define CRANDC XO19(129)
348 #define CRNAND XO19(225)
349 #define CROR XO19(449)
350 #define CRNOR XO19( 33)
352 #define EXTSB XO31(954)
353 #define EXTSH XO31(922)
354 #define EXTSW XO31(986)
355 #define ADD XO31(266)
356 #define ADDE XO31(138)
357 #define ADDME XO31(234)
358 #define ADDZE XO31(202)
359 #define ADDC XO31( 10)
360 #define AND XO31( 28)
361 #define SUBF XO31( 40)
362 #define SUBFC XO31( 8)
363 #define SUBFE XO31(136)
364 #define SUBFME XO31(232)
365 #define SUBFZE XO31(200)
367 #define XOR XO31(316)
368 #define MULLW XO31(235)
369 #define MULHW XO31( 75)
370 #define MULHWU XO31( 11)
371 #define DIVW XO31(491)
372 #define DIVWU XO31(459)
374 #define CMPL XO31( 32)
375 #define LHBRX XO31(790)
376 #define LWBRX XO31(534)
377 #define LDBRX XO31(532)
378 #define STHBRX XO31(918)
379 #define STWBRX XO31(662)
380 #define STDBRX XO31(660)
381 #define MFSPR XO31(339)
382 #define MTSPR XO31(467)
383 #define SRAWI XO31(824)
384 #define NEG XO31(104)
385 #define MFCR XO31( 19)
386 #define MFOCRF (MFCR | (1u << 20))
387 #define NOR XO31(124)
388 #define CNTLZW XO31( 26)
389 #define CNTLZD XO31( 58)
390 #define CNTTZW XO31(538)
391 #define CNTTZD XO31(570)
392 #define CNTPOPW XO31(378)
393 #define CNTPOPD XO31(506)
394 #define ANDC XO31( 60)
395 #define ORC XO31(412)
396 #define EQV XO31(284)
397 #define NAND XO31(476)
398 #define ISEL XO31( 15)
400 #define MULLD XO31(233)
401 #define MULHD XO31( 73)
402 #define MULHDU XO31( 9)
403 #define DIVD XO31(489)
404 #define DIVDU XO31(457)
406 #define LBZX XO31( 87)
407 #define LHZX XO31(279)
408 #define LHAX XO31(343)
409 #define LWZX XO31( 23)
410 #define STBX XO31(215)
411 #define STHX XO31(407)
412 #define STWX XO31(151)
414 #define EIEIO XO31(854)
415 #define HWSYNC XO31(598)
416 #define LWSYNC (HWSYNC | (1u << 21))
418 #define SPR(a, b) ((((a)<<5)|(b))<<11)
420 #define CTR SPR(9, 0)
422 #define SLW XO31( 24)
423 #define SRW XO31(536)
424 #define SRAW XO31(792)
426 #define SLD XO31( 27)
427 #define SRD XO31(539)
428 #define SRAD XO31(794)
429 #define SRADI XO31(413<<1)
431 #define BRH XO31(219)
432 #define BRW XO31(155)
433 #define BRD XO31(187)
436 #define TRAP (TW | TO(31))
438 #define NOP ORI /* ori 0,0,0 */
440 #define LVX XO31(103)
441 #define LVEBX XO31(7)
442 #define LVEHX XO31(39)
443 #define LVEWX XO31(71)
444 #define LXSDX (XO31(588) | 1) /* v2.06, force tx=1 */
445 #define LXVDSX (XO31(332) | 1) /* v2.06, force tx=1 */
446 #define LXSIWZX (XO31(12) | 1) /* v2.07, force tx=1 */
447 #define LXV (OPCD(61) | 8 | 1) /* v3.00, force tx=1 */
448 #define LXSD (OPCD(57) | 2) /* v3.00 */
449 #define LXVWSX (XO31(364) | 1) /* v3.00, force tx=1 */
451 #define STVX XO31(231)
452 #define STVEWX XO31(199)
453 #define STXSDX (XO31(716) | 1) /* v2.06, force sx=1 */
454 #define STXSIWX (XO31(140) | 1) /* v2.07, force sx=1 */
455 #define STXV (OPCD(61) | 8 | 5) /* v3.00, force sx=1 */
456 #define STXSD (OPCD(61) | 2) /* v3.00 */
458 #define VADDSBS VX4(768)
459 #define VADDUBS VX4(512)
460 #define VADDUBM VX4(0)
461 #define VADDSHS VX4(832)
462 #define VADDUHS VX4(576)
463 #define VADDUHM VX4(64)
464 #define VADDSWS VX4(896)
465 #define VADDUWS VX4(640)
466 #define VADDUWM VX4(128)
467 #define VADDUDM VX4(192) /* v2.07 */
469 #define VSUBSBS VX4(1792)
470 #define VSUBUBS VX4(1536)
471 #define VSUBUBM VX4(1024)
472 #define VSUBSHS VX4(1856)
473 #define VSUBUHS VX4(1600)
474 #define VSUBUHM VX4(1088)
475 #define VSUBSWS VX4(1920)
476 #define VSUBUWS VX4(1664)
477 #define VSUBUWM VX4(1152)
478 #define VSUBUDM VX4(1216) /* v2.07 */
480 #define VNEGW (VX4(1538) | (6 << 16)) /* v3.00 */
481 #define VNEGD (VX4(1538) | (7 << 16)) /* v3.00 */
483 #define VMAXSB VX4(258)
484 #define VMAXSH VX4(322)
485 #define VMAXSW VX4(386)
486 #define VMAXSD VX4(450) /* v2.07 */
487 #define VMAXUB VX4(2)
488 #define VMAXUH VX4(66)
489 #define VMAXUW VX4(130)
490 #define VMAXUD VX4(194) /* v2.07 */
491 #define VMINSB VX4(770)
492 #define VMINSH VX4(834)
493 #define VMINSW VX4(898)
494 #define VMINSD VX4(962) /* v2.07 */
495 #define VMINUB VX4(514)
496 #define VMINUH VX4(578)
497 #define VMINUW VX4(642)
498 #define VMINUD VX4(706) /* v2.07 */
500 #define VCMPEQUB VX4(6)
501 #define VCMPEQUH VX4(70)
502 #define VCMPEQUW VX4(134)
503 #define VCMPEQUD VX4(199) /* v2.07 */
504 #define VCMPGTSB VX4(774)
505 #define VCMPGTSH VX4(838)
506 #define VCMPGTSW VX4(902)
507 #define VCMPGTSD VX4(967) /* v2.07 */
508 #define VCMPGTUB VX4(518)
509 #define VCMPGTUH VX4(582)
510 #define VCMPGTUW VX4(646)
511 #define VCMPGTUD VX4(711) /* v2.07 */
512 #define VCMPNEB VX4(7) /* v3.00 */
513 #define VCMPNEH VX4(71) /* v3.00 */
514 #define VCMPNEW VX4(135) /* v3.00 */
516 #define VSLB VX4(260)
517 #define VSLH VX4(324)
518 #define VSLW VX4(388)
519 #define VSLD VX4(1476) /* v2.07 */
520 #define VSRB VX4(516)
521 #define VSRH VX4(580)
522 #define VSRW VX4(644)
523 #define VSRD VX4(1732) /* v2.07 */
524 #define VSRAB VX4(772)
525 #define VSRAH VX4(836)
526 #define VSRAW VX4(900)
527 #define VSRAD VX4(964) /* v2.07 */
530 #define VRLW VX4(132)
531 #define VRLD VX4(196) /* v2.07 */
533 #define VMULEUB VX4(520)
534 #define VMULEUH VX4(584)
535 #define VMULEUW VX4(648) /* v2.07 */
536 #define VMULOUB VX4(8)
537 #define VMULOUH VX4(72)
538 #define VMULOUW VX4(136) /* v2.07 */
539 #define VMULUWM VX4(137) /* v2.07 */
540 #define VMULLD VX4(457) /* v3.10 */
541 #define VMSUMUHM VX4(38)
543 #define VMRGHB VX4(12)
544 #define VMRGHH VX4(76)
545 #define VMRGHW VX4(140)
546 #define VMRGLB VX4(268)
547 #define VMRGLH VX4(332)
548 #define VMRGLW VX4(396)
550 #define VPKUHUM VX4(14)
551 #define VPKUWUM VX4(78)
553 #define VAND VX4(1028)
554 #define VANDC VX4(1092)
555 #define VNOR VX4(1284)
556 #define VOR VX4(1156)
557 #define VXOR VX4(1220)
558 #define VEQV VX4(1668) /* v2.07 */
559 #define VNAND VX4(1412) /* v2.07 */
560 #define VORC VX4(1348) /* v2.07 */
562 #define VSPLTB VX4(524)
563 #define VSPLTH VX4(588)
564 #define VSPLTW VX4(652)
565 #define VSPLTISB VX4(780)
566 #define VSPLTISH VX4(844)
567 #define VSPLTISW VX4(908)
569 #define VSLDOI VX4(44)
571 #define XXPERMDI (OPCD(60) | (10 << 3) | 7) /* v2.06, force ax=bx=tx=1 */
572 #define XXSEL (OPCD(60) | (3 << 4) | 0xf) /* v2.06, force ax=bx=cx=tx=1 */
573 #define XXSPLTIB (OPCD(60) | (360 << 1) | 1) /* v3.00, force tx=1 */
575 #define MFVSRD (XO31(51) | 1) /* v2.07, force sx=1 */
576 #define MFVSRWZ (XO31(115) | 1) /* v2.07, force sx=1 */
577 #define MTVSRD (XO31(179) | 1) /* v2.07, force tx=1 */
578 #define MTVSRWZ (XO31(243) | 1) /* v2.07, force tx=1 */
579 #define MTVSRDD (XO31(435) | 1) /* v3.00, force tx=1 */
580 #define MTVSRWS (XO31(403) | 1) /* v3.00, force tx=1 */
582 #define RT(r) ((r)<<21)
583 #define RS(r) ((r)<<21)
584 #define RA(r) ((r)<<16)
585 #define RB(r) ((r)<<11)
586 #define TO(t) ((t)<<21)
587 #define SH(s) ((s)<<11)
588 #define MB(b) ((b)<<6)
589 #define ME(e) ((e)<<1)
590 #define BO(o) ((o)<<21)
591 #define MB64(b) ((b)<<5)
592 #define FXM(b) (1 << (19 - (b)))
594 #define VRT(r) (((r) & 31) << 21)
595 #define VRA(r) (((r) & 31) << 16)
596 #define VRB(r) (((r) & 31) << 11)
597 #define VRC(r) (((r) & 31) << 6)
601 #define TAB(t, a, b) (RT(t) | RA(a) | RB(b))
602 #define SAB(s, a, b) (RS(s) | RA(a) | RB(b))
603 #define TAI(s, a, i) (RT(s) | RA(a) | ((i) & 0xffff))
604 #define SAI(s, a, i) (RS(s) | RA(a) | ((i) & 0xffff))
606 #define BF(n) ((n)<<23)
607 #define BI(n, c) (((c)+((n)*4))<<16)
608 #define BT(n, c) (((c)+((n)*4))<<21)
609 #define BA(n, c) (((c)+((n)*4))<<16)
610 #define BB(n, c) (((c)+((n)*4))<<11)
611 #define BC_(n, c) (((c)+((n)*4))<<6)
613 #define BO_COND_TRUE BO(12)
614 #define BO_COND_FALSE BO( 4)
615 #define BO_ALWAYS BO(20)
624 static const uint32_t tcg_to_bc[] = {
625 [TCG_COND_EQ] = BC | BI(7, CR_EQ) | BO_COND_TRUE,
626 [TCG_COND_NE] = BC | BI(7, CR_EQ) | BO_COND_FALSE,
627 [TCG_COND_LT] = BC | BI(7, CR_LT) | BO_COND_TRUE,
628 [TCG_COND_GE] = BC | BI(7, CR_LT) | BO_COND_FALSE,
629 [TCG_COND_LE] = BC | BI(7, CR_GT) | BO_COND_FALSE,
630 [TCG_COND_GT] = BC | BI(7, CR_GT) | BO_COND_TRUE,
631 [TCG_COND_LTU] = BC | BI(7, CR_LT) | BO_COND_TRUE,
632 [TCG_COND_GEU] = BC | BI(7, CR_LT) | BO_COND_FALSE,
633 [TCG_COND_LEU] = BC | BI(7, CR_GT) | BO_COND_FALSE,
634 [TCG_COND_GTU] = BC | BI(7, CR_GT) | BO_COND_TRUE,
637 /* The low bit here is set if the RA and RB fields must be inverted. */
638 static const uint32_t tcg_to_isel[] = {
639 [TCG_COND_EQ] = ISEL | BC_(7, CR_EQ),
640 [TCG_COND_NE] = ISEL | BC_(7, CR_EQ) | 1,
641 [TCG_COND_LT] = ISEL | BC_(7, CR_LT),
642 [TCG_COND_GE] = ISEL | BC_(7, CR_LT) | 1,
643 [TCG_COND_LE] = ISEL | BC_(7, CR_GT) | 1,
644 [TCG_COND_GT] = ISEL | BC_(7, CR_GT),
645 [TCG_COND_LTU] = ISEL | BC_(7, CR_LT),
646 [TCG_COND_GEU] = ISEL | BC_(7, CR_LT) | 1,
647 [TCG_COND_LEU] = ISEL | BC_(7, CR_GT) | 1,
648 [TCG_COND_GTU] = ISEL | BC_(7, CR_GT),
651 static bool patch_reloc(tcg_insn_unit *code_ptr, int type,
652 intptr_t value, intptr_t addend)
654 const tcg_insn_unit *target;
659 target = (const tcg_insn_unit *)value;
663 return reloc_pc14(code_ptr, target);
665 return reloc_pc24(code_ptr, target);
668 * We are (slightly) abusing this relocation type. In particular,
669 * assert that the low 2 bits are zero, and do not modify them.
670 * That way we can use this with LD et al that have opcode bits
671 * in the low 2 bits of the insn.
673 if ((value & 3) || value != (int16_t)value) {
676 *code_ptr = (*code_ptr & ~0xfffc) | (value & 0xfffc);
680 * We are abusing this relocation type. Again, this points to
681 * a pair of insns, lis + load. This is an absolute address
682 * relocation for PPC32 so the lis cannot be removed.
686 if (hi + lo != value) {
689 code_ptr[0] = deposit32(code_ptr[0], 0, 16, hi >> 16);
690 code_ptr[1] = deposit32(code_ptr[1], 0, 16, lo);
693 g_assert_not_reached();
698 static void tcg_out_mem_long(TCGContext *s, int opi, int opx, TCGReg rt,
699 TCGReg base, tcg_target_long offset);
701 static bool tcg_out_mov(TCGContext *s, TCGType type, TCGReg ret, TCGReg arg)
708 tcg_debug_assert(TCG_TARGET_REG_BITS == 64);
711 if (ret < TCG_REG_V0) {
712 if (arg < TCG_REG_V0) {
713 tcg_out32(s, OR | SAB(arg, ret, arg));
715 } else if (have_isa_2_07) {
716 tcg_out32(s, (type == TCG_TYPE_I32 ? MFVSRWZ : MFVSRD)
717 | VRT(arg) | RA(ret));
720 /* Altivec does not support vector->integer moves. */
723 } else if (arg < TCG_REG_V0) {
725 tcg_out32(s, (type == TCG_TYPE_I32 ? MTVSRWZ : MTVSRD)
726 | VRT(ret) | RA(arg));
729 /* Altivec does not support integer->vector moves. */
736 tcg_debug_assert(ret >= TCG_REG_V0 && arg >= TCG_REG_V0);
737 tcg_out32(s, VOR | VRT(ret) | VRA(arg) | VRB(arg));
740 g_assert_not_reached();
745 static inline void tcg_out_rld(TCGContext *s, int op, TCGReg ra, TCGReg rs,
748 tcg_debug_assert(TCG_TARGET_REG_BITS == 64);
749 sh = SH(sh & 0x1f) | (((sh >> 5) & 1) << 1);
750 mb = MB64((mb >> 5) | ((mb << 1) & 0x3f));
751 tcg_out32(s, op | RA(ra) | RS(rs) | sh | mb);
754 static inline void tcg_out_rlw(TCGContext *s, int op, TCGReg ra, TCGReg rs,
755 int sh, int mb, int me)
757 tcg_out32(s, op | RA(ra) | RS(rs) | SH(sh) | MB(mb) | ME(me));
760 static inline void tcg_out_ext8s(TCGContext *s, TCGReg dst, TCGReg src)
762 tcg_out32(s, EXTSB | RA(dst) | RS(src));
765 static inline void tcg_out_ext16s(TCGContext *s, TCGReg dst, TCGReg src)
767 tcg_out32(s, EXTSH | RA(dst) | RS(src));
770 static inline void tcg_out_ext16u(TCGContext *s, TCGReg dst, TCGReg src)
772 tcg_out32(s, ANDI | SAI(src, dst, 0xffff));
775 static inline void tcg_out_ext32s(TCGContext *s, TCGReg dst, TCGReg src)
777 tcg_out32(s, EXTSW | RA(dst) | RS(src));
780 static inline void tcg_out_ext32u(TCGContext *s, TCGReg dst, TCGReg src)
782 tcg_out_rld(s, RLDICL, dst, src, 0, 32);
785 static inline void tcg_out_shli32(TCGContext *s, TCGReg dst, TCGReg src, int c)
787 tcg_out_rlw(s, RLWINM, dst, src, c, 0, 31 - c);
790 static inline void tcg_out_shli64(TCGContext *s, TCGReg dst, TCGReg src, int c)
792 tcg_out_rld(s, RLDICR, dst, src, c, 63 - c);
795 static inline void tcg_out_sari32(TCGContext *s, TCGReg dst, TCGReg src, int c)
797 /* Limit immediate shift count lest we create an illegal insn. */
798 tcg_out32(s, SRAWI | RA(dst) | RS(src) | SH(c & 31));
801 static inline void tcg_out_shri32(TCGContext *s, TCGReg dst, TCGReg src, int c)
803 tcg_out_rlw(s, RLWINM, dst, src, 32 - c, c, 31);
806 static inline void tcg_out_shri64(TCGContext *s, TCGReg dst, TCGReg src, int c)
808 tcg_out_rld(s, RLDICL, dst, src, 64 - c, c);
811 static inline void tcg_out_sari64(TCGContext *s, TCGReg dst, TCGReg src, int c)
813 tcg_out32(s, SRADI | RA(dst) | RS(src) | SH(c & 0x1f) | ((c >> 4) & 2));
816 static void tcg_out_bswap16(TCGContext *s, TCGReg dst, TCGReg src, int flags)
818 TCGReg tmp = dst == src ? TCG_REG_R0 : dst;
821 tcg_out32(s, BRH | RA(dst) | RS(src));
822 if (flags & TCG_BSWAP_OS) {
823 tcg_out_ext16s(s, dst, dst);
824 } else if ((flags & (TCG_BSWAP_IZ | TCG_BSWAP_OZ)) == TCG_BSWAP_OZ) {
825 tcg_out_ext16u(s, dst, dst);
832 * dep(a, b, m) -> (a & ~m) | (b & m)
834 * Begin with: src = xxxxabcd
836 /* tmp = rol32(src, 24) & 0x000000ff = 0000000c */
837 tcg_out_rlw(s, RLWINM, tmp, src, 24, 24, 31);
838 /* tmp = dep(tmp, rol32(src, 8), 0x0000ff00) = 000000dc */
839 tcg_out_rlw(s, RLWIMI, tmp, src, 8, 16, 23);
841 if (flags & TCG_BSWAP_OS) {
842 tcg_out_ext16s(s, dst, tmp);
844 tcg_out_mov(s, TCG_TYPE_REG, dst, tmp);
848 static void tcg_out_bswap32(TCGContext *s, TCGReg dst, TCGReg src, int flags)
850 TCGReg tmp = dst == src ? TCG_REG_R0 : dst;
853 tcg_out32(s, BRW | RA(dst) | RS(src));
854 if (flags & TCG_BSWAP_OS) {
855 tcg_out_ext32s(s, dst, dst);
856 } else if ((flags & (TCG_BSWAP_IZ | TCG_BSWAP_OZ)) == TCG_BSWAP_OZ) {
857 tcg_out_ext32u(s, dst, dst);
863 * Stolen from gcc's builtin_bswap32.
865 * dep(a, b, m) -> (a & ~m) | (b & m)
867 * Begin with: src = xxxxabcd
869 /* tmp = rol32(src, 8) & 0xffffffff = 0000bcda */
870 tcg_out_rlw(s, RLWINM, tmp, src, 8, 0, 31);
871 /* tmp = dep(tmp, rol32(src, 24), 0xff000000) = 0000dcda */
872 tcg_out_rlw(s, RLWIMI, tmp, src, 24, 0, 7);
873 /* tmp = dep(tmp, rol32(src, 24), 0x0000ff00) = 0000dcba */
874 tcg_out_rlw(s, RLWIMI, tmp, src, 24, 16, 23);
876 if (flags & TCG_BSWAP_OS) {
877 tcg_out_ext32s(s, dst, tmp);
879 tcg_out_mov(s, TCG_TYPE_REG, dst, tmp);
883 static void tcg_out_bswap64(TCGContext *s, TCGReg dst, TCGReg src)
885 TCGReg t0 = dst == src ? TCG_REG_R0 : dst;
886 TCGReg t1 = dst == src ? dst : TCG_REG_R0;
889 tcg_out32(s, BRD | RA(dst) | RS(src));
895 * dep(a, b, m) -> (a & ~m) | (b & m)
897 * Begin with: src = abcdefgh
899 /* t0 = rol32(src, 8) & 0xffffffff = 0000fghe */
900 tcg_out_rlw(s, RLWINM, t0, src, 8, 0, 31);
901 /* t0 = dep(t0, rol32(src, 24), 0xff000000) = 0000hghe */
902 tcg_out_rlw(s, RLWIMI, t0, src, 24, 0, 7);
903 /* t0 = dep(t0, rol32(src, 24), 0x0000ff00) = 0000hgfe */
904 tcg_out_rlw(s, RLWIMI, t0, src, 24, 16, 23);
906 /* t0 = rol64(t0, 32) = hgfe0000 */
907 tcg_out_rld(s, RLDICL, t0, t0, 32, 0);
908 /* t1 = rol64(src, 32) = efghabcd */
909 tcg_out_rld(s, RLDICL, t1, src, 32, 0);
911 /* t0 = dep(t0, rol32(t1, 24), 0xffffffff) = hgfebcda */
912 tcg_out_rlw(s, RLWIMI, t0, t1, 8, 0, 31);
913 /* t0 = dep(t0, rol32(t1, 24), 0xff000000) = hgfedcda */
914 tcg_out_rlw(s, RLWIMI, t0, t1, 24, 0, 7);
915 /* t0 = dep(t0, rol32(t1, 24), 0x0000ff00) = hgfedcba */
916 tcg_out_rlw(s, RLWIMI, t0, t1, 24, 16, 23);
918 tcg_out_mov(s, TCG_TYPE_REG, dst, t0);
921 /* Emit a move into ret of arg, if it can be done in one insn. */
922 static bool tcg_out_movi_one(TCGContext *s, TCGReg ret, tcg_target_long arg)
924 if (arg == (int16_t)arg) {
925 tcg_out32(s, ADDI | TAI(ret, 0, arg));
928 if (arg == (int32_t)arg && (arg & 0xffff) == 0) {
929 tcg_out32(s, ADDIS | TAI(ret, 0, arg >> 16));
935 static void tcg_out_movi_int(TCGContext *s, TCGType type, TCGReg ret,
936 tcg_target_long arg, bool in_prologue)
942 tcg_debug_assert(TCG_TARGET_REG_BITS == 64 || type == TCG_TYPE_I32);
944 if (TCG_TARGET_REG_BITS == 64 && type == TCG_TYPE_I32) {
948 /* Load 16-bit immediates with one insn. */
949 if (tcg_out_movi_one(s, ret, arg)) {
953 /* Load addresses within the TB with one insn. */
954 tb_diff = tcg_tbrel_diff(s, (void *)arg);
955 if (!in_prologue && USE_REG_TB && tb_diff == (int16_t)tb_diff) {
956 tcg_out32(s, ADDI | TAI(ret, TCG_REG_TB, tb_diff));
960 /* Load 32-bit immediates with two insns. Note that we've already
961 eliminated bare ADDIS, so we know both insns are required. */
962 if (TCG_TARGET_REG_BITS == 32 || arg == (int32_t)arg) {
963 tcg_out32(s, ADDIS | TAI(ret, 0, arg >> 16));
964 tcg_out32(s, ORI | SAI(ret, ret, arg));
967 if (arg == (uint32_t)arg && !(arg & 0x8000)) {
968 tcg_out32(s, ADDI | TAI(ret, 0, arg));
969 tcg_out32(s, ORIS | SAI(ret, ret, arg >> 16));
973 /* Load masked 16-bit value. */
974 if (arg > 0 && (arg & 0x8000)) {
976 if ((tmp & (tmp + 1)) == 0) {
977 int mb = clz64(tmp + 1) + 1;
978 tcg_out32(s, ADDI | TAI(ret, 0, arg));
979 tcg_out_rld(s, RLDICL, ret, ret, 0, mb);
984 /* Load common masks with 2 insns. */
987 if (tmp == (int16_t)tmp) {
988 tcg_out32(s, ADDI | TAI(ret, 0, tmp));
989 tcg_out_shli64(s, ret, ret, shift);
993 if (tcg_out_movi_one(s, ret, arg << shift)) {
994 tcg_out_shri64(s, ret, ret, shift);
998 /* Load addresses within 2GB of TB with 2 (or rarely 3) insns. */
999 if (!in_prologue && USE_REG_TB && tb_diff == (int32_t)tb_diff) {
1000 tcg_out_mem_long(s, ADDI, ADD, ret, TCG_REG_TB, tb_diff);
1004 /* Use the constant pool, if possible. */
1005 if (!in_prologue && USE_REG_TB) {
1006 new_pool_label(s, arg, R_PPC_ADDR16, s->code_ptr,
1007 tcg_tbrel_diff(s, NULL));
1008 tcg_out32(s, LD | TAI(ret, TCG_REG_TB, 0));
1012 tmp = arg >> 31 >> 1;
1013 tcg_out_movi(s, TCG_TYPE_I32, ret, tmp);
1015 tcg_out_shli64(s, ret, ret, 32);
1017 if (arg & 0xffff0000) {
1018 tcg_out32(s, ORIS | SAI(ret, ret, arg >> 16));
1021 tcg_out32(s, ORI | SAI(ret, ret, arg));
1025 static void tcg_out_dupi_vec(TCGContext *s, TCGType type, unsigned vece,
1026 TCGReg ret, int64_t val)
1035 if (low >= -16 && low < 16) {
1036 tcg_out32(s, VSPLTISB | VRT(ret) | ((val & 31) << 16));
1039 if (have_isa_3_00) {
1040 tcg_out32(s, XXSPLTIB | VRT(ret) | ((val & 0xff) << 11));
1047 if (low >= -16 && low < 16) {
1048 tcg_out32(s, VSPLTISH | VRT(ret) | ((val & 31) << 16));
1055 if (low >= -16 && low < 16) {
1056 tcg_out32(s, VSPLTISW | VRT(ret) | ((val & 31) << 16));
1063 * Otherwise we must load the value from the constant pool.
1067 add = tcg_tbrel_diff(s, NULL);
1074 load_insn = type == TCG_TYPE_V64 ? LXSDX : LXVDSX;
1075 load_insn |= VRT(ret) | RB(TCG_REG_TMP1);
1076 if (TCG_TARGET_REG_BITS == 64) {
1077 new_pool_label(s, val, rel, s->code_ptr, add);
1079 new_pool_l2(s, rel, s->code_ptr, add, val >> 32, val);
1082 load_insn = LVX | VRT(ret) | RB(TCG_REG_TMP1);
1083 if (TCG_TARGET_REG_BITS == 64) {
1084 new_pool_l2(s, rel, s->code_ptr, add, val, val);
1086 new_pool_l4(s, rel, s->code_ptr, add,
1087 val >> 32, val, val >> 32, val);
1092 tcg_out32(s, ADDI | TAI(TCG_REG_TMP1, 0, 0));
1093 load_insn |= RA(TCG_REG_TB);
1095 tcg_out32(s, ADDIS | TAI(TCG_REG_TMP1, 0, 0));
1096 tcg_out32(s, ADDI | TAI(TCG_REG_TMP1, TCG_REG_TMP1, 0));
1098 tcg_out32(s, load_insn);
1101 static void tcg_out_movi(TCGContext *s, TCGType type, TCGReg ret,
1102 tcg_target_long arg)
1107 tcg_debug_assert(ret < TCG_REG_V0);
1108 tcg_out_movi_int(s, type, ret, arg, false);
1112 g_assert_not_reached();
1116 static bool mask_operand(uint32_t c, int *mb, int *me)
1120 /* Accept a bit pattern like:
1124 Keep track of the transitions. */
1125 if (c == 0 || c == -1) {
1131 if (test & (test - 1)) {
1136 *mb = test ? clz32(test & -test) + 1 : 0;
1140 static bool mask64_operand(uint64_t c, int *mb, int *me)
1149 /* Accept 1..10..0. */
1155 /* Accept 0..01..1. */
1156 if (lsb == 1 && (c & (c + 1)) == 0) {
1157 *mb = clz64(c + 1) + 1;
1164 static void tcg_out_andi32(TCGContext *s, TCGReg dst, TCGReg src, uint32_t c)
1168 if (mask_operand(c, &mb, &me)) {
1169 tcg_out_rlw(s, RLWINM, dst, src, 0, mb, me);
1170 } else if ((c & 0xffff) == c) {
1171 tcg_out32(s, ANDI | SAI(src, dst, c));
1173 } else if ((c & 0xffff0000) == c) {
1174 tcg_out32(s, ANDIS | SAI(src, dst, c >> 16));
1177 tcg_out_movi(s, TCG_TYPE_I32, TCG_REG_R0, c);
1178 tcg_out32(s, AND | SAB(src, dst, TCG_REG_R0));
1182 static void tcg_out_andi64(TCGContext *s, TCGReg dst, TCGReg src, uint64_t c)
1186 tcg_debug_assert(TCG_TARGET_REG_BITS == 64);
1187 if (mask64_operand(c, &mb, &me)) {
1189 tcg_out_rld(s, RLDICR, dst, src, 0, me);
1191 tcg_out_rld(s, RLDICL, dst, src, 0, mb);
1193 } else if ((c & 0xffff) == c) {
1194 tcg_out32(s, ANDI | SAI(src, dst, c));
1196 } else if ((c & 0xffff0000) == c) {
1197 tcg_out32(s, ANDIS | SAI(src, dst, c >> 16));
1200 tcg_out_movi(s, TCG_TYPE_I64, TCG_REG_R0, c);
1201 tcg_out32(s, AND | SAB(src, dst, TCG_REG_R0));
1205 static void tcg_out_zori32(TCGContext *s, TCGReg dst, TCGReg src, uint32_t c,
1206 int op_lo, int op_hi)
1209 tcg_out32(s, op_hi | SAI(src, dst, c >> 16));
1213 tcg_out32(s, op_lo | SAI(src, dst, c));
1218 static void tcg_out_ori32(TCGContext *s, TCGReg dst, TCGReg src, uint32_t c)
1220 tcg_out_zori32(s, dst, src, c, ORI, ORIS);
1223 static void tcg_out_xori32(TCGContext *s, TCGReg dst, TCGReg src, uint32_t c)
1225 tcg_out_zori32(s, dst, src, c, XORI, XORIS);
1228 static void tcg_out_b(TCGContext *s, int mask, const tcg_insn_unit *target)
1230 ptrdiff_t disp = tcg_pcrel_diff(s, target);
1231 if (in_range_b(disp)) {
1232 tcg_out32(s, B | (disp & 0x3fffffc) | mask);
1234 tcg_out_movi(s, TCG_TYPE_PTR, TCG_REG_R0, (uintptr_t)target);
1235 tcg_out32(s, MTSPR | RS(TCG_REG_R0) | CTR);
1236 tcg_out32(s, BCCTR | BO_ALWAYS | mask);
1240 static void tcg_out_mem_long(TCGContext *s, int opi, int opx, TCGReg rt,
1241 TCGReg base, tcg_target_long offset)
1243 tcg_target_long orig = offset, l0, l1, extra = 0, align = 0;
1244 bool is_int_store = false;
1245 TCGReg rs = TCG_REG_TMP1;
1252 if (rt > TCG_REG_R0 && rt < TCG_REG_V0) {
1268 case STB: case STH: case STW:
1269 is_int_store = true;
1273 /* For unaligned, or very large offsets, use the indexed form. */
1274 if (offset & align || offset != (int32_t)offset || opi == 0) {
1278 tcg_debug_assert(!is_int_store || rs != rt);
1279 tcg_out_movi(s, TCG_TYPE_PTR, rs, orig);
1280 tcg_out32(s, opx | TAB(rt & 31, base, rs));
1284 l0 = (int16_t)offset;
1285 offset = (offset - l0) >> 16;
1286 l1 = (int16_t)offset;
1288 if (l1 < 0 && orig >= 0) {
1290 l1 = (int16_t)(offset - 0x4000);
1293 tcg_out32(s, ADDIS | TAI(rs, base, l1));
1297 tcg_out32(s, ADDIS | TAI(rs, base, extra));
1300 if (opi != ADDI || base != rt || l0 != 0) {
1301 tcg_out32(s, opi | TAI(rt & 31, base, l0));
1305 static void tcg_out_vsldoi(TCGContext *s, TCGReg ret,
1306 TCGReg va, TCGReg vb, int shb)
1308 tcg_out32(s, VSLDOI | VRT(ret) | VRA(va) | VRB(vb) | (shb << 6));
1311 static void tcg_out_ld(TCGContext *s, TCGType type, TCGReg ret,
1312 TCGReg base, intptr_t offset)
1318 if (ret < TCG_REG_V0) {
1319 tcg_out_mem_long(s, LWZ, LWZX, ret, base, offset);
1322 if (have_isa_2_07 && have_vsx) {
1323 tcg_out_mem_long(s, 0, LXSIWZX, ret, base, offset);
1326 tcg_debug_assert((offset & 3) == 0);
1327 tcg_out_mem_long(s, 0, LVEWX, ret, base, offset);
1328 shift = (offset - 4) & 0xc;
1330 tcg_out_vsldoi(s, ret, ret, ret, shift);
1334 if (ret < TCG_REG_V0) {
1335 tcg_debug_assert(TCG_TARGET_REG_BITS == 64);
1336 tcg_out_mem_long(s, LD, LDX, ret, base, offset);
1341 tcg_debug_assert(ret >= TCG_REG_V0);
1343 tcg_out_mem_long(s, have_isa_3_00 ? LXSD : 0, LXSDX,
1347 tcg_debug_assert((offset & 7) == 0);
1348 tcg_out_mem_long(s, 0, LVX, ret, base, offset & -16);
1350 tcg_out_vsldoi(s, ret, ret, ret, 8);
1354 tcg_debug_assert(ret >= TCG_REG_V0);
1355 tcg_debug_assert((offset & 15) == 0);
1356 tcg_out_mem_long(s, have_isa_3_00 ? LXV : 0,
1357 LVX, ret, base, offset);
1360 g_assert_not_reached();
1364 static void tcg_out_st(TCGContext *s, TCGType type, TCGReg arg,
1365 TCGReg base, intptr_t offset)
1371 if (arg < TCG_REG_V0) {
1372 tcg_out_mem_long(s, STW, STWX, arg, base, offset);
1375 if (have_isa_2_07 && have_vsx) {
1376 tcg_out_mem_long(s, 0, STXSIWX, arg, base, offset);
1379 assert((offset & 3) == 0);
1380 tcg_debug_assert((offset & 3) == 0);
1381 shift = (offset - 4) & 0xc;
1383 tcg_out_vsldoi(s, TCG_VEC_TMP1, arg, arg, shift);
1386 tcg_out_mem_long(s, 0, STVEWX, arg, base, offset);
1389 if (arg < TCG_REG_V0) {
1390 tcg_debug_assert(TCG_TARGET_REG_BITS == 64);
1391 tcg_out_mem_long(s, STD, STDX, arg, base, offset);
1396 tcg_debug_assert(arg >= TCG_REG_V0);
1398 tcg_out_mem_long(s, have_isa_3_00 ? STXSD : 0,
1399 STXSDX, arg, base, offset);
1402 tcg_debug_assert((offset & 7) == 0);
1404 tcg_out_vsldoi(s, TCG_VEC_TMP1, arg, arg, 8);
1407 tcg_out_mem_long(s, 0, STVEWX, arg, base, offset);
1408 tcg_out_mem_long(s, 0, STVEWX, arg, base, offset + 4);
1411 tcg_debug_assert(arg >= TCG_REG_V0);
1412 tcg_out_mem_long(s, have_isa_3_00 ? STXV : 0,
1413 STVX, arg, base, offset);
1416 g_assert_not_reached();
1420 static inline bool tcg_out_sti(TCGContext *s, TCGType type, TCGArg val,
1421 TCGReg base, intptr_t ofs)
1426 static void tcg_out_cmp(TCGContext *s, int cond, TCGArg arg1, TCGArg arg2,
1427 int const_arg2, int cr, TCGType type)
1432 tcg_debug_assert(TCG_TARGET_REG_BITS == 64 || type == TCG_TYPE_I32);
1434 /* Simplify the comparisons below wrt CMPI. */
1435 if (type == TCG_TYPE_I32) {
1436 arg2 = (int32_t)arg2;
1443 if ((int16_t) arg2 == arg2) {
1447 } else if ((uint16_t) arg2 == arg2) {
1462 if ((int16_t) arg2 == arg2) {
1477 if ((uint16_t) arg2 == arg2) {
1490 op |= BF(cr) | ((type == TCG_TYPE_I64) << 21);
1493 tcg_out32(s, op | RA(arg1) | (arg2 & 0xffff));
1496 tcg_out_movi(s, type, TCG_REG_R0, arg2);
1499 tcg_out32(s, op | RA(arg1) | RB(arg2));
1503 static void tcg_out_setcond_eq0(TCGContext *s, TCGType type,
1504 TCGReg dst, TCGReg src)
1506 if (type == TCG_TYPE_I32) {
1507 tcg_out32(s, CNTLZW | RS(src) | RA(dst));
1508 tcg_out_shri32(s, dst, dst, 5);
1510 tcg_out32(s, CNTLZD | RS(src) | RA(dst));
1511 tcg_out_shri64(s, dst, dst, 6);
1515 static void tcg_out_setcond_ne0(TCGContext *s, TCGReg dst, TCGReg src)
1517 /* X != 0 implies X + -1 generates a carry. Extra addition
1518 trickery means: R = X-1 + ~X + C = X-1 + (-X+1) + C = C. */
1520 tcg_out32(s, ADDIC | TAI(dst, src, -1));
1521 tcg_out32(s, SUBFE | TAB(dst, dst, src));
1523 tcg_out32(s, ADDIC | TAI(TCG_REG_R0, src, -1));
1524 tcg_out32(s, SUBFE | TAB(dst, TCG_REG_R0, src));
1528 static TCGReg tcg_gen_setcond_xor(TCGContext *s, TCGReg arg1, TCGArg arg2,
1532 if ((uint32_t)arg2 == arg2) {
1533 tcg_out_xori32(s, TCG_REG_R0, arg1, arg2);
1535 tcg_out_movi(s, TCG_TYPE_I64, TCG_REG_R0, arg2);
1536 tcg_out32(s, XOR | SAB(arg1, TCG_REG_R0, TCG_REG_R0));
1539 tcg_out32(s, XOR | SAB(arg1, TCG_REG_R0, arg2));
1544 static void tcg_out_setcond(TCGContext *s, TCGType type, TCGCond cond,
1545 TCGArg arg0, TCGArg arg1, TCGArg arg2,
1550 tcg_debug_assert(TCG_TARGET_REG_BITS == 64 || type == TCG_TYPE_I32);
1552 /* Ignore high bits of a potential constant arg2. */
1553 if (type == TCG_TYPE_I32) {
1554 arg2 = (uint32_t)arg2;
1557 /* Handle common and trivial cases before handling anything else. */
1561 tcg_out_setcond_eq0(s, type, arg0, arg1);
1564 if (TCG_TARGET_REG_BITS == 64 && type == TCG_TYPE_I32) {
1565 tcg_out_ext32u(s, TCG_REG_R0, arg1);
1568 tcg_out_setcond_ne0(s, arg0, arg1);
1571 tcg_out32(s, NOR | SAB(arg1, arg0, arg1));
1575 /* Extract the sign bit. */
1576 if (type == TCG_TYPE_I32) {
1577 tcg_out_shri32(s, arg0, arg1, 31);
1579 tcg_out_shri64(s, arg0, arg1, 63);
1587 /* If we have ISEL, we can implement everything with 3 or 4 insns.
1588 All other cases below are also at least 3 insns, so speed up the
1589 code generator by not considering them and always using ISEL. */
1593 tcg_out_cmp(s, cond, arg1, arg2, const_arg2, 7, type);
1595 isel = tcg_to_isel[cond];
1597 tcg_out_movi(s, type, arg0, 1);
1599 /* arg0 = (bc ? 0 : 1) */
1600 tab = TAB(arg0, 0, arg0);
1603 /* arg0 = (bc ? 1 : 0) */
1604 tcg_out_movi(s, type, TCG_REG_R0, 0);
1605 tab = TAB(arg0, arg0, TCG_REG_R0);
1607 tcg_out32(s, isel | tab);
1613 arg1 = tcg_gen_setcond_xor(s, arg1, arg2, const_arg2);
1614 tcg_out_setcond_eq0(s, type, arg0, arg1);
1618 arg1 = tcg_gen_setcond_xor(s, arg1, arg2, const_arg2);
1619 /* Discard the high bits only once, rather than both inputs. */
1620 if (TCG_TARGET_REG_BITS == 64 && type == TCG_TYPE_I32) {
1621 tcg_out_ext32u(s, TCG_REG_R0, arg1);
1624 tcg_out_setcond_ne0(s, arg0, arg1);
1642 crop = CRNOR | BT(7, CR_EQ) | BA(7, CR_LT) | BB(7, CR_LT);
1648 crop = CRNOR | BT(7, CR_EQ) | BA(7, CR_GT) | BB(7, CR_GT);
1650 tcg_out_cmp(s, cond, arg1, arg2, const_arg2, 7, type);
1654 tcg_out32(s, MFOCRF | RT(TCG_REG_R0) | FXM(7));
1655 tcg_out_rlw(s, RLWINM, arg0, TCG_REG_R0, sh, 31, 31);
1663 static void tcg_out_bc(TCGContext *s, int bc, TCGLabel *l)
1666 bc |= reloc_pc14_val(tcg_splitwx_to_rx(s->code_ptr), l->u.value_ptr);
1668 tcg_out_reloc(s, s->code_ptr, R_PPC_REL14, l, 0);
1673 static void tcg_out_brcond(TCGContext *s, TCGCond cond,
1674 TCGArg arg1, TCGArg arg2, int const_arg2,
1675 TCGLabel *l, TCGType type)
1677 tcg_out_cmp(s, cond, arg1, arg2, const_arg2, 7, type);
1678 tcg_out_bc(s, tcg_to_bc[cond], l);
1681 static void tcg_out_movcond(TCGContext *s, TCGType type, TCGCond cond,
1682 TCGArg dest, TCGArg c1, TCGArg c2, TCGArg v1,
1683 TCGArg v2, bool const_c2)
1685 /* If for some reason both inputs are zero, don't produce bad code. */
1686 if (v1 == 0 && v2 == 0) {
1687 tcg_out_movi(s, type, dest, 0);
1691 tcg_out_cmp(s, cond, c1, c2, const_c2, 7, type);
1694 int isel = tcg_to_isel[cond];
1696 /* Swap the V operands if the operation indicates inversion. */
1703 /* V1 == 0 is handled by isel; V2 == 0 must be handled by hand. */
1705 tcg_out_movi(s, type, TCG_REG_R0, 0);
1707 tcg_out32(s, isel | TAB(dest, v1, v2));
1710 cond = tcg_invert_cond(cond);
1712 } else if (dest != v1) {
1714 tcg_out_movi(s, type, dest, 0);
1716 tcg_out_mov(s, type, dest, v1);
1719 /* Branch forward over one insn */
1720 tcg_out32(s, tcg_to_bc[cond] | 8);
1722 tcg_out_movi(s, type, dest, 0);
1724 tcg_out_mov(s, type, dest, v2);
1729 static void tcg_out_cntxz(TCGContext *s, TCGType type, uint32_t opc,
1730 TCGArg a0, TCGArg a1, TCGArg a2, bool const_a2)
1732 if (const_a2 && a2 == (type == TCG_TYPE_I32 ? 32 : 64)) {
1733 tcg_out32(s, opc | RA(a0) | RS(a1));
1735 tcg_out_cmp(s, TCG_COND_EQ, a1, 0, 1, 7, type);
1736 /* Note that the only other valid constant for a2 is 0. */
1738 tcg_out32(s, opc | RA(TCG_REG_R0) | RS(a1));
1739 tcg_out32(s, tcg_to_isel[TCG_COND_EQ] | TAB(a0, a2, TCG_REG_R0));
1740 } else if (!const_a2 && a0 == a2) {
1741 tcg_out32(s, tcg_to_bc[TCG_COND_EQ] | 8);
1742 tcg_out32(s, opc | RA(a0) | RS(a1));
1744 tcg_out32(s, opc | RA(a0) | RS(a1));
1745 tcg_out32(s, tcg_to_bc[TCG_COND_NE] | 8);
1747 tcg_out_movi(s, type, a0, 0);
1749 tcg_out_mov(s, type, a0, a2);
1755 static void tcg_out_cmp2(TCGContext *s, const TCGArg *args,
1756 const int *const_args)
1758 static const struct { uint8_t bit1, bit2; } bits[] = {
1759 [TCG_COND_LT ] = { CR_LT, CR_LT },
1760 [TCG_COND_LE ] = { CR_LT, CR_GT },
1761 [TCG_COND_GT ] = { CR_GT, CR_GT },
1762 [TCG_COND_GE ] = { CR_GT, CR_LT },
1763 [TCG_COND_LTU] = { CR_LT, CR_LT },
1764 [TCG_COND_LEU] = { CR_LT, CR_GT },
1765 [TCG_COND_GTU] = { CR_GT, CR_GT },
1766 [TCG_COND_GEU] = { CR_GT, CR_LT },
1769 TCGCond cond = args[4], cond2;
1770 TCGArg al, ah, bl, bh;
1771 int blconst, bhconst;
1778 blconst = const_args[2];
1779 bhconst = const_args[3];
1788 tcg_out_cmp(s, cond, al, bl, blconst, 6, TCG_TYPE_I32);
1789 tcg_out_cmp(s, cond, ah, bh, bhconst, 7, TCG_TYPE_I32);
1790 tcg_out32(s, op | BT(7, CR_EQ) | BA(6, CR_EQ) | BB(7, CR_EQ));
1801 bit1 = bits[cond].bit1;
1802 bit2 = bits[cond].bit2;
1803 op = (bit1 != bit2 ? CRANDC : CRAND);
1804 cond2 = tcg_unsigned_cond(cond);
1806 tcg_out_cmp(s, cond, ah, bh, bhconst, 6, TCG_TYPE_I32);
1807 tcg_out_cmp(s, cond2, al, bl, blconst, 7, TCG_TYPE_I32);
1808 tcg_out32(s, op | BT(7, CR_EQ) | BA(6, CR_EQ) | BB(7, bit2));
1809 tcg_out32(s, CROR | BT(7, CR_EQ) | BA(6, bit1) | BB(7, CR_EQ));
1817 static void tcg_out_setcond2(TCGContext *s, const TCGArg *args,
1818 const int *const_args)
1820 tcg_out_cmp2(s, args + 1, const_args + 1);
1821 tcg_out32(s, MFOCRF | RT(TCG_REG_R0) | FXM(7));
1822 tcg_out_rlw(s, RLWINM, args[0], TCG_REG_R0, 31, 31, 31);
1825 static void tcg_out_brcond2 (TCGContext *s, const TCGArg *args,
1826 const int *const_args)
1828 tcg_out_cmp2(s, args, const_args);
1829 tcg_out_bc(s, BC | BI(7, CR_EQ) | BO_COND_TRUE, arg_label(args[5]));
1832 static void tcg_out_mb(TCGContext *s, TCGArg a0)
1834 uint32_t insn = HWSYNC;
1836 if (a0 == TCG_MO_LD_LD) {
1838 } else if (a0 == TCG_MO_ST_ST) {
1844 void tb_target_set_jmp_target(uintptr_t tc_ptr, uintptr_t jmp_rx,
1845 uintptr_t jmp_rw, uintptr_t addr)
1847 if (TCG_TARGET_REG_BITS == 64) {
1848 tcg_insn_unit i1, i2;
1849 intptr_t tb_diff = addr - tc_ptr;
1850 intptr_t br_diff = addr - (jmp_rx + 4);
1853 /* This does not exercise the range of the branch, but we do
1854 still need to be able to load the new value of TCG_REG_TB.
1855 But this does still happen quite often. */
1856 if (tb_diff == (int16_t)tb_diff) {
1857 i1 = ADDI | TAI(TCG_REG_TB, TCG_REG_TB, tb_diff);
1858 i2 = B | (br_diff & 0x3fffffc);
1860 intptr_t lo = (int16_t)tb_diff;
1861 intptr_t hi = (int32_t)(tb_diff - lo);
1862 assert(tb_diff == hi + lo);
1863 i1 = ADDIS | TAI(TCG_REG_TB, TCG_REG_TB, hi >> 16);
1864 i2 = ADDI | TAI(TCG_REG_TB, TCG_REG_TB, lo);
1866 #ifdef HOST_WORDS_BIGENDIAN
1867 pair = (uint64_t)i1 << 32 | i2;
1869 pair = (uint64_t)i2 << 32 | i1;
1872 /* As per the enclosing if, this is ppc64. Avoid the _Static_assert
1873 within qatomic_set that would fail to build a ppc32 host. */
1874 qatomic_set__nocheck((uint64_t *)jmp_rw, pair);
1875 flush_idcache_range(jmp_rx, jmp_rw, 8);
1877 intptr_t diff = addr - jmp_rx;
1878 tcg_debug_assert(in_range_b(diff));
1879 qatomic_set((uint32_t *)jmp_rw, B | (diff & 0x3fffffc));
1880 flush_idcache_range(jmp_rx, jmp_rw, 4);
1884 static void tcg_out_call(TCGContext *s, const tcg_insn_unit *target)
1887 /* Look through the descriptor. If the branch is in range, and we
1888 don't have to spend too much effort on building the toc. */
1889 const void *tgt = ((const void * const *)target)[0];
1890 uintptr_t toc = ((const uintptr_t *)target)[1];
1891 intptr_t diff = tcg_pcrel_diff(s, tgt);
1893 if (in_range_b(diff) && toc == (uint32_t)toc) {
1894 tcg_out_movi(s, TCG_TYPE_PTR, TCG_REG_TMP1, toc);
1895 tcg_out_b(s, LK, tgt);
1897 /* Fold the low bits of the constant into the addresses below. */
1898 intptr_t arg = (intptr_t)target;
1899 int ofs = (int16_t)arg;
1901 if (ofs + 8 < 0x8000) {
1906 tcg_out_movi(s, TCG_TYPE_PTR, TCG_REG_TMP1, arg);
1907 tcg_out_ld(s, TCG_TYPE_PTR, TCG_REG_R0, TCG_REG_TMP1, ofs);
1908 tcg_out32(s, MTSPR | RA(TCG_REG_R0) | CTR);
1909 tcg_out_ld(s, TCG_TYPE_PTR, TCG_REG_R2, TCG_REG_TMP1, ofs + SZP);
1910 tcg_out32(s, BCCTR | BO_ALWAYS | LK);
1912 #elif defined(_CALL_ELF) && _CALL_ELF == 2
1915 /* In the ELFv2 ABI, we have to set up r12 to contain the destination
1916 address, which the callee uses to compute its TOC address. */
1917 /* FIXME: when the branch is in range, we could avoid r12 load if we
1918 knew that the destination uses the same TOC, and what its local
1919 entry point offset is. */
1920 tcg_out_movi(s, TCG_TYPE_PTR, TCG_REG_R12, (intptr_t)target);
1922 diff = tcg_pcrel_diff(s, target);
1923 if (in_range_b(diff)) {
1924 tcg_out_b(s, LK, target);
1926 tcg_out32(s, MTSPR | RS(TCG_REG_R12) | CTR);
1927 tcg_out32(s, BCCTR | BO_ALWAYS | LK);
1930 tcg_out_b(s, LK, target);
1934 static const uint32_t qemu_ldx_opc[16] = {
1941 [MO_BSWAP | MO_UB] = LBZX,
1942 [MO_BSWAP | MO_UW] = LHBRX,
1943 [MO_BSWAP | MO_UL] = LWBRX,
1944 [MO_BSWAP | MO_Q] = LDBRX,
1947 static const uint32_t qemu_stx_opc[16] = {
1952 [MO_BSWAP | MO_UB] = STBX,
1953 [MO_BSWAP | MO_UW] = STHBRX,
1954 [MO_BSWAP | MO_UL] = STWBRX,
1955 [MO_BSWAP | MO_Q] = STDBRX,
1958 static const uint32_t qemu_exts_opc[4] = {
1959 EXTSB, EXTSH, EXTSW, 0
1962 #if defined (CONFIG_SOFTMMU)
1963 #include "../tcg-ldst.c.inc"
1965 /* helper signature: helper_ld_mmu(CPUState *env, target_ulong addr,
1966 * int mmu_idx, uintptr_t ra)
1968 static void * const qemu_ld_helpers[16] = {
1969 [MO_UB] = helper_ret_ldub_mmu,
1970 [MO_LEUW] = helper_le_lduw_mmu,
1971 [MO_LEUL] = helper_le_ldul_mmu,
1972 [MO_LEQ] = helper_le_ldq_mmu,
1973 [MO_BEUW] = helper_be_lduw_mmu,
1974 [MO_BEUL] = helper_be_ldul_mmu,
1975 [MO_BEQ] = helper_be_ldq_mmu,
1978 /* helper signature: helper_st_mmu(CPUState *env, target_ulong addr,
1979 * uintxx_t val, int mmu_idx, uintptr_t ra)
1981 static void * const qemu_st_helpers[16] = {
1982 [MO_UB] = helper_ret_stb_mmu,
1983 [MO_LEUW] = helper_le_stw_mmu,
1984 [MO_LEUL] = helper_le_stl_mmu,
1985 [MO_LEQ] = helper_le_stq_mmu,
1986 [MO_BEUW] = helper_be_stw_mmu,
1987 [MO_BEUL] = helper_be_stl_mmu,
1988 [MO_BEQ] = helper_be_stq_mmu,
1991 /* We expect to use a 16-bit negative offset from ENV. */
1992 QEMU_BUILD_BUG_ON(TLB_MASK_TABLE_OFS(0) > 0);
1993 QEMU_BUILD_BUG_ON(TLB_MASK_TABLE_OFS(0) < -32768);
1995 /* Perform the TLB load and compare. Places the result of the comparison
1996 in CR7, loads the addend of the TLB into R3, and returns the register
1997 containing the guest address (zero-extended into R4). Clobbers R0 and R2. */
1999 static TCGReg tcg_out_tlb_read(TCGContext *s, MemOp opc,
2000 TCGReg addrlo, TCGReg addrhi,
2001 int mem_index, bool is_read)
2005 ? offsetof(CPUTLBEntry, addr_read)
2006 : offsetof(CPUTLBEntry, addr_write));
2007 int fast_off = TLB_MASK_TABLE_OFS(mem_index);
2008 int mask_off = fast_off + offsetof(CPUTLBDescFast, mask);
2009 int table_off = fast_off + offsetof(CPUTLBDescFast, table);
2010 unsigned s_bits = opc & MO_SIZE;
2011 unsigned a_bits = get_alignment_bits(opc);
2013 /* Load tlb_mask[mmu_idx] and tlb_table[mmu_idx]. */
2014 tcg_out_ld(s, TCG_TYPE_PTR, TCG_REG_R3, TCG_AREG0, mask_off);
2015 tcg_out_ld(s, TCG_TYPE_PTR, TCG_REG_R4, TCG_AREG0, table_off);
2017 /* Extract the page index, shifted into place for tlb index. */
2018 if (TCG_TARGET_REG_BITS == 32) {
2019 tcg_out_shri32(s, TCG_REG_TMP1, addrlo,
2020 TARGET_PAGE_BITS - CPU_TLB_ENTRY_BITS);
2022 tcg_out_shri64(s, TCG_REG_TMP1, addrlo,
2023 TARGET_PAGE_BITS - CPU_TLB_ENTRY_BITS);
2025 tcg_out32(s, AND | SAB(TCG_REG_R3, TCG_REG_R3, TCG_REG_TMP1));
2027 /* Load the TLB comparator. */
2028 if (cmp_off == 0 && TCG_TARGET_REG_BITS >= TARGET_LONG_BITS) {
2029 uint32_t lxu = (TCG_TARGET_REG_BITS == 32 || TARGET_LONG_BITS == 32
2031 tcg_out32(s, lxu | TAB(TCG_REG_TMP1, TCG_REG_R3, TCG_REG_R4));
2033 tcg_out32(s, ADD | TAB(TCG_REG_R3, TCG_REG_R3, TCG_REG_R4));
2034 if (TCG_TARGET_REG_BITS < TARGET_LONG_BITS) {
2035 tcg_out_ld(s, TCG_TYPE_I32, TCG_REG_TMP1, TCG_REG_R3, cmp_off + 4);
2036 tcg_out_ld(s, TCG_TYPE_I32, TCG_REG_R4, TCG_REG_R3, cmp_off);
2038 tcg_out_ld(s, TCG_TYPE_TL, TCG_REG_TMP1, TCG_REG_R3, cmp_off);
2042 /* Load the TLB addend for use on the fast path. Do this asap
2043 to minimize any load use delay. */
2044 tcg_out_ld(s, TCG_TYPE_PTR, TCG_REG_R3, TCG_REG_R3,
2045 offsetof(CPUTLBEntry, addend));
2047 /* Clear the non-page, non-alignment bits from the address */
2048 if (TCG_TARGET_REG_BITS == 32) {
2049 /* We don't support unaligned accesses on 32-bits.
2050 * Preserve the bottom bits and thus trigger a comparison
2051 * failure on unaligned accesses.
2053 if (a_bits < s_bits) {
2056 tcg_out_rlw(s, RLWINM, TCG_REG_R0, addrlo, 0,
2057 (32 - a_bits) & 31, 31 - TARGET_PAGE_BITS);
2061 /* If the access is unaligned, we need to make sure we fail if we
2062 * cross a page boundary. The trick is to add the access size-1
2063 * to the address before masking the low bits. That will make the
2064 * address overflow to the next page if we cross a page boundary,
2065 * which will then force a mismatch of the TLB compare.
2067 if (a_bits < s_bits) {
2068 unsigned a_mask = (1 << a_bits) - 1;
2069 unsigned s_mask = (1 << s_bits) - 1;
2070 tcg_out32(s, ADDI | TAI(TCG_REG_R0, t, s_mask - a_mask));
2074 /* Mask the address for the requested alignment. */
2075 if (TARGET_LONG_BITS == 32) {
2076 tcg_out_rlw(s, RLWINM, TCG_REG_R0, t, 0,
2077 (32 - a_bits) & 31, 31 - TARGET_PAGE_BITS);
2078 /* Zero-extend the address for use in the final address. */
2079 tcg_out_ext32u(s, TCG_REG_R4, addrlo);
2080 addrlo = TCG_REG_R4;
2081 } else if (a_bits == 0) {
2082 tcg_out_rld(s, RLDICR, TCG_REG_R0, t, 0, 63 - TARGET_PAGE_BITS);
2084 tcg_out_rld(s, RLDICL, TCG_REG_R0, t,
2085 64 - TARGET_PAGE_BITS, TARGET_PAGE_BITS - a_bits);
2086 tcg_out_rld(s, RLDICL, TCG_REG_R0, TCG_REG_R0, TARGET_PAGE_BITS, 0);
2090 if (TCG_TARGET_REG_BITS < TARGET_LONG_BITS) {
2091 tcg_out_cmp(s, TCG_COND_EQ, TCG_REG_R0, TCG_REG_TMP1,
2092 0, 7, TCG_TYPE_I32);
2093 tcg_out_cmp(s, TCG_COND_EQ, addrhi, TCG_REG_R4, 0, 6, TCG_TYPE_I32);
2094 tcg_out32(s, CRAND | BT(7, CR_EQ) | BA(6, CR_EQ) | BB(7, CR_EQ));
2096 tcg_out_cmp(s, TCG_COND_EQ, TCG_REG_R0, TCG_REG_TMP1,
2103 /* Record the context of a call to the out of line helper code for the slow
2104 path for a load or store, so that we can later generate the correct
2106 static void add_qemu_ldst_label(TCGContext *s, bool is_ld, TCGMemOpIdx oi,
2107 TCGReg datalo_reg, TCGReg datahi_reg,
2108 TCGReg addrlo_reg, TCGReg addrhi_reg,
2109 tcg_insn_unit *raddr, tcg_insn_unit *lptr)
2111 TCGLabelQemuLdst *label = new_ldst_label(s);
2113 label->is_ld = is_ld;
2115 label->datalo_reg = datalo_reg;
2116 label->datahi_reg = datahi_reg;
2117 label->addrlo_reg = addrlo_reg;
2118 label->addrhi_reg = addrhi_reg;
2119 label->raddr = tcg_splitwx_to_rx(raddr);
2120 label->label_ptr[0] = lptr;
2123 static bool tcg_out_qemu_ld_slow_path(TCGContext *s, TCGLabelQemuLdst *lb)
2125 TCGMemOpIdx oi = lb->oi;
2126 MemOp opc = get_memop(oi);
2127 TCGReg hi, lo, arg = TCG_REG_R3;
2129 if (!reloc_pc14(lb->label_ptr[0], tcg_splitwx_to_rx(s->code_ptr))) {
2133 tcg_out_mov(s, TCG_TYPE_PTR, arg++, TCG_AREG0);
2135 lo = lb->addrlo_reg;
2136 hi = lb->addrhi_reg;
2137 if (TCG_TARGET_REG_BITS < TARGET_LONG_BITS) {
2138 #ifdef TCG_TARGET_CALL_ALIGN_ARGS
2141 tcg_out_mov(s, TCG_TYPE_I32, arg++, hi);
2142 tcg_out_mov(s, TCG_TYPE_I32, arg++, lo);
2144 /* If the address needed to be zero-extended, we'll have already
2145 placed it in R4. The only remaining case is 64-bit guest. */
2146 tcg_out_mov(s, TCG_TYPE_TL, arg++, lo);
2149 tcg_out_movi(s, TCG_TYPE_I32, arg++, oi);
2150 tcg_out32(s, MFSPR | RT(arg) | LR);
2152 tcg_out_call(s, qemu_ld_helpers[opc & (MO_BSWAP | MO_SIZE)]);
2154 lo = lb->datalo_reg;
2155 hi = lb->datahi_reg;
2156 if (TCG_TARGET_REG_BITS == 32 && (opc & MO_SIZE) == MO_64) {
2157 tcg_out_mov(s, TCG_TYPE_I32, lo, TCG_REG_R4);
2158 tcg_out_mov(s, TCG_TYPE_I32, hi, TCG_REG_R3);
2159 } else if (opc & MO_SIGN) {
2160 uint32_t insn = qemu_exts_opc[opc & MO_SIZE];
2161 tcg_out32(s, insn | RA(lo) | RS(TCG_REG_R3));
2163 tcg_out_mov(s, TCG_TYPE_REG, lo, TCG_REG_R3);
2166 tcg_out_b(s, 0, lb->raddr);
2170 static bool tcg_out_qemu_st_slow_path(TCGContext *s, TCGLabelQemuLdst *lb)
2172 TCGMemOpIdx oi = lb->oi;
2173 MemOp opc = get_memop(oi);
2174 MemOp s_bits = opc & MO_SIZE;
2175 TCGReg hi, lo, arg = TCG_REG_R3;
2177 if (!reloc_pc14(lb->label_ptr[0], tcg_splitwx_to_rx(s->code_ptr))) {
2181 tcg_out_mov(s, TCG_TYPE_PTR, arg++, TCG_AREG0);
2183 lo = lb->addrlo_reg;
2184 hi = lb->addrhi_reg;
2185 if (TCG_TARGET_REG_BITS < TARGET_LONG_BITS) {
2186 #ifdef TCG_TARGET_CALL_ALIGN_ARGS
2189 tcg_out_mov(s, TCG_TYPE_I32, arg++, hi);
2190 tcg_out_mov(s, TCG_TYPE_I32, arg++, lo);
2192 /* If the address needed to be zero-extended, we'll have already
2193 placed it in R4. The only remaining case is 64-bit guest. */
2194 tcg_out_mov(s, TCG_TYPE_TL, arg++, lo);
2197 lo = lb->datalo_reg;
2198 hi = lb->datahi_reg;
2199 if (TCG_TARGET_REG_BITS == 32) {
2202 #ifdef TCG_TARGET_CALL_ALIGN_ARGS
2205 tcg_out_mov(s, TCG_TYPE_I32, arg++, hi);
2208 tcg_out_mov(s, TCG_TYPE_I32, arg++, lo);
2211 tcg_out_rlw(s, RLWINM, arg++, lo, 0, 32 - (8 << s_bits), 31);
2215 if (s_bits == MO_64) {
2216 tcg_out_mov(s, TCG_TYPE_I64, arg++, lo);
2218 tcg_out_rld(s, RLDICL, arg++, lo, 0, 64 - (8 << s_bits));
2222 tcg_out_movi(s, TCG_TYPE_I32, arg++, oi);
2223 tcg_out32(s, MFSPR | RT(arg) | LR);
2225 tcg_out_call(s, qemu_st_helpers[opc & (MO_BSWAP | MO_SIZE)]);
2227 tcg_out_b(s, 0, lb->raddr);
2230 #endif /* SOFTMMU */
2232 static void tcg_out_qemu_ld(TCGContext *s, const TCGArg *args, bool is_64)
2234 TCGReg datalo, datahi, addrlo, rbase;
2235 TCGReg addrhi __attribute__((unused));
2238 #ifdef CONFIG_SOFTMMU
2240 tcg_insn_unit *label_ptr;
2244 datahi = (TCG_TARGET_REG_BITS == 32 && is_64 ? *args++ : 0);
2246 addrhi = (TCG_TARGET_REG_BITS < TARGET_LONG_BITS ? *args++ : 0);
2248 opc = get_memop(oi);
2249 s_bits = opc & MO_SIZE;
2251 #ifdef CONFIG_SOFTMMU
2252 mem_index = get_mmuidx(oi);
2253 addrlo = tcg_out_tlb_read(s, opc, addrlo, addrhi, mem_index, true);
2255 /* Load a pointer into the current opcode w/conditional branch-link. */
2256 label_ptr = s->code_ptr;
2257 tcg_out32(s, BC | BI(7, CR_EQ) | BO_COND_FALSE | LK);
2260 #else /* !CONFIG_SOFTMMU */
2261 rbase = guest_base ? TCG_GUEST_BASE_REG : 0;
2262 if (TCG_TARGET_REG_BITS > TARGET_LONG_BITS) {
2263 tcg_out_ext32u(s, TCG_REG_TMP1, addrlo);
2264 addrlo = TCG_REG_TMP1;
2268 if (TCG_TARGET_REG_BITS == 32 && s_bits == MO_64) {
2269 if (opc & MO_BSWAP) {
2270 tcg_out32(s, ADDI | TAI(TCG_REG_R0, addrlo, 4));
2271 tcg_out32(s, LWBRX | TAB(datalo, rbase, addrlo));
2272 tcg_out32(s, LWBRX | TAB(datahi, rbase, TCG_REG_R0));
2273 } else if (rbase != 0) {
2274 tcg_out32(s, ADDI | TAI(TCG_REG_R0, addrlo, 4));
2275 tcg_out32(s, LWZX | TAB(datahi, rbase, addrlo));
2276 tcg_out32(s, LWZX | TAB(datalo, rbase, TCG_REG_R0));
2277 } else if (addrlo == datahi) {
2278 tcg_out32(s, LWZ | TAI(datalo, addrlo, 4));
2279 tcg_out32(s, LWZ | TAI(datahi, addrlo, 0));
2281 tcg_out32(s, LWZ | TAI(datahi, addrlo, 0));
2282 tcg_out32(s, LWZ | TAI(datalo, addrlo, 4));
2285 uint32_t insn = qemu_ldx_opc[opc & (MO_BSWAP | MO_SSIZE)];
2286 if (!have_isa_2_06 && insn == LDBRX) {
2287 tcg_out32(s, ADDI | TAI(TCG_REG_R0, addrlo, 4));
2288 tcg_out32(s, LWBRX | TAB(datalo, rbase, addrlo));
2289 tcg_out32(s, LWBRX | TAB(TCG_REG_R0, rbase, TCG_REG_R0));
2290 tcg_out_rld(s, RLDIMI, datalo, TCG_REG_R0, 32, 0);
2292 tcg_out32(s, insn | TAB(datalo, rbase, addrlo));
2294 insn = qemu_ldx_opc[opc & (MO_SIZE | MO_BSWAP)];
2295 tcg_out32(s, insn | TAB(datalo, rbase, addrlo));
2296 insn = qemu_exts_opc[s_bits];
2297 tcg_out32(s, insn | RA(datalo) | RS(datalo));
2301 #ifdef CONFIG_SOFTMMU
2302 add_qemu_ldst_label(s, true, oi, datalo, datahi, addrlo, addrhi,
2303 s->code_ptr, label_ptr);
2307 static void tcg_out_qemu_st(TCGContext *s, const TCGArg *args, bool is_64)
2309 TCGReg datalo, datahi, addrlo, rbase;
2310 TCGReg addrhi __attribute__((unused));
2313 #ifdef CONFIG_SOFTMMU
2315 tcg_insn_unit *label_ptr;
2319 datahi = (TCG_TARGET_REG_BITS == 32 && is_64 ? *args++ : 0);
2321 addrhi = (TCG_TARGET_REG_BITS < TARGET_LONG_BITS ? *args++ : 0);
2323 opc = get_memop(oi);
2324 s_bits = opc & MO_SIZE;
2326 #ifdef CONFIG_SOFTMMU
2327 mem_index = get_mmuidx(oi);
2328 addrlo = tcg_out_tlb_read(s, opc, addrlo, addrhi, mem_index, false);
2330 /* Load a pointer into the current opcode w/conditional branch-link. */
2331 label_ptr = s->code_ptr;
2332 tcg_out32(s, BC | BI(7, CR_EQ) | BO_COND_FALSE | LK);
2335 #else /* !CONFIG_SOFTMMU */
2336 rbase = guest_base ? TCG_GUEST_BASE_REG : 0;
2337 if (TCG_TARGET_REG_BITS > TARGET_LONG_BITS) {
2338 tcg_out_ext32u(s, TCG_REG_TMP1, addrlo);
2339 addrlo = TCG_REG_TMP1;
2343 if (TCG_TARGET_REG_BITS == 32 && s_bits == MO_64) {
2344 if (opc & MO_BSWAP) {
2345 tcg_out32(s, ADDI | TAI(TCG_REG_R0, addrlo, 4));
2346 tcg_out32(s, STWBRX | SAB(datalo, rbase, addrlo));
2347 tcg_out32(s, STWBRX | SAB(datahi, rbase, TCG_REG_R0));
2348 } else if (rbase != 0) {
2349 tcg_out32(s, ADDI | TAI(TCG_REG_R0, addrlo, 4));
2350 tcg_out32(s, STWX | SAB(datahi, rbase, addrlo));
2351 tcg_out32(s, STWX | SAB(datalo, rbase, TCG_REG_R0));
2353 tcg_out32(s, STW | TAI(datahi, addrlo, 0));
2354 tcg_out32(s, STW | TAI(datalo, addrlo, 4));
2357 uint32_t insn = qemu_stx_opc[opc & (MO_BSWAP | MO_SIZE)];
2358 if (!have_isa_2_06 && insn == STDBRX) {
2359 tcg_out32(s, STWBRX | SAB(datalo, rbase, addrlo));
2360 tcg_out32(s, ADDI | TAI(TCG_REG_TMP1, addrlo, 4));
2361 tcg_out_shri64(s, TCG_REG_R0, datalo, 32);
2362 tcg_out32(s, STWBRX | SAB(TCG_REG_R0, rbase, TCG_REG_TMP1));
2364 tcg_out32(s, insn | SAB(datalo, rbase, addrlo));
2368 #ifdef CONFIG_SOFTMMU
2369 add_qemu_ldst_label(s, false, oi, datalo, datahi, addrlo, addrhi,
2370 s->code_ptr, label_ptr);
2374 static void tcg_out_nop_fill(tcg_insn_unit *p, int count)
2377 for (i = 0; i < count; ++i) {
2382 /* Parameters for function call generation, used in tcg.c. */
2383 #define TCG_TARGET_STACK_ALIGN 16
2384 #define TCG_TARGET_EXTEND_ARGS 1
2387 # define LINK_AREA_SIZE (6 * SZR)
2388 # define LR_OFFSET (1 * SZR)
2389 # define TCG_TARGET_CALL_STACK_OFFSET (LINK_AREA_SIZE + 8 * SZR)
2390 #elif defined(_CALL_DARWIN)
2391 # define LINK_AREA_SIZE (6 * SZR)
2392 # define LR_OFFSET (2 * SZR)
2393 #elif TCG_TARGET_REG_BITS == 64
2394 # if defined(_CALL_ELF) && _CALL_ELF == 2
2395 # define LINK_AREA_SIZE (4 * SZR)
2396 # define LR_OFFSET (1 * SZR)
2398 #else /* TCG_TARGET_REG_BITS == 32 */
2399 # if defined(_CALL_SYSV)
2400 # define LINK_AREA_SIZE (2 * SZR)
2401 # define LR_OFFSET (1 * SZR)
2405 # error "Unhandled abi"
2407 #ifndef TCG_TARGET_CALL_STACK_OFFSET
2408 # define TCG_TARGET_CALL_STACK_OFFSET LINK_AREA_SIZE
2411 #define CPU_TEMP_BUF_SIZE (CPU_TEMP_BUF_NLONGS * (int)sizeof(long))
2412 #define REG_SAVE_SIZE ((int)ARRAY_SIZE(tcg_target_callee_save_regs) * SZR)
2414 #define FRAME_SIZE ((TCG_TARGET_CALL_STACK_OFFSET \
2415 + TCG_STATIC_CALL_ARGS_SIZE \
2416 + CPU_TEMP_BUF_SIZE \
2418 + TCG_TARGET_STACK_ALIGN - 1) \
2419 & -TCG_TARGET_STACK_ALIGN)
2421 #define REG_SAVE_BOT (FRAME_SIZE - REG_SAVE_SIZE)
2423 static void tcg_target_qemu_prologue(TCGContext *s)
2428 const void **desc = (const void **)s->code_ptr;
2429 desc[0] = tcg_splitwx_to_rx(desc + 2); /* entry point */
2430 desc[1] = 0; /* environment pointer */
2431 s->code_ptr = (void *)(desc + 2); /* skip over descriptor */
2434 tcg_set_frame(s, TCG_REG_CALL_STACK, REG_SAVE_BOT - CPU_TEMP_BUF_SIZE,
2438 tcg_out32(s, MFSPR | RT(TCG_REG_R0) | LR);
2439 tcg_out32(s, (SZR == 8 ? STDU : STWU)
2440 | SAI(TCG_REG_R1, TCG_REG_R1, -FRAME_SIZE));
2442 for (i = 0; i < ARRAY_SIZE(tcg_target_callee_save_regs); ++i) {
2443 tcg_out_st(s, TCG_TYPE_REG, tcg_target_callee_save_regs[i],
2444 TCG_REG_R1, REG_SAVE_BOT + i * SZR);
2446 tcg_out_st(s, TCG_TYPE_PTR, TCG_REG_R0, TCG_REG_R1, FRAME_SIZE+LR_OFFSET);
2448 #ifndef CONFIG_SOFTMMU
2450 tcg_out_movi_int(s, TCG_TYPE_PTR, TCG_GUEST_BASE_REG, guest_base, true);
2451 tcg_regset_set_reg(s->reserved_regs, TCG_GUEST_BASE_REG);
2455 tcg_out_mov(s, TCG_TYPE_PTR, TCG_AREG0, tcg_target_call_iarg_regs[0]);
2456 tcg_out32(s, MTSPR | RS(tcg_target_call_iarg_regs[1]) | CTR);
2458 tcg_out_mov(s, TCG_TYPE_PTR, TCG_REG_TB, tcg_target_call_iarg_regs[1]);
2460 tcg_out32(s, BCCTR | BO_ALWAYS);
2463 tcg_code_gen_epilogue = tcg_splitwx_to_rx(s->code_ptr);
2465 tcg_out_ld(s, TCG_TYPE_PTR, TCG_REG_R0, TCG_REG_R1, FRAME_SIZE+LR_OFFSET);
2466 for (i = 0; i < ARRAY_SIZE(tcg_target_callee_save_regs); ++i) {
2467 tcg_out_ld(s, TCG_TYPE_REG, tcg_target_callee_save_regs[i],
2468 TCG_REG_R1, REG_SAVE_BOT + i * SZR);
2470 tcg_out32(s, MTSPR | RS(TCG_REG_R0) | LR);
2471 tcg_out32(s, ADDI | TAI(TCG_REG_R1, TCG_REG_R1, FRAME_SIZE));
2472 tcg_out32(s, BCLR | BO_ALWAYS);
2475 static void tcg_out_op(TCGContext *s, TCGOpcode opc,
2476 const TCGArg args[TCG_MAX_OP_ARGS],
2477 const int const_args[TCG_MAX_OP_ARGS])
2482 case INDEX_op_exit_tb:
2483 tcg_out_movi(s, TCG_TYPE_PTR, TCG_REG_R3, args[0]);
2484 tcg_out_b(s, 0, tcg_code_gen_epilogue);
2486 case INDEX_op_goto_tb:
2487 if (s->tb_jmp_insn_offset) {
2489 if (TCG_TARGET_REG_BITS == 64) {
2490 /* Ensure the next insns are 8-byte aligned. */
2491 if ((uintptr_t)s->code_ptr & 7) {
2494 s->tb_jmp_insn_offset[args[0]] = tcg_current_code_size(s);
2495 tcg_out32(s, ADDIS | TAI(TCG_REG_TB, TCG_REG_TB, 0));
2496 tcg_out32(s, ADDI | TAI(TCG_REG_TB, TCG_REG_TB, 0));
2498 s->tb_jmp_insn_offset[args[0]] = tcg_current_code_size(s);
2500 s->tb_jmp_reset_offset[args[0]] = tcg_current_code_size(s);
2504 /* Indirect jump. */
2505 tcg_debug_assert(s->tb_jmp_insn_offset == NULL);
2506 tcg_out_ld(s, TCG_TYPE_PTR, TCG_REG_TB, 0,
2507 (intptr_t)(s->tb_jmp_insn_offset + args[0]));
2509 tcg_out32(s, MTSPR | RS(TCG_REG_TB) | CTR);
2510 tcg_out32(s, BCCTR | BO_ALWAYS);
2511 set_jmp_reset_offset(s, args[0]);
2513 /* For the unlinked case, need to reset TCG_REG_TB. */
2514 tcg_out_mem_long(s, ADDI, ADD, TCG_REG_TB, TCG_REG_TB,
2515 -tcg_current_code_size(s));
2518 case INDEX_op_goto_ptr:
2519 tcg_out32(s, MTSPR | RS(args[0]) | CTR);
2521 tcg_out_mov(s, TCG_TYPE_PTR, TCG_REG_TB, args[0]);
2523 tcg_out32(s, ADDI | TAI(TCG_REG_R3, 0, 0));
2524 tcg_out32(s, BCCTR | BO_ALWAYS);
2528 TCGLabel *l = arg_label(args[0]);
2532 insn |= reloc_pc24_val(tcg_splitwx_to_rx(s->code_ptr),
2535 tcg_out_reloc(s, s->code_ptr, R_PPC_REL24, l, 0);
2540 case INDEX_op_ld8u_i32:
2541 case INDEX_op_ld8u_i64:
2542 tcg_out_mem_long(s, LBZ, LBZX, args[0], args[1], args[2]);
2544 case INDEX_op_ld8s_i32:
2545 case INDEX_op_ld8s_i64:
2546 tcg_out_mem_long(s, LBZ, LBZX, args[0], args[1], args[2]);
2547 tcg_out_ext8s(s, args[0], args[0]);
2549 case INDEX_op_ld16u_i32:
2550 case INDEX_op_ld16u_i64:
2551 tcg_out_mem_long(s, LHZ, LHZX, args[0], args[1], args[2]);
2553 case INDEX_op_ld16s_i32:
2554 case INDEX_op_ld16s_i64:
2555 tcg_out_mem_long(s, LHA, LHAX, args[0], args[1], args[2]);
2557 case INDEX_op_ld_i32:
2558 case INDEX_op_ld32u_i64:
2559 tcg_out_mem_long(s, LWZ, LWZX, args[0], args[1], args[2]);
2561 case INDEX_op_ld32s_i64:
2562 tcg_out_mem_long(s, LWA, LWAX, args[0], args[1], args[2]);
2564 case INDEX_op_ld_i64:
2565 tcg_out_mem_long(s, LD, LDX, args[0], args[1], args[2]);
2567 case INDEX_op_st8_i32:
2568 case INDEX_op_st8_i64:
2569 tcg_out_mem_long(s, STB, STBX, args[0], args[1], args[2]);
2571 case INDEX_op_st16_i32:
2572 case INDEX_op_st16_i64:
2573 tcg_out_mem_long(s, STH, STHX, args[0], args[1], args[2]);
2575 case INDEX_op_st_i32:
2576 case INDEX_op_st32_i64:
2577 tcg_out_mem_long(s, STW, STWX, args[0], args[1], args[2]);
2579 case INDEX_op_st_i64:
2580 tcg_out_mem_long(s, STD, STDX, args[0], args[1], args[2]);
2583 case INDEX_op_add_i32:
2584 a0 = args[0], a1 = args[1], a2 = args[2];
2585 if (const_args[2]) {
2587 tcg_out_mem_long(s, ADDI, ADD, a0, a1, (int32_t)a2);
2589 tcg_out32(s, ADD | TAB(a0, a1, a2));
2592 case INDEX_op_sub_i32:
2593 a0 = args[0], a1 = args[1], a2 = args[2];
2594 if (const_args[1]) {
2595 if (const_args[2]) {
2596 tcg_out_movi(s, TCG_TYPE_I32, a0, a1 - a2);
2598 tcg_out32(s, SUBFIC | TAI(a0, a2, a1));
2600 } else if (const_args[2]) {
2604 tcg_out32(s, SUBF | TAB(a0, a2, a1));
2608 case INDEX_op_and_i32:
2609 a0 = args[0], a1 = args[1], a2 = args[2];
2610 if (const_args[2]) {
2611 tcg_out_andi32(s, a0, a1, a2);
2613 tcg_out32(s, AND | SAB(a1, a0, a2));
2616 case INDEX_op_and_i64:
2617 a0 = args[0], a1 = args[1], a2 = args[2];
2618 if (const_args[2]) {
2619 tcg_out_andi64(s, a0, a1, a2);
2621 tcg_out32(s, AND | SAB(a1, a0, a2));
2624 case INDEX_op_or_i64:
2625 case INDEX_op_or_i32:
2626 a0 = args[0], a1 = args[1], a2 = args[2];
2627 if (const_args[2]) {
2628 tcg_out_ori32(s, a0, a1, a2);
2630 tcg_out32(s, OR | SAB(a1, a0, a2));
2633 case INDEX_op_xor_i64:
2634 case INDEX_op_xor_i32:
2635 a0 = args[0], a1 = args[1], a2 = args[2];
2636 if (const_args[2]) {
2637 tcg_out_xori32(s, a0, a1, a2);
2639 tcg_out32(s, XOR | SAB(a1, a0, a2));
2642 case INDEX_op_andc_i32:
2643 a0 = args[0], a1 = args[1], a2 = args[2];
2644 if (const_args[2]) {
2645 tcg_out_andi32(s, a0, a1, ~a2);
2647 tcg_out32(s, ANDC | SAB(a1, a0, a2));
2650 case INDEX_op_andc_i64:
2651 a0 = args[0], a1 = args[1], a2 = args[2];
2652 if (const_args[2]) {
2653 tcg_out_andi64(s, a0, a1, ~a2);
2655 tcg_out32(s, ANDC | SAB(a1, a0, a2));
2658 case INDEX_op_orc_i32:
2659 if (const_args[2]) {
2660 tcg_out_ori32(s, args[0], args[1], ~args[2]);
2664 case INDEX_op_orc_i64:
2665 tcg_out32(s, ORC | SAB(args[1], args[0], args[2]));
2667 case INDEX_op_eqv_i32:
2668 if (const_args[2]) {
2669 tcg_out_xori32(s, args[0], args[1], ~args[2]);
2673 case INDEX_op_eqv_i64:
2674 tcg_out32(s, EQV | SAB(args[1], args[0], args[2]));
2676 case INDEX_op_nand_i32:
2677 case INDEX_op_nand_i64:
2678 tcg_out32(s, NAND | SAB(args[1], args[0], args[2]));
2680 case INDEX_op_nor_i32:
2681 case INDEX_op_nor_i64:
2682 tcg_out32(s, NOR | SAB(args[1], args[0], args[2]));
2685 case INDEX_op_clz_i32:
2686 tcg_out_cntxz(s, TCG_TYPE_I32, CNTLZW, args[0], args[1],
2687 args[2], const_args[2]);
2689 case INDEX_op_ctz_i32:
2690 tcg_out_cntxz(s, TCG_TYPE_I32, CNTTZW, args[0], args[1],
2691 args[2], const_args[2]);
2693 case INDEX_op_ctpop_i32:
2694 tcg_out32(s, CNTPOPW | SAB(args[1], args[0], 0));
2697 case INDEX_op_clz_i64:
2698 tcg_out_cntxz(s, TCG_TYPE_I64, CNTLZD, args[0], args[1],
2699 args[2], const_args[2]);
2701 case INDEX_op_ctz_i64:
2702 tcg_out_cntxz(s, TCG_TYPE_I64, CNTTZD, args[0], args[1],
2703 args[2], const_args[2]);
2705 case INDEX_op_ctpop_i64:
2706 tcg_out32(s, CNTPOPD | SAB(args[1], args[0], 0));
2709 case INDEX_op_mul_i32:
2710 a0 = args[0], a1 = args[1], a2 = args[2];
2711 if (const_args[2]) {
2712 tcg_out32(s, MULLI | TAI(a0, a1, a2));
2714 tcg_out32(s, MULLW | TAB(a0, a1, a2));
2718 case INDEX_op_div_i32:
2719 tcg_out32(s, DIVW | TAB(args[0], args[1], args[2]));
2722 case INDEX_op_divu_i32:
2723 tcg_out32(s, DIVWU | TAB(args[0], args[1], args[2]));
2726 case INDEX_op_shl_i32:
2727 if (const_args[2]) {
2728 /* Limit immediate shift count lest we create an illegal insn. */
2729 tcg_out_shli32(s, args[0], args[1], args[2] & 31);
2731 tcg_out32(s, SLW | SAB(args[1], args[0], args[2]));
2734 case INDEX_op_shr_i32:
2735 if (const_args[2]) {
2736 /* Limit immediate shift count lest we create an illegal insn. */
2737 tcg_out_shri32(s, args[0], args[1], args[2] & 31);
2739 tcg_out32(s, SRW | SAB(args[1], args[0], args[2]));
2742 case INDEX_op_sar_i32:
2743 if (const_args[2]) {
2744 tcg_out_sari32(s, args[0], args[1], args[2]);
2746 tcg_out32(s, SRAW | SAB(args[1], args[0], args[2]));
2749 case INDEX_op_rotl_i32:
2750 if (const_args[2]) {
2751 tcg_out_rlw(s, RLWINM, args[0], args[1], args[2], 0, 31);
2753 tcg_out32(s, RLWNM | SAB(args[1], args[0], args[2])
2757 case INDEX_op_rotr_i32:
2758 if (const_args[2]) {
2759 tcg_out_rlw(s, RLWINM, args[0], args[1], 32 - args[2], 0, 31);
2761 tcg_out32(s, SUBFIC | TAI(TCG_REG_R0, args[2], 32));
2762 tcg_out32(s, RLWNM | SAB(args[1], args[0], TCG_REG_R0)
2767 case INDEX_op_brcond_i32:
2768 tcg_out_brcond(s, args[2], args[0], args[1], const_args[1],
2769 arg_label(args[3]), TCG_TYPE_I32);
2771 case INDEX_op_brcond_i64:
2772 tcg_out_brcond(s, args[2], args[0], args[1], const_args[1],
2773 arg_label(args[3]), TCG_TYPE_I64);
2775 case INDEX_op_brcond2_i32:
2776 tcg_out_brcond2(s, args, const_args);
2779 case INDEX_op_neg_i32:
2780 case INDEX_op_neg_i64:
2781 tcg_out32(s, NEG | RT(args[0]) | RA(args[1]));
2784 case INDEX_op_not_i32:
2785 case INDEX_op_not_i64:
2786 tcg_out32(s, NOR | SAB(args[1], args[0], args[1]));
2789 case INDEX_op_add_i64:
2790 a0 = args[0], a1 = args[1], a2 = args[2];
2791 if (const_args[2]) {
2793 tcg_out_mem_long(s, ADDI, ADD, a0, a1, a2);
2795 tcg_out32(s, ADD | TAB(a0, a1, a2));
2798 case INDEX_op_sub_i64:
2799 a0 = args[0], a1 = args[1], a2 = args[2];
2800 if (const_args[1]) {
2801 if (const_args[2]) {
2802 tcg_out_movi(s, TCG_TYPE_I64, a0, a1 - a2);
2804 tcg_out32(s, SUBFIC | TAI(a0, a2, a1));
2806 } else if (const_args[2]) {
2810 tcg_out32(s, SUBF | TAB(a0, a2, a1));
2814 case INDEX_op_shl_i64:
2815 if (const_args[2]) {
2816 /* Limit immediate shift count lest we create an illegal insn. */
2817 tcg_out_shli64(s, args[0], args[1], args[2] & 63);
2819 tcg_out32(s, SLD | SAB(args[1], args[0], args[2]));
2822 case INDEX_op_shr_i64:
2823 if (const_args[2]) {
2824 /* Limit immediate shift count lest we create an illegal insn. */
2825 tcg_out_shri64(s, args[0], args[1], args[2] & 63);
2827 tcg_out32(s, SRD | SAB(args[1], args[0], args[2]));
2830 case INDEX_op_sar_i64:
2831 if (const_args[2]) {
2832 tcg_out_sari64(s, args[0], args[1], args[2]);
2834 tcg_out32(s, SRAD | SAB(args[1], args[0], args[2]));
2837 case INDEX_op_rotl_i64:
2838 if (const_args[2]) {
2839 tcg_out_rld(s, RLDICL, args[0], args[1], args[2], 0);
2841 tcg_out32(s, RLDCL | SAB(args[1], args[0], args[2]) | MB64(0));
2844 case INDEX_op_rotr_i64:
2845 if (const_args[2]) {
2846 tcg_out_rld(s, RLDICL, args[0], args[1], 64 - args[2], 0);
2848 tcg_out32(s, SUBFIC | TAI(TCG_REG_R0, args[2], 64));
2849 tcg_out32(s, RLDCL | SAB(args[1], args[0], TCG_REG_R0) | MB64(0));
2853 case INDEX_op_mul_i64:
2854 a0 = args[0], a1 = args[1], a2 = args[2];
2855 if (const_args[2]) {
2856 tcg_out32(s, MULLI | TAI(a0, a1, a2));
2858 tcg_out32(s, MULLD | TAB(a0, a1, a2));
2861 case INDEX_op_div_i64:
2862 tcg_out32(s, DIVD | TAB(args[0], args[1], args[2]));
2864 case INDEX_op_divu_i64:
2865 tcg_out32(s, DIVDU | TAB(args[0], args[1], args[2]));
2868 case INDEX_op_qemu_ld_i32:
2869 tcg_out_qemu_ld(s, args, false);
2871 case INDEX_op_qemu_ld_i64:
2872 tcg_out_qemu_ld(s, args, true);
2874 case INDEX_op_qemu_st_i32:
2875 tcg_out_qemu_st(s, args, false);
2877 case INDEX_op_qemu_st_i64:
2878 tcg_out_qemu_st(s, args, true);
2881 case INDEX_op_ext8s_i32:
2882 case INDEX_op_ext8s_i64:
2883 tcg_out_ext8s(s, args[0], args[1]);
2885 case INDEX_op_ext16s_i32:
2886 case INDEX_op_ext16s_i64:
2887 tcg_out_ext16s(s, args[0], args[1]);
2889 case INDEX_op_ext_i32_i64:
2890 case INDEX_op_ext32s_i64:
2891 tcg_out_ext32s(s, args[0], args[1]);
2893 case INDEX_op_extu_i32_i64:
2894 tcg_out_ext32u(s, args[0], args[1]);
2897 case INDEX_op_setcond_i32:
2898 tcg_out_setcond(s, TCG_TYPE_I32, args[3], args[0], args[1], args[2],
2901 case INDEX_op_setcond_i64:
2902 tcg_out_setcond(s, TCG_TYPE_I64, args[3], args[0], args[1], args[2],
2905 case INDEX_op_setcond2_i32:
2906 tcg_out_setcond2(s, args, const_args);
2909 case INDEX_op_bswap16_i32:
2910 case INDEX_op_bswap16_i64:
2911 tcg_out_bswap16(s, args[0], args[1], args[2]);
2913 case INDEX_op_bswap32_i32:
2914 tcg_out_bswap32(s, args[0], args[1], 0);
2916 case INDEX_op_bswap32_i64:
2917 tcg_out_bswap32(s, args[0], args[1], args[2]);
2919 case INDEX_op_bswap64_i64:
2920 tcg_out_bswap64(s, args[0], args[1]);
2923 case INDEX_op_deposit_i32:
2924 if (const_args[2]) {
2925 uint32_t mask = ((2u << (args[4] - 1)) - 1) << args[3];
2926 tcg_out_andi32(s, args[0], args[0], ~mask);
2928 tcg_out_rlw(s, RLWIMI, args[0], args[2], args[3],
2929 32 - args[3] - args[4], 31 - args[3]);
2932 case INDEX_op_deposit_i64:
2933 if (const_args[2]) {
2934 uint64_t mask = ((2ull << (args[4] - 1)) - 1) << args[3];
2935 tcg_out_andi64(s, args[0], args[0], ~mask);
2937 tcg_out_rld(s, RLDIMI, args[0], args[2], args[3],
2938 64 - args[3] - args[4]);
2942 case INDEX_op_extract_i32:
2943 tcg_out_rlw(s, RLWINM, args[0], args[1],
2944 32 - args[2], 32 - args[3], 31);
2946 case INDEX_op_extract_i64:
2947 tcg_out_rld(s, RLDICL, args[0], args[1], 64 - args[2], 64 - args[3]);
2950 case INDEX_op_movcond_i32:
2951 tcg_out_movcond(s, TCG_TYPE_I32, args[5], args[0], args[1], args[2],
2952 args[3], args[4], const_args[2]);
2954 case INDEX_op_movcond_i64:
2955 tcg_out_movcond(s, TCG_TYPE_I64, args[5], args[0], args[1], args[2],
2956 args[3], args[4], const_args[2]);
2959 #if TCG_TARGET_REG_BITS == 64
2960 case INDEX_op_add2_i64:
2962 case INDEX_op_add2_i32:
2964 /* Note that the CA bit is defined based on the word size of the
2965 environment. So in 64-bit mode it's always carry-out of bit 63.
2966 The fallback code using deposit works just as well for 32-bit. */
2967 a0 = args[0], a1 = args[1];
2968 if (a0 == args[3] || (!const_args[5] && a0 == args[5])) {
2971 if (const_args[4]) {
2972 tcg_out32(s, ADDIC | TAI(a0, args[2], args[4]));
2974 tcg_out32(s, ADDC | TAB(a0, args[2], args[4]));
2976 if (const_args[5]) {
2977 tcg_out32(s, (args[5] ? ADDME : ADDZE) | RT(a1) | RA(args[3]));
2979 tcg_out32(s, ADDE | TAB(a1, args[3], args[5]));
2981 if (a0 != args[0]) {
2982 tcg_out_mov(s, TCG_TYPE_REG, args[0], a0);
2986 #if TCG_TARGET_REG_BITS == 64
2987 case INDEX_op_sub2_i64:
2989 case INDEX_op_sub2_i32:
2991 a0 = args[0], a1 = args[1];
2992 if (a0 == args[5] || (!const_args[3] && a0 == args[3])) {
2995 if (const_args[2]) {
2996 tcg_out32(s, SUBFIC | TAI(a0, args[4], args[2]));
2998 tcg_out32(s, SUBFC | TAB(a0, args[4], args[2]));
3000 if (const_args[3]) {
3001 tcg_out32(s, (args[3] ? SUBFME : SUBFZE) | RT(a1) | RA(args[5]));
3003 tcg_out32(s, SUBFE | TAB(a1, args[5], args[3]));
3005 if (a0 != args[0]) {
3006 tcg_out_mov(s, TCG_TYPE_REG, args[0], a0);
3010 case INDEX_op_muluh_i32:
3011 tcg_out32(s, MULHWU | TAB(args[0], args[1], args[2]));
3013 case INDEX_op_mulsh_i32:
3014 tcg_out32(s, MULHW | TAB(args[0], args[1], args[2]));
3016 case INDEX_op_muluh_i64:
3017 tcg_out32(s, MULHDU | TAB(args[0], args[1], args[2]));
3019 case INDEX_op_mulsh_i64:
3020 tcg_out32(s, MULHD | TAB(args[0], args[1], args[2]));
3024 tcg_out_mb(s, args[0]);
3027 case INDEX_op_mov_i32: /* Always emitted via tcg_out_mov. */
3028 case INDEX_op_mov_i64:
3029 case INDEX_op_call: /* Always emitted via tcg_out_call. */
3035 int tcg_can_emit_vec_op(TCGOpcode opc, TCGType type, unsigned vece)
3038 case INDEX_op_and_vec:
3039 case INDEX_op_or_vec:
3040 case INDEX_op_xor_vec:
3041 case INDEX_op_andc_vec:
3042 case INDEX_op_not_vec:
3044 case INDEX_op_orc_vec:
3045 return have_isa_2_07;
3046 case INDEX_op_add_vec:
3047 case INDEX_op_sub_vec:
3048 case INDEX_op_smax_vec:
3049 case INDEX_op_smin_vec:
3050 case INDEX_op_umax_vec:
3051 case INDEX_op_umin_vec:
3052 case INDEX_op_shlv_vec:
3053 case INDEX_op_shrv_vec:
3054 case INDEX_op_sarv_vec:
3055 case INDEX_op_rotlv_vec:
3056 return vece <= MO_32 || have_isa_2_07;
3057 case INDEX_op_ssadd_vec:
3058 case INDEX_op_sssub_vec:
3059 case INDEX_op_usadd_vec:
3060 case INDEX_op_ussub_vec:
3061 return vece <= MO_32;
3062 case INDEX_op_cmp_vec:
3063 case INDEX_op_shli_vec:
3064 case INDEX_op_shri_vec:
3065 case INDEX_op_sari_vec:
3066 case INDEX_op_rotli_vec:
3067 return vece <= MO_32 || have_isa_2_07 ? -1 : 0;
3068 case INDEX_op_neg_vec:
3069 return vece >= MO_32 && have_isa_3_00;
3070 case INDEX_op_mul_vec:
3076 return have_isa_2_07 ? 1 : -1;
3078 return have_isa_3_10;
3081 case INDEX_op_bitsel_vec:
3083 case INDEX_op_rotrv_vec:
3090 static bool tcg_out_dup_vec(TCGContext *s, TCGType type, unsigned vece,
3091 TCGReg dst, TCGReg src)
3093 tcg_debug_assert(dst >= TCG_REG_V0);
3095 /* Splat from integer reg allowed via constraints for v3.00. */
3096 if (src < TCG_REG_V0) {
3097 tcg_debug_assert(have_isa_3_00);
3100 tcg_out32(s, MTVSRDD | VRT(dst) | RA(src) | RB(src));
3103 tcg_out32(s, MTVSRWS | VRT(dst) | RA(src));
3106 /* Fail, so that we fall back on either dupm or mov+dup. */
3112 * Recall we use (or emulate) VSX integer loads, so the integer is
3113 * right justified within the left (zero-index) double-word.
3117 tcg_out32(s, VSPLTB | VRT(dst) | VRB(src) | (7 << 16));
3120 tcg_out32(s, VSPLTH | VRT(dst) | VRB(src) | (3 << 16));
3123 tcg_out32(s, VSPLTW | VRT(dst) | VRB(src) | (1 << 16));
3127 tcg_out32(s, XXPERMDI | VRT(dst) | VRA(src) | VRB(src));
3130 tcg_out_vsldoi(s, TCG_VEC_TMP1, src, src, 8);
3131 tcg_out_vsldoi(s, dst, TCG_VEC_TMP1, src, 8);
3134 g_assert_not_reached();
3139 static bool tcg_out_dupm_vec(TCGContext *s, TCGType type, unsigned vece,
3140 TCGReg out, TCGReg base, intptr_t offset)
3144 tcg_debug_assert(out >= TCG_REG_V0);
3147 if (have_isa_3_00) {
3148 tcg_out_mem_long(s, LXV, LVX, out, base, offset & -16);
3150 tcg_out_mem_long(s, 0, LVEBX, out, base, offset);
3152 elt = extract32(offset, 0, 4);
3153 #ifndef HOST_WORDS_BIGENDIAN
3156 tcg_out32(s, VSPLTB | VRT(out) | VRB(out) | (elt << 16));
3159 tcg_debug_assert((offset & 1) == 0);
3160 if (have_isa_3_00) {
3161 tcg_out_mem_long(s, LXV | 8, LVX, out, base, offset & -16);
3163 tcg_out_mem_long(s, 0, LVEHX, out, base, offset);
3165 elt = extract32(offset, 1, 3);
3166 #ifndef HOST_WORDS_BIGENDIAN
3169 tcg_out32(s, VSPLTH | VRT(out) | VRB(out) | (elt << 16));
3172 if (have_isa_3_00) {
3173 tcg_out_mem_long(s, 0, LXVWSX, out, base, offset);
3176 tcg_debug_assert((offset & 3) == 0);
3177 tcg_out_mem_long(s, 0, LVEWX, out, base, offset);
3178 elt = extract32(offset, 2, 2);
3179 #ifndef HOST_WORDS_BIGENDIAN
3182 tcg_out32(s, VSPLTW | VRT(out) | VRB(out) | (elt << 16));
3186 tcg_out_mem_long(s, 0, LXVDSX, out, base, offset);
3189 tcg_debug_assert((offset & 7) == 0);
3190 tcg_out_mem_long(s, 0, LVX, out, base, offset & -16);
3191 tcg_out_vsldoi(s, TCG_VEC_TMP1, out, out, 8);
3192 elt = extract32(offset, 3, 1);
3193 #ifndef HOST_WORDS_BIGENDIAN
3197 tcg_out_vsldoi(s, out, out, TCG_VEC_TMP1, 8);
3199 tcg_out_vsldoi(s, out, TCG_VEC_TMP1, out, 8);
3203 g_assert_not_reached();
3208 static void tcg_out_vec_op(TCGContext *s, TCGOpcode opc,
3209 unsigned vecl, unsigned vece,
3210 const TCGArg args[TCG_MAX_OP_ARGS],
3211 const int const_args[TCG_MAX_OP_ARGS])
3213 static const uint32_t
3214 add_op[4] = { VADDUBM, VADDUHM, VADDUWM, VADDUDM },
3215 sub_op[4] = { VSUBUBM, VSUBUHM, VSUBUWM, VSUBUDM },
3216 mul_op[4] = { 0, 0, VMULUWM, VMULLD },
3217 neg_op[4] = { 0, 0, VNEGW, VNEGD },
3218 eq_op[4] = { VCMPEQUB, VCMPEQUH, VCMPEQUW, VCMPEQUD },
3219 ne_op[4] = { VCMPNEB, VCMPNEH, VCMPNEW, 0 },
3220 gts_op[4] = { VCMPGTSB, VCMPGTSH, VCMPGTSW, VCMPGTSD },
3221 gtu_op[4] = { VCMPGTUB, VCMPGTUH, VCMPGTUW, VCMPGTUD },
3222 ssadd_op[4] = { VADDSBS, VADDSHS, VADDSWS, 0 },
3223 usadd_op[4] = { VADDUBS, VADDUHS, VADDUWS, 0 },
3224 sssub_op[4] = { VSUBSBS, VSUBSHS, VSUBSWS, 0 },
3225 ussub_op[4] = { VSUBUBS, VSUBUHS, VSUBUWS, 0 },
3226 umin_op[4] = { VMINUB, VMINUH, VMINUW, VMINUD },
3227 smin_op[4] = { VMINSB, VMINSH, VMINSW, VMINSD },
3228 umax_op[4] = { VMAXUB, VMAXUH, VMAXUW, VMAXUD },
3229 smax_op[4] = { VMAXSB, VMAXSH, VMAXSW, VMAXSD },
3230 shlv_op[4] = { VSLB, VSLH, VSLW, VSLD },
3231 shrv_op[4] = { VSRB, VSRH, VSRW, VSRD },
3232 sarv_op[4] = { VSRAB, VSRAH, VSRAW, VSRAD },
3233 mrgh_op[4] = { VMRGHB, VMRGHH, VMRGHW, 0 },
3234 mrgl_op[4] = { VMRGLB, VMRGLH, VMRGLW, 0 },
3235 muleu_op[4] = { VMULEUB, VMULEUH, VMULEUW, 0 },
3236 mulou_op[4] = { VMULOUB, VMULOUH, VMULOUW, 0 },
3237 pkum_op[4] = { VPKUHUM, VPKUWUM, 0, 0 },
3238 rotl_op[4] = { VRLB, VRLH, VRLW, VRLD };
3240 TCGType type = vecl + TCG_TYPE_V64;
3241 TCGArg a0 = args[0], a1 = args[1], a2 = args[2];
3245 case INDEX_op_ld_vec:
3246 tcg_out_ld(s, type, a0, a1, a2);
3248 case INDEX_op_st_vec:
3249 tcg_out_st(s, type, a0, a1, a2);
3251 case INDEX_op_dupm_vec:
3252 tcg_out_dupm_vec(s, type, vece, a0, a1, a2);
3255 case INDEX_op_add_vec:
3256 insn = add_op[vece];
3258 case INDEX_op_sub_vec:
3259 insn = sub_op[vece];
3261 case INDEX_op_neg_vec:
3262 insn = neg_op[vece];
3266 case INDEX_op_mul_vec:
3267 insn = mul_op[vece];
3269 case INDEX_op_ssadd_vec:
3270 insn = ssadd_op[vece];
3272 case INDEX_op_sssub_vec:
3273 insn = sssub_op[vece];
3275 case INDEX_op_usadd_vec:
3276 insn = usadd_op[vece];
3278 case INDEX_op_ussub_vec:
3279 insn = ussub_op[vece];
3281 case INDEX_op_smin_vec:
3282 insn = smin_op[vece];
3284 case INDEX_op_umin_vec:
3285 insn = umin_op[vece];
3287 case INDEX_op_smax_vec:
3288 insn = smax_op[vece];
3290 case INDEX_op_umax_vec:
3291 insn = umax_op[vece];
3293 case INDEX_op_shlv_vec:
3294 insn = shlv_op[vece];
3296 case INDEX_op_shrv_vec:
3297 insn = shrv_op[vece];
3299 case INDEX_op_sarv_vec:
3300 insn = sarv_op[vece];
3302 case INDEX_op_and_vec:
3305 case INDEX_op_or_vec:
3308 case INDEX_op_xor_vec:
3311 case INDEX_op_andc_vec:
3314 case INDEX_op_not_vec:
3318 case INDEX_op_orc_vec:
3322 case INDEX_op_cmp_vec:
3331 insn = gts_op[vece];
3334 insn = gtu_op[vece];
3337 g_assert_not_reached();
3341 case INDEX_op_bitsel_vec:
3342 tcg_out32(s, XXSEL | VRT(a0) | VRC(a1) | VRB(a2) | VRA(args[3]));
3345 case INDEX_op_dup2_vec:
3346 assert(TCG_TARGET_REG_BITS == 32);
3347 /* With inputs a1 = xLxx, a2 = xHxx */
3348 tcg_out32(s, VMRGHW | VRT(a0) | VRA(a2) | VRB(a1)); /* a0 = xxHL */
3349 tcg_out_vsldoi(s, TCG_VEC_TMP1, a0, a0, 8); /* tmp = HLxx */
3350 tcg_out_vsldoi(s, a0, a0, TCG_VEC_TMP1, 8); /* a0 = HLHL */
3353 case INDEX_op_ppc_mrgh_vec:
3354 insn = mrgh_op[vece];
3356 case INDEX_op_ppc_mrgl_vec:
3357 insn = mrgl_op[vece];
3359 case INDEX_op_ppc_muleu_vec:
3360 insn = muleu_op[vece];
3362 case INDEX_op_ppc_mulou_vec:
3363 insn = mulou_op[vece];
3365 case INDEX_op_ppc_pkum_vec:
3366 insn = pkum_op[vece];
3368 case INDEX_op_rotlv_vec:
3369 insn = rotl_op[vece];
3371 case INDEX_op_ppc_msum_vec:
3372 tcg_debug_assert(vece == MO_16);
3373 tcg_out32(s, VMSUMUHM | VRT(a0) | VRA(a1) | VRB(a2) | VRC(args[3]));
3376 case INDEX_op_mov_vec: /* Always emitted via tcg_out_mov. */
3377 case INDEX_op_dup_vec: /* Always emitted via tcg_out_dup_vec. */
3379 g_assert_not_reached();
3382 tcg_debug_assert(insn != 0);
3383 tcg_out32(s, insn | VRT(a0) | VRA(a1) | VRB(a2));
3386 static void expand_vec_shi(TCGType type, unsigned vece, TCGv_vec v0,
3387 TCGv_vec v1, TCGArg imm, TCGOpcode opci)
3391 if (vece == MO_32) {
3393 * Only 5 bits are significant, and VSPLTISB can represent -16..15.
3394 * So using negative numbers gets us the 4th bit easily.
3396 imm = sextract32(imm, 0, 5);
3398 imm &= (8 << vece) - 1;
3401 /* Splat w/bytes for xxspltib when 2.07 allows MO_64. */
3402 t1 = tcg_constant_vec(type, MO_8, imm);
3403 vec_gen_3(opci, type, vece, tcgv_vec_arg(v0),
3404 tcgv_vec_arg(v1), tcgv_vec_arg(t1));
3407 static void expand_vec_cmp(TCGType type, unsigned vece, TCGv_vec v0,
3408 TCGv_vec v1, TCGv_vec v2, TCGCond cond)
3410 bool need_swap = false, need_inv = false;
3412 tcg_debug_assert(vece <= MO_32 || have_isa_2_07);
3420 if (have_isa_3_00 && vece <= MO_32) {
3434 need_swap = need_inv = true;
3437 g_assert_not_reached();
3441 cond = tcg_invert_cond(cond);
3445 t1 = v1, v1 = v2, v2 = t1;
3446 cond = tcg_swap_cond(cond);
3449 vec_gen_4(INDEX_op_cmp_vec, type, vece, tcgv_vec_arg(v0),
3450 tcgv_vec_arg(v1), tcgv_vec_arg(v2), cond);
3453 tcg_gen_not_vec(vece, v0, v0);
3457 static void expand_vec_mul(TCGType type, unsigned vece, TCGv_vec v0,
3458 TCGv_vec v1, TCGv_vec v2)
3460 TCGv_vec t1 = tcg_temp_new_vec(type);
3461 TCGv_vec t2 = tcg_temp_new_vec(type);
3467 vec_gen_3(INDEX_op_ppc_muleu_vec, type, vece, tcgv_vec_arg(t1),
3468 tcgv_vec_arg(v1), tcgv_vec_arg(v2));
3469 vec_gen_3(INDEX_op_ppc_mulou_vec, type, vece, tcgv_vec_arg(t2),
3470 tcgv_vec_arg(v1), tcgv_vec_arg(v2));
3471 vec_gen_3(INDEX_op_ppc_mrgh_vec, type, vece + 1, tcgv_vec_arg(v0),
3472 tcgv_vec_arg(t1), tcgv_vec_arg(t2));
3473 vec_gen_3(INDEX_op_ppc_mrgl_vec, type, vece + 1, tcgv_vec_arg(t1),
3474 tcgv_vec_arg(t1), tcgv_vec_arg(t2));
3475 vec_gen_3(INDEX_op_ppc_pkum_vec, type, vece, tcgv_vec_arg(v0),
3476 tcgv_vec_arg(v0), tcgv_vec_arg(t1));
3480 tcg_debug_assert(!have_isa_2_07);
3482 * Only 5 bits are significant, and VSPLTISB can represent -16..15.
3483 * So using -16 is a quick way to represent 16.
3485 c16 = tcg_constant_vec(type, MO_8, -16);
3486 c0 = tcg_constant_vec(type, MO_8, 0);
3488 vec_gen_3(INDEX_op_rotlv_vec, type, MO_32, tcgv_vec_arg(t1),
3489 tcgv_vec_arg(v2), tcgv_vec_arg(c16));
3490 vec_gen_3(INDEX_op_ppc_mulou_vec, type, MO_16, tcgv_vec_arg(t2),
3491 tcgv_vec_arg(v1), tcgv_vec_arg(v2));
3492 vec_gen_4(INDEX_op_ppc_msum_vec, type, MO_16, tcgv_vec_arg(t1),
3493 tcgv_vec_arg(v1), tcgv_vec_arg(t1), tcgv_vec_arg(c0));
3494 vec_gen_3(INDEX_op_shlv_vec, type, MO_32, tcgv_vec_arg(t1),
3495 tcgv_vec_arg(t1), tcgv_vec_arg(c16));
3496 tcg_gen_add_vec(MO_32, v0, t1, t2);
3500 g_assert_not_reached();
3502 tcg_temp_free_vec(t1);
3503 tcg_temp_free_vec(t2);
3506 void tcg_expand_vec_op(TCGOpcode opc, TCGType type, unsigned vece,
3510 TCGv_vec v0, v1, v2, t0;
3514 v0 = temp_tcgv_vec(arg_temp(a0));
3515 v1 = temp_tcgv_vec(arg_temp(va_arg(va, TCGArg)));
3516 a2 = va_arg(va, TCGArg);
3519 case INDEX_op_shli_vec:
3520 expand_vec_shi(type, vece, v0, v1, a2, INDEX_op_shlv_vec);
3522 case INDEX_op_shri_vec:
3523 expand_vec_shi(type, vece, v0, v1, a2, INDEX_op_shrv_vec);
3525 case INDEX_op_sari_vec:
3526 expand_vec_shi(type, vece, v0, v1, a2, INDEX_op_sarv_vec);
3528 case INDEX_op_rotli_vec:
3529 expand_vec_shi(type, vece, v0, v1, a2, INDEX_op_rotlv_vec);
3531 case INDEX_op_cmp_vec:
3532 v2 = temp_tcgv_vec(arg_temp(a2));
3533 expand_vec_cmp(type, vece, v0, v1, v2, va_arg(va, TCGArg));
3535 case INDEX_op_mul_vec:
3536 v2 = temp_tcgv_vec(arg_temp(a2));
3537 expand_vec_mul(type, vece, v0, v1, v2);
3539 case INDEX_op_rotlv_vec:
3540 v2 = temp_tcgv_vec(arg_temp(a2));
3541 t0 = tcg_temp_new_vec(type);
3542 tcg_gen_neg_vec(vece, t0, v2);
3543 tcg_gen_rotlv_vec(vece, v0, v1, t0);
3544 tcg_temp_free_vec(t0);
3547 g_assert_not_reached();
3552 static TCGConstraintSetIndex tcg_target_op_def(TCGOpcode op)
3555 case INDEX_op_goto_ptr:
3558 case INDEX_op_ld8u_i32:
3559 case INDEX_op_ld8s_i32:
3560 case INDEX_op_ld16u_i32:
3561 case INDEX_op_ld16s_i32:
3562 case INDEX_op_ld_i32:
3563 case INDEX_op_ctpop_i32:
3564 case INDEX_op_neg_i32:
3565 case INDEX_op_not_i32:
3566 case INDEX_op_ext8s_i32:
3567 case INDEX_op_ext16s_i32:
3568 case INDEX_op_bswap16_i32:
3569 case INDEX_op_bswap32_i32:
3570 case INDEX_op_extract_i32:
3571 case INDEX_op_ld8u_i64:
3572 case INDEX_op_ld8s_i64:
3573 case INDEX_op_ld16u_i64:
3574 case INDEX_op_ld16s_i64:
3575 case INDEX_op_ld32u_i64:
3576 case INDEX_op_ld32s_i64:
3577 case INDEX_op_ld_i64:
3578 case INDEX_op_ctpop_i64:
3579 case INDEX_op_neg_i64:
3580 case INDEX_op_not_i64:
3581 case INDEX_op_ext8s_i64:
3582 case INDEX_op_ext16s_i64:
3583 case INDEX_op_ext32s_i64:
3584 case INDEX_op_ext_i32_i64:
3585 case INDEX_op_extu_i32_i64:
3586 case INDEX_op_bswap16_i64:
3587 case INDEX_op_bswap32_i64:
3588 case INDEX_op_bswap64_i64:
3589 case INDEX_op_extract_i64:
3590 return C_O1_I1(r, r);
3592 case INDEX_op_st8_i32:
3593 case INDEX_op_st16_i32:
3594 case INDEX_op_st_i32:
3595 case INDEX_op_st8_i64:
3596 case INDEX_op_st16_i64:
3597 case INDEX_op_st32_i64:
3598 case INDEX_op_st_i64:
3599 return C_O0_I2(r, r);
3601 case INDEX_op_add_i32:
3602 case INDEX_op_and_i32:
3603 case INDEX_op_or_i32:
3604 case INDEX_op_xor_i32:
3605 case INDEX_op_andc_i32:
3606 case INDEX_op_orc_i32:
3607 case INDEX_op_eqv_i32:
3608 case INDEX_op_shl_i32:
3609 case INDEX_op_shr_i32:
3610 case INDEX_op_sar_i32:
3611 case INDEX_op_rotl_i32:
3612 case INDEX_op_rotr_i32:
3613 case INDEX_op_setcond_i32:
3614 case INDEX_op_and_i64:
3615 case INDEX_op_andc_i64:
3616 case INDEX_op_shl_i64:
3617 case INDEX_op_shr_i64:
3618 case INDEX_op_sar_i64:
3619 case INDEX_op_rotl_i64:
3620 case INDEX_op_rotr_i64:
3621 case INDEX_op_setcond_i64:
3622 return C_O1_I2(r, r, ri);
3624 case INDEX_op_mul_i32:
3625 case INDEX_op_mul_i64:
3626 return C_O1_I2(r, r, rI);
3628 case INDEX_op_div_i32:
3629 case INDEX_op_divu_i32:
3630 case INDEX_op_nand_i32:
3631 case INDEX_op_nor_i32:
3632 case INDEX_op_muluh_i32:
3633 case INDEX_op_mulsh_i32:
3634 case INDEX_op_orc_i64:
3635 case INDEX_op_eqv_i64:
3636 case INDEX_op_nand_i64:
3637 case INDEX_op_nor_i64:
3638 case INDEX_op_div_i64:
3639 case INDEX_op_divu_i64:
3640 case INDEX_op_mulsh_i64:
3641 case INDEX_op_muluh_i64:
3642 return C_O1_I2(r, r, r);
3644 case INDEX_op_sub_i32:
3645 return C_O1_I2(r, rI, ri);
3646 case INDEX_op_add_i64:
3647 return C_O1_I2(r, r, rT);
3648 case INDEX_op_or_i64:
3649 case INDEX_op_xor_i64:
3650 return C_O1_I2(r, r, rU);
3651 case INDEX_op_sub_i64:
3652 return C_O1_I2(r, rI, rT);
3653 case INDEX_op_clz_i32:
3654 case INDEX_op_ctz_i32:
3655 case INDEX_op_clz_i64:
3656 case INDEX_op_ctz_i64:
3657 return C_O1_I2(r, r, rZW);
3659 case INDEX_op_brcond_i32:
3660 case INDEX_op_brcond_i64:
3661 return C_O0_I2(r, ri);
3663 case INDEX_op_movcond_i32:
3664 case INDEX_op_movcond_i64:
3665 return C_O1_I4(r, r, ri, rZ, rZ);
3666 case INDEX_op_deposit_i32:
3667 case INDEX_op_deposit_i64:
3668 return C_O1_I2(r, 0, rZ);
3669 case INDEX_op_brcond2_i32:
3670 return C_O0_I4(r, r, ri, ri);
3671 case INDEX_op_setcond2_i32:
3672 return C_O1_I4(r, r, r, ri, ri);
3673 case INDEX_op_add2_i64:
3674 case INDEX_op_add2_i32:
3675 return C_O2_I4(r, r, r, r, rI, rZM);
3676 case INDEX_op_sub2_i64:
3677 case INDEX_op_sub2_i32:
3678 return C_O2_I4(r, r, rI, rZM, r, r);
3680 case INDEX_op_qemu_ld_i32:
3681 return (TCG_TARGET_REG_BITS == 64 || TARGET_LONG_BITS == 32
3683 : C_O1_I2(r, L, L));
3685 case INDEX_op_qemu_st_i32:
3686 return (TCG_TARGET_REG_BITS == 64 || TARGET_LONG_BITS == 32
3688 : C_O0_I3(S, S, S));
3690 case INDEX_op_qemu_ld_i64:
3691 return (TCG_TARGET_REG_BITS == 64 ? C_O1_I1(r, L)
3692 : TARGET_LONG_BITS == 32 ? C_O2_I1(L, L, L)
3693 : C_O2_I2(L, L, L, L));
3695 case INDEX_op_qemu_st_i64:
3696 return (TCG_TARGET_REG_BITS == 64 ? C_O0_I2(S, S)
3697 : TARGET_LONG_BITS == 32 ? C_O0_I3(S, S, S)
3698 : C_O0_I4(S, S, S, S));
3700 case INDEX_op_add_vec:
3701 case INDEX_op_sub_vec:
3702 case INDEX_op_mul_vec:
3703 case INDEX_op_and_vec:
3704 case INDEX_op_or_vec:
3705 case INDEX_op_xor_vec:
3706 case INDEX_op_andc_vec:
3707 case INDEX_op_orc_vec:
3708 case INDEX_op_cmp_vec:
3709 case INDEX_op_ssadd_vec:
3710 case INDEX_op_sssub_vec:
3711 case INDEX_op_usadd_vec:
3712 case INDEX_op_ussub_vec:
3713 case INDEX_op_smax_vec:
3714 case INDEX_op_smin_vec:
3715 case INDEX_op_umax_vec:
3716 case INDEX_op_umin_vec:
3717 case INDEX_op_shlv_vec:
3718 case INDEX_op_shrv_vec:
3719 case INDEX_op_sarv_vec:
3720 case INDEX_op_rotlv_vec:
3721 case INDEX_op_rotrv_vec:
3722 case INDEX_op_ppc_mrgh_vec:
3723 case INDEX_op_ppc_mrgl_vec:
3724 case INDEX_op_ppc_muleu_vec:
3725 case INDEX_op_ppc_mulou_vec:
3726 case INDEX_op_ppc_pkum_vec:
3727 case INDEX_op_dup2_vec:
3728 return C_O1_I2(v, v, v);
3730 case INDEX_op_not_vec:
3731 case INDEX_op_neg_vec:
3732 return C_O1_I1(v, v);
3734 case INDEX_op_dup_vec:
3735 return have_isa_3_00 ? C_O1_I1(v, vr) : C_O1_I1(v, v);
3737 case INDEX_op_ld_vec:
3738 case INDEX_op_dupm_vec:
3739 return C_O1_I1(v, r);
3741 case INDEX_op_st_vec:
3742 return C_O0_I2(v, r);
3744 case INDEX_op_bitsel_vec:
3745 case INDEX_op_ppc_msum_vec:
3746 return C_O1_I3(v, v, v, v);
3749 g_assert_not_reached();
3753 static void tcg_target_init(TCGContext *s)
3755 unsigned long hwcap = qemu_getauxval(AT_HWCAP);
3756 unsigned long hwcap2 = qemu_getauxval(AT_HWCAP2);
3758 have_isa = tcg_isa_base;
3759 if (hwcap & PPC_FEATURE_ARCH_2_06) {
3760 have_isa = tcg_isa_2_06;
3762 #ifdef PPC_FEATURE2_ARCH_2_07
3763 if (hwcap2 & PPC_FEATURE2_ARCH_2_07) {
3764 have_isa = tcg_isa_2_07;
3767 #ifdef PPC_FEATURE2_ARCH_3_00
3768 if (hwcap2 & PPC_FEATURE2_ARCH_3_00) {
3769 have_isa = tcg_isa_3_00;
3772 #ifdef PPC_FEATURE2_ARCH_3_10
3773 if (hwcap2 & PPC_FEATURE2_ARCH_3_10) {
3774 have_isa = tcg_isa_3_10;
3778 #ifdef PPC_FEATURE2_HAS_ISEL
3779 /* Prefer explicit instruction from the kernel. */
3780 have_isel = (hwcap2 & PPC_FEATURE2_HAS_ISEL) != 0;
3782 /* Fall back to knowing Power7 (2.06) has ISEL. */
3783 have_isel = have_isa_2_06;
3786 if (hwcap & PPC_FEATURE_HAS_ALTIVEC) {
3787 have_altivec = true;
3788 /* We only care about the portion of VSX that overlaps Altivec. */
3789 if (hwcap & PPC_FEATURE_HAS_VSX) {
3794 tcg_target_available_regs[TCG_TYPE_I32] = 0xffffffff;
3795 tcg_target_available_regs[TCG_TYPE_I64] = 0xffffffff;
3797 tcg_target_available_regs[TCG_TYPE_V64] = 0xffffffff00000000ull;
3798 tcg_target_available_regs[TCG_TYPE_V128] = 0xffffffff00000000ull;
3801 tcg_target_call_clobber_regs = 0;
3802 tcg_regset_set_reg(tcg_target_call_clobber_regs, TCG_REG_R0);
3803 tcg_regset_set_reg(tcg_target_call_clobber_regs, TCG_REG_R2);
3804 tcg_regset_set_reg(tcg_target_call_clobber_regs, TCG_REG_R3);
3805 tcg_regset_set_reg(tcg_target_call_clobber_regs, TCG_REG_R4);
3806 tcg_regset_set_reg(tcg_target_call_clobber_regs, TCG_REG_R5);
3807 tcg_regset_set_reg(tcg_target_call_clobber_regs, TCG_REG_R6);
3808 tcg_regset_set_reg(tcg_target_call_clobber_regs, TCG_REG_R7);
3809 tcg_regset_set_reg(tcg_target_call_clobber_regs, TCG_REG_R8);
3810 tcg_regset_set_reg(tcg_target_call_clobber_regs, TCG_REG_R9);
3811 tcg_regset_set_reg(tcg_target_call_clobber_regs, TCG_REG_R10);
3812 tcg_regset_set_reg(tcg_target_call_clobber_regs, TCG_REG_R11);
3813 tcg_regset_set_reg(tcg_target_call_clobber_regs, TCG_REG_R12);
3815 tcg_regset_set_reg(tcg_target_call_clobber_regs, TCG_REG_V0);
3816 tcg_regset_set_reg(tcg_target_call_clobber_regs, TCG_REG_V1);
3817 tcg_regset_set_reg(tcg_target_call_clobber_regs, TCG_REG_V2);
3818 tcg_regset_set_reg(tcg_target_call_clobber_regs, TCG_REG_V3);
3819 tcg_regset_set_reg(tcg_target_call_clobber_regs, TCG_REG_V4);
3820 tcg_regset_set_reg(tcg_target_call_clobber_regs, TCG_REG_V5);
3821 tcg_regset_set_reg(tcg_target_call_clobber_regs, TCG_REG_V6);
3822 tcg_regset_set_reg(tcg_target_call_clobber_regs, TCG_REG_V7);
3823 tcg_regset_set_reg(tcg_target_call_clobber_regs, TCG_REG_V8);
3824 tcg_regset_set_reg(tcg_target_call_clobber_regs, TCG_REG_V9);
3825 tcg_regset_set_reg(tcg_target_call_clobber_regs, TCG_REG_V10);
3826 tcg_regset_set_reg(tcg_target_call_clobber_regs, TCG_REG_V11);
3827 tcg_regset_set_reg(tcg_target_call_clobber_regs, TCG_REG_V12);
3828 tcg_regset_set_reg(tcg_target_call_clobber_regs, TCG_REG_V13);
3829 tcg_regset_set_reg(tcg_target_call_clobber_regs, TCG_REG_V14);
3830 tcg_regset_set_reg(tcg_target_call_clobber_regs, TCG_REG_V15);
3831 tcg_regset_set_reg(tcg_target_call_clobber_regs, TCG_REG_V16);
3832 tcg_regset_set_reg(tcg_target_call_clobber_regs, TCG_REG_V17);
3833 tcg_regset_set_reg(tcg_target_call_clobber_regs, TCG_REG_V18);
3834 tcg_regset_set_reg(tcg_target_call_clobber_regs, TCG_REG_V19);
3836 s->reserved_regs = 0;
3837 tcg_regset_set_reg(s->reserved_regs, TCG_REG_R0); /* tcg temp */
3838 tcg_regset_set_reg(s->reserved_regs, TCG_REG_R1); /* stack pointer */
3839 #if defined(_CALL_SYSV)
3840 tcg_regset_set_reg(s->reserved_regs, TCG_REG_R2); /* toc pointer */
3842 #if defined(_CALL_SYSV) || TCG_TARGET_REG_BITS == 64
3843 tcg_regset_set_reg(s->reserved_regs, TCG_REG_R13); /* thread pointer */
3845 tcg_regset_set_reg(s->reserved_regs, TCG_REG_TMP1); /* mem temp */
3846 tcg_regset_set_reg(s->reserved_regs, TCG_VEC_TMP1);
3847 tcg_regset_set_reg(s->reserved_regs, TCG_VEC_TMP2);
3849 tcg_regset_set_reg(s->reserved_regs, TCG_REG_TB); /* tb->tc_ptr */
3856 DebugFrameFDEHeader fde;
3857 uint8_t fde_def_cfa[4];
3858 uint8_t fde_reg_ofs[ARRAY_SIZE(tcg_target_callee_save_regs) * 2 + 3];
3861 /* We're expecting a 2 byte uleb128 encoded value. */
3862 QEMU_BUILD_BUG_ON(FRAME_SIZE >= (1 << 14));
3864 #if TCG_TARGET_REG_BITS == 64
3865 # define ELF_HOST_MACHINE EM_PPC64
3867 # define ELF_HOST_MACHINE EM_PPC
3870 static DebugFrame debug_frame = {
3871 .cie.len = sizeof(DebugFrameCIE)-4, /* length after .len member */
3874 .cie.code_align = 1,
3875 .cie.data_align = (-SZR & 0x7f), /* sleb128 -SZR */
3876 .cie.return_column = 65,
3878 /* Total FDE size does not include the "len" member. */
3879 .fde.len = sizeof(DebugFrame) - offsetof(DebugFrame, fde.cie_offset),
3882 12, TCG_REG_R1, /* DW_CFA_def_cfa r1, ... */
3883 (FRAME_SIZE & 0x7f) | 0x80, /* ... uleb128 FRAME_SIZE */
3887 /* DW_CFA_offset_extended_sf, lr, LR_OFFSET */
3888 0x11, 65, (LR_OFFSET / -SZR) & 0x7f,
3892 void tcg_register_jit(const void *buf, size_t buf_size)
3894 uint8_t *p = &debug_frame.fde_reg_ofs[3];
3897 for (i = 0; i < ARRAY_SIZE(tcg_target_callee_save_regs); ++i, p += 2) {
3898 p[0] = 0x80 + tcg_target_callee_save_regs[i];
3899 p[1] = (FRAME_SIZE - (REG_SAVE_BOT + i * SZR)) / SZR;
3902 debug_frame.fde.func_start = (uintptr_t)buf;
3903 debug_frame.fde.func_len = buf_size;
3905 tcg_register_jit_int(buf, buf_size, &debug_frame, sizeof(debug_frame));
3907 #endif /* __ELF__ */