]> Git Repo - qemu.git/blame - target-ppc/translate.c
target-ppc: Add VSX ISA2.06 xtdiv Instructions
[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
79aceca5 2297/*** Floating-Point status & ctrl register ***/
99e300ef 2298
54623277 2299/* mcrfs */
99e300ef 2300static void gen_mcrfs(DisasContext *ctx)
79aceca5 2301{
30304420 2302 TCGv tmp = tcg_temp_new();
7c58044c
JM
2303 int bfa;
2304
76a66253 2305 if (unlikely(!ctx->fpu_enabled)) {
e06fcd75 2306 gen_exception(ctx, POWERPC_EXCP_FPU);
3cc62370
FB
2307 return;
2308 }
7c58044c 2309 bfa = 4 * (7 - crfS(ctx->opcode));
30304420
DG
2310 tcg_gen_shri_tl(tmp, cpu_fpscr, bfa);
2311 tcg_gen_trunc_tl_i32(cpu_crf[crfD(ctx->opcode)], tmp);
2312 tcg_temp_free(tmp);
e1571908 2313 tcg_gen_andi_i32(cpu_crf[crfD(ctx->opcode)], cpu_crf[crfD(ctx->opcode)], 0xf);
30304420 2314 tcg_gen_andi_tl(cpu_fpscr, cpu_fpscr, ~(0xF << bfa));
79aceca5
FB
2315}
2316
2317/* mffs */
99e300ef 2318static void gen_mffs(DisasContext *ctx)
79aceca5 2319{
76a66253 2320 if (unlikely(!ctx->fpu_enabled)) {
e06fcd75 2321 gen_exception(ctx, POWERPC_EXCP_FPU);
3cc62370
FB
2322 return;
2323 }
7c58044c 2324 gen_reset_fpstatus();
30304420 2325 tcg_gen_extu_tl_i64(cpu_fpr[rD(ctx->opcode)], cpu_fpscr);
af12906f 2326 gen_compute_fprf(cpu_fpr[rD(ctx->opcode)], 0, Rc(ctx->opcode) != 0);
79aceca5
FB
2327}
2328
2329/* mtfsb0 */
99e300ef 2330static void gen_mtfsb0(DisasContext *ctx)
79aceca5 2331{
fb0eaffc 2332 uint8_t crb;
3b46e624 2333
76a66253 2334 if (unlikely(!ctx->fpu_enabled)) {
e06fcd75 2335 gen_exception(ctx, POWERPC_EXCP_FPU);
3cc62370
FB
2336 return;
2337 }
6e35d524 2338 crb = 31 - crbD(ctx->opcode);
7c58044c 2339 gen_reset_fpstatus();
6e35d524 2340 if (likely(crb != FPSCR_FEX && crb != FPSCR_VX)) {
eb44b959
AJ
2341 TCGv_i32 t0;
2342 /* NIP cannot be restored if the memory exception comes from an helper */
2343 gen_update_nip(ctx, ctx->nip - 4);
2344 t0 = tcg_const_i32(crb);
8e703949 2345 gen_helper_fpscr_clrbit(cpu_env, t0);
6e35d524
AJ
2346 tcg_temp_free_i32(t0);
2347 }
7c58044c 2348 if (unlikely(Rc(ctx->opcode) != 0)) {
30304420
DG
2349 tcg_gen_trunc_tl_i32(cpu_crf[1], cpu_fpscr);
2350 tcg_gen_shri_i32(cpu_crf[1], cpu_crf[1], FPSCR_OX);
7c58044c 2351 }
79aceca5
FB
2352}
2353
2354/* mtfsb1 */
99e300ef 2355static void gen_mtfsb1(DisasContext *ctx)
79aceca5 2356{
fb0eaffc 2357 uint8_t crb;
3b46e624 2358
76a66253 2359 if (unlikely(!ctx->fpu_enabled)) {
e06fcd75 2360 gen_exception(ctx, POWERPC_EXCP_FPU);
3cc62370
FB
2361 return;
2362 }
6e35d524 2363 crb = 31 - crbD(ctx->opcode);
7c58044c
JM
2364 gen_reset_fpstatus();
2365 /* XXX: we pretend we can only do IEEE floating-point computations */
af12906f 2366 if (likely(crb != FPSCR_FEX && crb != FPSCR_VX && crb != FPSCR_NI)) {
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_setbit(cpu_env, t0);
0f2f39c2 2372 tcg_temp_free_i32(t0);
af12906f 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
JM
2377 }
2378 /* We can raise a differed exception */
8e703949 2379 gen_helper_float_check_status(cpu_env);
79aceca5
FB
2380}
2381
2382/* mtfsf */
99e300ef 2383static void gen_mtfsf(DisasContext *ctx)
79aceca5 2384{
0f2f39c2 2385 TCGv_i32 t0;
7d08d856 2386 int flm, l, w;
af12906f 2387
76a66253 2388 if (unlikely(!ctx->fpu_enabled)) {
e06fcd75 2389 gen_exception(ctx, POWERPC_EXCP_FPU);
3cc62370
FB
2390 return;
2391 }
7d08d856
AJ
2392 flm = FPFLM(ctx->opcode);
2393 l = FPL(ctx->opcode);
2394 w = FPW(ctx->opcode);
2395 if (unlikely(w & !(ctx->insns_flags2 & PPC2_ISA205))) {
2396 gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL);
2397 return;
2398 }
eb44b959
AJ
2399 /* NIP cannot be restored if the memory exception comes from an helper */
2400 gen_update_nip(ctx, ctx->nip - 4);
7c58044c 2401 gen_reset_fpstatus();
7d08d856
AJ
2402 if (l) {
2403 t0 = tcg_const_i32((ctx->insns_flags2 & PPC2_ISA205) ? 0xffff : 0xff);
2404 } else {
2405 t0 = tcg_const_i32(flm << (w * 8));
2406 }
8e703949 2407 gen_helper_store_fpscr(cpu_env, cpu_fpr[rB(ctx->opcode)], t0);
0f2f39c2 2408 tcg_temp_free_i32(t0);
7c58044c 2409 if (unlikely(Rc(ctx->opcode) != 0)) {
30304420
DG
2410 tcg_gen_trunc_tl_i32(cpu_crf[1], cpu_fpscr);
2411 tcg_gen_shri_i32(cpu_crf[1], cpu_crf[1], FPSCR_OX);
7c58044c
JM
2412 }
2413 /* We can raise a differed exception */
8e703949 2414 gen_helper_float_check_status(cpu_env);
79aceca5
FB
2415}
2416
2417/* mtfsfi */
99e300ef 2418static void gen_mtfsfi(DisasContext *ctx)
79aceca5 2419{
7d08d856 2420 int bf, sh, w;
0f2f39c2
AJ
2421 TCGv_i64 t0;
2422 TCGv_i32 t1;
7c58044c 2423
76a66253 2424 if (unlikely(!ctx->fpu_enabled)) {
e06fcd75 2425 gen_exception(ctx, POWERPC_EXCP_FPU);
3cc62370
FB
2426 return;
2427 }
7d08d856
AJ
2428 w = FPW(ctx->opcode);
2429 bf = FPBF(ctx->opcode);
2430 if (unlikely(w & !(ctx->insns_flags2 & PPC2_ISA205))) {
2431 gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL);
2432 return;
2433 }
2434 sh = (8 * w) + 7 - bf;
eb44b959
AJ
2435 /* NIP cannot be restored if the memory exception comes from an helper */
2436 gen_update_nip(ctx, ctx->nip - 4);
7c58044c 2437 gen_reset_fpstatus();
7d08d856 2438 t0 = tcg_const_i64(((uint64_t)FPIMM(ctx->opcode)) << (4 * sh));
af12906f 2439 t1 = tcg_const_i32(1 << sh);
8e703949 2440 gen_helper_store_fpscr(cpu_env, t0, t1);
0f2f39c2
AJ
2441 tcg_temp_free_i64(t0);
2442 tcg_temp_free_i32(t1);
7c58044c 2443 if (unlikely(Rc(ctx->opcode) != 0)) {
30304420
DG
2444 tcg_gen_trunc_tl_i32(cpu_crf[1], cpu_fpscr);
2445 tcg_gen_shri_i32(cpu_crf[1], cpu_crf[1], FPSCR_OX);
7c58044c
JM
2446 }
2447 /* We can raise a differed exception */
8e703949 2448 gen_helper_float_check_status(cpu_env);
79aceca5
FB
2449}
2450
76a66253
JM
2451/*** Addressing modes ***/
2452/* Register indirect with immediate index : EA = (rA|0) + SIMM */
636aa200
BS
2453static inline void gen_addr_imm_index(DisasContext *ctx, TCGv EA,
2454 target_long maskl)
76a66253
JM
2455{
2456 target_long simm = SIMM(ctx->opcode);
2457
be147d08 2458 simm &= ~maskl;
76db3ba4 2459 if (rA(ctx->opcode) == 0) {
c791fe84
RH
2460 if (NARROW_MODE(ctx)) {
2461 simm = (uint32_t)simm;
2462 }
e2be8d8d 2463 tcg_gen_movi_tl(EA, simm);
76db3ba4 2464 } else if (likely(simm != 0)) {
e2be8d8d 2465 tcg_gen_addi_tl(EA, cpu_gpr[rA(ctx->opcode)], simm);
c791fe84 2466 if (NARROW_MODE(ctx)) {
76db3ba4
AJ
2467 tcg_gen_ext32u_tl(EA, EA);
2468 }
76db3ba4 2469 } else {
c791fe84 2470 if (NARROW_MODE(ctx)) {
76db3ba4 2471 tcg_gen_ext32u_tl(EA, cpu_gpr[rA(ctx->opcode)]);
c791fe84
RH
2472 } else {
2473 tcg_gen_mov_tl(EA, cpu_gpr[rA(ctx->opcode)]);
2474 }
76db3ba4 2475 }
76a66253
JM
2476}
2477
636aa200 2478static inline void gen_addr_reg_index(DisasContext *ctx, TCGv EA)
76a66253 2479{
76db3ba4 2480 if (rA(ctx->opcode) == 0) {
c791fe84 2481 if (NARROW_MODE(ctx)) {
76db3ba4 2482 tcg_gen_ext32u_tl(EA, cpu_gpr[rB(ctx->opcode)]);
c791fe84
RH
2483 } else {
2484 tcg_gen_mov_tl(EA, cpu_gpr[rB(ctx->opcode)]);
2485 }
76db3ba4 2486 } else {
e2be8d8d 2487 tcg_gen_add_tl(EA, cpu_gpr[rA(ctx->opcode)], cpu_gpr[rB(ctx->opcode)]);
c791fe84 2488 if (NARROW_MODE(ctx)) {
76db3ba4
AJ
2489 tcg_gen_ext32u_tl(EA, EA);
2490 }
76db3ba4 2491 }
76a66253
JM
2492}
2493
636aa200 2494static inline void gen_addr_register(DisasContext *ctx, TCGv EA)
76a66253 2495{
76db3ba4 2496 if (rA(ctx->opcode) == 0) {
e2be8d8d 2497 tcg_gen_movi_tl(EA, 0);
c791fe84
RH
2498 } else if (NARROW_MODE(ctx)) {
2499 tcg_gen_ext32u_tl(EA, cpu_gpr[rA(ctx->opcode)]);
76db3ba4 2500 } else {
c791fe84 2501 tcg_gen_mov_tl(EA, cpu_gpr[rA(ctx->opcode)]);
76db3ba4
AJ
2502 }
2503}
2504
636aa200
BS
2505static inline void gen_addr_add(DisasContext *ctx, TCGv ret, TCGv arg1,
2506 target_long val)
76db3ba4
AJ
2507{
2508 tcg_gen_addi_tl(ret, arg1, val);
c791fe84 2509 if (NARROW_MODE(ctx)) {
76db3ba4
AJ
2510 tcg_gen_ext32u_tl(ret, ret);
2511 }
76a66253
JM
2512}
2513
636aa200 2514static inline void gen_check_align(DisasContext *ctx, TCGv EA, int mask)
cf360a32
AJ
2515{
2516 int l1 = gen_new_label();
2517 TCGv t0 = tcg_temp_new();
2518 TCGv_i32 t1, t2;
2519 /* NIP cannot be restored if the memory exception comes from an helper */
2520 gen_update_nip(ctx, ctx->nip - 4);
2521 tcg_gen_andi_tl(t0, EA, mask);
2522 tcg_gen_brcondi_tl(TCG_COND_EQ, t0, 0, l1);
2523 t1 = tcg_const_i32(POWERPC_EXCP_ALIGN);
2524 t2 = tcg_const_i32(0);
e5f17ac6 2525 gen_helper_raise_exception_err(cpu_env, t1, t2);
cf360a32
AJ
2526 tcg_temp_free_i32(t1);
2527 tcg_temp_free_i32(t2);
2528 gen_set_label(l1);
2529 tcg_temp_free(t0);
2530}
2531
7863667f 2532/*** Integer load ***/
636aa200 2533static inline void gen_qemu_ld8u(DisasContext *ctx, TCGv arg1, TCGv arg2)
76db3ba4
AJ
2534{
2535 tcg_gen_qemu_ld8u(arg1, arg2, ctx->mem_idx);
2536}
2537
636aa200 2538static inline void gen_qemu_ld8s(DisasContext *ctx, TCGv arg1, TCGv arg2)
76db3ba4
AJ
2539{
2540 tcg_gen_qemu_ld8s(arg1, arg2, ctx->mem_idx);
2541}
2542
636aa200 2543static inline void gen_qemu_ld16u(DisasContext *ctx, TCGv arg1, TCGv arg2)
76db3ba4
AJ
2544{
2545 tcg_gen_qemu_ld16u(arg1, arg2, ctx->mem_idx);
2546 if (unlikely(ctx->le_mode)) {
fa3966a3 2547 tcg_gen_bswap16_tl(arg1, arg1);
76db3ba4 2548 }
b61f2753
AJ
2549}
2550
636aa200 2551static inline void gen_qemu_ld16s(DisasContext *ctx, TCGv arg1, TCGv arg2)
b61f2753 2552{
76db3ba4 2553 if (unlikely(ctx->le_mode)) {
76db3ba4 2554 tcg_gen_qemu_ld16u(arg1, arg2, ctx->mem_idx);
fa3966a3 2555 tcg_gen_bswap16_tl(arg1, arg1);
76db3ba4 2556 tcg_gen_ext16s_tl(arg1, arg1);
76db3ba4
AJ
2557 } else {
2558 tcg_gen_qemu_ld16s(arg1, arg2, ctx->mem_idx);
2559 }
b61f2753
AJ
2560}
2561
636aa200 2562static inline void gen_qemu_ld32u(DisasContext *ctx, TCGv arg1, TCGv arg2)
b61f2753 2563{
76db3ba4
AJ
2564 tcg_gen_qemu_ld32u(arg1, arg2, ctx->mem_idx);
2565 if (unlikely(ctx->le_mode)) {
fa3966a3 2566 tcg_gen_bswap32_tl(arg1, arg1);
76db3ba4 2567 }
b61f2753
AJ
2568}
2569
f976b09e
AG
2570static void gen_qemu_ld32u_i64(DisasContext *ctx, TCGv_i64 val, TCGv addr)
2571{
2572 TCGv tmp = tcg_temp_new();
2573 gen_qemu_ld32u(ctx, tmp, addr);
2574 tcg_gen_extu_tl_i64(val, tmp);
2575 tcg_temp_free(tmp);
2576}
2577
636aa200 2578static inline void gen_qemu_ld32s(DisasContext *ctx, TCGv arg1, TCGv arg2)
b61f2753 2579{
a457e7ee 2580 if (unlikely(ctx->le_mode)) {
76db3ba4 2581 tcg_gen_qemu_ld32u(arg1, arg2, ctx->mem_idx);
fa3966a3
AJ
2582 tcg_gen_bswap32_tl(arg1, arg1);
2583 tcg_gen_ext32s_tl(arg1, arg1);
b61f2753 2584 } else
76db3ba4 2585 tcg_gen_qemu_ld32s(arg1, arg2, ctx->mem_idx);
b61f2753
AJ
2586}
2587
636aa200 2588static inline void gen_qemu_ld64(DisasContext *ctx, TCGv_i64 arg1, TCGv arg2)
b61f2753 2589{
76db3ba4
AJ
2590 tcg_gen_qemu_ld64(arg1, arg2, ctx->mem_idx);
2591 if (unlikely(ctx->le_mode)) {
66896cb8 2592 tcg_gen_bswap64_i64(arg1, arg1);
76db3ba4 2593 }
b61f2753
AJ
2594}
2595
636aa200 2596static inline void gen_qemu_st8(DisasContext *ctx, TCGv arg1, TCGv arg2)
b61f2753 2597{
76db3ba4 2598 tcg_gen_qemu_st8(arg1, arg2, ctx->mem_idx);
b61f2753
AJ
2599}
2600
636aa200 2601static inline void gen_qemu_st16(DisasContext *ctx, TCGv arg1, TCGv arg2)
b61f2753 2602{
76db3ba4 2603 if (unlikely(ctx->le_mode)) {
76db3ba4
AJ
2604 TCGv t0 = tcg_temp_new();
2605 tcg_gen_ext16u_tl(t0, arg1);
fa3966a3 2606 tcg_gen_bswap16_tl(t0, t0);
76db3ba4
AJ
2607 tcg_gen_qemu_st16(t0, arg2, ctx->mem_idx);
2608 tcg_temp_free(t0);
76db3ba4
AJ
2609 } else {
2610 tcg_gen_qemu_st16(arg1, arg2, ctx->mem_idx);
2611 }
b61f2753
AJ
2612}
2613
636aa200 2614static inline void gen_qemu_st32(DisasContext *ctx, TCGv arg1, TCGv arg2)
b61f2753 2615{
76db3ba4 2616 if (unlikely(ctx->le_mode)) {
fa3966a3
AJ
2617 TCGv t0 = tcg_temp_new();
2618 tcg_gen_ext32u_tl(t0, arg1);
2619 tcg_gen_bswap32_tl(t0, t0);
76db3ba4
AJ
2620 tcg_gen_qemu_st32(t0, arg2, ctx->mem_idx);
2621 tcg_temp_free(t0);
76db3ba4
AJ
2622 } else {
2623 tcg_gen_qemu_st32(arg1, arg2, ctx->mem_idx);
2624 }
b61f2753
AJ
2625}
2626
f976b09e
AG
2627static void gen_qemu_st32_i64(DisasContext *ctx, TCGv_i64 val, TCGv addr)
2628{
2629 TCGv tmp = tcg_temp_new();
2630 tcg_gen_trunc_i64_tl(tmp, val);
2631 gen_qemu_st32(ctx, tmp, addr);
2632 tcg_temp_free(tmp);
2633}
2634
636aa200 2635static inline void gen_qemu_st64(DisasContext *ctx, TCGv_i64 arg1, TCGv arg2)
b61f2753 2636{
76db3ba4 2637 if (unlikely(ctx->le_mode)) {
a7812ae4 2638 TCGv_i64 t0 = tcg_temp_new_i64();
66896cb8 2639 tcg_gen_bswap64_i64(t0, arg1);
76db3ba4 2640 tcg_gen_qemu_st64(t0, arg2, ctx->mem_idx);
a7812ae4 2641 tcg_temp_free_i64(t0);
b61f2753 2642 } else
76db3ba4 2643 tcg_gen_qemu_st64(arg1, arg2, ctx->mem_idx);
b61f2753
AJ
2644}
2645
0c8aacd4 2646#define GEN_LD(name, ldop, opc, type) \
99e300ef 2647static void glue(gen_, name)(DisasContext *ctx) \
79aceca5 2648{ \
76db3ba4
AJ
2649 TCGv EA; \
2650 gen_set_access_type(ctx, ACCESS_INT); \
2651 EA = tcg_temp_new(); \
2652 gen_addr_imm_index(ctx, EA, 0); \
2653 gen_qemu_##ldop(ctx, cpu_gpr[rD(ctx->opcode)], EA); \
b61f2753 2654 tcg_temp_free(EA); \
79aceca5
FB
2655}
2656
0c8aacd4 2657#define GEN_LDU(name, ldop, opc, type) \
99e300ef 2658static void glue(gen_, name##u)(DisasContext *ctx) \
79aceca5 2659{ \
b61f2753 2660 TCGv EA; \
76a66253
JM
2661 if (unlikely(rA(ctx->opcode) == 0 || \
2662 rA(ctx->opcode) == rD(ctx->opcode))) { \
e06fcd75 2663 gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL); \
9fddaa0c 2664 return; \
9a64fbe4 2665 } \
76db3ba4 2666 gen_set_access_type(ctx, ACCESS_INT); \
0c8aacd4 2667 EA = tcg_temp_new(); \
9d53c753 2668 if (type == PPC_64B) \
76db3ba4 2669 gen_addr_imm_index(ctx, EA, 0x03); \
9d53c753 2670 else \
76db3ba4
AJ
2671 gen_addr_imm_index(ctx, EA, 0); \
2672 gen_qemu_##ldop(ctx, cpu_gpr[rD(ctx->opcode)], EA); \
b61f2753
AJ
2673 tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], EA); \
2674 tcg_temp_free(EA); \
79aceca5
FB
2675}
2676
0c8aacd4 2677#define GEN_LDUX(name, ldop, opc2, opc3, type) \
99e300ef 2678static void glue(gen_, name##ux)(DisasContext *ctx) \
79aceca5 2679{ \
b61f2753 2680 TCGv EA; \
76a66253
JM
2681 if (unlikely(rA(ctx->opcode) == 0 || \
2682 rA(ctx->opcode) == rD(ctx->opcode))) { \
e06fcd75 2683 gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL); \
9fddaa0c 2684 return; \
9a64fbe4 2685 } \
76db3ba4 2686 gen_set_access_type(ctx, ACCESS_INT); \
0c8aacd4 2687 EA = tcg_temp_new(); \
76db3ba4
AJ
2688 gen_addr_reg_index(ctx, EA); \
2689 gen_qemu_##ldop(ctx, cpu_gpr[rD(ctx->opcode)], EA); \
b61f2753
AJ
2690 tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], EA); \
2691 tcg_temp_free(EA); \
79aceca5
FB
2692}
2693
cd6e9320 2694#define GEN_LDX_E(name, ldop, opc2, opc3, type, type2) \
99e300ef 2695static void glue(gen_, name##x)(DisasContext *ctx) \
79aceca5 2696{ \
76db3ba4
AJ
2697 TCGv EA; \
2698 gen_set_access_type(ctx, ACCESS_INT); \
2699 EA = tcg_temp_new(); \
2700 gen_addr_reg_index(ctx, EA); \
2701 gen_qemu_##ldop(ctx, cpu_gpr[rD(ctx->opcode)], EA); \
b61f2753 2702 tcg_temp_free(EA); \
79aceca5 2703}
cd6e9320
TH
2704#define GEN_LDX(name, ldop, opc2, opc3, type) \
2705 GEN_LDX_E(name, ldop, opc2, opc3, type, PPC_NONE)
79aceca5 2706
0c8aacd4
AJ
2707#define GEN_LDS(name, ldop, op, type) \
2708GEN_LD(name, ldop, op | 0x20, type); \
2709GEN_LDU(name, ldop, op | 0x21, type); \
2710GEN_LDUX(name, ldop, 0x17, op | 0x01, type); \
2711GEN_LDX(name, ldop, 0x17, op | 0x00, type)
79aceca5
FB
2712
2713/* lbz lbzu lbzux lbzx */
0c8aacd4 2714GEN_LDS(lbz, ld8u, 0x02, PPC_INTEGER);
79aceca5 2715/* lha lhau lhaux lhax */
0c8aacd4 2716GEN_LDS(lha, ld16s, 0x0A, PPC_INTEGER);
79aceca5 2717/* lhz lhzu lhzux lhzx */
0c8aacd4 2718GEN_LDS(lhz, ld16u, 0x08, PPC_INTEGER);
79aceca5 2719/* lwz lwzu lwzux lwzx */
0c8aacd4 2720GEN_LDS(lwz, ld32u, 0x00, PPC_INTEGER);
d9bce9d9 2721#if defined(TARGET_PPC64)
d9bce9d9 2722/* lwaux */
0c8aacd4 2723GEN_LDUX(lwa, ld32s, 0x15, 0x0B, PPC_64B);
d9bce9d9 2724/* lwax */
0c8aacd4 2725GEN_LDX(lwa, ld32s, 0x15, 0x0A, PPC_64B);
d9bce9d9 2726/* ldux */
0c8aacd4 2727GEN_LDUX(ld, ld64, 0x15, 0x01, PPC_64B);
d9bce9d9 2728/* ldx */
0c8aacd4 2729GEN_LDX(ld, ld64, 0x15, 0x00, PPC_64B);
99e300ef
BS
2730
2731static void gen_ld(DisasContext *ctx)
d9bce9d9 2732{
b61f2753 2733 TCGv EA;
d9bce9d9
JM
2734 if (Rc(ctx->opcode)) {
2735 if (unlikely(rA(ctx->opcode) == 0 ||
2736 rA(ctx->opcode) == rD(ctx->opcode))) {
e06fcd75 2737 gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL);
d9bce9d9
JM
2738 return;
2739 }
2740 }
76db3ba4 2741 gen_set_access_type(ctx, ACCESS_INT);
a7812ae4 2742 EA = tcg_temp_new();
76db3ba4 2743 gen_addr_imm_index(ctx, EA, 0x03);
d9bce9d9
JM
2744 if (ctx->opcode & 0x02) {
2745 /* lwa (lwau is undefined) */
76db3ba4 2746 gen_qemu_ld32s(ctx, cpu_gpr[rD(ctx->opcode)], EA);
d9bce9d9
JM
2747 } else {
2748 /* ld - ldu */
76db3ba4 2749 gen_qemu_ld64(ctx, cpu_gpr[rD(ctx->opcode)], EA);
d9bce9d9 2750 }
d9bce9d9 2751 if (Rc(ctx->opcode))
b61f2753
AJ
2752 tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], EA);
2753 tcg_temp_free(EA);
d9bce9d9 2754}
99e300ef 2755
54623277 2756/* lq */
99e300ef 2757static void gen_lq(DisasContext *ctx)
be147d08
JM
2758{
2759#if defined(CONFIG_USER_ONLY)
e06fcd75 2760 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
be147d08
JM
2761#else
2762 int ra, rd;
b61f2753 2763 TCGv EA;
be147d08
JM
2764
2765 /* Restore CPU state */
76db3ba4 2766 if (unlikely(ctx->mem_idx == 0)) {
e06fcd75 2767 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
be147d08
JM
2768 return;
2769 }
2770 ra = rA(ctx->opcode);
2771 rd = rD(ctx->opcode);
2772 if (unlikely((rd & 1) || rd == ra)) {
e06fcd75 2773 gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL);
be147d08
JM
2774 return;
2775 }
76db3ba4 2776 if (unlikely(ctx->le_mode)) {
be147d08 2777 /* Little-endian mode is not handled */
e06fcd75 2778 gen_exception_err(ctx, POWERPC_EXCP_ALIGN, POWERPC_EXCP_ALIGN_LE);
be147d08
JM
2779 return;
2780 }
76db3ba4 2781 gen_set_access_type(ctx, ACCESS_INT);
a7812ae4 2782 EA = tcg_temp_new();
76db3ba4
AJ
2783 gen_addr_imm_index(ctx, EA, 0x0F);
2784 gen_qemu_ld64(ctx, cpu_gpr[rd], EA);
2785 gen_addr_add(ctx, EA, EA, 8);
2786 gen_qemu_ld64(ctx, cpu_gpr[rd+1], EA);
b61f2753 2787 tcg_temp_free(EA);
be147d08
JM
2788#endif
2789}
d9bce9d9 2790#endif
79aceca5
FB
2791
2792/*** Integer store ***/
0c8aacd4 2793#define GEN_ST(name, stop, opc, type) \
99e300ef 2794static void glue(gen_, name)(DisasContext *ctx) \
79aceca5 2795{ \
76db3ba4
AJ
2796 TCGv EA; \
2797 gen_set_access_type(ctx, ACCESS_INT); \
2798 EA = tcg_temp_new(); \
2799 gen_addr_imm_index(ctx, EA, 0); \
2800 gen_qemu_##stop(ctx, cpu_gpr[rS(ctx->opcode)], EA); \
b61f2753 2801 tcg_temp_free(EA); \
79aceca5
FB
2802}
2803
0c8aacd4 2804#define GEN_STU(name, stop, opc, type) \
99e300ef 2805static void glue(gen_, stop##u)(DisasContext *ctx) \
79aceca5 2806{ \
b61f2753 2807 TCGv EA; \
76a66253 2808 if (unlikely(rA(ctx->opcode) == 0)) { \
e06fcd75 2809 gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL); \
9fddaa0c 2810 return; \
9a64fbe4 2811 } \
76db3ba4 2812 gen_set_access_type(ctx, ACCESS_INT); \
0c8aacd4 2813 EA = tcg_temp_new(); \
9d53c753 2814 if (type == PPC_64B) \
76db3ba4 2815 gen_addr_imm_index(ctx, EA, 0x03); \
9d53c753 2816 else \
76db3ba4
AJ
2817 gen_addr_imm_index(ctx, EA, 0); \
2818 gen_qemu_##stop(ctx, cpu_gpr[rS(ctx->opcode)], EA); \
b61f2753
AJ
2819 tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], EA); \
2820 tcg_temp_free(EA); \
79aceca5
FB
2821}
2822
0c8aacd4 2823#define GEN_STUX(name, stop, opc2, opc3, type) \
99e300ef 2824static void glue(gen_, name##ux)(DisasContext *ctx) \
79aceca5 2825{ \
b61f2753 2826 TCGv EA; \
76a66253 2827 if (unlikely(rA(ctx->opcode) == 0)) { \
e06fcd75 2828 gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL); \
9fddaa0c 2829 return; \
9a64fbe4 2830 } \
76db3ba4 2831 gen_set_access_type(ctx, ACCESS_INT); \
0c8aacd4 2832 EA = tcg_temp_new(); \
76db3ba4
AJ
2833 gen_addr_reg_index(ctx, EA); \
2834 gen_qemu_##stop(ctx, cpu_gpr[rS(ctx->opcode)], EA); \
b61f2753
AJ
2835 tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], EA); \
2836 tcg_temp_free(EA); \
79aceca5
FB
2837}
2838
cd6e9320
TH
2839#define GEN_STX_E(name, stop, opc2, opc3, type, type2) \
2840static void glue(gen_, name##x)(DisasContext *ctx) \
79aceca5 2841{ \
76db3ba4
AJ
2842 TCGv EA; \
2843 gen_set_access_type(ctx, ACCESS_INT); \
2844 EA = tcg_temp_new(); \
2845 gen_addr_reg_index(ctx, EA); \
2846 gen_qemu_##stop(ctx, cpu_gpr[rS(ctx->opcode)], EA); \
b61f2753 2847 tcg_temp_free(EA); \
79aceca5 2848}
cd6e9320
TH
2849#define GEN_STX(name, stop, opc2, opc3, type) \
2850 GEN_STX_E(name, stop, opc2, opc3, type, PPC_NONE)
79aceca5 2851
0c8aacd4
AJ
2852#define GEN_STS(name, stop, op, type) \
2853GEN_ST(name, stop, op | 0x20, type); \
2854GEN_STU(name, stop, op | 0x21, type); \
2855GEN_STUX(name, stop, 0x17, op | 0x01, type); \
2856GEN_STX(name, stop, 0x17, op | 0x00, type)
79aceca5
FB
2857
2858/* stb stbu stbux stbx */
0c8aacd4 2859GEN_STS(stb, st8, 0x06, PPC_INTEGER);
79aceca5 2860/* sth sthu sthux sthx */
0c8aacd4 2861GEN_STS(sth, st16, 0x0C, PPC_INTEGER);
79aceca5 2862/* stw stwu stwux stwx */
0c8aacd4 2863GEN_STS(stw, st32, 0x04, PPC_INTEGER);
d9bce9d9 2864#if defined(TARGET_PPC64)
0c8aacd4
AJ
2865GEN_STUX(std, st64, 0x15, 0x05, PPC_64B);
2866GEN_STX(std, st64, 0x15, 0x04, PPC_64B);
99e300ef
BS
2867
2868static void gen_std(DisasContext *ctx)
d9bce9d9 2869{
be147d08 2870 int rs;
b61f2753 2871 TCGv EA;
be147d08
JM
2872
2873 rs = rS(ctx->opcode);
2874 if ((ctx->opcode & 0x3) == 0x2) {
2875#if defined(CONFIG_USER_ONLY)
e06fcd75 2876 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
be147d08
JM
2877#else
2878 /* stq */
76db3ba4 2879 if (unlikely(ctx->mem_idx == 0)) {
e06fcd75 2880 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
be147d08
JM
2881 return;
2882 }
2883 if (unlikely(rs & 1)) {
e06fcd75 2884 gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL);
d9bce9d9
JM
2885 return;
2886 }
76db3ba4 2887 if (unlikely(ctx->le_mode)) {
be147d08 2888 /* Little-endian mode is not handled */
e06fcd75 2889 gen_exception_err(ctx, POWERPC_EXCP_ALIGN, POWERPC_EXCP_ALIGN_LE);
be147d08
JM
2890 return;
2891 }
76db3ba4 2892 gen_set_access_type(ctx, ACCESS_INT);
a7812ae4 2893 EA = tcg_temp_new();
76db3ba4
AJ
2894 gen_addr_imm_index(ctx, EA, 0x03);
2895 gen_qemu_st64(ctx, cpu_gpr[rs], EA);
2896 gen_addr_add(ctx, EA, EA, 8);
2897 gen_qemu_st64(ctx, cpu_gpr[rs+1], EA);
b61f2753 2898 tcg_temp_free(EA);
be147d08
JM
2899#endif
2900 } else {
2901 /* std / stdu */
2902 if (Rc(ctx->opcode)) {
2903 if (unlikely(rA(ctx->opcode) == 0)) {
e06fcd75 2904 gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL);
be147d08
JM
2905 return;
2906 }
2907 }
76db3ba4 2908 gen_set_access_type(ctx, ACCESS_INT);
a7812ae4 2909 EA = tcg_temp_new();
76db3ba4
AJ
2910 gen_addr_imm_index(ctx, EA, 0x03);
2911 gen_qemu_st64(ctx, cpu_gpr[rs], EA);
be147d08 2912 if (Rc(ctx->opcode))
b61f2753
AJ
2913 tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], EA);
2914 tcg_temp_free(EA);
d9bce9d9 2915 }
d9bce9d9
JM
2916}
2917#endif
79aceca5
FB
2918/*** Integer load and store with byte reverse ***/
2919/* lhbrx */
86178a57 2920static inline void gen_qemu_ld16ur(DisasContext *ctx, TCGv arg1, TCGv arg2)
b61f2753 2921{
76db3ba4
AJ
2922 tcg_gen_qemu_ld16u(arg1, arg2, ctx->mem_idx);
2923 if (likely(!ctx->le_mode)) {
fa3966a3 2924 tcg_gen_bswap16_tl(arg1, arg1);
76db3ba4 2925 }
b61f2753 2926}
0c8aacd4 2927GEN_LDX(lhbr, ld16ur, 0x16, 0x18, PPC_INTEGER);
b61f2753 2928
79aceca5 2929/* lwbrx */
86178a57 2930static inline void gen_qemu_ld32ur(DisasContext *ctx, TCGv arg1, TCGv arg2)
b61f2753 2931{
76db3ba4
AJ
2932 tcg_gen_qemu_ld32u(arg1, arg2, ctx->mem_idx);
2933 if (likely(!ctx->le_mode)) {
fa3966a3 2934 tcg_gen_bswap32_tl(arg1, arg1);
76db3ba4 2935 }
b61f2753 2936}
0c8aacd4 2937GEN_LDX(lwbr, ld32ur, 0x16, 0x10, PPC_INTEGER);
b61f2753 2938
cd6e9320
TH
2939#if defined(TARGET_PPC64)
2940/* ldbrx */
2941static inline void gen_qemu_ld64ur(DisasContext *ctx, TCGv arg1, TCGv arg2)
2942{
2943 tcg_gen_qemu_ld64(arg1, arg2, ctx->mem_idx);
2944 if (likely(!ctx->le_mode)) {
2945 tcg_gen_bswap64_tl(arg1, arg1);
2946 }
2947}
2948GEN_LDX_E(ldbr, ld64ur, 0x14, 0x10, PPC_NONE, PPC2_DBRX);
2949#endif /* TARGET_PPC64 */
2950
79aceca5 2951/* sthbrx */
86178a57 2952static inline void gen_qemu_st16r(DisasContext *ctx, TCGv arg1, TCGv arg2)
b61f2753 2953{
76db3ba4 2954 if (likely(!ctx->le_mode)) {
76db3ba4
AJ
2955 TCGv t0 = tcg_temp_new();
2956 tcg_gen_ext16u_tl(t0, arg1);
fa3966a3 2957 tcg_gen_bswap16_tl(t0, t0);
76db3ba4
AJ
2958 tcg_gen_qemu_st16(t0, arg2, ctx->mem_idx);
2959 tcg_temp_free(t0);
76db3ba4
AJ
2960 } else {
2961 tcg_gen_qemu_st16(arg1, arg2, ctx->mem_idx);
2962 }
b61f2753 2963}
0c8aacd4 2964GEN_STX(sthbr, st16r, 0x16, 0x1C, PPC_INTEGER);
b61f2753 2965
79aceca5 2966/* stwbrx */
86178a57 2967static inline void gen_qemu_st32r(DisasContext *ctx, TCGv arg1, TCGv arg2)
b61f2753 2968{
76db3ba4 2969 if (likely(!ctx->le_mode)) {
fa3966a3
AJ
2970 TCGv t0 = tcg_temp_new();
2971 tcg_gen_ext32u_tl(t0, arg1);
2972 tcg_gen_bswap32_tl(t0, t0);
76db3ba4
AJ
2973 tcg_gen_qemu_st32(t0, arg2, ctx->mem_idx);
2974 tcg_temp_free(t0);
76db3ba4
AJ
2975 } else {
2976 tcg_gen_qemu_st32(arg1, arg2, ctx->mem_idx);
2977 }
b61f2753 2978}
0c8aacd4 2979GEN_STX(stwbr, st32r, 0x16, 0x14, PPC_INTEGER);
79aceca5 2980
cd6e9320
TH
2981#if defined(TARGET_PPC64)
2982/* stdbrx */
2983static inline void gen_qemu_st64r(DisasContext *ctx, TCGv arg1, TCGv arg2)
2984{
2985 if (likely(!ctx->le_mode)) {
2986 TCGv t0 = tcg_temp_new();
2987 tcg_gen_bswap64_tl(t0, arg1);
2988 tcg_gen_qemu_st64(t0, arg2, ctx->mem_idx);
2989 tcg_temp_free(t0);
2990 } else {
2991 tcg_gen_qemu_st64(arg1, arg2, ctx->mem_idx);
2992 }
2993}
2994GEN_STX_E(stdbr, st64r, 0x14, 0x14, PPC_NONE, PPC2_DBRX);
2995#endif /* TARGET_PPC64 */
2996
79aceca5 2997/*** Integer load and store multiple ***/
99e300ef 2998
54623277 2999/* lmw */
99e300ef 3000static void gen_lmw(DisasContext *ctx)
79aceca5 3001{
76db3ba4
AJ
3002 TCGv t0;
3003 TCGv_i32 t1;
3004 gen_set_access_type(ctx, ACCESS_INT);
76a66253 3005 /* NIP cannot be restored if the memory exception comes from an helper */
d9bce9d9 3006 gen_update_nip(ctx, ctx->nip - 4);
76db3ba4
AJ
3007 t0 = tcg_temp_new();
3008 t1 = tcg_const_i32(rD(ctx->opcode));
3009 gen_addr_imm_index(ctx, t0, 0);
2f5a189c 3010 gen_helper_lmw(cpu_env, t0, t1);
ff4a62cd
AJ
3011 tcg_temp_free(t0);
3012 tcg_temp_free_i32(t1);
79aceca5
FB
3013}
3014
3015/* stmw */
99e300ef 3016static void gen_stmw(DisasContext *ctx)
79aceca5 3017{
76db3ba4
AJ
3018 TCGv t0;
3019 TCGv_i32 t1;
3020 gen_set_access_type(ctx, ACCESS_INT);
76a66253 3021 /* NIP cannot be restored if the memory exception comes from an helper */
d9bce9d9 3022 gen_update_nip(ctx, ctx->nip - 4);
76db3ba4
AJ
3023 t0 = tcg_temp_new();
3024 t1 = tcg_const_i32(rS(ctx->opcode));
3025 gen_addr_imm_index(ctx, t0, 0);
2f5a189c 3026 gen_helper_stmw(cpu_env, t0, t1);
ff4a62cd
AJ
3027 tcg_temp_free(t0);
3028 tcg_temp_free_i32(t1);
79aceca5
FB
3029}
3030
3031/*** Integer load and store strings ***/
54623277 3032
79aceca5 3033/* lswi */
3fc6c082 3034/* PowerPC32 specification says we must generate an exception if
9a64fbe4
FB
3035 * rA is in the range of registers to be loaded.
3036 * In an other hand, IBM says this is valid, but rA won't be loaded.
3037 * For now, I'll follow the spec...
3038 */
99e300ef 3039static void gen_lswi(DisasContext *ctx)
79aceca5 3040{
dfbc799d
AJ
3041 TCGv t0;
3042 TCGv_i32 t1, t2;
79aceca5
FB
3043 int nb = NB(ctx->opcode);
3044 int start = rD(ctx->opcode);
9a64fbe4 3045 int ra = rA(ctx->opcode);
79aceca5
FB
3046 int nr;
3047
3048 if (nb == 0)
3049 nb = 32;
3050 nr = nb / 4;
76a66253
JM
3051 if (unlikely(((start + nr) > 32 &&
3052 start <= ra && (start + nr - 32) > ra) ||
3053 ((start + nr) <= 32 && start <= ra && (start + nr) > ra))) {
e06fcd75 3054 gen_inval_exception(ctx, POWERPC_EXCP_INVAL_LSWX);
9fddaa0c 3055 return;
297d8e62 3056 }
76db3ba4 3057 gen_set_access_type(ctx, ACCESS_INT);
8dd4983c 3058 /* NIP cannot be restored if the memory exception comes from an helper */
d9bce9d9 3059 gen_update_nip(ctx, ctx->nip - 4);
dfbc799d 3060 t0 = tcg_temp_new();
76db3ba4 3061 gen_addr_register(ctx, t0);
dfbc799d
AJ
3062 t1 = tcg_const_i32(nb);
3063 t2 = tcg_const_i32(start);
2f5a189c 3064 gen_helper_lsw(cpu_env, t0, t1, t2);
dfbc799d
AJ
3065 tcg_temp_free(t0);
3066 tcg_temp_free_i32(t1);
3067 tcg_temp_free_i32(t2);
79aceca5
FB
3068}
3069
3070/* lswx */
99e300ef 3071static void gen_lswx(DisasContext *ctx)
79aceca5 3072{
76db3ba4
AJ
3073 TCGv t0;
3074 TCGv_i32 t1, t2, t3;
3075 gen_set_access_type(ctx, ACCESS_INT);
76a66253 3076 /* NIP cannot be restored if the memory exception comes from an helper */
d9bce9d9 3077 gen_update_nip(ctx, ctx->nip - 4);
76db3ba4
AJ
3078 t0 = tcg_temp_new();
3079 gen_addr_reg_index(ctx, t0);
3080 t1 = tcg_const_i32(rD(ctx->opcode));
3081 t2 = tcg_const_i32(rA(ctx->opcode));
3082 t3 = tcg_const_i32(rB(ctx->opcode));
2f5a189c 3083 gen_helper_lswx(cpu_env, t0, t1, t2, t3);
dfbc799d
AJ
3084 tcg_temp_free(t0);
3085 tcg_temp_free_i32(t1);
3086 tcg_temp_free_i32(t2);
3087 tcg_temp_free_i32(t3);
79aceca5
FB
3088}
3089
3090/* stswi */
99e300ef 3091static void gen_stswi(DisasContext *ctx)
79aceca5 3092{
76db3ba4
AJ
3093 TCGv t0;
3094 TCGv_i32 t1, t2;
4b3686fa 3095 int nb = NB(ctx->opcode);
76db3ba4 3096 gen_set_access_type(ctx, ACCESS_INT);
76a66253 3097 /* NIP cannot be restored if the memory exception comes from an helper */
d9bce9d9 3098 gen_update_nip(ctx, ctx->nip - 4);
76db3ba4
AJ
3099 t0 = tcg_temp_new();
3100 gen_addr_register(ctx, t0);
4b3686fa
FB
3101 if (nb == 0)
3102 nb = 32;
dfbc799d 3103 t1 = tcg_const_i32(nb);
76db3ba4 3104 t2 = tcg_const_i32(rS(ctx->opcode));
2f5a189c 3105 gen_helper_stsw(cpu_env, t0, t1, t2);
dfbc799d
AJ
3106 tcg_temp_free(t0);
3107 tcg_temp_free_i32(t1);
3108 tcg_temp_free_i32(t2);
79aceca5
FB
3109}
3110
3111/* stswx */
99e300ef 3112static void gen_stswx(DisasContext *ctx)
79aceca5 3113{
76db3ba4
AJ
3114 TCGv t0;
3115 TCGv_i32 t1, t2;
3116 gen_set_access_type(ctx, ACCESS_INT);
8dd4983c 3117 /* NIP cannot be restored if the memory exception comes from an helper */
5fafdf24 3118 gen_update_nip(ctx, ctx->nip - 4);
76db3ba4
AJ
3119 t0 = tcg_temp_new();
3120 gen_addr_reg_index(ctx, t0);
3121 t1 = tcg_temp_new_i32();
dfbc799d
AJ
3122 tcg_gen_trunc_tl_i32(t1, cpu_xer);
3123 tcg_gen_andi_i32(t1, t1, 0x7F);
76db3ba4 3124 t2 = tcg_const_i32(rS(ctx->opcode));
2f5a189c 3125 gen_helper_stsw(cpu_env, t0, t1, t2);
dfbc799d
AJ
3126 tcg_temp_free(t0);
3127 tcg_temp_free_i32(t1);
3128 tcg_temp_free_i32(t2);
79aceca5
FB
3129}
3130
3131/*** Memory synchronisation ***/
3132/* eieio */
99e300ef 3133static void gen_eieio(DisasContext *ctx)
79aceca5 3134{
79aceca5
FB
3135}
3136
3137/* isync */
99e300ef 3138static void gen_isync(DisasContext *ctx)
79aceca5 3139{
e06fcd75 3140 gen_stop_exception(ctx);
79aceca5
FB
3141}
3142
111bfab3 3143/* lwarx */
99e300ef 3144static void gen_lwarx(DisasContext *ctx)
79aceca5 3145{
76db3ba4 3146 TCGv t0;
18b21a2f 3147 TCGv gpr = cpu_gpr[rD(ctx->opcode)];
76db3ba4
AJ
3148 gen_set_access_type(ctx, ACCESS_RES);
3149 t0 = tcg_temp_local_new();
3150 gen_addr_reg_index(ctx, t0);
cf360a32 3151 gen_check_align(ctx, t0, 0x03);
18b21a2f 3152 gen_qemu_ld32u(ctx, gpr, t0);
cf360a32 3153 tcg_gen_mov_tl(cpu_reserve, t0);
1328c2bf 3154 tcg_gen_st_tl(gpr, cpu_env, offsetof(CPUPPCState, reserve_val));
cf360a32 3155 tcg_temp_free(t0);
79aceca5
FB
3156}
3157
4425265b
NF
3158#if defined(CONFIG_USER_ONLY)
3159static void gen_conditional_store (DisasContext *ctx, TCGv EA,
3160 int reg, int size)
3161{
3162 TCGv t0 = tcg_temp_new();
3163 uint32_t save_exception = ctx->exception;
3164
1328c2bf 3165 tcg_gen_st_tl(EA, cpu_env, offsetof(CPUPPCState, reserve_ea));
4425265b 3166 tcg_gen_movi_tl(t0, (size << 5) | reg);
1328c2bf 3167 tcg_gen_st_tl(t0, cpu_env, offsetof(CPUPPCState, reserve_info));
4425265b
NF
3168 tcg_temp_free(t0);
3169 gen_update_nip(ctx, ctx->nip-4);
3170 ctx->exception = POWERPC_EXCP_BRANCH;
3171 gen_exception(ctx, POWERPC_EXCP_STCX);
3172 ctx->exception = save_exception;
3173}
3174#endif
3175
79aceca5 3176/* stwcx. */
e8eaa2c0 3177static void gen_stwcx_(DisasContext *ctx)
79aceca5 3178{
76db3ba4
AJ
3179 TCGv t0;
3180 gen_set_access_type(ctx, ACCESS_RES);
3181 t0 = tcg_temp_local_new();
3182 gen_addr_reg_index(ctx, t0);
cf360a32 3183 gen_check_align(ctx, t0, 0x03);
4425265b
NF
3184#if defined(CONFIG_USER_ONLY)
3185 gen_conditional_store(ctx, t0, rS(ctx->opcode), 4);
3186#else
3187 {
3188 int l1;
3189
da91a00f 3190 tcg_gen_trunc_tl_i32(cpu_crf[0], cpu_so);
4425265b
NF
3191 l1 = gen_new_label();
3192 tcg_gen_brcond_tl(TCG_COND_NE, t0, cpu_reserve, l1);
3193 tcg_gen_ori_i32(cpu_crf[0], cpu_crf[0], 1 << CRF_EQ);
3194 gen_qemu_st32(ctx, cpu_gpr[rS(ctx->opcode)], t0);
3195 gen_set_label(l1);
3196 tcg_gen_movi_tl(cpu_reserve, -1);
3197 }
3198#endif
cf360a32 3199 tcg_temp_free(t0);
79aceca5
FB
3200}
3201
426613db 3202#if defined(TARGET_PPC64)
426613db 3203/* ldarx */
99e300ef 3204static void gen_ldarx(DisasContext *ctx)
426613db 3205{
76db3ba4 3206 TCGv t0;
18b21a2f 3207 TCGv gpr = cpu_gpr[rD(ctx->opcode)];
76db3ba4
AJ
3208 gen_set_access_type(ctx, ACCESS_RES);
3209 t0 = tcg_temp_local_new();
3210 gen_addr_reg_index(ctx, t0);
cf360a32 3211 gen_check_align(ctx, t0, 0x07);
18b21a2f 3212 gen_qemu_ld64(ctx, gpr, t0);
cf360a32 3213 tcg_gen_mov_tl(cpu_reserve, t0);
1328c2bf 3214 tcg_gen_st_tl(gpr, cpu_env, offsetof(CPUPPCState, reserve_val));
cf360a32 3215 tcg_temp_free(t0);
426613db
JM
3216}
3217
3218/* stdcx. */
e8eaa2c0 3219static void gen_stdcx_(DisasContext *ctx)
426613db 3220{
76db3ba4
AJ
3221 TCGv t0;
3222 gen_set_access_type(ctx, ACCESS_RES);
3223 t0 = tcg_temp_local_new();
3224 gen_addr_reg_index(ctx, t0);
cf360a32 3225 gen_check_align(ctx, t0, 0x07);
4425265b
NF
3226#if defined(CONFIG_USER_ONLY)
3227 gen_conditional_store(ctx, t0, rS(ctx->opcode), 8);
3228#else
3229 {
3230 int l1;
da91a00f 3231 tcg_gen_trunc_tl_i32(cpu_crf[0], cpu_so);
4425265b
NF
3232 l1 = gen_new_label();
3233 tcg_gen_brcond_tl(TCG_COND_NE, t0, cpu_reserve, l1);
3234 tcg_gen_ori_i32(cpu_crf[0], cpu_crf[0], 1 << CRF_EQ);
3235 gen_qemu_st64(ctx, cpu_gpr[rS(ctx->opcode)], t0);
3236 gen_set_label(l1);
3237 tcg_gen_movi_tl(cpu_reserve, -1);
3238 }
3239#endif
cf360a32 3240 tcg_temp_free(t0);
426613db
JM
3241}
3242#endif /* defined(TARGET_PPC64) */
3243
79aceca5 3244/* sync */
99e300ef 3245static void gen_sync(DisasContext *ctx)
79aceca5 3246{
79aceca5
FB
3247}
3248
0db1b20e 3249/* wait */
99e300ef 3250static void gen_wait(DisasContext *ctx)
0db1b20e 3251{
931ff272 3252 TCGv_i32 t0 = tcg_temp_new_i32();
259186a7
AF
3253 tcg_gen_st_i32(t0, cpu_env,
3254 -offsetof(PowerPCCPU, env) + offsetof(CPUState, halted));
931ff272 3255 tcg_temp_free_i32(t0);
0db1b20e 3256 /* Stop translation, as the CPU is supposed to sleep from now */
e06fcd75 3257 gen_exception_err(ctx, EXCP_HLT, 1);
0db1b20e
JM
3258}
3259
79aceca5 3260/*** Floating-point load ***/
a0d7d5a7 3261#define GEN_LDF(name, ldop, opc, type) \
99e300ef 3262static void glue(gen_, name)(DisasContext *ctx) \
79aceca5 3263{ \
a0d7d5a7 3264 TCGv EA; \
76a66253 3265 if (unlikely(!ctx->fpu_enabled)) { \
e06fcd75 3266 gen_exception(ctx, POWERPC_EXCP_FPU); \
4ecc3190
FB
3267 return; \
3268 } \
76db3ba4 3269 gen_set_access_type(ctx, ACCESS_FLOAT); \
a0d7d5a7 3270 EA = tcg_temp_new(); \
76db3ba4
AJ
3271 gen_addr_imm_index(ctx, EA, 0); \
3272 gen_qemu_##ldop(ctx, cpu_fpr[rD(ctx->opcode)], EA); \
a0d7d5a7 3273 tcg_temp_free(EA); \
79aceca5
FB
3274}
3275
a0d7d5a7 3276#define GEN_LDUF(name, ldop, opc, type) \
99e300ef 3277static void glue(gen_, name##u)(DisasContext *ctx) \
79aceca5 3278{ \
a0d7d5a7 3279 TCGv EA; \
76a66253 3280 if (unlikely(!ctx->fpu_enabled)) { \
e06fcd75 3281 gen_exception(ctx, POWERPC_EXCP_FPU); \
4ecc3190
FB
3282 return; \
3283 } \
76a66253 3284 if (unlikely(rA(ctx->opcode) == 0)) { \
e06fcd75 3285 gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL); \
9fddaa0c 3286 return; \
9a64fbe4 3287 } \
76db3ba4 3288 gen_set_access_type(ctx, ACCESS_FLOAT); \
a0d7d5a7 3289 EA = tcg_temp_new(); \
76db3ba4
AJ
3290 gen_addr_imm_index(ctx, EA, 0); \
3291 gen_qemu_##ldop(ctx, cpu_fpr[rD(ctx->opcode)], EA); \
a0d7d5a7
AJ
3292 tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], EA); \
3293 tcg_temp_free(EA); \
79aceca5
FB
3294}
3295
a0d7d5a7 3296#define GEN_LDUXF(name, ldop, opc, type) \
99e300ef 3297static void glue(gen_, name##ux)(DisasContext *ctx) \
79aceca5 3298{ \
a0d7d5a7 3299 TCGv EA; \
76a66253 3300 if (unlikely(!ctx->fpu_enabled)) { \
e06fcd75 3301 gen_exception(ctx, POWERPC_EXCP_FPU); \
4ecc3190
FB
3302 return; \
3303 } \
76a66253 3304 if (unlikely(rA(ctx->opcode) == 0)) { \
e06fcd75 3305 gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL); \
9fddaa0c 3306 return; \
9a64fbe4 3307 } \
76db3ba4 3308 gen_set_access_type(ctx, ACCESS_FLOAT); \
a0d7d5a7 3309 EA = tcg_temp_new(); \
76db3ba4
AJ
3310 gen_addr_reg_index(ctx, EA); \
3311 gen_qemu_##ldop(ctx, cpu_fpr[rD(ctx->opcode)], EA); \
a0d7d5a7
AJ
3312 tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], EA); \
3313 tcg_temp_free(EA); \
79aceca5
FB
3314}
3315
a0d7d5a7 3316#define GEN_LDXF(name, ldop, opc2, opc3, type) \
99e300ef 3317static void glue(gen_, name##x)(DisasContext *ctx) \
79aceca5 3318{ \
a0d7d5a7 3319 TCGv EA; \
76a66253 3320 if (unlikely(!ctx->fpu_enabled)) { \
e06fcd75 3321 gen_exception(ctx, POWERPC_EXCP_FPU); \
4ecc3190
FB
3322 return; \
3323 } \
76db3ba4 3324 gen_set_access_type(ctx, ACCESS_FLOAT); \
a0d7d5a7 3325 EA = tcg_temp_new(); \
76db3ba4
AJ
3326 gen_addr_reg_index(ctx, EA); \
3327 gen_qemu_##ldop(ctx, cpu_fpr[rD(ctx->opcode)], EA); \
a0d7d5a7 3328 tcg_temp_free(EA); \
79aceca5
FB
3329}
3330
a0d7d5a7
AJ
3331#define GEN_LDFS(name, ldop, op, type) \
3332GEN_LDF(name, ldop, op | 0x20, type); \
3333GEN_LDUF(name, ldop, op | 0x21, type); \
3334GEN_LDUXF(name, ldop, op | 0x01, type); \
3335GEN_LDXF(name, ldop, 0x17, op | 0x00, type)
3336
636aa200 3337static inline void gen_qemu_ld32fs(DisasContext *ctx, TCGv_i64 arg1, TCGv arg2)
a0d7d5a7
AJ
3338{
3339 TCGv t0 = tcg_temp_new();
3340 TCGv_i32 t1 = tcg_temp_new_i32();
76db3ba4 3341 gen_qemu_ld32u(ctx, t0, arg2);
a0d7d5a7
AJ
3342 tcg_gen_trunc_tl_i32(t1, t0);
3343 tcg_temp_free(t0);
8e703949 3344 gen_helper_float32_to_float64(arg1, cpu_env, t1);
a0d7d5a7
AJ
3345 tcg_temp_free_i32(t1);
3346}
79aceca5 3347
a0d7d5a7
AJ
3348 /* lfd lfdu lfdux lfdx */
3349GEN_LDFS(lfd, ld64, 0x12, PPC_FLOAT);
3350 /* lfs lfsu lfsux lfsx */
3351GEN_LDFS(lfs, ld32fs, 0x10, PPC_FLOAT);
79aceca5 3352
05050ee8
AJ
3353/* lfdp */
3354static void gen_lfdp(DisasContext *ctx)
3355{
3356 TCGv EA;
3357 if (unlikely(!ctx->fpu_enabled)) {
3358 gen_exception(ctx, POWERPC_EXCP_FPU);
3359 return;
3360 }
3361 gen_set_access_type(ctx, ACCESS_FLOAT);
3362 EA = tcg_temp_new();
3363 gen_addr_imm_index(ctx, EA, 0); \
3364 if (unlikely(ctx->le_mode)) {
3365 gen_qemu_ld64(ctx, cpu_fpr[rD(ctx->opcode) + 1], EA);
3366 tcg_gen_addi_tl(EA, EA, 8);
3367 gen_qemu_ld64(ctx, cpu_fpr[rD(ctx->opcode)], EA);
3368 } else {
3369 gen_qemu_ld64(ctx, cpu_fpr[rD(ctx->opcode)], EA);
3370 tcg_gen_addi_tl(EA, EA, 8);
3371 gen_qemu_ld64(ctx, cpu_fpr[rD(ctx->opcode) + 1], EA);
3372 }
3373 tcg_temp_free(EA);
3374}
3375
3376/* lfdpx */
3377static void gen_lfdpx(DisasContext *ctx)
3378{
3379 TCGv EA;
3380 if (unlikely(!ctx->fpu_enabled)) {
3381 gen_exception(ctx, POWERPC_EXCP_FPU);
3382 return;
3383 }
3384 gen_set_access_type(ctx, ACCESS_FLOAT);
3385 EA = tcg_temp_new();
3386 gen_addr_reg_index(ctx, EA);
3387 if (unlikely(ctx->le_mode)) {
3388 gen_qemu_ld64(ctx, cpu_fpr[rD(ctx->opcode) + 1], EA);
3389 tcg_gen_addi_tl(EA, EA, 8);
3390 gen_qemu_ld64(ctx, cpu_fpr[rD(ctx->opcode)], EA);
3391 } else {
3392 gen_qemu_ld64(ctx, cpu_fpr[rD(ctx->opcode)], EA);
3393 tcg_gen_addi_tl(EA, EA, 8);
3394 gen_qemu_ld64(ctx, cpu_fpr[rD(ctx->opcode) + 1], EA);
3395 }
3396 tcg_temp_free(EA);
3397}
3398
199f830d
AJ
3399/* lfiwax */
3400static void gen_lfiwax(DisasContext *ctx)
3401{
3402 TCGv EA;
3403 TCGv t0;
3404 if (unlikely(!ctx->fpu_enabled)) {
3405 gen_exception(ctx, POWERPC_EXCP_FPU);
3406 return;
3407 }
3408 gen_set_access_type(ctx, ACCESS_FLOAT);
3409 EA = tcg_temp_new();
3410 t0 = tcg_temp_new();
3411 gen_addr_reg_index(ctx, EA);
909eedb7 3412 gen_qemu_ld32s(ctx, t0, EA);
199f830d 3413 tcg_gen_ext_tl_i64(cpu_fpr[rD(ctx->opcode)], t0);
199f830d
AJ
3414 tcg_temp_free(EA);
3415 tcg_temp_free(t0);
3416}
3417
79aceca5 3418/*** Floating-point store ***/
a0d7d5a7 3419#define GEN_STF(name, stop, opc, type) \
99e300ef 3420static void glue(gen_, name)(DisasContext *ctx) \
79aceca5 3421{ \
a0d7d5a7 3422 TCGv EA; \
76a66253 3423 if (unlikely(!ctx->fpu_enabled)) { \
e06fcd75 3424 gen_exception(ctx, POWERPC_EXCP_FPU); \
4ecc3190
FB
3425 return; \
3426 } \
76db3ba4 3427 gen_set_access_type(ctx, ACCESS_FLOAT); \
a0d7d5a7 3428 EA = tcg_temp_new(); \
76db3ba4
AJ
3429 gen_addr_imm_index(ctx, EA, 0); \
3430 gen_qemu_##stop(ctx, cpu_fpr[rS(ctx->opcode)], EA); \
a0d7d5a7 3431 tcg_temp_free(EA); \
79aceca5
FB
3432}
3433
a0d7d5a7 3434#define GEN_STUF(name, stop, opc, type) \
99e300ef 3435static void glue(gen_, name##u)(DisasContext *ctx) \
79aceca5 3436{ \
a0d7d5a7 3437 TCGv EA; \
76a66253 3438 if (unlikely(!ctx->fpu_enabled)) { \
e06fcd75 3439 gen_exception(ctx, POWERPC_EXCP_FPU); \
4ecc3190
FB
3440 return; \
3441 } \
76a66253 3442 if (unlikely(rA(ctx->opcode) == 0)) { \
e06fcd75 3443 gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL); \
9fddaa0c 3444 return; \
9a64fbe4 3445 } \
76db3ba4 3446 gen_set_access_type(ctx, ACCESS_FLOAT); \
a0d7d5a7 3447 EA = tcg_temp_new(); \
76db3ba4
AJ
3448 gen_addr_imm_index(ctx, EA, 0); \
3449 gen_qemu_##stop(ctx, cpu_fpr[rS(ctx->opcode)], EA); \
a0d7d5a7
AJ
3450 tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], EA); \
3451 tcg_temp_free(EA); \
79aceca5
FB
3452}
3453
a0d7d5a7 3454#define GEN_STUXF(name, stop, opc, type) \
99e300ef 3455static void glue(gen_, name##ux)(DisasContext *ctx) \
79aceca5 3456{ \
a0d7d5a7 3457 TCGv EA; \
76a66253 3458 if (unlikely(!ctx->fpu_enabled)) { \
e06fcd75 3459 gen_exception(ctx, POWERPC_EXCP_FPU); \
4ecc3190
FB
3460 return; \
3461 } \
76a66253 3462 if (unlikely(rA(ctx->opcode) == 0)) { \
e06fcd75 3463 gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL); \
9fddaa0c 3464 return; \
9a64fbe4 3465 } \
76db3ba4 3466 gen_set_access_type(ctx, ACCESS_FLOAT); \
a0d7d5a7 3467 EA = tcg_temp_new(); \
76db3ba4
AJ
3468 gen_addr_reg_index(ctx, EA); \
3469 gen_qemu_##stop(ctx, cpu_fpr[rS(ctx->opcode)], EA); \
a0d7d5a7
AJ
3470 tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], EA); \
3471 tcg_temp_free(EA); \
79aceca5
FB
3472}
3473
a0d7d5a7 3474#define GEN_STXF(name, stop, opc2, opc3, type) \
99e300ef 3475static void glue(gen_, name##x)(DisasContext *ctx) \
79aceca5 3476{ \
a0d7d5a7 3477 TCGv EA; \
76a66253 3478 if (unlikely(!ctx->fpu_enabled)) { \
e06fcd75 3479 gen_exception(ctx, POWERPC_EXCP_FPU); \
4ecc3190
FB
3480 return; \
3481 } \
76db3ba4 3482 gen_set_access_type(ctx, ACCESS_FLOAT); \
a0d7d5a7 3483 EA = tcg_temp_new(); \
76db3ba4
AJ
3484 gen_addr_reg_index(ctx, EA); \
3485 gen_qemu_##stop(ctx, cpu_fpr[rS(ctx->opcode)], EA); \
a0d7d5a7 3486 tcg_temp_free(EA); \
79aceca5
FB
3487}
3488
a0d7d5a7
AJ
3489#define GEN_STFS(name, stop, op, type) \
3490GEN_STF(name, stop, op | 0x20, type); \
3491GEN_STUF(name, stop, op | 0x21, type); \
3492GEN_STUXF(name, stop, op | 0x01, type); \
3493GEN_STXF(name, stop, 0x17, op | 0x00, type)
3494
636aa200 3495static inline void gen_qemu_st32fs(DisasContext *ctx, TCGv_i64 arg1, TCGv arg2)
a0d7d5a7
AJ
3496{
3497 TCGv_i32 t0 = tcg_temp_new_i32();
3498 TCGv t1 = tcg_temp_new();
8e703949 3499 gen_helper_float64_to_float32(t0, cpu_env, arg1);
a0d7d5a7
AJ
3500 tcg_gen_extu_i32_tl(t1, t0);
3501 tcg_temp_free_i32(t0);
76db3ba4 3502 gen_qemu_st32(ctx, t1, arg2);
a0d7d5a7
AJ
3503 tcg_temp_free(t1);
3504}
79aceca5
FB
3505
3506/* stfd stfdu stfdux stfdx */
a0d7d5a7 3507GEN_STFS(stfd, st64, 0x16, PPC_FLOAT);
79aceca5 3508/* stfs stfsu stfsux stfsx */
a0d7d5a7 3509GEN_STFS(stfs, st32fs, 0x14, PPC_FLOAT);
79aceca5 3510
44bc0c4d
AJ
3511/* stfdp */
3512static void gen_stfdp(DisasContext *ctx)
3513{
3514 TCGv EA;
3515 if (unlikely(!ctx->fpu_enabled)) {
3516 gen_exception(ctx, POWERPC_EXCP_FPU);
3517 return;
3518 }
3519 gen_set_access_type(ctx, ACCESS_FLOAT);
3520 EA = tcg_temp_new();
3521 gen_addr_imm_index(ctx, EA, 0); \
3522 if (unlikely(ctx->le_mode)) {
3523 gen_qemu_st64(ctx, cpu_fpr[rD(ctx->opcode) + 1], EA);
3524 tcg_gen_addi_tl(EA, EA, 8);
3525 gen_qemu_st64(ctx, cpu_fpr[rD(ctx->opcode)], EA);
3526 } else {
3527 gen_qemu_st64(ctx, cpu_fpr[rD(ctx->opcode)], EA);
3528 tcg_gen_addi_tl(EA, EA, 8);
3529 gen_qemu_st64(ctx, cpu_fpr[rD(ctx->opcode) + 1], EA);
3530 }
3531 tcg_temp_free(EA);
3532}
3533
3534/* stfdpx */
3535static void gen_stfdpx(DisasContext *ctx)
3536{
3537 TCGv EA;
3538 if (unlikely(!ctx->fpu_enabled)) {
3539 gen_exception(ctx, POWERPC_EXCP_FPU);
3540 return;
3541 }
3542 gen_set_access_type(ctx, ACCESS_FLOAT);
3543 EA = tcg_temp_new();
3544 gen_addr_reg_index(ctx, EA);
3545 if (unlikely(ctx->le_mode)) {
3546 gen_qemu_st64(ctx, cpu_fpr[rD(ctx->opcode) + 1], EA);
3547 tcg_gen_addi_tl(EA, EA, 8);
3548 gen_qemu_st64(ctx, cpu_fpr[rD(ctx->opcode)], EA);
3549 } else {
3550 gen_qemu_st64(ctx, cpu_fpr[rD(ctx->opcode)], EA);
3551 tcg_gen_addi_tl(EA, EA, 8);
3552 gen_qemu_st64(ctx, cpu_fpr[rD(ctx->opcode) + 1], EA);
3553 }
3554 tcg_temp_free(EA);
3555}
3556
79aceca5 3557/* Optional: */
636aa200 3558static inline void gen_qemu_st32fiw(DisasContext *ctx, TCGv_i64 arg1, TCGv arg2)
a0d7d5a7
AJ
3559{
3560 TCGv t0 = tcg_temp_new();
3561 tcg_gen_trunc_i64_tl(t0, arg1),
76db3ba4 3562 gen_qemu_st32(ctx, t0, arg2);
a0d7d5a7
AJ
3563 tcg_temp_free(t0);
3564}
79aceca5 3565/* stfiwx */
a0d7d5a7 3566GEN_STXF(stfiw, st32fiw, 0x17, 0x1E, PPC_FLOAT_STFIWX);
79aceca5 3567
697ab892
DG
3568static inline void gen_update_cfar(DisasContext *ctx, target_ulong nip)
3569{
3570#if defined(TARGET_PPC64)
3571 if (ctx->has_cfar)
3572 tcg_gen_movi_tl(cpu_cfar, nip);
3573#endif
3574}
3575
79aceca5 3576/*** Branch ***/
636aa200 3577static inline void gen_goto_tb(DisasContext *ctx, int n, target_ulong dest)
c1942362
FB
3578{
3579 TranslationBlock *tb;
3580 tb = ctx->tb;
e0c8f9ce 3581 if (NARROW_MODE(ctx)) {
a2ffb812 3582 dest = (uint32_t) dest;
e0c8f9ce 3583 }
57fec1fe 3584 if ((tb->pc & TARGET_PAGE_MASK) == (dest & TARGET_PAGE_MASK) &&
8cbcb4fa 3585 likely(!ctx->singlestep_enabled)) {
57fec1fe 3586 tcg_gen_goto_tb(n);
a2ffb812 3587 tcg_gen_movi_tl(cpu_nip, dest & ~3);
8cfd0495 3588 tcg_gen_exit_tb((uintptr_t)tb + n);
c1942362 3589 } else {
a2ffb812 3590 tcg_gen_movi_tl(cpu_nip, dest & ~3);
8cbcb4fa
AJ
3591 if (unlikely(ctx->singlestep_enabled)) {
3592 if ((ctx->singlestep_enabled &
bdc4e053 3593 (CPU_BRANCH_STEP | CPU_SINGLE_STEP)) &&
f0cc4aa8
JG
3594 (ctx->exception == POWERPC_EXCP_BRANCH ||
3595 ctx->exception == POWERPC_EXCP_TRACE)) {
8cbcb4fa
AJ
3596 target_ulong tmp = ctx->nip;
3597 ctx->nip = dest;
e06fcd75 3598 gen_exception(ctx, POWERPC_EXCP_TRACE);
8cbcb4fa
AJ
3599 ctx->nip = tmp;
3600 }
3601 if (ctx->singlestep_enabled & GDBSTUB_SINGLE_STEP) {
e06fcd75 3602 gen_debug_exception(ctx);
8cbcb4fa
AJ
3603 }
3604 }
57fec1fe 3605 tcg_gen_exit_tb(0);
c1942362 3606 }
c53be334
FB
3607}
3608
636aa200 3609static inline void gen_setlr(DisasContext *ctx, target_ulong nip)
e1833e1f 3610{
e0c8f9ce
RH
3611 if (NARROW_MODE(ctx)) {
3612 nip = (uint32_t)nip;
3613 }
3614 tcg_gen_movi_tl(cpu_lr, nip);
e1833e1f
JM
3615}
3616
79aceca5 3617/* b ba bl bla */
99e300ef 3618static void gen_b(DisasContext *ctx)
79aceca5 3619{
76a66253 3620 target_ulong li, target;
38a64f9d 3621
8cbcb4fa 3622 ctx->exception = POWERPC_EXCP_BRANCH;
38a64f9d 3623 /* sign extend LI */
e0c8f9ce
RH
3624 li = LI(ctx->opcode);
3625 li = (li ^ 0x02000000) - 0x02000000;
3626 if (likely(AA(ctx->opcode) == 0)) {
046d6672 3627 target = ctx->nip + li - 4;
e0c8f9ce 3628 } else {
9a64fbe4 3629 target = li;
e0c8f9ce
RH
3630 }
3631 if (LK(ctx->opcode)) {
e1833e1f 3632 gen_setlr(ctx, ctx->nip);
e0c8f9ce 3633 }
697ab892 3634 gen_update_cfar(ctx, ctx->nip);
c1942362 3635 gen_goto_tb(ctx, 0, target);
79aceca5
FB
3636}
3637
e98a6e40
FB
3638#define BCOND_IM 0
3639#define BCOND_LR 1
3640#define BCOND_CTR 2
3641
636aa200 3642static inline void gen_bcond(DisasContext *ctx, int type)
d9bce9d9 3643{
d9bce9d9 3644 uint32_t bo = BO(ctx->opcode);
05f92404 3645 int l1;
a2ffb812 3646 TCGv target;
e98a6e40 3647
8cbcb4fa 3648 ctx->exception = POWERPC_EXCP_BRANCH;
a2ffb812 3649 if (type == BCOND_LR || type == BCOND_CTR) {
a7812ae4 3650 target = tcg_temp_local_new();
a2ffb812
AJ
3651 if (type == BCOND_CTR)
3652 tcg_gen_mov_tl(target, cpu_ctr);
3653 else
3654 tcg_gen_mov_tl(target, cpu_lr);
d2e9fd8f 3655 } else {
3656 TCGV_UNUSED(target);
e98a6e40 3657 }
e1833e1f
JM
3658 if (LK(ctx->opcode))
3659 gen_setlr(ctx, ctx->nip);
a2ffb812
AJ
3660 l1 = gen_new_label();
3661 if ((bo & 0x4) == 0) {
3662 /* Decrement and test CTR */
a7812ae4 3663 TCGv temp = tcg_temp_new();
a2ffb812 3664 if (unlikely(type == BCOND_CTR)) {
e06fcd75 3665 gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL);
a2ffb812
AJ
3666 return;
3667 }
3668 tcg_gen_subi_tl(cpu_ctr, cpu_ctr, 1);
e0c8f9ce 3669 if (NARROW_MODE(ctx)) {
a2ffb812 3670 tcg_gen_ext32u_tl(temp, cpu_ctr);
e0c8f9ce 3671 } else {
a2ffb812 3672 tcg_gen_mov_tl(temp, cpu_ctr);
e0c8f9ce 3673 }
a2ffb812
AJ
3674 if (bo & 0x2) {
3675 tcg_gen_brcondi_tl(TCG_COND_NE, temp, 0, l1);
3676 } else {
3677 tcg_gen_brcondi_tl(TCG_COND_EQ, temp, 0, l1);
e98a6e40 3678 }
a7812ae4 3679 tcg_temp_free(temp);
a2ffb812
AJ
3680 }
3681 if ((bo & 0x10) == 0) {
3682 /* Test CR */
3683 uint32_t bi = BI(ctx->opcode);
3684 uint32_t mask = 1 << (3 - (bi & 0x03));
a7812ae4 3685 TCGv_i32 temp = tcg_temp_new_i32();
a2ffb812 3686
d9bce9d9 3687 if (bo & 0x8) {
a2ffb812
AJ
3688 tcg_gen_andi_i32(temp, cpu_crf[bi >> 2], mask);
3689 tcg_gen_brcondi_i32(TCG_COND_EQ, temp, 0, l1);
d9bce9d9 3690 } else {
a2ffb812
AJ
3691 tcg_gen_andi_i32(temp, cpu_crf[bi >> 2], mask);
3692 tcg_gen_brcondi_i32(TCG_COND_NE, temp, 0, l1);
d9bce9d9 3693 }
a7812ae4 3694 tcg_temp_free_i32(temp);
d9bce9d9 3695 }
697ab892 3696 gen_update_cfar(ctx, ctx->nip);
e98a6e40 3697 if (type == BCOND_IM) {
a2ffb812
AJ
3698 target_ulong li = (target_long)((int16_t)(BD(ctx->opcode)));
3699 if (likely(AA(ctx->opcode) == 0)) {
3700 gen_goto_tb(ctx, 0, ctx->nip + li - 4);
3701 } else {
3702 gen_goto_tb(ctx, 0, li);
3703 }
c53be334 3704 gen_set_label(l1);
c1942362 3705 gen_goto_tb(ctx, 1, ctx->nip);
e98a6e40 3706 } else {
e0c8f9ce 3707 if (NARROW_MODE(ctx)) {
a2ffb812 3708 tcg_gen_andi_tl(cpu_nip, target, (uint32_t)~3);
e0c8f9ce 3709 } else {
a2ffb812 3710 tcg_gen_andi_tl(cpu_nip, target, ~3);
e0c8f9ce 3711 }
a2ffb812
AJ
3712 tcg_gen_exit_tb(0);
3713 gen_set_label(l1);
e0c8f9ce 3714 gen_update_nip(ctx, ctx->nip);
57fec1fe 3715 tcg_gen_exit_tb(0);
08e46e54 3716 }
e98a6e40
FB
3717}
3718
99e300ef 3719static void gen_bc(DisasContext *ctx)
3b46e624 3720{
e98a6e40
FB
3721 gen_bcond(ctx, BCOND_IM);
3722}
3723
99e300ef 3724static void gen_bcctr(DisasContext *ctx)
3b46e624 3725{
e98a6e40
FB
3726 gen_bcond(ctx, BCOND_CTR);
3727}
3728
99e300ef 3729static void gen_bclr(DisasContext *ctx)
3b46e624 3730{
e98a6e40
FB
3731 gen_bcond(ctx, BCOND_LR);
3732}
79aceca5
FB
3733
3734/*** Condition register logical ***/
e1571908 3735#define GEN_CRLOGIC(name, tcg_op, opc) \
99e300ef 3736static void glue(gen_, name)(DisasContext *ctx) \
79aceca5 3737{ \
fc0d441e
JM
3738 uint8_t bitmask; \
3739 int sh; \
a7812ae4 3740 TCGv_i32 t0, t1; \
fc0d441e 3741 sh = (crbD(ctx->opcode) & 0x03) - (crbA(ctx->opcode) & 0x03); \
a7812ae4 3742 t0 = tcg_temp_new_i32(); \
fc0d441e 3743 if (sh > 0) \
fea0c503 3744 tcg_gen_shri_i32(t0, cpu_crf[crbA(ctx->opcode) >> 2], sh); \
fc0d441e 3745 else if (sh < 0) \
fea0c503 3746 tcg_gen_shli_i32(t0, cpu_crf[crbA(ctx->opcode) >> 2], -sh); \
e1571908 3747 else \
fea0c503 3748 tcg_gen_mov_i32(t0, cpu_crf[crbA(ctx->opcode) >> 2]); \
a7812ae4 3749 t1 = tcg_temp_new_i32(); \
fc0d441e
JM
3750 sh = (crbD(ctx->opcode) & 0x03) - (crbB(ctx->opcode) & 0x03); \
3751 if (sh > 0) \
fea0c503 3752 tcg_gen_shri_i32(t1, cpu_crf[crbB(ctx->opcode) >> 2], sh); \
fc0d441e 3753 else if (sh < 0) \
fea0c503 3754 tcg_gen_shli_i32(t1, cpu_crf[crbB(ctx->opcode) >> 2], -sh); \
e1571908 3755 else \
fea0c503
AJ
3756 tcg_gen_mov_i32(t1, cpu_crf[crbB(ctx->opcode) >> 2]); \
3757 tcg_op(t0, t0, t1); \
fc0d441e 3758 bitmask = 1 << (3 - (crbD(ctx->opcode) & 0x03)); \
fea0c503
AJ
3759 tcg_gen_andi_i32(t0, t0, bitmask); \
3760 tcg_gen_andi_i32(t1, cpu_crf[crbD(ctx->opcode) >> 2], ~bitmask); \
3761 tcg_gen_or_i32(cpu_crf[crbD(ctx->opcode) >> 2], t0, t1); \
a7812ae4
PB
3762 tcg_temp_free_i32(t0); \
3763 tcg_temp_free_i32(t1); \
79aceca5
FB
3764}
3765
3766/* crand */
e1571908 3767GEN_CRLOGIC(crand, tcg_gen_and_i32, 0x08);
79aceca5 3768/* crandc */
e1571908 3769GEN_CRLOGIC(crandc, tcg_gen_andc_i32, 0x04);
79aceca5 3770/* creqv */
e1571908 3771GEN_CRLOGIC(creqv, tcg_gen_eqv_i32, 0x09);
79aceca5 3772/* crnand */
e1571908 3773GEN_CRLOGIC(crnand, tcg_gen_nand_i32, 0x07);
79aceca5 3774/* crnor */
e1571908 3775GEN_CRLOGIC(crnor, tcg_gen_nor_i32, 0x01);
79aceca5 3776/* cror */
e1571908 3777GEN_CRLOGIC(cror, tcg_gen_or_i32, 0x0E);
79aceca5 3778/* crorc */
e1571908 3779GEN_CRLOGIC(crorc, tcg_gen_orc_i32, 0x0D);
79aceca5 3780/* crxor */
e1571908 3781GEN_CRLOGIC(crxor, tcg_gen_xor_i32, 0x06);
99e300ef 3782
54623277 3783/* mcrf */
99e300ef 3784static void gen_mcrf(DisasContext *ctx)
79aceca5 3785{
47e4661c 3786 tcg_gen_mov_i32(cpu_crf[crfD(ctx->opcode)], cpu_crf[crfS(ctx->opcode)]);
79aceca5
FB
3787}
3788
3789/*** System linkage ***/
99e300ef 3790
54623277 3791/* rfi (mem_idx only) */
99e300ef 3792static void gen_rfi(DisasContext *ctx)
79aceca5 3793{
9a64fbe4 3794#if defined(CONFIG_USER_ONLY)
e06fcd75 3795 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
9a64fbe4
FB
3796#else
3797 /* Restore CPU state */
76db3ba4 3798 if (unlikely(!ctx->mem_idx)) {
e06fcd75 3799 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
9fddaa0c 3800 return;
9a64fbe4 3801 }
697ab892 3802 gen_update_cfar(ctx, ctx->nip);
e5f17ac6 3803 gen_helper_rfi(cpu_env);
e06fcd75 3804 gen_sync_exception(ctx);
9a64fbe4 3805#endif
79aceca5
FB
3806}
3807
426613db 3808#if defined(TARGET_PPC64)
99e300ef 3809static void gen_rfid(DisasContext *ctx)
426613db
JM
3810{
3811#if defined(CONFIG_USER_ONLY)
e06fcd75 3812 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
426613db
JM
3813#else
3814 /* Restore CPU state */
76db3ba4 3815 if (unlikely(!ctx->mem_idx)) {
e06fcd75 3816 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
426613db
JM
3817 return;
3818 }
697ab892 3819 gen_update_cfar(ctx, ctx->nip);
e5f17ac6 3820 gen_helper_rfid(cpu_env);
e06fcd75 3821 gen_sync_exception(ctx);
426613db
JM
3822#endif
3823}
426613db 3824
99e300ef 3825static void gen_hrfid(DisasContext *ctx)
be147d08
JM
3826{
3827#if defined(CONFIG_USER_ONLY)
e06fcd75 3828 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
be147d08
JM
3829#else
3830 /* Restore CPU state */
76db3ba4 3831 if (unlikely(ctx->mem_idx <= 1)) {
e06fcd75 3832 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
be147d08
JM
3833 return;
3834 }
e5f17ac6 3835 gen_helper_hrfid(cpu_env);
e06fcd75 3836 gen_sync_exception(ctx);
be147d08
JM
3837#endif
3838}
3839#endif
3840
79aceca5 3841/* sc */
417bf010
JM
3842#if defined(CONFIG_USER_ONLY)
3843#define POWERPC_SYSCALL POWERPC_EXCP_SYSCALL_USER
3844#else
3845#define POWERPC_SYSCALL POWERPC_EXCP_SYSCALL
3846#endif
99e300ef 3847static void gen_sc(DisasContext *ctx)
79aceca5 3848{
e1833e1f
JM
3849 uint32_t lev;
3850
3851 lev = (ctx->opcode >> 5) & 0x7F;
e06fcd75 3852 gen_exception_err(ctx, POWERPC_SYSCALL, lev);
79aceca5
FB
3853}
3854
3855/*** Trap ***/
99e300ef 3856
54623277 3857/* tw */
99e300ef 3858static void gen_tw(DisasContext *ctx)
79aceca5 3859{
cab3bee2 3860 TCGv_i32 t0 = tcg_const_i32(TO(ctx->opcode));
db9a231d
AJ
3861 /* Update the nip since this might generate a trap exception */
3862 gen_update_nip(ctx, ctx->nip);
e5f17ac6
BS
3863 gen_helper_tw(cpu_env, cpu_gpr[rA(ctx->opcode)], cpu_gpr[rB(ctx->opcode)],
3864 t0);
cab3bee2 3865 tcg_temp_free_i32(t0);
79aceca5
FB
3866}
3867
3868/* twi */
99e300ef 3869static void gen_twi(DisasContext *ctx)
79aceca5 3870{
cab3bee2
AJ
3871 TCGv t0 = tcg_const_tl(SIMM(ctx->opcode));
3872 TCGv_i32 t1 = tcg_const_i32(TO(ctx->opcode));
db9a231d
AJ
3873 /* Update the nip since this might generate a trap exception */
3874 gen_update_nip(ctx, ctx->nip);
e5f17ac6 3875 gen_helper_tw(cpu_env, cpu_gpr[rA(ctx->opcode)], t0, t1);
cab3bee2
AJ
3876 tcg_temp_free(t0);
3877 tcg_temp_free_i32(t1);
79aceca5
FB
3878}
3879
d9bce9d9
JM
3880#if defined(TARGET_PPC64)
3881/* td */
99e300ef 3882static void gen_td(DisasContext *ctx)
d9bce9d9 3883{
cab3bee2 3884 TCGv_i32 t0 = tcg_const_i32(TO(ctx->opcode));
db9a231d
AJ
3885 /* Update the nip since this might generate a trap exception */
3886 gen_update_nip(ctx, ctx->nip);
e5f17ac6
BS
3887 gen_helper_td(cpu_env, cpu_gpr[rA(ctx->opcode)], cpu_gpr[rB(ctx->opcode)],
3888 t0);
cab3bee2 3889 tcg_temp_free_i32(t0);
d9bce9d9
JM
3890}
3891
3892/* tdi */
99e300ef 3893static void gen_tdi(DisasContext *ctx)
d9bce9d9 3894{
cab3bee2
AJ
3895 TCGv t0 = tcg_const_tl(SIMM(ctx->opcode));
3896 TCGv_i32 t1 = tcg_const_i32(TO(ctx->opcode));
db9a231d
AJ
3897 /* Update the nip since this might generate a trap exception */
3898 gen_update_nip(ctx, ctx->nip);
e5f17ac6 3899 gen_helper_td(cpu_env, cpu_gpr[rA(ctx->opcode)], t0, t1);
cab3bee2
AJ
3900 tcg_temp_free(t0);
3901 tcg_temp_free_i32(t1);
d9bce9d9
JM
3902}
3903#endif
3904
79aceca5 3905/*** Processor control ***/
99e300ef 3906
da91a00f
RH
3907static void gen_read_xer(TCGv dst)
3908{
3909 TCGv t0 = tcg_temp_new();
3910 TCGv t1 = tcg_temp_new();
3911 TCGv t2 = tcg_temp_new();
3912 tcg_gen_mov_tl(dst, cpu_xer);
3913 tcg_gen_shli_tl(t0, cpu_so, XER_SO);
3914 tcg_gen_shli_tl(t1, cpu_ov, XER_OV);
3915 tcg_gen_shli_tl(t2, cpu_ca, XER_CA);
3916 tcg_gen_or_tl(t0, t0, t1);
3917 tcg_gen_or_tl(dst, dst, t2);
3918 tcg_gen_or_tl(dst, dst, t0);
3919 tcg_temp_free(t0);
3920 tcg_temp_free(t1);
3921 tcg_temp_free(t2);
3922}
3923
3924static void gen_write_xer(TCGv src)
3925{
3926 tcg_gen_andi_tl(cpu_xer, src,
3927 ~((1u << XER_SO) | (1u << XER_OV) | (1u << XER_CA)));
3928 tcg_gen_shri_tl(cpu_so, src, XER_SO);
3929 tcg_gen_shri_tl(cpu_ov, src, XER_OV);
3930 tcg_gen_shri_tl(cpu_ca, src, XER_CA);
3931 tcg_gen_andi_tl(cpu_so, cpu_so, 1);
3932 tcg_gen_andi_tl(cpu_ov, cpu_ov, 1);
3933 tcg_gen_andi_tl(cpu_ca, cpu_ca, 1);
3934}
3935
54623277 3936/* mcrxr */
99e300ef 3937static void gen_mcrxr(DisasContext *ctx)
79aceca5 3938{
da91a00f
RH
3939 TCGv_i32 t0 = tcg_temp_new_i32();
3940 TCGv_i32 t1 = tcg_temp_new_i32();
3941 TCGv_i32 dst = cpu_crf[crfD(ctx->opcode)];
3942
3943 tcg_gen_trunc_tl_i32(t0, cpu_so);
3944 tcg_gen_trunc_tl_i32(t1, cpu_ov);
3945 tcg_gen_trunc_tl_i32(dst, cpu_ca);
3946 tcg_gen_shri_i32(t0, t0, 2);
3947 tcg_gen_shri_i32(t1, t1, 1);
3948 tcg_gen_or_i32(dst, dst, t0);
3949 tcg_gen_or_i32(dst, dst, t1);
3950 tcg_temp_free_i32(t0);
3951 tcg_temp_free_i32(t1);
3952
3953 tcg_gen_movi_tl(cpu_so, 0);
3954 tcg_gen_movi_tl(cpu_ov, 0);
3955 tcg_gen_movi_tl(cpu_ca, 0);
79aceca5
FB
3956}
3957
0cfe11ea 3958/* mfcr mfocrf */
99e300ef 3959static void gen_mfcr(DisasContext *ctx)
79aceca5 3960{
76a66253 3961 uint32_t crm, crn;
3b46e624 3962
76a66253
JM
3963 if (likely(ctx->opcode & 0x00100000)) {
3964 crm = CRM(ctx->opcode);
8dd640e4 3965 if (likely(crm && ((crm & (crm - 1)) == 0))) {
0cfe11ea 3966 crn = ctz32 (crm);
e1571908 3967 tcg_gen_extu_i32_tl(cpu_gpr[rD(ctx->opcode)], cpu_crf[7 - crn]);
0497d2f4
AJ
3968 tcg_gen_shli_tl(cpu_gpr[rD(ctx->opcode)],
3969 cpu_gpr[rD(ctx->opcode)], crn * 4);
76a66253 3970 }
d9bce9d9 3971 } else {
651721b2
AJ
3972 TCGv_i32 t0 = tcg_temp_new_i32();
3973 tcg_gen_mov_i32(t0, cpu_crf[0]);
3974 tcg_gen_shli_i32(t0, t0, 4);
3975 tcg_gen_or_i32(t0, t0, cpu_crf[1]);
3976 tcg_gen_shli_i32(t0, t0, 4);
3977 tcg_gen_or_i32(t0, t0, cpu_crf[2]);
3978 tcg_gen_shli_i32(t0, t0, 4);
3979 tcg_gen_or_i32(t0, t0, cpu_crf[3]);
3980 tcg_gen_shli_i32(t0, t0, 4);
3981 tcg_gen_or_i32(t0, t0, cpu_crf[4]);
3982 tcg_gen_shli_i32(t0, t0, 4);
3983 tcg_gen_or_i32(t0, t0, cpu_crf[5]);
3984 tcg_gen_shli_i32(t0, t0, 4);
3985 tcg_gen_or_i32(t0, t0, cpu_crf[6]);
3986 tcg_gen_shli_i32(t0, t0, 4);
3987 tcg_gen_or_i32(t0, t0, cpu_crf[7]);
3988 tcg_gen_extu_i32_tl(cpu_gpr[rD(ctx->opcode)], t0);
3989 tcg_temp_free_i32(t0);
d9bce9d9 3990 }
79aceca5
FB
3991}
3992
3993/* mfmsr */
99e300ef 3994static void gen_mfmsr(DisasContext *ctx)
79aceca5 3995{
9a64fbe4 3996#if defined(CONFIG_USER_ONLY)
e06fcd75 3997 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
9a64fbe4 3998#else
76db3ba4 3999 if (unlikely(!ctx->mem_idx)) {
e06fcd75 4000 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
9fddaa0c 4001 return;
9a64fbe4 4002 }
6527f6ea 4003 tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_msr);
9a64fbe4 4004#endif
79aceca5
FB
4005}
4006
7b13448f 4007static void spr_noaccess(void *opaque, int gprn, int sprn)
3fc6c082 4008{
7b13448f 4009#if 0
3fc6c082
FB
4010 sprn = ((sprn >> 5) & 0x1F) | ((sprn & 0x1F) << 5);
4011 printf("ERROR: try to access SPR %d !\n", sprn);
7b13448f 4012#endif
3fc6c082
FB
4013}
4014#define SPR_NOACCESS (&spr_noaccess)
3fc6c082 4015
79aceca5 4016/* mfspr */
636aa200 4017static inline void gen_op_mfspr(DisasContext *ctx)
79aceca5 4018{
45d827d2 4019 void (*read_cb)(void *opaque, int gprn, int sprn);
79aceca5
FB
4020 uint32_t sprn = SPR(ctx->opcode);
4021
3fc6c082 4022#if !defined(CONFIG_USER_ONLY)
76db3ba4 4023 if (ctx->mem_idx == 2)
be147d08 4024 read_cb = ctx->spr_cb[sprn].hea_read;
76db3ba4 4025 else if (ctx->mem_idx)
3fc6c082
FB
4026 read_cb = ctx->spr_cb[sprn].oea_read;
4027 else
9a64fbe4 4028#endif
3fc6c082 4029 read_cb = ctx->spr_cb[sprn].uea_read;
76a66253
JM
4030 if (likely(read_cb != NULL)) {
4031 if (likely(read_cb != SPR_NOACCESS)) {
45d827d2 4032 (*read_cb)(ctx, rD(ctx->opcode), sprn);
3fc6c082
FB
4033 } else {
4034 /* Privilege exception */
9fceefa7
JM
4035 /* This is a hack to avoid warnings when running Linux:
4036 * this OS breaks the PowerPC virtualisation model,
4037 * allowing userland application to read the PVR
4038 */
4039 if (sprn != SPR_PVR) {
c05541ee
AB
4040 qemu_log("Trying to read privileged spr %d (0x%03x) at "
4041 TARGET_FMT_lx "\n", sprn, sprn, ctx->nip - 4);
4042 printf("Trying to read privileged spr %d (0x%03x) at "
4043 TARGET_FMT_lx "\n", sprn, sprn, ctx->nip - 4);
f24e5695 4044 }
e06fcd75 4045 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
79aceca5 4046 }
3fc6c082
FB
4047 } else {
4048 /* Not defined */
c05541ee
AB
4049 qemu_log("Trying to read invalid spr %d (0x%03x) at "
4050 TARGET_FMT_lx "\n", sprn, sprn, ctx->nip - 4);
4051 printf("Trying to read invalid spr %d (0x%03x) at "
4052 TARGET_FMT_lx "\n", sprn, sprn, ctx->nip - 4);
e06fcd75 4053 gen_inval_exception(ctx, POWERPC_EXCP_INVAL_SPR);
79aceca5 4054 }
79aceca5
FB
4055}
4056
99e300ef 4057static void gen_mfspr(DisasContext *ctx)
79aceca5 4058{
3fc6c082 4059 gen_op_mfspr(ctx);
76a66253 4060}
3fc6c082
FB
4061
4062/* mftb */
99e300ef 4063static void gen_mftb(DisasContext *ctx)
3fc6c082
FB
4064{
4065 gen_op_mfspr(ctx);
79aceca5
FB
4066}
4067
0cfe11ea 4068/* mtcrf mtocrf*/
99e300ef 4069static void gen_mtcrf(DisasContext *ctx)
79aceca5 4070{
76a66253 4071 uint32_t crm, crn;
3b46e624 4072
76a66253 4073 crm = CRM(ctx->opcode);
8dd640e4 4074 if (likely((ctx->opcode & 0x00100000))) {
4075 if (crm && ((crm & (crm - 1)) == 0)) {
4076 TCGv_i32 temp = tcg_temp_new_i32();
0cfe11ea 4077 crn = ctz32 (crm);
8dd640e4 4078 tcg_gen_trunc_tl_i32(temp, cpu_gpr[rS(ctx->opcode)]);
0cfe11ea
AJ
4079 tcg_gen_shri_i32(temp, temp, crn * 4);
4080 tcg_gen_andi_i32(cpu_crf[7 - crn], temp, 0xf);
8dd640e4 4081 tcg_temp_free_i32(temp);
4082 }
76a66253 4083 } else {
651721b2
AJ
4084 TCGv_i32 temp = tcg_temp_new_i32();
4085 tcg_gen_trunc_tl_i32(temp, cpu_gpr[rS(ctx->opcode)]);
4086 for (crn = 0 ; crn < 8 ; crn++) {
4087 if (crm & (1 << crn)) {
4088 tcg_gen_shri_i32(cpu_crf[7 - crn], temp, crn * 4);
4089 tcg_gen_andi_i32(cpu_crf[7 - crn], cpu_crf[7 - crn], 0xf);
4090 }
4091 }
a7812ae4 4092 tcg_temp_free_i32(temp);
76a66253 4093 }
79aceca5
FB
4094}
4095
4096/* mtmsr */
426613db 4097#if defined(TARGET_PPC64)
99e300ef 4098static void gen_mtmsrd(DisasContext *ctx)
426613db
JM
4099{
4100#if defined(CONFIG_USER_ONLY)
e06fcd75 4101 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
426613db 4102#else
76db3ba4 4103 if (unlikely(!ctx->mem_idx)) {
e06fcd75 4104 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
426613db
JM
4105 return;
4106 }
be147d08
JM
4107 if (ctx->opcode & 0x00010000) {
4108 /* Special form that does not need any synchronisation */
6527f6ea
AJ
4109 TCGv t0 = tcg_temp_new();
4110 tcg_gen_andi_tl(t0, cpu_gpr[rS(ctx->opcode)], (1 << MSR_RI) | (1 << MSR_EE));
4111 tcg_gen_andi_tl(cpu_msr, cpu_msr, ~((1 << MSR_RI) | (1 << MSR_EE)));
4112 tcg_gen_or_tl(cpu_msr, cpu_msr, t0);
4113 tcg_temp_free(t0);
be147d08 4114 } else {
056b05f8
JM
4115 /* XXX: we need to update nip before the store
4116 * if we enter power saving mode, we will exit the loop
4117 * directly from ppc_store_msr
4118 */
be147d08 4119 gen_update_nip(ctx, ctx->nip);
e5f17ac6 4120 gen_helper_store_msr(cpu_env, cpu_gpr[rS(ctx->opcode)]);
be147d08
JM
4121 /* Must stop the translation as machine state (may have) changed */
4122 /* Note that mtmsr is not always defined as context-synchronizing */
e06fcd75 4123 gen_stop_exception(ctx);
be147d08 4124 }
426613db
JM
4125#endif
4126}
4127#endif
4128
99e300ef 4129static void gen_mtmsr(DisasContext *ctx)
79aceca5 4130{
9a64fbe4 4131#if defined(CONFIG_USER_ONLY)
e06fcd75 4132 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
9a64fbe4 4133#else
76db3ba4 4134 if (unlikely(!ctx->mem_idx)) {
e06fcd75 4135 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
9fddaa0c 4136 return;
9a64fbe4 4137 }
be147d08
JM
4138 if (ctx->opcode & 0x00010000) {
4139 /* Special form that does not need any synchronisation */
6527f6ea
AJ
4140 TCGv t0 = tcg_temp_new();
4141 tcg_gen_andi_tl(t0, cpu_gpr[rS(ctx->opcode)], (1 << MSR_RI) | (1 << MSR_EE));
4142 tcg_gen_andi_tl(cpu_msr, cpu_msr, ~((1 << MSR_RI) | (1 << MSR_EE)));
4143 tcg_gen_or_tl(cpu_msr, cpu_msr, t0);
4144 tcg_temp_free(t0);
be147d08 4145 } else {
8018dc63
AG
4146 TCGv msr = tcg_temp_new();
4147
056b05f8
JM
4148 /* XXX: we need to update nip before the store
4149 * if we enter power saving mode, we will exit the loop
4150 * directly from ppc_store_msr
4151 */
be147d08 4152 gen_update_nip(ctx, ctx->nip);
d9bce9d9 4153#if defined(TARGET_PPC64)
8018dc63
AG
4154 tcg_gen_deposit_tl(msr, cpu_msr, cpu_gpr[rS(ctx->opcode)], 0, 32);
4155#else
4156 tcg_gen_mov_tl(msr, cpu_gpr[rS(ctx->opcode)]);
d9bce9d9 4157#endif
e5f17ac6 4158 gen_helper_store_msr(cpu_env, msr);
be147d08 4159 /* Must stop the translation as machine state (may have) changed */
6527f6ea 4160 /* Note that mtmsr is not always defined as context-synchronizing */
e06fcd75 4161 gen_stop_exception(ctx);
be147d08 4162 }
9a64fbe4 4163#endif
79aceca5
FB
4164}
4165
4166/* mtspr */
99e300ef 4167static void gen_mtspr(DisasContext *ctx)
79aceca5 4168{
45d827d2 4169 void (*write_cb)(void *opaque, int sprn, int gprn);
79aceca5
FB
4170 uint32_t sprn = SPR(ctx->opcode);
4171
3fc6c082 4172#if !defined(CONFIG_USER_ONLY)
76db3ba4 4173 if (ctx->mem_idx == 2)
be147d08 4174 write_cb = ctx->spr_cb[sprn].hea_write;
76db3ba4 4175 else if (ctx->mem_idx)
3fc6c082
FB
4176 write_cb = ctx->spr_cb[sprn].oea_write;
4177 else
9a64fbe4 4178#endif
3fc6c082 4179 write_cb = ctx->spr_cb[sprn].uea_write;
76a66253
JM
4180 if (likely(write_cb != NULL)) {
4181 if (likely(write_cb != SPR_NOACCESS)) {
45d827d2 4182 (*write_cb)(ctx, sprn, rS(ctx->opcode));
3fc6c082
FB
4183 } else {
4184 /* Privilege exception */
c05541ee
AB
4185 qemu_log("Trying to write privileged spr %d (0x%03x) at "
4186 TARGET_FMT_lx "\n", sprn, sprn, ctx->nip - 4);
4187 printf("Trying to write privileged spr %d (0x%03x) at "
4188 TARGET_FMT_lx "\n", sprn, sprn, ctx->nip - 4);
e06fcd75 4189 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
76a66253 4190 }
3fc6c082
FB
4191 } else {
4192 /* Not defined */
c05541ee
AB
4193 qemu_log("Trying to write invalid spr %d (0x%03x) at "
4194 TARGET_FMT_lx "\n", sprn, sprn, ctx->nip - 4);
4195 printf("Trying to write invalid spr %d (0x%03x) at "
4196 TARGET_FMT_lx "\n", sprn, sprn, ctx->nip - 4);
e06fcd75 4197 gen_inval_exception(ctx, POWERPC_EXCP_INVAL_SPR);
79aceca5 4198 }
79aceca5
FB
4199}
4200
4201/*** Cache management ***/
99e300ef 4202
54623277 4203/* dcbf */
99e300ef 4204static void gen_dcbf(DisasContext *ctx)
79aceca5 4205{
dac454af 4206 /* XXX: specification says this is treated as a load by the MMU */
76db3ba4
AJ
4207 TCGv t0;
4208 gen_set_access_type(ctx, ACCESS_CACHE);
4209 t0 = tcg_temp_new();
4210 gen_addr_reg_index(ctx, t0);
4211 gen_qemu_ld8u(ctx, t0, t0);
fea0c503 4212 tcg_temp_free(t0);
79aceca5
FB
4213}
4214
4215/* dcbi (Supervisor only) */
99e300ef 4216static void gen_dcbi(DisasContext *ctx)
79aceca5 4217{
a541f297 4218#if defined(CONFIG_USER_ONLY)
e06fcd75 4219 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
a541f297 4220#else
b61f2753 4221 TCGv EA, val;
76db3ba4 4222 if (unlikely(!ctx->mem_idx)) {
e06fcd75 4223 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
9fddaa0c 4224 return;
9a64fbe4 4225 }
a7812ae4 4226 EA = tcg_temp_new();
76db3ba4
AJ
4227 gen_set_access_type(ctx, ACCESS_CACHE);
4228 gen_addr_reg_index(ctx, EA);
a7812ae4 4229 val = tcg_temp_new();
76a66253 4230 /* XXX: specification says this should be treated as a store by the MMU */
76db3ba4
AJ
4231 gen_qemu_ld8u(ctx, val, EA);
4232 gen_qemu_st8(ctx, val, EA);
b61f2753
AJ
4233 tcg_temp_free(val);
4234 tcg_temp_free(EA);
a541f297 4235#endif
79aceca5
FB
4236}
4237
4238/* dcdst */
99e300ef 4239static void gen_dcbst(DisasContext *ctx)
79aceca5 4240{
76a66253 4241 /* XXX: specification say this is treated as a load by the MMU */
76db3ba4
AJ
4242 TCGv t0;
4243 gen_set_access_type(ctx, ACCESS_CACHE);
4244 t0 = tcg_temp_new();
4245 gen_addr_reg_index(ctx, t0);
4246 gen_qemu_ld8u(ctx, t0, t0);
fea0c503 4247 tcg_temp_free(t0);
79aceca5
FB
4248}
4249
4250/* dcbt */
99e300ef 4251static void gen_dcbt(DisasContext *ctx)
79aceca5 4252{
0db1b20e 4253 /* interpreted as no-op */
76a66253
JM
4254 /* XXX: specification say this is treated as a load by the MMU
4255 * but does not generate any exception
4256 */
79aceca5
FB
4257}
4258
4259/* dcbtst */
99e300ef 4260static void gen_dcbtst(DisasContext *ctx)
79aceca5 4261{
0db1b20e 4262 /* interpreted as no-op */
76a66253
JM
4263 /* XXX: specification say this is treated as a load by the MMU
4264 * but does not generate any exception
4265 */
79aceca5
FB
4266}
4267
4268/* dcbz */
99e300ef 4269static void gen_dcbz(DisasContext *ctx)
79aceca5 4270{
8e33944f
AG
4271 TCGv tcgv_addr;
4272 TCGv_i32 tcgv_is_dcbzl;
4273 int is_dcbzl = ctx->opcode & 0x00200000 ? 1 : 0;
d63001d1 4274
76db3ba4 4275 gen_set_access_type(ctx, ACCESS_CACHE);
799a8c8d
AJ
4276 /* NIP cannot be restored if the memory exception comes from an helper */
4277 gen_update_nip(ctx, ctx->nip - 4);
8e33944f
AG
4278 tcgv_addr = tcg_temp_new();
4279 tcgv_is_dcbzl = tcg_const_i32(is_dcbzl);
4280
4281 gen_addr_reg_index(ctx, tcgv_addr);
4282 gen_helper_dcbz(cpu_env, tcgv_addr, tcgv_is_dcbzl);
4283
4284 tcg_temp_free(tcgv_addr);
4285 tcg_temp_free_i32(tcgv_is_dcbzl);
79aceca5
FB
4286}
4287
ae1c1a3d 4288/* dst / dstt */
99e300ef 4289static void gen_dst(DisasContext *ctx)
ae1c1a3d
AJ
4290{
4291 if (rA(ctx->opcode) == 0) {
4292 gen_inval_exception(ctx, POWERPC_EXCP_INVAL_LSWX);
4293 } else {
4294 /* interpreted as no-op */
4295 }
4296}
4297
4298/* dstst /dststt */
99e300ef 4299static void gen_dstst(DisasContext *ctx)
ae1c1a3d
AJ
4300{
4301 if (rA(ctx->opcode) == 0) {
4302 gen_inval_exception(ctx, POWERPC_EXCP_INVAL_LSWX);
4303 } else {
4304 /* interpreted as no-op */
4305 }
4306
4307}
4308
4309/* dss / dssall */
99e300ef 4310static void gen_dss(DisasContext *ctx)
ae1c1a3d
AJ
4311{
4312 /* interpreted as no-op */
4313}
4314
79aceca5 4315/* icbi */
99e300ef 4316static void gen_icbi(DisasContext *ctx)
79aceca5 4317{
76db3ba4
AJ
4318 TCGv t0;
4319 gen_set_access_type(ctx, ACCESS_CACHE);
30032c94
JM
4320 /* NIP cannot be restored if the memory exception comes from an helper */
4321 gen_update_nip(ctx, ctx->nip - 4);
76db3ba4
AJ
4322 t0 = tcg_temp_new();
4323 gen_addr_reg_index(ctx, t0);
2f5a189c 4324 gen_helper_icbi(cpu_env, t0);
37d269df 4325 tcg_temp_free(t0);
79aceca5
FB
4326}
4327
4328/* Optional: */
4329/* dcba */
99e300ef 4330static void gen_dcba(DisasContext *ctx)
79aceca5 4331{
0db1b20e
JM
4332 /* interpreted as no-op */
4333 /* XXX: specification say this is treated as a store by the MMU
4334 * but does not generate any exception
4335 */
79aceca5
FB
4336}
4337
4338/*** Segment register manipulation ***/
4339/* Supervisor only: */
99e300ef 4340
54623277 4341/* mfsr */
99e300ef 4342static void gen_mfsr(DisasContext *ctx)
79aceca5 4343{
9a64fbe4 4344#if defined(CONFIG_USER_ONLY)
e06fcd75 4345 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
9a64fbe4 4346#else
74d37793 4347 TCGv t0;
76db3ba4 4348 if (unlikely(!ctx->mem_idx)) {
e06fcd75 4349 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
9fddaa0c 4350 return;
9a64fbe4 4351 }
74d37793 4352 t0 = tcg_const_tl(SR(ctx->opcode));
c6c7cf05 4353 gen_helper_load_sr(cpu_gpr[rD(ctx->opcode)], cpu_env, t0);
74d37793 4354 tcg_temp_free(t0);
9a64fbe4 4355#endif
79aceca5
FB
4356}
4357
4358/* mfsrin */
99e300ef 4359static void gen_mfsrin(DisasContext *ctx)
79aceca5 4360{
9a64fbe4 4361#if defined(CONFIG_USER_ONLY)
e06fcd75 4362 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
9a64fbe4 4363#else
74d37793 4364 TCGv t0;
76db3ba4 4365 if (unlikely(!ctx->mem_idx)) {
e06fcd75 4366 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
9fddaa0c 4367 return;
9a64fbe4 4368 }
74d37793
AJ
4369 t0 = tcg_temp_new();
4370 tcg_gen_shri_tl(t0, cpu_gpr[rB(ctx->opcode)], 28);
4371 tcg_gen_andi_tl(t0, t0, 0xF);
c6c7cf05 4372 gen_helper_load_sr(cpu_gpr[rD(ctx->opcode)], cpu_env, t0);
74d37793 4373 tcg_temp_free(t0);
9a64fbe4 4374#endif
79aceca5
FB
4375}
4376
4377/* mtsr */
99e300ef 4378static void gen_mtsr(DisasContext *ctx)
79aceca5 4379{
9a64fbe4 4380#if defined(CONFIG_USER_ONLY)
e06fcd75 4381 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
9a64fbe4 4382#else
74d37793 4383 TCGv t0;
76db3ba4 4384 if (unlikely(!ctx->mem_idx)) {
e06fcd75 4385 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
9fddaa0c 4386 return;
9a64fbe4 4387 }
74d37793 4388 t0 = tcg_const_tl(SR(ctx->opcode));
c6c7cf05 4389 gen_helper_store_sr(cpu_env, t0, cpu_gpr[rS(ctx->opcode)]);
74d37793 4390 tcg_temp_free(t0);
9a64fbe4 4391#endif
79aceca5
FB
4392}
4393
4394/* mtsrin */
99e300ef 4395static void gen_mtsrin(DisasContext *ctx)
79aceca5 4396{
9a64fbe4 4397#if defined(CONFIG_USER_ONLY)
e06fcd75 4398 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
9a64fbe4 4399#else
74d37793 4400 TCGv t0;
76db3ba4 4401 if (unlikely(!ctx->mem_idx)) {
e06fcd75 4402 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
9fddaa0c 4403 return;
9a64fbe4 4404 }
74d37793
AJ
4405 t0 = tcg_temp_new();
4406 tcg_gen_shri_tl(t0, cpu_gpr[rB(ctx->opcode)], 28);
4407 tcg_gen_andi_tl(t0, t0, 0xF);
c6c7cf05 4408 gen_helper_store_sr(cpu_env, t0, cpu_gpr[rD(ctx->opcode)]);
74d37793 4409 tcg_temp_free(t0);
9a64fbe4 4410#endif
79aceca5
FB
4411}
4412
12de9a39
JM
4413#if defined(TARGET_PPC64)
4414/* Specific implementation for PowerPC 64 "bridge" emulation using SLB */
e8eaa2c0 4415
54623277 4416/* mfsr */
e8eaa2c0 4417static void gen_mfsr_64b(DisasContext *ctx)
12de9a39
JM
4418{
4419#if defined(CONFIG_USER_ONLY)
e06fcd75 4420 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
12de9a39 4421#else
74d37793 4422 TCGv t0;
76db3ba4 4423 if (unlikely(!ctx->mem_idx)) {
e06fcd75 4424 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
12de9a39
JM
4425 return;
4426 }
74d37793 4427 t0 = tcg_const_tl(SR(ctx->opcode));
c6c7cf05 4428 gen_helper_load_sr(cpu_gpr[rD(ctx->opcode)], cpu_env, t0);
74d37793 4429 tcg_temp_free(t0);
12de9a39
JM
4430#endif
4431}
4432
4433/* mfsrin */
e8eaa2c0 4434static void gen_mfsrin_64b(DisasContext *ctx)
12de9a39
JM
4435{
4436#if defined(CONFIG_USER_ONLY)
e06fcd75 4437 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
12de9a39 4438#else
74d37793 4439 TCGv t0;
76db3ba4 4440 if (unlikely(!ctx->mem_idx)) {
e06fcd75 4441 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
12de9a39
JM
4442 return;
4443 }
74d37793
AJ
4444 t0 = tcg_temp_new();
4445 tcg_gen_shri_tl(t0, cpu_gpr[rB(ctx->opcode)], 28);
4446 tcg_gen_andi_tl(t0, t0, 0xF);
c6c7cf05 4447 gen_helper_load_sr(cpu_gpr[rD(ctx->opcode)], cpu_env, t0);
74d37793 4448 tcg_temp_free(t0);
12de9a39
JM
4449#endif
4450}
4451
4452/* mtsr */
e8eaa2c0 4453static void gen_mtsr_64b(DisasContext *ctx)
12de9a39
JM
4454{
4455#if defined(CONFIG_USER_ONLY)
e06fcd75 4456 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
12de9a39 4457#else
74d37793 4458 TCGv t0;
76db3ba4 4459 if (unlikely(!ctx->mem_idx)) {
e06fcd75 4460 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
12de9a39
JM
4461 return;
4462 }
74d37793 4463 t0 = tcg_const_tl(SR(ctx->opcode));
c6c7cf05 4464 gen_helper_store_sr(cpu_env, t0, cpu_gpr[rS(ctx->opcode)]);
74d37793 4465 tcg_temp_free(t0);
12de9a39
JM
4466#endif
4467}
4468
4469/* mtsrin */
e8eaa2c0 4470static void gen_mtsrin_64b(DisasContext *ctx)
12de9a39
JM
4471{
4472#if defined(CONFIG_USER_ONLY)
e06fcd75 4473 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
12de9a39 4474#else
74d37793 4475 TCGv t0;
76db3ba4 4476 if (unlikely(!ctx->mem_idx)) {
e06fcd75 4477 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
12de9a39
JM
4478 return;
4479 }
74d37793
AJ
4480 t0 = tcg_temp_new();
4481 tcg_gen_shri_tl(t0, cpu_gpr[rB(ctx->opcode)], 28);
4482 tcg_gen_andi_tl(t0, t0, 0xF);
c6c7cf05 4483 gen_helper_store_sr(cpu_env, t0, cpu_gpr[rS(ctx->opcode)]);
74d37793 4484 tcg_temp_free(t0);
12de9a39
JM
4485#endif
4486}
f6b868fc
BS
4487
4488/* slbmte */
e8eaa2c0 4489static void gen_slbmte(DisasContext *ctx)
f6b868fc
BS
4490{
4491#if defined(CONFIG_USER_ONLY)
4492 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
4493#else
4494 if (unlikely(!ctx->mem_idx)) {
4495 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
4496 return;
4497 }
c6c7cf05
BS
4498 gen_helper_store_slb(cpu_env, cpu_gpr[rB(ctx->opcode)],
4499 cpu_gpr[rS(ctx->opcode)]);
f6b868fc
BS
4500#endif
4501}
4502
efdef95f
DG
4503static void gen_slbmfee(DisasContext *ctx)
4504{
4505#if defined(CONFIG_USER_ONLY)
4506 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
4507#else
4508 if (unlikely(!ctx->mem_idx)) {
4509 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
4510 return;
4511 }
c6c7cf05 4512 gen_helper_load_slb_esid(cpu_gpr[rS(ctx->opcode)], cpu_env,
efdef95f
DG
4513 cpu_gpr[rB(ctx->opcode)]);
4514#endif
4515}
4516
4517static void gen_slbmfev(DisasContext *ctx)
4518{
4519#if defined(CONFIG_USER_ONLY)
4520 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
4521#else
4522 if (unlikely(!ctx->mem_idx)) {
4523 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
4524 return;
4525 }
c6c7cf05 4526 gen_helper_load_slb_vsid(cpu_gpr[rS(ctx->opcode)], cpu_env,
efdef95f
DG
4527 cpu_gpr[rB(ctx->opcode)]);
4528#endif
4529}
12de9a39
JM
4530#endif /* defined(TARGET_PPC64) */
4531
79aceca5 4532/*** Lookaside buffer management ***/
76db3ba4 4533/* Optional & mem_idx only: */
99e300ef 4534
54623277 4535/* tlbia */
99e300ef 4536static void gen_tlbia(DisasContext *ctx)
79aceca5 4537{
9a64fbe4 4538#if defined(CONFIG_USER_ONLY)
e06fcd75 4539 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
9a64fbe4 4540#else
76db3ba4 4541 if (unlikely(!ctx->mem_idx)) {
e06fcd75 4542 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
9fddaa0c 4543 return;
9a64fbe4 4544 }
c6c7cf05 4545 gen_helper_tlbia(cpu_env);
9a64fbe4 4546#endif
79aceca5
FB
4547}
4548
bf14b1ce 4549/* tlbiel */
99e300ef 4550static void gen_tlbiel(DisasContext *ctx)
bf14b1ce
BS
4551{
4552#if defined(CONFIG_USER_ONLY)
4553 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
4554#else
4555 if (unlikely(!ctx->mem_idx)) {
4556 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
4557 return;
4558 }
c6c7cf05 4559 gen_helper_tlbie(cpu_env, cpu_gpr[rB(ctx->opcode)]);
bf14b1ce
BS
4560#endif
4561}
4562
79aceca5 4563/* tlbie */
99e300ef 4564static void gen_tlbie(DisasContext *ctx)
79aceca5 4565{
9a64fbe4 4566#if defined(CONFIG_USER_ONLY)
e06fcd75 4567 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
9a64fbe4 4568#else
76db3ba4 4569 if (unlikely(!ctx->mem_idx)) {
e06fcd75 4570 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
9fddaa0c 4571 return;
9a64fbe4 4572 }
9ca3f7f3 4573 if (NARROW_MODE(ctx)) {
74d37793
AJ
4574 TCGv t0 = tcg_temp_new();
4575 tcg_gen_ext32u_tl(t0, cpu_gpr[rB(ctx->opcode)]);
c6c7cf05 4576 gen_helper_tlbie(cpu_env, t0);
74d37793 4577 tcg_temp_free(t0);
9ca3f7f3 4578 } else {
c6c7cf05 4579 gen_helper_tlbie(cpu_env, cpu_gpr[rB(ctx->opcode)]);
9ca3f7f3 4580 }
9a64fbe4 4581#endif
79aceca5
FB
4582}
4583
4584/* tlbsync */
99e300ef 4585static void gen_tlbsync(DisasContext *ctx)
79aceca5 4586{
9a64fbe4 4587#if defined(CONFIG_USER_ONLY)
e06fcd75 4588 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
9a64fbe4 4589#else
76db3ba4 4590 if (unlikely(!ctx->mem_idx)) {
e06fcd75 4591 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
9fddaa0c 4592 return;
9a64fbe4
FB
4593 }
4594 /* This has no effect: it should ensure that all previous
4595 * tlbie have completed
4596 */
e06fcd75 4597 gen_stop_exception(ctx);
9a64fbe4 4598#endif
79aceca5
FB
4599}
4600
426613db
JM
4601#if defined(TARGET_PPC64)
4602/* slbia */
99e300ef 4603static void gen_slbia(DisasContext *ctx)
426613db
JM
4604{
4605#if defined(CONFIG_USER_ONLY)
e06fcd75 4606 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
426613db 4607#else
76db3ba4 4608 if (unlikely(!ctx->mem_idx)) {
e06fcd75 4609 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
426613db
JM
4610 return;
4611 }
c6c7cf05 4612 gen_helper_slbia(cpu_env);
426613db
JM
4613#endif
4614}
4615
4616/* slbie */
99e300ef 4617static void gen_slbie(DisasContext *ctx)
426613db
JM
4618{
4619#if defined(CONFIG_USER_ONLY)
e06fcd75 4620 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
426613db 4621#else
76db3ba4 4622 if (unlikely(!ctx->mem_idx)) {
e06fcd75 4623 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
426613db
JM
4624 return;
4625 }
c6c7cf05 4626 gen_helper_slbie(cpu_env, cpu_gpr[rB(ctx->opcode)]);
426613db
JM
4627#endif
4628}
4629#endif
4630
79aceca5
FB
4631/*** External control ***/
4632/* Optional: */
99e300ef 4633
54623277 4634/* eciwx */
99e300ef 4635static void gen_eciwx(DisasContext *ctx)
79aceca5 4636{
76db3ba4 4637 TCGv t0;
fa407c03 4638 /* Should check EAR[E] ! */
76db3ba4
AJ
4639 gen_set_access_type(ctx, ACCESS_EXT);
4640 t0 = tcg_temp_new();
4641 gen_addr_reg_index(ctx, t0);
fa407c03 4642 gen_check_align(ctx, t0, 0x03);
76db3ba4 4643 gen_qemu_ld32u(ctx, cpu_gpr[rD(ctx->opcode)], t0);
fa407c03 4644 tcg_temp_free(t0);
76a66253
JM
4645}
4646
4647/* ecowx */
99e300ef 4648static void gen_ecowx(DisasContext *ctx)
76a66253 4649{
76db3ba4 4650 TCGv t0;
fa407c03 4651 /* Should check EAR[E] ! */
76db3ba4
AJ
4652 gen_set_access_type(ctx, ACCESS_EXT);
4653 t0 = tcg_temp_new();
4654 gen_addr_reg_index(ctx, t0);
fa407c03 4655 gen_check_align(ctx, t0, 0x03);
76db3ba4 4656 gen_qemu_st32(ctx, cpu_gpr[rD(ctx->opcode)], t0);
fa407c03 4657 tcg_temp_free(t0);
76a66253
JM
4658}
4659
4660/* PowerPC 601 specific instructions */
99e300ef 4661
54623277 4662/* abs - abs. */
99e300ef 4663static void gen_abs(DisasContext *ctx)
76a66253 4664{
22e0e173
AJ
4665 int l1 = gen_new_label();
4666 int l2 = gen_new_label();
4667 tcg_gen_brcondi_tl(TCG_COND_GE, cpu_gpr[rA(ctx->opcode)], 0, l1);
4668 tcg_gen_neg_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)]);
4669 tcg_gen_br(l2);
4670 gen_set_label(l1);
4671 tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)]);
4672 gen_set_label(l2);
76a66253 4673 if (unlikely(Rc(ctx->opcode) != 0))
22e0e173 4674 gen_set_Rc0(ctx, cpu_gpr[rD(ctx->opcode)]);
76a66253
JM
4675}
4676
4677/* abso - abso. */
99e300ef 4678static void gen_abso(DisasContext *ctx)
76a66253 4679{
22e0e173
AJ
4680 int l1 = gen_new_label();
4681 int l2 = gen_new_label();
4682 int l3 = gen_new_label();
4683 /* Start with XER OV disabled, the most likely case */
da91a00f 4684 tcg_gen_movi_tl(cpu_ov, 0);
22e0e173
AJ
4685 tcg_gen_brcondi_tl(TCG_COND_GE, cpu_gpr[rA(ctx->opcode)], 0, l2);
4686 tcg_gen_brcondi_tl(TCG_COND_NE, cpu_gpr[rA(ctx->opcode)], 0x80000000, l1);
da91a00f
RH
4687 tcg_gen_movi_tl(cpu_ov, 1);
4688 tcg_gen_movi_tl(cpu_so, 1);
22e0e173
AJ
4689 tcg_gen_br(l2);
4690 gen_set_label(l1);
4691 tcg_gen_neg_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)]);
4692 tcg_gen_br(l3);
4693 gen_set_label(l2);
4694 tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)]);
4695 gen_set_label(l3);
76a66253 4696 if (unlikely(Rc(ctx->opcode) != 0))
22e0e173 4697 gen_set_Rc0(ctx, cpu_gpr[rD(ctx->opcode)]);
76a66253
JM
4698}
4699
4700/* clcs */
99e300ef 4701static void gen_clcs(DisasContext *ctx)
76a66253 4702{
22e0e173 4703 TCGv_i32 t0 = tcg_const_i32(rA(ctx->opcode));
d523dd00 4704 gen_helper_clcs(cpu_gpr[rD(ctx->opcode)], cpu_env, t0);
22e0e173 4705 tcg_temp_free_i32(t0);
c7697e1f 4706 /* Rc=1 sets CR0 to an undefined state */
76a66253
JM
4707}
4708
4709/* div - div. */
99e300ef 4710static void gen_div(DisasContext *ctx)
76a66253 4711{
d15f74fb
BS
4712 gen_helper_div(cpu_gpr[rD(ctx->opcode)], cpu_env, cpu_gpr[rA(ctx->opcode)],
4713 cpu_gpr[rB(ctx->opcode)]);
76a66253 4714 if (unlikely(Rc(ctx->opcode) != 0))
22e0e173 4715 gen_set_Rc0(ctx, cpu_gpr[rD(ctx->opcode)]);
76a66253
JM
4716}
4717
4718/* divo - divo. */
99e300ef 4719static void gen_divo(DisasContext *ctx)
76a66253 4720{
d15f74fb
BS
4721 gen_helper_divo(cpu_gpr[rD(ctx->opcode)], cpu_env, cpu_gpr[rA(ctx->opcode)],
4722 cpu_gpr[rB(ctx->opcode)]);
76a66253 4723 if (unlikely(Rc(ctx->opcode) != 0))
22e0e173 4724 gen_set_Rc0(ctx, cpu_gpr[rD(ctx->opcode)]);
76a66253
JM
4725}
4726
4727/* divs - divs. */
99e300ef 4728static void gen_divs(DisasContext *ctx)
76a66253 4729{
d15f74fb
BS
4730 gen_helper_divs(cpu_gpr[rD(ctx->opcode)], cpu_env, cpu_gpr[rA(ctx->opcode)],
4731 cpu_gpr[rB(ctx->opcode)]);
76a66253 4732 if (unlikely(Rc(ctx->opcode) != 0))
22e0e173 4733 gen_set_Rc0(ctx, cpu_gpr[rD(ctx->opcode)]);
76a66253
JM
4734}
4735
4736/* divso - divso. */
99e300ef 4737static void gen_divso(DisasContext *ctx)
76a66253 4738{
d15f74fb
BS
4739 gen_helper_divso(cpu_gpr[rD(ctx->opcode)], cpu_env,
4740 cpu_gpr[rA(ctx->opcode)], cpu_gpr[rB(ctx->opcode)]);
76a66253 4741 if (unlikely(Rc(ctx->opcode) != 0))
22e0e173 4742 gen_set_Rc0(ctx, cpu_gpr[rD(ctx->opcode)]);
76a66253
JM
4743}
4744
4745/* doz - doz. */
99e300ef 4746static void gen_doz(DisasContext *ctx)
76a66253 4747{
22e0e173
AJ
4748 int l1 = gen_new_label();
4749 int l2 = gen_new_label();
4750 tcg_gen_brcond_tl(TCG_COND_GE, cpu_gpr[rB(ctx->opcode)], cpu_gpr[rA(ctx->opcode)], l1);
4751 tcg_gen_sub_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rB(ctx->opcode)], cpu_gpr[rA(ctx->opcode)]);
4752 tcg_gen_br(l2);
4753 gen_set_label(l1);
4754 tcg_gen_movi_tl(cpu_gpr[rD(ctx->opcode)], 0);
4755 gen_set_label(l2);
76a66253 4756 if (unlikely(Rc(ctx->opcode) != 0))
22e0e173 4757 gen_set_Rc0(ctx, cpu_gpr[rD(ctx->opcode)]);
76a66253
JM
4758}
4759
4760/* dozo - dozo. */
99e300ef 4761static void gen_dozo(DisasContext *ctx)
76a66253 4762{
22e0e173
AJ
4763 int l1 = gen_new_label();
4764 int l2 = gen_new_label();
4765 TCGv t0 = tcg_temp_new();
4766 TCGv t1 = tcg_temp_new();
4767 TCGv t2 = tcg_temp_new();
4768 /* Start with XER OV disabled, the most likely case */
da91a00f 4769 tcg_gen_movi_tl(cpu_ov, 0);
22e0e173
AJ
4770 tcg_gen_brcond_tl(TCG_COND_GE, cpu_gpr[rB(ctx->opcode)], cpu_gpr[rA(ctx->opcode)], l1);
4771 tcg_gen_sub_tl(t0, cpu_gpr[rB(ctx->opcode)], cpu_gpr[rA(ctx->opcode)]);
4772 tcg_gen_xor_tl(t1, cpu_gpr[rB(ctx->opcode)], cpu_gpr[rA(ctx->opcode)]);
4773 tcg_gen_xor_tl(t2, cpu_gpr[rA(ctx->opcode)], t0);
4774 tcg_gen_andc_tl(t1, t1, t2);
4775 tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], t0);
4776 tcg_gen_brcondi_tl(TCG_COND_GE, t1, 0, l2);
da91a00f
RH
4777 tcg_gen_movi_tl(cpu_ov, 1);
4778 tcg_gen_movi_tl(cpu_so, 1);
22e0e173
AJ
4779 tcg_gen_br(l2);
4780 gen_set_label(l1);
4781 tcg_gen_movi_tl(cpu_gpr[rD(ctx->opcode)], 0);
4782 gen_set_label(l2);
4783 tcg_temp_free(t0);
4784 tcg_temp_free(t1);
4785 tcg_temp_free(t2);
76a66253 4786 if (unlikely(Rc(ctx->opcode) != 0))
22e0e173 4787 gen_set_Rc0(ctx, cpu_gpr[rD(ctx->opcode)]);
76a66253
JM
4788}
4789
4790/* dozi */
99e300ef 4791static void gen_dozi(DisasContext *ctx)
76a66253 4792{
22e0e173
AJ
4793 target_long simm = SIMM(ctx->opcode);
4794 int l1 = gen_new_label();
4795 int l2 = gen_new_label();
4796 tcg_gen_brcondi_tl(TCG_COND_LT, cpu_gpr[rA(ctx->opcode)], simm, l1);
4797 tcg_gen_subfi_tl(cpu_gpr[rD(ctx->opcode)], simm, cpu_gpr[rA(ctx->opcode)]);
4798 tcg_gen_br(l2);
4799 gen_set_label(l1);
4800 tcg_gen_movi_tl(cpu_gpr[rD(ctx->opcode)], 0);
4801 gen_set_label(l2);
4802 if (unlikely(Rc(ctx->opcode) != 0))
4803 gen_set_Rc0(ctx, cpu_gpr[rD(ctx->opcode)]);
76a66253
JM
4804}
4805
76a66253 4806/* lscbx - lscbx. */
99e300ef 4807static void gen_lscbx(DisasContext *ctx)
76a66253 4808{
bdb4b689
AJ
4809 TCGv t0 = tcg_temp_new();
4810 TCGv_i32 t1 = tcg_const_i32(rD(ctx->opcode));
4811 TCGv_i32 t2 = tcg_const_i32(rA(ctx->opcode));
4812 TCGv_i32 t3 = tcg_const_i32(rB(ctx->opcode));
76a66253 4813
76db3ba4 4814 gen_addr_reg_index(ctx, t0);
76a66253 4815 /* NIP cannot be restored if the memory exception comes from an helper */
d9bce9d9 4816 gen_update_nip(ctx, ctx->nip - 4);
2f5a189c 4817 gen_helper_lscbx(t0, cpu_env, t0, t1, t2, t3);
bdb4b689
AJ
4818 tcg_temp_free_i32(t1);
4819 tcg_temp_free_i32(t2);
4820 tcg_temp_free_i32(t3);
3d7b417e 4821 tcg_gen_andi_tl(cpu_xer, cpu_xer, ~0x7F);
bdb4b689 4822 tcg_gen_or_tl(cpu_xer, cpu_xer, t0);
76a66253 4823 if (unlikely(Rc(ctx->opcode) != 0))
bdb4b689
AJ
4824 gen_set_Rc0(ctx, t0);
4825 tcg_temp_free(t0);
76a66253
JM
4826}
4827
4828/* maskg - maskg. */
99e300ef 4829static void gen_maskg(DisasContext *ctx)
76a66253 4830{
22e0e173
AJ
4831 int l1 = gen_new_label();
4832 TCGv t0 = tcg_temp_new();
4833 TCGv t1 = tcg_temp_new();
4834 TCGv t2 = tcg_temp_new();
4835 TCGv t3 = tcg_temp_new();
4836 tcg_gen_movi_tl(t3, 0xFFFFFFFF);
4837 tcg_gen_andi_tl(t0, cpu_gpr[rB(ctx->opcode)], 0x1F);
4838 tcg_gen_andi_tl(t1, cpu_gpr[rS(ctx->opcode)], 0x1F);
4839 tcg_gen_addi_tl(t2, t0, 1);
4840 tcg_gen_shr_tl(t2, t3, t2);
4841 tcg_gen_shr_tl(t3, t3, t1);
4842 tcg_gen_xor_tl(cpu_gpr[rA(ctx->opcode)], t2, t3);
4843 tcg_gen_brcond_tl(TCG_COND_GE, t0, t1, l1);
4844 tcg_gen_neg_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rA(ctx->opcode)]);
4845 gen_set_label(l1);
4846 tcg_temp_free(t0);
4847 tcg_temp_free(t1);
4848 tcg_temp_free(t2);
4849 tcg_temp_free(t3);
76a66253 4850 if (unlikely(Rc(ctx->opcode) != 0))
22e0e173 4851 gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
76a66253
JM
4852}
4853
4854/* maskir - maskir. */
99e300ef 4855static void gen_maskir(DisasContext *ctx)
76a66253 4856{
22e0e173
AJ
4857 TCGv t0 = tcg_temp_new();
4858 TCGv t1 = tcg_temp_new();
4859 tcg_gen_and_tl(t0, cpu_gpr[rS(ctx->opcode)], cpu_gpr[rB(ctx->opcode)]);
4860 tcg_gen_andc_tl(t1, cpu_gpr[rA(ctx->opcode)], cpu_gpr[rB(ctx->opcode)]);
4861 tcg_gen_or_tl(cpu_gpr[rA(ctx->opcode)], t0, t1);
4862 tcg_temp_free(t0);
4863 tcg_temp_free(t1);
76a66253 4864 if (unlikely(Rc(ctx->opcode) != 0))
22e0e173 4865 gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
76a66253
JM
4866}
4867
4868/* mul - mul. */
99e300ef 4869static void gen_mul(DisasContext *ctx)
76a66253 4870{
22e0e173
AJ
4871 TCGv_i64 t0 = tcg_temp_new_i64();
4872 TCGv_i64 t1 = tcg_temp_new_i64();
4873 TCGv t2 = tcg_temp_new();
4874 tcg_gen_extu_tl_i64(t0, cpu_gpr[rA(ctx->opcode)]);
4875 tcg_gen_extu_tl_i64(t1, cpu_gpr[rB(ctx->opcode)]);
4876 tcg_gen_mul_i64(t0, t0, t1);
4877 tcg_gen_trunc_i64_tl(t2, t0);
4878 gen_store_spr(SPR_MQ, t2);
4879 tcg_gen_shri_i64(t1, t0, 32);
4880 tcg_gen_trunc_i64_tl(cpu_gpr[rD(ctx->opcode)], t1);
4881 tcg_temp_free_i64(t0);
4882 tcg_temp_free_i64(t1);
4883 tcg_temp_free(t2);
76a66253 4884 if (unlikely(Rc(ctx->opcode) != 0))
22e0e173 4885 gen_set_Rc0(ctx, cpu_gpr[rD(ctx->opcode)]);
76a66253
JM
4886}
4887
4888/* mulo - mulo. */
99e300ef 4889static void gen_mulo(DisasContext *ctx)
76a66253 4890{
22e0e173
AJ
4891 int l1 = gen_new_label();
4892 TCGv_i64 t0 = tcg_temp_new_i64();
4893 TCGv_i64 t1 = tcg_temp_new_i64();
4894 TCGv t2 = tcg_temp_new();
4895 /* Start with XER OV disabled, the most likely case */
da91a00f 4896 tcg_gen_movi_tl(cpu_ov, 0);
22e0e173
AJ
4897 tcg_gen_extu_tl_i64(t0, cpu_gpr[rA(ctx->opcode)]);
4898 tcg_gen_extu_tl_i64(t1, cpu_gpr[rB(ctx->opcode)]);
4899 tcg_gen_mul_i64(t0, t0, t1);
4900 tcg_gen_trunc_i64_tl(t2, t0);
4901 gen_store_spr(SPR_MQ, t2);
4902 tcg_gen_shri_i64(t1, t0, 32);
4903 tcg_gen_trunc_i64_tl(cpu_gpr[rD(ctx->opcode)], t1);
4904 tcg_gen_ext32s_i64(t1, t0);
4905 tcg_gen_brcond_i64(TCG_COND_EQ, t0, t1, l1);
da91a00f
RH
4906 tcg_gen_movi_tl(cpu_ov, 1);
4907 tcg_gen_movi_tl(cpu_so, 1);
22e0e173
AJ
4908 gen_set_label(l1);
4909 tcg_temp_free_i64(t0);
4910 tcg_temp_free_i64(t1);
4911 tcg_temp_free(t2);
76a66253 4912 if (unlikely(Rc(ctx->opcode) != 0))
22e0e173 4913 gen_set_Rc0(ctx, cpu_gpr[rD(ctx->opcode)]);
76a66253
JM
4914}
4915
4916/* nabs - nabs. */
99e300ef 4917static void gen_nabs(DisasContext *ctx)
76a66253 4918{
22e0e173
AJ
4919 int l1 = gen_new_label();
4920 int l2 = gen_new_label();
4921 tcg_gen_brcondi_tl(TCG_COND_GT, cpu_gpr[rA(ctx->opcode)], 0, l1);
4922 tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)]);
4923 tcg_gen_br(l2);
4924 gen_set_label(l1);
4925 tcg_gen_neg_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)]);
4926 gen_set_label(l2);
76a66253 4927 if (unlikely(Rc(ctx->opcode) != 0))
22e0e173 4928 gen_set_Rc0(ctx, cpu_gpr[rD(ctx->opcode)]);
76a66253
JM
4929}
4930
4931/* nabso - nabso. */
99e300ef 4932static void gen_nabso(DisasContext *ctx)
76a66253 4933{
22e0e173
AJ
4934 int l1 = gen_new_label();
4935 int l2 = gen_new_label();
4936 tcg_gen_brcondi_tl(TCG_COND_GT, cpu_gpr[rA(ctx->opcode)], 0, l1);
4937 tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)]);
4938 tcg_gen_br(l2);
4939 gen_set_label(l1);
4940 tcg_gen_neg_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)]);
4941 gen_set_label(l2);
4942 /* nabs never overflows */
da91a00f 4943 tcg_gen_movi_tl(cpu_ov, 0);
76a66253 4944 if (unlikely(Rc(ctx->opcode) != 0))
22e0e173 4945 gen_set_Rc0(ctx, cpu_gpr[rD(ctx->opcode)]);
76a66253
JM
4946}
4947
4948/* rlmi - rlmi. */
99e300ef 4949static void gen_rlmi(DisasContext *ctx)
76a66253 4950{
7487953d
AJ
4951 uint32_t mb = MB(ctx->opcode);
4952 uint32_t me = ME(ctx->opcode);
4953 TCGv t0 = tcg_temp_new();
4954 tcg_gen_andi_tl(t0, cpu_gpr[rB(ctx->opcode)], 0x1F);
4955 tcg_gen_rotl_tl(t0, cpu_gpr[rS(ctx->opcode)], t0);
4956 tcg_gen_andi_tl(t0, t0, MASK(mb, me));
4957 tcg_gen_andi_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rA(ctx->opcode)], ~MASK(mb, me));
4958 tcg_gen_or_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rA(ctx->opcode)], t0);
4959 tcg_temp_free(t0);
76a66253 4960 if (unlikely(Rc(ctx->opcode) != 0))
7487953d 4961 gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
76a66253
JM
4962}
4963
4964/* rrib - rrib. */
99e300ef 4965static void gen_rrib(DisasContext *ctx)
76a66253 4966{
7487953d
AJ
4967 TCGv t0 = tcg_temp_new();
4968 TCGv t1 = tcg_temp_new();
4969 tcg_gen_andi_tl(t0, cpu_gpr[rB(ctx->opcode)], 0x1F);
4970 tcg_gen_movi_tl(t1, 0x80000000);
4971 tcg_gen_shr_tl(t1, t1, t0);
4972 tcg_gen_shr_tl(t0, cpu_gpr[rS(ctx->opcode)], t0);
4973 tcg_gen_and_tl(t0, t0, t1);
4974 tcg_gen_andc_tl(t1, cpu_gpr[rA(ctx->opcode)], t1);
4975 tcg_gen_or_tl(cpu_gpr[rA(ctx->opcode)], t0, t1);
4976 tcg_temp_free(t0);
4977 tcg_temp_free(t1);
76a66253 4978 if (unlikely(Rc(ctx->opcode) != 0))
7487953d 4979 gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
76a66253
JM
4980}
4981
4982/* sle - sle. */
99e300ef 4983static void gen_sle(DisasContext *ctx)
76a66253 4984{
7487953d
AJ
4985 TCGv t0 = tcg_temp_new();
4986 TCGv t1 = tcg_temp_new();
4987 tcg_gen_andi_tl(t1, cpu_gpr[rB(ctx->opcode)], 0x1F);
4988 tcg_gen_shl_tl(t0, cpu_gpr[rS(ctx->opcode)], t1);
4989 tcg_gen_subfi_tl(t1, 32, t1);
4990 tcg_gen_shr_tl(t1, cpu_gpr[rS(ctx->opcode)], t1);
4991 tcg_gen_or_tl(t1, t0, t1);
4992 tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], t0);
4993 gen_store_spr(SPR_MQ, t1);
4994 tcg_temp_free(t0);
4995 tcg_temp_free(t1);
76a66253 4996 if (unlikely(Rc(ctx->opcode) != 0))
7487953d 4997 gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
76a66253
JM
4998}
4999
5000/* sleq - sleq. */
99e300ef 5001static void gen_sleq(DisasContext *ctx)
76a66253 5002{
7487953d
AJ
5003 TCGv t0 = tcg_temp_new();
5004 TCGv t1 = tcg_temp_new();
5005 TCGv t2 = tcg_temp_new();
5006 tcg_gen_andi_tl(t0, cpu_gpr[rB(ctx->opcode)], 0x1F);
5007 tcg_gen_movi_tl(t2, 0xFFFFFFFF);
5008 tcg_gen_shl_tl(t2, t2, t0);
5009 tcg_gen_rotl_tl(t0, cpu_gpr[rS(ctx->opcode)], t0);
5010 gen_load_spr(t1, SPR_MQ);
5011 gen_store_spr(SPR_MQ, t0);
5012 tcg_gen_and_tl(t0, t0, t2);
5013 tcg_gen_andc_tl(t1, t1, t2);
5014 tcg_gen_or_tl(cpu_gpr[rA(ctx->opcode)], t0, t1);
5015 tcg_temp_free(t0);
5016 tcg_temp_free(t1);
5017 tcg_temp_free(t2);
76a66253 5018 if (unlikely(Rc(ctx->opcode) != 0))
7487953d 5019 gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
76a66253
JM
5020}
5021
5022/* sliq - sliq. */
99e300ef 5023static void gen_sliq(DisasContext *ctx)
76a66253 5024{
7487953d
AJ
5025 int sh = SH(ctx->opcode);
5026 TCGv t0 = tcg_temp_new();
5027 TCGv t1 = tcg_temp_new();
5028 tcg_gen_shli_tl(t0, cpu_gpr[rS(ctx->opcode)], sh);
5029 tcg_gen_shri_tl(t1, cpu_gpr[rS(ctx->opcode)], 32 - sh);
5030 tcg_gen_or_tl(t1, t0, t1);
5031 tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], t0);
5032 gen_store_spr(SPR_MQ, t1);
5033 tcg_temp_free(t0);
5034 tcg_temp_free(t1);
76a66253 5035 if (unlikely(Rc(ctx->opcode) != 0))
7487953d 5036 gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
76a66253
JM
5037}
5038
5039/* slliq - slliq. */
99e300ef 5040static void gen_slliq(DisasContext *ctx)
76a66253 5041{
7487953d
AJ
5042 int sh = SH(ctx->opcode);
5043 TCGv t0 = tcg_temp_new();
5044 TCGv t1 = tcg_temp_new();
5045 tcg_gen_rotli_tl(t0, cpu_gpr[rS(ctx->opcode)], sh);
5046 gen_load_spr(t1, SPR_MQ);
5047 gen_store_spr(SPR_MQ, t0);
5048 tcg_gen_andi_tl(t0, t0, (0xFFFFFFFFU << sh));
5049 tcg_gen_andi_tl(t1, t1, ~(0xFFFFFFFFU << sh));
5050 tcg_gen_or_tl(cpu_gpr[rA(ctx->opcode)], t0, t1);
5051 tcg_temp_free(t0);
5052 tcg_temp_free(t1);
76a66253 5053 if (unlikely(Rc(ctx->opcode) != 0))
7487953d 5054 gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
76a66253
JM
5055}
5056
5057/* sllq - sllq. */
99e300ef 5058static void gen_sllq(DisasContext *ctx)
76a66253 5059{
7487953d
AJ
5060 int l1 = gen_new_label();
5061 int l2 = gen_new_label();
5062 TCGv t0 = tcg_temp_local_new();
5063 TCGv t1 = tcg_temp_local_new();
5064 TCGv t2 = tcg_temp_local_new();
5065 tcg_gen_andi_tl(t2, cpu_gpr[rB(ctx->opcode)], 0x1F);
5066 tcg_gen_movi_tl(t1, 0xFFFFFFFF);
5067 tcg_gen_shl_tl(t1, t1, t2);
5068 tcg_gen_andi_tl(t0, cpu_gpr[rB(ctx->opcode)], 0x20);
5069 tcg_gen_brcondi_tl(TCG_COND_EQ, t0, 0, l1);
5070 gen_load_spr(t0, SPR_MQ);
5071 tcg_gen_and_tl(cpu_gpr[rA(ctx->opcode)], t0, t1);
5072 tcg_gen_br(l2);
5073 gen_set_label(l1);
5074 tcg_gen_shl_tl(t0, cpu_gpr[rS(ctx->opcode)], t2);
5075 gen_load_spr(t2, SPR_MQ);
5076 tcg_gen_andc_tl(t1, t2, t1);
5077 tcg_gen_or_tl(cpu_gpr[rA(ctx->opcode)], t0, t1);
5078 gen_set_label(l2);
5079 tcg_temp_free(t0);
5080 tcg_temp_free(t1);
5081 tcg_temp_free(t2);
76a66253 5082 if (unlikely(Rc(ctx->opcode) != 0))
7487953d 5083 gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
76a66253
JM
5084}
5085
5086/* slq - slq. */
99e300ef 5087static void gen_slq(DisasContext *ctx)
76a66253 5088{
7487953d
AJ
5089 int l1 = gen_new_label();
5090 TCGv t0 = tcg_temp_new();
5091 TCGv t1 = tcg_temp_new();
5092 tcg_gen_andi_tl(t1, cpu_gpr[rB(ctx->opcode)], 0x1F);
5093 tcg_gen_shl_tl(t0, cpu_gpr[rS(ctx->opcode)], t1);
5094 tcg_gen_subfi_tl(t1, 32, t1);
5095 tcg_gen_shr_tl(t1, cpu_gpr[rS(ctx->opcode)], t1);
5096 tcg_gen_or_tl(t1, t0, t1);
5097 gen_store_spr(SPR_MQ, t1);
5098 tcg_gen_andi_tl(t1, cpu_gpr[rB(ctx->opcode)], 0x20);
5099 tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], t0);
5100 tcg_gen_brcondi_tl(TCG_COND_EQ, t1, 0, l1);
5101 tcg_gen_movi_tl(cpu_gpr[rA(ctx->opcode)], 0);
5102 gen_set_label(l1);
5103 tcg_temp_free(t0);
5104 tcg_temp_free(t1);
76a66253 5105 if (unlikely(Rc(ctx->opcode) != 0))
7487953d 5106 gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
76a66253
JM
5107}
5108
d9bce9d9 5109/* sraiq - sraiq. */
99e300ef 5110static void gen_sraiq(DisasContext *ctx)
76a66253 5111{
7487953d
AJ
5112 int sh = SH(ctx->opcode);
5113 int l1 = gen_new_label();
5114 TCGv t0 = tcg_temp_new();
5115 TCGv t1 = tcg_temp_new();
5116 tcg_gen_shri_tl(t0, cpu_gpr[rS(ctx->opcode)], sh);
5117 tcg_gen_shli_tl(t1, cpu_gpr[rS(ctx->opcode)], 32 - sh);
5118 tcg_gen_or_tl(t0, t0, t1);
5119 gen_store_spr(SPR_MQ, t0);
da91a00f 5120 tcg_gen_movi_tl(cpu_ca, 0);
7487953d
AJ
5121 tcg_gen_brcondi_tl(TCG_COND_EQ, t1, 0, l1);
5122 tcg_gen_brcondi_tl(TCG_COND_GE, cpu_gpr[rS(ctx->opcode)], 0, l1);
da91a00f 5123 tcg_gen_movi_tl(cpu_ca, 1);
7487953d
AJ
5124 gen_set_label(l1);
5125 tcg_gen_sari_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)], sh);
5126 tcg_temp_free(t0);
5127 tcg_temp_free(t1);
76a66253 5128 if (unlikely(Rc(ctx->opcode) != 0))
7487953d 5129 gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
76a66253
JM
5130}
5131
5132/* sraq - sraq. */
99e300ef 5133static void gen_sraq(DisasContext *ctx)
76a66253 5134{
7487953d
AJ
5135 int l1 = gen_new_label();
5136 int l2 = gen_new_label();
5137 TCGv t0 = tcg_temp_new();
5138 TCGv t1 = tcg_temp_local_new();
5139 TCGv t2 = tcg_temp_local_new();
5140 tcg_gen_andi_tl(t2, cpu_gpr[rB(ctx->opcode)], 0x1F);
5141 tcg_gen_shr_tl(t0, cpu_gpr[rS(ctx->opcode)], t2);
5142 tcg_gen_sar_tl(t1, cpu_gpr[rS(ctx->opcode)], t2);
5143 tcg_gen_subfi_tl(t2, 32, t2);
5144 tcg_gen_shl_tl(t2, cpu_gpr[rS(ctx->opcode)], t2);
5145 tcg_gen_or_tl(t0, t0, t2);
5146 gen_store_spr(SPR_MQ, t0);
5147 tcg_gen_andi_tl(t0, cpu_gpr[rB(ctx->opcode)], 0x20);
5148 tcg_gen_brcondi_tl(TCG_COND_EQ, t2, 0, l1);
5149 tcg_gen_mov_tl(t2, cpu_gpr[rS(ctx->opcode)]);
5150 tcg_gen_sari_tl(t1, cpu_gpr[rS(ctx->opcode)], 31);
5151 gen_set_label(l1);
5152 tcg_temp_free(t0);
5153 tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], t1);
da91a00f 5154 tcg_gen_movi_tl(cpu_ca, 0);
7487953d
AJ
5155 tcg_gen_brcondi_tl(TCG_COND_GE, t1, 0, l2);
5156 tcg_gen_brcondi_tl(TCG_COND_EQ, t2, 0, l2);
da91a00f 5157 tcg_gen_movi_tl(cpu_ca, 1);
7487953d
AJ
5158 gen_set_label(l2);
5159 tcg_temp_free(t1);
5160 tcg_temp_free(t2);
76a66253 5161 if (unlikely(Rc(ctx->opcode) != 0))
7487953d 5162 gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
76a66253
JM
5163}
5164
5165/* sre - sre. */
99e300ef 5166static void gen_sre(DisasContext *ctx)
76a66253 5167{
7487953d
AJ
5168 TCGv t0 = tcg_temp_new();
5169 TCGv t1 = tcg_temp_new();
5170 tcg_gen_andi_tl(t1, cpu_gpr[rB(ctx->opcode)], 0x1F);
5171 tcg_gen_shr_tl(t0, cpu_gpr[rS(ctx->opcode)], t1);
5172 tcg_gen_subfi_tl(t1, 32, t1);
5173 tcg_gen_shl_tl(t1, cpu_gpr[rS(ctx->opcode)], t1);
5174 tcg_gen_or_tl(t1, t0, t1);
5175 tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], t0);
5176 gen_store_spr(SPR_MQ, t1);
5177 tcg_temp_free(t0);
5178 tcg_temp_free(t1);
76a66253 5179 if (unlikely(Rc(ctx->opcode) != 0))
7487953d 5180 gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
76a66253
JM
5181}
5182
5183/* srea - srea. */
99e300ef 5184static void gen_srea(DisasContext *ctx)
76a66253 5185{
7487953d
AJ
5186 TCGv t0 = tcg_temp_new();
5187 TCGv t1 = tcg_temp_new();
5188 tcg_gen_andi_tl(t1, cpu_gpr[rB(ctx->opcode)], 0x1F);
5189 tcg_gen_rotr_tl(t0, cpu_gpr[rS(ctx->opcode)], t1);
5190 gen_store_spr(SPR_MQ, t0);
5191 tcg_gen_sar_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)], t1);
5192 tcg_temp_free(t0);
5193 tcg_temp_free(t1);
76a66253 5194 if (unlikely(Rc(ctx->opcode) != 0))
7487953d 5195 gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
76a66253
JM
5196}
5197
5198/* sreq */
99e300ef 5199static void gen_sreq(DisasContext *ctx)
76a66253 5200{
7487953d
AJ
5201 TCGv t0 = tcg_temp_new();
5202 TCGv t1 = tcg_temp_new();
5203 TCGv t2 = tcg_temp_new();
5204 tcg_gen_andi_tl(t0, cpu_gpr[rB(ctx->opcode)], 0x1F);
5205 tcg_gen_movi_tl(t1, 0xFFFFFFFF);
5206 tcg_gen_shr_tl(t1, t1, t0);
5207 tcg_gen_rotr_tl(t0, cpu_gpr[rS(ctx->opcode)], t0);
5208 gen_load_spr(t2, SPR_MQ);
5209 gen_store_spr(SPR_MQ, t0);
5210 tcg_gen_and_tl(t0, t0, t1);
5211 tcg_gen_andc_tl(t2, t2, t1);
5212 tcg_gen_or_tl(cpu_gpr[rA(ctx->opcode)], t0, t2);
5213 tcg_temp_free(t0);
5214 tcg_temp_free(t1);
5215 tcg_temp_free(t2);
76a66253 5216 if (unlikely(Rc(ctx->opcode) != 0))
7487953d 5217 gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
76a66253
JM
5218}
5219
5220/* sriq */
99e300ef 5221static void gen_sriq(DisasContext *ctx)
76a66253 5222{
7487953d
AJ
5223 int sh = SH(ctx->opcode);
5224 TCGv t0 = tcg_temp_new();
5225 TCGv t1 = tcg_temp_new();
5226 tcg_gen_shri_tl(t0, cpu_gpr[rS(ctx->opcode)], sh);
5227 tcg_gen_shli_tl(t1, cpu_gpr[rS(ctx->opcode)], 32 - sh);
5228 tcg_gen_or_tl(t1, t0, t1);
5229 tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], t0);
5230 gen_store_spr(SPR_MQ, t1);
5231 tcg_temp_free(t0);
5232 tcg_temp_free(t1);
76a66253 5233 if (unlikely(Rc(ctx->opcode) != 0))
7487953d 5234 gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
76a66253
JM
5235}
5236
5237/* srliq */
99e300ef 5238static void gen_srliq(DisasContext *ctx)
76a66253 5239{
7487953d
AJ
5240 int sh = SH(ctx->opcode);
5241 TCGv t0 = tcg_temp_new();
5242 TCGv t1 = tcg_temp_new();
5243 tcg_gen_rotri_tl(t0, cpu_gpr[rS(ctx->opcode)], sh);
5244 gen_load_spr(t1, SPR_MQ);
5245 gen_store_spr(SPR_MQ, t0);
5246 tcg_gen_andi_tl(t0, t0, (0xFFFFFFFFU >> sh));
5247 tcg_gen_andi_tl(t1, t1, ~(0xFFFFFFFFU >> sh));
5248 tcg_gen_or_tl(cpu_gpr[rA(ctx->opcode)], t0, t1);
5249 tcg_temp_free(t0);
5250 tcg_temp_free(t1);
76a66253 5251 if (unlikely(Rc(ctx->opcode) != 0))
7487953d 5252 gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
76a66253
JM
5253}
5254
5255/* srlq */
99e300ef 5256static void gen_srlq(DisasContext *ctx)
76a66253 5257{
7487953d
AJ
5258 int l1 = gen_new_label();
5259 int l2 = gen_new_label();
5260 TCGv t0 = tcg_temp_local_new();
5261 TCGv t1 = tcg_temp_local_new();
5262 TCGv t2 = tcg_temp_local_new();
5263 tcg_gen_andi_tl(t2, cpu_gpr[rB(ctx->opcode)], 0x1F);
5264 tcg_gen_movi_tl(t1, 0xFFFFFFFF);
5265 tcg_gen_shr_tl(t2, t1, t2);
5266 tcg_gen_andi_tl(t0, cpu_gpr[rB(ctx->opcode)], 0x20);
5267 tcg_gen_brcondi_tl(TCG_COND_EQ, t0, 0, l1);
5268 gen_load_spr(t0, SPR_MQ);
5269 tcg_gen_and_tl(cpu_gpr[rA(ctx->opcode)], t0, t2);
5270 tcg_gen_br(l2);
5271 gen_set_label(l1);
5272 tcg_gen_shr_tl(t0, cpu_gpr[rS(ctx->opcode)], t2);
5273 tcg_gen_and_tl(t0, t0, t2);
5274 gen_load_spr(t1, SPR_MQ);
5275 tcg_gen_andc_tl(t1, t1, t2);
5276 tcg_gen_or_tl(cpu_gpr[rA(ctx->opcode)], t0, t1);
5277 gen_set_label(l2);
5278 tcg_temp_free(t0);
5279 tcg_temp_free(t1);
5280 tcg_temp_free(t2);
76a66253 5281 if (unlikely(Rc(ctx->opcode) != 0))
7487953d 5282 gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
76a66253
JM
5283}
5284
5285/* srq */
99e300ef 5286static void gen_srq(DisasContext *ctx)
76a66253 5287{
7487953d
AJ
5288 int l1 = gen_new_label();
5289 TCGv t0 = tcg_temp_new();
5290 TCGv t1 = tcg_temp_new();
5291 tcg_gen_andi_tl(t1, cpu_gpr[rB(ctx->opcode)], 0x1F);
5292 tcg_gen_shr_tl(t0, cpu_gpr[rS(ctx->opcode)], t1);
5293 tcg_gen_subfi_tl(t1, 32, t1);
5294 tcg_gen_shl_tl(t1, cpu_gpr[rS(ctx->opcode)], t1);
5295 tcg_gen_or_tl(t1, t0, t1);
5296 gen_store_spr(SPR_MQ, t1);
5297 tcg_gen_andi_tl(t1, cpu_gpr[rB(ctx->opcode)], 0x20);
5298 tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], t0);
5299 tcg_gen_brcondi_tl(TCG_COND_EQ, t0, 0, l1);
5300 tcg_gen_movi_tl(cpu_gpr[rA(ctx->opcode)], 0);
5301 gen_set_label(l1);
5302 tcg_temp_free(t0);
5303 tcg_temp_free(t1);
76a66253 5304 if (unlikely(Rc(ctx->opcode) != 0))
7487953d 5305 gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
76a66253
JM
5306}
5307
5308/* PowerPC 602 specific instructions */
99e300ef 5309
54623277 5310/* dsa */
99e300ef 5311static void gen_dsa(DisasContext *ctx)
76a66253
JM
5312{
5313 /* XXX: TODO */
e06fcd75 5314 gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL);
76a66253
JM
5315}
5316
5317/* esa */
99e300ef 5318static void gen_esa(DisasContext *ctx)
76a66253
JM
5319{
5320 /* XXX: TODO */
e06fcd75 5321 gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL);
76a66253
JM
5322}
5323
5324/* mfrom */
99e300ef 5325static void gen_mfrom(DisasContext *ctx)
76a66253
JM
5326{
5327#if defined(CONFIG_USER_ONLY)
e06fcd75 5328 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
76a66253 5329#else
76db3ba4 5330 if (unlikely(!ctx->mem_idx)) {
e06fcd75 5331 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
76a66253
JM
5332 return;
5333 }
cf02a65c 5334 gen_helper_602_mfrom(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)]);
76a66253
JM
5335#endif
5336}
5337
5338/* 602 - 603 - G2 TLB management */
e8eaa2c0 5339
54623277 5340/* tlbld */
e8eaa2c0 5341static void gen_tlbld_6xx(DisasContext *ctx)
76a66253
JM
5342{
5343#if defined(CONFIG_USER_ONLY)
e06fcd75 5344 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
76a66253 5345#else
76db3ba4 5346 if (unlikely(!ctx->mem_idx)) {
e06fcd75 5347 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
76a66253
JM
5348 return;
5349 }
c6c7cf05 5350 gen_helper_6xx_tlbd(cpu_env, cpu_gpr[rB(ctx->opcode)]);
76a66253
JM
5351#endif
5352}
5353
5354/* tlbli */
e8eaa2c0 5355static void gen_tlbli_6xx(DisasContext *ctx)
76a66253
JM
5356{
5357#if defined(CONFIG_USER_ONLY)
e06fcd75 5358 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
76a66253 5359#else
76db3ba4 5360 if (unlikely(!ctx->mem_idx)) {
e06fcd75 5361 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
76a66253
JM
5362 return;
5363 }
c6c7cf05 5364 gen_helper_6xx_tlbi(cpu_env, cpu_gpr[rB(ctx->opcode)]);
76a66253
JM
5365#endif
5366}
5367
7dbe11ac 5368/* 74xx TLB management */
e8eaa2c0 5369
54623277 5370/* tlbld */
e8eaa2c0 5371static void gen_tlbld_74xx(DisasContext *ctx)
7dbe11ac
JM
5372{
5373#if defined(CONFIG_USER_ONLY)
e06fcd75 5374 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
7dbe11ac 5375#else
76db3ba4 5376 if (unlikely(!ctx->mem_idx)) {
e06fcd75 5377 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
7dbe11ac
JM
5378 return;
5379 }
c6c7cf05 5380 gen_helper_74xx_tlbd(cpu_env, cpu_gpr[rB(ctx->opcode)]);
7dbe11ac
JM
5381#endif
5382}
5383
5384/* tlbli */
e8eaa2c0 5385static void gen_tlbli_74xx(DisasContext *ctx)
7dbe11ac
JM
5386{
5387#if defined(CONFIG_USER_ONLY)
e06fcd75 5388 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
7dbe11ac 5389#else
76db3ba4 5390 if (unlikely(!ctx->mem_idx)) {
e06fcd75 5391 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
7dbe11ac
JM
5392 return;
5393 }
c6c7cf05 5394 gen_helper_74xx_tlbi(cpu_env, cpu_gpr[rB(ctx->opcode)]);
7dbe11ac
JM
5395#endif
5396}
5397
76a66253 5398/* POWER instructions not in PowerPC 601 */
99e300ef 5399
54623277 5400/* clf */
99e300ef 5401static void gen_clf(DisasContext *ctx)
76a66253
JM
5402{
5403 /* Cache line flush: implemented as no-op */
5404}
5405
5406/* cli */
99e300ef 5407static void gen_cli(DisasContext *ctx)
76a66253 5408{
7f75ffd3 5409 /* Cache line invalidate: privileged and treated as no-op */
76a66253 5410#if defined(CONFIG_USER_ONLY)
e06fcd75 5411 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
76a66253 5412#else
76db3ba4 5413 if (unlikely(!ctx->mem_idx)) {
e06fcd75 5414 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
76a66253
JM
5415 return;
5416 }
5417#endif
5418}
5419
5420/* dclst */
99e300ef 5421static void gen_dclst(DisasContext *ctx)
76a66253
JM
5422{
5423 /* Data cache line store: treated as no-op */
5424}
5425
99e300ef 5426static void gen_mfsri(DisasContext *ctx)
76a66253
JM
5427{
5428#if defined(CONFIG_USER_ONLY)
e06fcd75 5429 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
76a66253 5430#else
74d37793
AJ
5431 int ra = rA(ctx->opcode);
5432 int rd = rD(ctx->opcode);
5433 TCGv t0;
76db3ba4 5434 if (unlikely(!ctx->mem_idx)) {
e06fcd75 5435 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
76a66253
JM
5436 return;
5437 }
74d37793 5438 t0 = tcg_temp_new();
76db3ba4 5439 gen_addr_reg_index(ctx, t0);
74d37793
AJ
5440 tcg_gen_shri_tl(t0, t0, 28);
5441 tcg_gen_andi_tl(t0, t0, 0xF);
c6c7cf05 5442 gen_helper_load_sr(cpu_gpr[rd], cpu_env, t0);
74d37793 5443 tcg_temp_free(t0);
76a66253 5444 if (ra != 0 && ra != rd)
74d37793 5445 tcg_gen_mov_tl(cpu_gpr[ra], cpu_gpr[rd]);
76a66253
JM
5446#endif
5447}
5448
99e300ef 5449static void gen_rac(DisasContext *ctx)
76a66253
JM
5450{
5451#if defined(CONFIG_USER_ONLY)
e06fcd75 5452 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
76a66253 5453#else
22e0e173 5454 TCGv t0;
76db3ba4 5455 if (unlikely(!ctx->mem_idx)) {
e06fcd75 5456 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
76a66253
JM
5457 return;
5458 }
22e0e173 5459 t0 = tcg_temp_new();
76db3ba4 5460 gen_addr_reg_index(ctx, t0);
c6c7cf05 5461 gen_helper_rac(cpu_gpr[rD(ctx->opcode)], cpu_env, t0);
22e0e173 5462 tcg_temp_free(t0);
76a66253
JM
5463#endif
5464}
5465
99e300ef 5466static void gen_rfsvc(DisasContext *ctx)
76a66253
JM
5467{
5468#if defined(CONFIG_USER_ONLY)
e06fcd75 5469 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
76a66253 5470#else
76db3ba4 5471 if (unlikely(!ctx->mem_idx)) {
e06fcd75 5472 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
76a66253
JM
5473 return;
5474 }
e5f17ac6 5475 gen_helper_rfsvc(cpu_env);
e06fcd75 5476 gen_sync_exception(ctx);
76a66253
JM
5477#endif
5478}
5479
5480/* svc is not implemented for now */
5481
5482/* POWER2 specific instructions */
5483/* Quad manipulation (load/store two floats at a time) */
76a66253
JM
5484
5485/* lfq */
99e300ef 5486static void gen_lfq(DisasContext *ctx)
76a66253 5487{
01a4afeb 5488 int rd = rD(ctx->opcode);
76db3ba4
AJ
5489 TCGv t0;
5490 gen_set_access_type(ctx, ACCESS_FLOAT);
5491 t0 = tcg_temp_new();
5492 gen_addr_imm_index(ctx, t0, 0);
5493 gen_qemu_ld64(ctx, cpu_fpr[rd], t0);
5494 gen_addr_add(ctx, t0, t0, 8);
5495 gen_qemu_ld64(ctx, cpu_fpr[(rd + 1) % 32], t0);
01a4afeb 5496 tcg_temp_free(t0);
76a66253
JM
5497}
5498
5499/* lfqu */
99e300ef 5500static void gen_lfqu(DisasContext *ctx)
76a66253
JM
5501{
5502 int ra = rA(ctx->opcode);
01a4afeb 5503 int rd = rD(ctx->opcode);
76db3ba4
AJ
5504 TCGv t0, t1;
5505 gen_set_access_type(ctx, ACCESS_FLOAT);
5506 t0 = tcg_temp_new();
5507 t1 = tcg_temp_new();
5508 gen_addr_imm_index(ctx, t0, 0);
5509 gen_qemu_ld64(ctx, cpu_fpr[rd], t0);
5510 gen_addr_add(ctx, t1, t0, 8);
5511 gen_qemu_ld64(ctx, cpu_fpr[(rd + 1) % 32], t1);
76a66253 5512 if (ra != 0)
01a4afeb
AJ
5513 tcg_gen_mov_tl(cpu_gpr[ra], t0);
5514 tcg_temp_free(t0);
5515 tcg_temp_free(t1);
76a66253
JM
5516}
5517
5518/* lfqux */
99e300ef 5519static void gen_lfqux(DisasContext *ctx)
76a66253
JM
5520{
5521 int ra = rA(ctx->opcode);
01a4afeb 5522 int rd = rD(ctx->opcode);
76db3ba4
AJ
5523 gen_set_access_type(ctx, ACCESS_FLOAT);
5524 TCGv t0, t1;
5525 t0 = tcg_temp_new();
5526 gen_addr_reg_index(ctx, t0);
5527 gen_qemu_ld64(ctx, cpu_fpr[rd], t0);
5528 t1 = tcg_temp_new();
5529 gen_addr_add(ctx, t1, t0, 8);
5530 gen_qemu_ld64(ctx, cpu_fpr[(rd + 1) % 32], t1);
5531 tcg_temp_free(t1);
76a66253 5532 if (ra != 0)
01a4afeb
AJ
5533 tcg_gen_mov_tl(cpu_gpr[ra], t0);
5534 tcg_temp_free(t0);
76a66253
JM
5535}
5536
5537/* lfqx */
99e300ef 5538static void gen_lfqx(DisasContext *ctx)
76a66253 5539{
01a4afeb 5540 int rd = rD(ctx->opcode);
76db3ba4
AJ
5541 TCGv t0;
5542 gen_set_access_type(ctx, ACCESS_FLOAT);
5543 t0 = tcg_temp_new();
5544 gen_addr_reg_index(ctx, t0);
5545 gen_qemu_ld64(ctx, cpu_fpr[rd], t0);
5546 gen_addr_add(ctx, t0, t0, 8);
5547 gen_qemu_ld64(ctx, cpu_fpr[(rd + 1) % 32], t0);
01a4afeb 5548 tcg_temp_free(t0);
76a66253
JM
5549}
5550
5551/* stfq */
99e300ef 5552static void gen_stfq(DisasContext *ctx)
76a66253 5553{
01a4afeb 5554 int rd = rD(ctx->opcode);
76db3ba4
AJ
5555 TCGv t0;
5556 gen_set_access_type(ctx, ACCESS_FLOAT);
5557 t0 = tcg_temp_new();
5558 gen_addr_imm_index(ctx, t0, 0);
5559 gen_qemu_st64(ctx, cpu_fpr[rd], t0);
5560 gen_addr_add(ctx, t0, t0, 8);
5561 gen_qemu_st64(ctx, cpu_fpr[(rd + 1) % 32], t0);
01a4afeb 5562 tcg_temp_free(t0);
76a66253
JM
5563}
5564
5565/* stfqu */
99e300ef 5566static void gen_stfqu(DisasContext *ctx)
76a66253
JM
5567{
5568 int ra = rA(ctx->opcode);
01a4afeb 5569 int rd = rD(ctx->opcode);
76db3ba4
AJ
5570 TCGv t0, t1;
5571 gen_set_access_type(ctx, ACCESS_FLOAT);
5572 t0 = tcg_temp_new();
5573 gen_addr_imm_index(ctx, t0, 0);
5574 gen_qemu_st64(ctx, cpu_fpr[rd], t0);
5575 t1 = tcg_temp_new();
5576 gen_addr_add(ctx, t1, t0, 8);
5577 gen_qemu_st64(ctx, cpu_fpr[(rd + 1) % 32], t1);
5578 tcg_temp_free(t1);
76a66253 5579 if (ra != 0)
01a4afeb
AJ
5580 tcg_gen_mov_tl(cpu_gpr[ra], t0);
5581 tcg_temp_free(t0);
76a66253
JM
5582}
5583
5584/* stfqux */
99e300ef 5585static void gen_stfqux(DisasContext *ctx)
76a66253
JM
5586{
5587 int ra = rA(ctx->opcode);
01a4afeb 5588 int rd = rD(ctx->opcode);
76db3ba4
AJ
5589 TCGv t0, t1;
5590 gen_set_access_type(ctx, ACCESS_FLOAT);
5591 t0 = tcg_temp_new();
5592 gen_addr_reg_index(ctx, t0);
5593 gen_qemu_st64(ctx, cpu_fpr[rd], t0);
5594 t1 = tcg_temp_new();
5595 gen_addr_add(ctx, t1, t0, 8);
5596 gen_qemu_st64(ctx, cpu_fpr[(rd + 1) % 32], t1);
5597 tcg_temp_free(t1);
76a66253 5598 if (ra != 0)
01a4afeb
AJ
5599 tcg_gen_mov_tl(cpu_gpr[ra], t0);
5600 tcg_temp_free(t0);
76a66253
JM
5601}
5602
5603/* stfqx */
99e300ef 5604static void gen_stfqx(DisasContext *ctx)
76a66253 5605{
01a4afeb 5606 int rd = rD(ctx->opcode);
76db3ba4
AJ
5607 TCGv t0;
5608 gen_set_access_type(ctx, ACCESS_FLOAT);
5609 t0 = tcg_temp_new();
5610 gen_addr_reg_index(ctx, t0);
5611 gen_qemu_st64(ctx, cpu_fpr[rd], t0);
5612 gen_addr_add(ctx, t0, t0, 8);
5613 gen_qemu_st64(ctx, cpu_fpr[(rd + 1) % 32], t0);
01a4afeb 5614 tcg_temp_free(t0);
76a66253
JM
5615}
5616
5617/* BookE specific instructions */
99e300ef 5618
54623277 5619/* XXX: not implemented on 440 ? */
99e300ef 5620static void gen_mfapidi(DisasContext *ctx)
76a66253
JM
5621{
5622 /* XXX: TODO */
e06fcd75 5623 gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL);
76a66253
JM
5624}
5625
2662a059 5626/* XXX: not implemented on 440 ? */
99e300ef 5627static void gen_tlbiva(DisasContext *ctx)
76a66253
JM
5628{
5629#if defined(CONFIG_USER_ONLY)
e06fcd75 5630 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
76a66253 5631#else
74d37793 5632 TCGv t0;
76db3ba4 5633 if (unlikely(!ctx->mem_idx)) {
e06fcd75 5634 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
76a66253
JM
5635 return;
5636 }
ec72e276 5637 t0 = tcg_temp_new();
76db3ba4 5638 gen_addr_reg_index(ctx, t0);
c6c7cf05 5639 gen_helper_tlbie(cpu_env, cpu_gpr[rB(ctx->opcode)]);
74d37793 5640 tcg_temp_free(t0);
76a66253
JM
5641#endif
5642}
5643
5644/* All 405 MAC instructions are translated here */
636aa200
BS
5645static inline void gen_405_mulladd_insn(DisasContext *ctx, int opc2, int opc3,
5646 int ra, int rb, int rt, int Rc)
76a66253 5647{
182608d4
AJ
5648 TCGv t0, t1;
5649
a7812ae4
PB
5650 t0 = tcg_temp_local_new();
5651 t1 = tcg_temp_local_new();
182608d4 5652
76a66253
JM
5653 switch (opc3 & 0x0D) {
5654 case 0x05:
5655 /* macchw - macchw. - macchwo - macchwo. */
5656 /* macchws - macchws. - macchwso - macchwso. */
5657 /* nmacchw - nmacchw. - nmacchwo - nmacchwo. */
5658 /* nmacchws - nmacchws. - nmacchwso - nmacchwso. */
5659 /* mulchw - mulchw. */
182608d4
AJ
5660 tcg_gen_ext16s_tl(t0, cpu_gpr[ra]);
5661 tcg_gen_sari_tl(t1, cpu_gpr[rb], 16);
5662 tcg_gen_ext16s_tl(t1, t1);
76a66253
JM
5663 break;
5664 case 0x04:
5665 /* macchwu - macchwu. - macchwuo - macchwuo. */
5666 /* macchwsu - macchwsu. - macchwsuo - macchwsuo. */
5667 /* mulchwu - mulchwu. */
182608d4
AJ
5668 tcg_gen_ext16u_tl(t0, cpu_gpr[ra]);
5669 tcg_gen_shri_tl(t1, cpu_gpr[rb], 16);
5670 tcg_gen_ext16u_tl(t1, t1);
76a66253
JM
5671 break;
5672 case 0x01:
5673 /* machhw - machhw. - machhwo - machhwo. */
5674 /* machhws - machhws. - machhwso - machhwso. */
5675 /* nmachhw - nmachhw. - nmachhwo - nmachhwo. */
5676 /* nmachhws - nmachhws. - nmachhwso - nmachhwso. */
5677 /* mulhhw - mulhhw. */
182608d4
AJ
5678 tcg_gen_sari_tl(t0, cpu_gpr[ra], 16);
5679 tcg_gen_ext16s_tl(t0, t0);
5680 tcg_gen_sari_tl(t1, cpu_gpr[rb], 16);
5681 tcg_gen_ext16s_tl(t1, t1);
76a66253
JM
5682 break;
5683 case 0x00:
5684 /* machhwu - machhwu. - machhwuo - machhwuo. */
5685 /* machhwsu - machhwsu. - machhwsuo - machhwsuo. */
5686 /* mulhhwu - mulhhwu. */
182608d4
AJ
5687 tcg_gen_shri_tl(t0, cpu_gpr[ra], 16);
5688 tcg_gen_ext16u_tl(t0, t0);
5689 tcg_gen_shri_tl(t1, cpu_gpr[rb], 16);
5690 tcg_gen_ext16u_tl(t1, t1);
76a66253
JM
5691 break;
5692 case 0x0D:
5693 /* maclhw - maclhw. - maclhwo - maclhwo. */
5694 /* maclhws - maclhws. - maclhwso - maclhwso. */
5695 /* nmaclhw - nmaclhw. - nmaclhwo - nmaclhwo. */
5696 /* nmaclhws - nmaclhws. - nmaclhwso - nmaclhwso. */
5697 /* mullhw - mullhw. */
182608d4
AJ
5698 tcg_gen_ext16s_tl(t0, cpu_gpr[ra]);
5699 tcg_gen_ext16s_tl(t1, cpu_gpr[rb]);
76a66253
JM
5700 break;
5701 case 0x0C:
5702 /* maclhwu - maclhwu. - maclhwuo - maclhwuo. */
5703 /* maclhwsu - maclhwsu. - maclhwsuo - maclhwsuo. */
5704 /* mullhwu - mullhwu. */
182608d4
AJ
5705 tcg_gen_ext16u_tl(t0, cpu_gpr[ra]);
5706 tcg_gen_ext16u_tl(t1, cpu_gpr[rb]);
76a66253
JM
5707 break;
5708 }
76a66253 5709 if (opc2 & 0x04) {
182608d4
AJ
5710 /* (n)multiply-and-accumulate (0x0C / 0x0E) */
5711 tcg_gen_mul_tl(t1, t0, t1);
5712 if (opc2 & 0x02) {
5713 /* nmultiply-and-accumulate (0x0E) */
5714 tcg_gen_sub_tl(t0, cpu_gpr[rt], t1);
5715 } else {
5716 /* multiply-and-accumulate (0x0C) */
5717 tcg_gen_add_tl(t0, cpu_gpr[rt], t1);
5718 }
5719
5720 if (opc3 & 0x12) {
5721 /* Check overflow and/or saturate */
5722 int l1 = gen_new_label();
5723
5724 if (opc3 & 0x10) {
5725 /* Start with XER OV disabled, the most likely case */
da91a00f 5726 tcg_gen_movi_tl(cpu_ov, 0);
182608d4
AJ
5727 }
5728 if (opc3 & 0x01) {
5729 /* Signed */
5730 tcg_gen_xor_tl(t1, cpu_gpr[rt], t1);
5731 tcg_gen_brcondi_tl(TCG_COND_GE, t1, 0, l1);
5732 tcg_gen_xor_tl(t1, cpu_gpr[rt], t0);
5733 tcg_gen_brcondi_tl(TCG_COND_LT, t1, 0, l1);
bdc4e053 5734 if (opc3 & 0x02) {
182608d4
AJ
5735 /* Saturate */
5736 tcg_gen_sari_tl(t0, cpu_gpr[rt], 31);
5737 tcg_gen_xori_tl(t0, t0, 0x7fffffff);
5738 }
5739 } else {
5740 /* Unsigned */
5741 tcg_gen_brcond_tl(TCG_COND_GEU, t0, t1, l1);
bdc4e053 5742 if (opc3 & 0x02) {
182608d4
AJ
5743 /* Saturate */
5744 tcg_gen_movi_tl(t0, UINT32_MAX);
5745 }
5746 }
5747 if (opc3 & 0x10) {
5748 /* Check overflow */
da91a00f
RH
5749 tcg_gen_movi_tl(cpu_ov, 1);
5750 tcg_gen_movi_tl(cpu_so, 1);
182608d4
AJ
5751 }
5752 gen_set_label(l1);
5753 tcg_gen_mov_tl(cpu_gpr[rt], t0);
5754 }
5755 } else {
5756 tcg_gen_mul_tl(cpu_gpr[rt], t0, t1);
76a66253 5757 }
182608d4
AJ
5758 tcg_temp_free(t0);
5759 tcg_temp_free(t1);
76a66253
JM
5760 if (unlikely(Rc) != 0) {
5761 /* Update Rc0 */
182608d4 5762 gen_set_Rc0(ctx, cpu_gpr[rt]);
76a66253
JM
5763 }
5764}
5765
a750fc0b 5766#define GEN_MAC_HANDLER(name, opc2, opc3) \
99e300ef 5767static void glue(gen_, name)(DisasContext *ctx) \
76a66253
JM
5768{ \
5769 gen_405_mulladd_insn(ctx, opc2, opc3, rA(ctx->opcode), rB(ctx->opcode), \
5770 rD(ctx->opcode), Rc(ctx->opcode)); \
5771}
5772
5773/* macchw - macchw. */
a750fc0b 5774GEN_MAC_HANDLER(macchw, 0x0C, 0x05);
76a66253 5775/* macchwo - macchwo. */
a750fc0b 5776GEN_MAC_HANDLER(macchwo, 0x0C, 0x15);
76a66253 5777/* macchws - macchws. */
a750fc0b 5778GEN_MAC_HANDLER(macchws, 0x0C, 0x07);
76a66253 5779/* macchwso - macchwso. */
a750fc0b 5780GEN_MAC_HANDLER(macchwso, 0x0C, 0x17);
76a66253 5781/* macchwsu - macchwsu. */
a750fc0b 5782GEN_MAC_HANDLER(macchwsu, 0x0C, 0x06);
76a66253 5783/* macchwsuo - macchwsuo. */
a750fc0b 5784GEN_MAC_HANDLER(macchwsuo, 0x0C, 0x16);
76a66253 5785/* macchwu - macchwu. */
a750fc0b 5786GEN_MAC_HANDLER(macchwu, 0x0C, 0x04);
76a66253 5787/* macchwuo - macchwuo. */
a750fc0b 5788GEN_MAC_HANDLER(macchwuo, 0x0C, 0x14);
76a66253 5789/* machhw - machhw. */
a750fc0b 5790GEN_MAC_HANDLER(machhw, 0x0C, 0x01);
76a66253 5791/* machhwo - machhwo. */
a750fc0b 5792GEN_MAC_HANDLER(machhwo, 0x0C, 0x11);
76a66253 5793/* machhws - machhws. */
a750fc0b 5794GEN_MAC_HANDLER(machhws, 0x0C, 0x03);
76a66253 5795/* machhwso - machhwso. */
a750fc0b 5796GEN_MAC_HANDLER(machhwso, 0x0C, 0x13);
76a66253 5797/* machhwsu - machhwsu. */
a750fc0b 5798GEN_MAC_HANDLER(machhwsu, 0x0C, 0x02);
76a66253 5799/* machhwsuo - machhwsuo. */
a750fc0b 5800GEN_MAC_HANDLER(machhwsuo, 0x0C, 0x12);
76a66253 5801/* machhwu - machhwu. */
a750fc0b 5802GEN_MAC_HANDLER(machhwu, 0x0C, 0x00);
76a66253 5803/* machhwuo - machhwuo. */
a750fc0b 5804GEN_MAC_HANDLER(machhwuo, 0x0C, 0x10);
76a66253 5805/* maclhw - maclhw. */
a750fc0b 5806GEN_MAC_HANDLER(maclhw, 0x0C, 0x0D);
76a66253 5807/* maclhwo - maclhwo. */
a750fc0b 5808GEN_MAC_HANDLER(maclhwo, 0x0C, 0x1D);
76a66253 5809/* maclhws - maclhws. */
a750fc0b 5810GEN_MAC_HANDLER(maclhws, 0x0C, 0x0F);
76a66253 5811/* maclhwso - maclhwso. */
a750fc0b 5812GEN_MAC_HANDLER(maclhwso, 0x0C, 0x1F);
76a66253 5813/* maclhwu - maclhwu. */
a750fc0b 5814GEN_MAC_HANDLER(maclhwu, 0x0C, 0x0C);
76a66253 5815/* maclhwuo - maclhwuo. */
a750fc0b 5816GEN_MAC_HANDLER(maclhwuo, 0x0C, 0x1C);
76a66253 5817/* maclhwsu - maclhwsu. */
a750fc0b 5818GEN_MAC_HANDLER(maclhwsu, 0x0C, 0x0E);
76a66253 5819/* maclhwsuo - maclhwsuo. */
a750fc0b 5820GEN_MAC_HANDLER(maclhwsuo, 0x0C, 0x1E);
76a66253 5821/* nmacchw - nmacchw. */
a750fc0b 5822GEN_MAC_HANDLER(nmacchw, 0x0E, 0x05);
76a66253 5823/* nmacchwo - nmacchwo. */
a750fc0b 5824GEN_MAC_HANDLER(nmacchwo, 0x0E, 0x15);
76a66253 5825/* nmacchws - nmacchws. */
a750fc0b 5826GEN_MAC_HANDLER(nmacchws, 0x0E, 0x07);
76a66253 5827/* nmacchwso - nmacchwso. */
a750fc0b 5828GEN_MAC_HANDLER(nmacchwso, 0x0E, 0x17);
76a66253 5829/* nmachhw - nmachhw. */
a750fc0b 5830GEN_MAC_HANDLER(nmachhw, 0x0E, 0x01);
76a66253 5831/* nmachhwo - nmachhwo. */
a750fc0b 5832GEN_MAC_HANDLER(nmachhwo, 0x0E, 0x11);
76a66253 5833/* nmachhws - nmachhws. */
a750fc0b 5834GEN_MAC_HANDLER(nmachhws, 0x0E, 0x03);
76a66253 5835/* nmachhwso - nmachhwso. */
a750fc0b 5836GEN_MAC_HANDLER(nmachhwso, 0x0E, 0x13);
76a66253 5837/* nmaclhw - nmaclhw. */
a750fc0b 5838GEN_MAC_HANDLER(nmaclhw, 0x0E, 0x0D);
76a66253 5839/* nmaclhwo - nmaclhwo. */
a750fc0b 5840GEN_MAC_HANDLER(nmaclhwo, 0x0E, 0x1D);
76a66253 5841/* nmaclhws - nmaclhws. */
a750fc0b 5842GEN_MAC_HANDLER(nmaclhws, 0x0E, 0x0F);
76a66253 5843/* nmaclhwso - nmaclhwso. */
a750fc0b 5844GEN_MAC_HANDLER(nmaclhwso, 0x0E, 0x1F);
76a66253
JM
5845
5846/* mulchw - mulchw. */
a750fc0b 5847GEN_MAC_HANDLER(mulchw, 0x08, 0x05);
76a66253 5848/* mulchwu - mulchwu. */
a750fc0b 5849GEN_MAC_HANDLER(mulchwu, 0x08, 0x04);
76a66253 5850/* mulhhw - mulhhw. */
a750fc0b 5851GEN_MAC_HANDLER(mulhhw, 0x08, 0x01);
76a66253 5852/* mulhhwu - mulhhwu. */
a750fc0b 5853GEN_MAC_HANDLER(mulhhwu, 0x08, 0x00);
76a66253 5854/* mullhw - mullhw. */
a750fc0b 5855GEN_MAC_HANDLER(mullhw, 0x08, 0x0D);
76a66253 5856/* mullhwu - mullhwu. */
a750fc0b 5857GEN_MAC_HANDLER(mullhwu, 0x08, 0x0C);
76a66253
JM
5858
5859/* mfdcr */
99e300ef 5860static void gen_mfdcr(DisasContext *ctx)
76a66253
JM
5861{
5862#if defined(CONFIG_USER_ONLY)
e06fcd75 5863 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
76a66253 5864#else
06dca6a7 5865 TCGv dcrn;
76db3ba4 5866 if (unlikely(!ctx->mem_idx)) {
e06fcd75 5867 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
76a66253
JM
5868 return;
5869 }
06dca6a7
AJ
5870 /* NIP cannot be restored if the memory exception comes from an helper */
5871 gen_update_nip(ctx, ctx->nip - 4);
5872 dcrn = tcg_const_tl(SPR(ctx->opcode));
d0f1562d 5873 gen_helper_load_dcr(cpu_gpr[rD(ctx->opcode)], cpu_env, dcrn);
06dca6a7 5874 tcg_temp_free(dcrn);
76a66253
JM
5875#endif
5876}
5877
5878/* mtdcr */
99e300ef 5879static void gen_mtdcr(DisasContext *ctx)
76a66253
JM
5880{
5881#if defined(CONFIG_USER_ONLY)
e06fcd75 5882 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
76a66253 5883#else
06dca6a7 5884 TCGv dcrn;
76db3ba4 5885 if (unlikely(!ctx->mem_idx)) {
e06fcd75 5886 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
76a66253
JM
5887 return;
5888 }
06dca6a7
AJ
5889 /* NIP cannot be restored if the memory exception comes from an helper */
5890 gen_update_nip(ctx, ctx->nip - 4);
5891 dcrn = tcg_const_tl(SPR(ctx->opcode));
d0f1562d 5892 gen_helper_store_dcr(cpu_env, dcrn, cpu_gpr[rS(ctx->opcode)]);
06dca6a7 5893 tcg_temp_free(dcrn);
a42bd6cc
JM
5894#endif
5895}
5896
5897/* mfdcrx */
2662a059 5898/* XXX: not implemented on 440 ? */
99e300ef 5899static void gen_mfdcrx(DisasContext *ctx)
a42bd6cc
JM
5900{
5901#if defined(CONFIG_USER_ONLY)
e06fcd75 5902 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
a42bd6cc 5903#else
76db3ba4 5904 if (unlikely(!ctx->mem_idx)) {
e06fcd75 5905 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
a42bd6cc
JM
5906 return;
5907 }
06dca6a7
AJ
5908 /* NIP cannot be restored if the memory exception comes from an helper */
5909 gen_update_nip(ctx, ctx->nip - 4);
d0f1562d
BS
5910 gen_helper_load_dcr(cpu_gpr[rD(ctx->opcode)], cpu_env,
5911 cpu_gpr[rA(ctx->opcode)]);
a750fc0b 5912 /* Note: Rc update flag set leads to undefined state of Rc0 */
a42bd6cc
JM
5913#endif
5914}
5915
5916/* mtdcrx */
2662a059 5917/* XXX: not implemented on 440 ? */
99e300ef 5918static void gen_mtdcrx(DisasContext *ctx)
a42bd6cc
JM
5919{
5920#if defined(CONFIG_USER_ONLY)
e06fcd75 5921 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
a42bd6cc 5922#else
76db3ba4 5923 if (unlikely(!ctx->mem_idx)) {
e06fcd75 5924 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
a42bd6cc
JM
5925 return;
5926 }
06dca6a7
AJ
5927 /* NIP cannot be restored if the memory exception comes from an helper */
5928 gen_update_nip(ctx, ctx->nip - 4);
d0f1562d
BS
5929 gen_helper_store_dcr(cpu_env, cpu_gpr[rA(ctx->opcode)],
5930 cpu_gpr[rS(ctx->opcode)]);
a750fc0b 5931 /* Note: Rc update flag set leads to undefined state of Rc0 */
76a66253
JM
5932#endif
5933}
5934
a750fc0b 5935/* mfdcrux (PPC 460) : user-mode access to DCR */
99e300ef 5936static void gen_mfdcrux(DisasContext *ctx)
a750fc0b 5937{
06dca6a7
AJ
5938 /* NIP cannot be restored if the memory exception comes from an helper */
5939 gen_update_nip(ctx, ctx->nip - 4);
d0f1562d
BS
5940 gen_helper_load_dcr(cpu_gpr[rD(ctx->opcode)], cpu_env,
5941 cpu_gpr[rA(ctx->opcode)]);
a750fc0b
JM
5942 /* Note: Rc update flag set leads to undefined state of Rc0 */
5943}
5944
5945/* mtdcrux (PPC 460) : user-mode access to DCR */
99e300ef 5946static void gen_mtdcrux(DisasContext *ctx)
a750fc0b 5947{
06dca6a7
AJ
5948 /* NIP cannot be restored if the memory exception comes from an helper */
5949 gen_update_nip(ctx, ctx->nip - 4);
975e5463 5950 gen_helper_store_dcr(cpu_env, cpu_gpr[rA(ctx->opcode)],
d0f1562d 5951 cpu_gpr[rS(ctx->opcode)]);
a750fc0b
JM
5952 /* Note: Rc update flag set leads to undefined state of Rc0 */
5953}
5954
76a66253 5955/* dccci */
99e300ef 5956static void gen_dccci(DisasContext *ctx)
76a66253
JM
5957{
5958#if defined(CONFIG_USER_ONLY)
e06fcd75 5959 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
76a66253 5960#else
76db3ba4 5961 if (unlikely(!ctx->mem_idx)) {
e06fcd75 5962 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
76a66253
JM
5963 return;
5964 }
5965 /* interpreted as no-op */
5966#endif
5967}
5968
5969/* dcread */
99e300ef 5970static void gen_dcread(DisasContext *ctx)
76a66253
JM
5971{
5972#if defined(CONFIG_USER_ONLY)
e06fcd75 5973 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
76a66253 5974#else
b61f2753 5975 TCGv EA, val;
76db3ba4 5976 if (unlikely(!ctx->mem_idx)) {
e06fcd75 5977 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
76a66253
JM
5978 return;
5979 }
76db3ba4 5980 gen_set_access_type(ctx, ACCESS_CACHE);
a7812ae4 5981 EA = tcg_temp_new();
76db3ba4 5982 gen_addr_reg_index(ctx, EA);
a7812ae4 5983 val = tcg_temp_new();
76db3ba4 5984 gen_qemu_ld32u(ctx, val, EA);
b61f2753
AJ
5985 tcg_temp_free(val);
5986 tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], EA);
5987 tcg_temp_free(EA);
76a66253
JM
5988#endif
5989}
5990
5991/* icbt */
e8eaa2c0 5992static void gen_icbt_40x(DisasContext *ctx)
76a66253
JM
5993{
5994 /* interpreted as no-op */
5995 /* XXX: specification say this is treated as a load by the MMU
5996 * but does not generate any exception
5997 */
5998}
5999
6000/* iccci */
99e300ef 6001static void gen_iccci(DisasContext *ctx)
76a66253
JM
6002{
6003#if defined(CONFIG_USER_ONLY)
e06fcd75 6004 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
76a66253 6005#else
76db3ba4 6006 if (unlikely(!ctx->mem_idx)) {
e06fcd75 6007 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
76a66253
JM
6008 return;
6009 }
6010 /* interpreted as no-op */
6011#endif
6012}
6013
6014/* icread */
99e300ef 6015static void gen_icread(DisasContext *ctx)
76a66253
JM
6016{
6017#if defined(CONFIG_USER_ONLY)
e06fcd75 6018 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
76a66253 6019#else
76db3ba4 6020 if (unlikely(!ctx->mem_idx)) {
e06fcd75 6021 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
76a66253
JM
6022 return;
6023 }
6024 /* interpreted as no-op */
6025#endif
6026}
6027
76db3ba4 6028/* rfci (mem_idx only) */
e8eaa2c0 6029static void gen_rfci_40x(DisasContext *ctx)
a42bd6cc
JM
6030{
6031#if defined(CONFIG_USER_ONLY)
e06fcd75 6032 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
a42bd6cc 6033#else
76db3ba4 6034 if (unlikely(!ctx->mem_idx)) {
e06fcd75 6035 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
a42bd6cc
JM
6036 return;
6037 }
6038 /* Restore CPU state */
e5f17ac6 6039 gen_helper_40x_rfci(cpu_env);
e06fcd75 6040 gen_sync_exception(ctx);
a42bd6cc
JM
6041#endif
6042}
6043
99e300ef 6044static void gen_rfci(DisasContext *ctx)
a42bd6cc
JM
6045{
6046#if defined(CONFIG_USER_ONLY)
e06fcd75 6047 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
a42bd6cc 6048#else
76db3ba4 6049 if (unlikely(!ctx->mem_idx)) {
e06fcd75 6050 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
a42bd6cc
JM
6051 return;
6052 }
6053 /* Restore CPU state */
e5f17ac6 6054 gen_helper_rfci(cpu_env);
e06fcd75 6055 gen_sync_exception(ctx);
a42bd6cc
JM
6056#endif
6057}
6058
6059/* BookE specific */
99e300ef 6060
54623277 6061/* XXX: not implemented on 440 ? */
99e300ef 6062static void gen_rfdi(DisasContext *ctx)
76a66253
JM
6063{
6064#if defined(CONFIG_USER_ONLY)
e06fcd75 6065 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
76a66253 6066#else
76db3ba4 6067 if (unlikely(!ctx->mem_idx)) {
e06fcd75 6068 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
76a66253
JM
6069 return;
6070 }
6071 /* Restore CPU state */
e5f17ac6 6072 gen_helper_rfdi(cpu_env);
e06fcd75 6073 gen_sync_exception(ctx);
76a66253
JM
6074#endif
6075}
6076
2662a059 6077/* XXX: not implemented on 440 ? */
99e300ef 6078static void gen_rfmci(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_rfmci(cpu_env);
e06fcd75 6089 gen_sync_exception(ctx);
a42bd6cc
JM
6090#endif
6091}
5eb7995e 6092
d9bce9d9 6093/* TLB management - PowerPC 405 implementation */
e8eaa2c0 6094
54623277 6095/* tlbre */
e8eaa2c0 6096static void gen_tlbre_40x(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 switch (rB(ctx->opcode)) {
6106 case 0:
c6c7cf05
BS
6107 gen_helper_4xx_tlbre_hi(cpu_gpr[rD(ctx->opcode)], cpu_env,
6108 cpu_gpr[rA(ctx->opcode)]);
76a66253
JM
6109 break;
6110 case 1:
c6c7cf05
BS
6111 gen_helper_4xx_tlbre_lo(cpu_gpr[rD(ctx->opcode)], cpu_env,
6112 cpu_gpr[rA(ctx->opcode)]);
76a66253
JM
6113 break;
6114 default:
e06fcd75 6115 gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL);
76a66253 6116 break;
9a64fbe4 6117 }
76a66253
JM
6118#endif
6119}
6120
d9bce9d9 6121/* tlbsx - tlbsx. */
e8eaa2c0 6122static void gen_tlbsx_40x(DisasContext *ctx)
76a66253
JM
6123{
6124#if defined(CONFIG_USER_ONLY)
e06fcd75 6125 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
76a66253 6126#else
74d37793 6127 TCGv t0;
76db3ba4 6128 if (unlikely(!ctx->mem_idx)) {
e06fcd75 6129 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
76a66253
JM
6130 return;
6131 }
74d37793 6132 t0 = tcg_temp_new();
76db3ba4 6133 gen_addr_reg_index(ctx, t0);
c6c7cf05 6134 gen_helper_4xx_tlbsx(cpu_gpr[rD(ctx->opcode)], cpu_env, t0);
74d37793
AJ
6135 tcg_temp_free(t0);
6136 if (Rc(ctx->opcode)) {
6137 int l1 = gen_new_label();
da91a00f 6138 tcg_gen_trunc_tl_i32(cpu_crf[0], cpu_so);
74d37793
AJ
6139 tcg_gen_brcondi_tl(TCG_COND_EQ, cpu_gpr[rD(ctx->opcode)], -1, l1);
6140 tcg_gen_ori_i32(cpu_crf[0], cpu_crf[0], 0x02);
6141 gen_set_label(l1);
6142 }
76a66253 6143#endif
79aceca5
FB
6144}
6145
76a66253 6146/* tlbwe */
e8eaa2c0 6147static void gen_tlbwe_40x(DisasContext *ctx)
79aceca5 6148{
76a66253 6149#if defined(CONFIG_USER_ONLY)
e06fcd75 6150 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
76a66253 6151#else
76db3ba4 6152 if (unlikely(!ctx->mem_idx)) {
e06fcd75 6153 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
76a66253
JM
6154 return;
6155 }
6156 switch (rB(ctx->opcode)) {
6157 case 0:
c6c7cf05
BS
6158 gen_helper_4xx_tlbwe_hi(cpu_env, cpu_gpr[rA(ctx->opcode)],
6159 cpu_gpr[rS(ctx->opcode)]);
76a66253
JM
6160 break;
6161 case 1:
c6c7cf05
BS
6162 gen_helper_4xx_tlbwe_lo(cpu_env, cpu_gpr[rA(ctx->opcode)],
6163 cpu_gpr[rS(ctx->opcode)]);
76a66253
JM
6164 break;
6165 default:
e06fcd75 6166 gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL);
76a66253 6167 break;
9a64fbe4 6168 }
76a66253
JM
6169#endif
6170}
6171
a4bb6c3e 6172/* TLB management - PowerPC 440 implementation */
e8eaa2c0 6173
54623277 6174/* tlbre */
e8eaa2c0 6175static void gen_tlbre_440(DisasContext *ctx)
5eb7995e
JM
6176{
6177#if defined(CONFIG_USER_ONLY)
e06fcd75 6178 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
5eb7995e 6179#else
76db3ba4 6180 if (unlikely(!ctx->mem_idx)) {
e06fcd75 6181 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
5eb7995e
JM
6182 return;
6183 }
6184 switch (rB(ctx->opcode)) {
6185 case 0:
5eb7995e 6186 case 1:
5eb7995e 6187 case 2:
74d37793
AJ
6188 {
6189 TCGv_i32 t0 = tcg_const_i32(rB(ctx->opcode));
c6c7cf05
BS
6190 gen_helper_440_tlbre(cpu_gpr[rD(ctx->opcode)], cpu_env,
6191 t0, cpu_gpr[rA(ctx->opcode)]);
74d37793
AJ
6192 tcg_temp_free_i32(t0);
6193 }
5eb7995e
JM
6194 break;
6195 default:
e06fcd75 6196 gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL);
5eb7995e
JM
6197 break;
6198 }
6199#endif
6200}
6201
6202/* tlbsx - tlbsx. */
e8eaa2c0 6203static void gen_tlbsx_440(DisasContext *ctx)
5eb7995e
JM
6204{
6205#if defined(CONFIG_USER_ONLY)
e06fcd75 6206 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
5eb7995e 6207#else
74d37793 6208 TCGv t0;
76db3ba4 6209 if (unlikely(!ctx->mem_idx)) {
e06fcd75 6210 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
5eb7995e
JM
6211 return;
6212 }
74d37793 6213 t0 = tcg_temp_new();
76db3ba4 6214 gen_addr_reg_index(ctx, t0);
c6c7cf05 6215 gen_helper_440_tlbsx(cpu_gpr[rD(ctx->opcode)], cpu_env, t0);
74d37793
AJ
6216 tcg_temp_free(t0);
6217 if (Rc(ctx->opcode)) {
6218 int l1 = gen_new_label();
da91a00f 6219 tcg_gen_trunc_tl_i32(cpu_crf[0], cpu_so);
74d37793
AJ
6220 tcg_gen_brcondi_tl(TCG_COND_EQ, cpu_gpr[rD(ctx->opcode)], -1, l1);
6221 tcg_gen_ori_i32(cpu_crf[0], cpu_crf[0], 0x02);
6222 gen_set_label(l1);
6223 }
5eb7995e
JM
6224#endif
6225}
6226
6227/* tlbwe */
e8eaa2c0 6228static void gen_tlbwe_440(DisasContext *ctx)
5eb7995e
JM
6229{
6230#if defined(CONFIG_USER_ONLY)
e06fcd75 6231 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
5eb7995e 6232#else
76db3ba4 6233 if (unlikely(!ctx->mem_idx)) {
e06fcd75 6234 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
5eb7995e
JM
6235 return;
6236 }
6237 switch (rB(ctx->opcode)) {
6238 case 0:
5eb7995e 6239 case 1:
5eb7995e 6240 case 2:
74d37793
AJ
6241 {
6242 TCGv_i32 t0 = tcg_const_i32(rB(ctx->opcode));
c6c7cf05
BS
6243 gen_helper_440_tlbwe(cpu_env, t0, cpu_gpr[rA(ctx->opcode)],
6244 cpu_gpr[rS(ctx->opcode)]);
74d37793
AJ
6245 tcg_temp_free_i32(t0);
6246 }
5eb7995e
JM
6247 break;
6248 default:
e06fcd75 6249 gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL);
5eb7995e
JM
6250 break;
6251 }
6252#endif
6253}
6254
01662f3e
AG
6255/* TLB management - PowerPC BookE 2.06 implementation */
6256
6257/* tlbre */
6258static void gen_tlbre_booke206(DisasContext *ctx)
6259{
6260#if defined(CONFIG_USER_ONLY)
6261 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
6262#else
6263 if (unlikely(!ctx->mem_idx)) {
6264 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
6265 return;
6266 }
6267
c6c7cf05 6268 gen_helper_booke206_tlbre(cpu_env);
01662f3e
AG
6269#endif
6270}
6271
6272/* tlbsx - tlbsx. */
6273static void gen_tlbsx_booke206(DisasContext *ctx)
6274{
6275#if defined(CONFIG_USER_ONLY)
6276 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
6277#else
6278 TCGv t0;
6279 if (unlikely(!ctx->mem_idx)) {
6280 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
6281 return;
6282 }
6283
6284 if (rA(ctx->opcode)) {
6285 t0 = tcg_temp_new();
6286 tcg_gen_mov_tl(t0, cpu_gpr[rD(ctx->opcode)]);
6287 } else {
6288 t0 = tcg_const_tl(0);
6289 }
6290
6291 tcg_gen_add_tl(t0, t0, cpu_gpr[rB(ctx->opcode)]);
c6c7cf05 6292 gen_helper_booke206_tlbsx(cpu_env, t0);
01662f3e
AG
6293#endif
6294}
6295
6296/* tlbwe */
6297static void gen_tlbwe_booke206(DisasContext *ctx)
6298{
6299#if defined(CONFIG_USER_ONLY)
6300 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
6301#else
6302 if (unlikely(!ctx->mem_idx)) {
6303 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
6304 return;
6305 }
3f162d11 6306 gen_update_nip(ctx, ctx->nip - 4);
c6c7cf05 6307 gen_helper_booke206_tlbwe(cpu_env);
01662f3e
AG
6308#endif
6309}
6310
6311static void gen_tlbivax_booke206(DisasContext *ctx)
6312{
6313#if defined(CONFIG_USER_ONLY)
6314 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
6315#else
6316 TCGv t0;
6317 if (unlikely(!ctx->mem_idx)) {
6318 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
6319 return;
6320 }
6321
6322 t0 = tcg_temp_new();
6323 gen_addr_reg_index(ctx, t0);
6324
c6c7cf05 6325 gen_helper_booke206_tlbivax(cpu_env, t0);
01662f3e
AG
6326#endif
6327}
6328
6d3db821
AG
6329static void gen_tlbilx_booke206(DisasContext *ctx)
6330{
6331#if defined(CONFIG_USER_ONLY)
6332 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
6333#else
6334 TCGv t0;
6335 if (unlikely(!ctx->mem_idx)) {
6336 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
6337 return;
6338 }
6339
6340 t0 = tcg_temp_new();
6341 gen_addr_reg_index(ctx, t0);
6342
6343 switch((ctx->opcode >> 21) & 0x3) {
6344 case 0:
c6c7cf05 6345 gen_helper_booke206_tlbilx0(cpu_env, t0);
6d3db821
AG
6346 break;
6347 case 1:
c6c7cf05 6348 gen_helper_booke206_tlbilx1(cpu_env, t0);
6d3db821
AG
6349 break;
6350 case 3:
c6c7cf05 6351 gen_helper_booke206_tlbilx3(cpu_env, t0);
6d3db821
AG
6352 break;
6353 default:
6354 gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL);
6355 break;
6356 }
6357
6358 tcg_temp_free(t0);
6359#endif
6360}
6361
01662f3e 6362
76a66253 6363/* wrtee */
99e300ef 6364static void gen_wrtee(DisasContext *ctx)
76a66253
JM
6365{
6366#if defined(CONFIG_USER_ONLY)
e06fcd75 6367 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
76a66253 6368#else
6527f6ea 6369 TCGv t0;
76db3ba4 6370 if (unlikely(!ctx->mem_idx)) {
e06fcd75 6371 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
76a66253
JM
6372 return;
6373 }
6527f6ea
AJ
6374 t0 = tcg_temp_new();
6375 tcg_gen_andi_tl(t0, cpu_gpr[rD(ctx->opcode)], (1 << MSR_EE));
6376 tcg_gen_andi_tl(cpu_msr, cpu_msr, ~(1 << MSR_EE));
6377 tcg_gen_or_tl(cpu_msr, cpu_msr, t0);
6378 tcg_temp_free(t0);
dee96f6c
JM
6379 /* Stop translation to have a chance to raise an exception
6380 * if we just set msr_ee to 1
6381 */
e06fcd75 6382 gen_stop_exception(ctx);
76a66253
JM
6383#endif
6384}
6385
6386/* wrteei */
99e300ef 6387static void gen_wrteei(DisasContext *ctx)
76a66253
JM
6388{
6389#if defined(CONFIG_USER_ONLY)
e06fcd75 6390 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
76a66253 6391#else
76db3ba4 6392 if (unlikely(!ctx->mem_idx)) {
e06fcd75 6393 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
76a66253
JM
6394 return;
6395 }
fbe73008 6396 if (ctx->opcode & 0x00008000) {
6527f6ea
AJ
6397 tcg_gen_ori_tl(cpu_msr, cpu_msr, (1 << MSR_EE));
6398 /* Stop translation to have a chance to raise an exception */
e06fcd75 6399 gen_stop_exception(ctx);
6527f6ea 6400 } else {
1b6e5f99 6401 tcg_gen_andi_tl(cpu_msr, cpu_msr, ~(1 << MSR_EE));
6527f6ea 6402 }
76a66253
JM
6403#endif
6404}
6405
08e46e54 6406/* PowerPC 440 specific instructions */
99e300ef 6407
54623277 6408/* dlmzb */
99e300ef 6409static void gen_dlmzb(DisasContext *ctx)
76a66253 6410{
ef0d51af 6411 TCGv_i32 t0 = tcg_const_i32(Rc(ctx->opcode));
d15f74fb
BS
6412 gen_helper_dlmzb(cpu_gpr[rA(ctx->opcode)], cpu_env,
6413 cpu_gpr[rS(ctx->opcode)], cpu_gpr[rB(ctx->opcode)], t0);
ef0d51af 6414 tcg_temp_free_i32(t0);
76a66253
JM
6415}
6416
6417/* mbar replaces eieio on 440 */
99e300ef 6418static void gen_mbar(DisasContext *ctx)
76a66253
JM
6419{
6420 /* interpreted as no-op */
6421}
6422
6423/* msync replaces sync on 440 */
dcb2b9e1 6424static void gen_msync_4xx(DisasContext *ctx)
76a66253
JM
6425{
6426 /* interpreted as no-op */
6427}
6428
6429/* icbt */
e8eaa2c0 6430static void gen_icbt_440(DisasContext *ctx)
76a66253
JM
6431{
6432 /* interpreted as no-op */
6433 /* XXX: specification say this is treated as a load by the MMU
6434 * but does not generate any exception
6435 */
79aceca5
FB
6436}
6437
9e0b5cb1
AG
6438/* Embedded.Processor Control */
6439
6440static void gen_msgclr(DisasContext *ctx)
6441{
6442#if defined(CONFIG_USER_ONLY)
6443 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
6444#else
6445 if (unlikely(ctx->mem_idx == 0)) {
6446 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
6447 return;
6448 }
6449
e5f17ac6 6450 gen_helper_msgclr(cpu_env, cpu_gpr[rB(ctx->opcode)]);
9e0b5cb1
AG
6451#endif
6452}
6453
d5d11a39
AG
6454static void gen_msgsnd(DisasContext *ctx)
6455{
6456#if defined(CONFIG_USER_ONLY)
6457 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
6458#else
6459 if (unlikely(ctx->mem_idx == 0)) {
6460 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
6461 return;
6462 }
6463
6464 gen_helper_msgsnd(cpu_gpr[rB(ctx->opcode)]);
6465#endif
6466}
6467
a9d9eb8f
JM
6468/*** Altivec vector extension ***/
6469/* Altivec registers moves */
a9d9eb8f 6470
636aa200 6471static inline TCGv_ptr gen_avr_ptr(int reg)
564e571a 6472{
e4704b3b 6473 TCGv_ptr r = tcg_temp_new_ptr();
564e571a
AJ
6474 tcg_gen_addi_ptr(r, cpu_env, offsetof(CPUPPCState, avr[reg]));
6475 return r;
6476}
6477
a9d9eb8f 6478#define GEN_VR_LDX(name, opc2, opc3) \
99e300ef 6479static void glue(gen_, name)(DisasContext *ctx) \
a9d9eb8f 6480{ \
fe1e5c53 6481 TCGv EA; \
a9d9eb8f 6482 if (unlikely(!ctx->altivec_enabled)) { \
e06fcd75 6483 gen_exception(ctx, POWERPC_EXCP_VPU); \
a9d9eb8f
JM
6484 return; \
6485 } \
76db3ba4 6486 gen_set_access_type(ctx, ACCESS_INT); \
fe1e5c53 6487 EA = tcg_temp_new(); \
76db3ba4 6488 gen_addr_reg_index(ctx, EA); \
fe1e5c53 6489 tcg_gen_andi_tl(EA, EA, ~0xf); \
76db3ba4
AJ
6490 if (ctx->le_mode) { \
6491 gen_qemu_ld64(ctx, cpu_avrl[rD(ctx->opcode)], EA); \
fe1e5c53 6492 tcg_gen_addi_tl(EA, EA, 8); \
76db3ba4 6493 gen_qemu_ld64(ctx, cpu_avrh[rD(ctx->opcode)], EA); \
fe1e5c53 6494 } else { \
76db3ba4 6495 gen_qemu_ld64(ctx, cpu_avrh[rD(ctx->opcode)], EA); \
fe1e5c53 6496 tcg_gen_addi_tl(EA, EA, 8); \
76db3ba4 6497 gen_qemu_ld64(ctx, cpu_avrl[rD(ctx->opcode)], EA); \
fe1e5c53
AJ
6498 } \
6499 tcg_temp_free(EA); \
a9d9eb8f
JM
6500}
6501
6502#define GEN_VR_STX(name, opc2, opc3) \
99e300ef 6503static void gen_st##name(DisasContext *ctx) \
a9d9eb8f 6504{ \
fe1e5c53 6505 TCGv EA; \
a9d9eb8f 6506 if (unlikely(!ctx->altivec_enabled)) { \
e06fcd75 6507 gen_exception(ctx, POWERPC_EXCP_VPU); \
a9d9eb8f
JM
6508 return; \
6509 } \
76db3ba4 6510 gen_set_access_type(ctx, ACCESS_INT); \
fe1e5c53 6511 EA = tcg_temp_new(); \
76db3ba4 6512 gen_addr_reg_index(ctx, EA); \
fe1e5c53 6513 tcg_gen_andi_tl(EA, EA, ~0xf); \
76db3ba4
AJ
6514 if (ctx->le_mode) { \
6515 gen_qemu_st64(ctx, cpu_avrl[rD(ctx->opcode)], EA); \
fe1e5c53 6516 tcg_gen_addi_tl(EA, EA, 8); \
76db3ba4 6517 gen_qemu_st64(ctx, cpu_avrh[rD(ctx->opcode)], EA); \
fe1e5c53 6518 } else { \
76db3ba4 6519 gen_qemu_st64(ctx, cpu_avrh[rD(ctx->opcode)], EA); \
fe1e5c53 6520 tcg_gen_addi_tl(EA, EA, 8); \
76db3ba4 6521 gen_qemu_st64(ctx, cpu_avrl[rD(ctx->opcode)], EA); \
fe1e5c53
AJ
6522 } \
6523 tcg_temp_free(EA); \
a9d9eb8f
JM
6524}
6525
cbfb6ae9 6526#define GEN_VR_LVE(name, opc2, opc3) \
99e300ef 6527static void gen_lve##name(DisasContext *ctx) \
cbfb6ae9
AJ
6528 { \
6529 TCGv EA; \
6530 TCGv_ptr rs; \
6531 if (unlikely(!ctx->altivec_enabled)) { \
6532 gen_exception(ctx, POWERPC_EXCP_VPU); \
6533 return; \
6534 } \
6535 gen_set_access_type(ctx, ACCESS_INT); \
6536 EA = tcg_temp_new(); \
6537 gen_addr_reg_index(ctx, EA); \
6538 rs = gen_avr_ptr(rS(ctx->opcode)); \
2f5a189c 6539 gen_helper_lve##name(cpu_env, rs, EA); \
cbfb6ae9
AJ
6540 tcg_temp_free(EA); \
6541 tcg_temp_free_ptr(rs); \
6542 }
6543
6544#define GEN_VR_STVE(name, opc2, opc3) \
99e300ef 6545static void gen_stve##name(DisasContext *ctx) \
cbfb6ae9
AJ
6546 { \
6547 TCGv EA; \
6548 TCGv_ptr rs; \
6549 if (unlikely(!ctx->altivec_enabled)) { \
6550 gen_exception(ctx, POWERPC_EXCP_VPU); \
6551 return; \
6552 } \
6553 gen_set_access_type(ctx, ACCESS_INT); \
6554 EA = tcg_temp_new(); \
6555 gen_addr_reg_index(ctx, EA); \
6556 rs = gen_avr_ptr(rS(ctx->opcode)); \
2f5a189c 6557 gen_helper_stve##name(cpu_env, rs, EA); \
cbfb6ae9
AJ
6558 tcg_temp_free(EA); \
6559 tcg_temp_free_ptr(rs); \
6560 }
6561
fe1e5c53 6562GEN_VR_LDX(lvx, 0x07, 0x03);
a9d9eb8f 6563/* As we don't emulate the cache, lvxl is stricly equivalent to lvx */
fe1e5c53 6564GEN_VR_LDX(lvxl, 0x07, 0x0B);
a9d9eb8f 6565
cbfb6ae9
AJ
6566GEN_VR_LVE(bx, 0x07, 0x00);
6567GEN_VR_LVE(hx, 0x07, 0x01);
6568GEN_VR_LVE(wx, 0x07, 0x02);
6569
fe1e5c53 6570GEN_VR_STX(svx, 0x07, 0x07);
a9d9eb8f 6571/* As we don't emulate the cache, stvxl is stricly equivalent to stvx */
fe1e5c53 6572GEN_VR_STX(svxl, 0x07, 0x0F);
a9d9eb8f 6573
cbfb6ae9
AJ
6574GEN_VR_STVE(bx, 0x07, 0x04);
6575GEN_VR_STVE(hx, 0x07, 0x05);
6576GEN_VR_STVE(wx, 0x07, 0x06);
6577
99e300ef 6578static void gen_lvsl(DisasContext *ctx)
bf8d8ded
AJ
6579{
6580 TCGv_ptr rd;
6581 TCGv EA;
6582 if (unlikely(!ctx->altivec_enabled)) {
6583 gen_exception(ctx, POWERPC_EXCP_VPU);
6584 return;
6585 }
6586 EA = tcg_temp_new();
6587 gen_addr_reg_index(ctx, EA);
6588 rd = gen_avr_ptr(rD(ctx->opcode));
6589 gen_helper_lvsl(rd, EA);
6590 tcg_temp_free(EA);
6591 tcg_temp_free_ptr(rd);
6592}
6593
99e300ef 6594static void gen_lvsr(DisasContext *ctx)
bf8d8ded
AJ
6595{
6596 TCGv_ptr rd;
6597 TCGv EA;
6598 if (unlikely(!ctx->altivec_enabled)) {
6599 gen_exception(ctx, POWERPC_EXCP_VPU);
6600 return;
6601 }
6602 EA = tcg_temp_new();
6603 gen_addr_reg_index(ctx, EA);
6604 rd = gen_avr_ptr(rD(ctx->opcode));
6605 gen_helper_lvsr(rd, EA);
6606 tcg_temp_free(EA);
6607 tcg_temp_free_ptr(rd);
6608}
6609
99e300ef 6610static void gen_mfvscr(DisasContext *ctx)
785f451b
AJ
6611{
6612 TCGv_i32 t;
6613 if (unlikely(!ctx->altivec_enabled)) {
6614 gen_exception(ctx, POWERPC_EXCP_VPU);
6615 return;
6616 }
6617 tcg_gen_movi_i64(cpu_avrh[rD(ctx->opcode)], 0);
6618 t = tcg_temp_new_i32();
1328c2bf 6619 tcg_gen_ld_i32(t, cpu_env, offsetof(CPUPPCState, vscr));
785f451b 6620 tcg_gen_extu_i32_i64(cpu_avrl[rD(ctx->opcode)], t);
fce5ecb7 6621 tcg_temp_free_i32(t);
785f451b
AJ
6622}
6623
99e300ef 6624static void gen_mtvscr(DisasContext *ctx)
785f451b 6625{
6e87b7c7 6626 TCGv_ptr p;
785f451b
AJ
6627 if (unlikely(!ctx->altivec_enabled)) {
6628 gen_exception(ctx, POWERPC_EXCP_VPU);
6629 return;
6630 }
6e87b7c7 6631 p = gen_avr_ptr(rD(ctx->opcode));
d15f74fb 6632 gen_helper_mtvscr(cpu_env, p);
6e87b7c7 6633 tcg_temp_free_ptr(p);
785f451b
AJ
6634}
6635
7a9b96cf
AJ
6636/* Logical operations */
6637#define GEN_VX_LOGICAL(name, tcg_op, opc2, opc3) \
99e300ef 6638static void glue(gen_, name)(DisasContext *ctx) \
7a9b96cf
AJ
6639{ \
6640 if (unlikely(!ctx->altivec_enabled)) { \
6641 gen_exception(ctx, POWERPC_EXCP_VPU); \
6642 return; \
6643 } \
6644 tcg_op(cpu_avrh[rD(ctx->opcode)], cpu_avrh[rA(ctx->opcode)], cpu_avrh[rB(ctx->opcode)]); \
6645 tcg_op(cpu_avrl[rD(ctx->opcode)], cpu_avrl[rA(ctx->opcode)], cpu_avrl[rB(ctx->opcode)]); \
6646}
6647
6648GEN_VX_LOGICAL(vand, tcg_gen_and_i64, 2, 16);
6649GEN_VX_LOGICAL(vandc, tcg_gen_andc_i64, 2, 17);
6650GEN_VX_LOGICAL(vor, tcg_gen_or_i64, 2, 18);
6651GEN_VX_LOGICAL(vxor, tcg_gen_xor_i64, 2, 19);
6652GEN_VX_LOGICAL(vnor, tcg_gen_nor_i64, 2, 20);
6653
8e27dd6f 6654#define GEN_VXFORM(name, opc2, opc3) \
99e300ef 6655static void glue(gen_, name)(DisasContext *ctx) \
8e27dd6f
AJ
6656{ \
6657 TCGv_ptr ra, rb, rd; \
6658 if (unlikely(!ctx->altivec_enabled)) { \
6659 gen_exception(ctx, POWERPC_EXCP_VPU); \
6660 return; \
6661 } \
6662 ra = gen_avr_ptr(rA(ctx->opcode)); \
6663 rb = gen_avr_ptr(rB(ctx->opcode)); \
6664 rd = gen_avr_ptr(rD(ctx->opcode)); \
6665 gen_helper_##name (rd, ra, rb); \
6666 tcg_temp_free_ptr(ra); \
6667 tcg_temp_free_ptr(rb); \
6668 tcg_temp_free_ptr(rd); \
6669}
6670
d15f74fb
BS
6671#define GEN_VXFORM_ENV(name, opc2, opc3) \
6672static void glue(gen_, name)(DisasContext *ctx) \
6673{ \
6674 TCGv_ptr ra, rb, rd; \
6675 if (unlikely(!ctx->altivec_enabled)) { \
6676 gen_exception(ctx, POWERPC_EXCP_VPU); \
6677 return; \
6678 } \
6679 ra = gen_avr_ptr(rA(ctx->opcode)); \
6680 rb = gen_avr_ptr(rB(ctx->opcode)); \
6681 rd = gen_avr_ptr(rD(ctx->opcode)); \
54cddd21 6682 gen_helper_##name(cpu_env, rd, ra, rb); \
d15f74fb
BS
6683 tcg_temp_free_ptr(ra); \
6684 tcg_temp_free_ptr(rb); \
6685 tcg_temp_free_ptr(rd); \
6686}
6687
7872c51c
AJ
6688GEN_VXFORM(vaddubm, 0, 0);
6689GEN_VXFORM(vadduhm, 0, 1);
6690GEN_VXFORM(vadduwm, 0, 2);
6691GEN_VXFORM(vsububm, 0, 16);
6692GEN_VXFORM(vsubuhm, 0, 17);
6693GEN_VXFORM(vsubuwm, 0, 18);
e4039339
AJ
6694GEN_VXFORM(vmaxub, 1, 0);
6695GEN_VXFORM(vmaxuh, 1, 1);
6696GEN_VXFORM(vmaxuw, 1, 2);
6697GEN_VXFORM(vmaxsb, 1, 4);
6698GEN_VXFORM(vmaxsh, 1, 5);
6699GEN_VXFORM(vmaxsw, 1, 6);
6700GEN_VXFORM(vminub, 1, 8);
6701GEN_VXFORM(vminuh, 1, 9);
6702GEN_VXFORM(vminuw, 1, 10);
6703GEN_VXFORM(vminsb, 1, 12);
6704GEN_VXFORM(vminsh, 1, 13);
6705GEN_VXFORM(vminsw, 1, 14);
fab3cbe9
AJ
6706GEN_VXFORM(vavgub, 1, 16);
6707GEN_VXFORM(vavguh, 1, 17);
6708GEN_VXFORM(vavguw, 1, 18);
6709GEN_VXFORM(vavgsb, 1, 20);
6710GEN_VXFORM(vavgsh, 1, 21);
6711GEN_VXFORM(vavgsw, 1, 22);
3b430048
AJ
6712GEN_VXFORM(vmrghb, 6, 0);
6713GEN_VXFORM(vmrghh, 6, 1);
6714GEN_VXFORM(vmrghw, 6, 2);
6715GEN_VXFORM(vmrglb, 6, 4);
6716GEN_VXFORM(vmrglh, 6, 5);
6717GEN_VXFORM(vmrglw, 6, 6);
2c277908
AJ
6718GEN_VXFORM(vmuloub, 4, 0);
6719GEN_VXFORM(vmulouh, 4, 1);
6720GEN_VXFORM(vmulosb, 4, 4);
6721GEN_VXFORM(vmulosh, 4, 5);
6722GEN_VXFORM(vmuleub, 4, 8);
6723GEN_VXFORM(vmuleuh, 4, 9);
6724GEN_VXFORM(vmulesb, 4, 12);
6725GEN_VXFORM(vmulesh, 4, 13);
d79f0809
AJ
6726GEN_VXFORM(vslb, 2, 4);
6727GEN_VXFORM(vslh, 2, 5);
6728GEN_VXFORM(vslw, 2, 6);
07ef34c3
AJ
6729GEN_VXFORM(vsrb, 2, 8);
6730GEN_VXFORM(vsrh, 2, 9);
6731GEN_VXFORM(vsrw, 2, 10);
6732GEN_VXFORM(vsrab, 2, 12);
6733GEN_VXFORM(vsrah, 2, 13);
6734GEN_VXFORM(vsraw, 2, 14);
7b239bec
AJ
6735GEN_VXFORM(vslo, 6, 16);
6736GEN_VXFORM(vsro, 6, 17);
e343da72
AJ
6737GEN_VXFORM(vaddcuw, 0, 6);
6738GEN_VXFORM(vsubcuw, 0, 22);
d15f74fb
BS
6739GEN_VXFORM_ENV(vaddubs, 0, 8);
6740GEN_VXFORM_ENV(vadduhs, 0, 9);
6741GEN_VXFORM_ENV(vadduws, 0, 10);
6742GEN_VXFORM_ENV(vaddsbs, 0, 12);
6743GEN_VXFORM_ENV(vaddshs, 0, 13);
6744GEN_VXFORM_ENV(vaddsws, 0, 14);
6745GEN_VXFORM_ENV(vsububs, 0, 24);
6746GEN_VXFORM_ENV(vsubuhs, 0, 25);
6747GEN_VXFORM_ENV(vsubuws, 0, 26);
6748GEN_VXFORM_ENV(vsubsbs, 0, 28);
6749GEN_VXFORM_ENV(vsubshs, 0, 29);
6750GEN_VXFORM_ENV(vsubsws, 0, 30);
5e1d0985
AJ
6751GEN_VXFORM(vrlb, 2, 0);
6752GEN_VXFORM(vrlh, 2, 1);
6753GEN_VXFORM(vrlw, 2, 2);
d9430add
AJ
6754GEN_VXFORM(vsl, 2, 7);
6755GEN_VXFORM(vsr, 2, 11);
d15f74fb
BS
6756GEN_VXFORM_ENV(vpkuhum, 7, 0);
6757GEN_VXFORM_ENV(vpkuwum, 7, 1);
6758GEN_VXFORM_ENV(vpkuhus, 7, 2);
6759GEN_VXFORM_ENV(vpkuwus, 7, 3);
6760GEN_VXFORM_ENV(vpkshus, 7, 4);
6761GEN_VXFORM_ENV(vpkswus, 7, 5);
6762GEN_VXFORM_ENV(vpkshss, 7, 6);
6763GEN_VXFORM_ENV(vpkswss, 7, 7);
1dd9ffb9 6764GEN_VXFORM(vpkpx, 7, 12);
d15f74fb
BS
6765GEN_VXFORM_ENV(vsum4ubs, 4, 24);
6766GEN_VXFORM_ENV(vsum4sbs, 4, 28);
6767GEN_VXFORM_ENV(vsum4shs, 4, 25);
6768GEN_VXFORM_ENV(vsum2sws, 4, 26);
6769GEN_VXFORM_ENV(vsumsws, 4, 30);
6770GEN_VXFORM_ENV(vaddfp, 5, 0);
6771GEN_VXFORM_ENV(vsubfp, 5, 1);
6772GEN_VXFORM_ENV(vmaxfp, 5, 16);
6773GEN_VXFORM_ENV(vminfp, 5, 17);
fab3cbe9 6774
0cbcd906 6775#define GEN_VXRFORM1(opname, name, str, opc2, opc3) \
e8eaa2c0 6776static void glue(gen_, name)(DisasContext *ctx) \
0cbcd906
AJ
6777 { \
6778 TCGv_ptr ra, rb, rd; \
6779 if (unlikely(!ctx->altivec_enabled)) { \
6780 gen_exception(ctx, POWERPC_EXCP_VPU); \
6781 return; \
6782 } \
6783 ra = gen_avr_ptr(rA(ctx->opcode)); \
6784 rb = gen_avr_ptr(rB(ctx->opcode)); \
6785 rd = gen_avr_ptr(rD(ctx->opcode)); \
d15f74fb 6786 gen_helper_##opname(cpu_env, rd, ra, rb); \
0cbcd906
AJ
6787 tcg_temp_free_ptr(ra); \
6788 tcg_temp_free_ptr(rb); \
6789 tcg_temp_free_ptr(rd); \
6790 }
6791
6792#define GEN_VXRFORM(name, opc2, opc3) \
6793 GEN_VXRFORM1(name, name, #name, opc2, opc3) \
6794 GEN_VXRFORM1(name##_dot, name##_, #name ".", opc2, (opc3 | (0x1 << 4)))
6795
1add6e23
AJ
6796GEN_VXRFORM(vcmpequb, 3, 0)
6797GEN_VXRFORM(vcmpequh, 3, 1)
6798GEN_VXRFORM(vcmpequw, 3, 2)
6799GEN_VXRFORM(vcmpgtsb, 3, 12)
6800GEN_VXRFORM(vcmpgtsh, 3, 13)
6801GEN_VXRFORM(vcmpgtsw, 3, 14)
6802GEN_VXRFORM(vcmpgtub, 3, 8)
6803GEN_VXRFORM(vcmpgtuh, 3, 9)
6804GEN_VXRFORM(vcmpgtuw, 3, 10)
819ca121
AJ
6805GEN_VXRFORM(vcmpeqfp, 3, 3)
6806GEN_VXRFORM(vcmpgefp, 3, 7)
6807GEN_VXRFORM(vcmpgtfp, 3, 11)
6808GEN_VXRFORM(vcmpbfp, 3, 15)
1add6e23 6809
c026766b 6810#define GEN_VXFORM_SIMM(name, opc2, opc3) \
99e300ef 6811static void glue(gen_, name)(DisasContext *ctx) \
c026766b
AJ
6812 { \
6813 TCGv_ptr rd; \
6814 TCGv_i32 simm; \
6815 if (unlikely(!ctx->altivec_enabled)) { \
6816 gen_exception(ctx, POWERPC_EXCP_VPU); \
6817 return; \
6818 } \
6819 simm = tcg_const_i32(SIMM5(ctx->opcode)); \
6820 rd = gen_avr_ptr(rD(ctx->opcode)); \
6821 gen_helper_##name (rd, simm); \
6822 tcg_temp_free_i32(simm); \
6823 tcg_temp_free_ptr(rd); \
6824 }
6825
6826GEN_VXFORM_SIMM(vspltisb, 6, 12);
6827GEN_VXFORM_SIMM(vspltish, 6, 13);
6828GEN_VXFORM_SIMM(vspltisw, 6, 14);
6829
de5f2484 6830#define GEN_VXFORM_NOA(name, opc2, opc3) \
99e300ef 6831static void glue(gen_, name)(DisasContext *ctx) \
de5f2484
AJ
6832 { \
6833 TCGv_ptr rb, rd; \
6834 if (unlikely(!ctx->altivec_enabled)) { \
6835 gen_exception(ctx, POWERPC_EXCP_VPU); \
6836 return; \
6837 } \
6838 rb = gen_avr_ptr(rB(ctx->opcode)); \
6839 rd = gen_avr_ptr(rD(ctx->opcode)); \
6840 gen_helper_##name (rd, rb); \
6841 tcg_temp_free_ptr(rb); \
6842 tcg_temp_free_ptr(rd); \
6843 }
6844
d15f74fb
BS
6845#define GEN_VXFORM_NOA_ENV(name, opc2, opc3) \
6846static void glue(gen_, name)(DisasContext *ctx) \
6847 { \
6848 TCGv_ptr rb, rd; \
6849 \
6850 if (unlikely(!ctx->altivec_enabled)) { \
6851 gen_exception(ctx, POWERPC_EXCP_VPU); \
6852 return; \
6853 } \
6854 rb = gen_avr_ptr(rB(ctx->opcode)); \
6855 rd = gen_avr_ptr(rD(ctx->opcode)); \
6856 gen_helper_##name(cpu_env, rd, rb); \
6857 tcg_temp_free_ptr(rb); \
6858 tcg_temp_free_ptr(rd); \
6859 }
6860
6cf1c6e5
AJ
6861GEN_VXFORM_NOA(vupkhsb, 7, 8);
6862GEN_VXFORM_NOA(vupkhsh, 7, 9);
6863GEN_VXFORM_NOA(vupklsb, 7, 10);
6864GEN_VXFORM_NOA(vupklsh, 7, 11);
79f85c3a
AJ
6865GEN_VXFORM_NOA(vupkhpx, 7, 13);
6866GEN_VXFORM_NOA(vupklpx, 7, 15);
d15f74fb
BS
6867GEN_VXFORM_NOA_ENV(vrefp, 5, 4);
6868GEN_VXFORM_NOA_ENV(vrsqrtefp, 5, 5);
6869GEN_VXFORM_NOA_ENV(vexptefp, 5, 6);
6870GEN_VXFORM_NOA_ENV(vlogefp, 5, 7);
6871GEN_VXFORM_NOA_ENV(vrfim, 5, 8);
6872GEN_VXFORM_NOA_ENV(vrfin, 5, 9);
6873GEN_VXFORM_NOA_ENV(vrfip, 5, 10);
6874GEN_VXFORM_NOA_ENV(vrfiz, 5, 11);
79f85c3a 6875
21d21583 6876#define GEN_VXFORM_SIMM(name, opc2, opc3) \
99e300ef 6877static void glue(gen_, name)(DisasContext *ctx) \
21d21583
AJ
6878 { \
6879 TCGv_ptr rd; \
6880 TCGv_i32 simm; \
6881 if (unlikely(!ctx->altivec_enabled)) { \
6882 gen_exception(ctx, POWERPC_EXCP_VPU); \
6883 return; \
6884 } \
6885 simm = tcg_const_i32(SIMM5(ctx->opcode)); \
6886 rd = gen_avr_ptr(rD(ctx->opcode)); \
6887 gen_helper_##name (rd, simm); \
6888 tcg_temp_free_i32(simm); \
6889 tcg_temp_free_ptr(rd); \
6890 }
6891
27a4edb3 6892#define GEN_VXFORM_UIMM(name, opc2, opc3) \
99e300ef 6893static void glue(gen_, name)(DisasContext *ctx) \
27a4edb3
AJ
6894 { \
6895 TCGv_ptr rb, rd; \
6896 TCGv_i32 uimm; \
6897 if (unlikely(!ctx->altivec_enabled)) { \
6898 gen_exception(ctx, POWERPC_EXCP_VPU); \
6899 return; \
6900 } \
6901 uimm = tcg_const_i32(UIMM5(ctx->opcode)); \
6902 rb = gen_avr_ptr(rB(ctx->opcode)); \
6903 rd = gen_avr_ptr(rD(ctx->opcode)); \
6904 gen_helper_##name (rd, rb, uimm); \
6905 tcg_temp_free_i32(uimm); \
6906 tcg_temp_free_ptr(rb); \
6907 tcg_temp_free_ptr(rd); \
6908 }
6909
d15f74fb
BS
6910#define GEN_VXFORM_UIMM_ENV(name, opc2, opc3) \
6911static void glue(gen_, name)(DisasContext *ctx) \
6912 { \
6913 TCGv_ptr rb, rd; \
6914 TCGv_i32 uimm; \
6915 \
6916 if (unlikely(!ctx->altivec_enabled)) { \
6917 gen_exception(ctx, POWERPC_EXCP_VPU); \
6918 return; \
6919 } \
6920 uimm = tcg_const_i32(UIMM5(ctx->opcode)); \
6921 rb = gen_avr_ptr(rB(ctx->opcode)); \
6922 rd = gen_avr_ptr(rD(ctx->opcode)); \
6923 gen_helper_##name(cpu_env, rd, rb, uimm); \
6924 tcg_temp_free_i32(uimm); \
6925 tcg_temp_free_ptr(rb); \
6926 tcg_temp_free_ptr(rd); \
6927 }
6928
e4e6bee7
AJ
6929GEN_VXFORM_UIMM(vspltb, 6, 8);
6930GEN_VXFORM_UIMM(vsplth, 6, 9);
6931GEN_VXFORM_UIMM(vspltw, 6, 10);
d15f74fb
BS
6932GEN_VXFORM_UIMM_ENV(vcfux, 5, 12);
6933GEN_VXFORM_UIMM_ENV(vcfsx, 5, 13);
6934GEN_VXFORM_UIMM_ENV(vctuxs, 5, 14);
6935GEN_VXFORM_UIMM_ENV(vctsxs, 5, 15);
e4e6bee7 6936
99e300ef 6937static void gen_vsldoi(DisasContext *ctx)
cd633b10
AJ
6938{
6939 TCGv_ptr ra, rb, rd;
fce5ecb7 6940 TCGv_i32 sh;
cd633b10
AJ
6941 if (unlikely(!ctx->altivec_enabled)) {
6942 gen_exception(ctx, POWERPC_EXCP_VPU);
6943 return;
6944 }
6945 ra = gen_avr_ptr(rA(ctx->opcode));
6946 rb = gen_avr_ptr(rB(ctx->opcode));
6947 rd = gen_avr_ptr(rD(ctx->opcode));
6948 sh = tcg_const_i32(VSH(ctx->opcode));
6949 gen_helper_vsldoi (rd, ra, rb, sh);
6950 tcg_temp_free_ptr(ra);
6951 tcg_temp_free_ptr(rb);
6952 tcg_temp_free_ptr(rd);
fce5ecb7 6953 tcg_temp_free_i32(sh);
cd633b10
AJ
6954}
6955
707cec33 6956#define GEN_VAFORM_PAIRED(name0, name1, opc2) \
d15f74fb 6957static void glue(gen_, name0##_##name1)(DisasContext *ctx) \
707cec33
AJ
6958 { \
6959 TCGv_ptr ra, rb, rc, rd; \
6960 if (unlikely(!ctx->altivec_enabled)) { \
6961 gen_exception(ctx, POWERPC_EXCP_VPU); \
6962 return; \
6963 } \
6964 ra = gen_avr_ptr(rA(ctx->opcode)); \
6965 rb = gen_avr_ptr(rB(ctx->opcode)); \
6966 rc = gen_avr_ptr(rC(ctx->opcode)); \
6967 rd = gen_avr_ptr(rD(ctx->opcode)); \
6968 if (Rc(ctx->opcode)) { \
d15f74fb 6969 gen_helper_##name1(cpu_env, rd, ra, rb, rc); \
707cec33 6970 } else { \
d15f74fb 6971 gen_helper_##name0(cpu_env, rd, ra, rb, rc); \
707cec33
AJ
6972 } \
6973 tcg_temp_free_ptr(ra); \
6974 tcg_temp_free_ptr(rb); \
6975 tcg_temp_free_ptr(rc); \
6976 tcg_temp_free_ptr(rd); \
6977 }
6978
b161ae27
AJ
6979GEN_VAFORM_PAIRED(vmhaddshs, vmhraddshs, 16)
6980
99e300ef 6981static void gen_vmladduhm(DisasContext *ctx)
bcd2ee23
AJ
6982{
6983 TCGv_ptr ra, rb, rc, rd;
6984 if (unlikely(!ctx->altivec_enabled)) {
6985 gen_exception(ctx, POWERPC_EXCP_VPU);
6986 return;
6987 }
6988 ra = gen_avr_ptr(rA(ctx->opcode));
6989 rb = gen_avr_ptr(rB(ctx->opcode));
6990 rc = gen_avr_ptr(rC(ctx->opcode));
6991 rd = gen_avr_ptr(rD(ctx->opcode));
6992 gen_helper_vmladduhm(rd, ra, rb, rc);
6993 tcg_temp_free_ptr(ra);
6994 tcg_temp_free_ptr(rb);
6995 tcg_temp_free_ptr(rc);
6996 tcg_temp_free_ptr(rd);
6997}
6998
b04ae981 6999GEN_VAFORM_PAIRED(vmsumubm, vmsummbm, 18)
4d9903b6 7000GEN_VAFORM_PAIRED(vmsumuhm, vmsumuhs, 19)
eae07261 7001GEN_VAFORM_PAIRED(vmsumshm, vmsumshs, 20)
d1258698 7002GEN_VAFORM_PAIRED(vsel, vperm, 21)
35cf7c7e 7003GEN_VAFORM_PAIRED(vmaddfp, vnmsubfp, 23)
b04ae981 7004
472b24ce
TM
7005/*** VSX extension ***/
7006
7007static inline TCGv_i64 cpu_vsrh(int n)
7008{
7009 if (n < 32) {
7010 return cpu_fpr[n];
7011 } else {
7012 return cpu_avrh[n-32];
7013 }
7014}
7015
7016static inline TCGv_i64 cpu_vsrl(int n)
7017{
7018 if (n < 32) {
7019 return cpu_vsr[n];
7020 } else {
7021 return cpu_avrl[n-32];
7022 }
7023}
7024
fa1832d7
TM
7025static void gen_lxsdx(DisasContext *ctx)
7026{
7027 TCGv EA;
7028 if (unlikely(!ctx->vsx_enabled)) {
7029 gen_exception(ctx, POWERPC_EXCP_VSXU);
7030 return;
7031 }
7032 gen_set_access_type(ctx, ACCESS_INT);
7033 EA = tcg_temp_new();
7034 gen_addr_reg_index(ctx, EA);
7035 gen_qemu_ld64(ctx, cpu_vsrh(xT(ctx->opcode)), EA);
7036 /* NOTE: cpu_vsrl is undefined */
7037 tcg_temp_free(EA);
7038}
7039
304af367
TM
7040static void gen_lxvd2x(DisasContext *ctx)
7041{
7042 TCGv EA;
7043 if (unlikely(!ctx->vsx_enabled)) {
7044 gen_exception(ctx, POWERPC_EXCP_VSXU);
7045 return;
7046 }
7047 gen_set_access_type(ctx, ACCESS_INT);
7048 EA = tcg_temp_new();
7049 gen_addr_reg_index(ctx, EA);
7050 gen_qemu_ld64(ctx, cpu_vsrh(xT(ctx->opcode)), EA);
7051 tcg_gen_addi_tl(EA, EA, 8);
7052 gen_qemu_ld64(ctx, cpu_vsrl(xT(ctx->opcode)), EA);
7053 tcg_temp_free(EA);
7054}
7055
ca03b467
TM
7056static void gen_lxvdsx(DisasContext *ctx)
7057{
7058 TCGv EA;
7059 if (unlikely(!ctx->vsx_enabled)) {
7060 gen_exception(ctx, POWERPC_EXCP_VSXU);
7061 return;
7062 }
7063 gen_set_access_type(ctx, ACCESS_INT);
7064 EA = tcg_temp_new();
7065 gen_addr_reg_index(ctx, EA);
7066 gen_qemu_ld64(ctx, cpu_vsrh(xT(ctx->opcode)), EA);
f976b09e 7067 tcg_gen_mov_i64(cpu_vsrl(xT(ctx->opcode)), cpu_vsrh(xT(ctx->opcode)));
ca03b467
TM
7068 tcg_temp_free(EA);
7069}
7070
897e61d1
TM
7071static void gen_lxvw4x(DisasContext *ctx)
7072{
f976b09e
AG
7073 TCGv EA;
7074 TCGv_i64 tmp;
897e61d1
TM
7075 TCGv_i64 xth = cpu_vsrh(xT(ctx->opcode));
7076 TCGv_i64 xtl = cpu_vsrl(xT(ctx->opcode));
7077 if (unlikely(!ctx->vsx_enabled)) {
7078 gen_exception(ctx, POWERPC_EXCP_VSXU);
7079 return;
7080 }
7081 gen_set_access_type(ctx, ACCESS_INT);
7082 EA = tcg_temp_new();
f976b09e
AG
7083 tmp = tcg_temp_new_i64();
7084
897e61d1 7085 gen_addr_reg_index(ctx, EA);
f976b09e 7086 gen_qemu_ld32u_i64(ctx, tmp, EA);
897e61d1 7087 tcg_gen_addi_tl(EA, EA, 4);
f976b09e 7088 gen_qemu_ld32u_i64(ctx, xth, EA);
897e61d1
TM
7089 tcg_gen_deposit_i64(xth, xth, tmp, 32, 32);
7090
7091 tcg_gen_addi_tl(EA, EA, 4);
f976b09e 7092 gen_qemu_ld32u_i64(ctx, tmp, EA);
897e61d1 7093 tcg_gen_addi_tl(EA, EA, 4);
f976b09e 7094 gen_qemu_ld32u_i64(ctx, xtl, EA);
897e61d1
TM
7095 tcg_gen_deposit_i64(xtl, xtl, tmp, 32, 32);
7096
7097 tcg_temp_free(EA);
f976b09e 7098 tcg_temp_free_i64(tmp);
897e61d1
TM
7099}
7100
9231ba9e
TM
7101static void gen_stxsdx(DisasContext *ctx)
7102{
7103 TCGv EA;
7104 if (unlikely(!ctx->vsx_enabled)) {
7105 gen_exception(ctx, POWERPC_EXCP_VSXU);
7106 return;
7107 }
7108 gen_set_access_type(ctx, ACCESS_INT);
7109 EA = tcg_temp_new();
7110 gen_addr_reg_index(ctx, EA);
7111 gen_qemu_st64(ctx, cpu_vsrh(xS(ctx->opcode)), EA);
7112 tcg_temp_free(EA);
7113}
7114
fbed2478
TM
7115static void gen_stxvd2x(DisasContext *ctx)
7116{
7117 TCGv EA;
7118 if (unlikely(!ctx->vsx_enabled)) {
7119 gen_exception(ctx, POWERPC_EXCP_VSXU);
7120 return;
7121 }
7122 gen_set_access_type(ctx, ACCESS_INT);
7123 EA = tcg_temp_new();
7124 gen_addr_reg_index(ctx, EA);
7125 gen_qemu_st64(ctx, cpu_vsrh(xS(ctx->opcode)), EA);
7126 tcg_gen_addi_tl(EA, EA, 8);
7127 gen_qemu_st64(ctx, cpu_vsrl(xS(ctx->opcode)), EA);
7128 tcg_temp_free(EA);
7129}
7130
86e61ce3
TM
7131static void gen_stxvw4x(DisasContext *ctx)
7132{
f976b09e
AG
7133 TCGv_i64 tmp;
7134 TCGv EA;
86e61ce3
TM
7135 if (unlikely(!ctx->vsx_enabled)) {
7136 gen_exception(ctx, POWERPC_EXCP_VSXU);
7137 return;
7138 }
7139 gen_set_access_type(ctx, ACCESS_INT);
7140 EA = tcg_temp_new();
7141 gen_addr_reg_index(ctx, EA);
f976b09e 7142 tmp = tcg_temp_new_i64();
86e61ce3
TM
7143
7144 tcg_gen_shri_i64(tmp, cpu_vsrh(xS(ctx->opcode)), 32);
f976b09e 7145 gen_qemu_st32_i64(ctx, tmp, EA);
86e61ce3 7146 tcg_gen_addi_tl(EA, EA, 4);
f976b09e 7147 gen_qemu_st32_i64(ctx, cpu_vsrh(xS(ctx->opcode)), EA);
86e61ce3
TM
7148
7149 tcg_gen_shri_i64(tmp, cpu_vsrl(xS(ctx->opcode)), 32);
7150 tcg_gen_addi_tl(EA, EA, 4);
f976b09e 7151 gen_qemu_st32_i64(ctx, tmp, EA);
86e61ce3 7152 tcg_gen_addi_tl(EA, EA, 4);
f976b09e 7153 gen_qemu_st32_i64(ctx, cpu_vsrl(xS(ctx->opcode)), EA);
86e61ce3
TM
7154
7155 tcg_temp_free(EA);
f976b09e 7156 tcg_temp_free_i64(tmp);
86e61ce3
TM
7157}
7158
cd73f2c9
TM
7159static void gen_xxpermdi(DisasContext *ctx)
7160{
7161 if (unlikely(!ctx->vsx_enabled)) {
7162 gen_exception(ctx, POWERPC_EXCP_VSXU);
7163 return;
7164 }
7165
7166 if ((DM(ctx->opcode) & 2) == 0) {
7167 tcg_gen_mov_i64(cpu_vsrh(xT(ctx->opcode)), cpu_vsrh(xA(ctx->opcode)));
7168 } else {
7169 tcg_gen_mov_i64(cpu_vsrh(xT(ctx->opcode)), cpu_vsrl(xA(ctx->opcode)));
7170 }
7171 if ((DM(ctx->opcode) & 1) == 0) {
7172 tcg_gen_mov_i64(cpu_vsrl(xT(ctx->opcode)), cpu_vsrh(xB(ctx->opcode)));
7173 } else {
7174 tcg_gen_mov_i64(cpu_vsrl(xT(ctx->opcode)), cpu_vsrl(xB(ctx->opcode)));
7175 }
7176}
7177
df020ce0
TM
7178#define OP_ABS 1
7179#define OP_NABS 2
7180#define OP_NEG 3
7181#define OP_CPSGN 4
7182#define SGN_MASK_DP 0x8000000000000000ul
7183#define SGN_MASK_SP 0x8000000080000000ul
7184
7185#define VSX_SCALAR_MOVE(name, op, sgn_mask) \
7186static void glue(gen_, name)(DisasContext * ctx) \
7187 { \
7188 TCGv_i64 xb, sgm; \
7189 if (unlikely(!ctx->vsx_enabled)) { \
7190 gen_exception(ctx, POWERPC_EXCP_VSXU); \
7191 return; \
7192 } \
f976b09e
AG
7193 xb = tcg_temp_new_i64(); \
7194 sgm = tcg_temp_new_i64(); \
df020ce0
TM
7195 tcg_gen_mov_i64(xb, cpu_vsrh(xB(ctx->opcode))); \
7196 tcg_gen_movi_i64(sgm, sgn_mask); \
7197 switch (op) { \
7198 case OP_ABS: { \
7199 tcg_gen_andc_i64(xb, xb, sgm); \
7200 break; \
7201 } \
7202 case OP_NABS: { \
7203 tcg_gen_or_i64(xb, xb, sgm); \
7204 break; \
7205 } \
7206 case OP_NEG: { \
7207 tcg_gen_xor_i64(xb, xb, sgm); \
7208 break; \
7209 } \
7210 case OP_CPSGN: { \
f976b09e 7211 TCGv_i64 xa = tcg_temp_new_i64(); \
df020ce0
TM
7212 tcg_gen_mov_i64(xa, cpu_vsrh(xA(ctx->opcode))); \
7213 tcg_gen_and_i64(xa, xa, sgm); \
7214 tcg_gen_andc_i64(xb, xb, sgm); \
7215 tcg_gen_or_i64(xb, xb, xa); \
f976b09e 7216 tcg_temp_free_i64(xa); \
df020ce0
TM
7217 break; \
7218 } \
7219 } \
7220 tcg_gen_mov_i64(cpu_vsrh(xT(ctx->opcode)), xb); \
f976b09e
AG
7221 tcg_temp_free_i64(xb); \
7222 tcg_temp_free_i64(sgm); \
df020ce0
TM
7223 }
7224
7225VSX_SCALAR_MOVE(xsabsdp, OP_ABS, SGN_MASK_DP)
7226VSX_SCALAR_MOVE(xsnabsdp, OP_NABS, SGN_MASK_DP)
7227VSX_SCALAR_MOVE(xsnegdp, OP_NEG, SGN_MASK_DP)
7228VSX_SCALAR_MOVE(xscpsgndp, OP_CPSGN, SGN_MASK_DP)
7229
be574920
TM
7230#define VSX_VECTOR_MOVE(name, op, sgn_mask) \
7231static void glue(gen_, name)(DisasContext * ctx) \
7232 { \
7233 TCGv_i64 xbh, xbl, sgm; \
7234 if (unlikely(!ctx->vsx_enabled)) { \
7235 gen_exception(ctx, POWERPC_EXCP_VSXU); \
7236 return; \
7237 } \
f976b09e
AG
7238 xbh = tcg_temp_new_i64(); \
7239 xbl = tcg_temp_new_i64(); \
7240 sgm = tcg_temp_new_i64(); \
be574920
TM
7241 tcg_gen_mov_i64(xbh, cpu_vsrh(xB(ctx->opcode))); \
7242 tcg_gen_mov_i64(xbl, cpu_vsrl(xB(ctx->opcode))); \
7243 tcg_gen_movi_i64(sgm, sgn_mask); \
7244 switch (op) { \
7245 case OP_ABS: { \
7246 tcg_gen_andc_i64(xbh, xbh, sgm); \
7247 tcg_gen_andc_i64(xbl, xbl, sgm); \
7248 break; \
7249 } \
7250 case OP_NABS: { \
7251 tcg_gen_or_i64(xbh, xbh, sgm); \
7252 tcg_gen_or_i64(xbl, xbl, sgm); \
7253 break; \
7254 } \
7255 case OP_NEG: { \
7256 tcg_gen_xor_i64(xbh, xbh, sgm); \
7257 tcg_gen_xor_i64(xbl, xbl, sgm); \
7258 break; \
7259 } \
7260 case OP_CPSGN: { \
f976b09e
AG
7261 TCGv_i64 xah = tcg_temp_new_i64(); \
7262 TCGv_i64 xal = tcg_temp_new_i64(); \
be574920
TM
7263 tcg_gen_mov_i64(xah, cpu_vsrh(xA(ctx->opcode))); \
7264 tcg_gen_mov_i64(xal, cpu_vsrl(xA(ctx->opcode))); \
7265 tcg_gen_and_i64(xah, xah, sgm); \
7266 tcg_gen_and_i64(xal, xal, sgm); \
7267 tcg_gen_andc_i64(xbh, xbh, sgm); \
7268 tcg_gen_andc_i64(xbl, xbl, sgm); \
7269 tcg_gen_or_i64(xbh, xbh, xah); \
7270 tcg_gen_or_i64(xbl, xbl, xal); \
f976b09e
AG
7271 tcg_temp_free_i64(xah); \
7272 tcg_temp_free_i64(xal); \
be574920
TM
7273 break; \
7274 } \
7275 } \
7276 tcg_gen_mov_i64(cpu_vsrh(xT(ctx->opcode)), xbh); \
7277 tcg_gen_mov_i64(cpu_vsrl(xT(ctx->opcode)), xbl); \
f976b09e
AG
7278 tcg_temp_free_i64(xbh); \
7279 tcg_temp_free_i64(xbl); \
7280 tcg_temp_free_i64(sgm); \
be574920
TM
7281 }
7282
7283VSX_VECTOR_MOVE(xvabsdp, OP_ABS, SGN_MASK_DP)
7284VSX_VECTOR_MOVE(xvnabsdp, OP_NABS, SGN_MASK_DP)
7285VSX_VECTOR_MOVE(xvnegdp, OP_NEG, SGN_MASK_DP)
7286VSX_VECTOR_MOVE(xvcpsgndp, OP_CPSGN, SGN_MASK_DP)
7287VSX_VECTOR_MOVE(xvabssp, OP_ABS, SGN_MASK_SP)
7288VSX_VECTOR_MOVE(xvnabssp, OP_NABS, SGN_MASK_SP)
7289VSX_VECTOR_MOVE(xvnegsp, OP_NEG, SGN_MASK_SP)
7290VSX_VECTOR_MOVE(xvcpsgnsp, OP_CPSGN, SGN_MASK_SP)
7291
3c3cbbdc
TM
7292#define GEN_VSX_HELPER_2(name, op1, op2, inval, type) \
7293static void gen_##name(DisasContext * ctx) \
7294{ \
7295 TCGv_i32 opc; \
7296 if (unlikely(!ctx->vsx_enabled)) { \
7297 gen_exception(ctx, POWERPC_EXCP_VSXU); \
7298 return; \
7299 } \
7300 /* NIP cannot be restored if the memory exception comes from an helper */ \
7301 gen_update_nip(ctx, ctx->nip - 4); \
7302 opc = tcg_const_i32(ctx->opcode); \
7303 gen_helper_##name(cpu_env, opc); \
7304 tcg_temp_free_i32(opc); \
7305}
be574920 7306
ee6e02c0
TM
7307GEN_VSX_HELPER_2(xsadddp, 0x00, 0x04, 0, PPC2_VSX)
7308GEN_VSX_HELPER_2(xssubdp, 0x00, 0x05, 0, PPC2_VSX)
5e591d88 7309GEN_VSX_HELPER_2(xsmuldp, 0x00, 0x06, 0, PPC2_VSX)
4b98eeef 7310GEN_VSX_HELPER_2(xsdivdp, 0x00, 0x07, 0, PPC2_VSX)
2009227f 7311GEN_VSX_HELPER_2(xsredp, 0x14, 0x05, 0, PPC2_VSX)
d32404fe 7312GEN_VSX_HELPER_2(xssqrtdp, 0x16, 0x04, 0, PPC2_VSX)
d3f9df8f 7313GEN_VSX_HELPER_2(xsrsqrtedp, 0x14, 0x04, 0, PPC2_VSX)
bc80838f 7314GEN_VSX_HELPER_2(xstdivdp, 0x14, 0x07, 0, PPC2_VSX)
ee6e02c0
TM
7315
7316GEN_VSX_HELPER_2(xvadddp, 0x00, 0x0C, 0, PPC2_VSX)
7317GEN_VSX_HELPER_2(xvsubdp, 0x00, 0x0D, 0, PPC2_VSX)
5e591d88 7318GEN_VSX_HELPER_2(xvmuldp, 0x00, 0x0E, 0, PPC2_VSX)
4b98eeef 7319GEN_VSX_HELPER_2(xvdivdp, 0x00, 0x0F, 0, PPC2_VSX)
2009227f 7320GEN_VSX_HELPER_2(xvredp, 0x14, 0x0D, 0, PPC2_VSX)
d32404fe 7321GEN_VSX_HELPER_2(xvsqrtdp, 0x16, 0x0C, 0, PPC2_VSX)
d3f9df8f 7322GEN_VSX_HELPER_2(xvrsqrtedp, 0x14, 0x0C, 0, PPC2_VSX)
bc80838f 7323GEN_VSX_HELPER_2(xvtdivdp, 0x14, 0x0F, 0, PPC2_VSX)
ee6e02c0
TM
7324
7325GEN_VSX_HELPER_2(xvaddsp, 0x00, 0x08, 0, PPC2_VSX)
7326GEN_VSX_HELPER_2(xvsubsp, 0x00, 0x09, 0, PPC2_VSX)
5e591d88 7327GEN_VSX_HELPER_2(xvmulsp, 0x00, 0x0A, 0, PPC2_VSX)
4b98eeef 7328GEN_VSX_HELPER_2(xvdivsp, 0x00, 0x0B, 0, PPC2_VSX)
2009227f 7329GEN_VSX_HELPER_2(xvresp, 0x14, 0x09, 0, PPC2_VSX)
d32404fe 7330GEN_VSX_HELPER_2(xvsqrtsp, 0x16, 0x08, 0, PPC2_VSX)
d3f9df8f 7331GEN_VSX_HELPER_2(xvrsqrtesp, 0x14, 0x08, 0, PPC2_VSX)
bc80838f 7332GEN_VSX_HELPER_2(xvtdivsp, 0x14, 0x0B, 0, PPC2_VSX)
ee6e02c0 7333
79ca8a6a
TM
7334#define VSX_LOGICAL(name, tcg_op) \
7335static void glue(gen_, name)(DisasContext * ctx) \
7336 { \
7337 if (unlikely(!ctx->vsx_enabled)) { \
7338 gen_exception(ctx, POWERPC_EXCP_VSXU); \
7339 return; \
7340 } \
7341 tcg_op(cpu_vsrh(xT(ctx->opcode)), cpu_vsrh(xA(ctx->opcode)), \
7342 cpu_vsrh(xB(ctx->opcode))); \
7343 tcg_op(cpu_vsrl(xT(ctx->opcode)), cpu_vsrl(xA(ctx->opcode)), \
7344 cpu_vsrl(xB(ctx->opcode))); \
7345 }
7346
f976b09e
AG
7347VSX_LOGICAL(xxland, tcg_gen_and_i64)
7348VSX_LOGICAL(xxlandc, tcg_gen_andc_i64)
7349VSX_LOGICAL(xxlor, tcg_gen_or_i64)
7350VSX_LOGICAL(xxlxor, tcg_gen_xor_i64)
7351VSX_LOGICAL(xxlnor, tcg_gen_nor_i64)
df020ce0 7352
ce577d2e
TM
7353#define VSX_XXMRG(name, high) \
7354static void glue(gen_, name)(DisasContext * ctx) \
7355 { \
7356 TCGv_i64 a0, a1, b0, b1; \
7357 if (unlikely(!ctx->vsx_enabled)) { \
7358 gen_exception(ctx, POWERPC_EXCP_VSXU); \
7359 return; \
7360 } \
f976b09e
AG
7361 a0 = tcg_temp_new_i64(); \
7362 a1 = tcg_temp_new_i64(); \
7363 b0 = tcg_temp_new_i64(); \
7364 b1 = tcg_temp_new_i64(); \
ce577d2e
TM
7365 if (high) { \
7366 tcg_gen_mov_i64(a0, cpu_vsrh(xA(ctx->opcode))); \
7367 tcg_gen_mov_i64(a1, cpu_vsrh(xA(ctx->opcode))); \
7368 tcg_gen_mov_i64(b0, cpu_vsrh(xB(ctx->opcode))); \
7369 tcg_gen_mov_i64(b1, cpu_vsrh(xB(ctx->opcode))); \
7370 } else { \
7371 tcg_gen_mov_i64(a0, cpu_vsrl(xA(ctx->opcode))); \
7372 tcg_gen_mov_i64(a1, cpu_vsrl(xA(ctx->opcode))); \
7373 tcg_gen_mov_i64(b0, cpu_vsrl(xB(ctx->opcode))); \
7374 tcg_gen_mov_i64(b1, cpu_vsrl(xB(ctx->opcode))); \
7375 } \
7376 tcg_gen_shri_i64(a0, a0, 32); \
7377 tcg_gen_shri_i64(b0, b0, 32); \
7378 tcg_gen_deposit_i64(cpu_vsrh(xT(ctx->opcode)), \
7379 b0, a0, 32, 32); \
7380 tcg_gen_deposit_i64(cpu_vsrl(xT(ctx->opcode)), \
7381 b1, a1, 32, 32); \
f976b09e
AG
7382 tcg_temp_free_i64(a0); \
7383 tcg_temp_free_i64(a1); \
7384 tcg_temp_free_i64(b0); \
7385 tcg_temp_free_i64(b1); \
ce577d2e
TM
7386 }
7387
7388VSX_XXMRG(xxmrghw, 1)
7389VSX_XXMRG(xxmrglw, 0)
7390
551e3ef7
TM
7391static void gen_xxsel(DisasContext * ctx)
7392{
7393 TCGv_i64 a, b, c;
7394 if (unlikely(!ctx->vsx_enabled)) {
7395 gen_exception(ctx, POWERPC_EXCP_VSXU);
7396 return;
7397 }
f976b09e
AG
7398 a = tcg_temp_new_i64();
7399 b = tcg_temp_new_i64();
7400 c = tcg_temp_new_i64();
551e3ef7
TM
7401
7402 tcg_gen_mov_i64(a, cpu_vsrh(xA(ctx->opcode)));
7403 tcg_gen_mov_i64(b, cpu_vsrh(xB(ctx->opcode)));
7404 tcg_gen_mov_i64(c, cpu_vsrh(xC(ctx->opcode)));
7405
7406 tcg_gen_and_i64(b, b, c);
7407 tcg_gen_andc_i64(a, a, c);
7408 tcg_gen_or_i64(cpu_vsrh(xT(ctx->opcode)), a, b);
7409
7410 tcg_gen_mov_i64(a, cpu_vsrl(xA(ctx->opcode)));
7411 tcg_gen_mov_i64(b, cpu_vsrl(xB(ctx->opcode)));
7412 tcg_gen_mov_i64(c, cpu_vsrl(xC(ctx->opcode)));
7413
7414 tcg_gen_and_i64(b, b, c);
7415 tcg_gen_andc_i64(a, a, c);
7416 tcg_gen_or_i64(cpu_vsrl(xT(ctx->opcode)), a, b);
7417
f976b09e
AG
7418 tcg_temp_free_i64(a);
7419 tcg_temp_free_i64(b);
7420 tcg_temp_free_i64(c);
551e3ef7
TM
7421}
7422
76c15fe0
TM
7423static void gen_xxspltw(DisasContext *ctx)
7424{
7425 TCGv_i64 b, b2;
7426 TCGv_i64 vsr = (UIM(ctx->opcode) & 2) ?
7427 cpu_vsrl(xB(ctx->opcode)) :
7428 cpu_vsrh(xB(ctx->opcode));
7429
7430 if (unlikely(!ctx->vsx_enabled)) {
7431 gen_exception(ctx, POWERPC_EXCP_VSXU);
7432 return;
7433 }
7434
f976b09e
AG
7435 b = tcg_temp_new_i64();
7436 b2 = tcg_temp_new_i64();
76c15fe0
TM
7437
7438 if (UIM(ctx->opcode) & 1) {
7439 tcg_gen_ext32u_i64(b, vsr);
7440 } else {
7441 tcg_gen_shri_i64(b, vsr, 32);
7442 }
7443
7444 tcg_gen_shli_i64(b2, b, 32);
7445 tcg_gen_or_i64(cpu_vsrh(xT(ctx->opcode)), b, b2);
7446 tcg_gen_mov_i64(cpu_vsrl(xT(ctx->opcode)), cpu_vsrh(xT(ctx->opcode)));
7447
f976b09e
AG
7448 tcg_temp_free_i64(b);
7449 tcg_temp_free_i64(b2);
76c15fe0
TM
7450}
7451
acc42968
TM
7452static void gen_xxsldwi(DisasContext *ctx)
7453{
7454 TCGv_i64 xth, xtl;
7455 if (unlikely(!ctx->vsx_enabled)) {
7456 gen_exception(ctx, POWERPC_EXCP_VSXU);
7457 return;
7458 }
f976b09e
AG
7459 xth = tcg_temp_new_i64();
7460 xtl = tcg_temp_new_i64();
acc42968
TM
7461
7462 switch (SHW(ctx->opcode)) {
7463 case 0: {
7464 tcg_gen_mov_i64(xth, cpu_vsrh(xA(ctx->opcode)));
7465 tcg_gen_mov_i64(xtl, cpu_vsrl(xA(ctx->opcode)));
7466 break;
7467 }
7468 case 1: {
f976b09e 7469 TCGv_i64 t0 = tcg_temp_new_i64();
acc42968
TM
7470 tcg_gen_mov_i64(xth, cpu_vsrh(xA(ctx->opcode)));
7471 tcg_gen_shli_i64(xth, xth, 32);
7472 tcg_gen_mov_i64(t0, cpu_vsrl(xA(ctx->opcode)));
7473 tcg_gen_shri_i64(t0, t0, 32);
7474 tcg_gen_or_i64(xth, xth, t0);
7475 tcg_gen_mov_i64(xtl, cpu_vsrl(xA(ctx->opcode)));
7476 tcg_gen_shli_i64(xtl, xtl, 32);
7477 tcg_gen_mov_i64(t0, cpu_vsrh(xB(ctx->opcode)));
7478 tcg_gen_shri_i64(t0, t0, 32);
7479 tcg_gen_or_i64(xtl, xtl, t0);
f976b09e 7480 tcg_temp_free_i64(t0);
acc42968
TM
7481 break;
7482 }
7483 case 2: {
7484 tcg_gen_mov_i64(xth, cpu_vsrl(xA(ctx->opcode)));
7485 tcg_gen_mov_i64(xtl, cpu_vsrh(xB(ctx->opcode)));
7486 break;
7487 }
7488 case 3: {
f976b09e 7489 TCGv_i64 t0 = tcg_temp_new_i64();
acc42968
TM
7490 tcg_gen_mov_i64(xth, cpu_vsrl(xA(ctx->opcode)));
7491 tcg_gen_shli_i64(xth, xth, 32);
7492 tcg_gen_mov_i64(t0, cpu_vsrh(xB(ctx->opcode)));
7493 tcg_gen_shri_i64(t0, t0, 32);
7494 tcg_gen_or_i64(xth, xth, t0);
7495 tcg_gen_mov_i64(xtl, cpu_vsrh(xB(ctx->opcode)));
7496 tcg_gen_shli_i64(xtl, xtl, 32);
7497 tcg_gen_mov_i64(t0, cpu_vsrl(xB(ctx->opcode)));
7498 tcg_gen_shri_i64(t0, t0, 32);
7499 tcg_gen_or_i64(xtl, xtl, t0);
f976b09e 7500 tcg_temp_free_i64(t0);
acc42968
TM
7501 break;
7502 }
7503 }
7504
7505 tcg_gen_mov_i64(cpu_vsrh(xT(ctx->opcode)), xth);
7506 tcg_gen_mov_i64(cpu_vsrl(xT(ctx->opcode)), xtl);
7507
f976b09e
AG
7508 tcg_temp_free_i64(xth);
7509 tcg_temp_free_i64(xtl);
acc42968
TM
7510}
7511
ce577d2e 7512
0487d6a8 7513/*** SPE extension ***/
0487d6a8 7514/* Register moves */
3cd7d1dd 7515
a0e13900
FC
7516static inline void gen_evmra(DisasContext *ctx)
7517{
7518
7519 if (unlikely(!ctx->spe_enabled)) {
27a69bb0 7520 gen_exception(ctx, POWERPC_EXCP_SPEU);
a0e13900
FC
7521 return;
7522 }
7523
7524#if defined(TARGET_PPC64)
7525 /* rD := rA */
7526 tcg_gen_mov_i64(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)]);
7527
7528 /* spe_acc := rA */
7529 tcg_gen_st_i64(cpu_gpr[rA(ctx->opcode)],
7530 cpu_env,
1328c2bf 7531 offsetof(CPUPPCState, spe_acc));
a0e13900
FC
7532#else
7533 TCGv_i64 tmp = tcg_temp_new_i64();
7534
7535 /* tmp := rA_lo + rA_hi << 32 */
7536 tcg_gen_concat_i32_i64(tmp, cpu_gpr[rA(ctx->opcode)], cpu_gprh[rA(ctx->opcode)]);
7537
7538 /* spe_acc := tmp */
1328c2bf 7539 tcg_gen_st_i64(tmp, cpu_env, offsetof(CPUPPCState, spe_acc));
a0e13900
FC
7540 tcg_temp_free_i64(tmp);
7541
7542 /* rD := rA */
7543 tcg_gen_mov_i32(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)]);
7544 tcg_gen_mov_i32(cpu_gprh[rD(ctx->opcode)], cpu_gprh[rA(ctx->opcode)]);
7545#endif
7546}
7547
636aa200
BS
7548static inline void gen_load_gpr64(TCGv_i64 t, int reg)
7549{
f78fb44e
AJ
7550#if defined(TARGET_PPC64)
7551 tcg_gen_mov_i64(t, cpu_gpr[reg]);
7552#else
36aa55dc 7553 tcg_gen_concat_i32_i64(t, cpu_gpr[reg], cpu_gprh[reg]);
3cd7d1dd 7554#endif
f78fb44e 7555}
3cd7d1dd 7556
636aa200
BS
7557static inline void gen_store_gpr64(int reg, TCGv_i64 t)
7558{
f78fb44e
AJ
7559#if defined(TARGET_PPC64)
7560 tcg_gen_mov_i64(cpu_gpr[reg], t);
7561#else
a7812ae4 7562 TCGv_i64 tmp = tcg_temp_new_i64();
f78fb44e 7563 tcg_gen_trunc_i64_i32(cpu_gpr[reg], t);
f78fb44e
AJ
7564 tcg_gen_shri_i64(tmp, t, 32);
7565 tcg_gen_trunc_i64_i32(cpu_gprh[reg], tmp);
a7812ae4 7566 tcg_temp_free_i64(tmp);
3cd7d1dd 7567#endif
f78fb44e 7568}
3cd7d1dd 7569
70560da7 7570#define GEN_SPE(name0, name1, opc2, opc3, inval0, inval1, type) \
99e300ef 7571static void glue(gen_, name0##_##name1)(DisasContext *ctx) \
0487d6a8
JM
7572{ \
7573 if (Rc(ctx->opcode)) \
7574 gen_##name1(ctx); \
7575 else \
7576 gen_##name0(ctx); \
7577}
7578
7579/* Handler for undefined SPE opcodes */
636aa200 7580static inline void gen_speundef(DisasContext *ctx)
0487d6a8 7581{
e06fcd75 7582 gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL);
0487d6a8
JM
7583}
7584
57951c27
AJ
7585/* SPE logic */
7586#if defined(TARGET_PPC64)
7587#define GEN_SPEOP_LOGIC2(name, tcg_op) \
636aa200 7588static inline void gen_##name(DisasContext *ctx) \
0487d6a8
JM
7589{ \
7590 if (unlikely(!ctx->spe_enabled)) { \
27a69bb0 7591 gen_exception(ctx, POWERPC_EXCP_SPEU); \
0487d6a8
JM
7592 return; \
7593 } \
57951c27
AJ
7594 tcg_op(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)], \
7595 cpu_gpr[rB(ctx->opcode)]); \
7596}
7597#else
7598#define GEN_SPEOP_LOGIC2(name, tcg_op) \
636aa200 7599static inline void gen_##name(DisasContext *ctx) \
57951c27
AJ
7600{ \
7601 if (unlikely(!ctx->spe_enabled)) { \
27a69bb0 7602 gen_exception(ctx, POWERPC_EXCP_SPEU); \
57951c27
AJ
7603 return; \
7604 } \
7605 tcg_op(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)], \
7606 cpu_gpr[rB(ctx->opcode)]); \
7607 tcg_op(cpu_gprh[rD(ctx->opcode)], cpu_gprh[rA(ctx->opcode)], \
7608 cpu_gprh[rB(ctx->opcode)]); \
0487d6a8 7609}
57951c27
AJ
7610#endif
7611
7612GEN_SPEOP_LOGIC2(evand, tcg_gen_and_tl);
7613GEN_SPEOP_LOGIC2(evandc, tcg_gen_andc_tl);
7614GEN_SPEOP_LOGIC2(evxor, tcg_gen_xor_tl);
7615GEN_SPEOP_LOGIC2(evor, tcg_gen_or_tl);
7616GEN_SPEOP_LOGIC2(evnor, tcg_gen_nor_tl);
7617GEN_SPEOP_LOGIC2(eveqv, tcg_gen_eqv_tl);
7618GEN_SPEOP_LOGIC2(evorc, tcg_gen_orc_tl);
7619GEN_SPEOP_LOGIC2(evnand, tcg_gen_nand_tl);
0487d6a8 7620
57951c27
AJ
7621/* SPE logic immediate */
7622#if defined(TARGET_PPC64)
7623#define GEN_SPEOP_TCG_LOGIC_IMM2(name, tcg_opi) \
636aa200 7624static inline void gen_##name(DisasContext *ctx) \
3d3a6a0a
AJ
7625{ \
7626 if (unlikely(!ctx->spe_enabled)) { \
27a69bb0 7627 gen_exception(ctx, POWERPC_EXCP_SPEU); \
3d3a6a0a
AJ
7628 return; \
7629 } \
a7812ae4
PB
7630 TCGv_i32 t0 = tcg_temp_local_new_i32(); \
7631 TCGv_i32 t1 = tcg_temp_local_new_i32(); \
7632 TCGv_i64 t2 = tcg_temp_local_new_i64(); \
57951c27
AJ
7633 tcg_gen_trunc_i64_i32(t0, cpu_gpr[rA(ctx->opcode)]); \
7634 tcg_opi(t0, t0, rB(ctx->opcode)); \
7635 tcg_gen_shri_i64(t2, cpu_gpr[rA(ctx->opcode)], 32); \
7636 tcg_gen_trunc_i64_i32(t1, t2); \
a7812ae4 7637 tcg_temp_free_i64(t2); \
57951c27
AJ
7638 tcg_opi(t1, t1, rB(ctx->opcode)); \
7639 tcg_gen_concat_i32_i64(cpu_gpr[rD(ctx->opcode)], t0, t1); \
a7812ae4
PB
7640 tcg_temp_free_i32(t0); \
7641 tcg_temp_free_i32(t1); \
3d3a6a0a 7642}
57951c27
AJ
7643#else
7644#define GEN_SPEOP_TCG_LOGIC_IMM2(name, tcg_opi) \
636aa200 7645static inline void gen_##name(DisasContext *ctx) \
0487d6a8
JM
7646{ \
7647 if (unlikely(!ctx->spe_enabled)) { \
27a69bb0 7648 gen_exception(ctx, POWERPC_EXCP_SPEU); \
0487d6a8
JM
7649 return; \
7650 } \
57951c27
AJ
7651 tcg_opi(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)], \
7652 rB(ctx->opcode)); \
7653 tcg_opi(cpu_gprh[rD(ctx->opcode)], cpu_gprh[rA(ctx->opcode)], \
7654 rB(ctx->opcode)); \
0487d6a8 7655}
57951c27
AJ
7656#endif
7657GEN_SPEOP_TCG_LOGIC_IMM2(evslwi, tcg_gen_shli_i32);
7658GEN_SPEOP_TCG_LOGIC_IMM2(evsrwiu, tcg_gen_shri_i32);
7659GEN_SPEOP_TCG_LOGIC_IMM2(evsrwis, tcg_gen_sari_i32);
7660GEN_SPEOP_TCG_LOGIC_IMM2(evrlwi, tcg_gen_rotli_i32);
0487d6a8 7661
57951c27
AJ
7662/* SPE arithmetic */
7663#if defined(TARGET_PPC64)
7664#define GEN_SPEOP_ARITH1(name, tcg_op) \
636aa200 7665static inline void gen_##name(DisasContext *ctx) \
0487d6a8
JM
7666{ \
7667 if (unlikely(!ctx->spe_enabled)) { \
27a69bb0 7668 gen_exception(ctx, POWERPC_EXCP_SPEU); \
0487d6a8
JM
7669 return; \
7670 } \
a7812ae4
PB
7671 TCGv_i32 t0 = tcg_temp_local_new_i32(); \
7672 TCGv_i32 t1 = tcg_temp_local_new_i32(); \
7673 TCGv_i64 t2 = tcg_temp_local_new_i64(); \
57951c27
AJ
7674 tcg_gen_trunc_i64_i32(t0, cpu_gpr[rA(ctx->opcode)]); \
7675 tcg_op(t0, t0); \
7676 tcg_gen_shri_i64(t2, cpu_gpr[rA(ctx->opcode)], 32); \
7677 tcg_gen_trunc_i64_i32(t1, t2); \
a7812ae4 7678 tcg_temp_free_i64(t2); \
57951c27
AJ
7679 tcg_op(t1, t1); \
7680 tcg_gen_concat_i32_i64(cpu_gpr[rD(ctx->opcode)], t0, t1); \
a7812ae4
PB
7681 tcg_temp_free_i32(t0); \
7682 tcg_temp_free_i32(t1); \
0487d6a8 7683}
57951c27 7684#else
a7812ae4 7685#define GEN_SPEOP_ARITH1(name, tcg_op) \
636aa200 7686static inline void gen_##name(DisasContext *ctx) \
57951c27
AJ
7687{ \
7688 if (unlikely(!ctx->spe_enabled)) { \
27a69bb0 7689 gen_exception(ctx, POWERPC_EXCP_SPEU); \
57951c27
AJ
7690 return; \
7691 } \
7692 tcg_op(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)]); \
7693 tcg_op(cpu_gprh[rD(ctx->opcode)], cpu_gprh[rA(ctx->opcode)]); \
7694}
7695#endif
0487d6a8 7696
636aa200 7697static inline void gen_op_evabs(TCGv_i32 ret, TCGv_i32 arg1)
57951c27
AJ
7698{
7699 int l1 = gen_new_label();
7700 int l2 = gen_new_label();
0487d6a8 7701
57951c27
AJ
7702 tcg_gen_brcondi_i32(TCG_COND_GE, arg1, 0, l1);
7703 tcg_gen_neg_i32(ret, arg1);
7704 tcg_gen_br(l2);
7705 gen_set_label(l1);
a7812ae4 7706 tcg_gen_mov_i32(ret, arg1);
57951c27
AJ
7707 gen_set_label(l2);
7708}
7709GEN_SPEOP_ARITH1(evabs, gen_op_evabs);
7710GEN_SPEOP_ARITH1(evneg, tcg_gen_neg_i32);
7711GEN_SPEOP_ARITH1(evextsb, tcg_gen_ext8s_i32);
7712GEN_SPEOP_ARITH1(evextsh, tcg_gen_ext16s_i32);
636aa200 7713static inline void gen_op_evrndw(TCGv_i32 ret, TCGv_i32 arg1)
0487d6a8 7714{
57951c27
AJ
7715 tcg_gen_addi_i32(ret, arg1, 0x8000);
7716 tcg_gen_ext16u_i32(ret, ret);
7717}
7718GEN_SPEOP_ARITH1(evrndw, gen_op_evrndw);
a7812ae4
PB
7719GEN_SPEOP_ARITH1(evcntlsw, gen_helper_cntlsw32);
7720GEN_SPEOP_ARITH1(evcntlzw, gen_helper_cntlzw32);
0487d6a8 7721
57951c27
AJ
7722#if defined(TARGET_PPC64)
7723#define GEN_SPEOP_ARITH2(name, tcg_op) \
636aa200 7724static inline void gen_##name(DisasContext *ctx) \
0487d6a8
JM
7725{ \
7726 if (unlikely(!ctx->spe_enabled)) { \
27a69bb0 7727 gen_exception(ctx, POWERPC_EXCP_SPEU); \
0487d6a8
JM
7728 return; \
7729 } \
a7812ae4
PB
7730 TCGv_i32 t0 = tcg_temp_local_new_i32(); \
7731 TCGv_i32 t1 = tcg_temp_local_new_i32(); \
7732 TCGv_i32 t2 = tcg_temp_local_new_i32(); \
501e23c4 7733 TCGv_i64 t3 = tcg_temp_local_new_i64(); \
57951c27
AJ
7734 tcg_gen_trunc_i64_i32(t0, cpu_gpr[rA(ctx->opcode)]); \
7735 tcg_gen_trunc_i64_i32(t2, cpu_gpr[rB(ctx->opcode)]); \
7736 tcg_op(t0, t0, t2); \
7737 tcg_gen_shri_i64(t3, cpu_gpr[rA(ctx->opcode)], 32); \
7738 tcg_gen_trunc_i64_i32(t1, t3); \
7739 tcg_gen_shri_i64(t3, cpu_gpr[rB(ctx->opcode)], 32); \
7740 tcg_gen_trunc_i64_i32(t2, t3); \
a7812ae4 7741 tcg_temp_free_i64(t3); \
57951c27 7742 tcg_op(t1, t1, t2); \
a7812ae4 7743 tcg_temp_free_i32(t2); \
57951c27 7744 tcg_gen_concat_i32_i64(cpu_gpr[rD(ctx->opcode)], t0, t1); \
a7812ae4
PB
7745 tcg_temp_free_i32(t0); \
7746 tcg_temp_free_i32(t1); \
0487d6a8 7747}
57951c27
AJ
7748#else
7749#define GEN_SPEOP_ARITH2(name, tcg_op) \
636aa200 7750static inline void gen_##name(DisasContext *ctx) \
0487d6a8
JM
7751{ \
7752 if (unlikely(!ctx->spe_enabled)) { \
27a69bb0 7753 gen_exception(ctx, POWERPC_EXCP_SPEU); \
0487d6a8
JM
7754 return; \
7755 } \
57951c27
AJ
7756 tcg_op(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)], \
7757 cpu_gpr[rB(ctx->opcode)]); \
7758 tcg_op(cpu_gprh[rD(ctx->opcode)], cpu_gprh[rA(ctx->opcode)], \
7759 cpu_gprh[rB(ctx->opcode)]); \
0487d6a8 7760}
57951c27 7761#endif
0487d6a8 7762
636aa200 7763static inline void gen_op_evsrwu(TCGv_i32 ret, TCGv_i32 arg1, TCGv_i32 arg2)
57951c27 7764{
a7812ae4 7765 TCGv_i32 t0;
57951c27 7766 int l1, l2;
0487d6a8 7767
57951c27
AJ
7768 l1 = gen_new_label();
7769 l2 = gen_new_label();
a7812ae4 7770 t0 = tcg_temp_local_new_i32();
57951c27
AJ
7771 /* No error here: 6 bits are used */
7772 tcg_gen_andi_i32(t0, arg2, 0x3F);
7773 tcg_gen_brcondi_i32(TCG_COND_GE, t0, 32, l1);
7774 tcg_gen_shr_i32(ret, arg1, t0);
7775 tcg_gen_br(l2);
7776 gen_set_label(l1);
7777 tcg_gen_movi_i32(ret, 0);
0aef4261 7778 gen_set_label(l2);
a7812ae4 7779 tcg_temp_free_i32(t0);
57951c27
AJ
7780}
7781GEN_SPEOP_ARITH2(evsrwu, gen_op_evsrwu);
636aa200 7782static inline void gen_op_evsrws(TCGv_i32 ret, TCGv_i32 arg1, TCGv_i32 arg2)
57951c27 7783{
a7812ae4 7784 TCGv_i32 t0;
57951c27
AJ
7785 int l1, l2;
7786
7787 l1 = gen_new_label();
7788 l2 = gen_new_label();
a7812ae4 7789 t0 = tcg_temp_local_new_i32();
57951c27
AJ
7790 /* No error here: 6 bits are used */
7791 tcg_gen_andi_i32(t0, arg2, 0x3F);
7792 tcg_gen_brcondi_i32(TCG_COND_GE, t0, 32, l1);
7793 tcg_gen_sar_i32(ret, arg1, t0);
7794 tcg_gen_br(l2);
7795 gen_set_label(l1);
7796 tcg_gen_movi_i32(ret, 0);
0aef4261 7797 gen_set_label(l2);
a7812ae4 7798 tcg_temp_free_i32(t0);
57951c27
AJ
7799}
7800GEN_SPEOP_ARITH2(evsrws, gen_op_evsrws);
636aa200 7801static inline void gen_op_evslw(TCGv_i32 ret, TCGv_i32 arg1, TCGv_i32 arg2)
57951c27 7802{
a7812ae4 7803 TCGv_i32 t0;
57951c27
AJ
7804 int l1, l2;
7805
7806 l1 = gen_new_label();
7807 l2 = gen_new_label();
a7812ae4 7808 t0 = tcg_temp_local_new_i32();
57951c27
AJ
7809 /* No error here: 6 bits are used */
7810 tcg_gen_andi_i32(t0, arg2, 0x3F);
7811 tcg_gen_brcondi_i32(TCG_COND_GE, t0, 32, l1);
7812 tcg_gen_shl_i32(ret, arg1, t0);
7813 tcg_gen_br(l2);
7814 gen_set_label(l1);
7815 tcg_gen_movi_i32(ret, 0);
e29ef9fa 7816 gen_set_label(l2);
a7812ae4 7817 tcg_temp_free_i32(t0);
57951c27
AJ
7818}
7819GEN_SPEOP_ARITH2(evslw, gen_op_evslw);
636aa200 7820static inline void gen_op_evrlw(TCGv_i32 ret, TCGv_i32 arg1, TCGv_i32 arg2)
57951c27 7821{
a7812ae4 7822 TCGv_i32 t0 = tcg_temp_new_i32();
57951c27
AJ
7823 tcg_gen_andi_i32(t0, arg2, 0x1F);
7824 tcg_gen_rotl_i32(ret, arg1, t0);
a7812ae4 7825 tcg_temp_free_i32(t0);
57951c27
AJ
7826}
7827GEN_SPEOP_ARITH2(evrlw, gen_op_evrlw);
636aa200 7828static inline void gen_evmergehi(DisasContext *ctx)
57951c27
AJ
7829{
7830 if (unlikely(!ctx->spe_enabled)) {
27a69bb0 7831 gen_exception(ctx, POWERPC_EXCP_SPEU);
57951c27
AJ
7832 return;
7833 }
7834#if defined(TARGET_PPC64)
a7812ae4
PB
7835 TCGv t0 = tcg_temp_new();
7836 TCGv t1 = tcg_temp_new();
57951c27
AJ
7837 tcg_gen_shri_tl(t0, cpu_gpr[rB(ctx->opcode)], 32);
7838 tcg_gen_andi_tl(t1, cpu_gpr[rA(ctx->opcode)], 0xFFFFFFFF0000000ULL);
7839 tcg_gen_or_tl(cpu_gpr[rD(ctx->opcode)], t0, t1);
7840 tcg_temp_free(t0);
7841 tcg_temp_free(t1);
7842#else
7843 tcg_gen_mov_i32(cpu_gpr[rD(ctx->opcode)], cpu_gprh[rB(ctx->opcode)]);
7844 tcg_gen_mov_i32(cpu_gprh[rD(ctx->opcode)], cpu_gprh[rA(ctx->opcode)]);
7845#endif
7846}
7847GEN_SPEOP_ARITH2(evaddw, tcg_gen_add_i32);
636aa200 7848static inline void gen_op_evsubf(TCGv_i32 ret, TCGv_i32 arg1, TCGv_i32 arg2)
0487d6a8 7849{
57951c27
AJ
7850 tcg_gen_sub_i32(ret, arg2, arg1);
7851}
7852GEN_SPEOP_ARITH2(evsubfw, gen_op_evsubf);
0487d6a8 7853
57951c27
AJ
7854/* SPE arithmetic immediate */
7855#if defined(TARGET_PPC64)
7856#define GEN_SPEOP_ARITH_IMM2(name, tcg_op) \
636aa200 7857static inline void gen_##name(DisasContext *ctx) \
57951c27
AJ
7858{ \
7859 if (unlikely(!ctx->spe_enabled)) { \
27a69bb0 7860 gen_exception(ctx, POWERPC_EXCP_SPEU); \
57951c27
AJ
7861 return; \
7862 } \
a7812ae4
PB
7863 TCGv_i32 t0 = tcg_temp_local_new_i32(); \
7864 TCGv_i32 t1 = tcg_temp_local_new_i32(); \
7865 TCGv_i64 t2 = tcg_temp_local_new_i64(); \
57951c27
AJ
7866 tcg_gen_trunc_i64_i32(t0, cpu_gpr[rB(ctx->opcode)]); \
7867 tcg_op(t0, t0, rA(ctx->opcode)); \
7868 tcg_gen_shri_i64(t2, cpu_gpr[rB(ctx->opcode)], 32); \
7869 tcg_gen_trunc_i64_i32(t1, t2); \
e06fcd75 7870 tcg_temp_free_i64(t2); \
57951c27
AJ
7871 tcg_op(t1, t1, rA(ctx->opcode)); \
7872 tcg_gen_concat_i32_i64(cpu_gpr[rD(ctx->opcode)], t0, t1); \
a7812ae4
PB
7873 tcg_temp_free_i32(t0); \
7874 tcg_temp_free_i32(t1); \
57951c27
AJ
7875}
7876#else
7877#define GEN_SPEOP_ARITH_IMM2(name, tcg_op) \
636aa200 7878static inline void gen_##name(DisasContext *ctx) \
57951c27
AJ
7879{ \
7880 if (unlikely(!ctx->spe_enabled)) { \
27a69bb0 7881 gen_exception(ctx, POWERPC_EXCP_SPEU); \
57951c27
AJ
7882 return; \
7883 } \
7884 tcg_op(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rB(ctx->opcode)], \
7885 rA(ctx->opcode)); \
7886 tcg_op(cpu_gprh[rD(ctx->opcode)], cpu_gprh[rB(ctx->opcode)], \
7887 rA(ctx->opcode)); \
7888}
7889#endif
7890GEN_SPEOP_ARITH_IMM2(evaddiw, tcg_gen_addi_i32);
7891GEN_SPEOP_ARITH_IMM2(evsubifw, tcg_gen_subi_i32);
7892
7893/* SPE comparison */
7894#if defined(TARGET_PPC64)
7895#define GEN_SPEOP_COMP(name, tcg_cond) \
636aa200 7896static inline void gen_##name(DisasContext *ctx) \
57951c27
AJ
7897{ \
7898 if (unlikely(!ctx->spe_enabled)) { \
27a69bb0 7899 gen_exception(ctx, POWERPC_EXCP_SPEU); \
57951c27
AJ
7900 return; \
7901 } \
7902 int l1 = gen_new_label(); \
7903 int l2 = gen_new_label(); \
7904 int l3 = gen_new_label(); \
7905 int l4 = gen_new_label(); \
a7812ae4
PB
7906 TCGv_i32 t0 = tcg_temp_local_new_i32(); \
7907 TCGv_i32 t1 = tcg_temp_local_new_i32(); \
7908 TCGv_i64 t2 = tcg_temp_local_new_i64(); \
57951c27
AJ
7909 tcg_gen_trunc_i64_i32(t0, cpu_gpr[rA(ctx->opcode)]); \
7910 tcg_gen_trunc_i64_i32(t1, cpu_gpr[rB(ctx->opcode)]); \
7911 tcg_gen_brcond_i32(tcg_cond, t0, t1, l1); \
a7812ae4 7912 tcg_gen_movi_i32(cpu_crf[crfD(ctx->opcode)], 0); \
57951c27
AJ
7913 tcg_gen_br(l2); \
7914 gen_set_label(l1); \
7915 tcg_gen_movi_i32(cpu_crf[crfD(ctx->opcode)], \
7916 CRF_CL | CRF_CH_OR_CL | CRF_CH_AND_CL); \
7917 gen_set_label(l2); \
7918 tcg_gen_shri_i64(t2, cpu_gpr[rA(ctx->opcode)], 32); \
7919 tcg_gen_trunc_i64_i32(t0, t2); \
7920 tcg_gen_shri_i64(t2, cpu_gpr[rB(ctx->opcode)], 32); \
7921 tcg_gen_trunc_i64_i32(t1, t2); \
a7812ae4 7922 tcg_temp_free_i64(t2); \
57951c27
AJ
7923 tcg_gen_brcond_i32(tcg_cond, t0, t1, l3); \
7924 tcg_gen_andi_i32(cpu_crf[crfD(ctx->opcode)], cpu_crf[crfD(ctx->opcode)], \
7925 ~(CRF_CH | CRF_CH_AND_CL)); \
7926 tcg_gen_br(l4); \
7927 gen_set_label(l3); \
7928 tcg_gen_ori_i32(cpu_crf[crfD(ctx->opcode)], cpu_crf[crfD(ctx->opcode)], \
7929 CRF_CH | CRF_CH_OR_CL); \
7930 gen_set_label(l4); \
a7812ae4
PB
7931 tcg_temp_free_i32(t0); \
7932 tcg_temp_free_i32(t1); \
57951c27
AJ
7933}
7934#else
7935#define GEN_SPEOP_COMP(name, tcg_cond) \
636aa200 7936static inline void gen_##name(DisasContext *ctx) \
57951c27
AJ
7937{ \
7938 if (unlikely(!ctx->spe_enabled)) { \
27a69bb0 7939 gen_exception(ctx, POWERPC_EXCP_SPEU); \
57951c27
AJ
7940 return; \
7941 } \
7942 int l1 = gen_new_label(); \
7943 int l2 = gen_new_label(); \
7944 int l3 = gen_new_label(); \
7945 int l4 = gen_new_label(); \
7946 \
7947 tcg_gen_brcond_i32(tcg_cond, cpu_gpr[rA(ctx->opcode)], \
7948 cpu_gpr[rB(ctx->opcode)], l1); \
7949 tcg_gen_movi_tl(cpu_crf[crfD(ctx->opcode)], 0); \
7950 tcg_gen_br(l2); \
7951 gen_set_label(l1); \
7952 tcg_gen_movi_i32(cpu_crf[crfD(ctx->opcode)], \
7953 CRF_CL | CRF_CH_OR_CL | CRF_CH_AND_CL); \
7954 gen_set_label(l2); \
7955 tcg_gen_brcond_i32(tcg_cond, cpu_gprh[rA(ctx->opcode)], \
7956 cpu_gprh[rB(ctx->opcode)], l3); \
7957 tcg_gen_andi_i32(cpu_crf[crfD(ctx->opcode)], cpu_crf[crfD(ctx->opcode)], \
7958 ~(CRF_CH | CRF_CH_AND_CL)); \
7959 tcg_gen_br(l4); \
7960 gen_set_label(l3); \
7961 tcg_gen_ori_i32(cpu_crf[crfD(ctx->opcode)], cpu_crf[crfD(ctx->opcode)], \
7962 CRF_CH | CRF_CH_OR_CL); \
7963 gen_set_label(l4); \
7964}
7965#endif
7966GEN_SPEOP_COMP(evcmpgtu, TCG_COND_GTU);
7967GEN_SPEOP_COMP(evcmpgts, TCG_COND_GT);
7968GEN_SPEOP_COMP(evcmpltu, TCG_COND_LTU);
7969GEN_SPEOP_COMP(evcmplts, TCG_COND_LT);
7970GEN_SPEOP_COMP(evcmpeq, TCG_COND_EQ);
7971
7972/* SPE misc */
636aa200 7973static inline void gen_brinc(DisasContext *ctx)
57951c27
AJ
7974{
7975 /* Note: brinc is usable even if SPE is disabled */
a7812ae4
PB
7976 gen_helper_brinc(cpu_gpr[rD(ctx->opcode)],
7977 cpu_gpr[rA(ctx->opcode)], cpu_gpr[rB(ctx->opcode)]);
0487d6a8 7978}
636aa200 7979static inline void gen_evmergelo(DisasContext *ctx)
57951c27
AJ
7980{
7981 if (unlikely(!ctx->spe_enabled)) {
27a69bb0 7982 gen_exception(ctx, POWERPC_EXCP_SPEU);
57951c27
AJ
7983 return;
7984 }
7985#if defined(TARGET_PPC64)
a7812ae4
PB
7986 TCGv t0 = tcg_temp_new();
7987 TCGv t1 = tcg_temp_new();
17d9b3af 7988 tcg_gen_ext32u_tl(t0, cpu_gpr[rB(ctx->opcode)]);
57951c27
AJ
7989 tcg_gen_shli_tl(t1, cpu_gpr[rA(ctx->opcode)], 32);
7990 tcg_gen_or_tl(cpu_gpr[rD(ctx->opcode)], t0, t1);
7991 tcg_temp_free(t0);
7992 tcg_temp_free(t1);
7993#else
57951c27 7994 tcg_gen_mov_i32(cpu_gprh[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)]);
33890b3e 7995 tcg_gen_mov_i32(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rB(ctx->opcode)]);
57951c27
AJ
7996#endif
7997}
636aa200 7998static inline void gen_evmergehilo(DisasContext *ctx)
57951c27
AJ
7999{
8000 if (unlikely(!ctx->spe_enabled)) {
27a69bb0 8001 gen_exception(ctx, POWERPC_EXCP_SPEU);
57951c27
AJ
8002 return;
8003 }
8004#if defined(TARGET_PPC64)
a7812ae4
PB
8005 TCGv t0 = tcg_temp_new();
8006 TCGv t1 = tcg_temp_new();
17d9b3af 8007 tcg_gen_ext32u_tl(t0, cpu_gpr[rB(ctx->opcode)]);
57951c27
AJ
8008 tcg_gen_andi_tl(t1, cpu_gpr[rA(ctx->opcode)], 0xFFFFFFFF0000000ULL);
8009 tcg_gen_or_tl(cpu_gpr[rD(ctx->opcode)], t0, t1);
8010 tcg_temp_free(t0);
8011 tcg_temp_free(t1);
8012#else
8013 tcg_gen_mov_i32(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rB(ctx->opcode)]);
8014 tcg_gen_mov_i32(cpu_gprh[rD(ctx->opcode)], cpu_gprh[rA(ctx->opcode)]);
8015#endif
8016}
636aa200 8017static inline void gen_evmergelohi(DisasContext *ctx)
57951c27
AJ
8018{
8019 if (unlikely(!ctx->spe_enabled)) {
27a69bb0 8020 gen_exception(ctx, POWERPC_EXCP_SPEU);
57951c27
AJ
8021 return;
8022 }
8023#if defined(TARGET_PPC64)
a7812ae4
PB
8024 TCGv t0 = tcg_temp_new();
8025 TCGv t1 = tcg_temp_new();
57951c27
AJ
8026 tcg_gen_shri_tl(t0, cpu_gpr[rB(ctx->opcode)], 32);
8027 tcg_gen_shli_tl(t1, cpu_gpr[rA(ctx->opcode)], 32);
8028 tcg_gen_or_tl(cpu_gpr[rD(ctx->opcode)], t0, t1);
8029 tcg_temp_free(t0);
8030 tcg_temp_free(t1);
8031#else
33890b3e
NF
8032 if (rD(ctx->opcode) == rA(ctx->opcode)) {
8033 TCGv_i32 tmp = tcg_temp_new_i32();
8034 tcg_gen_mov_i32(tmp, cpu_gpr[rA(ctx->opcode)]);
8035 tcg_gen_mov_i32(cpu_gpr[rD(ctx->opcode)], cpu_gprh[rB(ctx->opcode)]);
8036 tcg_gen_mov_i32(cpu_gprh[rD(ctx->opcode)], tmp);
8037 tcg_temp_free_i32(tmp);
8038 } else {
8039 tcg_gen_mov_i32(cpu_gpr[rD(ctx->opcode)], cpu_gprh[rB(ctx->opcode)]);
8040 tcg_gen_mov_i32(cpu_gprh[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)]);
8041 }
57951c27
AJ
8042#endif
8043}
636aa200 8044static inline void gen_evsplati(DisasContext *ctx)
57951c27 8045{
ae01847f 8046 uint64_t imm = ((int32_t)(rA(ctx->opcode) << 27)) >> 27;
0487d6a8 8047
57951c27 8048#if defined(TARGET_PPC64)
38d14952 8049 tcg_gen_movi_tl(cpu_gpr[rD(ctx->opcode)], (imm << 32) | imm);
57951c27
AJ
8050#else
8051 tcg_gen_movi_i32(cpu_gpr[rD(ctx->opcode)], imm);
8052 tcg_gen_movi_i32(cpu_gprh[rD(ctx->opcode)], imm);
8053#endif
8054}
636aa200 8055static inline void gen_evsplatfi(DisasContext *ctx)
0487d6a8 8056{
ae01847f 8057 uint64_t imm = rA(ctx->opcode) << 27;
0487d6a8 8058
57951c27 8059#if defined(TARGET_PPC64)
38d14952 8060 tcg_gen_movi_tl(cpu_gpr[rD(ctx->opcode)], (imm << 32) | imm);
57951c27
AJ
8061#else
8062 tcg_gen_movi_i32(cpu_gpr[rD(ctx->opcode)], imm);
8063 tcg_gen_movi_i32(cpu_gprh[rD(ctx->opcode)], imm);
8064#endif
0487d6a8
JM
8065}
8066
636aa200 8067static inline void gen_evsel(DisasContext *ctx)
57951c27
AJ
8068{
8069 int l1 = gen_new_label();
8070 int l2 = gen_new_label();
8071 int l3 = gen_new_label();
8072 int l4 = gen_new_label();
a7812ae4 8073 TCGv_i32 t0 = tcg_temp_local_new_i32();
57951c27 8074#if defined(TARGET_PPC64)
a7812ae4
PB
8075 TCGv t1 = tcg_temp_local_new();
8076 TCGv t2 = tcg_temp_local_new();
57951c27
AJ
8077#endif
8078 tcg_gen_andi_i32(t0, cpu_crf[ctx->opcode & 0x07], 1 << 3);
8079 tcg_gen_brcondi_i32(TCG_COND_EQ, t0, 0, l1);
8080#if defined(TARGET_PPC64)
8081 tcg_gen_andi_tl(t1, cpu_gpr[rA(ctx->opcode)], 0xFFFFFFFF00000000ULL);
8082#else
8083 tcg_gen_mov_tl(cpu_gprh[rD(ctx->opcode)], cpu_gprh[rA(ctx->opcode)]);
8084#endif
8085 tcg_gen_br(l2);
8086 gen_set_label(l1);
8087#if defined(TARGET_PPC64)
8088 tcg_gen_andi_tl(t1, cpu_gpr[rB(ctx->opcode)], 0xFFFFFFFF00000000ULL);
8089#else
8090 tcg_gen_mov_tl(cpu_gprh[rD(ctx->opcode)], cpu_gprh[rB(ctx->opcode)]);
8091#endif
8092 gen_set_label(l2);
8093 tcg_gen_andi_i32(t0, cpu_crf[ctx->opcode & 0x07], 1 << 2);
8094 tcg_gen_brcondi_i32(TCG_COND_EQ, t0, 0, l3);
8095#if defined(TARGET_PPC64)
17d9b3af 8096 tcg_gen_ext32u_tl(t2, cpu_gpr[rA(ctx->opcode)]);
57951c27
AJ
8097#else
8098 tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)]);
8099#endif
8100 tcg_gen_br(l4);
8101 gen_set_label(l3);
8102#if defined(TARGET_PPC64)
17d9b3af 8103 tcg_gen_ext32u_tl(t2, cpu_gpr[rB(ctx->opcode)]);
57951c27
AJ
8104#else
8105 tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rB(ctx->opcode)]);
8106#endif
8107 gen_set_label(l4);
a7812ae4 8108 tcg_temp_free_i32(t0);
57951c27
AJ
8109#if defined(TARGET_PPC64)
8110 tcg_gen_or_tl(cpu_gpr[rD(ctx->opcode)], t1, t2);
8111 tcg_temp_free(t1);
8112 tcg_temp_free(t2);
8113#endif
8114}
e8eaa2c0
BS
8115
8116static void gen_evsel0(DisasContext *ctx)
57951c27
AJ
8117{
8118 gen_evsel(ctx);
8119}
e8eaa2c0
BS
8120
8121static void gen_evsel1(DisasContext *ctx)
57951c27
AJ
8122{
8123 gen_evsel(ctx);
8124}
e8eaa2c0
BS
8125
8126static void gen_evsel2(DisasContext *ctx)
57951c27
AJ
8127{
8128 gen_evsel(ctx);
8129}
e8eaa2c0
BS
8130
8131static void gen_evsel3(DisasContext *ctx)
57951c27
AJ
8132{
8133 gen_evsel(ctx);
8134}
0487d6a8 8135
a0e13900
FC
8136/* Multiply */
8137
8138static inline void gen_evmwumi(DisasContext *ctx)
8139{
8140 TCGv_i64 t0, t1;
8141
8142 if (unlikely(!ctx->spe_enabled)) {
27a69bb0 8143 gen_exception(ctx, POWERPC_EXCP_SPEU);
a0e13900
FC
8144 return;
8145 }
8146
8147 t0 = tcg_temp_new_i64();
8148 t1 = tcg_temp_new_i64();
8149
8150 /* t0 := rA; t1 := rB */
8151#if defined(TARGET_PPC64)
8152 tcg_gen_ext32u_tl(t0, cpu_gpr[rA(ctx->opcode)]);
8153 tcg_gen_ext32u_tl(t1, cpu_gpr[rB(ctx->opcode)]);
8154#else
8155 tcg_gen_extu_tl_i64(t0, cpu_gpr[rA(ctx->opcode)]);
8156 tcg_gen_extu_tl_i64(t1, cpu_gpr[rB(ctx->opcode)]);
8157#endif
8158
8159 tcg_gen_mul_i64(t0, t0, t1); /* t0 := rA * rB */
8160
8161 gen_store_gpr64(rD(ctx->opcode), t0); /* rD := t0 */
8162
8163 tcg_temp_free_i64(t0);
8164 tcg_temp_free_i64(t1);
8165}
8166
8167static inline void gen_evmwumia(DisasContext *ctx)
8168{
8169 TCGv_i64 tmp;
8170
8171 if (unlikely(!ctx->spe_enabled)) {
27a69bb0 8172 gen_exception(ctx, POWERPC_EXCP_SPEU);
a0e13900
FC
8173 return;
8174 }
8175
8176 gen_evmwumi(ctx); /* rD := rA * rB */
8177
8178 tmp = tcg_temp_new_i64();
8179
8180 /* acc := rD */
8181 gen_load_gpr64(tmp, rD(ctx->opcode));
1328c2bf 8182 tcg_gen_st_i64(tmp, cpu_env, offsetof(CPUPPCState, spe_acc));
a0e13900
FC
8183 tcg_temp_free_i64(tmp);
8184}
8185
8186static inline void gen_evmwumiaa(DisasContext *ctx)
8187{
8188 TCGv_i64 acc;
8189 TCGv_i64 tmp;
8190
8191 if (unlikely(!ctx->spe_enabled)) {
27a69bb0 8192 gen_exception(ctx, POWERPC_EXCP_SPEU);
a0e13900
FC
8193 return;
8194 }
8195
8196 gen_evmwumi(ctx); /* rD := rA * rB */
8197
8198 acc = tcg_temp_new_i64();
8199 tmp = tcg_temp_new_i64();
8200
8201 /* tmp := rD */
8202 gen_load_gpr64(tmp, rD(ctx->opcode));
8203
8204 /* Load acc */
1328c2bf 8205 tcg_gen_ld_i64(acc, cpu_env, offsetof(CPUPPCState, spe_acc));
a0e13900
FC
8206
8207 /* acc := tmp + acc */
8208 tcg_gen_add_i64(acc, acc, tmp);
8209
8210 /* Store acc */
1328c2bf 8211 tcg_gen_st_i64(acc, cpu_env, offsetof(CPUPPCState, spe_acc));
a0e13900
FC
8212
8213 /* rD := acc */
8214 gen_store_gpr64(rD(ctx->opcode), acc);
8215
8216 tcg_temp_free_i64(acc);
8217 tcg_temp_free_i64(tmp);
8218}
8219
8220static inline void gen_evmwsmi(DisasContext *ctx)
8221{
8222 TCGv_i64 t0, t1;
8223
8224 if (unlikely(!ctx->spe_enabled)) {
27a69bb0 8225 gen_exception(ctx, POWERPC_EXCP_SPEU);
a0e13900
FC
8226 return;
8227 }
8228
8229 t0 = tcg_temp_new_i64();
8230 t1 = tcg_temp_new_i64();
8231
8232 /* t0 := rA; t1 := rB */
8233#if defined(TARGET_PPC64)
8234 tcg_gen_ext32s_tl(t0, cpu_gpr[rA(ctx->opcode)]);
8235 tcg_gen_ext32s_tl(t1, cpu_gpr[rB(ctx->opcode)]);
8236#else
8237 tcg_gen_ext_tl_i64(t0, cpu_gpr[rA(ctx->opcode)]);
8238 tcg_gen_ext_tl_i64(t1, cpu_gpr[rB(ctx->opcode)]);
8239#endif
8240
8241 tcg_gen_mul_i64(t0, t0, t1); /* t0 := rA * rB */
8242
8243 gen_store_gpr64(rD(ctx->opcode), t0); /* rD := t0 */
8244
8245 tcg_temp_free_i64(t0);
8246 tcg_temp_free_i64(t1);
8247}
8248
8249static inline void gen_evmwsmia(DisasContext *ctx)
8250{
8251 TCGv_i64 tmp;
8252
8253 gen_evmwsmi(ctx); /* rD := rA * rB */
8254
8255 tmp = tcg_temp_new_i64();
8256
8257 /* acc := rD */
8258 gen_load_gpr64(tmp, rD(ctx->opcode));
1328c2bf 8259 tcg_gen_st_i64(tmp, cpu_env, offsetof(CPUPPCState, spe_acc));
a0e13900
FC
8260
8261 tcg_temp_free_i64(tmp);
8262}
8263
8264static inline void gen_evmwsmiaa(DisasContext *ctx)
8265{
8266 TCGv_i64 acc = tcg_temp_new_i64();
8267 TCGv_i64 tmp = tcg_temp_new_i64();
8268
8269 gen_evmwsmi(ctx); /* rD := rA * rB */
8270
8271 acc = tcg_temp_new_i64();
8272 tmp = tcg_temp_new_i64();
8273
8274 /* tmp := rD */
8275 gen_load_gpr64(tmp, rD(ctx->opcode));
8276
8277 /* Load acc */
1328c2bf 8278 tcg_gen_ld_i64(acc, cpu_env, offsetof(CPUPPCState, spe_acc));
a0e13900
FC
8279
8280 /* acc := tmp + acc */
8281 tcg_gen_add_i64(acc, acc, tmp);
8282
8283 /* Store acc */
1328c2bf 8284 tcg_gen_st_i64(acc, cpu_env, offsetof(CPUPPCState, spe_acc));
a0e13900
FC
8285
8286 /* rD := acc */
8287 gen_store_gpr64(rD(ctx->opcode), acc);
8288
8289 tcg_temp_free_i64(acc);
8290 tcg_temp_free_i64(tmp);
8291}
8292
70560da7
FC
8293GEN_SPE(evaddw, speundef, 0x00, 0x08, 0x00000000, 0xFFFFFFFF, PPC_SPE); ////
8294GEN_SPE(evaddiw, speundef, 0x01, 0x08, 0x00000000, 0xFFFFFFFF, PPC_SPE);
8295GEN_SPE(evsubfw, speundef, 0x02, 0x08, 0x00000000, 0xFFFFFFFF, PPC_SPE); ////
8296GEN_SPE(evsubifw, speundef, 0x03, 0x08, 0x00000000, 0xFFFFFFFF, PPC_SPE);
8297GEN_SPE(evabs, evneg, 0x04, 0x08, 0x0000F800, 0x0000F800, PPC_SPE); ////
8298GEN_SPE(evextsb, evextsh, 0x05, 0x08, 0x0000F800, 0x0000F800, PPC_SPE); ////
8299GEN_SPE(evrndw, evcntlzw, 0x06, 0x08, 0x0000F800, 0x0000F800, PPC_SPE); ////
8300GEN_SPE(evcntlsw, brinc, 0x07, 0x08, 0x0000F800, 0x00000000, PPC_SPE); //
8301GEN_SPE(evmra, speundef, 0x02, 0x13, 0x0000F800, 0xFFFFFFFF, PPC_SPE);
8302GEN_SPE(speundef, evand, 0x08, 0x08, 0xFFFFFFFF, 0x00000000, PPC_SPE); ////
8303GEN_SPE(evandc, speundef, 0x09, 0x08, 0x00000000, 0xFFFFFFFF, PPC_SPE); ////
8304GEN_SPE(evxor, evor, 0x0B, 0x08, 0x00000000, 0x00000000, PPC_SPE); ////
8305GEN_SPE(evnor, eveqv, 0x0C, 0x08, 0x00000000, 0x00000000, PPC_SPE); ////
8306GEN_SPE(evmwumi, evmwsmi, 0x0C, 0x11, 0x00000000, 0x00000000, PPC_SPE);
8307GEN_SPE(evmwumia, evmwsmia, 0x1C, 0x11, 0x00000000, 0x00000000, PPC_SPE);
8308GEN_SPE(evmwumiaa, evmwsmiaa, 0x0C, 0x15, 0x00000000, 0x00000000, PPC_SPE);
8309GEN_SPE(speundef, evorc, 0x0D, 0x08, 0xFFFFFFFF, 0x00000000, PPC_SPE); ////
8310GEN_SPE(evnand, speundef, 0x0F, 0x08, 0x00000000, 0xFFFFFFFF, PPC_SPE); ////
8311GEN_SPE(evsrwu, evsrws, 0x10, 0x08, 0x00000000, 0x00000000, PPC_SPE); ////
8312GEN_SPE(evsrwiu, evsrwis, 0x11, 0x08, 0x00000000, 0x00000000, PPC_SPE);
8313GEN_SPE(evslw, speundef, 0x12, 0x08, 0x00000000, 0xFFFFFFFF, PPC_SPE); ////
8314GEN_SPE(evslwi, speundef, 0x13, 0x08, 0x00000000, 0xFFFFFFFF, PPC_SPE);
8315GEN_SPE(evrlw, evsplati, 0x14, 0x08, 0x00000000, 0x0000F800, PPC_SPE); //
8316GEN_SPE(evrlwi, evsplatfi, 0x15, 0x08, 0x00000000, 0x0000F800, PPC_SPE);
8317GEN_SPE(evmergehi, evmergelo, 0x16, 0x08, 0x00000000, 0x00000000, PPC_SPE); ////
8318GEN_SPE(evmergehilo, evmergelohi, 0x17, 0x08, 0x00000000, 0x00000000, PPC_SPE); ////
8319GEN_SPE(evcmpgtu, evcmpgts, 0x18, 0x08, 0x00600000, 0x00600000, PPC_SPE); ////
8320GEN_SPE(evcmpltu, evcmplts, 0x19, 0x08, 0x00600000, 0x00600000, PPC_SPE); ////
8321GEN_SPE(evcmpeq, speundef, 0x1A, 0x08, 0x00600000, 0xFFFFFFFF, PPC_SPE); ////
0487d6a8 8322
6a6ae23f 8323/* SPE load and stores */
636aa200 8324static inline void gen_addr_spe_imm_index(DisasContext *ctx, TCGv EA, int sh)
6a6ae23f
AJ
8325{
8326 target_ulong uimm = rB(ctx->opcode);
8327
76db3ba4 8328 if (rA(ctx->opcode) == 0) {
6a6ae23f 8329 tcg_gen_movi_tl(EA, uimm << sh);
76db3ba4 8330 } else {
6a6ae23f 8331 tcg_gen_addi_tl(EA, cpu_gpr[rA(ctx->opcode)], uimm << sh);
c791fe84 8332 if (NARROW_MODE(ctx)) {
76db3ba4
AJ
8333 tcg_gen_ext32u_tl(EA, EA);
8334 }
76db3ba4 8335 }
0487d6a8 8336}
6a6ae23f 8337
636aa200 8338static inline void gen_op_evldd(DisasContext *ctx, TCGv addr)
6a6ae23f
AJ
8339{
8340#if defined(TARGET_PPC64)
76db3ba4 8341 gen_qemu_ld64(ctx, cpu_gpr[rD(ctx->opcode)], addr);
6a6ae23f
AJ
8342#else
8343 TCGv_i64 t0 = tcg_temp_new_i64();
76db3ba4 8344 gen_qemu_ld64(ctx, t0, addr);
6a6ae23f
AJ
8345 tcg_gen_trunc_i64_i32(cpu_gpr[rD(ctx->opcode)], t0);
8346 tcg_gen_shri_i64(t0, t0, 32);
8347 tcg_gen_trunc_i64_i32(cpu_gprh[rD(ctx->opcode)], t0);
8348 tcg_temp_free_i64(t0);
8349#endif
0487d6a8 8350}
6a6ae23f 8351
636aa200 8352static inline void gen_op_evldw(DisasContext *ctx, TCGv addr)
6a6ae23f 8353{
0487d6a8 8354#if defined(TARGET_PPC64)
6a6ae23f 8355 TCGv t0 = tcg_temp_new();
76db3ba4 8356 gen_qemu_ld32u(ctx, t0, addr);
6a6ae23f 8357 tcg_gen_shli_tl(cpu_gpr[rD(ctx->opcode)], t0, 32);
76db3ba4
AJ
8358 gen_addr_add(ctx, addr, addr, 4);
8359 gen_qemu_ld32u(ctx, t0, addr);
6a6ae23f
AJ
8360 tcg_gen_or_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rD(ctx->opcode)], t0);
8361 tcg_temp_free(t0);
8362#else
76db3ba4
AJ
8363 gen_qemu_ld32u(ctx, cpu_gprh[rD(ctx->opcode)], addr);
8364 gen_addr_add(ctx, addr, addr, 4);
8365 gen_qemu_ld32u(ctx, cpu_gpr[rD(ctx->opcode)], addr);
6a6ae23f 8366#endif
0487d6a8 8367}
6a6ae23f 8368
636aa200 8369static inline void gen_op_evldh(DisasContext *ctx, TCGv addr)
6a6ae23f
AJ
8370{
8371 TCGv t0 = tcg_temp_new();
8372#if defined(TARGET_PPC64)
76db3ba4 8373 gen_qemu_ld16u(ctx, t0, addr);
6a6ae23f 8374 tcg_gen_shli_tl(cpu_gpr[rD(ctx->opcode)], t0, 48);
76db3ba4
AJ
8375 gen_addr_add(ctx, addr, addr, 2);
8376 gen_qemu_ld16u(ctx, t0, addr);
6a6ae23f
AJ
8377 tcg_gen_shli_tl(t0, t0, 32);
8378 tcg_gen_or_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rD(ctx->opcode)], t0);
76db3ba4
AJ
8379 gen_addr_add(ctx, addr, addr, 2);
8380 gen_qemu_ld16u(ctx, t0, addr);
6a6ae23f
AJ
8381 tcg_gen_shli_tl(t0, t0, 16);
8382 tcg_gen_or_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rD(ctx->opcode)], t0);
76db3ba4
AJ
8383 gen_addr_add(ctx, addr, addr, 2);
8384 gen_qemu_ld16u(ctx, t0, addr);
6a6ae23f 8385 tcg_gen_or_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rD(ctx->opcode)], t0);
0487d6a8 8386#else
76db3ba4 8387 gen_qemu_ld16u(ctx, t0, addr);
6a6ae23f 8388 tcg_gen_shli_tl(cpu_gprh[rD(ctx->opcode)], t0, 16);
76db3ba4
AJ
8389 gen_addr_add(ctx, addr, addr, 2);
8390 gen_qemu_ld16u(ctx, t0, addr);
6a6ae23f 8391 tcg_gen_or_tl(cpu_gprh[rD(ctx->opcode)], cpu_gprh[rD(ctx->opcode)], t0);
76db3ba4
AJ
8392 gen_addr_add(ctx, addr, addr, 2);
8393 gen_qemu_ld16u(ctx, t0, addr);
6a6ae23f 8394 tcg_gen_shli_tl(cpu_gprh[rD(ctx->opcode)], t0, 16);
76db3ba4
AJ
8395 gen_addr_add(ctx, addr, addr, 2);
8396 gen_qemu_ld16u(ctx, t0, addr);
6a6ae23f 8397 tcg_gen_or_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rD(ctx->opcode)], t0);
0487d6a8 8398#endif
6a6ae23f 8399 tcg_temp_free(t0);
0487d6a8
JM
8400}
8401
636aa200 8402static inline void gen_op_evlhhesplat(DisasContext *ctx, TCGv addr)
6a6ae23f
AJ
8403{
8404 TCGv t0 = tcg_temp_new();
76db3ba4 8405 gen_qemu_ld16u(ctx, t0, addr);
6a6ae23f
AJ
8406#if defined(TARGET_PPC64)
8407 tcg_gen_shli_tl(cpu_gpr[rD(ctx->opcode)], t0, 48);
8408 tcg_gen_shli_tl(t0, t0, 16);
8409 tcg_gen_or_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rD(ctx->opcode)], t0);
8410#else
8411 tcg_gen_shli_tl(t0, t0, 16);
8412 tcg_gen_mov_tl(cpu_gprh[rD(ctx->opcode)], t0);
8413 tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], t0);
8414#endif
8415 tcg_temp_free(t0);
0487d6a8
JM
8416}
8417
636aa200 8418static inline void gen_op_evlhhousplat(DisasContext *ctx, TCGv addr)
6a6ae23f
AJ
8419{
8420 TCGv t0 = tcg_temp_new();
76db3ba4 8421 gen_qemu_ld16u(ctx, t0, addr);
6a6ae23f
AJ
8422#if defined(TARGET_PPC64)
8423 tcg_gen_shli_tl(cpu_gpr[rD(ctx->opcode)], t0, 32);
8424 tcg_gen_or_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rD(ctx->opcode)], t0);
8425#else
8426 tcg_gen_mov_tl(cpu_gprh[rD(ctx->opcode)], t0);
8427 tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], t0);
8428#endif
8429 tcg_temp_free(t0);
0487d6a8
JM
8430}
8431
636aa200 8432static inline void gen_op_evlhhossplat(DisasContext *ctx, TCGv addr)
6a6ae23f
AJ
8433{
8434 TCGv t0 = tcg_temp_new();
76db3ba4 8435 gen_qemu_ld16s(ctx, t0, addr);
6a6ae23f
AJ
8436#if defined(TARGET_PPC64)
8437 tcg_gen_shli_tl(cpu_gpr[rD(ctx->opcode)], t0, 32);
8438 tcg_gen_ext32u_tl(t0, t0);
8439 tcg_gen_or_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rD(ctx->opcode)], t0);
8440#else
8441 tcg_gen_mov_tl(cpu_gprh[rD(ctx->opcode)], t0);
8442 tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], t0);
8443#endif
8444 tcg_temp_free(t0);
8445}
8446
636aa200 8447static inline void gen_op_evlwhe(DisasContext *ctx, TCGv addr)
6a6ae23f
AJ
8448{
8449 TCGv t0 = tcg_temp_new();
8450#if defined(TARGET_PPC64)
76db3ba4 8451 gen_qemu_ld16u(ctx, t0, addr);
6a6ae23f 8452 tcg_gen_shli_tl(cpu_gpr[rD(ctx->opcode)], t0, 48);
76db3ba4
AJ
8453 gen_addr_add(ctx, addr, addr, 2);
8454 gen_qemu_ld16u(ctx, t0, addr);
6a6ae23f
AJ
8455 tcg_gen_shli_tl(t0, t0, 16);
8456 tcg_gen_or_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rD(ctx->opcode)], t0);
8457#else
76db3ba4 8458 gen_qemu_ld16u(ctx, t0, addr);
6a6ae23f 8459 tcg_gen_shli_tl(cpu_gprh[rD(ctx->opcode)], t0, 16);
76db3ba4
AJ
8460 gen_addr_add(ctx, addr, addr, 2);
8461 gen_qemu_ld16u(ctx, t0, addr);
6a6ae23f
AJ
8462 tcg_gen_shli_tl(cpu_gpr[rD(ctx->opcode)], t0, 16);
8463#endif
8464 tcg_temp_free(t0);
8465}
8466
636aa200 8467static inline void gen_op_evlwhou(DisasContext *ctx, TCGv addr)
6a6ae23f
AJ
8468{
8469#if defined(TARGET_PPC64)
8470 TCGv t0 = tcg_temp_new();
76db3ba4
AJ
8471 gen_qemu_ld16u(ctx, cpu_gpr[rD(ctx->opcode)], addr);
8472 gen_addr_add(ctx, addr, addr, 2);
8473 gen_qemu_ld16u(ctx, t0, addr);
6a6ae23f
AJ
8474 tcg_gen_shli_tl(t0, t0, 32);
8475 tcg_gen_or_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rD(ctx->opcode)], t0);
8476 tcg_temp_free(t0);
8477#else
76db3ba4
AJ
8478 gen_qemu_ld16u(ctx, cpu_gprh[rD(ctx->opcode)], addr);
8479 gen_addr_add(ctx, addr, addr, 2);
8480 gen_qemu_ld16u(ctx, cpu_gpr[rD(ctx->opcode)], addr);
6a6ae23f
AJ
8481#endif
8482}
8483
636aa200 8484static inline void gen_op_evlwhos(DisasContext *ctx, TCGv addr)
6a6ae23f
AJ
8485{
8486#if defined(TARGET_PPC64)
8487 TCGv t0 = tcg_temp_new();
76db3ba4 8488 gen_qemu_ld16s(ctx, t0, addr);
6a6ae23f 8489 tcg_gen_ext32u_tl(cpu_gpr[rD(ctx->opcode)], t0);
76db3ba4
AJ
8490 gen_addr_add(ctx, addr, addr, 2);
8491 gen_qemu_ld16s(ctx, t0, addr);
6a6ae23f
AJ
8492 tcg_gen_shli_tl(t0, t0, 32);
8493 tcg_gen_or_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rD(ctx->opcode)], t0);
8494 tcg_temp_free(t0);
8495#else
76db3ba4
AJ
8496 gen_qemu_ld16s(ctx, cpu_gprh[rD(ctx->opcode)], addr);
8497 gen_addr_add(ctx, addr, addr, 2);
8498 gen_qemu_ld16s(ctx, cpu_gpr[rD(ctx->opcode)], addr);
6a6ae23f
AJ
8499#endif
8500}
8501
636aa200 8502static inline void gen_op_evlwwsplat(DisasContext *ctx, TCGv addr)
6a6ae23f
AJ
8503{
8504 TCGv t0 = tcg_temp_new();
76db3ba4 8505 gen_qemu_ld32u(ctx, t0, addr);
0487d6a8 8506#if defined(TARGET_PPC64)
6a6ae23f
AJ
8507 tcg_gen_shli_tl(cpu_gpr[rD(ctx->opcode)], t0, 32);
8508 tcg_gen_or_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rD(ctx->opcode)], t0);
8509#else
8510 tcg_gen_mov_tl(cpu_gprh[rD(ctx->opcode)], t0);
8511 tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], t0);
8512#endif
8513 tcg_temp_free(t0);
8514}
8515
636aa200 8516static inline void gen_op_evlwhsplat(DisasContext *ctx, TCGv addr)
6a6ae23f
AJ
8517{
8518 TCGv t0 = tcg_temp_new();
8519#if defined(TARGET_PPC64)
76db3ba4 8520 gen_qemu_ld16u(ctx, t0, addr);
6a6ae23f
AJ
8521 tcg_gen_shli_tl(cpu_gpr[rD(ctx->opcode)], t0, 48);
8522 tcg_gen_shli_tl(t0, t0, 32);
8523 tcg_gen_or_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rD(ctx->opcode)], t0);
76db3ba4
AJ
8524 gen_addr_add(ctx, addr, addr, 2);
8525 gen_qemu_ld16u(ctx, t0, addr);
6a6ae23f
AJ
8526 tcg_gen_or_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rD(ctx->opcode)], t0);
8527 tcg_gen_shli_tl(t0, t0, 16);
8528 tcg_gen_or_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rD(ctx->opcode)], t0);
8529#else
76db3ba4 8530 gen_qemu_ld16u(ctx, t0, addr);
6a6ae23f
AJ
8531 tcg_gen_shli_tl(cpu_gprh[rD(ctx->opcode)], t0, 16);
8532 tcg_gen_or_tl(cpu_gprh[rD(ctx->opcode)], cpu_gprh[rD(ctx->opcode)], t0);
76db3ba4
AJ
8533 gen_addr_add(ctx, addr, addr, 2);
8534 gen_qemu_ld16u(ctx, t0, addr);
6a6ae23f
AJ
8535 tcg_gen_shli_tl(cpu_gpr[rD(ctx->opcode)], t0, 16);
8536 tcg_gen_or_tl(cpu_gpr[rD(ctx->opcode)], cpu_gprh[rD(ctx->opcode)], t0);
0487d6a8 8537#endif
6a6ae23f
AJ
8538 tcg_temp_free(t0);
8539}
8540
636aa200 8541static inline void gen_op_evstdd(DisasContext *ctx, TCGv addr)
6a6ae23f
AJ
8542{
8543#if defined(TARGET_PPC64)
76db3ba4 8544 gen_qemu_st64(ctx, cpu_gpr[rS(ctx->opcode)], addr);
0487d6a8 8545#else
6a6ae23f
AJ
8546 TCGv_i64 t0 = tcg_temp_new_i64();
8547 tcg_gen_concat_i32_i64(t0, cpu_gpr[rS(ctx->opcode)], cpu_gprh[rS(ctx->opcode)]);
76db3ba4 8548 gen_qemu_st64(ctx, t0, addr);
6a6ae23f
AJ
8549 tcg_temp_free_i64(t0);
8550#endif
8551}
8552
636aa200 8553static inline void gen_op_evstdw(DisasContext *ctx, TCGv addr)
6a6ae23f 8554{
0487d6a8 8555#if defined(TARGET_PPC64)
6a6ae23f
AJ
8556 TCGv t0 = tcg_temp_new();
8557 tcg_gen_shri_tl(t0, cpu_gpr[rS(ctx->opcode)], 32);
76db3ba4 8558 gen_qemu_st32(ctx, t0, addr);
6a6ae23f
AJ
8559 tcg_temp_free(t0);
8560#else
76db3ba4 8561 gen_qemu_st32(ctx, cpu_gprh[rS(ctx->opcode)], addr);
6a6ae23f 8562#endif
76db3ba4
AJ
8563 gen_addr_add(ctx, addr, addr, 4);
8564 gen_qemu_st32(ctx, cpu_gpr[rS(ctx->opcode)], addr);
6a6ae23f
AJ
8565}
8566
636aa200 8567static inline void gen_op_evstdh(DisasContext *ctx, TCGv addr)
6a6ae23f
AJ
8568{
8569 TCGv t0 = tcg_temp_new();
8570#if defined(TARGET_PPC64)
8571 tcg_gen_shri_tl(t0, cpu_gpr[rS(ctx->opcode)], 48);
8572#else
8573 tcg_gen_shri_tl(t0, cpu_gprh[rS(ctx->opcode)], 16);
8574#endif
76db3ba4
AJ
8575 gen_qemu_st16(ctx, t0, addr);
8576 gen_addr_add(ctx, addr, addr, 2);
6a6ae23f
AJ
8577#if defined(TARGET_PPC64)
8578 tcg_gen_shri_tl(t0, cpu_gpr[rS(ctx->opcode)], 32);
76db3ba4 8579 gen_qemu_st16(ctx, t0, addr);
6a6ae23f 8580#else
76db3ba4 8581 gen_qemu_st16(ctx, cpu_gprh[rS(ctx->opcode)], addr);
6a6ae23f 8582#endif
76db3ba4 8583 gen_addr_add(ctx, addr, addr, 2);
6a6ae23f 8584 tcg_gen_shri_tl(t0, cpu_gpr[rS(ctx->opcode)], 16);
76db3ba4 8585 gen_qemu_st16(ctx, t0, addr);
6a6ae23f 8586 tcg_temp_free(t0);
76db3ba4
AJ
8587 gen_addr_add(ctx, addr, addr, 2);
8588 gen_qemu_st16(ctx, cpu_gpr[rS(ctx->opcode)], addr);
6a6ae23f
AJ
8589}
8590
636aa200 8591static inline void gen_op_evstwhe(DisasContext *ctx, TCGv addr)
6a6ae23f
AJ
8592{
8593 TCGv t0 = tcg_temp_new();
8594#if defined(TARGET_PPC64)
8595 tcg_gen_shri_tl(t0, cpu_gpr[rS(ctx->opcode)], 48);
8596#else
8597 tcg_gen_shri_tl(t0, cpu_gprh[rS(ctx->opcode)], 16);
8598#endif
76db3ba4
AJ
8599 gen_qemu_st16(ctx, t0, addr);
8600 gen_addr_add(ctx, addr, addr, 2);
6a6ae23f 8601 tcg_gen_shri_tl(t0, cpu_gpr[rS(ctx->opcode)], 16);
76db3ba4 8602 gen_qemu_st16(ctx, t0, addr);
6a6ae23f
AJ
8603 tcg_temp_free(t0);
8604}
8605
636aa200 8606static inline void gen_op_evstwho(DisasContext *ctx, TCGv addr)
6a6ae23f
AJ
8607{
8608#if defined(TARGET_PPC64)
8609 TCGv t0 = tcg_temp_new();
8610 tcg_gen_shri_tl(t0, cpu_gpr[rS(ctx->opcode)], 32);
76db3ba4 8611 gen_qemu_st16(ctx, t0, addr);
6a6ae23f
AJ
8612 tcg_temp_free(t0);
8613#else
76db3ba4 8614 gen_qemu_st16(ctx, cpu_gprh[rS(ctx->opcode)], addr);
6a6ae23f 8615#endif
76db3ba4
AJ
8616 gen_addr_add(ctx, addr, addr, 2);
8617 gen_qemu_st16(ctx, cpu_gpr[rS(ctx->opcode)], addr);
6a6ae23f
AJ
8618}
8619
636aa200 8620static inline void gen_op_evstwwe(DisasContext *ctx, TCGv addr)
6a6ae23f
AJ
8621{
8622#if defined(TARGET_PPC64)
8623 TCGv t0 = tcg_temp_new();
8624 tcg_gen_shri_tl(t0, cpu_gpr[rS(ctx->opcode)], 32);
76db3ba4 8625 gen_qemu_st32(ctx, t0, addr);
6a6ae23f
AJ
8626 tcg_temp_free(t0);
8627#else
76db3ba4 8628 gen_qemu_st32(ctx, cpu_gprh[rS(ctx->opcode)], addr);
6a6ae23f
AJ
8629#endif
8630}
8631
636aa200 8632static inline void gen_op_evstwwo(DisasContext *ctx, TCGv addr)
6a6ae23f 8633{
76db3ba4 8634 gen_qemu_st32(ctx, cpu_gpr[rS(ctx->opcode)], addr);
6a6ae23f
AJ
8635}
8636
8637#define GEN_SPEOP_LDST(name, opc2, sh) \
99e300ef 8638static void glue(gen_, name)(DisasContext *ctx) \
6a6ae23f
AJ
8639{ \
8640 TCGv t0; \
8641 if (unlikely(!ctx->spe_enabled)) { \
27a69bb0 8642 gen_exception(ctx, POWERPC_EXCP_SPEU); \
6a6ae23f
AJ
8643 return; \
8644 } \
76db3ba4 8645 gen_set_access_type(ctx, ACCESS_INT); \
6a6ae23f
AJ
8646 t0 = tcg_temp_new(); \
8647 if (Rc(ctx->opcode)) { \
76db3ba4 8648 gen_addr_spe_imm_index(ctx, t0, sh); \
6a6ae23f 8649 } else { \
76db3ba4 8650 gen_addr_reg_index(ctx, t0); \
6a6ae23f
AJ
8651 } \
8652 gen_op_##name(ctx, t0); \
8653 tcg_temp_free(t0); \
8654}
8655
8656GEN_SPEOP_LDST(evldd, 0x00, 3);
8657GEN_SPEOP_LDST(evldw, 0x01, 3);
8658GEN_SPEOP_LDST(evldh, 0x02, 3);
8659GEN_SPEOP_LDST(evlhhesplat, 0x04, 1);
8660GEN_SPEOP_LDST(evlhhousplat, 0x06, 1);
8661GEN_SPEOP_LDST(evlhhossplat, 0x07, 1);
8662GEN_SPEOP_LDST(evlwhe, 0x08, 2);
8663GEN_SPEOP_LDST(evlwhou, 0x0A, 2);
8664GEN_SPEOP_LDST(evlwhos, 0x0B, 2);
8665GEN_SPEOP_LDST(evlwwsplat, 0x0C, 2);
8666GEN_SPEOP_LDST(evlwhsplat, 0x0E, 2);
8667
8668GEN_SPEOP_LDST(evstdd, 0x10, 3);
8669GEN_SPEOP_LDST(evstdw, 0x11, 3);
8670GEN_SPEOP_LDST(evstdh, 0x12, 3);
8671GEN_SPEOP_LDST(evstwhe, 0x18, 2);
8672GEN_SPEOP_LDST(evstwho, 0x1A, 2);
8673GEN_SPEOP_LDST(evstwwe, 0x1C, 2);
8674GEN_SPEOP_LDST(evstwwo, 0x1E, 2);
0487d6a8
JM
8675
8676/* Multiply and add - TODO */
8677#if 0
70560da7
FC
8678GEN_SPE(speundef, evmhessf, 0x01, 0x10, 0xFFFFFFFF, 0x00000000, PPC_SPE);//
8679GEN_SPE(speundef, evmhossf, 0x03, 0x10, 0xFFFFFFFF, 0x00000000, PPC_SPE);
8680GEN_SPE(evmheumi, evmhesmi, 0x04, 0x10, 0x00000000, 0x00000000, PPC_SPE);
8681GEN_SPE(speundef, evmhesmf, 0x05, 0x10, 0xFFFFFFFF, 0x00000000, PPC_SPE);
8682GEN_SPE(evmhoumi, evmhosmi, 0x06, 0x10, 0x00000000, 0x00000000, PPC_SPE);
8683GEN_SPE(speundef, evmhosmf, 0x07, 0x10, 0xFFFFFFFF, 0x00000000, PPC_SPE);
8684GEN_SPE(speundef, evmhessfa, 0x11, 0x10, 0xFFFFFFFF, 0x00000000, PPC_SPE);
8685GEN_SPE(speundef, evmhossfa, 0x13, 0x10, 0xFFFFFFFF, 0x00000000, PPC_SPE);
8686GEN_SPE(evmheumia, evmhesmia, 0x14, 0x10, 0x00000000, 0x00000000, PPC_SPE);
8687GEN_SPE(speundef, evmhesmfa, 0x15, 0x10, 0xFFFFFFFF, 0x00000000, PPC_SPE);
8688GEN_SPE(evmhoumia, evmhosmia, 0x16, 0x10, 0x00000000, 0x00000000, PPC_SPE);
8689GEN_SPE(speundef, evmhosmfa, 0x17, 0x10, 0xFFFFFFFF, 0x00000000, PPC_SPE);
8690
8691GEN_SPE(speundef, evmwhssf, 0x03, 0x11, 0xFFFFFFFF, 0x00000000, PPC_SPE);
8692GEN_SPE(evmwlumi, speundef, 0x04, 0x11, 0x00000000, 0xFFFFFFFF, PPC_SPE);
8693GEN_SPE(evmwhumi, evmwhsmi, 0x06, 0x11, 0x00000000, 0x00000000, PPC_SPE);
8694GEN_SPE(speundef, evmwhsmf, 0x07, 0x11, 0xFFFFFFFF, 0x00000000, PPC_SPE);
8695GEN_SPE(speundef, evmwssf, 0x09, 0x11, 0xFFFFFFFF, 0x00000000, PPC_SPE);
8696GEN_SPE(speundef, evmwsmf, 0x0D, 0x11, 0xFFFFFFFF, 0x00000000, PPC_SPE);
8697GEN_SPE(speundef, evmwhssfa, 0x13, 0x11, 0xFFFFFFFF, 0x00000000, PPC_SPE);
8698GEN_SPE(evmwlumia, speundef, 0x14, 0x11, 0x00000000, 0xFFFFFFFF, PPC_SPE);
8699GEN_SPE(evmwhumia, evmwhsmia, 0x16, 0x11, 0x00000000, 0x00000000, PPC_SPE);
8700GEN_SPE(speundef, evmwhsmfa, 0x17, 0x11, 0xFFFFFFFF, 0x00000000, PPC_SPE);
8701GEN_SPE(speundef, evmwssfa, 0x19, 0x11, 0xFFFFFFFF, 0x00000000, PPC_SPE);
8702GEN_SPE(speundef, evmwsmfa, 0x1D, 0x11, 0xFFFFFFFF, 0x00000000, PPC_SPE);
8703
8704GEN_SPE(evadduiaaw, evaddsiaaw, 0x00, 0x13, 0x0000F800, 0x0000F800, PPC_SPE);
8705GEN_SPE(evsubfusiaaw, evsubfssiaaw, 0x01, 0x13, 0x0000F800, 0x0000F800, PPC_SPE);
8706GEN_SPE(evaddumiaaw, evaddsmiaaw, 0x04, 0x13, 0x0000F800, 0x0000F800, PPC_SPE);
8707GEN_SPE(evsubfumiaaw, evsubfsmiaaw, 0x05, 0x13, 0x0000F800, 0x0000F800, PPC_SPE);
8708GEN_SPE(evdivws, evdivwu, 0x06, 0x13, 0x00000000, 0x00000000, PPC_SPE);
8709
8710GEN_SPE(evmheusiaaw, evmhessiaaw, 0x00, 0x14, 0x00000000, 0x00000000, PPC_SPE);
8711GEN_SPE(speundef, evmhessfaaw, 0x01, 0x14, 0xFFFFFFFF, 0x00000000, PPC_SPE);
8712GEN_SPE(evmhousiaaw, evmhossiaaw, 0x02, 0x14, 0x00000000, 0x00000000, PPC_SPE);
8713GEN_SPE(speundef, evmhossfaaw, 0x03, 0x14, 0xFFFFFFFF, 0x00000000, PPC_SPE);
8714GEN_SPE(evmheumiaaw, evmhesmiaaw, 0x04, 0x14, 0x00000000, 0x00000000, PPC_SPE);
8715GEN_SPE(speundef, evmhesmfaaw, 0x05, 0x14, 0xFFFFFFFF, 0x00000000, PPC_SPE);
8716GEN_SPE(evmhoumiaaw, evmhosmiaaw, 0x06, 0x14, 0x00000000, 0x00000000, PPC_SPE);
8717GEN_SPE(speundef, evmhosmfaaw, 0x07, 0x14, 0xFFFFFFFF, 0x00000000, PPC_SPE);
8718GEN_SPE(evmhegumiaa, evmhegsmiaa, 0x14, 0x14, 0x00000000, 0x00000000, PPC_SPE);
8719GEN_SPE(speundef, evmhegsmfaa, 0x15, 0x14, 0xFFFFFFFF, 0x00000000, PPC_SPE);
8720GEN_SPE(evmhogumiaa, evmhogsmiaa, 0x16, 0x14, 0x00000000, 0x00000000, PPC_SPE);
8721GEN_SPE(speundef, evmhogsmfaa, 0x17, 0x14, 0xFFFFFFFF, 0x00000000, PPC_SPE);
8722
8723GEN_SPE(evmwlusiaaw, evmwlssiaaw, 0x00, 0x15, 0x00000000, 0x00000000, PPC_SPE);
8724GEN_SPE(evmwlumiaaw, evmwlsmiaaw, 0x04, 0x15, 0x00000000, 0x00000000, PPC_SPE);
8725GEN_SPE(speundef, evmwssfaa, 0x09, 0x15, 0xFFFFFFFF, 0x00000000, PPC_SPE);
8726GEN_SPE(speundef, evmwsmfaa, 0x0D, 0x15, 0xFFFFFFFF, 0x00000000, PPC_SPE);
8727
8728GEN_SPE(evmheusianw, evmhessianw, 0x00, 0x16, 0x00000000, 0x00000000, PPC_SPE);
8729GEN_SPE(speundef, evmhessfanw, 0x01, 0x16, 0xFFFFFFFF, 0x00000000, PPC_SPE);
8730GEN_SPE(evmhousianw, evmhossianw, 0x02, 0x16, 0x00000000, 0x00000000, PPC_SPE);
8731GEN_SPE(speundef, evmhossfanw, 0x03, 0x16, 0xFFFFFFFF, 0x00000000, PPC_SPE);
8732GEN_SPE(evmheumianw, evmhesmianw, 0x04, 0x16, 0x00000000, 0x00000000, PPC_SPE);
8733GEN_SPE(speundef, evmhesmfanw, 0x05, 0x16, 0xFFFFFFFF, 0x00000000, PPC_SPE);
8734GEN_SPE(evmhoumianw, evmhosmianw, 0x06, 0x16, 0x00000000, 0x00000000, PPC_SPE);
8735GEN_SPE(speundef, evmhosmfanw, 0x07, 0x16, 0xFFFFFFFF, 0x00000000, PPC_SPE);
8736GEN_SPE(evmhegumian, evmhegsmian, 0x14, 0x16, 0x00000000, 0x00000000, PPC_SPE);
8737GEN_SPE(speundef, evmhegsmfan, 0x15, 0x16, 0xFFFFFFFF, 0x00000000, PPC_SPE);
8738GEN_SPE(evmhigumian, evmhigsmian, 0x16, 0x16, 0x00000000, 0x00000000, PPC_SPE);
8739GEN_SPE(speundef, evmhogsmfan, 0x17, 0x16, 0xFFFFFFFF, 0x00000000, PPC_SPE);
8740
8741GEN_SPE(evmwlusianw, evmwlssianw, 0x00, 0x17, 0x00000000, 0x00000000, PPC_SPE);
8742GEN_SPE(evmwlumianw, evmwlsmianw, 0x04, 0x17, 0x00000000, 0x00000000, PPC_SPE);
8743GEN_SPE(speundef, evmwssfan, 0x09, 0x17, 0xFFFFFFFF, 0x00000000, PPC_SPE);
8744GEN_SPE(evmwumian, evmwsmian, 0x0C, 0x17, 0x00000000, 0x00000000, PPC_SPE);
8745GEN_SPE(speundef, evmwsmfan, 0x0D, 0x17, 0xFFFFFFFF, 0x00000000, PPC_SPE);
0487d6a8
JM
8746#endif
8747
8748/*** SPE floating-point extension ***/
1c97856d
AJ
8749#if defined(TARGET_PPC64)
8750#define GEN_SPEFPUOP_CONV_32_32(name) \
636aa200 8751static inline void gen_##name(DisasContext *ctx) \
0487d6a8 8752{ \
1c97856d
AJ
8753 TCGv_i32 t0; \
8754 TCGv t1; \
8755 t0 = tcg_temp_new_i32(); \
8756 tcg_gen_trunc_tl_i32(t0, cpu_gpr[rB(ctx->opcode)]); \
8e703949 8757 gen_helper_##name(t0, cpu_env, t0); \
1c97856d
AJ
8758 t1 = tcg_temp_new(); \
8759 tcg_gen_extu_i32_tl(t1, t0); \
8760 tcg_temp_free_i32(t0); \
8761 tcg_gen_andi_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rD(ctx->opcode)], \
8762 0xFFFFFFFF00000000ULL); \
8763 tcg_gen_or_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rD(ctx->opcode)], t1); \
8764 tcg_temp_free(t1); \
0487d6a8 8765}
1c97856d 8766#define GEN_SPEFPUOP_CONV_32_64(name) \
636aa200 8767static inline void gen_##name(DisasContext *ctx) \
1c97856d
AJ
8768{ \
8769 TCGv_i32 t0; \
8770 TCGv t1; \
8771 t0 = tcg_temp_new_i32(); \
8e703949 8772 gen_helper_##name(t0, cpu_env, cpu_gpr[rB(ctx->opcode)]); \
1c97856d
AJ
8773 t1 = tcg_temp_new(); \
8774 tcg_gen_extu_i32_tl(t1, t0); \
8775 tcg_temp_free_i32(t0); \
8776 tcg_gen_andi_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rD(ctx->opcode)], \
8777 0xFFFFFFFF00000000ULL); \
8778 tcg_gen_or_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rD(ctx->opcode)], t1); \
8779 tcg_temp_free(t1); \
8780}
8781#define GEN_SPEFPUOP_CONV_64_32(name) \
636aa200 8782static inline void gen_##name(DisasContext *ctx) \
1c97856d
AJ
8783{ \
8784 TCGv_i32 t0 = tcg_temp_new_i32(); \
8785 tcg_gen_trunc_tl_i32(t0, cpu_gpr[rB(ctx->opcode)]); \
8e703949 8786 gen_helper_##name(cpu_gpr[rD(ctx->opcode)], cpu_env, t0); \
1c97856d
AJ
8787 tcg_temp_free_i32(t0); \
8788}
8789#define GEN_SPEFPUOP_CONV_64_64(name) \
636aa200 8790static inline void gen_##name(DisasContext *ctx) \
1c97856d 8791{ \
8e703949
BS
8792 gen_helper_##name(cpu_gpr[rD(ctx->opcode)], cpu_env, \
8793 cpu_gpr[rB(ctx->opcode)]); \
1c97856d
AJ
8794}
8795#define GEN_SPEFPUOP_ARITH2_32_32(name) \
636aa200 8796static inline void gen_##name(DisasContext *ctx) \
57951c27 8797{ \
1c97856d
AJ
8798 TCGv_i32 t0, t1; \
8799 TCGv_i64 t2; \
57951c27 8800 if (unlikely(!ctx->spe_enabled)) { \
27a69bb0 8801 gen_exception(ctx, POWERPC_EXCP_SPEU); \
57951c27
AJ
8802 return; \
8803 } \
1c97856d
AJ
8804 t0 = tcg_temp_new_i32(); \
8805 t1 = tcg_temp_new_i32(); \
8806 tcg_gen_trunc_tl_i32(t0, cpu_gpr[rA(ctx->opcode)]); \
8807 tcg_gen_trunc_tl_i32(t1, cpu_gpr[rB(ctx->opcode)]); \
8e703949 8808 gen_helper_##name(t0, cpu_env, t0, t1); \
1c97856d
AJ
8809 tcg_temp_free_i32(t1); \
8810 t2 = tcg_temp_new(); \
8811 tcg_gen_extu_i32_tl(t2, t0); \
8812 tcg_temp_free_i32(t0); \
8813 tcg_gen_andi_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rD(ctx->opcode)], \
8814 0xFFFFFFFF00000000ULL); \
8815 tcg_gen_or_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rD(ctx->opcode)], t2); \
8816 tcg_temp_free(t2); \
57951c27 8817}
1c97856d 8818#define GEN_SPEFPUOP_ARITH2_64_64(name) \
636aa200 8819static inline void gen_##name(DisasContext *ctx) \
57951c27
AJ
8820{ \
8821 if (unlikely(!ctx->spe_enabled)) { \
27a69bb0 8822 gen_exception(ctx, POWERPC_EXCP_SPEU); \
57951c27
AJ
8823 return; \
8824 } \
8e703949
BS
8825 gen_helper_##name(cpu_gpr[rD(ctx->opcode)], cpu_env, \
8826 cpu_gpr[rA(ctx->opcode)], cpu_gpr[rB(ctx->opcode)]); \
57951c27 8827}
1c97856d 8828#define GEN_SPEFPUOP_COMP_32(name) \
636aa200 8829static inline void gen_##name(DisasContext *ctx) \
57951c27 8830{ \
1c97856d 8831 TCGv_i32 t0, t1; \
57951c27 8832 if (unlikely(!ctx->spe_enabled)) { \
27a69bb0 8833 gen_exception(ctx, POWERPC_EXCP_SPEU); \
57951c27
AJ
8834 return; \
8835 } \
1c97856d
AJ
8836 t0 = tcg_temp_new_i32(); \
8837 t1 = tcg_temp_new_i32(); \
8838 tcg_gen_trunc_tl_i32(t0, cpu_gpr[rA(ctx->opcode)]); \
8839 tcg_gen_trunc_tl_i32(t1, cpu_gpr[rB(ctx->opcode)]); \
8e703949 8840 gen_helper_##name(cpu_crf[crfD(ctx->opcode)], cpu_env, t0, t1); \
1c97856d
AJ
8841 tcg_temp_free_i32(t0); \
8842 tcg_temp_free_i32(t1); \
8843}
8844#define GEN_SPEFPUOP_COMP_64(name) \
636aa200 8845static inline void gen_##name(DisasContext *ctx) \
1c97856d
AJ
8846{ \
8847 if (unlikely(!ctx->spe_enabled)) { \
27a69bb0 8848 gen_exception(ctx, POWERPC_EXCP_SPEU); \
1c97856d
AJ
8849 return; \
8850 } \
8e703949 8851 gen_helper_##name(cpu_crf[crfD(ctx->opcode)], cpu_env, \
1c97856d
AJ
8852 cpu_gpr[rA(ctx->opcode)], cpu_gpr[rB(ctx->opcode)]); \
8853}
8854#else
8855#define GEN_SPEFPUOP_CONV_32_32(name) \
636aa200 8856static inline void gen_##name(DisasContext *ctx) \
1c97856d 8857{ \
8e703949
BS
8858 gen_helper_##name(cpu_gpr[rD(ctx->opcode)], cpu_env, \
8859 cpu_gpr[rB(ctx->opcode)]); \
57951c27 8860}
1c97856d 8861#define GEN_SPEFPUOP_CONV_32_64(name) \
636aa200 8862static inline void gen_##name(DisasContext *ctx) \
1c97856d
AJ
8863{ \
8864 TCGv_i64 t0 = tcg_temp_new_i64(); \
8865 gen_load_gpr64(t0, rB(ctx->opcode)); \
8e703949 8866 gen_helper_##name(cpu_gpr[rD(ctx->opcode)], cpu_env, t0); \
1c97856d
AJ
8867 tcg_temp_free_i64(t0); \
8868}
8869#define GEN_SPEFPUOP_CONV_64_32(name) \
636aa200 8870static inline void gen_##name(DisasContext *ctx) \
1c97856d
AJ
8871{ \
8872 TCGv_i64 t0 = tcg_temp_new_i64(); \
8e703949 8873 gen_helper_##name(t0, cpu_env, cpu_gpr[rB(ctx->opcode)]); \
1c97856d
AJ
8874 gen_store_gpr64(rD(ctx->opcode), t0); \
8875 tcg_temp_free_i64(t0); \
8876}
8877#define GEN_SPEFPUOP_CONV_64_64(name) \
636aa200 8878static inline void gen_##name(DisasContext *ctx) \
1c97856d
AJ
8879{ \
8880 TCGv_i64 t0 = tcg_temp_new_i64(); \
8881 gen_load_gpr64(t0, rB(ctx->opcode)); \
8e703949 8882 gen_helper_##name(t0, cpu_env, t0); \
1c97856d
AJ
8883 gen_store_gpr64(rD(ctx->opcode), t0); \
8884 tcg_temp_free_i64(t0); \
8885}
8886#define GEN_SPEFPUOP_ARITH2_32_32(name) \
636aa200 8887static inline void gen_##name(DisasContext *ctx) \
1c97856d
AJ
8888{ \
8889 if (unlikely(!ctx->spe_enabled)) { \
27a69bb0 8890 gen_exception(ctx, POWERPC_EXCP_SPEU); \
1c97856d
AJ
8891 return; \
8892 } \
8e703949 8893 gen_helper_##name(cpu_gpr[rD(ctx->opcode)], cpu_env, \
1c97856d
AJ
8894 cpu_gpr[rA(ctx->opcode)], cpu_gpr[rB(ctx->opcode)]); \
8895}
8896#define GEN_SPEFPUOP_ARITH2_64_64(name) \
636aa200 8897static inline void gen_##name(DisasContext *ctx) \
1c97856d
AJ
8898{ \
8899 TCGv_i64 t0, t1; \
8900 if (unlikely(!ctx->spe_enabled)) { \
27a69bb0 8901 gen_exception(ctx, POWERPC_EXCP_SPEU); \
1c97856d
AJ
8902 return; \
8903 } \
8904 t0 = tcg_temp_new_i64(); \
8905 t1 = tcg_temp_new_i64(); \
8906 gen_load_gpr64(t0, rA(ctx->opcode)); \
8907 gen_load_gpr64(t1, rB(ctx->opcode)); \
8e703949 8908 gen_helper_##name(t0, cpu_env, t0, t1); \
1c97856d
AJ
8909 gen_store_gpr64(rD(ctx->opcode), t0); \
8910 tcg_temp_free_i64(t0); \
8911 tcg_temp_free_i64(t1); \
8912}
8913#define GEN_SPEFPUOP_COMP_32(name) \
636aa200 8914static inline void gen_##name(DisasContext *ctx) \
1c97856d
AJ
8915{ \
8916 if (unlikely(!ctx->spe_enabled)) { \
27a69bb0 8917 gen_exception(ctx, POWERPC_EXCP_SPEU); \
1c97856d
AJ
8918 return; \
8919 } \
8e703949 8920 gen_helper_##name(cpu_crf[crfD(ctx->opcode)], cpu_env, \
1c97856d
AJ
8921 cpu_gpr[rA(ctx->opcode)], cpu_gpr[rB(ctx->opcode)]); \
8922}
8923#define GEN_SPEFPUOP_COMP_64(name) \
636aa200 8924static inline void gen_##name(DisasContext *ctx) \
1c97856d
AJ
8925{ \
8926 TCGv_i64 t0, t1; \
8927 if (unlikely(!ctx->spe_enabled)) { \
27a69bb0 8928 gen_exception(ctx, POWERPC_EXCP_SPEU); \
1c97856d
AJ
8929 return; \
8930 } \
8931 t0 = tcg_temp_new_i64(); \
8932 t1 = tcg_temp_new_i64(); \
8933 gen_load_gpr64(t0, rA(ctx->opcode)); \
8934 gen_load_gpr64(t1, rB(ctx->opcode)); \
8e703949 8935 gen_helper_##name(cpu_crf[crfD(ctx->opcode)], cpu_env, t0, t1); \
1c97856d
AJ
8936 tcg_temp_free_i64(t0); \
8937 tcg_temp_free_i64(t1); \
8938}
8939#endif
57951c27 8940
0487d6a8
JM
8941/* Single precision floating-point vectors operations */
8942/* Arithmetic */
1c97856d
AJ
8943GEN_SPEFPUOP_ARITH2_64_64(evfsadd);
8944GEN_SPEFPUOP_ARITH2_64_64(evfssub);
8945GEN_SPEFPUOP_ARITH2_64_64(evfsmul);
8946GEN_SPEFPUOP_ARITH2_64_64(evfsdiv);
636aa200 8947static inline void gen_evfsabs(DisasContext *ctx)
1c97856d
AJ
8948{
8949 if (unlikely(!ctx->spe_enabled)) {
27a69bb0 8950 gen_exception(ctx, POWERPC_EXCP_SPEU);
1c97856d
AJ
8951 return;
8952 }
8953#if defined(TARGET_PPC64)
6d5c34fa 8954 tcg_gen_andi_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)], ~0x8000000080000000LL);
1c97856d 8955#else
6d5c34fa
MP
8956 tcg_gen_andi_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)], ~0x80000000);
8957 tcg_gen_andi_tl(cpu_gprh[rD(ctx->opcode)], cpu_gprh[rA(ctx->opcode)], ~0x80000000);
1c97856d
AJ
8958#endif
8959}
636aa200 8960static inline void gen_evfsnabs(DisasContext *ctx)
1c97856d
AJ
8961{
8962 if (unlikely(!ctx->spe_enabled)) {
27a69bb0 8963 gen_exception(ctx, POWERPC_EXCP_SPEU);
1c97856d
AJ
8964 return;
8965 }
8966#if defined(TARGET_PPC64)
6d5c34fa 8967 tcg_gen_ori_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)], 0x8000000080000000LL);
1c97856d 8968#else
6d5c34fa
MP
8969 tcg_gen_ori_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)], 0x80000000);
8970 tcg_gen_ori_tl(cpu_gprh[rD(ctx->opcode)], cpu_gprh[rA(ctx->opcode)], 0x80000000);
1c97856d
AJ
8971#endif
8972}
636aa200 8973static inline void gen_evfsneg(DisasContext *ctx)
1c97856d
AJ
8974{
8975 if (unlikely(!ctx->spe_enabled)) {
27a69bb0 8976 gen_exception(ctx, POWERPC_EXCP_SPEU);
1c97856d
AJ
8977 return;
8978 }
8979#if defined(TARGET_PPC64)
6d5c34fa 8980 tcg_gen_xori_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)], 0x8000000080000000LL);
1c97856d 8981#else
6d5c34fa
MP
8982 tcg_gen_xori_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)], 0x80000000);
8983 tcg_gen_xori_tl(cpu_gprh[rD(ctx->opcode)], cpu_gprh[rA(ctx->opcode)], 0x80000000);
1c97856d
AJ
8984#endif
8985}
8986
0487d6a8 8987/* Conversion */
1c97856d
AJ
8988GEN_SPEFPUOP_CONV_64_64(evfscfui);
8989GEN_SPEFPUOP_CONV_64_64(evfscfsi);
8990GEN_SPEFPUOP_CONV_64_64(evfscfuf);
8991GEN_SPEFPUOP_CONV_64_64(evfscfsf);
8992GEN_SPEFPUOP_CONV_64_64(evfsctui);
8993GEN_SPEFPUOP_CONV_64_64(evfsctsi);
8994GEN_SPEFPUOP_CONV_64_64(evfsctuf);
8995GEN_SPEFPUOP_CONV_64_64(evfsctsf);
8996GEN_SPEFPUOP_CONV_64_64(evfsctuiz);
8997GEN_SPEFPUOP_CONV_64_64(evfsctsiz);
8998
0487d6a8 8999/* Comparison */
1c97856d
AJ
9000GEN_SPEFPUOP_COMP_64(evfscmpgt);
9001GEN_SPEFPUOP_COMP_64(evfscmplt);
9002GEN_SPEFPUOP_COMP_64(evfscmpeq);
9003GEN_SPEFPUOP_COMP_64(evfststgt);
9004GEN_SPEFPUOP_COMP_64(evfststlt);
9005GEN_SPEFPUOP_COMP_64(evfststeq);
0487d6a8
JM
9006
9007/* Opcodes definitions */
70560da7
FC
9008GEN_SPE(evfsadd, evfssub, 0x00, 0x0A, 0x00000000, 0x00000000, PPC_SPE_SINGLE); //
9009GEN_SPE(evfsabs, evfsnabs, 0x02, 0x0A, 0x0000F800, 0x0000F800, PPC_SPE_SINGLE); //
9010GEN_SPE(evfsneg, speundef, 0x03, 0x0A, 0x0000F800, 0xFFFFFFFF, PPC_SPE_SINGLE); //
9011GEN_SPE(evfsmul, evfsdiv, 0x04, 0x0A, 0x00000000, 0x00000000, PPC_SPE_SINGLE); //
9012GEN_SPE(evfscmpgt, evfscmplt, 0x06, 0x0A, 0x00600000, 0x00600000, PPC_SPE_SINGLE); //
9013GEN_SPE(evfscmpeq, speundef, 0x07, 0x0A, 0x00600000, 0xFFFFFFFF, PPC_SPE_SINGLE); //
9014GEN_SPE(evfscfui, evfscfsi, 0x08, 0x0A, 0x00180000, 0x00180000, PPC_SPE_SINGLE); //
9015GEN_SPE(evfscfuf, evfscfsf, 0x09, 0x0A, 0x00180000, 0x00180000, PPC_SPE_SINGLE); //
9016GEN_SPE(evfsctui, evfsctsi, 0x0A, 0x0A, 0x00180000, 0x00180000, PPC_SPE_SINGLE); //
9017GEN_SPE(evfsctuf, evfsctsf, 0x0B, 0x0A, 0x00180000, 0x00180000, PPC_SPE_SINGLE); //
9018GEN_SPE(evfsctuiz, speundef, 0x0C, 0x0A, 0x00180000, 0xFFFFFFFF, PPC_SPE_SINGLE); //
9019GEN_SPE(evfsctsiz, speundef, 0x0D, 0x0A, 0x00180000, 0xFFFFFFFF, PPC_SPE_SINGLE); //
9020GEN_SPE(evfststgt, evfststlt, 0x0E, 0x0A, 0x00600000, 0x00600000, PPC_SPE_SINGLE); //
9021GEN_SPE(evfststeq, speundef, 0x0F, 0x0A, 0x00600000, 0xFFFFFFFF, PPC_SPE_SINGLE); //
0487d6a8
JM
9022
9023/* Single precision floating-point operations */
9024/* Arithmetic */
1c97856d
AJ
9025GEN_SPEFPUOP_ARITH2_32_32(efsadd);
9026GEN_SPEFPUOP_ARITH2_32_32(efssub);
9027GEN_SPEFPUOP_ARITH2_32_32(efsmul);
9028GEN_SPEFPUOP_ARITH2_32_32(efsdiv);
636aa200 9029static inline void gen_efsabs(DisasContext *ctx)
1c97856d
AJ
9030{
9031 if (unlikely(!ctx->spe_enabled)) {
27a69bb0 9032 gen_exception(ctx, POWERPC_EXCP_SPEU);
1c97856d
AJ
9033 return;
9034 }
6d5c34fa 9035 tcg_gen_andi_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)], (target_long)~0x80000000LL);
1c97856d 9036}
636aa200 9037static inline void gen_efsnabs(DisasContext *ctx)
1c97856d
AJ
9038{
9039 if (unlikely(!ctx->spe_enabled)) {
27a69bb0 9040 gen_exception(ctx, POWERPC_EXCP_SPEU);
1c97856d
AJ
9041 return;
9042 }
6d5c34fa 9043 tcg_gen_ori_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)], 0x80000000);
1c97856d 9044}
636aa200 9045static inline void gen_efsneg(DisasContext *ctx)
1c97856d
AJ
9046{
9047 if (unlikely(!ctx->spe_enabled)) {
27a69bb0 9048 gen_exception(ctx, POWERPC_EXCP_SPEU);
1c97856d
AJ
9049 return;
9050 }
6d5c34fa 9051 tcg_gen_xori_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)], 0x80000000);
1c97856d
AJ
9052}
9053
0487d6a8 9054/* Conversion */
1c97856d
AJ
9055GEN_SPEFPUOP_CONV_32_32(efscfui);
9056GEN_SPEFPUOP_CONV_32_32(efscfsi);
9057GEN_SPEFPUOP_CONV_32_32(efscfuf);
9058GEN_SPEFPUOP_CONV_32_32(efscfsf);
9059GEN_SPEFPUOP_CONV_32_32(efsctui);
9060GEN_SPEFPUOP_CONV_32_32(efsctsi);
9061GEN_SPEFPUOP_CONV_32_32(efsctuf);
9062GEN_SPEFPUOP_CONV_32_32(efsctsf);
9063GEN_SPEFPUOP_CONV_32_32(efsctuiz);
9064GEN_SPEFPUOP_CONV_32_32(efsctsiz);
9065GEN_SPEFPUOP_CONV_32_64(efscfd);
9066
0487d6a8 9067/* Comparison */
1c97856d
AJ
9068GEN_SPEFPUOP_COMP_32(efscmpgt);
9069GEN_SPEFPUOP_COMP_32(efscmplt);
9070GEN_SPEFPUOP_COMP_32(efscmpeq);
9071GEN_SPEFPUOP_COMP_32(efststgt);
9072GEN_SPEFPUOP_COMP_32(efststlt);
9073GEN_SPEFPUOP_COMP_32(efststeq);
0487d6a8
JM
9074
9075/* Opcodes definitions */
70560da7
FC
9076GEN_SPE(efsadd, efssub, 0x00, 0x0B, 0x00000000, 0x00000000, PPC_SPE_SINGLE); //
9077GEN_SPE(efsabs, efsnabs, 0x02, 0x0B, 0x0000F800, 0x0000F800, PPC_SPE_SINGLE); //
9078GEN_SPE(efsneg, speundef, 0x03, 0x0B, 0x0000F800, 0xFFFFFFFF, PPC_SPE_SINGLE); //
9079GEN_SPE(efsmul, efsdiv, 0x04, 0x0B, 0x00000000, 0x00000000, PPC_SPE_SINGLE); //
9080GEN_SPE(efscmpgt, efscmplt, 0x06, 0x0B, 0x00600000, 0x00600000, PPC_SPE_SINGLE); //
9081GEN_SPE(efscmpeq, efscfd, 0x07, 0x0B, 0x00600000, 0x00180000, PPC_SPE_SINGLE); //
9082GEN_SPE(efscfui, efscfsi, 0x08, 0x0B, 0x00180000, 0x00180000, PPC_SPE_SINGLE); //
9083GEN_SPE(efscfuf, efscfsf, 0x09, 0x0B, 0x00180000, 0x00180000, PPC_SPE_SINGLE); //
9084GEN_SPE(efsctui, efsctsi, 0x0A, 0x0B, 0x00180000, 0x00180000, PPC_SPE_SINGLE); //
9085GEN_SPE(efsctuf, efsctsf, 0x0B, 0x0B, 0x00180000, 0x00180000, PPC_SPE_SINGLE); //
9086GEN_SPE(efsctuiz, speundef, 0x0C, 0x0B, 0x00180000, 0xFFFFFFFF, PPC_SPE_SINGLE); //
9087GEN_SPE(efsctsiz, speundef, 0x0D, 0x0B, 0x00180000, 0xFFFFFFFF, PPC_SPE_SINGLE); //
9088GEN_SPE(efststgt, efststlt, 0x0E, 0x0B, 0x00600000, 0x00600000, PPC_SPE_SINGLE); //
9089GEN_SPE(efststeq, speundef, 0x0F, 0x0B, 0x00600000, 0xFFFFFFFF, PPC_SPE_SINGLE); //
0487d6a8
JM
9090
9091/* Double precision floating-point operations */
9092/* Arithmetic */
1c97856d
AJ
9093GEN_SPEFPUOP_ARITH2_64_64(efdadd);
9094GEN_SPEFPUOP_ARITH2_64_64(efdsub);
9095GEN_SPEFPUOP_ARITH2_64_64(efdmul);
9096GEN_SPEFPUOP_ARITH2_64_64(efddiv);
636aa200 9097static inline void gen_efdabs(DisasContext *ctx)
1c97856d
AJ
9098{
9099 if (unlikely(!ctx->spe_enabled)) {
27a69bb0 9100 gen_exception(ctx, POWERPC_EXCP_SPEU);
1c97856d
AJ
9101 return;
9102 }
9103#if defined(TARGET_PPC64)
6d5c34fa 9104 tcg_gen_andi_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)], ~0x8000000000000000LL);
1c97856d 9105#else
6d5c34fa
MP
9106 tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)]);
9107 tcg_gen_andi_tl(cpu_gprh[rD(ctx->opcode)], cpu_gprh[rA(ctx->opcode)], ~0x80000000);
1c97856d
AJ
9108#endif
9109}
636aa200 9110static inline void gen_efdnabs(DisasContext *ctx)
1c97856d
AJ
9111{
9112 if (unlikely(!ctx->spe_enabled)) {
27a69bb0 9113 gen_exception(ctx, POWERPC_EXCP_SPEU);
1c97856d
AJ
9114 return;
9115 }
9116#if defined(TARGET_PPC64)
6d5c34fa 9117 tcg_gen_ori_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)], 0x8000000000000000LL);
1c97856d 9118#else
6d5c34fa
MP
9119 tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)]);
9120 tcg_gen_ori_tl(cpu_gprh[rD(ctx->opcode)], cpu_gprh[rA(ctx->opcode)], 0x80000000);
1c97856d
AJ
9121#endif
9122}
636aa200 9123static inline void gen_efdneg(DisasContext *ctx)
1c97856d
AJ
9124{
9125 if (unlikely(!ctx->spe_enabled)) {
27a69bb0 9126 gen_exception(ctx, POWERPC_EXCP_SPEU);
1c97856d
AJ
9127 return;
9128 }
9129#if defined(TARGET_PPC64)
6d5c34fa 9130 tcg_gen_xori_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)], 0x8000000000000000LL);
1c97856d 9131#else
6d5c34fa
MP
9132 tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)]);
9133 tcg_gen_xori_tl(cpu_gprh[rD(ctx->opcode)], cpu_gprh[rA(ctx->opcode)], 0x80000000);
1c97856d
AJ
9134#endif
9135}
9136
0487d6a8 9137/* Conversion */
1c97856d
AJ
9138GEN_SPEFPUOP_CONV_64_32(efdcfui);
9139GEN_SPEFPUOP_CONV_64_32(efdcfsi);
9140GEN_SPEFPUOP_CONV_64_32(efdcfuf);
9141GEN_SPEFPUOP_CONV_64_32(efdcfsf);
9142GEN_SPEFPUOP_CONV_32_64(efdctui);
9143GEN_SPEFPUOP_CONV_32_64(efdctsi);
9144GEN_SPEFPUOP_CONV_32_64(efdctuf);
9145GEN_SPEFPUOP_CONV_32_64(efdctsf);
9146GEN_SPEFPUOP_CONV_32_64(efdctuiz);
9147GEN_SPEFPUOP_CONV_32_64(efdctsiz);
9148GEN_SPEFPUOP_CONV_64_32(efdcfs);
9149GEN_SPEFPUOP_CONV_64_64(efdcfuid);
9150GEN_SPEFPUOP_CONV_64_64(efdcfsid);
9151GEN_SPEFPUOP_CONV_64_64(efdctuidz);
9152GEN_SPEFPUOP_CONV_64_64(efdctsidz);
0487d6a8 9153
0487d6a8 9154/* Comparison */
1c97856d
AJ
9155GEN_SPEFPUOP_COMP_64(efdcmpgt);
9156GEN_SPEFPUOP_COMP_64(efdcmplt);
9157GEN_SPEFPUOP_COMP_64(efdcmpeq);
9158GEN_SPEFPUOP_COMP_64(efdtstgt);
9159GEN_SPEFPUOP_COMP_64(efdtstlt);
9160GEN_SPEFPUOP_COMP_64(efdtsteq);
0487d6a8
JM
9161
9162/* Opcodes definitions */
70560da7
FC
9163GEN_SPE(efdadd, efdsub, 0x10, 0x0B, 0x00000000, 0x00000000, PPC_SPE_DOUBLE); //
9164GEN_SPE(efdcfuid, efdcfsid, 0x11, 0x0B, 0x00180000, 0x00180000, PPC_SPE_DOUBLE); //
9165GEN_SPE(efdabs, efdnabs, 0x12, 0x0B, 0x0000F800, 0x0000F800, PPC_SPE_DOUBLE); //
9166GEN_SPE(efdneg, speundef, 0x13, 0x0B, 0x0000F800, 0xFFFFFFFF, PPC_SPE_DOUBLE); //
9167GEN_SPE(efdmul, efddiv, 0x14, 0x0B, 0x00000000, 0x00000000, PPC_SPE_DOUBLE); //
9168GEN_SPE(efdctuidz, efdctsidz, 0x15, 0x0B, 0x00180000, 0x00180000, PPC_SPE_DOUBLE); //
9169GEN_SPE(efdcmpgt, efdcmplt, 0x16, 0x0B, 0x00600000, 0x00600000, PPC_SPE_DOUBLE); //
9170GEN_SPE(efdcmpeq, efdcfs, 0x17, 0x0B, 0x00600000, 0x00180000, PPC_SPE_DOUBLE); //
9171GEN_SPE(efdcfui, efdcfsi, 0x18, 0x0B, 0x00180000, 0x00180000, PPC_SPE_DOUBLE); //
9172GEN_SPE(efdcfuf, efdcfsf, 0x19, 0x0B, 0x00180000, 0x00180000, PPC_SPE_DOUBLE); //
9173GEN_SPE(efdctui, efdctsi, 0x1A, 0x0B, 0x00180000, 0x00180000, PPC_SPE_DOUBLE); //
9174GEN_SPE(efdctuf, efdctsf, 0x1B, 0x0B, 0x00180000, 0x00180000, PPC_SPE_DOUBLE); //
9175GEN_SPE(efdctuiz, speundef, 0x1C, 0x0B, 0x00180000, 0xFFFFFFFF, PPC_SPE_DOUBLE); //
9176GEN_SPE(efdctsiz, speundef, 0x1D, 0x0B, 0x00180000, 0xFFFFFFFF, PPC_SPE_DOUBLE); //
9177GEN_SPE(efdtstgt, efdtstlt, 0x1E, 0x0B, 0x00600000, 0x00600000, PPC_SPE_DOUBLE); //
9178GEN_SPE(efdtsteq, speundef, 0x1F, 0x0B, 0x00600000, 0xFFFFFFFF, PPC_SPE_DOUBLE); //
0487d6a8 9179
c227f099 9180static opcode_t opcodes[] = {
5c55ff99
BS
9181GEN_HANDLER(invalid, 0x00, 0x00, 0x00, 0xFFFFFFFF, PPC_NONE),
9182GEN_HANDLER(cmp, 0x1F, 0x00, 0x00, 0x00400000, PPC_INTEGER),
9183GEN_HANDLER(cmpi, 0x0B, 0xFF, 0xFF, 0x00400000, PPC_INTEGER),
9184GEN_HANDLER(cmpl, 0x1F, 0x00, 0x01, 0x00400000, PPC_INTEGER),
9185GEN_HANDLER(cmpli, 0x0A, 0xFF, 0xFF, 0x00400000, PPC_INTEGER),
fcfda20f 9186GEN_HANDLER_E(cmpb, 0x1F, 0x1C, 0x0F, 0x00000001, PPC_NONE, PPC2_ISA205),
5c55ff99
BS
9187GEN_HANDLER(isel, 0x1F, 0x0F, 0xFF, 0x00000001, PPC_ISEL),
9188GEN_HANDLER(addi, 0x0E, 0xFF, 0xFF, 0x00000000, PPC_INTEGER),
9189GEN_HANDLER(addic, 0x0C, 0xFF, 0xFF, 0x00000000, PPC_INTEGER),
9190GEN_HANDLER2(addic_, "addic.", 0x0D, 0xFF, 0xFF, 0x00000000, PPC_INTEGER),
9191GEN_HANDLER(addis, 0x0F, 0xFF, 0xFF, 0x00000000, PPC_INTEGER),
9192GEN_HANDLER(mulhw, 0x1F, 0x0B, 0x02, 0x00000400, PPC_INTEGER),
9193GEN_HANDLER(mulhwu, 0x1F, 0x0B, 0x00, 0x00000400, PPC_INTEGER),
9194GEN_HANDLER(mullw, 0x1F, 0x0B, 0x07, 0x00000000, PPC_INTEGER),
9195GEN_HANDLER(mullwo, 0x1F, 0x0B, 0x17, 0x00000000, PPC_INTEGER),
9196GEN_HANDLER(mulli, 0x07, 0xFF, 0xFF, 0x00000000, PPC_INTEGER),
9197#if defined(TARGET_PPC64)
9198GEN_HANDLER(mulld, 0x1F, 0x09, 0x07, 0x00000000, PPC_64B),
9199#endif
9200GEN_HANDLER(neg, 0x1F, 0x08, 0x03, 0x0000F800, PPC_INTEGER),
9201GEN_HANDLER(nego, 0x1F, 0x08, 0x13, 0x0000F800, PPC_INTEGER),
9202GEN_HANDLER(subfic, 0x08, 0xFF, 0xFF, 0x00000000, PPC_INTEGER),
9203GEN_HANDLER2(andi_, "andi.", 0x1C, 0xFF, 0xFF, 0x00000000, PPC_INTEGER),
9204GEN_HANDLER2(andis_, "andis.", 0x1D, 0xFF, 0xFF, 0x00000000, PPC_INTEGER),
9205GEN_HANDLER(cntlzw, 0x1F, 0x1A, 0x00, 0x00000000, PPC_INTEGER),
9206GEN_HANDLER(or, 0x1F, 0x1C, 0x0D, 0x00000000, PPC_INTEGER),
9207GEN_HANDLER(xor, 0x1F, 0x1C, 0x09, 0x00000000, PPC_INTEGER),
9208GEN_HANDLER(ori, 0x18, 0xFF, 0xFF, 0x00000000, PPC_INTEGER),
9209GEN_HANDLER(oris, 0x19, 0xFF, 0xFF, 0x00000000, PPC_INTEGER),
9210GEN_HANDLER(xori, 0x1A, 0xFF, 0xFF, 0x00000000, PPC_INTEGER),
9211GEN_HANDLER(xoris, 0x1B, 0xFF, 0xFF, 0x00000000, PPC_INTEGER),
9212GEN_HANDLER(popcntb, 0x1F, 0x03, 0x03, 0x0000F801, PPC_POPCNTB),
eaabeef2 9213GEN_HANDLER(popcntw, 0x1F, 0x1A, 0x0b, 0x0000F801, PPC_POPCNTWD),
725bcec2 9214GEN_HANDLER_E(prtyw, 0x1F, 0x1A, 0x04, 0x0000F801, PPC_NONE, PPC2_ISA205),
5c55ff99 9215#if defined(TARGET_PPC64)
eaabeef2 9216GEN_HANDLER(popcntd, 0x1F, 0x1A, 0x0F, 0x0000F801, PPC_POPCNTWD),
5c55ff99 9217GEN_HANDLER(cntlzd, 0x1F, 0x1A, 0x01, 0x00000000, PPC_64B),
725bcec2 9218GEN_HANDLER_E(prtyd, 0x1F, 0x1A, 0x05, 0x0000F801, PPC_NONE, PPC2_ISA205),
5c55ff99
BS
9219#endif
9220GEN_HANDLER(rlwimi, 0x14, 0xFF, 0xFF, 0x00000000, PPC_INTEGER),
9221GEN_HANDLER(rlwinm, 0x15, 0xFF, 0xFF, 0x00000000, PPC_INTEGER),
9222GEN_HANDLER(rlwnm, 0x17, 0xFF, 0xFF, 0x00000000, PPC_INTEGER),
9223GEN_HANDLER(slw, 0x1F, 0x18, 0x00, 0x00000000, PPC_INTEGER),
9224GEN_HANDLER(sraw, 0x1F, 0x18, 0x18, 0x00000000, PPC_INTEGER),
9225GEN_HANDLER(srawi, 0x1F, 0x18, 0x19, 0x00000000, PPC_INTEGER),
9226GEN_HANDLER(srw, 0x1F, 0x18, 0x10, 0x00000000, PPC_INTEGER),
9227#if defined(TARGET_PPC64)
9228GEN_HANDLER(sld, 0x1F, 0x1B, 0x00, 0x00000000, PPC_64B),
9229GEN_HANDLER(srad, 0x1F, 0x1A, 0x18, 0x00000000, PPC_64B),
9230GEN_HANDLER2(sradi0, "sradi", 0x1F, 0x1A, 0x19, 0x00000000, PPC_64B),
9231GEN_HANDLER2(sradi1, "sradi", 0x1F, 0x1B, 0x19, 0x00000000, PPC_64B),
9232GEN_HANDLER(srd, 0x1F, 0x1B, 0x10, 0x00000000, PPC_64B),
9233#endif
9234GEN_HANDLER(frsqrtes, 0x3B, 0x1A, 0xFF, 0x001F07C0, PPC_FLOAT_FRSQRTES),
9235GEN_HANDLER(fsqrt, 0x3F, 0x16, 0xFF, 0x001F07C0, PPC_FLOAT_FSQRT),
9236GEN_HANDLER(fsqrts, 0x3B, 0x16, 0xFF, 0x001F07C0, PPC_FLOAT_FSQRT),
9237GEN_HANDLER(fcmpo, 0x3F, 0x00, 0x01, 0x00600001, PPC_FLOAT),
9238GEN_HANDLER(fcmpu, 0x3F, 0x00, 0x00, 0x00600001, PPC_FLOAT),
bf45a2e6 9239GEN_HANDLER(fabs, 0x3F, 0x08, 0x08, 0x001F0000, PPC_FLOAT),
5c55ff99 9240GEN_HANDLER(fmr, 0x3F, 0x08, 0x02, 0x001F0000, PPC_FLOAT),
bf45a2e6
AJ
9241GEN_HANDLER(fnabs, 0x3F, 0x08, 0x04, 0x001F0000, PPC_FLOAT),
9242GEN_HANDLER(fneg, 0x3F, 0x08, 0x01, 0x001F0000, PPC_FLOAT),
f0332888 9243GEN_HANDLER_E(fcpsgn, 0x3F, 0x08, 0x00, 0x00000000, PPC_NONE, PPC2_ISA205),
5c55ff99
BS
9244GEN_HANDLER(mcrfs, 0x3F, 0x00, 0x02, 0x0063F801, PPC_FLOAT),
9245GEN_HANDLER(mffs, 0x3F, 0x07, 0x12, 0x001FF800, PPC_FLOAT),
9246GEN_HANDLER(mtfsb0, 0x3F, 0x06, 0x02, 0x001FF800, PPC_FLOAT),
9247GEN_HANDLER(mtfsb1, 0x3F, 0x06, 0x01, 0x001FF800, PPC_FLOAT),
7d08d856
AJ
9248GEN_HANDLER(mtfsf, 0x3F, 0x07, 0x16, 0x00000000, PPC_FLOAT),
9249GEN_HANDLER(mtfsfi, 0x3F, 0x06, 0x04, 0x006e0800, PPC_FLOAT),
5c55ff99
BS
9250#if defined(TARGET_PPC64)
9251GEN_HANDLER(ld, 0x3A, 0xFF, 0xFF, 0x00000000, PPC_64B),
9252GEN_HANDLER(lq, 0x38, 0xFF, 0xFF, 0x00000000, PPC_64BX),
9253GEN_HANDLER(std, 0x3E, 0xFF, 0xFF, 0x00000000, PPC_64B),
9254#endif
9255GEN_HANDLER(lmw, 0x2E, 0xFF, 0xFF, 0x00000000, PPC_INTEGER),
9256GEN_HANDLER(stmw, 0x2F, 0xFF, 0xFF, 0x00000000, PPC_INTEGER),
9257GEN_HANDLER(lswi, 0x1F, 0x15, 0x12, 0x00000001, PPC_STRING),
9258GEN_HANDLER(lswx, 0x1F, 0x15, 0x10, 0x00000001, PPC_STRING),
9259GEN_HANDLER(stswi, 0x1F, 0x15, 0x16, 0x00000001, PPC_STRING),
9260GEN_HANDLER(stswx, 0x1F, 0x15, 0x14, 0x00000001, PPC_STRING),
9261GEN_HANDLER(eieio, 0x1F, 0x16, 0x1A, 0x03FFF801, PPC_MEM_EIEIO),
9262GEN_HANDLER(isync, 0x13, 0x16, 0x04, 0x03FFF801, PPC_MEM),
f844c817 9263GEN_HANDLER(lwarx, 0x1F, 0x14, 0x00, 0x00000000, PPC_RES),
5c55ff99
BS
9264GEN_HANDLER2(stwcx_, "stwcx.", 0x1F, 0x16, 0x04, 0x00000000, PPC_RES),
9265#if defined(TARGET_PPC64)
f844c817 9266GEN_HANDLER(ldarx, 0x1F, 0x14, 0x02, 0x00000000, PPC_64B),
5c55ff99
BS
9267GEN_HANDLER2(stdcx_, "stdcx.", 0x1F, 0x16, 0x06, 0x00000000, PPC_64B),
9268#endif
9269GEN_HANDLER(sync, 0x1F, 0x16, 0x12, 0x039FF801, PPC_MEM_SYNC),
9270GEN_HANDLER(wait, 0x1F, 0x1E, 0x01, 0x03FFF801, PPC_WAIT),
9271GEN_HANDLER(b, 0x12, 0xFF, 0xFF, 0x00000000, PPC_FLOW),
9272GEN_HANDLER(bc, 0x10, 0xFF, 0xFF, 0x00000000, PPC_FLOW),
9273GEN_HANDLER(bcctr, 0x13, 0x10, 0x10, 0x00000000, PPC_FLOW),
9274GEN_HANDLER(bclr, 0x13, 0x10, 0x00, 0x00000000, PPC_FLOW),
9275GEN_HANDLER(mcrf, 0x13, 0x00, 0xFF, 0x00000001, PPC_INTEGER),
9276GEN_HANDLER(rfi, 0x13, 0x12, 0x01, 0x03FF8001, PPC_FLOW),
9277#if defined(TARGET_PPC64)
9278GEN_HANDLER(rfid, 0x13, 0x12, 0x00, 0x03FF8001, PPC_64B),
9279GEN_HANDLER(hrfid, 0x13, 0x12, 0x08, 0x03FF8001, PPC_64H),
9280#endif
9281GEN_HANDLER(sc, 0x11, 0xFF, 0xFF, 0x03FFF01D, PPC_FLOW),
9282GEN_HANDLER(tw, 0x1F, 0x04, 0x00, 0x00000001, PPC_FLOW),
9283GEN_HANDLER(twi, 0x03, 0xFF, 0xFF, 0x00000000, PPC_FLOW),
9284#if defined(TARGET_PPC64)
9285GEN_HANDLER(td, 0x1F, 0x04, 0x02, 0x00000001, PPC_64B),
9286GEN_HANDLER(tdi, 0x02, 0xFF, 0xFF, 0x00000000, PPC_64B),
9287#endif
9288GEN_HANDLER(mcrxr, 0x1F, 0x00, 0x10, 0x007FF801, PPC_MISC),
9289GEN_HANDLER(mfcr, 0x1F, 0x13, 0x00, 0x00000801, PPC_MISC),
9290GEN_HANDLER(mfmsr, 0x1F, 0x13, 0x02, 0x001FF801, PPC_MISC),
9291GEN_HANDLER(mfspr, 0x1F, 0x13, 0x0A, 0x00000001, PPC_MISC),
9292GEN_HANDLER(mftb, 0x1F, 0x13, 0x0B, 0x00000001, PPC_MFTB),
9293GEN_HANDLER(mtcrf, 0x1F, 0x10, 0x04, 0x00000801, PPC_MISC),
9294#if defined(TARGET_PPC64)
9295GEN_HANDLER(mtmsrd, 0x1F, 0x12, 0x05, 0x001EF801, PPC_64B),
9296#endif
9297GEN_HANDLER(mtmsr, 0x1F, 0x12, 0x04, 0x001FF801, PPC_MISC),
9298GEN_HANDLER(mtspr, 0x1F, 0x13, 0x0E, 0x00000001, PPC_MISC),
9299GEN_HANDLER(dcbf, 0x1F, 0x16, 0x02, 0x03C00001, PPC_CACHE),
9300GEN_HANDLER(dcbi, 0x1F, 0x16, 0x0E, 0x03E00001, PPC_CACHE),
9301GEN_HANDLER(dcbst, 0x1F, 0x16, 0x01, 0x03E00001, PPC_CACHE),
9302GEN_HANDLER(dcbt, 0x1F, 0x16, 0x08, 0x02000001, PPC_CACHE),
9303GEN_HANDLER(dcbtst, 0x1F, 0x16, 0x07, 0x02000001, PPC_CACHE),
8e33944f 9304GEN_HANDLER(dcbz, 0x1F, 0x16, 0x1F, 0x03C00001, PPC_CACHE_DCBZ),
5c55ff99
BS
9305GEN_HANDLER(dst, 0x1F, 0x16, 0x0A, 0x01800001, PPC_ALTIVEC),
9306GEN_HANDLER(dstst, 0x1F, 0x16, 0x0B, 0x02000001, PPC_ALTIVEC),
9307GEN_HANDLER(dss, 0x1F, 0x16, 0x19, 0x019FF801, PPC_ALTIVEC),
9308GEN_HANDLER(icbi, 0x1F, 0x16, 0x1E, 0x03E00001, PPC_CACHE_ICBI),
9309GEN_HANDLER(dcba, 0x1F, 0x16, 0x17, 0x03E00001, PPC_CACHE_DCBA),
9310GEN_HANDLER(mfsr, 0x1F, 0x13, 0x12, 0x0010F801, PPC_SEGMENT),
9311GEN_HANDLER(mfsrin, 0x1F, 0x13, 0x14, 0x001F0001, PPC_SEGMENT),
9312GEN_HANDLER(mtsr, 0x1F, 0x12, 0x06, 0x0010F801, PPC_SEGMENT),
9313GEN_HANDLER(mtsrin, 0x1F, 0x12, 0x07, 0x001F0001, PPC_SEGMENT),
9314#if defined(TARGET_PPC64)
9315GEN_HANDLER2(mfsr_64b, "mfsr", 0x1F, 0x13, 0x12, 0x0010F801, PPC_SEGMENT_64B),
9316GEN_HANDLER2(mfsrin_64b, "mfsrin", 0x1F, 0x13, 0x14, 0x001F0001,
9317 PPC_SEGMENT_64B),
9318GEN_HANDLER2(mtsr_64b, "mtsr", 0x1F, 0x12, 0x06, 0x0010F801, PPC_SEGMENT_64B),
9319GEN_HANDLER2(mtsrin_64b, "mtsrin", 0x1F, 0x12, 0x07, 0x001F0001,
9320 PPC_SEGMENT_64B),
efdef95f
DG
9321GEN_HANDLER2(slbmte, "slbmte", 0x1F, 0x12, 0x0C, 0x001F0001, PPC_SEGMENT_64B),
9322GEN_HANDLER2(slbmfee, "slbmfee", 0x1F, 0x13, 0x1C, 0x001F0001, PPC_SEGMENT_64B),
9323GEN_HANDLER2(slbmfev, "slbmfev", 0x1F, 0x13, 0x1A, 0x001F0001, PPC_SEGMENT_64B),
5c55ff99
BS
9324#endif
9325GEN_HANDLER(tlbia, 0x1F, 0x12, 0x0B, 0x03FFFC01, PPC_MEM_TLBIA),
9326GEN_HANDLER(tlbiel, 0x1F, 0x12, 0x08, 0x03FF0001, PPC_MEM_TLBIE),
9327GEN_HANDLER(tlbie, 0x1F, 0x12, 0x09, 0x03FF0001, PPC_MEM_TLBIE),
9328GEN_HANDLER(tlbsync, 0x1F, 0x16, 0x11, 0x03FFF801, PPC_MEM_TLBSYNC),
9329#if defined(TARGET_PPC64)
9330GEN_HANDLER(slbia, 0x1F, 0x12, 0x0F, 0x03FFFC01, PPC_SLBI),
9331GEN_HANDLER(slbie, 0x1F, 0x12, 0x0D, 0x03FF0001, PPC_SLBI),
9332#endif
9333GEN_HANDLER(eciwx, 0x1F, 0x16, 0x0D, 0x00000001, PPC_EXTERN),
9334GEN_HANDLER(ecowx, 0x1F, 0x16, 0x09, 0x00000001, PPC_EXTERN),
9335GEN_HANDLER(abs, 0x1F, 0x08, 0x0B, 0x0000F800, PPC_POWER_BR),
9336GEN_HANDLER(abso, 0x1F, 0x08, 0x1B, 0x0000F800, PPC_POWER_BR),
9337GEN_HANDLER(clcs, 0x1F, 0x10, 0x13, 0x0000F800, PPC_POWER_BR),
9338GEN_HANDLER(div, 0x1F, 0x0B, 0x0A, 0x00000000, PPC_POWER_BR),
9339GEN_HANDLER(divo, 0x1F, 0x0B, 0x1A, 0x00000000, PPC_POWER_BR),
9340GEN_HANDLER(divs, 0x1F, 0x0B, 0x0B, 0x00000000, PPC_POWER_BR),
9341GEN_HANDLER(divso, 0x1F, 0x0B, 0x1B, 0x00000000, PPC_POWER_BR),
9342GEN_HANDLER(doz, 0x1F, 0x08, 0x08, 0x00000000, PPC_POWER_BR),
9343GEN_HANDLER(dozo, 0x1F, 0x08, 0x18, 0x00000000, PPC_POWER_BR),
9344GEN_HANDLER(dozi, 0x09, 0xFF, 0xFF, 0x00000000, PPC_POWER_BR),
9345GEN_HANDLER(lscbx, 0x1F, 0x15, 0x08, 0x00000000, PPC_POWER_BR),
9346GEN_HANDLER(maskg, 0x1F, 0x1D, 0x00, 0x00000000, PPC_POWER_BR),
9347GEN_HANDLER(maskir, 0x1F, 0x1D, 0x10, 0x00000000, PPC_POWER_BR),
9348GEN_HANDLER(mul, 0x1F, 0x0B, 0x03, 0x00000000, PPC_POWER_BR),
9349GEN_HANDLER(mulo, 0x1F, 0x0B, 0x13, 0x00000000, PPC_POWER_BR),
9350GEN_HANDLER(nabs, 0x1F, 0x08, 0x0F, 0x00000000, PPC_POWER_BR),
9351GEN_HANDLER(nabso, 0x1F, 0x08, 0x1F, 0x00000000, PPC_POWER_BR),
9352GEN_HANDLER(rlmi, 0x16, 0xFF, 0xFF, 0x00000000, PPC_POWER_BR),
9353GEN_HANDLER(rrib, 0x1F, 0x19, 0x10, 0x00000000, PPC_POWER_BR),
9354GEN_HANDLER(sle, 0x1F, 0x19, 0x04, 0x00000000, PPC_POWER_BR),
9355GEN_HANDLER(sleq, 0x1F, 0x19, 0x06, 0x00000000, PPC_POWER_BR),
9356GEN_HANDLER(sliq, 0x1F, 0x18, 0x05, 0x00000000, PPC_POWER_BR),
9357GEN_HANDLER(slliq, 0x1F, 0x18, 0x07, 0x00000000, PPC_POWER_BR),
9358GEN_HANDLER(sllq, 0x1F, 0x18, 0x06, 0x00000000, PPC_POWER_BR),
9359GEN_HANDLER(slq, 0x1F, 0x18, 0x04, 0x00000000, PPC_POWER_BR),
9360GEN_HANDLER(sraiq, 0x1F, 0x18, 0x1D, 0x00000000, PPC_POWER_BR),
9361GEN_HANDLER(sraq, 0x1F, 0x18, 0x1C, 0x00000000, PPC_POWER_BR),
9362GEN_HANDLER(sre, 0x1F, 0x19, 0x14, 0x00000000, PPC_POWER_BR),
9363GEN_HANDLER(srea, 0x1F, 0x19, 0x1C, 0x00000000, PPC_POWER_BR),
9364GEN_HANDLER(sreq, 0x1F, 0x19, 0x16, 0x00000000, PPC_POWER_BR),
9365GEN_HANDLER(sriq, 0x1F, 0x18, 0x15, 0x00000000, PPC_POWER_BR),
9366GEN_HANDLER(srliq, 0x1F, 0x18, 0x17, 0x00000000, PPC_POWER_BR),
9367GEN_HANDLER(srlq, 0x1F, 0x18, 0x16, 0x00000000, PPC_POWER_BR),
9368GEN_HANDLER(srq, 0x1F, 0x18, 0x14, 0x00000000, PPC_POWER_BR),
9369GEN_HANDLER(dsa, 0x1F, 0x14, 0x13, 0x03FFF801, PPC_602_SPEC),
9370GEN_HANDLER(esa, 0x1F, 0x14, 0x12, 0x03FFF801, PPC_602_SPEC),
9371GEN_HANDLER(mfrom, 0x1F, 0x09, 0x08, 0x03E0F801, PPC_602_SPEC),
9372GEN_HANDLER2(tlbld_6xx, "tlbld", 0x1F, 0x12, 0x1E, 0x03FF0001, PPC_6xx_TLB),
9373GEN_HANDLER2(tlbli_6xx, "tlbli", 0x1F, 0x12, 0x1F, 0x03FF0001, PPC_6xx_TLB),
9374GEN_HANDLER2(tlbld_74xx, "tlbld", 0x1F, 0x12, 0x1E, 0x03FF0001, PPC_74xx_TLB),
9375GEN_HANDLER2(tlbli_74xx, "tlbli", 0x1F, 0x12, 0x1F, 0x03FF0001, PPC_74xx_TLB),
9376GEN_HANDLER(clf, 0x1F, 0x16, 0x03, 0x03E00000, PPC_POWER),
9377GEN_HANDLER(cli, 0x1F, 0x16, 0x0F, 0x03E00000, PPC_POWER),
9378GEN_HANDLER(dclst, 0x1F, 0x16, 0x13, 0x03E00000, PPC_POWER),
9379GEN_HANDLER(mfsri, 0x1F, 0x13, 0x13, 0x00000001, PPC_POWER),
9380GEN_HANDLER(rac, 0x1F, 0x12, 0x19, 0x00000001, PPC_POWER),
9381GEN_HANDLER(rfsvc, 0x13, 0x12, 0x02, 0x03FFF0001, PPC_POWER),
9382GEN_HANDLER(lfq, 0x38, 0xFF, 0xFF, 0x00000003, PPC_POWER2),
9383GEN_HANDLER(lfqu, 0x39, 0xFF, 0xFF, 0x00000003, PPC_POWER2),
9384GEN_HANDLER(lfqux, 0x1F, 0x17, 0x19, 0x00000001, PPC_POWER2),
9385GEN_HANDLER(lfqx, 0x1F, 0x17, 0x18, 0x00000001, PPC_POWER2),
9386GEN_HANDLER(stfq, 0x3C, 0xFF, 0xFF, 0x00000003, PPC_POWER2),
9387GEN_HANDLER(stfqu, 0x3D, 0xFF, 0xFF, 0x00000003, PPC_POWER2),
9388GEN_HANDLER(stfqux, 0x1F, 0x17, 0x1D, 0x00000001, PPC_POWER2),
9389GEN_HANDLER(stfqx, 0x1F, 0x17, 0x1C, 0x00000001, PPC_POWER2),
9390GEN_HANDLER(mfapidi, 0x1F, 0x13, 0x08, 0x0000F801, PPC_MFAPIDI),
9391GEN_HANDLER(tlbiva, 0x1F, 0x12, 0x18, 0x03FFF801, PPC_TLBIVA),
9392GEN_HANDLER(mfdcr, 0x1F, 0x03, 0x0A, 0x00000001, PPC_DCR),
9393GEN_HANDLER(mtdcr, 0x1F, 0x03, 0x0E, 0x00000001, PPC_DCR),
9394GEN_HANDLER(mfdcrx, 0x1F, 0x03, 0x08, 0x00000000, PPC_DCRX),
9395GEN_HANDLER(mtdcrx, 0x1F, 0x03, 0x0C, 0x00000000, PPC_DCRX),
9396GEN_HANDLER(mfdcrux, 0x1F, 0x03, 0x09, 0x00000000, PPC_DCRUX),
9397GEN_HANDLER(mtdcrux, 0x1F, 0x03, 0x0D, 0x00000000, PPC_DCRUX),
9398GEN_HANDLER(dccci, 0x1F, 0x06, 0x0E, 0x03E00001, PPC_4xx_COMMON),
9399GEN_HANDLER(dcread, 0x1F, 0x06, 0x0F, 0x00000001, PPC_4xx_COMMON),
9400GEN_HANDLER2(icbt_40x, "icbt", 0x1F, 0x06, 0x08, 0x03E00001, PPC_40x_ICBT),
9401GEN_HANDLER(iccci, 0x1F, 0x06, 0x1E, 0x00000001, PPC_4xx_COMMON),
9402GEN_HANDLER(icread, 0x1F, 0x06, 0x1F, 0x03E00001, PPC_4xx_COMMON),
9403GEN_HANDLER2(rfci_40x, "rfci", 0x13, 0x13, 0x01, 0x03FF8001, PPC_40x_EXCP),
01662f3e 9404GEN_HANDLER_E(rfci, 0x13, 0x13, 0x01, 0x03FF8001, PPC_BOOKE, PPC2_BOOKE206),
5c55ff99
BS
9405GEN_HANDLER(rfdi, 0x13, 0x07, 0x01, 0x03FF8001, PPC_RFDI),
9406GEN_HANDLER(rfmci, 0x13, 0x06, 0x01, 0x03FF8001, PPC_RFMCI),
9407GEN_HANDLER2(tlbre_40x, "tlbre", 0x1F, 0x12, 0x1D, 0x00000001, PPC_40x_TLB),
9408GEN_HANDLER2(tlbsx_40x, "tlbsx", 0x1F, 0x12, 0x1C, 0x00000000, PPC_40x_TLB),
9409GEN_HANDLER2(tlbwe_40x, "tlbwe", 0x1F, 0x12, 0x1E, 0x00000001, PPC_40x_TLB),
9410GEN_HANDLER2(tlbre_440, "tlbre", 0x1F, 0x12, 0x1D, 0x00000001, PPC_BOOKE),
9411GEN_HANDLER2(tlbsx_440, "tlbsx", 0x1F, 0x12, 0x1C, 0x00000000, PPC_BOOKE),
9412GEN_HANDLER2(tlbwe_440, "tlbwe", 0x1F, 0x12, 0x1E, 0x00000001, PPC_BOOKE),
01662f3e
AG
9413GEN_HANDLER2_E(tlbre_booke206, "tlbre", 0x1F, 0x12, 0x1D, 0x00000001,
9414 PPC_NONE, PPC2_BOOKE206),
9415GEN_HANDLER2_E(tlbsx_booke206, "tlbsx", 0x1F, 0x12, 0x1C, 0x00000000,
9416 PPC_NONE, PPC2_BOOKE206),
9417GEN_HANDLER2_E(tlbwe_booke206, "tlbwe", 0x1F, 0x12, 0x1E, 0x00000001,
9418 PPC_NONE, PPC2_BOOKE206),
9419GEN_HANDLER2_E(tlbivax_booke206, "tlbivax", 0x1F, 0x12, 0x18, 0x00000001,
9420 PPC_NONE, PPC2_BOOKE206),
6d3db821
AG
9421GEN_HANDLER2_E(tlbilx_booke206, "tlbilx", 0x1F, 0x12, 0x00, 0x03800001,
9422 PPC_NONE, PPC2_BOOKE206),
d5d11a39
AG
9423GEN_HANDLER2_E(msgsnd, "msgsnd", 0x1F, 0x0E, 0x06, 0x03ff0001,
9424 PPC_NONE, PPC2_PRCNTL),
9e0b5cb1
AG
9425GEN_HANDLER2_E(msgclr, "msgclr", 0x1F, 0x0E, 0x07, 0x03ff0001,
9426 PPC_NONE, PPC2_PRCNTL),
5c55ff99 9427GEN_HANDLER(wrtee, 0x1F, 0x03, 0x04, 0x000FFC01, PPC_WRTEE),
fbe73008 9428GEN_HANDLER(wrteei, 0x1F, 0x03, 0x05, 0x000E7C01, PPC_WRTEE),
5c55ff99 9429GEN_HANDLER(dlmzb, 0x1F, 0x0E, 0x02, 0x00000000, PPC_440_SPEC),
01662f3e
AG
9430GEN_HANDLER_E(mbar, 0x1F, 0x16, 0x1a, 0x001FF801,
9431 PPC_BOOKE, PPC2_BOOKE206),
dcb2b9e1 9432GEN_HANDLER(msync_4xx, 0x1F, 0x16, 0x12, 0x03FFF801, PPC_BOOKE),
01662f3e
AG
9433GEN_HANDLER2_E(icbt_440, "icbt", 0x1F, 0x16, 0x00, 0x03E00001,
9434 PPC_BOOKE, PPC2_BOOKE206),
5c55ff99
BS
9435GEN_HANDLER(lvsl, 0x1f, 0x06, 0x00, 0x00000001, PPC_ALTIVEC),
9436GEN_HANDLER(lvsr, 0x1f, 0x06, 0x01, 0x00000001, PPC_ALTIVEC),
9437GEN_HANDLER(mfvscr, 0x04, 0x2, 0x18, 0x001ff800, PPC_ALTIVEC),
9438GEN_HANDLER(mtvscr, 0x04, 0x2, 0x19, 0x03ff0000, PPC_ALTIVEC),
9439GEN_HANDLER(vsldoi, 0x04, 0x16, 0xFF, 0x00000400, PPC_ALTIVEC),
9440GEN_HANDLER(vmladduhm, 0x04, 0x11, 0xFF, 0x00000000, PPC_ALTIVEC),
9441GEN_HANDLER2(evsel0, "evsel", 0x04, 0x1c, 0x09, 0x00000000, PPC_SPE),
9442GEN_HANDLER2(evsel1, "evsel", 0x04, 0x1d, 0x09, 0x00000000, PPC_SPE),
9443GEN_HANDLER2(evsel2, "evsel", 0x04, 0x1e, 0x09, 0x00000000, PPC_SPE),
9444GEN_HANDLER2(evsel3, "evsel", 0x04, 0x1f, 0x09, 0x00000000, PPC_SPE),
9445
9446#undef GEN_INT_ARITH_ADD
9447#undef GEN_INT_ARITH_ADD_CONST
9448#define GEN_INT_ARITH_ADD(name, opc3, add_ca, compute_ca, compute_ov) \
9449GEN_HANDLER(name, 0x1F, 0x0A, opc3, 0x00000000, PPC_INTEGER),
9450#define GEN_INT_ARITH_ADD_CONST(name, opc3, const_val, \
9451 add_ca, compute_ca, compute_ov) \
9452GEN_HANDLER(name, 0x1F, 0x0A, opc3, 0x0000F800, PPC_INTEGER),
9453GEN_INT_ARITH_ADD(add, 0x08, 0, 0, 0)
9454GEN_INT_ARITH_ADD(addo, 0x18, 0, 0, 1)
9455GEN_INT_ARITH_ADD(addc, 0x00, 0, 1, 0)
9456GEN_INT_ARITH_ADD(addco, 0x10, 0, 1, 1)
9457GEN_INT_ARITH_ADD(adde, 0x04, 1, 1, 0)
9458GEN_INT_ARITH_ADD(addeo, 0x14, 1, 1, 1)
9459GEN_INT_ARITH_ADD_CONST(addme, 0x07, -1LL, 1, 1, 0)
9460GEN_INT_ARITH_ADD_CONST(addmeo, 0x17, -1LL, 1, 1, 1)
9461GEN_INT_ARITH_ADD_CONST(addze, 0x06, 0, 1, 1, 0)
9462GEN_INT_ARITH_ADD_CONST(addzeo, 0x16, 0, 1, 1, 1)
9463
9464#undef GEN_INT_ARITH_DIVW
9465#define GEN_INT_ARITH_DIVW(name, opc3, sign, compute_ov) \
9466GEN_HANDLER(name, 0x1F, 0x0B, opc3, 0x00000000, PPC_INTEGER)
9467GEN_INT_ARITH_DIVW(divwu, 0x0E, 0, 0),
9468GEN_INT_ARITH_DIVW(divwuo, 0x1E, 0, 1),
9469GEN_INT_ARITH_DIVW(divw, 0x0F, 1, 0),
9470GEN_INT_ARITH_DIVW(divwo, 0x1F, 1, 1),
9471
9472#if defined(TARGET_PPC64)
9473#undef GEN_INT_ARITH_DIVD
9474#define GEN_INT_ARITH_DIVD(name, opc3, sign, compute_ov) \
9475GEN_HANDLER(name, 0x1F, 0x09, opc3, 0x00000000, PPC_64B)
9476GEN_INT_ARITH_DIVD(divdu, 0x0E, 0, 0),
9477GEN_INT_ARITH_DIVD(divduo, 0x1E, 0, 1),
9478GEN_INT_ARITH_DIVD(divd, 0x0F, 1, 0),
9479GEN_INT_ARITH_DIVD(divdo, 0x1F, 1, 1),
9480
9481#undef GEN_INT_ARITH_MUL_HELPER
9482#define GEN_INT_ARITH_MUL_HELPER(name, opc3) \
9483GEN_HANDLER(name, 0x1F, 0x09, opc3, 0x00000000, PPC_64B)
9484GEN_INT_ARITH_MUL_HELPER(mulhdu, 0x00),
9485GEN_INT_ARITH_MUL_HELPER(mulhd, 0x02),
9486GEN_INT_ARITH_MUL_HELPER(mulldo, 0x17),
9487#endif
9488
9489#undef GEN_INT_ARITH_SUBF
9490#undef GEN_INT_ARITH_SUBF_CONST
9491#define GEN_INT_ARITH_SUBF(name, opc3, add_ca, compute_ca, compute_ov) \
9492GEN_HANDLER(name, 0x1F, 0x08, opc3, 0x00000000, PPC_INTEGER),
9493#define GEN_INT_ARITH_SUBF_CONST(name, opc3, const_val, \
9494 add_ca, compute_ca, compute_ov) \
9495GEN_HANDLER(name, 0x1F, 0x08, opc3, 0x0000F800, PPC_INTEGER),
9496GEN_INT_ARITH_SUBF(subf, 0x01, 0, 0, 0)
9497GEN_INT_ARITH_SUBF(subfo, 0x11, 0, 0, 1)
9498GEN_INT_ARITH_SUBF(subfc, 0x00, 0, 1, 0)
9499GEN_INT_ARITH_SUBF(subfco, 0x10, 0, 1, 1)
9500GEN_INT_ARITH_SUBF(subfe, 0x04, 1, 1, 0)
9501GEN_INT_ARITH_SUBF(subfeo, 0x14, 1, 1, 1)
9502GEN_INT_ARITH_SUBF_CONST(subfme, 0x07, -1LL, 1, 1, 0)
9503GEN_INT_ARITH_SUBF_CONST(subfmeo, 0x17, -1LL, 1, 1, 1)
9504GEN_INT_ARITH_SUBF_CONST(subfze, 0x06, 0, 1, 1, 0)
9505GEN_INT_ARITH_SUBF_CONST(subfzeo, 0x16, 0, 1, 1, 1)
9506
9507#undef GEN_LOGICAL1
9508#undef GEN_LOGICAL2
9509#define GEN_LOGICAL2(name, tcg_op, opc, type) \
9510GEN_HANDLER(name, 0x1F, 0x1C, opc, 0x00000000, type)
9511#define GEN_LOGICAL1(name, tcg_op, opc, type) \
9512GEN_HANDLER(name, 0x1F, 0x1A, opc, 0x00000000, type)
9513GEN_LOGICAL2(and, tcg_gen_and_tl, 0x00, PPC_INTEGER),
9514GEN_LOGICAL2(andc, tcg_gen_andc_tl, 0x01, PPC_INTEGER),
9515GEN_LOGICAL2(eqv, tcg_gen_eqv_tl, 0x08, PPC_INTEGER),
9516GEN_LOGICAL1(extsb, tcg_gen_ext8s_tl, 0x1D, PPC_INTEGER),
9517GEN_LOGICAL1(extsh, tcg_gen_ext16s_tl, 0x1C, PPC_INTEGER),
9518GEN_LOGICAL2(nand, tcg_gen_nand_tl, 0x0E, PPC_INTEGER),
9519GEN_LOGICAL2(nor, tcg_gen_nor_tl, 0x03, PPC_INTEGER),
9520GEN_LOGICAL2(orc, tcg_gen_orc_tl, 0x0C, PPC_INTEGER),
9521#if defined(TARGET_PPC64)
9522GEN_LOGICAL1(extsw, tcg_gen_ext32s_tl, 0x1E, PPC_64B),
9523#endif
9524
9525#if defined(TARGET_PPC64)
9526#undef GEN_PPC64_R2
9527#undef GEN_PPC64_R4
9528#define GEN_PPC64_R2(name, opc1, opc2) \
9529GEN_HANDLER2(name##0, stringify(name), opc1, opc2, 0xFF, 0x00000000, PPC_64B),\
9530GEN_HANDLER2(name##1, stringify(name), opc1, opc2 | 0x10, 0xFF, 0x00000000, \
9531 PPC_64B)
9532#define GEN_PPC64_R4(name, opc1, opc2) \
9533GEN_HANDLER2(name##0, stringify(name), opc1, opc2, 0xFF, 0x00000000, PPC_64B),\
9534GEN_HANDLER2(name##1, stringify(name), opc1, opc2 | 0x01, 0xFF, 0x00000000, \
9535 PPC_64B), \
9536GEN_HANDLER2(name##2, stringify(name), opc1, opc2 | 0x10, 0xFF, 0x00000000, \
9537 PPC_64B), \
9538GEN_HANDLER2(name##3, stringify(name), opc1, opc2 | 0x11, 0xFF, 0x00000000, \
9539 PPC_64B)
9540GEN_PPC64_R4(rldicl, 0x1E, 0x00),
9541GEN_PPC64_R4(rldicr, 0x1E, 0x02),
9542GEN_PPC64_R4(rldic, 0x1E, 0x04),
9543GEN_PPC64_R2(rldcl, 0x1E, 0x08),
9544GEN_PPC64_R2(rldcr, 0x1E, 0x09),
9545GEN_PPC64_R4(rldimi, 0x1E, 0x06),
9546#endif
9547
9548#undef _GEN_FLOAT_ACB
9549#undef GEN_FLOAT_ACB
9550#undef _GEN_FLOAT_AB
9551#undef GEN_FLOAT_AB
9552#undef _GEN_FLOAT_AC
9553#undef GEN_FLOAT_AC
9554#undef GEN_FLOAT_B
9555#undef GEN_FLOAT_BS
9556#define _GEN_FLOAT_ACB(name, op, op1, op2, isfloat, set_fprf, type) \
9557GEN_HANDLER(f##name, op1, op2, 0xFF, 0x00000000, type)
9558#define GEN_FLOAT_ACB(name, op2, set_fprf, type) \
9559_GEN_FLOAT_ACB(name, name, 0x3F, op2, 0, set_fprf, type), \
9560_GEN_FLOAT_ACB(name##s, name, 0x3B, op2, 1, set_fprf, type)
9561#define _GEN_FLOAT_AB(name, op, op1, op2, inval, isfloat, set_fprf, type) \
9562GEN_HANDLER(f##name, op1, op2, 0xFF, inval, type)
9563#define GEN_FLOAT_AB(name, op2, inval, set_fprf, type) \
9564_GEN_FLOAT_AB(name, name, 0x3F, op2, inval, 0, set_fprf, type), \
9565_GEN_FLOAT_AB(name##s, name, 0x3B, op2, inval, 1, set_fprf, type)
9566#define _GEN_FLOAT_AC(name, op, op1, op2, inval, isfloat, set_fprf, type) \
9567GEN_HANDLER(f##name, op1, op2, 0xFF, inval, type)
9568#define GEN_FLOAT_AC(name, op2, inval, set_fprf, type) \
9569_GEN_FLOAT_AC(name, name, 0x3F, op2, inval, 0, set_fprf, type), \
9570_GEN_FLOAT_AC(name##s, name, 0x3B, op2, inval, 1, set_fprf, type)
9571#define GEN_FLOAT_B(name, op2, op3, set_fprf, type) \
9572GEN_HANDLER(f##name, 0x3F, op2, op3, 0x001F0000, type)
9573#define GEN_FLOAT_BS(name, op1, op2, set_fprf, type) \
9574GEN_HANDLER(f##name, op1, op2, 0xFF, 0x001F07C0, type)
9575
9576GEN_FLOAT_AB(add, 0x15, 0x000007C0, 1, PPC_FLOAT),
9577GEN_FLOAT_AB(div, 0x12, 0x000007C0, 1, PPC_FLOAT),
9578GEN_FLOAT_AC(mul, 0x19, 0x0000F800, 1, PPC_FLOAT),
9579GEN_FLOAT_BS(re, 0x3F, 0x18, 1, PPC_FLOAT_EXT),
9580GEN_FLOAT_BS(res, 0x3B, 0x18, 1, PPC_FLOAT_FRES),
9581GEN_FLOAT_BS(rsqrte, 0x3F, 0x1A, 1, PPC_FLOAT_FRSQRTE),
9582_GEN_FLOAT_ACB(sel, sel, 0x3F, 0x17, 0, 0, PPC_FLOAT_FSEL),
9583GEN_FLOAT_AB(sub, 0x14, 0x000007C0, 1, PPC_FLOAT),
9584GEN_FLOAT_ACB(madd, 0x1D, 1, PPC_FLOAT),
9585GEN_FLOAT_ACB(msub, 0x1C, 1, PPC_FLOAT),
9586GEN_FLOAT_ACB(nmadd, 0x1F, 1, PPC_FLOAT),
9587GEN_FLOAT_ACB(nmsub, 0x1E, 1, PPC_FLOAT),
9588GEN_FLOAT_B(ctiw, 0x0E, 0x00, 0, PPC_FLOAT),
9589GEN_FLOAT_B(ctiwz, 0x0F, 0x00, 0, PPC_FLOAT),
9590GEN_FLOAT_B(rsp, 0x0C, 0x00, 1, PPC_FLOAT),
9591#if defined(TARGET_PPC64)
9592GEN_FLOAT_B(cfid, 0x0E, 0x1A, 1, PPC_64B),
9593GEN_FLOAT_B(ctid, 0x0E, 0x19, 0, PPC_64B),
9594GEN_FLOAT_B(ctidz, 0x0F, 0x19, 0, PPC_64B),
9595#endif
9596GEN_FLOAT_B(rin, 0x08, 0x0C, 1, PPC_FLOAT_EXT),
9597GEN_FLOAT_B(riz, 0x08, 0x0D, 1, PPC_FLOAT_EXT),
9598GEN_FLOAT_B(rip, 0x08, 0x0E, 1, PPC_FLOAT_EXT),
9599GEN_FLOAT_B(rim, 0x08, 0x0F, 1, PPC_FLOAT_EXT),
5c55ff99
BS
9600
9601#undef GEN_LD
9602#undef GEN_LDU
9603#undef GEN_LDUX
cd6e9320 9604#undef GEN_LDX_E
5c55ff99
BS
9605#undef GEN_LDS
9606#define GEN_LD(name, ldop, opc, type) \
9607GEN_HANDLER(name, opc, 0xFF, 0xFF, 0x00000000, type),
9608#define GEN_LDU(name, ldop, opc, type) \
9609GEN_HANDLER(name##u, opc, 0xFF, 0xFF, 0x00000000, type),
9610#define GEN_LDUX(name, ldop, opc2, opc3, type) \
9611GEN_HANDLER(name##ux, 0x1F, opc2, opc3, 0x00000001, type),
cd6e9320
TH
9612#define GEN_LDX_E(name, ldop, opc2, opc3, type, type2) \
9613GEN_HANDLER_E(name##x, 0x1F, opc2, opc3, 0x00000001, type, type2),
5c55ff99
BS
9614#define GEN_LDS(name, ldop, op, type) \
9615GEN_LD(name, ldop, op | 0x20, type) \
9616GEN_LDU(name, ldop, op | 0x21, type) \
9617GEN_LDUX(name, ldop, 0x17, op | 0x01, type) \
9618GEN_LDX(name, ldop, 0x17, op | 0x00, type)
9619
9620GEN_LDS(lbz, ld8u, 0x02, PPC_INTEGER)
9621GEN_LDS(lha, ld16s, 0x0A, PPC_INTEGER)
9622GEN_LDS(lhz, ld16u, 0x08, PPC_INTEGER)
9623GEN_LDS(lwz, ld32u, 0x00, PPC_INTEGER)
9624#if defined(TARGET_PPC64)
9625GEN_LDUX(lwa, ld32s, 0x15, 0x0B, PPC_64B)
9626GEN_LDX(lwa, ld32s, 0x15, 0x0A, PPC_64B)
9627GEN_LDUX(ld, ld64, 0x15, 0x01, PPC_64B)
9628GEN_LDX(ld, ld64, 0x15, 0x00, PPC_64B)
cd6e9320 9629GEN_LDX_E(ldbr, ld64ur, 0x14, 0x10, PPC_NONE, PPC2_DBRX)
5c55ff99
BS
9630#endif
9631GEN_LDX(lhbr, ld16ur, 0x16, 0x18, PPC_INTEGER)
9632GEN_LDX(lwbr, ld32ur, 0x16, 0x10, PPC_INTEGER)
9633
9634#undef GEN_ST
9635#undef GEN_STU
9636#undef GEN_STUX
cd6e9320 9637#undef GEN_STX_E
5c55ff99
BS
9638#undef GEN_STS
9639#define GEN_ST(name, stop, opc, type) \
9640GEN_HANDLER(name, opc, 0xFF, 0xFF, 0x00000000, type),
9641#define GEN_STU(name, stop, opc, type) \
9642GEN_HANDLER(stop##u, opc, 0xFF, 0xFF, 0x00000000, type),
9643#define GEN_STUX(name, stop, opc2, opc3, type) \
9644GEN_HANDLER(name##ux, 0x1F, opc2, opc3, 0x00000001, type),
cd6e9320
TH
9645#define GEN_STX_E(name, stop, opc2, opc3, type, type2) \
9646GEN_HANDLER_E(name##x, 0x1F, opc2, opc3, 0x00000001, type, type2),
5c55ff99
BS
9647#define GEN_STS(name, stop, op, type) \
9648GEN_ST(name, stop, op | 0x20, type) \
9649GEN_STU(name, stop, op | 0x21, type) \
9650GEN_STUX(name, stop, 0x17, op | 0x01, type) \
9651GEN_STX(name, stop, 0x17, op | 0x00, type)
9652
9653GEN_STS(stb, st8, 0x06, PPC_INTEGER)
9654GEN_STS(sth, st16, 0x0C, PPC_INTEGER)
9655GEN_STS(stw, st32, 0x04, PPC_INTEGER)
9656#if defined(TARGET_PPC64)
9657GEN_STUX(std, st64, 0x15, 0x05, PPC_64B)
9658GEN_STX(std, st64, 0x15, 0x04, PPC_64B)
cd6e9320 9659GEN_STX_E(stdbr, st64r, 0x14, 0x14, PPC_NONE, PPC2_DBRX)
5c55ff99
BS
9660#endif
9661GEN_STX(sthbr, st16r, 0x16, 0x1C, PPC_INTEGER)
9662GEN_STX(stwbr, st32r, 0x16, 0x14, PPC_INTEGER)
9663
9664#undef GEN_LDF
9665#undef GEN_LDUF
9666#undef GEN_LDUXF
9667#undef GEN_LDXF
9668#undef GEN_LDFS
9669#define GEN_LDF(name, ldop, opc, type) \
9670GEN_HANDLER(name, opc, 0xFF, 0xFF, 0x00000000, type),
9671#define GEN_LDUF(name, ldop, opc, type) \
9672GEN_HANDLER(name##u, opc, 0xFF, 0xFF, 0x00000000, type),
9673#define GEN_LDUXF(name, ldop, opc, type) \
9674GEN_HANDLER(name##ux, 0x1F, 0x17, opc, 0x00000001, type),
9675#define GEN_LDXF(name, ldop, opc2, opc3, type) \
9676GEN_HANDLER(name##x, 0x1F, opc2, opc3, 0x00000001, type),
9677#define GEN_LDFS(name, ldop, op, type) \
9678GEN_LDF(name, ldop, op | 0x20, type) \
9679GEN_LDUF(name, ldop, op | 0x21, type) \
9680GEN_LDUXF(name, ldop, op | 0x01, type) \
9681GEN_LDXF(name, ldop, 0x17, op | 0x00, type)
9682
9683GEN_LDFS(lfd, ld64, 0x12, PPC_FLOAT)
9684GEN_LDFS(lfs, ld32fs, 0x10, PPC_FLOAT)
199f830d 9685GEN_HANDLER_E(lfiwax, 0x1f, 0x17, 0x1a, 0x00000001, PPC_NONE, PPC2_ISA205),
05050ee8
AJ
9686GEN_HANDLER_E(lfdp, 0x39, 0xFF, 0xFF, 0x00200003, PPC_NONE, PPC2_ISA205),
9687GEN_HANDLER_E(lfdpx, 0x1F, 0x17, 0x18, 0x00200001, PPC_NONE, PPC2_ISA205),
5c55ff99
BS
9688
9689#undef GEN_STF
9690#undef GEN_STUF
9691#undef GEN_STUXF
9692#undef GEN_STXF
9693#undef GEN_STFS
9694#define GEN_STF(name, stop, opc, type) \
9695GEN_HANDLER(name, opc, 0xFF, 0xFF, 0x00000000, type),
9696#define GEN_STUF(name, stop, opc, type) \
9697GEN_HANDLER(name##u, opc, 0xFF, 0xFF, 0x00000000, type),
9698#define GEN_STUXF(name, stop, opc, type) \
9699GEN_HANDLER(name##ux, 0x1F, 0x17, opc, 0x00000001, type),
9700#define GEN_STXF(name, stop, opc2, opc3, type) \
9701GEN_HANDLER(name##x, 0x1F, opc2, opc3, 0x00000001, type),
9702#define GEN_STFS(name, stop, op, type) \
9703GEN_STF(name, stop, op | 0x20, type) \
9704GEN_STUF(name, stop, op | 0x21, type) \
9705GEN_STUXF(name, stop, op | 0x01, type) \
9706GEN_STXF(name, stop, 0x17, op | 0x00, type)
9707
9708GEN_STFS(stfd, st64, 0x16, PPC_FLOAT)
9709GEN_STFS(stfs, st32fs, 0x14, PPC_FLOAT)
9710GEN_STXF(stfiw, st32fiw, 0x17, 0x1E, PPC_FLOAT_STFIWX)
44bc0c4d
AJ
9711GEN_HANDLER_E(stfdp, 0x3D, 0xFF, 0xFF, 0x00200003, PPC_NONE, PPC2_ISA205),
9712GEN_HANDLER_E(stfdpx, 0x1F, 0x17, 0x1C, 0x00200001, PPC_NONE, PPC2_ISA205),
5c55ff99
BS
9713
9714#undef GEN_CRLOGIC
9715#define GEN_CRLOGIC(name, tcg_op, opc) \
9716GEN_HANDLER(name, 0x13, 0x01, opc, 0x00000001, PPC_INTEGER)
9717GEN_CRLOGIC(crand, tcg_gen_and_i32, 0x08),
9718GEN_CRLOGIC(crandc, tcg_gen_andc_i32, 0x04),
9719GEN_CRLOGIC(creqv, tcg_gen_eqv_i32, 0x09),
9720GEN_CRLOGIC(crnand, tcg_gen_nand_i32, 0x07),
9721GEN_CRLOGIC(crnor, tcg_gen_nor_i32, 0x01),
9722GEN_CRLOGIC(cror, tcg_gen_or_i32, 0x0E),
9723GEN_CRLOGIC(crorc, tcg_gen_orc_i32, 0x0D),
9724GEN_CRLOGIC(crxor, tcg_gen_xor_i32, 0x06),
9725
9726#undef GEN_MAC_HANDLER
9727#define GEN_MAC_HANDLER(name, opc2, opc3) \
9728GEN_HANDLER(name, 0x04, opc2, opc3, 0x00000000, PPC_405_MAC)
9729GEN_MAC_HANDLER(macchw, 0x0C, 0x05),
9730GEN_MAC_HANDLER(macchwo, 0x0C, 0x15),
9731GEN_MAC_HANDLER(macchws, 0x0C, 0x07),
9732GEN_MAC_HANDLER(macchwso, 0x0C, 0x17),
9733GEN_MAC_HANDLER(macchwsu, 0x0C, 0x06),
9734GEN_MAC_HANDLER(macchwsuo, 0x0C, 0x16),
9735GEN_MAC_HANDLER(macchwu, 0x0C, 0x04),
9736GEN_MAC_HANDLER(macchwuo, 0x0C, 0x14),
9737GEN_MAC_HANDLER(machhw, 0x0C, 0x01),
9738GEN_MAC_HANDLER(machhwo, 0x0C, 0x11),
9739GEN_MAC_HANDLER(machhws, 0x0C, 0x03),
9740GEN_MAC_HANDLER(machhwso, 0x0C, 0x13),
9741GEN_MAC_HANDLER(machhwsu, 0x0C, 0x02),
9742GEN_MAC_HANDLER(machhwsuo, 0x0C, 0x12),
9743GEN_MAC_HANDLER(machhwu, 0x0C, 0x00),
9744GEN_MAC_HANDLER(machhwuo, 0x0C, 0x10),
9745GEN_MAC_HANDLER(maclhw, 0x0C, 0x0D),
9746GEN_MAC_HANDLER(maclhwo, 0x0C, 0x1D),
9747GEN_MAC_HANDLER(maclhws, 0x0C, 0x0F),
9748GEN_MAC_HANDLER(maclhwso, 0x0C, 0x1F),
9749GEN_MAC_HANDLER(maclhwu, 0x0C, 0x0C),
9750GEN_MAC_HANDLER(maclhwuo, 0x0C, 0x1C),
9751GEN_MAC_HANDLER(maclhwsu, 0x0C, 0x0E),
9752GEN_MAC_HANDLER(maclhwsuo, 0x0C, 0x1E),
9753GEN_MAC_HANDLER(nmacchw, 0x0E, 0x05),
9754GEN_MAC_HANDLER(nmacchwo, 0x0E, 0x15),
9755GEN_MAC_HANDLER(nmacchws, 0x0E, 0x07),
9756GEN_MAC_HANDLER(nmacchwso, 0x0E, 0x17),
9757GEN_MAC_HANDLER(nmachhw, 0x0E, 0x01),
9758GEN_MAC_HANDLER(nmachhwo, 0x0E, 0x11),
9759GEN_MAC_HANDLER(nmachhws, 0x0E, 0x03),
9760GEN_MAC_HANDLER(nmachhwso, 0x0E, 0x13),
9761GEN_MAC_HANDLER(nmaclhw, 0x0E, 0x0D),
9762GEN_MAC_HANDLER(nmaclhwo, 0x0E, 0x1D),
9763GEN_MAC_HANDLER(nmaclhws, 0x0E, 0x0F),
9764GEN_MAC_HANDLER(nmaclhwso, 0x0E, 0x1F),
9765GEN_MAC_HANDLER(mulchw, 0x08, 0x05),
9766GEN_MAC_HANDLER(mulchwu, 0x08, 0x04),
9767GEN_MAC_HANDLER(mulhhw, 0x08, 0x01),
9768GEN_MAC_HANDLER(mulhhwu, 0x08, 0x00),
9769GEN_MAC_HANDLER(mullhw, 0x08, 0x0D),
9770GEN_MAC_HANDLER(mullhwu, 0x08, 0x0C),
9771
9772#undef GEN_VR_LDX
9773#undef GEN_VR_STX
9774#undef GEN_VR_LVE
9775#undef GEN_VR_STVE
9776#define GEN_VR_LDX(name, opc2, opc3) \
9777GEN_HANDLER(name, 0x1F, opc2, opc3, 0x00000001, PPC_ALTIVEC)
9778#define GEN_VR_STX(name, opc2, opc3) \
9779GEN_HANDLER(st##name, 0x1F, opc2, opc3, 0x00000001, PPC_ALTIVEC)
9780#define GEN_VR_LVE(name, opc2, opc3) \
9781 GEN_HANDLER(lve##name, 0x1F, opc2, opc3, 0x00000001, PPC_ALTIVEC)
9782#define GEN_VR_STVE(name, opc2, opc3) \
9783 GEN_HANDLER(stve##name, 0x1F, opc2, opc3, 0x00000001, PPC_ALTIVEC)
9784GEN_VR_LDX(lvx, 0x07, 0x03),
9785GEN_VR_LDX(lvxl, 0x07, 0x0B),
9786GEN_VR_LVE(bx, 0x07, 0x00),
9787GEN_VR_LVE(hx, 0x07, 0x01),
9788GEN_VR_LVE(wx, 0x07, 0x02),
9789GEN_VR_STX(svx, 0x07, 0x07),
9790GEN_VR_STX(svxl, 0x07, 0x0F),
9791GEN_VR_STVE(bx, 0x07, 0x04),
9792GEN_VR_STVE(hx, 0x07, 0x05),
9793GEN_VR_STVE(wx, 0x07, 0x06),
9794
9795#undef GEN_VX_LOGICAL
9796#define GEN_VX_LOGICAL(name, tcg_op, opc2, opc3) \
9797GEN_HANDLER(name, 0x04, opc2, opc3, 0x00000000, PPC_ALTIVEC)
9798GEN_VX_LOGICAL(vand, tcg_gen_and_i64, 2, 16),
9799GEN_VX_LOGICAL(vandc, tcg_gen_andc_i64, 2, 17),
9800GEN_VX_LOGICAL(vor, tcg_gen_or_i64, 2, 18),
9801GEN_VX_LOGICAL(vxor, tcg_gen_xor_i64, 2, 19),
9802GEN_VX_LOGICAL(vnor, tcg_gen_nor_i64, 2, 20),
9803
9804#undef GEN_VXFORM
9805#define GEN_VXFORM(name, opc2, opc3) \
9806GEN_HANDLER(name, 0x04, opc2, opc3, 0x00000000, PPC_ALTIVEC)
9807GEN_VXFORM(vaddubm, 0, 0),
9808GEN_VXFORM(vadduhm, 0, 1),
9809GEN_VXFORM(vadduwm, 0, 2),
9810GEN_VXFORM(vsububm, 0, 16),
9811GEN_VXFORM(vsubuhm, 0, 17),
9812GEN_VXFORM(vsubuwm, 0, 18),
9813GEN_VXFORM(vmaxub, 1, 0),
9814GEN_VXFORM(vmaxuh, 1, 1),
9815GEN_VXFORM(vmaxuw, 1, 2),
9816GEN_VXFORM(vmaxsb, 1, 4),
9817GEN_VXFORM(vmaxsh, 1, 5),
9818GEN_VXFORM(vmaxsw, 1, 6),
9819GEN_VXFORM(vminub, 1, 8),
9820GEN_VXFORM(vminuh, 1, 9),
9821GEN_VXFORM(vminuw, 1, 10),
9822GEN_VXFORM(vminsb, 1, 12),
9823GEN_VXFORM(vminsh, 1, 13),
9824GEN_VXFORM(vminsw, 1, 14),
9825GEN_VXFORM(vavgub, 1, 16),
9826GEN_VXFORM(vavguh, 1, 17),
9827GEN_VXFORM(vavguw, 1, 18),
9828GEN_VXFORM(vavgsb, 1, 20),
9829GEN_VXFORM(vavgsh, 1, 21),
9830GEN_VXFORM(vavgsw, 1, 22),
9831GEN_VXFORM(vmrghb, 6, 0),
9832GEN_VXFORM(vmrghh, 6, 1),
9833GEN_VXFORM(vmrghw, 6, 2),
9834GEN_VXFORM(vmrglb, 6, 4),
9835GEN_VXFORM(vmrglh, 6, 5),
9836GEN_VXFORM(vmrglw, 6, 6),
9837GEN_VXFORM(vmuloub, 4, 0),
9838GEN_VXFORM(vmulouh, 4, 1),
9839GEN_VXFORM(vmulosb, 4, 4),
9840GEN_VXFORM(vmulosh, 4, 5),
9841GEN_VXFORM(vmuleub, 4, 8),
9842GEN_VXFORM(vmuleuh, 4, 9),
9843GEN_VXFORM(vmulesb, 4, 12),
9844GEN_VXFORM(vmulesh, 4, 13),
9845GEN_VXFORM(vslb, 2, 4),
9846GEN_VXFORM(vslh, 2, 5),
9847GEN_VXFORM(vslw, 2, 6),
9848GEN_VXFORM(vsrb, 2, 8),
9849GEN_VXFORM(vsrh, 2, 9),
9850GEN_VXFORM(vsrw, 2, 10),
9851GEN_VXFORM(vsrab, 2, 12),
9852GEN_VXFORM(vsrah, 2, 13),
9853GEN_VXFORM(vsraw, 2, 14),
9854GEN_VXFORM(vslo, 6, 16),
9855GEN_VXFORM(vsro, 6, 17),
9856GEN_VXFORM(vaddcuw, 0, 6),
9857GEN_VXFORM(vsubcuw, 0, 22),
9858GEN_VXFORM(vaddubs, 0, 8),
9859GEN_VXFORM(vadduhs, 0, 9),
9860GEN_VXFORM(vadduws, 0, 10),
9861GEN_VXFORM(vaddsbs, 0, 12),
9862GEN_VXFORM(vaddshs, 0, 13),
9863GEN_VXFORM(vaddsws, 0, 14),
9864GEN_VXFORM(vsububs, 0, 24),
9865GEN_VXFORM(vsubuhs, 0, 25),
9866GEN_VXFORM(vsubuws, 0, 26),
9867GEN_VXFORM(vsubsbs, 0, 28),
9868GEN_VXFORM(vsubshs, 0, 29),
9869GEN_VXFORM(vsubsws, 0, 30),
9870GEN_VXFORM(vrlb, 2, 0),
9871GEN_VXFORM(vrlh, 2, 1),
9872GEN_VXFORM(vrlw, 2, 2),
9873GEN_VXFORM(vsl, 2, 7),
9874GEN_VXFORM(vsr, 2, 11),
9875GEN_VXFORM(vpkuhum, 7, 0),
9876GEN_VXFORM(vpkuwum, 7, 1),
9877GEN_VXFORM(vpkuhus, 7, 2),
9878GEN_VXFORM(vpkuwus, 7, 3),
9879GEN_VXFORM(vpkshus, 7, 4),
9880GEN_VXFORM(vpkswus, 7, 5),
9881GEN_VXFORM(vpkshss, 7, 6),
9882GEN_VXFORM(vpkswss, 7, 7),
9883GEN_VXFORM(vpkpx, 7, 12),
9884GEN_VXFORM(vsum4ubs, 4, 24),
9885GEN_VXFORM(vsum4sbs, 4, 28),
9886GEN_VXFORM(vsum4shs, 4, 25),
9887GEN_VXFORM(vsum2sws, 4, 26),
9888GEN_VXFORM(vsumsws, 4, 30),
9889GEN_VXFORM(vaddfp, 5, 0),
9890GEN_VXFORM(vsubfp, 5, 1),
9891GEN_VXFORM(vmaxfp, 5, 16),
9892GEN_VXFORM(vminfp, 5, 17),
9893
9894#undef GEN_VXRFORM1
9895#undef GEN_VXRFORM
9896#define GEN_VXRFORM1(opname, name, str, opc2, opc3) \
9897 GEN_HANDLER2(name, str, 0x4, opc2, opc3, 0x00000000, PPC_ALTIVEC),
9898#define GEN_VXRFORM(name, opc2, opc3) \
9899 GEN_VXRFORM1(name, name, #name, opc2, opc3) \
9900 GEN_VXRFORM1(name##_dot, name##_, #name ".", opc2, (opc3 | (0x1 << 4)))
9901GEN_VXRFORM(vcmpequb, 3, 0)
9902GEN_VXRFORM(vcmpequh, 3, 1)
9903GEN_VXRFORM(vcmpequw, 3, 2)
9904GEN_VXRFORM(vcmpgtsb, 3, 12)
9905GEN_VXRFORM(vcmpgtsh, 3, 13)
9906GEN_VXRFORM(vcmpgtsw, 3, 14)
9907GEN_VXRFORM(vcmpgtub, 3, 8)
9908GEN_VXRFORM(vcmpgtuh, 3, 9)
9909GEN_VXRFORM(vcmpgtuw, 3, 10)
9910GEN_VXRFORM(vcmpeqfp, 3, 3)
9911GEN_VXRFORM(vcmpgefp, 3, 7)
9912GEN_VXRFORM(vcmpgtfp, 3, 11)
9913GEN_VXRFORM(vcmpbfp, 3, 15)
9914
9915#undef GEN_VXFORM_SIMM
9916#define GEN_VXFORM_SIMM(name, opc2, opc3) \
9917 GEN_HANDLER(name, 0x04, opc2, opc3, 0x00000000, PPC_ALTIVEC)
9918GEN_VXFORM_SIMM(vspltisb, 6, 12),
9919GEN_VXFORM_SIMM(vspltish, 6, 13),
9920GEN_VXFORM_SIMM(vspltisw, 6, 14),
9921
9922#undef GEN_VXFORM_NOA
9923#define GEN_VXFORM_NOA(name, opc2, opc3) \
9924 GEN_HANDLER(name, 0x04, opc2, opc3, 0x001f0000, PPC_ALTIVEC)
9925GEN_VXFORM_NOA(vupkhsb, 7, 8),
9926GEN_VXFORM_NOA(vupkhsh, 7, 9),
9927GEN_VXFORM_NOA(vupklsb, 7, 10),
9928GEN_VXFORM_NOA(vupklsh, 7, 11),
9929GEN_VXFORM_NOA(vupkhpx, 7, 13),
9930GEN_VXFORM_NOA(vupklpx, 7, 15),
9931GEN_VXFORM_NOA(vrefp, 5, 4),
9932GEN_VXFORM_NOA(vrsqrtefp, 5, 5),
0bffbc6c 9933GEN_VXFORM_NOA(vexptefp, 5, 6),
5c55ff99
BS
9934GEN_VXFORM_NOA(vlogefp, 5, 7),
9935GEN_VXFORM_NOA(vrfim, 5, 8),
9936GEN_VXFORM_NOA(vrfin, 5, 9),
9937GEN_VXFORM_NOA(vrfip, 5, 10),
9938GEN_VXFORM_NOA(vrfiz, 5, 11),
9939
9940#undef GEN_VXFORM_UIMM
9941#define GEN_VXFORM_UIMM(name, opc2, opc3) \
9942 GEN_HANDLER(name, 0x04, opc2, opc3, 0x00000000, PPC_ALTIVEC)
9943GEN_VXFORM_UIMM(vspltb, 6, 8),
9944GEN_VXFORM_UIMM(vsplth, 6, 9),
9945GEN_VXFORM_UIMM(vspltw, 6, 10),
9946GEN_VXFORM_UIMM(vcfux, 5, 12),
9947GEN_VXFORM_UIMM(vcfsx, 5, 13),
9948GEN_VXFORM_UIMM(vctuxs, 5, 14),
9949GEN_VXFORM_UIMM(vctsxs, 5, 15),
9950
9951#undef GEN_VAFORM_PAIRED
9952#define GEN_VAFORM_PAIRED(name0, name1, opc2) \
9953 GEN_HANDLER(name0##_##name1, 0x04, opc2, 0xFF, 0x00000000, PPC_ALTIVEC)
9954GEN_VAFORM_PAIRED(vmhaddshs, vmhraddshs, 16),
9955GEN_VAFORM_PAIRED(vmsumubm, vmsummbm, 18),
9956GEN_VAFORM_PAIRED(vmsumuhm, vmsumuhs, 19),
9957GEN_VAFORM_PAIRED(vmsumshm, vmsumshs, 20),
9958GEN_VAFORM_PAIRED(vsel, vperm, 21),
9959GEN_VAFORM_PAIRED(vmaddfp, vnmsubfp, 23),
9960
fa1832d7 9961GEN_HANDLER_E(lxsdx, 0x1F, 0x0C, 0x12, 0, PPC_NONE, PPC2_VSX),
304af367 9962GEN_HANDLER_E(lxvd2x, 0x1F, 0x0C, 0x1A, 0, PPC_NONE, PPC2_VSX),
ca03b467 9963GEN_HANDLER_E(lxvdsx, 0x1F, 0x0C, 0x0A, 0, PPC_NONE, PPC2_VSX),
897e61d1 9964GEN_HANDLER_E(lxvw4x, 0x1F, 0x0C, 0x18, 0, PPC_NONE, PPC2_VSX),
304af367 9965
9231ba9e 9966GEN_HANDLER_E(stxsdx, 0x1F, 0xC, 0x16, 0, PPC_NONE, PPC2_VSX),
fbed2478 9967GEN_HANDLER_E(stxvd2x, 0x1F, 0xC, 0x1E, 0, PPC_NONE, PPC2_VSX),
86e61ce3 9968GEN_HANDLER_E(stxvw4x, 0x1F, 0xC, 0x1C, 0, PPC_NONE, PPC2_VSX),
fbed2478 9969
df020ce0
TM
9970#undef GEN_XX2FORM
9971#define GEN_XX2FORM(name, opc2, opc3, fl2) \
9972GEN_HANDLER2_E(name, #name, 0x3C, opc2 | 0, opc3, 0, PPC_NONE, fl2), \
9973GEN_HANDLER2_E(name, #name, 0x3C, opc2 | 1, opc3, 0, PPC_NONE, fl2)
9974
9975#undef GEN_XX3FORM
9976#define GEN_XX3FORM(name, opc2, opc3, fl2) \
9977GEN_HANDLER2_E(name, #name, 0x3C, opc2 | 0, opc3, 0, PPC_NONE, fl2), \
9978GEN_HANDLER2_E(name, #name, 0x3C, opc2 | 1, opc3, 0, PPC_NONE, fl2), \
9979GEN_HANDLER2_E(name, #name, 0x3C, opc2 | 2, opc3, 0, PPC_NONE, fl2), \
9980GEN_HANDLER2_E(name, #name, 0x3C, opc2 | 3, opc3, 0, PPC_NONE, fl2)
9981
cd73f2c9
TM
9982#undef GEN_XX3FORM_DM
9983#define GEN_XX3FORM_DM(name, opc2, opc3) \
9984GEN_HANDLER2_E(name, #name, 0x3C, opc2|0x00, opc3|0x00, 0, PPC_NONE, PPC2_VSX),\
9985GEN_HANDLER2_E(name, #name, 0x3C, opc2|0x01, opc3|0x00, 0, PPC_NONE, PPC2_VSX),\
9986GEN_HANDLER2_E(name, #name, 0x3C, opc2|0x02, opc3|0x00, 0, PPC_NONE, PPC2_VSX),\
9987GEN_HANDLER2_E(name, #name, 0x3C, opc2|0x03, opc3|0x00, 0, PPC_NONE, PPC2_VSX),\
9988GEN_HANDLER2_E(name, #name, 0x3C, opc2|0x00, opc3|0x04, 0, PPC_NONE, PPC2_VSX),\
9989GEN_HANDLER2_E(name, #name, 0x3C, opc2|0x01, opc3|0x04, 0, PPC_NONE, PPC2_VSX),\
9990GEN_HANDLER2_E(name, #name, 0x3C, opc2|0x02, opc3|0x04, 0, PPC_NONE, PPC2_VSX),\
9991GEN_HANDLER2_E(name, #name, 0x3C, opc2|0x03, opc3|0x04, 0, PPC_NONE, PPC2_VSX),\
9992GEN_HANDLER2_E(name, #name, 0x3C, opc2|0x00, opc3|0x08, 0, PPC_NONE, PPC2_VSX),\
9993GEN_HANDLER2_E(name, #name, 0x3C, opc2|0x01, opc3|0x08, 0, PPC_NONE, PPC2_VSX),\
9994GEN_HANDLER2_E(name, #name, 0x3C, opc2|0x02, opc3|0x08, 0, PPC_NONE, PPC2_VSX),\
9995GEN_HANDLER2_E(name, #name, 0x3C, opc2|0x03, opc3|0x08, 0, PPC_NONE, PPC2_VSX),\
9996GEN_HANDLER2_E(name, #name, 0x3C, opc2|0x00, opc3|0x0C, 0, PPC_NONE, PPC2_VSX),\
9997GEN_HANDLER2_E(name, #name, 0x3C, opc2|0x01, opc3|0x0C, 0, PPC_NONE, PPC2_VSX),\
9998GEN_HANDLER2_E(name, #name, 0x3C, opc2|0x02, opc3|0x0C, 0, PPC_NONE, PPC2_VSX),\
9999GEN_HANDLER2_E(name, #name, 0x3C, opc2|0x03, opc3|0x0C, 0, PPC_NONE, PPC2_VSX)
10000
df020ce0
TM
10001GEN_XX2FORM(xsabsdp, 0x12, 0x15, PPC2_VSX),
10002GEN_XX2FORM(xsnabsdp, 0x12, 0x16, PPC2_VSX),
10003GEN_XX2FORM(xsnegdp, 0x12, 0x17, PPC2_VSX),
10004GEN_XX3FORM(xscpsgndp, 0x00, 0x16, PPC2_VSX),
10005
be574920
TM
10006GEN_XX2FORM(xvabsdp, 0x12, 0x1D, PPC2_VSX),
10007GEN_XX2FORM(xvnabsdp, 0x12, 0x1E, PPC2_VSX),
10008GEN_XX2FORM(xvnegdp, 0x12, 0x1F, PPC2_VSX),
10009GEN_XX3FORM(xvcpsgndp, 0x00, 0x1E, PPC2_VSX),
10010GEN_XX2FORM(xvabssp, 0x12, 0x19, PPC2_VSX),
10011GEN_XX2FORM(xvnabssp, 0x12, 0x1A, PPC2_VSX),
10012GEN_XX2FORM(xvnegsp, 0x12, 0x1B, PPC2_VSX),
10013GEN_XX3FORM(xvcpsgnsp, 0x00, 0x1A, PPC2_VSX),
79ca8a6a 10014
ee6e02c0
TM
10015GEN_XX3FORM(xsadddp, 0x00, 0x04, PPC2_VSX),
10016GEN_XX3FORM(xssubdp, 0x00, 0x05, PPC2_VSX),
5e591d88 10017GEN_XX3FORM(xsmuldp, 0x00, 0x06, PPC2_VSX),
4b98eeef 10018GEN_XX3FORM(xsdivdp, 0x00, 0x07, PPC2_VSX),
2009227f 10019GEN_XX2FORM(xsredp, 0x14, 0x05, PPC2_VSX),
d32404fe 10020GEN_XX2FORM(xssqrtdp, 0x16, 0x04, PPC2_VSX),
d3f9df8f 10021GEN_XX2FORM(xsrsqrtedp, 0x14, 0x04, PPC2_VSX),
bc80838f 10022GEN_XX3FORM(xstdivdp, 0x14, 0x07, PPC2_VSX),
ee6e02c0
TM
10023
10024GEN_XX3FORM(xvadddp, 0x00, 0x0C, PPC2_VSX),
10025GEN_XX3FORM(xvsubdp, 0x00, 0x0D, PPC2_VSX),
5e591d88 10026GEN_XX3FORM(xvmuldp, 0x00, 0x0E, PPC2_VSX),
4b98eeef 10027GEN_XX3FORM(xvdivdp, 0x00, 0x0F, PPC2_VSX),
2009227f 10028GEN_XX2FORM(xvredp, 0x14, 0x0D, PPC2_VSX),
d32404fe 10029GEN_XX2FORM(xvsqrtdp, 0x16, 0x0C, PPC2_VSX),
d3f9df8f 10030GEN_XX2FORM(xvrsqrtedp, 0x14, 0x0C, PPC2_VSX),
bc80838f 10031GEN_XX3FORM(xvtdivdp, 0x14, 0x0F, PPC2_VSX),
ee6e02c0
TM
10032
10033GEN_XX3FORM(xvaddsp, 0x00, 0x08, PPC2_VSX),
10034GEN_XX3FORM(xvsubsp, 0x00, 0x09, PPC2_VSX),
5e591d88 10035GEN_XX3FORM(xvmulsp, 0x00, 0x0A, PPC2_VSX),
4b98eeef 10036GEN_XX3FORM(xvdivsp, 0x00, 0x0B, PPC2_VSX),
2009227f 10037GEN_XX2FORM(xvresp, 0x14, 0x09, PPC2_VSX),
d32404fe 10038GEN_XX2FORM(xvsqrtsp, 0x16, 0x08, PPC2_VSX),
d3f9df8f 10039GEN_XX2FORM(xvrsqrtesp, 0x14, 0x08, PPC2_VSX),
bc80838f 10040GEN_XX3FORM(xvtdivsp, 0x14, 0x0B, PPC2_VSX),
ee6e02c0 10041
79ca8a6a
TM
10042#undef VSX_LOGICAL
10043#define VSX_LOGICAL(name, opc2, opc3, fl2) \
10044GEN_XX3FORM(name, opc2, opc3, fl2)
10045
10046VSX_LOGICAL(xxland, 0x8, 0x10, PPC2_VSX),
10047VSX_LOGICAL(xxlandc, 0x8, 0x11, PPC2_VSX),
10048VSX_LOGICAL(xxlor, 0x8, 0x12, PPC2_VSX),
10049VSX_LOGICAL(xxlxor, 0x8, 0x13, PPC2_VSX),
10050VSX_LOGICAL(xxlnor, 0x8, 0x14, PPC2_VSX),
ce577d2e
TM
10051GEN_XX3FORM(xxmrghw, 0x08, 0x02, PPC2_VSX),
10052GEN_XX3FORM(xxmrglw, 0x08, 0x06, PPC2_VSX),
76c15fe0 10053GEN_XX2FORM(xxspltw, 0x08, 0x0A, PPC2_VSX),
acc42968 10054GEN_XX3FORM_DM(xxsldwi, 0x08, 0x00),
79ca8a6a 10055
551e3ef7
TM
10056#define GEN_XXSEL_ROW(opc3) \
10057GEN_HANDLER2_E(xxsel, "xxsel", 0x3C, 0x18, opc3, 0, PPC_NONE, PPC2_VSX), \
10058GEN_HANDLER2_E(xxsel, "xxsel", 0x3C, 0x19, opc3, 0, PPC_NONE, PPC2_VSX), \
10059GEN_HANDLER2_E(xxsel, "xxsel", 0x3C, 0x1A, opc3, 0, PPC_NONE, PPC2_VSX), \
10060GEN_HANDLER2_E(xxsel, "xxsel", 0x3C, 0x1B, opc3, 0, PPC_NONE, PPC2_VSX), \
10061GEN_HANDLER2_E(xxsel, "xxsel", 0x3C, 0x1C, opc3, 0, PPC_NONE, PPC2_VSX), \
10062GEN_HANDLER2_E(xxsel, "xxsel", 0x3C, 0x1D, opc3, 0, PPC_NONE, PPC2_VSX), \
10063GEN_HANDLER2_E(xxsel, "xxsel", 0x3C, 0x1E, opc3, 0, PPC_NONE, PPC2_VSX), \
10064GEN_HANDLER2_E(xxsel, "xxsel", 0x3C, 0x1F, opc3, 0, PPC_NONE, PPC2_VSX), \
10065
10066GEN_XXSEL_ROW(0x00)
10067GEN_XXSEL_ROW(0x01)
10068GEN_XXSEL_ROW(0x02)
10069GEN_XXSEL_ROW(0x03)
10070GEN_XXSEL_ROW(0x04)
10071GEN_XXSEL_ROW(0x05)
10072GEN_XXSEL_ROW(0x06)
10073GEN_XXSEL_ROW(0x07)
10074GEN_XXSEL_ROW(0x08)
10075GEN_XXSEL_ROW(0x09)
10076GEN_XXSEL_ROW(0x0A)
10077GEN_XXSEL_ROW(0x0B)
10078GEN_XXSEL_ROW(0x0C)
10079GEN_XXSEL_ROW(0x0D)
10080GEN_XXSEL_ROW(0x0E)
10081GEN_XXSEL_ROW(0x0F)
10082GEN_XXSEL_ROW(0x10)
10083GEN_XXSEL_ROW(0x11)
10084GEN_XXSEL_ROW(0x12)
10085GEN_XXSEL_ROW(0x13)
10086GEN_XXSEL_ROW(0x14)
10087GEN_XXSEL_ROW(0x15)
10088GEN_XXSEL_ROW(0x16)
10089GEN_XXSEL_ROW(0x17)
10090GEN_XXSEL_ROW(0x18)
10091GEN_XXSEL_ROW(0x19)
10092GEN_XXSEL_ROW(0x1A)
10093GEN_XXSEL_ROW(0x1B)
10094GEN_XXSEL_ROW(0x1C)
10095GEN_XXSEL_ROW(0x1D)
10096GEN_XXSEL_ROW(0x1E)
10097GEN_XXSEL_ROW(0x1F)
10098
cd73f2c9
TM
10099GEN_XX3FORM_DM(xxpermdi, 0x08, 0x01),
10100
5c55ff99 10101#undef GEN_SPE
70560da7
FC
10102#define GEN_SPE(name0, name1, opc2, opc3, inval0, inval1, type) \
10103 GEN_OPCODE_DUAL(name0##_##name1, 0x04, opc2, opc3, inval0, inval1, type, PPC_NONE)
10104GEN_SPE(evaddw, speundef, 0x00, 0x08, 0x00000000, 0xFFFFFFFF, PPC_SPE),
10105GEN_SPE(evaddiw, speundef, 0x01, 0x08, 0x00000000, 0xFFFFFFFF, PPC_SPE),
10106GEN_SPE(evsubfw, speundef, 0x02, 0x08, 0x00000000, 0xFFFFFFFF, PPC_SPE),
10107GEN_SPE(evsubifw, speundef, 0x03, 0x08, 0x00000000, 0xFFFFFFFF, PPC_SPE),
10108GEN_SPE(evabs, evneg, 0x04, 0x08, 0x0000F800, 0x0000F800, PPC_SPE),
10109GEN_SPE(evextsb, evextsh, 0x05, 0x08, 0x0000F800, 0x0000F800, PPC_SPE),
10110GEN_SPE(evrndw, evcntlzw, 0x06, 0x08, 0x0000F800, 0x0000F800, PPC_SPE),
10111GEN_SPE(evcntlsw, brinc, 0x07, 0x08, 0x0000F800, 0x00000000, PPC_SPE),
10112GEN_SPE(evmra, speundef, 0x02, 0x13, 0x0000F800, 0xFFFFFFFF, PPC_SPE),
10113GEN_SPE(speundef, evand, 0x08, 0x08, 0xFFFFFFFF, 0x00000000, PPC_SPE),
10114GEN_SPE(evandc, speundef, 0x09, 0x08, 0x00000000, 0xFFFFFFFF, PPC_SPE),
10115GEN_SPE(evxor, evor, 0x0B, 0x08, 0x00000000, 0x00000000, PPC_SPE),
10116GEN_SPE(evnor, eveqv, 0x0C, 0x08, 0x00000000, 0x00000000, PPC_SPE),
10117GEN_SPE(evmwumi, evmwsmi, 0x0C, 0x11, 0x00000000, 0x00000000, PPC_SPE),
10118GEN_SPE(evmwumia, evmwsmia, 0x1C, 0x11, 0x00000000, 0x00000000, PPC_SPE),
10119GEN_SPE(evmwumiaa, evmwsmiaa, 0x0C, 0x15, 0x00000000, 0x00000000, PPC_SPE),
10120GEN_SPE(speundef, evorc, 0x0D, 0x08, 0xFFFFFFFF, 0x00000000, PPC_SPE),
10121GEN_SPE(evnand, speundef, 0x0F, 0x08, 0x00000000, 0xFFFFFFFF, PPC_SPE),
10122GEN_SPE(evsrwu, evsrws, 0x10, 0x08, 0x00000000, 0x00000000, PPC_SPE),
10123GEN_SPE(evsrwiu, evsrwis, 0x11, 0x08, 0x00000000, 0x00000000, PPC_SPE),
10124GEN_SPE(evslw, speundef, 0x12, 0x08, 0x00000000, 0xFFFFFFFF, PPC_SPE),
10125GEN_SPE(evslwi, speundef, 0x13, 0x08, 0x00000000, 0xFFFFFFFF, PPC_SPE),
10126GEN_SPE(evrlw, evsplati, 0x14, 0x08, 0x00000000, 0x0000F800, PPC_SPE),
10127GEN_SPE(evrlwi, evsplatfi, 0x15, 0x08, 0x00000000, 0x0000F800, PPC_SPE),
10128GEN_SPE(evmergehi, evmergelo, 0x16, 0x08, 0x00000000, 0x00000000, PPC_SPE),
10129GEN_SPE(evmergehilo, evmergelohi, 0x17, 0x08, 0x00000000, 0x00000000, PPC_SPE),
10130GEN_SPE(evcmpgtu, evcmpgts, 0x18, 0x08, 0x00600000, 0x00600000, PPC_SPE),
10131GEN_SPE(evcmpltu, evcmplts, 0x19, 0x08, 0x00600000, 0x00600000, PPC_SPE),
10132GEN_SPE(evcmpeq, speundef, 0x1A, 0x08, 0x00600000, 0xFFFFFFFF, PPC_SPE),
10133
10134GEN_SPE(evfsadd, evfssub, 0x00, 0x0A, 0x00000000, 0x00000000, PPC_SPE_SINGLE),
10135GEN_SPE(evfsabs, evfsnabs, 0x02, 0x0A, 0x0000F800, 0x0000F800, PPC_SPE_SINGLE),
10136GEN_SPE(evfsneg, speundef, 0x03, 0x0A, 0x0000F800, 0xFFFFFFFF, PPC_SPE_SINGLE),
10137GEN_SPE(evfsmul, evfsdiv, 0x04, 0x0A, 0x00000000, 0x00000000, PPC_SPE_SINGLE),
10138GEN_SPE(evfscmpgt, evfscmplt, 0x06, 0x0A, 0x00600000, 0x00600000, PPC_SPE_SINGLE),
10139GEN_SPE(evfscmpeq, speundef, 0x07, 0x0A, 0x00600000, 0xFFFFFFFF, PPC_SPE_SINGLE),
10140GEN_SPE(evfscfui, evfscfsi, 0x08, 0x0A, 0x00180000, 0x00180000, PPC_SPE_SINGLE),
10141GEN_SPE(evfscfuf, evfscfsf, 0x09, 0x0A, 0x00180000, 0x00180000, PPC_SPE_SINGLE),
10142GEN_SPE(evfsctui, evfsctsi, 0x0A, 0x0A, 0x00180000, 0x00180000, PPC_SPE_SINGLE),
10143GEN_SPE(evfsctuf, evfsctsf, 0x0B, 0x0A, 0x00180000, 0x00180000, PPC_SPE_SINGLE),
10144GEN_SPE(evfsctuiz, speundef, 0x0C, 0x0A, 0x00180000, 0xFFFFFFFF, PPC_SPE_SINGLE),
10145GEN_SPE(evfsctsiz, speundef, 0x0D, 0x0A, 0x00180000, 0xFFFFFFFF, PPC_SPE_SINGLE),
10146GEN_SPE(evfststgt, evfststlt, 0x0E, 0x0A, 0x00600000, 0x00600000, PPC_SPE_SINGLE),
10147GEN_SPE(evfststeq, speundef, 0x0F, 0x0A, 0x00600000, 0xFFFFFFFF, PPC_SPE_SINGLE),
10148
10149GEN_SPE(efsadd, efssub, 0x00, 0x0B, 0x00000000, 0x00000000, PPC_SPE_SINGLE),
10150GEN_SPE(efsabs, efsnabs, 0x02, 0x0B, 0x0000F800, 0x0000F800, PPC_SPE_SINGLE),
10151GEN_SPE(efsneg, speundef, 0x03, 0x0B, 0x0000F800, 0xFFFFFFFF, PPC_SPE_SINGLE),
10152GEN_SPE(efsmul, efsdiv, 0x04, 0x0B, 0x00000000, 0x00000000, PPC_SPE_SINGLE),
10153GEN_SPE(efscmpgt, efscmplt, 0x06, 0x0B, 0x00600000, 0x00600000, PPC_SPE_SINGLE),
10154GEN_SPE(efscmpeq, efscfd, 0x07, 0x0B, 0x00600000, 0x00180000, PPC_SPE_SINGLE),
10155GEN_SPE(efscfui, efscfsi, 0x08, 0x0B, 0x00180000, 0x00180000, PPC_SPE_SINGLE),
10156GEN_SPE(efscfuf, efscfsf, 0x09, 0x0B, 0x00180000, 0x00180000, PPC_SPE_SINGLE),
10157GEN_SPE(efsctui, efsctsi, 0x0A, 0x0B, 0x00180000, 0x00180000, PPC_SPE_SINGLE),
10158GEN_SPE(efsctuf, efsctsf, 0x0B, 0x0B, 0x00180000, 0x00180000, PPC_SPE_SINGLE),
10159GEN_SPE(efsctuiz, speundef, 0x0C, 0x0B, 0x00180000, 0xFFFFFFFF, PPC_SPE_SINGLE),
10160GEN_SPE(efsctsiz, speundef, 0x0D, 0x0B, 0x00180000, 0xFFFFFFFF, PPC_SPE_SINGLE),
10161GEN_SPE(efststgt, efststlt, 0x0E, 0x0B, 0x00600000, 0x00600000, PPC_SPE_SINGLE),
10162GEN_SPE(efststeq, speundef, 0x0F, 0x0B, 0x00600000, 0xFFFFFFFF, PPC_SPE_SINGLE),
10163
10164GEN_SPE(efdadd, efdsub, 0x10, 0x0B, 0x00000000, 0x00000000, PPC_SPE_DOUBLE),
10165GEN_SPE(efdcfuid, efdcfsid, 0x11, 0x0B, 0x00180000, 0x00180000, PPC_SPE_DOUBLE),
10166GEN_SPE(efdabs, efdnabs, 0x12, 0x0B, 0x0000F800, 0x0000F800, PPC_SPE_DOUBLE),
10167GEN_SPE(efdneg, speundef, 0x13, 0x0B, 0x0000F800, 0xFFFFFFFF, PPC_SPE_DOUBLE),
10168GEN_SPE(efdmul, efddiv, 0x14, 0x0B, 0x00000000, 0x00000000, PPC_SPE_DOUBLE),
10169GEN_SPE(efdctuidz, efdctsidz, 0x15, 0x0B, 0x00180000, 0x00180000, PPC_SPE_DOUBLE),
10170GEN_SPE(efdcmpgt, efdcmplt, 0x16, 0x0B, 0x00600000, 0x00600000, PPC_SPE_DOUBLE),
10171GEN_SPE(efdcmpeq, efdcfs, 0x17, 0x0B, 0x00600000, 0x00180000, PPC_SPE_DOUBLE),
10172GEN_SPE(efdcfui, efdcfsi, 0x18, 0x0B, 0x00180000, 0x00180000, PPC_SPE_DOUBLE),
10173GEN_SPE(efdcfuf, efdcfsf, 0x19, 0x0B, 0x00180000, 0x00180000, PPC_SPE_DOUBLE),
10174GEN_SPE(efdctui, efdctsi, 0x1A, 0x0B, 0x00180000, 0x00180000, PPC_SPE_DOUBLE),
10175GEN_SPE(efdctuf, efdctsf, 0x1B, 0x0B, 0x00180000, 0x00180000, PPC_SPE_DOUBLE),
10176GEN_SPE(efdctuiz, speundef, 0x1C, 0x0B, 0x00180000, 0xFFFFFFFF, PPC_SPE_DOUBLE),
10177GEN_SPE(efdctsiz, speundef, 0x1D, 0x0B, 0x00180000, 0xFFFFFFFF, PPC_SPE_DOUBLE),
10178GEN_SPE(efdtstgt, efdtstlt, 0x1E, 0x0B, 0x00600000, 0x00600000, PPC_SPE_DOUBLE),
10179GEN_SPE(efdtsteq, speundef, 0x1F, 0x0B, 0x00600000, 0xFFFFFFFF, PPC_SPE_DOUBLE),
5c55ff99
BS
10180
10181#undef GEN_SPEOP_LDST
10182#define GEN_SPEOP_LDST(name, opc2, sh) \
10183GEN_HANDLER(name, 0x04, opc2, 0x0C, 0x00000000, PPC_SPE)
10184GEN_SPEOP_LDST(evldd, 0x00, 3),
10185GEN_SPEOP_LDST(evldw, 0x01, 3),
10186GEN_SPEOP_LDST(evldh, 0x02, 3),
10187GEN_SPEOP_LDST(evlhhesplat, 0x04, 1),
10188GEN_SPEOP_LDST(evlhhousplat, 0x06, 1),
10189GEN_SPEOP_LDST(evlhhossplat, 0x07, 1),
10190GEN_SPEOP_LDST(evlwhe, 0x08, 2),
10191GEN_SPEOP_LDST(evlwhou, 0x0A, 2),
10192GEN_SPEOP_LDST(evlwhos, 0x0B, 2),
10193GEN_SPEOP_LDST(evlwwsplat, 0x0C, 2),
10194GEN_SPEOP_LDST(evlwhsplat, 0x0E, 2),
10195
10196GEN_SPEOP_LDST(evstdd, 0x10, 3),
10197GEN_SPEOP_LDST(evstdw, 0x11, 3),
10198GEN_SPEOP_LDST(evstdh, 0x12, 3),
10199GEN_SPEOP_LDST(evstwhe, 0x18, 2),
10200GEN_SPEOP_LDST(evstwho, 0x1A, 2),
10201GEN_SPEOP_LDST(evstwwe, 0x1C, 2),
10202GEN_SPEOP_LDST(evstwwo, 0x1E, 2),
10203};
10204
0411a972 10205#include "helper_regs.h"
a1389542 10206#include "translate_init.c"
79aceca5 10207
9a64fbe4 10208/*****************************************************************************/
3fc6c082 10209/* Misc PowerPC helpers */
878096ee
AF
10210void ppc_cpu_dump_state(CPUState *cs, FILE *f, fprintf_function cpu_fprintf,
10211 int flags)
79aceca5 10212{
3fc6c082
FB
10213#define RGPL 4
10214#define RFPL 4
3fc6c082 10215
878096ee
AF
10216 PowerPCCPU *cpu = POWERPC_CPU(cs);
10217 CPUPPCState *env = &cpu->env;
79aceca5
FB
10218 int i;
10219
90e189ec 10220 cpu_fprintf(f, "NIP " TARGET_FMT_lx " LR " TARGET_FMT_lx " CTR "
9a78eead 10221 TARGET_FMT_lx " XER " TARGET_FMT_lx "\n",
da91a00f 10222 env->nip, env->lr, env->ctr, cpu_read_xer(env));
90e189ec
BS
10223 cpu_fprintf(f, "MSR " TARGET_FMT_lx " HID0 " TARGET_FMT_lx " HF "
10224 TARGET_FMT_lx " idx %d\n", env->msr, env->spr[SPR_HID0],
10225 env->hflags, env->mmu_idx);
d9bce9d9 10226#if !defined(NO_TIMER_DUMP)
9a78eead 10227 cpu_fprintf(f, "TB %08" PRIu32 " %08" PRIu64
76a66253 10228#if !defined(CONFIG_USER_ONLY)
9a78eead 10229 " DECR %08" PRIu32
76a66253
JM
10230#endif
10231 "\n",
077fc206 10232 cpu_ppc_load_tbu(env), cpu_ppc_load_tbl(env)
76a66253
JM
10233#if !defined(CONFIG_USER_ONLY)
10234 , cpu_ppc_load_decr(env)
10235#endif
10236 );
077fc206 10237#endif
76a66253 10238 for (i = 0; i < 32; i++) {
3fc6c082
FB
10239 if ((i & (RGPL - 1)) == 0)
10240 cpu_fprintf(f, "GPR%02d", i);
b11ebf64 10241 cpu_fprintf(f, " %016" PRIx64, ppc_dump_gpr(env, i));
3fc6c082 10242 if ((i & (RGPL - 1)) == (RGPL - 1))
7fe48483 10243 cpu_fprintf(f, "\n");
76a66253 10244 }
3fc6c082 10245 cpu_fprintf(f, "CR ");
76a66253 10246 for (i = 0; i < 8; i++)
7fe48483
FB
10247 cpu_fprintf(f, "%01x", env->crf[i]);
10248 cpu_fprintf(f, " [");
76a66253
JM
10249 for (i = 0; i < 8; i++) {
10250 char a = '-';
10251 if (env->crf[i] & 0x08)
10252 a = 'L';
10253 else if (env->crf[i] & 0x04)
10254 a = 'G';
10255 else if (env->crf[i] & 0x02)
10256 a = 'E';
7fe48483 10257 cpu_fprintf(f, " %c%c", a, env->crf[i] & 0x01 ? 'O' : ' ');
76a66253 10258 }
90e189ec
BS
10259 cpu_fprintf(f, " ] RES " TARGET_FMT_lx "\n",
10260 env->reserve_addr);
3fc6c082
FB
10261 for (i = 0; i < 32; i++) {
10262 if ((i & (RFPL - 1)) == 0)
10263 cpu_fprintf(f, "FPR%02d", i);
26a76461 10264 cpu_fprintf(f, " %016" PRIx64, *((uint64_t *)&env->fpr[i]));
3fc6c082 10265 if ((i & (RFPL - 1)) == (RFPL - 1))
7fe48483 10266 cpu_fprintf(f, "\n");
79aceca5 10267 }
30304420 10268 cpu_fprintf(f, "FPSCR " TARGET_FMT_lx "\n", env->fpscr);
f2e63a42 10269#if !defined(CONFIG_USER_ONLY)
90dc8812
SW
10270 cpu_fprintf(f, " SRR0 " TARGET_FMT_lx " SRR1 " TARGET_FMT_lx
10271 " PVR " TARGET_FMT_lx " VRSAVE " TARGET_FMT_lx "\n",
10272 env->spr[SPR_SRR0], env->spr[SPR_SRR1],
10273 env->spr[SPR_PVR], env->spr[SPR_VRSAVE]);
10274
10275 cpu_fprintf(f, "SPRG0 " TARGET_FMT_lx " SPRG1 " TARGET_FMT_lx
10276 " SPRG2 " TARGET_FMT_lx " SPRG3 " TARGET_FMT_lx "\n",
10277 env->spr[SPR_SPRG0], env->spr[SPR_SPRG1],
10278 env->spr[SPR_SPRG2], env->spr[SPR_SPRG3]);
10279
10280 cpu_fprintf(f, "SPRG4 " TARGET_FMT_lx " SPRG5 " TARGET_FMT_lx
10281 " SPRG6 " TARGET_FMT_lx " SPRG7 " TARGET_FMT_lx "\n",
10282 env->spr[SPR_SPRG4], env->spr[SPR_SPRG5],
10283 env->spr[SPR_SPRG6], env->spr[SPR_SPRG7]);
10284
10285 if (env->excp_model == POWERPC_EXCP_BOOKE) {
10286 cpu_fprintf(f, "CSRR0 " TARGET_FMT_lx " CSRR1 " TARGET_FMT_lx
10287 " MCSRR0 " TARGET_FMT_lx " MCSRR1 " TARGET_FMT_lx "\n",
10288 env->spr[SPR_BOOKE_CSRR0], env->spr[SPR_BOOKE_CSRR1],
10289 env->spr[SPR_BOOKE_MCSRR0], env->spr[SPR_BOOKE_MCSRR1]);
10290
10291 cpu_fprintf(f, " TCR " TARGET_FMT_lx " TSR " TARGET_FMT_lx
10292 " ESR " TARGET_FMT_lx " DEAR " TARGET_FMT_lx "\n",
10293 env->spr[SPR_BOOKE_TCR], env->spr[SPR_BOOKE_TSR],
10294 env->spr[SPR_BOOKE_ESR], env->spr[SPR_BOOKE_DEAR]);
10295
10296 cpu_fprintf(f, " PIR " TARGET_FMT_lx " DECAR " TARGET_FMT_lx
10297 " IVPR " TARGET_FMT_lx " EPCR " TARGET_FMT_lx "\n",
10298 env->spr[SPR_BOOKE_PIR], env->spr[SPR_BOOKE_DECAR],
10299 env->spr[SPR_BOOKE_IVPR], env->spr[SPR_BOOKE_EPCR]);
10300
10301 cpu_fprintf(f, " MCSR " TARGET_FMT_lx " SPRG8 " TARGET_FMT_lx
10302 " EPR " TARGET_FMT_lx "\n",
10303 env->spr[SPR_BOOKE_MCSR], env->spr[SPR_BOOKE_SPRG8],
10304 env->spr[SPR_BOOKE_EPR]);
10305
10306 /* FSL-specific */
10307 cpu_fprintf(f, " MCAR " TARGET_FMT_lx " PID1 " TARGET_FMT_lx
10308 " PID2 " TARGET_FMT_lx " SVR " TARGET_FMT_lx "\n",
10309 env->spr[SPR_Exxx_MCAR], env->spr[SPR_BOOKE_PID1],
10310 env->spr[SPR_BOOKE_PID2], env->spr[SPR_E500_SVR]);
10311
10312 /*
10313 * IVORs are left out as they are large and do not change often --
10314 * they can be read with "p $ivor0", "p $ivor1", etc.
10315 */
10316 }
10317
697ab892
DG
10318#if defined(TARGET_PPC64)
10319 if (env->flags & POWERPC_FLAG_CFAR) {
10320 cpu_fprintf(f, " CFAR " TARGET_FMT_lx"\n", env->cfar);
10321 }
10322#endif
10323
90dc8812
SW
10324 switch (env->mmu_model) {
10325 case POWERPC_MMU_32B:
10326 case POWERPC_MMU_601:
10327 case POWERPC_MMU_SOFT_6xx:
10328 case POWERPC_MMU_SOFT_74xx:
10329#if defined(TARGET_PPC64)
90dc8812 10330 case POWERPC_MMU_64B:
ca480de6
AB
10331 case POWERPC_MMU_2_06:
10332 case POWERPC_MMU_2_06a:
10333 case POWERPC_MMU_2_06d:
90dc8812 10334#endif
ca480de6
AB
10335 cpu_fprintf(f, " SDR1 " TARGET_FMT_lx " DAR " TARGET_FMT_lx
10336 " DSISR " TARGET_FMT_lx "\n", env->spr[SPR_SDR1],
10337 env->spr[SPR_DAR], env->spr[SPR_DSISR]);
90dc8812 10338 break;
01662f3e 10339 case POWERPC_MMU_BOOKE206:
90dc8812
SW
10340 cpu_fprintf(f, " MAS0 " TARGET_FMT_lx " MAS1 " TARGET_FMT_lx
10341 " MAS2 " TARGET_FMT_lx " MAS3 " TARGET_FMT_lx "\n",
10342 env->spr[SPR_BOOKE_MAS0], env->spr[SPR_BOOKE_MAS1],
10343 env->spr[SPR_BOOKE_MAS2], env->spr[SPR_BOOKE_MAS3]);
10344
10345 cpu_fprintf(f, " MAS4 " TARGET_FMT_lx " MAS6 " TARGET_FMT_lx
10346 " MAS7 " TARGET_FMT_lx " PID " TARGET_FMT_lx "\n",
10347 env->spr[SPR_BOOKE_MAS4], env->spr[SPR_BOOKE_MAS6],
10348 env->spr[SPR_BOOKE_MAS7], env->spr[SPR_BOOKE_PID]);
10349
10350 cpu_fprintf(f, "MMUCFG " TARGET_FMT_lx " TLB0CFG " TARGET_FMT_lx
10351 " TLB1CFG " TARGET_FMT_lx "\n",
10352 env->spr[SPR_MMUCFG], env->spr[SPR_BOOKE_TLB0CFG],
10353 env->spr[SPR_BOOKE_TLB1CFG]);
10354 break;
10355 default:
10356 break;
10357 }
f2e63a42 10358#endif
79aceca5 10359
3fc6c082
FB
10360#undef RGPL
10361#undef RFPL
79aceca5
FB
10362}
10363
878096ee
AF
10364void ppc_cpu_dump_statistics(CPUState *cs, FILE*f,
10365 fprintf_function cpu_fprintf, int flags)
76a66253
JM
10366{
10367#if defined(DO_PPC_STATISTICS)
878096ee 10368 PowerPCCPU *cpu = POWERPC_CPU(cs);
c227f099 10369 opc_handler_t **t1, **t2, **t3, *handler;
76a66253
JM
10370 int op1, op2, op3;
10371
878096ee 10372 t1 = cpu->env.opcodes;
76a66253
JM
10373 for (op1 = 0; op1 < 64; op1++) {
10374 handler = t1[op1];
10375 if (is_indirect_opcode(handler)) {
10376 t2 = ind_table(handler);
10377 for (op2 = 0; op2 < 32; op2++) {
10378 handler = t2[op2];
10379 if (is_indirect_opcode(handler)) {
10380 t3 = ind_table(handler);
10381 for (op3 = 0; op3 < 32; op3++) {
10382 handler = t3[op3];
10383 if (handler->count == 0)
10384 continue;
10385 cpu_fprintf(f, "%02x %02x %02x (%02x %04d) %16s: "
0bfcd599 10386 "%016" PRIx64 " %" PRId64 "\n",
76a66253
JM
10387 op1, op2, op3, op1, (op3 << 5) | op2,
10388 handler->oname,
10389 handler->count, handler->count);
10390 }
10391 } else {
10392 if (handler->count == 0)
10393 continue;
10394 cpu_fprintf(f, "%02x %02x (%02x %04d) %16s: "
0bfcd599 10395 "%016" PRIx64 " %" PRId64 "\n",
76a66253
JM
10396 op1, op2, op1, op2, handler->oname,
10397 handler->count, handler->count);
10398 }
10399 }
10400 } else {
10401 if (handler->count == 0)
10402 continue;
0bfcd599
BS
10403 cpu_fprintf(f, "%02x (%02x ) %16s: %016" PRIx64
10404 " %" PRId64 "\n",
76a66253
JM
10405 op1, op1, handler->oname,
10406 handler->count, handler->count);
10407 }
10408 }
10409#endif
10410}
10411
9a64fbe4 10412/*****************************************************************************/
213fe1f5 10413static inline void gen_intermediate_code_internal(PowerPCCPU *cpu,
636aa200 10414 TranslationBlock *tb,
213fe1f5 10415 bool search_pc)
79aceca5 10416{
ed2803da 10417 CPUState *cs = CPU(cpu);
213fe1f5 10418 CPUPPCState *env = &cpu->env;
9fddaa0c 10419 DisasContext ctx, *ctxp = &ctx;
c227f099 10420 opc_handler_t **table, *handler;
0fa85d43 10421 target_ulong pc_start;
79aceca5 10422 uint16_t *gen_opc_end;
a1d1bb31 10423 CPUBreakpoint *bp;
79aceca5 10424 int j, lj = -1;
2e70f6ef
PB
10425 int num_insns;
10426 int max_insns;
79aceca5
FB
10427
10428 pc_start = tb->pc;
92414b31 10429 gen_opc_end = tcg_ctx.gen_opc_buf + OPC_MAX_SIZE;
046d6672 10430 ctx.nip = pc_start;
79aceca5 10431 ctx.tb = tb;
e1833e1f 10432 ctx.exception = POWERPC_EXCP_NONE;
3fc6c082 10433 ctx.spr_cb = env->spr_cb;
76db3ba4 10434 ctx.mem_idx = env->mmu_idx;
7d08d856
AJ
10435 ctx.insns_flags = env->insns_flags;
10436 ctx.insns_flags2 = env->insns_flags2;
76db3ba4
AJ
10437 ctx.access_type = -1;
10438 ctx.le_mode = env->hflags & (1 << MSR_LE) ? 1 : 0;
d9bce9d9 10439#if defined(TARGET_PPC64)
e42a61f1 10440 ctx.sf_mode = msr_is_64bit(env, env->msr);
697ab892 10441 ctx.has_cfar = !!(env->flags & POWERPC_FLAG_CFAR);
9a64fbe4 10442#endif
3cc62370 10443 ctx.fpu_enabled = msr_fp;
a9d9eb8f 10444 if ((env->flags & POWERPC_FLAG_SPE) && msr_spe)
d26bfc9a
JM
10445 ctx.spe_enabled = msr_spe;
10446 else
10447 ctx.spe_enabled = 0;
a9d9eb8f
JM
10448 if ((env->flags & POWERPC_FLAG_VRE) && msr_vr)
10449 ctx.altivec_enabled = msr_vr;
10450 else
10451 ctx.altivec_enabled = 0;
1f29871c
TM
10452 if ((env->flags & POWERPC_FLAG_VSX) && msr_vsx) {
10453 ctx.vsx_enabled = msr_vsx;
10454 } else {
10455 ctx.vsx_enabled = 0;
10456 }
d26bfc9a 10457 if ((env->flags & POWERPC_FLAG_SE) && msr_se)
8cbcb4fa 10458 ctx.singlestep_enabled = CPU_SINGLE_STEP;
d26bfc9a 10459 else
8cbcb4fa 10460 ctx.singlestep_enabled = 0;
d26bfc9a 10461 if ((env->flags & POWERPC_FLAG_BE) && msr_be)
8cbcb4fa 10462 ctx.singlestep_enabled |= CPU_BRANCH_STEP;
ed2803da 10463 if (unlikely(cs->singlestep_enabled)) {
8cbcb4fa 10464 ctx.singlestep_enabled |= GDBSTUB_SINGLE_STEP;
ed2803da 10465 }
3fc6c082 10466#if defined (DO_SINGLE_STEP) && 0
9a64fbe4
FB
10467 /* Single step trace mode */
10468 msr_se = 1;
10469#endif
2e70f6ef
PB
10470 num_insns = 0;
10471 max_insns = tb->cflags & CF_COUNT_MASK;
10472 if (max_insns == 0)
10473 max_insns = CF_COUNT_MASK;
10474
806f352d 10475 gen_tb_start();
9a64fbe4 10476 /* Set env in case of segfault during code fetch */
efd7f486
EV
10477 while (ctx.exception == POWERPC_EXCP_NONE
10478 && tcg_ctx.gen_opc_ptr < gen_opc_end) {
72cf2d4f
BS
10479 if (unlikely(!QTAILQ_EMPTY(&env->breakpoints))) {
10480 QTAILQ_FOREACH(bp, &env->breakpoints, entry) {
a1d1bb31 10481 if (bp->pc == ctx.nip) {
e06fcd75 10482 gen_debug_exception(ctxp);
ea4e754f
FB
10483 break;
10484 }
10485 }
10486 }
76a66253 10487 if (unlikely(search_pc)) {
92414b31 10488 j = tcg_ctx.gen_opc_ptr - tcg_ctx.gen_opc_buf;
79aceca5
FB
10489 if (lj < j) {
10490 lj++;
10491 while (lj < j)
ab1103de 10492 tcg_ctx.gen_opc_instr_start[lj++] = 0;
79aceca5 10493 }
25983cad 10494 tcg_ctx.gen_opc_pc[lj] = ctx.nip;
ab1103de 10495 tcg_ctx.gen_opc_instr_start[lj] = 1;
c9c99c22 10496 tcg_ctx.gen_opc_icount[lj] = num_insns;
79aceca5 10497 }
d12d51d5 10498 LOG_DISAS("----------------\n");
90e189ec 10499 LOG_DISAS("nip=" TARGET_FMT_lx " super=%d ir=%d\n",
d12d51d5 10500 ctx.nip, ctx.mem_idx, (int)msr_ir);
2e70f6ef
PB
10501 if (num_insns + 1 == max_insns && (tb->cflags & CF_LAST_IO))
10502 gen_io_start();
76db3ba4 10503 if (unlikely(ctx.le_mode)) {
2f5a189c 10504 ctx.opcode = bswap32(cpu_ldl_code(env, ctx.nip));
056401ea 10505 } else {
2f5a189c 10506 ctx.opcode = cpu_ldl_code(env, ctx.nip);
111bfab3 10507 }
d12d51d5 10508 LOG_DISAS("translate opcode %08x (%02x %02x %02x) (%s)\n",
9a64fbe4 10509 ctx.opcode, opc1(ctx.opcode), opc2(ctx.opcode),
476b6d16 10510 opc3(ctx.opcode), ctx.le_mode ? "little" : "big");
fdefe51c 10511 if (unlikely(qemu_loglevel_mask(CPU_LOG_TB_OP | CPU_LOG_TB_OP_OPT))) {
731c54f8 10512 tcg_gen_debug_insn_start(ctx.nip);
fdefe51c 10513 }
046d6672 10514 ctx.nip += 4;
3fc6c082 10515 table = env->opcodes;
2e70f6ef 10516 num_insns++;
79aceca5
FB
10517 handler = table[opc1(ctx.opcode)];
10518 if (is_indirect_opcode(handler)) {
10519 table = ind_table(handler);
10520 handler = table[opc2(ctx.opcode)];
10521 if (is_indirect_opcode(handler)) {
10522 table = ind_table(handler);
10523 handler = table[opc3(ctx.opcode)];
10524 }
10525 }
10526 /* Is opcode *REALLY* valid ? */
76a66253 10527 if (unlikely(handler->handler == &gen_invalid)) {
93fcfe39
AL
10528 if (qemu_log_enabled()) {
10529 qemu_log("invalid/unsupported opcode: "
90e189ec
BS
10530 "%02x - %02x - %02x (%08x) " TARGET_FMT_lx " %d\n",
10531 opc1(ctx.opcode), opc2(ctx.opcode),
10532 opc3(ctx.opcode), ctx.opcode, ctx.nip - 4, (int)msr_ir);
4b3686fa 10533 }
76a66253 10534 } else {
70560da7
FC
10535 uint32_t inval;
10536
10537 if (unlikely(handler->type & (PPC_SPE | PPC_SPE_SINGLE | PPC_SPE_DOUBLE) && Rc(ctx.opcode))) {
10538 inval = handler->inval2;
10539 } else {
10540 inval = handler->inval1;
10541 }
10542
10543 if (unlikely((ctx.opcode & inval) != 0)) {
93fcfe39
AL
10544 if (qemu_log_enabled()) {
10545 qemu_log("invalid bits: %08x for opcode: "
90e189ec 10546 "%02x - %02x - %02x (%08x) " TARGET_FMT_lx "\n",
70560da7 10547 ctx.opcode & inval, opc1(ctx.opcode),
90e189ec
BS
10548 opc2(ctx.opcode), opc3(ctx.opcode),
10549 ctx.opcode, ctx.nip - 4);
76a66253 10550 }
e06fcd75 10551 gen_inval_exception(ctxp, POWERPC_EXCP_INVAL_INVAL);
4b3686fa 10552 break;
79aceca5 10553 }
79aceca5 10554 }
4b3686fa 10555 (*(handler->handler))(&ctx);
76a66253
JM
10556#if defined(DO_PPC_STATISTICS)
10557 handler->count++;
10558#endif
9a64fbe4 10559 /* Check trace mode exceptions */
8cbcb4fa
AJ
10560 if (unlikely(ctx.singlestep_enabled & CPU_SINGLE_STEP &&
10561 (ctx.nip <= 0x100 || ctx.nip > 0xF00) &&
10562 ctx.exception != POWERPC_SYSCALL &&
10563 ctx.exception != POWERPC_EXCP_TRAP &&
10564 ctx.exception != POWERPC_EXCP_BRANCH)) {
e06fcd75 10565 gen_exception(ctxp, POWERPC_EXCP_TRACE);
d26bfc9a 10566 } else if (unlikely(((ctx.nip & (TARGET_PAGE_SIZE - 1)) == 0) ||
ed2803da 10567 (cs->singlestep_enabled) ||
1b530a6d 10568 singlestep ||
2e70f6ef 10569 num_insns >= max_insns)) {
d26bfc9a
JM
10570 /* if we reach a page boundary or are single stepping, stop
10571 * generation
10572 */
8dd4983c 10573 break;
76a66253 10574 }
3fc6c082 10575 }
2e70f6ef
PB
10576 if (tb->cflags & CF_LAST_IO)
10577 gen_io_end();
e1833e1f 10578 if (ctx.exception == POWERPC_EXCP_NONE) {
c1942362 10579 gen_goto_tb(&ctx, 0, ctx.nip);
e1833e1f 10580 } else if (ctx.exception != POWERPC_EXCP_BRANCH) {
ed2803da 10581 if (unlikely(cs->singlestep_enabled)) {
e06fcd75 10582 gen_debug_exception(ctxp);
8cbcb4fa 10583 }
76a66253 10584 /* Generate the return instruction */
57fec1fe 10585 tcg_gen_exit_tb(0);
9a64fbe4 10586 }
806f352d 10587 gen_tb_end(tb, num_insns);
efd7f486 10588 *tcg_ctx.gen_opc_ptr = INDEX_op_end;
76a66253 10589 if (unlikely(search_pc)) {
92414b31 10590 j = tcg_ctx.gen_opc_ptr - tcg_ctx.gen_opc_buf;
9a64fbe4
FB
10591 lj++;
10592 while (lj <= j)
ab1103de 10593 tcg_ctx.gen_opc_instr_start[lj++] = 0;
9a64fbe4 10594 } else {
046d6672 10595 tb->size = ctx.nip - pc_start;
2e70f6ef 10596 tb->icount = num_insns;
9a64fbe4 10597 }
d9bce9d9 10598#if defined(DEBUG_DISAS)
8fec2b8c 10599 if (qemu_loglevel_mask(CPU_LOG_TB_IN_ASM)) {
76a66253 10600 int flags;
237c0af0 10601 flags = env->bfd_mach;
76db3ba4 10602 flags |= ctx.le_mode << 16;
93fcfe39 10603 qemu_log("IN: %s\n", lookup_symbol(pc_start));
f4359b9f 10604 log_target_disas(env, pc_start, ctx.nip - pc_start, flags);
93fcfe39 10605 qemu_log("\n");
9fddaa0c 10606 }
79aceca5 10607#endif
79aceca5
FB
10608}
10609
1328c2bf 10610void gen_intermediate_code (CPUPPCState *env, struct TranslationBlock *tb)
79aceca5 10611{
213fe1f5 10612 gen_intermediate_code_internal(ppc_env_get_cpu(env), tb, false);
79aceca5
FB
10613}
10614
1328c2bf 10615void gen_intermediate_code_pc (CPUPPCState *env, struct TranslationBlock *tb)
79aceca5 10616{
213fe1f5 10617 gen_intermediate_code_internal(ppc_env_get_cpu(env), tb, true);
79aceca5 10618}
d2856f1a 10619
1328c2bf 10620void restore_state_to_opc(CPUPPCState *env, TranslationBlock *tb, int pc_pos)
d2856f1a 10621{
25983cad 10622 env->nip = tcg_ctx.gen_opc_pc[pc_pos];
d2856f1a 10623}
This page took 2.914767 seconds and 4 git commands to generate.