2 * PowerPC emulation for qemu: main translation routines.
4 * Copyright (c) 2003-2007 Jocelyn Mayer
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
30 #include "qemu-common.h"
36 #define CPU_SINGLE_STEP 0x1
37 #define CPU_BRANCH_STEP 0x2
38 #define GDBSTUB_SINGLE_STEP 0x4
40 /* Include definitions for instructions classes and implementations flags */
41 //#define DO_SINGLE_STEP
42 //#define PPC_DEBUG_DISAS
43 //#define DO_PPC_STATISTICS
44 //#define OPTIMIZE_FPRF_UPDATE
46 /*****************************************************************************/
47 /* Code translation helpers */
49 /* global register indexes */
50 static TCGv_ptr cpu_env;
51 static char cpu_reg_names[10*3 + 22*4 /* GPR */
52 #if !defined(TARGET_PPC64)
53 + 10*4 + 22*5 /* SPE GPRh */
55 + 10*4 + 22*5 /* FPR */
56 + 2*(10*6 + 22*7) /* AVRh, AVRl */
58 static TCGv cpu_gpr[32];
59 #if !defined(TARGET_PPC64)
60 static TCGv cpu_gprh[32];
62 static TCGv_i64 cpu_fpr[32];
63 static TCGv_i64 cpu_avrh[32], cpu_avrl[32];
64 static TCGv_i32 cpu_crf[8];
69 static TCGv_i32 cpu_fpscr;
70 static TCGv_i32 cpu_access_type;
72 /* dyngen register indexes */
74 #if defined(TARGET_PPC64)
77 static TCGv_i64 cpu_T64[3];
79 static TCGv_i64 cpu_FT[2];
81 #include "gen-icount.h"
83 void ppc_translate_init(void)
87 static int done_init = 0;
92 cpu_env = tcg_global_reg_new_ptr(TCG_AREG0, "env");
93 #if TARGET_LONG_BITS > HOST_LONG_BITS
94 cpu_T[0] = tcg_global_mem_new(TCG_AREG0, offsetof(CPUState, t0), "T0");
95 cpu_T[1] = tcg_global_mem_new(TCG_AREG0, offsetof(CPUState, t1), "T1");
96 cpu_T[2] = tcg_global_mem_new(TCG_AREG0, offsetof(CPUState, t2), "T2");
98 cpu_T[0] = tcg_global_reg_new(TCG_AREG1, "T0");
99 cpu_T[1] = tcg_global_reg_new(TCG_AREG2, "T1");
101 /* XXX: This is a temporary workaround for i386.
102 * On i386 qemu_st32 runs out of registers.
103 * The proper fix is to remove cpu_T.
105 cpu_T[2] = tcg_global_mem_new(TCG_AREG0, offsetof(CPUState, t2), "T2");
107 cpu_T[2] = tcg_global_reg_new(TCG_AREG3, "T2");
110 #if !defined(TARGET_PPC64)
111 cpu_T64[0] = tcg_global_mem_new_i64(TCG_AREG0, offsetof(CPUState, t0_64),
113 cpu_T64[1] = tcg_global_mem_new_i64(TCG_AREG0, offsetof(CPUState, t1_64),
115 cpu_T64[2] = tcg_global_mem_new_i64(TCG_AREG0, offsetof(CPUState, t2_64),
119 cpu_FT[0] = tcg_global_mem_new_i64(TCG_AREG0,
120 offsetof(CPUState, ft0), "FT0");
121 cpu_FT[1] = tcg_global_mem_new_i64(TCG_AREG0,
122 offsetof(CPUState, ft1), "FT1");
126 for (i = 0; i < 8; i++) {
127 sprintf(p, "crf%d", i);
128 cpu_crf[i] = tcg_global_mem_new_i32(TCG_AREG0,
129 offsetof(CPUState, crf[i]), p);
133 for (i = 0; i < 32; i++) {
134 sprintf(p, "r%d", i);
135 cpu_gpr[i] = tcg_global_mem_new(TCG_AREG0,
136 offsetof(CPUState, gpr[i]), p);
137 p += (i < 10) ? 3 : 4;
138 #if !defined(TARGET_PPC64)
139 sprintf(p, "r%dH", i);
140 cpu_gprh[i] = tcg_global_mem_new_i32(TCG_AREG0,
141 offsetof(CPUState, gprh[i]), p);
142 p += (i < 10) ? 4 : 5;
145 sprintf(p, "fp%d", i);
146 cpu_fpr[i] = tcg_global_mem_new_i64(TCG_AREG0,
147 offsetof(CPUState, fpr[i]), p);
148 p += (i < 10) ? 4 : 5;
150 sprintf(p, "avr%dH", i);
151 #ifdef WORDS_BIGENDIAN
152 cpu_avrh[i] = tcg_global_mem_new_i64(TCG_AREG0,
153 offsetof(CPUState, avr[i].u64[0]), p);
155 cpu_avrh[i] = tcg_global_mem_new_i64(TCG_AREG0,
156 offsetof(CPUState, avr[i].u64[1]), p);
158 p += (i < 10) ? 6 : 7;
160 sprintf(p, "avr%dL", i);
161 #ifdef WORDS_BIGENDIAN
162 cpu_avrl[i] = tcg_global_mem_new_i64(TCG_AREG0,
163 offsetof(CPUState, avr[i].u64[1]), p);
165 cpu_avrl[i] = tcg_global_mem_new_i64(TCG_AREG0,
166 offsetof(CPUState, avr[i].u64[0]), p);
168 p += (i < 10) ? 6 : 7;
171 cpu_nip = tcg_global_mem_new(TCG_AREG0,
172 offsetof(CPUState, nip), "nip");
174 cpu_ctr = tcg_global_mem_new(TCG_AREG0,
175 offsetof(CPUState, ctr), "ctr");
177 cpu_lr = tcg_global_mem_new(TCG_AREG0,
178 offsetof(CPUState, lr), "lr");
180 cpu_xer = tcg_global_mem_new(TCG_AREG0,
181 offsetof(CPUState, xer), "xer");
183 cpu_fpscr = tcg_global_mem_new_i32(TCG_AREG0,
184 offsetof(CPUState, fpscr), "fpscr");
186 cpu_access_type = tcg_global_mem_new_i32(TCG_AREG0,
187 offsetof(CPUState, access_type), "access_type");
189 /* register helpers */
196 #if defined(OPTIMIZE_FPRF_UPDATE)
197 static uint16_t *gen_fprf_buf[OPC_BUF_SIZE];
198 static uint16_t **gen_fprf_ptr;
201 /* internal defines */
202 typedef struct DisasContext {
203 struct TranslationBlock *tb;
207 /* Routine used to access memory */
209 /* Translation flags */
210 #if !defined(CONFIG_USER_ONLY)
213 #if defined(TARGET_PPC64)
219 ppc_spr_t *spr_cb; /* Needed to check rights for mfspr/mtspr */
220 int singlestep_enabled;
221 int dcache_line_size;
224 struct opc_handler_t {
227 /* instruction type */
230 void (*handler)(DisasContext *ctx);
231 #if defined(DO_PPC_STATISTICS) || defined(PPC_DUMP_CPU)
234 #if defined(DO_PPC_STATISTICS)
239 static always_inline void gen_reset_fpstatus (void)
241 #ifdef CONFIG_SOFTFLOAT
242 gen_op_reset_fpstatus();
246 static always_inline void gen_compute_fprf (TCGv_i64 arg, int set_fprf, int set_rc)
248 TCGv_i32 t0 = tcg_temp_new_i32();
251 /* This case might be optimized later */
252 #if defined(OPTIMIZE_FPRF_UPDATE)
253 *gen_fprf_ptr++ = gen_opc_ptr;
255 tcg_gen_movi_i32(t0, 1);
256 gen_helper_compute_fprf(t0, arg, t0);
257 if (unlikely(set_rc)) {
258 tcg_gen_mov_i32(cpu_crf[1], t0);
260 gen_helper_float_check_status();
261 } else if (unlikely(set_rc)) {
262 /* We always need to compute fpcc */
263 tcg_gen_movi_i32(t0, 0);
264 gen_helper_compute_fprf(t0, arg, t0);
265 tcg_gen_mov_i32(cpu_crf[1], t0);
267 gen_helper_float_check_status();
270 tcg_temp_free_i32(t0);
273 static always_inline void gen_optimize_fprf (void)
275 #if defined(OPTIMIZE_FPRF_UPDATE)
278 for (ptr = gen_fprf_buf; ptr != (gen_fprf_ptr - 1); ptr++)
279 *ptr = INDEX_op_nop1;
280 gen_fprf_ptr = gen_fprf_buf;
284 static always_inline void gen_set_access_type(int access_type)
286 tcg_gen_movi_i32(cpu_access_type, access_type);
289 static always_inline void gen_update_nip (DisasContext *ctx, target_ulong nip)
291 #if defined(TARGET_PPC64)
293 tcg_gen_movi_tl(cpu_nip, nip);
296 tcg_gen_movi_tl(cpu_nip, (uint32_t)nip);
299 #define GEN_EXCP(ctx, excp, error) \
301 TCGv_i32 t0 = tcg_const_i32(excp); \
302 TCGv_i32 t1 = tcg_const_i32(error); \
303 if ((ctx)->exception == POWERPC_EXCP_NONE) { \
304 gen_update_nip(ctx, (ctx)->nip); \
306 gen_helper_raise_exception_err(t0, t1); \
307 tcg_temp_free_i32(t0); \
308 tcg_temp_free_i32(t1); \
309 ctx->exception = (excp); \
312 #define GEN_EXCP_INVAL(ctx) \
313 GEN_EXCP((ctx), POWERPC_EXCP_PROGRAM, \
314 POWERPC_EXCP_INVAL | POWERPC_EXCP_INVAL_INVAL)
316 #define GEN_EXCP_PRIVOPC(ctx) \
317 GEN_EXCP((ctx), POWERPC_EXCP_PROGRAM, \
318 POWERPC_EXCP_INVAL | POWERPC_EXCP_PRIV_OPC)
320 #define GEN_EXCP_PRIVREG(ctx) \
321 GEN_EXCP((ctx), POWERPC_EXCP_PROGRAM, \
322 POWERPC_EXCP_INVAL | POWERPC_EXCP_PRIV_REG)
324 #define GEN_EXCP_NO_FP(ctx) \
325 GEN_EXCP(ctx, POWERPC_EXCP_FPU, 0)
327 #define GEN_EXCP_NO_AP(ctx) \
328 GEN_EXCP(ctx, POWERPC_EXCP_APU, 0)
330 #define GEN_EXCP_NO_VR(ctx) \
331 GEN_EXCP(ctx, POWERPC_EXCP_VPU, 0)
333 /* Stop translation */
334 static always_inline void GEN_STOP (DisasContext *ctx)
336 gen_update_nip(ctx, ctx->nip);
337 ctx->exception = POWERPC_EXCP_STOP;
340 /* No need to update nip here, as execution flow will change */
341 static always_inline void GEN_SYNC (DisasContext *ctx)
343 ctx->exception = POWERPC_EXCP_SYNC;
346 #define GEN_HANDLER(name, opc1, opc2, opc3, inval, type) \
347 static void gen_##name (DisasContext *ctx); \
348 GEN_OPCODE(name, opc1, opc2, opc3, inval, type); \
349 static void gen_##name (DisasContext *ctx)
351 #define GEN_HANDLER2(name, onam, opc1, opc2, opc3, inval, type) \
352 static void gen_##name (DisasContext *ctx); \
353 GEN_OPCODE2(name, onam, opc1, opc2, opc3, inval, type); \
354 static void gen_##name (DisasContext *ctx)
356 typedef struct opcode_t {
357 unsigned char opc1, opc2, opc3;
358 #if HOST_LONG_BITS == 64 /* Explicitly align to 64 bits */
359 unsigned char pad[5];
361 unsigned char pad[1];
363 opc_handler_t handler;
367 /*****************************************************************************/
368 /*** Instruction decoding ***/
369 #define EXTRACT_HELPER(name, shift, nb) \
370 static always_inline uint32_t name (uint32_t opcode) \
372 return (opcode >> (shift)) & ((1 << (nb)) - 1); \
375 #define EXTRACT_SHELPER(name, shift, nb) \
376 static always_inline int32_t name (uint32_t opcode) \
378 return (int16_t)((opcode >> (shift)) & ((1 << (nb)) - 1)); \
382 EXTRACT_HELPER(opc1, 26, 6);
384 EXTRACT_HELPER(opc2, 1, 5);
386 EXTRACT_HELPER(opc3, 6, 5);
387 /* Update Cr0 flags */
388 EXTRACT_HELPER(Rc, 0, 1);
390 EXTRACT_HELPER(rD, 21, 5);
392 EXTRACT_HELPER(rS, 21, 5);
394 EXTRACT_HELPER(rA, 16, 5);
396 EXTRACT_HELPER(rB, 11, 5);
398 EXTRACT_HELPER(rC, 6, 5);
400 EXTRACT_HELPER(crfD, 23, 3);
401 EXTRACT_HELPER(crfS, 18, 3);
402 EXTRACT_HELPER(crbD, 21, 5);
403 EXTRACT_HELPER(crbA, 16, 5);
404 EXTRACT_HELPER(crbB, 11, 5);
406 EXTRACT_HELPER(_SPR, 11, 10);
407 static always_inline uint32_t SPR (uint32_t opcode)
409 uint32_t sprn = _SPR(opcode);
411 return ((sprn >> 5) & 0x1F) | ((sprn & 0x1F) << 5);
413 /*** Get constants ***/
414 EXTRACT_HELPER(IMM, 12, 8);
415 /* 16 bits signed immediate value */
416 EXTRACT_SHELPER(SIMM, 0, 16);
417 /* 16 bits unsigned immediate value */
418 EXTRACT_HELPER(UIMM, 0, 16);
420 EXTRACT_HELPER(NB, 11, 5);
422 EXTRACT_HELPER(SH, 11, 5);
424 EXTRACT_HELPER(MB, 6, 5);
426 EXTRACT_HELPER(ME, 1, 5);
428 EXTRACT_HELPER(TO, 21, 5);
430 EXTRACT_HELPER(CRM, 12, 8);
431 EXTRACT_HELPER(FM, 17, 8);
432 EXTRACT_HELPER(SR, 16, 4);
433 EXTRACT_HELPER(FPIMM, 12, 4);
435 /*** Jump target decoding ***/
437 EXTRACT_SHELPER(d, 0, 16);
438 /* Immediate address */
439 static always_inline target_ulong LI (uint32_t opcode)
441 return (opcode >> 0) & 0x03FFFFFC;
444 static always_inline uint32_t BD (uint32_t opcode)
446 return (opcode >> 0) & 0xFFFC;
449 EXTRACT_HELPER(BO, 21, 5);
450 EXTRACT_HELPER(BI, 16, 5);
451 /* Absolute/relative address */
452 EXTRACT_HELPER(AA, 1, 1);
454 EXTRACT_HELPER(LK, 0, 1);
456 /* Create a mask between <start> and <end> bits */
457 static always_inline target_ulong MASK (uint32_t start, uint32_t end)
461 #if defined(TARGET_PPC64)
462 if (likely(start == 0)) {
463 ret = UINT64_MAX << (63 - end);
464 } else if (likely(end == 63)) {
465 ret = UINT64_MAX >> start;
468 if (likely(start == 0)) {
469 ret = UINT32_MAX << (31 - end);
470 } else if (likely(end == 31)) {
471 ret = UINT32_MAX >> start;
475 ret = (((target_ulong)(-1ULL)) >> (start)) ^
476 (((target_ulong)(-1ULL) >> (end)) >> 1);
477 if (unlikely(start > end))
484 /*****************************************************************************/
485 /* PowerPC Instructions types definitions */
487 PPC_NONE = 0x0000000000000000ULL,
488 /* PowerPC base instructions set */
489 PPC_INSNS_BASE = 0x0000000000000001ULL,
490 /* integer operations instructions */
491 #define PPC_INTEGER PPC_INSNS_BASE
492 /* flow control instructions */
493 #define PPC_FLOW PPC_INSNS_BASE
494 /* virtual memory instructions */
495 #define PPC_MEM PPC_INSNS_BASE
496 /* ld/st with reservation instructions */
497 #define PPC_RES PPC_INSNS_BASE
498 /* spr/msr access instructions */
499 #define PPC_MISC PPC_INSNS_BASE
500 /* Deprecated instruction sets */
501 /* Original POWER instruction set */
502 PPC_POWER = 0x0000000000000002ULL,
503 /* POWER2 instruction set extension */
504 PPC_POWER2 = 0x0000000000000004ULL,
505 /* Power RTC support */
506 PPC_POWER_RTC = 0x0000000000000008ULL,
507 /* Power-to-PowerPC bridge (601) */
508 PPC_POWER_BR = 0x0000000000000010ULL,
509 /* 64 bits PowerPC instruction set */
510 PPC_64B = 0x0000000000000020ULL,
511 /* New 64 bits extensions (PowerPC 2.0x) */
512 PPC_64BX = 0x0000000000000040ULL,
513 /* 64 bits hypervisor extensions */
514 PPC_64H = 0x0000000000000080ULL,
515 /* New wait instruction (PowerPC 2.0x) */
516 PPC_WAIT = 0x0000000000000100ULL,
517 /* Time base mftb instruction */
518 PPC_MFTB = 0x0000000000000200ULL,
520 /* Fixed-point unit extensions */
521 /* PowerPC 602 specific */
522 PPC_602_SPEC = 0x0000000000000400ULL,
523 /* isel instruction */
524 PPC_ISEL = 0x0000000000000800ULL,
525 /* popcntb instruction */
526 PPC_POPCNTB = 0x0000000000001000ULL,
527 /* string load / store */
528 PPC_STRING = 0x0000000000002000ULL,
530 /* Floating-point unit extensions */
531 /* Optional floating point instructions */
532 PPC_FLOAT = 0x0000000000010000ULL,
533 /* New floating-point extensions (PowerPC 2.0x) */
534 PPC_FLOAT_EXT = 0x0000000000020000ULL,
535 PPC_FLOAT_FSQRT = 0x0000000000040000ULL,
536 PPC_FLOAT_FRES = 0x0000000000080000ULL,
537 PPC_FLOAT_FRSQRTE = 0x0000000000100000ULL,
538 PPC_FLOAT_FRSQRTES = 0x0000000000200000ULL,
539 PPC_FLOAT_FSEL = 0x0000000000400000ULL,
540 PPC_FLOAT_STFIWX = 0x0000000000800000ULL,
542 /* Vector/SIMD extensions */
543 /* Altivec support */
544 PPC_ALTIVEC = 0x0000000001000000ULL,
545 /* PowerPC 2.03 SPE extension */
546 PPC_SPE = 0x0000000002000000ULL,
547 /* PowerPC 2.03 SPE floating-point extension */
548 PPC_SPEFPU = 0x0000000004000000ULL,
550 /* Optional memory control instructions */
551 PPC_MEM_TLBIA = 0x0000000010000000ULL,
552 PPC_MEM_TLBIE = 0x0000000020000000ULL,
553 PPC_MEM_TLBSYNC = 0x0000000040000000ULL,
554 /* sync instruction */
555 PPC_MEM_SYNC = 0x0000000080000000ULL,
556 /* eieio instruction */
557 PPC_MEM_EIEIO = 0x0000000100000000ULL,
559 /* Cache control instructions */
560 PPC_CACHE = 0x0000000200000000ULL,
561 /* icbi instruction */
562 PPC_CACHE_ICBI = 0x0000000400000000ULL,
563 /* dcbz instruction with fixed cache line size */
564 PPC_CACHE_DCBZ = 0x0000000800000000ULL,
565 /* dcbz instruction with tunable cache line size */
566 PPC_CACHE_DCBZT = 0x0000001000000000ULL,
567 /* dcba instruction */
568 PPC_CACHE_DCBA = 0x0000002000000000ULL,
569 /* Freescale cache locking instructions */
570 PPC_CACHE_LOCK = 0x0000004000000000ULL,
572 /* MMU related extensions */
573 /* external control instructions */
574 PPC_EXTERN = 0x0000010000000000ULL,
575 /* segment register access instructions */
576 PPC_SEGMENT = 0x0000020000000000ULL,
577 /* PowerPC 6xx TLB management instructions */
578 PPC_6xx_TLB = 0x0000040000000000ULL,
579 /* PowerPC 74xx TLB management instructions */
580 PPC_74xx_TLB = 0x0000080000000000ULL,
581 /* PowerPC 40x TLB management instructions */
582 PPC_40x_TLB = 0x0000100000000000ULL,
583 /* segment register access instructions for PowerPC 64 "bridge" */
584 PPC_SEGMENT_64B = 0x0000200000000000ULL,
586 PPC_SLBI = 0x0000400000000000ULL,
588 /* Embedded PowerPC dedicated instructions */
589 PPC_WRTEE = 0x0001000000000000ULL,
590 /* PowerPC 40x exception model */
591 PPC_40x_EXCP = 0x0002000000000000ULL,
592 /* PowerPC 405 Mac instructions */
593 PPC_405_MAC = 0x0004000000000000ULL,
594 /* PowerPC 440 specific instructions */
595 PPC_440_SPEC = 0x0008000000000000ULL,
596 /* BookE (embedded) PowerPC specification */
597 PPC_BOOKE = 0x0010000000000000ULL,
598 /* mfapidi instruction */
599 PPC_MFAPIDI = 0x0020000000000000ULL,
600 /* tlbiva instruction */
601 PPC_TLBIVA = 0x0040000000000000ULL,
602 /* tlbivax instruction */
603 PPC_TLBIVAX = 0x0080000000000000ULL,
604 /* PowerPC 4xx dedicated instructions */
605 PPC_4xx_COMMON = 0x0100000000000000ULL,
606 /* PowerPC 40x ibct instructions */
607 PPC_40x_ICBT = 0x0200000000000000ULL,
608 /* rfmci is not implemented in all BookE PowerPC */
609 PPC_RFMCI = 0x0400000000000000ULL,
610 /* rfdi instruction */
611 PPC_RFDI = 0x0800000000000000ULL,
613 PPC_DCR = 0x1000000000000000ULL,
614 /* DCR extended accesse */
615 PPC_DCRX = 0x2000000000000000ULL,
616 /* user-mode DCR access, implemented in PowerPC 460 */
617 PPC_DCRUX = 0x4000000000000000ULL,
620 /*****************************************************************************/
621 /* PowerPC instructions table */
622 #if HOST_LONG_BITS == 64
627 #if defined(__APPLE__)
628 #define OPCODES_SECTION \
629 __attribute__ ((section("__TEXT,__opcodes"), unused, aligned (OPC_ALIGN) ))
631 #define OPCODES_SECTION \
632 __attribute__ ((section(".opcodes"), unused, aligned (OPC_ALIGN) ))
635 #if defined(DO_PPC_STATISTICS)
636 #define GEN_OPCODE(name, op1, op2, op3, invl, _typ) \
637 OPCODES_SECTION opcode_t opc_##name = { \
645 .handler = &gen_##name, \
646 .oname = stringify(name), \
648 .oname = stringify(name), \
650 #define GEN_OPCODE2(name, onam, op1, op2, op3, invl, _typ) \
651 OPCODES_SECTION opcode_t opc_##name = { \
659 .handler = &gen_##name, \
665 #define GEN_OPCODE(name, op1, op2, op3, invl, _typ) \
666 OPCODES_SECTION opcode_t opc_##name = { \
674 .handler = &gen_##name, \
676 .oname = stringify(name), \
678 #define GEN_OPCODE2(name, onam, op1, op2, op3, invl, _typ) \
679 OPCODES_SECTION opcode_t opc_##name = { \
687 .handler = &gen_##name, \
693 #define GEN_OPCODE_MARK(name) \
694 OPCODES_SECTION opcode_t opc_##name = { \
700 .inval = 0x00000000, \
704 .oname = stringify(name), \
707 /* Start opcode list */
708 GEN_OPCODE_MARK(start);
710 /* Invalid instruction */
711 GEN_HANDLER(invalid, 0x00, 0x00, 0x00, 0xFFFFFFFF, PPC_NONE)
716 static opc_handler_t invalid_handler = {
719 .handler = gen_invalid,
722 /*** Integer comparison ***/
724 static always_inline void gen_op_cmp(TCGv arg0, TCGv arg1, int s, int crf)
728 tcg_gen_trunc_tl_i32(cpu_crf[crf], cpu_xer);
729 tcg_gen_shri_i32(cpu_crf[crf], cpu_crf[crf], XER_SO);
730 tcg_gen_andi_i32(cpu_crf[crf], cpu_crf[crf], 1);
732 l1 = gen_new_label();
733 l2 = gen_new_label();
734 l3 = gen_new_label();
736 tcg_gen_brcond_tl(TCG_COND_LT, arg0, arg1, l1);
737 tcg_gen_brcond_tl(TCG_COND_GT, arg0, arg1, l2);
739 tcg_gen_brcond_tl(TCG_COND_LTU, arg0, arg1, l1);
740 tcg_gen_brcond_tl(TCG_COND_GTU, arg0, arg1, l2);
742 tcg_gen_ori_i32(cpu_crf[crf], cpu_crf[crf], 1 << CRF_EQ);
745 tcg_gen_ori_i32(cpu_crf[crf], cpu_crf[crf], 1 << CRF_LT);
748 tcg_gen_ori_i32(cpu_crf[crf], cpu_crf[crf], 1 << CRF_GT);
752 static always_inline void gen_op_cmpi(TCGv arg0, target_ulong arg1, int s, int crf)
754 TCGv t0 = tcg_const_local_tl(arg1);
755 gen_op_cmp(arg0, t0, s, crf);
759 #if defined(TARGET_PPC64)
760 static always_inline void gen_op_cmp32(TCGv arg0, TCGv arg1, int s, int crf)
763 t0 = tcg_temp_local_new();
764 t1 = tcg_temp_local_new();
766 tcg_gen_ext32s_tl(t0, arg0);
767 tcg_gen_ext32s_tl(t1, arg1);
769 tcg_gen_ext32u_tl(t0, arg0);
770 tcg_gen_ext32u_tl(t1, arg1);
772 gen_op_cmp(t0, t1, s, crf);
777 static always_inline void gen_op_cmpi32(TCGv arg0, target_ulong arg1, int s, int crf)
779 TCGv t0 = tcg_const_local_tl(arg1);
780 gen_op_cmp32(arg0, t0, s, crf);
785 static always_inline void gen_set_Rc0 (DisasContext *ctx, TCGv reg)
787 #if defined(TARGET_PPC64)
789 gen_op_cmpi32(reg, 0, 1, 0);
792 gen_op_cmpi(reg, 0, 1, 0);
796 GEN_HANDLER(cmp, 0x1F, 0x00, 0x00, 0x00400000, PPC_INTEGER)
798 #if defined(TARGET_PPC64)
799 if (!(ctx->sf_mode && (ctx->opcode & 0x00200000)))
800 gen_op_cmp32(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rB(ctx->opcode)],
801 1, crfD(ctx->opcode));
804 gen_op_cmp(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rB(ctx->opcode)],
805 1, crfD(ctx->opcode));
809 GEN_HANDLER(cmpi, 0x0B, 0xFF, 0xFF, 0x00400000, PPC_INTEGER)
811 #if defined(TARGET_PPC64)
812 if (!(ctx->sf_mode && (ctx->opcode & 0x00200000)))
813 gen_op_cmpi32(cpu_gpr[rA(ctx->opcode)], SIMM(ctx->opcode),
814 1, crfD(ctx->opcode));
817 gen_op_cmpi(cpu_gpr[rA(ctx->opcode)], SIMM(ctx->opcode),
818 1, crfD(ctx->opcode));
822 GEN_HANDLER(cmpl, 0x1F, 0x00, 0x01, 0x00400000, PPC_INTEGER)
824 #if defined(TARGET_PPC64)
825 if (!(ctx->sf_mode && (ctx->opcode & 0x00200000)))
826 gen_op_cmp32(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rB(ctx->opcode)],
827 0, crfD(ctx->opcode));
830 gen_op_cmp(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rB(ctx->opcode)],
831 0, crfD(ctx->opcode));
835 GEN_HANDLER(cmpli, 0x0A, 0xFF, 0xFF, 0x00400000, PPC_INTEGER)
837 #if defined(TARGET_PPC64)
838 if (!(ctx->sf_mode && (ctx->opcode & 0x00200000)))
839 gen_op_cmpi32(cpu_gpr[rA(ctx->opcode)], UIMM(ctx->opcode),
840 0, crfD(ctx->opcode));
843 gen_op_cmpi(cpu_gpr[rA(ctx->opcode)], UIMM(ctx->opcode),
844 0, crfD(ctx->opcode));
847 /* isel (PowerPC 2.03 specification) */
848 GEN_HANDLER(isel, 0x1F, 0x0F, 0xFF, 0x00000001, PPC_ISEL)
851 uint32_t bi = rC(ctx->opcode);
855 l1 = gen_new_label();
856 l2 = gen_new_label();
858 mask = 1 << (3 - (bi & 0x03));
859 t0 = tcg_temp_new_i32();
860 tcg_gen_andi_i32(t0, cpu_crf[bi >> 2], mask);
861 tcg_gen_brcondi_i32(TCG_COND_EQ, t0, 0, l1);
862 if (rA(ctx->opcode) == 0)
863 tcg_gen_movi_tl(cpu_gpr[rD(ctx->opcode)], 0);
865 tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)]);
868 tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rB(ctx->opcode)]);
870 tcg_temp_free_i32(t0);
873 /*** Integer arithmetic ***/
875 static always_inline void gen_op_arith_compute_ov(DisasContext *ctx, TCGv arg0, TCGv arg1, TCGv arg2, int sub)
880 l1 = gen_new_label();
881 /* Start with XER OV disabled, the most likely case */
882 tcg_gen_andi_tl(cpu_xer, cpu_xer, ~(1 << XER_OV));
883 t0 = tcg_temp_local_new();
884 tcg_gen_xor_tl(t0, arg0, arg1);
885 #if defined(TARGET_PPC64)
887 tcg_gen_ext32s_tl(t0, t0);
890 tcg_gen_brcondi_tl(TCG_COND_LT, t0, 0, l1);
892 tcg_gen_brcondi_tl(TCG_COND_GE, t0, 0, l1);
893 tcg_gen_xor_tl(t0, arg1, arg2);
894 #if defined(TARGET_PPC64)
896 tcg_gen_ext32s_tl(t0, t0);
899 tcg_gen_brcondi_tl(TCG_COND_GE, t0, 0, l1);
901 tcg_gen_brcondi_tl(TCG_COND_LT, t0, 0, l1);
902 tcg_gen_ori_tl(cpu_xer, cpu_xer, (1 << XER_OV) | (1 << XER_SO));
907 static always_inline void gen_op_arith_compute_ca(DisasContext *ctx, TCGv arg1, TCGv arg2, int sub)
909 int l1 = gen_new_label();
911 #if defined(TARGET_PPC64)
912 if (!(ctx->sf_mode)) {
917 tcg_gen_ext32u_tl(t0, arg1);
918 tcg_gen_ext32u_tl(t1, arg2);
920 tcg_gen_brcond_tl(TCG_COND_GTU, t0, t1, l1);
922 tcg_gen_brcond_tl(TCG_COND_GEU, t0, t1, l1);
924 tcg_gen_ori_tl(cpu_xer, cpu_xer, 1 << XER_CA);
932 tcg_gen_brcond_tl(TCG_COND_GTU, arg1, arg2, l1);
934 tcg_gen_brcond_tl(TCG_COND_GEU, arg1, arg2, l1);
936 tcg_gen_ori_tl(cpu_xer, cpu_xer, 1 << XER_CA);
941 /* Common add function */
942 static always_inline void gen_op_arith_add(DisasContext *ctx, TCGv ret, TCGv arg1, TCGv arg2,
943 int add_ca, int compute_ca, int compute_ov)
947 if ((!compute_ca && !compute_ov) ||
948 (!TCGV_EQUAL(ret,arg1) && !TCGV_EQUAL(ret, arg2))) {
951 t0 = tcg_temp_local_new();
955 t1 = tcg_temp_local_new();
956 tcg_gen_andi_tl(t1, cpu_xer, (1 << XER_CA));
957 tcg_gen_shri_tl(t1, t1, XER_CA);
960 if (compute_ca && compute_ov) {
961 /* Start with XER CA and OV disabled, the most likely case */
962 tcg_gen_andi_tl(cpu_xer, cpu_xer, ~((1 << XER_CA) | (1 << XER_OV)));
963 } else if (compute_ca) {
964 /* Start with XER CA disabled, the most likely case */
965 tcg_gen_andi_tl(cpu_xer, cpu_xer, ~(1 << XER_CA));
966 } else if (compute_ov) {
967 /* Start with XER OV disabled, the most likely case */
968 tcg_gen_andi_tl(cpu_xer, cpu_xer, ~(1 << XER_OV));
971 tcg_gen_add_tl(t0, arg1, arg2);
974 gen_op_arith_compute_ca(ctx, t0, arg1, 0);
977 tcg_gen_add_tl(t0, t0, t1);
978 gen_op_arith_compute_ca(ctx, t0, t1, 0);
982 gen_op_arith_compute_ov(ctx, t0, arg1, arg2, 0);
985 if (unlikely(Rc(ctx->opcode) != 0))
986 gen_set_Rc0(ctx, t0);
988 if (!TCGV_EQUAL(t0, ret)) {
989 tcg_gen_mov_tl(ret, t0);
993 /* Add functions with two operands */
994 #define GEN_INT_ARITH_ADD(name, opc3, add_ca, compute_ca, compute_ov) \
995 GEN_HANDLER(name, 0x1F, 0x0A, opc3, 0x00000000, PPC_INTEGER) \
997 gen_op_arith_add(ctx, cpu_gpr[rD(ctx->opcode)], \
998 cpu_gpr[rA(ctx->opcode)], cpu_gpr[rB(ctx->opcode)], \
999 add_ca, compute_ca, compute_ov); \
1001 /* Add functions with one operand and one immediate */
1002 #define GEN_INT_ARITH_ADD_CONST(name, opc3, const_val, \
1003 add_ca, compute_ca, compute_ov) \
1004 GEN_HANDLER(name, 0x1F, 0x0A, opc3, 0x0000F800, PPC_INTEGER) \
1006 TCGv t0 = tcg_const_local_tl(const_val); \
1007 gen_op_arith_add(ctx, cpu_gpr[rD(ctx->opcode)], \
1008 cpu_gpr[rA(ctx->opcode)], t0, \
1009 add_ca, compute_ca, compute_ov); \
1010 tcg_temp_free(t0); \
1013 /* add add. addo addo. */
1014 GEN_INT_ARITH_ADD(add, 0x08, 0, 0, 0)
1015 GEN_INT_ARITH_ADD(addo, 0x18, 0, 0, 1)
1016 /* addc addc. addco addco. */
1017 GEN_INT_ARITH_ADD(addc, 0x00, 0, 1, 0)
1018 GEN_INT_ARITH_ADD(addco, 0x10, 0, 1, 1)
1019 /* adde adde. addeo addeo. */
1020 GEN_INT_ARITH_ADD(adde, 0x04, 1, 1, 0)
1021 GEN_INT_ARITH_ADD(addeo, 0x14, 1, 1, 1)
1022 /* addme addme. addmeo addmeo. */
1023 GEN_INT_ARITH_ADD_CONST(addme, 0x07, -1LL, 1, 1, 0)
1024 GEN_INT_ARITH_ADD_CONST(addmeo, 0x17, -1LL, 1, 1, 1)
1025 /* addze addze. addzeo addzeo.*/
1026 GEN_INT_ARITH_ADD_CONST(addze, 0x06, 0, 1, 1, 0)
1027 GEN_INT_ARITH_ADD_CONST(addzeo, 0x16, 0, 1, 1, 1)
1029 GEN_HANDLER(addi, 0x0E, 0xFF, 0xFF, 0x00000000, PPC_INTEGER)
1031 target_long simm = SIMM(ctx->opcode);
1033 if (rA(ctx->opcode) == 0) {
1035 tcg_gen_movi_tl(cpu_gpr[rD(ctx->opcode)], simm);
1037 tcg_gen_addi_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)], simm);
1041 static always_inline void gen_op_addic (DisasContext *ctx, TCGv ret, TCGv arg1,
1044 target_long simm = SIMM(ctx->opcode);
1046 /* Start with XER CA and OV disabled, the most likely case */
1047 tcg_gen_andi_tl(cpu_xer, cpu_xer, ~(1 << XER_CA));
1049 if (likely(simm != 0)) {
1050 TCGv t0 = tcg_temp_local_new();
1051 tcg_gen_addi_tl(t0, arg1, simm);
1052 gen_op_arith_compute_ca(ctx, t0, arg1, 0);
1053 tcg_gen_mov_tl(ret, t0);
1056 tcg_gen_mov_tl(ret, arg1);
1059 gen_set_Rc0(ctx, ret);
1062 GEN_HANDLER(addic, 0x0C, 0xFF, 0xFF, 0x00000000, PPC_INTEGER)
1064 gen_op_addic(ctx, cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)], 0);
1066 GEN_HANDLER2(addic_, "addic.", 0x0D, 0xFF, 0xFF, 0x00000000, PPC_INTEGER)
1068 gen_op_addic(ctx, cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)], 1);
1071 GEN_HANDLER(addis, 0x0F, 0xFF, 0xFF, 0x00000000, PPC_INTEGER)
1073 target_long simm = SIMM(ctx->opcode);
1075 if (rA(ctx->opcode) == 0) {
1077 tcg_gen_movi_tl(cpu_gpr[rD(ctx->opcode)], simm << 16);
1079 tcg_gen_addi_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)], simm << 16);
1083 static always_inline void gen_op_arith_divw (DisasContext *ctx, TCGv ret, TCGv arg1, TCGv arg2,
1084 int sign, int compute_ov)
1086 int l1 = gen_new_label();
1087 int l2 = gen_new_label();
1088 TCGv_i32 t0 = tcg_temp_local_new_i32();
1089 TCGv_i32 t1 = tcg_temp_local_new_i32();
1091 tcg_gen_trunc_tl_i32(t0, arg1);
1092 tcg_gen_trunc_tl_i32(t1, arg2);
1093 tcg_gen_brcondi_i32(TCG_COND_EQ, t1, 0, l1);
1095 int l3 = gen_new_label();
1096 tcg_gen_brcondi_i32(TCG_COND_NE, t1, -1, l3);
1097 tcg_gen_brcondi_i32(TCG_COND_EQ, t0, INT32_MIN, l1);
1099 tcg_gen_div_i32(t0, t0, t1);
1101 tcg_gen_divu_i32(t0, t0, t1);
1104 tcg_gen_andi_tl(cpu_xer, cpu_xer, ~(1 << XER_OV));
1109 tcg_gen_sari_i32(t0, t0, 31);
1111 tcg_gen_movi_i32(t0, 0);
1114 tcg_gen_ori_tl(cpu_xer, cpu_xer, (1 << XER_OV) | (1 << XER_SO));
1117 tcg_gen_extu_i32_tl(ret, t0);
1118 tcg_temp_free_i32(t0);
1119 tcg_temp_free_i32(t1);
1120 if (unlikely(Rc(ctx->opcode) != 0))
1121 gen_set_Rc0(ctx, ret);
1124 #define GEN_INT_ARITH_DIVW(name, opc3, sign, compute_ov) \
1125 GEN_HANDLER(name, 0x1F, 0x0B, opc3, 0x00000000, PPC_INTEGER) \
1127 gen_op_arith_divw(ctx, cpu_gpr[rD(ctx->opcode)], \
1128 cpu_gpr[rA(ctx->opcode)], cpu_gpr[rB(ctx->opcode)], \
1129 sign, compute_ov); \
1131 /* divwu divwu. divwuo divwuo. */
1132 GEN_INT_ARITH_DIVW(divwu, 0x0E, 0, 0);
1133 GEN_INT_ARITH_DIVW(divwuo, 0x1E, 0, 1);
1134 /* divw divw. divwo divwo. */
1135 GEN_INT_ARITH_DIVW(divw, 0x0F, 1, 0);
1136 GEN_INT_ARITH_DIVW(divwo, 0x1F, 1, 1);
1137 #if defined(TARGET_PPC64)
1138 static always_inline void gen_op_arith_divd (DisasContext *ctx, TCGv ret, TCGv arg1, TCGv arg2,
1139 int sign, int compute_ov)
1141 int l1 = gen_new_label();
1142 int l2 = gen_new_label();
1144 tcg_gen_brcondi_i64(TCG_COND_EQ, arg2, 0, l1);
1146 int l3 = gen_new_label();
1147 tcg_gen_brcondi_i64(TCG_COND_NE, arg2, -1, l3);
1148 tcg_gen_brcondi_i64(TCG_COND_EQ, arg1, INT64_MIN, l1);
1150 tcg_gen_div_i64(ret, arg1, arg2);
1152 tcg_gen_divu_i64(ret, arg1, arg2);
1155 tcg_gen_andi_tl(cpu_xer, cpu_xer, ~(1 << XER_OV));
1160 tcg_gen_sari_i64(ret, arg1, 63);
1162 tcg_gen_movi_i64(ret, 0);
1165 tcg_gen_ori_tl(cpu_xer, cpu_xer, (1 << XER_OV) | (1 << XER_SO));
1168 if (unlikely(Rc(ctx->opcode) != 0))
1169 gen_set_Rc0(ctx, ret);
1171 #define GEN_INT_ARITH_DIVD(name, opc3, sign, compute_ov) \
1172 GEN_HANDLER(name, 0x1F, 0x09, opc3, 0x00000000, PPC_64B) \
1174 gen_op_arith_divd(ctx, cpu_gpr[rD(ctx->opcode)], \
1175 cpu_gpr[rA(ctx->opcode)], cpu_gpr[rB(ctx->opcode)], \
1176 sign, compute_ov); \
1178 /* divwu divwu. divwuo divwuo. */
1179 GEN_INT_ARITH_DIVD(divdu, 0x0E, 0, 0);
1180 GEN_INT_ARITH_DIVD(divduo, 0x1E, 0, 1);
1181 /* divw divw. divwo divwo. */
1182 GEN_INT_ARITH_DIVD(divd, 0x0F, 1, 0);
1183 GEN_INT_ARITH_DIVD(divdo, 0x1F, 1, 1);
1187 GEN_HANDLER(mulhw, 0x1F, 0x0B, 0x02, 0x00000400, PPC_INTEGER)
1191 t0 = tcg_temp_new_i64();
1192 t1 = tcg_temp_new_i64();
1193 #if defined(TARGET_PPC64)
1194 tcg_gen_ext32s_tl(t0, cpu_gpr[rA(ctx->opcode)]);
1195 tcg_gen_ext32s_tl(t1, cpu_gpr[rB(ctx->opcode)]);
1196 tcg_gen_mul_i64(t0, t0, t1);
1197 tcg_gen_shri_i64(cpu_gpr[rD(ctx->opcode)], t0, 32);
1199 tcg_gen_ext_tl_i64(t0, cpu_gpr[rA(ctx->opcode)]);
1200 tcg_gen_ext_tl_i64(t1, cpu_gpr[rB(ctx->opcode)]);
1201 tcg_gen_mul_i64(t0, t0, t1);
1202 tcg_gen_shri_i64(t0, t0, 32);
1203 tcg_gen_trunc_i64_tl(cpu_gpr[rD(ctx->opcode)], t0);
1205 tcg_temp_free_i64(t0);
1206 tcg_temp_free_i64(t1);
1207 if (unlikely(Rc(ctx->opcode) != 0))
1208 gen_set_Rc0(ctx, cpu_gpr[rD(ctx->opcode)]);
1210 /* mulhwu mulhwu. */
1211 GEN_HANDLER(mulhwu, 0x1F, 0x0B, 0x00, 0x00000400, PPC_INTEGER)
1215 t0 = tcg_temp_new_i64();
1216 t1 = tcg_temp_new_i64();
1217 #if defined(TARGET_PPC64)
1218 tcg_gen_ext32u_i64(t0, cpu_gpr[rA(ctx->opcode)]);
1219 tcg_gen_ext32u_i64(t1, cpu_gpr[rB(ctx->opcode)]);
1220 tcg_gen_mul_i64(t0, t0, t1);
1221 tcg_gen_shri_i64(cpu_gpr[rD(ctx->opcode)], t0, 32);
1223 tcg_gen_extu_tl_i64(t0, cpu_gpr[rA(ctx->opcode)]);
1224 tcg_gen_extu_tl_i64(t1, cpu_gpr[rB(ctx->opcode)]);
1225 tcg_gen_mul_i64(t0, t0, t1);
1226 tcg_gen_shri_i64(t0, t0, 32);
1227 tcg_gen_trunc_i64_tl(cpu_gpr[rD(ctx->opcode)], t0);
1229 tcg_temp_free_i64(t0);
1230 tcg_temp_free_i64(t1);
1231 if (unlikely(Rc(ctx->opcode) != 0))
1232 gen_set_Rc0(ctx, cpu_gpr[rD(ctx->opcode)]);
1235 GEN_HANDLER(mullw, 0x1F, 0x0B, 0x07, 0x00000000, PPC_INTEGER)
1237 tcg_gen_mul_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)],
1238 cpu_gpr[rB(ctx->opcode)]);
1239 tcg_gen_ext32s_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rD(ctx->opcode)]);
1240 if (unlikely(Rc(ctx->opcode) != 0))
1241 gen_set_Rc0(ctx, cpu_gpr[rD(ctx->opcode)]);
1243 /* mullwo mullwo. */
1244 GEN_HANDLER(mullwo, 0x1F, 0x0B, 0x17, 0x00000000, PPC_INTEGER)
1249 t0 = tcg_temp_new_i64();
1250 t1 = tcg_temp_new_i64();
1251 l1 = gen_new_label();
1252 /* Start with XER OV disabled, the most likely case */
1253 tcg_gen_andi_tl(cpu_xer, cpu_xer, ~(1 << XER_OV));
1254 #if defined(TARGET_PPC64)
1255 tcg_gen_ext32s_i64(t0, cpu_gpr[rA(ctx->opcode)]);
1256 tcg_gen_ext32s_i64(t1, cpu_gpr[rB(ctx->opcode)]);
1258 tcg_gen_ext_tl_i64(t0, cpu_gpr[rA(ctx->opcode)]);
1259 tcg_gen_ext_tl_i64(t1, cpu_gpr[rB(ctx->opcode)]);
1261 tcg_gen_mul_i64(t0, t0, t1);
1262 #if defined(TARGET_PPC64)
1263 tcg_gen_ext32s_i64(cpu_gpr[rD(ctx->opcode)], t0);
1264 tcg_gen_brcond_i64(TCG_COND_EQ, t0, cpu_gpr[rD(ctx->opcode)], l1);
1266 tcg_gen_trunc_i64_tl(cpu_gpr[rD(ctx->opcode)], t0);
1267 tcg_gen_ext32s_i64(t1, t0);
1268 tcg_gen_brcond_i64(TCG_COND_EQ, t0, t1, l1);
1270 tcg_gen_ori_tl(cpu_xer, cpu_xer, (1 << XER_OV) | (1 << XER_SO));
1272 tcg_temp_free_i64(t0);
1273 tcg_temp_free_i64(t1);
1274 if (unlikely(Rc(ctx->opcode) != 0))
1275 gen_set_Rc0(ctx, cpu_gpr[rD(ctx->opcode)]);
1278 GEN_HANDLER(mulli, 0x07, 0xFF, 0xFF, 0x00000000, PPC_INTEGER)
1280 tcg_gen_muli_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)],
1283 #if defined(TARGET_PPC64)
1284 #define GEN_INT_ARITH_MUL_HELPER(name, opc3) \
1285 GEN_HANDLER(name, 0x1F, 0x09, opc3, 0x00000000, PPC_64B) \
1287 gen_helper_##name (cpu_gpr[rD(ctx->opcode)], \
1288 cpu_gpr[rA(ctx->opcode)], cpu_gpr[rB(ctx->opcode)]); \
1289 if (unlikely(Rc(ctx->opcode) != 0)) \
1290 gen_set_Rc0(ctx, cpu_gpr[rD(ctx->opcode)]); \
1293 GEN_INT_ARITH_MUL_HELPER(mulhdu, 0x00);
1294 /* mulhdu mulhdu. */
1295 GEN_INT_ARITH_MUL_HELPER(mulhd, 0x02);
1297 GEN_HANDLER(mulld, 0x1F, 0x09, 0x07, 0x00000000, PPC_64B)
1299 tcg_gen_mul_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)],
1300 cpu_gpr[rB(ctx->opcode)]);
1301 if (unlikely(Rc(ctx->opcode) != 0))
1302 gen_set_Rc0(ctx, cpu_gpr[rD(ctx->opcode)]);
1304 /* mulldo mulldo. */
1305 GEN_INT_ARITH_MUL_HELPER(mulldo, 0x17);
1308 /* neg neg. nego nego. */
1309 static always_inline void gen_op_arith_neg (DisasContext *ctx, TCGv ret, TCGv arg1, int ov_check)
1311 int l1 = gen_new_label();
1312 int l2 = gen_new_label();
1313 TCGv t0 = tcg_temp_local_new();
1314 #if defined(TARGET_PPC64)
1316 tcg_gen_mov_tl(t0, arg1);
1317 tcg_gen_brcondi_tl(TCG_COND_EQ, t0, INT64_MIN, l1);
1321 tcg_gen_ext32s_tl(t0, arg1);
1322 tcg_gen_brcondi_tl(TCG_COND_EQ, t0, INT32_MIN, l1);
1324 tcg_gen_neg_tl(ret, arg1);
1326 tcg_gen_andi_tl(cpu_xer, cpu_xer, ~(1 << XER_OV));
1330 tcg_gen_mov_tl(ret, t0);
1332 tcg_gen_ori_tl(cpu_xer, cpu_xer, (1 << XER_OV) | (1 << XER_SO));
1336 if (unlikely(Rc(ctx->opcode) != 0))
1337 gen_set_Rc0(ctx, ret);
1339 GEN_HANDLER(neg, 0x1F, 0x08, 0x03, 0x0000F800, PPC_INTEGER)
1341 gen_op_arith_neg(ctx, cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)], 0);
1343 GEN_HANDLER(nego, 0x1F, 0x08, 0x13, 0x0000F800, PPC_INTEGER)
1345 gen_op_arith_neg(ctx, cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)], 1);
1348 /* Common subf function */
1349 static always_inline void gen_op_arith_subf(DisasContext *ctx, TCGv ret, TCGv arg1, TCGv arg2,
1350 int add_ca, int compute_ca, int compute_ov)
1354 if ((!compute_ca && !compute_ov) ||
1355 (!TCGV_EQUAL(ret, arg1) && !TCGV_EQUAL(ret, arg2))) {
1358 t0 = tcg_temp_local_new();
1362 t1 = tcg_temp_local_new();
1363 tcg_gen_andi_tl(t1, cpu_xer, (1 << XER_CA));
1364 tcg_gen_shri_tl(t1, t1, XER_CA);
1367 if (compute_ca && compute_ov) {
1368 /* Start with XER CA and OV disabled, the most likely case */
1369 tcg_gen_andi_tl(cpu_xer, cpu_xer, ~((1 << XER_CA) | (1 << XER_OV)));
1370 } else if (compute_ca) {
1371 /* Start with XER CA disabled, the most likely case */
1372 tcg_gen_andi_tl(cpu_xer, cpu_xer, ~(1 << XER_CA));
1373 } else if (compute_ov) {
1374 /* Start with XER OV disabled, the most likely case */
1375 tcg_gen_andi_tl(cpu_xer, cpu_xer, ~(1 << XER_OV));
1379 tcg_gen_not_tl(t0, arg1);
1380 tcg_gen_add_tl(t0, t0, arg2);
1381 gen_op_arith_compute_ca(ctx, t0, arg2, 0);
1382 tcg_gen_add_tl(t0, t0, t1);
1383 gen_op_arith_compute_ca(ctx, t0, t1, 0);
1386 tcg_gen_sub_tl(t0, arg2, arg1);
1388 gen_op_arith_compute_ca(ctx, t0, arg2, 1);
1392 gen_op_arith_compute_ov(ctx, t0, arg1, arg2, 1);
1395 if (unlikely(Rc(ctx->opcode) != 0))
1396 gen_set_Rc0(ctx, t0);
1398 if (!TCGV_EQUAL(t0, ret)) {
1399 tcg_gen_mov_tl(ret, t0);
1403 /* Sub functions with Two operands functions */
1404 #define GEN_INT_ARITH_SUBF(name, opc3, add_ca, compute_ca, compute_ov) \
1405 GEN_HANDLER(name, 0x1F, 0x08, opc3, 0x00000000, PPC_INTEGER) \
1407 gen_op_arith_subf(ctx, cpu_gpr[rD(ctx->opcode)], \
1408 cpu_gpr[rA(ctx->opcode)], cpu_gpr[rB(ctx->opcode)], \
1409 add_ca, compute_ca, compute_ov); \
1411 /* Sub functions with one operand and one immediate */
1412 #define GEN_INT_ARITH_SUBF_CONST(name, opc3, const_val, \
1413 add_ca, compute_ca, compute_ov) \
1414 GEN_HANDLER(name, 0x1F, 0x08, opc3, 0x0000F800, PPC_INTEGER) \
1416 TCGv t0 = tcg_const_local_tl(const_val); \
1417 gen_op_arith_subf(ctx, cpu_gpr[rD(ctx->opcode)], \
1418 cpu_gpr[rA(ctx->opcode)], t0, \
1419 add_ca, compute_ca, compute_ov); \
1420 tcg_temp_free(t0); \
1422 /* subf subf. subfo subfo. */
1423 GEN_INT_ARITH_SUBF(subf, 0x01, 0, 0, 0)
1424 GEN_INT_ARITH_SUBF(subfo, 0x11, 0, 0, 1)
1425 /* subfc subfc. subfco subfco. */
1426 GEN_INT_ARITH_SUBF(subfc, 0x00, 0, 1, 0)
1427 GEN_INT_ARITH_SUBF(subfco, 0x10, 0, 1, 1)
1428 /* subfe subfe. subfeo subfo. */
1429 GEN_INT_ARITH_SUBF(subfe, 0x04, 1, 1, 0)
1430 GEN_INT_ARITH_SUBF(subfeo, 0x14, 1, 1, 1)
1431 /* subfme subfme. subfmeo subfmeo. */
1432 GEN_INT_ARITH_SUBF_CONST(subfme, 0x07, -1LL, 1, 1, 0)
1433 GEN_INT_ARITH_SUBF_CONST(subfmeo, 0x17, -1LL, 1, 1, 1)
1434 /* subfze subfze. subfzeo subfzeo.*/
1435 GEN_INT_ARITH_SUBF_CONST(subfze, 0x06, 0, 1, 1, 0)
1436 GEN_INT_ARITH_SUBF_CONST(subfzeo, 0x16, 0, 1, 1, 1)
1438 GEN_HANDLER(subfic, 0x08, 0xFF, 0xFF, 0x00000000, PPC_INTEGER)
1440 /* Start with XER CA and OV disabled, the most likely case */
1441 tcg_gen_andi_tl(cpu_xer, cpu_xer, ~(1 << XER_CA));
1442 TCGv t0 = tcg_temp_local_new();
1443 TCGv t1 = tcg_const_local_tl(SIMM(ctx->opcode));
1444 tcg_gen_sub_tl(t0, t1, cpu_gpr[rA(ctx->opcode)]);
1445 gen_op_arith_compute_ca(ctx, t0, t1, 1);
1447 tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], t0);
1451 /*** Integer logical ***/
1452 #define GEN_LOGICAL2(name, tcg_op, opc, type) \
1453 GEN_HANDLER(name, 0x1F, 0x1C, opc, 0x00000000, type) \
1455 tcg_op(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)], \
1456 cpu_gpr[rB(ctx->opcode)]); \
1457 if (unlikely(Rc(ctx->opcode) != 0)) \
1458 gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]); \
1461 #define GEN_LOGICAL1(name, tcg_op, opc, type) \
1462 GEN_HANDLER(name, 0x1F, 0x1A, opc, 0x00000000, type) \
1464 tcg_op(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)]); \
1465 if (unlikely(Rc(ctx->opcode) != 0)) \
1466 gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]); \
1470 GEN_LOGICAL2(and, tcg_gen_and_tl, 0x00, PPC_INTEGER);
1472 GEN_LOGICAL2(andc, tcg_gen_andc_tl, 0x01, PPC_INTEGER);
1474 GEN_HANDLER2(andi_, "andi.", 0x1C, 0xFF, 0xFF, 0x00000000, PPC_INTEGER)
1476 tcg_gen_andi_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)], UIMM(ctx->opcode));
1477 gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
1480 GEN_HANDLER2(andis_, "andis.", 0x1D, 0xFF, 0xFF, 0x00000000, PPC_INTEGER)
1482 tcg_gen_andi_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)], UIMM(ctx->opcode) << 16);
1483 gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
1486 GEN_HANDLER(cntlzw, 0x1F, 0x1A, 0x00, 0x00000000, PPC_INTEGER)
1488 gen_helper_cntlzw(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)]);
1489 if (unlikely(Rc(ctx->opcode) != 0))
1490 gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
1493 GEN_LOGICAL2(eqv, tcg_gen_eqv_tl, 0x08, PPC_INTEGER);
1494 /* extsb & extsb. */
1495 GEN_LOGICAL1(extsb, tcg_gen_ext8s_tl, 0x1D, PPC_INTEGER);
1496 /* extsh & extsh. */
1497 GEN_LOGICAL1(extsh, tcg_gen_ext16s_tl, 0x1C, PPC_INTEGER);
1499 GEN_LOGICAL2(nand, tcg_gen_nand_tl, 0x0E, PPC_INTEGER);
1501 GEN_LOGICAL2(nor, tcg_gen_nor_tl, 0x03, PPC_INTEGER);
1503 GEN_HANDLER(or, 0x1F, 0x1C, 0x0D, 0x00000000, PPC_INTEGER)
1507 rs = rS(ctx->opcode);
1508 ra = rA(ctx->opcode);
1509 rb = rB(ctx->opcode);
1510 /* Optimisation for mr. ri case */
1511 if (rs != ra || rs != rb) {
1513 tcg_gen_or_tl(cpu_gpr[ra], cpu_gpr[rs], cpu_gpr[rb]);
1515 tcg_gen_mov_tl(cpu_gpr[ra], cpu_gpr[rs]);
1516 if (unlikely(Rc(ctx->opcode) != 0))
1517 gen_set_Rc0(ctx, cpu_gpr[ra]);
1518 } else if (unlikely(Rc(ctx->opcode) != 0)) {
1519 gen_set_Rc0(ctx, cpu_gpr[rs]);
1520 #if defined(TARGET_PPC64)
1526 /* Set process priority to low */
1530 /* Set process priority to medium-low */
1534 /* Set process priority to normal */
1537 #if !defined(CONFIG_USER_ONLY)
1539 if (ctx->supervisor > 0) {
1540 /* Set process priority to very low */
1545 if (ctx->supervisor > 0) {
1546 /* Set process priority to medium-hight */
1551 if (ctx->supervisor > 0) {
1552 /* Set process priority to high */
1557 if (ctx->supervisor > 1) {
1558 /* Set process priority to very high */
1568 TCGv t0 = tcg_temp_new();
1569 tcg_gen_ld_tl(t0, cpu_env, offsetof(CPUState, spr[SPR_PPR]));
1570 tcg_gen_andi_tl(t0, t0, ~0x001C000000000000ULL);
1571 tcg_gen_ori_tl(t0, t0, ((uint64_t)prio) << 50);
1572 tcg_gen_st_tl(t0, cpu_env, offsetof(CPUState, spr[SPR_PPR]));
1579 GEN_LOGICAL2(orc, tcg_gen_orc_tl, 0x0C, PPC_INTEGER);
1581 GEN_HANDLER(xor, 0x1F, 0x1C, 0x09, 0x00000000, PPC_INTEGER)
1583 /* Optimisation for "set to zero" case */
1584 if (rS(ctx->opcode) != rB(ctx->opcode))
1585 tcg_gen_xor_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)], cpu_gpr[rB(ctx->opcode)]);
1587 tcg_gen_movi_tl(cpu_gpr[rA(ctx->opcode)], 0);
1588 if (unlikely(Rc(ctx->opcode) != 0))
1589 gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
1592 GEN_HANDLER(ori, 0x18, 0xFF, 0xFF, 0x00000000, PPC_INTEGER)
1594 target_ulong uimm = UIMM(ctx->opcode);
1596 if (rS(ctx->opcode) == rA(ctx->opcode) && uimm == 0) {
1598 /* XXX: should handle special NOPs for POWER series */
1601 tcg_gen_ori_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)], uimm);
1604 GEN_HANDLER(oris, 0x19, 0xFF, 0xFF, 0x00000000, PPC_INTEGER)
1606 target_ulong uimm = UIMM(ctx->opcode);
1608 if (rS(ctx->opcode) == rA(ctx->opcode) && uimm == 0) {
1612 tcg_gen_ori_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)], uimm << 16);
1615 GEN_HANDLER(xori, 0x1A, 0xFF, 0xFF, 0x00000000, PPC_INTEGER)
1617 target_ulong uimm = UIMM(ctx->opcode);
1619 if (rS(ctx->opcode) == rA(ctx->opcode) && uimm == 0) {
1623 tcg_gen_xori_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)], uimm);
1626 GEN_HANDLER(xoris, 0x1B, 0xFF, 0xFF, 0x00000000, PPC_INTEGER)
1628 target_ulong uimm = UIMM(ctx->opcode);
1630 if (rS(ctx->opcode) == rA(ctx->opcode) && uimm == 0) {
1634 tcg_gen_xori_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)], uimm << 16);
1636 /* popcntb : PowerPC 2.03 specification */
1637 GEN_HANDLER(popcntb, 0x1F, 0x03, 0x03, 0x0000F801, PPC_POPCNTB)
1639 #if defined(TARGET_PPC64)
1641 gen_helper_popcntb_64(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)]);
1644 gen_helper_popcntb(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)]);
1647 #if defined(TARGET_PPC64)
1648 /* extsw & extsw. */
1649 GEN_LOGICAL1(extsw, tcg_gen_ext32s_tl, 0x1E, PPC_64B);
1651 GEN_HANDLER(cntlzd, 0x1F, 0x1A, 0x01, 0x00000000, PPC_64B)
1653 gen_helper_cntlzd(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)]);
1654 if (unlikely(Rc(ctx->opcode) != 0))
1655 gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
1659 /*** Integer rotate ***/
1660 /* rlwimi & rlwimi. */
1661 GEN_HANDLER(rlwimi, 0x14, 0xFF, 0xFF, 0x00000000, PPC_INTEGER)
1663 uint32_t mb, me, sh;
1665 mb = MB(ctx->opcode);
1666 me = ME(ctx->opcode);
1667 sh = SH(ctx->opcode);
1668 if (likely(sh == 0 && mb == 0 && me == 31)) {
1669 tcg_gen_ext32u_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)]);
1673 TCGv t0 = tcg_temp_new();
1674 #if defined(TARGET_PPC64)
1675 TCGv_i32 t2 = tcg_temp_new_i32();
1676 tcg_gen_trunc_i64_i32(t2, cpu_gpr[rS(ctx->opcode)]);
1677 tcg_gen_rotli_i32(t2, t2, sh);
1678 tcg_gen_extu_i32_i64(t0, t2);
1679 tcg_temp_free_i32(t2);
1681 tcg_gen_rotli_i32(t0, cpu_gpr[rS(ctx->opcode)], sh);
1683 #if defined(TARGET_PPC64)
1687 mask = MASK(mb, me);
1688 t1 = tcg_temp_new();
1689 tcg_gen_andi_tl(t0, t0, mask);
1690 tcg_gen_andi_tl(t1, cpu_gpr[rA(ctx->opcode)], ~mask);
1691 tcg_gen_or_tl(cpu_gpr[rA(ctx->opcode)], t0, t1);
1695 if (unlikely(Rc(ctx->opcode) != 0))
1696 gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
1698 /* rlwinm & rlwinm. */
1699 GEN_HANDLER(rlwinm, 0x15, 0xFF, 0xFF, 0x00000000, PPC_INTEGER)
1701 uint32_t mb, me, sh;
1703 sh = SH(ctx->opcode);
1704 mb = MB(ctx->opcode);
1705 me = ME(ctx->opcode);
1707 if (likely(mb == 0 && me == (31 - sh))) {
1708 if (likely(sh == 0)) {
1709 tcg_gen_ext32u_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)]);
1711 TCGv t0 = tcg_temp_new();
1712 tcg_gen_ext32u_tl(t0, cpu_gpr[rS(ctx->opcode)]);
1713 tcg_gen_shli_tl(t0, t0, sh);
1714 tcg_gen_ext32u_tl(cpu_gpr[rA(ctx->opcode)], t0);
1717 } else if (likely(sh != 0 && me == 31 && sh == (32 - mb))) {
1718 TCGv t0 = tcg_temp_new();
1719 tcg_gen_ext32u_tl(t0, cpu_gpr[rS(ctx->opcode)]);
1720 tcg_gen_shri_tl(t0, t0, mb);
1721 tcg_gen_ext32u_tl(cpu_gpr[rA(ctx->opcode)], t0);
1724 TCGv t0 = tcg_temp_new();
1725 #if defined(TARGET_PPC64)
1726 TCGv_i32 t1 = tcg_temp_new_i32();
1727 tcg_gen_trunc_i64_i32(t1, cpu_gpr[rS(ctx->opcode)]);
1728 tcg_gen_rotli_i32(t1, t1, sh);
1729 tcg_gen_extu_i32_i64(t0, t1);
1730 tcg_temp_free_i32(t1);
1732 tcg_gen_rotli_i32(t0, cpu_gpr[rS(ctx->opcode)], sh);
1734 #if defined(TARGET_PPC64)
1738 tcg_gen_andi_tl(cpu_gpr[rA(ctx->opcode)], t0, MASK(mb, me));
1741 if (unlikely(Rc(ctx->opcode) != 0))
1742 gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
1744 /* rlwnm & rlwnm. */
1745 GEN_HANDLER(rlwnm, 0x17, 0xFF, 0xFF, 0x00000000, PPC_INTEGER)
1749 #if defined(TARGET_PPC64)
1753 mb = MB(ctx->opcode);
1754 me = ME(ctx->opcode);
1755 t0 = tcg_temp_new();
1756 tcg_gen_andi_tl(t0, cpu_gpr[rB(ctx->opcode)], 0x1f);
1757 #if defined(TARGET_PPC64)
1758 t1 = tcg_temp_new_i32();
1759 t2 = tcg_temp_new_i32();
1760 tcg_gen_trunc_i64_i32(t1, cpu_gpr[rS(ctx->opcode)]);
1761 tcg_gen_trunc_i64_i32(t2, t0);
1762 tcg_gen_rotl_i32(t1, t1, t2);
1763 tcg_gen_extu_i32_i64(t0, t1);
1764 tcg_temp_free_i32(t1);
1765 tcg_temp_free_i32(t2);
1767 tcg_gen_rotl_i32(t0, cpu_gpr[rS(ctx->opcode)], t0);
1769 if (unlikely(mb != 0 || me != 31)) {
1770 #if defined(TARGET_PPC64)
1774 tcg_gen_andi_tl(cpu_gpr[rA(ctx->opcode)], t0, MASK(mb, me));
1776 tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], t0);
1779 if (unlikely(Rc(ctx->opcode) != 0))
1780 gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
1783 #if defined(TARGET_PPC64)
1784 #define GEN_PPC64_R2(name, opc1, opc2) \
1785 GEN_HANDLER2(name##0, stringify(name), opc1, opc2, 0xFF, 0x00000000, PPC_64B) \
1787 gen_##name(ctx, 0); \
1789 GEN_HANDLER2(name##1, stringify(name), opc1, opc2 | 0x10, 0xFF, 0x00000000, \
1792 gen_##name(ctx, 1); \
1794 #define GEN_PPC64_R4(name, opc1, opc2) \
1795 GEN_HANDLER2(name##0, stringify(name), opc1, opc2, 0xFF, 0x00000000, PPC_64B) \
1797 gen_##name(ctx, 0, 0); \
1799 GEN_HANDLER2(name##1, stringify(name), opc1, opc2 | 0x01, 0xFF, 0x00000000, \
1802 gen_##name(ctx, 0, 1); \
1804 GEN_HANDLER2(name##2, stringify(name), opc1, opc2 | 0x10, 0xFF, 0x00000000, \
1807 gen_##name(ctx, 1, 0); \
1809 GEN_HANDLER2(name##3, stringify(name), opc1, opc2 | 0x11, 0xFF, 0x00000000, \
1812 gen_##name(ctx, 1, 1); \
1815 static always_inline void gen_rldinm (DisasContext *ctx, uint32_t mb,
1816 uint32_t me, uint32_t sh)
1818 if (likely(sh != 0 && mb == 0 && me == (63 - sh))) {
1819 tcg_gen_shli_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)], sh);
1820 } else if (likely(sh != 0 && me == 63 && sh == (64 - mb))) {
1821 tcg_gen_shri_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)], mb);
1823 TCGv t0 = tcg_temp_new();
1824 tcg_gen_rotli_tl(t0, cpu_gpr[rS(ctx->opcode)], sh);
1825 if (likely(mb == 0 && me == 63)) {
1826 tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], t0);
1828 tcg_gen_andi_tl(cpu_gpr[rA(ctx->opcode)], t0, MASK(mb, me));
1832 if (unlikely(Rc(ctx->opcode) != 0))
1833 gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
1835 /* rldicl - rldicl. */
1836 static always_inline void gen_rldicl (DisasContext *ctx, int mbn, int shn)
1840 sh = SH(ctx->opcode) | (shn << 5);
1841 mb = MB(ctx->opcode) | (mbn << 5);
1842 gen_rldinm(ctx, mb, 63, sh);
1844 GEN_PPC64_R4(rldicl, 0x1E, 0x00);
1845 /* rldicr - rldicr. */
1846 static always_inline void gen_rldicr (DisasContext *ctx, int men, int shn)
1850 sh = SH(ctx->opcode) | (shn << 5);
1851 me = MB(ctx->opcode) | (men << 5);
1852 gen_rldinm(ctx, 0, me, sh);
1854 GEN_PPC64_R4(rldicr, 0x1E, 0x02);
1855 /* rldic - rldic. */
1856 static always_inline void gen_rldic (DisasContext *ctx, int mbn, int shn)
1860 sh = SH(ctx->opcode) | (shn << 5);
1861 mb = MB(ctx->opcode) | (mbn << 5);
1862 gen_rldinm(ctx, mb, 63 - sh, sh);
1864 GEN_PPC64_R4(rldic, 0x1E, 0x04);
1866 static always_inline void gen_rldnm (DisasContext *ctx, uint32_t mb,
1871 mb = MB(ctx->opcode);
1872 me = ME(ctx->opcode);
1873 t0 = tcg_temp_new();
1874 tcg_gen_andi_tl(t0, cpu_gpr[rB(ctx->opcode)], 0x3f);
1875 tcg_gen_rotl_tl(t0, cpu_gpr[rS(ctx->opcode)], t0);
1876 if (unlikely(mb != 0 || me != 63)) {
1877 tcg_gen_andi_tl(cpu_gpr[rA(ctx->opcode)], t0, MASK(mb, me));
1879 tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], t0);
1882 if (unlikely(Rc(ctx->opcode) != 0))
1883 gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
1886 /* rldcl - rldcl. */
1887 static always_inline void gen_rldcl (DisasContext *ctx, int mbn)
1891 mb = MB(ctx->opcode) | (mbn << 5);
1892 gen_rldnm(ctx, mb, 63);
1894 GEN_PPC64_R2(rldcl, 0x1E, 0x08);
1895 /* rldcr - rldcr. */
1896 static always_inline void gen_rldcr (DisasContext *ctx, int men)
1900 me = MB(ctx->opcode) | (men << 5);
1901 gen_rldnm(ctx, 0, me);
1903 GEN_PPC64_R2(rldcr, 0x1E, 0x09);
1904 /* rldimi - rldimi. */
1905 static always_inline void gen_rldimi (DisasContext *ctx, int mbn, int shn)
1907 uint32_t sh, mb, me;
1909 sh = SH(ctx->opcode) | (shn << 5);
1910 mb = MB(ctx->opcode) | (mbn << 5);
1912 if (unlikely(sh == 0 && mb == 0)) {
1913 tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)]);
1918 t0 = tcg_temp_new();
1919 tcg_gen_rotli_tl(t0, cpu_gpr[rS(ctx->opcode)], sh);
1920 t1 = tcg_temp_new();
1921 mask = MASK(mb, me);
1922 tcg_gen_andi_tl(t0, t0, mask);
1923 tcg_gen_andi_tl(t1, cpu_gpr[rA(ctx->opcode)], ~mask);
1924 tcg_gen_or_tl(cpu_gpr[rA(ctx->opcode)], t0, t1);
1928 if (unlikely(Rc(ctx->opcode) != 0))
1929 gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
1931 GEN_PPC64_R4(rldimi, 0x1E, 0x06);
1934 /*** Integer shift ***/
1936 GEN_HANDLER(slw, 0x1F, 0x18, 0x00, 0x00000000, PPC_INTEGER)
1940 l1 = gen_new_label();
1941 l2 = gen_new_label();
1943 t0 = tcg_temp_local_new();
1944 tcg_gen_andi_tl(t0, cpu_gpr[rB(ctx->opcode)], 0x3f);
1945 tcg_gen_brcondi_tl(TCG_COND_LT, t0, 0x20, l1);
1946 tcg_gen_movi_tl(cpu_gpr[rA(ctx->opcode)], 0);
1949 tcg_gen_shl_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)], t0);
1950 tcg_gen_ext32u_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rA(ctx->opcode)]);
1953 if (unlikely(Rc(ctx->opcode) != 0))
1954 gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
1957 GEN_HANDLER(sraw, 0x1F, 0x18, 0x18, 0x00000000, PPC_INTEGER)
1959 gen_helper_sraw(cpu_gpr[rA(ctx->opcode)],
1960 cpu_gpr[rS(ctx->opcode)], cpu_gpr[rB(ctx->opcode)]);
1961 if (unlikely(Rc(ctx->opcode) != 0))
1962 gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
1964 /* srawi & srawi. */
1965 GEN_HANDLER(srawi, 0x1F, 0x18, 0x19, 0x00000000, PPC_INTEGER)
1967 int sh = SH(ctx->opcode);
1971 l1 = gen_new_label();
1972 l2 = gen_new_label();
1973 t0 = tcg_temp_local_new();
1974 tcg_gen_ext32s_tl(t0, cpu_gpr[rS(ctx->opcode)]);
1975 tcg_gen_brcondi_tl(TCG_COND_GE, t0, 0, l1);
1976 tcg_gen_andi_tl(t0, cpu_gpr[rS(ctx->opcode)], (1ULL << sh) - 1);
1977 tcg_gen_brcondi_tl(TCG_COND_EQ, t0, 0, l1);
1978 tcg_gen_ori_tl(cpu_xer, cpu_xer, 1 << XER_CA);
1981 tcg_gen_andi_tl(cpu_xer, cpu_xer, ~(1 << XER_CA));
1983 tcg_gen_ext32s_tl(t0, cpu_gpr[rS(ctx->opcode)]);
1984 tcg_gen_sari_tl(cpu_gpr[rA(ctx->opcode)], t0, sh);
1987 tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)]);
1988 tcg_gen_andi_tl(cpu_xer, cpu_xer, ~(1 << XER_CA));
1990 if (unlikely(Rc(ctx->opcode) != 0))
1991 gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
1994 GEN_HANDLER(srw, 0x1F, 0x18, 0x10, 0x00000000, PPC_INTEGER)
1998 l1 = gen_new_label();
1999 l2 = gen_new_label();
2001 t0 = tcg_temp_local_new();
2002 tcg_gen_andi_tl(t0, cpu_gpr[rB(ctx->opcode)], 0x3f);
2003 tcg_gen_brcondi_tl(TCG_COND_LT, t0, 0x20, l1);
2004 tcg_gen_movi_tl(cpu_gpr[rA(ctx->opcode)], 0);
2007 t1 = tcg_temp_new();
2008 tcg_gen_ext32u_tl(t1, cpu_gpr[rS(ctx->opcode)]);
2009 tcg_gen_shr_tl(cpu_gpr[rA(ctx->opcode)], t1, t0);
2013 if (unlikely(Rc(ctx->opcode) != 0))
2014 gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
2016 #if defined(TARGET_PPC64)
2018 GEN_HANDLER(sld, 0x1F, 0x1B, 0x00, 0x00000000, PPC_64B)
2022 l1 = gen_new_label();
2023 l2 = gen_new_label();
2025 t0 = tcg_temp_local_new();
2026 tcg_gen_andi_tl(t0, cpu_gpr[rB(ctx->opcode)], 0x7f);
2027 tcg_gen_brcondi_tl(TCG_COND_LT, t0, 0x40, l1);
2028 tcg_gen_movi_tl(cpu_gpr[rA(ctx->opcode)], 0);
2031 tcg_gen_shl_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)], t0);
2034 if (unlikely(Rc(ctx->opcode) != 0))
2035 gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
2038 GEN_HANDLER(srad, 0x1F, 0x1A, 0x18, 0x00000000, PPC_64B)
2040 gen_helper_srad(cpu_gpr[rA(ctx->opcode)],
2041 cpu_gpr[rS(ctx->opcode)], cpu_gpr[rB(ctx->opcode)]);
2042 if (unlikely(Rc(ctx->opcode) != 0))
2043 gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
2045 /* sradi & sradi. */
2046 static always_inline void gen_sradi (DisasContext *ctx, int n)
2048 int sh = SH(ctx->opcode) + (n << 5);
2052 l1 = gen_new_label();
2053 l2 = gen_new_label();
2054 t0 = tcg_temp_local_new();
2055 tcg_gen_brcondi_tl(TCG_COND_GE, cpu_gpr[rS(ctx->opcode)], 0, l1);
2056 tcg_gen_andi_tl(t0, cpu_gpr[rS(ctx->opcode)], (1ULL << sh) - 1);
2057 tcg_gen_brcondi_tl(TCG_COND_EQ, t0, 0, l1);
2058 tcg_gen_ori_tl(cpu_xer, cpu_xer, 1 << XER_CA);
2061 tcg_gen_andi_tl(cpu_xer, cpu_xer, ~(1 << XER_CA));
2064 tcg_gen_sari_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)], sh);
2066 tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)]);
2067 tcg_gen_andi_tl(cpu_xer, cpu_xer, ~(1 << XER_CA));
2069 if (unlikely(Rc(ctx->opcode) != 0))
2070 gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
2072 GEN_HANDLER2(sradi0, "sradi", 0x1F, 0x1A, 0x19, 0x00000000, PPC_64B)
2076 GEN_HANDLER2(sradi1, "sradi", 0x1F, 0x1B, 0x19, 0x00000000, PPC_64B)
2081 GEN_HANDLER(srd, 0x1F, 0x1B, 0x10, 0x00000000, PPC_64B)
2085 l1 = gen_new_label();
2086 l2 = gen_new_label();
2088 t0 = tcg_temp_local_new();
2089 tcg_gen_andi_tl(t0, cpu_gpr[rB(ctx->opcode)], 0x7f);
2090 tcg_gen_brcondi_tl(TCG_COND_LT, t0, 0x40, l1);
2091 tcg_gen_movi_tl(cpu_gpr[rA(ctx->opcode)], 0);
2094 tcg_gen_shr_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)], t0);
2097 if (unlikely(Rc(ctx->opcode) != 0))
2098 gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
2102 /*** Floating-Point arithmetic ***/
2103 #define _GEN_FLOAT_ACB(name, op, op1, op2, isfloat, set_fprf, type) \
2104 GEN_HANDLER(f##name, op1, op2, 0xFF, 0x00000000, type) \
2106 if (unlikely(!ctx->fpu_enabled)) { \
2107 GEN_EXCP_NO_FP(ctx); \
2110 gen_reset_fpstatus(); \
2111 gen_helper_f##op(cpu_fpr[rD(ctx->opcode)], cpu_fpr[rA(ctx->opcode)], \
2112 cpu_fpr[rC(ctx->opcode)], cpu_fpr[rB(ctx->opcode)]); \
2114 gen_helper_frsp(cpu_fpr[rD(ctx->opcode)], cpu_fpr[rD(ctx->opcode)]); \
2116 gen_compute_fprf(cpu_fpr[rD(ctx->opcode)], set_fprf, \
2117 Rc(ctx->opcode) != 0); \
2120 #define GEN_FLOAT_ACB(name, op2, set_fprf, type) \
2121 _GEN_FLOAT_ACB(name, name, 0x3F, op2, 0, set_fprf, type); \
2122 _GEN_FLOAT_ACB(name##s, name, 0x3B, op2, 1, set_fprf, type);
2124 #define _GEN_FLOAT_AB(name, op, op1, op2, inval, isfloat, set_fprf, type) \
2125 GEN_HANDLER(f##name, op1, op2, 0xFF, inval, type) \
2127 if (unlikely(!ctx->fpu_enabled)) { \
2128 GEN_EXCP_NO_FP(ctx); \
2131 gen_reset_fpstatus(); \
2132 gen_helper_f##op(cpu_fpr[rD(ctx->opcode)], cpu_fpr[rA(ctx->opcode)], \
2133 cpu_fpr[rB(ctx->opcode)]); \
2135 gen_helper_frsp(cpu_fpr[rD(ctx->opcode)], cpu_fpr[rD(ctx->opcode)]); \
2137 gen_compute_fprf(cpu_fpr[rD(ctx->opcode)], \
2138 set_fprf, Rc(ctx->opcode) != 0); \
2140 #define GEN_FLOAT_AB(name, op2, inval, set_fprf, type) \
2141 _GEN_FLOAT_AB(name, name, 0x3F, op2, inval, 0, set_fprf, type); \
2142 _GEN_FLOAT_AB(name##s, name, 0x3B, op2, inval, 1, set_fprf, type);
2144 #define _GEN_FLOAT_AC(name, op, op1, op2, inval, isfloat, set_fprf, type) \
2145 GEN_HANDLER(f##name, op1, op2, 0xFF, inval, type) \
2147 if (unlikely(!ctx->fpu_enabled)) { \
2148 GEN_EXCP_NO_FP(ctx); \
2151 gen_reset_fpstatus(); \
2152 gen_helper_f##op(cpu_fpr[rD(ctx->opcode)], cpu_fpr[rA(ctx->opcode)], \
2153 cpu_fpr[rC(ctx->opcode)]); \
2155 gen_helper_frsp(cpu_fpr[rD(ctx->opcode)], cpu_fpr[rD(ctx->opcode)]); \
2157 gen_compute_fprf(cpu_fpr[rD(ctx->opcode)], \
2158 set_fprf, Rc(ctx->opcode) != 0); \
2160 #define GEN_FLOAT_AC(name, op2, inval, set_fprf, type) \
2161 _GEN_FLOAT_AC(name, name, 0x3F, op2, inval, 0, set_fprf, type); \
2162 _GEN_FLOAT_AC(name##s, name, 0x3B, op2, inval, 1, set_fprf, type);
2164 #define GEN_FLOAT_B(name, op2, op3, set_fprf, type) \
2165 GEN_HANDLER(f##name, 0x3F, op2, op3, 0x001F0000, type) \
2167 if (unlikely(!ctx->fpu_enabled)) { \
2168 GEN_EXCP_NO_FP(ctx); \
2171 gen_reset_fpstatus(); \
2172 gen_helper_f##name(cpu_fpr[rD(ctx->opcode)], cpu_fpr[rB(ctx->opcode)]); \
2173 gen_compute_fprf(cpu_fpr[rD(ctx->opcode)], \
2174 set_fprf, Rc(ctx->opcode) != 0); \
2177 #define GEN_FLOAT_BS(name, op1, op2, set_fprf, type) \
2178 GEN_HANDLER(f##name, op1, op2, 0xFF, 0x001F07C0, type) \
2180 if (unlikely(!ctx->fpu_enabled)) { \
2181 GEN_EXCP_NO_FP(ctx); \
2184 gen_reset_fpstatus(); \
2185 gen_helper_f##name(cpu_fpr[rD(ctx->opcode)], cpu_fpr[rB(ctx->opcode)]); \
2186 gen_compute_fprf(cpu_fpr[rD(ctx->opcode)], \
2187 set_fprf, Rc(ctx->opcode) != 0); \
2191 GEN_FLOAT_AB(add, 0x15, 0x000007C0, 1, PPC_FLOAT);
2193 GEN_FLOAT_AB(div, 0x12, 0x000007C0, 1, PPC_FLOAT);
2195 GEN_FLOAT_AC(mul, 0x19, 0x0000F800, 1, PPC_FLOAT);
2198 GEN_FLOAT_BS(re, 0x3F, 0x18, 1, PPC_FLOAT_EXT);
2201 GEN_FLOAT_BS(res, 0x3B, 0x18, 1, PPC_FLOAT_FRES);
2204 GEN_FLOAT_BS(rsqrte, 0x3F, 0x1A, 1, PPC_FLOAT_FRSQRTE);
2207 GEN_HANDLER(frsqrtes, 0x3B, 0x1A, 0xFF, 0x001F07C0, PPC_FLOAT_FRSQRTES)
2209 if (unlikely(!ctx->fpu_enabled)) {
2210 GEN_EXCP_NO_FP(ctx);
2213 gen_reset_fpstatus();
2214 gen_helper_frsqrte(cpu_fpr[rD(ctx->opcode)], cpu_fpr[rB(ctx->opcode)]);
2215 gen_helper_frsp(cpu_fpr[rD(ctx->opcode)], cpu_fpr[rD(ctx->opcode)]);
2216 gen_compute_fprf(cpu_fpr[rD(ctx->opcode)], 1, Rc(ctx->opcode) != 0);
2220 _GEN_FLOAT_ACB(sel, sel, 0x3F, 0x17, 0, 0, PPC_FLOAT_FSEL);
2222 GEN_FLOAT_AB(sub, 0x14, 0x000007C0, 1, PPC_FLOAT);
2225 GEN_HANDLER(fsqrt, 0x3F, 0x16, 0xFF, 0x001F07C0, PPC_FLOAT_FSQRT)
2227 if (unlikely(!ctx->fpu_enabled)) {
2228 GEN_EXCP_NO_FP(ctx);
2231 gen_reset_fpstatus();
2232 gen_helper_fsqrt(cpu_fpr[rD(ctx->opcode)], cpu_fpr[rB(ctx->opcode)]);
2233 gen_compute_fprf(cpu_fpr[rD(ctx->opcode)], 1, Rc(ctx->opcode) != 0);
2236 GEN_HANDLER(fsqrts, 0x3B, 0x16, 0xFF, 0x001F07C0, PPC_FLOAT_FSQRT)
2238 if (unlikely(!ctx->fpu_enabled)) {
2239 GEN_EXCP_NO_FP(ctx);
2242 gen_reset_fpstatus();
2243 gen_helper_fsqrt(cpu_fpr[rD(ctx->opcode)], cpu_fpr[rB(ctx->opcode)]);
2244 gen_helper_frsp(cpu_fpr[rD(ctx->opcode)], cpu_fpr[rD(ctx->opcode)]);
2245 gen_compute_fprf(cpu_fpr[rD(ctx->opcode)], 1, Rc(ctx->opcode) != 0);
2248 /*** Floating-Point multiply-and-add ***/
2249 /* fmadd - fmadds */
2250 GEN_FLOAT_ACB(madd, 0x1D, 1, PPC_FLOAT);
2251 /* fmsub - fmsubs */
2252 GEN_FLOAT_ACB(msub, 0x1C, 1, PPC_FLOAT);
2253 /* fnmadd - fnmadds */
2254 GEN_FLOAT_ACB(nmadd, 0x1F, 1, PPC_FLOAT);
2255 /* fnmsub - fnmsubs */
2256 GEN_FLOAT_ACB(nmsub, 0x1E, 1, PPC_FLOAT);
2258 /*** Floating-Point round & convert ***/
2260 GEN_FLOAT_B(ctiw, 0x0E, 0x00, 0, PPC_FLOAT);
2262 GEN_FLOAT_B(ctiwz, 0x0F, 0x00, 0, PPC_FLOAT);
2264 GEN_FLOAT_B(rsp, 0x0C, 0x00, 1, PPC_FLOAT);
2265 #if defined(TARGET_PPC64)
2267 GEN_FLOAT_B(cfid, 0x0E, 0x1A, 1, PPC_64B);
2269 GEN_FLOAT_B(ctid, 0x0E, 0x19, 0, PPC_64B);
2271 GEN_FLOAT_B(ctidz, 0x0F, 0x19, 0, PPC_64B);
2275 GEN_FLOAT_B(rin, 0x08, 0x0C, 1, PPC_FLOAT_EXT);
2277 GEN_FLOAT_B(riz, 0x08, 0x0D, 1, PPC_FLOAT_EXT);
2279 GEN_FLOAT_B(rip, 0x08, 0x0E, 1, PPC_FLOAT_EXT);
2281 GEN_FLOAT_B(rim, 0x08, 0x0F, 1, PPC_FLOAT_EXT);
2283 /*** Floating-Point compare ***/
2285 GEN_HANDLER(fcmpo, 0x3F, 0x00, 0x01, 0x00600001, PPC_FLOAT)
2287 if (unlikely(!ctx->fpu_enabled)) {
2288 GEN_EXCP_NO_FP(ctx);
2291 gen_reset_fpstatus();
2292 gen_helper_fcmpo(cpu_crf[crfD(ctx->opcode)],
2293 cpu_fpr[rA(ctx->opcode)], cpu_fpr[rB(ctx->opcode)]);
2294 gen_helper_float_check_status();
2298 GEN_HANDLER(fcmpu, 0x3F, 0x00, 0x00, 0x00600001, PPC_FLOAT)
2300 if (unlikely(!ctx->fpu_enabled)) {
2301 GEN_EXCP_NO_FP(ctx);
2304 gen_reset_fpstatus();
2305 gen_helper_fcmpu(cpu_crf[crfD(ctx->opcode)],
2306 cpu_fpr[rA(ctx->opcode)], cpu_fpr[rB(ctx->opcode)]);
2307 gen_helper_float_check_status();
2310 /*** Floating-point move ***/
2312 /* XXX: beware that fabs never checks for NaNs nor update FPSCR */
2313 GEN_FLOAT_B(abs, 0x08, 0x08, 0, PPC_FLOAT);
2316 /* XXX: beware that fmr never checks for NaNs nor update FPSCR */
2317 GEN_HANDLER(fmr, 0x3F, 0x08, 0x02, 0x001F0000, PPC_FLOAT)
2319 if (unlikely(!ctx->fpu_enabled)) {
2320 GEN_EXCP_NO_FP(ctx);
2323 tcg_gen_mov_i64(cpu_fpr[rD(ctx->opcode)], cpu_fpr[rB(ctx->opcode)]);
2324 gen_compute_fprf(cpu_fpr[rD(ctx->opcode)], 0, Rc(ctx->opcode) != 0);
2328 /* XXX: beware that fnabs never checks for NaNs nor update FPSCR */
2329 GEN_FLOAT_B(nabs, 0x08, 0x04, 0, PPC_FLOAT);
2331 /* XXX: beware that fneg never checks for NaNs nor update FPSCR */
2332 GEN_FLOAT_B(neg, 0x08, 0x01, 0, PPC_FLOAT);
2334 /*** Floating-Point status & ctrl register ***/
2336 GEN_HANDLER(mcrfs, 0x3F, 0x00, 0x02, 0x0063F801, PPC_FLOAT)
2340 if (unlikely(!ctx->fpu_enabled)) {
2341 GEN_EXCP_NO_FP(ctx);
2344 gen_optimize_fprf();
2345 bfa = 4 * (7 - crfS(ctx->opcode));
2346 tcg_gen_shri_i32(cpu_crf[crfD(ctx->opcode)], cpu_fpscr, bfa);
2347 tcg_gen_andi_i32(cpu_crf[crfD(ctx->opcode)], cpu_crf[crfD(ctx->opcode)], 0xf);
2348 tcg_gen_andi_i32(cpu_fpscr, cpu_fpscr, ~(0xF << bfa));
2352 GEN_HANDLER(mffs, 0x3F, 0x07, 0x12, 0x001FF800, PPC_FLOAT)
2354 if (unlikely(!ctx->fpu_enabled)) {
2355 GEN_EXCP_NO_FP(ctx);
2358 gen_optimize_fprf();
2359 gen_reset_fpstatus();
2360 tcg_gen_extu_i32_i64(cpu_fpr[rD(ctx->opcode)], cpu_fpscr);
2361 gen_compute_fprf(cpu_fpr[rD(ctx->opcode)], 0, Rc(ctx->opcode) != 0);
2365 GEN_HANDLER(mtfsb0, 0x3F, 0x06, 0x02, 0x001FF800, PPC_FLOAT)
2369 if (unlikely(!ctx->fpu_enabled)) {
2370 GEN_EXCP_NO_FP(ctx);
2373 crb = 32 - (crbD(ctx->opcode) >> 2);
2374 gen_optimize_fprf();
2375 gen_reset_fpstatus();
2376 if (likely(crb != 30 && crb != 29))
2377 tcg_gen_andi_i32(cpu_fpscr, cpu_fpscr, ~(1 << crb));
2378 if (unlikely(Rc(ctx->opcode) != 0)) {
2379 tcg_gen_shri_i32(cpu_crf[1], cpu_fpscr, FPSCR_OX);
2384 GEN_HANDLER(mtfsb1, 0x3F, 0x06, 0x01, 0x001FF800, PPC_FLOAT)
2388 if (unlikely(!ctx->fpu_enabled)) {
2389 GEN_EXCP_NO_FP(ctx);
2392 crb = 32 - (crbD(ctx->opcode) >> 2);
2393 gen_optimize_fprf();
2394 gen_reset_fpstatus();
2395 /* XXX: we pretend we can only do IEEE floating-point computations */
2396 if (likely(crb != FPSCR_FEX && crb != FPSCR_VX && crb != FPSCR_NI)) {
2397 TCGv_i32 t0 = tcg_const_i32(crb);
2398 gen_helper_fpscr_setbit(t0);
2399 tcg_temp_free_i32(t0);
2401 if (unlikely(Rc(ctx->opcode) != 0)) {
2402 tcg_gen_shri_i32(cpu_crf[1], cpu_fpscr, FPSCR_OX);
2404 /* We can raise a differed exception */
2405 gen_helper_float_check_status();
2409 GEN_HANDLER(mtfsf, 0x3F, 0x07, 0x16, 0x02010000, PPC_FLOAT)
2413 if (unlikely(!ctx->fpu_enabled)) {
2414 GEN_EXCP_NO_FP(ctx);
2417 gen_optimize_fprf();
2418 gen_reset_fpstatus();
2419 t0 = tcg_const_i32(FM(ctx->opcode));
2420 gen_helper_store_fpscr(cpu_fpr[rB(ctx->opcode)], t0);
2421 tcg_temp_free_i32(t0);
2422 if (unlikely(Rc(ctx->opcode) != 0)) {
2423 tcg_gen_shri_i32(cpu_crf[1], cpu_fpscr, FPSCR_OX);
2425 /* We can raise a differed exception */
2426 gen_helper_float_check_status();
2430 GEN_HANDLER(mtfsfi, 0x3F, 0x06, 0x04, 0x006f0800, PPC_FLOAT)
2436 if (unlikely(!ctx->fpu_enabled)) {
2437 GEN_EXCP_NO_FP(ctx);
2440 bf = crbD(ctx->opcode) >> 2;
2442 gen_optimize_fprf();
2443 gen_reset_fpstatus();
2444 t0 = tcg_const_i64(FPIMM(ctx->opcode) << (4 * sh));
2445 t1 = tcg_const_i32(1 << sh);
2446 gen_helper_store_fpscr(t0, t1);
2447 tcg_temp_free_i64(t0);
2448 tcg_temp_free_i32(t1);
2449 if (unlikely(Rc(ctx->opcode) != 0)) {
2450 tcg_gen_shri_i32(cpu_crf[1], cpu_fpscr, FPSCR_OX);
2452 /* We can raise a differed exception */
2453 gen_helper_float_check_status();
2456 /*** Addressing modes ***/
2457 /* Register indirect with immediate index : EA = (rA|0) + SIMM */
2458 static always_inline void gen_addr_imm_index (TCGv EA,
2462 target_long simm = SIMM(ctx->opcode);
2465 if (rA(ctx->opcode) == 0)
2466 tcg_gen_movi_tl(EA, simm);
2467 else if (likely(simm != 0))
2468 tcg_gen_addi_tl(EA, cpu_gpr[rA(ctx->opcode)], simm);
2470 tcg_gen_mov_tl(EA, cpu_gpr[rA(ctx->opcode)]);
2473 static always_inline void gen_addr_reg_index (TCGv EA,
2476 if (rA(ctx->opcode) == 0)
2477 tcg_gen_mov_tl(EA, cpu_gpr[rB(ctx->opcode)]);
2479 tcg_gen_add_tl(EA, cpu_gpr[rA(ctx->opcode)], cpu_gpr[rB(ctx->opcode)]);
2482 static always_inline void gen_addr_register (TCGv EA,
2485 if (rA(ctx->opcode) == 0)
2486 tcg_gen_movi_tl(EA, 0);
2488 tcg_gen_mov_tl(EA, cpu_gpr[rA(ctx->opcode)]);
2491 #if defined(TARGET_PPC64)
2492 #define _GEN_MEM_FUNCS(name, mode) \
2493 &gen_op_##name##_##mode, \
2494 &gen_op_##name##_le_##mode, \
2495 &gen_op_##name##_64_##mode, \
2496 &gen_op_##name##_le_64_##mode
2498 #define _GEN_MEM_FUNCS(name, mode) \
2499 &gen_op_##name##_##mode, \
2500 &gen_op_##name##_le_##mode
2502 #if defined(CONFIG_USER_ONLY)
2503 #if defined(TARGET_PPC64)
2504 #define NB_MEM_FUNCS 4
2506 #define NB_MEM_FUNCS 2
2508 #define GEN_MEM_FUNCS(name) \
2509 _GEN_MEM_FUNCS(name, raw)
2511 #if defined(TARGET_PPC64)
2512 #define NB_MEM_FUNCS 12
2514 #define NB_MEM_FUNCS 6
2516 #define GEN_MEM_FUNCS(name) \
2517 _GEN_MEM_FUNCS(name, user), \
2518 _GEN_MEM_FUNCS(name, kernel), \
2519 _GEN_MEM_FUNCS(name, hypv)
2522 /*** Integer load ***/
2523 #if defined(TARGET_PPC64)
2524 #define GEN_QEMU_LD_PPC64(width) \
2525 static always_inline void gen_qemu_ld##width##_ppc64(TCGv t0, TCGv t1, int flags)\
2527 if (likely(flags & 2)) \
2528 tcg_gen_qemu_ld##width(t0, t1, flags >> 2); \
2530 TCGv addr = tcg_temp_new(); \
2531 tcg_gen_ext32u_tl(addr, t1); \
2532 tcg_gen_qemu_ld##width(t0, addr, flags >> 2); \
2533 tcg_temp_free(addr); \
2536 GEN_QEMU_LD_PPC64(8u)
2537 GEN_QEMU_LD_PPC64(8s)
2538 GEN_QEMU_LD_PPC64(16u)
2539 GEN_QEMU_LD_PPC64(16s)
2540 GEN_QEMU_LD_PPC64(32u)
2541 GEN_QEMU_LD_PPC64(32s)
2542 GEN_QEMU_LD_PPC64(64)
2544 #define GEN_QEMU_ST_PPC64(width) \
2545 static always_inline void gen_qemu_st##width##_ppc64(TCGv t0, TCGv t1, int flags)\
2547 if (likely(flags & 2)) \
2548 tcg_gen_qemu_st##width(t0, t1, flags >> 2); \
2550 TCGv addr = tcg_temp_new(); \
2551 tcg_gen_ext32u_tl(addr, t1); \
2552 tcg_gen_qemu_st##width(t0, addr, flags >> 2); \
2553 tcg_temp_free(addr); \
2556 GEN_QEMU_ST_PPC64(8)
2557 GEN_QEMU_ST_PPC64(16)
2558 GEN_QEMU_ST_PPC64(32)
2559 GEN_QEMU_ST_PPC64(64)
2561 static always_inline void gen_qemu_ld8u(TCGv arg0, TCGv arg1, int flags)
2563 gen_qemu_ld8u_ppc64(arg0, arg1, flags);
2566 static always_inline void gen_qemu_ld8s(TCGv arg0, TCGv arg1, int flags)
2568 gen_qemu_ld8s_ppc64(arg0, arg1, flags);
2571 static always_inline void gen_qemu_ld16u(TCGv arg0, TCGv arg1, int flags)
2573 if (unlikely(flags & 1)) {
2575 gen_qemu_ld16u_ppc64(arg0, arg1, flags);
2576 t0 = tcg_temp_new_i32();
2577 tcg_gen_trunc_tl_i32(t0, arg0);
2578 tcg_gen_bswap16_i32(t0, t0);
2579 tcg_gen_extu_i32_tl(arg0, t0);
2580 tcg_temp_free_i32(t0);
2582 gen_qemu_ld16u_ppc64(arg0, arg1, flags);
2585 static always_inline void gen_qemu_ld16s(TCGv arg0, TCGv arg1, int flags)
2587 if (unlikely(flags & 1)) {
2589 gen_qemu_ld16u_ppc64(arg0, arg1, flags);
2590 t0 = tcg_temp_new_i32();
2591 tcg_gen_trunc_tl_i32(t0, arg0);
2592 tcg_gen_bswap16_i32(t0, t0);
2593 tcg_gen_extu_i32_tl(arg0, t0);
2594 tcg_gen_ext16s_tl(arg0, arg0);
2595 tcg_temp_free_i32(t0);
2597 gen_qemu_ld16s_ppc64(arg0, arg1, flags);
2600 static always_inline void gen_qemu_ld32u(TCGv arg0, TCGv arg1, int flags)
2602 if (unlikely(flags & 1)) {
2604 gen_qemu_ld32u_ppc64(arg0, arg1, flags);
2605 t0 = tcg_temp_new_i32();
2606 tcg_gen_trunc_tl_i32(t0, arg0);
2607 tcg_gen_bswap_i32(t0, t0);
2608 tcg_gen_extu_i32_tl(arg0, t0);
2609 tcg_temp_free_i32(t0);
2611 gen_qemu_ld32u_ppc64(arg0, arg1, flags);
2614 static always_inline void gen_qemu_ld32s(TCGv arg0, TCGv arg1, int flags)
2616 if (unlikely(flags & 1)) {
2618 gen_qemu_ld32u_ppc64(arg0, arg1, flags);
2619 t0 = tcg_temp_new_i32();
2620 tcg_gen_trunc_tl_i32(t0, arg0);
2621 tcg_gen_bswap_i32(t0, t0);
2622 tcg_gen_ext_i32_tl(arg0, t0);
2623 tcg_temp_free_i32(t0);
2625 gen_qemu_ld32s_ppc64(arg0, arg1, flags);
2628 static always_inline void gen_qemu_ld64(TCGv arg0, TCGv arg1, int flags)
2630 gen_qemu_ld64_ppc64(arg0, arg1, flags);
2631 if (unlikely(flags & 1))
2632 tcg_gen_bswap_i64(arg0, arg0);
2635 static always_inline void gen_qemu_st8(TCGv arg0, TCGv arg1, int flags)
2637 gen_qemu_st8_ppc64(arg0, arg1, flags);
2640 static always_inline void gen_qemu_st16(TCGv arg0, TCGv arg1, int flags)
2642 if (unlikely(flags & 1)) {
2645 t0 = tcg_temp_new_i32();
2646 tcg_gen_trunc_tl_i32(t0, arg0);
2647 tcg_gen_ext16u_i32(t0, t0);
2648 tcg_gen_bswap16_i32(t0, t0);
2649 t1 = tcg_temp_new_i64();
2650 tcg_gen_extu_i32_tl(t1, t0);
2651 tcg_temp_free_i32(t0);
2652 gen_qemu_st16_ppc64(t1, arg1, flags);
2653 tcg_temp_free_i64(t1);
2655 gen_qemu_st16_ppc64(arg0, arg1, flags);
2658 static always_inline void gen_qemu_st32(TCGv arg0, TCGv arg1, int flags)
2660 if (unlikely(flags & 1)) {
2663 t0 = tcg_temp_new_i32();
2664 tcg_gen_trunc_tl_i32(t0, arg0);
2665 tcg_gen_bswap_i32(t0, t0);
2666 t1 = tcg_temp_new_i64();
2667 tcg_gen_extu_i32_tl(t1, t0);
2668 tcg_temp_free_i32(t0);
2669 gen_qemu_st32_ppc64(t1, arg1, flags);
2670 tcg_temp_free_i64(t1);
2672 gen_qemu_st32_ppc64(arg0, arg1, flags);
2675 static always_inline void gen_qemu_st64(TCGv arg0, TCGv arg1, int flags)
2677 if (unlikely(flags & 1)) {
2678 TCGv_i64 t0 = tcg_temp_new_i64();
2679 tcg_gen_bswap_i64(t0, arg0);
2680 gen_qemu_st64_ppc64(t0, arg1, flags);
2681 tcg_temp_free_i64(t0);
2683 gen_qemu_st64_ppc64(arg0, arg1, flags);
2687 #else /* defined(TARGET_PPC64) */
2688 #define GEN_QEMU_LD_PPC32(width) \
2689 static always_inline void gen_qemu_ld##width##_ppc32(TCGv arg0, TCGv arg1, int flags) \
2691 tcg_gen_qemu_ld##width(arg0, arg1, flags >> 1); \
2693 GEN_QEMU_LD_PPC32(8u)
2694 GEN_QEMU_LD_PPC32(8s)
2695 GEN_QEMU_LD_PPC32(16u)
2696 GEN_QEMU_LD_PPC32(16s)
2697 GEN_QEMU_LD_PPC32(32u)
2698 GEN_QEMU_LD_PPC32(32s)
2699 static always_inline void gen_qemu_ld64_ppc32(TCGv_i64 arg0, TCGv arg1, int flags)
2701 tcg_gen_qemu_ld64(arg0, arg1, flags >> 1);
2704 #define GEN_QEMU_ST_PPC32(width) \
2705 static always_inline void gen_qemu_st##width##_ppc32(TCGv arg0, TCGv arg1, int flags) \
2707 tcg_gen_qemu_st##width(arg0, arg1, flags >> 1); \
2709 GEN_QEMU_ST_PPC32(8)
2710 GEN_QEMU_ST_PPC32(16)
2711 GEN_QEMU_ST_PPC32(32)
2712 static always_inline void gen_qemu_st64_ppc32(TCGv_i64 arg0, TCGv arg1, int flags)
2714 tcg_gen_qemu_st64(arg0, arg1, flags >> 1);
2717 static always_inline void gen_qemu_ld8u(TCGv arg0, TCGv arg1, int flags)
2719 gen_qemu_ld8u_ppc32(arg0, arg1, flags >> 1);
2722 static always_inline void gen_qemu_ld8s(TCGv arg0, TCGv arg1, int flags)
2724 gen_qemu_ld8s_ppc32(arg0, arg1, flags >> 1);
2727 static always_inline void gen_qemu_ld16u(TCGv arg0, TCGv arg1, int flags)
2729 gen_qemu_ld16u_ppc32(arg0, arg1, flags >> 1);
2730 if (unlikely(flags & 1))
2731 tcg_gen_bswap16_i32(arg0, arg0);
2734 static always_inline void gen_qemu_ld16s(TCGv arg0, TCGv arg1, int flags)
2736 if (unlikely(flags & 1)) {
2737 gen_qemu_ld16u_ppc32(arg0, arg1, flags);
2738 tcg_gen_bswap16_i32(arg0, arg0);
2739 tcg_gen_ext16s_i32(arg0, arg0);
2741 gen_qemu_ld16s_ppc32(arg0, arg1, flags);
2744 static always_inline void gen_qemu_ld32u(TCGv arg0, TCGv arg1, int flags)
2746 gen_qemu_ld32u_ppc32(arg0, arg1, flags);
2747 if (unlikely(flags & 1))
2748 tcg_gen_bswap_i32(arg0, arg0);
2751 static always_inline void gen_qemu_ld64(TCGv_i64 arg0, TCGv arg1, int flags)
2753 gen_qemu_ld64_ppc32(arg0, arg1, flags);
2754 if (unlikely(flags & 1))
2755 tcg_gen_bswap_i64(arg0, arg0);
2758 static always_inline void gen_qemu_st8(TCGv arg0, TCGv arg1, int flags)
2760 gen_qemu_st8_ppc32(arg0, arg1, flags);
2763 static always_inline void gen_qemu_st16(TCGv arg0, TCGv arg1, int flags)
2765 if (unlikely(flags & 1)) {
2766 TCGv_i32 temp = tcg_temp_new_i32();
2767 tcg_gen_ext16u_i32(temp, arg0);
2768 tcg_gen_bswap16_i32(temp, temp);
2769 gen_qemu_st16_ppc32(temp, arg1, flags);
2770 tcg_temp_free_i32(temp);
2772 gen_qemu_st16_ppc32(arg0, arg1, flags);
2775 static always_inline void gen_qemu_st32(TCGv arg0, TCGv arg1, int flags)
2777 if (unlikely(flags & 1)) {
2778 TCGv_i32 temp = tcg_temp_new_i32();
2779 tcg_gen_bswap_i32(temp, arg0);
2780 gen_qemu_st32_ppc32(temp, arg1, flags);
2781 tcg_temp_free_i32(temp);
2783 gen_qemu_st32_ppc32(arg0, arg1, flags);
2786 static always_inline void gen_qemu_st64(TCGv_i64 arg0, TCGv arg1, int flags)
2788 if (unlikely(flags & 1)) {
2789 TCGv_i64 temp = tcg_temp_new_i64();
2790 tcg_gen_bswap_i64(temp, arg0);
2791 gen_qemu_st64_ppc32(temp, arg1, flags);
2792 tcg_temp_free_i64(temp);
2794 gen_qemu_st64_ppc32(arg0, arg1, flags);
2798 #define GEN_LD(name, ldop, opc, type) \
2799 GEN_HANDLER(name, opc, 0xFF, 0xFF, 0x00000000, type) \
2801 TCGv EA = tcg_temp_new(); \
2802 gen_set_access_type(ACCESS_INT); \
2803 gen_addr_imm_index(EA, ctx, 0); \
2804 gen_qemu_##ldop(cpu_gpr[rD(ctx->opcode)], EA, ctx->mem_idx); \
2805 tcg_temp_free(EA); \
2808 #define GEN_LDU(name, ldop, opc, type) \
2809 GEN_HANDLER(name##u, opc, 0xFF, 0xFF, 0x00000000, type) \
2812 if (unlikely(rA(ctx->opcode) == 0 || \
2813 rA(ctx->opcode) == rD(ctx->opcode))) { \
2814 GEN_EXCP_INVAL(ctx); \
2817 EA = tcg_temp_new(); \
2818 gen_set_access_type(ACCESS_INT); \
2819 if (type == PPC_64B) \
2820 gen_addr_imm_index(EA, ctx, 0x03); \
2822 gen_addr_imm_index(EA, ctx, 0); \
2823 gen_qemu_##ldop(cpu_gpr[rD(ctx->opcode)], EA, ctx->mem_idx); \
2824 tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], EA); \
2825 tcg_temp_free(EA); \
2828 #define GEN_LDUX(name, ldop, opc2, opc3, type) \
2829 GEN_HANDLER(name##ux, 0x1F, opc2, opc3, 0x00000001, type) \
2832 if (unlikely(rA(ctx->opcode) == 0 || \
2833 rA(ctx->opcode) == rD(ctx->opcode))) { \
2834 GEN_EXCP_INVAL(ctx); \
2837 EA = tcg_temp_new(); \
2838 gen_set_access_type(ACCESS_INT); \
2839 gen_addr_reg_index(EA, ctx); \
2840 gen_qemu_##ldop(cpu_gpr[rD(ctx->opcode)], EA, ctx->mem_idx); \
2841 tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], EA); \
2842 tcg_temp_free(EA); \
2845 #define GEN_LDX(name, ldop, opc2, opc3, type) \
2846 GEN_HANDLER(name##x, 0x1F, opc2, opc3, 0x00000001, type) \
2848 TCGv EA = tcg_temp_new(); \
2849 gen_set_access_type(ACCESS_INT); \
2850 gen_addr_reg_index(EA, ctx); \
2851 gen_qemu_##ldop(cpu_gpr[rD(ctx->opcode)], EA, ctx->mem_idx); \
2852 tcg_temp_free(EA); \
2855 #define GEN_LDS(name, ldop, op, type) \
2856 GEN_LD(name, ldop, op | 0x20, type); \
2857 GEN_LDU(name, ldop, op | 0x21, type); \
2858 GEN_LDUX(name, ldop, 0x17, op | 0x01, type); \
2859 GEN_LDX(name, ldop, 0x17, op | 0x00, type)
2861 /* lbz lbzu lbzux lbzx */
2862 GEN_LDS(lbz, ld8u, 0x02, PPC_INTEGER);
2863 /* lha lhau lhaux lhax */
2864 GEN_LDS(lha, ld16s, 0x0A, PPC_INTEGER);
2865 /* lhz lhzu lhzux lhzx */
2866 GEN_LDS(lhz, ld16u, 0x08, PPC_INTEGER);
2867 /* lwz lwzu lwzux lwzx */
2868 GEN_LDS(lwz, ld32u, 0x00, PPC_INTEGER);
2869 #if defined(TARGET_PPC64)
2871 GEN_LDUX(lwa, ld32s, 0x15, 0x0B, PPC_64B);
2873 GEN_LDX(lwa, ld32s, 0x15, 0x0A, PPC_64B);
2875 GEN_LDUX(ld, ld64, 0x15, 0x01, PPC_64B);
2877 GEN_LDX(ld, ld64, 0x15, 0x00, PPC_64B);
2878 GEN_HANDLER(ld, 0x3A, 0xFF, 0xFF, 0x00000000, PPC_64B)
2881 if (Rc(ctx->opcode)) {
2882 if (unlikely(rA(ctx->opcode) == 0 ||
2883 rA(ctx->opcode) == rD(ctx->opcode))) {
2884 GEN_EXCP_INVAL(ctx);
2888 EA = tcg_temp_new();
2889 gen_set_access_type(ACCESS_INT);
2890 gen_addr_imm_index(EA, ctx, 0x03);
2891 if (ctx->opcode & 0x02) {
2892 /* lwa (lwau is undefined) */
2893 gen_qemu_ld32s(cpu_gpr[rD(ctx->opcode)], EA, ctx->mem_idx);
2896 gen_qemu_ld64(cpu_gpr[rD(ctx->opcode)], EA, ctx->mem_idx);
2898 if (Rc(ctx->opcode))
2899 tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], EA);
2903 GEN_HANDLER(lq, 0x38, 0xFF, 0xFF, 0x00000000, PPC_64BX)
2905 #if defined(CONFIG_USER_ONLY)
2906 GEN_EXCP_PRIVOPC(ctx);
2911 /* Restore CPU state */
2912 if (unlikely(ctx->supervisor == 0)) {
2913 GEN_EXCP_PRIVOPC(ctx);
2916 ra = rA(ctx->opcode);
2917 rd = rD(ctx->opcode);
2918 if (unlikely((rd & 1) || rd == ra)) {
2919 GEN_EXCP_INVAL(ctx);
2922 if (unlikely(ctx->mem_idx & 1)) {
2923 /* Little-endian mode is not handled */
2924 GEN_EXCP(ctx, POWERPC_EXCP_ALIGN, POWERPC_EXCP_ALIGN_LE);
2927 EA = tcg_temp_new();
2928 gen_set_access_type(ACCESS_INT);
2929 gen_addr_imm_index(EA, ctx, 0x0F);
2930 gen_qemu_ld64(cpu_gpr[rd], EA, ctx->mem_idx);
2931 tcg_gen_addi_tl(EA, EA, 8);
2932 gen_qemu_ld64(cpu_gpr[rd+1], EA, ctx->mem_idx);
2938 /*** Integer store ***/
2939 #define GEN_ST(name, stop, opc, type) \
2940 GEN_HANDLER(name, opc, 0xFF, 0xFF, 0x00000000, type) \
2942 TCGv EA = tcg_temp_new(); \
2943 gen_set_access_type(ACCESS_INT); \
2944 gen_addr_imm_index(EA, ctx, 0); \
2945 gen_qemu_##stop(cpu_gpr[rS(ctx->opcode)], EA, ctx->mem_idx); \
2946 tcg_temp_free(EA); \
2949 #define GEN_STU(name, stop, opc, type) \
2950 GEN_HANDLER(stop##u, opc, 0xFF, 0xFF, 0x00000000, type) \
2953 if (unlikely(rA(ctx->opcode) == 0)) { \
2954 GEN_EXCP_INVAL(ctx); \
2957 EA = tcg_temp_new(); \
2958 gen_set_access_type(ACCESS_INT); \
2959 if (type == PPC_64B) \
2960 gen_addr_imm_index(EA, ctx, 0x03); \
2962 gen_addr_imm_index(EA, ctx, 0); \
2963 gen_qemu_##stop(cpu_gpr[rS(ctx->opcode)], EA, ctx->mem_idx); \
2964 tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], EA); \
2965 tcg_temp_free(EA); \
2968 #define GEN_STUX(name, stop, opc2, opc3, type) \
2969 GEN_HANDLER(name##ux, 0x1F, opc2, opc3, 0x00000001, type) \
2972 if (unlikely(rA(ctx->opcode) == 0)) { \
2973 GEN_EXCP_INVAL(ctx); \
2976 EA = tcg_temp_new(); \
2977 gen_set_access_type(ACCESS_INT); \
2978 gen_addr_reg_index(EA, ctx); \
2979 gen_qemu_##stop(cpu_gpr[rS(ctx->opcode)], EA, ctx->mem_idx); \
2980 tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], EA); \
2981 tcg_temp_free(EA); \
2984 #define GEN_STX(name, stop, opc2, opc3, type) \
2985 GEN_HANDLER(name##x, 0x1F, opc2, opc3, 0x00000001, type) \
2987 TCGv EA = tcg_temp_new(); \
2988 gen_set_access_type(ACCESS_INT); \
2989 gen_addr_reg_index(EA, ctx); \
2990 gen_qemu_##stop(cpu_gpr[rS(ctx->opcode)], EA, ctx->mem_idx); \
2991 tcg_temp_free(EA); \
2994 #define GEN_STS(name, stop, op, type) \
2995 GEN_ST(name, stop, op | 0x20, type); \
2996 GEN_STU(name, stop, op | 0x21, type); \
2997 GEN_STUX(name, stop, 0x17, op | 0x01, type); \
2998 GEN_STX(name, stop, 0x17, op | 0x00, type)
3000 /* stb stbu stbux stbx */
3001 GEN_STS(stb, st8, 0x06, PPC_INTEGER);
3002 /* sth sthu sthux sthx */
3003 GEN_STS(sth, st16, 0x0C, PPC_INTEGER);
3004 /* stw stwu stwux stwx */
3005 GEN_STS(stw, st32, 0x04, PPC_INTEGER);
3006 #if defined(TARGET_PPC64)
3007 GEN_STUX(std, st64, 0x15, 0x05, PPC_64B);
3008 GEN_STX(std, st64, 0x15, 0x04, PPC_64B);
3009 GEN_HANDLER(std, 0x3E, 0xFF, 0xFF, 0x00000000, PPC_64B)
3014 rs = rS(ctx->opcode);
3015 if ((ctx->opcode & 0x3) == 0x2) {
3016 #if defined(CONFIG_USER_ONLY)
3017 GEN_EXCP_PRIVOPC(ctx);
3020 if (unlikely(ctx->supervisor == 0)) {
3021 GEN_EXCP_PRIVOPC(ctx);
3024 if (unlikely(rs & 1)) {
3025 GEN_EXCP_INVAL(ctx);
3028 if (unlikely(ctx->mem_idx & 1)) {
3029 /* Little-endian mode is not handled */
3030 GEN_EXCP(ctx, POWERPC_EXCP_ALIGN, POWERPC_EXCP_ALIGN_LE);
3033 EA = tcg_temp_new();
3034 gen_set_access_type(ACCESS_INT);
3035 gen_addr_imm_index(EA, ctx, 0x03);
3036 gen_qemu_st64(cpu_gpr[rs], EA, ctx->mem_idx);
3037 tcg_gen_addi_tl(EA, EA, 8);
3038 gen_qemu_st64(cpu_gpr[rs+1], EA, ctx->mem_idx);
3043 if (Rc(ctx->opcode)) {
3044 if (unlikely(rA(ctx->opcode) == 0)) {
3045 GEN_EXCP_INVAL(ctx);
3049 EA = tcg_temp_new();
3050 gen_set_access_type(ACCESS_INT);
3051 gen_addr_imm_index(EA, ctx, 0x03);
3052 gen_qemu_st64(cpu_gpr[rs], EA, ctx->mem_idx);
3053 if (Rc(ctx->opcode))
3054 tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], EA);
3059 /*** Integer load and store with byte reverse ***/
3061 void always_inline gen_qemu_ld16ur(TCGv t0, TCGv t1, int flags)
3063 TCGv_i32 temp = tcg_temp_new_i32();
3064 gen_qemu_ld16u(t0, t1, flags);
3065 tcg_gen_trunc_tl_i32(temp, t0);
3066 tcg_gen_bswap16_i32(temp, temp);
3067 tcg_gen_extu_i32_tl(t0, temp);
3068 tcg_temp_free_i32(temp);
3070 GEN_LDX(lhbr, ld16ur, 0x16, 0x18, PPC_INTEGER);
3073 void always_inline gen_qemu_ld32ur(TCGv t0, TCGv t1, int flags)
3075 TCGv_i32 temp = tcg_temp_new_i32();
3076 gen_qemu_ld32u(t0, t1, flags);
3077 tcg_gen_trunc_tl_i32(temp, t0);
3078 tcg_gen_bswap_i32(temp, temp);
3079 tcg_gen_extu_i32_tl(t0, temp);
3080 tcg_temp_free_i32(temp);
3082 GEN_LDX(lwbr, ld32ur, 0x16, 0x10, PPC_INTEGER);
3085 void always_inline gen_qemu_st16r(TCGv t0, TCGv t1, int flags)
3087 TCGv_i32 temp = tcg_temp_new_i32();
3088 TCGv t2 = tcg_temp_new();
3089 tcg_gen_trunc_tl_i32(temp, t0);
3090 tcg_gen_ext16u_i32(temp, temp);
3091 tcg_gen_bswap16_i32(temp, temp);
3092 tcg_gen_extu_i32_tl(t2, temp);
3093 tcg_temp_free_i32(temp);
3094 gen_qemu_st16(t2, t1, flags);
3097 GEN_STX(sthbr, st16r, 0x16, 0x1C, PPC_INTEGER);
3100 void always_inline gen_qemu_st32r(TCGv t0, TCGv t1, int flags)
3102 TCGv_i32 temp = tcg_temp_new_i32();
3103 TCGv t2 = tcg_temp_new();
3104 tcg_gen_trunc_tl_i32(temp, t0);
3105 tcg_gen_bswap_i32(temp, temp);
3106 tcg_gen_extu_i32_tl(t2, temp);
3107 tcg_temp_free_i32(temp);
3108 gen_qemu_st32(t2, t1, flags);
3111 GEN_STX(stwbr, st32r, 0x16, 0x14, PPC_INTEGER);
3113 /*** Integer load and store multiple ***/
3114 #define op_ldstm(name, reg) (*gen_op_##name[ctx->mem_idx])(reg)
3115 static GenOpFunc1 *gen_op_lmw[NB_MEM_FUNCS] = {
3118 static GenOpFunc1 *gen_op_stmw[NB_MEM_FUNCS] = {
3119 GEN_MEM_FUNCS(stmw),
3123 GEN_HANDLER(lmw, 0x2E, 0xFF, 0xFF, 0x00000000, PPC_INTEGER)
3125 /* NIP cannot be restored if the memory exception comes from an helper */
3126 gen_update_nip(ctx, ctx->nip - 4);
3127 gen_addr_imm_index(cpu_T[0], ctx, 0);
3128 op_ldstm(lmw, rD(ctx->opcode));
3132 GEN_HANDLER(stmw, 0x2F, 0xFF, 0xFF, 0x00000000, PPC_INTEGER)
3134 /* NIP cannot be restored if the memory exception comes from an helper */
3135 gen_update_nip(ctx, ctx->nip - 4);
3136 gen_addr_imm_index(cpu_T[0], ctx, 0);
3137 op_ldstm(stmw, rS(ctx->opcode));
3140 /*** Integer load and store strings ***/
3141 #define op_ldsts(name, start) (*gen_op_##name[ctx->mem_idx])(start)
3142 #define op_ldstsx(name, rd, ra, rb) (*gen_op_##name[ctx->mem_idx])(rd, ra, rb)
3143 /* string load & stores are by definition endian-safe */
3144 #define gen_op_lswi_le_raw gen_op_lswi_raw
3145 #define gen_op_lswi_le_user gen_op_lswi_user
3146 #define gen_op_lswi_le_kernel gen_op_lswi_kernel
3147 #define gen_op_lswi_le_hypv gen_op_lswi_hypv
3148 #define gen_op_lswi_le_64_raw gen_op_lswi_raw
3149 #define gen_op_lswi_le_64_user gen_op_lswi_user
3150 #define gen_op_lswi_le_64_kernel gen_op_lswi_kernel
3151 #define gen_op_lswi_le_64_hypv gen_op_lswi_hypv
3152 static GenOpFunc1 *gen_op_lswi[NB_MEM_FUNCS] = {
3153 GEN_MEM_FUNCS(lswi),
3155 #define gen_op_lswx_le_raw gen_op_lswx_raw
3156 #define gen_op_lswx_le_user gen_op_lswx_user
3157 #define gen_op_lswx_le_kernel gen_op_lswx_kernel
3158 #define gen_op_lswx_le_hypv gen_op_lswx_hypv
3159 #define gen_op_lswx_le_64_raw gen_op_lswx_raw
3160 #define gen_op_lswx_le_64_user gen_op_lswx_user
3161 #define gen_op_lswx_le_64_kernel gen_op_lswx_kernel
3162 #define gen_op_lswx_le_64_hypv gen_op_lswx_hypv
3163 static GenOpFunc3 *gen_op_lswx[NB_MEM_FUNCS] = {
3164 GEN_MEM_FUNCS(lswx),
3166 #define gen_op_stsw_le_raw gen_op_stsw_raw
3167 #define gen_op_stsw_le_user gen_op_stsw_user
3168 #define gen_op_stsw_le_kernel gen_op_stsw_kernel
3169 #define gen_op_stsw_le_hypv gen_op_stsw_hypv
3170 #define gen_op_stsw_le_64_raw gen_op_stsw_raw
3171 #define gen_op_stsw_le_64_user gen_op_stsw_user
3172 #define gen_op_stsw_le_64_kernel gen_op_stsw_kernel
3173 #define gen_op_stsw_le_64_hypv gen_op_stsw_hypv
3174 static GenOpFunc1 *gen_op_stsw[NB_MEM_FUNCS] = {
3175 GEN_MEM_FUNCS(stsw),
3179 /* PowerPC32 specification says we must generate an exception if
3180 * rA is in the range of registers to be loaded.
3181 * In an other hand, IBM says this is valid, but rA won't be loaded.
3182 * For now, I'll follow the spec...
3184 GEN_HANDLER(lswi, 0x1F, 0x15, 0x12, 0x00000001, PPC_STRING)
3186 int nb = NB(ctx->opcode);
3187 int start = rD(ctx->opcode);
3188 int ra = rA(ctx->opcode);
3194 if (unlikely(((start + nr) > 32 &&
3195 start <= ra && (start + nr - 32) > ra) ||
3196 ((start + nr) <= 32 && start <= ra && (start + nr) > ra))) {
3197 GEN_EXCP(ctx, POWERPC_EXCP_PROGRAM,
3198 POWERPC_EXCP_INVAL | POWERPC_EXCP_INVAL_LSWX);
3201 /* NIP cannot be restored if the memory exception comes from an helper */
3202 gen_update_nip(ctx, ctx->nip - 4);
3203 gen_addr_register(cpu_T[0], ctx);
3204 tcg_gen_movi_tl(cpu_T[1], nb);
3205 op_ldsts(lswi, start);
3209 GEN_HANDLER(lswx, 0x1F, 0x15, 0x10, 0x00000001, PPC_STRING)
3211 int ra = rA(ctx->opcode);
3212 int rb = rB(ctx->opcode);
3214 /* NIP cannot be restored if the memory exception comes from an helper */
3215 gen_update_nip(ctx, ctx->nip - 4);
3216 gen_addr_reg_index(cpu_T[0], ctx);
3220 tcg_gen_andi_tl(cpu_T[1], cpu_xer, 0x7F);
3221 op_ldstsx(lswx, rD(ctx->opcode), ra, rb);
3225 GEN_HANDLER(stswi, 0x1F, 0x15, 0x16, 0x00000001, PPC_STRING)
3227 int nb = NB(ctx->opcode);
3229 /* NIP cannot be restored if the memory exception comes from an helper */
3230 gen_update_nip(ctx, ctx->nip - 4);
3231 gen_addr_register(cpu_T[0], ctx);
3234 tcg_gen_movi_tl(cpu_T[1], nb);
3235 op_ldsts(stsw, rS(ctx->opcode));
3239 GEN_HANDLER(stswx, 0x1F, 0x15, 0x14, 0x00000001, PPC_STRING)
3241 /* NIP cannot be restored if the memory exception comes from an helper */
3242 gen_update_nip(ctx, ctx->nip - 4);
3243 gen_addr_reg_index(cpu_T[0], ctx);
3244 tcg_gen_andi_tl(cpu_T[1], cpu_xer, 0x7F);
3245 op_ldsts(stsw, rS(ctx->opcode));
3248 /*** Memory synchronisation ***/
3250 GEN_HANDLER(eieio, 0x1F, 0x16, 0x1A, 0x03FFF801, PPC_MEM_EIEIO)
3255 GEN_HANDLER(isync, 0x13, 0x16, 0x04, 0x03FFF801, PPC_MEM)
3260 #define op_lwarx() (*gen_op_lwarx[ctx->mem_idx])()
3261 #define op_stwcx() (*gen_op_stwcx[ctx->mem_idx])()
3262 static GenOpFunc *gen_op_lwarx[NB_MEM_FUNCS] = {
3263 GEN_MEM_FUNCS(lwarx),
3265 static GenOpFunc *gen_op_stwcx[NB_MEM_FUNCS] = {
3266 GEN_MEM_FUNCS(stwcx),
3270 GEN_HANDLER(lwarx, 0x1F, 0x14, 0x00, 0x00000001, PPC_RES)
3272 /* NIP cannot be restored if the memory exception comes from an helper */
3273 gen_update_nip(ctx, ctx->nip - 4);
3274 gen_set_access_type(ACCESS_RES);
3275 gen_addr_reg_index(cpu_T[0], ctx);
3277 tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_T[1]);
3281 GEN_HANDLER2(stwcx_, "stwcx.", 0x1F, 0x16, 0x04, 0x00000000, PPC_RES)
3283 /* NIP cannot be restored if the memory exception comes from an helper */
3284 gen_update_nip(ctx, ctx->nip - 4);
3285 gen_set_access_type(ACCESS_RES);
3286 gen_addr_reg_index(cpu_T[0], ctx);
3287 tcg_gen_mov_tl(cpu_T[1], cpu_gpr[rS(ctx->opcode)]);
3291 #if defined(TARGET_PPC64)
3292 #define op_ldarx() (*gen_op_ldarx[ctx->mem_idx])()
3293 #define op_stdcx() (*gen_op_stdcx[ctx->mem_idx])()
3294 static GenOpFunc *gen_op_ldarx[NB_MEM_FUNCS] = {
3295 GEN_MEM_FUNCS(ldarx),
3297 static GenOpFunc *gen_op_stdcx[NB_MEM_FUNCS] = {
3298 GEN_MEM_FUNCS(stdcx),
3302 GEN_HANDLER(ldarx, 0x1F, 0x14, 0x02, 0x00000001, PPC_64B)
3304 /* NIP cannot be restored if the memory exception comes from an helper */
3305 gen_update_nip(ctx, ctx->nip - 4);
3306 gen_set_access_type(ACCESS_RES);
3307 gen_addr_reg_index(cpu_T[0], ctx);
3309 tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_T[1]);
3313 GEN_HANDLER2(stdcx_, "stdcx.", 0x1F, 0x16, 0x06, 0x00000000, PPC_64B)
3315 /* NIP cannot be restored if the memory exception comes from an helper */
3316 gen_update_nip(ctx, ctx->nip - 4);
3317 gen_set_access_type(ACCESS_RES);
3318 gen_addr_reg_index(cpu_T[0], ctx);
3319 tcg_gen_mov_tl(cpu_T[1], cpu_gpr[rS(ctx->opcode)]);
3322 #endif /* defined(TARGET_PPC64) */
3325 GEN_HANDLER(sync, 0x1F, 0x16, 0x12, 0x039FF801, PPC_MEM_SYNC)
3330 GEN_HANDLER(wait, 0x1F, 0x1E, 0x01, 0x03FFF801, PPC_WAIT)
3332 /* Stop translation, as the CPU is supposed to sleep from now */
3334 GEN_EXCP(ctx, EXCP_HLT, 1);
3337 /*** Floating-point load ***/
3338 #define GEN_LDF(name, ldop, opc, type) \
3339 GEN_HANDLER(name, opc, 0xFF, 0xFF, 0x00000000, type) \
3342 if (unlikely(!ctx->fpu_enabled)) { \
3343 GEN_EXCP_NO_FP(ctx); \
3346 gen_set_access_type(ACCESS_FLOAT); \
3347 EA = tcg_temp_new(); \
3348 gen_addr_imm_index(EA, ctx, 0); \
3349 gen_qemu_##ldop(cpu_fpr[rD(ctx->opcode)], EA, ctx->mem_idx); \
3350 tcg_temp_free(EA); \
3353 #define GEN_LDUF(name, ldop, opc, type) \
3354 GEN_HANDLER(name##u, opc, 0xFF, 0xFF, 0x00000000, type) \
3357 if (unlikely(!ctx->fpu_enabled)) { \
3358 GEN_EXCP_NO_FP(ctx); \
3361 if (unlikely(rA(ctx->opcode) == 0)) { \
3362 GEN_EXCP_INVAL(ctx); \
3365 gen_set_access_type(ACCESS_FLOAT); \
3366 EA = tcg_temp_new(); \
3367 gen_addr_imm_index(EA, ctx, 0); \
3368 gen_qemu_##ldop(cpu_fpr[rD(ctx->opcode)], EA, ctx->mem_idx); \
3369 tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], EA); \
3370 tcg_temp_free(EA); \
3373 #define GEN_LDUXF(name, ldop, opc, type) \
3374 GEN_HANDLER(name##ux, 0x1F, 0x17, opc, 0x00000001, type) \
3377 if (unlikely(!ctx->fpu_enabled)) { \
3378 GEN_EXCP_NO_FP(ctx); \
3381 if (unlikely(rA(ctx->opcode) == 0)) { \
3382 GEN_EXCP_INVAL(ctx); \
3385 gen_set_access_type(ACCESS_FLOAT); \
3386 EA = tcg_temp_new(); \
3387 gen_addr_reg_index(EA, ctx); \
3388 gen_qemu_##ldop(cpu_fpr[rD(ctx->opcode)], EA, ctx->mem_idx); \
3389 tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], EA); \
3390 tcg_temp_free(EA); \
3393 #define GEN_LDXF(name, ldop, opc2, opc3, type) \
3394 GEN_HANDLER(name##x, 0x1F, opc2, opc3, 0x00000001, type) \
3397 if (unlikely(!ctx->fpu_enabled)) { \
3398 GEN_EXCP_NO_FP(ctx); \
3401 gen_set_access_type(ACCESS_FLOAT); \
3402 EA = tcg_temp_new(); \
3403 gen_addr_reg_index(EA, ctx); \
3404 gen_qemu_##ldop(cpu_fpr[rD(ctx->opcode)], EA, ctx->mem_idx); \
3405 tcg_temp_free(EA); \
3408 #define GEN_LDFS(name, ldop, op, type) \
3409 GEN_LDF(name, ldop, op | 0x20, type); \
3410 GEN_LDUF(name, ldop, op | 0x21, type); \
3411 GEN_LDUXF(name, ldop, op | 0x01, type); \
3412 GEN_LDXF(name, ldop, 0x17, op | 0x00, type)
3414 static always_inline void gen_qemu_ld32fs(TCGv_i64 arg1, TCGv arg2, int flags)
3416 TCGv t0 = tcg_temp_new();
3417 TCGv_i32 t1 = tcg_temp_new_i32();
3418 gen_qemu_ld32u(t0, arg2, flags);
3419 tcg_gen_trunc_tl_i32(t1, t0);
3421 gen_helper_float32_to_float64(arg1, t1);
3422 tcg_temp_free_i32(t1);
3425 /* lfd lfdu lfdux lfdx */
3426 GEN_LDFS(lfd, ld64, 0x12, PPC_FLOAT);
3427 /* lfs lfsu lfsux lfsx */
3428 GEN_LDFS(lfs, ld32fs, 0x10, PPC_FLOAT);
3430 /*** Floating-point store ***/
3431 #define GEN_STF(name, stop, opc, type) \
3432 GEN_HANDLER(name, opc, 0xFF, 0xFF, 0x00000000, type) \
3435 if (unlikely(!ctx->fpu_enabled)) { \
3436 GEN_EXCP_NO_FP(ctx); \
3439 gen_set_access_type(ACCESS_FLOAT); \
3440 EA = tcg_temp_new(); \
3441 gen_addr_imm_index(EA, ctx, 0); \
3442 gen_qemu_##stop(cpu_fpr[rS(ctx->opcode)], EA, ctx->mem_idx); \
3443 tcg_temp_free(EA); \
3446 #define GEN_STUF(name, stop, opc, type) \
3447 GEN_HANDLER(name##u, opc, 0xFF, 0xFF, 0x00000000, type) \
3450 if (unlikely(!ctx->fpu_enabled)) { \
3451 GEN_EXCP_NO_FP(ctx); \
3454 if (unlikely(rA(ctx->opcode) == 0)) { \
3455 GEN_EXCP_INVAL(ctx); \
3458 gen_set_access_type(ACCESS_FLOAT); \
3459 EA = tcg_temp_new(); \
3460 gen_addr_imm_index(EA, ctx, 0); \
3461 gen_qemu_##stop(cpu_fpr[rS(ctx->opcode)], EA, ctx->mem_idx); \
3462 tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], EA); \
3463 tcg_temp_free(EA); \
3466 #define GEN_STUXF(name, stop, opc, type) \
3467 GEN_HANDLER(name##ux, 0x1F, 0x17, opc, 0x00000001, type) \
3470 if (unlikely(!ctx->fpu_enabled)) { \
3471 GEN_EXCP_NO_FP(ctx); \
3474 if (unlikely(rA(ctx->opcode) == 0)) { \
3475 GEN_EXCP_INVAL(ctx); \
3478 gen_set_access_type(ACCESS_FLOAT); \
3479 EA = tcg_temp_new(); \
3480 gen_addr_reg_index(EA, ctx); \
3481 gen_qemu_##stop(cpu_fpr[rS(ctx->opcode)], EA, ctx->mem_idx); \
3482 tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], EA); \
3483 tcg_temp_free(EA); \
3486 #define GEN_STXF(name, stop, opc2, opc3, type) \
3487 GEN_HANDLER(name##x, 0x1F, opc2, opc3, 0x00000001, type) \
3490 if (unlikely(!ctx->fpu_enabled)) { \
3491 GEN_EXCP_NO_FP(ctx); \
3494 gen_set_access_type(ACCESS_FLOAT); \
3495 EA = tcg_temp_new(); \
3496 gen_addr_reg_index(EA, ctx); \
3497 gen_qemu_##stop(cpu_fpr[rS(ctx->opcode)], EA, ctx->mem_idx); \
3498 tcg_temp_free(EA); \
3501 #define GEN_STFS(name, stop, op, type) \
3502 GEN_STF(name, stop, op | 0x20, type); \
3503 GEN_STUF(name, stop, op | 0x21, type); \
3504 GEN_STUXF(name, stop, op | 0x01, type); \
3505 GEN_STXF(name, stop, 0x17, op | 0x00, type)
3507 static always_inline void gen_qemu_st32fs(TCGv_i64 arg1, TCGv arg2, int flags)
3509 TCGv_i32 t0 = tcg_temp_new_i32();
3510 TCGv t1 = tcg_temp_new();
3511 gen_helper_float64_to_float32(t0, arg1);
3512 tcg_gen_extu_i32_tl(t1, t0);
3513 tcg_temp_free_i32(t0);
3514 gen_qemu_st32(t1, arg2, flags);
3518 /* stfd stfdu stfdux stfdx */
3519 GEN_STFS(stfd, st64, 0x16, PPC_FLOAT);
3520 /* stfs stfsu stfsux stfsx */
3521 GEN_STFS(stfs, st32fs, 0x14, PPC_FLOAT);
3524 static always_inline void gen_qemu_st32fiw(TCGv_i64 arg1, TCGv arg2, int flags)
3526 TCGv t0 = tcg_temp_new();
3527 tcg_gen_trunc_i64_tl(t0, arg1),
3528 gen_qemu_st32(t0, arg2, flags);
3532 GEN_STXF(stfiw, st32fiw, 0x17, 0x1E, PPC_FLOAT_STFIWX);
3535 static always_inline void gen_goto_tb (DisasContext *ctx, int n,
3538 TranslationBlock *tb;
3540 #if defined(TARGET_PPC64)
3542 dest = (uint32_t) dest;
3544 if ((tb->pc & TARGET_PAGE_MASK) == (dest & TARGET_PAGE_MASK) &&
3545 likely(!ctx->singlestep_enabled)) {
3547 tcg_gen_movi_tl(cpu_nip, dest & ~3);
3548 tcg_gen_exit_tb((long)tb + n);
3550 tcg_gen_movi_tl(cpu_nip, dest & ~3);
3551 if (unlikely(ctx->singlestep_enabled)) {
3552 if ((ctx->singlestep_enabled &
3553 (CPU_BRANCH_STEP | CPU_SINGLE_STEP)) &&
3554 ctx->exception == POWERPC_EXCP_BRANCH) {
3555 target_ulong tmp = ctx->nip;
3557 GEN_EXCP(ctx, POWERPC_EXCP_TRACE, 0);
3560 if (ctx->singlestep_enabled & GDBSTUB_SINGLE_STEP) {
3561 gen_update_nip(ctx, dest);
3562 gen_helper_raise_debug();
3569 static always_inline void gen_setlr (DisasContext *ctx, target_ulong nip)
3571 #if defined(TARGET_PPC64)
3572 if (ctx->sf_mode == 0)
3573 tcg_gen_movi_tl(cpu_lr, (uint32_t)nip);
3576 tcg_gen_movi_tl(cpu_lr, nip);
3580 GEN_HANDLER(b, 0x12, 0xFF, 0xFF, 0x00000000, PPC_FLOW)
3582 target_ulong li, target;
3584 ctx->exception = POWERPC_EXCP_BRANCH;
3585 /* sign extend LI */
3586 #if defined(TARGET_PPC64)
3588 li = ((int64_t)LI(ctx->opcode) << 38) >> 38;
3591 li = ((int32_t)LI(ctx->opcode) << 6) >> 6;
3592 if (likely(AA(ctx->opcode) == 0))
3593 target = ctx->nip + li - 4;
3596 if (LK(ctx->opcode))
3597 gen_setlr(ctx, ctx->nip);
3598 gen_goto_tb(ctx, 0, target);
3605 static always_inline void gen_bcond (DisasContext *ctx, int type)
3607 uint32_t bo = BO(ctx->opcode);
3608 int l1 = gen_new_label();
3611 ctx->exception = POWERPC_EXCP_BRANCH;
3612 if (type == BCOND_LR || type == BCOND_CTR) {
3613 target = tcg_temp_local_new();
3614 if (type == BCOND_CTR)
3615 tcg_gen_mov_tl(target, cpu_ctr);
3617 tcg_gen_mov_tl(target, cpu_lr);
3619 if (LK(ctx->opcode))
3620 gen_setlr(ctx, ctx->nip);
3621 l1 = gen_new_label();
3622 if ((bo & 0x4) == 0) {
3623 /* Decrement and test CTR */
3624 TCGv temp = tcg_temp_new();
3625 if (unlikely(type == BCOND_CTR)) {
3626 GEN_EXCP_INVAL(ctx);
3629 tcg_gen_subi_tl(cpu_ctr, cpu_ctr, 1);
3630 #if defined(TARGET_PPC64)
3632 tcg_gen_ext32u_tl(temp, cpu_ctr);
3635 tcg_gen_mov_tl(temp, cpu_ctr);
3637 tcg_gen_brcondi_tl(TCG_COND_NE, temp, 0, l1);
3639 tcg_gen_brcondi_tl(TCG_COND_EQ, temp, 0, l1);
3641 tcg_temp_free(temp);
3643 if ((bo & 0x10) == 0) {
3645 uint32_t bi = BI(ctx->opcode);
3646 uint32_t mask = 1 << (3 - (bi & 0x03));
3647 TCGv_i32 temp = tcg_temp_new_i32();
3650 tcg_gen_andi_i32(temp, cpu_crf[bi >> 2], mask);
3651 tcg_gen_brcondi_i32(TCG_COND_EQ, temp, 0, l1);
3653 tcg_gen_andi_i32(temp, cpu_crf[bi >> 2], mask);
3654 tcg_gen_brcondi_i32(TCG_COND_NE, temp, 0, l1);
3656 tcg_temp_free_i32(temp);
3658 if (type == BCOND_IM) {
3659 target_ulong li = (target_long)((int16_t)(BD(ctx->opcode)));
3660 if (likely(AA(ctx->opcode) == 0)) {
3661 gen_goto_tb(ctx, 0, ctx->nip + li - 4);
3663 gen_goto_tb(ctx, 0, li);
3666 gen_goto_tb(ctx, 1, ctx->nip);
3668 #if defined(TARGET_PPC64)
3669 if (!(ctx->sf_mode))
3670 tcg_gen_andi_tl(cpu_nip, target, (uint32_t)~3);
3673 tcg_gen_andi_tl(cpu_nip, target, ~3);
3676 #if defined(TARGET_PPC64)
3677 if (!(ctx->sf_mode))
3678 tcg_gen_movi_tl(cpu_nip, (uint32_t)ctx->nip);
3681 tcg_gen_movi_tl(cpu_nip, ctx->nip);
3686 GEN_HANDLER(bc, 0x10, 0xFF, 0xFF, 0x00000000, PPC_FLOW)
3688 gen_bcond(ctx, BCOND_IM);
3691 GEN_HANDLER(bcctr, 0x13, 0x10, 0x10, 0x00000000, PPC_FLOW)
3693 gen_bcond(ctx, BCOND_CTR);
3696 GEN_HANDLER(bclr, 0x13, 0x10, 0x00, 0x00000000, PPC_FLOW)
3698 gen_bcond(ctx, BCOND_LR);
3701 /*** Condition register logical ***/
3702 #define GEN_CRLOGIC(name, tcg_op, opc) \
3703 GEN_HANDLER(name, 0x13, 0x01, opc, 0x00000001, PPC_INTEGER) \
3708 sh = (crbD(ctx->opcode) & 0x03) - (crbA(ctx->opcode) & 0x03); \
3709 t0 = tcg_temp_new_i32(); \
3711 tcg_gen_shri_i32(t0, cpu_crf[crbA(ctx->opcode) >> 2], sh); \
3713 tcg_gen_shli_i32(t0, cpu_crf[crbA(ctx->opcode) >> 2], -sh); \
3715 tcg_gen_mov_i32(t0, cpu_crf[crbA(ctx->opcode) >> 2]); \
3716 t1 = tcg_temp_new_i32(); \
3717 sh = (crbD(ctx->opcode) & 0x03) - (crbB(ctx->opcode) & 0x03); \
3719 tcg_gen_shri_i32(t1, cpu_crf[crbB(ctx->opcode) >> 2], sh); \
3721 tcg_gen_shli_i32(t1, cpu_crf[crbB(ctx->opcode) >> 2], -sh); \
3723 tcg_gen_mov_i32(t1, cpu_crf[crbB(ctx->opcode) >> 2]); \
3724 tcg_op(t0, t0, t1); \
3725 bitmask = 1 << (3 - (crbD(ctx->opcode) & 0x03)); \
3726 tcg_gen_andi_i32(t0, t0, bitmask); \
3727 tcg_gen_andi_i32(t1, cpu_crf[crbD(ctx->opcode) >> 2], ~bitmask); \
3728 tcg_gen_or_i32(cpu_crf[crbD(ctx->opcode) >> 2], t0, t1); \
3729 tcg_temp_free_i32(t0); \
3730 tcg_temp_free_i32(t1); \
3734 GEN_CRLOGIC(crand, tcg_gen_and_i32, 0x08);
3736 GEN_CRLOGIC(crandc, tcg_gen_andc_i32, 0x04);
3738 GEN_CRLOGIC(creqv, tcg_gen_eqv_i32, 0x09);
3740 GEN_CRLOGIC(crnand, tcg_gen_nand_i32, 0x07);
3742 GEN_CRLOGIC(crnor, tcg_gen_nor_i32, 0x01);
3744 GEN_CRLOGIC(cror, tcg_gen_or_i32, 0x0E);
3746 GEN_CRLOGIC(crorc, tcg_gen_orc_i32, 0x0D);
3748 GEN_CRLOGIC(crxor, tcg_gen_xor_i32, 0x06);
3750 GEN_HANDLER(mcrf, 0x13, 0x00, 0xFF, 0x00000001, PPC_INTEGER)
3752 tcg_gen_mov_i32(cpu_crf[crfD(ctx->opcode)], cpu_crf[crfS(ctx->opcode)]);
3755 /*** System linkage ***/
3756 /* rfi (supervisor only) */
3757 GEN_HANDLER(rfi, 0x13, 0x12, 0x01, 0x03FF8001, PPC_FLOW)
3759 #if defined(CONFIG_USER_ONLY)
3760 GEN_EXCP_PRIVOPC(ctx);
3762 /* Restore CPU state */
3763 if (unlikely(!ctx->supervisor)) {
3764 GEN_EXCP_PRIVOPC(ctx);
3772 #if defined(TARGET_PPC64)
3773 GEN_HANDLER(rfid, 0x13, 0x12, 0x00, 0x03FF8001, PPC_64B)
3775 #if defined(CONFIG_USER_ONLY)
3776 GEN_EXCP_PRIVOPC(ctx);
3778 /* Restore CPU state */
3779 if (unlikely(!ctx->supervisor)) {
3780 GEN_EXCP_PRIVOPC(ctx);
3788 GEN_HANDLER(hrfid, 0x13, 0x12, 0x08, 0x03FF8001, PPC_64H)
3790 #if defined(CONFIG_USER_ONLY)
3791 GEN_EXCP_PRIVOPC(ctx);
3793 /* Restore CPU state */
3794 if (unlikely(ctx->supervisor <= 1)) {
3795 GEN_EXCP_PRIVOPC(ctx);
3805 #if defined(CONFIG_USER_ONLY)
3806 #define POWERPC_SYSCALL POWERPC_EXCP_SYSCALL_USER
3808 #define POWERPC_SYSCALL POWERPC_EXCP_SYSCALL
3810 GEN_HANDLER(sc, 0x11, 0xFF, 0xFF, 0x03FFF01D, PPC_FLOW)
3814 lev = (ctx->opcode >> 5) & 0x7F;
3815 GEN_EXCP(ctx, POWERPC_SYSCALL, lev);
3820 GEN_HANDLER(tw, 0x1F, 0x04, 0x00, 0x00000001, PPC_FLOW)
3822 TCGv_i32 t0 = tcg_const_i32(TO(ctx->opcode));
3823 /* Update the nip since this might generate a trap exception */
3824 gen_update_nip(ctx, ctx->nip);
3825 gen_helper_tw(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rB(ctx->opcode)], t0);
3826 tcg_temp_free_i32(t0);
3830 GEN_HANDLER(twi, 0x03, 0xFF, 0xFF, 0x00000000, PPC_FLOW)
3832 TCGv t0 = tcg_const_tl(SIMM(ctx->opcode));
3833 TCGv_i32 t1 = tcg_const_i32(TO(ctx->opcode));
3834 /* Update the nip since this might generate a trap exception */
3835 gen_update_nip(ctx, ctx->nip);
3836 gen_helper_tw(cpu_gpr[rA(ctx->opcode)], t0, t1);
3838 tcg_temp_free_i32(t1);
3841 #if defined(TARGET_PPC64)
3843 GEN_HANDLER(td, 0x1F, 0x04, 0x02, 0x00000001, PPC_64B)
3845 TCGv_i32 t0 = tcg_const_i32(TO(ctx->opcode));
3846 /* Update the nip since this might generate a trap exception */
3847 gen_update_nip(ctx, ctx->nip);
3848 gen_helper_td(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rB(ctx->opcode)], t0);
3849 tcg_temp_free_i32(t0);
3853 GEN_HANDLER(tdi, 0x02, 0xFF, 0xFF, 0x00000000, PPC_64B)
3855 TCGv t0 = tcg_const_tl(SIMM(ctx->opcode));
3856 TCGv_i32 t1 = tcg_const_i32(TO(ctx->opcode));
3857 /* Update the nip since this might generate a trap exception */
3858 gen_update_nip(ctx, ctx->nip);
3859 gen_helper_td(cpu_gpr[rA(ctx->opcode)], t0, t1);
3861 tcg_temp_free_i32(t1);
3865 /*** Processor control ***/
3867 GEN_HANDLER(mcrxr, 0x1F, 0x00, 0x10, 0x007FF801, PPC_MISC)
3869 tcg_gen_trunc_tl_i32(cpu_crf[crfD(ctx->opcode)], cpu_xer);
3870 tcg_gen_shri_i32(cpu_crf[crfD(ctx->opcode)], cpu_crf[crfD(ctx->opcode)], XER_CA);
3871 tcg_gen_andi_tl(cpu_xer, cpu_xer, ~(1 << XER_SO | 1 << XER_OV | 1 << XER_CA));
3875 GEN_HANDLER(mfcr, 0x1F, 0x13, 0x00, 0x00000801, PPC_MISC)
3879 if (likely(ctx->opcode & 0x00100000)) {
3880 crm = CRM(ctx->opcode);
3881 if (likely((crm ^ (crm - 1)) == 0)) {
3883 tcg_gen_extu_i32_tl(cpu_gpr[rD(ctx->opcode)], cpu_crf[7 - crn]);
3886 gen_helper_load_cr(cpu_gpr[rD(ctx->opcode)]);
3891 GEN_HANDLER(mfmsr, 0x1F, 0x13, 0x02, 0x001FF801, PPC_MISC)
3893 #if defined(CONFIG_USER_ONLY)
3894 GEN_EXCP_PRIVREG(ctx);
3896 if (unlikely(!ctx->supervisor)) {
3897 GEN_EXCP_PRIVREG(ctx);
3901 tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_T[0]);
3906 #define SPR_NOACCESS ((void *)(-1UL))
3908 static void spr_noaccess (void *opaque, int sprn)
3910 sprn = ((sprn >> 5) & 0x1F) | ((sprn & 0x1F) << 5);
3911 printf("ERROR: try to access SPR %d !\n", sprn);
3913 #define SPR_NOACCESS (&spr_noaccess)
3917 static always_inline void gen_op_mfspr (DisasContext *ctx)
3919 void (*read_cb)(void *opaque, int sprn);
3920 uint32_t sprn = SPR(ctx->opcode);
3922 #if !defined(CONFIG_USER_ONLY)
3923 if (ctx->supervisor == 2)
3924 read_cb = ctx->spr_cb[sprn].hea_read;
3925 else if (ctx->supervisor)
3926 read_cb = ctx->spr_cb[sprn].oea_read;
3929 read_cb = ctx->spr_cb[sprn].uea_read;
3930 if (likely(read_cb != NULL)) {
3931 if (likely(read_cb != SPR_NOACCESS)) {
3932 (*read_cb)(ctx, sprn);
3933 tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_T[0]);
3935 /* Privilege exception */
3936 /* This is a hack to avoid warnings when running Linux:
3937 * this OS breaks the PowerPC virtualisation model,
3938 * allowing userland application to read the PVR
3940 if (sprn != SPR_PVR) {
3941 if (loglevel != 0) {
3942 fprintf(logfile, "Trying to read privileged spr %d %03x at "
3943 ADDRX "\n", sprn, sprn, ctx->nip);
3945 printf("Trying to read privileged spr %d %03x at " ADDRX "\n",
3946 sprn, sprn, ctx->nip);
3948 GEN_EXCP_PRIVREG(ctx);
3952 if (loglevel != 0) {
3953 fprintf(logfile, "Trying to read invalid spr %d %03x at "
3954 ADDRX "\n", sprn, sprn, ctx->nip);
3956 printf("Trying to read invalid spr %d %03x at " ADDRX "\n",
3957 sprn, sprn, ctx->nip);
3958 GEN_EXCP(ctx, POWERPC_EXCP_PROGRAM,
3959 POWERPC_EXCP_INVAL | POWERPC_EXCP_INVAL_SPR);
3963 GEN_HANDLER(mfspr, 0x1F, 0x13, 0x0A, 0x00000001, PPC_MISC)
3969 GEN_HANDLER(mftb, 0x1F, 0x13, 0x0B, 0x00000001, PPC_MFTB)
3975 GEN_HANDLER(mtcrf, 0x1F, 0x10, 0x04, 0x00000801, PPC_MISC)
3979 crm = CRM(ctx->opcode);
3980 if (likely((ctx->opcode & 0x00100000) || (crm ^ (crm - 1)) == 0)) {
3981 TCGv_i32 temp = tcg_temp_new_i32();
3983 tcg_gen_trunc_tl_i32(temp, cpu_gpr[rS(ctx->opcode)]);
3984 tcg_gen_shri_i32(cpu_crf[7 - crn], temp, crn * 4);
3985 tcg_gen_andi_i32(cpu_crf[7 - crn], cpu_crf[7 - crn], 0xf);
3986 tcg_temp_free_i32(temp);
3988 TCGv_i32 temp = tcg_const_i32(crm);
3989 gen_helper_store_cr(cpu_gpr[rS(ctx->opcode)], temp);
3990 tcg_temp_free_i32(temp);
3995 #if defined(TARGET_PPC64)
3996 GEN_HANDLER(mtmsrd, 0x1F, 0x12, 0x05, 0x001EF801, PPC_64B)
3998 #if defined(CONFIG_USER_ONLY)
3999 GEN_EXCP_PRIVREG(ctx);
4001 if (unlikely(!ctx->supervisor)) {
4002 GEN_EXCP_PRIVREG(ctx);
4005 tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rS(ctx->opcode)]);
4006 if (ctx->opcode & 0x00010000) {
4007 /* Special form that does not need any synchronisation */
4008 gen_op_update_riee();
4010 /* XXX: we need to update nip before the store
4011 * if we enter power saving mode, we will exit the loop
4012 * directly from ppc_store_msr
4014 gen_update_nip(ctx, ctx->nip);
4016 /* Must stop the translation as machine state (may have) changed */
4017 /* Note that mtmsr is not always defined as context-synchronizing */
4018 ctx->exception = POWERPC_EXCP_STOP;
4024 GEN_HANDLER(mtmsr, 0x1F, 0x12, 0x04, 0x001FF801, PPC_MISC)
4026 #if defined(CONFIG_USER_ONLY)
4027 GEN_EXCP_PRIVREG(ctx);
4029 if (unlikely(!ctx->supervisor)) {
4030 GEN_EXCP_PRIVREG(ctx);
4033 tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rS(ctx->opcode)]);
4034 if (ctx->opcode & 0x00010000) {
4035 /* Special form that does not need any synchronisation */
4036 gen_op_update_riee();
4038 /* XXX: we need to update nip before the store
4039 * if we enter power saving mode, we will exit the loop
4040 * directly from ppc_store_msr
4042 gen_update_nip(ctx, ctx->nip);
4043 #if defined(TARGET_PPC64)
4045 gen_op_store_msr_32();
4049 /* Must stop the translation as machine state (may have) changed */
4050 /* Note that mtmsrd is not always defined as context-synchronizing */
4051 ctx->exception = POWERPC_EXCP_STOP;
4057 GEN_HANDLER(mtspr, 0x1F, 0x13, 0x0E, 0x00000001, PPC_MISC)
4059 void (*write_cb)(void *opaque, int sprn);
4060 uint32_t sprn = SPR(ctx->opcode);
4062 #if !defined(CONFIG_USER_ONLY)
4063 if (ctx->supervisor == 2)
4064 write_cb = ctx->spr_cb[sprn].hea_write;
4065 else if (ctx->supervisor)
4066 write_cb = ctx->spr_cb[sprn].oea_write;
4069 write_cb = ctx->spr_cb[sprn].uea_write;
4070 if (likely(write_cb != NULL)) {
4071 if (likely(write_cb != SPR_NOACCESS)) {
4072 tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rS(ctx->opcode)]);
4073 (*write_cb)(ctx, sprn);
4075 /* Privilege exception */
4076 if (loglevel != 0) {
4077 fprintf(logfile, "Trying to write privileged spr %d %03x at "
4078 ADDRX "\n", sprn, sprn, ctx->nip);
4080 printf("Trying to write privileged spr %d %03x at " ADDRX "\n",
4081 sprn, sprn, ctx->nip);
4082 GEN_EXCP_PRIVREG(ctx);
4086 if (loglevel != 0) {
4087 fprintf(logfile, "Trying to write invalid spr %d %03x at "
4088 ADDRX "\n", sprn, sprn, ctx->nip);
4090 printf("Trying to write invalid spr %d %03x at " ADDRX "\n",
4091 sprn, sprn, ctx->nip);
4092 GEN_EXCP(ctx, POWERPC_EXCP_PROGRAM,
4093 POWERPC_EXCP_INVAL | POWERPC_EXCP_INVAL_SPR);
4097 /*** Cache management ***/
4099 GEN_HANDLER(dcbf, 0x1F, 0x16, 0x02, 0x03C00001, PPC_CACHE)
4101 /* XXX: specification says this is treated as a load by the MMU */
4102 TCGv t0 = tcg_temp_new();
4103 gen_set_access_type(ACCESS_CACHE);
4104 gen_addr_reg_index(t0, ctx);
4105 gen_qemu_ld8u(t0, t0, ctx->mem_idx);
4109 /* dcbi (Supervisor only) */
4110 GEN_HANDLER(dcbi, 0x1F, 0x16, 0x0E, 0x03E00001, PPC_CACHE)
4112 #if defined(CONFIG_USER_ONLY)
4113 GEN_EXCP_PRIVOPC(ctx);
4116 if (unlikely(!ctx->supervisor)) {
4117 GEN_EXCP_PRIVOPC(ctx);
4120 EA = tcg_temp_new();
4121 gen_set_access_type(ACCESS_CACHE);
4122 gen_addr_reg_index(EA, ctx);
4123 val = tcg_temp_new();
4124 /* XXX: specification says this should be treated as a store by the MMU */
4125 gen_qemu_ld8u(val, EA, ctx->mem_idx);
4126 gen_qemu_st8(val, EA, ctx->mem_idx);
4133 GEN_HANDLER(dcbst, 0x1F, 0x16, 0x01, 0x03E00001, PPC_CACHE)
4135 /* XXX: specification say this is treated as a load by the MMU */
4136 TCGv t0 = tcg_temp_new();
4137 gen_set_access_type(ACCESS_CACHE);
4138 gen_addr_reg_index(t0, ctx);
4139 gen_qemu_ld8u(t0, t0, ctx->mem_idx);
4144 GEN_HANDLER(dcbt, 0x1F, 0x16, 0x08, 0x02000001, PPC_CACHE)
4146 /* interpreted as no-op */
4147 /* XXX: specification say this is treated as a load by the MMU
4148 * but does not generate any exception
4153 GEN_HANDLER(dcbtst, 0x1F, 0x16, 0x07, 0x02000001, PPC_CACHE)
4155 /* interpreted as no-op */
4156 /* XXX: specification say this is treated as a load by the MMU
4157 * but does not generate any exception
4162 #define op_dcbz(n) (*gen_op_dcbz[n][ctx->mem_idx])()
4163 static GenOpFunc *gen_op_dcbz[4][NB_MEM_FUNCS] = {
4164 /* 32 bytes cache line size */
4166 #define gen_op_dcbz_l32_le_raw gen_op_dcbz_l32_raw
4167 #define gen_op_dcbz_l32_le_user gen_op_dcbz_l32_user
4168 #define gen_op_dcbz_l32_le_kernel gen_op_dcbz_l32_kernel
4169 #define gen_op_dcbz_l32_le_hypv gen_op_dcbz_l32_hypv
4170 #define gen_op_dcbz_l32_le_64_raw gen_op_dcbz_l32_64_raw
4171 #define gen_op_dcbz_l32_le_64_user gen_op_dcbz_l32_64_user
4172 #define gen_op_dcbz_l32_le_64_kernel gen_op_dcbz_l32_64_kernel
4173 #define gen_op_dcbz_l32_le_64_hypv gen_op_dcbz_l32_64_hypv
4174 GEN_MEM_FUNCS(dcbz_l32),
4176 /* 64 bytes cache line size */
4178 #define gen_op_dcbz_l64_le_raw gen_op_dcbz_l64_raw
4179 #define gen_op_dcbz_l64_le_user gen_op_dcbz_l64_user
4180 #define gen_op_dcbz_l64_le_kernel gen_op_dcbz_l64_kernel
4181 #define gen_op_dcbz_l64_le_hypv gen_op_dcbz_l64_hypv
4182 #define gen_op_dcbz_l64_le_64_raw gen_op_dcbz_l64_64_raw
4183 #define gen_op_dcbz_l64_le_64_user gen_op_dcbz_l64_64_user
4184 #define gen_op_dcbz_l64_le_64_kernel gen_op_dcbz_l64_64_kernel
4185 #define gen_op_dcbz_l64_le_64_hypv gen_op_dcbz_l64_64_hypv
4186 GEN_MEM_FUNCS(dcbz_l64),
4188 /* 128 bytes cache line size */
4190 #define gen_op_dcbz_l128_le_raw gen_op_dcbz_l128_raw
4191 #define gen_op_dcbz_l128_le_user gen_op_dcbz_l128_user
4192 #define gen_op_dcbz_l128_le_kernel gen_op_dcbz_l128_kernel
4193 #define gen_op_dcbz_l128_le_hypv gen_op_dcbz_l128_hypv
4194 #define gen_op_dcbz_l128_le_64_raw gen_op_dcbz_l128_64_raw
4195 #define gen_op_dcbz_l128_le_64_user gen_op_dcbz_l128_64_user
4196 #define gen_op_dcbz_l128_le_64_kernel gen_op_dcbz_l128_64_kernel
4197 #define gen_op_dcbz_l128_le_64_hypv gen_op_dcbz_l128_64_hypv
4198 GEN_MEM_FUNCS(dcbz_l128),
4200 /* tunable cache line size */
4202 #define gen_op_dcbz_le_raw gen_op_dcbz_raw
4203 #define gen_op_dcbz_le_user gen_op_dcbz_user
4204 #define gen_op_dcbz_le_kernel gen_op_dcbz_kernel
4205 #define gen_op_dcbz_le_hypv gen_op_dcbz_hypv
4206 #define gen_op_dcbz_le_64_raw gen_op_dcbz_64_raw
4207 #define gen_op_dcbz_le_64_user gen_op_dcbz_64_user
4208 #define gen_op_dcbz_le_64_kernel gen_op_dcbz_64_kernel
4209 #define gen_op_dcbz_le_64_hypv gen_op_dcbz_64_hypv
4210 GEN_MEM_FUNCS(dcbz),
4214 static always_inline void handler_dcbz (DisasContext *ctx,
4215 int dcache_line_size)
4219 switch (dcache_line_size) {
4236 GEN_HANDLER(dcbz, 0x1F, 0x16, 0x1F, 0x03E00001, PPC_CACHE_DCBZ)
4238 gen_addr_reg_index(cpu_T[0], ctx);
4239 handler_dcbz(ctx, ctx->dcache_line_size);
4240 gen_op_check_reservation();
4243 GEN_HANDLER2(dcbz_970, "dcbz", 0x1F, 0x16, 0x1F, 0x03C00001, PPC_CACHE_DCBZT)
4245 gen_addr_reg_index(cpu_T[0], ctx);
4246 if (ctx->opcode & 0x00200000)
4247 handler_dcbz(ctx, ctx->dcache_line_size);
4249 handler_dcbz(ctx, -1);
4250 gen_op_check_reservation();
4254 #define op_icbi() (*gen_op_icbi[ctx->mem_idx])()
4255 #define gen_op_icbi_le_raw gen_op_icbi_raw
4256 #define gen_op_icbi_le_user gen_op_icbi_user
4257 #define gen_op_icbi_le_kernel gen_op_icbi_kernel
4258 #define gen_op_icbi_le_hypv gen_op_icbi_hypv
4259 #define gen_op_icbi_le_64_raw gen_op_icbi_64_raw
4260 #define gen_op_icbi_le_64_user gen_op_icbi_64_user
4261 #define gen_op_icbi_le_64_kernel gen_op_icbi_64_kernel
4262 #define gen_op_icbi_le_64_hypv gen_op_icbi_64_hypv
4263 static GenOpFunc *gen_op_icbi[NB_MEM_FUNCS] = {
4264 GEN_MEM_FUNCS(icbi),
4267 GEN_HANDLER(icbi, 0x1F, 0x16, 0x1E, 0x03E00001, PPC_CACHE_ICBI)
4269 /* NIP cannot be restored if the memory exception comes from an helper */
4270 gen_update_nip(ctx, ctx->nip - 4);
4271 gen_addr_reg_index(cpu_T[0], ctx);
4277 GEN_HANDLER(dcba, 0x1F, 0x16, 0x17, 0x03E00001, PPC_CACHE_DCBA)
4279 /* interpreted as no-op */
4280 /* XXX: specification say this is treated as a store by the MMU
4281 * but does not generate any exception
4285 /*** Segment register manipulation ***/
4286 /* Supervisor only: */
4288 GEN_HANDLER(mfsr, 0x1F, 0x13, 0x12, 0x0010F801, PPC_SEGMENT)
4290 #if defined(CONFIG_USER_ONLY)
4291 GEN_EXCP_PRIVREG(ctx);
4293 if (unlikely(!ctx->supervisor)) {
4294 GEN_EXCP_PRIVREG(ctx);
4297 tcg_gen_movi_tl(cpu_T[1], SR(ctx->opcode));
4299 tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_T[0]);
4304 GEN_HANDLER(mfsrin, 0x1F, 0x13, 0x14, 0x001F0001, PPC_SEGMENT)
4306 #if defined(CONFIG_USER_ONLY)
4307 GEN_EXCP_PRIVREG(ctx);
4309 if (unlikely(!ctx->supervisor)) {
4310 GEN_EXCP_PRIVREG(ctx);
4313 tcg_gen_mov_tl(cpu_T[1], cpu_gpr[rB(ctx->opcode)]);
4316 tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_T[0]);
4321 GEN_HANDLER(mtsr, 0x1F, 0x12, 0x06, 0x0010F801, PPC_SEGMENT)
4323 #if defined(CONFIG_USER_ONLY)
4324 GEN_EXCP_PRIVREG(ctx);
4326 if (unlikely(!ctx->supervisor)) {
4327 GEN_EXCP_PRIVREG(ctx);
4330 tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rS(ctx->opcode)]);
4331 tcg_gen_movi_tl(cpu_T[1], SR(ctx->opcode));
4337 GEN_HANDLER(mtsrin, 0x1F, 0x12, 0x07, 0x001F0001, PPC_SEGMENT)
4339 #if defined(CONFIG_USER_ONLY)
4340 GEN_EXCP_PRIVREG(ctx);
4342 if (unlikely(!ctx->supervisor)) {
4343 GEN_EXCP_PRIVREG(ctx);
4346 tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rS(ctx->opcode)]);
4347 tcg_gen_mov_tl(cpu_T[1], cpu_gpr[rB(ctx->opcode)]);
4353 #if defined(TARGET_PPC64)
4354 /* Specific implementation for PowerPC 64 "bridge" emulation using SLB */
4356 GEN_HANDLER2(mfsr_64b, "mfsr", 0x1F, 0x13, 0x12, 0x0010F801, PPC_SEGMENT_64B)
4358 #if defined(CONFIG_USER_ONLY)
4359 GEN_EXCP_PRIVREG(ctx);
4361 if (unlikely(!ctx->supervisor)) {
4362 GEN_EXCP_PRIVREG(ctx);
4365 tcg_gen_movi_tl(cpu_T[1], SR(ctx->opcode));
4367 tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_T[0]);
4372 GEN_HANDLER2(mfsrin_64b, "mfsrin", 0x1F, 0x13, 0x14, 0x001F0001,
4375 #if defined(CONFIG_USER_ONLY)
4376 GEN_EXCP_PRIVREG(ctx);
4378 if (unlikely(!ctx->supervisor)) {
4379 GEN_EXCP_PRIVREG(ctx);
4382 tcg_gen_mov_tl(cpu_T[1], cpu_gpr[rB(ctx->opcode)]);
4385 tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_T[0]);
4390 GEN_HANDLER2(mtsr_64b, "mtsr", 0x1F, 0x12, 0x06, 0x0010F801, PPC_SEGMENT_64B)
4392 #if defined(CONFIG_USER_ONLY)
4393 GEN_EXCP_PRIVREG(ctx);
4395 if (unlikely(!ctx->supervisor)) {
4396 GEN_EXCP_PRIVREG(ctx);
4399 tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rS(ctx->opcode)]);
4400 tcg_gen_movi_tl(cpu_T[1], SR(ctx->opcode));
4406 GEN_HANDLER2(mtsrin_64b, "mtsrin", 0x1F, 0x12, 0x07, 0x001F0001,
4409 #if defined(CONFIG_USER_ONLY)
4410 GEN_EXCP_PRIVREG(ctx);
4412 if (unlikely(!ctx->supervisor)) {
4413 GEN_EXCP_PRIVREG(ctx);
4416 tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rS(ctx->opcode)]);
4417 tcg_gen_mov_tl(cpu_T[1], cpu_gpr[rB(ctx->opcode)]);
4422 #endif /* defined(TARGET_PPC64) */
4424 /*** Lookaside buffer management ***/
4425 /* Optional & supervisor only: */
4427 GEN_HANDLER(tlbia, 0x1F, 0x12, 0x0B, 0x03FFFC01, PPC_MEM_TLBIA)
4429 #if defined(CONFIG_USER_ONLY)
4430 GEN_EXCP_PRIVOPC(ctx);
4432 if (unlikely(!ctx->supervisor)) {
4433 GEN_EXCP_PRIVOPC(ctx);
4441 GEN_HANDLER(tlbie, 0x1F, 0x12, 0x09, 0x03FF0001, PPC_MEM_TLBIE)
4443 #if defined(CONFIG_USER_ONLY)
4444 GEN_EXCP_PRIVOPC(ctx);
4446 if (unlikely(!ctx->supervisor)) {
4447 GEN_EXCP_PRIVOPC(ctx);
4450 tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rB(ctx->opcode)]);
4451 #if defined(TARGET_PPC64)
4461 GEN_HANDLER(tlbsync, 0x1F, 0x16, 0x11, 0x03FFF801, PPC_MEM_TLBSYNC)
4463 #if defined(CONFIG_USER_ONLY)
4464 GEN_EXCP_PRIVOPC(ctx);
4466 if (unlikely(!ctx->supervisor)) {
4467 GEN_EXCP_PRIVOPC(ctx);
4470 /* This has no effect: it should ensure that all previous
4471 * tlbie have completed
4477 #if defined(TARGET_PPC64)
4479 GEN_HANDLER(slbia, 0x1F, 0x12, 0x0F, 0x03FFFC01, PPC_SLBI)
4481 #if defined(CONFIG_USER_ONLY)
4482 GEN_EXCP_PRIVOPC(ctx);
4484 if (unlikely(!ctx->supervisor)) {
4485 GEN_EXCP_PRIVOPC(ctx);
4493 GEN_HANDLER(slbie, 0x1F, 0x12, 0x0D, 0x03FF0001, PPC_SLBI)
4495 #if defined(CONFIG_USER_ONLY)
4496 GEN_EXCP_PRIVOPC(ctx);
4498 if (unlikely(!ctx->supervisor)) {
4499 GEN_EXCP_PRIVOPC(ctx);
4502 tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rB(ctx->opcode)]);
4508 /*** External control ***/
4510 #define op_eciwx() (*gen_op_eciwx[ctx->mem_idx])()
4511 #define op_ecowx() (*gen_op_ecowx[ctx->mem_idx])()
4512 static GenOpFunc *gen_op_eciwx[NB_MEM_FUNCS] = {
4513 GEN_MEM_FUNCS(eciwx),
4515 static GenOpFunc *gen_op_ecowx[NB_MEM_FUNCS] = {
4516 GEN_MEM_FUNCS(ecowx),
4520 GEN_HANDLER(eciwx, 0x1F, 0x16, 0x0D, 0x00000001, PPC_EXTERN)
4522 /* Should check EAR[E] & alignment ! */
4523 gen_set_access_type(ACCESS_RES);
4524 gen_addr_reg_index(cpu_T[0], ctx);
4526 tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_T[0]);
4530 GEN_HANDLER(ecowx, 0x1F, 0x16, 0x09, 0x00000001, PPC_EXTERN)
4532 /* Should check EAR[E] & alignment ! */
4533 gen_addr_reg_index(cpu_T[0], ctx);
4534 tcg_gen_mov_tl(cpu_T[1], cpu_gpr[rS(ctx->opcode)]);
4538 /* PowerPC 601 specific instructions */
4540 GEN_HANDLER(abs, 0x1F, 0x08, 0x0B, 0x0000F800, PPC_POWER_BR)
4542 tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rA(ctx->opcode)]);
4544 tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_T[0]);
4545 if (unlikely(Rc(ctx->opcode) != 0))
4546 gen_set_Rc0(ctx, cpu_T[0]);
4550 GEN_HANDLER(abso, 0x1F, 0x08, 0x1B, 0x0000F800, PPC_POWER_BR)
4552 tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rA(ctx->opcode)]);
4553 gen_op_POWER_abso();
4554 tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_T[0]);
4555 if (unlikely(Rc(ctx->opcode) != 0))
4556 gen_set_Rc0(ctx, cpu_T[0]);
4560 GEN_HANDLER(clcs, 0x1F, 0x10, 0x13, 0x0000F800, PPC_POWER_BR)
4562 tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rA(ctx->opcode)]);
4563 gen_op_POWER_clcs();
4564 /* Rc=1 sets CR0 to an undefined state */
4565 tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_T[0]);
4569 GEN_HANDLER(div, 0x1F, 0x0B, 0x0A, 0x00000000, PPC_POWER_BR)
4571 tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rA(ctx->opcode)]);
4572 tcg_gen_mov_tl(cpu_T[1], cpu_gpr[rB(ctx->opcode)]);
4574 tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_T[0]);
4575 if (unlikely(Rc(ctx->opcode) != 0))
4576 gen_set_Rc0(ctx, cpu_T[0]);
4580 GEN_HANDLER(divo, 0x1F, 0x0B, 0x1A, 0x00000000, PPC_POWER_BR)
4582 tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rA(ctx->opcode)]);
4583 tcg_gen_mov_tl(cpu_T[1], cpu_gpr[rB(ctx->opcode)]);
4584 gen_op_POWER_divo();
4585 tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_T[0]);
4586 if (unlikely(Rc(ctx->opcode) != 0))
4587 gen_set_Rc0(ctx, cpu_T[0]);
4591 GEN_HANDLER(divs, 0x1F, 0x0B, 0x0B, 0x00000000, PPC_POWER_BR)
4593 tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rA(ctx->opcode)]);
4594 tcg_gen_mov_tl(cpu_T[1], cpu_gpr[rB(ctx->opcode)]);
4595 gen_op_POWER_divs();
4596 tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_T[0]);
4597 if (unlikely(Rc(ctx->opcode) != 0))
4598 gen_set_Rc0(ctx, cpu_T[0]);
4601 /* divso - divso. */
4602 GEN_HANDLER(divso, 0x1F, 0x0B, 0x1B, 0x00000000, PPC_POWER_BR)
4604 tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rA(ctx->opcode)]);
4605 tcg_gen_mov_tl(cpu_T[1], cpu_gpr[rB(ctx->opcode)]);
4606 gen_op_POWER_divso();
4607 tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_T[0]);
4608 if (unlikely(Rc(ctx->opcode) != 0))
4609 gen_set_Rc0(ctx, cpu_T[0]);
4613 GEN_HANDLER(doz, 0x1F, 0x08, 0x08, 0x00000000, PPC_POWER_BR)
4615 tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rA(ctx->opcode)]);
4616 tcg_gen_mov_tl(cpu_T[1], cpu_gpr[rB(ctx->opcode)]);
4618 tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_T[0]);
4619 if (unlikely(Rc(ctx->opcode) != 0))
4620 gen_set_Rc0(ctx, cpu_T[0]);
4624 GEN_HANDLER(dozo, 0x1F, 0x08, 0x18, 0x00000000, PPC_POWER_BR)
4626 tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rA(ctx->opcode)]);
4627 tcg_gen_mov_tl(cpu_T[1], cpu_gpr[rB(ctx->opcode)]);
4628 gen_op_POWER_dozo();
4629 tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_T[0]);
4630 if (unlikely(Rc(ctx->opcode) != 0))
4631 gen_set_Rc0(ctx, cpu_T[0]);
4635 GEN_HANDLER(dozi, 0x09, 0xFF, 0xFF, 0x00000000, PPC_POWER_BR)
4637 tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rA(ctx->opcode)]);
4638 tcg_gen_movi_tl(cpu_T[1], SIMM(ctx->opcode));
4640 tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_T[0]);
4643 /* As lscbx load from memory byte after byte, it's always endian safe.
4644 * Original POWER is 32 bits only, define 64 bits ops as 32 bits ones
4646 #define op_POWER_lscbx(start, ra, rb) \
4647 (*gen_op_POWER_lscbx[ctx->mem_idx])(start, ra, rb)
4648 #define gen_op_POWER_lscbx_64_raw gen_op_POWER_lscbx_raw
4649 #define gen_op_POWER_lscbx_64_user gen_op_POWER_lscbx_user
4650 #define gen_op_POWER_lscbx_64_kernel gen_op_POWER_lscbx_kernel
4651 #define gen_op_POWER_lscbx_64_hypv gen_op_POWER_lscbx_hypv
4652 #define gen_op_POWER_lscbx_le_raw gen_op_POWER_lscbx_raw
4653 #define gen_op_POWER_lscbx_le_user gen_op_POWER_lscbx_user
4654 #define gen_op_POWER_lscbx_le_kernel gen_op_POWER_lscbx_kernel
4655 #define gen_op_POWER_lscbx_le_hypv gen_op_POWER_lscbx_hypv
4656 #define gen_op_POWER_lscbx_le_64_raw gen_op_POWER_lscbx_raw
4657 #define gen_op_POWER_lscbx_le_64_user gen_op_POWER_lscbx_user
4658 #define gen_op_POWER_lscbx_le_64_kernel gen_op_POWER_lscbx_kernel
4659 #define gen_op_POWER_lscbx_le_64_hypv gen_op_POWER_lscbx_hypv
4660 static GenOpFunc3 *gen_op_POWER_lscbx[NB_MEM_FUNCS] = {
4661 GEN_MEM_FUNCS(POWER_lscbx),
4664 /* lscbx - lscbx. */
4665 GEN_HANDLER(lscbx, 0x1F, 0x15, 0x08, 0x00000000, PPC_POWER_BR)
4667 int ra = rA(ctx->opcode);
4668 int rb = rB(ctx->opcode);
4670 gen_addr_reg_index(cpu_T[0], ctx);
4674 /* NIP cannot be restored if the memory exception comes from an helper */
4675 gen_update_nip(ctx, ctx->nip - 4);
4676 tcg_gen_andi_tl(cpu_T[1], cpu_xer, 0x7F);
4677 tcg_gen_shri_tl(cpu_T[2], cpu_xer, XER_CMP);
4678 tcg_gen_andi_tl(cpu_T[2], cpu_T[2], 0xFF);
4679 op_POWER_lscbx(rD(ctx->opcode), ra, rb);
4680 tcg_gen_andi_tl(cpu_xer, cpu_xer, ~0x7F);
4681 tcg_gen_or_tl(cpu_xer, cpu_xer, cpu_T[0]);
4682 if (unlikely(Rc(ctx->opcode) != 0))
4683 gen_set_Rc0(ctx, cpu_T[0]);
4686 /* maskg - maskg. */
4687 GEN_HANDLER(maskg, 0x1F, 0x1D, 0x00, 0x00000000, PPC_POWER_BR)
4689 tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rS(ctx->opcode)]);
4690 tcg_gen_mov_tl(cpu_T[1], cpu_gpr[rB(ctx->opcode)]);
4691 gen_op_POWER_maskg();
4692 tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], cpu_T[0]);
4693 if (unlikely(Rc(ctx->opcode) != 0))
4694 gen_set_Rc0(ctx, cpu_T[0]);
4697 /* maskir - maskir. */
4698 GEN_HANDLER(maskir, 0x1F, 0x1D, 0x10, 0x00000000, PPC_POWER_BR)
4700 tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rA(ctx->opcode)]);
4701 tcg_gen_mov_tl(cpu_T[1], cpu_gpr[rS(ctx->opcode)]);
4702 tcg_gen_mov_tl(cpu_T[2], cpu_gpr[rB(ctx->opcode)]);
4703 gen_op_POWER_maskir();
4704 tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], cpu_T[0]);
4705 if (unlikely(Rc(ctx->opcode) != 0))
4706 gen_set_Rc0(ctx, cpu_T[0]);
4710 GEN_HANDLER(mul, 0x1F, 0x0B, 0x03, 0x00000000, PPC_POWER_BR)
4712 tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rA(ctx->opcode)]);
4713 tcg_gen_mov_tl(cpu_T[1], cpu_gpr[rB(ctx->opcode)]);
4715 tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_T[0]);
4716 if (unlikely(Rc(ctx->opcode) != 0))
4717 gen_set_Rc0(ctx, cpu_T[0]);
4721 GEN_HANDLER(mulo, 0x1F, 0x0B, 0x13, 0x00000000, PPC_POWER_BR)
4723 tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rA(ctx->opcode)]);
4724 tcg_gen_mov_tl(cpu_T[1], cpu_gpr[rB(ctx->opcode)]);
4725 gen_op_POWER_mulo();
4726 tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_T[0]);
4727 if (unlikely(Rc(ctx->opcode) != 0))
4728 gen_set_Rc0(ctx, cpu_T[0]);
4732 GEN_HANDLER(nabs, 0x1F, 0x08, 0x0F, 0x00000000, PPC_POWER_BR)
4734 tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rA(ctx->opcode)]);
4735 gen_op_POWER_nabs();
4736 tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_T[0]);
4737 if (unlikely(Rc(ctx->opcode) != 0))
4738 gen_set_Rc0(ctx, cpu_T[0]);
4741 /* nabso - nabso. */
4742 GEN_HANDLER(nabso, 0x1F, 0x08, 0x1F, 0x00000000, PPC_POWER_BR)
4744 tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rA(ctx->opcode)]);
4745 gen_op_POWER_nabso();
4746 tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_T[0]);
4747 if (unlikely(Rc(ctx->opcode) != 0))
4748 gen_set_Rc0(ctx, cpu_T[0]);
4752 GEN_HANDLER(rlmi, 0x16, 0xFF, 0xFF, 0x00000000, PPC_POWER_BR)
4756 mb = MB(ctx->opcode);
4757 me = ME(ctx->opcode);
4758 tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rS(ctx->opcode)]);
4759 tcg_gen_mov_tl(cpu_T[1], cpu_gpr[rA(ctx->opcode)]);
4760 tcg_gen_mov_tl(cpu_T[2], cpu_gpr[rB(ctx->opcode)]);
4761 gen_op_POWER_rlmi(MASK(mb, me), ~MASK(mb, me));
4762 tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], cpu_T[0]);
4763 if (unlikely(Rc(ctx->opcode) != 0))
4764 gen_set_Rc0(ctx, cpu_T[0]);
4768 GEN_HANDLER(rrib, 0x1F, 0x19, 0x10, 0x00000000, PPC_POWER_BR)
4770 tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rS(ctx->opcode)]);
4771 tcg_gen_mov_tl(cpu_T[1], cpu_gpr[rA(ctx->opcode)]);
4772 tcg_gen_mov_tl(cpu_T[2], cpu_gpr[rB(ctx->opcode)]);
4773 gen_op_POWER_rrib();
4774 tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], cpu_T[0]);
4775 if (unlikely(Rc(ctx->opcode) != 0))
4776 gen_set_Rc0(ctx, cpu_T[0]);
4780 GEN_HANDLER(sle, 0x1F, 0x19, 0x04, 0x00000000, PPC_POWER_BR)
4782 tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rS(ctx->opcode)]);
4783 tcg_gen_mov_tl(cpu_T[1], cpu_gpr[rB(ctx->opcode)]);
4785 tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], cpu_T[0]);
4786 if (unlikely(Rc(ctx->opcode) != 0))
4787 gen_set_Rc0(ctx, cpu_T[0]);
4791 GEN_HANDLER(sleq, 0x1F, 0x19, 0x06, 0x00000000, PPC_POWER_BR)
4793 tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rS(ctx->opcode)]);
4794 tcg_gen_mov_tl(cpu_T[1], cpu_gpr[rB(ctx->opcode)]);
4795 gen_op_POWER_sleq();
4796 tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], cpu_T[0]);
4797 if (unlikely(Rc(ctx->opcode) != 0))
4798 gen_set_Rc0(ctx, cpu_T[0]);
4802 GEN_HANDLER(sliq, 0x1F, 0x18, 0x05, 0x00000000, PPC_POWER_BR)
4804 tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rS(ctx->opcode)]);
4805 tcg_gen_movi_tl(cpu_T[1], SH(ctx->opcode));
4807 tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], cpu_T[0]);
4808 if (unlikely(Rc(ctx->opcode) != 0))
4809 gen_set_Rc0(ctx, cpu_T[0]);
4812 /* slliq - slliq. */
4813 GEN_HANDLER(slliq, 0x1F, 0x18, 0x07, 0x00000000, PPC_POWER_BR)
4815 tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rS(ctx->opcode)]);
4816 tcg_gen_movi_tl(cpu_T[1], SH(ctx->opcode));
4817 gen_op_POWER_sleq();
4818 tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], cpu_T[0]);
4819 if (unlikely(Rc(ctx->opcode) != 0))
4820 gen_set_Rc0(ctx, cpu_T[0]);
4824 GEN_HANDLER(sllq, 0x1F, 0x18, 0x06, 0x00000000, PPC_POWER_BR)
4826 tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rS(ctx->opcode)]);
4827 tcg_gen_mov_tl(cpu_T[1], cpu_gpr[rB(ctx->opcode)]);
4828 gen_op_POWER_sllq();
4829 tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], cpu_T[0]);
4830 if (unlikely(Rc(ctx->opcode) != 0))
4831 gen_set_Rc0(ctx, cpu_T[0]);
4835 GEN_HANDLER(slq, 0x1F, 0x18, 0x04, 0x00000000, PPC_POWER_BR)
4837 tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rS(ctx->opcode)]);
4838 tcg_gen_mov_tl(cpu_T[1], cpu_gpr[rB(ctx->opcode)]);
4840 tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], cpu_T[0]);
4841 if (unlikely(Rc(ctx->opcode) != 0))
4842 gen_set_Rc0(ctx, cpu_T[0]);
4845 /* sraiq - sraiq. */
4846 GEN_HANDLER(sraiq, 0x1F, 0x18, 0x1D, 0x00000000, PPC_POWER_BR)
4848 tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rS(ctx->opcode)]);
4849 tcg_gen_movi_tl(cpu_T[1], SH(ctx->opcode));
4850 gen_op_POWER_sraq();
4851 tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], cpu_T[0]);
4852 if (unlikely(Rc(ctx->opcode) != 0))
4853 gen_set_Rc0(ctx, cpu_T[0]);
4857 GEN_HANDLER(sraq, 0x1F, 0x18, 0x1C, 0x00000000, PPC_POWER_BR)
4859 tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rS(ctx->opcode)]);
4860 tcg_gen_mov_tl(cpu_T[1], cpu_gpr[rB(ctx->opcode)]);
4861 gen_op_POWER_sraq();
4862 tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], cpu_T[0]);
4863 if (unlikely(Rc(ctx->opcode) != 0))
4864 gen_set_Rc0(ctx, cpu_T[0]);
4868 GEN_HANDLER(sre, 0x1F, 0x19, 0x14, 0x00000000, PPC_POWER_BR)
4870 tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rS(ctx->opcode)]);
4871 tcg_gen_mov_tl(cpu_T[1], cpu_gpr[rB(ctx->opcode)]);
4873 tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], cpu_T[0]);
4874 if (unlikely(Rc(ctx->opcode) != 0))
4875 gen_set_Rc0(ctx, cpu_T[0]);
4879 GEN_HANDLER(srea, 0x1F, 0x19, 0x1C, 0x00000000, PPC_POWER_BR)
4881 tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rS(ctx->opcode)]);
4882 tcg_gen_mov_tl(cpu_T[1], cpu_gpr[rB(ctx->opcode)]);
4883 gen_op_POWER_srea();
4884 tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], cpu_T[0]);
4885 if (unlikely(Rc(ctx->opcode) != 0))
4886 gen_set_Rc0(ctx, cpu_T[0]);
4890 GEN_HANDLER(sreq, 0x1F, 0x19, 0x16, 0x00000000, PPC_POWER_BR)
4892 tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rS(ctx->opcode)]);
4893 tcg_gen_mov_tl(cpu_T[1], cpu_gpr[rB(ctx->opcode)]);
4894 gen_op_POWER_sreq();
4895 tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], cpu_T[0]);
4896 if (unlikely(Rc(ctx->opcode) != 0))
4897 gen_set_Rc0(ctx, cpu_T[0]);
4901 GEN_HANDLER(sriq, 0x1F, 0x18, 0x15, 0x00000000, PPC_POWER_BR)
4903 tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rS(ctx->opcode)]);
4904 tcg_gen_movi_tl(cpu_T[1], SH(ctx->opcode));
4906 tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], cpu_T[0]);
4907 if (unlikely(Rc(ctx->opcode) != 0))
4908 gen_set_Rc0(ctx, cpu_T[0]);
4912 GEN_HANDLER(srliq, 0x1F, 0x18, 0x17, 0x00000000, PPC_POWER_BR)
4914 tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rS(ctx->opcode)]);
4915 tcg_gen_mov_tl(cpu_T[1], cpu_gpr[rB(ctx->opcode)]);
4916 tcg_gen_movi_tl(cpu_T[1], SH(ctx->opcode));
4917 gen_op_POWER_srlq();
4918 tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], cpu_T[0]);
4919 if (unlikely(Rc(ctx->opcode) != 0))
4920 gen_set_Rc0(ctx, cpu_T[0]);
4924 GEN_HANDLER(srlq, 0x1F, 0x18, 0x16, 0x00000000, PPC_POWER_BR)
4926 tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rS(ctx->opcode)]);
4927 tcg_gen_mov_tl(cpu_T[1], cpu_gpr[rB(ctx->opcode)]);
4928 gen_op_POWER_srlq();
4929 tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], cpu_T[0]);
4930 if (unlikely(Rc(ctx->opcode) != 0))
4931 gen_set_Rc0(ctx, cpu_T[0]);
4935 GEN_HANDLER(srq, 0x1F, 0x18, 0x14, 0x00000000, PPC_POWER_BR)
4937 tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rS(ctx->opcode)]);
4938 tcg_gen_mov_tl(cpu_T[1], cpu_gpr[rB(ctx->opcode)]);
4940 tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], cpu_T[0]);
4941 if (unlikely(Rc(ctx->opcode) != 0))
4942 gen_set_Rc0(ctx, cpu_T[0]);
4945 /* PowerPC 602 specific instructions */
4947 GEN_HANDLER(dsa, 0x1F, 0x14, 0x13, 0x03FFF801, PPC_602_SPEC)
4950 GEN_EXCP_INVAL(ctx);
4954 GEN_HANDLER(esa, 0x1F, 0x14, 0x12, 0x03FFF801, PPC_602_SPEC)
4957 GEN_EXCP_INVAL(ctx);
4961 GEN_HANDLER(mfrom, 0x1F, 0x09, 0x08, 0x03E0F801, PPC_602_SPEC)
4963 #if defined(CONFIG_USER_ONLY)
4964 GEN_EXCP_PRIVOPC(ctx);
4966 if (unlikely(!ctx->supervisor)) {
4967 GEN_EXCP_PRIVOPC(ctx);
4970 tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rA(ctx->opcode)]);
4972 tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_T[0]);
4976 /* 602 - 603 - G2 TLB management */
4978 GEN_HANDLER2(tlbld_6xx, "tlbld", 0x1F, 0x12, 0x1E, 0x03FF0001, PPC_6xx_TLB)
4980 #if defined(CONFIG_USER_ONLY)
4981 GEN_EXCP_PRIVOPC(ctx);
4983 if (unlikely(!ctx->supervisor)) {
4984 GEN_EXCP_PRIVOPC(ctx);
4987 tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rB(ctx->opcode)]);
4993 GEN_HANDLER2(tlbli_6xx, "tlbli", 0x1F, 0x12, 0x1F, 0x03FF0001, PPC_6xx_TLB)
4995 #if defined(CONFIG_USER_ONLY)
4996 GEN_EXCP_PRIVOPC(ctx);
4998 if (unlikely(!ctx->supervisor)) {
4999 GEN_EXCP_PRIVOPC(ctx);
5002 tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rB(ctx->opcode)]);
5007 /* 74xx TLB management */
5009 GEN_HANDLER2(tlbld_74xx, "tlbld", 0x1F, 0x12, 0x1E, 0x03FF0001, PPC_74xx_TLB)
5011 #if defined(CONFIG_USER_ONLY)
5012 GEN_EXCP_PRIVOPC(ctx);
5014 if (unlikely(!ctx->supervisor)) {
5015 GEN_EXCP_PRIVOPC(ctx);
5018 tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rB(ctx->opcode)]);
5019 gen_op_74xx_tlbld();
5024 GEN_HANDLER2(tlbli_74xx, "tlbli", 0x1F, 0x12, 0x1F, 0x03FF0001, PPC_74xx_TLB)
5026 #if defined(CONFIG_USER_ONLY)
5027 GEN_EXCP_PRIVOPC(ctx);
5029 if (unlikely(!ctx->supervisor)) {
5030 GEN_EXCP_PRIVOPC(ctx);
5033 tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rB(ctx->opcode)]);
5034 gen_op_74xx_tlbli();
5038 /* POWER instructions not in PowerPC 601 */
5040 GEN_HANDLER(clf, 0x1F, 0x16, 0x03, 0x03E00000, PPC_POWER)
5042 /* Cache line flush: implemented as no-op */
5046 GEN_HANDLER(cli, 0x1F, 0x16, 0x0F, 0x03E00000, PPC_POWER)
5048 /* Cache line invalidate: privileged and treated as no-op */
5049 #if defined(CONFIG_USER_ONLY)
5050 GEN_EXCP_PRIVOPC(ctx);
5052 if (unlikely(!ctx->supervisor)) {
5053 GEN_EXCP_PRIVOPC(ctx);
5060 GEN_HANDLER(dclst, 0x1F, 0x16, 0x13, 0x03E00000, PPC_POWER)
5062 /* Data cache line store: treated as no-op */
5065 GEN_HANDLER(mfsri, 0x1F, 0x13, 0x13, 0x00000001, PPC_POWER)
5067 #if defined(CONFIG_USER_ONLY)
5068 GEN_EXCP_PRIVOPC(ctx);
5070 if (unlikely(!ctx->supervisor)) {
5071 GEN_EXCP_PRIVOPC(ctx);
5074 int ra = rA(ctx->opcode);
5075 int rd = rD(ctx->opcode);
5077 gen_addr_reg_index(cpu_T[0], ctx);
5078 gen_op_POWER_mfsri();
5079 tcg_gen_mov_tl(cpu_gpr[rd], cpu_T[0]);
5080 if (ra != 0 && ra != rd)
5081 tcg_gen_mov_tl(cpu_gpr[ra], cpu_T[1]);
5085 GEN_HANDLER(rac, 0x1F, 0x12, 0x19, 0x00000001, PPC_POWER)
5087 #if defined(CONFIG_USER_ONLY)
5088 GEN_EXCP_PRIVOPC(ctx);
5090 if (unlikely(!ctx->supervisor)) {
5091 GEN_EXCP_PRIVOPC(ctx);
5094 gen_addr_reg_index(cpu_T[0], ctx);
5096 tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_T[0]);
5100 GEN_HANDLER(rfsvc, 0x13, 0x12, 0x02, 0x03FFF0001, PPC_POWER)
5102 #if defined(CONFIG_USER_ONLY)
5103 GEN_EXCP_PRIVOPC(ctx);
5105 if (unlikely(!ctx->supervisor)) {
5106 GEN_EXCP_PRIVOPC(ctx);
5109 gen_op_POWER_rfsvc();
5114 /* svc is not implemented for now */
5116 /* POWER2 specific instructions */
5117 /* Quad manipulation (load/store two floats at a time) */
5118 /* Original POWER2 is 32 bits only, define 64 bits ops as 32 bits ones */
5119 #define op_POWER2_lfq() (*gen_op_POWER2_lfq[ctx->mem_idx])()
5120 #define op_POWER2_stfq() (*gen_op_POWER2_stfq[ctx->mem_idx])()
5121 #define gen_op_POWER2_lfq_64_raw gen_op_POWER2_lfq_raw
5122 #define gen_op_POWER2_lfq_64_user gen_op_POWER2_lfq_user
5123 #define gen_op_POWER2_lfq_64_kernel gen_op_POWER2_lfq_kernel
5124 #define gen_op_POWER2_lfq_64_hypv gen_op_POWER2_lfq_hypv
5125 #define gen_op_POWER2_lfq_le_64_raw gen_op_POWER2_lfq_le_raw
5126 #define gen_op_POWER2_lfq_le_64_user gen_op_POWER2_lfq_le_user
5127 #define gen_op_POWER2_lfq_le_64_kernel gen_op_POWER2_lfq_le_kernel
5128 #define gen_op_POWER2_lfq_le_64_hypv gen_op_POWER2_lfq_le_hypv
5129 #define gen_op_POWER2_stfq_64_raw gen_op_POWER2_stfq_raw
5130 #define gen_op_POWER2_stfq_64_user gen_op_POWER2_stfq_user
5131 #define gen_op_POWER2_stfq_64_kernel gen_op_POWER2_stfq_kernel
5132 #define gen_op_POWER2_stfq_64_hypv gen_op_POWER2_stfq_hypv
5133 #define gen_op_POWER2_stfq_le_64_raw gen_op_POWER2_stfq_le_raw
5134 #define gen_op_POWER2_stfq_le_64_user gen_op_POWER2_stfq_le_user
5135 #define gen_op_POWER2_stfq_le_64_kernel gen_op_POWER2_stfq_le_kernel
5136 #define gen_op_POWER2_stfq_le_64_hypv gen_op_POWER2_stfq_le_hypv
5137 static GenOpFunc *gen_op_POWER2_lfq[NB_MEM_FUNCS] = {
5138 GEN_MEM_FUNCS(POWER2_lfq),
5140 static GenOpFunc *gen_op_POWER2_stfq[NB_MEM_FUNCS] = {
5141 GEN_MEM_FUNCS(POWER2_stfq),
5145 GEN_HANDLER(lfq, 0x38, 0xFF, 0xFF, 0x00000003, PPC_POWER2)
5147 /* NIP cannot be restored if the memory exception comes from an helper */
5148 gen_update_nip(ctx, ctx->nip - 4);
5149 gen_addr_imm_index(cpu_T[0], ctx, 0);
5151 tcg_gen_mov_i64(cpu_fpr[rD(ctx->opcode)], cpu_FT[0]);
5152 tcg_gen_mov_i64(cpu_fpr[rD(ctx->opcode) + 1], cpu_FT[1]);
5156 GEN_HANDLER(lfqu, 0x39, 0xFF, 0xFF, 0x00000003, PPC_POWER2)
5158 int ra = rA(ctx->opcode);
5160 /* NIP cannot be restored if the memory exception comes from an helper */
5161 gen_update_nip(ctx, ctx->nip - 4);
5162 gen_addr_imm_index(cpu_T[0], ctx, 0);
5164 tcg_gen_mov_i64(cpu_fpr[rD(ctx->opcode)], cpu_FT[0]);
5165 tcg_gen_mov_i64(cpu_fpr[rD(ctx->opcode) + 1], cpu_FT[1]);
5167 tcg_gen_mov_tl(cpu_gpr[ra], cpu_T[0]);
5171 GEN_HANDLER(lfqux, 0x1F, 0x17, 0x19, 0x00000001, PPC_POWER2)
5173 int ra = rA(ctx->opcode);
5175 /* NIP cannot be restored if the memory exception comes from an helper */
5176 gen_update_nip(ctx, ctx->nip - 4);
5177 gen_addr_reg_index(cpu_T[0], ctx);
5179 tcg_gen_mov_i64(cpu_fpr[rD(ctx->opcode)], cpu_FT[0]);
5180 tcg_gen_mov_i64(cpu_fpr[rD(ctx->opcode) + 1], cpu_FT[1]);
5182 tcg_gen_mov_tl(cpu_gpr[ra], cpu_T[0]);
5186 GEN_HANDLER(lfqx, 0x1F, 0x17, 0x18, 0x00000001, PPC_POWER2)
5188 /* NIP cannot be restored if the memory exception comes from an helper */
5189 gen_update_nip(ctx, ctx->nip - 4);
5190 gen_addr_reg_index(cpu_T[0], ctx);
5192 tcg_gen_mov_i64(cpu_fpr[rD(ctx->opcode)], cpu_FT[0]);
5193 tcg_gen_mov_i64(cpu_fpr[rD(ctx->opcode) + 1], cpu_FT[1]);
5197 GEN_HANDLER(stfq, 0x3C, 0xFF, 0xFF, 0x00000003, PPC_POWER2)
5199 /* NIP cannot be restored if the memory exception comes from an helper */
5200 gen_update_nip(ctx, ctx->nip - 4);
5201 gen_addr_imm_index(cpu_T[0], ctx, 0);
5202 tcg_gen_mov_i64(cpu_FT[0], cpu_fpr[rS(ctx->opcode)]);
5203 tcg_gen_mov_i64(cpu_FT[1], cpu_fpr[rS(ctx->opcode) + 1]);
5208 GEN_HANDLER(stfqu, 0x3D, 0xFF, 0xFF, 0x00000003, PPC_POWER2)
5210 int ra = rA(ctx->opcode);
5212 /* NIP cannot be restored if the memory exception comes from an helper */
5213 gen_update_nip(ctx, ctx->nip - 4);
5214 gen_addr_imm_index(cpu_T[0], ctx, 0);
5215 tcg_gen_mov_i64(cpu_FT[0], cpu_fpr[rS(ctx->opcode)]);
5216 tcg_gen_mov_i64(cpu_FT[1], cpu_fpr[rS(ctx->opcode) + 1]);
5219 tcg_gen_mov_tl(cpu_gpr[ra], cpu_T[0]);
5223 GEN_HANDLER(stfqux, 0x1F, 0x17, 0x1D, 0x00000001, PPC_POWER2)
5225 int ra = rA(ctx->opcode);
5227 /* NIP cannot be restored if the memory exception comes from an helper */
5228 gen_update_nip(ctx, ctx->nip - 4);
5229 gen_addr_reg_index(cpu_T[0], ctx);
5230 tcg_gen_mov_i64(cpu_FT[0], cpu_fpr[rS(ctx->opcode)]);
5231 tcg_gen_mov_i64(cpu_FT[1], cpu_fpr[rS(ctx->opcode) + 1]);
5234 tcg_gen_mov_tl(cpu_gpr[ra], cpu_T[0]);
5238 GEN_HANDLER(stfqx, 0x1F, 0x17, 0x1C, 0x00000001, PPC_POWER2)
5240 /* NIP cannot be restored if the memory exception comes from an helper */
5241 gen_update_nip(ctx, ctx->nip - 4);
5242 gen_addr_reg_index(cpu_T[0], ctx);
5243 tcg_gen_mov_i64(cpu_FT[0], cpu_fpr[rS(ctx->opcode)]);
5244 tcg_gen_mov_i64(cpu_FT[1], cpu_fpr[rS(ctx->opcode) + 1]);
5248 /* BookE specific instructions */
5249 /* XXX: not implemented on 440 ? */
5250 GEN_HANDLER(mfapidi, 0x1F, 0x13, 0x08, 0x0000F801, PPC_MFAPIDI)
5253 GEN_EXCP_INVAL(ctx);
5256 /* XXX: not implemented on 440 ? */
5257 GEN_HANDLER(tlbiva, 0x1F, 0x12, 0x18, 0x03FFF801, PPC_TLBIVA)
5259 #if defined(CONFIG_USER_ONLY)
5260 GEN_EXCP_PRIVOPC(ctx);
5262 if (unlikely(!ctx->supervisor)) {
5263 GEN_EXCP_PRIVOPC(ctx);
5266 gen_addr_reg_index(cpu_T[0], ctx);
5267 /* Use the same micro-ops as for tlbie */
5268 #if defined(TARGET_PPC64)
5277 /* All 405 MAC instructions are translated here */
5278 static always_inline void gen_405_mulladd_insn (DisasContext *ctx,
5280 int ra, int rb, int rt, int Rc)
5284 t0 = tcg_temp_local_new();
5285 t1 = tcg_temp_local_new();
5287 switch (opc3 & 0x0D) {
5289 /* macchw - macchw. - macchwo - macchwo. */
5290 /* macchws - macchws. - macchwso - macchwso. */
5291 /* nmacchw - nmacchw. - nmacchwo - nmacchwo. */
5292 /* nmacchws - nmacchws. - nmacchwso - nmacchwso. */
5293 /* mulchw - mulchw. */
5294 tcg_gen_ext16s_tl(t0, cpu_gpr[ra]);
5295 tcg_gen_sari_tl(t1, cpu_gpr[rb], 16);
5296 tcg_gen_ext16s_tl(t1, t1);
5299 /* macchwu - macchwu. - macchwuo - macchwuo. */
5300 /* macchwsu - macchwsu. - macchwsuo - macchwsuo. */
5301 /* mulchwu - mulchwu. */
5302 tcg_gen_ext16u_tl(t0, cpu_gpr[ra]);
5303 tcg_gen_shri_tl(t1, cpu_gpr[rb], 16);
5304 tcg_gen_ext16u_tl(t1, t1);
5307 /* machhw - machhw. - machhwo - machhwo. */
5308 /* machhws - machhws. - machhwso - machhwso. */
5309 /* nmachhw - nmachhw. - nmachhwo - nmachhwo. */
5310 /* nmachhws - nmachhws. - nmachhwso - nmachhwso. */
5311 /* mulhhw - mulhhw. */
5312 tcg_gen_sari_tl(t0, cpu_gpr[ra], 16);
5313 tcg_gen_ext16s_tl(t0, t0);
5314 tcg_gen_sari_tl(t1, cpu_gpr[rb], 16);
5315 tcg_gen_ext16s_tl(t1, t1);
5318 /* machhwu - machhwu. - machhwuo - machhwuo. */
5319 /* machhwsu - machhwsu. - machhwsuo - machhwsuo. */
5320 /* mulhhwu - mulhhwu. */
5321 tcg_gen_shri_tl(t0, cpu_gpr[ra], 16);
5322 tcg_gen_ext16u_tl(t0, t0);
5323 tcg_gen_shri_tl(t1, cpu_gpr[rb], 16);
5324 tcg_gen_ext16u_tl(t1, t1);
5327 /* maclhw - maclhw. - maclhwo - maclhwo. */
5328 /* maclhws - maclhws. - maclhwso - maclhwso. */
5329 /* nmaclhw - nmaclhw. - nmaclhwo - nmaclhwo. */
5330 /* nmaclhws - nmaclhws. - nmaclhwso - nmaclhwso. */
5331 /* mullhw - mullhw. */
5332 tcg_gen_ext16s_tl(t0, cpu_gpr[ra]);
5333 tcg_gen_ext16s_tl(t1, cpu_gpr[rb]);
5336 /* maclhwu - maclhwu. - maclhwuo - maclhwuo. */
5337 /* maclhwsu - maclhwsu. - maclhwsuo - maclhwsuo. */
5338 /* mullhwu - mullhwu. */
5339 tcg_gen_ext16u_tl(t0, cpu_gpr[ra]);
5340 tcg_gen_ext16u_tl(t1, cpu_gpr[rb]);
5344 /* (n)multiply-and-accumulate (0x0C / 0x0E) */
5345 tcg_gen_mul_tl(t1, t0, t1);
5347 /* nmultiply-and-accumulate (0x0E) */
5348 tcg_gen_sub_tl(t0, cpu_gpr[rt], t1);
5350 /* multiply-and-accumulate (0x0C) */
5351 tcg_gen_add_tl(t0, cpu_gpr[rt], t1);
5355 /* Check overflow and/or saturate */
5356 int l1 = gen_new_label();
5359 /* Start with XER OV disabled, the most likely case */
5360 tcg_gen_andi_tl(cpu_xer, cpu_xer, ~(1 << XER_OV));
5364 tcg_gen_xor_tl(t1, cpu_gpr[rt], t1);
5365 tcg_gen_brcondi_tl(TCG_COND_GE, t1, 0, l1);
5366 tcg_gen_xor_tl(t1, cpu_gpr[rt], t0);
5367 tcg_gen_brcondi_tl(TCG_COND_LT, t1, 0, l1);
5370 tcg_gen_sari_tl(t0, cpu_gpr[rt], 31);
5371 tcg_gen_xori_tl(t0, t0, 0x7fffffff);
5375 tcg_gen_brcond_tl(TCG_COND_GEU, t0, t1, l1);
5378 tcg_gen_movi_tl(t0, UINT32_MAX);
5382 /* Check overflow */
5383 tcg_gen_ori_tl(cpu_xer, cpu_xer, (1 << XER_OV) | (1 << XER_SO));
5386 tcg_gen_mov_tl(cpu_gpr[rt], t0);
5389 tcg_gen_mul_tl(cpu_gpr[rt], t0, t1);
5393 if (unlikely(Rc) != 0) {
5395 gen_set_Rc0(ctx, cpu_gpr[rt]);
5399 #define GEN_MAC_HANDLER(name, opc2, opc3) \
5400 GEN_HANDLER(name, 0x04, opc2, opc3, 0x00000000, PPC_405_MAC) \
5402 gen_405_mulladd_insn(ctx, opc2, opc3, rA(ctx->opcode), rB(ctx->opcode), \
5403 rD(ctx->opcode), Rc(ctx->opcode)); \
5406 /* macchw - macchw. */
5407 GEN_MAC_HANDLER(macchw, 0x0C, 0x05);
5408 /* macchwo - macchwo. */
5409 GEN_MAC_HANDLER(macchwo, 0x0C, 0x15);
5410 /* macchws - macchws. */
5411 GEN_MAC_HANDLER(macchws, 0x0C, 0x07);
5412 /* macchwso - macchwso. */
5413 GEN_MAC_HANDLER(macchwso, 0x0C, 0x17);
5414 /* macchwsu - macchwsu. */
5415 GEN_MAC_HANDLER(macchwsu, 0x0C, 0x06);
5416 /* macchwsuo - macchwsuo. */
5417 GEN_MAC_HANDLER(macchwsuo, 0x0C, 0x16);
5418 /* macchwu - macchwu. */
5419 GEN_MAC_HANDLER(macchwu, 0x0C, 0x04);
5420 /* macchwuo - macchwuo. */
5421 GEN_MAC_HANDLER(macchwuo, 0x0C, 0x14);
5422 /* machhw - machhw. */
5423 GEN_MAC_HANDLER(machhw, 0x0C, 0x01);
5424 /* machhwo - machhwo. */
5425 GEN_MAC_HANDLER(machhwo, 0x0C, 0x11);
5426 /* machhws - machhws. */
5427 GEN_MAC_HANDLER(machhws, 0x0C, 0x03);
5428 /* machhwso - machhwso. */
5429 GEN_MAC_HANDLER(machhwso, 0x0C, 0x13);
5430 /* machhwsu - machhwsu. */
5431 GEN_MAC_HANDLER(machhwsu, 0x0C, 0x02);
5432 /* machhwsuo - machhwsuo. */
5433 GEN_MAC_HANDLER(machhwsuo, 0x0C, 0x12);
5434 /* machhwu - machhwu. */
5435 GEN_MAC_HANDLER(machhwu, 0x0C, 0x00);
5436 /* machhwuo - machhwuo. */
5437 GEN_MAC_HANDLER(machhwuo, 0x0C, 0x10);
5438 /* maclhw - maclhw. */
5439 GEN_MAC_HANDLER(maclhw, 0x0C, 0x0D);
5440 /* maclhwo - maclhwo. */
5441 GEN_MAC_HANDLER(maclhwo, 0x0C, 0x1D);
5442 /* maclhws - maclhws. */
5443 GEN_MAC_HANDLER(maclhws, 0x0C, 0x0F);
5444 /* maclhwso - maclhwso. */
5445 GEN_MAC_HANDLER(maclhwso, 0x0C, 0x1F);
5446 /* maclhwu - maclhwu. */
5447 GEN_MAC_HANDLER(maclhwu, 0x0C, 0x0C);
5448 /* maclhwuo - maclhwuo. */
5449 GEN_MAC_HANDLER(maclhwuo, 0x0C, 0x1C);
5450 /* maclhwsu - maclhwsu. */
5451 GEN_MAC_HANDLER(maclhwsu, 0x0C, 0x0E);
5452 /* maclhwsuo - maclhwsuo. */
5453 GEN_MAC_HANDLER(maclhwsuo, 0x0C, 0x1E);
5454 /* nmacchw - nmacchw. */
5455 GEN_MAC_HANDLER(nmacchw, 0x0E, 0x05);
5456 /* nmacchwo - nmacchwo. */
5457 GEN_MAC_HANDLER(nmacchwo, 0x0E, 0x15);
5458 /* nmacchws - nmacchws. */
5459 GEN_MAC_HANDLER(nmacchws, 0x0E, 0x07);
5460 /* nmacchwso - nmacchwso. */
5461 GEN_MAC_HANDLER(nmacchwso, 0x0E, 0x17);
5462 /* nmachhw - nmachhw. */
5463 GEN_MAC_HANDLER(nmachhw, 0x0E, 0x01);
5464 /* nmachhwo - nmachhwo. */
5465 GEN_MAC_HANDLER(nmachhwo, 0x0E, 0x11);
5466 /* nmachhws - nmachhws. */
5467 GEN_MAC_HANDLER(nmachhws, 0x0E, 0x03);
5468 /* nmachhwso - nmachhwso. */
5469 GEN_MAC_HANDLER(nmachhwso, 0x0E, 0x13);
5470 /* nmaclhw - nmaclhw. */
5471 GEN_MAC_HANDLER(nmaclhw, 0x0E, 0x0D);
5472 /* nmaclhwo - nmaclhwo. */
5473 GEN_MAC_HANDLER(nmaclhwo, 0x0E, 0x1D);
5474 /* nmaclhws - nmaclhws. */
5475 GEN_MAC_HANDLER(nmaclhws, 0x0E, 0x0F);
5476 /* nmaclhwso - nmaclhwso. */
5477 GEN_MAC_HANDLER(nmaclhwso, 0x0E, 0x1F);
5479 /* mulchw - mulchw. */
5480 GEN_MAC_HANDLER(mulchw, 0x08, 0x05);
5481 /* mulchwu - mulchwu. */
5482 GEN_MAC_HANDLER(mulchwu, 0x08, 0x04);
5483 /* mulhhw - mulhhw. */
5484 GEN_MAC_HANDLER(mulhhw, 0x08, 0x01);
5485 /* mulhhwu - mulhhwu. */
5486 GEN_MAC_HANDLER(mulhhwu, 0x08, 0x00);
5487 /* mullhw - mullhw. */
5488 GEN_MAC_HANDLER(mullhw, 0x08, 0x0D);
5489 /* mullhwu - mullhwu. */
5490 GEN_MAC_HANDLER(mullhwu, 0x08, 0x0C);
5493 GEN_HANDLER(mfdcr, 0x1F, 0x03, 0x0A, 0x00000001, PPC_DCR)
5495 #if defined(CONFIG_USER_ONLY)
5496 GEN_EXCP_PRIVREG(ctx);
5498 uint32_t dcrn = SPR(ctx->opcode);
5500 if (unlikely(!ctx->supervisor)) {
5501 GEN_EXCP_PRIVREG(ctx);
5504 tcg_gen_movi_tl(cpu_T[0], dcrn);
5506 tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_T[0]);
5511 GEN_HANDLER(mtdcr, 0x1F, 0x03, 0x0E, 0x00000001, PPC_DCR)
5513 #if defined(CONFIG_USER_ONLY)
5514 GEN_EXCP_PRIVREG(ctx);
5516 uint32_t dcrn = SPR(ctx->opcode);
5518 if (unlikely(!ctx->supervisor)) {
5519 GEN_EXCP_PRIVREG(ctx);
5522 tcg_gen_movi_tl(cpu_T[0], dcrn);
5523 tcg_gen_mov_tl(cpu_T[1], cpu_gpr[rS(ctx->opcode)]);
5529 /* XXX: not implemented on 440 ? */
5530 GEN_HANDLER(mfdcrx, 0x1F, 0x03, 0x08, 0x00000000, PPC_DCRX)
5532 #if defined(CONFIG_USER_ONLY)
5533 GEN_EXCP_PRIVREG(ctx);
5535 if (unlikely(!ctx->supervisor)) {
5536 GEN_EXCP_PRIVREG(ctx);
5539 tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rA(ctx->opcode)]);
5541 tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_T[0]);
5542 /* Note: Rc update flag set leads to undefined state of Rc0 */
5547 /* XXX: not implemented on 440 ? */
5548 GEN_HANDLER(mtdcrx, 0x1F, 0x03, 0x0C, 0x00000000, PPC_DCRX)
5550 #if defined(CONFIG_USER_ONLY)
5551 GEN_EXCP_PRIVREG(ctx);
5553 if (unlikely(!ctx->supervisor)) {
5554 GEN_EXCP_PRIVREG(ctx);
5557 tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rA(ctx->opcode)]);
5558 tcg_gen_mov_tl(cpu_T[1], cpu_gpr[rS(ctx->opcode)]);
5560 /* Note: Rc update flag set leads to undefined state of Rc0 */
5564 /* mfdcrux (PPC 460) : user-mode access to DCR */
5565 GEN_HANDLER(mfdcrux, 0x1F, 0x03, 0x09, 0x00000000, PPC_DCRUX)
5567 tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rA(ctx->opcode)]);
5569 tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_T[0]);
5570 /* Note: Rc update flag set leads to undefined state of Rc0 */
5573 /* mtdcrux (PPC 460) : user-mode access to DCR */
5574 GEN_HANDLER(mtdcrux, 0x1F, 0x03, 0x0D, 0x00000000, PPC_DCRUX)
5576 tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rA(ctx->opcode)]);
5577 tcg_gen_mov_tl(cpu_T[1], cpu_gpr[rS(ctx->opcode)]);
5579 /* Note: Rc update flag set leads to undefined state of Rc0 */
5583 GEN_HANDLER(dccci, 0x1F, 0x06, 0x0E, 0x03E00001, PPC_4xx_COMMON)
5585 #if defined(CONFIG_USER_ONLY)
5586 GEN_EXCP_PRIVOPC(ctx);
5588 if (unlikely(!ctx->supervisor)) {
5589 GEN_EXCP_PRIVOPC(ctx);
5592 /* interpreted as no-op */
5597 GEN_HANDLER(dcread, 0x1F, 0x06, 0x0F, 0x00000001, PPC_4xx_COMMON)
5599 #if defined(CONFIG_USER_ONLY)
5600 GEN_EXCP_PRIVOPC(ctx);
5603 if (unlikely(!ctx->supervisor)) {
5604 GEN_EXCP_PRIVOPC(ctx);
5607 EA = tcg_temp_new();
5608 gen_set_access_type(ACCESS_CACHE);
5609 gen_addr_reg_index(EA, ctx);
5610 val = tcg_temp_new();
5611 gen_qemu_ld32u(val, EA, ctx->mem_idx);
5613 tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], EA);
5619 GEN_HANDLER2(icbt_40x, "icbt", 0x1F, 0x06, 0x08, 0x03E00001, PPC_40x_ICBT)
5621 /* interpreted as no-op */
5622 /* XXX: specification say this is treated as a load by the MMU
5623 * but does not generate any exception
5628 GEN_HANDLER(iccci, 0x1F, 0x06, 0x1E, 0x00000001, PPC_4xx_COMMON)
5630 #if defined(CONFIG_USER_ONLY)
5631 GEN_EXCP_PRIVOPC(ctx);
5633 if (unlikely(!ctx->supervisor)) {
5634 GEN_EXCP_PRIVOPC(ctx);
5637 /* interpreted as no-op */
5642 GEN_HANDLER(icread, 0x1F, 0x06, 0x1F, 0x03E00001, PPC_4xx_COMMON)
5644 #if defined(CONFIG_USER_ONLY)
5645 GEN_EXCP_PRIVOPC(ctx);
5647 if (unlikely(!ctx->supervisor)) {
5648 GEN_EXCP_PRIVOPC(ctx);
5651 /* interpreted as no-op */
5655 /* rfci (supervisor only) */
5656 GEN_HANDLER2(rfci_40x, "rfci", 0x13, 0x13, 0x01, 0x03FF8001, PPC_40x_EXCP)
5658 #if defined(CONFIG_USER_ONLY)
5659 GEN_EXCP_PRIVOPC(ctx);
5661 if (unlikely(!ctx->supervisor)) {
5662 GEN_EXCP_PRIVOPC(ctx);
5665 /* Restore CPU state */
5671 GEN_HANDLER(rfci, 0x13, 0x13, 0x01, 0x03FF8001, PPC_BOOKE)
5673 #if defined(CONFIG_USER_ONLY)
5674 GEN_EXCP_PRIVOPC(ctx);
5676 if (unlikely(!ctx->supervisor)) {
5677 GEN_EXCP_PRIVOPC(ctx);
5680 /* Restore CPU state */
5686 /* BookE specific */
5687 /* XXX: not implemented on 440 ? */
5688 GEN_HANDLER(rfdi, 0x13, 0x07, 0x01, 0x03FF8001, PPC_RFDI)
5690 #if defined(CONFIG_USER_ONLY)
5691 GEN_EXCP_PRIVOPC(ctx);
5693 if (unlikely(!ctx->supervisor)) {
5694 GEN_EXCP_PRIVOPC(ctx);
5697 /* Restore CPU state */
5703 /* XXX: not implemented on 440 ? */
5704 GEN_HANDLER(rfmci, 0x13, 0x06, 0x01, 0x03FF8001, PPC_RFMCI)
5706 #if defined(CONFIG_USER_ONLY)
5707 GEN_EXCP_PRIVOPC(ctx);
5709 if (unlikely(!ctx->supervisor)) {
5710 GEN_EXCP_PRIVOPC(ctx);
5713 /* Restore CPU state */
5719 /* TLB management - PowerPC 405 implementation */
5721 GEN_HANDLER2(tlbre_40x, "tlbre", 0x1F, 0x12, 0x1D, 0x00000001, PPC_40x_TLB)
5723 #if defined(CONFIG_USER_ONLY)
5724 GEN_EXCP_PRIVOPC(ctx);
5726 if (unlikely(!ctx->supervisor)) {
5727 GEN_EXCP_PRIVOPC(ctx);
5730 switch (rB(ctx->opcode)) {
5732 tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rA(ctx->opcode)]);
5733 gen_op_4xx_tlbre_hi();
5734 tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_T[0]);
5737 tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rA(ctx->opcode)]);
5738 gen_op_4xx_tlbre_lo();
5739 tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_T[0]);
5742 GEN_EXCP_INVAL(ctx);
5748 /* tlbsx - tlbsx. */
5749 GEN_HANDLER2(tlbsx_40x, "tlbsx", 0x1F, 0x12, 0x1C, 0x00000000, PPC_40x_TLB)
5751 #if defined(CONFIG_USER_ONLY)
5752 GEN_EXCP_PRIVOPC(ctx);
5754 if (unlikely(!ctx->supervisor)) {
5755 GEN_EXCP_PRIVOPC(ctx);
5758 gen_addr_reg_index(cpu_T[0], ctx);
5760 if (Rc(ctx->opcode))
5761 gen_op_4xx_tlbsx_check();
5762 tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_T[0]);
5767 GEN_HANDLER2(tlbwe_40x, "tlbwe", 0x1F, 0x12, 0x1E, 0x00000001, PPC_40x_TLB)
5769 #if defined(CONFIG_USER_ONLY)
5770 GEN_EXCP_PRIVOPC(ctx);
5772 if (unlikely(!ctx->supervisor)) {
5773 GEN_EXCP_PRIVOPC(ctx);
5776 switch (rB(ctx->opcode)) {
5778 tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rA(ctx->opcode)]);
5779 tcg_gen_mov_tl(cpu_T[1], cpu_gpr[rS(ctx->opcode)]);
5780 gen_op_4xx_tlbwe_hi();
5783 tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rA(ctx->opcode)]);
5784 tcg_gen_mov_tl(cpu_T[1], cpu_gpr[rS(ctx->opcode)]);
5785 gen_op_4xx_tlbwe_lo();
5788 GEN_EXCP_INVAL(ctx);
5794 /* TLB management - PowerPC 440 implementation */
5796 GEN_HANDLER2(tlbre_440, "tlbre", 0x1F, 0x12, 0x1D, 0x00000001, PPC_BOOKE)
5798 #if defined(CONFIG_USER_ONLY)
5799 GEN_EXCP_PRIVOPC(ctx);
5801 if (unlikely(!ctx->supervisor)) {
5802 GEN_EXCP_PRIVOPC(ctx);
5805 switch (rB(ctx->opcode)) {
5809 tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rA(ctx->opcode)]);
5810 gen_op_440_tlbre(rB(ctx->opcode));
5811 tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_T[0]);
5814 GEN_EXCP_INVAL(ctx);
5820 /* tlbsx - tlbsx. */
5821 GEN_HANDLER2(tlbsx_440, "tlbsx", 0x1F, 0x12, 0x1C, 0x00000000, PPC_BOOKE)
5823 #if defined(CONFIG_USER_ONLY)
5824 GEN_EXCP_PRIVOPC(ctx);
5826 if (unlikely(!ctx->supervisor)) {
5827 GEN_EXCP_PRIVOPC(ctx);
5830 gen_addr_reg_index(cpu_T[0], ctx);
5832 if (Rc(ctx->opcode))
5833 gen_op_4xx_tlbsx_check();
5834 tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_T[0]);
5839 GEN_HANDLER2(tlbwe_440, "tlbwe", 0x1F, 0x12, 0x1E, 0x00000001, PPC_BOOKE)
5841 #if defined(CONFIG_USER_ONLY)
5842 GEN_EXCP_PRIVOPC(ctx);
5844 if (unlikely(!ctx->supervisor)) {
5845 GEN_EXCP_PRIVOPC(ctx);
5848 switch (rB(ctx->opcode)) {
5852 tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rA(ctx->opcode)]);
5853 tcg_gen_mov_tl(cpu_T[1], cpu_gpr[rS(ctx->opcode)]);
5854 gen_op_440_tlbwe(rB(ctx->opcode));
5857 GEN_EXCP_INVAL(ctx);
5864 GEN_HANDLER(wrtee, 0x1F, 0x03, 0x04, 0x000FFC01, PPC_WRTEE)
5866 #if defined(CONFIG_USER_ONLY)
5867 GEN_EXCP_PRIVOPC(ctx);
5869 if (unlikely(!ctx->supervisor)) {
5870 GEN_EXCP_PRIVOPC(ctx);
5873 tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rD(ctx->opcode)]);
5875 /* Stop translation to have a chance to raise an exception
5876 * if we just set msr_ee to 1
5883 GEN_HANDLER(wrteei, 0x1F, 0x03, 0x05, 0x000EFC01, PPC_WRTEE)
5885 #if defined(CONFIG_USER_ONLY)
5886 GEN_EXCP_PRIVOPC(ctx);
5888 if (unlikely(!ctx->supervisor)) {
5889 GEN_EXCP_PRIVOPC(ctx);
5892 tcg_gen_movi_tl(cpu_T[0], ctx->opcode & 0x00010000);
5894 /* Stop translation to have a chance to raise an exception
5895 * if we just set msr_ee to 1
5901 /* PowerPC 440 specific instructions */
5903 GEN_HANDLER(dlmzb, 0x1F, 0x0E, 0x02, 0x00000000, PPC_440_SPEC)
5905 tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rS(ctx->opcode)]);
5906 tcg_gen_mov_tl(cpu_T[1], cpu_gpr[rB(ctx->opcode)]);
5908 tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], cpu_T[0]);
5909 tcg_gen_andi_tl(cpu_xer, cpu_xer, ~0x7F);
5910 tcg_gen_or_tl(cpu_xer, cpu_xer, cpu_T[0]);
5911 if (Rc(ctx->opcode)) {
5912 gen_op_440_dlmzb_update_Rc();
5913 tcg_gen_trunc_tl_i32(cpu_crf[0], cpu_T[0]);
5914 tcg_gen_andi_i32(cpu_crf[0], cpu_crf[0], 0xf);
5918 /* mbar replaces eieio on 440 */
5919 GEN_HANDLER(mbar, 0x1F, 0x16, 0x13, 0x001FF801, PPC_BOOKE)
5921 /* interpreted as no-op */
5924 /* msync replaces sync on 440 */
5925 GEN_HANDLER(msync, 0x1F, 0x16, 0x12, 0x03FFF801, PPC_BOOKE)
5927 /* interpreted as no-op */
5931 GEN_HANDLER2(icbt_440, "icbt", 0x1F, 0x16, 0x00, 0x03E00001, PPC_BOOKE)
5933 /* interpreted as no-op */
5934 /* XXX: specification say this is treated as a load by the MMU
5935 * but does not generate any exception
5939 /*** Altivec vector extension ***/
5940 /* Altivec registers moves */
5942 #define GEN_VR_LDX(name, opc2, opc3) \
5943 GEN_HANDLER(name, 0x1F, opc2, opc3, 0x00000001, PPC_ALTIVEC) \
5946 if (unlikely(!ctx->altivec_enabled)) { \
5947 GEN_EXCP_NO_VR(ctx); \
5950 EA = tcg_temp_new(); \
5951 gen_addr_reg_index(EA, ctx); \
5952 tcg_gen_andi_tl(EA, EA, ~0xf); \
5953 if (ctx->mem_idx & 1) { \
5954 gen_qemu_ld64(cpu_avrl[rD(ctx->opcode)], EA, ctx->mem_idx); \
5955 tcg_gen_addi_tl(EA, EA, 8); \
5956 gen_qemu_ld64(cpu_avrh[rD(ctx->opcode)], EA, ctx->mem_idx); \
5958 gen_qemu_ld64(cpu_avrh[rD(ctx->opcode)], EA, ctx->mem_idx); \
5959 tcg_gen_addi_tl(EA, EA, 8); \
5960 gen_qemu_ld64(cpu_avrl[rD(ctx->opcode)], EA, ctx->mem_idx); \
5962 tcg_temp_free(EA); \
5965 #define GEN_VR_STX(name, opc2, opc3) \
5966 GEN_HANDLER(st##name, 0x1F, opc2, opc3, 0x00000001, PPC_ALTIVEC) \
5969 if (unlikely(!ctx->altivec_enabled)) { \
5970 GEN_EXCP_NO_VR(ctx); \
5973 EA = tcg_temp_new(); \
5974 gen_addr_reg_index(EA, ctx); \
5975 tcg_gen_andi_tl(EA, EA, ~0xf); \
5976 if (ctx->mem_idx & 1) { \
5977 gen_qemu_st64(cpu_avrl[rD(ctx->opcode)], EA, ctx->mem_idx); \
5978 tcg_gen_addi_tl(EA, EA, 8); \
5979 gen_qemu_st64(cpu_avrh[rD(ctx->opcode)], EA, ctx->mem_idx); \
5981 gen_qemu_st64(cpu_avrh[rD(ctx->opcode)], EA, ctx->mem_idx); \
5982 tcg_gen_addi_tl(EA, EA, 8); \
5983 gen_qemu_st64(cpu_avrl[rD(ctx->opcode)], EA, ctx->mem_idx); \
5985 tcg_temp_free(EA); \
5988 GEN_VR_LDX(lvx, 0x07, 0x03);
5989 /* As we don't emulate the cache, lvxl is stricly equivalent to lvx */
5990 GEN_VR_LDX(lvxl, 0x07, 0x0B);
5992 GEN_VR_STX(svx, 0x07, 0x07);
5993 /* As we don't emulate the cache, stvxl is stricly equivalent to stvx */
5994 GEN_VR_STX(svxl, 0x07, 0x0F);
5996 /*** SPE extension ***/
5997 /* Register moves */
5999 static always_inline void gen_load_gpr64(TCGv_i64 t, int reg) {
6000 #if defined(TARGET_PPC64)
6001 tcg_gen_mov_i64(t, cpu_gpr[reg]);
6003 tcg_gen_concat_i32_i64(t, cpu_gpr[reg], cpu_gprh[reg]);
6007 static always_inline void gen_store_gpr64(int reg, TCGv_i64 t) {
6008 #if defined(TARGET_PPC64)
6009 tcg_gen_mov_i64(cpu_gpr[reg], t);
6011 TCGv_i64 tmp = tcg_temp_new_i64();
6012 tcg_gen_trunc_i64_i32(cpu_gpr[reg], t);
6013 tcg_gen_shri_i64(tmp, t, 32);
6014 tcg_gen_trunc_i64_i32(cpu_gprh[reg], tmp);
6015 tcg_temp_free_i64(tmp);
6019 #define GEN_SPE(name0, name1, opc2, opc3, inval, type) \
6020 GEN_HANDLER(name0##_##name1, 0x04, opc2, opc3, inval, type) \
6022 if (Rc(ctx->opcode)) \
6028 /* Handler for undefined SPE opcodes */
6029 static always_inline void gen_speundef (DisasContext *ctx)
6031 GEN_EXCP_INVAL(ctx);
6034 /* SPE load and stores */
6035 static always_inline void gen_addr_spe_imm_index (TCGv EA, DisasContext *ctx, int sh)
6037 target_long simm = rB(ctx->opcode);
6039 if (rA(ctx->opcode) == 0)
6040 tcg_gen_movi_tl(EA, simm << sh);
6041 else if (likely(simm != 0))
6042 tcg_gen_addi_tl(EA, cpu_gpr[rA(ctx->opcode)], simm << sh);
6044 tcg_gen_mov_tl(EA, cpu_gpr[rA(ctx->opcode)]);
6047 #define op_spe_ldst(name) (*gen_op_##name[ctx->mem_idx])()
6048 #define OP_SPE_LD_TABLE(name) \
6049 static GenOpFunc *gen_op_spe_l##name[NB_MEM_FUNCS] = { \
6050 GEN_MEM_FUNCS(spe_l##name), \
6052 #define OP_SPE_ST_TABLE(name) \
6053 static GenOpFunc *gen_op_spe_st##name[NB_MEM_FUNCS] = { \
6054 GEN_MEM_FUNCS(spe_st##name), \
6057 #define GEN_SPE_LD(name, sh) \
6058 static always_inline void gen_evl##name (DisasContext *ctx) \
6060 if (unlikely(!ctx->spe_enabled)) { \
6061 GEN_EXCP_NO_AP(ctx); \
6064 gen_addr_spe_imm_index(cpu_T[0], ctx, sh); \
6065 op_spe_ldst(spe_l##name); \
6066 gen_store_gpr64(rD(ctx->opcode), cpu_T64[1]); \
6069 #define GEN_SPE_LDX(name) \
6070 static always_inline void gen_evl##name##x (DisasContext *ctx) \
6072 if (unlikely(!ctx->spe_enabled)) { \
6073 GEN_EXCP_NO_AP(ctx); \
6076 gen_addr_reg_index(cpu_T[0], ctx); \
6077 op_spe_ldst(spe_l##name); \
6078 gen_store_gpr64(rD(ctx->opcode), cpu_T64[1]); \
6081 #define GEN_SPEOP_LD(name, sh) \
6082 OP_SPE_LD_TABLE(name); \
6083 GEN_SPE_LD(name, sh); \
6086 #define GEN_SPE_ST(name, sh) \
6087 static always_inline void gen_evst##name (DisasContext *ctx) \
6089 if (unlikely(!ctx->spe_enabled)) { \
6090 GEN_EXCP_NO_AP(ctx); \
6093 gen_addr_spe_imm_index(cpu_T[0], ctx, sh); \
6094 gen_load_gpr64(cpu_T64[1], rS(ctx->opcode)); \
6095 op_spe_ldst(spe_st##name); \
6098 #define GEN_SPE_STX(name) \
6099 static always_inline void gen_evst##name##x (DisasContext *ctx) \
6101 if (unlikely(!ctx->spe_enabled)) { \
6102 GEN_EXCP_NO_AP(ctx); \
6105 gen_addr_reg_index(cpu_T[0], ctx); \
6106 gen_load_gpr64(cpu_T64[1], rS(ctx->opcode)); \
6107 op_spe_ldst(spe_st##name); \
6110 #define GEN_SPEOP_ST(name, sh) \
6111 OP_SPE_ST_TABLE(name); \
6112 GEN_SPE_ST(name, sh); \
6115 #define GEN_SPEOP_LDST(name, sh) \
6116 GEN_SPEOP_LD(name, sh); \
6117 GEN_SPEOP_ST(name, sh)
6120 #if defined(TARGET_PPC64)
6121 #define GEN_SPEOP_LOGIC2(name, tcg_op) \
6122 static always_inline void gen_##name (DisasContext *ctx) \
6124 if (unlikely(!ctx->spe_enabled)) { \
6125 GEN_EXCP_NO_AP(ctx); \
6128 tcg_op(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)], \
6129 cpu_gpr[rB(ctx->opcode)]); \
6132 #define GEN_SPEOP_LOGIC2(name, tcg_op) \
6133 static always_inline void gen_##name (DisasContext *ctx) \
6135 if (unlikely(!ctx->spe_enabled)) { \
6136 GEN_EXCP_NO_AP(ctx); \
6139 tcg_op(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)], \
6140 cpu_gpr[rB(ctx->opcode)]); \
6141 tcg_op(cpu_gprh[rD(ctx->opcode)], cpu_gprh[rA(ctx->opcode)], \
6142 cpu_gprh[rB(ctx->opcode)]); \
6146 GEN_SPEOP_LOGIC2(evand, tcg_gen_and_tl);
6147 GEN_SPEOP_LOGIC2(evandc, tcg_gen_andc_tl);
6148 GEN_SPEOP_LOGIC2(evxor, tcg_gen_xor_tl);
6149 GEN_SPEOP_LOGIC2(evor, tcg_gen_or_tl);
6150 GEN_SPEOP_LOGIC2(evnor, tcg_gen_nor_tl);
6151 GEN_SPEOP_LOGIC2(eveqv, tcg_gen_eqv_tl);
6152 GEN_SPEOP_LOGIC2(evorc, tcg_gen_orc_tl);
6153 GEN_SPEOP_LOGIC2(evnand, tcg_gen_nand_tl);
6155 /* SPE logic immediate */
6156 #if defined(TARGET_PPC64)
6157 #define GEN_SPEOP_TCG_LOGIC_IMM2(name, tcg_opi) \
6158 static always_inline void gen_##name (DisasContext *ctx) \
6160 if (unlikely(!ctx->spe_enabled)) { \
6161 GEN_EXCP_NO_AP(ctx); \
6164 TCGv_i32 t0 = tcg_temp_local_new_i32(); \
6165 TCGv_i32 t1 = tcg_temp_local_new_i32(); \
6166 TCGv_i64 t2 = tcg_temp_local_new_i64(); \
6167 tcg_gen_trunc_i64_i32(t0, cpu_gpr[rA(ctx->opcode)]); \
6168 tcg_opi(t0, t0, rB(ctx->opcode)); \
6169 tcg_gen_shri_i64(t2, cpu_gpr[rA(ctx->opcode)], 32); \
6170 tcg_gen_trunc_i64_i32(t1, t2); \
6171 tcg_temp_free_i64(t2); \
6172 tcg_opi(t1, t1, rB(ctx->opcode)); \
6173 tcg_gen_concat_i32_i64(cpu_gpr[rD(ctx->opcode)], t0, t1); \
6174 tcg_temp_free_i32(t0); \
6175 tcg_temp_free_i32(t1); \
6178 #define GEN_SPEOP_TCG_LOGIC_IMM2(name, tcg_opi) \
6179 static always_inline void gen_##name (DisasContext *ctx) \
6181 if (unlikely(!ctx->spe_enabled)) { \
6182 GEN_EXCP_NO_AP(ctx); \
6185 tcg_opi(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)], \
6187 tcg_opi(cpu_gprh[rD(ctx->opcode)], cpu_gprh[rA(ctx->opcode)], \
6191 GEN_SPEOP_TCG_LOGIC_IMM2(evslwi, tcg_gen_shli_i32);
6192 GEN_SPEOP_TCG_LOGIC_IMM2(evsrwiu, tcg_gen_shri_i32);
6193 GEN_SPEOP_TCG_LOGIC_IMM2(evsrwis, tcg_gen_sari_i32);
6194 GEN_SPEOP_TCG_LOGIC_IMM2(evrlwi, tcg_gen_rotli_i32);
6196 /* SPE arithmetic */
6197 #if defined(TARGET_PPC64)
6198 #define GEN_SPEOP_ARITH1(name, tcg_op) \
6199 static always_inline void gen_##name (DisasContext *ctx) \
6201 if (unlikely(!ctx->spe_enabled)) { \
6202 GEN_EXCP_NO_AP(ctx); \
6205 TCGv_i32 t0 = tcg_temp_local_new_i32(); \
6206 TCGv_i32 t1 = tcg_temp_local_new_i32(); \
6207 TCGv_i64 t2 = tcg_temp_local_new_i64(); \
6208 tcg_gen_trunc_i64_i32(t0, cpu_gpr[rA(ctx->opcode)]); \
6210 tcg_gen_shri_i64(t2, cpu_gpr[rA(ctx->opcode)], 32); \
6211 tcg_gen_trunc_i64_i32(t1, t2); \
6212 tcg_temp_free_i64(t2); \
6214 tcg_gen_concat_i32_i64(cpu_gpr[rD(ctx->opcode)], t0, t1); \
6215 tcg_temp_free_i32(t0); \
6216 tcg_temp_free_i32(t1); \
6219 #define GEN_SPEOP_ARITH1(name, tcg_op) \
6220 static always_inline void gen_##name (DisasContext *ctx) \
6222 if (unlikely(!ctx->spe_enabled)) { \
6223 GEN_EXCP_NO_AP(ctx); \
6226 tcg_op(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)]); \
6227 tcg_op(cpu_gprh[rD(ctx->opcode)], cpu_gprh[rA(ctx->opcode)]); \
6231 static always_inline void gen_op_evabs (TCGv_i32 ret, TCGv_i32 arg1)
6233 int l1 = gen_new_label();
6234 int l2 = gen_new_label();
6236 tcg_gen_brcondi_i32(TCG_COND_GE, arg1, 0, l1);
6237 tcg_gen_neg_i32(ret, arg1);
6240 tcg_gen_mov_i32(ret, arg1);
6243 GEN_SPEOP_ARITH1(evabs, gen_op_evabs);
6244 GEN_SPEOP_ARITH1(evneg, tcg_gen_neg_i32);
6245 GEN_SPEOP_ARITH1(evextsb, tcg_gen_ext8s_i32);
6246 GEN_SPEOP_ARITH1(evextsh, tcg_gen_ext16s_i32);
6247 static always_inline void gen_op_evrndw (TCGv_i32 ret, TCGv_i32 arg1)
6249 tcg_gen_addi_i32(ret, arg1, 0x8000);
6250 tcg_gen_ext16u_i32(ret, ret);
6252 GEN_SPEOP_ARITH1(evrndw, gen_op_evrndw);
6253 GEN_SPEOP_ARITH1(evcntlsw, gen_helper_cntlsw32);
6254 GEN_SPEOP_ARITH1(evcntlzw, gen_helper_cntlzw32);
6256 #if defined(TARGET_PPC64)
6257 #define GEN_SPEOP_ARITH2(name, tcg_op) \
6258 static always_inline void gen_##name (DisasContext *ctx) \
6260 if (unlikely(!ctx->spe_enabled)) { \
6261 GEN_EXCP_NO_AP(ctx); \
6264 TCGv_i32 t0 = tcg_temp_local_new_i32(); \
6265 TCGv_i32 t1 = tcg_temp_local_new_i32(); \
6266 TCGv_i32 t2 = tcg_temp_local_new_i32(); \
6267 TCGv_i64 t3 = tcg_temp_local_new(TCG_TYPE_I64); \
6268 tcg_gen_trunc_i64_i32(t0, cpu_gpr[rA(ctx->opcode)]); \
6269 tcg_gen_trunc_i64_i32(t2, cpu_gpr[rB(ctx->opcode)]); \
6270 tcg_op(t0, t0, t2); \
6271 tcg_gen_shri_i64(t3, cpu_gpr[rA(ctx->opcode)], 32); \
6272 tcg_gen_trunc_i64_i32(t1, t3); \
6273 tcg_gen_shri_i64(t3, cpu_gpr[rB(ctx->opcode)], 32); \
6274 tcg_gen_trunc_i64_i32(t2, t3); \
6275 tcg_temp_free_i64(t3); \
6276 tcg_op(t1, t1, t2); \
6277 tcg_temp_free_i32(t2); \
6278 tcg_gen_concat_i32_i64(cpu_gpr[rD(ctx->opcode)], t0, t1); \
6279 tcg_temp_free_i32(t0); \
6280 tcg_temp_free_i32(t1); \
6283 #define GEN_SPEOP_ARITH2(name, tcg_op) \
6284 static always_inline void gen_##name (DisasContext *ctx) \
6286 if (unlikely(!ctx->spe_enabled)) { \
6287 GEN_EXCP_NO_AP(ctx); \
6290 tcg_op(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)], \
6291 cpu_gpr[rB(ctx->opcode)]); \
6292 tcg_op(cpu_gprh[rD(ctx->opcode)], cpu_gprh[rA(ctx->opcode)], \
6293 cpu_gprh[rB(ctx->opcode)]); \
6297 static always_inline void gen_op_evsrwu (TCGv_i32 ret, TCGv_i32 arg1, TCGv_i32 arg2)
6302 l1 = gen_new_label();
6303 l2 = gen_new_label();
6304 t0 = tcg_temp_local_new_i32();
6305 /* No error here: 6 bits are used */
6306 tcg_gen_andi_i32(t0, arg2, 0x3F);
6307 tcg_gen_brcondi_i32(TCG_COND_GE, t0, 32, l1);
6308 tcg_gen_shr_i32(ret, arg1, t0);
6311 tcg_gen_movi_i32(ret, 0);
6313 tcg_temp_free_i32(t0);
6315 GEN_SPEOP_ARITH2(evsrwu, gen_op_evsrwu);
6316 static always_inline void gen_op_evsrws (TCGv_i32 ret, TCGv_i32 arg1, TCGv_i32 arg2)
6321 l1 = gen_new_label();
6322 l2 = gen_new_label();
6323 t0 = tcg_temp_local_new_i32();
6324 /* No error here: 6 bits are used */
6325 tcg_gen_andi_i32(t0, arg2, 0x3F);
6326 tcg_gen_brcondi_i32(TCG_COND_GE, t0, 32, l1);
6327 tcg_gen_sar_i32(ret, arg1, t0);
6330 tcg_gen_movi_i32(ret, 0);
6332 tcg_temp_free_i32(t0);
6334 GEN_SPEOP_ARITH2(evsrws, gen_op_evsrws);
6335 static always_inline void gen_op_evslw (TCGv_i32 ret, TCGv_i32 arg1, TCGv_i32 arg2)
6340 l1 = gen_new_label();
6341 l2 = gen_new_label();
6342 t0 = tcg_temp_local_new_i32();
6343 /* No error here: 6 bits are used */
6344 tcg_gen_andi_i32(t0, arg2, 0x3F);
6345 tcg_gen_brcondi_i32(TCG_COND_GE, t0, 32, l1);
6346 tcg_gen_shl_i32(ret, arg1, t0);
6349 tcg_gen_movi_i32(ret, 0);
6351 tcg_temp_free_i32(t0);
6353 GEN_SPEOP_ARITH2(evslw, gen_op_evslw);
6354 static always_inline void gen_op_evrlw (TCGv_i32 ret, TCGv_i32 arg1, TCGv_i32 arg2)
6356 TCGv_i32 t0 = tcg_temp_new_i32();
6357 tcg_gen_andi_i32(t0, arg2, 0x1F);
6358 tcg_gen_rotl_i32(ret, arg1, t0);
6359 tcg_temp_free_i32(t0);
6361 GEN_SPEOP_ARITH2(evrlw, gen_op_evrlw);
6362 static always_inline void gen_evmergehi (DisasContext *ctx)
6364 if (unlikely(!ctx->spe_enabled)) {
6365 GEN_EXCP_NO_AP(ctx);
6368 #if defined(TARGET_PPC64)
6369 TCGv t0 = tcg_temp_new();
6370 TCGv t1 = tcg_temp_new();
6371 tcg_gen_shri_tl(t0, cpu_gpr[rB(ctx->opcode)], 32);
6372 tcg_gen_andi_tl(t1, cpu_gpr[rA(ctx->opcode)], 0xFFFFFFFF0000000ULL);
6373 tcg_gen_or_tl(cpu_gpr[rD(ctx->opcode)], t0, t1);
6377 tcg_gen_mov_i32(cpu_gpr[rD(ctx->opcode)], cpu_gprh[rB(ctx->opcode)]);
6378 tcg_gen_mov_i32(cpu_gprh[rD(ctx->opcode)], cpu_gprh[rA(ctx->opcode)]);
6381 GEN_SPEOP_ARITH2(evaddw, tcg_gen_add_i32);
6382 static always_inline void gen_op_evsubf (TCGv_i32 ret, TCGv_i32 arg1, TCGv_i32 arg2)
6384 tcg_gen_sub_i32(ret, arg2, arg1);
6386 GEN_SPEOP_ARITH2(evsubfw, gen_op_evsubf);
6388 /* SPE arithmetic immediate */
6389 #if defined(TARGET_PPC64)
6390 #define GEN_SPEOP_ARITH_IMM2(name, tcg_op) \
6391 static always_inline void gen_##name (DisasContext *ctx) \
6393 if (unlikely(!ctx->spe_enabled)) { \
6394 GEN_EXCP_NO_AP(ctx); \
6397 TCGv_i32 t0 = tcg_temp_local_new_i32(); \
6398 TCGv_i32 t1 = tcg_temp_local_new_i32(); \
6399 TCGv_i64 t2 = tcg_temp_local_new_i64(); \
6400 tcg_gen_trunc_i64_i32(t0, cpu_gpr[rB(ctx->opcode)]); \
6401 tcg_op(t0, t0, rA(ctx->opcode)); \
6402 tcg_gen_shri_i64(t2, cpu_gpr[rB(ctx->opcode)], 32); \
6403 tcg_gen_trunc_i64_i32(t1, t2); \
6404 tcg_temp_free_i64(t2); \
6405 tcg_op(t1, t1, rA(ctx->opcode)); \
6406 tcg_gen_concat_i32_i64(cpu_gpr[rD(ctx->opcode)], t0, t1); \
6407 tcg_temp_free_i32(t0); \
6408 tcg_temp_free_i32(t1); \
6411 #define GEN_SPEOP_ARITH_IMM2(name, tcg_op) \
6412 static always_inline void gen_##name (DisasContext *ctx) \
6414 if (unlikely(!ctx->spe_enabled)) { \
6415 GEN_EXCP_NO_AP(ctx); \
6418 tcg_op(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rB(ctx->opcode)], \
6420 tcg_op(cpu_gprh[rD(ctx->opcode)], cpu_gprh[rB(ctx->opcode)], \
6424 GEN_SPEOP_ARITH_IMM2(evaddiw, tcg_gen_addi_i32);
6425 GEN_SPEOP_ARITH_IMM2(evsubifw, tcg_gen_subi_i32);
6427 /* SPE comparison */
6428 #if defined(TARGET_PPC64)
6429 #define GEN_SPEOP_COMP(name, tcg_cond) \
6430 static always_inline void gen_##name (DisasContext *ctx) \
6432 if (unlikely(!ctx->spe_enabled)) { \
6433 GEN_EXCP_NO_AP(ctx); \
6436 int l1 = gen_new_label(); \
6437 int l2 = gen_new_label(); \
6438 int l3 = gen_new_label(); \
6439 int l4 = gen_new_label(); \
6440 TCGv_i32 t0 = tcg_temp_local_new_i32(); \
6441 TCGv_i32 t1 = tcg_temp_local_new_i32(); \
6442 TCGv_i64 t2 = tcg_temp_local_new_i64(); \
6443 tcg_gen_trunc_i64_i32(t0, cpu_gpr[rA(ctx->opcode)]); \
6444 tcg_gen_trunc_i64_i32(t1, cpu_gpr[rB(ctx->opcode)]); \
6445 tcg_gen_brcond_i32(tcg_cond, t0, t1, l1); \
6446 tcg_gen_movi_i32(cpu_crf[crfD(ctx->opcode)], 0); \
6448 gen_set_label(l1); \
6449 tcg_gen_movi_i32(cpu_crf[crfD(ctx->opcode)], \
6450 CRF_CL | CRF_CH_OR_CL | CRF_CH_AND_CL); \
6451 gen_set_label(l2); \
6452 tcg_gen_shri_i64(t2, cpu_gpr[rA(ctx->opcode)], 32); \
6453 tcg_gen_trunc_i64_i32(t0, t2); \
6454 tcg_gen_shri_i64(t2, cpu_gpr[rB(ctx->opcode)], 32); \
6455 tcg_gen_trunc_i64_i32(t1, t2); \
6456 tcg_temp_free_i64(t2); \
6457 tcg_gen_brcond_i32(tcg_cond, t0, t1, l3); \
6458 tcg_gen_andi_i32(cpu_crf[crfD(ctx->opcode)], cpu_crf[crfD(ctx->opcode)], \
6459 ~(CRF_CH | CRF_CH_AND_CL)); \
6461 gen_set_label(l3); \
6462 tcg_gen_ori_i32(cpu_crf[crfD(ctx->opcode)], cpu_crf[crfD(ctx->opcode)], \
6463 CRF_CH | CRF_CH_OR_CL); \
6464 gen_set_label(l4); \
6465 tcg_temp_free_i32(t0); \
6466 tcg_temp_free_i32(t1); \
6469 #define GEN_SPEOP_COMP(name, tcg_cond) \
6470 static always_inline void gen_##name (DisasContext *ctx) \
6472 if (unlikely(!ctx->spe_enabled)) { \
6473 GEN_EXCP_NO_AP(ctx); \
6476 int l1 = gen_new_label(); \
6477 int l2 = gen_new_label(); \
6478 int l3 = gen_new_label(); \
6479 int l4 = gen_new_label(); \
6481 tcg_gen_brcond_i32(tcg_cond, cpu_gpr[rA(ctx->opcode)], \
6482 cpu_gpr[rB(ctx->opcode)], l1); \
6483 tcg_gen_movi_tl(cpu_crf[crfD(ctx->opcode)], 0); \
6485 gen_set_label(l1); \
6486 tcg_gen_movi_i32(cpu_crf[crfD(ctx->opcode)], \
6487 CRF_CL | CRF_CH_OR_CL | CRF_CH_AND_CL); \
6488 gen_set_label(l2); \
6489 tcg_gen_brcond_i32(tcg_cond, cpu_gprh[rA(ctx->opcode)], \
6490 cpu_gprh[rB(ctx->opcode)], l3); \
6491 tcg_gen_andi_i32(cpu_crf[crfD(ctx->opcode)], cpu_crf[crfD(ctx->opcode)], \
6492 ~(CRF_CH | CRF_CH_AND_CL)); \
6494 gen_set_label(l3); \
6495 tcg_gen_ori_i32(cpu_crf[crfD(ctx->opcode)], cpu_crf[crfD(ctx->opcode)], \
6496 CRF_CH | CRF_CH_OR_CL); \
6497 gen_set_label(l4); \
6500 GEN_SPEOP_COMP(evcmpgtu, TCG_COND_GTU);
6501 GEN_SPEOP_COMP(evcmpgts, TCG_COND_GT);
6502 GEN_SPEOP_COMP(evcmpltu, TCG_COND_LTU);
6503 GEN_SPEOP_COMP(evcmplts, TCG_COND_LT);
6504 GEN_SPEOP_COMP(evcmpeq, TCG_COND_EQ);
6507 static always_inline void gen_brinc (DisasContext *ctx)
6509 /* Note: brinc is usable even if SPE is disabled */
6510 gen_helper_brinc(cpu_gpr[rD(ctx->opcode)],
6511 cpu_gpr[rA(ctx->opcode)], cpu_gpr[rB(ctx->opcode)]);
6513 static always_inline void gen_evmergelo (DisasContext *ctx)
6515 if (unlikely(!ctx->spe_enabled)) {
6516 GEN_EXCP_NO_AP(ctx);
6519 #if defined(TARGET_PPC64)
6520 TCGv t0 = tcg_temp_new();
6521 TCGv t1 = tcg_temp_new();
6522 tcg_gen_andi_tl(t0, cpu_gpr[rB(ctx->opcode)], 0x00000000FFFFFFFFLL);
6523 tcg_gen_shli_tl(t1, cpu_gpr[rA(ctx->opcode)], 32);
6524 tcg_gen_or_tl(cpu_gpr[rD(ctx->opcode)], t0, t1);
6528 tcg_gen_mov_i32(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rB(ctx->opcode)]);
6529 tcg_gen_mov_i32(cpu_gprh[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)]);
6532 static always_inline void gen_evmergehilo (DisasContext *ctx)
6534 if (unlikely(!ctx->spe_enabled)) {
6535 GEN_EXCP_NO_AP(ctx);
6538 #if defined(TARGET_PPC64)
6539 TCGv t0 = tcg_temp_new();
6540 TCGv t1 = tcg_temp_new();
6541 tcg_gen_andi_tl(t0, cpu_gpr[rB(ctx->opcode)], 0x00000000FFFFFFFFLL);
6542 tcg_gen_andi_tl(t1, cpu_gpr[rA(ctx->opcode)], 0xFFFFFFFF0000000ULL);
6543 tcg_gen_or_tl(cpu_gpr[rD(ctx->opcode)], t0, t1);
6547 tcg_gen_mov_i32(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rB(ctx->opcode)]);
6548 tcg_gen_mov_i32(cpu_gprh[rD(ctx->opcode)], cpu_gprh[rA(ctx->opcode)]);
6551 static always_inline void gen_evmergelohi (DisasContext *ctx)
6553 if (unlikely(!ctx->spe_enabled)) {
6554 GEN_EXCP_NO_AP(ctx);
6557 #if defined(TARGET_PPC64)
6558 TCGv t0 = tcg_temp_new();
6559 TCGv t1 = tcg_temp_new();
6560 tcg_gen_shri_tl(t0, cpu_gpr[rB(ctx->opcode)], 32);
6561 tcg_gen_shli_tl(t1, cpu_gpr[rA(ctx->opcode)], 32);
6562 tcg_gen_or_tl(cpu_gpr[rD(ctx->opcode)], t0, t1);
6566 tcg_gen_mov_i32(cpu_gpr[rD(ctx->opcode)], cpu_gprh[rB(ctx->opcode)]);
6567 tcg_gen_mov_i32(cpu_gprh[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)]);
6570 static always_inline void gen_evsplati (DisasContext *ctx)
6572 uint64_t imm = ((int32_t)(rA(ctx->opcode) << 11)) >> 27;
6574 #if defined(TARGET_PPC64)
6575 tcg_gen_movi_tl(cpu_gpr[rD(ctx->opcode)], (imm << 32) | imm);
6577 tcg_gen_movi_i32(cpu_gpr[rD(ctx->opcode)], imm);
6578 tcg_gen_movi_i32(cpu_gprh[rD(ctx->opcode)], imm);
6581 static always_inline void gen_evsplatfi (DisasContext *ctx)
6583 uint64_t imm = rA(ctx->opcode) << 11;
6585 #if defined(TARGET_PPC64)
6586 tcg_gen_movi_tl(cpu_gpr[rD(ctx->opcode)], (imm << 32) | imm);
6588 tcg_gen_movi_i32(cpu_gpr[rD(ctx->opcode)], imm);
6589 tcg_gen_movi_i32(cpu_gprh[rD(ctx->opcode)], imm);
6593 static always_inline void gen_evsel (DisasContext *ctx)
6595 int l1 = gen_new_label();
6596 int l2 = gen_new_label();
6597 int l3 = gen_new_label();
6598 int l4 = gen_new_label();
6599 TCGv_i32 t0 = tcg_temp_local_new_i32();
6600 #if defined(TARGET_PPC64)
6601 TCGv t1 = tcg_temp_local_new();
6602 TCGv t2 = tcg_temp_local_new();
6604 tcg_gen_andi_i32(t0, cpu_crf[ctx->opcode & 0x07], 1 << 3);
6605 tcg_gen_brcondi_i32(TCG_COND_EQ, t0, 0, l1);
6606 #if defined(TARGET_PPC64)
6607 tcg_gen_andi_tl(t1, cpu_gpr[rA(ctx->opcode)], 0xFFFFFFFF00000000ULL);
6609 tcg_gen_mov_tl(cpu_gprh[rD(ctx->opcode)], cpu_gprh[rA(ctx->opcode)]);
6613 #if defined(TARGET_PPC64)
6614 tcg_gen_andi_tl(t1, cpu_gpr[rB(ctx->opcode)], 0xFFFFFFFF00000000ULL);
6616 tcg_gen_mov_tl(cpu_gprh[rD(ctx->opcode)], cpu_gprh[rB(ctx->opcode)]);
6619 tcg_gen_andi_i32(t0, cpu_crf[ctx->opcode & 0x07], 1 << 2);
6620 tcg_gen_brcondi_i32(TCG_COND_EQ, t0, 0, l3);
6621 #if defined(TARGET_PPC64)
6622 tcg_gen_andi_tl(t2, cpu_gpr[rA(ctx->opcode)], 0x00000000FFFFFFFFULL);
6624 tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)]);
6628 #if defined(TARGET_PPC64)
6629 tcg_gen_andi_tl(t2, cpu_gpr[rB(ctx->opcode)], 0x00000000FFFFFFFFULL);
6631 tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rB(ctx->opcode)]);
6634 tcg_temp_free_i32(t0);
6635 #if defined(TARGET_PPC64)
6636 tcg_gen_or_tl(cpu_gpr[rD(ctx->opcode)], t1, t2);
6641 GEN_HANDLER2(evsel0, "evsel", 0x04, 0x1c, 0x09, 0x00000000, PPC_SPE)
6645 GEN_HANDLER2(evsel1, "evsel", 0x04, 0x1d, 0x09, 0x00000000, PPC_SPE)
6649 GEN_HANDLER2(evsel2, "evsel", 0x04, 0x1e, 0x09, 0x00000000, PPC_SPE)
6653 GEN_HANDLER2(evsel3, "evsel", 0x04, 0x1f, 0x09, 0x00000000, PPC_SPE)
6658 GEN_SPE(evaddw, speundef, 0x00, 0x08, 0x00000000, PPC_SPE); ////
6659 GEN_SPE(evaddiw, speundef, 0x01, 0x08, 0x00000000, PPC_SPE);
6660 GEN_SPE(evsubfw, speundef, 0x02, 0x08, 0x00000000, PPC_SPE); ////
6661 GEN_SPE(evsubifw, speundef, 0x03, 0x08, 0x00000000, PPC_SPE);
6662 GEN_SPE(evabs, evneg, 0x04, 0x08, 0x0000F800, PPC_SPE); ////
6663 GEN_SPE(evextsb, evextsh, 0x05, 0x08, 0x0000F800, PPC_SPE); ////
6664 GEN_SPE(evrndw, evcntlzw, 0x06, 0x08, 0x0000F800, PPC_SPE); ////
6665 GEN_SPE(evcntlsw, brinc, 0x07, 0x08, 0x00000000, PPC_SPE); //
6666 GEN_SPE(speundef, evand, 0x08, 0x08, 0x00000000, PPC_SPE); ////
6667 GEN_SPE(evandc, speundef, 0x09, 0x08, 0x00000000, PPC_SPE); ////
6668 GEN_SPE(evxor, evor, 0x0B, 0x08, 0x00000000, PPC_SPE); ////
6669 GEN_SPE(evnor, eveqv, 0x0C, 0x08, 0x00000000, PPC_SPE); ////
6670 GEN_SPE(speundef, evorc, 0x0D, 0x08, 0x00000000, PPC_SPE); ////
6671 GEN_SPE(evnand, speundef, 0x0F, 0x08, 0x00000000, PPC_SPE); ////
6672 GEN_SPE(evsrwu, evsrws, 0x10, 0x08, 0x00000000, PPC_SPE); ////
6673 GEN_SPE(evsrwiu, evsrwis, 0x11, 0x08, 0x00000000, PPC_SPE);
6674 GEN_SPE(evslw, speundef, 0x12, 0x08, 0x00000000, PPC_SPE); ////
6675 GEN_SPE(evslwi, speundef, 0x13, 0x08, 0x00000000, PPC_SPE);
6676 GEN_SPE(evrlw, evsplati, 0x14, 0x08, 0x00000000, PPC_SPE); //
6677 GEN_SPE(evrlwi, evsplatfi, 0x15, 0x08, 0x00000000, PPC_SPE);
6678 GEN_SPE(evmergehi, evmergelo, 0x16, 0x08, 0x00000000, PPC_SPE); ////
6679 GEN_SPE(evmergehilo, evmergelohi, 0x17, 0x08, 0x00000000, PPC_SPE); ////
6680 GEN_SPE(evcmpgtu, evcmpgts, 0x18, 0x08, 0x00600000, PPC_SPE); ////
6681 GEN_SPE(evcmpltu, evcmplts, 0x19, 0x08, 0x00600000, PPC_SPE); ////
6682 GEN_SPE(evcmpeq, speundef, 0x1A, 0x08, 0x00600000, PPC_SPE); ////
6684 /* Load and stores */
6685 GEN_SPEOP_LDST(dd, 3);
6686 GEN_SPEOP_LDST(dw, 3);
6687 GEN_SPEOP_LDST(dh, 3);
6688 GEN_SPEOP_LDST(whe, 2);
6689 GEN_SPEOP_LD(whou, 2);
6690 GEN_SPEOP_LD(whos, 2);
6691 GEN_SPEOP_ST(who, 2);
6693 #define _GEN_OP_SPE_STWWE(suffix) \
6694 static always_inline void gen_op_spe_stwwe_##suffix (void) \
6696 gen_op_srli32_T1_64(); \
6697 gen_op_spe_stwwo_##suffix(); \
6699 #define _GEN_OP_SPE_STWWE_LE(suffix) \
6700 static always_inline void gen_op_spe_stwwe_le_##suffix (void) \
6702 gen_op_srli32_T1_64(); \
6703 gen_op_spe_stwwo_le_##suffix(); \
6705 #if defined(TARGET_PPC64)
6706 #define GEN_OP_SPE_STWWE(suffix) \
6707 _GEN_OP_SPE_STWWE(suffix); \
6708 _GEN_OP_SPE_STWWE_LE(suffix); \
6709 static always_inline void gen_op_spe_stwwe_64_##suffix (void) \
6711 gen_op_srli32_T1_64(); \
6712 gen_op_spe_stwwo_64_##suffix(); \
6714 static always_inline void gen_op_spe_stwwe_le_64_##suffix (void) \
6716 gen_op_srli32_T1_64(); \
6717 gen_op_spe_stwwo_le_64_##suffix(); \
6720 #define GEN_OP_SPE_STWWE(suffix) \
6721 _GEN_OP_SPE_STWWE(suffix); \
6722 _GEN_OP_SPE_STWWE_LE(suffix)
6724 #if defined(CONFIG_USER_ONLY)
6725 GEN_OP_SPE_STWWE(raw);
6726 #else /* defined(CONFIG_USER_ONLY) */
6727 GEN_OP_SPE_STWWE(user);
6728 GEN_OP_SPE_STWWE(kernel);
6729 GEN_OP_SPE_STWWE(hypv);
6730 #endif /* defined(CONFIG_USER_ONLY) */
6731 GEN_SPEOP_ST(wwe, 2);
6732 GEN_SPEOP_ST(wwo, 2);
6734 #define GEN_SPE_LDSPLAT(name, op, suffix) \
6735 static always_inline void gen_op_spe_l##name##_##suffix (void) \
6737 gen_op_##op##_##suffix(); \
6738 gen_op_splatw_T1_64(); \
6741 #define GEN_OP_SPE_LHE(suffix) \
6742 static always_inline void gen_op_spe_lhe_##suffix (void) \
6744 gen_op_spe_lh_##suffix(); \
6745 gen_op_sli16_T1_64(); \
6748 #define GEN_OP_SPE_LHX(suffix) \
6749 static always_inline void gen_op_spe_lhx_##suffix (void) \
6751 gen_op_spe_lh_##suffix(); \
6752 gen_op_extsh_T1_64(); \
6755 #if defined(CONFIG_USER_ONLY)
6756 GEN_OP_SPE_LHE(raw);
6757 GEN_SPE_LDSPLAT(hhesplat, spe_lhe, raw);
6758 GEN_OP_SPE_LHE(le_raw);
6759 GEN_SPE_LDSPLAT(hhesplat, spe_lhe, le_raw);
6760 GEN_SPE_LDSPLAT(hhousplat, spe_lh, raw);
6761 GEN_SPE_LDSPLAT(hhousplat, spe_lh, le_raw);
6762 GEN_OP_SPE_LHX(raw);
6763 GEN_SPE_LDSPLAT(hhossplat, spe_lhx, raw);
6764 GEN_OP_SPE_LHX(le_raw);
6765 GEN_SPE_LDSPLAT(hhossplat, spe_lhx, le_raw);
6766 #if defined(TARGET_PPC64)
6767 GEN_OP_SPE_LHE(64_raw);
6768 GEN_SPE_LDSPLAT(hhesplat, spe_lhe, 64_raw);
6769 GEN_OP_SPE_LHE(le_64_raw);
6770 GEN_SPE_LDSPLAT(hhesplat, spe_lhe, le_64_raw);
6771 GEN_SPE_LDSPLAT(hhousplat, spe_lh, 64_raw);
6772 GEN_SPE_LDSPLAT(hhousplat, spe_lh, le_64_raw);
6773 GEN_OP_SPE_LHX(64_raw);
6774 GEN_SPE_LDSPLAT(hhossplat, spe_lhx, 64_raw);
6775 GEN_OP_SPE_LHX(le_64_raw);
6776 GEN_SPE_LDSPLAT(hhossplat, spe_lhx, le_64_raw);
6779 GEN_OP_SPE_LHE(user);
6780 GEN_OP_SPE_LHE(kernel);
6781 GEN_OP_SPE_LHE(hypv);
6782 GEN_SPE_LDSPLAT(hhesplat, spe_lhe, user);
6783 GEN_SPE_LDSPLAT(hhesplat, spe_lhe, kernel);
6784 GEN_SPE_LDSPLAT(hhesplat, spe_lhe, hypv);
6785 GEN_OP_SPE_LHE(le_user);
6786 GEN_OP_SPE_LHE(le_kernel);
6787 GEN_OP_SPE_LHE(le_hypv);
6788 GEN_SPE_LDSPLAT(hhesplat, spe_lhe, le_user);
6789 GEN_SPE_LDSPLAT(hhesplat, spe_lhe, le_kernel);
6790 GEN_SPE_LDSPLAT(hhesplat, spe_lhe, le_hypv);
6791 GEN_SPE_LDSPLAT(hhousplat, spe_lh, user);
6792 GEN_SPE_LDSPLAT(hhousplat, spe_lh, kernel);
6793 GEN_SPE_LDSPLAT(hhousplat, spe_lh, hypv);
6794 GEN_SPE_LDSPLAT(hhousplat, spe_lh, le_user);
6795 GEN_SPE_LDSPLAT(hhousplat, spe_lh, le_kernel);
6796 GEN_SPE_LDSPLAT(hhousplat, spe_lh, le_hypv);
6797 GEN_OP_SPE_LHX(user);
6798 GEN_OP_SPE_LHX(kernel);
6799 GEN_OP_SPE_LHX(hypv);
6800 GEN_SPE_LDSPLAT(hhossplat, spe_lhx, user);
6801 GEN_SPE_LDSPLAT(hhossplat, spe_lhx, kernel);
6802 GEN_SPE_LDSPLAT(hhossplat, spe_lhx, hypv);
6803 GEN_OP_SPE_LHX(le_user);
6804 GEN_OP_SPE_LHX(le_kernel);
6805 GEN_OP_SPE_LHX(le_hypv);
6806 GEN_SPE_LDSPLAT(hhossplat, spe_lhx, le_user);
6807 GEN_SPE_LDSPLAT(hhossplat, spe_lhx, le_kernel);
6808 GEN_SPE_LDSPLAT(hhossplat, spe_lhx, le_hypv);
6809 #if defined(TARGET_PPC64)
6810 GEN_OP_SPE_LHE(64_user);
6811 GEN_OP_SPE_LHE(64_kernel);
6812 GEN_OP_SPE_LHE(64_hypv);
6813 GEN_SPE_LDSPLAT(hhesplat, spe_lhe, 64_user);
6814 GEN_SPE_LDSPLAT(hhesplat, spe_lhe, 64_kernel);
6815 GEN_SPE_LDSPLAT(hhesplat, spe_lhe, 64_hypv);
6816 GEN_OP_SPE_LHE(le_64_user);
6817 GEN_OP_SPE_LHE(le_64_kernel);
6818 GEN_OP_SPE_LHE(le_64_hypv);
6819 GEN_SPE_LDSPLAT(hhesplat, spe_lhe, le_64_user);
6820 GEN_SPE_LDSPLAT(hhesplat, spe_lhe, le_64_kernel);
6821 GEN_SPE_LDSPLAT(hhesplat, spe_lhe, le_64_hypv);
6822 GEN_SPE_LDSPLAT(hhousplat, spe_lh, 64_user);
6823 GEN_SPE_LDSPLAT(hhousplat, spe_lh, 64_kernel);
6824 GEN_SPE_LDSPLAT(hhousplat, spe_lh, 64_hypv);
6825 GEN_SPE_LDSPLAT(hhousplat, spe_lh, le_64_user);
6826 GEN_SPE_LDSPLAT(hhousplat, spe_lh, le_64_kernel);
6827 GEN_SPE_LDSPLAT(hhousplat, spe_lh, le_64_hypv);
6828 GEN_OP_SPE_LHX(64_user);
6829 GEN_OP_SPE_LHX(64_kernel);
6830 GEN_OP_SPE_LHX(64_hypv);
6831 GEN_SPE_LDSPLAT(hhossplat, spe_lhx, 64_user);
6832 GEN_SPE_LDSPLAT(hhossplat, spe_lhx, 64_kernel);
6833 GEN_SPE_LDSPLAT(hhossplat, spe_lhx, 64_hypv);
6834 GEN_OP_SPE_LHX(le_64_user);
6835 GEN_OP_SPE_LHX(le_64_kernel);
6836 GEN_OP_SPE_LHX(le_64_hypv);
6837 GEN_SPE_LDSPLAT(hhossplat, spe_lhx, le_64_user);
6838 GEN_SPE_LDSPLAT(hhossplat, spe_lhx, le_64_kernel);
6839 GEN_SPE_LDSPLAT(hhossplat, spe_lhx, le_64_hypv);
6842 GEN_SPEOP_LD(hhesplat, 1);
6843 GEN_SPEOP_LD(hhousplat, 1);
6844 GEN_SPEOP_LD(hhossplat, 1);
6845 GEN_SPEOP_LD(wwsplat, 2);
6846 GEN_SPEOP_LD(whsplat, 2);
6848 GEN_SPE(evlddx, evldd, 0x00, 0x0C, 0x00000000, PPC_SPE); //
6849 GEN_SPE(evldwx, evldw, 0x01, 0x0C, 0x00000000, PPC_SPE); //
6850 GEN_SPE(evldhx, evldh, 0x02, 0x0C, 0x00000000, PPC_SPE); //
6851 GEN_SPE(evlhhesplatx, evlhhesplat, 0x04, 0x0C, 0x00000000, PPC_SPE); //
6852 GEN_SPE(evlhhousplatx, evlhhousplat, 0x06, 0x0C, 0x00000000, PPC_SPE); //
6853 GEN_SPE(evlhhossplatx, evlhhossplat, 0x07, 0x0C, 0x00000000, PPC_SPE); //
6854 GEN_SPE(evlwhex, evlwhe, 0x08, 0x0C, 0x00000000, PPC_SPE); //
6855 GEN_SPE(evlwhoux, evlwhou, 0x0A, 0x0C, 0x00000000, PPC_SPE); //
6856 GEN_SPE(evlwhosx, evlwhos, 0x0B, 0x0C, 0x00000000, PPC_SPE); //
6857 GEN_SPE(evlwwsplatx, evlwwsplat, 0x0C, 0x0C, 0x00000000, PPC_SPE); //
6858 GEN_SPE(evlwhsplatx, evlwhsplat, 0x0E, 0x0C, 0x00000000, PPC_SPE); //
6859 GEN_SPE(evstddx, evstdd, 0x10, 0x0C, 0x00000000, PPC_SPE); //
6860 GEN_SPE(evstdwx, evstdw, 0x11, 0x0C, 0x00000000, PPC_SPE); //
6861 GEN_SPE(evstdhx, evstdh, 0x12, 0x0C, 0x00000000, PPC_SPE); //
6862 GEN_SPE(evstwhex, evstwhe, 0x18, 0x0C, 0x00000000, PPC_SPE); //
6863 GEN_SPE(evstwhox, evstwho, 0x1A, 0x0C, 0x00000000, PPC_SPE); //
6864 GEN_SPE(evstwwex, evstwwe, 0x1C, 0x0C, 0x00000000, PPC_SPE); //
6865 GEN_SPE(evstwwox, evstwwo, 0x1E, 0x0C, 0x00000000, PPC_SPE); //
6867 /* Multiply and add - TODO */
6869 GEN_SPE(speundef, evmhessf, 0x01, 0x10, 0x00000000, PPC_SPE);
6870 GEN_SPE(speundef, evmhossf, 0x03, 0x10, 0x00000000, PPC_SPE);
6871 GEN_SPE(evmheumi, evmhesmi, 0x04, 0x10, 0x00000000, PPC_SPE);
6872 GEN_SPE(speundef, evmhesmf, 0x05, 0x10, 0x00000000, PPC_SPE);
6873 GEN_SPE(evmhoumi, evmhosmi, 0x06, 0x10, 0x00000000, PPC_SPE);
6874 GEN_SPE(speundef, evmhosmf, 0x07, 0x10, 0x00000000, PPC_SPE);
6875 GEN_SPE(speundef, evmhessfa, 0x11, 0x10, 0x00000000, PPC_SPE);
6876 GEN_SPE(speundef, evmhossfa, 0x13, 0x10, 0x00000000, PPC_SPE);
6877 GEN_SPE(evmheumia, evmhesmia, 0x14, 0x10, 0x00000000, PPC_SPE);
6878 GEN_SPE(speundef, evmhesmfa, 0x15, 0x10, 0x00000000, PPC_SPE);
6879 GEN_SPE(evmhoumia, evmhosmia, 0x16, 0x10, 0x00000000, PPC_SPE);
6880 GEN_SPE(speundef, evmhosmfa, 0x17, 0x10, 0x00000000, PPC_SPE);
6882 GEN_SPE(speundef, evmwhssf, 0x03, 0x11, 0x00000000, PPC_SPE);
6883 GEN_SPE(evmwlumi, speundef, 0x04, 0x11, 0x00000000, PPC_SPE);
6884 GEN_SPE(evmwhumi, evmwhsmi, 0x06, 0x11, 0x00000000, PPC_SPE);
6885 GEN_SPE(speundef, evmwhsmf, 0x07, 0x11, 0x00000000, PPC_SPE);
6886 GEN_SPE(speundef, evmwssf, 0x09, 0x11, 0x00000000, PPC_SPE);
6887 GEN_SPE(evmwumi, evmwsmi, 0x0C, 0x11, 0x00000000, PPC_SPE);
6888 GEN_SPE(speundef, evmwsmf, 0x0D, 0x11, 0x00000000, PPC_SPE);
6889 GEN_SPE(speundef, evmwhssfa, 0x13, 0x11, 0x00000000, PPC_SPE);
6890 GEN_SPE(evmwlumia, speundef, 0x14, 0x11, 0x00000000, PPC_SPE);
6891 GEN_SPE(evmwhumia, evmwhsmia, 0x16, 0x11, 0x00000000, PPC_SPE);
6892 GEN_SPE(speundef, evmwhsmfa, 0x17, 0x11, 0x00000000, PPC_SPE);
6893 GEN_SPE(speundef, evmwssfa, 0x19, 0x11, 0x00000000, PPC_SPE);
6894 GEN_SPE(evmwumia, evmwsmia, 0x1C, 0x11, 0x00000000, PPC_SPE);
6895 GEN_SPE(speundef, evmwsmfa, 0x1D, 0x11, 0x00000000, PPC_SPE);
6897 GEN_SPE(evadduiaaw, evaddsiaaw, 0x00, 0x13, 0x0000F800, PPC_SPE);
6898 GEN_SPE(evsubfusiaaw, evsubfssiaaw, 0x01, 0x13, 0x0000F800, PPC_SPE);
6899 GEN_SPE(evaddumiaaw, evaddsmiaaw, 0x04, 0x13, 0x0000F800, PPC_SPE);
6900 GEN_SPE(evsubfumiaaw, evsubfsmiaaw, 0x05, 0x13, 0x0000F800, PPC_SPE);
6901 GEN_SPE(evdivws, evdivwu, 0x06, 0x13, 0x00000000, PPC_SPE);
6902 GEN_SPE(evmra, speundef, 0x07, 0x13, 0x0000F800, PPC_SPE);
6904 GEN_SPE(evmheusiaaw, evmhessiaaw, 0x00, 0x14, 0x00000000, PPC_SPE);
6905 GEN_SPE(speundef, evmhessfaaw, 0x01, 0x14, 0x00000000, PPC_SPE);
6906 GEN_SPE(evmhousiaaw, evmhossiaaw, 0x02, 0x14, 0x00000000, PPC_SPE);
6907 GEN_SPE(speundef, evmhossfaaw, 0x03, 0x14, 0x00000000, PPC_SPE);
6908 GEN_SPE(evmheumiaaw, evmhesmiaaw, 0x04, 0x14, 0x00000000, PPC_SPE);
6909 GEN_SPE(speundef, evmhesmfaaw, 0x05, 0x14, 0x00000000, PPC_SPE);
6910 GEN_SPE(evmhoumiaaw, evmhosmiaaw, 0x06, 0x14, 0x00000000, PPC_SPE);
6911 GEN_SPE(speundef, evmhosmfaaw, 0x07, 0x14, 0x00000000, PPC_SPE);
6912 GEN_SPE(evmhegumiaa, evmhegsmiaa, 0x14, 0x14, 0x00000000, PPC_SPE);
6913 GEN_SPE(speundef, evmhegsmfaa, 0x15, 0x14, 0x00000000, PPC_SPE);
6914 GEN_SPE(evmhogumiaa, evmhogsmiaa, 0x16, 0x14, 0x00000000, PPC_SPE);
6915 GEN_SPE(speundef, evmhogsmfaa, 0x17, 0x14, 0x00000000, PPC_SPE);
6917 GEN_SPE(evmwlusiaaw, evmwlssiaaw, 0x00, 0x15, 0x00000000, PPC_SPE);
6918 GEN_SPE(evmwlumiaaw, evmwlsmiaaw, 0x04, 0x15, 0x00000000, PPC_SPE);
6919 GEN_SPE(speundef, evmwssfaa, 0x09, 0x15, 0x00000000, PPC_SPE);
6920 GEN_SPE(evmwumiaa, evmwsmiaa, 0x0C, 0x15, 0x00000000, PPC_SPE);
6921 GEN_SPE(speundef, evmwsmfaa, 0x0D, 0x15, 0x00000000, PPC_SPE);
6923 GEN_SPE(evmheusianw, evmhessianw, 0x00, 0x16, 0x00000000, PPC_SPE);
6924 GEN_SPE(speundef, evmhessfanw, 0x01, 0x16, 0x00000000, PPC_SPE);
6925 GEN_SPE(evmhousianw, evmhossianw, 0x02, 0x16, 0x00000000, PPC_SPE);
6926 GEN_SPE(speundef, evmhossfanw, 0x03, 0x16, 0x00000000, PPC_SPE);
6927 GEN_SPE(evmheumianw, evmhesmianw, 0x04, 0x16, 0x00000000, PPC_SPE);
6928 GEN_SPE(speundef, evmhesmfanw, 0x05, 0x16, 0x00000000, PPC_SPE);
6929 GEN_SPE(evmhoumianw, evmhosmianw, 0x06, 0x16, 0x00000000, PPC_SPE);
6930 GEN_SPE(speundef, evmhosmfanw, 0x07, 0x16, 0x00000000, PPC_SPE);
6931 GEN_SPE(evmhegumian, evmhegsmian, 0x14, 0x16, 0x00000000, PPC_SPE);
6932 GEN_SPE(speundef, evmhegsmfan, 0x15, 0x16, 0x00000000, PPC_SPE);
6933 GEN_SPE(evmhigumian, evmhigsmian, 0x16, 0x16, 0x00000000, PPC_SPE);
6934 GEN_SPE(speundef, evmhogsmfan, 0x17, 0x16, 0x00000000, PPC_SPE);
6936 GEN_SPE(evmwlusianw, evmwlssianw, 0x00, 0x17, 0x00000000, PPC_SPE);
6937 GEN_SPE(evmwlumianw, evmwlsmianw, 0x04, 0x17, 0x00000000, PPC_SPE);
6938 GEN_SPE(speundef, evmwssfan, 0x09, 0x17, 0x00000000, PPC_SPE);
6939 GEN_SPE(evmwumian, evmwsmian, 0x0C, 0x17, 0x00000000, PPC_SPE);
6940 GEN_SPE(speundef, evmwsmfan, 0x0D, 0x17, 0x00000000, PPC_SPE);
6943 /*** SPE floating-point extension ***/
6944 #if defined(TARGET_PPC64)
6945 #define GEN_SPEFPUOP_CONV_32_32(name) \
6946 static always_inline void gen_##name (DisasContext *ctx) \
6950 t0 = tcg_temp_new_i32(); \
6951 tcg_gen_trunc_tl_i32(t0, cpu_gpr[rB(ctx->opcode)]); \
6952 gen_helper_##name(t0, t0); \
6953 t1 = tcg_temp_new(); \
6954 tcg_gen_extu_i32_tl(t1, t0); \
6955 tcg_temp_free_i32(t0); \
6956 tcg_gen_andi_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rD(ctx->opcode)], \
6957 0xFFFFFFFF00000000ULL); \
6958 tcg_gen_or_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rD(ctx->opcode)], t1); \
6959 tcg_temp_free(t1); \
6961 #define GEN_SPEFPUOP_CONV_32_64(name) \
6962 static always_inline void gen_##name (DisasContext *ctx) \
6966 t0 = tcg_temp_new_i32(); \
6967 gen_helper_##name(t0, cpu_gpr[rB(ctx->opcode)]); \
6968 t1 = tcg_temp_new(); \
6969 tcg_gen_extu_i32_tl(t1, t0); \
6970 tcg_temp_free_i32(t0); \
6971 tcg_gen_andi_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rD(ctx->opcode)], \
6972 0xFFFFFFFF00000000ULL); \
6973 tcg_gen_or_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rD(ctx->opcode)], t1); \
6974 tcg_temp_free(t1); \
6976 #define GEN_SPEFPUOP_CONV_64_32(name) \
6977 static always_inline void gen_##name (DisasContext *ctx) \
6979 TCGv_i32 t0 = tcg_temp_new_i32(); \
6980 tcg_gen_trunc_tl_i32(t0, cpu_gpr[rB(ctx->opcode)]); \
6981 gen_helper_##name(cpu_gpr[rD(ctx->opcode)], t0); \
6982 tcg_temp_free_i32(t0); \
6984 #define GEN_SPEFPUOP_CONV_64_64(name) \
6985 static always_inline void gen_##name (DisasContext *ctx) \
6987 gen_helper_##name(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rB(ctx->opcode)]); \
6989 #define GEN_SPEFPUOP_ARITH2_32_32(name) \
6990 static always_inline void gen_##name (DisasContext *ctx) \
6994 if (unlikely(!ctx->spe_enabled)) { \
6995 GEN_EXCP_NO_AP(ctx); \
6998 t0 = tcg_temp_new_i32(); \
6999 t1 = tcg_temp_new_i32(); \
7000 tcg_gen_trunc_tl_i32(t0, cpu_gpr[rA(ctx->opcode)]); \
7001 tcg_gen_trunc_tl_i32(t1, cpu_gpr[rB(ctx->opcode)]); \
7002 gen_helper_##name(t0, t0, t1); \
7003 tcg_temp_free_i32(t1); \
7004 t2 = tcg_temp_new(); \
7005 tcg_gen_extu_i32_tl(t2, t0); \
7006 tcg_temp_free_i32(t0); \
7007 tcg_gen_andi_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rD(ctx->opcode)], \
7008 0xFFFFFFFF00000000ULL); \
7009 tcg_gen_or_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rD(ctx->opcode)], t2); \
7010 tcg_temp_free(t2); \
7012 #define GEN_SPEFPUOP_ARITH2_64_64(name) \
7013 static always_inline void gen_##name (DisasContext *ctx) \
7015 if (unlikely(!ctx->spe_enabled)) { \
7016 GEN_EXCP_NO_AP(ctx); \
7019 gen_helper_##name(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)], \
7020 cpu_gpr[rB(ctx->opcode)]); \
7022 #define GEN_SPEFPUOP_COMP_32(name) \
7023 static always_inline void gen_##name (DisasContext *ctx) \
7026 if (unlikely(!ctx->spe_enabled)) { \
7027 GEN_EXCP_NO_AP(ctx); \
7030 t0 = tcg_temp_new_i32(); \
7031 t1 = tcg_temp_new_i32(); \
7032 tcg_gen_trunc_tl_i32(t0, cpu_gpr[rA(ctx->opcode)]); \
7033 tcg_gen_trunc_tl_i32(t1, cpu_gpr[rB(ctx->opcode)]); \
7034 gen_helper_##name(cpu_crf[crfD(ctx->opcode)], t0, t1); \
7035 tcg_temp_free_i32(t0); \
7036 tcg_temp_free_i32(t1); \
7038 #define GEN_SPEFPUOP_COMP_64(name) \
7039 static always_inline void gen_##name (DisasContext *ctx) \
7041 if (unlikely(!ctx->spe_enabled)) { \
7042 GEN_EXCP_NO_AP(ctx); \
7045 gen_helper_##name(cpu_crf[crfD(ctx->opcode)], \
7046 cpu_gpr[rA(ctx->opcode)], cpu_gpr[rB(ctx->opcode)]); \
7049 #define GEN_SPEFPUOP_CONV_32_32(name) \
7050 static always_inline void gen_##name (DisasContext *ctx) \
7052 gen_helper_##name(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rB(ctx->opcode)]); \
7054 #define GEN_SPEFPUOP_CONV_32_64(name) \
7055 static always_inline void gen_##name (DisasContext *ctx) \
7057 TCGv_i64 t0 = tcg_temp_new_i64(); \
7058 gen_load_gpr64(t0, rB(ctx->opcode)); \
7059 gen_helper_##name(cpu_gpr[rD(ctx->opcode)], t0); \
7060 tcg_temp_free_i64(t0); \
7062 #define GEN_SPEFPUOP_CONV_64_32(name) \
7063 static always_inline void gen_##name (DisasContext *ctx) \
7065 TCGv_i64 t0 = tcg_temp_new_i64(); \
7066 gen_helper_##name(t0, cpu_gpr[rB(ctx->opcode)]); \
7067 gen_store_gpr64(rD(ctx->opcode), t0); \
7068 tcg_temp_free_i64(t0); \
7070 #define GEN_SPEFPUOP_CONV_64_64(name) \
7071 static always_inline void gen_##name (DisasContext *ctx) \
7073 TCGv_i64 t0 = tcg_temp_new_i64(); \
7074 gen_load_gpr64(t0, rB(ctx->opcode)); \
7075 gen_helper_##name(t0, t0); \
7076 gen_store_gpr64(rD(ctx->opcode), t0); \
7077 tcg_temp_free_i64(t0); \
7079 #define GEN_SPEFPUOP_ARITH2_32_32(name) \
7080 static always_inline void gen_##name (DisasContext *ctx) \
7082 if (unlikely(!ctx->spe_enabled)) { \
7083 GEN_EXCP_NO_AP(ctx); \
7086 gen_helper_##name(cpu_gpr[rD(ctx->opcode)], \
7087 cpu_gpr[rA(ctx->opcode)], cpu_gpr[rB(ctx->opcode)]); \
7089 #define GEN_SPEFPUOP_ARITH2_64_64(name) \
7090 static always_inline void gen_##name (DisasContext *ctx) \
7093 if (unlikely(!ctx->spe_enabled)) { \
7094 GEN_EXCP_NO_AP(ctx); \
7097 t0 = tcg_temp_new_i64(); \
7098 t1 = tcg_temp_new_i64(); \
7099 gen_load_gpr64(t0, rA(ctx->opcode)); \
7100 gen_load_gpr64(t1, rB(ctx->opcode)); \
7101 gen_helper_##name(t0, t0, t1); \
7102 gen_store_gpr64(rD(ctx->opcode), t0); \
7103 tcg_temp_free_i64(t0); \
7104 tcg_temp_free_i64(t1); \
7106 #define GEN_SPEFPUOP_COMP_32(name) \
7107 static always_inline void gen_##name (DisasContext *ctx) \
7109 if (unlikely(!ctx->spe_enabled)) { \
7110 GEN_EXCP_NO_AP(ctx); \
7113 gen_helper_##name(cpu_crf[crfD(ctx->opcode)], \
7114 cpu_gpr[rA(ctx->opcode)], cpu_gpr[rB(ctx->opcode)]); \
7116 #define GEN_SPEFPUOP_COMP_64(name) \
7117 static always_inline void gen_##name (DisasContext *ctx) \
7120 if (unlikely(!ctx->spe_enabled)) { \
7121 GEN_EXCP_NO_AP(ctx); \
7124 t0 = tcg_temp_new_i64(); \
7125 t1 = tcg_temp_new_i64(); \
7126 gen_load_gpr64(t0, rA(ctx->opcode)); \
7127 gen_load_gpr64(t1, rB(ctx->opcode)); \
7128 gen_helper_##name(cpu_crf[crfD(ctx->opcode)], t0, t1); \
7129 tcg_temp_free_i64(t0); \
7130 tcg_temp_free_i64(t1); \
7134 /* Single precision floating-point vectors operations */
7136 GEN_SPEFPUOP_ARITH2_64_64(evfsadd);
7137 GEN_SPEFPUOP_ARITH2_64_64(evfssub);
7138 GEN_SPEFPUOP_ARITH2_64_64(evfsmul);
7139 GEN_SPEFPUOP_ARITH2_64_64(evfsdiv);
7140 static always_inline void gen_evfsabs (DisasContext *ctx)
7142 if (unlikely(!ctx->spe_enabled)) {
7143 GEN_EXCP_NO_AP(ctx);
7146 #if defined(TARGET_PPC64)
7147 tcg_gen_andi_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rA(ctx->opcode)], ~0x8000000080000000LL);
7149 tcg_gen_andi_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rA(ctx->opcode)], ~0x80000000);
7150 tcg_gen_andi_tl(cpu_gprh[rA(ctx->opcode)], cpu_gprh[rA(ctx->opcode)], ~0x80000000);
7153 static always_inline void gen_evfsnabs (DisasContext *ctx)
7155 if (unlikely(!ctx->spe_enabled)) {
7156 GEN_EXCP_NO_AP(ctx);
7159 #if defined(TARGET_PPC64)
7160 tcg_gen_ori_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rA(ctx->opcode)], 0x8000000080000000LL);
7162 tcg_gen_ori_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rA(ctx->opcode)], 0x80000000);
7163 tcg_gen_ori_tl(cpu_gprh[rA(ctx->opcode)], cpu_gprh[rA(ctx->opcode)], 0x80000000);
7166 static always_inline void gen_evfsneg (DisasContext *ctx)
7168 if (unlikely(!ctx->spe_enabled)) {
7169 GEN_EXCP_NO_AP(ctx);
7172 #if defined(TARGET_PPC64)
7173 tcg_gen_xori_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rA(ctx->opcode)], 0x8000000080000000LL);
7175 tcg_gen_xori_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rA(ctx->opcode)], 0x80000000);
7176 tcg_gen_xori_tl(cpu_gprh[rA(ctx->opcode)], cpu_gprh[rA(ctx->opcode)], 0x80000000);
7181 GEN_SPEFPUOP_CONV_64_64(evfscfui);
7182 GEN_SPEFPUOP_CONV_64_64(evfscfsi);
7183 GEN_SPEFPUOP_CONV_64_64(evfscfuf);
7184 GEN_SPEFPUOP_CONV_64_64(evfscfsf);
7185 GEN_SPEFPUOP_CONV_64_64(evfsctui);
7186 GEN_SPEFPUOP_CONV_64_64(evfsctsi);
7187 GEN_SPEFPUOP_CONV_64_64(evfsctuf);
7188 GEN_SPEFPUOP_CONV_64_64(evfsctsf);
7189 GEN_SPEFPUOP_CONV_64_64(evfsctuiz);
7190 GEN_SPEFPUOP_CONV_64_64(evfsctsiz);
7193 GEN_SPEFPUOP_COMP_64(evfscmpgt);
7194 GEN_SPEFPUOP_COMP_64(evfscmplt);
7195 GEN_SPEFPUOP_COMP_64(evfscmpeq);
7196 GEN_SPEFPUOP_COMP_64(evfststgt);
7197 GEN_SPEFPUOP_COMP_64(evfststlt);
7198 GEN_SPEFPUOP_COMP_64(evfststeq);
7200 /* Opcodes definitions */
7201 GEN_SPE(evfsadd, evfssub, 0x00, 0x0A, 0x00000000, PPC_SPEFPU); //
7202 GEN_SPE(evfsabs, evfsnabs, 0x02, 0x0A, 0x0000F800, PPC_SPEFPU); //
7203 GEN_SPE(evfsneg, speundef, 0x03, 0x0A, 0x0000F800, PPC_SPEFPU); //
7204 GEN_SPE(evfsmul, evfsdiv, 0x04, 0x0A, 0x00000000, PPC_SPEFPU); //
7205 GEN_SPE(evfscmpgt, evfscmplt, 0x06, 0x0A, 0x00600000, PPC_SPEFPU); //
7206 GEN_SPE(evfscmpeq, speundef, 0x07, 0x0A, 0x00600000, PPC_SPEFPU); //
7207 GEN_SPE(evfscfui, evfscfsi, 0x08, 0x0A, 0x00180000, PPC_SPEFPU); //
7208 GEN_SPE(evfscfuf, evfscfsf, 0x09, 0x0A, 0x00180000, PPC_SPEFPU); //
7209 GEN_SPE(evfsctui, evfsctsi, 0x0A, 0x0A, 0x00180000, PPC_SPEFPU); //
7210 GEN_SPE(evfsctuf, evfsctsf, 0x0B, 0x0A, 0x00180000, PPC_SPEFPU); //
7211 GEN_SPE(evfsctuiz, speundef, 0x0C, 0x0A, 0x00180000, PPC_SPEFPU); //
7212 GEN_SPE(evfsctsiz, speundef, 0x0D, 0x0A, 0x00180000, PPC_SPEFPU); //
7213 GEN_SPE(evfststgt, evfststlt, 0x0E, 0x0A, 0x00600000, PPC_SPEFPU); //
7214 GEN_SPE(evfststeq, speundef, 0x0F, 0x0A, 0x00600000, PPC_SPEFPU); //
7216 /* Single precision floating-point operations */
7218 GEN_SPEFPUOP_ARITH2_32_32(efsadd);
7219 GEN_SPEFPUOP_ARITH2_32_32(efssub);
7220 GEN_SPEFPUOP_ARITH2_32_32(efsmul);
7221 GEN_SPEFPUOP_ARITH2_32_32(efsdiv);
7222 static always_inline void gen_efsabs (DisasContext *ctx)
7224 if (unlikely(!ctx->spe_enabled)) {
7225 GEN_EXCP_NO_AP(ctx);
7228 tcg_gen_andi_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rA(ctx->opcode)], (target_long)~0x80000000LL);
7230 static always_inline void gen_efsnabs (DisasContext *ctx)
7232 if (unlikely(!ctx->spe_enabled)) {
7233 GEN_EXCP_NO_AP(ctx);
7236 tcg_gen_ori_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rA(ctx->opcode)], 0x80000000);
7238 static always_inline void gen_efsneg (DisasContext *ctx)
7240 if (unlikely(!ctx->spe_enabled)) {
7241 GEN_EXCP_NO_AP(ctx);
7244 tcg_gen_xori_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rA(ctx->opcode)], 0x80000000);
7248 GEN_SPEFPUOP_CONV_32_32(efscfui);
7249 GEN_SPEFPUOP_CONV_32_32(efscfsi);
7250 GEN_SPEFPUOP_CONV_32_32(efscfuf);
7251 GEN_SPEFPUOP_CONV_32_32(efscfsf);
7252 GEN_SPEFPUOP_CONV_32_32(efsctui);
7253 GEN_SPEFPUOP_CONV_32_32(efsctsi);
7254 GEN_SPEFPUOP_CONV_32_32(efsctuf);
7255 GEN_SPEFPUOP_CONV_32_32(efsctsf);
7256 GEN_SPEFPUOP_CONV_32_32(efsctuiz);
7257 GEN_SPEFPUOP_CONV_32_32(efsctsiz);
7258 GEN_SPEFPUOP_CONV_32_64(efscfd);
7261 GEN_SPEFPUOP_COMP_32(efscmpgt);
7262 GEN_SPEFPUOP_COMP_32(efscmplt);
7263 GEN_SPEFPUOP_COMP_32(efscmpeq);
7264 GEN_SPEFPUOP_COMP_32(efststgt);
7265 GEN_SPEFPUOP_COMP_32(efststlt);
7266 GEN_SPEFPUOP_COMP_32(efststeq);
7268 /* Opcodes definitions */
7269 GEN_SPE(efsadd, efssub, 0x00, 0x0B, 0x00000000, PPC_SPEFPU); //
7270 GEN_SPE(efsabs, efsnabs, 0x02, 0x0B, 0x0000F800, PPC_SPEFPU); //
7271 GEN_SPE(efsneg, speundef, 0x03, 0x0B, 0x0000F800, PPC_SPEFPU); //
7272 GEN_SPE(efsmul, efsdiv, 0x04, 0x0B, 0x00000000, PPC_SPEFPU); //
7273 GEN_SPE(efscmpgt, efscmplt, 0x06, 0x0B, 0x00600000, PPC_SPEFPU); //
7274 GEN_SPE(efscmpeq, efscfd, 0x07, 0x0B, 0x00600000, PPC_SPEFPU); //
7275 GEN_SPE(efscfui, efscfsi, 0x08, 0x0B, 0x00180000, PPC_SPEFPU); //
7276 GEN_SPE(efscfuf, efscfsf, 0x09, 0x0B, 0x00180000, PPC_SPEFPU); //
7277 GEN_SPE(efsctui, efsctsi, 0x0A, 0x0B, 0x00180000, PPC_SPEFPU); //
7278 GEN_SPE(efsctuf, efsctsf, 0x0B, 0x0B, 0x00180000, PPC_SPEFPU); //
7279 GEN_SPE(efsctuiz, speundef, 0x0C, 0x0B, 0x00180000, PPC_SPEFPU); //
7280 GEN_SPE(efsctsiz, speundef, 0x0D, 0x0B, 0x00180000, PPC_SPEFPU); //
7281 GEN_SPE(efststgt, efststlt, 0x0E, 0x0B, 0x00600000, PPC_SPEFPU); //
7282 GEN_SPE(efststeq, speundef, 0x0F, 0x0B, 0x00600000, PPC_SPEFPU); //
7284 /* Double precision floating-point operations */
7286 GEN_SPEFPUOP_ARITH2_64_64(efdadd);
7287 GEN_SPEFPUOP_ARITH2_64_64(efdsub);
7288 GEN_SPEFPUOP_ARITH2_64_64(efdmul);
7289 GEN_SPEFPUOP_ARITH2_64_64(efddiv);
7290 static always_inline void gen_efdabs (DisasContext *ctx)
7292 if (unlikely(!ctx->spe_enabled)) {
7293 GEN_EXCP_NO_AP(ctx);
7296 #if defined(TARGET_PPC64)
7297 tcg_gen_andi_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rA(ctx->opcode)], ~0x8000000000000000LL);
7299 tcg_gen_andi_tl(cpu_gprh[rA(ctx->opcode)], cpu_gprh[rA(ctx->opcode)], ~0x80000000);
7302 static always_inline void gen_efdnabs (DisasContext *ctx)
7304 if (unlikely(!ctx->spe_enabled)) {
7305 GEN_EXCP_NO_AP(ctx);
7308 #if defined(TARGET_PPC64)
7309 tcg_gen_ori_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rA(ctx->opcode)], 0x8000000000000000LL);
7311 tcg_gen_ori_tl(cpu_gprh[rA(ctx->opcode)], cpu_gprh[rA(ctx->opcode)], 0x80000000);
7314 static always_inline void gen_efdneg (DisasContext *ctx)
7316 if (unlikely(!ctx->spe_enabled)) {
7317 GEN_EXCP_NO_AP(ctx);
7320 #if defined(TARGET_PPC64)
7321 tcg_gen_xori_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rA(ctx->opcode)], 0x8000000000000000LL);
7323 tcg_gen_xori_tl(cpu_gprh[rA(ctx->opcode)], cpu_gprh[rA(ctx->opcode)], 0x80000000);
7328 GEN_SPEFPUOP_CONV_64_32(efdcfui);
7329 GEN_SPEFPUOP_CONV_64_32(efdcfsi);
7330 GEN_SPEFPUOP_CONV_64_32(efdcfuf);
7331 GEN_SPEFPUOP_CONV_64_32(efdcfsf);
7332 GEN_SPEFPUOP_CONV_32_64(efdctui);
7333 GEN_SPEFPUOP_CONV_32_64(efdctsi);
7334 GEN_SPEFPUOP_CONV_32_64(efdctuf);
7335 GEN_SPEFPUOP_CONV_32_64(efdctsf);
7336 GEN_SPEFPUOP_CONV_32_64(efdctuiz);
7337 GEN_SPEFPUOP_CONV_32_64(efdctsiz);
7338 GEN_SPEFPUOP_CONV_64_32(efdcfs);
7339 GEN_SPEFPUOP_CONV_64_64(efdcfuid);
7340 GEN_SPEFPUOP_CONV_64_64(efdcfsid);
7341 GEN_SPEFPUOP_CONV_64_64(efdctuidz);
7342 GEN_SPEFPUOP_CONV_64_64(efdctsidz);
7345 GEN_SPEFPUOP_COMP_64(efdcmpgt);
7346 GEN_SPEFPUOP_COMP_64(efdcmplt);
7347 GEN_SPEFPUOP_COMP_64(efdcmpeq);
7348 GEN_SPEFPUOP_COMP_64(efdtstgt);
7349 GEN_SPEFPUOP_COMP_64(efdtstlt);
7350 GEN_SPEFPUOP_COMP_64(efdtsteq);
7352 /* Opcodes definitions */
7353 GEN_SPE(efdadd, efdsub, 0x10, 0x0B, 0x00000000, PPC_SPEFPU); //
7354 GEN_SPE(efdcfuid, efdcfsid, 0x11, 0x0B, 0x00180000, PPC_SPEFPU); //
7355 GEN_SPE(efdabs, efdnabs, 0x12, 0x0B, 0x0000F800, PPC_SPEFPU); //
7356 GEN_SPE(efdneg, speundef, 0x13, 0x0B, 0x0000F800, PPC_SPEFPU); //
7357 GEN_SPE(efdmul, efddiv, 0x14, 0x0B, 0x00000000, PPC_SPEFPU); //
7358 GEN_SPE(efdctuidz, efdctsidz, 0x15, 0x0B, 0x00180000, PPC_SPEFPU); //
7359 GEN_SPE(efdcmpgt, efdcmplt, 0x16, 0x0B, 0x00600000, PPC_SPEFPU); //
7360 GEN_SPE(efdcmpeq, efdcfs, 0x17, 0x0B, 0x00600000, PPC_SPEFPU); //
7361 GEN_SPE(efdcfui, efdcfsi, 0x18, 0x0B, 0x00180000, PPC_SPEFPU); //
7362 GEN_SPE(efdcfuf, efdcfsf, 0x19, 0x0B, 0x00180000, PPC_SPEFPU); //
7363 GEN_SPE(efdctui, efdctsi, 0x1A, 0x0B, 0x00180000, PPC_SPEFPU); //
7364 GEN_SPE(efdctuf, efdctsf, 0x1B, 0x0B, 0x00180000, PPC_SPEFPU); //
7365 GEN_SPE(efdctuiz, speundef, 0x1C, 0x0B, 0x00180000, PPC_SPEFPU); //
7366 GEN_SPE(efdctsiz, speundef, 0x1D, 0x0B, 0x00180000, PPC_SPEFPU); //
7367 GEN_SPE(efdtstgt, efdtstlt, 0x1E, 0x0B, 0x00600000, PPC_SPEFPU); //
7368 GEN_SPE(efdtsteq, speundef, 0x1F, 0x0B, 0x00600000, PPC_SPEFPU); //
7370 /* End opcode list */
7371 GEN_OPCODE_MARK(end);
7373 #include "translate_init.c"
7374 #include "helper_regs.h"
7376 /*****************************************************************************/
7377 /* Misc PowerPC helpers */
7378 void cpu_dump_state (CPUState *env, FILE *f,
7379 int (*cpu_fprintf)(FILE *f, const char *fmt, ...),
7387 cpu_fprintf(f, "NIP " ADDRX " LR " ADDRX " CTR " ADDRX " XER %08x\n",
7388 env->nip, env->lr, env->ctr, env->xer);
7389 cpu_fprintf(f, "MSR " ADDRX " HID0 " ADDRX " HF " ADDRX " idx %d\n",
7390 env->msr, env->spr[SPR_HID0], env->hflags, env->mmu_idx);
7391 #if !defined(NO_TIMER_DUMP)
7392 cpu_fprintf(f, "TB %08x %08x "
7393 #if !defined(CONFIG_USER_ONLY)
7397 cpu_ppc_load_tbu(env), cpu_ppc_load_tbl(env)
7398 #if !defined(CONFIG_USER_ONLY)
7399 , cpu_ppc_load_decr(env)
7403 for (i = 0; i < 32; i++) {
7404 if ((i & (RGPL - 1)) == 0)
7405 cpu_fprintf(f, "GPR%02d", i);
7406 cpu_fprintf(f, " " REGX, ppc_dump_gpr(env, i));
7407 if ((i & (RGPL - 1)) == (RGPL - 1))
7408 cpu_fprintf(f, "\n");
7410 cpu_fprintf(f, "CR ");
7411 for (i = 0; i < 8; i++)
7412 cpu_fprintf(f, "%01x", env->crf[i]);
7413 cpu_fprintf(f, " [");
7414 for (i = 0; i < 8; i++) {
7416 if (env->crf[i] & 0x08)
7418 else if (env->crf[i] & 0x04)
7420 else if (env->crf[i] & 0x02)
7422 cpu_fprintf(f, " %c%c", a, env->crf[i] & 0x01 ? 'O' : ' ');
7424 cpu_fprintf(f, " ] RES " ADDRX "\n", env->reserve);
7425 for (i = 0; i < 32; i++) {
7426 if ((i & (RFPL - 1)) == 0)
7427 cpu_fprintf(f, "FPR%02d", i);
7428 cpu_fprintf(f, " %016" PRIx64, *((uint64_t *)&env->fpr[i]));
7429 if ((i & (RFPL - 1)) == (RFPL - 1))
7430 cpu_fprintf(f, "\n");
7432 #if !defined(CONFIG_USER_ONLY)
7433 cpu_fprintf(f, "SRR0 " ADDRX " SRR1 " ADDRX " SDR1 " ADDRX "\n",
7434 env->spr[SPR_SRR0], env->spr[SPR_SRR1], env->sdr1);
7441 void cpu_dump_statistics (CPUState *env, FILE*f,
7442 int (*cpu_fprintf)(FILE *f, const char *fmt, ...),
7445 #if defined(DO_PPC_STATISTICS)
7446 opc_handler_t **t1, **t2, **t3, *handler;
7450 for (op1 = 0; op1 < 64; op1++) {
7452 if (is_indirect_opcode(handler)) {
7453 t2 = ind_table(handler);
7454 for (op2 = 0; op2 < 32; op2++) {
7456 if (is_indirect_opcode(handler)) {
7457 t3 = ind_table(handler);
7458 for (op3 = 0; op3 < 32; op3++) {
7460 if (handler->count == 0)
7462 cpu_fprintf(f, "%02x %02x %02x (%02x %04d) %16s: "
7464 op1, op2, op3, op1, (op3 << 5) | op2,
7466 handler->count, handler->count);
7469 if (handler->count == 0)
7471 cpu_fprintf(f, "%02x %02x (%02x %04d) %16s: "
7473 op1, op2, op1, op2, handler->oname,
7474 handler->count, handler->count);
7478 if (handler->count == 0)
7480 cpu_fprintf(f, "%02x (%02x ) %16s: %016llx %lld\n",
7481 op1, op1, handler->oname,
7482 handler->count, handler->count);
7488 /*****************************************************************************/
7489 static always_inline void gen_intermediate_code_internal (CPUState *env,
7490 TranslationBlock *tb,
7493 DisasContext ctx, *ctxp = &ctx;
7494 opc_handler_t **table, *handler;
7495 target_ulong pc_start;
7496 uint16_t *gen_opc_end;
7497 int supervisor, little_endian;
7504 gen_opc_end = gen_opc_buf + OPC_MAX_SIZE;
7505 #if defined(OPTIMIZE_FPRF_UPDATE)
7506 gen_fprf_ptr = gen_fprf_buf;
7510 ctx.exception = POWERPC_EXCP_NONE;
7511 ctx.spr_cb = env->spr_cb;
7512 supervisor = env->mmu_idx;
7513 #if !defined(CONFIG_USER_ONLY)
7514 ctx.supervisor = supervisor;
7516 little_endian = env->hflags & (1 << MSR_LE) ? 1 : 0;
7517 #if defined(TARGET_PPC64)
7518 ctx.sf_mode = msr_sf;
7519 ctx.mem_idx = (supervisor << 2) | (msr_sf << 1) | little_endian;
7521 ctx.mem_idx = (supervisor << 1) | little_endian;
7523 ctx.dcache_line_size = env->dcache_line_size;
7524 ctx.fpu_enabled = msr_fp;
7525 if ((env->flags & POWERPC_FLAG_SPE) && msr_spe)
7526 ctx.spe_enabled = msr_spe;
7528 ctx.spe_enabled = 0;
7529 if ((env->flags & POWERPC_FLAG_VRE) && msr_vr)
7530 ctx.altivec_enabled = msr_vr;
7532 ctx.altivec_enabled = 0;
7533 if ((env->flags & POWERPC_FLAG_SE) && msr_se)
7534 ctx.singlestep_enabled = CPU_SINGLE_STEP;
7536 ctx.singlestep_enabled = 0;
7537 if ((env->flags & POWERPC_FLAG_BE) && msr_be)
7538 ctx.singlestep_enabled |= CPU_BRANCH_STEP;
7539 if (unlikely(env->singlestep_enabled))
7540 ctx.singlestep_enabled |= GDBSTUB_SINGLE_STEP;
7541 #if defined (DO_SINGLE_STEP) && 0
7542 /* Single step trace mode */
7546 max_insns = tb->cflags & CF_COUNT_MASK;
7548 max_insns = CF_COUNT_MASK;
7551 /* Set env in case of segfault during code fetch */
7552 while (ctx.exception == POWERPC_EXCP_NONE && gen_opc_ptr < gen_opc_end) {
7553 if (unlikely(!TAILQ_EMPTY(&env->breakpoints))) {
7554 TAILQ_FOREACH(bp, &env->breakpoints, entry) {
7555 if (bp->pc == ctx.nip) {
7556 gen_update_nip(&ctx, ctx.nip);
7557 gen_helper_raise_debug();
7562 if (unlikely(search_pc)) {
7563 j = gen_opc_ptr - gen_opc_buf;
7567 gen_opc_instr_start[lj++] = 0;
7568 gen_opc_pc[lj] = ctx.nip;
7569 gen_opc_instr_start[lj] = 1;
7570 gen_opc_icount[lj] = num_insns;
7573 #if defined PPC_DEBUG_DISAS
7574 if (loglevel & CPU_LOG_TB_IN_ASM) {
7575 fprintf(logfile, "----------------\n");
7576 fprintf(logfile, "nip=" ADDRX " super=%d ir=%d\n",
7577 ctx.nip, supervisor, (int)msr_ir);
7580 if (num_insns + 1 == max_insns && (tb->cflags & CF_LAST_IO))
7582 if (unlikely(little_endian)) {
7583 ctx.opcode = bswap32(ldl_code(ctx.nip));
7585 ctx.opcode = ldl_code(ctx.nip);
7587 #if defined PPC_DEBUG_DISAS
7588 if (loglevel & CPU_LOG_TB_IN_ASM) {
7589 fprintf(logfile, "translate opcode %08x (%02x %02x %02x) (%s)\n",
7590 ctx.opcode, opc1(ctx.opcode), opc2(ctx.opcode),
7591 opc3(ctx.opcode), little_endian ? "little" : "big");
7595 table = env->opcodes;
7597 handler = table[opc1(ctx.opcode)];
7598 if (is_indirect_opcode(handler)) {
7599 table = ind_table(handler);
7600 handler = table[opc2(ctx.opcode)];
7601 if (is_indirect_opcode(handler)) {
7602 table = ind_table(handler);
7603 handler = table[opc3(ctx.opcode)];
7606 /* Is opcode *REALLY* valid ? */
7607 if (unlikely(handler->handler == &gen_invalid)) {
7608 if (loglevel != 0) {
7609 fprintf(logfile, "invalid/unsupported opcode: "
7610 "%02x - %02x - %02x (%08x) " ADDRX " %d\n",
7611 opc1(ctx.opcode), opc2(ctx.opcode),
7612 opc3(ctx.opcode), ctx.opcode, ctx.nip - 4, (int)msr_ir);
7614 printf("invalid/unsupported opcode: "
7615 "%02x - %02x - %02x (%08x) " ADDRX " %d\n",
7616 opc1(ctx.opcode), opc2(ctx.opcode),
7617 opc3(ctx.opcode), ctx.opcode, ctx.nip - 4, (int)msr_ir);
7620 if (unlikely((ctx.opcode & handler->inval) != 0)) {
7621 if (loglevel != 0) {
7622 fprintf(logfile, "invalid bits: %08x for opcode: "
7623 "%02x - %02x - %02x (%08x) " ADDRX "\n",
7624 ctx.opcode & handler->inval, opc1(ctx.opcode),
7625 opc2(ctx.opcode), opc3(ctx.opcode),
7626 ctx.opcode, ctx.nip - 4);
7628 printf("invalid bits: %08x for opcode: "
7629 "%02x - %02x - %02x (%08x) " ADDRX "\n",
7630 ctx.opcode & handler->inval, opc1(ctx.opcode),
7631 opc2(ctx.opcode), opc3(ctx.opcode),
7632 ctx.opcode, ctx.nip - 4);
7634 GEN_EXCP_INVAL(ctxp);
7638 (*(handler->handler))(&ctx);
7639 #if defined(DO_PPC_STATISTICS)
7642 /* Check trace mode exceptions */
7643 if (unlikely(ctx.singlestep_enabled & CPU_SINGLE_STEP &&
7644 (ctx.nip <= 0x100 || ctx.nip > 0xF00) &&
7645 ctx.exception != POWERPC_SYSCALL &&
7646 ctx.exception != POWERPC_EXCP_TRAP &&
7647 ctx.exception != POWERPC_EXCP_BRANCH)) {
7648 GEN_EXCP(ctxp, POWERPC_EXCP_TRACE, 0);
7649 } else if (unlikely(((ctx.nip & (TARGET_PAGE_SIZE - 1)) == 0) ||
7650 (env->singlestep_enabled) ||
7651 num_insns >= max_insns)) {
7652 /* if we reach a page boundary or are single stepping, stop
7657 #if defined (DO_SINGLE_STEP)
7661 if (tb->cflags & CF_LAST_IO)
7663 if (ctx.exception == POWERPC_EXCP_NONE) {
7664 gen_goto_tb(&ctx, 0, ctx.nip);
7665 } else if (ctx.exception != POWERPC_EXCP_BRANCH) {
7666 if (unlikely(env->singlestep_enabled)) {
7667 gen_update_nip(&ctx, ctx.nip);
7668 gen_helper_raise_debug();
7670 /* Generate the return instruction */
7673 gen_icount_end(tb, num_insns);
7674 *gen_opc_ptr = INDEX_op_end;
7675 if (unlikely(search_pc)) {
7676 j = gen_opc_ptr - gen_opc_buf;
7679 gen_opc_instr_start[lj++] = 0;
7681 tb->size = ctx.nip - pc_start;
7682 tb->icount = num_insns;
7684 #if defined(DEBUG_DISAS)
7685 if (loglevel & CPU_LOG_TB_CPU) {
7686 fprintf(logfile, "---------------- excp: %04x\n", ctx.exception);
7687 cpu_dump_state(env, logfile, fprintf, 0);
7689 if (loglevel & CPU_LOG_TB_IN_ASM) {
7691 flags = env->bfd_mach;
7692 flags |= little_endian << 16;
7693 fprintf(logfile, "IN: %s\n", lookup_symbol(pc_start));
7694 target_disas(logfile, pc_start, ctx.nip - pc_start, flags);
7695 fprintf(logfile, "\n");
7700 void gen_intermediate_code (CPUState *env, struct TranslationBlock *tb)
7702 gen_intermediate_code_internal(env, tb, 0);
7705 void gen_intermediate_code_pc (CPUState *env, struct TranslationBlock *tb)
7707 gen_intermediate_code_internal(env, tb, 1);
7710 void gen_pc_load(CPUState *env, TranslationBlock *tb,
7711 unsigned long searched_pc, int pc_pos, void *puc)
7713 env->nip = gen_opc_pc[pc_pos];