]> Git Repo - qemu.git/blame - target-ppc/translate.c
target-ppc: Scalar Round to Single Precision
[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);
390/* Destination */
391EXTRACT_HELPER(rD, 21, 5);
392/* Source */
393EXTRACT_HELPER(rS, 21, 5);
394/* First operand */
395EXTRACT_HELPER(rA, 16, 5);
396/* Second operand */
397EXTRACT_HELPER(rB, 11, 5);
398/* Third operand */
399EXTRACT_HELPER(rC, 6, 5);
400/*** Get CRn ***/
401EXTRACT_HELPER(crfD, 23, 3);
402EXTRACT_HELPER(crfS, 18, 3);
403EXTRACT_HELPER(crbD, 21, 5);
404EXTRACT_HELPER(crbA, 16, 5);
405EXTRACT_HELPER(crbB, 11, 5);
406/* SPR / TBL */
3fc6c082 407EXTRACT_HELPER(_SPR, 11, 10);
636aa200 408static inline uint32_t SPR(uint32_t opcode)
3fc6c082
FB
409{
410 uint32_t sprn = _SPR(opcode);
411
412 return ((sprn >> 5) & 0x1F) | ((sprn & 0x1F) << 5);
413}
79aceca5
FB
414/*** Get constants ***/
415EXTRACT_HELPER(IMM, 12, 8);
416/* 16 bits signed immediate value */
417EXTRACT_SHELPER(SIMM, 0, 16);
418/* 16 bits unsigned immediate value */
419EXTRACT_HELPER(UIMM, 0, 16);
21d21583
AJ
420/* 5 bits signed immediate value */
421EXTRACT_HELPER(SIMM5, 16, 5);
27a4edb3
AJ
422/* 5 bits signed immediate value */
423EXTRACT_HELPER(UIMM5, 16, 5);
79aceca5
FB
424/* Bit count */
425EXTRACT_HELPER(NB, 11, 5);
426/* Shift count */
427EXTRACT_HELPER(SH, 11, 5);
cd633b10
AJ
428/* Vector shift count */
429EXTRACT_HELPER(VSH, 6, 4);
79aceca5
FB
430/* Mask start */
431EXTRACT_HELPER(MB, 6, 5);
432/* Mask end */
433EXTRACT_HELPER(ME, 1, 5);
fb0eaffc
FB
434/* Trap operand */
435EXTRACT_HELPER(TO, 21, 5);
79aceca5
FB
436
437EXTRACT_HELPER(CRM, 12, 8);
79aceca5 438EXTRACT_HELPER(SR, 16, 4);
7d08d856
AJ
439
440/* mtfsf/mtfsfi */
779f6590 441EXTRACT_HELPER(FPBF, 23, 3);
e4bb997e 442EXTRACT_HELPER(FPIMM, 12, 4);
779f6590 443EXTRACT_HELPER(FPL, 25, 1);
7d08d856
AJ
444EXTRACT_HELPER(FPFLM, 17, 8);
445EXTRACT_HELPER(FPW, 16, 1);
fb0eaffc 446
79aceca5
FB
447/*** Jump target decoding ***/
448/* Displacement */
449EXTRACT_SHELPER(d, 0, 16);
450/* Immediate address */
636aa200 451static inline target_ulong LI(uint32_t opcode)
79aceca5
FB
452{
453 return (opcode >> 0) & 0x03FFFFFC;
454}
455
636aa200 456static inline uint32_t BD(uint32_t opcode)
79aceca5
FB
457{
458 return (opcode >> 0) & 0xFFFC;
459}
460
461EXTRACT_HELPER(BO, 21, 5);
462EXTRACT_HELPER(BI, 16, 5);
463/* Absolute/relative address */
464EXTRACT_HELPER(AA, 1, 1);
465/* Link */
466EXTRACT_HELPER(LK, 0, 1);
467
468/* Create a mask between <start> and <end> bits */
636aa200 469static inline target_ulong MASK(uint32_t start, uint32_t end)
79aceca5 470{
76a66253 471 target_ulong ret;
79aceca5 472
76a66253
JM
473#if defined(TARGET_PPC64)
474 if (likely(start == 0)) {
6f2d8978 475 ret = UINT64_MAX << (63 - end);
76a66253 476 } else if (likely(end == 63)) {
6f2d8978 477 ret = UINT64_MAX >> start;
76a66253
JM
478 }
479#else
480 if (likely(start == 0)) {
6f2d8978 481 ret = UINT32_MAX << (31 - end);
76a66253 482 } else if (likely(end == 31)) {
6f2d8978 483 ret = UINT32_MAX >> start;
76a66253
JM
484 }
485#endif
486 else {
487 ret = (((target_ulong)(-1ULL)) >> (start)) ^
488 (((target_ulong)(-1ULL) >> (end)) >> 1);
489 if (unlikely(start > end))
490 return ~ret;
491 }
79aceca5
FB
492
493 return ret;
494}
495
f9fc6d81
TM
496EXTRACT_HELPER_SPLIT(xT, 0, 1, 21, 5);
497EXTRACT_HELPER_SPLIT(xS, 0, 1, 21, 5);
498EXTRACT_HELPER_SPLIT(xA, 2, 1, 16, 5);
499EXTRACT_HELPER_SPLIT(xB, 1, 1, 11, 5);
551e3ef7 500EXTRACT_HELPER_SPLIT(xC, 3, 1, 6, 5);
f9fc6d81 501EXTRACT_HELPER(DM, 8, 2);
76c15fe0 502EXTRACT_HELPER(UIM, 16, 2);
acc42968 503EXTRACT_HELPER(SHW, 8, 2);
a750fc0b 504/*****************************************************************************/
a750fc0b 505/* PowerPC instructions table */
933dc6eb 506
76a66253 507#if defined(DO_PPC_STATISTICS)
a5858d7a 508#define GEN_OPCODE(name, op1, op2, op3, invl, _typ, _typ2) \
5c55ff99 509{ \
79aceca5
FB
510 .opc1 = op1, \
511 .opc2 = op2, \
512 .opc3 = op3, \
18fba28c 513 .pad = { 0, }, \
79aceca5 514 .handler = { \
70560da7
FC
515 .inval1 = invl, \
516 .type = _typ, \
517 .type2 = _typ2, \
518 .handler = &gen_##name, \
519 .oname = stringify(name), \
520 }, \
521 .oname = stringify(name), \
522}
523#define GEN_OPCODE_DUAL(name, op1, op2, op3, invl1, invl2, _typ, _typ2) \
524{ \
525 .opc1 = op1, \
526 .opc2 = op2, \
527 .opc3 = op3, \
528 .pad = { 0, }, \
529 .handler = { \
530 .inval1 = invl1, \
531 .inval2 = invl2, \
9a64fbe4 532 .type = _typ, \
a5858d7a 533 .type2 = _typ2, \
79aceca5 534 .handler = &gen_##name, \
76a66253 535 .oname = stringify(name), \
79aceca5 536 }, \
3fc6c082 537 .oname = stringify(name), \
79aceca5 538}
a5858d7a 539#define GEN_OPCODE2(name, onam, op1, op2, op3, invl, _typ, _typ2) \
5c55ff99 540{ \
c7697e1f
JM
541 .opc1 = op1, \
542 .opc2 = op2, \
543 .opc3 = op3, \
544 .pad = { 0, }, \
545 .handler = { \
70560da7 546 .inval1 = invl, \
c7697e1f 547 .type = _typ, \
a5858d7a 548 .type2 = _typ2, \
c7697e1f
JM
549 .handler = &gen_##name, \
550 .oname = onam, \
551 }, \
552 .oname = onam, \
553}
76a66253 554#else
a5858d7a 555#define GEN_OPCODE(name, op1, op2, op3, invl, _typ, _typ2) \
5c55ff99 556{ \
c7697e1f
JM
557 .opc1 = op1, \
558 .opc2 = op2, \
559 .opc3 = op3, \
560 .pad = { 0, }, \
561 .handler = { \
70560da7
FC
562 .inval1 = invl, \
563 .type = _typ, \
564 .type2 = _typ2, \
565 .handler = &gen_##name, \
566 }, \
567 .oname = stringify(name), \
568}
569#define GEN_OPCODE_DUAL(name, op1, op2, op3, invl1, invl2, _typ, _typ2) \
570{ \
571 .opc1 = op1, \
572 .opc2 = op2, \
573 .opc3 = op3, \
574 .pad = { 0, }, \
575 .handler = { \
576 .inval1 = invl1, \
577 .inval2 = invl2, \
c7697e1f 578 .type = _typ, \
a5858d7a 579 .type2 = _typ2, \
c7697e1f 580 .handler = &gen_##name, \
5c55ff99
BS
581 }, \
582 .oname = stringify(name), \
583}
a5858d7a 584#define GEN_OPCODE2(name, onam, op1, op2, op3, invl, _typ, _typ2) \
5c55ff99
BS
585{ \
586 .opc1 = op1, \
587 .opc2 = op2, \
588 .opc3 = op3, \
589 .pad = { 0, }, \
590 .handler = { \
70560da7 591 .inval1 = invl, \
5c55ff99 592 .type = _typ, \
a5858d7a 593 .type2 = _typ2, \
5c55ff99
BS
594 .handler = &gen_##name, \
595 }, \
596 .oname = onam, \
597}
598#endif
2e610050 599
5c55ff99 600/* SPR load/store helpers */
636aa200 601static inline void gen_load_spr(TCGv t, int reg)
5c55ff99 602{
1328c2bf 603 tcg_gen_ld_tl(t, cpu_env, offsetof(CPUPPCState, spr[reg]));
5c55ff99 604}
2e610050 605
636aa200 606static inline void gen_store_spr(int reg, TCGv t)
5c55ff99 607{
1328c2bf 608 tcg_gen_st_tl(t, cpu_env, offsetof(CPUPPCState, spr[reg]));
5c55ff99 609}
2e610050 610
54623277 611/* Invalid instruction */
99e300ef 612static void gen_invalid(DisasContext *ctx)
9a64fbe4 613{
e06fcd75 614 gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL);
9a64fbe4
FB
615}
616
c227f099 617static opc_handler_t invalid_handler = {
70560da7
FC
618 .inval1 = 0xFFFFFFFF,
619 .inval2 = 0xFFFFFFFF,
9a64fbe4 620 .type = PPC_NONE,
a5858d7a 621 .type2 = PPC_NONE,
79aceca5
FB
622 .handler = gen_invalid,
623};
624
e1571908
AJ
625/*** Integer comparison ***/
626
636aa200 627static inline void gen_op_cmp(TCGv arg0, TCGv arg1, int s, int crf)
e1571908 628{
2fdcb629
RH
629 TCGv t0 = tcg_temp_new();
630 TCGv_i32 t1 = tcg_temp_new_i32();
e1571908 631
da91a00f 632 tcg_gen_trunc_tl_i32(cpu_crf[crf], cpu_so);
e1571908 633
2fdcb629
RH
634 tcg_gen_setcond_tl((s ? TCG_COND_LT: TCG_COND_LTU), t0, arg0, arg1);
635 tcg_gen_trunc_tl_i32(t1, t0);
636 tcg_gen_shli_i32(t1, t1, CRF_LT);
637 tcg_gen_or_i32(cpu_crf[crf], cpu_crf[crf], t1);
638
639 tcg_gen_setcond_tl((s ? TCG_COND_GT: TCG_COND_GTU), t0, arg0, arg1);
640 tcg_gen_trunc_tl_i32(t1, t0);
641 tcg_gen_shli_i32(t1, t1, CRF_GT);
642 tcg_gen_or_i32(cpu_crf[crf], cpu_crf[crf], t1);
643
644 tcg_gen_setcond_tl(TCG_COND_EQ, t0, arg0, arg1);
645 tcg_gen_trunc_tl_i32(t1, t0);
646 tcg_gen_shli_i32(t1, t1, CRF_EQ);
647 tcg_gen_or_i32(cpu_crf[crf], cpu_crf[crf], t1);
648
649 tcg_temp_free(t0);
650 tcg_temp_free_i32(t1);
e1571908
AJ
651}
652
636aa200 653static inline void gen_op_cmpi(TCGv arg0, target_ulong arg1, int s, int crf)
e1571908 654{
2fdcb629 655 TCGv t0 = tcg_const_tl(arg1);
ea363694
AJ
656 gen_op_cmp(arg0, t0, s, crf);
657 tcg_temp_free(t0);
e1571908
AJ
658}
659
636aa200 660static inline void gen_op_cmp32(TCGv arg0, TCGv arg1, int s, int crf)
e1571908 661{
ea363694 662 TCGv t0, t1;
2fdcb629
RH
663 t0 = tcg_temp_new();
664 t1 = tcg_temp_new();
e1571908 665 if (s) {
ea363694
AJ
666 tcg_gen_ext32s_tl(t0, arg0);
667 tcg_gen_ext32s_tl(t1, arg1);
e1571908 668 } else {
ea363694
AJ
669 tcg_gen_ext32u_tl(t0, arg0);
670 tcg_gen_ext32u_tl(t1, arg1);
e1571908 671 }
ea363694
AJ
672 gen_op_cmp(t0, t1, s, crf);
673 tcg_temp_free(t1);
674 tcg_temp_free(t0);
e1571908
AJ
675}
676
636aa200 677static inline void gen_op_cmpi32(TCGv arg0, target_ulong arg1, int s, int crf)
e1571908 678{
2fdcb629 679 TCGv t0 = tcg_const_tl(arg1);
ea363694
AJ
680 gen_op_cmp32(arg0, t0, s, crf);
681 tcg_temp_free(t0);
e1571908 682}
e1571908 683
636aa200 684static inline void gen_set_Rc0(DisasContext *ctx, TCGv reg)
e1571908 685{
02765534 686 if (NARROW_MODE(ctx)) {
e1571908 687 gen_op_cmpi32(reg, 0, 1, 0);
02765534 688 } else {
e1571908 689 gen_op_cmpi(reg, 0, 1, 0);
02765534 690 }
e1571908
AJ
691}
692
693/* cmp */
99e300ef 694static void gen_cmp(DisasContext *ctx)
e1571908 695{
36f48d9c 696 if ((ctx->opcode & 0x00200000) && (ctx->insns_flags & PPC_64B)) {
e1571908
AJ
697 gen_op_cmp(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rB(ctx->opcode)],
698 1, crfD(ctx->opcode));
36f48d9c
AG
699 } else {
700 gen_op_cmp32(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rB(ctx->opcode)],
701 1, crfD(ctx->opcode));
02765534 702 }
e1571908
AJ
703}
704
705/* cmpi */
99e300ef 706static void gen_cmpi(DisasContext *ctx)
e1571908 707{
36f48d9c 708 if ((ctx->opcode & 0x00200000) && (ctx->insns_flags & PPC_64B)) {
e1571908
AJ
709 gen_op_cmpi(cpu_gpr[rA(ctx->opcode)], SIMM(ctx->opcode),
710 1, crfD(ctx->opcode));
36f48d9c
AG
711 } else {
712 gen_op_cmpi32(cpu_gpr[rA(ctx->opcode)], SIMM(ctx->opcode),
713 1, crfD(ctx->opcode));
02765534 714 }
e1571908
AJ
715}
716
717/* cmpl */
99e300ef 718static void gen_cmpl(DisasContext *ctx)
e1571908 719{
36f48d9c 720 if ((ctx->opcode & 0x00200000) && (ctx->insns_flags & PPC_64B)) {
e1571908
AJ
721 gen_op_cmp(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rB(ctx->opcode)],
722 0, crfD(ctx->opcode));
36f48d9c
AG
723 } else {
724 gen_op_cmp32(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rB(ctx->opcode)],
725 0, crfD(ctx->opcode));
02765534 726 }
e1571908
AJ
727}
728
729/* cmpli */
99e300ef 730static void gen_cmpli(DisasContext *ctx)
e1571908 731{
36f48d9c 732 if ((ctx->opcode & 0x00200000) && (ctx->insns_flags & PPC_64B)) {
e1571908
AJ
733 gen_op_cmpi(cpu_gpr[rA(ctx->opcode)], UIMM(ctx->opcode),
734 0, crfD(ctx->opcode));
36f48d9c
AG
735 } else {
736 gen_op_cmpi32(cpu_gpr[rA(ctx->opcode)], UIMM(ctx->opcode),
737 0, crfD(ctx->opcode));
02765534 738 }
e1571908
AJ
739}
740
741/* isel (PowerPC 2.03 specification) */
99e300ef 742static void gen_isel(DisasContext *ctx)
e1571908
AJ
743{
744 int l1, l2;
745 uint32_t bi = rC(ctx->opcode);
746 uint32_t mask;
a7812ae4 747 TCGv_i32 t0;
e1571908
AJ
748
749 l1 = gen_new_label();
750 l2 = gen_new_label();
751
752 mask = 1 << (3 - (bi & 0x03));
a7812ae4 753 t0 = tcg_temp_new_i32();
fea0c503
AJ
754 tcg_gen_andi_i32(t0, cpu_crf[bi >> 2], mask);
755 tcg_gen_brcondi_i32(TCG_COND_EQ, t0, 0, l1);
e1571908
AJ
756 if (rA(ctx->opcode) == 0)
757 tcg_gen_movi_tl(cpu_gpr[rD(ctx->opcode)], 0);
758 else
759 tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)]);
760 tcg_gen_br(l2);
761 gen_set_label(l1);
762 tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rB(ctx->opcode)]);
763 gen_set_label(l2);
a7812ae4 764 tcg_temp_free_i32(t0);
e1571908
AJ
765}
766
fcfda20f
AJ
767/* cmpb: PowerPC 2.05 specification */
768static void gen_cmpb(DisasContext *ctx)
769{
770 gen_helper_cmpb(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)],
771 cpu_gpr[rB(ctx->opcode)]);
772}
773
79aceca5 774/*** Integer arithmetic ***/
79aceca5 775
636aa200
BS
776static inline void gen_op_arith_compute_ov(DisasContext *ctx, TCGv arg0,
777 TCGv arg1, TCGv arg2, int sub)
74637406 778{
ffe30937 779 TCGv t0 = tcg_temp_new();
79aceca5 780
8e7a6db9 781 tcg_gen_xor_tl(cpu_ov, arg0, arg2);
74637406 782 tcg_gen_xor_tl(t0, arg1, arg2);
ffe30937
RH
783 if (sub) {
784 tcg_gen_and_tl(cpu_ov, cpu_ov, t0);
785 } else {
786 tcg_gen_andc_tl(cpu_ov, cpu_ov, t0);
787 }
788 tcg_temp_free(t0);
02765534 789 if (NARROW_MODE(ctx)) {
ffe30937
RH
790 tcg_gen_ext32s_tl(cpu_ov, cpu_ov);
791 }
ffe30937
RH
792 tcg_gen_shri_tl(cpu_ov, cpu_ov, TARGET_LONG_BITS - 1);
793 tcg_gen_or_tl(cpu_so, cpu_so, cpu_ov);
79aceca5
FB
794}
795
74637406 796/* Common add function */
636aa200 797static inline void gen_op_arith_add(DisasContext *ctx, TCGv ret, TCGv arg1,
b5a73f8d
RH
798 TCGv arg2, bool add_ca, bool compute_ca,
799 bool compute_ov, bool compute_rc0)
74637406 800{
b5a73f8d 801 TCGv t0 = ret;
d9bce9d9 802
752d634e 803 if (compute_ca || compute_ov) {
146de60d 804 t0 = tcg_temp_new();
74637406 805 }
79aceca5 806
da91a00f 807 if (compute_ca) {
79482e5a 808 if (NARROW_MODE(ctx)) {
752d634e
RH
809 /* Caution: a non-obvious corner case of the spec is that we
810 must produce the *entire* 64-bit addition, but produce the
811 carry into bit 32. */
79482e5a 812 TCGv t1 = tcg_temp_new();
752d634e
RH
813 tcg_gen_xor_tl(t1, arg1, arg2); /* add without carry */
814 tcg_gen_add_tl(t0, arg1, arg2);
79482e5a
RH
815 if (add_ca) {
816 tcg_gen_add_tl(t0, t0, cpu_ca);
817 }
752d634e
RH
818 tcg_gen_xor_tl(cpu_ca, t0, t1); /* bits changed w/ carry */
819 tcg_temp_free(t1);
820 tcg_gen_shri_tl(cpu_ca, cpu_ca, 32); /* extract bit 32 */
821 tcg_gen_andi_tl(cpu_ca, cpu_ca, 1);
b5a73f8d 822 } else {
79482e5a
RH
823 TCGv zero = tcg_const_tl(0);
824 if (add_ca) {
825 tcg_gen_add2_tl(t0, cpu_ca, arg1, zero, cpu_ca, zero);
826 tcg_gen_add2_tl(t0, cpu_ca, t0, cpu_ca, arg2, zero);
827 } else {
828 tcg_gen_add2_tl(t0, cpu_ca, arg1, zero, arg2, zero);
829 }
830 tcg_temp_free(zero);
b5a73f8d 831 }
b5a73f8d
RH
832 } else {
833 tcg_gen_add_tl(t0, arg1, arg2);
834 if (add_ca) {
835 tcg_gen_add_tl(t0, t0, cpu_ca);
836 }
da91a00f 837 }
79aceca5 838
74637406
AJ
839 if (compute_ov) {
840 gen_op_arith_compute_ov(ctx, t0, arg1, arg2, 0);
841 }
b5a73f8d 842 if (unlikely(compute_rc0)) {
74637406 843 gen_set_Rc0(ctx, t0);
b5a73f8d 844 }
74637406 845
a7812ae4 846 if (!TCGV_EQUAL(t0, ret)) {
74637406
AJ
847 tcg_gen_mov_tl(ret, t0);
848 tcg_temp_free(t0);
849 }
39dd32ee 850}
74637406
AJ
851/* Add functions with two operands */
852#define GEN_INT_ARITH_ADD(name, opc3, add_ca, compute_ca, compute_ov) \
b5a73f8d 853static void glue(gen_, name)(DisasContext *ctx) \
74637406
AJ
854{ \
855 gen_op_arith_add(ctx, cpu_gpr[rD(ctx->opcode)], \
856 cpu_gpr[rA(ctx->opcode)], cpu_gpr[rB(ctx->opcode)], \
b5a73f8d 857 add_ca, compute_ca, compute_ov, Rc(ctx->opcode)); \
74637406
AJ
858}
859/* Add functions with one operand and one immediate */
860#define GEN_INT_ARITH_ADD_CONST(name, opc3, const_val, \
861 add_ca, compute_ca, compute_ov) \
b5a73f8d 862static void glue(gen_, name)(DisasContext *ctx) \
74637406 863{ \
b5a73f8d 864 TCGv t0 = tcg_const_tl(const_val); \
74637406
AJ
865 gen_op_arith_add(ctx, cpu_gpr[rD(ctx->opcode)], \
866 cpu_gpr[rA(ctx->opcode)], t0, \
b5a73f8d 867 add_ca, compute_ca, compute_ov, Rc(ctx->opcode)); \
74637406
AJ
868 tcg_temp_free(t0); \
869}
870
871/* add add. addo addo. */
872GEN_INT_ARITH_ADD(add, 0x08, 0, 0, 0)
873GEN_INT_ARITH_ADD(addo, 0x18, 0, 0, 1)
874/* addc addc. addco addco. */
875GEN_INT_ARITH_ADD(addc, 0x00, 0, 1, 0)
876GEN_INT_ARITH_ADD(addco, 0x10, 0, 1, 1)
877/* adde adde. addeo addeo. */
878GEN_INT_ARITH_ADD(adde, 0x04, 1, 1, 0)
879GEN_INT_ARITH_ADD(addeo, 0x14, 1, 1, 1)
880/* addme addme. addmeo addmeo. */
881GEN_INT_ARITH_ADD_CONST(addme, 0x07, -1LL, 1, 1, 0)
882GEN_INT_ARITH_ADD_CONST(addmeo, 0x17, -1LL, 1, 1, 1)
883/* addze addze. addzeo addzeo.*/
884GEN_INT_ARITH_ADD_CONST(addze, 0x06, 0, 1, 1, 0)
885GEN_INT_ARITH_ADD_CONST(addzeo, 0x16, 0, 1, 1, 1)
886/* addi */
99e300ef 887static void gen_addi(DisasContext *ctx)
d9bce9d9 888{
74637406
AJ
889 target_long simm = SIMM(ctx->opcode);
890
891 if (rA(ctx->opcode) == 0) {
892 /* li case */
893 tcg_gen_movi_tl(cpu_gpr[rD(ctx->opcode)], simm);
894 } else {
b5a73f8d
RH
895 tcg_gen_addi_tl(cpu_gpr[rD(ctx->opcode)],
896 cpu_gpr[rA(ctx->opcode)], simm);
74637406 897 }
d9bce9d9 898}
74637406 899/* addic addic.*/
b5a73f8d 900static inline void gen_op_addic(DisasContext *ctx, bool compute_rc0)
d9bce9d9 901{
b5a73f8d
RH
902 TCGv c = tcg_const_tl(SIMM(ctx->opcode));
903 gen_op_arith_add(ctx, cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)],
904 c, 0, 1, 0, compute_rc0);
905 tcg_temp_free(c);
d9bce9d9 906}
99e300ef
BS
907
908static void gen_addic(DisasContext *ctx)
d9bce9d9 909{
b5a73f8d 910 gen_op_addic(ctx, 0);
d9bce9d9 911}
e8eaa2c0
BS
912
913static void gen_addic_(DisasContext *ctx)
d9bce9d9 914{
b5a73f8d 915 gen_op_addic(ctx, 1);
d9bce9d9 916}
99e300ef 917
54623277 918/* addis */
99e300ef 919static void gen_addis(DisasContext *ctx)
d9bce9d9 920{
74637406
AJ
921 target_long simm = SIMM(ctx->opcode);
922
923 if (rA(ctx->opcode) == 0) {
924 /* lis case */
925 tcg_gen_movi_tl(cpu_gpr[rD(ctx->opcode)], simm << 16);
926 } else {
b5a73f8d
RH
927 tcg_gen_addi_tl(cpu_gpr[rD(ctx->opcode)],
928 cpu_gpr[rA(ctx->opcode)], simm << 16);
74637406 929 }
d9bce9d9 930}
74637406 931
636aa200
BS
932static inline void gen_op_arith_divw(DisasContext *ctx, TCGv ret, TCGv arg1,
933 TCGv arg2, int sign, int compute_ov)
d9bce9d9 934{
2ef1b120
AJ
935 int l1 = gen_new_label();
936 int l2 = gen_new_label();
a7812ae4
PB
937 TCGv_i32 t0 = tcg_temp_local_new_i32();
938 TCGv_i32 t1 = tcg_temp_local_new_i32();
74637406 939
2ef1b120
AJ
940 tcg_gen_trunc_tl_i32(t0, arg1);
941 tcg_gen_trunc_tl_i32(t1, arg2);
942 tcg_gen_brcondi_i32(TCG_COND_EQ, t1, 0, l1);
74637406 943 if (sign) {
2ef1b120
AJ
944 int l3 = gen_new_label();
945 tcg_gen_brcondi_i32(TCG_COND_NE, t1, -1, l3);
946 tcg_gen_brcondi_i32(TCG_COND_EQ, t0, INT32_MIN, l1);
74637406 947 gen_set_label(l3);
2ef1b120 948 tcg_gen_div_i32(t0, t0, t1);
74637406 949 } else {
2ef1b120 950 tcg_gen_divu_i32(t0, t0, t1);
74637406
AJ
951 }
952 if (compute_ov) {
da91a00f 953 tcg_gen_movi_tl(cpu_ov, 0);
74637406
AJ
954 }
955 tcg_gen_br(l2);
956 gen_set_label(l1);
957 if (sign) {
2ef1b120 958 tcg_gen_sari_i32(t0, t0, 31);
74637406
AJ
959 } else {
960 tcg_gen_movi_i32(t0, 0);
961 }
962 if (compute_ov) {
da91a00f
RH
963 tcg_gen_movi_tl(cpu_ov, 1);
964 tcg_gen_movi_tl(cpu_so, 1);
74637406
AJ
965 }
966 gen_set_label(l2);
2ef1b120 967 tcg_gen_extu_i32_tl(ret, t0);
a7812ae4
PB
968 tcg_temp_free_i32(t0);
969 tcg_temp_free_i32(t1);
74637406
AJ
970 if (unlikely(Rc(ctx->opcode) != 0))
971 gen_set_Rc0(ctx, ret);
d9bce9d9 972}
74637406
AJ
973/* Div functions */
974#define GEN_INT_ARITH_DIVW(name, opc3, sign, compute_ov) \
99e300ef 975static void glue(gen_, name)(DisasContext *ctx) \
74637406
AJ
976{ \
977 gen_op_arith_divw(ctx, cpu_gpr[rD(ctx->opcode)], \
978 cpu_gpr[rA(ctx->opcode)], cpu_gpr[rB(ctx->opcode)], \
979 sign, compute_ov); \
980}
981/* divwu divwu. divwuo divwuo. */
982GEN_INT_ARITH_DIVW(divwu, 0x0E, 0, 0);
983GEN_INT_ARITH_DIVW(divwuo, 0x1E, 0, 1);
984/* divw divw. divwo divwo. */
985GEN_INT_ARITH_DIVW(divw, 0x0F, 1, 0);
986GEN_INT_ARITH_DIVW(divwo, 0x1F, 1, 1);
d9bce9d9 987#if defined(TARGET_PPC64)
636aa200
BS
988static inline void gen_op_arith_divd(DisasContext *ctx, TCGv ret, TCGv arg1,
989 TCGv arg2, int sign, int compute_ov)
d9bce9d9 990{
2ef1b120
AJ
991 int l1 = gen_new_label();
992 int l2 = gen_new_label();
74637406
AJ
993
994 tcg_gen_brcondi_i64(TCG_COND_EQ, arg2, 0, l1);
995 if (sign) {
2ef1b120 996 int l3 = gen_new_label();
74637406
AJ
997 tcg_gen_brcondi_i64(TCG_COND_NE, arg2, -1, l3);
998 tcg_gen_brcondi_i64(TCG_COND_EQ, arg1, INT64_MIN, l1);
999 gen_set_label(l3);
74637406
AJ
1000 tcg_gen_div_i64(ret, arg1, arg2);
1001 } else {
1002 tcg_gen_divu_i64(ret, arg1, arg2);
1003 }
1004 if (compute_ov) {
da91a00f 1005 tcg_gen_movi_tl(cpu_ov, 0);
74637406
AJ
1006 }
1007 tcg_gen_br(l2);
1008 gen_set_label(l1);
1009 if (sign) {
1010 tcg_gen_sari_i64(ret, arg1, 63);
1011 } else {
1012 tcg_gen_movi_i64(ret, 0);
1013 }
1014 if (compute_ov) {
da91a00f
RH
1015 tcg_gen_movi_tl(cpu_ov, 1);
1016 tcg_gen_movi_tl(cpu_so, 1);
74637406
AJ
1017 }
1018 gen_set_label(l2);
1019 if (unlikely(Rc(ctx->opcode) != 0))
1020 gen_set_Rc0(ctx, ret);
d9bce9d9 1021}
74637406 1022#define GEN_INT_ARITH_DIVD(name, opc3, sign, compute_ov) \
99e300ef 1023static void glue(gen_, name)(DisasContext *ctx) \
74637406 1024{ \
2ef1b120
AJ
1025 gen_op_arith_divd(ctx, cpu_gpr[rD(ctx->opcode)], \
1026 cpu_gpr[rA(ctx->opcode)], cpu_gpr[rB(ctx->opcode)], \
1027 sign, compute_ov); \
74637406
AJ
1028}
1029/* divwu divwu. divwuo divwuo. */
1030GEN_INT_ARITH_DIVD(divdu, 0x0E, 0, 0);
1031GEN_INT_ARITH_DIVD(divduo, 0x1E, 0, 1);
1032/* divw divw. divwo divwo. */
1033GEN_INT_ARITH_DIVD(divd, 0x0F, 1, 0);
1034GEN_INT_ARITH_DIVD(divdo, 0x1F, 1, 1);
d9bce9d9 1035#endif
74637406
AJ
1036
1037/* mulhw mulhw. */
99e300ef 1038static void gen_mulhw(DisasContext *ctx)
d9bce9d9 1039{
23ad1d5d
RH
1040 TCGv_i32 t0 = tcg_temp_new_i32();
1041 TCGv_i32 t1 = tcg_temp_new_i32();
74637406 1042
23ad1d5d
RH
1043 tcg_gen_trunc_tl_i32(t0, cpu_gpr[rA(ctx->opcode)]);
1044 tcg_gen_trunc_tl_i32(t1, cpu_gpr[rB(ctx->opcode)]);
1045 tcg_gen_muls2_i32(t0, t1, t0, t1);
1046 tcg_gen_extu_i32_tl(cpu_gpr[rD(ctx->opcode)], t1);
1047 tcg_temp_free_i32(t0);
1048 tcg_temp_free_i32(t1);
74637406
AJ
1049 if (unlikely(Rc(ctx->opcode) != 0))
1050 gen_set_Rc0(ctx, cpu_gpr[rD(ctx->opcode)]);
d9bce9d9 1051}
99e300ef 1052
54623277 1053/* mulhwu mulhwu. */
99e300ef 1054static void gen_mulhwu(DisasContext *ctx)
d9bce9d9 1055{
23ad1d5d
RH
1056 TCGv_i32 t0 = tcg_temp_new_i32();
1057 TCGv_i32 t1 = tcg_temp_new_i32();
74637406 1058
23ad1d5d
RH
1059 tcg_gen_trunc_tl_i32(t0, cpu_gpr[rA(ctx->opcode)]);
1060 tcg_gen_trunc_tl_i32(t1, cpu_gpr[rB(ctx->opcode)]);
1061 tcg_gen_mulu2_i32(t0, t1, t0, t1);
1062 tcg_gen_extu_i32_tl(cpu_gpr[rD(ctx->opcode)], t1);
1063 tcg_temp_free_i32(t0);
1064 tcg_temp_free_i32(t1);
74637406
AJ
1065 if (unlikely(Rc(ctx->opcode) != 0))
1066 gen_set_Rc0(ctx, cpu_gpr[rD(ctx->opcode)]);
d9bce9d9 1067}
99e300ef 1068
54623277 1069/* mullw mullw. */
99e300ef 1070static void gen_mullw(DisasContext *ctx)
d9bce9d9 1071{
74637406
AJ
1072 tcg_gen_mul_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)],
1073 cpu_gpr[rB(ctx->opcode)]);
1e4c090f 1074 tcg_gen_ext32s_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rD(ctx->opcode)]);
74637406
AJ
1075 if (unlikely(Rc(ctx->opcode) != 0))
1076 gen_set_Rc0(ctx, cpu_gpr[rD(ctx->opcode)]);
d9bce9d9 1077}
99e300ef 1078
54623277 1079/* mullwo mullwo. */
99e300ef 1080static void gen_mullwo(DisasContext *ctx)
d9bce9d9 1081{
e4a2c846
RH
1082 TCGv_i32 t0 = tcg_temp_new_i32();
1083 TCGv_i32 t1 = tcg_temp_new_i32();
74637406 1084
e4a2c846
RH
1085 tcg_gen_trunc_tl_i32(t0, cpu_gpr[rA(ctx->opcode)]);
1086 tcg_gen_trunc_tl_i32(t1, cpu_gpr[rB(ctx->opcode)]);
1087 tcg_gen_muls2_i32(t0, t1, t0, t1);
1088 tcg_gen_ext_i32_tl(cpu_gpr[rD(ctx->opcode)], t0);
1089
1090 tcg_gen_sari_i32(t0, t0, 31);
1091 tcg_gen_setcond_i32(TCG_COND_NE, t0, t0, t1);
1092 tcg_gen_extu_i32_tl(cpu_ov, t0);
1093 tcg_gen_or_tl(cpu_so, cpu_so, cpu_ov);
1094
1095 tcg_temp_free_i32(t0);
1096 tcg_temp_free_i32(t1);
74637406
AJ
1097 if (unlikely(Rc(ctx->opcode) != 0))
1098 gen_set_Rc0(ctx, cpu_gpr[rD(ctx->opcode)]);
d9bce9d9 1099}
99e300ef 1100
54623277 1101/* mulli */
99e300ef 1102static void gen_mulli(DisasContext *ctx)
d9bce9d9 1103{
74637406
AJ
1104 tcg_gen_muli_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)],
1105 SIMM(ctx->opcode));
d9bce9d9 1106}
23ad1d5d 1107
d9bce9d9 1108#if defined(TARGET_PPC64)
74637406 1109/* mulhd mulhd. */
23ad1d5d
RH
1110static void gen_mulhd(DisasContext *ctx)
1111{
1112 TCGv lo = tcg_temp_new();
1113 tcg_gen_muls2_tl(lo, cpu_gpr[rD(ctx->opcode)],
1114 cpu_gpr[rA(ctx->opcode)], cpu_gpr[rB(ctx->opcode)]);
1115 tcg_temp_free(lo);
1116 if (unlikely(Rc(ctx->opcode) != 0)) {
1117 gen_set_Rc0(ctx, cpu_gpr[rD(ctx->opcode)]);
1118 }
1119}
1120
74637406 1121/* mulhdu mulhdu. */
23ad1d5d
RH
1122static void gen_mulhdu(DisasContext *ctx)
1123{
1124 TCGv lo = tcg_temp_new();
1125 tcg_gen_mulu2_tl(lo, cpu_gpr[rD(ctx->opcode)],
1126 cpu_gpr[rA(ctx->opcode)], cpu_gpr[rB(ctx->opcode)]);
1127 tcg_temp_free(lo);
1128 if (unlikely(Rc(ctx->opcode) != 0)) {
1129 gen_set_Rc0(ctx, cpu_gpr[rD(ctx->opcode)]);
1130 }
1131}
99e300ef 1132
54623277 1133/* mulld mulld. */
99e300ef 1134static void gen_mulld(DisasContext *ctx)
d9bce9d9 1135{
74637406
AJ
1136 tcg_gen_mul_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)],
1137 cpu_gpr[rB(ctx->opcode)]);
1138 if (unlikely(Rc(ctx->opcode) != 0))
1139 gen_set_Rc0(ctx, cpu_gpr[rD(ctx->opcode)]);
d9bce9d9 1140}
d15f74fb 1141
74637406 1142/* mulldo mulldo. */
d15f74fb
BS
1143static void gen_mulldo(DisasContext *ctx)
1144{
1145 gen_helper_mulldo(cpu_gpr[rD(ctx->opcode)], cpu_env,
1146 cpu_gpr[rA(ctx->opcode)], cpu_gpr[rB(ctx->opcode)]);
1147 if (unlikely(Rc(ctx->opcode) != 0)) {
1148 gen_set_Rc0(ctx, cpu_gpr[rD(ctx->opcode)]);
1149 }
1150}
d9bce9d9 1151#endif
74637406 1152
74637406 1153/* Common subf function */
636aa200 1154static inline void gen_op_arith_subf(DisasContext *ctx, TCGv ret, TCGv arg1,
b5a73f8d
RH
1155 TCGv arg2, bool add_ca, bool compute_ca,
1156 bool compute_ov, bool compute_rc0)
79aceca5 1157{
b5a73f8d 1158 TCGv t0 = ret;
79aceca5 1159
752d634e 1160 if (compute_ca || compute_ov) {
b5a73f8d 1161 t0 = tcg_temp_new();
da91a00f 1162 }
74637406 1163
79482e5a
RH
1164 if (compute_ca) {
1165 /* dest = ~arg1 + arg2 [+ ca]. */
1166 if (NARROW_MODE(ctx)) {
752d634e
RH
1167 /* Caution: a non-obvious corner case of the spec is that we
1168 must produce the *entire* 64-bit addition, but produce the
1169 carry into bit 32. */
79482e5a 1170 TCGv inv1 = tcg_temp_new();
752d634e 1171 TCGv t1 = tcg_temp_new();
79482e5a 1172 tcg_gen_not_tl(inv1, arg1);
79482e5a 1173 if (add_ca) {
752d634e 1174 tcg_gen_add_tl(t0, arg2, cpu_ca);
79482e5a 1175 } else {
752d634e 1176 tcg_gen_addi_tl(t0, arg2, 1);
79482e5a 1177 }
752d634e 1178 tcg_gen_xor_tl(t1, arg2, inv1); /* add without carry */
79482e5a 1179 tcg_gen_add_tl(t0, t0, inv1);
752d634e
RH
1180 tcg_gen_xor_tl(cpu_ca, t0, t1); /* bits changes w/ carry */
1181 tcg_temp_free(t1);
1182 tcg_gen_shri_tl(cpu_ca, cpu_ca, 32); /* extract bit 32 */
1183 tcg_gen_andi_tl(cpu_ca, cpu_ca, 1);
79482e5a 1184 } else if (add_ca) {
08f4a0f7
RH
1185 TCGv zero, inv1 = tcg_temp_new();
1186 tcg_gen_not_tl(inv1, arg1);
b5a73f8d
RH
1187 zero = tcg_const_tl(0);
1188 tcg_gen_add2_tl(t0, cpu_ca, arg2, zero, cpu_ca, zero);
08f4a0f7 1189 tcg_gen_add2_tl(t0, cpu_ca, t0, cpu_ca, inv1, zero);
b5a73f8d 1190 tcg_temp_free(zero);
08f4a0f7 1191 tcg_temp_free(inv1);
b5a73f8d 1192 } else {
79482e5a 1193 tcg_gen_setcond_tl(TCG_COND_GEU, cpu_ca, arg2, arg1);
b5a73f8d 1194 tcg_gen_sub_tl(t0, arg2, arg1);
b5a73f8d 1195 }
79482e5a
RH
1196 } else if (add_ca) {
1197 /* Since we're ignoring carry-out, we can simplify the
1198 standard ~arg1 + arg2 + ca to arg2 - arg1 + ca - 1. */
1199 tcg_gen_sub_tl(t0, arg2, arg1);
1200 tcg_gen_add_tl(t0, t0, cpu_ca);
1201 tcg_gen_subi_tl(t0, t0, 1);
79aceca5 1202 } else {
b5a73f8d 1203 tcg_gen_sub_tl(t0, arg2, arg1);
74637406 1204 }
b5a73f8d 1205
74637406
AJ
1206 if (compute_ov) {
1207 gen_op_arith_compute_ov(ctx, t0, arg1, arg2, 1);
1208 }
b5a73f8d 1209 if (unlikely(compute_rc0)) {
74637406 1210 gen_set_Rc0(ctx, t0);
b5a73f8d 1211 }
74637406 1212
a7812ae4 1213 if (!TCGV_EQUAL(t0, ret)) {
74637406
AJ
1214 tcg_gen_mov_tl(ret, t0);
1215 tcg_temp_free(t0);
79aceca5 1216 }
79aceca5 1217}
74637406
AJ
1218/* Sub functions with Two operands functions */
1219#define GEN_INT_ARITH_SUBF(name, opc3, add_ca, compute_ca, compute_ov) \
b5a73f8d 1220static void glue(gen_, name)(DisasContext *ctx) \
74637406
AJ
1221{ \
1222 gen_op_arith_subf(ctx, cpu_gpr[rD(ctx->opcode)], \
1223 cpu_gpr[rA(ctx->opcode)], cpu_gpr[rB(ctx->opcode)], \
b5a73f8d 1224 add_ca, compute_ca, compute_ov, Rc(ctx->opcode)); \
74637406
AJ
1225}
1226/* Sub functions with one operand and one immediate */
1227#define GEN_INT_ARITH_SUBF_CONST(name, opc3, const_val, \
1228 add_ca, compute_ca, compute_ov) \
b5a73f8d 1229static void glue(gen_, name)(DisasContext *ctx) \
74637406 1230{ \
b5a73f8d 1231 TCGv t0 = tcg_const_tl(const_val); \
74637406
AJ
1232 gen_op_arith_subf(ctx, cpu_gpr[rD(ctx->opcode)], \
1233 cpu_gpr[rA(ctx->opcode)], t0, \
b5a73f8d 1234 add_ca, compute_ca, compute_ov, Rc(ctx->opcode)); \
74637406
AJ
1235 tcg_temp_free(t0); \
1236}
1237/* subf subf. subfo subfo. */
1238GEN_INT_ARITH_SUBF(subf, 0x01, 0, 0, 0)
1239GEN_INT_ARITH_SUBF(subfo, 0x11, 0, 0, 1)
1240/* subfc subfc. subfco subfco. */
1241GEN_INT_ARITH_SUBF(subfc, 0x00, 0, 1, 0)
1242GEN_INT_ARITH_SUBF(subfco, 0x10, 0, 1, 1)
1243/* subfe subfe. subfeo subfo. */
1244GEN_INT_ARITH_SUBF(subfe, 0x04, 1, 1, 0)
1245GEN_INT_ARITH_SUBF(subfeo, 0x14, 1, 1, 1)
1246/* subfme subfme. subfmeo subfmeo. */
1247GEN_INT_ARITH_SUBF_CONST(subfme, 0x07, -1LL, 1, 1, 0)
1248GEN_INT_ARITH_SUBF_CONST(subfmeo, 0x17, -1LL, 1, 1, 1)
1249/* subfze subfze. subfzeo subfzeo.*/
1250GEN_INT_ARITH_SUBF_CONST(subfze, 0x06, 0, 1, 1, 0)
1251GEN_INT_ARITH_SUBF_CONST(subfzeo, 0x16, 0, 1, 1, 1)
99e300ef 1252
54623277 1253/* subfic */
99e300ef 1254static void gen_subfic(DisasContext *ctx)
79aceca5 1255{
b5a73f8d
RH
1256 TCGv c = tcg_const_tl(SIMM(ctx->opcode));
1257 gen_op_arith_subf(ctx, cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)],
1258 c, 0, 1, 0, 0);
1259 tcg_temp_free(c);
79aceca5
FB
1260}
1261
fd3f0081
RH
1262/* neg neg. nego nego. */
1263static inline void gen_op_arith_neg(DisasContext *ctx, bool compute_ov)
1264{
1265 TCGv zero = tcg_const_tl(0);
1266 gen_op_arith_subf(ctx, cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)],
1267 zero, 0, 0, compute_ov, Rc(ctx->opcode));
1268 tcg_temp_free(zero);
1269}
1270
1271static void gen_neg(DisasContext *ctx)
1272{
1273 gen_op_arith_neg(ctx, 0);
1274}
1275
1276static void gen_nego(DisasContext *ctx)
1277{
1278 gen_op_arith_neg(ctx, 1);
1279}
1280
79aceca5 1281/*** Integer logical ***/
26d67362 1282#define GEN_LOGICAL2(name, tcg_op, opc, type) \
99e300ef 1283static void glue(gen_, name)(DisasContext *ctx) \
79aceca5 1284{ \
26d67362
AJ
1285 tcg_op(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)], \
1286 cpu_gpr[rB(ctx->opcode)]); \
76a66253 1287 if (unlikely(Rc(ctx->opcode) != 0)) \
26d67362 1288 gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]); \
79aceca5 1289}
79aceca5 1290
26d67362 1291#define GEN_LOGICAL1(name, tcg_op, opc, type) \
99e300ef 1292static void glue(gen_, name)(DisasContext *ctx) \
79aceca5 1293{ \
26d67362 1294 tcg_op(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)]); \
76a66253 1295 if (unlikely(Rc(ctx->opcode) != 0)) \
26d67362 1296 gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]); \
79aceca5
FB
1297}
1298
1299/* and & and. */
26d67362 1300GEN_LOGICAL2(and, tcg_gen_and_tl, 0x00, PPC_INTEGER);
79aceca5 1301/* andc & andc. */
26d67362 1302GEN_LOGICAL2(andc, tcg_gen_andc_tl, 0x01, PPC_INTEGER);
e8eaa2c0 1303
54623277 1304/* andi. */
e8eaa2c0 1305static void gen_andi_(DisasContext *ctx)
79aceca5 1306{
26d67362
AJ
1307 tcg_gen_andi_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)], UIMM(ctx->opcode));
1308 gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
79aceca5 1309}
e8eaa2c0 1310
54623277 1311/* andis. */
e8eaa2c0 1312static void gen_andis_(DisasContext *ctx)
79aceca5 1313{
26d67362
AJ
1314 tcg_gen_andi_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)], UIMM(ctx->opcode) << 16);
1315 gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
79aceca5 1316}
99e300ef 1317
54623277 1318/* cntlzw */
99e300ef 1319static void gen_cntlzw(DisasContext *ctx)
26d67362 1320{
a7812ae4 1321 gen_helper_cntlzw(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)]);
26d67362 1322 if (unlikely(Rc(ctx->opcode) != 0))
2e31f5d3 1323 gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
26d67362 1324}
79aceca5 1325/* eqv & eqv. */
26d67362 1326GEN_LOGICAL2(eqv, tcg_gen_eqv_tl, 0x08, PPC_INTEGER);
79aceca5 1327/* extsb & extsb. */
26d67362 1328GEN_LOGICAL1(extsb, tcg_gen_ext8s_tl, 0x1D, PPC_INTEGER);
79aceca5 1329/* extsh & extsh. */
26d67362 1330GEN_LOGICAL1(extsh, tcg_gen_ext16s_tl, 0x1C, PPC_INTEGER);
79aceca5 1331/* nand & nand. */
26d67362 1332GEN_LOGICAL2(nand, tcg_gen_nand_tl, 0x0E, PPC_INTEGER);
79aceca5 1333/* nor & nor. */
26d67362 1334GEN_LOGICAL2(nor, tcg_gen_nor_tl, 0x03, PPC_INTEGER);
99e300ef 1335
54623277 1336/* or & or. */
99e300ef 1337static void gen_or(DisasContext *ctx)
9a64fbe4 1338{
76a66253
JM
1339 int rs, ra, rb;
1340
1341 rs = rS(ctx->opcode);
1342 ra = rA(ctx->opcode);
1343 rb = rB(ctx->opcode);
1344 /* Optimisation for mr. ri case */
1345 if (rs != ra || rs != rb) {
26d67362
AJ
1346 if (rs != rb)
1347 tcg_gen_or_tl(cpu_gpr[ra], cpu_gpr[rs], cpu_gpr[rb]);
1348 else
1349 tcg_gen_mov_tl(cpu_gpr[ra], cpu_gpr[rs]);
76a66253 1350 if (unlikely(Rc(ctx->opcode) != 0))
26d67362 1351 gen_set_Rc0(ctx, cpu_gpr[ra]);
76a66253 1352 } else if (unlikely(Rc(ctx->opcode) != 0)) {
26d67362 1353 gen_set_Rc0(ctx, cpu_gpr[rs]);
c80f84e3
JM
1354#if defined(TARGET_PPC64)
1355 } else {
26d67362
AJ
1356 int prio = 0;
1357
c80f84e3
JM
1358 switch (rs) {
1359 case 1:
1360 /* Set process priority to low */
26d67362 1361 prio = 2;
c80f84e3
JM
1362 break;
1363 case 6:
1364 /* Set process priority to medium-low */
26d67362 1365 prio = 3;
c80f84e3
JM
1366 break;
1367 case 2:
1368 /* Set process priority to normal */
26d67362 1369 prio = 4;
c80f84e3 1370 break;
be147d08
JM
1371#if !defined(CONFIG_USER_ONLY)
1372 case 31:
76db3ba4 1373 if (ctx->mem_idx > 0) {
be147d08 1374 /* Set process priority to very low */
26d67362 1375 prio = 1;
be147d08
JM
1376 }
1377 break;
1378 case 5:
76db3ba4 1379 if (ctx->mem_idx > 0) {
be147d08 1380 /* Set process priority to medium-hight */
26d67362 1381 prio = 5;
be147d08
JM
1382 }
1383 break;
1384 case 3:
76db3ba4 1385 if (ctx->mem_idx > 0) {
be147d08 1386 /* Set process priority to high */
26d67362 1387 prio = 6;
be147d08
JM
1388 }
1389 break;
be147d08 1390 case 7:
76db3ba4 1391 if (ctx->mem_idx > 1) {
be147d08 1392 /* Set process priority to very high */
26d67362 1393 prio = 7;
be147d08
JM
1394 }
1395 break;
be147d08 1396#endif
c80f84e3
JM
1397 default:
1398 /* nop */
1399 break;
1400 }
26d67362 1401 if (prio) {
a7812ae4 1402 TCGv t0 = tcg_temp_new();
54cdcae6 1403 gen_load_spr(t0, SPR_PPR);
ea363694
AJ
1404 tcg_gen_andi_tl(t0, t0, ~0x001C000000000000ULL);
1405 tcg_gen_ori_tl(t0, t0, ((uint64_t)prio) << 50);
54cdcae6 1406 gen_store_spr(SPR_PPR, t0);
ea363694 1407 tcg_temp_free(t0);
26d67362 1408 }
c80f84e3 1409#endif
9a64fbe4 1410 }
9a64fbe4 1411}
79aceca5 1412/* orc & orc. */
26d67362 1413GEN_LOGICAL2(orc, tcg_gen_orc_tl, 0x0C, PPC_INTEGER);
99e300ef 1414
54623277 1415/* xor & xor. */
99e300ef 1416static void gen_xor(DisasContext *ctx)
9a64fbe4 1417{
9a64fbe4 1418 /* Optimisation for "set to zero" case */
26d67362 1419 if (rS(ctx->opcode) != rB(ctx->opcode))
312179c4 1420 tcg_gen_xor_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)], cpu_gpr[rB(ctx->opcode)]);
26d67362
AJ
1421 else
1422 tcg_gen_movi_tl(cpu_gpr[rA(ctx->opcode)], 0);
76a66253 1423 if (unlikely(Rc(ctx->opcode) != 0))
26d67362 1424 gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
9a64fbe4 1425}
99e300ef 1426
54623277 1427/* ori */
99e300ef 1428static void gen_ori(DisasContext *ctx)
79aceca5 1429{
76a66253 1430 target_ulong uimm = UIMM(ctx->opcode);
79aceca5 1431
9a64fbe4
FB
1432 if (rS(ctx->opcode) == rA(ctx->opcode) && uimm == 0) {
1433 /* NOP */
76a66253 1434 /* XXX: should handle special NOPs for POWER series */
9a64fbe4 1435 return;
76a66253 1436 }
26d67362 1437 tcg_gen_ori_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)], uimm);
79aceca5 1438}
99e300ef 1439
54623277 1440/* oris */
99e300ef 1441static void gen_oris(DisasContext *ctx)
79aceca5 1442{
76a66253 1443 target_ulong uimm = UIMM(ctx->opcode);
79aceca5 1444
9a64fbe4
FB
1445 if (rS(ctx->opcode) == rA(ctx->opcode) && uimm == 0) {
1446 /* NOP */
1447 return;
76a66253 1448 }
26d67362 1449 tcg_gen_ori_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)], uimm << 16);
79aceca5 1450}
99e300ef 1451
54623277 1452/* xori */
99e300ef 1453static void gen_xori(DisasContext *ctx)
79aceca5 1454{
76a66253 1455 target_ulong uimm = UIMM(ctx->opcode);
9a64fbe4
FB
1456
1457 if (rS(ctx->opcode) == rA(ctx->opcode) && uimm == 0) {
1458 /* NOP */
1459 return;
1460 }
26d67362 1461 tcg_gen_xori_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)], uimm);
79aceca5 1462}
99e300ef 1463
54623277 1464/* xoris */
99e300ef 1465static void gen_xoris(DisasContext *ctx)
79aceca5 1466{
76a66253 1467 target_ulong uimm = UIMM(ctx->opcode);
9a64fbe4
FB
1468
1469 if (rS(ctx->opcode) == rA(ctx->opcode) && uimm == 0) {
1470 /* NOP */
1471 return;
1472 }
26d67362 1473 tcg_gen_xori_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)], uimm << 16);
79aceca5 1474}
99e300ef 1475
54623277 1476/* popcntb : PowerPC 2.03 specification */
99e300ef 1477static void gen_popcntb(DisasContext *ctx)
d9bce9d9 1478{
eaabeef2
DG
1479 gen_helper_popcntb(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)]);
1480}
1481
1482static void gen_popcntw(DisasContext *ctx)
1483{
1484 gen_helper_popcntw(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)]);
1485}
1486
d9bce9d9 1487#if defined(TARGET_PPC64)
eaabeef2
DG
1488/* popcntd: PowerPC 2.06 specification */
1489static void gen_popcntd(DisasContext *ctx)
1490{
1491 gen_helper_popcntd(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)]);
d9bce9d9 1492}
eaabeef2 1493#endif
d9bce9d9 1494
725bcec2
AJ
1495/* prtyw: PowerPC 2.05 specification */
1496static void gen_prtyw(DisasContext *ctx)
1497{
1498 TCGv ra = cpu_gpr[rA(ctx->opcode)];
1499 TCGv rs = cpu_gpr[rS(ctx->opcode)];
1500 TCGv t0 = tcg_temp_new();
1501 tcg_gen_shri_tl(t0, rs, 16);
1502 tcg_gen_xor_tl(ra, rs, t0);
1503 tcg_gen_shri_tl(t0, ra, 8);
1504 tcg_gen_xor_tl(ra, ra, t0);
1505 tcg_gen_andi_tl(ra, ra, (target_ulong)0x100000001ULL);
1506 tcg_temp_free(t0);
1507}
1508
1509#if defined(TARGET_PPC64)
1510/* prtyd: PowerPC 2.05 specification */
1511static void gen_prtyd(DisasContext *ctx)
1512{
1513 TCGv ra = cpu_gpr[rA(ctx->opcode)];
1514 TCGv rs = cpu_gpr[rS(ctx->opcode)];
1515 TCGv t0 = tcg_temp_new();
1516 tcg_gen_shri_tl(t0, rs, 32);
1517 tcg_gen_xor_tl(ra, rs, t0);
1518 tcg_gen_shri_tl(t0, ra, 16);
1519 tcg_gen_xor_tl(ra, ra, t0);
1520 tcg_gen_shri_tl(t0, ra, 8);
1521 tcg_gen_xor_tl(ra, ra, t0);
1522 tcg_gen_andi_tl(ra, ra, 1);
1523 tcg_temp_free(t0);
1524}
1525#endif
1526
d9bce9d9
JM
1527#if defined(TARGET_PPC64)
1528/* extsw & extsw. */
26d67362 1529GEN_LOGICAL1(extsw, tcg_gen_ext32s_tl, 0x1E, PPC_64B);
99e300ef 1530
54623277 1531/* cntlzd */
99e300ef 1532static void gen_cntlzd(DisasContext *ctx)
26d67362 1533{
a7812ae4 1534 gen_helper_cntlzd(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)]);
26d67362
AJ
1535 if (unlikely(Rc(ctx->opcode) != 0))
1536 gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
1537}
d9bce9d9
JM
1538#endif
1539
79aceca5 1540/*** Integer rotate ***/
99e300ef 1541
54623277 1542/* rlwimi & rlwimi. */
99e300ef 1543static void gen_rlwimi(DisasContext *ctx)
79aceca5 1544{
76a66253 1545 uint32_t mb, me, sh;
79aceca5
FB
1546
1547 mb = MB(ctx->opcode);
1548 me = ME(ctx->opcode);
76a66253 1549 sh = SH(ctx->opcode);
d03ef511
AJ
1550 if (likely(sh == 0 && mb == 0 && me == 31)) {
1551 tcg_gen_ext32u_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)]);
1552 } else {
d03ef511 1553 target_ulong mask;
a7812ae4
PB
1554 TCGv t1;
1555 TCGv t0 = tcg_temp_new();
54843a58 1556#if defined(TARGET_PPC64)
a7812ae4
PB
1557 TCGv_i32 t2 = tcg_temp_new_i32();
1558 tcg_gen_trunc_i64_i32(t2, cpu_gpr[rS(ctx->opcode)]);
1559 tcg_gen_rotli_i32(t2, t2, sh);
1560 tcg_gen_extu_i32_i64(t0, t2);
1561 tcg_temp_free_i32(t2);
54843a58
AJ
1562#else
1563 tcg_gen_rotli_i32(t0, cpu_gpr[rS(ctx->opcode)], sh);
1564#endif
76a66253 1565#if defined(TARGET_PPC64)
d03ef511
AJ
1566 mb += 32;
1567 me += 32;
76a66253 1568#endif
d03ef511 1569 mask = MASK(mb, me);
a7812ae4 1570 t1 = tcg_temp_new();
d03ef511
AJ
1571 tcg_gen_andi_tl(t0, t0, mask);
1572 tcg_gen_andi_tl(t1, cpu_gpr[rA(ctx->opcode)], ~mask);
1573 tcg_gen_or_tl(cpu_gpr[rA(ctx->opcode)], t0, t1);
1574 tcg_temp_free(t0);
1575 tcg_temp_free(t1);
1576 }
76a66253 1577 if (unlikely(Rc(ctx->opcode) != 0))
d03ef511 1578 gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
79aceca5 1579}
99e300ef 1580
54623277 1581/* rlwinm & rlwinm. */
99e300ef 1582static void gen_rlwinm(DisasContext *ctx)
79aceca5
FB
1583{
1584 uint32_t mb, me, sh;
3b46e624 1585
79aceca5
FB
1586 sh = SH(ctx->opcode);
1587 mb = MB(ctx->opcode);
1588 me = ME(ctx->opcode);
d03ef511
AJ
1589
1590 if (likely(mb == 0 && me == (31 - sh))) {
1591 if (likely(sh == 0)) {
1592 tcg_gen_ext32u_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)]);
1593 } else {
a7812ae4 1594 TCGv t0 = tcg_temp_new();
d03ef511
AJ
1595 tcg_gen_ext32u_tl(t0, cpu_gpr[rS(ctx->opcode)]);
1596 tcg_gen_shli_tl(t0, t0, sh);
1597 tcg_gen_ext32u_tl(cpu_gpr[rA(ctx->opcode)], t0);
1598 tcg_temp_free(t0);
79aceca5 1599 }
d03ef511 1600 } else if (likely(sh != 0 && me == 31 && sh == (32 - mb))) {
a7812ae4 1601 TCGv t0 = tcg_temp_new();
d03ef511
AJ
1602 tcg_gen_ext32u_tl(t0, cpu_gpr[rS(ctx->opcode)]);
1603 tcg_gen_shri_tl(t0, t0, mb);
1604 tcg_gen_ext32u_tl(cpu_gpr[rA(ctx->opcode)], t0);
1605 tcg_temp_free(t0);
1606 } else {
a7812ae4 1607 TCGv t0 = tcg_temp_new();
54843a58 1608#if defined(TARGET_PPC64)
a7812ae4 1609 TCGv_i32 t1 = tcg_temp_new_i32();
54843a58
AJ
1610 tcg_gen_trunc_i64_i32(t1, cpu_gpr[rS(ctx->opcode)]);
1611 tcg_gen_rotli_i32(t1, t1, sh);
1612 tcg_gen_extu_i32_i64(t0, t1);
a7812ae4 1613 tcg_temp_free_i32(t1);
54843a58
AJ
1614#else
1615 tcg_gen_rotli_i32(t0, cpu_gpr[rS(ctx->opcode)], sh);
1616#endif
76a66253 1617#if defined(TARGET_PPC64)
d03ef511
AJ
1618 mb += 32;
1619 me += 32;
76a66253 1620#endif
d03ef511
AJ
1621 tcg_gen_andi_tl(cpu_gpr[rA(ctx->opcode)], t0, MASK(mb, me));
1622 tcg_temp_free(t0);
1623 }
76a66253 1624 if (unlikely(Rc(ctx->opcode) != 0))
d03ef511 1625 gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
79aceca5 1626}
99e300ef 1627
54623277 1628/* rlwnm & rlwnm. */
99e300ef 1629static void gen_rlwnm(DisasContext *ctx)
79aceca5
FB
1630{
1631 uint32_t mb, me;
54843a58
AJ
1632 TCGv t0;
1633#if defined(TARGET_PPC64)
a7812ae4 1634 TCGv_i32 t1, t2;
54843a58 1635#endif
79aceca5
FB
1636
1637 mb = MB(ctx->opcode);
1638 me = ME(ctx->opcode);
a7812ae4 1639 t0 = tcg_temp_new();
d03ef511 1640 tcg_gen_andi_tl(t0, cpu_gpr[rB(ctx->opcode)], 0x1f);
54843a58 1641#if defined(TARGET_PPC64)
a7812ae4
PB
1642 t1 = tcg_temp_new_i32();
1643 t2 = tcg_temp_new_i32();
54843a58
AJ
1644 tcg_gen_trunc_i64_i32(t1, cpu_gpr[rS(ctx->opcode)]);
1645 tcg_gen_trunc_i64_i32(t2, t0);
1646 tcg_gen_rotl_i32(t1, t1, t2);
1647 tcg_gen_extu_i32_i64(t0, t1);
a7812ae4
PB
1648 tcg_temp_free_i32(t1);
1649 tcg_temp_free_i32(t2);
54843a58
AJ
1650#else
1651 tcg_gen_rotl_i32(t0, cpu_gpr[rS(ctx->opcode)], t0);
1652#endif
76a66253
JM
1653 if (unlikely(mb != 0 || me != 31)) {
1654#if defined(TARGET_PPC64)
1655 mb += 32;
1656 me += 32;
1657#endif
54843a58 1658 tcg_gen_andi_tl(cpu_gpr[rA(ctx->opcode)], t0, MASK(mb, me));
d03ef511 1659 } else {
54843a58 1660 tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], t0);
79aceca5 1661 }
54843a58 1662 tcg_temp_free(t0);
76a66253 1663 if (unlikely(Rc(ctx->opcode) != 0))
d03ef511 1664 gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
79aceca5
FB
1665}
1666
d9bce9d9
JM
1667#if defined(TARGET_PPC64)
1668#define GEN_PPC64_R2(name, opc1, opc2) \
e8eaa2c0 1669static void glue(gen_, name##0)(DisasContext *ctx) \
d9bce9d9
JM
1670{ \
1671 gen_##name(ctx, 0); \
1672} \
e8eaa2c0
BS
1673 \
1674static void glue(gen_, name##1)(DisasContext *ctx) \
d9bce9d9
JM
1675{ \
1676 gen_##name(ctx, 1); \
1677}
1678#define GEN_PPC64_R4(name, opc1, opc2) \
e8eaa2c0 1679static void glue(gen_, name##0)(DisasContext *ctx) \
d9bce9d9
JM
1680{ \
1681 gen_##name(ctx, 0, 0); \
1682} \
e8eaa2c0
BS
1683 \
1684static void glue(gen_, name##1)(DisasContext *ctx) \
d9bce9d9
JM
1685{ \
1686 gen_##name(ctx, 0, 1); \
1687} \
e8eaa2c0
BS
1688 \
1689static void glue(gen_, name##2)(DisasContext *ctx) \
d9bce9d9
JM
1690{ \
1691 gen_##name(ctx, 1, 0); \
1692} \
e8eaa2c0
BS
1693 \
1694static void glue(gen_, name##3)(DisasContext *ctx) \
d9bce9d9
JM
1695{ \
1696 gen_##name(ctx, 1, 1); \
1697}
51789c41 1698
636aa200
BS
1699static inline void gen_rldinm(DisasContext *ctx, uint32_t mb, uint32_t me,
1700 uint32_t sh)
51789c41 1701{
d03ef511
AJ
1702 if (likely(sh != 0 && mb == 0 && me == (63 - sh))) {
1703 tcg_gen_shli_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)], sh);
1704 } else if (likely(sh != 0 && me == 63 && sh == (64 - mb))) {
1705 tcg_gen_shri_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)], mb);
1706 } else {
a7812ae4 1707 TCGv t0 = tcg_temp_new();
54843a58 1708 tcg_gen_rotli_tl(t0, cpu_gpr[rS(ctx->opcode)], sh);
d03ef511 1709 if (likely(mb == 0 && me == 63)) {
54843a58 1710 tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], t0);
d03ef511
AJ
1711 } else {
1712 tcg_gen_andi_tl(cpu_gpr[rA(ctx->opcode)], t0, MASK(mb, me));
51789c41 1713 }
d03ef511 1714 tcg_temp_free(t0);
51789c41 1715 }
51789c41 1716 if (unlikely(Rc(ctx->opcode) != 0))
d03ef511 1717 gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
51789c41 1718}
d9bce9d9 1719/* rldicl - rldicl. */
636aa200 1720static inline void gen_rldicl(DisasContext *ctx, int mbn, int shn)
d9bce9d9 1721{
51789c41 1722 uint32_t sh, mb;
d9bce9d9 1723
9d53c753
JM
1724 sh = SH(ctx->opcode) | (shn << 5);
1725 mb = MB(ctx->opcode) | (mbn << 5);
51789c41 1726 gen_rldinm(ctx, mb, 63, sh);
d9bce9d9 1727}
51789c41 1728GEN_PPC64_R4(rldicl, 0x1E, 0x00);
d9bce9d9 1729/* rldicr - rldicr. */
636aa200 1730static inline void gen_rldicr(DisasContext *ctx, int men, int shn)
d9bce9d9 1731{
51789c41 1732 uint32_t sh, me;
d9bce9d9 1733
9d53c753
JM
1734 sh = SH(ctx->opcode) | (shn << 5);
1735 me = MB(ctx->opcode) | (men << 5);
51789c41 1736 gen_rldinm(ctx, 0, me, sh);
d9bce9d9 1737}
51789c41 1738GEN_PPC64_R4(rldicr, 0x1E, 0x02);
d9bce9d9 1739/* rldic - rldic. */
636aa200 1740static inline void gen_rldic(DisasContext *ctx, int mbn, int shn)
d9bce9d9 1741{
51789c41 1742 uint32_t sh, mb;
d9bce9d9 1743
9d53c753
JM
1744 sh = SH(ctx->opcode) | (shn << 5);
1745 mb = MB(ctx->opcode) | (mbn << 5);
51789c41
JM
1746 gen_rldinm(ctx, mb, 63 - sh, sh);
1747}
1748GEN_PPC64_R4(rldic, 0x1E, 0x04);
1749
636aa200 1750static inline void gen_rldnm(DisasContext *ctx, uint32_t mb, uint32_t me)
51789c41 1751{
54843a58 1752 TCGv t0;
d03ef511 1753
a7812ae4 1754 t0 = tcg_temp_new();
d03ef511 1755 tcg_gen_andi_tl(t0, cpu_gpr[rB(ctx->opcode)], 0x3f);
54843a58 1756 tcg_gen_rotl_tl(t0, cpu_gpr[rS(ctx->opcode)], t0);
51789c41 1757 if (unlikely(mb != 0 || me != 63)) {
54843a58
AJ
1758 tcg_gen_andi_tl(cpu_gpr[rA(ctx->opcode)], t0, MASK(mb, me));
1759 } else {
1760 tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], t0);
1761 }
1762 tcg_temp_free(t0);
51789c41 1763 if (unlikely(Rc(ctx->opcode) != 0))
d03ef511 1764 gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
d9bce9d9 1765}
51789c41 1766
d9bce9d9 1767/* rldcl - rldcl. */
636aa200 1768static inline void gen_rldcl(DisasContext *ctx, int mbn)
d9bce9d9 1769{
51789c41 1770 uint32_t mb;
d9bce9d9 1771
9d53c753 1772 mb = MB(ctx->opcode) | (mbn << 5);
51789c41 1773 gen_rldnm(ctx, mb, 63);
d9bce9d9 1774}
36081602 1775GEN_PPC64_R2(rldcl, 0x1E, 0x08);
d9bce9d9 1776/* rldcr - rldcr. */
636aa200 1777static inline void gen_rldcr(DisasContext *ctx, int men)
d9bce9d9 1778{
51789c41 1779 uint32_t me;
d9bce9d9 1780
9d53c753 1781 me = MB(ctx->opcode) | (men << 5);
51789c41 1782 gen_rldnm(ctx, 0, me);
d9bce9d9 1783}
36081602 1784GEN_PPC64_R2(rldcr, 0x1E, 0x09);
d9bce9d9 1785/* rldimi - rldimi. */
636aa200 1786static inline void gen_rldimi(DisasContext *ctx, int mbn, int shn)
d9bce9d9 1787{
271a916e 1788 uint32_t sh, mb, me;
d9bce9d9 1789
9d53c753
JM
1790 sh = SH(ctx->opcode) | (shn << 5);
1791 mb = MB(ctx->opcode) | (mbn << 5);
271a916e 1792 me = 63 - sh;
d03ef511
AJ
1793 if (unlikely(sh == 0 && mb == 0)) {
1794 tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)]);
1795 } else {
1796 TCGv t0, t1;
1797 target_ulong mask;
1798
a7812ae4 1799 t0 = tcg_temp_new();
54843a58 1800 tcg_gen_rotli_tl(t0, cpu_gpr[rS(ctx->opcode)], sh);
a7812ae4 1801 t1 = tcg_temp_new();
d03ef511
AJ
1802 mask = MASK(mb, me);
1803 tcg_gen_andi_tl(t0, t0, mask);
1804 tcg_gen_andi_tl(t1, cpu_gpr[rA(ctx->opcode)], ~mask);
1805 tcg_gen_or_tl(cpu_gpr[rA(ctx->opcode)], t0, t1);
1806 tcg_temp_free(t0);
1807 tcg_temp_free(t1);
51789c41 1808 }
51789c41 1809 if (unlikely(Rc(ctx->opcode) != 0))
d03ef511 1810 gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
d9bce9d9 1811}
36081602 1812GEN_PPC64_R4(rldimi, 0x1E, 0x06);
d9bce9d9
JM
1813#endif
1814
79aceca5 1815/*** Integer shift ***/
99e300ef 1816
54623277 1817/* slw & slw. */
99e300ef 1818static void gen_slw(DisasContext *ctx)
26d67362 1819{
7fd6bf7d 1820 TCGv t0, t1;
26d67362 1821
7fd6bf7d
AJ
1822 t0 = tcg_temp_new();
1823 /* AND rS with a mask that is 0 when rB >= 0x20 */
1824#if defined(TARGET_PPC64)
1825 tcg_gen_shli_tl(t0, cpu_gpr[rB(ctx->opcode)], 0x3a);
1826 tcg_gen_sari_tl(t0, t0, 0x3f);
1827#else
1828 tcg_gen_shli_tl(t0, cpu_gpr[rB(ctx->opcode)], 0x1a);
1829 tcg_gen_sari_tl(t0, t0, 0x1f);
1830#endif
1831 tcg_gen_andc_tl(t0, cpu_gpr[rS(ctx->opcode)], t0);
1832 t1 = tcg_temp_new();
1833 tcg_gen_andi_tl(t1, cpu_gpr[rB(ctx->opcode)], 0x1f);
1834 tcg_gen_shl_tl(cpu_gpr[rA(ctx->opcode)], t0, t1);
1835 tcg_temp_free(t1);
fea0c503 1836 tcg_temp_free(t0);
7fd6bf7d 1837 tcg_gen_ext32u_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rA(ctx->opcode)]);
26d67362
AJ
1838 if (unlikely(Rc(ctx->opcode) != 0))
1839 gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
1840}
99e300ef 1841
54623277 1842/* sraw & sraw. */
99e300ef 1843static void gen_sraw(DisasContext *ctx)
26d67362 1844{
d15f74fb 1845 gen_helper_sraw(cpu_gpr[rA(ctx->opcode)], cpu_env,
a7812ae4 1846 cpu_gpr[rS(ctx->opcode)], cpu_gpr[rB(ctx->opcode)]);
26d67362
AJ
1847 if (unlikely(Rc(ctx->opcode) != 0))
1848 gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
1849}
99e300ef 1850
54623277 1851/* srawi & srawi. */
99e300ef 1852static void gen_srawi(DisasContext *ctx)
79aceca5 1853{
26d67362 1854 int sh = SH(ctx->opcode);
ba4af3e4
RH
1855 TCGv dst = cpu_gpr[rA(ctx->opcode)];
1856 TCGv src = cpu_gpr[rS(ctx->opcode)];
1857 if (sh == 0) {
1858 tcg_gen_mov_tl(dst, src);
da91a00f 1859 tcg_gen_movi_tl(cpu_ca, 0);
26d67362 1860 } else {
ba4af3e4
RH
1861 TCGv t0;
1862 tcg_gen_ext32s_tl(dst, src);
1863 tcg_gen_andi_tl(cpu_ca, dst, (1ULL << sh) - 1);
1864 t0 = tcg_temp_new();
1865 tcg_gen_sari_tl(t0, dst, TARGET_LONG_BITS - 1);
1866 tcg_gen_and_tl(cpu_ca, cpu_ca, t0);
1867 tcg_temp_free(t0);
1868 tcg_gen_setcondi_tl(TCG_COND_NE, cpu_ca, cpu_ca, 0);
1869 tcg_gen_sari_tl(dst, dst, sh);
1870 }
1871 if (unlikely(Rc(ctx->opcode) != 0)) {
1872 gen_set_Rc0(ctx, dst);
d9bce9d9 1873 }
79aceca5 1874}
99e300ef 1875
54623277 1876/* srw & srw. */
99e300ef 1877static void gen_srw(DisasContext *ctx)
26d67362 1878{
fea0c503 1879 TCGv t0, t1;
d9bce9d9 1880
7fd6bf7d
AJ
1881 t0 = tcg_temp_new();
1882 /* AND rS with a mask that is 0 when rB >= 0x20 */
1883#if defined(TARGET_PPC64)
1884 tcg_gen_shli_tl(t0, cpu_gpr[rB(ctx->opcode)], 0x3a);
1885 tcg_gen_sari_tl(t0, t0, 0x3f);
1886#else
1887 tcg_gen_shli_tl(t0, cpu_gpr[rB(ctx->opcode)], 0x1a);
1888 tcg_gen_sari_tl(t0, t0, 0x1f);
1889#endif
1890 tcg_gen_andc_tl(t0, cpu_gpr[rS(ctx->opcode)], t0);
1891 tcg_gen_ext32u_tl(t0, t0);
a7812ae4 1892 t1 = tcg_temp_new();
7fd6bf7d
AJ
1893 tcg_gen_andi_tl(t1, cpu_gpr[rB(ctx->opcode)], 0x1f);
1894 tcg_gen_shr_tl(cpu_gpr[rA(ctx->opcode)], t0, t1);
fea0c503 1895 tcg_temp_free(t1);
fea0c503 1896 tcg_temp_free(t0);
26d67362
AJ
1897 if (unlikely(Rc(ctx->opcode) != 0))
1898 gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
1899}
54623277 1900
d9bce9d9
JM
1901#if defined(TARGET_PPC64)
1902/* sld & sld. */
99e300ef 1903static void gen_sld(DisasContext *ctx)
26d67362 1904{
7fd6bf7d 1905 TCGv t0, t1;
26d67362 1906
7fd6bf7d
AJ
1907 t0 = tcg_temp_new();
1908 /* AND rS with a mask that is 0 when rB >= 0x40 */
1909 tcg_gen_shli_tl(t0, cpu_gpr[rB(ctx->opcode)], 0x39);
1910 tcg_gen_sari_tl(t0, t0, 0x3f);
1911 tcg_gen_andc_tl(t0, cpu_gpr[rS(ctx->opcode)], t0);
1912 t1 = tcg_temp_new();
1913 tcg_gen_andi_tl(t1, cpu_gpr[rB(ctx->opcode)], 0x3f);
1914 tcg_gen_shl_tl(cpu_gpr[rA(ctx->opcode)], t0, t1);
1915 tcg_temp_free(t1);
fea0c503 1916 tcg_temp_free(t0);
26d67362
AJ
1917 if (unlikely(Rc(ctx->opcode) != 0))
1918 gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
1919}
99e300ef 1920
54623277 1921/* srad & srad. */
99e300ef 1922static void gen_srad(DisasContext *ctx)
26d67362 1923{
d15f74fb 1924 gen_helper_srad(cpu_gpr[rA(ctx->opcode)], cpu_env,
a7812ae4 1925 cpu_gpr[rS(ctx->opcode)], cpu_gpr[rB(ctx->opcode)]);
26d67362
AJ
1926 if (unlikely(Rc(ctx->opcode) != 0))
1927 gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
1928}
d9bce9d9 1929/* sradi & sradi. */
636aa200 1930static inline void gen_sradi(DisasContext *ctx, int n)
d9bce9d9 1931{
26d67362 1932 int sh = SH(ctx->opcode) + (n << 5);
ba4af3e4
RH
1933 TCGv dst = cpu_gpr[rA(ctx->opcode)];
1934 TCGv src = cpu_gpr[rS(ctx->opcode)];
1935 if (sh == 0) {
1936 tcg_gen_mov_tl(dst, src);
da91a00f 1937 tcg_gen_movi_tl(cpu_ca, 0);
26d67362 1938 } else {
ba4af3e4
RH
1939 TCGv t0;
1940 tcg_gen_andi_tl(cpu_ca, src, (1ULL << sh) - 1);
1941 t0 = tcg_temp_new();
1942 tcg_gen_sari_tl(t0, src, TARGET_LONG_BITS - 1);
1943 tcg_gen_and_tl(cpu_ca, cpu_ca, t0);
1944 tcg_temp_free(t0);
1945 tcg_gen_setcondi_tl(TCG_COND_NE, cpu_ca, cpu_ca, 0);
1946 tcg_gen_sari_tl(dst, src, sh);
1947 }
1948 if (unlikely(Rc(ctx->opcode) != 0)) {
1949 gen_set_Rc0(ctx, dst);
d9bce9d9 1950 }
d9bce9d9 1951}
e8eaa2c0
BS
1952
1953static void gen_sradi0(DisasContext *ctx)
d9bce9d9
JM
1954{
1955 gen_sradi(ctx, 0);
1956}
e8eaa2c0
BS
1957
1958static void gen_sradi1(DisasContext *ctx)
d9bce9d9
JM
1959{
1960 gen_sradi(ctx, 1);
1961}
99e300ef 1962
54623277 1963/* srd & srd. */
99e300ef 1964static void gen_srd(DisasContext *ctx)
26d67362 1965{
7fd6bf7d 1966 TCGv t0, t1;
26d67362 1967
7fd6bf7d
AJ
1968 t0 = tcg_temp_new();
1969 /* AND rS with a mask that is 0 when rB >= 0x40 */
1970 tcg_gen_shli_tl(t0, cpu_gpr[rB(ctx->opcode)], 0x39);
1971 tcg_gen_sari_tl(t0, t0, 0x3f);
1972 tcg_gen_andc_tl(t0, cpu_gpr[rS(ctx->opcode)], t0);
1973 t1 = tcg_temp_new();
1974 tcg_gen_andi_tl(t1, cpu_gpr[rB(ctx->opcode)], 0x3f);
1975 tcg_gen_shr_tl(cpu_gpr[rA(ctx->opcode)], t0, t1);
1976 tcg_temp_free(t1);
fea0c503 1977 tcg_temp_free(t0);
26d67362
AJ
1978 if (unlikely(Rc(ctx->opcode) != 0))
1979 gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
1980}
d9bce9d9 1981#endif
79aceca5
FB
1982
1983/*** Floating-Point arithmetic ***/
7c58044c 1984#define _GEN_FLOAT_ACB(name, op, op1, op2, isfloat, set_fprf, type) \
99e300ef 1985static void gen_f##name(DisasContext *ctx) \
9a64fbe4 1986{ \
76a66253 1987 if (unlikely(!ctx->fpu_enabled)) { \
e06fcd75 1988 gen_exception(ctx, POWERPC_EXCP_FPU); \
3cc62370
FB
1989 return; \
1990 } \
eb44b959
AJ
1991 /* NIP cannot be restored if the memory exception comes from an helper */ \
1992 gen_update_nip(ctx, ctx->nip - 4); \
7c58044c 1993 gen_reset_fpstatus(); \
8e703949
BS
1994 gen_helper_f##op(cpu_fpr[rD(ctx->opcode)], cpu_env, \
1995 cpu_fpr[rA(ctx->opcode)], \
af12906f 1996 cpu_fpr[rC(ctx->opcode)], cpu_fpr[rB(ctx->opcode)]); \
4ecc3190 1997 if (isfloat) { \
8e703949
BS
1998 gen_helper_frsp(cpu_fpr[rD(ctx->opcode)], cpu_env, \
1999 cpu_fpr[rD(ctx->opcode)]); \
4ecc3190 2000 } \
af12906f
AJ
2001 gen_compute_fprf(cpu_fpr[rD(ctx->opcode)], set_fprf, \
2002 Rc(ctx->opcode) != 0); \
9a64fbe4
FB
2003}
2004
7c58044c
JM
2005#define GEN_FLOAT_ACB(name, op2, set_fprf, type) \
2006_GEN_FLOAT_ACB(name, name, 0x3F, op2, 0, set_fprf, type); \
2007_GEN_FLOAT_ACB(name##s, name, 0x3B, op2, 1, set_fprf, type);
9a64fbe4 2008
7c58044c 2009#define _GEN_FLOAT_AB(name, op, op1, op2, inval, isfloat, set_fprf, type) \
99e300ef 2010static void gen_f##name(DisasContext *ctx) \
9a64fbe4 2011{ \
76a66253 2012 if (unlikely(!ctx->fpu_enabled)) { \
e06fcd75 2013 gen_exception(ctx, POWERPC_EXCP_FPU); \
3cc62370
FB
2014 return; \
2015 } \
eb44b959
AJ
2016 /* NIP cannot be restored if the memory exception comes from an helper */ \
2017 gen_update_nip(ctx, ctx->nip - 4); \
7c58044c 2018 gen_reset_fpstatus(); \
8e703949
BS
2019 gen_helper_f##op(cpu_fpr[rD(ctx->opcode)], cpu_env, \
2020 cpu_fpr[rA(ctx->opcode)], \
af12906f 2021 cpu_fpr[rB(ctx->opcode)]); \
4ecc3190 2022 if (isfloat) { \
8e703949
BS
2023 gen_helper_frsp(cpu_fpr[rD(ctx->opcode)], cpu_env, \
2024 cpu_fpr[rD(ctx->opcode)]); \
4ecc3190 2025 } \
af12906f
AJ
2026 gen_compute_fprf(cpu_fpr[rD(ctx->opcode)], \
2027 set_fprf, Rc(ctx->opcode) != 0); \
9a64fbe4 2028}
7c58044c
JM
2029#define GEN_FLOAT_AB(name, op2, inval, set_fprf, type) \
2030_GEN_FLOAT_AB(name, name, 0x3F, op2, inval, 0, set_fprf, type); \
2031_GEN_FLOAT_AB(name##s, name, 0x3B, op2, inval, 1, set_fprf, type);
9a64fbe4 2032
7c58044c 2033#define _GEN_FLOAT_AC(name, op, op1, op2, inval, 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)], \
2045 cpu_fpr[rC(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)], \
2051 set_fprf, Rc(ctx->opcode) != 0); \
9a64fbe4 2052}
7c58044c
JM
2053#define GEN_FLOAT_AC(name, op2, inval, set_fprf, type) \
2054_GEN_FLOAT_AC(name, name, 0x3F, op2, inval, 0, set_fprf, type); \
2055_GEN_FLOAT_AC(name##s, name, 0x3B, op2, inval, 1, set_fprf, type);
9a64fbe4 2056
7c58044c 2057#define GEN_FLOAT_B(name, op2, op3, set_fprf, type) \
99e300ef 2058static void gen_f##name(DisasContext *ctx) \
9a64fbe4 2059{ \
76a66253 2060 if (unlikely(!ctx->fpu_enabled)) { \
e06fcd75 2061 gen_exception(ctx, POWERPC_EXCP_FPU); \
3cc62370
FB
2062 return; \
2063 } \
eb44b959
AJ
2064 /* NIP cannot be restored if the memory exception comes from an helper */ \
2065 gen_update_nip(ctx, ctx->nip - 4); \
7c58044c 2066 gen_reset_fpstatus(); \
8e703949
BS
2067 gen_helper_f##name(cpu_fpr[rD(ctx->opcode)], cpu_env, \
2068 cpu_fpr[rB(ctx->opcode)]); \
af12906f
AJ
2069 gen_compute_fprf(cpu_fpr[rD(ctx->opcode)], \
2070 set_fprf, Rc(ctx->opcode) != 0); \
79aceca5
FB
2071}
2072
7c58044c 2073#define GEN_FLOAT_BS(name, op1, op2, set_fprf, type) \
99e300ef 2074static void gen_f##name(DisasContext *ctx) \
9a64fbe4 2075{ \
76a66253 2076 if (unlikely(!ctx->fpu_enabled)) { \
e06fcd75 2077 gen_exception(ctx, POWERPC_EXCP_FPU); \
3cc62370
FB
2078 return; \
2079 } \
eb44b959
AJ
2080 /* NIP cannot be restored if the memory exception comes from an helper */ \
2081 gen_update_nip(ctx, ctx->nip - 4); \
7c58044c 2082 gen_reset_fpstatus(); \
8e703949
BS
2083 gen_helper_f##name(cpu_fpr[rD(ctx->opcode)], cpu_env, \
2084 cpu_fpr[rB(ctx->opcode)]); \
af12906f
AJ
2085 gen_compute_fprf(cpu_fpr[rD(ctx->opcode)], \
2086 set_fprf, Rc(ctx->opcode) != 0); \
79aceca5
FB
2087}
2088
9a64fbe4 2089/* fadd - fadds */
7c58044c 2090GEN_FLOAT_AB(add, 0x15, 0x000007C0, 1, PPC_FLOAT);
4ecc3190 2091/* fdiv - fdivs */
7c58044c 2092GEN_FLOAT_AB(div, 0x12, 0x000007C0, 1, PPC_FLOAT);
4ecc3190 2093/* fmul - fmuls */
7c58044c 2094GEN_FLOAT_AC(mul, 0x19, 0x0000F800, 1, PPC_FLOAT);
79aceca5 2095
d7e4b87e 2096/* fre */
7c58044c 2097GEN_FLOAT_BS(re, 0x3F, 0x18, 1, PPC_FLOAT_EXT);
d7e4b87e 2098
a750fc0b 2099/* fres */
7c58044c 2100GEN_FLOAT_BS(res, 0x3B, 0x18, 1, PPC_FLOAT_FRES);
79aceca5 2101
a750fc0b 2102/* frsqrte */
7c58044c
JM
2103GEN_FLOAT_BS(rsqrte, 0x3F, 0x1A, 1, PPC_FLOAT_FRSQRTE);
2104
2105/* frsqrtes */
99e300ef 2106static void gen_frsqrtes(DisasContext *ctx)
7c58044c 2107{
af12906f 2108 if (unlikely(!ctx->fpu_enabled)) {
e06fcd75 2109 gen_exception(ctx, POWERPC_EXCP_FPU);
af12906f
AJ
2110 return;
2111 }
eb44b959
AJ
2112 /* NIP cannot be restored if the memory exception comes from an helper */
2113 gen_update_nip(ctx, ctx->nip - 4);
af12906f 2114 gen_reset_fpstatus();
8e703949
BS
2115 gen_helper_frsqrte(cpu_fpr[rD(ctx->opcode)], cpu_env,
2116 cpu_fpr[rB(ctx->opcode)]);
2117 gen_helper_frsp(cpu_fpr[rD(ctx->opcode)], cpu_env,
2118 cpu_fpr[rD(ctx->opcode)]);
af12906f 2119 gen_compute_fprf(cpu_fpr[rD(ctx->opcode)], 1, Rc(ctx->opcode) != 0);
7c58044c 2120}
79aceca5 2121
a750fc0b 2122/* fsel */
7c58044c 2123_GEN_FLOAT_ACB(sel, sel, 0x3F, 0x17, 0, 0, PPC_FLOAT_FSEL);
4ecc3190 2124/* fsub - fsubs */
7c58044c 2125GEN_FLOAT_AB(sub, 0x14, 0x000007C0, 1, PPC_FLOAT);
79aceca5 2126/* Optional: */
99e300ef 2127
54623277 2128/* fsqrt */
99e300ef 2129static void gen_fsqrt(DisasContext *ctx)
c7d344af 2130{
76a66253 2131 if (unlikely(!ctx->fpu_enabled)) {
e06fcd75 2132 gen_exception(ctx, POWERPC_EXCP_FPU);
c7d344af
FB
2133 return;
2134 }
eb44b959
AJ
2135 /* NIP cannot be restored if the memory exception comes from an helper */
2136 gen_update_nip(ctx, ctx->nip - 4);
7c58044c 2137 gen_reset_fpstatus();
8e703949
BS
2138 gen_helper_fsqrt(cpu_fpr[rD(ctx->opcode)], cpu_env,
2139 cpu_fpr[rB(ctx->opcode)]);
af12906f 2140 gen_compute_fprf(cpu_fpr[rD(ctx->opcode)], 1, Rc(ctx->opcode) != 0);
c7d344af 2141}
79aceca5 2142
99e300ef 2143static void gen_fsqrts(DisasContext *ctx)
79aceca5 2144{
76a66253 2145 if (unlikely(!ctx->fpu_enabled)) {
e06fcd75 2146 gen_exception(ctx, POWERPC_EXCP_FPU);
3cc62370
FB
2147 return;
2148 }
eb44b959
AJ
2149 /* NIP cannot be restored if the memory exception comes from an helper */
2150 gen_update_nip(ctx, ctx->nip - 4);
7c58044c 2151 gen_reset_fpstatus();
8e703949
BS
2152 gen_helper_fsqrt(cpu_fpr[rD(ctx->opcode)], cpu_env,
2153 cpu_fpr[rB(ctx->opcode)]);
2154 gen_helper_frsp(cpu_fpr[rD(ctx->opcode)], cpu_env,
2155 cpu_fpr[rD(ctx->opcode)]);
af12906f 2156 gen_compute_fprf(cpu_fpr[rD(ctx->opcode)], 1, Rc(ctx->opcode) != 0);
79aceca5
FB
2157}
2158
2159/*** Floating-Point multiply-and-add ***/
4ecc3190 2160/* fmadd - fmadds */
7c58044c 2161GEN_FLOAT_ACB(madd, 0x1D, 1, PPC_FLOAT);
4ecc3190 2162/* fmsub - fmsubs */
7c58044c 2163GEN_FLOAT_ACB(msub, 0x1C, 1, PPC_FLOAT);
4ecc3190 2164/* fnmadd - fnmadds */
7c58044c 2165GEN_FLOAT_ACB(nmadd, 0x1F, 1, PPC_FLOAT);
4ecc3190 2166/* fnmsub - fnmsubs */
7c58044c 2167GEN_FLOAT_ACB(nmsub, 0x1E, 1, PPC_FLOAT);
79aceca5
FB
2168
2169/*** Floating-Point round & convert ***/
2170/* fctiw */
7c58044c 2171GEN_FLOAT_B(ctiw, 0x0E, 0x00, 0, PPC_FLOAT);
79aceca5 2172/* fctiwz */
7c58044c 2173GEN_FLOAT_B(ctiwz, 0x0F, 0x00, 0, PPC_FLOAT);
79aceca5 2174/* frsp */
7c58044c 2175GEN_FLOAT_B(rsp, 0x0C, 0x00, 1, PPC_FLOAT);
426613db
JM
2176#if defined(TARGET_PPC64)
2177/* fcfid */
7c58044c 2178GEN_FLOAT_B(cfid, 0x0E, 0x1A, 1, PPC_64B);
426613db 2179/* fctid */
7c58044c 2180GEN_FLOAT_B(ctid, 0x0E, 0x19, 0, PPC_64B);
426613db 2181/* fctidz */
7c58044c 2182GEN_FLOAT_B(ctidz, 0x0F, 0x19, 0, PPC_64B);
426613db 2183#endif
79aceca5 2184
d7e4b87e 2185/* frin */
7c58044c 2186GEN_FLOAT_B(rin, 0x08, 0x0C, 1, PPC_FLOAT_EXT);
d7e4b87e 2187/* friz */
7c58044c 2188GEN_FLOAT_B(riz, 0x08, 0x0D, 1, PPC_FLOAT_EXT);
d7e4b87e 2189/* frip */
7c58044c 2190GEN_FLOAT_B(rip, 0x08, 0x0E, 1, PPC_FLOAT_EXT);
d7e4b87e 2191/* frim */
7c58044c 2192GEN_FLOAT_B(rim, 0x08, 0x0F, 1, PPC_FLOAT_EXT);
d7e4b87e 2193
79aceca5 2194/*** Floating-Point compare ***/
99e300ef 2195
54623277 2196/* fcmpo */
99e300ef 2197static void gen_fcmpo(DisasContext *ctx)
79aceca5 2198{
330c483b 2199 TCGv_i32 crf;
76a66253 2200 if (unlikely(!ctx->fpu_enabled)) {
e06fcd75 2201 gen_exception(ctx, POWERPC_EXCP_FPU);
3cc62370
FB
2202 return;
2203 }
eb44b959
AJ
2204 /* NIP cannot be restored if the memory exception comes from an helper */
2205 gen_update_nip(ctx, ctx->nip - 4);
7c58044c 2206 gen_reset_fpstatus();
9a819377 2207 crf = tcg_const_i32(crfD(ctx->opcode));
8e703949
BS
2208 gen_helper_fcmpo(cpu_env, cpu_fpr[rA(ctx->opcode)],
2209 cpu_fpr[rB(ctx->opcode)], crf);
330c483b 2210 tcg_temp_free_i32(crf);
8e703949 2211 gen_helper_float_check_status(cpu_env);
79aceca5
FB
2212}
2213
2214/* fcmpu */
99e300ef 2215static void gen_fcmpu(DisasContext *ctx)
79aceca5 2216{
330c483b 2217 TCGv_i32 crf;
76a66253 2218 if (unlikely(!ctx->fpu_enabled)) {
e06fcd75 2219 gen_exception(ctx, POWERPC_EXCP_FPU);
3cc62370
FB
2220 return;
2221 }
eb44b959
AJ
2222 /* NIP cannot be restored if the memory exception comes from an helper */
2223 gen_update_nip(ctx, ctx->nip - 4);
7c58044c 2224 gen_reset_fpstatus();
9a819377 2225 crf = tcg_const_i32(crfD(ctx->opcode));
8e703949
BS
2226 gen_helper_fcmpu(cpu_env, cpu_fpr[rA(ctx->opcode)],
2227 cpu_fpr[rB(ctx->opcode)], crf);
330c483b 2228 tcg_temp_free_i32(crf);
8e703949 2229 gen_helper_float_check_status(cpu_env);
79aceca5
FB
2230}
2231
9a64fbe4
FB
2232/*** Floating-point move ***/
2233/* fabs */
7c58044c 2234/* XXX: beware that fabs never checks for NaNs nor update FPSCR */
bf45a2e6
AJ
2235static void gen_fabs(DisasContext *ctx)
2236{
2237 if (unlikely(!ctx->fpu_enabled)) {
2238 gen_exception(ctx, POWERPC_EXCP_FPU);
2239 return;
2240 }
2241 tcg_gen_andi_i64(cpu_fpr[rD(ctx->opcode)], cpu_fpr[rB(ctx->opcode)],
2242 ~(1ULL << 63));
2243 gen_compute_fprf(cpu_fpr[rD(ctx->opcode)], 0, Rc(ctx->opcode) != 0);
2244}
9a64fbe4
FB
2245
2246/* fmr - fmr. */
7c58044c 2247/* XXX: beware that fmr never checks for NaNs nor update FPSCR */
99e300ef 2248static void gen_fmr(DisasContext *ctx)
9a64fbe4 2249{
76a66253 2250 if (unlikely(!ctx->fpu_enabled)) {
e06fcd75 2251 gen_exception(ctx, POWERPC_EXCP_FPU);
3cc62370
FB
2252 return;
2253 }
af12906f
AJ
2254 tcg_gen_mov_i64(cpu_fpr[rD(ctx->opcode)], cpu_fpr[rB(ctx->opcode)]);
2255 gen_compute_fprf(cpu_fpr[rD(ctx->opcode)], 0, Rc(ctx->opcode) != 0);
9a64fbe4
FB
2256}
2257
2258/* fnabs */
7c58044c 2259/* XXX: beware that fnabs never checks for NaNs nor update FPSCR */
bf45a2e6
AJ
2260static void gen_fnabs(DisasContext *ctx)
2261{
2262 if (unlikely(!ctx->fpu_enabled)) {
2263 gen_exception(ctx, POWERPC_EXCP_FPU);
2264 return;
2265 }
2266 tcg_gen_ori_i64(cpu_fpr[rD(ctx->opcode)], cpu_fpr[rB(ctx->opcode)],
2267 1ULL << 63);
2268 gen_compute_fprf(cpu_fpr[rD(ctx->opcode)], 0, Rc(ctx->opcode) != 0);
2269}
2270
9a64fbe4 2271/* fneg */
7c58044c 2272/* XXX: beware that fneg never checks for NaNs nor update FPSCR */
bf45a2e6
AJ
2273static void gen_fneg(DisasContext *ctx)
2274{
2275 if (unlikely(!ctx->fpu_enabled)) {
2276 gen_exception(ctx, POWERPC_EXCP_FPU);
2277 return;
2278 }
2279 tcg_gen_xori_i64(cpu_fpr[rD(ctx->opcode)], cpu_fpr[rB(ctx->opcode)],
2280 1ULL << 63);
2281 gen_compute_fprf(cpu_fpr[rD(ctx->opcode)], 0, Rc(ctx->opcode) != 0);
2282}
9a64fbe4 2283
f0332888
AJ
2284/* fcpsgn: PowerPC 2.05 specification */
2285/* XXX: beware that fcpsgn never checks for NaNs nor update FPSCR */
2286static void gen_fcpsgn(DisasContext *ctx)
2287{
2288 if (unlikely(!ctx->fpu_enabled)) {
2289 gen_exception(ctx, POWERPC_EXCP_FPU);
2290 return;
2291 }
2292 tcg_gen_deposit_i64(cpu_fpr[rD(ctx->opcode)], cpu_fpr[rA(ctx->opcode)],
2293 cpu_fpr[rB(ctx->opcode)], 0, 63);
2294 gen_compute_fprf(cpu_fpr[rD(ctx->opcode)], 0, Rc(ctx->opcode) != 0);
2295}
2296
097ec5d8
TM
2297static void gen_fmrgew(DisasContext *ctx)
2298{
2299 TCGv_i64 b0;
2300 if (unlikely(!ctx->fpu_enabled)) {
2301 gen_exception(ctx, POWERPC_EXCP_FPU);
2302 return;
2303 }
2304 b0 = tcg_temp_new_i64();
2305 tcg_gen_shri_i64(b0, cpu_fpr[rB(ctx->opcode)], 32);
2306 tcg_gen_deposit_i64(cpu_fpr[rD(ctx->opcode)], cpu_fpr[rA(ctx->opcode)],
2307 b0, 0, 32);
2308 tcg_temp_free_i64(b0);
2309}
2310
2311static void gen_fmrgow(DisasContext *ctx)
2312{
2313 if (unlikely(!ctx->fpu_enabled)) {
2314 gen_exception(ctx, POWERPC_EXCP_FPU);
2315 return;
2316 }
2317 tcg_gen_deposit_i64(cpu_fpr[rD(ctx->opcode)],
2318 cpu_fpr[rB(ctx->opcode)],
2319 cpu_fpr[rA(ctx->opcode)],
2320 32, 32);
2321}
2322
79aceca5 2323/*** Floating-Point status & ctrl register ***/
99e300ef 2324
54623277 2325/* mcrfs */
99e300ef 2326static void gen_mcrfs(DisasContext *ctx)
79aceca5 2327{
30304420 2328 TCGv tmp = tcg_temp_new();
7c58044c
JM
2329 int bfa;
2330
76a66253 2331 if (unlikely(!ctx->fpu_enabled)) {
e06fcd75 2332 gen_exception(ctx, POWERPC_EXCP_FPU);
3cc62370
FB
2333 return;
2334 }
7c58044c 2335 bfa = 4 * (7 - crfS(ctx->opcode));
30304420
DG
2336 tcg_gen_shri_tl(tmp, cpu_fpscr, bfa);
2337 tcg_gen_trunc_tl_i32(cpu_crf[crfD(ctx->opcode)], tmp);
2338 tcg_temp_free(tmp);
e1571908 2339 tcg_gen_andi_i32(cpu_crf[crfD(ctx->opcode)], cpu_crf[crfD(ctx->opcode)], 0xf);
30304420 2340 tcg_gen_andi_tl(cpu_fpscr, cpu_fpscr, ~(0xF << bfa));
79aceca5
FB
2341}
2342
2343/* mffs */
99e300ef 2344static void gen_mffs(DisasContext *ctx)
79aceca5 2345{
76a66253 2346 if (unlikely(!ctx->fpu_enabled)) {
e06fcd75 2347 gen_exception(ctx, POWERPC_EXCP_FPU);
3cc62370
FB
2348 return;
2349 }
7c58044c 2350 gen_reset_fpstatus();
30304420 2351 tcg_gen_extu_tl_i64(cpu_fpr[rD(ctx->opcode)], cpu_fpscr);
af12906f 2352 gen_compute_fprf(cpu_fpr[rD(ctx->opcode)], 0, Rc(ctx->opcode) != 0);
79aceca5
FB
2353}
2354
2355/* mtfsb0 */
99e300ef 2356static void gen_mtfsb0(DisasContext *ctx)
79aceca5 2357{
fb0eaffc 2358 uint8_t crb;
3b46e624 2359
76a66253 2360 if (unlikely(!ctx->fpu_enabled)) {
e06fcd75 2361 gen_exception(ctx, POWERPC_EXCP_FPU);
3cc62370
FB
2362 return;
2363 }
6e35d524 2364 crb = 31 - crbD(ctx->opcode);
7c58044c 2365 gen_reset_fpstatus();
6e35d524 2366 if (likely(crb != FPSCR_FEX && crb != FPSCR_VX)) {
eb44b959
AJ
2367 TCGv_i32 t0;
2368 /* NIP cannot be restored if the memory exception comes from an helper */
2369 gen_update_nip(ctx, ctx->nip - 4);
2370 t0 = tcg_const_i32(crb);
8e703949 2371 gen_helper_fpscr_clrbit(cpu_env, t0);
6e35d524
AJ
2372 tcg_temp_free_i32(t0);
2373 }
7c58044c 2374 if (unlikely(Rc(ctx->opcode) != 0)) {
30304420
DG
2375 tcg_gen_trunc_tl_i32(cpu_crf[1], cpu_fpscr);
2376 tcg_gen_shri_i32(cpu_crf[1], cpu_crf[1], FPSCR_OX);
7c58044c 2377 }
79aceca5
FB
2378}
2379
2380/* mtfsb1 */
99e300ef 2381static void gen_mtfsb1(DisasContext *ctx)
79aceca5 2382{
fb0eaffc 2383 uint8_t crb;
3b46e624 2384
76a66253 2385 if (unlikely(!ctx->fpu_enabled)) {
e06fcd75 2386 gen_exception(ctx, POWERPC_EXCP_FPU);
3cc62370
FB
2387 return;
2388 }
6e35d524 2389 crb = 31 - crbD(ctx->opcode);
7c58044c
JM
2390 gen_reset_fpstatus();
2391 /* XXX: we pretend we can only do IEEE floating-point computations */
af12906f 2392 if (likely(crb != FPSCR_FEX && crb != FPSCR_VX && crb != FPSCR_NI)) {
eb44b959
AJ
2393 TCGv_i32 t0;
2394 /* NIP cannot be restored if the memory exception comes from an helper */
2395 gen_update_nip(ctx, ctx->nip - 4);
2396 t0 = tcg_const_i32(crb);
8e703949 2397 gen_helper_fpscr_setbit(cpu_env, t0);
0f2f39c2 2398 tcg_temp_free_i32(t0);
af12906f 2399 }
7c58044c 2400 if (unlikely(Rc(ctx->opcode) != 0)) {
30304420
DG
2401 tcg_gen_trunc_tl_i32(cpu_crf[1], cpu_fpscr);
2402 tcg_gen_shri_i32(cpu_crf[1], cpu_crf[1], FPSCR_OX);
7c58044c
JM
2403 }
2404 /* We can raise a differed exception */
8e703949 2405 gen_helper_float_check_status(cpu_env);
79aceca5
FB
2406}
2407
2408/* mtfsf */
99e300ef 2409static void gen_mtfsf(DisasContext *ctx)
79aceca5 2410{
0f2f39c2 2411 TCGv_i32 t0;
7d08d856 2412 int flm, l, w;
af12906f 2413
76a66253 2414 if (unlikely(!ctx->fpu_enabled)) {
e06fcd75 2415 gen_exception(ctx, POWERPC_EXCP_FPU);
3cc62370
FB
2416 return;
2417 }
7d08d856
AJ
2418 flm = FPFLM(ctx->opcode);
2419 l = FPL(ctx->opcode);
2420 w = FPW(ctx->opcode);
2421 if (unlikely(w & !(ctx->insns_flags2 & PPC2_ISA205))) {
2422 gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL);
2423 return;
2424 }
eb44b959
AJ
2425 /* NIP cannot be restored if the memory exception comes from an helper */
2426 gen_update_nip(ctx, ctx->nip - 4);
7c58044c 2427 gen_reset_fpstatus();
7d08d856
AJ
2428 if (l) {
2429 t0 = tcg_const_i32((ctx->insns_flags2 & PPC2_ISA205) ? 0xffff : 0xff);
2430 } else {
2431 t0 = tcg_const_i32(flm << (w * 8));
2432 }
8e703949 2433 gen_helper_store_fpscr(cpu_env, cpu_fpr[rB(ctx->opcode)], t0);
0f2f39c2 2434 tcg_temp_free_i32(t0);
7c58044c 2435 if (unlikely(Rc(ctx->opcode) != 0)) {
30304420
DG
2436 tcg_gen_trunc_tl_i32(cpu_crf[1], cpu_fpscr);
2437 tcg_gen_shri_i32(cpu_crf[1], cpu_crf[1], FPSCR_OX);
7c58044c
JM
2438 }
2439 /* We can raise a differed exception */
8e703949 2440 gen_helper_float_check_status(cpu_env);
79aceca5
FB
2441}
2442
2443/* mtfsfi */
99e300ef 2444static void gen_mtfsfi(DisasContext *ctx)
79aceca5 2445{
7d08d856 2446 int bf, sh, w;
0f2f39c2
AJ
2447 TCGv_i64 t0;
2448 TCGv_i32 t1;
7c58044c 2449
76a66253 2450 if (unlikely(!ctx->fpu_enabled)) {
e06fcd75 2451 gen_exception(ctx, POWERPC_EXCP_FPU);
3cc62370
FB
2452 return;
2453 }
7d08d856
AJ
2454 w = FPW(ctx->opcode);
2455 bf = FPBF(ctx->opcode);
2456 if (unlikely(w & !(ctx->insns_flags2 & PPC2_ISA205))) {
2457 gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL);
2458 return;
2459 }
2460 sh = (8 * w) + 7 - bf;
eb44b959
AJ
2461 /* NIP cannot be restored if the memory exception comes from an helper */
2462 gen_update_nip(ctx, ctx->nip - 4);
7c58044c 2463 gen_reset_fpstatus();
7d08d856 2464 t0 = tcg_const_i64(((uint64_t)FPIMM(ctx->opcode)) << (4 * sh));
af12906f 2465 t1 = tcg_const_i32(1 << sh);
8e703949 2466 gen_helper_store_fpscr(cpu_env, t0, t1);
0f2f39c2
AJ
2467 tcg_temp_free_i64(t0);
2468 tcg_temp_free_i32(t1);
7c58044c 2469 if (unlikely(Rc(ctx->opcode) != 0)) {
30304420
DG
2470 tcg_gen_trunc_tl_i32(cpu_crf[1], cpu_fpscr);
2471 tcg_gen_shri_i32(cpu_crf[1], cpu_crf[1], FPSCR_OX);
7c58044c
JM
2472 }
2473 /* We can raise a differed exception */
8e703949 2474 gen_helper_float_check_status(cpu_env);
79aceca5
FB
2475}
2476
76a66253
JM
2477/*** Addressing modes ***/
2478/* Register indirect with immediate index : EA = (rA|0) + SIMM */
636aa200
BS
2479static inline void gen_addr_imm_index(DisasContext *ctx, TCGv EA,
2480 target_long maskl)
76a66253
JM
2481{
2482 target_long simm = SIMM(ctx->opcode);
2483
be147d08 2484 simm &= ~maskl;
76db3ba4 2485 if (rA(ctx->opcode) == 0) {
c791fe84
RH
2486 if (NARROW_MODE(ctx)) {
2487 simm = (uint32_t)simm;
2488 }
e2be8d8d 2489 tcg_gen_movi_tl(EA, simm);
76db3ba4 2490 } else if (likely(simm != 0)) {
e2be8d8d 2491 tcg_gen_addi_tl(EA, cpu_gpr[rA(ctx->opcode)], simm);
c791fe84 2492 if (NARROW_MODE(ctx)) {
76db3ba4
AJ
2493 tcg_gen_ext32u_tl(EA, EA);
2494 }
76db3ba4 2495 } else {
c791fe84 2496 if (NARROW_MODE(ctx)) {
76db3ba4 2497 tcg_gen_ext32u_tl(EA, cpu_gpr[rA(ctx->opcode)]);
c791fe84
RH
2498 } else {
2499 tcg_gen_mov_tl(EA, cpu_gpr[rA(ctx->opcode)]);
2500 }
76db3ba4 2501 }
76a66253
JM
2502}
2503
636aa200 2504static inline void gen_addr_reg_index(DisasContext *ctx, TCGv EA)
76a66253 2505{
76db3ba4 2506 if (rA(ctx->opcode) == 0) {
c791fe84 2507 if (NARROW_MODE(ctx)) {
76db3ba4 2508 tcg_gen_ext32u_tl(EA, cpu_gpr[rB(ctx->opcode)]);
c791fe84
RH
2509 } else {
2510 tcg_gen_mov_tl(EA, cpu_gpr[rB(ctx->opcode)]);
2511 }
76db3ba4 2512 } else {
e2be8d8d 2513 tcg_gen_add_tl(EA, cpu_gpr[rA(ctx->opcode)], cpu_gpr[rB(ctx->opcode)]);
c791fe84 2514 if (NARROW_MODE(ctx)) {
76db3ba4
AJ
2515 tcg_gen_ext32u_tl(EA, EA);
2516 }
76db3ba4 2517 }
76a66253
JM
2518}
2519
636aa200 2520static inline void gen_addr_register(DisasContext *ctx, TCGv EA)
76a66253 2521{
76db3ba4 2522 if (rA(ctx->opcode) == 0) {
e2be8d8d 2523 tcg_gen_movi_tl(EA, 0);
c791fe84
RH
2524 } else if (NARROW_MODE(ctx)) {
2525 tcg_gen_ext32u_tl(EA, cpu_gpr[rA(ctx->opcode)]);
76db3ba4 2526 } else {
c791fe84 2527 tcg_gen_mov_tl(EA, cpu_gpr[rA(ctx->opcode)]);
76db3ba4
AJ
2528 }
2529}
2530
636aa200
BS
2531static inline void gen_addr_add(DisasContext *ctx, TCGv ret, TCGv arg1,
2532 target_long val)
76db3ba4
AJ
2533{
2534 tcg_gen_addi_tl(ret, arg1, val);
c791fe84 2535 if (NARROW_MODE(ctx)) {
76db3ba4
AJ
2536 tcg_gen_ext32u_tl(ret, ret);
2537 }
76a66253
JM
2538}
2539
636aa200 2540static inline void gen_check_align(DisasContext *ctx, TCGv EA, int mask)
cf360a32
AJ
2541{
2542 int l1 = gen_new_label();
2543 TCGv t0 = tcg_temp_new();
2544 TCGv_i32 t1, t2;
2545 /* NIP cannot be restored if the memory exception comes from an helper */
2546 gen_update_nip(ctx, ctx->nip - 4);
2547 tcg_gen_andi_tl(t0, EA, mask);
2548 tcg_gen_brcondi_tl(TCG_COND_EQ, t0, 0, l1);
2549 t1 = tcg_const_i32(POWERPC_EXCP_ALIGN);
2550 t2 = tcg_const_i32(0);
e5f17ac6 2551 gen_helper_raise_exception_err(cpu_env, t1, t2);
cf360a32
AJ
2552 tcg_temp_free_i32(t1);
2553 tcg_temp_free_i32(t2);
2554 gen_set_label(l1);
2555 tcg_temp_free(t0);
2556}
2557
7863667f 2558/*** Integer load ***/
636aa200 2559static inline void gen_qemu_ld8u(DisasContext *ctx, TCGv arg1, TCGv arg2)
76db3ba4
AJ
2560{
2561 tcg_gen_qemu_ld8u(arg1, arg2, ctx->mem_idx);
2562}
2563
636aa200 2564static inline void gen_qemu_ld8s(DisasContext *ctx, TCGv arg1, TCGv arg2)
76db3ba4
AJ
2565{
2566 tcg_gen_qemu_ld8s(arg1, arg2, ctx->mem_idx);
2567}
2568
636aa200 2569static inline void gen_qemu_ld16u(DisasContext *ctx, TCGv arg1, TCGv arg2)
76db3ba4
AJ
2570{
2571 tcg_gen_qemu_ld16u(arg1, arg2, ctx->mem_idx);
2572 if (unlikely(ctx->le_mode)) {
fa3966a3 2573 tcg_gen_bswap16_tl(arg1, arg1);
76db3ba4 2574 }
b61f2753
AJ
2575}
2576
636aa200 2577static inline void gen_qemu_ld16s(DisasContext *ctx, TCGv arg1, TCGv arg2)
b61f2753 2578{
76db3ba4 2579 if (unlikely(ctx->le_mode)) {
76db3ba4 2580 tcg_gen_qemu_ld16u(arg1, arg2, ctx->mem_idx);
fa3966a3 2581 tcg_gen_bswap16_tl(arg1, arg1);
76db3ba4 2582 tcg_gen_ext16s_tl(arg1, arg1);
76db3ba4
AJ
2583 } else {
2584 tcg_gen_qemu_ld16s(arg1, arg2, ctx->mem_idx);
2585 }
b61f2753
AJ
2586}
2587
636aa200 2588static inline void gen_qemu_ld32u(DisasContext *ctx, TCGv arg1, TCGv arg2)
b61f2753 2589{
76db3ba4
AJ
2590 tcg_gen_qemu_ld32u(arg1, arg2, ctx->mem_idx);
2591 if (unlikely(ctx->le_mode)) {
fa3966a3 2592 tcg_gen_bswap32_tl(arg1, arg1);
76db3ba4 2593 }
b61f2753
AJ
2594}
2595
f976b09e
AG
2596static void gen_qemu_ld32u_i64(DisasContext *ctx, TCGv_i64 val, TCGv addr)
2597{
2598 TCGv tmp = tcg_temp_new();
2599 gen_qemu_ld32u(ctx, tmp, addr);
2600 tcg_gen_extu_tl_i64(val, tmp);
2601 tcg_temp_free(tmp);
2602}
2603
636aa200 2604static inline void gen_qemu_ld32s(DisasContext *ctx, TCGv arg1, TCGv arg2)
b61f2753 2605{
a457e7ee 2606 if (unlikely(ctx->le_mode)) {
76db3ba4 2607 tcg_gen_qemu_ld32u(arg1, arg2, ctx->mem_idx);
fa3966a3
AJ
2608 tcg_gen_bswap32_tl(arg1, arg1);
2609 tcg_gen_ext32s_tl(arg1, arg1);
b61f2753 2610 } else
76db3ba4 2611 tcg_gen_qemu_ld32s(arg1, arg2, ctx->mem_idx);
b61f2753
AJ
2612}
2613
cac7f0ba
TM
2614static void gen_qemu_ld32s_i64(DisasContext *ctx, TCGv_i64 val, TCGv addr)
2615{
2616 TCGv tmp = tcg_temp_new();
2617 gen_qemu_ld32s(ctx, tmp, addr);
2618 tcg_gen_ext_tl_i64(val, tmp);
2619 tcg_temp_free(tmp);
2620}
2621
636aa200 2622static inline void gen_qemu_ld64(DisasContext *ctx, TCGv_i64 arg1, TCGv arg2)
b61f2753 2623{
76db3ba4
AJ
2624 tcg_gen_qemu_ld64(arg1, arg2, ctx->mem_idx);
2625 if (unlikely(ctx->le_mode)) {
66896cb8 2626 tcg_gen_bswap64_i64(arg1, arg1);
76db3ba4 2627 }
b61f2753
AJ
2628}
2629
636aa200 2630static inline void gen_qemu_st8(DisasContext *ctx, TCGv arg1, TCGv arg2)
b61f2753 2631{
76db3ba4 2632 tcg_gen_qemu_st8(arg1, arg2, ctx->mem_idx);
b61f2753
AJ
2633}
2634
636aa200 2635static inline void gen_qemu_st16(DisasContext *ctx, TCGv arg1, TCGv arg2)
b61f2753 2636{
76db3ba4 2637 if (unlikely(ctx->le_mode)) {
76db3ba4
AJ
2638 TCGv t0 = tcg_temp_new();
2639 tcg_gen_ext16u_tl(t0, arg1);
fa3966a3 2640 tcg_gen_bswap16_tl(t0, t0);
76db3ba4
AJ
2641 tcg_gen_qemu_st16(t0, arg2, ctx->mem_idx);
2642 tcg_temp_free(t0);
76db3ba4
AJ
2643 } else {
2644 tcg_gen_qemu_st16(arg1, arg2, ctx->mem_idx);
2645 }
b61f2753
AJ
2646}
2647
636aa200 2648static inline void gen_qemu_st32(DisasContext *ctx, TCGv arg1, TCGv arg2)
b61f2753 2649{
76db3ba4 2650 if (unlikely(ctx->le_mode)) {
fa3966a3
AJ
2651 TCGv t0 = tcg_temp_new();
2652 tcg_gen_ext32u_tl(t0, arg1);
2653 tcg_gen_bswap32_tl(t0, t0);
76db3ba4
AJ
2654 tcg_gen_qemu_st32(t0, arg2, ctx->mem_idx);
2655 tcg_temp_free(t0);
76db3ba4
AJ
2656 } else {
2657 tcg_gen_qemu_st32(arg1, arg2, ctx->mem_idx);
2658 }
b61f2753
AJ
2659}
2660
f976b09e
AG
2661static void gen_qemu_st32_i64(DisasContext *ctx, TCGv_i64 val, TCGv addr)
2662{
2663 TCGv tmp = tcg_temp_new();
2664 tcg_gen_trunc_i64_tl(tmp, val);
2665 gen_qemu_st32(ctx, tmp, addr);
2666 tcg_temp_free(tmp);
2667}
2668
636aa200 2669static inline void gen_qemu_st64(DisasContext *ctx, TCGv_i64 arg1, TCGv arg2)
b61f2753 2670{
76db3ba4 2671 if (unlikely(ctx->le_mode)) {
a7812ae4 2672 TCGv_i64 t0 = tcg_temp_new_i64();
66896cb8 2673 tcg_gen_bswap64_i64(t0, arg1);
76db3ba4 2674 tcg_gen_qemu_st64(t0, arg2, ctx->mem_idx);
a7812ae4 2675 tcg_temp_free_i64(t0);
b61f2753 2676 } else
76db3ba4 2677 tcg_gen_qemu_st64(arg1, arg2, ctx->mem_idx);
b61f2753
AJ
2678}
2679
0c8aacd4 2680#define GEN_LD(name, ldop, opc, type) \
99e300ef 2681static void glue(gen_, name)(DisasContext *ctx) \
79aceca5 2682{ \
76db3ba4
AJ
2683 TCGv EA; \
2684 gen_set_access_type(ctx, ACCESS_INT); \
2685 EA = tcg_temp_new(); \
2686 gen_addr_imm_index(ctx, EA, 0); \
2687 gen_qemu_##ldop(ctx, cpu_gpr[rD(ctx->opcode)], EA); \
b61f2753 2688 tcg_temp_free(EA); \
79aceca5
FB
2689}
2690
0c8aacd4 2691#define GEN_LDU(name, ldop, opc, type) \
99e300ef 2692static void glue(gen_, name##u)(DisasContext *ctx) \
79aceca5 2693{ \
b61f2753 2694 TCGv EA; \
76a66253
JM
2695 if (unlikely(rA(ctx->opcode) == 0 || \
2696 rA(ctx->opcode) == rD(ctx->opcode))) { \
e06fcd75 2697 gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL); \
9fddaa0c 2698 return; \
9a64fbe4 2699 } \
76db3ba4 2700 gen_set_access_type(ctx, ACCESS_INT); \
0c8aacd4 2701 EA = tcg_temp_new(); \
9d53c753 2702 if (type == PPC_64B) \
76db3ba4 2703 gen_addr_imm_index(ctx, EA, 0x03); \
9d53c753 2704 else \
76db3ba4
AJ
2705 gen_addr_imm_index(ctx, EA, 0); \
2706 gen_qemu_##ldop(ctx, cpu_gpr[rD(ctx->opcode)], EA); \
b61f2753
AJ
2707 tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], EA); \
2708 tcg_temp_free(EA); \
79aceca5
FB
2709}
2710
0c8aacd4 2711#define GEN_LDUX(name, ldop, opc2, opc3, type) \
99e300ef 2712static void glue(gen_, name##ux)(DisasContext *ctx) \
79aceca5 2713{ \
b61f2753 2714 TCGv EA; \
76a66253
JM
2715 if (unlikely(rA(ctx->opcode) == 0 || \
2716 rA(ctx->opcode) == rD(ctx->opcode))) { \
e06fcd75 2717 gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL); \
9fddaa0c 2718 return; \
9a64fbe4 2719 } \
76db3ba4 2720 gen_set_access_type(ctx, ACCESS_INT); \
0c8aacd4 2721 EA = tcg_temp_new(); \
76db3ba4
AJ
2722 gen_addr_reg_index(ctx, EA); \
2723 gen_qemu_##ldop(ctx, cpu_gpr[rD(ctx->opcode)], EA); \
b61f2753
AJ
2724 tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], EA); \
2725 tcg_temp_free(EA); \
79aceca5
FB
2726}
2727
cd6e9320 2728#define GEN_LDX_E(name, ldop, opc2, opc3, type, type2) \
99e300ef 2729static void glue(gen_, name##x)(DisasContext *ctx) \
79aceca5 2730{ \
76db3ba4
AJ
2731 TCGv EA; \
2732 gen_set_access_type(ctx, ACCESS_INT); \
2733 EA = tcg_temp_new(); \
2734 gen_addr_reg_index(ctx, EA); \
2735 gen_qemu_##ldop(ctx, cpu_gpr[rD(ctx->opcode)], EA); \
b61f2753 2736 tcg_temp_free(EA); \
79aceca5 2737}
cd6e9320
TH
2738#define GEN_LDX(name, ldop, opc2, opc3, type) \
2739 GEN_LDX_E(name, ldop, opc2, opc3, type, PPC_NONE)
79aceca5 2740
0c8aacd4
AJ
2741#define GEN_LDS(name, ldop, op, type) \
2742GEN_LD(name, ldop, op | 0x20, type); \
2743GEN_LDU(name, ldop, op | 0x21, type); \
2744GEN_LDUX(name, ldop, 0x17, op | 0x01, type); \
2745GEN_LDX(name, ldop, 0x17, op | 0x00, type)
79aceca5
FB
2746
2747/* lbz lbzu lbzux lbzx */
0c8aacd4 2748GEN_LDS(lbz, ld8u, 0x02, PPC_INTEGER);
79aceca5 2749/* lha lhau lhaux lhax */
0c8aacd4 2750GEN_LDS(lha, ld16s, 0x0A, PPC_INTEGER);
79aceca5 2751/* lhz lhzu lhzux lhzx */
0c8aacd4 2752GEN_LDS(lhz, ld16u, 0x08, PPC_INTEGER);
79aceca5 2753/* lwz lwzu lwzux lwzx */
0c8aacd4 2754GEN_LDS(lwz, ld32u, 0x00, PPC_INTEGER);
d9bce9d9 2755#if defined(TARGET_PPC64)
d9bce9d9 2756/* lwaux */
0c8aacd4 2757GEN_LDUX(lwa, ld32s, 0x15, 0x0B, PPC_64B);
d9bce9d9 2758/* lwax */
0c8aacd4 2759GEN_LDX(lwa, ld32s, 0x15, 0x0A, PPC_64B);
d9bce9d9 2760/* ldux */
0c8aacd4 2761GEN_LDUX(ld, ld64, 0x15, 0x01, PPC_64B);
d9bce9d9 2762/* ldx */
0c8aacd4 2763GEN_LDX(ld, ld64, 0x15, 0x00, PPC_64B);
99e300ef
BS
2764
2765static void gen_ld(DisasContext *ctx)
d9bce9d9 2766{
b61f2753 2767 TCGv EA;
d9bce9d9
JM
2768 if (Rc(ctx->opcode)) {
2769 if (unlikely(rA(ctx->opcode) == 0 ||
2770 rA(ctx->opcode) == rD(ctx->opcode))) {
e06fcd75 2771 gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL);
d9bce9d9
JM
2772 return;
2773 }
2774 }
76db3ba4 2775 gen_set_access_type(ctx, ACCESS_INT);
a7812ae4 2776 EA = tcg_temp_new();
76db3ba4 2777 gen_addr_imm_index(ctx, EA, 0x03);
d9bce9d9
JM
2778 if (ctx->opcode & 0x02) {
2779 /* lwa (lwau is undefined) */
76db3ba4 2780 gen_qemu_ld32s(ctx, cpu_gpr[rD(ctx->opcode)], EA);
d9bce9d9
JM
2781 } else {
2782 /* ld - ldu */
76db3ba4 2783 gen_qemu_ld64(ctx, cpu_gpr[rD(ctx->opcode)], EA);
d9bce9d9 2784 }
d9bce9d9 2785 if (Rc(ctx->opcode))
b61f2753
AJ
2786 tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], EA);
2787 tcg_temp_free(EA);
d9bce9d9 2788}
99e300ef 2789
54623277 2790/* lq */
99e300ef 2791static void gen_lq(DisasContext *ctx)
be147d08
JM
2792{
2793#if defined(CONFIG_USER_ONLY)
e06fcd75 2794 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
be147d08
JM
2795#else
2796 int ra, rd;
b61f2753 2797 TCGv EA;
be147d08
JM
2798
2799 /* Restore CPU state */
76db3ba4 2800 if (unlikely(ctx->mem_idx == 0)) {
e06fcd75 2801 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
be147d08
JM
2802 return;
2803 }
2804 ra = rA(ctx->opcode);
2805 rd = rD(ctx->opcode);
2806 if (unlikely((rd & 1) || rd == ra)) {
e06fcd75 2807 gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL);
be147d08
JM
2808 return;
2809 }
76db3ba4 2810 if (unlikely(ctx->le_mode)) {
be147d08 2811 /* Little-endian mode is not handled */
e06fcd75 2812 gen_exception_err(ctx, POWERPC_EXCP_ALIGN, POWERPC_EXCP_ALIGN_LE);
be147d08
JM
2813 return;
2814 }
76db3ba4 2815 gen_set_access_type(ctx, ACCESS_INT);
a7812ae4 2816 EA = tcg_temp_new();
76db3ba4
AJ
2817 gen_addr_imm_index(ctx, EA, 0x0F);
2818 gen_qemu_ld64(ctx, cpu_gpr[rd], EA);
2819 gen_addr_add(ctx, EA, EA, 8);
2820 gen_qemu_ld64(ctx, cpu_gpr[rd+1], EA);
b61f2753 2821 tcg_temp_free(EA);
be147d08
JM
2822#endif
2823}
d9bce9d9 2824#endif
79aceca5
FB
2825
2826/*** Integer store ***/
0c8aacd4 2827#define GEN_ST(name, stop, opc, type) \
99e300ef 2828static void glue(gen_, name)(DisasContext *ctx) \
79aceca5 2829{ \
76db3ba4
AJ
2830 TCGv EA; \
2831 gen_set_access_type(ctx, ACCESS_INT); \
2832 EA = tcg_temp_new(); \
2833 gen_addr_imm_index(ctx, EA, 0); \
2834 gen_qemu_##stop(ctx, cpu_gpr[rS(ctx->opcode)], EA); \
b61f2753 2835 tcg_temp_free(EA); \
79aceca5
FB
2836}
2837
0c8aacd4 2838#define GEN_STU(name, stop, opc, type) \
99e300ef 2839static void glue(gen_, stop##u)(DisasContext *ctx) \
79aceca5 2840{ \
b61f2753 2841 TCGv EA; \
76a66253 2842 if (unlikely(rA(ctx->opcode) == 0)) { \
e06fcd75 2843 gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL); \
9fddaa0c 2844 return; \
9a64fbe4 2845 } \
76db3ba4 2846 gen_set_access_type(ctx, ACCESS_INT); \
0c8aacd4 2847 EA = tcg_temp_new(); \
9d53c753 2848 if (type == PPC_64B) \
76db3ba4 2849 gen_addr_imm_index(ctx, EA, 0x03); \
9d53c753 2850 else \
76db3ba4
AJ
2851 gen_addr_imm_index(ctx, EA, 0); \
2852 gen_qemu_##stop(ctx, cpu_gpr[rS(ctx->opcode)], EA); \
b61f2753
AJ
2853 tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], EA); \
2854 tcg_temp_free(EA); \
79aceca5
FB
2855}
2856
0c8aacd4 2857#define GEN_STUX(name, stop, opc2, opc3, type) \
99e300ef 2858static void glue(gen_, name##ux)(DisasContext *ctx) \
79aceca5 2859{ \
b61f2753 2860 TCGv EA; \
76a66253 2861 if (unlikely(rA(ctx->opcode) == 0)) { \
e06fcd75 2862 gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL); \
9fddaa0c 2863 return; \
9a64fbe4 2864 } \
76db3ba4 2865 gen_set_access_type(ctx, ACCESS_INT); \
0c8aacd4 2866 EA = tcg_temp_new(); \
76db3ba4
AJ
2867 gen_addr_reg_index(ctx, EA); \
2868 gen_qemu_##stop(ctx, cpu_gpr[rS(ctx->opcode)], EA); \
b61f2753
AJ
2869 tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], EA); \
2870 tcg_temp_free(EA); \
79aceca5
FB
2871}
2872
cd6e9320
TH
2873#define GEN_STX_E(name, stop, opc2, opc3, type, type2) \
2874static void glue(gen_, name##x)(DisasContext *ctx) \
79aceca5 2875{ \
76db3ba4
AJ
2876 TCGv EA; \
2877 gen_set_access_type(ctx, ACCESS_INT); \
2878 EA = tcg_temp_new(); \
2879 gen_addr_reg_index(ctx, EA); \
2880 gen_qemu_##stop(ctx, cpu_gpr[rS(ctx->opcode)], EA); \
b61f2753 2881 tcg_temp_free(EA); \
79aceca5 2882}
cd6e9320
TH
2883#define GEN_STX(name, stop, opc2, opc3, type) \
2884 GEN_STX_E(name, stop, opc2, opc3, type, PPC_NONE)
79aceca5 2885
0c8aacd4
AJ
2886#define GEN_STS(name, stop, op, type) \
2887GEN_ST(name, stop, op | 0x20, type); \
2888GEN_STU(name, stop, op | 0x21, type); \
2889GEN_STUX(name, stop, 0x17, op | 0x01, type); \
2890GEN_STX(name, stop, 0x17, op | 0x00, type)
79aceca5
FB
2891
2892/* stb stbu stbux stbx */
0c8aacd4 2893GEN_STS(stb, st8, 0x06, PPC_INTEGER);
79aceca5 2894/* sth sthu sthux sthx */
0c8aacd4 2895GEN_STS(sth, st16, 0x0C, PPC_INTEGER);
79aceca5 2896/* stw stwu stwux stwx */
0c8aacd4 2897GEN_STS(stw, st32, 0x04, PPC_INTEGER);
d9bce9d9 2898#if defined(TARGET_PPC64)
0c8aacd4
AJ
2899GEN_STUX(std, st64, 0x15, 0x05, PPC_64B);
2900GEN_STX(std, st64, 0x15, 0x04, PPC_64B);
99e300ef
BS
2901
2902static void gen_std(DisasContext *ctx)
d9bce9d9 2903{
be147d08 2904 int rs;
b61f2753 2905 TCGv EA;
be147d08
JM
2906
2907 rs = rS(ctx->opcode);
2908 if ((ctx->opcode & 0x3) == 0x2) {
2909#if defined(CONFIG_USER_ONLY)
e06fcd75 2910 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
be147d08
JM
2911#else
2912 /* stq */
76db3ba4 2913 if (unlikely(ctx->mem_idx == 0)) {
e06fcd75 2914 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
be147d08
JM
2915 return;
2916 }
2917 if (unlikely(rs & 1)) {
e06fcd75 2918 gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL);
d9bce9d9
JM
2919 return;
2920 }
76db3ba4 2921 if (unlikely(ctx->le_mode)) {
be147d08 2922 /* Little-endian mode is not handled */
e06fcd75 2923 gen_exception_err(ctx, POWERPC_EXCP_ALIGN, POWERPC_EXCP_ALIGN_LE);
be147d08
JM
2924 return;
2925 }
76db3ba4 2926 gen_set_access_type(ctx, ACCESS_INT);
a7812ae4 2927 EA = tcg_temp_new();
76db3ba4
AJ
2928 gen_addr_imm_index(ctx, EA, 0x03);
2929 gen_qemu_st64(ctx, cpu_gpr[rs], EA);
2930 gen_addr_add(ctx, EA, EA, 8);
2931 gen_qemu_st64(ctx, cpu_gpr[rs+1], EA);
b61f2753 2932 tcg_temp_free(EA);
be147d08
JM
2933#endif
2934 } else {
2935 /* std / stdu */
2936 if (Rc(ctx->opcode)) {
2937 if (unlikely(rA(ctx->opcode) == 0)) {
e06fcd75 2938 gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL);
be147d08
JM
2939 return;
2940 }
2941 }
76db3ba4 2942 gen_set_access_type(ctx, ACCESS_INT);
a7812ae4 2943 EA = tcg_temp_new();
76db3ba4
AJ
2944 gen_addr_imm_index(ctx, EA, 0x03);
2945 gen_qemu_st64(ctx, cpu_gpr[rs], EA);
be147d08 2946 if (Rc(ctx->opcode))
b61f2753
AJ
2947 tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], EA);
2948 tcg_temp_free(EA);
d9bce9d9 2949 }
d9bce9d9
JM
2950}
2951#endif
79aceca5
FB
2952/*** Integer load and store with byte reverse ***/
2953/* lhbrx */
86178a57 2954static inline void gen_qemu_ld16ur(DisasContext *ctx, TCGv arg1, TCGv arg2)
b61f2753 2955{
76db3ba4
AJ
2956 tcg_gen_qemu_ld16u(arg1, arg2, ctx->mem_idx);
2957 if (likely(!ctx->le_mode)) {
fa3966a3 2958 tcg_gen_bswap16_tl(arg1, arg1);
76db3ba4 2959 }
b61f2753 2960}
0c8aacd4 2961GEN_LDX(lhbr, ld16ur, 0x16, 0x18, PPC_INTEGER);
b61f2753 2962
79aceca5 2963/* lwbrx */
86178a57 2964static inline void gen_qemu_ld32ur(DisasContext *ctx, TCGv arg1, TCGv arg2)
b61f2753 2965{
76db3ba4
AJ
2966 tcg_gen_qemu_ld32u(arg1, arg2, ctx->mem_idx);
2967 if (likely(!ctx->le_mode)) {
fa3966a3 2968 tcg_gen_bswap32_tl(arg1, arg1);
76db3ba4 2969 }
b61f2753 2970}
0c8aacd4 2971GEN_LDX(lwbr, ld32ur, 0x16, 0x10, PPC_INTEGER);
b61f2753 2972
cd6e9320
TH
2973#if defined(TARGET_PPC64)
2974/* ldbrx */
2975static inline void gen_qemu_ld64ur(DisasContext *ctx, TCGv arg1, TCGv arg2)
2976{
2977 tcg_gen_qemu_ld64(arg1, arg2, ctx->mem_idx);
2978 if (likely(!ctx->le_mode)) {
2979 tcg_gen_bswap64_tl(arg1, arg1);
2980 }
2981}
2982GEN_LDX_E(ldbr, ld64ur, 0x14, 0x10, PPC_NONE, PPC2_DBRX);
2983#endif /* TARGET_PPC64 */
2984
79aceca5 2985/* sthbrx */
86178a57 2986static inline void gen_qemu_st16r(DisasContext *ctx, TCGv arg1, TCGv arg2)
b61f2753 2987{
76db3ba4 2988 if (likely(!ctx->le_mode)) {
76db3ba4
AJ
2989 TCGv t0 = tcg_temp_new();
2990 tcg_gen_ext16u_tl(t0, arg1);
fa3966a3 2991 tcg_gen_bswap16_tl(t0, t0);
76db3ba4
AJ
2992 tcg_gen_qemu_st16(t0, arg2, ctx->mem_idx);
2993 tcg_temp_free(t0);
76db3ba4
AJ
2994 } else {
2995 tcg_gen_qemu_st16(arg1, arg2, ctx->mem_idx);
2996 }
b61f2753 2997}
0c8aacd4 2998GEN_STX(sthbr, st16r, 0x16, 0x1C, PPC_INTEGER);
b61f2753 2999
79aceca5 3000/* stwbrx */
86178a57 3001static inline void gen_qemu_st32r(DisasContext *ctx, TCGv arg1, TCGv arg2)
b61f2753 3002{
76db3ba4 3003 if (likely(!ctx->le_mode)) {
fa3966a3
AJ
3004 TCGv t0 = tcg_temp_new();
3005 tcg_gen_ext32u_tl(t0, arg1);
3006 tcg_gen_bswap32_tl(t0, t0);
76db3ba4
AJ
3007 tcg_gen_qemu_st32(t0, arg2, ctx->mem_idx);
3008 tcg_temp_free(t0);
76db3ba4
AJ
3009 } else {
3010 tcg_gen_qemu_st32(arg1, arg2, ctx->mem_idx);
3011 }
b61f2753 3012}
0c8aacd4 3013GEN_STX(stwbr, st32r, 0x16, 0x14, PPC_INTEGER);
79aceca5 3014
cd6e9320
TH
3015#if defined(TARGET_PPC64)
3016/* stdbrx */
3017static inline void gen_qemu_st64r(DisasContext *ctx, TCGv arg1, TCGv arg2)
3018{
3019 if (likely(!ctx->le_mode)) {
3020 TCGv t0 = tcg_temp_new();
3021 tcg_gen_bswap64_tl(t0, arg1);
3022 tcg_gen_qemu_st64(t0, arg2, ctx->mem_idx);
3023 tcg_temp_free(t0);
3024 } else {
3025 tcg_gen_qemu_st64(arg1, arg2, ctx->mem_idx);
3026 }
3027}
3028GEN_STX_E(stdbr, st64r, 0x14, 0x14, PPC_NONE, PPC2_DBRX);
3029#endif /* TARGET_PPC64 */
3030
79aceca5 3031/*** Integer load and store multiple ***/
99e300ef 3032
54623277 3033/* lmw */
99e300ef 3034static void gen_lmw(DisasContext *ctx)
79aceca5 3035{
76db3ba4
AJ
3036 TCGv t0;
3037 TCGv_i32 t1;
3038 gen_set_access_type(ctx, ACCESS_INT);
76a66253 3039 /* NIP cannot be restored if the memory exception comes from an helper */
d9bce9d9 3040 gen_update_nip(ctx, ctx->nip - 4);
76db3ba4
AJ
3041 t0 = tcg_temp_new();
3042 t1 = tcg_const_i32(rD(ctx->opcode));
3043 gen_addr_imm_index(ctx, t0, 0);
2f5a189c 3044 gen_helper_lmw(cpu_env, t0, t1);
ff4a62cd
AJ
3045 tcg_temp_free(t0);
3046 tcg_temp_free_i32(t1);
79aceca5
FB
3047}
3048
3049/* stmw */
99e300ef 3050static void gen_stmw(DisasContext *ctx)
79aceca5 3051{
76db3ba4
AJ
3052 TCGv t0;
3053 TCGv_i32 t1;
3054 gen_set_access_type(ctx, ACCESS_INT);
76a66253 3055 /* NIP cannot be restored if the memory exception comes from an helper */
d9bce9d9 3056 gen_update_nip(ctx, ctx->nip - 4);
76db3ba4
AJ
3057 t0 = tcg_temp_new();
3058 t1 = tcg_const_i32(rS(ctx->opcode));
3059 gen_addr_imm_index(ctx, t0, 0);
2f5a189c 3060 gen_helper_stmw(cpu_env, t0, t1);
ff4a62cd
AJ
3061 tcg_temp_free(t0);
3062 tcg_temp_free_i32(t1);
79aceca5
FB
3063}
3064
3065/*** Integer load and store strings ***/
54623277 3066
79aceca5 3067/* lswi */
3fc6c082 3068/* PowerPC32 specification says we must generate an exception if
9a64fbe4
FB
3069 * rA is in the range of registers to be loaded.
3070 * In an other hand, IBM says this is valid, but rA won't be loaded.
3071 * For now, I'll follow the spec...
3072 */
99e300ef 3073static void gen_lswi(DisasContext *ctx)
79aceca5 3074{
dfbc799d
AJ
3075 TCGv t0;
3076 TCGv_i32 t1, t2;
79aceca5
FB
3077 int nb = NB(ctx->opcode);
3078 int start = rD(ctx->opcode);
9a64fbe4 3079 int ra = rA(ctx->opcode);
79aceca5
FB
3080 int nr;
3081
3082 if (nb == 0)
3083 nb = 32;
3084 nr = nb / 4;
76a66253
JM
3085 if (unlikely(((start + nr) > 32 &&
3086 start <= ra && (start + nr - 32) > ra) ||
3087 ((start + nr) <= 32 && start <= ra && (start + nr) > ra))) {
e06fcd75 3088 gen_inval_exception(ctx, POWERPC_EXCP_INVAL_LSWX);
9fddaa0c 3089 return;
297d8e62 3090 }
76db3ba4 3091 gen_set_access_type(ctx, ACCESS_INT);
8dd4983c 3092 /* NIP cannot be restored if the memory exception comes from an helper */
d9bce9d9 3093 gen_update_nip(ctx, ctx->nip - 4);
dfbc799d 3094 t0 = tcg_temp_new();
76db3ba4 3095 gen_addr_register(ctx, t0);
dfbc799d
AJ
3096 t1 = tcg_const_i32(nb);
3097 t2 = tcg_const_i32(start);
2f5a189c 3098 gen_helper_lsw(cpu_env, t0, t1, t2);
dfbc799d
AJ
3099 tcg_temp_free(t0);
3100 tcg_temp_free_i32(t1);
3101 tcg_temp_free_i32(t2);
79aceca5
FB
3102}
3103
3104/* lswx */
99e300ef 3105static void gen_lswx(DisasContext *ctx)
79aceca5 3106{
76db3ba4
AJ
3107 TCGv t0;
3108 TCGv_i32 t1, t2, t3;
3109 gen_set_access_type(ctx, ACCESS_INT);
76a66253 3110 /* NIP cannot be restored if the memory exception comes from an helper */
d9bce9d9 3111 gen_update_nip(ctx, ctx->nip - 4);
76db3ba4
AJ
3112 t0 = tcg_temp_new();
3113 gen_addr_reg_index(ctx, t0);
3114 t1 = tcg_const_i32(rD(ctx->opcode));
3115 t2 = tcg_const_i32(rA(ctx->opcode));
3116 t3 = tcg_const_i32(rB(ctx->opcode));
2f5a189c 3117 gen_helper_lswx(cpu_env, t0, t1, t2, t3);
dfbc799d
AJ
3118 tcg_temp_free(t0);
3119 tcg_temp_free_i32(t1);
3120 tcg_temp_free_i32(t2);
3121 tcg_temp_free_i32(t3);
79aceca5
FB
3122}
3123
3124/* stswi */
99e300ef 3125static void gen_stswi(DisasContext *ctx)
79aceca5 3126{
76db3ba4
AJ
3127 TCGv t0;
3128 TCGv_i32 t1, t2;
4b3686fa 3129 int nb = NB(ctx->opcode);
76db3ba4 3130 gen_set_access_type(ctx, ACCESS_INT);
76a66253 3131 /* NIP cannot be restored if the memory exception comes from an helper */
d9bce9d9 3132 gen_update_nip(ctx, ctx->nip - 4);
76db3ba4
AJ
3133 t0 = tcg_temp_new();
3134 gen_addr_register(ctx, t0);
4b3686fa
FB
3135 if (nb == 0)
3136 nb = 32;
dfbc799d 3137 t1 = tcg_const_i32(nb);
76db3ba4 3138 t2 = tcg_const_i32(rS(ctx->opcode));
2f5a189c 3139 gen_helper_stsw(cpu_env, t0, t1, t2);
dfbc799d
AJ
3140 tcg_temp_free(t0);
3141 tcg_temp_free_i32(t1);
3142 tcg_temp_free_i32(t2);
79aceca5
FB
3143}
3144
3145/* stswx */
99e300ef 3146static void gen_stswx(DisasContext *ctx)
79aceca5 3147{
76db3ba4
AJ
3148 TCGv t0;
3149 TCGv_i32 t1, t2;
3150 gen_set_access_type(ctx, ACCESS_INT);
8dd4983c 3151 /* NIP cannot be restored if the memory exception comes from an helper */
5fafdf24 3152 gen_update_nip(ctx, ctx->nip - 4);
76db3ba4
AJ
3153 t0 = tcg_temp_new();
3154 gen_addr_reg_index(ctx, t0);
3155 t1 = tcg_temp_new_i32();
dfbc799d
AJ
3156 tcg_gen_trunc_tl_i32(t1, cpu_xer);
3157 tcg_gen_andi_i32(t1, t1, 0x7F);
76db3ba4 3158 t2 = tcg_const_i32(rS(ctx->opcode));
2f5a189c 3159 gen_helper_stsw(cpu_env, t0, t1, t2);
dfbc799d
AJ
3160 tcg_temp_free(t0);
3161 tcg_temp_free_i32(t1);
3162 tcg_temp_free_i32(t2);
79aceca5
FB
3163}
3164
3165/*** Memory synchronisation ***/
3166/* eieio */
99e300ef 3167static void gen_eieio(DisasContext *ctx)
79aceca5 3168{
79aceca5
FB
3169}
3170
3171/* isync */
99e300ef 3172static void gen_isync(DisasContext *ctx)
79aceca5 3173{
e06fcd75 3174 gen_stop_exception(ctx);
79aceca5
FB
3175}
3176
111bfab3 3177/* lwarx */
99e300ef 3178static void gen_lwarx(DisasContext *ctx)
79aceca5 3179{
76db3ba4 3180 TCGv t0;
18b21a2f 3181 TCGv gpr = cpu_gpr[rD(ctx->opcode)];
76db3ba4
AJ
3182 gen_set_access_type(ctx, ACCESS_RES);
3183 t0 = tcg_temp_local_new();
3184 gen_addr_reg_index(ctx, t0);
cf360a32 3185 gen_check_align(ctx, t0, 0x03);
18b21a2f 3186 gen_qemu_ld32u(ctx, gpr, t0);
cf360a32 3187 tcg_gen_mov_tl(cpu_reserve, t0);
1328c2bf 3188 tcg_gen_st_tl(gpr, cpu_env, offsetof(CPUPPCState, reserve_val));
cf360a32 3189 tcg_temp_free(t0);
79aceca5
FB
3190}
3191
4425265b
NF
3192#if defined(CONFIG_USER_ONLY)
3193static void gen_conditional_store (DisasContext *ctx, TCGv EA,
3194 int reg, int size)
3195{
3196 TCGv t0 = tcg_temp_new();
3197 uint32_t save_exception = ctx->exception;
3198
1328c2bf 3199 tcg_gen_st_tl(EA, cpu_env, offsetof(CPUPPCState, reserve_ea));
4425265b 3200 tcg_gen_movi_tl(t0, (size << 5) | reg);
1328c2bf 3201 tcg_gen_st_tl(t0, cpu_env, offsetof(CPUPPCState, reserve_info));
4425265b
NF
3202 tcg_temp_free(t0);
3203 gen_update_nip(ctx, ctx->nip-4);
3204 ctx->exception = POWERPC_EXCP_BRANCH;
3205 gen_exception(ctx, POWERPC_EXCP_STCX);
3206 ctx->exception = save_exception;
3207}
3208#endif
3209
79aceca5 3210/* stwcx. */
e8eaa2c0 3211static void gen_stwcx_(DisasContext *ctx)
79aceca5 3212{
76db3ba4
AJ
3213 TCGv t0;
3214 gen_set_access_type(ctx, ACCESS_RES);
3215 t0 = tcg_temp_local_new();
3216 gen_addr_reg_index(ctx, t0);
cf360a32 3217 gen_check_align(ctx, t0, 0x03);
4425265b
NF
3218#if defined(CONFIG_USER_ONLY)
3219 gen_conditional_store(ctx, t0, rS(ctx->opcode), 4);
3220#else
3221 {
3222 int l1;
3223
da91a00f 3224 tcg_gen_trunc_tl_i32(cpu_crf[0], cpu_so);
4425265b
NF
3225 l1 = gen_new_label();
3226 tcg_gen_brcond_tl(TCG_COND_NE, t0, cpu_reserve, l1);
3227 tcg_gen_ori_i32(cpu_crf[0], cpu_crf[0], 1 << CRF_EQ);
3228 gen_qemu_st32(ctx, cpu_gpr[rS(ctx->opcode)], t0);
3229 gen_set_label(l1);
3230 tcg_gen_movi_tl(cpu_reserve, -1);
3231 }
3232#endif
cf360a32 3233 tcg_temp_free(t0);
79aceca5
FB
3234}
3235
426613db 3236#if defined(TARGET_PPC64)
426613db 3237/* ldarx */
99e300ef 3238static void gen_ldarx(DisasContext *ctx)
426613db 3239{
76db3ba4 3240 TCGv t0;
18b21a2f 3241 TCGv gpr = cpu_gpr[rD(ctx->opcode)];
76db3ba4
AJ
3242 gen_set_access_type(ctx, ACCESS_RES);
3243 t0 = tcg_temp_local_new();
3244 gen_addr_reg_index(ctx, t0);
cf360a32 3245 gen_check_align(ctx, t0, 0x07);
18b21a2f 3246 gen_qemu_ld64(ctx, gpr, t0);
cf360a32 3247 tcg_gen_mov_tl(cpu_reserve, t0);
1328c2bf 3248 tcg_gen_st_tl(gpr, cpu_env, offsetof(CPUPPCState, reserve_val));
cf360a32 3249 tcg_temp_free(t0);
426613db
JM
3250}
3251
3252/* stdcx. */
e8eaa2c0 3253static void gen_stdcx_(DisasContext *ctx)
426613db 3254{
76db3ba4
AJ
3255 TCGv t0;
3256 gen_set_access_type(ctx, ACCESS_RES);
3257 t0 = tcg_temp_local_new();
3258 gen_addr_reg_index(ctx, t0);
cf360a32 3259 gen_check_align(ctx, t0, 0x07);
4425265b
NF
3260#if defined(CONFIG_USER_ONLY)
3261 gen_conditional_store(ctx, t0, rS(ctx->opcode), 8);
3262#else
3263 {
3264 int l1;
da91a00f 3265 tcg_gen_trunc_tl_i32(cpu_crf[0], cpu_so);
4425265b
NF
3266 l1 = gen_new_label();
3267 tcg_gen_brcond_tl(TCG_COND_NE, t0, cpu_reserve, l1);
3268 tcg_gen_ori_i32(cpu_crf[0], cpu_crf[0], 1 << CRF_EQ);
3269 gen_qemu_st64(ctx, cpu_gpr[rS(ctx->opcode)], t0);
3270 gen_set_label(l1);
3271 tcg_gen_movi_tl(cpu_reserve, -1);
3272 }
3273#endif
cf360a32 3274 tcg_temp_free(t0);
426613db
JM
3275}
3276#endif /* defined(TARGET_PPC64) */
3277
79aceca5 3278/* sync */
99e300ef 3279static void gen_sync(DisasContext *ctx)
79aceca5 3280{
79aceca5
FB
3281}
3282
0db1b20e 3283/* wait */
99e300ef 3284static void gen_wait(DisasContext *ctx)
0db1b20e 3285{
931ff272 3286 TCGv_i32 t0 = tcg_temp_new_i32();
259186a7
AF
3287 tcg_gen_st_i32(t0, cpu_env,
3288 -offsetof(PowerPCCPU, env) + offsetof(CPUState, halted));
931ff272 3289 tcg_temp_free_i32(t0);
0db1b20e 3290 /* Stop translation, as the CPU is supposed to sleep from now */
e06fcd75 3291 gen_exception_err(ctx, EXCP_HLT, 1);
0db1b20e
JM
3292}
3293
79aceca5 3294/*** Floating-point load ***/
a0d7d5a7 3295#define GEN_LDF(name, ldop, opc, type) \
99e300ef 3296static void glue(gen_, name)(DisasContext *ctx) \
79aceca5 3297{ \
a0d7d5a7 3298 TCGv EA; \
76a66253 3299 if (unlikely(!ctx->fpu_enabled)) { \
e06fcd75 3300 gen_exception(ctx, POWERPC_EXCP_FPU); \
4ecc3190
FB
3301 return; \
3302 } \
76db3ba4 3303 gen_set_access_type(ctx, ACCESS_FLOAT); \
a0d7d5a7 3304 EA = tcg_temp_new(); \
76db3ba4
AJ
3305 gen_addr_imm_index(ctx, EA, 0); \
3306 gen_qemu_##ldop(ctx, cpu_fpr[rD(ctx->opcode)], EA); \
a0d7d5a7 3307 tcg_temp_free(EA); \
79aceca5
FB
3308}
3309
a0d7d5a7 3310#define GEN_LDUF(name, ldop, opc, type) \
99e300ef 3311static void glue(gen_, name##u)(DisasContext *ctx) \
79aceca5 3312{ \
a0d7d5a7 3313 TCGv EA; \
76a66253 3314 if (unlikely(!ctx->fpu_enabled)) { \
e06fcd75 3315 gen_exception(ctx, POWERPC_EXCP_FPU); \
4ecc3190
FB
3316 return; \
3317 } \
76a66253 3318 if (unlikely(rA(ctx->opcode) == 0)) { \
e06fcd75 3319 gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL); \
9fddaa0c 3320 return; \
9a64fbe4 3321 } \
76db3ba4 3322 gen_set_access_type(ctx, ACCESS_FLOAT); \
a0d7d5a7 3323 EA = tcg_temp_new(); \
76db3ba4
AJ
3324 gen_addr_imm_index(ctx, EA, 0); \
3325 gen_qemu_##ldop(ctx, cpu_fpr[rD(ctx->opcode)], EA); \
a0d7d5a7
AJ
3326 tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], EA); \
3327 tcg_temp_free(EA); \
79aceca5
FB
3328}
3329
a0d7d5a7 3330#define GEN_LDUXF(name, ldop, opc, type) \
99e300ef 3331static void glue(gen_, name##ux)(DisasContext *ctx) \
79aceca5 3332{ \
a0d7d5a7 3333 TCGv EA; \
76a66253 3334 if (unlikely(!ctx->fpu_enabled)) { \
e06fcd75 3335 gen_exception(ctx, POWERPC_EXCP_FPU); \
4ecc3190
FB
3336 return; \
3337 } \
76a66253 3338 if (unlikely(rA(ctx->opcode) == 0)) { \
e06fcd75 3339 gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL); \
9fddaa0c 3340 return; \
9a64fbe4 3341 } \
76db3ba4 3342 gen_set_access_type(ctx, ACCESS_FLOAT); \
a0d7d5a7 3343 EA = tcg_temp_new(); \
76db3ba4
AJ
3344 gen_addr_reg_index(ctx, EA); \
3345 gen_qemu_##ldop(ctx, cpu_fpr[rD(ctx->opcode)], EA); \
a0d7d5a7
AJ
3346 tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], EA); \
3347 tcg_temp_free(EA); \
79aceca5
FB
3348}
3349
a0d7d5a7 3350#define GEN_LDXF(name, ldop, opc2, opc3, type) \
99e300ef 3351static void glue(gen_, name##x)(DisasContext *ctx) \
79aceca5 3352{ \
a0d7d5a7 3353 TCGv EA; \
76a66253 3354 if (unlikely(!ctx->fpu_enabled)) { \
e06fcd75 3355 gen_exception(ctx, POWERPC_EXCP_FPU); \
4ecc3190
FB
3356 return; \
3357 } \
76db3ba4 3358 gen_set_access_type(ctx, ACCESS_FLOAT); \
a0d7d5a7 3359 EA = tcg_temp_new(); \
76db3ba4
AJ
3360 gen_addr_reg_index(ctx, EA); \
3361 gen_qemu_##ldop(ctx, cpu_fpr[rD(ctx->opcode)], EA); \
a0d7d5a7 3362 tcg_temp_free(EA); \
79aceca5
FB
3363}
3364
a0d7d5a7
AJ
3365#define GEN_LDFS(name, ldop, op, type) \
3366GEN_LDF(name, ldop, op | 0x20, type); \
3367GEN_LDUF(name, ldop, op | 0x21, type); \
3368GEN_LDUXF(name, ldop, op | 0x01, type); \
3369GEN_LDXF(name, ldop, 0x17, op | 0x00, type)
3370
636aa200 3371static inline void gen_qemu_ld32fs(DisasContext *ctx, TCGv_i64 arg1, TCGv arg2)
a0d7d5a7
AJ
3372{
3373 TCGv t0 = tcg_temp_new();
3374 TCGv_i32 t1 = tcg_temp_new_i32();
76db3ba4 3375 gen_qemu_ld32u(ctx, t0, arg2);
a0d7d5a7
AJ
3376 tcg_gen_trunc_tl_i32(t1, t0);
3377 tcg_temp_free(t0);
8e703949 3378 gen_helper_float32_to_float64(arg1, cpu_env, t1);
a0d7d5a7
AJ
3379 tcg_temp_free_i32(t1);
3380}
79aceca5 3381
a0d7d5a7
AJ
3382 /* lfd lfdu lfdux lfdx */
3383GEN_LDFS(lfd, ld64, 0x12, PPC_FLOAT);
3384 /* lfs lfsu lfsux lfsx */
3385GEN_LDFS(lfs, ld32fs, 0x10, PPC_FLOAT);
79aceca5 3386
05050ee8
AJ
3387/* lfdp */
3388static void gen_lfdp(DisasContext *ctx)
3389{
3390 TCGv EA;
3391 if (unlikely(!ctx->fpu_enabled)) {
3392 gen_exception(ctx, POWERPC_EXCP_FPU);
3393 return;
3394 }
3395 gen_set_access_type(ctx, ACCESS_FLOAT);
3396 EA = tcg_temp_new();
3397 gen_addr_imm_index(ctx, EA, 0); \
3398 if (unlikely(ctx->le_mode)) {
3399 gen_qemu_ld64(ctx, cpu_fpr[rD(ctx->opcode) + 1], EA);
3400 tcg_gen_addi_tl(EA, EA, 8);
3401 gen_qemu_ld64(ctx, cpu_fpr[rD(ctx->opcode)], EA);
3402 } else {
3403 gen_qemu_ld64(ctx, cpu_fpr[rD(ctx->opcode)], EA);
3404 tcg_gen_addi_tl(EA, EA, 8);
3405 gen_qemu_ld64(ctx, cpu_fpr[rD(ctx->opcode) + 1], EA);
3406 }
3407 tcg_temp_free(EA);
3408}
3409
3410/* lfdpx */
3411static void gen_lfdpx(DisasContext *ctx)
3412{
3413 TCGv EA;
3414 if (unlikely(!ctx->fpu_enabled)) {
3415 gen_exception(ctx, POWERPC_EXCP_FPU);
3416 return;
3417 }
3418 gen_set_access_type(ctx, ACCESS_FLOAT);
3419 EA = tcg_temp_new();
3420 gen_addr_reg_index(ctx, EA);
3421 if (unlikely(ctx->le_mode)) {
3422 gen_qemu_ld64(ctx, cpu_fpr[rD(ctx->opcode) + 1], EA);
3423 tcg_gen_addi_tl(EA, EA, 8);
3424 gen_qemu_ld64(ctx, cpu_fpr[rD(ctx->opcode)], EA);
3425 } else {
3426 gen_qemu_ld64(ctx, cpu_fpr[rD(ctx->opcode)], EA);
3427 tcg_gen_addi_tl(EA, EA, 8);
3428 gen_qemu_ld64(ctx, cpu_fpr[rD(ctx->opcode) + 1], EA);
3429 }
3430 tcg_temp_free(EA);
3431}
3432
199f830d
AJ
3433/* lfiwax */
3434static void gen_lfiwax(DisasContext *ctx)
3435{
3436 TCGv EA;
3437 TCGv t0;
3438 if (unlikely(!ctx->fpu_enabled)) {
3439 gen_exception(ctx, POWERPC_EXCP_FPU);
3440 return;
3441 }
3442 gen_set_access_type(ctx, ACCESS_FLOAT);
3443 EA = tcg_temp_new();
3444 t0 = tcg_temp_new();
3445 gen_addr_reg_index(ctx, EA);
909eedb7 3446 gen_qemu_ld32s(ctx, t0, EA);
199f830d 3447 tcg_gen_ext_tl_i64(cpu_fpr[rD(ctx->opcode)], t0);
199f830d
AJ
3448 tcg_temp_free(EA);
3449 tcg_temp_free(t0);
3450}
3451
79aceca5 3452/*** Floating-point store ***/
a0d7d5a7 3453#define GEN_STF(name, stop, opc, type) \
99e300ef 3454static void glue(gen_, name)(DisasContext *ctx) \
79aceca5 3455{ \
a0d7d5a7 3456 TCGv EA; \
76a66253 3457 if (unlikely(!ctx->fpu_enabled)) { \
e06fcd75 3458 gen_exception(ctx, POWERPC_EXCP_FPU); \
4ecc3190
FB
3459 return; \
3460 } \
76db3ba4 3461 gen_set_access_type(ctx, ACCESS_FLOAT); \
a0d7d5a7 3462 EA = tcg_temp_new(); \
76db3ba4
AJ
3463 gen_addr_imm_index(ctx, EA, 0); \
3464 gen_qemu_##stop(ctx, cpu_fpr[rS(ctx->opcode)], EA); \
a0d7d5a7 3465 tcg_temp_free(EA); \
79aceca5
FB
3466}
3467
a0d7d5a7 3468#define GEN_STUF(name, stop, opc, type) \
99e300ef 3469static void glue(gen_, name##u)(DisasContext *ctx) \
79aceca5 3470{ \
a0d7d5a7 3471 TCGv EA; \
76a66253 3472 if (unlikely(!ctx->fpu_enabled)) { \
e06fcd75 3473 gen_exception(ctx, POWERPC_EXCP_FPU); \
4ecc3190
FB
3474 return; \
3475 } \
76a66253 3476 if (unlikely(rA(ctx->opcode) == 0)) { \
e06fcd75 3477 gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL); \
9fddaa0c 3478 return; \
9a64fbe4 3479 } \
76db3ba4 3480 gen_set_access_type(ctx, ACCESS_FLOAT); \
a0d7d5a7 3481 EA = tcg_temp_new(); \
76db3ba4
AJ
3482 gen_addr_imm_index(ctx, EA, 0); \
3483 gen_qemu_##stop(ctx, cpu_fpr[rS(ctx->opcode)], EA); \
a0d7d5a7
AJ
3484 tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], EA); \
3485 tcg_temp_free(EA); \
79aceca5
FB
3486}
3487
a0d7d5a7 3488#define GEN_STUXF(name, stop, opc, type) \
99e300ef 3489static void glue(gen_, name##ux)(DisasContext *ctx) \
79aceca5 3490{ \
a0d7d5a7 3491 TCGv EA; \
76a66253 3492 if (unlikely(!ctx->fpu_enabled)) { \
e06fcd75 3493 gen_exception(ctx, POWERPC_EXCP_FPU); \
4ecc3190
FB
3494 return; \
3495 } \
76a66253 3496 if (unlikely(rA(ctx->opcode) == 0)) { \
e06fcd75 3497 gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL); \
9fddaa0c 3498 return; \
9a64fbe4 3499 } \
76db3ba4 3500 gen_set_access_type(ctx, ACCESS_FLOAT); \
a0d7d5a7 3501 EA = tcg_temp_new(); \
76db3ba4
AJ
3502 gen_addr_reg_index(ctx, EA); \
3503 gen_qemu_##stop(ctx, cpu_fpr[rS(ctx->opcode)], EA); \
a0d7d5a7
AJ
3504 tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], EA); \
3505 tcg_temp_free(EA); \
79aceca5
FB
3506}
3507
a0d7d5a7 3508#define GEN_STXF(name, stop, opc2, opc3, type) \
99e300ef 3509static void glue(gen_, name##x)(DisasContext *ctx) \
79aceca5 3510{ \
a0d7d5a7 3511 TCGv EA; \
76a66253 3512 if (unlikely(!ctx->fpu_enabled)) { \
e06fcd75 3513 gen_exception(ctx, POWERPC_EXCP_FPU); \
4ecc3190
FB
3514 return; \
3515 } \
76db3ba4 3516 gen_set_access_type(ctx, ACCESS_FLOAT); \
a0d7d5a7 3517 EA = tcg_temp_new(); \
76db3ba4
AJ
3518 gen_addr_reg_index(ctx, EA); \
3519 gen_qemu_##stop(ctx, cpu_fpr[rS(ctx->opcode)], EA); \
a0d7d5a7 3520 tcg_temp_free(EA); \
79aceca5
FB
3521}
3522
a0d7d5a7
AJ
3523#define GEN_STFS(name, stop, op, type) \
3524GEN_STF(name, stop, op | 0x20, type); \
3525GEN_STUF(name, stop, op | 0x21, type); \
3526GEN_STUXF(name, stop, op | 0x01, type); \
3527GEN_STXF(name, stop, 0x17, op | 0x00, type)
3528
636aa200 3529static inline void gen_qemu_st32fs(DisasContext *ctx, TCGv_i64 arg1, TCGv arg2)
a0d7d5a7
AJ
3530{
3531 TCGv_i32 t0 = tcg_temp_new_i32();
3532 TCGv t1 = tcg_temp_new();
8e703949 3533 gen_helper_float64_to_float32(t0, cpu_env, arg1);
a0d7d5a7
AJ
3534 tcg_gen_extu_i32_tl(t1, t0);
3535 tcg_temp_free_i32(t0);
76db3ba4 3536 gen_qemu_st32(ctx, t1, arg2);
a0d7d5a7
AJ
3537 tcg_temp_free(t1);
3538}
79aceca5
FB
3539
3540/* stfd stfdu stfdux stfdx */
a0d7d5a7 3541GEN_STFS(stfd, st64, 0x16, PPC_FLOAT);
79aceca5 3542/* stfs stfsu stfsux stfsx */
a0d7d5a7 3543GEN_STFS(stfs, st32fs, 0x14, PPC_FLOAT);
79aceca5 3544
44bc0c4d
AJ
3545/* stfdp */
3546static void gen_stfdp(DisasContext *ctx)
3547{
3548 TCGv EA;
3549 if (unlikely(!ctx->fpu_enabled)) {
3550 gen_exception(ctx, POWERPC_EXCP_FPU);
3551 return;
3552 }
3553 gen_set_access_type(ctx, ACCESS_FLOAT);
3554 EA = tcg_temp_new();
3555 gen_addr_imm_index(ctx, EA, 0); \
3556 if (unlikely(ctx->le_mode)) {
3557 gen_qemu_st64(ctx, cpu_fpr[rD(ctx->opcode) + 1], EA);
3558 tcg_gen_addi_tl(EA, EA, 8);
3559 gen_qemu_st64(ctx, cpu_fpr[rD(ctx->opcode)], EA);
3560 } else {
3561 gen_qemu_st64(ctx, cpu_fpr[rD(ctx->opcode)], EA);
3562 tcg_gen_addi_tl(EA, EA, 8);
3563 gen_qemu_st64(ctx, cpu_fpr[rD(ctx->opcode) + 1], EA);
3564 }
3565 tcg_temp_free(EA);
3566}
3567
3568/* stfdpx */
3569static void gen_stfdpx(DisasContext *ctx)
3570{
3571 TCGv EA;
3572 if (unlikely(!ctx->fpu_enabled)) {
3573 gen_exception(ctx, POWERPC_EXCP_FPU);
3574 return;
3575 }
3576 gen_set_access_type(ctx, ACCESS_FLOAT);
3577 EA = tcg_temp_new();
3578 gen_addr_reg_index(ctx, EA);
3579 if (unlikely(ctx->le_mode)) {
3580 gen_qemu_st64(ctx, cpu_fpr[rD(ctx->opcode) + 1], EA);
3581 tcg_gen_addi_tl(EA, EA, 8);
3582 gen_qemu_st64(ctx, cpu_fpr[rD(ctx->opcode)], EA);
3583 } else {
3584 gen_qemu_st64(ctx, cpu_fpr[rD(ctx->opcode)], EA);
3585 tcg_gen_addi_tl(EA, EA, 8);
3586 gen_qemu_st64(ctx, cpu_fpr[rD(ctx->opcode) + 1], EA);
3587 }
3588 tcg_temp_free(EA);
3589}
3590
79aceca5 3591/* Optional: */
636aa200 3592static inline void gen_qemu_st32fiw(DisasContext *ctx, TCGv_i64 arg1, TCGv arg2)
a0d7d5a7
AJ
3593{
3594 TCGv t0 = tcg_temp_new();
3595 tcg_gen_trunc_i64_tl(t0, arg1),
76db3ba4 3596 gen_qemu_st32(ctx, t0, arg2);
a0d7d5a7
AJ
3597 tcg_temp_free(t0);
3598}
79aceca5 3599/* stfiwx */
a0d7d5a7 3600GEN_STXF(stfiw, st32fiw, 0x17, 0x1E, PPC_FLOAT_STFIWX);
79aceca5 3601
697ab892
DG
3602static inline void gen_update_cfar(DisasContext *ctx, target_ulong nip)
3603{
3604#if defined(TARGET_PPC64)
3605 if (ctx->has_cfar)
3606 tcg_gen_movi_tl(cpu_cfar, nip);
3607#endif
3608}
3609
79aceca5 3610/*** Branch ***/
636aa200 3611static inline void gen_goto_tb(DisasContext *ctx, int n, target_ulong dest)
c1942362
FB
3612{
3613 TranslationBlock *tb;
3614 tb = ctx->tb;
e0c8f9ce 3615 if (NARROW_MODE(ctx)) {
a2ffb812 3616 dest = (uint32_t) dest;
e0c8f9ce 3617 }
57fec1fe 3618 if ((tb->pc & TARGET_PAGE_MASK) == (dest & TARGET_PAGE_MASK) &&
8cbcb4fa 3619 likely(!ctx->singlestep_enabled)) {
57fec1fe 3620 tcg_gen_goto_tb(n);
a2ffb812 3621 tcg_gen_movi_tl(cpu_nip, dest & ~3);
8cfd0495 3622 tcg_gen_exit_tb((uintptr_t)tb + n);
c1942362 3623 } else {
a2ffb812 3624 tcg_gen_movi_tl(cpu_nip, dest & ~3);
8cbcb4fa
AJ
3625 if (unlikely(ctx->singlestep_enabled)) {
3626 if ((ctx->singlestep_enabled &
bdc4e053 3627 (CPU_BRANCH_STEP | CPU_SINGLE_STEP)) &&
f0cc4aa8
JG
3628 (ctx->exception == POWERPC_EXCP_BRANCH ||
3629 ctx->exception == POWERPC_EXCP_TRACE)) {
8cbcb4fa
AJ
3630 target_ulong tmp = ctx->nip;
3631 ctx->nip = dest;
e06fcd75 3632 gen_exception(ctx, POWERPC_EXCP_TRACE);
8cbcb4fa
AJ
3633 ctx->nip = tmp;
3634 }
3635 if (ctx->singlestep_enabled & GDBSTUB_SINGLE_STEP) {
e06fcd75 3636 gen_debug_exception(ctx);
8cbcb4fa
AJ
3637 }
3638 }
57fec1fe 3639 tcg_gen_exit_tb(0);
c1942362 3640 }
c53be334
FB
3641}
3642
636aa200 3643static inline void gen_setlr(DisasContext *ctx, target_ulong nip)
e1833e1f 3644{
e0c8f9ce
RH
3645 if (NARROW_MODE(ctx)) {
3646 nip = (uint32_t)nip;
3647 }
3648 tcg_gen_movi_tl(cpu_lr, nip);
e1833e1f
JM
3649}
3650
79aceca5 3651/* b ba bl bla */
99e300ef 3652static void gen_b(DisasContext *ctx)
79aceca5 3653{
76a66253 3654 target_ulong li, target;
38a64f9d 3655
8cbcb4fa 3656 ctx->exception = POWERPC_EXCP_BRANCH;
38a64f9d 3657 /* sign extend LI */
e0c8f9ce
RH
3658 li = LI(ctx->opcode);
3659 li = (li ^ 0x02000000) - 0x02000000;
3660 if (likely(AA(ctx->opcode) == 0)) {
046d6672 3661 target = ctx->nip + li - 4;
e0c8f9ce 3662 } else {
9a64fbe4 3663 target = li;
e0c8f9ce
RH
3664 }
3665 if (LK(ctx->opcode)) {
e1833e1f 3666 gen_setlr(ctx, ctx->nip);
e0c8f9ce 3667 }
697ab892 3668 gen_update_cfar(ctx, ctx->nip);
c1942362 3669 gen_goto_tb(ctx, 0, target);
79aceca5
FB
3670}
3671
e98a6e40
FB
3672#define BCOND_IM 0
3673#define BCOND_LR 1
3674#define BCOND_CTR 2
3675
636aa200 3676static inline void gen_bcond(DisasContext *ctx, int type)
d9bce9d9 3677{
d9bce9d9 3678 uint32_t bo = BO(ctx->opcode);
05f92404 3679 int l1;
a2ffb812 3680 TCGv target;
e98a6e40 3681
8cbcb4fa 3682 ctx->exception = POWERPC_EXCP_BRANCH;
a2ffb812 3683 if (type == BCOND_LR || type == BCOND_CTR) {
a7812ae4 3684 target = tcg_temp_local_new();
a2ffb812
AJ
3685 if (type == BCOND_CTR)
3686 tcg_gen_mov_tl(target, cpu_ctr);
3687 else
3688 tcg_gen_mov_tl(target, cpu_lr);
d2e9fd8f 3689 } else {
3690 TCGV_UNUSED(target);
e98a6e40 3691 }
e1833e1f
JM
3692 if (LK(ctx->opcode))
3693 gen_setlr(ctx, ctx->nip);
a2ffb812
AJ
3694 l1 = gen_new_label();
3695 if ((bo & 0x4) == 0) {
3696 /* Decrement and test CTR */
a7812ae4 3697 TCGv temp = tcg_temp_new();
a2ffb812 3698 if (unlikely(type == BCOND_CTR)) {
e06fcd75 3699 gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL);
a2ffb812
AJ
3700 return;
3701 }
3702 tcg_gen_subi_tl(cpu_ctr, cpu_ctr, 1);
e0c8f9ce 3703 if (NARROW_MODE(ctx)) {
a2ffb812 3704 tcg_gen_ext32u_tl(temp, cpu_ctr);
e0c8f9ce 3705 } else {
a2ffb812 3706 tcg_gen_mov_tl(temp, cpu_ctr);
e0c8f9ce 3707 }
a2ffb812
AJ
3708 if (bo & 0x2) {
3709 tcg_gen_brcondi_tl(TCG_COND_NE, temp, 0, l1);
3710 } else {
3711 tcg_gen_brcondi_tl(TCG_COND_EQ, temp, 0, l1);
e98a6e40 3712 }
a7812ae4 3713 tcg_temp_free(temp);
a2ffb812
AJ
3714 }
3715 if ((bo & 0x10) == 0) {
3716 /* Test CR */
3717 uint32_t bi = BI(ctx->opcode);
3718 uint32_t mask = 1 << (3 - (bi & 0x03));
a7812ae4 3719 TCGv_i32 temp = tcg_temp_new_i32();
a2ffb812 3720
d9bce9d9 3721 if (bo & 0x8) {
a2ffb812
AJ
3722 tcg_gen_andi_i32(temp, cpu_crf[bi >> 2], mask);
3723 tcg_gen_brcondi_i32(TCG_COND_EQ, temp, 0, l1);
d9bce9d9 3724 } else {
a2ffb812
AJ
3725 tcg_gen_andi_i32(temp, cpu_crf[bi >> 2], mask);
3726 tcg_gen_brcondi_i32(TCG_COND_NE, temp, 0, l1);
d9bce9d9 3727 }
a7812ae4 3728 tcg_temp_free_i32(temp);
d9bce9d9 3729 }
697ab892 3730 gen_update_cfar(ctx, ctx->nip);
e98a6e40 3731 if (type == BCOND_IM) {
a2ffb812
AJ
3732 target_ulong li = (target_long)((int16_t)(BD(ctx->opcode)));
3733 if (likely(AA(ctx->opcode) == 0)) {
3734 gen_goto_tb(ctx, 0, ctx->nip + li - 4);
3735 } else {
3736 gen_goto_tb(ctx, 0, li);
3737 }
c53be334 3738 gen_set_label(l1);
c1942362 3739 gen_goto_tb(ctx, 1, ctx->nip);
e98a6e40 3740 } else {
e0c8f9ce 3741 if (NARROW_MODE(ctx)) {
a2ffb812 3742 tcg_gen_andi_tl(cpu_nip, target, (uint32_t)~3);
e0c8f9ce 3743 } else {
a2ffb812 3744 tcg_gen_andi_tl(cpu_nip, target, ~3);
e0c8f9ce 3745 }
a2ffb812
AJ
3746 tcg_gen_exit_tb(0);
3747 gen_set_label(l1);
e0c8f9ce 3748 gen_update_nip(ctx, ctx->nip);
57fec1fe 3749 tcg_gen_exit_tb(0);
08e46e54 3750 }
e98a6e40
FB
3751}
3752
99e300ef 3753static void gen_bc(DisasContext *ctx)
3b46e624 3754{
e98a6e40
FB
3755 gen_bcond(ctx, BCOND_IM);
3756}
3757
99e300ef 3758static void gen_bcctr(DisasContext *ctx)
3b46e624 3759{
e98a6e40
FB
3760 gen_bcond(ctx, BCOND_CTR);
3761}
3762
99e300ef 3763static void gen_bclr(DisasContext *ctx)
3b46e624 3764{
e98a6e40
FB
3765 gen_bcond(ctx, BCOND_LR);
3766}
79aceca5
FB
3767
3768/*** Condition register logical ***/
e1571908 3769#define GEN_CRLOGIC(name, tcg_op, opc) \
99e300ef 3770static void glue(gen_, name)(DisasContext *ctx) \
79aceca5 3771{ \
fc0d441e
JM
3772 uint8_t bitmask; \
3773 int sh; \
a7812ae4 3774 TCGv_i32 t0, t1; \
fc0d441e 3775 sh = (crbD(ctx->opcode) & 0x03) - (crbA(ctx->opcode) & 0x03); \
a7812ae4 3776 t0 = tcg_temp_new_i32(); \
fc0d441e 3777 if (sh > 0) \
fea0c503 3778 tcg_gen_shri_i32(t0, cpu_crf[crbA(ctx->opcode) >> 2], sh); \
fc0d441e 3779 else if (sh < 0) \
fea0c503 3780 tcg_gen_shli_i32(t0, cpu_crf[crbA(ctx->opcode) >> 2], -sh); \
e1571908 3781 else \
fea0c503 3782 tcg_gen_mov_i32(t0, cpu_crf[crbA(ctx->opcode) >> 2]); \
a7812ae4 3783 t1 = tcg_temp_new_i32(); \
fc0d441e
JM
3784 sh = (crbD(ctx->opcode) & 0x03) - (crbB(ctx->opcode) & 0x03); \
3785 if (sh > 0) \
fea0c503 3786 tcg_gen_shri_i32(t1, cpu_crf[crbB(ctx->opcode) >> 2], sh); \
fc0d441e 3787 else if (sh < 0) \
fea0c503 3788 tcg_gen_shli_i32(t1, cpu_crf[crbB(ctx->opcode) >> 2], -sh); \
e1571908 3789 else \
fea0c503
AJ
3790 tcg_gen_mov_i32(t1, cpu_crf[crbB(ctx->opcode) >> 2]); \
3791 tcg_op(t0, t0, t1); \
fc0d441e 3792 bitmask = 1 << (3 - (crbD(ctx->opcode) & 0x03)); \
fea0c503
AJ
3793 tcg_gen_andi_i32(t0, t0, bitmask); \
3794 tcg_gen_andi_i32(t1, cpu_crf[crbD(ctx->opcode) >> 2], ~bitmask); \
3795 tcg_gen_or_i32(cpu_crf[crbD(ctx->opcode) >> 2], t0, t1); \
a7812ae4
PB
3796 tcg_temp_free_i32(t0); \
3797 tcg_temp_free_i32(t1); \
79aceca5
FB
3798}
3799
3800/* crand */
e1571908 3801GEN_CRLOGIC(crand, tcg_gen_and_i32, 0x08);
79aceca5 3802/* crandc */
e1571908 3803GEN_CRLOGIC(crandc, tcg_gen_andc_i32, 0x04);
79aceca5 3804/* creqv */
e1571908 3805GEN_CRLOGIC(creqv, tcg_gen_eqv_i32, 0x09);
79aceca5 3806/* crnand */
e1571908 3807GEN_CRLOGIC(crnand, tcg_gen_nand_i32, 0x07);
79aceca5 3808/* crnor */
e1571908 3809GEN_CRLOGIC(crnor, tcg_gen_nor_i32, 0x01);
79aceca5 3810/* cror */
e1571908 3811GEN_CRLOGIC(cror, tcg_gen_or_i32, 0x0E);
79aceca5 3812/* crorc */
e1571908 3813GEN_CRLOGIC(crorc, tcg_gen_orc_i32, 0x0D);
79aceca5 3814/* crxor */
e1571908 3815GEN_CRLOGIC(crxor, tcg_gen_xor_i32, 0x06);
99e300ef 3816
54623277 3817/* mcrf */
99e300ef 3818static void gen_mcrf(DisasContext *ctx)
79aceca5 3819{
47e4661c 3820 tcg_gen_mov_i32(cpu_crf[crfD(ctx->opcode)], cpu_crf[crfS(ctx->opcode)]);
79aceca5
FB
3821}
3822
3823/*** System linkage ***/
99e300ef 3824
54623277 3825/* rfi (mem_idx only) */
99e300ef 3826static void gen_rfi(DisasContext *ctx)
79aceca5 3827{
9a64fbe4 3828#if defined(CONFIG_USER_ONLY)
e06fcd75 3829 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
9a64fbe4
FB
3830#else
3831 /* Restore CPU state */
76db3ba4 3832 if (unlikely(!ctx->mem_idx)) {
e06fcd75 3833 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
9fddaa0c 3834 return;
9a64fbe4 3835 }
697ab892 3836 gen_update_cfar(ctx, ctx->nip);
e5f17ac6 3837 gen_helper_rfi(cpu_env);
e06fcd75 3838 gen_sync_exception(ctx);
9a64fbe4 3839#endif
79aceca5
FB
3840}
3841
426613db 3842#if defined(TARGET_PPC64)
99e300ef 3843static void gen_rfid(DisasContext *ctx)
426613db
JM
3844{
3845#if defined(CONFIG_USER_ONLY)
e06fcd75 3846 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
426613db
JM
3847#else
3848 /* Restore CPU state */
76db3ba4 3849 if (unlikely(!ctx->mem_idx)) {
e06fcd75 3850 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
426613db
JM
3851 return;
3852 }
697ab892 3853 gen_update_cfar(ctx, ctx->nip);
e5f17ac6 3854 gen_helper_rfid(cpu_env);
e06fcd75 3855 gen_sync_exception(ctx);
426613db
JM
3856#endif
3857}
426613db 3858
99e300ef 3859static void gen_hrfid(DisasContext *ctx)
be147d08
JM
3860{
3861#if defined(CONFIG_USER_ONLY)
e06fcd75 3862 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
be147d08
JM
3863#else
3864 /* Restore CPU state */
76db3ba4 3865 if (unlikely(ctx->mem_idx <= 1)) {
e06fcd75 3866 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
be147d08
JM
3867 return;
3868 }
e5f17ac6 3869 gen_helper_hrfid(cpu_env);
e06fcd75 3870 gen_sync_exception(ctx);
be147d08
JM
3871#endif
3872}
3873#endif
3874
79aceca5 3875/* sc */
417bf010
JM
3876#if defined(CONFIG_USER_ONLY)
3877#define POWERPC_SYSCALL POWERPC_EXCP_SYSCALL_USER
3878#else
3879#define POWERPC_SYSCALL POWERPC_EXCP_SYSCALL
3880#endif
99e300ef 3881static void gen_sc(DisasContext *ctx)
79aceca5 3882{
e1833e1f
JM
3883 uint32_t lev;
3884
3885 lev = (ctx->opcode >> 5) & 0x7F;
e06fcd75 3886 gen_exception_err(ctx, POWERPC_SYSCALL, lev);
79aceca5
FB
3887}
3888
3889/*** Trap ***/
99e300ef 3890
54623277 3891/* tw */
99e300ef 3892static void gen_tw(DisasContext *ctx)
79aceca5 3893{
cab3bee2 3894 TCGv_i32 t0 = tcg_const_i32(TO(ctx->opcode));
db9a231d
AJ
3895 /* Update the nip since this might generate a trap exception */
3896 gen_update_nip(ctx, ctx->nip);
e5f17ac6
BS
3897 gen_helper_tw(cpu_env, cpu_gpr[rA(ctx->opcode)], cpu_gpr[rB(ctx->opcode)],
3898 t0);
cab3bee2 3899 tcg_temp_free_i32(t0);
79aceca5
FB
3900}
3901
3902/* twi */
99e300ef 3903static void gen_twi(DisasContext *ctx)
79aceca5 3904{
cab3bee2
AJ
3905 TCGv t0 = tcg_const_tl(SIMM(ctx->opcode));
3906 TCGv_i32 t1 = tcg_const_i32(TO(ctx->opcode));
db9a231d
AJ
3907 /* Update the nip since this might generate a trap exception */
3908 gen_update_nip(ctx, ctx->nip);
e5f17ac6 3909 gen_helper_tw(cpu_env, cpu_gpr[rA(ctx->opcode)], t0, t1);
cab3bee2
AJ
3910 tcg_temp_free(t0);
3911 tcg_temp_free_i32(t1);
79aceca5
FB
3912}
3913
d9bce9d9
JM
3914#if defined(TARGET_PPC64)
3915/* td */
99e300ef 3916static void gen_td(DisasContext *ctx)
d9bce9d9 3917{
cab3bee2 3918 TCGv_i32 t0 = tcg_const_i32(TO(ctx->opcode));
db9a231d
AJ
3919 /* Update the nip since this might generate a trap exception */
3920 gen_update_nip(ctx, ctx->nip);
e5f17ac6
BS
3921 gen_helper_td(cpu_env, cpu_gpr[rA(ctx->opcode)], cpu_gpr[rB(ctx->opcode)],
3922 t0);
cab3bee2 3923 tcg_temp_free_i32(t0);
d9bce9d9
JM
3924}
3925
3926/* tdi */
99e300ef 3927static void gen_tdi(DisasContext *ctx)
d9bce9d9 3928{
cab3bee2
AJ
3929 TCGv t0 = tcg_const_tl(SIMM(ctx->opcode));
3930 TCGv_i32 t1 = tcg_const_i32(TO(ctx->opcode));
db9a231d
AJ
3931 /* Update the nip since this might generate a trap exception */
3932 gen_update_nip(ctx, ctx->nip);
e5f17ac6 3933 gen_helper_td(cpu_env, cpu_gpr[rA(ctx->opcode)], t0, t1);
cab3bee2
AJ
3934 tcg_temp_free(t0);
3935 tcg_temp_free_i32(t1);
d9bce9d9
JM
3936}
3937#endif
3938
79aceca5 3939/*** Processor control ***/
99e300ef 3940
da91a00f
RH
3941static void gen_read_xer(TCGv dst)
3942{
3943 TCGv t0 = tcg_temp_new();
3944 TCGv t1 = tcg_temp_new();
3945 TCGv t2 = tcg_temp_new();
3946 tcg_gen_mov_tl(dst, cpu_xer);
3947 tcg_gen_shli_tl(t0, cpu_so, XER_SO);
3948 tcg_gen_shli_tl(t1, cpu_ov, XER_OV);
3949 tcg_gen_shli_tl(t2, cpu_ca, XER_CA);
3950 tcg_gen_or_tl(t0, t0, t1);
3951 tcg_gen_or_tl(dst, dst, t2);
3952 tcg_gen_or_tl(dst, dst, t0);
3953 tcg_temp_free(t0);
3954 tcg_temp_free(t1);
3955 tcg_temp_free(t2);
3956}
3957
3958static void gen_write_xer(TCGv src)
3959{
3960 tcg_gen_andi_tl(cpu_xer, src,
3961 ~((1u << XER_SO) | (1u << XER_OV) | (1u << XER_CA)));
3962 tcg_gen_shri_tl(cpu_so, src, XER_SO);
3963 tcg_gen_shri_tl(cpu_ov, src, XER_OV);
3964 tcg_gen_shri_tl(cpu_ca, src, XER_CA);
3965 tcg_gen_andi_tl(cpu_so, cpu_so, 1);
3966 tcg_gen_andi_tl(cpu_ov, cpu_ov, 1);
3967 tcg_gen_andi_tl(cpu_ca, cpu_ca, 1);
3968}
3969
54623277 3970/* mcrxr */
99e300ef 3971static void gen_mcrxr(DisasContext *ctx)
79aceca5 3972{
da91a00f
RH
3973 TCGv_i32 t0 = tcg_temp_new_i32();
3974 TCGv_i32 t1 = tcg_temp_new_i32();
3975 TCGv_i32 dst = cpu_crf[crfD(ctx->opcode)];
3976
3977 tcg_gen_trunc_tl_i32(t0, cpu_so);
3978 tcg_gen_trunc_tl_i32(t1, cpu_ov);
3979 tcg_gen_trunc_tl_i32(dst, cpu_ca);
3980 tcg_gen_shri_i32(t0, t0, 2);
3981 tcg_gen_shri_i32(t1, t1, 1);
3982 tcg_gen_or_i32(dst, dst, t0);
3983 tcg_gen_or_i32(dst, dst, t1);
3984 tcg_temp_free_i32(t0);
3985 tcg_temp_free_i32(t1);
3986
3987 tcg_gen_movi_tl(cpu_so, 0);
3988 tcg_gen_movi_tl(cpu_ov, 0);
3989 tcg_gen_movi_tl(cpu_ca, 0);
79aceca5
FB
3990}
3991
0cfe11ea 3992/* mfcr mfocrf */
99e300ef 3993static void gen_mfcr(DisasContext *ctx)
79aceca5 3994{
76a66253 3995 uint32_t crm, crn;
3b46e624 3996
76a66253
JM
3997 if (likely(ctx->opcode & 0x00100000)) {
3998 crm = CRM(ctx->opcode);
8dd640e4 3999 if (likely(crm && ((crm & (crm - 1)) == 0))) {
0cfe11ea 4000 crn = ctz32 (crm);
e1571908 4001 tcg_gen_extu_i32_tl(cpu_gpr[rD(ctx->opcode)], cpu_crf[7 - crn]);
0497d2f4
AJ
4002 tcg_gen_shli_tl(cpu_gpr[rD(ctx->opcode)],
4003 cpu_gpr[rD(ctx->opcode)], crn * 4);
76a66253 4004 }
d9bce9d9 4005 } else {
651721b2
AJ
4006 TCGv_i32 t0 = tcg_temp_new_i32();
4007 tcg_gen_mov_i32(t0, cpu_crf[0]);
4008 tcg_gen_shli_i32(t0, t0, 4);
4009 tcg_gen_or_i32(t0, t0, cpu_crf[1]);
4010 tcg_gen_shli_i32(t0, t0, 4);
4011 tcg_gen_or_i32(t0, t0, cpu_crf[2]);
4012 tcg_gen_shli_i32(t0, t0, 4);
4013 tcg_gen_or_i32(t0, t0, cpu_crf[3]);
4014 tcg_gen_shli_i32(t0, t0, 4);
4015 tcg_gen_or_i32(t0, t0, cpu_crf[4]);
4016 tcg_gen_shli_i32(t0, t0, 4);
4017 tcg_gen_or_i32(t0, t0, cpu_crf[5]);
4018 tcg_gen_shli_i32(t0, t0, 4);
4019 tcg_gen_or_i32(t0, t0, cpu_crf[6]);
4020 tcg_gen_shli_i32(t0, t0, 4);
4021 tcg_gen_or_i32(t0, t0, cpu_crf[7]);
4022 tcg_gen_extu_i32_tl(cpu_gpr[rD(ctx->opcode)], t0);
4023 tcg_temp_free_i32(t0);
d9bce9d9 4024 }
79aceca5
FB
4025}
4026
4027/* mfmsr */
99e300ef 4028static void gen_mfmsr(DisasContext *ctx)
79aceca5 4029{
9a64fbe4 4030#if defined(CONFIG_USER_ONLY)
e06fcd75 4031 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
9a64fbe4 4032#else
76db3ba4 4033 if (unlikely(!ctx->mem_idx)) {
e06fcd75 4034 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
9fddaa0c 4035 return;
9a64fbe4 4036 }
6527f6ea 4037 tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_msr);
9a64fbe4 4038#endif
79aceca5
FB
4039}
4040
7b13448f 4041static void spr_noaccess(void *opaque, int gprn, int sprn)
3fc6c082 4042{
7b13448f 4043#if 0
3fc6c082
FB
4044 sprn = ((sprn >> 5) & 0x1F) | ((sprn & 0x1F) << 5);
4045 printf("ERROR: try to access SPR %d !\n", sprn);
7b13448f 4046#endif
3fc6c082
FB
4047}
4048#define SPR_NOACCESS (&spr_noaccess)
3fc6c082 4049
79aceca5 4050/* mfspr */
636aa200 4051static inline void gen_op_mfspr(DisasContext *ctx)
79aceca5 4052{
45d827d2 4053 void (*read_cb)(void *opaque, int gprn, int sprn);
79aceca5
FB
4054 uint32_t sprn = SPR(ctx->opcode);
4055
3fc6c082 4056#if !defined(CONFIG_USER_ONLY)
76db3ba4 4057 if (ctx->mem_idx == 2)
be147d08 4058 read_cb = ctx->spr_cb[sprn].hea_read;
76db3ba4 4059 else if (ctx->mem_idx)
3fc6c082
FB
4060 read_cb = ctx->spr_cb[sprn].oea_read;
4061 else
9a64fbe4 4062#endif
3fc6c082 4063 read_cb = ctx->spr_cb[sprn].uea_read;
76a66253
JM
4064 if (likely(read_cb != NULL)) {
4065 if (likely(read_cb != SPR_NOACCESS)) {
45d827d2 4066 (*read_cb)(ctx, rD(ctx->opcode), sprn);
3fc6c082
FB
4067 } else {
4068 /* Privilege exception */
9fceefa7
JM
4069 /* This is a hack to avoid warnings when running Linux:
4070 * this OS breaks the PowerPC virtualisation model,
4071 * allowing userland application to read the PVR
4072 */
4073 if (sprn != SPR_PVR) {
c05541ee
AB
4074 qemu_log("Trying to read privileged spr %d (0x%03x) at "
4075 TARGET_FMT_lx "\n", sprn, sprn, ctx->nip - 4);
4076 printf("Trying to read privileged spr %d (0x%03x) at "
4077 TARGET_FMT_lx "\n", sprn, sprn, ctx->nip - 4);
f24e5695 4078 }
e06fcd75 4079 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
79aceca5 4080 }
3fc6c082
FB
4081 } else {
4082 /* Not defined */
c05541ee
AB
4083 qemu_log("Trying to read invalid spr %d (0x%03x) at "
4084 TARGET_FMT_lx "\n", sprn, sprn, ctx->nip - 4);
4085 printf("Trying to read invalid spr %d (0x%03x) at "
4086 TARGET_FMT_lx "\n", sprn, sprn, ctx->nip - 4);
e06fcd75 4087 gen_inval_exception(ctx, POWERPC_EXCP_INVAL_SPR);
79aceca5 4088 }
79aceca5
FB
4089}
4090
99e300ef 4091static void gen_mfspr(DisasContext *ctx)
79aceca5 4092{
3fc6c082 4093 gen_op_mfspr(ctx);
76a66253 4094}
3fc6c082
FB
4095
4096/* mftb */
99e300ef 4097static void gen_mftb(DisasContext *ctx)
3fc6c082
FB
4098{
4099 gen_op_mfspr(ctx);
79aceca5
FB
4100}
4101
0cfe11ea 4102/* mtcrf mtocrf*/
99e300ef 4103static void gen_mtcrf(DisasContext *ctx)
79aceca5 4104{
76a66253 4105 uint32_t crm, crn;
3b46e624 4106
76a66253 4107 crm = CRM(ctx->opcode);
8dd640e4 4108 if (likely((ctx->opcode & 0x00100000))) {
4109 if (crm && ((crm & (crm - 1)) == 0)) {
4110 TCGv_i32 temp = tcg_temp_new_i32();
0cfe11ea 4111 crn = ctz32 (crm);
8dd640e4 4112 tcg_gen_trunc_tl_i32(temp, cpu_gpr[rS(ctx->opcode)]);
0cfe11ea
AJ
4113 tcg_gen_shri_i32(temp, temp, crn * 4);
4114 tcg_gen_andi_i32(cpu_crf[7 - crn], temp, 0xf);
8dd640e4 4115 tcg_temp_free_i32(temp);
4116 }
76a66253 4117 } else {
651721b2
AJ
4118 TCGv_i32 temp = tcg_temp_new_i32();
4119 tcg_gen_trunc_tl_i32(temp, cpu_gpr[rS(ctx->opcode)]);
4120 for (crn = 0 ; crn < 8 ; crn++) {
4121 if (crm & (1 << crn)) {
4122 tcg_gen_shri_i32(cpu_crf[7 - crn], temp, crn * 4);
4123 tcg_gen_andi_i32(cpu_crf[7 - crn], cpu_crf[7 - crn], 0xf);
4124 }
4125 }
a7812ae4 4126 tcg_temp_free_i32(temp);
76a66253 4127 }
79aceca5
FB
4128}
4129
4130/* mtmsr */
426613db 4131#if defined(TARGET_PPC64)
99e300ef 4132static void gen_mtmsrd(DisasContext *ctx)
426613db
JM
4133{
4134#if defined(CONFIG_USER_ONLY)
e06fcd75 4135 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
426613db 4136#else
76db3ba4 4137 if (unlikely(!ctx->mem_idx)) {
e06fcd75 4138 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
426613db
JM
4139 return;
4140 }
be147d08
JM
4141 if (ctx->opcode & 0x00010000) {
4142 /* Special form that does not need any synchronisation */
6527f6ea
AJ
4143 TCGv t0 = tcg_temp_new();
4144 tcg_gen_andi_tl(t0, cpu_gpr[rS(ctx->opcode)], (1 << MSR_RI) | (1 << MSR_EE));
4145 tcg_gen_andi_tl(cpu_msr, cpu_msr, ~((1 << MSR_RI) | (1 << MSR_EE)));
4146 tcg_gen_or_tl(cpu_msr, cpu_msr, t0);
4147 tcg_temp_free(t0);
be147d08 4148 } else {
056b05f8
JM
4149 /* XXX: we need to update nip before the store
4150 * if we enter power saving mode, we will exit the loop
4151 * directly from ppc_store_msr
4152 */
be147d08 4153 gen_update_nip(ctx, ctx->nip);
e5f17ac6 4154 gen_helper_store_msr(cpu_env, cpu_gpr[rS(ctx->opcode)]);
be147d08
JM
4155 /* Must stop the translation as machine state (may have) changed */
4156 /* Note that mtmsr is not always defined as context-synchronizing */
e06fcd75 4157 gen_stop_exception(ctx);
be147d08 4158 }
426613db
JM
4159#endif
4160}
4161#endif
4162
99e300ef 4163static void gen_mtmsr(DisasContext *ctx)
79aceca5 4164{
9a64fbe4 4165#if defined(CONFIG_USER_ONLY)
e06fcd75 4166 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
9a64fbe4 4167#else
76db3ba4 4168 if (unlikely(!ctx->mem_idx)) {
e06fcd75 4169 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
9fddaa0c 4170 return;
9a64fbe4 4171 }
be147d08
JM
4172 if (ctx->opcode & 0x00010000) {
4173 /* Special form that does not need any synchronisation */
6527f6ea
AJ
4174 TCGv t0 = tcg_temp_new();
4175 tcg_gen_andi_tl(t0, cpu_gpr[rS(ctx->opcode)], (1 << MSR_RI) | (1 << MSR_EE));
4176 tcg_gen_andi_tl(cpu_msr, cpu_msr, ~((1 << MSR_RI) | (1 << MSR_EE)));
4177 tcg_gen_or_tl(cpu_msr, cpu_msr, t0);
4178 tcg_temp_free(t0);
be147d08 4179 } else {
8018dc63
AG
4180 TCGv msr = tcg_temp_new();
4181
056b05f8
JM
4182 /* XXX: we need to update nip before the store
4183 * if we enter power saving mode, we will exit the loop
4184 * directly from ppc_store_msr
4185 */
be147d08 4186 gen_update_nip(ctx, ctx->nip);
d9bce9d9 4187#if defined(TARGET_PPC64)
8018dc63
AG
4188 tcg_gen_deposit_tl(msr, cpu_msr, cpu_gpr[rS(ctx->opcode)], 0, 32);
4189#else
4190 tcg_gen_mov_tl(msr, cpu_gpr[rS(ctx->opcode)]);
d9bce9d9 4191#endif
e5f17ac6 4192 gen_helper_store_msr(cpu_env, msr);
be147d08 4193 /* Must stop the translation as machine state (may have) changed */
6527f6ea 4194 /* Note that mtmsr is not always defined as context-synchronizing */
e06fcd75 4195 gen_stop_exception(ctx);
be147d08 4196 }
9a64fbe4 4197#endif
79aceca5
FB
4198}
4199
4200/* mtspr */
99e300ef 4201static void gen_mtspr(DisasContext *ctx)
79aceca5 4202{
45d827d2 4203 void (*write_cb)(void *opaque, int sprn, int gprn);
79aceca5
FB
4204 uint32_t sprn = SPR(ctx->opcode);
4205
3fc6c082 4206#if !defined(CONFIG_USER_ONLY)
76db3ba4 4207 if (ctx->mem_idx == 2)
be147d08 4208 write_cb = ctx->spr_cb[sprn].hea_write;
76db3ba4 4209 else if (ctx->mem_idx)
3fc6c082
FB
4210 write_cb = ctx->spr_cb[sprn].oea_write;
4211 else
9a64fbe4 4212#endif
3fc6c082 4213 write_cb = ctx->spr_cb[sprn].uea_write;
76a66253
JM
4214 if (likely(write_cb != NULL)) {
4215 if (likely(write_cb != SPR_NOACCESS)) {
45d827d2 4216 (*write_cb)(ctx, sprn, rS(ctx->opcode));
3fc6c082
FB
4217 } else {
4218 /* Privilege exception */
c05541ee
AB
4219 qemu_log("Trying to write privileged spr %d (0x%03x) at "
4220 TARGET_FMT_lx "\n", sprn, sprn, ctx->nip - 4);
4221 printf("Trying to write privileged spr %d (0x%03x) at "
4222 TARGET_FMT_lx "\n", sprn, sprn, ctx->nip - 4);
e06fcd75 4223 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
76a66253 4224 }
3fc6c082
FB
4225 } else {
4226 /* Not defined */
c05541ee
AB
4227 qemu_log("Trying to write invalid spr %d (0x%03x) at "
4228 TARGET_FMT_lx "\n", sprn, sprn, ctx->nip - 4);
4229 printf("Trying to write invalid spr %d (0x%03x) at "
4230 TARGET_FMT_lx "\n", sprn, sprn, ctx->nip - 4);
e06fcd75 4231 gen_inval_exception(ctx, POWERPC_EXCP_INVAL_SPR);
79aceca5 4232 }
79aceca5
FB
4233}
4234
4235/*** Cache management ***/
99e300ef 4236
54623277 4237/* dcbf */
99e300ef 4238static void gen_dcbf(DisasContext *ctx)
79aceca5 4239{
dac454af 4240 /* XXX: specification says this is treated as a load by the MMU */
76db3ba4
AJ
4241 TCGv t0;
4242 gen_set_access_type(ctx, ACCESS_CACHE);
4243 t0 = tcg_temp_new();
4244 gen_addr_reg_index(ctx, t0);
4245 gen_qemu_ld8u(ctx, t0, t0);
fea0c503 4246 tcg_temp_free(t0);
79aceca5
FB
4247}
4248
4249/* dcbi (Supervisor only) */
99e300ef 4250static void gen_dcbi(DisasContext *ctx)
79aceca5 4251{
a541f297 4252#if defined(CONFIG_USER_ONLY)
e06fcd75 4253 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
a541f297 4254#else
b61f2753 4255 TCGv EA, val;
76db3ba4 4256 if (unlikely(!ctx->mem_idx)) {
e06fcd75 4257 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
9fddaa0c 4258 return;
9a64fbe4 4259 }
a7812ae4 4260 EA = tcg_temp_new();
76db3ba4
AJ
4261 gen_set_access_type(ctx, ACCESS_CACHE);
4262 gen_addr_reg_index(ctx, EA);
a7812ae4 4263 val = tcg_temp_new();
76a66253 4264 /* XXX: specification says this should be treated as a store by the MMU */
76db3ba4
AJ
4265 gen_qemu_ld8u(ctx, val, EA);
4266 gen_qemu_st8(ctx, val, EA);
b61f2753
AJ
4267 tcg_temp_free(val);
4268 tcg_temp_free(EA);
a541f297 4269#endif
79aceca5
FB
4270}
4271
4272/* dcdst */
99e300ef 4273static void gen_dcbst(DisasContext *ctx)
79aceca5 4274{
76a66253 4275 /* XXX: specification say this is treated as a load by the MMU */
76db3ba4
AJ
4276 TCGv t0;
4277 gen_set_access_type(ctx, ACCESS_CACHE);
4278 t0 = tcg_temp_new();
4279 gen_addr_reg_index(ctx, t0);
4280 gen_qemu_ld8u(ctx, t0, t0);
fea0c503 4281 tcg_temp_free(t0);
79aceca5
FB
4282}
4283
4284/* dcbt */
99e300ef 4285static void gen_dcbt(DisasContext *ctx)
79aceca5 4286{
0db1b20e 4287 /* interpreted as no-op */
76a66253
JM
4288 /* XXX: specification say this is treated as a load by the MMU
4289 * but does not generate any exception
4290 */
79aceca5
FB
4291}
4292
4293/* dcbtst */
99e300ef 4294static void gen_dcbtst(DisasContext *ctx)
79aceca5 4295{
0db1b20e 4296 /* interpreted as no-op */
76a66253
JM
4297 /* XXX: specification say this is treated as a load by the MMU
4298 * but does not generate any exception
4299 */
79aceca5
FB
4300}
4301
4302/* dcbz */
99e300ef 4303static void gen_dcbz(DisasContext *ctx)
79aceca5 4304{
8e33944f
AG
4305 TCGv tcgv_addr;
4306 TCGv_i32 tcgv_is_dcbzl;
4307 int is_dcbzl = ctx->opcode & 0x00200000 ? 1 : 0;
d63001d1 4308
76db3ba4 4309 gen_set_access_type(ctx, ACCESS_CACHE);
799a8c8d
AJ
4310 /* NIP cannot be restored if the memory exception comes from an helper */
4311 gen_update_nip(ctx, ctx->nip - 4);
8e33944f
AG
4312 tcgv_addr = tcg_temp_new();
4313 tcgv_is_dcbzl = tcg_const_i32(is_dcbzl);
4314
4315 gen_addr_reg_index(ctx, tcgv_addr);
4316 gen_helper_dcbz(cpu_env, tcgv_addr, tcgv_is_dcbzl);
4317
4318 tcg_temp_free(tcgv_addr);
4319 tcg_temp_free_i32(tcgv_is_dcbzl);
79aceca5
FB
4320}
4321
ae1c1a3d 4322/* dst / dstt */
99e300ef 4323static void gen_dst(DisasContext *ctx)
ae1c1a3d
AJ
4324{
4325 if (rA(ctx->opcode) == 0) {
4326 gen_inval_exception(ctx, POWERPC_EXCP_INVAL_LSWX);
4327 } else {
4328 /* interpreted as no-op */
4329 }
4330}
4331
4332/* dstst /dststt */
99e300ef 4333static void gen_dstst(DisasContext *ctx)
ae1c1a3d
AJ
4334{
4335 if (rA(ctx->opcode) == 0) {
4336 gen_inval_exception(ctx, POWERPC_EXCP_INVAL_LSWX);
4337 } else {
4338 /* interpreted as no-op */
4339 }
4340
4341}
4342
4343/* dss / dssall */
99e300ef 4344static void gen_dss(DisasContext *ctx)
ae1c1a3d
AJ
4345{
4346 /* interpreted as no-op */
4347}
4348
79aceca5 4349/* icbi */
99e300ef 4350static void gen_icbi(DisasContext *ctx)
79aceca5 4351{
76db3ba4
AJ
4352 TCGv t0;
4353 gen_set_access_type(ctx, ACCESS_CACHE);
30032c94
JM
4354 /* NIP cannot be restored if the memory exception comes from an helper */
4355 gen_update_nip(ctx, ctx->nip - 4);
76db3ba4
AJ
4356 t0 = tcg_temp_new();
4357 gen_addr_reg_index(ctx, t0);
2f5a189c 4358 gen_helper_icbi(cpu_env, t0);
37d269df 4359 tcg_temp_free(t0);
79aceca5
FB
4360}
4361
4362/* Optional: */
4363/* dcba */
99e300ef 4364static void gen_dcba(DisasContext *ctx)
79aceca5 4365{
0db1b20e
JM
4366 /* interpreted as no-op */
4367 /* XXX: specification say this is treated as a store by the MMU
4368 * but does not generate any exception
4369 */
79aceca5
FB
4370}
4371
4372/*** Segment register manipulation ***/
4373/* Supervisor only: */
99e300ef 4374
54623277 4375/* mfsr */
99e300ef 4376static void gen_mfsr(DisasContext *ctx)
79aceca5 4377{
9a64fbe4 4378#if defined(CONFIG_USER_ONLY)
e06fcd75 4379 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
9a64fbe4 4380#else
74d37793 4381 TCGv t0;
76db3ba4 4382 if (unlikely(!ctx->mem_idx)) {
e06fcd75 4383 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
9fddaa0c 4384 return;
9a64fbe4 4385 }
74d37793 4386 t0 = tcg_const_tl(SR(ctx->opcode));
c6c7cf05 4387 gen_helper_load_sr(cpu_gpr[rD(ctx->opcode)], cpu_env, t0);
74d37793 4388 tcg_temp_free(t0);
9a64fbe4 4389#endif
79aceca5
FB
4390}
4391
4392/* mfsrin */
99e300ef 4393static void gen_mfsrin(DisasContext *ctx)
79aceca5 4394{
9a64fbe4 4395#if defined(CONFIG_USER_ONLY)
e06fcd75 4396 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
9a64fbe4 4397#else
74d37793 4398 TCGv t0;
76db3ba4 4399 if (unlikely(!ctx->mem_idx)) {
e06fcd75 4400 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
9fddaa0c 4401 return;
9a64fbe4 4402 }
74d37793
AJ
4403 t0 = tcg_temp_new();
4404 tcg_gen_shri_tl(t0, cpu_gpr[rB(ctx->opcode)], 28);
4405 tcg_gen_andi_tl(t0, t0, 0xF);
c6c7cf05 4406 gen_helper_load_sr(cpu_gpr[rD(ctx->opcode)], cpu_env, t0);
74d37793 4407 tcg_temp_free(t0);
9a64fbe4 4408#endif
79aceca5
FB
4409}
4410
4411/* mtsr */
99e300ef 4412static void gen_mtsr(DisasContext *ctx)
79aceca5 4413{
9a64fbe4 4414#if defined(CONFIG_USER_ONLY)
e06fcd75 4415 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
9a64fbe4 4416#else
74d37793 4417 TCGv t0;
76db3ba4 4418 if (unlikely(!ctx->mem_idx)) {
e06fcd75 4419 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
9fddaa0c 4420 return;
9a64fbe4 4421 }
74d37793 4422 t0 = tcg_const_tl(SR(ctx->opcode));
c6c7cf05 4423 gen_helper_store_sr(cpu_env, t0, cpu_gpr[rS(ctx->opcode)]);
74d37793 4424 tcg_temp_free(t0);
9a64fbe4 4425#endif
79aceca5
FB
4426}
4427
4428/* mtsrin */
99e300ef 4429static void gen_mtsrin(DisasContext *ctx)
79aceca5 4430{
9a64fbe4 4431#if defined(CONFIG_USER_ONLY)
e06fcd75 4432 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
9a64fbe4 4433#else
74d37793 4434 TCGv t0;
76db3ba4 4435 if (unlikely(!ctx->mem_idx)) {
e06fcd75 4436 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
9fddaa0c 4437 return;
9a64fbe4 4438 }
74d37793
AJ
4439 t0 = tcg_temp_new();
4440 tcg_gen_shri_tl(t0, cpu_gpr[rB(ctx->opcode)], 28);
4441 tcg_gen_andi_tl(t0, t0, 0xF);
c6c7cf05 4442 gen_helper_store_sr(cpu_env, t0, cpu_gpr[rD(ctx->opcode)]);
74d37793 4443 tcg_temp_free(t0);
9a64fbe4 4444#endif
79aceca5
FB
4445}
4446
12de9a39
JM
4447#if defined(TARGET_PPC64)
4448/* Specific implementation for PowerPC 64 "bridge" emulation using SLB */
e8eaa2c0 4449
54623277 4450/* mfsr */
e8eaa2c0 4451static void gen_mfsr_64b(DisasContext *ctx)
12de9a39
JM
4452{
4453#if defined(CONFIG_USER_ONLY)
e06fcd75 4454 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
12de9a39 4455#else
74d37793 4456 TCGv t0;
76db3ba4 4457 if (unlikely(!ctx->mem_idx)) {
e06fcd75 4458 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
12de9a39
JM
4459 return;
4460 }
74d37793 4461 t0 = tcg_const_tl(SR(ctx->opcode));
c6c7cf05 4462 gen_helper_load_sr(cpu_gpr[rD(ctx->opcode)], cpu_env, t0);
74d37793 4463 tcg_temp_free(t0);
12de9a39
JM
4464#endif
4465}
4466
4467/* mfsrin */
e8eaa2c0 4468static void gen_mfsrin_64b(DisasContext *ctx)
12de9a39
JM
4469{
4470#if defined(CONFIG_USER_ONLY)
e06fcd75 4471 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
12de9a39 4472#else
74d37793 4473 TCGv t0;
76db3ba4 4474 if (unlikely(!ctx->mem_idx)) {
e06fcd75 4475 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
12de9a39
JM
4476 return;
4477 }
74d37793
AJ
4478 t0 = tcg_temp_new();
4479 tcg_gen_shri_tl(t0, cpu_gpr[rB(ctx->opcode)], 28);
4480 tcg_gen_andi_tl(t0, t0, 0xF);
c6c7cf05 4481 gen_helper_load_sr(cpu_gpr[rD(ctx->opcode)], cpu_env, t0);
74d37793 4482 tcg_temp_free(t0);
12de9a39
JM
4483#endif
4484}
4485
4486/* mtsr */
e8eaa2c0 4487static void gen_mtsr_64b(DisasContext *ctx)
12de9a39
JM
4488{
4489#if defined(CONFIG_USER_ONLY)
e06fcd75 4490 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
12de9a39 4491#else
74d37793 4492 TCGv t0;
76db3ba4 4493 if (unlikely(!ctx->mem_idx)) {
e06fcd75 4494 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
12de9a39
JM
4495 return;
4496 }
74d37793 4497 t0 = tcg_const_tl(SR(ctx->opcode));
c6c7cf05 4498 gen_helper_store_sr(cpu_env, t0, cpu_gpr[rS(ctx->opcode)]);
74d37793 4499 tcg_temp_free(t0);
12de9a39
JM
4500#endif
4501}
4502
4503/* mtsrin */
e8eaa2c0 4504static void gen_mtsrin_64b(DisasContext *ctx)
12de9a39
JM
4505{
4506#if defined(CONFIG_USER_ONLY)
e06fcd75 4507 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
12de9a39 4508#else
74d37793 4509 TCGv t0;
76db3ba4 4510 if (unlikely(!ctx->mem_idx)) {
e06fcd75 4511 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
12de9a39
JM
4512 return;
4513 }
74d37793
AJ
4514 t0 = tcg_temp_new();
4515 tcg_gen_shri_tl(t0, cpu_gpr[rB(ctx->opcode)], 28);
4516 tcg_gen_andi_tl(t0, t0, 0xF);
c6c7cf05 4517 gen_helper_store_sr(cpu_env, t0, cpu_gpr[rS(ctx->opcode)]);
74d37793 4518 tcg_temp_free(t0);
12de9a39
JM
4519#endif
4520}
f6b868fc
BS
4521
4522/* slbmte */
e8eaa2c0 4523static void gen_slbmte(DisasContext *ctx)
f6b868fc
BS
4524{
4525#if defined(CONFIG_USER_ONLY)
4526 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
4527#else
4528 if (unlikely(!ctx->mem_idx)) {
4529 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
4530 return;
4531 }
c6c7cf05
BS
4532 gen_helper_store_slb(cpu_env, cpu_gpr[rB(ctx->opcode)],
4533 cpu_gpr[rS(ctx->opcode)]);
f6b868fc
BS
4534#endif
4535}
4536
efdef95f
DG
4537static void gen_slbmfee(DisasContext *ctx)
4538{
4539#if defined(CONFIG_USER_ONLY)
4540 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
4541#else
4542 if (unlikely(!ctx->mem_idx)) {
4543 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
4544 return;
4545 }
c6c7cf05 4546 gen_helper_load_slb_esid(cpu_gpr[rS(ctx->opcode)], cpu_env,
efdef95f
DG
4547 cpu_gpr[rB(ctx->opcode)]);
4548#endif
4549}
4550
4551static void gen_slbmfev(DisasContext *ctx)
4552{
4553#if defined(CONFIG_USER_ONLY)
4554 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
4555#else
4556 if (unlikely(!ctx->mem_idx)) {
4557 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
4558 return;
4559 }
c6c7cf05 4560 gen_helper_load_slb_vsid(cpu_gpr[rS(ctx->opcode)], cpu_env,
efdef95f
DG
4561 cpu_gpr[rB(ctx->opcode)]);
4562#endif
4563}
12de9a39
JM
4564#endif /* defined(TARGET_PPC64) */
4565
79aceca5 4566/*** Lookaside buffer management ***/
76db3ba4 4567/* Optional & mem_idx only: */
99e300ef 4568
54623277 4569/* tlbia */
99e300ef 4570static void gen_tlbia(DisasContext *ctx)
79aceca5 4571{
9a64fbe4 4572#if defined(CONFIG_USER_ONLY)
e06fcd75 4573 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
9a64fbe4 4574#else
76db3ba4 4575 if (unlikely(!ctx->mem_idx)) {
e06fcd75 4576 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
9fddaa0c 4577 return;
9a64fbe4 4578 }
c6c7cf05 4579 gen_helper_tlbia(cpu_env);
9a64fbe4 4580#endif
79aceca5
FB
4581}
4582
bf14b1ce 4583/* tlbiel */
99e300ef 4584static void gen_tlbiel(DisasContext *ctx)
bf14b1ce
BS
4585{
4586#if defined(CONFIG_USER_ONLY)
4587 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
4588#else
4589 if (unlikely(!ctx->mem_idx)) {
4590 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
4591 return;
4592 }
c6c7cf05 4593 gen_helper_tlbie(cpu_env, cpu_gpr[rB(ctx->opcode)]);
bf14b1ce
BS
4594#endif
4595}
4596
79aceca5 4597/* tlbie */
99e300ef 4598static void gen_tlbie(DisasContext *ctx)
79aceca5 4599{
9a64fbe4 4600#if defined(CONFIG_USER_ONLY)
e06fcd75 4601 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
9a64fbe4 4602#else
76db3ba4 4603 if (unlikely(!ctx->mem_idx)) {
e06fcd75 4604 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
9fddaa0c 4605 return;
9a64fbe4 4606 }
9ca3f7f3 4607 if (NARROW_MODE(ctx)) {
74d37793
AJ
4608 TCGv t0 = tcg_temp_new();
4609 tcg_gen_ext32u_tl(t0, cpu_gpr[rB(ctx->opcode)]);
c6c7cf05 4610 gen_helper_tlbie(cpu_env, t0);
74d37793 4611 tcg_temp_free(t0);
9ca3f7f3 4612 } else {
c6c7cf05 4613 gen_helper_tlbie(cpu_env, cpu_gpr[rB(ctx->opcode)]);
9ca3f7f3 4614 }
9a64fbe4 4615#endif
79aceca5
FB
4616}
4617
4618/* tlbsync */
99e300ef 4619static void gen_tlbsync(DisasContext *ctx)
79aceca5 4620{
9a64fbe4 4621#if defined(CONFIG_USER_ONLY)
e06fcd75 4622 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
9a64fbe4 4623#else
76db3ba4 4624 if (unlikely(!ctx->mem_idx)) {
e06fcd75 4625 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
9fddaa0c 4626 return;
9a64fbe4
FB
4627 }
4628 /* This has no effect: it should ensure that all previous
4629 * tlbie have completed
4630 */
e06fcd75 4631 gen_stop_exception(ctx);
9a64fbe4 4632#endif
79aceca5
FB
4633}
4634
426613db
JM
4635#if defined(TARGET_PPC64)
4636/* slbia */
99e300ef 4637static void gen_slbia(DisasContext *ctx)
426613db
JM
4638{
4639#if defined(CONFIG_USER_ONLY)
e06fcd75 4640 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
426613db 4641#else
76db3ba4 4642 if (unlikely(!ctx->mem_idx)) {
e06fcd75 4643 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
426613db
JM
4644 return;
4645 }
c6c7cf05 4646 gen_helper_slbia(cpu_env);
426613db
JM
4647#endif
4648}
4649
4650/* slbie */
99e300ef 4651static void gen_slbie(DisasContext *ctx)
426613db
JM
4652{
4653#if defined(CONFIG_USER_ONLY)
e06fcd75 4654 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
426613db 4655#else
76db3ba4 4656 if (unlikely(!ctx->mem_idx)) {
e06fcd75 4657 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
426613db
JM
4658 return;
4659 }
c6c7cf05 4660 gen_helper_slbie(cpu_env, cpu_gpr[rB(ctx->opcode)]);
426613db
JM
4661#endif
4662}
4663#endif
4664
79aceca5
FB
4665/*** External control ***/
4666/* Optional: */
99e300ef 4667
54623277 4668/* eciwx */
99e300ef 4669static void gen_eciwx(DisasContext *ctx)
79aceca5 4670{
76db3ba4 4671 TCGv t0;
fa407c03 4672 /* Should check EAR[E] ! */
76db3ba4
AJ
4673 gen_set_access_type(ctx, ACCESS_EXT);
4674 t0 = tcg_temp_new();
4675 gen_addr_reg_index(ctx, t0);
fa407c03 4676 gen_check_align(ctx, t0, 0x03);
76db3ba4 4677 gen_qemu_ld32u(ctx, cpu_gpr[rD(ctx->opcode)], t0);
fa407c03 4678 tcg_temp_free(t0);
76a66253
JM
4679}
4680
4681/* ecowx */
99e300ef 4682static void gen_ecowx(DisasContext *ctx)
76a66253 4683{
76db3ba4 4684 TCGv t0;
fa407c03 4685 /* Should check EAR[E] ! */
76db3ba4
AJ
4686 gen_set_access_type(ctx, ACCESS_EXT);
4687 t0 = tcg_temp_new();
4688 gen_addr_reg_index(ctx, t0);
fa407c03 4689 gen_check_align(ctx, t0, 0x03);
76db3ba4 4690 gen_qemu_st32(ctx, cpu_gpr[rD(ctx->opcode)], t0);
fa407c03 4691 tcg_temp_free(t0);
76a66253
JM
4692}
4693
4694/* PowerPC 601 specific instructions */
99e300ef 4695
54623277 4696/* abs - abs. */
99e300ef 4697static void gen_abs(DisasContext *ctx)
76a66253 4698{
22e0e173
AJ
4699 int l1 = gen_new_label();
4700 int l2 = gen_new_label();
4701 tcg_gen_brcondi_tl(TCG_COND_GE, cpu_gpr[rA(ctx->opcode)], 0, l1);
4702 tcg_gen_neg_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)]);
4703 tcg_gen_br(l2);
4704 gen_set_label(l1);
4705 tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)]);
4706 gen_set_label(l2);
76a66253 4707 if (unlikely(Rc(ctx->opcode) != 0))
22e0e173 4708 gen_set_Rc0(ctx, cpu_gpr[rD(ctx->opcode)]);
76a66253
JM
4709}
4710
4711/* abso - abso. */
99e300ef 4712static void gen_abso(DisasContext *ctx)
76a66253 4713{
22e0e173
AJ
4714 int l1 = gen_new_label();
4715 int l2 = gen_new_label();
4716 int l3 = gen_new_label();
4717 /* Start with XER OV disabled, the most likely case */
da91a00f 4718 tcg_gen_movi_tl(cpu_ov, 0);
22e0e173
AJ
4719 tcg_gen_brcondi_tl(TCG_COND_GE, cpu_gpr[rA(ctx->opcode)], 0, l2);
4720 tcg_gen_brcondi_tl(TCG_COND_NE, cpu_gpr[rA(ctx->opcode)], 0x80000000, l1);
da91a00f
RH
4721 tcg_gen_movi_tl(cpu_ov, 1);
4722 tcg_gen_movi_tl(cpu_so, 1);
22e0e173
AJ
4723 tcg_gen_br(l2);
4724 gen_set_label(l1);
4725 tcg_gen_neg_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)]);
4726 tcg_gen_br(l3);
4727 gen_set_label(l2);
4728 tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)]);
4729 gen_set_label(l3);
76a66253 4730 if (unlikely(Rc(ctx->opcode) != 0))
22e0e173 4731 gen_set_Rc0(ctx, cpu_gpr[rD(ctx->opcode)]);
76a66253
JM
4732}
4733
4734/* clcs */
99e300ef 4735static void gen_clcs(DisasContext *ctx)
76a66253 4736{
22e0e173 4737 TCGv_i32 t0 = tcg_const_i32(rA(ctx->opcode));
d523dd00 4738 gen_helper_clcs(cpu_gpr[rD(ctx->opcode)], cpu_env, t0);
22e0e173 4739 tcg_temp_free_i32(t0);
c7697e1f 4740 /* Rc=1 sets CR0 to an undefined state */
76a66253
JM
4741}
4742
4743/* div - div. */
99e300ef 4744static void gen_div(DisasContext *ctx)
76a66253 4745{
d15f74fb
BS
4746 gen_helper_div(cpu_gpr[rD(ctx->opcode)], cpu_env, cpu_gpr[rA(ctx->opcode)],
4747 cpu_gpr[rB(ctx->opcode)]);
76a66253 4748 if (unlikely(Rc(ctx->opcode) != 0))
22e0e173 4749 gen_set_Rc0(ctx, cpu_gpr[rD(ctx->opcode)]);
76a66253
JM
4750}
4751
4752/* divo - divo. */
99e300ef 4753static void gen_divo(DisasContext *ctx)
76a66253 4754{
d15f74fb
BS
4755 gen_helper_divo(cpu_gpr[rD(ctx->opcode)], cpu_env, cpu_gpr[rA(ctx->opcode)],
4756 cpu_gpr[rB(ctx->opcode)]);
76a66253 4757 if (unlikely(Rc(ctx->opcode) != 0))
22e0e173 4758 gen_set_Rc0(ctx, cpu_gpr[rD(ctx->opcode)]);
76a66253
JM
4759}
4760
4761/* divs - divs. */
99e300ef 4762static void gen_divs(DisasContext *ctx)
76a66253 4763{
d15f74fb
BS
4764 gen_helper_divs(cpu_gpr[rD(ctx->opcode)], cpu_env, cpu_gpr[rA(ctx->opcode)],
4765 cpu_gpr[rB(ctx->opcode)]);
76a66253 4766 if (unlikely(Rc(ctx->opcode) != 0))
22e0e173 4767 gen_set_Rc0(ctx, cpu_gpr[rD(ctx->opcode)]);
76a66253
JM
4768}
4769
4770/* divso - divso. */
99e300ef 4771static void gen_divso(DisasContext *ctx)
76a66253 4772{
d15f74fb
BS
4773 gen_helper_divso(cpu_gpr[rD(ctx->opcode)], cpu_env,
4774 cpu_gpr[rA(ctx->opcode)], cpu_gpr[rB(ctx->opcode)]);
76a66253 4775 if (unlikely(Rc(ctx->opcode) != 0))
22e0e173 4776 gen_set_Rc0(ctx, cpu_gpr[rD(ctx->opcode)]);
76a66253
JM
4777}
4778
4779/* doz - doz. */
99e300ef 4780static void gen_doz(DisasContext *ctx)
76a66253 4781{
22e0e173
AJ
4782 int l1 = gen_new_label();
4783 int l2 = gen_new_label();
4784 tcg_gen_brcond_tl(TCG_COND_GE, cpu_gpr[rB(ctx->opcode)], cpu_gpr[rA(ctx->opcode)], l1);
4785 tcg_gen_sub_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rB(ctx->opcode)], cpu_gpr[rA(ctx->opcode)]);
4786 tcg_gen_br(l2);
4787 gen_set_label(l1);
4788 tcg_gen_movi_tl(cpu_gpr[rD(ctx->opcode)], 0);
4789 gen_set_label(l2);
76a66253 4790 if (unlikely(Rc(ctx->opcode) != 0))
22e0e173 4791 gen_set_Rc0(ctx, cpu_gpr[rD(ctx->opcode)]);
76a66253
JM
4792}
4793
4794/* dozo - dozo. */
99e300ef 4795static void gen_dozo(DisasContext *ctx)
76a66253 4796{
22e0e173
AJ
4797 int l1 = gen_new_label();
4798 int l2 = gen_new_label();
4799 TCGv t0 = tcg_temp_new();
4800 TCGv t1 = tcg_temp_new();
4801 TCGv t2 = tcg_temp_new();
4802 /* Start with XER OV disabled, the most likely case */
da91a00f 4803 tcg_gen_movi_tl(cpu_ov, 0);
22e0e173
AJ
4804 tcg_gen_brcond_tl(TCG_COND_GE, cpu_gpr[rB(ctx->opcode)], cpu_gpr[rA(ctx->opcode)], l1);
4805 tcg_gen_sub_tl(t0, cpu_gpr[rB(ctx->opcode)], cpu_gpr[rA(ctx->opcode)]);
4806 tcg_gen_xor_tl(t1, cpu_gpr[rB(ctx->opcode)], cpu_gpr[rA(ctx->opcode)]);
4807 tcg_gen_xor_tl(t2, cpu_gpr[rA(ctx->opcode)], t0);
4808 tcg_gen_andc_tl(t1, t1, t2);
4809 tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], t0);
4810 tcg_gen_brcondi_tl(TCG_COND_GE, t1, 0, l2);
da91a00f
RH
4811 tcg_gen_movi_tl(cpu_ov, 1);
4812 tcg_gen_movi_tl(cpu_so, 1);
22e0e173
AJ
4813 tcg_gen_br(l2);
4814 gen_set_label(l1);
4815 tcg_gen_movi_tl(cpu_gpr[rD(ctx->opcode)], 0);
4816 gen_set_label(l2);
4817 tcg_temp_free(t0);
4818 tcg_temp_free(t1);
4819 tcg_temp_free(t2);
76a66253 4820 if (unlikely(Rc(ctx->opcode) != 0))
22e0e173 4821 gen_set_Rc0(ctx, cpu_gpr[rD(ctx->opcode)]);
76a66253
JM
4822}
4823
4824/* dozi */
99e300ef 4825static void gen_dozi(DisasContext *ctx)
76a66253 4826{
22e0e173
AJ
4827 target_long simm = SIMM(ctx->opcode);
4828 int l1 = gen_new_label();
4829 int l2 = gen_new_label();
4830 tcg_gen_brcondi_tl(TCG_COND_LT, cpu_gpr[rA(ctx->opcode)], simm, l1);
4831 tcg_gen_subfi_tl(cpu_gpr[rD(ctx->opcode)], simm, cpu_gpr[rA(ctx->opcode)]);
4832 tcg_gen_br(l2);
4833 gen_set_label(l1);
4834 tcg_gen_movi_tl(cpu_gpr[rD(ctx->opcode)], 0);
4835 gen_set_label(l2);
4836 if (unlikely(Rc(ctx->opcode) != 0))
4837 gen_set_Rc0(ctx, cpu_gpr[rD(ctx->opcode)]);
76a66253
JM
4838}
4839
76a66253 4840/* lscbx - lscbx. */
99e300ef 4841static void gen_lscbx(DisasContext *ctx)
76a66253 4842{
bdb4b689
AJ
4843 TCGv t0 = tcg_temp_new();
4844 TCGv_i32 t1 = tcg_const_i32(rD(ctx->opcode));
4845 TCGv_i32 t2 = tcg_const_i32(rA(ctx->opcode));
4846 TCGv_i32 t3 = tcg_const_i32(rB(ctx->opcode));
76a66253 4847
76db3ba4 4848 gen_addr_reg_index(ctx, t0);
76a66253 4849 /* NIP cannot be restored if the memory exception comes from an helper */
d9bce9d9 4850 gen_update_nip(ctx, ctx->nip - 4);
2f5a189c 4851 gen_helper_lscbx(t0, cpu_env, t0, t1, t2, t3);
bdb4b689
AJ
4852 tcg_temp_free_i32(t1);
4853 tcg_temp_free_i32(t2);
4854 tcg_temp_free_i32(t3);
3d7b417e 4855 tcg_gen_andi_tl(cpu_xer, cpu_xer, ~0x7F);
bdb4b689 4856 tcg_gen_or_tl(cpu_xer, cpu_xer, t0);
76a66253 4857 if (unlikely(Rc(ctx->opcode) != 0))
bdb4b689
AJ
4858 gen_set_Rc0(ctx, t0);
4859 tcg_temp_free(t0);
76a66253
JM
4860}
4861
4862/* maskg - maskg. */
99e300ef 4863static void gen_maskg(DisasContext *ctx)
76a66253 4864{
22e0e173
AJ
4865 int l1 = gen_new_label();
4866 TCGv t0 = tcg_temp_new();
4867 TCGv t1 = tcg_temp_new();
4868 TCGv t2 = tcg_temp_new();
4869 TCGv t3 = tcg_temp_new();
4870 tcg_gen_movi_tl(t3, 0xFFFFFFFF);
4871 tcg_gen_andi_tl(t0, cpu_gpr[rB(ctx->opcode)], 0x1F);
4872 tcg_gen_andi_tl(t1, cpu_gpr[rS(ctx->opcode)], 0x1F);
4873 tcg_gen_addi_tl(t2, t0, 1);
4874 tcg_gen_shr_tl(t2, t3, t2);
4875 tcg_gen_shr_tl(t3, t3, t1);
4876 tcg_gen_xor_tl(cpu_gpr[rA(ctx->opcode)], t2, t3);
4877 tcg_gen_brcond_tl(TCG_COND_GE, t0, t1, l1);
4878 tcg_gen_neg_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rA(ctx->opcode)]);
4879 gen_set_label(l1);
4880 tcg_temp_free(t0);
4881 tcg_temp_free(t1);
4882 tcg_temp_free(t2);
4883 tcg_temp_free(t3);
76a66253 4884 if (unlikely(Rc(ctx->opcode) != 0))
22e0e173 4885 gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
76a66253
JM
4886}
4887
4888/* maskir - maskir. */
99e300ef 4889static void gen_maskir(DisasContext *ctx)
76a66253 4890{
22e0e173
AJ
4891 TCGv t0 = tcg_temp_new();
4892 TCGv t1 = tcg_temp_new();
4893 tcg_gen_and_tl(t0, cpu_gpr[rS(ctx->opcode)], cpu_gpr[rB(ctx->opcode)]);
4894 tcg_gen_andc_tl(t1, cpu_gpr[rA(ctx->opcode)], cpu_gpr[rB(ctx->opcode)]);
4895 tcg_gen_or_tl(cpu_gpr[rA(ctx->opcode)], t0, t1);
4896 tcg_temp_free(t0);
4897 tcg_temp_free(t1);
76a66253 4898 if (unlikely(Rc(ctx->opcode) != 0))
22e0e173 4899 gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
76a66253
JM
4900}
4901
4902/* mul - mul. */
99e300ef 4903static void gen_mul(DisasContext *ctx)
76a66253 4904{
22e0e173
AJ
4905 TCGv_i64 t0 = tcg_temp_new_i64();
4906 TCGv_i64 t1 = tcg_temp_new_i64();
4907 TCGv t2 = tcg_temp_new();
4908 tcg_gen_extu_tl_i64(t0, cpu_gpr[rA(ctx->opcode)]);
4909 tcg_gen_extu_tl_i64(t1, cpu_gpr[rB(ctx->opcode)]);
4910 tcg_gen_mul_i64(t0, t0, t1);
4911 tcg_gen_trunc_i64_tl(t2, t0);
4912 gen_store_spr(SPR_MQ, t2);
4913 tcg_gen_shri_i64(t1, t0, 32);
4914 tcg_gen_trunc_i64_tl(cpu_gpr[rD(ctx->opcode)], t1);
4915 tcg_temp_free_i64(t0);
4916 tcg_temp_free_i64(t1);
4917 tcg_temp_free(t2);
76a66253 4918 if (unlikely(Rc(ctx->opcode) != 0))
22e0e173 4919 gen_set_Rc0(ctx, cpu_gpr[rD(ctx->opcode)]);
76a66253
JM
4920}
4921
4922/* mulo - mulo. */
99e300ef 4923static void gen_mulo(DisasContext *ctx)
76a66253 4924{
22e0e173
AJ
4925 int l1 = gen_new_label();
4926 TCGv_i64 t0 = tcg_temp_new_i64();
4927 TCGv_i64 t1 = tcg_temp_new_i64();
4928 TCGv t2 = tcg_temp_new();
4929 /* Start with XER OV disabled, the most likely case */
da91a00f 4930 tcg_gen_movi_tl(cpu_ov, 0);
22e0e173
AJ
4931 tcg_gen_extu_tl_i64(t0, cpu_gpr[rA(ctx->opcode)]);
4932 tcg_gen_extu_tl_i64(t1, cpu_gpr[rB(ctx->opcode)]);
4933 tcg_gen_mul_i64(t0, t0, t1);
4934 tcg_gen_trunc_i64_tl(t2, t0);
4935 gen_store_spr(SPR_MQ, t2);
4936 tcg_gen_shri_i64(t1, t0, 32);
4937 tcg_gen_trunc_i64_tl(cpu_gpr[rD(ctx->opcode)], t1);
4938 tcg_gen_ext32s_i64(t1, t0);
4939 tcg_gen_brcond_i64(TCG_COND_EQ, t0, t1, l1);
da91a00f
RH
4940 tcg_gen_movi_tl(cpu_ov, 1);
4941 tcg_gen_movi_tl(cpu_so, 1);
22e0e173
AJ
4942 gen_set_label(l1);
4943 tcg_temp_free_i64(t0);
4944 tcg_temp_free_i64(t1);
4945 tcg_temp_free(t2);
76a66253 4946 if (unlikely(Rc(ctx->opcode) != 0))
22e0e173 4947 gen_set_Rc0(ctx, cpu_gpr[rD(ctx->opcode)]);
76a66253
JM
4948}
4949
4950/* nabs - nabs. */
99e300ef 4951static void gen_nabs(DisasContext *ctx)
76a66253 4952{
22e0e173
AJ
4953 int l1 = gen_new_label();
4954 int l2 = gen_new_label();
4955 tcg_gen_brcondi_tl(TCG_COND_GT, cpu_gpr[rA(ctx->opcode)], 0, l1);
4956 tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)]);
4957 tcg_gen_br(l2);
4958 gen_set_label(l1);
4959 tcg_gen_neg_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)]);
4960 gen_set_label(l2);
76a66253 4961 if (unlikely(Rc(ctx->opcode) != 0))
22e0e173 4962 gen_set_Rc0(ctx, cpu_gpr[rD(ctx->opcode)]);
76a66253
JM
4963}
4964
4965/* nabso - nabso. */
99e300ef 4966static void gen_nabso(DisasContext *ctx)
76a66253 4967{
22e0e173
AJ
4968 int l1 = gen_new_label();
4969 int l2 = gen_new_label();
4970 tcg_gen_brcondi_tl(TCG_COND_GT, cpu_gpr[rA(ctx->opcode)], 0, l1);
4971 tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)]);
4972 tcg_gen_br(l2);
4973 gen_set_label(l1);
4974 tcg_gen_neg_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)]);
4975 gen_set_label(l2);
4976 /* nabs never overflows */
da91a00f 4977 tcg_gen_movi_tl(cpu_ov, 0);
76a66253 4978 if (unlikely(Rc(ctx->opcode) != 0))
22e0e173 4979 gen_set_Rc0(ctx, cpu_gpr[rD(ctx->opcode)]);
76a66253
JM
4980}
4981
4982/* rlmi - rlmi. */
99e300ef 4983static void gen_rlmi(DisasContext *ctx)
76a66253 4984{
7487953d
AJ
4985 uint32_t mb = MB(ctx->opcode);
4986 uint32_t me = ME(ctx->opcode);
4987 TCGv t0 = tcg_temp_new();
4988 tcg_gen_andi_tl(t0, cpu_gpr[rB(ctx->opcode)], 0x1F);
4989 tcg_gen_rotl_tl(t0, cpu_gpr[rS(ctx->opcode)], t0);
4990 tcg_gen_andi_tl(t0, t0, MASK(mb, me));
4991 tcg_gen_andi_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rA(ctx->opcode)], ~MASK(mb, me));
4992 tcg_gen_or_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rA(ctx->opcode)], t0);
4993 tcg_temp_free(t0);
76a66253 4994 if (unlikely(Rc(ctx->opcode) != 0))
7487953d 4995 gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
76a66253
JM
4996}
4997
4998/* rrib - rrib. */
99e300ef 4999static void gen_rrib(DisasContext *ctx)
76a66253 5000{
7487953d
AJ
5001 TCGv t0 = tcg_temp_new();
5002 TCGv t1 = tcg_temp_new();
5003 tcg_gen_andi_tl(t0, cpu_gpr[rB(ctx->opcode)], 0x1F);
5004 tcg_gen_movi_tl(t1, 0x80000000);
5005 tcg_gen_shr_tl(t1, t1, t0);
5006 tcg_gen_shr_tl(t0, cpu_gpr[rS(ctx->opcode)], t0);
5007 tcg_gen_and_tl(t0, t0, t1);
5008 tcg_gen_andc_tl(t1, cpu_gpr[rA(ctx->opcode)], t1);
5009 tcg_gen_or_tl(cpu_gpr[rA(ctx->opcode)], t0, t1);
5010 tcg_temp_free(t0);
5011 tcg_temp_free(t1);
76a66253 5012 if (unlikely(Rc(ctx->opcode) != 0))
7487953d 5013 gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
76a66253
JM
5014}
5015
5016/* sle - sle. */
99e300ef 5017static void gen_sle(DisasContext *ctx)
76a66253 5018{
7487953d
AJ
5019 TCGv t0 = tcg_temp_new();
5020 TCGv t1 = tcg_temp_new();
5021 tcg_gen_andi_tl(t1, cpu_gpr[rB(ctx->opcode)], 0x1F);
5022 tcg_gen_shl_tl(t0, cpu_gpr[rS(ctx->opcode)], t1);
5023 tcg_gen_subfi_tl(t1, 32, t1);
5024 tcg_gen_shr_tl(t1, cpu_gpr[rS(ctx->opcode)], t1);
5025 tcg_gen_or_tl(t1, t0, t1);
5026 tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], t0);
5027 gen_store_spr(SPR_MQ, t1);
5028 tcg_temp_free(t0);
5029 tcg_temp_free(t1);
76a66253 5030 if (unlikely(Rc(ctx->opcode) != 0))
7487953d 5031 gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
76a66253
JM
5032}
5033
5034/* sleq - sleq. */
99e300ef 5035static void gen_sleq(DisasContext *ctx)
76a66253 5036{
7487953d
AJ
5037 TCGv t0 = tcg_temp_new();
5038 TCGv t1 = tcg_temp_new();
5039 TCGv t2 = tcg_temp_new();
5040 tcg_gen_andi_tl(t0, cpu_gpr[rB(ctx->opcode)], 0x1F);
5041 tcg_gen_movi_tl(t2, 0xFFFFFFFF);
5042 tcg_gen_shl_tl(t2, t2, t0);
5043 tcg_gen_rotl_tl(t0, cpu_gpr[rS(ctx->opcode)], t0);
5044 gen_load_spr(t1, SPR_MQ);
5045 gen_store_spr(SPR_MQ, t0);
5046 tcg_gen_and_tl(t0, t0, t2);
5047 tcg_gen_andc_tl(t1, t1, t2);
5048 tcg_gen_or_tl(cpu_gpr[rA(ctx->opcode)], t0, t1);
5049 tcg_temp_free(t0);
5050 tcg_temp_free(t1);
5051 tcg_temp_free(t2);
76a66253 5052 if (unlikely(Rc(ctx->opcode) != 0))
7487953d 5053 gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
76a66253
JM
5054}
5055
5056/* sliq - sliq. */
99e300ef 5057static void gen_sliq(DisasContext *ctx)
76a66253 5058{
7487953d
AJ
5059 int sh = SH(ctx->opcode);
5060 TCGv t0 = tcg_temp_new();
5061 TCGv t1 = tcg_temp_new();
5062 tcg_gen_shli_tl(t0, cpu_gpr[rS(ctx->opcode)], sh);
5063 tcg_gen_shri_tl(t1, cpu_gpr[rS(ctx->opcode)], 32 - sh);
5064 tcg_gen_or_tl(t1, t0, t1);
5065 tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], t0);
5066 gen_store_spr(SPR_MQ, t1);
5067 tcg_temp_free(t0);
5068 tcg_temp_free(t1);
76a66253 5069 if (unlikely(Rc(ctx->opcode) != 0))
7487953d 5070 gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
76a66253
JM
5071}
5072
5073/* slliq - slliq. */
99e300ef 5074static void gen_slliq(DisasContext *ctx)
76a66253 5075{
7487953d
AJ
5076 int sh = SH(ctx->opcode);
5077 TCGv t0 = tcg_temp_new();
5078 TCGv t1 = tcg_temp_new();
5079 tcg_gen_rotli_tl(t0, cpu_gpr[rS(ctx->opcode)], sh);
5080 gen_load_spr(t1, SPR_MQ);
5081 gen_store_spr(SPR_MQ, t0);
5082 tcg_gen_andi_tl(t0, t0, (0xFFFFFFFFU << sh));
5083 tcg_gen_andi_tl(t1, t1, ~(0xFFFFFFFFU << sh));
5084 tcg_gen_or_tl(cpu_gpr[rA(ctx->opcode)], t0, t1);
5085 tcg_temp_free(t0);
5086 tcg_temp_free(t1);
76a66253 5087 if (unlikely(Rc(ctx->opcode) != 0))
7487953d 5088 gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
76a66253
JM
5089}
5090
5091/* sllq - sllq. */
99e300ef 5092static void gen_sllq(DisasContext *ctx)
76a66253 5093{
7487953d
AJ
5094 int l1 = gen_new_label();
5095 int l2 = gen_new_label();
5096 TCGv t0 = tcg_temp_local_new();
5097 TCGv t1 = tcg_temp_local_new();
5098 TCGv t2 = tcg_temp_local_new();
5099 tcg_gen_andi_tl(t2, cpu_gpr[rB(ctx->opcode)], 0x1F);
5100 tcg_gen_movi_tl(t1, 0xFFFFFFFF);
5101 tcg_gen_shl_tl(t1, t1, t2);
5102 tcg_gen_andi_tl(t0, cpu_gpr[rB(ctx->opcode)], 0x20);
5103 tcg_gen_brcondi_tl(TCG_COND_EQ, t0, 0, l1);
5104 gen_load_spr(t0, SPR_MQ);
5105 tcg_gen_and_tl(cpu_gpr[rA(ctx->opcode)], t0, t1);
5106 tcg_gen_br(l2);
5107 gen_set_label(l1);
5108 tcg_gen_shl_tl(t0, cpu_gpr[rS(ctx->opcode)], t2);
5109 gen_load_spr(t2, SPR_MQ);
5110 tcg_gen_andc_tl(t1, t2, t1);
5111 tcg_gen_or_tl(cpu_gpr[rA(ctx->opcode)], t0, t1);
5112 gen_set_label(l2);
5113 tcg_temp_free(t0);
5114 tcg_temp_free(t1);
5115 tcg_temp_free(t2);
76a66253 5116 if (unlikely(Rc(ctx->opcode) != 0))
7487953d 5117 gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
76a66253
JM
5118}
5119
5120/* slq - slq. */
99e300ef 5121static void gen_slq(DisasContext *ctx)
76a66253 5122{
7487953d
AJ
5123 int l1 = gen_new_label();
5124 TCGv t0 = tcg_temp_new();
5125 TCGv t1 = tcg_temp_new();
5126 tcg_gen_andi_tl(t1, cpu_gpr[rB(ctx->opcode)], 0x1F);
5127 tcg_gen_shl_tl(t0, cpu_gpr[rS(ctx->opcode)], t1);
5128 tcg_gen_subfi_tl(t1, 32, t1);
5129 tcg_gen_shr_tl(t1, cpu_gpr[rS(ctx->opcode)], t1);
5130 tcg_gen_or_tl(t1, t0, t1);
5131 gen_store_spr(SPR_MQ, t1);
5132 tcg_gen_andi_tl(t1, cpu_gpr[rB(ctx->opcode)], 0x20);
5133 tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], t0);
5134 tcg_gen_brcondi_tl(TCG_COND_EQ, t1, 0, l1);
5135 tcg_gen_movi_tl(cpu_gpr[rA(ctx->opcode)], 0);
5136 gen_set_label(l1);
5137 tcg_temp_free(t0);
5138 tcg_temp_free(t1);
76a66253 5139 if (unlikely(Rc(ctx->opcode) != 0))
7487953d 5140 gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
76a66253
JM
5141}
5142
d9bce9d9 5143/* sraiq - sraiq. */
99e300ef 5144static void gen_sraiq(DisasContext *ctx)
76a66253 5145{
7487953d
AJ
5146 int sh = SH(ctx->opcode);
5147 int l1 = gen_new_label();
5148 TCGv t0 = tcg_temp_new();
5149 TCGv t1 = tcg_temp_new();
5150 tcg_gen_shri_tl(t0, cpu_gpr[rS(ctx->opcode)], sh);
5151 tcg_gen_shli_tl(t1, cpu_gpr[rS(ctx->opcode)], 32 - sh);
5152 tcg_gen_or_tl(t0, t0, t1);
5153 gen_store_spr(SPR_MQ, t0);
da91a00f 5154 tcg_gen_movi_tl(cpu_ca, 0);
7487953d
AJ
5155 tcg_gen_brcondi_tl(TCG_COND_EQ, t1, 0, l1);
5156 tcg_gen_brcondi_tl(TCG_COND_GE, cpu_gpr[rS(ctx->opcode)], 0, l1);
da91a00f 5157 tcg_gen_movi_tl(cpu_ca, 1);
7487953d
AJ
5158 gen_set_label(l1);
5159 tcg_gen_sari_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)], sh);
5160 tcg_temp_free(t0);
5161 tcg_temp_free(t1);
76a66253 5162 if (unlikely(Rc(ctx->opcode) != 0))
7487953d 5163 gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
76a66253
JM
5164}
5165
5166/* sraq - sraq. */
99e300ef 5167static void gen_sraq(DisasContext *ctx)
76a66253 5168{
7487953d
AJ
5169 int l1 = gen_new_label();
5170 int l2 = gen_new_label();
5171 TCGv t0 = tcg_temp_new();
5172 TCGv t1 = tcg_temp_local_new();
5173 TCGv t2 = tcg_temp_local_new();
5174 tcg_gen_andi_tl(t2, cpu_gpr[rB(ctx->opcode)], 0x1F);
5175 tcg_gen_shr_tl(t0, cpu_gpr[rS(ctx->opcode)], t2);
5176 tcg_gen_sar_tl(t1, cpu_gpr[rS(ctx->opcode)], t2);
5177 tcg_gen_subfi_tl(t2, 32, t2);
5178 tcg_gen_shl_tl(t2, cpu_gpr[rS(ctx->opcode)], t2);
5179 tcg_gen_or_tl(t0, t0, t2);
5180 gen_store_spr(SPR_MQ, t0);
5181 tcg_gen_andi_tl(t0, cpu_gpr[rB(ctx->opcode)], 0x20);
5182 tcg_gen_brcondi_tl(TCG_COND_EQ, t2, 0, l1);
5183 tcg_gen_mov_tl(t2, cpu_gpr[rS(ctx->opcode)]);
5184 tcg_gen_sari_tl(t1, cpu_gpr[rS(ctx->opcode)], 31);
5185 gen_set_label(l1);
5186 tcg_temp_free(t0);
5187 tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], t1);
da91a00f 5188 tcg_gen_movi_tl(cpu_ca, 0);
7487953d
AJ
5189 tcg_gen_brcondi_tl(TCG_COND_GE, t1, 0, l2);
5190 tcg_gen_brcondi_tl(TCG_COND_EQ, t2, 0, l2);
da91a00f 5191 tcg_gen_movi_tl(cpu_ca, 1);
7487953d
AJ
5192 gen_set_label(l2);
5193 tcg_temp_free(t1);
5194 tcg_temp_free(t2);
76a66253 5195 if (unlikely(Rc(ctx->opcode) != 0))
7487953d 5196 gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
76a66253
JM
5197}
5198
5199/* sre - sre. */
99e300ef 5200static void gen_sre(DisasContext *ctx)
76a66253 5201{
7487953d
AJ
5202 TCGv t0 = tcg_temp_new();
5203 TCGv t1 = tcg_temp_new();
5204 tcg_gen_andi_tl(t1, cpu_gpr[rB(ctx->opcode)], 0x1F);
5205 tcg_gen_shr_tl(t0, cpu_gpr[rS(ctx->opcode)], t1);
5206 tcg_gen_subfi_tl(t1, 32, t1);
5207 tcg_gen_shl_tl(t1, cpu_gpr[rS(ctx->opcode)], t1);
5208 tcg_gen_or_tl(t1, t0, t1);
5209 tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], t0);
5210 gen_store_spr(SPR_MQ, t1);
5211 tcg_temp_free(t0);
5212 tcg_temp_free(t1);
76a66253 5213 if (unlikely(Rc(ctx->opcode) != 0))
7487953d 5214 gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
76a66253
JM
5215}
5216
5217/* srea - srea. */
99e300ef 5218static void gen_srea(DisasContext *ctx)
76a66253 5219{
7487953d
AJ
5220 TCGv t0 = tcg_temp_new();
5221 TCGv t1 = tcg_temp_new();
5222 tcg_gen_andi_tl(t1, cpu_gpr[rB(ctx->opcode)], 0x1F);
5223 tcg_gen_rotr_tl(t0, cpu_gpr[rS(ctx->opcode)], t1);
5224 gen_store_spr(SPR_MQ, t0);
5225 tcg_gen_sar_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)], t1);
5226 tcg_temp_free(t0);
5227 tcg_temp_free(t1);
76a66253 5228 if (unlikely(Rc(ctx->opcode) != 0))
7487953d 5229 gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
76a66253
JM
5230}
5231
5232/* sreq */
99e300ef 5233static void gen_sreq(DisasContext *ctx)
76a66253 5234{
7487953d
AJ
5235 TCGv t0 = tcg_temp_new();
5236 TCGv t1 = tcg_temp_new();
5237 TCGv t2 = tcg_temp_new();
5238 tcg_gen_andi_tl(t0, cpu_gpr[rB(ctx->opcode)], 0x1F);
5239 tcg_gen_movi_tl(t1, 0xFFFFFFFF);
5240 tcg_gen_shr_tl(t1, t1, t0);
5241 tcg_gen_rotr_tl(t0, cpu_gpr[rS(ctx->opcode)], t0);
5242 gen_load_spr(t2, SPR_MQ);
5243 gen_store_spr(SPR_MQ, t0);
5244 tcg_gen_and_tl(t0, t0, t1);
5245 tcg_gen_andc_tl(t2, t2, t1);
5246 tcg_gen_or_tl(cpu_gpr[rA(ctx->opcode)], t0, t2);
5247 tcg_temp_free(t0);
5248 tcg_temp_free(t1);
5249 tcg_temp_free(t2);
76a66253 5250 if (unlikely(Rc(ctx->opcode) != 0))
7487953d 5251 gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
76a66253
JM
5252}
5253
5254/* sriq */
99e300ef 5255static void gen_sriq(DisasContext *ctx)
76a66253 5256{
7487953d
AJ
5257 int sh = SH(ctx->opcode);
5258 TCGv t0 = tcg_temp_new();
5259 TCGv t1 = tcg_temp_new();
5260 tcg_gen_shri_tl(t0, cpu_gpr[rS(ctx->opcode)], sh);
5261 tcg_gen_shli_tl(t1, cpu_gpr[rS(ctx->opcode)], 32 - sh);
5262 tcg_gen_or_tl(t1, t0, t1);
5263 tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], t0);
5264 gen_store_spr(SPR_MQ, t1);
5265 tcg_temp_free(t0);
5266 tcg_temp_free(t1);
76a66253 5267 if (unlikely(Rc(ctx->opcode) != 0))
7487953d 5268 gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
76a66253
JM
5269}
5270
5271/* srliq */
99e300ef 5272static void gen_srliq(DisasContext *ctx)
76a66253 5273{
7487953d
AJ
5274 int sh = SH(ctx->opcode);
5275 TCGv t0 = tcg_temp_new();
5276 TCGv t1 = tcg_temp_new();
5277 tcg_gen_rotri_tl(t0, cpu_gpr[rS(ctx->opcode)], sh);
5278 gen_load_spr(t1, SPR_MQ);
5279 gen_store_spr(SPR_MQ, t0);
5280 tcg_gen_andi_tl(t0, t0, (0xFFFFFFFFU >> sh));
5281 tcg_gen_andi_tl(t1, t1, ~(0xFFFFFFFFU >> sh));
5282 tcg_gen_or_tl(cpu_gpr[rA(ctx->opcode)], t0, t1);
5283 tcg_temp_free(t0);
5284 tcg_temp_free(t1);
76a66253 5285 if (unlikely(Rc(ctx->opcode) != 0))
7487953d 5286 gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
76a66253
JM
5287}
5288
5289/* srlq */
99e300ef 5290static void gen_srlq(DisasContext *ctx)
76a66253 5291{
7487953d
AJ
5292 int l1 = gen_new_label();
5293 int l2 = gen_new_label();
5294 TCGv t0 = tcg_temp_local_new();
5295 TCGv t1 = tcg_temp_local_new();
5296 TCGv t2 = tcg_temp_local_new();
5297 tcg_gen_andi_tl(t2, cpu_gpr[rB(ctx->opcode)], 0x1F);
5298 tcg_gen_movi_tl(t1, 0xFFFFFFFF);
5299 tcg_gen_shr_tl(t2, t1, t2);
5300 tcg_gen_andi_tl(t0, cpu_gpr[rB(ctx->opcode)], 0x20);
5301 tcg_gen_brcondi_tl(TCG_COND_EQ, t0, 0, l1);
5302 gen_load_spr(t0, SPR_MQ);
5303 tcg_gen_and_tl(cpu_gpr[rA(ctx->opcode)], t0, t2);
5304 tcg_gen_br(l2);
5305 gen_set_label(l1);
5306 tcg_gen_shr_tl(t0, cpu_gpr[rS(ctx->opcode)], t2);
5307 tcg_gen_and_tl(t0, t0, t2);
5308 gen_load_spr(t1, SPR_MQ);
5309 tcg_gen_andc_tl(t1, t1, t2);
5310 tcg_gen_or_tl(cpu_gpr[rA(ctx->opcode)], t0, t1);
5311 gen_set_label(l2);
5312 tcg_temp_free(t0);
5313 tcg_temp_free(t1);
5314 tcg_temp_free(t2);
76a66253 5315 if (unlikely(Rc(ctx->opcode) != 0))
7487953d 5316 gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
76a66253
JM
5317}
5318
5319/* srq */
99e300ef 5320static void gen_srq(DisasContext *ctx)
76a66253 5321{
7487953d
AJ
5322 int l1 = gen_new_label();
5323 TCGv t0 = tcg_temp_new();
5324 TCGv t1 = tcg_temp_new();
5325 tcg_gen_andi_tl(t1, cpu_gpr[rB(ctx->opcode)], 0x1F);
5326 tcg_gen_shr_tl(t0, cpu_gpr[rS(ctx->opcode)], t1);
5327 tcg_gen_subfi_tl(t1, 32, t1);
5328 tcg_gen_shl_tl(t1, cpu_gpr[rS(ctx->opcode)], t1);
5329 tcg_gen_or_tl(t1, t0, t1);
5330 gen_store_spr(SPR_MQ, t1);
5331 tcg_gen_andi_tl(t1, cpu_gpr[rB(ctx->opcode)], 0x20);
5332 tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], t0);
5333 tcg_gen_brcondi_tl(TCG_COND_EQ, t0, 0, l1);
5334 tcg_gen_movi_tl(cpu_gpr[rA(ctx->opcode)], 0);
5335 gen_set_label(l1);
5336 tcg_temp_free(t0);
5337 tcg_temp_free(t1);
76a66253 5338 if (unlikely(Rc(ctx->opcode) != 0))
7487953d 5339 gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
76a66253
JM
5340}
5341
5342/* PowerPC 602 specific instructions */
99e300ef 5343
54623277 5344/* dsa */
99e300ef 5345static void gen_dsa(DisasContext *ctx)
76a66253
JM
5346{
5347 /* XXX: TODO */
e06fcd75 5348 gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL);
76a66253
JM
5349}
5350
5351/* esa */
99e300ef 5352static void gen_esa(DisasContext *ctx)
76a66253
JM
5353{
5354 /* XXX: TODO */
e06fcd75 5355 gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL);
76a66253
JM
5356}
5357
5358/* mfrom */
99e300ef 5359static void gen_mfrom(DisasContext *ctx)
76a66253
JM
5360{
5361#if defined(CONFIG_USER_ONLY)
e06fcd75 5362 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
76a66253 5363#else
76db3ba4 5364 if (unlikely(!ctx->mem_idx)) {
e06fcd75 5365 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
76a66253
JM
5366 return;
5367 }
cf02a65c 5368 gen_helper_602_mfrom(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)]);
76a66253
JM
5369#endif
5370}
5371
5372/* 602 - 603 - G2 TLB management */
e8eaa2c0 5373
54623277 5374/* tlbld */
e8eaa2c0 5375static void gen_tlbld_6xx(DisasContext *ctx)
76a66253
JM
5376{
5377#if defined(CONFIG_USER_ONLY)
e06fcd75 5378 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
76a66253 5379#else
76db3ba4 5380 if (unlikely(!ctx->mem_idx)) {
e06fcd75 5381 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
76a66253
JM
5382 return;
5383 }
c6c7cf05 5384 gen_helper_6xx_tlbd(cpu_env, cpu_gpr[rB(ctx->opcode)]);
76a66253
JM
5385#endif
5386}
5387
5388/* tlbli */
e8eaa2c0 5389static void gen_tlbli_6xx(DisasContext *ctx)
76a66253
JM
5390{
5391#if defined(CONFIG_USER_ONLY)
e06fcd75 5392 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
76a66253 5393#else
76db3ba4 5394 if (unlikely(!ctx->mem_idx)) {
e06fcd75 5395 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
76a66253
JM
5396 return;
5397 }
c6c7cf05 5398 gen_helper_6xx_tlbi(cpu_env, cpu_gpr[rB(ctx->opcode)]);
76a66253
JM
5399#endif
5400}
5401
7dbe11ac 5402/* 74xx TLB management */
e8eaa2c0 5403
54623277 5404/* tlbld */
e8eaa2c0 5405static void gen_tlbld_74xx(DisasContext *ctx)
7dbe11ac
JM
5406{
5407#if defined(CONFIG_USER_ONLY)
e06fcd75 5408 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
7dbe11ac 5409#else
76db3ba4 5410 if (unlikely(!ctx->mem_idx)) {
e06fcd75 5411 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
7dbe11ac
JM
5412 return;
5413 }
c6c7cf05 5414 gen_helper_74xx_tlbd(cpu_env, cpu_gpr[rB(ctx->opcode)]);
7dbe11ac
JM
5415#endif
5416}
5417
5418/* tlbli */
e8eaa2c0 5419static void gen_tlbli_74xx(DisasContext *ctx)
7dbe11ac
JM
5420{
5421#if defined(CONFIG_USER_ONLY)
e06fcd75 5422 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
7dbe11ac 5423#else
76db3ba4 5424 if (unlikely(!ctx->mem_idx)) {
e06fcd75 5425 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
7dbe11ac
JM
5426 return;
5427 }
c6c7cf05 5428 gen_helper_74xx_tlbi(cpu_env, cpu_gpr[rB(ctx->opcode)]);
7dbe11ac
JM
5429#endif
5430}
5431
76a66253 5432/* POWER instructions not in PowerPC 601 */
99e300ef 5433
54623277 5434/* clf */
99e300ef 5435static void gen_clf(DisasContext *ctx)
76a66253
JM
5436{
5437 /* Cache line flush: implemented as no-op */
5438}
5439
5440/* cli */
99e300ef 5441static void gen_cli(DisasContext *ctx)
76a66253 5442{
7f75ffd3 5443 /* Cache line invalidate: privileged and treated as no-op */
76a66253 5444#if defined(CONFIG_USER_ONLY)
e06fcd75 5445 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
76a66253 5446#else
76db3ba4 5447 if (unlikely(!ctx->mem_idx)) {
e06fcd75 5448 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
76a66253
JM
5449 return;
5450 }
5451#endif
5452}
5453
5454/* dclst */
99e300ef 5455static void gen_dclst(DisasContext *ctx)
76a66253
JM
5456{
5457 /* Data cache line store: treated as no-op */
5458}
5459
99e300ef 5460static void gen_mfsri(DisasContext *ctx)
76a66253
JM
5461{
5462#if defined(CONFIG_USER_ONLY)
e06fcd75 5463 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
76a66253 5464#else
74d37793
AJ
5465 int ra = rA(ctx->opcode);
5466 int rd = rD(ctx->opcode);
5467 TCGv t0;
76db3ba4 5468 if (unlikely(!ctx->mem_idx)) {
e06fcd75 5469 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
76a66253
JM
5470 return;
5471 }
74d37793 5472 t0 = tcg_temp_new();
76db3ba4 5473 gen_addr_reg_index(ctx, t0);
74d37793
AJ
5474 tcg_gen_shri_tl(t0, t0, 28);
5475 tcg_gen_andi_tl(t0, t0, 0xF);
c6c7cf05 5476 gen_helper_load_sr(cpu_gpr[rd], cpu_env, t0);
74d37793 5477 tcg_temp_free(t0);
76a66253 5478 if (ra != 0 && ra != rd)
74d37793 5479 tcg_gen_mov_tl(cpu_gpr[ra], cpu_gpr[rd]);
76a66253
JM
5480#endif
5481}
5482
99e300ef 5483static void gen_rac(DisasContext *ctx)
76a66253
JM
5484{
5485#if defined(CONFIG_USER_ONLY)
e06fcd75 5486 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
76a66253 5487#else
22e0e173 5488 TCGv t0;
76db3ba4 5489 if (unlikely(!ctx->mem_idx)) {
e06fcd75 5490 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
76a66253
JM
5491 return;
5492 }
22e0e173 5493 t0 = tcg_temp_new();
76db3ba4 5494 gen_addr_reg_index(ctx, t0);
c6c7cf05 5495 gen_helper_rac(cpu_gpr[rD(ctx->opcode)], cpu_env, t0);
22e0e173 5496 tcg_temp_free(t0);
76a66253
JM
5497#endif
5498}
5499
99e300ef 5500static void gen_rfsvc(DisasContext *ctx)
76a66253
JM
5501{
5502#if defined(CONFIG_USER_ONLY)
e06fcd75 5503 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
76a66253 5504#else
76db3ba4 5505 if (unlikely(!ctx->mem_idx)) {
e06fcd75 5506 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
76a66253
JM
5507 return;
5508 }
e5f17ac6 5509 gen_helper_rfsvc(cpu_env);
e06fcd75 5510 gen_sync_exception(ctx);
76a66253
JM
5511#endif
5512}
5513
5514/* svc is not implemented for now */
5515
5516/* POWER2 specific instructions */
5517/* Quad manipulation (load/store two floats at a time) */
76a66253
JM
5518
5519/* lfq */
99e300ef 5520static void gen_lfq(DisasContext *ctx)
76a66253 5521{
01a4afeb 5522 int rd = rD(ctx->opcode);
76db3ba4
AJ
5523 TCGv t0;
5524 gen_set_access_type(ctx, ACCESS_FLOAT);
5525 t0 = tcg_temp_new();
5526 gen_addr_imm_index(ctx, t0, 0);
5527 gen_qemu_ld64(ctx, cpu_fpr[rd], t0);
5528 gen_addr_add(ctx, t0, t0, 8);
5529 gen_qemu_ld64(ctx, cpu_fpr[(rd + 1) % 32], t0);
01a4afeb 5530 tcg_temp_free(t0);
76a66253
JM
5531}
5532
5533/* lfqu */
99e300ef 5534static void gen_lfqu(DisasContext *ctx)
76a66253
JM
5535{
5536 int ra = rA(ctx->opcode);
01a4afeb 5537 int rd = rD(ctx->opcode);
76db3ba4
AJ
5538 TCGv t0, t1;
5539 gen_set_access_type(ctx, ACCESS_FLOAT);
5540 t0 = tcg_temp_new();
5541 t1 = tcg_temp_new();
5542 gen_addr_imm_index(ctx, t0, 0);
5543 gen_qemu_ld64(ctx, cpu_fpr[rd], t0);
5544 gen_addr_add(ctx, t1, t0, 8);
5545 gen_qemu_ld64(ctx, cpu_fpr[(rd + 1) % 32], t1);
76a66253 5546 if (ra != 0)
01a4afeb
AJ
5547 tcg_gen_mov_tl(cpu_gpr[ra], t0);
5548 tcg_temp_free(t0);
5549 tcg_temp_free(t1);
76a66253
JM
5550}
5551
5552/* lfqux */
99e300ef 5553static void gen_lfqux(DisasContext *ctx)
76a66253
JM
5554{
5555 int ra = rA(ctx->opcode);
01a4afeb 5556 int rd = rD(ctx->opcode);
76db3ba4
AJ
5557 gen_set_access_type(ctx, ACCESS_FLOAT);
5558 TCGv t0, t1;
5559 t0 = tcg_temp_new();
5560 gen_addr_reg_index(ctx, t0);
5561 gen_qemu_ld64(ctx, cpu_fpr[rd], t0);
5562 t1 = tcg_temp_new();
5563 gen_addr_add(ctx, t1, t0, 8);
5564 gen_qemu_ld64(ctx, cpu_fpr[(rd + 1) % 32], t1);
5565 tcg_temp_free(t1);
76a66253 5566 if (ra != 0)
01a4afeb
AJ
5567 tcg_gen_mov_tl(cpu_gpr[ra], t0);
5568 tcg_temp_free(t0);
76a66253
JM
5569}
5570
5571/* lfqx */
99e300ef 5572static void gen_lfqx(DisasContext *ctx)
76a66253 5573{
01a4afeb 5574 int rd = rD(ctx->opcode);
76db3ba4
AJ
5575 TCGv t0;
5576 gen_set_access_type(ctx, ACCESS_FLOAT);
5577 t0 = tcg_temp_new();
5578 gen_addr_reg_index(ctx, t0);
5579 gen_qemu_ld64(ctx, cpu_fpr[rd], t0);
5580 gen_addr_add(ctx, t0, t0, 8);
5581 gen_qemu_ld64(ctx, cpu_fpr[(rd + 1) % 32], t0);
01a4afeb 5582 tcg_temp_free(t0);
76a66253
JM
5583}
5584
5585/* stfq */
99e300ef 5586static void gen_stfq(DisasContext *ctx)
76a66253 5587{
01a4afeb 5588 int rd = rD(ctx->opcode);
76db3ba4
AJ
5589 TCGv t0;
5590 gen_set_access_type(ctx, ACCESS_FLOAT);
5591 t0 = tcg_temp_new();
5592 gen_addr_imm_index(ctx, t0, 0);
5593 gen_qemu_st64(ctx, cpu_fpr[rd], t0);
5594 gen_addr_add(ctx, t0, t0, 8);
5595 gen_qemu_st64(ctx, cpu_fpr[(rd + 1) % 32], t0);
01a4afeb 5596 tcg_temp_free(t0);
76a66253
JM
5597}
5598
5599/* stfqu */
99e300ef 5600static void gen_stfqu(DisasContext *ctx)
76a66253
JM
5601{
5602 int ra = rA(ctx->opcode);
01a4afeb 5603 int rd = rD(ctx->opcode);
76db3ba4
AJ
5604 TCGv t0, t1;
5605 gen_set_access_type(ctx, ACCESS_FLOAT);
5606 t0 = tcg_temp_new();
5607 gen_addr_imm_index(ctx, t0, 0);
5608 gen_qemu_st64(ctx, cpu_fpr[rd], t0);
5609 t1 = tcg_temp_new();
5610 gen_addr_add(ctx, t1, t0, 8);
5611 gen_qemu_st64(ctx, cpu_fpr[(rd + 1) % 32], t1);
5612 tcg_temp_free(t1);
76a66253 5613 if (ra != 0)
01a4afeb
AJ
5614 tcg_gen_mov_tl(cpu_gpr[ra], t0);
5615 tcg_temp_free(t0);
76a66253
JM
5616}
5617
5618/* stfqux */
99e300ef 5619static void gen_stfqux(DisasContext *ctx)
76a66253
JM
5620{
5621 int ra = rA(ctx->opcode);
01a4afeb 5622 int rd = rD(ctx->opcode);
76db3ba4
AJ
5623 TCGv t0, t1;
5624 gen_set_access_type(ctx, ACCESS_FLOAT);
5625 t0 = tcg_temp_new();
5626 gen_addr_reg_index(ctx, t0);
5627 gen_qemu_st64(ctx, cpu_fpr[rd], t0);
5628 t1 = tcg_temp_new();
5629 gen_addr_add(ctx, t1, t0, 8);
5630 gen_qemu_st64(ctx, cpu_fpr[(rd + 1) % 32], t1);
5631 tcg_temp_free(t1);
76a66253 5632 if (ra != 0)
01a4afeb
AJ
5633 tcg_gen_mov_tl(cpu_gpr[ra], t0);
5634 tcg_temp_free(t0);
76a66253
JM
5635}
5636
5637/* stfqx */
99e300ef 5638static void gen_stfqx(DisasContext *ctx)
76a66253 5639{
01a4afeb 5640 int rd = rD(ctx->opcode);
76db3ba4
AJ
5641 TCGv t0;
5642 gen_set_access_type(ctx, ACCESS_FLOAT);
5643 t0 = tcg_temp_new();
5644 gen_addr_reg_index(ctx, t0);
5645 gen_qemu_st64(ctx, cpu_fpr[rd], t0);
5646 gen_addr_add(ctx, t0, t0, 8);
5647 gen_qemu_st64(ctx, cpu_fpr[(rd + 1) % 32], t0);
01a4afeb 5648 tcg_temp_free(t0);
76a66253
JM
5649}
5650
5651/* BookE specific instructions */
99e300ef 5652
54623277 5653/* XXX: not implemented on 440 ? */
99e300ef 5654static void gen_mfapidi(DisasContext *ctx)
76a66253
JM
5655{
5656 /* XXX: TODO */
e06fcd75 5657 gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL);
76a66253
JM
5658}
5659
2662a059 5660/* XXX: not implemented on 440 ? */
99e300ef 5661static void gen_tlbiva(DisasContext *ctx)
76a66253
JM
5662{
5663#if defined(CONFIG_USER_ONLY)
e06fcd75 5664 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
76a66253 5665#else
74d37793 5666 TCGv t0;
76db3ba4 5667 if (unlikely(!ctx->mem_idx)) {
e06fcd75 5668 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
76a66253
JM
5669 return;
5670 }
ec72e276 5671 t0 = tcg_temp_new();
76db3ba4 5672 gen_addr_reg_index(ctx, t0);
c6c7cf05 5673 gen_helper_tlbie(cpu_env, cpu_gpr[rB(ctx->opcode)]);
74d37793 5674 tcg_temp_free(t0);
76a66253
JM
5675#endif
5676}
5677
5678/* All 405 MAC instructions are translated here */
636aa200
BS
5679static inline void gen_405_mulladd_insn(DisasContext *ctx, int opc2, int opc3,
5680 int ra, int rb, int rt, int Rc)
76a66253 5681{
182608d4
AJ
5682 TCGv t0, t1;
5683
a7812ae4
PB
5684 t0 = tcg_temp_local_new();
5685 t1 = tcg_temp_local_new();
182608d4 5686
76a66253
JM
5687 switch (opc3 & 0x0D) {
5688 case 0x05:
5689 /* macchw - macchw. - macchwo - macchwo. */
5690 /* macchws - macchws. - macchwso - macchwso. */
5691 /* nmacchw - nmacchw. - nmacchwo - nmacchwo. */
5692 /* nmacchws - nmacchws. - nmacchwso - nmacchwso. */
5693 /* mulchw - mulchw. */
182608d4
AJ
5694 tcg_gen_ext16s_tl(t0, cpu_gpr[ra]);
5695 tcg_gen_sari_tl(t1, cpu_gpr[rb], 16);
5696 tcg_gen_ext16s_tl(t1, t1);
76a66253
JM
5697 break;
5698 case 0x04:
5699 /* macchwu - macchwu. - macchwuo - macchwuo. */
5700 /* macchwsu - macchwsu. - macchwsuo - macchwsuo. */
5701 /* mulchwu - mulchwu. */
182608d4
AJ
5702 tcg_gen_ext16u_tl(t0, cpu_gpr[ra]);
5703 tcg_gen_shri_tl(t1, cpu_gpr[rb], 16);
5704 tcg_gen_ext16u_tl(t1, t1);
76a66253
JM
5705 break;
5706 case 0x01:
5707 /* machhw - machhw. - machhwo - machhwo. */
5708 /* machhws - machhws. - machhwso - machhwso. */
5709 /* nmachhw - nmachhw. - nmachhwo - nmachhwo. */
5710 /* nmachhws - nmachhws. - nmachhwso - nmachhwso. */
5711 /* mulhhw - mulhhw. */
182608d4
AJ
5712 tcg_gen_sari_tl(t0, cpu_gpr[ra], 16);
5713 tcg_gen_ext16s_tl(t0, t0);
5714 tcg_gen_sari_tl(t1, cpu_gpr[rb], 16);
5715 tcg_gen_ext16s_tl(t1, t1);
76a66253
JM
5716 break;
5717 case 0x00:
5718 /* machhwu - machhwu. - machhwuo - machhwuo. */
5719 /* machhwsu - machhwsu. - machhwsuo - machhwsuo. */
5720 /* mulhhwu - mulhhwu. */
182608d4
AJ
5721 tcg_gen_shri_tl(t0, cpu_gpr[ra], 16);
5722 tcg_gen_ext16u_tl(t0, t0);
5723 tcg_gen_shri_tl(t1, cpu_gpr[rb], 16);
5724 tcg_gen_ext16u_tl(t1, t1);
76a66253
JM
5725 break;
5726 case 0x0D:
5727 /* maclhw - maclhw. - maclhwo - maclhwo. */
5728 /* maclhws - maclhws. - maclhwso - maclhwso. */
5729 /* nmaclhw - nmaclhw. - nmaclhwo - nmaclhwo. */
5730 /* nmaclhws - nmaclhws. - nmaclhwso - nmaclhwso. */
5731 /* mullhw - mullhw. */
182608d4
AJ
5732 tcg_gen_ext16s_tl(t0, cpu_gpr[ra]);
5733 tcg_gen_ext16s_tl(t1, cpu_gpr[rb]);
76a66253
JM
5734 break;
5735 case 0x0C:
5736 /* maclhwu - maclhwu. - maclhwuo - maclhwuo. */
5737 /* maclhwsu - maclhwsu. - maclhwsuo - maclhwsuo. */
5738 /* mullhwu - mullhwu. */
182608d4
AJ
5739 tcg_gen_ext16u_tl(t0, cpu_gpr[ra]);
5740 tcg_gen_ext16u_tl(t1, cpu_gpr[rb]);
76a66253
JM
5741 break;
5742 }
76a66253 5743 if (opc2 & 0x04) {
182608d4
AJ
5744 /* (n)multiply-and-accumulate (0x0C / 0x0E) */
5745 tcg_gen_mul_tl(t1, t0, t1);
5746 if (opc2 & 0x02) {
5747 /* nmultiply-and-accumulate (0x0E) */
5748 tcg_gen_sub_tl(t0, cpu_gpr[rt], t1);
5749 } else {
5750 /* multiply-and-accumulate (0x0C) */
5751 tcg_gen_add_tl(t0, cpu_gpr[rt], t1);
5752 }
5753
5754 if (opc3 & 0x12) {
5755 /* Check overflow and/or saturate */
5756 int l1 = gen_new_label();
5757
5758 if (opc3 & 0x10) {
5759 /* Start with XER OV disabled, the most likely case */
da91a00f 5760 tcg_gen_movi_tl(cpu_ov, 0);
182608d4
AJ
5761 }
5762 if (opc3 & 0x01) {
5763 /* Signed */
5764 tcg_gen_xor_tl(t1, cpu_gpr[rt], t1);
5765 tcg_gen_brcondi_tl(TCG_COND_GE, t1, 0, l1);
5766 tcg_gen_xor_tl(t1, cpu_gpr[rt], t0);
5767 tcg_gen_brcondi_tl(TCG_COND_LT, t1, 0, l1);
bdc4e053 5768 if (opc3 & 0x02) {
182608d4
AJ
5769 /* Saturate */
5770 tcg_gen_sari_tl(t0, cpu_gpr[rt], 31);
5771 tcg_gen_xori_tl(t0, t0, 0x7fffffff);
5772 }
5773 } else {
5774 /* Unsigned */
5775 tcg_gen_brcond_tl(TCG_COND_GEU, t0, t1, l1);
bdc4e053 5776 if (opc3 & 0x02) {
182608d4
AJ
5777 /* Saturate */
5778 tcg_gen_movi_tl(t0, UINT32_MAX);
5779 }
5780 }
5781 if (opc3 & 0x10) {
5782 /* Check overflow */
da91a00f
RH
5783 tcg_gen_movi_tl(cpu_ov, 1);
5784 tcg_gen_movi_tl(cpu_so, 1);
182608d4
AJ
5785 }
5786 gen_set_label(l1);
5787 tcg_gen_mov_tl(cpu_gpr[rt], t0);
5788 }
5789 } else {
5790 tcg_gen_mul_tl(cpu_gpr[rt], t0, t1);
76a66253 5791 }
182608d4
AJ
5792 tcg_temp_free(t0);
5793 tcg_temp_free(t1);
76a66253
JM
5794 if (unlikely(Rc) != 0) {
5795 /* Update Rc0 */
182608d4 5796 gen_set_Rc0(ctx, cpu_gpr[rt]);
76a66253
JM
5797 }
5798}
5799
a750fc0b 5800#define GEN_MAC_HANDLER(name, opc2, opc3) \
99e300ef 5801static void glue(gen_, name)(DisasContext *ctx) \
76a66253
JM
5802{ \
5803 gen_405_mulladd_insn(ctx, opc2, opc3, rA(ctx->opcode), rB(ctx->opcode), \
5804 rD(ctx->opcode), Rc(ctx->opcode)); \
5805}
5806
5807/* macchw - macchw. */
a750fc0b 5808GEN_MAC_HANDLER(macchw, 0x0C, 0x05);
76a66253 5809/* macchwo - macchwo. */
a750fc0b 5810GEN_MAC_HANDLER(macchwo, 0x0C, 0x15);
76a66253 5811/* macchws - macchws. */
a750fc0b 5812GEN_MAC_HANDLER(macchws, 0x0C, 0x07);
76a66253 5813/* macchwso - macchwso. */
a750fc0b 5814GEN_MAC_HANDLER(macchwso, 0x0C, 0x17);
76a66253 5815/* macchwsu - macchwsu. */
a750fc0b 5816GEN_MAC_HANDLER(macchwsu, 0x0C, 0x06);
76a66253 5817/* macchwsuo - macchwsuo. */
a750fc0b 5818GEN_MAC_HANDLER(macchwsuo, 0x0C, 0x16);
76a66253 5819/* macchwu - macchwu. */
a750fc0b 5820GEN_MAC_HANDLER(macchwu, 0x0C, 0x04);
76a66253 5821/* macchwuo - macchwuo. */
a750fc0b 5822GEN_MAC_HANDLER(macchwuo, 0x0C, 0x14);
76a66253 5823/* machhw - machhw. */
a750fc0b 5824GEN_MAC_HANDLER(machhw, 0x0C, 0x01);
76a66253 5825/* machhwo - machhwo. */
a750fc0b 5826GEN_MAC_HANDLER(machhwo, 0x0C, 0x11);
76a66253 5827/* machhws - machhws. */
a750fc0b 5828GEN_MAC_HANDLER(machhws, 0x0C, 0x03);
76a66253 5829/* machhwso - machhwso. */
a750fc0b 5830GEN_MAC_HANDLER(machhwso, 0x0C, 0x13);
76a66253 5831/* machhwsu - machhwsu. */
a750fc0b 5832GEN_MAC_HANDLER(machhwsu, 0x0C, 0x02);
76a66253 5833/* machhwsuo - machhwsuo. */
a750fc0b 5834GEN_MAC_HANDLER(machhwsuo, 0x0C, 0x12);
76a66253 5835/* machhwu - machhwu. */
a750fc0b 5836GEN_MAC_HANDLER(machhwu, 0x0C, 0x00);
76a66253 5837/* machhwuo - machhwuo. */
a750fc0b 5838GEN_MAC_HANDLER(machhwuo, 0x0C, 0x10);
76a66253 5839/* maclhw - maclhw. */
a750fc0b 5840GEN_MAC_HANDLER(maclhw, 0x0C, 0x0D);
76a66253 5841/* maclhwo - maclhwo. */
a750fc0b 5842GEN_MAC_HANDLER(maclhwo, 0x0C, 0x1D);
76a66253 5843/* maclhws - maclhws. */
a750fc0b 5844GEN_MAC_HANDLER(maclhws, 0x0C, 0x0F);
76a66253 5845/* maclhwso - maclhwso. */
a750fc0b 5846GEN_MAC_HANDLER(maclhwso, 0x0C, 0x1F);
76a66253 5847/* maclhwu - maclhwu. */
a750fc0b 5848GEN_MAC_HANDLER(maclhwu, 0x0C, 0x0C);
76a66253 5849/* maclhwuo - maclhwuo. */
a750fc0b 5850GEN_MAC_HANDLER(maclhwuo, 0x0C, 0x1C);
76a66253 5851/* maclhwsu - maclhwsu. */
a750fc0b 5852GEN_MAC_HANDLER(maclhwsu, 0x0C, 0x0E);
76a66253 5853/* maclhwsuo - maclhwsuo. */
a750fc0b 5854GEN_MAC_HANDLER(maclhwsuo, 0x0C, 0x1E);
76a66253 5855/* nmacchw - nmacchw. */
a750fc0b 5856GEN_MAC_HANDLER(nmacchw, 0x0E, 0x05);
76a66253 5857/* nmacchwo - nmacchwo. */
a750fc0b 5858GEN_MAC_HANDLER(nmacchwo, 0x0E, 0x15);
76a66253 5859/* nmacchws - nmacchws. */
a750fc0b 5860GEN_MAC_HANDLER(nmacchws, 0x0E, 0x07);
76a66253 5861/* nmacchwso - nmacchwso. */
a750fc0b 5862GEN_MAC_HANDLER(nmacchwso, 0x0E, 0x17);
76a66253 5863/* nmachhw - nmachhw. */
a750fc0b 5864GEN_MAC_HANDLER(nmachhw, 0x0E, 0x01);
76a66253 5865/* nmachhwo - nmachhwo. */
a750fc0b 5866GEN_MAC_HANDLER(nmachhwo, 0x0E, 0x11);
76a66253 5867/* nmachhws - nmachhws. */
a750fc0b 5868GEN_MAC_HANDLER(nmachhws, 0x0E, 0x03);
76a66253 5869/* nmachhwso - nmachhwso. */
a750fc0b 5870GEN_MAC_HANDLER(nmachhwso, 0x0E, 0x13);
76a66253 5871/* nmaclhw - nmaclhw. */
a750fc0b 5872GEN_MAC_HANDLER(nmaclhw, 0x0E, 0x0D);
76a66253 5873/* nmaclhwo - nmaclhwo. */
a750fc0b 5874GEN_MAC_HANDLER(nmaclhwo, 0x0E, 0x1D);
76a66253 5875/* nmaclhws - nmaclhws. */
a750fc0b 5876GEN_MAC_HANDLER(nmaclhws, 0x0E, 0x0F);
76a66253 5877/* nmaclhwso - nmaclhwso. */
a750fc0b 5878GEN_MAC_HANDLER(nmaclhwso, 0x0E, 0x1F);
76a66253
JM
5879
5880/* mulchw - mulchw. */
a750fc0b 5881GEN_MAC_HANDLER(mulchw, 0x08, 0x05);
76a66253 5882/* mulchwu - mulchwu. */
a750fc0b 5883GEN_MAC_HANDLER(mulchwu, 0x08, 0x04);
76a66253 5884/* mulhhw - mulhhw. */
a750fc0b 5885GEN_MAC_HANDLER(mulhhw, 0x08, 0x01);
76a66253 5886/* mulhhwu - mulhhwu. */
a750fc0b 5887GEN_MAC_HANDLER(mulhhwu, 0x08, 0x00);
76a66253 5888/* mullhw - mullhw. */
a750fc0b 5889GEN_MAC_HANDLER(mullhw, 0x08, 0x0D);
76a66253 5890/* mullhwu - mullhwu. */
a750fc0b 5891GEN_MAC_HANDLER(mullhwu, 0x08, 0x0C);
76a66253
JM
5892
5893/* mfdcr */
99e300ef 5894static void gen_mfdcr(DisasContext *ctx)
76a66253
JM
5895{
5896#if defined(CONFIG_USER_ONLY)
e06fcd75 5897 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
76a66253 5898#else
06dca6a7 5899 TCGv dcrn;
76db3ba4 5900 if (unlikely(!ctx->mem_idx)) {
e06fcd75 5901 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
76a66253
JM
5902 return;
5903 }
06dca6a7
AJ
5904 /* NIP cannot be restored if the memory exception comes from an helper */
5905 gen_update_nip(ctx, ctx->nip - 4);
5906 dcrn = tcg_const_tl(SPR(ctx->opcode));
d0f1562d 5907 gen_helper_load_dcr(cpu_gpr[rD(ctx->opcode)], cpu_env, dcrn);
06dca6a7 5908 tcg_temp_free(dcrn);
76a66253
JM
5909#endif
5910}
5911
5912/* mtdcr */
99e300ef 5913static void gen_mtdcr(DisasContext *ctx)
76a66253
JM
5914{
5915#if defined(CONFIG_USER_ONLY)
e06fcd75 5916 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
76a66253 5917#else
06dca6a7 5918 TCGv dcrn;
76db3ba4 5919 if (unlikely(!ctx->mem_idx)) {
e06fcd75 5920 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
76a66253
JM
5921 return;
5922 }
06dca6a7
AJ
5923 /* NIP cannot be restored if the memory exception comes from an helper */
5924 gen_update_nip(ctx, ctx->nip - 4);
5925 dcrn = tcg_const_tl(SPR(ctx->opcode));
d0f1562d 5926 gen_helper_store_dcr(cpu_env, dcrn, cpu_gpr[rS(ctx->opcode)]);
06dca6a7 5927 tcg_temp_free(dcrn);
a42bd6cc
JM
5928#endif
5929}
5930
5931/* mfdcrx */
2662a059 5932/* XXX: not implemented on 440 ? */
99e300ef 5933static void gen_mfdcrx(DisasContext *ctx)
a42bd6cc
JM
5934{
5935#if defined(CONFIG_USER_ONLY)
e06fcd75 5936 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
a42bd6cc 5937#else
76db3ba4 5938 if (unlikely(!ctx->mem_idx)) {
e06fcd75 5939 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
a42bd6cc
JM
5940 return;
5941 }
06dca6a7
AJ
5942 /* NIP cannot be restored if the memory exception comes from an helper */
5943 gen_update_nip(ctx, ctx->nip - 4);
d0f1562d
BS
5944 gen_helper_load_dcr(cpu_gpr[rD(ctx->opcode)], cpu_env,
5945 cpu_gpr[rA(ctx->opcode)]);
a750fc0b 5946 /* Note: Rc update flag set leads to undefined state of Rc0 */
a42bd6cc
JM
5947#endif
5948}
5949
5950/* mtdcrx */
2662a059 5951/* XXX: not implemented on 440 ? */
99e300ef 5952static void gen_mtdcrx(DisasContext *ctx)
a42bd6cc
JM
5953{
5954#if defined(CONFIG_USER_ONLY)
e06fcd75 5955 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
a42bd6cc 5956#else
76db3ba4 5957 if (unlikely(!ctx->mem_idx)) {
e06fcd75 5958 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
a42bd6cc
JM
5959 return;
5960 }
06dca6a7
AJ
5961 /* NIP cannot be restored if the memory exception comes from an helper */
5962 gen_update_nip(ctx, ctx->nip - 4);
d0f1562d
BS
5963 gen_helper_store_dcr(cpu_env, cpu_gpr[rA(ctx->opcode)],
5964 cpu_gpr[rS(ctx->opcode)]);
a750fc0b 5965 /* Note: Rc update flag set leads to undefined state of Rc0 */
76a66253
JM
5966#endif
5967}
5968
a750fc0b 5969/* mfdcrux (PPC 460) : user-mode access to DCR */
99e300ef 5970static void gen_mfdcrux(DisasContext *ctx)
a750fc0b 5971{
06dca6a7
AJ
5972 /* NIP cannot be restored if the memory exception comes from an helper */
5973 gen_update_nip(ctx, ctx->nip - 4);
d0f1562d
BS
5974 gen_helper_load_dcr(cpu_gpr[rD(ctx->opcode)], cpu_env,
5975 cpu_gpr[rA(ctx->opcode)]);
a750fc0b
JM
5976 /* Note: Rc update flag set leads to undefined state of Rc0 */
5977}
5978
5979/* mtdcrux (PPC 460) : user-mode access to DCR */
99e300ef 5980static void gen_mtdcrux(DisasContext *ctx)
a750fc0b 5981{
06dca6a7
AJ
5982 /* NIP cannot be restored if the memory exception comes from an helper */
5983 gen_update_nip(ctx, ctx->nip - 4);
975e5463 5984 gen_helper_store_dcr(cpu_env, cpu_gpr[rA(ctx->opcode)],
d0f1562d 5985 cpu_gpr[rS(ctx->opcode)]);
a750fc0b
JM
5986 /* Note: Rc update flag set leads to undefined state of Rc0 */
5987}
5988
76a66253 5989/* dccci */
99e300ef 5990static void gen_dccci(DisasContext *ctx)
76a66253
JM
5991{
5992#if defined(CONFIG_USER_ONLY)
e06fcd75 5993 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
76a66253 5994#else
76db3ba4 5995 if (unlikely(!ctx->mem_idx)) {
e06fcd75 5996 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
76a66253
JM
5997 return;
5998 }
5999 /* interpreted as no-op */
6000#endif
6001}
6002
6003/* dcread */
99e300ef 6004static void gen_dcread(DisasContext *ctx)
76a66253
JM
6005{
6006#if defined(CONFIG_USER_ONLY)
e06fcd75 6007 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
76a66253 6008#else
b61f2753 6009 TCGv EA, val;
76db3ba4 6010 if (unlikely(!ctx->mem_idx)) {
e06fcd75 6011 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
76a66253
JM
6012 return;
6013 }
76db3ba4 6014 gen_set_access_type(ctx, ACCESS_CACHE);
a7812ae4 6015 EA = tcg_temp_new();
76db3ba4 6016 gen_addr_reg_index(ctx, EA);
a7812ae4 6017 val = tcg_temp_new();
76db3ba4 6018 gen_qemu_ld32u(ctx, val, EA);
b61f2753
AJ
6019 tcg_temp_free(val);
6020 tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], EA);
6021 tcg_temp_free(EA);
76a66253
JM
6022#endif
6023}
6024
6025/* icbt */
e8eaa2c0 6026static void gen_icbt_40x(DisasContext *ctx)
76a66253
JM
6027{
6028 /* interpreted as no-op */
6029 /* XXX: specification say this is treated as a load by the MMU
6030 * but does not generate any exception
6031 */
6032}
6033
6034/* iccci */
99e300ef 6035static void gen_iccci(DisasContext *ctx)
76a66253
JM
6036{
6037#if defined(CONFIG_USER_ONLY)
e06fcd75 6038 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
76a66253 6039#else
76db3ba4 6040 if (unlikely(!ctx->mem_idx)) {
e06fcd75 6041 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
76a66253
JM
6042 return;
6043 }
6044 /* interpreted as no-op */
6045#endif
6046}
6047
6048/* icread */
99e300ef 6049static void gen_icread(DisasContext *ctx)
76a66253
JM
6050{
6051#if defined(CONFIG_USER_ONLY)
e06fcd75 6052 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
76a66253 6053#else
76db3ba4 6054 if (unlikely(!ctx->mem_idx)) {
e06fcd75 6055 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
76a66253
JM
6056 return;
6057 }
6058 /* interpreted as no-op */
6059#endif
6060}
6061
76db3ba4 6062/* rfci (mem_idx only) */
e8eaa2c0 6063static void gen_rfci_40x(DisasContext *ctx)
a42bd6cc
JM
6064{
6065#if defined(CONFIG_USER_ONLY)
e06fcd75 6066 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
a42bd6cc 6067#else
76db3ba4 6068 if (unlikely(!ctx->mem_idx)) {
e06fcd75 6069 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
a42bd6cc
JM
6070 return;
6071 }
6072 /* Restore CPU state */
e5f17ac6 6073 gen_helper_40x_rfci(cpu_env);
e06fcd75 6074 gen_sync_exception(ctx);
a42bd6cc
JM
6075#endif
6076}
6077
99e300ef 6078static void gen_rfci(DisasContext *ctx)
a42bd6cc
JM
6079{
6080#if defined(CONFIG_USER_ONLY)
e06fcd75 6081 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
a42bd6cc 6082#else
76db3ba4 6083 if (unlikely(!ctx->mem_idx)) {
e06fcd75 6084 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
a42bd6cc
JM
6085 return;
6086 }
6087 /* Restore CPU state */
e5f17ac6 6088 gen_helper_rfci(cpu_env);
e06fcd75 6089 gen_sync_exception(ctx);
a42bd6cc
JM
6090#endif
6091}
6092
6093/* BookE specific */
99e300ef 6094
54623277 6095/* XXX: not implemented on 440 ? */
99e300ef 6096static void gen_rfdi(DisasContext *ctx)
76a66253
JM
6097{
6098#if defined(CONFIG_USER_ONLY)
e06fcd75 6099 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
76a66253 6100#else
76db3ba4 6101 if (unlikely(!ctx->mem_idx)) {
e06fcd75 6102 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
76a66253
JM
6103 return;
6104 }
6105 /* Restore CPU state */
e5f17ac6 6106 gen_helper_rfdi(cpu_env);
e06fcd75 6107 gen_sync_exception(ctx);
76a66253
JM
6108#endif
6109}
6110
2662a059 6111/* XXX: not implemented on 440 ? */
99e300ef 6112static void gen_rfmci(DisasContext *ctx)
a42bd6cc
JM
6113{
6114#if defined(CONFIG_USER_ONLY)
e06fcd75 6115 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
a42bd6cc 6116#else
76db3ba4 6117 if (unlikely(!ctx->mem_idx)) {
e06fcd75 6118 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
a42bd6cc
JM
6119 return;
6120 }
6121 /* Restore CPU state */
e5f17ac6 6122 gen_helper_rfmci(cpu_env);
e06fcd75 6123 gen_sync_exception(ctx);
a42bd6cc
JM
6124#endif
6125}
5eb7995e 6126
d9bce9d9 6127/* TLB management - PowerPC 405 implementation */
e8eaa2c0 6128
54623277 6129/* tlbre */
e8eaa2c0 6130static void gen_tlbre_40x(DisasContext *ctx)
76a66253
JM
6131{
6132#if defined(CONFIG_USER_ONLY)
e06fcd75 6133 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
76a66253 6134#else
76db3ba4 6135 if (unlikely(!ctx->mem_idx)) {
e06fcd75 6136 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
76a66253
JM
6137 return;
6138 }
6139 switch (rB(ctx->opcode)) {
6140 case 0:
c6c7cf05
BS
6141 gen_helper_4xx_tlbre_hi(cpu_gpr[rD(ctx->opcode)], cpu_env,
6142 cpu_gpr[rA(ctx->opcode)]);
76a66253
JM
6143 break;
6144 case 1:
c6c7cf05
BS
6145 gen_helper_4xx_tlbre_lo(cpu_gpr[rD(ctx->opcode)], cpu_env,
6146 cpu_gpr[rA(ctx->opcode)]);
76a66253
JM
6147 break;
6148 default:
e06fcd75 6149 gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL);
76a66253 6150 break;
9a64fbe4 6151 }
76a66253
JM
6152#endif
6153}
6154
d9bce9d9 6155/* tlbsx - tlbsx. */
e8eaa2c0 6156static void gen_tlbsx_40x(DisasContext *ctx)
76a66253
JM
6157{
6158#if defined(CONFIG_USER_ONLY)
e06fcd75 6159 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
76a66253 6160#else
74d37793 6161 TCGv t0;
76db3ba4 6162 if (unlikely(!ctx->mem_idx)) {
e06fcd75 6163 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
76a66253
JM
6164 return;
6165 }
74d37793 6166 t0 = tcg_temp_new();
76db3ba4 6167 gen_addr_reg_index(ctx, t0);
c6c7cf05 6168 gen_helper_4xx_tlbsx(cpu_gpr[rD(ctx->opcode)], cpu_env, t0);
74d37793
AJ
6169 tcg_temp_free(t0);
6170 if (Rc(ctx->opcode)) {
6171 int l1 = gen_new_label();
da91a00f 6172 tcg_gen_trunc_tl_i32(cpu_crf[0], cpu_so);
74d37793
AJ
6173 tcg_gen_brcondi_tl(TCG_COND_EQ, cpu_gpr[rD(ctx->opcode)], -1, l1);
6174 tcg_gen_ori_i32(cpu_crf[0], cpu_crf[0], 0x02);
6175 gen_set_label(l1);
6176 }
76a66253 6177#endif
79aceca5
FB
6178}
6179
76a66253 6180/* tlbwe */
e8eaa2c0 6181static void gen_tlbwe_40x(DisasContext *ctx)
79aceca5 6182{
76a66253 6183#if defined(CONFIG_USER_ONLY)
e06fcd75 6184 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
76a66253 6185#else
76db3ba4 6186 if (unlikely(!ctx->mem_idx)) {
e06fcd75 6187 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
76a66253
JM
6188 return;
6189 }
6190 switch (rB(ctx->opcode)) {
6191 case 0:
c6c7cf05
BS
6192 gen_helper_4xx_tlbwe_hi(cpu_env, cpu_gpr[rA(ctx->opcode)],
6193 cpu_gpr[rS(ctx->opcode)]);
76a66253
JM
6194 break;
6195 case 1:
c6c7cf05
BS
6196 gen_helper_4xx_tlbwe_lo(cpu_env, cpu_gpr[rA(ctx->opcode)],
6197 cpu_gpr[rS(ctx->opcode)]);
76a66253
JM
6198 break;
6199 default:
e06fcd75 6200 gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL);
76a66253 6201 break;
9a64fbe4 6202 }
76a66253
JM
6203#endif
6204}
6205
a4bb6c3e 6206/* TLB management - PowerPC 440 implementation */
e8eaa2c0 6207
54623277 6208/* tlbre */
e8eaa2c0 6209static void gen_tlbre_440(DisasContext *ctx)
5eb7995e
JM
6210{
6211#if defined(CONFIG_USER_ONLY)
e06fcd75 6212 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
5eb7995e 6213#else
76db3ba4 6214 if (unlikely(!ctx->mem_idx)) {
e06fcd75 6215 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
5eb7995e
JM
6216 return;
6217 }
6218 switch (rB(ctx->opcode)) {
6219 case 0:
5eb7995e 6220 case 1:
5eb7995e 6221 case 2:
74d37793
AJ
6222 {
6223 TCGv_i32 t0 = tcg_const_i32(rB(ctx->opcode));
c6c7cf05
BS
6224 gen_helper_440_tlbre(cpu_gpr[rD(ctx->opcode)], cpu_env,
6225 t0, cpu_gpr[rA(ctx->opcode)]);
74d37793
AJ
6226 tcg_temp_free_i32(t0);
6227 }
5eb7995e
JM
6228 break;
6229 default:
e06fcd75 6230 gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL);
5eb7995e
JM
6231 break;
6232 }
6233#endif
6234}
6235
6236/* tlbsx - tlbsx. */
e8eaa2c0 6237static void gen_tlbsx_440(DisasContext *ctx)
5eb7995e
JM
6238{
6239#if defined(CONFIG_USER_ONLY)
e06fcd75 6240 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
5eb7995e 6241#else
74d37793 6242 TCGv t0;
76db3ba4 6243 if (unlikely(!ctx->mem_idx)) {
e06fcd75 6244 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
5eb7995e
JM
6245 return;
6246 }
74d37793 6247 t0 = tcg_temp_new();
76db3ba4 6248 gen_addr_reg_index(ctx, t0);
c6c7cf05 6249 gen_helper_440_tlbsx(cpu_gpr[rD(ctx->opcode)], cpu_env, t0);
74d37793
AJ
6250 tcg_temp_free(t0);
6251 if (Rc(ctx->opcode)) {
6252 int l1 = gen_new_label();
da91a00f 6253 tcg_gen_trunc_tl_i32(cpu_crf[0], cpu_so);
74d37793
AJ
6254 tcg_gen_brcondi_tl(TCG_COND_EQ, cpu_gpr[rD(ctx->opcode)], -1, l1);
6255 tcg_gen_ori_i32(cpu_crf[0], cpu_crf[0], 0x02);
6256 gen_set_label(l1);
6257 }
5eb7995e
JM
6258#endif
6259}
6260
6261/* tlbwe */
e8eaa2c0 6262static void gen_tlbwe_440(DisasContext *ctx)
5eb7995e
JM
6263{
6264#if defined(CONFIG_USER_ONLY)
e06fcd75 6265 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
5eb7995e 6266#else
76db3ba4 6267 if (unlikely(!ctx->mem_idx)) {
e06fcd75 6268 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
5eb7995e
JM
6269 return;
6270 }
6271 switch (rB(ctx->opcode)) {
6272 case 0:
5eb7995e 6273 case 1:
5eb7995e 6274 case 2:
74d37793
AJ
6275 {
6276 TCGv_i32 t0 = tcg_const_i32(rB(ctx->opcode));
c6c7cf05
BS
6277 gen_helper_440_tlbwe(cpu_env, t0, cpu_gpr[rA(ctx->opcode)],
6278 cpu_gpr[rS(ctx->opcode)]);
74d37793
AJ
6279 tcg_temp_free_i32(t0);
6280 }
5eb7995e
JM
6281 break;
6282 default:
e06fcd75 6283 gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL);
5eb7995e
JM
6284 break;
6285 }
6286#endif
6287}
6288
01662f3e
AG
6289/* TLB management - PowerPC BookE 2.06 implementation */
6290
6291/* tlbre */
6292static void gen_tlbre_booke206(DisasContext *ctx)
6293{
6294#if defined(CONFIG_USER_ONLY)
6295 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
6296#else
6297 if (unlikely(!ctx->mem_idx)) {
6298 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
6299 return;
6300 }
6301
c6c7cf05 6302 gen_helper_booke206_tlbre(cpu_env);
01662f3e
AG
6303#endif
6304}
6305
6306/* tlbsx - tlbsx. */
6307static void gen_tlbsx_booke206(DisasContext *ctx)
6308{
6309#if defined(CONFIG_USER_ONLY)
6310 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
6311#else
6312 TCGv t0;
6313 if (unlikely(!ctx->mem_idx)) {
6314 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
6315 return;
6316 }
6317
6318 if (rA(ctx->opcode)) {
6319 t0 = tcg_temp_new();
6320 tcg_gen_mov_tl(t0, cpu_gpr[rD(ctx->opcode)]);
6321 } else {
6322 t0 = tcg_const_tl(0);
6323 }
6324
6325 tcg_gen_add_tl(t0, t0, cpu_gpr[rB(ctx->opcode)]);
c6c7cf05 6326 gen_helper_booke206_tlbsx(cpu_env, t0);
01662f3e
AG
6327#endif
6328}
6329
6330/* tlbwe */
6331static void gen_tlbwe_booke206(DisasContext *ctx)
6332{
6333#if defined(CONFIG_USER_ONLY)
6334 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
6335#else
6336 if (unlikely(!ctx->mem_idx)) {
6337 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
6338 return;
6339 }
3f162d11 6340 gen_update_nip(ctx, ctx->nip - 4);
c6c7cf05 6341 gen_helper_booke206_tlbwe(cpu_env);
01662f3e
AG
6342#endif
6343}
6344
6345static void gen_tlbivax_booke206(DisasContext *ctx)
6346{
6347#if defined(CONFIG_USER_ONLY)
6348 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
6349#else
6350 TCGv t0;
6351 if (unlikely(!ctx->mem_idx)) {
6352 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
6353 return;
6354 }
6355
6356 t0 = tcg_temp_new();
6357 gen_addr_reg_index(ctx, t0);
6358
c6c7cf05 6359 gen_helper_booke206_tlbivax(cpu_env, t0);
01662f3e
AG
6360#endif
6361}
6362
6d3db821
AG
6363static void gen_tlbilx_booke206(DisasContext *ctx)
6364{
6365#if defined(CONFIG_USER_ONLY)
6366 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
6367#else
6368 TCGv t0;
6369 if (unlikely(!ctx->mem_idx)) {
6370 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
6371 return;
6372 }
6373
6374 t0 = tcg_temp_new();
6375 gen_addr_reg_index(ctx, t0);
6376
6377 switch((ctx->opcode >> 21) & 0x3) {
6378 case 0:
c6c7cf05 6379 gen_helper_booke206_tlbilx0(cpu_env, t0);
6d3db821
AG
6380 break;
6381 case 1:
c6c7cf05 6382 gen_helper_booke206_tlbilx1(cpu_env, t0);
6d3db821
AG
6383 break;
6384 case 3:
c6c7cf05 6385 gen_helper_booke206_tlbilx3(cpu_env, t0);
6d3db821
AG
6386 break;
6387 default:
6388 gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL);
6389 break;
6390 }
6391
6392 tcg_temp_free(t0);
6393#endif
6394}
6395
01662f3e 6396
76a66253 6397/* wrtee */
99e300ef 6398static void gen_wrtee(DisasContext *ctx)
76a66253
JM
6399{
6400#if defined(CONFIG_USER_ONLY)
e06fcd75 6401 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
76a66253 6402#else
6527f6ea 6403 TCGv t0;
76db3ba4 6404 if (unlikely(!ctx->mem_idx)) {
e06fcd75 6405 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
76a66253
JM
6406 return;
6407 }
6527f6ea
AJ
6408 t0 = tcg_temp_new();
6409 tcg_gen_andi_tl(t0, cpu_gpr[rD(ctx->opcode)], (1 << MSR_EE));
6410 tcg_gen_andi_tl(cpu_msr, cpu_msr, ~(1 << MSR_EE));
6411 tcg_gen_or_tl(cpu_msr, cpu_msr, t0);
6412 tcg_temp_free(t0);
dee96f6c
JM
6413 /* Stop translation to have a chance to raise an exception
6414 * if we just set msr_ee to 1
6415 */
e06fcd75 6416 gen_stop_exception(ctx);
76a66253
JM
6417#endif
6418}
6419
6420/* wrteei */
99e300ef 6421static void gen_wrteei(DisasContext *ctx)
76a66253
JM
6422{
6423#if defined(CONFIG_USER_ONLY)
e06fcd75 6424 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
76a66253 6425#else
76db3ba4 6426 if (unlikely(!ctx->mem_idx)) {
e06fcd75 6427 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
76a66253
JM
6428 return;
6429 }
fbe73008 6430 if (ctx->opcode & 0x00008000) {
6527f6ea
AJ
6431 tcg_gen_ori_tl(cpu_msr, cpu_msr, (1 << MSR_EE));
6432 /* Stop translation to have a chance to raise an exception */
e06fcd75 6433 gen_stop_exception(ctx);
6527f6ea 6434 } else {
1b6e5f99 6435 tcg_gen_andi_tl(cpu_msr, cpu_msr, ~(1 << MSR_EE));
6527f6ea 6436 }
76a66253
JM
6437#endif
6438}
6439
08e46e54 6440/* PowerPC 440 specific instructions */
99e300ef 6441
54623277 6442/* dlmzb */
99e300ef 6443static void gen_dlmzb(DisasContext *ctx)
76a66253 6444{
ef0d51af 6445 TCGv_i32 t0 = tcg_const_i32(Rc(ctx->opcode));
d15f74fb
BS
6446 gen_helper_dlmzb(cpu_gpr[rA(ctx->opcode)], cpu_env,
6447 cpu_gpr[rS(ctx->opcode)], cpu_gpr[rB(ctx->opcode)], t0);
ef0d51af 6448 tcg_temp_free_i32(t0);
76a66253
JM
6449}
6450
6451/* mbar replaces eieio on 440 */
99e300ef 6452static void gen_mbar(DisasContext *ctx)
76a66253
JM
6453{
6454 /* interpreted as no-op */
6455}
6456
6457/* msync replaces sync on 440 */
dcb2b9e1 6458static void gen_msync_4xx(DisasContext *ctx)
76a66253
JM
6459{
6460 /* interpreted as no-op */
6461}
6462
6463/* icbt */
e8eaa2c0 6464static void gen_icbt_440(DisasContext *ctx)
76a66253
JM
6465{
6466 /* interpreted as no-op */
6467 /* XXX: specification say this is treated as a load by the MMU
6468 * but does not generate any exception
6469 */
79aceca5
FB
6470}
6471
9e0b5cb1
AG
6472/* Embedded.Processor Control */
6473
6474static void gen_msgclr(DisasContext *ctx)
6475{
6476#if defined(CONFIG_USER_ONLY)
6477 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
6478#else
6479 if (unlikely(ctx->mem_idx == 0)) {
6480 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
6481 return;
6482 }
6483
e5f17ac6 6484 gen_helper_msgclr(cpu_env, cpu_gpr[rB(ctx->opcode)]);
9e0b5cb1
AG
6485#endif
6486}
6487
d5d11a39
AG
6488static void gen_msgsnd(DisasContext *ctx)
6489{
6490#if defined(CONFIG_USER_ONLY)
6491 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
6492#else
6493 if (unlikely(ctx->mem_idx == 0)) {
6494 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
6495 return;
6496 }
6497
6498 gen_helper_msgsnd(cpu_gpr[rB(ctx->opcode)]);
6499#endif
6500}
6501
a9d9eb8f
JM
6502/*** Altivec vector extension ***/
6503/* Altivec registers moves */
a9d9eb8f 6504
636aa200 6505static inline TCGv_ptr gen_avr_ptr(int reg)
564e571a 6506{
e4704b3b 6507 TCGv_ptr r = tcg_temp_new_ptr();
564e571a
AJ
6508 tcg_gen_addi_ptr(r, cpu_env, offsetof(CPUPPCState, avr[reg]));
6509 return r;
6510}
6511
a9d9eb8f 6512#define GEN_VR_LDX(name, opc2, opc3) \
99e300ef 6513static void glue(gen_, name)(DisasContext *ctx) \
a9d9eb8f 6514{ \
fe1e5c53 6515 TCGv EA; \
a9d9eb8f 6516 if (unlikely(!ctx->altivec_enabled)) { \
e06fcd75 6517 gen_exception(ctx, POWERPC_EXCP_VPU); \
a9d9eb8f
JM
6518 return; \
6519 } \
76db3ba4 6520 gen_set_access_type(ctx, ACCESS_INT); \
fe1e5c53 6521 EA = tcg_temp_new(); \
76db3ba4 6522 gen_addr_reg_index(ctx, EA); \
fe1e5c53 6523 tcg_gen_andi_tl(EA, EA, ~0xf); \
76db3ba4
AJ
6524 if (ctx->le_mode) { \
6525 gen_qemu_ld64(ctx, cpu_avrl[rD(ctx->opcode)], EA); \
fe1e5c53 6526 tcg_gen_addi_tl(EA, EA, 8); \
76db3ba4 6527 gen_qemu_ld64(ctx, cpu_avrh[rD(ctx->opcode)], EA); \
fe1e5c53 6528 } else { \
76db3ba4 6529 gen_qemu_ld64(ctx, cpu_avrh[rD(ctx->opcode)], EA); \
fe1e5c53 6530 tcg_gen_addi_tl(EA, EA, 8); \
76db3ba4 6531 gen_qemu_ld64(ctx, cpu_avrl[rD(ctx->opcode)], EA); \
fe1e5c53
AJ
6532 } \
6533 tcg_temp_free(EA); \
a9d9eb8f
JM
6534}
6535
6536#define GEN_VR_STX(name, opc2, opc3) \
99e300ef 6537static void gen_st##name(DisasContext *ctx) \
a9d9eb8f 6538{ \
fe1e5c53 6539 TCGv EA; \
a9d9eb8f 6540 if (unlikely(!ctx->altivec_enabled)) { \
e06fcd75 6541 gen_exception(ctx, POWERPC_EXCP_VPU); \
a9d9eb8f
JM
6542 return; \
6543 } \
76db3ba4 6544 gen_set_access_type(ctx, ACCESS_INT); \
fe1e5c53 6545 EA = tcg_temp_new(); \
76db3ba4 6546 gen_addr_reg_index(ctx, EA); \
fe1e5c53 6547 tcg_gen_andi_tl(EA, EA, ~0xf); \
76db3ba4
AJ
6548 if (ctx->le_mode) { \
6549 gen_qemu_st64(ctx, cpu_avrl[rD(ctx->opcode)], EA); \
fe1e5c53 6550 tcg_gen_addi_tl(EA, EA, 8); \
76db3ba4 6551 gen_qemu_st64(ctx, cpu_avrh[rD(ctx->opcode)], EA); \
fe1e5c53 6552 } else { \
76db3ba4 6553 gen_qemu_st64(ctx, cpu_avrh[rD(ctx->opcode)], EA); \
fe1e5c53 6554 tcg_gen_addi_tl(EA, EA, 8); \
76db3ba4 6555 gen_qemu_st64(ctx, cpu_avrl[rD(ctx->opcode)], EA); \
fe1e5c53
AJ
6556 } \
6557 tcg_temp_free(EA); \
a9d9eb8f
JM
6558}
6559
cbfb6ae9 6560#define GEN_VR_LVE(name, opc2, opc3) \
99e300ef 6561static void gen_lve##name(DisasContext *ctx) \
cbfb6ae9
AJ
6562 { \
6563 TCGv EA; \
6564 TCGv_ptr rs; \
6565 if (unlikely(!ctx->altivec_enabled)) { \
6566 gen_exception(ctx, POWERPC_EXCP_VPU); \
6567 return; \
6568 } \
6569 gen_set_access_type(ctx, ACCESS_INT); \
6570 EA = tcg_temp_new(); \
6571 gen_addr_reg_index(ctx, EA); \
6572 rs = gen_avr_ptr(rS(ctx->opcode)); \
2f5a189c 6573 gen_helper_lve##name(cpu_env, rs, EA); \
cbfb6ae9
AJ
6574 tcg_temp_free(EA); \
6575 tcg_temp_free_ptr(rs); \
6576 }
6577
6578#define GEN_VR_STVE(name, opc2, opc3) \
99e300ef 6579static void gen_stve##name(DisasContext *ctx) \
cbfb6ae9
AJ
6580 { \
6581 TCGv EA; \
6582 TCGv_ptr rs; \
6583 if (unlikely(!ctx->altivec_enabled)) { \
6584 gen_exception(ctx, POWERPC_EXCP_VPU); \
6585 return; \
6586 } \
6587 gen_set_access_type(ctx, ACCESS_INT); \
6588 EA = tcg_temp_new(); \
6589 gen_addr_reg_index(ctx, EA); \
6590 rs = gen_avr_ptr(rS(ctx->opcode)); \
2f5a189c 6591 gen_helper_stve##name(cpu_env, rs, EA); \
cbfb6ae9
AJ
6592 tcg_temp_free(EA); \
6593 tcg_temp_free_ptr(rs); \
6594 }
6595
fe1e5c53 6596GEN_VR_LDX(lvx, 0x07, 0x03);
a9d9eb8f 6597/* As we don't emulate the cache, lvxl is stricly equivalent to lvx */
fe1e5c53 6598GEN_VR_LDX(lvxl, 0x07, 0x0B);
a9d9eb8f 6599
cbfb6ae9
AJ
6600GEN_VR_LVE(bx, 0x07, 0x00);
6601GEN_VR_LVE(hx, 0x07, 0x01);
6602GEN_VR_LVE(wx, 0x07, 0x02);
6603
fe1e5c53 6604GEN_VR_STX(svx, 0x07, 0x07);
a9d9eb8f 6605/* As we don't emulate the cache, stvxl is stricly equivalent to stvx */
fe1e5c53 6606GEN_VR_STX(svxl, 0x07, 0x0F);
a9d9eb8f 6607
cbfb6ae9
AJ
6608GEN_VR_STVE(bx, 0x07, 0x04);
6609GEN_VR_STVE(hx, 0x07, 0x05);
6610GEN_VR_STVE(wx, 0x07, 0x06);
6611
99e300ef 6612static void gen_lvsl(DisasContext *ctx)
bf8d8ded
AJ
6613{
6614 TCGv_ptr rd;
6615 TCGv EA;
6616 if (unlikely(!ctx->altivec_enabled)) {
6617 gen_exception(ctx, POWERPC_EXCP_VPU);
6618 return;
6619 }
6620 EA = tcg_temp_new();
6621 gen_addr_reg_index(ctx, EA);
6622 rd = gen_avr_ptr(rD(ctx->opcode));
6623 gen_helper_lvsl(rd, EA);
6624 tcg_temp_free(EA);
6625 tcg_temp_free_ptr(rd);
6626}
6627
99e300ef 6628static void gen_lvsr(DisasContext *ctx)
bf8d8ded
AJ
6629{
6630 TCGv_ptr rd;
6631 TCGv EA;
6632 if (unlikely(!ctx->altivec_enabled)) {
6633 gen_exception(ctx, POWERPC_EXCP_VPU);
6634 return;
6635 }
6636 EA = tcg_temp_new();
6637 gen_addr_reg_index(ctx, EA);
6638 rd = gen_avr_ptr(rD(ctx->opcode));
6639 gen_helper_lvsr(rd, EA);
6640 tcg_temp_free(EA);
6641 tcg_temp_free_ptr(rd);
6642}
6643
99e300ef 6644static void gen_mfvscr(DisasContext *ctx)
785f451b
AJ
6645{
6646 TCGv_i32 t;
6647 if (unlikely(!ctx->altivec_enabled)) {
6648 gen_exception(ctx, POWERPC_EXCP_VPU);
6649 return;
6650 }
6651 tcg_gen_movi_i64(cpu_avrh[rD(ctx->opcode)], 0);
6652 t = tcg_temp_new_i32();
1328c2bf 6653 tcg_gen_ld_i32(t, cpu_env, offsetof(CPUPPCState, vscr));
785f451b 6654 tcg_gen_extu_i32_i64(cpu_avrl[rD(ctx->opcode)], t);
fce5ecb7 6655 tcg_temp_free_i32(t);
785f451b
AJ
6656}
6657
99e300ef 6658static void gen_mtvscr(DisasContext *ctx)
785f451b 6659{
6e87b7c7 6660 TCGv_ptr p;
785f451b
AJ
6661 if (unlikely(!ctx->altivec_enabled)) {
6662 gen_exception(ctx, POWERPC_EXCP_VPU);
6663 return;
6664 }
6e87b7c7 6665 p = gen_avr_ptr(rD(ctx->opcode));
d15f74fb 6666 gen_helper_mtvscr(cpu_env, p);
6e87b7c7 6667 tcg_temp_free_ptr(p);
785f451b
AJ
6668}
6669
7a9b96cf
AJ
6670/* Logical operations */
6671#define GEN_VX_LOGICAL(name, tcg_op, opc2, opc3) \
99e300ef 6672static void glue(gen_, name)(DisasContext *ctx) \
7a9b96cf
AJ
6673{ \
6674 if (unlikely(!ctx->altivec_enabled)) { \
6675 gen_exception(ctx, POWERPC_EXCP_VPU); \
6676 return; \
6677 } \
6678 tcg_op(cpu_avrh[rD(ctx->opcode)], cpu_avrh[rA(ctx->opcode)], cpu_avrh[rB(ctx->opcode)]); \
6679 tcg_op(cpu_avrl[rD(ctx->opcode)], cpu_avrl[rA(ctx->opcode)], cpu_avrl[rB(ctx->opcode)]); \
6680}
6681
6682GEN_VX_LOGICAL(vand, tcg_gen_and_i64, 2, 16);
6683GEN_VX_LOGICAL(vandc, tcg_gen_andc_i64, 2, 17);
6684GEN_VX_LOGICAL(vor, tcg_gen_or_i64, 2, 18);
6685GEN_VX_LOGICAL(vxor, tcg_gen_xor_i64, 2, 19);
6686GEN_VX_LOGICAL(vnor, tcg_gen_nor_i64, 2, 20);
6687
8e27dd6f 6688#define GEN_VXFORM(name, opc2, opc3) \
99e300ef 6689static void glue(gen_, name)(DisasContext *ctx) \
8e27dd6f
AJ
6690{ \
6691 TCGv_ptr ra, rb, rd; \
6692 if (unlikely(!ctx->altivec_enabled)) { \
6693 gen_exception(ctx, POWERPC_EXCP_VPU); \
6694 return; \
6695 } \
6696 ra = gen_avr_ptr(rA(ctx->opcode)); \
6697 rb = gen_avr_ptr(rB(ctx->opcode)); \
6698 rd = gen_avr_ptr(rD(ctx->opcode)); \
6699 gen_helper_##name (rd, ra, rb); \
6700 tcg_temp_free_ptr(ra); \
6701 tcg_temp_free_ptr(rb); \
6702 tcg_temp_free_ptr(rd); \
6703}
6704
d15f74fb
BS
6705#define GEN_VXFORM_ENV(name, opc2, opc3) \
6706static void glue(gen_, name)(DisasContext *ctx) \
6707{ \
6708 TCGv_ptr ra, rb, rd; \
6709 if (unlikely(!ctx->altivec_enabled)) { \
6710 gen_exception(ctx, POWERPC_EXCP_VPU); \
6711 return; \
6712 } \
6713 ra = gen_avr_ptr(rA(ctx->opcode)); \
6714 rb = gen_avr_ptr(rB(ctx->opcode)); \
6715 rd = gen_avr_ptr(rD(ctx->opcode)); \
54cddd21 6716 gen_helper_##name(cpu_env, rd, ra, rb); \
d15f74fb
BS
6717 tcg_temp_free_ptr(ra); \
6718 tcg_temp_free_ptr(rb); \
6719 tcg_temp_free_ptr(rd); \
6720}
6721
7872c51c
AJ
6722GEN_VXFORM(vaddubm, 0, 0);
6723GEN_VXFORM(vadduhm, 0, 1);
6724GEN_VXFORM(vadduwm, 0, 2);
6725GEN_VXFORM(vsububm, 0, 16);
6726GEN_VXFORM(vsubuhm, 0, 17);
6727GEN_VXFORM(vsubuwm, 0, 18);
e4039339
AJ
6728GEN_VXFORM(vmaxub, 1, 0);
6729GEN_VXFORM(vmaxuh, 1, 1);
6730GEN_VXFORM(vmaxuw, 1, 2);
6731GEN_VXFORM(vmaxsb, 1, 4);
6732GEN_VXFORM(vmaxsh, 1, 5);
6733GEN_VXFORM(vmaxsw, 1, 6);
6734GEN_VXFORM(vminub, 1, 8);
6735GEN_VXFORM(vminuh, 1, 9);
6736GEN_VXFORM(vminuw, 1, 10);
6737GEN_VXFORM(vminsb, 1, 12);
6738GEN_VXFORM(vminsh, 1, 13);
6739GEN_VXFORM(vminsw, 1, 14);
fab3cbe9
AJ
6740GEN_VXFORM(vavgub, 1, 16);
6741GEN_VXFORM(vavguh, 1, 17);
6742GEN_VXFORM(vavguw, 1, 18);
6743GEN_VXFORM(vavgsb, 1, 20);
6744GEN_VXFORM(vavgsh, 1, 21);
6745GEN_VXFORM(vavgsw, 1, 22);
3b430048
AJ
6746GEN_VXFORM(vmrghb, 6, 0);
6747GEN_VXFORM(vmrghh, 6, 1);
6748GEN_VXFORM(vmrghw, 6, 2);
6749GEN_VXFORM(vmrglb, 6, 4);
6750GEN_VXFORM(vmrglh, 6, 5);
6751GEN_VXFORM(vmrglw, 6, 6);
2c277908
AJ
6752GEN_VXFORM(vmuloub, 4, 0);
6753GEN_VXFORM(vmulouh, 4, 1);
6754GEN_VXFORM(vmulosb, 4, 4);
6755GEN_VXFORM(vmulosh, 4, 5);
6756GEN_VXFORM(vmuleub, 4, 8);
6757GEN_VXFORM(vmuleuh, 4, 9);
6758GEN_VXFORM(vmulesb, 4, 12);
6759GEN_VXFORM(vmulesh, 4, 13);
d79f0809
AJ
6760GEN_VXFORM(vslb, 2, 4);
6761GEN_VXFORM(vslh, 2, 5);
6762GEN_VXFORM(vslw, 2, 6);
07ef34c3
AJ
6763GEN_VXFORM(vsrb, 2, 8);
6764GEN_VXFORM(vsrh, 2, 9);
6765GEN_VXFORM(vsrw, 2, 10);
6766GEN_VXFORM(vsrab, 2, 12);
6767GEN_VXFORM(vsrah, 2, 13);
6768GEN_VXFORM(vsraw, 2, 14);
7b239bec
AJ
6769GEN_VXFORM(vslo, 6, 16);
6770GEN_VXFORM(vsro, 6, 17);
e343da72
AJ
6771GEN_VXFORM(vaddcuw, 0, 6);
6772GEN_VXFORM(vsubcuw, 0, 22);
d15f74fb
BS
6773GEN_VXFORM_ENV(vaddubs, 0, 8);
6774GEN_VXFORM_ENV(vadduhs, 0, 9);
6775GEN_VXFORM_ENV(vadduws, 0, 10);
6776GEN_VXFORM_ENV(vaddsbs, 0, 12);
6777GEN_VXFORM_ENV(vaddshs, 0, 13);
6778GEN_VXFORM_ENV(vaddsws, 0, 14);
6779GEN_VXFORM_ENV(vsububs, 0, 24);
6780GEN_VXFORM_ENV(vsubuhs, 0, 25);
6781GEN_VXFORM_ENV(vsubuws, 0, 26);
6782GEN_VXFORM_ENV(vsubsbs, 0, 28);
6783GEN_VXFORM_ENV(vsubshs, 0, 29);
6784GEN_VXFORM_ENV(vsubsws, 0, 30);
5e1d0985
AJ
6785GEN_VXFORM(vrlb, 2, 0);
6786GEN_VXFORM(vrlh, 2, 1);
6787GEN_VXFORM(vrlw, 2, 2);
d9430add
AJ
6788GEN_VXFORM(vsl, 2, 7);
6789GEN_VXFORM(vsr, 2, 11);
d15f74fb
BS
6790GEN_VXFORM_ENV(vpkuhum, 7, 0);
6791GEN_VXFORM_ENV(vpkuwum, 7, 1);
6792GEN_VXFORM_ENV(vpkuhus, 7, 2);
6793GEN_VXFORM_ENV(vpkuwus, 7, 3);
6794GEN_VXFORM_ENV(vpkshus, 7, 4);
6795GEN_VXFORM_ENV(vpkswus, 7, 5);
6796GEN_VXFORM_ENV(vpkshss, 7, 6);
6797GEN_VXFORM_ENV(vpkswss, 7, 7);
1dd9ffb9 6798GEN_VXFORM(vpkpx, 7, 12);
d15f74fb
BS
6799GEN_VXFORM_ENV(vsum4ubs, 4, 24);
6800GEN_VXFORM_ENV(vsum4sbs, 4, 28);
6801GEN_VXFORM_ENV(vsum4shs, 4, 25);
6802GEN_VXFORM_ENV(vsum2sws, 4, 26);
6803GEN_VXFORM_ENV(vsumsws, 4, 30);
6804GEN_VXFORM_ENV(vaddfp, 5, 0);
6805GEN_VXFORM_ENV(vsubfp, 5, 1);
6806GEN_VXFORM_ENV(vmaxfp, 5, 16);
6807GEN_VXFORM_ENV(vminfp, 5, 17);
fab3cbe9 6808
0cbcd906 6809#define GEN_VXRFORM1(opname, name, str, opc2, opc3) \
e8eaa2c0 6810static void glue(gen_, name)(DisasContext *ctx) \
0cbcd906
AJ
6811 { \
6812 TCGv_ptr ra, rb, rd; \
6813 if (unlikely(!ctx->altivec_enabled)) { \
6814 gen_exception(ctx, POWERPC_EXCP_VPU); \
6815 return; \
6816 } \
6817 ra = gen_avr_ptr(rA(ctx->opcode)); \
6818 rb = gen_avr_ptr(rB(ctx->opcode)); \
6819 rd = gen_avr_ptr(rD(ctx->opcode)); \
d15f74fb 6820 gen_helper_##opname(cpu_env, rd, ra, rb); \
0cbcd906
AJ
6821 tcg_temp_free_ptr(ra); \
6822 tcg_temp_free_ptr(rb); \
6823 tcg_temp_free_ptr(rd); \
6824 }
6825
6826#define GEN_VXRFORM(name, opc2, opc3) \
6827 GEN_VXRFORM1(name, name, #name, opc2, opc3) \
6828 GEN_VXRFORM1(name##_dot, name##_, #name ".", opc2, (opc3 | (0x1 << 4)))
6829
1add6e23
AJ
6830GEN_VXRFORM(vcmpequb, 3, 0)
6831GEN_VXRFORM(vcmpequh, 3, 1)
6832GEN_VXRFORM(vcmpequw, 3, 2)
6833GEN_VXRFORM(vcmpgtsb, 3, 12)
6834GEN_VXRFORM(vcmpgtsh, 3, 13)
6835GEN_VXRFORM(vcmpgtsw, 3, 14)
6836GEN_VXRFORM(vcmpgtub, 3, 8)
6837GEN_VXRFORM(vcmpgtuh, 3, 9)
6838GEN_VXRFORM(vcmpgtuw, 3, 10)
819ca121
AJ
6839GEN_VXRFORM(vcmpeqfp, 3, 3)
6840GEN_VXRFORM(vcmpgefp, 3, 7)
6841GEN_VXRFORM(vcmpgtfp, 3, 11)
6842GEN_VXRFORM(vcmpbfp, 3, 15)
1add6e23 6843
c026766b 6844#define GEN_VXFORM_SIMM(name, opc2, opc3) \
99e300ef 6845static void glue(gen_, name)(DisasContext *ctx) \
c026766b
AJ
6846 { \
6847 TCGv_ptr rd; \
6848 TCGv_i32 simm; \
6849 if (unlikely(!ctx->altivec_enabled)) { \
6850 gen_exception(ctx, POWERPC_EXCP_VPU); \
6851 return; \
6852 } \
6853 simm = tcg_const_i32(SIMM5(ctx->opcode)); \
6854 rd = gen_avr_ptr(rD(ctx->opcode)); \
6855 gen_helper_##name (rd, simm); \
6856 tcg_temp_free_i32(simm); \
6857 tcg_temp_free_ptr(rd); \
6858 }
6859
6860GEN_VXFORM_SIMM(vspltisb, 6, 12);
6861GEN_VXFORM_SIMM(vspltish, 6, 13);
6862GEN_VXFORM_SIMM(vspltisw, 6, 14);
6863
de5f2484 6864#define GEN_VXFORM_NOA(name, opc2, opc3) \
99e300ef 6865static void glue(gen_, name)(DisasContext *ctx) \
de5f2484
AJ
6866 { \
6867 TCGv_ptr rb, rd; \
6868 if (unlikely(!ctx->altivec_enabled)) { \
6869 gen_exception(ctx, POWERPC_EXCP_VPU); \
6870 return; \
6871 } \
6872 rb = gen_avr_ptr(rB(ctx->opcode)); \
6873 rd = gen_avr_ptr(rD(ctx->opcode)); \
6874 gen_helper_##name (rd, rb); \
6875 tcg_temp_free_ptr(rb); \
6876 tcg_temp_free_ptr(rd); \
6877 }
6878
d15f74fb
BS
6879#define GEN_VXFORM_NOA_ENV(name, opc2, opc3) \
6880static void glue(gen_, name)(DisasContext *ctx) \
6881 { \
6882 TCGv_ptr rb, rd; \
6883 \
6884 if (unlikely(!ctx->altivec_enabled)) { \
6885 gen_exception(ctx, POWERPC_EXCP_VPU); \
6886 return; \
6887 } \
6888 rb = gen_avr_ptr(rB(ctx->opcode)); \
6889 rd = gen_avr_ptr(rD(ctx->opcode)); \
6890 gen_helper_##name(cpu_env, rd, rb); \
6891 tcg_temp_free_ptr(rb); \
6892 tcg_temp_free_ptr(rd); \
6893 }
6894
6cf1c6e5
AJ
6895GEN_VXFORM_NOA(vupkhsb, 7, 8);
6896GEN_VXFORM_NOA(vupkhsh, 7, 9);
6897GEN_VXFORM_NOA(vupklsb, 7, 10);
6898GEN_VXFORM_NOA(vupklsh, 7, 11);
79f85c3a
AJ
6899GEN_VXFORM_NOA(vupkhpx, 7, 13);
6900GEN_VXFORM_NOA(vupklpx, 7, 15);
d15f74fb
BS
6901GEN_VXFORM_NOA_ENV(vrefp, 5, 4);
6902GEN_VXFORM_NOA_ENV(vrsqrtefp, 5, 5);
6903GEN_VXFORM_NOA_ENV(vexptefp, 5, 6);
6904GEN_VXFORM_NOA_ENV(vlogefp, 5, 7);
6905GEN_VXFORM_NOA_ENV(vrfim, 5, 8);
6906GEN_VXFORM_NOA_ENV(vrfin, 5, 9);
6907GEN_VXFORM_NOA_ENV(vrfip, 5, 10);
6908GEN_VXFORM_NOA_ENV(vrfiz, 5, 11);
79f85c3a 6909
21d21583 6910#define GEN_VXFORM_SIMM(name, opc2, opc3) \
99e300ef 6911static void glue(gen_, name)(DisasContext *ctx) \
21d21583
AJ
6912 { \
6913 TCGv_ptr rd; \
6914 TCGv_i32 simm; \
6915 if (unlikely(!ctx->altivec_enabled)) { \
6916 gen_exception(ctx, POWERPC_EXCP_VPU); \
6917 return; \
6918 } \
6919 simm = tcg_const_i32(SIMM5(ctx->opcode)); \
6920 rd = gen_avr_ptr(rD(ctx->opcode)); \
6921 gen_helper_##name (rd, simm); \
6922 tcg_temp_free_i32(simm); \
6923 tcg_temp_free_ptr(rd); \
6924 }
6925
27a4edb3 6926#define GEN_VXFORM_UIMM(name, opc2, opc3) \
99e300ef 6927static void glue(gen_, name)(DisasContext *ctx) \
27a4edb3
AJ
6928 { \
6929 TCGv_ptr rb, rd; \
6930 TCGv_i32 uimm; \
6931 if (unlikely(!ctx->altivec_enabled)) { \
6932 gen_exception(ctx, POWERPC_EXCP_VPU); \
6933 return; \
6934 } \
6935 uimm = tcg_const_i32(UIMM5(ctx->opcode)); \
6936 rb = gen_avr_ptr(rB(ctx->opcode)); \
6937 rd = gen_avr_ptr(rD(ctx->opcode)); \
6938 gen_helper_##name (rd, rb, uimm); \
6939 tcg_temp_free_i32(uimm); \
6940 tcg_temp_free_ptr(rb); \
6941 tcg_temp_free_ptr(rd); \
6942 }
6943
d15f74fb
BS
6944#define GEN_VXFORM_UIMM_ENV(name, opc2, opc3) \
6945static void glue(gen_, name)(DisasContext *ctx) \
6946 { \
6947 TCGv_ptr rb, rd; \
6948 TCGv_i32 uimm; \
6949 \
6950 if (unlikely(!ctx->altivec_enabled)) { \
6951 gen_exception(ctx, POWERPC_EXCP_VPU); \
6952 return; \
6953 } \
6954 uimm = tcg_const_i32(UIMM5(ctx->opcode)); \
6955 rb = gen_avr_ptr(rB(ctx->opcode)); \
6956 rd = gen_avr_ptr(rD(ctx->opcode)); \
6957 gen_helper_##name(cpu_env, rd, rb, uimm); \
6958 tcg_temp_free_i32(uimm); \
6959 tcg_temp_free_ptr(rb); \
6960 tcg_temp_free_ptr(rd); \
6961 }
6962
e4e6bee7
AJ
6963GEN_VXFORM_UIMM(vspltb, 6, 8);
6964GEN_VXFORM_UIMM(vsplth, 6, 9);
6965GEN_VXFORM_UIMM(vspltw, 6, 10);
d15f74fb
BS
6966GEN_VXFORM_UIMM_ENV(vcfux, 5, 12);
6967GEN_VXFORM_UIMM_ENV(vcfsx, 5, 13);
6968GEN_VXFORM_UIMM_ENV(vctuxs, 5, 14);
6969GEN_VXFORM_UIMM_ENV(vctsxs, 5, 15);
e4e6bee7 6970
99e300ef 6971static void gen_vsldoi(DisasContext *ctx)
cd633b10
AJ
6972{
6973 TCGv_ptr ra, rb, rd;
fce5ecb7 6974 TCGv_i32 sh;
cd633b10
AJ
6975 if (unlikely(!ctx->altivec_enabled)) {
6976 gen_exception(ctx, POWERPC_EXCP_VPU);
6977 return;
6978 }
6979 ra = gen_avr_ptr(rA(ctx->opcode));
6980 rb = gen_avr_ptr(rB(ctx->opcode));
6981 rd = gen_avr_ptr(rD(ctx->opcode));
6982 sh = tcg_const_i32(VSH(ctx->opcode));
6983 gen_helper_vsldoi (rd, ra, rb, sh);
6984 tcg_temp_free_ptr(ra);
6985 tcg_temp_free_ptr(rb);
6986 tcg_temp_free_ptr(rd);
fce5ecb7 6987 tcg_temp_free_i32(sh);
cd633b10
AJ
6988}
6989
707cec33 6990#define GEN_VAFORM_PAIRED(name0, name1, opc2) \
d15f74fb 6991static void glue(gen_, name0##_##name1)(DisasContext *ctx) \
707cec33
AJ
6992 { \
6993 TCGv_ptr ra, rb, rc, rd; \
6994 if (unlikely(!ctx->altivec_enabled)) { \
6995 gen_exception(ctx, POWERPC_EXCP_VPU); \
6996 return; \
6997 } \
6998 ra = gen_avr_ptr(rA(ctx->opcode)); \
6999 rb = gen_avr_ptr(rB(ctx->opcode)); \
7000 rc = gen_avr_ptr(rC(ctx->opcode)); \
7001 rd = gen_avr_ptr(rD(ctx->opcode)); \
7002 if (Rc(ctx->opcode)) { \
d15f74fb 7003 gen_helper_##name1(cpu_env, rd, ra, rb, rc); \
707cec33 7004 } else { \
d15f74fb 7005 gen_helper_##name0(cpu_env, rd, ra, rb, rc); \
707cec33
AJ
7006 } \
7007 tcg_temp_free_ptr(ra); \
7008 tcg_temp_free_ptr(rb); \
7009 tcg_temp_free_ptr(rc); \
7010 tcg_temp_free_ptr(rd); \
7011 }
7012
b161ae27
AJ
7013GEN_VAFORM_PAIRED(vmhaddshs, vmhraddshs, 16)
7014
99e300ef 7015static void gen_vmladduhm(DisasContext *ctx)
bcd2ee23
AJ
7016{
7017 TCGv_ptr ra, rb, rc, rd;
7018 if (unlikely(!ctx->altivec_enabled)) {
7019 gen_exception(ctx, POWERPC_EXCP_VPU);
7020 return;
7021 }
7022 ra = gen_avr_ptr(rA(ctx->opcode));
7023 rb = gen_avr_ptr(rB(ctx->opcode));
7024 rc = gen_avr_ptr(rC(ctx->opcode));
7025 rd = gen_avr_ptr(rD(ctx->opcode));
7026 gen_helper_vmladduhm(rd, ra, rb, rc);
7027 tcg_temp_free_ptr(ra);
7028 tcg_temp_free_ptr(rb);
7029 tcg_temp_free_ptr(rc);
7030 tcg_temp_free_ptr(rd);
7031}
7032
b04ae981 7033GEN_VAFORM_PAIRED(vmsumubm, vmsummbm, 18)
4d9903b6 7034GEN_VAFORM_PAIRED(vmsumuhm, vmsumuhs, 19)
eae07261 7035GEN_VAFORM_PAIRED(vmsumshm, vmsumshs, 20)
d1258698 7036GEN_VAFORM_PAIRED(vsel, vperm, 21)
35cf7c7e 7037GEN_VAFORM_PAIRED(vmaddfp, vnmsubfp, 23)
b04ae981 7038
472b24ce
TM
7039/*** VSX extension ***/
7040
7041static inline TCGv_i64 cpu_vsrh(int n)
7042{
7043 if (n < 32) {
7044 return cpu_fpr[n];
7045 } else {
7046 return cpu_avrh[n-32];
7047 }
7048}
7049
7050static inline TCGv_i64 cpu_vsrl(int n)
7051{
7052 if (n < 32) {
7053 return cpu_vsr[n];
7054 } else {
7055 return cpu_avrl[n-32];
7056 }
7057}
7058
e072fe79
TM
7059#define VSX_LOAD_SCALAR(name, operation) \
7060static void gen_##name(DisasContext *ctx) \
7061{ \
7062 TCGv EA; \
7063 if (unlikely(!ctx->vsx_enabled)) { \
7064 gen_exception(ctx, POWERPC_EXCP_VSXU); \
7065 return; \
7066 } \
7067 gen_set_access_type(ctx, ACCESS_INT); \
7068 EA = tcg_temp_new(); \
7069 gen_addr_reg_index(ctx, EA); \
7070 gen_qemu_##operation(ctx, cpu_vsrh(xT(ctx->opcode)), EA); \
7071 /* NOTE: cpu_vsrl is undefined */ \
7072 tcg_temp_free(EA); \
7073}
7074
7075VSX_LOAD_SCALAR(lxsdx, ld64)
cac7f0ba
TM
7076VSX_LOAD_SCALAR(lxsiwax, ld32s_i64)
7077VSX_LOAD_SCALAR(lxsiwzx, ld32u_i64)
7078VSX_LOAD_SCALAR(lxsspx, ld32fs)
fa1832d7 7079
304af367
TM
7080static void gen_lxvd2x(DisasContext *ctx)
7081{
7082 TCGv EA;
7083 if (unlikely(!ctx->vsx_enabled)) {
7084 gen_exception(ctx, POWERPC_EXCP_VSXU);
7085 return;
7086 }
7087 gen_set_access_type(ctx, ACCESS_INT);
7088 EA = tcg_temp_new();
7089 gen_addr_reg_index(ctx, EA);
7090 gen_qemu_ld64(ctx, cpu_vsrh(xT(ctx->opcode)), EA);
7091 tcg_gen_addi_tl(EA, EA, 8);
7092 gen_qemu_ld64(ctx, cpu_vsrl(xT(ctx->opcode)), EA);
7093 tcg_temp_free(EA);
7094}
7095
ca03b467
TM
7096static void gen_lxvdsx(DisasContext *ctx)
7097{
7098 TCGv EA;
7099 if (unlikely(!ctx->vsx_enabled)) {
7100 gen_exception(ctx, POWERPC_EXCP_VSXU);
7101 return;
7102 }
7103 gen_set_access_type(ctx, ACCESS_INT);
7104 EA = tcg_temp_new();
7105 gen_addr_reg_index(ctx, EA);
7106 gen_qemu_ld64(ctx, cpu_vsrh(xT(ctx->opcode)), EA);
f976b09e 7107 tcg_gen_mov_i64(cpu_vsrl(xT(ctx->opcode)), cpu_vsrh(xT(ctx->opcode)));
ca03b467
TM
7108 tcg_temp_free(EA);
7109}
7110
897e61d1
TM
7111static void gen_lxvw4x(DisasContext *ctx)
7112{
f976b09e
AG
7113 TCGv EA;
7114 TCGv_i64 tmp;
897e61d1
TM
7115 TCGv_i64 xth = cpu_vsrh(xT(ctx->opcode));
7116 TCGv_i64 xtl = cpu_vsrl(xT(ctx->opcode));
7117 if (unlikely(!ctx->vsx_enabled)) {
7118 gen_exception(ctx, POWERPC_EXCP_VSXU);
7119 return;
7120 }
7121 gen_set_access_type(ctx, ACCESS_INT);
7122 EA = tcg_temp_new();
f976b09e
AG
7123 tmp = tcg_temp_new_i64();
7124
897e61d1 7125 gen_addr_reg_index(ctx, EA);
f976b09e 7126 gen_qemu_ld32u_i64(ctx, tmp, EA);
897e61d1 7127 tcg_gen_addi_tl(EA, EA, 4);
f976b09e 7128 gen_qemu_ld32u_i64(ctx, xth, EA);
897e61d1
TM
7129 tcg_gen_deposit_i64(xth, xth, tmp, 32, 32);
7130
7131 tcg_gen_addi_tl(EA, EA, 4);
f976b09e 7132 gen_qemu_ld32u_i64(ctx, tmp, EA);
897e61d1 7133 tcg_gen_addi_tl(EA, EA, 4);
f976b09e 7134 gen_qemu_ld32u_i64(ctx, xtl, EA);
897e61d1
TM
7135 tcg_gen_deposit_i64(xtl, xtl, tmp, 32, 32);
7136
7137 tcg_temp_free(EA);
f976b09e 7138 tcg_temp_free_i64(tmp);
897e61d1
TM
7139}
7140
f026da78
TM
7141#define VSX_STORE_SCALAR(name, operation) \
7142static void gen_##name(DisasContext *ctx) \
7143{ \
7144 TCGv EA; \
7145 if (unlikely(!ctx->vsx_enabled)) { \
7146 gen_exception(ctx, POWERPC_EXCP_VSXU); \
7147 return; \
7148 } \
7149 gen_set_access_type(ctx, ACCESS_INT); \
7150 EA = tcg_temp_new(); \
7151 gen_addr_reg_index(ctx, EA); \
7152 gen_qemu_##operation(ctx, cpu_vsrh(xS(ctx->opcode)), EA); \
7153 tcg_temp_free(EA); \
9231ba9e
TM
7154}
7155
f026da78 7156VSX_STORE_SCALAR(stxsdx, st64)
e16a626b
TM
7157VSX_STORE_SCALAR(stxsiwx, st32_i64)
7158VSX_STORE_SCALAR(stxsspx, st32fs)
f026da78 7159
fbed2478
TM
7160static void gen_stxvd2x(DisasContext *ctx)
7161{
7162 TCGv EA;
7163 if (unlikely(!ctx->vsx_enabled)) {
7164 gen_exception(ctx, POWERPC_EXCP_VSXU);
7165 return;
7166 }
7167 gen_set_access_type(ctx, ACCESS_INT);
7168 EA = tcg_temp_new();
7169 gen_addr_reg_index(ctx, EA);
7170 gen_qemu_st64(ctx, cpu_vsrh(xS(ctx->opcode)), EA);
7171 tcg_gen_addi_tl(EA, EA, 8);
7172 gen_qemu_st64(ctx, cpu_vsrl(xS(ctx->opcode)), EA);
7173 tcg_temp_free(EA);
7174}
7175
86e61ce3
TM
7176static void gen_stxvw4x(DisasContext *ctx)
7177{
f976b09e
AG
7178 TCGv_i64 tmp;
7179 TCGv EA;
86e61ce3
TM
7180 if (unlikely(!ctx->vsx_enabled)) {
7181 gen_exception(ctx, POWERPC_EXCP_VSXU);
7182 return;
7183 }
7184 gen_set_access_type(ctx, ACCESS_INT);
7185 EA = tcg_temp_new();
7186 gen_addr_reg_index(ctx, EA);
f976b09e 7187 tmp = tcg_temp_new_i64();
86e61ce3
TM
7188
7189 tcg_gen_shri_i64(tmp, cpu_vsrh(xS(ctx->opcode)), 32);
f976b09e 7190 gen_qemu_st32_i64(ctx, tmp, EA);
86e61ce3 7191 tcg_gen_addi_tl(EA, EA, 4);
f976b09e 7192 gen_qemu_st32_i64(ctx, cpu_vsrh(xS(ctx->opcode)), EA);
86e61ce3
TM
7193
7194 tcg_gen_shri_i64(tmp, cpu_vsrl(xS(ctx->opcode)), 32);
7195 tcg_gen_addi_tl(EA, EA, 4);
f976b09e 7196 gen_qemu_st32_i64(ctx, tmp, EA);
86e61ce3 7197 tcg_gen_addi_tl(EA, EA, 4);
f976b09e 7198 gen_qemu_st32_i64(ctx, cpu_vsrl(xS(ctx->opcode)), EA);
86e61ce3
TM
7199
7200 tcg_temp_free(EA);
f976b09e 7201 tcg_temp_free_i64(tmp);
86e61ce3
TM
7202}
7203
f5c0f7f9
TM
7204#define MV_VSRW(name, tcgop1, tcgop2, target, source) \
7205static void gen_##name(DisasContext *ctx) \
7206{ \
7207 if (xS(ctx->opcode) < 32) { \
7208 if (unlikely(!ctx->fpu_enabled)) { \
7209 gen_exception(ctx, POWERPC_EXCP_FPU); \
7210 return; \
7211 } \
7212 } else { \
7213 if (unlikely(!ctx->altivec_enabled)) { \
7214 gen_exception(ctx, POWERPC_EXCP_VPU); \
7215 return; \
7216 } \
7217 } \
7218 TCGv_i64 tmp = tcg_temp_new_i64(); \
7219 tcg_gen_##tcgop1(tmp, source); \
7220 tcg_gen_##tcgop2(target, tmp); \
7221 tcg_temp_free_i64(tmp); \
7222}
7223
7224
7225MV_VSRW(mfvsrwz, ext32u_i64, trunc_i64_tl, cpu_gpr[rA(ctx->opcode)], \
7226 cpu_vsrh(xS(ctx->opcode)))
7227MV_VSRW(mtvsrwa, extu_tl_i64, ext32s_i64, cpu_vsrh(xT(ctx->opcode)), \
7228 cpu_gpr[rA(ctx->opcode)])
7229MV_VSRW(mtvsrwz, extu_tl_i64, ext32u_i64, cpu_vsrh(xT(ctx->opcode)), \
7230 cpu_gpr[rA(ctx->opcode)])
7231
7232#if defined(TARGET_PPC64)
7233#define MV_VSRD(name, target, source) \
7234static void gen_##name(DisasContext *ctx) \
7235{ \
7236 if (xS(ctx->opcode) < 32) { \
7237 if (unlikely(!ctx->fpu_enabled)) { \
7238 gen_exception(ctx, POWERPC_EXCP_FPU); \
7239 return; \
7240 } \
7241 } else { \
7242 if (unlikely(!ctx->altivec_enabled)) { \
7243 gen_exception(ctx, POWERPC_EXCP_VPU); \
7244 return; \
7245 } \
7246 } \
7247 tcg_gen_mov_i64(target, source); \
7248}
7249
7250MV_VSRD(mfvsrd, cpu_gpr[rA(ctx->opcode)], cpu_vsrh(xS(ctx->opcode)))
7251MV_VSRD(mtvsrd, cpu_vsrh(xT(ctx->opcode)), cpu_gpr[rA(ctx->opcode)])
7252
7253#endif
7254
cd73f2c9
TM
7255static void gen_xxpermdi(DisasContext *ctx)
7256{
7257 if (unlikely(!ctx->vsx_enabled)) {
7258 gen_exception(ctx, POWERPC_EXCP_VSXU);
7259 return;
7260 }
7261
7262 if ((DM(ctx->opcode) & 2) == 0) {
7263 tcg_gen_mov_i64(cpu_vsrh(xT(ctx->opcode)), cpu_vsrh(xA(ctx->opcode)));
7264 } else {
7265 tcg_gen_mov_i64(cpu_vsrh(xT(ctx->opcode)), cpu_vsrl(xA(ctx->opcode)));
7266 }
7267 if ((DM(ctx->opcode) & 1) == 0) {
7268 tcg_gen_mov_i64(cpu_vsrl(xT(ctx->opcode)), cpu_vsrh(xB(ctx->opcode)));
7269 } else {
7270 tcg_gen_mov_i64(cpu_vsrl(xT(ctx->opcode)), cpu_vsrl(xB(ctx->opcode)));
7271 }
7272}
7273
df020ce0
TM
7274#define OP_ABS 1
7275#define OP_NABS 2
7276#define OP_NEG 3
7277#define OP_CPSGN 4
7278#define SGN_MASK_DP 0x8000000000000000ul
7279#define SGN_MASK_SP 0x8000000080000000ul
7280
7281#define VSX_SCALAR_MOVE(name, op, sgn_mask) \
7282static void glue(gen_, name)(DisasContext * ctx) \
7283 { \
7284 TCGv_i64 xb, sgm; \
7285 if (unlikely(!ctx->vsx_enabled)) { \
7286 gen_exception(ctx, POWERPC_EXCP_VSXU); \
7287 return; \
7288 } \
f976b09e
AG
7289 xb = tcg_temp_new_i64(); \
7290 sgm = tcg_temp_new_i64(); \
df020ce0
TM
7291 tcg_gen_mov_i64(xb, cpu_vsrh(xB(ctx->opcode))); \
7292 tcg_gen_movi_i64(sgm, sgn_mask); \
7293 switch (op) { \
7294 case OP_ABS: { \
7295 tcg_gen_andc_i64(xb, xb, sgm); \
7296 break; \
7297 } \
7298 case OP_NABS: { \
7299 tcg_gen_or_i64(xb, xb, sgm); \
7300 break; \
7301 } \
7302 case OP_NEG: { \
7303 tcg_gen_xor_i64(xb, xb, sgm); \
7304 break; \
7305 } \
7306 case OP_CPSGN: { \
f976b09e 7307 TCGv_i64 xa = tcg_temp_new_i64(); \
df020ce0
TM
7308 tcg_gen_mov_i64(xa, cpu_vsrh(xA(ctx->opcode))); \
7309 tcg_gen_and_i64(xa, xa, sgm); \
7310 tcg_gen_andc_i64(xb, xb, sgm); \
7311 tcg_gen_or_i64(xb, xb, xa); \
f976b09e 7312 tcg_temp_free_i64(xa); \
df020ce0
TM
7313 break; \
7314 } \
7315 } \
7316 tcg_gen_mov_i64(cpu_vsrh(xT(ctx->opcode)), xb); \
f976b09e
AG
7317 tcg_temp_free_i64(xb); \
7318 tcg_temp_free_i64(sgm); \
df020ce0
TM
7319 }
7320
7321VSX_SCALAR_MOVE(xsabsdp, OP_ABS, SGN_MASK_DP)
7322VSX_SCALAR_MOVE(xsnabsdp, OP_NABS, SGN_MASK_DP)
7323VSX_SCALAR_MOVE(xsnegdp, OP_NEG, SGN_MASK_DP)
7324VSX_SCALAR_MOVE(xscpsgndp, OP_CPSGN, SGN_MASK_DP)
7325
be574920
TM
7326#define VSX_VECTOR_MOVE(name, op, sgn_mask) \
7327static void glue(gen_, name)(DisasContext * ctx) \
7328 { \
7329 TCGv_i64 xbh, xbl, sgm; \
7330 if (unlikely(!ctx->vsx_enabled)) { \
7331 gen_exception(ctx, POWERPC_EXCP_VSXU); \
7332 return; \
7333 } \
f976b09e
AG
7334 xbh = tcg_temp_new_i64(); \
7335 xbl = tcg_temp_new_i64(); \
7336 sgm = tcg_temp_new_i64(); \
be574920
TM
7337 tcg_gen_mov_i64(xbh, cpu_vsrh(xB(ctx->opcode))); \
7338 tcg_gen_mov_i64(xbl, cpu_vsrl(xB(ctx->opcode))); \
7339 tcg_gen_movi_i64(sgm, sgn_mask); \
7340 switch (op) { \
7341 case OP_ABS: { \
7342 tcg_gen_andc_i64(xbh, xbh, sgm); \
7343 tcg_gen_andc_i64(xbl, xbl, sgm); \
7344 break; \
7345 } \
7346 case OP_NABS: { \
7347 tcg_gen_or_i64(xbh, xbh, sgm); \
7348 tcg_gen_or_i64(xbl, xbl, sgm); \
7349 break; \
7350 } \
7351 case OP_NEG: { \
7352 tcg_gen_xor_i64(xbh, xbh, sgm); \
7353 tcg_gen_xor_i64(xbl, xbl, sgm); \
7354 break; \
7355 } \
7356 case OP_CPSGN: { \
f976b09e
AG
7357 TCGv_i64 xah = tcg_temp_new_i64(); \
7358 TCGv_i64 xal = tcg_temp_new_i64(); \
be574920
TM
7359 tcg_gen_mov_i64(xah, cpu_vsrh(xA(ctx->opcode))); \
7360 tcg_gen_mov_i64(xal, cpu_vsrl(xA(ctx->opcode))); \
7361 tcg_gen_and_i64(xah, xah, sgm); \
7362 tcg_gen_and_i64(xal, xal, sgm); \
7363 tcg_gen_andc_i64(xbh, xbh, sgm); \
7364 tcg_gen_andc_i64(xbl, xbl, sgm); \
7365 tcg_gen_or_i64(xbh, xbh, xah); \
7366 tcg_gen_or_i64(xbl, xbl, xal); \
f976b09e
AG
7367 tcg_temp_free_i64(xah); \
7368 tcg_temp_free_i64(xal); \
be574920
TM
7369 break; \
7370 } \
7371 } \
7372 tcg_gen_mov_i64(cpu_vsrh(xT(ctx->opcode)), xbh); \
7373 tcg_gen_mov_i64(cpu_vsrl(xT(ctx->opcode)), xbl); \
f976b09e
AG
7374 tcg_temp_free_i64(xbh); \
7375 tcg_temp_free_i64(xbl); \
7376 tcg_temp_free_i64(sgm); \
be574920
TM
7377 }
7378
7379VSX_VECTOR_MOVE(xvabsdp, OP_ABS, SGN_MASK_DP)
7380VSX_VECTOR_MOVE(xvnabsdp, OP_NABS, SGN_MASK_DP)
7381VSX_VECTOR_MOVE(xvnegdp, OP_NEG, SGN_MASK_DP)
7382VSX_VECTOR_MOVE(xvcpsgndp, OP_CPSGN, SGN_MASK_DP)
7383VSX_VECTOR_MOVE(xvabssp, OP_ABS, SGN_MASK_SP)
7384VSX_VECTOR_MOVE(xvnabssp, OP_NABS, SGN_MASK_SP)
7385VSX_VECTOR_MOVE(xvnegsp, OP_NEG, SGN_MASK_SP)
7386VSX_VECTOR_MOVE(xvcpsgnsp, OP_CPSGN, SGN_MASK_SP)
7387
3c3cbbdc
TM
7388#define GEN_VSX_HELPER_2(name, op1, op2, inval, type) \
7389static void gen_##name(DisasContext * ctx) \
7390{ \
7391 TCGv_i32 opc; \
7392 if (unlikely(!ctx->vsx_enabled)) { \
7393 gen_exception(ctx, POWERPC_EXCP_VSXU); \
7394 return; \
7395 } \
7396 /* NIP cannot be restored if the memory exception comes from an helper */ \
7397 gen_update_nip(ctx, ctx->nip - 4); \
7398 opc = tcg_const_i32(ctx->opcode); \
7399 gen_helper_##name(cpu_env, opc); \
7400 tcg_temp_free_i32(opc); \
7401}
be574920 7402
3d1140bf
TM
7403#define GEN_VSX_HELPER_XT_XB_ENV(name, op1, op2, inval, type) \
7404static void gen_##name(DisasContext * ctx) \
7405{ \
7406 if (unlikely(!ctx->vsx_enabled)) { \
7407 gen_exception(ctx, POWERPC_EXCP_VSXU); \
7408 return; \
7409 } \
7410 /* NIP cannot be restored if the exception comes */ \
7411 /* from a helper. */ \
7412 gen_update_nip(ctx, ctx->nip - 4); \
7413 \
7414 gen_helper_##name(cpu_vsrh(xT(ctx->opcode)), cpu_env, \
7415 cpu_vsrh(xB(ctx->opcode))); \
7416}
7417
ee6e02c0
TM
7418GEN_VSX_HELPER_2(xsadddp, 0x00, 0x04, 0, PPC2_VSX)
7419GEN_VSX_HELPER_2(xssubdp, 0x00, 0x05, 0, PPC2_VSX)
5e591d88 7420GEN_VSX_HELPER_2(xsmuldp, 0x00, 0x06, 0, PPC2_VSX)
4b98eeef 7421GEN_VSX_HELPER_2(xsdivdp, 0x00, 0x07, 0, PPC2_VSX)
2009227f 7422GEN_VSX_HELPER_2(xsredp, 0x14, 0x05, 0, PPC2_VSX)
d32404fe 7423GEN_VSX_HELPER_2(xssqrtdp, 0x16, 0x04, 0, PPC2_VSX)
d3f9df8f 7424GEN_VSX_HELPER_2(xsrsqrtedp, 0x14, 0x04, 0, PPC2_VSX)
bc80838f 7425GEN_VSX_HELPER_2(xstdivdp, 0x14, 0x07, 0, PPC2_VSX)
5cb151ac 7426GEN_VSX_HELPER_2(xstsqrtdp, 0x14, 0x06, 0, PPC2_VSX)
595c6eef
TM
7427GEN_VSX_HELPER_2(xsmaddadp, 0x04, 0x04, 0, PPC2_VSX)
7428GEN_VSX_HELPER_2(xsmaddmdp, 0x04, 0x05, 0, PPC2_VSX)
7429GEN_VSX_HELPER_2(xsmsubadp, 0x04, 0x06, 0, PPC2_VSX)
7430GEN_VSX_HELPER_2(xsmsubmdp, 0x04, 0x07, 0, PPC2_VSX)
7431GEN_VSX_HELPER_2(xsnmaddadp, 0x04, 0x14, 0, PPC2_VSX)
7432GEN_VSX_HELPER_2(xsnmaddmdp, 0x04, 0x15, 0, PPC2_VSX)
7433GEN_VSX_HELPER_2(xsnmsubadp, 0x04, 0x16, 0, PPC2_VSX)
7434GEN_VSX_HELPER_2(xsnmsubmdp, 0x04, 0x17, 0, PPC2_VSX)
4f17e9c7
TM
7435GEN_VSX_HELPER_2(xscmpodp, 0x0C, 0x05, 0, PPC2_VSX)
7436GEN_VSX_HELPER_2(xscmpudp, 0x0C, 0x04, 0, PPC2_VSX)
959e9c9d
TM
7437GEN_VSX_HELPER_2(xsmaxdp, 0x00, 0x14, 0, PPC2_VSX)
7438GEN_VSX_HELPER_2(xsmindp, 0x00, 0x15, 0, PPC2_VSX)
ed8ac568
TM
7439GEN_VSX_HELPER_2(xscvdpsp, 0x12, 0x10, 0, PPC2_VSX)
7440GEN_VSX_HELPER_2(xscvspdp, 0x12, 0x14, 0, PPC2_VSX)
5177d2ca
TM
7441GEN_VSX_HELPER_2(xscvdpsxds, 0x10, 0x15, 0, PPC2_VSX)
7442GEN_VSX_HELPER_2(xscvdpsxws, 0x10, 0x05, 0, PPC2_VSX)
7443GEN_VSX_HELPER_2(xscvdpuxds, 0x10, 0x14, 0, PPC2_VSX)
7444GEN_VSX_HELPER_2(xscvdpuxws, 0x10, 0x04, 0, PPC2_VSX)
7445GEN_VSX_HELPER_2(xscvsxddp, 0x10, 0x17, 0, PPC2_VSX)
7446GEN_VSX_HELPER_2(xscvuxddp, 0x10, 0x16, 0, PPC2_VSX)
88e33d08
TM
7447GEN_VSX_HELPER_2(xsrdpi, 0x12, 0x04, 0, PPC2_VSX)
7448GEN_VSX_HELPER_2(xsrdpic, 0x16, 0x06, 0, PPC2_VSX)
7449GEN_VSX_HELPER_2(xsrdpim, 0x12, 0x07, 0, PPC2_VSX)
7450GEN_VSX_HELPER_2(xsrdpip, 0x12, 0x06, 0, PPC2_VSX)
7451GEN_VSX_HELPER_2(xsrdpiz, 0x12, 0x05, 0, PPC2_VSX)
3d1140bf 7452GEN_VSX_HELPER_XT_XB_ENV(xsrsp, 0x12, 0x11, 0, PPC2_VSX207)
ee6e02c0 7453
3fd0aadf
TM
7454GEN_VSX_HELPER_2(xsaddsp, 0x00, 0x00, 0, PPC2_VSX207)
7455GEN_VSX_HELPER_2(xssubsp, 0x00, 0x01, 0, PPC2_VSX207)
ab9408a2 7456GEN_VSX_HELPER_2(xsmulsp, 0x00, 0x02, 0, PPC2_VSX207)
b24d0b47 7457GEN_VSX_HELPER_2(xsdivsp, 0x00, 0x03, 0, PPC2_VSX207)
2c0c52ae 7458GEN_VSX_HELPER_2(xsresp, 0x14, 0x01, 0, PPC2_VSX207)
cea4e574 7459GEN_VSX_HELPER_2(xssqrtsp, 0x16, 0x00, 0, PPC2_VSX207)
968e76bc 7460GEN_VSX_HELPER_2(xsrsqrtesp, 0x14, 0x00, 0, PPC2_VSX207)
f53f81e0
TM
7461GEN_VSX_HELPER_2(xsmaddasp, 0x04, 0x00, 0, PPC2_VSX207)
7462GEN_VSX_HELPER_2(xsmaddmsp, 0x04, 0x01, 0, PPC2_VSX207)
7463GEN_VSX_HELPER_2(xsmsubasp, 0x04, 0x02, 0, PPC2_VSX207)
7464GEN_VSX_HELPER_2(xsmsubmsp, 0x04, 0x03, 0, PPC2_VSX207)
7465GEN_VSX_HELPER_2(xsnmaddasp, 0x04, 0x10, 0, PPC2_VSX207)
7466GEN_VSX_HELPER_2(xsnmaddmsp, 0x04, 0x11, 0, PPC2_VSX207)
7467GEN_VSX_HELPER_2(xsnmsubasp, 0x04, 0x12, 0, PPC2_VSX207)
7468GEN_VSX_HELPER_2(xsnmsubmsp, 0x04, 0x13, 0, PPC2_VSX207)
74698350
TM
7469GEN_VSX_HELPER_2(xscvsxdsp, 0x10, 0x13, 0, PPC2_VSX207)
7470GEN_VSX_HELPER_2(xscvuxdsp, 0x10, 0x12, 0, PPC2_VSX207)
3fd0aadf 7471
ee6e02c0
TM
7472GEN_VSX_HELPER_2(xvadddp, 0x00, 0x0C, 0, PPC2_VSX)
7473GEN_VSX_HELPER_2(xvsubdp, 0x00, 0x0D, 0, PPC2_VSX)
5e591d88 7474GEN_VSX_HELPER_2(xvmuldp, 0x00, 0x0E, 0, PPC2_VSX)
4b98eeef 7475GEN_VSX_HELPER_2(xvdivdp, 0x00, 0x0F, 0, PPC2_VSX)
2009227f 7476GEN_VSX_HELPER_2(xvredp, 0x14, 0x0D, 0, PPC2_VSX)
d32404fe 7477GEN_VSX_HELPER_2(xvsqrtdp, 0x16, 0x0C, 0, PPC2_VSX)
d3f9df8f 7478GEN_VSX_HELPER_2(xvrsqrtedp, 0x14, 0x0C, 0, PPC2_VSX)
bc80838f 7479GEN_VSX_HELPER_2(xvtdivdp, 0x14, 0x0F, 0, PPC2_VSX)
5cb151ac 7480GEN_VSX_HELPER_2(xvtsqrtdp, 0x14, 0x0E, 0, PPC2_VSX)
595c6eef
TM
7481GEN_VSX_HELPER_2(xvmaddadp, 0x04, 0x0C, 0, PPC2_VSX)
7482GEN_VSX_HELPER_2(xvmaddmdp, 0x04, 0x0D, 0, PPC2_VSX)
7483GEN_VSX_HELPER_2(xvmsubadp, 0x04, 0x0E, 0, PPC2_VSX)
7484GEN_VSX_HELPER_2(xvmsubmdp, 0x04, 0x0F, 0, PPC2_VSX)
7485GEN_VSX_HELPER_2(xvnmaddadp, 0x04, 0x1C, 0, PPC2_VSX)
7486GEN_VSX_HELPER_2(xvnmaddmdp, 0x04, 0x1D, 0, PPC2_VSX)
7487GEN_VSX_HELPER_2(xvnmsubadp, 0x04, 0x1E, 0, PPC2_VSX)
7488GEN_VSX_HELPER_2(xvnmsubmdp, 0x04, 0x1F, 0, PPC2_VSX)
959e9c9d
TM
7489GEN_VSX_HELPER_2(xvmaxdp, 0x00, 0x1C, 0, PPC2_VSX)
7490GEN_VSX_HELPER_2(xvmindp, 0x00, 0x1D, 0, PPC2_VSX)
354a6dec
TM
7491GEN_VSX_HELPER_2(xvcmpeqdp, 0x0C, 0x0C, 0, PPC2_VSX)
7492GEN_VSX_HELPER_2(xvcmpgtdp, 0x0C, 0x0D, 0, PPC2_VSX)
7493GEN_VSX_HELPER_2(xvcmpgedp, 0x0C, 0x0E, 0, PPC2_VSX)
ed8ac568 7494GEN_VSX_HELPER_2(xvcvdpsp, 0x12, 0x18, 0, PPC2_VSX)
5177d2ca
TM
7495GEN_VSX_HELPER_2(xvcvdpsxds, 0x10, 0x1D, 0, PPC2_VSX)
7496GEN_VSX_HELPER_2(xvcvdpsxws, 0x10, 0x0D, 0, PPC2_VSX)
7497GEN_VSX_HELPER_2(xvcvdpuxds, 0x10, 0x1C, 0, PPC2_VSX)
7498GEN_VSX_HELPER_2(xvcvdpuxws, 0x10, 0x0C, 0, PPC2_VSX)
7499GEN_VSX_HELPER_2(xvcvsxddp, 0x10, 0x1F, 0, PPC2_VSX)
7500GEN_VSX_HELPER_2(xvcvuxddp, 0x10, 0x1E, 0, PPC2_VSX)
7501GEN_VSX_HELPER_2(xvcvsxwdp, 0x10, 0x0F, 0, PPC2_VSX)
7502GEN_VSX_HELPER_2(xvcvuxwdp, 0x10, 0x0E, 0, PPC2_VSX)
88e33d08
TM
7503GEN_VSX_HELPER_2(xvrdpi, 0x12, 0x0C, 0, PPC2_VSX)
7504GEN_VSX_HELPER_2(xvrdpic, 0x16, 0x0E, 0, PPC2_VSX)
7505GEN_VSX_HELPER_2(xvrdpim, 0x12, 0x0F, 0, PPC2_VSX)
7506GEN_VSX_HELPER_2(xvrdpip, 0x12, 0x0E, 0, PPC2_VSX)
7507GEN_VSX_HELPER_2(xvrdpiz, 0x12, 0x0D, 0, PPC2_VSX)
ee6e02c0
TM
7508
7509GEN_VSX_HELPER_2(xvaddsp, 0x00, 0x08, 0, PPC2_VSX)
7510GEN_VSX_HELPER_2(xvsubsp, 0x00, 0x09, 0, PPC2_VSX)
5e591d88 7511GEN_VSX_HELPER_2(xvmulsp, 0x00, 0x0A, 0, PPC2_VSX)
4b98eeef 7512GEN_VSX_HELPER_2(xvdivsp, 0x00, 0x0B, 0, PPC2_VSX)
2009227f 7513GEN_VSX_HELPER_2(xvresp, 0x14, 0x09, 0, PPC2_VSX)
d32404fe 7514GEN_VSX_HELPER_2(xvsqrtsp, 0x16, 0x08, 0, PPC2_VSX)
d3f9df8f 7515GEN_VSX_HELPER_2(xvrsqrtesp, 0x14, 0x08, 0, PPC2_VSX)
bc80838f 7516GEN_VSX_HELPER_2(xvtdivsp, 0x14, 0x0B, 0, PPC2_VSX)
5cb151ac 7517GEN_VSX_HELPER_2(xvtsqrtsp, 0x14, 0x0A, 0, PPC2_VSX)
595c6eef
TM
7518GEN_VSX_HELPER_2(xvmaddasp, 0x04, 0x08, 0, PPC2_VSX)
7519GEN_VSX_HELPER_2(xvmaddmsp, 0x04, 0x09, 0, PPC2_VSX)
7520GEN_VSX_HELPER_2(xvmsubasp, 0x04, 0x0A, 0, PPC2_VSX)
7521GEN_VSX_HELPER_2(xvmsubmsp, 0x04, 0x0B, 0, PPC2_VSX)
7522GEN_VSX_HELPER_2(xvnmaddasp, 0x04, 0x18, 0, PPC2_VSX)
7523GEN_VSX_HELPER_2(xvnmaddmsp, 0x04, 0x19, 0, PPC2_VSX)
7524GEN_VSX_HELPER_2(xvnmsubasp, 0x04, 0x1A, 0, PPC2_VSX)
7525GEN_VSX_HELPER_2(xvnmsubmsp, 0x04, 0x1B, 0, PPC2_VSX)
959e9c9d
TM
7526GEN_VSX_HELPER_2(xvmaxsp, 0x00, 0x18, 0, PPC2_VSX)
7527GEN_VSX_HELPER_2(xvminsp, 0x00, 0x19, 0, PPC2_VSX)
354a6dec
TM
7528GEN_VSX_HELPER_2(xvcmpeqsp, 0x0C, 0x08, 0, PPC2_VSX)
7529GEN_VSX_HELPER_2(xvcmpgtsp, 0x0C, 0x09, 0, PPC2_VSX)
7530GEN_VSX_HELPER_2(xvcmpgesp, 0x0C, 0x0A, 0, PPC2_VSX)
ed8ac568 7531GEN_VSX_HELPER_2(xvcvspdp, 0x12, 0x1C, 0, PPC2_VSX)
5177d2ca
TM
7532GEN_VSX_HELPER_2(xvcvspsxds, 0x10, 0x19, 0, PPC2_VSX)
7533GEN_VSX_HELPER_2(xvcvspsxws, 0x10, 0x09, 0, PPC2_VSX)
7534GEN_VSX_HELPER_2(xvcvspuxds, 0x10, 0x18, 0, PPC2_VSX)
7535GEN_VSX_HELPER_2(xvcvspuxws, 0x10, 0x08, 0, PPC2_VSX)
7536GEN_VSX_HELPER_2(xvcvsxdsp, 0x10, 0x1B, 0, PPC2_VSX)
7537GEN_VSX_HELPER_2(xvcvuxdsp, 0x10, 0x1A, 0, PPC2_VSX)
7538GEN_VSX_HELPER_2(xvcvsxwsp, 0x10, 0x0B, 0, PPC2_VSX)
7539GEN_VSX_HELPER_2(xvcvuxwsp, 0x10, 0x0A, 0, PPC2_VSX)
88e33d08
TM
7540GEN_VSX_HELPER_2(xvrspi, 0x12, 0x08, 0, PPC2_VSX)
7541GEN_VSX_HELPER_2(xvrspic, 0x16, 0x0A, 0, PPC2_VSX)
7542GEN_VSX_HELPER_2(xvrspim, 0x12, 0x0B, 0, PPC2_VSX)
7543GEN_VSX_HELPER_2(xvrspip, 0x12, 0x0A, 0, PPC2_VSX)
7544GEN_VSX_HELPER_2(xvrspiz, 0x12, 0x09, 0, PPC2_VSX)
ee6e02c0 7545
79ca8a6a
TM
7546#define VSX_LOGICAL(name, tcg_op) \
7547static void glue(gen_, name)(DisasContext * ctx) \
7548 { \
7549 if (unlikely(!ctx->vsx_enabled)) { \
7550 gen_exception(ctx, POWERPC_EXCP_VSXU); \
7551 return; \
7552 } \
7553 tcg_op(cpu_vsrh(xT(ctx->opcode)), cpu_vsrh(xA(ctx->opcode)), \
7554 cpu_vsrh(xB(ctx->opcode))); \
7555 tcg_op(cpu_vsrl(xT(ctx->opcode)), cpu_vsrl(xA(ctx->opcode)), \
7556 cpu_vsrl(xB(ctx->opcode))); \
7557 }
7558
f976b09e
AG
7559VSX_LOGICAL(xxland, tcg_gen_and_i64)
7560VSX_LOGICAL(xxlandc, tcg_gen_andc_i64)
7561VSX_LOGICAL(xxlor, tcg_gen_or_i64)
7562VSX_LOGICAL(xxlxor, tcg_gen_xor_i64)
7563VSX_LOGICAL(xxlnor, tcg_gen_nor_i64)
67a33f37
TM
7564VSX_LOGICAL(xxleqv, tcg_gen_eqv_i64)
7565VSX_LOGICAL(xxlnand, tcg_gen_nand_i64)
7566VSX_LOGICAL(xxlorc, tcg_gen_orc_i64)
df020ce0 7567
ce577d2e
TM
7568#define VSX_XXMRG(name, high) \
7569static void glue(gen_, name)(DisasContext * ctx) \
7570 { \
7571 TCGv_i64 a0, a1, b0, b1; \
7572 if (unlikely(!ctx->vsx_enabled)) { \
7573 gen_exception(ctx, POWERPC_EXCP_VSXU); \
7574 return; \
7575 } \
f976b09e
AG
7576 a0 = tcg_temp_new_i64(); \
7577 a1 = tcg_temp_new_i64(); \
7578 b0 = tcg_temp_new_i64(); \
7579 b1 = tcg_temp_new_i64(); \
ce577d2e
TM
7580 if (high) { \
7581 tcg_gen_mov_i64(a0, cpu_vsrh(xA(ctx->opcode))); \
7582 tcg_gen_mov_i64(a1, cpu_vsrh(xA(ctx->opcode))); \
7583 tcg_gen_mov_i64(b0, cpu_vsrh(xB(ctx->opcode))); \
7584 tcg_gen_mov_i64(b1, cpu_vsrh(xB(ctx->opcode))); \
7585 } else { \
7586 tcg_gen_mov_i64(a0, cpu_vsrl(xA(ctx->opcode))); \
7587 tcg_gen_mov_i64(a1, cpu_vsrl(xA(ctx->opcode))); \
7588 tcg_gen_mov_i64(b0, cpu_vsrl(xB(ctx->opcode))); \
7589 tcg_gen_mov_i64(b1, cpu_vsrl(xB(ctx->opcode))); \
7590 } \
7591 tcg_gen_shri_i64(a0, a0, 32); \
7592 tcg_gen_shri_i64(b0, b0, 32); \
7593 tcg_gen_deposit_i64(cpu_vsrh(xT(ctx->opcode)), \
7594 b0, a0, 32, 32); \
7595 tcg_gen_deposit_i64(cpu_vsrl(xT(ctx->opcode)), \
7596 b1, a1, 32, 32); \
f976b09e
AG
7597 tcg_temp_free_i64(a0); \
7598 tcg_temp_free_i64(a1); \
7599 tcg_temp_free_i64(b0); \
7600 tcg_temp_free_i64(b1); \
ce577d2e
TM
7601 }
7602
7603VSX_XXMRG(xxmrghw, 1)
7604VSX_XXMRG(xxmrglw, 0)
7605
551e3ef7
TM
7606static void gen_xxsel(DisasContext * ctx)
7607{
7608 TCGv_i64 a, b, c;
7609 if (unlikely(!ctx->vsx_enabled)) {
7610 gen_exception(ctx, POWERPC_EXCP_VSXU);
7611 return;
7612 }
f976b09e
AG
7613 a = tcg_temp_new_i64();
7614 b = tcg_temp_new_i64();
7615 c = tcg_temp_new_i64();
551e3ef7
TM
7616
7617 tcg_gen_mov_i64(a, cpu_vsrh(xA(ctx->opcode)));
7618 tcg_gen_mov_i64(b, cpu_vsrh(xB(ctx->opcode)));
7619 tcg_gen_mov_i64(c, cpu_vsrh(xC(ctx->opcode)));
7620
7621 tcg_gen_and_i64(b, b, c);
7622 tcg_gen_andc_i64(a, a, c);
7623 tcg_gen_or_i64(cpu_vsrh(xT(ctx->opcode)), a, b);
7624
7625 tcg_gen_mov_i64(a, cpu_vsrl(xA(ctx->opcode)));
7626 tcg_gen_mov_i64(b, cpu_vsrl(xB(ctx->opcode)));
7627 tcg_gen_mov_i64(c, cpu_vsrl(xC(ctx->opcode)));
7628
7629 tcg_gen_and_i64(b, b, c);
7630 tcg_gen_andc_i64(a, a, c);
7631 tcg_gen_or_i64(cpu_vsrl(xT(ctx->opcode)), a, b);
7632
f976b09e
AG
7633 tcg_temp_free_i64(a);
7634 tcg_temp_free_i64(b);
7635 tcg_temp_free_i64(c);
551e3ef7
TM
7636}
7637
76c15fe0
TM
7638static void gen_xxspltw(DisasContext *ctx)
7639{
7640 TCGv_i64 b, b2;
7641 TCGv_i64 vsr = (UIM(ctx->opcode) & 2) ?
7642 cpu_vsrl(xB(ctx->opcode)) :
7643 cpu_vsrh(xB(ctx->opcode));
7644
7645 if (unlikely(!ctx->vsx_enabled)) {
7646 gen_exception(ctx, POWERPC_EXCP_VSXU);
7647 return;
7648 }
7649
f976b09e
AG
7650 b = tcg_temp_new_i64();
7651 b2 = tcg_temp_new_i64();
76c15fe0
TM
7652
7653 if (UIM(ctx->opcode) & 1) {
7654 tcg_gen_ext32u_i64(b, vsr);
7655 } else {
7656 tcg_gen_shri_i64(b, vsr, 32);
7657 }
7658
7659 tcg_gen_shli_i64(b2, b, 32);
7660 tcg_gen_or_i64(cpu_vsrh(xT(ctx->opcode)), b, b2);
7661 tcg_gen_mov_i64(cpu_vsrl(xT(ctx->opcode)), cpu_vsrh(xT(ctx->opcode)));
7662
f976b09e
AG
7663 tcg_temp_free_i64(b);
7664 tcg_temp_free_i64(b2);
76c15fe0
TM
7665}
7666
acc42968
TM
7667static void gen_xxsldwi(DisasContext *ctx)
7668{
7669 TCGv_i64 xth, xtl;
7670 if (unlikely(!ctx->vsx_enabled)) {
7671 gen_exception(ctx, POWERPC_EXCP_VSXU);
7672 return;
7673 }
f976b09e
AG
7674 xth = tcg_temp_new_i64();
7675 xtl = tcg_temp_new_i64();
acc42968
TM
7676
7677 switch (SHW(ctx->opcode)) {
7678 case 0: {
7679 tcg_gen_mov_i64(xth, cpu_vsrh(xA(ctx->opcode)));
7680 tcg_gen_mov_i64(xtl, cpu_vsrl(xA(ctx->opcode)));
7681 break;
7682 }
7683 case 1: {
f976b09e 7684 TCGv_i64 t0 = tcg_temp_new_i64();
acc42968
TM
7685 tcg_gen_mov_i64(xth, cpu_vsrh(xA(ctx->opcode)));
7686 tcg_gen_shli_i64(xth, xth, 32);
7687 tcg_gen_mov_i64(t0, cpu_vsrl(xA(ctx->opcode)));
7688 tcg_gen_shri_i64(t0, t0, 32);
7689 tcg_gen_or_i64(xth, xth, t0);
7690 tcg_gen_mov_i64(xtl, cpu_vsrl(xA(ctx->opcode)));
7691 tcg_gen_shli_i64(xtl, xtl, 32);
7692 tcg_gen_mov_i64(t0, cpu_vsrh(xB(ctx->opcode)));
7693 tcg_gen_shri_i64(t0, t0, 32);
7694 tcg_gen_or_i64(xtl, xtl, t0);
f976b09e 7695 tcg_temp_free_i64(t0);
acc42968
TM
7696 break;
7697 }
7698 case 2: {
7699 tcg_gen_mov_i64(xth, cpu_vsrl(xA(ctx->opcode)));
7700 tcg_gen_mov_i64(xtl, cpu_vsrh(xB(ctx->opcode)));
7701 break;
7702 }
7703 case 3: {
f976b09e 7704 TCGv_i64 t0 = tcg_temp_new_i64();
acc42968
TM
7705 tcg_gen_mov_i64(xth, cpu_vsrl(xA(ctx->opcode)));
7706 tcg_gen_shli_i64(xth, xth, 32);
7707 tcg_gen_mov_i64(t0, cpu_vsrh(xB(ctx->opcode)));
7708 tcg_gen_shri_i64(t0, t0, 32);
7709 tcg_gen_or_i64(xth, xth, t0);
7710 tcg_gen_mov_i64(xtl, cpu_vsrh(xB(ctx->opcode)));
7711 tcg_gen_shli_i64(xtl, xtl, 32);
7712 tcg_gen_mov_i64(t0, cpu_vsrl(xB(ctx->opcode)));
7713 tcg_gen_shri_i64(t0, t0, 32);
7714 tcg_gen_or_i64(xtl, xtl, t0);
f976b09e 7715 tcg_temp_free_i64(t0);
acc42968
TM
7716 break;
7717 }
7718 }
7719
7720 tcg_gen_mov_i64(cpu_vsrh(xT(ctx->opcode)), xth);
7721 tcg_gen_mov_i64(cpu_vsrl(xT(ctx->opcode)), xtl);
7722
f976b09e
AG
7723 tcg_temp_free_i64(xth);
7724 tcg_temp_free_i64(xtl);
acc42968
TM
7725}
7726
ce577d2e 7727
0487d6a8 7728/*** SPE extension ***/
0487d6a8 7729/* Register moves */
3cd7d1dd 7730
a0e13900
FC
7731static inline void gen_evmra(DisasContext *ctx)
7732{
7733
7734 if (unlikely(!ctx->spe_enabled)) {
27a69bb0 7735 gen_exception(ctx, POWERPC_EXCP_SPEU);
a0e13900
FC
7736 return;
7737 }
7738
7739#if defined(TARGET_PPC64)
7740 /* rD := rA */
7741 tcg_gen_mov_i64(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)]);
7742
7743 /* spe_acc := rA */
7744 tcg_gen_st_i64(cpu_gpr[rA(ctx->opcode)],
7745 cpu_env,
1328c2bf 7746 offsetof(CPUPPCState, spe_acc));
a0e13900
FC
7747#else
7748 TCGv_i64 tmp = tcg_temp_new_i64();
7749
7750 /* tmp := rA_lo + rA_hi << 32 */
7751 tcg_gen_concat_i32_i64(tmp, cpu_gpr[rA(ctx->opcode)], cpu_gprh[rA(ctx->opcode)]);
7752
7753 /* spe_acc := tmp */
1328c2bf 7754 tcg_gen_st_i64(tmp, cpu_env, offsetof(CPUPPCState, spe_acc));
a0e13900
FC
7755 tcg_temp_free_i64(tmp);
7756
7757 /* rD := rA */
7758 tcg_gen_mov_i32(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)]);
7759 tcg_gen_mov_i32(cpu_gprh[rD(ctx->opcode)], cpu_gprh[rA(ctx->opcode)]);
7760#endif
7761}
7762
636aa200
BS
7763static inline void gen_load_gpr64(TCGv_i64 t, int reg)
7764{
f78fb44e
AJ
7765#if defined(TARGET_PPC64)
7766 tcg_gen_mov_i64(t, cpu_gpr[reg]);
7767#else
36aa55dc 7768 tcg_gen_concat_i32_i64(t, cpu_gpr[reg], cpu_gprh[reg]);
3cd7d1dd 7769#endif
f78fb44e 7770}
3cd7d1dd 7771
636aa200
BS
7772static inline void gen_store_gpr64(int reg, TCGv_i64 t)
7773{
f78fb44e
AJ
7774#if defined(TARGET_PPC64)
7775 tcg_gen_mov_i64(cpu_gpr[reg], t);
7776#else
a7812ae4 7777 TCGv_i64 tmp = tcg_temp_new_i64();
f78fb44e 7778 tcg_gen_trunc_i64_i32(cpu_gpr[reg], t);
f78fb44e
AJ
7779 tcg_gen_shri_i64(tmp, t, 32);
7780 tcg_gen_trunc_i64_i32(cpu_gprh[reg], tmp);
a7812ae4 7781 tcg_temp_free_i64(tmp);
3cd7d1dd 7782#endif
f78fb44e 7783}
3cd7d1dd 7784
70560da7 7785#define GEN_SPE(name0, name1, opc2, opc3, inval0, inval1, type) \
99e300ef 7786static void glue(gen_, name0##_##name1)(DisasContext *ctx) \
0487d6a8
JM
7787{ \
7788 if (Rc(ctx->opcode)) \
7789 gen_##name1(ctx); \
7790 else \
7791 gen_##name0(ctx); \
7792}
7793
7794/* Handler for undefined SPE opcodes */
636aa200 7795static inline void gen_speundef(DisasContext *ctx)
0487d6a8 7796{
e06fcd75 7797 gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL);
0487d6a8
JM
7798}
7799
57951c27
AJ
7800/* SPE logic */
7801#if defined(TARGET_PPC64)
7802#define GEN_SPEOP_LOGIC2(name, tcg_op) \
636aa200 7803static inline void gen_##name(DisasContext *ctx) \
0487d6a8
JM
7804{ \
7805 if (unlikely(!ctx->spe_enabled)) { \
27a69bb0 7806 gen_exception(ctx, POWERPC_EXCP_SPEU); \
0487d6a8
JM
7807 return; \
7808 } \
57951c27
AJ
7809 tcg_op(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)], \
7810 cpu_gpr[rB(ctx->opcode)]); \
7811}
7812#else
7813#define GEN_SPEOP_LOGIC2(name, tcg_op) \
636aa200 7814static inline void gen_##name(DisasContext *ctx) \
57951c27
AJ
7815{ \
7816 if (unlikely(!ctx->spe_enabled)) { \
27a69bb0 7817 gen_exception(ctx, POWERPC_EXCP_SPEU); \
57951c27
AJ
7818 return; \
7819 } \
7820 tcg_op(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)], \
7821 cpu_gpr[rB(ctx->opcode)]); \
7822 tcg_op(cpu_gprh[rD(ctx->opcode)], cpu_gprh[rA(ctx->opcode)], \
7823 cpu_gprh[rB(ctx->opcode)]); \
0487d6a8 7824}
57951c27
AJ
7825#endif
7826
7827GEN_SPEOP_LOGIC2(evand, tcg_gen_and_tl);
7828GEN_SPEOP_LOGIC2(evandc, tcg_gen_andc_tl);
7829GEN_SPEOP_LOGIC2(evxor, tcg_gen_xor_tl);
7830GEN_SPEOP_LOGIC2(evor, tcg_gen_or_tl);
7831GEN_SPEOP_LOGIC2(evnor, tcg_gen_nor_tl);
7832GEN_SPEOP_LOGIC2(eveqv, tcg_gen_eqv_tl);
7833GEN_SPEOP_LOGIC2(evorc, tcg_gen_orc_tl);
7834GEN_SPEOP_LOGIC2(evnand, tcg_gen_nand_tl);
0487d6a8 7835
57951c27
AJ
7836/* SPE logic immediate */
7837#if defined(TARGET_PPC64)
7838#define GEN_SPEOP_TCG_LOGIC_IMM2(name, tcg_opi) \
636aa200 7839static inline void gen_##name(DisasContext *ctx) \
3d3a6a0a
AJ
7840{ \
7841 if (unlikely(!ctx->spe_enabled)) { \
27a69bb0 7842 gen_exception(ctx, POWERPC_EXCP_SPEU); \
3d3a6a0a
AJ
7843 return; \
7844 } \
a7812ae4
PB
7845 TCGv_i32 t0 = tcg_temp_local_new_i32(); \
7846 TCGv_i32 t1 = tcg_temp_local_new_i32(); \
7847 TCGv_i64 t2 = tcg_temp_local_new_i64(); \
57951c27
AJ
7848 tcg_gen_trunc_i64_i32(t0, cpu_gpr[rA(ctx->opcode)]); \
7849 tcg_opi(t0, t0, rB(ctx->opcode)); \
7850 tcg_gen_shri_i64(t2, cpu_gpr[rA(ctx->opcode)], 32); \
7851 tcg_gen_trunc_i64_i32(t1, t2); \
a7812ae4 7852 tcg_temp_free_i64(t2); \
57951c27
AJ
7853 tcg_opi(t1, t1, rB(ctx->opcode)); \
7854 tcg_gen_concat_i32_i64(cpu_gpr[rD(ctx->opcode)], t0, t1); \
a7812ae4
PB
7855 tcg_temp_free_i32(t0); \
7856 tcg_temp_free_i32(t1); \
3d3a6a0a 7857}
57951c27
AJ
7858#else
7859#define GEN_SPEOP_TCG_LOGIC_IMM2(name, tcg_opi) \
636aa200 7860static inline void gen_##name(DisasContext *ctx) \
0487d6a8
JM
7861{ \
7862 if (unlikely(!ctx->spe_enabled)) { \
27a69bb0 7863 gen_exception(ctx, POWERPC_EXCP_SPEU); \
0487d6a8
JM
7864 return; \
7865 } \
57951c27
AJ
7866 tcg_opi(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)], \
7867 rB(ctx->opcode)); \
7868 tcg_opi(cpu_gprh[rD(ctx->opcode)], cpu_gprh[rA(ctx->opcode)], \
7869 rB(ctx->opcode)); \
0487d6a8 7870}
57951c27
AJ
7871#endif
7872GEN_SPEOP_TCG_LOGIC_IMM2(evslwi, tcg_gen_shli_i32);
7873GEN_SPEOP_TCG_LOGIC_IMM2(evsrwiu, tcg_gen_shri_i32);
7874GEN_SPEOP_TCG_LOGIC_IMM2(evsrwis, tcg_gen_sari_i32);
7875GEN_SPEOP_TCG_LOGIC_IMM2(evrlwi, tcg_gen_rotli_i32);
0487d6a8 7876
57951c27
AJ
7877/* SPE arithmetic */
7878#if defined(TARGET_PPC64)
7879#define GEN_SPEOP_ARITH1(name, tcg_op) \
636aa200 7880static inline void gen_##name(DisasContext *ctx) \
0487d6a8
JM
7881{ \
7882 if (unlikely(!ctx->spe_enabled)) { \
27a69bb0 7883 gen_exception(ctx, POWERPC_EXCP_SPEU); \
0487d6a8
JM
7884 return; \
7885 } \
a7812ae4
PB
7886 TCGv_i32 t0 = tcg_temp_local_new_i32(); \
7887 TCGv_i32 t1 = tcg_temp_local_new_i32(); \
7888 TCGv_i64 t2 = tcg_temp_local_new_i64(); \
57951c27
AJ
7889 tcg_gen_trunc_i64_i32(t0, cpu_gpr[rA(ctx->opcode)]); \
7890 tcg_op(t0, t0); \
7891 tcg_gen_shri_i64(t2, cpu_gpr[rA(ctx->opcode)], 32); \
7892 tcg_gen_trunc_i64_i32(t1, t2); \
a7812ae4 7893 tcg_temp_free_i64(t2); \
57951c27
AJ
7894 tcg_op(t1, t1); \
7895 tcg_gen_concat_i32_i64(cpu_gpr[rD(ctx->opcode)], t0, t1); \
a7812ae4
PB
7896 tcg_temp_free_i32(t0); \
7897 tcg_temp_free_i32(t1); \
0487d6a8 7898}
57951c27 7899#else
a7812ae4 7900#define GEN_SPEOP_ARITH1(name, tcg_op) \
636aa200 7901static inline void gen_##name(DisasContext *ctx) \
57951c27
AJ
7902{ \
7903 if (unlikely(!ctx->spe_enabled)) { \
27a69bb0 7904 gen_exception(ctx, POWERPC_EXCP_SPEU); \
57951c27
AJ
7905 return; \
7906 } \
7907 tcg_op(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)]); \
7908 tcg_op(cpu_gprh[rD(ctx->opcode)], cpu_gprh[rA(ctx->opcode)]); \
7909}
7910#endif
0487d6a8 7911
636aa200 7912static inline void gen_op_evabs(TCGv_i32 ret, TCGv_i32 arg1)
57951c27
AJ
7913{
7914 int l1 = gen_new_label();
7915 int l2 = gen_new_label();
0487d6a8 7916
57951c27
AJ
7917 tcg_gen_brcondi_i32(TCG_COND_GE, arg1, 0, l1);
7918 tcg_gen_neg_i32(ret, arg1);
7919 tcg_gen_br(l2);
7920 gen_set_label(l1);
a7812ae4 7921 tcg_gen_mov_i32(ret, arg1);
57951c27
AJ
7922 gen_set_label(l2);
7923}
7924GEN_SPEOP_ARITH1(evabs, gen_op_evabs);
7925GEN_SPEOP_ARITH1(evneg, tcg_gen_neg_i32);
7926GEN_SPEOP_ARITH1(evextsb, tcg_gen_ext8s_i32);
7927GEN_SPEOP_ARITH1(evextsh, tcg_gen_ext16s_i32);
636aa200 7928static inline void gen_op_evrndw(TCGv_i32 ret, TCGv_i32 arg1)
0487d6a8 7929{
57951c27
AJ
7930 tcg_gen_addi_i32(ret, arg1, 0x8000);
7931 tcg_gen_ext16u_i32(ret, ret);
7932}
7933GEN_SPEOP_ARITH1(evrndw, gen_op_evrndw);
a7812ae4
PB
7934GEN_SPEOP_ARITH1(evcntlsw, gen_helper_cntlsw32);
7935GEN_SPEOP_ARITH1(evcntlzw, gen_helper_cntlzw32);
0487d6a8 7936
57951c27
AJ
7937#if defined(TARGET_PPC64)
7938#define GEN_SPEOP_ARITH2(name, tcg_op) \
636aa200 7939static inline void gen_##name(DisasContext *ctx) \
0487d6a8
JM
7940{ \
7941 if (unlikely(!ctx->spe_enabled)) { \
27a69bb0 7942 gen_exception(ctx, POWERPC_EXCP_SPEU); \
0487d6a8
JM
7943 return; \
7944 } \
a7812ae4
PB
7945 TCGv_i32 t0 = tcg_temp_local_new_i32(); \
7946 TCGv_i32 t1 = tcg_temp_local_new_i32(); \
7947 TCGv_i32 t2 = tcg_temp_local_new_i32(); \
501e23c4 7948 TCGv_i64 t3 = tcg_temp_local_new_i64(); \
57951c27
AJ
7949 tcg_gen_trunc_i64_i32(t0, cpu_gpr[rA(ctx->opcode)]); \
7950 tcg_gen_trunc_i64_i32(t2, cpu_gpr[rB(ctx->opcode)]); \
7951 tcg_op(t0, t0, t2); \
7952 tcg_gen_shri_i64(t3, cpu_gpr[rA(ctx->opcode)], 32); \
7953 tcg_gen_trunc_i64_i32(t1, t3); \
7954 tcg_gen_shri_i64(t3, cpu_gpr[rB(ctx->opcode)], 32); \
7955 tcg_gen_trunc_i64_i32(t2, t3); \
a7812ae4 7956 tcg_temp_free_i64(t3); \
57951c27 7957 tcg_op(t1, t1, t2); \
a7812ae4 7958 tcg_temp_free_i32(t2); \
57951c27 7959 tcg_gen_concat_i32_i64(cpu_gpr[rD(ctx->opcode)], t0, t1); \
a7812ae4
PB
7960 tcg_temp_free_i32(t0); \
7961 tcg_temp_free_i32(t1); \
0487d6a8 7962}
57951c27
AJ
7963#else
7964#define GEN_SPEOP_ARITH2(name, tcg_op) \
636aa200 7965static inline void gen_##name(DisasContext *ctx) \
0487d6a8
JM
7966{ \
7967 if (unlikely(!ctx->spe_enabled)) { \
27a69bb0 7968 gen_exception(ctx, POWERPC_EXCP_SPEU); \
0487d6a8
JM
7969 return; \
7970 } \
57951c27
AJ
7971 tcg_op(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)], \
7972 cpu_gpr[rB(ctx->opcode)]); \
7973 tcg_op(cpu_gprh[rD(ctx->opcode)], cpu_gprh[rA(ctx->opcode)], \
7974 cpu_gprh[rB(ctx->opcode)]); \
0487d6a8 7975}
57951c27 7976#endif
0487d6a8 7977
636aa200 7978static inline void gen_op_evsrwu(TCGv_i32 ret, TCGv_i32 arg1, TCGv_i32 arg2)
57951c27 7979{
a7812ae4 7980 TCGv_i32 t0;
57951c27 7981 int l1, l2;
0487d6a8 7982
57951c27
AJ
7983 l1 = gen_new_label();
7984 l2 = gen_new_label();
a7812ae4 7985 t0 = tcg_temp_local_new_i32();
57951c27
AJ
7986 /* No error here: 6 bits are used */
7987 tcg_gen_andi_i32(t0, arg2, 0x3F);
7988 tcg_gen_brcondi_i32(TCG_COND_GE, t0, 32, l1);
7989 tcg_gen_shr_i32(ret, arg1, t0);
7990 tcg_gen_br(l2);
7991 gen_set_label(l1);
7992 tcg_gen_movi_i32(ret, 0);
0aef4261 7993 gen_set_label(l2);
a7812ae4 7994 tcg_temp_free_i32(t0);
57951c27
AJ
7995}
7996GEN_SPEOP_ARITH2(evsrwu, gen_op_evsrwu);
636aa200 7997static inline void gen_op_evsrws(TCGv_i32 ret, TCGv_i32 arg1, TCGv_i32 arg2)
57951c27 7998{
a7812ae4 7999 TCGv_i32 t0;
57951c27
AJ
8000 int l1, l2;
8001
8002 l1 = gen_new_label();
8003 l2 = gen_new_label();
a7812ae4 8004 t0 = tcg_temp_local_new_i32();
57951c27
AJ
8005 /* No error here: 6 bits are used */
8006 tcg_gen_andi_i32(t0, arg2, 0x3F);
8007 tcg_gen_brcondi_i32(TCG_COND_GE, t0, 32, l1);
8008 tcg_gen_sar_i32(ret, arg1, t0);
8009 tcg_gen_br(l2);
8010 gen_set_label(l1);
8011 tcg_gen_movi_i32(ret, 0);
0aef4261 8012 gen_set_label(l2);
a7812ae4 8013 tcg_temp_free_i32(t0);
57951c27
AJ
8014}
8015GEN_SPEOP_ARITH2(evsrws, gen_op_evsrws);
636aa200 8016static inline void gen_op_evslw(TCGv_i32 ret, TCGv_i32 arg1, TCGv_i32 arg2)
57951c27 8017{
a7812ae4 8018 TCGv_i32 t0;
57951c27
AJ
8019 int l1, l2;
8020
8021 l1 = gen_new_label();
8022 l2 = gen_new_label();
a7812ae4 8023 t0 = tcg_temp_local_new_i32();
57951c27
AJ
8024 /* No error here: 6 bits are used */
8025 tcg_gen_andi_i32(t0, arg2, 0x3F);
8026 tcg_gen_brcondi_i32(TCG_COND_GE, t0, 32, l1);
8027 tcg_gen_shl_i32(ret, arg1, t0);
8028 tcg_gen_br(l2);
8029 gen_set_label(l1);
8030 tcg_gen_movi_i32(ret, 0);
e29ef9fa 8031 gen_set_label(l2);
a7812ae4 8032 tcg_temp_free_i32(t0);
57951c27
AJ
8033}
8034GEN_SPEOP_ARITH2(evslw, gen_op_evslw);
636aa200 8035static inline void gen_op_evrlw(TCGv_i32 ret, TCGv_i32 arg1, TCGv_i32 arg2)
57951c27 8036{
a7812ae4 8037 TCGv_i32 t0 = tcg_temp_new_i32();
57951c27
AJ
8038 tcg_gen_andi_i32(t0, arg2, 0x1F);
8039 tcg_gen_rotl_i32(ret, arg1, t0);
a7812ae4 8040 tcg_temp_free_i32(t0);
57951c27
AJ
8041}
8042GEN_SPEOP_ARITH2(evrlw, gen_op_evrlw);
636aa200 8043static inline void gen_evmergehi(DisasContext *ctx)
57951c27
AJ
8044{
8045 if (unlikely(!ctx->spe_enabled)) {
27a69bb0 8046 gen_exception(ctx, POWERPC_EXCP_SPEU);
57951c27
AJ
8047 return;
8048 }
8049#if defined(TARGET_PPC64)
a7812ae4
PB
8050 TCGv t0 = tcg_temp_new();
8051 TCGv t1 = tcg_temp_new();
57951c27
AJ
8052 tcg_gen_shri_tl(t0, cpu_gpr[rB(ctx->opcode)], 32);
8053 tcg_gen_andi_tl(t1, cpu_gpr[rA(ctx->opcode)], 0xFFFFFFFF0000000ULL);
8054 tcg_gen_or_tl(cpu_gpr[rD(ctx->opcode)], t0, t1);
8055 tcg_temp_free(t0);
8056 tcg_temp_free(t1);
8057#else
8058 tcg_gen_mov_i32(cpu_gpr[rD(ctx->opcode)], cpu_gprh[rB(ctx->opcode)]);
8059 tcg_gen_mov_i32(cpu_gprh[rD(ctx->opcode)], cpu_gprh[rA(ctx->opcode)]);
8060#endif
8061}
8062GEN_SPEOP_ARITH2(evaddw, tcg_gen_add_i32);
636aa200 8063static inline void gen_op_evsubf(TCGv_i32 ret, TCGv_i32 arg1, TCGv_i32 arg2)
0487d6a8 8064{
57951c27
AJ
8065 tcg_gen_sub_i32(ret, arg2, arg1);
8066}
8067GEN_SPEOP_ARITH2(evsubfw, gen_op_evsubf);
0487d6a8 8068
57951c27
AJ
8069/* SPE arithmetic immediate */
8070#if defined(TARGET_PPC64)
8071#define GEN_SPEOP_ARITH_IMM2(name, tcg_op) \
636aa200 8072static inline void gen_##name(DisasContext *ctx) \
57951c27
AJ
8073{ \
8074 if (unlikely(!ctx->spe_enabled)) { \
27a69bb0 8075 gen_exception(ctx, POWERPC_EXCP_SPEU); \
57951c27
AJ
8076 return; \
8077 } \
a7812ae4
PB
8078 TCGv_i32 t0 = tcg_temp_local_new_i32(); \
8079 TCGv_i32 t1 = tcg_temp_local_new_i32(); \
8080 TCGv_i64 t2 = tcg_temp_local_new_i64(); \
57951c27
AJ
8081 tcg_gen_trunc_i64_i32(t0, cpu_gpr[rB(ctx->opcode)]); \
8082 tcg_op(t0, t0, rA(ctx->opcode)); \
8083 tcg_gen_shri_i64(t2, cpu_gpr[rB(ctx->opcode)], 32); \
8084 tcg_gen_trunc_i64_i32(t1, t2); \
e06fcd75 8085 tcg_temp_free_i64(t2); \
57951c27
AJ
8086 tcg_op(t1, t1, rA(ctx->opcode)); \
8087 tcg_gen_concat_i32_i64(cpu_gpr[rD(ctx->opcode)], t0, t1); \
a7812ae4
PB
8088 tcg_temp_free_i32(t0); \
8089 tcg_temp_free_i32(t1); \
57951c27
AJ
8090}
8091#else
8092#define GEN_SPEOP_ARITH_IMM2(name, tcg_op) \
636aa200 8093static inline void gen_##name(DisasContext *ctx) \
57951c27
AJ
8094{ \
8095 if (unlikely(!ctx->spe_enabled)) { \
27a69bb0 8096 gen_exception(ctx, POWERPC_EXCP_SPEU); \
57951c27
AJ
8097 return; \
8098 } \
8099 tcg_op(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rB(ctx->opcode)], \
8100 rA(ctx->opcode)); \
8101 tcg_op(cpu_gprh[rD(ctx->opcode)], cpu_gprh[rB(ctx->opcode)], \
8102 rA(ctx->opcode)); \
8103}
8104#endif
8105GEN_SPEOP_ARITH_IMM2(evaddiw, tcg_gen_addi_i32);
8106GEN_SPEOP_ARITH_IMM2(evsubifw, tcg_gen_subi_i32);
8107
8108/* SPE comparison */
8109#if defined(TARGET_PPC64)
8110#define GEN_SPEOP_COMP(name, tcg_cond) \
636aa200 8111static inline void gen_##name(DisasContext *ctx) \
57951c27
AJ
8112{ \
8113 if (unlikely(!ctx->spe_enabled)) { \
27a69bb0 8114 gen_exception(ctx, POWERPC_EXCP_SPEU); \
57951c27
AJ
8115 return; \
8116 } \
8117 int l1 = gen_new_label(); \
8118 int l2 = gen_new_label(); \
8119 int l3 = gen_new_label(); \
8120 int l4 = gen_new_label(); \
a7812ae4
PB
8121 TCGv_i32 t0 = tcg_temp_local_new_i32(); \
8122 TCGv_i32 t1 = tcg_temp_local_new_i32(); \
8123 TCGv_i64 t2 = tcg_temp_local_new_i64(); \
57951c27
AJ
8124 tcg_gen_trunc_i64_i32(t0, cpu_gpr[rA(ctx->opcode)]); \
8125 tcg_gen_trunc_i64_i32(t1, cpu_gpr[rB(ctx->opcode)]); \
8126 tcg_gen_brcond_i32(tcg_cond, t0, t1, l1); \
a7812ae4 8127 tcg_gen_movi_i32(cpu_crf[crfD(ctx->opcode)], 0); \
57951c27
AJ
8128 tcg_gen_br(l2); \
8129 gen_set_label(l1); \
8130 tcg_gen_movi_i32(cpu_crf[crfD(ctx->opcode)], \
8131 CRF_CL | CRF_CH_OR_CL | CRF_CH_AND_CL); \
8132 gen_set_label(l2); \
8133 tcg_gen_shri_i64(t2, cpu_gpr[rA(ctx->opcode)], 32); \
8134 tcg_gen_trunc_i64_i32(t0, t2); \
8135 tcg_gen_shri_i64(t2, cpu_gpr[rB(ctx->opcode)], 32); \
8136 tcg_gen_trunc_i64_i32(t1, t2); \
a7812ae4 8137 tcg_temp_free_i64(t2); \
57951c27
AJ
8138 tcg_gen_brcond_i32(tcg_cond, t0, t1, l3); \
8139 tcg_gen_andi_i32(cpu_crf[crfD(ctx->opcode)], cpu_crf[crfD(ctx->opcode)], \
8140 ~(CRF_CH | CRF_CH_AND_CL)); \
8141 tcg_gen_br(l4); \
8142 gen_set_label(l3); \
8143 tcg_gen_ori_i32(cpu_crf[crfD(ctx->opcode)], cpu_crf[crfD(ctx->opcode)], \
8144 CRF_CH | CRF_CH_OR_CL); \
8145 gen_set_label(l4); \
a7812ae4
PB
8146 tcg_temp_free_i32(t0); \
8147 tcg_temp_free_i32(t1); \
57951c27
AJ
8148}
8149#else
8150#define GEN_SPEOP_COMP(name, tcg_cond) \
636aa200 8151static inline void gen_##name(DisasContext *ctx) \
57951c27
AJ
8152{ \
8153 if (unlikely(!ctx->spe_enabled)) { \
27a69bb0 8154 gen_exception(ctx, POWERPC_EXCP_SPEU); \
57951c27
AJ
8155 return; \
8156 } \
8157 int l1 = gen_new_label(); \
8158 int l2 = gen_new_label(); \
8159 int l3 = gen_new_label(); \
8160 int l4 = gen_new_label(); \
8161 \
8162 tcg_gen_brcond_i32(tcg_cond, cpu_gpr[rA(ctx->opcode)], \
8163 cpu_gpr[rB(ctx->opcode)], l1); \
8164 tcg_gen_movi_tl(cpu_crf[crfD(ctx->opcode)], 0); \
8165 tcg_gen_br(l2); \
8166 gen_set_label(l1); \
8167 tcg_gen_movi_i32(cpu_crf[crfD(ctx->opcode)], \
8168 CRF_CL | CRF_CH_OR_CL | CRF_CH_AND_CL); \
8169 gen_set_label(l2); \
8170 tcg_gen_brcond_i32(tcg_cond, cpu_gprh[rA(ctx->opcode)], \
8171 cpu_gprh[rB(ctx->opcode)], l3); \
8172 tcg_gen_andi_i32(cpu_crf[crfD(ctx->opcode)], cpu_crf[crfD(ctx->opcode)], \
8173 ~(CRF_CH | CRF_CH_AND_CL)); \
8174 tcg_gen_br(l4); \
8175 gen_set_label(l3); \
8176 tcg_gen_ori_i32(cpu_crf[crfD(ctx->opcode)], cpu_crf[crfD(ctx->opcode)], \
8177 CRF_CH | CRF_CH_OR_CL); \
8178 gen_set_label(l4); \
8179}
8180#endif
8181GEN_SPEOP_COMP(evcmpgtu, TCG_COND_GTU);
8182GEN_SPEOP_COMP(evcmpgts, TCG_COND_GT);
8183GEN_SPEOP_COMP(evcmpltu, TCG_COND_LTU);
8184GEN_SPEOP_COMP(evcmplts, TCG_COND_LT);
8185GEN_SPEOP_COMP(evcmpeq, TCG_COND_EQ);
8186
8187/* SPE misc */
636aa200 8188static inline void gen_brinc(DisasContext *ctx)
57951c27
AJ
8189{
8190 /* Note: brinc is usable even if SPE is disabled */
a7812ae4
PB
8191 gen_helper_brinc(cpu_gpr[rD(ctx->opcode)],
8192 cpu_gpr[rA(ctx->opcode)], cpu_gpr[rB(ctx->opcode)]);
0487d6a8 8193}
636aa200 8194static inline void gen_evmergelo(DisasContext *ctx)
57951c27
AJ
8195{
8196 if (unlikely(!ctx->spe_enabled)) {
27a69bb0 8197 gen_exception(ctx, POWERPC_EXCP_SPEU);
57951c27
AJ
8198 return;
8199 }
8200#if defined(TARGET_PPC64)
a7812ae4
PB
8201 TCGv t0 = tcg_temp_new();
8202 TCGv t1 = tcg_temp_new();
17d9b3af 8203 tcg_gen_ext32u_tl(t0, cpu_gpr[rB(ctx->opcode)]);
57951c27
AJ
8204 tcg_gen_shli_tl(t1, cpu_gpr[rA(ctx->opcode)], 32);
8205 tcg_gen_or_tl(cpu_gpr[rD(ctx->opcode)], t0, t1);
8206 tcg_temp_free(t0);
8207 tcg_temp_free(t1);
8208#else
57951c27 8209 tcg_gen_mov_i32(cpu_gprh[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)]);
33890b3e 8210 tcg_gen_mov_i32(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rB(ctx->opcode)]);
57951c27
AJ
8211#endif
8212}
636aa200 8213static inline void gen_evmergehilo(DisasContext *ctx)
57951c27
AJ
8214{
8215 if (unlikely(!ctx->spe_enabled)) {
27a69bb0 8216 gen_exception(ctx, POWERPC_EXCP_SPEU);
57951c27
AJ
8217 return;
8218 }
8219#if defined(TARGET_PPC64)
a7812ae4
PB
8220 TCGv t0 = tcg_temp_new();
8221 TCGv t1 = tcg_temp_new();
17d9b3af 8222 tcg_gen_ext32u_tl(t0, cpu_gpr[rB(ctx->opcode)]);
57951c27
AJ
8223 tcg_gen_andi_tl(t1, cpu_gpr[rA(ctx->opcode)], 0xFFFFFFFF0000000ULL);
8224 tcg_gen_or_tl(cpu_gpr[rD(ctx->opcode)], t0, t1);
8225 tcg_temp_free(t0);
8226 tcg_temp_free(t1);
8227#else
8228 tcg_gen_mov_i32(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rB(ctx->opcode)]);
8229 tcg_gen_mov_i32(cpu_gprh[rD(ctx->opcode)], cpu_gprh[rA(ctx->opcode)]);
8230#endif
8231}
636aa200 8232static inline void gen_evmergelohi(DisasContext *ctx)
57951c27
AJ
8233{
8234 if (unlikely(!ctx->spe_enabled)) {
27a69bb0 8235 gen_exception(ctx, POWERPC_EXCP_SPEU);
57951c27
AJ
8236 return;
8237 }
8238#if defined(TARGET_PPC64)
a7812ae4
PB
8239 TCGv t0 = tcg_temp_new();
8240 TCGv t1 = tcg_temp_new();
57951c27
AJ
8241 tcg_gen_shri_tl(t0, cpu_gpr[rB(ctx->opcode)], 32);
8242 tcg_gen_shli_tl(t1, cpu_gpr[rA(ctx->opcode)], 32);
8243 tcg_gen_or_tl(cpu_gpr[rD(ctx->opcode)], t0, t1);
8244 tcg_temp_free(t0);
8245 tcg_temp_free(t1);
8246#else
33890b3e
NF
8247 if (rD(ctx->opcode) == rA(ctx->opcode)) {
8248 TCGv_i32 tmp = tcg_temp_new_i32();
8249 tcg_gen_mov_i32(tmp, cpu_gpr[rA(ctx->opcode)]);
8250 tcg_gen_mov_i32(cpu_gpr[rD(ctx->opcode)], cpu_gprh[rB(ctx->opcode)]);
8251 tcg_gen_mov_i32(cpu_gprh[rD(ctx->opcode)], tmp);
8252 tcg_temp_free_i32(tmp);
8253 } else {
8254 tcg_gen_mov_i32(cpu_gpr[rD(ctx->opcode)], cpu_gprh[rB(ctx->opcode)]);
8255 tcg_gen_mov_i32(cpu_gprh[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)]);
8256 }
57951c27
AJ
8257#endif
8258}
636aa200 8259static inline void gen_evsplati(DisasContext *ctx)
57951c27 8260{
ae01847f 8261 uint64_t imm = ((int32_t)(rA(ctx->opcode) << 27)) >> 27;
0487d6a8 8262
57951c27 8263#if defined(TARGET_PPC64)
38d14952 8264 tcg_gen_movi_tl(cpu_gpr[rD(ctx->opcode)], (imm << 32) | imm);
57951c27
AJ
8265#else
8266 tcg_gen_movi_i32(cpu_gpr[rD(ctx->opcode)], imm);
8267 tcg_gen_movi_i32(cpu_gprh[rD(ctx->opcode)], imm);
8268#endif
8269}
636aa200 8270static inline void gen_evsplatfi(DisasContext *ctx)
0487d6a8 8271{
ae01847f 8272 uint64_t imm = rA(ctx->opcode) << 27;
0487d6a8 8273
57951c27 8274#if defined(TARGET_PPC64)
38d14952 8275 tcg_gen_movi_tl(cpu_gpr[rD(ctx->opcode)], (imm << 32) | imm);
57951c27
AJ
8276#else
8277 tcg_gen_movi_i32(cpu_gpr[rD(ctx->opcode)], imm);
8278 tcg_gen_movi_i32(cpu_gprh[rD(ctx->opcode)], imm);
8279#endif
0487d6a8
JM
8280}
8281
636aa200 8282static inline void gen_evsel(DisasContext *ctx)
57951c27
AJ
8283{
8284 int l1 = gen_new_label();
8285 int l2 = gen_new_label();
8286 int l3 = gen_new_label();
8287 int l4 = gen_new_label();
a7812ae4 8288 TCGv_i32 t0 = tcg_temp_local_new_i32();
57951c27 8289#if defined(TARGET_PPC64)
a7812ae4
PB
8290 TCGv t1 = tcg_temp_local_new();
8291 TCGv t2 = tcg_temp_local_new();
57951c27
AJ
8292#endif
8293 tcg_gen_andi_i32(t0, cpu_crf[ctx->opcode & 0x07], 1 << 3);
8294 tcg_gen_brcondi_i32(TCG_COND_EQ, t0, 0, l1);
8295#if defined(TARGET_PPC64)
8296 tcg_gen_andi_tl(t1, cpu_gpr[rA(ctx->opcode)], 0xFFFFFFFF00000000ULL);
8297#else
8298 tcg_gen_mov_tl(cpu_gprh[rD(ctx->opcode)], cpu_gprh[rA(ctx->opcode)]);
8299#endif
8300 tcg_gen_br(l2);
8301 gen_set_label(l1);
8302#if defined(TARGET_PPC64)
8303 tcg_gen_andi_tl(t1, cpu_gpr[rB(ctx->opcode)], 0xFFFFFFFF00000000ULL);
8304#else
8305 tcg_gen_mov_tl(cpu_gprh[rD(ctx->opcode)], cpu_gprh[rB(ctx->opcode)]);
8306#endif
8307 gen_set_label(l2);
8308 tcg_gen_andi_i32(t0, cpu_crf[ctx->opcode & 0x07], 1 << 2);
8309 tcg_gen_brcondi_i32(TCG_COND_EQ, t0, 0, l3);
8310#if defined(TARGET_PPC64)
17d9b3af 8311 tcg_gen_ext32u_tl(t2, cpu_gpr[rA(ctx->opcode)]);
57951c27
AJ
8312#else
8313 tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)]);
8314#endif
8315 tcg_gen_br(l4);
8316 gen_set_label(l3);
8317#if defined(TARGET_PPC64)
17d9b3af 8318 tcg_gen_ext32u_tl(t2, cpu_gpr[rB(ctx->opcode)]);
57951c27
AJ
8319#else
8320 tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rB(ctx->opcode)]);
8321#endif
8322 gen_set_label(l4);
a7812ae4 8323 tcg_temp_free_i32(t0);
57951c27
AJ
8324#if defined(TARGET_PPC64)
8325 tcg_gen_or_tl(cpu_gpr[rD(ctx->opcode)], t1, t2);
8326 tcg_temp_free(t1);
8327 tcg_temp_free(t2);
8328#endif
8329}
e8eaa2c0
BS
8330
8331static void gen_evsel0(DisasContext *ctx)
57951c27
AJ
8332{
8333 gen_evsel(ctx);
8334}
e8eaa2c0
BS
8335
8336static void gen_evsel1(DisasContext *ctx)
57951c27
AJ
8337{
8338 gen_evsel(ctx);
8339}
e8eaa2c0
BS
8340
8341static void gen_evsel2(DisasContext *ctx)
57951c27
AJ
8342{
8343 gen_evsel(ctx);
8344}
e8eaa2c0
BS
8345
8346static void gen_evsel3(DisasContext *ctx)
57951c27
AJ
8347{
8348 gen_evsel(ctx);
8349}
0487d6a8 8350
a0e13900
FC
8351/* Multiply */
8352
8353static inline void gen_evmwumi(DisasContext *ctx)
8354{
8355 TCGv_i64 t0, t1;
8356
8357 if (unlikely(!ctx->spe_enabled)) {
27a69bb0 8358 gen_exception(ctx, POWERPC_EXCP_SPEU);
a0e13900
FC
8359 return;
8360 }
8361
8362 t0 = tcg_temp_new_i64();
8363 t1 = tcg_temp_new_i64();
8364
8365 /* t0 := rA; t1 := rB */
8366#if defined(TARGET_PPC64)
8367 tcg_gen_ext32u_tl(t0, cpu_gpr[rA(ctx->opcode)]);
8368 tcg_gen_ext32u_tl(t1, cpu_gpr[rB(ctx->opcode)]);
8369#else
8370 tcg_gen_extu_tl_i64(t0, cpu_gpr[rA(ctx->opcode)]);
8371 tcg_gen_extu_tl_i64(t1, cpu_gpr[rB(ctx->opcode)]);
8372#endif
8373
8374 tcg_gen_mul_i64(t0, t0, t1); /* t0 := rA * rB */
8375
8376 gen_store_gpr64(rD(ctx->opcode), t0); /* rD := t0 */
8377
8378 tcg_temp_free_i64(t0);
8379 tcg_temp_free_i64(t1);
8380}
8381
8382static inline void gen_evmwumia(DisasContext *ctx)
8383{
8384 TCGv_i64 tmp;
8385
8386 if (unlikely(!ctx->spe_enabled)) {
27a69bb0 8387 gen_exception(ctx, POWERPC_EXCP_SPEU);
a0e13900
FC
8388 return;
8389 }
8390
8391 gen_evmwumi(ctx); /* rD := rA * rB */
8392
8393 tmp = tcg_temp_new_i64();
8394
8395 /* acc := rD */
8396 gen_load_gpr64(tmp, rD(ctx->opcode));
1328c2bf 8397 tcg_gen_st_i64(tmp, cpu_env, offsetof(CPUPPCState, spe_acc));
a0e13900
FC
8398 tcg_temp_free_i64(tmp);
8399}
8400
8401static inline void gen_evmwumiaa(DisasContext *ctx)
8402{
8403 TCGv_i64 acc;
8404 TCGv_i64 tmp;
8405
8406 if (unlikely(!ctx->spe_enabled)) {
27a69bb0 8407 gen_exception(ctx, POWERPC_EXCP_SPEU);
a0e13900
FC
8408 return;
8409 }
8410
8411 gen_evmwumi(ctx); /* rD := rA * rB */
8412
8413 acc = tcg_temp_new_i64();
8414 tmp = tcg_temp_new_i64();
8415
8416 /* tmp := rD */
8417 gen_load_gpr64(tmp, rD(ctx->opcode));
8418
8419 /* Load acc */
1328c2bf 8420 tcg_gen_ld_i64(acc, cpu_env, offsetof(CPUPPCState, spe_acc));
a0e13900
FC
8421
8422 /* acc := tmp + acc */
8423 tcg_gen_add_i64(acc, acc, tmp);
8424
8425 /* Store acc */
1328c2bf 8426 tcg_gen_st_i64(acc, cpu_env, offsetof(CPUPPCState, spe_acc));
a0e13900
FC
8427
8428 /* rD := acc */
8429 gen_store_gpr64(rD(ctx->opcode), acc);
8430
8431 tcg_temp_free_i64(acc);
8432 tcg_temp_free_i64(tmp);
8433}
8434
8435static inline void gen_evmwsmi(DisasContext *ctx)
8436{
8437 TCGv_i64 t0, t1;
8438
8439 if (unlikely(!ctx->spe_enabled)) {
27a69bb0 8440 gen_exception(ctx, POWERPC_EXCP_SPEU);
a0e13900
FC
8441 return;
8442 }
8443
8444 t0 = tcg_temp_new_i64();
8445 t1 = tcg_temp_new_i64();
8446
8447 /* t0 := rA; t1 := rB */
8448#if defined(TARGET_PPC64)
8449 tcg_gen_ext32s_tl(t0, cpu_gpr[rA(ctx->opcode)]);
8450 tcg_gen_ext32s_tl(t1, cpu_gpr[rB(ctx->opcode)]);
8451#else
8452 tcg_gen_ext_tl_i64(t0, cpu_gpr[rA(ctx->opcode)]);
8453 tcg_gen_ext_tl_i64(t1, cpu_gpr[rB(ctx->opcode)]);
8454#endif
8455
8456 tcg_gen_mul_i64(t0, t0, t1); /* t0 := rA * rB */
8457
8458 gen_store_gpr64(rD(ctx->opcode), t0); /* rD := t0 */
8459
8460 tcg_temp_free_i64(t0);
8461 tcg_temp_free_i64(t1);
8462}
8463
8464static inline void gen_evmwsmia(DisasContext *ctx)
8465{
8466 TCGv_i64 tmp;
8467
8468 gen_evmwsmi(ctx); /* rD := rA * rB */
8469
8470 tmp = tcg_temp_new_i64();
8471
8472 /* acc := rD */
8473 gen_load_gpr64(tmp, rD(ctx->opcode));
1328c2bf 8474 tcg_gen_st_i64(tmp, cpu_env, offsetof(CPUPPCState, spe_acc));
a0e13900
FC
8475
8476 tcg_temp_free_i64(tmp);
8477}
8478
8479static inline void gen_evmwsmiaa(DisasContext *ctx)
8480{
8481 TCGv_i64 acc = tcg_temp_new_i64();
8482 TCGv_i64 tmp = tcg_temp_new_i64();
8483
8484 gen_evmwsmi(ctx); /* rD := rA * rB */
8485
8486 acc = tcg_temp_new_i64();
8487 tmp = tcg_temp_new_i64();
8488
8489 /* tmp := rD */
8490 gen_load_gpr64(tmp, rD(ctx->opcode));
8491
8492 /* Load acc */
1328c2bf 8493 tcg_gen_ld_i64(acc, cpu_env, offsetof(CPUPPCState, spe_acc));
a0e13900
FC
8494
8495 /* acc := tmp + acc */
8496 tcg_gen_add_i64(acc, acc, tmp);
8497
8498 /* Store acc */
1328c2bf 8499 tcg_gen_st_i64(acc, cpu_env, offsetof(CPUPPCState, spe_acc));
a0e13900
FC
8500
8501 /* rD := acc */
8502 gen_store_gpr64(rD(ctx->opcode), acc);
8503
8504 tcg_temp_free_i64(acc);
8505 tcg_temp_free_i64(tmp);
8506}
8507
70560da7
FC
8508GEN_SPE(evaddw, speundef, 0x00, 0x08, 0x00000000, 0xFFFFFFFF, PPC_SPE); ////
8509GEN_SPE(evaddiw, speundef, 0x01, 0x08, 0x00000000, 0xFFFFFFFF, PPC_SPE);
8510GEN_SPE(evsubfw, speundef, 0x02, 0x08, 0x00000000, 0xFFFFFFFF, PPC_SPE); ////
8511GEN_SPE(evsubifw, speundef, 0x03, 0x08, 0x00000000, 0xFFFFFFFF, PPC_SPE);
8512GEN_SPE(evabs, evneg, 0x04, 0x08, 0x0000F800, 0x0000F800, PPC_SPE); ////
8513GEN_SPE(evextsb, evextsh, 0x05, 0x08, 0x0000F800, 0x0000F800, PPC_SPE); ////
8514GEN_SPE(evrndw, evcntlzw, 0x06, 0x08, 0x0000F800, 0x0000F800, PPC_SPE); ////
8515GEN_SPE(evcntlsw, brinc, 0x07, 0x08, 0x0000F800, 0x00000000, PPC_SPE); //
8516GEN_SPE(evmra, speundef, 0x02, 0x13, 0x0000F800, 0xFFFFFFFF, PPC_SPE);
8517GEN_SPE(speundef, evand, 0x08, 0x08, 0xFFFFFFFF, 0x00000000, PPC_SPE); ////
8518GEN_SPE(evandc, speundef, 0x09, 0x08, 0x00000000, 0xFFFFFFFF, PPC_SPE); ////
8519GEN_SPE(evxor, evor, 0x0B, 0x08, 0x00000000, 0x00000000, PPC_SPE); ////
8520GEN_SPE(evnor, eveqv, 0x0C, 0x08, 0x00000000, 0x00000000, PPC_SPE); ////
8521GEN_SPE(evmwumi, evmwsmi, 0x0C, 0x11, 0x00000000, 0x00000000, PPC_SPE);
8522GEN_SPE(evmwumia, evmwsmia, 0x1C, 0x11, 0x00000000, 0x00000000, PPC_SPE);
8523GEN_SPE(evmwumiaa, evmwsmiaa, 0x0C, 0x15, 0x00000000, 0x00000000, PPC_SPE);
8524GEN_SPE(speundef, evorc, 0x0D, 0x08, 0xFFFFFFFF, 0x00000000, PPC_SPE); ////
8525GEN_SPE(evnand, speundef, 0x0F, 0x08, 0x00000000, 0xFFFFFFFF, PPC_SPE); ////
8526GEN_SPE(evsrwu, evsrws, 0x10, 0x08, 0x00000000, 0x00000000, PPC_SPE); ////
8527GEN_SPE(evsrwiu, evsrwis, 0x11, 0x08, 0x00000000, 0x00000000, PPC_SPE);
8528GEN_SPE(evslw, speundef, 0x12, 0x08, 0x00000000, 0xFFFFFFFF, PPC_SPE); ////
8529GEN_SPE(evslwi, speundef, 0x13, 0x08, 0x00000000, 0xFFFFFFFF, PPC_SPE);
8530GEN_SPE(evrlw, evsplati, 0x14, 0x08, 0x00000000, 0x0000F800, PPC_SPE); //
8531GEN_SPE(evrlwi, evsplatfi, 0x15, 0x08, 0x00000000, 0x0000F800, PPC_SPE);
8532GEN_SPE(evmergehi, evmergelo, 0x16, 0x08, 0x00000000, 0x00000000, PPC_SPE); ////
8533GEN_SPE(evmergehilo, evmergelohi, 0x17, 0x08, 0x00000000, 0x00000000, PPC_SPE); ////
8534GEN_SPE(evcmpgtu, evcmpgts, 0x18, 0x08, 0x00600000, 0x00600000, PPC_SPE); ////
8535GEN_SPE(evcmpltu, evcmplts, 0x19, 0x08, 0x00600000, 0x00600000, PPC_SPE); ////
8536GEN_SPE(evcmpeq, speundef, 0x1A, 0x08, 0x00600000, 0xFFFFFFFF, PPC_SPE); ////
0487d6a8 8537
6a6ae23f 8538/* SPE load and stores */
636aa200 8539static inline void gen_addr_spe_imm_index(DisasContext *ctx, TCGv EA, int sh)
6a6ae23f
AJ
8540{
8541 target_ulong uimm = rB(ctx->opcode);
8542
76db3ba4 8543 if (rA(ctx->opcode) == 0) {
6a6ae23f 8544 tcg_gen_movi_tl(EA, uimm << sh);
76db3ba4 8545 } else {
6a6ae23f 8546 tcg_gen_addi_tl(EA, cpu_gpr[rA(ctx->opcode)], uimm << sh);
c791fe84 8547 if (NARROW_MODE(ctx)) {
76db3ba4
AJ
8548 tcg_gen_ext32u_tl(EA, EA);
8549 }
76db3ba4 8550 }
0487d6a8 8551}
6a6ae23f 8552
636aa200 8553static inline void gen_op_evldd(DisasContext *ctx, TCGv addr)
6a6ae23f
AJ
8554{
8555#if defined(TARGET_PPC64)
76db3ba4 8556 gen_qemu_ld64(ctx, cpu_gpr[rD(ctx->opcode)], addr);
6a6ae23f
AJ
8557#else
8558 TCGv_i64 t0 = tcg_temp_new_i64();
76db3ba4 8559 gen_qemu_ld64(ctx, t0, addr);
6a6ae23f
AJ
8560 tcg_gen_trunc_i64_i32(cpu_gpr[rD(ctx->opcode)], t0);
8561 tcg_gen_shri_i64(t0, t0, 32);
8562 tcg_gen_trunc_i64_i32(cpu_gprh[rD(ctx->opcode)], t0);
8563 tcg_temp_free_i64(t0);
8564#endif
0487d6a8 8565}
6a6ae23f 8566
636aa200 8567static inline void gen_op_evldw(DisasContext *ctx, TCGv addr)
6a6ae23f 8568{
0487d6a8 8569#if defined(TARGET_PPC64)
6a6ae23f 8570 TCGv t0 = tcg_temp_new();
76db3ba4 8571 gen_qemu_ld32u(ctx, t0, addr);
6a6ae23f 8572 tcg_gen_shli_tl(cpu_gpr[rD(ctx->opcode)], t0, 32);
76db3ba4
AJ
8573 gen_addr_add(ctx, addr, addr, 4);
8574 gen_qemu_ld32u(ctx, t0, addr);
6a6ae23f
AJ
8575 tcg_gen_or_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rD(ctx->opcode)], t0);
8576 tcg_temp_free(t0);
8577#else
76db3ba4
AJ
8578 gen_qemu_ld32u(ctx, cpu_gprh[rD(ctx->opcode)], addr);
8579 gen_addr_add(ctx, addr, addr, 4);
8580 gen_qemu_ld32u(ctx, cpu_gpr[rD(ctx->opcode)], addr);
6a6ae23f 8581#endif
0487d6a8 8582}
6a6ae23f 8583
636aa200 8584static inline void gen_op_evldh(DisasContext *ctx, TCGv addr)
6a6ae23f
AJ
8585{
8586 TCGv t0 = tcg_temp_new();
8587#if defined(TARGET_PPC64)
76db3ba4 8588 gen_qemu_ld16u(ctx, t0, addr);
6a6ae23f 8589 tcg_gen_shli_tl(cpu_gpr[rD(ctx->opcode)], t0, 48);
76db3ba4
AJ
8590 gen_addr_add(ctx, addr, addr, 2);
8591 gen_qemu_ld16u(ctx, t0, addr);
6a6ae23f
AJ
8592 tcg_gen_shli_tl(t0, t0, 32);
8593 tcg_gen_or_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rD(ctx->opcode)], t0);
76db3ba4
AJ
8594 gen_addr_add(ctx, addr, addr, 2);
8595 gen_qemu_ld16u(ctx, t0, addr);
6a6ae23f
AJ
8596 tcg_gen_shli_tl(t0, t0, 16);
8597 tcg_gen_or_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rD(ctx->opcode)], t0);
76db3ba4
AJ
8598 gen_addr_add(ctx, addr, addr, 2);
8599 gen_qemu_ld16u(ctx, t0, addr);
6a6ae23f 8600 tcg_gen_or_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rD(ctx->opcode)], t0);
0487d6a8 8601#else
76db3ba4 8602 gen_qemu_ld16u(ctx, t0, addr);
6a6ae23f 8603 tcg_gen_shli_tl(cpu_gprh[rD(ctx->opcode)], t0, 16);
76db3ba4
AJ
8604 gen_addr_add(ctx, addr, addr, 2);
8605 gen_qemu_ld16u(ctx, t0, addr);
6a6ae23f 8606 tcg_gen_or_tl(cpu_gprh[rD(ctx->opcode)], cpu_gprh[rD(ctx->opcode)], t0);
76db3ba4
AJ
8607 gen_addr_add(ctx, addr, addr, 2);
8608 gen_qemu_ld16u(ctx, t0, addr);
6a6ae23f 8609 tcg_gen_shli_tl(cpu_gprh[rD(ctx->opcode)], t0, 16);
76db3ba4
AJ
8610 gen_addr_add(ctx, addr, addr, 2);
8611 gen_qemu_ld16u(ctx, t0, addr);
6a6ae23f 8612 tcg_gen_or_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rD(ctx->opcode)], t0);
0487d6a8 8613#endif
6a6ae23f 8614 tcg_temp_free(t0);
0487d6a8
JM
8615}
8616
636aa200 8617static inline void gen_op_evlhhesplat(DisasContext *ctx, TCGv addr)
6a6ae23f
AJ
8618{
8619 TCGv t0 = tcg_temp_new();
76db3ba4 8620 gen_qemu_ld16u(ctx, t0, addr);
6a6ae23f
AJ
8621#if defined(TARGET_PPC64)
8622 tcg_gen_shli_tl(cpu_gpr[rD(ctx->opcode)], t0, 48);
8623 tcg_gen_shli_tl(t0, t0, 16);
8624 tcg_gen_or_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rD(ctx->opcode)], t0);
8625#else
8626 tcg_gen_shli_tl(t0, t0, 16);
8627 tcg_gen_mov_tl(cpu_gprh[rD(ctx->opcode)], t0);
8628 tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], t0);
8629#endif
8630 tcg_temp_free(t0);
0487d6a8
JM
8631}
8632
636aa200 8633static inline void gen_op_evlhhousplat(DisasContext *ctx, TCGv addr)
6a6ae23f
AJ
8634{
8635 TCGv t0 = tcg_temp_new();
76db3ba4 8636 gen_qemu_ld16u(ctx, t0, addr);
6a6ae23f
AJ
8637#if defined(TARGET_PPC64)
8638 tcg_gen_shli_tl(cpu_gpr[rD(ctx->opcode)], t0, 32);
8639 tcg_gen_or_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rD(ctx->opcode)], t0);
8640#else
8641 tcg_gen_mov_tl(cpu_gprh[rD(ctx->opcode)], t0);
8642 tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], t0);
8643#endif
8644 tcg_temp_free(t0);
0487d6a8
JM
8645}
8646
636aa200 8647static inline void gen_op_evlhhossplat(DisasContext *ctx, TCGv addr)
6a6ae23f
AJ
8648{
8649 TCGv t0 = tcg_temp_new();
76db3ba4 8650 gen_qemu_ld16s(ctx, t0, addr);
6a6ae23f
AJ
8651#if defined(TARGET_PPC64)
8652 tcg_gen_shli_tl(cpu_gpr[rD(ctx->opcode)], t0, 32);
8653 tcg_gen_ext32u_tl(t0, t0);
8654 tcg_gen_or_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rD(ctx->opcode)], t0);
8655#else
8656 tcg_gen_mov_tl(cpu_gprh[rD(ctx->opcode)], t0);
8657 tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], t0);
8658#endif
8659 tcg_temp_free(t0);
8660}
8661
636aa200 8662static inline void gen_op_evlwhe(DisasContext *ctx, TCGv addr)
6a6ae23f
AJ
8663{
8664 TCGv t0 = tcg_temp_new();
8665#if defined(TARGET_PPC64)
76db3ba4 8666 gen_qemu_ld16u(ctx, t0, addr);
6a6ae23f 8667 tcg_gen_shli_tl(cpu_gpr[rD(ctx->opcode)], t0, 48);
76db3ba4
AJ
8668 gen_addr_add(ctx, addr, addr, 2);
8669 gen_qemu_ld16u(ctx, t0, addr);
6a6ae23f
AJ
8670 tcg_gen_shli_tl(t0, t0, 16);
8671 tcg_gen_or_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rD(ctx->opcode)], t0);
8672#else
76db3ba4 8673 gen_qemu_ld16u(ctx, t0, addr);
6a6ae23f 8674 tcg_gen_shli_tl(cpu_gprh[rD(ctx->opcode)], t0, 16);
76db3ba4
AJ
8675 gen_addr_add(ctx, addr, addr, 2);
8676 gen_qemu_ld16u(ctx, t0, addr);
6a6ae23f
AJ
8677 tcg_gen_shli_tl(cpu_gpr[rD(ctx->opcode)], t0, 16);
8678#endif
8679 tcg_temp_free(t0);
8680}
8681
636aa200 8682static inline void gen_op_evlwhou(DisasContext *ctx, TCGv addr)
6a6ae23f
AJ
8683{
8684#if defined(TARGET_PPC64)
8685 TCGv t0 = tcg_temp_new();
76db3ba4
AJ
8686 gen_qemu_ld16u(ctx, cpu_gpr[rD(ctx->opcode)], addr);
8687 gen_addr_add(ctx, addr, addr, 2);
8688 gen_qemu_ld16u(ctx, t0, addr);
6a6ae23f
AJ
8689 tcg_gen_shli_tl(t0, t0, 32);
8690 tcg_gen_or_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rD(ctx->opcode)], t0);
8691 tcg_temp_free(t0);
8692#else
76db3ba4
AJ
8693 gen_qemu_ld16u(ctx, cpu_gprh[rD(ctx->opcode)], addr);
8694 gen_addr_add(ctx, addr, addr, 2);
8695 gen_qemu_ld16u(ctx, cpu_gpr[rD(ctx->opcode)], addr);
6a6ae23f
AJ
8696#endif
8697}
8698
636aa200 8699static inline void gen_op_evlwhos(DisasContext *ctx, TCGv addr)
6a6ae23f
AJ
8700{
8701#if defined(TARGET_PPC64)
8702 TCGv t0 = tcg_temp_new();
76db3ba4 8703 gen_qemu_ld16s(ctx, t0, addr);
6a6ae23f 8704 tcg_gen_ext32u_tl(cpu_gpr[rD(ctx->opcode)], t0);
76db3ba4
AJ
8705 gen_addr_add(ctx, addr, addr, 2);
8706 gen_qemu_ld16s(ctx, t0, addr);
6a6ae23f
AJ
8707 tcg_gen_shli_tl(t0, t0, 32);
8708 tcg_gen_or_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rD(ctx->opcode)], t0);
8709 tcg_temp_free(t0);
8710#else
76db3ba4
AJ
8711 gen_qemu_ld16s(ctx, cpu_gprh[rD(ctx->opcode)], addr);
8712 gen_addr_add(ctx, addr, addr, 2);
8713 gen_qemu_ld16s(ctx, cpu_gpr[rD(ctx->opcode)], addr);
6a6ae23f
AJ
8714#endif
8715}
8716
636aa200 8717static inline void gen_op_evlwwsplat(DisasContext *ctx, TCGv addr)
6a6ae23f
AJ
8718{
8719 TCGv t0 = tcg_temp_new();
76db3ba4 8720 gen_qemu_ld32u(ctx, t0, addr);
0487d6a8 8721#if defined(TARGET_PPC64)
6a6ae23f
AJ
8722 tcg_gen_shli_tl(cpu_gpr[rD(ctx->opcode)], t0, 32);
8723 tcg_gen_or_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rD(ctx->opcode)], t0);
8724#else
8725 tcg_gen_mov_tl(cpu_gprh[rD(ctx->opcode)], t0);
8726 tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], t0);
8727#endif
8728 tcg_temp_free(t0);
8729}
8730
636aa200 8731static inline void gen_op_evlwhsplat(DisasContext *ctx, TCGv addr)
6a6ae23f
AJ
8732{
8733 TCGv t0 = tcg_temp_new();
8734#if defined(TARGET_PPC64)
76db3ba4 8735 gen_qemu_ld16u(ctx, t0, addr);
6a6ae23f
AJ
8736 tcg_gen_shli_tl(cpu_gpr[rD(ctx->opcode)], t0, 48);
8737 tcg_gen_shli_tl(t0, t0, 32);
8738 tcg_gen_or_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rD(ctx->opcode)], t0);
76db3ba4
AJ
8739 gen_addr_add(ctx, addr, addr, 2);
8740 gen_qemu_ld16u(ctx, t0, addr);
6a6ae23f
AJ
8741 tcg_gen_or_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rD(ctx->opcode)], t0);
8742 tcg_gen_shli_tl(t0, t0, 16);
8743 tcg_gen_or_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rD(ctx->opcode)], t0);
8744#else
76db3ba4 8745 gen_qemu_ld16u(ctx, t0, addr);
6a6ae23f
AJ
8746 tcg_gen_shli_tl(cpu_gprh[rD(ctx->opcode)], t0, 16);
8747 tcg_gen_or_tl(cpu_gprh[rD(ctx->opcode)], cpu_gprh[rD(ctx->opcode)], t0);
76db3ba4
AJ
8748 gen_addr_add(ctx, addr, addr, 2);
8749 gen_qemu_ld16u(ctx, t0, addr);
6a6ae23f
AJ
8750 tcg_gen_shli_tl(cpu_gpr[rD(ctx->opcode)], t0, 16);
8751 tcg_gen_or_tl(cpu_gpr[rD(ctx->opcode)], cpu_gprh[rD(ctx->opcode)], t0);
0487d6a8 8752#endif
6a6ae23f
AJ
8753 tcg_temp_free(t0);
8754}
8755
636aa200 8756static inline void gen_op_evstdd(DisasContext *ctx, TCGv addr)
6a6ae23f
AJ
8757{
8758#if defined(TARGET_PPC64)
76db3ba4 8759 gen_qemu_st64(ctx, cpu_gpr[rS(ctx->opcode)], addr);
0487d6a8 8760#else
6a6ae23f
AJ
8761 TCGv_i64 t0 = tcg_temp_new_i64();
8762 tcg_gen_concat_i32_i64(t0, cpu_gpr[rS(ctx->opcode)], cpu_gprh[rS(ctx->opcode)]);
76db3ba4 8763 gen_qemu_st64(ctx, t0, addr);
6a6ae23f
AJ
8764 tcg_temp_free_i64(t0);
8765#endif
8766}
8767
636aa200 8768static inline void gen_op_evstdw(DisasContext *ctx, TCGv addr)
6a6ae23f 8769{
0487d6a8 8770#if defined(TARGET_PPC64)
6a6ae23f
AJ
8771 TCGv t0 = tcg_temp_new();
8772 tcg_gen_shri_tl(t0, cpu_gpr[rS(ctx->opcode)], 32);
76db3ba4 8773 gen_qemu_st32(ctx, t0, addr);
6a6ae23f
AJ
8774 tcg_temp_free(t0);
8775#else
76db3ba4 8776 gen_qemu_st32(ctx, cpu_gprh[rS(ctx->opcode)], addr);
6a6ae23f 8777#endif
76db3ba4
AJ
8778 gen_addr_add(ctx, addr, addr, 4);
8779 gen_qemu_st32(ctx, cpu_gpr[rS(ctx->opcode)], addr);
6a6ae23f
AJ
8780}
8781
636aa200 8782static inline void gen_op_evstdh(DisasContext *ctx, TCGv addr)
6a6ae23f
AJ
8783{
8784 TCGv t0 = tcg_temp_new();
8785#if defined(TARGET_PPC64)
8786 tcg_gen_shri_tl(t0, cpu_gpr[rS(ctx->opcode)], 48);
8787#else
8788 tcg_gen_shri_tl(t0, cpu_gprh[rS(ctx->opcode)], 16);
8789#endif
76db3ba4
AJ
8790 gen_qemu_st16(ctx, t0, addr);
8791 gen_addr_add(ctx, addr, addr, 2);
6a6ae23f
AJ
8792#if defined(TARGET_PPC64)
8793 tcg_gen_shri_tl(t0, cpu_gpr[rS(ctx->opcode)], 32);
76db3ba4 8794 gen_qemu_st16(ctx, t0, addr);
6a6ae23f 8795#else
76db3ba4 8796 gen_qemu_st16(ctx, cpu_gprh[rS(ctx->opcode)], addr);
6a6ae23f 8797#endif
76db3ba4 8798 gen_addr_add(ctx, addr, addr, 2);
6a6ae23f 8799 tcg_gen_shri_tl(t0, cpu_gpr[rS(ctx->opcode)], 16);
76db3ba4 8800 gen_qemu_st16(ctx, t0, addr);
6a6ae23f 8801 tcg_temp_free(t0);
76db3ba4
AJ
8802 gen_addr_add(ctx, addr, addr, 2);
8803 gen_qemu_st16(ctx, cpu_gpr[rS(ctx->opcode)], addr);
6a6ae23f
AJ
8804}
8805
636aa200 8806static inline void gen_op_evstwhe(DisasContext *ctx, TCGv addr)
6a6ae23f
AJ
8807{
8808 TCGv t0 = tcg_temp_new();
8809#if defined(TARGET_PPC64)
8810 tcg_gen_shri_tl(t0, cpu_gpr[rS(ctx->opcode)], 48);
8811#else
8812 tcg_gen_shri_tl(t0, cpu_gprh[rS(ctx->opcode)], 16);
8813#endif
76db3ba4
AJ
8814 gen_qemu_st16(ctx, t0, addr);
8815 gen_addr_add(ctx, addr, addr, 2);
6a6ae23f 8816 tcg_gen_shri_tl(t0, cpu_gpr[rS(ctx->opcode)], 16);
76db3ba4 8817 gen_qemu_st16(ctx, t0, addr);
6a6ae23f
AJ
8818 tcg_temp_free(t0);
8819}
8820
636aa200 8821static inline void gen_op_evstwho(DisasContext *ctx, TCGv addr)
6a6ae23f
AJ
8822{
8823#if defined(TARGET_PPC64)
8824 TCGv t0 = tcg_temp_new();
8825 tcg_gen_shri_tl(t0, cpu_gpr[rS(ctx->opcode)], 32);
76db3ba4 8826 gen_qemu_st16(ctx, t0, addr);
6a6ae23f
AJ
8827 tcg_temp_free(t0);
8828#else
76db3ba4 8829 gen_qemu_st16(ctx, cpu_gprh[rS(ctx->opcode)], addr);
6a6ae23f 8830#endif
76db3ba4
AJ
8831 gen_addr_add(ctx, addr, addr, 2);
8832 gen_qemu_st16(ctx, cpu_gpr[rS(ctx->opcode)], addr);
6a6ae23f
AJ
8833}
8834
636aa200 8835static inline void gen_op_evstwwe(DisasContext *ctx, TCGv addr)
6a6ae23f
AJ
8836{
8837#if defined(TARGET_PPC64)
8838 TCGv t0 = tcg_temp_new();
8839 tcg_gen_shri_tl(t0, cpu_gpr[rS(ctx->opcode)], 32);
76db3ba4 8840 gen_qemu_st32(ctx, t0, addr);
6a6ae23f
AJ
8841 tcg_temp_free(t0);
8842#else
76db3ba4 8843 gen_qemu_st32(ctx, cpu_gprh[rS(ctx->opcode)], addr);
6a6ae23f
AJ
8844#endif
8845}
8846
636aa200 8847static inline void gen_op_evstwwo(DisasContext *ctx, TCGv addr)
6a6ae23f 8848{
76db3ba4 8849 gen_qemu_st32(ctx, cpu_gpr[rS(ctx->opcode)], addr);
6a6ae23f
AJ
8850}
8851
8852#define GEN_SPEOP_LDST(name, opc2, sh) \
99e300ef 8853static void glue(gen_, name)(DisasContext *ctx) \
6a6ae23f
AJ
8854{ \
8855 TCGv t0; \
8856 if (unlikely(!ctx->spe_enabled)) { \
27a69bb0 8857 gen_exception(ctx, POWERPC_EXCP_SPEU); \
6a6ae23f
AJ
8858 return; \
8859 } \
76db3ba4 8860 gen_set_access_type(ctx, ACCESS_INT); \
6a6ae23f
AJ
8861 t0 = tcg_temp_new(); \
8862 if (Rc(ctx->opcode)) { \
76db3ba4 8863 gen_addr_spe_imm_index(ctx, t0, sh); \
6a6ae23f 8864 } else { \
76db3ba4 8865 gen_addr_reg_index(ctx, t0); \
6a6ae23f
AJ
8866 } \
8867 gen_op_##name(ctx, t0); \
8868 tcg_temp_free(t0); \
8869}
8870
8871GEN_SPEOP_LDST(evldd, 0x00, 3);
8872GEN_SPEOP_LDST(evldw, 0x01, 3);
8873GEN_SPEOP_LDST(evldh, 0x02, 3);
8874GEN_SPEOP_LDST(evlhhesplat, 0x04, 1);
8875GEN_SPEOP_LDST(evlhhousplat, 0x06, 1);
8876GEN_SPEOP_LDST(evlhhossplat, 0x07, 1);
8877GEN_SPEOP_LDST(evlwhe, 0x08, 2);
8878GEN_SPEOP_LDST(evlwhou, 0x0A, 2);
8879GEN_SPEOP_LDST(evlwhos, 0x0B, 2);
8880GEN_SPEOP_LDST(evlwwsplat, 0x0C, 2);
8881GEN_SPEOP_LDST(evlwhsplat, 0x0E, 2);
8882
8883GEN_SPEOP_LDST(evstdd, 0x10, 3);
8884GEN_SPEOP_LDST(evstdw, 0x11, 3);
8885GEN_SPEOP_LDST(evstdh, 0x12, 3);
8886GEN_SPEOP_LDST(evstwhe, 0x18, 2);
8887GEN_SPEOP_LDST(evstwho, 0x1A, 2);
8888GEN_SPEOP_LDST(evstwwe, 0x1C, 2);
8889GEN_SPEOP_LDST(evstwwo, 0x1E, 2);
0487d6a8
JM
8890
8891/* Multiply and add - TODO */
8892#if 0
70560da7
FC
8893GEN_SPE(speundef, evmhessf, 0x01, 0x10, 0xFFFFFFFF, 0x00000000, PPC_SPE);//
8894GEN_SPE(speundef, evmhossf, 0x03, 0x10, 0xFFFFFFFF, 0x00000000, PPC_SPE);
8895GEN_SPE(evmheumi, evmhesmi, 0x04, 0x10, 0x00000000, 0x00000000, PPC_SPE);
8896GEN_SPE(speundef, evmhesmf, 0x05, 0x10, 0xFFFFFFFF, 0x00000000, PPC_SPE);
8897GEN_SPE(evmhoumi, evmhosmi, 0x06, 0x10, 0x00000000, 0x00000000, PPC_SPE);
8898GEN_SPE(speundef, evmhosmf, 0x07, 0x10, 0xFFFFFFFF, 0x00000000, PPC_SPE);
8899GEN_SPE(speundef, evmhessfa, 0x11, 0x10, 0xFFFFFFFF, 0x00000000, PPC_SPE);
8900GEN_SPE(speundef, evmhossfa, 0x13, 0x10, 0xFFFFFFFF, 0x00000000, PPC_SPE);
8901GEN_SPE(evmheumia, evmhesmia, 0x14, 0x10, 0x00000000, 0x00000000, PPC_SPE);
8902GEN_SPE(speundef, evmhesmfa, 0x15, 0x10, 0xFFFFFFFF, 0x00000000, PPC_SPE);
8903GEN_SPE(evmhoumia, evmhosmia, 0x16, 0x10, 0x00000000, 0x00000000, PPC_SPE);
8904GEN_SPE(speundef, evmhosmfa, 0x17, 0x10, 0xFFFFFFFF, 0x00000000, PPC_SPE);
8905
8906GEN_SPE(speundef, evmwhssf, 0x03, 0x11, 0xFFFFFFFF, 0x00000000, PPC_SPE);
8907GEN_SPE(evmwlumi, speundef, 0x04, 0x11, 0x00000000, 0xFFFFFFFF, PPC_SPE);
8908GEN_SPE(evmwhumi, evmwhsmi, 0x06, 0x11, 0x00000000, 0x00000000, PPC_SPE);
8909GEN_SPE(speundef, evmwhsmf, 0x07, 0x11, 0xFFFFFFFF, 0x00000000, PPC_SPE);
8910GEN_SPE(speundef, evmwssf, 0x09, 0x11, 0xFFFFFFFF, 0x00000000, PPC_SPE);
8911GEN_SPE(speundef, evmwsmf, 0x0D, 0x11, 0xFFFFFFFF, 0x00000000, PPC_SPE);
8912GEN_SPE(speundef, evmwhssfa, 0x13, 0x11, 0xFFFFFFFF, 0x00000000, PPC_SPE);
8913GEN_SPE(evmwlumia, speundef, 0x14, 0x11, 0x00000000, 0xFFFFFFFF, PPC_SPE);
8914GEN_SPE(evmwhumia, evmwhsmia, 0x16, 0x11, 0x00000000, 0x00000000, PPC_SPE);
8915GEN_SPE(speundef, evmwhsmfa, 0x17, 0x11, 0xFFFFFFFF, 0x00000000, PPC_SPE);
8916GEN_SPE(speundef, evmwssfa, 0x19, 0x11, 0xFFFFFFFF, 0x00000000, PPC_SPE);
8917GEN_SPE(speundef, evmwsmfa, 0x1D, 0x11, 0xFFFFFFFF, 0x00000000, PPC_SPE);
8918
8919GEN_SPE(evadduiaaw, evaddsiaaw, 0x00, 0x13, 0x0000F800, 0x0000F800, PPC_SPE);
8920GEN_SPE(evsubfusiaaw, evsubfssiaaw, 0x01, 0x13, 0x0000F800, 0x0000F800, PPC_SPE);
8921GEN_SPE(evaddumiaaw, evaddsmiaaw, 0x04, 0x13, 0x0000F800, 0x0000F800, PPC_SPE);
8922GEN_SPE(evsubfumiaaw, evsubfsmiaaw, 0x05, 0x13, 0x0000F800, 0x0000F800, PPC_SPE);
8923GEN_SPE(evdivws, evdivwu, 0x06, 0x13, 0x00000000, 0x00000000, PPC_SPE);
8924
8925GEN_SPE(evmheusiaaw, evmhessiaaw, 0x00, 0x14, 0x00000000, 0x00000000, PPC_SPE);
8926GEN_SPE(speundef, evmhessfaaw, 0x01, 0x14, 0xFFFFFFFF, 0x00000000, PPC_SPE);
8927GEN_SPE(evmhousiaaw, evmhossiaaw, 0x02, 0x14, 0x00000000, 0x00000000, PPC_SPE);
8928GEN_SPE(speundef, evmhossfaaw, 0x03, 0x14, 0xFFFFFFFF, 0x00000000, PPC_SPE);
8929GEN_SPE(evmheumiaaw, evmhesmiaaw, 0x04, 0x14, 0x00000000, 0x00000000, PPC_SPE);
8930GEN_SPE(speundef, evmhesmfaaw, 0x05, 0x14, 0xFFFFFFFF, 0x00000000, PPC_SPE);
8931GEN_SPE(evmhoumiaaw, evmhosmiaaw, 0x06, 0x14, 0x00000000, 0x00000000, PPC_SPE);
8932GEN_SPE(speundef, evmhosmfaaw, 0x07, 0x14, 0xFFFFFFFF, 0x00000000, PPC_SPE);
8933GEN_SPE(evmhegumiaa, evmhegsmiaa, 0x14, 0x14, 0x00000000, 0x00000000, PPC_SPE);
8934GEN_SPE(speundef, evmhegsmfaa, 0x15, 0x14, 0xFFFFFFFF, 0x00000000, PPC_SPE);
8935GEN_SPE(evmhogumiaa, evmhogsmiaa, 0x16, 0x14, 0x00000000, 0x00000000, PPC_SPE);
8936GEN_SPE(speundef, evmhogsmfaa, 0x17, 0x14, 0xFFFFFFFF, 0x00000000, PPC_SPE);
8937
8938GEN_SPE(evmwlusiaaw, evmwlssiaaw, 0x00, 0x15, 0x00000000, 0x00000000, PPC_SPE);
8939GEN_SPE(evmwlumiaaw, evmwlsmiaaw, 0x04, 0x15, 0x00000000, 0x00000000, PPC_SPE);
8940GEN_SPE(speundef, evmwssfaa, 0x09, 0x15, 0xFFFFFFFF, 0x00000000, PPC_SPE);
8941GEN_SPE(speundef, evmwsmfaa, 0x0D, 0x15, 0xFFFFFFFF, 0x00000000, PPC_SPE);
8942
8943GEN_SPE(evmheusianw, evmhessianw, 0x00, 0x16, 0x00000000, 0x00000000, PPC_SPE);
8944GEN_SPE(speundef, evmhessfanw, 0x01, 0x16, 0xFFFFFFFF, 0x00000000, PPC_SPE);
8945GEN_SPE(evmhousianw, evmhossianw, 0x02, 0x16, 0x00000000, 0x00000000, PPC_SPE);
8946GEN_SPE(speundef, evmhossfanw, 0x03, 0x16, 0xFFFFFFFF, 0x00000000, PPC_SPE);
8947GEN_SPE(evmheumianw, evmhesmianw, 0x04, 0x16, 0x00000000, 0x00000000, PPC_SPE);
8948GEN_SPE(speundef, evmhesmfanw, 0x05, 0x16, 0xFFFFFFFF, 0x00000000, PPC_SPE);
8949GEN_SPE(evmhoumianw, evmhosmianw, 0x06, 0x16, 0x00000000, 0x00000000, PPC_SPE);
8950GEN_SPE(speundef, evmhosmfanw, 0x07, 0x16, 0xFFFFFFFF, 0x00000000, PPC_SPE);
8951GEN_SPE(evmhegumian, evmhegsmian, 0x14, 0x16, 0x00000000, 0x00000000, PPC_SPE);
8952GEN_SPE(speundef, evmhegsmfan, 0x15, 0x16, 0xFFFFFFFF, 0x00000000, PPC_SPE);
8953GEN_SPE(evmhigumian, evmhigsmian, 0x16, 0x16, 0x00000000, 0x00000000, PPC_SPE);
8954GEN_SPE(speundef, evmhogsmfan, 0x17, 0x16, 0xFFFFFFFF, 0x00000000, PPC_SPE);
8955
8956GEN_SPE(evmwlusianw, evmwlssianw, 0x00, 0x17, 0x00000000, 0x00000000, PPC_SPE);
8957GEN_SPE(evmwlumianw, evmwlsmianw, 0x04, 0x17, 0x00000000, 0x00000000, PPC_SPE);
8958GEN_SPE(speundef, evmwssfan, 0x09, 0x17, 0xFFFFFFFF, 0x00000000, PPC_SPE);
8959GEN_SPE(evmwumian, evmwsmian, 0x0C, 0x17, 0x00000000, 0x00000000, PPC_SPE);
8960GEN_SPE(speundef, evmwsmfan, 0x0D, 0x17, 0xFFFFFFFF, 0x00000000, PPC_SPE);
0487d6a8
JM
8961#endif
8962
8963/*** SPE floating-point extension ***/
1c97856d
AJ
8964#if defined(TARGET_PPC64)
8965#define GEN_SPEFPUOP_CONV_32_32(name) \
636aa200 8966static inline void gen_##name(DisasContext *ctx) \
0487d6a8 8967{ \
1c97856d
AJ
8968 TCGv_i32 t0; \
8969 TCGv t1; \
8970 t0 = tcg_temp_new_i32(); \
8971 tcg_gen_trunc_tl_i32(t0, cpu_gpr[rB(ctx->opcode)]); \
8e703949 8972 gen_helper_##name(t0, cpu_env, t0); \
1c97856d
AJ
8973 t1 = tcg_temp_new(); \
8974 tcg_gen_extu_i32_tl(t1, t0); \
8975 tcg_temp_free_i32(t0); \
8976 tcg_gen_andi_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rD(ctx->opcode)], \
8977 0xFFFFFFFF00000000ULL); \
8978 tcg_gen_or_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rD(ctx->opcode)], t1); \
8979 tcg_temp_free(t1); \
0487d6a8 8980}
1c97856d 8981#define GEN_SPEFPUOP_CONV_32_64(name) \
636aa200 8982static inline void gen_##name(DisasContext *ctx) \
1c97856d
AJ
8983{ \
8984 TCGv_i32 t0; \
8985 TCGv t1; \
8986 t0 = tcg_temp_new_i32(); \
8e703949 8987 gen_helper_##name(t0, cpu_env, cpu_gpr[rB(ctx->opcode)]); \
1c97856d
AJ
8988 t1 = tcg_temp_new(); \
8989 tcg_gen_extu_i32_tl(t1, t0); \
8990 tcg_temp_free_i32(t0); \
8991 tcg_gen_andi_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rD(ctx->opcode)], \
8992 0xFFFFFFFF00000000ULL); \
8993 tcg_gen_or_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rD(ctx->opcode)], t1); \
8994 tcg_temp_free(t1); \
8995}
8996#define GEN_SPEFPUOP_CONV_64_32(name) \
636aa200 8997static inline void gen_##name(DisasContext *ctx) \
1c97856d
AJ
8998{ \
8999 TCGv_i32 t0 = tcg_temp_new_i32(); \
9000 tcg_gen_trunc_tl_i32(t0, cpu_gpr[rB(ctx->opcode)]); \
8e703949 9001 gen_helper_##name(cpu_gpr[rD(ctx->opcode)], cpu_env, t0); \
1c97856d
AJ
9002 tcg_temp_free_i32(t0); \
9003}
9004#define GEN_SPEFPUOP_CONV_64_64(name) \
636aa200 9005static inline void gen_##name(DisasContext *ctx) \
1c97856d 9006{ \
8e703949
BS
9007 gen_helper_##name(cpu_gpr[rD(ctx->opcode)], cpu_env, \
9008 cpu_gpr[rB(ctx->opcode)]); \
1c97856d
AJ
9009}
9010#define GEN_SPEFPUOP_ARITH2_32_32(name) \
636aa200 9011static inline void gen_##name(DisasContext *ctx) \
57951c27 9012{ \
1c97856d
AJ
9013 TCGv_i32 t0, t1; \
9014 TCGv_i64 t2; \
57951c27 9015 if (unlikely(!ctx->spe_enabled)) { \
27a69bb0 9016 gen_exception(ctx, POWERPC_EXCP_SPEU); \
57951c27
AJ
9017 return; \
9018 } \
1c97856d
AJ
9019 t0 = tcg_temp_new_i32(); \
9020 t1 = tcg_temp_new_i32(); \
9021 tcg_gen_trunc_tl_i32(t0, cpu_gpr[rA(ctx->opcode)]); \
9022 tcg_gen_trunc_tl_i32(t1, cpu_gpr[rB(ctx->opcode)]); \
8e703949 9023 gen_helper_##name(t0, cpu_env, t0, t1); \
1c97856d
AJ
9024 tcg_temp_free_i32(t1); \
9025 t2 = tcg_temp_new(); \
9026 tcg_gen_extu_i32_tl(t2, t0); \
9027 tcg_temp_free_i32(t0); \
9028 tcg_gen_andi_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rD(ctx->opcode)], \
9029 0xFFFFFFFF00000000ULL); \
9030 tcg_gen_or_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rD(ctx->opcode)], t2); \
9031 tcg_temp_free(t2); \
57951c27 9032}
1c97856d 9033#define GEN_SPEFPUOP_ARITH2_64_64(name) \
636aa200 9034static inline void gen_##name(DisasContext *ctx) \
57951c27
AJ
9035{ \
9036 if (unlikely(!ctx->spe_enabled)) { \
27a69bb0 9037 gen_exception(ctx, POWERPC_EXCP_SPEU); \
57951c27
AJ
9038 return; \
9039 } \
8e703949
BS
9040 gen_helper_##name(cpu_gpr[rD(ctx->opcode)], cpu_env, \
9041 cpu_gpr[rA(ctx->opcode)], cpu_gpr[rB(ctx->opcode)]); \
57951c27 9042}
1c97856d 9043#define GEN_SPEFPUOP_COMP_32(name) \
636aa200 9044static inline void gen_##name(DisasContext *ctx) \
57951c27 9045{ \
1c97856d 9046 TCGv_i32 t0, t1; \
57951c27 9047 if (unlikely(!ctx->spe_enabled)) { \
27a69bb0 9048 gen_exception(ctx, POWERPC_EXCP_SPEU); \
57951c27
AJ
9049 return; \
9050 } \
1c97856d
AJ
9051 t0 = tcg_temp_new_i32(); \
9052 t1 = tcg_temp_new_i32(); \
9053 tcg_gen_trunc_tl_i32(t0, cpu_gpr[rA(ctx->opcode)]); \
9054 tcg_gen_trunc_tl_i32(t1, cpu_gpr[rB(ctx->opcode)]); \
8e703949 9055 gen_helper_##name(cpu_crf[crfD(ctx->opcode)], cpu_env, t0, t1); \
1c97856d
AJ
9056 tcg_temp_free_i32(t0); \
9057 tcg_temp_free_i32(t1); \
9058}
9059#define GEN_SPEFPUOP_COMP_64(name) \
636aa200 9060static inline void gen_##name(DisasContext *ctx) \
1c97856d
AJ
9061{ \
9062 if (unlikely(!ctx->spe_enabled)) { \
27a69bb0 9063 gen_exception(ctx, POWERPC_EXCP_SPEU); \
1c97856d
AJ
9064 return; \
9065 } \
8e703949 9066 gen_helper_##name(cpu_crf[crfD(ctx->opcode)], cpu_env, \
1c97856d
AJ
9067 cpu_gpr[rA(ctx->opcode)], cpu_gpr[rB(ctx->opcode)]); \
9068}
9069#else
9070#define GEN_SPEFPUOP_CONV_32_32(name) \
636aa200 9071static inline void gen_##name(DisasContext *ctx) \
1c97856d 9072{ \
8e703949
BS
9073 gen_helper_##name(cpu_gpr[rD(ctx->opcode)], cpu_env, \
9074 cpu_gpr[rB(ctx->opcode)]); \
57951c27 9075}
1c97856d 9076#define GEN_SPEFPUOP_CONV_32_64(name) \
636aa200 9077static inline void gen_##name(DisasContext *ctx) \
1c97856d
AJ
9078{ \
9079 TCGv_i64 t0 = tcg_temp_new_i64(); \
9080 gen_load_gpr64(t0, rB(ctx->opcode)); \
8e703949 9081 gen_helper_##name(cpu_gpr[rD(ctx->opcode)], cpu_env, t0); \
1c97856d
AJ
9082 tcg_temp_free_i64(t0); \
9083}
9084#define GEN_SPEFPUOP_CONV_64_32(name) \
636aa200 9085static inline void gen_##name(DisasContext *ctx) \
1c97856d
AJ
9086{ \
9087 TCGv_i64 t0 = tcg_temp_new_i64(); \
8e703949 9088 gen_helper_##name(t0, cpu_env, cpu_gpr[rB(ctx->opcode)]); \
1c97856d
AJ
9089 gen_store_gpr64(rD(ctx->opcode), t0); \
9090 tcg_temp_free_i64(t0); \
9091}
9092#define GEN_SPEFPUOP_CONV_64_64(name) \
636aa200 9093static inline void gen_##name(DisasContext *ctx) \
1c97856d
AJ
9094{ \
9095 TCGv_i64 t0 = tcg_temp_new_i64(); \
9096 gen_load_gpr64(t0, rB(ctx->opcode)); \
8e703949 9097 gen_helper_##name(t0, cpu_env, t0); \
1c97856d
AJ
9098 gen_store_gpr64(rD(ctx->opcode), t0); \
9099 tcg_temp_free_i64(t0); \
9100}
9101#define GEN_SPEFPUOP_ARITH2_32_32(name) \
636aa200 9102static inline void gen_##name(DisasContext *ctx) \
1c97856d
AJ
9103{ \
9104 if (unlikely(!ctx->spe_enabled)) { \
27a69bb0 9105 gen_exception(ctx, POWERPC_EXCP_SPEU); \
1c97856d
AJ
9106 return; \
9107 } \
8e703949 9108 gen_helper_##name(cpu_gpr[rD(ctx->opcode)], cpu_env, \
1c97856d
AJ
9109 cpu_gpr[rA(ctx->opcode)], cpu_gpr[rB(ctx->opcode)]); \
9110}
9111#define GEN_SPEFPUOP_ARITH2_64_64(name) \
636aa200 9112static inline void gen_##name(DisasContext *ctx) \
1c97856d
AJ
9113{ \
9114 TCGv_i64 t0, t1; \
9115 if (unlikely(!ctx->spe_enabled)) { \
27a69bb0 9116 gen_exception(ctx, POWERPC_EXCP_SPEU); \
1c97856d
AJ
9117 return; \
9118 } \
9119 t0 = tcg_temp_new_i64(); \
9120 t1 = tcg_temp_new_i64(); \
9121 gen_load_gpr64(t0, rA(ctx->opcode)); \
9122 gen_load_gpr64(t1, rB(ctx->opcode)); \
8e703949 9123 gen_helper_##name(t0, cpu_env, t0, t1); \
1c97856d
AJ
9124 gen_store_gpr64(rD(ctx->opcode), t0); \
9125 tcg_temp_free_i64(t0); \
9126 tcg_temp_free_i64(t1); \
9127}
9128#define GEN_SPEFPUOP_COMP_32(name) \
636aa200 9129static inline void gen_##name(DisasContext *ctx) \
1c97856d
AJ
9130{ \
9131 if (unlikely(!ctx->spe_enabled)) { \
27a69bb0 9132 gen_exception(ctx, POWERPC_EXCP_SPEU); \
1c97856d
AJ
9133 return; \
9134 } \
8e703949 9135 gen_helper_##name(cpu_crf[crfD(ctx->opcode)], cpu_env, \
1c97856d
AJ
9136 cpu_gpr[rA(ctx->opcode)], cpu_gpr[rB(ctx->opcode)]); \
9137}
9138#define GEN_SPEFPUOP_COMP_64(name) \
636aa200 9139static inline void gen_##name(DisasContext *ctx) \
1c97856d
AJ
9140{ \
9141 TCGv_i64 t0, t1; \
9142 if (unlikely(!ctx->spe_enabled)) { \
27a69bb0 9143 gen_exception(ctx, POWERPC_EXCP_SPEU); \
1c97856d
AJ
9144 return; \
9145 } \
9146 t0 = tcg_temp_new_i64(); \
9147 t1 = tcg_temp_new_i64(); \
9148 gen_load_gpr64(t0, rA(ctx->opcode)); \
9149 gen_load_gpr64(t1, rB(ctx->opcode)); \
8e703949 9150 gen_helper_##name(cpu_crf[crfD(ctx->opcode)], cpu_env, t0, t1); \
1c97856d
AJ
9151 tcg_temp_free_i64(t0); \
9152 tcg_temp_free_i64(t1); \
9153}
9154#endif
57951c27 9155
0487d6a8
JM
9156/* Single precision floating-point vectors operations */
9157/* Arithmetic */
1c97856d
AJ
9158GEN_SPEFPUOP_ARITH2_64_64(evfsadd);
9159GEN_SPEFPUOP_ARITH2_64_64(evfssub);
9160GEN_SPEFPUOP_ARITH2_64_64(evfsmul);
9161GEN_SPEFPUOP_ARITH2_64_64(evfsdiv);
636aa200 9162static inline void gen_evfsabs(DisasContext *ctx)
1c97856d
AJ
9163{
9164 if (unlikely(!ctx->spe_enabled)) {
27a69bb0 9165 gen_exception(ctx, POWERPC_EXCP_SPEU);
1c97856d
AJ
9166 return;
9167 }
9168#if defined(TARGET_PPC64)
6d5c34fa 9169 tcg_gen_andi_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)], ~0x8000000080000000LL);
1c97856d 9170#else
6d5c34fa
MP
9171 tcg_gen_andi_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)], ~0x80000000);
9172 tcg_gen_andi_tl(cpu_gprh[rD(ctx->opcode)], cpu_gprh[rA(ctx->opcode)], ~0x80000000);
1c97856d
AJ
9173#endif
9174}
636aa200 9175static inline void gen_evfsnabs(DisasContext *ctx)
1c97856d
AJ
9176{
9177 if (unlikely(!ctx->spe_enabled)) {
27a69bb0 9178 gen_exception(ctx, POWERPC_EXCP_SPEU);
1c97856d
AJ
9179 return;
9180 }
9181#if defined(TARGET_PPC64)
6d5c34fa 9182 tcg_gen_ori_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)], 0x8000000080000000LL);
1c97856d 9183#else
6d5c34fa
MP
9184 tcg_gen_ori_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)], 0x80000000);
9185 tcg_gen_ori_tl(cpu_gprh[rD(ctx->opcode)], cpu_gprh[rA(ctx->opcode)], 0x80000000);
1c97856d
AJ
9186#endif
9187}
636aa200 9188static inline void gen_evfsneg(DisasContext *ctx)
1c97856d
AJ
9189{
9190 if (unlikely(!ctx->spe_enabled)) {
27a69bb0 9191 gen_exception(ctx, POWERPC_EXCP_SPEU);
1c97856d
AJ
9192 return;
9193 }
9194#if defined(TARGET_PPC64)
6d5c34fa 9195 tcg_gen_xori_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)], 0x8000000080000000LL);
1c97856d 9196#else
6d5c34fa
MP
9197 tcg_gen_xori_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)], 0x80000000);
9198 tcg_gen_xori_tl(cpu_gprh[rD(ctx->opcode)], cpu_gprh[rA(ctx->opcode)], 0x80000000);
1c97856d
AJ
9199#endif
9200}
9201
0487d6a8 9202/* Conversion */
1c97856d
AJ
9203GEN_SPEFPUOP_CONV_64_64(evfscfui);
9204GEN_SPEFPUOP_CONV_64_64(evfscfsi);
9205GEN_SPEFPUOP_CONV_64_64(evfscfuf);
9206GEN_SPEFPUOP_CONV_64_64(evfscfsf);
9207GEN_SPEFPUOP_CONV_64_64(evfsctui);
9208GEN_SPEFPUOP_CONV_64_64(evfsctsi);
9209GEN_SPEFPUOP_CONV_64_64(evfsctuf);
9210GEN_SPEFPUOP_CONV_64_64(evfsctsf);
9211GEN_SPEFPUOP_CONV_64_64(evfsctuiz);
9212GEN_SPEFPUOP_CONV_64_64(evfsctsiz);
9213
0487d6a8 9214/* Comparison */
1c97856d
AJ
9215GEN_SPEFPUOP_COMP_64(evfscmpgt);
9216GEN_SPEFPUOP_COMP_64(evfscmplt);
9217GEN_SPEFPUOP_COMP_64(evfscmpeq);
9218GEN_SPEFPUOP_COMP_64(evfststgt);
9219GEN_SPEFPUOP_COMP_64(evfststlt);
9220GEN_SPEFPUOP_COMP_64(evfststeq);
0487d6a8
JM
9221
9222/* Opcodes definitions */
70560da7
FC
9223GEN_SPE(evfsadd, evfssub, 0x00, 0x0A, 0x00000000, 0x00000000, PPC_SPE_SINGLE); //
9224GEN_SPE(evfsabs, evfsnabs, 0x02, 0x0A, 0x0000F800, 0x0000F800, PPC_SPE_SINGLE); //
9225GEN_SPE(evfsneg, speundef, 0x03, 0x0A, 0x0000F800, 0xFFFFFFFF, PPC_SPE_SINGLE); //
9226GEN_SPE(evfsmul, evfsdiv, 0x04, 0x0A, 0x00000000, 0x00000000, PPC_SPE_SINGLE); //
9227GEN_SPE(evfscmpgt, evfscmplt, 0x06, 0x0A, 0x00600000, 0x00600000, PPC_SPE_SINGLE); //
9228GEN_SPE(evfscmpeq, speundef, 0x07, 0x0A, 0x00600000, 0xFFFFFFFF, PPC_SPE_SINGLE); //
9229GEN_SPE(evfscfui, evfscfsi, 0x08, 0x0A, 0x00180000, 0x00180000, PPC_SPE_SINGLE); //
9230GEN_SPE(evfscfuf, evfscfsf, 0x09, 0x0A, 0x00180000, 0x00180000, PPC_SPE_SINGLE); //
9231GEN_SPE(evfsctui, evfsctsi, 0x0A, 0x0A, 0x00180000, 0x00180000, PPC_SPE_SINGLE); //
9232GEN_SPE(evfsctuf, evfsctsf, 0x0B, 0x0A, 0x00180000, 0x00180000, PPC_SPE_SINGLE); //
9233GEN_SPE(evfsctuiz, speundef, 0x0C, 0x0A, 0x00180000, 0xFFFFFFFF, PPC_SPE_SINGLE); //
9234GEN_SPE(evfsctsiz, speundef, 0x0D, 0x0A, 0x00180000, 0xFFFFFFFF, PPC_SPE_SINGLE); //
9235GEN_SPE(evfststgt, evfststlt, 0x0E, 0x0A, 0x00600000, 0x00600000, PPC_SPE_SINGLE); //
9236GEN_SPE(evfststeq, speundef, 0x0F, 0x0A, 0x00600000, 0xFFFFFFFF, PPC_SPE_SINGLE); //
0487d6a8
JM
9237
9238/* Single precision floating-point operations */
9239/* Arithmetic */
1c97856d
AJ
9240GEN_SPEFPUOP_ARITH2_32_32(efsadd);
9241GEN_SPEFPUOP_ARITH2_32_32(efssub);
9242GEN_SPEFPUOP_ARITH2_32_32(efsmul);
9243GEN_SPEFPUOP_ARITH2_32_32(efsdiv);
636aa200 9244static inline void gen_efsabs(DisasContext *ctx)
1c97856d
AJ
9245{
9246 if (unlikely(!ctx->spe_enabled)) {
27a69bb0 9247 gen_exception(ctx, POWERPC_EXCP_SPEU);
1c97856d
AJ
9248 return;
9249 }
6d5c34fa 9250 tcg_gen_andi_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)], (target_long)~0x80000000LL);
1c97856d 9251}
636aa200 9252static inline void gen_efsnabs(DisasContext *ctx)
1c97856d
AJ
9253{
9254 if (unlikely(!ctx->spe_enabled)) {
27a69bb0 9255 gen_exception(ctx, POWERPC_EXCP_SPEU);
1c97856d
AJ
9256 return;
9257 }
6d5c34fa 9258 tcg_gen_ori_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)], 0x80000000);
1c97856d 9259}
636aa200 9260static inline void gen_efsneg(DisasContext *ctx)
1c97856d
AJ
9261{
9262 if (unlikely(!ctx->spe_enabled)) {
27a69bb0 9263 gen_exception(ctx, POWERPC_EXCP_SPEU);
1c97856d
AJ
9264 return;
9265 }
6d5c34fa 9266 tcg_gen_xori_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)], 0x80000000);
1c97856d
AJ
9267}
9268
0487d6a8 9269/* Conversion */
1c97856d
AJ
9270GEN_SPEFPUOP_CONV_32_32(efscfui);
9271GEN_SPEFPUOP_CONV_32_32(efscfsi);
9272GEN_SPEFPUOP_CONV_32_32(efscfuf);
9273GEN_SPEFPUOP_CONV_32_32(efscfsf);
9274GEN_SPEFPUOP_CONV_32_32(efsctui);
9275GEN_SPEFPUOP_CONV_32_32(efsctsi);
9276GEN_SPEFPUOP_CONV_32_32(efsctuf);
9277GEN_SPEFPUOP_CONV_32_32(efsctsf);
9278GEN_SPEFPUOP_CONV_32_32(efsctuiz);
9279GEN_SPEFPUOP_CONV_32_32(efsctsiz);
9280GEN_SPEFPUOP_CONV_32_64(efscfd);
9281
0487d6a8 9282/* Comparison */
1c97856d
AJ
9283GEN_SPEFPUOP_COMP_32(efscmpgt);
9284GEN_SPEFPUOP_COMP_32(efscmplt);
9285GEN_SPEFPUOP_COMP_32(efscmpeq);
9286GEN_SPEFPUOP_COMP_32(efststgt);
9287GEN_SPEFPUOP_COMP_32(efststlt);
9288GEN_SPEFPUOP_COMP_32(efststeq);
0487d6a8
JM
9289
9290/* Opcodes definitions */
70560da7
FC
9291GEN_SPE(efsadd, efssub, 0x00, 0x0B, 0x00000000, 0x00000000, PPC_SPE_SINGLE); //
9292GEN_SPE(efsabs, efsnabs, 0x02, 0x0B, 0x0000F800, 0x0000F800, PPC_SPE_SINGLE); //
9293GEN_SPE(efsneg, speundef, 0x03, 0x0B, 0x0000F800, 0xFFFFFFFF, PPC_SPE_SINGLE); //
9294GEN_SPE(efsmul, efsdiv, 0x04, 0x0B, 0x00000000, 0x00000000, PPC_SPE_SINGLE); //
9295GEN_SPE(efscmpgt, efscmplt, 0x06, 0x0B, 0x00600000, 0x00600000, PPC_SPE_SINGLE); //
9296GEN_SPE(efscmpeq, efscfd, 0x07, 0x0B, 0x00600000, 0x00180000, PPC_SPE_SINGLE); //
9297GEN_SPE(efscfui, efscfsi, 0x08, 0x0B, 0x00180000, 0x00180000, PPC_SPE_SINGLE); //
9298GEN_SPE(efscfuf, efscfsf, 0x09, 0x0B, 0x00180000, 0x00180000, PPC_SPE_SINGLE); //
9299GEN_SPE(efsctui, efsctsi, 0x0A, 0x0B, 0x00180000, 0x00180000, PPC_SPE_SINGLE); //
9300GEN_SPE(efsctuf, efsctsf, 0x0B, 0x0B, 0x00180000, 0x00180000, PPC_SPE_SINGLE); //
9301GEN_SPE(efsctuiz, speundef, 0x0C, 0x0B, 0x00180000, 0xFFFFFFFF, PPC_SPE_SINGLE); //
9302GEN_SPE(efsctsiz, speundef, 0x0D, 0x0B, 0x00180000, 0xFFFFFFFF, PPC_SPE_SINGLE); //
9303GEN_SPE(efststgt, efststlt, 0x0E, 0x0B, 0x00600000, 0x00600000, PPC_SPE_SINGLE); //
9304GEN_SPE(efststeq, speundef, 0x0F, 0x0B, 0x00600000, 0xFFFFFFFF, PPC_SPE_SINGLE); //
0487d6a8
JM
9305
9306/* Double precision floating-point operations */
9307/* Arithmetic */
1c97856d
AJ
9308GEN_SPEFPUOP_ARITH2_64_64(efdadd);
9309GEN_SPEFPUOP_ARITH2_64_64(efdsub);
9310GEN_SPEFPUOP_ARITH2_64_64(efdmul);
9311GEN_SPEFPUOP_ARITH2_64_64(efddiv);
636aa200 9312static inline void gen_efdabs(DisasContext *ctx)
1c97856d
AJ
9313{
9314 if (unlikely(!ctx->spe_enabled)) {
27a69bb0 9315 gen_exception(ctx, POWERPC_EXCP_SPEU);
1c97856d
AJ
9316 return;
9317 }
9318#if defined(TARGET_PPC64)
6d5c34fa 9319 tcg_gen_andi_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)], ~0x8000000000000000LL);
1c97856d 9320#else
6d5c34fa
MP
9321 tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)]);
9322 tcg_gen_andi_tl(cpu_gprh[rD(ctx->opcode)], cpu_gprh[rA(ctx->opcode)], ~0x80000000);
1c97856d
AJ
9323#endif
9324}
636aa200 9325static inline void gen_efdnabs(DisasContext *ctx)
1c97856d
AJ
9326{
9327 if (unlikely(!ctx->spe_enabled)) {
27a69bb0 9328 gen_exception(ctx, POWERPC_EXCP_SPEU);
1c97856d
AJ
9329 return;
9330 }
9331#if defined(TARGET_PPC64)
6d5c34fa 9332 tcg_gen_ori_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)], 0x8000000000000000LL);
1c97856d 9333#else
6d5c34fa
MP
9334 tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)]);
9335 tcg_gen_ori_tl(cpu_gprh[rD(ctx->opcode)], cpu_gprh[rA(ctx->opcode)], 0x80000000);
1c97856d
AJ
9336#endif
9337}
636aa200 9338static inline void gen_efdneg(DisasContext *ctx)
1c97856d
AJ
9339{
9340 if (unlikely(!ctx->spe_enabled)) {
27a69bb0 9341 gen_exception(ctx, POWERPC_EXCP_SPEU);
1c97856d
AJ
9342 return;
9343 }
9344#if defined(TARGET_PPC64)
6d5c34fa 9345 tcg_gen_xori_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)], 0x8000000000000000LL);
1c97856d 9346#else
6d5c34fa
MP
9347 tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)]);
9348 tcg_gen_xori_tl(cpu_gprh[rD(ctx->opcode)], cpu_gprh[rA(ctx->opcode)], 0x80000000);
1c97856d
AJ
9349#endif
9350}
9351
0487d6a8 9352/* Conversion */
1c97856d
AJ
9353GEN_SPEFPUOP_CONV_64_32(efdcfui);
9354GEN_SPEFPUOP_CONV_64_32(efdcfsi);
9355GEN_SPEFPUOP_CONV_64_32(efdcfuf);
9356GEN_SPEFPUOP_CONV_64_32(efdcfsf);
9357GEN_SPEFPUOP_CONV_32_64(efdctui);
9358GEN_SPEFPUOP_CONV_32_64(efdctsi);
9359GEN_SPEFPUOP_CONV_32_64(efdctuf);
9360GEN_SPEFPUOP_CONV_32_64(efdctsf);
9361GEN_SPEFPUOP_CONV_32_64(efdctuiz);
9362GEN_SPEFPUOP_CONV_32_64(efdctsiz);
9363GEN_SPEFPUOP_CONV_64_32(efdcfs);
9364GEN_SPEFPUOP_CONV_64_64(efdcfuid);
9365GEN_SPEFPUOP_CONV_64_64(efdcfsid);
9366GEN_SPEFPUOP_CONV_64_64(efdctuidz);
9367GEN_SPEFPUOP_CONV_64_64(efdctsidz);
0487d6a8 9368
0487d6a8 9369/* Comparison */
1c97856d
AJ
9370GEN_SPEFPUOP_COMP_64(efdcmpgt);
9371GEN_SPEFPUOP_COMP_64(efdcmplt);
9372GEN_SPEFPUOP_COMP_64(efdcmpeq);
9373GEN_SPEFPUOP_COMP_64(efdtstgt);
9374GEN_SPEFPUOP_COMP_64(efdtstlt);
9375GEN_SPEFPUOP_COMP_64(efdtsteq);
0487d6a8
JM
9376
9377/* Opcodes definitions */
70560da7
FC
9378GEN_SPE(efdadd, efdsub, 0x10, 0x0B, 0x00000000, 0x00000000, PPC_SPE_DOUBLE); //
9379GEN_SPE(efdcfuid, efdcfsid, 0x11, 0x0B, 0x00180000, 0x00180000, PPC_SPE_DOUBLE); //
9380GEN_SPE(efdabs, efdnabs, 0x12, 0x0B, 0x0000F800, 0x0000F800, PPC_SPE_DOUBLE); //
9381GEN_SPE(efdneg, speundef, 0x13, 0x0B, 0x0000F800, 0xFFFFFFFF, PPC_SPE_DOUBLE); //
9382GEN_SPE(efdmul, efddiv, 0x14, 0x0B, 0x00000000, 0x00000000, PPC_SPE_DOUBLE); //
9383GEN_SPE(efdctuidz, efdctsidz, 0x15, 0x0B, 0x00180000, 0x00180000, PPC_SPE_DOUBLE); //
9384GEN_SPE(efdcmpgt, efdcmplt, 0x16, 0x0B, 0x00600000, 0x00600000, PPC_SPE_DOUBLE); //
9385GEN_SPE(efdcmpeq, efdcfs, 0x17, 0x0B, 0x00600000, 0x00180000, PPC_SPE_DOUBLE); //
9386GEN_SPE(efdcfui, efdcfsi, 0x18, 0x0B, 0x00180000, 0x00180000, PPC_SPE_DOUBLE); //
9387GEN_SPE(efdcfuf, efdcfsf, 0x19, 0x0B, 0x00180000, 0x00180000, PPC_SPE_DOUBLE); //
9388GEN_SPE(efdctui, efdctsi, 0x1A, 0x0B, 0x00180000, 0x00180000, PPC_SPE_DOUBLE); //
9389GEN_SPE(efdctuf, efdctsf, 0x1B, 0x0B, 0x00180000, 0x00180000, PPC_SPE_DOUBLE); //
9390GEN_SPE(efdctuiz, speundef, 0x1C, 0x0B, 0x00180000, 0xFFFFFFFF, PPC_SPE_DOUBLE); //
9391GEN_SPE(efdctsiz, speundef, 0x1D, 0x0B, 0x00180000, 0xFFFFFFFF, PPC_SPE_DOUBLE); //
9392GEN_SPE(efdtstgt, efdtstlt, 0x1E, 0x0B, 0x00600000, 0x00600000, PPC_SPE_DOUBLE); //
9393GEN_SPE(efdtsteq, speundef, 0x1F, 0x0B, 0x00600000, 0xFFFFFFFF, PPC_SPE_DOUBLE); //
0487d6a8 9394
c227f099 9395static opcode_t opcodes[] = {
5c55ff99
BS
9396GEN_HANDLER(invalid, 0x00, 0x00, 0x00, 0xFFFFFFFF, PPC_NONE),
9397GEN_HANDLER(cmp, 0x1F, 0x00, 0x00, 0x00400000, PPC_INTEGER),
9398GEN_HANDLER(cmpi, 0x0B, 0xFF, 0xFF, 0x00400000, PPC_INTEGER),
9399GEN_HANDLER(cmpl, 0x1F, 0x00, 0x01, 0x00400000, PPC_INTEGER),
9400GEN_HANDLER(cmpli, 0x0A, 0xFF, 0xFF, 0x00400000, PPC_INTEGER),
fcfda20f 9401GEN_HANDLER_E(cmpb, 0x1F, 0x1C, 0x0F, 0x00000001, PPC_NONE, PPC2_ISA205),
5c55ff99
BS
9402GEN_HANDLER(isel, 0x1F, 0x0F, 0xFF, 0x00000001, PPC_ISEL),
9403GEN_HANDLER(addi, 0x0E, 0xFF, 0xFF, 0x00000000, PPC_INTEGER),
9404GEN_HANDLER(addic, 0x0C, 0xFF, 0xFF, 0x00000000, PPC_INTEGER),
9405GEN_HANDLER2(addic_, "addic.", 0x0D, 0xFF, 0xFF, 0x00000000, PPC_INTEGER),
9406GEN_HANDLER(addis, 0x0F, 0xFF, 0xFF, 0x00000000, PPC_INTEGER),
9407GEN_HANDLER(mulhw, 0x1F, 0x0B, 0x02, 0x00000400, PPC_INTEGER),
9408GEN_HANDLER(mulhwu, 0x1F, 0x0B, 0x00, 0x00000400, PPC_INTEGER),
9409GEN_HANDLER(mullw, 0x1F, 0x0B, 0x07, 0x00000000, PPC_INTEGER),
9410GEN_HANDLER(mullwo, 0x1F, 0x0B, 0x17, 0x00000000, PPC_INTEGER),
9411GEN_HANDLER(mulli, 0x07, 0xFF, 0xFF, 0x00000000, PPC_INTEGER),
9412#if defined(TARGET_PPC64)
9413GEN_HANDLER(mulld, 0x1F, 0x09, 0x07, 0x00000000, PPC_64B),
9414#endif
9415GEN_HANDLER(neg, 0x1F, 0x08, 0x03, 0x0000F800, PPC_INTEGER),
9416GEN_HANDLER(nego, 0x1F, 0x08, 0x13, 0x0000F800, PPC_INTEGER),
9417GEN_HANDLER(subfic, 0x08, 0xFF, 0xFF, 0x00000000, PPC_INTEGER),
9418GEN_HANDLER2(andi_, "andi.", 0x1C, 0xFF, 0xFF, 0x00000000, PPC_INTEGER),
9419GEN_HANDLER2(andis_, "andis.", 0x1D, 0xFF, 0xFF, 0x00000000, PPC_INTEGER),
9420GEN_HANDLER(cntlzw, 0x1F, 0x1A, 0x00, 0x00000000, PPC_INTEGER),
9421GEN_HANDLER(or, 0x1F, 0x1C, 0x0D, 0x00000000, PPC_INTEGER),
9422GEN_HANDLER(xor, 0x1F, 0x1C, 0x09, 0x00000000, PPC_INTEGER),
9423GEN_HANDLER(ori, 0x18, 0xFF, 0xFF, 0x00000000, PPC_INTEGER),
9424GEN_HANDLER(oris, 0x19, 0xFF, 0xFF, 0x00000000, PPC_INTEGER),
9425GEN_HANDLER(xori, 0x1A, 0xFF, 0xFF, 0x00000000, PPC_INTEGER),
9426GEN_HANDLER(xoris, 0x1B, 0xFF, 0xFF, 0x00000000, PPC_INTEGER),
9427GEN_HANDLER(popcntb, 0x1F, 0x03, 0x03, 0x0000F801, PPC_POPCNTB),
eaabeef2 9428GEN_HANDLER(popcntw, 0x1F, 0x1A, 0x0b, 0x0000F801, PPC_POPCNTWD),
725bcec2 9429GEN_HANDLER_E(prtyw, 0x1F, 0x1A, 0x04, 0x0000F801, PPC_NONE, PPC2_ISA205),
5c55ff99 9430#if defined(TARGET_PPC64)
eaabeef2 9431GEN_HANDLER(popcntd, 0x1F, 0x1A, 0x0F, 0x0000F801, PPC_POPCNTWD),
5c55ff99 9432GEN_HANDLER(cntlzd, 0x1F, 0x1A, 0x01, 0x00000000, PPC_64B),
725bcec2 9433GEN_HANDLER_E(prtyd, 0x1F, 0x1A, 0x05, 0x0000F801, PPC_NONE, PPC2_ISA205),
5c55ff99
BS
9434#endif
9435GEN_HANDLER(rlwimi, 0x14, 0xFF, 0xFF, 0x00000000, PPC_INTEGER),
9436GEN_HANDLER(rlwinm, 0x15, 0xFF, 0xFF, 0x00000000, PPC_INTEGER),
9437GEN_HANDLER(rlwnm, 0x17, 0xFF, 0xFF, 0x00000000, PPC_INTEGER),
9438GEN_HANDLER(slw, 0x1F, 0x18, 0x00, 0x00000000, PPC_INTEGER),
9439GEN_HANDLER(sraw, 0x1F, 0x18, 0x18, 0x00000000, PPC_INTEGER),
9440GEN_HANDLER(srawi, 0x1F, 0x18, 0x19, 0x00000000, PPC_INTEGER),
9441GEN_HANDLER(srw, 0x1F, 0x18, 0x10, 0x00000000, PPC_INTEGER),
9442#if defined(TARGET_PPC64)
9443GEN_HANDLER(sld, 0x1F, 0x1B, 0x00, 0x00000000, PPC_64B),
9444GEN_HANDLER(srad, 0x1F, 0x1A, 0x18, 0x00000000, PPC_64B),
9445GEN_HANDLER2(sradi0, "sradi", 0x1F, 0x1A, 0x19, 0x00000000, PPC_64B),
9446GEN_HANDLER2(sradi1, "sradi", 0x1F, 0x1B, 0x19, 0x00000000, PPC_64B),
9447GEN_HANDLER(srd, 0x1F, 0x1B, 0x10, 0x00000000, PPC_64B),
9448#endif
9449GEN_HANDLER(frsqrtes, 0x3B, 0x1A, 0xFF, 0x001F07C0, PPC_FLOAT_FRSQRTES),
9450GEN_HANDLER(fsqrt, 0x3F, 0x16, 0xFF, 0x001F07C0, PPC_FLOAT_FSQRT),
9451GEN_HANDLER(fsqrts, 0x3B, 0x16, 0xFF, 0x001F07C0, PPC_FLOAT_FSQRT),
9452GEN_HANDLER(fcmpo, 0x3F, 0x00, 0x01, 0x00600001, PPC_FLOAT),
9453GEN_HANDLER(fcmpu, 0x3F, 0x00, 0x00, 0x00600001, PPC_FLOAT),
bf45a2e6 9454GEN_HANDLER(fabs, 0x3F, 0x08, 0x08, 0x001F0000, PPC_FLOAT),
5c55ff99 9455GEN_HANDLER(fmr, 0x3F, 0x08, 0x02, 0x001F0000, PPC_FLOAT),
bf45a2e6
AJ
9456GEN_HANDLER(fnabs, 0x3F, 0x08, 0x04, 0x001F0000, PPC_FLOAT),
9457GEN_HANDLER(fneg, 0x3F, 0x08, 0x01, 0x001F0000, PPC_FLOAT),
f0332888 9458GEN_HANDLER_E(fcpsgn, 0x3F, 0x08, 0x00, 0x00000000, PPC_NONE, PPC2_ISA205),
097ec5d8
TM
9459GEN_HANDLER_E(fmrgew, 0x3F, 0x06, 0x1E, 0x00000001, PPC_NONE, PPC2_VSX207),
9460GEN_HANDLER_E(fmrgow, 0x3F, 0x06, 0x1A, 0x00000001, PPC_NONE, PPC2_VSX207),
5c55ff99
BS
9461GEN_HANDLER(mcrfs, 0x3F, 0x00, 0x02, 0x0063F801, PPC_FLOAT),
9462GEN_HANDLER(mffs, 0x3F, 0x07, 0x12, 0x001FF800, PPC_FLOAT),
9463GEN_HANDLER(mtfsb0, 0x3F, 0x06, 0x02, 0x001FF800, PPC_FLOAT),
9464GEN_HANDLER(mtfsb1, 0x3F, 0x06, 0x01, 0x001FF800, PPC_FLOAT),
7d08d856
AJ
9465GEN_HANDLER(mtfsf, 0x3F, 0x07, 0x16, 0x00000000, PPC_FLOAT),
9466GEN_HANDLER(mtfsfi, 0x3F, 0x06, 0x04, 0x006e0800, PPC_FLOAT),
5c55ff99
BS
9467#if defined(TARGET_PPC64)
9468GEN_HANDLER(ld, 0x3A, 0xFF, 0xFF, 0x00000000, PPC_64B),
9469GEN_HANDLER(lq, 0x38, 0xFF, 0xFF, 0x00000000, PPC_64BX),
9470GEN_HANDLER(std, 0x3E, 0xFF, 0xFF, 0x00000000, PPC_64B),
9471#endif
9472GEN_HANDLER(lmw, 0x2E, 0xFF, 0xFF, 0x00000000, PPC_INTEGER),
9473GEN_HANDLER(stmw, 0x2F, 0xFF, 0xFF, 0x00000000, PPC_INTEGER),
9474GEN_HANDLER(lswi, 0x1F, 0x15, 0x12, 0x00000001, PPC_STRING),
9475GEN_HANDLER(lswx, 0x1F, 0x15, 0x10, 0x00000001, PPC_STRING),
9476GEN_HANDLER(stswi, 0x1F, 0x15, 0x16, 0x00000001, PPC_STRING),
9477GEN_HANDLER(stswx, 0x1F, 0x15, 0x14, 0x00000001, PPC_STRING),
9478GEN_HANDLER(eieio, 0x1F, 0x16, 0x1A, 0x03FFF801, PPC_MEM_EIEIO),
9479GEN_HANDLER(isync, 0x13, 0x16, 0x04, 0x03FFF801, PPC_MEM),
f844c817 9480GEN_HANDLER(lwarx, 0x1F, 0x14, 0x00, 0x00000000, PPC_RES),
5c55ff99
BS
9481GEN_HANDLER2(stwcx_, "stwcx.", 0x1F, 0x16, 0x04, 0x00000000, PPC_RES),
9482#if defined(TARGET_PPC64)
f844c817 9483GEN_HANDLER(ldarx, 0x1F, 0x14, 0x02, 0x00000000, PPC_64B),
5c55ff99
BS
9484GEN_HANDLER2(stdcx_, "stdcx.", 0x1F, 0x16, 0x06, 0x00000000, PPC_64B),
9485#endif
9486GEN_HANDLER(sync, 0x1F, 0x16, 0x12, 0x039FF801, PPC_MEM_SYNC),
9487GEN_HANDLER(wait, 0x1F, 0x1E, 0x01, 0x03FFF801, PPC_WAIT),
9488GEN_HANDLER(b, 0x12, 0xFF, 0xFF, 0x00000000, PPC_FLOW),
9489GEN_HANDLER(bc, 0x10, 0xFF, 0xFF, 0x00000000, PPC_FLOW),
9490GEN_HANDLER(bcctr, 0x13, 0x10, 0x10, 0x00000000, PPC_FLOW),
9491GEN_HANDLER(bclr, 0x13, 0x10, 0x00, 0x00000000, PPC_FLOW),
9492GEN_HANDLER(mcrf, 0x13, 0x00, 0xFF, 0x00000001, PPC_INTEGER),
9493GEN_HANDLER(rfi, 0x13, 0x12, 0x01, 0x03FF8001, PPC_FLOW),
9494#if defined(TARGET_PPC64)
9495GEN_HANDLER(rfid, 0x13, 0x12, 0x00, 0x03FF8001, PPC_64B),
9496GEN_HANDLER(hrfid, 0x13, 0x12, 0x08, 0x03FF8001, PPC_64H),
9497#endif
9498GEN_HANDLER(sc, 0x11, 0xFF, 0xFF, 0x03FFF01D, PPC_FLOW),
9499GEN_HANDLER(tw, 0x1F, 0x04, 0x00, 0x00000001, PPC_FLOW),
9500GEN_HANDLER(twi, 0x03, 0xFF, 0xFF, 0x00000000, PPC_FLOW),
9501#if defined(TARGET_PPC64)
9502GEN_HANDLER(td, 0x1F, 0x04, 0x02, 0x00000001, PPC_64B),
9503GEN_HANDLER(tdi, 0x02, 0xFF, 0xFF, 0x00000000, PPC_64B),
9504#endif
9505GEN_HANDLER(mcrxr, 0x1F, 0x00, 0x10, 0x007FF801, PPC_MISC),
9506GEN_HANDLER(mfcr, 0x1F, 0x13, 0x00, 0x00000801, PPC_MISC),
9507GEN_HANDLER(mfmsr, 0x1F, 0x13, 0x02, 0x001FF801, PPC_MISC),
9508GEN_HANDLER(mfspr, 0x1F, 0x13, 0x0A, 0x00000001, PPC_MISC),
9509GEN_HANDLER(mftb, 0x1F, 0x13, 0x0B, 0x00000001, PPC_MFTB),
9510GEN_HANDLER(mtcrf, 0x1F, 0x10, 0x04, 0x00000801, PPC_MISC),
9511#if defined(TARGET_PPC64)
9512GEN_HANDLER(mtmsrd, 0x1F, 0x12, 0x05, 0x001EF801, PPC_64B),
9513#endif
9514GEN_HANDLER(mtmsr, 0x1F, 0x12, 0x04, 0x001FF801, PPC_MISC),
9515GEN_HANDLER(mtspr, 0x1F, 0x13, 0x0E, 0x00000001, PPC_MISC),
9516GEN_HANDLER(dcbf, 0x1F, 0x16, 0x02, 0x03C00001, PPC_CACHE),
9517GEN_HANDLER(dcbi, 0x1F, 0x16, 0x0E, 0x03E00001, PPC_CACHE),
9518GEN_HANDLER(dcbst, 0x1F, 0x16, 0x01, 0x03E00001, PPC_CACHE),
9519GEN_HANDLER(dcbt, 0x1F, 0x16, 0x08, 0x02000001, PPC_CACHE),
9520GEN_HANDLER(dcbtst, 0x1F, 0x16, 0x07, 0x02000001, PPC_CACHE),
8e33944f 9521GEN_HANDLER(dcbz, 0x1F, 0x16, 0x1F, 0x03C00001, PPC_CACHE_DCBZ),
5c55ff99
BS
9522GEN_HANDLER(dst, 0x1F, 0x16, 0x0A, 0x01800001, PPC_ALTIVEC),
9523GEN_HANDLER(dstst, 0x1F, 0x16, 0x0B, 0x02000001, PPC_ALTIVEC),
9524GEN_HANDLER(dss, 0x1F, 0x16, 0x19, 0x019FF801, PPC_ALTIVEC),
9525GEN_HANDLER(icbi, 0x1F, 0x16, 0x1E, 0x03E00001, PPC_CACHE_ICBI),
9526GEN_HANDLER(dcba, 0x1F, 0x16, 0x17, 0x03E00001, PPC_CACHE_DCBA),
9527GEN_HANDLER(mfsr, 0x1F, 0x13, 0x12, 0x0010F801, PPC_SEGMENT),
9528GEN_HANDLER(mfsrin, 0x1F, 0x13, 0x14, 0x001F0001, PPC_SEGMENT),
9529GEN_HANDLER(mtsr, 0x1F, 0x12, 0x06, 0x0010F801, PPC_SEGMENT),
9530GEN_HANDLER(mtsrin, 0x1F, 0x12, 0x07, 0x001F0001, PPC_SEGMENT),
9531#if defined(TARGET_PPC64)
9532GEN_HANDLER2(mfsr_64b, "mfsr", 0x1F, 0x13, 0x12, 0x0010F801, PPC_SEGMENT_64B),
9533GEN_HANDLER2(mfsrin_64b, "mfsrin", 0x1F, 0x13, 0x14, 0x001F0001,
9534 PPC_SEGMENT_64B),
9535GEN_HANDLER2(mtsr_64b, "mtsr", 0x1F, 0x12, 0x06, 0x0010F801, PPC_SEGMENT_64B),
9536GEN_HANDLER2(mtsrin_64b, "mtsrin", 0x1F, 0x12, 0x07, 0x001F0001,
9537 PPC_SEGMENT_64B),
efdef95f
DG
9538GEN_HANDLER2(slbmte, "slbmte", 0x1F, 0x12, 0x0C, 0x001F0001, PPC_SEGMENT_64B),
9539GEN_HANDLER2(slbmfee, "slbmfee", 0x1F, 0x13, 0x1C, 0x001F0001, PPC_SEGMENT_64B),
9540GEN_HANDLER2(slbmfev, "slbmfev", 0x1F, 0x13, 0x1A, 0x001F0001, PPC_SEGMENT_64B),
5c55ff99
BS
9541#endif
9542GEN_HANDLER(tlbia, 0x1F, 0x12, 0x0B, 0x03FFFC01, PPC_MEM_TLBIA),
9543GEN_HANDLER(tlbiel, 0x1F, 0x12, 0x08, 0x03FF0001, PPC_MEM_TLBIE),
9544GEN_HANDLER(tlbie, 0x1F, 0x12, 0x09, 0x03FF0001, PPC_MEM_TLBIE),
9545GEN_HANDLER(tlbsync, 0x1F, 0x16, 0x11, 0x03FFF801, PPC_MEM_TLBSYNC),
9546#if defined(TARGET_PPC64)
9547GEN_HANDLER(slbia, 0x1F, 0x12, 0x0F, 0x03FFFC01, PPC_SLBI),
9548GEN_HANDLER(slbie, 0x1F, 0x12, 0x0D, 0x03FF0001, PPC_SLBI),
9549#endif
9550GEN_HANDLER(eciwx, 0x1F, 0x16, 0x0D, 0x00000001, PPC_EXTERN),
9551GEN_HANDLER(ecowx, 0x1F, 0x16, 0x09, 0x00000001, PPC_EXTERN),
9552GEN_HANDLER(abs, 0x1F, 0x08, 0x0B, 0x0000F800, PPC_POWER_BR),
9553GEN_HANDLER(abso, 0x1F, 0x08, 0x1B, 0x0000F800, PPC_POWER_BR),
9554GEN_HANDLER(clcs, 0x1F, 0x10, 0x13, 0x0000F800, PPC_POWER_BR),
9555GEN_HANDLER(div, 0x1F, 0x0B, 0x0A, 0x00000000, PPC_POWER_BR),
9556GEN_HANDLER(divo, 0x1F, 0x0B, 0x1A, 0x00000000, PPC_POWER_BR),
9557GEN_HANDLER(divs, 0x1F, 0x0B, 0x0B, 0x00000000, PPC_POWER_BR),
9558GEN_HANDLER(divso, 0x1F, 0x0B, 0x1B, 0x00000000, PPC_POWER_BR),
9559GEN_HANDLER(doz, 0x1F, 0x08, 0x08, 0x00000000, PPC_POWER_BR),
9560GEN_HANDLER(dozo, 0x1F, 0x08, 0x18, 0x00000000, PPC_POWER_BR),
9561GEN_HANDLER(dozi, 0x09, 0xFF, 0xFF, 0x00000000, PPC_POWER_BR),
9562GEN_HANDLER(lscbx, 0x1F, 0x15, 0x08, 0x00000000, PPC_POWER_BR),
9563GEN_HANDLER(maskg, 0x1F, 0x1D, 0x00, 0x00000000, PPC_POWER_BR),
9564GEN_HANDLER(maskir, 0x1F, 0x1D, 0x10, 0x00000000, PPC_POWER_BR),
9565GEN_HANDLER(mul, 0x1F, 0x0B, 0x03, 0x00000000, PPC_POWER_BR),
9566GEN_HANDLER(mulo, 0x1F, 0x0B, 0x13, 0x00000000, PPC_POWER_BR),
9567GEN_HANDLER(nabs, 0x1F, 0x08, 0x0F, 0x00000000, PPC_POWER_BR),
9568GEN_HANDLER(nabso, 0x1F, 0x08, 0x1F, 0x00000000, PPC_POWER_BR),
9569GEN_HANDLER(rlmi, 0x16, 0xFF, 0xFF, 0x00000000, PPC_POWER_BR),
9570GEN_HANDLER(rrib, 0x1F, 0x19, 0x10, 0x00000000, PPC_POWER_BR),
9571GEN_HANDLER(sle, 0x1F, 0x19, 0x04, 0x00000000, PPC_POWER_BR),
9572GEN_HANDLER(sleq, 0x1F, 0x19, 0x06, 0x00000000, PPC_POWER_BR),
9573GEN_HANDLER(sliq, 0x1F, 0x18, 0x05, 0x00000000, PPC_POWER_BR),
9574GEN_HANDLER(slliq, 0x1F, 0x18, 0x07, 0x00000000, PPC_POWER_BR),
9575GEN_HANDLER(sllq, 0x1F, 0x18, 0x06, 0x00000000, PPC_POWER_BR),
9576GEN_HANDLER(slq, 0x1F, 0x18, 0x04, 0x00000000, PPC_POWER_BR),
9577GEN_HANDLER(sraiq, 0x1F, 0x18, 0x1D, 0x00000000, PPC_POWER_BR),
9578GEN_HANDLER(sraq, 0x1F, 0x18, 0x1C, 0x00000000, PPC_POWER_BR),
9579GEN_HANDLER(sre, 0x1F, 0x19, 0x14, 0x00000000, PPC_POWER_BR),
9580GEN_HANDLER(srea, 0x1F, 0x19, 0x1C, 0x00000000, PPC_POWER_BR),
9581GEN_HANDLER(sreq, 0x1F, 0x19, 0x16, 0x00000000, PPC_POWER_BR),
9582GEN_HANDLER(sriq, 0x1F, 0x18, 0x15, 0x00000000, PPC_POWER_BR),
9583GEN_HANDLER(srliq, 0x1F, 0x18, 0x17, 0x00000000, PPC_POWER_BR),
9584GEN_HANDLER(srlq, 0x1F, 0x18, 0x16, 0x00000000, PPC_POWER_BR),
9585GEN_HANDLER(srq, 0x1F, 0x18, 0x14, 0x00000000, PPC_POWER_BR),
9586GEN_HANDLER(dsa, 0x1F, 0x14, 0x13, 0x03FFF801, PPC_602_SPEC),
9587GEN_HANDLER(esa, 0x1F, 0x14, 0x12, 0x03FFF801, PPC_602_SPEC),
9588GEN_HANDLER(mfrom, 0x1F, 0x09, 0x08, 0x03E0F801, PPC_602_SPEC),
9589GEN_HANDLER2(tlbld_6xx, "tlbld", 0x1F, 0x12, 0x1E, 0x03FF0001, PPC_6xx_TLB),
9590GEN_HANDLER2(tlbli_6xx, "tlbli", 0x1F, 0x12, 0x1F, 0x03FF0001, PPC_6xx_TLB),
9591GEN_HANDLER2(tlbld_74xx, "tlbld", 0x1F, 0x12, 0x1E, 0x03FF0001, PPC_74xx_TLB),
9592GEN_HANDLER2(tlbli_74xx, "tlbli", 0x1F, 0x12, 0x1F, 0x03FF0001, PPC_74xx_TLB),
9593GEN_HANDLER(clf, 0x1F, 0x16, 0x03, 0x03E00000, PPC_POWER),
9594GEN_HANDLER(cli, 0x1F, 0x16, 0x0F, 0x03E00000, PPC_POWER),
9595GEN_HANDLER(dclst, 0x1F, 0x16, 0x13, 0x03E00000, PPC_POWER),
9596GEN_HANDLER(mfsri, 0x1F, 0x13, 0x13, 0x00000001, PPC_POWER),
9597GEN_HANDLER(rac, 0x1F, 0x12, 0x19, 0x00000001, PPC_POWER),
9598GEN_HANDLER(rfsvc, 0x13, 0x12, 0x02, 0x03FFF0001, PPC_POWER),
9599GEN_HANDLER(lfq, 0x38, 0xFF, 0xFF, 0x00000003, PPC_POWER2),
9600GEN_HANDLER(lfqu, 0x39, 0xFF, 0xFF, 0x00000003, PPC_POWER2),
9601GEN_HANDLER(lfqux, 0x1F, 0x17, 0x19, 0x00000001, PPC_POWER2),
9602GEN_HANDLER(lfqx, 0x1F, 0x17, 0x18, 0x00000001, PPC_POWER2),
9603GEN_HANDLER(stfq, 0x3C, 0xFF, 0xFF, 0x00000003, PPC_POWER2),
9604GEN_HANDLER(stfqu, 0x3D, 0xFF, 0xFF, 0x00000003, PPC_POWER2),
9605GEN_HANDLER(stfqux, 0x1F, 0x17, 0x1D, 0x00000001, PPC_POWER2),
9606GEN_HANDLER(stfqx, 0x1F, 0x17, 0x1C, 0x00000001, PPC_POWER2),
9607GEN_HANDLER(mfapidi, 0x1F, 0x13, 0x08, 0x0000F801, PPC_MFAPIDI),
9608GEN_HANDLER(tlbiva, 0x1F, 0x12, 0x18, 0x03FFF801, PPC_TLBIVA),
9609GEN_HANDLER(mfdcr, 0x1F, 0x03, 0x0A, 0x00000001, PPC_DCR),
9610GEN_HANDLER(mtdcr, 0x1F, 0x03, 0x0E, 0x00000001, PPC_DCR),
9611GEN_HANDLER(mfdcrx, 0x1F, 0x03, 0x08, 0x00000000, PPC_DCRX),
9612GEN_HANDLER(mtdcrx, 0x1F, 0x03, 0x0C, 0x00000000, PPC_DCRX),
9613GEN_HANDLER(mfdcrux, 0x1F, 0x03, 0x09, 0x00000000, PPC_DCRUX),
9614GEN_HANDLER(mtdcrux, 0x1F, 0x03, 0x0D, 0x00000000, PPC_DCRUX),
9615GEN_HANDLER(dccci, 0x1F, 0x06, 0x0E, 0x03E00001, PPC_4xx_COMMON),
9616GEN_HANDLER(dcread, 0x1F, 0x06, 0x0F, 0x00000001, PPC_4xx_COMMON),
9617GEN_HANDLER2(icbt_40x, "icbt", 0x1F, 0x06, 0x08, 0x03E00001, PPC_40x_ICBT),
9618GEN_HANDLER(iccci, 0x1F, 0x06, 0x1E, 0x00000001, PPC_4xx_COMMON),
9619GEN_HANDLER(icread, 0x1F, 0x06, 0x1F, 0x03E00001, PPC_4xx_COMMON),
9620GEN_HANDLER2(rfci_40x, "rfci", 0x13, 0x13, 0x01, 0x03FF8001, PPC_40x_EXCP),
01662f3e 9621GEN_HANDLER_E(rfci, 0x13, 0x13, 0x01, 0x03FF8001, PPC_BOOKE, PPC2_BOOKE206),
5c55ff99
BS
9622GEN_HANDLER(rfdi, 0x13, 0x07, 0x01, 0x03FF8001, PPC_RFDI),
9623GEN_HANDLER(rfmci, 0x13, 0x06, 0x01, 0x03FF8001, PPC_RFMCI),
9624GEN_HANDLER2(tlbre_40x, "tlbre", 0x1F, 0x12, 0x1D, 0x00000001, PPC_40x_TLB),
9625GEN_HANDLER2(tlbsx_40x, "tlbsx", 0x1F, 0x12, 0x1C, 0x00000000, PPC_40x_TLB),
9626GEN_HANDLER2(tlbwe_40x, "tlbwe", 0x1F, 0x12, 0x1E, 0x00000001, PPC_40x_TLB),
9627GEN_HANDLER2(tlbre_440, "tlbre", 0x1F, 0x12, 0x1D, 0x00000001, PPC_BOOKE),
9628GEN_HANDLER2(tlbsx_440, "tlbsx", 0x1F, 0x12, 0x1C, 0x00000000, PPC_BOOKE),
9629GEN_HANDLER2(tlbwe_440, "tlbwe", 0x1F, 0x12, 0x1E, 0x00000001, PPC_BOOKE),
01662f3e
AG
9630GEN_HANDLER2_E(tlbre_booke206, "tlbre", 0x1F, 0x12, 0x1D, 0x00000001,
9631 PPC_NONE, PPC2_BOOKE206),
9632GEN_HANDLER2_E(tlbsx_booke206, "tlbsx", 0x1F, 0x12, 0x1C, 0x00000000,
9633 PPC_NONE, PPC2_BOOKE206),
9634GEN_HANDLER2_E(tlbwe_booke206, "tlbwe", 0x1F, 0x12, 0x1E, 0x00000001,
9635 PPC_NONE, PPC2_BOOKE206),
9636GEN_HANDLER2_E(tlbivax_booke206, "tlbivax", 0x1F, 0x12, 0x18, 0x00000001,
9637 PPC_NONE, PPC2_BOOKE206),
6d3db821
AG
9638GEN_HANDLER2_E(tlbilx_booke206, "tlbilx", 0x1F, 0x12, 0x00, 0x03800001,
9639 PPC_NONE, PPC2_BOOKE206),
d5d11a39
AG
9640GEN_HANDLER2_E(msgsnd, "msgsnd", 0x1F, 0x0E, 0x06, 0x03ff0001,
9641 PPC_NONE, PPC2_PRCNTL),
9e0b5cb1
AG
9642GEN_HANDLER2_E(msgclr, "msgclr", 0x1F, 0x0E, 0x07, 0x03ff0001,
9643 PPC_NONE, PPC2_PRCNTL),
5c55ff99 9644GEN_HANDLER(wrtee, 0x1F, 0x03, 0x04, 0x000FFC01, PPC_WRTEE),
fbe73008 9645GEN_HANDLER(wrteei, 0x1F, 0x03, 0x05, 0x000E7C01, PPC_WRTEE),
5c55ff99 9646GEN_HANDLER(dlmzb, 0x1F, 0x0E, 0x02, 0x00000000, PPC_440_SPEC),
01662f3e
AG
9647GEN_HANDLER_E(mbar, 0x1F, 0x16, 0x1a, 0x001FF801,
9648 PPC_BOOKE, PPC2_BOOKE206),
dcb2b9e1 9649GEN_HANDLER(msync_4xx, 0x1F, 0x16, 0x12, 0x03FFF801, PPC_BOOKE),
01662f3e
AG
9650GEN_HANDLER2_E(icbt_440, "icbt", 0x1F, 0x16, 0x00, 0x03E00001,
9651 PPC_BOOKE, PPC2_BOOKE206),
5c55ff99
BS
9652GEN_HANDLER(lvsl, 0x1f, 0x06, 0x00, 0x00000001, PPC_ALTIVEC),
9653GEN_HANDLER(lvsr, 0x1f, 0x06, 0x01, 0x00000001, PPC_ALTIVEC),
9654GEN_HANDLER(mfvscr, 0x04, 0x2, 0x18, 0x001ff800, PPC_ALTIVEC),
9655GEN_HANDLER(mtvscr, 0x04, 0x2, 0x19, 0x03ff0000, PPC_ALTIVEC),
9656GEN_HANDLER(vsldoi, 0x04, 0x16, 0xFF, 0x00000400, PPC_ALTIVEC),
9657GEN_HANDLER(vmladduhm, 0x04, 0x11, 0xFF, 0x00000000, PPC_ALTIVEC),
9658GEN_HANDLER2(evsel0, "evsel", 0x04, 0x1c, 0x09, 0x00000000, PPC_SPE),
9659GEN_HANDLER2(evsel1, "evsel", 0x04, 0x1d, 0x09, 0x00000000, PPC_SPE),
9660GEN_HANDLER2(evsel2, "evsel", 0x04, 0x1e, 0x09, 0x00000000, PPC_SPE),
9661GEN_HANDLER2(evsel3, "evsel", 0x04, 0x1f, 0x09, 0x00000000, PPC_SPE),
9662
9663#undef GEN_INT_ARITH_ADD
9664#undef GEN_INT_ARITH_ADD_CONST
9665#define GEN_INT_ARITH_ADD(name, opc3, add_ca, compute_ca, compute_ov) \
9666GEN_HANDLER(name, 0x1F, 0x0A, opc3, 0x00000000, PPC_INTEGER),
9667#define GEN_INT_ARITH_ADD_CONST(name, opc3, const_val, \
9668 add_ca, compute_ca, compute_ov) \
9669GEN_HANDLER(name, 0x1F, 0x0A, opc3, 0x0000F800, PPC_INTEGER),
9670GEN_INT_ARITH_ADD(add, 0x08, 0, 0, 0)
9671GEN_INT_ARITH_ADD(addo, 0x18, 0, 0, 1)
9672GEN_INT_ARITH_ADD(addc, 0x00, 0, 1, 0)
9673GEN_INT_ARITH_ADD(addco, 0x10, 0, 1, 1)
9674GEN_INT_ARITH_ADD(adde, 0x04, 1, 1, 0)
9675GEN_INT_ARITH_ADD(addeo, 0x14, 1, 1, 1)
9676GEN_INT_ARITH_ADD_CONST(addme, 0x07, -1LL, 1, 1, 0)
9677GEN_INT_ARITH_ADD_CONST(addmeo, 0x17, -1LL, 1, 1, 1)
9678GEN_INT_ARITH_ADD_CONST(addze, 0x06, 0, 1, 1, 0)
9679GEN_INT_ARITH_ADD_CONST(addzeo, 0x16, 0, 1, 1, 1)
9680
9681#undef GEN_INT_ARITH_DIVW
9682#define GEN_INT_ARITH_DIVW(name, opc3, sign, compute_ov) \
9683GEN_HANDLER(name, 0x1F, 0x0B, opc3, 0x00000000, PPC_INTEGER)
9684GEN_INT_ARITH_DIVW(divwu, 0x0E, 0, 0),
9685GEN_INT_ARITH_DIVW(divwuo, 0x1E, 0, 1),
9686GEN_INT_ARITH_DIVW(divw, 0x0F, 1, 0),
9687GEN_INT_ARITH_DIVW(divwo, 0x1F, 1, 1),
9688
9689#if defined(TARGET_PPC64)
9690#undef GEN_INT_ARITH_DIVD
9691#define GEN_INT_ARITH_DIVD(name, opc3, sign, compute_ov) \
9692GEN_HANDLER(name, 0x1F, 0x09, opc3, 0x00000000, PPC_64B)
9693GEN_INT_ARITH_DIVD(divdu, 0x0E, 0, 0),
9694GEN_INT_ARITH_DIVD(divduo, 0x1E, 0, 1),
9695GEN_INT_ARITH_DIVD(divd, 0x0F, 1, 0),
9696GEN_INT_ARITH_DIVD(divdo, 0x1F, 1, 1),
9697
9698#undef GEN_INT_ARITH_MUL_HELPER
9699#define GEN_INT_ARITH_MUL_HELPER(name, opc3) \
9700GEN_HANDLER(name, 0x1F, 0x09, opc3, 0x00000000, PPC_64B)
9701GEN_INT_ARITH_MUL_HELPER(mulhdu, 0x00),
9702GEN_INT_ARITH_MUL_HELPER(mulhd, 0x02),
9703GEN_INT_ARITH_MUL_HELPER(mulldo, 0x17),
9704#endif
9705
9706#undef GEN_INT_ARITH_SUBF
9707#undef GEN_INT_ARITH_SUBF_CONST
9708#define GEN_INT_ARITH_SUBF(name, opc3, add_ca, compute_ca, compute_ov) \
9709GEN_HANDLER(name, 0x1F, 0x08, opc3, 0x00000000, PPC_INTEGER),
9710#define GEN_INT_ARITH_SUBF_CONST(name, opc3, const_val, \
9711 add_ca, compute_ca, compute_ov) \
9712GEN_HANDLER(name, 0x1F, 0x08, opc3, 0x0000F800, PPC_INTEGER),
9713GEN_INT_ARITH_SUBF(subf, 0x01, 0, 0, 0)
9714GEN_INT_ARITH_SUBF(subfo, 0x11, 0, 0, 1)
9715GEN_INT_ARITH_SUBF(subfc, 0x00, 0, 1, 0)
9716GEN_INT_ARITH_SUBF(subfco, 0x10, 0, 1, 1)
9717GEN_INT_ARITH_SUBF(subfe, 0x04, 1, 1, 0)
9718GEN_INT_ARITH_SUBF(subfeo, 0x14, 1, 1, 1)
9719GEN_INT_ARITH_SUBF_CONST(subfme, 0x07, -1LL, 1, 1, 0)
9720GEN_INT_ARITH_SUBF_CONST(subfmeo, 0x17, -1LL, 1, 1, 1)
9721GEN_INT_ARITH_SUBF_CONST(subfze, 0x06, 0, 1, 1, 0)
9722GEN_INT_ARITH_SUBF_CONST(subfzeo, 0x16, 0, 1, 1, 1)
9723
9724#undef GEN_LOGICAL1
9725#undef GEN_LOGICAL2
9726#define GEN_LOGICAL2(name, tcg_op, opc, type) \
9727GEN_HANDLER(name, 0x1F, 0x1C, opc, 0x00000000, type)
9728#define GEN_LOGICAL1(name, tcg_op, opc, type) \
9729GEN_HANDLER(name, 0x1F, 0x1A, opc, 0x00000000, type)
9730GEN_LOGICAL2(and, tcg_gen_and_tl, 0x00, PPC_INTEGER),
9731GEN_LOGICAL2(andc, tcg_gen_andc_tl, 0x01, PPC_INTEGER),
9732GEN_LOGICAL2(eqv, tcg_gen_eqv_tl, 0x08, PPC_INTEGER),
9733GEN_LOGICAL1(extsb, tcg_gen_ext8s_tl, 0x1D, PPC_INTEGER),
9734GEN_LOGICAL1(extsh, tcg_gen_ext16s_tl, 0x1C, PPC_INTEGER),
9735GEN_LOGICAL2(nand, tcg_gen_nand_tl, 0x0E, PPC_INTEGER),
9736GEN_LOGICAL2(nor, tcg_gen_nor_tl, 0x03, PPC_INTEGER),
9737GEN_LOGICAL2(orc, tcg_gen_orc_tl, 0x0C, PPC_INTEGER),
9738#if defined(TARGET_PPC64)
9739GEN_LOGICAL1(extsw, tcg_gen_ext32s_tl, 0x1E, PPC_64B),
9740#endif
9741
9742#if defined(TARGET_PPC64)
9743#undef GEN_PPC64_R2
9744#undef GEN_PPC64_R4
9745#define GEN_PPC64_R2(name, opc1, opc2) \
9746GEN_HANDLER2(name##0, stringify(name), opc1, opc2, 0xFF, 0x00000000, PPC_64B),\
9747GEN_HANDLER2(name##1, stringify(name), opc1, opc2 | 0x10, 0xFF, 0x00000000, \
9748 PPC_64B)
9749#define GEN_PPC64_R4(name, opc1, opc2) \
9750GEN_HANDLER2(name##0, stringify(name), opc1, opc2, 0xFF, 0x00000000, PPC_64B),\
9751GEN_HANDLER2(name##1, stringify(name), opc1, opc2 | 0x01, 0xFF, 0x00000000, \
9752 PPC_64B), \
9753GEN_HANDLER2(name##2, stringify(name), opc1, opc2 | 0x10, 0xFF, 0x00000000, \
9754 PPC_64B), \
9755GEN_HANDLER2(name##3, stringify(name), opc1, opc2 | 0x11, 0xFF, 0x00000000, \
9756 PPC_64B)
9757GEN_PPC64_R4(rldicl, 0x1E, 0x00),
9758GEN_PPC64_R4(rldicr, 0x1E, 0x02),
9759GEN_PPC64_R4(rldic, 0x1E, 0x04),
9760GEN_PPC64_R2(rldcl, 0x1E, 0x08),
9761GEN_PPC64_R2(rldcr, 0x1E, 0x09),
9762GEN_PPC64_R4(rldimi, 0x1E, 0x06),
9763#endif
9764
9765#undef _GEN_FLOAT_ACB
9766#undef GEN_FLOAT_ACB
9767#undef _GEN_FLOAT_AB
9768#undef GEN_FLOAT_AB
9769#undef _GEN_FLOAT_AC
9770#undef GEN_FLOAT_AC
9771#undef GEN_FLOAT_B
9772#undef GEN_FLOAT_BS
9773#define _GEN_FLOAT_ACB(name, op, op1, op2, isfloat, set_fprf, type) \
9774GEN_HANDLER(f##name, op1, op2, 0xFF, 0x00000000, type)
9775#define GEN_FLOAT_ACB(name, op2, set_fprf, type) \
9776_GEN_FLOAT_ACB(name, name, 0x3F, op2, 0, set_fprf, type), \
9777_GEN_FLOAT_ACB(name##s, name, 0x3B, op2, 1, set_fprf, type)
9778#define _GEN_FLOAT_AB(name, op, op1, op2, inval, isfloat, set_fprf, type) \
9779GEN_HANDLER(f##name, op1, op2, 0xFF, inval, type)
9780#define GEN_FLOAT_AB(name, op2, inval, set_fprf, type) \
9781_GEN_FLOAT_AB(name, name, 0x3F, op2, inval, 0, set_fprf, type), \
9782_GEN_FLOAT_AB(name##s, name, 0x3B, op2, inval, 1, set_fprf, type)
9783#define _GEN_FLOAT_AC(name, op, op1, op2, inval, isfloat, set_fprf, type) \
9784GEN_HANDLER(f##name, op1, op2, 0xFF, inval, type)
9785#define GEN_FLOAT_AC(name, op2, inval, set_fprf, type) \
9786_GEN_FLOAT_AC(name, name, 0x3F, op2, inval, 0, set_fprf, type), \
9787_GEN_FLOAT_AC(name##s, name, 0x3B, op2, inval, 1, set_fprf, type)
9788#define GEN_FLOAT_B(name, op2, op3, set_fprf, type) \
9789GEN_HANDLER(f##name, 0x3F, op2, op3, 0x001F0000, type)
9790#define GEN_FLOAT_BS(name, op1, op2, set_fprf, type) \
9791GEN_HANDLER(f##name, op1, op2, 0xFF, 0x001F07C0, type)
9792
9793GEN_FLOAT_AB(add, 0x15, 0x000007C0, 1, PPC_FLOAT),
9794GEN_FLOAT_AB(div, 0x12, 0x000007C0, 1, PPC_FLOAT),
9795GEN_FLOAT_AC(mul, 0x19, 0x0000F800, 1, PPC_FLOAT),
9796GEN_FLOAT_BS(re, 0x3F, 0x18, 1, PPC_FLOAT_EXT),
9797GEN_FLOAT_BS(res, 0x3B, 0x18, 1, PPC_FLOAT_FRES),
9798GEN_FLOAT_BS(rsqrte, 0x3F, 0x1A, 1, PPC_FLOAT_FRSQRTE),
9799_GEN_FLOAT_ACB(sel, sel, 0x3F, 0x17, 0, 0, PPC_FLOAT_FSEL),
9800GEN_FLOAT_AB(sub, 0x14, 0x000007C0, 1, PPC_FLOAT),
9801GEN_FLOAT_ACB(madd, 0x1D, 1, PPC_FLOAT),
9802GEN_FLOAT_ACB(msub, 0x1C, 1, PPC_FLOAT),
9803GEN_FLOAT_ACB(nmadd, 0x1F, 1, PPC_FLOAT),
9804GEN_FLOAT_ACB(nmsub, 0x1E, 1, PPC_FLOAT),
9805GEN_FLOAT_B(ctiw, 0x0E, 0x00, 0, PPC_FLOAT),
9806GEN_FLOAT_B(ctiwz, 0x0F, 0x00, 0, PPC_FLOAT),
9807GEN_FLOAT_B(rsp, 0x0C, 0x00, 1, PPC_FLOAT),
9808#if defined(TARGET_PPC64)
9809GEN_FLOAT_B(cfid, 0x0E, 0x1A, 1, PPC_64B),
9810GEN_FLOAT_B(ctid, 0x0E, 0x19, 0, PPC_64B),
9811GEN_FLOAT_B(ctidz, 0x0F, 0x19, 0, PPC_64B),
9812#endif
9813GEN_FLOAT_B(rin, 0x08, 0x0C, 1, PPC_FLOAT_EXT),
9814GEN_FLOAT_B(riz, 0x08, 0x0D, 1, PPC_FLOAT_EXT),
9815GEN_FLOAT_B(rip, 0x08, 0x0E, 1, PPC_FLOAT_EXT),
9816GEN_FLOAT_B(rim, 0x08, 0x0F, 1, PPC_FLOAT_EXT),
5c55ff99
BS
9817
9818#undef GEN_LD
9819#undef GEN_LDU
9820#undef GEN_LDUX
cd6e9320 9821#undef GEN_LDX_E
5c55ff99
BS
9822#undef GEN_LDS
9823#define GEN_LD(name, ldop, opc, type) \
9824GEN_HANDLER(name, opc, 0xFF, 0xFF, 0x00000000, type),
9825#define GEN_LDU(name, ldop, opc, type) \
9826GEN_HANDLER(name##u, opc, 0xFF, 0xFF, 0x00000000, type),
9827#define GEN_LDUX(name, ldop, opc2, opc3, type) \
9828GEN_HANDLER(name##ux, 0x1F, opc2, opc3, 0x00000001, type),
cd6e9320
TH
9829#define GEN_LDX_E(name, ldop, opc2, opc3, type, type2) \
9830GEN_HANDLER_E(name##x, 0x1F, opc2, opc3, 0x00000001, type, type2),
5c55ff99
BS
9831#define GEN_LDS(name, ldop, op, type) \
9832GEN_LD(name, ldop, op | 0x20, type) \
9833GEN_LDU(name, ldop, op | 0x21, type) \
9834GEN_LDUX(name, ldop, 0x17, op | 0x01, type) \
9835GEN_LDX(name, ldop, 0x17, op | 0x00, type)
9836
9837GEN_LDS(lbz, ld8u, 0x02, PPC_INTEGER)
9838GEN_LDS(lha, ld16s, 0x0A, PPC_INTEGER)
9839GEN_LDS(lhz, ld16u, 0x08, PPC_INTEGER)
9840GEN_LDS(lwz, ld32u, 0x00, PPC_INTEGER)
9841#if defined(TARGET_PPC64)
9842GEN_LDUX(lwa, ld32s, 0x15, 0x0B, PPC_64B)
9843GEN_LDX(lwa, ld32s, 0x15, 0x0A, PPC_64B)
9844GEN_LDUX(ld, ld64, 0x15, 0x01, PPC_64B)
9845GEN_LDX(ld, ld64, 0x15, 0x00, PPC_64B)
cd6e9320 9846GEN_LDX_E(ldbr, ld64ur, 0x14, 0x10, PPC_NONE, PPC2_DBRX)
5c55ff99
BS
9847#endif
9848GEN_LDX(lhbr, ld16ur, 0x16, 0x18, PPC_INTEGER)
9849GEN_LDX(lwbr, ld32ur, 0x16, 0x10, PPC_INTEGER)
9850
9851#undef GEN_ST
9852#undef GEN_STU
9853#undef GEN_STUX
cd6e9320 9854#undef GEN_STX_E
5c55ff99
BS
9855#undef GEN_STS
9856#define GEN_ST(name, stop, opc, type) \
9857GEN_HANDLER(name, opc, 0xFF, 0xFF, 0x00000000, type),
9858#define GEN_STU(name, stop, opc, type) \
9859GEN_HANDLER(stop##u, opc, 0xFF, 0xFF, 0x00000000, type),
9860#define GEN_STUX(name, stop, opc2, opc3, type) \
9861GEN_HANDLER(name##ux, 0x1F, opc2, opc3, 0x00000001, type),
cd6e9320
TH
9862#define GEN_STX_E(name, stop, opc2, opc3, type, type2) \
9863GEN_HANDLER_E(name##x, 0x1F, opc2, opc3, 0x00000001, type, type2),
5c55ff99
BS
9864#define GEN_STS(name, stop, op, type) \
9865GEN_ST(name, stop, op | 0x20, type) \
9866GEN_STU(name, stop, op | 0x21, type) \
9867GEN_STUX(name, stop, 0x17, op | 0x01, type) \
9868GEN_STX(name, stop, 0x17, op | 0x00, type)
9869
9870GEN_STS(stb, st8, 0x06, PPC_INTEGER)
9871GEN_STS(sth, st16, 0x0C, PPC_INTEGER)
9872GEN_STS(stw, st32, 0x04, PPC_INTEGER)
9873#if defined(TARGET_PPC64)
9874GEN_STUX(std, st64, 0x15, 0x05, PPC_64B)
9875GEN_STX(std, st64, 0x15, 0x04, PPC_64B)
cd6e9320 9876GEN_STX_E(stdbr, st64r, 0x14, 0x14, PPC_NONE, PPC2_DBRX)
5c55ff99
BS
9877#endif
9878GEN_STX(sthbr, st16r, 0x16, 0x1C, PPC_INTEGER)
9879GEN_STX(stwbr, st32r, 0x16, 0x14, PPC_INTEGER)
9880
9881#undef GEN_LDF
9882#undef GEN_LDUF
9883#undef GEN_LDUXF
9884#undef GEN_LDXF
9885#undef GEN_LDFS
9886#define GEN_LDF(name, ldop, opc, type) \
9887GEN_HANDLER(name, opc, 0xFF, 0xFF, 0x00000000, type),
9888#define GEN_LDUF(name, ldop, opc, type) \
9889GEN_HANDLER(name##u, opc, 0xFF, 0xFF, 0x00000000, type),
9890#define GEN_LDUXF(name, ldop, opc, type) \
9891GEN_HANDLER(name##ux, 0x1F, 0x17, opc, 0x00000001, type),
9892#define GEN_LDXF(name, ldop, opc2, opc3, type) \
9893GEN_HANDLER(name##x, 0x1F, opc2, opc3, 0x00000001, type),
9894#define GEN_LDFS(name, ldop, op, type) \
9895GEN_LDF(name, ldop, op | 0x20, type) \
9896GEN_LDUF(name, ldop, op | 0x21, type) \
9897GEN_LDUXF(name, ldop, op | 0x01, type) \
9898GEN_LDXF(name, ldop, 0x17, op | 0x00, type)
9899
9900GEN_LDFS(lfd, ld64, 0x12, PPC_FLOAT)
9901GEN_LDFS(lfs, ld32fs, 0x10, PPC_FLOAT)
199f830d 9902GEN_HANDLER_E(lfiwax, 0x1f, 0x17, 0x1a, 0x00000001, PPC_NONE, PPC2_ISA205),
05050ee8
AJ
9903GEN_HANDLER_E(lfdp, 0x39, 0xFF, 0xFF, 0x00200003, PPC_NONE, PPC2_ISA205),
9904GEN_HANDLER_E(lfdpx, 0x1F, 0x17, 0x18, 0x00200001, PPC_NONE, PPC2_ISA205),
5c55ff99
BS
9905
9906#undef GEN_STF
9907#undef GEN_STUF
9908#undef GEN_STUXF
9909#undef GEN_STXF
9910#undef GEN_STFS
9911#define GEN_STF(name, stop, opc, type) \
9912GEN_HANDLER(name, opc, 0xFF, 0xFF, 0x00000000, type),
9913#define GEN_STUF(name, stop, opc, type) \
9914GEN_HANDLER(name##u, opc, 0xFF, 0xFF, 0x00000000, type),
9915#define GEN_STUXF(name, stop, opc, type) \
9916GEN_HANDLER(name##ux, 0x1F, 0x17, opc, 0x00000001, type),
9917#define GEN_STXF(name, stop, opc2, opc3, type) \
9918GEN_HANDLER(name##x, 0x1F, opc2, opc3, 0x00000001, type),
9919#define GEN_STFS(name, stop, op, type) \
9920GEN_STF(name, stop, op | 0x20, type) \
9921GEN_STUF(name, stop, op | 0x21, type) \
9922GEN_STUXF(name, stop, op | 0x01, type) \
9923GEN_STXF(name, stop, 0x17, op | 0x00, type)
9924
9925GEN_STFS(stfd, st64, 0x16, PPC_FLOAT)
9926GEN_STFS(stfs, st32fs, 0x14, PPC_FLOAT)
9927GEN_STXF(stfiw, st32fiw, 0x17, 0x1E, PPC_FLOAT_STFIWX)
44bc0c4d
AJ
9928GEN_HANDLER_E(stfdp, 0x3D, 0xFF, 0xFF, 0x00200003, PPC_NONE, PPC2_ISA205),
9929GEN_HANDLER_E(stfdpx, 0x1F, 0x17, 0x1C, 0x00200001, PPC_NONE, PPC2_ISA205),
5c55ff99
BS
9930
9931#undef GEN_CRLOGIC
9932#define GEN_CRLOGIC(name, tcg_op, opc) \
9933GEN_HANDLER(name, 0x13, 0x01, opc, 0x00000001, PPC_INTEGER)
9934GEN_CRLOGIC(crand, tcg_gen_and_i32, 0x08),
9935GEN_CRLOGIC(crandc, tcg_gen_andc_i32, 0x04),
9936GEN_CRLOGIC(creqv, tcg_gen_eqv_i32, 0x09),
9937GEN_CRLOGIC(crnand, tcg_gen_nand_i32, 0x07),
9938GEN_CRLOGIC(crnor, tcg_gen_nor_i32, 0x01),
9939GEN_CRLOGIC(cror, tcg_gen_or_i32, 0x0E),
9940GEN_CRLOGIC(crorc, tcg_gen_orc_i32, 0x0D),
9941GEN_CRLOGIC(crxor, tcg_gen_xor_i32, 0x06),
9942
9943#undef GEN_MAC_HANDLER
9944#define GEN_MAC_HANDLER(name, opc2, opc3) \
9945GEN_HANDLER(name, 0x04, opc2, opc3, 0x00000000, PPC_405_MAC)
9946GEN_MAC_HANDLER(macchw, 0x0C, 0x05),
9947GEN_MAC_HANDLER(macchwo, 0x0C, 0x15),
9948GEN_MAC_HANDLER(macchws, 0x0C, 0x07),
9949GEN_MAC_HANDLER(macchwso, 0x0C, 0x17),
9950GEN_MAC_HANDLER(macchwsu, 0x0C, 0x06),
9951GEN_MAC_HANDLER(macchwsuo, 0x0C, 0x16),
9952GEN_MAC_HANDLER(macchwu, 0x0C, 0x04),
9953GEN_MAC_HANDLER(macchwuo, 0x0C, 0x14),
9954GEN_MAC_HANDLER(machhw, 0x0C, 0x01),
9955GEN_MAC_HANDLER(machhwo, 0x0C, 0x11),
9956GEN_MAC_HANDLER(machhws, 0x0C, 0x03),
9957GEN_MAC_HANDLER(machhwso, 0x0C, 0x13),
9958GEN_MAC_HANDLER(machhwsu, 0x0C, 0x02),
9959GEN_MAC_HANDLER(machhwsuo, 0x0C, 0x12),
9960GEN_MAC_HANDLER(machhwu, 0x0C, 0x00),
9961GEN_MAC_HANDLER(machhwuo, 0x0C, 0x10),
9962GEN_MAC_HANDLER(maclhw, 0x0C, 0x0D),
9963GEN_MAC_HANDLER(maclhwo, 0x0C, 0x1D),
9964GEN_MAC_HANDLER(maclhws, 0x0C, 0x0F),
9965GEN_MAC_HANDLER(maclhwso, 0x0C, 0x1F),
9966GEN_MAC_HANDLER(maclhwu, 0x0C, 0x0C),
9967GEN_MAC_HANDLER(maclhwuo, 0x0C, 0x1C),
9968GEN_MAC_HANDLER(maclhwsu, 0x0C, 0x0E),
9969GEN_MAC_HANDLER(maclhwsuo, 0x0C, 0x1E),
9970GEN_MAC_HANDLER(nmacchw, 0x0E, 0x05),
9971GEN_MAC_HANDLER(nmacchwo, 0x0E, 0x15),
9972GEN_MAC_HANDLER(nmacchws, 0x0E, 0x07),
9973GEN_MAC_HANDLER(nmacchwso, 0x0E, 0x17),
9974GEN_MAC_HANDLER(nmachhw, 0x0E, 0x01),
9975GEN_MAC_HANDLER(nmachhwo, 0x0E, 0x11),
9976GEN_MAC_HANDLER(nmachhws, 0x0E, 0x03),
9977GEN_MAC_HANDLER(nmachhwso, 0x0E, 0x13),
9978GEN_MAC_HANDLER(nmaclhw, 0x0E, 0x0D),
9979GEN_MAC_HANDLER(nmaclhwo, 0x0E, 0x1D),
9980GEN_MAC_HANDLER(nmaclhws, 0x0E, 0x0F),
9981GEN_MAC_HANDLER(nmaclhwso, 0x0E, 0x1F),
9982GEN_MAC_HANDLER(mulchw, 0x08, 0x05),
9983GEN_MAC_HANDLER(mulchwu, 0x08, 0x04),
9984GEN_MAC_HANDLER(mulhhw, 0x08, 0x01),
9985GEN_MAC_HANDLER(mulhhwu, 0x08, 0x00),
9986GEN_MAC_HANDLER(mullhw, 0x08, 0x0D),
9987GEN_MAC_HANDLER(mullhwu, 0x08, 0x0C),
9988
9989#undef GEN_VR_LDX
9990#undef GEN_VR_STX
9991#undef GEN_VR_LVE
9992#undef GEN_VR_STVE
9993#define GEN_VR_LDX(name, opc2, opc3) \
9994GEN_HANDLER(name, 0x1F, opc2, opc3, 0x00000001, PPC_ALTIVEC)
9995#define GEN_VR_STX(name, opc2, opc3) \
9996GEN_HANDLER(st##name, 0x1F, opc2, opc3, 0x00000001, PPC_ALTIVEC)
9997#define GEN_VR_LVE(name, opc2, opc3) \
9998 GEN_HANDLER(lve##name, 0x1F, opc2, opc3, 0x00000001, PPC_ALTIVEC)
9999#define GEN_VR_STVE(name, opc2, opc3) \
10000 GEN_HANDLER(stve##name, 0x1F, opc2, opc3, 0x00000001, PPC_ALTIVEC)
10001GEN_VR_LDX(lvx, 0x07, 0x03),
10002GEN_VR_LDX(lvxl, 0x07, 0x0B),
10003GEN_VR_LVE(bx, 0x07, 0x00),
10004GEN_VR_LVE(hx, 0x07, 0x01),
10005GEN_VR_LVE(wx, 0x07, 0x02),
10006GEN_VR_STX(svx, 0x07, 0x07),
10007GEN_VR_STX(svxl, 0x07, 0x0F),
10008GEN_VR_STVE(bx, 0x07, 0x04),
10009GEN_VR_STVE(hx, 0x07, 0x05),
10010GEN_VR_STVE(wx, 0x07, 0x06),
10011
10012#undef GEN_VX_LOGICAL
10013#define GEN_VX_LOGICAL(name, tcg_op, opc2, opc3) \
10014GEN_HANDLER(name, 0x04, opc2, opc3, 0x00000000, PPC_ALTIVEC)
10015GEN_VX_LOGICAL(vand, tcg_gen_and_i64, 2, 16),
10016GEN_VX_LOGICAL(vandc, tcg_gen_andc_i64, 2, 17),
10017GEN_VX_LOGICAL(vor, tcg_gen_or_i64, 2, 18),
10018GEN_VX_LOGICAL(vxor, tcg_gen_xor_i64, 2, 19),
10019GEN_VX_LOGICAL(vnor, tcg_gen_nor_i64, 2, 20),
10020
10021#undef GEN_VXFORM
10022#define GEN_VXFORM(name, opc2, opc3) \
10023GEN_HANDLER(name, 0x04, opc2, opc3, 0x00000000, PPC_ALTIVEC)
10024GEN_VXFORM(vaddubm, 0, 0),
10025GEN_VXFORM(vadduhm, 0, 1),
10026GEN_VXFORM(vadduwm, 0, 2),
10027GEN_VXFORM(vsububm, 0, 16),
10028GEN_VXFORM(vsubuhm, 0, 17),
10029GEN_VXFORM(vsubuwm, 0, 18),
10030GEN_VXFORM(vmaxub, 1, 0),
10031GEN_VXFORM(vmaxuh, 1, 1),
10032GEN_VXFORM(vmaxuw, 1, 2),
10033GEN_VXFORM(vmaxsb, 1, 4),
10034GEN_VXFORM(vmaxsh, 1, 5),
10035GEN_VXFORM(vmaxsw, 1, 6),
10036GEN_VXFORM(vminub, 1, 8),
10037GEN_VXFORM(vminuh, 1, 9),
10038GEN_VXFORM(vminuw, 1, 10),
10039GEN_VXFORM(vminsb, 1, 12),
10040GEN_VXFORM(vminsh, 1, 13),
10041GEN_VXFORM(vminsw, 1, 14),
10042GEN_VXFORM(vavgub, 1, 16),
10043GEN_VXFORM(vavguh, 1, 17),
10044GEN_VXFORM(vavguw, 1, 18),
10045GEN_VXFORM(vavgsb, 1, 20),
10046GEN_VXFORM(vavgsh, 1, 21),
10047GEN_VXFORM(vavgsw, 1, 22),
10048GEN_VXFORM(vmrghb, 6, 0),
10049GEN_VXFORM(vmrghh, 6, 1),
10050GEN_VXFORM(vmrghw, 6, 2),
10051GEN_VXFORM(vmrglb, 6, 4),
10052GEN_VXFORM(vmrglh, 6, 5),
10053GEN_VXFORM(vmrglw, 6, 6),
10054GEN_VXFORM(vmuloub, 4, 0),
10055GEN_VXFORM(vmulouh, 4, 1),
10056GEN_VXFORM(vmulosb, 4, 4),
10057GEN_VXFORM(vmulosh, 4, 5),
10058GEN_VXFORM(vmuleub, 4, 8),
10059GEN_VXFORM(vmuleuh, 4, 9),
10060GEN_VXFORM(vmulesb, 4, 12),
10061GEN_VXFORM(vmulesh, 4, 13),
10062GEN_VXFORM(vslb, 2, 4),
10063GEN_VXFORM(vslh, 2, 5),
10064GEN_VXFORM(vslw, 2, 6),
10065GEN_VXFORM(vsrb, 2, 8),
10066GEN_VXFORM(vsrh, 2, 9),
10067GEN_VXFORM(vsrw, 2, 10),
10068GEN_VXFORM(vsrab, 2, 12),
10069GEN_VXFORM(vsrah, 2, 13),
10070GEN_VXFORM(vsraw, 2, 14),
10071GEN_VXFORM(vslo, 6, 16),
10072GEN_VXFORM(vsro, 6, 17),
10073GEN_VXFORM(vaddcuw, 0, 6),
10074GEN_VXFORM(vsubcuw, 0, 22),
10075GEN_VXFORM(vaddubs, 0, 8),
10076GEN_VXFORM(vadduhs, 0, 9),
10077GEN_VXFORM(vadduws, 0, 10),
10078GEN_VXFORM(vaddsbs, 0, 12),
10079GEN_VXFORM(vaddshs, 0, 13),
10080GEN_VXFORM(vaddsws, 0, 14),
10081GEN_VXFORM(vsububs, 0, 24),
10082GEN_VXFORM(vsubuhs, 0, 25),
10083GEN_VXFORM(vsubuws, 0, 26),
10084GEN_VXFORM(vsubsbs, 0, 28),
10085GEN_VXFORM(vsubshs, 0, 29),
10086GEN_VXFORM(vsubsws, 0, 30),
10087GEN_VXFORM(vrlb, 2, 0),
10088GEN_VXFORM(vrlh, 2, 1),
10089GEN_VXFORM(vrlw, 2, 2),
10090GEN_VXFORM(vsl, 2, 7),
10091GEN_VXFORM(vsr, 2, 11),
10092GEN_VXFORM(vpkuhum, 7, 0),
10093GEN_VXFORM(vpkuwum, 7, 1),
10094GEN_VXFORM(vpkuhus, 7, 2),
10095GEN_VXFORM(vpkuwus, 7, 3),
10096GEN_VXFORM(vpkshus, 7, 4),
10097GEN_VXFORM(vpkswus, 7, 5),
10098GEN_VXFORM(vpkshss, 7, 6),
10099GEN_VXFORM(vpkswss, 7, 7),
10100GEN_VXFORM(vpkpx, 7, 12),
10101GEN_VXFORM(vsum4ubs, 4, 24),
10102GEN_VXFORM(vsum4sbs, 4, 28),
10103GEN_VXFORM(vsum4shs, 4, 25),
10104GEN_VXFORM(vsum2sws, 4, 26),
10105GEN_VXFORM(vsumsws, 4, 30),
10106GEN_VXFORM(vaddfp, 5, 0),
10107GEN_VXFORM(vsubfp, 5, 1),
10108GEN_VXFORM(vmaxfp, 5, 16),
10109GEN_VXFORM(vminfp, 5, 17),
10110
10111#undef GEN_VXRFORM1
10112#undef GEN_VXRFORM
10113#define GEN_VXRFORM1(opname, name, str, opc2, opc3) \
10114 GEN_HANDLER2(name, str, 0x4, opc2, opc3, 0x00000000, PPC_ALTIVEC),
10115#define GEN_VXRFORM(name, opc2, opc3) \
10116 GEN_VXRFORM1(name, name, #name, opc2, opc3) \
10117 GEN_VXRFORM1(name##_dot, name##_, #name ".", opc2, (opc3 | (0x1 << 4)))
10118GEN_VXRFORM(vcmpequb, 3, 0)
10119GEN_VXRFORM(vcmpequh, 3, 1)
10120GEN_VXRFORM(vcmpequw, 3, 2)
10121GEN_VXRFORM(vcmpgtsb, 3, 12)
10122GEN_VXRFORM(vcmpgtsh, 3, 13)
10123GEN_VXRFORM(vcmpgtsw, 3, 14)
10124GEN_VXRFORM(vcmpgtub, 3, 8)
10125GEN_VXRFORM(vcmpgtuh, 3, 9)
10126GEN_VXRFORM(vcmpgtuw, 3, 10)
10127GEN_VXRFORM(vcmpeqfp, 3, 3)
10128GEN_VXRFORM(vcmpgefp, 3, 7)
10129GEN_VXRFORM(vcmpgtfp, 3, 11)
10130GEN_VXRFORM(vcmpbfp, 3, 15)
10131
10132#undef GEN_VXFORM_SIMM
10133#define GEN_VXFORM_SIMM(name, opc2, opc3) \
10134 GEN_HANDLER(name, 0x04, opc2, opc3, 0x00000000, PPC_ALTIVEC)
10135GEN_VXFORM_SIMM(vspltisb, 6, 12),
10136GEN_VXFORM_SIMM(vspltish, 6, 13),
10137GEN_VXFORM_SIMM(vspltisw, 6, 14),
10138
10139#undef GEN_VXFORM_NOA
10140#define GEN_VXFORM_NOA(name, opc2, opc3) \
10141 GEN_HANDLER(name, 0x04, opc2, opc3, 0x001f0000, PPC_ALTIVEC)
10142GEN_VXFORM_NOA(vupkhsb, 7, 8),
10143GEN_VXFORM_NOA(vupkhsh, 7, 9),
10144GEN_VXFORM_NOA(vupklsb, 7, 10),
10145GEN_VXFORM_NOA(vupklsh, 7, 11),
10146GEN_VXFORM_NOA(vupkhpx, 7, 13),
10147GEN_VXFORM_NOA(vupklpx, 7, 15),
10148GEN_VXFORM_NOA(vrefp, 5, 4),
10149GEN_VXFORM_NOA(vrsqrtefp, 5, 5),
0bffbc6c 10150GEN_VXFORM_NOA(vexptefp, 5, 6),
5c55ff99
BS
10151GEN_VXFORM_NOA(vlogefp, 5, 7),
10152GEN_VXFORM_NOA(vrfim, 5, 8),
10153GEN_VXFORM_NOA(vrfin, 5, 9),
10154GEN_VXFORM_NOA(vrfip, 5, 10),
10155GEN_VXFORM_NOA(vrfiz, 5, 11),
10156
10157#undef GEN_VXFORM_UIMM
10158#define GEN_VXFORM_UIMM(name, opc2, opc3) \
10159 GEN_HANDLER(name, 0x04, opc2, opc3, 0x00000000, PPC_ALTIVEC)
10160GEN_VXFORM_UIMM(vspltb, 6, 8),
10161GEN_VXFORM_UIMM(vsplth, 6, 9),
10162GEN_VXFORM_UIMM(vspltw, 6, 10),
10163GEN_VXFORM_UIMM(vcfux, 5, 12),
10164GEN_VXFORM_UIMM(vcfsx, 5, 13),
10165GEN_VXFORM_UIMM(vctuxs, 5, 14),
10166GEN_VXFORM_UIMM(vctsxs, 5, 15),
10167
10168#undef GEN_VAFORM_PAIRED
10169#define GEN_VAFORM_PAIRED(name0, name1, opc2) \
10170 GEN_HANDLER(name0##_##name1, 0x04, opc2, 0xFF, 0x00000000, PPC_ALTIVEC)
10171GEN_VAFORM_PAIRED(vmhaddshs, vmhraddshs, 16),
10172GEN_VAFORM_PAIRED(vmsumubm, vmsummbm, 18),
10173GEN_VAFORM_PAIRED(vmsumuhm, vmsumuhs, 19),
10174GEN_VAFORM_PAIRED(vmsumshm, vmsumshs, 20),
10175GEN_VAFORM_PAIRED(vsel, vperm, 21),
10176GEN_VAFORM_PAIRED(vmaddfp, vnmsubfp, 23),
10177
fa1832d7 10178GEN_HANDLER_E(lxsdx, 0x1F, 0x0C, 0x12, 0, PPC_NONE, PPC2_VSX),
cac7f0ba
TM
10179GEN_HANDLER_E(lxsiwax, 0x1F, 0x0C, 0x02, 0, PPC_NONE, PPC2_VSX207),
10180GEN_HANDLER_E(lxsiwzx, 0x1F, 0x0C, 0x00, 0, PPC_NONE, PPC2_VSX207),
10181GEN_HANDLER_E(lxsspx, 0x1F, 0x0C, 0x10, 0, PPC_NONE, PPC2_VSX207),
304af367 10182GEN_HANDLER_E(lxvd2x, 0x1F, 0x0C, 0x1A, 0, PPC_NONE, PPC2_VSX),
ca03b467 10183GEN_HANDLER_E(lxvdsx, 0x1F, 0x0C, 0x0A, 0, PPC_NONE, PPC2_VSX),
897e61d1 10184GEN_HANDLER_E(lxvw4x, 0x1F, 0x0C, 0x18, 0, PPC_NONE, PPC2_VSX),
304af367 10185
9231ba9e 10186GEN_HANDLER_E(stxsdx, 0x1F, 0xC, 0x16, 0, PPC_NONE, PPC2_VSX),
e16a626b
TM
10187GEN_HANDLER_E(stxsiwx, 0x1F, 0xC, 0x04, 0, PPC_NONE, PPC2_VSX207),
10188GEN_HANDLER_E(stxsspx, 0x1F, 0xC, 0x14, 0, PPC_NONE, PPC2_VSX207),
fbed2478 10189GEN_HANDLER_E(stxvd2x, 0x1F, 0xC, 0x1E, 0, PPC_NONE, PPC2_VSX),
86e61ce3 10190GEN_HANDLER_E(stxvw4x, 0x1F, 0xC, 0x1C, 0, PPC_NONE, PPC2_VSX),
fbed2478 10191
f5c0f7f9
TM
10192GEN_HANDLER_E(mfvsrwz, 0x1F, 0x13, 0x03, 0x0000F800, PPC_NONE, PPC2_VSX207),
10193GEN_HANDLER_E(mtvsrwa, 0x1F, 0x13, 0x06, 0x0000F800, PPC_NONE, PPC2_VSX207),
10194GEN_HANDLER_E(mtvsrwz, 0x1F, 0x13, 0x07, 0x0000F800, PPC_NONE, PPC2_VSX207),
10195#if defined(TARGET_PPC64)
10196GEN_HANDLER_E(mfvsrd, 0x1F, 0x13, 0x01, 0x0000F800, PPC_NONE, PPC2_VSX207),
10197GEN_HANDLER_E(mtvsrd, 0x1F, 0x13, 0x05, 0x0000F800, PPC_NONE, PPC2_VSX207),
10198#endif
10199
df020ce0
TM
10200#undef GEN_XX2FORM
10201#define GEN_XX2FORM(name, opc2, opc3, fl2) \
10202GEN_HANDLER2_E(name, #name, 0x3C, opc2 | 0, opc3, 0, PPC_NONE, fl2), \
10203GEN_HANDLER2_E(name, #name, 0x3C, opc2 | 1, opc3, 0, PPC_NONE, fl2)
10204
10205#undef GEN_XX3FORM
10206#define GEN_XX3FORM(name, opc2, opc3, fl2) \
10207GEN_HANDLER2_E(name, #name, 0x3C, opc2 | 0, opc3, 0, PPC_NONE, fl2), \
10208GEN_HANDLER2_E(name, #name, 0x3C, opc2 | 1, opc3, 0, PPC_NONE, fl2), \
10209GEN_HANDLER2_E(name, #name, 0x3C, opc2 | 2, opc3, 0, PPC_NONE, fl2), \
10210GEN_HANDLER2_E(name, #name, 0x3C, opc2 | 3, opc3, 0, PPC_NONE, fl2)
10211
354a6dec
TM
10212#undef GEN_XX3_RC_FORM
10213#define GEN_XX3_RC_FORM(name, opc2, opc3, fl2) \
10214GEN_HANDLER2_E(name, #name, 0x3C, opc2 | 0x00, opc3 | 0x00, 0, PPC_NONE, fl2), \
10215GEN_HANDLER2_E(name, #name, 0x3C, opc2 | 0x01, opc3 | 0x00, 0, PPC_NONE, fl2), \
10216GEN_HANDLER2_E(name, #name, 0x3C, opc2 | 0x02, opc3 | 0x00, 0, PPC_NONE, fl2), \
10217GEN_HANDLER2_E(name, #name, 0x3C, opc2 | 0x03, opc3 | 0x00, 0, PPC_NONE, fl2), \
10218GEN_HANDLER2_E(name, #name, 0x3C, opc2 | 0x00, opc3 | 0x10, 0, PPC_NONE, fl2), \
10219GEN_HANDLER2_E(name, #name, 0x3C, opc2 | 0x01, opc3 | 0x10, 0, PPC_NONE, fl2), \
10220GEN_HANDLER2_E(name, #name, 0x3C, opc2 | 0x02, opc3 | 0x10, 0, PPC_NONE, fl2), \
10221GEN_HANDLER2_E(name, #name, 0x3C, opc2 | 0x03, opc3 | 0x10, 0, PPC_NONE, fl2)
10222
cd73f2c9
TM
10223#undef GEN_XX3FORM_DM
10224#define GEN_XX3FORM_DM(name, opc2, opc3) \
10225GEN_HANDLER2_E(name, #name, 0x3C, opc2|0x00, opc3|0x00, 0, PPC_NONE, PPC2_VSX),\
10226GEN_HANDLER2_E(name, #name, 0x3C, opc2|0x01, opc3|0x00, 0, PPC_NONE, PPC2_VSX),\
10227GEN_HANDLER2_E(name, #name, 0x3C, opc2|0x02, opc3|0x00, 0, PPC_NONE, PPC2_VSX),\
10228GEN_HANDLER2_E(name, #name, 0x3C, opc2|0x03, opc3|0x00, 0, PPC_NONE, PPC2_VSX),\
10229GEN_HANDLER2_E(name, #name, 0x3C, opc2|0x00, opc3|0x04, 0, PPC_NONE, PPC2_VSX),\
10230GEN_HANDLER2_E(name, #name, 0x3C, opc2|0x01, opc3|0x04, 0, PPC_NONE, PPC2_VSX),\
10231GEN_HANDLER2_E(name, #name, 0x3C, opc2|0x02, opc3|0x04, 0, PPC_NONE, PPC2_VSX),\
10232GEN_HANDLER2_E(name, #name, 0x3C, opc2|0x03, opc3|0x04, 0, PPC_NONE, PPC2_VSX),\
10233GEN_HANDLER2_E(name, #name, 0x3C, opc2|0x00, opc3|0x08, 0, PPC_NONE, PPC2_VSX),\
10234GEN_HANDLER2_E(name, #name, 0x3C, opc2|0x01, opc3|0x08, 0, PPC_NONE, PPC2_VSX),\
10235GEN_HANDLER2_E(name, #name, 0x3C, opc2|0x02, opc3|0x08, 0, PPC_NONE, PPC2_VSX),\
10236GEN_HANDLER2_E(name, #name, 0x3C, opc2|0x03, opc3|0x08, 0, PPC_NONE, PPC2_VSX),\
10237GEN_HANDLER2_E(name, #name, 0x3C, opc2|0x00, opc3|0x0C, 0, PPC_NONE, PPC2_VSX),\
10238GEN_HANDLER2_E(name, #name, 0x3C, opc2|0x01, opc3|0x0C, 0, PPC_NONE, PPC2_VSX),\
10239GEN_HANDLER2_E(name, #name, 0x3C, opc2|0x02, opc3|0x0C, 0, PPC_NONE, PPC2_VSX),\
10240GEN_HANDLER2_E(name, #name, 0x3C, opc2|0x03, opc3|0x0C, 0, PPC_NONE, PPC2_VSX)
10241
df020ce0
TM
10242GEN_XX2FORM(xsabsdp, 0x12, 0x15, PPC2_VSX),
10243GEN_XX2FORM(xsnabsdp, 0x12, 0x16, PPC2_VSX),
10244GEN_XX2FORM(xsnegdp, 0x12, 0x17, PPC2_VSX),
10245GEN_XX3FORM(xscpsgndp, 0x00, 0x16, PPC2_VSX),
10246
be574920
TM
10247GEN_XX2FORM(xvabsdp, 0x12, 0x1D, PPC2_VSX),
10248GEN_XX2FORM(xvnabsdp, 0x12, 0x1E, PPC2_VSX),
10249GEN_XX2FORM(xvnegdp, 0x12, 0x1F, PPC2_VSX),
10250GEN_XX3FORM(xvcpsgndp, 0x00, 0x1E, PPC2_VSX),
10251GEN_XX2FORM(xvabssp, 0x12, 0x19, PPC2_VSX),
10252GEN_XX2FORM(xvnabssp, 0x12, 0x1A, PPC2_VSX),
10253GEN_XX2FORM(xvnegsp, 0x12, 0x1B, PPC2_VSX),
10254GEN_XX3FORM(xvcpsgnsp, 0x00, 0x1A, PPC2_VSX),
79ca8a6a 10255
ee6e02c0
TM
10256GEN_XX3FORM(xsadddp, 0x00, 0x04, PPC2_VSX),
10257GEN_XX3FORM(xssubdp, 0x00, 0x05, PPC2_VSX),
5e591d88 10258GEN_XX3FORM(xsmuldp, 0x00, 0x06, PPC2_VSX),
4b98eeef 10259GEN_XX3FORM(xsdivdp, 0x00, 0x07, PPC2_VSX),
2009227f 10260GEN_XX2FORM(xsredp, 0x14, 0x05, PPC2_VSX),
d32404fe 10261GEN_XX2FORM(xssqrtdp, 0x16, 0x04, PPC2_VSX),
d3f9df8f 10262GEN_XX2FORM(xsrsqrtedp, 0x14, 0x04, PPC2_VSX),
bc80838f 10263GEN_XX3FORM(xstdivdp, 0x14, 0x07, PPC2_VSX),
5cb151ac 10264GEN_XX2FORM(xstsqrtdp, 0x14, 0x06, PPC2_VSX),
595c6eef
TM
10265GEN_XX3FORM(xsmaddadp, 0x04, 0x04, PPC2_VSX),
10266GEN_XX3FORM(xsmaddmdp, 0x04, 0x05, PPC2_VSX),
10267GEN_XX3FORM(xsmsubadp, 0x04, 0x06, PPC2_VSX),
10268GEN_XX3FORM(xsmsubmdp, 0x04, 0x07, PPC2_VSX),
10269GEN_XX3FORM(xsnmaddadp, 0x04, 0x14, PPC2_VSX),
10270GEN_XX3FORM(xsnmaddmdp, 0x04, 0x15, PPC2_VSX),
10271GEN_XX3FORM(xsnmsubadp, 0x04, 0x16, PPC2_VSX),
10272GEN_XX3FORM(xsnmsubmdp, 0x04, 0x17, PPC2_VSX),
4f17e9c7
TM
10273GEN_XX2FORM(xscmpodp, 0x0C, 0x05, PPC2_VSX),
10274GEN_XX2FORM(xscmpudp, 0x0C, 0x04, PPC2_VSX),
959e9c9d
TM
10275GEN_XX3FORM(xsmaxdp, 0x00, 0x14, PPC2_VSX),
10276GEN_XX3FORM(xsmindp, 0x00, 0x15, PPC2_VSX),
ed8ac568
TM
10277GEN_XX2FORM(xscvdpsp, 0x12, 0x10, PPC2_VSX),
10278GEN_XX2FORM(xscvspdp, 0x12, 0x14, PPC2_VSX),
5177d2ca
TM
10279GEN_XX2FORM(xscvdpsxds, 0x10, 0x15, PPC2_VSX),
10280GEN_XX2FORM(xscvdpsxws, 0x10, 0x05, PPC2_VSX),
10281GEN_XX2FORM(xscvdpuxds, 0x10, 0x14, PPC2_VSX),
10282GEN_XX2FORM(xscvdpuxws, 0x10, 0x04, PPC2_VSX),
10283GEN_XX2FORM(xscvsxddp, 0x10, 0x17, PPC2_VSX),
10284GEN_XX2FORM(xscvuxddp, 0x10, 0x16, PPC2_VSX),
88e33d08
TM
10285GEN_XX2FORM(xsrdpi, 0x12, 0x04, PPC2_VSX),
10286GEN_XX2FORM(xsrdpic, 0x16, 0x06, PPC2_VSX),
10287GEN_XX2FORM(xsrdpim, 0x12, 0x07, PPC2_VSX),
10288GEN_XX2FORM(xsrdpip, 0x12, 0x06, PPC2_VSX),
10289GEN_XX2FORM(xsrdpiz, 0x12, 0x05, PPC2_VSX),
ee6e02c0 10290
3fd0aadf
TM
10291GEN_XX3FORM(xsaddsp, 0x00, 0x00, PPC2_VSX207),
10292GEN_XX3FORM(xssubsp, 0x00, 0x01, PPC2_VSX207),
ab9408a2 10293GEN_XX3FORM(xsmulsp, 0x00, 0x02, PPC2_VSX207),
b24d0b47 10294GEN_XX3FORM(xsdivsp, 0x00, 0x03, PPC2_VSX207),
2c0c52ae 10295GEN_XX2FORM(xsresp, 0x14, 0x01, PPC2_VSX207),
3d1140bf 10296GEN_XX2FORM(xsrsp, 0x12, 0x11, PPC2_VSX207),
cea4e574 10297GEN_XX2FORM(xssqrtsp, 0x16, 0x00, PPC2_VSX207),
968e76bc 10298GEN_XX2FORM(xsrsqrtesp, 0x14, 0x00, PPC2_VSX207),
f53f81e0
TM
10299GEN_XX3FORM(xsmaddasp, 0x04, 0x00, PPC2_VSX207),
10300GEN_XX3FORM(xsmaddmsp, 0x04, 0x01, PPC2_VSX207),
10301GEN_XX3FORM(xsmsubasp, 0x04, 0x02, PPC2_VSX207),
10302GEN_XX3FORM(xsmsubmsp, 0x04, 0x03, PPC2_VSX207),
10303GEN_XX3FORM(xsnmaddasp, 0x04, 0x10, PPC2_VSX207),
10304GEN_XX3FORM(xsnmaddmsp, 0x04, 0x11, PPC2_VSX207),
10305GEN_XX3FORM(xsnmsubasp, 0x04, 0x12, PPC2_VSX207),
10306GEN_XX3FORM(xsnmsubmsp, 0x04, 0x13, PPC2_VSX207),
74698350
TM
10307GEN_XX2FORM(xscvsxdsp, 0x10, 0x13, PPC2_VSX207),
10308GEN_XX2FORM(xscvuxdsp, 0x10, 0x12, PPC2_VSX207),
3fd0aadf 10309
ee6e02c0
TM
10310GEN_XX3FORM(xvadddp, 0x00, 0x0C, PPC2_VSX),
10311GEN_XX3FORM(xvsubdp, 0x00, 0x0D, PPC2_VSX),
5e591d88 10312GEN_XX3FORM(xvmuldp, 0x00, 0x0E, PPC2_VSX),
4b98eeef 10313GEN_XX3FORM(xvdivdp, 0x00, 0x0F, PPC2_VSX),
2009227f 10314GEN_XX2FORM(xvredp, 0x14, 0x0D, PPC2_VSX),
d32404fe 10315GEN_XX2FORM(xvsqrtdp, 0x16, 0x0C, PPC2_VSX),
d3f9df8f 10316GEN_XX2FORM(xvrsqrtedp, 0x14, 0x0C, PPC2_VSX),
bc80838f 10317GEN_XX3FORM(xvtdivdp, 0x14, 0x0F, PPC2_VSX),
5cb151ac 10318GEN_XX2FORM(xvtsqrtdp, 0x14, 0x0E, PPC2_VSX),
595c6eef
TM
10319GEN_XX3FORM(xvmaddadp, 0x04, 0x0C, PPC2_VSX),
10320GEN_XX3FORM(xvmaddmdp, 0x04, 0x0D, PPC2_VSX),
10321GEN_XX3FORM(xvmsubadp, 0x04, 0x0E, PPC2_VSX),
10322GEN_XX3FORM(xvmsubmdp, 0x04, 0x0F, PPC2_VSX),
10323GEN_XX3FORM(xvnmaddadp, 0x04, 0x1C, PPC2_VSX),
10324GEN_XX3FORM(xvnmaddmdp, 0x04, 0x1D, PPC2_VSX),
10325GEN_XX3FORM(xvnmsubadp, 0x04, 0x1E, PPC2_VSX),
10326GEN_XX3FORM(xvnmsubmdp, 0x04, 0x1F, PPC2_VSX),
959e9c9d
TM
10327GEN_XX3FORM(xvmaxdp, 0x00, 0x1C, PPC2_VSX),
10328GEN_XX3FORM(xvmindp, 0x00, 0x1D, PPC2_VSX),
354a6dec
TM
10329GEN_XX3_RC_FORM(xvcmpeqdp, 0x0C, 0x0C, PPC2_VSX),
10330GEN_XX3_RC_FORM(xvcmpgtdp, 0x0C, 0x0D, PPC2_VSX),
10331GEN_XX3_RC_FORM(xvcmpgedp, 0x0C, 0x0E, PPC2_VSX),
ed8ac568 10332GEN_XX2FORM(xvcvdpsp, 0x12, 0x18, PPC2_VSX),
5177d2ca
TM
10333GEN_XX2FORM(xvcvdpsxds, 0x10, 0x1D, PPC2_VSX),
10334GEN_XX2FORM(xvcvdpsxws, 0x10, 0x0D, PPC2_VSX),
10335GEN_XX2FORM(xvcvdpuxds, 0x10, 0x1C, PPC2_VSX),
10336GEN_XX2FORM(xvcvdpuxws, 0x10, 0x0C, PPC2_VSX),
10337GEN_XX2FORM(xvcvsxddp, 0x10, 0x1F, PPC2_VSX),
10338GEN_XX2FORM(xvcvuxddp, 0x10, 0x1E, PPC2_VSX),
10339GEN_XX2FORM(xvcvsxwdp, 0x10, 0x0F, PPC2_VSX),
10340GEN_XX2FORM(xvcvuxwdp, 0x10, 0x0E, PPC2_VSX),
88e33d08
TM
10341GEN_XX2FORM(xvrdpi, 0x12, 0x0C, PPC2_VSX),
10342GEN_XX2FORM(xvrdpic, 0x16, 0x0E, PPC2_VSX),
10343GEN_XX2FORM(xvrdpim, 0x12, 0x0F, PPC2_VSX),
10344GEN_XX2FORM(xvrdpip, 0x12, 0x0E, PPC2_VSX),
10345GEN_XX2FORM(xvrdpiz, 0x12, 0x0D, PPC2_VSX),
ee6e02c0
TM
10346
10347GEN_XX3FORM(xvaddsp, 0x00, 0x08, PPC2_VSX),
10348GEN_XX3FORM(xvsubsp, 0x00, 0x09, PPC2_VSX),
5e591d88 10349GEN_XX3FORM(xvmulsp, 0x00, 0x0A, PPC2_VSX),
4b98eeef 10350GEN_XX3FORM(xvdivsp, 0x00, 0x0B, PPC2_VSX),
2009227f 10351GEN_XX2FORM(xvresp, 0x14, 0x09, PPC2_VSX),
d32404fe 10352GEN_XX2FORM(xvsqrtsp, 0x16, 0x08, PPC2_VSX),
d3f9df8f 10353GEN_XX2FORM(xvrsqrtesp, 0x14, 0x08, PPC2_VSX),
bc80838f 10354GEN_XX3FORM(xvtdivsp, 0x14, 0x0B, PPC2_VSX),
5cb151ac 10355GEN_XX2FORM(xvtsqrtsp, 0x14, 0x0A, PPC2_VSX),
595c6eef
TM
10356GEN_XX3FORM(xvmaddasp, 0x04, 0x08, PPC2_VSX),
10357GEN_XX3FORM(xvmaddmsp, 0x04, 0x09, PPC2_VSX),
10358GEN_XX3FORM(xvmsubasp, 0x04, 0x0A, PPC2_VSX),
10359GEN_XX3FORM(xvmsubmsp, 0x04, 0x0B, PPC2_VSX),
10360GEN_XX3FORM(xvnmaddasp, 0x04, 0x18, PPC2_VSX),
10361GEN_XX3FORM(xvnmaddmsp, 0x04, 0x19, PPC2_VSX),
10362GEN_XX3FORM(xvnmsubasp, 0x04, 0x1A, PPC2_VSX),
10363GEN_XX3FORM(xvnmsubmsp, 0x04, 0x1B, PPC2_VSX),
959e9c9d
TM
10364GEN_XX3FORM(xvmaxsp, 0x00, 0x18, PPC2_VSX),
10365GEN_XX3FORM(xvminsp, 0x00, 0x19, PPC2_VSX),
354a6dec
TM
10366GEN_XX3_RC_FORM(xvcmpeqsp, 0x0C, 0x08, PPC2_VSX),
10367GEN_XX3_RC_FORM(xvcmpgtsp, 0x0C, 0x09, PPC2_VSX),
10368GEN_XX3_RC_FORM(xvcmpgesp, 0x0C, 0x0A, PPC2_VSX),
ed8ac568 10369GEN_XX2FORM(xvcvspdp, 0x12, 0x1C, PPC2_VSX),
5177d2ca
TM
10370GEN_XX2FORM(xvcvspsxds, 0x10, 0x19, PPC2_VSX),
10371GEN_XX2FORM(xvcvspsxws, 0x10, 0x09, PPC2_VSX),
10372GEN_XX2FORM(xvcvspuxds, 0x10, 0x18, PPC2_VSX),
10373GEN_XX2FORM(xvcvspuxws, 0x10, 0x08, PPC2_VSX),
10374GEN_XX2FORM(xvcvsxdsp, 0x10, 0x1B, PPC2_VSX),
10375GEN_XX2FORM(xvcvuxdsp, 0x10, 0x1A, PPC2_VSX),
10376GEN_XX2FORM(xvcvsxwsp, 0x10, 0x0B, PPC2_VSX),
10377GEN_XX2FORM(xvcvuxwsp, 0x10, 0x0A, PPC2_VSX),
88e33d08
TM
10378GEN_XX2FORM(xvrspi, 0x12, 0x08, PPC2_VSX),
10379GEN_XX2FORM(xvrspic, 0x16, 0x0A, PPC2_VSX),
10380GEN_XX2FORM(xvrspim, 0x12, 0x0B, PPC2_VSX),
10381GEN_XX2FORM(xvrspip, 0x12, 0x0A, PPC2_VSX),
10382GEN_XX2FORM(xvrspiz, 0x12, 0x09, PPC2_VSX),
ee6e02c0 10383
79ca8a6a
TM
10384#undef VSX_LOGICAL
10385#define VSX_LOGICAL(name, opc2, opc3, fl2) \
10386GEN_XX3FORM(name, opc2, opc3, fl2)
10387
10388VSX_LOGICAL(xxland, 0x8, 0x10, PPC2_VSX),
10389VSX_LOGICAL(xxlandc, 0x8, 0x11, PPC2_VSX),
10390VSX_LOGICAL(xxlor, 0x8, 0x12, PPC2_VSX),
10391VSX_LOGICAL(xxlxor, 0x8, 0x13, PPC2_VSX),
10392VSX_LOGICAL(xxlnor, 0x8, 0x14, PPC2_VSX),
67a33f37
TM
10393VSX_LOGICAL(xxleqv, 0x8, 0x17, PPC2_VSX207),
10394VSX_LOGICAL(xxlnand, 0x8, 0x16, PPC2_VSX207),
10395VSX_LOGICAL(xxlorc, 0x8, 0x15, PPC2_VSX207),
ce577d2e
TM
10396GEN_XX3FORM(xxmrghw, 0x08, 0x02, PPC2_VSX),
10397GEN_XX3FORM(xxmrglw, 0x08, 0x06, PPC2_VSX),
76c15fe0 10398GEN_XX2FORM(xxspltw, 0x08, 0x0A, PPC2_VSX),
acc42968 10399GEN_XX3FORM_DM(xxsldwi, 0x08, 0x00),
79ca8a6a 10400
551e3ef7
TM
10401#define GEN_XXSEL_ROW(opc3) \
10402GEN_HANDLER2_E(xxsel, "xxsel", 0x3C, 0x18, opc3, 0, PPC_NONE, PPC2_VSX), \
10403GEN_HANDLER2_E(xxsel, "xxsel", 0x3C, 0x19, opc3, 0, PPC_NONE, PPC2_VSX), \
10404GEN_HANDLER2_E(xxsel, "xxsel", 0x3C, 0x1A, opc3, 0, PPC_NONE, PPC2_VSX), \
10405GEN_HANDLER2_E(xxsel, "xxsel", 0x3C, 0x1B, opc3, 0, PPC_NONE, PPC2_VSX), \
10406GEN_HANDLER2_E(xxsel, "xxsel", 0x3C, 0x1C, opc3, 0, PPC_NONE, PPC2_VSX), \
10407GEN_HANDLER2_E(xxsel, "xxsel", 0x3C, 0x1D, opc3, 0, PPC_NONE, PPC2_VSX), \
10408GEN_HANDLER2_E(xxsel, "xxsel", 0x3C, 0x1E, opc3, 0, PPC_NONE, PPC2_VSX), \
10409GEN_HANDLER2_E(xxsel, "xxsel", 0x3C, 0x1F, opc3, 0, PPC_NONE, PPC2_VSX), \
10410
10411GEN_XXSEL_ROW(0x00)
10412GEN_XXSEL_ROW(0x01)
10413GEN_XXSEL_ROW(0x02)
10414GEN_XXSEL_ROW(0x03)
10415GEN_XXSEL_ROW(0x04)
10416GEN_XXSEL_ROW(0x05)
10417GEN_XXSEL_ROW(0x06)
10418GEN_XXSEL_ROW(0x07)
10419GEN_XXSEL_ROW(0x08)
10420GEN_XXSEL_ROW(0x09)
10421GEN_XXSEL_ROW(0x0A)
10422GEN_XXSEL_ROW(0x0B)
10423GEN_XXSEL_ROW(0x0C)
10424GEN_XXSEL_ROW(0x0D)
10425GEN_XXSEL_ROW(0x0E)
10426GEN_XXSEL_ROW(0x0F)
10427GEN_XXSEL_ROW(0x10)
10428GEN_XXSEL_ROW(0x11)
10429GEN_XXSEL_ROW(0x12)
10430GEN_XXSEL_ROW(0x13)
10431GEN_XXSEL_ROW(0x14)
10432GEN_XXSEL_ROW(0x15)
10433GEN_XXSEL_ROW(0x16)
10434GEN_XXSEL_ROW(0x17)
10435GEN_XXSEL_ROW(0x18)
10436GEN_XXSEL_ROW(0x19)
10437GEN_XXSEL_ROW(0x1A)
10438GEN_XXSEL_ROW(0x1B)
10439GEN_XXSEL_ROW(0x1C)
10440GEN_XXSEL_ROW(0x1D)
10441GEN_XXSEL_ROW(0x1E)
10442GEN_XXSEL_ROW(0x1F)
10443
cd73f2c9
TM
10444GEN_XX3FORM_DM(xxpermdi, 0x08, 0x01),
10445
5c55ff99 10446#undef GEN_SPE
70560da7
FC
10447#define GEN_SPE(name0, name1, opc2, opc3, inval0, inval1, type) \
10448 GEN_OPCODE_DUAL(name0##_##name1, 0x04, opc2, opc3, inval0, inval1, type, PPC_NONE)
10449GEN_SPE(evaddw, speundef, 0x00, 0x08, 0x00000000, 0xFFFFFFFF, PPC_SPE),
10450GEN_SPE(evaddiw, speundef, 0x01, 0x08, 0x00000000, 0xFFFFFFFF, PPC_SPE),
10451GEN_SPE(evsubfw, speundef, 0x02, 0x08, 0x00000000, 0xFFFFFFFF, PPC_SPE),
10452GEN_SPE(evsubifw, speundef, 0x03, 0x08, 0x00000000, 0xFFFFFFFF, PPC_SPE),
10453GEN_SPE(evabs, evneg, 0x04, 0x08, 0x0000F800, 0x0000F800, PPC_SPE),
10454GEN_SPE(evextsb, evextsh, 0x05, 0x08, 0x0000F800, 0x0000F800, PPC_SPE),
10455GEN_SPE(evrndw, evcntlzw, 0x06, 0x08, 0x0000F800, 0x0000F800, PPC_SPE),
10456GEN_SPE(evcntlsw, brinc, 0x07, 0x08, 0x0000F800, 0x00000000, PPC_SPE),
10457GEN_SPE(evmra, speundef, 0x02, 0x13, 0x0000F800, 0xFFFFFFFF, PPC_SPE),
10458GEN_SPE(speundef, evand, 0x08, 0x08, 0xFFFFFFFF, 0x00000000, PPC_SPE),
10459GEN_SPE(evandc, speundef, 0x09, 0x08, 0x00000000, 0xFFFFFFFF, PPC_SPE),
10460GEN_SPE(evxor, evor, 0x0B, 0x08, 0x00000000, 0x00000000, PPC_SPE),
10461GEN_SPE(evnor, eveqv, 0x0C, 0x08, 0x00000000, 0x00000000, PPC_SPE),
10462GEN_SPE(evmwumi, evmwsmi, 0x0C, 0x11, 0x00000000, 0x00000000, PPC_SPE),
10463GEN_SPE(evmwumia, evmwsmia, 0x1C, 0x11, 0x00000000, 0x00000000, PPC_SPE),
10464GEN_SPE(evmwumiaa, evmwsmiaa, 0x0C, 0x15, 0x00000000, 0x00000000, PPC_SPE),
10465GEN_SPE(speundef, evorc, 0x0D, 0x08, 0xFFFFFFFF, 0x00000000, PPC_SPE),
10466GEN_SPE(evnand, speundef, 0x0F, 0x08, 0x00000000, 0xFFFFFFFF, PPC_SPE),
10467GEN_SPE(evsrwu, evsrws, 0x10, 0x08, 0x00000000, 0x00000000, PPC_SPE),
10468GEN_SPE(evsrwiu, evsrwis, 0x11, 0x08, 0x00000000, 0x00000000, PPC_SPE),
10469GEN_SPE(evslw, speundef, 0x12, 0x08, 0x00000000, 0xFFFFFFFF, PPC_SPE),
10470GEN_SPE(evslwi, speundef, 0x13, 0x08, 0x00000000, 0xFFFFFFFF, PPC_SPE),
10471GEN_SPE(evrlw, evsplati, 0x14, 0x08, 0x00000000, 0x0000F800, PPC_SPE),
10472GEN_SPE(evrlwi, evsplatfi, 0x15, 0x08, 0x00000000, 0x0000F800, PPC_SPE),
10473GEN_SPE(evmergehi, evmergelo, 0x16, 0x08, 0x00000000, 0x00000000, PPC_SPE),
10474GEN_SPE(evmergehilo, evmergelohi, 0x17, 0x08, 0x00000000, 0x00000000, PPC_SPE),
10475GEN_SPE(evcmpgtu, evcmpgts, 0x18, 0x08, 0x00600000, 0x00600000, PPC_SPE),
10476GEN_SPE(evcmpltu, evcmplts, 0x19, 0x08, 0x00600000, 0x00600000, PPC_SPE),
10477GEN_SPE(evcmpeq, speundef, 0x1A, 0x08, 0x00600000, 0xFFFFFFFF, PPC_SPE),
10478
10479GEN_SPE(evfsadd, evfssub, 0x00, 0x0A, 0x00000000, 0x00000000, PPC_SPE_SINGLE),
10480GEN_SPE(evfsabs, evfsnabs, 0x02, 0x0A, 0x0000F800, 0x0000F800, PPC_SPE_SINGLE),
10481GEN_SPE(evfsneg, speundef, 0x03, 0x0A, 0x0000F800, 0xFFFFFFFF, PPC_SPE_SINGLE),
10482GEN_SPE(evfsmul, evfsdiv, 0x04, 0x0A, 0x00000000, 0x00000000, PPC_SPE_SINGLE),
10483GEN_SPE(evfscmpgt, evfscmplt, 0x06, 0x0A, 0x00600000, 0x00600000, PPC_SPE_SINGLE),
10484GEN_SPE(evfscmpeq, speundef, 0x07, 0x0A, 0x00600000, 0xFFFFFFFF, PPC_SPE_SINGLE),
10485GEN_SPE(evfscfui, evfscfsi, 0x08, 0x0A, 0x00180000, 0x00180000, PPC_SPE_SINGLE),
10486GEN_SPE(evfscfuf, evfscfsf, 0x09, 0x0A, 0x00180000, 0x00180000, PPC_SPE_SINGLE),
10487GEN_SPE(evfsctui, evfsctsi, 0x0A, 0x0A, 0x00180000, 0x00180000, PPC_SPE_SINGLE),
10488GEN_SPE(evfsctuf, evfsctsf, 0x0B, 0x0A, 0x00180000, 0x00180000, PPC_SPE_SINGLE),
10489GEN_SPE(evfsctuiz, speundef, 0x0C, 0x0A, 0x00180000, 0xFFFFFFFF, PPC_SPE_SINGLE),
10490GEN_SPE(evfsctsiz, speundef, 0x0D, 0x0A, 0x00180000, 0xFFFFFFFF, PPC_SPE_SINGLE),
10491GEN_SPE(evfststgt, evfststlt, 0x0E, 0x0A, 0x00600000, 0x00600000, PPC_SPE_SINGLE),
10492GEN_SPE(evfststeq, speundef, 0x0F, 0x0A, 0x00600000, 0xFFFFFFFF, PPC_SPE_SINGLE),
10493
10494GEN_SPE(efsadd, efssub, 0x00, 0x0B, 0x00000000, 0x00000000, PPC_SPE_SINGLE),
10495GEN_SPE(efsabs, efsnabs, 0x02, 0x0B, 0x0000F800, 0x0000F800, PPC_SPE_SINGLE),
10496GEN_SPE(efsneg, speundef, 0x03, 0x0B, 0x0000F800, 0xFFFFFFFF, PPC_SPE_SINGLE),
10497GEN_SPE(efsmul, efsdiv, 0x04, 0x0B, 0x00000000, 0x00000000, PPC_SPE_SINGLE),
10498GEN_SPE(efscmpgt, efscmplt, 0x06, 0x0B, 0x00600000, 0x00600000, PPC_SPE_SINGLE),
10499GEN_SPE(efscmpeq, efscfd, 0x07, 0x0B, 0x00600000, 0x00180000, PPC_SPE_SINGLE),
10500GEN_SPE(efscfui, efscfsi, 0x08, 0x0B, 0x00180000, 0x00180000, PPC_SPE_SINGLE),
10501GEN_SPE(efscfuf, efscfsf, 0x09, 0x0B, 0x00180000, 0x00180000, PPC_SPE_SINGLE),
10502GEN_SPE(efsctui, efsctsi, 0x0A, 0x0B, 0x00180000, 0x00180000, PPC_SPE_SINGLE),
10503GEN_SPE(efsctuf, efsctsf, 0x0B, 0x0B, 0x00180000, 0x00180000, PPC_SPE_SINGLE),
10504GEN_SPE(efsctuiz, speundef, 0x0C, 0x0B, 0x00180000, 0xFFFFFFFF, PPC_SPE_SINGLE),
10505GEN_SPE(efsctsiz, speundef, 0x0D, 0x0B, 0x00180000, 0xFFFFFFFF, PPC_SPE_SINGLE),
10506GEN_SPE(efststgt, efststlt, 0x0E, 0x0B, 0x00600000, 0x00600000, PPC_SPE_SINGLE),
10507GEN_SPE(efststeq, speundef, 0x0F, 0x0B, 0x00600000, 0xFFFFFFFF, PPC_SPE_SINGLE),
10508
10509GEN_SPE(efdadd, efdsub, 0x10, 0x0B, 0x00000000, 0x00000000, PPC_SPE_DOUBLE),
10510GEN_SPE(efdcfuid, efdcfsid, 0x11, 0x0B, 0x00180000, 0x00180000, PPC_SPE_DOUBLE),
10511GEN_SPE(efdabs, efdnabs, 0x12, 0x0B, 0x0000F800, 0x0000F800, PPC_SPE_DOUBLE),
10512GEN_SPE(efdneg, speundef, 0x13, 0x0B, 0x0000F800, 0xFFFFFFFF, PPC_SPE_DOUBLE),
10513GEN_SPE(efdmul, efddiv, 0x14, 0x0B, 0x00000000, 0x00000000, PPC_SPE_DOUBLE),
10514GEN_SPE(efdctuidz, efdctsidz, 0x15, 0x0B, 0x00180000, 0x00180000, PPC_SPE_DOUBLE),
10515GEN_SPE(efdcmpgt, efdcmplt, 0x16, 0x0B, 0x00600000, 0x00600000, PPC_SPE_DOUBLE),
10516GEN_SPE(efdcmpeq, efdcfs, 0x17, 0x0B, 0x00600000, 0x00180000, PPC_SPE_DOUBLE),
10517GEN_SPE(efdcfui, efdcfsi, 0x18, 0x0B, 0x00180000, 0x00180000, PPC_SPE_DOUBLE),
10518GEN_SPE(efdcfuf, efdcfsf, 0x19, 0x0B, 0x00180000, 0x00180000, PPC_SPE_DOUBLE),
10519GEN_SPE(efdctui, efdctsi, 0x1A, 0x0B, 0x00180000, 0x00180000, PPC_SPE_DOUBLE),
10520GEN_SPE(efdctuf, efdctsf, 0x1B, 0x0B, 0x00180000, 0x00180000, PPC_SPE_DOUBLE),
10521GEN_SPE(efdctuiz, speundef, 0x1C, 0x0B, 0x00180000, 0xFFFFFFFF, PPC_SPE_DOUBLE),
10522GEN_SPE(efdctsiz, speundef, 0x1D, 0x0B, 0x00180000, 0xFFFFFFFF, PPC_SPE_DOUBLE),
10523GEN_SPE(efdtstgt, efdtstlt, 0x1E, 0x0B, 0x00600000, 0x00600000, PPC_SPE_DOUBLE),
10524GEN_SPE(efdtsteq, speundef, 0x1F, 0x0B, 0x00600000, 0xFFFFFFFF, PPC_SPE_DOUBLE),
5c55ff99
BS
10525
10526#undef GEN_SPEOP_LDST
10527#define GEN_SPEOP_LDST(name, opc2, sh) \
10528GEN_HANDLER(name, 0x04, opc2, 0x0C, 0x00000000, PPC_SPE)
10529GEN_SPEOP_LDST(evldd, 0x00, 3),
10530GEN_SPEOP_LDST(evldw, 0x01, 3),
10531GEN_SPEOP_LDST(evldh, 0x02, 3),
10532GEN_SPEOP_LDST(evlhhesplat, 0x04, 1),
10533GEN_SPEOP_LDST(evlhhousplat, 0x06, 1),
10534GEN_SPEOP_LDST(evlhhossplat, 0x07, 1),
10535GEN_SPEOP_LDST(evlwhe, 0x08, 2),
10536GEN_SPEOP_LDST(evlwhou, 0x0A, 2),
10537GEN_SPEOP_LDST(evlwhos, 0x0B, 2),
10538GEN_SPEOP_LDST(evlwwsplat, 0x0C, 2),
10539GEN_SPEOP_LDST(evlwhsplat, 0x0E, 2),
10540
10541GEN_SPEOP_LDST(evstdd, 0x10, 3),
10542GEN_SPEOP_LDST(evstdw, 0x11, 3),
10543GEN_SPEOP_LDST(evstdh, 0x12, 3),
10544GEN_SPEOP_LDST(evstwhe, 0x18, 2),
10545GEN_SPEOP_LDST(evstwho, 0x1A, 2),
10546GEN_SPEOP_LDST(evstwwe, 0x1C, 2),
10547GEN_SPEOP_LDST(evstwwo, 0x1E, 2),
10548};
10549
0411a972 10550#include "helper_regs.h"
a1389542 10551#include "translate_init.c"
79aceca5 10552
9a64fbe4 10553/*****************************************************************************/
3fc6c082 10554/* Misc PowerPC helpers */
878096ee
AF
10555void ppc_cpu_dump_state(CPUState *cs, FILE *f, fprintf_function cpu_fprintf,
10556 int flags)
79aceca5 10557{
3fc6c082
FB
10558#define RGPL 4
10559#define RFPL 4
3fc6c082 10560
878096ee
AF
10561 PowerPCCPU *cpu = POWERPC_CPU(cs);
10562 CPUPPCState *env = &cpu->env;
79aceca5
FB
10563 int i;
10564
90e189ec 10565 cpu_fprintf(f, "NIP " TARGET_FMT_lx " LR " TARGET_FMT_lx " CTR "
9a78eead 10566 TARGET_FMT_lx " XER " TARGET_FMT_lx "\n",
da91a00f 10567 env->nip, env->lr, env->ctr, cpu_read_xer(env));
90e189ec
BS
10568 cpu_fprintf(f, "MSR " TARGET_FMT_lx " HID0 " TARGET_FMT_lx " HF "
10569 TARGET_FMT_lx " idx %d\n", env->msr, env->spr[SPR_HID0],
10570 env->hflags, env->mmu_idx);
d9bce9d9 10571#if !defined(NO_TIMER_DUMP)
9a78eead 10572 cpu_fprintf(f, "TB %08" PRIu32 " %08" PRIu64
76a66253 10573#if !defined(CONFIG_USER_ONLY)
9a78eead 10574 " DECR %08" PRIu32
76a66253
JM
10575#endif
10576 "\n",
077fc206 10577 cpu_ppc_load_tbu(env), cpu_ppc_load_tbl(env)
76a66253
JM
10578#if !defined(CONFIG_USER_ONLY)
10579 , cpu_ppc_load_decr(env)
10580#endif
10581 );
077fc206 10582#endif
76a66253 10583 for (i = 0; i < 32; i++) {
3fc6c082
FB
10584 if ((i & (RGPL - 1)) == 0)
10585 cpu_fprintf(f, "GPR%02d", i);
b11ebf64 10586 cpu_fprintf(f, " %016" PRIx64, ppc_dump_gpr(env, i));
3fc6c082 10587 if ((i & (RGPL - 1)) == (RGPL - 1))
7fe48483 10588 cpu_fprintf(f, "\n");
76a66253 10589 }
3fc6c082 10590 cpu_fprintf(f, "CR ");
76a66253 10591 for (i = 0; i < 8; i++)
7fe48483
FB
10592 cpu_fprintf(f, "%01x", env->crf[i]);
10593 cpu_fprintf(f, " [");
76a66253
JM
10594 for (i = 0; i < 8; i++) {
10595 char a = '-';
10596 if (env->crf[i] & 0x08)
10597 a = 'L';
10598 else if (env->crf[i] & 0x04)
10599 a = 'G';
10600 else if (env->crf[i] & 0x02)
10601 a = 'E';
7fe48483 10602 cpu_fprintf(f, " %c%c", a, env->crf[i] & 0x01 ? 'O' : ' ');
76a66253 10603 }
90e189ec
BS
10604 cpu_fprintf(f, " ] RES " TARGET_FMT_lx "\n",
10605 env->reserve_addr);
3fc6c082
FB
10606 for (i = 0; i < 32; i++) {
10607 if ((i & (RFPL - 1)) == 0)
10608 cpu_fprintf(f, "FPR%02d", i);
26a76461 10609 cpu_fprintf(f, " %016" PRIx64, *((uint64_t *)&env->fpr[i]));
3fc6c082 10610 if ((i & (RFPL - 1)) == (RFPL - 1))
7fe48483 10611 cpu_fprintf(f, "\n");
79aceca5 10612 }
30304420 10613 cpu_fprintf(f, "FPSCR " TARGET_FMT_lx "\n", env->fpscr);
f2e63a42 10614#if !defined(CONFIG_USER_ONLY)
90dc8812
SW
10615 cpu_fprintf(f, " SRR0 " TARGET_FMT_lx " SRR1 " TARGET_FMT_lx
10616 " PVR " TARGET_FMT_lx " VRSAVE " TARGET_FMT_lx "\n",
10617 env->spr[SPR_SRR0], env->spr[SPR_SRR1],
10618 env->spr[SPR_PVR], env->spr[SPR_VRSAVE]);
10619
10620 cpu_fprintf(f, "SPRG0 " TARGET_FMT_lx " SPRG1 " TARGET_FMT_lx
10621 " SPRG2 " TARGET_FMT_lx " SPRG3 " TARGET_FMT_lx "\n",
10622 env->spr[SPR_SPRG0], env->spr[SPR_SPRG1],
10623 env->spr[SPR_SPRG2], env->spr[SPR_SPRG3]);
10624
10625 cpu_fprintf(f, "SPRG4 " TARGET_FMT_lx " SPRG5 " TARGET_FMT_lx
10626 " SPRG6 " TARGET_FMT_lx " SPRG7 " TARGET_FMT_lx "\n",
10627 env->spr[SPR_SPRG4], env->spr[SPR_SPRG5],
10628 env->spr[SPR_SPRG6], env->spr[SPR_SPRG7]);
10629
10630 if (env->excp_model == POWERPC_EXCP_BOOKE) {
10631 cpu_fprintf(f, "CSRR0 " TARGET_FMT_lx " CSRR1 " TARGET_FMT_lx
10632 " MCSRR0 " TARGET_FMT_lx " MCSRR1 " TARGET_FMT_lx "\n",
10633 env->spr[SPR_BOOKE_CSRR0], env->spr[SPR_BOOKE_CSRR1],
10634 env->spr[SPR_BOOKE_MCSRR0], env->spr[SPR_BOOKE_MCSRR1]);
10635
10636 cpu_fprintf(f, " TCR " TARGET_FMT_lx " TSR " TARGET_FMT_lx
10637 " ESR " TARGET_FMT_lx " DEAR " TARGET_FMT_lx "\n",
10638 env->spr[SPR_BOOKE_TCR], env->spr[SPR_BOOKE_TSR],
10639 env->spr[SPR_BOOKE_ESR], env->spr[SPR_BOOKE_DEAR]);
10640
10641 cpu_fprintf(f, " PIR " TARGET_FMT_lx " DECAR " TARGET_FMT_lx
10642 " IVPR " TARGET_FMT_lx " EPCR " TARGET_FMT_lx "\n",
10643 env->spr[SPR_BOOKE_PIR], env->spr[SPR_BOOKE_DECAR],
10644 env->spr[SPR_BOOKE_IVPR], env->spr[SPR_BOOKE_EPCR]);
10645
10646 cpu_fprintf(f, " MCSR " TARGET_FMT_lx " SPRG8 " TARGET_FMT_lx
10647 " EPR " TARGET_FMT_lx "\n",
10648 env->spr[SPR_BOOKE_MCSR], env->spr[SPR_BOOKE_SPRG8],
10649 env->spr[SPR_BOOKE_EPR]);
10650
10651 /* FSL-specific */
10652 cpu_fprintf(f, " MCAR " TARGET_FMT_lx " PID1 " TARGET_FMT_lx
10653 " PID2 " TARGET_FMT_lx " SVR " TARGET_FMT_lx "\n",
10654 env->spr[SPR_Exxx_MCAR], env->spr[SPR_BOOKE_PID1],
10655 env->spr[SPR_BOOKE_PID2], env->spr[SPR_E500_SVR]);
10656
10657 /*
10658 * IVORs are left out as they are large and do not change often --
10659 * they can be read with "p $ivor0", "p $ivor1", etc.
10660 */
10661 }
10662
697ab892
DG
10663#if defined(TARGET_PPC64)
10664 if (env->flags & POWERPC_FLAG_CFAR) {
10665 cpu_fprintf(f, " CFAR " TARGET_FMT_lx"\n", env->cfar);
10666 }
10667#endif
10668
90dc8812
SW
10669 switch (env->mmu_model) {
10670 case POWERPC_MMU_32B:
10671 case POWERPC_MMU_601:
10672 case POWERPC_MMU_SOFT_6xx:
10673 case POWERPC_MMU_SOFT_74xx:
10674#if defined(TARGET_PPC64)
90dc8812 10675 case POWERPC_MMU_64B:
ca480de6
AB
10676 case POWERPC_MMU_2_06:
10677 case POWERPC_MMU_2_06a:
10678 case POWERPC_MMU_2_06d:
90dc8812 10679#endif
ca480de6
AB
10680 cpu_fprintf(f, " SDR1 " TARGET_FMT_lx " DAR " TARGET_FMT_lx
10681 " DSISR " TARGET_FMT_lx "\n", env->spr[SPR_SDR1],
10682 env->spr[SPR_DAR], env->spr[SPR_DSISR]);
90dc8812 10683 break;
01662f3e 10684 case POWERPC_MMU_BOOKE206:
90dc8812
SW
10685 cpu_fprintf(f, " MAS0 " TARGET_FMT_lx " MAS1 " TARGET_FMT_lx
10686 " MAS2 " TARGET_FMT_lx " MAS3 " TARGET_FMT_lx "\n",
10687 env->spr[SPR_BOOKE_MAS0], env->spr[SPR_BOOKE_MAS1],
10688 env->spr[SPR_BOOKE_MAS2], env->spr[SPR_BOOKE_MAS3]);
10689
10690 cpu_fprintf(f, " MAS4 " TARGET_FMT_lx " MAS6 " TARGET_FMT_lx
10691 " MAS7 " TARGET_FMT_lx " PID " TARGET_FMT_lx "\n",
10692 env->spr[SPR_BOOKE_MAS4], env->spr[SPR_BOOKE_MAS6],
10693 env->spr[SPR_BOOKE_MAS7], env->spr[SPR_BOOKE_PID]);
10694
10695 cpu_fprintf(f, "MMUCFG " TARGET_FMT_lx " TLB0CFG " TARGET_FMT_lx
10696 " TLB1CFG " TARGET_FMT_lx "\n",
10697 env->spr[SPR_MMUCFG], env->spr[SPR_BOOKE_TLB0CFG],
10698 env->spr[SPR_BOOKE_TLB1CFG]);
10699 break;
10700 default:
10701 break;
10702 }
f2e63a42 10703#endif
79aceca5 10704
3fc6c082
FB
10705#undef RGPL
10706#undef RFPL
79aceca5
FB
10707}
10708
878096ee
AF
10709void ppc_cpu_dump_statistics(CPUState *cs, FILE*f,
10710 fprintf_function cpu_fprintf, int flags)
76a66253
JM
10711{
10712#if defined(DO_PPC_STATISTICS)
878096ee 10713 PowerPCCPU *cpu = POWERPC_CPU(cs);
c227f099 10714 opc_handler_t **t1, **t2, **t3, *handler;
76a66253
JM
10715 int op1, op2, op3;
10716
878096ee 10717 t1 = cpu->env.opcodes;
76a66253
JM
10718 for (op1 = 0; op1 < 64; op1++) {
10719 handler = t1[op1];
10720 if (is_indirect_opcode(handler)) {
10721 t2 = ind_table(handler);
10722 for (op2 = 0; op2 < 32; op2++) {
10723 handler = t2[op2];
10724 if (is_indirect_opcode(handler)) {
10725 t3 = ind_table(handler);
10726 for (op3 = 0; op3 < 32; op3++) {
10727 handler = t3[op3];
10728 if (handler->count == 0)
10729 continue;
10730 cpu_fprintf(f, "%02x %02x %02x (%02x %04d) %16s: "
0bfcd599 10731 "%016" PRIx64 " %" PRId64 "\n",
76a66253
JM
10732 op1, op2, op3, op1, (op3 << 5) | op2,
10733 handler->oname,
10734 handler->count, handler->count);
10735 }
10736 } else {
10737 if (handler->count == 0)
10738 continue;
10739 cpu_fprintf(f, "%02x %02x (%02x %04d) %16s: "
0bfcd599 10740 "%016" PRIx64 " %" PRId64 "\n",
76a66253
JM
10741 op1, op2, op1, op2, handler->oname,
10742 handler->count, handler->count);
10743 }
10744 }
10745 } else {
10746 if (handler->count == 0)
10747 continue;
0bfcd599
BS
10748 cpu_fprintf(f, "%02x (%02x ) %16s: %016" PRIx64
10749 " %" PRId64 "\n",
76a66253
JM
10750 op1, op1, handler->oname,
10751 handler->count, handler->count);
10752 }
10753 }
10754#endif
10755}
10756
9a64fbe4 10757/*****************************************************************************/
213fe1f5 10758static inline void gen_intermediate_code_internal(PowerPCCPU *cpu,
636aa200 10759 TranslationBlock *tb,
213fe1f5 10760 bool search_pc)
79aceca5 10761{
ed2803da 10762 CPUState *cs = CPU(cpu);
213fe1f5 10763 CPUPPCState *env = &cpu->env;
9fddaa0c 10764 DisasContext ctx, *ctxp = &ctx;
c227f099 10765 opc_handler_t **table, *handler;
0fa85d43 10766 target_ulong pc_start;
79aceca5 10767 uint16_t *gen_opc_end;
a1d1bb31 10768 CPUBreakpoint *bp;
79aceca5 10769 int j, lj = -1;
2e70f6ef
PB
10770 int num_insns;
10771 int max_insns;
79aceca5
FB
10772
10773 pc_start = tb->pc;
92414b31 10774 gen_opc_end = tcg_ctx.gen_opc_buf + OPC_MAX_SIZE;
046d6672 10775 ctx.nip = pc_start;
79aceca5 10776 ctx.tb = tb;
e1833e1f 10777 ctx.exception = POWERPC_EXCP_NONE;
3fc6c082 10778 ctx.spr_cb = env->spr_cb;
76db3ba4 10779 ctx.mem_idx = env->mmu_idx;
7d08d856
AJ
10780 ctx.insns_flags = env->insns_flags;
10781 ctx.insns_flags2 = env->insns_flags2;
76db3ba4
AJ
10782 ctx.access_type = -1;
10783 ctx.le_mode = env->hflags & (1 << MSR_LE) ? 1 : 0;
d9bce9d9 10784#if defined(TARGET_PPC64)
e42a61f1 10785 ctx.sf_mode = msr_is_64bit(env, env->msr);
697ab892 10786 ctx.has_cfar = !!(env->flags & POWERPC_FLAG_CFAR);
9a64fbe4 10787#endif
3cc62370 10788 ctx.fpu_enabled = msr_fp;
a9d9eb8f 10789 if ((env->flags & POWERPC_FLAG_SPE) && msr_spe)
d26bfc9a
JM
10790 ctx.spe_enabled = msr_spe;
10791 else
10792 ctx.spe_enabled = 0;
a9d9eb8f
JM
10793 if ((env->flags & POWERPC_FLAG_VRE) && msr_vr)
10794 ctx.altivec_enabled = msr_vr;
10795 else
10796 ctx.altivec_enabled = 0;
1f29871c
TM
10797 if ((env->flags & POWERPC_FLAG_VSX) && msr_vsx) {
10798 ctx.vsx_enabled = msr_vsx;
10799 } else {
10800 ctx.vsx_enabled = 0;
10801 }
d26bfc9a 10802 if ((env->flags & POWERPC_FLAG_SE) && msr_se)
8cbcb4fa 10803 ctx.singlestep_enabled = CPU_SINGLE_STEP;
d26bfc9a 10804 else
8cbcb4fa 10805 ctx.singlestep_enabled = 0;
d26bfc9a 10806 if ((env->flags & POWERPC_FLAG_BE) && msr_be)
8cbcb4fa 10807 ctx.singlestep_enabled |= CPU_BRANCH_STEP;
ed2803da 10808 if (unlikely(cs->singlestep_enabled)) {
8cbcb4fa 10809 ctx.singlestep_enabled |= GDBSTUB_SINGLE_STEP;
ed2803da 10810 }
3fc6c082 10811#if defined (DO_SINGLE_STEP) && 0
9a64fbe4
FB
10812 /* Single step trace mode */
10813 msr_se = 1;
10814#endif
2e70f6ef
PB
10815 num_insns = 0;
10816 max_insns = tb->cflags & CF_COUNT_MASK;
10817 if (max_insns == 0)
10818 max_insns = CF_COUNT_MASK;
10819
806f352d 10820 gen_tb_start();
9a64fbe4 10821 /* Set env in case of segfault during code fetch */
efd7f486
EV
10822 while (ctx.exception == POWERPC_EXCP_NONE
10823 && tcg_ctx.gen_opc_ptr < gen_opc_end) {
72cf2d4f
BS
10824 if (unlikely(!QTAILQ_EMPTY(&env->breakpoints))) {
10825 QTAILQ_FOREACH(bp, &env->breakpoints, entry) {
a1d1bb31 10826 if (bp->pc == ctx.nip) {
e06fcd75 10827 gen_debug_exception(ctxp);
ea4e754f
FB
10828 break;
10829 }
10830 }
10831 }
76a66253 10832 if (unlikely(search_pc)) {
92414b31 10833 j = tcg_ctx.gen_opc_ptr - tcg_ctx.gen_opc_buf;
79aceca5
FB
10834 if (lj < j) {
10835 lj++;
10836 while (lj < j)
ab1103de 10837 tcg_ctx.gen_opc_instr_start[lj++] = 0;
79aceca5 10838 }
25983cad 10839 tcg_ctx.gen_opc_pc[lj] = ctx.nip;
ab1103de 10840 tcg_ctx.gen_opc_instr_start[lj] = 1;
c9c99c22 10841 tcg_ctx.gen_opc_icount[lj] = num_insns;
79aceca5 10842 }
d12d51d5 10843 LOG_DISAS("----------------\n");
90e189ec 10844 LOG_DISAS("nip=" TARGET_FMT_lx " super=%d ir=%d\n",
d12d51d5 10845 ctx.nip, ctx.mem_idx, (int)msr_ir);
2e70f6ef
PB
10846 if (num_insns + 1 == max_insns && (tb->cflags & CF_LAST_IO))
10847 gen_io_start();
76db3ba4 10848 if (unlikely(ctx.le_mode)) {
2f5a189c 10849 ctx.opcode = bswap32(cpu_ldl_code(env, ctx.nip));
056401ea 10850 } else {
2f5a189c 10851 ctx.opcode = cpu_ldl_code(env, ctx.nip);
111bfab3 10852 }
d12d51d5 10853 LOG_DISAS("translate opcode %08x (%02x %02x %02x) (%s)\n",
9a64fbe4 10854 ctx.opcode, opc1(ctx.opcode), opc2(ctx.opcode),
476b6d16 10855 opc3(ctx.opcode), ctx.le_mode ? "little" : "big");
fdefe51c 10856 if (unlikely(qemu_loglevel_mask(CPU_LOG_TB_OP | CPU_LOG_TB_OP_OPT))) {
731c54f8 10857 tcg_gen_debug_insn_start(ctx.nip);
fdefe51c 10858 }
046d6672 10859 ctx.nip += 4;
3fc6c082 10860 table = env->opcodes;
2e70f6ef 10861 num_insns++;
79aceca5
FB
10862 handler = table[opc1(ctx.opcode)];
10863 if (is_indirect_opcode(handler)) {
10864 table = ind_table(handler);
10865 handler = table[opc2(ctx.opcode)];
10866 if (is_indirect_opcode(handler)) {
10867 table = ind_table(handler);
10868 handler = table[opc3(ctx.opcode)];
10869 }
10870 }
10871 /* Is opcode *REALLY* valid ? */
76a66253 10872 if (unlikely(handler->handler == &gen_invalid)) {
93fcfe39
AL
10873 if (qemu_log_enabled()) {
10874 qemu_log("invalid/unsupported opcode: "
90e189ec
BS
10875 "%02x - %02x - %02x (%08x) " TARGET_FMT_lx " %d\n",
10876 opc1(ctx.opcode), opc2(ctx.opcode),
10877 opc3(ctx.opcode), ctx.opcode, ctx.nip - 4, (int)msr_ir);
4b3686fa 10878 }
76a66253 10879 } else {
70560da7
FC
10880 uint32_t inval;
10881
10882 if (unlikely(handler->type & (PPC_SPE | PPC_SPE_SINGLE | PPC_SPE_DOUBLE) && Rc(ctx.opcode))) {
10883 inval = handler->inval2;
10884 } else {
10885 inval = handler->inval1;
10886 }
10887
10888 if (unlikely((ctx.opcode & inval) != 0)) {
93fcfe39
AL
10889 if (qemu_log_enabled()) {
10890 qemu_log("invalid bits: %08x for opcode: "
90e189ec 10891 "%02x - %02x - %02x (%08x) " TARGET_FMT_lx "\n",
70560da7 10892 ctx.opcode & inval, opc1(ctx.opcode),
90e189ec
BS
10893 opc2(ctx.opcode), opc3(ctx.opcode),
10894 ctx.opcode, ctx.nip - 4);
76a66253 10895 }
e06fcd75 10896 gen_inval_exception(ctxp, POWERPC_EXCP_INVAL_INVAL);
4b3686fa 10897 break;
79aceca5 10898 }
79aceca5 10899 }
4b3686fa 10900 (*(handler->handler))(&ctx);
76a66253
JM
10901#if defined(DO_PPC_STATISTICS)
10902 handler->count++;
10903#endif
9a64fbe4 10904 /* Check trace mode exceptions */
8cbcb4fa
AJ
10905 if (unlikely(ctx.singlestep_enabled & CPU_SINGLE_STEP &&
10906 (ctx.nip <= 0x100 || ctx.nip > 0xF00) &&
10907 ctx.exception != POWERPC_SYSCALL &&
10908 ctx.exception != POWERPC_EXCP_TRAP &&
10909 ctx.exception != POWERPC_EXCP_BRANCH)) {
e06fcd75 10910 gen_exception(ctxp, POWERPC_EXCP_TRACE);
d26bfc9a 10911 } else if (unlikely(((ctx.nip & (TARGET_PAGE_SIZE - 1)) == 0) ||
ed2803da 10912 (cs->singlestep_enabled) ||
1b530a6d 10913 singlestep ||
2e70f6ef 10914 num_insns >= max_insns)) {
d26bfc9a
JM
10915 /* if we reach a page boundary or are single stepping, stop
10916 * generation
10917 */
8dd4983c 10918 break;
76a66253 10919 }
3fc6c082 10920 }
2e70f6ef
PB
10921 if (tb->cflags & CF_LAST_IO)
10922 gen_io_end();
e1833e1f 10923 if (ctx.exception == POWERPC_EXCP_NONE) {
c1942362 10924 gen_goto_tb(&ctx, 0, ctx.nip);
e1833e1f 10925 } else if (ctx.exception != POWERPC_EXCP_BRANCH) {
ed2803da 10926 if (unlikely(cs->singlestep_enabled)) {
e06fcd75 10927 gen_debug_exception(ctxp);
8cbcb4fa 10928 }
76a66253 10929 /* Generate the return instruction */
57fec1fe 10930 tcg_gen_exit_tb(0);
9a64fbe4 10931 }
806f352d 10932 gen_tb_end(tb, num_insns);
efd7f486 10933 *tcg_ctx.gen_opc_ptr = INDEX_op_end;
76a66253 10934 if (unlikely(search_pc)) {
92414b31 10935 j = tcg_ctx.gen_opc_ptr - tcg_ctx.gen_opc_buf;
9a64fbe4
FB
10936 lj++;
10937 while (lj <= j)
ab1103de 10938 tcg_ctx.gen_opc_instr_start[lj++] = 0;
9a64fbe4 10939 } else {
046d6672 10940 tb->size = ctx.nip - pc_start;
2e70f6ef 10941 tb->icount = num_insns;
9a64fbe4 10942 }
d9bce9d9 10943#if defined(DEBUG_DISAS)
8fec2b8c 10944 if (qemu_loglevel_mask(CPU_LOG_TB_IN_ASM)) {
76a66253 10945 int flags;
237c0af0 10946 flags = env->bfd_mach;
76db3ba4 10947 flags |= ctx.le_mode << 16;
93fcfe39 10948 qemu_log("IN: %s\n", lookup_symbol(pc_start));
f4359b9f 10949 log_target_disas(env, pc_start, ctx.nip - pc_start, flags);
93fcfe39 10950 qemu_log("\n");
9fddaa0c 10951 }
79aceca5 10952#endif
79aceca5
FB
10953}
10954
1328c2bf 10955void gen_intermediate_code (CPUPPCState *env, struct TranslationBlock *tb)
79aceca5 10956{
213fe1f5 10957 gen_intermediate_code_internal(ppc_env_get_cpu(env), tb, false);
79aceca5
FB
10958}
10959
1328c2bf 10960void gen_intermediate_code_pc (CPUPPCState *env, struct TranslationBlock *tb)
79aceca5 10961{
213fe1f5 10962 gen_intermediate_code_internal(ppc_env_get_cpu(env), tb, true);
79aceca5 10963}
d2856f1a 10964
1328c2bf 10965void restore_state_to_opc(CPUPPCState *env, TranslationBlock *tb, int pc_pos)
d2856f1a 10966{
25983cad 10967 env->nip = tcg_ctx.gen_opc_pc[pc_pos];
d2856f1a 10968}
This page took 2.933018 seconds and 4 git commands to generate.