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];
70 static TCGv cpu_reserve;
71 static TCGv_i32 cpu_fpscr;
72 static TCGv_i32 cpu_access_type;
74 #include "gen-icount.h"
76 void ppc_translate_init(void)
80 static int done_init = 0;
85 cpu_env = tcg_global_reg_new_ptr(TCG_AREG0, "env");
89 for (i = 0; i < 8; i++) {
90 sprintf(p, "crf%d", i);
91 cpu_crf[i] = tcg_global_mem_new_i32(TCG_AREG0,
92 offsetof(CPUState, crf[i]), p);
96 for (i = 0; i < 32; i++) {
98 cpu_gpr[i] = tcg_global_mem_new(TCG_AREG0,
99 offsetof(CPUState, gpr[i]), p);
100 p += (i < 10) ? 3 : 4;
101 #if !defined(TARGET_PPC64)
102 sprintf(p, "r%dH", i);
103 cpu_gprh[i] = tcg_global_mem_new_i32(TCG_AREG0,
104 offsetof(CPUState, gprh[i]), p);
105 p += (i < 10) ? 4 : 5;
108 sprintf(p, "fp%d", i);
109 cpu_fpr[i] = tcg_global_mem_new_i64(TCG_AREG0,
110 offsetof(CPUState, fpr[i]), p);
111 p += (i < 10) ? 4 : 5;
113 sprintf(p, "avr%dH", i);
114 #ifdef WORDS_BIGENDIAN
115 cpu_avrh[i] = tcg_global_mem_new_i64(TCG_AREG0,
116 offsetof(CPUState, avr[i].u64[0]), p);
118 cpu_avrh[i] = tcg_global_mem_new_i64(TCG_AREG0,
119 offsetof(CPUState, avr[i].u64[1]), p);
121 p += (i < 10) ? 6 : 7;
123 sprintf(p, "avr%dL", i);
124 #ifdef WORDS_BIGENDIAN
125 cpu_avrl[i] = tcg_global_mem_new_i64(TCG_AREG0,
126 offsetof(CPUState, avr[i].u64[1]), p);
128 cpu_avrl[i] = tcg_global_mem_new_i64(TCG_AREG0,
129 offsetof(CPUState, avr[i].u64[0]), p);
131 p += (i < 10) ? 6 : 7;
134 cpu_nip = tcg_global_mem_new(TCG_AREG0,
135 offsetof(CPUState, nip), "nip");
137 cpu_msr = tcg_global_mem_new(TCG_AREG0,
138 offsetof(CPUState, msr), "msr");
140 cpu_ctr = tcg_global_mem_new(TCG_AREG0,
141 offsetof(CPUState, ctr), "ctr");
143 cpu_lr = tcg_global_mem_new(TCG_AREG0,
144 offsetof(CPUState, lr), "lr");
146 cpu_xer = tcg_global_mem_new(TCG_AREG0,
147 offsetof(CPUState, xer), "xer");
149 cpu_reserve = tcg_global_mem_new(TCG_AREG0,
150 offsetof(CPUState, reserve), "reserve");
152 cpu_fpscr = tcg_global_mem_new_i32(TCG_AREG0,
153 offsetof(CPUState, fpscr), "fpscr");
155 cpu_access_type = tcg_global_mem_new_i32(TCG_AREG0,
156 offsetof(CPUState, access_type), "access_type");
158 /* register helpers */
165 #if defined(OPTIMIZE_FPRF_UPDATE)
166 static uint16_t *gen_fprf_buf[OPC_BUF_SIZE];
167 static uint16_t **gen_fprf_ptr;
170 /* internal defines */
171 typedef struct DisasContext {
172 struct TranslationBlock *tb;
176 /* Routine used to access memory */
179 /* Translation flags */
181 #if defined(TARGET_PPC64)
187 ppc_spr_t *spr_cb; /* Needed to check rights for mfspr/mtspr */
188 int singlestep_enabled;
191 struct opc_handler_t {
194 /* instruction type */
197 void (*handler)(DisasContext *ctx);
198 #if defined(DO_PPC_STATISTICS) || defined(PPC_DUMP_CPU)
201 #if defined(DO_PPC_STATISTICS)
206 static always_inline void gen_reset_fpstatus (void)
208 #ifdef CONFIG_SOFTFLOAT
209 gen_helper_reset_fpstatus();
213 static always_inline void gen_compute_fprf (TCGv_i64 arg, int set_fprf, int set_rc)
215 TCGv_i32 t0 = tcg_temp_new_i32();
218 /* This case might be optimized later */
219 #if defined(OPTIMIZE_FPRF_UPDATE)
220 *gen_fprf_ptr++ = gen_opc_ptr;
222 tcg_gen_movi_i32(t0, 1);
223 gen_helper_compute_fprf(t0, arg, t0);
224 if (unlikely(set_rc)) {
225 tcg_gen_mov_i32(cpu_crf[1], t0);
227 gen_helper_float_check_status();
228 } else if (unlikely(set_rc)) {
229 /* We always need to compute fpcc */
230 tcg_gen_movi_i32(t0, 0);
231 gen_helper_compute_fprf(t0, arg, t0);
232 tcg_gen_mov_i32(cpu_crf[1], t0);
235 tcg_temp_free_i32(t0);
238 static always_inline void gen_optimize_fprf (void)
240 #if defined(OPTIMIZE_FPRF_UPDATE)
243 for (ptr = gen_fprf_buf; ptr != (gen_fprf_ptr - 1); ptr++)
244 *ptr = INDEX_op_nop1;
245 gen_fprf_ptr = gen_fprf_buf;
249 static always_inline void gen_set_access_type (DisasContext *ctx, int access_type)
251 if (ctx->access_type != access_type) {
252 tcg_gen_movi_i32(cpu_access_type, access_type);
253 ctx->access_type = access_type;
257 static always_inline void gen_update_nip (DisasContext *ctx, target_ulong nip)
259 #if defined(TARGET_PPC64)
261 tcg_gen_movi_tl(cpu_nip, nip);
264 tcg_gen_movi_tl(cpu_nip, (uint32_t)nip);
267 static always_inline void gen_exception_err (DisasContext *ctx, uint32_t excp, uint32_t error)
270 if (ctx->exception == POWERPC_EXCP_NONE) {
271 gen_update_nip(ctx, ctx->nip);
273 t0 = tcg_const_i32(excp);
274 t1 = tcg_const_i32(error);
275 gen_helper_raise_exception_err(t0, t1);
276 tcg_temp_free_i32(t0);
277 tcg_temp_free_i32(t1);
278 ctx->exception = (excp);
281 static always_inline void gen_exception (DisasContext *ctx, uint32_t excp)
284 if (ctx->exception == POWERPC_EXCP_NONE) {
285 gen_update_nip(ctx, ctx->nip);
287 t0 = tcg_const_i32(excp);
288 gen_helper_raise_exception(t0);
289 tcg_temp_free_i32(t0);
290 ctx->exception = (excp);
293 static always_inline void gen_debug_exception (DisasContext *ctx)
296 gen_update_nip(ctx, ctx->nip);
297 t0 = tcg_const_i32(EXCP_DEBUG);
298 gen_helper_raise_exception(t0);
299 tcg_temp_free_i32(t0);
302 static always_inline void gen_inval_exception (DisasContext *ctx, uint32_t error)
304 gen_exception_err(ctx, POWERPC_EXCP_PROGRAM, POWERPC_EXCP_INVAL | error);
307 /* Stop translation */
308 static always_inline void gen_stop_exception (DisasContext *ctx)
310 gen_update_nip(ctx, ctx->nip);
311 ctx->exception = POWERPC_EXCP_STOP;
314 /* No need to update nip here, as execution flow will change */
315 static always_inline void gen_sync_exception (DisasContext *ctx)
317 ctx->exception = POWERPC_EXCP_SYNC;
320 #define GEN_HANDLER(name, opc1, opc2, opc3, inval, type) \
321 static void gen_##name (DisasContext *ctx); \
322 GEN_OPCODE(name, opc1, opc2, opc3, inval, type); \
323 static void gen_##name (DisasContext *ctx)
325 #define GEN_HANDLER2(name, onam, opc1, opc2, opc3, inval, type) \
326 static void gen_##name (DisasContext *ctx); \
327 GEN_OPCODE2(name, onam, opc1, opc2, opc3, inval, type); \
328 static void gen_##name (DisasContext *ctx)
330 typedef struct opcode_t {
331 unsigned char opc1, opc2, opc3;
332 #if HOST_LONG_BITS == 64 /* Explicitly align to 64 bits */
333 unsigned char pad[5];
335 unsigned char pad[1];
337 opc_handler_t handler;
341 /*****************************************************************************/
342 /*** Instruction decoding ***/
343 #define EXTRACT_HELPER(name, shift, nb) \
344 static always_inline uint32_t name (uint32_t opcode) \
346 return (opcode >> (shift)) & ((1 << (nb)) - 1); \
349 #define EXTRACT_SHELPER(name, shift, nb) \
350 static always_inline int32_t name (uint32_t opcode) \
352 return (int16_t)((opcode >> (shift)) & ((1 << (nb)) - 1)); \
356 EXTRACT_HELPER(opc1, 26, 6);
358 EXTRACT_HELPER(opc2, 1, 5);
360 EXTRACT_HELPER(opc3, 6, 5);
361 /* Update Cr0 flags */
362 EXTRACT_HELPER(Rc, 0, 1);
364 EXTRACT_HELPER(rD, 21, 5);
366 EXTRACT_HELPER(rS, 21, 5);
368 EXTRACT_HELPER(rA, 16, 5);
370 EXTRACT_HELPER(rB, 11, 5);
372 EXTRACT_HELPER(rC, 6, 5);
374 EXTRACT_HELPER(crfD, 23, 3);
375 EXTRACT_HELPER(crfS, 18, 3);
376 EXTRACT_HELPER(crbD, 21, 5);
377 EXTRACT_HELPER(crbA, 16, 5);
378 EXTRACT_HELPER(crbB, 11, 5);
380 EXTRACT_HELPER(_SPR, 11, 10);
381 static always_inline uint32_t SPR (uint32_t opcode)
383 uint32_t sprn = _SPR(opcode);
385 return ((sprn >> 5) & 0x1F) | ((sprn & 0x1F) << 5);
387 /*** Get constants ***/
388 EXTRACT_HELPER(IMM, 12, 8);
389 /* 16 bits signed immediate value */
390 EXTRACT_SHELPER(SIMM, 0, 16);
391 /* 16 bits unsigned immediate value */
392 EXTRACT_HELPER(UIMM, 0, 16);
394 EXTRACT_HELPER(NB, 11, 5);
396 EXTRACT_HELPER(SH, 11, 5);
398 EXTRACT_HELPER(MB, 6, 5);
400 EXTRACT_HELPER(ME, 1, 5);
402 EXTRACT_HELPER(TO, 21, 5);
404 EXTRACT_HELPER(CRM, 12, 8);
405 EXTRACT_HELPER(FM, 17, 8);
406 EXTRACT_HELPER(SR, 16, 4);
407 EXTRACT_HELPER(FPIMM, 12, 4);
409 /*** Jump target decoding ***/
411 EXTRACT_SHELPER(d, 0, 16);
412 /* Immediate address */
413 static always_inline target_ulong LI (uint32_t opcode)
415 return (opcode >> 0) & 0x03FFFFFC;
418 static always_inline uint32_t BD (uint32_t opcode)
420 return (opcode >> 0) & 0xFFFC;
423 EXTRACT_HELPER(BO, 21, 5);
424 EXTRACT_HELPER(BI, 16, 5);
425 /* Absolute/relative address */
426 EXTRACT_HELPER(AA, 1, 1);
428 EXTRACT_HELPER(LK, 0, 1);
430 /* Create a mask between <start> and <end> bits */
431 static always_inline target_ulong MASK (uint32_t start, uint32_t end)
435 #if defined(TARGET_PPC64)
436 if (likely(start == 0)) {
437 ret = UINT64_MAX << (63 - end);
438 } else if (likely(end == 63)) {
439 ret = UINT64_MAX >> start;
442 if (likely(start == 0)) {
443 ret = UINT32_MAX << (31 - end);
444 } else if (likely(end == 31)) {
445 ret = UINT32_MAX >> start;
449 ret = (((target_ulong)(-1ULL)) >> (start)) ^
450 (((target_ulong)(-1ULL) >> (end)) >> 1);
451 if (unlikely(start > end))
458 /*****************************************************************************/
459 /* PowerPC Instructions types definitions */
461 PPC_NONE = 0x0000000000000000ULL,
462 /* PowerPC base instructions set */
463 PPC_INSNS_BASE = 0x0000000000000001ULL,
464 /* integer operations instructions */
465 #define PPC_INTEGER PPC_INSNS_BASE
466 /* flow control instructions */
467 #define PPC_FLOW PPC_INSNS_BASE
468 /* virtual memory instructions */
469 #define PPC_MEM PPC_INSNS_BASE
470 /* ld/st with reservation instructions */
471 #define PPC_RES PPC_INSNS_BASE
472 /* spr/msr access instructions */
473 #define PPC_MISC PPC_INSNS_BASE
474 /* Deprecated instruction sets */
475 /* Original POWER instruction set */
476 PPC_POWER = 0x0000000000000002ULL,
477 /* POWER2 instruction set extension */
478 PPC_POWER2 = 0x0000000000000004ULL,
479 /* Power RTC support */
480 PPC_POWER_RTC = 0x0000000000000008ULL,
481 /* Power-to-PowerPC bridge (601) */
482 PPC_POWER_BR = 0x0000000000000010ULL,
483 /* 64 bits PowerPC instruction set */
484 PPC_64B = 0x0000000000000020ULL,
485 /* New 64 bits extensions (PowerPC 2.0x) */
486 PPC_64BX = 0x0000000000000040ULL,
487 /* 64 bits hypervisor extensions */
488 PPC_64H = 0x0000000000000080ULL,
489 /* New wait instruction (PowerPC 2.0x) */
490 PPC_WAIT = 0x0000000000000100ULL,
491 /* Time base mftb instruction */
492 PPC_MFTB = 0x0000000000000200ULL,
494 /* Fixed-point unit extensions */
495 /* PowerPC 602 specific */
496 PPC_602_SPEC = 0x0000000000000400ULL,
497 /* isel instruction */
498 PPC_ISEL = 0x0000000000000800ULL,
499 /* popcntb instruction */
500 PPC_POPCNTB = 0x0000000000001000ULL,
501 /* string load / store */
502 PPC_STRING = 0x0000000000002000ULL,
504 /* Floating-point unit extensions */
505 /* Optional floating point instructions */
506 PPC_FLOAT = 0x0000000000010000ULL,
507 /* New floating-point extensions (PowerPC 2.0x) */
508 PPC_FLOAT_EXT = 0x0000000000020000ULL,
509 PPC_FLOAT_FSQRT = 0x0000000000040000ULL,
510 PPC_FLOAT_FRES = 0x0000000000080000ULL,
511 PPC_FLOAT_FRSQRTE = 0x0000000000100000ULL,
512 PPC_FLOAT_FRSQRTES = 0x0000000000200000ULL,
513 PPC_FLOAT_FSEL = 0x0000000000400000ULL,
514 PPC_FLOAT_STFIWX = 0x0000000000800000ULL,
516 /* Vector/SIMD extensions */
517 /* Altivec support */
518 PPC_ALTIVEC = 0x0000000001000000ULL,
519 /* PowerPC 2.03 SPE extension */
520 PPC_SPE = 0x0000000002000000ULL,
521 /* PowerPC 2.03 SPE floating-point extension */
522 PPC_SPEFPU = 0x0000000004000000ULL,
524 /* Optional memory control instructions */
525 PPC_MEM_TLBIA = 0x0000000010000000ULL,
526 PPC_MEM_TLBIE = 0x0000000020000000ULL,
527 PPC_MEM_TLBSYNC = 0x0000000040000000ULL,
528 /* sync instruction */
529 PPC_MEM_SYNC = 0x0000000080000000ULL,
530 /* eieio instruction */
531 PPC_MEM_EIEIO = 0x0000000100000000ULL,
533 /* Cache control instructions */
534 PPC_CACHE = 0x0000000200000000ULL,
535 /* icbi instruction */
536 PPC_CACHE_ICBI = 0x0000000400000000ULL,
537 /* dcbz instruction with fixed cache line size */
538 PPC_CACHE_DCBZ = 0x0000000800000000ULL,
539 /* dcbz instruction with tunable cache line size */
540 PPC_CACHE_DCBZT = 0x0000001000000000ULL,
541 /* dcba instruction */
542 PPC_CACHE_DCBA = 0x0000002000000000ULL,
543 /* Freescale cache locking instructions */
544 PPC_CACHE_LOCK = 0x0000004000000000ULL,
546 /* MMU related extensions */
547 /* external control instructions */
548 PPC_EXTERN = 0x0000010000000000ULL,
549 /* segment register access instructions */
550 PPC_SEGMENT = 0x0000020000000000ULL,
551 /* PowerPC 6xx TLB management instructions */
552 PPC_6xx_TLB = 0x0000040000000000ULL,
553 /* PowerPC 74xx TLB management instructions */
554 PPC_74xx_TLB = 0x0000080000000000ULL,
555 /* PowerPC 40x TLB management instructions */
556 PPC_40x_TLB = 0x0000100000000000ULL,
557 /* segment register access instructions for PowerPC 64 "bridge" */
558 PPC_SEGMENT_64B = 0x0000200000000000ULL,
560 PPC_SLBI = 0x0000400000000000ULL,
562 /* Embedded PowerPC dedicated instructions */
563 PPC_WRTEE = 0x0001000000000000ULL,
564 /* PowerPC 40x exception model */
565 PPC_40x_EXCP = 0x0002000000000000ULL,
566 /* PowerPC 405 Mac instructions */
567 PPC_405_MAC = 0x0004000000000000ULL,
568 /* PowerPC 440 specific instructions */
569 PPC_440_SPEC = 0x0008000000000000ULL,
570 /* BookE (embedded) PowerPC specification */
571 PPC_BOOKE = 0x0010000000000000ULL,
572 /* mfapidi instruction */
573 PPC_MFAPIDI = 0x0020000000000000ULL,
574 /* tlbiva instruction */
575 PPC_TLBIVA = 0x0040000000000000ULL,
576 /* tlbivax instruction */
577 PPC_TLBIVAX = 0x0080000000000000ULL,
578 /* PowerPC 4xx dedicated instructions */
579 PPC_4xx_COMMON = 0x0100000000000000ULL,
580 /* PowerPC 40x ibct instructions */
581 PPC_40x_ICBT = 0x0200000000000000ULL,
582 /* rfmci is not implemented in all BookE PowerPC */
583 PPC_RFMCI = 0x0400000000000000ULL,
584 /* rfdi instruction */
585 PPC_RFDI = 0x0800000000000000ULL,
587 PPC_DCR = 0x1000000000000000ULL,
588 /* DCR extended accesse */
589 PPC_DCRX = 0x2000000000000000ULL,
590 /* user-mode DCR access, implemented in PowerPC 460 */
591 PPC_DCRUX = 0x4000000000000000ULL,
594 /*****************************************************************************/
595 /* PowerPC instructions table */
596 #if HOST_LONG_BITS == 64
601 #if defined(__APPLE__)
602 #define OPCODES_SECTION \
603 __attribute__ ((section("__TEXT,__opcodes"), unused, aligned (OPC_ALIGN) ))
605 #define OPCODES_SECTION \
606 __attribute__ ((section(".opcodes"), unused, aligned (OPC_ALIGN) ))
609 #if defined(DO_PPC_STATISTICS)
610 #define GEN_OPCODE(name, op1, op2, op3, invl, _typ) \
611 OPCODES_SECTION opcode_t opc_##name = { \
619 .handler = &gen_##name, \
620 .oname = stringify(name), \
622 .oname = stringify(name), \
624 #define GEN_OPCODE2(name, onam, op1, op2, op3, invl, _typ) \
625 OPCODES_SECTION opcode_t opc_##name = { \
633 .handler = &gen_##name, \
639 #define GEN_OPCODE(name, op1, op2, op3, invl, _typ) \
640 OPCODES_SECTION opcode_t opc_##name = { \
648 .handler = &gen_##name, \
650 .oname = stringify(name), \
652 #define GEN_OPCODE2(name, onam, op1, op2, op3, invl, _typ) \
653 OPCODES_SECTION opcode_t opc_##name = { \
661 .handler = &gen_##name, \
667 #define GEN_OPCODE_MARK(name) \
668 OPCODES_SECTION opcode_t opc_##name = { \
674 .inval = 0x00000000, \
678 .oname = stringify(name), \
681 /* SPR load/store helpers */
682 static always_inline void gen_load_spr(TCGv t, int reg)
684 tcg_gen_ld_tl(t, cpu_env, offsetof(CPUState, spr[reg]));
687 static always_inline void gen_store_spr(int reg, TCGv t)
689 tcg_gen_st_tl(t, cpu_env, offsetof(CPUState, spr[reg]));
692 /* Start opcode list */
693 GEN_OPCODE_MARK(start);
695 /* Invalid instruction */
696 GEN_HANDLER(invalid, 0x00, 0x00, 0x00, 0xFFFFFFFF, PPC_NONE)
698 gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL);
701 static opc_handler_t invalid_handler = {
704 .handler = gen_invalid,
707 /*** Integer comparison ***/
709 static always_inline void gen_op_cmp(TCGv arg0, TCGv arg1, int s, int crf)
713 tcg_gen_trunc_tl_i32(cpu_crf[crf], cpu_xer);
714 tcg_gen_shri_i32(cpu_crf[crf], cpu_crf[crf], XER_SO);
715 tcg_gen_andi_i32(cpu_crf[crf], cpu_crf[crf], 1);
717 l1 = gen_new_label();
718 l2 = gen_new_label();
719 l3 = gen_new_label();
721 tcg_gen_brcond_tl(TCG_COND_LT, arg0, arg1, l1);
722 tcg_gen_brcond_tl(TCG_COND_GT, arg0, arg1, l2);
724 tcg_gen_brcond_tl(TCG_COND_LTU, arg0, arg1, l1);
725 tcg_gen_brcond_tl(TCG_COND_GTU, arg0, arg1, l2);
727 tcg_gen_ori_i32(cpu_crf[crf], cpu_crf[crf], 1 << CRF_EQ);
730 tcg_gen_ori_i32(cpu_crf[crf], cpu_crf[crf], 1 << CRF_LT);
733 tcg_gen_ori_i32(cpu_crf[crf], cpu_crf[crf], 1 << CRF_GT);
737 static always_inline void gen_op_cmpi(TCGv arg0, target_ulong arg1, int s, int crf)
739 TCGv t0 = tcg_const_local_tl(arg1);
740 gen_op_cmp(arg0, t0, s, crf);
744 #if defined(TARGET_PPC64)
745 static always_inline void gen_op_cmp32(TCGv arg0, TCGv arg1, int s, int crf)
748 t0 = tcg_temp_local_new();
749 t1 = tcg_temp_local_new();
751 tcg_gen_ext32s_tl(t0, arg0);
752 tcg_gen_ext32s_tl(t1, arg1);
754 tcg_gen_ext32u_tl(t0, arg0);
755 tcg_gen_ext32u_tl(t1, arg1);
757 gen_op_cmp(t0, t1, s, crf);
762 static always_inline void gen_op_cmpi32(TCGv arg0, target_ulong arg1, int s, int crf)
764 TCGv t0 = tcg_const_local_tl(arg1);
765 gen_op_cmp32(arg0, t0, s, crf);
770 static always_inline void gen_set_Rc0 (DisasContext *ctx, TCGv reg)
772 #if defined(TARGET_PPC64)
774 gen_op_cmpi32(reg, 0, 1, 0);
777 gen_op_cmpi(reg, 0, 1, 0);
781 GEN_HANDLER(cmp, 0x1F, 0x00, 0x00, 0x00400000, PPC_INTEGER)
783 #if defined(TARGET_PPC64)
784 if (!(ctx->sf_mode && (ctx->opcode & 0x00200000)))
785 gen_op_cmp32(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rB(ctx->opcode)],
786 1, crfD(ctx->opcode));
789 gen_op_cmp(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rB(ctx->opcode)],
790 1, crfD(ctx->opcode));
794 GEN_HANDLER(cmpi, 0x0B, 0xFF, 0xFF, 0x00400000, PPC_INTEGER)
796 #if defined(TARGET_PPC64)
797 if (!(ctx->sf_mode && (ctx->opcode & 0x00200000)))
798 gen_op_cmpi32(cpu_gpr[rA(ctx->opcode)], SIMM(ctx->opcode),
799 1, crfD(ctx->opcode));
802 gen_op_cmpi(cpu_gpr[rA(ctx->opcode)], SIMM(ctx->opcode),
803 1, crfD(ctx->opcode));
807 GEN_HANDLER(cmpl, 0x1F, 0x00, 0x01, 0x00400000, PPC_INTEGER)
809 #if defined(TARGET_PPC64)
810 if (!(ctx->sf_mode && (ctx->opcode & 0x00200000)))
811 gen_op_cmp32(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rB(ctx->opcode)],
812 0, crfD(ctx->opcode));
815 gen_op_cmp(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rB(ctx->opcode)],
816 0, crfD(ctx->opcode));
820 GEN_HANDLER(cmpli, 0x0A, 0xFF, 0xFF, 0x00400000, PPC_INTEGER)
822 #if defined(TARGET_PPC64)
823 if (!(ctx->sf_mode && (ctx->opcode & 0x00200000)))
824 gen_op_cmpi32(cpu_gpr[rA(ctx->opcode)], UIMM(ctx->opcode),
825 0, crfD(ctx->opcode));
828 gen_op_cmpi(cpu_gpr[rA(ctx->opcode)], UIMM(ctx->opcode),
829 0, crfD(ctx->opcode));
832 /* isel (PowerPC 2.03 specification) */
833 GEN_HANDLER(isel, 0x1F, 0x0F, 0xFF, 0x00000001, PPC_ISEL)
836 uint32_t bi = rC(ctx->opcode);
840 l1 = gen_new_label();
841 l2 = gen_new_label();
843 mask = 1 << (3 - (bi & 0x03));
844 t0 = tcg_temp_new_i32();
845 tcg_gen_andi_i32(t0, cpu_crf[bi >> 2], mask);
846 tcg_gen_brcondi_i32(TCG_COND_EQ, t0, 0, l1);
847 if (rA(ctx->opcode) == 0)
848 tcg_gen_movi_tl(cpu_gpr[rD(ctx->opcode)], 0);
850 tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)]);
853 tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rB(ctx->opcode)]);
855 tcg_temp_free_i32(t0);
858 /*** Integer arithmetic ***/
860 static always_inline void gen_op_arith_compute_ov(DisasContext *ctx, TCGv arg0, TCGv arg1, TCGv arg2, int sub)
865 l1 = gen_new_label();
866 /* Start with XER OV disabled, the most likely case */
867 tcg_gen_andi_tl(cpu_xer, cpu_xer, ~(1 << XER_OV));
868 t0 = tcg_temp_local_new();
869 tcg_gen_xor_tl(t0, arg0, arg1);
870 #if defined(TARGET_PPC64)
872 tcg_gen_ext32s_tl(t0, t0);
875 tcg_gen_brcondi_tl(TCG_COND_LT, t0, 0, l1);
877 tcg_gen_brcondi_tl(TCG_COND_GE, t0, 0, l1);
878 tcg_gen_xor_tl(t0, arg1, arg2);
879 #if defined(TARGET_PPC64)
881 tcg_gen_ext32s_tl(t0, t0);
884 tcg_gen_brcondi_tl(TCG_COND_GE, t0, 0, l1);
886 tcg_gen_brcondi_tl(TCG_COND_LT, t0, 0, l1);
887 tcg_gen_ori_tl(cpu_xer, cpu_xer, (1 << XER_OV) | (1 << XER_SO));
892 static always_inline void gen_op_arith_compute_ca(DisasContext *ctx, TCGv arg1, TCGv arg2, int sub)
894 int l1 = gen_new_label();
896 #if defined(TARGET_PPC64)
897 if (!(ctx->sf_mode)) {
902 tcg_gen_ext32u_tl(t0, arg1);
903 tcg_gen_ext32u_tl(t1, arg2);
905 tcg_gen_brcond_tl(TCG_COND_GTU, t0, t1, l1);
907 tcg_gen_brcond_tl(TCG_COND_GEU, t0, t1, l1);
909 tcg_gen_ori_tl(cpu_xer, cpu_xer, 1 << XER_CA);
917 tcg_gen_brcond_tl(TCG_COND_GTU, arg1, arg2, l1);
919 tcg_gen_brcond_tl(TCG_COND_GEU, arg1, arg2, l1);
921 tcg_gen_ori_tl(cpu_xer, cpu_xer, 1 << XER_CA);
926 /* Common add function */
927 static always_inline void gen_op_arith_add(DisasContext *ctx, TCGv ret, TCGv arg1, TCGv arg2,
928 int add_ca, int compute_ca, int compute_ov)
932 if ((!compute_ca && !compute_ov) ||
933 (!TCGV_EQUAL(ret,arg1) && !TCGV_EQUAL(ret, arg2))) {
936 t0 = tcg_temp_local_new();
940 t1 = tcg_temp_local_new();
941 tcg_gen_andi_tl(t1, cpu_xer, (1 << XER_CA));
942 tcg_gen_shri_tl(t1, t1, XER_CA);
945 if (compute_ca && compute_ov) {
946 /* Start with XER CA and OV disabled, the most likely case */
947 tcg_gen_andi_tl(cpu_xer, cpu_xer, ~((1 << XER_CA) | (1 << XER_OV)));
948 } else if (compute_ca) {
949 /* Start with XER CA disabled, the most likely case */
950 tcg_gen_andi_tl(cpu_xer, cpu_xer, ~(1 << XER_CA));
951 } else if (compute_ov) {
952 /* Start with XER OV disabled, the most likely case */
953 tcg_gen_andi_tl(cpu_xer, cpu_xer, ~(1 << XER_OV));
956 tcg_gen_add_tl(t0, arg1, arg2);
959 gen_op_arith_compute_ca(ctx, t0, arg1, 0);
962 tcg_gen_add_tl(t0, t0, t1);
963 gen_op_arith_compute_ca(ctx, t0, t1, 0);
967 gen_op_arith_compute_ov(ctx, t0, arg1, arg2, 0);
970 if (unlikely(Rc(ctx->opcode) != 0))
971 gen_set_Rc0(ctx, t0);
973 if (!TCGV_EQUAL(t0, ret)) {
974 tcg_gen_mov_tl(ret, t0);
978 /* Add functions with two operands */
979 #define GEN_INT_ARITH_ADD(name, opc3, add_ca, compute_ca, compute_ov) \
980 GEN_HANDLER(name, 0x1F, 0x0A, opc3, 0x00000000, PPC_INTEGER) \
982 gen_op_arith_add(ctx, cpu_gpr[rD(ctx->opcode)], \
983 cpu_gpr[rA(ctx->opcode)], cpu_gpr[rB(ctx->opcode)], \
984 add_ca, compute_ca, compute_ov); \
986 /* Add functions with one operand and one immediate */
987 #define GEN_INT_ARITH_ADD_CONST(name, opc3, const_val, \
988 add_ca, compute_ca, compute_ov) \
989 GEN_HANDLER(name, 0x1F, 0x0A, opc3, 0x0000F800, PPC_INTEGER) \
991 TCGv t0 = tcg_const_local_tl(const_val); \
992 gen_op_arith_add(ctx, cpu_gpr[rD(ctx->opcode)], \
993 cpu_gpr[rA(ctx->opcode)], t0, \
994 add_ca, compute_ca, compute_ov); \
998 /* add add. addo addo. */
999 GEN_INT_ARITH_ADD(add, 0x08, 0, 0, 0)
1000 GEN_INT_ARITH_ADD(addo, 0x18, 0, 0, 1)
1001 /* addc addc. addco addco. */
1002 GEN_INT_ARITH_ADD(addc, 0x00, 0, 1, 0)
1003 GEN_INT_ARITH_ADD(addco, 0x10, 0, 1, 1)
1004 /* adde adde. addeo addeo. */
1005 GEN_INT_ARITH_ADD(adde, 0x04, 1, 1, 0)
1006 GEN_INT_ARITH_ADD(addeo, 0x14, 1, 1, 1)
1007 /* addme addme. addmeo addmeo. */
1008 GEN_INT_ARITH_ADD_CONST(addme, 0x07, -1LL, 1, 1, 0)
1009 GEN_INT_ARITH_ADD_CONST(addmeo, 0x17, -1LL, 1, 1, 1)
1010 /* addze addze. addzeo addzeo.*/
1011 GEN_INT_ARITH_ADD_CONST(addze, 0x06, 0, 1, 1, 0)
1012 GEN_INT_ARITH_ADD_CONST(addzeo, 0x16, 0, 1, 1, 1)
1014 GEN_HANDLER(addi, 0x0E, 0xFF, 0xFF, 0x00000000, PPC_INTEGER)
1016 target_long simm = SIMM(ctx->opcode);
1018 if (rA(ctx->opcode) == 0) {
1020 tcg_gen_movi_tl(cpu_gpr[rD(ctx->opcode)], simm);
1022 tcg_gen_addi_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)], simm);
1026 static always_inline void gen_op_addic (DisasContext *ctx, TCGv ret, TCGv arg1,
1029 target_long simm = SIMM(ctx->opcode);
1031 /* Start with XER CA and OV disabled, the most likely case */
1032 tcg_gen_andi_tl(cpu_xer, cpu_xer, ~(1 << XER_CA));
1034 if (likely(simm != 0)) {
1035 TCGv t0 = tcg_temp_local_new();
1036 tcg_gen_addi_tl(t0, arg1, simm);
1037 gen_op_arith_compute_ca(ctx, t0, arg1, 0);
1038 tcg_gen_mov_tl(ret, t0);
1041 tcg_gen_mov_tl(ret, arg1);
1044 gen_set_Rc0(ctx, ret);
1047 GEN_HANDLER(addic, 0x0C, 0xFF, 0xFF, 0x00000000, PPC_INTEGER)
1049 gen_op_addic(ctx, cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)], 0);
1051 GEN_HANDLER2(addic_, "addic.", 0x0D, 0xFF, 0xFF, 0x00000000, PPC_INTEGER)
1053 gen_op_addic(ctx, cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)], 1);
1056 GEN_HANDLER(addis, 0x0F, 0xFF, 0xFF, 0x00000000, PPC_INTEGER)
1058 target_long simm = SIMM(ctx->opcode);
1060 if (rA(ctx->opcode) == 0) {
1062 tcg_gen_movi_tl(cpu_gpr[rD(ctx->opcode)], simm << 16);
1064 tcg_gen_addi_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)], simm << 16);
1068 static always_inline void gen_op_arith_divw (DisasContext *ctx, TCGv ret, TCGv arg1, TCGv arg2,
1069 int sign, int compute_ov)
1071 int l1 = gen_new_label();
1072 int l2 = gen_new_label();
1073 TCGv_i32 t0 = tcg_temp_local_new_i32();
1074 TCGv_i32 t1 = tcg_temp_local_new_i32();
1076 tcg_gen_trunc_tl_i32(t0, arg1);
1077 tcg_gen_trunc_tl_i32(t1, arg2);
1078 tcg_gen_brcondi_i32(TCG_COND_EQ, t1, 0, l1);
1080 int l3 = gen_new_label();
1081 tcg_gen_brcondi_i32(TCG_COND_NE, t1, -1, l3);
1082 tcg_gen_brcondi_i32(TCG_COND_EQ, t0, INT32_MIN, l1);
1084 tcg_gen_div_i32(t0, t0, t1);
1086 tcg_gen_divu_i32(t0, t0, t1);
1089 tcg_gen_andi_tl(cpu_xer, cpu_xer, ~(1 << XER_OV));
1094 tcg_gen_sari_i32(t0, t0, 31);
1096 tcg_gen_movi_i32(t0, 0);
1099 tcg_gen_ori_tl(cpu_xer, cpu_xer, (1 << XER_OV) | (1 << XER_SO));
1102 tcg_gen_extu_i32_tl(ret, t0);
1103 tcg_temp_free_i32(t0);
1104 tcg_temp_free_i32(t1);
1105 if (unlikely(Rc(ctx->opcode) != 0))
1106 gen_set_Rc0(ctx, ret);
1109 #define GEN_INT_ARITH_DIVW(name, opc3, sign, compute_ov) \
1110 GEN_HANDLER(name, 0x1F, 0x0B, opc3, 0x00000000, PPC_INTEGER) \
1112 gen_op_arith_divw(ctx, cpu_gpr[rD(ctx->opcode)], \
1113 cpu_gpr[rA(ctx->opcode)], cpu_gpr[rB(ctx->opcode)], \
1114 sign, compute_ov); \
1116 /* divwu divwu. divwuo divwuo. */
1117 GEN_INT_ARITH_DIVW(divwu, 0x0E, 0, 0);
1118 GEN_INT_ARITH_DIVW(divwuo, 0x1E, 0, 1);
1119 /* divw divw. divwo divwo. */
1120 GEN_INT_ARITH_DIVW(divw, 0x0F, 1, 0);
1121 GEN_INT_ARITH_DIVW(divwo, 0x1F, 1, 1);
1122 #if defined(TARGET_PPC64)
1123 static always_inline void gen_op_arith_divd (DisasContext *ctx, TCGv ret, TCGv arg1, TCGv arg2,
1124 int sign, int compute_ov)
1126 int l1 = gen_new_label();
1127 int l2 = gen_new_label();
1129 tcg_gen_brcondi_i64(TCG_COND_EQ, arg2, 0, l1);
1131 int l3 = gen_new_label();
1132 tcg_gen_brcondi_i64(TCG_COND_NE, arg2, -1, l3);
1133 tcg_gen_brcondi_i64(TCG_COND_EQ, arg1, INT64_MIN, l1);
1135 tcg_gen_div_i64(ret, arg1, arg2);
1137 tcg_gen_divu_i64(ret, arg1, arg2);
1140 tcg_gen_andi_tl(cpu_xer, cpu_xer, ~(1 << XER_OV));
1145 tcg_gen_sari_i64(ret, arg1, 63);
1147 tcg_gen_movi_i64(ret, 0);
1150 tcg_gen_ori_tl(cpu_xer, cpu_xer, (1 << XER_OV) | (1 << XER_SO));
1153 if (unlikely(Rc(ctx->opcode) != 0))
1154 gen_set_Rc0(ctx, ret);
1156 #define GEN_INT_ARITH_DIVD(name, opc3, sign, compute_ov) \
1157 GEN_HANDLER(name, 0x1F, 0x09, opc3, 0x00000000, PPC_64B) \
1159 gen_op_arith_divd(ctx, cpu_gpr[rD(ctx->opcode)], \
1160 cpu_gpr[rA(ctx->opcode)], cpu_gpr[rB(ctx->opcode)], \
1161 sign, compute_ov); \
1163 /* divwu divwu. divwuo divwuo. */
1164 GEN_INT_ARITH_DIVD(divdu, 0x0E, 0, 0);
1165 GEN_INT_ARITH_DIVD(divduo, 0x1E, 0, 1);
1166 /* divw divw. divwo divwo. */
1167 GEN_INT_ARITH_DIVD(divd, 0x0F, 1, 0);
1168 GEN_INT_ARITH_DIVD(divdo, 0x1F, 1, 1);
1172 GEN_HANDLER(mulhw, 0x1F, 0x0B, 0x02, 0x00000400, PPC_INTEGER)
1176 t0 = tcg_temp_new_i64();
1177 t1 = tcg_temp_new_i64();
1178 #if defined(TARGET_PPC64)
1179 tcg_gen_ext32s_tl(t0, cpu_gpr[rA(ctx->opcode)]);
1180 tcg_gen_ext32s_tl(t1, cpu_gpr[rB(ctx->opcode)]);
1181 tcg_gen_mul_i64(t0, t0, t1);
1182 tcg_gen_shri_i64(cpu_gpr[rD(ctx->opcode)], t0, 32);
1184 tcg_gen_ext_tl_i64(t0, cpu_gpr[rA(ctx->opcode)]);
1185 tcg_gen_ext_tl_i64(t1, cpu_gpr[rB(ctx->opcode)]);
1186 tcg_gen_mul_i64(t0, t0, t1);
1187 tcg_gen_shri_i64(t0, t0, 32);
1188 tcg_gen_trunc_i64_tl(cpu_gpr[rD(ctx->opcode)], t0);
1190 tcg_temp_free_i64(t0);
1191 tcg_temp_free_i64(t1);
1192 if (unlikely(Rc(ctx->opcode) != 0))
1193 gen_set_Rc0(ctx, cpu_gpr[rD(ctx->opcode)]);
1195 /* mulhwu mulhwu. */
1196 GEN_HANDLER(mulhwu, 0x1F, 0x0B, 0x00, 0x00000400, PPC_INTEGER)
1200 t0 = tcg_temp_new_i64();
1201 t1 = tcg_temp_new_i64();
1202 #if defined(TARGET_PPC64)
1203 tcg_gen_ext32u_i64(t0, cpu_gpr[rA(ctx->opcode)]);
1204 tcg_gen_ext32u_i64(t1, cpu_gpr[rB(ctx->opcode)]);
1205 tcg_gen_mul_i64(t0, t0, t1);
1206 tcg_gen_shri_i64(cpu_gpr[rD(ctx->opcode)], t0, 32);
1208 tcg_gen_extu_tl_i64(t0, cpu_gpr[rA(ctx->opcode)]);
1209 tcg_gen_extu_tl_i64(t1, cpu_gpr[rB(ctx->opcode)]);
1210 tcg_gen_mul_i64(t0, t0, t1);
1211 tcg_gen_shri_i64(t0, t0, 32);
1212 tcg_gen_trunc_i64_tl(cpu_gpr[rD(ctx->opcode)], t0);
1214 tcg_temp_free_i64(t0);
1215 tcg_temp_free_i64(t1);
1216 if (unlikely(Rc(ctx->opcode) != 0))
1217 gen_set_Rc0(ctx, cpu_gpr[rD(ctx->opcode)]);
1220 GEN_HANDLER(mullw, 0x1F, 0x0B, 0x07, 0x00000000, PPC_INTEGER)
1222 tcg_gen_mul_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)],
1223 cpu_gpr[rB(ctx->opcode)]);
1224 tcg_gen_ext32s_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rD(ctx->opcode)]);
1225 if (unlikely(Rc(ctx->opcode) != 0))
1226 gen_set_Rc0(ctx, cpu_gpr[rD(ctx->opcode)]);
1228 /* mullwo mullwo. */
1229 GEN_HANDLER(mullwo, 0x1F, 0x0B, 0x17, 0x00000000, PPC_INTEGER)
1234 t0 = tcg_temp_new_i64();
1235 t1 = tcg_temp_new_i64();
1236 l1 = gen_new_label();
1237 /* Start with XER OV disabled, the most likely case */
1238 tcg_gen_andi_tl(cpu_xer, cpu_xer, ~(1 << XER_OV));
1239 #if defined(TARGET_PPC64)
1240 tcg_gen_ext32s_i64(t0, cpu_gpr[rA(ctx->opcode)]);
1241 tcg_gen_ext32s_i64(t1, cpu_gpr[rB(ctx->opcode)]);
1243 tcg_gen_ext_tl_i64(t0, cpu_gpr[rA(ctx->opcode)]);
1244 tcg_gen_ext_tl_i64(t1, cpu_gpr[rB(ctx->opcode)]);
1246 tcg_gen_mul_i64(t0, t0, t1);
1247 #if defined(TARGET_PPC64)
1248 tcg_gen_ext32s_i64(cpu_gpr[rD(ctx->opcode)], t0);
1249 tcg_gen_brcond_i64(TCG_COND_EQ, t0, cpu_gpr[rD(ctx->opcode)], l1);
1251 tcg_gen_trunc_i64_tl(cpu_gpr[rD(ctx->opcode)], t0);
1252 tcg_gen_ext32s_i64(t1, t0);
1253 tcg_gen_brcond_i64(TCG_COND_EQ, t0, t1, l1);
1255 tcg_gen_ori_tl(cpu_xer, cpu_xer, (1 << XER_OV) | (1 << XER_SO));
1257 tcg_temp_free_i64(t0);
1258 tcg_temp_free_i64(t1);
1259 if (unlikely(Rc(ctx->opcode) != 0))
1260 gen_set_Rc0(ctx, cpu_gpr[rD(ctx->opcode)]);
1263 GEN_HANDLER(mulli, 0x07, 0xFF, 0xFF, 0x00000000, PPC_INTEGER)
1265 tcg_gen_muli_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)],
1268 #if defined(TARGET_PPC64)
1269 #define GEN_INT_ARITH_MUL_HELPER(name, opc3) \
1270 GEN_HANDLER(name, 0x1F, 0x09, opc3, 0x00000000, PPC_64B) \
1272 gen_helper_##name (cpu_gpr[rD(ctx->opcode)], \
1273 cpu_gpr[rA(ctx->opcode)], cpu_gpr[rB(ctx->opcode)]); \
1274 if (unlikely(Rc(ctx->opcode) != 0)) \
1275 gen_set_Rc0(ctx, cpu_gpr[rD(ctx->opcode)]); \
1278 GEN_INT_ARITH_MUL_HELPER(mulhdu, 0x00);
1279 /* mulhdu mulhdu. */
1280 GEN_INT_ARITH_MUL_HELPER(mulhd, 0x02);
1282 GEN_HANDLER(mulld, 0x1F, 0x09, 0x07, 0x00000000, PPC_64B)
1284 tcg_gen_mul_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)],
1285 cpu_gpr[rB(ctx->opcode)]);
1286 if (unlikely(Rc(ctx->opcode) != 0))
1287 gen_set_Rc0(ctx, cpu_gpr[rD(ctx->opcode)]);
1289 /* mulldo mulldo. */
1290 GEN_INT_ARITH_MUL_HELPER(mulldo, 0x17);
1293 /* neg neg. nego nego. */
1294 static always_inline void gen_op_arith_neg (DisasContext *ctx, TCGv ret, TCGv arg1, int ov_check)
1296 int l1 = gen_new_label();
1297 int l2 = gen_new_label();
1298 TCGv t0 = tcg_temp_local_new();
1299 #if defined(TARGET_PPC64)
1301 tcg_gen_mov_tl(t0, arg1);
1302 tcg_gen_brcondi_tl(TCG_COND_EQ, t0, INT64_MIN, l1);
1306 tcg_gen_ext32s_tl(t0, arg1);
1307 tcg_gen_brcondi_tl(TCG_COND_EQ, t0, INT32_MIN, l1);
1309 tcg_gen_neg_tl(ret, arg1);
1311 tcg_gen_andi_tl(cpu_xer, cpu_xer, ~(1 << XER_OV));
1315 tcg_gen_mov_tl(ret, t0);
1317 tcg_gen_ori_tl(cpu_xer, cpu_xer, (1 << XER_OV) | (1 << XER_SO));
1321 if (unlikely(Rc(ctx->opcode) != 0))
1322 gen_set_Rc0(ctx, ret);
1324 GEN_HANDLER(neg, 0x1F, 0x08, 0x03, 0x0000F800, PPC_INTEGER)
1326 gen_op_arith_neg(ctx, cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)], 0);
1328 GEN_HANDLER(nego, 0x1F, 0x08, 0x13, 0x0000F800, PPC_INTEGER)
1330 gen_op_arith_neg(ctx, cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)], 1);
1333 /* Common subf function */
1334 static always_inline void gen_op_arith_subf(DisasContext *ctx, TCGv ret, TCGv arg1, TCGv arg2,
1335 int add_ca, int compute_ca, int compute_ov)
1339 if ((!compute_ca && !compute_ov) ||
1340 (!TCGV_EQUAL(ret, arg1) && !TCGV_EQUAL(ret, arg2))) {
1343 t0 = tcg_temp_local_new();
1347 t1 = tcg_temp_local_new();
1348 tcg_gen_andi_tl(t1, cpu_xer, (1 << XER_CA));
1349 tcg_gen_shri_tl(t1, t1, XER_CA);
1352 if (compute_ca && compute_ov) {
1353 /* Start with XER CA and OV disabled, the most likely case */
1354 tcg_gen_andi_tl(cpu_xer, cpu_xer, ~((1 << XER_CA) | (1 << XER_OV)));
1355 } else if (compute_ca) {
1356 /* Start with XER CA disabled, the most likely case */
1357 tcg_gen_andi_tl(cpu_xer, cpu_xer, ~(1 << XER_CA));
1358 } else if (compute_ov) {
1359 /* Start with XER OV disabled, the most likely case */
1360 tcg_gen_andi_tl(cpu_xer, cpu_xer, ~(1 << XER_OV));
1364 tcg_gen_not_tl(t0, arg1);
1365 tcg_gen_add_tl(t0, t0, arg2);
1366 gen_op_arith_compute_ca(ctx, t0, arg2, 0);
1367 tcg_gen_add_tl(t0, t0, t1);
1368 gen_op_arith_compute_ca(ctx, t0, t1, 0);
1371 tcg_gen_sub_tl(t0, arg2, arg1);
1373 gen_op_arith_compute_ca(ctx, t0, arg2, 1);
1377 gen_op_arith_compute_ov(ctx, t0, arg1, arg2, 1);
1380 if (unlikely(Rc(ctx->opcode) != 0))
1381 gen_set_Rc0(ctx, t0);
1383 if (!TCGV_EQUAL(t0, ret)) {
1384 tcg_gen_mov_tl(ret, t0);
1388 /* Sub functions with Two operands functions */
1389 #define GEN_INT_ARITH_SUBF(name, opc3, add_ca, compute_ca, compute_ov) \
1390 GEN_HANDLER(name, 0x1F, 0x08, opc3, 0x00000000, PPC_INTEGER) \
1392 gen_op_arith_subf(ctx, cpu_gpr[rD(ctx->opcode)], \
1393 cpu_gpr[rA(ctx->opcode)], cpu_gpr[rB(ctx->opcode)], \
1394 add_ca, compute_ca, compute_ov); \
1396 /* Sub functions with one operand and one immediate */
1397 #define GEN_INT_ARITH_SUBF_CONST(name, opc3, const_val, \
1398 add_ca, compute_ca, compute_ov) \
1399 GEN_HANDLER(name, 0x1F, 0x08, opc3, 0x0000F800, PPC_INTEGER) \
1401 TCGv t0 = tcg_const_local_tl(const_val); \
1402 gen_op_arith_subf(ctx, cpu_gpr[rD(ctx->opcode)], \
1403 cpu_gpr[rA(ctx->opcode)], t0, \
1404 add_ca, compute_ca, compute_ov); \
1405 tcg_temp_free(t0); \
1407 /* subf subf. subfo subfo. */
1408 GEN_INT_ARITH_SUBF(subf, 0x01, 0, 0, 0)
1409 GEN_INT_ARITH_SUBF(subfo, 0x11, 0, 0, 1)
1410 /* subfc subfc. subfco subfco. */
1411 GEN_INT_ARITH_SUBF(subfc, 0x00, 0, 1, 0)
1412 GEN_INT_ARITH_SUBF(subfco, 0x10, 0, 1, 1)
1413 /* subfe subfe. subfeo subfo. */
1414 GEN_INT_ARITH_SUBF(subfe, 0x04, 1, 1, 0)
1415 GEN_INT_ARITH_SUBF(subfeo, 0x14, 1, 1, 1)
1416 /* subfme subfme. subfmeo subfmeo. */
1417 GEN_INT_ARITH_SUBF_CONST(subfme, 0x07, -1LL, 1, 1, 0)
1418 GEN_INT_ARITH_SUBF_CONST(subfmeo, 0x17, -1LL, 1, 1, 1)
1419 /* subfze subfze. subfzeo subfzeo.*/
1420 GEN_INT_ARITH_SUBF_CONST(subfze, 0x06, 0, 1, 1, 0)
1421 GEN_INT_ARITH_SUBF_CONST(subfzeo, 0x16, 0, 1, 1, 1)
1423 GEN_HANDLER(subfic, 0x08, 0xFF, 0xFF, 0x00000000, PPC_INTEGER)
1425 /* Start with XER CA and OV disabled, the most likely case */
1426 tcg_gen_andi_tl(cpu_xer, cpu_xer, ~(1 << XER_CA));
1427 TCGv t0 = tcg_temp_local_new();
1428 TCGv t1 = tcg_const_local_tl(SIMM(ctx->opcode));
1429 tcg_gen_sub_tl(t0, t1, cpu_gpr[rA(ctx->opcode)]);
1430 gen_op_arith_compute_ca(ctx, t0, t1, 1);
1432 tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], t0);
1436 /*** Integer logical ***/
1437 #define GEN_LOGICAL2(name, tcg_op, opc, type) \
1438 GEN_HANDLER(name, 0x1F, 0x1C, opc, 0x00000000, type) \
1440 tcg_op(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)], \
1441 cpu_gpr[rB(ctx->opcode)]); \
1442 if (unlikely(Rc(ctx->opcode) != 0)) \
1443 gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]); \
1446 #define GEN_LOGICAL1(name, tcg_op, opc, type) \
1447 GEN_HANDLER(name, 0x1F, 0x1A, opc, 0x00000000, type) \
1449 tcg_op(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)]); \
1450 if (unlikely(Rc(ctx->opcode) != 0)) \
1451 gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]); \
1455 GEN_LOGICAL2(and, tcg_gen_and_tl, 0x00, PPC_INTEGER);
1457 GEN_LOGICAL2(andc, tcg_gen_andc_tl, 0x01, PPC_INTEGER);
1459 GEN_HANDLER2(andi_, "andi.", 0x1C, 0xFF, 0xFF, 0x00000000, PPC_INTEGER)
1461 tcg_gen_andi_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)], UIMM(ctx->opcode));
1462 gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
1465 GEN_HANDLER2(andis_, "andis.", 0x1D, 0xFF, 0xFF, 0x00000000, PPC_INTEGER)
1467 tcg_gen_andi_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)], UIMM(ctx->opcode) << 16);
1468 gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
1471 GEN_HANDLER(cntlzw, 0x1F, 0x1A, 0x00, 0x00000000, PPC_INTEGER)
1473 gen_helper_cntlzw(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)]);
1474 if (unlikely(Rc(ctx->opcode) != 0))
1475 gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
1478 GEN_LOGICAL2(eqv, tcg_gen_eqv_tl, 0x08, PPC_INTEGER);
1479 /* extsb & extsb. */
1480 GEN_LOGICAL1(extsb, tcg_gen_ext8s_tl, 0x1D, PPC_INTEGER);
1481 /* extsh & extsh. */
1482 GEN_LOGICAL1(extsh, tcg_gen_ext16s_tl, 0x1C, PPC_INTEGER);
1484 GEN_LOGICAL2(nand, tcg_gen_nand_tl, 0x0E, PPC_INTEGER);
1486 GEN_LOGICAL2(nor, tcg_gen_nor_tl, 0x03, PPC_INTEGER);
1488 GEN_HANDLER(or, 0x1F, 0x1C, 0x0D, 0x00000000, PPC_INTEGER)
1492 rs = rS(ctx->opcode);
1493 ra = rA(ctx->opcode);
1494 rb = rB(ctx->opcode);
1495 /* Optimisation for mr. ri case */
1496 if (rs != ra || rs != rb) {
1498 tcg_gen_or_tl(cpu_gpr[ra], cpu_gpr[rs], cpu_gpr[rb]);
1500 tcg_gen_mov_tl(cpu_gpr[ra], cpu_gpr[rs]);
1501 if (unlikely(Rc(ctx->opcode) != 0))
1502 gen_set_Rc0(ctx, cpu_gpr[ra]);
1503 } else if (unlikely(Rc(ctx->opcode) != 0)) {
1504 gen_set_Rc0(ctx, cpu_gpr[rs]);
1505 #if defined(TARGET_PPC64)
1511 /* Set process priority to low */
1515 /* Set process priority to medium-low */
1519 /* Set process priority to normal */
1522 #if !defined(CONFIG_USER_ONLY)
1524 if (ctx->mem_idx > 0) {
1525 /* Set process priority to very low */
1530 if (ctx->mem_idx > 0) {
1531 /* Set process priority to medium-hight */
1536 if (ctx->mem_idx > 0) {
1537 /* Set process priority to high */
1542 if (ctx->mem_idx > 1) {
1543 /* Set process priority to very high */
1553 TCGv t0 = tcg_temp_new();
1554 gen_load_spr(t0, SPR_PPR);
1555 tcg_gen_andi_tl(t0, t0, ~0x001C000000000000ULL);
1556 tcg_gen_ori_tl(t0, t0, ((uint64_t)prio) << 50);
1557 gen_store_spr(SPR_PPR, t0);
1564 GEN_LOGICAL2(orc, tcg_gen_orc_tl, 0x0C, PPC_INTEGER);
1566 GEN_HANDLER(xor, 0x1F, 0x1C, 0x09, 0x00000000, PPC_INTEGER)
1568 /* Optimisation for "set to zero" case */
1569 if (rS(ctx->opcode) != rB(ctx->opcode))
1570 tcg_gen_xor_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)], cpu_gpr[rB(ctx->opcode)]);
1572 tcg_gen_movi_tl(cpu_gpr[rA(ctx->opcode)], 0);
1573 if (unlikely(Rc(ctx->opcode) != 0))
1574 gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
1577 GEN_HANDLER(ori, 0x18, 0xFF, 0xFF, 0x00000000, PPC_INTEGER)
1579 target_ulong uimm = UIMM(ctx->opcode);
1581 if (rS(ctx->opcode) == rA(ctx->opcode) && uimm == 0) {
1583 /* XXX: should handle special NOPs for POWER series */
1586 tcg_gen_ori_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)], uimm);
1589 GEN_HANDLER(oris, 0x19, 0xFF, 0xFF, 0x00000000, PPC_INTEGER)
1591 target_ulong uimm = UIMM(ctx->opcode);
1593 if (rS(ctx->opcode) == rA(ctx->opcode) && uimm == 0) {
1597 tcg_gen_ori_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)], uimm << 16);
1600 GEN_HANDLER(xori, 0x1A, 0xFF, 0xFF, 0x00000000, PPC_INTEGER)
1602 target_ulong uimm = UIMM(ctx->opcode);
1604 if (rS(ctx->opcode) == rA(ctx->opcode) && uimm == 0) {
1608 tcg_gen_xori_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)], uimm);
1611 GEN_HANDLER(xoris, 0x1B, 0xFF, 0xFF, 0x00000000, PPC_INTEGER)
1613 target_ulong uimm = UIMM(ctx->opcode);
1615 if (rS(ctx->opcode) == rA(ctx->opcode) && uimm == 0) {
1619 tcg_gen_xori_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)], uimm << 16);
1621 /* popcntb : PowerPC 2.03 specification */
1622 GEN_HANDLER(popcntb, 0x1F, 0x03, 0x03, 0x0000F801, PPC_POPCNTB)
1624 #if defined(TARGET_PPC64)
1626 gen_helper_popcntb_64(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)]);
1629 gen_helper_popcntb(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)]);
1632 #if defined(TARGET_PPC64)
1633 /* extsw & extsw. */
1634 GEN_LOGICAL1(extsw, tcg_gen_ext32s_tl, 0x1E, PPC_64B);
1636 GEN_HANDLER(cntlzd, 0x1F, 0x1A, 0x01, 0x00000000, PPC_64B)
1638 gen_helper_cntlzd(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)]);
1639 if (unlikely(Rc(ctx->opcode) != 0))
1640 gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
1644 /*** Integer rotate ***/
1645 /* rlwimi & rlwimi. */
1646 GEN_HANDLER(rlwimi, 0x14, 0xFF, 0xFF, 0x00000000, PPC_INTEGER)
1648 uint32_t mb, me, sh;
1650 mb = MB(ctx->opcode);
1651 me = ME(ctx->opcode);
1652 sh = SH(ctx->opcode);
1653 if (likely(sh == 0 && mb == 0 && me == 31)) {
1654 tcg_gen_ext32u_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)]);
1658 TCGv t0 = tcg_temp_new();
1659 #if defined(TARGET_PPC64)
1660 TCGv_i32 t2 = tcg_temp_new_i32();
1661 tcg_gen_trunc_i64_i32(t2, cpu_gpr[rS(ctx->opcode)]);
1662 tcg_gen_rotli_i32(t2, t2, sh);
1663 tcg_gen_extu_i32_i64(t0, t2);
1664 tcg_temp_free_i32(t2);
1666 tcg_gen_rotli_i32(t0, cpu_gpr[rS(ctx->opcode)], sh);
1668 #if defined(TARGET_PPC64)
1672 mask = MASK(mb, me);
1673 t1 = tcg_temp_new();
1674 tcg_gen_andi_tl(t0, t0, mask);
1675 tcg_gen_andi_tl(t1, cpu_gpr[rA(ctx->opcode)], ~mask);
1676 tcg_gen_or_tl(cpu_gpr[rA(ctx->opcode)], t0, t1);
1680 if (unlikely(Rc(ctx->opcode) != 0))
1681 gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
1683 /* rlwinm & rlwinm. */
1684 GEN_HANDLER(rlwinm, 0x15, 0xFF, 0xFF, 0x00000000, PPC_INTEGER)
1686 uint32_t mb, me, sh;
1688 sh = SH(ctx->opcode);
1689 mb = MB(ctx->opcode);
1690 me = ME(ctx->opcode);
1692 if (likely(mb == 0 && me == (31 - sh))) {
1693 if (likely(sh == 0)) {
1694 tcg_gen_ext32u_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)]);
1696 TCGv t0 = tcg_temp_new();
1697 tcg_gen_ext32u_tl(t0, cpu_gpr[rS(ctx->opcode)]);
1698 tcg_gen_shli_tl(t0, t0, sh);
1699 tcg_gen_ext32u_tl(cpu_gpr[rA(ctx->opcode)], t0);
1702 } else if (likely(sh != 0 && me == 31 && sh == (32 - mb))) {
1703 TCGv t0 = tcg_temp_new();
1704 tcg_gen_ext32u_tl(t0, cpu_gpr[rS(ctx->opcode)]);
1705 tcg_gen_shri_tl(t0, t0, mb);
1706 tcg_gen_ext32u_tl(cpu_gpr[rA(ctx->opcode)], t0);
1709 TCGv t0 = tcg_temp_new();
1710 #if defined(TARGET_PPC64)
1711 TCGv_i32 t1 = tcg_temp_new_i32();
1712 tcg_gen_trunc_i64_i32(t1, cpu_gpr[rS(ctx->opcode)]);
1713 tcg_gen_rotli_i32(t1, t1, sh);
1714 tcg_gen_extu_i32_i64(t0, t1);
1715 tcg_temp_free_i32(t1);
1717 tcg_gen_rotli_i32(t0, cpu_gpr[rS(ctx->opcode)], sh);
1719 #if defined(TARGET_PPC64)
1723 tcg_gen_andi_tl(cpu_gpr[rA(ctx->opcode)], t0, MASK(mb, me));
1726 if (unlikely(Rc(ctx->opcode) != 0))
1727 gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
1729 /* rlwnm & rlwnm. */
1730 GEN_HANDLER(rlwnm, 0x17, 0xFF, 0xFF, 0x00000000, PPC_INTEGER)
1734 #if defined(TARGET_PPC64)
1738 mb = MB(ctx->opcode);
1739 me = ME(ctx->opcode);
1740 t0 = tcg_temp_new();
1741 tcg_gen_andi_tl(t0, cpu_gpr[rB(ctx->opcode)], 0x1f);
1742 #if defined(TARGET_PPC64)
1743 t1 = tcg_temp_new_i32();
1744 t2 = tcg_temp_new_i32();
1745 tcg_gen_trunc_i64_i32(t1, cpu_gpr[rS(ctx->opcode)]);
1746 tcg_gen_trunc_i64_i32(t2, t0);
1747 tcg_gen_rotl_i32(t1, t1, t2);
1748 tcg_gen_extu_i32_i64(t0, t1);
1749 tcg_temp_free_i32(t1);
1750 tcg_temp_free_i32(t2);
1752 tcg_gen_rotl_i32(t0, cpu_gpr[rS(ctx->opcode)], t0);
1754 if (unlikely(mb != 0 || me != 31)) {
1755 #if defined(TARGET_PPC64)
1759 tcg_gen_andi_tl(cpu_gpr[rA(ctx->opcode)], t0, MASK(mb, me));
1761 tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], t0);
1764 if (unlikely(Rc(ctx->opcode) != 0))
1765 gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
1768 #if defined(TARGET_PPC64)
1769 #define GEN_PPC64_R2(name, opc1, opc2) \
1770 GEN_HANDLER2(name##0, stringify(name), opc1, opc2, 0xFF, 0x00000000, PPC_64B) \
1772 gen_##name(ctx, 0); \
1774 GEN_HANDLER2(name##1, stringify(name), opc1, opc2 | 0x10, 0xFF, 0x00000000, \
1777 gen_##name(ctx, 1); \
1779 #define GEN_PPC64_R4(name, opc1, opc2) \
1780 GEN_HANDLER2(name##0, stringify(name), opc1, opc2, 0xFF, 0x00000000, PPC_64B) \
1782 gen_##name(ctx, 0, 0); \
1784 GEN_HANDLER2(name##1, stringify(name), opc1, opc2 | 0x01, 0xFF, 0x00000000, \
1787 gen_##name(ctx, 0, 1); \
1789 GEN_HANDLER2(name##2, stringify(name), opc1, opc2 | 0x10, 0xFF, 0x00000000, \
1792 gen_##name(ctx, 1, 0); \
1794 GEN_HANDLER2(name##3, stringify(name), opc1, opc2 | 0x11, 0xFF, 0x00000000, \
1797 gen_##name(ctx, 1, 1); \
1800 static always_inline void gen_rldinm (DisasContext *ctx, uint32_t mb,
1801 uint32_t me, uint32_t sh)
1803 if (likely(sh != 0 && mb == 0 && me == (63 - sh))) {
1804 tcg_gen_shli_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)], sh);
1805 } else if (likely(sh != 0 && me == 63 && sh == (64 - mb))) {
1806 tcg_gen_shri_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)], mb);
1808 TCGv t0 = tcg_temp_new();
1809 tcg_gen_rotli_tl(t0, cpu_gpr[rS(ctx->opcode)], sh);
1810 if (likely(mb == 0 && me == 63)) {
1811 tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], t0);
1813 tcg_gen_andi_tl(cpu_gpr[rA(ctx->opcode)], t0, MASK(mb, me));
1817 if (unlikely(Rc(ctx->opcode) != 0))
1818 gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
1820 /* rldicl - rldicl. */
1821 static always_inline void gen_rldicl (DisasContext *ctx, int mbn, int shn)
1825 sh = SH(ctx->opcode) | (shn << 5);
1826 mb = MB(ctx->opcode) | (mbn << 5);
1827 gen_rldinm(ctx, mb, 63, sh);
1829 GEN_PPC64_R4(rldicl, 0x1E, 0x00);
1830 /* rldicr - rldicr. */
1831 static always_inline void gen_rldicr (DisasContext *ctx, int men, int shn)
1835 sh = SH(ctx->opcode) | (shn << 5);
1836 me = MB(ctx->opcode) | (men << 5);
1837 gen_rldinm(ctx, 0, me, sh);
1839 GEN_PPC64_R4(rldicr, 0x1E, 0x02);
1840 /* rldic - rldic. */
1841 static always_inline void gen_rldic (DisasContext *ctx, int mbn, int shn)
1845 sh = SH(ctx->opcode) | (shn << 5);
1846 mb = MB(ctx->opcode) | (mbn << 5);
1847 gen_rldinm(ctx, mb, 63 - sh, sh);
1849 GEN_PPC64_R4(rldic, 0x1E, 0x04);
1851 static always_inline void gen_rldnm (DisasContext *ctx, uint32_t mb,
1856 mb = MB(ctx->opcode);
1857 me = ME(ctx->opcode);
1858 t0 = tcg_temp_new();
1859 tcg_gen_andi_tl(t0, cpu_gpr[rB(ctx->opcode)], 0x3f);
1860 tcg_gen_rotl_tl(t0, cpu_gpr[rS(ctx->opcode)], t0);
1861 if (unlikely(mb != 0 || me != 63)) {
1862 tcg_gen_andi_tl(cpu_gpr[rA(ctx->opcode)], t0, MASK(mb, me));
1864 tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], t0);
1867 if (unlikely(Rc(ctx->opcode) != 0))
1868 gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
1871 /* rldcl - rldcl. */
1872 static always_inline void gen_rldcl (DisasContext *ctx, int mbn)
1876 mb = MB(ctx->opcode) | (mbn << 5);
1877 gen_rldnm(ctx, mb, 63);
1879 GEN_PPC64_R2(rldcl, 0x1E, 0x08);
1880 /* rldcr - rldcr. */
1881 static always_inline void gen_rldcr (DisasContext *ctx, int men)
1885 me = MB(ctx->opcode) | (men << 5);
1886 gen_rldnm(ctx, 0, me);
1888 GEN_PPC64_R2(rldcr, 0x1E, 0x09);
1889 /* rldimi - rldimi. */
1890 static always_inline void gen_rldimi (DisasContext *ctx, int mbn, int shn)
1892 uint32_t sh, mb, me;
1894 sh = SH(ctx->opcode) | (shn << 5);
1895 mb = MB(ctx->opcode) | (mbn << 5);
1897 if (unlikely(sh == 0 && mb == 0)) {
1898 tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)]);
1903 t0 = tcg_temp_new();
1904 tcg_gen_rotli_tl(t0, cpu_gpr[rS(ctx->opcode)], sh);
1905 t1 = tcg_temp_new();
1906 mask = MASK(mb, me);
1907 tcg_gen_andi_tl(t0, t0, mask);
1908 tcg_gen_andi_tl(t1, cpu_gpr[rA(ctx->opcode)], ~mask);
1909 tcg_gen_or_tl(cpu_gpr[rA(ctx->opcode)], t0, t1);
1913 if (unlikely(Rc(ctx->opcode) != 0))
1914 gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
1916 GEN_PPC64_R4(rldimi, 0x1E, 0x06);
1919 /*** Integer shift ***/
1921 GEN_HANDLER(slw, 0x1F, 0x18, 0x00, 0x00000000, PPC_INTEGER)
1925 l1 = gen_new_label();
1926 l2 = gen_new_label();
1928 t0 = tcg_temp_local_new();
1929 tcg_gen_andi_tl(t0, cpu_gpr[rB(ctx->opcode)], 0x3f);
1930 tcg_gen_brcondi_tl(TCG_COND_LT, t0, 0x20, l1);
1931 tcg_gen_movi_tl(cpu_gpr[rA(ctx->opcode)], 0);
1934 tcg_gen_shl_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)], t0);
1935 tcg_gen_ext32u_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rA(ctx->opcode)]);
1938 if (unlikely(Rc(ctx->opcode) != 0))
1939 gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
1942 GEN_HANDLER(sraw, 0x1F, 0x18, 0x18, 0x00000000, PPC_INTEGER)
1944 gen_helper_sraw(cpu_gpr[rA(ctx->opcode)],
1945 cpu_gpr[rS(ctx->opcode)], cpu_gpr[rB(ctx->opcode)]);
1946 if (unlikely(Rc(ctx->opcode) != 0))
1947 gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
1949 /* srawi & srawi. */
1950 GEN_HANDLER(srawi, 0x1F, 0x18, 0x19, 0x00000000, PPC_INTEGER)
1952 int sh = SH(ctx->opcode);
1956 l1 = gen_new_label();
1957 l2 = gen_new_label();
1958 t0 = tcg_temp_local_new();
1959 tcg_gen_ext32s_tl(t0, cpu_gpr[rS(ctx->opcode)]);
1960 tcg_gen_brcondi_tl(TCG_COND_GE, t0, 0, l1);
1961 tcg_gen_andi_tl(t0, cpu_gpr[rS(ctx->opcode)], (1ULL << sh) - 1);
1962 tcg_gen_brcondi_tl(TCG_COND_EQ, t0, 0, l1);
1963 tcg_gen_ori_tl(cpu_xer, cpu_xer, 1 << XER_CA);
1966 tcg_gen_andi_tl(cpu_xer, cpu_xer, ~(1 << XER_CA));
1968 tcg_gen_ext32s_tl(t0, cpu_gpr[rS(ctx->opcode)]);
1969 tcg_gen_sari_tl(cpu_gpr[rA(ctx->opcode)], t0, sh);
1972 tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)]);
1973 tcg_gen_andi_tl(cpu_xer, cpu_xer, ~(1 << XER_CA));
1975 if (unlikely(Rc(ctx->opcode) != 0))
1976 gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
1979 GEN_HANDLER(srw, 0x1F, 0x18, 0x10, 0x00000000, PPC_INTEGER)
1983 l1 = gen_new_label();
1984 l2 = gen_new_label();
1986 t0 = tcg_temp_local_new();
1987 tcg_gen_andi_tl(t0, cpu_gpr[rB(ctx->opcode)], 0x3f);
1988 tcg_gen_brcondi_tl(TCG_COND_LT, t0, 0x20, l1);
1989 tcg_gen_movi_tl(cpu_gpr[rA(ctx->opcode)], 0);
1992 t1 = tcg_temp_new();
1993 tcg_gen_ext32u_tl(t1, cpu_gpr[rS(ctx->opcode)]);
1994 tcg_gen_shr_tl(cpu_gpr[rA(ctx->opcode)], t1, t0);
1998 if (unlikely(Rc(ctx->opcode) != 0))
1999 gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
2001 #if defined(TARGET_PPC64)
2003 GEN_HANDLER(sld, 0x1F, 0x1B, 0x00, 0x00000000, PPC_64B)
2007 l1 = gen_new_label();
2008 l2 = gen_new_label();
2010 t0 = tcg_temp_local_new();
2011 tcg_gen_andi_tl(t0, cpu_gpr[rB(ctx->opcode)], 0x7f);
2012 tcg_gen_brcondi_tl(TCG_COND_LT, t0, 0x40, l1);
2013 tcg_gen_movi_tl(cpu_gpr[rA(ctx->opcode)], 0);
2016 tcg_gen_shl_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)], t0);
2019 if (unlikely(Rc(ctx->opcode) != 0))
2020 gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
2023 GEN_HANDLER(srad, 0x1F, 0x1A, 0x18, 0x00000000, PPC_64B)
2025 gen_helper_srad(cpu_gpr[rA(ctx->opcode)],
2026 cpu_gpr[rS(ctx->opcode)], cpu_gpr[rB(ctx->opcode)]);
2027 if (unlikely(Rc(ctx->opcode) != 0))
2028 gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
2030 /* sradi & sradi. */
2031 static always_inline void gen_sradi (DisasContext *ctx, int n)
2033 int sh = SH(ctx->opcode) + (n << 5);
2037 l1 = gen_new_label();
2038 l2 = gen_new_label();
2039 t0 = tcg_temp_local_new();
2040 tcg_gen_brcondi_tl(TCG_COND_GE, cpu_gpr[rS(ctx->opcode)], 0, l1);
2041 tcg_gen_andi_tl(t0, cpu_gpr[rS(ctx->opcode)], (1ULL << sh) - 1);
2042 tcg_gen_brcondi_tl(TCG_COND_EQ, t0, 0, l1);
2043 tcg_gen_ori_tl(cpu_xer, cpu_xer, 1 << XER_CA);
2046 tcg_gen_andi_tl(cpu_xer, cpu_xer, ~(1 << XER_CA));
2049 tcg_gen_sari_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)], sh);
2051 tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)]);
2052 tcg_gen_andi_tl(cpu_xer, cpu_xer, ~(1 << XER_CA));
2054 if (unlikely(Rc(ctx->opcode) != 0))
2055 gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
2057 GEN_HANDLER2(sradi0, "sradi", 0x1F, 0x1A, 0x19, 0x00000000, PPC_64B)
2061 GEN_HANDLER2(sradi1, "sradi", 0x1F, 0x1B, 0x19, 0x00000000, PPC_64B)
2066 GEN_HANDLER(srd, 0x1F, 0x1B, 0x10, 0x00000000, PPC_64B)
2070 l1 = gen_new_label();
2071 l2 = gen_new_label();
2073 t0 = tcg_temp_local_new();
2074 tcg_gen_andi_tl(t0, cpu_gpr[rB(ctx->opcode)], 0x7f);
2075 tcg_gen_brcondi_tl(TCG_COND_LT, t0, 0x40, l1);
2076 tcg_gen_movi_tl(cpu_gpr[rA(ctx->opcode)], 0);
2079 tcg_gen_shr_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)], t0);
2082 if (unlikely(Rc(ctx->opcode) != 0))
2083 gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
2087 /*** Floating-Point arithmetic ***/
2088 #define _GEN_FLOAT_ACB(name, op, op1, op2, isfloat, set_fprf, type) \
2089 GEN_HANDLER(f##name, op1, op2, 0xFF, 0x00000000, type) \
2091 if (unlikely(!ctx->fpu_enabled)) { \
2092 gen_exception(ctx, POWERPC_EXCP_FPU); \
2095 gen_reset_fpstatus(); \
2096 gen_helper_f##op(cpu_fpr[rD(ctx->opcode)], cpu_fpr[rA(ctx->opcode)], \
2097 cpu_fpr[rC(ctx->opcode)], cpu_fpr[rB(ctx->opcode)]); \
2099 gen_helper_frsp(cpu_fpr[rD(ctx->opcode)], cpu_fpr[rD(ctx->opcode)]); \
2101 gen_compute_fprf(cpu_fpr[rD(ctx->opcode)], set_fprf, \
2102 Rc(ctx->opcode) != 0); \
2105 #define GEN_FLOAT_ACB(name, op2, set_fprf, type) \
2106 _GEN_FLOAT_ACB(name, name, 0x3F, op2, 0, set_fprf, type); \
2107 _GEN_FLOAT_ACB(name##s, name, 0x3B, op2, 1, set_fprf, type);
2109 #define _GEN_FLOAT_AB(name, op, op1, op2, inval, isfloat, set_fprf, type) \
2110 GEN_HANDLER(f##name, op1, op2, 0xFF, inval, type) \
2112 if (unlikely(!ctx->fpu_enabled)) { \
2113 gen_exception(ctx, POWERPC_EXCP_FPU); \
2116 gen_reset_fpstatus(); \
2117 gen_helper_f##op(cpu_fpr[rD(ctx->opcode)], cpu_fpr[rA(ctx->opcode)], \
2118 cpu_fpr[rB(ctx->opcode)]); \
2120 gen_helper_frsp(cpu_fpr[rD(ctx->opcode)], cpu_fpr[rD(ctx->opcode)]); \
2122 gen_compute_fprf(cpu_fpr[rD(ctx->opcode)], \
2123 set_fprf, Rc(ctx->opcode) != 0); \
2125 #define GEN_FLOAT_AB(name, op2, inval, set_fprf, type) \
2126 _GEN_FLOAT_AB(name, name, 0x3F, op2, inval, 0, set_fprf, type); \
2127 _GEN_FLOAT_AB(name##s, name, 0x3B, op2, inval, 1, set_fprf, type);
2129 #define _GEN_FLOAT_AC(name, op, op1, op2, inval, isfloat, set_fprf, type) \
2130 GEN_HANDLER(f##name, op1, op2, 0xFF, inval, type) \
2132 if (unlikely(!ctx->fpu_enabled)) { \
2133 gen_exception(ctx, POWERPC_EXCP_FPU); \
2136 gen_reset_fpstatus(); \
2137 gen_helper_f##op(cpu_fpr[rD(ctx->opcode)], cpu_fpr[rA(ctx->opcode)], \
2138 cpu_fpr[rC(ctx->opcode)]); \
2140 gen_helper_frsp(cpu_fpr[rD(ctx->opcode)], cpu_fpr[rD(ctx->opcode)]); \
2142 gen_compute_fprf(cpu_fpr[rD(ctx->opcode)], \
2143 set_fprf, Rc(ctx->opcode) != 0); \
2145 #define GEN_FLOAT_AC(name, op2, inval, set_fprf, type) \
2146 _GEN_FLOAT_AC(name, name, 0x3F, op2, inval, 0, set_fprf, type); \
2147 _GEN_FLOAT_AC(name##s, name, 0x3B, op2, inval, 1, set_fprf, type);
2149 #define GEN_FLOAT_B(name, op2, op3, set_fprf, type) \
2150 GEN_HANDLER(f##name, 0x3F, op2, op3, 0x001F0000, type) \
2152 if (unlikely(!ctx->fpu_enabled)) { \
2153 gen_exception(ctx, POWERPC_EXCP_FPU); \
2156 gen_reset_fpstatus(); \
2157 gen_helper_f##name(cpu_fpr[rD(ctx->opcode)], cpu_fpr[rB(ctx->opcode)]); \
2158 gen_compute_fprf(cpu_fpr[rD(ctx->opcode)], \
2159 set_fprf, Rc(ctx->opcode) != 0); \
2162 #define GEN_FLOAT_BS(name, op1, op2, set_fprf, type) \
2163 GEN_HANDLER(f##name, op1, op2, 0xFF, 0x001F07C0, type) \
2165 if (unlikely(!ctx->fpu_enabled)) { \
2166 gen_exception(ctx, POWERPC_EXCP_FPU); \
2169 gen_reset_fpstatus(); \
2170 gen_helper_f##name(cpu_fpr[rD(ctx->opcode)], cpu_fpr[rB(ctx->opcode)]); \
2171 gen_compute_fprf(cpu_fpr[rD(ctx->opcode)], \
2172 set_fprf, Rc(ctx->opcode) != 0); \
2176 GEN_FLOAT_AB(add, 0x15, 0x000007C0, 1, PPC_FLOAT);
2178 GEN_FLOAT_AB(div, 0x12, 0x000007C0, 1, PPC_FLOAT);
2180 GEN_FLOAT_AC(mul, 0x19, 0x0000F800, 1, PPC_FLOAT);
2183 GEN_FLOAT_BS(re, 0x3F, 0x18, 1, PPC_FLOAT_EXT);
2186 GEN_FLOAT_BS(res, 0x3B, 0x18, 1, PPC_FLOAT_FRES);
2189 GEN_FLOAT_BS(rsqrte, 0x3F, 0x1A, 1, PPC_FLOAT_FRSQRTE);
2192 GEN_HANDLER(frsqrtes, 0x3B, 0x1A, 0xFF, 0x001F07C0, PPC_FLOAT_FRSQRTES)
2194 if (unlikely(!ctx->fpu_enabled)) {
2195 gen_exception(ctx, POWERPC_EXCP_FPU);
2198 gen_reset_fpstatus();
2199 gen_helper_frsqrte(cpu_fpr[rD(ctx->opcode)], cpu_fpr[rB(ctx->opcode)]);
2200 gen_helper_frsp(cpu_fpr[rD(ctx->opcode)], cpu_fpr[rD(ctx->opcode)]);
2201 gen_compute_fprf(cpu_fpr[rD(ctx->opcode)], 1, Rc(ctx->opcode) != 0);
2205 _GEN_FLOAT_ACB(sel, sel, 0x3F, 0x17, 0, 0, PPC_FLOAT_FSEL);
2207 GEN_FLOAT_AB(sub, 0x14, 0x000007C0, 1, PPC_FLOAT);
2210 GEN_HANDLER(fsqrt, 0x3F, 0x16, 0xFF, 0x001F07C0, PPC_FLOAT_FSQRT)
2212 if (unlikely(!ctx->fpu_enabled)) {
2213 gen_exception(ctx, POWERPC_EXCP_FPU);
2216 gen_reset_fpstatus();
2217 gen_helper_fsqrt(cpu_fpr[rD(ctx->opcode)], cpu_fpr[rB(ctx->opcode)]);
2218 gen_compute_fprf(cpu_fpr[rD(ctx->opcode)], 1, Rc(ctx->opcode) != 0);
2221 GEN_HANDLER(fsqrts, 0x3B, 0x16, 0xFF, 0x001F07C0, PPC_FLOAT_FSQRT)
2223 if (unlikely(!ctx->fpu_enabled)) {
2224 gen_exception(ctx, POWERPC_EXCP_FPU);
2227 gen_reset_fpstatus();
2228 gen_helper_fsqrt(cpu_fpr[rD(ctx->opcode)], cpu_fpr[rB(ctx->opcode)]);
2229 gen_helper_frsp(cpu_fpr[rD(ctx->opcode)], cpu_fpr[rD(ctx->opcode)]);
2230 gen_compute_fprf(cpu_fpr[rD(ctx->opcode)], 1, Rc(ctx->opcode) != 0);
2233 /*** Floating-Point multiply-and-add ***/
2234 /* fmadd - fmadds */
2235 GEN_FLOAT_ACB(madd, 0x1D, 1, PPC_FLOAT);
2236 /* fmsub - fmsubs */
2237 GEN_FLOAT_ACB(msub, 0x1C, 1, PPC_FLOAT);
2238 /* fnmadd - fnmadds */
2239 GEN_FLOAT_ACB(nmadd, 0x1F, 1, PPC_FLOAT);
2240 /* fnmsub - fnmsubs */
2241 GEN_FLOAT_ACB(nmsub, 0x1E, 1, PPC_FLOAT);
2243 /*** Floating-Point round & convert ***/
2245 GEN_FLOAT_B(ctiw, 0x0E, 0x00, 0, PPC_FLOAT);
2247 GEN_FLOAT_B(ctiwz, 0x0F, 0x00, 0, PPC_FLOAT);
2249 GEN_FLOAT_B(rsp, 0x0C, 0x00, 1, PPC_FLOAT);
2250 #if defined(TARGET_PPC64)
2252 GEN_FLOAT_B(cfid, 0x0E, 0x1A, 1, PPC_64B);
2254 GEN_FLOAT_B(ctid, 0x0E, 0x19, 0, PPC_64B);
2256 GEN_FLOAT_B(ctidz, 0x0F, 0x19, 0, PPC_64B);
2260 GEN_FLOAT_B(rin, 0x08, 0x0C, 1, PPC_FLOAT_EXT);
2262 GEN_FLOAT_B(riz, 0x08, 0x0D, 1, PPC_FLOAT_EXT);
2264 GEN_FLOAT_B(rip, 0x08, 0x0E, 1, PPC_FLOAT_EXT);
2266 GEN_FLOAT_B(rim, 0x08, 0x0F, 1, PPC_FLOAT_EXT);
2268 /*** Floating-Point compare ***/
2270 GEN_HANDLER(fcmpo, 0x3F, 0x00, 0x01, 0x00600001, PPC_FLOAT)
2272 if (unlikely(!ctx->fpu_enabled)) {
2273 gen_exception(ctx, POWERPC_EXCP_FPU);
2276 gen_reset_fpstatus();
2277 gen_helper_fcmpo(cpu_crf[crfD(ctx->opcode)],
2278 cpu_fpr[rA(ctx->opcode)], cpu_fpr[rB(ctx->opcode)]);
2279 gen_helper_float_check_status();
2283 GEN_HANDLER(fcmpu, 0x3F, 0x00, 0x00, 0x00600001, PPC_FLOAT)
2285 if (unlikely(!ctx->fpu_enabled)) {
2286 gen_exception(ctx, POWERPC_EXCP_FPU);
2289 gen_reset_fpstatus();
2290 gen_helper_fcmpu(cpu_crf[crfD(ctx->opcode)],
2291 cpu_fpr[rA(ctx->opcode)], cpu_fpr[rB(ctx->opcode)]);
2292 gen_helper_float_check_status();
2295 /*** Floating-point move ***/
2297 /* XXX: beware that fabs never checks for NaNs nor update FPSCR */
2298 GEN_FLOAT_B(abs, 0x08, 0x08, 0, PPC_FLOAT);
2301 /* XXX: beware that fmr never checks for NaNs nor update FPSCR */
2302 GEN_HANDLER(fmr, 0x3F, 0x08, 0x02, 0x001F0000, PPC_FLOAT)
2304 if (unlikely(!ctx->fpu_enabled)) {
2305 gen_exception(ctx, POWERPC_EXCP_FPU);
2308 tcg_gen_mov_i64(cpu_fpr[rD(ctx->opcode)], cpu_fpr[rB(ctx->opcode)]);
2309 gen_compute_fprf(cpu_fpr[rD(ctx->opcode)], 0, Rc(ctx->opcode) != 0);
2313 /* XXX: beware that fnabs never checks for NaNs nor update FPSCR */
2314 GEN_FLOAT_B(nabs, 0x08, 0x04, 0, PPC_FLOAT);
2316 /* XXX: beware that fneg never checks for NaNs nor update FPSCR */
2317 GEN_FLOAT_B(neg, 0x08, 0x01, 0, PPC_FLOAT);
2319 /*** Floating-Point status & ctrl register ***/
2321 GEN_HANDLER(mcrfs, 0x3F, 0x00, 0x02, 0x0063F801, PPC_FLOAT)
2325 if (unlikely(!ctx->fpu_enabled)) {
2326 gen_exception(ctx, POWERPC_EXCP_FPU);
2329 gen_optimize_fprf();
2330 bfa = 4 * (7 - crfS(ctx->opcode));
2331 tcg_gen_shri_i32(cpu_crf[crfD(ctx->opcode)], cpu_fpscr, bfa);
2332 tcg_gen_andi_i32(cpu_crf[crfD(ctx->opcode)], cpu_crf[crfD(ctx->opcode)], 0xf);
2333 tcg_gen_andi_i32(cpu_fpscr, cpu_fpscr, ~(0xF << bfa));
2337 GEN_HANDLER(mffs, 0x3F, 0x07, 0x12, 0x001FF800, PPC_FLOAT)
2339 if (unlikely(!ctx->fpu_enabled)) {
2340 gen_exception(ctx, POWERPC_EXCP_FPU);
2343 gen_optimize_fprf();
2344 gen_reset_fpstatus();
2345 tcg_gen_extu_i32_i64(cpu_fpr[rD(ctx->opcode)], cpu_fpscr);
2346 gen_compute_fprf(cpu_fpr[rD(ctx->opcode)], 0, Rc(ctx->opcode) != 0);
2350 GEN_HANDLER(mtfsb0, 0x3F, 0x06, 0x02, 0x001FF800, PPC_FLOAT)
2354 if (unlikely(!ctx->fpu_enabled)) {
2355 gen_exception(ctx, POWERPC_EXCP_FPU);
2358 crb = 31 - crbD(ctx->opcode);
2359 gen_optimize_fprf();
2360 gen_reset_fpstatus();
2361 if (likely(crb != FPSCR_FEX && crb != FPSCR_VX)) {
2362 TCGv_i32 t0 = tcg_const_i32(crb);
2363 gen_helper_fpscr_clrbit(t0);
2364 tcg_temp_free_i32(t0);
2366 if (unlikely(Rc(ctx->opcode) != 0)) {
2367 tcg_gen_shri_i32(cpu_crf[1], cpu_fpscr, FPSCR_OX);
2372 GEN_HANDLER(mtfsb1, 0x3F, 0x06, 0x01, 0x001FF800, PPC_FLOAT)
2376 if (unlikely(!ctx->fpu_enabled)) {
2377 gen_exception(ctx, POWERPC_EXCP_FPU);
2380 crb = 31 - crbD(ctx->opcode);
2381 gen_optimize_fprf();
2382 gen_reset_fpstatus();
2383 /* XXX: we pretend we can only do IEEE floating-point computations */
2384 if (likely(crb != FPSCR_FEX && crb != FPSCR_VX && crb != FPSCR_NI)) {
2385 TCGv_i32 t0 = tcg_const_i32(crb);
2386 gen_helper_fpscr_setbit(t0);
2387 tcg_temp_free_i32(t0);
2389 if (unlikely(Rc(ctx->opcode) != 0)) {
2390 tcg_gen_shri_i32(cpu_crf[1], cpu_fpscr, FPSCR_OX);
2392 /* We can raise a differed exception */
2393 gen_helper_float_check_status();
2397 GEN_HANDLER(mtfsf, 0x3F, 0x07, 0x16, 0x02010000, PPC_FLOAT)
2401 if (unlikely(!ctx->fpu_enabled)) {
2402 gen_exception(ctx, POWERPC_EXCP_FPU);
2405 gen_optimize_fprf();
2406 gen_reset_fpstatus();
2407 t0 = tcg_const_i32(FM(ctx->opcode));
2408 gen_helper_store_fpscr(cpu_fpr[rB(ctx->opcode)], t0);
2409 tcg_temp_free_i32(t0);
2410 if (unlikely(Rc(ctx->opcode) != 0)) {
2411 tcg_gen_shri_i32(cpu_crf[1], cpu_fpscr, FPSCR_OX);
2413 /* We can raise a differed exception */
2414 gen_helper_float_check_status();
2418 GEN_HANDLER(mtfsfi, 0x3F, 0x06, 0x04, 0x006f0800, PPC_FLOAT)
2424 if (unlikely(!ctx->fpu_enabled)) {
2425 gen_exception(ctx, POWERPC_EXCP_FPU);
2428 bf = crbD(ctx->opcode) >> 2;
2430 gen_optimize_fprf();
2431 gen_reset_fpstatus();
2432 t0 = tcg_const_i64(FPIMM(ctx->opcode) << (4 * sh));
2433 t1 = tcg_const_i32(1 << sh);
2434 gen_helper_store_fpscr(t0, t1);
2435 tcg_temp_free_i64(t0);
2436 tcg_temp_free_i32(t1);
2437 if (unlikely(Rc(ctx->opcode) != 0)) {
2438 tcg_gen_shri_i32(cpu_crf[1], cpu_fpscr, FPSCR_OX);
2440 /* We can raise a differed exception */
2441 gen_helper_float_check_status();
2444 /*** Addressing modes ***/
2445 /* Register indirect with immediate index : EA = (rA|0) + SIMM */
2446 static always_inline void gen_addr_imm_index (DisasContext *ctx, TCGv EA, target_long maskl)
2448 target_long simm = SIMM(ctx->opcode);
2451 if (rA(ctx->opcode) == 0) {
2452 #if defined(TARGET_PPC64)
2453 if (!ctx->sf_mode) {
2454 tcg_gen_movi_tl(EA, (uint32_t)simm);
2457 tcg_gen_movi_tl(EA, simm);
2458 } else if (likely(simm != 0)) {
2459 tcg_gen_addi_tl(EA, cpu_gpr[rA(ctx->opcode)], simm);
2460 #if defined(TARGET_PPC64)
2461 if (!ctx->sf_mode) {
2462 tcg_gen_ext32u_tl(EA, EA);
2466 #if defined(TARGET_PPC64)
2467 if (!ctx->sf_mode) {
2468 tcg_gen_ext32u_tl(EA, cpu_gpr[rA(ctx->opcode)]);
2471 tcg_gen_mov_tl(EA, cpu_gpr[rA(ctx->opcode)]);
2475 static always_inline void gen_addr_reg_index (DisasContext *ctx, TCGv EA)
2477 if (rA(ctx->opcode) == 0) {
2478 #if defined(TARGET_PPC64)
2479 if (!ctx->sf_mode) {
2480 tcg_gen_ext32u_tl(EA, cpu_gpr[rB(ctx->opcode)]);
2483 tcg_gen_mov_tl(EA, cpu_gpr[rB(ctx->opcode)]);
2485 tcg_gen_add_tl(EA, cpu_gpr[rA(ctx->opcode)], cpu_gpr[rB(ctx->opcode)]);
2486 #if defined(TARGET_PPC64)
2487 if (!ctx->sf_mode) {
2488 tcg_gen_ext32u_tl(EA, EA);
2494 static always_inline void gen_addr_register (DisasContext *ctx, TCGv EA)
2496 if (rA(ctx->opcode) == 0) {
2497 tcg_gen_movi_tl(EA, 0);
2499 #if defined(TARGET_PPC64)
2500 if (!ctx->sf_mode) {
2501 tcg_gen_ext32u_tl(EA, cpu_gpr[rA(ctx->opcode)]);
2504 tcg_gen_mov_tl(EA, cpu_gpr[rA(ctx->opcode)]);
2508 static always_inline void gen_addr_add (DisasContext *ctx, TCGv ret, TCGv arg1, target_long val)
2510 tcg_gen_addi_tl(ret, arg1, val);
2511 #if defined(TARGET_PPC64)
2512 if (!ctx->sf_mode) {
2513 tcg_gen_ext32u_tl(ret, ret);
2518 static always_inline void gen_check_align (DisasContext *ctx, TCGv EA, int mask)
2520 int l1 = gen_new_label();
2521 TCGv t0 = tcg_temp_new();
2523 /* NIP cannot be restored if the memory exception comes from an helper */
2524 gen_update_nip(ctx, ctx->nip - 4);
2525 tcg_gen_andi_tl(t0, EA, mask);
2526 tcg_gen_brcondi_tl(TCG_COND_EQ, t0, 0, l1);
2527 t1 = tcg_const_i32(POWERPC_EXCP_ALIGN);
2528 t2 = tcg_const_i32(0);
2529 gen_helper_raise_exception_err(t1, t2);
2530 tcg_temp_free_i32(t1);
2531 tcg_temp_free_i32(t2);
2536 /*** Integer load ***/
2537 static always_inline void gen_qemu_ld8u(DisasContext *ctx, TCGv arg1, TCGv arg2)
2539 tcg_gen_qemu_ld8u(arg1, arg2, ctx->mem_idx);
2542 static always_inline void gen_qemu_ld8s(DisasContext *ctx, TCGv arg1, TCGv arg2)
2544 tcg_gen_qemu_ld8s(arg1, arg2, ctx->mem_idx);
2547 static always_inline void gen_qemu_ld16u(DisasContext *ctx, TCGv arg1, TCGv arg2)
2549 tcg_gen_qemu_ld16u(arg1, arg2, ctx->mem_idx);
2550 if (unlikely(ctx->le_mode)) {
2551 #if defined(TARGET_PPC64)
2552 TCGv_i32 t0 = tcg_temp_new_i32();
2553 tcg_gen_trunc_tl_i32(t0, arg1);
2554 tcg_gen_bswap16_i32(t0, t0);
2555 tcg_gen_extu_i32_tl(arg1, t0);
2556 tcg_temp_free_i32(t0);
2558 tcg_gen_bswap16_i32(arg1, arg1);
2563 static always_inline void gen_qemu_ld16s(DisasContext *ctx, TCGv arg1, TCGv arg2)
2565 if (unlikely(ctx->le_mode)) {
2566 #if defined(TARGET_PPC64)
2568 tcg_gen_qemu_ld16u(arg1, arg2, ctx->mem_idx);
2569 t0 = tcg_temp_new_i32();
2570 tcg_gen_trunc_tl_i32(t0, arg1);
2571 tcg_gen_bswap16_i32(t0, t0);
2572 tcg_gen_extu_i32_tl(arg1, t0);
2573 tcg_gen_ext16s_tl(arg1, arg1);
2574 tcg_temp_free_i32(t0);
2576 tcg_gen_qemu_ld16u(arg1, arg2, ctx->mem_idx);
2577 tcg_gen_bswap16_i32(arg1, arg1);
2578 tcg_gen_ext16s_i32(arg1, arg1);
2581 tcg_gen_qemu_ld16s(arg1, arg2, ctx->mem_idx);
2585 static always_inline void gen_qemu_ld32u(DisasContext *ctx, TCGv arg1, TCGv arg2)
2587 tcg_gen_qemu_ld32u(arg1, arg2, ctx->mem_idx);
2588 if (unlikely(ctx->le_mode)) {
2589 #if defined(TARGET_PPC64)
2590 TCGv_i32 t0 = tcg_temp_new_i32();
2591 tcg_gen_trunc_tl_i32(t0, arg1);
2592 tcg_gen_bswap_i32(t0, t0);
2593 tcg_gen_extu_i32_tl(arg1, t0);
2594 tcg_temp_free_i32(t0);
2596 tcg_gen_bswap_i32(arg1, arg1);
2601 #if defined(TARGET_PPC64)
2602 static always_inline void gen_qemu_ld32s(DisasContext *ctx, TCGv arg1, TCGv arg2)
2604 if (unlikely(ctx->mem_idx)) {
2606 tcg_gen_qemu_ld32u(arg1, arg2, ctx->mem_idx);
2607 t0 = tcg_temp_new_i32();
2608 tcg_gen_trunc_tl_i32(t0, arg1);
2609 tcg_gen_bswap_i32(t0, t0);
2610 tcg_gen_ext_i32_tl(arg1, t0);
2611 tcg_temp_free_i32(t0);
2613 tcg_gen_qemu_ld32s(arg1, arg2, ctx->mem_idx);
2617 static always_inline void gen_qemu_ld64(DisasContext *ctx, TCGv_i64 arg1, TCGv arg2)
2619 tcg_gen_qemu_ld64(arg1, arg2, ctx->mem_idx);
2620 if (unlikely(ctx->le_mode)) {
2621 tcg_gen_bswap_i64(arg1, arg1);
2625 static always_inline void gen_qemu_st8(DisasContext *ctx, TCGv arg1, TCGv arg2)
2627 tcg_gen_qemu_st8(arg1, arg2, ctx->mem_idx);
2630 static always_inline void gen_qemu_st16(DisasContext *ctx, TCGv arg1, TCGv arg2)
2632 if (unlikely(ctx->le_mode)) {
2633 #if defined(TARGET_PPC64)
2636 t0 = tcg_temp_new_i32();
2637 tcg_gen_trunc_tl_i32(t0, arg1);
2638 tcg_gen_ext16u_i32(t0, t0);
2639 tcg_gen_bswap16_i32(t0, t0);
2640 t1 = tcg_temp_new();
2641 tcg_gen_extu_i32_tl(t1, t0);
2642 tcg_temp_free_i32(t0);
2643 tcg_gen_qemu_st16(t1, arg2, ctx->mem_idx);
2646 TCGv t0 = tcg_temp_new();
2647 tcg_gen_ext16u_tl(t0, arg1);
2648 tcg_gen_bswap16_i32(t0, t0);
2649 tcg_gen_qemu_st16(t0, arg2, ctx->mem_idx);
2653 tcg_gen_qemu_st16(arg1, arg2, ctx->mem_idx);
2657 static always_inline void gen_qemu_st32(DisasContext *ctx, TCGv arg1, TCGv arg2)
2659 if (unlikely(ctx->le_mode)) {
2660 #if defined(TARGET_PPC64)
2663 t0 = tcg_temp_new_i32();
2664 tcg_gen_trunc_tl_i32(t0, arg1);
2665 tcg_gen_bswap_i32(t0, t0);
2666 t1 = tcg_temp_new();
2667 tcg_gen_extu_i32_tl(t1, t0);
2668 tcg_temp_free_i32(t0);
2669 tcg_gen_qemu_st32(t1, arg2, ctx->mem_idx);
2672 TCGv t0 = tcg_temp_new_i32();
2673 tcg_gen_bswap_i32(t0, arg1);
2674 tcg_gen_qemu_st32(t0, arg2, ctx->mem_idx);
2678 tcg_gen_qemu_st32(arg1, arg2, ctx->mem_idx);
2682 static always_inline void gen_qemu_st64(DisasContext *ctx, TCGv_i64 arg1, TCGv arg2)
2684 if (unlikely(ctx->le_mode)) {
2685 TCGv_i64 t0 = tcg_temp_new_i64();
2686 tcg_gen_bswap_i64(t0, arg1);
2687 tcg_gen_qemu_st64(t0, arg2, ctx->mem_idx);
2688 tcg_temp_free_i64(t0);
2690 tcg_gen_qemu_st64(arg1, arg2, ctx->mem_idx);
2693 #define GEN_LD(name, ldop, opc, type) \
2694 GEN_HANDLER(name, opc, 0xFF, 0xFF, 0x00000000, type) \
2697 gen_set_access_type(ctx, ACCESS_INT); \
2698 EA = tcg_temp_new(); \
2699 gen_addr_imm_index(ctx, EA, 0); \
2700 gen_qemu_##ldop(ctx, cpu_gpr[rD(ctx->opcode)], EA); \
2701 tcg_temp_free(EA); \
2704 #define GEN_LDU(name, ldop, opc, type) \
2705 GEN_HANDLER(name##u, opc, 0xFF, 0xFF, 0x00000000, type) \
2708 if (unlikely(rA(ctx->opcode) == 0 || \
2709 rA(ctx->opcode) == rD(ctx->opcode))) { \
2710 gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL); \
2713 gen_set_access_type(ctx, ACCESS_INT); \
2714 EA = tcg_temp_new(); \
2715 if (type == PPC_64B) \
2716 gen_addr_imm_index(ctx, EA, 0x03); \
2718 gen_addr_imm_index(ctx, EA, 0); \
2719 gen_qemu_##ldop(ctx, cpu_gpr[rD(ctx->opcode)], EA); \
2720 tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], EA); \
2721 tcg_temp_free(EA); \
2724 #define GEN_LDUX(name, ldop, opc2, opc3, type) \
2725 GEN_HANDLER(name##ux, 0x1F, opc2, opc3, 0x00000001, type) \
2728 if (unlikely(rA(ctx->opcode) == 0 || \
2729 rA(ctx->opcode) == rD(ctx->opcode))) { \
2730 gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL); \
2733 gen_set_access_type(ctx, ACCESS_INT); \
2734 EA = tcg_temp_new(); \
2735 gen_addr_reg_index(ctx, EA); \
2736 gen_qemu_##ldop(ctx, cpu_gpr[rD(ctx->opcode)], EA); \
2737 tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], EA); \
2738 tcg_temp_free(EA); \
2741 #define GEN_LDX(name, ldop, opc2, opc3, type) \
2742 GEN_HANDLER(name##x, 0x1F, opc2, opc3, 0x00000001, type) \
2745 gen_set_access_type(ctx, ACCESS_INT); \
2746 EA = tcg_temp_new(); \
2747 gen_addr_reg_index(ctx, EA); \
2748 gen_qemu_##ldop(ctx, cpu_gpr[rD(ctx->opcode)], EA); \
2749 tcg_temp_free(EA); \
2752 #define GEN_LDS(name, ldop, op, type) \
2753 GEN_LD(name, ldop, op | 0x20, type); \
2754 GEN_LDU(name, ldop, op | 0x21, type); \
2755 GEN_LDUX(name, ldop, 0x17, op | 0x01, type); \
2756 GEN_LDX(name, ldop, 0x17, op | 0x00, type)
2758 /* lbz lbzu lbzux lbzx */
2759 GEN_LDS(lbz, ld8u, 0x02, PPC_INTEGER);
2760 /* lha lhau lhaux lhax */
2761 GEN_LDS(lha, ld16s, 0x0A, PPC_INTEGER);
2762 /* lhz lhzu lhzux lhzx */
2763 GEN_LDS(lhz, ld16u, 0x08, PPC_INTEGER);
2764 /* lwz lwzu lwzux lwzx */
2765 GEN_LDS(lwz, ld32u, 0x00, PPC_INTEGER);
2766 #if defined(TARGET_PPC64)
2768 GEN_LDUX(lwa, ld32s, 0x15, 0x0B, PPC_64B);
2770 GEN_LDX(lwa, ld32s, 0x15, 0x0A, PPC_64B);
2772 GEN_LDUX(ld, ld64, 0x15, 0x01, PPC_64B);
2774 GEN_LDX(ld, ld64, 0x15, 0x00, PPC_64B);
2775 GEN_HANDLER(ld, 0x3A, 0xFF, 0xFF, 0x00000000, PPC_64B)
2778 if (Rc(ctx->opcode)) {
2779 if (unlikely(rA(ctx->opcode) == 0 ||
2780 rA(ctx->opcode) == rD(ctx->opcode))) {
2781 gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL);
2785 gen_set_access_type(ctx, ACCESS_INT);
2786 EA = tcg_temp_new();
2787 gen_addr_imm_index(ctx, EA, 0x03);
2788 if (ctx->opcode & 0x02) {
2789 /* lwa (lwau is undefined) */
2790 gen_qemu_ld32s(ctx, cpu_gpr[rD(ctx->opcode)], EA);
2793 gen_qemu_ld64(ctx, cpu_gpr[rD(ctx->opcode)], EA);
2795 if (Rc(ctx->opcode))
2796 tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], EA);
2800 GEN_HANDLER(lq, 0x38, 0xFF, 0xFF, 0x00000000, PPC_64BX)
2802 #if defined(CONFIG_USER_ONLY)
2803 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
2808 /* Restore CPU state */
2809 if (unlikely(ctx->mem_idx == 0)) {
2810 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
2813 ra = rA(ctx->opcode);
2814 rd = rD(ctx->opcode);
2815 if (unlikely((rd & 1) || rd == ra)) {
2816 gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL);
2819 if (unlikely(ctx->le_mode)) {
2820 /* Little-endian mode is not handled */
2821 gen_exception_err(ctx, POWERPC_EXCP_ALIGN, POWERPC_EXCP_ALIGN_LE);
2824 gen_set_access_type(ctx, ACCESS_INT);
2825 EA = tcg_temp_new();
2826 gen_addr_imm_index(ctx, EA, 0x0F);
2827 gen_qemu_ld64(ctx, cpu_gpr[rd], EA);
2828 gen_addr_add(ctx, EA, EA, 8);
2829 gen_qemu_ld64(ctx, cpu_gpr[rd+1], EA);
2835 /*** Integer store ***/
2836 #define GEN_ST(name, stop, opc, type) \
2837 GEN_HANDLER(name, opc, 0xFF, 0xFF, 0x00000000, type) \
2840 gen_set_access_type(ctx, ACCESS_INT); \
2841 EA = tcg_temp_new(); \
2842 gen_addr_imm_index(ctx, EA, 0); \
2843 gen_qemu_##stop(ctx, cpu_gpr[rS(ctx->opcode)], EA); \
2844 tcg_temp_free(EA); \
2847 #define GEN_STU(name, stop, opc, type) \
2848 GEN_HANDLER(stop##u, opc, 0xFF, 0xFF, 0x00000000, type) \
2851 if (unlikely(rA(ctx->opcode) == 0)) { \
2852 gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL); \
2855 gen_set_access_type(ctx, ACCESS_INT); \
2856 EA = tcg_temp_new(); \
2857 if (type == PPC_64B) \
2858 gen_addr_imm_index(ctx, EA, 0x03); \
2860 gen_addr_imm_index(ctx, EA, 0); \
2861 gen_qemu_##stop(ctx, cpu_gpr[rS(ctx->opcode)], EA); \
2862 tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], EA); \
2863 tcg_temp_free(EA); \
2866 #define GEN_STUX(name, stop, opc2, opc3, type) \
2867 GEN_HANDLER(name##ux, 0x1F, opc2, opc3, 0x00000001, type) \
2870 if (unlikely(rA(ctx->opcode) == 0)) { \
2871 gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL); \
2874 gen_set_access_type(ctx, ACCESS_INT); \
2875 EA = tcg_temp_new(); \
2876 gen_addr_reg_index(ctx, EA); \
2877 gen_qemu_##stop(ctx, cpu_gpr[rS(ctx->opcode)], EA); \
2878 tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], EA); \
2879 tcg_temp_free(EA); \
2882 #define GEN_STX(name, stop, opc2, opc3, type) \
2883 GEN_HANDLER(name##x, 0x1F, opc2, opc3, 0x00000001, type) \
2886 gen_set_access_type(ctx, ACCESS_INT); \
2887 EA = tcg_temp_new(); \
2888 gen_addr_reg_index(ctx, EA); \
2889 gen_qemu_##stop(ctx, cpu_gpr[rS(ctx->opcode)], EA); \
2890 tcg_temp_free(EA); \
2893 #define GEN_STS(name, stop, op, type) \
2894 GEN_ST(name, stop, op | 0x20, type); \
2895 GEN_STU(name, stop, op | 0x21, type); \
2896 GEN_STUX(name, stop, 0x17, op | 0x01, type); \
2897 GEN_STX(name, stop, 0x17, op | 0x00, type)
2899 /* stb stbu stbux stbx */
2900 GEN_STS(stb, st8, 0x06, PPC_INTEGER);
2901 /* sth sthu sthux sthx */
2902 GEN_STS(sth, st16, 0x0C, PPC_INTEGER);
2903 /* stw stwu stwux stwx */
2904 GEN_STS(stw, st32, 0x04, PPC_INTEGER);
2905 #if defined(TARGET_PPC64)
2906 GEN_STUX(std, st64, 0x15, 0x05, PPC_64B);
2907 GEN_STX(std, st64, 0x15, 0x04, PPC_64B);
2908 GEN_HANDLER(std, 0x3E, 0xFF, 0xFF, 0x00000000, PPC_64B)
2913 rs = rS(ctx->opcode);
2914 if ((ctx->opcode & 0x3) == 0x2) {
2915 #if defined(CONFIG_USER_ONLY)
2916 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
2919 if (unlikely(ctx->mem_idx == 0)) {
2920 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
2923 if (unlikely(rs & 1)) {
2924 gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL);
2927 if (unlikely(ctx->le_mode)) {
2928 /* Little-endian mode is not handled */
2929 gen_exception_err(ctx, POWERPC_EXCP_ALIGN, POWERPC_EXCP_ALIGN_LE);
2932 gen_set_access_type(ctx, ACCESS_INT);
2933 EA = tcg_temp_new();
2934 gen_addr_imm_index(ctx, EA, 0x03);
2935 gen_qemu_st64(ctx, cpu_gpr[rs], EA);
2936 gen_addr_add(ctx, EA, EA, 8);
2937 gen_qemu_st64(ctx, cpu_gpr[rs+1], EA);
2942 if (Rc(ctx->opcode)) {
2943 if (unlikely(rA(ctx->opcode) == 0)) {
2944 gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL);
2948 gen_set_access_type(ctx, ACCESS_INT);
2949 EA = tcg_temp_new();
2950 gen_addr_imm_index(ctx, EA, 0x03);
2951 gen_qemu_st64(ctx, cpu_gpr[rs], EA);
2952 if (Rc(ctx->opcode))
2953 tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], EA);
2958 /*** Integer load and store with byte reverse ***/
2960 static void always_inline gen_qemu_ld16ur(DisasContext *ctx, TCGv arg1, TCGv arg2)
2962 tcg_gen_qemu_ld16u(arg1, arg2, ctx->mem_idx);
2963 if (likely(!ctx->le_mode)) {
2964 #if defined(TARGET_PPC64)
2965 TCGv_i32 t0 = tcg_temp_new_i32();
2966 tcg_gen_trunc_tl_i32(t0, arg1);
2967 tcg_gen_bswap16_i32(t0, t0);
2968 tcg_gen_extu_i32_tl(arg1, t0);
2969 tcg_temp_free_i32(t0);
2971 tcg_gen_bswap16_i32(arg1, arg1);
2975 GEN_LDX(lhbr, ld16ur, 0x16, 0x18, PPC_INTEGER);
2978 static void always_inline gen_qemu_ld32ur(DisasContext *ctx, TCGv arg1, TCGv arg2)
2980 tcg_gen_qemu_ld32u(arg1, arg2, ctx->mem_idx);
2981 if (likely(!ctx->le_mode)) {
2982 #if defined(TARGET_PPC64)
2983 TCGv_i32 t0 = tcg_temp_new_i32();
2984 tcg_gen_trunc_tl_i32(t0, arg1);
2985 tcg_gen_bswap_i32(t0, t0);
2986 tcg_gen_extu_i32_tl(arg1, t0);
2987 tcg_temp_free_i32(t0);
2989 tcg_gen_bswap_i32(arg1, arg1);
2993 GEN_LDX(lwbr, ld32ur, 0x16, 0x10, PPC_INTEGER);
2996 static void always_inline gen_qemu_st16r(DisasContext *ctx, TCGv arg1, TCGv arg2)
2998 if (likely(!ctx->le_mode)) {
2999 #if defined(TARGET_PPC64)
3002 t0 = tcg_temp_new_i32();
3003 tcg_gen_trunc_tl_i32(t0, arg1);
3004 tcg_gen_ext16u_i32(t0, t0);
3005 tcg_gen_bswap16_i32(t0, t0);
3006 t1 = tcg_temp_new();
3007 tcg_gen_extu_i32_tl(t1, t0);
3008 tcg_temp_free_i32(t0);
3009 tcg_gen_qemu_st16(t1, arg2, ctx->mem_idx);
3012 TCGv t0 = tcg_temp_new();
3013 tcg_gen_ext16u_tl(t0, arg1);
3014 tcg_gen_bswap16_i32(t0, t0);
3015 tcg_gen_qemu_st16(t0, arg2, ctx->mem_idx);
3019 tcg_gen_qemu_st16(arg1, arg2, ctx->mem_idx);
3022 GEN_STX(sthbr, st16r, 0x16, 0x1C, PPC_INTEGER);
3025 static void always_inline gen_qemu_st32r(DisasContext *ctx, TCGv arg1, TCGv arg2)
3027 if (likely(!ctx->le_mode)) {
3028 #if defined(TARGET_PPC64)
3031 t0 = tcg_temp_new_i32();
3032 tcg_gen_trunc_tl_i32(t0, arg1);
3033 tcg_gen_bswap_i32(t0, t0);
3034 t1 = tcg_temp_new();
3035 tcg_gen_extu_i32_tl(t1, t0);
3036 tcg_temp_free_i32(t0);
3037 tcg_gen_qemu_st32(t1, arg2, ctx->mem_idx);
3040 TCGv t0 = tcg_temp_new_i32();
3041 tcg_gen_bswap_i32(t0, arg1);
3042 tcg_gen_qemu_st32(t0, arg2, ctx->mem_idx);
3046 tcg_gen_qemu_st32(arg1, arg2, ctx->mem_idx);
3049 GEN_STX(stwbr, st32r, 0x16, 0x14, PPC_INTEGER);
3051 /*** Integer load and store multiple ***/
3053 GEN_HANDLER(lmw, 0x2E, 0xFF, 0xFF, 0x00000000, PPC_INTEGER)
3057 gen_set_access_type(ctx, ACCESS_INT);
3058 /* NIP cannot be restored if the memory exception comes from an helper */
3059 gen_update_nip(ctx, ctx->nip - 4);
3060 t0 = tcg_temp_new();
3061 t1 = tcg_const_i32(rD(ctx->opcode));
3062 gen_addr_imm_index(ctx, t0, 0);
3063 gen_helper_lmw(t0, t1);
3065 tcg_temp_free_i32(t1);
3069 GEN_HANDLER(stmw, 0x2F, 0xFF, 0xFF, 0x00000000, PPC_INTEGER)
3073 gen_set_access_type(ctx, ACCESS_INT);
3074 /* NIP cannot be restored if the memory exception comes from an helper */
3075 gen_update_nip(ctx, ctx->nip - 4);
3076 t0 = tcg_temp_new();
3077 t1 = tcg_const_i32(rS(ctx->opcode));
3078 gen_addr_imm_index(ctx, t0, 0);
3079 gen_helper_stmw(t0, t1);
3081 tcg_temp_free_i32(t1);
3084 /*** Integer load and store strings ***/
3086 /* PowerPC32 specification says we must generate an exception if
3087 * rA is in the range of registers to be loaded.
3088 * In an other hand, IBM says this is valid, but rA won't be loaded.
3089 * For now, I'll follow the spec...
3091 GEN_HANDLER(lswi, 0x1F, 0x15, 0x12, 0x00000001, PPC_STRING)
3095 int nb = NB(ctx->opcode);
3096 int start = rD(ctx->opcode);
3097 int ra = rA(ctx->opcode);
3103 if (unlikely(((start + nr) > 32 &&
3104 start <= ra && (start + nr - 32) > ra) ||
3105 ((start + nr) <= 32 && start <= ra && (start + nr) > ra))) {
3106 gen_inval_exception(ctx, POWERPC_EXCP_INVAL_LSWX);
3109 gen_set_access_type(ctx, ACCESS_INT);
3110 /* NIP cannot be restored if the memory exception comes from an helper */
3111 gen_update_nip(ctx, ctx->nip - 4);
3112 t0 = tcg_temp_new();
3113 gen_addr_register(ctx, t0);
3114 t1 = tcg_const_i32(nb);
3115 t2 = tcg_const_i32(start);
3116 gen_helper_lsw(t0, t1, t2);
3118 tcg_temp_free_i32(t1);
3119 tcg_temp_free_i32(t2);
3123 GEN_HANDLER(lswx, 0x1F, 0x15, 0x10, 0x00000001, PPC_STRING)
3126 TCGv_i32 t1, t2, t3;
3127 gen_set_access_type(ctx, ACCESS_INT);
3128 /* NIP cannot be restored if the memory exception comes from an helper */
3129 gen_update_nip(ctx, ctx->nip - 4);
3130 t0 = tcg_temp_new();
3131 gen_addr_reg_index(ctx, t0);
3132 t1 = tcg_const_i32(rD(ctx->opcode));
3133 t2 = tcg_const_i32(rA(ctx->opcode));
3134 t3 = tcg_const_i32(rB(ctx->opcode));
3135 gen_helper_lswx(t0, t1, t2, t3);
3137 tcg_temp_free_i32(t1);
3138 tcg_temp_free_i32(t2);
3139 tcg_temp_free_i32(t3);
3143 GEN_HANDLER(stswi, 0x1F, 0x15, 0x16, 0x00000001, PPC_STRING)
3147 int nb = NB(ctx->opcode);
3148 gen_set_access_type(ctx, ACCESS_INT);
3149 /* NIP cannot be restored if the memory exception comes from an helper */
3150 gen_update_nip(ctx, ctx->nip - 4);
3151 t0 = tcg_temp_new();
3152 gen_addr_register(ctx, t0);
3155 t1 = tcg_const_i32(nb);
3156 t2 = tcg_const_i32(rS(ctx->opcode));
3157 gen_helper_stsw(t0, t1, t2);
3159 tcg_temp_free_i32(t1);
3160 tcg_temp_free_i32(t2);
3164 GEN_HANDLER(stswx, 0x1F, 0x15, 0x14, 0x00000001, PPC_STRING)
3168 gen_set_access_type(ctx, ACCESS_INT);
3169 /* NIP cannot be restored if the memory exception comes from an helper */
3170 gen_update_nip(ctx, ctx->nip - 4);
3171 t0 = tcg_temp_new();
3172 gen_addr_reg_index(ctx, t0);
3173 t1 = tcg_temp_new_i32();
3174 tcg_gen_trunc_tl_i32(t1, cpu_xer);
3175 tcg_gen_andi_i32(t1, t1, 0x7F);
3176 t2 = tcg_const_i32(rS(ctx->opcode));
3177 gen_helper_stsw(t0, t1, t2);
3179 tcg_temp_free_i32(t1);
3180 tcg_temp_free_i32(t2);
3183 /*** Memory synchronisation ***/
3185 GEN_HANDLER(eieio, 0x1F, 0x16, 0x1A, 0x03FFF801, PPC_MEM_EIEIO)
3190 GEN_HANDLER(isync, 0x13, 0x16, 0x04, 0x03FFF801, PPC_MEM)
3192 gen_stop_exception(ctx);
3196 GEN_HANDLER(lwarx, 0x1F, 0x14, 0x00, 0x00000001, PPC_RES)
3199 gen_set_access_type(ctx, ACCESS_RES);
3200 t0 = tcg_temp_local_new();
3201 gen_addr_reg_index(ctx, t0);
3202 gen_check_align(ctx, t0, 0x03);
3203 gen_qemu_ld32u(ctx, cpu_gpr[rD(ctx->opcode)], t0);
3204 tcg_gen_mov_tl(cpu_reserve, t0);
3209 GEN_HANDLER2(stwcx_, "stwcx.", 0x1F, 0x16, 0x04, 0x00000000, PPC_RES)
3213 gen_set_access_type(ctx, ACCESS_RES);
3214 t0 = tcg_temp_local_new();
3215 gen_addr_reg_index(ctx, t0);
3216 gen_check_align(ctx, t0, 0x03);
3217 tcg_gen_trunc_tl_i32(cpu_crf[0], cpu_xer);
3218 tcg_gen_shri_i32(cpu_crf[0], cpu_crf[0], XER_SO);
3219 tcg_gen_andi_i32(cpu_crf[0], cpu_crf[0], 1);
3220 l1 = gen_new_label();
3221 tcg_gen_brcond_tl(TCG_COND_NE, t0, cpu_reserve, l1);
3222 tcg_gen_ori_i32(cpu_crf[0], cpu_crf[0], 1 << CRF_EQ);
3223 gen_qemu_st32(ctx, cpu_gpr[rS(ctx->opcode)], t0);
3225 tcg_gen_movi_tl(cpu_reserve, -1);
3229 #if defined(TARGET_PPC64)
3231 GEN_HANDLER(ldarx, 0x1F, 0x14, 0x02, 0x00000001, PPC_64B)
3234 gen_set_access_type(ctx, ACCESS_RES);
3235 t0 = tcg_temp_local_new();
3236 gen_addr_reg_index(ctx, t0);
3237 gen_check_align(ctx, t0, 0x07);
3238 gen_qemu_ld64(ctx, cpu_gpr[rD(ctx->opcode)], t0);
3239 tcg_gen_mov_tl(cpu_reserve, t0);
3244 GEN_HANDLER2(stdcx_, "stdcx.", 0x1F, 0x16, 0x06, 0x00000000, PPC_64B)
3248 gen_set_access_type(ctx, ACCESS_RES);
3249 t0 = tcg_temp_local_new();
3250 gen_addr_reg_index(ctx, t0);
3251 gen_check_align(ctx, t0, 0x07);
3252 tcg_gen_trunc_tl_i32(cpu_crf[0], cpu_xer);
3253 tcg_gen_shri_i32(cpu_crf[0], cpu_crf[0], XER_SO);
3254 tcg_gen_andi_i32(cpu_crf[0], cpu_crf[0], 1);
3255 l1 = gen_new_label();
3256 tcg_gen_brcond_tl(TCG_COND_NE, t0, cpu_reserve, l1);
3257 tcg_gen_ori_i32(cpu_crf[0], cpu_crf[0], 1 << CRF_EQ);
3258 gen_qemu_st64(ctx, cpu_gpr[rS(ctx->opcode)], t0);
3260 tcg_gen_movi_tl(cpu_reserve, -1);
3263 #endif /* defined(TARGET_PPC64) */
3266 GEN_HANDLER(sync, 0x1F, 0x16, 0x12, 0x039FF801, PPC_MEM_SYNC)
3271 GEN_HANDLER(wait, 0x1F, 0x1E, 0x01, 0x03FFF801, PPC_WAIT)
3273 TCGv_i32 t0 = tcg_temp_new_i32();
3274 tcg_gen_st_i32(t0, cpu_env, offsetof(CPUState, halted));
3275 tcg_temp_free_i32(t0);
3276 /* Stop translation, as the CPU is supposed to sleep from now */
3277 gen_exception_err(ctx, EXCP_HLT, 1);
3280 /*** Floating-point load ***/
3281 #define GEN_LDF(name, ldop, opc, type) \
3282 GEN_HANDLER(name, opc, 0xFF, 0xFF, 0x00000000, type) \
3285 if (unlikely(!ctx->fpu_enabled)) { \
3286 gen_exception(ctx, POWERPC_EXCP_FPU); \
3289 gen_set_access_type(ctx, ACCESS_FLOAT); \
3290 EA = tcg_temp_new(); \
3291 gen_addr_imm_index(ctx, EA, 0); \
3292 gen_qemu_##ldop(ctx, cpu_fpr[rD(ctx->opcode)], EA); \
3293 tcg_temp_free(EA); \
3296 #define GEN_LDUF(name, ldop, opc, type) \
3297 GEN_HANDLER(name##u, opc, 0xFF, 0xFF, 0x00000000, type) \
3300 if (unlikely(!ctx->fpu_enabled)) { \
3301 gen_exception(ctx, POWERPC_EXCP_FPU); \
3304 if (unlikely(rA(ctx->opcode) == 0)) { \
3305 gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL); \
3308 gen_set_access_type(ctx, ACCESS_FLOAT); \
3309 EA = tcg_temp_new(); \
3310 gen_addr_imm_index(ctx, EA, 0); \
3311 gen_qemu_##ldop(ctx, cpu_fpr[rD(ctx->opcode)], EA); \
3312 tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], EA); \
3313 tcg_temp_free(EA); \
3316 #define GEN_LDUXF(name, ldop, opc, type) \
3317 GEN_HANDLER(name##ux, 0x1F, 0x17, opc, 0x00000001, type) \
3320 if (unlikely(!ctx->fpu_enabled)) { \
3321 gen_exception(ctx, POWERPC_EXCP_FPU); \
3324 if (unlikely(rA(ctx->opcode) == 0)) { \
3325 gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL); \
3328 gen_set_access_type(ctx, ACCESS_FLOAT); \
3329 EA = tcg_temp_new(); \
3330 gen_addr_reg_index(ctx, EA); \
3331 gen_qemu_##ldop(ctx, cpu_fpr[rD(ctx->opcode)], EA); \
3332 tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], EA); \
3333 tcg_temp_free(EA); \
3336 #define GEN_LDXF(name, ldop, opc2, opc3, type) \
3337 GEN_HANDLER(name##x, 0x1F, opc2, opc3, 0x00000001, type) \
3340 if (unlikely(!ctx->fpu_enabled)) { \
3341 gen_exception(ctx, POWERPC_EXCP_FPU); \
3344 gen_set_access_type(ctx, ACCESS_FLOAT); \
3345 EA = tcg_temp_new(); \
3346 gen_addr_reg_index(ctx, EA); \
3347 gen_qemu_##ldop(ctx, cpu_fpr[rD(ctx->opcode)], EA); \
3348 tcg_temp_free(EA); \
3351 #define GEN_LDFS(name, ldop, op, type) \
3352 GEN_LDF(name, ldop, op | 0x20, type); \
3353 GEN_LDUF(name, ldop, op | 0x21, type); \
3354 GEN_LDUXF(name, ldop, op | 0x01, type); \
3355 GEN_LDXF(name, ldop, 0x17, op | 0x00, type)
3357 static always_inline void gen_qemu_ld32fs(DisasContext *ctx, TCGv_i64 arg1, TCGv arg2)
3359 TCGv t0 = tcg_temp_new();
3360 TCGv_i32 t1 = tcg_temp_new_i32();
3361 gen_qemu_ld32u(ctx, t0, arg2);
3362 tcg_gen_trunc_tl_i32(t1, t0);
3364 gen_helper_float32_to_float64(arg1, t1);
3365 tcg_temp_free_i32(t1);
3368 /* lfd lfdu lfdux lfdx */
3369 GEN_LDFS(lfd, ld64, 0x12, PPC_FLOAT);
3370 /* lfs lfsu lfsux lfsx */
3371 GEN_LDFS(lfs, ld32fs, 0x10, PPC_FLOAT);
3373 /*** Floating-point store ***/
3374 #define GEN_STF(name, stop, opc, type) \
3375 GEN_HANDLER(name, opc, 0xFF, 0xFF, 0x00000000, type) \
3378 if (unlikely(!ctx->fpu_enabled)) { \
3379 gen_exception(ctx, POWERPC_EXCP_FPU); \
3382 gen_set_access_type(ctx, ACCESS_FLOAT); \
3383 EA = tcg_temp_new(); \
3384 gen_addr_imm_index(ctx, EA, 0); \
3385 gen_qemu_##stop(ctx, cpu_fpr[rS(ctx->opcode)], EA); \
3386 tcg_temp_free(EA); \
3389 #define GEN_STUF(name, stop, opc, type) \
3390 GEN_HANDLER(name##u, opc, 0xFF, 0xFF, 0x00000000, type) \
3393 if (unlikely(!ctx->fpu_enabled)) { \
3394 gen_exception(ctx, POWERPC_EXCP_FPU); \
3397 if (unlikely(rA(ctx->opcode) == 0)) { \
3398 gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL); \
3401 gen_set_access_type(ctx, ACCESS_FLOAT); \
3402 EA = tcg_temp_new(); \
3403 gen_addr_imm_index(ctx, EA, 0); \
3404 gen_qemu_##stop(ctx, cpu_fpr[rS(ctx->opcode)], EA); \
3405 tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], EA); \
3406 tcg_temp_free(EA); \
3409 #define GEN_STUXF(name, stop, opc, type) \
3410 GEN_HANDLER(name##ux, 0x1F, 0x17, opc, 0x00000001, type) \
3413 if (unlikely(!ctx->fpu_enabled)) { \
3414 gen_exception(ctx, POWERPC_EXCP_FPU); \
3417 if (unlikely(rA(ctx->opcode) == 0)) { \
3418 gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL); \
3421 gen_set_access_type(ctx, ACCESS_FLOAT); \
3422 EA = tcg_temp_new(); \
3423 gen_addr_reg_index(ctx, EA); \
3424 gen_qemu_##stop(ctx, cpu_fpr[rS(ctx->opcode)], EA); \
3425 tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], EA); \
3426 tcg_temp_free(EA); \
3429 #define GEN_STXF(name, stop, opc2, opc3, type) \
3430 GEN_HANDLER(name##x, 0x1F, opc2, opc3, 0x00000001, type) \
3433 if (unlikely(!ctx->fpu_enabled)) { \
3434 gen_exception(ctx, POWERPC_EXCP_FPU); \
3437 gen_set_access_type(ctx, ACCESS_FLOAT); \
3438 EA = tcg_temp_new(); \
3439 gen_addr_reg_index(ctx, EA); \
3440 gen_qemu_##stop(ctx, cpu_fpr[rS(ctx->opcode)], EA); \
3441 tcg_temp_free(EA); \
3444 #define GEN_STFS(name, stop, op, type) \
3445 GEN_STF(name, stop, op | 0x20, type); \
3446 GEN_STUF(name, stop, op | 0x21, type); \
3447 GEN_STUXF(name, stop, op | 0x01, type); \
3448 GEN_STXF(name, stop, 0x17, op | 0x00, type)
3450 static always_inline void gen_qemu_st32fs(DisasContext *ctx, TCGv_i64 arg1, TCGv arg2)
3452 TCGv_i32 t0 = tcg_temp_new_i32();
3453 TCGv t1 = tcg_temp_new();
3454 gen_helper_float64_to_float32(t0, arg1);
3455 tcg_gen_extu_i32_tl(t1, t0);
3456 tcg_temp_free_i32(t0);
3457 gen_qemu_st32(ctx, t1, arg2);
3461 /* stfd stfdu stfdux stfdx */
3462 GEN_STFS(stfd, st64, 0x16, PPC_FLOAT);
3463 /* stfs stfsu stfsux stfsx */
3464 GEN_STFS(stfs, st32fs, 0x14, PPC_FLOAT);
3467 static always_inline void gen_qemu_st32fiw(DisasContext *ctx, TCGv_i64 arg1, TCGv arg2)
3469 TCGv t0 = tcg_temp_new();
3470 tcg_gen_trunc_i64_tl(t0, arg1),
3471 gen_qemu_st32(ctx, t0, arg2);
3475 GEN_STXF(stfiw, st32fiw, 0x17, 0x1E, PPC_FLOAT_STFIWX);
3478 static always_inline void gen_goto_tb (DisasContext *ctx, int n,
3481 TranslationBlock *tb;
3483 #if defined(TARGET_PPC64)
3485 dest = (uint32_t) dest;
3487 if ((tb->pc & TARGET_PAGE_MASK) == (dest & TARGET_PAGE_MASK) &&
3488 likely(!ctx->singlestep_enabled)) {
3490 tcg_gen_movi_tl(cpu_nip, dest & ~3);
3491 tcg_gen_exit_tb((long)tb + n);
3493 tcg_gen_movi_tl(cpu_nip, dest & ~3);
3494 if (unlikely(ctx->singlestep_enabled)) {
3495 if ((ctx->singlestep_enabled &
3496 (CPU_BRANCH_STEP | CPU_SINGLE_STEP)) &&
3497 ctx->exception == POWERPC_EXCP_BRANCH) {
3498 target_ulong tmp = ctx->nip;
3500 gen_exception(ctx, POWERPC_EXCP_TRACE);
3503 if (ctx->singlestep_enabled & GDBSTUB_SINGLE_STEP) {
3504 gen_debug_exception(ctx);
3511 static always_inline void gen_setlr (DisasContext *ctx, target_ulong nip)
3513 #if defined(TARGET_PPC64)
3514 if (ctx->sf_mode == 0)
3515 tcg_gen_movi_tl(cpu_lr, (uint32_t)nip);
3518 tcg_gen_movi_tl(cpu_lr, nip);
3522 GEN_HANDLER(b, 0x12, 0xFF, 0xFF, 0x00000000, PPC_FLOW)
3524 target_ulong li, target;
3526 ctx->exception = POWERPC_EXCP_BRANCH;
3527 /* sign extend LI */
3528 #if defined(TARGET_PPC64)
3530 li = ((int64_t)LI(ctx->opcode) << 38) >> 38;
3533 li = ((int32_t)LI(ctx->opcode) << 6) >> 6;
3534 if (likely(AA(ctx->opcode) == 0))
3535 target = ctx->nip + li - 4;
3538 if (LK(ctx->opcode))
3539 gen_setlr(ctx, ctx->nip);
3540 gen_goto_tb(ctx, 0, target);
3547 static always_inline void gen_bcond (DisasContext *ctx, int type)
3549 uint32_t bo = BO(ctx->opcode);
3550 int l1 = gen_new_label();
3553 ctx->exception = POWERPC_EXCP_BRANCH;
3554 if (type == BCOND_LR || type == BCOND_CTR) {
3555 target = tcg_temp_local_new();
3556 if (type == BCOND_CTR)
3557 tcg_gen_mov_tl(target, cpu_ctr);
3559 tcg_gen_mov_tl(target, cpu_lr);
3561 if (LK(ctx->opcode))
3562 gen_setlr(ctx, ctx->nip);
3563 l1 = gen_new_label();
3564 if ((bo & 0x4) == 0) {
3565 /* Decrement and test CTR */
3566 TCGv temp = tcg_temp_new();
3567 if (unlikely(type == BCOND_CTR)) {
3568 gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL);
3571 tcg_gen_subi_tl(cpu_ctr, cpu_ctr, 1);
3572 #if defined(TARGET_PPC64)
3574 tcg_gen_ext32u_tl(temp, cpu_ctr);
3577 tcg_gen_mov_tl(temp, cpu_ctr);
3579 tcg_gen_brcondi_tl(TCG_COND_NE, temp, 0, l1);
3581 tcg_gen_brcondi_tl(TCG_COND_EQ, temp, 0, l1);
3583 tcg_temp_free(temp);
3585 if ((bo & 0x10) == 0) {
3587 uint32_t bi = BI(ctx->opcode);
3588 uint32_t mask = 1 << (3 - (bi & 0x03));
3589 TCGv_i32 temp = tcg_temp_new_i32();
3592 tcg_gen_andi_i32(temp, cpu_crf[bi >> 2], mask);
3593 tcg_gen_brcondi_i32(TCG_COND_EQ, temp, 0, l1);
3595 tcg_gen_andi_i32(temp, cpu_crf[bi >> 2], mask);
3596 tcg_gen_brcondi_i32(TCG_COND_NE, temp, 0, l1);
3598 tcg_temp_free_i32(temp);
3600 if (type == BCOND_IM) {
3601 target_ulong li = (target_long)((int16_t)(BD(ctx->opcode)));
3602 if (likely(AA(ctx->opcode) == 0)) {
3603 gen_goto_tb(ctx, 0, ctx->nip + li - 4);
3605 gen_goto_tb(ctx, 0, li);
3608 gen_goto_tb(ctx, 1, ctx->nip);
3610 #if defined(TARGET_PPC64)
3611 if (!(ctx->sf_mode))
3612 tcg_gen_andi_tl(cpu_nip, target, (uint32_t)~3);
3615 tcg_gen_andi_tl(cpu_nip, target, ~3);
3618 #if defined(TARGET_PPC64)
3619 if (!(ctx->sf_mode))
3620 tcg_gen_movi_tl(cpu_nip, (uint32_t)ctx->nip);
3623 tcg_gen_movi_tl(cpu_nip, ctx->nip);
3628 GEN_HANDLER(bc, 0x10, 0xFF, 0xFF, 0x00000000, PPC_FLOW)
3630 gen_bcond(ctx, BCOND_IM);
3633 GEN_HANDLER(bcctr, 0x13, 0x10, 0x10, 0x00000000, PPC_FLOW)
3635 gen_bcond(ctx, BCOND_CTR);
3638 GEN_HANDLER(bclr, 0x13, 0x10, 0x00, 0x00000000, PPC_FLOW)
3640 gen_bcond(ctx, BCOND_LR);
3643 /*** Condition register logical ***/
3644 #define GEN_CRLOGIC(name, tcg_op, opc) \
3645 GEN_HANDLER(name, 0x13, 0x01, opc, 0x00000001, PPC_INTEGER) \
3650 sh = (crbD(ctx->opcode) & 0x03) - (crbA(ctx->opcode) & 0x03); \
3651 t0 = tcg_temp_new_i32(); \
3653 tcg_gen_shri_i32(t0, cpu_crf[crbA(ctx->opcode) >> 2], sh); \
3655 tcg_gen_shli_i32(t0, cpu_crf[crbA(ctx->opcode) >> 2], -sh); \
3657 tcg_gen_mov_i32(t0, cpu_crf[crbA(ctx->opcode) >> 2]); \
3658 t1 = tcg_temp_new_i32(); \
3659 sh = (crbD(ctx->opcode) & 0x03) - (crbB(ctx->opcode) & 0x03); \
3661 tcg_gen_shri_i32(t1, cpu_crf[crbB(ctx->opcode) >> 2], sh); \
3663 tcg_gen_shli_i32(t1, cpu_crf[crbB(ctx->opcode) >> 2], -sh); \
3665 tcg_gen_mov_i32(t1, cpu_crf[crbB(ctx->opcode) >> 2]); \
3666 tcg_op(t0, t0, t1); \
3667 bitmask = 1 << (3 - (crbD(ctx->opcode) & 0x03)); \
3668 tcg_gen_andi_i32(t0, t0, bitmask); \
3669 tcg_gen_andi_i32(t1, cpu_crf[crbD(ctx->opcode) >> 2], ~bitmask); \
3670 tcg_gen_or_i32(cpu_crf[crbD(ctx->opcode) >> 2], t0, t1); \
3671 tcg_temp_free_i32(t0); \
3672 tcg_temp_free_i32(t1); \
3676 GEN_CRLOGIC(crand, tcg_gen_and_i32, 0x08);
3678 GEN_CRLOGIC(crandc, tcg_gen_andc_i32, 0x04);
3680 GEN_CRLOGIC(creqv, tcg_gen_eqv_i32, 0x09);
3682 GEN_CRLOGIC(crnand, tcg_gen_nand_i32, 0x07);
3684 GEN_CRLOGIC(crnor, tcg_gen_nor_i32, 0x01);
3686 GEN_CRLOGIC(cror, tcg_gen_or_i32, 0x0E);
3688 GEN_CRLOGIC(crorc, tcg_gen_orc_i32, 0x0D);
3690 GEN_CRLOGIC(crxor, tcg_gen_xor_i32, 0x06);
3692 GEN_HANDLER(mcrf, 0x13, 0x00, 0xFF, 0x00000001, PPC_INTEGER)
3694 tcg_gen_mov_i32(cpu_crf[crfD(ctx->opcode)], cpu_crf[crfS(ctx->opcode)]);
3697 /*** System linkage ***/
3698 /* rfi (mem_idx only) */
3699 GEN_HANDLER(rfi, 0x13, 0x12, 0x01, 0x03FF8001, PPC_FLOW)
3701 #if defined(CONFIG_USER_ONLY)
3702 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
3704 /* Restore CPU state */
3705 if (unlikely(!ctx->mem_idx)) {
3706 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
3710 gen_sync_exception(ctx);
3714 #if defined(TARGET_PPC64)
3715 GEN_HANDLER(rfid, 0x13, 0x12, 0x00, 0x03FF8001, PPC_64B)
3717 #if defined(CONFIG_USER_ONLY)
3718 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
3720 /* Restore CPU state */
3721 if (unlikely(!ctx->mem_idx)) {
3722 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
3726 gen_sync_exception(ctx);
3730 GEN_HANDLER(hrfid, 0x13, 0x12, 0x08, 0x03FF8001, PPC_64H)
3732 #if defined(CONFIG_USER_ONLY)
3733 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
3735 /* Restore CPU state */
3736 if (unlikely(ctx->mem_idx <= 1)) {
3737 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
3741 gen_sync_exception(ctx);
3747 #if defined(CONFIG_USER_ONLY)
3748 #define POWERPC_SYSCALL POWERPC_EXCP_SYSCALL_USER
3750 #define POWERPC_SYSCALL POWERPC_EXCP_SYSCALL
3752 GEN_HANDLER(sc, 0x11, 0xFF, 0xFF, 0x03FFF01D, PPC_FLOW)
3756 lev = (ctx->opcode >> 5) & 0x7F;
3757 gen_exception_err(ctx, POWERPC_SYSCALL, lev);
3762 GEN_HANDLER(tw, 0x1F, 0x04, 0x00, 0x00000001, PPC_FLOW)
3764 TCGv_i32 t0 = tcg_const_i32(TO(ctx->opcode));
3765 /* Update the nip since this might generate a trap exception */
3766 gen_update_nip(ctx, ctx->nip);
3767 gen_helper_tw(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rB(ctx->opcode)], t0);
3768 tcg_temp_free_i32(t0);
3772 GEN_HANDLER(twi, 0x03, 0xFF, 0xFF, 0x00000000, PPC_FLOW)
3774 TCGv t0 = tcg_const_tl(SIMM(ctx->opcode));
3775 TCGv_i32 t1 = tcg_const_i32(TO(ctx->opcode));
3776 /* Update the nip since this might generate a trap exception */
3777 gen_update_nip(ctx, ctx->nip);
3778 gen_helper_tw(cpu_gpr[rA(ctx->opcode)], t0, t1);
3780 tcg_temp_free_i32(t1);
3783 #if defined(TARGET_PPC64)
3785 GEN_HANDLER(td, 0x1F, 0x04, 0x02, 0x00000001, PPC_64B)
3787 TCGv_i32 t0 = tcg_const_i32(TO(ctx->opcode));
3788 /* Update the nip since this might generate a trap exception */
3789 gen_update_nip(ctx, ctx->nip);
3790 gen_helper_td(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rB(ctx->opcode)], t0);
3791 tcg_temp_free_i32(t0);
3795 GEN_HANDLER(tdi, 0x02, 0xFF, 0xFF, 0x00000000, PPC_64B)
3797 TCGv t0 = tcg_const_tl(SIMM(ctx->opcode));
3798 TCGv_i32 t1 = tcg_const_i32(TO(ctx->opcode));
3799 /* Update the nip since this might generate a trap exception */
3800 gen_update_nip(ctx, ctx->nip);
3801 gen_helper_td(cpu_gpr[rA(ctx->opcode)], t0, t1);
3803 tcg_temp_free_i32(t1);
3807 /*** Processor control ***/
3809 GEN_HANDLER(mcrxr, 0x1F, 0x00, 0x10, 0x007FF801, PPC_MISC)
3811 tcg_gen_trunc_tl_i32(cpu_crf[crfD(ctx->opcode)], cpu_xer);
3812 tcg_gen_shri_i32(cpu_crf[crfD(ctx->opcode)], cpu_crf[crfD(ctx->opcode)], XER_CA);
3813 tcg_gen_andi_tl(cpu_xer, cpu_xer, ~(1 << XER_SO | 1 << XER_OV | 1 << XER_CA));
3817 GEN_HANDLER(mfcr, 0x1F, 0x13, 0x00, 0x00000801, PPC_MISC)
3821 if (likely(ctx->opcode & 0x00100000)) {
3822 crm = CRM(ctx->opcode);
3823 if (likely((crm ^ (crm - 1)) == 0)) {
3825 tcg_gen_extu_i32_tl(cpu_gpr[rD(ctx->opcode)], cpu_crf[7 - crn]);
3828 gen_helper_load_cr(cpu_gpr[rD(ctx->opcode)]);
3833 GEN_HANDLER(mfmsr, 0x1F, 0x13, 0x02, 0x001FF801, PPC_MISC)
3835 #if defined(CONFIG_USER_ONLY)
3836 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
3838 if (unlikely(!ctx->mem_idx)) {
3839 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
3842 tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_msr);
3847 #define SPR_NOACCESS ((void *)(-1UL))
3849 static void spr_noaccess (void *opaque, int sprn)
3851 sprn = ((sprn >> 5) & 0x1F) | ((sprn & 0x1F) << 5);
3852 printf("ERROR: try to access SPR %d !\n", sprn);
3854 #define SPR_NOACCESS (&spr_noaccess)
3858 static always_inline void gen_op_mfspr (DisasContext *ctx)
3860 void (*read_cb)(void *opaque, int gprn, int sprn);
3861 uint32_t sprn = SPR(ctx->opcode);
3863 #if !defined(CONFIG_USER_ONLY)
3864 if (ctx->mem_idx == 2)
3865 read_cb = ctx->spr_cb[sprn].hea_read;
3866 else if (ctx->mem_idx)
3867 read_cb = ctx->spr_cb[sprn].oea_read;
3870 read_cb = ctx->spr_cb[sprn].uea_read;
3871 if (likely(read_cb != NULL)) {
3872 if (likely(read_cb != SPR_NOACCESS)) {
3873 (*read_cb)(ctx, rD(ctx->opcode), sprn);
3875 /* Privilege exception */
3876 /* This is a hack to avoid warnings when running Linux:
3877 * this OS breaks the PowerPC virtualisation model,
3878 * allowing userland application to read the PVR
3880 if (sprn != SPR_PVR) {
3881 if (loglevel != 0) {
3882 fprintf(logfile, "Trying to read privileged spr %d %03x at "
3883 ADDRX "\n", sprn, sprn, ctx->nip);
3885 printf("Trying to read privileged spr %d %03x at " ADDRX "\n",
3886 sprn, sprn, ctx->nip);
3888 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
3892 if (loglevel != 0) {
3893 fprintf(logfile, "Trying to read invalid spr %d %03x at "
3894 ADDRX "\n", sprn, sprn, ctx->nip);
3896 printf("Trying to read invalid spr %d %03x at " ADDRX "\n",
3897 sprn, sprn, ctx->nip);
3898 gen_inval_exception(ctx, POWERPC_EXCP_INVAL_SPR);
3902 GEN_HANDLER(mfspr, 0x1F, 0x13, 0x0A, 0x00000001, PPC_MISC)
3908 GEN_HANDLER(mftb, 0x1F, 0x13, 0x0B, 0x00000001, PPC_MFTB)
3914 GEN_HANDLER(mtcrf, 0x1F, 0x10, 0x04, 0x00000801, PPC_MISC)
3918 crm = CRM(ctx->opcode);
3919 if (likely((ctx->opcode & 0x00100000) || (crm ^ (crm - 1)) == 0)) {
3920 TCGv_i32 temp = tcg_temp_new_i32();
3922 tcg_gen_trunc_tl_i32(temp, cpu_gpr[rS(ctx->opcode)]);
3923 tcg_gen_shri_i32(cpu_crf[7 - crn], temp, crn * 4);
3924 tcg_gen_andi_i32(cpu_crf[7 - crn], cpu_crf[7 - crn], 0xf);
3925 tcg_temp_free_i32(temp);
3927 TCGv_i32 temp = tcg_const_i32(crm);
3928 gen_helper_store_cr(cpu_gpr[rS(ctx->opcode)], temp);
3929 tcg_temp_free_i32(temp);
3934 #if defined(TARGET_PPC64)
3935 GEN_HANDLER(mtmsrd, 0x1F, 0x12, 0x05, 0x001EF801, PPC_64B)
3937 #if defined(CONFIG_USER_ONLY)
3938 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
3940 if (unlikely(!ctx->mem_idx)) {
3941 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
3944 if (ctx->opcode & 0x00010000) {
3945 /* Special form that does not need any synchronisation */
3946 TCGv t0 = tcg_temp_new();
3947 tcg_gen_andi_tl(t0, cpu_gpr[rS(ctx->opcode)], (1 << MSR_RI) | (1 << MSR_EE));
3948 tcg_gen_andi_tl(cpu_msr, cpu_msr, ~((1 << MSR_RI) | (1 << MSR_EE)));
3949 tcg_gen_or_tl(cpu_msr, cpu_msr, t0);
3952 /* XXX: we need to update nip before the store
3953 * if we enter power saving mode, we will exit the loop
3954 * directly from ppc_store_msr
3956 gen_update_nip(ctx, ctx->nip);
3957 gen_helper_store_msr(cpu_gpr[rS(ctx->opcode)]);
3958 /* Must stop the translation as machine state (may have) changed */
3959 /* Note that mtmsr is not always defined as context-synchronizing */
3960 gen_stop_exception(ctx);
3966 GEN_HANDLER(mtmsr, 0x1F, 0x12, 0x04, 0x001FF801, PPC_MISC)
3968 #if defined(CONFIG_USER_ONLY)
3969 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
3971 if (unlikely(!ctx->mem_idx)) {
3972 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
3975 if (ctx->opcode & 0x00010000) {
3976 /* Special form that does not need any synchronisation */
3977 TCGv t0 = tcg_temp_new();
3978 tcg_gen_andi_tl(t0, cpu_gpr[rS(ctx->opcode)], (1 << MSR_RI) | (1 << MSR_EE));
3979 tcg_gen_andi_tl(cpu_msr, cpu_msr, ~((1 << MSR_RI) | (1 << MSR_EE)));
3980 tcg_gen_or_tl(cpu_msr, cpu_msr, t0);
3983 /* XXX: we need to update nip before the store
3984 * if we enter power saving mode, we will exit the loop
3985 * directly from ppc_store_msr
3987 gen_update_nip(ctx, ctx->nip);
3988 #if defined(TARGET_PPC64)
3989 if (!ctx->sf_mode) {
3990 TCGv t0 = tcg_temp_new();
3991 TCGv t1 = tcg_temp_new();
3992 tcg_gen_andi_tl(t0, cpu_msr, 0xFFFFFFFF00000000ULL);
3993 tcg_gen_ext32u_tl(t1, cpu_gpr[rS(ctx->opcode)]);
3994 tcg_gen_or_tl(t0, t0, t1);
3996 gen_helper_store_msr(t0);
4000 gen_helper_store_msr(cpu_gpr[rS(ctx->opcode)]);
4001 /* Must stop the translation as machine state (may have) changed */
4002 /* Note that mtmsr is not always defined as context-synchronizing */
4003 gen_stop_exception(ctx);
4009 GEN_HANDLER(mtspr, 0x1F, 0x13, 0x0E, 0x00000001, PPC_MISC)
4011 void (*write_cb)(void *opaque, int sprn, int gprn);
4012 uint32_t sprn = SPR(ctx->opcode);
4014 #if !defined(CONFIG_USER_ONLY)
4015 if (ctx->mem_idx == 2)
4016 write_cb = ctx->spr_cb[sprn].hea_write;
4017 else if (ctx->mem_idx)
4018 write_cb = ctx->spr_cb[sprn].oea_write;
4021 write_cb = ctx->spr_cb[sprn].uea_write;
4022 if (likely(write_cb != NULL)) {
4023 if (likely(write_cb != SPR_NOACCESS)) {
4024 (*write_cb)(ctx, sprn, rS(ctx->opcode));
4026 /* Privilege exception */
4027 if (loglevel != 0) {
4028 fprintf(logfile, "Trying to write privileged spr %d %03x at "
4029 ADDRX "\n", sprn, sprn, ctx->nip);
4031 printf("Trying to write privileged spr %d %03x at " ADDRX "\n",
4032 sprn, sprn, ctx->nip);
4033 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
4037 if (loglevel != 0) {
4038 fprintf(logfile, "Trying to write invalid spr %d %03x at "
4039 ADDRX "\n", sprn, sprn, ctx->nip);
4041 printf("Trying to write invalid spr %d %03x at " ADDRX "\n",
4042 sprn, sprn, ctx->nip);
4043 gen_inval_exception(ctx, POWERPC_EXCP_INVAL_SPR);
4047 /*** Cache management ***/
4049 GEN_HANDLER(dcbf, 0x1F, 0x16, 0x02, 0x03C00001, PPC_CACHE)
4051 /* XXX: specification says this is treated as a load by the MMU */
4053 gen_set_access_type(ctx, ACCESS_CACHE);
4054 t0 = tcg_temp_new();
4055 gen_addr_reg_index(ctx, t0);
4056 gen_qemu_ld8u(ctx, t0, t0);
4060 /* dcbi (Supervisor only) */
4061 GEN_HANDLER(dcbi, 0x1F, 0x16, 0x0E, 0x03E00001, PPC_CACHE)
4063 #if defined(CONFIG_USER_ONLY)
4064 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
4067 if (unlikely(!ctx->mem_idx)) {
4068 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
4071 EA = tcg_temp_new();
4072 gen_set_access_type(ctx, ACCESS_CACHE);
4073 gen_addr_reg_index(ctx, EA);
4074 val = tcg_temp_new();
4075 /* XXX: specification says this should be treated as a store by the MMU */
4076 gen_qemu_ld8u(ctx, val, EA);
4077 gen_qemu_st8(ctx, val, EA);
4084 GEN_HANDLER(dcbst, 0x1F, 0x16, 0x01, 0x03E00001, PPC_CACHE)
4086 /* XXX: specification say this is treated as a load by the MMU */
4088 gen_set_access_type(ctx, ACCESS_CACHE);
4089 t0 = tcg_temp_new();
4090 gen_addr_reg_index(ctx, t0);
4091 gen_qemu_ld8u(ctx, t0, t0);
4096 GEN_HANDLER(dcbt, 0x1F, 0x16, 0x08, 0x02000001, PPC_CACHE)
4098 /* interpreted as no-op */
4099 /* XXX: specification say this is treated as a load by the MMU
4100 * but does not generate any exception
4105 GEN_HANDLER(dcbtst, 0x1F, 0x16, 0x07, 0x02000001, PPC_CACHE)
4107 /* interpreted as no-op */
4108 /* XXX: specification say this is treated as a load by the MMU
4109 * but does not generate any exception
4114 GEN_HANDLER(dcbz, 0x1F, 0x16, 0x1F, 0x03E00001, PPC_CACHE_DCBZ)
4117 gen_set_access_type(ctx, ACCESS_CACHE);
4118 /* NIP cannot be restored if the memory exception comes from an helper */
4119 gen_update_nip(ctx, ctx->nip - 4);
4120 t0 = tcg_temp_new();
4121 gen_addr_reg_index(ctx, t0);
4122 gen_helper_dcbz(t0);
4126 GEN_HANDLER2(dcbz_970, "dcbz", 0x1F, 0x16, 0x1F, 0x03C00001, PPC_CACHE_DCBZT)
4129 gen_set_access_type(ctx, ACCESS_CACHE);
4130 /* NIP cannot be restored if the memory exception comes from an helper */
4131 gen_update_nip(ctx, ctx->nip - 4);
4132 t0 = tcg_temp_new();
4133 gen_addr_reg_index(ctx, t0);
4134 if (ctx->opcode & 0x00200000)
4135 gen_helper_dcbz(t0);
4137 gen_helper_dcbz_970(t0);
4142 GEN_HANDLER(icbi, 0x1F, 0x16, 0x1E, 0x03E00001, PPC_CACHE_ICBI)
4145 gen_set_access_type(ctx, ACCESS_CACHE);
4146 /* NIP cannot be restored if the memory exception comes from an helper */
4147 gen_update_nip(ctx, ctx->nip - 4);
4148 t0 = tcg_temp_new();
4149 gen_addr_reg_index(ctx, t0);
4150 gen_helper_icbi(t0);
4156 GEN_HANDLER(dcba, 0x1F, 0x16, 0x17, 0x03E00001, PPC_CACHE_DCBA)
4158 /* interpreted as no-op */
4159 /* XXX: specification say this is treated as a store by the MMU
4160 * but does not generate any exception
4164 /*** Segment register manipulation ***/
4165 /* Supervisor only: */
4167 GEN_HANDLER(mfsr, 0x1F, 0x13, 0x12, 0x0010F801, PPC_SEGMENT)
4169 #if defined(CONFIG_USER_ONLY)
4170 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
4173 if (unlikely(!ctx->mem_idx)) {
4174 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
4177 t0 = tcg_const_tl(SR(ctx->opcode));
4178 gen_helper_load_sr(cpu_gpr[rD(ctx->opcode)], t0);
4184 GEN_HANDLER(mfsrin, 0x1F, 0x13, 0x14, 0x001F0001, PPC_SEGMENT)
4186 #if defined(CONFIG_USER_ONLY)
4187 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
4190 if (unlikely(!ctx->mem_idx)) {
4191 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
4194 t0 = tcg_temp_new();
4195 tcg_gen_shri_tl(t0, cpu_gpr[rB(ctx->opcode)], 28);
4196 tcg_gen_andi_tl(t0, t0, 0xF);
4197 gen_helper_load_sr(cpu_gpr[rD(ctx->opcode)], t0);
4203 GEN_HANDLER(mtsr, 0x1F, 0x12, 0x06, 0x0010F801, PPC_SEGMENT)
4205 #if defined(CONFIG_USER_ONLY)
4206 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
4209 if (unlikely(!ctx->mem_idx)) {
4210 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
4213 t0 = tcg_const_tl(SR(ctx->opcode));
4214 gen_helper_store_sr(t0, cpu_gpr[rS(ctx->opcode)]);
4220 GEN_HANDLER(mtsrin, 0x1F, 0x12, 0x07, 0x001F0001, PPC_SEGMENT)
4222 #if defined(CONFIG_USER_ONLY)
4223 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
4226 if (unlikely(!ctx->mem_idx)) {
4227 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
4230 t0 = tcg_temp_new();
4231 tcg_gen_shri_tl(t0, cpu_gpr[rB(ctx->opcode)], 28);
4232 tcg_gen_andi_tl(t0, t0, 0xF);
4233 gen_helper_store_sr(t0, cpu_gpr[rD(ctx->opcode)]);
4238 #if defined(TARGET_PPC64)
4239 /* Specific implementation for PowerPC 64 "bridge" emulation using SLB */
4241 GEN_HANDLER2(mfsr_64b, "mfsr", 0x1F, 0x13, 0x12, 0x0010F801, PPC_SEGMENT_64B)
4243 #if defined(CONFIG_USER_ONLY)
4244 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
4247 if (unlikely(!ctx->mem_idx)) {
4248 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
4251 t0 = tcg_const_tl(SR(ctx->opcode));
4252 gen_helper_load_slb(cpu_gpr[rD(ctx->opcode)], t0);
4258 GEN_HANDLER2(mfsrin_64b, "mfsrin", 0x1F, 0x13, 0x14, 0x001F0001,
4261 #if defined(CONFIG_USER_ONLY)
4262 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
4265 if (unlikely(!ctx->mem_idx)) {
4266 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
4269 t0 = tcg_temp_new();
4270 tcg_gen_shri_tl(t0, cpu_gpr[rB(ctx->opcode)], 28);
4271 tcg_gen_andi_tl(t0, t0, 0xF);
4272 gen_helper_load_slb(cpu_gpr[rD(ctx->opcode)], t0);
4278 GEN_HANDLER2(mtsr_64b, "mtsr", 0x1F, 0x12, 0x06, 0x0010F801, PPC_SEGMENT_64B)
4280 #if defined(CONFIG_USER_ONLY)
4281 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
4284 if (unlikely(!ctx->mem_idx)) {
4285 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
4288 t0 = tcg_const_tl(SR(ctx->opcode));
4289 gen_helper_store_slb(t0, cpu_gpr[rS(ctx->opcode)]);
4295 GEN_HANDLER2(mtsrin_64b, "mtsrin", 0x1F, 0x12, 0x07, 0x001F0001,
4298 #if defined(CONFIG_USER_ONLY)
4299 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
4302 if (unlikely(!ctx->mem_idx)) {
4303 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
4306 t0 = tcg_temp_new();
4307 tcg_gen_shri_tl(t0, cpu_gpr[rB(ctx->opcode)], 28);
4308 tcg_gen_andi_tl(t0, t0, 0xF);
4309 gen_helper_store_slb(t0, cpu_gpr[rS(ctx->opcode)]);
4313 #endif /* defined(TARGET_PPC64) */
4315 /*** Lookaside buffer management ***/
4316 /* Optional & mem_idx only: */
4318 GEN_HANDLER(tlbia, 0x1F, 0x12, 0x0B, 0x03FFFC01, PPC_MEM_TLBIA)
4320 #if defined(CONFIG_USER_ONLY)
4321 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
4323 if (unlikely(!ctx->mem_idx)) {
4324 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
4332 GEN_HANDLER(tlbie, 0x1F, 0x12, 0x09, 0x03FF0001, PPC_MEM_TLBIE)
4334 #if defined(CONFIG_USER_ONLY)
4335 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
4337 if (unlikely(!ctx->mem_idx)) {
4338 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
4341 #if defined(TARGET_PPC64)
4342 if (!ctx->sf_mode) {
4343 TCGv t0 = tcg_temp_new();
4344 tcg_gen_ext32u_tl(t0, cpu_gpr[rB(ctx->opcode)]);
4345 gen_helper_tlbie(t0);
4349 gen_helper_tlbie(cpu_gpr[rB(ctx->opcode)]);
4354 GEN_HANDLER(tlbsync, 0x1F, 0x16, 0x11, 0x03FFF801, PPC_MEM_TLBSYNC)
4356 #if defined(CONFIG_USER_ONLY)
4357 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
4359 if (unlikely(!ctx->mem_idx)) {
4360 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
4363 /* This has no effect: it should ensure that all previous
4364 * tlbie have completed
4366 gen_stop_exception(ctx);
4370 #if defined(TARGET_PPC64)
4372 GEN_HANDLER(slbia, 0x1F, 0x12, 0x0F, 0x03FFFC01, PPC_SLBI)
4374 #if defined(CONFIG_USER_ONLY)
4375 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
4377 if (unlikely(!ctx->mem_idx)) {
4378 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
4386 GEN_HANDLER(slbie, 0x1F, 0x12, 0x0D, 0x03FF0001, PPC_SLBI)
4388 #if defined(CONFIG_USER_ONLY)
4389 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
4391 if (unlikely(!ctx->mem_idx)) {
4392 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
4395 gen_helper_slbie(cpu_gpr[rB(ctx->opcode)]);
4400 /*** External control ***/
4403 GEN_HANDLER(eciwx, 0x1F, 0x16, 0x0D, 0x00000001, PPC_EXTERN)
4406 /* Should check EAR[E] ! */
4407 gen_set_access_type(ctx, ACCESS_EXT);
4408 t0 = tcg_temp_new();
4409 gen_addr_reg_index(ctx, t0);
4410 gen_check_align(ctx, t0, 0x03);
4411 gen_qemu_ld32u(ctx, cpu_gpr[rD(ctx->opcode)], t0);
4416 GEN_HANDLER(ecowx, 0x1F, 0x16, 0x09, 0x00000001, PPC_EXTERN)
4419 /* Should check EAR[E] ! */
4420 gen_set_access_type(ctx, ACCESS_EXT);
4421 t0 = tcg_temp_new();
4422 gen_addr_reg_index(ctx, t0);
4423 gen_check_align(ctx, t0, 0x03);
4424 gen_qemu_st32(ctx, cpu_gpr[rD(ctx->opcode)], t0);
4428 /* PowerPC 601 specific instructions */
4430 GEN_HANDLER(abs, 0x1F, 0x08, 0x0B, 0x0000F800, PPC_POWER_BR)
4432 int l1 = gen_new_label();
4433 int l2 = gen_new_label();
4434 tcg_gen_brcondi_tl(TCG_COND_GE, cpu_gpr[rA(ctx->opcode)], 0, l1);
4435 tcg_gen_neg_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)]);
4438 tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)]);
4440 if (unlikely(Rc(ctx->opcode) != 0))
4441 gen_set_Rc0(ctx, cpu_gpr[rD(ctx->opcode)]);
4445 GEN_HANDLER(abso, 0x1F, 0x08, 0x1B, 0x0000F800, PPC_POWER_BR)
4447 int l1 = gen_new_label();
4448 int l2 = gen_new_label();
4449 int l3 = gen_new_label();
4450 /* Start with XER OV disabled, the most likely case */
4451 tcg_gen_andi_tl(cpu_xer, cpu_xer, ~(1 << XER_OV));
4452 tcg_gen_brcondi_tl(TCG_COND_GE, cpu_gpr[rA(ctx->opcode)], 0, l2);
4453 tcg_gen_brcondi_tl(TCG_COND_NE, cpu_gpr[rA(ctx->opcode)], 0x80000000, l1);
4454 tcg_gen_ori_tl(cpu_xer, cpu_xer, (1 << XER_OV) | (1 << XER_SO));
4457 tcg_gen_neg_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)]);
4460 tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)]);
4462 if (unlikely(Rc(ctx->opcode) != 0))
4463 gen_set_Rc0(ctx, cpu_gpr[rD(ctx->opcode)]);
4467 GEN_HANDLER(clcs, 0x1F, 0x10, 0x13, 0x0000F800, PPC_POWER_BR)
4469 TCGv_i32 t0 = tcg_const_i32(rA(ctx->opcode));
4470 gen_helper_clcs(cpu_gpr[rD(ctx->opcode)], t0);
4471 tcg_temp_free_i32(t0);
4472 /* Rc=1 sets CR0 to an undefined state */
4476 GEN_HANDLER(div, 0x1F, 0x0B, 0x0A, 0x00000000, PPC_POWER_BR)
4478 gen_helper_div(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)], cpu_gpr[rB(ctx->opcode)]);
4479 if (unlikely(Rc(ctx->opcode) != 0))
4480 gen_set_Rc0(ctx, cpu_gpr[rD(ctx->opcode)]);
4484 GEN_HANDLER(divo, 0x1F, 0x0B, 0x1A, 0x00000000, PPC_POWER_BR)
4486 gen_helper_divo(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)], cpu_gpr[rB(ctx->opcode)]);
4487 if (unlikely(Rc(ctx->opcode) != 0))
4488 gen_set_Rc0(ctx, cpu_gpr[rD(ctx->opcode)]);
4492 GEN_HANDLER(divs, 0x1F, 0x0B, 0x0B, 0x00000000, PPC_POWER_BR)
4494 gen_helper_divs(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)], cpu_gpr[rB(ctx->opcode)]);
4495 if (unlikely(Rc(ctx->opcode) != 0))
4496 gen_set_Rc0(ctx, cpu_gpr[rD(ctx->opcode)]);
4499 /* divso - divso. */
4500 GEN_HANDLER(divso, 0x1F, 0x0B, 0x1B, 0x00000000, PPC_POWER_BR)
4502 gen_helper_divso(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)], cpu_gpr[rB(ctx->opcode)]);
4503 if (unlikely(Rc(ctx->opcode) != 0))
4504 gen_set_Rc0(ctx, cpu_gpr[rD(ctx->opcode)]);
4508 GEN_HANDLER(doz, 0x1F, 0x08, 0x08, 0x00000000, PPC_POWER_BR)
4510 int l1 = gen_new_label();
4511 int l2 = gen_new_label();
4512 tcg_gen_brcond_tl(TCG_COND_GE, cpu_gpr[rB(ctx->opcode)], cpu_gpr[rA(ctx->opcode)], l1);
4513 tcg_gen_sub_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rB(ctx->opcode)], cpu_gpr[rA(ctx->opcode)]);
4516 tcg_gen_movi_tl(cpu_gpr[rD(ctx->opcode)], 0);
4518 if (unlikely(Rc(ctx->opcode) != 0))
4519 gen_set_Rc0(ctx, cpu_gpr[rD(ctx->opcode)]);
4523 GEN_HANDLER(dozo, 0x1F, 0x08, 0x18, 0x00000000, PPC_POWER_BR)
4525 int l1 = gen_new_label();
4526 int l2 = gen_new_label();
4527 TCGv t0 = tcg_temp_new();
4528 TCGv t1 = tcg_temp_new();
4529 TCGv t2 = tcg_temp_new();
4530 /* Start with XER OV disabled, the most likely case */
4531 tcg_gen_andi_tl(cpu_xer, cpu_xer, ~(1 << XER_OV));
4532 tcg_gen_brcond_tl(TCG_COND_GE, cpu_gpr[rB(ctx->opcode)], cpu_gpr[rA(ctx->opcode)], l1);
4533 tcg_gen_sub_tl(t0, cpu_gpr[rB(ctx->opcode)], cpu_gpr[rA(ctx->opcode)]);
4534 tcg_gen_xor_tl(t1, cpu_gpr[rB(ctx->opcode)], cpu_gpr[rA(ctx->opcode)]);
4535 tcg_gen_xor_tl(t2, cpu_gpr[rA(ctx->opcode)], t0);
4536 tcg_gen_andc_tl(t1, t1, t2);
4537 tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], t0);
4538 tcg_gen_brcondi_tl(TCG_COND_GE, t1, 0, l2);
4539 tcg_gen_ori_tl(cpu_xer, cpu_xer, (1 << XER_OV) | (1 << XER_SO));
4542 tcg_gen_movi_tl(cpu_gpr[rD(ctx->opcode)], 0);
4547 if (unlikely(Rc(ctx->opcode) != 0))
4548 gen_set_Rc0(ctx, cpu_gpr[rD(ctx->opcode)]);
4552 GEN_HANDLER(dozi, 0x09, 0xFF, 0xFF, 0x00000000, PPC_POWER_BR)
4554 target_long simm = SIMM(ctx->opcode);
4555 int l1 = gen_new_label();
4556 int l2 = gen_new_label();
4557 tcg_gen_brcondi_tl(TCG_COND_LT, cpu_gpr[rA(ctx->opcode)], simm, l1);
4558 tcg_gen_subfi_tl(cpu_gpr[rD(ctx->opcode)], simm, cpu_gpr[rA(ctx->opcode)]);
4561 tcg_gen_movi_tl(cpu_gpr[rD(ctx->opcode)], 0);
4563 if (unlikely(Rc(ctx->opcode) != 0))
4564 gen_set_Rc0(ctx, cpu_gpr[rD(ctx->opcode)]);
4567 /* lscbx - lscbx. */
4568 GEN_HANDLER(lscbx, 0x1F, 0x15, 0x08, 0x00000000, PPC_POWER_BR)
4570 TCGv t0 = tcg_temp_new();
4571 TCGv_i32 t1 = tcg_const_i32(rD(ctx->opcode));
4572 TCGv_i32 t2 = tcg_const_i32(rA(ctx->opcode));
4573 TCGv_i32 t3 = tcg_const_i32(rB(ctx->opcode));
4575 gen_addr_reg_index(ctx, t0);
4576 /* NIP cannot be restored if the memory exception comes from an helper */
4577 gen_update_nip(ctx, ctx->nip - 4);
4578 gen_helper_lscbx(t0, t0, t1, t2, t3);
4579 tcg_temp_free_i32(t1);
4580 tcg_temp_free_i32(t2);
4581 tcg_temp_free_i32(t3);
4582 tcg_gen_andi_tl(cpu_xer, cpu_xer, ~0x7F);
4583 tcg_gen_or_tl(cpu_xer, cpu_xer, t0);
4584 if (unlikely(Rc(ctx->opcode) != 0))
4585 gen_set_Rc0(ctx, t0);
4589 /* maskg - maskg. */
4590 GEN_HANDLER(maskg, 0x1F, 0x1D, 0x00, 0x00000000, PPC_POWER_BR)
4592 int l1 = gen_new_label();
4593 TCGv t0 = tcg_temp_new();
4594 TCGv t1 = tcg_temp_new();
4595 TCGv t2 = tcg_temp_new();
4596 TCGv t3 = tcg_temp_new();
4597 tcg_gen_movi_tl(t3, 0xFFFFFFFF);
4598 tcg_gen_andi_tl(t0, cpu_gpr[rB(ctx->opcode)], 0x1F);
4599 tcg_gen_andi_tl(t1, cpu_gpr[rS(ctx->opcode)], 0x1F);
4600 tcg_gen_addi_tl(t2, t0, 1);
4601 tcg_gen_shr_tl(t2, t3, t2);
4602 tcg_gen_shr_tl(t3, t3, t1);
4603 tcg_gen_xor_tl(cpu_gpr[rA(ctx->opcode)], t2, t3);
4604 tcg_gen_brcond_tl(TCG_COND_GE, t0, t1, l1);
4605 tcg_gen_neg_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rA(ctx->opcode)]);
4611 if (unlikely(Rc(ctx->opcode) != 0))
4612 gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
4615 /* maskir - maskir. */
4616 GEN_HANDLER(maskir, 0x1F, 0x1D, 0x10, 0x00000000, PPC_POWER_BR)
4618 TCGv t0 = tcg_temp_new();
4619 TCGv t1 = tcg_temp_new();
4620 tcg_gen_and_tl(t0, cpu_gpr[rS(ctx->opcode)], cpu_gpr[rB(ctx->opcode)]);
4621 tcg_gen_andc_tl(t1, cpu_gpr[rA(ctx->opcode)], cpu_gpr[rB(ctx->opcode)]);
4622 tcg_gen_or_tl(cpu_gpr[rA(ctx->opcode)], t0, t1);
4625 if (unlikely(Rc(ctx->opcode) != 0))
4626 gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
4630 GEN_HANDLER(mul, 0x1F, 0x0B, 0x03, 0x00000000, PPC_POWER_BR)
4632 TCGv_i64 t0 = tcg_temp_new_i64();
4633 TCGv_i64 t1 = tcg_temp_new_i64();
4634 TCGv t2 = tcg_temp_new();
4635 tcg_gen_extu_tl_i64(t0, cpu_gpr[rA(ctx->opcode)]);
4636 tcg_gen_extu_tl_i64(t1, cpu_gpr[rB(ctx->opcode)]);
4637 tcg_gen_mul_i64(t0, t0, t1);
4638 tcg_gen_trunc_i64_tl(t2, t0);
4639 gen_store_spr(SPR_MQ, t2);
4640 tcg_gen_shri_i64(t1, t0, 32);
4641 tcg_gen_trunc_i64_tl(cpu_gpr[rD(ctx->opcode)], t1);
4642 tcg_temp_free_i64(t0);
4643 tcg_temp_free_i64(t1);
4645 if (unlikely(Rc(ctx->opcode) != 0))
4646 gen_set_Rc0(ctx, cpu_gpr[rD(ctx->opcode)]);
4650 GEN_HANDLER(mulo, 0x1F, 0x0B, 0x13, 0x00000000, PPC_POWER_BR)
4652 int l1 = gen_new_label();
4653 TCGv_i64 t0 = tcg_temp_new_i64();
4654 TCGv_i64 t1 = tcg_temp_new_i64();
4655 TCGv t2 = tcg_temp_new();
4656 /* Start with XER OV disabled, the most likely case */
4657 tcg_gen_andi_tl(cpu_xer, cpu_xer, ~(1 << XER_OV));
4658 tcg_gen_extu_tl_i64(t0, cpu_gpr[rA(ctx->opcode)]);
4659 tcg_gen_extu_tl_i64(t1, cpu_gpr[rB(ctx->opcode)]);
4660 tcg_gen_mul_i64(t0, t0, t1);
4661 tcg_gen_trunc_i64_tl(t2, t0);
4662 gen_store_spr(SPR_MQ, t2);
4663 tcg_gen_shri_i64(t1, t0, 32);
4664 tcg_gen_trunc_i64_tl(cpu_gpr[rD(ctx->opcode)], t1);
4665 tcg_gen_ext32s_i64(t1, t0);
4666 tcg_gen_brcond_i64(TCG_COND_EQ, t0, t1, l1);
4667 tcg_gen_ori_tl(cpu_xer, cpu_xer, (1 << XER_OV) | (1 << XER_SO));
4669 tcg_temp_free_i64(t0);
4670 tcg_temp_free_i64(t1);
4672 if (unlikely(Rc(ctx->opcode) != 0))
4673 gen_set_Rc0(ctx, cpu_gpr[rD(ctx->opcode)]);
4677 GEN_HANDLER(nabs, 0x1F, 0x08, 0x0F, 0x00000000, PPC_POWER_BR)
4679 int l1 = gen_new_label();
4680 int l2 = gen_new_label();
4681 tcg_gen_brcondi_tl(TCG_COND_GT, cpu_gpr[rA(ctx->opcode)], 0, l1);
4682 tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)]);
4685 tcg_gen_neg_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)]);
4687 if (unlikely(Rc(ctx->opcode) != 0))
4688 gen_set_Rc0(ctx, cpu_gpr[rD(ctx->opcode)]);
4691 /* nabso - nabso. */
4692 GEN_HANDLER(nabso, 0x1F, 0x08, 0x1F, 0x00000000, PPC_POWER_BR)
4694 int l1 = gen_new_label();
4695 int l2 = gen_new_label();
4696 tcg_gen_brcondi_tl(TCG_COND_GT, cpu_gpr[rA(ctx->opcode)], 0, l1);
4697 tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)]);
4700 tcg_gen_neg_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)]);
4702 /* nabs never overflows */
4703 tcg_gen_andi_tl(cpu_xer, cpu_xer, ~(1 << XER_OV));
4704 if (unlikely(Rc(ctx->opcode) != 0))
4705 gen_set_Rc0(ctx, cpu_gpr[rD(ctx->opcode)]);
4709 GEN_HANDLER(rlmi, 0x16, 0xFF, 0xFF, 0x00000000, PPC_POWER_BR)
4711 uint32_t mb = MB(ctx->opcode);
4712 uint32_t me = ME(ctx->opcode);
4713 TCGv t0 = tcg_temp_new();
4714 tcg_gen_andi_tl(t0, cpu_gpr[rB(ctx->opcode)], 0x1F);
4715 tcg_gen_rotl_tl(t0, cpu_gpr[rS(ctx->opcode)], t0);
4716 tcg_gen_andi_tl(t0, t0, MASK(mb, me));
4717 tcg_gen_andi_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rA(ctx->opcode)], ~MASK(mb, me));
4718 tcg_gen_or_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rA(ctx->opcode)], t0);
4720 if (unlikely(Rc(ctx->opcode) != 0))
4721 gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
4725 GEN_HANDLER(rrib, 0x1F, 0x19, 0x10, 0x00000000, PPC_POWER_BR)
4727 TCGv t0 = tcg_temp_new();
4728 TCGv t1 = tcg_temp_new();
4729 tcg_gen_andi_tl(t0, cpu_gpr[rB(ctx->opcode)], 0x1F);
4730 tcg_gen_movi_tl(t1, 0x80000000);
4731 tcg_gen_shr_tl(t1, t1, t0);
4732 tcg_gen_shr_tl(t0, cpu_gpr[rS(ctx->opcode)], t0);
4733 tcg_gen_and_tl(t0, t0, t1);
4734 tcg_gen_andc_tl(t1, cpu_gpr[rA(ctx->opcode)], t1);
4735 tcg_gen_or_tl(cpu_gpr[rA(ctx->opcode)], t0, t1);
4738 if (unlikely(Rc(ctx->opcode) != 0))
4739 gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
4743 GEN_HANDLER(sle, 0x1F, 0x19, 0x04, 0x00000000, PPC_POWER_BR)
4745 TCGv t0 = tcg_temp_new();
4746 TCGv t1 = tcg_temp_new();
4747 tcg_gen_andi_tl(t1, cpu_gpr[rB(ctx->opcode)], 0x1F);
4748 tcg_gen_shl_tl(t0, cpu_gpr[rS(ctx->opcode)], t1);
4749 tcg_gen_subfi_tl(t1, 32, t1);
4750 tcg_gen_shr_tl(t1, cpu_gpr[rS(ctx->opcode)], t1);
4751 tcg_gen_or_tl(t1, t0, t1);
4752 tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], t0);
4753 gen_store_spr(SPR_MQ, t1);
4756 if (unlikely(Rc(ctx->opcode) != 0))
4757 gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
4761 GEN_HANDLER(sleq, 0x1F, 0x19, 0x06, 0x00000000, PPC_POWER_BR)
4763 TCGv t0 = tcg_temp_new();
4764 TCGv t1 = tcg_temp_new();
4765 TCGv t2 = tcg_temp_new();
4766 tcg_gen_andi_tl(t0, cpu_gpr[rB(ctx->opcode)], 0x1F);
4767 tcg_gen_movi_tl(t2, 0xFFFFFFFF);
4768 tcg_gen_shl_tl(t2, t2, t0);
4769 tcg_gen_rotl_tl(t0, cpu_gpr[rS(ctx->opcode)], t0);
4770 gen_load_spr(t1, SPR_MQ);
4771 gen_store_spr(SPR_MQ, t0);
4772 tcg_gen_and_tl(t0, t0, t2);
4773 tcg_gen_andc_tl(t1, t1, t2);
4774 tcg_gen_or_tl(cpu_gpr[rA(ctx->opcode)], t0, t1);
4778 if (unlikely(Rc(ctx->opcode) != 0))
4779 gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
4783 GEN_HANDLER(sliq, 0x1F, 0x18, 0x05, 0x00000000, PPC_POWER_BR)
4785 int sh = SH(ctx->opcode);
4786 TCGv t0 = tcg_temp_new();
4787 TCGv t1 = tcg_temp_new();
4788 tcg_gen_shli_tl(t0, cpu_gpr[rS(ctx->opcode)], sh);
4789 tcg_gen_shri_tl(t1, cpu_gpr[rS(ctx->opcode)], 32 - sh);
4790 tcg_gen_or_tl(t1, t0, t1);
4791 tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], t0);
4792 gen_store_spr(SPR_MQ, t1);
4795 if (unlikely(Rc(ctx->opcode) != 0))
4796 gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
4799 /* slliq - slliq. */
4800 GEN_HANDLER(slliq, 0x1F, 0x18, 0x07, 0x00000000, PPC_POWER_BR)
4802 int sh = SH(ctx->opcode);
4803 TCGv t0 = tcg_temp_new();
4804 TCGv t1 = tcg_temp_new();
4805 tcg_gen_rotli_tl(t0, cpu_gpr[rS(ctx->opcode)], sh);
4806 gen_load_spr(t1, SPR_MQ);
4807 gen_store_spr(SPR_MQ, t0);
4808 tcg_gen_andi_tl(t0, t0, (0xFFFFFFFFU << sh));
4809 tcg_gen_andi_tl(t1, t1, ~(0xFFFFFFFFU << sh));
4810 tcg_gen_or_tl(cpu_gpr[rA(ctx->opcode)], t0, t1);
4813 if (unlikely(Rc(ctx->opcode) != 0))
4814 gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
4818 GEN_HANDLER(sllq, 0x1F, 0x18, 0x06, 0x00000000, PPC_POWER_BR)
4820 int l1 = gen_new_label();
4821 int l2 = gen_new_label();
4822 TCGv t0 = tcg_temp_local_new();
4823 TCGv t1 = tcg_temp_local_new();
4824 TCGv t2 = tcg_temp_local_new();
4825 tcg_gen_andi_tl(t2, cpu_gpr[rB(ctx->opcode)], 0x1F);
4826 tcg_gen_movi_tl(t1, 0xFFFFFFFF);
4827 tcg_gen_shl_tl(t1, t1, t2);
4828 tcg_gen_andi_tl(t0, cpu_gpr[rB(ctx->opcode)], 0x20);
4829 tcg_gen_brcondi_tl(TCG_COND_EQ, t0, 0, l1);
4830 gen_load_spr(t0, SPR_MQ);
4831 tcg_gen_and_tl(cpu_gpr[rA(ctx->opcode)], t0, t1);
4834 tcg_gen_shl_tl(t0, cpu_gpr[rS(ctx->opcode)], t2);
4835 gen_load_spr(t2, SPR_MQ);
4836 tcg_gen_andc_tl(t1, t2, t1);
4837 tcg_gen_or_tl(cpu_gpr[rA(ctx->opcode)], t0, t1);
4842 if (unlikely(Rc(ctx->opcode) != 0))
4843 gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
4847 GEN_HANDLER(slq, 0x1F, 0x18, 0x04, 0x00000000, PPC_POWER_BR)
4849 int l1 = gen_new_label();
4850 TCGv t0 = tcg_temp_new();
4851 TCGv t1 = tcg_temp_new();
4852 tcg_gen_andi_tl(t1, cpu_gpr[rB(ctx->opcode)], 0x1F);
4853 tcg_gen_shl_tl(t0, cpu_gpr[rS(ctx->opcode)], t1);
4854 tcg_gen_subfi_tl(t1, 32, t1);
4855 tcg_gen_shr_tl(t1, cpu_gpr[rS(ctx->opcode)], t1);
4856 tcg_gen_or_tl(t1, t0, t1);
4857 gen_store_spr(SPR_MQ, t1);
4858 tcg_gen_andi_tl(t1, cpu_gpr[rB(ctx->opcode)], 0x20);
4859 tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], t0);
4860 tcg_gen_brcondi_tl(TCG_COND_EQ, t1, 0, l1);
4861 tcg_gen_movi_tl(cpu_gpr[rA(ctx->opcode)], 0);
4865 if (unlikely(Rc(ctx->opcode) != 0))
4866 gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
4869 /* sraiq - sraiq. */
4870 GEN_HANDLER(sraiq, 0x1F, 0x18, 0x1D, 0x00000000, PPC_POWER_BR)
4872 int sh = SH(ctx->opcode);
4873 int l1 = gen_new_label();
4874 TCGv t0 = tcg_temp_new();
4875 TCGv t1 = tcg_temp_new();
4876 tcg_gen_shri_tl(t0, cpu_gpr[rS(ctx->opcode)], sh);
4877 tcg_gen_shli_tl(t1, cpu_gpr[rS(ctx->opcode)], 32 - sh);
4878 tcg_gen_or_tl(t0, t0, t1);
4879 gen_store_spr(SPR_MQ, t0);
4880 tcg_gen_andi_tl(cpu_xer, cpu_xer, ~(1 << XER_CA));
4881 tcg_gen_brcondi_tl(TCG_COND_EQ, t1, 0, l1);
4882 tcg_gen_brcondi_tl(TCG_COND_GE, cpu_gpr[rS(ctx->opcode)], 0, l1);
4883 tcg_gen_ori_tl(cpu_xer, cpu_xer, (1 << XER_CA));
4885 tcg_gen_sari_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)], sh);
4888 if (unlikely(Rc(ctx->opcode) != 0))
4889 gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
4893 GEN_HANDLER(sraq, 0x1F, 0x18, 0x1C, 0x00000000, PPC_POWER_BR)
4895 int l1 = gen_new_label();
4896 int l2 = gen_new_label();
4897 TCGv t0 = tcg_temp_new();
4898 TCGv t1 = tcg_temp_local_new();
4899 TCGv t2 = tcg_temp_local_new();
4900 tcg_gen_andi_tl(t2, cpu_gpr[rB(ctx->opcode)], 0x1F);
4901 tcg_gen_shr_tl(t0, cpu_gpr[rS(ctx->opcode)], t2);
4902 tcg_gen_sar_tl(t1, cpu_gpr[rS(ctx->opcode)], t2);
4903 tcg_gen_subfi_tl(t2, 32, t2);
4904 tcg_gen_shl_tl(t2, cpu_gpr[rS(ctx->opcode)], t2);
4905 tcg_gen_or_tl(t0, t0, t2);
4906 gen_store_spr(SPR_MQ, t0);
4907 tcg_gen_andi_tl(t0, cpu_gpr[rB(ctx->opcode)], 0x20);
4908 tcg_gen_brcondi_tl(TCG_COND_EQ, t2, 0, l1);
4909 tcg_gen_mov_tl(t2, cpu_gpr[rS(ctx->opcode)]);
4910 tcg_gen_sari_tl(t1, cpu_gpr[rS(ctx->opcode)], 31);
4913 tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], t1);
4914 tcg_gen_andi_tl(cpu_xer, cpu_xer, ~(1 << XER_CA));
4915 tcg_gen_brcondi_tl(TCG_COND_GE, t1, 0, l2);
4916 tcg_gen_brcondi_tl(TCG_COND_EQ, t2, 0, l2);
4917 tcg_gen_ori_tl(cpu_xer, cpu_xer, (1 << XER_CA));
4921 if (unlikely(Rc(ctx->opcode) != 0))
4922 gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
4926 GEN_HANDLER(sre, 0x1F, 0x19, 0x14, 0x00000000, PPC_POWER_BR)
4928 TCGv t0 = tcg_temp_new();
4929 TCGv t1 = tcg_temp_new();
4930 tcg_gen_andi_tl(t1, cpu_gpr[rB(ctx->opcode)], 0x1F);
4931 tcg_gen_shr_tl(t0, cpu_gpr[rS(ctx->opcode)], t1);
4932 tcg_gen_subfi_tl(t1, 32, t1);
4933 tcg_gen_shl_tl(t1, cpu_gpr[rS(ctx->opcode)], t1);
4934 tcg_gen_or_tl(t1, t0, t1);
4935 tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], t0);
4936 gen_store_spr(SPR_MQ, t1);
4939 if (unlikely(Rc(ctx->opcode) != 0))
4940 gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
4944 GEN_HANDLER(srea, 0x1F, 0x19, 0x1C, 0x00000000, PPC_POWER_BR)
4946 TCGv t0 = tcg_temp_new();
4947 TCGv t1 = tcg_temp_new();
4948 tcg_gen_andi_tl(t1, cpu_gpr[rB(ctx->opcode)], 0x1F);
4949 tcg_gen_rotr_tl(t0, cpu_gpr[rS(ctx->opcode)], t1);
4950 gen_store_spr(SPR_MQ, t0);
4951 tcg_gen_sar_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)], t1);
4954 if (unlikely(Rc(ctx->opcode) != 0))
4955 gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
4959 GEN_HANDLER(sreq, 0x1F, 0x19, 0x16, 0x00000000, PPC_POWER_BR)
4961 TCGv t0 = tcg_temp_new();
4962 TCGv t1 = tcg_temp_new();
4963 TCGv t2 = tcg_temp_new();
4964 tcg_gen_andi_tl(t0, cpu_gpr[rB(ctx->opcode)], 0x1F);
4965 tcg_gen_movi_tl(t1, 0xFFFFFFFF);
4966 tcg_gen_shr_tl(t1, t1, t0);
4967 tcg_gen_rotr_tl(t0, cpu_gpr[rS(ctx->opcode)], t0);
4968 gen_load_spr(t2, SPR_MQ);
4969 gen_store_spr(SPR_MQ, t0);
4970 tcg_gen_and_tl(t0, t0, t1);
4971 tcg_gen_andc_tl(t2, t2, t1);
4972 tcg_gen_or_tl(cpu_gpr[rA(ctx->opcode)], t0, t2);
4976 if (unlikely(Rc(ctx->opcode) != 0))
4977 gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
4981 GEN_HANDLER(sriq, 0x1F, 0x18, 0x15, 0x00000000, PPC_POWER_BR)
4983 int sh = SH(ctx->opcode);
4984 TCGv t0 = tcg_temp_new();
4985 TCGv t1 = tcg_temp_new();
4986 tcg_gen_shri_tl(t0, cpu_gpr[rS(ctx->opcode)], sh);
4987 tcg_gen_shli_tl(t1, cpu_gpr[rS(ctx->opcode)], 32 - sh);
4988 tcg_gen_or_tl(t1, t0, t1);
4989 tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], t0);
4990 gen_store_spr(SPR_MQ, t1);
4993 if (unlikely(Rc(ctx->opcode) != 0))
4994 gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
4998 GEN_HANDLER(srliq, 0x1F, 0x18, 0x17, 0x00000000, PPC_POWER_BR)
5000 int sh = SH(ctx->opcode);
5001 TCGv t0 = tcg_temp_new();
5002 TCGv t1 = tcg_temp_new();
5003 tcg_gen_rotri_tl(t0, cpu_gpr[rS(ctx->opcode)], sh);
5004 gen_load_spr(t1, SPR_MQ);
5005 gen_store_spr(SPR_MQ, t0);
5006 tcg_gen_andi_tl(t0, t0, (0xFFFFFFFFU >> sh));
5007 tcg_gen_andi_tl(t1, t1, ~(0xFFFFFFFFU >> sh));
5008 tcg_gen_or_tl(cpu_gpr[rA(ctx->opcode)], t0, t1);
5011 if (unlikely(Rc(ctx->opcode) != 0))
5012 gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
5016 GEN_HANDLER(srlq, 0x1F, 0x18, 0x16, 0x00000000, PPC_POWER_BR)
5018 int l1 = gen_new_label();
5019 int l2 = gen_new_label();
5020 TCGv t0 = tcg_temp_local_new();
5021 TCGv t1 = tcg_temp_local_new();
5022 TCGv t2 = tcg_temp_local_new();
5023 tcg_gen_andi_tl(t2, cpu_gpr[rB(ctx->opcode)], 0x1F);
5024 tcg_gen_movi_tl(t1, 0xFFFFFFFF);
5025 tcg_gen_shr_tl(t2, t1, t2);
5026 tcg_gen_andi_tl(t0, cpu_gpr[rB(ctx->opcode)], 0x20);
5027 tcg_gen_brcondi_tl(TCG_COND_EQ, t0, 0, l1);
5028 gen_load_spr(t0, SPR_MQ);
5029 tcg_gen_and_tl(cpu_gpr[rA(ctx->opcode)], t0, t2);
5032 tcg_gen_shr_tl(t0, cpu_gpr[rS(ctx->opcode)], t2);
5033 tcg_gen_and_tl(t0, t0, t2);
5034 gen_load_spr(t1, SPR_MQ);
5035 tcg_gen_andc_tl(t1, t1, t2);
5036 tcg_gen_or_tl(cpu_gpr[rA(ctx->opcode)], t0, t1);
5041 if (unlikely(Rc(ctx->opcode) != 0))
5042 gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
5046 GEN_HANDLER(srq, 0x1F, 0x18, 0x14, 0x00000000, PPC_POWER_BR)
5048 int l1 = gen_new_label();
5049 TCGv t0 = tcg_temp_new();
5050 TCGv t1 = tcg_temp_new();
5051 tcg_gen_andi_tl(t1, cpu_gpr[rB(ctx->opcode)], 0x1F);
5052 tcg_gen_shr_tl(t0, cpu_gpr[rS(ctx->opcode)], t1);
5053 tcg_gen_subfi_tl(t1, 32, t1);
5054 tcg_gen_shl_tl(t1, cpu_gpr[rS(ctx->opcode)], t1);
5055 tcg_gen_or_tl(t1, t0, t1);
5056 gen_store_spr(SPR_MQ, t1);
5057 tcg_gen_andi_tl(t1, cpu_gpr[rB(ctx->opcode)], 0x20);
5058 tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], t0);
5059 tcg_gen_brcondi_tl(TCG_COND_EQ, t0, 0, l1);
5060 tcg_gen_movi_tl(cpu_gpr[rA(ctx->opcode)], 0);
5064 if (unlikely(Rc(ctx->opcode) != 0))
5065 gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
5068 /* PowerPC 602 specific instructions */
5070 GEN_HANDLER(dsa, 0x1F, 0x14, 0x13, 0x03FFF801, PPC_602_SPEC)
5073 gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL);
5077 GEN_HANDLER(esa, 0x1F, 0x14, 0x12, 0x03FFF801, PPC_602_SPEC)
5080 gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL);
5084 GEN_HANDLER(mfrom, 0x1F, 0x09, 0x08, 0x03E0F801, PPC_602_SPEC)
5086 #if defined(CONFIG_USER_ONLY)
5087 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
5089 if (unlikely(!ctx->mem_idx)) {
5090 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
5093 gen_helper_602_mfrom(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)]);
5097 /* 602 - 603 - G2 TLB management */
5099 GEN_HANDLER2(tlbld_6xx, "tlbld", 0x1F, 0x12, 0x1E, 0x03FF0001, PPC_6xx_TLB)
5101 #if defined(CONFIG_USER_ONLY)
5102 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
5104 if (unlikely(!ctx->mem_idx)) {
5105 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
5108 gen_helper_6xx_tlbd(cpu_gpr[rB(ctx->opcode)]);
5113 GEN_HANDLER2(tlbli_6xx, "tlbli", 0x1F, 0x12, 0x1F, 0x03FF0001, PPC_6xx_TLB)
5115 #if defined(CONFIG_USER_ONLY)
5116 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
5118 if (unlikely(!ctx->mem_idx)) {
5119 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
5122 gen_helper_6xx_tlbi(cpu_gpr[rB(ctx->opcode)]);
5126 /* 74xx TLB management */
5128 GEN_HANDLER2(tlbld_74xx, "tlbld", 0x1F, 0x12, 0x1E, 0x03FF0001, PPC_74xx_TLB)
5130 #if defined(CONFIG_USER_ONLY)
5131 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
5133 if (unlikely(!ctx->mem_idx)) {
5134 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
5137 gen_helper_74xx_tlbd(cpu_gpr[rB(ctx->opcode)]);
5142 GEN_HANDLER2(tlbli_74xx, "tlbli", 0x1F, 0x12, 0x1F, 0x03FF0001, PPC_74xx_TLB)
5144 #if defined(CONFIG_USER_ONLY)
5145 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
5147 if (unlikely(!ctx->mem_idx)) {
5148 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
5151 gen_helper_74xx_tlbi(cpu_gpr[rB(ctx->opcode)]);
5155 /* POWER instructions not in PowerPC 601 */
5157 GEN_HANDLER(clf, 0x1F, 0x16, 0x03, 0x03E00000, PPC_POWER)
5159 /* Cache line flush: implemented as no-op */
5163 GEN_HANDLER(cli, 0x1F, 0x16, 0x0F, 0x03E00000, PPC_POWER)
5165 /* Cache line invalidate: privileged and treated as no-op */
5166 #if defined(CONFIG_USER_ONLY)
5167 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
5169 if (unlikely(!ctx->mem_idx)) {
5170 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
5177 GEN_HANDLER(dclst, 0x1F, 0x16, 0x13, 0x03E00000, PPC_POWER)
5179 /* Data cache line store: treated as no-op */
5182 GEN_HANDLER(mfsri, 0x1F, 0x13, 0x13, 0x00000001, PPC_POWER)
5184 #if defined(CONFIG_USER_ONLY)
5185 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
5187 int ra = rA(ctx->opcode);
5188 int rd = rD(ctx->opcode);
5190 if (unlikely(!ctx->mem_idx)) {
5191 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
5194 t0 = tcg_temp_new();
5195 gen_addr_reg_index(ctx, t0);
5196 tcg_gen_shri_tl(t0, t0, 28);
5197 tcg_gen_andi_tl(t0, t0, 0xF);
5198 gen_helper_load_sr(cpu_gpr[rd], t0);
5200 if (ra != 0 && ra != rd)
5201 tcg_gen_mov_tl(cpu_gpr[ra], cpu_gpr[rd]);
5205 GEN_HANDLER(rac, 0x1F, 0x12, 0x19, 0x00000001, PPC_POWER)
5207 #if defined(CONFIG_USER_ONLY)
5208 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
5211 if (unlikely(!ctx->mem_idx)) {
5212 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
5215 t0 = tcg_temp_new();
5216 gen_addr_reg_index(ctx, t0);
5217 gen_helper_rac(cpu_gpr[rD(ctx->opcode)], t0);
5222 GEN_HANDLER(rfsvc, 0x13, 0x12, 0x02, 0x03FFF0001, PPC_POWER)
5224 #if defined(CONFIG_USER_ONLY)
5225 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
5227 if (unlikely(!ctx->mem_idx)) {
5228 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
5232 gen_sync_exception(ctx);
5236 /* svc is not implemented for now */
5238 /* POWER2 specific instructions */
5239 /* Quad manipulation (load/store two floats at a time) */
5242 GEN_HANDLER(lfq, 0x38, 0xFF, 0xFF, 0x00000003, PPC_POWER2)
5244 int rd = rD(ctx->opcode);
5246 gen_set_access_type(ctx, ACCESS_FLOAT);
5247 t0 = tcg_temp_new();
5248 gen_addr_imm_index(ctx, t0, 0);
5249 gen_qemu_ld64(ctx, cpu_fpr[rd], t0);
5250 gen_addr_add(ctx, t0, t0, 8);
5251 gen_qemu_ld64(ctx, cpu_fpr[(rd + 1) % 32], t0);
5256 GEN_HANDLER(lfqu, 0x39, 0xFF, 0xFF, 0x00000003, PPC_POWER2)
5258 int ra = rA(ctx->opcode);
5259 int rd = rD(ctx->opcode);
5261 gen_set_access_type(ctx, ACCESS_FLOAT);
5262 t0 = tcg_temp_new();
5263 t1 = tcg_temp_new();
5264 gen_addr_imm_index(ctx, t0, 0);
5265 gen_qemu_ld64(ctx, cpu_fpr[rd], t0);
5266 gen_addr_add(ctx, t1, t0, 8);
5267 gen_qemu_ld64(ctx, cpu_fpr[(rd + 1) % 32], t1);
5269 tcg_gen_mov_tl(cpu_gpr[ra], t0);
5275 GEN_HANDLER(lfqux, 0x1F, 0x17, 0x19, 0x00000001, PPC_POWER2)
5277 int ra = rA(ctx->opcode);
5278 int rd = rD(ctx->opcode);
5279 gen_set_access_type(ctx, ACCESS_FLOAT);
5281 t0 = tcg_temp_new();
5282 gen_addr_reg_index(ctx, t0);
5283 gen_qemu_ld64(ctx, cpu_fpr[rd], t0);
5284 t1 = tcg_temp_new();
5285 gen_addr_add(ctx, t1, t0, 8);
5286 gen_qemu_ld64(ctx, cpu_fpr[(rd + 1) % 32], t1);
5289 tcg_gen_mov_tl(cpu_gpr[ra], t0);
5294 GEN_HANDLER(lfqx, 0x1F, 0x17, 0x18, 0x00000001, PPC_POWER2)
5296 int rd = rD(ctx->opcode);
5298 gen_set_access_type(ctx, ACCESS_FLOAT);
5299 t0 = tcg_temp_new();
5300 gen_addr_reg_index(ctx, t0);
5301 gen_qemu_ld64(ctx, cpu_fpr[rd], t0);
5302 gen_addr_add(ctx, t0, t0, 8);
5303 gen_qemu_ld64(ctx, cpu_fpr[(rd + 1) % 32], t0);
5308 GEN_HANDLER(stfq, 0x3C, 0xFF, 0xFF, 0x00000003, PPC_POWER2)
5310 int rd = rD(ctx->opcode);
5312 gen_set_access_type(ctx, ACCESS_FLOAT);
5313 t0 = tcg_temp_new();
5314 gen_addr_imm_index(ctx, t0, 0);
5315 gen_qemu_st64(ctx, cpu_fpr[rd], t0);
5316 gen_addr_add(ctx, t0, t0, 8);
5317 gen_qemu_st64(ctx, cpu_fpr[(rd + 1) % 32], t0);
5322 GEN_HANDLER(stfqu, 0x3D, 0xFF, 0xFF, 0x00000003, PPC_POWER2)
5324 int ra = rA(ctx->opcode);
5325 int rd = rD(ctx->opcode);
5327 gen_set_access_type(ctx, ACCESS_FLOAT);
5328 t0 = tcg_temp_new();
5329 gen_addr_imm_index(ctx, t0, 0);
5330 gen_qemu_st64(ctx, cpu_fpr[rd], t0);
5331 t1 = tcg_temp_new();
5332 gen_addr_add(ctx, t1, t0, 8);
5333 gen_qemu_st64(ctx, cpu_fpr[(rd + 1) % 32], t1);
5336 tcg_gen_mov_tl(cpu_gpr[ra], t0);
5341 GEN_HANDLER(stfqux, 0x1F, 0x17, 0x1D, 0x00000001, PPC_POWER2)
5343 int ra = rA(ctx->opcode);
5344 int rd = rD(ctx->opcode);
5346 gen_set_access_type(ctx, ACCESS_FLOAT);
5347 t0 = tcg_temp_new();
5348 gen_addr_reg_index(ctx, t0);
5349 gen_qemu_st64(ctx, cpu_fpr[rd], t0);
5350 t1 = tcg_temp_new();
5351 gen_addr_add(ctx, t1, t0, 8);
5352 gen_qemu_st64(ctx, cpu_fpr[(rd + 1) % 32], t1);
5355 tcg_gen_mov_tl(cpu_gpr[ra], t0);
5360 GEN_HANDLER(stfqx, 0x1F, 0x17, 0x1C, 0x00000001, PPC_POWER2)
5362 int rd = rD(ctx->opcode);
5364 gen_set_access_type(ctx, ACCESS_FLOAT);
5365 t0 = tcg_temp_new();
5366 gen_addr_reg_index(ctx, t0);
5367 gen_qemu_st64(ctx, cpu_fpr[rd], t0);
5368 gen_addr_add(ctx, t0, t0, 8);
5369 gen_qemu_st64(ctx, cpu_fpr[(rd + 1) % 32], t0);
5373 /* BookE specific instructions */
5374 /* XXX: not implemented on 440 ? */
5375 GEN_HANDLER(mfapidi, 0x1F, 0x13, 0x08, 0x0000F801, PPC_MFAPIDI)
5378 gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL);
5381 /* XXX: not implemented on 440 ? */
5382 GEN_HANDLER(tlbiva, 0x1F, 0x12, 0x18, 0x03FFF801, PPC_TLBIVA)
5384 #if defined(CONFIG_USER_ONLY)
5385 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
5388 if (unlikely(!ctx->mem_idx)) {
5389 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
5392 t0 = tcg_temp_new();
5393 gen_addr_reg_index(ctx, t0);
5394 gen_helper_tlbie(cpu_gpr[rB(ctx->opcode)]);
5399 /* All 405 MAC instructions are translated here */
5400 static always_inline void gen_405_mulladd_insn (DisasContext *ctx,
5402 int ra, int rb, int rt, int Rc)
5406 t0 = tcg_temp_local_new();
5407 t1 = tcg_temp_local_new();
5409 switch (opc3 & 0x0D) {
5411 /* macchw - macchw. - macchwo - macchwo. */
5412 /* macchws - macchws. - macchwso - macchwso. */
5413 /* nmacchw - nmacchw. - nmacchwo - nmacchwo. */
5414 /* nmacchws - nmacchws. - nmacchwso - nmacchwso. */
5415 /* mulchw - mulchw. */
5416 tcg_gen_ext16s_tl(t0, cpu_gpr[ra]);
5417 tcg_gen_sari_tl(t1, cpu_gpr[rb], 16);
5418 tcg_gen_ext16s_tl(t1, t1);
5421 /* macchwu - macchwu. - macchwuo - macchwuo. */
5422 /* macchwsu - macchwsu. - macchwsuo - macchwsuo. */
5423 /* mulchwu - mulchwu. */
5424 tcg_gen_ext16u_tl(t0, cpu_gpr[ra]);
5425 tcg_gen_shri_tl(t1, cpu_gpr[rb], 16);
5426 tcg_gen_ext16u_tl(t1, t1);
5429 /* machhw - machhw. - machhwo - machhwo. */
5430 /* machhws - machhws. - machhwso - machhwso. */
5431 /* nmachhw - nmachhw. - nmachhwo - nmachhwo. */
5432 /* nmachhws - nmachhws. - nmachhwso - nmachhwso. */
5433 /* mulhhw - mulhhw. */
5434 tcg_gen_sari_tl(t0, cpu_gpr[ra], 16);
5435 tcg_gen_ext16s_tl(t0, t0);
5436 tcg_gen_sari_tl(t1, cpu_gpr[rb], 16);
5437 tcg_gen_ext16s_tl(t1, t1);
5440 /* machhwu - machhwu. - machhwuo - machhwuo. */
5441 /* machhwsu - machhwsu. - machhwsuo - machhwsuo. */
5442 /* mulhhwu - mulhhwu. */
5443 tcg_gen_shri_tl(t0, cpu_gpr[ra], 16);
5444 tcg_gen_ext16u_tl(t0, t0);
5445 tcg_gen_shri_tl(t1, cpu_gpr[rb], 16);
5446 tcg_gen_ext16u_tl(t1, t1);
5449 /* maclhw - maclhw. - maclhwo - maclhwo. */
5450 /* maclhws - maclhws. - maclhwso - maclhwso. */
5451 /* nmaclhw - nmaclhw. - nmaclhwo - nmaclhwo. */
5452 /* nmaclhws - nmaclhws. - nmaclhwso - nmaclhwso. */
5453 /* mullhw - mullhw. */
5454 tcg_gen_ext16s_tl(t0, cpu_gpr[ra]);
5455 tcg_gen_ext16s_tl(t1, cpu_gpr[rb]);
5458 /* maclhwu - maclhwu. - maclhwuo - maclhwuo. */
5459 /* maclhwsu - maclhwsu. - maclhwsuo - maclhwsuo. */
5460 /* mullhwu - mullhwu. */
5461 tcg_gen_ext16u_tl(t0, cpu_gpr[ra]);
5462 tcg_gen_ext16u_tl(t1, cpu_gpr[rb]);
5466 /* (n)multiply-and-accumulate (0x0C / 0x0E) */
5467 tcg_gen_mul_tl(t1, t0, t1);
5469 /* nmultiply-and-accumulate (0x0E) */
5470 tcg_gen_sub_tl(t0, cpu_gpr[rt], t1);
5472 /* multiply-and-accumulate (0x0C) */
5473 tcg_gen_add_tl(t0, cpu_gpr[rt], t1);
5477 /* Check overflow and/or saturate */
5478 int l1 = gen_new_label();
5481 /* Start with XER OV disabled, the most likely case */
5482 tcg_gen_andi_tl(cpu_xer, cpu_xer, ~(1 << XER_OV));
5486 tcg_gen_xor_tl(t1, cpu_gpr[rt], t1);
5487 tcg_gen_brcondi_tl(TCG_COND_GE, t1, 0, l1);
5488 tcg_gen_xor_tl(t1, cpu_gpr[rt], t0);
5489 tcg_gen_brcondi_tl(TCG_COND_LT, t1, 0, l1);
5492 tcg_gen_sari_tl(t0, cpu_gpr[rt], 31);
5493 tcg_gen_xori_tl(t0, t0, 0x7fffffff);
5497 tcg_gen_brcond_tl(TCG_COND_GEU, t0, t1, l1);
5500 tcg_gen_movi_tl(t0, UINT32_MAX);
5504 /* Check overflow */
5505 tcg_gen_ori_tl(cpu_xer, cpu_xer, (1 << XER_OV) | (1 << XER_SO));
5508 tcg_gen_mov_tl(cpu_gpr[rt], t0);
5511 tcg_gen_mul_tl(cpu_gpr[rt], t0, t1);
5515 if (unlikely(Rc) != 0) {
5517 gen_set_Rc0(ctx, cpu_gpr[rt]);
5521 #define GEN_MAC_HANDLER(name, opc2, opc3) \
5522 GEN_HANDLER(name, 0x04, opc2, opc3, 0x00000000, PPC_405_MAC) \
5524 gen_405_mulladd_insn(ctx, opc2, opc3, rA(ctx->opcode), rB(ctx->opcode), \
5525 rD(ctx->opcode), Rc(ctx->opcode)); \
5528 /* macchw - macchw. */
5529 GEN_MAC_HANDLER(macchw, 0x0C, 0x05);
5530 /* macchwo - macchwo. */
5531 GEN_MAC_HANDLER(macchwo, 0x0C, 0x15);
5532 /* macchws - macchws. */
5533 GEN_MAC_HANDLER(macchws, 0x0C, 0x07);
5534 /* macchwso - macchwso. */
5535 GEN_MAC_HANDLER(macchwso, 0x0C, 0x17);
5536 /* macchwsu - macchwsu. */
5537 GEN_MAC_HANDLER(macchwsu, 0x0C, 0x06);
5538 /* macchwsuo - macchwsuo. */
5539 GEN_MAC_HANDLER(macchwsuo, 0x0C, 0x16);
5540 /* macchwu - macchwu. */
5541 GEN_MAC_HANDLER(macchwu, 0x0C, 0x04);
5542 /* macchwuo - macchwuo. */
5543 GEN_MAC_HANDLER(macchwuo, 0x0C, 0x14);
5544 /* machhw - machhw. */
5545 GEN_MAC_HANDLER(machhw, 0x0C, 0x01);
5546 /* machhwo - machhwo. */
5547 GEN_MAC_HANDLER(machhwo, 0x0C, 0x11);
5548 /* machhws - machhws. */
5549 GEN_MAC_HANDLER(machhws, 0x0C, 0x03);
5550 /* machhwso - machhwso. */
5551 GEN_MAC_HANDLER(machhwso, 0x0C, 0x13);
5552 /* machhwsu - machhwsu. */
5553 GEN_MAC_HANDLER(machhwsu, 0x0C, 0x02);
5554 /* machhwsuo - machhwsuo. */
5555 GEN_MAC_HANDLER(machhwsuo, 0x0C, 0x12);
5556 /* machhwu - machhwu. */
5557 GEN_MAC_HANDLER(machhwu, 0x0C, 0x00);
5558 /* machhwuo - machhwuo. */
5559 GEN_MAC_HANDLER(machhwuo, 0x0C, 0x10);
5560 /* maclhw - maclhw. */
5561 GEN_MAC_HANDLER(maclhw, 0x0C, 0x0D);
5562 /* maclhwo - maclhwo. */
5563 GEN_MAC_HANDLER(maclhwo, 0x0C, 0x1D);
5564 /* maclhws - maclhws. */
5565 GEN_MAC_HANDLER(maclhws, 0x0C, 0x0F);
5566 /* maclhwso - maclhwso. */
5567 GEN_MAC_HANDLER(maclhwso, 0x0C, 0x1F);
5568 /* maclhwu - maclhwu. */
5569 GEN_MAC_HANDLER(maclhwu, 0x0C, 0x0C);
5570 /* maclhwuo - maclhwuo. */
5571 GEN_MAC_HANDLER(maclhwuo, 0x0C, 0x1C);
5572 /* maclhwsu - maclhwsu. */
5573 GEN_MAC_HANDLER(maclhwsu, 0x0C, 0x0E);
5574 /* maclhwsuo - maclhwsuo. */
5575 GEN_MAC_HANDLER(maclhwsuo, 0x0C, 0x1E);
5576 /* nmacchw - nmacchw. */
5577 GEN_MAC_HANDLER(nmacchw, 0x0E, 0x05);
5578 /* nmacchwo - nmacchwo. */
5579 GEN_MAC_HANDLER(nmacchwo, 0x0E, 0x15);
5580 /* nmacchws - nmacchws. */
5581 GEN_MAC_HANDLER(nmacchws, 0x0E, 0x07);
5582 /* nmacchwso - nmacchwso. */
5583 GEN_MAC_HANDLER(nmacchwso, 0x0E, 0x17);
5584 /* nmachhw - nmachhw. */
5585 GEN_MAC_HANDLER(nmachhw, 0x0E, 0x01);
5586 /* nmachhwo - nmachhwo. */
5587 GEN_MAC_HANDLER(nmachhwo, 0x0E, 0x11);
5588 /* nmachhws - nmachhws. */
5589 GEN_MAC_HANDLER(nmachhws, 0x0E, 0x03);
5590 /* nmachhwso - nmachhwso. */
5591 GEN_MAC_HANDLER(nmachhwso, 0x0E, 0x13);
5592 /* nmaclhw - nmaclhw. */
5593 GEN_MAC_HANDLER(nmaclhw, 0x0E, 0x0D);
5594 /* nmaclhwo - nmaclhwo. */
5595 GEN_MAC_HANDLER(nmaclhwo, 0x0E, 0x1D);
5596 /* nmaclhws - nmaclhws. */
5597 GEN_MAC_HANDLER(nmaclhws, 0x0E, 0x0F);
5598 /* nmaclhwso - nmaclhwso. */
5599 GEN_MAC_HANDLER(nmaclhwso, 0x0E, 0x1F);
5601 /* mulchw - mulchw. */
5602 GEN_MAC_HANDLER(mulchw, 0x08, 0x05);
5603 /* mulchwu - mulchwu. */
5604 GEN_MAC_HANDLER(mulchwu, 0x08, 0x04);
5605 /* mulhhw - mulhhw. */
5606 GEN_MAC_HANDLER(mulhhw, 0x08, 0x01);
5607 /* mulhhwu - mulhhwu. */
5608 GEN_MAC_HANDLER(mulhhwu, 0x08, 0x00);
5609 /* mullhw - mullhw. */
5610 GEN_MAC_HANDLER(mullhw, 0x08, 0x0D);
5611 /* mullhwu - mullhwu. */
5612 GEN_MAC_HANDLER(mullhwu, 0x08, 0x0C);
5615 GEN_HANDLER(mfdcr, 0x1F, 0x03, 0x0A, 0x00000001, PPC_DCR)
5617 #if defined(CONFIG_USER_ONLY)
5618 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
5621 if (unlikely(!ctx->mem_idx)) {
5622 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
5625 /* NIP cannot be restored if the memory exception comes from an helper */
5626 gen_update_nip(ctx, ctx->nip - 4);
5627 dcrn = tcg_const_tl(SPR(ctx->opcode));
5628 gen_helper_load_dcr(cpu_gpr[rD(ctx->opcode)], dcrn);
5629 tcg_temp_free(dcrn);
5634 GEN_HANDLER(mtdcr, 0x1F, 0x03, 0x0E, 0x00000001, PPC_DCR)
5636 #if defined(CONFIG_USER_ONLY)
5637 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
5640 if (unlikely(!ctx->mem_idx)) {
5641 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
5644 /* NIP cannot be restored if the memory exception comes from an helper */
5645 gen_update_nip(ctx, ctx->nip - 4);
5646 dcrn = tcg_const_tl(SPR(ctx->opcode));
5647 gen_helper_store_dcr(dcrn, cpu_gpr[rS(ctx->opcode)]);
5648 tcg_temp_free(dcrn);
5653 /* XXX: not implemented on 440 ? */
5654 GEN_HANDLER(mfdcrx, 0x1F, 0x03, 0x08, 0x00000000, PPC_DCRX)
5656 #if defined(CONFIG_USER_ONLY)
5657 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
5659 if (unlikely(!ctx->mem_idx)) {
5660 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
5663 /* NIP cannot be restored if the memory exception comes from an helper */
5664 gen_update_nip(ctx, ctx->nip - 4);
5665 gen_helper_load_dcr(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)]);
5666 /* Note: Rc update flag set leads to undefined state of Rc0 */
5671 /* XXX: not implemented on 440 ? */
5672 GEN_HANDLER(mtdcrx, 0x1F, 0x03, 0x0C, 0x00000000, PPC_DCRX)
5674 #if defined(CONFIG_USER_ONLY)
5675 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
5677 if (unlikely(!ctx->mem_idx)) {
5678 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
5681 /* NIP cannot be restored if the memory exception comes from an helper */
5682 gen_update_nip(ctx, ctx->nip - 4);
5683 gen_helper_store_dcr(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)]);
5684 /* Note: Rc update flag set leads to undefined state of Rc0 */
5688 /* mfdcrux (PPC 460) : user-mode access to DCR */
5689 GEN_HANDLER(mfdcrux, 0x1F, 0x03, 0x09, 0x00000000, PPC_DCRUX)
5691 /* NIP cannot be restored if the memory exception comes from an helper */
5692 gen_update_nip(ctx, ctx->nip - 4);
5693 gen_helper_load_dcr(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)]);
5694 /* Note: Rc update flag set leads to undefined state of Rc0 */
5697 /* mtdcrux (PPC 460) : user-mode access to DCR */
5698 GEN_HANDLER(mtdcrux, 0x1F, 0x03, 0x0D, 0x00000000, PPC_DCRUX)
5700 /* NIP cannot be restored if the memory exception comes from an helper */
5701 gen_update_nip(ctx, ctx->nip - 4);
5702 gen_helper_store_dcr(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)]);
5703 /* Note: Rc update flag set leads to undefined state of Rc0 */
5707 GEN_HANDLER(dccci, 0x1F, 0x06, 0x0E, 0x03E00001, PPC_4xx_COMMON)
5709 #if defined(CONFIG_USER_ONLY)
5710 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
5712 if (unlikely(!ctx->mem_idx)) {
5713 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
5716 /* interpreted as no-op */
5721 GEN_HANDLER(dcread, 0x1F, 0x06, 0x0F, 0x00000001, PPC_4xx_COMMON)
5723 #if defined(CONFIG_USER_ONLY)
5724 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
5727 if (unlikely(!ctx->mem_idx)) {
5728 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
5731 gen_set_access_type(ctx, ACCESS_CACHE);
5732 EA = tcg_temp_new();
5733 gen_addr_reg_index(ctx, EA);
5734 val = tcg_temp_new();
5735 gen_qemu_ld32u(ctx, val, EA);
5737 tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], EA);
5743 GEN_HANDLER2(icbt_40x, "icbt", 0x1F, 0x06, 0x08, 0x03E00001, PPC_40x_ICBT)
5745 /* interpreted as no-op */
5746 /* XXX: specification say this is treated as a load by the MMU
5747 * but does not generate any exception
5752 GEN_HANDLER(iccci, 0x1F, 0x06, 0x1E, 0x00000001, PPC_4xx_COMMON)
5754 #if defined(CONFIG_USER_ONLY)
5755 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
5757 if (unlikely(!ctx->mem_idx)) {
5758 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
5761 /* interpreted as no-op */
5766 GEN_HANDLER(icread, 0x1F, 0x06, 0x1F, 0x03E00001, PPC_4xx_COMMON)
5768 #if defined(CONFIG_USER_ONLY)
5769 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
5771 if (unlikely(!ctx->mem_idx)) {
5772 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
5775 /* interpreted as no-op */
5779 /* rfci (mem_idx only) */
5780 GEN_HANDLER2(rfci_40x, "rfci", 0x13, 0x13, 0x01, 0x03FF8001, PPC_40x_EXCP)
5782 #if defined(CONFIG_USER_ONLY)
5783 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
5785 if (unlikely(!ctx->mem_idx)) {
5786 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
5789 /* Restore CPU state */
5790 gen_helper_40x_rfci();
5791 gen_sync_exception(ctx);
5795 GEN_HANDLER(rfci, 0x13, 0x13, 0x01, 0x03FF8001, PPC_BOOKE)
5797 #if defined(CONFIG_USER_ONLY)
5798 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
5800 if (unlikely(!ctx->mem_idx)) {
5801 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
5804 /* Restore CPU state */
5806 gen_sync_exception(ctx);
5810 /* BookE specific */
5811 /* XXX: not implemented on 440 ? */
5812 GEN_HANDLER(rfdi, 0x13, 0x07, 0x01, 0x03FF8001, PPC_RFDI)
5814 #if defined(CONFIG_USER_ONLY)
5815 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
5817 if (unlikely(!ctx->mem_idx)) {
5818 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
5821 /* Restore CPU state */
5823 gen_sync_exception(ctx);
5827 /* XXX: not implemented on 440 ? */
5828 GEN_HANDLER(rfmci, 0x13, 0x06, 0x01, 0x03FF8001, PPC_RFMCI)
5830 #if defined(CONFIG_USER_ONLY)
5831 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
5833 if (unlikely(!ctx->mem_idx)) {
5834 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
5837 /* Restore CPU state */
5839 gen_sync_exception(ctx);
5843 /* TLB management - PowerPC 405 implementation */
5845 GEN_HANDLER2(tlbre_40x, "tlbre", 0x1F, 0x12, 0x1D, 0x00000001, PPC_40x_TLB)
5847 #if defined(CONFIG_USER_ONLY)
5848 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
5850 if (unlikely(!ctx->mem_idx)) {
5851 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
5854 switch (rB(ctx->opcode)) {
5856 gen_helper_4xx_tlbre_hi(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)]);
5859 gen_helper_4xx_tlbre_lo(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)]);
5862 gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL);
5868 /* tlbsx - tlbsx. */
5869 GEN_HANDLER2(tlbsx_40x, "tlbsx", 0x1F, 0x12, 0x1C, 0x00000000, PPC_40x_TLB)
5871 #if defined(CONFIG_USER_ONLY)
5872 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
5875 if (unlikely(!ctx->mem_idx)) {
5876 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
5879 t0 = tcg_temp_new();
5880 gen_addr_reg_index(ctx, t0);
5881 gen_helper_4xx_tlbsx(cpu_gpr[rD(ctx->opcode)], t0);
5883 if (Rc(ctx->opcode)) {
5884 int l1 = gen_new_label();
5885 tcg_gen_trunc_tl_i32(cpu_crf[0], cpu_xer);
5886 tcg_gen_shri_i32(cpu_crf[0], cpu_crf[0], XER_SO);
5887 tcg_gen_andi_i32(cpu_crf[0], cpu_crf[0], 1);
5888 tcg_gen_brcondi_tl(TCG_COND_EQ, cpu_gpr[rD(ctx->opcode)], -1, l1);
5889 tcg_gen_ori_i32(cpu_crf[0], cpu_crf[0], 0x02);
5896 GEN_HANDLER2(tlbwe_40x, "tlbwe", 0x1F, 0x12, 0x1E, 0x00000001, PPC_40x_TLB)
5898 #if defined(CONFIG_USER_ONLY)
5899 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
5901 if (unlikely(!ctx->mem_idx)) {
5902 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
5905 switch (rB(ctx->opcode)) {
5907 gen_helper_4xx_tlbwe_hi(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)]);
5910 gen_helper_4xx_tlbwe_lo(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)]);
5913 gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL);
5919 /* TLB management - PowerPC 440 implementation */
5921 GEN_HANDLER2(tlbre_440, "tlbre", 0x1F, 0x12, 0x1D, 0x00000001, PPC_BOOKE)
5923 #if defined(CONFIG_USER_ONLY)
5924 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
5926 if (unlikely(!ctx->mem_idx)) {
5927 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
5930 switch (rB(ctx->opcode)) {
5935 TCGv_i32 t0 = tcg_const_i32(rB(ctx->opcode));
5936 gen_helper_440_tlbwe(t0, cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)]);
5937 tcg_temp_free_i32(t0);
5941 gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL);
5947 /* tlbsx - tlbsx. */
5948 GEN_HANDLER2(tlbsx_440, "tlbsx", 0x1F, 0x12, 0x1C, 0x00000000, PPC_BOOKE)
5950 #if defined(CONFIG_USER_ONLY)
5951 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
5954 if (unlikely(!ctx->mem_idx)) {
5955 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
5958 t0 = tcg_temp_new();
5959 gen_addr_reg_index(ctx, t0);
5960 gen_helper_440_tlbsx(cpu_gpr[rD(ctx->opcode)], t0);
5962 if (Rc(ctx->opcode)) {
5963 int l1 = gen_new_label();
5964 tcg_gen_trunc_tl_i32(cpu_crf[0], cpu_xer);
5965 tcg_gen_shri_i32(cpu_crf[0], cpu_crf[0], XER_SO);
5966 tcg_gen_andi_i32(cpu_crf[0], cpu_crf[0], 1);
5967 tcg_gen_brcondi_tl(TCG_COND_EQ, cpu_gpr[rD(ctx->opcode)], -1, l1);
5968 tcg_gen_ori_i32(cpu_crf[0], cpu_crf[0], 0x02);
5975 GEN_HANDLER2(tlbwe_440, "tlbwe", 0x1F, 0x12, 0x1E, 0x00000001, PPC_BOOKE)
5977 #if defined(CONFIG_USER_ONLY)
5978 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
5980 if (unlikely(!ctx->mem_idx)) {
5981 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
5984 switch (rB(ctx->opcode)) {
5989 TCGv_i32 t0 = tcg_const_i32(rB(ctx->opcode));
5990 gen_helper_440_tlbwe(t0, cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)]);
5991 tcg_temp_free_i32(t0);
5995 gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL);
6002 GEN_HANDLER(wrtee, 0x1F, 0x03, 0x04, 0x000FFC01, PPC_WRTEE)
6004 #if defined(CONFIG_USER_ONLY)
6005 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
6008 if (unlikely(!ctx->mem_idx)) {
6009 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
6012 t0 = tcg_temp_new();
6013 tcg_gen_andi_tl(t0, cpu_gpr[rD(ctx->opcode)], (1 << MSR_EE));
6014 tcg_gen_andi_tl(cpu_msr, cpu_msr, ~(1 << MSR_EE));
6015 tcg_gen_or_tl(cpu_msr, cpu_msr, t0);
6017 /* Stop translation to have a chance to raise an exception
6018 * if we just set msr_ee to 1
6020 gen_stop_exception(ctx);
6025 GEN_HANDLER(wrteei, 0x1F, 0x03, 0x05, 0x000EFC01, PPC_WRTEE)
6027 #if defined(CONFIG_USER_ONLY)
6028 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
6030 if (unlikely(!ctx->mem_idx)) {
6031 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
6034 if (ctx->opcode & 0x00010000) {
6035 tcg_gen_ori_tl(cpu_msr, cpu_msr, (1 << MSR_EE));
6036 /* Stop translation to have a chance to raise an exception */
6037 gen_stop_exception(ctx);
6039 tcg_gen_andi_tl(cpu_msr, cpu_msr, (1 << MSR_EE));
6044 /* PowerPC 440 specific instructions */
6046 GEN_HANDLER(dlmzb, 0x1F, 0x0E, 0x02, 0x00000000, PPC_440_SPEC)
6048 TCGv_i32 t0 = tcg_const_i32(Rc(ctx->opcode));
6049 gen_helper_dlmzb(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)],
6050 cpu_gpr[rB(ctx->opcode)], t0);
6051 tcg_temp_free_i32(t0);
6054 /* mbar replaces eieio on 440 */
6055 GEN_HANDLER(mbar, 0x1F, 0x16, 0x1a, 0x001FF801, PPC_BOOKE)
6057 /* interpreted as no-op */
6060 /* msync replaces sync on 440 */
6061 GEN_HANDLER(msync, 0x1F, 0x16, 0x12, 0x03FFF801, PPC_BOOKE)
6063 /* interpreted as no-op */
6067 GEN_HANDLER2(icbt_440, "icbt", 0x1F, 0x16, 0x00, 0x03E00001, PPC_BOOKE)
6069 /* interpreted as no-op */
6070 /* XXX: specification say this is treated as a load by the MMU
6071 * but does not generate any exception
6075 /*** Altivec vector extension ***/
6076 /* Altivec registers moves */
6078 #define GEN_VR_LDX(name, opc2, opc3) \
6079 GEN_HANDLER(name, 0x1F, opc2, opc3, 0x00000001, PPC_ALTIVEC) \
6082 if (unlikely(!ctx->altivec_enabled)) { \
6083 gen_exception(ctx, POWERPC_EXCP_VPU); \
6086 gen_set_access_type(ctx, ACCESS_INT); \
6087 EA = tcg_temp_new(); \
6088 gen_addr_reg_index(ctx, EA); \
6089 tcg_gen_andi_tl(EA, EA, ~0xf); \
6090 if (ctx->le_mode) { \
6091 gen_qemu_ld64(ctx, cpu_avrl[rD(ctx->opcode)], EA); \
6092 tcg_gen_addi_tl(EA, EA, 8); \
6093 gen_qemu_ld64(ctx, cpu_avrh[rD(ctx->opcode)], EA); \
6095 gen_qemu_ld64(ctx, cpu_avrh[rD(ctx->opcode)], EA); \
6096 tcg_gen_addi_tl(EA, EA, 8); \
6097 gen_qemu_ld64(ctx, cpu_avrl[rD(ctx->opcode)], EA); \
6099 tcg_temp_free(EA); \
6102 #define GEN_VR_STX(name, opc2, opc3) \
6103 GEN_HANDLER(st##name, 0x1F, opc2, opc3, 0x00000001, PPC_ALTIVEC) \
6106 if (unlikely(!ctx->altivec_enabled)) { \
6107 gen_exception(ctx, POWERPC_EXCP_VPU); \
6110 gen_set_access_type(ctx, ACCESS_INT); \
6111 EA = tcg_temp_new(); \
6112 gen_addr_reg_index(ctx, EA); \
6113 tcg_gen_andi_tl(EA, EA, ~0xf); \
6114 if (ctx->le_mode) { \
6115 gen_qemu_st64(ctx, cpu_avrl[rD(ctx->opcode)], EA); \
6116 tcg_gen_addi_tl(EA, EA, 8); \
6117 gen_qemu_st64(ctx, cpu_avrh[rD(ctx->opcode)], EA); \
6119 gen_qemu_st64(ctx, cpu_avrh[rD(ctx->opcode)], EA); \
6120 tcg_gen_addi_tl(EA, EA, 8); \
6121 gen_qemu_st64(ctx, cpu_avrl[rD(ctx->opcode)], EA); \
6123 tcg_temp_free(EA); \
6126 GEN_VR_LDX(lvx, 0x07, 0x03);
6127 /* As we don't emulate the cache, lvxl is stricly equivalent to lvx */
6128 GEN_VR_LDX(lvxl, 0x07, 0x0B);
6130 GEN_VR_STX(svx, 0x07, 0x07);
6131 /* As we don't emulate the cache, stvxl is stricly equivalent to stvx */
6132 GEN_VR_STX(svxl, 0x07, 0x0F);
6134 /*** SPE extension ***/
6135 /* Register moves */
6137 static always_inline void gen_load_gpr64(TCGv_i64 t, int reg) {
6138 #if defined(TARGET_PPC64)
6139 tcg_gen_mov_i64(t, cpu_gpr[reg]);
6141 tcg_gen_concat_i32_i64(t, cpu_gpr[reg], cpu_gprh[reg]);
6145 static always_inline void gen_store_gpr64(int reg, TCGv_i64 t) {
6146 #if defined(TARGET_PPC64)
6147 tcg_gen_mov_i64(cpu_gpr[reg], t);
6149 TCGv_i64 tmp = tcg_temp_new_i64();
6150 tcg_gen_trunc_i64_i32(cpu_gpr[reg], t);
6151 tcg_gen_shri_i64(tmp, t, 32);
6152 tcg_gen_trunc_i64_i32(cpu_gprh[reg], tmp);
6153 tcg_temp_free_i64(tmp);
6157 #define GEN_SPE(name0, name1, opc2, opc3, inval, type) \
6158 GEN_HANDLER(name0##_##name1, 0x04, opc2, opc3, inval, type) \
6160 if (Rc(ctx->opcode)) \
6166 /* Handler for undefined SPE opcodes */
6167 static always_inline void gen_speundef (DisasContext *ctx)
6169 gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL);
6173 #if defined(TARGET_PPC64)
6174 #define GEN_SPEOP_LOGIC2(name, tcg_op) \
6175 static always_inline void gen_##name (DisasContext *ctx) \
6177 if (unlikely(!ctx->spe_enabled)) { \
6178 gen_exception(ctx, POWERPC_EXCP_APU); \
6181 tcg_op(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)], \
6182 cpu_gpr[rB(ctx->opcode)]); \
6185 #define GEN_SPEOP_LOGIC2(name, tcg_op) \
6186 static always_inline void gen_##name (DisasContext *ctx) \
6188 if (unlikely(!ctx->spe_enabled)) { \
6189 gen_exception(ctx, POWERPC_EXCP_APU); \
6192 tcg_op(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)], \
6193 cpu_gpr[rB(ctx->opcode)]); \
6194 tcg_op(cpu_gprh[rD(ctx->opcode)], cpu_gprh[rA(ctx->opcode)], \
6195 cpu_gprh[rB(ctx->opcode)]); \
6199 GEN_SPEOP_LOGIC2(evand, tcg_gen_and_tl);
6200 GEN_SPEOP_LOGIC2(evandc, tcg_gen_andc_tl);
6201 GEN_SPEOP_LOGIC2(evxor, tcg_gen_xor_tl);
6202 GEN_SPEOP_LOGIC2(evor, tcg_gen_or_tl);
6203 GEN_SPEOP_LOGIC2(evnor, tcg_gen_nor_tl);
6204 GEN_SPEOP_LOGIC2(eveqv, tcg_gen_eqv_tl);
6205 GEN_SPEOP_LOGIC2(evorc, tcg_gen_orc_tl);
6206 GEN_SPEOP_LOGIC2(evnand, tcg_gen_nand_tl);
6208 /* SPE logic immediate */
6209 #if defined(TARGET_PPC64)
6210 #define GEN_SPEOP_TCG_LOGIC_IMM2(name, tcg_opi) \
6211 static always_inline void gen_##name (DisasContext *ctx) \
6213 if (unlikely(!ctx->spe_enabled)) { \
6214 gen_exception(ctx, POWERPC_EXCP_APU); \
6217 TCGv_i32 t0 = tcg_temp_local_new_i32(); \
6218 TCGv_i32 t1 = tcg_temp_local_new_i32(); \
6219 TCGv_i64 t2 = tcg_temp_local_new_i64(); \
6220 tcg_gen_trunc_i64_i32(t0, cpu_gpr[rA(ctx->opcode)]); \
6221 tcg_opi(t0, t0, rB(ctx->opcode)); \
6222 tcg_gen_shri_i64(t2, cpu_gpr[rA(ctx->opcode)], 32); \
6223 tcg_gen_trunc_i64_i32(t1, t2); \
6224 tcg_temp_free_i64(t2); \
6225 tcg_opi(t1, t1, rB(ctx->opcode)); \
6226 tcg_gen_concat_i32_i64(cpu_gpr[rD(ctx->opcode)], t0, t1); \
6227 tcg_temp_free_i32(t0); \
6228 tcg_temp_free_i32(t1); \
6231 #define GEN_SPEOP_TCG_LOGIC_IMM2(name, tcg_opi) \
6232 static always_inline void gen_##name (DisasContext *ctx) \
6234 if (unlikely(!ctx->spe_enabled)) { \
6235 gen_exception(ctx, POWERPC_EXCP_APU); \
6238 tcg_opi(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)], \
6240 tcg_opi(cpu_gprh[rD(ctx->opcode)], cpu_gprh[rA(ctx->opcode)], \
6244 GEN_SPEOP_TCG_LOGIC_IMM2(evslwi, tcg_gen_shli_i32);
6245 GEN_SPEOP_TCG_LOGIC_IMM2(evsrwiu, tcg_gen_shri_i32);
6246 GEN_SPEOP_TCG_LOGIC_IMM2(evsrwis, tcg_gen_sari_i32);
6247 GEN_SPEOP_TCG_LOGIC_IMM2(evrlwi, tcg_gen_rotli_i32);
6249 /* SPE arithmetic */
6250 #if defined(TARGET_PPC64)
6251 #define GEN_SPEOP_ARITH1(name, tcg_op) \
6252 static always_inline void gen_##name (DisasContext *ctx) \
6254 if (unlikely(!ctx->spe_enabled)) { \
6255 gen_exception(ctx, POWERPC_EXCP_APU); \
6258 TCGv_i32 t0 = tcg_temp_local_new_i32(); \
6259 TCGv_i32 t1 = tcg_temp_local_new_i32(); \
6260 TCGv_i64 t2 = tcg_temp_local_new_i64(); \
6261 tcg_gen_trunc_i64_i32(t0, cpu_gpr[rA(ctx->opcode)]); \
6263 tcg_gen_shri_i64(t2, cpu_gpr[rA(ctx->opcode)], 32); \
6264 tcg_gen_trunc_i64_i32(t1, t2); \
6265 tcg_temp_free_i64(t2); \
6267 tcg_gen_concat_i32_i64(cpu_gpr[rD(ctx->opcode)], t0, t1); \
6268 tcg_temp_free_i32(t0); \
6269 tcg_temp_free_i32(t1); \
6272 #define GEN_SPEOP_ARITH1(name, tcg_op) \
6273 static always_inline void gen_##name (DisasContext *ctx) \
6275 if (unlikely(!ctx->spe_enabled)) { \
6276 gen_exception(ctx, POWERPC_EXCP_APU); \
6279 tcg_op(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)]); \
6280 tcg_op(cpu_gprh[rD(ctx->opcode)], cpu_gprh[rA(ctx->opcode)]); \
6284 static always_inline void gen_op_evabs (TCGv_i32 ret, TCGv_i32 arg1)
6286 int l1 = gen_new_label();
6287 int l2 = gen_new_label();
6289 tcg_gen_brcondi_i32(TCG_COND_GE, arg1, 0, l1);
6290 tcg_gen_neg_i32(ret, arg1);
6293 tcg_gen_mov_i32(ret, arg1);
6296 GEN_SPEOP_ARITH1(evabs, gen_op_evabs);
6297 GEN_SPEOP_ARITH1(evneg, tcg_gen_neg_i32);
6298 GEN_SPEOP_ARITH1(evextsb, tcg_gen_ext8s_i32);
6299 GEN_SPEOP_ARITH1(evextsh, tcg_gen_ext16s_i32);
6300 static always_inline void gen_op_evrndw (TCGv_i32 ret, TCGv_i32 arg1)
6302 tcg_gen_addi_i32(ret, arg1, 0x8000);
6303 tcg_gen_ext16u_i32(ret, ret);
6305 GEN_SPEOP_ARITH1(evrndw, gen_op_evrndw);
6306 GEN_SPEOP_ARITH1(evcntlsw, gen_helper_cntlsw32);
6307 GEN_SPEOP_ARITH1(evcntlzw, gen_helper_cntlzw32);
6309 #if defined(TARGET_PPC64)
6310 #define GEN_SPEOP_ARITH2(name, tcg_op) \
6311 static always_inline void gen_##name (DisasContext *ctx) \
6313 if (unlikely(!ctx->spe_enabled)) { \
6314 gen_exception(ctx, POWERPC_EXCP_APU); \
6317 TCGv_i32 t0 = tcg_temp_local_new_i32(); \
6318 TCGv_i32 t1 = tcg_temp_local_new_i32(); \
6319 TCGv_i32 t2 = tcg_temp_local_new_i32(); \
6320 TCGv_i64 t3 = tcg_temp_local_new(TCG_TYPE_I64); \
6321 tcg_gen_trunc_i64_i32(t0, cpu_gpr[rA(ctx->opcode)]); \
6322 tcg_gen_trunc_i64_i32(t2, cpu_gpr[rB(ctx->opcode)]); \
6323 tcg_op(t0, t0, t2); \
6324 tcg_gen_shri_i64(t3, cpu_gpr[rA(ctx->opcode)], 32); \
6325 tcg_gen_trunc_i64_i32(t1, t3); \
6326 tcg_gen_shri_i64(t3, cpu_gpr[rB(ctx->opcode)], 32); \
6327 tcg_gen_trunc_i64_i32(t2, t3); \
6328 tcg_temp_free_i64(t3); \
6329 tcg_op(t1, t1, t2); \
6330 tcg_temp_free_i32(t2); \
6331 tcg_gen_concat_i32_i64(cpu_gpr[rD(ctx->opcode)], t0, t1); \
6332 tcg_temp_free_i32(t0); \
6333 tcg_temp_free_i32(t1); \
6336 #define GEN_SPEOP_ARITH2(name, tcg_op) \
6337 static always_inline void gen_##name (DisasContext *ctx) \
6339 if (unlikely(!ctx->spe_enabled)) { \
6340 gen_exception(ctx, POWERPC_EXCP_APU); \
6343 tcg_op(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)], \
6344 cpu_gpr[rB(ctx->opcode)]); \
6345 tcg_op(cpu_gprh[rD(ctx->opcode)], cpu_gprh[rA(ctx->opcode)], \
6346 cpu_gprh[rB(ctx->opcode)]); \
6350 static always_inline void gen_op_evsrwu (TCGv_i32 ret, TCGv_i32 arg1, TCGv_i32 arg2)
6355 l1 = gen_new_label();
6356 l2 = gen_new_label();
6357 t0 = tcg_temp_local_new_i32();
6358 /* No error here: 6 bits are used */
6359 tcg_gen_andi_i32(t0, arg2, 0x3F);
6360 tcg_gen_brcondi_i32(TCG_COND_GE, t0, 32, l1);
6361 tcg_gen_shr_i32(ret, arg1, t0);
6364 tcg_gen_movi_i32(ret, 0);
6366 tcg_temp_free_i32(t0);
6368 GEN_SPEOP_ARITH2(evsrwu, gen_op_evsrwu);
6369 static always_inline void gen_op_evsrws (TCGv_i32 ret, TCGv_i32 arg1, TCGv_i32 arg2)
6374 l1 = gen_new_label();
6375 l2 = gen_new_label();
6376 t0 = tcg_temp_local_new_i32();
6377 /* No error here: 6 bits are used */
6378 tcg_gen_andi_i32(t0, arg2, 0x3F);
6379 tcg_gen_brcondi_i32(TCG_COND_GE, t0, 32, l1);
6380 tcg_gen_sar_i32(ret, arg1, t0);
6383 tcg_gen_movi_i32(ret, 0);
6385 tcg_temp_free_i32(t0);
6387 GEN_SPEOP_ARITH2(evsrws, gen_op_evsrws);
6388 static always_inline void gen_op_evslw (TCGv_i32 ret, TCGv_i32 arg1, TCGv_i32 arg2)
6393 l1 = gen_new_label();
6394 l2 = gen_new_label();
6395 t0 = tcg_temp_local_new_i32();
6396 /* No error here: 6 bits are used */
6397 tcg_gen_andi_i32(t0, arg2, 0x3F);
6398 tcg_gen_brcondi_i32(TCG_COND_GE, t0, 32, l1);
6399 tcg_gen_shl_i32(ret, arg1, t0);
6402 tcg_gen_movi_i32(ret, 0);
6404 tcg_temp_free_i32(t0);
6406 GEN_SPEOP_ARITH2(evslw, gen_op_evslw);
6407 static always_inline void gen_op_evrlw (TCGv_i32 ret, TCGv_i32 arg1, TCGv_i32 arg2)
6409 TCGv_i32 t0 = tcg_temp_new_i32();
6410 tcg_gen_andi_i32(t0, arg2, 0x1F);
6411 tcg_gen_rotl_i32(ret, arg1, t0);
6412 tcg_temp_free_i32(t0);
6414 GEN_SPEOP_ARITH2(evrlw, gen_op_evrlw);
6415 static always_inline void gen_evmergehi (DisasContext *ctx)
6417 if (unlikely(!ctx->spe_enabled)) {
6418 gen_exception(ctx, POWERPC_EXCP_APU);
6421 #if defined(TARGET_PPC64)
6422 TCGv t0 = tcg_temp_new();
6423 TCGv t1 = tcg_temp_new();
6424 tcg_gen_shri_tl(t0, cpu_gpr[rB(ctx->opcode)], 32);
6425 tcg_gen_andi_tl(t1, cpu_gpr[rA(ctx->opcode)], 0xFFFFFFFF0000000ULL);
6426 tcg_gen_or_tl(cpu_gpr[rD(ctx->opcode)], t0, t1);
6430 tcg_gen_mov_i32(cpu_gpr[rD(ctx->opcode)], cpu_gprh[rB(ctx->opcode)]);
6431 tcg_gen_mov_i32(cpu_gprh[rD(ctx->opcode)], cpu_gprh[rA(ctx->opcode)]);
6434 GEN_SPEOP_ARITH2(evaddw, tcg_gen_add_i32);
6435 static always_inline void gen_op_evsubf (TCGv_i32 ret, TCGv_i32 arg1, TCGv_i32 arg2)
6437 tcg_gen_sub_i32(ret, arg2, arg1);
6439 GEN_SPEOP_ARITH2(evsubfw, gen_op_evsubf);
6441 /* SPE arithmetic immediate */
6442 #if defined(TARGET_PPC64)
6443 #define GEN_SPEOP_ARITH_IMM2(name, tcg_op) \
6444 static always_inline void gen_##name (DisasContext *ctx) \
6446 if (unlikely(!ctx->spe_enabled)) { \
6447 gen_exception(ctx, POWERPC_EXCP_APU); \
6450 TCGv_i32 t0 = tcg_temp_local_new_i32(); \
6451 TCGv_i32 t1 = tcg_temp_local_new_i32(); \
6452 TCGv_i64 t2 = tcg_temp_local_new_i64(); \
6453 tcg_gen_trunc_i64_i32(t0, cpu_gpr[rB(ctx->opcode)]); \
6454 tcg_op(t0, t0, rA(ctx->opcode)); \
6455 tcg_gen_shri_i64(t2, cpu_gpr[rB(ctx->opcode)], 32); \
6456 tcg_gen_trunc_i64_i32(t1, t2); \
6457 tcg_temp_free_i64(t2); \
6458 tcg_op(t1, t1, rA(ctx->opcode)); \
6459 tcg_gen_concat_i32_i64(cpu_gpr[rD(ctx->opcode)], t0, t1); \
6460 tcg_temp_free_i32(t0); \
6461 tcg_temp_free_i32(t1); \
6464 #define GEN_SPEOP_ARITH_IMM2(name, tcg_op) \
6465 static always_inline void gen_##name (DisasContext *ctx) \
6467 if (unlikely(!ctx->spe_enabled)) { \
6468 gen_exception(ctx, POWERPC_EXCP_APU); \
6471 tcg_op(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rB(ctx->opcode)], \
6473 tcg_op(cpu_gprh[rD(ctx->opcode)], cpu_gprh[rB(ctx->opcode)], \
6477 GEN_SPEOP_ARITH_IMM2(evaddiw, tcg_gen_addi_i32);
6478 GEN_SPEOP_ARITH_IMM2(evsubifw, tcg_gen_subi_i32);
6480 /* SPE comparison */
6481 #if defined(TARGET_PPC64)
6482 #define GEN_SPEOP_COMP(name, tcg_cond) \
6483 static always_inline void gen_##name (DisasContext *ctx) \
6485 if (unlikely(!ctx->spe_enabled)) { \
6486 gen_exception(ctx, POWERPC_EXCP_APU); \
6489 int l1 = gen_new_label(); \
6490 int l2 = gen_new_label(); \
6491 int l3 = gen_new_label(); \
6492 int l4 = gen_new_label(); \
6493 TCGv_i32 t0 = tcg_temp_local_new_i32(); \
6494 TCGv_i32 t1 = tcg_temp_local_new_i32(); \
6495 TCGv_i64 t2 = tcg_temp_local_new_i64(); \
6496 tcg_gen_trunc_i64_i32(t0, cpu_gpr[rA(ctx->opcode)]); \
6497 tcg_gen_trunc_i64_i32(t1, cpu_gpr[rB(ctx->opcode)]); \
6498 tcg_gen_brcond_i32(tcg_cond, t0, t1, l1); \
6499 tcg_gen_movi_i32(cpu_crf[crfD(ctx->opcode)], 0); \
6501 gen_set_label(l1); \
6502 tcg_gen_movi_i32(cpu_crf[crfD(ctx->opcode)], \
6503 CRF_CL | CRF_CH_OR_CL | CRF_CH_AND_CL); \
6504 gen_set_label(l2); \
6505 tcg_gen_shri_i64(t2, cpu_gpr[rA(ctx->opcode)], 32); \
6506 tcg_gen_trunc_i64_i32(t0, t2); \
6507 tcg_gen_shri_i64(t2, cpu_gpr[rB(ctx->opcode)], 32); \
6508 tcg_gen_trunc_i64_i32(t1, t2); \
6509 tcg_temp_free_i64(t2); \
6510 tcg_gen_brcond_i32(tcg_cond, t0, t1, l3); \
6511 tcg_gen_andi_i32(cpu_crf[crfD(ctx->opcode)], cpu_crf[crfD(ctx->opcode)], \
6512 ~(CRF_CH | CRF_CH_AND_CL)); \
6514 gen_set_label(l3); \
6515 tcg_gen_ori_i32(cpu_crf[crfD(ctx->opcode)], cpu_crf[crfD(ctx->opcode)], \
6516 CRF_CH | CRF_CH_OR_CL); \
6517 gen_set_label(l4); \
6518 tcg_temp_free_i32(t0); \
6519 tcg_temp_free_i32(t1); \
6522 #define GEN_SPEOP_COMP(name, tcg_cond) \
6523 static always_inline void gen_##name (DisasContext *ctx) \
6525 if (unlikely(!ctx->spe_enabled)) { \
6526 gen_exception(ctx, POWERPC_EXCP_APU); \
6529 int l1 = gen_new_label(); \
6530 int l2 = gen_new_label(); \
6531 int l3 = gen_new_label(); \
6532 int l4 = gen_new_label(); \
6534 tcg_gen_brcond_i32(tcg_cond, cpu_gpr[rA(ctx->opcode)], \
6535 cpu_gpr[rB(ctx->opcode)], l1); \
6536 tcg_gen_movi_tl(cpu_crf[crfD(ctx->opcode)], 0); \
6538 gen_set_label(l1); \
6539 tcg_gen_movi_i32(cpu_crf[crfD(ctx->opcode)], \
6540 CRF_CL | CRF_CH_OR_CL | CRF_CH_AND_CL); \
6541 gen_set_label(l2); \
6542 tcg_gen_brcond_i32(tcg_cond, cpu_gprh[rA(ctx->opcode)], \
6543 cpu_gprh[rB(ctx->opcode)], l3); \
6544 tcg_gen_andi_i32(cpu_crf[crfD(ctx->opcode)], cpu_crf[crfD(ctx->opcode)], \
6545 ~(CRF_CH | CRF_CH_AND_CL)); \
6547 gen_set_label(l3); \
6548 tcg_gen_ori_i32(cpu_crf[crfD(ctx->opcode)], cpu_crf[crfD(ctx->opcode)], \
6549 CRF_CH | CRF_CH_OR_CL); \
6550 gen_set_label(l4); \
6553 GEN_SPEOP_COMP(evcmpgtu, TCG_COND_GTU);
6554 GEN_SPEOP_COMP(evcmpgts, TCG_COND_GT);
6555 GEN_SPEOP_COMP(evcmpltu, TCG_COND_LTU);
6556 GEN_SPEOP_COMP(evcmplts, TCG_COND_LT);
6557 GEN_SPEOP_COMP(evcmpeq, TCG_COND_EQ);
6560 static always_inline void gen_brinc (DisasContext *ctx)
6562 /* Note: brinc is usable even if SPE is disabled */
6563 gen_helper_brinc(cpu_gpr[rD(ctx->opcode)],
6564 cpu_gpr[rA(ctx->opcode)], cpu_gpr[rB(ctx->opcode)]);
6566 static always_inline void gen_evmergelo (DisasContext *ctx)
6568 if (unlikely(!ctx->spe_enabled)) {
6569 gen_exception(ctx, POWERPC_EXCP_APU);
6572 #if defined(TARGET_PPC64)
6573 TCGv t0 = tcg_temp_new();
6574 TCGv t1 = tcg_temp_new();
6575 tcg_gen_andi_tl(t0, cpu_gpr[rB(ctx->opcode)], 0x00000000FFFFFFFFLL);
6576 tcg_gen_shli_tl(t1, cpu_gpr[rA(ctx->opcode)], 32);
6577 tcg_gen_or_tl(cpu_gpr[rD(ctx->opcode)], t0, t1);
6581 tcg_gen_mov_i32(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rB(ctx->opcode)]);
6582 tcg_gen_mov_i32(cpu_gprh[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)]);
6585 static always_inline void gen_evmergehilo (DisasContext *ctx)
6587 if (unlikely(!ctx->spe_enabled)) {
6588 gen_exception(ctx, POWERPC_EXCP_APU);
6591 #if defined(TARGET_PPC64)
6592 TCGv t0 = tcg_temp_new();
6593 TCGv t1 = tcg_temp_new();
6594 tcg_gen_andi_tl(t0, cpu_gpr[rB(ctx->opcode)], 0x00000000FFFFFFFFLL);
6595 tcg_gen_andi_tl(t1, cpu_gpr[rA(ctx->opcode)], 0xFFFFFFFF0000000ULL);
6596 tcg_gen_or_tl(cpu_gpr[rD(ctx->opcode)], t0, t1);
6600 tcg_gen_mov_i32(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rB(ctx->opcode)]);
6601 tcg_gen_mov_i32(cpu_gprh[rD(ctx->opcode)], cpu_gprh[rA(ctx->opcode)]);
6604 static always_inline void gen_evmergelohi (DisasContext *ctx)
6606 if (unlikely(!ctx->spe_enabled)) {
6607 gen_exception(ctx, POWERPC_EXCP_APU);
6610 #if defined(TARGET_PPC64)
6611 TCGv t0 = tcg_temp_new();
6612 TCGv t1 = tcg_temp_new();
6613 tcg_gen_shri_tl(t0, cpu_gpr[rB(ctx->opcode)], 32);
6614 tcg_gen_shli_tl(t1, cpu_gpr[rA(ctx->opcode)], 32);
6615 tcg_gen_or_tl(cpu_gpr[rD(ctx->opcode)], t0, t1);
6619 tcg_gen_mov_i32(cpu_gpr[rD(ctx->opcode)], cpu_gprh[rB(ctx->opcode)]);
6620 tcg_gen_mov_i32(cpu_gprh[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)]);
6623 static always_inline void gen_evsplati (DisasContext *ctx)
6625 uint64_t imm = ((int32_t)(rA(ctx->opcode) << 11)) >> 27;
6627 #if defined(TARGET_PPC64)
6628 tcg_gen_movi_tl(cpu_gpr[rD(ctx->opcode)], (imm << 32) | imm);
6630 tcg_gen_movi_i32(cpu_gpr[rD(ctx->opcode)], imm);
6631 tcg_gen_movi_i32(cpu_gprh[rD(ctx->opcode)], imm);
6634 static always_inline void gen_evsplatfi (DisasContext *ctx)
6636 uint64_t imm = rA(ctx->opcode) << 11;
6638 #if defined(TARGET_PPC64)
6639 tcg_gen_movi_tl(cpu_gpr[rD(ctx->opcode)], (imm << 32) | imm);
6641 tcg_gen_movi_i32(cpu_gpr[rD(ctx->opcode)], imm);
6642 tcg_gen_movi_i32(cpu_gprh[rD(ctx->opcode)], imm);
6646 static always_inline void gen_evsel (DisasContext *ctx)
6648 int l1 = gen_new_label();
6649 int l2 = gen_new_label();
6650 int l3 = gen_new_label();
6651 int l4 = gen_new_label();
6652 TCGv_i32 t0 = tcg_temp_local_new_i32();
6653 #if defined(TARGET_PPC64)
6654 TCGv t1 = tcg_temp_local_new();
6655 TCGv t2 = tcg_temp_local_new();
6657 tcg_gen_andi_i32(t0, cpu_crf[ctx->opcode & 0x07], 1 << 3);
6658 tcg_gen_brcondi_i32(TCG_COND_EQ, t0, 0, l1);
6659 #if defined(TARGET_PPC64)
6660 tcg_gen_andi_tl(t1, cpu_gpr[rA(ctx->opcode)], 0xFFFFFFFF00000000ULL);
6662 tcg_gen_mov_tl(cpu_gprh[rD(ctx->opcode)], cpu_gprh[rA(ctx->opcode)]);
6666 #if defined(TARGET_PPC64)
6667 tcg_gen_andi_tl(t1, cpu_gpr[rB(ctx->opcode)], 0xFFFFFFFF00000000ULL);
6669 tcg_gen_mov_tl(cpu_gprh[rD(ctx->opcode)], cpu_gprh[rB(ctx->opcode)]);
6672 tcg_gen_andi_i32(t0, cpu_crf[ctx->opcode & 0x07], 1 << 2);
6673 tcg_gen_brcondi_i32(TCG_COND_EQ, t0, 0, l3);
6674 #if defined(TARGET_PPC64)
6675 tcg_gen_andi_tl(t2, cpu_gpr[rA(ctx->opcode)], 0x00000000FFFFFFFFULL);
6677 tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)]);
6681 #if defined(TARGET_PPC64)
6682 tcg_gen_andi_tl(t2, cpu_gpr[rB(ctx->opcode)], 0x00000000FFFFFFFFULL);
6684 tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rB(ctx->opcode)]);
6687 tcg_temp_free_i32(t0);
6688 #if defined(TARGET_PPC64)
6689 tcg_gen_or_tl(cpu_gpr[rD(ctx->opcode)], t1, t2);
6694 GEN_HANDLER2(evsel0, "evsel", 0x04, 0x1c, 0x09, 0x00000000, PPC_SPE)
6698 GEN_HANDLER2(evsel1, "evsel", 0x04, 0x1d, 0x09, 0x00000000, PPC_SPE)
6702 GEN_HANDLER2(evsel2, "evsel", 0x04, 0x1e, 0x09, 0x00000000, PPC_SPE)
6706 GEN_HANDLER2(evsel3, "evsel", 0x04, 0x1f, 0x09, 0x00000000, PPC_SPE)
6711 GEN_SPE(evaddw, speundef, 0x00, 0x08, 0x00000000, PPC_SPE); ////
6712 GEN_SPE(evaddiw, speundef, 0x01, 0x08, 0x00000000, PPC_SPE);
6713 GEN_SPE(evsubfw, speundef, 0x02, 0x08, 0x00000000, PPC_SPE); ////
6714 GEN_SPE(evsubifw, speundef, 0x03, 0x08, 0x00000000, PPC_SPE);
6715 GEN_SPE(evabs, evneg, 0x04, 0x08, 0x0000F800, PPC_SPE); ////
6716 GEN_SPE(evextsb, evextsh, 0x05, 0x08, 0x0000F800, PPC_SPE); ////
6717 GEN_SPE(evrndw, evcntlzw, 0x06, 0x08, 0x0000F800, PPC_SPE); ////
6718 GEN_SPE(evcntlsw, brinc, 0x07, 0x08, 0x00000000, PPC_SPE); //
6719 GEN_SPE(speundef, evand, 0x08, 0x08, 0x00000000, PPC_SPE); ////
6720 GEN_SPE(evandc, speundef, 0x09, 0x08, 0x00000000, PPC_SPE); ////
6721 GEN_SPE(evxor, evor, 0x0B, 0x08, 0x00000000, PPC_SPE); ////
6722 GEN_SPE(evnor, eveqv, 0x0C, 0x08, 0x00000000, PPC_SPE); ////
6723 GEN_SPE(speundef, evorc, 0x0D, 0x08, 0x00000000, PPC_SPE); ////
6724 GEN_SPE(evnand, speundef, 0x0F, 0x08, 0x00000000, PPC_SPE); ////
6725 GEN_SPE(evsrwu, evsrws, 0x10, 0x08, 0x00000000, PPC_SPE); ////
6726 GEN_SPE(evsrwiu, evsrwis, 0x11, 0x08, 0x00000000, PPC_SPE);
6727 GEN_SPE(evslw, speundef, 0x12, 0x08, 0x00000000, PPC_SPE); ////
6728 GEN_SPE(evslwi, speundef, 0x13, 0x08, 0x00000000, PPC_SPE);
6729 GEN_SPE(evrlw, evsplati, 0x14, 0x08, 0x00000000, PPC_SPE); //
6730 GEN_SPE(evrlwi, evsplatfi, 0x15, 0x08, 0x00000000, PPC_SPE);
6731 GEN_SPE(evmergehi, evmergelo, 0x16, 0x08, 0x00000000, PPC_SPE); ////
6732 GEN_SPE(evmergehilo, evmergelohi, 0x17, 0x08, 0x00000000, PPC_SPE); ////
6733 GEN_SPE(evcmpgtu, evcmpgts, 0x18, 0x08, 0x00600000, PPC_SPE); ////
6734 GEN_SPE(evcmpltu, evcmplts, 0x19, 0x08, 0x00600000, PPC_SPE); ////
6735 GEN_SPE(evcmpeq, speundef, 0x1A, 0x08, 0x00600000, PPC_SPE); ////
6737 /* SPE load and stores */
6738 static always_inline void gen_addr_spe_imm_index (DisasContext *ctx, TCGv EA, int sh)
6740 target_ulong uimm = rB(ctx->opcode);
6742 if (rA(ctx->opcode) == 0) {
6743 tcg_gen_movi_tl(EA, uimm << sh);
6745 tcg_gen_addi_tl(EA, cpu_gpr[rA(ctx->opcode)], uimm << sh);
6746 #if defined(TARGET_PPC64)
6747 if (!ctx->sf_mode) {
6748 tcg_gen_ext32u_tl(EA, EA);
6754 static always_inline void gen_op_evldd(DisasContext *ctx, TCGv addr)
6756 #if defined(TARGET_PPC64)
6757 gen_qemu_ld64(ctx, cpu_gpr[rD(ctx->opcode)], addr);
6759 TCGv_i64 t0 = tcg_temp_new_i64();
6760 gen_qemu_ld64(ctx, t0, addr);
6761 tcg_gen_trunc_i64_i32(cpu_gpr[rD(ctx->opcode)], t0);
6762 tcg_gen_shri_i64(t0, t0, 32);
6763 tcg_gen_trunc_i64_i32(cpu_gprh[rD(ctx->opcode)], t0);
6764 tcg_temp_free_i64(t0);
6768 static always_inline void gen_op_evldw(DisasContext *ctx, TCGv addr)
6770 #if defined(TARGET_PPC64)
6771 TCGv t0 = tcg_temp_new();
6772 gen_qemu_ld32u(ctx, t0, addr);
6773 tcg_gen_shli_tl(cpu_gpr[rD(ctx->opcode)], t0, 32);
6774 gen_addr_add(ctx, addr, addr, 4);
6775 gen_qemu_ld32u(ctx, t0, addr);
6776 tcg_gen_or_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rD(ctx->opcode)], t0);
6779 gen_qemu_ld32u(ctx, cpu_gprh[rD(ctx->opcode)], addr);
6780 gen_addr_add(ctx, addr, addr, 4);
6781 gen_qemu_ld32u(ctx, cpu_gpr[rD(ctx->opcode)], addr);
6785 static always_inline void gen_op_evldh(DisasContext *ctx, TCGv addr)
6787 TCGv t0 = tcg_temp_new();
6788 #if defined(TARGET_PPC64)
6789 gen_qemu_ld16u(ctx, t0, addr);
6790 tcg_gen_shli_tl(cpu_gpr[rD(ctx->opcode)], t0, 48);
6791 gen_addr_add(ctx, addr, addr, 2);
6792 gen_qemu_ld16u(ctx, t0, addr);
6793 tcg_gen_shli_tl(t0, t0, 32);
6794 tcg_gen_or_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rD(ctx->opcode)], t0);
6795 gen_addr_add(ctx, addr, addr, 2);
6796 gen_qemu_ld16u(ctx, t0, addr);
6797 tcg_gen_shli_tl(t0, t0, 16);
6798 tcg_gen_or_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rD(ctx->opcode)], t0);
6799 gen_addr_add(ctx, addr, addr, 2);
6800 gen_qemu_ld16u(ctx, t0, addr);
6801 tcg_gen_or_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rD(ctx->opcode)], t0);
6803 gen_qemu_ld16u(ctx, t0, addr);
6804 tcg_gen_shli_tl(cpu_gprh[rD(ctx->opcode)], t0, 16);
6805 gen_addr_add(ctx, addr, addr, 2);
6806 gen_qemu_ld16u(ctx, t0, addr);
6807 tcg_gen_or_tl(cpu_gprh[rD(ctx->opcode)], cpu_gprh[rD(ctx->opcode)], t0);
6808 gen_addr_add(ctx, addr, addr, 2);
6809 gen_qemu_ld16u(ctx, t0, addr);
6810 tcg_gen_shli_tl(cpu_gprh[rD(ctx->opcode)], t0, 16);
6811 gen_addr_add(ctx, addr, addr, 2);
6812 gen_qemu_ld16u(ctx, t0, addr);
6813 tcg_gen_or_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rD(ctx->opcode)], t0);
6818 static always_inline void gen_op_evlhhesplat(DisasContext *ctx, TCGv addr)
6820 TCGv t0 = tcg_temp_new();
6821 gen_qemu_ld16u(ctx, t0, addr);
6822 #if defined(TARGET_PPC64)
6823 tcg_gen_shli_tl(cpu_gpr[rD(ctx->opcode)], t0, 48);
6824 tcg_gen_shli_tl(t0, t0, 16);
6825 tcg_gen_or_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rD(ctx->opcode)], t0);
6827 tcg_gen_shli_tl(t0, t0, 16);
6828 tcg_gen_mov_tl(cpu_gprh[rD(ctx->opcode)], t0);
6829 tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], t0);
6834 static always_inline void gen_op_evlhhousplat(DisasContext *ctx, TCGv addr)
6836 TCGv t0 = tcg_temp_new();
6837 gen_qemu_ld16u(ctx, t0, addr);
6838 #if defined(TARGET_PPC64)
6839 tcg_gen_shli_tl(cpu_gpr[rD(ctx->opcode)], t0, 32);
6840 tcg_gen_or_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rD(ctx->opcode)], t0);
6842 tcg_gen_mov_tl(cpu_gprh[rD(ctx->opcode)], t0);
6843 tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], t0);
6848 static always_inline void gen_op_evlhhossplat(DisasContext *ctx, TCGv addr)
6850 TCGv t0 = tcg_temp_new();
6851 gen_qemu_ld16s(ctx, t0, addr);
6852 #if defined(TARGET_PPC64)
6853 tcg_gen_shli_tl(cpu_gpr[rD(ctx->opcode)], t0, 32);
6854 tcg_gen_ext32u_tl(t0, t0);
6855 tcg_gen_or_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rD(ctx->opcode)], t0);
6857 tcg_gen_mov_tl(cpu_gprh[rD(ctx->opcode)], t0);
6858 tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], t0);
6863 static always_inline void gen_op_evlwhe(DisasContext *ctx, TCGv addr)
6865 TCGv t0 = tcg_temp_new();
6866 #if defined(TARGET_PPC64)
6867 gen_qemu_ld16u(ctx, t0, addr);
6868 tcg_gen_shli_tl(cpu_gpr[rD(ctx->opcode)], t0, 48);
6869 gen_addr_add(ctx, addr, addr, 2);
6870 gen_qemu_ld16u(ctx, t0, addr);
6871 tcg_gen_shli_tl(t0, t0, 16);
6872 tcg_gen_or_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rD(ctx->opcode)], t0);
6874 gen_qemu_ld16u(ctx, t0, addr);
6875 tcg_gen_shli_tl(cpu_gprh[rD(ctx->opcode)], t0, 16);
6876 gen_addr_add(ctx, addr, addr, 2);
6877 gen_qemu_ld16u(ctx, t0, addr);
6878 tcg_gen_shli_tl(cpu_gpr[rD(ctx->opcode)], t0, 16);
6883 static always_inline void gen_op_evlwhou(DisasContext *ctx, TCGv addr)
6885 #if defined(TARGET_PPC64)
6886 TCGv t0 = tcg_temp_new();
6887 gen_qemu_ld16u(ctx, cpu_gpr[rD(ctx->opcode)], addr);
6888 gen_addr_add(ctx, addr, addr, 2);
6889 gen_qemu_ld16u(ctx, t0, addr);
6890 tcg_gen_shli_tl(t0, t0, 32);
6891 tcg_gen_or_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rD(ctx->opcode)], t0);
6894 gen_qemu_ld16u(ctx, cpu_gprh[rD(ctx->opcode)], addr);
6895 gen_addr_add(ctx, addr, addr, 2);
6896 gen_qemu_ld16u(ctx, cpu_gpr[rD(ctx->opcode)], addr);
6900 static always_inline void gen_op_evlwhos(DisasContext *ctx, TCGv addr)
6902 #if defined(TARGET_PPC64)
6903 TCGv t0 = tcg_temp_new();
6904 gen_qemu_ld16s(ctx, t0, addr);
6905 tcg_gen_ext32u_tl(cpu_gpr[rD(ctx->opcode)], t0);
6906 gen_addr_add(ctx, addr, addr, 2);
6907 gen_qemu_ld16s(ctx, t0, addr);
6908 tcg_gen_shli_tl(t0, t0, 32);
6909 tcg_gen_or_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rD(ctx->opcode)], t0);
6912 gen_qemu_ld16s(ctx, cpu_gprh[rD(ctx->opcode)], addr);
6913 gen_addr_add(ctx, addr, addr, 2);
6914 gen_qemu_ld16s(ctx, cpu_gpr[rD(ctx->opcode)], addr);
6918 static always_inline void gen_op_evlwwsplat(DisasContext *ctx, TCGv addr)
6920 TCGv t0 = tcg_temp_new();
6921 gen_qemu_ld32u(ctx, t0, addr);
6922 #if defined(TARGET_PPC64)
6923 tcg_gen_shli_tl(cpu_gpr[rD(ctx->opcode)], t0, 32);
6924 tcg_gen_or_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rD(ctx->opcode)], t0);
6926 tcg_gen_mov_tl(cpu_gprh[rD(ctx->opcode)], t0);
6927 tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], t0);
6932 static always_inline void gen_op_evlwhsplat(DisasContext *ctx, TCGv addr)
6934 TCGv t0 = tcg_temp_new();
6935 #if defined(TARGET_PPC64)
6936 gen_qemu_ld16u(ctx, t0, addr);
6937 tcg_gen_shli_tl(cpu_gpr[rD(ctx->opcode)], t0, 48);
6938 tcg_gen_shli_tl(t0, t0, 32);
6939 tcg_gen_or_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rD(ctx->opcode)], t0);
6940 gen_addr_add(ctx, addr, addr, 2);
6941 gen_qemu_ld16u(ctx, t0, addr);
6942 tcg_gen_or_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rD(ctx->opcode)], t0);
6943 tcg_gen_shli_tl(t0, t0, 16);
6944 tcg_gen_or_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rD(ctx->opcode)], t0);
6946 gen_qemu_ld16u(ctx, t0, addr);
6947 tcg_gen_shli_tl(cpu_gprh[rD(ctx->opcode)], t0, 16);
6948 tcg_gen_or_tl(cpu_gprh[rD(ctx->opcode)], cpu_gprh[rD(ctx->opcode)], t0);
6949 gen_addr_add(ctx, addr, addr, 2);
6950 gen_qemu_ld16u(ctx, t0, addr);
6951 tcg_gen_shli_tl(cpu_gpr[rD(ctx->opcode)], t0, 16);
6952 tcg_gen_or_tl(cpu_gpr[rD(ctx->opcode)], cpu_gprh[rD(ctx->opcode)], t0);
6957 static always_inline void gen_op_evstdd(DisasContext *ctx, TCGv addr)
6959 #if defined(TARGET_PPC64)
6960 gen_qemu_st64(ctx, cpu_gpr[rS(ctx->opcode)], addr);
6962 TCGv_i64 t0 = tcg_temp_new_i64();
6963 tcg_gen_concat_i32_i64(t0, cpu_gpr[rS(ctx->opcode)], cpu_gprh[rS(ctx->opcode)]);
6964 gen_qemu_st64(ctx, t0, addr);
6965 tcg_temp_free_i64(t0);
6969 static always_inline void gen_op_evstdw(DisasContext *ctx, TCGv addr)
6971 #if defined(TARGET_PPC64)
6972 TCGv t0 = tcg_temp_new();
6973 tcg_gen_shri_tl(t0, cpu_gpr[rS(ctx->opcode)], 32);
6974 gen_qemu_st32(ctx, t0, addr);
6977 gen_qemu_st32(ctx, cpu_gprh[rS(ctx->opcode)], addr);
6979 gen_addr_add(ctx, addr, addr, 4);
6980 gen_qemu_st32(ctx, cpu_gpr[rS(ctx->opcode)], addr);
6983 static always_inline void gen_op_evstdh(DisasContext *ctx, TCGv addr)
6985 TCGv t0 = tcg_temp_new();
6986 #if defined(TARGET_PPC64)
6987 tcg_gen_shri_tl(t0, cpu_gpr[rS(ctx->opcode)], 48);
6989 tcg_gen_shri_tl(t0, cpu_gprh[rS(ctx->opcode)], 16);
6991 gen_qemu_st16(ctx, t0, addr);
6992 gen_addr_add(ctx, addr, addr, 2);
6993 #if defined(TARGET_PPC64)
6994 tcg_gen_shri_tl(t0, cpu_gpr[rS(ctx->opcode)], 32);
6995 gen_qemu_st16(ctx, t0, addr);
6997 gen_qemu_st16(ctx, cpu_gprh[rS(ctx->opcode)], addr);
6999 gen_addr_add(ctx, addr, addr, 2);
7000 tcg_gen_shri_tl(t0, cpu_gpr[rS(ctx->opcode)], 16);
7001 gen_qemu_st16(ctx, t0, addr);
7003 gen_addr_add(ctx, addr, addr, 2);
7004 gen_qemu_st16(ctx, cpu_gpr[rS(ctx->opcode)], addr);
7007 static always_inline void gen_op_evstwhe(DisasContext *ctx, TCGv addr)
7009 TCGv t0 = tcg_temp_new();
7010 #if defined(TARGET_PPC64)
7011 tcg_gen_shri_tl(t0, cpu_gpr[rS(ctx->opcode)], 48);
7013 tcg_gen_shri_tl(t0, cpu_gprh[rS(ctx->opcode)], 16);
7015 gen_qemu_st16(ctx, t0, addr);
7016 gen_addr_add(ctx, addr, addr, 2);
7017 tcg_gen_shri_tl(t0, cpu_gpr[rS(ctx->opcode)], 16);
7018 gen_qemu_st16(ctx, t0, addr);
7022 static always_inline void gen_op_evstwho(DisasContext *ctx, TCGv addr)
7024 #if defined(TARGET_PPC64)
7025 TCGv t0 = tcg_temp_new();
7026 tcg_gen_shri_tl(t0, cpu_gpr[rS(ctx->opcode)], 32);
7027 gen_qemu_st16(ctx, t0, addr);
7030 gen_qemu_st16(ctx, cpu_gprh[rS(ctx->opcode)], addr);
7032 gen_addr_add(ctx, addr, addr, 2);
7033 gen_qemu_st16(ctx, cpu_gpr[rS(ctx->opcode)], addr);
7036 static always_inline void gen_op_evstwwe(DisasContext *ctx, TCGv addr)
7038 #if defined(TARGET_PPC64)
7039 TCGv t0 = tcg_temp_new();
7040 tcg_gen_shri_tl(t0, cpu_gpr[rS(ctx->opcode)], 32);
7041 gen_qemu_st32(ctx, t0, addr);
7044 gen_qemu_st32(ctx, cpu_gprh[rS(ctx->opcode)], addr);
7048 static always_inline void gen_op_evstwwo(DisasContext *ctx, TCGv addr)
7050 gen_qemu_st32(ctx, cpu_gpr[rS(ctx->opcode)], addr);
7053 #define GEN_SPEOP_LDST(name, opc2, sh) \
7054 GEN_HANDLER(name, 0x04, opc2, 0x0C, 0x00000000, PPC_SPE) \
7057 if (unlikely(!ctx->spe_enabled)) { \
7058 gen_exception(ctx, POWERPC_EXCP_APU); \
7061 gen_set_access_type(ctx, ACCESS_INT); \
7062 t0 = tcg_temp_new(); \
7063 if (Rc(ctx->opcode)) { \
7064 gen_addr_spe_imm_index(ctx, t0, sh); \
7066 gen_addr_reg_index(ctx, t0); \
7068 gen_op_##name(ctx, t0); \
7069 tcg_temp_free(t0); \
7072 GEN_SPEOP_LDST(evldd, 0x00, 3);
7073 GEN_SPEOP_LDST(evldw, 0x01, 3);
7074 GEN_SPEOP_LDST(evldh, 0x02, 3);
7075 GEN_SPEOP_LDST(evlhhesplat, 0x04, 1);
7076 GEN_SPEOP_LDST(evlhhousplat, 0x06, 1);
7077 GEN_SPEOP_LDST(evlhhossplat, 0x07, 1);
7078 GEN_SPEOP_LDST(evlwhe, 0x08, 2);
7079 GEN_SPEOP_LDST(evlwhou, 0x0A, 2);
7080 GEN_SPEOP_LDST(evlwhos, 0x0B, 2);
7081 GEN_SPEOP_LDST(evlwwsplat, 0x0C, 2);
7082 GEN_SPEOP_LDST(evlwhsplat, 0x0E, 2);
7084 GEN_SPEOP_LDST(evstdd, 0x10, 3);
7085 GEN_SPEOP_LDST(evstdw, 0x11, 3);
7086 GEN_SPEOP_LDST(evstdh, 0x12, 3);
7087 GEN_SPEOP_LDST(evstwhe, 0x18, 2);
7088 GEN_SPEOP_LDST(evstwho, 0x1A, 2);
7089 GEN_SPEOP_LDST(evstwwe, 0x1C, 2);
7090 GEN_SPEOP_LDST(evstwwo, 0x1E, 2);
7092 /* Multiply and add - TODO */
7094 GEN_SPE(speundef, evmhessf, 0x01, 0x10, 0x00000000, PPC_SPE);
7095 GEN_SPE(speundef, evmhossf, 0x03, 0x10, 0x00000000, PPC_SPE);
7096 GEN_SPE(evmheumi, evmhesmi, 0x04, 0x10, 0x00000000, PPC_SPE);
7097 GEN_SPE(speundef, evmhesmf, 0x05, 0x10, 0x00000000, PPC_SPE);
7098 GEN_SPE(evmhoumi, evmhosmi, 0x06, 0x10, 0x00000000, PPC_SPE);
7099 GEN_SPE(speundef, evmhosmf, 0x07, 0x10, 0x00000000, PPC_SPE);
7100 GEN_SPE(speundef, evmhessfa, 0x11, 0x10, 0x00000000, PPC_SPE);
7101 GEN_SPE(speundef, evmhossfa, 0x13, 0x10, 0x00000000, PPC_SPE);
7102 GEN_SPE(evmheumia, evmhesmia, 0x14, 0x10, 0x00000000, PPC_SPE);
7103 GEN_SPE(speundef, evmhesmfa, 0x15, 0x10, 0x00000000, PPC_SPE);
7104 GEN_SPE(evmhoumia, evmhosmia, 0x16, 0x10, 0x00000000, PPC_SPE);
7105 GEN_SPE(speundef, evmhosmfa, 0x17, 0x10, 0x00000000, PPC_SPE);
7107 GEN_SPE(speundef, evmwhssf, 0x03, 0x11, 0x00000000, PPC_SPE);
7108 GEN_SPE(evmwlumi, speundef, 0x04, 0x11, 0x00000000, PPC_SPE);
7109 GEN_SPE(evmwhumi, evmwhsmi, 0x06, 0x11, 0x00000000, PPC_SPE);
7110 GEN_SPE(speundef, evmwhsmf, 0x07, 0x11, 0x00000000, PPC_SPE);
7111 GEN_SPE(speundef, evmwssf, 0x09, 0x11, 0x00000000, PPC_SPE);
7112 GEN_SPE(evmwumi, evmwsmi, 0x0C, 0x11, 0x00000000, PPC_SPE);
7113 GEN_SPE(speundef, evmwsmf, 0x0D, 0x11, 0x00000000, PPC_SPE);
7114 GEN_SPE(speundef, evmwhssfa, 0x13, 0x11, 0x00000000, PPC_SPE);
7115 GEN_SPE(evmwlumia, speundef, 0x14, 0x11, 0x00000000, PPC_SPE);
7116 GEN_SPE(evmwhumia, evmwhsmia, 0x16, 0x11, 0x00000000, PPC_SPE);
7117 GEN_SPE(speundef, evmwhsmfa, 0x17, 0x11, 0x00000000, PPC_SPE);
7118 GEN_SPE(speundef, evmwssfa, 0x19, 0x11, 0x00000000, PPC_SPE);
7119 GEN_SPE(evmwumia, evmwsmia, 0x1C, 0x11, 0x00000000, PPC_SPE);
7120 GEN_SPE(speundef, evmwsmfa, 0x1D, 0x11, 0x00000000, PPC_SPE);
7122 GEN_SPE(evadduiaaw, evaddsiaaw, 0x00, 0x13, 0x0000F800, PPC_SPE);
7123 GEN_SPE(evsubfusiaaw, evsubfssiaaw, 0x01, 0x13, 0x0000F800, PPC_SPE);
7124 GEN_SPE(evaddumiaaw, evaddsmiaaw, 0x04, 0x13, 0x0000F800, PPC_SPE);
7125 GEN_SPE(evsubfumiaaw, evsubfsmiaaw, 0x05, 0x13, 0x0000F800, PPC_SPE);
7126 GEN_SPE(evdivws, evdivwu, 0x06, 0x13, 0x00000000, PPC_SPE);
7127 GEN_SPE(evmra, speundef, 0x07, 0x13, 0x0000F800, PPC_SPE);
7129 GEN_SPE(evmheusiaaw, evmhessiaaw, 0x00, 0x14, 0x00000000, PPC_SPE);
7130 GEN_SPE(speundef, evmhessfaaw, 0x01, 0x14, 0x00000000, PPC_SPE);
7131 GEN_SPE(evmhousiaaw, evmhossiaaw, 0x02, 0x14, 0x00000000, PPC_SPE);
7132 GEN_SPE(speundef, evmhossfaaw, 0x03, 0x14, 0x00000000, PPC_SPE);
7133 GEN_SPE(evmheumiaaw, evmhesmiaaw, 0x04, 0x14, 0x00000000, PPC_SPE);
7134 GEN_SPE(speundef, evmhesmfaaw, 0x05, 0x14, 0x00000000, PPC_SPE);
7135 GEN_SPE(evmhoumiaaw, evmhosmiaaw, 0x06, 0x14, 0x00000000, PPC_SPE);
7136 GEN_SPE(speundef, evmhosmfaaw, 0x07, 0x14, 0x00000000, PPC_SPE);
7137 GEN_SPE(evmhegumiaa, evmhegsmiaa, 0x14, 0x14, 0x00000000, PPC_SPE);
7138 GEN_SPE(speundef, evmhegsmfaa, 0x15, 0x14, 0x00000000, PPC_SPE);
7139 GEN_SPE(evmhogumiaa, evmhogsmiaa, 0x16, 0x14, 0x00000000, PPC_SPE);
7140 GEN_SPE(speundef, evmhogsmfaa, 0x17, 0x14, 0x00000000, PPC_SPE);
7142 GEN_SPE(evmwlusiaaw, evmwlssiaaw, 0x00, 0x15, 0x00000000, PPC_SPE);
7143 GEN_SPE(evmwlumiaaw, evmwlsmiaaw, 0x04, 0x15, 0x00000000, PPC_SPE);
7144 GEN_SPE(speundef, evmwssfaa, 0x09, 0x15, 0x00000000, PPC_SPE);
7145 GEN_SPE(evmwumiaa, evmwsmiaa, 0x0C, 0x15, 0x00000000, PPC_SPE);
7146 GEN_SPE(speundef, evmwsmfaa, 0x0D, 0x15, 0x00000000, PPC_SPE);
7148 GEN_SPE(evmheusianw, evmhessianw, 0x00, 0x16, 0x00000000, PPC_SPE);
7149 GEN_SPE(speundef, evmhessfanw, 0x01, 0x16, 0x00000000, PPC_SPE);
7150 GEN_SPE(evmhousianw, evmhossianw, 0x02, 0x16, 0x00000000, PPC_SPE);
7151 GEN_SPE(speundef, evmhossfanw, 0x03, 0x16, 0x00000000, PPC_SPE);
7152 GEN_SPE(evmheumianw, evmhesmianw, 0x04, 0x16, 0x00000000, PPC_SPE);
7153 GEN_SPE(speundef, evmhesmfanw, 0x05, 0x16, 0x00000000, PPC_SPE);
7154 GEN_SPE(evmhoumianw, evmhosmianw, 0x06, 0x16, 0x00000000, PPC_SPE);
7155 GEN_SPE(speundef, evmhosmfanw, 0x07, 0x16, 0x00000000, PPC_SPE);
7156 GEN_SPE(evmhegumian, evmhegsmian, 0x14, 0x16, 0x00000000, PPC_SPE);
7157 GEN_SPE(speundef, evmhegsmfan, 0x15, 0x16, 0x00000000, PPC_SPE);
7158 GEN_SPE(evmhigumian, evmhigsmian, 0x16, 0x16, 0x00000000, PPC_SPE);
7159 GEN_SPE(speundef, evmhogsmfan, 0x17, 0x16, 0x00000000, PPC_SPE);
7161 GEN_SPE(evmwlusianw, evmwlssianw, 0x00, 0x17, 0x00000000, PPC_SPE);
7162 GEN_SPE(evmwlumianw, evmwlsmianw, 0x04, 0x17, 0x00000000, PPC_SPE);
7163 GEN_SPE(speundef, evmwssfan, 0x09, 0x17, 0x00000000, PPC_SPE);
7164 GEN_SPE(evmwumian, evmwsmian, 0x0C, 0x17, 0x00000000, PPC_SPE);
7165 GEN_SPE(speundef, evmwsmfan, 0x0D, 0x17, 0x00000000, PPC_SPE);
7168 /*** SPE floating-point extension ***/
7169 #if defined(TARGET_PPC64)
7170 #define GEN_SPEFPUOP_CONV_32_32(name) \
7171 static always_inline void gen_##name (DisasContext *ctx) \
7175 t0 = tcg_temp_new_i32(); \
7176 tcg_gen_trunc_tl_i32(t0, cpu_gpr[rB(ctx->opcode)]); \
7177 gen_helper_##name(t0, t0); \
7178 t1 = tcg_temp_new(); \
7179 tcg_gen_extu_i32_tl(t1, t0); \
7180 tcg_temp_free_i32(t0); \
7181 tcg_gen_andi_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rD(ctx->opcode)], \
7182 0xFFFFFFFF00000000ULL); \
7183 tcg_gen_or_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rD(ctx->opcode)], t1); \
7184 tcg_temp_free(t1); \
7186 #define GEN_SPEFPUOP_CONV_32_64(name) \
7187 static always_inline void gen_##name (DisasContext *ctx) \
7191 t0 = tcg_temp_new_i32(); \
7192 gen_helper_##name(t0, cpu_gpr[rB(ctx->opcode)]); \
7193 t1 = tcg_temp_new(); \
7194 tcg_gen_extu_i32_tl(t1, t0); \
7195 tcg_temp_free_i32(t0); \
7196 tcg_gen_andi_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rD(ctx->opcode)], \
7197 0xFFFFFFFF00000000ULL); \
7198 tcg_gen_or_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rD(ctx->opcode)], t1); \
7199 tcg_temp_free(t1); \
7201 #define GEN_SPEFPUOP_CONV_64_32(name) \
7202 static always_inline void gen_##name (DisasContext *ctx) \
7204 TCGv_i32 t0 = tcg_temp_new_i32(); \
7205 tcg_gen_trunc_tl_i32(t0, cpu_gpr[rB(ctx->opcode)]); \
7206 gen_helper_##name(cpu_gpr[rD(ctx->opcode)], t0); \
7207 tcg_temp_free_i32(t0); \
7209 #define GEN_SPEFPUOP_CONV_64_64(name) \
7210 static always_inline void gen_##name (DisasContext *ctx) \
7212 gen_helper_##name(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rB(ctx->opcode)]); \
7214 #define GEN_SPEFPUOP_ARITH2_32_32(name) \
7215 static always_inline void gen_##name (DisasContext *ctx) \
7219 if (unlikely(!ctx->spe_enabled)) { \
7220 gen_exception(ctx, POWERPC_EXCP_APU); \
7223 t0 = tcg_temp_new_i32(); \
7224 t1 = tcg_temp_new_i32(); \
7225 tcg_gen_trunc_tl_i32(t0, cpu_gpr[rA(ctx->opcode)]); \
7226 tcg_gen_trunc_tl_i32(t1, cpu_gpr[rB(ctx->opcode)]); \
7227 gen_helper_##name(t0, t0, t1); \
7228 tcg_temp_free_i32(t1); \
7229 t2 = tcg_temp_new(); \
7230 tcg_gen_extu_i32_tl(t2, t0); \
7231 tcg_temp_free_i32(t0); \
7232 tcg_gen_andi_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rD(ctx->opcode)], \
7233 0xFFFFFFFF00000000ULL); \
7234 tcg_gen_or_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rD(ctx->opcode)], t2); \
7235 tcg_temp_free(t2); \
7237 #define GEN_SPEFPUOP_ARITH2_64_64(name) \
7238 static always_inline void gen_##name (DisasContext *ctx) \
7240 if (unlikely(!ctx->spe_enabled)) { \
7241 gen_exception(ctx, POWERPC_EXCP_APU); \
7244 gen_helper_##name(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)], \
7245 cpu_gpr[rB(ctx->opcode)]); \
7247 #define GEN_SPEFPUOP_COMP_32(name) \
7248 static always_inline void gen_##name (DisasContext *ctx) \
7251 if (unlikely(!ctx->spe_enabled)) { \
7252 gen_exception(ctx, POWERPC_EXCP_APU); \
7255 t0 = tcg_temp_new_i32(); \
7256 t1 = tcg_temp_new_i32(); \
7257 tcg_gen_trunc_tl_i32(t0, cpu_gpr[rA(ctx->opcode)]); \
7258 tcg_gen_trunc_tl_i32(t1, cpu_gpr[rB(ctx->opcode)]); \
7259 gen_helper_##name(cpu_crf[crfD(ctx->opcode)], t0, t1); \
7260 tcg_temp_free_i32(t0); \
7261 tcg_temp_free_i32(t1); \
7263 #define GEN_SPEFPUOP_COMP_64(name) \
7264 static always_inline void gen_##name (DisasContext *ctx) \
7266 if (unlikely(!ctx->spe_enabled)) { \
7267 gen_exception(ctx, POWERPC_EXCP_APU); \
7270 gen_helper_##name(cpu_crf[crfD(ctx->opcode)], \
7271 cpu_gpr[rA(ctx->opcode)], cpu_gpr[rB(ctx->opcode)]); \
7274 #define GEN_SPEFPUOP_CONV_32_32(name) \
7275 static always_inline void gen_##name (DisasContext *ctx) \
7277 gen_helper_##name(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rB(ctx->opcode)]); \
7279 #define GEN_SPEFPUOP_CONV_32_64(name) \
7280 static always_inline void gen_##name (DisasContext *ctx) \
7282 TCGv_i64 t0 = tcg_temp_new_i64(); \
7283 gen_load_gpr64(t0, rB(ctx->opcode)); \
7284 gen_helper_##name(cpu_gpr[rD(ctx->opcode)], t0); \
7285 tcg_temp_free_i64(t0); \
7287 #define GEN_SPEFPUOP_CONV_64_32(name) \
7288 static always_inline void gen_##name (DisasContext *ctx) \
7290 TCGv_i64 t0 = tcg_temp_new_i64(); \
7291 gen_helper_##name(t0, cpu_gpr[rB(ctx->opcode)]); \
7292 gen_store_gpr64(rD(ctx->opcode), t0); \
7293 tcg_temp_free_i64(t0); \
7295 #define GEN_SPEFPUOP_CONV_64_64(name) \
7296 static always_inline void gen_##name (DisasContext *ctx) \
7298 TCGv_i64 t0 = tcg_temp_new_i64(); \
7299 gen_load_gpr64(t0, rB(ctx->opcode)); \
7300 gen_helper_##name(t0, t0); \
7301 gen_store_gpr64(rD(ctx->opcode), t0); \
7302 tcg_temp_free_i64(t0); \
7304 #define GEN_SPEFPUOP_ARITH2_32_32(name) \
7305 static always_inline void gen_##name (DisasContext *ctx) \
7307 if (unlikely(!ctx->spe_enabled)) { \
7308 gen_exception(ctx, POWERPC_EXCP_APU); \
7311 gen_helper_##name(cpu_gpr[rD(ctx->opcode)], \
7312 cpu_gpr[rA(ctx->opcode)], cpu_gpr[rB(ctx->opcode)]); \
7314 #define GEN_SPEFPUOP_ARITH2_64_64(name) \
7315 static always_inline void gen_##name (DisasContext *ctx) \
7318 if (unlikely(!ctx->spe_enabled)) { \
7319 gen_exception(ctx, POWERPC_EXCP_APU); \
7322 t0 = tcg_temp_new_i64(); \
7323 t1 = tcg_temp_new_i64(); \
7324 gen_load_gpr64(t0, rA(ctx->opcode)); \
7325 gen_load_gpr64(t1, rB(ctx->opcode)); \
7326 gen_helper_##name(t0, t0, t1); \
7327 gen_store_gpr64(rD(ctx->opcode), t0); \
7328 tcg_temp_free_i64(t0); \
7329 tcg_temp_free_i64(t1); \
7331 #define GEN_SPEFPUOP_COMP_32(name) \
7332 static always_inline void gen_##name (DisasContext *ctx) \
7334 if (unlikely(!ctx->spe_enabled)) { \
7335 gen_exception(ctx, POWERPC_EXCP_APU); \
7338 gen_helper_##name(cpu_crf[crfD(ctx->opcode)], \
7339 cpu_gpr[rA(ctx->opcode)], cpu_gpr[rB(ctx->opcode)]); \
7341 #define GEN_SPEFPUOP_COMP_64(name) \
7342 static always_inline void gen_##name (DisasContext *ctx) \
7345 if (unlikely(!ctx->spe_enabled)) { \
7346 gen_exception(ctx, POWERPC_EXCP_APU); \
7349 t0 = tcg_temp_new_i64(); \
7350 t1 = tcg_temp_new_i64(); \
7351 gen_load_gpr64(t0, rA(ctx->opcode)); \
7352 gen_load_gpr64(t1, rB(ctx->opcode)); \
7353 gen_helper_##name(cpu_crf[crfD(ctx->opcode)], t0, t1); \
7354 tcg_temp_free_i64(t0); \
7355 tcg_temp_free_i64(t1); \
7359 /* Single precision floating-point vectors operations */
7361 GEN_SPEFPUOP_ARITH2_64_64(evfsadd);
7362 GEN_SPEFPUOP_ARITH2_64_64(evfssub);
7363 GEN_SPEFPUOP_ARITH2_64_64(evfsmul);
7364 GEN_SPEFPUOP_ARITH2_64_64(evfsdiv);
7365 static always_inline void gen_evfsabs (DisasContext *ctx)
7367 if (unlikely(!ctx->spe_enabled)) {
7368 gen_exception(ctx, POWERPC_EXCP_APU);
7371 #if defined(TARGET_PPC64)
7372 tcg_gen_andi_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rA(ctx->opcode)], ~0x8000000080000000LL);
7374 tcg_gen_andi_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rA(ctx->opcode)], ~0x80000000);
7375 tcg_gen_andi_tl(cpu_gprh[rA(ctx->opcode)], cpu_gprh[rA(ctx->opcode)], ~0x80000000);
7378 static always_inline void gen_evfsnabs (DisasContext *ctx)
7380 if (unlikely(!ctx->spe_enabled)) {
7381 gen_exception(ctx, POWERPC_EXCP_APU);
7384 #if defined(TARGET_PPC64)
7385 tcg_gen_ori_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rA(ctx->opcode)], 0x8000000080000000LL);
7387 tcg_gen_ori_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rA(ctx->opcode)], 0x80000000);
7388 tcg_gen_ori_tl(cpu_gprh[rA(ctx->opcode)], cpu_gprh[rA(ctx->opcode)], 0x80000000);
7391 static always_inline void gen_evfsneg (DisasContext *ctx)
7393 if (unlikely(!ctx->spe_enabled)) {
7394 gen_exception(ctx, POWERPC_EXCP_APU);
7397 #if defined(TARGET_PPC64)
7398 tcg_gen_xori_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rA(ctx->opcode)], 0x8000000080000000LL);
7400 tcg_gen_xori_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rA(ctx->opcode)], 0x80000000);
7401 tcg_gen_xori_tl(cpu_gprh[rA(ctx->opcode)], cpu_gprh[rA(ctx->opcode)], 0x80000000);
7406 GEN_SPEFPUOP_CONV_64_64(evfscfui);
7407 GEN_SPEFPUOP_CONV_64_64(evfscfsi);
7408 GEN_SPEFPUOP_CONV_64_64(evfscfuf);
7409 GEN_SPEFPUOP_CONV_64_64(evfscfsf);
7410 GEN_SPEFPUOP_CONV_64_64(evfsctui);
7411 GEN_SPEFPUOP_CONV_64_64(evfsctsi);
7412 GEN_SPEFPUOP_CONV_64_64(evfsctuf);
7413 GEN_SPEFPUOP_CONV_64_64(evfsctsf);
7414 GEN_SPEFPUOP_CONV_64_64(evfsctuiz);
7415 GEN_SPEFPUOP_CONV_64_64(evfsctsiz);
7418 GEN_SPEFPUOP_COMP_64(evfscmpgt);
7419 GEN_SPEFPUOP_COMP_64(evfscmplt);
7420 GEN_SPEFPUOP_COMP_64(evfscmpeq);
7421 GEN_SPEFPUOP_COMP_64(evfststgt);
7422 GEN_SPEFPUOP_COMP_64(evfststlt);
7423 GEN_SPEFPUOP_COMP_64(evfststeq);
7425 /* Opcodes definitions */
7426 GEN_SPE(evfsadd, evfssub, 0x00, 0x0A, 0x00000000, PPC_SPEFPU); //
7427 GEN_SPE(evfsabs, evfsnabs, 0x02, 0x0A, 0x0000F800, PPC_SPEFPU); //
7428 GEN_SPE(evfsneg, speundef, 0x03, 0x0A, 0x0000F800, PPC_SPEFPU); //
7429 GEN_SPE(evfsmul, evfsdiv, 0x04, 0x0A, 0x00000000, PPC_SPEFPU); //
7430 GEN_SPE(evfscmpgt, evfscmplt, 0x06, 0x0A, 0x00600000, PPC_SPEFPU); //
7431 GEN_SPE(evfscmpeq, speundef, 0x07, 0x0A, 0x00600000, PPC_SPEFPU); //
7432 GEN_SPE(evfscfui, evfscfsi, 0x08, 0x0A, 0x00180000, PPC_SPEFPU); //
7433 GEN_SPE(evfscfuf, evfscfsf, 0x09, 0x0A, 0x00180000, PPC_SPEFPU); //
7434 GEN_SPE(evfsctui, evfsctsi, 0x0A, 0x0A, 0x00180000, PPC_SPEFPU); //
7435 GEN_SPE(evfsctuf, evfsctsf, 0x0B, 0x0A, 0x00180000, PPC_SPEFPU); //
7436 GEN_SPE(evfsctuiz, speundef, 0x0C, 0x0A, 0x00180000, PPC_SPEFPU); //
7437 GEN_SPE(evfsctsiz, speundef, 0x0D, 0x0A, 0x00180000, PPC_SPEFPU); //
7438 GEN_SPE(evfststgt, evfststlt, 0x0E, 0x0A, 0x00600000, PPC_SPEFPU); //
7439 GEN_SPE(evfststeq, speundef, 0x0F, 0x0A, 0x00600000, PPC_SPEFPU); //
7441 /* Single precision floating-point operations */
7443 GEN_SPEFPUOP_ARITH2_32_32(efsadd);
7444 GEN_SPEFPUOP_ARITH2_32_32(efssub);
7445 GEN_SPEFPUOP_ARITH2_32_32(efsmul);
7446 GEN_SPEFPUOP_ARITH2_32_32(efsdiv);
7447 static always_inline void gen_efsabs (DisasContext *ctx)
7449 if (unlikely(!ctx->spe_enabled)) {
7450 gen_exception(ctx, POWERPC_EXCP_APU);
7453 tcg_gen_andi_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rA(ctx->opcode)], (target_long)~0x80000000LL);
7455 static always_inline void gen_efsnabs (DisasContext *ctx)
7457 if (unlikely(!ctx->spe_enabled)) {
7458 gen_exception(ctx, POWERPC_EXCP_APU);
7461 tcg_gen_ori_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rA(ctx->opcode)], 0x80000000);
7463 static always_inline void gen_efsneg (DisasContext *ctx)
7465 if (unlikely(!ctx->spe_enabled)) {
7466 gen_exception(ctx, POWERPC_EXCP_APU);
7469 tcg_gen_xori_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rA(ctx->opcode)], 0x80000000);
7473 GEN_SPEFPUOP_CONV_32_32(efscfui);
7474 GEN_SPEFPUOP_CONV_32_32(efscfsi);
7475 GEN_SPEFPUOP_CONV_32_32(efscfuf);
7476 GEN_SPEFPUOP_CONV_32_32(efscfsf);
7477 GEN_SPEFPUOP_CONV_32_32(efsctui);
7478 GEN_SPEFPUOP_CONV_32_32(efsctsi);
7479 GEN_SPEFPUOP_CONV_32_32(efsctuf);
7480 GEN_SPEFPUOP_CONV_32_32(efsctsf);
7481 GEN_SPEFPUOP_CONV_32_32(efsctuiz);
7482 GEN_SPEFPUOP_CONV_32_32(efsctsiz);
7483 GEN_SPEFPUOP_CONV_32_64(efscfd);
7486 GEN_SPEFPUOP_COMP_32(efscmpgt);
7487 GEN_SPEFPUOP_COMP_32(efscmplt);
7488 GEN_SPEFPUOP_COMP_32(efscmpeq);
7489 GEN_SPEFPUOP_COMP_32(efststgt);
7490 GEN_SPEFPUOP_COMP_32(efststlt);
7491 GEN_SPEFPUOP_COMP_32(efststeq);
7493 /* Opcodes definitions */
7494 GEN_SPE(efsadd, efssub, 0x00, 0x0B, 0x00000000, PPC_SPEFPU); //
7495 GEN_SPE(efsabs, efsnabs, 0x02, 0x0B, 0x0000F800, PPC_SPEFPU); //
7496 GEN_SPE(efsneg, speundef, 0x03, 0x0B, 0x0000F800, PPC_SPEFPU); //
7497 GEN_SPE(efsmul, efsdiv, 0x04, 0x0B, 0x00000000, PPC_SPEFPU); //
7498 GEN_SPE(efscmpgt, efscmplt, 0x06, 0x0B, 0x00600000, PPC_SPEFPU); //
7499 GEN_SPE(efscmpeq, efscfd, 0x07, 0x0B, 0x00600000, PPC_SPEFPU); //
7500 GEN_SPE(efscfui, efscfsi, 0x08, 0x0B, 0x00180000, PPC_SPEFPU); //
7501 GEN_SPE(efscfuf, efscfsf, 0x09, 0x0B, 0x00180000, PPC_SPEFPU); //
7502 GEN_SPE(efsctui, efsctsi, 0x0A, 0x0B, 0x00180000, PPC_SPEFPU); //
7503 GEN_SPE(efsctuf, efsctsf, 0x0B, 0x0B, 0x00180000, PPC_SPEFPU); //
7504 GEN_SPE(efsctuiz, speundef, 0x0C, 0x0B, 0x00180000, PPC_SPEFPU); //
7505 GEN_SPE(efsctsiz, speundef, 0x0D, 0x0B, 0x00180000, PPC_SPEFPU); //
7506 GEN_SPE(efststgt, efststlt, 0x0E, 0x0B, 0x00600000, PPC_SPEFPU); //
7507 GEN_SPE(efststeq, speundef, 0x0F, 0x0B, 0x00600000, PPC_SPEFPU); //
7509 /* Double precision floating-point operations */
7511 GEN_SPEFPUOP_ARITH2_64_64(efdadd);
7512 GEN_SPEFPUOP_ARITH2_64_64(efdsub);
7513 GEN_SPEFPUOP_ARITH2_64_64(efdmul);
7514 GEN_SPEFPUOP_ARITH2_64_64(efddiv);
7515 static always_inline void gen_efdabs (DisasContext *ctx)
7517 if (unlikely(!ctx->spe_enabled)) {
7518 gen_exception(ctx, POWERPC_EXCP_APU);
7521 #if defined(TARGET_PPC64)
7522 tcg_gen_andi_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rA(ctx->opcode)], ~0x8000000000000000LL);
7524 tcg_gen_andi_tl(cpu_gprh[rA(ctx->opcode)], cpu_gprh[rA(ctx->opcode)], ~0x80000000);
7527 static always_inline void gen_efdnabs (DisasContext *ctx)
7529 if (unlikely(!ctx->spe_enabled)) {
7530 gen_exception(ctx, POWERPC_EXCP_APU);
7533 #if defined(TARGET_PPC64)
7534 tcg_gen_ori_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rA(ctx->opcode)], 0x8000000000000000LL);
7536 tcg_gen_ori_tl(cpu_gprh[rA(ctx->opcode)], cpu_gprh[rA(ctx->opcode)], 0x80000000);
7539 static always_inline void gen_efdneg (DisasContext *ctx)
7541 if (unlikely(!ctx->spe_enabled)) {
7542 gen_exception(ctx, POWERPC_EXCP_APU);
7545 #if defined(TARGET_PPC64)
7546 tcg_gen_xori_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rA(ctx->opcode)], 0x8000000000000000LL);
7548 tcg_gen_xori_tl(cpu_gprh[rA(ctx->opcode)], cpu_gprh[rA(ctx->opcode)], 0x80000000);
7553 GEN_SPEFPUOP_CONV_64_32(efdcfui);
7554 GEN_SPEFPUOP_CONV_64_32(efdcfsi);
7555 GEN_SPEFPUOP_CONV_64_32(efdcfuf);
7556 GEN_SPEFPUOP_CONV_64_32(efdcfsf);
7557 GEN_SPEFPUOP_CONV_32_64(efdctui);
7558 GEN_SPEFPUOP_CONV_32_64(efdctsi);
7559 GEN_SPEFPUOP_CONV_32_64(efdctuf);
7560 GEN_SPEFPUOP_CONV_32_64(efdctsf);
7561 GEN_SPEFPUOP_CONV_32_64(efdctuiz);
7562 GEN_SPEFPUOP_CONV_32_64(efdctsiz);
7563 GEN_SPEFPUOP_CONV_64_32(efdcfs);
7564 GEN_SPEFPUOP_CONV_64_64(efdcfuid);
7565 GEN_SPEFPUOP_CONV_64_64(efdcfsid);
7566 GEN_SPEFPUOP_CONV_64_64(efdctuidz);
7567 GEN_SPEFPUOP_CONV_64_64(efdctsidz);
7570 GEN_SPEFPUOP_COMP_64(efdcmpgt);
7571 GEN_SPEFPUOP_COMP_64(efdcmplt);
7572 GEN_SPEFPUOP_COMP_64(efdcmpeq);
7573 GEN_SPEFPUOP_COMP_64(efdtstgt);
7574 GEN_SPEFPUOP_COMP_64(efdtstlt);
7575 GEN_SPEFPUOP_COMP_64(efdtsteq);
7577 /* Opcodes definitions */
7578 GEN_SPE(efdadd, efdsub, 0x10, 0x0B, 0x00000000, PPC_SPEFPU); //
7579 GEN_SPE(efdcfuid, efdcfsid, 0x11, 0x0B, 0x00180000, PPC_SPEFPU); //
7580 GEN_SPE(efdabs, efdnabs, 0x12, 0x0B, 0x0000F800, PPC_SPEFPU); //
7581 GEN_SPE(efdneg, speundef, 0x13, 0x0B, 0x0000F800, PPC_SPEFPU); //
7582 GEN_SPE(efdmul, efddiv, 0x14, 0x0B, 0x00000000, PPC_SPEFPU); //
7583 GEN_SPE(efdctuidz, efdctsidz, 0x15, 0x0B, 0x00180000, PPC_SPEFPU); //
7584 GEN_SPE(efdcmpgt, efdcmplt, 0x16, 0x0B, 0x00600000, PPC_SPEFPU); //
7585 GEN_SPE(efdcmpeq, efdcfs, 0x17, 0x0B, 0x00600000, PPC_SPEFPU); //
7586 GEN_SPE(efdcfui, efdcfsi, 0x18, 0x0B, 0x00180000, PPC_SPEFPU); //
7587 GEN_SPE(efdcfuf, efdcfsf, 0x19, 0x0B, 0x00180000, PPC_SPEFPU); //
7588 GEN_SPE(efdctui, efdctsi, 0x1A, 0x0B, 0x00180000, PPC_SPEFPU); //
7589 GEN_SPE(efdctuf, efdctsf, 0x1B, 0x0B, 0x00180000, PPC_SPEFPU); //
7590 GEN_SPE(efdctuiz, speundef, 0x1C, 0x0B, 0x00180000, PPC_SPEFPU); //
7591 GEN_SPE(efdctsiz, speundef, 0x1D, 0x0B, 0x00180000, PPC_SPEFPU); //
7592 GEN_SPE(efdtstgt, efdtstlt, 0x1E, 0x0B, 0x00600000, PPC_SPEFPU); //
7593 GEN_SPE(efdtsteq, speundef, 0x1F, 0x0B, 0x00600000, PPC_SPEFPU); //
7595 /* End opcode list */
7596 GEN_OPCODE_MARK(end);
7598 #include "translate_init.c"
7599 #include "helper_regs.h"
7601 /*****************************************************************************/
7602 /* Misc PowerPC helpers */
7603 void cpu_dump_state (CPUState *env, FILE *f,
7604 int (*cpu_fprintf)(FILE *f, const char *fmt, ...),
7612 cpu_fprintf(f, "NIP " ADDRX " LR " ADDRX " CTR " ADDRX " XER %08x\n",
7613 env->nip, env->lr, env->ctr, env->xer);
7614 cpu_fprintf(f, "MSR " ADDRX " HID0 " ADDRX " HF " ADDRX " idx %d\n",
7615 env->msr, env->spr[SPR_HID0], env->hflags, env->mmu_idx);
7616 #if !defined(NO_TIMER_DUMP)
7617 cpu_fprintf(f, "TB %08x %08x "
7618 #if !defined(CONFIG_USER_ONLY)
7622 cpu_ppc_load_tbu(env), cpu_ppc_load_tbl(env)
7623 #if !defined(CONFIG_USER_ONLY)
7624 , cpu_ppc_load_decr(env)
7628 for (i = 0; i < 32; i++) {
7629 if ((i & (RGPL - 1)) == 0)
7630 cpu_fprintf(f, "GPR%02d", i);
7631 cpu_fprintf(f, " " REGX, ppc_dump_gpr(env, i));
7632 if ((i & (RGPL - 1)) == (RGPL - 1))
7633 cpu_fprintf(f, "\n");
7635 cpu_fprintf(f, "CR ");
7636 for (i = 0; i < 8; i++)
7637 cpu_fprintf(f, "%01x", env->crf[i]);
7638 cpu_fprintf(f, " [");
7639 for (i = 0; i < 8; i++) {
7641 if (env->crf[i] & 0x08)
7643 else if (env->crf[i] & 0x04)
7645 else if (env->crf[i] & 0x02)
7647 cpu_fprintf(f, " %c%c", a, env->crf[i] & 0x01 ? 'O' : ' ');
7649 cpu_fprintf(f, " ] RES " ADDRX "\n", env->reserve);
7650 for (i = 0; i < 32; i++) {
7651 if ((i & (RFPL - 1)) == 0)
7652 cpu_fprintf(f, "FPR%02d", i);
7653 cpu_fprintf(f, " %016" PRIx64, *((uint64_t *)&env->fpr[i]));
7654 if ((i & (RFPL - 1)) == (RFPL - 1))
7655 cpu_fprintf(f, "\n");
7657 cpu_fprintf(f, "FPSCR %08x\n", env->fpscr);
7658 #if !defined(CONFIG_USER_ONLY)
7659 cpu_fprintf(f, "SRR0 " ADDRX " SRR1 " ADDRX " SDR1 " ADDRX "\n",
7660 env->spr[SPR_SRR0], env->spr[SPR_SRR1], env->sdr1);
7667 void cpu_dump_statistics (CPUState *env, FILE*f,
7668 int (*cpu_fprintf)(FILE *f, const char *fmt, ...),
7671 #if defined(DO_PPC_STATISTICS)
7672 opc_handler_t **t1, **t2, **t3, *handler;
7676 for (op1 = 0; op1 < 64; op1++) {
7678 if (is_indirect_opcode(handler)) {
7679 t2 = ind_table(handler);
7680 for (op2 = 0; op2 < 32; op2++) {
7682 if (is_indirect_opcode(handler)) {
7683 t3 = ind_table(handler);
7684 for (op3 = 0; op3 < 32; op3++) {
7686 if (handler->count == 0)
7688 cpu_fprintf(f, "%02x %02x %02x (%02x %04d) %16s: "
7690 op1, op2, op3, op1, (op3 << 5) | op2,
7692 handler->count, handler->count);
7695 if (handler->count == 0)
7697 cpu_fprintf(f, "%02x %02x (%02x %04d) %16s: "
7699 op1, op2, op1, op2, handler->oname,
7700 handler->count, handler->count);
7704 if (handler->count == 0)
7706 cpu_fprintf(f, "%02x (%02x ) %16s: %016llx %lld\n",
7707 op1, op1, handler->oname,
7708 handler->count, handler->count);
7714 /*****************************************************************************/
7715 static always_inline void gen_intermediate_code_internal (CPUState *env,
7716 TranslationBlock *tb,
7719 DisasContext ctx, *ctxp = &ctx;
7720 opc_handler_t **table, *handler;
7721 target_ulong pc_start;
7722 uint16_t *gen_opc_end;
7729 gen_opc_end = gen_opc_buf + OPC_MAX_SIZE;
7730 #if defined(OPTIMIZE_FPRF_UPDATE)
7731 gen_fprf_ptr = gen_fprf_buf;
7735 ctx.exception = POWERPC_EXCP_NONE;
7736 ctx.spr_cb = env->spr_cb;
7737 ctx.mem_idx = env->mmu_idx;
7738 ctx.access_type = -1;
7739 ctx.le_mode = env->hflags & (1 << MSR_LE) ? 1 : 0;
7740 #if defined(TARGET_PPC64)
7741 ctx.sf_mode = msr_sf;
7743 ctx.fpu_enabled = msr_fp;
7744 if ((env->flags & POWERPC_FLAG_SPE) && msr_spe)
7745 ctx.spe_enabled = msr_spe;
7747 ctx.spe_enabled = 0;
7748 if ((env->flags & POWERPC_FLAG_VRE) && msr_vr)
7749 ctx.altivec_enabled = msr_vr;
7751 ctx.altivec_enabled = 0;
7752 if ((env->flags & POWERPC_FLAG_SE) && msr_se)
7753 ctx.singlestep_enabled = CPU_SINGLE_STEP;
7755 ctx.singlestep_enabled = 0;
7756 if ((env->flags & POWERPC_FLAG_BE) && msr_be)
7757 ctx.singlestep_enabled |= CPU_BRANCH_STEP;
7758 if (unlikely(env->singlestep_enabled))
7759 ctx.singlestep_enabled |= GDBSTUB_SINGLE_STEP;
7760 #if defined (DO_SINGLE_STEP) && 0
7761 /* Single step trace mode */
7765 max_insns = tb->cflags & CF_COUNT_MASK;
7767 max_insns = CF_COUNT_MASK;
7770 /* Set env in case of segfault during code fetch */
7771 while (ctx.exception == POWERPC_EXCP_NONE && gen_opc_ptr < gen_opc_end) {
7772 if (unlikely(!TAILQ_EMPTY(&env->breakpoints))) {
7773 TAILQ_FOREACH(bp, &env->breakpoints, entry) {
7774 if (bp->pc == ctx.nip) {
7775 gen_debug_exception(ctxp);
7780 if (unlikely(search_pc)) {
7781 j = gen_opc_ptr - gen_opc_buf;
7785 gen_opc_instr_start[lj++] = 0;
7786 gen_opc_pc[lj] = ctx.nip;
7787 gen_opc_instr_start[lj] = 1;
7788 gen_opc_icount[lj] = num_insns;
7791 #if defined PPC_DEBUG_DISAS
7792 if (loglevel & CPU_LOG_TB_IN_ASM) {
7793 fprintf(logfile, "----------------\n");
7794 fprintf(logfile, "nip=" ADDRX " super=%d ir=%d\n",
7795 ctx.nip, ctx.mem_idx, (int)msr_ir);
7798 if (num_insns + 1 == max_insns && (tb->cflags & CF_LAST_IO))
7800 if (unlikely(ctx.le_mode)) {
7801 ctx.opcode = bswap32(ldl_code(ctx.nip));
7803 ctx.opcode = ldl_code(ctx.nip);
7805 #if defined PPC_DEBUG_DISAS
7806 if (loglevel & CPU_LOG_TB_IN_ASM) {
7807 fprintf(logfile, "translate opcode %08x (%02x %02x %02x) (%s)\n",
7808 ctx.opcode, opc1(ctx.opcode), opc2(ctx.opcode),
7809 opc3(ctx.opcode), little_endian ? "little" : "big");
7813 table = env->opcodes;
7815 handler = table[opc1(ctx.opcode)];
7816 if (is_indirect_opcode(handler)) {
7817 table = ind_table(handler);
7818 handler = table[opc2(ctx.opcode)];
7819 if (is_indirect_opcode(handler)) {
7820 table = ind_table(handler);
7821 handler = table[opc3(ctx.opcode)];
7824 /* Is opcode *REALLY* valid ? */
7825 if (unlikely(handler->handler == &gen_invalid)) {
7826 if (loglevel != 0) {
7827 fprintf(logfile, "invalid/unsupported opcode: "
7828 "%02x - %02x - %02x (%08x) " ADDRX " %d\n",
7829 opc1(ctx.opcode), opc2(ctx.opcode),
7830 opc3(ctx.opcode), ctx.opcode, ctx.nip - 4, (int)msr_ir);
7832 printf("invalid/unsupported opcode: "
7833 "%02x - %02x - %02x (%08x) " ADDRX " %d\n",
7834 opc1(ctx.opcode), opc2(ctx.opcode),
7835 opc3(ctx.opcode), ctx.opcode, ctx.nip - 4, (int)msr_ir);
7838 if (unlikely((ctx.opcode & handler->inval) != 0)) {
7839 if (loglevel != 0) {
7840 fprintf(logfile, "invalid bits: %08x for opcode: "
7841 "%02x - %02x - %02x (%08x) " ADDRX "\n",
7842 ctx.opcode & handler->inval, opc1(ctx.opcode),
7843 opc2(ctx.opcode), opc3(ctx.opcode),
7844 ctx.opcode, ctx.nip - 4);
7846 printf("invalid bits: %08x for opcode: "
7847 "%02x - %02x - %02x (%08x) " ADDRX "\n",
7848 ctx.opcode & handler->inval, opc1(ctx.opcode),
7849 opc2(ctx.opcode), opc3(ctx.opcode),
7850 ctx.opcode, ctx.nip - 4);
7852 gen_inval_exception(ctxp, POWERPC_EXCP_INVAL_INVAL);
7856 (*(handler->handler))(&ctx);
7857 #if defined(DO_PPC_STATISTICS)
7860 /* Check trace mode exceptions */
7861 if (unlikely(ctx.singlestep_enabled & CPU_SINGLE_STEP &&
7862 (ctx.nip <= 0x100 || ctx.nip > 0xF00) &&
7863 ctx.exception != POWERPC_SYSCALL &&
7864 ctx.exception != POWERPC_EXCP_TRAP &&
7865 ctx.exception != POWERPC_EXCP_BRANCH)) {
7866 gen_exception(ctxp, POWERPC_EXCP_TRACE);
7867 } else if (unlikely(((ctx.nip & (TARGET_PAGE_SIZE - 1)) == 0) ||
7868 (env->singlestep_enabled) ||
7869 num_insns >= max_insns)) {
7870 /* if we reach a page boundary or are single stepping, stop
7875 #if defined (DO_SINGLE_STEP)
7879 if (tb->cflags & CF_LAST_IO)
7881 if (ctx.exception == POWERPC_EXCP_NONE) {
7882 gen_goto_tb(&ctx, 0, ctx.nip);
7883 } else if (ctx.exception != POWERPC_EXCP_BRANCH) {
7884 if (unlikely(env->singlestep_enabled)) {
7885 gen_debug_exception(ctxp);
7887 /* Generate the return instruction */
7890 gen_icount_end(tb, num_insns);
7891 *gen_opc_ptr = INDEX_op_end;
7892 if (unlikely(search_pc)) {
7893 j = gen_opc_ptr - gen_opc_buf;
7896 gen_opc_instr_start[lj++] = 0;
7898 tb->size = ctx.nip - pc_start;
7899 tb->icount = num_insns;
7901 #if defined(DEBUG_DISAS)
7902 if (loglevel & CPU_LOG_TB_CPU) {
7903 fprintf(logfile, "---------------- excp: %04x\n", ctx.exception);
7904 cpu_dump_state(env, logfile, fprintf, 0);
7906 if (loglevel & CPU_LOG_TB_IN_ASM) {
7908 flags = env->bfd_mach;
7909 flags |= ctx.le_mode << 16;
7910 fprintf(logfile, "IN: %s\n", lookup_symbol(pc_start));
7911 target_disas(logfile, pc_start, ctx.nip - pc_start, flags);
7912 fprintf(logfile, "\n");
7917 void gen_intermediate_code (CPUState *env, struct TranslationBlock *tb)
7919 gen_intermediate_code_internal(env, tb, 0);
7922 void gen_intermediate_code_pc (CPUState *env, struct TranslationBlock *tb)
7924 gen_intermediate_code_internal(env, tb, 1);
7927 void gen_pc_load(CPUState *env, TranslationBlock *tb,
7928 unsigned long searched_pc, int pc_pos, void *puc)
7930 env->nip = gen_opc_pc[pc_pos];