]> Git Repo - qemu.git/blame - target-ppc/translate.c
target-ppc: Altivec 2.07: vmuluw Instruction
[qemu.git] / target-ppc / translate.c
CommitLineData
79aceca5 1/*
3fc6c082 2 * PowerPC emulation for qemu: main translation routines.
5fafdf24 3 *
76a66253 4 * Copyright (c) 2003-2007 Jocelyn Mayer
90dc8812 5 * Copyright (C) 2011 Freescale Semiconductor, Inc.
79aceca5
FB
6 *
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2 of the License, or (at your option) any later version.
11 *
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
8167ee88 18 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
79aceca5 19 */
c6a1c22b 20
79aceca5 21#include "cpu.h"
76cad711 22#include "disas/disas.h"
57fec1fe 23#include "tcg-op.h"
1de7afc9 24#include "qemu/host-utils.h"
79aceca5 25
a7812ae4
PB
26#include "helper.h"
27#define GEN_HELPER 1
28#include "helper.h"
29
8cbcb4fa
AJ
30#define CPU_SINGLE_STEP 0x1
31#define CPU_BRANCH_STEP 0x2
32#define GDBSTUB_SINGLE_STEP 0x4
33
a750fc0b 34/* Include definitions for instructions classes and implementations flags */
9fddaa0c 35//#define PPC_DEBUG_DISAS
76a66253 36//#define DO_PPC_STATISTICS
79aceca5 37
d12d51d5 38#ifdef PPC_DEBUG_DISAS
93fcfe39 39# define LOG_DISAS(...) qemu_log_mask(CPU_LOG_TB_IN_ASM, ## __VA_ARGS__)
d12d51d5
AL
40#else
41# define LOG_DISAS(...) do { } while (0)
42#endif
a750fc0b
JM
43/*****************************************************************************/
44/* Code translation helpers */
c53be334 45
f78fb44e 46/* global register indexes */
a7812ae4 47static TCGv_ptr cpu_env;
1d542695 48static char cpu_reg_names[10*3 + 22*4 /* GPR */
f78fb44e 49#if !defined(TARGET_PPC64)
1d542695 50 + 10*4 + 22*5 /* SPE GPRh */
f78fb44e 51#endif
a5e26afa 52 + 10*4 + 22*5 /* FPR */
47e4661c 53 + 2*(10*6 + 22*7) /* AVRh, AVRl */
472b24ce 54 + 10*5 + 22*6 /* VSR */
47e4661c 55 + 8*5 /* CRF */];
f78fb44e
AJ
56static TCGv cpu_gpr[32];
57#if !defined(TARGET_PPC64)
58static TCGv cpu_gprh[32];
59#endif
a7812ae4
PB
60static TCGv_i64 cpu_fpr[32];
61static TCGv_i64 cpu_avrh[32], cpu_avrl[32];
472b24ce 62static TCGv_i64 cpu_vsr[32];
a7812ae4 63static TCGv_i32 cpu_crf[8];
bd568f18 64static TCGv cpu_nip;
6527f6ea 65static TCGv cpu_msr;
cfdcd37a
AJ
66static TCGv cpu_ctr;
67static TCGv cpu_lr;
697ab892
DG
68#if defined(TARGET_PPC64)
69static TCGv cpu_cfar;
70#endif
da91a00f 71static TCGv cpu_xer, cpu_so, cpu_ov, cpu_ca;
cf360a32 72static TCGv cpu_reserve;
30304420 73static TCGv cpu_fpscr;
a7859e89 74static TCGv_i32 cpu_access_type;
f78fb44e 75
022c62cb 76#include "exec/gen-icount.h"
2e70f6ef
PB
77
78void ppc_translate_init(void)
79{
f78fb44e
AJ
80 int i;
81 char* p;
2dc766da 82 size_t cpu_reg_names_size;
b2437bf2 83 static int done_init = 0;
f78fb44e 84
2e70f6ef
PB
85 if (done_init)
86 return;
f78fb44e 87
a7812ae4 88 cpu_env = tcg_global_reg_new_ptr(TCG_AREG0, "env");
a7812ae4 89
f78fb44e 90 p = cpu_reg_names;
2dc766da 91 cpu_reg_names_size = sizeof(cpu_reg_names);
47e4661c
AJ
92
93 for (i = 0; i < 8; i++) {
2dc766da 94 snprintf(p, cpu_reg_names_size, "crf%d", i);
a7812ae4 95 cpu_crf[i] = tcg_global_mem_new_i32(TCG_AREG0,
1328c2bf 96 offsetof(CPUPPCState, crf[i]), p);
47e4661c 97 p += 5;
2dc766da 98 cpu_reg_names_size -= 5;
47e4661c
AJ
99 }
100
f78fb44e 101 for (i = 0; i < 32; i++) {
2dc766da 102 snprintf(p, cpu_reg_names_size, "r%d", i);
a7812ae4 103 cpu_gpr[i] = tcg_global_mem_new(TCG_AREG0,
1328c2bf 104 offsetof(CPUPPCState, gpr[i]), p);
f78fb44e 105 p += (i < 10) ? 3 : 4;
2dc766da 106 cpu_reg_names_size -= (i < 10) ? 3 : 4;
f78fb44e 107#if !defined(TARGET_PPC64)
2dc766da 108 snprintf(p, cpu_reg_names_size, "r%dH", i);
a7812ae4 109 cpu_gprh[i] = tcg_global_mem_new_i32(TCG_AREG0,
1328c2bf 110 offsetof(CPUPPCState, gprh[i]), p);
f78fb44e 111 p += (i < 10) ? 4 : 5;
2dc766da 112 cpu_reg_names_size -= (i < 10) ? 4 : 5;
f78fb44e 113#endif
1d542695 114
2dc766da 115 snprintf(p, cpu_reg_names_size, "fp%d", i);
a7812ae4 116 cpu_fpr[i] = tcg_global_mem_new_i64(TCG_AREG0,
1328c2bf 117 offsetof(CPUPPCState, fpr[i]), p);
ec1ac72d 118 p += (i < 10) ? 4 : 5;
2dc766da 119 cpu_reg_names_size -= (i < 10) ? 4 : 5;
a5e26afa 120
2dc766da 121 snprintf(p, cpu_reg_names_size, "avr%dH", i);
e2542fe2 122#ifdef HOST_WORDS_BIGENDIAN
fe1e5c53 123 cpu_avrh[i] = tcg_global_mem_new_i64(TCG_AREG0,
1328c2bf 124 offsetof(CPUPPCState, avr[i].u64[0]), p);
fe1e5c53 125#else
a7812ae4 126 cpu_avrh[i] = tcg_global_mem_new_i64(TCG_AREG0,
1328c2bf 127 offsetof(CPUPPCState, avr[i].u64[1]), p);
fe1e5c53 128#endif
1d542695 129 p += (i < 10) ? 6 : 7;
2dc766da 130 cpu_reg_names_size -= (i < 10) ? 6 : 7;
ec1ac72d 131
2dc766da 132 snprintf(p, cpu_reg_names_size, "avr%dL", i);
e2542fe2 133#ifdef HOST_WORDS_BIGENDIAN
fe1e5c53 134 cpu_avrl[i] = tcg_global_mem_new_i64(TCG_AREG0,
1328c2bf 135 offsetof(CPUPPCState, avr[i].u64[1]), p);
fe1e5c53 136#else
a7812ae4 137 cpu_avrl[i] = tcg_global_mem_new_i64(TCG_AREG0,
1328c2bf 138 offsetof(CPUPPCState, avr[i].u64[0]), p);
fe1e5c53 139#endif
1d542695 140 p += (i < 10) ? 6 : 7;
2dc766da 141 cpu_reg_names_size -= (i < 10) ? 6 : 7;
472b24ce
TM
142 snprintf(p, cpu_reg_names_size, "vsr%d", i);
143 cpu_vsr[i] = tcg_global_mem_new_i64(TCG_AREG0,
144 offsetof(CPUPPCState, vsr[i]), p);
145 p += (i < 10) ? 5 : 6;
146 cpu_reg_names_size -= (i < 10) ? 5 : 6;
f78fb44e 147 }
f10dc08e 148
a7812ae4 149 cpu_nip = tcg_global_mem_new(TCG_AREG0,
1328c2bf 150 offsetof(CPUPPCState, nip), "nip");
bd568f18 151
6527f6ea 152 cpu_msr = tcg_global_mem_new(TCG_AREG0,
1328c2bf 153 offsetof(CPUPPCState, msr), "msr");
6527f6ea 154
a7812ae4 155 cpu_ctr = tcg_global_mem_new(TCG_AREG0,
1328c2bf 156 offsetof(CPUPPCState, ctr), "ctr");
cfdcd37a 157
a7812ae4 158 cpu_lr = tcg_global_mem_new(TCG_AREG0,
1328c2bf 159 offsetof(CPUPPCState, lr), "lr");
cfdcd37a 160
697ab892
DG
161#if defined(TARGET_PPC64)
162 cpu_cfar = tcg_global_mem_new(TCG_AREG0,
1328c2bf 163 offsetof(CPUPPCState, cfar), "cfar");
697ab892
DG
164#endif
165
a7812ae4 166 cpu_xer = tcg_global_mem_new(TCG_AREG0,
1328c2bf 167 offsetof(CPUPPCState, xer), "xer");
da91a00f
RH
168 cpu_so = tcg_global_mem_new(TCG_AREG0,
169 offsetof(CPUPPCState, so), "SO");
170 cpu_ov = tcg_global_mem_new(TCG_AREG0,
171 offsetof(CPUPPCState, ov), "OV");
172 cpu_ca = tcg_global_mem_new(TCG_AREG0,
173 offsetof(CPUPPCState, ca), "CA");
3d7b417e 174
cf360a32 175 cpu_reserve = tcg_global_mem_new(TCG_AREG0,
1328c2bf 176 offsetof(CPUPPCState, reserve_addr),
18b21a2f 177 "reserve_addr");
cf360a32 178
30304420
DG
179 cpu_fpscr = tcg_global_mem_new(TCG_AREG0,
180 offsetof(CPUPPCState, fpscr), "fpscr");
e1571908 181
a7859e89 182 cpu_access_type = tcg_global_mem_new_i32(TCG_AREG0,
1328c2bf 183 offsetof(CPUPPCState, access_type), "access_type");
a7859e89 184
2e70f6ef
PB
185 done_init = 1;
186}
187
79aceca5
FB
188/* internal defines */
189typedef struct DisasContext {
190 struct TranslationBlock *tb;
0fa85d43 191 target_ulong nip;
79aceca5 192 uint32_t opcode;
9a64fbe4 193 uint32_t exception;
3cc62370
FB
194 /* Routine used to access memory */
195 int mem_idx;
76db3ba4 196 int access_type;
3cc62370 197 /* Translation flags */
76db3ba4 198 int le_mode;
d9bce9d9
JM
199#if defined(TARGET_PPC64)
200 int sf_mode;
697ab892 201 int has_cfar;
9a64fbe4 202#endif
3cc62370 203 int fpu_enabled;
a9d9eb8f 204 int altivec_enabled;
1f29871c 205 int vsx_enabled;
0487d6a8 206 int spe_enabled;
c227f099 207 ppc_spr_t *spr_cb; /* Needed to check rights for mfspr/mtspr */
ea4e754f 208 int singlestep_enabled;
7d08d856
AJ
209 uint64_t insns_flags;
210 uint64_t insns_flags2;
79aceca5
FB
211} DisasContext;
212
79482e5a
RH
213/* True when active word size < size of target_long. */
214#ifdef TARGET_PPC64
215# define NARROW_MODE(C) (!(C)->sf_mode)
216#else
217# define NARROW_MODE(C) 0
218#endif
219
c227f099 220struct opc_handler_t {
70560da7
FC
221 /* invalid bits for instruction 1 (Rc(opcode) == 0) */
222 uint32_t inval1;
223 /* invalid bits for instruction 2 (Rc(opcode) == 1) */
224 uint32_t inval2;
9a64fbe4 225 /* instruction type */
0487d6a8 226 uint64_t type;
a5858d7a
AG
227 /* extended instruction type */
228 uint64_t type2;
79aceca5
FB
229 /* handler */
230 void (*handler)(DisasContext *ctx);
a750fc0b 231#if defined(DO_PPC_STATISTICS) || defined(PPC_DUMP_CPU)
b55266b5 232 const char *oname;
a750fc0b
JM
233#endif
234#if defined(DO_PPC_STATISTICS)
76a66253
JM
235 uint64_t count;
236#endif
3fc6c082 237};
79aceca5 238
636aa200 239static inline void gen_reset_fpstatus(void)
7c58044c 240{
8e703949 241 gen_helper_reset_fpstatus(cpu_env);
7c58044c
JM
242}
243
636aa200 244static inline void gen_compute_fprf(TCGv_i64 arg, int set_fprf, int set_rc)
7c58044c 245{
0f2f39c2 246 TCGv_i32 t0 = tcg_temp_new_i32();
af12906f 247
7c58044c
JM
248 if (set_fprf != 0) {
249 /* This case might be optimized later */
0f2f39c2 250 tcg_gen_movi_i32(t0, 1);
8e703949 251 gen_helper_compute_fprf(t0, cpu_env, arg, t0);
a7812ae4 252 if (unlikely(set_rc)) {
0f2f39c2 253 tcg_gen_mov_i32(cpu_crf[1], t0);
a7812ae4 254 }
8e703949 255 gen_helper_float_check_status(cpu_env);
7c58044c
JM
256 } else if (unlikely(set_rc)) {
257 /* We always need to compute fpcc */
0f2f39c2 258 tcg_gen_movi_i32(t0, 0);
8e703949 259 gen_helper_compute_fprf(t0, cpu_env, arg, t0);
0f2f39c2 260 tcg_gen_mov_i32(cpu_crf[1], t0);
7c58044c 261 }
af12906f 262
0f2f39c2 263 tcg_temp_free_i32(t0);
7c58044c
JM
264}
265
636aa200 266static inline void gen_set_access_type(DisasContext *ctx, int access_type)
a7859e89 267{
76db3ba4
AJ
268 if (ctx->access_type != access_type) {
269 tcg_gen_movi_i32(cpu_access_type, access_type);
270 ctx->access_type = access_type;
271 }
a7859e89
AJ
272}
273
636aa200 274static inline void gen_update_nip(DisasContext *ctx, target_ulong nip)
d9bce9d9 275{
e0c8f9ce
RH
276 if (NARROW_MODE(ctx)) {
277 nip = (uint32_t)nip;
278 }
279 tcg_gen_movi_tl(cpu_nip, nip);
d9bce9d9
JM
280}
281
636aa200 282static inline void gen_exception_err(DisasContext *ctx, uint32_t excp, uint32_t error)
e06fcd75
AJ
283{
284 TCGv_i32 t0, t1;
285 if (ctx->exception == POWERPC_EXCP_NONE) {
286 gen_update_nip(ctx, ctx->nip);
287 }
288 t0 = tcg_const_i32(excp);
289 t1 = tcg_const_i32(error);
e5f17ac6 290 gen_helper_raise_exception_err(cpu_env, t0, t1);
e06fcd75
AJ
291 tcg_temp_free_i32(t0);
292 tcg_temp_free_i32(t1);
293 ctx->exception = (excp);
294}
e1833e1f 295
636aa200 296static inline void gen_exception(DisasContext *ctx, uint32_t excp)
e06fcd75
AJ
297{
298 TCGv_i32 t0;
299 if (ctx->exception == POWERPC_EXCP_NONE) {
300 gen_update_nip(ctx, ctx->nip);
301 }
302 t0 = tcg_const_i32(excp);
e5f17ac6 303 gen_helper_raise_exception(cpu_env, t0);
e06fcd75
AJ
304 tcg_temp_free_i32(t0);
305 ctx->exception = (excp);
306}
e1833e1f 307
636aa200 308static inline void gen_debug_exception(DisasContext *ctx)
e06fcd75
AJ
309{
310 TCGv_i32 t0;
5518f3a6 311
ee2b3994
SB
312 if ((ctx->exception != POWERPC_EXCP_BRANCH) &&
313 (ctx->exception != POWERPC_EXCP_SYNC)) {
5518f3a6 314 gen_update_nip(ctx, ctx->nip);
ee2b3994 315 }
e06fcd75 316 t0 = tcg_const_i32(EXCP_DEBUG);
e5f17ac6 317 gen_helper_raise_exception(cpu_env, t0);
e06fcd75
AJ
318 tcg_temp_free_i32(t0);
319}
9a64fbe4 320
636aa200 321static inline void gen_inval_exception(DisasContext *ctx, uint32_t error)
e06fcd75
AJ
322{
323 gen_exception_err(ctx, POWERPC_EXCP_PROGRAM, POWERPC_EXCP_INVAL | error);
324}
a9d9eb8f 325
f24e5695 326/* Stop translation */
636aa200 327static inline void gen_stop_exception(DisasContext *ctx)
3fc6c082 328{
d9bce9d9 329 gen_update_nip(ctx, ctx->nip);
e1833e1f 330 ctx->exception = POWERPC_EXCP_STOP;
3fc6c082
FB
331}
332
f24e5695 333/* No need to update nip here, as execution flow will change */
636aa200 334static inline void gen_sync_exception(DisasContext *ctx)
2be0071f 335{
e1833e1f 336 ctx->exception = POWERPC_EXCP_SYNC;
2be0071f
FB
337}
338
79aceca5 339#define GEN_HANDLER(name, opc1, opc2, opc3, inval, type) \
a5858d7a
AG
340GEN_OPCODE(name, opc1, opc2, opc3, inval, type, PPC_NONE)
341
342#define GEN_HANDLER_E(name, opc1, opc2, opc3, inval, type, type2) \
343GEN_OPCODE(name, opc1, opc2, opc3, inval, type, type2)
79aceca5 344
c7697e1f 345#define GEN_HANDLER2(name, onam, opc1, opc2, opc3, inval, type) \
a5858d7a
AG
346GEN_OPCODE2(name, onam, opc1, opc2, opc3, inval, type, PPC_NONE)
347
348#define GEN_HANDLER2_E(name, onam, opc1, opc2, opc3, inval, type, type2) \
349GEN_OPCODE2(name, onam, opc1, opc2, opc3, inval, type, type2)
c7697e1f 350
c227f099 351typedef struct opcode_t {
79aceca5 352 unsigned char opc1, opc2, opc3;
1235fc06 353#if HOST_LONG_BITS == 64 /* Explicitly align to 64 bits */
18fba28c
FB
354 unsigned char pad[5];
355#else
356 unsigned char pad[1];
357#endif
c227f099 358 opc_handler_t handler;
b55266b5 359 const char *oname;
c227f099 360} opcode_t;
79aceca5 361
a750fc0b 362/*****************************************************************************/
79aceca5
FB
363/*** Instruction decoding ***/
364#define EXTRACT_HELPER(name, shift, nb) \
636aa200 365static inline uint32_t name(uint32_t opcode) \
79aceca5
FB
366{ \
367 return (opcode >> (shift)) & ((1 << (nb)) - 1); \
368}
369
370#define EXTRACT_SHELPER(name, shift, nb) \
636aa200 371static inline int32_t name(uint32_t opcode) \
79aceca5 372{ \
18fba28c 373 return (int16_t)((opcode >> (shift)) & ((1 << (nb)) - 1)); \
79aceca5
FB
374}
375
f9fc6d81
TM
376#define EXTRACT_HELPER_SPLIT(name, shift1, nb1, shift2, nb2) \
377static inline uint32_t name(uint32_t opcode) \
378{ \
379 return (((opcode >> (shift1)) & ((1 << (nb1)) - 1)) << nb2) | \
380 ((opcode >> (shift2)) & ((1 << (nb2)) - 1)); \
381}
79aceca5
FB
382/* Opcode part 1 */
383EXTRACT_HELPER(opc1, 26, 6);
384/* Opcode part 2 */
385EXTRACT_HELPER(opc2, 1, 5);
386/* Opcode part 3 */
387EXTRACT_HELPER(opc3, 6, 5);
388/* Update Cr0 flags */
389EXTRACT_HELPER(Rc, 0, 1);
a737d3eb
TM
390/* Update Cr6 flags (Altivec) */
391EXTRACT_HELPER(Rc21, 10, 1);
79aceca5
FB
392/* Destination */
393EXTRACT_HELPER(rD, 21, 5);
394/* Source */
395EXTRACT_HELPER(rS, 21, 5);
396/* First operand */
397EXTRACT_HELPER(rA, 16, 5);
398/* Second operand */
399EXTRACT_HELPER(rB, 11, 5);
400/* Third operand */
401EXTRACT_HELPER(rC, 6, 5);
402/*** Get CRn ***/
403EXTRACT_HELPER(crfD, 23, 3);
404EXTRACT_HELPER(crfS, 18, 3);
405EXTRACT_HELPER(crbD, 21, 5);
406EXTRACT_HELPER(crbA, 16, 5);
407EXTRACT_HELPER(crbB, 11, 5);
408/* SPR / TBL */
3fc6c082 409EXTRACT_HELPER(_SPR, 11, 10);
636aa200 410static inline uint32_t SPR(uint32_t opcode)
3fc6c082
FB
411{
412 uint32_t sprn = _SPR(opcode);
413
414 return ((sprn >> 5) & 0x1F) | ((sprn & 0x1F) << 5);
415}
79aceca5
FB
416/*** Get constants ***/
417EXTRACT_HELPER(IMM, 12, 8);
418/* 16 bits signed immediate value */
419EXTRACT_SHELPER(SIMM, 0, 16);
420/* 16 bits unsigned immediate value */
421EXTRACT_HELPER(UIMM, 0, 16);
21d21583
AJ
422/* 5 bits signed immediate value */
423EXTRACT_HELPER(SIMM5, 16, 5);
27a4edb3
AJ
424/* 5 bits signed immediate value */
425EXTRACT_HELPER(UIMM5, 16, 5);
79aceca5
FB
426/* Bit count */
427EXTRACT_HELPER(NB, 11, 5);
428/* Shift count */
429EXTRACT_HELPER(SH, 11, 5);
cd633b10
AJ
430/* Vector shift count */
431EXTRACT_HELPER(VSH, 6, 4);
79aceca5
FB
432/* Mask start */
433EXTRACT_HELPER(MB, 6, 5);
434/* Mask end */
435EXTRACT_HELPER(ME, 1, 5);
fb0eaffc
FB
436/* Trap operand */
437EXTRACT_HELPER(TO, 21, 5);
79aceca5
FB
438
439EXTRACT_HELPER(CRM, 12, 8);
79aceca5 440EXTRACT_HELPER(SR, 16, 4);
7d08d856
AJ
441
442/* mtfsf/mtfsfi */
779f6590 443EXTRACT_HELPER(FPBF, 23, 3);
e4bb997e 444EXTRACT_HELPER(FPIMM, 12, 4);
779f6590 445EXTRACT_HELPER(FPL, 25, 1);
7d08d856
AJ
446EXTRACT_HELPER(FPFLM, 17, 8);
447EXTRACT_HELPER(FPW, 16, 1);
fb0eaffc 448
79aceca5
FB
449/*** Jump target decoding ***/
450/* Displacement */
451EXTRACT_SHELPER(d, 0, 16);
452/* Immediate address */
636aa200 453static inline target_ulong LI(uint32_t opcode)
79aceca5
FB
454{
455 return (opcode >> 0) & 0x03FFFFFC;
456}
457
636aa200 458static inline uint32_t BD(uint32_t opcode)
79aceca5
FB
459{
460 return (opcode >> 0) & 0xFFFC;
461}
462
463EXTRACT_HELPER(BO, 21, 5);
464EXTRACT_HELPER(BI, 16, 5);
465/* Absolute/relative address */
466EXTRACT_HELPER(AA, 1, 1);
467/* Link */
468EXTRACT_HELPER(LK, 0, 1);
469
470/* Create a mask between <start> and <end> bits */
636aa200 471static inline target_ulong MASK(uint32_t start, uint32_t end)
79aceca5 472{
76a66253 473 target_ulong ret;
79aceca5 474
76a66253
JM
475#if defined(TARGET_PPC64)
476 if (likely(start == 0)) {
6f2d8978 477 ret = UINT64_MAX << (63 - end);
76a66253 478 } else if (likely(end == 63)) {
6f2d8978 479 ret = UINT64_MAX >> start;
76a66253
JM
480 }
481#else
482 if (likely(start == 0)) {
6f2d8978 483 ret = UINT32_MAX << (31 - end);
76a66253 484 } else if (likely(end == 31)) {
6f2d8978 485 ret = UINT32_MAX >> start;
76a66253
JM
486 }
487#endif
488 else {
489 ret = (((target_ulong)(-1ULL)) >> (start)) ^
490 (((target_ulong)(-1ULL) >> (end)) >> 1);
491 if (unlikely(start > end))
492 return ~ret;
493 }
79aceca5
FB
494
495 return ret;
496}
497
f9fc6d81
TM
498EXTRACT_HELPER_SPLIT(xT, 0, 1, 21, 5);
499EXTRACT_HELPER_SPLIT(xS, 0, 1, 21, 5);
500EXTRACT_HELPER_SPLIT(xA, 2, 1, 16, 5);
501EXTRACT_HELPER_SPLIT(xB, 1, 1, 11, 5);
551e3ef7 502EXTRACT_HELPER_SPLIT(xC, 3, 1, 6, 5);
f9fc6d81 503EXTRACT_HELPER(DM, 8, 2);
76c15fe0 504EXTRACT_HELPER(UIM, 16, 2);
acc42968 505EXTRACT_HELPER(SHW, 8, 2);
a750fc0b 506/*****************************************************************************/
a750fc0b 507/* PowerPC instructions table */
933dc6eb 508
76a66253 509#if defined(DO_PPC_STATISTICS)
a5858d7a 510#define GEN_OPCODE(name, op1, op2, op3, invl, _typ, _typ2) \
5c55ff99 511{ \
79aceca5
FB
512 .opc1 = op1, \
513 .opc2 = op2, \
514 .opc3 = op3, \
18fba28c 515 .pad = { 0, }, \
79aceca5 516 .handler = { \
70560da7
FC
517 .inval1 = invl, \
518 .type = _typ, \
519 .type2 = _typ2, \
520 .handler = &gen_##name, \
521 .oname = stringify(name), \
522 }, \
523 .oname = stringify(name), \
524}
525#define GEN_OPCODE_DUAL(name, op1, op2, op3, invl1, invl2, _typ, _typ2) \
526{ \
527 .opc1 = op1, \
528 .opc2 = op2, \
529 .opc3 = op3, \
530 .pad = { 0, }, \
531 .handler = { \
532 .inval1 = invl1, \
533 .inval2 = invl2, \
9a64fbe4 534 .type = _typ, \
a5858d7a 535 .type2 = _typ2, \
79aceca5 536 .handler = &gen_##name, \
76a66253 537 .oname = stringify(name), \
79aceca5 538 }, \
3fc6c082 539 .oname = stringify(name), \
79aceca5 540}
a5858d7a 541#define GEN_OPCODE2(name, onam, op1, op2, op3, invl, _typ, _typ2) \
5c55ff99 542{ \
c7697e1f
JM
543 .opc1 = op1, \
544 .opc2 = op2, \
545 .opc3 = op3, \
546 .pad = { 0, }, \
547 .handler = { \
70560da7 548 .inval1 = invl, \
c7697e1f 549 .type = _typ, \
a5858d7a 550 .type2 = _typ2, \
c7697e1f
JM
551 .handler = &gen_##name, \
552 .oname = onam, \
553 }, \
554 .oname = onam, \
555}
76a66253 556#else
a5858d7a 557#define GEN_OPCODE(name, op1, op2, op3, invl, _typ, _typ2) \
5c55ff99 558{ \
c7697e1f
JM
559 .opc1 = op1, \
560 .opc2 = op2, \
561 .opc3 = op3, \
562 .pad = { 0, }, \
563 .handler = { \
70560da7
FC
564 .inval1 = invl, \
565 .type = _typ, \
566 .type2 = _typ2, \
567 .handler = &gen_##name, \
568 }, \
569 .oname = stringify(name), \
570}
571#define GEN_OPCODE_DUAL(name, op1, op2, op3, invl1, invl2, _typ, _typ2) \
572{ \
573 .opc1 = op1, \
574 .opc2 = op2, \
575 .opc3 = op3, \
576 .pad = { 0, }, \
577 .handler = { \
578 .inval1 = invl1, \
579 .inval2 = invl2, \
c7697e1f 580 .type = _typ, \
a5858d7a 581 .type2 = _typ2, \
c7697e1f 582 .handler = &gen_##name, \
5c55ff99
BS
583 }, \
584 .oname = stringify(name), \
585}
a5858d7a 586#define GEN_OPCODE2(name, onam, op1, op2, op3, invl, _typ, _typ2) \
5c55ff99
BS
587{ \
588 .opc1 = op1, \
589 .opc2 = op2, \
590 .opc3 = op3, \
591 .pad = { 0, }, \
592 .handler = { \
70560da7 593 .inval1 = invl, \
5c55ff99 594 .type = _typ, \
a5858d7a 595 .type2 = _typ2, \
5c55ff99
BS
596 .handler = &gen_##name, \
597 }, \
598 .oname = onam, \
599}
600#endif
2e610050 601
5c55ff99 602/* SPR load/store helpers */
636aa200 603static inline void gen_load_spr(TCGv t, int reg)
5c55ff99 604{
1328c2bf 605 tcg_gen_ld_tl(t, cpu_env, offsetof(CPUPPCState, spr[reg]));
5c55ff99 606}
2e610050 607
636aa200 608static inline void gen_store_spr(int reg, TCGv t)
5c55ff99 609{
1328c2bf 610 tcg_gen_st_tl(t, cpu_env, offsetof(CPUPPCState, spr[reg]));
5c55ff99 611}
2e610050 612
54623277 613/* Invalid instruction */
99e300ef 614static void gen_invalid(DisasContext *ctx)
9a64fbe4 615{
e06fcd75 616 gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL);
9a64fbe4
FB
617}
618
c227f099 619static opc_handler_t invalid_handler = {
70560da7
FC
620 .inval1 = 0xFFFFFFFF,
621 .inval2 = 0xFFFFFFFF,
9a64fbe4 622 .type = PPC_NONE,
a5858d7a 623 .type2 = PPC_NONE,
79aceca5
FB
624 .handler = gen_invalid,
625};
626
71a8c019
TM
627#if defined(TARGET_PPC64)
628/* NOTE: as this time, the only use of is_user_mode() is in 64 bit code. And */
629/* so the function is wrapped in the standard 64-bit ifdef in order to */
630/* avoid compiler warnings in 32-bit implementations. */
631static bool is_user_mode(DisasContext *ctx)
632{
633#if defined(CONFIG_USER_ONLY)
634 return true;
635#else
636 return ctx->mem_idx == 0;
637#endif
638}
639#endif
640
e1571908
AJ
641/*** Integer comparison ***/
642
636aa200 643static inline void gen_op_cmp(TCGv arg0, TCGv arg1, int s, int crf)
e1571908 644{
2fdcb629
RH
645 TCGv t0 = tcg_temp_new();
646 TCGv_i32 t1 = tcg_temp_new_i32();
e1571908 647
da91a00f 648 tcg_gen_trunc_tl_i32(cpu_crf[crf], cpu_so);
e1571908 649
2fdcb629
RH
650 tcg_gen_setcond_tl((s ? TCG_COND_LT: TCG_COND_LTU), t0, arg0, arg1);
651 tcg_gen_trunc_tl_i32(t1, t0);
652 tcg_gen_shli_i32(t1, t1, CRF_LT);
653 tcg_gen_or_i32(cpu_crf[crf], cpu_crf[crf], t1);
654
655 tcg_gen_setcond_tl((s ? TCG_COND_GT: TCG_COND_GTU), t0, arg0, arg1);
656 tcg_gen_trunc_tl_i32(t1, t0);
657 tcg_gen_shli_i32(t1, t1, CRF_GT);
658 tcg_gen_or_i32(cpu_crf[crf], cpu_crf[crf], t1);
659
660 tcg_gen_setcond_tl(TCG_COND_EQ, t0, arg0, arg1);
661 tcg_gen_trunc_tl_i32(t1, t0);
662 tcg_gen_shli_i32(t1, t1, CRF_EQ);
663 tcg_gen_or_i32(cpu_crf[crf], cpu_crf[crf], t1);
664
665 tcg_temp_free(t0);
666 tcg_temp_free_i32(t1);
e1571908
AJ
667}
668
636aa200 669static inline void gen_op_cmpi(TCGv arg0, target_ulong arg1, int s, int crf)
e1571908 670{
2fdcb629 671 TCGv t0 = tcg_const_tl(arg1);
ea363694
AJ
672 gen_op_cmp(arg0, t0, s, crf);
673 tcg_temp_free(t0);
e1571908
AJ
674}
675
636aa200 676static inline void gen_op_cmp32(TCGv arg0, TCGv arg1, int s, int crf)
e1571908 677{
ea363694 678 TCGv t0, t1;
2fdcb629
RH
679 t0 = tcg_temp_new();
680 t1 = tcg_temp_new();
e1571908 681 if (s) {
ea363694
AJ
682 tcg_gen_ext32s_tl(t0, arg0);
683 tcg_gen_ext32s_tl(t1, arg1);
e1571908 684 } else {
ea363694
AJ
685 tcg_gen_ext32u_tl(t0, arg0);
686 tcg_gen_ext32u_tl(t1, arg1);
e1571908 687 }
ea363694
AJ
688 gen_op_cmp(t0, t1, s, crf);
689 tcg_temp_free(t1);
690 tcg_temp_free(t0);
e1571908
AJ
691}
692
636aa200 693static inline void gen_op_cmpi32(TCGv arg0, target_ulong arg1, int s, int crf)
e1571908 694{
2fdcb629 695 TCGv t0 = tcg_const_tl(arg1);
ea363694
AJ
696 gen_op_cmp32(arg0, t0, s, crf);
697 tcg_temp_free(t0);
e1571908 698}
e1571908 699
636aa200 700static inline void gen_set_Rc0(DisasContext *ctx, TCGv reg)
e1571908 701{
02765534 702 if (NARROW_MODE(ctx)) {
e1571908 703 gen_op_cmpi32(reg, 0, 1, 0);
02765534 704 } else {
e1571908 705 gen_op_cmpi(reg, 0, 1, 0);
02765534 706 }
e1571908
AJ
707}
708
709/* cmp */
99e300ef 710static void gen_cmp(DisasContext *ctx)
e1571908 711{
36f48d9c 712 if ((ctx->opcode & 0x00200000) && (ctx->insns_flags & PPC_64B)) {
e1571908
AJ
713 gen_op_cmp(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rB(ctx->opcode)],
714 1, crfD(ctx->opcode));
36f48d9c
AG
715 } else {
716 gen_op_cmp32(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rB(ctx->opcode)],
717 1, crfD(ctx->opcode));
02765534 718 }
e1571908
AJ
719}
720
721/* cmpi */
99e300ef 722static void gen_cmpi(DisasContext *ctx)
e1571908 723{
36f48d9c 724 if ((ctx->opcode & 0x00200000) && (ctx->insns_flags & PPC_64B)) {
e1571908
AJ
725 gen_op_cmpi(cpu_gpr[rA(ctx->opcode)], SIMM(ctx->opcode),
726 1, crfD(ctx->opcode));
36f48d9c
AG
727 } else {
728 gen_op_cmpi32(cpu_gpr[rA(ctx->opcode)], SIMM(ctx->opcode),
729 1, crfD(ctx->opcode));
02765534 730 }
e1571908
AJ
731}
732
733/* cmpl */
99e300ef 734static void gen_cmpl(DisasContext *ctx)
e1571908 735{
36f48d9c 736 if ((ctx->opcode & 0x00200000) && (ctx->insns_flags & PPC_64B)) {
e1571908
AJ
737 gen_op_cmp(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rB(ctx->opcode)],
738 0, crfD(ctx->opcode));
36f48d9c
AG
739 } else {
740 gen_op_cmp32(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rB(ctx->opcode)],
741 0, crfD(ctx->opcode));
02765534 742 }
e1571908
AJ
743}
744
745/* cmpli */
99e300ef 746static void gen_cmpli(DisasContext *ctx)
e1571908 747{
36f48d9c 748 if ((ctx->opcode & 0x00200000) && (ctx->insns_flags & PPC_64B)) {
e1571908
AJ
749 gen_op_cmpi(cpu_gpr[rA(ctx->opcode)], UIMM(ctx->opcode),
750 0, crfD(ctx->opcode));
36f48d9c
AG
751 } else {
752 gen_op_cmpi32(cpu_gpr[rA(ctx->opcode)], UIMM(ctx->opcode),
753 0, crfD(ctx->opcode));
02765534 754 }
e1571908
AJ
755}
756
757/* isel (PowerPC 2.03 specification) */
99e300ef 758static void gen_isel(DisasContext *ctx)
e1571908
AJ
759{
760 int l1, l2;
761 uint32_t bi = rC(ctx->opcode);
762 uint32_t mask;
a7812ae4 763 TCGv_i32 t0;
e1571908
AJ
764
765 l1 = gen_new_label();
766 l2 = gen_new_label();
767
768 mask = 1 << (3 - (bi & 0x03));
a7812ae4 769 t0 = tcg_temp_new_i32();
fea0c503
AJ
770 tcg_gen_andi_i32(t0, cpu_crf[bi >> 2], mask);
771 tcg_gen_brcondi_i32(TCG_COND_EQ, t0, 0, l1);
e1571908
AJ
772 if (rA(ctx->opcode) == 0)
773 tcg_gen_movi_tl(cpu_gpr[rD(ctx->opcode)], 0);
774 else
775 tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)]);
776 tcg_gen_br(l2);
777 gen_set_label(l1);
778 tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rB(ctx->opcode)]);
779 gen_set_label(l2);
a7812ae4 780 tcg_temp_free_i32(t0);
e1571908
AJ
781}
782
fcfda20f
AJ
783/* cmpb: PowerPC 2.05 specification */
784static void gen_cmpb(DisasContext *ctx)
785{
786 gen_helper_cmpb(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)],
787 cpu_gpr[rB(ctx->opcode)]);
788}
789
79aceca5 790/*** Integer arithmetic ***/
79aceca5 791
636aa200
BS
792static inline void gen_op_arith_compute_ov(DisasContext *ctx, TCGv arg0,
793 TCGv arg1, TCGv arg2, int sub)
74637406 794{
ffe30937 795 TCGv t0 = tcg_temp_new();
79aceca5 796
8e7a6db9 797 tcg_gen_xor_tl(cpu_ov, arg0, arg2);
74637406 798 tcg_gen_xor_tl(t0, arg1, arg2);
ffe30937
RH
799 if (sub) {
800 tcg_gen_and_tl(cpu_ov, cpu_ov, t0);
801 } else {
802 tcg_gen_andc_tl(cpu_ov, cpu_ov, t0);
803 }
804 tcg_temp_free(t0);
02765534 805 if (NARROW_MODE(ctx)) {
ffe30937
RH
806 tcg_gen_ext32s_tl(cpu_ov, cpu_ov);
807 }
ffe30937
RH
808 tcg_gen_shri_tl(cpu_ov, cpu_ov, TARGET_LONG_BITS - 1);
809 tcg_gen_or_tl(cpu_so, cpu_so, cpu_ov);
79aceca5
FB
810}
811
74637406 812/* Common add function */
636aa200 813static inline void gen_op_arith_add(DisasContext *ctx, TCGv ret, TCGv arg1,
b5a73f8d
RH
814 TCGv arg2, bool add_ca, bool compute_ca,
815 bool compute_ov, bool compute_rc0)
74637406 816{
b5a73f8d 817 TCGv t0 = ret;
d9bce9d9 818
752d634e 819 if (compute_ca || compute_ov) {
146de60d 820 t0 = tcg_temp_new();
74637406 821 }
79aceca5 822
da91a00f 823 if (compute_ca) {
79482e5a 824 if (NARROW_MODE(ctx)) {
752d634e
RH
825 /* Caution: a non-obvious corner case of the spec is that we
826 must produce the *entire* 64-bit addition, but produce the
827 carry into bit 32. */
79482e5a 828 TCGv t1 = tcg_temp_new();
752d634e
RH
829 tcg_gen_xor_tl(t1, arg1, arg2); /* add without carry */
830 tcg_gen_add_tl(t0, arg1, arg2);
79482e5a
RH
831 if (add_ca) {
832 tcg_gen_add_tl(t0, t0, cpu_ca);
833 }
752d634e
RH
834 tcg_gen_xor_tl(cpu_ca, t0, t1); /* bits changed w/ carry */
835 tcg_temp_free(t1);
836 tcg_gen_shri_tl(cpu_ca, cpu_ca, 32); /* extract bit 32 */
837 tcg_gen_andi_tl(cpu_ca, cpu_ca, 1);
b5a73f8d 838 } else {
79482e5a
RH
839 TCGv zero = tcg_const_tl(0);
840 if (add_ca) {
841 tcg_gen_add2_tl(t0, cpu_ca, arg1, zero, cpu_ca, zero);
842 tcg_gen_add2_tl(t0, cpu_ca, t0, cpu_ca, arg2, zero);
843 } else {
844 tcg_gen_add2_tl(t0, cpu_ca, arg1, zero, arg2, zero);
845 }
846 tcg_temp_free(zero);
b5a73f8d 847 }
b5a73f8d
RH
848 } else {
849 tcg_gen_add_tl(t0, arg1, arg2);
850 if (add_ca) {
851 tcg_gen_add_tl(t0, t0, cpu_ca);
852 }
da91a00f 853 }
79aceca5 854
74637406
AJ
855 if (compute_ov) {
856 gen_op_arith_compute_ov(ctx, t0, arg1, arg2, 0);
857 }
b5a73f8d 858 if (unlikely(compute_rc0)) {
74637406 859 gen_set_Rc0(ctx, t0);
b5a73f8d 860 }
74637406 861
a7812ae4 862 if (!TCGV_EQUAL(t0, ret)) {
74637406
AJ
863 tcg_gen_mov_tl(ret, t0);
864 tcg_temp_free(t0);
865 }
39dd32ee 866}
74637406
AJ
867/* Add functions with two operands */
868#define GEN_INT_ARITH_ADD(name, opc3, add_ca, compute_ca, compute_ov) \
b5a73f8d 869static void glue(gen_, name)(DisasContext *ctx) \
74637406
AJ
870{ \
871 gen_op_arith_add(ctx, cpu_gpr[rD(ctx->opcode)], \
872 cpu_gpr[rA(ctx->opcode)], cpu_gpr[rB(ctx->opcode)], \
b5a73f8d 873 add_ca, compute_ca, compute_ov, Rc(ctx->opcode)); \
74637406
AJ
874}
875/* Add functions with one operand and one immediate */
876#define GEN_INT_ARITH_ADD_CONST(name, opc3, const_val, \
877 add_ca, compute_ca, compute_ov) \
b5a73f8d 878static void glue(gen_, name)(DisasContext *ctx) \
74637406 879{ \
b5a73f8d 880 TCGv t0 = tcg_const_tl(const_val); \
74637406
AJ
881 gen_op_arith_add(ctx, cpu_gpr[rD(ctx->opcode)], \
882 cpu_gpr[rA(ctx->opcode)], t0, \
b5a73f8d 883 add_ca, compute_ca, compute_ov, Rc(ctx->opcode)); \
74637406
AJ
884 tcg_temp_free(t0); \
885}
886
887/* add add. addo addo. */
888GEN_INT_ARITH_ADD(add, 0x08, 0, 0, 0)
889GEN_INT_ARITH_ADD(addo, 0x18, 0, 0, 1)
890/* addc addc. addco addco. */
891GEN_INT_ARITH_ADD(addc, 0x00, 0, 1, 0)
892GEN_INT_ARITH_ADD(addco, 0x10, 0, 1, 1)
893/* adde adde. addeo addeo. */
894GEN_INT_ARITH_ADD(adde, 0x04, 1, 1, 0)
895GEN_INT_ARITH_ADD(addeo, 0x14, 1, 1, 1)
896/* addme addme. addmeo addmeo. */
897GEN_INT_ARITH_ADD_CONST(addme, 0x07, -1LL, 1, 1, 0)
898GEN_INT_ARITH_ADD_CONST(addmeo, 0x17, -1LL, 1, 1, 1)
899/* addze addze. addzeo addzeo.*/
900GEN_INT_ARITH_ADD_CONST(addze, 0x06, 0, 1, 1, 0)
901GEN_INT_ARITH_ADD_CONST(addzeo, 0x16, 0, 1, 1, 1)
902/* addi */
99e300ef 903static void gen_addi(DisasContext *ctx)
d9bce9d9 904{
74637406
AJ
905 target_long simm = SIMM(ctx->opcode);
906
907 if (rA(ctx->opcode) == 0) {
908 /* li case */
909 tcg_gen_movi_tl(cpu_gpr[rD(ctx->opcode)], simm);
910 } else {
b5a73f8d
RH
911 tcg_gen_addi_tl(cpu_gpr[rD(ctx->opcode)],
912 cpu_gpr[rA(ctx->opcode)], simm);
74637406 913 }
d9bce9d9 914}
74637406 915/* addic addic.*/
b5a73f8d 916static inline void gen_op_addic(DisasContext *ctx, bool compute_rc0)
d9bce9d9 917{
b5a73f8d
RH
918 TCGv c = tcg_const_tl(SIMM(ctx->opcode));
919 gen_op_arith_add(ctx, cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)],
920 c, 0, 1, 0, compute_rc0);
921 tcg_temp_free(c);
d9bce9d9 922}
99e300ef
BS
923
924static void gen_addic(DisasContext *ctx)
d9bce9d9 925{
b5a73f8d 926 gen_op_addic(ctx, 0);
d9bce9d9 927}
e8eaa2c0
BS
928
929static void gen_addic_(DisasContext *ctx)
d9bce9d9 930{
b5a73f8d 931 gen_op_addic(ctx, 1);
d9bce9d9 932}
99e300ef 933
54623277 934/* addis */
99e300ef 935static void gen_addis(DisasContext *ctx)
d9bce9d9 936{
74637406
AJ
937 target_long simm = SIMM(ctx->opcode);
938
939 if (rA(ctx->opcode) == 0) {
940 /* lis case */
941 tcg_gen_movi_tl(cpu_gpr[rD(ctx->opcode)], simm << 16);
942 } else {
b5a73f8d
RH
943 tcg_gen_addi_tl(cpu_gpr[rD(ctx->opcode)],
944 cpu_gpr[rA(ctx->opcode)], simm << 16);
74637406 945 }
d9bce9d9 946}
74637406 947
636aa200
BS
948static inline void gen_op_arith_divw(DisasContext *ctx, TCGv ret, TCGv arg1,
949 TCGv arg2, int sign, int compute_ov)
d9bce9d9 950{
2ef1b120
AJ
951 int l1 = gen_new_label();
952 int l2 = gen_new_label();
a7812ae4
PB
953 TCGv_i32 t0 = tcg_temp_local_new_i32();
954 TCGv_i32 t1 = tcg_temp_local_new_i32();
74637406 955
2ef1b120
AJ
956 tcg_gen_trunc_tl_i32(t0, arg1);
957 tcg_gen_trunc_tl_i32(t1, arg2);
958 tcg_gen_brcondi_i32(TCG_COND_EQ, t1, 0, l1);
74637406 959 if (sign) {
2ef1b120
AJ
960 int l3 = gen_new_label();
961 tcg_gen_brcondi_i32(TCG_COND_NE, t1, -1, l3);
962 tcg_gen_brcondi_i32(TCG_COND_EQ, t0, INT32_MIN, l1);
74637406 963 gen_set_label(l3);
2ef1b120 964 tcg_gen_div_i32(t0, t0, t1);
74637406 965 } else {
2ef1b120 966 tcg_gen_divu_i32(t0, t0, t1);
74637406
AJ
967 }
968 if (compute_ov) {
da91a00f 969 tcg_gen_movi_tl(cpu_ov, 0);
74637406
AJ
970 }
971 tcg_gen_br(l2);
972 gen_set_label(l1);
973 if (sign) {
2ef1b120 974 tcg_gen_sari_i32(t0, t0, 31);
74637406
AJ
975 } else {
976 tcg_gen_movi_i32(t0, 0);
977 }
978 if (compute_ov) {
da91a00f
RH
979 tcg_gen_movi_tl(cpu_ov, 1);
980 tcg_gen_movi_tl(cpu_so, 1);
74637406
AJ
981 }
982 gen_set_label(l2);
2ef1b120 983 tcg_gen_extu_i32_tl(ret, t0);
a7812ae4
PB
984 tcg_temp_free_i32(t0);
985 tcg_temp_free_i32(t1);
74637406
AJ
986 if (unlikely(Rc(ctx->opcode) != 0))
987 gen_set_Rc0(ctx, ret);
d9bce9d9 988}
74637406
AJ
989/* Div functions */
990#define GEN_INT_ARITH_DIVW(name, opc3, sign, compute_ov) \
99e300ef 991static void glue(gen_, name)(DisasContext *ctx) \
74637406
AJ
992{ \
993 gen_op_arith_divw(ctx, cpu_gpr[rD(ctx->opcode)], \
994 cpu_gpr[rA(ctx->opcode)], cpu_gpr[rB(ctx->opcode)], \
995 sign, compute_ov); \
996}
997/* divwu divwu. divwuo divwuo. */
998GEN_INT_ARITH_DIVW(divwu, 0x0E, 0, 0);
999GEN_INT_ARITH_DIVW(divwuo, 0x1E, 0, 1);
1000/* divw divw. divwo divwo. */
1001GEN_INT_ARITH_DIVW(divw, 0x0F, 1, 0);
1002GEN_INT_ARITH_DIVW(divwo, 0x1F, 1, 1);
98d1eb27
TM
1003
1004/* div[wd]eu[o][.] */
1005#define GEN_DIVE(name, hlpr, compute_ov) \
1006static void gen_##name(DisasContext *ctx) \
1007{ \
1008 TCGv_i32 t0 = tcg_const_i32(compute_ov); \
1009 gen_helper_##hlpr(cpu_gpr[rD(ctx->opcode)], cpu_env, \
1010 cpu_gpr[rA(ctx->opcode)], cpu_gpr[rB(ctx->opcode)], t0); \
1011 tcg_temp_free_i32(t0); \
1012 if (unlikely(Rc(ctx->opcode) != 0)) { \
1013 gen_set_Rc0(ctx, cpu_gpr[rD(ctx->opcode)]); \
1014 } \
1015}
1016
6a4fda33
TM
1017GEN_DIVE(divweu, divweu, 0);
1018GEN_DIVE(divweuo, divweu, 1);
a98eb9e9
TM
1019GEN_DIVE(divwe, divwe, 0);
1020GEN_DIVE(divweo, divwe, 1);
6a4fda33 1021
d9bce9d9 1022#if defined(TARGET_PPC64)
636aa200
BS
1023static inline void gen_op_arith_divd(DisasContext *ctx, TCGv ret, TCGv arg1,
1024 TCGv arg2, int sign, int compute_ov)
d9bce9d9 1025{
2ef1b120
AJ
1026 int l1 = gen_new_label();
1027 int l2 = gen_new_label();
74637406
AJ
1028
1029 tcg_gen_brcondi_i64(TCG_COND_EQ, arg2, 0, l1);
1030 if (sign) {
2ef1b120 1031 int l3 = gen_new_label();
74637406
AJ
1032 tcg_gen_brcondi_i64(TCG_COND_NE, arg2, -1, l3);
1033 tcg_gen_brcondi_i64(TCG_COND_EQ, arg1, INT64_MIN, l1);
1034 gen_set_label(l3);
74637406
AJ
1035 tcg_gen_div_i64(ret, arg1, arg2);
1036 } else {
1037 tcg_gen_divu_i64(ret, arg1, arg2);
1038 }
1039 if (compute_ov) {
da91a00f 1040 tcg_gen_movi_tl(cpu_ov, 0);
74637406
AJ
1041 }
1042 tcg_gen_br(l2);
1043 gen_set_label(l1);
1044 if (sign) {
1045 tcg_gen_sari_i64(ret, arg1, 63);
1046 } else {
1047 tcg_gen_movi_i64(ret, 0);
1048 }
1049 if (compute_ov) {
da91a00f
RH
1050 tcg_gen_movi_tl(cpu_ov, 1);
1051 tcg_gen_movi_tl(cpu_so, 1);
74637406
AJ
1052 }
1053 gen_set_label(l2);
1054 if (unlikely(Rc(ctx->opcode) != 0))
1055 gen_set_Rc0(ctx, ret);
d9bce9d9 1056}
74637406 1057#define GEN_INT_ARITH_DIVD(name, opc3, sign, compute_ov) \
99e300ef 1058static void glue(gen_, name)(DisasContext *ctx) \
74637406 1059{ \
2ef1b120
AJ
1060 gen_op_arith_divd(ctx, cpu_gpr[rD(ctx->opcode)], \
1061 cpu_gpr[rA(ctx->opcode)], cpu_gpr[rB(ctx->opcode)], \
1062 sign, compute_ov); \
74637406
AJ
1063}
1064/* divwu divwu. divwuo divwuo. */
1065GEN_INT_ARITH_DIVD(divdu, 0x0E, 0, 0);
1066GEN_INT_ARITH_DIVD(divduo, 0x1E, 0, 1);
1067/* divw divw. divwo divwo. */
1068GEN_INT_ARITH_DIVD(divd, 0x0F, 1, 0);
1069GEN_INT_ARITH_DIVD(divdo, 0x1F, 1, 1);
98d1eb27
TM
1070
1071GEN_DIVE(divdeu, divdeu, 0);
1072GEN_DIVE(divdeuo, divdeu, 1);
e44259b6
TM
1073GEN_DIVE(divde, divde, 0);
1074GEN_DIVE(divdeo, divde, 1);
d9bce9d9 1075#endif
74637406
AJ
1076
1077/* mulhw mulhw. */
99e300ef 1078static void gen_mulhw(DisasContext *ctx)
d9bce9d9 1079{
23ad1d5d
RH
1080 TCGv_i32 t0 = tcg_temp_new_i32();
1081 TCGv_i32 t1 = tcg_temp_new_i32();
74637406 1082
23ad1d5d
RH
1083 tcg_gen_trunc_tl_i32(t0, cpu_gpr[rA(ctx->opcode)]);
1084 tcg_gen_trunc_tl_i32(t1, cpu_gpr[rB(ctx->opcode)]);
1085 tcg_gen_muls2_i32(t0, t1, t0, t1);
1086 tcg_gen_extu_i32_tl(cpu_gpr[rD(ctx->opcode)], t1);
1087 tcg_temp_free_i32(t0);
1088 tcg_temp_free_i32(t1);
74637406
AJ
1089 if (unlikely(Rc(ctx->opcode) != 0))
1090 gen_set_Rc0(ctx, cpu_gpr[rD(ctx->opcode)]);
d9bce9d9 1091}
99e300ef 1092
54623277 1093/* mulhwu mulhwu. */
99e300ef 1094static void gen_mulhwu(DisasContext *ctx)
d9bce9d9 1095{
23ad1d5d
RH
1096 TCGv_i32 t0 = tcg_temp_new_i32();
1097 TCGv_i32 t1 = tcg_temp_new_i32();
74637406 1098
23ad1d5d
RH
1099 tcg_gen_trunc_tl_i32(t0, cpu_gpr[rA(ctx->opcode)]);
1100 tcg_gen_trunc_tl_i32(t1, cpu_gpr[rB(ctx->opcode)]);
1101 tcg_gen_mulu2_i32(t0, t1, t0, t1);
1102 tcg_gen_extu_i32_tl(cpu_gpr[rD(ctx->opcode)], t1);
1103 tcg_temp_free_i32(t0);
1104 tcg_temp_free_i32(t1);
74637406
AJ
1105 if (unlikely(Rc(ctx->opcode) != 0))
1106 gen_set_Rc0(ctx, cpu_gpr[rD(ctx->opcode)]);
d9bce9d9 1107}
99e300ef 1108
54623277 1109/* mullw mullw. */
99e300ef 1110static void gen_mullw(DisasContext *ctx)
d9bce9d9 1111{
74637406
AJ
1112 tcg_gen_mul_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)],
1113 cpu_gpr[rB(ctx->opcode)]);
1e4c090f 1114 tcg_gen_ext32s_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rD(ctx->opcode)]);
74637406
AJ
1115 if (unlikely(Rc(ctx->opcode) != 0))
1116 gen_set_Rc0(ctx, cpu_gpr[rD(ctx->opcode)]);
d9bce9d9 1117}
99e300ef 1118
54623277 1119/* mullwo mullwo. */
99e300ef 1120static void gen_mullwo(DisasContext *ctx)
d9bce9d9 1121{
e4a2c846
RH
1122 TCGv_i32 t0 = tcg_temp_new_i32();
1123 TCGv_i32 t1 = tcg_temp_new_i32();
74637406 1124
e4a2c846
RH
1125 tcg_gen_trunc_tl_i32(t0, cpu_gpr[rA(ctx->opcode)]);
1126 tcg_gen_trunc_tl_i32(t1, cpu_gpr[rB(ctx->opcode)]);
1127 tcg_gen_muls2_i32(t0, t1, t0, t1);
1128 tcg_gen_ext_i32_tl(cpu_gpr[rD(ctx->opcode)], t0);
1129
1130 tcg_gen_sari_i32(t0, t0, 31);
1131 tcg_gen_setcond_i32(TCG_COND_NE, t0, t0, t1);
1132 tcg_gen_extu_i32_tl(cpu_ov, t0);
1133 tcg_gen_or_tl(cpu_so, cpu_so, cpu_ov);
1134
1135 tcg_temp_free_i32(t0);
1136 tcg_temp_free_i32(t1);
74637406
AJ
1137 if (unlikely(Rc(ctx->opcode) != 0))
1138 gen_set_Rc0(ctx, cpu_gpr[rD(ctx->opcode)]);
d9bce9d9 1139}
99e300ef 1140
54623277 1141/* mulli */
99e300ef 1142static void gen_mulli(DisasContext *ctx)
d9bce9d9 1143{
74637406
AJ
1144 tcg_gen_muli_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)],
1145 SIMM(ctx->opcode));
d9bce9d9 1146}
23ad1d5d 1147
d9bce9d9 1148#if defined(TARGET_PPC64)
74637406 1149/* mulhd mulhd. */
23ad1d5d
RH
1150static void gen_mulhd(DisasContext *ctx)
1151{
1152 TCGv lo = tcg_temp_new();
1153 tcg_gen_muls2_tl(lo, cpu_gpr[rD(ctx->opcode)],
1154 cpu_gpr[rA(ctx->opcode)], cpu_gpr[rB(ctx->opcode)]);
1155 tcg_temp_free(lo);
1156 if (unlikely(Rc(ctx->opcode) != 0)) {
1157 gen_set_Rc0(ctx, cpu_gpr[rD(ctx->opcode)]);
1158 }
1159}
1160
74637406 1161/* mulhdu mulhdu. */
23ad1d5d
RH
1162static void gen_mulhdu(DisasContext *ctx)
1163{
1164 TCGv lo = tcg_temp_new();
1165 tcg_gen_mulu2_tl(lo, cpu_gpr[rD(ctx->opcode)],
1166 cpu_gpr[rA(ctx->opcode)], cpu_gpr[rB(ctx->opcode)]);
1167 tcg_temp_free(lo);
1168 if (unlikely(Rc(ctx->opcode) != 0)) {
1169 gen_set_Rc0(ctx, cpu_gpr[rD(ctx->opcode)]);
1170 }
1171}
99e300ef 1172
54623277 1173/* mulld mulld. */
99e300ef 1174static void gen_mulld(DisasContext *ctx)
d9bce9d9 1175{
74637406
AJ
1176 tcg_gen_mul_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)],
1177 cpu_gpr[rB(ctx->opcode)]);
1178 if (unlikely(Rc(ctx->opcode) != 0))
1179 gen_set_Rc0(ctx, cpu_gpr[rD(ctx->opcode)]);
d9bce9d9 1180}
d15f74fb 1181
74637406 1182/* mulldo mulldo. */
d15f74fb
BS
1183static void gen_mulldo(DisasContext *ctx)
1184{
1185 gen_helper_mulldo(cpu_gpr[rD(ctx->opcode)], cpu_env,
1186 cpu_gpr[rA(ctx->opcode)], cpu_gpr[rB(ctx->opcode)]);
1187 if (unlikely(Rc(ctx->opcode) != 0)) {
1188 gen_set_Rc0(ctx, cpu_gpr[rD(ctx->opcode)]);
1189 }
1190}
d9bce9d9 1191#endif
74637406 1192
74637406 1193/* Common subf function */
636aa200 1194static inline void gen_op_arith_subf(DisasContext *ctx, TCGv ret, TCGv arg1,
b5a73f8d
RH
1195 TCGv arg2, bool add_ca, bool compute_ca,
1196 bool compute_ov, bool compute_rc0)
79aceca5 1197{
b5a73f8d 1198 TCGv t0 = ret;
79aceca5 1199
752d634e 1200 if (compute_ca || compute_ov) {
b5a73f8d 1201 t0 = tcg_temp_new();
da91a00f 1202 }
74637406 1203
79482e5a
RH
1204 if (compute_ca) {
1205 /* dest = ~arg1 + arg2 [+ ca]. */
1206 if (NARROW_MODE(ctx)) {
752d634e
RH
1207 /* Caution: a non-obvious corner case of the spec is that we
1208 must produce the *entire* 64-bit addition, but produce the
1209 carry into bit 32. */
79482e5a 1210 TCGv inv1 = tcg_temp_new();
752d634e 1211 TCGv t1 = tcg_temp_new();
79482e5a 1212 tcg_gen_not_tl(inv1, arg1);
79482e5a 1213 if (add_ca) {
752d634e 1214 tcg_gen_add_tl(t0, arg2, cpu_ca);
79482e5a 1215 } else {
752d634e 1216 tcg_gen_addi_tl(t0, arg2, 1);
79482e5a 1217 }
752d634e 1218 tcg_gen_xor_tl(t1, arg2, inv1); /* add without carry */
79482e5a 1219 tcg_gen_add_tl(t0, t0, inv1);
752d634e
RH
1220 tcg_gen_xor_tl(cpu_ca, t0, t1); /* bits changes w/ carry */
1221 tcg_temp_free(t1);
1222 tcg_gen_shri_tl(cpu_ca, cpu_ca, 32); /* extract bit 32 */
1223 tcg_gen_andi_tl(cpu_ca, cpu_ca, 1);
79482e5a 1224 } else if (add_ca) {
08f4a0f7
RH
1225 TCGv zero, inv1 = tcg_temp_new();
1226 tcg_gen_not_tl(inv1, arg1);
b5a73f8d
RH
1227 zero = tcg_const_tl(0);
1228 tcg_gen_add2_tl(t0, cpu_ca, arg2, zero, cpu_ca, zero);
08f4a0f7 1229 tcg_gen_add2_tl(t0, cpu_ca, t0, cpu_ca, inv1, zero);
b5a73f8d 1230 tcg_temp_free(zero);
08f4a0f7 1231 tcg_temp_free(inv1);
b5a73f8d 1232 } else {
79482e5a 1233 tcg_gen_setcond_tl(TCG_COND_GEU, cpu_ca, arg2, arg1);
b5a73f8d 1234 tcg_gen_sub_tl(t0, arg2, arg1);
b5a73f8d 1235 }
79482e5a
RH
1236 } else if (add_ca) {
1237 /* Since we're ignoring carry-out, we can simplify the
1238 standard ~arg1 + arg2 + ca to arg2 - arg1 + ca - 1. */
1239 tcg_gen_sub_tl(t0, arg2, arg1);
1240 tcg_gen_add_tl(t0, t0, cpu_ca);
1241 tcg_gen_subi_tl(t0, t0, 1);
79aceca5 1242 } else {
b5a73f8d 1243 tcg_gen_sub_tl(t0, arg2, arg1);
74637406 1244 }
b5a73f8d 1245
74637406
AJ
1246 if (compute_ov) {
1247 gen_op_arith_compute_ov(ctx, t0, arg1, arg2, 1);
1248 }
b5a73f8d 1249 if (unlikely(compute_rc0)) {
74637406 1250 gen_set_Rc0(ctx, t0);
b5a73f8d 1251 }
74637406 1252
a7812ae4 1253 if (!TCGV_EQUAL(t0, ret)) {
74637406
AJ
1254 tcg_gen_mov_tl(ret, t0);
1255 tcg_temp_free(t0);
79aceca5 1256 }
79aceca5 1257}
74637406
AJ
1258/* Sub functions with Two operands functions */
1259#define GEN_INT_ARITH_SUBF(name, opc3, add_ca, compute_ca, compute_ov) \
b5a73f8d 1260static void glue(gen_, name)(DisasContext *ctx) \
74637406
AJ
1261{ \
1262 gen_op_arith_subf(ctx, cpu_gpr[rD(ctx->opcode)], \
1263 cpu_gpr[rA(ctx->opcode)], cpu_gpr[rB(ctx->opcode)], \
b5a73f8d 1264 add_ca, compute_ca, compute_ov, Rc(ctx->opcode)); \
74637406
AJ
1265}
1266/* Sub functions with one operand and one immediate */
1267#define GEN_INT_ARITH_SUBF_CONST(name, opc3, const_val, \
1268 add_ca, compute_ca, compute_ov) \
b5a73f8d 1269static void glue(gen_, name)(DisasContext *ctx) \
74637406 1270{ \
b5a73f8d 1271 TCGv t0 = tcg_const_tl(const_val); \
74637406
AJ
1272 gen_op_arith_subf(ctx, cpu_gpr[rD(ctx->opcode)], \
1273 cpu_gpr[rA(ctx->opcode)], t0, \
b5a73f8d 1274 add_ca, compute_ca, compute_ov, Rc(ctx->opcode)); \
74637406
AJ
1275 tcg_temp_free(t0); \
1276}
1277/* subf subf. subfo subfo. */
1278GEN_INT_ARITH_SUBF(subf, 0x01, 0, 0, 0)
1279GEN_INT_ARITH_SUBF(subfo, 0x11, 0, 0, 1)
1280/* subfc subfc. subfco subfco. */
1281GEN_INT_ARITH_SUBF(subfc, 0x00, 0, 1, 0)
1282GEN_INT_ARITH_SUBF(subfco, 0x10, 0, 1, 1)
1283/* subfe subfe. subfeo subfo. */
1284GEN_INT_ARITH_SUBF(subfe, 0x04, 1, 1, 0)
1285GEN_INT_ARITH_SUBF(subfeo, 0x14, 1, 1, 1)
1286/* subfme subfme. subfmeo subfmeo. */
1287GEN_INT_ARITH_SUBF_CONST(subfme, 0x07, -1LL, 1, 1, 0)
1288GEN_INT_ARITH_SUBF_CONST(subfmeo, 0x17, -1LL, 1, 1, 1)
1289/* subfze subfze. subfzeo subfzeo.*/
1290GEN_INT_ARITH_SUBF_CONST(subfze, 0x06, 0, 1, 1, 0)
1291GEN_INT_ARITH_SUBF_CONST(subfzeo, 0x16, 0, 1, 1, 1)
99e300ef 1292
54623277 1293/* subfic */
99e300ef 1294static void gen_subfic(DisasContext *ctx)
79aceca5 1295{
b5a73f8d
RH
1296 TCGv c = tcg_const_tl(SIMM(ctx->opcode));
1297 gen_op_arith_subf(ctx, cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)],
1298 c, 0, 1, 0, 0);
1299 tcg_temp_free(c);
79aceca5
FB
1300}
1301
fd3f0081
RH
1302/* neg neg. nego nego. */
1303static inline void gen_op_arith_neg(DisasContext *ctx, bool compute_ov)
1304{
1305 TCGv zero = tcg_const_tl(0);
1306 gen_op_arith_subf(ctx, cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)],
1307 zero, 0, 0, compute_ov, Rc(ctx->opcode));
1308 tcg_temp_free(zero);
1309}
1310
1311static void gen_neg(DisasContext *ctx)
1312{
1313 gen_op_arith_neg(ctx, 0);
1314}
1315
1316static void gen_nego(DisasContext *ctx)
1317{
1318 gen_op_arith_neg(ctx, 1);
1319}
1320
79aceca5 1321/*** Integer logical ***/
26d67362 1322#define GEN_LOGICAL2(name, tcg_op, opc, type) \
99e300ef 1323static void glue(gen_, name)(DisasContext *ctx) \
79aceca5 1324{ \
26d67362
AJ
1325 tcg_op(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)], \
1326 cpu_gpr[rB(ctx->opcode)]); \
76a66253 1327 if (unlikely(Rc(ctx->opcode) != 0)) \
26d67362 1328 gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]); \
79aceca5 1329}
79aceca5 1330
26d67362 1331#define GEN_LOGICAL1(name, tcg_op, opc, type) \
99e300ef 1332static void glue(gen_, name)(DisasContext *ctx) \
79aceca5 1333{ \
26d67362 1334 tcg_op(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)]); \
76a66253 1335 if (unlikely(Rc(ctx->opcode) != 0)) \
26d67362 1336 gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]); \
79aceca5
FB
1337}
1338
1339/* and & and. */
26d67362 1340GEN_LOGICAL2(and, tcg_gen_and_tl, 0x00, PPC_INTEGER);
79aceca5 1341/* andc & andc. */
26d67362 1342GEN_LOGICAL2(andc, tcg_gen_andc_tl, 0x01, PPC_INTEGER);
e8eaa2c0 1343
54623277 1344/* andi. */
e8eaa2c0 1345static void gen_andi_(DisasContext *ctx)
79aceca5 1346{
26d67362
AJ
1347 tcg_gen_andi_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)], UIMM(ctx->opcode));
1348 gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
79aceca5 1349}
e8eaa2c0 1350
54623277 1351/* andis. */
e8eaa2c0 1352static void gen_andis_(DisasContext *ctx)
79aceca5 1353{
26d67362
AJ
1354 tcg_gen_andi_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)], UIMM(ctx->opcode) << 16);
1355 gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
79aceca5 1356}
99e300ef 1357
54623277 1358/* cntlzw */
99e300ef 1359static void gen_cntlzw(DisasContext *ctx)
26d67362 1360{
a7812ae4 1361 gen_helper_cntlzw(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)]);
26d67362 1362 if (unlikely(Rc(ctx->opcode) != 0))
2e31f5d3 1363 gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
26d67362 1364}
79aceca5 1365/* eqv & eqv. */
26d67362 1366GEN_LOGICAL2(eqv, tcg_gen_eqv_tl, 0x08, PPC_INTEGER);
79aceca5 1367/* extsb & extsb. */
26d67362 1368GEN_LOGICAL1(extsb, tcg_gen_ext8s_tl, 0x1D, PPC_INTEGER);
79aceca5 1369/* extsh & extsh. */
26d67362 1370GEN_LOGICAL1(extsh, tcg_gen_ext16s_tl, 0x1C, PPC_INTEGER);
79aceca5 1371/* nand & nand. */
26d67362 1372GEN_LOGICAL2(nand, tcg_gen_nand_tl, 0x0E, PPC_INTEGER);
79aceca5 1373/* nor & nor. */
26d67362 1374GEN_LOGICAL2(nor, tcg_gen_nor_tl, 0x03, PPC_INTEGER);
99e300ef 1375
54623277 1376/* or & or. */
99e300ef 1377static void gen_or(DisasContext *ctx)
9a64fbe4 1378{
76a66253
JM
1379 int rs, ra, rb;
1380
1381 rs = rS(ctx->opcode);
1382 ra = rA(ctx->opcode);
1383 rb = rB(ctx->opcode);
1384 /* Optimisation for mr. ri case */
1385 if (rs != ra || rs != rb) {
26d67362
AJ
1386 if (rs != rb)
1387 tcg_gen_or_tl(cpu_gpr[ra], cpu_gpr[rs], cpu_gpr[rb]);
1388 else
1389 tcg_gen_mov_tl(cpu_gpr[ra], cpu_gpr[rs]);
76a66253 1390 if (unlikely(Rc(ctx->opcode) != 0))
26d67362 1391 gen_set_Rc0(ctx, cpu_gpr[ra]);
76a66253 1392 } else if (unlikely(Rc(ctx->opcode) != 0)) {
26d67362 1393 gen_set_Rc0(ctx, cpu_gpr[rs]);
c80f84e3
JM
1394#if defined(TARGET_PPC64)
1395 } else {
26d67362
AJ
1396 int prio = 0;
1397
c80f84e3
JM
1398 switch (rs) {
1399 case 1:
1400 /* Set process priority to low */
26d67362 1401 prio = 2;
c80f84e3
JM
1402 break;
1403 case 6:
1404 /* Set process priority to medium-low */
26d67362 1405 prio = 3;
c80f84e3
JM
1406 break;
1407 case 2:
1408 /* Set process priority to normal */
26d67362 1409 prio = 4;
c80f84e3 1410 break;
be147d08
JM
1411#if !defined(CONFIG_USER_ONLY)
1412 case 31:
76db3ba4 1413 if (ctx->mem_idx > 0) {
be147d08 1414 /* Set process priority to very low */
26d67362 1415 prio = 1;
be147d08
JM
1416 }
1417 break;
1418 case 5:
76db3ba4 1419 if (ctx->mem_idx > 0) {
be147d08 1420 /* Set process priority to medium-hight */
26d67362 1421 prio = 5;
be147d08
JM
1422 }
1423 break;
1424 case 3:
76db3ba4 1425 if (ctx->mem_idx > 0) {
be147d08 1426 /* Set process priority to high */
26d67362 1427 prio = 6;
be147d08
JM
1428 }
1429 break;
be147d08 1430 case 7:
76db3ba4 1431 if (ctx->mem_idx > 1) {
be147d08 1432 /* Set process priority to very high */
26d67362 1433 prio = 7;
be147d08
JM
1434 }
1435 break;
be147d08 1436#endif
c80f84e3
JM
1437 default:
1438 /* nop */
1439 break;
1440 }
26d67362 1441 if (prio) {
a7812ae4 1442 TCGv t0 = tcg_temp_new();
54cdcae6 1443 gen_load_spr(t0, SPR_PPR);
ea363694
AJ
1444 tcg_gen_andi_tl(t0, t0, ~0x001C000000000000ULL);
1445 tcg_gen_ori_tl(t0, t0, ((uint64_t)prio) << 50);
54cdcae6 1446 gen_store_spr(SPR_PPR, t0);
ea363694 1447 tcg_temp_free(t0);
26d67362 1448 }
c80f84e3 1449#endif
9a64fbe4 1450 }
9a64fbe4 1451}
79aceca5 1452/* orc & orc. */
26d67362 1453GEN_LOGICAL2(orc, tcg_gen_orc_tl, 0x0C, PPC_INTEGER);
99e300ef 1454
54623277 1455/* xor & xor. */
99e300ef 1456static void gen_xor(DisasContext *ctx)
9a64fbe4 1457{
9a64fbe4 1458 /* Optimisation for "set to zero" case */
26d67362 1459 if (rS(ctx->opcode) != rB(ctx->opcode))
312179c4 1460 tcg_gen_xor_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)], cpu_gpr[rB(ctx->opcode)]);
26d67362
AJ
1461 else
1462 tcg_gen_movi_tl(cpu_gpr[rA(ctx->opcode)], 0);
76a66253 1463 if (unlikely(Rc(ctx->opcode) != 0))
26d67362 1464 gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
9a64fbe4 1465}
99e300ef 1466
54623277 1467/* ori */
99e300ef 1468static void gen_ori(DisasContext *ctx)
79aceca5 1469{
76a66253 1470 target_ulong uimm = UIMM(ctx->opcode);
79aceca5 1471
9a64fbe4
FB
1472 if (rS(ctx->opcode) == rA(ctx->opcode) && uimm == 0) {
1473 /* NOP */
76a66253 1474 /* XXX: should handle special NOPs for POWER series */
9a64fbe4 1475 return;
76a66253 1476 }
26d67362 1477 tcg_gen_ori_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)], uimm);
79aceca5 1478}
99e300ef 1479
54623277 1480/* oris */
99e300ef 1481static void gen_oris(DisasContext *ctx)
79aceca5 1482{
76a66253 1483 target_ulong uimm = UIMM(ctx->opcode);
79aceca5 1484
9a64fbe4
FB
1485 if (rS(ctx->opcode) == rA(ctx->opcode) && uimm == 0) {
1486 /* NOP */
1487 return;
76a66253 1488 }
26d67362 1489 tcg_gen_ori_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)], uimm << 16);
79aceca5 1490}
99e300ef 1491
54623277 1492/* xori */
99e300ef 1493static void gen_xori(DisasContext *ctx)
79aceca5 1494{
76a66253 1495 target_ulong uimm = UIMM(ctx->opcode);
9a64fbe4
FB
1496
1497 if (rS(ctx->opcode) == rA(ctx->opcode) && uimm == 0) {
1498 /* NOP */
1499 return;
1500 }
26d67362 1501 tcg_gen_xori_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)], uimm);
79aceca5 1502}
99e300ef 1503
54623277 1504/* xoris */
99e300ef 1505static void gen_xoris(DisasContext *ctx)
79aceca5 1506{
76a66253 1507 target_ulong uimm = UIMM(ctx->opcode);
9a64fbe4
FB
1508
1509 if (rS(ctx->opcode) == rA(ctx->opcode) && uimm == 0) {
1510 /* NOP */
1511 return;
1512 }
26d67362 1513 tcg_gen_xori_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)], uimm << 16);
79aceca5 1514}
99e300ef 1515
54623277 1516/* popcntb : PowerPC 2.03 specification */
99e300ef 1517static void gen_popcntb(DisasContext *ctx)
d9bce9d9 1518{
eaabeef2
DG
1519 gen_helper_popcntb(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)]);
1520}
1521
1522static void gen_popcntw(DisasContext *ctx)
1523{
1524 gen_helper_popcntw(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)]);
1525}
1526
d9bce9d9 1527#if defined(TARGET_PPC64)
eaabeef2
DG
1528/* popcntd: PowerPC 2.06 specification */
1529static void gen_popcntd(DisasContext *ctx)
1530{
1531 gen_helper_popcntd(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)]);
d9bce9d9 1532}
eaabeef2 1533#endif
d9bce9d9 1534
725bcec2
AJ
1535/* prtyw: PowerPC 2.05 specification */
1536static void gen_prtyw(DisasContext *ctx)
1537{
1538 TCGv ra = cpu_gpr[rA(ctx->opcode)];
1539 TCGv rs = cpu_gpr[rS(ctx->opcode)];
1540 TCGv t0 = tcg_temp_new();
1541 tcg_gen_shri_tl(t0, rs, 16);
1542 tcg_gen_xor_tl(ra, rs, t0);
1543 tcg_gen_shri_tl(t0, ra, 8);
1544 tcg_gen_xor_tl(ra, ra, t0);
1545 tcg_gen_andi_tl(ra, ra, (target_ulong)0x100000001ULL);
1546 tcg_temp_free(t0);
1547}
1548
1549#if defined(TARGET_PPC64)
1550/* prtyd: PowerPC 2.05 specification */
1551static void gen_prtyd(DisasContext *ctx)
1552{
1553 TCGv ra = cpu_gpr[rA(ctx->opcode)];
1554 TCGv rs = cpu_gpr[rS(ctx->opcode)];
1555 TCGv t0 = tcg_temp_new();
1556 tcg_gen_shri_tl(t0, rs, 32);
1557 tcg_gen_xor_tl(ra, rs, t0);
1558 tcg_gen_shri_tl(t0, ra, 16);
1559 tcg_gen_xor_tl(ra, ra, t0);
1560 tcg_gen_shri_tl(t0, ra, 8);
1561 tcg_gen_xor_tl(ra, ra, t0);
1562 tcg_gen_andi_tl(ra, ra, 1);
1563 tcg_temp_free(t0);
1564}
1565#endif
1566
86ba37ed
TM
1567#if defined(TARGET_PPC64)
1568/* bpermd */
1569static void gen_bpermd(DisasContext *ctx)
1570{
1571 gen_helper_bpermd(cpu_gpr[rA(ctx->opcode)],
1572 cpu_gpr[rS(ctx->opcode)], cpu_gpr[rB(ctx->opcode)]);
1573}
1574#endif
1575
d9bce9d9
JM
1576#if defined(TARGET_PPC64)
1577/* extsw & extsw. */
26d67362 1578GEN_LOGICAL1(extsw, tcg_gen_ext32s_tl, 0x1E, PPC_64B);
99e300ef 1579
54623277 1580/* cntlzd */
99e300ef 1581static void gen_cntlzd(DisasContext *ctx)
26d67362 1582{
a7812ae4 1583 gen_helper_cntlzd(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)]);
26d67362
AJ
1584 if (unlikely(Rc(ctx->opcode) != 0))
1585 gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
1586}
d9bce9d9
JM
1587#endif
1588
79aceca5 1589/*** Integer rotate ***/
99e300ef 1590
54623277 1591/* rlwimi & rlwimi. */
99e300ef 1592static void gen_rlwimi(DisasContext *ctx)
79aceca5 1593{
76a66253 1594 uint32_t mb, me, sh;
79aceca5
FB
1595
1596 mb = MB(ctx->opcode);
1597 me = ME(ctx->opcode);
76a66253 1598 sh = SH(ctx->opcode);
d03ef511
AJ
1599 if (likely(sh == 0 && mb == 0 && me == 31)) {
1600 tcg_gen_ext32u_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)]);
1601 } else {
d03ef511 1602 target_ulong mask;
a7812ae4
PB
1603 TCGv t1;
1604 TCGv t0 = tcg_temp_new();
54843a58 1605#if defined(TARGET_PPC64)
a7812ae4
PB
1606 TCGv_i32 t2 = tcg_temp_new_i32();
1607 tcg_gen_trunc_i64_i32(t2, cpu_gpr[rS(ctx->opcode)]);
1608 tcg_gen_rotli_i32(t2, t2, sh);
1609 tcg_gen_extu_i32_i64(t0, t2);
1610 tcg_temp_free_i32(t2);
54843a58
AJ
1611#else
1612 tcg_gen_rotli_i32(t0, cpu_gpr[rS(ctx->opcode)], sh);
1613#endif
76a66253 1614#if defined(TARGET_PPC64)
d03ef511
AJ
1615 mb += 32;
1616 me += 32;
76a66253 1617#endif
d03ef511 1618 mask = MASK(mb, me);
a7812ae4 1619 t1 = tcg_temp_new();
d03ef511
AJ
1620 tcg_gen_andi_tl(t0, t0, mask);
1621 tcg_gen_andi_tl(t1, cpu_gpr[rA(ctx->opcode)], ~mask);
1622 tcg_gen_or_tl(cpu_gpr[rA(ctx->opcode)], t0, t1);
1623 tcg_temp_free(t0);
1624 tcg_temp_free(t1);
1625 }
76a66253 1626 if (unlikely(Rc(ctx->opcode) != 0))
d03ef511 1627 gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
79aceca5 1628}
99e300ef 1629
54623277 1630/* rlwinm & rlwinm. */
99e300ef 1631static void gen_rlwinm(DisasContext *ctx)
79aceca5
FB
1632{
1633 uint32_t mb, me, sh;
3b46e624 1634
79aceca5
FB
1635 sh = SH(ctx->opcode);
1636 mb = MB(ctx->opcode);
1637 me = ME(ctx->opcode);
d03ef511
AJ
1638
1639 if (likely(mb == 0 && me == (31 - sh))) {
1640 if (likely(sh == 0)) {
1641 tcg_gen_ext32u_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)]);
1642 } else {
a7812ae4 1643 TCGv t0 = tcg_temp_new();
d03ef511
AJ
1644 tcg_gen_ext32u_tl(t0, cpu_gpr[rS(ctx->opcode)]);
1645 tcg_gen_shli_tl(t0, t0, sh);
1646 tcg_gen_ext32u_tl(cpu_gpr[rA(ctx->opcode)], t0);
1647 tcg_temp_free(t0);
79aceca5 1648 }
d03ef511 1649 } else if (likely(sh != 0 && me == 31 && sh == (32 - mb))) {
a7812ae4 1650 TCGv t0 = tcg_temp_new();
d03ef511
AJ
1651 tcg_gen_ext32u_tl(t0, cpu_gpr[rS(ctx->opcode)]);
1652 tcg_gen_shri_tl(t0, t0, mb);
1653 tcg_gen_ext32u_tl(cpu_gpr[rA(ctx->opcode)], t0);
1654 tcg_temp_free(t0);
1655 } else {
a7812ae4 1656 TCGv t0 = tcg_temp_new();
54843a58 1657#if defined(TARGET_PPC64)
a7812ae4 1658 TCGv_i32 t1 = tcg_temp_new_i32();
54843a58
AJ
1659 tcg_gen_trunc_i64_i32(t1, cpu_gpr[rS(ctx->opcode)]);
1660 tcg_gen_rotli_i32(t1, t1, sh);
1661 tcg_gen_extu_i32_i64(t0, t1);
a7812ae4 1662 tcg_temp_free_i32(t1);
54843a58
AJ
1663#else
1664 tcg_gen_rotli_i32(t0, cpu_gpr[rS(ctx->opcode)], sh);
1665#endif
76a66253 1666#if defined(TARGET_PPC64)
d03ef511
AJ
1667 mb += 32;
1668 me += 32;
76a66253 1669#endif
d03ef511
AJ
1670 tcg_gen_andi_tl(cpu_gpr[rA(ctx->opcode)], t0, MASK(mb, me));
1671 tcg_temp_free(t0);
1672 }
76a66253 1673 if (unlikely(Rc(ctx->opcode) != 0))
d03ef511 1674 gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
79aceca5 1675}
99e300ef 1676
54623277 1677/* rlwnm & rlwnm. */
99e300ef 1678static void gen_rlwnm(DisasContext *ctx)
79aceca5
FB
1679{
1680 uint32_t mb, me;
54843a58
AJ
1681 TCGv t0;
1682#if defined(TARGET_PPC64)
a7812ae4 1683 TCGv_i32 t1, t2;
54843a58 1684#endif
79aceca5
FB
1685
1686 mb = MB(ctx->opcode);
1687 me = ME(ctx->opcode);
a7812ae4 1688 t0 = tcg_temp_new();
d03ef511 1689 tcg_gen_andi_tl(t0, cpu_gpr[rB(ctx->opcode)], 0x1f);
54843a58 1690#if defined(TARGET_PPC64)
a7812ae4
PB
1691 t1 = tcg_temp_new_i32();
1692 t2 = tcg_temp_new_i32();
54843a58
AJ
1693 tcg_gen_trunc_i64_i32(t1, cpu_gpr[rS(ctx->opcode)]);
1694 tcg_gen_trunc_i64_i32(t2, t0);
1695 tcg_gen_rotl_i32(t1, t1, t2);
1696 tcg_gen_extu_i32_i64(t0, t1);
a7812ae4
PB
1697 tcg_temp_free_i32(t1);
1698 tcg_temp_free_i32(t2);
54843a58
AJ
1699#else
1700 tcg_gen_rotl_i32(t0, cpu_gpr[rS(ctx->opcode)], t0);
1701#endif
76a66253
JM
1702 if (unlikely(mb != 0 || me != 31)) {
1703#if defined(TARGET_PPC64)
1704 mb += 32;
1705 me += 32;
1706#endif
54843a58 1707 tcg_gen_andi_tl(cpu_gpr[rA(ctx->opcode)], t0, MASK(mb, me));
d03ef511 1708 } else {
54843a58 1709 tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], t0);
79aceca5 1710 }
54843a58 1711 tcg_temp_free(t0);
76a66253 1712 if (unlikely(Rc(ctx->opcode) != 0))
d03ef511 1713 gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
79aceca5
FB
1714}
1715
d9bce9d9
JM
1716#if defined(TARGET_PPC64)
1717#define GEN_PPC64_R2(name, opc1, opc2) \
e8eaa2c0 1718static void glue(gen_, name##0)(DisasContext *ctx) \
d9bce9d9
JM
1719{ \
1720 gen_##name(ctx, 0); \
1721} \
e8eaa2c0
BS
1722 \
1723static void glue(gen_, name##1)(DisasContext *ctx) \
d9bce9d9
JM
1724{ \
1725 gen_##name(ctx, 1); \
1726}
1727#define GEN_PPC64_R4(name, opc1, opc2) \
e8eaa2c0 1728static void glue(gen_, name##0)(DisasContext *ctx) \
d9bce9d9
JM
1729{ \
1730 gen_##name(ctx, 0, 0); \
1731} \
e8eaa2c0
BS
1732 \
1733static void glue(gen_, name##1)(DisasContext *ctx) \
d9bce9d9
JM
1734{ \
1735 gen_##name(ctx, 0, 1); \
1736} \
e8eaa2c0
BS
1737 \
1738static void glue(gen_, name##2)(DisasContext *ctx) \
d9bce9d9
JM
1739{ \
1740 gen_##name(ctx, 1, 0); \
1741} \
e8eaa2c0
BS
1742 \
1743static void glue(gen_, name##3)(DisasContext *ctx) \
d9bce9d9
JM
1744{ \
1745 gen_##name(ctx, 1, 1); \
1746}
51789c41 1747
636aa200
BS
1748static inline void gen_rldinm(DisasContext *ctx, uint32_t mb, uint32_t me,
1749 uint32_t sh)
51789c41 1750{
d03ef511
AJ
1751 if (likely(sh != 0 && mb == 0 && me == (63 - sh))) {
1752 tcg_gen_shli_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)], sh);
1753 } else if (likely(sh != 0 && me == 63 && sh == (64 - mb))) {
1754 tcg_gen_shri_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)], mb);
1755 } else {
a7812ae4 1756 TCGv t0 = tcg_temp_new();
54843a58 1757 tcg_gen_rotli_tl(t0, cpu_gpr[rS(ctx->opcode)], sh);
d03ef511 1758 if (likely(mb == 0 && me == 63)) {
54843a58 1759 tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], t0);
d03ef511
AJ
1760 } else {
1761 tcg_gen_andi_tl(cpu_gpr[rA(ctx->opcode)], t0, MASK(mb, me));
51789c41 1762 }
d03ef511 1763 tcg_temp_free(t0);
51789c41 1764 }
51789c41 1765 if (unlikely(Rc(ctx->opcode) != 0))
d03ef511 1766 gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
51789c41 1767}
d9bce9d9 1768/* rldicl - rldicl. */
636aa200 1769static inline void gen_rldicl(DisasContext *ctx, int mbn, int shn)
d9bce9d9 1770{
51789c41 1771 uint32_t sh, mb;
d9bce9d9 1772
9d53c753
JM
1773 sh = SH(ctx->opcode) | (shn << 5);
1774 mb = MB(ctx->opcode) | (mbn << 5);
51789c41 1775 gen_rldinm(ctx, mb, 63, sh);
d9bce9d9 1776}
51789c41 1777GEN_PPC64_R4(rldicl, 0x1E, 0x00);
d9bce9d9 1778/* rldicr - rldicr. */
636aa200 1779static inline void gen_rldicr(DisasContext *ctx, int men, int shn)
d9bce9d9 1780{
51789c41 1781 uint32_t sh, me;
d9bce9d9 1782
9d53c753
JM
1783 sh = SH(ctx->opcode) | (shn << 5);
1784 me = MB(ctx->opcode) | (men << 5);
51789c41 1785 gen_rldinm(ctx, 0, me, sh);
d9bce9d9 1786}
51789c41 1787GEN_PPC64_R4(rldicr, 0x1E, 0x02);
d9bce9d9 1788/* rldic - rldic. */
636aa200 1789static inline void gen_rldic(DisasContext *ctx, int mbn, int shn)
d9bce9d9 1790{
51789c41 1791 uint32_t sh, mb;
d9bce9d9 1792
9d53c753
JM
1793 sh = SH(ctx->opcode) | (shn << 5);
1794 mb = MB(ctx->opcode) | (mbn << 5);
51789c41
JM
1795 gen_rldinm(ctx, mb, 63 - sh, sh);
1796}
1797GEN_PPC64_R4(rldic, 0x1E, 0x04);
1798
636aa200 1799static inline void gen_rldnm(DisasContext *ctx, uint32_t mb, uint32_t me)
51789c41 1800{
54843a58 1801 TCGv t0;
d03ef511 1802
a7812ae4 1803 t0 = tcg_temp_new();
d03ef511 1804 tcg_gen_andi_tl(t0, cpu_gpr[rB(ctx->opcode)], 0x3f);
54843a58 1805 tcg_gen_rotl_tl(t0, cpu_gpr[rS(ctx->opcode)], t0);
51789c41 1806 if (unlikely(mb != 0 || me != 63)) {
54843a58
AJ
1807 tcg_gen_andi_tl(cpu_gpr[rA(ctx->opcode)], t0, MASK(mb, me));
1808 } else {
1809 tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], t0);
1810 }
1811 tcg_temp_free(t0);
51789c41 1812 if (unlikely(Rc(ctx->opcode) != 0))
d03ef511 1813 gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
d9bce9d9 1814}
51789c41 1815
d9bce9d9 1816/* rldcl - rldcl. */
636aa200 1817static inline void gen_rldcl(DisasContext *ctx, int mbn)
d9bce9d9 1818{
51789c41 1819 uint32_t mb;
d9bce9d9 1820
9d53c753 1821 mb = MB(ctx->opcode) | (mbn << 5);
51789c41 1822 gen_rldnm(ctx, mb, 63);
d9bce9d9 1823}
36081602 1824GEN_PPC64_R2(rldcl, 0x1E, 0x08);
d9bce9d9 1825/* rldcr - rldcr. */
636aa200 1826static inline void gen_rldcr(DisasContext *ctx, int men)
d9bce9d9 1827{
51789c41 1828 uint32_t me;
d9bce9d9 1829
9d53c753 1830 me = MB(ctx->opcode) | (men << 5);
51789c41 1831 gen_rldnm(ctx, 0, me);
d9bce9d9 1832}
36081602 1833GEN_PPC64_R2(rldcr, 0x1E, 0x09);
d9bce9d9 1834/* rldimi - rldimi. */
636aa200 1835static inline void gen_rldimi(DisasContext *ctx, int mbn, int shn)
d9bce9d9 1836{
271a916e 1837 uint32_t sh, mb, me;
d9bce9d9 1838
9d53c753
JM
1839 sh = SH(ctx->opcode) | (shn << 5);
1840 mb = MB(ctx->opcode) | (mbn << 5);
271a916e 1841 me = 63 - sh;
d03ef511
AJ
1842 if (unlikely(sh == 0 && mb == 0)) {
1843 tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)]);
1844 } else {
1845 TCGv t0, t1;
1846 target_ulong mask;
1847
a7812ae4 1848 t0 = tcg_temp_new();
54843a58 1849 tcg_gen_rotli_tl(t0, cpu_gpr[rS(ctx->opcode)], sh);
a7812ae4 1850 t1 = tcg_temp_new();
d03ef511
AJ
1851 mask = MASK(mb, me);
1852 tcg_gen_andi_tl(t0, t0, mask);
1853 tcg_gen_andi_tl(t1, cpu_gpr[rA(ctx->opcode)], ~mask);
1854 tcg_gen_or_tl(cpu_gpr[rA(ctx->opcode)], t0, t1);
1855 tcg_temp_free(t0);
1856 tcg_temp_free(t1);
51789c41 1857 }
51789c41 1858 if (unlikely(Rc(ctx->opcode) != 0))
d03ef511 1859 gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
d9bce9d9 1860}
36081602 1861GEN_PPC64_R4(rldimi, 0x1E, 0x06);
d9bce9d9
JM
1862#endif
1863
79aceca5 1864/*** Integer shift ***/
99e300ef 1865
54623277 1866/* slw & slw. */
99e300ef 1867static void gen_slw(DisasContext *ctx)
26d67362 1868{
7fd6bf7d 1869 TCGv t0, t1;
26d67362 1870
7fd6bf7d
AJ
1871 t0 = tcg_temp_new();
1872 /* AND rS with a mask that is 0 when rB >= 0x20 */
1873#if defined(TARGET_PPC64)
1874 tcg_gen_shli_tl(t0, cpu_gpr[rB(ctx->opcode)], 0x3a);
1875 tcg_gen_sari_tl(t0, t0, 0x3f);
1876#else
1877 tcg_gen_shli_tl(t0, cpu_gpr[rB(ctx->opcode)], 0x1a);
1878 tcg_gen_sari_tl(t0, t0, 0x1f);
1879#endif
1880 tcg_gen_andc_tl(t0, cpu_gpr[rS(ctx->opcode)], t0);
1881 t1 = tcg_temp_new();
1882 tcg_gen_andi_tl(t1, cpu_gpr[rB(ctx->opcode)], 0x1f);
1883 tcg_gen_shl_tl(cpu_gpr[rA(ctx->opcode)], t0, t1);
1884 tcg_temp_free(t1);
fea0c503 1885 tcg_temp_free(t0);
7fd6bf7d 1886 tcg_gen_ext32u_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rA(ctx->opcode)]);
26d67362
AJ
1887 if (unlikely(Rc(ctx->opcode) != 0))
1888 gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
1889}
99e300ef 1890
54623277 1891/* sraw & sraw. */
99e300ef 1892static void gen_sraw(DisasContext *ctx)
26d67362 1893{
d15f74fb 1894 gen_helper_sraw(cpu_gpr[rA(ctx->opcode)], cpu_env,
a7812ae4 1895 cpu_gpr[rS(ctx->opcode)], cpu_gpr[rB(ctx->opcode)]);
26d67362
AJ
1896 if (unlikely(Rc(ctx->opcode) != 0))
1897 gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
1898}
99e300ef 1899
54623277 1900/* srawi & srawi. */
99e300ef 1901static void gen_srawi(DisasContext *ctx)
79aceca5 1902{
26d67362 1903 int sh = SH(ctx->opcode);
ba4af3e4
RH
1904 TCGv dst = cpu_gpr[rA(ctx->opcode)];
1905 TCGv src = cpu_gpr[rS(ctx->opcode)];
1906 if (sh == 0) {
1907 tcg_gen_mov_tl(dst, src);
da91a00f 1908 tcg_gen_movi_tl(cpu_ca, 0);
26d67362 1909 } else {
ba4af3e4
RH
1910 TCGv t0;
1911 tcg_gen_ext32s_tl(dst, src);
1912 tcg_gen_andi_tl(cpu_ca, dst, (1ULL << sh) - 1);
1913 t0 = tcg_temp_new();
1914 tcg_gen_sari_tl(t0, dst, TARGET_LONG_BITS - 1);
1915 tcg_gen_and_tl(cpu_ca, cpu_ca, t0);
1916 tcg_temp_free(t0);
1917 tcg_gen_setcondi_tl(TCG_COND_NE, cpu_ca, cpu_ca, 0);
1918 tcg_gen_sari_tl(dst, dst, sh);
1919 }
1920 if (unlikely(Rc(ctx->opcode) != 0)) {
1921 gen_set_Rc0(ctx, dst);
d9bce9d9 1922 }
79aceca5 1923}
99e300ef 1924
54623277 1925/* srw & srw. */
99e300ef 1926static void gen_srw(DisasContext *ctx)
26d67362 1927{
fea0c503 1928 TCGv t0, t1;
d9bce9d9 1929
7fd6bf7d
AJ
1930 t0 = tcg_temp_new();
1931 /* AND rS with a mask that is 0 when rB >= 0x20 */
1932#if defined(TARGET_PPC64)
1933 tcg_gen_shli_tl(t0, cpu_gpr[rB(ctx->opcode)], 0x3a);
1934 tcg_gen_sari_tl(t0, t0, 0x3f);
1935#else
1936 tcg_gen_shli_tl(t0, cpu_gpr[rB(ctx->opcode)], 0x1a);
1937 tcg_gen_sari_tl(t0, t0, 0x1f);
1938#endif
1939 tcg_gen_andc_tl(t0, cpu_gpr[rS(ctx->opcode)], t0);
1940 tcg_gen_ext32u_tl(t0, t0);
a7812ae4 1941 t1 = tcg_temp_new();
7fd6bf7d
AJ
1942 tcg_gen_andi_tl(t1, cpu_gpr[rB(ctx->opcode)], 0x1f);
1943 tcg_gen_shr_tl(cpu_gpr[rA(ctx->opcode)], t0, t1);
fea0c503 1944 tcg_temp_free(t1);
fea0c503 1945 tcg_temp_free(t0);
26d67362
AJ
1946 if (unlikely(Rc(ctx->opcode) != 0))
1947 gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
1948}
54623277 1949
d9bce9d9
JM
1950#if defined(TARGET_PPC64)
1951/* sld & sld. */
99e300ef 1952static void gen_sld(DisasContext *ctx)
26d67362 1953{
7fd6bf7d 1954 TCGv t0, t1;
26d67362 1955
7fd6bf7d
AJ
1956 t0 = tcg_temp_new();
1957 /* AND rS with a mask that is 0 when rB >= 0x40 */
1958 tcg_gen_shli_tl(t0, cpu_gpr[rB(ctx->opcode)], 0x39);
1959 tcg_gen_sari_tl(t0, t0, 0x3f);
1960 tcg_gen_andc_tl(t0, cpu_gpr[rS(ctx->opcode)], t0);
1961 t1 = tcg_temp_new();
1962 tcg_gen_andi_tl(t1, cpu_gpr[rB(ctx->opcode)], 0x3f);
1963 tcg_gen_shl_tl(cpu_gpr[rA(ctx->opcode)], t0, t1);
1964 tcg_temp_free(t1);
fea0c503 1965 tcg_temp_free(t0);
26d67362
AJ
1966 if (unlikely(Rc(ctx->opcode) != 0))
1967 gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
1968}
99e300ef 1969
54623277 1970/* srad & srad. */
99e300ef 1971static void gen_srad(DisasContext *ctx)
26d67362 1972{
d15f74fb 1973 gen_helper_srad(cpu_gpr[rA(ctx->opcode)], cpu_env,
a7812ae4 1974 cpu_gpr[rS(ctx->opcode)], cpu_gpr[rB(ctx->opcode)]);
26d67362
AJ
1975 if (unlikely(Rc(ctx->opcode) != 0))
1976 gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
1977}
d9bce9d9 1978/* sradi & sradi. */
636aa200 1979static inline void gen_sradi(DisasContext *ctx, int n)
d9bce9d9 1980{
26d67362 1981 int sh = SH(ctx->opcode) + (n << 5);
ba4af3e4
RH
1982 TCGv dst = cpu_gpr[rA(ctx->opcode)];
1983 TCGv src = cpu_gpr[rS(ctx->opcode)];
1984 if (sh == 0) {
1985 tcg_gen_mov_tl(dst, src);
da91a00f 1986 tcg_gen_movi_tl(cpu_ca, 0);
26d67362 1987 } else {
ba4af3e4
RH
1988 TCGv t0;
1989 tcg_gen_andi_tl(cpu_ca, src, (1ULL << sh) - 1);
1990 t0 = tcg_temp_new();
1991 tcg_gen_sari_tl(t0, src, TARGET_LONG_BITS - 1);
1992 tcg_gen_and_tl(cpu_ca, cpu_ca, t0);
1993 tcg_temp_free(t0);
1994 tcg_gen_setcondi_tl(TCG_COND_NE, cpu_ca, cpu_ca, 0);
1995 tcg_gen_sari_tl(dst, src, sh);
1996 }
1997 if (unlikely(Rc(ctx->opcode) != 0)) {
1998 gen_set_Rc0(ctx, dst);
d9bce9d9 1999 }
d9bce9d9 2000}
e8eaa2c0
BS
2001
2002static void gen_sradi0(DisasContext *ctx)
d9bce9d9
JM
2003{
2004 gen_sradi(ctx, 0);
2005}
e8eaa2c0
BS
2006
2007static void gen_sradi1(DisasContext *ctx)
d9bce9d9
JM
2008{
2009 gen_sradi(ctx, 1);
2010}
99e300ef 2011
54623277 2012/* srd & srd. */
99e300ef 2013static void gen_srd(DisasContext *ctx)
26d67362 2014{
7fd6bf7d 2015 TCGv t0, t1;
26d67362 2016
7fd6bf7d
AJ
2017 t0 = tcg_temp_new();
2018 /* AND rS with a mask that is 0 when rB >= 0x40 */
2019 tcg_gen_shli_tl(t0, cpu_gpr[rB(ctx->opcode)], 0x39);
2020 tcg_gen_sari_tl(t0, t0, 0x3f);
2021 tcg_gen_andc_tl(t0, cpu_gpr[rS(ctx->opcode)], t0);
2022 t1 = tcg_temp_new();
2023 tcg_gen_andi_tl(t1, cpu_gpr[rB(ctx->opcode)], 0x3f);
2024 tcg_gen_shr_tl(cpu_gpr[rA(ctx->opcode)], t0, t1);
2025 tcg_temp_free(t1);
fea0c503 2026 tcg_temp_free(t0);
26d67362
AJ
2027 if (unlikely(Rc(ctx->opcode) != 0))
2028 gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
2029}
d9bce9d9 2030#endif
79aceca5
FB
2031
2032/*** Floating-Point arithmetic ***/
7c58044c 2033#define _GEN_FLOAT_ACB(name, op, op1, op2, isfloat, set_fprf, type) \
99e300ef 2034static void gen_f##name(DisasContext *ctx) \
9a64fbe4 2035{ \
76a66253 2036 if (unlikely(!ctx->fpu_enabled)) { \
e06fcd75 2037 gen_exception(ctx, POWERPC_EXCP_FPU); \
3cc62370
FB
2038 return; \
2039 } \
eb44b959
AJ
2040 /* NIP cannot be restored if the memory exception comes from an helper */ \
2041 gen_update_nip(ctx, ctx->nip - 4); \
7c58044c 2042 gen_reset_fpstatus(); \
8e703949
BS
2043 gen_helper_f##op(cpu_fpr[rD(ctx->opcode)], cpu_env, \
2044 cpu_fpr[rA(ctx->opcode)], \
af12906f 2045 cpu_fpr[rC(ctx->opcode)], cpu_fpr[rB(ctx->opcode)]); \
4ecc3190 2046 if (isfloat) { \
8e703949
BS
2047 gen_helper_frsp(cpu_fpr[rD(ctx->opcode)], cpu_env, \
2048 cpu_fpr[rD(ctx->opcode)]); \
4ecc3190 2049 } \
af12906f
AJ
2050 gen_compute_fprf(cpu_fpr[rD(ctx->opcode)], set_fprf, \
2051 Rc(ctx->opcode) != 0); \
9a64fbe4
FB
2052}
2053
7c58044c
JM
2054#define GEN_FLOAT_ACB(name, op2, set_fprf, type) \
2055_GEN_FLOAT_ACB(name, name, 0x3F, op2, 0, set_fprf, type); \
2056_GEN_FLOAT_ACB(name##s, name, 0x3B, op2, 1, set_fprf, type);
9a64fbe4 2057
7c58044c 2058#define _GEN_FLOAT_AB(name, op, op1, op2, inval, isfloat, set_fprf, type) \
99e300ef 2059static void gen_f##name(DisasContext *ctx) \
9a64fbe4 2060{ \
76a66253 2061 if (unlikely(!ctx->fpu_enabled)) { \
e06fcd75 2062 gen_exception(ctx, POWERPC_EXCP_FPU); \
3cc62370
FB
2063 return; \
2064 } \
eb44b959
AJ
2065 /* NIP cannot be restored if the memory exception comes from an helper */ \
2066 gen_update_nip(ctx, ctx->nip - 4); \
7c58044c 2067 gen_reset_fpstatus(); \
8e703949
BS
2068 gen_helper_f##op(cpu_fpr[rD(ctx->opcode)], cpu_env, \
2069 cpu_fpr[rA(ctx->opcode)], \
af12906f 2070 cpu_fpr[rB(ctx->opcode)]); \
4ecc3190 2071 if (isfloat) { \
8e703949
BS
2072 gen_helper_frsp(cpu_fpr[rD(ctx->opcode)], cpu_env, \
2073 cpu_fpr[rD(ctx->opcode)]); \
4ecc3190 2074 } \
af12906f
AJ
2075 gen_compute_fprf(cpu_fpr[rD(ctx->opcode)], \
2076 set_fprf, Rc(ctx->opcode) != 0); \
9a64fbe4 2077}
7c58044c
JM
2078#define GEN_FLOAT_AB(name, op2, inval, set_fprf, type) \
2079_GEN_FLOAT_AB(name, name, 0x3F, op2, inval, 0, set_fprf, type); \
2080_GEN_FLOAT_AB(name##s, name, 0x3B, op2, inval, 1, set_fprf, type);
9a64fbe4 2081
7c58044c 2082#define _GEN_FLOAT_AC(name, op, op1, op2, inval, isfloat, set_fprf, type) \
99e300ef 2083static void gen_f##name(DisasContext *ctx) \
9a64fbe4 2084{ \
76a66253 2085 if (unlikely(!ctx->fpu_enabled)) { \
e06fcd75 2086 gen_exception(ctx, POWERPC_EXCP_FPU); \
3cc62370
FB
2087 return; \
2088 } \
eb44b959
AJ
2089 /* NIP cannot be restored if the memory exception comes from an helper */ \
2090 gen_update_nip(ctx, ctx->nip - 4); \
7c58044c 2091 gen_reset_fpstatus(); \
8e703949
BS
2092 gen_helper_f##op(cpu_fpr[rD(ctx->opcode)], cpu_env, \
2093 cpu_fpr[rA(ctx->opcode)], \
2094 cpu_fpr[rC(ctx->opcode)]); \
4ecc3190 2095 if (isfloat) { \
8e703949
BS
2096 gen_helper_frsp(cpu_fpr[rD(ctx->opcode)], cpu_env, \
2097 cpu_fpr[rD(ctx->opcode)]); \
4ecc3190 2098 } \
af12906f
AJ
2099 gen_compute_fprf(cpu_fpr[rD(ctx->opcode)], \
2100 set_fprf, Rc(ctx->opcode) != 0); \
9a64fbe4 2101}
7c58044c
JM
2102#define GEN_FLOAT_AC(name, op2, inval, set_fprf, type) \
2103_GEN_FLOAT_AC(name, name, 0x3F, op2, inval, 0, set_fprf, type); \
2104_GEN_FLOAT_AC(name##s, name, 0x3B, op2, inval, 1, set_fprf, type);
9a64fbe4 2105
7c58044c 2106#define GEN_FLOAT_B(name, op2, op3, set_fprf, type) \
99e300ef 2107static void gen_f##name(DisasContext *ctx) \
9a64fbe4 2108{ \
76a66253 2109 if (unlikely(!ctx->fpu_enabled)) { \
e06fcd75 2110 gen_exception(ctx, POWERPC_EXCP_FPU); \
3cc62370
FB
2111 return; \
2112 } \
eb44b959
AJ
2113 /* NIP cannot be restored if the memory exception comes from an helper */ \
2114 gen_update_nip(ctx, ctx->nip - 4); \
7c58044c 2115 gen_reset_fpstatus(); \
8e703949
BS
2116 gen_helper_f##name(cpu_fpr[rD(ctx->opcode)], cpu_env, \
2117 cpu_fpr[rB(ctx->opcode)]); \
af12906f
AJ
2118 gen_compute_fprf(cpu_fpr[rD(ctx->opcode)], \
2119 set_fprf, Rc(ctx->opcode) != 0); \
79aceca5
FB
2120}
2121
7c58044c 2122#define GEN_FLOAT_BS(name, op1, op2, set_fprf, type) \
99e300ef 2123static void gen_f##name(DisasContext *ctx) \
9a64fbe4 2124{ \
76a66253 2125 if (unlikely(!ctx->fpu_enabled)) { \
e06fcd75 2126 gen_exception(ctx, POWERPC_EXCP_FPU); \
3cc62370
FB
2127 return; \
2128 } \
eb44b959
AJ
2129 /* NIP cannot be restored if the memory exception comes from an helper */ \
2130 gen_update_nip(ctx, ctx->nip - 4); \
7c58044c 2131 gen_reset_fpstatus(); \
8e703949
BS
2132 gen_helper_f##name(cpu_fpr[rD(ctx->opcode)], cpu_env, \
2133 cpu_fpr[rB(ctx->opcode)]); \
af12906f
AJ
2134 gen_compute_fprf(cpu_fpr[rD(ctx->opcode)], \
2135 set_fprf, Rc(ctx->opcode) != 0); \
79aceca5
FB
2136}
2137
9a64fbe4 2138/* fadd - fadds */
7c58044c 2139GEN_FLOAT_AB(add, 0x15, 0x000007C0, 1, PPC_FLOAT);
4ecc3190 2140/* fdiv - fdivs */
7c58044c 2141GEN_FLOAT_AB(div, 0x12, 0x000007C0, 1, PPC_FLOAT);
4ecc3190 2142/* fmul - fmuls */
7c58044c 2143GEN_FLOAT_AC(mul, 0x19, 0x0000F800, 1, PPC_FLOAT);
79aceca5 2144
d7e4b87e 2145/* fre */
7c58044c 2146GEN_FLOAT_BS(re, 0x3F, 0x18, 1, PPC_FLOAT_EXT);
d7e4b87e 2147
a750fc0b 2148/* fres */
7c58044c 2149GEN_FLOAT_BS(res, 0x3B, 0x18, 1, PPC_FLOAT_FRES);
79aceca5 2150
a750fc0b 2151/* frsqrte */
7c58044c
JM
2152GEN_FLOAT_BS(rsqrte, 0x3F, 0x1A, 1, PPC_FLOAT_FRSQRTE);
2153
2154/* frsqrtes */
99e300ef 2155static void gen_frsqrtes(DisasContext *ctx)
7c58044c 2156{
af12906f 2157 if (unlikely(!ctx->fpu_enabled)) {
e06fcd75 2158 gen_exception(ctx, POWERPC_EXCP_FPU);
af12906f
AJ
2159 return;
2160 }
eb44b959
AJ
2161 /* NIP cannot be restored if the memory exception comes from an helper */
2162 gen_update_nip(ctx, ctx->nip - 4);
af12906f 2163 gen_reset_fpstatus();
8e703949
BS
2164 gen_helper_frsqrte(cpu_fpr[rD(ctx->opcode)], cpu_env,
2165 cpu_fpr[rB(ctx->opcode)]);
2166 gen_helper_frsp(cpu_fpr[rD(ctx->opcode)], cpu_env,
2167 cpu_fpr[rD(ctx->opcode)]);
af12906f 2168 gen_compute_fprf(cpu_fpr[rD(ctx->opcode)], 1, Rc(ctx->opcode) != 0);
7c58044c 2169}
79aceca5 2170
a750fc0b 2171/* fsel */
7c58044c 2172_GEN_FLOAT_ACB(sel, sel, 0x3F, 0x17, 0, 0, PPC_FLOAT_FSEL);
4ecc3190 2173/* fsub - fsubs */
7c58044c 2174GEN_FLOAT_AB(sub, 0x14, 0x000007C0, 1, PPC_FLOAT);
79aceca5 2175/* Optional: */
99e300ef 2176
54623277 2177/* fsqrt */
99e300ef 2178static void gen_fsqrt(DisasContext *ctx)
c7d344af 2179{
76a66253 2180 if (unlikely(!ctx->fpu_enabled)) {
e06fcd75 2181 gen_exception(ctx, POWERPC_EXCP_FPU);
c7d344af
FB
2182 return;
2183 }
eb44b959
AJ
2184 /* NIP cannot be restored if the memory exception comes from an helper */
2185 gen_update_nip(ctx, ctx->nip - 4);
7c58044c 2186 gen_reset_fpstatus();
8e703949
BS
2187 gen_helper_fsqrt(cpu_fpr[rD(ctx->opcode)], cpu_env,
2188 cpu_fpr[rB(ctx->opcode)]);
af12906f 2189 gen_compute_fprf(cpu_fpr[rD(ctx->opcode)], 1, Rc(ctx->opcode) != 0);
c7d344af 2190}
79aceca5 2191
99e300ef 2192static void gen_fsqrts(DisasContext *ctx)
79aceca5 2193{
76a66253 2194 if (unlikely(!ctx->fpu_enabled)) {
e06fcd75 2195 gen_exception(ctx, POWERPC_EXCP_FPU);
3cc62370
FB
2196 return;
2197 }
eb44b959
AJ
2198 /* NIP cannot be restored if the memory exception comes from an helper */
2199 gen_update_nip(ctx, ctx->nip - 4);
7c58044c 2200 gen_reset_fpstatus();
8e703949
BS
2201 gen_helper_fsqrt(cpu_fpr[rD(ctx->opcode)], cpu_env,
2202 cpu_fpr[rB(ctx->opcode)]);
2203 gen_helper_frsp(cpu_fpr[rD(ctx->opcode)], cpu_env,
2204 cpu_fpr[rD(ctx->opcode)]);
af12906f 2205 gen_compute_fprf(cpu_fpr[rD(ctx->opcode)], 1, Rc(ctx->opcode) != 0);
79aceca5
FB
2206}
2207
2208/*** Floating-Point multiply-and-add ***/
4ecc3190 2209/* fmadd - fmadds */
7c58044c 2210GEN_FLOAT_ACB(madd, 0x1D, 1, PPC_FLOAT);
4ecc3190 2211/* fmsub - fmsubs */
7c58044c 2212GEN_FLOAT_ACB(msub, 0x1C, 1, PPC_FLOAT);
4ecc3190 2213/* fnmadd - fnmadds */
7c58044c 2214GEN_FLOAT_ACB(nmadd, 0x1F, 1, PPC_FLOAT);
4ecc3190 2215/* fnmsub - fnmsubs */
7c58044c 2216GEN_FLOAT_ACB(nmsub, 0x1E, 1, PPC_FLOAT);
79aceca5
FB
2217
2218/*** Floating-Point round & convert ***/
2219/* fctiw */
7c58044c 2220GEN_FLOAT_B(ctiw, 0x0E, 0x00, 0, PPC_FLOAT);
fab7fe42
TM
2221/* fctiwu */
2222GEN_FLOAT_B(ctiwu, 0x0E, 0x04, 0, PPC2_FP_CVT_ISA206);
79aceca5 2223/* fctiwz */
7c58044c 2224GEN_FLOAT_B(ctiwz, 0x0F, 0x00, 0, PPC_FLOAT);
fab7fe42
TM
2225/* fctiwuz */
2226GEN_FLOAT_B(ctiwuz, 0x0F, 0x04, 0, PPC2_FP_CVT_ISA206);
79aceca5 2227/* frsp */
7c58044c 2228GEN_FLOAT_B(rsp, 0x0C, 0x00, 1, PPC_FLOAT);
426613db
JM
2229#if defined(TARGET_PPC64)
2230/* fcfid */
7c58044c 2231GEN_FLOAT_B(cfid, 0x0E, 0x1A, 1, PPC_64B);
28288b48
TM
2232/* fcfids */
2233GEN_FLOAT_B(cfids, 0x0E, 0x1A, 0, PPC2_FP_CVT_ISA206);
2234/* fcfidu */
2235GEN_FLOAT_B(cfidu, 0x0E, 0x1E, 0, PPC2_FP_CVT_ISA206);
2236/* fcfidus */
2237GEN_FLOAT_B(cfidus, 0x0E, 0x1E, 0, PPC2_FP_CVT_ISA206);
426613db 2238/* fctid */
7c58044c 2239GEN_FLOAT_B(ctid, 0x0E, 0x19, 0, PPC_64B);
fab7fe42
TM
2240/* fctidu */
2241GEN_FLOAT_B(ctidu, 0x0E, 0x1D, 0, PPC2_FP_CVT_ISA206);
426613db 2242/* fctidz */
7c58044c 2243GEN_FLOAT_B(ctidz, 0x0F, 0x19, 0, PPC_64B);
fab7fe42
TM
2244/* fctidu */
2245GEN_FLOAT_B(ctiduz, 0x0F, 0x1D, 0, PPC2_FP_CVT_ISA206);
426613db 2246#endif
79aceca5 2247
d7e4b87e 2248/* frin */
7c58044c 2249GEN_FLOAT_B(rin, 0x08, 0x0C, 1, PPC_FLOAT_EXT);
d7e4b87e 2250/* friz */
7c58044c 2251GEN_FLOAT_B(riz, 0x08, 0x0D, 1, PPC_FLOAT_EXT);
d7e4b87e 2252/* frip */
7c58044c 2253GEN_FLOAT_B(rip, 0x08, 0x0E, 1, PPC_FLOAT_EXT);
d7e4b87e 2254/* frim */
7c58044c 2255GEN_FLOAT_B(rim, 0x08, 0x0F, 1, PPC_FLOAT_EXT);
d7e4b87e 2256
da29cb7b
TM
2257static void gen_ftdiv(DisasContext *ctx)
2258{
2259 if (unlikely(!ctx->fpu_enabled)) {
2260 gen_exception(ctx, POWERPC_EXCP_FPU);
2261 return;
2262 }
2263 gen_helper_ftdiv(cpu_crf[crfD(ctx->opcode)], cpu_fpr[rA(ctx->opcode)],
2264 cpu_fpr[rB(ctx->opcode)]);
2265}
2266
6d41d146
TM
2267static void gen_ftsqrt(DisasContext *ctx)
2268{
2269 if (unlikely(!ctx->fpu_enabled)) {
2270 gen_exception(ctx, POWERPC_EXCP_FPU);
2271 return;
2272 }
2273 gen_helper_ftsqrt(cpu_crf[crfD(ctx->opcode)], cpu_fpr[rB(ctx->opcode)]);
2274}
2275
da29cb7b
TM
2276
2277
79aceca5 2278/*** Floating-Point compare ***/
99e300ef 2279
54623277 2280/* fcmpo */
99e300ef 2281static void gen_fcmpo(DisasContext *ctx)
79aceca5 2282{
330c483b 2283 TCGv_i32 crf;
76a66253 2284 if (unlikely(!ctx->fpu_enabled)) {
e06fcd75 2285 gen_exception(ctx, POWERPC_EXCP_FPU);
3cc62370
FB
2286 return;
2287 }
eb44b959
AJ
2288 /* NIP cannot be restored if the memory exception comes from an helper */
2289 gen_update_nip(ctx, ctx->nip - 4);
7c58044c 2290 gen_reset_fpstatus();
9a819377 2291 crf = tcg_const_i32(crfD(ctx->opcode));
8e703949
BS
2292 gen_helper_fcmpo(cpu_env, cpu_fpr[rA(ctx->opcode)],
2293 cpu_fpr[rB(ctx->opcode)], crf);
330c483b 2294 tcg_temp_free_i32(crf);
8e703949 2295 gen_helper_float_check_status(cpu_env);
79aceca5
FB
2296}
2297
2298/* fcmpu */
99e300ef 2299static void gen_fcmpu(DisasContext *ctx)
79aceca5 2300{
330c483b 2301 TCGv_i32 crf;
76a66253 2302 if (unlikely(!ctx->fpu_enabled)) {
e06fcd75 2303 gen_exception(ctx, POWERPC_EXCP_FPU);
3cc62370
FB
2304 return;
2305 }
eb44b959
AJ
2306 /* NIP cannot be restored if the memory exception comes from an helper */
2307 gen_update_nip(ctx, ctx->nip - 4);
7c58044c 2308 gen_reset_fpstatus();
9a819377 2309 crf = tcg_const_i32(crfD(ctx->opcode));
8e703949
BS
2310 gen_helper_fcmpu(cpu_env, cpu_fpr[rA(ctx->opcode)],
2311 cpu_fpr[rB(ctx->opcode)], crf);
330c483b 2312 tcg_temp_free_i32(crf);
8e703949 2313 gen_helper_float_check_status(cpu_env);
79aceca5
FB
2314}
2315
9a64fbe4
FB
2316/*** Floating-point move ***/
2317/* fabs */
7c58044c 2318/* XXX: beware that fabs never checks for NaNs nor update FPSCR */
bf45a2e6
AJ
2319static void gen_fabs(DisasContext *ctx)
2320{
2321 if (unlikely(!ctx->fpu_enabled)) {
2322 gen_exception(ctx, POWERPC_EXCP_FPU);
2323 return;
2324 }
2325 tcg_gen_andi_i64(cpu_fpr[rD(ctx->opcode)], cpu_fpr[rB(ctx->opcode)],
2326 ~(1ULL << 63));
2327 gen_compute_fprf(cpu_fpr[rD(ctx->opcode)], 0, Rc(ctx->opcode) != 0);
2328}
9a64fbe4
FB
2329
2330/* fmr - fmr. */
7c58044c 2331/* XXX: beware that fmr never checks for NaNs nor update FPSCR */
99e300ef 2332static void gen_fmr(DisasContext *ctx)
9a64fbe4 2333{
76a66253 2334 if (unlikely(!ctx->fpu_enabled)) {
e06fcd75 2335 gen_exception(ctx, POWERPC_EXCP_FPU);
3cc62370
FB
2336 return;
2337 }
af12906f
AJ
2338 tcg_gen_mov_i64(cpu_fpr[rD(ctx->opcode)], cpu_fpr[rB(ctx->opcode)]);
2339 gen_compute_fprf(cpu_fpr[rD(ctx->opcode)], 0, Rc(ctx->opcode) != 0);
9a64fbe4
FB
2340}
2341
2342/* fnabs */
7c58044c 2343/* XXX: beware that fnabs never checks for NaNs nor update FPSCR */
bf45a2e6
AJ
2344static void gen_fnabs(DisasContext *ctx)
2345{
2346 if (unlikely(!ctx->fpu_enabled)) {
2347 gen_exception(ctx, POWERPC_EXCP_FPU);
2348 return;
2349 }
2350 tcg_gen_ori_i64(cpu_fpr[rD(ctx->opcode)], cpu_fpr[rB(ctx->opcode)],
2351 1ULL << 63);
2352 gen_compute_fprf(cpu_fpr[rD(ctx->opcode)], 0, Rc(ctx->opcode) != 0);
2353}
2354
9a64fbe4 2355/* fneg */
7c58044c 2356/* XXX: beware that fneg never checks for NaNs nor update FPSCR */
bf45a2e6
AJ
2357static void gen_fneg(DisasContext *ctx)
2358{
2359 if (unlikely(!ctx->fpu_enabled)) {
2360 gen_exception(ctx, POWERPC_EXCP_FPU);
2361 return;
2362 }
2363 tcg_gen_xori_i64(cpu_fpr[rD(ctx->opcode)], cpu_fpr[rB(ctx->opcode)],
2364 1ULL << 63);
2365 gen_compute_fprf(cpu_fpr[rD(ctx->opcode)], 0, Rc(ctx->opcode) != 0);
2366}
9a64fbe4 2367
f0332888
AJ
2368/* fcpsgn: PowerPC 2.05 specification */
2369/* XXX: beware that fcpsgn never checks for NaNs nor update FPSCR */
2370static void gen_fcpsgn(DisasContext *ctx)
2371{
2372 if (unlikely(!ctx->fpu_enabled)) {
2373 gen_exception(ctx, POWERPC_EXCP_FPU);
2374 return;
2375 }
2376 tcg_gen_deposit_i64(cpu_fpr[rD(ctx->opcode)], cpu_fpr[rA(ctx->opcode)],
2377 cpu_fpr[rB(ctx->opcode)], 0, 63);
2378 gen_compute_fprf(cpu_fpr[rD(ctx->opcode)], 0, Rc(ctx->opcode) != 0);
2379}
2380
097ec5d8
TM
2381static void gen_fmrgew(DisasContext *ctx)
2382{
2383 TCGv_i64 b0;
2384 if (unlikely(!ctx->fpu_enabled)) {
2385 gen_exception(ctx, POWERPC_EXCP_FPU);
2386 return;
2387 }
2388 b0 = tcg_temp_new_i64();
2389 tcg_gen_shri_i64(b0, cpu_fpr[rB(ctx->opcode)], 32);
2390 tcg_gen_deposit_i64(cpu_fpr[rD(ctx->opcode)], cpu_fpr[rA(ctx->opcode)],
2391 b0, 0, 32);
2392 tcg_temp_free_i64(b0);
2393}
2394
2395static void gen_fmrgow(DisasContext *ctx)
2396{
2397 if (unlikely(!ctx->fpu_enabled)) {
2398 gen_exception(ctx, POWERPC_EXCP_FPU);
2399 return;
2400 }
2401 tcg_gen_deposit_i64(cpu_fpr[rD(ctx->opcode)],
2402 cpu_fpr[rB(ctx->opcode)],
2403 cpu_fpr[rA(ctx->opcode)],
2404 32, 32);
2405}
2406
79aceca5 2407/*** Floating-Point status & ctrl register ***/
99e300ef 2408
54623277 2409/* mcrfs */
99e300ef 2410static void gen_mcrfs(DisasContext *ctx)
79aceca5 2411{
30304420 2412 TCGv tmp = tcg_temp_new();
7c58044c
JM
2413 int bfa;
2414
76a66253 2415 if (unlikely(!ctx->fpu_enabled)) {
e06fcd75 2416 gen_exception(ctx, POWERPC_EXCP_FPU);
3cc62370
FB
2417 return;
2418 }
7c58044c 2419 bfa = 4 * (7 - crfS(ctx->opcode));
30304420
DG
2420 tcg_gen_shri_tl(tmp, cpu_fpscr, bfa);
2421 tcg_gen_trunc_tl_i32(cpu_crf[crfD(ctx->opcode)], tmp);
2422 tcg_temp_free(tmp);
e1571908 2423 tcg_gen_andi_i32(cpu_crf[crfD(ctx->opcode)], cpu_crf[crfD(ctx->opcode)], 0xf);
30304420 2424 tcg_gen_andi_tl(cpu_fpscr, cpu_fpscr, ~(0xF << bfa));
79aceca5
FB
2425}
2426
2427/* mffs */
99e300ef 2428static void gen_mffs(DisasContext *ctx)
79aceca5 2429{
76a66253 2430 if (unlikely(!ctx->fpu_enabled)) {
e06fcd75 2431 gen_exception(ctx, POWERPC_EXCP_FPU);
3cc62370
FB
2432 return;
2433 }
7c58044c 2434 gen_reset_fpstatus();
30304420 2435 tcg_gen_extu_tl_i64(cpu_fpr[rD(ctx->opcode)], cpu_fpscr);
af12906f 2436 gen_compute_fprf(cpu_fpr[rD(ctx->opcode)], 0, Rc(ctx->opcode) != 0);
79aceca5
FB
2437}
2438
2439/* mtfsb0 */
99e300ef 2440static void gen_mtfsb0(DisasContext *ctx)
79aceca5 2441{
fb0eaffc 2442 uint8_t crb;
3b46e624 2443
76a66253 2444 if (unlikely(!ctx->fpu_enabled)) {
e06fcd75 2445 gen_exception(ctx, POWERPC_EXCP_FPU);
3cc62370
FB
2446 return;
2447 }
6e35d524 2448 crb = 31 - crbD(ctx->opcode);
7c58044c 2449 gen_reset_fpstatus();
6e35d524 2450 if (likely(crb != FPSCR_FEX && crb != FPSCR_VX)) {
eb44b959
AJ
2451 TCGv_i32 t0;
2452 /* NIP cannot be restored if the memory exception comes from an helper */
2453 gen_update_nip(ctx, ctx->nip - 4);
2454 t0 = tcg_const_i32(crb);
8e703949 2455 gen_helper_fpscr_clrbit(cpu_env, t0);
6e35d524
AJ
2456 tcg_temp_free_i32(t0);
2457 }
7c58044c 2458 if (unlikely(Rc(ctx->opcode) != 0)) {
30304420
DG
2459 tcg_gen_trunc_tl_i32(cpu_crf[1], cpu_fpscr);
2460 tcg_gen_shri_i32(cpu_crf[1], cpu_crf[1], FPSCR_OX);
7c58044c 2461 }
79aceca5
FB
2462}
2463
2464/* mtfsb1 */
99e300ef 2465static void gen_mtfsb1(DisasContext *ctx)
79aceca5 2466{
fb0eaffc 2467 uint8_t crb;
3b46e624 2468
76a66253 2469 if (unlikely(!ctx->fpu_enabled)) {
e06fcd75 2470 gen_exception(ctx, POWERPC_EXCP_FPU);
3cc62370
FB
2471 return;
2472 }
6e35d524 2473 crb = 31 - crbD(ctx->opcode);
7c58044c
JM
2474 gen_reset_fpstatus();
2475 /* XXX: we pretend we can only do IEEE floating-point computations */
af12906f 2476 if (likely(crb != FPSCR_FEX && crb != FPSCR_VX && crb != FPSCR_NI)) {
eb44b959
AJ
2477 TCGv_i32 t0;
2478 /* NIP cannot be restored if the memory exception comes from an helper */
2479 gen_update_nip(ctx, ctx->nip - 4);
2480 t0 = tcg_const_i32(crb);
8e703949 2481 gen_helper_fpscr_setbit(cpu_env, t0);
0f2f39c2 2482 tcg_temp_free_i32(t0);
af12906f 2483 }
7c58044c 2484 if (unlikely(Rc(ctx->opcode) != 0)) {
30304420
DG
2485 tcg_gen_trunc_tl_i32(cpu_crf[1], cpu_fpscr);
2486 tcg_gen_shri_i32(cpu_crf[1], cpu_crf[1], FPSCR_OX);
7c58044c
JM
2487 }
2488 /* We can raise a differed exception */
8e703949 2489 gen_helper_float_check_status(cpu_env);
79aceca5
FB
2490}
2491
2492/* mtfsf */
99e300ef 2493static void gen_mtfsf(DisasContext *ctx)
79aceca5 2494{
0f2f39c2 2495 TCGv_i32 t0;
7d08d856 2496 int flm, l, w;
af12906f 2497
76a66253 2498 if (unlikely(!ctx->fpu_enabled)) {
e06fcd75 2499 gen_exception(ctx, POWERPC_EXCP_FPU);
3cc62370
FB
2500 return;
2501 }
7d08d856
AJ
2502 flm = FPFLM(ctx->opcode);
2503 l = FPL(ctx->opcode);
2504 w = FPW(ctx->opcode);
2505 if (unlikely(w & !(ctx->insns_flags2 & PPC2_ISA205))) {
2506 gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL);
2507 return;
2508 }
eb44b959
AJ
2509 /* NIP cannot be restored if the memory exception comes from an helper */
2510 gen_update_nip(ctx, ctx->nip - 4);
7c58044c 2511 gen_reset_fpstatus();
7d08d856
AJ
2512 if (l) {
2513 t0 = tcg_const_i32((ctx->insns_flags2 & PPC2_ISA205) ? 0xffff : 0xff);
2514 } else {
2515 t0 = tcg_const_i32(flm << (w * 8));
2516 }
8e703949 2517 gen_helper_store_fpscr(cpu_env, cpu_fpr[rB(ctx->opcode)], t0);
0f2f39c2 2518 tcg_temp_free_i32(t0);
7c58044c 2519 if (unlikely(Rc(ctx->opcode) != 0)) {
30304420
DG
2520 tcg_gen_trunc_tl_i32(cpu_crf[1], cpu_fpscr);
2521 tcg_gen_shri_i32(cpu_crf[1], cpu_crf[1], FPSCR_OX);
7c58044c
JM
2522 }
2523 /* We can raise a differed exception */
8e703949 2524 gen_helper_float_check_status(cpu_env);
79aceca5
FB
2525}
2526
2527/* mtfsfi */
99e300ef 2528static void gen_mtfsfi(DisasContext *ctx)
79aceca5 2529{
7d08d856 2530 int bf, sh, w;
0f2f39c2
AJ
2531 TCGv_i64 t0;
2532 TCGv_i32 t1;
7c58044c 2533
76a66253 2534 if (unlikely(!ctx->fpu_enabled)) {
e06fcd75 2535 gen_exception(ctx, POWERPC_EXCP_FPU);
3cc62370
FB
2536 return;
2537 }
7d08d856
AJ
2538 w = FPW(ctx->opcode);
2539 bf = FPBF(ctx->opcode);
2540 if (unlikely(w & !(ctx->insns_flags2 & PPC2_ISA205))) {
2541 gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL);
2542 return;
2543 }
2544 sh = (8 * w) + 7 - bf;
eb44b959
AJ
2545 /* NIP cannot be restored if the memory exception comes from an helper */
2546 gen_update_nip(ctx, ctx->nip - 4);
7c58044c 2547 gen_reset_fpstatus();
7d08d856 2548 t0 = tcg_const_i64(((uint64_t)FPIMM(ctx->opcode)) << (4 * sh));
af12906f 2549 t1 = tcg_const_i32(1 << sh);
8e703949 2550 gen_helper_store_fpscr(cpu_env, t0, t1);
0f2f39c2
AJ
2551 tcg_temp_free_i64(t0);
2552 tcg_temp_free_i32(t1);
7c58044c 2553 if (unlikely(Rc(ctx->opcode) != 0)) {
30304420
DG
2554 tcg_gen_trunc_tl_i32(cpu_crf[1], cpu_fpscr);
2555 tcg_gen_shri_i32(cpu_crf[1], cpu_crf[1], FPSCR_OX);
7c58044c
JM
2556 }
2557 /* We can raise a differed exception */
8e703949 2558 gen_helper_float_check_status(cpu_env);
79aceca5
FB
2559}
2560
76a66253
JM
2561/*** Addressing modes ***/
2562/* Register indirect with immediate index : EA = (rA|0) + SIMM */
636aa200
BS
2563static inline void gen_addr_imm_index(DisasContext *ctx, TCGv EA,
2564 target_long maskl)
76a66253
JM
2565{
2566 target_long simm = SIMM(ctx->opcode);
2567
be147d08 2568 simm &= ~maskl;
76db3ba4 2569 if (rA(ctx->opcode) == 0) {
c791fe84
RH
2570 if (NARROW_MODE(ctx)) {
2571 simm = (uint32_t)simm;
2572 }
e2be8d8d 2573 tcg_gen_movi_tl(EA, simm);
76db3ba4 2574 } else if (likely(simm != 0)) {
e2be8d8d 2575 tcg_gen_addi_tl(EA, cpu_gpr[rA(ctx->opcode)], simm);
c791fe84 2576 if (NARROW_MODE(ctx)) {
76db3ba4
AJ
2577 tcg_gen_ext32u_tl(EA, EA);
2578 }
76db3ba4 2579 } else {
c791fe84 2580 if (NARROW_MODE(ctx)) {
76db3ba4 2581 tcg_gen_ext32u_tl(EA, cpu_gpr[rA(ctx->opcode)]);
c791fe84
RH
2582 } else {
2583 tcg_gen_mov_tl(EA, cpu_gpr[rA(ctx->opcode)]);
2584 }
76db3ba4 2585 }
76a66253
JM
2586}
2587
636aa200 2588static inline void gen_addr_reg_index(DisasContext *ctx, TCGv EA)
76a66253 2589{
76db3ba4 2590 if (rA(ctx->opcode) == 0) {
c791fe84 2591 if (NARROW_MODE(ctx)) {
76db3ba4 2592 tcg_gen_ext32u_tl(EA, cpu_gpr[rB(ctx->opcode)]);
c791fe84
RH
2593 } else {
2594 tcg_gen_mov_tl(EA, cpu_gpr[rB(ctx->opcode)]);
2595 }
76db3ba4 2596 } else {
e2be8d8d 2597 tcg_gen_add_tl(EA, cpu_gpr[rA(ctx->opcode)], cpu_gpr[rB(ctx->opcode)]);
c791fe84 2598 if (NARROW_MODE(ctx)) {
76db3ba4
AJ
2599 tcg_gen_ext32u_tl(EA, EA);
2600 }
76db3ba4 2601 }
76a66253
JM
2602}
2603
636aa200 2604static inline void gen_addr_register(DisasContext *ctx, TCGv EA)
76a66253 2605{
76db3ba4 2606 if (rA(ctx->opcode) == 0) {
e2be8d8d 2607 tcg_gen_movi_tl(EA, 0);
c791fe84
RH
2608 } else if (NARROW_MODE(ctx)) {
2609 tcg_gen_ext32u_tl(EA, cpu_gpr[rA(ctx->opcode)]);
76db3ba4 2610 } else {
c791fe84 2611 tcg_gen_mov_tl(EA, cpu_gpr[rA(ctx->opcode)]);
76db3ba4
AJ
2612 }
2613}
2614
636aa200
BS
2615static inline void gen_addr_add(DisasContext *ctx, TCGv ret, TCGv arg1,
2616 target_long val)
76db3ba4
AJ
2617{
2618 tcg_gen_addi_tl(ret, arg1, val);
c791fe84 2619 if (NARROW_MODE(ctx)) {
76db3ba4
AJ
2620 tcg_gen_ext32u_tl(ret, ret);
2621 }
76a66253
JM
2622}
2623
636aa200 2624static inline void gen_check_align(DisasContext *ctx, TCGv EA, int mask)
cf360a32
AJ
2625{
2626 int l1 = gen_new_label();
2627 TCGv t0 = tcg_temp_new();
2628 TCGv_i32 t1, t2;
2629 /* NIP cannot be restored if the memory exception comes from an helper */
2630 gen_update_nip(ctx, ctx->nip - 4);
2631 tcg_gen_andi_tl(t0, EA, mask);
2632 tcg_gen_brcondi_tl(TCG_COND_EQ, t0, 0, l1);
2633 t1 = tcg_const_i32(POWERPC_EXCP_ALIGN);
2634 t2 = tcg_const_i32(0);
e5f17ac6 2635 gen_helper_raise_exception_err(cpu_env, t1, t2);
cf360a32
AJ
2636 tcg_temp_free_i32(t1);
2637 tcg_temp_free_i32(t2);
2638 gen_set_label(l1);
2639 tcg_temp_free(t0);
2640}
2641
7863667f 2642/*** Integer load ***/
636aa200 2643static inline void gen_qemu_ld8u(DisasContext *ctx, TCGv arg1, TCGv arg2)
76db3ba4
AJ
2644{
2645 tcg_gen_qemu_ld8u(arg1, arg2, ctx->mem_idx);
2646}
2647
636aa200 2648static inline void gen_qemu_ld8s(DisasContext *ctx, TCGv arg1, TCGv arg2)
76db3ba4
AJ
2649{
2650 tcg_gen_qemu_ld8s(arg1, arg2, ctx->mem_idx);
2651}
2652
636aa200 2653static inline void gen_qemu_ld16u(DisasContext *ctx, TCGv arg1, TCGv arg2)
76db3ba4
AJ
2654{
2655 tcg_gen_qemu_ld16u(arg1, arg2, ctx->mem_idx);
2656 if (unlikely(ctx->le_mode)) {
fa3966a3 2657 tcg_gen_bswap16_tl(arg1, arg1);
76db3ba4 2658 }
b61f2753
AJ
2659}
2660
636aa200 2661static inline void gen_qemu_ld16s(DisasContext *ctx, TCGv arg1, TCGv arg2)
b61f2753 2662{
76db3ba4 2663 if (unlikely(ctx->le_mode)) {
76db3ba4 2664 tcg_gen_qemu_ld16u(arg1, arg2, ctx->mem_idx);
fa3966a3 2665 tcg_gen_bswap16_tl(arg1, arg1);
76db3ba4 2666 tcg_gen_ext16s_tl(arg1, arg1);
76db3ba4
AJ
2667 } else {
2668 tcg_gen_qemu_ld16s(arg1, arg2, ctx->mem_idx);
2669 }
b61f2753
AJ
2670}
2671
636aa200 2672static inline void gen_qemu_ld32u(DisasContext *ctx, TCGv arg1, TCGv arg2)
b61f2753 2673{
76db3ba4
AJ
2674 tcg_gen_qemu_ld32u(arg1, arg2, ctx->mem_idx);
2675 if (unlikely(ctx->le_mode)) {
fa3966a3 2676 tcg_gen_bswap32_tl(arg1, arg1);
76db3ba4 2677 }
b61f2753
AJ
2678}
2679
f976b09e
AG
2680static void gen_qemu_ld32u_i64(DisasContext *ctx, TCGv_i64 val, TCGv addr)
2681{
2682 TCGv tmp = tcg_temp_new();
2683 gen_qemu_ld32u(ctx, tmp, addr);
2684 tcg_gen_extu_tl_i64(val, tmp);
2685 tcg_temp_free(tmp);
2686}
2687
636aa200 2688static inline void gen_qemu_ld32s(DisasContext *ctx, TCGv arg1, TCGv arg2)
b61f2753 2689{
a457e7ee 2690 if (unlikely(ctx->le_mode)) {
76db3ba4 2691 tcg_gen_qemu_ld32u(arg1, arg2, ctx->mem_idx);
fa3966a3
AJ
2692 tcg_gen_bswap32_tl(arg1, arg1);
2693 tcg_gen_ext32s_tl(arg1, arg1);
b61f2753 2694 } else
76db3ba4 2695 tcg_gen_qemu_ld32s(arg1, arg2, ctx->mem_idx);
b61f2753
AJ
2696}
2697
cac7f0ba
TM
2698static void gen_qemu_ld32s_i64(DisasContext *ctx, TCGv_i64 val, TCGv addr)
2699{
2700 TCGv tmp = tcg_temp_new();
2701 gen_qemu_ld32s(ctx, tmp, addr);
2702 tcg_gen_ext_tl_i64(val, tmp);
2703 tcg_temp_free(tmp);
2704}
2705
636aa200 2706static inline void gen_qemu_ld64(DisasContext *ctx, TCGv_i64 arg1, TCGv arg2)
b61f2753 2707{
76db3ba4
AJ
2708 tcg_gen_qemu_ld64(arg1, arg2, ctx->mem_idx);
2709 if (unlikely(ctx->le_mode)) {
66896cb8 2710 tcg_gen_bswap64_i64(arg1, arg1);
76db3ba4 2711 }
b61f2753
AJ
2712}
2713
636aa200 2714static inline void gen_qemu_st8(DisasContext *ctx, TCGv arg1, TCGv arg2)
b61f2753 2715{
76db3ba4 2716 tcg_gen_qemu_st8(arg1, arg2, ctx->mem_idx);
b61f2753
AJ
2717}
2718
636aa200 2719static inline void gen_qemu_st16(DisasContext *ctx, TCGv arg1, TCGv arg2)
b61f2753 2720{
76db3ba4 2721 if (unlikely(ctx->le_mode)) {
76db3ba4
AJ
2722 TCGv t0 = tcg_temp_new();
2723 tcg_gen_ext16u_tl(t0, arg1);
fa3966a3 2724 tcg_gen_bswap16_tl(t0, t0);
76db3ba4
AJ
2725 tcg_gen_qemu_st16(t0, arg2, ctx->mem_idx);
2726 tcg_temp_free(t0);
76db3ba4
AJ
2727 } else {
2728 tcg_gen_qemu_st16(arg1, arg2, ctx->mem_idx);
2729 }
b61f2753
AJ
2730}
2731
636aa200 2732static inline void gen_qemu_st32(DisasContext *ctx, TCGv arg1, TCGv arg2)
b61f2753 2733{
76db3ba4 2734 if (unlikely(ctx->le_mode)) {
fa3966a3
AJ
2735 TCGv t0 = tcg_temp_new();
2736 tcg_gen_ext32u_tl(t0, arg1);
2737 tcg_gen_bswap32_tl(t0, t0);
76db3ba4
AJ
2738 tcg_gen_qemu_st32(t0, arg2, ctx->mem_idx);
2739 tcg_temp_free(t0);
76db3ba4
AJ
2740 } else {
2741 tcg_gen_qemu_st32(arg1, arg2, ctx->mem_idx);
2742 }
b61f2753
AJ
2743}
2744
f976b09e
AG
2745static void gen_qemu_st32_i64(DisasContext *ctx, TCGv_i64 val, TCGv addr)
2746{
2747 TCGv tmp = tcg_temp_new();
2748 tcg_gen_trunc_i64_tl(tmp, val);
2749 gen_qemu_st32(ctx, tmp, addr);
2750 tcg_temp_free(tmp);
2751}
2752
636aa200 2753static inline void gen_qemu_st64(DisasContext *ctx, TCGv_i64 arg1, TCGv arg2)
b61f2753 2754{
76db3ba4 2755 if (unlikely(ctx->le_mode)) {
a7812ae4 2756 TCGv_i64 t0 = tcg_temp_new_i64();
66896cb8 2757 tcg_gen_bswap64_i64(t0, arg1);
76db3ba4 2758 tcg_gen_qemu_st64(t0, arg2, ctx->mem_idx);
a7812ae4 2759 tcg_temp_free_i64(t0);
b61f2753 2760 } else
76db3ba4 2761 tcg_gen_qemu_st64(arg1, arg2, ctx->mem_idx);
b61f2753
AJ
2762}
2763
0c8aacd4 2764#define GEN_LD(name, ldop, opc, type) \
99e300ef 2765static void glue(gen_, name)(DisasContext *ctx) \
79aceca5 2766{ \
76db3ba4
AJ
2767 TCGv EA; \
2768 gen_set_access_type(ctx, ACCESS_INT); \
2769 EA = tcg_temp_new(); \
2770 gen_addr_imm_index(ctx, EA, 0); \
2771 gen_qemu_##ldop(ctx, cpu_gpr[rD(ctx->opcode)], EA); \
b61f2753 2772 tcg_temp_free(EA); \
79aceca5
FB
2773}
2774
0c8aacd4 2775#define GEN_LDU(name, ldop, opc, type) \
99e300ef 2776static void glue(gen_, name##u)(DisasContext *ctx) \
79aceca5 2777{ \
b61f2753 2778 TCGv EA; \
76a66253
JM
2779 if (unlikely(rA(ctx->opcode) == 0 || \
2780 rA(ctx->opcode) == rD(ctx->opcode))) { \
e06fcd75 2781 gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL); \
9fddaa0c 2782 return; \
9a64fbe4 2783 } \
76db3ba4 2784 gen_set_access_type(ctx, ACCESS_INT); \
0c8aacd4 2785 EA = tcg_temp_new(); \
9d53c753 2786 if (type == PPC_64B) \
76db3ba4 2787 gen_addr_imm_index(ctx, EA, 0x03); \
9d53c753 2788 else \
76db3ba4
AJ
2789 gen_addr_imm_index(ctx, EA, 0); \
2790 gen_qemu_##ldop(ctx, cpu_gpr[rD(ctx->opcode)], EA); \
b61f2753
AJ
2791 tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], EA); \
2792 tcg_temp_free(EA); \
79aceca5
FB
2793}
2794
0c8aacd4 2795#define GEN_LDUX(name, ldop, opc2, opc3, type) \
99e300ef 2796static void glue(gen_, name##ux)(DisasContext *ctx) \
79aceca5 2797{ \
b61f2753 2798 TCGv EA; \
76a66253
JM
2799 if (unlikely(rA(ctx->opcode) == 0 || \
2800 rA(ctx->opcode) == rD(ctx->opcode))) { \
e06fcd75 2801 gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL); \
9fddaa0c 2802 return; \
9a64fbe4 2803 } \
76db3ba4 2804 gen_set_access_type(ctx, ACCESS_INT); \
0c8aacd4 2805 EA = tcg_temp_new(); \
76db3ba4
AJ
2806 gen_addr_reg_index(ctx, EA); \
2807 gen_qemu_##ldop(ctx, cpu_gpr[rD(ctx->opcode)], EA); \
b61f2753
AJ
2808 tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], EA); \
2809 tcg_temp_free(EA); \
79aceca5
FB
2810}
2811
cd6e9320 2812#define GEN_LDX_E(name, ldop, opc2, opc3, type, type2) \
99e300ef 2813static void glue(gen_, name##x)(DisasContext *ctx) \
79aceca5 2814{ \
76db3ba4
AJ
2815 TCGv EA; \
2816 gen_set_access_type(ctx, ACCESS_INT); \
2817 EA = tcg_temp_new(); \
2818 gen_addr_reg_index(ctx, EA); \
2819 gen_qemu_##ldop(ctx, cpu_gpr[rD(ctx->opcode)], EA); \
b61f2753 2820 tcg_temp_free(EA); \
79aceca5 2821}
cd6e9320
TH
2822#define GEN_LDX(name, ldop, opc2, opc3, type) \
2823 GEN_LDX_E(name, ldop, opc2, opc3, type, PPC_NONE)
79aceca5 2824
0c8aacd4
AJ
2825#define GEN_LDS(name, ldop, op, type) \
2826GEN_LD(name, ldop, op | 0x20, type); \
2827GEN_LDU(name, ldop, op | 0x21, type); \
2828GEN_LDUX(name, ldop, 0x17, op | 0x01, type); \
2829GEN_LDX(name, ldop, 0x17, op | 0x00, type)
79aceca5
FB
2830
2831/* lbz lbzu lbzux lbzx */
0c8aacd4 2832GEN_LDS(lbz, ld8u, 0x02, PPC_INTEGER);
79aceca5 2833/* lha lhau lhaux lhax */
0c8aacd4 2834GEN_LDS(lha, ld16s, 0x0A, PPC_INTEGER);
79aceca5 2835/* lhz lhzu lhzux lhzx */
0c8aacd4 2836GEN_LDS(lhz, ld16u, 0x08, PPC_INTEGER);
79aceca5 2837/* lwz lwzu lwzux lwzx */
0c8aacd4 2838GEN_LDS(lwz, ld32u, 0x00, PPC_INTEGER);
d9bce9d9 2839#if defined(TARGET_PPC64)
d9bce9d9 2840/* lwaux */
0c8aacd4 2841GEN_LDUX(lwa, ld32s, 0x15, 0x0B, PPC_64B);
d9bce9d9 2842/* lwax */
0c8aacd4 2843GEN_LDX(lwa, ld32s, 0x15, 0x0A, PPC_64B);
d9bce9d9 2844/* ldux */
0c8aacd4 2845GEN_LDUX(ld, ld64, 0x15, 0x01, PPC_64B);
d9bce9d9 2846/* ldx */
0c8aacd4 2847GEN_LDX(ld, ld64, 0x15, 0x00, PPC_64B);
99e300ef
BS
2848
2849static void gen_ld(DisasContext *ctx)
d9bce9d9 2850{
b61f2753 2851 TCGv EA;
d9bce9d9
JM
2852 if (Rc(ctx->opcode)) {
2853 if (unlikely(rA(ctx->opcode) == 0 ||
2854 rA(ctx->opcode) == rD(ctx->opcode))) {
e06fcd75 2855 gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL);
d9bce9d9
JM
2856 return;
2857 }
2858 }
76db3ba4 2859 gen_set_access_type(ctx, ACCESS_INT);
a7812ae4 2860 EA = tcg_temp_new();
76db3ba4 2861 gen_addr_imm_index(ctx, EA, 0x03);
d9bce9d9
JM
2862 if (ctx->opcode & 0x02) {
2863 /* lwa (lwau is undefined) */
76db3ba4 2864 gen_qemu_ld32s(ctx, cpu_gpr[rD(ctx->opcode)], EA);
d9bce9d9
JM
2865 } else {
2866 /* ld - ldu */
76db3ba4 2867 gen_qemu_ld64(ctx, cpu_gpr[rD(ctx->opcode)], EA);
d9bce9d9 2868 }
d9bce9d9 2869 if (Rc(ctx->opcode))
b61f2753
AJ
2870 tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], EA);
2871 tcg_temp_free(EA);
d9bce9d9 2872}
99e300ef 2873
54623277 2874/* lq */
99e300ef 2875static void gen_lq(DisasContext *ctx)
be147d08 2876{
be147d08 2877 int ra, rd;
b61f2753 2878 TCGv EA;
be147d08 2879
e0498daa
TM
2880 /* lq is a legal user mode instruction starting in ISA 2.07 */
2881 bool legal_in_user_mode = (ctx->insns_flags2 & PPC2_LSQ_ISA207) != 0;
2882 bool le_is_supported = (ctx->insns_flags2 & PPC2_LSQ_ISA207) != 0;
2883
2884 if (!legal_in_user_mode && is_user_mode(ctx)) {
e06fcd75 2885 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
be147d08
JM
2886 return;
2887 }
e0498daa
TM
2888
2889 if (!le_is_supported && ctx->le_mode) {
2890 gen_exception_err(ctx, POWERPC_EXCP_ALIGN, POWERPC_EXCP_ALIGN_LE);
2891 return;
2892 }
2893
be147d08
JM
2894 ra = rA(ctx->opcode);
2895 rd = rD(ctx->opcode);
2896 if (unlikely((rd & 1) || rd == ra)) {
e06fcd75 2897 gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL);
be147d08
JM
2898 return;
2899 }
e0498daa 2900
76db3ba4 2901 gen_set_access_type(ctx, ACCESS_INT);
a7812ae4 2902 EA = tcg_temp_new();
76db3ba4 2903 gen_addr_imm_index(ctx, EA, 0x0F);
e0498daa
TM
2904
2905 if (unlikely(ctx->le_mode)) {
2906 gen_qemu_ld64(ctx, cpu_gpr[rd+1], EA);
2907 gen_addr_add(ctx, EA, EA, 8);
2908 gen_qemu_ld64(ctx, cpu_gpr[rd], EA);
2909 } else {
2910 gen_qemu_ld64(ctx, cpu_gpr[rd], EA);
2911 gen_addr_add(ctx, EA, EA, 8);
2912 gen_qemu_ld64(ctx, cpu_gpr[rd+1], EA);
2913 }
b61f2753 2914 tcg_temp_free(EA);
be147d08 2915}
d9bce9d9 2916#endif
79aceca5
FB
2917
2918/*** Integer store ***/
0c8aacd4 2919#define GEN_ST(name, stop, opc, type) \
99e300ef 2920static void glue(gen_, name)(DisasContext *ctx) \
79aceca5 2921{ \
76db3ba4
AJ
2922 TCGv EA; \
2923 gen_set_access_type(ctx, ACCESS_INT); \
2924 EA = tcg_temp_new(); \
2925 gen_addr_imm_index(ctx, EA, 0); \
2926 gen_qemu_##stop(ctx, cpu_gpr[rS(ctx->opcode)], EA); \
b61f2753 2927 tcg_temp_free(EA); \
79aceca5
FB
2928}
2929
0c8aacd4 2930#define GEN_STU(name, stop, opc, type) \
99e300ef 2931static void glue(gen_, stop##u)(DisasContext *ctx) \
79aceca5 2932{ \
b61f2753 2933 TCGv EA; \
76a66253 2934 if (unlikely(rA(ctx->opcode) == 0)) { \
e06fcd75 2935 gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL); \
9fddaa0c 2936 return; \
9a64fbe4 2937 } \
76db3ba4 2938 gen_set_access_type(ctx, ACCESS_INT); \
0c8aacd4 2939 EA = tcg_temp_new(); \
9d53c753 2940 if (type == PPC_64B) \
76db3ba4 2941 gen_addr_imm_index(ctx, EA, 0x03); \
9d53c753 2942 else \
76db3ba4
AJ
2943 gen_addr_imm_index(ctx, EA, 0); \
2944 gen_qemu_##stop(ctx, cpu_gpr[rS(ctx->opcode)], EA); \
b61f2753
AJ
2945 tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], EA); \
2946 tcg_temp_free(EA); \
79aceca5
FB
2947}
2948
0c8aacd4 2949#define GEN_STUX(name, stop, opc2, opc3, type) \
99e300ef 2950static void glue(gen_, name##ux)(DisasContext *ctx) \
79aceca5 2951{ \
b61f2753 2952 TCGv EA; \
76a66253 2953 if (unlikely(rA(ctx->opcode) == 0)) { \
e06fcd75 2954 gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL); \
9fddaa0c 2955 return; \
9a64fbe4 2956 } \
76db3ba4 2957 gen_set_access_type(ctx, ACCESS_INT); \
0c8aacd4 2958 EA = tcg_temp_new(); \
76db3ba4
AJ
2959 gen_addr_reg_index(ctx, EA); \
2960 gen_qemu_##stop(ctx, cpu_gpr[rS(ctx->opcode)], EA); \
b61f2753
AJ
2961 tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], EA); \
2962 tcg_temp_free(EA); \
79aceca5
FB
2963}
2964
cd6e9320
TH
2965#define GEN_STX_E(name, stop, opc2, opc3, type, type2) \
2966static void glue(gen_, name##x)(DisasContext *ctx) \
79aceca5 2967{ \
76db3ba4
AJ
2968 TCGv EA; \
2969 gen_set_access_type(ctx, ACCESS_INT); \
2970 EA = tcg_temp_new(); \
2971 gen_addr_reg_index(ctx, EA); \
2972 gen_qemu_##stop(ctx, cpu_gpr[rS(ctx->opcode)], EA); \
b61f2753 2973 tcg_temp_free(EA); \
79aceca5 2974}
cd6e9320
TH
2975#define GEN_STX(name, stop, opc2, opc3, type) \
2976 GEN_STX_E(name, stop, opc2, opc3, type, PPC_NONE)
79aceca5 2977
0c8aacd4
AJ
2978#define GEN_STS(name, stop, op, type) \
2979GEN_ST(name, stop, op | 0x20, type); \
2980GEN_STU(name, stop, op | 0x21, type); \
2981GEN_STUX(name, stop, 0x17, op | 0x01, type); \
2982GEN_STX(name, stop, 0x17, op | 0x00, type)
79aceca5
FB
2983
2984/* stb stbu stbux stbx */
0c8aacd4 2985GEN_STS(stb, st8, 0x06, PPC_INTEGER);
79aceca5 2986/* sth sthu sthux sthx */
0c8aacd4 2987GEN_STS(sth, st16, 0x0C, PPC_INTEGER);
79aceca5 2988/* stw stwu stwux stwx */
0c8aacd4 2989GEN_STS(stw, st32, 0x04, PPC_INTEGER);
d9bce9d9 2990#if defined(TARGET_PPC64)
0c8aacd4
AJ
2991GEN_STUX(std, st64, 0x15, 0x05, PPC_64B);
2992GEN_STX(std, st64, 0x15, 0x04, PPC_64B);
99e300ef
BS
2993
2994static void gen_std(DisasContext *ctx)
d9bce9d9 2995{
be147d08 2996 int rs;
b61f2753 2997 TCGv EA;
be147d08
JM
2998
2999 rs = rS(ctx->opcode);
84cab1e2
TM
3000 if ((ctx->opcode & 0x3) == 0x2) { /* stq */
3001
3002 bool legal_in_user_mode = (ctx->insns_flags2 & PPC2_LSQ_ISA207) != 0;
3003 bool le_is_supported = (ctx->insns_flags2 & PPC2_LSQ_ISA207) != 0;
3004
3005 if (!legal_in_user_mode && is_user_mode(ctx)) {
e06fcd75 3006 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
be147d08
JM
3007 return;
3008 }
84cab1e2
TM
3009
3010 if (!le_is_supported && ctx->le_mode) {
3011 gen_exception_err(ctx, POWERPC_EXCP_ALIGN, POWERPC_EXCP_ALIGN_LE);
d9bce9d9
JM
3012 return;
3013 }
84cab1e2
TM
3014
3015 if (unlikely(rs & 1)) {
3016 gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL);
be147d08
JM
3017 return;
3018 }
76db3ba4 3019 gen_set_access_type(ctx, ACCESS_INT);
a7812ae4 3020 EA = tcg_temp_new();
76db3ba4 3021 gen_addr_imm_index(ctx, EA, 0x03);
84cab1e2
TM
3022
3023 if (unlikely(ctx->le_mode)) {
3024 gen_qemu_st64(ctx, cpu_gpr[rs+1], EA);
3025 gen_addr_add(ctx, EA, EA, 8);
3026 gen_qemu_st64(ctx, cpu_gpr[rs], EA);
3027 } else {
3028 gen_qemu_st64(ctx, cpu_gpr[rs], EA);
3029 gen_addr_add(ctx, EA, EA, 8);
3030 gen_qemu_st64(ctx, cpu_gpr[rs+1], EA);
3031 }
b61f2753 3032 tcg_temp_free(EA);
be147d08 3033 } else {
84cab1e2 3034 /* std / stdu*/
be147d08
JM
3035 if (Rc(ctx->opcode)) {
3036 if (unlikely(rA(ctx->opcode) == 0)) {
e06fcd75 3037 gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL);
be147d08
JM
3038 return;
3039 }
3040 }
76db3ba4 3041 gen_set_access_type(ctx, ACCESS_INT);
a7812ae4 3042 EA = tcg_temp_new();
76db3ba4
AJ
3043 gen_addr_imm_index(ctx, EA, 0x03);
3044 gen_qemu_st64(ctx, cpu_gpr[rs], EA);
be147d08 3045 if (Rc(ctx->opcode))
b61f2753
AJ
3046 tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], EA);
3047 tcg_temp_free(EA);
d9bce9d9 3048 }
d9bce9d9
JM
3049}
3050#endif
79aceca5
FB
3051/*** Integer load and store with byte reverse ***/
3052/* lhbrx */
86178a57 3053static inline void gen_qemu_ld16ur(DisasContext *ctx, TCGv arg1, TCGv arg2)
b61f2753 3054{
76db3ba4
AJ
3055 tcg_gen_qemu_ld16u(arg1, arg2, ctx->mem_idx);
3056 if (likely(!ctx->le_mode)) {
fa3966a3 3057 tcg_gen_bswap16_tl(arg1, arg1);
76db3ba4 3058 }
b61f2753 3059}
0c8aacd4 3060GEN_LDX(lhbr, ld16ur, 0x16, 0x18, PPC_INTEGER);
b61f2753 3061
79aceca5 3062/* lwbrx */
86178a57 3063static inline void gen_qemu_ld32ur(DisasContext *ctx, TCGv arg1, TCGv arg2)
b61f2753 3064{
76db3ba4
AJ
3065 tcg_gen_qemu_ld32u(arg1, arg2, ctx->mem_idx);
3066 if (likely(!ctx->le_mode)) {
fa3966a3 3067 tcg_gen_bswap32_tl(arg1, arg1);
76db3ba4 3068 }
b61f2753 3069}
0c8aacd4 3070GEN_LDX(lwbr, ld32ur, 0x16, 0x10, PPC_INTEGER);
b61f2753 3071
cd6e9320
TH
3072#if defined(TARGET_PPC64)
3073/* ldbrx */
3074static inline void gen_qemu_ld64ur(DisasContext *ctx, TCGv arg1, TCGv arg2)
3075{
3076 tcg_gen_qemu_ld64(arg1, arg2, ctx->mem_idx);
3077 if (likely(!ctx->le_mode)) {
3078 tcg_gen_bswap64_tl(arg1, arg1);
3079 }
3080}
3081GEN_LDX_E(ldbr, ld64ur, 0x14, 0x10, PPC_NONE, PPC2_DBRX);
3082#endif /* TARGET_PPC64 */
3083
79aceca5 3084/* sthbrx */
86178a57 3085static inline void gen_qemu_st16r(DisasContext *ctx, TCGv arg1, TCGv arg2)
b61f2753 3086{
76db3ba4 3087 if (likely(!ctx->le_mode)) {
76db3ba4
AJ
3088 TCGv t0 = tcg_temp_new();
3089 tcg_gen_ext16u_tl(t0, arg1);
fa3966a3 3090 tcg_gen_bswap16_tl(t0, t0);
76db3ba4
AJ
3091 tcg_gen_qemu_st16(t0, arg2, ctx->mem_idx);
3092 tcg_temp_free(t0);
76db3ba4
AJ
3093 } else {
3094 tcg_gen_qemu_st16(arg1, arg2, ctx->mem_idx);
3095 }
b61f2753 3096}
0c8aacd4 3097GEN_STX(sthbr, st16r, 0x16, 0x1C, PPC_INTEGER);
b61f2753 3098
79aceca5 3099/* stwbrx */
86178a57 3100static inline void gen_qemu_st32r(DisasContext *ctx, TCGv arg1, TCGv arg2)
b61f2753 3101{
76db3ba4 3102 if (likely(!ctx->le_mode)) {
fa3966a3
AJ
3103 TCGv t0 = tcg_temp_new();
3104 tcg_gen_ext32u_tl(t0, arg1);
3105 tcg_gen_bswap32_tl(t0, t0);
76db3ba4
AJ
3106 tcg_gen_qemu_st32(t0, arg2, ctx->mem_idx);
3107 tcg_temp_free(t0);
76db3ba4
AJ
3108 } else {
3109 tcg_gen_qemu_st32(arg1, arg2, ctx->mem_idx);
3110 }
b61f2753 3111}
0c8aacd4 3112GEN_STX(stwbr, st32r, 0x16, 0x14, PPC_INTEGER);
79aceca5 3113
cd6e9320
TH
3114#if defined(TARGET_PPC64)
3115/* stdbrx */
3116static inline void gen_qemu_st64r(DisasContext *ctx, TCGv arg1, TCGv arg2)
3117{
3118 if (likely(!ctx->le_mode)) {
3119 TCGv t0 = tcg_temp_new();
3120 tcg_gen_bswap64_tl(t0, arg1);
3121 tcg_gen_qemu_st64(t0, arg2, ctx->mem_idx);
3122 tcg_temp_free(t0);
3123 } else {
3124 tcg_gen_qemu_st64(arg1, arg2, ctx->mem_idx);
3125 }
3126}
3127GEN_STX_E(stdbr, st64r, 0x14, 0x14, PPC_NONE, PPC2_DBRX);
3128#endif /* TARGET_PPC64 */
3129
79aceca5 3130/*** Integer load and store multiple ***/
99e300ef 3131
54623277 3132/* lmw */
99e300ef 3133static void gen_lmw(DisasContext *ctx)
79aceca5 3134{
76db3ba4
AJ
3135 TCGv t0;
3136 TCGv_i32 t1;
3137 gen_set_access_type(ctx, ACCESS_INT);
76a66253 3138 /* NIP cannot be restored if the memory exception comes from an helper */
d9bce9d9 3139 gen_update_nip(ctx, ctx->nip - 4);
76db3ba4
AJ
3140 t0 = tcg_temp_new();
3141 t1 = tcg_const_i32(rD(ctx->opcode));
3142 gen_addr_imm_index(ctx, t0, 0);
2f5a189c 3143 gen_helper_lmw(cpu_env, t0, t1);
ff4a62cd
AJ
3144 tcg_temp_free(t0);
3145 tcg_temp_free_i32(t1);
79aceca5
FB
3146}
3147
3148/* stmw */
99e300ef 3149static void gen_stmw(DisasContext *ctx)
79aceca5 3150{
76db3ba4
AJ
3151 TCGv t0;
3152 TCGv_i32 t1;
3153 gen_set_access_type(ctx, ACCESS_INT);
76a66253 3154 /* NIP cannot be restored if the memory exception comes from an helper */
d9bce9d9 3155 gen_update_nip(ctx, ctx->nip - 4);
76db3ba4
AJ
3156 t0 = tcg_temp_new();
3157 t1 = tcg_const_i32(rS(ctx->opcode));
3158 gen_addr_imm_index(ctx, t0, 0);
2f5a189c 3159 gen_helper_stmw(cpu_env, t0, t1);
ff4a62cd
AJ
3160 tcg_temp_free(t0);
3161 tcg_temp_free_i32(t1);
79aceca5
FB
3162}
3163
3164/*** Integer load and store strings ***/
54623277 3165
79aceca5 3166/* lswi */
3fc6c082 3167/* PowerPC32 specification says we must generate an exception if
9a64fbe4
FB
3168 * rA is in the range of registers to be loaded.
3169 * In an other hand, IBM says this is valid, but rA won't be loaded.
3170 * For now, I'll follow the spec...
3171 */
99e300ef 3172static void gen_lswi(DisasContext *ctx)
79aceca5 3173{
dfbc799d
AJ
3174 TCGv t0;
3175 TCGv_i32 t1, t2;
79aceca5
FB
3176 int nb = NB(ctx->opcode);
3177 int start = rD(ctx->opcode);
9a64fbe4 3178 int ra = rA(ctx->opcode);
79aceca5
FB
3179 int nr;
3180
3181 if (nb == 0)
3182 nb = 32;
3183 nr = nb / 4;
76a66253
JM
3184 if (unlikely(((start + nr) > 32 &&
3185 start <= ra && (start + nr - 32) > ra) ||
3186 ((start + nr) <= 32 && start <= ra && (start + nr) > ra))) {
e06fcd75 3187 gen_inval_exception(ctx, POWERPC_EXCP_INVAL_LSWX);
9fddaa0c 3188 return;
297d8e62 3189 }
76db3ba4 3190 gen_set_access_type(ctx, ACCESS_INT);
8dd4983c 3191 /* NIP cannot be restored if the memory exception comes from an helper */
d9bce9d9 3192 gen_update_nip(ctx, ctx->nip - 4);
dfbc799d 3193 t0 = tcg_temp_new();
76db3ba4 3194 gen_addr_register(ctx, t0);
dfbc799d
AJ
3195 t1 = tcg_const_i32(nb);
3196 t2 = tcg_const_i32(start);
2f5a189c 3197 gen_helper_lsw(cpu_env, t0, t1, t2);
dfbc799d
AJ
3198 tcg_temp_free(t0);
3199 tcg_temp_free_i32(t1);
3200 tcg_temp_free_i32(t2);
79aceca5
FB
3201}
3202
3203/* lswx */
99e300ef 3204static void gen_lswx(DisasContext *ctx)
79aceca5 3205{
76db3ba4
AJ
3206 TCGv t0;
3207 TCGv_i32 t1, t2, t3;
3208 gen_set_access_type(ctx, ACCESS_INT);
76a66253 3209 /* NIP cannot be restored if the memory exception comes from an helper */
d9bce9d9 3210 gen_update_nip(ctx, ctx->nip - 4);
76db3ba4
AJ
3211 t0 = tcg_temp_new();
3212 gen_addr_reg_index(ctx, t0);
3213 t1 = tcg_const_i32(rD(ctx->opcode));
3214 t2 = tcg_const_i32(rA(ctx->opcode));
3215 t3 = tcg_const_i32(rB(ctx->opcode));
2f5a189c 3216 gen_helper_lswx(cpu_env, t0, t1, t2, t3);
dfbc799d
AJ
3217 tcg_temp_free(t0);
3218 tcg_temp_free_i32(t1);
3219 tcg_temp_free_i32(t2);
3220 tcg_temp_free_i32(t3);
79aceca5
FB
3221}
3222
3223/* stswi */
99e300ef 3224static void gen_stswi(DisasContext *ctx)
79aceca5 3225{
76db3ba4
AJ
3226 TCGv t0;
3227 TCGv_i32 t1, t2;
4b3686fa 3228 int nb = NB(ctx->opcode);
76db3ba4 3229 gen_set_access_type(ctx, ACCESS_INT);
76a66253 3230 /* NIP cannot be restored if the memory exception comes from an helper */
d9bce9d9 3231 gen_update_nip(ctx, ctx->nip - 4);
76db3ba4
AJ
3232 t0 = tcg_temp_new();
3233 gen_addr_register(ctx, t0);
4b3686fa
FB
3234 if (nb == 0)
3235 nb = 32;
dfbc799d 3236 t1 = tcg_const_i32(nb);
76db3ba4 3237 t2 = tcg_const_i32(rS(ctx->opcode));
2f5a189c 3238 gen_helper_stsw(cpu_env, t0, t1, t2);
dfbc799d
AJ
3239 tcg_temp_free(t0);
3240 tcg_temp_free_i32(t1);
3241 tcg_temp_free_i32(t2);
79aceca5
FB
3242}
3243
3244/* stswx */
99e300ef 3245static void gen_stswx(DisasContext *ctx)
79aceca5 3246{
76db3ba4
AJ
3247 TCGv t0;
3248 TCGv_i32 t1, t2;
3249 gen_set_access_type(ctx, ACCESS_INT);
8dd4983c 3250 /* NIP cannot be restored if the memory exception comes from an helper */
5fafdf24 3251 gen_update_nip(ctx, ctx->nip - 4);
76db3ba4
AJ
3252 t0 = tcg_temp_new();
3253 gen_addr_reg_index(ctx, t0);
3254 t1 = tcg_temp_new_i32();
dfbc799d
AJ
3255 tcg_gen_trunc_tl_i32(t1, cpu_xer);
3256 tcg_gen_andi_i32(t1, t1, 0x7F);
76db3ba4 3257 t2 = tcg_const_i32(rS(ctx->opcode));
2f5a189c 3258 gen_helper_stsw(cpu_env, t0, t1, t2);
dfbc799d
AJ
3259 tcg_temp_free(t0);
3260 tcg_temp_free_i32(t1);
3261 tcg_temp_free_i32(t2);
79aceca5
FB
3262}
3263
3264/*** Memory synchronisation ***/
3265/* eieio */
99e300ef 3266static void gen_eieio(DisasContext *ctx)
79aceca5 3267{
79aceca5
FB
3268}
3269
3270/* isync */
99e300ef 3271static void gen_isync(DisasContext *ctx)
79aceca5 3272{
e06fcd75 3273 gen_stop_exception(ctx);
79aceca5
FB
3274}
3275
5c77a786
TM
3276#define LARX(name, len, loadop) \
3277static void gen_##name(DisasContext *ctx) \
3278{ \
3279 TCGv t0; \
3280 TCGv gpr = cpu_gpr[rD(ctx->opcode)]; \
3281 gen_set_access_type(ctx, ACCESS_RES); \
3282 t0 = tcg_temp_local_new(); \
3283 gen_addr_reg_index(ctx, t0); \
3284 if ((len) > 1) { \
3285 gen_check_align(ctx, t0, (len)-1); \
3286 } \
3287 gen_qemu_##loadop(ctx, gpr, t0); \
3288 tcg_gen_mov_tl(cpu_reserve, t0); \
3289 tcg_gen_st_tl(gpr, cpu_env, offsetof(CPUPPCState, reserve_val)); \
3290 tcg_temp_free(t0); \
79aceca5
FB
3291}
3292
5c77a786
TM
3293/* lwarx */
3294LARX(lbarx, 1, ld8u);
3295LARX(lharx, 2, ld16u);
3296LARX(lwarx, 4, ld32u);
3297
3298
4425265b 3299#if defined(CONFIG_USER_ONLY)
587c51f7
TM
3300static void gen_conditional_store(DisasContext *ctx, TCGv EA,
3301 int reg, int size)
4425265b
NF
3302{
3303 TCGv t0 = tcg_temp_new();
3304 uint32_t save_exception = ctx->exception;
3305
1328c2bf 3306 tcg_gen_st_tl(EA, cpu_env, offsetof(CPUPPCState, reserve_ea));
4425265b 3307 tcg_gen_movi_tl(t0, (size << 5) | reg);
1328c2bf 3308 tcg_gen_st_tl(t0, cpu_env, offsetof(CPUPPCState, reserve_info));
4425265b
NF
3309 tcg_temp_free(t0);
3310 gen_update_nip(ctx, ctx->nip-4);
3311 ctx->exception = POWERPC_EXCP_BRANCH;
3312 gen_exception(ctx, POWERPC_EXCP_STCX);
3313 ctx->exception = save_exception;
3314}
4425265b 3315#else
587c51f7
TM
3316static void gen_conditional_store(DisasContext *ctx, TCGv EA,
3317 int reg, int size)
3318{
3319 int l1;
4425265b 3320
587c51f7
TM
3321 tcg_gen_trunc_tl_i32(cpu_crf[0], cpu_so);
3322 l1 = gen_new_label();
3323 tcg_gen_brcond_tl(TCG_COND_NE, EA, cpu_reserve, l1);
3324 tcg_gen_ori_i32(cpu_crf[0], cpu_crf[0], 1 << CRF_EQ);
3325#if defined(TARGET_PPC64)
3326 if (size == 8) {
3327 gen_qemu_st64(ctx, cpu_gpr[reg], EA);
3328 } else
3329#endif
3330 if (size == 4) {
3331 gen_qemu_st32(ctx, cpu_gpr[reg], EA);
3332 } else if (size == 2) {
3333 gen_qemu_st16(ctx, cpu_gpr[reg], EA);
27b95bfe
TM
3334#if defined(TARGET_PPC64)
3335 } else if (size == 16) {
3336 TCGv gpr1, gpr2;
3337 if (unlikely(ctx->le_mode)) {
3338 gpr1 = cpu_gpr[reg+1];
3339 gpr2 = cpu_gpr[reg];
3340 } else {
3341 gpr1 = cpu_gpr[reg];
3342 gpr2 = cpu_gpr[reg+1];
3343 }
3344 gen_qemu_st64(ctx, gpr1, EA);
3345 gen_addr_add(ctx, EA, EA, 8);
3346 gen_qemu_st64(ctx, gpr2, EA);
3347#endif
587c51f7
TM
3348 } else {
3349 gen_qemu_st8(ctx, cpu_gpr[reg], EA);
4425265b 3350 }
587c51f7
TM
3351 gen_set_label(l1);
3352 tcg_gen_movi_tl(cpu_reserve, -1);
3353}
4425265b 3354#endif
587c51f7
TM
3355
3356#define STCX(name, len) \
3357static void gen_##name(DisasContext *ctx) \
3358{ \
3359 TCGv t0; \
27b95bfe
TM
3360 if (unlikely((len == 16) && (rD(ctx->opcode) & 1))) { \
3361 gen_inval_exception(ctx, \
3362 POWERPC_EXCP_INVAL_INVAL); \
3363 return; \
3364 } \
587c51f7
TM
3365 gen_set_access_type(ctx, ACCESS_RES); \
3366 t0 = tcg_temp_local_new(); \
3367 gen_addr_reg_index(ctx, t0); \
3368 if (len > 1) { \
3369 gen_check_align(ctx, t0, (len)-1); \
3370 } \
3371 gen_conditional_store(ctx, t0, rS(ctx->opcode), len); \
3372 tcg_temp_free(t0); \
79aceca5
FB
3373}
3374
587c51f7
TM
3375STCX(stbcx_, 1);
3376STCX(sthcx_, 2);
3377STCX(stwcx_, 4);
3378
426613db 3379#if defined(TARGET_PPC64)
426613db 3380/* ldarx */
5c77a786 3381LARX(ldarx, 8, ld64);
426613db 3382
9c294d5a
TM
3383/* lqarx */
3384static void gen_lqarx(DisasContext *ctx)
3385{
3386 TCGv EA;
3387 int rd = rD(ctx->opcode);
3388 TCGv gpr1, gpr2;
3389
3390 if (unlikely((rd & 1) || (rd == rA(ctx->opcode)) ||
3391 (rd == rB(ctx->opcode)))) {
3392 gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL);
3393 return;
3394 }
3395
3396 gen_set_access_type(ctx, ACCESS_RES);
3397 EA = tcg_temp_local_new();
3398 gen_addr_reg_index(ctx, EA);
3399 gen_check_align(ctx, EA, 15);
3400 if (unlikely(ctx->le_mode)) {
3401 gpr1 = cpu_gpr[rd+1];
3402 gpr2 = cpu_gpr[rd];
3403 } else {
3404 gpr1 = cpu_gpr[rd];
3405 gpr2 = cpu_gpr[rd+1];
3406 }
3407 gen_qemu_ld64(ctx, gpr1, EA);
3408 tcg_gen_mov_tl(cpu_reserve, EA);
3409
3410 gen_addr_add(ctx, EA, EA, 8);
3411 gen_qemu_ld64(ctx, gpr2, EA);
3412
3413 tcg_gen_st_tl(gpr1, cpu_env, offsetof(CPUPPCState, reserve_val));
3414 tcg_gen_st_tl(gpr2, cpu_env, offsetof(CPUPPCState, reserve_val2));
3415
3416 tcg_temp_free(EA);
3417}
3418
426613db 3419/* stdcx. */
587c51f7 3420STCX(stdcx_, 8);
27b95bfe 3421STCX(stqcx_, 16);
426613db
JM
3422#endif /* defined(TARGET_PPC64) */
3423
79aceca5 3424/* sync */
99e300ef 3425static void gen_sync(DisasContext *ctx)
79aceca5 3426{
79aceca5
FB
3427}
3428
0db1b20e 3429/* wait */
99e300ef 3430static void gen_wait(DisasContext *ctx)
0db1b20e 3431{
931ff272 3432 TCGv_i32 t0 = tcg_temp_new_i32();
259186a7
AF
3433 tcg_gen_st_i32(t0, cpu_env,
3434 -offsetof(PowerPCCPU, env) + offsetof(CPUState, halted));
931ff272 3435 tcg_temp_free_i32(t0);
0db1b20e 3436 /* Stop translation, as the CPU is supposed to sleep from now */
e06fcd75 3437 gen_exception_err(ctx, EXCP_HLT, 1);
0db1b20e
JM
3438}
3439
79aceca5 3440/*** Floating-point load ***/
a0d7d5a7 3441#define GEN_LDF(name, ldop, opc, type) \
99e300ef 3442static void glue(gen_, name)(DisasContext *ctx) \
79aceca5 3443{ \
a0d7d5a7 3444 TCGv EA; \
76a66253 3445 if (unlikely(!ctx->fpu_enabled)) { \
e06fcd75 3446 gen_exception(ctx, POWERPC_EXCP_FPU); \
4ecc3190
FB
3447 return; \
3448 } \
76db3ba4 3449 gen_set_access_type(ctx, ACCESS_FLOAT); \
a0d7d5a7 3450 EA = tcg_temp_new(); \
76db3ba4
AJ
3451 gen_addr_imm_index(ctx, EA, 0); \
3452 gen_qemu_##ldop(ctx, cpu_fpr[rD(ctx->opcode)], EA); \
a0d7d5a7 3453 tcg_temp_free(EA); \
79aceca5
FB
3454}
3455
a0d7d5a7 3456#define GEN_LDUF(name, ldop, opc, type) \
99e300ef 3457static void glue(gen_, name##u)(DisasContext *ctx) \
79aceca5 3458{ \
a0d7d5a7 3459 TCGv EA; \
76a66253 3460 if (unlikely(!ctx->fpu_enabled)) { \
e06fcd75 3461 gen_exception(ctx, POWERPC_EXCP_FPU); \
4ecc3190
FB
3462 return; \
3463 } \
76a66253 3464 if (unlikely(rA(ctx->opcode) == 0)) { \
e06fcd75 3465 gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL); \
9fddaa0c 3466 return; \
9a64fbe4 3467 } \
76db3ba4 3468 gen_set_access_type(ctx, ACCESS_FLOAT); \
a0d7d5a7 3469 EA = tcg_temp_new(); \
76db3ba4
AJ
3470 gen_addr_imm_index(ctx, EA, 0); \
3471 gen_qemu_##ldop(ctx, cpu_fpr[rD(ctx->opcode)], EA); \
a0d7d5a7
AJ
3472 tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], EA); \
3473 tcg_temp_free(EA); \
79aceca5
FB
3474}
3475
a0d7d5a7 3476#define GEN_LDUXF(name, ldop, opc, type) \
99e300ef 3477static void glue(gen_, name##ux)(DisasContext *ctx) \
79aceca5 3478{ \
a0d7d5a7 3479 TCGv EA; \
76a66253 3480 if (unlikely(!ctx->fpu_enabled)) { \
e06fcd75 3481 gen_exception(ctx, POWERPC_EXCP_FPU); \
4ecc3190
FB
3482 return; \
3483 } \
76a66253 3484 if (unlikely(rA(ctx->opcode) == 0)) { \
e06fcd75 3485 gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL); \
9fddaa0c 3486 return; \
9a64fbe4 3487 } \
76db3ba4 3488 gen_set_access_type(ctx, ACCESS_FLOAT); \
a0d7d5a7 3489 EA = tcg_temp_new(); \
76db3ba4
AJ
3490 gen_addr_reg_index(ctx, EA); \
3491 gen_qemu_##ldop(ctx, cpu_fpr[rD(ctx->opcode)], EA); \
a0d7d5a7
AJ
3492 tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], EA); \
3493 tcg_temp_free(EA); \
79aceca5
FB
3494}
3495
a0d7d5a7 3496#define GEN_LDXF(name, ldop, opc2, opc3, type) \
99e300ef 3497static void glue(gen_, name##x)(DisasContext *ctx) \
79aceca5 3498{ \
a0d7d5a7 3499 TCGv EA; \
76a66253 3500 if (unlikely(!ctx->fpu_enabled)) { \
e06fcd75 3501 gen_exception(ctx, POWERPC_EXCP_FPU); \
4ecc3190
FB
3502 return; \
3503 } \
76db3ba4 3504 gen_set_access_type(ctx, ACCESS_FLOAT); \
a0d7d5a7 3505 EA = tcg_temp_new(); \
76db3ba4
AJ
3506 gen_addr_reg_index(ctx, EA); \
3507 gen_qemu_##ldop(ctx, cpu_fpr[rD(ctx->opcode)], EA); \
a0d7d5a7 3508 tcg_temp_free(EA); \
79aceca5
FB
3509}
3510
a0d7d5a7
AJ
3511#define GEN_LDFS(name, ldop, op, type) \
3512GEN_LDF(name, ldop, op | 0x20, type); \
3513GEN_LDUF(name, ldop, op | 0x21, type); \
3514GEN_LDUXF(name, ldop, op | 0x01, type); \
3515GEN_LDXF(name, ldop, 0x17, op | 0x00, type)
3516
636aa200 3517static inline void gen_qemu_ld32fs(DisasContext *ctx, TCGv_i64 arg1, TCGv arg2)
a0d7d5a7
AJ
3518{
3519 TCGv t0 = tcg_temp_new();
3520 TCGv_i32 t1 = tcg_temp_new_i32();
76db3ba4 3521 gen_qemu_ld32u(ctx, t0, arg2);
a0d7d5a7
AJ
3522 tcg_gen_trunc_tl_i32(t1, t0);
3523 tcg_temp_free(t0);
8e703949 3524 gen_helper_float32_to_float64(arg1, cpu_env, t1);
a0d7d5a7
AJ
3525 tcg_temp_free_i32(t1);
3526}
79aceca5 3527
a0d7d5a7
AJ
3528 /* lfd lfdu lfdux lfdx */
3529GEN_LDFS(lfd, ld64, 0x12, PPC_FLOAT);
3530 /* lfs lfsu lfsux lfsx */
3531GEN_LDFS(lfs, ld32fs, 0x10, PPC_FLOAT);
79aceca5 3532
05050ee8
AJ
3533/* lfdp */
3534static void gen_lfdp(DisasContext *ctx)
3535{
3536 TCGv EA;
3537 if (unlikely(!ctx->fpu_enabled)) {
3538 gen_exception(ctx, POWERPC_EXCP_FPU);
3539 return;
3540 }
3541 gen_set_access_type(ctx, ACCESS_FLOAT);
3542 EA = tcg_temp_new();
3543 gen_addr_imm_index(ctx, EA, 0); \
3544 if (unlikely(ctx->le_mode)) {
3545 gen_qemu_ld64(ctx, cpu_fpr[rD(ctx->opcode) + 1], EA);
3546 tcg_gen_addi_tl(EA, EA, 8);
3547 gen_qemu_ld64(ctx, cpu_fpr[rD(ctx->opcode)], EA);
3548 } else {
3549 gen_qemu_ld64(ctx, cpu_fpr[rD(ctx->opcode)], EA);
3550 tcg_gen_addi_tl(EA, EA, 8);
3551 gen_qemu_ld64(ctx, cpu_fpr[rD(ctx->opcode) + 1], EA);
3552 }
3553 tcg_temp_free(EA);
3554}
3555
3556/* lfdpx */
3557static void gen_lfdpx(DisasContext *ctx)
3558{
3559 TCGv EA;
3560 if (unlikely(!ctx->fpu_enabled)) {
3561 gen_exception(ctx, POWERPC_EXCP_FPU);
3562 return;
3563 }
3564 gen_set_access_type(ctx, ACCESS_FLOAT);
3565 EA = tcg_temp_new();
3566 gen_addr_reg_index(ctx, EA);
3567 if (unlikely(ctx->le_mode)) {
3568 gen_qemu_ld64(ctx, cpu_fpr[rD(ctx->opcode) + 1], EA);
3569 tcg_gen_addi_tl(EA, EA, 8);
3570 gen_qemu_ld64(ctx, cpu_fpr[rD(ctx->opcode)], EA);
3571 } else {
3572 gen_qemu_ld64(ctx, cpu_fpr[rD(ctx->opcode)], EA);
3573 tcg_gen_addi_tl(EA, EA, 8);
3574 gen_qemu_ld64(ctx, cpu_fpr[rD(ctx->opcode) + 1], EA);
3575 }
3576 tcg_temp_free(EA);
3577}
3578
199f830d
AJ
3579/* lfiwax */
3580static void gen_lfiwax(DisasContext *ctx)
3581{
3582 TCGv EA;
3583 TCGv t0;
3584 if (unlikely(!ctx->fpu_enabled)) {
3585 gen_exception(ctx, POWERPC_EXCP_FPU);
3586 return;
3587 }
3588 gen_set_access_type(ctx, ACCESS_FLOAT);
3589 EA = tcg_temp_new();
3590 t0 = tcg_temp_new();
3591 gen_addr_reg_index(ctx, EA);
909eedb7 3592 gen_qemu_ld32s(ctx, t0, EA);
199f830d 3593 tcg_gen_ext_tl_i64(cpu_fpr[rD(ctx->opcode)], t0);
199f830d
AJ
3594 tcg_temp_free(EA);
3595 tcg_temp_free(t0);
3596}
3597
66c3e328
TM
3598/* lfiwzx */
3599static void gen_lfiwzx(DisasContext *ctx)
3600{
3601 TCGv EA;
3602 if (unlikely(!ctx->fpu_enabled)) {
3603 gen_exception(ctx, POWERPC_EXCP_FPU);
3604 return;
3605 }
3606 gen_set_access_type(ctx, ACCESS_FLOAT);
3607 EA = tcg_temp_new();
3608 gen_addr_reg_index(ctx, EA);
3609 gen_qemu_ld32u_i64(ctx, cpu_fpr[rD(ctx->opcode)], EA);
3610 tcg_temp_free(EA);
3611}
79aceca5 3612/*** Floating-point store ***/
a0d7d5a7 3613#define GEN_STF(name, stop, opc, type) \
99e300ef 3614static void glue(gen_, name)(DisasContext *ctx) \
79aceca5 3615{ \
a0d7d5a7 3616 TCGv EA; \
76a66253 3617 if (unlikely(!ctx->fpu_enabled)) { \
e06fcd75 3618 gen_exception(ctx, POWERPC_EXCP_FPU); \
4ecc3190
FB
3619 return; \
3620 } \
76db3ba4 3621 gen_set_access_type(ctx, ACCESS_FLOAT); \
a0d7d5a7 3622 EA = tcg_temp_new(); \
76db3ba4
AJ
3623 gen_addr_imm_index(ctx, EA, 0); \
3624 gen_qemu_##stop(ctx, cpu_fpr[rS(ctx->opcode)], EA); \
a0d7d5a7 3625 tcg_temp_free(EA); \
79aceca5
FB
3626}
3627
a0d7d5a7 3628#define GEN_STUF(name, stop, opc, type) \
99e300ef 3629static void glue(gen_, name##u)(DisasContext *ctx) \
79aceca5 3630{ \
a0d7d5a7 3631 TCGv EA; \
76a66253 3632 if (unlikely(!ctx->fpu_enabled)) { \
e06fcd75 3633 gen_exception(ctx, POWERPC_EXCP_FPU); \
4ecc3190
FB
3634 return; \
3635 } \
76a66253 3636 if (unlikely(rA(ctx->opcode) == 0)) { \
e06fcd75 3637 gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL); \
9fddaa0c 3638 return; \
9a64fbe4 3639 } \
76db3ba4 3640 gen_set_access_type(ctx, ACCESS_FLOAT); \
a0d7d5a7 3641 EA = tcg_temp_new(); \
76db3ba4
AJ
3642 gen_addr_imm_index(ctx, EA, 0); \
3643 gen_qemu_##stop(ctx, cpu_fpr[rS(ctx->opcode)], EA); \
a0d7d5a7
AJ
3644 tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], EA); \
3645 tcg_temp_free(EA); \
79aceca5
FB
3646}
3647
a0d7d5a7 3648#define GEN_STUXF(name, stop, opc, type) \
99e300ef 3649static void glue(gen_, name##ux)(DisasContext *ctx) \
79aceca5 3650{ \
a0d7d5a7 3651 TCGv EA; \
76a66253 3652 if (unlikely(!ctx->fpu_enabled)) { \
e06fcd75 3653 gen_exception(ctx, POWERPC_EXCP_FPU); \
4ecc3190
FB
3654 return; \
3655 } \
76a66253 3656 if (unlikely(rA(ctx->opcode) == 0)) { \
e06fcd75 3657 gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL); \
9fddaa0c 3658 return; \
9a64fbe4 3659 } \
76db3ba4 3660 gen_set_access_type(ctx, ACCESS_FLOAT); \
a0d7d5a7 3661 EA = tcg_temp_new(); \
76db3ba4
AJ
3662 gen_addr_reg_index(ctx, EA); \
3663 gen_qemu_##stop(ctx, cpu_fpr[rS(ctx->opcode)], EA); \
a0d7d5a7
AJ
3664 tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], EA); \
3665 tcg_temp_free(EA); \
79aceca5
FB
3666}
3667
a0d7d5a7 3668#define GEN_STXF(name, stop, opc2, opc3, type) \
99e300ef 3669static void glue(gen_, name##x)(DisasContext *ctx) \
79aceca5 3670{ \
a0d7d5a7 3671 TCGv EA; \
76a66253 3672 if (unlikely(!ctx->fpu_enabled)) { \
e06fcd75 3673 gen_exception(ctx, POWERPC_EXCP_FPU); \
4ecc3190
FB
3674 return; \
3675 } \
76db3ba4 3676 gen_set_access_type(ctx, ACCESS_FLOAT); \
a0d7d5a7 3677 EA = tcg_temp_new(); \
76db3ba4
AJ
3678 gen_addr_reg_index(ctx, EA); \
3679 gen_qemu_##stop(ctx, cpu_fpr[rS(ctx->opcode)], EA); \
a0d7d5a7 3680 tcg_temp_free(EA); \
79aceca5
FB
3681}
3682
a0d7d5a7
AJ
3683#define GEN_STFS(name, stop, op, type) \
3684GEN_STF(name, stop, op | 0x20, type); \
3685GEN_STUF(name, stop, op | 0x21, type); \
3686GEN_STUXF(name, stop, op | 0x01, type); \
3687GEN_STXF(name, stop, 0x17, op | 0x00, type)
3688
636aa200 3689static inline void gen_qemu_st32fs(DisasContext *ctx, TCGv_i64 arg1, TCGv arg2)
a0d7d5a7
AJ
3690{
3691 TCGv_i32 t0 = tcg_temp_new_i32();
3692 TCGv t1 = tcg_temp_new();
8e703949 3693 gen_helper_float64_to_float32(t0, cpu_env, arg1);
a0d7d5a7
AJ
3694 tcg_gen_extu_i32_tl(t1, t0);
3695 tcg_temp_free_i32(t0);
76db3ba4 3696 gen_qemu_st32(ctx, t1, arg2);
a0d7d5a7
AJ
3697 tcg_temp_free(t1);
3698}
79aceca5
FB
3699
3700/* stfd stfdu stfdux stfdx */
a0d7d5a7 3701GEN_STFS(stfd, st64, 0x16, PPC_FLOAT);
79aceca5 3702/* stfs stfsu stfsux stfsx */
a0d7d5a7 3703GEN_STFS(stfs, st32fs, 0x14, PPC_FLOAT);
79aceca5 3704
44bc0c4d
AJ
3705/* stfdp */
3706static void gen_stfdp(DisasContext *ctx)
3707{
3708 TCGv EA;
3709 if (unlikely(!ctx->fpu_enabled)) {
3710 gen_exception(ctx, POWERPC_EXCP_FPU);
3711 return;
3712 }
3713 gen_set_access_type(ctx, ACCESS_FLOAT);
3714 EA = tcg_temp_new();
3715 gen_addr_imm_index(ctx, EA, 0); \
3716 if (unlikely(ctx->le_mode)) {
3717 gen_qemu_st64(ctx, cpu_fpr[rD(ctx->opcode) + 1], EA);
3718 tcg_gen_addi_tl(EA, EA, 8);
3719 gen_qemu_st64(ctx, cpu_fpr[rD(ctx->opcode)], EA);
3720 } else {
3721 gen_qemu_st64(ctx, cpu_fpr[rD(ctx->opcode)], EA);
3722 tcg_gen_addi_tl(EA, EA, 8);
3723 gen_qemu_st64(ctx, cpu_fpr[rD(ctx->opcode) + 1], EA);
3724 }
3725 tcg_temp_free(EA);
3726}
3727
3728/* stfdpx */
3729static void gen_stfdpx(DisasContext *ctx)
3730{
3731 TCGv EA;
3732 if (unlikely(!ctx->fpu_enabled)) {
3733 gen_exception(ctx, POWERPC_EXCP_FPU);
3734 return;
3735 }
3736 gen_set_access_type(ctx, ACCESS_FLOAT);
3737 EA = tcg_temp_new();
3738 gen_addr_reg_index(ctx, EA);
3739 if (unlikely(ctx->le_mode)) {
3740 gen_qemu_st64(ctx, cpu_fpr[rD(ctx->opcode) + 1], EA);
3741 tcg_gen_addi_tl(EA, EA, 8);
3742 gen_qemu_st64(ctx, cpu_fpr[rD(ctx->opcode)], EA);
3743 } else {
3744 gen_qemu_st64(ctx, cpu_fpr[rD(ctx->opcode)], EA);
3745 tcg_gen_addi_tl(EA, EA, 8);
3746 gen_qemu_st64(ctx, cpu_fpr[rD(ctx->opcode) + 1], EA);
3747 }
3748 tcg_temp_free(EA);
3749}
3750
79aceca5 3751/* Optional: */
636aa200 3752static inline void gen_qemu_st32fiw(DisasContext *ctx, TCGv_i64 arg1, TCGv arg2)
a0d7d5a7
AJ
3753{
3754 TCGv t0 = tcg_temp_new();
3755 tcg_gen_trunc_i64_tl(t0, arg1),
76db3ba4 3756 gen_qemu_st32(ctx, t0, arg2);
a0d7d5a7
AJ
3757 tcg_temp_free(t0);
3758}
79aceca5 3759/* stfiwx */
a0d7d5a7 3760GEN_STXF(stfiw, st32fiw, 0x17, 0x1E, PPC_FLOAT_STFIWX);
79aceca5 3761
697ab892
DG
3762static inline void gen_update_cfar(DisasContext *ctx, target_ulong nip)
3763{
3764#if defined(TARGET_PPC64)
3765 if (ctx->has_cfar)
3766 tcg_gen_movi_tl(cpu_cfar, nip);
3767#endif
3768}
3769
79aceca5 3770/*** Branch ***/
636aa200 3771static inline void gen_goto_tb(DisasContext *ctx, int n, target_ulong dest)
c1942362
FB
3772{
3773 TranslationBlock *tb;
3774 tb = ctx->tb;
e0c8f9ce 3775 if (NARROW_MODE(ctx)) {
a2ffb812 3776 dest = (uint32_t) dest;
e0c8f9ce 3777 }
57fec1fe 3778 if ((tb->pc & TARGET_PAGE_MASK) == (dest & TARGET_PAGE_MASK) &&
8cbcb4fa 3779 likely(!ctx->singlestep_enabled)) {
57fec1fe 3780 tcg_gen_goto_tb(n);
a2ffb812 3781 tcg_gen_movi_tl(cpu_nip, dest & ~3);
8cfd0495 3782 tcg_gen_exit_tb((uintptr_t)tb + n);
c1942362 3783 } else {
a2ffb812 3784 tcg_gen_movi_tl(cpu_nip, dest & ~3);
8cbcb4fa
AJ
3785 if (unlikely(ctx->singlestep_enabled)) {
3786 if ((ctx->singlestep_enabled &
bdc4e053 3787 (CPU_BRANCH_STEP | CPU_SINGLE_STEP)) &&
f0cc4aa8
JG
3788 (ctx->exception == POWERPC_EXCP_BRANCH ||
3789 ctx->exception == POWERPC_EXCP_TRACE)) {
8cbcb4fa
AJ
3790 target_ulong tmp = ctx->nip;
3791 ctx->nip = dest;
e06fcd75 3792 gen_exception(ctx, POWERPC_EXCP_TRACE);
8cbcb4fa
AJ
3793 ctx->nip = tmp;
3794 }
3795 if (ctx->singlestep_enabled & GDBSTUB_SINGLE_STEP) {
e06fcd75 3796 gen_debug_exception(ctx);
8cbcb4fa
AJ
3797 }
3798 }
57fec1fe 3799 tcg_gen_exit_tb(0);
c1942362 3800 }
c53be334
FB
3801}
3802
636aa200 3803static inline void gen_setlr(DisasContext *ctx, target_ulong nip)
e1833e1f 3804{
e0c8f9ce
RH
3805 if (NARROW_MODE(ctx)) {
3806 nip = (uint32_t)nip;
3807 }
3808 tcg_gen_movi_tl(cpu_lr, nip);
e1833e1f
JM
3809}
3810
79aceca5 3811/* b ba bl bla */
99e300ef 3812static void gen_b(DisasContext *ctx)
79aceca5 3813{
76a66253 3814 target_ulong li, target;
38a64f9d 3815
8cbcb4fa 3816 ctx->exception = POWERPC_EXCP_BRANCH;
38a64f9d 3817 /* sign extend LI */
e0c8f9ce
RH
3818 li = LI(ctx->opcode);
3819 li = (li ^ 0x02000000) - 0x02000000;
3820 if (likely(AA(ctx->opcode) == 0)) {
046d6672 3821 target = ctx->nip + li - 4;
e0c8f9ce 3822 } else {
9a64fbe4 3823 target = li;
e0c8f9ce
RH
3824 }
3825 if (LK(ctx->opcode)) {
e1833e1f 3826 gen_setlr(ctx, ctx->nip);
e0c8f9ce 3827 }
697ab892 3828 gen_update_cfar(ctx, ctx->nip);
c1942362 3829 gen_goto_tb(ctx, 0, target);
79aceca5
FB
3830}
3831
e98a6e40
FB
3832#define BCOND_IM 0
3833#define BCOND_LR 1
3834#define BCOND_CTR 2
52a4984d 3835#define BCOND_TAR 3
e98a6e40 3836
636aa200 3837static inline void gen_bcond(DisasContext *ctx, int type)
d9bce9d9 3838{
d9bce9d9 3839 uint32_t bo = BO(ctx->opcode);
05f92404 3840 int l1;
a2ffb812 3841 TCGv target;
e98a6e40 3842
8cbcb4fa 3843 ctx->exception = POWERPC_EXCP_BRANCH;
52a4984d 3844 if (type == BCOND_LR || type == BCOND_CTR || type == BCOND_TAR) {
a7812ae4 3845 target = tcg_temp_local_new();
a2ffb812
AJ
3846 if (type == BCOND_CTR)
3847 tcg_gen_mov_tl(target, cpu_ctr);
52a4984d
TM
3848 else if (type == BCOND_TAR)
3849 gen_load_spr(target, SPR_TAR);
a2ffb812
AJ
3850 else
3851 tcg_gen_mov_tl(target, cpu_lr);
d2e9fd8f 3852 } else {
3853 TCGV_UNUSED(target);
e98a6e40 3854 }
e1833e1f
JM
3855 if (LK(ctx->opcode))
3856 gen_setlr(ctx, ctx->nip);
a2ffb812
AJ
3857 l1 = gen_new_label();
3858 if ((bo & 0x4) == 0) {
3859 /* Decrement and test CTR */
a7812ae4 3860 TCGv temp = tcg_temp_new();
a2ffb812 3861 if (unlikely(type == BCOND_CTR)) {
e06fcd75 3862 gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL);
a2ffb812
AJ
3863 return;
3864 }
3865 tcg_gen_subi_tl(cpu_ctr, cpu_ctr, 1);
e0c8f9ce 3866 if (NARROW_MODE(ctx)) {
a2ffb812 3867 tcg_gen_ext32u_tl(temp, cpu_ctr);
e0c8f9ce 3868 } else {
a2ffb812 3869 tcg_gen_mov_tl(temp, cpu_ctr);
e0c8f9ce 3870 }
a2ffb812
AJ
3871 if (bo & 0x2) {
3872 tcg_gen_brcondi_tl(TCG_COND_NE, temp, 0, l1);
3873 } else {
3874 tcg_gen_brcondi_tl(TCG_COND_EQ, temp, 0, l1);
e98a6e40 3875 }
a7812ae4 3876 tcg_temp_free(temp);
a2ffb812
AJ
3877 }
3878 if ((bo & 0x10) == 0) {
3879 /* Test CR */
3880 uint32_t bi = BI(ctx->opcode);
3881 uint32_t mask = 1 << (3 - (bi & 0x03));
a7812ae4 3882 TCGv_i32 temp = tcg_temp_new_i32();
a2ffb812 3883
d9bce9d9 3884 if (bo & 0x8) {
a2ffb812
AJ
3885 tcg_gen_andi_i32(temp, cpu_crf[bi >> 2], mask);
3886 tcg_gen_brcondi_i32(TCG_COND_EQ, temp, 0, l1);
d9bce9d9 3887 } else {
a2ffb812
AJ
3888 tcg_gen_andi_i32(temp, cpu_crf[bi >> 2], mask);
3889 tcg_gen_brcondi_i32(TCG_COND_NE, temp, 0, l1);
d9bce9d9 3890 }
a7812ae4 3891 tcg_temp_free_i32(temp);
d9bce9d9 3892 }
697ab892 3893 gen_update_cfar(ctx, ctx->nip);
e98a6e40 3894 if (type == BCOND_IM) {
a2ffb812
AJ
3895 target_ulong li = (target_long)((int16_t)(BD(ctx->opcode)));
3896 if (likely(AA(ctx->opcode) == 0)) {
3897 gen_goto_tb(ctx, 0, ctx->nip + li - 4);
3898 } else {
3899 gen_goto_tb(ctx, 0, li);
3900 }
c53be334 3901 gen_set_label(l1);
c1942362 3902 gen_goto_tb(ctx, 1, ctx->nip);
e98a6e40 3903 } else {
e0c8f9ce 3904 if (NARROW_MODE(ctx)) {
a2ffb812 3905 tcg_gen_andi_tl(cpu_nip, target, (uint32_t)~3);
e0c8f9ce 3906 } else {
a2ffb812 3907 tcg_gen_andi_tl(cpu_nip, target, ~3);
e0c8f9ce 3908 }
a2ffb812
AJ
3909 tcg_gen_exit_tb(0);
3910 gen_set_label(l1);
e0c8f9ce 3911 gen_update_nip(ctx, ctx->nip);
57fec1fe 3912 tcg_gen_exit_tb(0);
08e46e54 3913 }
e98a6e40
FB
3914}
3915
99e300ef 3916static void gen_bc(DisasContext *ctx)
3b46e624 3917{
e98a6e40
FB
3918 gen_bcond(ctx, BCOND_IM);
3919}
3920
99e300ef 3921static void gen_bcctr(DisasContext *ctx)
3b46e624 3922{
e98a6e40
FB
3923 gen_bcond(ctx, BCOND_CTR);
3924}
3925
99e300ef 3926static void gen_bclr(DisasContext *ctx)
3b46e624 3927{
e98a6e40
FB
3928 gen_bcond(ctx, BCOND_LR);
3929}
79aceca5 3930
52a4984d
TM
3931static void gen_bctar(DisasContext *ctx)
3932{
3933 gen_bcond(ctx, BCOND_TAR);
3934}
3935
79aceca5 3936/*** Condition register logical ***/
e1571908 3937#define GEN_CRLOGIC(name, tcg_op, opc) \
99e300ef 3938static void glue(gen_, name)(DisasContext *ctx) \
79aceca5 3939{ \
fc0d441e
JM
3940 uint8_t bitmask; \
3941 int sh; \
a7812ae4 3942 TCGv_i32 t0, t1; \
fc0d441e 3943 sh = (crbD(ctx->opcode) & 0x03) - (crbA(ctx->opcode) & 0x03); \
a7812ae4 3944 t0 = tcg_temp_new_i32(); \
fc0d441e 3945 if (sh > 0) \
fea0c503 3946 tcg_gen_shri_i32(t0, cpu_crf[crbA(ctx->opcode) >> 2], sh); \
fc0d441e 3947 else if (sh < 0) \
fea0c503 3948 tcg_gen_shli_i32(t0, cpu_crf[crbA(ctx->opcode) >> 2], -sh); \
e1571908 3949 else \
fea0c503 3950 tcg_gen_mov_i32(t0, cpu_crf[crbA(ctx->opcode) >> 2]); \
a7812ae4 3951 t1 = tcg_temp_new_i32(); \
fc0d441e
JM
3952 sh = (crbD(ctx->opcode) & 0x03) - (crbB(ctx->opcode) & 0x03); \
3953 if (sh > 0) \
fea0c503 3954 tcg_gen_shri_i32(t1, cpu_crf[crbB(ctx->opcode) >> 2], sh); \
fc0d441e 3955 else if (sh < 0) \
fea0c503 3956 tcg_gen_shli_i32(t1, cpu_crf[crbB(ctx->opcode) >> 2], -sh); \
e1571908 3957 else \
fea0c503
AJ
3958 tcg_gen_mov_i32(t1, cpu_crf[crbB(ctx->opcode) >> 2]); \
3959 tcg_op(t0, t0, t1); \
fc0d441e 3960 bitmask = 1 << (3 - (crbD(ctx->opcode) & 0x03)); \
fea0c503
AJ
3961 tcg_gen_andi_i32(t0, t0, bitmask); \
3962 tcg_gen_andi_i32(t1, cpu_crf[crbD(ctx->opcode) >> 2], ~bitmask); \
3963 tcg_gen_or_i32(cpu_crf[crbD(ctx->opcode) >> 2], t0, t1); \
a7812ae4
PB
3964 tcg_temp_free_i32(t0); \
3965 tcg_temp_free_i32(t1); \
79aceca5
FB
3966}
3967
3968/* crand */
e1571908 3969GEN_CRLOGIC(crand, tcg_gen_and_i32, 0x08);
79aceca5 3970/* crandc */
e1571908 3971GEN_CRLOGIC(crandc, tcg_gen_andc_i32, 0x04);
79aceca5 3972/* creqv */
e1571908 3973GEN_CRLOGIC(creqv, tcg_gen_eqv_i32, 0x09);
79aceca5 3974/* crnand */
e1571908 3975GEN_CRLOGIC(crnand, tcg_gen_nand_i32, 0x07);
79aceca5 3976/* crnor */
e1571908 3977GEN_CRLOGIC(crnor, tcg_gen_nor_i32, 0x01);
79aceca5 3978/* cror */
e1571908 3979GEN_CRLOGIC(cror, tcg_gen_or_i32, 0x0E);
79aceca5 3980/* crorc */
e1571908 3981GEN_CRLOGIC(crorc, tcg_gen_orc_i32, 0x0D);
79aceca5 3982/* crxor */
e1571908 3983GEN_CRLOGIC(crxor, tcg_gen_xor_i32, 0x06);
99e300ef 3984
54623277 3985/* mcrf */
99e300ef 3986static void gen_mcrf(DisasContext *ctx)
79aceca5 3987{
47e4661c 3988 tcg_gen_mov_i32(cpu_crf[crfD(ctx->opcode)], cpu_crf[crfS(ctx->opcode)]);
79aceca5
FB
3989}
3990
3991/*** System linkage ***/
99e300ef 3992
54623277 3993/* rfi (mem_idx only) */
99e300ef 3994static void gen_rfi(DisasContext *ctx)
79aceca5 3995{
9a64fbe4 3996#if defined(CONFIG_USER_ONLY)
e06fcd75 3997 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
9a64fbe4
FB
3998#else
3999 /* Restore CPU state */
76db3ba4 4000 if (unlikely(!ctx->mem_idx)) {
e06fcd75 4001 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
9fddaa0c 4002 return;
9a64fbe4 4003 }
697ab892 4004 gen_update_cfar(ctx, ctx->nip);
e5f17ac6 4005 gen_helper_rfi(cpu_env);
e06fcd75 4006 gen_sync_exception(ctx);
9a64fbe4 4007#endif
79aceca5
FB
4008}
4009
426613db 4010#if defined(TARGET_PPC64)
99e300ef 4011static void gen_rfid(DisasContext *ctx)
426613db
JM
4012{
4013#if defined(CONFIG_USER_ONLY)
e06fcd75 4014 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
426613db
JM
4015#else
4016 /* Restore CPU state */
76db3ba4 4017 if (unlikely(!ctx->mem_idx)) {
e06fcd75 4018 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
426613db
JM
4019 return;
4020 }
697ab892 4021 gen_update_cfar(ctx, ctx->nip);
e5f17ac6 4022 gen_helper_rfid(cpu_env);
e06fcd75 4023 gen_sync_exception(ctx);
426613db
JM
4024#endif
4025}
426613db 4026
99e300ef 4027static void gen_hrfid(DisasContext *ctx)
be147d08
JM
4028{
4029#if defined(CONFIG_USER_ONLY)
e06fcd75 4030 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
be147d08
JM
4031#else
4032 /* Restore CPU state */
76db3ba4 4033 if (unlikely(ctx->mem_idx <= 1)) {
e06fcd75 4034 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
be147d08
JM
4035 return;
4036 }
e5f17ac6 4037 gen_helper_hrfid(cpu_env);
e06fcd75 4038 gen_sync_exception(ctx);
be147d08
JM
4039#endif
4040}
4041#endif
4042
79aceca5 4043/* sc */
417bf010
JM
4044#if defined(CONFIG_USER_ONLY)
4045#define POWERPC_SYSCALL POWERPC_EXCP_SYSCALL_USER
4046#else
4047#define POWERPC_SYSCALL POWERPC_EXCP_SYSCALL
4048#endif
99e300ef 4049static void gen_sc(DisasContext *ctx)
79aceca5 4050{
e1833e1f
JM
4051 uint32_t lev;
4052
4053 lev = (ctx->opcode >> 5) & 0x7F;
e06fcd75 4054 gen_exception_err(ctx, POWERPC_SYSCALL, lev);
79aceca5
FB
4055}
4056
4057/*** Trap ***/
99e300ef 4058
54623277 4059/* tw */
99e300ef 4060static void gen_tw(DisasContext *ctx)
79aceca5 4061{
cab3bee2 4062 TCGv_i32 t0 = tcg_const_i32(TO(ctx->opcode));
db9a231d
AJ
4063 /* Update the nip since this might generate a trap exception */
4064 gen_update_nip(ctx, ctx->nip);
e5f17ac6
BS
4065 gen_helper_tw(cpu_env, cpu_gpr[rA(ctx->opcode)], cpu_gpr[rB(ctx->opcode)],
4066 t0);
cab3bee2 4067 tcg_temp_free_i32(t0);
79aceca5
FB
4068}
4069
4070/* twi */
99e300ef 4071static void gen_twi(DisasContext *ctx)
79aceca5 4072{
cab3bee2
AJ
4073 TCGv t0 = tcg_const_tl(SIMM(ctx->opcode));
4074 TCGv_i32 t1 = tcg_const_i32(TO(ctx->opcode));
db9a231d
AJ
4075 /* Update the nip since this might generate a trap exception */
4076 gen_update_nip(ctx, ctx->nip);
e5f17ac6 4077 gen_helper_tw(cpu_env, cpu_gpr[rA(ctx->opcode)], t0, t1);
cab3bee2
AJ
4078 tcg_temp_free(t0);
4079 tcg_temp_free_i32(t1);
79aceca5
FB
4080}
4081
d9bce9d9
JM
4082#if defined(TARGET_PPC64)
4083/* td */
99e300ef 4084static void gen_td(DisasContext *ctx)
d9bce9d9 4085{
cab3bee2 4086 TCGv_i32 t0 = tcg_const_i32(TO(ctx->opcode));
db9a231d
AJ
4087 /* Update the nip since this might generate a trap exception */
4088 gen_update_nip(ctx, ctx->nip);
e5f17ac6
BS
4089 gen_helper_td(cpu_env, cpu_gpr[rA(ctx->opcode)], cpu_gpr[rB(ctx->opcode)],
4090 t0);
cab3bee2 4091 tcg_temp_free_i32(t0);
d9bce9d9
JM
4092}
4093
4094/* tdi */
99e300ef 4095static void gen_tdi(DisasContext *ctx)
d9bce9d9 4096{
cab3bee2
AJ
4097 TCGv t0 = tcg_const_tl(SIMM(ctx->opcode));
4098 TCGv_i32 t1 = tcg_const_i32(TO(ctx->opcode));
db9a231d
AJ
4099 /* Update the nip since this might generate a trap exception */
4100 gen_update_nip(ctx, ctx->nip);
e5f17ac6 4101 gen_helper_td(cpu_env, cpu_gpr[rA(ctx->opcode)], t0, t1);
cab3bee2
AJ
4102 tcg_temp_free(t0);
4103 tcg_temp_free_i32(t1);
d9bce9d9
JM
4104}
4105#endif
4106
79aceca5 4107/*** Processor control ***/
99e300ef 4108
da91a00f
RH
4109static void gen_read_xer(TCGv dst)
4110{
4111 TCGv t0 = tcg_temp_new();
4112 TCGv t1 = tcg_temp_new();
4113 TCGv t2 = tcg_temp_new();
4114 tcg_gen_mov_tl(dst, cpu_xer);
4115 tcg_gen_shli_tl(t0, cpu_so, XER_SO);
4116 tcg_gen_shli_tl(t1, cpu_ov, XER_OV);
4117 tcg_gen_shli_tl(t2, cpu_ca, XER_CA);
4118 tcg_gen_or_tl(t0, t0, t1);
4119 tcg_gen_or_tl(dst, dst, t2);
4120 tcg_gen_or_tl(dst, dst, t0);
4121 tcg_temp_free(t0);
4122 tcg_temp_free(t1);
4123 tcg_temp_free(t2);
4124}
4125
4126static void gen_write_xer(TCGv src)
4127{
4128 tcg_gen_andi_tl(cpu_xer, src,
4129 ~((1u << XER_SO) | (1u << XER_OV) | (1u << XER_CA)));
4130 tcg_gen_shri_tl(cpu_so, src, XER_SO);
4131 tcg_gen_shri_tl(cpu_ov, src, XER_OV);
4132 tcg_gen_shri_tl(cpu_ca, src, XER_CA);
4133 tcg_gen_andi_tl(cpu_so, cpu_so, 1);
4134 tcg_gen_andi_tl(cpu_ov, cpu_ov, 1);
4135 tcg_gen_andi_tl(cpu_ca, cpu_ca, 1);
4136}
4137
54623277 4138/* mcrxr */
99e300ef 4139static void gen_mcrxr(DisasContext *ctx)
79aceca5 4140{
da91a00f
RH
4141 TCGv_i32 t0 = tcg_temp_new_i32();
4142 TCGv_i32 t1 = tcg_temp_new_i32();
4143 TCGv_i32 dst = cpu_crf[crfD(ctx->opcode)];
4144
4145 tcg_gen_trunc_tl_i32(t0, cpu_so);
4146 tcg_gen_trunc_tl_i32(t1, cpu_ov);
4147 tcg_gen_trunc_tl_i32(dst, cpu_ca);
4148 tcg_gen_shri_i32(t0, t0, 2);
4149 tcg_gen_shri_i32(t1, t1, 1);
4150 tcg_gen_or_i32(dst, dst, t0);
4151 tcg_gen_or_i32(dst, dst, t1);
4152 tcg_temp_free_i32(t0);
4153 tcg_temp_free_i32(t1);
4154
4155 tcg_gen_movi_tl(cpu_so, 0);
4156 tcg_gen_movi_tl(cpu_ov, 0);
4157 tcg_gen_movi_tl(cpu_ca, 0);
79aceca5
FB
4158}
4159
0cfe11ea 4160/* mfcr mfocrf */
99e300ef 4161static void gen_mfcr(DisasContext *ctx)
79aceca5 4162{
76a66253 4163 uint32_t crm, crn;
3b46e624 4164
76a66253
JM
4165 if (likely(ctx->opcode & 0x00100000)) {
4166 crm = CRM(ctx->opcode);
8dd640e4 4167 if (likely(crm && ((crm & (crm - 1)) == 0))) {
0cfe11ea 4168 crn = ctz32 (crm);
e1571908 4169 tcg_gen_extu_i32_tl(cpu_gpr[rD(ctx->opcode)], cpu_crf[7 - crn]);
0497d2f4
AJ
4170 tcg_gen_shli_tl(cpu_gpr[rD(ctx->opcode)],
4171 cpu_gpr[rD(ctx->opcode)], crn * 4);
76a66253 4172 }
d9bce9d9 4173 } else {
651721b2
AJ
4174 TCGv_i32 t0 = tcg_temp_new_i32();
4175 tcg_gen_mov_i32(t0, cpu_crf[0]);
4176 tcg_gen_shli_i32(t0, t0, 4);
4177 tcg_gen_or_i32(t0, t0, cpu_crf[1]);
4178 tcg_gen_shli_i32(t0, t0, 4);
4179 tcg_gen_or_i32(t0, t0, cpu_crf[2]);
4180 tcg_gen_shli_i32(t0, t0, 4);
4181 tcg_gen_or_i32(t0, t0, cpu_crf[3]);
4182 tcg_gen_shli_i32(t0, t0, 4);
4183 tcg_gen_or_i32(t0, t0, cpu_crf[4]);
4184 tcg_gen_shli_i32(t0, t0, 4);
4185 tcg_gen_or_i32(t0, t0, cpu_crf[5]);
4186 tcg_gen_shli_i32(t0, t0, 4);
4187 tcg_gen_or_i32(t0, t0, cpu_crf[6]);
4188 tcg_gen_shli_i32(t0, t0, 4);
4189 tcg_gen_or_i32(t0, t0, cpu_crf[7]);
4190 tcg_gen_extu_i32_tl(cpu_gpr[rD(ctx->opcode)], t0);
4191 tcg_temp_free_i32(t0);
d9bce9d9 4192 }
79aceca5
FB
4193}
4194
4195/* mfmsr */
99e300ef 4196static void gen_mfmsr(DisasContext *ctx)
79aceca5 4197{
9a64fbe4 4198#if defined(CONFIG_USER_ONLY)
e06fcd75 4199 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
9a64fbe4 4200#else
76db3ba4 4201 if (unlikely(!ctx->mem_idx)) {
e06fcd75 4202 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
9fddaa0c 4203 return;
9a64fbe4 4204 }
6527f6ea 4205 tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_msr);
9a64fbe4 4206#endif
79aceca5
FB
4207}
4208
7b13448f 4209static void spr_noaccess(void *opaque, int gprn, int sprn)
3fc6c082 4210{
7b13448f 4211#if 0
3fc6c082
FB
4212 sprn = ((sprn >> 5) & 0x1F) | ((sprn & 0x1F) << 5);
4213 printf("ERROR: try to access SPR %d !\n", sprn);
7b13448f 4214#endif
3fc6c082
FB
4215}
4216#define SPR_NOACCESS (&spr_noaccess)
3fc6c082 4217
79aceca5 4218/* mfspr */
636aa200 4219static inline void gen_op_mfspr(DisasContext *ctx)
79aceca5 4220{
45d827d2 4221 void (*read_cb)(void *opaque, int gprn, int sprn);
79aceca5
FB
4222 uint32_t sprn = SPR(ctx->opcode);
4223
3fc6c082 4224#if !defined(CONFIG_USER_ONLY)
76db3ba4 4225 if (ctx->mem_idx == 2)
be147d08 4226 read_cb = ctx->spr_cb[sprn].hea_read;
76db3ba4 4227 else if (ctx->mem_idx)
3fc6c082
FB
4228 read_cb = ctx->spr_cb[sprn].oea_read;
4229 else
9a64fbe4 4230#endif
3fc6c082 4231 read_cb = ctx->spr_cb[sprn].uea_read;
76a66253
JM
4232 if (likely(read_cb != NULL)) {
4233 if (likely(read_cb != SPR_NOACCESS)) {
45d827d2 4234 (*read_cb)(ctx, rD(ctx->opcode), sprn);
3fc6c082
FB
4235 } else {
4236 /* Privilege exception */
9fceefa7
JM
4237 /* This is a hack to avoid warnings when running Linux:
4238 * this OS breaks the PowerPC virtualisation model,
4239 * allowing userland application to read the PVR
4240 */
4241 if (sprn != SPR_PVR) {
c05541ee
AB
4242 qemu_log("Trying to read privileged spr %d (0x%03x) at "
4243 TARGET_FMT_lx "\n", sprn, sprn, ctx->nip - 4);
4244 printf("Trying to read privileged spr %d (0x%03x) at "
4245 TARGET_FMT_lx "\n", sprn, sprn, ctx->nip - 4);
f24e5695 4246 }
e06fcd75 4247 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
79aceca5 4248 }
3fc6c082
FB
4249 } else {
4250 /* Not defined */
c05541ee
AB
4251 qemu_log("Trying to read invalid spr %d (0x%03x) at "
4252 TARGET_FMT_lx "\n", sprn, sprn, ctx->nip - 4);
4253 printf("Trying to read invalid spr %d (0x%03x) at "
4254 TARGET_FMT_lx "\n", sprn, sprn, ctx->nip - 4);
e06fcd75 4255 gen_inval_exception(ctx, POWERPC_EXCP_INVAL_SPR);
79aceca5 4256 }
79aceca5
FB
4257}
4258
99e300ef 4259static void gen_mfspr(DisasContext *ctx)
79aceca5 4260{
3fc6c082 4261 gen_op_mfspr(ctx);
76a66253 4262}
3fc6c082
FB
4263
4264/* mftb */
99e300ef 4265static void gen_mftb(DisasContext *ctx)
3fc6c082
FB
4266{
4267 gen_op_mfspr(ctx);
79aceca5
FB
4268}
4269
0cfe11ea 4270/* mtcrf mtocrf*/
99e300ef 4271static void gen_mtcrf(DisasContext *ctx)
79aceca5 4272{
76a66253 4273 uint32_t crm, crn;
3b46e624 4274
76a66253 4275 crm = CRM(ctx->opcode);
8dd640e4 4276 if (likely((ctx->opcode & 0x00100000))) {
4277 if (crm && ((crm & (crm - 1)) == 0)) {
4278 TCGv_i32 temp = tcg_temp_new_i32();
0cfe11ea 4279 crn = ctz32 (crm);
8dd640e4 4280 tcg_gen_trunc_tl_i32(temp, cpu_gpr[rS(ctx->opcode)]);
0cfe11ea
AJ
4281 tcg_gen_shri_i32(temp, temp, crn * 4);
4282 tcg_gen_andi_i32(cpu_crf[7 - crn], temp, 0xf);
8dd640e4 4283 tcg_temp_free_i32(temp);
4284 }
76a66253 4285 } else {
651721b2
AJ
4286 TCGv_i32 temp = tcg_temp_new_i32();
4287 tcg_gen_trunc_tl_i32(temp, cpu_gpr[rS(ctx->opcode)]);
4288 for (crn = 0 ; crn < 8 ; crn++) {
4289 if (crm & (1 << crn)) {
4290 tcg_gen_shri_i32(cpu_crf[7 - crn], temp, crn * 4);
4291 tcg_gen_andi_i32(cpu_crf[7 - crn], cpu_crf[7 - crn], 0xf);
4292 }
4293 }
a7812ae4 4294 tcg_temp_free_i32(temp);
76a66253 4295 }
79aceca5
FB
4296}
4297
4298/* mtmsr */
426613db 4299#if defined(TARGET_PPC64)
99e300ef 4300static void gen_mtmsrd(DisasContext *ctx)
426613db
JM
4301{
4302#if defined(CONFIG_USER_ONLY)
e06fcd75 4303 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
426613db 4304#else
76db3ba4 4305 if (unlikely(!ctx->mem_idx)) {
e06fcd75 4306 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
426613db
JM
4307 return;
4308 }
be147d08
JM
4309 if (ctx->opcode & 0x00010000) {
4310 /* Special form that does not need any synchronisation */
6527f6ea
AJ
4311 TCGv t0 = tcg_temp_new();
4312 tcg_gen_andi_tl(t0, cpu_gpr[rS(ctx->opcode)], (1 << MSR_RI) | (1 << MSR_EE));
4313 tcg_gen_andi_tl(cpu_msr, cpu_msr, ~((1 << MSR_RI) | (1 << MSR_EE)));
4314 tcg_gen_or_tl(cpu_msr, cpu_msr, t0);
4315 tcg_temp_free(t0);
be147d08 4316 } else {
056b05f8
JM
4317 /* XXX: we need to update nip before the store
4318 * if we enter power saving mode, we will exit the loop
4319 * directly from ppc_store_msr
4320 */
be147d08 4321 gen_update_nip(ctx, ctx->nip);
e5f17ac6 4322 gen_helper_store_msr(cpu_env, cpu_gpr[rS(ctx->opcode)]);
be147d08
JM
4323 /* Must stop the translation as machine state (may have) changed */
4324 /* Note that mtmsr is not always defined as context-synchronizing */
e06fcd75 4325 gen_stop_exception(ctx);
be147d08 4326 }
426613db
JM
4327#endif
4328}
4329#endif
4330
99e300ef 4331static void gen_mtmsr(DisasContext *ctx)
79aceca5 4332{
9a64fbe4 4333#if defined(CONFIG_USER_ONLY)
e06fcd75 4334 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
9a64fbe4 4335#else
76db3ba4 4336 if (unlikely(!ctx->mem_idx)) {
e06fcd75 4337 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
9fddaa0c 4338 return;
9a64fbe4 4339 }
be147d08
JM
4340 if (ctx->opcode & 0x00010000) {
4341 /* Special form that does not need any synchronisation */
6527f6ea
AJ
4342 TCGv t0 = tcg_temp_new();
4343 tcg_gen_andi_tl(t0, cpu_gpr[rS(ctx->opcode)], (1 << MSR_RI) | (1 << MSR_EE));
4344 tcg_gen_andi_tl(cpu_msr, cpu_msr, ~((1 << MSR_RI) | (1 << MSR_EE)));
4345 tcg_gen_or_tl(cpu_msr, cpu_msr, t0);
4346 tcg_temp_free(t0);
be147d08 4347 } else {
8018dc63
AG
4348 TCGv msr = tcg_temp_new();
4349
056b05f8
JM
4350 /* XXX: we need to update nip before the store
4351 * if we enter power saving mode, we will exit the loop
4352 * directly from ppc_store_msr
4353 */
be147d08 4354 gen_update_nip(ctx, ctx->nip);
d9bce9d9 4355#if defined(TARGET_PPC64)
8018dc63
AG
4356 tcg_gen_deposit_tl(msr, cpu_msr, cpu_gpr[rS(ctx->opcode)], 0, 32);
4357#else
4358 tcg_gen_mov_tl(msr, cpu_gpr[rS(ctx->opcode)]);
d9bce9d9 4359#endif
e5f17ac6 4360 gen_helper_store_msr(cpu_env, msr);
be147d08 4361 /* Must stop the translation as machine state (may have) changed */
6527f6ea 4362 /* Note that mtmsr is not always defined as context-synchronizing */
e06fcd75 4363 gen_stop_exception(ctx);
be147d08 4364 }
9a64fbe4 4365#endif
79aceca5
FB
4366}
4367
4368/* mtspr */
99e300ef 4369static void gen_mtspr(DisasContext *ctx)
79aceca5 4370{
45d827d2 4371 void (*write_cb)(void *opaque, int sprn, int gprn);
79aceca5
FB
4372 uint32_t sprn = SPR(ctx->opcode);
4373
3fc6c082 4374#if !defined(CONFIG_USER_ONLY)
76db3ba4 4375 if (ctx->mem_idx == 2)
be147d08 4376 write_cb = ctx->spr_cb[sprn].hea_write;
76db3ba4 4377 else if (ctx->mem_idx)
3fc6c082
FB
4378 write_cb = ctx->spr_cb[sprn].oea_write;
4379 else
9a64fbe4 4380#endif
3fc6c082 4381 write_cb = ctx->spr_cb[sprn].uea_write;
76a66253
JM
4382 if (likely(write_cb != NULL)) {
4383 if (likely(write_cb != SPR_NOACCESS)) {
45d827d2 4384 (*write_cb)(ctx, sprn, rS(ctx->opcode));
3fc6c082
FB
4385 } else {
4386 /* Privilege exception */
c05541ee
AB
4387 qemu_log("Trying to write privileged spr %d (0x%03x) at "
4388 TARGET_FMT_lx "\n", sprn, sprn, ctx->nip - 4);
4389 printf("Trying to write privileged spr %d (0x%03x) at "
4390 TARGET_FMT_lx "\n", sprn, sprn, ctx->nip - 4);
e06fcd75 4391 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
76a66253 4392 }
3fc6c082
FB
4393 } else {
4394 /* Not defined */
c05541ee
AB
4395 qemu_log("Trying to write invalid spr %d (0x%03x) at "
4396 TARGET_FMT_lx "\n", sprn, sprn, ctx->nip - 4);
4397 printf("Trying to write invalid spr %d (0x%03x) at "
4398 TARGET_FMT_lx "\n", sprn, sprn, ctx->nip - 4);
e06fcd75 4399 gen_inval_exception(ctx, POWERPC_EXCP_INVAL_SPR);
79aceca5 4400 }
79aceca5
FB
4401}
4402
4403/*** Cache management ***/
99e300ef 4404
54623277 4405/* dcbf */
99e300ef 4406static void gen_dcbf(DisasContext *ctx)
79aceca5 4407{
dac454af 4408 /* XXX: specification says this is treated as a load by the MMU */
76db3ba4
AJ
4409 TCGv t0;
4410 gen_set_access_type(ctx, ACCESS_CACHE);
4411 t0 = tcg_temp_new();
4412 gen_addr_reg_index(ctx, t0);
4413 gen_qemu_ld8u(ctx, t0, t0);
fea0c503 4414 tcg_temp_free(t0);
79aceca5
FB
4415}
4416
4417/* dcbi (Supervisor only) */
99e300ef 4418static void gen_dcbi(DisasContext *ctx)
79aceca5 4419{
a541f297 4420#if defined(CONFIG_USER_ONLY)
e06fcd75 4421 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
a541f297 4422#else
b61f2753 4423 TCGv EA, val;
76db3ba4 4424 if (unlikely(!ctx->mem_idx)) {
e06fcd75 4425 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
9fddaa0c 4426 return;
9a64fbe4 4427 }
a7812ae4 4428 EA = tcg_temp_new();
76db3ba4
AJ
4429 gen_set_access_type(ctx, ACCESS_CACHE);
4430 gen_addr_reg_index(ctx, EA);
a7812ae4 4431 val = tcg_temp_new();
76a66253 4432 /* XXX: specification says this should be treated as a store by the MMU */
76db3ba4
AJ
4433 gen_qemu_ld8u(ctx, val, EA);
4434 gen_qemu_st8(ctx, val, EA);
b61f2753
AJ
4435 tcg_temp_free(val);
4436 tcg_temp_free(EA);
a541f297 4437#endif
79aceca5
FB
4438}
4439
4440/* dcdst */
99e300ef 4441static void gen_dcbst(DisasContext *ctx)
79aceca5 4442{
76a66253 4443 /* XXX: specification say this is treated as a load by the MMU */
76db3ba4
AJ
4444 TCGv t0;
4445 gen_set_access_type(ctx, ACCESS_CACHE);
4446 t0 = tcg_temp_new();
4447 gen_addr_reg_index(ctx, t0);
4448 gen_qemu_ld8u(ctx, t0, t0);
fea0c503 4449 tcg_temp_free(t0);
79aceca5
FB
4450}
4451
4452/* dcbt */
99e300ef 4453static void gen_dcbt(DisasContext *ctx)
79aceca5 4454{
0db1b20e 4455 /* interpreted as no-op */
76a66253
JM
4456 /* XXX: specification say this is treated as a load by the MMU
4457 * but does not generate any exception
4458 */
79aceca5
FB
4459}
4460
4461/* dcbtst */
99e300ef 4462static void gen_dcbtst(DisasContext *ctx)
79aceca5 4463{
0db1b20e 4464 /* interpreted as no-op */
76a66253
JM
4465 /* XXX: specification say this is treated as a load by the MMU
4466 * but does not generate any exception
4467 */
79aceca5
FB
4468}
4469
4470/* dcbz */
99e300ef 4471static void gen_dcbz(DisasContext *ctx)
79aceca5 4472{
8e33944f
AG
4473 TCGv tcgv_addr;
4474 TCGv_i32 tcgv_is_dcbzl;
4475 int is_dcbzl = ctx->opcode & 0x00200000 ? 1 : 0;
d63001d1 4476
76db3ba4 4477 gen_set_access_type(ctx, ACCESS_CACHE);
799a8c8d
AJ
4478 /* NIP cannot be restored if the memory exception comes from an helper */
4479 gen_update_nip(ctx, ctx->nip - 4);
8e33944f
AG
4480 tcgv_addr = tcg_temp_new();
4481 tcgv_is_dcbzl = tcg_const_i32(is_dcbzl);
4482
4483 gen_addr_reg_index(ctx, tcgv_addr);
4484 gen_helper_dcbz(cpu_env, tcgv_addr, tcgv_is_dcbzl);
4485
4486 tcg_temp_free(tcgv_addr);
4487 tcg_temp_free_i32(tcgv_is_dcbzl);
79aceca5
FB
4488}
4489
ae1c1a3d 4490/* dst / dstt */
99e300ef 4491static void gen_dst(DisasContext *ctx)
ae1c1a3d
AJ
4492{
4493 if (rA(ctx->opcode) == 0) {
4494 gen_inval_exception(ctx, POWERPC_EXCP_INVAL_LSWX);
4495 } else {
4496 /* interpreted as no-op */
4497 }
4498}
4499
4500/* dstst /dststt */
99e300ef 4501static void gen_dstst(DisasContext *ctx)
ae1c1a3d
AJ
4502{
4503 if (rA(ctx->opcode) == 0) {
4504 gen_inval_exception(ctx, POWERPC_EXCP_INVAL_LSWX);
4505 } else {
4506 /* interpreted as no-op */
4507 }
4508
4509}
4510
4511/* dss / dssall */
99e300ef 4512static void gen_dss(DisasContext *ctx)
ae1c1a3d
AJ
4513{
4514 /* interpreted as no-op */
4515}
4516
79aceca5 4517/* icbi */
99e300ef 4518static void gen_icbi(DisasContext *ctx)
79aceca5 4519{
76db3ba4
AJ
4520 TCGv t0;
4521 gen_set_access_type(ctx, ACCESS_CACHE);
30032c94
JM
4522 /* NIP cannot be restored if the memory exception comes from an helper */
4523 gen_update_nip(ctx, ctx->nip - 4);
76db3ba4
AJ
4524 t0 = tcg_temp_new();
4525 gen_addr_reg_index(ctx, t0);
2f5a189c 4526 gen_helper_icbi(cpu_env, t0);
37d269df 4527 tcg_temp_free(t0);
79aceca5
FB
4528}
4529
4530/* Optional: */
4531/* dcba */
99e300ef 4532static void gen_dcba(DisasContext *ctx)
79aceca5 4533{
0db1b20e
JM
4534 /* interpreted as no-op */
4535 /* XXX: specification say this is treated as a store by the MMU
4536 * but does not generate any exception
4537 */
79aceca5
FB
4538}
4539
4540/*** Segment register manipulation ***/
4541/* Supervisor only: */
99e300ef 4542
54623277 4543/* mfsr */
99e300ef 4544static void gen_mfsr(DisasContext *ctx)
79aceca5 4545{
9a64fbe4 4546#if defined(CONFIG_USER_ONLY)
e06fcd75 4547 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
9a64fbe4 4548#else
74d37793 4549 TCGv t0;
76db3ba4 4550 if (unlikely(!ctx->mem_idx)) {
e06fcd75 4551 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
9fddaa0c 4552 return;
9a64fbe4 4553 }
74d37793 4554 t0 = tcg_const_tl(SR(ctx->opcode));
c6c7cf05 4555 gen_helper_load_sr(cpu_gpr[rD(ctx->opcode)], cpu_env, t0);
74d37793 4556 tcg_temp_free(t0);
9a64fbe4 4557#endif
79aceca5
FB
4558}
4559
4560/* mfsrin */
99e300ef 4561static void gen_mfsrin(DisasContext *ctx)
79aceca5 4562{
9a64fbe4 4563#if defined(CONFIG_USER_ONLY)
e06fcd75 4564 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
9a64fbe4 4565#else
74d37793 4566 TCGv t0;
76db3ba4 4567 if (unlikely(!ctx->mem_idx)) {
e06fcd75 4568 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
9fddaa0c 4569 return;
9a64fbe4 4570 }
74d37793
AJ
4571 t0 = tcg_temp_new();
4572 tcg_gen_shri_tl(t0, cpu_gpr[rB(ctx->opcode)], 28);
4573 tcg_gen_andi_tl(t0, t0, 0xF);
c6c7cf05 4574 gen_helper_load_sr(cpu_gpr[rD(ctx->opcode)], cpu_env, t0);
74d37793 4575 tcg_temp_free(t0);
9a64fbe4 4576#endif
79aceca5
FB
4577}
4578
4579/* mtsr */
99e300ef 4580static void gen_mtsr(DisasContext *ctx)
79aceca5 4581{
9a64fbe4 4582#if defined(CONFIG_USER_ONLY)
e06fcd75 4583 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
9a64fbe4 4584#else
74d37793 4585 TCGv t0;
76db3ba4 4586 if (unlikely(!ctx->mem_idx)) {
e06fcd75 4587 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
9fddaa0c 4588 return;
9a64fbe4 4589 }
74d37793 4590 t0 = tcg_const_tl(SR(ctx->opcode));
c6c7cf05 4591 gen_helper_store_sr(cpu_env, t0, cpu_gpr[rS(ctx->opcode)]);
74d37793 4592 tcg_temp_free(t0);
9a64fbe4 4593#endif
79aceca5
FB
4594}
4595
4596/* mtsrin */
99e300ef 4597static void gen_mtsrin(DisasContext *ctx)
79aceca5 4598{
9a64fbe4 4599#if defined(CONFIG_USER_ONLY)
e06fcd75 4600 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
9a64fbe4 4601#else
74d37793 4602 TCGv t0;
76db3ba4 4603 if (unlikely(!ctx->mem_idx)) {
e06fcd75 4604 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
9fddaa0c 4605 return;
9a64fbe4 4606 }
74d37793
AJ
4607 t0 = tcg_temp_new();
4608 tcg_gen_shri_tl(t0, cpu_gpr[rB(ctx->opcode)], 28);
4609 tcg_gen_andi_tl(t0, t0, 0xF);
c6c7cf05 4610 gen_helper_store_sr(cpu_env, t0, cpu_gpr[rD(ctx->opcode)]);
74d37793 4611 tcg_temp_free(t0);
9a64fbe4 4612#endif
79aceca5
FB
4613}
4614
12de9a39
JM
4615#if defined(TARGET_PPC64)
4616/* Specific implementation for PowerPC 64 "bridge" emulation using SLB */
e8eaa2c0 4617
54623277 4618/* mfsr */
e8eaa2c0 4619static void gen_mfsr_64b(DisasContext *ctx)
12de9a39
JM
4620{
4621#if defined(CONFIG_USER_ONLY)
e06fcd75 4622 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
12de9a39 4623#else
74d37793 4624 TCGv t0;
76db3ba4 4625 if (unlikely(!ctx->mem_idx)) {
e06fcd75 4626 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
12de9a39
JM
4627 return;
4628 }
74d37793 4629 t0 = tcg_const_tl(SR(ctx->opcode));
c6c7cf05 4630 gen_helper_load_sr(cpu_gpr[rD(ctx->opcode)], cpu_env, t0);
74d37793 4631 tcg_temp_free(t0);
12de9a39
JM
4632#endif
4633}
4634
4635/* mfsrin */
e8eaa2c0 4636static void gen_mfsrin_64b(DisasContext *ctx)
12de9a39
JM
4637{
4638#if defined(CONFIG_USER_ONLY)
e06fcd75 4639 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
12de9a39 4640#else
74d37793 4641 TCGv t0;
76db3ba4 4642 if (unlikely(!ctx->mem_idx)) {
e06fcd75 4643 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
12de9a39
JM
4644 return;
4645 }
74d37793
AJ
4646 t0 = tcg_temp_new();
4647 tcg_gen_shri_tl(t0, cpu_gpr[rB(ctx->opcode)], 28);
4648 tcg_gen_andi_tl(t0, t0, 0xF);
c6c7cf05 4649 gen_helper_load_sr(cpu_gpr[rD(ctx->opcode)], cpu_env, t0);
74d37793 4650 tcg_temp_free(t0);
12de9a39
JM
4651#endif
4652}
4653
4654/* mtsr */
e8eaa2c0 4655static void gen_mtsr_64b(DisasContext *ctx)
12de9a39
JM
4656{
4657#if defined(CONFIG_USER_ONLY)
e06fcd75 4658 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
12de9a39 4659#else
74d37793 4660 TCGv t0;
76db3ba4 4661 if (unlikely(!ctx->mem_idx)) {
e06fcd75 4662 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
12de9a39
JM
4663 return;
4664 }
74d37793 4665 t0 = tcg_const_tl(SR(ctx->opcode));
c6c7cf05 4666 gen_helper_store_sr(cpu_env, t0, cpu_gpr[rS(ctx->opcode)]);
74d37793 4667 tcg_temp_free(t0);
12de9a39
JM
4668#endif
4669}
4670
4671/* mtsrin */
e8eaa2c0 4672static void gen_mtsrin_64b(DisasContext *ctx)
12de9a39
JM
4673{
4674#if defined(CONFIG_USER_ONLY)
e06fcd75 4675 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
12de9a39 4676#else
74d37793 4677 TCGv t0;
76db3ba4 4678 if (unlikely(!ctx->mem_idx)) {
e06fcd75 4679 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
12de9a39
JM
4680 return;
4681 }
74d37793
AJ
4682 t0 = tcg_temp_new();
4683 tcg_gen_shri_tl(t0, cpu_gpr[rB(ctx->opcode)], 28);
4684 tcg_gen_andi_tl(t0, t0, 0xF);
c6c7cf05 4685 gen_helper_store_sr(cpu_env, t0, cpu_gpr[rS(ctx->opcode)]);
74d37793 4686 tcg_temp_free(t0);
12de9a39
JM
4687#endif
4688}
f6b868fc
BS
4689
4690/* slbmte */
e8eaa2c0 4691static void gen_slbmte(DisasContext *ctx)
f6b868fc
BS
4692{
4693#if defined(CONFIG_USER_ONLY)
4694 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
4695#else
4696 if (unlikely(!ctx->mem_idx)) {
4697 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
4698 return;
4699 }
c6c7cf05
BS
4700 gen_helper_store_slb(cpu_env, cpu_gpr[rB(ctx->opcode)],
4701 cpu_gpr[rS(ctx->opcode)]);
f6b868fc
BS
4702#endif
4703}
4704
efdef95f
DG
4705static void gen_slbmfee(DisasContext *ctx)
4706{
4707#if defined(CONFIG_USER_ONLY)
4708 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
4709#else
4710 if (unlikely(!ctx->mem_idx)) {
4711 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
4712 return;
4713 }
c6c7cf05 4714 gen_helper_load_slb_esid(cpu_gpr[rS(ctx->opcode)], cpu_env,
efdef95f
DG
4715 cpu_gpr[rB(ctx->opcode)]);
4716#endif
4717}
4718
4719static void gen_slbmfev(DisasContext *ctx)
4720{
4721#if defined(CONFIG_USER_ONLY)
4722 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
4723#else
4724 if (unlikely(!ctx->mem_idx)) {
4725 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
4726 return;
4727 }
c6c7cf05 4728 gen_helper_load_slb_vsid(cpu_gpr[rS(ctx->opcode)], cpu_env,
efdef95f
DG
4729 cpu_gpr[rB(ctx->opcode)]);
4730#endif
4731}
12de9a39
JM
4732#endif /* defined(TARGET_PPC64) */
4733
79aceca5 4734/*** Lookaside buffer management ***/
76db3ba4 4735/* Optional & mem_idx only: */
99e300ef 4736
54623277 4737/* tlbia */
99e300ef 4738static void gen_tlbia(DisasContext *ctx)
79aceca5 4739{
9a64fbe4 4740#if defined(CONFIG_USER_ONLY)
e06fcd75 4741 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
9a64fbe4 4742#else
76db3ba4 4743 if (unlikely(!ctx->mem_idx)) {
e06fcd75 4744 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
9fddaa0c 4745 return;
9a64fbe4 4746 }
c6c7cf05 4747 gen_helper_tlbia(cpu_env);
9a64fbe4 4748#endif
79aceca5
FB
4749}
4750
bf14b1ce 4751/* tlbiel */
99e300ef 4752static void gen_tlbiel(DisasContext *ctx)
bf14b1ce
BS
4753{
4754#if defined(CONFIG_USER_ONLY)
4755 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
4756#else
4757 if (unlikely(!ctx->mem_idx)) {
4758 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
4759 return;
4760 }
c6c7cf05 4761 gen_helper_tlbie(cpu_env, cpu_gpr[rB(ctx->opcode)]);
bf14b1ce
BS
4762#endif
4763}
4764
79aceca5 4765/* tlbie */
99e300ef 4766static void gen_tlbie(DisasContext *ctx)
79aceca5 4767{
9a64fbe4 4768#if defined(CONFIG_USER_ONLY)
e06fcd75 4769 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
9a64fbe4 4770#else
76db3ba4 4771 if (unlikely(!ctx->mem_idx)) {
e06fcd75 4772 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
9fddaa0c 4773 return;
9a64fbe4 4774 }
9ca3f7f3 4775 if (NARROW_MODE(ctx)) {
74d37793
AJ
4776 TCGv t0 = tcg_temp_new();
4777 tcg_gen_ext32u_tl(t0, cpu_gpr[rB(ctx->opcode)]);
c6c7cf05 4778 gen_helper_tlbie(cpu_env, t0);
74d37793 4779 tcg_temp_free(t0);
9ca3f7f3 4780 } else {
c6c7cf05 4781 gen_helper_tlbie(cpu_env, cpu_gpr[rB(ctx->opcode)]);
9ca3f7f3 4782 }
9a64fbe4 4783#endif
79aceca5
FB
4784}
4785
4786/* tlbsync */
99e300ef 4787static void gen_tlbsync(DisasContext *ctx)
79aceca5 4788{
9a64fbe4 4789#if defined(CONFIG_USER_ONLY)
e06fcd75 4790 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
9a64fbe4 4791#else
76db3ba4 4792 if (unlikely(!ctx->mem_idx)) {
e06fcd75 4793 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
9fddaa0c 4794 return;
9a64fbe4
FB
4795 }
4796 /* This has no effect: it should ensure that all previous
4797 * tlbie have completed
4798 */
e06fcd75 4799 gen_stop_exception(ctx);
9a64fbe4 4800#endif
79aceca5
FB
4801}
4802
426613db
JM
4803#if defined(TARGET_PPC64)
4804/* slbia */
99e300ef 4805static void gen_slbia(DisasContext *ctx)
426613db
JM
4806{
4807#if defined(CONFIG_USER_ONLY)
e06fcd75 4808 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
426613db 4809#else
76db3ba4 4810 if (unlikely(!ctx->mem_idx)) {
e06fcd75 4811 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
426613db
JM
4812 return;
4813 }
c6c7cf05 4814 gen_helper_slbia(cpu_env);
426613db
JM
4815#endif
4816}
4817
4818/* slbie */
99e300ef 4819static void gen_slbie(DisasContext *ctx)
426613db
JM
4820{
4821#if defined(CONFIG_USER_ONLY)
e06fcd75 4822 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
426613db 4823#else
76db3ba4 4824 if (unlikely(!ctx->mem_idx)) {
e06fcd75 4825 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
426613db
JM
4826 return;
4827 }
c6c7cf05 4828 gen_helper_slbie(cpu_env, cpu_gpr[rB(ctx->opcode)]);
426613db
JM
4829#endif
4830}
4831#endif
4832
79aceca5
FB
4833/*** External control ***/
4834/* Optional: */
99e300ef 4835
54623277 4836/* eciwx */
99e300ef 4837static void gen_eciwx(DisasContext *ctx)
79aceca5 4838{
76db3ba4 4839 TCGv t0;
fa407c03 4840 /* Should check EAR[E] ! */
76db3ba4
AJ
4841 gen_set_access_type(ctx, ACCESS_EXT);
4842 t0 = tcg_temp_new();
4843 gen_addr_reg_index(ctx, t0);
fa407c03 4844 gen_check_align(ctx, t0, 0x03);
76db3ba4 4845 gen_qemu_ld32u(ctx, cpu_gpr[rD(ctx->opcode)], t0);
fa407c03 4846 tcg_temp_free(t0);
76a66253
JM
4847}
4848
4849/* ecowx */
99e300ef 4850static void gen_ecowx(DisasContext *ctx)
76a66253 4851{
76db3ba4 4852 TCGv t0;
fa407c03 4853 /* Should check EAR[E] ! */
76db3ba4
AJ
4854 gen_set_access_type(ctx, ACCESS_EXT);
4855 t0 = tcg_temp_new();
4856 gen_addr_reg_index(ctx, t0);
fa407c03 4857 gen_check_align(ctx, t0, 0x03);
76db3ba4 4858 gen_qemu_st32(ctx, cpu_gpr[rD(ctx->opcode)], t0);
fa407c03 4859 tcg_temp_free(t0);
76a66253
JM
4860}
4861
4862/* PowerPC 601 specific instructions */
99e300ef 4863
54623277 4864/* abs - abs. */
99e300ef 4865static void gen_abs(DisasContext *ctx)
76a66253 4866{
22e0e173
AJ
4867 int l1 = gen_new_label();
4868 int l2 = gen_new_label();
4869 tcg_gen_brcondi_tl(TCG_COND_GE, cpu_gpr[rA(ctx->opcode)], 0, l1);
4870 tcg_gen_neg_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)]);
4871 tcg_gen_br(l2);
4872 gen_set_label(l1);
4873 tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)]);
4874 gen_set_label(l2);
76a66253 4875 if (unlikely(Rc(ctx->opcode) != 0))
22e0e173 4876 gen_set_Rc0(ctx, cpu_gpr[rD(ctx->opcode)]);
76a66253
JM
4877}
4878
4879/* abso - abso. */
99e300ef 4880static void gen_abso(DisasContext *ctx)
76a66253 4881{
22e0e173
AJ
4882 int l1 = gen_new_label();
4883 int l2 = gen_new_label();
4884 int l3 = gen_new_label();
4885 /* Start with XER OV disabled, the most likely case */
da91a00f 4886 tcg_gen_movi_tl(cpu_ov, 0);
22e0e173
AJ
4887 tcg_gen_brcondi_tl(TCG_COND_GE, cpu_gpr[rA(ctx->opcode)], 0, l2);
4888 tcg_gen_brcondi_tl(TCG_COND_NE, cpu_gpr[rA(ctx->opcode)], 0x80000000, l1);
da91a00f
RH
4889 tcg_gen_movi_tl(cpu_ov, 1);
4890 tcg_gen_movi_tl(cpu_so, 1);
22e0e173
AJ
4891 tcg_gen_br(l2);
4892 gen_set_label(l1);
4893 tcg_gen_neg_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)]);
4894 tcg_gen_br(l3);
4895 gen_set_label(l2);
4896 tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)]);
4897 gen_set_label(l3);
76a66253 4898 if (unlikely(Rc(ctx->opcode) != 0))
22e0e173 4899 gen_set_Rc0(ctx, cpu_gpr[rD(ctx->opcode)]);
76a66253
JM
4900}
4901
4902/* clcs */
99e300ef 4903static void gen_clcs(DisasContext *ctx)
76a66253 4904{
22e0e173 4905 TCGv_i32 t0 = tcg_const_i32(rA(ctx->opcode));
d523dd00 4906 gen_helper_clcs(cpu_gpr[rD(ctx->opcode)], cpu_env, t0);
22e0e173 4907 tcg_temp_free_i32(t0);
c7697e1f 4908 /* Rc=1 sets CR0 to an undefined state */
76a66253
JM
4909}
4910
4911/* div - div. */
99e300ef 4912static void gen_div(DisasContext *ctx)
76a66253 4913{
d15f74fb
BS
4914 gen_helper_div(cpu_gpr[rD(ctx->opcode)], cpu_env, cpu_gpr[rA(ctx->opcode)],
4915 cpu_gpr[rB(ctx->opcode)]);
76a66253 4916 if (unlikely(Rc(ctx->opcode) != 0))
22e0e173 4917 gen_set_Rc0(ctx, cpu_gpr[rD(ctx->opcode)]);
76a66253
JM
4918}
4919
4920/* divo - divo. */
99e300ef 4921static void gen_divo(DisasContext *ctx)
76a66253 4922{
d15f74fb
BS
4923 gen_helper_divo(cpu_gpr[rD(ctx->opcode)], cpu_env, cpu_gpr[rA(ctx->opcode)],
4924 cpu_gpr[rB(ctx->opcode)]);
76a66253 4925 if (unlikely(Rc(ctx->opcode) != 0))
22e0e173 4926 gen_set_Rc0(ctx, cpu_gpr[rD(ctx->opcode)]);
76a66253
JM
4927}
4928
4929/* divs - divs. */
99e300ef 4930static void gen_divs(DisasContext *ctx)
76a66253 4931{
d15f74fb
BS
4932 gen_helper_divs(cpu_gpr[rD(ctx->opcode)], cpu_env, cpu_gpr[rA(ctx->opcode)],
4933 cpu_gpr[rB(ctx->opcode)]);
76a66253 4934 if (unlikely(Rc(ctx->opcode) != 0))
22e0e173 4935 gen_set_Rc0(ctx, cpu_gpr[rD(ctx->opcode)]);
76a66253
JM
4936}
4937
4938/* divso - divso. */
99e300ef 4939static void gen_divso(DisasContext *ctx)
76a66253 4940{
d15f74fb
BS
4941 gen_helper_divso(cpu_gpr[rD(ctx->opcode)], cpu_env,
4942 cpu_gpr[rA(ctx->opcode)], cpu_gpr[rB(ctx->opcode)]);
76a66253 4943 if (unlikely(Rc(ctx->opcode) != 0))
22e0e173 4944 gen_set_Rc0(ctx, cpu_gpr[rD(ctx->opcode)]);
76a66253
JM
4945}
4946
4947/* doz - doz. */
99e300ef 4948static void gen_doz(DisasContext *ctx)
76a66253 4949{
22e0e173
AJ
4950 int l1 = gen_new_label();
4951 int l2 = gen_new_label();
4952 tcg_gen_brcond_tl(TCG_COND_GE, cpu_gpr[rB(ctx->opcode)], cpu_gpr[rA(ctx->opcode)], l1);
4953 tcg_gen_sub_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rB(ctx->opcode)], cpu_gpr[rA(ctx->opcode)]);
4954 tcg_gen_br(l2);
4955 gen_set_label(l1);
4956 tcg_gen_movi_tl(cpu_gpr[rD(ctx->opcode)], 0);
4957 gen_set_label(l2);
76a66253 4958 if (unlikely(Rc(ctx->opcode) != 0))
22e0e173 4959 gen_set_Rc0(ctx, cpu_gpr[rD(ctx->opcode)]);
76a66253
JM
4960}
4961
4962/* dozo - dozo. */
99e300ef 4963static void gen_dozo(DisasContext *ctx)
76a66253 4964{
22e0e173
AJ
4965 int l1 = gen_new_label();
4966 int l2 = gen_new_label();
4967 TCGv t0 = tcg_temp_new();
4968 TCGv t1 = tcg_temp_new();
4969 TCGv t2 = tcg_temp_new();
4970 /* Start with XER OV disabled, the most likely case */
da91a00f 4971 tcg_gen_movi_tl(cpu_ov, 0);
22e0e173
AJ
4972 tcg_gen_brcond_tl(TCG_COND_GE, cpu_gpr[rB(ctx->opcode)], cpu_gpr[rA(ctx->opcode)], l1);
4973 tcg_gen_sub_tl(t0, cpu_gpr[rB(ctx->opcode)], cpu_gpr[rA(ctx->opcode)]);
4974 tcg_gen_xor_tl(t1, cpu_gpr[rB(ctx->opcode)], cpu_gpr[rA(ctx->opcode)]);
4975 tcg_gen_xor_tl(t2, cpu_gpr[rA(ctx->opcode)], t0);
4976 tcg_gen_andc_tl(t1, t1, t2);
4977 tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], t0);
4978 tcg_gen_brcondi_tl(TCG_COND_GE, t1, 0, l2);
da91a00f
RH
4979 tcg_gen_movi_tl(cpu_ov, 1);
4980 tcg_gen_movi_tl(cpu_so, 1);
22e0e173
AJ
4981 tcg_gen_br(l2);
4982 gen_set_label(l1);
4983 tcg_gen_movi_tl(cpu_gpr[rD(ctx->opcode)], 0);
4984 gen_set_label(l2);
4985 tcg_temp_free(t0);
4986 tcg_temp_free(t1);
4987 tcg_temp_free(t2);
76a66253 4988 if (unlikely(Rc(ctx->opcode) != 0))
22e0e173 4989 gen_set_Rc0(ctx, cpu_gpr[rD(ctx->opcode)]);
76a66253
JM
4990}
4991
4992/* dozi */
99e300ef 4993static void gen_dozi(DisasContext *ctx)
76a66253 4994{
22e0e173
AJ
4995 target_long simm = SIMM(ctx->opcode);
4996 int l1 = gen_new_label();
4997 int l2 = gen_new_label();
4998 tcg_gen_brcondi_tl(TCG_COND_LT, cpu_gpr[rA(ctx->opcode)], simm, l1);
4999 tcg_gen_subfi_tl(cpu_gpr[rD(ctx->opcode)], simm, cpu_gpr[rA(ctx->opcode)]);
5000 tcg_gen_br(l2);
5001 gen_set_label(l1);
5002 tcg_gen_movi_tl(cpu_gpr[rD(ctx->opcode)], 0);
5003 gen_set_label(l2);
5004 if (unlikely(Rc(ctx->opcode) != 0))
5005 gen_set_Rc0(ctx, cpu_gpr[rD(ctx->opcode)]);
76a66253
JM
5006}
5007
76a66253 5008/* lscbx - lscbx. */
99e300ef 5009static void gen_lscbx(DisasContext *ctx)
76a66253 5010{
bdb4b689
AJ
5011 TCGv t0 = tcg_temp_new();
5012 TCGv_i32 t1 = tcg_const_i32(rD(ctx->opcode));
5013 TCGv_i32 t2 = tcg_const_i32(rA(ctx->opcode));
5014 TCGv_i32 t3 = tcg_const_i32(rB(ctx->opcode));
76a66253 5015
76db3ba4 5016 gen_addr_reg_index(ctx, t0);
76a66253 5017 /* NIP cannot be restored if the memory exception comes from an helper */
d9bce9d9 5018 gen_update_nip(ctx, ctx->nip - 4);
2f5a189c 5019 gen_helper_lscbx(t0, cpu_env, t0, t1, t2, t3);
bdb4b689
AJ
5020 tcg_temp_free_i32(t1);
5021 tcg_temp_free_i32(t2);
5022 tcg_temp_free_i32(t3);
3d7b417e 5023 tcg_gen_andi_tl(cpu_xer, cpu_xer, ~0x7F);
bdb4b689 5024 tcg_gen_or_tl(cpu_xer, cpu_xer, t0);
76a66253 5025 if (unlikely(Rc(ctx->opcode) != 0))
bdb4b689
AJ
5026 gen_set_Rc0(ctx, t0);
5027 tcg_temp_free(t0);
76a66253
JM
5028}
5029
5030/* maskg - maskg. */
99e300ef 5031static void gen_maskg(DisasContext *ctx)
76a66253 5032{
22e0e173
AJ
5033 int l1 = gen_new_label();
5034 TCGv t0 = tcg_temp_new();
5035 TCGv t1 = tcg_temp_new();
5036 TCGv t2 = tcg_temp_new();
5037 TCGv t3 = tcg_temp_new();
5038 tcg_gen_movi_tl(t3, 0xFFFFFFFF);
5039 tcg_gen_andi_tl(t0, cpu_gpr[rB(ctx->opcode)], 0x1F);
5040 tcg_gen_andi_tl(t1, cpu_gpr[rS(ctx->opcode)], 0x1F);
5041 tcg_gen_addi_tl(t2, t0, 1);
5042 tcg_gen_shr_tl(t2, t3, t2);
5043 tcg_gen_shr_tl(t3, t3, t1);
5044 tcg_gen_xor_tl(cpu_gpr[rA(ctx->opcode)], t2, t3);
5045 tcg_gen_brcond_tl(TCG_COND_GE, t0, t1, l1);
5046 tcg_gen_neg_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rA(ctx->opcode)]);
5047 gen_set_label(l1);
5048 tcg_temp_free(t0);
5049 tcg_temp_free(t1);
5050 tcg_temp_free(t2);
5051 tcg_temp_free(t3);
76a66253 5052 if (unlikely(Rc(ctx->opcode) != 0))
22e0e173 5053 gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
76a66253
JM
5054}
5055
5056/* maskir - maskir. */
99e300ef 5057static void gen_maskir(DisasContext *ctx)
76a66253 5058{
22e0e173
AJ
5059 TCGv t0 = tcg_temp_new();
5060 TCGv t1 = tcg_temp_new();
5061 tcg_gen_and_tl(t0, cpu_gpr[rS(ctx->opcode)], cpu_gpr[rB(ctx->opcode)]);
5062 tcg_gen_andc_tl(t1, cpu_gpr[rA(ctx->opcode)], cpu_gpr[rB(ctx->opcode)]);
5063 tcg_gen_or_tl(cpu_gpr[rA(ctx->opcode)], t0, t1);
5064 tcg_temp_free(t0);
5065 tcg_temp_free(t1);
76a66253 5066 if (unlikely(Rc(ctx->opcode) != 0))
22e0e173 5067 gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
76a66253
JM
5068}
5069
5070/* mul - mul. */
99e300ef 5071static void gen_mul(DisasContext *ctx)
76a66253 5072{
22e0e173
AJ
5073 TCGv_i64 t0 = tcg_temp_new_i64();
5074 TCGv_i64 t1 = tcg_temp_new_i64();
5075 TCGv t2 = tcg_temp_new();
5076 tcg_gen_extu_tl_i64(t0, cpu_gpr[rA(ctx->opcode)]);
5077 tcg_gen_extu_tl_i64(t1, cpu_gpr[rB(ctx->opcode)]);
5078 tcg_gen_mul_i64(t0, t0, t1);
5079 tcg_gen_trunc_i64_tl(t2, t0);
5080 gen_store_spr(SPR_MQ, t2);
5081 tcg_gen_shri_i64(t1, t0, 32);
5082 tcg_gen_trunc_i64_tl(cpu_gpr[rD(ctx->opcode)], t1);
5083 tcg_temp_free_i64(t0);
5084 tcg_temp_free_i64(t1);
5085 tcg_temp_free(t2);
76a66253 5086 if (unlikely(Rc(ctx->opcode) != 0))
22e0e173 5087 gen_set_Rc0(ctx, cpu_gpr[rD(ctx->opcode)]);
76a66253
JM
5088}
5089
5090/* mulo - mulo. */
99e300ef 5091static void gen_mulo(DisasContext *ctx)
76a66253 5092{
22e0e173
AJ
5093 int l1 = gen_new_label();
5094 TCGv_i64 t0 = tcg_temp_new_i64();
5095 TCGv_i64 t1 = tcg_temp_new_i64();
5096 TCGv t2 = tcg_temp_new();
5097 /* Start with XER OV disabled, the most likely case */
da91a00f 5098 tcg_gen_movi_tl(cpu_ov, 0);
22e0e173
AJ
5099 tcg_gen_extu_tl_i64(t0, cpu_gpr[rA(ctx->opcode)]);
5100 tcg_gen_extu_tl_i64(t1, cpu_gpr[rB(ctx->opcode)]);
5101 tcg_gen_mul_i64(t0, t0, t1);
5102 tcg_gen_trunc_i64_tl(t2, t0);
5103 gen_store_spr(SPR_MQ, t2);
5104 tcg_gen_shri_i64(t1, t0, 32);
5105 tcg_gen_trunc_i64_tl(cpu_gpr[rD(ctx->opcode)], t1);
5106 tcg_gen_ext32s_i64(t1, t0);
5107 tcg_gen_brcond_i64(TCG_COND_EQ, t0, t1, l1);
da91a00f
RH
5108 tcg_gen_movi_tl(cpu_ov, 1);
5109 tcg_gen_movi_tl(cpu_so, 1);
22e0e173
AJ
5110 gen_set_label(l1);
5111 tcg_temp_free_i64(t0);
5112 tcg_temp_free_i64(t1);
5113 tcg_temp_free(t2);
76a66253 5114 if (unlikely(Rc(ctx->opcode) != 0))
22e0e173 5115 gen_set_Rc0(ctx, cpu_gpr[rD(ctx->opcode)]);
76a66253
JM
5116}
5117
5118/* nabs - nabs. */
99e300ef 5119static void gen_nabs(DisasContext *ctx)
76a66253 5120{
22e0e173
AJ
5121 int l1 = gen_new_label();
5122 int l2 = gen_new_label();
5123 tcg_gen_brcondi_tl(TCG_COND_GT, cpu_gpr[rA(ctx->opcode)], 0, l1);
5124 tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)]);
5125 tcg_gen_br(l2);
5126 gen_set_label(l1);
5127 tcg_gen_neg_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)]);
5128 gen_set_label(l2);
76a66253 5129 if (unlikely(Rc(ctx->opcode) != 0))
22e0e173 5130 gen_set_Rc0(ctx, cpu_gpr[rD(ctx->opcode)]);
76a66253
JM
5131}
5132
5133/* nabso - nabso. */
99e300ef 5134static void gen_nabso(DisasContext *ctx)
76a66253 5135{
22e0e173
AJ
5136 int l1 = gen_new_label();
5137 int l2 = gen_new_label();
5138 tcg_gen_brcondi_tl(TCG_COND_GT, cpu_gpr[rA(ctx->opcode)], 0, l1);
5139 tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)]);
5140 tcg_gen_br(l2);
5141 gen_set_label(l1);
5142 tcg_gen_neg_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)]);
5143 gen_set_label(l2);
5144 /* nabs never overflows */
da91a00f 5145 tcg_gen_movi_tl(cpu_ov, 0);
76a66253 5146 if (unlikely(Rc(ctx->opcode) != 0))
22e0e173 5147 gen_set_Rc0(ctx, cpu_gpr[rD(ctx->opcode)]);
76a66253
JM
5148}
5149
5150/* rlmi - rlmi. */
99e300ef 5151static void gen_rlmi(DisasContext *ctx)
76a66253 5152{
7487953d
AJ
5153 uint32_t mb = MB(ctx->opcode);
5154 uint32_t me = ME(ctx->opcode);
5155 TCGv t0 = tcg_temp_new();
5156 tcg_gen_andi_tl(t0, cpu_gpr[rB(ctx->opcode)], 0x1F);
5157 tcg_gen_rotl_tl(t0, cpu_gpr[rS(ctx->opcode)], t0);
5158 tcg_gen_andi_tl(t0, t0, MASK(mb, me));
5159 tcg_gen_andi_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rA(ctx->opcode)], ~MASK(mb, me));
5160 tcg_gen_or_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rA(ctx->opcode)], t0);
5161 tcg_temp_free(t0);
76a66253 5162 if (unlikely(Rc(ctx->opcode) != 0))
7487953d 5163 gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
76a66253
JM
5164}
5165
5166/* rrib - rrib. */
99e300ef 5167static void gen_rrib(DisasContext *ctx)
76a66253 5168{
7487953d
AJ
5169 TCGv t0 = tcg_temp_new();
5170 TCGv t1 = tcg_temp_new();
5171 tcg_gen_andi_tl(t0, cpu_gpr[rB(ctx->opcode)], 0x1F);
5172 tcg_gen_movi_tl(t1, 0x80000000);
5173 tcg_gen_shr_tl(t1, t1, t0);
5174 tcg_gen_shr_tl(t0, cpu_gpr[rS(ctx->opcode)], t0);
5175 tcg_gen_and_tl(t0, t0, t1);
5176 tcg_gen_andc_tl(t1, cpu_gpr[rA(ctx->opcode)], t1);
5177 tcg_gen_or_tl(cpu_gpr[rA(ctx->opcode)], t0, t1);
5178 tcg_temp_free(t0);
5179 tcg_temp_free(t1);
76a66253 5180 if (unlikely(Rc(ctx->opcode) != 0))
7487953d 5181 gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
76a66253
JM
5182}
5183
5184/* sle - sle. */
99e300ef 5185static void gen_sle(DisasContext *ctx)
76a66253 5186{
7487953d
AJ
5187 TCGv t0 = tcg_temp_new();
5188 TCGv t1 = tcg_temp_new();
5189 tcg_gen_andi_tl(t1, cpu_gpr[rB(ctx->opcode)], 0x1F);
5190 tcg_gen_shl_tl(t0, cpu_gpr[rS(ctx->opcode)], t1);
5191 tcg_gen_subfi_tl(t1, 32, t1);
5192 tcg_gen_shr_tl(t1, cpu_gpr[rS(ctx->opcode)], t1);
5193 tcg_gen_or_tl(t1, t0, t1);
5194 tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], t0);
5195 gen_store_spr(SPR_MQ, t1);
5196 tcg_temp_free(t0);
5197 tcg_temp_free(t1);
76a66253 5198 if (unlikely(Rc(ctx->opcode) != 0))
7487953d 5199 gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
76a66253
JM
5200}
5201
5202/* sleq - sleq. */
99e300ef 5203static void gen_sleq(DisasContext *ctx)
76a66253 5204{
7487953d
AJ
5205 TCGv t0 = tcg_temp_new();
5206 TCGv t1 = tcg_temp_new();
5207 TCGv t2 = tcg_temp_new();
5208 tcg_gen_andi_tl(t0, cpu_gpr[rB(ctx->opcode)], 0x1F);
5209 tcg_gen_movi_tl(t2, 0xFFFFFFFF);
5210 tcg_gen_shl_tl(t2, t2, t0);
5211 tcg_gen_rotl_tl(t0, cpu_gpr[rS(ctx->opcode)], t0);
5212 gen_load_spr(t1, SPR_MQ);
5213 gen_store_spr(SPR_MQ, t0);
5214 tcg_gen_and_tl(t0, t0, t2);
5215 tcg_gen_andc_tl(t1, t1, t2);
5216 tcg_gen_or_tl(cpu_gpr[rA(ctx->opcode)], t0, t1);
5217 tcg_temp_free(t0);
5218 tcg_temp_free(t1);
5219 tcg_temp_free(t2);
76a66253 5220 if (unlikely(Rc(ctx->opcode) != 0))
7487953d 5221 gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
76a66253
JM
5222}
5223
5224/* sliq - sliq. */
99e300ef 5225static void gen_sliq(DisasContext *ctx)
76a66253 5226{
7487953d
AJ
5227 int sh = SH(ctx->opcode);
5228 TCGv t0 = tcg_temp_new();
5229 TCGv t1 = tcg_temp_new();
5230 tcg_gen_shli_tl(t0, cpu_gpr[rS(ctx->opcode)], sh);
5231 tcg_gen_shri_tl(t1, cpu_gpr[rS(ctx->opcode)], 32 - sh);
5232 tcg_gen_or_tl(t1, t0, t1);
5233 tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], t0);
5234 gen_store_spr(SPR_MQ, t1);
5235 tcg_temp_free(t0);
5236 tcg_temp_free(t1);
76a66253 5237 if (unlikely(Rc(ctx->opcode) != 0))
7487953d 5238 gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
76a66253
JM
5239}
5240
5241/* slliq - slliq. */
99e300ef 5242static void gen_slliq(DisasContext *ctx)
76a66253 5243{
7487953d
AJ
5244 int sh = SH(ctx->opcode);
5245 TCGv t0 = tcg_temp_new();
5246 TCGv t1 = tcg_temp_new();
5247 tcg_gen_rotli_tl(t0, cpu_gpr[rS(ctx->opcode)], sh);
5248 gen_load_spr(t1, SPR_MQ);
5249 gen_store_spr(SPR_MQ, t0);
5250 tcg_gen_andi_tl(t0, t0, (0xFFFFFFFFU << sh));
5251 tcg_gen_andi_tl(t1, t1, ~(0xFFFFFFFFU << sh));
5252 tcg_gen_or_tl(cpu_gpr[rA(ctx->opcode)], t0, t1);
5253 tcg_temp_free(t0);
5254 tcg_temp_free(t1);
76a66253 5255 if (unlikely(Rc(ctx->opcode) != 0))
7487953d 5256 gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
76a66253
JM
5257}
5258
5259/* sllq - sllq. */
99e300ef 5260static void gen_sllq(DisasContext *ctx)
76a66253 5261{
7487953d
AJ
5262 int l1 = gen_new_label();
5263 int l2 = gen_new_label();
5264 TCGv t0 = tcg_temp_local_new();
5265 TCGv t1 = tcg_temp_local_new();
5266 TCGv t2 = tcg_temp_local_new();
5267 tcg_gen_andi_tl(t2, cpu_gpr[rB(ctx->opcode)], 0x1F);
5268 tcg_gen_movi_tl(t1, 0xFFFFFFFF);
5269 tcg_gen_shl_tl(t1, t1, t2);
5270 tcg_gen_andi_tl(t0, cpu_gpr[rB(ctx->opcode)], 0x20);
5271 tcg_gen_brcondi_tl(TCG_COND_EQ, t0, 0, l1);
5272 gen_load_spr(t0, SPR_MQ);
5273 tcg_gen_and_tl(cpu_gpr[rA(ctx->opcode)], t0, t1);
5274 tcg_gen_br(l2);
5275 gen_set_label(l1);
5276 tcg_gen_shl_tl(t0, cpu_gpr[rS(ctx->opcode)], t2);
5277 gen_load_spr(t2, SPR_MQ);
5278 tcg_gen_andc_tl(t1, t2, t1);
5279 tcg_gen_or_tl(cpu_gpr[rA(ctx->opcode)], t0, t1);
5280 gen_set_label(l2);
5281 tcg_temp_free(t0);
5282 tcg_temp_free(t1);
5283 tcg_temp_free(t2);
76a66253 5284 if (unlikely(Rc(ctx->opcode) != 0))
7487953d 5285 gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
76a66253
JM
5286}
5287
5288/* slq - slq. */
99e300ef 5289static void gen_slq(DisasContext *ctx)
76a66253 5290{
7487953d
AJ
5291 int l1 = gen_new_label();
5292 TCGv t0 = tcg_temp_new();
5293 TCGv t1 = tcg_temp_new();
5294 tcg_gen_andi_tl(t1, cpu_gpr[rB(ctx->opcode)], 0x1F);
5295 tcg_gen_shl_tl(t0, cpu_gpr[rS(ctx->opcode)], t1);
5296 tcg_gen_subfi_tl(t1, 32, t1);
5297 tcg_gen_shr_tl(t1, cpu_gpr[rS(ctx->opcode)], t1);
5298 tcg_gen_or_tl(t1, t0, t1);
5299 gen_store_spr(SPR_MQ, t1);
5300 tcg_gen_andi_tl(t1, cpu_gpr[rB(ctx->opcode)], 0x20);
5301 tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], t0);
5302 tcg_gen_brcondi_tl(TCG_COND_EQ, t1, 0, l1);
5303 tcg_gen_movi_tl(cpu_gpr[rA(ctx->opcode)], 0);
5304 gen_set_label(l1);
5305 tcg_temp_free(t0);
5306 tcg_temp_free(t1);
76a66253 5307 if (unlikely(Rc(ctx->opcode) != 0))
7487953d 5308 gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
76a66253
JM
5309}
5310
d9bce9d9 5311/* sraiq - sraiq. */
99e300ef 5312static void gen_sraiq(DisasContext *ctx)
76a66253 5313{
7487953d
AJ
5314 int sh = SH(ctx->opcode);
5315 int l1 = gen_new_label();
5316 TCGv t0 = tcg_temp_new();
5317 TCGv t1 = tcg_temp_new();
5318 tcg_gen_shri_tl(t0, cpu_gpr[rS(ctx->opcode)], sh);
5319 tcg_gen_shli_tl(t1, cpu_gpr[rS(ctx->opcode)], 32 - sh);
5320 tcg_gen_or_tl(t0, t0, t1);
5321 gen_store_spr(SPR_MQ, t0);
da91a00f 5322 tcg_gen_movi_tl(cpu_ca, 0);
7487953d
AJ
5323 tcg_gen_brcondi_tl(TCG_COND_EQ, t1, 0, l1);
5324 tcg_gen_brcondi_tl(TCG_COND_GE, cpu_gpr[rS(ctx->opcode)], 0, l1);
da91a00f 5325 tcg_gen_movi_tl(cpu_ca, 1);
7487953d
AJ
5326 gen_set_label(l1);
5327 tcg_gen_sari_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)], sh);
5328 tcg_temp_free(t0);
5329 tcg_temp_free(t1);
76a66253 5330 if (unlikely(Rc(ctx->opcode) != 0))
7487953d 5331 gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
76a66253
JM
5332}
5333
5334/* sraq - sraq. */
99e300ef 5335static void gen_sraq(DisasContext *ctx)
76a66253 5336{
7487953d
AJ
5337 int l1 = gen_new_label();
5338 int l2 = gen_new_label();
5339 TCGv t0 = tcg_temp_new();
5340 TCGv t1 = tcg_temp_local_new();
5341 TCGv t2 = tcg_temp_local_new();
5342 tcg_gen_andi_tl(t2, cpu_gpr[rB(ctx->opcode)], 0x1F);
5343 tcg_gen_shr_tl(t0, cpu_gpr[rS(ctx->opcode)], t2);
5344 tcg_gen_sar_tl(t1, cpu_gpr[rS(ctx->opcode)], t2);
5345 tcg_gen_subfi_tl(t2, 32, t2);
5346 tcg_gen_shl_tl(t2, cpu_gpr[rS(ctx->opcode)], t2);
5347 tcg_gen_or_tl(t0, t0, t2);
5348 gen_store_spr(SPR_MQ, t0);
5349 tcg_gen_andi_tl(t0, cpu_gpr[rB(ctx->opcode)], 0x20);
5350 tcg_gen_brcondi_tl(TCG_COND_EQ, t2, 0, l1);
5351 tcg_gen_mov_tl(t2, cpu_gpr[rS(ctx->opcode)]);
5352 tcg_gen_sari_tl(t1, cpu_gpr[rS(ctx->opcode)], 31);
5353 gen_set_label(l1);
5354 tcg_temp_free(t0);
5355 tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], t1);
da91a00f 5356 tcg_gen_movi_tl(cpu_ca, 0);
7487953d
AJ
5357 tcg_gen_brcondi_tl(TCG_COND_GE, t1, 0, l2);
5358 tcg_gen_brcondi_tl(TCG_COND_EQ, t2, 0, l2);
da91a00f 5359 tcg_gen_movi_tl(cpu_ca, 1);
7487953d
AJ
5360 gen_set_label(l2);
5361 tcg_temp_free(t1);
5362 tcg_temp_free(t2);
76a66253 5363 if (unlikely(Rc(ctx->opcode) != 0))
7487953d 5364 gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
76a66253
JM
5365}
5366
5367/* sre - sre. */
99e300ef 5368static void gen_sre(DisasContext *ctx)
76a66253 5369{
7487953d
AJ
5370 TCGv t0 = tcg_temp_new();
5371 TCGv t1 = tcg_temp_new();
5372 tcg_gen_andi_tl(t1, cpu_gpr[rB(ctx->opcode)], 0x1F);
5373 tcg_gen_shr_tl(t0, cpu_gpr[rS(ctx->opcode)], t1);
5374 tcg_gen_subfi_tl(t1, 32, t1);
5375 tcg_gen_shl_tl(t1, cpu_gpr[rS(ctx->opcode)], t1);
5376 tcg_gen_or_tl(t1, t0, t1);
5377 tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], t0);
5378 gen_store_spr(SPR_MQ, t1);
5379 tcg_temp_free(t0);
5380 tcg_temp_free(t1);
76a66253 5381 if (unlikely(Rc(ctx->opcode) != 0))
7487953d 5382 gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
76a66253
JM
5383}
5384
5385/* srea - srea. */
99e300ef 5386static void gen_srea(DisasContext *ctx)
76a66253 5387{
7487953d
AJ
5388 TCGv t0 = tcg_temp_new();
5389 TCGv t1 = tcg_temp_new();
5390 tcg_gen_andi_tl(t1, cpu_gpr[rB(ctx->opcode)], 0x1F);
5391 tcg_gen_rotr_tl(t0, cpu_gpr[rS(ctx->opcode)], t1);
5392 gen_store_spr(SPR_MQ, t0);
5393 tcg_gen_sar_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)], t1);
5394 tcg_temp_free(t0);
5395 tcg_temp_free(t1);
76a66253 5396 if (unlikely(Rc(ctx->opcode) != 0))
7487953d 5397 gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
76a66253
JM
5398}
5399
5400/* sreq */
99e300ef 5401static void gen_sreq(DisasContext *ctx)
76a66253 5402{
7487953d
AJ
5403 TCGv t0 = tcg_temp_new();
5404 TCGv t1 = tcg_temp_new();
5405 TCGv t2 = tcg_temp_new();
5406 tcg_gen_andi_tl(t0, cpu_gpr[rB(ctx->opcode)], 0x1F);
5407 tcg_gen_movi_tl(t1, 0xFFFFFFFF);
5408 tcg_gen_shr_tl(t1, t1, t0);
5409 tcg_gen_rotr_tl(t0, cpu_gpr[rS(ctx->opcode)], t0);
5410 gen_load_spr(t2, SPR_MQ);
5411 gen_store_spr(SPR_MQ, t0);
5412 tcg_gen_and_tl(t0, t0, t1);
5413 tcg_gen_andc_tl(t2, t2, t1);
5414 tcg_gen_or_tl(cpu_gpr[rA(ctx->opcode)], t0, t2);
5415 tcg_temp_free(t0);
5416 tcg_temp_free(t1);
5417 tcg_temp_free(t2);
76a66253 5418 if (unlikely(Rc(ctx->opcode) != 0))
7487953d 5419 gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
76a66253
JM
5420}
5421
5422/* sriq */
99e300ef 5423static void gen_sriq(DisasContext *ctx)
76a66253 5424{
7487953d
AJ
5425 int sh = SH(ctx->opcode);
5426 TCGv t0 = tcg_temp_new();
5427 TCGv t1 = tcg_temp_new();
5428 tcg_gen_shri_tl(t0, cpu_gpr[rS(ctx->opcode)], sh);
5429 tcg_gen_shli_tl(t1, cpu_gpr[rS(ctx->opcode)], 32 - sh);
5430 tcg_gen_or_tl(t1, t0, t1);
5431 tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], t0);
5432 gen_store_spr(SPR_MQ, t1);
5433 tcg_temp_free(t0);
5434 tcg_temp_free(t1);
76a66253 5435 if (unlikely(Rc(ctx->opcode) != 0))
7487953d 5436 gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
76a66253
JM
5437}
5438
5439/* srliq */
99e300ef 5440static void gen_srliq(DisasContext *ctx)
76a66253 5441{
7487953d
AJ
5442 int sh = SH(ctx->opcode);
5443 TCGv t0 = tcg_temp_new();
5444 TCGv t1 = tcg_temp_new();
5445 tcg_gen_rotri_tl(t0, cpu_gpr[rS(ctx->opcode)], sh);
5446 gen_load_spr(t1, SPR_MQ);
5447 gen_store_spr(SPR_MQ, t0);
5448 tcg_gen_andi_tl(t0, t0, (0xFFFFFFFFU >> sh));
5449 tcg_gen_andi_tl(t1, t1, ~(0xFFFFFFFFU >> sh));
5450 tcg_gen_or_tl(cpu_gpr[rA(ctx->opcode)], t0, t1);
5451 tcg_temp_free(t0);
5452 tcg_temp_free(t1);
76a66253 5453 if (unlikely(Rc(ctx->opcode) != 0))
7487953d 5454 gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
76a66253
JM
5455}
5456
5457/* srlq */
99e300ef 5458static void gen_srlq(DisasContext *ctx)
76a66253 5459{
7487953d
AJ
5460 int l1 = gen_new_label();
5461 int l2 = gen_new_label();
5462 TCGv t0 = tcg_temp_local_new();
5463 TCGv t1 = tcg_temp_local_new();
5464 TCGv t2 = tcg_temp_local_new();
5465 tcg_gen_andi_tl(t2, cpu_gpr[rB(ctx->opcode)], 0x1F);
5466 tcg_gen_movi_tl(t1, 0xFFFFFFFF);
5467 tcg_gen_shr_tl(t2, t1, t2);
5468 tcg_gen_andi_tl(t0, cpu_gpr[rB(ctx->opcode)], 0x20);
5469 tcg_gen_brcondi_tl(TCG_COND_EQ, t0, 0, l1);
5470 gen_load_spr(t0, SPR_MQ);
5471 tcg_gen_and_tl(cpu_gpr[rA(ctx->opcode)], t0, t2);
5472 tcg_gen_br(l2);
5473 gen_set_label(l1);
5474 tcg_gen_shr_tl(t0, cpu_gpr[rS(ctx->opcode)], t2);
5475 tcg_gen_and_tl(t0, t0, t2);
5476 gen_load_spr(t1, SPR_MQ);
5477 tcg_gen_andc_tl(t1, t1, t2);
5478 tcg_gen_or_tl(cpu_gpr[rA(ctx->opcode)], t0, t1);
5479 gen_set_label(l2);
5480 tcg_temp_free(t0);
5481 tcg_temp_free(t1);
5482 tcg_temp_free(t2);
76a66253 5483 if (unlikely(Rc(ctx->opcode) != 0))
7487953d 5484 gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
76a66253
JM
5485}
5486
5487/* srq */
99e300ef 5488static void gen_srq(DisasContext *ctx)
76a66253 5489{
7487953d
AJ
5490 int l1 = gen_new_label();
5491 TCGv t0 = tcg_temp_new();
5492 TCGv t1 = tcg_temp_new();
5493 tcg_gen_andi_tl(t1, cpu_gpr[rB(ctx->opcode)], 0x1F);
5494 tcg_gen_shr_tl(t0, cpu_gpr[rS(ctx->opcode)], t1);
5495 tcg_gen_subfi_tl(t1, 32, t1);
5496 tcg_gen_shl_tl(t1, cpu_gpr[rS(ctx->opcode)], t1);
5497 tcg_gen_or_tl(t1, t0, t1);
5498 gen_store_spr(SPR_MQ, t1);
5499 tcg_gen_andi_tl(t1, cpu_gpr[rB(ctx->opcode)], 0x20);
5500 tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], t0);
5501 tcg_gen_brcondi_tl(TCG_COND_EQ, t0, 0, l1);
5502 tcg_gen_movi_tl(cpu_gpr[rA(ctx->opcode)], 0);
5503 gen_set_label(l1);
5504 tcg_temp_free(t0);
5505 tcg_temp_free(t1);
76a66253 5506 if (unlikely(Rc(ctx->opcode) != 0))
7487953d 5507 gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
76a66253
JM
5508}
5509
5510/* PowerPC 602 specific instructions */
99e300ef 5511
54623277 5512/* dsa */
99e300ef 5513static void gen_dsa(DisasContext *ctx)
76a66253
JM
5514{
5515 /* XXX: TODO */
e06fcd75 5516 gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL);
76a66253
JM
5517}
5518
5519/* esa */
99e300ef 5520static void gen_esa(DisasContext *ctx)
76a66253
JM
5521{
5522 /* XXX: TODO */
e06fcd75 5523 gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL);
76a66253
JM
5524}
5525
5526/* mfrom */
99e300ef 5527static void gen_mfrom(DisasContext *ctx)
76a66253
JM
5528{
5529#if defined(CONFIG_USER_ONLY)
e06fcd75 5530 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
76a66253 5531#else
76db3ba4 5532 if (unlikely(!ctx->mem_idx)) {
e06fcd75 5533 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
76a66253
JM
5534 return;
5535 }
cf02a65c 5536 gen_helper_602_mfrom(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)]);
76a66253
JM
5537#endif
5538}
5539
5540/* 602 - 603 - G2 TLB management */
e8eaa2c0 5541
54623277 5542/* tlbld */
e8eaa2c0 5543static void gen_tlbld_6xx(DisasContext *ctx)
76a66253
JM
5544{
5545#if defined(CONFIG_USER_ONLY)
e06fcd75 5546 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
76a66253 5547#else
76db3ba4 5548 if (unlikely(!ctx->mem_idx)) {
e06fcd75 5549 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
76a66253
JM
5550 return;
5551 }
c6c7cf05 5552 gen_helper_6xx_tlbd(cpu_env, cpu_gpr[rB(ctx->opcode)]);
76a66253
JM
5553#endif
5554}
5555
5556/* tlbli */
e8eaa2c0 5557static void gen_tlbli_6xx(DisasContext *ctx)
76a66253
JM
5558{
5559#if defined(CONFIG_USER_ONLY)
e06fcd75 5560 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
76a66253 5561#else
76db3ba4 5562 if (unlikely(!ctx->mem_idx)) {
e06fcd75 5563 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
76a66253
JM
5564 return;
5565 }
c6c7cf05 5566 gen_helper_6xx_tlbi(cpu_env, cpu_gpr[rB(ctx->opcode)]);
76a66253
JM
5567#endif
5568}
5569
7dbe11ac 5570/* 74xx TLB management */
e8eaa2c0 5571
54623277 5572/* tlbld */
e8eaa2c0 5573static void gen_tlbld_74xx(DisasContext *ctx)
7dbe11ac
JM
5574{
5575#if defined(CONFIG_USER_ONLY)
e06fcd75 5576 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
7dbe11ac 5577#else
76db3ba4 5578 if (unlikely(!ctx->mem_idx)) {
e06fcd75 5579 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
7dbe11ac
JM
5580 return;
5581 }
c6c7cf05 5582 gen_helper_74xx_tlbd(cpu_env, cpu_gpr[rB(ctx->opcode)]);
7dbe11ac
JM
5583#endif
5584}
5585
5586/* tlbli */
e8eaa2c0 5587static void gen_tlbli_74xx(DisasContext *ctx)
7dbe11ac
JM
5588{
5589#if defined(CONFIG_USER_ONLY)
e06fcd75 5590 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
7dbe11ac 5591#else
76db3ba4 5592 if (unlikely(!ctx->mem_idx)) {
e06fcd75 5593 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
7dbe11ac
JM
5594 return;
5595 }
c6c7cf05 5596 gen_helper_74xx_tlbi(cpu_env, cpu_gpr[rB(ctx->opcode)]);
7dbe11ac
JM
5597#endif
5598}
5599
76a66253 5600/* POWER instructions not in PowerPC 601 */
99e300ef 5601
54623277 5602/* clf */
99e300ef 5603static void gen_clf(DisasContext *ctx)
76a66253
JM
5604{
5605 /* Cache line flush: implemented as no-op */
5606}
5607
5608/* cli */
99e300ef 5609static void gen_cli(DisasContext *ctx)
76a66253 5610{
7f75ffd3 5611 /* Cache line invalidate: privileged and treated as no-op */
76a66253 5612#if defined(CONFIG_USER_ONLY)
e06fcd75 5613 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
76a66253 5614#else
76db3ba4 5615 if (unlikely(!ctx->mem_idx)) {
e06fcd75 5616 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
76a66253
JM
5617 return;
5618 }
5619#endif
5620}
5621
5622/* dclst */
99e300ef 5623static void gen_dclst(DisasContext *ctx)
76a66253
JM
5624{
5625 /* Data cache line store: treated as no-op */
5626}
5627
99e300ef 5628static void gen_mfsri(DisasContext *ctx)
76a66253
JM
5629{
5630#if defined(CONFIG_USER_ONLY)
e06fcd75 5631 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
76a66253 5632#else
74d37793
AJ
5633 int ra = rA(ctx->opcode);
5634 int rd = rD(ctx->opcode);
5635 TCGv t0;
76db3ba4 5636 if (unlikely(!ctx->mem_idx)) {
e06fcd75 5637 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
76a66253
JM
5638 return;
5639 }
74d37793 5640 t0 = tcg_temp_new();
76db3ba4 5641 gen_addr_reg_index(ctx, t0);
74d37793
AJ
5642 tcg_gen_shri_tl(t0, t0, 28);
5643 tcg_gen_andi_tl(t0, t0, 0xF);
c6c7cf05 5644 gen_helper_load_sr(cpu_gpr[rd], cpu_env, t0);
74d37793 5645 tcg_temp_free(t0);
76a66253 5646 if (ra != 0 && ra != rd)
74d37793 5647 tcg_gen_mov_tl(cpu_gpr[ra], cpu_gpr[rd]);
76a66253
JM
5648#endif
5649}
5650
99e300ef 5651static void gen_rac(DisasContext *ctx)
76a66253
JM
5652{
5653#if defined(CONFIG_USER_ONLY)
e06fcd75 5654 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
76a66253 5655#else
22e0e173 5656 TCGv t0;
76db3ba4 5657 if (unlikely(!ctx->mem_idx)) {
e06fcd75 5658 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
76a66253
JM
5659 return;
5660 }
22e0e173 5661 t0 = tcg_temp_new();
76db3ba4 5662 gen_addr_reg_index(ctx, t0);
c6c7cf05 5663 gen_helper_rac(cpu_gpr[rD(ctx->opcode)], cpu_env, t0);
22e0e173 5664 tcg_temp_free(t0);
76a66253
JM
5665#endif
5666}
5667
99e300ef 5668static void gen_rfsvc(DisasContext *ctx)
76a66253
JM
5669{
5670#if defined(CONFIG_USER_ONLY)
e06fcd75 5671 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
76a66253 5672#else
76db3ba4 5673 if (unlikely(!ctx->mem_idx)) {
e06fcd75 5674 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
76a66253
JM
5675 return;
5676 }
e5f17ac6 5677 gen_helper_rfsvc(cpu_env);
e06fcd75 5678 gen_sync_exception(ctx);
76a66253
JM
5679#endif
5680}
5681
5682/* svc is not implemented for now */
5683
5684/* POWER2 specific instructions */
5685/* Quad manipulation (load/store two floats at a time) */
76a66253
JM
5686
5687/* lfq */
99e300ef 5688static void gen_lfq(DisasContext *ctx)
76a66253 5689{
01a4afeb 5690 int rd = rD(ctx->opcode);
76db3ba4
AJ
5691 TCGv t0;
5692 gen_set_access_type(ctx, ACCESS_FLOAT);
5693 t0 = tcg_temp_new();
5694 gen_addr_imm_index(ctx, t0, 0);
5695 gen_qemu_ld64(ctx, cpu_fpr[rd], t0);
5696 gen_addr_add(ctx, t0, t0, 8);
5697 gen_qemu_ld64(ctx, cpu_fpr[(rd + 1) % 32], t0);
01a4afeb 5698 tcg_temp_free(t0);
76a66253
JM
5699}
5700
5701/* lfqu */
99e300ef 5702static void gen_lfqu(DisasContext *ctx)
76a66253
JM
5703{
5704 int ra = rA(ctx->opcode);
01a4afeb 5705 int rd = rD(ctx->opcode);
76db3ba4
AJ
5706 TCGv t0, t1;
5707 gen_set_access_type(ctx, ACCESS_FLOAT);
5708 t0 = tcg_temp_new();
5709 t1 = tcg_temp_new();
5710 gen_addr_imm_index(ctx, t0, 0);
5711 gen_qemu_ld64(ctx, cpu_fpr[rd], t0);
5712 gen_addr_add(ctx, t1, t0, 8);
5713 gen_qemu_ld64(ctx, cpu_fpr[(rd + 1) % 32], t1);
76a66253 5714 if (ra != 0)
01a4afeb
AJ
5715 tcg_gen_mov_tl(cpu_gpr[ra], t0);
5716 tcg_temp_free(t0);
5717 tcg_temp_free(t1);
76a66253
JM
5718}
5719
5720/* lfqux */
99e300ef 5721static void gen_lfqux(DisasContext *ctx)
76a66253
JM
5722{
5723 int ra = rA(ctx->opcode);
01a4afeb 5724 int rd = rD(ctx->opcode);
76db3ba4
AJ
5725 gen_set_access_type(ctx, ACCESS_FLOAT);
5726 TCGv t0, t1;
5727 t0 = tcg_temp_new();
5728 gen_addr_reg_index(ctx, t0);
5729 gen_qemu_ld64(ctx, cpu_fpr[rd], t0);
5730 t1 = tcg_temp_new();
5731 gen_addr_add(ctx, t1, t0, 8);
5732 gen_qemu_ld64(ctx, cpu_fpr[(rd + 1) % 32], t1);
5733 tcg_temp_free(t1);
76a66253 5734 if (ra != 0)
01a4afeb
AJ
5735 tcg_gen_mov_tl(cpu_gpr[ra], t0);
5736 tcg_temp_free(t0);
76a66253
JM
5737}
5738
5739/* lfqx */
99e300ef 5740static void gen_lfqx(DisasContext *ctx)
76a66253 5741{
01a4afeb 5742 int rd = rD(ctx->opcode);
76db3ba4
AJ
5743 TCGv t0;
5744 gen_set_access_type(ctx, ACCESS_FLOAT);
5745 t0 = tcg_temp_new();
5746 gen_addr_reg_index(ctx, t0);
5747 gen_qemu_ld64(ctx, cpu_fpr[rd], t0);
5748 gen_addr_add(ctx, t0, t0, 8);
5749 gen_qemu_ld64(ctx, cpu_fpr[(rd + 1) % 32], t0);
01a4afeb 5750 tcg_temp_free(t0);
76a66253
JM
5751}
5752
5753/* stfq */
99e300ef 5754static void gen_stfq(DisasContext *ctx)
76a66253 5755{
01a4afeb 5756 int rd = rD(ctx->opcode);
76db3ba4
AJ
5757 TCGv t0;
5758 gen_set_access_type(ctx, ACCESS_FLOAT);
5759 t0 = tcg_temp_new();
5760 gen_addr_imm_index(ctx, t0, 0);
5761 gen_qemu_st64(ctx, cpu_fpr[rd], t0);
5762 gen_addr_add(ctx, t0, t0, 8);
5763 gen_qemu_st64(ctx, cpu_fpr[(rd + 1) % 32], t0);
01a4afeb 5764 tcg_temp_free(t0);
76a66253
JM
5765}
5766
5767/* stfqu */
99e300ef 5768static void gen_stfqu(DisasContext *ctx)
76a66253
JM
5769{
5770 int ra = rA(ctx->opcode);
01a4afeb 5771 int rd = rD(ctx->opcode);
76db3ba4
AJ
5772 TCGv t0, t1;
5773 gen_set_access_type(ctx, ACCESS_FLOAT);
5774 t0 = tcg_temp_new();
5775 gen_addr_imm_index(ctx, t0, 0);
5776 gen_qemu_st64(ctx, cpu_fpr[rd], t0);
5777 t1 = tcg_temp_new();
5778 gen_addr_add(ctx, t1, t0, 8);
5779 gen_qemu_st64(ctx, cpu_fpr[(rd + 1) % 32], t1);
5780 tcg_temp_free(t1);
76a66253 5781 if (ra != 0)
01a4afeb
AJ
5782 tcg_gen_mov_tl(cpu_gpr[ra], t0);
5783 tcg_temp_free(t0);
76a66253
JM
5784}
5785
5786/* stfqux */
99e300ef 5787static void gen_stfqux(DisasContext *ctx)
76a66253
JM
5788{
5789 int ra = rA(ctx->opcode);
01a4afeb 5790 int rd = rD(ctx->opcode);
76db3ba4
AJ
5791 TCGv t0, t1;
5792 gen_set_access_type(ctx, ACCESS_FLOAT);
5793 t0 = tcg_temp_new();
5794 gen_addr_reg_index(ctx, t0);
5795 gen_qemu_st64(ctx, cpu_fpr[rd], t0);
5796 t1 = tcg_temp_new();
5797 gen_addr_add(ctx, t1, t0, 8);
5798 gen_qemu_st64(ctx, cpu_fpr[(rd + 1) % 32], t1);
5799 tcg_temp_free(t1);
76a66253 5800 if (ra != 0)
01a4afeb
AJ
5801 tcg_gen_mov_tl(cpu_gpr[ra], t0);
5802 tcg_temp_free(t0);
76a66253
JM
5803}
5804
5805/* stfqx */
99e300ef 5806static void gen_stfqx(DisasContext *ctx)
76a66253 5807{
01a4afeb 5808 int rd = rD(ctx->opcode);
76db3ba4
AJ
5809 TCGv t0;
5810 gen_set_access_type(ctx, ACCESS_FLOAT);
5811 t0 = tcg_temp_new();
5812 gen_addr_reg_index(ctx, t0);
5813 gen_qemu_st64(ctx, cpu_fpr[rd], t0);
5814 gen_addr_add(ctx, t0, t0, 8);
5815 gen_qemu_st64(ctx, cpu_fpr[(rd + 1) % 32], t0);
01a4afeb 5816 tcg_temp_free(t0);
76a66253
JM
5817}
5818
5819/* BookE specific instructions */
99e300ef 5820
54623277 5821/* XXX: not implemented on 440 ? */
99e300ef 5822static void gen_mfapidi(DisasContext *ctx)
76a66253
JM
5823{
5824 /* XXX: TODO */
e06fcd75 5825 gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL);
76a66253
JM
5826}
5827
2662a059 5828/* XXX: not implemented on 440 ? */
99e300ef 5829static void gen_tlbiva(DisasContext *ctx)
76a66253
JM
5830{
5831#if defined(CONFIG_USER_ONLY)
e06fcd75 5832 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
76a66253 5833#else
74d37793 5834 TCGv t0;
76db3ba4 5835 if (unlikely(!ctx->mem_idx)) {
e06fcd75 5836 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
76a66253
JM
5837 return;
5838 }
ec72e276 5839 t0 = tcg_temp_new();
76db3ba4 5840 gen_addr_reg_index(ctx, t0);
c6c7cf05 5841 gen_helper_tlbie(cpu_env, cpu_gpr[rB(ctx->opcode)]);
74d37793 5842 tcg_temp_free(t0);
76a66253
JM
5843#endif
5844}
5845
5846/* All 405 MAC instructions are translated here */
636aa200
BS
5847static inline void gen_405_mulladd_insn(DisasContext *ctx, int opc2, int opc3,
5848 int ra, int rb, int rt, int Rc)
76a66253 5849{
182608d4
AJ
5850 TCGv t0, t1;
5851
a7812ae4
PB
5852 t0 = tcg_temp_local_new();
5853 t1 = tcg_temp_local_new();
182608d4 5854
76a66253
JM
5855 switch (opc3 & 0x0D) {
5856 case 0x05:
5857 /* macchw - macchw. - macchwo - macchwo. */
5858 /* macchws - macchws. - macchwso - macchwso. */
5859 /* nmacchw - nmacchw. - nmacchwo - nmacchwo. */
5860 /* nmacchws - nmacchws. - nmacchwso - nmacchwso. */
5861 /* mulchw - mulchw. */
182608d4
AJ
5862 tcg_gen_ext16s_tl(t0, cpu_gpr[ra]);
5863 tcg_gen_sari_tl(t1, cpu_gpr[rb], 16);
5864 tcg_gen_ext16s_tl(t1, t1);
76a66253
JM
5865 break;
5866 case 0x04:
5867 /* macchwu - macchwu. - macchwuo - macchwuo. */
5868 /* macchwsu - macchwsu. - macchwsuo - macchwsuo. */
5869 /* mulchwu - mulchwu. */
182608d4
AJ
5870 tcg_gen_ext16u_tl(t0, cpu_gpr[ra]);
5871 tcg_gen_shri_tl(t1, cpu_gpr[rb], 16);
5872 tcg_gen_ext16u_tl(t1, t1);
76a66253
JM
5873 break;
5874 case 0x01:
5875 /* machhw - machhw. - machhwo - machhwo. */
5876 /* machhws - machhws. - machhwso - machhwso. */
5877 /* nmachhw - nmachhw. - nmachhwo - nmachhwo. */
5878 /* nmachhws - nmachhws. - nmachhwso - nmachhwso. */
5879 /* mulhhw - mulhhw. */
182608d4
AJ
5880 tcg_gen_sari_tl(t0, cpu_gpr[ra], 16);
5881 tcg_gen_ext16s_tl(t0, t0);
5882 tcg_gen_sari_tl(t1, cpu_gpr[rb], 16);
5883 tcg_gen_ext16s_tl(t1, t1);
76a66253
JM
5884 break;
5885 case 0x00:
5886 /* machhwu - machhwu. - machhwuo - machhwuo. */
5887 /* machhwsu - machhwsu. - machhwsuo - machhwsuo. */
5888 /* mulhhwu - mulhhwu. */
182608d4
AJ
5889 tcg_gen_shri_tl(t0, cpu_gpr[ra], 16);
5890 tcg_gen_ext16u_tl(t0, t0);
5891 tcg_gen_shri_tl(t1, cpu_gpr[rb], 16);
5892 tcg_gen_ext16u_tl(t1, t1);
76a66253
JM
5893 break;
5894 case 0x0D:
5895 /* maclhw - maclhw. - maclhwo - maclhwo. */
5896 /* maclhws - maclhws. - maclhwso - maclhwso. */
5897 /* nmaclhw - nmaclhw. - nmaclhwo - nmaclhwo. */
5898 /* nmaclhws - nmaclhws. - nmaclhwso - nmaclhwso. */
5899 /* mullhw - mullhw. */
182608d4
AJ
5900 tcg_gen_ext16s_tl(t0, cpu_gpr[ra]);
5901 tcg_gen_ext16s_tl(t1, cpu_gpr[rb]);
76a66253
JM
5902 break;
5903 case 0x0C:
5904 /* maclhwu - maclhwu. - maclhwuo - maclhwuo. */
5905 /* maclhwsu - maclhwsu. - maclhwsuo - maclhwsuo. */
5906 /* mullhwu - mullhwu. */
182608d4
AJ
5907 tcg_gen_ext16u_tl(t0, cpu_gpr[ra]);
5908 tcg_gen_ext16u_tl(t1, cpu_gpr[rb]);
76a66253
JM
5909 break;
5910 }
76a66253 5911 if (opc2 & 0x04) {
182608d4
AJ
5912 /* (n)multiply-and-accumulate (0x0C / 0x0E) */
5913 tcg_gen_mul_tl(t1, t0, t1);
5914 if (opc2 & 0x02) {
5915 /* nmultiply-and-accumulate (0x0E) */
5916 tcg_gen_sub_tl(t0, cpu_gpr[rt], t1);
5917 } else {
5918 /* multiply-and-accumulate (0x0C) */
5919 tcg_gen_add_tl(t0, cpu_gpr[rt], t1);
5920 }
5921
5922 if (opc3 & 0x12) {
5923 /* Check overflow and/or saturate */
5924 int l1 = gen_new_label();
5925
5926 if (opc3 & 0x10) {
5927 /* Start with XER OV disabled, the most likely case */
da91a00f 5928 tcg_gen_movi_tl(cpu_ov, 0);
182608d4
AJ
5929 }
5930 if (opc3 & 0x01) {
5931 /* Signed */
5932 tcg_gen_xor_tl(t1, cpu_gpr[rt], t1);
5933 tcg_gen_brcondi_tl(TCG_COND_GE, t1, 0, l1);
5934 tcg_gen_xor_tl(t1, cpu_gpr[rt], t0);
5935 tcg_gen_brcondi_tl(TCG_COND_LT, t1, 0, l1);
bdc4e053 5936 if (opc3 & 0x02) {
182608d4
AJ
5937 /* Saturate */
5938 tcg_gen_sari_tl(t0, cpu_gpr[rt], 31);
5939 tcg_gen_xori_tl(t0, t0, 0x7fffffff);
5940 }
5941 } else {
5942 /* Unsigned */
5943 tcg_gen_brcond_tl(TCG_COND_GEU, t0, t1, l1);
bdc4e053 5944 if (opc3 & 0x02) {
182608d4
AJ
5945 /* Saturate */
5946 tcg_gen_movi_tl(t0, UINT32_MAX);
5947 }
5948 }
5949 if (opc3 & 0x10) {
5950 /* Check overflow */
da91a00f
RH
5951 tcg_gen_movi_tl(cpu_ov, 1);
5952 tcg_gen_movi_tl(cpu_so, 1);
182608d4
AJ
5953 }
5954 gen_set_label(l1);
5955 tcg_gen_mov_tl(cpu_gpr[rt], t0);
5956 }
5957 } else {
5958 tcg_gen_mul_tl(cpu_gpr[rt], t0, t1);
76a66253 5959 }
182608d4
AJ
5960 tcg_temp_free(t0);
5961 tcg_temp_free(t1);
76a66253
JM
5962 if (unlikely(Rc) != 0) {
5963 /* Update Rc0 */
182608d4 5964 gen_set_Rc0(ctx, cpu_gpr[rt]);
76a66253
JM
5965 }
5966}
5967
a750fc0b 5968#define GEN_MAC_HANDLER(name, opc2, opc3) \
99e300ef 5969static void glue(gen_, name)(DisasContext *ctx) \
76a66253
JM
5970{ \
5971 gen_405_mulladd_insn(ctx, opc2, opc3, rA(ctx->opcode), rB(ctx->opcode), \
5972 rD(ctx->opcode), Rc(ctx->opcode)); \
5973}
5974
5975/* macchw - macchw. */
a750fc0b 5976GEN_MAC_HANDLER(macchw, 0x0C, 0x05);
76a66253 5977/* macchwo - macchwo. */
a750fc0b 5978GEN_MAC_HANDLER(macchwo, 0x0C, 0x15);
76a66253 5979/* macchws - macchws. */
a750fc0b 5980GEN_MAC_HANDLER(macchws, 0x0C, 0x07);
76a66253 5981/* macchwso - macchwso. */
a750fc0b 5982GEN_MAC_HANDLER(macchwso, 0x0C, 0x17);
76a66253 5983/* macchwsu - macchwsu. */
a750fc0b 5984GEN_MAC_HANDLER(macchwsu, 0x0C, 0x06);
76a66253 5985/* macchwsuo - macchwsuo. */
a750fc0b 5986GEN_MAC_HANDLER(macchwsuo, 0x0C, 0x16);
76a66253 5987/* macchwu - macchwu. */
a750fc0b 5988GEN_MAC_HANDLER(macchwu, 0x0C, 0x04);
76a66253 5989/* macchwuo - macchwuo. */
a750fc0b 5990GEN_MAC_HANDLER(macchwuo, 0x0C, 0x14);
76a66253 5991/* machhw - machhw. */
a750fc0b 5992GEN_MAC_HANDLER(machhw, 0x0C, 0x01);
76a66253 5993/* machhwo - machhwo. */
a750fc0b 5994GEN_MAC_HANDLER(machhwo, 0x0C, 0x11);
76a66253 5995/* machhws - machhws. */
a750fc0b 5996GEN_MAC_HANDLER(machhws, 0x0C, 0x03);
76a66253 5997/* machhwso - machhwso. */
a750fc0b 5998GEN_MAC_HANDLER(machhwso, 0x0C, 0x13);
76a66253 5999/* machhwsu - machhwsu. */
a750fc0b 6000GEN_MAC_HANDLER(machhwsu, 0x0C, 0x02);
76a66253 6001/* machhwsuo - machhwsuo. */
a750fc0b 6002GEN_MAC_HANDLER(machhwsuo, 0x0C, 0x12);
76a66253 6003/* machhwu - machhwu. */
a750fc0b 6004GEN_MAC_HANDLER(machhwu, 0x0C, 0x00);
76a66253 6005/* machhwuo - machhwuo. */
a750fc0b 6006GEN_MAC_HANDLER(machhwuo, 0x0C, 0x10);
76a66253 6007/* maclhw - maclhw. */
a750fc0b 6008GEN_MAC_HANDLER(maclhw, 0x0C, 0x0D);
76a66253 6009/* maclhwo - maclhwo. */
a750fc0b 6010GEN_MAC_HANDLER(maclhwo, 0x0C, 0x1D);
76a66253 6011/* maclhws - maclhws. */
a750fc0b 6012GEN_MAC_HANDLER(maclhws, 0x0C, 0x0F);
76a66253 6013/* maclhwso - maclhwso. */
a750fc0b 6014GEN_MAC_HANDLER(maclhwso, 0x0C, 0x1F);
76a66253 6015/* maclhwu - maclhwu. */
a750fc0b 6016GEN_MAC_HANDLER(maclhwu, 0x0C, 0x0C);
76a66253 6017/* maclhwuo - maclhwuo. */
a750fc0b 6018GEN_MAC_HANDLER(maclhwuo, 0x0C, 0x1C);
76a66253 6019/* maclhwsu - maclhwsu. */
a750fc0b 6020GEN_MAC_HANDLER(maclhwsu, 0x0C, 0x0E);
76a66253 6021/* maclhwsuo - maclhwsuo. */
a750fc0b 6022GEN_MAC_HANDLER(maclhwsuo, 0x0C, 0x1E);
76a66253 6023/* nmacchw - nmacchw. */
a750fc0b 6024GEN_MAC_HANDLER(nmacchw, 0x0E, 0x05);
76a66253 6025/* nmacchwo - nmacchwo. */
a750fc0b 6026GEN_MAC_HANDLER(nmacchwo, 0x0E, 0x15);
76a66253 6027/* nmacchws - nmacchws. */
a750fc0b 6028GEN_MAC_HANDLER(nmacchws, 0x0E, 0x07);
76a66253 6029/* nmacchwso - nmacchwso. */
a750fc0b 6030GEN_MAC_HANDLER(nmacchwso, 0x0E, 0x17);
76a66253 6031/* nmachhw - nmachhw. */
a750fc0b 6032GEN_MAC_HANDLER(nmachhw, 0x0E, 0x01);
76a66253 6033/* nmachhwo - nmachhwo. */
a750fc0b 6034GEN_MAC_HANDLER(nmachhwo, 0x0E, 0x11);
76a66253 6035/* nmachhws - nmachhws. */
a750fc0b 6036GEN_MAC_HANDLER(nmachhws, 0x0E, 0x03);
76a66253 6037/* nmachhwso - nmachhwso. */
a750fc0b 6038GEN_MAC_HANDLER(nmachhwso, 0x0E, 0x13);
76a66253 6039/* nmaclhw - nmaclhw. */
a750fc0b 6040GEN_MAC_HANDLER(nmaclhw, 0x0E, 0x0D);
76a66253 6041/* nmaclhwo - nmaclhwo. */
a750fc0b 6042GEN_MAC_HANDLER(nmaclhwo, 0x0E, 0x1D);
76a66253 6043/* nmaclhws - nmaclhws. */
a750fc0b 6044GEN_MAC_HANDLER(nmaclhws, 0x0E, 0x0F);
76a66253 6045/* nmaclhwso - nmaclhwso. */
a750fc0b 6046GEN_MAC_HANDLER(nmaclhwso, 0x0E, 0x1F);
76a66253
JM
6047
6048/* mulchw - mulchw. */
a750fc0b 6049GEN_MAC_HANDLER(mulchw, 0x08, 0x05);
76a66253 6050/* mulchwu - mulchwu. */
a750fc0b 6051GEN_MAC_HANDLER(mulchwu, 0x08, 0x04);
76a66253 6052/* mulhhw - mulhhw. */
a750fc0b 6053GEN_MAC_HANDLER(mulhhw, 0x08, 0x01);
76a66253 6054/* mulhhwu - mulhhwu. */
a750fc0b 6055GEN_MAC_HANDLER(mulhhwu, 0x08, 0x00);
76a66253 6056/* mullhw - mullhw. */
a750fc0b 6057GEN_MAC_HANDLER(mullhw, 0x08, 0x0D);
76a66253 6058/* mullhwu - mullhwu. */
a750fc0b 6059GEN_MAC_HANDLER(mullhwu, 0x08, 0x0C);
76a66253
JM
6060
6061/* mfdcr */
99e300ef 6062static void gen_mfdcr(DisasContext *ctx)
76a66253
JM
6063{
6064#if defined(CONFIG_USER_ONLY)
e06fcd75 6065 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
76a66253 6066#else
06dca6a7 6067 TCGv dcrn;
76db3ba4 6068 if (unlikely(!ctx->mem_idx)) {
e06fcd75 6069 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
76a66253
JM
6070 return;
6071 }
06dca6a7
AJ
6072 /* NIP cannot be restored if the memory exception comes from an helper */
6073 gen_update_nip(ctx, ctx->nip - 4);
6074 dcrn = tcg_const_tl(SPR(ctx->opcode));
d0f1562d 6075 gen_helper_load_dcr(cpu_gpr[rD(ctx->opcode)], cpu_env, dcrn);
06dca6a7 6076 tcg_temp_free(dcrn);
76a66253
JM
6077#endif
6078}
6079
6080/* mtdcr */
99e300ef 6081static void gen_mtdcr(DisasContext *ctx)
76a66253
JM
6082{
6083#if defined(CONFIG_USER_ONLY)
e06fcd75 6084 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
76a66253 6085#else
06dca6a7 6086 TCGv dcrn;
76db3ba4 6087 if (unlikely(!ctx->mem_idx)) {
e06fcd75 6088 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
76a66253
JM
6089 return;
6090 }
06dca6a7
AJ
6091 /* NIP cannot be restored if the memory exception comes from an helper */
6092 gen_update_nip(ctx, ctx->nip - 4);
6093 dcrn = tcg_const_tl(SPR(ctx->opcode));
d0f1562d 6094 gen_helper_store_dcr(cpu_env, dcrn, cpu_gpr[rS(ctx->opcode)]);
06dca6a7 6095 tcg_temp_free(dcrn);
a42bd6cc
JM
6096#endif
6097}
6098
6099/* mfdcrx */
2662a059 6100/* XXX: not implemented on 440 ? */
99e300ef 6101static void gen_mfdcrx(DisasContext *ctx)
a42bd6cc
JM
6102{
6103#if defined(CONFIG_USER_ONLY)
e06fcd75 6104 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
a42bd6cc 6105#else
76db3ba4 6106 if (unlikely(!ctx->mem_idx)) {
e06fcd75 6107 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
a42bd6cc
JM
6108 return;
6109 }
06dca6a7
AJ
6110 /* NIP cannot be restored if the memory exception comes from an helper */
6111 gen_update_nip(ctx, ctx->nip - 4);
d0f1562d
BS
6112 gen_helper_load_dcr(cpu_gpr[rD(ctx->opcode)], cpu_env,
6113 cpu_gpr[rA(ctx->opcode)]);
a750fc0b 6114 /* Note: Rc update flag set leads to undefined state of Rc0 */
a42bd6cc
JM
6115#endif
6116}
6117
6118/* mtdcrx */
2662a059 6119/* XXX: not implemented on 440 ? */
99e300ef 6120static void gen_mtdcrx(DisasContext *ctx)
a42bd6cc
JM
6121{
6122#if defined(CONFIG_USER_ONLY)
e06fcd75 6123 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
a42bd6cc 6124#else
76db3ba4 6125 if (unlikely(!ctx->mem_idx)) {
e06fcd75 6126 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
a42bd6cc
JM
6127 return;
6128 }
06dca6a7
AJ
6129 /* NIP cannot be restored if the memory exception comes from an helper */
6130 gen_update_nip(ctx, ctx->nip - 4);
d0f1562d
BS
6131 gen_helper_store_dcr(cpu_env, cpu_gpr[rA(ctx->opcode)],
6132 cpu_gpr[rS(ctx->opcode)]);
a750fc0b 6133 /* Note: Rc update flag set leads to undefined state of Rc0 */
76a66253
JM
6134#endif
6135}
6136
a750fc0b 6137/* mfdcrux (PPC 460) : user-mode access to DCR */
99e300ef 6138static void gen_mfdcrux(DisasContext *ctx)
a750fc0b 6139{
06dca6a7
AJ
6140 /* NIP cannot be restored if the memory exception comes from an helper */
6141 gen_update_nip(ctx, ctx->nip - 4);
d0f1562d
BS
6142 gen_helper_load_dcr(cpu_gpr[rD(ctx->opcode)], cpu_env,
6143 cpu_gpr[rA(ctx->opcode)]);
a750fc0b
JM
6144 /* Note: Rc update flag set leads to undefined state of Rc0 */
6145}
6146
6147/* mtdcrux (PPC 460) : user-mode access to DCR */
99e300ef 6148static void gen_mtdcrux(DisasContext *ctx)
a750fc0b 6149{
06dca6a7
AJ
6150 /* NIP cannot be restored if the memory exception comes from an helper */
6151 gen_update_nip(ctx, ctx->nip - 4);
975e5463 6152 gen_helper_store_dcr(cpu_env, cpu_gpr[rA(ctx->opcode)],
d0f1562d 6153 cpu_gpr[rS(ctx->opcode)]);
a750fc0b
JM
6154 /* Note: Rc update flag set leads to undefined state of Rc0 */
6155}
6156
76a66253 6157/* dccci */
99e300ef 6158static void gen_dccci(DisasContext *ctx)
76a66253
JM
6159{
6160#if defined(CONFIG_USER_ONLY)
e06fcd75 6161 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
76a66253 6162#else
76db3ba4 6163 if (unlikely(!ctx->mem_idx)) {
e06fcd75 6164 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
76a66253
JM
6165 return;
6166 }
6167 /* interpreted as no-op */
6168#endif
6169}
6170
6171/* dcread */
99e300ef 6172static void gen_dcread(DisasContext *ctx)
76a66253
JM
6173{
6174#if defined(CONFIG_USER_ONLY)
e06fcd75 6175 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
76a66253 6176#else
b61f2753 6177 TCGv EA, val;
76db3ba4 6178 if (unlikely(!ctx->mem_idx)) {
e06fcd75 6179 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
76a66253
JM
6180 return;
6181 }
76db3ba4 6182 gen_set_access_type(ctx, ACCESS_CACHE);
a7812ae4 6183 EA = tcg_temp_new();
76db3ba4 6184 gen_addr_reg_index(ctx, EA);
a7812ae4 6185 val = tcg_temp_new();
76db3ba4 6186 gen_qemu_ld32u(ctx, val, EA);
b61f2753
AJ
6187 tcg_temp_free(val);
6188 tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], EA);
6189 tcg_temp_free(EA);
76a66253
JM
6190#endif
6191}
6192
6193/* icbt */
e8eaa2c0 6194static void gen_icbt_40x(DisasContext *ctx)
76a66253
JM
6195{
6196 /* interpreted as no-op */
6197 /* XXX: specification say this is treated as a load by the MMU
6198 * but does not generate any exception
6199 */
6200}
6201
6202/* iccci */
99e300ef 6203static void gen_iccci(DisasContext *ctx)
76a66253
JM
6204{
6205#if defined(CONFIG_USER_ONLY)
e06fcd75 6206 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
76a66253 6207#else
76db3ba4 6208 if (unlikely(!ctx->mem_idx)) {
e06fcd75 6209 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
76a66253
JM
6210 return;
6211 }
6212 /* interpreted as no-op */
6213#endif
6214}
6215
6216/* icread */
99e300ef 6217static void gen_icread(DisasContext *ctx)
76a66253
JM
6218{
6219#if defined(CONFIG_USER_ONLY)
e06fcd75 6220 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
76a66253 6221#else
76db3ba4 6222 if (unlikely(!ctx->mem_idx)) {
e06fcd75 6223 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
76a66253
JM
6224 return;
6225 }
6226 /* interpreted as no-op */
6227#endif
6228}
6229
76db3ba4 6230/* rfci (mem_idx only) */
e8eaa2c0 6231static void gen_rfci_40x(DisasContext *ctx)
a42bd6cc
JM
6232{
6233#if defined(CONFIG_USER_ONLY)
e06fcd75 6234 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
a42bd6cc 6235#else
76db3ba4 6236 if (unlikely(!ctx->mem_idx)) {
e06fcd75 6237 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
a42bd6cc
JM
6238 return;
6239 }
6240 /* Restore CPU state */
e5f17ac6 6241 gen_helper_40x_rfci(cpu_env);
e06fcd75 6242 gen_sync_exception(ctx);
a42bd6cc
JM
6243#endif
6244}
6245
99e300ef 6246static void gen_rfci(DisasContext *ctx)
a42bd6cc
JM
6247{
6248#if defined(CONFIG_USER_ONLY)
e06fcd75 6249 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
a42bd6cc 6250#else
76db3ba4 6251 if (unlikely(!ctx->mem_idx)) {
e06fcd75 6252 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
a42bd6cc
JM
6253 return;
6254 }
6255 /* Restore CPU state */
e5f17ac6 6256 gen_helper_rfci(cpu_env);
e06fcd75 6257 gen_sync_exception(ctx);
a42bd6cc
JM
6258#endif
6259}
6260
6261/* BookE specific */
99e300ef 6262
54623277 6263/* XXX: not implemented on 440 ? */
99e300ef 6264static void gen_rfdi(DisasContext *ctx)
76a66253
JM
6265{
6266#if defined(CONFIG_USER_ONLY)
e06fcd75 6267 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
76a66253 6268#else
76db3ba4 6269 if (unlikely(!ctx->mem_idx)) {
e06fcd75 6270 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
76a66253
JM
6271 return;
6272 }
6273 /* Restore CPU state */
e5f17ac6 6274 gen_helper_rfdi(cpu_env);
e06fcd75 6275 gen_sync_exception(ctx);
76a66253
JM
6276#endif
6277}
6278
2662a059 6279/* XXX: not implemented on 440 ? */
99e300ef 6280static void gen_rfmci(DisasContext *ctx)
a42bd6cc
JM
6281{
6282#if defined(CONFIG_USER_ONLY)
e06fcd75 6283 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
a42bd6cc 6284#else
76db3ba4 6285 if (unlikely(!ctx->mem_idx)) {
e06fcd75 6286 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
a42bd6cc
JM
6287 return;
6288 }
6289 /* Restore CPU state */
e5f17ac6 6290 gen_helper_rfmci(cpu_env);
e06fcd75 6291 gen_sync_exception(ctx);
a42bd6cc
JM
6292#endif
6293}
5eb7995e 6294
d9bce9d9 6295/* TLB management - PowerPC 405 implementation */
e8eaa2c0 6296
54623277 6297/* tlbre */
e8eaa2c0 6298static void gen_tlbre_40x(DisasContext *ctx)
76a66253
JM
6299{
6300#if defined(CONFIG_USER_ONLY)
e06fcd75 6301 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
76a66253 6302#else
76db3ba4 6303 if (unlikely(!ctx->mem_idx)) {
e06fcd75 6304 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
76a66253
JM
6305 return;
6306 }
6307 switch (rB(ctx->opcode)) {
6308 case 0:
c6c7cf05
BS
6309 gen_helper_4xx_tlbre_hi(cpu_gpr[rD(ctx->opcode)], cpu_env,
6310 cpu_gpr[rA(ctx->opcode)]);
76a66253
JM
6311 break;
6312 case 1:
c6c7cf05
BS
6313 gen_helper_4xx_tlbre_lo(cpu_gpr[rD(ctx->opcode)], cpu_env,
6314 cpu_gpr[rA(ctx->opcode)]);
76a66253
JM
6315 break;
6316 default:
e06fcd75 6317 gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL);
76a66253 6318 break;
9a64fbe4 6319 }
76a66253
JM
6320#endif
6321}
6322
d9bce9d9 6323/* tlbsx - tlbsx. */
e8eaa2c0 6324static void gen_tlbsx_40x(DisasContext *ctx)
76a66253
JM
6325{
6326#if defined(CONFIG_USER_ONLY)
e06fcd75 6327 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
76a66253 6328#else
74d37793 6329 TCGv t0;
76db3ba4 6330 if (unlikely(!ctx->mem_idx)) {
e06fcd75 6331 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
76a66253
JM
6332 return;
6333 }
74d37793 6334 t0 = tcg_temp_new();
76db3ba4 6335 gen_addr_reg_index(ctx, t0);
c6c7cf05 6336 gen_helper_4xx_tlbsx(cpu_gpr[rD(ctx->opcode)], cpu_env, t0);
74d37793
AJ
6337 tcg_temp_free(t0);
6338 if (Rc(ctx->opcode)) {
6339 int l1 = gen_new_label();
da91a00f 6340 tcg_gen_trunc_tl_i32(cpu_crf[0], cpu_so);
74d37793
AJ
6341 tcg_gen_brcondi_tl(TCG_COND_EQ, cpu_gpr[rD(ctx->opcode)], -1, l1);
6342 tcg_gen_ori_i32(cpu_crf[0], cpu_crf[0], 0x02);
6343 gen_set_label(l1);
6344 }
76a66253 6345#endif
79aceca5
FB
6346}
6347
76a66253 6348/* tlbwe */
e8eaa2c0 6349static void gen_tlbwe_40x(DisasContext *ctx)
79aceca5 6350{
76a66253 6351#if defined(CONFIG_USER_ONLY)
e06fcd75 6352 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
76a66253 6353#else
76db3ba4 6354 if (unlikely(!ctx->mem_idx)) {
e06fcd75 6355 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
76a66253
JM
6356 return;
6357 }
6358 switch (rB(ctx->opcode)) {
6359 case 0:
c6c7cf05
BS
6360 gen_helper_4xx_tlbwe_hi(cpu_env, cpu_gpr[rA(ctx->opcode)],
6361 cpu_gpr[rS(ctx->opcode)]);
76a66253
JM
6362 break;
6363 case 1:
c6c7cf05
BS
6364 gen_helper_4xx_tlbwe_lo(cpu_env, cpu_gpr[rA(ctx->opcode)],
6365 cpu_gpr[rS(ctx->opcode)]);
76a66253
JM
6366 break;
6367 default:
e06fcd75 6368 gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL);
76a66253 6369 break;
9a64fbe4 6370 }
76a66253
JM
6371#endif
6372}
6373
a4bb6c3e 6374/* TLB management - PowerPC 440 implementation */
e8eaa2c0 6375
54623277 6376/* tlbre */
e8eaa2c0 6377static void gen_tlbre_440(DisasContext *ctx)
5eb7995e
JM
6378{
6379#if defined(CONFIG_USER_ONLY)
e06fcd75 6380 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
5eb7995e 6381#else
76db3ba4 6382 if (unlikely(!ctx->mem_idx)) {
e06fcd75 6383 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
5eb7995e
JM
6384 return;
6385 }
6386 switch (rB(ctx->opcode)) {
6387 case 0:
5eb7995e 6388 case 1:
5eb7995e 6389 case 2:
74d37793
AJ
6390 {
6391 TCGv_i32 t0 = tcg_const_i32(rB(ctx->opcode));
c6c7cf05
BS
6392 gen_helper_440_tlbre(cpu_gpr[rD(ctx->opcode)], cpu_env,
6393 t0, cpu_gpr[rA(ctx->opcode)]);
74d37793
AJ
6394 tcg_temp_free_i32(t0);
6395 }
5eb7995e
JM
6396 break;
6397 default:
e06fcd75 6398 gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL);
5eb7995e
JM
6399 break;
6400 }
6401#endif
6402}
6403
6404/* tlbsx - tlbsx. */
e8eaa2c0 6405static void gen_tlbsx_440(DisasContext *ctx)
5eb7995e
JM
6406{
6407#if defined(CONFIG_USER_ONLY)
e06fcd75 6408 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
5eb7995e 6409#else
74d37793 6410 TCGv t0;
76db3ba4 6411 if (unlikely(!ctx->mem_idx)) {
e06fcd75 6412 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
5eb7995e
JM
6413 return;
6414 }
74d37793 6415 t0 = tcg_temp_new();
76db3ba4 6416 gen_addr_reg_index(ctx, t0);
c6c7cf05 6417 gen_helper_440_tlbsx(cpu_gpr[rD(ctx->opcode)], cpu_env, t0);
74d37793
AJ
6418 tcg_temp_free(t0);
6419 if (Rc(ctx->opcode)) {
6420 int l1 = gen_new_label();
da91a00f 6421 tcg_gen_trunc_tl_i32(cpu_crf[0], cpu_so);
74d37793
AJ
6422 tcg_gen_brcondi_tl(TCG_COND_EQ, cpu_gpr[rD(ctx->opcode)], -1, l1);
6423 tcg_gen_ori_i32(cpu_crf[0], cpu_crf[0], 0x02);
6424 gen_set_label(l1);
6425 }
5eb7995e
JM
6426#endif
6427}
6428
6429/* tlbwe */
e8eaa2c0 6430static void gen_tlbwe_440(DisasContext *ctx)
5eb7995e
JM
6431{
6432#if defined(CONFIG_USER_ONLY)
e06fcd75 6433 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
5eb7995e 6434#else
76db3ba4 6435 if (unlikely(!ctx->mem_idx)) {
e06fcd75 6436 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
5eb7995e
JM
6437 return;
6438 }
6439 switch (rB(ctx->opcode)) {
6440 case 0:
5eb7995e 6441 case 1:
5eb7995e 6442 case 2:
74d37793
AJ
6443 {
6444 TCGv_i32 t0 = tcg_const_i32(rB(ctx->opcode));
c6c7cf05
BS
6445 gen_helper_440_tlbwe(cpu_env, t0, cpu_gpr[rA(ctx->opcode)],
6446 cpu_gpr[rS(ctx->opcode)]);
74d37793
AJ
6447 tcg_temp_free_i32(t0);
6448 }
5eb7995e
JM
6449 break;
6450 default:
e06fcd75 6451 gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL);
5eb7995e
JM
6452 break;
6453 }
6454#endif
6455}
6456
01662f3e
AG
6457/* TLB management - PowerPC BookE 2.06 implementation */
6458
6459/* tlbre */
6460static void gen_tlbre_booke206(DisasContext *ctx)
6461{
6462#if defined(CONFIG_USER_ONLY)
6463 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
6464#else
6465 if (unlikely(!ctx->mem_idx)) {
6466 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
6467 return;
6468 }
6469
c6c7cf05 6470 gen_helper_booke206_tlbre(cpu_env);
01662f3e
AG
6471#endif
6472}
6473
6474/* tlbsx - tlbsx. */
6475static void gen_tlbsx_booke206(DisasContext *ctx)
6476{
6477#if defined(CONFIG_USER_ONLY)
6478 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
6479#else
6480 TCGv t0;
6481 if (unlikely(!ctx->mem_idx)) {
6482 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
6483 return;
6484 }
6485
6486 if (rA(ctx->opcode)) {
6487 t0 = tcg_temp_new();
6488 tcg_gen_mov_tl(t0, cpu_gpr[rD(ctx->opcode)]);
6489 } else {
6490 t0 = tcg_const_tl(0);
6491 }
6492
6493 tcg_gen_add_tl(t0, t0, cpu_gpr[rB(ctx->opcode)]);
c6c7cf05 6494 gen_helper_booke206_tlbsx(cpu_env, t0);
01662f3e
AG
6495#endif
6496}
6497
6498/* tlbwe */
6499static void gen_tlbwe_booke206(DisasContext *ctx)
6500{
6501#if defined(CONFIG_USER_ONLY)
6502 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
6503#else
6504 if (unlikely(!ctx->mem_idx)) {
6505 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
6506 return;
6507 }
3f162d11 6508 gen_update_nip(ctx, ctx->nip - 4);
c6c7cf05 6509 gen_helper_booke206_tlbwe(cpu_env);
01662f3e
AG
6510#endif
6511}
6512
6513static void gen_tlbivax_booke206(DisasContext *ctx)
6514{
6515#if defined(CONFIG_USER_ONLY)
6516 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
6517#else
6518 TCGv t0;
6519 if (unlikely(!ctx->mem_idx)) {
6520 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
6521 return;
6522 }
6523
6524 t0 = tcg_temp_new();
6525 gen_addr_reg_index(ctx, t0);
6526
c6c7cf05 6527 gen_helper_booke206_tlbivax(cpu_env, t0);
01662f3e
AG
6528#endif
6529}
6530
6d3db821
AG
6531static void gen_tlbilx_booke206(DisasContext *ctx)
6532{
6533#if defined(CONFIG_USER_ONLY)
6534 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
6535#else
6536 TCGv t0;
6537 if (unlikely(!ctx->mem_idx)) {
6538 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
6539 return;
6540 }
6541
6542 t0 = tcg_temp_new();
6543 gen_addr_reg_index(ctx, t0);
6544
6545 switch((ctx->opcode >> 21) & 0x3) {
6546 case 0:
c6c7cf05 6547 gen_helper_booke206_tlbilx0(cpu_env, t0);
6d3db821
AG
6548 break;
6549 case 1:
c6c7cf05 6550 gen_helper_booke206_tlbilx1(cpu_env, t0);
6d3db821
AG
6551 break;
6552 case 3:
c6c7cf05 6553 gen_helper_booke206_tlbilx3(cpu_env, t0);
6d3db821
AG
6554 break;
6555 default:
6556 gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL);
6557 break;
6558 }
6559
6560 tcg_temp_free(t0);
6561#endif
6562}
6563
01662f3e 6564
76a66253 6565/* wrtee */
99e300ef 6566static void gen_wrtee(DisasContext *ctx)
76a66253
JM
6567{
6568#if defined(CONFIG_USER_ONLY)
e06fcd75 6569 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
76a66253 6570#else
6527f6ea 6571 TCGv t0;
76db3ba4 6572 if (unlikely(!ctx->mem_idx)) {
e06fcd75 6573 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
76a66253
JM
6574 return;
6575 }
6527f6ea
AJ
6576 t0 = tcg_temp_new();
6577 tcg_gen_andi_tl(t0, cpu_gpr[rD(ctx->opcode)], (1 << MSR_EE));
6578 tcg_gen_andi_tl(cpu_msr, cpu_msr, ~(1 << MSR_EE));
6579 tcg_gen_or_tl(cpu_msr, cpu_msr, t0);
6580 tcg_temp_free(t0);
dee96f6c
JM
6581 /* Stop translation to have a chance to raise an exception
6582 * if we just set msr_ee to 1
6583 */
e06fcd75 6584 gen_stop_exception(ctx);
76a66253
JM
6585#endif
6586}
6587
6588/* wrteei */
99e300ef 6589static void gen_wrteei(DisasContext *ctx)
76a66253
JM
6590{
6591#if defined(CONFIG_USER_ONLY)
e06fcd75 6592 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
76a66253 6593#else
76db3ba4 6594 if (unlikely(!ctx->mem_idx)) {
e06fcd75 6595 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
76a66253
JM
6596 return;
6597 }
fbe73008 6598 if (ctx->opcode & 0x00008000) {
6527f6ea
AJ
6599 tcg_gen_ori_tl(cpu_msr, cpu_msr, (1 << MSR_EE));
6600 /* Stop translation to have a chance to raise an exception */
e06fcd75 6601 gen_stop_exception(ctx);
6527f6ea 6602 } else {
1b6e5f99 6603 tcg_gen_andi_tl(cpu_msr, cpu_msr, ~(1 << MSR_EE));
6527f6ea 6604 }
76a66253
JM
6605#endif
6606}
6607
08e46e54 6608/* PowerPC 440 specific instructions */
99e300ef 6609
54623277 6610/* dlmzb */
99e300ef 6611static void gen_dlmzb(DisasContext *ctx)
76a66253 6612{
ef0d51af 6613 TCGv_i32 t0 = tcg_const_i32(Rc(ctx->opcode));
d15f74fb
BS
6614 gen_helper_dlmzb(cpu_gpr[rA(ctx->opcode)], cpu_env,
6615 cpu_gpr[rS(ctx->opcode)], cpu_gpr[rB(ctx->opcode)], t0);
ef0d51af 6616 tcg_temp_free_i32(t0);
76a66253
JM
6617}
6618
6619/* mbar replaces eieio on 440 */
99e300ef 6620static void gen_mbar(DisasContext *ctx)
76a66253
JM
6621{
6622 /* interpreted as no-op */
6623}
6624
6625/* msync replaces sync on 440 */
dcb2b9e1 6626static void gen_msync_4xx(DisasContext *ctx)
76a66253
JM
6627{
6628 /* interpreted as no-op */
6629}
6630
6631/* icbt */
e8eaa2c0 6632static void gen_icbt_440(DisasContext *ctx)
76a66253
JM
6633{
6634 /* interpreted as no-op */
6635 /* XXX: specification say this is treated as a load by the MMU
6636 * but does not generate any exception
6637 */
79aceca5
FB
6638}
6639
9e0b5cb1
AG
6640/* Embedded.Processor Control */
6641
6642static void gen_msgclr(DisasContext *ctx)
6643{
6644#if defined(CONFIG_USER_ONLY)
6645 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
6646#else
6647 if (unlikely(ctx->mem_idx == 0)) {
6648 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
6649 return;
6650 }
6651
e5f17ac6 6652 gen_helper_msgclr(cpu_env, cpu_gpr[rB(ctx->opcode)]);
9e0b5cb1
AG
6653#endif
6654}
6655
d5d11a39
AG
6656static void gen_msgsnd(DisasContext *ctx)
6657{
6658#if defined(CONFIG_USER_ONLY)
6659 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
6660#else
6661 if (unlikely(ctx->mem_idx == 0)) {
6662 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
6663 return;
6664 }
6665
6666 gen_helper_msgsnd(cpu_gpr[rB(ctx->opcode)]);
6667#endif
6668}
6669
a9d9eb8f
JM
6670/*** Altivec vector extension ***/
6671/* Altivec registers moves */
a9d9eb8f 6672
636aa200 6673static inline TCGv_ptr gen_avr_ptr(int reg)
564e571a 6674{
e4704b3b 6675 TCGv_ptr r = tcg_temp_new_ptr();
564e571a
AJ
6676 tcg_gen_addi_ptr(r, cpu_env, offsetof(CPUPPCState, avr[reg]));
6677 return r;
6678}
6679
a9d9eb8f 6680#define GEN_VR_LDX(name, opc2, opc3) \
99e300ef 6681static void glue(gen_, name)(DisasContext *ctx) \
a9d9eb8f 6682{ \
fe1e5c53 6683 TCGv EA; \
a9d9eb8f 6684 if (unlikely(!ctx->altivec_enabled)) { \
e06fcd75 6685 gen_exception(ctx, POWERPC_EXCP_VPU); \
a9d9eb8f
JM
6686 return; \
6687 } \
76db3ba4 6688 gen_set_access_type(ctx, ACCESS_INT); \
fe1e5c53 6689 EA = tcg_temp_new(); \
76db3ba4 6690 gen_addr_reg_index(ctx, EA); \
fe1e5c53 6691 tcg_gen_andi_tl(EA, EA, ~0xf); \
76db3ba4
AJ
6692 if (ctx->le_mode) { \
6693 gen_qemu_ld64(ctx, cpu_avrl[rD(ctx->opcode)], EA); \
fe1e5c53 6694 tcg_gen_addi_tl(EA, EA, 8); \
76db3ba4 6695 gen_qemu_ld64(ctx, cpu_avrh[rD(ctx->opcode)], EA); \
fe1e5c53 6696 } else { \
76db3ba4 6697 gen_qemu_ld64(ctx, cpu_avrh[rD(ctx->opcode)], EA); \
fe1e5c53 6698 tcg_gen_addi_tl(EA, EA, 8); \
76db3ba4 6699 gen_qemu_ld64(ctx, cpu_avrl[rD(ctx->opcode)], EA); \
fe1e5c53
AJ
6700 } \
6701 tcg_temp_free(EA); \
a9d9eb8f
JM
6702}
6703
6704#define GEN_VR_STX(name, opc2, opc3) \
99e300ef 6705static void gen_st##name(DisasContext *ctx) \
a9d9eb8f 6706{ \
fe1e5c53 6707 TCGv EA; \
a9d9eb8f 6708 if (unlikely(!ctx->altivec_enabled)) { \
e06fcd75 6709 gen_exception(ctx, POWERPC_EXCP_VPU); \
a9d9eb8f
JM
6710 return; \
6711 } \
76db3ba4 6712 gen_set_access_type(ctx, ACCESS_INT); \
fe1e5c53 6713 EA = tcg_temp_new(); \
76db3ba4 6714 gen_addr_reg_index(ctx, EA); \
fe1e5c53 6715 tcg_gen_andi_tl(EA, EA, ~0xf); \
76db3ba4
AJ
6716 if (ctx->le_mode) { \
6717 gen_qemu_st64(ctx, cpu_avrl[rD(ctx->opcode)], EA); \
fe1e5c53 6718 tcg_gen_addi_tl(EA, EA, 8); \
76db3ba4 6719 gen_qemu_st64(ctx, cpu_avrh[rD(ctx->opcode)], EA); \
fe1e5c53 6720 } else { \
76db3ba4 6721 gen_qemu_st64(ctx, cpu_avrh[rD(ctx->opcode)], EA); \
fe1e5c53 6722 tcg_gen_addi_tl(EA, EA, 8); \
76db3ba4 6723 gen_qemu_st64(ctx, cpu_avrl[rD(ctx->opcode)], EA); \
fe1e5c53
AJ
6724 } \
6725 tcg_temp_free(EA); \
a9d9eb8f
JM
6726}
6727
cbfb6ae9 6728#define GEN_VR_LVE(name, opc2, opc3) \
99e300ef 6729static void gen_lve##name(DisasContext *ctx) \
cbfb6ae9
AJ
6730 { \
6731 TCGv EA; \
6732 TCGv_ptr rs; \
6733 if (unlikely(!ctx->altivec_enabled)) { \
6734 gen_exception(ctx, POWERPC_EXCP_VPU); \
6735 return; \
6736 } \
6737 gen_set_access_type(ctx, ACCESS_INT); \
6738 EA = tcg_temp_new(); \
6739 gen_addr_reg_index(ctx, EA); \
6740 rs = gen_avr_ptr(rS(ctx->opcode)); \
2f5a189c 6741 gen_helper_lve##name(cpu_env, rs, EA); \
cbfb6ae9
AJ
6742 tcg_temp_free(EA); \
6743 tcg_temp_free_ptr(rs); \
6744 }
6745
6746#define GEN_VR_STVE(name, opc2, opc3) \
99e300ef 6747static void gen_stve##name(DisasContext *ctx) \
cbfb6ae9
AJ
6748 { \
6749 TCGv EA; \
6750 TCGv_ptr rs; \
6751 if (unlikely(!ctx->altivec_enabled)) { \
6752 gen_exception(ctx, POWERPC_EXCP_VPU); \
6753 return; \
6754 } \
6755 gen_set_access_type(ctx, ACCESS_INT); \
6756 EA = tcg_temp_new(); \
6757 gen_addr_reg_index(ctx, EA); \
6758 rs = gen_avr_ptr(rS(ctx->opcode)); \
2f5a189c 6759 gen_helper_stve##name(cpu_env, rs, EA); \
cbfb6ae9
AJ
6760 tcg_temp_free(EA); \
6761 tcg_temp_free_ptr(rs); \
6762 }
6763
fe1e5c53 6764GEN_VR_LDX(lvx, 0x07, 0x03);
a9d9eb8f 6765/* As we don't emulate the cache, lvxl is stricly equivalent to lvx */
fe1e5c53 6766GEN_VR_LDX(lvxl, 0x07, 0x0B);
a9d9eb8f 6767
cbfb6ae9
AJ
6768GEN_VR_LVE(bx, 0x07, 0x00);
6769GEN_VR_LVE(hx, 0x07, 0x01);
6770GEN_VR_LVE(wx, 0x07, 0x02);
6771
fe1e5c53 6772GEN_VR_STX(svx, 0x07, 0x07);
a9d9eb8f 6773/* As we don't emulate the cache, stvxl is stricly equivalent to stvx */
fe1e5c53 6774GEN_VR_STX(svxl, 0x07, 0x0F);
a9d9eb8f 6775
cbfb6ae9
AJ
6776GEN_VR_STVE(bx, 0x07, 0x04);
6777GEN_VR_STVE(hx, 0x07, 0x05);
6778GEN_VR_STVE(wx, 0x07, 0x06);
6779
99e300ef 6780static void gen_lvsl(DisasContext *ctx)
bf8d8ded
AJ
6781{
6782 TCGv_ptr rd;
6783 TCGv EA;
6784 if (unlikely(!ctx->altivec_enabled)) {
6785 gen_exception(ctx, POWERPC_EXCP_VPU);
6786 return;
6787 }
6788 EA = tcg_temp_new();
6789 gen_addr_reg_index(ctx, EA);
6790 rd = gen_avr_ptr(rD(ctx->opcode));
6791 gen_helper_lvsl(rd, EA);
6792 tcg_temp_free(EA);
6793 tcg_temp_free_ptr(rd);
6794}
6795
99e300ef 6796static void gen_lvsr(DisasContext *ctx)
bf8d8ded
AJ
6797{
6798 TCGv_ptr rd;
6799 TCGv EA;
6800 if (unlikely(!ctx->altivec_enabled)) {
6801 gen_exception(ctx, POWERPC_EXCP_VPU);
6802 return;
6803 }
6804 EA = tcg_temp_new();
6805 gen_addr_reg_index(ctx, EA);
6806 rd = gen_avr_ptr(rD(ctx->opcode));
6807 gen_helper_lvsr(rd, EA);
6808 tcg_temp_free(EA);
6809 tcg_temp_free_ptr(rd);
6810}
6811
99e300ef 6812static void gen_mfvscr(DisasContext *ctx)
785f451b
AJ
6813{
6814 TCGv_i32 t;
6815 if (unlikely(!ctx->altivec_enabled)) {
6816 gen_exception(ctx, POWERPC_EXCP_VPU);
6817 return;
6818 }
6819 tcg_gen_movi_i64(cpu_avrh[rD(ctx->opcode)], 0);
6820 t = tcg_temp_new_i32();
1328c2bf 6821 tcg_gen_ld_i32(t, cpu_env, offsetof(CPUPPCState, vscr));
785f451b 6822 tcg_gen_extu_i32_i64(cpu_avrl[rD(ctx->opcode)], t);
fce5ecb7 6823 tcg_temp_free_i32(t);
785f451b
AJ
6824}
6825
99e300ef 6826static void gen_mtvscr(DisasContext *ctx)
785f451b 6827{
6e87b7c7 6828 TCGv_ptr p;
785f451b
AJ
6829 if (unlikely(!ctx->altivec_enabled)) {
6830 gen_exception(ctx, POWERPC_EXCP_VPU);
6831 return;
6832 }
6e87b7c7 6833 p = gen_avr_ptr(rD(ctx->opcode));
d15f74fb 6834 gen_helper_mtvscr(cpu_env, p);
6e87b7c7 6835 tcg_temp_free_ptr(p);
785f451b
AJ
6836}
6837
7a9b96cf
AJ
6838/* Logical operations */
6839#define GEN_VX_LOGICAL(name, tcg_op, opc2, opc3) \
99e300ef 6840static void glue(gen_, name)(DisasContext *ctx) \
7a9b96cf
AJ
6841{ \
6842 if (unlikely(!ctx->altivec_enabled)) { \
6843 gen_exception(ctx, POWERPC_EXCP_VPU); \
6844 return; \
6845 } \
6846 tcg_op(cpu_avrh[rD(ctx->opcode)], cpu_avrh[rA(ctx->opcode)], cpu_avrh[rB(ctx->opcode)]); \
6847 tcg_op(cpu_avrl[rD(ctx->opcode)], cpu_avrl[rA(ctx->opcode)], cpu_avrl[rB(ctx->opcode)]); \
6848}
6849
6850GEN_VX_LOGICAL(vand, tcg_gen_and_i64, 2, 16);
6851GEN_VX_LOGICAL(vandc, tcg_gen_andc_i64, 2, 17);
6852GEN_VX_LOGICAL(vor, tcg_gen_or_i64, 2, 18);
6853GEN_VX_LOGICAL(vxor, tcg_gen_xor_i64, 2, 19);
6854GEN_VX_LOGICAL(vnor, tcg_gen_nor_i64, 2, 20);
111c5f54
TM
6855GEN_VX_LOGICAL(veqv, tcg_gen_eqv_i64, 2, 26);
6856GEN_VX_LOGICAL(vnand, tcg_gen_nand_i64, 2, 22);
6857GEN_VX_LOGICAL(vorc, tcg_gen_orc_i64, 2, 21);
7a9b96cf 6858
8e27dd6f 6859#define GEN_VXFORM(name, opc2, opc3) \
99e300ef 6860static void glue(gen_, name)(DisasContext *ctx) \
8e27dd6f
AJ
6861{ \
6862 TCGv_ptr ra, rb, rd; \
6863 if (unlikely(!ctx->altivec_enabled)) { \
6864 gen_exception(ctx, POWERPC_EXCP_VPU); \
6865 return; \
6866 } \
6867 ra = gen_avr_ptr(rA(ctx->opcode)); \
6868 rb = gen_avr_ptr(rB(ctx->opcode)); \
6869 rd = gen_avr_ptr(rD(ctx->opcode)); \
6870 gen_helper_##name (rd, ra, rb); \
6871 tcg_temp_free_ptr(ra); \
6872 tcg_temp_free_ptr(rb); \
6873 tcg_temp_free_ptr(rd); \
6874}
6875
d15f74fb
BS
6876#define GEN_VXFORM_ENV(name, opc2, opc3) \
6877static void glue(gen_, name)(DisasContext *ctx) \
6878{ \
6879 TCGv_ptr ra, rb, rd; \
6880 if (unlikely(!ctx->altivec_enabled)) { \
6881 gen_exception(ctx, POWERPC_EXCP_VPU); \
6882 return; \
6883 } \
6884 ra = gen_avr_ptr(rA(ctx->opcode)); \
6885 rb = gen_avr_ptr(rB(ctx->opcode)); \
6886 rd = gen_avr_ptr(rD(ctx->opcode)); \
54cddd21 6887 gen_helper_##name(cpu_env, rd, ra, rb); \
d15f74fb
BS
6888 tcg_temp_free_ptr(ra); \
6889 tcg_temp_free_ptr(rb); \
6890 tcg_temp_free_ptr(rd); \
9b47bb49
TM
6891}
6892
6893#define GEN_VXFORM3(name, opc2, opc3) \
6894static void glue(gen_, name)(DisasContext *ctx) \
6895{ \
6896 TCGv_ptr ra, rb, rc, rd; \
6897 if (unlikely(!ctx->altivec_enabled)) { \
6898 gen_exception(ctx, POWERPC_EXCP_VPU); \
6899 return; \
6900 } \
6901 ra = gen_avr_ptr(rA(ctx->opcode)); \
6902 rb = gen_avr_ptr(rB(ctx->opcode)); \
6903 rc = gen_avr_ptr(rC(ctx->opcode)); \
6904 rd = gen_avr_ptr(rD(ctx->opcode)); \
6905 gen_helper_##name(rd, ra, rb, rc); \
6906 tcg_temp_free_ptr(ra); \
6907 tcg_temp_free_ptr(rb); \
6908 tcg_temp_free_ptr(rc); \
6909 tcg_temp_free_ptr(rd); \
d15f74fb
BS
6910}
6911
5dffff5a
TM
6912/*
6913 * Support for Altivec instruction pairs that use bit 31 (Rc) as
6914 * an opcode bit. In general, these pairs come from different
6915 * versions of the ISA, so we must also support a pair of flags for
6916 * each instruction.
6917 */
6918#define GEN_VXFORM_DUAL(name0, flg0, flg2_0, name1, flg1, flg2_1) \
6919static void glue(gen_, name0##_##name1)(DisasContext *ctx) \
6920{ \
6921 if ((Rc(ctx->opcode) == 0) && \
6922 ((ctx->insns_flags & flg0) || (ctx->insns_flags2 & flg2_0))) { \
6923 gen_##name0(ctx); \
6924 } else if ((Rc(ctx->opcode) == 1) && \
6925 ((ctx->insns_flags & flg1) || (ctx->insns_flags2 & flg2_1))) { \
6926 gen_##name1(ctx); \
6927 } else { \
6928 gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL); \
6929 } \
6930}
6931
7872c51c
AJ
6932GEN_VXFORM(vaddubm, 0, 0);
6933GEN_VXFORM(vadduhm, 0, 1);
6934GEN_VXFORM(vadduwm, 0, 2);
56eabc75 6935GEN_VXFORM(vaddudm, 0, 3);
7872c51c
AJ
6936GEN_VXFORM(vsububm, 0, 16);
6937GEN_VXFORM(vsubuhm, 0, 17);
6938GEN_VXFORM(vsubuwm, 0, 18);
56eabc75 6939GEN_VXFORM(vsubudm, 0, 19);
e4039339
AJ
6940GEN_VXFORM(vmaxub, 1, 0);
6941GEN_VXFORM(vmaxuh, 1, 1);
6942GEN_VXFORM(vmaxuw, 1, 2);
6943GEN_VXFORM(vmaxsb, 1, 4);
6944GEN_VXFORM(vmaxsh, 1, 5);
6945GEN_VXFORM(vmaxsw, 1, 6);
6946GEN_VXFORM(vminub, 1, 8);
6947GEN_VXFORM(vminuh, 1, 9);
6948GEN_VXFORM(vminuw, 1, 10);
6949GEN_VXFORM(vminsb, 1, 12);
6950GEN_VXFORM(vminsh, 1, 13);
6951GEN_VXFORM(vminsw, 1, 14);
fab3cbe9
AJ
6952GEN_VXFORM(vavgub, 1, 16);
6953GEN_VXFORM(vavguh, 1, 17);
6954GEN_VXFORM(vavguw, 1, 18);
6955GEN_VXFORM(vavgsb, 1, 20);
6956GEN_VXFORM(vavgsh, 1, 21);
6957GEN_VXFORM(vavgsw, 1, 22);
3b430048
AJ
6958GEN_VXFORM(vmrghb, 6, 0);
6959GEN_VXFORM(vmrghh, 6, 1);
6960GEN_VXFORM(vmrghw, 6, 2);
6961GEN_VXFORM(vmrglb, 6, 4);
6962GEN_VXFORM(vmrglh, 6, 5);
6963GEN_VXFORM(vmrglw, 6, 6);
2c277908
AJ
6964GEN_VXFORM(vmuloub, 4, 0);
6965GEN_VXFORM(vmulouh, 4, 1);
63be0936 6966GEN_VXFORM(vmulouw, 4, 2);
953f0f58
TM
6967GEN_VXFORM(vmuluwm, 4, 2);
6968GEN_VXFORM_DUAL(vmulouw, PPC_ALTIVEC, PPC_NONE,
6969 vmuluwm, PPC_NONE, PPC2_ALTIVEC_207)
2c277908
AJ
6970GEN_VXFORM(vmulosb, 4, 4);
6971GEN_VXFORM(vmulosh, 4, 5);
63be0936 6972GEN_VXFORM(vmulosw, 4, 6);
2c277908
AJ
6973GEN_VXFORM(vmuleub, 4, 8);
6974GEN_VXFORM(vmuleuh, 4, 9);
63be0936 6975GEN_VXFORM(vmuleuw, 4, 10);
2c277908
AJ
6976GEN_VXFORM(vmulesb, 4, 12);
6977GEN_VXFORM(vmulesh, 4, 13);
63be0936 6978GEN_VXFORM(vmulesw, 4, 14);
d79f0809
AJ
6979GEN_VXFORM(vslb, 2, 4);
6980GEN_VXFORM(vslh, 2, 5);
6981GEN_VXFORM(vslw, 2, 6);
07ef34c3
AJ
6982GEN_VXFORM(vsrb, 2, 8);
6983GEN_VXFORM(vsrh, 2, 9);
6984GEN_VXFORM(vsrw, 2, 10);
6985GEN_VXFORM(vsrab, 2, 12);
6986GEN_VXFORM(vsrah, 2, 13);
6987GEN_VXFORM(vsraw, 2, 14);
7b239bec
AJ
6988GEN_VXFORM(vslo, 6, 16);
6989GEN_VXFORM(vsro, 6, 17);
e343da72
AJ
6990GEN_VXFORM(vaddcuw, 0, 6);
6991GEN_VXFORM(vsubcuw, 0, 22);
d15f74fb
BS
6992GEN_VXFORM_ENV(vaddubs, 0, 8);
6993GEN_VXFORM_ENV(vadduhs, 0, 9);
6994GEN_VXFORM_ENV(vadduws, 0, 10);
6995GEN_VXFORM_ENV(vaddsbs, 0, 12);
6996GEN_VXFORM_ENV(vaddshs, 0, 13);
6997GEN_VXFORM_ENV(vaddsws, 0, 14);
6998GEN_VXFORM_ENV(vsububs, 0, 24);
6999GEN_VXFORM_ENV(vsubuhs, 0, 25);
7000GEN_VXFORM_ENV(vsubuws, 0, 26);
7001GEN_VXFORM_ENV(vsubsbs, 0, 28);
7002GEN_VXFORM_ENV(vsubshs, 0, 29);
7003GEN_VXFORM_ENV(vsubsws, 0, 30);
5e1d0985
AJ
7004GEN_VXFORM(vrlb, 2, 0);
7005GEN_VXFORM(vrlh, 2, 1);
7006GEN_VXFORM(vrlw, 2, 2);
d9430add
AJ
7007GEN_VXFORM(vsl, 2, 7);
7008GEN_VXFORM(vsr, 2, 11);
d15f74fb
BS
7009GEN_VXFORM_ENV(vpkuhum, 7, 0);
7010GEN_VXFORM_ENV(vpkuwum, 7, 1);
7011GEN_VXFORM_ENV(vpkuhus, 7, 2);
7012GEN_VXFORM_ENV(vpkuwus, 7, 3);
7013GEN_VXFORM_ENV(vpkshus, 7, 4);
7014GEN_VXFORM_ENV(vpkswus, 7, 5);
7015GEN_VXFORM_ENV(vpkshss, 7, 6);
7016GEN_VXFORM_ENV(vpkswss, 7, 7);
1dd9ffb9 7017GEN_VXFORM(vpkpx, 7, 12);
d15f74fb
BS
7018GEN_VXFORM_ENV(vsum4ubs, 4, 24);
7019GEN_VXFORM_ENV(vsum4sbs, 4, 28);
7020GEN_VXFORM_ENV(vsum4shs, 4, 25);
7021GEN_VXFORM_ENV(vsum2sws, 4, 26);
7022GEN_VXFORM_ENV(vsumsws, 4, 30);
7023GEN_VXFORM_ENV(vaddfp, 5, 0);
7024GEN_VXFORM_ENV(vsubfp, 5, 1);
7025GEN_VXFORM_ENV(vmaxfp, 5, 16);
7026GEN_VXFORM_ENV(vminfp, 5, 17);
fab3cbe9 7027
0cbcd906 7028#define GEN_VXRFORM1(opname, name, str, opc2, opc3) \
e8eaa2c0 7029static void glue(gen_, name)(DisasContext *ctx) \
0cbcd906
AJ
7030 { \
7031 TCGv_ptr ra, rb, rd; \
7032 if (unlikely(!ctx->altivec_enabled)) { \
7033 gen_exception(ctx, POWERPC_EXCP_VPU); \
7034 return; \
7035 } \
7036 ra = gen_avr_ptr(rA(ctx->opcode)); \
7037 rb = gen_avr_ptr(rB(ctx->opcode)); \
7038 rd = gen_avr_ptr(rD(ctx->opcode)); \
d15f74fb 7039 gen_helper_##opname(cpu_env, rd, ra, rb); \
0cbcd906
AJ
7040 tcg_temp_free_ptr(ra); \
7041 tcg_temp_free_ptr(rb); \
7042 tcg_temp_free_ptr(rd); \
7043 }
7044
7045#define GEN_VXRFORM(name, opc2, opc3) \
7046 GEN_VXRFORM1(name, name, #name, opc2, opc3) \
7047 GEN_VXRFORM1(name##_dot, name##_, #name ".", opc2, (opc3 | (0x1 << 4)))
7048
a737d3eb
TM
7049/*
7050 * Support for Altivec instructions that use bit 31 (Rc) as an opcode
7051 * bit but also use bit 21 as an actual Rc bit. In general, thse pairs
7052 * come from different versions of the ISA, so we must also support a
7053 * pair of flags for each instruction.
7054 */
7055#define GEN_VXRFORM_DUAL(name0, flg0, flg2_0, name1, flg1, flg2_1) \
7056static void glue(gen_, name0##_##name1)(DisasContext *ctx) \
7057{ \
7058 if ((Rc(ctx->opcode) == 0) && \
7059 ((ctx->insns_flags & flg0) || (ctx->insns_flags2 & flg2_0))) { \
7060 if (Rc21(ctx->opcode) == 0) { \
7061 gen_##name0(ctx); \
7062 } else { \
7063 gen_##name0##_(ctx); \
7064 } \
7065 } else if ((Rc(ctx->opcode) == 1) && \
7066 ((ctx->insns_flags & flg1) || (ctx->insns_flags2 & flg2_1))) { \
7067 if (Rc21(ctx->opcode) == 0) { \
7068 gen_##name1(ctx); \
7069 } else { \
7070 gen_##name1##_(ctx); \
7071 } \
7072 } else { \
7073 gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL); \
7074 } \
7075}
7076
1add6e23
AJ
7077GEN_VXRFORM(vcmpequb, 3, 0)
7078GEN_VXRFORM(vcmpequh, 3, 1)
7079GEN_VXRFORM(vcmpequw, 3, 2)
7080GEN_VXRFORM(vcmpgtsb, 3, 12)
7081GEN_VXRFORM(vcmpgtsh, 3, 13)
7082GEN_VXRFORM(vcmpgtsw, 3, 14)
7083GEN_VXRFORM(vcmpgtub, 3, 8)
7084GEN_VXRFORM(vcmpgtuh, 3, 9)
7085GEN_VXRFORM(vcmpgtuw, 3, 10)
819ca121
AJ
7086GEN_VXRFORM(vcmpeqfp, 3, 3)
7087GEN_VXRFORM(vcmpgefp, 3, 7)
7088GEN_VXRFORM(vcmpgtfp, 3, 11)
7089GEN_VXRFORM(vcmpbfp, 3, 15)
1add6e23 7090
c026766b 7091#define GEN_VXFORM_SIMM(name, opc2, opc3) \
99e300ef 7092static void glue(gen_, name)(DisasContext *ctx) \
c026766b
AJ
7093 { \
7094 TCGv_ptr rd; \
7095 TCGv_i32 simm; \
7096 if (unlikely(!ctx->altivec_enabled)) { \
7097 gen_exception(ctx, POWERPC_EXCP_VPU); \
7098 return; \
7099 } \
7100 simm = tcg_const_i32(SIMM5(ctx->opcode)); \
7101 rd = gen_avr_ptr(rD(ctx->opcode)); \
7102 gen_helper_##name (rd, simm); \
7103 tcg_temp_free_i32(simm); \
7104 tcg_temp_free_ptr(rd); \
7105 }
7106
7107GEN_VXFORM_SIMM(vspltisb, 6, 12);
7108GEN_VXFORM_SIMM(vspltish, 6, 13);
7109GEN_VXFORM_SIMM(vspltisw, 6, 14);
7110
de5f2484 7111#define GEN_VXFORM_NOA(name, opc2, opc3) \
99e300ef 7112static void glue(gen_, name)(DisasContext *ctx) \
de5f2484
AJ
7113 { \
7114 TCGv_ptr rb, rd; \
7115 if (unlikely(!ctx->altivec_enabled)) { \
7116 gen_exception(ctx, POWERPC_EXCP_VPU); \
7117 return; \
7118 } \
7119 rb = gen_avr_ptr(rB(ctx->opcode)); \
7120 rd = gen_avr_ptr(rD(ctx->opcode)); \
7121 gen_helper_##name (rd, rb); \
7122 tcg_temp_free_ptr(rb); \
7123 tcg_temp_free_ptr(rd); \
7124 }
7125
d15f74fb
BS
7126#define GEN_VXFORM_NOA_ENV(name, opc2, opc3) \
7127static void glue(gen_, name)(DisasContext *ctx) \
7128 { \
7129 TCGv_ptr rb, rd; \
7130 \
7131 if (unlikely(!ctx->altivec_enabled)) { \
7132 gen_exception(ctx, POWERPC_EXCP_VPU); \
7133 return; \
7134 } \
7135 rb = gen_avr_ptr(rB(ctx->opcode)); \
7136 rd = gen_avr_ptr(rD(ctx->opcode)); \
7137 gen_helper_##name(cpu_env, rd, rb); \
7138 tcg_temp_free_ptr(rb); \
7139 tcg_temp_free_ptr(rd); \
7140 }
7141
6cf1c6e5
AJ
7142GEN_VXFORM_NOA(vupkhsb, 7, 8);
7143GEN_VXFORM_NOA(vupkhsh, 7, 9);
7144GEN_VXFORM_NOA(vupklsb, 7, 10);
7145GEN_VXFORM_NOA(vupklsh, 7, 11);
79f85c3a
AJ
7146GEN_VXFORM_NOA(vupkhpx, 7, 13);
7147GEN_VXFORM_NOA(vupklpx, 7, 15);
d15f74fb
BS
7148GEN_VXFORM_NOA_ENV(vrefp, 5, 4);
7149GEN_VXFORM_NOA_ENV(vrsqrtefp, 5, 5);
7150GEN_VXFORM_NOA_ENV(vexptefp, 5, 6);
7151GEN_VXFORM_NOA_ENV(vlogefp, 5, 7);
7152GEN_VXFORM_NOA_ENV(vrfim, 5, 8);
7153GEN_VXFORM_NOA_ENV(vrfin, 5, 9);
7154GEN_VXFORM_NOA_ENV(vrfip, 5, 10);
7155GEN_VXFORM_NOA_ENV(vrfiz, 5, 11);
79f85c3a 7156
21d21583 7157#define GEN_VXFORM_SIMM(name, opc2, opc3) \
99e300ef 7158static void glue(gen_, name)(DisasContext *ctx) \
21d21583
AJ
7159 { \
7160 TCGv_ptr rd; \
7161 TCGv_i32 simm; \
7162 if (unlikely(!ctx->altivec_enabled)) { \
7163 gen_exception(ctx, POWERPC_EXCP_VPU); \
7164 return; \
7165 } \
7166 simm = tcg_const_i32(SIMM5(ctx->opcode)); \
7167 rd = gen_avr_ptr(rD(ctx->opcode)); \
7168 gen_helper_##name (rd, simm); \
7169 tcg_temp_free_i32(simm); \
7170 tcg_temp_free_ptr(rd); \
7171 }
7172
27a4edb3 7173#define GEN_VXFORM_UIMM(name, opc2, opc3) \
99e300ef 7174static void glue(gen_, name)(DisasContext *ctx) \
27a4edb3
AJ
7175 { \
7176 TCGv_ptr rb, rd; \
7177 TCGv_i32 uimm; \
7178 if (unlikely(!ctx->altivec_enabled)) { \
7179 gen_exception(ctx, POWERPC_EXCP_VPU); \
7180 return; \
7181 } \
7182 uimm = tcg_const_i32(UIMM5(ctx->opcode)); \
7183 rb = gen_avr_ptr(rB(ctx->opcode)); \
7184 rd = gen_avr_ptr(rD(ctx->opcode)); \
7185 gen_helper_##name (rd, rb, uimm); \
7186 tcg_temp_free_i32(uimm); \
7187 tcg_temp_free_ptr(rb); \
7188 tcg_temp_free_ptr(rd); \
7189 }
7190
d15f74fb
BS
7191#define GEN_VXFORM_UIMM_ENV(name, opc2, opc3) \
7192static void glue(gen_, name)(DisasContext *ctx) \
7193 { \
7194 TCGv_ptr rb, rd; \
7195 TCGv_i32 uimm; \
7196 \
7197 if (unlikely(!ctx->altivec_enabled)) { \
7198 gen_exception(ctx, POWERPC_EXCP_VPU); \
7199 return; \
7200 } \
7201 uimm = tcg_const_i32(UIMM5(ctx->opcode)); \
7202 rb = gen_avr_ptr(rB(ctx->opcode)); \
7203 rd = gen_avr_ptr(rD(ctx->opcode)); \
7204 gen_helper_##name(cpu_env, rd, rb, uimm); \
7205 tcg_temp_free_i32(uimm); \
7206 tcg_temp_free_ptr(rb); \
7207 tcg_temp_free_ptr(rd); \
7208 }
7209
e4e6bee7
AJ
7210GEN_VXFORM_UIMM(vspltb, 6, 8);
7211GEN_VXFORM_UIMM(vsplth, 6, 9);
7212GEN_VXFORM_UIMM(vspltw, 6, 10);
d15f74fb
BS
7213GEN_VXFORM_UIMM_ENV(vcfux, 5, 12);
7214GEN_VXFORM_UIMM_ENV(vcfsx, 5, 13);
7215GEN_VXFORM_UIMM_ENV(vctuxs, 5, 14);
7216GEN_VXFORM_UIMM_ENV(vctsxs, 5, 15);
e4e6bee7 7217
99e300ef 7218static void gen_vsldoi(DisasContext *ctx)
cd633b10
AJ
7219{
7220 TCGv_ptr ra, rb, rd;
fce5ecb7 7221 TCGv_i32 sh;
cd633b10
AJ
7222 if (unlikely(!ctx->altivec_enabled)) {
7223 gen_exception(ctx, POWERPC_EXCP_VPU);
7224 return;
7225 }
7226 ra = gen_avr_ptr(rA(ctx->opcode));
7227 rb = gen_avr_ptr(rB(ctx->opcode));
7228 rd = gen_avr_ptr(rD(ctx->opcode));
7229 sh = tcg_const_i32(VSH(ctx->opcode));
7230 gen_helper_vsldoi (rd, ra, rb, sh);
7231 tcg_temp_free_ptr(ra);
7232 tcg_temp_free_ptr(rb);
7233 tcg_temp_free_ptr(rd);
fce5ecb7 7234 tcg_temp_free_i32(sh);
cd633b10
AJ
7235}
7236
707cec33 7237#define GEN_VAFORM_PAIRED(name0, name1, opc2) \
d15f74fb 7238static void glue(gen_, name0##_##name1)(DisasContext *ctx) \
707cec33
AJ
7239 { \
7240 TCGv_ptr ra, rb, rc, rd; \
7241 if (unlikely(!ctx->altivec_enabled)) { \
7242 gen_exception(ctx, POWERPC_EXCP_VPU); \
7243 return; \
7244 } \
7245 ra = gen_avr_ptr(rA(ctx->opcode)); \
7246 rb = gen_avr_ptr(rB(ctx->opcode)); \
7247 rc = gen_avr_ptr(rC(ctx->opcode)); \
7248 rd = gen_avr_ptr(rD(ctx->opcode)); \
7249 if (Rc(ctx->opcode)) { \
d15f74fb 7250 gen_helper_##name1(cpu_env, rd, ra, rb, rc); \
707cec33 7251 } else { \
d15f74fb 7252 gen_helper_##name0(cpu_env, rd, ra, rb, rc); \
707cec33
AJ
7253 } \
7254 tcg_temp_free_ptr(ra); \
7255 tcg_temp_free_ptr(rb); \
7256 tcg_temp_free_ptr(rc); \
7257 tcg_temp_free_ptr(rd); \
7258 }
7259
b161ae27
AJ
7260GEN_VAFORM_PAIRED(vmhaddshs, vmhraddshs, 16)
7261
99e300ef 7262static void gen_vmladduhm(DisasContext *ctx)
bcd2ee23
AJ
7263{
7264 TCGv_ptr ra, rb, rc, rd;
7265 if (unlikely(!ctx->altivec_enabled)) {
7266 gen_exception(ctx, POWERPC_EXCP_VPU);
7267 return;
7268 }
7269 ra = gen_avr_ptr(rA(ctx->opcode));
7270 rb = gen_avr_ptr(rB(ctx->opcode));
7271 rc = gen_avr_ptr(rC(ctx->opcode));
7272 rd = gen_avr_ptr(rD(ctx->opcode));
7273 gen_helper_vmladduhm(rd, ra, rb, rc);
7274 tcg_temp_free_ptr(ra);
7275 tcg_temp_free_ptr(rb);
7276 tcg_temp_free_ptr(rc);
7277 tcg_temp_free_ptr(rd);
7278}
7279
b04ae981 7280GEN_VAFORM_PAIRED(vmsumubm, vmsummbm, 18)
4d9903b6 7281GEN_VAFORM_PAIRED(vmsumuhm, vmsumuhs, 19)
eae07261 7282GEN_VAFORM_PAIRED(vmsumshm, vmsumshs, 20)
d1258698 7283GEN_VAFORM_PAIRED(vsel, vperm, 21)
35cf7c7e 7284GEN_VAFORM_PAIRED(vmaddfp, vnmsubfp, 23)
b04ae981 7285
472b24ce
TM
7286/*** VSX extension ***/
7287
7288static inline TCGv_i64 cpu_vsrh(int n)
7289{
7290 if (n < 32) {
7291 return cpu_fpr[n];
7292 } else {
7293 return cpu_avrh[n-32];
7294 }
7295}
7296
7297static inline TCGv_i64 cpu_vsrl(int n)
7298{
7299 if (n < 32) {
7300 return cpu_vsr[n];
7301 } else {
7302 return cpu_avrl[n-32];
7303 }
7304}
7305
e072fe79
TM
7306#define VSX_LOAD_SCALAR(name, operation) \
7307static void gen_##name(DisasContext *ctx) \
7308{ \
7309 TCGv EA; \
7310 if (unlikely(!ctx->vsx_enabled)) { \
7311 gen_exception(ctx, POWERPC_EXCP_VSXU); \
7312 return; \
7313 } \
7314 gen_set_access_type(ctx, ACCESS_INT); \
7315 EA = tcg_temp_new(); \
7316 gen_addr_reg_index(ctx, EA); \
7317 gen_qemu_##operation(ctx, cpu_vsrh(xT(ctx->opcode)), EA); \
7318 /* NOTE: cpu_vsrl is undefined */ \
7319 tcg_temp_free(EA); \
7320}
7321
7322VSX_LOAD_SCALAR(lxsdx, ld64)
cac7f0ba
TM
7323VSX_LOAD_SCALAR(lxsiwax, ld32s_i64)
7324VSX_LOAD_SCALAR(lxsiwzx, ld32u_i64)
7325VSX_LOAD_SCALAR(lxsspx, ld32fs)
fa1832d7 7326
304af367
TM
7327static void gen_lxvd2x(DisasContext *ctx)
7328{
7329 TCGv EA;
7330 if (unlikely(!ctx->vsx_enabled)) {
7331 gen_exception(ctx, POWERPC_EXCP_VSXU);
7332 return;
7333 }
7334 gen_set_access_type(ctx, ACCESS_INT);
7335 EA = tcg_temp_new();
7336 gen_addr_reg_index(ctx, EA);
7337 gen_qemu_ld64(ctx, cpu_vsrh(xT(ctx->opcode)), EA);
7338 tcg_gen_addi_tl(EA, EA, 8);
7339 gen_qemu_ld64(ctx, cpu_vsrl(xT(ctx->opcode)), EA);
7340 tcg_temp_free(EA);
7341}
7342
ca03b467
TM
7343static void gen_lxvdsx(DisasContext *ctx)
7344{
7345 TCGv EA;
7346 if (unlikely(!ctx->vsx_enabled)) {
7347 gen_exception(ctx, POWERPC_EXCP_VSXU);
7348 return;
7349 }
7350 gen_set_access_type(ctx, ACCESS_INT);
7351 EA = tcg_temp_new();
7352 gen_addr_reg_index(ctx, EA);
7353 gen_qemu_ld64(ctx, cpu_vsrh(xT(ctx->opcode)), EA);
f976b09e 7354 tcg_gen_mov_i64(cpu_vsrl(xT(ctx->opcode)), cpu_vsrh(xT(ctx->opcode)));
ca03b467
TM
7355 tcg_temp_free(EA);
7356}
7357
897e61d1
TM
7358static void gen_lxvw4x(DisasContext *ctx)
7359{
f976b09e
AG
7360 TCGv EA;
7361 TCGv_i64 tmp;
897e61d1
TM
7362 TCGv_i64 xth = cpu_vsrh(xT(ctx->opcode));
7363 TCGv_i64 xtl = cpu_vsrl(xT(ctx->opcode));
7364 if (unlikely(!ctx->vsx_enabled)) {
7365 gen_exception(ctx, POWERPC_EXCP_VSXU);
7366 return;
7367 }
7368 gen_set_access_type(ctx, ACCESS_INT);
7369 EA = tcg_temp_new();
f976b09e
AG
7370 tmp = tcg_temp_new_i64();
7371
897e61d1 7372 gen_addr_reg_index(ctx, EA);
f976b09e 7373 gen_qemu_ld32u_i64(ctx, tmp, EA);
897e61d1 7374 tcg_gen_addi_tl(EA, EA, 4);
f976b09e 7375 gen_qemu_ld32u_i64(ctx, xth, EA);
897e61d1
TM
7376 tcg_gen_deposit_i64(xth, xth, tmp, 32, 32);
7377
7378 tcg_gen_addi_tl(EA, EA, 4);
f976b09e 7379 gen_qemu_ld32u_i64(ctx, tmp, EA);
897e61d1 7380 tcg_gen_addi_tl(EA, EA, 4);
f976b09e 7381 gen_qemu_ld32u_i64(ctx, xtl, EA);
897e61d1
TM
7382 tcg_gen_deposit_i64(xtl, xtl, tmp, 32, 32);
7383
7384 tcg_temp_free(EA);
f976b09e 7385 tcg_temp_free_i64(tmp);
897e61d1
TM
7386}
7387
f026da78
TM
7388#define VSX_STORE_SCALAR(name, operation) \
7389static void gen_##name(DisasContext *ctx) \
7390{ \
7391 TCGv EA; \
7392 if (unlikely(!ctx->vsx_enabled)) { \
7393 gen_exception(ctx, POWERPC_EXCP_VSXU); \
7394 return; \
7395 } \
7396 gen_set_access_type(ctx, ACCESS_INT); \
7397 EA = tcg_temp_new(); \
7398 gen_addr_reg_index(ctx, EA); \
7399 gen_qemu_##operation(ctx, cpu_vsrh(xS(ctx->opcode)), EA); \
7400 tcg_temp_free(EA); \
9231ba9e
TM
7401}
7402
f026da78 7403VSX_STORE_SCALAR(stxsdx, st64)
e16a626b
TM
7404VSX_STORE_SCALAR(stxsiwx, st32_i64)
7405VSX_STORE_SCALAR(stxsspx, st32fs)
f026da78 7406
fbed2478
TM
7407static void gen_stxvd2x(DisasContext *ctx)
7408{
7409 TCGv EA;
7410 if (unlikely(!ctx->vsx_enabled)) {
7411 gen_exception(ctx, POWERPC_EXCP_VSXU);
7412 return;
7413 }
7414 gen_set_access_type(ctx, ACCESS_INT);
7415 EA = tcg_temp_new();
7416 gen_addr_reg_index(ctx, EA);
7417 gen_qemu_st64(ctx, cpu_vsrh(xS(ctx->opcode)), EA);
7418 tcg_gen_addi_tl(EA, EA, 8);
7419 gen_qemu_st64(ctx, cpu_vsrl(xS(ctx->opcode)), EA);
7420 tcg_temp_free(EA);
7421}
7422
86e61ce3
TM
7423static void gen_stxvw4x(DisasContext *ctx)
7424{
f976b09e
AG
7425 TCGv_i64 tmp;
7426 TCGv EA;
86e61ce3
TM
7427 if (unlikely(!ctx->vsx_enabled)) {
7428 gen_exception(ctx, POWERPC_EXCP_VSXU);
7429 return;
7430 }
7431 gen_set_access_type(ctx, ACCESS_INT);
7432 EA = tcg_temp_new();
7433 gen_addr_reg_index(ctx, EA);
f976b09e 7434 tmp = tcg_temp_new_i64();
86e61ce3
TM
7435
7436 tcg_gen_shri_i64(tmp, cpu_vsrh(xS(ctx->opcode)), 32);
f976b09e 7437 gen_qemu_st32_i64(ctx, tmp, EA);
86e61ce3 7438 tcg_gen_addi_tl(EA, EA, 4);
f976b09e 7439 gen_qemu_st32_i64(ctx, cpu_vsrh(xS(ctx->opcode)), EA);
86e61ce3
TM
7440
7441 tcg_gen_shri_i64(tmp, cpu_vsrl(xS(ctx->opcode)), 32);
7442 tcg_gen_addi_tl(EA, EA, 4);
f976b09e 7443 gen_qemu_st32_i64(ctx, tmp, EA);
86e61ce3 7444 tcg_gen_addi_tl(EA, EA, 4);
f976b09e 7445 gen_qemu_st32_i64(ctx, cpu_vsrl(xS(ctx->opcode)), EA);
86e61ce3
TM
7446
7447 tcg_temp_free(EA);
f976b09e 7448 tcg_temp_free_i64(tmp);
86e61ce3
TM
7449}
7450
f5c0f7f9
TM
7451#define MV_VSRW(name, tcgop1, tcgop2, target, source) \
7452static void gen_##name(DisasContext *ctx) \
7453{ \
7454 if (xS(ctx->opcode) < 32) { \
7455 if (unlikely(!ctx->fpu_enabled)) { \
7456 gen_exception(ctx, POWERPC_EXCP_FPU); \
7457 return; \
7458 } \
7459 } else { \
7460 if (unlikely(!ctx->altivec_enabled)) { \
7461 gen_exception(ctx, POWERPC_EXCP_VPU); \
7462 return; \
7463 } \
7464 } \
7465 TCGv_i64 tmp = tcg_temp_new_i64(); \
7466 tcg_gen_##tcgop1(tmp, source); \
7467 tcg_gen_##tcgop2(target, tmp); \
7468 tcg_temp_free_i64(tmp); \
7469}
7470
7471
7472MV_VSRW(mfvsrwz, ext32u_i64, trunc_i64_tl, cpu_gpr[rA(ctx->opcode)], \
7473 cpu_vsrh(xS(ctx->opcode)))
7474MV_VSRW(mtvsrwa, extu_tl_i64, ext32s_i64, cpu_vsrh(xT(ctx->opcode)), \
7475 cpu_gpr[rA(ctx->opcode)])
7476MV_VSRW(mtvsrwz, extu_tl_i64, ext32u_i64, cpu_vsrh(xT(ctx->opcode)), \
7477 cpu_gpr[rA(ctx->opcode)])
7478
7479#if defined(TARGET_PPC64)
7480#define MV_VSRD(name, target, source) \
7481static void gen_##name(DisasContext *ctx) \
7482{ \
7483 if (xS(ctx->opcode) < 32) { \
7484 if (unlikely(!ctx->fpu_enabled)) { \
7485 gen_exception(ctx, POWERPC_EXCP_FPU); \
7486 return; \
7487 } \
7488 } else { \
7489 if (unlikely(!ctx->altivec_enabled)) { \
7490 gen_exception(ctx, POWERPC_EXCP_VPU); \
7491 return; \
7492 } \
7493 } \
7494 tcg_gen_mov_i64(target, source); \
7495}
7496
7497MV_VSRD(mfvsrd, cpu_gpr[rA(ctx->opcode)], cpu_vsrh(xS(ctx->opcode)))
7498MV_VSRD(mtvsrd, cpu_vsrh(xT(ctx->opcode)), cpu_gpr[rA(ctx->opcode)])
7499
7500#endif
7501
cd73f2c9
TM
7502static void gen_xxpermdi(DisasContext *ctx)
7503{
7504 if (unlikely(!ctx->vsx_enabled)) {
7505 gen_exception(ctx, POWERPC_EXCP_VSXU);
7506 return;
7507 }
7508
f5bc1bfa
TM
7509 if (unlikely((xT(ctx->opcode) == xA(ctx->opcode)) ||
7510 (xT(ctx->opcode) == xB(ctx->opcode)))) {
7511 TCGv_i64 xh, xl;
7512
7513 xh = tcg_temp_new_i64();
7514 xl = tcg_temp_new_i64();
7515
7516 if ((DM(ctx->opcode) & 2) == 0) {
7517 tcg_gen_mov_i64(xh, cpu_vsrh(xA(ctx->opcode)));
7518 } else {
7519 tcg_gen_mov_i64(xh, cpu_vsrl(xA(ctx->opcode)));
7520 }
7521 if ((DM(ctx->opcode) & 1) == 0) {
7522 tcg_gen_mov_i64(xl, cpu_vsrh(xB(ctx->opcode)));
7523 } else {
7524 tcg_gen_mov_i64(xl, cpu_vsrl(xB(ctx->opcode)));
7525 }
7526
7527 tcg_gen_mov_i64(cpu_vsrh(xT(ctx->opcode)), xh);
7528 tcg_gen_mov_i64(cpu_vsrl(xT(ctx->opcode)), xl);
7529
7530 tcg_temp_free_i64(xh);
7531 tcg_temp_free_i64(xl);
cd73f2c9 7532 } else {
f5bc1bfa
TM
7533 if ((DM(ctx->opcode) & 2) == 0) {
7534 tcg_gen_mov_i64(cpu_vsrh(xT(ctx->opcode)), cpu_vsrh(xA(ctx->opcode)));
7535 } else {
7536 tcg_gen_mov_i64(cpu_vsrh(xT(ctx->opcode)), cpu_vsrl(xA(ctx->opcode)));
7537 }
7538 if ((DM(ctx->opcode) & 1) == 0) {
7539 tcg_gen_mov_i64(cpu_vsrl(xT(ctx->opcode)), cpu_vsrh(xB(ctx->opcode)));
7540 } else {
7541 tcg_gen_mov_i64(cpu_vsrl(xT(ctx->opcode)), cpu_vsrl(xB(ctx->opcode)));
7542 }
cd73f2c9
TM
7543 }
7544}
7545
df020ce0
TM
7546#define OP_ABS 1
7547#define OP_NABS 2
7548#define OP_NEG 3
7549#define OP_CPSGN 4
7550#define SGN_MASK_DP 0x8000000000000000ul
7551#define SGN_MASK_SP 0x8000000080000000ul
7552
7553#define VSX_SCALAR_MOVE(name, op, sgn_mask) \
7554static void glue(gen_, name)(DisasContext * ctx) \
7555 { \
7556 TCGv_i64 xb, sgm; \
7557 if (unlikely(!ctx->vsx_enabled)) { \
7558 gen_exception(ctx, POWERPC_EXCP_VSXU); \
7559 return; \
7560 } \
f976b09e
AG
7561 xb = tcg_temp_new_i64(); \
7562 sgm = tcg_temp_new_i64(); \
df020ce0
TM
7563 tcg_gen_mov_i64(xb, cpu_vsrh(xB(ctx->opcode))); \
7564 tcg_gen_movi_i64(sgm, sgn_mask); \
7565 switch (op) { \
7566 case OP_ABS: { \
7567 tcg_gen_andc_i64(xb, xb, sgm); \
7568 break; \
7569 } \
7570 case OP_NABS: { \
7571 tcg_gen_or_i64(xb, xb, sgm); \
7572 break; \
7573 } \
7574 case OP_NEG: { \
7575 tcg_gen_xor_i64(xb, xb, sgm); \
7576 break; \
7577 } \
7578 case OP_CPSGN: { \
f976b09e 7579 TCGv_i64 xa = tcg_temp_new_i64(); \
df020ce0
TM
7580 tcg_gen_mov_i64(xa, cpu_vsrh(xA(ctx->opcode))); \
7581 tcg_gen_and_i64(xa, xa, sgm); \
7582 tcg_gen_andc_i64(xb, xb, sgm); \
7583 tcg_gen_or_i64(xb, xb, xa); \
f976b09e 7584 tcg_temp_free_i64(xa); \
df020ce0
TM
7585 break; \
7586 } \
7587 } \
7588 tcg_gen_mov_i64(cpu_vsrh(xT(ctx->opcode)), xb); \
f976b09e
AG
7589 tcg_temp_free_i64(xb); \
7590 tcg_temp_free_i64(sgm); \
df020ce0
TM
7591 }
7592
7593VSX_SCALAR_MOVE(xsabsdp, OP_ABS, SGN_MASK_DP)
7594VSX_SCALAR_MOVE(xsnabsdp, OP_NABS, SGN_MASK_DP)
7595VSX_SCALAR_MOVE(xsnegdp, OP_NEG, SGN_MASK_DP)
7596VSX_SCALAR_MOVE(xscpsgndp, OP_CPSGN, SGN_MASK_DP)
7597
be574920
TM
7598#define VSX_VECTOR_MOVE(name, op, sgn_mask) \
7599static void glue(gen_, name)(DisasContext * ctx) \
7600 { \
7601 TCGv_i64 xbh, xbl, sgm; \
7602 if (unlikely(!ctx->vsx_enabled)) { \
7603 gen_exception(ctx, POWERPC_EXCP_VSXU); \
7604 return; \
7605 } \
f976b09e
AG
7606 xbh = tcg_temp_new_i64(); \
7607 xbl = tcg_temp_new_i64(); \
7608 sgm = tcg_temp_new_i64(); \
be574920
TM
7609 tcg_gen_mov_i64(xbh, cpu_vsrh(xB(ctx->opcode))); \
7610 tcg_gen_mov_i64(xbl, cpu_vsrl(xB(ctx->opcode))); \
7611 tcg_gen_movi_i64(sgm, sgn_mask); \
7612 switch (op) { \
7613 case OP_ABS: { \
7614 tcg_gen_andc_i64(xbh, xbh, sgm); \
7615 tcg_gen_andc_i64(xbl, xbl, sgm); \
7616 break; \
7617 } \
7618 case OP_NABS: { \
7619 tcg_gen_or_i64(xbh, xbh, sgm); \
7620 tcg_gen_or_i64(xbl, xbl, sgm); \
7621 break; \
7622 } \
7623 case OP_NEG: { \
7624 tcg_gen_xor_i64(xbh, xbh, sgm); \
7625 tcg_gen_xor_i64(xbl, xbl, sgm); \
7626 break; \
7627 } \
7628 case OP_CPSGN: { \
f976b09e
AG
7629 TCGv_i64 xah = tcg_temp_new_i64(); \
7630 TCGv_i64 xal = tcg_temp_new_i64(); \
be574920
TM
7631 tcg_gen_mov_i64(xah, cpu_vsrh(xA(ctx->opcode))); \
7632 tcg_gen_mov_i64(xal, cpu_vsrl(xA(ctx->opcode))); \
7633 tcg_gen_and_i64(xah, xah, sgm); \
7634 tcg_gen_and_i64(xal, xal, sgm); \
7635 tcg_gen_andc_i64(xbh, xbh, sgm); \
7636 tcg_gen_andc_i64(xbl, xbl, sgm); \
7637 tcg_gen_or_i64(xbh, xbh, xah); \
7638 tcg_gen_or_i64(xbl, xbl, xal); \
f976b09e
AG
7639 tcg_temp_free_i64(xah); \
7640 tcg_temp_free_i64(xal); \
be574920
TM
7641 break; \
7642 } \
7643 } \
7644 tcg_gen_mov_i64(cpu_vsrh(xT(ctx->opcode)), xbh); \
7645 tcg_gen_mov_i64(cpu_vsrl(xT(ctx->opcode)), xbl); \
f976b09e
AG
7646 tcg_temp_free_i64(xbh); \
7647 tcg_temp_free_i64(xbl); \
7648 tcg_temp_free_i64(sgm); \
be574920
TM
7649 }
7650
7651VSX_VECTOR_MOVE(xvabsdp, OP_ABS, SGN_MASK_DP)
7652VSX_VECTOR_MOVE(xvnabsdp, OP_NABS, SGN_MASK_DP)
7653VSX_VECTOR_MOVE(xvnegdp, OP_NEG, SGN_MASK_DP)
7654VSX_VECTOR_MOVE(xvcpsgndp, OP_CPSGN, SGN_MASK_DP)
7655VSX_VECTOR_MOVE(xvabssp, OP_ABS, SGN_MASK_SP)
7656VSX_VECTOR_MOVE(xvnabssp, OP_NABS, SGN_MASK_SP)
7657VSX_VECTOR_MOVE(xvnegsp, OP_NEG, SGN_MASK_SP)
7658VSX_VECTOR_MOVE(xvcpsgnsp, OP_CPSGN, SGN_MASK_SP)
7659
3c3cbbdc
TM
7660#define GEN_VSX_HELPER_2(name, op1, op2, inval, type) \
7661static void gen_##name(DisasContext * ctx) \
7662{ \
7663 TCGv_i32 opc; \
7664 if (unlikely(!ctx->vsx_enabled)) { \
7665 gen_exception(ctx, POWERPC_EXCP_VSXU); \
7666 return; \
7667 } \
7668 /* NIP cannot be restored if the memory exception comes from an helper */ \
7669 gen_update_nip(ctx, ctx->nip - 4); \
7670 opc = tcg_const_i32(ctx->opcode); \
7671 gen_helper_##name(cpu_env, opc); \
7672 tcg_temp_free_i32(opc); \
7673}
be574920 7674
3d1140bf
TM
7675#define GEN_VSX_HELPER_XT_XB_ENV(name, op1, op2, inval, type) \
7676static void gen_##name(DisasContext * ctx) \
7677{ \
7678 if (unlikely(!ctx->vsx_enabled)) { \
7679 gen_exception(ctx, POWERPC_EXCP_VSXU); \
7680 return; \
7681 } \
7682 /* NIP cannot be restored if the exception comes */ \
7683 /* from a helper. */ \
7684 gen_update_nip(ctx, ctx->nip - 4); \
7685 \
7686 gen_helper_##name(cpu_vsrh(xT(ctx->opcode)), cpu_env, \
7687 cpu_vsrh(xB(ctx->opcode))); \
7688}
7689
ee6e02c0
TM
7690GEN_VSX_HELPER_2(xsadddp, 0x00, 0x04, 0, PPC2_VSX)
7691GEN_VSX_HELPER_2(xssubdp, 0x00, 0x05, 0, PPC2_VSX)
5e591d88 7692GEN_VSX_HELPER_2(xsmuldp, 0x00, 0x06, 0, PPC2_VSX)
4b98eeef 7693GEN_VSX_HELPER_2(xsdivdp, 0x00, 0x07, 0, PPC2_VSX)
2009227f 7694GEN_VSX_HELPER_2(xsredp, 0x14, 0x05, 0, PPC2_VSX)
d32404fe 7695GEN_VSX_HELPER_2(xssqrtdp, 0x16, 0x04, 0, PPC2_VSX)
d3f9df8f 7696GEN_VSX_HELPER_2(xsrsqrtedp, 0x14, 0x04, 0, PPC2_VSX)
bc80838f 7697GEN_VSX_HELPER_2(xstdivdp, 0x14, 0x07, 0, PPC2_VSX)
5cb151ac 7698GEN_VSX_HELPER_2(xstsqrtdp, 0x14, 0x06, 0, PPC2_VSX)
595c6eef
TM
7699GEN_VSX_HELPER_2(xsmaddadp, 0x04, 0x04, 0, PPC2_VSX)
7700GEN_VSX_HELPER_2(xsmaddmdp, 0x04, 0x05, 0, PPC2_VSX)
7701GEN_VSX_HELPER_2(xsmsubadp, 0x04, 0x06, 0, PPC2_VSX)
7702GEN_VSX_HELPER_2(xsmsubmdp, 0x04, 0x07, 0, PPC2_VSX)
7703GEN_VSX_HELPER_2(xsnmaddadp, 0x04, 0x14, 0, PPC2_VSX)
7704GEN_VSX_HELPER_2(xsnmaddmdp, 0x04, 0x15, 0, PPC2_VSX)
7705GEN_VSX_HELPER_2(xsnmsubadp, 0x04, 0x16, 0, PPC2_VSX)
7706GEN_VSX_HELPER_2(xsnmsubmdp, 0x04, 0x17, 0, PPC2_VSX)
4f17e9c7
TM
7707GEN_VSX_HELPER_2(xscmpodp, 0x0C, 0x05, 0, PPC2_VSX)
7708GEN_VSX_HELPER_2(xscmpudp, 0x0C, 0x04, 0, PPC2_VSX)
959e9c9d
TM
7709GEN_VSX_HELPER_2(xsmaxdp, 0x00, 0x14, 0, PPC2_VSX)
7710GEN_VSX_HELPER_2(xsmindp, 0x00, 0x15, 0, PPC2_VSX)
ed8ac568 7711GEN_VSX_HELPER_2(xscvdpsp, 0x12, 0x10, 0, PPC2_VSX)
7ee19fb9 7712GEN_VSX_HELPER_XT_XB_ENV(xscvdpspn, 0x16, 0x10, 0, PPC2_VSX207)
ed8ac568 7713GEN_VSX_HELPER_2(xscvspdp, 0x12, 0x14, 0, PPC2_VSX)
7ee19fb9 7714GEN_VSX_HELPER_XT_XB_ENV(xscvspdpn, 0x16, 0x14, 0, PPC2_VSX207)
5177d2ca
TM
7715GEN_VSX_HELPER_2(xscvdpsxds, 0x10, 0x15, 0, PPC2_VSX)
7716GEN_VSX_HELPER_2(xscvdpsxws, 0x10, 0x05, 0, PPC2_VSX)
7717GEN_VSX_HELPER_2(xscvdpuxds, 0x10, 0x14, 0, PPC2_VSX)
7718GEN_VSX_HELPER_2(xscvdpuxws, 0x10, 0x04, 0, PPC2_VSX)
7719GEN_VSX_HELPER_2(xscvsxddp, 0x10, 0x17, 0, PPC2_VSX)
7720GEN_VSX_HELPER_2(xscvuxddp, 0x10, 0x16, 0, PPC2_VSX)
88e33d08
TM
7721GEN_VSX_HELPER_2(xsrdpi, 0x12, 0x04, 0, PPC2_VSX)
7722GEN_VSX_HELPER_2(xsrdpic, 0x16, 0x06, 0, PPC2_VSX)
7723GEN_VSX_HELPER_2(xsrdpim, 0x12, 0x07, 0, PPC2_VSX)
7724GEN_VSX_HELPER_2(xsrdpip, 0x12, 0x06, 0, PPC2_VSX)
7725GEN_VSX_HELPER_2(xsrdpiz, 0x12, 0x05, 0, PPC2_VSX)
3d1140bf 7726GEN_VSX_HELPER_XT_XB_ENV(xsrsp, 0x12, 0x11, 0, PPC2_VSX207)
ee6e02c0 7727
3fd0aadf
TM
7728GEN_VSX_HELPER_2(xsaddsp, 0x00, 0x00, 0, PPC2_VSX207)
7729GEN_VSX_HELPER_2(xssubsp, 0x00, 0x01, 0, PPC2_VSX207)
ab9408a2 7730GEN_VSX_HELPER_2(xsmulsp, 0x00, 0x02, 0, PPC2_VSX207)
b24d0b47 7731GEN_VSX_HELPER_2(xsdivsp, 0x00, 0x03, 0, PPC2_VSX207)
2c0c52ae 7732GEN_VSX_HELPER_2(xsresp, 0x14, 0x01, 0, PPC2_VSX207)
cea4e574 7733GEN_VSX_HELPER_2(xssqrtsp, 0x16, 0x00, 0, PPC2_VSX207)
968e76bc 7734GEN_VSX_HELPER_2(xsrsqrtesp, 0x14, 0x00, 0, PPC2_VSX207)
f53f81e0
TM
7735GEN_VSX_HELPER_2(xsmaddasp, 0x04, 0x00, 0, PPC2_VSX207)
7736GEN_VSX_HELPER_2(xsmaddmsp, 0x04, 0x01, 0, PPC2_VSX207)
7737GEN_VSX_HELPER_2(xsmsubasp, 0x04, 0x02, 0, PPC2_VSX207)
7738GEN_VSX_HELPER_2(xsmsubmsp, 0x04, 0x03, 0, PPC2_VSX207)
7739GEN_VSX_HELPER_2(xsnmaddasp, 0x04, 0x10, 0, PPC2_VSX207)
7740GEN_VSX_HELPER_2(xsnmaddmsp, 0x04, 0x11, 0, PPC2_VSX207)
7741GEN_VSX_HELPER_2(xsnmsubasp, 0x04, 0x12, 0, PPC2_VSX207)
7742GEN_VSX_HELPER_2(xsnmsubmsp, 0x04, 0x13, 0, PPC2_VSX207)
74698350
TM
7743GEN_VSX_HELPER_2(xscvsxdsp, 0x10, 0x13, 0, PPC2_VSX207)
7744GEN_VSX_HELPER_2(xscvuxdsp, 0x10, 0x12, 0, PPC2_VSX207)
3fd0aadf 7745
ee6e02c0
TM
7746GEN_VSX_HELPER_2(xvadddp, 0x00, 0x0C, 0, PPC2_VSX)
7747GEN_VSX_HELPER_2(xvsubdp, 0x00, 0x0D, 0, PPC2_VSX)
5e591d88 7748GEN_VSX_HELPER_2(xvmuldp, 0x00, 0x0E, 0, PPC2_VSX)
4b98eeef 7749GEN_VSX_HELPER_2(xvdivdp, 0x00, 0x0F, 0, PPC2_VSX)
2009227f 7750GEN_VSX_HELPER_2(xvredp, 0x14, 0x0D, 0, PPC2_VSX)
d32404fe 7751GEN_VSX_HELPER_2(xvsqrtdp, 0x16, 0x0C, 0, PPC2_VSX)
d3f9df8f 7752GEN_VSX_HELPER_2(xvrsqrtedp, 0x14, 0x0C, 0, PPC2_VSX)
bc80838f 7753GEN_VSX_HELPER_2(xvtdivdp, 0x14, 0x0F, 0, PPC2_VSX)
5cb151ac 7754GEN_VSX_HELPER_2(xvtsqrtdp, 0x14, 0x0E, 0, PPC2_VSX)
595c6eef
TM
7755GEN_VSX_HELPER_2(xvmaddadp, 0x04, 0x0C, 0, PPC2_VSX)
7756GEN_VSX_HELPER_2(xvmaddmdp, 0x04, 0x0D, 0, PPC2_VSX)
7757GEN_VSX_HELPER_2(xvmsubadp, 0x04, 0x0E, 0, PPC2_VSX)
7758GEN_VSX_HELPER_2(xvmsubmdp, 0x04, 0x0F, 0, PPC2_VSX)
7759GEN_VSX_HELPER_2(xvnmaddadp, 0x04, 0x1C, 0, PPC2_VSX)
7760GEN_VSX_HELPER_2(xvnmaddmdp, 0x04, 0x1D, 0, PPC2_VSX)
7761GEN_VSX_HELPER_2(xvnmsubadp, 0x04, 0x1E, 0, PPC2_VSX)
7762GEN_VSX_HELPER_2(xvnmsubmdp, 0x04, 0x1F, 0, PPC2_VSX)
959e9c9d
TM
7763GEN_VSX_HELPER_2(xvmaxdp, 0x00, 0x1C, 0, PPC2_VSX)
7764GEN_VSX_HELPER_2(xvmindp, 0x00, 0x1D, 0, PPC2_VSX)
354a6dec
TM
7765GEN_VSX_HELPER_2(xvcmpeqdp, 0x0C, 0x0C, 0, PPC2_VSX)
7766GEN_VSX_HELPER_2(xvcmpgtdp, 0x0C, 0x0D, 0, PPC2_VSX)
7767GEN_VSX_HELPER_2(xvcmpgedp, 0x0C, 0x0E, 0, PPC2_VSX)
ed8ac568 7768GEN_VSX_HELPER_2(xvcvdpsp, 0x12, 0x18, 0, PPC2_VSX)
5177d2ca
TM
7769GEN_VSX_HELPER_2(xvcvdpsxds, 0x10, 0x1D, 0, PPC2_VSX)
7770GEN_VSX_HELPER_2(xvcvdpsxws, 0x10, 0x0D, 0, PPC2_VSX)
7771GEN_VSX_HELPER_2(xvcvdpuxds, 0x10, 0x1C, 0, PPC2_VSX)
7772GEN_VSX_HELPER_2(xvcvdpuxws, 0x10, 0x0C, 0, PPC2_VSX)
7773GEN_VSX_HELPER_2(xvcvsxddp, 0x10, 0x1F, 0, PPC2_VSX)
7774GEN_VSX_HELPER_2(xvcvuxddp, 0x10, 0x1E, 0, PPC2_VSX)
7775GEN_VSX_HELPER_2(xvcvsxwdp, 0x10, 0x0F, 0, PPC2_VSX)
7776GEN_VSX_HELPER_2(xvcvuxwdp, 0x10, 0x0E, 0, PPC2_VSX)
88e33d08
TM
7777GEN_VSX_HELPER_2(xvrdpi, 0x12, 0x0C, 0, PPC2_VSX)
7778GEN_VSX_HELPER_2(xvrdpic, 0x16, 0x0E, 0, PPC2_VSX)
7779GEN_VSX_HELPER_2(xvrdpim, 0x12, 0x0F, 0, PPC2_VSX)
7780GEN_VSX_HELPER_2(xvrdpip, 0x12, 0x0E, 0, PPC2_VSX)
7781GEN_VSX_HELPER_2(xvrdpiz, 0x12, 0x0D, 0, PPC2_VSX)
ee6e02c0
TM
7782
7783GEN_VSX_HELPER_2(xvaddsp, 0x00, 0x08, 0, PPC2_VSX)
7784GEN_VSX_HELPER_2(xvsubsp, 0x00, 0x09, 0, PPC2_VSX)
5e591d88 7785GEN_VSX_HELPER_2(xvmulsp, 0x00, 0x0A, 0, PPC2_VSX)
4b98eeef 7786GEN_VSX_HELPER_2(xvdivsp, 0x00, 0x0B, 0, PPC2_VSX)
2009227f 7787GEN_VSX_HELPER_2(xvresp, 0x14, 0x09, 0, PPC2_VSX)
d32404fe 7788GEN_VSX_HELPER_2(xvsqrtsp, 0x16, 0x08, 0, PPC2_VSX)
d3f9df8f 7789GEN_VSX_HELPER_2(xvrsqrtesp, 0x14, 0x08, 0, PPC2_VSX)
bc80838f 7790GEN_VSX_HELPER_2(xvtdivsp, 0x14, 0x0B, 0, PPC2_VSX)
5cb151ac 7791GEN_VSX_HELPER_2(xvtsqrtsp, 0x14, 0x0A, 0, PPC2_VSX)
595c6eef
TM
7792GEN_VSX_HELPER_2(xvmaddasp, 0x04, 0x08, 0, PPC2_VSX)
7793GEN_VSX_HELPER_2(xvmaddmsp, 0x04, 0x09, 0, PPC2_VSX)
7794GEN_VSX_HELPER_2(xvmsubasp, 0x04, 0x0A, 0, PPC2_VSX)
7795GEN_VSX_HELPER_2(xvmsubmsp, 0x04, 0x0B, 0, PPC2_VSX)
7796GEN_VSX_HELPER_2(xvnmaddasp, 0x04, 0x18, 0, PPC2_VSX)
7797GEN_VSX_HELPER_2(xvnmaddmsp, 0x04, 0x19, 0, PPC2_VSX)
7798GEN_VSX_HELPER_2(xvnmsubasp, 0x04, 0x1A, 0, PPC2_VSX)
7799GEN_VSX_HELPER_2(xvnmsubmsp, 0x04, 0x1B, 0, PPC2_VSX)
959e9c9d
TM
7800GEN_VSX_HELPER_2(xvmaxsp, 0x00, 0x18, 0, PPC2_VSX)
7801GEN_VSX_HELPER_2(xvminsp, 0x00, 0x19, 0, PPC2_VSX)
354a6dec
TM
7802GEN_VSX_HELPER_2(xvcmpeqsp, 0x0C, 0x08, 0, PPC2_VSX)
7803GEN_VSX_HELPER_2(xvcmpgtsp, 0x0C, 0x09, 0, PPC2_VSX)
7804GEN_VSX_HELPER_2(xvcmpgesp, 0x0C, 0x0A, 0, PPC2_VSX)
ed8ac568 7805GEN_VSX_HELPER_2(xvcvspdp, 0x12, 0x1C, 0, PPC2_VSX)
5177d2ca
TM
7806GEN_VSX_HELPER_2(xvcvspsxds, 0x10, 0x19, 0, PPC2_VSX)
7807GEN_VSX_HELPER_2(xvcvspsxws, 0x10, 0x09, 0, PPC2_VSX)
7808GEN_VSX_HELPER_2(xvcvspuxds, 0x10, 0x18, 0, PPC2_VSX)
7809GEN_VSX_HELPER_2(xvcvspuxws, 0x10, 0x08, 0, PPC2_VSX)
7810GEN_VSX_HELPER_2(xvcvsxdsp, 0x10, 0x1B, 0, PPC2_VSX)
7811GEN_VSX_HELPER_2(xvcvuxdsp, 0x10, 0x1A, 0, PPC2_VSX)
7812GEN_VSX_HELPER_2(xvcvsxwsp, 0x10, 0x0B, 0, PPC2_VSX)
7813GEN_VSX_HELPER_2(xvcvuxwsp, 0x10, 0x0A, 0, PPC2_VSX)
88e33d08
TM
7814GEN_VSX_HELPER_2(xvrspi, 0x12, 0x08, 0, PPC2_VSX)
7815GEN_VSX_HELPER_2(xvrspic, 0x16, 0x0A, 0, PPC2_VSX)
7816GEN_VSX_HELPER_2(xvrspim, 0x12, 0x0B, 0, PPC2_VSX)
7817GEN_VSX_HELPER_2(xvrspip, 0x12, 0x0A, 0, PPC2_VSX)
7818GEN_VSX_HELPER_2(xvrspiz, 0x12, 0x09, 0, PPC2_VSX)
ee6e02c0 7819
79ca8a6a
TM
7820#define VSX_LOGICAL(name, tcg_op) \
7821static void glue(gen_, name)(DisasContext * ctx) \
7822 { \
7823 if (unlikely(!ctx->vsx_enabled)) { \
7824 gen_exception(ctx, POWERPC_EXCP_VSXU); \
7825 return; \
7826 } \
7827 tcg_op(cpu_vsrh(xT(ctx->opcode)), cpu_vsrh(xA(ctx->opcode)), \
7828 cpu_vsrh(xB(ctx->opcode))); \
7829 tcg_op(cpu_vsrl(xT(ctx->opcode)), cpu_vsrl(xA(ctx->opcode)), \
7830 cpu_vsrl(xB(ctx->opcode))); \
7831 }
7832
f976b09e
AG
7833VSX_LOGICAL(xxland, tcg_gen_and_i64)
7834VSX_LOGICAL(xxlandc, tcg_gen_andc_i64)
7835VSX_LOGICAL(xxlor, tcg_gen_or_i64)
7836VSX_LOGICAL(xxlxor, tcg_gen_xor_i64)
7837VSX_LOGICAL(xxlnor, tcg_gen_nor_i64)
67a33f37
TM
7838VSX_LOGICAL(xxleqv, tcg_gen_eqv_i64)
7839VSX_LOGICAL(xxlnand, tcg_gen_nand_i64)
7840VSX_LOGICAL(xxlorc, tcg_gen_orc_i64)
df020ce0 7841
ce577d2e
TM
7842#define VSX_XXMRG(name, high) \
7843static void glue(gen_, name)(DisasContext * ctx) \
7844 { \
7845 TCGv_i64 a0, a1, b0, b1; \
7846 if (unlikely(!ctx->vsx_enabled)) { \
7847 gen_exception(ctx, POWERPC_EXCP_VSXU); \
7848 return; \
7849 } \
f976b09e
AG
7850 a0 = tcg_temp_new_i64(); \
7851 a1 = tcg_temp_new_i64(); \
7852 b0 = tcg_temp_new_i64(); \
7853 b1 = tcg_temp_new_i64(); \
ce577d2e
TM
7854 if (high) { \
7855 tcg_gen_mov_i64(a0, cpu_vsrh(xA(ctx->opcode))); \
7856 tcg_gen_mov_i64(a1, cpu_vsrh(xA(ctx->opcode))); \
7857 tcg_gen_mov_i64(b0, cpu_vsrh(xB(ctx->opcode))); \
7858 tcg_gen_mov_i64(b1, cpu_vsrh(xB(ctx->opcode))); \
7859 } else { \
7860 tcg_gen_mov_i64(a0, cpu_vsrl(xA(ctx->opcode))); \
7861 tcg_gen_mov_i64(a1, cpu_vsrl(xA(ctx->opcode))); \
7862 tcg_gen_mov_i64(b0, cpu_vsrl(xB(ctx->opcode))); \
7863 tcg_gen_mov_i64(b1, cpu_vsrl(xB(ctx->opcode))); \
7864 } \
7865 tcg_gen_shri_i64(a0, a0, 32); \
7866 tcg_gen_shri_i64(b0, b0, 32); \
7867 tcg_gen_deposit_i64(cpu_vsrh(xT(ctx->opcode)), \
7868 b0, a0, 32, 32); \
7869 tcg_gen_deposit_i64(cpu_vsrl(xT(ctx->opcode)), \
7870 b1, a1, 32, 32); \
f976b09e
AG
7871 tcg_temp_free_i64(a0); \
7872 tcg_temp_free_i64(a1); \
7873 tcg_temp_free_i64(b0); \
7874 tcg_temp_free_i64(b1); \
ce577d2e
TM
7875 }
7876
7877VSX_XXMRG(xxmrghw, 1)
7878VSX_XXMRG(xxmrglw, 0)
7879
551e3ef7
TM
7880static void gen_xxsel(DisasContext * ctx)
7881{
7882 TCGv_i64 a, b, c;
7883 if (unlikely(!ctx->vsx_enabled)) {
7884 gen_exception(ctx, POWERPC_EXCP_VSXU);
7885 return;
7886 }
f976b09e
AG
7887 a = tcg_temp_new_i64();
7888 b = tcg_temp_new_i64();
7889 c = tcg_temp_new_i64();
551e3ef7
TM
7890
7891 tcg_gen_mov_i64(a, cpu_vsrh(xA(ctx->opcode)));
7892 tcg_gen_mov_i64(b, cpu_vsrh(xB(ctx->opcode)));
7893 tcg_gen_mov_i64(c, cpu_vsrh(xC(ctx->opcode)));
7894
7895 tcg_gen_and_i64(b, b, c);
7896 tcg_gen_andc_i64(a, a, c);
7897 tcg_gen_or_i64(cpu_vsrh(xT(ctx->opcode)), a, b);
7898
7899 tcg_gen_mov_i64(a, cpu_vsrl(xA(ctx->opcode)));
7900 tcg_gen_mov_i64(b, cpu_vsrl(xB(ctx->opcode)));
7901 tcg_gen_mov_i64(c, cpu_vsrl(xC(ctx->opcode)));
7902
7903 tcg_gen_and_i64(b, b, c);
7904 tcg_gen_andc_i64(a, a, c);
7905 tcg_gen_or_i64(cpu_vsrl(xT(ctx->opcode)), a, b);
7906
f976b09e
AG
7907 tcg_temp_free_i64(a);
7908 tcg_temp_free_i64(b);
7909 tcg_temp_free_i64(c);
551e3ef7
TM
7910}
7911
76c15fe0
TM
7912static void gen_xxspltw(DisasContext *ctx)
7913{
7914 TCGv_i64 b, b2;
7915 TCGv_i64 vsr = (UIM(ctx->opcode) & 2) ?
7916 cpu_vsrl(xB(ctx->opcode)) :
7917 cpu_vsrh(xB(ctx->opcode));
7918
7919 if (unlikely(!ctx->vsx_enabled)) {
7920 gen_exception(ctx, POWERPC_EXCP_VSXU);
7921 return;
7922 }
7923
f976b09e
AG
7924 b = tcg_temp_new_i64();
7925 b2 = tcg_temp_new_i64();
76c15fe0
TM
7926
7927 if (UIM(ctx->opcode) & 1) {
7928 tcg_gen_ext32u_i64(b, vsr);
7929 } else {
7930 tcg_gen_shri_i64(b, vsr, 32);
7931 }
7932
7933 tcg_gen_shli_i64(b2, b, 32);
7934 tcg_gen_or_i64(cpu_vsrh(xT(ctx->opcode)), b, b2);
7935 tcg_gen_mov_i64(cpu_vsrl(xT(ctx->opcode)), cpu_vsrh(xT(ctx->opcode)));
7936
f976b09e
AG
7937 tcg_temp_free_i64(b);
7938 tcg_temp_free_i64(b2);
76c15fe0
TM
7939}
7940
acc42968
TM
7941static void gen_xxsldwi(DisasContext *ctx)
7942{
7943 TCGv_i64 xth, xtl;
7944 if (unlikely(!ctx->vsx_enabled)) {
7945 gen_exception(ctx, POWERPC_EXCP_VSXU);
7946 return;
7947 }
f976b09e
AG
7948 xth = tcg_temp_new_i64();
7949 xtl = tcg_temp_new_i64();
acc42968
TM
7950
7951 switch (SHW(ctx->opcode)) {
7952 case 0: {
7953 tcg_gen_mov_i64(xth, cpu_vsrh(xA(ctx->opcode)));
7954 tcg_gen_mov_i64(xtl, cpu_vsrl(xA(ctx->opcode)));
7955 break;
7956 }
7957 case 1: {
f976b09e 7958 TCGv_i64 t0 = tcg_temp_new_i64();
acc42968
TM
7959 tcg_gen_mov_i64(xth, cpu_vsrh(xA(ctx->opcode)));
7960 tcg_gen_shli_i64(xth, xth, 32);
7961 tcg_gen_mov_i64(t0, cpu_vsrl(xA(ctx->opcode)));
7962 tcg_gen_shri_i64(t0, t0, 32);
7963 tcg_gen_or_i64(xth, xth, t0);
7964 tcg_gen_mov_i64(xtl, cpu_vsrl(xA(ctx->opcode)));
7965 tcg_gen_shli_i64(xtl, xtl, 32);
7966 tcg_gen_mov_i64(t0, cpu_vsrh(xB(ctx->opcode)));
7967 tcg_gen_shri_i64(t0, t0, 32);
7968 tcg_gen_or_i64(xtl, xtl, t0);
f976b09e 7969 tcg_temp_free_i64(t0);
acc42968
TM
7970 break;
7971 }
7972 case 2: {
7973 tcg_gen_mov_i64(xth, cpu_vsrl(xA(ctx->opcode)));
7974 tcg_gen_mov_i64(xtl, cpu_vsrh(xB(ctx->opcode)));
7975 break;
7976 }
7977 case 3: {
f976b09e 7978 TCGv_i64 t0 = tcg_temp_new_i64();
acc42968
TM
7979 tcg_gen_mov_i64(xth, cpu_vsrl(xA(ctx->opcode)));
7980 tcg_gen_shli_i64(xth, xth, 32);
7981 tcg_gen_mov_i64(t0, cpu_vsrh(xB(ctx->opcode)));
7982 tcg_gen_shri_i64(t0, t0, 32);
7983 tcg_gen_or_i64(xth, xth, t0);
7984 tcg_gen_mov_i64(xtl, cpu_vsrh(xB(ctx->opcode)));
7985 tcg_gen_shli_i64(xtl, xtl, 32);
7986 tcg_gen_mov_i64(t0, cpu_vsrl(xB(ctx->opcode)));
7987 tcg_gen_shri_i64(t0, t0, 32);
7988 tcg_gen_or_i64(xtl, xtl, t0);
f976b09e 7989 tcg_temp_free_i64(t0);
acc42968
TM
7990 break;
7991 }
7992 }
7993
7994 tcg_gen_mov_i64(cpu_vsrh(xT(ctx->opcode)), xth);
7995 tcg_gen_mov_i64(cpu_vsrl(xT(ctx->opcode)), xtl);
7996
f976b09e
AG
7997 tcg_temp_free_i64(xth);
7998 tcg_temp_free_i64(xtl);
acc42968
TM
7999}
8000
ce577d2e 8001
0487d6a8 8002/*** SPE extension ***/
0487d6a8 8003/* Register moves */
3cd7d1dd 8004
a0e13900
FC
8005static inline void gen_evmra(DisasContext *ctx)
8006{
8007
8008 if (unlikely(!ctx->spe_enabled)) {
27a69bb0 8009 gen_exception(ctx, POWERPC_EXCP_SPEU);
a0e13900
FC
8010 return;
8011 }
8012
8013#if defined(TARGET_PPC64)
8014 /* rD := rA */
8015 tcg_gen_mov_i64(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)]);
8016
8017 /* spe_acc := rA */
8018 tcg_gen_st_i64(cpu_gpr[rA(ctx->opcode)],
8019 cpu_env,
1328c2bf 8020 offsetof(CPUPPCState, spe_acc));
a0e13900
FC
8021#else
8022 TCGv_i64 tmp = tcg_temp_new_i64();
8023
8024 /* tmp := rA_lo + rA_hi << 32 */
8025 tcg_gen_concat_i32_i64(tmp, cpu_gpr[rA(ctx->opcode)], cpu_gprh[rA(ctx->opcode)]);
8026
8027 /* spe_acc := tmp */
1328c2bf 8028 tcg_gen_st_i64(tmp, cpu_env, offsetof(CPUPPCState, spe_acc));
a0e13900
FC
8029 tcg_temp_free_i64(tmp);
8030
8031 /* rD := rA */
8032 tcg_gen_mov_i32(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)]);
8033 tcg_gen_mov_i32(cpu_gprh[rD(ctx->opcode)], cpu_gprh[rA(ctx->opcode)]);
8034#endif
8035}
8036
636aa200
BS
8037static inline void gen_load_gpr64(TCGv_i64 t, int reg)
8038{
f78fb44e
AJ
8039#if defined(TARGET_PPC64)
8040 tcg_gen_mov_i64(t, cpu_gpr[reg]);
8041#else
36aa55dc 8042 tcg_gen_concat_i32_i64(t, cpu_gpr[reg], cpu_gprh[reg]);
3cd7d1dd 8043#endif
f78fb44e 8044}
3cd7d1dd 8045
636aa200
BS
8046static inline void gen_store_gpr64(int reg, TCGv_i64 t)
8047{
f78fb44e
AJ
8048#if defined(TARGET_PPC64)
8049 tcg_gen_mov_i64(cpu_gpr[reg], t);
8050#else
a7812ae4 8051 TCGv_i64 tmp = tcg_temp_new_i64();
f78fb44e 8052 tcg_gen_trunc_i64_i32(cpu_gpr[reg], t);
f78fb44e
AJ
8053 tcg_gen_shri_i64(tmp, t, 32);
8054 tcg_gen_trunc_i64_i32(cpu_gprh[reg], tmp);
a7812ae4 8055 tcg_temp_free_i64(tmp);
3cd7d1dd 8056#endif
f78fb44e 8057}
3cd7d1dd 8058
70560da7 8059#define GEN_SPE(name0, name1, opc2, opc3, inval0, inval1, type) \
99e300ef 8060static void glue(gen_, name0##_##name1)(DisasContext *ctx) \
0487d6a8
JM
8061{ \
8062 if (Rc(ctx->opcode)) \
8063 gen_##name1(ctx); \
8064 else \
8065 gen_##name0(ctx); \
8066}
8067
8068/* Handler for undefined SPE opcodes */
636aa200 8069static inline void gen_speundef(DisasContext *ctx)
0487d6a8 8070{
e06fcd75 8071 gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL);
0487d6a8
JM
8072}
8073
57951c27
AJ
8074/* SPE logic */
8075#if defined(TARGET_PPC64)
8076#define GEN_SPEOP_LOGIC2(name, tcg_op) \
636aa200 8077static inline void gen_##name(DisasContext *ctx) \
0487d6a8
JM
8078{ \
8079 if (unlikely(!ctx->spe_enabled)) { \
27a69bb0 8080 gen_exception(ctx, POWERPC_EXCP_SPEU); \
0487d6a8
JM
8081 return; \
8082 } \
57951c27
AJ
8083 tcg_op(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)], \
8084 cpu_gpr[rB(ctx->opcode)]); \
8085}
8086#else
8087#define GEN_SPEOP_LOGIC2(name, tcg_op) \
636aa200 8088static inline void gen_##name(DisasContext *ctx) \
57951c27
AJ
8089{ \
8090 if (unlikely(!ctx->spe_enabled)) { \
27a69bb0 8091 gen_exception(ctx, POWERPC_EXCP_SPEU); \
57951c27
AJ
8092 return; \
8093 } \
8094 tcg_op(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)], \
8095 cpu_gpr[rB(ctx->opcode)]); \
8096 tcg_op(cpu_gprh[rD(ctx->opcode)], cpu_gprh[rA(ctx->opcode)], \
8097 cpu_gprh[rB(ctx->opcode)]); \
0487d6a8 8098}
57951c27
AJ
8099#endif
8100
8101GEN_SPEOP_LOGIC2(evand, tcg_gen_and_tl);
8102GEN_SPEOP_LOGIC2(evandc, tcg_gen_andc_tl);
8103GEN_SPEOP_LOGIC2(evxor, tcg_gen_xor_tl);
8104GEN_SPEOP_LOGIC2(evor, tcg_gen_or_tl);
8105GEN_SPEOP_LOGIC2(evnor, tcg_gen_nor_tl);
8106GEN_SPEOP_LOGIC2(eveqv, tcg_gen_eqv_tl);
8107GEN_SPEOP_LOGIC2(evorc, tcg_gen_orc_tl);
8108GEN_SPEOP_LOGIC2(evnand, tcg_gen_nand_tl);
0487d6a8 8109
57951c27
AJ
8110/* SPE logic immediate */
8111#if defined(TARGET_PPC64)
8112#define GEN_SPEOP_TCG_LOGIC_IMM2(name, tcg_opi) \
636aa200 8113static inline void gen_##name(DisasContext *ctx) \
3d3a6a0a
AJ
8114{ \
8115 if (unlikely(!ctx->spe_enabled)) { \
27a69bb0 8116 gen_exception(ctx, POWERPC_EXCP_SPEU); \
3d3a6a0a
AJ
8117 return; \
8118 } \
a7812ae4
PB
8119 TCGv_i32 t0 = tcg_temp_local_new_i32(); \
8120 TCGv_i32 t1 = tcg_temp_local_new_i32(); \
8121 TCGv_i64 t2 = tcg_temp_local_new_i64(); \
57951c27
AJ
8122 tcg_gen_trunc_i64_i32(t0, cpu_gpr[rA(ctx->opcode)]); \
8123 tcg_opi(t0, t0, rB(ctx->opcode)); \
8124 tcg_gen_shri_i64(t2, cpu_gpr[rA(ctx->opcode)], 32); \
8125 tcg_gen_trunc_i64_i32(t1, t2); \
a7812ae4 8126 tcg_temp_free_i64(t2); \
57951c27
AJ
8127 tcg_opi(t1, t1, rB(ctx->opcode)); \
8128 tcg_gen_concat_i32_i64(cpu_gpr[rD(ctx->opcode)], t0, t1); \
a7812ae4
PB
8129 tcg_temp_free_i32(t0); \
8130 tcg_temp_free_i32(t1); \
3d3a6a0a 8131}
57951c27
AJ
8132#else
8133#define GEN_SPEOP_TCG_LOGIC_IMM2(name, tcg_opi) \
636aa200 8134static inline void gen_##name(DisasContext *ctx) \
0487d6a8
JM
8135{ \
8136 if (unlikely(!ctx->spe_enabled)) { \
27a69bb0 8137 gen_exception(ctx, POWERPC_EXCP_SPEU); \
0487d6a8
JM
8138 return; \
8139 } \
57951c27
AJ
8140 tcg_opi(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)], \
8141 rB(ctx->opcode)); \
8142 tcg_opi(cpu_gprh[rD(ctx->opcode)], cpu_gprh[rA(ctx->opcode)], \
8143 rB(ctx->opcode)); \
0487d6a8 8144}
57951c27
AJ
8145#endif
8146GEN_SPEOP_TCG_LOGIC_IMM2(evslwi, tcg_gen_shli_i32);
8147GEN_SPEOP_TCG_LOGIC_IMM2(evsrwiu, tcg_gen_shri_i32);
8148GEN_SPEOP_TCG_LOGIC_IMM2(evsrwis, tcg_gen_sari_i32);
8149GEN_SPEOP_TCG_LOGIC_IMM2(evrlwi, tcg_gen_rotli_i32);
0487d6a8 8150
57951c27
AJ
8151/* SPE arithmetic */
8152#if defined(TARGET_PPC64)
8153#define GEN_SPEOP_ARITH1(name, tcg_op) \
636aa200 8154static inline void gen_##name(DisasContext *ctx) \
0487d6a8
JM
8155{ \
8156 if (unlikely(!ctx->spe_enabled)) { \
27a69bb0 8157 gen_exception(ctx, POWERPC_EXCP_SPEU); \
0487d6a8
JM
8158 return; \
8159 } \
a7812ae4
PB
8160 TCGv_i32 t0 = tcg_temp_local_new_i32(); \
8161 TCGv_i32 t1 = tcg_temp_local_new_i32(); \
8162 TCGv_i64 t2 = tcg_temp_local_new_i64(); \
57951c27
AJ
8163 tcg_gen_trunc_i64_i32(t0, cpu_gpr[rA(ctx->opcode)]); \
8164 tcg_op(t0, t0); \
8165 tcg_gen_shri_i64(t2, cpu_gpr[rA(ctx->opcode)], 32); \
8166 tcg_gen_trunc_i64_i32(t1, t2); \
a7812ae4 8167 tcg_temp_free_i64(t2); \
57951c27
AJ
8168 tcg_op(t1, t1); \
8169 tcg_gen_concat_i32_i64(cpu_gpr[rD(ctx->opcode)], t0, t1); \
a7812ae4
PB
8170 tcg_temp_free_i32(t0); \
8171 tcg_temp_free_i32(t1); \
0487d6a8 8172}
57951c27 8173#else
a7812ae4 8174#define GEN_SPEOP_ARITH1(name, tcg_op) \
636aa200 8175static inline void gen_##name(DisasContext *ctx) \
57951c27
AJ
8176{ \
8177 if (unlikely(!ctx->spe_enabled)) { \
27a69bb0 8178 gen_exception(ctx, POWERPC_EXCP_SPEU); \
57951c27
AJ
8179 return; \
8180 } \
8181 tcg_op(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)]); \
8182 tcg_op(cpu_gprh[rD(ctx->opcode)], cpu_gprh[rA(ctx->opcode)]); \
8183}
8184#endif
0487d6a8 8185
636aa200 8186static inline void gen_op_evabs(TCGv_i32 ret, TCGv_i32 arg1)
57951c27
AJ
8187{
8188 int l1 = gen_new_label();
8189 int l2 = gen_new_label();
0487d6a8 8190
57951c27
AJ
8191 tcg_gen_brcondi_i32(TCG_COND_GE, arg1, 0, l1);
8192 tcg_gen_neg_i32(ret, arg1);
8193 tcg_gen_br(l2);
8194 gen_set_label(l1);
a7812ae4 8195 tcg_gen_mov_i32(ret, arg1);
57951c27
AJ
8196 gen_set_label(l2);
8197}
8198GEN_SPEOP_ARITH1(evabs, gen_op_evabs);
8199GEN_SPEOP_ARITH1(evneg, tcg_gen_neg_i32);
8200GEN_SPEOP_ARITH1(evextsb, tcg_gen_ext8s_i32);
8201GEN_SPEOP_ARITH1(evextsh, tcg_gen_ext16s_i32);
636aa200 8202static inline void gen_op_evrndw(TCGv_i32 ret, TCGv_i32 arg1)
0487d6a8 8203{
57951c27
AJ
8204 tcg_gen_addi_i32(ret, arg1, 0x8000);
8205 tcg_gen_ext16u_i32(ret, ret);
8206}
8207GEN_SPEOP_ARITH1(evrndw, gen_op_evrndw);
a7812ae4
PB
8208GEN_SPEOP_ARITH1(evcntlsw, gen_helper_cntlsw32);
8209GEN_SPEOP_ARITH1(evcntlzw, gen_helper_cntlzw32);
0487d6a8 8210
57951c27
AJ
8211#if defined(TARGET_PPC64)
8212#define GEN_SPEOP_ARITH2(name, tcg_op) \
636aa200 8213static inline void gen_##name(DisasContext *ctx) \
0487d6a8
JM
8214{ \
8215 if (unlikely(!ctx->spe_enabled)) { \
27a69bb0 8216 gen_exception(ctx, POWERPC_EXCP_SPEU); \
0487d6a8
JM
8217 return; \
8218 } \
a7812ae4
PB
8219 TCGv_i32 t0 = tcg_temp_local_new_i32(); \
8220 TCGv_i32 t1 = tcg_temp_local_new_i32(); \
8221 TCGv_i32 t2 = tcg_temp_local_new_i32(); \
501e23c4 8222 TCGv_i64 t3 = tcg_temp_local_new_i64(); \
57951c27
AJ
8223 tcg_gen_trunc_i64_i32(t0, cpu_gpr[rA(ctx->opcode)]); \
8224 tcg_gen_trunc_i64_i32(t2, cpu_gpr[rB(ctx->opcode)]); \
8225 tcg_op(t0, t0, t2); \
8226 tcg_gen_shri_i64(t3, cpu_gpr[rA(ctx->opcode)], 32); \
8227 tcg_gen_trunc_i64_i32(t1, t3); \
8228 tcg_gen_shri_i64(t3, cpu_gpr[rB(ctx->opcode)], 32); \
8229 tcg_gen_trunc_i64_i32(t2, t3); \
a7812ae4 8230 tcg_temp_free_i64(t3); \
57951c27 8231 tcg_op(t1, t1, t2); \
a7812ae4 8232 tcg_temp_free_i32(t2); \
57951c27 8233 tcg_gen_concat_i32_i64(cpu_gpr[rD(ctx->opcode)], t0, t1); \
a7812ae4
PB
8234 tcg_temp_free_i32(t0); \
8235 tcg_temp_free_i32(t1); \
0487d6a8 8236}
57951c27
AJ
8237#else
8238#define GEN_SPEOP_ARITH2(name, tcg_op) \
636aa200 8239static inline void gen_##name(DisasContext *ctx) \
0487d6a8
JM
8240{ \
8241 if (unlikely(!ctx->spe_enabled)) { \
27a69bb0 8242 gen_exception(ctx, POWERPC_EXCP_SPEU); \
0487d6a8
JM
8243 return; \
8244 } \
57951c27
AJ
8245 tcg_op(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)], \
8246 cpu_gpr[rB(ctx->opcode)]); \
8247 tcg_op(cpu_gprh[rD(ctx->opcode)], cpu_gprh[rA(ctx->opcode)], \
8248 cpu_gprh[rB(ctx->opcode)]); \
0487d6a8 8249}
57951c27 8250#endif
0487d6a8 8251
636aa200 8252static inline void gen_op_evsrwu(TCGv_i32 ret, TCGv_i32 arg1, TCGv_i32 arg2)
57951c27 8253{
a7812ae4 8254 TCGv_i32 t0;
57951c27 8255 int l1, l2;
0487d6a8 8256
57951c27
AJ
8257 l1 = gen_new_label();
8258 l2 = gen_new_label();
a7812ae4 8259 t0 = tcg_temp_local_new_i32();
57951c27
AJ
8260 /* No error here: 6 bits are used */
8261 tcg_gen_andi_i32(t0, arg2, 0x3F);
8262 tcg_gen_brcondi_i32(TCG_COND_GE, t0, 32, l1);
8263 tcg_gen_shr_i32(ret, arg1, t0);
8264 tcg_gen_br(l2);
8265 gen_set_label(l1);
8266 tcg_gen_movi_i32(ret, 0);
0aef4261 8267 gen_set_label(l2);
a7812ae4 8268 tcg_temp_free_i32(t0);
57951c27
AJ
8269}
8270GEN_SPEOP_ARITH2(evsrwu, gen_op_evsrwu);
636aa200 8271static inline void gen_op_evsrws(TCGv_i32 ret, TCGv_i32 arg1, TCGv_i32 arg2)
57951c27 8272{
a7812ae4 8273 TCGv_i32 t0;
57951c27
AJ
8274 int l1, l2;
8275
8276 l1 = gen_new_label();
8277 l2 = gen_new_label();
a7812ae4 8278 t0 = tcg_temp_local_new_i32();
57951c27
AJ
8279 /* No error here: 6 bits are used */
8280 tcg_gen_andi_i32(t0, arg2, 0x3F);
8281 tcg_gen_brcondi_i32(TCG_COND_GE, t0, 32, l1);
8282 tcg_gen_sar_i32(ret, arg1, t0);
8283 tcg_gen_br(l2);
8284 gen_set_label(l1);
8285 tcg_gen_movi_i32(ret, 0);
0aef4261 8286 gen_set_label(l2);
a7812ae4 8287 tcg_temp_free_i32(t0);
57951c27
AJ
8288}
8289GEN_SPEOP_ARITH2(evsrws, gen_op_evsrws);
636aa200 8290static inline void gen_op_evslw(TCGv_i32 ret, TCGv_i32 arg1, TCGv_i32 arg2)
57951c27 8291{
a7812ae4 8292 TCGv_i32 t0;
57951c27
AJ
8293 int l1, l2;
8294
8295 l1 = gen_new_label();
8296 l2 = gen_new_label();
a7812ae4 8297 t0 = tcg_temp_local_new_i32();
57951c27
AJ
8298 /* No error here: 6 bits are used */
8299 tcg_gen_andi_i32(t0, arg2, 0x3F);
8300 tcg_gen_brcondi_i32(TCG_COND_GE, t0, 32, l1);
8301 tcg_gen_shl_i32(ret, arg1, t0);
8302 tcg_gen_br(l2);
8303 gen_set_label(l1);
8304 tcg_gen_movi_i32(ret, 0);
e29ef9fa 8305 gen_set_label(l2);
a7812ae4 8306 tcg_temp_free_i32(t0);
57951c27
AJ
8307}
8308GEN_SPEOP_ARITH2(evslw, gen_op_evslw);
636aa200 8309static inline void gen_op_evrlw(TCGv_i32 ret, TCGv_i32 arg1, TCGv_i32 arg2)
57951c27 8310{
a7812ae4 8311 TCGv_i32 t0 = tcg_temp_new_i32();
57951c27
AJ
8312 tcg_gen_andi_i32(t0, arg2, 0x1F);
8313 tcg_gen_rotl_i32(ret, arg1, t0);
a7812ae4 8314 tcg_temp_free_i32(t0);
57951c27
AJ
8315}
8316GEN_SPEOP_ARITH2(evrlw, gen_op_evrlw);
636aa200 8317static inline void gen_evmergehi(DisasContext *ctx)
57951c27
AJ
8318{
8319 if (unlikely(!ctx->spe_enabled)) {
27a69bb0 8320 gen_exception(ctx, POWERPC_EXCP_SPEU);
57951c27
AJ
8321 return;
8322 }
8323#if defined(TARGET_PPC64)
a7812ae4
PB
8324 TCGv t0 = tcg_temp_new();
8325 TCGv t1 = tcg_temp_new();
57951c27
AJ
8326 tcg_gen_shri_tl(t0, cpu_gpr[rB(ctx->opcode)], 32);
8327 tcg_gen_andi_tl(t1, cpu_gpr[rA(ctx->opcode)], 0xFFFFFFFF0000000ULL);
8328 tcg_gen_or_tl(cpu_gpr[rD(ctx->opcode)], t0, t1);
8329 tcg_temp_free(t0);
8330 tcg_temp_free(t1);
8331#else
8332 tcg_gen_mov_i32(cpu_gpr[rD(ctx->opcode)], cpu_gprh[rB(ctx->opcode)]);
8333 tcg_gen_mov_i32(cpu_gprh[rD(ctx->opcode)], cpu_gprh[rA(ctx->opcode)]);
8334#endif
8335}
8336GEN_SPEOP_ARITH2(evaddw, tcg_gen_add_i32);
636aa200 8337static inline void gen_op_evsubf(TCGv_i32 ret, TCGv_i32 arg1, TCGv_i32 arg2)
0487d6a8 8338{
57951c27
AJ
8339 tcg_gen_sub_i32(ret, arg2, arg1);
8340}
8341GEN_SPEOP_ARITH2(evsubfw, gen_op_evsubf);
0487d6a8 8342
57951c27
AJ
8343/* SPE arithmetic immediate */
8344#if defined(TARGET_PPC64)
8345#define GEN_SPEOP_ARITH_IMM2(name, tcg_op) \
636aa200 8346static inline void gen_##name(DisasContext *ctx) \
57951c27
AJ
8347{ \
8348 if (unlikely(!ctx->spe_enabled)) { \
27a69bb0 8349 gen_exception(ctx, POWERPC_EXCP_SPEU); \
57951c27
AJ
8350 return; \
8351 } \
a7812ae4
PB
8352 TCGv_i32 t0 = tcg_temp_local_new_i32(); \
8353 TCGv_i32 t1 = tcg_temp_local_new_i32(); \
8354 TCGv_i64 t2 = tcg_temp_local_new_i64(); \
57951c27
AJ
8355 tcg_gen_trunc_i64_i32(t0, cpu_gpr[rB(ctx->opcode)]); \
8356 tcg_op(t0, t0, rA(ctx->opcode)); \
8357 tcg_gen_shri_i64(t2, cpu_gpr[rB(ctx->opcode)], 32); \
8358 tcg_gen_trunc_i64_i32(t1, t2); \
e06fcd75 8359 tcg_temp_free_i64(t2); \
57951c27
AJ
8360 tcg_op(t1, t1, rA(ctx->opcode)); \
8361 tcg_gen_concat_i32_i64(cpu_gpr[rD(ctx->opcode)], t0, t1); \
a7812ae4
PB
8362 tcg_temp_free_i32(t0); \
8363 tcg_temp_free_i32(t1); \
57951c27
AJ
8364}
8365#else
8366#define GEN_SPEOP_ARITH_IMM2(name, tcg_op) \
636aa200 8367static inline void gen_##name(DisasContext *ctx) \
57951c27
AJ
8368{ \
8369 if (unlikely(!ctx->spe_enabled)) { \
27a69bb0 8370 gen_exception(ctx, POWERPC_EXCP_SPEU); \
57951c27
AJ
8371 return; \
8372 } \
8373 tcg_op(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rB(ctx->opcode)], \
8374 rA(ctx->opcode)); \
8375 tcg_op(cpu_gprh[rD(ctx->opcode)], cpu_gprh[rB(ctx->opcode)], \
8376 rA(ctx->opcode)); \
8377}
8378#endif
8379GEN_SPEOP_ARITH_IMM2(evaddiw, tcg_gen_addi_i32);
8380GEN_SPEOP_ARITH_IMM2(evsubifw, tcg_gen_subi_i32);
8381
8382/* SPE comparison */
8383#if defined(TARGET_PPC64)
8384#define GEN_SPEOP_COMP(name, tcg_cond) \
636aa200 8385static inline void gen_##name(DisasContext *ctx) \
57951c27
AJ
8386{ \
8387 if (unlikely(!ctx->spe_enabled)) { \
27a69bb0 8388 gen_exception(ctx, POWERPC_EXCP_SPEU); \
57951c27
AJ
8389 return; \
8390 } \
8391 int l1 = gen_new_label(); \
8392 int l2 = gen_new_label(); \
8393 int l3 = gen_new_label(); \
8394 int l4 = gen_new_label(); \
a7812ae4
PB
8395 TCGv_i32 t0 = tcg_temp_local_new_i32(); \
8396 TCGv_i32 t1 = tcg_temp_local_new_i32(); \
8397 TCGv_i64 t2 = tcg_temp_local_new_i64(); \
57951c27
AJ
8398 tcg_gen_trunc_i64_i32(t0, cpu_gpr[rA(ctx->opcode)]); \
8399 tcg_gen_trunc_i64_i32(t1, cpu_gpr[rB(ctx->opcode)]); \
8400 tcg_gen_brcond_i32(tcg_cond, t0, t1, l1); \
a7812ae4 8401 tcg_gen_movi_i32(cpu_crf[crfD(ctx->opcode)], 0); \
57951c27
AJ
8402 tcg_gen_br(l2); \
8403 gen_set_label(l1); \
8404 tcg_gen_movi_i32(cpu_crf[crfD(ctx->opcode)], \
8405 CRF_CL | CRF_CH_OR_CL | CRF_CH_AND_CL); \
8406 gen_set_label(l2); \
8407 tcg_gen_shri_i64(t2, cpu_gpr[rA(ctx->opcode)], 32); \
8408 tcg_gen_trunc_i64_i32(t0, t2); \
8409 tcg_gen_shri_i64(t2, cpu_gpr[rB(ctx->opcode)], 32); \
8410 tcg_gen_trunc_i64_i32(t1, t2); \
a7812ae4 8411 tcg_temp_free_i64(t2); \
57951c27
AJ
8412 tcg_gen_brcond_i32(tcg_cond, t0, t1, l3); \
8413 tcg_gen_andi_i32(cpu_crf[crfD(ctx->opcode)], cpu_crf[crfD(ctx->opcode)], \
8414 ~(CRF_CH | CRF_CH_AND_CL)); \
8415 tcg_gen_br(l4); \
8416 gen_set_label(l3); \
8417 tcg_gen_ori_i32(cpu_crf[crfD(ctx->opcode)], cpu_crf[crfD(ctx->opcode)], \
8418 CRF_CH | CRF_CH_OR_CL); \
8419 gen_set_label(l4); \
a7812ae4
PB
8420 tcg_temp_free_i32(t0); \
8421 tcg_temp_free_i32(t1); \
57951c27
AJ
8422}
8423#else
8424#define GEN_SPEOP_COMP(name, tcg_cond) \
636aa200 8425static inline void gen_##name(DisasContext *ctx) \
57951c27
AJ
8426{ \
8427 if (unlikely(!ctx->spe_enabled)) { \
27a69bb0 8428 gen_exception(ctx, POWERPC_EXCP_SPEU); \
57951c27
AJ
8429 return; \
8430 } \
8431 int l1 = gen_new_label(); \
8432 int l2 = gen_new_label(); \
8433 int l3 = gen_new_label(); \
8434 int l4 = gen_new_label(); \
8435 \
8436 tcg_gen_brcond_i32(tcg_cond, cpu_gpr[rA(ctx->opcode)], \
8437 cpu_gpr[rB(ctx->opcode)], l1); \
8438 tcg_gen_movi_tl(cpu_crf[crfD(ctx->opcode)], 0); \
8439 tcg_gen_br(l2); \
8440 gen_set_label(l1); \
8441 tcg_gen_movi_i32(cpu_crf[crfD(ctx->opcode)], \
8442 CRF_CL | CRF_CH_OR_CL | CRF_CH_AND_CL); \
8443 gen_set_label(l2); \
8444 tcg_gen_brcond_i32(tcg_cond, cpu_gprh[rA(ctx->opcode)], \
8445 cpu_gprh[rB(ctx->opcode)], l3); \
8446 tcg_gen_andi_i32(cpu_crf[crfD(ctx->opcode)], cpu_crf[crfD(ctx->opcode)], \
8447 ~(CRF_CH | CRF_CH_AND_CL)); \
8448 tcg_gen_br(l4); \
8449 gen_set_label(l3); \
8450 tcg_gen_ori_i32(cpu_crf[crfD(ctx->opcode)], cpu_crf[crfD(ctx->opcode)], \
8451 CRF_CH | CRF_CH_OR_CL); \
8452 gen_set_label(l4); \
8453}
8454#endif
8455GEN_SPEOP_COMP(evcmpgtu, TCG_COND_GTU);
8456GEN_SPEOP_COMP(evcmpgts, TCG_COND_GT);
8457GEN_SPEOP_COMP(evcmpltu, TCG_COND_LTU);
8458GEN_SPEOP_COMP(evcmplts, TCG_COND_LT);
8459GEN_SPEOP_COMP(evcmpeq, TCG_COND_EQ);
8460
8461/* SPE misc */
636aa200 8462static inline void gen_brinc(DisasContext *ctx)
57951c27
AJ
8463{
8464 /* Note: brinc is usable even if SPE is disabled */
a7812ae4
PB
8465 gen_helper_brinc(cpu_gpr[rD(ctx->opcode)],
8466 cpu_gpr[rA(ctx->opcode)], cpu_gpr[rB(ctx->opcode)]);
0487d6a8 8467}
636aa200 8468static inline void gen_evmergelo(DisasContext *ctx)
57951c27
AJ
8469{
8470 if (unlikely(!ctx->spe_enabled)) {
27a69bb0 8471 gen_exception(ctx, POWERPC_EXCP_SPEU);
57951c27
AJ
8472 return;
8473 }
8474#if defined(TARGET_PPC64)
a7812ae4
PB
8475 TCGv t0 = tcg_temp_new();
8476 TCGv t1 = tcg_temp_new();
17d9b3af 8477 tcg_gen_ext32u_tl(t0, cpu_gpr[rB(ctx->opcode)]);
57951c27
AJ
8478 tcg_gen_shli_tl(t1, cpu_gpr[rA(ctx->opcode)], 32);
8479 tcg_gen_or_tl(cpu_gpr[rD(ctx->opcode)], t0, t1);
8480 tcg_temp_free(t0);
8481 tcg_temp_free(t1);
8482#else
57951c27 8483 tcg_gen_mov_i32(cpu_gprh[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)]);
33890b3e 8484 tcg_gen_mov_i32(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rB(ctx->opcode)]);
57951c27
AJ
8485#endif
8486}
636aa200 8487static inline void gen_evmergehilo(DisasContext *ctx)
57951c27
AJ
8488{
8489 if (unlikely(!ctx->spe_enabled)) {
27a69bb0 8490 gen_exception(ctx, POWERPC_EXCP_SPEU);
57951c27
AJ
8491 return;
8492 }
8493#if defined(TARGET_PPC64)
a7812ae4
PB
8494 TCGv t0 = tcg_temp_new();
8495 TCGv t1 = tcg_temp_new();
17d9b3af 8496 tcg_gen_ext32u_tl(t0, cpu_gpr[rB(ctx->opcode)]);
57951c27
AJ
8497 tcg_gen_andi_tl(t1, cpu_gpr[rA(ctx->opcode)], 0xFFFFFFFF0000000ULL);
8498 tcg_gen_or_tl(cpu_gpr[rD(ctx->opcode)], t0, t1);
8499 tcg_temp_free(t0);
8500 tcg_temp_free(t1);
8501#else
8502 tcg_gen_mov_i32(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rB(ctx->opcode)]);
8503 tcg_gen_mov_i32(cpu_gprh[rD(ctx->opcode)], cpu_gprh[rA(ctx->opcode)]);
8504#endif
8505}
636aa200 8506static inline void gen_evmergelohi(DisasContext *ctx)
57951c27
AJ
8507{
8508 if (unlikely(!ctx->spe_enabled)) {
27a69bb0 8509 gen_exception(ctx, POWERPC_EXCP_SPEU);
57951c27
AJ
8510 return;
8511 }
8512#if defined(TARGET_PPC64)
a7812ae4
PB
8513 TCGv t0 = tcg_temp_new();
8514 TCGv t1 = tcg_temp_new();
57951c27
AJ
8515 tcg_gen_shri_tl(t0, cpu_gpr[rB(ctx->opcode)], 32);
8516 tcg_gen_shli_tl(t1, cpu_gpr[rA(ctx->opcode)], 32);
8517 tcg_gen_or_tl(cpu_gpr[rD(ctx->opcode)], t0, t1);
8518 tcg_temp_free(t0);
8519 tcg_temp_free(t1);
8520#else
33890b3e
NF
8521 if (rD(ctx->opcode) == rA(ctx->opcode)) {
8522 TCGv_i32 tmp = tcg_temp_new_i32();
8523 tcg_gen_mov_i32(tmp, cpu_gpr[rA(ctx->opcode)]);
8524 tcg_gen_mov_i32(cpu_gpr[rD(ctx->opcode)], cpu_gprh[rB(ctx->opcode)]);
8525 tcg_gen_mov_i32(cpu_gprh[rD(ctx->opcode)], tmp);
8526 tcg_temp_free_i32(tmp);
8527 } else {
8528 tcg_gen_mov_i32(cpu_gpr[rD(ctx->opcode)], cpu_gprh[rB(ctx->opcode)]);
8529 tcg_gen_mov_i32(cpu_gprh[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)]);
8530 }
57951c27
AJ
8531#endif
8532}
636aa200 8533static inline void gen_evsplati(DisasContext *ctx)
57951c27 8534{
ae01847f 8535 uint64_t imm = ((int32_t)(rA(ctx->opcode) << 27)) >> 27;
0487d6a8 8536
57951c27 8537#if defined(TARGET_PPC64)
38d14952 8538 tcg_gen_movi_tl(cpu_gpr[rD(ctx->opcode)], (imm << 32) | imm);
57951c27
AJ
8539#else
8540 tcg_gen_movi_i32(cpu_gpr[rD(ctx->opcode)], imm);
8541 tcg_gen_movi_i32(cpu_gprh[rD(ctx->opcode)], imm);
8542#endif
8543}
636aa200 8544static inline void gen_evsplatfi(DisasContext *ctx)
0487d6a8 8545{
ae01847f 8546 uint64_t imm = rA(ctx->opcode) << 27;
0487d6a8 8547
57951c27 8548#if defined(TARGET_PPC64)
38d14952 8549 tcg_gen_movi_tl(cpu_gpr[rD(ctx->opcode)], (imm << 32) | imm);
57951c27
AJ
8550#else
8551 tcg_gen_movi_i32(cpu_gpr[rD(ctx->opcode)], imm);
8552 tcg_gen_movi_i32(cpu_gprh[rD(ctx->opcode)], imm);
8553#endif
0487d6a8
JM
8554}
8555
636aa200 8556static inline void gen_evsel(DisasContext *ctx)
57951c27
AJ
8557{
8558 int l1 = gen_new_label();
8559 int l2 = gen_new_label();
8560 int l3 = gen_new_label();
8561 int l4 = gen_new_label();
a7812ae4 8562 TCGv_i32 t0 = tcg_temp_local_new_i32();
57951c27 8563#if defined(TARGET_PPC64)
a7812ae4
PB
8564 TCGv t1 = tcg_temp_local_new();
8565 TCGv t2 = tcg_temp_local_new();
57951c27
AJ
8566#endif
8567 tcg_gen_andi_i32(t0, cpu_crf[ctx->opcode & 0x07], 1 << 3);
8568 tcg_gen_brcondi_i32(TCG_COND_EQ, t0, 0, l1);
8569#if defined(TARGET_PPC64)
8570 tcg_gen_andi_tl(t1, cpu_gpr[rA(ctx->opcode)], 0xFFFFFFFF00000000ULL);
8571#else
8572 tcg_gen_mov_tl(cpu_gprh[rD(ctx->opcode)], cpu_gprh[rA(ctx->opcode)]);
8573#endif
8574 tcg_gen_br(l2);
8575 gen_set_label(l1);
8576#if defined(TARGET_PPC64)
8577 tcg_gen_andi_tl(t1, cpu_gpr[rB(ctx->opcode)], 0xFFFFFFFF00000000ULL);
8578#else
8579 tcg_gen_mov_tl(cpu_gprh[rD(ctx->opcode)], cpu_gprh[rB(ctx->opcode)]);
8580#endif
8581 gen_set_label(l2);
8582 tcg_gen_andi_i32(t0, cpu_crf[ctx->opcode & 0x07], 1 << 2);
8583 tcg_gen_brcondi_i32(TCG_COND_EQ, t0, 0, l3);
8584#if defined(TARGET_PPC64)
17d9b3af 8585 tcg_gen_ext32u_tl(t2, cpu_gpr[rA(ctx->opcode)]);
57951c27
AJ
8586#else
8587 tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)]);
8588#endif
8589 tcg_gen_br(l4);
8590 gen_set_label(l3);
8591#if defined(TARGET_PPC64)
17d9b3af 8592 tcg_gen_ext32u_tl(t2, cpu_gpr[rB(ctx->opcode)]);
57951c27
AJ
8593#else
8594 tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rB(ctx->opcode)]);
8595#endif
8596 gen_set_label(l4);
a7812ae4 8597 tcg_temp_free_i32(t0);
57951c27
AJ
8598#if defined(TARGET_PPC64)
8599 tcg_gen_or_tl(cpu_gpr[rD(ctx->opcode)], t1, t2);
8600 tcg_temp_free(t1);
8601 tcg_temp_free(t2);
8602#endif
8603}
e8eaa2c0
BS
8604
8605static void gen_evsel0(DisasContext *ctx)
57951c27
AJ
8606{
8607 gen_evsel(ctx);
8608}
e8eaa2c0
BS
8609
8610static void gen_evsel1(DisasContext *ctx)
57951c27
AJ
8611{
8612 gen_evsel(ctx);
8613}
e8eaa2c0
BS
8614
8615static void gen_evsel2(DisasContext *ctx)
57951c27
AJ
8616{
8617 gen_evsel(ctx);
8618}
e8eaa2c0
BS
8619
8620static void gen_evsel3(DisasContext *ctx)
57951c27
AJ
8621{
8622 gen_evsel(ctx);
8623}
0487d6a8 8624
a0e13900
FC
8625/* Multiply */
8626
8627static inline void gen_evmwumi(DisasContext *ctx)
8628{
8629 TCGv_i64 t0, t1;
8630
8631 if (unlikely(!ctx->spe_enabled)) {
27a69bb0 8632 gen_exception(ctx, POWERPC_EXCP_SPEU);
a0e13900
FC
8633 return;
8634 }
8635
8636 t0 = tcg_temp_new_i64();
8637 t1 = tcg_temp_new_i64();
8638
8639 /* t0 := rA; t1 := rB */
8640#if defined(TARGET_PPC64)
8641 tcg_gen_ext32u_tl(t0, cpu_gpr[rA(ctx->opcode)]);
8642 tcg_gen_ext32u_tl(t1, cpu_gpr[rB(ctx->opcode)]);
8643#else
8644 tcg_gen_extu_tl_i64(t0, cpu_gpr[rA(ctx->opcode)]);
8645 tcg_gen_extu_tl_i64(t1, cpu_gpr[rB(ctx->opcode)]);
8646#endif
8647
8648 tcg_gen_mul_i64(t0, t0, t1); /* t0 := rA * rB */
8649
8650 gen_store_gpr64(rD(ctx->opcode), t0); /* rD := t0 */
8651
8652 tcg_temp_free_i64(t0);
8653 tcg_temp_free_i64(t1);
8654}
8655
8656static inline void gen_evmwumia(DisasContext *ctx)
8657{
8658 TCGv_i64 tmp;
8659
8660 if (unlikely(!ctx->spe_enabled)) {
27a69bb0 8661 gen_exception(ctx, POWERPC_EXCP_SPEU);
a0e13900
FC
8662 return;
8663 }
8664
8665 gen_evmwumi(ctx); /* rD := rA * rB */
8666
8667 tmp = tcg_temp_new_i64();
8668
8669 /* acc := rD */
8670 gen_load_gpr64(tmp, rD(ctx->opcode));
1328c2bf 8671 tcg_gen_st_i64(tmp, cpu_env, offsetof(CPUPPCState, spe_acc));
a0e13900
FC
8672 tcg_temp_free_i64(tmp);
8673}
8674
8675static inline void gen_evmwumiaa(DisasContext *ctx)
8676{
8677 TCGv_i64 acc;
8678 TCGv_i64 tmp;
8679
8680 if (unlikely(!ctx->spe_enabled)) {
27a69bb0 8681 gen_exception(ctx, POWERPC_EXCP_SPEU);
a0e13900
FC
8682 return;
8683 }
8684
8685 gen_evmwumi(ctx); /* rD := rA * rB */
8686
8687 acc = tcg_temp_new_i64();
8688 tmp = tcg_temp_new_i64();
8689
8690 /* tmp := rD */
8691 gen_load_gpr64(tmp, rD(ctx->opcode));
8692
8693 /* Load acc */
1328c2bf 8694 tcg_gen_ld_i64(acc, cpu_env, offsetof(CPUPPCState, spe_acc));
a0e13900
FC
8695
8696 /* acc := tmp + acc */
8697 tcg_gen_add_i64(acc, acc, tmp);
8698
8699 /* Store acc */
1328c2bf 8700 tcg_gen_st_i64(acc, cpu_env, offsetof(CPUPPCState, spe_acc));
a0e13900
FC
8701
8702 /* rD := acc */
8703 gen_store_gpr64(rD(ctx->opcode), acc);
8704
8705 tcg_temp_free_i64(acc);
8706 tcg_temp_free_i64(tmp);
8707}
8708
8709static inline void gen_evmwsmi(DisasContext *ctx)
8710{
8711 TCGv_i64 t0, t1;
8712
8713 if (unlikely(!ctx->spe_enabled)) {
27a69bb0 8714 gen_exception(ctx, POWERPC_EXCP_SPEU);
a0e13900
FC
8715 return;
8716 }
8717
8718 t0 = tcg_temp_new_i64();
8719 t1 = tcg_temp_new_i64();
8720
8721 /* t0 := rA; t1 := rB */
8722#if defined(TARGET_PPC64)
8723 tcg_gen_ext32s_tl(t0, cpu_gpr[rA(ctx->opcode)]);
8724 tcg_gen_ext32s_tl(t1, cpu_gpr[rB(ctx->opcode)]);
8725#else
8726 tcg_gen_ext_tl_i64(t0, cpu_gpr[rA(ctx->opcode)]);
8727 tcg_gen_ext_tl_i64(t1, cpu_gpr[rB(ctx->opcode)]);
8728#endif
8729
8730 tcg_gen_mul_i64(t0, t0, t1); /* t0 := rA * rB */
8731
8732 gen_store_gpr64(rD(ctx->opcode), t0); /* rD := t0 */
8733
8734 tcg_temp_free_i64(t0);
8735 tcg_temp_free_i64(t1);
8736}
8737
8738static inline void gen_evmwsmia(DisasContext *ctx)
8739{
8740 TCGv_i64 tmp;
8741
8742 gen_evmwsmi(ctx); /* rD := rA * rB */
8743
8744 tmp = tcg_temp_new_i64();
8745
8746 /* acc := rD */
8747 gen_load_gpr64(tmp, rD(ctx->opcode));
1328c2bf 8748 tcg_gen_st_i64(tmp, cpu_env, offsetof(CPUPPCState, spe_acc));
a0e13900
FC
8749
8750 tcg_temp_free_i64(tmp);
8751}
8752
8753static inline void gen_evmwsmiaa(DisasContext *ctx)
8754{
8755 TCGv_i64 acc = tcg_temp_new_i64();
8756 TCGv_i64 tmp = tcg_temp_new_i64();
8757
8758 gen_evmwsmi(ctx); /* rD := rA * rB */
8759
8760 acc = tcg_temp_new_i64();
8761 tmp = tcg_temp_new_i64();
8762
8763 /* tmp := rD */
8764 gen_load_gpr64(tmp, rD(ctx->opcode));
8765
8766 /* Load acc */
1328c2bf 8767 tcg_gen_ld_i64(acc, cpu_env, offsetof(CPUPPCState, spe_acc));
a0e13900
FC
8768
8769 /* acc := tmp + acc */
8770 tcg_gen_add_i64(acc, acc, tmp);
8771
8772 /* Store acc */
1328c2bf 8773 tcg_gen_st_i64(acc, cpu_env, offsetof(CPUPPCState, spe_acc));
a0e13900
FC
8774
8775 /* rD := acc */
8776 gen_store_gpr64(rD(ctx->opcode), acc);
8777
8778 tcg_temp_free_i64(acc);
8779 tcg_temp_free_i64(tmp);
8780}
8781
70560da7
FC
8782GEN_SPE(evaddw, speundef, 0x00, 0x08, 0x00000000, 0xFFFFFFFF, PPC_SPE); ////
8783GEN_SPE(evaddiw, speundef, 0x01, 0x08, 0x00000000, 0xFFFFFFFF, PPC_SPE);
8784GEN_SPE(evsubfw, speundef, 0x02, 0x08, 0x00000000, 0xFFFFFFFF, PPC_SPE); ////
8785GEN_SPE(evsubifw, speundef, 0x03, 0x08, 0x00000000, 0xFFFFFFFF, PPC_SPE);
8786GEN_SPE(evabs, evneg, 0x04, 0x08, 0x0000F800, 0x0000F800, PPC_SPE); ////
8787GEN_SPE(evextsb, evextsh, 0x05, 0x08, 0x0000F800, 0x0000F800, PPC_SPE); ////
8788GEN_SPE(evrndw, evcntlzw, 0x06, 0x08, 0x0000F800, 0x0000F800, PPC_SPE); ////
8789GEN_SPE(evcntlsw, brinc, 0x07, 0x08, 0x0000F800, 0x00000000, PPC_SPE); //
8790GEN_SPE(evmra, speundef, 0x02, 0x13, 0x0000F800, 0xFFFFFFFF, PPC_SPE);
8791GEN_SPE(speundef, evand, 0x08, 0x08, 0xFFFFFFFF, 0x00000000, PPC_SPE); ////
8792GEN_SPE(evandc, speundef, 0x09, 0x08, 0x00000000, 0xFFFFFFFF, PPC_SPE); ////
8793GEN_SPE(evxor, evor, 0x0B, 0x08, 0x00000000, 0x00000000, PPC_SPE); ////
8794GEN_SPE(evnor, eveqv, 0x0C, 0x08, 0x00000000, 0x00000000, PPC_SPE); ////
8795GEN_SPE(evmwumi, evmwsmi, 0x0C, 0x11, 0x00000000, 0x00000000, PPC_SPE);
8796GEN_SPE(evmwumia, evmwsmia, 0x1C, 0x11, 0x00000000, 0x00000000, PPC_SPE);
8797GEN_SPE(evmwumiaa, evmwsmiaa, 0x0C, 0x15, 0x00000000, 0x00000000, PPC_SPE);
8798GEN_SPE(speundef, evorc, 0x0D, 0x08, 0xFFFFFFFF, 0x00000000, PPC_SPE); ////
8799GEN_SPE(evnand, speundef, 0x0F, 0x08, 0x00000000, 0xFFFFFFFF, PPC_SPE); ////
8800GEN_SPE(evsrwu, evsrws, 0x10, 0x08, 0x00000000, 0x00000000, PPC_SPE); ////
8801GEN_SPE(evsrwiu, evsrwis, 0x11, 0x08, 0x00000000, 0x00000000, PPC_SPE);
8802GEN_SPE(evslw, speundef, 0x12, 0x08, 0x00000000, 0xFFFFFFFF, PPC_SPE); ////
8803GEN_SPE(evslwi, speundef, 0x13, 0x08, 0x00000000, 0xFFFFFFFF, PPC_SPE);
8804GEN_SPE(evrlw, evsplati, 0x14, 0x08, 0x00000000, 0x0000F800, PPC_SPE); //
8805GEN_SPE(evrlwi, evsplatfi, 0x15, 0x08, 0x00000000, 0x0000F800, PPC_SPE);
8806GEN_SPE(evmergehi, evmergelo, 0x16, 0x08, 0x00000000, 0x00000000, PPC_SPE); ////
8807GEN_SPE(evmergehilo, evmergelohi, 0x17, 0x08, 0x00000000, 0x00000000, PPC_SPE); ////
8808GEN_SPE(evcmpgtu, evcmpgts, 0x18, 0x08, 0x00600000, 0x00600000, PPC_SPE); ////
8809GEN_SPE(evcmpltu, evcmplts, 0x19, 0x08, 0x00600000, 0x00600000, PPC_SPE); ////
8810GEN_SPE(evcmpeq, speundef, 0x1A, 0x08, 0x00600000, 0xFFFFFFFF, PPC_SPE); ////
0487d6a8 8811
6a6ae23f 8812/* SPE load and stores */
636aa200 8813static inline void gen_addr_spe_imm_index(DisasContext *ctx, TCGv EA, int sh)
6a6ae23f
AJ
8814{
8815 target_ulong uimm = rB(ctx->opcode);
8816
76db3ba4 8817 if (rA(ctx->opcode) == 0) {
6a6ae23f 8818 tcg_gen_movi_tl(EA, uimm << sh);
76db3ba4 8819 } else {
6a6ae23f 8820 tcg_gen_addi_tl(EA, cpu_gpr[rA(ctx->opcode)], uimm << sh);
c791fe84 8821 if (NARROW_MODE(ctx)) {
76db3ba4
AJ
8822 tcg_gen_ext32u_tl(EA, EA);
8823 }
76db3ba4 8824 }
0487d6a8 8825}
6a6ae23f 8826
636aa200 8827static inline void gen_op_evldd(DisasContext *ctx, TCGv addr)
6a6ae23f
AJ
8828{
8829#if defined(TARGET_PPC64)
76db3ba4 8830 gen_qemu_ld64(ctx, cpu_gpr[rD(ctx->opcode)], addr);
6a6ae23f
AJ
8831#else
8832 TCGv_i64 t0 = tcg_temp_new_i64();
76db3ba4 8833 gen_qemu_ld64(ctx, t0, addr);
6a6ae23f
AJ
8834 tcg_gen_trunc_i64_i32(cpu_gpr[rD(ctx->opcode)], t0);
8835 tcg_gen_shri_i64(t0, t0, 32);
8836 tcg_gen_trunc_i64_i32(cpu_gprh[rD(ctx->opcode)], t0);
8837 tcg_temp_free_i64(t0);
8838#endif
0487d6a8 8839}
6a6ae23f 8840
636aa200 8841static inline void gen_op_evldw(DisasContext *ctx, TCGv addr)
6a6ae23f 8842{
0487d6a8 8843#if defined(TARGET_PPC64)
6a6ae23f 8844 TCGv t0 = tcg_temp_new();
76db3ba4 8845 gen_qemu_ld32u(ctx, t0, addr);
6a6ae23f 8846 tcg_gen_shli_tl(cpu_gpr[rD(ctx->opcode)], t0, 32);
76db3ba4
AJ
8847 gen_addr_add(ctx, addr, addr, 4);
8848 gen_qemu_ld32u(ctx, t0, addr);
6a6ae23f
AJ
8849 tcg_gen_or_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rD(ctx->opcode)], t0);
8850 tcg_temp_free(t0);
8851#else
76db3ba4
AJ
8852 gen_qemu_ld32u(ctx, cpu_gprh[rD(ctx->opcode)], addr);
8853 gen_addr_add(ctx, addr, addr, 4);
8854 gen_qemu_ld32u(ctx, cpu_gpr[rD(ctx->opcode)], addr);
6a6ae23f 8855#endif
0487d6a8 8856}
6a6ae23f 8857
636aa200 8858static inline void gen_op_evldh(DisasContext *ctx, TCGv addr)
6a6ae23f
AJ
8859{
8860 TCGv t0 = tcg_temp_new();
8861#if defined(TARGET_PPC64)
76db3ba4 8862 gen_qemu_ld16u(ctx, t0, addr);
6a6ae23f 8863 tcg_gen_shli_tl(cpu_gpr[rD(ctx->opcode)], t0, 48);
76db3ba4
AJ
8864 gen_addr_add(ctx, addr, addr, 2);
8865 gen_qemu_ld16u(ctx, t0, addr);
6a6ae23f
AJ
8866 tcg_gen_shli_tl(t0, t0, 32);
8867 tcg_gen_or_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rD(ctx->opcode)], t0);
76db3ba4
AJ
8868 gen_addr_add(ctx, addr, addr, 2);
8869 gen_qemu_ld16u(ctx, t0, addr);
6a6ae23f
AJ
8870 tcg_gen_shli_tl(t0, t0, 16);
8871 tcg_gen_or_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rD(ctx->opcode)], t0);
76db3ba4
AJ
8872 gen_addr_add(ctx, addr, addr, 2);
8873 gen_qemu_ld16u(ctx, t0, addr);
6a6ae23f 8874 tcg_gen_or_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rD(ctx->opcode)], t0);
0487d6a8 8875#else
76db3ba4 8876 gen_qemu_ld16u(ctx, t0, addr);
6a6ae23f 8877 tcg_gen_shli_tl(cpu_gprh[rD(ctx->opcode)], t0, 16);
76db3ba4
AJ
8878 gen_addr_add(ctx, addr, addr, 2);
8879 gen_qemu_ld16u(ctx, t0, addr);
6a6ae23f 8880 tcg_gen_or_tl(cpu_gprh[rD(ctx->opcode)], cpu_gprh[rD(ctx->opcode)], t0);
76db3ba4
AJ
8881 gen_addr_add(ctx, addr, addr, 2);
8882 gen_qemu_ld16u(ctx, t0, addr);
6a6ae23f 8883 tcg_gen_shli_tl(cpu_gprh[rD(ctx->opcode)], t0, 16);
76db3ba4
AJ
8884 gen_addr_add(ctx, addr, addr, 2);
8885 gen_qemu_ld16u(ctx, t0, addr);
6a6ae23f 8886 tcg_gen_or_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rD(ctx->opcode)], t0);
0487d6a8 8887#endif
6a6ae23f 8888 tcg_temp_free(t0);
0487d6a8
JM
8889}
8890
636aa200 8891static inline void gen_op_evlhhesplat(DisasContext *ctx, TCGv addr)
6a6ae23f
AJ
8892{
8893 TCGv t0 = tcg_temp_new();
76db3ba4 8894 gen_qemu_ld16u(ctx, t0, addr);
6a6ae23f
AJ
8895#if defined(TARGET_PPC64)
8896 tcg_gen_shli_tl(cpu_gpr[rD(ctx->opcode)], t0, 48);
8897 tcg_gen_shli_tl(t0, t0, 16);
8898 tcg_gen_or_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rD(ctx->opcode)], t0);
8899#else
8900 tcg_gen_shli_tl(t0, t0, 16);
8901 tcg_gen_mov_tl(cpu_gprh[rD(ctx->opcode)], t0);
8902 tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], t0);
8903#endif
8904 tcg_temp_free(t0);
0487d6a8
JM
8905}
8906
636aa200 8907static inline void gen_op_evlhhousplat(DisasContext *ctx, TCGv addr)
6a6ae23f
AJ
8908{
8909 TCGv t0 = tcg_temp_new();
76db3ba4 8910 gen_qemu_ld16u(ctx, t0, addr);
6a6ae23f
AJ
8911#if defined(TARGET_PPC64)
8912 tcg_gen_shli_tl(cpu_gpr[rD(ctx->opcode)], t0, 32);
8913 tcg_gen_or_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rD(ctx->opcode)], t0);
8914#else
8915 tcg_gen_mov_tl(cpu_gprh[rD(ctx->opcode)], t0);
8916 tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], t0);
8917#endif
8918 tcg_temp_free(t0);
0487d6a8
JM
8919}
8920
636aa200 8921static inline void gen_op_evlhhossplat(DisasContext *ctx, TCGv addr)
6a6ae23f
AJ
8922{
8923 TCGv t0 = tcg_temp_new();
76db3ba4 8924 gen_qemu_ld16s(ctx, t0, addr);
6a6ae23f
AJ
8925#if defined(TARGET_PPC64)
8926 tcg_gen_shli_tl(cpu_gpr[rD(ctx->opcode)], t0, 32);
8927 tcg_gen_ext32u_tl(t0, t0);
8928 tcg_gen_or_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rD(ctx->opcode)], t0);
8929#else
8930 tcg_gen_mov_tl(cpu_gprh[rD(ctx->opcode)], t0);
8931 tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], t0);
8932#endif
8933 tcg_temp_free(t0);
8934}
8935
636aa200 8936static inline void gen_op_evlwhe(DisasContext *ctx, TCGv addr)
6a6ae23f
AJ
8937{
8938 TCGv t0 = tcg_temp_new();
8939#if defined(TARGET_PPC64)
76db3ba4 8940 gen_qemu_ld16u(ctx, t0, addr);
6a6ae23f 8941 tcg_gen_shli_tl(cpu_gpr[rD(ctx->opcode)], t0, 48);
76db3ba4
AJ
8942 gen_addr_add(ctx, addr, addr, 2);
8943 gen_qemu_ld16u(ctx, t0, addr);
6a6ae23f
AJ
8944 tcg_gen_shli_tl(t0, t0, 16);
8945 tcg_gen_or_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rD(ctx->opcode)], t0);
8946#else
76db3ba4 8947 gen_qemu_ld16u(ctx, t0, addr);
6a6ae23f 8948 tcg_gen_shli_tl(cpu_gprh[rD(ctx->opcode)], t0, 16);
76db3ba4
AJ
8949 gen_addr_add(ctx, addr, addr, 2);
8950 gen_qemu_ld16u(ctx, t0, addr);
6a6ae23f
AJ
8951 tcg_gen_shli_tl(cpu_gpr[rD(ctx->opcode)], t0, 16);
8952#endif
8953 tcg_temp_free(t0);
8954}
8955
636aa200 8956static inline void gen_op_evlwhou(DisasContext *ctx, TCGv addr)
6a6ae23f
AJ
8957{
8958#if defined(TARGET_PPC64)
8959 TCGv t0 = tcg_temp_new();
76db3ba4
AJ
8960 gen_qemu_ld16u(ctx, cpu_gpr[rD(ctx->opcode)], addr);
8961 gen_addr_add(ctx, addr, addr, 2);
8962 gen_qemu_ld16u(ctx, t0, addr);
6a6ae23f
AJ
8963 tcg_gen_shli_tl(t0, t0, 32);
8964 tcg_gen_or_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rD(ctx->opcode)], t0);
8965 tcg_temp_free(t0);
8966#else
76db3ba4
AJ
8967 gen_qemu_ld16u(ctx, cpu_gprh[rD(ctx->opcode)], addr);
8968 gen_addr_add(ctx, addr, addr, 2);
8969 gen_qemu_ld16u(ctx, cpu_gpr[rD(ctx->opcode)], addr);
6a6ae23f
AJ
8970#endif
8971}
8972
636aa200 8973static inline void gen_op_evlwhos(DisasContext *ctx, TCGv addr)
6a6ae23f
AJ
8974{
8975#if defined(TARGET_PPC64)
8976 TCGv t0 = tcg_temp_new();
76db3ba4 8977 gen_qemu_ld16s(ctx, t0, addr);
6a6ae23f 8978 tcg_gen_ext32u_tl(cpu_gpr[rD(ctx->opcode)], t0);
76db3ba4
AJ
8979 gen_addr_add(ctx, addr, addr, 2);
8980 gen_qemu_ld16s(ctx, t0, addr);
6a6ae23f
AJ
8981 tcg_gen_shli_tl(t0, t0, 32);
8982 tcg_gen_or_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rD(ctx->opcode)], t0);
8983 tcg_temp_free(t0);
8984#else
76db3ba4
AJ
8985 gen_qemu_ld16s(ctx, cpu_gprh[rD(ctx->opcode)], addr);
8986 gen_addr_add(ctx, addr, addr, 2);
8987 gen_qemu_ld16s(ctx, cpu_gpr[rD(ctx->opcode)], addr);
6a6ae23f
AJ
8988#endif
8989}
8990
636aa200 8991static inline void gen_op_evlwwsplat(DisasContext *ctx, TCGv addr)
6a6ae23f
AJ
8992{
8993 TCGv t0 = tcg_temp_new();
76db3ba4 8994 gen_qemu_ld32u(ctx, t0, addr);
0487d6a8 8995#if defined(TARGET_PPC64)
6a6ae23f
AJ
8996 tcg_gen_shli_tl(cpu_gpr[rD(ctx->opcode)], t0, 32);
8997 tcg_gen_or_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rD(ctx->opcode)], t0);
8998#else
8999 tcg_gen_mov_tl(cpu_gprh[rD(ctx->opcode)], t0);
9000 tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], t0);
9001#endif
9002 tcg_temp_free(t0);
9003}
9004
636aa200 9005static inline void gen_op_evlwhsplat(DisasContext *ctx, TCGv addr)
6a6ae23f
AJ
9006{
9007 TCGv t0 = tcg_temp_new();
9008#if defined(TARGET_PPC64)
76db3ba4 9009 gen_qemu_ld16u(ctx, t0, addr);
6a6ae23f
AJ
9010 tcg_gen_shli_tl(cpu_gpr[rD(ctx->opcode)], t0, 48);
9011 tcg_gen_shli_tl(t0, t0, 32);
9012 tcg_gen_or_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rD(ctx->opcode)], t0);
76db3ba4
AJ
9013 gen_addr_add(ctx, addr, addr, 2);
9014 gen_qemu_ld16u(ctx, t0, addr);
6a6ae23f
AJ
9015 tcg_gen_or_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rD(ctx->opcode)], t0);
9016 tcg_gen_shli_tl(t0, t0, 16);
9017 tcg_gen_or_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rD(ctx->opcode)], t0);
9018#else
76db3ba4 9019 gen_qemu_ld16u(ctx, t0, addr);
6a6ae23f
AJ
9020 tcg_gen_shli_tl(cpu_gprh[rD(ctx->opcode)], t0, 16);
9021 tcg_gen_or_tl(cpu_gprh[rD(ctx->opcode)], cpu_gprh[rD(ctx->opcode)], t0);
76db3ba4
AJ
9022 gen_addr_add(ctx, addr, addr, 2);
9023 gen_qemu_ld16u(ctx, t0, addr);
6a6ae23f
AJ
9024 tcg_gen_shli_tl(cpu_gpr[rD(ctx->opcode)], t0, 16);
9025 tcg_gen_or_tl(cpu_gpr[rD(ctx->opcode)], cpu_gprh[rD(ctx->opcode)], t0);
0487d6a8 9026#endif
6a6ae23f
AJ
9027 tcg_temp_free(t0);
9028}
9029
636aa200 9030static inline void gen_op_evstdd(DisasContext *ctx, TCGv addr)
6a6ae23f
AJ
9031{
9032#if defined(TARGET_PPC64)
76db3ba4 9033 gen_qemu_st64(ctx, cpu_gpr[rS(ctx->opcode)], addr);
0487d6a8 9034#else
6a6ae23f
AJ
9035 TCGv_i64 t0 = tcg_temp_new_i64();
9036 tcg_gen_concat_i32_i64(t0, cpu_gpr[rS(ctx->opcode)], cpu_gprh[rS(ctx->opcode)]);
76db3ba4 9037 gen_qemu_st64(ctx, t0, addr);
6a6ae23f
AJ
9038 tcg_temp_free_i64(t0);
9039#endif
9040}
9041
636aa200 9042static inline void gen_op_evstdw(DisasContext *ctx, TCGv addr)
6a6ae23f 9043{
0487d6a8 9044#if defined(TARGET_PPC64)
6a6ae23f
AJ
9045 TCGv t0 = tcg_temp_new();
9046 tcg_gen_shri_tl(t0, cpu_gpr[rS(ctx->opcode)], 32);
76db3ba4 9047 gen_qemu_st32(ctx, t0, addr);
6a6ae23f
AJ
9048 tcg_temp_free(t0);
9049#else
76db3ba4 9050 gen_qemu_st32(ctx, cpu_gprh[rS(ctx->opcode)], addr);
6a6ae23f 9051#endif
76db3ba4
AJ
9052 gen_addr_add(ctx, addr, addr, 4);
9053 gen_qemu_st32(ctx, cpu_gpr[rS(ctx->opcode)], addr);
6a6ae23f
AJ
9054}
9055
636aa200 9056static inline void gen_op_evstdh(DisasContext *ctx, TCGv addr)
6a6ae23f
AJ
9057{
9058 TCGv t0 = tcg_temp_new();
9059#if defined(TARGET_PPC64)
9060 tcg_gen_shri_tl(t0, cpu_gpr[rS(ctx->opcode)], 48);
9061#else
9062 tcg_gen_shri_tl(t0, cpu_gprh[rS(ctx->opcode)], 16);
9063#endif
76db3ba4
AJ
9064 gen_qemu_st16(ctx, t0, addr);
9065 gen_addr_add(ctx, addr, addr, 2);
6a6ae23f
AJ
9066#if defined(TARGET_PPC64)
9067 tcg_gen_shri_tl(t0, cpu_gpr[rS(ctx->opcode)], 32);
76db3ba4 9068 gen_qemu_st16(ctx, t0, addr);
6a6ae23f 9069#else
76db3ba4 9070 gen_qemu_st16(ctx, cpu_gprh[rS(ctx->opcode)], addr);
6a6ae23f 9071#endif
76db3ba4 9072 gen_addr_add(ctx, addr, addr, 2);
6a6ae23f 9073 tcg_gen_shri_tl(t0, cpu_gpr[rS(ctx->opcode)], 16);
76db3ba4 9074 gen_qemu_st16(ctx, t0, addr);
6a6ae23f 9075 tcg_temp_free(t0);
76db3ba4
AJ
9076 gen_addr_add(ctx, addr, addr, 2);
9077 gen_qemu_st16(ctx, cpu_gpr[rS(ctx->opcode)], addr);
6a6ae23f
AJ
9078}
9079
636aa200 9080static inline void gen_op_evstwhe(DisasContext *ctx, TCGv addr)
6a6ae23f
AJ
9081{
9082 TCGv t0 = tcg_temp_new();
9083#if defined(TARGET_PPC64)
9084 tcg_gen_shri_tl(t0, cpu_gpr[rS(ctx->opcode)], 48);
9085#else
9086 tcg_gen_shri_tl(t0, cpu_gprh[rS(ctx->opcode)], 16);
9087#endif
76db3ba4
AJ
9088 gen_qemu_st16(ctx, t0, addr);
9089 gen_addr_add(ctx, addr, addr, 2);
6a6ae23f 9090 tcg_gen_shri_tl(t0, cpu_gpr[rS(ctx->opcode)], 16);
76db3ba4 9091 gen_qemu_st16(ctx, t0, addr);
6a6ae23f
AJ
9092 tcg_temp_free(t0);
9093}
9094
636aa200 9095static inline void gen_op_evstwho(DisasContext *ctx, TCGv addr)
6a6ae23f
AJ
9096{
9097#if defined(TARGET_PPC64)
9098 TCGv t0 = tcg_temp_new();
9099 tcg_gen_shri_tl(t0, cpu_gpr[rS(ctx->opcode)], 32);
76db3ba4 9100 gen_qemu_st16(ctx, t0, addr);
6a6ae23f
AJ
9101 tcg_temp_free(t0);
9102#else
76db3ba4 9103 gen_qemu_st16(ctx, cpu_gprh[rS(ctx->opcode)], addr);
6a6ae23f 9104#endif
76db3ba4
AJ
9105 gen_addr_add(ctx, addr, addr, 2);
9106 gen_qemu_st16(ctx, cpu_gpr[rS(ctx->opcode)], addr);
6a6ae23f
AJ
9107}
9108
636aa200 9109static inline void gen_op_evstwwe(DisasContext *ctx, TCGv addr)
6a6ae23f
AJ
9110{
9111#if defined(TARGET_PPC64)
9112 TCGv t0 = tcg_temp_new();
9113 tcg_gen_shri_tl(t0, cpu_gpr[rS(ctx->opcode)], 32);
76db3ba4 9114 gen_qemu_st32(ctx, t0, addr);
6a6ae23f
AJ
9115 tcg_temp_free(t0);
9116#else
76db3ba4 9117 gen_qemu_st32(ctx, cpu_gprh[rS(ctx->opcode)], addr);
6a6ae23f
AJ
9118#endif
9119}
9120
636aa200 9121static inline void gen_op_evstwwo(DisasContext *ctx, TCGv addr)
6a6ae23f 9122{
76db3ba4 9123 gen_qemu_st32(ctx, cpu_gpr[rS(ctx->opcode)], addr);
6a6ae23f
AJ
9124}
9125
9126#define GEN_SPEOP_LDST(name, opc2, sh) \
99e300ef 9127static void glue(gen_, name)(DisasContext *ctx) \
6a6ae23f
AJ
9128{ \
9129 TCGv t0; \
9130 if (unlikely(!ctx->spe_enabled)) { \
27a69bb0 9131 gen_exception(ctx, POWERPC_EXCP_SPEU); \
6a6ae23f
AJ
9132 return; \
9133 } \
76db3ba4 9134 gen_set_access_type(ctx, ACCESS_INT); \
6a6ae23f
AJ
9135 t0 = tcg_temp_new(); \
9136 if (Rc(ctx->opcode)) { \
76db3ba4 9137 gen_addr_spe_imm_index(ctx, t0, sh); \
6a6ae23f 9138 } else { \
76db3ba4 9139 gen_addr_reg_index(ctx, t0); \
6a6ae23f
AJ
9140 } \
9141 gen_op_##name(ctx, t0); \
9142 tcg_temp_free(t0); \
9143}
9144
9145GEN_SPEOP_LDST(evldd, 0x00, 3);
9146GEN_SPEOP_LDST(evldw, 0x01, 3);
9147GEN_SPEOP_LDST(evldh, 0x02, 3);
9148GEN_SPEOP_LDST(evlhhesplat, 0x04, 1);
9149GEN_SPEOP_LDST(evlhhousplat, 0x06, 1);
9150GEN_SPEOP_LDST(evlhhossplat, 0x07, 1);
9151GEN_SPEOP_LDST(evlwhe, 0x08, 2);
9152GEN_SPEOP_LDST(evlwhou, 0x0A, 2);
9153GEN_SPEOP_LDST(evlwhos, 0x0B, 2);
9154GEN_SPEOP_LDST(evlwwsplat, 0x0C, 2);
9155GEN_SPEOP_LDST(evlwhsplat, 0x0E, 2);
9156
9157GEN_SPEOP_LDST(evstdd, 0x10, 3);
9158GEN_SPEOP_LDST(evstdw, 0x11, 3);
9159GEN_SPEOP_LDST(evstdh, 0x12, 3);
9160GEN_SPEOP_LDST(evstwhe, 0x18, 2);
9161GEN_SPEOP_LDST(evstwho, 0x1A, 2);
9162GEN_SPEOP_LDST(evstwwe, 0x1C, 2);
9163GEN_SPEOP_LDST(evstwwo, 0x1E, 2);
0487d6a8
JM
9164
9165/* Multiply and add - TODO */
9166#if 0
70560da7
FC
9167GEN_SPE(speundef, evmhessf, 0x01, 0x10, 0xFFFFFFFF, 0x00000000, PPC_SPE);//
9168GEN_SPE(speundef, evmhossf, 0x03, 0x10, 0xFFFFFFFF, 0x00000000, PPC_SPE);
9169GEN_SPE(evmheumi, evmhesmi, 0x04, 0x10, 0x00000000, 0x00000000, PPC_SPE);
9170GEN_SPE(speundef, evmhesmf, 0x05, 0x10, 0xFFFFFFFF, 0x00000000, PPC_SPE);
9171GEN_SPE(evmhoumi, evmhosmi, 0x06, 0x10, 0x00000000, 0x00000000, PPC_SPE);
9172GEN_SPE(speundef, evmhosmf, 0x07, 0x10, 0xFFFFFFFF, 0x00000000, PPC_SPE);
9173GEN_SPE(speundef, evmhessfa, 0x11, 0x10, 0xFFFFFFFF, 0x00000000, PPC_SPE);
9174GEN_SPE(speundef, evmhossfa, 0x13, 0x10, 0xFFFFFFFF, 0x00000000, PPC_SPE);
9175GEN_SPE(evmheumia, evmhesmia, 0x14, 0x10, 0x00000000, 0x00000000, PPC_SPE);
9176GEN_SPE(speundef, evmhesmfa, 0x15, 0x10, 0xFFFFFFFF, 0x00000000, PPC_SPE);
9177GEN_SPE(evmhoumia, evmhosmia, 0x16, 0x10, 0x00000000, 0x00000000, PPC_SPE);
9178GEN_SPE(speundef, evmhosmfa, 0x17, 0x10, 0xFFFFFFFF, 0x00000000, PPC_SPE);
9179
9180GEN_SPE(speundef, evmwhssf, 0x03, 0x11, 0xFFFFFFFF, 0x00000000, PPC_SPE);
9181GEN_SPE(evmwlumi, speundef, 0x04, 0x11, 0x00000000, 0xFFFFFFFF, PPC_SPE);
9182GEN_SPE(evmwhumi, evmwhsmi, 0x06, 0x11, 0x00000000, 0x00000000, PPC_SPE);
9183GEN_SPE(speundef, evmwhsmf, 0x07, 0x11, 0xFFFFFFFF, 0x00000000, PPC_SPE);
9184GEN_SPE(speundef, evmwssf, 0x09, 0x11, 0xFFFFFFFF, 0x00000000, PPC_SPE);
9185GEN_SPE(speundef, evmwsmf, 0x0D, 0x11, 0xFFFFFFFF, 0x00000000, PPC_SPE);
9186GEN_SPE(speundef, evmwhssfa, 0x13, 0x11, 0xFFFFFFFF, 0x00000000, PPC_SPE);
9187GEN_SPE(evmwlumia, speundef, 0x14, 0x11, 0x00000000, 0xFFFFFFFF, PPC_SPE);
9188GEN_SPE(evmwhumia, evmwhsmia, 0x16, 0x11, 0x00000000, 0x00000000, PPC_SPE);
9189GEN_SPE(speundef, evmwhsmfa, 0x17, 0x11, 0xFFFFFFFF, 0x00000000, PPC_SPE);
9190GEN_SPE(speundef, evmwssfa, 0x19, 0x11, 0xFFFFFFFF, 0x00000000, PPC_SPE);
9191GEN_SPE(speundef, evmwsmfa, 0x1D, 0x11, 0xFFFFFFFF, 0x00000000, PPC_SPE);
9192
9193GEN_SPE(evadduiaaw, evaddsiaaw, 0x00, 0x13, 0x0000F800, 0x0000F800, PPC_SPE);
9194GEN_SPE(evsubfusiaaw, evsubfssiaaw, 0x01, 0x13, 0x0000F800, 0x0000F800, PPC_SPE);
9195GEN_SPE(evaddumiaaw, evaddsmiaaw, 0x04, 0x13, 0x0000F800, 0x0000F800, PPC_SPE);
9196GEN_SPE(evsubfumiaaw, evsubfsmiaaw, 0x05, 0x13, 0x0000F800, 0x0000F800, PPC_SPE);
9197GEN_SPE(evdivws, evdivwu, 0x06, 0x13, 0x00000000, 0x00000000, PPC_SPE);
9198
9199GEN_SPE(evmheusiaaw, evmhessiaaw, 0x00, 0x14, 0x00000000, 0x00000000, PPC_SPE);
9200GEN_SPE(speundef, evmhessfaaw, 0x01, 0x14, 0xFFFFFFFF, 0x00000000, PPC_SPE);
9201GEN_SPE(evmhousiaaw, evmhossiaaw, 0x02, 0x14, 0x00000000, 0x00000000, PPC_SPE);
9202GEN_SPE(speundef, evmhossfaaw, 0x03, 0x14, 0xFFFFFFFF, 0x00000000, PPC_SPE);
9203GEN_SPE(evmheumiaaw, evmhesmiaaw, 0x04, 0x14, 0x00000000, 0x00000000, PPC_SPE);
9204GEN_SPE(speundef, evmhesmfaaw, 0x05, 0x14, 0xFFFFFFFF, 0x00000000, PPC_SPE);
9205GEN_SPE(evmhoumiaaw, evmhosmiaaw, 0x06, 0x14, 0x00000000, 0x00000000, PPC_SPE);
9206GEN_SPE(speundef, evmhosmfaaw, 0x07, 0x14, 0xFFFFFFFF, 0x00000000, PPC_SPE);
9207GEN_SPE(evmhegumiaa, evmhegsmiaa, 0x14, 0x14, 0x00000000, 0x00000000, PPC_SPE);
9208GEN_SPE(speundef, evmhegsmfaa, 0x15, 0x14, 0xFFFFFFFF, 0x00000000, PPC_SPE);
9209GEN_SPE(evmhogumiaa, evmhogsmiaa, 0x16, 0x14, 0x00000000, 0x00000000, PPC_SPE);
9210GEN_SPE(speundef, evmhogsmfaa, 0x17, 0x14, 0xFFFFFFFF, 0x00000000, PPC_SPE);
9211
9212GEN_SPE(evmwlusiaaw, evmwlssiaaw, 0x00, 0x15, 0x00000000, 0x00000000, PPC_SPE);
9213GEN_SPE(evmwlumiaaw, evmwlsmiaaw, 0x04, 0x15, 0x00000000, 0x00000000, PPC_SPE);
9214GEN_SPE(speundef, evmwssfaa, 0x09, 0x15, 0xFFFFFFFF, 0x00000000, PPC_SPE);
9215GEN_SPE(speundef, evmwsmfaa, 0x0D, 0x15, 0xFFFFFFFF, 0x00000000, PPC_SPE);
9216
9217GEN_SPE(evmheusianw, evmhessianw, 0x00, 0x16, 0x00000000, 0x00000000, PPC_SPE);
9218GEN_SPE(speundef, evmhessfanw, 0x01, 0x16, 0xFFFFFFFF, 0x00000000, PPC_SPE);
9219GEN_SPE(evmhousianw, evmhossianw, 0x02, 0x16, 0x00000000, 0x00000000, PPC_SPE);
9220GEN_SPE(speundef, evmhossfanw, 0x03, 0x16, 0xFFFFFFFF, 0x00000000, PPC_SPE);
9221GEN_SPE(evmheumianw, evmhesmianw, 0x04, 0x16, 0x00000000, 0x00000000, PPC_SPE);
9222GEN_SPE(speundef, evmhesmfanw, 0x05, 0x16, 0xFFFFFFFF, 0x00000000, PPC_SPE);
9223GEN_SPE(evmhoumianw, evmhosmianw, 0x06, 0x16, 0x00000000, 0x00000000, PPC_SPE);
9224GEN_SPE(speundef, evmhosmfanw, 0x07, 0x16, 0xFFFFFFFF, 0x00000000, PPC_SPE);
9225GEN_SPE(evmhegumian, evmhegsmian, 0x14, 0x16, 0x00000000, 0x00000000, PPC_SPE);
9226GEN_SPE(speundef, evmhegsmfan, 0x15, 0x16, 0xFFFFFFFF, 0x00000000, PPC_SPE);
9227GEN_SPE(evmhigumian, evmhigsmian, 0x16, 0x16, 0x00000000, 0x00000000, PPC_SPE);
9228GEN_SPE(speundef, evmhogsmfan, 0x17, 0x16, 0xFFFFFFFF, 0x00000000, PPC_SPE);
9229
9230GEN_SPE(evmwlusianw, evmwlssianw, 0x00, 0x17, 0x00000000, 0x00000000, PPC_SPE);
9231GEN_SPE(evmwlumianw, evmwlsmianw, 0x04, 0x17, 0x00000000, 0x00000000, PPC_SPE);
9232GEN_SPE(speundef, evmwssfan, 0x09, 0x17, 0xFFFFFFFF, 0x00000000, PPC_SPE);
9233GEN_SPE(evmwumian, evmwsmian, 0x0C, 0x17, 0x00000000, 0x00000000, PPC_SPE);
9234GEN_SPE(speundef, evmwsmfan, 0x0D, 0x17, 0xFFFFFFFF, 0x00000000, PPC_SPE);
0487d6a8
JM
9235#endif
9236
9237/*** SPE floating-point extension ***/
1c97856d
AJ
9238#if defined(TARGET_PPC64)
9239#define GEN_SPEFPUOP_CONV_32_32(name) \
636aa200 9240static inline void gen_##name(DisasContext *ctx) \
0487d6a8 9241{ \
1c97856d
AJ
9242 TCGv_i32 t0; \
9243 TCGv t1; \
9244 t0 = tcg_temp_new_i32(); \
9245 tcg_gen_trunc_tl_i32(t0, cpu_gpr[rB(ctx->opcode)]); \
8e703949 9246 gen_helper_##name(t0, cpu_env, t0); \
1c97856d
AJ
9247 t1 = tcg_temp_new(); \
9248 tcg_gen_extu_i32_tl(t1, t0); \
9249 tcg_temp_free_i32(t0); \
9250 tcg_gen_andi_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rD(ctx->opcode)], \
9251 0xFFFFFFFF00000000ULL); \
9252 tcg_gen_or_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rD(ctx->opcode)], t1); \
9253 tcg_temp_free(t1); \
0487d6a8 9254}
1c97856d 9255#define GEN_SPEFPUOP_CONV_32_64(name) \
636aa200 9256static inline void gen_##name(DisasContext *ctx) \
1c97856d
AJ
9257{ \
9258 TCGv_i32 t0; \
9259 TCGv t1; \
9260 t0 = tcg_temp_new_i32(); \
8e703949 9261 gen_helper_##name(t0, cpu_env, cpu_gpr[rB(ctx->opcode)]); \
1c97856d
AJ
9262 t1 = tcg_temp_new(); \
9263 tcg_gen_extu_i32_tl(t1, t0); \
9264 tcg_temp_free_i32(t0); \
9265 tcg_gen_andi_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rD(ctx->opcode)], \
9266 0xFFFFFFFF00000000ULL); \
9267 tcg_gen_or_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rD(ctx->opcode)], t1); \
9268 tcg_temp_free(t1); \
9269}
9270#define GEN_SPEFPUOP_CONV_64_32(name) \
636aa200 9271static inline void gen_##name(DisasContext *ctx) \
1c97856d
AJ
9272{ \
9273 TCGv_i32 t0 = tcg_temp_new_i32(); \
9274 tcg_gen_trunc_tl_i32(t0, cpu_gpr[rB(ctx->opcode)]); \
8e703949 9275 gen_helper_##name(cpu_gpr[rD(ctx->opcode)], cpu_env, t0); \
1c97856d
AJ
9276 tcg_temp_free_i32(t0); \
9277}
9278#define GEN_SPEFPUOP_CONV_64_64(name) \
636aa200 9279static inline void gen_##name(DisasContext *ctx) \
1c97856d 9280{ \
8e703949
BS
9281 gen_helper_##name(cpu_gpr[rD(ctx->opcode)], cpu_env, \
9282 cpu_gpr[rB(ctx->opcode)]); \
1c97856d
AJ
9283}
9284#define GEN_SPEFPUOP_ARITH2_32_32(name) \
636aa200 9285static inline void gen_##name(DisasContext *ctx) \
57951c27 9286{ \
1c97856d
AJ
9287 TCGv_i32 t0, t1; \
9288 TCGv_i64 t2; \
57951c27 9289 if (unlikely(!ctx->spe_enabled)) { \
27a69bb0 9290 gen_exception(ctx, POWERPC_EXCP_SPEU); \
57951c27
AJ
9291 return; \
9292 } \
1c97856d
AJ
9293 t0 = tcg_temp_new_i32(); \
9294 t1 = tcg_temp_new_i32(); \
9295 tcg_gen_trunc_tl_i32(t0, cpu_gpr[rA(ctx->opcode)]); \
9296 tcg_gen_trunc_tl_i32(t1, cpu_gpr[rB(ctx->opcode)]); \
8e703949 9297 gen_helper_##name(t0, cpu_env, t0, t1); \
1c97856d
AJ
9298 tcg_temp_free_i32(t1); \
9299 t2 = tcg_temp_new(); \
9300 tcg_gen_extu_i32_tl(t2, t0); \
9301 tcg_temp_free_i32(t0); \
9302 tcg_gen_andi_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rD(ctx->opcode)], \
9303 0xFFFFFFFF00000000ULL); \
9304 tcg_gen_or_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rD(ctx->opcode)], t2); \
9305 tcg_temp_free(t2); \
57951c27 9306}
1c97856d 9307#define GEN_SPEFPUOP_ARITH2_64_64(name) \
636aa200 9308static inline void gen_##name(DisasContext *ctx) \
57951c27
AJ
9309{ \
9310 if (unlikely(!ctx->spe_enabled)) { \
27a69bb0 9311 gen_exception(ctx, POWERPC_EXCP_SPEU); \
57951c27
AJ
9312 return; \
9313 } \
8e703949
BS
9314 gen_helper_##name(cpu_gpr[rD(ctx->opcode)], cpu_env, \
9315 cpu_gpr[rA(ctx->opcode)], cpu_gpr[rB(ctx->opcode)]); \
57951c27 9316}
1c97856d 9317#define GEN_SPEFPUOP_COMP_32(name) \
636aa200 9318static inline void gen_##name(DisasContext *ctx) \
57951c27 9319{ \
1c97856d 9320 TCGv_i32 t0, t1; \
57951c27 9321 if (unlikely(!ctx->spe_enabled)) { \
27a69bb0 9322 gen_exception(ctx, POWERPC_EXCP_SPEU); \
57951c27
AJ
9323 return; \
9324 } \
1c97856d
AJ
9325 t0 = tcg_temp_new_i32(); \
9326 t1 = tcg_temp_new_i32(); \
9327 tcg_gen_trunc_tl_i32(t0, cpu_gpr[rA(ctx->opcode)]); \
9328 tcg_gen_trunc_tl_i32(t1, cpu_gpr[rB(ctx->opcode)]); \
8e703949 9329 gen_helper_##name(cpu_crf[crfD(ctx->opcode)], cpu_env, t0, t1); \
1c97856d
AJ
9330 tcg_temp_free_i32(t0); \
9331 tcg_temp_free_i32(t1); \
9332}
9333#define GEN_SPEFPUOP_COMP_64(name) \
636aa200 9334static inline void gen_##name(DisasContext *ctx) \
1c97856d
AJ
9335{ \
9336 if (unlikely(!ctx->spe_enabled)) { \
27a69bb0 9337 gen_exception(ctx, POWERPC_EXCP_SPEU); \
1c97856d
AJ
9338 return; \
9339 } \
8e703949 9340 gen_helper_##name(cpu_crf[crfD(ctx->opcode)], cpu_env, \
1c97856d
AJ
9341 cpu_gpr[rA(ctx->opcode)], cpu_gpr[rB(ctx->opcode)]); \
9342}
9343#else
9344#define GEN_SPEFPUOP_CONV_32_32(name) \
636aa200 9345static inline void gen_##name(DisasContext *ctx) \
1c97856d 9346{ \
8e703949
BS
9347 gen_helper_##name(cpu_gpr[rD(ctx->opcode)], cpu_env, \
9348 cpu_gpr[rB(ctx->opcode)]); \
57951c27 9349}
1c97856d 9350#define GEN_SPEFPUOP_CONV_32_64(name) \
636aa200 9351static inline void gen_##name(DisasContext *ctx) \
1c97856d
AJ
9352{ \
9353 TCGv_i64 t0 = tcg_temp_new_i64(); \
9354 gen_load_gpr64(t0, rB(ctx->opcode)); \
8e703949 9355 gen_helper_##name(cpu_gpr[rD(ctx->opcode)], cpu_env, t0); \
1c97856d
AJ
9356 tcg_temp_free_i64(t0); \
9357}
9358#define GEN_SPEFPUOP_CONV_64_32(name) \
636aa200 9359static inline void gen_##name(DisasContext *ctx) \
1c97856d
AJ
9360{ \
9361 TCGv_i64 t0 = tcg_temp_new_i64(); \
8e703949 9362 gen_helper_##name(t0, cpu_env, cpu_gpr[rB(ctx->opcode)]); \
1c97856d
AJ
9363 gen_store_gpr64(rD(ctx->opcode), t0); \
9364 tcg_temp_free_i64(t0); \
9365}
9366#define GEN_SPEFPUOP_CONV_64_64(name) \
636aa200 9367static inline void gen_##name(DisasContext *ctx) \
1c97856d
AJ
9368{ \
9369 TCGv_i64 t0 = tcg_temp_new_i64(); \
9370 gen_load_gpr64(t0, rB(ctx->opcode)); \
8e703949 9371 gen_helper_##name(t0, cpu_env, t0); \
1c97856d
AJ
9372 gen_store_gpr64(rD(ctx->opcode), t0); \
9373 tcg_temp_free_i64(t0); \
9374}
9375#define GEN_SPEFPUOP_ARITH2_32_32(name) \
636aa200 9376static inline void gen_##name(DisasContext *ctx) \
1c97856d
AJ
9377{ \
9378 if (unlikely(!ctx->spe_enabled)) { \
27a69bb0 9379 gen_exception(ctx, POWERPC_EXCP_SPEU); \
1c97856d
AJ
9380 return; \
9381 } \
8e703949 9382 gen_helper_##name(cpu_gpr[rD(ctx->opcode)], cpu_env, \
1c97856d
AJ
9383 cpu_gpr[rA(ctx->opcode)], cpu_gpr[rB(ctx->opcode)]); \
9384}
9385#define GEN_SPEFPUOP_ARITH2_64_64(name) \
636aa200 9386static inline void gen_##name(DisasContext *ctx) \
1c97856d
AJ
9387{ \
9388 TCGv_i64 t0, t1; \
9389 if (unlikely(!ctx->spe_enabled)) { \
27a69bb0 9390 gen_exception(ctx, POWERPC_EXCP_SPEU); \
1c97856d
AJ
9391 return; \
9392 } \
9393 t0 = tcg_temp_new_i64(); \
9394 t1 = tcg_temp_new_i64(); \
9395 gen_load_gpr64(t0, rA(ctx->opcode)); \
9396 gen_load_gpr64(t1, rB(ctx->opcode)); \
8e703949 9397 gen_helper_##name(t0, cpu_env, t0, t1); \
1c97856d
AJ
9398 gen_store_gpr64(rD(ctx->opcode), t0); \
9399 tcg_temp_free_i64(t0); \
9400 tcg_temp_free_i64(t1); \
9401}
9402#define GEN_SPEFPUOP_COMP_32(name) \
636aa200 9403static inline void gen_##name(DisasContext *ctx) \
1c97856d
AJ
9404{ \
9405 if (unlikely(!ctx->spe_enabled)) { \
27a69bb0 9406 gen_exception(ctx, POWERPC_EXCP_SPEU); \
1c97856d
AJ
9407 return; \
9408 } \
8e703949 9409 gen_helper_##name(cpu_crf[crfD(ctx->opcode)], cpu_env, \
1c97856d
AJ
9410 cpu_gpr[rA(ctx->opcode)], cpu_gpr[rB(ctx->opcode)]); \
9411}
9412#define GEN_SPEFPUOP_COMP_64(name) \
636aa200 9413static inline void gen_##name(DisasContext *ctx) \
1c97856d
AJ
9414{ \
9415 TCGv_i64 t0, t1; \
9416 if (unlikely(!ctx->spe_enabled)) { \
27a69bb0 9417 gen_exception(ctx, POWERPC_EXCP_SPEU); \
1c97856d
AJ
9418 return; \
9419 } \
9420 t0 = tcg_temp_new_i64(); \
9421 t1 = tcg_temp_new_i64(); \
9422 gen_load_gpr64(t0, rA(ctx->opcode)); \
9423 gen_load_gpr64(t1, rB(ctx->opcode)); \
8e703949 9424 gen_helper_##name(cpu_crf[crfD(ctx->opcode)], cpu_env, t0, t1); \
1c97856d
AJ
9425 tcg_temp_free_i64(t0); \
9426 tcg_temp_free_i64(t1); \
9427}
9428#endif
57951c27 9429
0487d6a8
JM
9430/* Single precision floating-point vectors operations */
9431/* Arithmetic */
1c97856d
AJ
9432GEN_SPEFPUOP_ARITH2_64_64(evfsadd);
9433GEN_SPEFPUOP_ARITH2_64_64(evfssub);
9434GEN_SPEFPUOP_ARITH2_64_64(evfsmul);
9435GEN_SPEFPUOP_ARITH2_64_64(evfsdiv);
636aa200 9436static inline void gen_evfsabs(DisasContext *ctx)
1c97856d
AJ
9437{
9438 if (unlikely(!ctx->spe_enabled)) {
27a69bb0 9439 gen_exception(ctx, POWERPC_EXCP_SPEU);
1c97856d
AJ
9440 return;
9441 }
9442#if defined(TARGET_PPC64)
6d5c34fa 9443 tcg_gen_andi_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)], ~0x8000000080000000LL);
1c97856d 9444#else
6d5c34fa
MP
9445 tcg_gen_andi_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)], ~0x80000000);
9446 tcg_gen_andi_tl(cpu_gprh[rD(ctx->opcode)], cpu_gprh[rA(ctx->opcode)], ~0x80000000);
1c97856d
AJ
9447#endif
9448}
636aa200 9449static inline void gen_evfsnabs(DisasContext *ctx)
1c97856d
AJ
9450{
9451 if (unlikely(!ctx->spe_enabled)) {
27a69bb0 9452 gen_exception(ctx, POWERPC_EXCP_SPEU);
1c97856d
AJ
9453 return;
9454 }
9455#if defined(TARGET_PPC64)
6d5c34fa 9456 tcg_gen_ori_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)], 0x8000000080000000LL);
1c97856d 9457#else
6d5c34fa
MP
9458 tcg_gen_ori_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)], 0x80000000);
9459 tcg_gen_ori_tl(cpu_gprh[rD(ctx->opcode)], cpu_gprh[rA(ctx->opcode)], 0x80000000);
1c97856d
AJ
9460#endif
9461}
636aa200 9462static inline void gen_evfsneg(DisasContext *ctx)
1c97856d
AJ
9463{
9464 if (unlikely(!ctx->spe_enabled)) {
27a69bb0 9465 gen_exception(ctx, POWERPC_EXCP_SPEU);
1c97856d
AJ
9466 return;
9467 }
9468#if defined(TARGET_PPC64)
6d5c34fa 9469 tcg_gen_xori_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)], 0x8000000080000000LL);
1c97856d 9470#else
6d5c34fa
MP
9471 tcg_gen_xori_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)], 0x80000000);
9472 tcg_gen_xori_tl(cpu_gprh[rD(ctx->opcode)], cpu_gprh[rA(ctx->opcode)], 0x80000000);
1c97856d
AJ
9473#endif
9474}
9475
0487d6a8 9476/* Conversion */
1c97856d
AJ
9477GEN_SPEFPUOP_CONV_64_64(evfscfui);
9478GEN_SPEFPUOP_CONV_64_64(evfscfsi);
9479GEN_SPEFPUOP_CONV_64_64(evfscfuf);
9480GEN_SPEFPUOP_CONV_64_64(evfscfsf);
9481GEN_SPEFPUOP_CONV_64_64(evfsctui);
9482GEN_SPEFPUOP_CONV_64_64(evfsctsi);
9483GEN_SPEFPUOP_CONV_64_64(evfsctuf);
9484GEN_SPEFPUOP_CONV_64_64(evfsctsf);
9485GEN_SPEFPUOP_CONV_64_64(evfsctuiz);
9486GEN_SPEFPUOP_CONV_64_64(evfsctsiz);
9487
0487d6a8 9488/* Comparison */
1c97856d
AJ
9489GEN_SPEFPUOP_COMP_64(evfscmpgt);
9490GEN_SPEFPUOP_COMP_64(evfscmplt);
9491GEN_SPEFPUOP_COMP_64(evfscmpeq);
9492GEN_SPEFPUOP_COMP_64(evfststgt);
9493GEN_SPEFPUOP_COMP_64(evfststlt);
9494GEN_SPEFPUOP_COMP_64(evfststeq);
0487d6a8
JM
9495
9496/* Opcodes definitions */
70560da7
FC
9497GEN_SPE(evfsadd, evfssub, 0x00, 0x0A, 0x00000000, 0x00000000, PPC_SPE_SINGLE); //
9498GEN_SPE(evfsabs, evfsnabs, 0x02, 0x0A, 0x0000F800, 0x0000F800, PPC_SPE_SINGLE); //
9499GEN_SPE(evfsneg, speundef, 0x03, 0x0A, 0x0000F800, 0xFFFFFFFF, PPC_SPE_SINGLE); //
9500GEN_SPE(evfsmul, evfsdiv, 0x04, 0x0A, 0x00000000, 0x00000000, PPC_SPE_SINGLE); //
9501GEN_SPE(evfscmpgt, evfscmplt, 0x06, 0x0A, 0x00600000, 0x00600000, PPC_SPE_SINGLE); //
9502GEN_SPE(evfscmpeq, speundef, 0x07, 0x0A, 0x00600000, 0xFFFFFFFF, PPC_SPE_SINGLE); //
9503GEN_SPE(evfscfui, evfscfsi, 0x08, 0x0A, 0x00180000, 0x00180000, PPC_SPE_SINGLE); //
9504GEN_SPE(evfscfuf, evfscfsf, 0x09, 0x0A, 0x00180000, 0x00180000, PPC_SPE_SINGLE); //
9505GEN_SPE(evfsctui, evfsctsi, 0x0A, 0x0A, 0x00180000, 0x00180000, PPC_SPE_SINGLE); //
9506GEN_SPE(evfsctuf, evfsctsf, 0x0B, 0x0A, 0x00180000, 0x00180000, PPC_SPE_SINGLE); //
9507GEN_SPE(evfsctuiz, speundef, 0x0C, 0x0A, 0x00180000, 0xFFFFFFFF, PPC_SPE_SINGLE); //
9508GEN_SPE(evfsctsiz, speundef, 0x0D, 0x0A, 0x00180000, 0xFFFFFFFF, PPC_SPE_SINGLE); //
9509GEN_SPE(evfststgt, evfststlt, 0x0E, 0x0A, 0x00600000, 0x00600000, PPC_SPE_SINGLE); //
9510GEN_SPE(evfststeq, speundef, 0x0F, 0x0A, 0x00600000, 0xFFFFFFFF, PPC_SPE_SINGLE); //
0487d6a8
JM
9511
9512/* Single precision floating-point operations */
9513/* Arithmetic */
1c97856d
AJ
9514GEN_SPEFPUOP_ARITH2_32_32(efsadd);
9515GEN_SPEFPUOP_ARITH2_32_32(efssub);
9516GEN_SPEFPUOP_ARITH2_32_32(efsmul);
9517GEN_SPEFPUOP_ARITH2_32_32(efsdiv);
636aa200 9518static inline void gen_efsabs(DisasContext *ctx)
1c97856d
AJ
9519{
9520 if (unlikely(!ctx->spe_enabled)) {
27a69bb0 9521 gen_exception(ctx, POWERPC_EXCP_SPEU);
1c97856d
AJ
9522 return;
9523 }
6d5c34fa 9524 tcg_gen_andi_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)], (target_long)~0x80000000LL);
1c97856d 9525}
636aa200 9526static inline void gen_efsnabs(DisasContext *ctx)
1c97856d
AJ
9527{
9528 if (unlikely(!ctx->spe_enabled)) {
27a69bb0 9529 gen_exception(ctx, POWERPC_EXCP_SPEU);
1c97856d
AJ
9530 return;
9531 }
6d5c34fa 9532 tcg_gen_ori_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)], 0x80000000);
1c97856d 9533}
636aa200 9534static inline void gen_efsneg(DisasContext *ctx)
1c97856d
AJ
9535{
9536 if (unlikely(!ctx->spe_enabled)) {
27a69bb0 9537 gen_exception(ctx, POWERPC_EXCP_SPEU);
1c97856d
AJ
9538 return;
9539 }
6d5c34fa 9540 tcg_gen_xori_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)], 0x80000000);
1c97856d
AJ
9541}
9542
0487d6a8 9543/* Conversion */
1c97856d
AJ
9544GEN_SPEFPUOP_CONV_32_32(efscfui);
9545GEN_SPEFPUOP_CONV_32_32(efscfsi);
9546GEN_SPEFPUOP_CONV_32_32(efscfuf);
9547GEN_SPEFPUOP_CONV_32_32(efscfsf);
9548GEN_SPEFPUOP_CONV_32_32(efsctui);
9549GEN_SPEFPUOP_CONV_32_32(efsctsi);
9550GEN_SPEFPUOP_CONV_32_32(efsctuf);
9551GEN_SPEFPUOP_CONV_32_32(efsctsf);
9552GEN_SPEFPUOP_CONV_32_32(efsctuiz);
9553GEN_SPEFPUOP_CONV_32_32(efsctsiz);
9554GEN_SPEFPUOP_CONV_32_64(efscfd);
9555
0487d6a8 9556/* Comparison */
1c97856d
AJ
9557GEN_SPEFPUOP_COMP_32(efscmpgt);
9558GEN_SPEFPUOP_COMP_32(efscmplt);
9559GEN_SPEFPUOP_COMP_32(efscmpeq);
9560GEN_SPEFPUOP_COMP_32(efststgt);
9561GEN_SPEFPUOP_COMP_32(efststlt);
9562GEN_SPEFPUOP_COMP_32(efststeq);
0487d6a8
JM
9563
9564/* Opcodes definitions */
70560da7
FC
9565GEN_SPE(efsadd, efssub, 0x00, 0x0B, 0x00000000, 0x00000000, PPC_SPE_SINGLE); //
9566GEN_SPE(efsabs, efsnabs, 0x02, 0x0B, 0x0000F800, 0x0000F800, PPC_SPE_SINGLE); //
9567GEN_SPE(efsneg, speundef, 0x03, 0x0B, 0x0000F800, 0xFFFFFFFF, PPC_SPE_SINGLE); //
9568GEN_SPE(efsmul, efsdiv, 0x04, 0x0B, 0x00000000, 0x00000000, PPC_SPE_SINGLE); //
9569GEN_SPE(efscmpgt, efscmplt, 0x06, 0x0B, 0x00600000, 0x00600000, PPC_SPE_SINGLE); //
9570GEN_SPE(efscmpeq, efscfd, 0x07, 0x0B, 0x00600000, 0x00180000, PPC_SPE_SINGLE); //
9571GEN_SPE(efscfui, efscfsi, 0x08, 0x0B, 0x00180000, 0x00180000, PPC_SPE_SINGLE); //
9572GEN_SPE(efscfuf, efscfsf, 0x09, 0x0B, 0x00180000, 0x00180000, PPC_SPE_SINGLE); //
9573GEN_SPE(efsctui, efsctsi, 0x0A, 0x0B, 0x00180000, 0x00180000, PPC_SPE_SINGLE); //
9574GEN_SPE(efsctuf, efsctsf, 0x0B, 0x0B, 0x00180000, 0x00180000, PPC_SPE_SINGLE); //
9575GEN_SPE(efsctuiz, speundef, 0x0C, 0x0B, 0x00180000, 0xFFFFFFFF, PPC_SPE_SINGLE); //
9576GEN_SPE(efsctsiz, speundef, 0x0D, 0x0B, 0x00180000, 0xFFFFFFFF, PPC_SPE_SINGLE); //
9577GEN_SPE(efststgt, efststlt, 0x0E, 0x0B, 0x00600000, 0x00600000, PPC_SPE_SINGLE); //
9578GEN_SPE(efststeq, speundef, 0x0F, 0x0B, 0x00600000, 0xFFFFFFFF, PPC_SPE_SINGLE); //
0487d6a8
JM
9579
9580/* Double precision floating-point operations */
9581/* Arithmetic */
1c97856d
AJ
9582GEN_SPEFPUOP_ARITH2_64_64(efdadd);
9583GEN_SPEFPUOP_ARITH2_64_64(efdsub);
9584GEN_SPEFPUOP_ARITH2_64_64(efdmul);
9585GEN_SPEFPUOP_ARITH2_64_64(efddiv);
636aa200 9586static inline void gen_efdabs(DisasContext *ctx)
1c97856d
AJ
9587{
9588 if (unlikely(!ctx->spe_enabled)) {
27a69bb0 9589 gen_exception(ctx, POWERPC_EXCP_SPEU);
1c97856d
AJ
9590 return;
9591 }
9592#if defined(TARGET_PPC64)
6d5c34fa 9593 tcg_gen_andi_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)], ~0x8000000000000000LL);
1c97856d 9594#else
6d5c34fa
MP
9595 tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)]);
9596 tcg_gen_andi_tl(cpu_gprh[rD(ctx->opcode)], cpu_gprh[rA(ctx->opcode)], ~0x80000000);
1c97856d
AJ
9597#endif
9598}
636aa200 9599static inline void gen_efdnabs(DisasContext *ctx)
1c97856d
AJ
9600{
9601 if (unlikely(!ctx->spe_enabled)) {
27a69bb0 9602 gen_exception(ctx, POWERPC_EXCP_SPEU);
1c97856d
AJ
9603 return;
9604 }
9605#if defined(TARGET_PPC64)
6d5c34fa 9606 tcg_gen_ori_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)], 0x8000000000000000LL);
1c97856d 9607#else
6d5c34fa
MP
9608 tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)]);
9609 tcg_gen_ori_tl(cpu_gprh[rD(ctx->opcode)], cpu_gprh[rA(ctx->opcode)], 0x80000000);
1c97856d
AJ
9610#endif
9611}
636aa200 9612static inline void gen_efdneg(DisasContext *ctx)
1c97856d
AJ
9613{
9614 if (unlikely(!ctx->spe_enabled)) {
27a69bb0 9615 gen_exception(ctx, POWERPC_EXCP_SPEU);
1c97856d
AJ
9616 return;
9617 }
9618#if defined(TARGET_PPC64)
6d5c34fa 9619 tcg_gen_xori_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)], 0x8000000000000000LL);
1c97856d 9620#else
6d5c34fa
MP
9621 tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)]);
9622 tcg_gen_xori_tl(cpu_gprh[rD(ctx->opcode)], cpu_gprh[rA(ctx->opcode)], 0x80000000);
1c97856d
AJ
9623#endif
9624}
9625
0487d6a8 9626/* Conversion */
1c97856d
AJ
9627GEN_SPEFPUOP_CONV_64_32(efdcfui);
9628GEN_SPEFPUOP_CONV_64_32(efdcfsi);
9629GEN_SPEFPUOP_CONV_64_32(efdcfuf);
9630GEN_SPEFPUOP_CONV_64_32(efdcfsf);
9631GEN_SPEFPUOP_CONV_32_64(efdctui);
9632GEN_SPEFPUOP_CONV_32_64(efdctsi);
9633GEN_SPEFPUOP_CONV_32_64(efdctuf);
9634GEN_SPEFPUOP_CONV_32_64(efdctsf);
9635GEN_SPEFPUOP_CONV_32_64(efdctuiz);
9636GEN_SPEFPUOP_CONV_32_64(efdctsiz);
9637GEN_SPEFPUOP_CONV_64_32(efdcfs);
9638GEN_SPEFPUOP_CONV_64_64(efdcfuid);
9639GEN_SPEFPUOP_CONV_64_64(efdcfsid);
9640GEN_SPEFPUOP_CONV_64_64(efdctuidz);
9641GEN_SPEFPUOP_CONV_64_64(efdctsidz);
0487d6a8 9642
0487d6a8 9643/* Comparison */
1c97856d
AJ
9644GEN_SPEFPUOP_COMP_64(efdcmpgt);
9645GEN_SPEFPUOP_COMP_64(efdcmplt);
9646GEN_SPEFPUOP_COMP_64(efdcmpeq);
9647GEN_SPEFPUOP_COMP_64(efdtstgt);
9648GEN_SPEFPUOP_COMP_64(efdtstlt);
9649GEN_SPEFPUOP_COMP_64(efdtsteq);
0487d6a8
JM
9650
9651/* Opcodes definitions */
70560da7
FC
9652GEN_SPE(efdadd, efdsub, 0x10, 0x0B, 0x00000000, 0x00000000, PPC_SPE_DOUBLE); //
9653GEN_SPE(efdcfuid, efdcfsid, 0x11, 0x0B, 0x00180000, 0x00180000, PPC_SPE_DOUBLE); //
9654GEN_SPE(efdabs, efdnabs, 0x12, 0x0B, 0x0000F800, 0x0000F800, PPC_SPE_DOUBLE); //
9655GEN_SPE(efdneg, speundef, 0x13, 0x0B, 0x0000F800, 0xFFFFFFFF, PPC_SPE_DOUBLE); //
9656GEN_SPE(efdmul, efddiv, 0x14, 0x0B, 0x00000000, 0x00000000, PPC_SPE_DOUBLE); //
9657GEN_SPE(efdctuidz, efdctsidz, 0x15, 0x0B, 0x00180000, 0x00180000, PPC_SPE_DOUBLE); //
9658GEN_SPE(efdcmpgt, efdcmplt, 0x16, 0x0B, 0x00600000, 0x00600000, PPC_SPE_DOUBLE); //
9659GEN_SPE(efdcmpeq, efdcfs, 0x17, 0x0B, 0x00600000, 0x00180000, PPC_SPE_DOUBLE); //
9660GEN_SPE(efdcfui, efdcfsi, 0x18, 0x0B, 0x00180000, 0x00180000, PPC_SPE_DOUBLE); //
9661GEN_SPE(efdcfuf, efdcfsf, 0x19, 0x0B, 0x00180000, 0x00180000, PPC_SPE_DOUBLE); //
9662GEN_SPE(efdctui, efdctsi, 0x1A, 0x0B, 0x00180000, 0x00180000, PPC_SPE_DOUBLE); //
9663GEN_SPE(efdctuf, efdctsf, 0x1B, 0x0B, 0x00180000, 0x00180000, PPC_SPE_DOUBLE); //
9664GEN_SPE(efdctuiz, speundef, 0x1C, 0x0B, 0x00180000, 0xFFFFFFFF, PPC_SPE_DOUBLE); //
9665GEN_SPE(efdctsiz, speundef, 0x1D, 0x0B, 0x00180000, 0xFFFFFFFF, PPC_SPE_DOUBLE); //
9666GEN_SPE(efdtstgt, efdtstlt, 0x1E, 0x0B, 0x00600000, 0x00600000, PPC_SPE_DOUBLE); //
9667GEN_SPE(efdtsteq, speundef, 0x1F, 0x0B, 0x00600000, 0xFFFFFFFF, PPC_SPE_DOUBLE); //
0487d6a8 9668
c227f099 9669static opcode_t opcodes[] = {
5c55ff99
BS
9670GEN_HANDLER(invalid, 0x00, 0x00, 0x00, 0xFFFFFFFF, PPC_NONE),
9671GEN_HANDLER(cmp, 0x1F, 0x00, 0x00, 0x00400000, PPC_INTEGER),
9672GEN_HANDLER(cmpi, 0x0B, 0xFF, 0xFF, 0x00400000, PPC_INTEGER),
9673GEN_HANDLER(cmpl, 0x1F, 0x00, 0x01, 0x00400000, PPC_INTEGER),
9674GEN_HANDLER(cmpli, 0x0A, 0xFF, 0xFF, 0x00400000, PPC_INTEGER),
fcfda20f 9675GEN_HANDLER_E(cmpb, 0x1F, 0x1C, 0x0F, 0x00000001, PPC_NONE, PPC2_ISA205),
5c55ff99
BS
9676GEN_HANDLER(isel, 0x1F, 0x0F, 0xFF, 0x00000001, PPC_ISEL),
9677GEN_HANDLER(addi, 0x0E, 0xFF, 0xFF, 0x00000000, PPC_INTEGER),
9678GEN_HANDLER(addic, 0x0C, 0xFF, 0xFF, 0x00000000, PPC_INTEGER),
9679GEN_HANDLER2(addic_, "addic.", 0x0D, 0xFF, 0xFF, 0x00000000, PPC_INTEGER),
9680GEN_HANDLER(addis, 0x0F, 0xFF, 0xFF, 0x00000000, PPC_INTEGER),
9681GEN_HANDLER(mulhw, 0x1F, 0x0B, 0x02, 0x00000400, PPC_INTEGER),
9682GEN_HANDLER(mulhwu, 0x1F, 0x0B, 0x00, 0x00000400, PPC_INTEGER),
9683GEN_HANDLER(mullw, 0x1F, 0x0B, 0x07, 0x00000000, PPC_INTEGER),
9684GEN_HANDLER(mullwo, 0x1F, 0x0B, 0x17, 0x00000000, PPC_INTEGER),
9685GEN_HANDLER(mulli, 0x07, 0xFF, 0xFF, 0x00000000, PPC_INTEGER),
9686#if defined(TARGET_PPC64)
9687GEN_HANDLER(mulld, 0x1F, 0x09, 0x07, 0x00000000, PPC_64B),
9688#endif
9689GEN_HANDLER(neg, 0x1F, 0x08, 0x03, 0x0000F800, PPC_INTEGER),
9690GEN_HANDLER(nego, 0x1F, 0x08, 0x13, 0x0000F800, PPC_INTEGER),
9691GEN_HANDLER(subfic, 0x08, 0xFF, 0xFF, 0x00000000, PPC_INTEGER),
9692GEN_HANDLER2(andi_, "andi.", 0x1C, 0xFF, 0xFF, 0x00000000, PPC_INTEGER),
9693GEN_HANDLER2(andis_, "andis.", 0x1D, 0xFF, 0xFF, 0x00000000, PPC_INTEGER),
9694GEN_HANDLER(cntlzw, 0x1F, 0x1A, 0x00, 0x00000000, PPC_INTEGER),
9695GEN_HANDLER(or, 0x1F, 0x1C, 0x0D, 0x00000000, PPC_INTEGER),
9696GEN_HANDLER(xor, 0x1F, 0x1C, 0x09, 0x00000000, PPC_INTEGER),
9697GEN_HANDLER(ori, 0x18, 0xFF, 0xFF, 0x00000000, PPC_INTEGER),
9698GEN_HANDLER(oris, 0x19, 0xFF, 0xFF, 0x00000000, PPC_INTEGER),
9699GEN_HANDLER(xori, 0x1A, 0xFF, 0xFF, 0x00000000, PPC_INTEGER),
9700GEN_HANDLER(xoris, 0x1B, 0xFF, 0xFF, 0x00000000, PPC_INTEGER),
9701GEN_HANDLER(popcntb, 0x1F, 0x03, 0x03, 0x0000F801, PPC_POPCNTB),
eaabeef2 9702GEN_HANDLER(popcntw, 0x1F, 0x1A, 0x0b, 0x0000F801, PPC_POPCNTWD),
725bcec2 9703GEN_HANDLER_E(prtyw, 0x1F, 0x1A, 0x04, 0x0000F801, PPC_NONE, PPC2_ISA205),
5c55ff99 9704#if defined(TARGET_PPC64)
eaabeef2 9705GEN_HANDLER(popcntd, 0x1F, 0x1A, 0x0F, 0x0000F801, PPC_POPCNTWD),
5c55ff99 9706GEN_HANDLER(cntlzd, 0x1F, 0x1A, 0x01, 0x00000000, PPC_64B),
725bcec2 9707GEN_HANDLER_E(prtyd, 0x1F, 0x1A, 0x05, 0x0000F801, PPC_NONE, PPC2_ISA205),
86ba37ed 9708GEN_HANDLER_E(bpermd, 0x1F, 0x1C, 0x07, 0x00000001, PPC_NONE, PPC2_PERM_ISA206),
5c55ff99
BS
9709#endif
9710GEN_HANDLER(rlwimi, 0x14, 0xFF, 0xFF, 0x00000000, PPC_INTEGER),
9711GEN_HANDLER(rlwinm, 0x15, 0xFF, 0xFF, 0x00000000, PPC_INTEGER),
9712GEN_HANDLER(rlwnm, 0x17, 0xFF, 0xFF, 0x00000000, PPC_INTEGER),
9713GEN_HANDLER(slw, 0x1F, 0x18, 0x00, 0x00000000, PPC_INTEGER),
9714GEN_HANDLER(sraw, 0x1F, 0x18, 0x18, 0x00000000, PPC_INTEGER),
9715GEN_HANDLER(srawi, 0x1F, 0x18, 0x19, 0x00000000, PPC_INTEGER),
9716GEN_HANDLER(srw, 0x1F, 0x18, 0x10, 0x00000000, PPC_INTEGER),
9717#if defined(TARGET_PPC64)
9718GEN_HANDLER(sld, 0x1F, 0x1B, 0x00, 0x00000000, PPC_64B),
9719GEN_HANDLER(srad, 0x1F, 0x1A, 0x18, 0x00000000, PPC_64B),
9720GEN_HANDLER2(sradi0, "sradi", 0x1F, 0x1A, 0x19, 0x00000000, PPC_64B),
9721GEN_HANDLER2(sradi1, "sradi", 0x1F, 0x1B, 0x19, 0x00000000, PPC_64B),
9722GEN_HANDLER(srd, 0x1F, 0x1B, 0x10, 0x00000000, PPC_64B),
9723#endif
9724GEN_HANDLER(frsqrtes, 0x3B, 0x1A, 0xFF, 0x001F07C0, PPC_FLOAT_FRSQRTES),
9725GEN_HANDLER(fsqrt, 0x3F, 0x16, 0xFF, 0x001F07C0, PPC_FLOAT_FSQRT),
9726GEN_HANDLER(fsqrts, 0x3B, 0x16, 0xFF, 0x001F07C0, PPC_FLOAT_FSQRT),
9727GEN_HANDLER(fcmpo, 0x3F, 0x00, 0x01, 0x00600001, PPC_FLOAT),
9728GEN_HANDLER(fcmpu, 0x3F, 0x00, 0x00, 0x00600001, PPC_FLOAT),
bf45a2e6 9729GEN_HANDLER(fabs, 0x3F, 0x08, 0x08, 0x001F0000, PPC_FLOAT),
5c55ff99 9730GEN_HANDLER(fmr, 0x3F, 0x08, 0x02, 0x001F0000, PPC_FLOAT),
bf45a2e6
AJ
9731GEN_HANDLER(fnabs, 0x3F, 0x08, 0x04, 0x001F0000, PPC_FLOAT),
9732GEN_HANDLER(fneg, 0x3F, 0x08, 0x01, 0x001F0000, PPC_FLOAT),
f0332888 9733GEN_HANDLER_E(fcpsgn, 0x3F, 0x08, 0x00, 0x00000000, PPC_NONE, PPC2_ISA205),
097ec5d8
TM
9734GEN_HANDLER_E(fmrgew, 0x3F, 0x06, 0x1E, 0x00000001, PPC_NONE, PPC2_VSX207),
9735GEN_HANDLER_E(fmrgow, 0x3F, 0x06, 0x1A, 0x00000001, PPC_NONE, PPC2_VSX207),
5c55ff99
BS
9736GEN_HANDLER(mcrfs, 0x3F, 0x00, 0x02, 0x0063F801, PPC_FLOAT),
9737GEN_HANDLER(mffs, 0x3F, 0x07, 0x12, 0x001FF800, PPC_FLOAT),
9738GEN_HANDLER(mtfsb0, 0x3F, 0x06, 0x02, 0x001FF800, PPC_FLOAT),
9739GEN_HANDLER(mtfsb1, 0x3F, 0x06, 0x01, 0x001FF800, PPC_FLOAT),
7d08d856
AJ
9740GEN_HANDLER(mtfsf, 0x3F, 0x07, 0x16, 0x00000000, PPC_FLOAT),
9741GEN_HANDLER(mtfsfi, 0x3F, 0x06, 0x04, 0x006e0800, PPC_FLOAT),
5c55ff99
BS
9742#if defined(TARGET_PPC64)
9743GEN_HANDLER(ld, 0x3A, 0xFF, 0xFF, 0x00000000, PPC_64B),
9744GEN_HANDLER(lq, 0x38, 0xFF, 0xFF, 0x00000000, PPC_64BX),
9745GEN_HANDLER(std, 0x3E, 0xFF, 0xFF, 0x00000000, PPC_64B),
9746#endif
9747GEN_HANDLER(lmw, 0x2E, 0xFF, 0xFF, 0x00000000, PPC_INTEGER),
9748GEN_HANDLER(stmw, 0x2F, 0xFF, 0xFF, 0x00000000, PPC_INTEGER),
9749GEN_HANDLER(lswi, 0x1F, 0x15, 0x12, 0x00000001, PPC_STRING),
9750GEN_HANDLER(lswx, 0x1F, 0x15, 0x10, 0x00000001, PPC_STRING),
9751GEN_HANDLER(stswi, 0x1F, 0x15, 0x16, 0x00000001, PPC_STRING),
9752GEN_HANDLER(stswx, 0x1F, 0x15, 0x14, 0x00000001, PPC_STRING),
9753GEN_HANDLER(eieio, 0x1F, 0x16, 0x1A, 0x03FFF801, PPC_MEM_EIEIO),
9754GEN_HANDLER(isync, 0x13, 0x16, 0x04, 0x03FFF801, PPC_MEM),
5c77a786
TM
9755GEN_HANDLER_E(lbarx, 0x1F, 0x14, 0x01, 0, PPC_NONE, PPC2_ATOMIC_ISA206),
9756GEN_HANDLER_E(lharx, 0x1F, 0x14, 0x03, 0, PPC_NONE, PPC2_ATOMIC_ISA206),
f844c817 9757GEN_HANDLER(lwarx, 0x1F, 0x14, 0x00, 0x00000000, PPC_RES),
587c51f7
TM
9758GEN_HANDLER_E(stbcx_, 0x1F, 0x16, 0x15, 0, PPC_NONE, PPC2_ATOMIC_ISA206),
9759GEN_HANDLER_E(sthcx_, 0x1F, 0x16, 0x16, 0, PPC_NONE, PPC2_ATOMIC_ISA206),
5c55ff99
BS
9760GEN_HANDLER2(stwcx_, "stwcx.", 0x1F, 0x16, 0x04, 0x00000000, PPC_RES),
9761#if defined(TARGET_PPC64)
f844c817 9762GEN_HANDLER(ldarx, 0x1F, 0x14, 0x02, 0x00000000, PPC_64B),
9c294d5a 9763GEN_HANDLER_E(lqarx, 0x1F, 0x14, 0x08, 0, PPC_NONE, PPC2_LSQ_ISA207),
5c55ff99 9764GEN_HANDLER2(stdcx_, "stdcx.", 0x1F, 0x16, 0x06, 0x00000000, PPC_64B),
27b95bfe 9765GEN_HANDLER_E(stqcx_, 0x1F, 0x16, 0x05, 0, PPC_NONE, PPC2_LSQ_ISA207),
5c55ff99
BS
9766#endif
9767GEN_HANDLER(sync, 0x1F, 0x16, 0x12, 0x039FF801, PPC_MEM_SYNC),
9768GEN_HANDLER(wait, 0x1F, 0x1E, 0x01, 0x03FFF801, PPC_WAIT),
9769GEN_HANDLER(b, 0x12, 0xFF, 0xFF, 0x00000000, PPC_FLOW),
9770GEN_HANDLER(bc, 0x10, 0xFF, 0xFF, 0x00000000, PPC_FLOW),
9771GEN_HANDLER(bcctr, 0x13, 0x10, 0x10, 0x00000000, PPC_FLOW),
9772GEN_HANDLER(bclr, 0x13, 0x10, 0x00, 0x00000000, PPC_FLOW),
52a4984d 9773GEN_HANDLER_E(bctar, 0x13, 0x10, 0x11, 0, PPC_NONE, PPC2_BCTAR_ISA207),
5c55ff99
BS
9774GEN_HANDLER(mcrf, 0x13, 0x00, 0xFF, 0x00000001, PPC_INTEGER),
9775GEN_HANDLER(rfi, 0x13, 0x12, 0x01, 0x03FF8001, PPC_FLOW),
9776#if defined(TARGET_PPC64)
9777GEN_HANDLER(rfid, 0x13, 0x12, 0x00, 0x03FF8001, PPC_64B),
9778GEN_HANDLER(hrfid, 0x13, 0x12, 0x08, 0x03FF8001, PPC_64H),
9779#endif
9780GEN_HANDLER(sc, 0x11, 0xFF, 0xFF, 0x03FFF01D, PPC_FLOW),
9781GEN_HANDLER(tw, 0x1F, 0x04, 0x00, 0x00000001, PPC_FLOW),
9782GEN_HANDLER(twi, 0x03, 0xFF, 0xFF, 0x00000000, PPC_FLOW),
9783#if defined(TARGET_PPC64)
9784GEN_HANDLER(td, 0x1F, 0x04, 0x02, 0x00000001, PPC_64B),
9785GEN_HANDLER(tdi, 0x02, 0xFF, 0xFF, 0x00000000, PPC_64B),
9786#endif
9787GEN_HANDLER(mcrxr, 0x1F, 0x00, 0x10, 0x007FF801, PPC_MISC),
9788GEN_HANDLER(mfcr, 0x1F, 0x13, 0x00, 0x00000801, PPC_MISC),
9789GEN_HANDLER(mfmsr, 0x1F, 0x13, 0x02, 0x001FF801, PPC_MISC),
9790GEN_HANDLER(mfspr, 0x1F, 0x13, 0x0A, 0x00000001, PPC_MISC),
9791GEN_HANDLER(mftb, 0x1F, 0x13, 0x0B, 0x00000001, PPC_MFTB),
9792GEN_HANDLER(mtcrf, 0x1F, 0x10, 0x04, 0x00000801, PPC_MISC),
9793#if defined(TARGET_PPC64)
9794GEN_HANDLER(mtmsrd, 0x1F, 0x12, 0x05, 0x001EF801, PPC_64B),
9795#endif
9796GEN_HANDLER(mtmsr, 0x1F, 0x12, 0x04, 0x001FF801, PPC_MISC),
9797GEN_HANDLER(mtspr, 0x1F, 0x13, 0x0E, 0x00000001, PPC_MISC),
9798GEN_HANDLER(dcbf, 0x1F, 0x16, 0x02, 0x03C00001, PPC_CACHE),
9799GEN_HANDLER(dcbi, 0x1F, 0x16, 0x0E, 0x03E00001, PPC_CACHE),
9800GEN_HANDLER(dcbst, 0x1F, 0x16, 0x01, 0x03E00001, PPC_CACHE),
3f34cf91
CLG
9801GEN_HANDLER(dcbt, 0x1F, 0x16, 0x08, 0x00000001, PPC_CACHE),
9802GEN_HANDLER(dcbtst, 0x1F, 0x16, 0x07, 0x00000001, PPC_CACHE),
8e33944f 9803GEN_HANDLER(dcbz, 0x1F, 0x16, 0x1F, 0x03C00001, PPC_CACHE_DCBZ),
5c55ff99
BS
9804GEN_HANDLER(dst, 0x1F, 0x16, 0x0A, 0x01800001, PPC_ALTIVEC),
9805GEN_HANDLER(dstst, 0x1F, 0x16, 0x0B, 0x02000001, PPC_ALTIVEC),
9806GEN_HANDLER(dss, 0x1F, 0x16, 0x19, 0x019FF801, PPC_ALTIVEC),
9807GEN_HANDLER(icbi, 0x1F, 0x16, 0x1E, 0x03E00001, PPC_CACHE_ICBI),
9808GEN_HANDLER(dcba, 0x1F, 0x16, 0x17, 0x03E00001, PPC_CACHE_DCBA),
9809GEN_HANDLER(mfsr, 0x1F, 0x13, 0x12, 0x0010F801, PPC_SEGMENT),
9810GEN_HANDLER(mfsrin, 0x1F, 0x13, 0x14, 0x001F0001, PPC_SEGMENT),
9811GEN_HANDLER(mtsr, 0x1F, 0x12, 0x06, 0x0010F801, PPC_SEGMENT),
9812GEN_HANDLER(mtsrin, 0x1F, 0x12, 0x07, 0x001F0001, PPC_SEGMENT),
9813#if defined(TARGET_PPC64)
9814GEN_HANDLER2(mfsr_64b, "mfsr", 0x1F, 0x13, 0x12, 0x0010F801, PPC_SEGMENT_64B),
9815GEN_HANDLER2(mfsrin_64b, "mfsrin", 0x1F, 0x13, 0x14, 0x001F0001,
9816 PPC_SEGMENT_64B),
9817GEN_HANDLER2(mtsr_64b, "mtsr", 0x1F, 0x12, 0x06, 0x0010F801, PPC_SEGMENT_64B),
9818GEN_HANDLER2(mtsrin_64b, "mtsrin", 0x1F, 0x12, 0x07, 0x001F0001,
9819 PPC_SEGMENT_64B),
efdef95f
DG
9820GEN_HANDLER2(slbmte, "slbmte", 0x1F, 0x12, 0x0C, 0x001F0001, PPC_SEGMENT_64B),
9821GEN_HANDLER2(slbmfee, "slbmfee", 0x1F, 0x13, 0x1C, 0x001F0001, PPC_SEGMENT_64B),
9822GEN_HANDLER2(slbmfev, "slbmfev", 0x1F, 0x13, 0x1A, 0x001F0001, PPC_SEGMENT_64B),
5c55ff99
BS
9823#endif
9824GEN_HANDLER(tlbia, 0x1F, 0x12, 0x0B, 0x03FFFC01, PPC_MEM_TLBIA),
9825GEN_HANDLER(tlbiel, 0x1F, 0x12, 0x08, 0x03FF0001, PPC_MEM_TLBIE),
9826GEN_HANDLER(tlbie, 0x1F, 0x12, 0x09, 0x03FF0001, PPC_MEM_TLBIE),
9827GEN_HANDLER(tlbsync, 0x1F, 0x16, 0x11, 0x03FFF801, PPC_MEM_TLBSYNC),
9828#if defined(TARGET_PPC64)
9829GEN_HANDLER(slbia, 0x1F, 0x12, 0x0F, 0x03FFFC01, PPC_SLBI),
9830GEN_HANDLER(slbie, 0x1F, 0x12, 0x0D, 0x03FF0001, PPC_SLBI),
9831#endif
9832GEN_HANDLER(eciwx, 0x1F, 0x16, 0x0D, 0x00000001, PPC_EXTERN),
9833GEN_HANDLER(ecowx, 0x1F, 0x16, 0x09, 0x00000001, PPC_EXTERN),
9834GEN_HANDLER(abs, 0x1F, 0x08, 0x0B, 0x0000F800, PPC_POWER_BR),
9835GEN_HANDLER(abso, 0x1F, 0x08, 0x1B, 0x0000F800, PPC_POWER_BR),
9836GEN_HANDLER(clcs, 0x1F, 0x10, 0x13, 0x0000F800, PPC_POWER_BR),
9837GEN_HANDLER(div, 0x1F, 0x0B, 0x0A, 0x00000000, PPC_POWER_BR),
9838GEN_HANDLER(divo, 0x1F, 0x0B, 0x1A, 0x00000000, PPC_POWER_BR),
9839GEN_HANDLER(divs, 0x1F, 0x0B, 0x0B, 0x00000000, PPC_POWER_BR),
9840GEN_HANDLER(divso, 0x1F, 0x0B, 0x1B, 0x00000000, PPC_POWER_BR),
9841GEN_HANDLER(doz, 0x1F, 0x08, 0x08, 0x00000000, PPC_POWER_BR),
9842GEN_HANDLER(dozo, 0x1F, 0x08, 0x18, 0x00000000, PPC_POWER_BR),
9843GEN_HANDLER(dozi, 0x09, 0xFF, 0xFF, 0x00000000, PPC_POWER_BR),
9844GEN_HANDLER(lscbx, 0x1F, 0x15, 0x08, 0x00000000, PPC_POWER_BR),
9845GEN_HANDLER(maskg, 0x1F, 0x1D, 0x00, 0x00000000, PPC_POWER_BR),
9846GEN_HANDLER(maskir, 0x1F, 0x1D, 0x10, 0x00000000, PPC_POWER_BR),
9847GEN_HANDLER(mul, 0x1F, 0x0B, 0x03, 0x00000000, PPC_POWER_BR),
9848GEN_HANDLER(mulo, 0x1F, 0x0B, 0x13, 0x00000000, PPC_POWER_BR),
9849GEN_HANDLER(nabs, 0x1F, 0x08, 0x0F, 0x00000000, PPC_POWER_BR),
9850GEN_HANDLER(nabso, 0x1F, 0x08, 0x1F, 0x00000000, PPC_POWER_BR),
9851GEN_HANDLER(rlmi, 0x16, 0xFF, 0xFF, 0x00000000, PPC_POWER_BR),
9852GEN_HANDLER(rrib, 0x1F, 0x19, 0x10, 0x00000000, PPC_POWER_BR),
9853GEN_HANDLER(sle, 0x1F, 0x19, 0x04, 0x00000000, PPC_POWER_BR),
9854GEN_HANDLER(sleq, 0x1F, 0x19, 0x06, 0x00000000, PPC_POWER_BR),
9855GEN_HANDLER(sliq, 0x1F, 0x18, 0x05, 0x00000000, PPC_POWER_BR),
9856GEN_HANDLER(slliq, 0x1F, 0x18, 0x07, 0x00000000, PPC_POWER_BR),
9857GEN_HANDLER(sllq, 0x1F, 0x18, 0x06, 0x00000000, PPC_POWER_BR),
9858GEN_HANDLER(slq, 0x1F, 0x18, 0x04, 0x00000000, PPC_POWER_BR),
9859GEN_HANDLER(sraiq, 0x1F, 0x18, 0x1D, 0x00000000, PPC_POWER_BR),
9860GEN_HANDLER(sraq, 0x1F, 0x18, 0x1C, 0x00000000, PPC_POWER_BR),
9861GEN_HANDLER(sre, 0x1F, 0x19, 0x14, 0x00000000, PPC_POWER_BR),
9862GEN_HANDLER(srea, 0x1F, 0x19, 0x1C, 0x00000000, PPC_POWER_BR),
9863GEN_HANDLER(sreq, 0x1F, 0x19, 0x16, 0x00000000, PPC_POWER_BR),
9864GEN_HANDLER(sriq, 0x1F, 0x18, 0x15, 0x00000000, PPC_POWER_BR),
9865GEN_HANDLER(srliq, 0x1F, 0x18, 0x17, 0x00000000, PPC_POWER_BR),
9866GEN_HANDLER(srlq, 0x1F, 0x18, 0x16, 0x00000000, PPC_POWER_BR),
9867GEN_HANDLER(srq, 0x1F, 0x18, 0x14, 0x00000000, PPC_POWER_BR),
9868GEN_HANDLER(dsa, 0x1F, 0x14, 0x13, 0x03FFF801, PPC_602_SPEC),
9869GEN_HANDLER(esa, 0x1F, 0x14, 0x12, 0x03FFF801, PPC_602_SPEC),
9870GEN_HANDLER(mfrom, 0x1F, 0x09, 0x08, 0x03E0F801, PPC_602_SPEC),
9871GEN_HANDLER2(tlbld_6xx, "tlbld", 0x1F, 0x12, 0x1E, 0x03FF0001, PPC_6xx_TLB),
9872GEN_HANDLER2(tlbli_6xx, "tlbli", 0x1F, 0x12, 0x1F, 0x03FF0001, PPC_6xx_TLB),
9873GEN_HANDLER2(tlbld_74xx, "tlbld", 0x1F, 0x12, 0x1E, 0x03FF0001, PPC_74xx_TLB),
9874GEN_HANDLER2(tlbli_74xx, "tlbli", 0x1F, 0x12, 0x1F, 0x03FF0001, PPC_74xx_TLB),
9875GEN_HANDLER(clf, 0x1F, 0x16, 0x03, 0x03E00000, PPC_POWER),
9876GEN_HANDLER(cli, 0x1F, 0x16, 0x0F, 0x03E00000, PPC_POWER),
9877GEN_HANDLER(dclst, 0x1F, 0x16, 0x13, 0x03E00000, PPC_POWER),
9878GEN_HANDLER(mfsri, 0x1F, 0x13, 0x13, 0x00000001, PPC_POWER),
9879GEN_HANDLER(rac, 0x1F, 0x12, 0x19, 0x00000001, PPC_POWER),
9880GEN_HANDLER(rfsvc, 0x13, 0x12, 0x02, 0x03FFF0001, PPC_POWER),
9881GEN_HANDLER(lfq, 0x38, 0xFF, 0xFF, 0x00000003, PPC_POWER2),
9882GEN_HANDLER(lfqu, 0x39, 0xFF, 0xFF, 0x00000003, PPC_POWER2),
9883GEN_HANDLER(lfqux, 0x1F, 0x17, 0x19, 0x00000001, PPC_POWER2),
9884GEN_HANDLER(lfqx, 0x1F, 0x17, 0x18, 0x00000001, PPC_POWER2),
9885GEN_HANDLER(stfq, 0x3C, 0xFF, 0xFF, 0x00000003, PPC_POWER2),
9886GEN_HANDLER(stfqu, 0x3D, 0xFF, 0xFF, 0x00000003, PPC_POWER2),
9887GEN_HANDLER(stfqux, 0x1F, 0x17, 0x1D, 0x00000001, PPC_POWER2),
9888GEN_HANDLER(stfqx, 0x1F, 0x17, 0x1C, 0x00000001, PPC_POWER2),
9889GEN_HANDLER(mfapidi, 0x1F, 0x13, 0x08, 0x0000F801, PPC_MFAPIDI),
9890GEN_HANDLER(tlbiva, 0x1F, 0x12, 0x18, 0x03FFF801, PPC_TLBIVA),
9891GEN_HANDLER(mfdcr, 0x1F, 0x03, 0x0A, 0x00000001, PPC_DCR),
9892GEN_HANDLER(mtdcr, 0x1F, 0x03, 0x0E, 0x00000001, PPC_DCR),
9893GEN_HANDLER(mfdcrx, 0x1F, 0x03, 0x08, 0x00000000, PPC_DCRX),
9894GEN_HANDLER(mtdcrx, 0x1F, 0x03, 0x0C, 0x00000000, PPC_DCRX),
9895GEN_HANDLER(mfdcrux, 0x1F, 0x03, 0x09, 0x00000000, PPC_DCRUX),
9896GEN_HANDLER(mtdcrux, 0x1F, 0x03, 0x0D, 0x00000000, PPC_DCRUX),
9897GEN_HANDLER(dccci, 0x1F, 0x06, 0x0E, 0x03E00001, PPC_4xx_COMMON),
9898GEN_HANDLER(dcread, 0x1F, 0x06, 0x0F, 0x00000001, PPC_4xx_COMMON),
9899GEN_HANDLER2(icbt_40x, "icbt", 0x1F, 0x06, 0x08, 0x03E00001, PPC_40x_ICBT),
9900GEN_HANDLER(iccci, 0x1F, 0x06, 0x1E, 0x00000001, PPC_4xx_COMMON),
9901GEN_HANDLER(icread, 0x1F, 0x06, 0x1F, 0x03E00001, PPC_4xx_COMMON),
9902GEN_HANDLER2(rfci_40x, "rfci", 0x13, 0x13, 0x01, 0x03FF8001, PPC_40x_EXCP),
01662f3e 9903GEN_HANDLER_E(rfci, 0x13, 0x13, 0x01, 0x03FF8001, PPC_BOOKE, PPC2_BOOKE206),
5c55ff99
BS
9904GEN_HANDLER(rfdi, 0x13, 0x07, 0x01, 0x03FF8001, PPC_RFDI),
9905GEN_HANDLER(rfmci, 0x13, 0x06, 0x01, 0x03FF8001, PPC_RFMCI),
9906GEN_HANDLER2(tlbre_40x, "tlbre", 0x1F, 0x12, 0x1D, 0x00000001, PPC_40x_TLB),
9907GEN_HANDLER2(tlbsx_40x, "tlbsx", 0x1F, 0x12, 0x1C, 0x00000000, PPC_40x_TLB),
9908GEN_HANDLER2(tlbwe_40x, "tlbwe", 0x1F, 0x12, 0x1E, 0x00000001, PPC_40x_TLB),
9909GEN_HANDLER2(tlbre_440, "tlbre", 0x1F, 0x12, 0x1D, 0x00000001, PPC_BOOKE),
9910GEN_HANDLER2(tlbsx_440, "tlbsx", 0x1F, 0x12, 0x1C, 0x00000000, PPC_BOOKE),
9911GEN_HANDLER2(tlbwe_440, "tlbwe", 0x1F, 0x12, 0x1E, 0x00000001, PPC_BOOKE),
01662f3e
AG
9912GEN_HANDLER2_E(tlbre_booke206, "tlbre", 0x1F, 0x12, 0x1D, 0x00000001,
9913 PPC_NONE, PPC2_BOOKE206),
9914GEN_HANDLER2_E(tlbsx_booke206, "tlbsx", 0x1F, 0x12, 0x1C, 0x00000000,
9915 PPC_NONE, PPC2_BOOKE206),
9916GEN_HANDLER2_E(tlbwe_booke206, "tlbwe", 0x1F, 0x12, 0x1E, 0x00000001,
9917 PPC_NONE, PPC2_BOOKE206),
9918GEN_HANDLER2_E(tlbivax_booke206, "tlbivax", 0x1F, 0x12, 0x18, 0x00000001,
9919 PPC_NONE, PPC2_BOOKE206),
6d3db821
AG
9920GEN_HANDLER2_E(tlbilx_booke206, "tlbilx", 0x1F, 0x12, 0x00, 0x03800001,
9921 PPC_NONE, PPC2_BOOKE206),
d5d11a39
AG
9922GEN_HANDLER2_E(msgsnd, "msgsnd", 0x1F, 0x0E, 0x06, 0x03ff0001,
9923 PPC_NONE, PPC2_PRCNTL),
9e0b5cb1
AG
9924GEN_HANDLER2_E(msgclr, "msgclr", 0x1F, 0x0E, 0x07, 0x03ff0001,
9925 PPC_NONE, PPC2_PRCNTL),
5c55ff99 9926GEN_HANDLER(wrtee, 0x1F, 0x03, 0x04, 0x000FFC01, PPC_WRTEE),
fbe73008 9927GEN_HANDLER(wrteei, 0x1F, 0x03, 0x05, 0x000E7C01, PPC_WRTEE),
5c55ff99 9928GEN_HANDLER(dlmzb, 0x1F, 0x0E, 0x02, 0x00000000, PPC_440_SPEC),
01662f3e
AG
9929GEN_HANDLER_E(mbar, 0x1F, 0x16, 0x1a, 0x001FF801,
9930 PPC_BOOKE, PPC2_BOOKE206),
dcb2b9e1 9931GEN_HANDLER(msync_4xx, 0x1F, 0x16, 0x12, 0x03FFF801, PPC_BOOKE),
01662f3e
AG
9932GEN_HANDLER2_E(icbt_440, "icbt", 0x1F, 0x16, 0x00, 0x03E00001,
9933 PPC_BOOKE, PPC2_BOOKE206),
5c55ff99
BS
9934GEN_HANDLER(lvsl, 0x1f, 0x06, 0x00, 0x00000001, PPC_ALTIVEC),
9935GEN_HANDLER(lvsr, 0x1f, 0x06, 0x01, 0x00000001, PPC_ALTIVEC),
9936GEN_HANDLER(mfvscr, 0x04, 0x2, 0x18, 0x001ff800, PPC_ALTIVEC),
9937GEN_HANDLER(mtvscr, 0x04, 0x2, 0x19, 0x03ff0000, PPC_ALTIVEC),
9938GEN_HANDLER(vsldoi, 0x04, 0x16, 0xFF, 0x00000400, PPC_ALTIVEC),
9939GEN_HANDLER(vmladduhm, 0x04, 0x11, 0xFF, 0x00000000, PPC_ALTIVEC),
9940GEN_HANDLER2(evsel0, "evsel", 0x04, 0x1c, 0x09, 0x00000000, PPC_SPE),
9941GEN_HANDLER2(evsel1, "evsel", 0x04, 0x1d, 0x09, 0x00000000, PPC_SPE),
9942GEN_HANDLER2(evsel2, "evsel", 0x04, 0x1e, 0x09, 0x00000000, PPC_SPE),
9943GEN_HANDLER2(evsel3, "evsel", 0x04, 0x1f, 0x09, 0x00000000, PPC_SPE),
9944
9945#undef GEN_INT_ARITH_ADD
9946#undef GEN_INT_ARITH_ADD_CONST
9947#define GEN_INT_ARITH_ADD(name, opc3, add_ca, compute_ca, compute_ov) \
9948GEN_HANDLER(name, 0x1F, 0x0A, opc3, 0x00000000, PPC_INTEGER),
9949#define GEN_INT_ARITH_ADD_CONST(name, opc3, const_val, \
9950 add_ca, compute_ca, compute_ov) \
9951GEN_HANDLER(name, 0x1F, 0x0A, opc3, 0x0000F800, PPC_INTEGER),
9952GEN_INT_ARITH_ADD(add, 0x08, 0, 0, 0)
9953GEN_INT_ARITH_ADD(addo, 0x18, 0, 0, 1)
9954GEN_INT_ARITH_ADD(addc, 0x00, 0, 1, 0)
9955GEN_INT_ARITH_ADD(addco, 0x10, 0, 1, 1)
9956GEN_INT_ARITH_ADD(adde, 0x04, 1, 1, 0)
9957GEN_INT_ARITH_ADD(addeo, 0x14, 1, 1, 1)
9958GEN_INT_ARITH_ADD_CONST(addme, 0x07, -1LL, 1, 1, 0)
9959GEN_INT_ARITH_ADD_CONST(addmeo, 0x17, -1LL, 1, 1, 1)
9960GEN_INT_ARITH_ADD_CONST(addze, 0x06, 0, 1, 1, 0)
9961GEN_INT_ARITH_ADD_CONST(addzeo, 0x16, 0, 1, 1, 1)
9962
9963#undef GEN_INT_ARITH_DIVW
9964#define GEN_INT_ARITH_DIVW(name, opc3, sign, compute_ov) \
9965GEN_HANDLER(name, 0x1F, 0x0B, opc3, 0x00000000, PPC_INTEGER)
9966GEN_INT_ARITH_DIVW(divwu, 0x0E, 0, 0),
9967GEN_INT_ARITH_DIVW(divwuo, 0x1E, 0, 1),
9968GEN_INT_ARITH_DIVW(divw, 0x0F, 1, 0),
9969GEN_INT_ARITH_DIVW(divwo, 0x1F, 1, 1),
a98eb9e9
TM
9970GEN_HANDLER_E(divwe, 0x1F, 0x0B, 0x0D, 0, PPC_NONE, PPC2_DIVE_ISA206),
9971GEN_HANDLER_E(divweo, 0x1F, 0x0B, 0x1D, 0, PPC_NONE, PPC2_DIVE_ISA206),
6a4fda33
TM
9972GEN_HANDLER_E(divweu, 0x1F, 0x0B, 0x0C, 0, PPC_NONE, PPC2_DIVE_ISA206),
9973GEN_HANDLER_E(divweuo, 0x1F, 0x0B, 0x1C, 0, PPC_NONE, PPC2_DIVE_ISA206),
5c55ff99
BS
9974
9975#if defined(TARGET_PPC64)
9976#undef GEN_INT_ARITH_DIVD
9977#define GEN_INT_ARITH_DIVD(name, opc3, sign, compute_ov) \
9978GEN_HANDLER(name, 0x1F, 0x09, opc3, 0x00000000, PPC_64B)
9979GEN_INT_ARITH_DIVD(divdu, 0x0E, 0, 0),
9980GEN_INT_ARITH_DIVD(divduo, 0x1E, 0, 1),
9981GEN_INT_ARITH_DIVD(divd, 0x0F, 1, 0),
9982GEN_INT_ARITH_DIVD(divdo, 0x1F, 1, 1),
9983
98d1eb27
TM
9984GEN_HANDLER_E(divdeu, 0x1F, 0x09, 0x0C, 0, PPC_NONE, PPC2_DIVE_ISA206),
9985GEN_HANDLER_E(divdeuo, 0x1F, 0x09, 0x1C, 0, PPC_NONE, PPC2_DIVE_ISA206),
e44259b6
TM
9986GEN_HANDLER_E(divde, 0x1F, 0x09, 0x0D, 0, PPC_NONE, PPC2_DIVE_ISA206),
9987GEN_HANDLER_E(divdeo, 0x1F, 0x09, 0x1D, 0, PPC_NONE, PPC2_DIVE_ISA206),
98d1eb27 9988
5c55ff99
BS
9989#undef GEN_INT_ARITH_MUL_HELPER
9990#define GEN_INT_ARITH_MUL_HELPER(name, opc3) \
9991GEN_HANDLER(name, 0x1F, 0x09, opc3, 0x00000000, PPC_64B)
9992GEN_INT_ARITH_MUL_HELPER(mulhdu, 0x00),
9993GEN_INT_ARITH_MUL_HELPER(mulhd, 0x02),
9994GEN_INT_ARITH_MUL_HELPER(mulldo, 0x17),
9995#endif
9996
9997#undef GEN_INT_ARITH_SUBF
9998#undef GEN_INT_ARITH_SUBF_CONST
9999#define GEN_INT_ARITH_SUBF(name, opc3, add_ca, compute_ca, compute_ov) \
10000GEN_HANDLER(name, 0x1F, 0x08, opc3, 0x00000000, PPC_INTEGER),
10001#define GEN_INT_ARITH_SUBF_CONST(name, opc3, const_val, \
10002 add_ca, compute_ca, compute_ov) \
10003GEN_HANDLER(name, 0x1F, 0x08, opc3, 0x0000F800, PPC_INTEGER),
10004GEN_INT_ARITH_SUBF(subf, 0x01, 0, 0, 0)
10005GEN_INT_ARITH_SUBF(subfo, 0x11, 0, 0, 1)
10006GEN_INT_ARITH_SUBF(subfc, 0x00, 0, 1, 0)
10007GEN_INT_ARITH_SUBF(subfco, 0x10, 0, 1, 1)
10008GEN_INT_ARITH_SUBF(subfe, 0x04, 1, 1, 0)
10009GEN_INT_ARITH_SUBF(subfeo, 0x14, 1, 1, 1)
10010GEN_INT_ARITH_SUBF_CONST(subfme, 0x07, -1LL, 1, 1, 0)
10011GEN_INT_ARITH_SUBF_CONST(subfmeo, 0x17, -1LL, 1, 1, 1)
10012GEN_INT_ARITH_SUBF_CONST(subfze, 0x06, 0, 1, 1, 0)
10013GEN_INT_ARITH_SUBF_CONST(subfzeo, 0x16, 0, 1, 1, 1)
10014
10015#undef GEN_LOGICAL1
10016#undef GEN_LOGICAL2
10017#define GEN_LOGICAL2(name, tcg_op, opc, type) \
10018GEN_HANDLER(name, 0x1F, 0x1C, opc, 0x00000000, type)
10019#define GEN_LOGICAL1(name, tcg_op, opc, type) \
10020GEN_HANDLER(name, 0x1F, 0x1A, opc, 0x00000000, type)
10021GEN_LOGICAL2(and, tcg_gen_and_tl, 0x00, PPC_INTEGER),
10022GEN_LOGICAL2(andc, tcg_gen_andc_tl, 0x01, PPC_INTEGER),
10023GEN_LOGICAL2(eqv, tcg_gen_eqv_tl, 0x08, PPC_INTEGER),
10024GEN_LOGICAL1(extsb, tcg_gen_ext8s_tl, 0x1D, PPC_INTEGER),
10025GEN_LOGICAL1(extsh, tcg_gen_ext16s_tl, 0x1C, PPC_INTEGER),
10026GEN_LOGICAL2(nand, tcg_gen_nand_tl, 0x0E, PPC_INTEGER),
10027GEN_LOGICAL2(nor, tcg_gen_nor_tl, 0x03, PPC_INTEGER),
10028GEN_LOGICAL2(orc, tcg_gen_orc_tl, 0x0C, PPC_INTEGER),
10029#if defined(TARGET_PPC64)
10030GEN_LOGICAL1(extsw, tcg_gen_ext32s_tl, 0x1E, PPC_64B),
10031#endif
10032
10033#if defined(TARGET_PPC64)
10034#undef GEN_PPC64_R2
10035#undef GEN_PPC64_R4
10036#define GEN_PPC64_R2(name, opc1, opc2) \
10037GEN_HANDLER2(name##0, stringify(name), opc1, opc2, 0xFF, 0x00000000, PPC_64B),\
10038GEN_HANDLER2(name##1, stringify(name), opc1, opc2 | 0x10, 0xFF, 0x00000000, \
10039 PPC_64B)
10040#define GEN_PPC64_R4(name, opc1, opc2) \
10041GEN_HANDLER2(name##0, stringify(name), opc1, opc2, 0xFF, 0x00000000, PPC_64B),\
10042GEN_HANDLER2(name##1, stringify(name), opc1, opc2 | 0x01, 0xFF, 0x00000000, \
10043 PPC_64B), \
10044GEN_HANDLER2(name##2, stringify(name), opc1, opc2 | 0x10, 0xFF, 0x00000000, \
10045 PPC_64B), \
10046GEN_HANDLER2(name##3, stringify(name), opc1, opc2 | 0x11, 0xFF, 0x00000000, \
10047 PPC_64B)
10048GEN_PPC64_R4(rldicl, 0x1E, 0x00),
10049GEN_PPC64_R4(rldicr, 0x1E, 0x02),
10050GEN_PPC64_R4(rldic, 0x1E, 0x04),
10051GEN_PPC64_R2(rldcl, 0x1E, 0x08),
10052GEN_PPC64_R2(rldcr, 0x1E, 0x09),
10053GEN_PPC64_R4(rldimi, 0x1E, 0x06),
10054#endif
10055
10056#undef _GEN_FLOAT_ACB
10057#undef GEN_FLOAT_ACB
10058#undef _GEN_FLOAT_AB
10059#undef GEN_FLOAT_AB
10060#undef _GEN_FLOAT_AC
10061#undef GEN_FLOAT_AC
10062#undef GEN_FLOAT_B
10063#undef GEN_FLOAT_BS
10064#define _GEN_FLOAT_ACB(name, op, op1, op2, isfloat, set_fprf, type) \
10065GEN_HANDLER(f##name, op1, op2, 0xFF, 0x00000000, type)
10066#define GEN_FLOAT_ACB(name, op2, set_fprf, type) \
10067_GEN_FLOAT_ACB(name, name, 0x3F, op2, 0, set_fprf, type), \
10068_GEN_FLOAT_ACB(name##s, name, 0x3B, op2, 1, set_fprf, type)
10069#define _GEN_FLOAT_AB(name, op, op1, op2, inval, isfloat, set_fprf, type) \
10070GEN_HANDLER(f##name, op1, op2, 0xFF, inval, type)
10071#define GEN_FLOAT_AB(name, op2, inval, set_fprf, type) \
10072_GEN_FLOAT_AB(name, name, 0x3F, op2, inval, 0, set_fprf, type), \
10073_GEN_FLOAT_AB(name##s, name, 0x3B, op2, inval, 1, set_fprf, type)
10074#define _GEN_FLOAT_AC(name, op, op1, op2, inval, isfloat, set_fprf, type) \
10075GEN_HANDLER(f##name, op1, op2, 0xFF, inval, type)
10076#define GEN_FLOAT_AC(name, op2, inval, set_fprf, type) \
10077_GEN_FLOAT_AC(name, name, 0x3F, op2, inval, 0, set_fprf, type), \
10078_GEN_FLOAT_AC(name##s, name, 0x3B, op2, inval, 1, set_fprf, type)
10079#define GEN_FLOAT_B(name, op2, op3, set_fprf, type) \
10080GEN_HANDLER(f##name, 0x3F, op2, op3, 0x001F0000, type)
10081#define GEN_FLOAT_BS(name, op1, op2, set_fprf, type) \
10082GEN_HANDLER(f##name, op1, op2, 0xFF, 0x001F07C0, type)
10083
10084GEN_FLOAT_AB(add, 0x15, 0x000007C0, 1, PPC_FLOAT),
10085GEN_FLOAT_AB(div, 0x12, 0x000007C0, 1, PPC_FLOAT),
10086GEN_FLOAT_AC(mul, 0x19, 0x0000F800, 1, PPC_FLOAT),
10087GEN_FLOAT_BS(re, 0x3F, 0x18, 1, PPC_FLOAT_EXT),
10088GEN_FLOAT_BS(res, 0x3B, 0x18, 1, PPC_FLOAT_FRES),
10089GEN_FLOAT_BS(rsqrte, 0x3F, 0x1A, 1, PPC_FLOAT_FRSQRTE),
10090_GEN_FLOAT_ACB(sel, sel, 0x3F, 0x17, 0, 0, PPC_FLOAT_FSEL),
10091GEN_FLOAT_AB(sub, 0x14, 0x000007C0, 1, PPC_FLOAT),
10092GEN_FLOAT_ACB(madd, 0x1D, 1, PPC_FLOAT),
10093GEN_FLOAT_ACB(msub, 0x1C, 1, PPC_FLOAT),
10094GEN_FLOAT_ACB(nmadd, 0x1F, 1, PPC_FLOAT),
10095GEN_FLOAT_ACB(nmsub, 0x1E, 1, PPC_FLOAT),
da29cb7b 10096GEN_HANDLER_E(ftdiv, 0x3F, 0x00, 0x04, 1, PPC_NONE, PPC2_FP_TST_ISA206),
6d41d146 10097GEN_HANDLER_E(ftsqrt, 0x3F, 0x00, 0x05, 1, PPC_NONE, PPC2_FP_TST_ISA206),
5c55ff99 10098GEN_FLOAT_B(ctiw, 0x0E, 0x00, 0, PPC_FLOAT),
fab7fe42 10099GEN_HANDLER_E(fctiwu, 0x3F, 0x0E, 0x04, 0, PPC_NONE, PPC2_FP_CVT_ISA206),
5c55ff99 10100GEN_FLOAT_B(ctiwz, 0x0F, 0x00, 0, PPC_FLOAT),
fab7fe42 10101GEN_HANDLER_E(fctiwuz, 0x3F, 0x0F, 0x04, 0, PPC_NONE, PPC2_FP_CVT_ISA206),
5c55ff99
BS
10102GEN_FLOAT_B(rsp, 0x0C, 0x00, 1, PPC_FLOAT),
10103#if defined(TARGET_PPC64)
10104GEN_FLOAT_B(cfid, 0x0E, 0x1A, 1, PPC_64B),
28288b48
TM
10105GEN_HANDLER_E(fcfids, 0x3B, 0x0E, 0x1A, 0, PPC_NONE, PPC2_FP_CVT_ISA206),
10106GEN_HANDLER_E(fcfidu, 0x3F, 0x0E, 0x1E, 0, PPC_NONE, PPC2_FP_CVT_ISA206),
10107GEN_HANDLER_E(fcfidus, 0x3B, 0x0E, 0x1E, 0, PPC_NONE, PPC2_FP_CVT_ISA206),
5c55ff99 10108GEN_FLOAT_B(ctid, 0x0E, 0x19, 0, PPC_64B),
fab7fe42 10109GEN_HANDLER_E(fctidu, 0x3F, 0x0E, 0x1D, 0, PPC_NONE, PPC2_FP_CVT_ISA206),
5c55ff99 10110GEN_FLOAT_B(ctidz, 0x0F, 0x19, 0, PPC_64B),
fab7fe42 10111GEN_HANDLER_E(fctiduz, 0x3F, 0x0F, 0x1D, 0, PPC_NONE, PPC2_FP_CVT_ISA206),
5c55ff99
BS
10112#endif
10113GEN_FLOAT_B(rin, 0x08, 0x0C, 1, PPC_FLOAT_EXT),
10114GEN_FLOAT_B(riz, 0x08, 0x0D, 1, PPC_FLOAT_EXT),
10115GEN_FLOAT_B(rip, 0x08, 0x0E, 1, PPC_FLOAT_EXT),
10116GEN_FLOAT_B(rim, 0x08, 0x0F, 1, PPC_FLOAT_EXT),
5c55ff99
BS
10117
10118#undef GEN_LD
10119#undef GEN_LDU
10120#undef GEN_LDUX
cd6e9320 10121#undef GEN_LDX_E
5c55ff99
BS
10122#undef GEN_LDS
10123#define GEN_LD(name, ldop, opc, type) \
10124GEN_HANDLER(name, opc, 0xFF, 0xFF, 0x00000000, type),
10125#define GEN_LDU(name, ldop, opc, type) \
10126GEN_HANDLER(name##u, opc, 0xFF, 0xFF, 0x00000000, type),
10127#define GEN_LDUX(name, ldop, opc2, opc3, type) \
10128GEN_HANDLER(name##ux, 0x1F, opc2, opc3, 0x00000001, type),
cd6e9320
TH
10129#define GEN_LDX_E(name, ldop, opc2, opc3, type, type2) \
10130GEN_HANDLER_E(name##x, 0x1F, opc2, opc3, 0x00000001, type, type2),
5c55ff99
BS
10131#define GEN_LDS(name, ldop, op, type) \
10132GEN_LD(name, ldop, op | 0x20, type) \
10133GEN_LDU(name, ldop, op | 0x21, type) \
10134GEN_LDUX(name, ldop, 0x17, op | 0x01, type) \
10135GEN_LDX(name, ldop, 0x17, op | 0x00, type)
10136
10137GEN_LDS(lbz, ld8u, 0x02, PPC_INTEGER)
10138GEN_LDS(lha, ld16s, 0x0A, PPC_INTEGER)
10139GEN_LDS(lhz, ld16u, 0x08, PPC_INTEGER)
10140GEN_LDS(lwz, ld32u, 0x00, PPC_INTEGER)
10141#if defined(TARGET_PPC64)
10142GEN_LDUX(lwa, ld32s, 0x15, 0x0B, PPC_64B)
10143GEN_LDX(lwa, ld32s, 0x15, 0x0A, PPC_64B)
10144GEN_LDUX(ld, ld64, 0x15, 0x01, PPC_64B)
10145GEN_LDX(ld, ld64, 0x15, 0x00, PPC_64B)
cd6e9320 10146GEN_LDX_E(ldbr, ld64ur, 0x14, 0x10, PPC_NONE, PPC2_DBRX)
5c55ff99
BS
10147#endif
10148GEN_LDX(lhbr, ld16ur, 0x16, 0x18, PPC_INTEGER)
10149GEN_LDX(lwbr, ld32ur, 0x16, 0x10, PPC_INTEGER)
10150
10151#undef GEN_ST
10152#undef GEN_STU
10153#undef GEN_STUX
cd6e9320 10154#undef GEN_STX_E
5c55ff99
BS
10155#undef GEN_STS
10156#define GEN_ST(name, stop, opc, type) \
10157GEN_HANDLER(name, opc, 0xFF, 0xFF, 0x00000000, type),
10158#define GEN_STU(name, stop, opc, type) \
10159GEN_HANDLER(stop##u, opc, 0xFF, 0xFF, 0x00000000, type),
10160#define GEN_STUX(name, stop, opc2, opc3, type) \
10161GEN_HANDLER(name##ux, 0x1F, opc2, opc3, 0x00000001, type),
cd6e9320
TH
10162#define GEN_STX_E(name, stop, opc2, opc3, type, type2) \
10163GEN_HANDLER_E(name##x, 0x1F, opc2, opc3, 0x00000001, type, type2),
5c55ff99
BS
10164#define GEN_STS(name, stop, op, type) \
10165GEN_ST(name, stop, op | 0x20, type) \
10166GEN_STU(name, stop, op | 0x21, type) \
10167GEN_STUX(name, stop, 0x17, op | 0x01, type) \
10168GEN_STX(name, stop, 0x17, op | 0x00, type)
10169
10170GEN_STS(stb, st8, 0x06, PPC_INTEGER)
10171GEN_STS(sth, st16, 0x0C, PPC_INTEGER)
10172GEN_STS(stw, st32, 0x04, PPC_INTEGER)
10173#if defined(TARGET_PPC64)
10174GEN_STUX(std, st64, 0x15, 0x05, PPC_64B)
10175GEN_STX(std, st64, 0x15, 0x04, PPC_64B)
cd6e9320 10176GEN_STX_E(stdbr, st64r, 0x14, 0x14, PPC_NONE, PPC2_DBRX)
5c55ff99
BS
10177#endif
10178GEN_STX(sthbr, st16r, 0x16, 0x1C, PPC_INTEGER)
10179GEN_STX(stwbr, st32r, 0x16, 0x14, PPC_INTEGER)
10180
10181#undef GEN_LDF
10182#undef GEN_LDUF
10183#undef GEN_LDUXF
10184#undef GEN_LDXF
10185#undef GEN_LDFS
10186#define GEN_LDF(name, ldop, opc, type) \
10187GEN_HANDLER(name, opc, 0xFF, 0xFF, 0x00000000, type),
10188#define GEN_LDUF(name, ldop, opc, type) \
10189GEN_HANDLER(name##u, opc, 0xFF, 0xFF, 0x00000000, type),
10190#define GEN_LDUXF(name, ldop, opc, type) \
10191GEN_HANDLER(name##ux, 0x1F, 0x17, opc, 0x00000001, type),
10192#define GEN_LDXF(name, ldop, opc2, opc3, type) \
10193GEN_HANDLER(name##x, 0x1F, opc2, opc3, 0x00000001, type),
10194#define GEN_LDFS(name, ldop, op, type) \
10195GEN_LDF(name, ldop, op | 0x20, type) \
10196GEN_LDUF(name, ldop, op | 0x21, type) \
10197GEN_LDUXF(name, ldop, op | 0x01, type) \
10198GEN_LDXF(name, ldop, 0x17, op | 0x00, type)
10199
10200GEN_LDFS(lfd, ld64, 0x12, PPC_FLOAT)
10201GEN_LDFS(lfs, ld32fs, 0x10, PPC_FLOAT)
199f830d 10202GEN_HANDLER_E(lfiwax, 0x1f, 0x17, 0x1a, 0x00000001, PPC_NONE, PPC2_ISA205),
66c3e328 10203GEN_HANDLER_E(lfiwzx, 0x1f, 0x17, 0x1b, 0x1, PPC_NONE, PPC2_FP_CVT_ISA206),
05050ee8
AJ
10204GEN_HANDLER_E(lfdp, 0x39, 0xFF, 0xFF, 0x00200003, PPC_NONE, PPC2_ISA205),
10205GEN_HANDLER_E(lfdpx, 0x1F, 0x17, 0x18, 0x00200001, PPC_NONE, PPC2_ISA205),
5c55ff99
BS
10206
10207#undef GEN_STF
10208#undef GEN_STUF
10209#undef GEN_STUXF
10210#undef GEN_STXF
10211#undef GEN_STFS
10212#define GEN_STF(name, stop, opc, type) \
10213GEN_HANDLER(name, opc, 0xFF, 0xFF, 0x00000000, type),
10214#define GEN_STUF(name, stop, opc, type) \
10215GEN_HANDLER(name##u, opc, 0xFF, 0xFF, 0x00000000, type),
10216#define GEN_STUXF(name, stop, opc, type) \
10217GEN_HANDLER(name##ux, 0x1F, 0x17, opc, 0x00000001, type),
10218#define GEN_STXF(name, stop, opc2, opc3, type) \
10219GEN_HANDLER(name##x, 0x1F, opc2, opc3, 0x00000001, type),
10220#define GEN_STFS(name, stop, op, type) \
10221GEN_STF(name, stop, op | 0x20, type) \
10222GEN_STUF(name, stop, op | 0x21, type) \
10223GEN_STUXF(name, stop, op | 0x01, type) \
10224GEN_STXF(name, stop, 0x17, op | 0x00, type)
10225
10226GEN_STFS(stfd, st64, 0x16, PPC_FLOAT)
10227GEN_STFS(stfs, st32fs, 0x14, PPC_FLOAT)
10228GEN_STXF(stfiw, st32fiw, 0x17, 0x1E, PPC_FLOAT_STFIWX)
44bc0c4d
AJ
10229GEN_HANDLER_E(stfdp, 0x3D, 0xFF, 0xFF, 0x00200003, PPC_NONE, PPC2_ISA205),
10230GEN_HANDLER_E(stfdpx, 0x1F, 0x17, 0x1C, 0x00200001, PPC_NONE, PPC2_ISA205),
5c55ff99
BS
10231
10232#undef GEN_CRLOGIC
10233#define GEN_CRLOGIC(name, tcg_op, opc) \
10234GEN_HANDLER(name, 0x13, 0x01, opc, 0x00000001, PPC_INTEGER)
10235GEN_CRLOGIC(crand, tcg_gen_and_i32, 0x08),
10236GEN_CRLOGIC(crandc, tcg_gen_andc_i32, 0x04),
10237GEN_CRLOGIC(creqv, tcg_gen_eqv_i32, 0x09),
10238GEN_CRLOGIC(crnand, tcg_gen_nand_i32, 0x07),
10239GEN_CRLOGIC(crnor, tcg_gen_nor_i32, 0x01),
10240GEN_CRLOGIC(cror, tcg_gen_or_i32, 0x0E),
10241GEN_CRLOGIC(crorc, tcg_gen_orc_i32, 0x0D),
10242GEN_CRLOGIC(crxor, tcg_gen_xor_i32, 0x06),
10243
10244#undef GEN_MAC_HANDLER
10245#define GEN_MAC_HANDLER(name, opc2, opc3) \
10246GEN_HANDLER(name, 0x04, opc2, opc3, 0x00000000, PPC_405_MAC)
10247GEN_MAC_HANDLER(macchw, 0x0C, 0x05),
10248GEN_MAC_HANDLER(macchwo, 0x0C, 0x15),
10249GEN_MAC_HANDLER(macchws, 0x0C, 0x07),
10250GEN_MAC_HANDLER(macchwso, 0x0C, 0x17),
10251GEN_MAC_HANDLER(macchwsu, 0x0C, 0x06),
10252GEN_MAC_HANDLER(macchwsuo, 0x0C, 0x16),
10253GEN_MAC_HANDLER(macchwu, 0x0C, 0x04),
10254GEN_MAC_HANDLER(macchwuo, 0x0C, 0x14),
10255GEN_MAC_HANDLER(machhw, 0x0C, 0x01),
10256GEN_MAC_HANDLER(machhwo, 0x0C, 0x11),
10257GEN_MAC_HANDLER(machhws, 0x0C, 0x03),
10258GEN_MAC_HANDLER(machhwso, 0x0C, 0x13),
10259GEN_MAC_HANDLER(machhwsu, 0x0C, 0x02),
10260GEN_MAC_HANDLER(machhwsuo, 0x0C, 0x12),
10261GEN_MAC_HANDLER(machhwu, 0x0C, 0x00),
10262GEN_MAC_HANDLER(machhwuo, 0x0C, 0x10),
10263GEN_MAC_HANDLER(maclhw, 0x0C, 0x0D),
10264GEN_MAC_HANDLER(maclhwo, 0x0C, 0x1D),
10265GEN_MAC_HANDLER(maclhws, 0x0C, 0x0F),
10266GEN_MAC_HANDLER(maclhwso, 0x0C, 0x1F),
10267GEN_MAC_HANDLER(maclhwu, 0x0C, 0x0C),
10268GEN_MAC_HANDLER(maclhwuo, 0x0C, 0x1C),
10269GEN_MAC_HANDLER(maclhwsu, 0x0C, 0x0E),
10270GEN_MAC_HANDLER(maclhwsuo, 0x0C, 0x1E),
10271GEN_MAC_HANDLER(nmacchw, 0x0E, 0x05),
10272GEN_MAC_HANDLER(nmacchwo, 0x0E, 0x15),
10273GEN_MAC_HANDLER(nmacchws, 0x0E, 0x07),
10274GEN_MAC_HANDLER(nmacchwso, 0x0E, 0x17),
10275GEN_MAC_HANDLER(nmachhw, 0x0E, 0x01),
10276GEN_MAC_HANDLER(nmachhwo, 0x0E, 0x11),
10277GEN_MAC_HANDLER(nmachhws, 0x0E, 0x03),
10278GEN_MAC_HANDLER(nmachhwso, 0x0E, 0x13),
10279GEN_MAC_HANDLER(nmaclhw, 0x0E, 0x0D),
10280GEN_MAC_HANDLER(nmaclhwo, 0x0E, 0x1D),
10281GEN_MAC_HANDLER(nmaclhws, 0x0E, 0x0F),
10282GEN_MAC_HANDLER(nmaclhwso, 0x0E, 0x1F),
10283GEN_MAC_HANDLER(mulchw, 0x08, 0x05),
10284GEN_MAC_HANDLER(mulchwu, 0x08, 0x04),
10285GEN_MAC_HANDLER(mulhhw, 0x08, 0x01),
10286GEN_MAC_HANDLER(mulhhwu, 0x08, 0x00),
10287GEN_MAC_HANDLER(mullhw, 0x08, 0x0D),
10288GEN_MAC_HANDLER(mullhwu, 0x08, 0x0C),
10289
10290#undef GEN_VR_LDX
10291#undef GEN_VR_STX
10292#undef GEN_VR_LVE
10293#undef GEN_VR_STVE
10294#define GEN_VR_LDX(name, opc2, opc3) \
10295GEN_HANDLER(name, 0x1F, opc2, opc3, 0x00000001, PPC_ALTIVEC)
10296#define GEN_VR_STX(name, opc2, opc3) \
10297GEN_HANDLER(st##name, 0x1F, opc2, opc3, 0x00000001, PPC_ALTIVEC)
10298#define GEN_VR_LVE(name, opc2, opc3) \
10299 GEN_HANDLER(lve##name, 0x1F, opc2, opc3, 0x00000001, PPC_ALTIVEC)
10300#define GEN_VR_STVE(name, opc2, opc3) \
10301 GEN_HANDLER(stve##name, 0x1F, opc2, opc3, 0x00000001, PPC_ALTIVEC)
10302GEN_VR_LDX(lvx, 0x07, 0x03),
10303GEN_VR_LDX(lvxl, 0x07, 0x0B),
10304GEN_VR_LVE(bx, 0x07, 0x00),
10305GEN_VR_LVE(hx, 0x07, 0x01),
10306GEN_VR_LVE(wx, 0x07, 0x02),
10307GEN_VR_STX(svx, 0x07, 0x07),
10308GEN_VR_STX(svxl, 0x07, 0x0F),
10309GEN_VR_STVE(bx, 0x07, 0x04),
10310GEN_VR_STVE(hx, 0x07, 0x05),
10311GEN_VR_STVE(wx, 0x07, 0x06),
10312
10313#undef GEN_VX_LOGICAL
10314#define GEN_VX_LOGICAL(name, tcg_op, opc2, opc3) \
10315GEN_HANDLER(name, 0x04, opc2, opc3, 0x00000000, PPC_ALTIVEC)
111c5f54
TM
10316
10317#undef GEN_VX_LOGICAL_207
10318#define GEN_VX_LOGICAL_207(name, tcg_op, opc2, opc3) \
10319GEN_HANDLER_E(name, 0x04, opc2, opc3, 0x00000000, PPC_NONE, PPC2_ALTIVEC_207)
10320
5c55ff99
BS
10321GEN_VX_LOGICAL(vand, tcg_gen_and_i64, 2, 16),
10322GEN_VX_LOGICAL(vandc, tcg_gen_andc_i64, 2, 17),
10323GEN_VX_LOGICAL(vor, tcg_gen_or_i64, 2, 18),
10324GEN_VX_LOGICAL(vxor, tcg_gen_xor_i64, 2, 19),
10325GEN_VX_LOGICAL(vnor, tcg_gen_nor_i64, 2, 20),
111c5f54
TM
10326GEN_VX_LOGICAL_207(veqv, tcg_gen_eqv_i64, 2, 26),
10327GEN_VX_LOGICAL_207(vnand, tcg_gen_nand_i64, 2, 22),
10328GEN_VX_LOGICAL_207(vorc, tcg_gen_orc_i64, 2, 21),
5c55ff99
BS
10329
10330#undef GEN_VXFORM
10331#define GEN_VXFORM(name, opc2, opc3) \
10332GEN_HANDLER(name, 0x04, opc2, opc3, 0x00000000, PPC_ALTIVEC)
50f5fc0c
TM
10333
10334#undef GEN_VXFORM_207
10335#define GEN_VXFORM_207(name, opc2, opc3) \
10336GEN_HANDLER_E(name, 0x04, opc2, opc3, 0x00000000, PPC_NONE, PPC2_ALTIVEC_207)
10337
5dffff5a
TM
10338#undef GEN_VXFORM_DUAL
10339#define GEN_VXFORM_DUAL(name0, name1, opc2, opc3, type0, type1) \
10340GEN_HANDLER_E(name0##_##name1, 0x4, opc2, opc3, 0x00000000, type0, type1)
10341
a737d3eb
TM
10342#undef GEN_VXRFORM_DUAL
10343#define GEN_VXRFORM_DUAL(name0, name1, opc2, opc3, tp0, tp1) \
10344GEN_HANDLER_E(name0##_##name1, 0x4, opc2, opc3, 0x00000000, tp0, tp1), \
10345GEN_HANDLER_E(name0##_##name1, 0x4, opc2, (opc3 | 0x10), 0x00000000, tp0, tp1),
10346
5c55ff99
BS
10347GEN_VXFORM(vaddubm, 0, 0),
10348GEN_VXFORM(vadduhm, 0, 1),
10349GEN_VXFORM(vadduwm, 0, 2),
56eabc75 10350GEN_VXFORM_207(vaddudm, 0, 3),
5c55ff99
BS
10351GEN_VXFORM(vsububm, 0, 16),
10352GEN_VXFORM(vsubuhm, 0, 17),
10353GEN_VXFORM(vsubuwm, 0, 18),
56eabc75 10354GEN_VXFORM_207(vsubudm, 0, 19),
5c55ff99
BS
10355GEN_VXFORM(vmaxub, 1, 0),
10356GEN_VXFORM(vmaxuh, 1, 1),
10357GEN_VXFORM(vmaxuw, 1, 2),
10358GEN_VXFORM(vmaxsb, 1, 4),
10359GEN_VXFORM(vmaxsh, 1, 5),
10360GEN_VXFORM(vmaxsw, 1, 6),
10361GEN_VXFORM(vminub, 1, 8),
10362GEN_VXFORM(vminuh, 1, 9),
10363GEN_VXFORM(vminuw, 1, 10),
10364GEN_VXFORM(vminsb, 1, 12),
10365GEN_VXFORM(vminsh, 1, 13),
10366GEN_VXFORM(vminsw, 1, 14),
10367GEN_VXFORM(vavgub, 1, 16),
10368GEN_VXFORM(vavguh, 1, 17),
10369GEN_VXFORM(vavguw, 1, 18),
10370GEN_VXFORM(vavgsb, 1, 20),
10371GEN_VXFORM(vavgsh, 1, 21),
10372GEN_VXFORM(vavgsw, 1, 22),
10373GEN_VXFORM(vmrghb, 6, 0),
10374GEN_VXFORM(vmrghh, 6, 1),
10375GEN_VXFORM(vmrghw, 6, 2),
10376GEN_VXFORM(vmrglb, 6, 4),
10377GEN_VXFORM(vmrglh, 6, 5),
10378GEN_VXFORM(vmrglw, 6, 6),
10379GEN_VXFORM(vmuloub, 4, 0),
10380GEN_VXFORM(vmulouh, 4, 1),
953f0f58 10381GEN_VXFORM_DUAL(vmulouw, vmuluwm, 4, 2, PPC_ALTIVEC, PPC_NONE),
5c55ff99
BS
10382GEN_VXFORM(vmulosb, 4, 4),
10383GEN_VXFORM(vmulosh, 4, 5),
63be0936 10384GEN_VXFORM_207(vmulosw, 4, 6),
5c55ff99
BS
10385GEN_VXFORM(vmuleub, 4, 8),
10386GEN_VXFORM(vmuleuh, 4, 9),
63be0936 10387GEN_VXFORM_207(vmuleuw, 4, 10),
5c55ff99
BS
10388GEN_VXFORM(vmulesb, 4, 12),
10389GEN_VXFORM(vmulesh, 4, 13),
63be0936 10390GEN_VXFORM_207(vmulesw, 4, 14),
5c55ff99
BS
10391GEN_VXFORM(vslb, 2, 4),
10392GEN_VXFORM(vslh, 2, 5),
10393GEN_VXFORM(vslw, 2, 6),
10394GEN_VXFORM(vsrb, 2, 8),
10395GEN_VXFORM(vsrh, 2, 9),
10396GEN_VXFORM(vsrw, 2, 10),
10397GEN_VXFORM(vsrab, 2, 12),
10398GEN_VXFORM(vsrah, 2, 13),
10399GEN_VXFORM(vsraw, 2, 14),
10400GEN_VXFORM(vslo, 6, 16),
10401GEN_VXFORM(vsro, 6, 17),
10402GEN_VXFORM(vaddcuw, 0, 6),
10403GEN_VXFORM(vsubcuw, 0, 22),
10404GEN_VXFORM(vaddubs, 0, 8),
10405GEN_VXFORM(vadduhs, 0, 9),
10406GEN_VXFORM(vadduws, 0, 10),
10407GEN_VXFORM(vaddsbs, 0, 12),
10408GEN_VXFORM(vaddshs, 0, 13),
10409GEN_VXFORM(vaddsws, 0, 14),
10410GEN_VXFORM(vsububs, 0, 24),
10411GEN_VXFORM(vsubuhs, 0, 25),
10412GEN_VXFORM(vsubuws, 0, 26),
10413GEN_VXFORM(vsubsbs, 0, 28),
10414GEN_VXFORM(vsubshs, 0, 29),
10415GEN_VXFORM(vsubsws, 0, 30),
10416GEN_VXFORM(vrlb, 2, 0),
10417GEN_VXFORM(vrlh, 2, 1),
10418GEN_VXFORM(vrlw, 2, 2),
10419GEN_VXFORM(vsl, 2, 7),
10420GEN_VXFORM(vsr, 2, 11),
10421GEN_VXFORM(vpkuhum, 7, 0),
10422GEN_VXFORM(vpkuwum, 7, 1),
10423GEN_VXFORM(vpkuhus, 7, 2),
10424GEN_VXFORM(vpkuwus, 7, 3),
10425GEN_VXFORM(vpkshus, 7, 4),
10426GEN_VXFORM(vpkswus, 7, 5),
10427GEN_VXFORM(vpkshss, 7, 6),
10428GEN_VXFORM(vpkswss, 7, 7),
10429GEN_VXFORM(vpkpx, 7, 12),
10430GEN_VXFORM(vsum4ubs, 4, 24),
10431GEN_VXFORM(vsum4sbs, 4, 28),
10432GEN_VXFORM(vsum4shs, 4, 25),
10433GEN_VXFORM(vsum2sws, 4, 26),
10434GEN_VXFORM(vsumsws, 4, 30),
10435GEN_VXFORM(vaddfp, 5, 0),
10436GEN_VXFORM(vsubfp, 5, 1),
10437GEN_VXFORM(vmaxfp, 5, 16),
10438GEN_VXFORM(vminfp, 5, 17),
10439
10440#undef GEN_VXRFORM1
10441#undef GEN_VXRFORM
10442#define GEN_VXRFORM1(opname, name, str, opc2, opc3) \
10443 GEN_HANDLER2(name, str, 0x4, opc2, opc3, 0x00000000, PPC_ALTIVEC),
10444#define GEN_VXRFORM(name, opc2, opc3) \
10445 GEN_VXRFORM1(name, name, #name, opc2, opc3) \
10446 GEN_VXRFORM1(name##_dot, name##_, #name ".", opc2, (opc3 | (0x1 << 4)))
10447GEN_VXRFORM(vcmpequb, 3, 0)
10448GEN_VXRFORM(vcmpequh, 3, 1)
10449GEN_VXRFORM(vcmpequw, 3, 2)
10450GEN_VXRFORM(vcmpgtsb, 3, 12)
10451GEN_VXRFORM(vcmpgtsh, 3, 13)
10452GEN_VXRFORM(vcmpgtsw, 3, 14)
10453GEN_VXRFORM(vcmpgtub, 3, 8)
10454GEN_VXRFORM(vcmpgtuh, 3, 9)
10455GEN_VXRFORM(vcmpgtuw, 3, 10)
10456GEN_VXRFORM(vcmpeqfp, 3, 3)
10457GEN_VXRFORM(vcmpgefp, 3, 7)
10458GEN_VXRFORM(vcmpgtfp, 3, 11)
10459GEN_VXRFORM(vcmpbfp, 3, 15)
10460
10461#undef GEN_VXFORM_SIMM
10462#define GEN_VXFORM_SIMM(name, opc2, opc3) \
10463 GEN_HANDLER(name, 0x04, opc2, opc3, 0x00000000, PPC_ALTIVEC)
10464GEN_VXFORM_SIMM(vspltisb, 6, 12),
10465GEN_VXFORM_SIMM(vspltish, 6, 13),
10466GEN_VXFORM_SIMM(vspltisw, 6, 14),
10467
10468#undef GEN_VXFORM_NOA
10469#define GEN_VXFORM_NOA(name, opc2, opc3) \
10470 GEN_HANDLER(name, 0x04, opc2, opc3, 0x001f0000, PPC_ALTIVEC)
10471GEN_VXFORM_NOA(vupkhsb, 7, 8),
10472GEN_VXFORM_NOA(vupkhsh, 7, 9),
10473GEN_VXFORM_NOA(vupklsb, 7, 10),
10474GEN_VXFORM_NOA(vupklsh, 7, 11),
10475GEN_VXFORM_NOA(vupkhpx, 7, 13),
10476GEN_VXFORM_NOA(vupklpx, 7, 15),
10477GEN_VXFORM_NOA(vrefp, 5, 4),
10478GEN_VXFORM_NOA(vrsqrtefp, 5, 5),
0bffbc6c 10479GEN_VXFORM_NOA(vexptefp, 5, 6),
5c55ff99
BS
10480GEN_VXFORM_NOA(vlogefp, 5, 7),
10481GEN_VXFORM_NOA(vrfim, 5, 8),
10482GEN_VXFORM_NOA(vrfin, 5, 9),
10483GEN_VXFORM_NOA(vrfip, 5, 10),
10484GEN_VXFORM_NOA(vrfiz, 5, 11),
10485
10486#undef GEN_VXFORM_UIMM
10487#define GEN_VXFORM_UIMM(name, opc2, opc3) \
10488 GEN_HANDLER(name, 0x04, opc2, opc3, 0x00000000, PPC_ALTIVEC)
10489GEN_VXFORM_UIMM(vspltb, 6, 8),
10490GEN_VXFORM_UIMM(vsplth, 6, 9),
10491GEN_VXFORM_UIMM(vspltw, 6, 10),
10492GEN_VXFORM_UIMM(vcfux, 5, 12),
10493GEN_VXFORM_UIMM(vcfsx, 5, 13),
10494GEN_VXFORM_UIMM(vctuxs, 5, 14),
10495GEN_VXFORM_UIMM(vctsxs, 5, 15),
10496
10497#undef GEN_VAFORM_PAIRED
10498#define GEN_VAFORM_PAIRED(name0, name1, opc2) \
10499 GEN_HANDLER(name0##_##name1, 0x04, opc2, 0xFF, 0x00000000, PPC_ALTIVEC)
10500GEN_VAFORM_PAIRED(vmhaddshs, vmhraddshs, 16),
10501GEN_VAFORM_PAIRED(vmsumubm, vmsummbm, 18),
10502GEN_VAFORM_PAIRED(vmsumuhm, vmsumuhs, 19),
10503GEN_VAFORM_PAIRED(vmsumshm, vmsumshs, 20),
10504GEN_VAFORM_PAIRED(vsel, vperm, 21),
10505GEN_VAFORM_PAIRED(vmaddfp, vnmsubfp, 23),
10506
fa1832d7 10507GEN_HANDLER_E(lxsdx, 0x1F, 0x0C, 0x12, 0, PPC_NONE, PPC2_VSX),
cac7f0ba
TM
10508GEN_HANDLER_E(lxsiwax, 0x1F, 0x0C, 0x02, 0, PPC_NONE, PPC2_VSX207),
10509GEN_HANDLER_E(lxsiwzx, 0x1F, 0x0C, 0x00, 0, PPC_NONE, PPC2_VSX207),
10510GEN_HANDLER_E(lxsspx, 0x1F, 0x0C, 0x10, 0, PPC_NONE, PPC2_VSX207),
304af367 10511GEN_HANDLER_E(lxvd2x, 0x1F, 0x0C, 0x1A, 0, PPC_NONE, PPC2_VSX),
ca03b467 10512GEN_HANDLER_E(lxvdsx, 0x1F, 0x0C, 0x0A, 0, PPC_NONE, PPC2_VSX),
897e61d1 10513GEN_HANDLER_E(lxvw4x, 0x1F, 0x0C, 0x18, 0, PPC_NONE, PPC2_VSX),
304af367 10514
9231ba9e 10515GEN_HANDLER_E(stxsdx, 0x1F, 0xC, 0x16, 0, PPC_NONE, PPC2_VSX),
e16a626b
TM
10516GEN_HANDLER_E(stxsiwx, 0x1F, 0xC, 0x04, 0, PPC_NONE, PPC2_VSX207),
10517GEN_HANDLER_E(stxsspx, 0x1F, 0xC, 0x14, 0, PPC_NONE, PPC2_VSX207),
fbed2478 10518GEN_HANDLER_E(stxvd2x, 0x1F, 0xC, 0x1E, 0, PPC_NONE, PPC2_VSX),
86e61ce3 10519GEN_HANDLER_E(stxvw4x, 0x1F, 0xC, 0x1C, 0, PPC_NONE, PPC2_VSX),
fbed2478 10520
f5c0f7f9
TM
10521GEN_HANDLER_E(mfvsrwz, 0x1F, 0x13, 0x03, 0x0000F800, PPC_NONE, PPC2_VSX207),
10522GEN_HANDLER_E(mtvsrwa, 0x1F, 0x13, 0x06, 0x0000F800, PPC_NONE, PPC2_VSX207),
10523GEN_HANDLER_E(mtvsrwz, 0x1F, 0x13, 0x07, 0x0000F800, PPC_NONE, PPC2_VSX207),
10524#if defined(TARGET_PPC64)
10525GEN_HANDLER_E(mfvsrd, 0x1F, 0x13, 0x01, 0x0000F800, PPC_NONE, PPC2_VSX207),
10526GEN_HANDLER_E(mtvsrd, 0x1F, 0x13, 0x05, 0x0000F800, PPC_NONE, PPC2_VSX207),
10527#endif
10528
df020ce0
TM
10529#undef GEN_XX2FORM
10530#define GEN_XX2FORM(name, opc2, opc3, fl2) \
10531GEN_HANDLER2_E(name, #name, 0x3C, opc2 | 0, opc3, 0, PPC_NONE, fl2), \
10532GEN_HANDLER2_E(name, #name, 0x3C, opc2 | 1, opc3, 0, PPC_NONE, fl2)
10533
10534#undef GEN_XX3FORM
10535#define GEN_XX3FORM(name, opc2, opc3, fl2) \
10536GEN_HANDLER2_E(name, #name, 0x3C, opc2 | 0, opc3, 0, PPC_NONE, fl2), \
10537GEN_HANDLER2_E(name, #name, 0x3C, opc2 | 1, opc3, 0, PPC_NONE, fl2), \
10538GEN_HANDLER2_E(name, #name, 0x3C, opc2 | 2, opc3, 0, PPC_NONE, fl2), \
10539GEN_HANDLER2_E(name, #name, 0x3C, opc2 | 3, opc3, 0, PPC_NONE, fl2)
10540
354a6dec
TM
10541#undef GEN_XX3_RC_FORM
10542#define GEN_XX3_RC_FORM(name, opc2, opc3, fl2) \
10543GEN_HANDLER2_E(name, #name, 0x3C, opc2 | 0x00, opc3 | 0x00, 0, PPC_NONE, fl2), \
10544GEN_HANDLER2_E(name, #name, 0x3C, opc2 | 0x01, opc3 | 0x00, 0, PPC_NONE, fl2), \
10545GEN_HANDLER2_E(name, #name, 0x3C, opc2 | 0x02, opc3 | 0x00, 0, PPC_NONE, fl2), \
10546GEN_HANDLER2_E(name, #name, 0x3C, opc2 | 0x03, opc3 | 0x00, 0, PPC_NONE, fl2), \
10547GEN_HANDLER2_E(name, #name, 0x3C, opc2 | 0x00, opc3 | 0x10, 0, PPC_NONE, fl2), \
10548GEN_HANDLER2_E(name, #name, 0x3C, opc2 | 0x01, opc3 | 0x10, 0, PPC_NONE, fl2), \
10549GEN_HANDLER2_E(name, #name, 0x3C, opc2 | 0x02, opc3 | 0x10, 0, PPC_NONE, fl2), \
10550GEN_HANDLER2_E(name, #name, 0x3C, opc2 | 0x03, opc3 | 0x10, 0, PPC_NONE, fl2)
10551
cd73f2c9
TM
10552#undef GEN_XX3FORM_DM
10553#define GEN_XX3FORM_DM(name, opc2, opc3) \
10554GEN_HANDLER2_E(name, #name, 0x3C, opc2|0x00, opc3|0x00, 0, PPC_NONE, PPC2_VSX),\
10555GEN_HANDLER2_E(name, #name, 0x3C, opc2|0x01, opc3|0x00, 0, PPC_NONE, PPC2_VSX),\
10556GEN_HANDLER2_E(name, #name, 0x3C, opc2|0x02, opc3|0x00, 0, PPC_NONE, PPC2_VSX),\
10557GEN_HANDLER2_E(name, #name, 0x3C, opc2|0x03, opc3|0x00, 0, PPC_NONE, PPC2_VSX),\
10558GEN_HANDLER2_E(name, #name, 0x3C, opc2|0x00, opc3|0x04, 0, PPC_NONE, PPC2_VSX),\
10559GEN_HANDLER2_E(name, #name, 0x3C, opc2|0x01, opc3|0x04, 0, PPC_NONE, PPC2_VSX),\
10560GEN_HANDLER2_E(name, #name, 0x3C, opc2|0x02, opc3|0x04, 0, PPC_NONE, PPC2_VSX),\
10561GEN_HANDLER2_E(name, #name, 0x3C, opc2|0x03, opc3|0x04, 0, PPC_NONE, PPC2_VSX),\
10562GEN_HANDLER2_E(name, #name, 0x3C, opc2|0x00, opc3|0x08, 0, PPC_NONE, PPC2_VSX),\
10563GEN_HANDLER2_E(name, #name, 0x3C, opc2|0x01, opc3|0x08, 0, PPC_NONE, PPC2_VSX),\
10564GEN_HANDLER2_E(name, #name, 0x3C, opc2|0x02, opc3|0x08, 0, PPC_NONE, PPC2_VSX),\
10565GEN_HANDLER2_E(name, #name, 0x3C, opc2|0x03, opc3|0x08, 0, PPC_NONE, PPC2_VSX),\
10566GEN_HANDLER2_E(name, #name, 0x3C, opc2|0x00, opc3|0x0C, 0, PPC_NONE, PPC2_VSX),\
10567GEN_HANDLER2_E(name, #name, 0x3C, opc2|0x01, opc3|0x0C, 0, PPC_NONE, PPC2_VSX),\
10568GEN_HANDLER2_E(name, #name, 0x3C, opc2|0x02, opc3|0x0C, 0, PPC_NONE, PPC2_VSX),\
10569GEN_HANDLER2_E(name, #name, 0x3C, opc2|0x03, opc3|0x0C, 0, PPC_NONE, PPC2_VSX)
10570
df020ce0
TM
10571GEN_XX2FORM(xsabsdp, 0x12, 0x15, PPC2_VSX),
10572GEN_XX2FORM(xsnabsdp, 0x12, 0x16, PPC2_VSX),
10573GEN_XX2FORM(xsnegdp, 0x12, 0x17, PPC2_VSX),
10574GEN_XX3FORM(xscpsgndp, 0x00, 0x16, PPC2_VSX),
10575
be574920
TM
10576GEN_XX2FORM(xvabsdp, 0x12, 0x1D, PPC2_VSX),
10577GEN_XX2FORM(xvnabsdp, 0x12, 0x1E, PPC2_VSX),
10578GEN_XX2FORM(xvnegdp, 0x12, 0x1F, PPC2_VSX),
10579GEN_XX3FORM(xvcpsgndp, 0x00, 0x1E, PPC2_VSX),
10580GEN_XX2FORM(xvabssp, 0x12, 0x19, PPC2_VSX),
10581GEN_XX2FORM(xvnabssp, 0x12, 0x1A, PPC2_VSX),
10582GEN_XX2FORM(xvnegsp, 0x12, 0x1B, PPC2_VSX),
10583GEN_XX3FORM(xvcpsgnsp, 0x00, 0x1A, PPC2_VSX),
79ca8a6a 10584
ee6e02c0
TM
10585GEN_XX3FORM(xsadddp, 0x00, 0x04, PPC2_VSX),
10586GEN_XX3FORM(xssubdp, 0x00, 0x05, PPC2_VSX),
5e591d88 10587GEN_XX3FORM(xsmuldp, 0x00, 0x06, PPC2_VSX),
4b98eeef 10588GEN_XX3FORM(xsdivdp, 0x00, 0x07, PPC2_VSX),
2009227f 10589GEN_XX2FORM(xsredp, 0x14, 0x05, PPC2_VSX),
d32404fe 10590GEN_XX2FORM(xssqrtdp, 0x16, 0x04, PPC2_VSX),
d3f9df8f 10591GEN_XX2FORM(xsrsqrtedp, 0x14, 0x04, PPC2_VSX),
bc80838f 10592GEN_XX3FORM(xstdivdp, 0x14, 0x07, PPC2_VSX),
5cb151ac 10593GEN_XX2FORM(xstsqrtdp, 0x14, 0x06, PPC2_VSX),
595c6eef
TM
10594GEN_XX3FORM(xsmaddadp, 0x04, 0x04, PPC2_VSX),
10595GEN_XX3FORM(xsmaddmdp, 0x04, 0x05, PPC2_VSX),
10596GEN_XX3FORM(xsmsubadp, 0x04, 0x06, PPC2_VSX),
10597GEN_XX3FORM(xsmsubmdp, 0x04, 0x07, PPC2_VSX),
10598GEN_XX3FORM(xsnmaddadp, 0x04, 0x14, PPC2_VSX),
10599GEN_XX3FORM(xsnmaddmdp, 0x04, 0x15, PPC2_VSX),
10600GEN_XX3FORM(xsnmsubadp, 0x04, 0x16, PPC2_VSX),
10601GEN_XX3FORM(xsnmsubmdp, 0x04, 0x17, PPC2_VSX),
4f17e9c7
TM
10602GEN_XX2FORM(xscmpodp, 0x0C, 0x05, PPC2_VSX),
10603GEN_XX2FORM(xscmpudp, 0x0C, 0x04, PPC2_VSX),
959e9c9d
TM
10604GEN_XX3FORM(xsmaxdp, 0x00, 0x14, PPC2_VSX),
10605GEN_XX3FORM(xsmindp, 0x00, 0x15, PPC2_VSX),
ed8ac568 10606GEN_XX2FORM(xscvdpsp, 0x12, 0x10, PPC2_VSX),
7ee19fb9 10607GEN_XX2FORM(xscvdpspn, 0x16, 0x10, PPC2_VSX207),
ed8ac568 10608GEN_XX2FORM(xscvspdp, 0x12, 0x14, PPC2_VSX),
7ee19fb9 10609GEN_XX2FORM(xscvspdpn, 0x16, 0x14, PPC2_VSX207),
5177d2ca
TM
10610GEN_XX2FORM(xscvdpsxds, 0x10, 0x15, PPC2_VSX),
10611GEN_XX2FORM(xscvdpsxws, 0x10, 0x05, PPC2_VSX),
10612GEN_XX2FORM(xscvdpuxds, 0x10, 0x14, PPC2_VSX),
10613GEN_XX2FORM(xscvdpuxws, 0x10, 0x04, PPC2_VSX),
10614GEN_XX2FORM(xscvsxddp, 0x10, 0x17, PPC2_VSX),
10615GEN_XX2FORM(xscvuxddp, 0x10, 0x16, PPC2_VSX),
88e33d08
TM
10616GEN_XX2FORM(xsrdpi, 0x12, 0x04, PPC2_VSX),
10617GEN_XX2FORM(xsrdpic, 0x16, 0x06, PPC2_VSX),
10618GEN_XX2FORM(xsrdpim, 0x12, 0x07, PPC2_VSX),
10619GEN_XX2FORM(xsrdpip, 0x12, 0x06, PPC2_VSX),
10620GEN_XX2FORM(xsrdpiz, 0x12, 0x05, PPC2_VSX),
ee6e02c0 10621
3fd0aadf
TM
10622GEN_XX3FORM(xsaddsp, 0x00, 0x00, PPC2_VSX207),
10623GEN_XX3FORM(xssubsp, 0x00, 0x01, PPC2_VSX207),
ab9408a2 10624GEN_XX3FORM(xsmulsp, 0x00, 0x02, PPC2_VSX207),
b24d0b47 10625GEN_XX3FORM(xsdivsp, 0x00, 0x03, PPC2_VSX207),
2c0c52ae 10626GEN_XX2FORM(xsresp, 0x14, 0x01, PPC2_VSX207),
3d1140bf 10627GEN_XX2FORM(xsrsp, 0x12, 0x11, PPC2_VSX207),
cea4e574 10628GEN_XX2FORM(xssqrtsp, 0x16, 0x00, PPC2_VSX207),
968e76bc 10629GEN_XX2FORM(xsrsqrtesp, 0x14, 0x00, PPC2_VSX207),
f53f81e0
TM
10630GEN_XX3FORM(xsmaddasp, 0x04, 0x00, PPC2_VSX207),
10631GEN_XX3FORM(xsmaddmsp, 0x04, 0x01, PPC2_VSX207),
10632GEN_XX3FORM(xsmsubasp, 0x04, 0x02, PPC2_VSX207),
10633GEN_XX3FORM(xsmsubmsp, 0x04, 0x03, PPC2_VSX207),
10634GEN_XX3FORM(xsnmaddasp, 0x04, 0x10, PPC2_VSX207),
10635GEN_XX3FORM(xsnmaddmsp, 0x04, 0x11, PPC2_VSX207),
10636GEN_XX3FORM(xsnmsubasp, 0x04, 0x12, PPC2_VSX207),
10637GEN_XX3FORM(xsnmsubmsp, 0x04, 0x13, PPC2_VSX207),
74698350
TM
10638GEN_XX2FORM(xscvsxdsp, 0x10, 0x13, PPC2_VSX207),
10639GEN_XX2FORM(xscvuxdsp, 0x10, 0x12, PPC2_VSX207),
3fd0aadf 10640
ee6e02c0
TM
10641GEN_XX3FORM(xvadddp, 0x00, 0x0C, PPC2_VSX),
10642GEN_XX3FORM(xvsubdp, 0x00, 0x0D, PPC2_VSX),
5e591d88 10643GEN_XX3FORM(xvmuldp, 0x00, 0x0E, PPC2_VSX),
4b98eeef 10644GEN_XX3FORM(xvdivdp, 0x00, 0x0F, PPC2_VSX),
2009227f 10645GEN_XX2FORM(xvredp, 0x14, 0x0D, PPC2_VSX),
d32404fe 10646GEN_XX2FORM(xvsqrtdp, 0x16, 0x0C, PPC2_VSX),
d3f9df8f 10647GEN_XX2FORM(xvrsqrtedp, 0x14, 0x0C, PPC2_VSX),
bc80838f 10648GEN_XX3FORM(xvtdivdp, 0x14, 0x0F, PPC2_VSX),
5cb151ac 10649GEN_XX2FORM(xvtsqrtdp, 0x14, 0x0E, PPC2_VSX),
595c6eef
TM
10650GEN_XX3FORM(xvmaddadp, 0x04, 0x0C, PPC2_VSX),
10651GEN_XX3FORM(xvmaddmdp, 0x04, 0x0D, PPC2_VSX),
10652GEN_XX3FORM(xvmsubadp, 0x04, 0x0E, PPC2_VSX),
10653GEN_XX3FORM(xvmsubmdp, 0x04, 0x0F, PPC2_VSX),
10654GEN_XX3FORM(xvnmaddadp, 0x04, 0x1C, PPC2_VSX),
10655GEN_XX3FORM(xvnmaddmdp, 0x04, 0x1D, PPC2_VSX),
10656GEN_XX3FORM(xvnmsubadp, 0x04, 0x1E, PPC2_VSX),
10657GEN_XX3FORM(xvnmsubmdp, 0x04, 0x1F, PPC2_VSX),
959e9c9d
TM
10658GEN_XX3FORM(xvmaxdp, 0x00, 0x1C, PPC2_VSX),
10659GEN_XX3FORM(xvmindp, 0x00, 0x1D, PPC2_VSX),
354a6dec
TM
10660GEN_XX3_RC_FORM(xvcmpeqdp, 0x0C, 0x0C, PPC2_VSX),
10661GEN_XX3_RC_FORM(xvcmpgtdp, 0x0C, 0x0D, PPC2_VSX),
10662GEN_XX3_RC_FORM(xvcmpgedp, 0x0C, 0x0E, PPC2_VSX),
ed8ac568 10663GEN_XX2FORM(xvcvdpsp, 0x12, 0x18, PPC2_VSX),
5177d2ca
TM
10664GEN_XX2FORM(xvcvdpsxds, 0x10, 0x1D, PPC2_VSX),
10665GEN_XX2FORM(xvcvdpsxws, 0x10, 0x0D, PPC2_VSX),
10666GEN_XX2FORM(xvcvdpuxds, 0x10, 0x1C, PPC2_VSX),
10667GEN_XX2FORM(xvcvdpuxws, 0x10, 0x0C, PPC2_VSX),
10668GEN_XX2FORM(xvcvsxddp, 0x10, 0x1F, PPC2_VSX),
10669GEN_XX2FORM(xvcvuxddp, 0x10, 0x1E, PPC2_VSX),
10670GEN_XX2FORM(xvcvsxwdp, 0x10, 0x0F, PPC2_VSX),
10671GEN_XX2FORM(xvcvuxwdp, 0x10, 0x0E, PPC2_VSX),
88e33d08
TM
10672GEN_XX2FORM(xvrdpi, 0x12, 0x0C, PPC2_VSX),
10673GEN_XX2FORM(xvrdpic, 0x16, 0x0E, PPC2_VSX),
10674GEN_XX2FORM(xvrdpim, 0x12, 0x0F, PPC2_VSX),
10675GEN_XX2FORM(xvrdpip, 0x12, 0x0E, PPC2_VSX),
10676GEN_XX2FORM(xvrdpiz, 0x12, 0x0D, PPC2_VSX),
ee6e02c0
TM
10677
10678GEN_XX3FORM(xvaddsp, 0x00, 0x08, PPC2_VSX),
10679GEN_XX3FORM(xvsubsp, 0x00, 0x09, PPC2_VSX),
5e591d88 10680GEN_XX3FORM(xvmulsp, 0x00, 0x0A, PPC2_VSX),
4b98eeef 10681GEN_XX3FORM(xvdivsp, 0x00, 0x0B, PPC2_VSX),
2009227f 10682GEN_XX2FORM(xvresp, 0x14, 0x09, PPC2_VSX),
d32404fe 10683GEN_XX2FORM(xvsqrtsp, 0x16, 0x08, PPC2_VSX),
d3f9df8f 10684GEN_XX2FORM(xvrsqrtesp, 0x14, 0x08, PPC2_VSX),
bc80838f 10685GEN_XX3FORM(xvtdivsp, 0x14, 0x0B, PPC2_VSX),
5cb151ac 10686GEN_XX2FORM(xvtsqrtsp, 0x14, 0x0A, PPC2_VSX),
595c6eef
TM
10687GEN_XX3FORM(xvmaddasp, 0x04, 0x08, PPC2_VSX),
10688GEN_XX3FORM(xvmaddmsp, 0x04, 0x09, PPC2_VSX),
10689GEN_XX3FORM(xvmsubasp, 0x04, 0x0A, PPC2_VSX),
10690GEN_XX3FORM(xvmsubmsp, 0x04, 0x0B, PPC2_VSX),
10691GEN_XX3FORM(xvnmaddasp, 0x04, 0x18, PPC2_VSX),
10692GEN_XX3FORM(xvnmaddmsp, 0x04, 0x19, PPC2_VSX),
10693GEN_XX3FORM(xvnmsubasp, 0x04, 0x1A, PPC2_VSX),
10694GEN_XX3FORM(xvnmsubmsp, 0x04, 0x1B, PPC2_VSX),
959e9c9d
TM
10695GEN_XX3FORM(xvmaxsp, 0x00, 0x18, PPC2_VSX),
10696GEN_XX3FORM(xvminsp, 0x00, 0x19, PPC2_VSX),
354a6dec
TM
10697GEN_XX3_RC_FORM(xvcmpeqsp, 0x0C, 0x08, PPC2_VSX),
10698GEN_XX3_RC_FORM(xvcmpgtsp, 0x0C, 0x09, PPC2_VSX),
10699GEN_XX3_RC_FORM(xvcmpgesp, 0x0C, 0x0A, PPC2_VSX),
ed8ac568 10700GEN_XX2FORM(xvcvspdp, 0x12, 0x1C, PPC2_VSX),
5177d2ca
TM
10701GEN_XX2FORM(xvcvspsxds, 0x10, 0x19, PPC2_VSX),
10702GEN_XX2FORM(xvcvspsxws, 0x10, 0x09, PPC2_VSX),
10703GEN_XX2FORM(xvcvspuxds, 0x10, 0x18, PPC2_VSX),
10704GEN_XX2FORM(xvcvspuxws, 0x10, 0x08, PPC2_VSX),
10705GEN_XX2FORM(xvcvsxdsp, 0x10, 0x1B, PPC2_VSX),
10706GEN_XX2FORM(xvcvuxdsp, 0x10, 0x1A, PPC2_VSX),
10707GEN_XX2FORM(xvcvsxwsp, 0x10, 0x0B, PPC2_VSX),
10708GEN_XX2FORM(xvcvuxwsp, 0x10, 0x0A, PPC2_VSX),
88e33d08
TM
10709GEN_XX2FORM(xvrspi, 0x12, 0x08, PPC2_VSX),
10710GEN_XX2FORM(xvrspic, 0x16, 0x0A, PPC2_VSX),
10711GEN_XX2FORM(xvrspim, 0x12, 0x0B, PPC2_VSX),
10712GEN_XX2FORM(xvrspip, 0x12, 0x0A, PPC2_VSX),
10713GEN_XX2FORM(xvrspiz, 0x12, 0x09, PPC2_VSX),
ee6e02c0 10714
79ca8a6a
TM
10715#undef VSX_LOGICAL
10716#define VSX_LOGICAL(name, opc2, opc3, fl2) \
10717GEN_XX3FORM(name, opc2, opc3, fl2)
10718
10719VSX_LOGICAL(xxland, 0x8, 0x10, PPC2_VSX),
10720VSX_LOGICAL(xxlandc, 0x8, 0x11, PPC2_VSX),
10721VSX_LOGICAL(xxlor, 0x8, 0x12, PPC2_VSX),
10722VSX_LOGICAL(xxlxor, 0x8, 0x13, PPC2_VSX),
10723VSX_LOGICAL(xxlnor, 0x8, 0x14, PPC2_VSX),
67a33f37
TM
10724VSX_LOGICAL(xxleqv, 0x8, 0x17, PPC2_VSX207),
10725VSX_LOGICAL(xxlnand, 0x8, 0x16, PPC2_VSX207),
10726VSX_LOGICAL(xxlorc, 0x8, 0x15, PPC2_VSX207),
ce577d2e
TM
10727GEN_XX3FORM(xxmrghw, 0x08, 0x02, PPC2_VSX),
10728GEN_XX3FORM(xxmrglw, 0x08, 0x06, PPC2_VSX),
76c15fe0 10729GEN_XX2FORM(xxspltw, 0x08, 0x0A, PPC2_VSX),
acc42968 10730GEN_XX3FORM_DM(xxsldwi, 0x08, 0x00),
79ca8a6a 10731
551e3ef7
TM
10732#define GEN_XXSEL_ROW(opc3) \
10733GEN_HANDLER2_E(xxsel, "xxsel", 0x3C, 0x18, opc3, 0, PPC_NONE, PPC2_VSX), \
10734GEN_HANDLER2_E(xxsel, "xxsel", 0x3C, 0x19, opc3, 0, PPC_NONE, PPC2_VSX), \
10735GEN_HANDLER2_E(xxsel, "xxsel", 0x3C, 0x1A, opc3, 0, PPC_NONE, PPC2_VSX), \
10736GEN_HANDLER2_E(xxsel, "xxsel", 0x3C, 0x1B, opc3, 0, PPC_NONE, PPC2_VSX), \
10737GEN_HANDLER2_E(xxsel, "xxsel", 0x3C, 0x1C, opc3, 0, PPC_NONE, PPC2_VSX), \
10738GEN_HANDLER2_E(xxsel, "xxsel", 0x3C, 0x1D, opc3, 0, PPC_NONE, PPC2_VSX), \
10739GEN_HANDLER2_E(xxsel, "xxsel", 0x3C, 0x1E, opc3, 0, PPC_NONE, PPC2_VSX), \
10740GEN_HANDLER2_E(xxsel, "xxsel", 0x3C, 0x1F, opc3, 0, PPC_NONE, PPC2_VSX), \
10741
10742GEN_XXSEL_ROW(0x00)
10743GEN_XXSEL_ROW(0x01)
10744GEN_XXSEL_ROW(0x02)
10745GEN_XXSEL_ROW(0x03)
10746GEN_XXSEL_ROW(0x04)
10747GEN_XXSEL_ROW(0x05)
10748GEN_XXSEL_ROW(0x06)
10749GEN_XXSEL_ROW(0x07)
10750GEN_XXSEL_ROW(0x08)
10751GEN_XXSEL_ROW(0x09)
10752GEN_XXSEL_ROW(0x0A)
10753GEN_XXSEL_ROW(0x0B)
10754GEN_XXSEL_ROW(0x0C)
10755GEN_XXSEL_ROW(0x0D)
10756GEN_XXSEL_ROW(0x0E)
10757GEN_XXSEL_ROW(0x0F)
10758GEN_XXSEL_ROW(0x10)
10759GEN_XXSEL_ROW(0x11)
10760GEN_XXSEL_ROW(0x12)
10761GEN_XXSEL_ROW(0x13)
10762GEN_XXSEL_ROW(0x14)
10763GEN_XXSEL_ROW(0x15)
10764GEN_XXSEL_ROW(0x16)
10765GEN_XXSEL_ROW(0x17)
10766GEN_XXSEL_ROW(0x18)
10767GEN_XXSEL_ROW(0x19)
10768GEN_XXSEL_ROW(0x1A)
10769GEN_XXSEL_ROW(0x1B)
10770GEN_XXSEL_ROW(0x1C)
10771GEN_XXSEL_ROW(0x1D)
10772GEN_XXSEL_ROW(0x1E)
10773GEN_XXSEL_ROW(0x1F)
10774
cd73f2c9
TM
10775GEN_XX3FORM_DM(xxpermdi, 0x08, 0x01),
10776
5c55ff99 10777#undef GEN_SPE
70560da7
FC
10778#define GEN_SPE(name0, name1, opc2, opc3, inval0, inval1, type) \
10779 GEN_OPCODE_DUAL(name0##_##name1, 0x04, opc2, opc3, inval0, inval1, type, PPC_NONE)
10780GEN_SPE(evaddw, speundef, 0x00, 0x08, 0x00000000, 0xFFFFFFFF, PPC_SPE),
10781GEN_SPE(evaddiw, speundef, 0x01, 0x08, 0x00000000, 0xFFFFFFFF, PPC_SPE),
10782GEN_SPE(evsubfw, speundef, 0x02, 0x08, 0x00000000, 0xFFFFFFFF, PPC_SPE),
10783GEN_SPE(evsubifw, speundef, 0x03, 0x08, 0x00000000, 0xFFFFFFFF, PPC_SPE),
10784GEN_SPE(evabs, evneg, 0x04, 0x08, 0x0000F800, 0x0000F800, PPC_SPE),
10785GEN_SPE(evextsb, evextsh, 0x05, 0x08, 0x0000F800, 0x0000F800, PPC_SPE),
10786GEN_SPE(evrndw, evcntlzw, 0x06, 0x08, 0x0000F800, 0x0000F800, PPC_SPE),
10787GEN_SPE(evcntlsw, brinc, 0x07, 0x08, 0x0000F800, 0x00000000, PPC_SPE),
10788GEN_SPE(evmra, speundef, 0x02, 0x13, 0x0000F800, 0xFFFFFFFF, PPC_SPE),
10789GEN_SPE(speundef, evand, 0x08, 0x08, 0xFFFFFFFF, 0x00000000, PPC_SPE),
10790GEN_SPE(evandc, speundef, 0x09, 0x08, 0x00000000, 0xFFFFFFFF, PPC_SPE),
10791GEN_SPE(evxor, evor, 0x0B, 0x08, 0x00000000, 0x00000000, PPC_SPE),
10792GEN_SPE(evnor, eveqv, 0x0C, 0x08, 0x00000000, 0x00000000, PPC_SPE),
10793GEN_SPE(evmwumi, evmwsmi, 0x0C, 0x11, 0x00000000, 0x00000000, PPC_SPE),
10794GEN_SPE(evmwumia, evmwsmia, 0x1C, 0x11, 0x00000000, 0x00000000, PPC_SPE),
10795GEN_SPE(evmwumiaa, evmwsmiaa, 0x0C, 0x15, 0x00000000, 0x00000000, PPC_SPE),
10796GEN_SPE(speundef, evorc, 0x0D, 0x08, 0xFFFFFFFF, 0x00000000, PPC_SPE),
10797GEN_SPE(evnand, speundef, 0x0F, 0x08, 0x00000000, 0xFFFFFFFF, PPC_SPE),
10798GEN_SPE(evsrwu, evsrws, 0x10, 0x08, 0x00000000, 0x00000000, PPC_SPE),
10799GEN_SPE(evsrwiu, evsrwis, 0x11, 0x08, 0x00000000, 0x00000000, PPC_SPE),
10800GEN_SPE(evslw, speundef, 0x12, 0x08, 0x00000000, 0xFFFFFFFF, PPC_SPE),
10801GEN_SPE(evslwi, speundef, 0x13, 0x08, 0x00000000, 0xFFFFFFFF, PPC_SPE),
10802GEN_SPE(evrlw, evsplati, 0x14, 0x08, 0x00000000, 0x0000F800, PPC_SPE),
10803GEN_SPE(evrlwi, evsplatfi, 0x15, 0x08, 0x00000000, 0x0000F800, PPC_SPE),
10804GEN_SPE(evmergehi, evmergelo, 0x16, 0x08, 0x00000000, 0x00000000, PPC_SPE),
10805GEN_SPE(evmergehilo, evmergelohi, 0x17, 0x08, 0x00000000, 0x00000000, PPC_SPE),
10806GEN_SPE(evcmpgtu, evcmpgts, 0x18, 0x08, 0x00600000, 0x00600000, PPC_SPE),
10807GEN_SPE(evcmpltu, evcmplts, 0x19, 0x08, 0x00600000, 0x00600000, PPC_SPE),
10808GEN_SPE(evcmpeq, speundef, 0x1A, 0x08, 0x00600000, 0xFFFFFFFF, PPC_SPE),
10809
10810GEN_SPE(evfsadd, evfssub, 0x00, 0x0A, 0x00000000, 0x00000000, PPC_SPE_SINGLE),
10811GEN_SPE(evfsabs, evfsnabs, 0x02, 0x0A, 0x0000F800, 0x0000F800, PPC_SPE_SINGLE),
10812GEN_SPE(evfsneg, speundef, 0x03, 0x0A, 0x0000F800, 0xFFFFFFFF, PPC_SPE_SINGLE),
10813GEN_SPE(evfsmul, evfsdiv, 0x04, 0x0A, 0x00000000, 0x00000000, PPC_SPE_SINGLE),
10814GEN_SPE(evfscmpgt, evfscmplt, 0x06, 0x0A, 0x00600000, 0x00600000, PPC_SPE_SINGLE),
10815GEN_SPE(evfscmpeq, speundef, 0x07, 0x0A, 0x00600000, 0xFFFFFFFF, PPC_SPE_SINGLE),
10816GEN_SPE(evfscfui, evfscfsi, 0x08, 0x0A, 0x00180000, 0x00180000, PPC_SPE_SINGLE),
10817GEN_SPE(evfscfuf, evfscfsf, 0x09, 0x0A, 0x00180000, 0x00180000, PPC_SPE_SINGLE),
10818GEN_SPE(evfsctui, evfsctsi, 0x0A, 0x0A, 0x00180000, 0x00180000, PPC_SPE_SINGLE),
10819GEN_SPE(evfsctuf, evfsctsf, 0x0B, 0x0A, 0x00180000, 0x00180000, PPC_SPE_SINGLE),
10820GEN_SPE(evfsctuiz, speundef, 0x0C, 0x0A, 0x00180000, 0xFFFFFFFF, PPC_SPE_SINGLE),
10821GEN_SPE(evfsctsiz, speundef, 0x0D, 0x0A, 0x00180000, 0xFFFFFFFF, PPC_SPE_SINGLE),
10822GEN_SPE(evfststgt, evfststlt, 0x0E, 0x0A, 0x00600000, 0x00600000, PPC_SPE_SINGLE),
10823GEN_SPE(evfststeq, speundef, 0x0F, 0x0A, 0x00600000, 0xFFFFFFFF, PPC_SPE_SINGLE),
10824
10825GEN_SPE(efsadd, efssub, 0x00, 0x0B, 0x00000000, 0x00000000, PPC_SPE_SINGLE),
10826GEN_SPE(efsabs, efsnabs, 0x02, 0x0B, 0x0000F800, 0x0000F800, PPC_SPE_SINGLE),
10827GEN_SPE(efsneg, speundef, 0x03, 0x0B, 0x0000F800, 0xFFFFFFFF, PPC_SPE_SINGLE),
10828GEN_SPE(efsmul, efsdiv, 0x04, 0x0B, 0x00000000, 0x00000000, PPC_SPE_SINGLE),
10829GEN_SPE(efscmpgt, efscmplt, 0x06, 0x0B, 0x00600000, 0x00600000, PPC_SPE_SINGLE),
10830GEN_SPE(efscmpeq, efscfd, 0x07, 0x0B, 0x00600000, 0x00180000, PPC_SPE_SINGLE),
10831GEN_SPE(efscfui, efscfsi, 0x08, 0x0B, 0x00180000, 0x00180000, PPC_SPE_SINGLE),
10832GEN_SPE(efscfuf, efscfsf, 0x09, 0x0B, 0x00180000, 0x00180000, PPC_SPE_SINGLE),
10833GEN_SPE(efsctui, efsctsi, 0x0A, 0x0B, 0x00180000, 0x00180000, PPC_SPE_SINGLE),
10834GEN_SPE(efsctuf, efsctsf, 0x0B, 0x0B, 0x00180000, 0x00180000, PPC_SPE_SINGLE),
10835GEN_SPE(efsctuiz, speundef, 0x0C, 0x0B, 0x00180000, 0xFFFFFFFF, PPC_SPE_SINGLE),
10836GEN_SPE(efsctsiz, speundef, 0x0D, 0x0B, 0x00180000, 0xFFFFFFFF, PPC_SPE_SINGLE),
10837GEN_SPE(efststgt, efststlt, 0x0E, 0x0B, 0x00600000, 0x00600000, PPC_SPE_SINGLE),
10838GEN_SPE(efststeq, speundef, 0x0F, 0x0B, 0x00600000, 0xFFFFFFFF, PPC_SPE_SINGLE),
10839
10840GEN_SPE(efdadd, efdsub, 0x10, 0x0B, 0x00000000, 0x00000000, PPC_SPE_DOUBLE),
10841GEN_SPE(efdcfuid, efdcfsid, 0x11, 0x0B, 0x00180000, 0x00180000, PPC_SPE_DOUBLE),
10842GEN_SPE(efdabs, efdnabs, 0x12, 0x0B, 0x0000F800, 0x0000F800, PPC_SPE_DOUBLE),
10843GEN_SPE(efdneg, speundef, 0x13, 0x0B, 0x0000F800, 0xFFFFFFFF, PPC_SPE_DOUBLE),
10844GEN_SPE(efdmul, efddiv, 0x14, 0x0B, 0x00000000, 0x00000000, PPC_SPE_DOUBLE),
10845GEN_SPE(efdctuidz, efdctsidz, 0x15, 0x0B, 0x00180000, 0x00180000, PPC_SPE_DOUBLE),
10846GEN_SPE(efdcmpgt, efdcmplt, 0x16, 0x0B, 0x00600000, 0x00600000, PPC_SPE_DOUBLE),
10847GEN_SPE(efdcmpeq, efdcfs, 0x17, 0x0B, 0x00600000, 0x00180000, PPC_SPE_DOUBLE),
10848GEN_SPE(efdcfui, efdcfsi, 0x18, 0x0B, 0x00180000, 0x00180000, PPC_SPE_DOUBLE),
10849GEN_SPE(efdcfuf, efdcfsf, 0x19, 0x0B, 0x00180000, 0x00180000, PPC_SPE_DOUBLE),
10850GEN_SPE(efdctui, efdctsi, 0x1A, 0x0B, 0x00180000, 0x00180000, PPC_SPE_DOUBLE),
10851GEN_SPE(efdctuf, efdctsf, 0x1B, 0x0B, 0x00180000, 0x00180000, PPC_SPE_DOUBLE),
10852GEN_SPE(efdctuiz, speundef, 0x1C, 0x0B, 0x00180000, 0xFFFFFFFF, PPC_SPE_DOUBLE),
10853GEN_SPE(efdctsiz, speundef, 0x1D, 0x0B, 0x00180000, 0xFFFFFFFF, PPC_SPE_DOUBLE),
10854GEN_SPE(efdtstgt, efdtstlt, 0x1E, 0x0B, 0x00600000, 0x00600000, PPC_SPE_DOUBLE),
10855GEN_SPE(efdtsteq, speundef, 0x1F, 0x0B, 0x00600000, 0xFFFFFFFF, PPC_SPE_DOUBLE),
5c55ff99
BS
10856
10857#undef GEN_SPEOP_LDST
10858#define GEN_SPEOP_LDST(name, opc2, sh) \
10859GEN_HANDLER(name, 0x04, opc2, 0x0C, 0x00000000, PPC_SPE)
10860GEN_SPEOP_LDST(evldd, 0x00, 3),
10861GEN_SPEOP_LDST(evldw, 0x01, 3),
10862GEN_SPEOP_LDST(evldh, 0x02, 3),
10863GEN_SPEOP_LDST(evlhhesplat, 0x04, 1),
10864GEN_SPEOP_LDST(evlhhousplat, 0x06, 1),
10865GEN_SPEOP_LDST(evlhhossplat, 0x07, 1),
10866GEN_SPEOP_LDST(evlwhe, 0x08, 2),
10867GEN_SPEOP_LDST(evlwhou, 0x0A, 2),
10868GEN_SPEOP_LDST(evlwhos, 0x0B, 2),
10869GEN_SPEOP_LDST(evlwwsplat, 0x0C, 2),
10870GEN_SPEOP_LDST(evlwhsplat, 0x0E, 2),
10871
10872GEN_SPEOP_LDST(evstdd, 0x10, 3),
10873GEN_SPEOP_LDST(evstdw, 0x11, 3),
10874GEN_SPEOP_LDST(evstdh, 0x12, 3),
10875GEN_SPEOP_LDST(evstwhe, 0x18, 2),
10876GEN_SPEOP_LDST(evstwho, 0x1A, 2),
10877GEN_SPEOP_LDST(evstwwe, 0x1C, 2),
10878GEN_SPEOP_LDST(evstwwo, 0x1E, 2),
10879};
10880
0411a972 10881#include "helper_regs.h"
a1389542 10882#include "translate_init.c"
79aceca5 10883
9a64fbe4 10884/*****************************************************************************/
3fc6c082 10885/* Misc PowerPC helpers */
878096ee
AF
10886void ppc_cpu_dump_state(CPUState *cs, FILE *f, fprintf_function cpu_fprintf,
10887 int flags)
79aceca5 10888{
3fc6c082
FB
10889#define RGPL 4
10890#define RFPL 4
3fc6c082 10891
878096ee
AF
10892 PowerPCCPU *cpu = POWERPC_CPU(cs);
10893 CPUPPCState *env = &cpu->env;
79aceca5
FB
10894 int i;
10895
90e189ec 10896 cpu_fprintf(f, "NIP " TARGET_FMT_lx " LR " TARGET_FMT_lx " CTR "
9a78eead 10897 TARGET_FMT_lx " XER " TARGET_FMT_lx "\n",
da91a00f 10898 env->nip, env->lr, env->ctr, cpu_read_xer(env));
90e189ec
BS
10899 cpu_fprintf(f, "MSR " TARGET_FMT_lx " HID0 " TARGET_FMT_lx " HF "
10900 TARGET_FMT_lx " idx %d\n", env->msr, env->spr[SPR_HID0],
10901 env->hflags, env->mmu_idx);
d9bce9d9 10902#if !defined(NO_TIMER_DUMP)
9a78eead 10903 cpu_fprintf(f, "TB %08" PRIu32 " %08" PRIu64
76a66253 10904#if !defined(CONFIG_USER_ONLY)
9a78eead 10905 " DECR %08" PRIu32
76a66253
JM
10906#endif
10907 "\n",
077fc206 10908 cpu_ppc_load_tbu(env), cpu_ppc_load_tbl(env)
76a66253
JM
10909#if !defined(CONFIG_USER_ONLY)
10910 , cpu_ppc_load_decr(env)
10911#endif
10912 );
077fc206 10913#endif
76a66253 10914 for (i = 0; i < 32; i++) {
3fc6c082
FB
10915 if ((i & (RGPL - 1)) == 0)
10916 cpu_fprintf(f, "GPR%02d", i);
b11ebf64 10917 cpu_fprintf(f, " %016" PRIx64, ppc_dump_gpr(env, i));
3fc6c082 10918 if ((i & (RGPL - 1)) == (RGPL - 1))
7fe48483 10919 cpu_fprintf(f, "\n");
76a66253 10920 }
3fc6c082 10921 cpu_fprintf(f, "CR ");
76a66253 10922 for (i = 0; i < 8; i++)
7fe48483
FB
10923 cpu_fprintf(f, "%01x", env->crf[i]);
10924 cpu_fprintf(f, " [");
76a66253
JM
10925 for (i = 0; i < 8; i++) {
10926 char a = '-';
10927 if (env->crf[i] & 0x08)
10928 a = 'L';
10929 else if (env->crf[i] & 0x04)
10930 a = 'G';
10931 else if (env->crf[i] & 0x02)
10932 a = 'E';
7fe48483 10933 cpu_fprintf(f, " %c%c", a, env->crf[i] & 0x01 ? 'O' : ' ');
76a66253 10934 }
90e189ec
BS
10935 cpu_fprintf(f, " ] RES " TARGET_FMT_lx "\n",
10936 env->reserve_addr);
3fc6c082
FB
10937 for (i = 0; i < 32; i++) {
10938 if ((i & (RFPL - 1)) == 0)
10939 cpu_fprintf(f, "FPR%02d", i);
26a76461 10940 cpu_fprintf(f, " %016" PRIx64, *((uint64_t *)&env->fpr[i]));
3fc6c082 10941 if ((i & (RFPL - 1)) == (RFPL - 1))
7fe48483 10942 cpu_fprintf(f, "\n");
79aceca5 10943 }
30304420 10944 cpu_fprintf(f, "FPSCR " TARGET_FMT_lx "\n", env->fpscr);
f2e63a42 10945#if !defined(CONFIG_USER_ONLY)
90dc8812
SW
10946 cpu_fprintf(f, " SRR0 " TARGET_FMT_lx " SRR1 " TARGET_FMT_lx
10947 " PVR " TARGET_FMT_lx " VRSAVE " TARGET_FMT_lx "\n",
10948 env->spr[SPR_SRR0], env->spr[SPR_SRR1],
10949 env->spr[SPR_PVR], env->spr[SPR_VRSAVE]);
10950
10951 cpu_fprintf(f, "SPRG0 " TARGET_FMT_lx " SPRG1 " TARGET_FMT_lx
10952 " SPRG2 " TARGET_FMT_lx " SPRG3 " TARGET_FMT_lx "\n",
10953 env->spr[SPR_SPRG0], env->spr[SPR_SPRG1],
10954 env->spr[SPR_SPRG2], env->spr[SPR_SPRG3]);
10955
10956 cpu_fprintf(f, "SPRG4 " TARGET_FMT_lx " SPRG5 " TARGET_FMT_lx
10957 " SPRG6 " TARGET_FMT_lx " SPRG7 " TARGET_FMT_lx "\n",
10958 env->spr[SPR_SPRG4], env->spr[SPR_SPRG5],
10959 env->spr[SPR_SPRG6], env->spr[SPR_SPRG7]);
10960
10961 if (env->excp_model == POWERPC_EXCP_BOOKE) {
10962 cpu_fprintf(f, "CSRR0 " TARGET_FMT_lx " CSRR1 " TARGET_FMT_lx
10963 " MCSRR0 " TARGET_FMT_lx " MCSRR1 " TARGET_FMT_lx "\n",
10964 env->spr[SPR_BOOKE_CSRR0], env->spr[SPR_BOOKE_CSRR1],
10965 env->spr[SPR_BOOKE_MCSRR0], env->spr[SPR_BOOKE_MCSRR1]);
10966
10967 cpu_fprintf(f, " TCR " TARGET_FMT_lx " TSR " TARGET_FMT_lx
10968 " ESR " TARGET_FMT_lx " DEAR " TARGET_FMT_lx "\n",
10969 env->spr[SPR_BOOKE_TCR], env->spr[SPR_BOOKE_TSR],
10970 env->spr[SPR_BOOKE_ESR], env->spr[SPR_BOOKE_DEAR]);
10971
10972 cpu_fprintf(f, " PIR " TARGET_FMT_lx " DECAR " TARGET_FMT_lx
10973 " IVPR " TARGET_FMT_lx " EPCR " TARGET_FMT_lx "\n",
10974 env->spr[SPR_BOOKE_PIR], env->spr[SPR_BOOKE_DECAR],
10975 env->spr[SPR_BOOKE_IVPR], env->spr[SPR_BOOKE_EPCR]);
10976
10977 cpu_fprintf(f, " MCSR " TARGET_FMT_lx " SPRG8 " TARGET_FMT_lx
10978 " EPR " TARGET_FMT_lx "\n",
10979 env->spr[SPR_BOOKE_MCSR], env->spr[SPR_BOOKE_SPRG8],
10980 env->spr[SPR_BOOKE_EPR]);
10981
10982 /* FSL-specific */
10983 cpu_fprintf(f, " MCAR " TARGET_FMT_lx " PID1 " TARGET_FMT_lx
10984 " PID2 " TARGET_FMT_lx " SVR " TARGET_FMT_lx "\n",
10985 env->spr[SPR_Exxx_MCAR], env->spr[SPR_BOOKE_PID1],
10986 env->spr[SPR_BOOKE_PID2], env->spr[SPR_E500_SVR]);
10987
10988 /*
10989 * IVORs are left out as they are large and do not change often --
10990 * they can be read with "p $ivor0", "p $ivor1", etc.
10991 */
10992 }
10993
697ab892
DG
10994#if defined(TARGET_PPC64)
10995 if (env->flags & POWERPC_FLAG_CFAR) {
10996 cpu_fprintf(f, " CFAR " TARGET_FMT_lx"\n", env->cfar);
10997 }
10998#endif
10999
90dc8812
SW
11000 switch (env->mmu_model) {
11001 case POWERPC_MMU_32B:
11002 case POWERPC_MMU_601:
11003 case POWERPC_MMU_SOFT_6xx:
11004 case POWERPC_MMU_SOFT_74xx:
11005#if defined(TARGET_PPC64)
90dc8812 11006 case POWERPC_MMU_64B:
ca480de6
AB
11007 case POWERPC_MMU_2_06:
11008 case POWERPC_MMU_2_06a:
11009 case POWERPC_MMU_2_06d:
90dc8812 11010#endif
ca480de6
AB
11011 cpu_fprintf(f, " SDR1 " TARGET_FMT_lx " DAR " TARGET_FMT_lx
11012 " DSISR " TARGET_FMT_lx "\n", env->spr[SPR_SDR1],
11013 env->spr[SPR_DAR], env->spr[SPR_DSISR]);
90dc8812 11014 break;
01662f3e 11015 case POWERPC_MMU_BOOKE206:
90dc8812
SW
11016 cpu_fprintf(f, " MAS0 " TARGET_FMT_lx " MAS1 " TARGET_FMT_lx
11017 " MAS2 " TARGET_FMT_lx " MAS3 " TARGET_FMT_lx "\n",
11018 env->spr[SPR_BOOKE_MAS0], env->spr[SPR_BOOKE_MAS1],
11019 env->spr[SPR_BOOKE_MAS2], env->spr[SPR_BOOKE_MAS3]);
11020
11021 cpu_fprintf(f, " MAS4 " TARGET_FMT_lx " MAS6 " TARGET_FMT_lx
11022 " MAS7 " TARGET_FMT_lx " PID " TARGET_FMT_lx "\n",
11023 env->spr[SPR_BOOKE_MAS4], env->spr[SPR_BOOKE_MAS6],
11024 env->spr[SPR_BOOKE_MAS7], env->spr[SPR_BOOKE_PID]);
11025
11026 cpu_fprintf(f, "MMUCFG " TARGET_FMT_lx " TLB0CFG " TARGET_FMT_lx
11027 " TLB1CFG " TARGET_FMT_lx "\n",
11028 env->spr[SPR_MMUCFG], env->spr[SPR_BOOKE_TLB0CFG],
11029 env->spr[SPR_BOOKE_TLB1CFG]);
11030 break;
11031 default:
11032 break;
11033 }
f2e63a42 11034#endif
79aceca5 11035
3fc6c082
FB
11036#undef RGPL
11037#undef RFPL
79aceca5
FB
11038}
11039
878096ee
AF
11040void ppc_cpu_dump_statistics(CPUState *cs, FILE*f,
11041 fprintf_function cpu_fprintf, int flags)
76a66253
JM
11042{
11043#if defined(DO_PPC_STATISTICS)
878096ee 11044 PowerPCCPU *cpu = POWERPC_CPU(cs);
c227f099 11045 opc_handler_t **t1, **t2, **t3, *handler;
76a66253
JM
11046 int op1, op2, op3;
11047
878096ee 11048 t1 = cpu->env.opcodes;
76a66253
JM
11049 for (op1 = 0; op1 < 64; op1++) {
11050 handler = t1[op1];
11051 if (is_indirect_opcode(handler)) {
11052 t2 = ind_table(handler);
11053 for (op2 = 0; op2 < 32; op2++) {
11054 handler = t2[op2];
11055 if (is_indirect_opcode(handler)) {
11056 t3 = ind_table(handler);
11057 for (op3 = 0; op3 < 32; op3++) {
11058 handler = t3[op3];
11059 if (handler->count == 0)
11060 continue;
11061 cpu_fprintf(f, "%02x %02x %02x (%02x %04d) %16s: "
0bfcd599 11062 "%016" PRIx64 " %" PRId64 "\n",
76a66253
JM
11063 op1, op2, op3, op1, (op3 << 5) | op2,
11064 handler->oname,
11065 handler->count, handler->count);
11066 }
11067 } else {
11068 if (handler->count == 0)
11069 continue;
11070 cpu_fprintf(f, "%02x %02x (%02x %04d) %16s: "
0bfcd599 11071 "%016" PRIx64 " %" PRId64 "\n",
76a66253
JM
11072 op1, op2, op1, op2, handler->oname,
11073 handler->count, handler->count);
11074 }
11075 }
11076 } else {
11077 if (handler->count == 0)
11078 continue;
0bfcd599
BS
11079 cpu_fprintf(f, "%02x (%02x ) %16s: %016" PRIx64
11080 " %" PRId64 "\n",
76a66253
JM
11081 op1, op1, handler->oname,
11082 handler->count, handler->count);
11083 }
11084 }
11085#endif
11086}
11087
9a64fbe4 11088/*****************************************************************************/
213fe1f5 11089static inline void gen_intermediate_code_internal(PowerPCCPU *cpu,
636aa200 11090 TranslationBlock *tb,
213fe1f5 11091 bool search_pc)
79aceca5 11092{
ed2803da 11093 CPUState *cs = CPU(cpu);
213fe1f5 11094 CPUPPCState *env = &cpu->env;
9fddaa0c 11095 DisasContext ctx, *ctxp = &ctx;
c227f099 11096 opc_handler_t **table, *handler;
0fa85d43 11097 target_ulong pc_start;
79aceca5 11098 uint16_t *gen_opc_end;
a1d1bb31 11099 CPUBreakpoint *bp;
79aceca5 11100 int j, lj = -1;
2e70f6ef
PB
11101 int num_insns;
11102 int max_insns;
79aceca5
FB
11103
11104 pc_start = tb->pc;
92414b31 11105 gen_opc_end = tcg_ctx.gen_opc_buf + OPC_MAX_SIZE;
046d6672 11106 ctx.nip = pc_start;
79aceca5 11107 ctx.tb = tb;
e1833e1f 11108 ctx.exception = POWERPC_EXCP_NONE;
3fc6c082 11109 ctx.spr_cb = env->spr_cb;
76db3ba4 11110 ctx.mem_idx = env->mmu_idx;
7d08d856
AJ
11111 ctx.insns_flags = env->insns_flags;
11112 ctx.insns_flags2 = env->insns_flags2;
76db3ba4
AJ
11113 ctx.access_type = -1;
11114 ctx.le_mode = env->hflags & (1 << MSR_LE) ? 1 : 0;
d9bce9d9 11115#if defined(TARGET_PPC64)
e42a61f1 11116 ctx.sf_mode = msr_is_64bit(env, env->msr);
697ab892 11117 ctx.has_cfar = !!(env->flags & POWERPC_FLAG_CFAR);
9a64fbe4 11118#endif
3cc62370 11119 ctx.fpu_enabled = msr_fp;
a9d9eb8f 11120 if ((env->flags & POWERPC_FLAG_SPE) && msr_spe)
d26bfc9a
JM
11121 ctx.spe_enabled = msr_spe;
11122 else
11123 ctx.spe_enabled = 0;
a9d9eb8f
JM
11124 if ((env->flags & POWERPC_FLAG_VRE) && msr_vr)
11125 ctx.altivec_enabled = msr_vr;
11126 else
11127 ctx.altivec_enabled = 0;
1f29871c
TM
11128 if ((env->flags & POWERPC_FLAG_VSX) && msr_vsx) {
11129 ctx.vsx_enabled = msr_vsx;
11130 } else {
11131 ctx.vsx_enabled = 0;
11132 }
d26bfc9a 11133 if ((env->flags & POWERPC_FLAG_SE) && msr_se)
8cbcb4fa 11134 ctx.singlestep_enabled = CPU_SINGLE_STEP;
d26bfc9a 11135 else
8cbcb4fa 11136 ctx.singlestep_enabled = 0;
d26bfc9a 11137 if ((env->flags & POWERPC_FLAG_BE) && msr_be)
8cbcb4fa 11138 ctx.singlestep_enabled |= CPU_BRANCH_STEP;
ed2803da 11139 if (unlikely(cs->singlestep_enabled)) {
8cbcb4fa 11140 ctx.singlestep_enabled |= GDBSTUB_SINGLE_STEP;
ed2803da 11141 }
3fc6c082 11142#if defined (DO_SINGLE_STEP) && 0
9a64fbe4
FB
11143 /* Single step trace mode */
11144 msr_se = 1;
11145#endif
2e70f6ef
PB
11146 num_insns = 0;
11147 max_insns = tb->cflags & CF_COUNT_MASK;
11148 if (max_insns == 0)
11149 max_insns = CF_COUNT_MASK;
11150
806f352d 11151 gen_tb_start();
9a64fbe4 11152 /* Set env in case of segfault during code fetch */
efd7f486
EV
11153 while (ctx.exception == POWERPC_EXCP_NONE
11154 && tcg_ctx.gen_opc_ptr < gen_opc_end) {
72cf2d4f
BS
11155 if (unlikely(!QTAILQ_EMPTY(&env->breakpoints))) {
11156 QTAILQ_FOREACH(bp, &env->breakpoints, entry) {
a1d1bb31 11157 if (bp->pc == ctx.nip) {
e06fcd75 11158 gen_debug_exception(ctxp);
ea4e754f
FB
11159 break;
11160 }
11161 }
11162 }
76a66253 11163 if (unlikely(search_pc)) {
92414b31 11164 j = tcg_ctx.gen_opc_ptr - tcg_ctx.gen_opc_buf;
79aceca5
FB
11165 if (lj < j) {
11166 lj++;
11167 while (lj < j)
ab1103de 11168 tcg_ctx.gen_opc_instr_start[lj++] = 0;
79aceca5 11169 }
25983cad 11170 tcg_ctx.gen_opc_pc[lj] = ctx.nip;
ab1103de 11171 tcg_ctx.gen_opc_instr_start[lj] = 1;
c9c99c22 11172 tcg_ctx.gen_opc_icount[lj] = num_insns;
79aceca5 11173 }
d12d51d5 11174 LOG_DISAS("----------------\n");
90e189ec 11175 LOG_DISAS("nip=" TARGET_FMT_lx " super=%d ir=%d\n",
d12d51d5 11176 ctx.nip, ctx.mem_idx, (int)msr_ir);
2e70f6ef
PB
11177 if (num_insns + 1 == max_insns && (tb->cflags & CF_LAST_IO))
11178 gen_io_start();
76db3ba4 11179 if (unlikely(ctx.le_mode)) {
2f5a189c 11180 ctx.opcode = bswap32(cpu_ldl_code(env, ctx.nip));
056401ea 11181 } else {
2f5a189c 11182 ctx.opcode = cpu_ldl_code(env, ctx.nip);
111bfab3 11183 }
d12d51d5 11184 LOG_DISAS("translate opcode %08x (%02x %02x %02x) (%s)\n",
9a64fbe4 11185 ctx.opcode, opc1(ctx.opcode), opc2(ctx.opcode),
476b6d16 11186 opc3(ctx.opcode), ctx.le_mode ? "little" : "big");
fdefe51c 11187 if (unlikely(qemu_loglevel_mask(CPU_LOG_TB_OP | CPU_LOG_TB_OP_OPT))) {
731c54f8 11188 tcg_gen_debug_insn_start(ctx.nip);
fdefe51c 11189 }
046d6672 11190 ctx.nip += 4;
3fc6c082 11191 table = env->opcodes;
2e70f6ef 11192 num_insns++;
79aceca5
FB
11193 handler = table[opc1(ctx.opcode)];
11194 if (is_indirect_opcode(handler)) {
11195 table = ind_table(handler);
11196 handler = table[opc2(ctx.opcode)];
11197 if (is_indirect_opcode(handler)) {
11198 table = ind_table(handler);
11199 handler = table[opc3(ctx.opcode)];
11200 }
11201 }
11202 /* Is opcode *REALLY* valid ? */
76a66253 11203 if (unlikely(handler->handler == &gen_invalid)) {
93fcfe39
AL
11204 if (qemu_log_enabled()) {
11205 qemu_log("invalid/unsupported opcode: "
90e189ec
BS
11206 "%02x - %02x - %02x (%08x) " TARGET_FMT_lx " %d\n",
11207 opc1(ctx.opcode), opc2(ctx.opcode),
11208 opc3(ctx.opcode), ctx.opcode, ctx.nip - 4, (int)msr_ir);
4b3686fa 11209 }
76a66253 11210 } else {
70560da7
FC
11211 uint32_t inval;
11212
11213 if (unlikely(handler->type & (PPC_SPE | PPC_SPE_SINGLE | PPC_SPE_DOUBLE) && Rc(ctx.opcode))) {
11214 inval = handler->inval2;
11215 } else {
11216 inval = handler->inval1;
11217 }
11218
11219 if (unlikely((ctx.opcode & inval) != 0)) {
93fcfe39
AL
11220 if (qemu_log_enabled()) {
11221 qemu_log("invalid bits: %08x for opcode: "
90e189ec 11222 "%02x - %02x - %02x (%08x) " TARGET_FMT_lx "\n",
70560da7 11223 ctx.opcode & inval, opc1(ctx.opcode),
90e189ec
BS
11224 opc2(ctx.opcode), opc3(ctx.opcode),
11225 ctx.opcode, ctx.nip - 4);
76a66253 11226 }
e06fcd75 11227 gen_inval_exception(ctxp, POWERPC_EXCP_INVAL_INVAL);
4b3686fa 11228 break;
79aceca5 11229 }
79aceca5 11230 }
4b3686fa 11231 (*(handler->handler))(&ctx);
76a66253
JM
11232#if defined(DO_PPC_STATISTICS)
11233 handler->count++;
11234#endif
9a64fbe4 11235 /* Check trace mode exceptions */
8cbcb4fa
AJ
11236 if (unlikely(ctx.singlestep_enabled & CPU_SINGLE_STEP &&
11237 (ctx.nip <= 0x100 || ctx.nip > 0xF00) &&
11238 ctx.exception != POWERPC_SYSCALL &&
11239 ctx.exception != POWERPC_EXCP_TRAP &&
11240 ctx.exception != POWERPC_EXCP_BRANCH)) {
e06fcd75 11241 gen_exception(ctxp, POWERPC_EXCP_TRACE);
d26bfc9a 11242 } else if (unlikely(((ctx.nip & (TARGET_PAGE_SIZE - 1)) == 0) ||
ed2803da 11243 (cs->singlestep_enabled) ||
1b530a6d 11244 singlestep ||
2e70f6ef 11245 num_insns >= max_insns)) {
d26bfc9a
JM
11246 /* if we reach a page boundary or are single stepping, stop
11247 * generation
11248 */
8dd4983c 11249 break;
76a66253 11250 }
3fc6c082 11251 }
2e70f6ef
PB
11252 if (tb->cflags & CF_LAST_IO)
11253 gen_io_end();
e1833e1f 11254 if (ctx.exception == POWERPC_EXCP_NONE) {
c1942362 11255 gen_goto_tb(&ctx, 0, ctx.nip);
e1833e1f 11256 } else if (ctx.exception != POWERPC_EXCP_BRANCH) {
ed2803da 11257 if (unlikely(cs->singlestep_enabled)) {
e06fcd75 11258 gen_debug_exception(ctxp);
8cbcb4fa 11259 }
76a66253 11260 /* Generate the return instruction */
57fec1fe 11261 tcg_gen_exit_tb(0);
9a64fbe4 11262 }
806f352d 11263 gen_tb_end(tb, num_insns);
efd7f486 11264 *tcg_ctx.gen_opc_ptr = INDEX_op_end;
76a66253 11265 if (unlikely(search_pc)) {
92414b31 11266 j = tcg_ctx.gen_opc_ptr - tcg_ctx.gen_opc_buf;
9a64fbe4
FB
11267 lj++;
11268 while (lj <= j)
ab1103de 11269 tcg_ctx.gen_opc_instr_start[lj++] = 0;
9a64fbe4 11270 } else {
046d6672 11271 tb->size = ctx.nip - pc_start;
2e70f6ef 11272 tb->icount = num_insns;
9a64fbe4 11273 }
d9bce9d9 11274#if defined(DEBUG_DISAS)
8fec2b8c 11275 if (qemu_loglevel_mask(CPU_LOG_TB_IN_ASM)) {
76a66253 11276 int flags;
237c0af0 11277 flags = env->bfd_mach;
76db3ba4 11278 flags |= ctx.le_mode << 16;
93fcfe39 11279 qemu_log("IN: %s\n", lookup_symbol(pc_start));
f4359b9f 11280 log_target_disas(env, pc_start, ctx.nip - pc_start, flags);
93fcfe39 11281 qemu_log("\n");
9fddaa0c 11282 }
79aceca5 11283#endif
79aceca5
FB
11284}
11285
1328c2bf 11286void gen_intermediate_code (CPUPPCState *env, struct TranslationBlock *tb)
79aceca5 11287{
213fe1f5 11288 gen_intermediate_code_internal(ppc_env_get_cpu(env), tb, false);
79aceca5
FB
11289}
11290
1328c2bf 11291void gen_intermediate_code_pc (CPUPPCState *env, struct TranslationBlock *tb)
79aceca5 11292{
213fe1f5 11293 gen_intermediate_code_internal(ppc_env_get_cpu(env), tb, true);
79aceca5 11294}
d2856f1a 11295
1328c2bf 11296void restore_state_to_opc(CPUPPCState *env, TranslationBlock *tb, int pc_pos)
d2856f1a 11297{
25983cad 11298 env->nip = tcg_ctx.gen_opc_pc[pc_pos];
d2856f1a 11299}
This page took 2.925644 seconds and 4 git commands to generate.