]> Git Repo - qemu.git/blame - target-ppc/translate.c
target-ppc: Add VSX ISA2.06 xrsqrte 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)
ee6e02c0
TM
7314
7315GEN_VSX_HELPER_2(xvadddp, 0x00, 0x0C, 0, PPC2_VSX)
7316GEN_VSX_HELPER_2(xvsubdp, 0x00, 0x0D, 0, PPC2_VSX)
5e591d88 7317GEN_VSX_HELPER_2(xvmuldp, 0x00, 0x0E, 0, PPC2_VSX)
4b98eeef 7318GEN_VSX_HELPER_2(xvdivdp, 0x00, 0x0F, 0, PPC2_VSX)
2009227f 7319GEN_VSX_HELPER_2(xvredp, 0x14, 0x0D, 0, PPC2_VSX)
d32404fe 7320GEN_VSX_HELPER_2(xvsqrtdp, 0x16, 0x0C, 0, PPC2_VSX)
d3f9df8f 7321GEN_VSX_HELPER_2(xvrsqrtedp, 0x14, 0x0C, 0, PPC2_VSX)
ee6e02c0
TM
7322
7323GEN_VSX_HELPER_2(xvaddsp, 0x00, 0x08, 0, PPC2_VSX)
7324GEN_VSX_HELPER_2(xvsubsp, 0x00, 0x09, 0, PPC2_VSX)
5e591d88 7325GEN_VSX_HELPER_2(xvmulsp, 0x00, 0x0A, 0, PPC2_VSX)
4b98eeef 7326GEN_VSX_HELPER_2(xvdivsp, 0x00, 0x0B, 0, PPC2_VSX)
2009227f 7327GEN_VSX_HELPER_2(xvresp, 0x14, 0x09, 0, PPC2_VSX)
d32404fe 7328GEN_VSX_HELPER_2(xvsqrtsp, 0x16, 0x08, 0, PPC2_VSX)
d3f9df8f 7329GEN_VSX_HELPER_2(xvrsqrtesp, 0x14, 0x08, 0, PPC2_VSX)
ee6e02c0 7330
79ca8a6a
TM
7331#define VSX_LOGICAL(name, tcg_op) \
7332static void glue(gen_, name)(DisasContext * ctx) \
7333 { \
7334 if (unlikely(!ctx->vsx_enabled)) { \
7335 gen_exception(ctx, POWERPC_EXCP_VSXU); \
7336 return; \
7337 } \
7338 tcg_op(cpu_vsrh(xT(ctx->opcode)), cpu_vsrh(xA(ctx->opcode)), \
7339 cpu_vsrh(xB(ctx->opcode))); \
7340 tcg_op(cpu_vsrl(xT(ctx->opcode)), cpu_vsrl(xA(ctx->opcode)), \
7341 cpu_vsrl(xB(ctx->opcode))); \
7342 }
7343
f976b09e
AG
7344VSX_LOGICAL(xxland, tcg_gen_and_i64)
7345VSX_LOGICAL(xxlandc, tcg_gen_andc_i64)
7346VSX_LOGICAL(xxlor, tcg_gen_or_i64)
7347VSX_LOGICAL(xxlxor, tcg_gen_xor_i64)
7348VSX_LOGICAL(xxlnor, tcg_gen_nor_i64)
df020ce0 7349
ce577d2e
TM
7350#define VSX_XXMRG(name, high) \
7351static void glue(gen_, name)(DisasContext * ctx) \
7352 { \
7353 TCGv_i64 a0, a1, b0, b1; \
7354 if (unlikely(!ctx->vsx_enabled)) { \
7355 gen_exception(ctx, POWERPC_EXCP_VSXU); \
7356 return; \
7357 } \
f976b09e
AG
7358 a0 = tcg_temp_new_i64(); \
7359 a1 = tcg_temp_new_i64(); \
7360 b0 = tcg_temp_new_i64(); \
7361 b1 = tcg_temp_new_i64(); \
ce577d2e
TM
7362 if (high) { \
7363 tcg_gen_mov_i64(a0, cpu_vsrh(xA(ctx->opcode))); \
7364 tcg_gen_mov_i64(a1, cpu_vsrh(xA(ctx->opcode))); \
7365 tcg_gen_mov_i64(b0, cpu_vsrh(xB(ctx->opcode))); \
7366 tcg_gen_mov_i64(b1, cpu_vsrh(xB(ctx->opcode))); \
7367 } else { \
7368 tcg_gen_mov_i64(a0, cpu_vsrl(xA(ctx->opcode))); \
7369 tcg_gen_mov_i64(a1, cpu_vsrl(xA(ctx->opcode))); \
7370 tcg_gen_mov_i64(b0, cpu_vsrl(xB(ctx->opcode))); \
7371 tcg_gen_mov_i64(b1, cpu_vsrl(xB(ctx->opcode))); \
7372 } \
7373 tcg_gen_shri_i64(a0, a0, 32); \
7374 tcg_gen_shri_i64(b0, b0, 32); \
7375 tcg_gen_deposit_i64(cpu_vsrh(xT(ctx->opcode)), \
7376 b0, a0, 32, 32); \
7377 tcg_gen_deposit_i64(cpu_vsrl(xT(ctx->opcode)), \
7378 b1, a1, 32, 32); \
f976b09e
AG
7379 tcg_temp_free_i64(a0); \
7380 tcg_temp_free_i64(a1); \
7381 tcg_temp_free_i64(b0); \
7382 tcg_temp_free_i64(b1); \
ce577d2e
TM
7383 }
7384
7385VSX_XXMRG(xxmrghw, 1)
7386VSX_XXMRG(xxmrglw, 0)
7387
551e3ef7
TM
7388static void gen_xxsel(DisasContext * ctx)
7389{
7390 TCGv_i64 a, b, c;
7391 if (unlikely(!ctx->vsx_enabled)) {
7392 gen_exception(ctx, POWERPC_EXCP_VSXU);
7393 return;
7394 }
f976b09e
AG
7395 a = tcg_temp_new_i64();
7396 b = tcg_temp_new_i64();
7397 c = tcg_temp_new_i64();
551e3ef7
TM
7398
7399 tcg_gen_mov_i64(a, cpu_vsrh(xA(ctx->opcode)));
7400 tcg_gen_mov_i64(b, cpu_vsrh(xB(ctx->opcode)));
7401 tcg_gen_mov_i64(c, cpu_vsrh(xC(ctx->opcode)));
7402
7403 tcg_gen_and_i64(b, b, c);
7404 tcg_gen_andc_i64(a, a, c);
7405 tcg_gen_or_i64(cpu_vsrh(xT(ctx->opcode)), a, b);
7406
7407 tcg_gen_mov_i64(a, cpu_vsrl(xA(ctx->opcode)));
7408 tcg_gen_mov_i64(b, cpu_vsrl(xB(ctx->opcode)));
7409 tcg_gen_mov_i64(c, cpu_vsrl(xC(ctx->opcode)));
7410
7411 tcg_gen_and_i64(b, b, c);
7412 tcg_gen_andc_i64(a, a, c);
7413 tcg_gen_or_i64(cpu_vsrl(xT(ctx->opcode)), a, b);
7414
f976b09e
AG
7415 tcg_temp_free_i64(a);
7416 tcg_temp_free_i64(b);
7417 tcg_temp_free_i64(c);
551e3ef7
TM
7418}
7419
76c15fe0
TM
7420static void gen_xxspltw(DisasContext *ctx)
7421{
7422 TCGv_i64 b, b2;
7423 TCGv_i64 vsr = (UIM(ctx->opcode) & 2) ?
7424 cpu_vsrl(xB(ctx->opcode)) :
7425 cpu_vsrh(xB(ctx->opcode));
7426
7427 if (unlikely(!ctx->vsx_enabled)) {
7428 gen_exception(ctx, POWERPC_EXCP_VSXU);
7429 return;
7430 }
7431
f976b09e
AG
7432 b = tcg_temp_new_i64();
7433 b2 = tcg_temp_new_i64();
76c15fe0
TM
7434
7435 if (UIM(ctx->opcode) & 1) {
7436 tcg_gen_ext32u_i64(b, vsr);
7437 } else {
7438 tcg_gen_shri_i64(b, vsr, 32);
7439 }
7440
7441 tcg_gen_shli_i64(b2, b, 32);
7442 tcg_gen_or_i64(cpu_vsrh(xT(ctx->opcode)), b, b2);
7443 tcg_gen_mov_i64(cpu_vsrl(xT(ctx->opcode)), cpu_vsrh(xT(ctx->opcode)));
7444
f976b09e
AG
7445 tcg_temp_free_i64(b);
7446 tcg_temp_free_i64(b2);
76c15fe0
TM
7447}
7448
acc42968
TM
7449static void gen_xxsldwi(DisasContext *ctx)
7450{
7451 TCGv_i64 xth, xtl;
7452 if (unlikely(!ctx->vsx_enabled)) {
7453 gen_exception(ctx, POWERPC_EXCP_VSXU);
7454 return;
7455 }
f976b09e
AG
7456 xth = tcg_temp_new_i64();
7457 xtl = tcg_temp_new_i64();
acc42968
TM
7458
7459 switch (SHW(ctx->opcode)) {
7460 case 0: {
7461 tcg_gen_mov_i64(xth, cpu_vsrh(xA(ctx->opcode)));
7462 tcg_gen_mov_i64(xtl, cpu_vsrl(xA(ctx->opcode)));
7463 break;
7464 }
7465 case 1: {
f976b09e 7466 TCGv_i64 t0 = tcg_temp_new_i64();
acc42968
TM
7467 tcg_gen_mov_i64(xth, cpu_vsrh(xA(ctx->opcode)));
7468 tcg_gen_shli_i64(xth, xth, 32);
7469 tcg_gen_mov_i64(t0, cpu_vsrl(xA(ctx->opcode)));
7470 tcg_gen_shri_i64(t0, t0, 32);
7471 tcg_gen_or_i64(xth, xth, t0);
7472 tcg_gen_mov_i64(xtl, cpu_vsrl(xA(ctx->opcode)));
7473 tcg_gen_shli_i64(xtl, xtl, 32);
7474 tcg_gen_mov_i64(t0, cpu_vsrh(xB(ctx->opcode)));
7475 tcg_gen_shri_i64(t0, t0, 32);
7476 tcg_gen_or_i64(xtl, xtl, t0);
f976b09e 7477 tcg_temp_free_i64(t0);
acc42968
TM
7478 break;
7479 }
7480 case 2: {
7481 tcg_gen_mov_i64(xth, cpu_vsrl(xA(ctx->opcode)));
7482 tcg_gen_mov_i64(xtl, cpu_vsrh(xB(ctx->opcode)));
7483 break;
7484 }
7485 case 3: {
f976b09e 7486 TCGv_i64 t0 = tcg_temp_new_i64();
acc42968
TM
7487 tcg_gen_mov_i64(xth, cpu_vsrl(xA(ctx->opcode)));
7488 tcg_gen_shli_i64(xth, xth, 32);
7489 tcg_gen_mov_i64(t0, cpu_vsrh(xB(ctx->opcode)));
7490 tcg_gen_shri_i64(t0, t0, 32);
7491 tcg_gen_or_i64(xth, xth, t0);
7492 tcg_gen_mov_i64(xtl, cpu_vsrh(xB(ctx->opcode)));
7493 tcg_gen_shli_i64(xtl, xtl, 32);
7494 tcg_gen_mov_i64(t0, cpu_vsrl(xB(ctx->opcode)));
7495 tcg_gen_shri_i64(t0, t0, 32);
7496 tcg_gen_or_i64(xtl, xtl, t0);
f976b09e 7497 tcg_temp_free_i64(t0);
acc42968
TM
7498 break;
7499 }
7500 }
7501
7502 tcg_gen_mov_i64(cpu_vsrh(xT(ctx->opcode)), xth);
7503 tcg_gen_mov_i64(cpu_vsrl(xT(ctx->opcode)), xtl);
7504
f976b09e
AG
7505 tcg_temp_free_i64(xth);
7506 tcg_temp_free_i64(xtl);
acc42968
TM
7507}
7508
ce577d2e 7509
0487d6a8 7510/*** SPE extension ***/
0487d6a8 7511/* Register moves */
3cd7d1dd 7512
a0e13900
FC
7513static inline void gen_evmra(DisasContext *ctx)
7514{
7515
7516 if (unlikely(!ctx->spe_enabled)) {
27a69bb0 7517 gen_exception(ctx, POWERPC_EXCP_SPEU);
a0e13900
FC
7518 return;
7519 }
7520
7521#if defined(TARGET_PPC64)
7522 /* rD := rA */
7523 tcg_gen_mov_i64(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)]);
7524
7525 /* spe_acc := rA */
7526 tcg_gen_st_i64(cpu_gpr[rA(ctx->opcode)],
7527 cpu_env,
1328c2bf 7528 offsetof(CPUPPCState, spe_acc));
a0e13900
FC
7529#else
7530 TCGv_i64 tmp = tcg_temp_new_i64();
7531
7532 /* tmp := rA_lo + rA_hi << 32 */
7533 tcg_gen_concat_i32_i64(tmp, cpu_gpr[rA(ctx->opcode)], cpu_gprh[rA(ctx->opcode)]);
7534
7535 /* spe_acc := tmp */
1328c2bf 7536 tcg_gen_st_i64(tmp, cpu_env, offsetof(CPUPPCState, spe_acc));
a0e13900
FC
7537 tcg_temp_free_i64(tmp);
7538
7539 /* rD := rA */
7540 tcg_gen_mov_i32(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)]);
7541 tcg_gen_mov_i32(cpu_gprh[rD(ctx->opcode)], cpu_gprh[rA(ctx->opcode)]);
7542#endif
7543}
7544
636aa200
BS
7545static inline void gen_load_gpr64(TCGv_i64 t, int reg)
7546{
f78fb44e
AJ
7547#if defined(TARGET_PPC64)
7548 tcg_gen_mov_i64(t, cpu_gpr[reg]);
7549#else
36aa55dc 7550 tcg_gen_concat_i32_i64(t, cpu_gpr[reg], cpu_gprh[reg]);
3cd7d1dd 7551#endif
f78fb44e 7552}
3cd7d1dd 7553
636aa200
BS
7554static inline void gen_store_gpr64(int reg, TCGv_i64 t)
7555{
f78fb44e
AJ
7556#if defined(TARGET_PPC64)
7557 tcg_gen_mov_i64(cpu_gpr[reg], t);
7558#else
a7812ae4 7559 TCGv_i64 tmp = tcg_temp_new_i64();
f78fb44e 7560 tcg_gen_trunc_i64_i32(cpu_gpr[reg], t);
f78fb44e
AJ
7561 tcg_gen_shri_i64(tmp, t, 32);
7562 tcg_gen_trunc_i64_i32(cpu_gprh[reg], tmp);
a7812ae4 7563 tcg_temp_free_i64(tmp);
3cd7d1dd 7564#endif
f78fb44e 7565}
3cd7d1dd 7566
70560da7 7567#define GEN_SPE(name0, name1, opc2, opc3, inval0, inval1, type) \
99e300ef 7568static void glue(gen_, name0##_##name1)(DisasContext *ctx) \
0487d6a8
JM
7569{ \
7570 if (Rc(ctx->opcode)) \
7571 gen_##name1(ctx); \
7572 else \
7573 gen_##name0(ctx); \
7574}
7575
7576/* Handler for undefined SPE opcodes */
636aa200 7577static inline void gen_speundef(DisasContext *ctx)
0487d6a8 7578{
e06fcd75 7579 gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL);
0487d6a8
JM
7580}
7581
57951c27
AJ
7582/* SPE logic */
7583#if defined(TARGET_PPC64)
7584#define GEN_SPEOP_LOGIC2(name, tcg_op) \
636aa200 7585static inline void gen_##name(DisasContext *ctx) \
0487d6a8
JM
7586{ \
7587 if (unlikely(!ctx->spe_enabled)) { \
27a69bb0 7588 gen_exception(ctx, POWERPC_EXCP_SPEU); \
0487d6a8
JM
7589 return; \
7590 } \
57951c27
AJ
7591 tcg_op(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)], \
7592 cpu_gpr[rB(ctx->opcode)]); \
7593}
7594#else
7595#define GEN_SPEOP_LOGIC2(name, tcg_op) \
636aa200 7596static inline void gen_##name(DisasContext *ctx) \
57951c27
AJ
7597{ \
7598 if (unlikely(!ctx->spe_enabled)) { \
27a69bb0 7599 gen_exception(ctx, POWERPC_EXCP_SPEU); \
57951c27
AJ
7600 return; \
7601 } \
7602 tcg_op(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)], \
7603 cpu_gpr[rB(ctx->opcode)]); \
7604 tcg_op(cpu_gprh[rD(ctx->opcode)], cpu_gprh[rA(ctx->opcode)], \
7605 cpu_gprh[rB(ctx->opcode)]); \
0487d6a8 7606}
57951c27
AJ
7607#endif
7608
7609GEN_SPEOP_LOGIC2(evand, tcg_gen_and_tl);
7610GEN_SPEOP_LOGIC2(evandc, tcg_gen_andc_tl);
7611GEN_SPEOP_LOGIC2(evxor, tcg_gen_xor_tl);
7612GEN_SPEOP_LOGIC2(evor, tcg_gen_or_tl);
7613GEN_SPEOP_LOGIC2(evnor, tcg_gen_nor_tl);
7614GEN_SPEOP_LOGIC2(eveqv, tcg_gen_eqv_tl);
7615GEN_SPEOP_LOGIC2(evorc, tcg_gen_orc_tl);
7616GEN_SPEOP_LOGIC2(evnand, tcg_gen_nand_tl);
0487d6a8 7617
57951c27
AJ
7618/* SPE logic immediate */
7619#if defined(TARGET_PPC64)
7620#define GEN_SPEOP_TCG_LOGIC_IMM2(name, tcg_opi) \
636aa200 7621static inline void gen_##name(DisasContext *ctx) \
3d3a6a0a
AJ
7622{ \
7623 if (unlikely(!ctx->spe_enabled)) { \
27a69bb0 7624 gen_exception(ctx, POWERPC_EXCP_SPEU); \
3d3a6a0a
AJ
7625 return; \
7626 } \
a7812ae4
PB
7627 TCGv_i32 t0 = tcg_temp_local_new_i32(); \
7628 TCGv_i32 t1 = tcg_temp_local_new_i32(); \
7629 TCGv_i64 t2 = tcg_temp_local_new_i64(); \
57951c27
AJ
7630 tcg_gen_trunc_i64_i32(t0, cpu_gpr[rA(ctx->opcode)]); \
7631 tcg_opi(t0, t0, rB(ctx->opcode)); \
7632 tcg_gen_shri_i64(t2, cpu_gpr[rA(ctx->opcode)], 32); \
7633 tcg_gen_trunc_i64_i32(t1, t2); \
a7812ae4 7634 tcg_temp_free_i64(t2); \
57951c27
AJ
7635 tcg_opi(t1, t1, rB(ctx->opcode)); \
7636 tcg_gen_concat_i32_i64(cpu_gpr[rD(ctx->opcode)], t0, t1); \
a7812ae4
PB
7637 tcg_temp_free_i32(t0); \
7638 tcg_temp_free_i32(t1); \
3d3a6a0a 7639}
57951c27
AJ
7640#else
7641#define GEN_SPEOP_TCG_LOGIC_IMM2(name, tcg_opi) \
636aa200 7642static inline void gen_##name(DisasContext *ctx) \
0487d6a8
JM
7643{ \
7644 if (unlikely(!ctx->spe_enabled)) { \
27a69bb0 7645 gen_exception(ctx, POWERPC_EXCP_SPEU); \
0487d6a8
JM
7646 return; \
7647 } \
57951c27
AJ
7648 tcg_opi(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)], \
7649 rB(ctx->opcode)); \
7650 tcg_opi(cpu_gprh[rD(ctx->opcode)], cpu_gprh[rA(ctx->opcode)], \
7651 rB(ctx->opcode)); \
0487d6a8 7652}
57951c27
AJ
7653#endif
7654GEN_SPEOP_TCG_LOGIC_IMM2(evslwi, tcg_gen_shli_i32);
7655GEN_SPEOP_TCG_LOGIC_IMM2(evsrwiu, tcg_gen_shri_i32);
7656GEN_SPEOP_TCG_LOGIC_IMM2(evsrwis, tcg_gen_sari_i32);
7657GEN_SPEOP_TCG_LOGIC_IMM2(evrlwi, tcg_gen_rotli_i32);
0487d6a8 7658
57951c27
AJ
7659/* SPE arithmetic */
7660#if defined(TARGET_PPC64)
7661#define GEN_SPEOP_ARITH1(name, tcg_op) \
636aa200 7662static inline void gen_##name(DisasContext *ctx) \
0487d6a8
JM
7663{ \
7664 if (unlikely(!ctx->spe_enabled)) { \
27a69bb0 7665 gen_exception(ctx, POWERPC_EXCP_SPEU); \
0487d6a8
JM
7666 return; \
7667 } \
a7812ae4
PB
7668 TCGv_i32 t0 = tcg_temp_local_new_i32(); \
7669 TCGv_i32 t1 = tcg_temp_local_new_i32(); \
7670 TCGv_i64 t2 = tcg_temp_local_new_i64(); \
57951c27
AJ
7671 tcg_gen_trunc_i64_i32(t0, cpu_gpr[rA(ctx->opcode)]); \
7672 tcg_op(t0, t0); \
7673 tcg_gen_shri_i64(t2, cpu_gpr[rA(ctx->opcode)], 32); \
7674 tcg_gen_trunc_i64_i32(t1, t2); \
a7812ae4 7675 tcg_temp_free_i64(t2); \
57951c27
AJ
7676 tcg_op(t1, t1); \
7677 tcg_gen_concat_i32_i64(cpu_gpr[rD(ctx->opcode)], t0, t1); \
a7812ae4
PB
7678 tcg_temp_free_i32(t0); \
7679 tcg_temp_free_i32(t1); \
0487d6a8 7680}
57951c27 7681#else
a7812ae4 7682#define GEN_SPEOP_ARITH1(name, tcg_op) \
636aa200 7683static inline void gen_##name(DisasContext *ctx) \
57951c27
AJ
7684{ \
7685 if (unlikely(!ctx->spe_enabled)) { \
27a69bb0 7686 gen_exception(ctx, POWERPC_EXCP_SPEU); \
57951c27
AJ
7687 return; \
7688 } \
7689 tcg_op(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)]); \
7690 tcg_op(cpu_gprh[rD(ctx->opcode)], cpu_gprh[rA(ctx->opcode)]); \
7691}
7692#endif
0487d6a8 7693
636aa200 7694static inline void gen_op_evabs(TCGv_i32 ret, TCGv_i32 arg1)
57951c27
AJ
7695{
7696 int l1 = gen_new_label();
7697 int l2 = gen_new_label();
0487d6a8 7698
57951c27
AJ
7699 tcg_gen_brcondi_i32(TCG_COND_GE, arg1, 0, l1);
7700 tcg_gen_neg_i32(ret, arg1);
7701 tcg_gen_br(l2);
7702 gen_set_label(l1);
a7812ae4 7703 tcg_gen_mov_i32(ret, arg1);
57951c27
AJ
7704 gen_set_label(l2);
7705}
7706GEN_SPEOP_ARITH1(evabs, gen_op_evabs);
7707GEN_SPEOP_ARITH1(evneg, tcg_gen_neg_i32);
7708GEN_SPEOP_ARITH1(evextsb, tcg_gen_ext8s_i32);
7709GEN_SPEOP_ARITH1(evextsh, tcg_gen_ext16s_i32);
636aa200 7710static inline void gen_op_evrndw(TCGv_i32 ret, TCGv_i32 arg1)
0487d6a8 7711{
57951c27
AJ
7712 tcg_gen_addi_i32(ret, arg1, 0x8000);
7713 tcg_gen_ext16u_i32(ret, ret);
7714}
7715GEN_SPEOP_ARITH1(evrndw, gen_op_evrndw);
a7812ae4
PB
7716GEN_SPEOP_ARITH1(evcntlsw, gen_helper_cntlsw32);
7717GEN_SPEOP_ARITH1(evcntlzw, gen_helper_cntlzw32);
0487d6a8 7718
57951c27
AJ
7719#if defined(TARGET_PPC64)
7720#define GEN_SPEOP_ARITH2(name, tcg_op) \
636aa200 7721static inline void gen_##name(DisasContext *ctx) \
0487d6a8
JM
7722{ \
7723 if (unlikely(!ctx->spe_enabled)) { \
27a69bb0 7724 gen_exception(ctx, POWERPC_EXCP_SPEU); \
0487d6a8
JM
7725 return; \
7726 } \
a7812ae4
PB
7727 TCGv_i32 t0 = tcg_temp_local_new_i32(); \
7728 TCGv_i32 t1 = tcg_temp_local_new_i32(); \
7729 TCGv_i32 t2 = tcg_temp_local_new_i32(); \
501e23c4 7730 TCGv_i64 t3 = tcg_temp_local_new_i64(); \
57951c27
AJ
7731 tcg_gen_trunc_i64_i32(t0, cpu_gpr[rA(ctx->opcode)]); \
7732 tcg_gen_trunc_i64_i32(t2, cpu_gpr[rB(ctx->opcode)]); \
7733 tcg_op(t0, t0, t2); \
7734 tcg_gen_shri_i64(t3, cpu_gpr[rA(ctx->opcode)], 32); \
7735 tcg_gen_trunc_i64_i32(t1, t3); \
7736 tcg_gen_shri_i64(t3, cpu_gpr[rB(ctx->opcode)], 32); \
7737 tcg_gen_trunc_i64_i32(t2, t3); \
a7812ae4 7738 tcg_temp_free_i64(t3); \
57951c27 7739 tcg_op(t1, t1, t2); \
a7812ae4 7740 tcg_temp_free_i32(t2); \
57951c27 7741 tcg_gen_concat_i32_i64(cpu_gpr[rD(ctx->opcode)], t0, t1); \
a7812ae4
PB
7742 tcg_temp_free_i32(t0); \
7743 tcg_temp_free_i32(t1); \
0487d6a8 7744}
57951c27
AJ
7745#else
7746#define GEN_SPEOP_ARITH2(name, tcg_op) \
636aa200 7747static inline void gen_##name(DisasContext *ctx) \
0487d6a8
JM
7748{ \
7749 if (unlikely(!ctx->spe_enabled)) { \
27a69bb0 7750 gen_exception(ctx, POWERPC_EXCP_SPEU); \
0487d6a8
JM
7751 return; \
7752 } \
57951c27
AJ
7753 tcg_op(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)], \
7754 cpu_gpr[rB(ctx->opcode)]); \
7755 tcg_op(cpu_gprh[rD(ctx->opcode)], cpu_gprh[rA(ctx->opcode)], \
7756 cpu_gprh[rB(ctx->opcode)]); \
0487d6a8 7757}
57951c27 7758#endif
0487d6a8 7759
636aa200 7760static inline void gen_op_evsrwu(TCGv_i32 ret, TCGv_i32 arg1, TCGv_i32 arg2)
57951c27 7761{
a7812ae4 7762 TCGv_i32 t0;
57951c27 7763 int l1, l2;
0487d6a8 7764
57951c27
AJ
7765 l1 = gen_new_label();
7766 l2 = gen_new_label();
a7812ae4 7767 t0 = tcg_temp_local_new_i32();
57951c27
AJ
7768 /* No error here: 6 bits are used */
7769 tcg_gen_andi_i32(t0, arg2, 0x3F);
7770 tcg_gen_brcondi_i32(TCG_COND_GE, t0, 32, l1);
7771 tcg_gen_shr_i32(ret, arg1, t0);
7772 tcg_gen_br(l2);
7773 gen_set_label(l1);
7774 tcg_gen_movi_i32(ret, 0);
0aef4261 7775 gen_set_label(l2);
a7812ae4 7776 tcg_temp_free_i32(t0);
57951c27
AJ
7777}
7778GEN_SPEOP_ARITH2(evsrwu, gen_op_evsrwu);
636aa200 7779static inline void gen_op_evsrws(TCGv_i32 ret, TCGv_i32 arg1, TCGv_i32 arg2)
57951c27 7780{
a7812ae4 7781 TCGv_i32 t0;
57951c27
AJ
7782 int l1, l2;
7783
7784 l1 = gen_new_label();
7785 l2 = gen_new_label();
a7812ae4 7786 t0 = tcg_temp_local_new_i32();
57951c27
AJ
7787 /* No error here: 6 bits are used */
7788 tcg_gen_andi_i32(t0, arg2, 0x3F);
7789 tcg_gen_brcondi_i32(TCG_COND_GE, t0, 32, l1);
7790 tcg_gen_sar_i32(ret, arg1, t0);
7791 tcg_gen_br(l2);
7792 gen_set_label(l1);
7793 tcg_gen_movi_i32(ret, 0);
0aef4261 7794 gen_set_label(l2);
a7812ae4 7795 tcg_temp_free_i32(t0);
57951c27
AJ
7796}
7797GEN_SPEOP_ARITH2(evsrws, gen_op_evsrws);
636aa200 7798static inline void gen_op_evslw(TCGv_i32 ret, TCGv_i32 arg1, TCGv_i32 arg2)
57951c27 7799{
a7812ae4 7800 TCGv_i32 t0;
57951c27
AJ
7801 int l1, l2;
7802
7803 l1 = gen_new_label();
7804 l2 = gen_new_label();
a7812ae4 7805 t0 = tcg_temp_local_new_i32();
57951c27
AJ
7806 /* No error here: 6 bits are used */
7807 tcg_gen_andi_i32(t0, arg2, 0x3F);
7808 tcg_gen_brcondi_i32(TCG_COND_GE, t0, 32, l1);
7809 tcg_gen_shl_i32(ret, arg1, t0);
7810 tcg_gen_br(l2);
7811 gen_set_label(l1);
7812 tcg_gen_movi_i32(ret, 0);
e29ef9fa 7813 gen_set_label(l2);
a7812ae4 7814 tcg_temp_free_i32(t0);
57951c27
AJ
7815}
7816GEN_SPEOP_ARITH2(evslw, gen_op_evslw);
636aa200 7817static inline void gen_op_evrlw(TCGv_i32 ret, TCGv_i32 arg1, TCGv_i32 arg2)
57951c27 7818{
a7812ae4 7819 TCGv_i32 t0 = tcg_temp_new_i32();
57951c27
AJ
7820 tcg_gen_andi_i32(t0, arg2, 0x1F);
7821 tcg_gen_rotl_i32(ret, arg1, t0);
a7812ae4 7822 tcg_temp_free_i32(t0);
57951c27
AJ
7823}
7824GEN_SPEOP_ARITH2(evrlw, gen_op_evrlw);
636aa200 7825static inline void gen_evmergehi(DisasContext *ctx)
57951c27
AJ
7826{
7827 if (unlikely(!ctx->spe_enabled)) {
27a69bb0 7828 gen_exception(ctx, POWERPC_EXCP_SPEU);
57951c27
AJ
7829 return;
7830 }
7831#if defined(TARGET_PPC64)
a7812ae4
PB
7832 TCGv t0 = tcg_temp_new();
7833 TCGv t1 = tcg_temp_new();
57951c27
AJ
7834 tcg_gen_shri_tl(t0, cpu_gpr[rB(ctx->opcode)], 32);
7835 tcg_gen_andi_tl(t1, cpu_gpr[rA(ctx->opcode)], 0xFFFFFFFF0000000ULL);
7836 tcg_gen_or_tl(cpu_gpr[rD(ctx->opcode)], t0, t1);
7837 tcg_temp_free(t0);
7838 tcg_temp_free(t1);
7839#else
7840 tcg_gen_mov_i32(cpu_gpr[rD(ctx->opcode)], cpu_gprh[rB(ctx->opcode)]);
7841 tcg_gen_mov_i32(cpu_gprh[rD(ctx->opcode)], cpu_gprh[rA(ctx->opcode)]);
7842#endif
7843}
7844GEN_SPEOP_ARITH2(evaddw, tcg_gen_add_i32);
636aa200 7845static inline void gen_op_evsubf(TCGv_i32 ret, TCGv_i32 arg1, TCGv_i32 arg2)
0487d6a8 7846{
57951c27
AJ
7847 tcg_gen_sub_i32(ret, arg2, arg1);
7848}
7849GEN_SPEOP_ARITH2(evsubfw, gen_op_evsubf);
0487d6a8 7850
57951c27
AJ
7851/* SPE arithmetic immediate */
7852#if defined(TARGET_PPC64)
7853#define GEN_SPEOP_ARITH_IMM2(name, tcg_op) \
636aa200 7854static inline void gen_##name(DisasContext *ctx) \
57951c27
AJ
7855{ \
7856 if (unlikely(!ctx->spe_enabled)) { \
27a69bb0 7857 gen_exception(ctx, POWERPC_EXCP_SPEU); \
57951c27
AJ
7858 return; \
7859 } \
a7812ae4
PB
7860 TCGv_i32 t0 = tcg_temp_local_new_i32(); \
7861 TCGv_i32 t1 = tcg_temp_local_new_i32(); \
7862 TCGv_i64 t2 = tcg_temp_local_new_i64(); \
57951c27
AJ
7863 tcg_gen_trunc_i64_i32(t0, cpu_gpr[rB(ctx->opcode)]); \
7864 tcg_op(t0, t0, rA(ctx->opcode)); \
7865 tcg_gen_shri_i64(t2, cpu_gpr[rB(ctx->opcode)], 32); \
7866 tcg_gen_trunc_i64_i32(t1, t2); \
e06fcd75 7867 tcg_temp_free_i64(t2); \
57951c27
AJ
7868 tcg_op(t1, t1, rA(ctx->opcode)); \
7869 tcg_gen_concat_i32_i64(cpu_gpr[rD(ctx->opcode)], t0, t1); \
a7812ae4
PB
7870 tcg_temp_free_i32(t0); \
7871 tcg_temp_free_i32(t1); \
57951c27
AJ
7872}
7873#else
7874#define GEN_SPEOP_ARITH_IMM2(name, tcg_op) \
636aa200 7875static inline void gen_##name(DisasContext *ctx) \
57951c27
AJ
7876{ \
7877 if (unlikely(!ctx->spe_enabled)) { \
27a69bb0 7878 gen_exception(ctx, POWERPC_EXCP_SPEU); \
57951c27
AJ
7879 return; \
7880 } \
7881 tcg_op(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rB(ctx->opcode)], \
7882 rA(ctx->opcode)); \
7883 tcg_op(cpu_gprh[rD(ctx->opcode)], cpu_gprh[rB(ctx->opcode)], \
7884 rA(ctx->opcode)); \
7885}
7886#endif
7887GEN_SPEOP_ARITH_IMM2(evaddiw, tcg_gen_addi_i32);
7888GEN_SPEOP_ARITH_IMM2(evsubifw, tcg_gen_subi_i32);
7889
7890/* SPE comparison */
7891#if defined(TARGET_PPC64)
7892#define GEN_SPEOP_COMP(name, tcg_cond) \
636aa200 7893static inline void gen_##name(DisasContext *ctx) \
57951c27
AJ
7894{ \
7895 if (unlikely(!ctx->spe_enabled)) { \
27a69bb0 7896 gen_exception(ctx, POWERPC_EXCP_SPEU); \
57951c27
AJ
7897 return; \
7898 } \
7899 int l1 = gen_new_label(); \
7900 int l2 = gen_new_label(); \
7901 int l3 = gen_new_label(); \
7902 int l4 = gen_new_label(); \
a7812ae4
PB
7903 TCGv_i32 t0 = tcg_temp_local_new_i32(); \
7904 TCGv_i32 t1 = tcg_temp_local_new_i32(); \
7905 TCGv_i64 t2 = tcg_temp_local_new_i64(); \
57951c27
AJ
7906 tcg_gen_trunc_i64_i32(t0, cpu_gpr[rA(ctx->opcode)]); \
7907 tcg_gen_trunc_i64_i32(t1, cpu_gpr[rB(ctx->opcode)]); \
7908 tcg_gen_brcond_i32(tcg_cond, t0, t1, l1); \
a7812ae4 7909 tcg_gen_movi_i32(cpu_crf[crfD(ctx->opcode)], 0); \
57951c27
AJ
7910 tcg_gen_br(l2); \
7911 gen_set_label(l1); \
7912 tcg_gen_movi_i32(cpu_crf[crfD(ctx->opcode)], \
7913 CRF_CL | CRF_CH_OR_CL | CRF_CH_AND_CL); \
7914 gen_set_label(l2); \
7915 tcg_gen_shri_i64(t2, cpu_gpr[rA(ctx->opcode)], 32); \
7916 tcg_gen_trunc_i64_i32(t0, t2); \
7917 tcg_gen_shri_i64(t2, cpu_gpr[rB(ctx->opcode)], 32); \
7918 tcg_gen_trunc_i64_i32(t1, t2); \
a7812ae4 7919 tcg_temp_free_i64(t2); \
57951c27
AJ
7920 tcg_gen_brcond_i32(tcg_cond, t0, t1, l3); \
7921 tcg_gen_andi_i32(cpu_crf[crfD(ctx->opcode)], cpu_crf[crfD(ctx->opcode)], \
7922 ~(CRF_CH | CRF_CH_AND_CL)); \
7923 tcg_gen_br(l4); \
7924 gen_set_label(l3); \
7925 tcg_gen_ori_i32(cpu_crf[crfD(ctx->opcode)], cpu_crf[crfD(ctx->opcode)], \
7926 CRF_CH | CRF_CH_OR_CL); \
7927 gen_set_label(l4); \
a7812ae4
PB
7928 tcg_temp_free_i32(t0); \
7929 tcg_temp_free_i32(t1); \
57951c27
AJ
7930}
7931#else
7932#define GEN_SPEOP_COMP(name, tcg_cond) \
636aa200 7933static inline void gen_##name(DisasContext *ctx) \
57951c27
AJ
7934{ \
7935 if (unlikely(!ctx->spe_enabled)) { \
27a69bb0 7936 gen_exception(ctx, POWERPC_EXCP_SPEU); \
57951c27
AJ
7937 return; \
7938 } \
7939 int l1 = gen_new_label(); \
7940 int l2 = gen_new_label(); \
7941 int l3 = gen_new_label(); \
7942 int l4 = gen_new_label(); \
7943 \
7944 tcg_gen_brcond_i32(tcg_cond, cpu_gpr[rA(ctx->opcode)], \
7945 cpu_gpr[rB(ctx->opcode)], l1); \
7946 tcg_gen_movi_tl(cpu_crf[crfD(ctx->opcode)], 0); \
7947 tcg_gen_br(l2); \
7948 gen_set_label(l1); \
7949 tcg_gen_movi_i32(cpu_crf[crfD(ctx->opcode)], \
7950 CRF_CL | CRF_CH_OR_CL | CRF_CH_AND_CL); \
7951 gen_set_label(l2); \
7952 tcg_gen_brcond_i32(tcg_cond, cpu_gprh[rA(ctx->opcode)], \
7953 cpu_gprh[rB(ctx->opcode)], l3); \
7954 tcg_gen_andi_i32(cpu_crf[crfD(ctx->opcode)], cpu_crf[crfD(ctx->opcode)], \
7955 ~(CRF_CH | CRF_CH_AND_CL)); \
7956 tcg_gen_br(l4); \
7957 gen_set_label(l3); \
7958 tcg_gen_ori_i32(cpu_crf[crfD(ctx->opcode)], cpu_crf[crfD(ctx->opcode)], \
7959 CRF_CH | CRF_CH_OR_CL); \
7960 gen_set_label(l4); \
7961}
7962#endif
7963GEN_SPEOP_COMP(evcmpgtu, TCG_COND_GTU);
7964GEN_SPEOP_COMP(evcmpgts, TCG_COND_GT);
7965GEN_SPEOP_COMP(evcmpltu, TCG_COND_LTU);
7966GEN_SPEOP_COMP(evcmplts, TCG_COND_LT);
7967GEN_SPEOP_COMP(evcmpeq, TCG_COND_EQ);
7968
7969/* SPE misc */
636aa200 7970static inline void gen_brinc(DisasContext *ctx)
57951c27
AJ
7971{
7972 /* Note: brinc is usable even if SPE is disabled */
a7812ae4
PB
7973 gen_helper_brinc(cpu_gpr[rD(ctx->opcode)],
7974 cpu_gpr[rA(ctx->opcode)], cpu_gpr[rB(ctx->opcode)]);
0487d6a8 7975}
636aa200 7976static inline void gen_evmergelo(DisasContext *ctx)
57951c27
AJ
7977{
7978 if (unlikely(!ctx->spe_enabled)) {
27a69bb0 7979 gen_exception(ctx, POWERPC_EXCP_SPEU);
57951c27
AJ
7980 return;
7981 }
7982#if defined(TARGET_PPC64)
a7812ae4
PB
7983 TCGv t0 = tcg_temp_new();
7984 TCGv t1 = tcg_temp_new();
17d9b3af 7985 tcg_gen_ext32u_tl(t0, cpu_gpr[rB(ctx->opcode)]);
57951c27
AJ
7986 tcg_gen_shli_tl(t1, cpu_gpr[rA(ctx->opcode)], 32);
7987 tcg_gen_or_tl(cpu_gpr[rD(ctx->opcode)], t0, t1);
7988 tcg_temp_free(t0);
7989 tcg_temp_free(t1);
7990#else
57951c27 7991 tcg_gen_mov_i32(cpu_gprh[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)]);
33890b3e 7992 tcg_gen_mov_i32(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rB(ctx->opcode)]);
57951c27
AJ
7993#endif
7994}
636aa200 7995static inline void gen_evmergehilo(DisasContext *ctx)
57951c27
AJ
7996{
7997 if (unlikely(!ctx->spe_enabled)) {
27a69bb0 7998 gen_exception(ctx, POWERPC_EXCP_SPEU);
57951c27
AJ
7999 return;
8000 }
8001#if defined(TARGET_PPC64)
a7812ae4
PB
8002 TCGv t0 = tcg_temp_new();
8003 TCGv t1 = tcg_temp_new();
17d9b3af 8004 tcg_gen_ext32u_tl(t0, cpu_gpr[rB(ctx->opcode)]);
57951c27
AJ
8005 tcg_gen_andi_tl(t1, cpu_gpr[rA(ctx->opcode)], 0xFFFFFFFF0000000ULL);
8006 tcg_gen_or_tl(cpu_gpr[rD(ctx->opcode)], t0, t1);
8007 tcg_temp_free(t0);
8008 tcg_temp_free(t1);
8009#else
8010 tcg_gen_mov_i32(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rB(ctx->opcode)]);
8011 tcg_gen_mov_i32(cpu_gprh[rD(ctx->opcode)], cpu_gprh[rA(ctx->opcode)]);
8012#endif
8013}
636aa200 8014static inline void gen_evmergelohi(DisasContext *ctx)
57951c27
AJ
8015{
8016 if (unlikely(!ctx->spe_enabled)) {
27a69bb0 8017 gen_exception(ctx, POWERPC_EXCP_SPEU);
57951c27
AJ
8018 return;
8019 }
8020#if defined(TARGET_PPC64)
a7812ae4
PB
8021 TCGv t0 = tcg_temp_new();
8022 TCGv t1 = tcg_temp_new();
57951c27
AJ
8023 tcg_gen_shri_tl(t0, cpu_gpr[rB(ctx->opcode)], 32);
8024 tcg_gen_shli_tl(t1, cpu_gpr[rA(ctx->opcode)], 32);
8025 tcg_gen_or_tl(cpu_gpr[rD(ctx->opcode)], t0, t1);
8026 tcg_temp_free(t0);
8027 tcg_temp_free(t1);
8028#else
33890b3e
NF
8029 if (rD(ctx->opcode) == rA(ctx->opcode)) {
8030 TCGv_i32 tmp = tcg_temp_new_i32();
8031 tcg_gen_mov_i32(tmp, cpu_gpr[rA(ctx->opcode)]);
8032 tcg_gen_mov_i32(cpu_gpr[rD(ctx->opcode)], cpu_gprh[rB(ctx->opcode)]);
8033 tcg_gen_mov_i32(cpu_gprh[rD(ctx->opcode)], tmp);
8034 tcg_temp_free_i32(tmp);
8035 } else {
8036 tcg_gen_mov_i32(cpu_gpr[rD(ctx->opcode)], cpu_gprh[rB(ctx->opcode)]);
8037 tcg_gen_mov_i32(cpu_gprh[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)]);
8038 }
57951c27
AJ
8039#endif
8040}
636aa200 8041static inline void gen_evsplati(DisasContext *ctx)
57951c27 8042{
ae01847f 8043 uint64_t imm = ((int32_t)(rA(ctx->opcode) << 27)) >> 27;
0487d6a8 8044
57951c27 8045#if defined(TARGET_PPC64)
38d14952 8046 tcg_gen_movi_tl(cpu_gpr[rD(ctx->opcode)], (imm << 32) | imm);
57951c27
AJ
8047#else
8048 tcg_gen_movi_i32(cpu_gpr[rD(ctx->opcode)], imm);
8049 tcg_gen_movi_i32(cpu_gprh[rD(ctx->opcode)], imm);
8050#endif
8051}
636aa200 8052static inline void gen_evsplatfi(DisasContext *ctx)
0487d6a8 8053{
ae01847f 8054 uint64_t imm = rA(ctx->opcode) << 27;
0487d6a8 8055
57951c27 8056#if defined(TARGET_PPC64)
38d14952 8057 tcg_gen_movi_tl(cpu_gpr[rD(ctx->opcode)], (imm << 32) | imm);
57951c27
AJ
8058#else
8059 tcg_gen_movi_i32(cpu_gpr[rD(ctx->opcode)], imm);
8060 tcg_gen_movi_i32(cpu_gprh[rD(ctx->opcode)], imm);
8061#endif
0487d6a8
JM
8062}
8063
636aa200 8064static inline void gen_evsel(DisasContext *ctx)
57951c27
AJ
8065{
8066 int l1 = gen_new_label();
8067 int l2 = gen_new_label();
8068 int l3 = gen_new_label();
8069 int l4 = gen_new_label();
a7812ae4 8070 TCGv_i32 t0 = tcg_temp_local_new_i32();
57951c27 8071#if defined(TARGET_PPC64)
a7812ae4
PB
8072 TCGv t1 = tcg_temp_local_new();
8073 TCGv t2 = tcg_temp_local_new();
57951c27
AJ
8074#endif
8075 tcg_gen_andi_i32(t0, cpu_crf[ctx->opcode & 0x07], 1 << 3);
8076 tcg_gen_brcondi_i32(TCG_COND_EQ, t0, 0, l1);
8077#if defined(TARGET_PPC64)
8078 tcg_gen_andi_tl(t1, cpu_gpr[rA(ctx->opcode)], 0xFFFFFFFF00000000ULL);
8079#else
8080 tcg_gen_mov_tl(cpu_gprh[rD(ctx->opcode)], cpu_gprh[rA(ctx->opcode)]);
8081#endif
8082 tcg_gen_br(l2);
8083 gen_set_label(l1);
8084#if defined(TARGET_PPC64)
8085 tcg_gen_andi_tl(t1, cpu_gpr[rB(ctx->opcode)], 0xFFFFFFFF00000000ULL);
8086#else
8087 tcg_gen_mov_tl(cpu_gprh[rD(ctx->opcode)], cpu_gprh[rB(ctx->opcode)]);
8088#endif
8089 gen_set_label(l2);
8090 tcg_gen_andi_i32(t0, cpu_crf[ctx->opcode & 0x07], 1 << 2);
8091 tcg_gen_brcondi_i32(TCG_COND_EQ, t0, 0, l3);
8092#if defined(TARGET_PPC64)
17d9b3af 8093 tcg_gen_ext32u_tl(t2, cpu_gpr[rA(ctx->opcode)]);
57951c27
AJ
8094#else
8095 tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)]);
8096#endif
8097 tcg_gen_br(l4);
8098 gen_set_label(l3);
8099#if defined(TARGET_PPC64)
17d9b3af 8100 tcg_gen_ext32u_tl(t2, cpu_gpr[rB(ctx->opcode)]);
57951c27
AJ
8101#else
8102 tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rB(ctx->opcode)]);
8103#endif
8104 gen_set_label(l4);
a7812ae4 8105 tcg_temp_free_i32(t0);
57951c27
AJ
8106#if defined(TARGET_PPC64)
8107 tcg_gen_or_tl(cpu_gpr[rD(ctx->opcode)], t1, t2);
8108 tcg_temp_free(t1);
8109 tcg_temp_free(t2);
8110#endif
8111}
e8eaa2c0
BS
8112
8113static void gen_evsel0(DisasContext *ctx)
57951c27
AJ
8114{
8115 gen_evsel(ctx);
8116}
e8eaa2c0
BS
8117
8118static void gen_evsel1(DisasContext *ctx)
57951c27
AJ
8119{
8120 gen_evsel(ctx);
8121}
e8eaa2c0
BS
8122
8123static void gen_evsel2(DisasContext *ctx)
57951c27
AJ
8124{
8125 gen_evsel(ctx);
8126}
e8eaa2c0
BS
8127
8128static void gen_evsel3(DisasContext *ctx)
57951c27
AJ
8129{
8130 gen_evsel(ctx);
8131}
0487d6a8 8132
a0e13900
FC
8133/* Multiply */
8134
8135static inline void gen_evmwumi(DisasContext *ctx)
8136{
8137 TCGv_i64 t0, t1;
8138
8139 if (unlikely(!ctx->spe_enabled)) {
27a69bb0 8140 gen_exception(ctx, POWERPC_EXCP_SPEU);
a0e13900
FC
8141 return;
8142 }
8143
8144 t0 = tcg_temp_new_i64();
8145 t1 = tcg_temp_new_i64();
8146
8147 /* t0 := rA; t1 := rB */
8148#if defined(TARGET_PPC64)
8149 tcg_gen_ext32u_tl(t0, cpu_gpr[rA(ctx->opcode)]);
8150 tcg_gen_ext32u_tl(t1, cpu_gpr[rB(ctx->opcode)]);
8151#else
8152 tcg_gen_extu_tl_i64(t0, cpu_gpr[rA(ctx->opcode)]);
8153 tcg_gen_extu_tl_i64(t1, cpu_gpr[rB(ctx->opcode)]);
8154#endif
8155
8156 tcg_gen_mul_i64(t0, t0, t1); /* t0 := rA * rB */
8157
8158 gen_store_gpr64(rD(ctx->opcode), t0); /* rD := t0 */
8159
8160 tcg_temp_free_i64(t0);
8161 tcg_temp_free_i64(t1);
8162}
8163
8164static inline void gen_evmwumia(DisasContext *ctx)
8165{
8166 TCGv_i64 tmp;
8167
8168 if (unlikely(!ctx->spe_enabled)) {
27a69bb0 8169 gen_exception(ctx, POWERPC_EXCP_SPEU);
a0e13900
FC
8170 return;
8171 }
8172
8173 gen_evmwumi(ctx); /* rD := rA * rB */
8174
8175 tmp = tcg_temp_new_i64();
8176
8177 /* acc := rD */
8178 gen_load_gpr64(tmp, rD(ctx->opcode));
1328c2bf 8179 tcg_gen_st_i64(tmp, cpu_env, offsetof(CPUPPCState, spe_acc));
a0e13900
FC
8180 tcg_temp_free_i64(tmp);
8181}
8182
8183static inline void gen_evmwumiaa(DisasContext *ctx)
8184{
8185 TCGv_i64 acc;
8186 TCGv_i64 tmp;
8187
8188 if (unlikely(!ctx->spe_enabled)) {
27a69bb0 8189 gen_exception(ctx, POWERPC_EXCP_SPEU);
a0e13900
FC
8190 return;
8191 }
8192
8193 gen_evmwumi(ctx); /* rD := rA * rB */
8194
8195 acc = tcg_temp_new_i64();
8196 tmp = tcg_temp_new_i64();
8197
8198 /* tmp := rD */
8199 gen_load_gpr64(tmp, rD(ctx->opcode));
8200
8201 /* Load acc */
1328c2bf 8202 tcg_gen_ld_i64(acc, cpu_env, offsetof(CPUPPCState, spe_acc));
a0e13900
FC
8203
8204 /* acc := tmp + acc */
8205 tcg_gen_add_i64(acc, acc, tmp);
8206
8207 /* Store acc */
1328c2bf 8208 tcg_gen_st_i64(acc, cpu_env, offsetof(CPUPPCState, spe_acc));
a0e13900
FC
8209
8210 /* rD := acc */
8211 gen_store_gpr64(rD(ctx->opcode), acc);
8212
8213 tcg_temp_free_i64(acc);
8214 tcg_temp_free_i64(tmp);
8215}
8216
8217static inline void gen_evmwsmi(DisasContext *ctx)
8218{
8219 TCGv_i64 t0, t1;
8220
8221 if (unlikely(!ctx->spe_enabled)) {
27a69bb0 8222 gen_exception(ctx, POWERPC_EXCP_SPEU);
a0e13900
FC
8223 return;
8224 }
8225
8226 t0 = tcg_temp_new_i64();
8227 t1 = tcg_temp_new_i64();
8228
8229 /* t0 := rA; t1 := rB */
8230#if defined(TARGET_PPC64)
8231 tcg_gen_ext32s_tl(t0, cpu_gpr[rA(ctx->opcode)]);
8232 tcg_gen_ext32s_tl(t1, cpu_gpr[rB(ctx->opcode)]);
8233#else
8234 tcg_gen_ext_tl_i64(t0, cpu_gpr[rA(ctx->opcode)]);
8235 tcg_gen_ext_tl_i64(t1, cpu_gpr[rB(ctx->opcode)]);
8236#endif
8237
8238 tcg_gen_mul_i64(t0, t0, t1); /* t0 := rA * rB */
8239
8240 gen_store_gpr64(rD(ctx->opcode), t0); /* rD := t0 */
8241
8242 tcg_temp_free_i64(t0);
8243 tcg_temp_free_i64(t1);
8244}
8245
8246static inline void gen_evmwsmia(DisasContext *ctx)
8247{
8248 TCGv_i64 tmp;
8249
8250 gen_evmwsmi(ctx); /* rD := rA * rB */
8251
8252 tmp = tcg_temp_new_i64();
8253
8254 /* acc := rD */
8255 gen_load_gpr64(tmp, rD(ctx->opcode));
1328c2bf 8256 tcg_gen_st_i64(tmp, cpu_env, offsetof(CPUPPCState, spe_acc));
a0e13900
FC
8257
8258 tcg_temp_free_i64(tmp);
8259}
8260
8261static inline void gen_evmwsmiaa(DisasContext *ctx)
8262{
8263 TCGv_i64 acc = tcg_temp_new_i64();
8264 TCGv_i64 tmp = tcg_temp_new_i64();
8265
8266 gen_evmwsmi(ctx); /* rD := rA * rB */
8267
8268 acc = tcg_temp_new_i64();
8269 tmp = tcg_temp_new_i64();
8270
8271 /* tmp := rD */
8272 gen_load_gpr64(tmp, rD(ctx->opcode));
8273
8274 /* Load acc */
1328c2bf 8275 tcg_gen_ld_i64(acc, cpu_env, offsetof(CPUPPCState, spe_acc));
a0e13900
FC
8276
8277 /* acc := tmp + acc */
8278 tcg_gen_add_i64(acc, acc, tmp);
8279
8280 /* Store acc */
1328c2bf 8281 tcg_gen_st_i64(acc, cpu_env, offsetof(CPUPPCState, spe_acc));
a0e13900
FC
8282
8283 /* rD := acc */
8284 gen_store_gpr64(rD(ctx->opcode), acc);
8285
8286 tcg_temp_free_i64(acc);
8287 tcg_temp_free_i64(tmp);
8288}
8289
70560da7
FC
8290GEN_SPE(evaddw, speundef, 0x00, 0x08, 0x00000000, 0xFFFFFFFF, PPC_SPE); ////
8291GEN_SPE(evaddiw, speundef, 0x01, 0x08, 0x00000000, 0xFFFFFFFF, PPC_SPE);
8292GEN_SPE(evsubfw, speundef, 0x02, 0x08, 0x00000000, 0xFFFFFFFF, PPC_SPE); ////
8293GEN_SPE(evsubifw, speundef, 0x03, 0x08, 0x00000000, 0xFFFFFFFF, PPC_SPE);
8294GEN_SPE(evabs, evneg, 0x04, 0x08, 0x0000F800, 0x0000F800, PPC_SPE); ////
8295GEN_SPE(evextsb, evextsh, 0x05, 0x08, 0x0000F800, 0x0000F800, PPC_SPE); ////
8296GEN_SPE(evrndw, evcntlzw, 0x06, 0x08, 0x0000F800, 0x0000F800, PPC_SPE); ////
8297GEN_SPE(evcntlsw, brinc, 0x07, 0x08, 0x0000F800, 0x00000000, PPC_SPE); //
8298GEN_SPE(evmra, speundef, 0x02, 0x13, 0x0000F800, 0xFFFFFFFF, PPC_SPE);
8299GEN_SPE(speundef, evand, 0x08, 0x08, 0xFFFFFFFF, 0x00000000, PPC_SPE); ////
8300GEN_SPE(evandc, speundef, 0x09, 0x08, 0x00000000, 0xFFFFFFFF, PPC_SPE); ////
8301GEN_SPE(evxor, evor, 0x0B, 0x08, 0x00000000, 0x00000000, PPC_SPE); ////
8302GEN_SPE(evnor, eveqv, 0x0C, 0x08, 0x00000000, 0x00000000, PPC_SPE); ////
8303GEN_SPE(evmwumi, evmwsmi, 0x0C, 0x11, 0x00000000, 0x00000000, PPC_SPE);
8304GEN_SPE(evmwumia, evmwsmia, 0x1C, 0x11, 0x00000000, 0x00000000, PPC_SPE);
8305GEN_SPE(evmwumiaa, evmwsmiaa, 0x0C, 0x15, 0x00000000, 0x00000000, PPC_SPE);
8306GEN_SPE(speundef, evorc, 0x0D, 0x08, 0xFFFFFFFF, 0x00000000, PPC_SPE); ////
8307GEN_SPE(evnand, speundef, 0x0F, 0x08, 0x00000000, 0xFFFFFFFF, PPC_SPE); ////
8308GEN_SPE(evsrwu, evsrws, 0x10, 0x08, 0x00000000, 0x00000000, PPC_SPE); ////
8309GEN_SPE(evsrwiu, evsrwis, 0x11, 0x08, 0x00000000, 0x00000000, PPC_SPE);
8310GEN_SPE(evslw, speundef, 0x12, 0x08, 0x00000000, 0xFFFFFFFF, PPC_SPE); ////
8311GEN_SPE(evslwi, speundef, 0x13, 0x08, 0x00000000, 0xFFFFFFFF, PPC_SPE);
8312GEN_SPE(evrlw, evsplati, 0x14, 0x08, 0x00000000, 0x0000F800, PPC_SPE); //
8313GEN_SPE(evrlwi, evsplatfi, 0x15, 0x08, 0x00000000, 0x0000F800, PPC_SPE);
8314GEN_SPE(evmergehi, evmergelo, 0x16, 0x08, 0x00000000, 0x00000000, PPC_SPE); ////
8315GEN_SPE(evmergehilo, evmergelohi, 0x17, 0x08, 0x00000000, 0x00000000, PPC_SPE); ////
8316GEN_SPE(evcmpgtu, evcmpgts, 0x18, 0x08, 0x00600000, 0x00600000, PPC_SPE); ////
8317GEN_SPE(evcmpltu, evcmplts, 0x19, 0x08, 0x00600000, 0x00600000, PPC_SPE); ////
8318GEN_SPE(evcmpeq, speundef, 0x1A, 0x08, 0x00600000, 0xFFFFFFFF, PPC_SPE); ////
0487d6a8 8319
6a6ae23f 8320/* SPE load and stores */
636aa200 8321static inline void gen_addr_spe_imm_index(DisasContext *ctx, TCGv EA, int sh)
6a6ae23f
AJ
8322{
8323 target_ulong uimm = rB(ctx->opcode);
8324
76db3ba4 8325 if (rA(ctx->opcode) == 0) {
6a6ae23f 8326 tcg_gen_movi_tl(EA, uimm << sh);
76db3ba4 8327 } else {
6a6ae23f 8328 tcg_gen_addi_tl(EA, cpu_gpr[rA(ctx->opcode)], uimm << sh);
c791fe84 8329 if (NARROW_MODE(ctx)) {
76db3ba4
AJ
8330 tcg_gen_ext32u_tl(EA, EA);
8331 }
76db3ba4 8332 }
0487d6a8 8333}
6a6ae23f 8334
636aa200 8335static inline void gen_op_evldd(DisasContext *ctx, TCGv addr)
6a6ae23f
AJ
8336{
8337#if defined(TARGET_PPC64)
76db3ba4 8338 gen_qemu_ld64(ctx, cpu_gpr[rD(ctx->opcode)], addr);
6a6ae23f
AJ
8339#else
8340 TCGv_i64 t0 = tcg_temp_new_i64();
76db3ba4 8341 gen_qemu_ld64(ctx, t0, addr);
6a6ae23f
AJ
8342 tcg_gen_trunc_i64_i32(cpu_gpr[rD(ctx->opcode)], t0);
8343 tcg_gen_shri_i64(t0, t0, 32);
8344 tcg_gen_trunc_i64_i32(cpu_gprh[rD(ctx->opcode)], t0);
8345 tcg_temp_free_i64(t0);
8346#endif
0487d6a8 8347}
6a6ae23f 8348
636aa200 8349static inline void gen_op_evldw(DisasContext *ctx, TCGv addr)
6a6ae23f 8350{
0487d6a8 8351#if defined(TARGET_PPC64)
6a6ae23f 8352 TCGv t0 = tcg_temp_new();
76db3ba4 8353 gen_qemu_ld32u(ctx, t0, addr);
6a6ae23f 8354 tcg_gen_shli_tl(cpu_gpr[rD(ctx->opcode)], t0, 32);
76db3ba4
AJ
8355 gen_addr_add(ctx, addr, addr, 4);
8356 gen_qemu_ld32u(ctx, t0, addr);
6a6ae23f
AJ
8357 tcg_gen_or_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rD(ctx->opcode)], t0);
8358 tcg_temp_free(t0);
8359#else
76db3ba4
AJ
8360 gen_qemu_ld32u(ctx, cpu_gprh[rD(ctx->opcode)], addr);
8361 gen_addr_add(ctx, addr, addr, 4);
8362 gen_qemu_ld32u(ctx, cpu_gpr[rD(ctx->opcode)], addr);
6a6ae23f 8363#endif
0487d6a8 8364}
6a6ae23f 8365
636aa200 8366static inline void gen_op_evldh(DisasContext *ctx, TCGv addr)
6a6ae23f
AJ
8367{
8368 TCGv t0 = tcg_temp_new();
8369#if defined(TARGET_PPC64)
76db3ba4 8370 gen_qemu_ld16u(ctx, t0, addr);
6a6ae23f 8371 tcg_gen_shli_tl(cpu_gpr[rD(ctx->opcode)], t0, 48);
76db3ba4
AJ
8372 gen_addr_add(ctx, addr, addr, 2);
8373 gen_qemu_ld16u(ctx, t0, addr);
6a6ae23f
AJ
8374 tcg_gen_shli_tl(t0, t0, 32);
8375 tcg_gen_or_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rD(ctx->opcode)], t0);
76db3ba4
AJ
8376 gen_addr_add(ctx, addr, addr, 2);
8377 gen_qemu_ld16u(ctx, t0, addr);
6a6ae23f
AJ
8378 tcg_gen_shli_tl(t0, t0, 16);
8379 tcg_gen_or_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rD(ctx->opcode)], t0);
76db3ba4
AJ
8380 gen_addr_add(ctx, addr, addr, 2);
8381 gen_qemu_ld16u(ctx, t0, addr);
6a6ae23f 8382 tcg_gen_or_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rD(ctx->opcode)], t0);
0487d6a8 8383#else
76db3ba4 8384 gen_qemu_ld16u(ctx, t0, addr);
6a6ae23f 8385 tcg_gen_shli_tl(cpu_gprh[rD(ctx->opcode)], t0, 16);
76db3ba4
AJ
8386 gen_addr_add(ctx, addr, addr, 2);
8387 gen_qemu_ld16u(ctx, t0, addr);
6a6ae23f 8388 tcg_gen_or_tl(cpu_gprh[rD(ctx->opcode)], cpu_gprh[rD(ctx->opcode)], t0);
76db3ba4
AJ
8389 gen_addr_add(ctx, addr, addr, 2);
8390 gen_qemu_ld16u(ctx, t0, addr);
6a6ae23f 8391 tcg_gen_shli_tl(cpu_gprh[rD(ctx->opcode)], t0, 16);
76db3ba4
AJ
8392 gen_addr_add(ctx, addr, addr, 2);
8393 gen_qemu_ld16u(ctx, t0, addr);
6a6ae23f 8394 tcg_gen_or_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rD(ctx->opcode)], t0);
0487d6a8 8395#endif
6a6ae23f 8396 tcg_temp_free(t0);
0487d6a8
JM
8397}
8398
636aa200 8399static inline void gen_op_evlhhesplat(DisasContext *ctx, TCGv addr)
6a6ae23f
AJ
8400{
8401 TCGv t0 = tcg_temp_new();
76db3ba4 8402 gen_qemu_ld16u(ctx, t0, addr);
6a6ae23f
AJ
8403#if defined(TARGET_PPC64)
8404 tcg_gen_shli_tl(cpu_gpr[rD(ctx->opcode)], t0, 48);
8405 tcg_gen_shli_tl(t0, t0, 16);
8406 tcg_gen_or_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rD(ctx->opcode)], t0);
8407#else
8408 tcg_gen_shli_tl(t0, t0, 16);
8409 tcg_gen_mov_tl(cpu_gprh[rD(ctx->opcode)], t0);
8410 tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], t0);
8411#endif
8412 tcg_temp_free(t0);
0487d6a8
JM
8413}
8414
636aa200 8415static inline void gen_op_evlhhousplat(DisasContext *ctx, TCGv addr)
6a6ae23f
AJ
8416{
8417 TCGv t0 = tcg_temp_new();
76db3ba4 8418 gen_qemu_ld16u(ctx, t0, addr);
6a6ae23f
AJ
8419#if defined(TARGET_PPC64)
8420 tcg_gen_shli_tl(cpu_gpr[rD(ctx->opcode)], t0, 32);
8421 tcg_gen_or_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rD(ctx->opcode)], t0);
8422#else
8423 tcg_gen_mov_tl(cpu_gprh[rD(ctx->opcode)], t0);
8424 tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], t0);
8425#endif
8426 tcg_temp_free(t0);
0487d6a8
JM
8427}
8428
636aa200 8429static inline void gen_op_evlhhossplat(DisasContext *ctx, TCGv addr)
6a6ae23f
AJ
8430{
8431 TCGv t0 = tcg_temp_new();
76db3ba4 8432 gen_qemu_ld16s(ctx, t0, addr);
6a6ae23f
AJ
8433#if defined(TARGET_PPC64)
8434 tcg_gen_shli_tl(cpu_gpr[rD(ctx->opcode)], t0, 32);
8435 tcg_gen_ext32u_tl(t0, t0);
8436 tcg_gen_or_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rD(ctx->opcode)], t0);
8437#else
8438 tcg_gen_mov_tl(cpu_gprh[rD(ctx->opcode)], t0);
8439 tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], t0);
8440#endif
8441 tcg_temp_free(t0);
8442}
8443
636aa200 8444static inline void gen_op_evlwhe(DisasContext *ctx, TCGv addr)
6a6ae23f
AJ
8445{
8446 TCGv t0 = tcg_temp_new();
8447#if defined(TARGET_PPC64)
76db3ba4 8448 gen_qemu_ld16u(ctx, t0, addr);
6a6ae23f 8449 tcg_gen_shli_tl(cpu_gpr[rD(ctx->opcode)], t0, 48);
76db3ba4
AJ
8450 gen_addr_add(ctx, addr, addr, 2);
8451 gen_qemu_ld16u(ctx, t0, addr);
6a6ae23f
AJ
8452 tcg_gen_shli_tl(t0, t0, 16);
8453 tcg_gen_or_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rD(ctx->opcode)], t0);
8454#else
76db3ba4 8455 gen_qemu_ld16u(ctx, t0, addr);
6a6ae23f 8456 tcg_gen_shli_tl(cpu_gprh[rD(ctx->opcode)], t0, 16);
76db3ba4
AJ
8457 gen_addr_add(ctx, addr, addr, 2);
8458 gen_qemu_ld16u(ctx, t0, addr);
6a6ae23f
AJ
8459 tcg_gen_shli_tl(cpu_gpr[rD(ctx->opcode)], t0, 16);
8460#endif
8461 tcg_temp_free(t0);
8462}
8463
636aa200 8464static inline void gen_op_evlwhou(DisasContext *ctx, TCGv addr)
6a6ae23f
AJ
8465{
8466#if defined(TARGET_PPC64)
8467 TCGv t0 = tcg_temp_new();
76db3ba4
AJ
8468 gen_qemu_ld16u(ctx, cpu_gpr[rD(ctx->opcode)], addr);
8469 gen_addr_add(ctx, addr, addr, 2);
8470 gen_qemu_ld16u(ctx, t0, addr);
6a6ae23f
AJ
8471 tcg_gen_shli_tl(t0, t0, 32);
8472 tcg_gen_or_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rD(ctx->opcode)], t0);
8473 tcg_temp_free(t0);
8474#else
76db3ba4
AJ
8475 gen_qemu_ld16u(ctx, cpu_gprh[rD(ctx->opcode)], addr);
8476 gen_addr_add(ctx, addr, addr, 2);
8477 gen_qemu_ld16u(ctx, cpu_gpr[rD(ctx->opcode)], addr);
6a6ae23f
AJ
8478#endif
8479}
8480
636aa200 8481static inline void gen_op_evlwhos(DisasContext *ctx, TCGv addr)
6a6ae23f
AJ
8482{
8483#if defined(TARGET_PPC64)
8484 TCGv t0 = tcg_temp_new();
76db3ba4 8485 gen_qemu_ld16s(ctx, t0, addr);
6a6ae23f 8486 tcg_gen_ext32u_tl(cpu_gpr[rD(ctx->opcode)], t0);
76db3ba4
AJ
8487 gen_addr_add(ctx, addr, addr, 2);
8488 gen_qemu_ld16s(ctx, t0, addr);
6a6ae23f
AJ
8489 tcg_gen_shli_tl(t0, t0, 32);
8490 tcg_gen_or_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rD(ctx->opcode)], t0);
8491 tcg_temp_free(t0);
8492#else
76db3ba4
AJ
8493 gen_qemu_ld16s(ctx, cpu_gprh[rD(ctx->opcode)], addr);
8494 gen_addr_add(ctx, addr, addr, 2);
8495 gen_qemu_ld16s(ctx, cpu_gpr[rD(ctx->opcode)], addr);
6a6ae23f
AJ
8496#endif
8497}
8498
636aa200 8499static inline void gen_op_evlwwsplat(DisasContext *ctx, TCGv addr)
6a6ae23f
AJ
8500{
8501 TCGv t0 = tcg_temp_new();
76db3ba4 8502 gen_qemu_ld32u(ctx, t0, addr);
0487d6a8 8503#if defined(TARGET_PPC64)
6a6ae23f
AJ
8504 tcg_gen_shli_tl(cpu_gpr[rD(ctx->opcode)], t0, 32);
8505 tcg_gen_or_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rD(ctx->opcode)], t0);
8506#else
8507 tcg_gen_mov_tl(cpu_gprh[rD(ctx->opcode)], t0);
8508 tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], t0);
8509#endif
8510 tcg_temp_free(t0);
8511}
8512
636aa200 8513static inline void gen_op_evlwhsplat(DisasContext *ctx, TCGv addr)
6a6ae23f
AJ
8514{
8515 TCGv t0 = tcg_temp_new();
8516#if defined(TARGET_PPC64)
76db3ba4 8517 gen_qemu_ld16u(ctx, t0, addr);
6a6ae23f
AJ
8518 tcg_gen_shli_tl(cpu_gpr[rD(ctx->opcode)], t0, 48);
8519 tcg_gen_shli_tl(t0, t0, 32);
8520 tcg_gen_or_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rD(ctx->opcode)], t0);
76db3ba4
AJ
8521 gen_addr_add(ctx, addr, addr, 2);
8522 gen_qemu_ld16u(ctx, t0, addr);
6a6ae23f
AJ
8523 tcg_gen_or_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rD(ctx->opcode)], t0);
8524 tcg_gen_shli_tl(t0, t0, 16);
8525 tcg_gen_or_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rD(ctx->opcode)], t0);
8526#else
76db3ba4 8527 gen_qemu_ld16u(ctx, t0, addr);
6a6ae23f
AJ
8528 tcg_gen_shli_tl(cpu_gprh[rD(ctx->opcode)], t0, 16);
8529 tcg_gen_or_tl(cpu_gprh[rD(ctx->opcode)], cpu_gprh[rD(ctx->opcode)], t0);
76db3ba4
AJ
8530 gen_addr_add(ctx, addr, addr, 2);
8531 gen_qemu_ld16u(ctx, t0, addr);
6a6ae23f
AJ
8532 tcg_gen_shli_tl(cpu_gpr[rD(ctx->opcode)], t0, 16);
8533 tcg_gen_or_tl(cpu_gpr[rD(ctx->opcode)], cpu_gprh[rD(ctx->opcode)], t0);
0487d6a8 8534#endif
6a6ae23f
AJ
8535 tcg_temp_free(t0);
8536}
8537
636aa200 8538static inline void gen_op_evstdd(DisasContext *ctx, TCGv addr)
6a6ae23f
AJ
8539{
8540#if defined(TARGET_PPC64)
76db3ba4 8541 gen_qemu_st64(ctx, cpu_gpr[rS(ctx->opcode)], addr);
0487d6a8 8542#else
6a6ae23f
AJ
8543 TCGv_i64 t0 = tcg_temp_new_i64();
8544 tcg_gen_concat_i32_i64(t0, cpu_gpr[rS(ctx->opcode)], cpu_gprh[rS(ctx->opcode)]);
76db3ba4 8545 gen_qemu_st64(ctx, t0, addr);
6a6ae23f
AJ
8546 tcg_temp_free_i64(t0);
8547#endif
8548}
8549
636aa200 8550static inline void gen_op_evstdw(DisasContext *ctx, TCGv addr)
6a6ae23f 8551{
0487d6a8 8552#if defined(TARGET_PPC64)
6a6ae23f
AJ
8553 TCGv t0 = tcg_temp_new();
8554 tcg_gen_shri_tl(t0, cpu_gpr[rS(ctx->opcode)], 32);
76db3ba4 8555 gen_qemu_st32(ctx, t0, addr);
6a6ae23f
AJ
8556 tcg_temp_free(t0);
8557#else
76db3ba4 8558 gen_qemu_st32(ctx, cpu_gprh[rS(ctx->opcode)], addr);
6a6ae23f 8559#endif
76db3ba4
AJ
8560 gen_addr_add(ctx, addr, addr, 4);
8561 gen_qemu_st32(ctx, cpu_gpr[rS(ctx->opcode)], addr);
6a6ae23f
AJ
8562}
8563
636aa200 8564static inline void gen_op_evstdh(DisasContext *ctx, TCGv addr)
6a6ae23f
AJ
8565{
8566 TCGv t0 = tcg_temp_new();
8567#if defined(TARGET_PPC64)
8568 tcg_gen_shri_tl(t0, cpu_gpr[rS(ctx->opcode)], 48);
8569#else
8570 tcg_gen_shri_tl(t0, cpu_gprh[rS(ctx->opcode)], 16);
8571#endif
76db3ba4
AJ
8572 gen_qemu_st16(ctx, t0, addr);
8573 gen_addr_add(ctx, addr, addr, 2);
6a6ae23f
AJ
8574#if defined(TARGET_PPC64)
8575 tcg_gen_shri_tl(t0, cpu_gpr[rS(ctx->opcode)], 32);
76db3ba4 8576 gen_qemu_st16(ctx, t0, addr);
6a6ae23f 8577#else
76db3ba4 8578 gen_qemu_st16(ctx, cpu_gprh[rS(ctx->opcode)], addr);
6a6ae23f 8579#endif
76db3ba4 8580 gen_addr_add(ctx, addr, addr, 2);
6a6ae23f 8581 tcg_gen_shri_tl(t0, cpu_gpr[rS(ctx->opcode)], 16);
76db3ba4 8582 gen_qemu_st16(ctx, t0, addr);
6a6ae23f 8583 tcg_temp_free(t0);
76db3ba4
AJ
8584 gen_addr_add(ctx, addr, addr, 2);
8585 gen_qemu_st16(ctx, cpu_gpr[rS(ctx->opcode)], addr);
6a6ae23f
AJ
8586}
8587
636aa200 8588static inline void gen_op_evstwhe(DisasContext *ctx, TCGv addr)
6a6ae23f
AJ
8589{
8590 TCGv t0 = tcg_temp_new();
8591#if defined(TARGET_PPC64)
8592 tcg_gen_shri_tl(t0, cpu_gpr[rS(ctx->opcode)], 48);
8593#else
8594 tcg_gen_shri_tl(t0, cpu_gprh[rS(ctx->opcode)], 16);
8595#endif
76db3ba4
AJ
8596 gen_qemu_st16(ctx, t0, addr);
8597 gen_addr_add(ctx, addr, addr, 2);
6a6ae23f 8598 tcg_gen_shri_tl(t0, cpu_gpr[rS(ctx->opcode)], 16);
76db3ba4 8599 gen_qemu_st16(ctx, t0, addr);
6a6ae23f
AJ
8600 tcg_temp_free(t0);
8601}
8602
636aa200 8603static inline void gen_op_evstwho(DisasContext *ctx, TCGv addr)
6a6ae23f
AJ
8604{
8605#if defined(TARGET_PPC64)
8606 TCGv t0 = tcg_temp_new();
8607 tcg_gen_shri_tl(t0, cpu_gpr[rS(ctx->opcode)], 32);
76db3ba4 8608 gen_qemu_st16(ctx, t0, addr);
6a6ae23f
AJ
8609 tcg_temp_free(t0);
8610#else
76db3ba4 8611 gen_qemu_st16(ctx, cpu_gprh[rS(ctx->opcode)], addr);
6a6ae23f 8612#endif
76db3ba4
AJ
8613 gen_addr_add(ctx, addr, addr, 2);
8614 gen_qemu_st16(ctx, cpu_gpr[rS(ctx->opcode)], addr);
6a6ae23f
AJ
8615}
8616
636aa200 8617static inline void gen_op_evstwwe(DisasContext *ctx, TCGv addr)
6a6ae23f
AJ
8618{
8619#if defined(TARGET_PPC64)
8620 TCGv t0 = tcg_temp_new();
8621 tcg_gen_shri_tl(t0, cpu_gpr[rS(ctx->opcode)], 32);
76db3ba4 8622 gen_qemu_st32(ctx, t0, addr);
6a6ae23f
AJ
8623 tcg_temp_free(t0);
8624#else
76db3ba4 8625 gen_qemu_st32(ctx, cpu_gprh[rS(ctx->opcode)], addr);
6a6ae23f
AJ
8626#endif
8627}
8628
636aa200 8629static inline void gen_op_evstwwo(DisasContext *ctx, TCGv addr)
6a6ae23f 8630{
76db3ba4 8631 gen_qemu_st32(ctx, cpu_gpr[rS(ctx->opcode)], addr);
6a6ae23f
AJ
8632}
8633
8634#define GEN_SPEOP_LDST(name, opc2, sh) \
99e300ef 8635static void glue(gen_, name)(DisasContext *ctx) \
6a6ae23f
AJ
8636{ \
8637 TCGv t0; \
8638 if (unlikely(!ctx->spe_enabled)) { \
27a69bb0 8639 gen_exception(ctx, POWERPC_EXCP_SPEU); \
6a6ae23f
AJ
8640 return; \
8641 } \
76db3ba4 8642 gen_set_access_type(ctx, ACCESS_INT); \
6a6ae23f
AJ
8643 t0 = tcg_temp_new(); \
8644 if (Rc(ctx->opcode)) { \
76db3ba4 8645 gen_addr_spe_imm_index(ctx, t0, sh); \
6a6ae23f 8646 } else { \
76db3ba4 8647 gen_addr_reg_index(ctx, t0); \
6a6ae23f
AJ
8648 } \
8649 gen_op_##name(ctx, t0); \
8650 tcg_temp_free(t0); \
8651}
8652
8653GEN_SPEOP_LDST(evldd, 0x00, 3);
8654GEN_SPEOP_LDST(evldw, 0x01, 3);
8655GEN_SPEOP_LDST(evldh, 0x02, 3);
8656GEN_SPEOP_LDST(evlhhesplat, 0x04, 1);
8657GEN_SPEOP_LDST(evlhhousplat, 0x06, 1);
8658GEN_SPEOP_LDST(evlhhossplat, 0x07, 1);
8659GEN_SPEOP_LDST(evlwhe, 0x08, 2);
8660GEN_SPEOP_LDST(evlwhou, 0x0A, 2);
8661GEN_SPEOP_LDST(evlwhos, 0x0B, 2);
8662GEN_SPEOP_LDST(evlwwsplat, 0x0C, 2);
8663GEN_SPEOP_LDST(evlwhsplat, 0x0E, 2);
8664
8665GEN_SPEOP_LDST(evstdd, 0x10, 3);
8666GEN_SPEOP_LDST(evstdw, 0x11, 3);
8667GEN_SPEOP_LDST(evstdh, 0x12, 3);
8668GEN_SPEOP_LDST(evstwhe, 0x18, 2);
8669GEN_SPEOP_LDST(evstwho, 0x1A, 2);
8670GEN_SPEOP_LDST(evstwwe, 0x1C, 2);
8671GEN_SPEOP_LDST(evstwwo, 0x1E, 2);
0487d6a8
JM
8672
8673/* Multiply and add - TODO */
8674#if 0
70560da7
FC
8675GEN_SPE(speundef, evmhessf, 0x01, 0x10, 0xFFFFFFFF, 0x00000000, PPC_SPE);//
8676GEN_SPE(speundef, evmhossf, 0x03, 0x10, 0xFFFFFFFF, 0x00000000, PPC_SPE);
8677GEN_SPE(evmheumi, evmhesmi, 0x04, 0x10, 0x00000000, 0x00000000, PPC_SPE);
8678GEN_SPE(speundef, evmhesmf, 0x05, 0x10, 0xFFFFFFFF, 0x00000000, PPC_SPE);
8679GEN_SPE(evmhoumi, evmhosmi, 0x06, 0x10, 0x00000000, 0x00000000, PPC_SPE);
8680GEN_SPE(speundef, evmhosmf, 0x07, 0x10, 0xFFFFFFFF, 0x00000000, PPC_SPE);
8681GEN_SPE(speundef, evmhessfa, 0x11, 0x10, 0xFFFFFFFF, 0x00000000, PPC_SPE);
8682GEN_SPE(speundef, evmhossfa, 0x13, 0x10, 0xFFFFFFFF, 0x00000000, PPC_SPE);
8683GEN_SPE(evmheumia, evmhesmia, 0x14, 0x10, 0x00000000, 0x00000000, PPC_SPE);
8684GEN_SPE(speundef, evmhesmfa, 0x15, 0x10, 0xFFFFFFFF, 0x00000000, PPC_SPE);
8685GEN_SPE(evmhoumia, evmhosmia, 0x16, 0x10, 0x00000000, 0x00000000, PPC_SPE);
8686GEN_SPE(speundef, evmhosmfa, 0x17, 0x10, 0xFFFFFFFF, 0x00000000, PPC_SPE);
8687
8688GEN_SPE(speundef, evmwhssf, 0x03, 0x11, 0xFFFFFFFF, 0x00000000, PPC_SPE);
8689GEN_SPE(evmwlumi, speundef, 0x04, 0x11, 0x00000000, 0xFFFFFFFF, PPC_SPE);
8690GEN_SPE(evmwhumi, evmwhsmi, 0x06, 0x11, 0x00000000, 0x00000000, PPC_SPE);
8691GEN_SPE(speundef, evmwhsmf, 0x07, 0x11, 0xFFFFFFFF, 0x00000000, PPC_SPE);
8692GEN_SPE(speundef, evmwssf, 0x09, 0x11, 0xFFFFFFFF, 0x00000000, PPC_SPE);
8693GEN_SPE(speundef, evmwsmf, 0x0D, 0x11, 0xFFFFFFFF, 0x00000000, PPC_SPE);
8694GEN_SPE(speundef, evmwhssfa, 0x13, 0x11, 0xFFFFFFFF, 0x00000000, PPC_SPE);
8695GEN_SPE(evmwlumia, speundef, 0x14, 0x11, 0x00000000, 0xFFFFFFFF, PPC_SPE);
8696GEN_SPE(evmwhumia, evmwhsmia, 0x16, 0x11, 0x00000000, 0x00000000, PPC_SPE);
8697GEN_SPE(speundef, evmwhsmfa, 0x17, 0x11, 0xFFFFFFFF, 0x00000000, PPC_SPE);
8698GEN_SPE(speundef, evmwssfa, 0x19, 0x11, 0xFFFFFFFF, 0x00000000, PPC_SPE);
8699GEN_SPE(speundef, evmwsmfa, 0x1D, 0x11, 0xFFFFFFFF, 0x00000000, PPC_SPE);
8700
8701GEN_SPE(evadduiaaw, evaddsiaaw, 0x00, 0x13, 0x0000F800, 0x0000F800, PPC_SPE);
8702GEN_SPE(evsubfusiaaw, evsubfssiaaw, 0x01, 0x13, 0x0000F800, 0x0000F800, PPC_SPE);
8703GEN_SPE(evaddumiaaw, evaddsmiaaw, 0x04, 0x13, 0x0000F800, 0x0000F800, PPC_SPE);
8704GEN_SPE(evsubfumiaaw, evsubfsmiaaw, 0x05, 0x13, 0x0000F800, 0x0000F800, PPC_SPE);
8705GEN_SPE(evdivws, evdivwu, 0x06, 0x13, 0x00000000, 0x00000000, PPC_SPE);
8706
8707GEN_SPE(evmheusiaaw, evmhessiaaw, 0x00, 0x14, 0x00000000, 0x00000000, PPC_SPE);
8708GEN_SPE(speundef, evmhessfaaw, 0x01, 0x14, 0xFFFFFFFF, 0x00000000, PPC_SPE);
8709GEN_SPE(evmhousiaaw, evmhossiaaw, 0x02, 0x14, 0x00000000, 0x00000000, PPC_SPE);
8710GEN_SPE(speundef, evmhossfaaw, 0x03, 0x14, 0xFFFFFFFF, 0x00000000, PPC_SPE);
8711GEN_SPE(evmheumiaaw, evmhesmiaaw, 0x04, 0x14, 0x00000000, 0x00000000, PPC_SPE);
8712GEN_SPE(speundef, evmhesmfaaw, 0x05, 0x14, 0xFFFFFFFF, 0x00000000, PPC_SPE);
8713GEN_SPE(evmhoumiaaw, evmhosmiaaw, 0x06, 0x14, 0x00000000, 0x00000000, PPC_SPE);
8714GEN_SPE(speundef, evmhosmfaaw, 0x07, 0x14, 0xFFFFFFFF, 0x00000000, PPC_SPE);
8715GEN_SPE(evmhegumiaa, evmhegsmiaa, 0x14, 0x14, 0x00000000, 0x00000000, PPC_SPE);
8716GEN_SPE(speundef, evmhegsmfaa, 0x15, 0x14, 0xFFFFFFFF, 0x00000000, PPC_SPE);
8717GEN_SPE(evmhogumiaa, evmhogsmiaa, 0x16, 0x14, 0x00000000, 0x00000000, PPC_SPE);
8718GEN_SPE(speundef, evmhogsmfaa, 0x17, 0x14, 0xFFFFFFFF, 0x00000000, PPC_SPE);
8719
8720GEN_SPE(evmwlusiaaw, evmwlssiaaw, 0x00, 0x15, 0x00000000, 0x00000000, PPC_SPE);
8721GEN_SPE(evmwlumiaaw, evmwlsmiaaw, 0x04, 0x15, 0x00000000, 0x00000000, PPC_SPE);
8722GEN_SPE(speundef, evmwssfaa, 0x09, 0x15, 0xFFFFFFFF, 0x00000000, PPC_SPE);
8723GEN_SPE(speundef, evmwsmfaa, 0x0D, 0x15, 0xFFFFFFFF, 0x00000000, PPC_SPE);
8724
8725GEN_SPE(evmheusianw, evmhessianw, 0x00, 0x16, 0x00000000, 0x00000000, PPC_SPE);
8726GEN_SPE(speundef, evmhessfanw, 0x01, 0x16, 0xFFFFFFFF, 0x00000000, PPC_SPE);
8727GEN_SPE(evmhousianw, evmhossianw, 0x02, 0x16, 0x00000000, 0x00000000, PPC_SPE);
8728GEN_SPE(speundef, evmhossfanw, 0x03, 0x16, 0xFFFFFFFF, 0x00000000, PPC_SPE);
8729GEN_SPE(evmheumianw, evmhesmianw, 0x04, 0x16, 0x00000000, 0x00000000, PPC_SPE);
8730GEN_SPE(speundef, evmhesmfanw, 0x05, 0x16, 0xFFFFFFFF, 0x00000000, PPC_SPE);
8731GEN_SPE(evmhoumianw, evmhosmianw, 0x06, 0x16, 0x00000000, 0x00000000, PPC_SPE);
8732GEN_SPE(speundef, evmhosmfanw, 0x07, 0x16, 0xFFFFFFFF, 0x00000000, PPC_SPE);
8733GEN_SPE(evmhegumian, evmhegsmian, 0x14, 0x16, 0x00000000, 0x00000000, PPC_SPE);
8734GEN_SPE(speundef, evmhegsmfan, 0x15, 0x16, 0xFFFFFFFF, 0x00000000, PPC_SPE);
8735GEN_SPE(evmhigumian, evmhigsmian, 0x16, 0x16, 0x00000000, 0x00000000, PPC_SPE);
8736GEN_SPE(speundef, evmhogsmfan, 0x17, 0x16, 0xFFFFFFFF, 0x00000000, PPC_SPE);
8737
8738GEN_SPE(evmwlusianw, evmwlssianw, 0x00, 0x17, 0x00000000, 0x00000000, PPC_SPE);
8739GEN_SPE(evmwlumianw, evmwlsmianw, 0x04, 0x17, 0x00000000, 0x00000000, PPC_SPE);
8740GEN_SPE(speundef, evmwssfan, 0x09, 0x17, 0xFFFFFFFF, 0x00000000, PPC_SPE);
8741GEN_SPE(evmwumian, evmwsmian, 0x0C, 0x17, 0x00000000, 0x00000000, PPC_SPE);
8742GEN_SPE(speundef, evmwsmfan, 0x0D, 0x17, 0xFFFFFFFF, 0x00000000, PPC_SPE);
0487d6a8
JM
8743#endif
8744
8745/*** SPE floating-point extension ***/
1c97856d
AJ
8746#if defined(TARGET_PPC64)
8747#define GEN_SPEFPUOP_CONV_32_32(name) \
636aa200 8748static inline void gen_##name(DisasContext *ctx) \
0487d6a8 8749{ \
1c97856d
AJ
8750 TCGv_i32 t0; \
8751 TCGv t1; \
8752 t0 = tcg_temp_new_i32(); \
8753 tcg_gen_trunc_tl_i32(t0, cpu_gpr[rB(ctx->opcode)]); \
8e703949 8754 gen_helper_##name(t0, cpu_env, t0); \
1c97856d
AJ
8755 t1 = tcg_temp_new(); \
8756 tcg_gen_extu_i32_tl(t1, t0); \
8757 tcg_temp_free_i32(t0); \
8758 tcg_gen_andi_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rD(ctx->opcode)], \
8759 0xFFFFFFFF00000000ULL); \
8760 tcg_gen_or_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rD(ctx->opcode)], t1); \
8761 tcg_temp_free(t1); \
0487d6a8 8762}
1c97856d 8763#define GEN_SPEFPUOP_CONV_32_64(name) \
636aa200 8764static inline void gen_##name(DisasContext *ctx) \
1c97856d
AJ
8765{ \
8766 TCGv_i32 t0; \
8767 TCGv t1; \
8768 t0 = tcg_temp_new_i32(); \
8e703949 8769 gen_helper_##name(t0, cpu_env, cpu_gpr[rB(ctx->opcode)]); \
1c97856d
AJ
8770 t1 = tcg_temp_new(); \
8771 tcg_gen_extu_i32_tl(t1, t0); \
8772 tcg_temp_free_i32(t0); \
8773 tcg_gen_andi_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rD(ctx->opcode)], \
8774 0xFFFFFFFF00000000ULL); \
8775 tcg_gen_or_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rD(ctx->opcode)], t1); \
8776 tcg_temp_free(t1); \
8777}
8778#define GEN_SPEFPUOP_CONV_64_32(name) \
636aa200 8779static inline void gen_##name(DisasContext *ctx) \
1c97856d
AJ
8780{ \
8781 TCGv_i32 t0 = tcg_temp_new_i32(); \
8782 tcg_gen_trunc_tl_i32(t0, cpu_gpr[rB(ctx->opcode)]); \
8e703949 8783 gen_helper_##name(cpu_gpr[rD(ctx->opcode)], cpu_env, t0); \
1c97856d
AJ
8784 tcg_temp_free_i32(t0); \
8785}
8786#define GEN_SPEFPUOP_CONV_64_64(name) \
636aa200 8787static inline void gen_##name(DisasContext *ctx) \
1c97856d 8788{ \
8e703949
BS
8789 gen_helper_##name(cpu_gpr[rD(ctx->opcode)], cpu_env, \
8790 cpu_gpr[rB(ctx->opcode)]); \
1c97856d
AJ
8791}
8792#define GEN_SPEFPUOP_ARITH2_32_32(name) \
636aa200 8793static inline void gen_##name(DisasContext *ctx) \
57951c27 8794{ \
1c97856d
AJ
8795 TCGv_i32 t0, t1; \
8796 TCGv_i64 t2; \
57951c27 8797 if (unlikely(!ctx->spe_enabled)) { \
27a69bb0 8798 gen_exception(ctx, POWERPC_EXCP_SPEU); \
57951c27
AJ
8799 return; \
8800 } \
1c97856d
AJ
8801 t0 = tcg_temp_new_i32(); \
8802 t1 = tcg_temp_new_i32(); \
8803 tcg_gen_trunc_tl_i32(t0, cpu_gpr[rA(ctx->opcode)]); \
8804 tcg_gen_trunc_tl_i32(t1, cpu_gpr[rB(ctx->opcode)]); \
8e703949 8805 gen_helper_##name(t0, cpu_env, t0, t1); \
1c97856d
AJ
8806 tcg_temp_free_i32(t1); \
8807 t2 = tcg_temp_new(); \
8808 tcg_gen_extu_i32_tl(t2, t0); \
8809 tcg_temp_free_i32(t0); \
8810 tcg_gen_andi_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rD(ctx->opcode)], \
8811 0xFFFFFFFF00000000ULL); \
8812 tcg_gen_or_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rD(ctx->opcode)], t2); \
8813 tcg_temp_free(t2); \
57951c27 8814}
1c97856d 8815#define GEN_SPEFPUOP_ARITH2_64_64(name) \
636aa200 8816static inline void gen_##name(DisasContext *ctx) \
57951c27
AJ
8817{ \
8818 if (unlikely(!ctx->spe_enabled)) { \
27a69bb0 8819 gen_exception(ctx, POWERPC_EXCP_SPEU); \
57951c27
AJ
8820 return; \
8821 } \
8e703949
BS
8822 gen_helper_##name(cpu_gpr[rD(ctx->opcode)], cpu_env, \
8823 cpu_gpr[rA(ctx->opcode)], cpu_gpr[rB(ctx->opcode)]); \
57951c27 8824}
1c97856d 8825#define GEN_SPEFPUOP_COMP_32(name) \
636aa200 8826static inline void gen_##name(DisasContext *ctx) \
57951c27 8827{ \
1c97856d 8828 TCGv_i32 t0, t1; \
57951c27 8829 if (unlikely(!ctx->spe_enabled)) { \
27a69bb0 8830 gen_exception(ctx, POWERPC_EXCP_SPEU); \
57951c27
AJ
8831 return; \
8832 } \
1c97856d
AJ
8833 t0 = tcg_temp_new_i32(); \
8834 t1 = tcg_temp_new_i32(); \
8835 tcg_gen_trunc_tl_i32(t0, cpu_gpr[rA(ctx->opcode)]); \
8836 tcg_gen_trunc_tl_i32(t1, cpu_gpr[rB(ctx->opcode)]); \
8e703949 8837 gen_helper_##name(cpu_crf[crfD(ctx->opcode)], cpu_env, t0, t1); \
1c97856d
AJ
8838 tcg_temp_free_i32(t0); \
8839 tcg_temp_free_i32(t1); \
8840}
8841#define GEN_SPEFPUOP_COMP_64(name) \
636aa200 8842static inline void gen_##name(DisasContext *ctx) \
1c97856d
AJ
8843{ \
8844 if (unlikely(!ctx->spe_enabled)) { \
27a69bb0 8845 gen_exception(ctx, POWERPC_EXCP_SPEU); \
1c97856d
AJ
8846 return; \
8847 } \
8e703949 8848 gen_helper_##name(cpu_crf[crfD(ctx->opcode)], cpu_env, \
1c97856d
AJ
8849 cpu_gpr[rA(ctx->opcode)], cpu_gpr[rB(ctx->opcode)]); \
8850}
8851#else
8852#define GEN_SPEFPUOP_CONV_32_32(name) \
636aa200 8853static inline void gen_##name(DisasContext *ctx) \
1c97856d 8854{ \
8e703949
BS
8855 gen_helper_##name(cpu_gpr[rD(ctx->opcode)], cpu_env, \
8856 cpu_gpr[rB(ctx->opcode)]); \
57951c27 8857}
1c97856d 8858#define GEN_SPEFPUOP_CONV_32_64(name) \
636aa200 8859static inline void gen_##name(DisasContext *ctx) \
1c97856d
AJ
8860{ \
8861 TCGv_i64 t0 = tcg_temp_new_i64(); \
8862 gen_load_gpr64(t0, rB(ctx->opcode)); \
8e703949 8863 gen_helper_##name(cpu_gpr[rD(ctx->opcode)], cpu_env, t0); \
1c97856d
AJ
8864 tcg_temp_free_i64(t0); \
8865}
8866#define GEN_SPEFPUOP_CONV_64_32(name) \
636aa200 8867static inline void gen_##name(DisasContext *ctx) \
1c97856d
AJ
8868{ \
8869 TCGv_i64 t0 = tcg_temp_new_i64(); \
8e703949 8870 gen_helper_##name(t0, cpu_env, cpu_gpr[rB(ctx->opcode)]); \
1c97856d
AJ
8871 gen_store_gpr64(rD(ctx->opcode), t0); \
8872 tcg_temp_free_i64(t0); \
8873}
8874#define GEN_SPEFPUOP_CONV_64_64(name) \
636aa200 8875static inline void gen_##name(DisasContext *ctx) \
1c97856d
AJ
8876{ \
8877 TCGv_i64 t0 = tcg_temp_new_i64(); \
8878 gen_load_gpr64(t0, rB(ctx->opcode)); \
8e703949 8879 gen_helper_##name(t0, cpu_env, t0); \
1c97856d
AJ
8880 gen_store_gpr64(rD(ctx->opcode), t0); \
8881 tcg_temp_free_i64(t0); \
8882}
8883#define GEN_SPEFPUOP_ARITH2_32_32(name) \
636aa200 8884static inline void gen_##name(DisasContext *ctx) \
1c97856d
AJ
8885{ \
8886 if (unlikely(!ctx->spe_enabled)) { \
27a69bb0 8887 gen_exception(ctx, POWERPC_EXCP_SPEU); \
1c97856d
AJ
8888 return; \
8889 } \
8e703949 8890 gen_helper_##name(cpu_gpr[rD(ctx->opcode)], cpu_env, \
1c97856d
AJ
8891 cpu_gpr[rA(ctx->opcode)], cpu_gpr[rB(ctx->opcode)]); \
8892}
8893#define GEN_SPEFPUOP_ARITH2_64_64(name) \
636aa200 8894static inline void gen_##name(DisasContext *ctx) \
1c97856d
AJ
8895{ \
8896 TCGv_i64 t0, t1; \
8897 if (unlikely(!ctx->spe_enabled)) { \
27a69bb0 8898 gen_exception(ctx, POWERPC_EXCP_SPEU); \
1c97856d
AJ
8899 return; \
8900 } \
8901 t0 = tcg_temp_new_i64(); \
8902 t1 = tcg_temp_new_i64(); \
8903 gen_load_gpr64(t0, rA(ctx->opcode)); \
8904 gen_load_gpr64(t1, rB(ctx->opcode)); \
8e703949 8905 gen_helper_##name(t0, cpu_env, t0, t1); \
1c97856d
AJ
8906 gen_store_gpr64(rD(ctx->opcode), t0); \
8907 tcg_temp_free_i64(t0); \
8908 tcg_temp_free_i64(t1); \
8909}
8910#define GEN_SPEFPUOP_COMP_32(name) \
636aa200 8911static inline void gen_##name(DisasContext *ctx) \
1c97856d
AJ
8912{ \
8913 if (unlikely(!ctx->spe_enabled)) { \
27a69bb0 8914 gen_exception(ctx, POWERPC_EXCP_SPEU); \
1c97856d
AJ
8915 return; \
8916 } \
8e703949 8917 gen_helper_##name(cpu_crf[crfD(ctx->opcode)], cpu_env, \
1c97856d
AJ
8918 cpu_gpr[rA(ctx->opcode)], cpu_gpr[rB(ctx->opcode)]); \
8919}
8920#define GEN_SPEFPUOP_COMP_64(name) \
636aa200 8921static inline void gen_##name(DisasContext *ctx) \
1c97856d
AJ
8922{ \
8923 TCGv_i64 t0, t1; \
8924 if (unlikely(!ctx->spe_enabled)) { \
27a69bb0 8925 gen_exception(ctx, POWERPC_EXCP_SPEU); \
1c97856d
AJ
8926 return; \
8927 } \
8928 t0 = tcg_temp_new_i64(); \
8929 t1 = tcg_temp_new_i64(); \
8930 gen_load_gpr64(t0, rA(ctx->opcode)); \
8931 gen_load_gpr64(t1, rB(ctx->opcode)); \
8e703949 8932 gen_helper_##name(cpu_crf[crfD(ctx->opcode)], cpu_env, t0, t1); \
1c97856d
AJ
8933 tcg_temp_free_i64(t0); \
8934 tcg_temp_free_i64(t1); \
8935}
8936#endif
57951c27 8937
0487d6a8
JM
8938/* Single precision floating-point vectors operations */
8939/* Arithmetic */
1c97856d
AJ
8940GEN_SPEFPUOP_ARITH2_64_64(evfsadd);
8941GEN_SPEFPUOP_ARITH2_64_64(evfssub);
8942GEN_SPEFPUOP_ARITH2_64_64(evfsmul);
8943GEN_SPEFPUOP_ARITH2_64_64(evfsdiv);
636aa200 8944static inline void gen_evfsabs(DisasContext *ctx)
1c97856d
AJ
8945{
8946 if (unlikely(!ctx->spe_enabled)) {
27a69bb0 8947 gen_exception(ctx, POWERPC_EXCP_SPEU);
1c97856d
AJ
8948 return;
8949 }
8950#if defined(TARGET_PPC64)
6d5c34fa 8951 tcg_gen_andi_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)], ~0x8000000080000000LL);
1c97856d 8952#else
6d5c34fa
MP
8953 tcg_gen_andi_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)], ~0x80000000);
8954 tcg_gen_andi_tl(cpu_gprh[rD(ctx->opcode)], cpu_gprh[rA(ctx->opcode)], ~0x80000000);
1c97856d
AJ
8955#endif
8956}
636aa200 8957static inline void gen_evfsnabs(DisasContext *ctx)
1c97856d
AJ
8958{
8959 if (unlikely(!ctx->spe_enabled)) {
27a69bb0 8960 gen_exception(ctx, POWERPC_EXCP_SPEU);
1c97856d
AJ
8961 return;
8962 }
8963#if defined(TARGET_PPC64)
6d5c34fa 8964 tcg_gen_ori_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)], 0x8000000080000000LL);
1c97856d 8965#else
6d5c34fa
MP
8966 tcg_gen_ori_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)], 0x80000000);
8967 tcg_gen_ori_tl(cpu_gprh[rD(ctx->opcode)], cpu_gprh[rA(ctx->opcode)], 0x80000000);
1c97856d
AJ
8968#endif
8969}
636aa200 8970static inline void gen_evfsneg(DisasContext *ctx)
1c97856d
AJ
8971{
8972 if (unlikely(!ctx->spe_enabled)) {
27a69bb0 8973 gen_exception(ctx, POWERPC_EXCP_SPEU);
1c97856d
AJ
8974 return;
8975 }
8976#if defined(TARGET_PPC64)
6d5c34fa 8977 tcg_gen_xori_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)], 0x8000000080000000LL);
1c97856d 8978#else
6d5c34fa
MP
8979 tcg_gen_xori_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)], 0x80000000);
8980 tcg_gen_xori_tl(cpu_gprh[rD(ctx->opcode)], cpu_gprh[rA(ctx->opcode)], 0x80000000);
1c97856d
AJ
8981#endif
8982}
8983
0487d6a8 8984/* Conversion */
1c97856d
AJ
8985GEN_SPEFPUOP_CONV_64_64(evfscfui);
8986GEN_SPEFPUOP_CONV_64_64(evfscfsi);
8987GEN_SPEFPUOP_CONV_64_64(evfscfuf);
8988GEN_SPEFPUOP_CONV_64_64(evfscfsf);
8989GEN_SPEFPUOP_CONV_64_64(evfsctui);
8990GEN_SPEFPUOP_CONV_64_64(evfsctsi);
8991GEN_SPEFPUOP_CONV_64_64(evfsctuf);
8992GEN_SPEFPUOP_CONV_64_64(evfsctsf);
8993GEN_SPEFPUOP_CONV_64_64(evfsctuiz);
8994GEN_SPEFPUOP_CONV_64_64(evfsctsiz);
8995
0487d6a8 8996/* Comparison */
1c97856d
AJ
8997GEN_SPEFPUOP_COMP_64(evfscmpgt);
8998GEN_SPEFPUOP_COMP_64(evfscmplt);
8999GEN_SPEFPUOP_COMP_64(evfscmpeq);
9000GEN_SPEFPUOP_COMP_64(evfststgt);
9001GEN_SPEFPUOP_COMP_64(evfststlt);
9002GEN_SPEFPUOP_COMP_64(evfststeq);
0487d6a8
JM
9003
9004/* Opcodes definitions */
70560da7
FC
9005GEN_SPE(evfsadd, evfssub, 0x00, 0x0A, 0x00000000, 0x00000000, PPC_SPE_SINGLE); //
9006GEN_SPE(evfsabs, evfsnabs, 0x02, 0x0A, 0x0000F800, 0x0000F800, PPC_SPE_SINGLE); //
9007GEN_SPE(evfsneg, speundef, 0x03, 0x0A, 0x0000F800, 0xFFFFFFFF, PPC_SPE_SINGLE); //
9008GEN_SPE(evfsmul, evfsdiv, 0x04, 0x0A, 0x00000000, 0x00000000, PPC_SPE_SINGLE); //
9009GEN_SPE(evfscmpgt, evfscmplt, 0x06, 0x0A, 0x00600000, 0x00600000, PPC_SPE_SINGLE); //
9010GEN_SPE(evfscmpeq, speundef, 0x07, 0x0A, 0x00600000, 0xFFFFFFFF, PPC_SPE_SINGLE); //
9011GEN_SPE(evfscfui, evfscfsi, 0x08, 0x0A, 0x00180000, 0x00180000, PPC_SPE_SINGLE); //
9012GEN_SPE(evfscfuf, evfscfsf, 0x09, 0x0A, 0x00180000, 0x00180000, PPC_SPE_SINGLE); //
9013GEN_SPE(evfsctui, evfsctsi, 0x0A, 0x0A, 0x00180000, 0x00180000, PPC_SPE_SINGLE); //
9014GEN_SPE(evfsctuf, evfsctsf, 0x0B, 0x0A, 0x00180000, 0x00180000, PPC_SPE_SINGLE); //
9015GEN_SPE(evfsctuiz, speundef, 0x0C, 0x0A, 0x00180000, 0xFFFFFFFF, PPC_SPE_SINGLE); //
9016GEN_SPE(evfsctsiz, speundef, 0x0D, 0x0A, 0x00180000, 0xFFFFFFFF, PPC_SPE_SINGLE); //
9017GEN_SPE(evfststgt, evfststlt, 0x0E, 0x0A, 0x00600000, 0x00600000, PPC_SPE_SINGLE); //
9018GEN_SPE(evfststeq, speundef, 0x0F, 0x0A, 0x00600000, 0xFFFFFFFF, PPC_SPE_SINGLE); //
0487d6a8
JM
9019
9020/* Single precision floating-point operations */
9021/* Arithmetic */
1c97856d
AJ
9022GEN_SPEFPUOP_ARITH2_32_32(efsadd);
9023GEN_SPEFPUOP_ARITH2_32_32(efssub);
9024GEN_SPEFPUOP_ARITH2_32_32(efsmul);
9025GEN_SPEFPUOP_ARITH2_32_32(efsdiv);
636aa200 9026static inline void gen_efsabs(DisasContext *ctx)
1c97856d
AJ
9027{
9028 if (unlikely(!ctx->spe_enabled)) {
27a69bb0 9029 gen_exception(ctx, POWERPC_EXCP_SPEU);
1c97856d
AJ
9030 return;
9031 }
6d5c34fa 9032 tcg_gen_andi_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)], (target_long)~0x80000000LL);
1c97856d 9033}
636aa200 9034static inline void gen_efsnabs(DisasContext *ctx)
1c97856d
AJ
9035{
9036 if (unlikely(!ctx->spe_enabled)) {
27a69bb0 9037 gen_exception(ctx, POWERPC_EXCP_SPEU);
1c97856d
AJ
9038 return;
9039 }
6d5c34fa 9040 tcg_gen_ori_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)], 0x80000000);
1c97856d 9041}
636aa200 9042static inline void gen_efsneg(DisasContext *ctx)
1c97856d
AJ
9043{
9044 if (unlikely(!ctx->spe_enabled)) {
27a69bb0 9045 gen_exception(ctx, POWERPC_EXCP_SPEU);
1c97856d
AJ
9046 return;
9047 }
6d5c34fa 9048 tcg_gen_xori_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)], 0x80000000);
1c97856d
AJ
9049}
9050
0487d6a8 9051/* Conversion */
1c97856d
AJ
9052GEN_SPEFPUOP_CONV_32_32(efscfui);
9053GEN_SPEFPUOP_CONV_32_32(efscfsi);
9054GEN_SPEFPUOP_CONV_32_32(efscfuf);
9055GEN_SPEFPUOP_CONV_32_32(efscfsf);
9056GEN_SPEFPUOP_CONV_32_32(efsctui);
9057GEN_SPEFPUOP_CONV_32_32(efsctsi);
9058GEN_SPEFPUOP_CONV_32_32(efsctuf);
9059GEN_SPEFPUOP_CONV_32_32(efsctsf);
9060GEN_SPEFPUOP_CONV_32_32(efsctuiz);
9061GEN_SPEFPUOP_CONV_32_32(efsctsiz);
9062GEN_SPEFPUOP_CONV_32_64(efscfd);
9063
0487d6a8 9064/* Comparison */
1c97856d
AJ
9065GEN_SPEFPUOP_COMP_32(efscmpgt);
9066GEN_SPEFPUOP_COMP_32(efscmplt);
9067GEN_SPEFPUOP_COMP_32(efscmpeq);
9068GEN_SPEFPUOP_COMP_32(efststgt);
9069GEN_SPEFPUOP_COMP_32(efststlt);
9070GEN_SPEFPUOP_COMP_32(efststeq);
0487d6a8
JM
9071
9072/* Opcodes definitions */
70560da7
FC
9073GEN_SPE(efsadd, efssub, 0x00, 0x0B, 0x00000000, 0x00000000, PPC_SPE_SINGLE); //
9074GEN_SPE(efsabs, efsnabs, 0x02, 0x0B, 0x0000F800, 0x0000F800, PPC_SPE_SINGLE); //
9075GEN_SPE(efsneg, speundef, 0x03, 0x0B, 0x0000F800, 0xFFFFFFFF, PPC_SPE_SINGLE); //
9076GEN_SPE(efsmul, efsdiv, 0x04, 0x0B, 0x00000000, 0x00000000, PPC_SPE_SINGLE); //
9077GEN_SPE(efscmpgt, efscmplt, 0x06, 0x0B, 0x00600000, 0x00600000, PPC_SPE_SINGLE); //
9078GEN_SPE(efscmpeq, efscfd, 0x07, 0x0B, 0x00600000, 0x00180000, PPC_SPE_SINGLE); //
9079GEN_SPE(efscfui, efscfsi, 0x08, 0x0B, 0x00180000, 0x00180000, PPC_SPE_SINGLE); //
9080GEN_SPE(efscfuf, efscfsf, 0x09, 0x0B, 0x00180000, 0x00180000, PPC_SPE_SINGLE); //
9081GEN_SPE(efsctui, efsctsi, 0x0A, 0x0B, 0x00180000, 0x00180000, PPC_SPE_SINGLE); //
9082GEN_SPE(efsctuf, efsctsf, 0x0B, 0x0B, 0x00180000, 0x00180000, PPC_SPE_SINGLE); //
9083GEN_SPE(efsctuiz, speundef, 0x0C, 0x0B, 0x00180000, 0xFFFFFFFF, PPC_SPE_SINGLE); //
9084GEN_SPE(efsctsiz, speundef, 0x0D, 0x0B, 0x00180000, 0xFFFFFFFF, PPC_SPE_SINGLE); //
9085GEN_SPE(efststgt, efststlt, 0x0E, 0x0B, 0x00600000, 0x00600000, PPC_SPE_SINGLE); //
9086GEN_SPE(efststeq, speundef, 0x0F, 0x0B, 0x00600000, 0xFFFFFFFF, PPC_SPE_SINGLE); //
0487d6a8
JM
9087
9088/* Double precision floating-point operations */
9089/* Arithmetic */
1c97856d
AJ
9090GEN_SPEFPUOP_ARITH2_64_64(efdadd);
9091GEN_SPEFPUOP_ARITH2_64_64(efdsub);
9092GEN_SPEFPUOP_ARITH2_64_64(efdmul);
9093GEN_SPEFPUOP_ARITH2_64_64(efddiv);
636aa200 9094static inline void gen_efdabs(DisasContext *ctx)
1c97856d
AJ
9095{
9096 if (unlikely(!ctx->spe_enabled)) {
27a69bb0 9097 gen_exception(ctx, POWERPC_EXCP_SPEU);
1c97856d
AJ
9098 return;
9099 }
9100#if defined(TARGET_PPC64)
6d5c34fa 9101 tcg_gen_andi_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)], ~0x8000000000000000LL);
1c97856d 9102#else
6d5c34fa
MP
9103 tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)]);
9104 tcg_gen_andi_tl(cpu_gprh[rD(ctx->opcode)], cpu_gprh[rA(ctx->opcode)], ~0x80000000);
1c97856d
AJ
9105#endif
9106}
636aa200 9107static inline void gen_efdnabs(DisasContext *ctx)
1c97856d
AJ
9108{
9109 if (unlikely(!ctx->spe_enabled)) {
27a69bb0 9110 gen_exception(ctx, POWERPC_EXCP_SPEU);
1c97856d
AJ
9111 return;
9112 }
9113#if defined(TARGET_PPC64)
6d5c34fa 9114 tcg_gen_ori_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)], 0x8000000000000000LL);
1c97856d 9115#else
6d5c34fa
MP
9116 tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)]);
9117 tcg_gen_ori_tl(cpu_gprh[rD(ctx->opcode)], cpu_gprh[rA(ctx->opcode)], 0x80000000);
1c97856d
AJ
9118#endif
9119}
636aa200 9120static inline void gen_efdneg(DisasContext *ctx)
1c97856d
AJ
9121{
9122 if (unlikely(!ctx->spe_enabled)) {
27a69bb0 9123 gen_exception(ctx, POWERPC_EXCP_SPEU);
1c97856d
AJ
9124 return;
9125 }
9126#if defined(TARGET_PPC64)
6d5c34fa 9127 tcg_gen_xori_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)], 0x8000000000000000LL);
1c97856d 9128#else
6d5c34fa
MP
9129 tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)]);
9130 tcg_gen_xori_tl(cpu_gprh[rD(ctx->opcode)], cpu_gprh[rA(ctx->opcode)], 0x80000000);
1c97856d
AJ
9131#endif
9132}
9133
0487d6a8 9134/* Conversion */
1c97856d
AJ
9135GEN_SPEFPUOP_CONV_64_32(efdcfui);
9136GEN_SPEFPUOP_CONV_64_32(efdcfsi);
9137GEN_SPEFPUOP_CONV_64_32(efdcfuf);
9138GEN_SPEFPUOP_CONV_64_32(efdcfsf);
9139GEN_SPEFPUOP_CONV_32_64(efdctui);
9140GEN_SPEFPUOP_CONV_32_64(efdctsi);
9141GEN_SPEFPUOP_CONV_32_64(efdctuf);
9142GEN_SPEFPUOP_CONV_32_64(efdctsf);
9143GEN_SPEFPUOP_CONV_32_64(efdctuiz);
9144GEN_SPEFPUOP_CONV_32_64(efdctsiz);
9145GEN_SPEFPUOP_CONV_64_32(efdcfs);
9146GEN_SPEFPUOP_CONV_64_64(efdcfuid);
9147GEN_SPEFPUOP_CONV_64_64(efdcfsid);
9148GEN_SPEFPUOP_CONV_64_64(efdctuidz);
9149GEN_SPEFPUOP_CONV_64_64(efdctsidz);
0487d6a8 9150
0487d6a8 9151/* Comparison */
1c97856d
AJ
9152GEN_SPEFPUOP_COMP_64(efdcmpgt);
9153GEN_SPEFPUOP_COMP_64(efdcmplt);
9154GEN_SPEFPUOP_COMP_64(efdcmpeq);
9155GEN_SPEFPUOP_COMP_64(efdtstgt);
9156GEN_SPEFPUOP_COMP_64(efdtstlt);
9157GEN_SPEFPUOP_COMP_64(efdtsteq);
0487d6a8
JM
9158
9159/* Opcodes definitions */
70560da7
FC
9160GEN_SPE(efdadd, efdsub, 0x10, 0x0B, 0x00000000, 0x00000000, PPC_SPE_DOUBLE); //
9161GEN_SPE(efdcfuid, efdcfsid, 0x11, 0x0B, 0x00180000, 0x00180000, PPC_SPE_DOUBLE); //
9162GEN_SPE(efdabs, efdnabs, 0x12, 0x0B, 0x0000F800, 0x0000F800, PPC_SPE_DOUBLE); //
9163GEN_SPE(efdneg, speundef, 0x13, 0x0B, 0x0000F800, 0xFFFFFFFF, PPC_SPE_DOUBLE); //
9164GEN_SPE(efdmul, efddiv, 0x14, 0x0B, 0x00000000, 0x00000000, PPC_SPE_DOUBLE); //
9165GEN_SPE(efdctuidz, efdctsidz, 0x15, 0x0B, 0x00180000, 0x00180000, PPC_SPE_DOUBLE); //
9166GEN_SPE(efdcmpgt, efdcmplt, 0x16, 0x0B, 0x00600000, 0x00600000, PPC_SPE_DOUBLE); //
9167GEN_SPE(efdcmpeq, efdcfs, 0x17, 0x0B, 0x00600000, 0x00180000, PPC_SPE_DOUBLE); //
9168GEN_SPE(efdcfui, efdcfsi, 0x18, 0x0B, 0x00180000, 0x00180000, PPC_SPE_DOUBLE); //
9169GEN_SPE(efdcfuf, efdcfsf, 0x19, 0x0B, 0x00180000, 0x00180000, PPC_SPE_DOUBLE); //
9170GEN_SPE(efdctui, efdctsi, 0x1A, 0x0B, 0x00180000, 0x00180000, PPC_SPE_DOUBLE); //
9171GEN_SPE(efdctuf, efdctsf, 0x1B, 0x0B, 0x00180000, 0x00180000, PPC_SPE_DOUBLE); //
9172GEN_SPE(efdctuiz, speundef, 0x1C, 0x0B, 0x00180000, 0xFFFFFFFF, PPC_SPE_DOUBLE); //
9173GEN_SPE(efdctsiz, speundef, 0x1D, 0x0B, 0x00180000, 0xFFFFFFFF, PPC_SPE_DOUBLE); //
9174GEN_SPE(efdtstgt, efdtstlt, 0x1E, 0x0B, 0x00600000, 0x00600000, PPC_SPE_DOUBLE); //
9175GEN_SPE(efdtsteq, speundef, 0x1F, 0x0B, 0x00600000, 0xFFFFFFFF, PPC_SPE_DOUBLE); //
0487d6a8 9176
c227f099 9177static opcode_t opcodes[] = {
5c55ff99
BS
9178GEN_HANDLER(invalid, 0x00, 0x00, 0x00, 0xFFFFFFFF, PPC_NONE),
9179GEN_HANDLER(cmp, 0x1F, 0x00, 0x00, 0x00400000, PPC_INTEGER),
9180GEN_HANDLER(cmpi, 0x0B, 0xFF, 0xFF, 0x00400000, PPC_INTEGER),
9181GEN_HANDLER(cmpl, 0x1F, 0x00, 0x01, 0x00400000, PPC_INTEGER),
9182GEN_HANDLER(cmpli, 0x0A, 0xFF, 0xFF, 0x00400000, PPC_INTEGER),
fcfda20f 9183GEN_HANDLER_E(cmpb, 0x1F, 0x1C, 0x0F, 0x00000001, PPC_NONE, PPC2_ISA205),
5c55ff99
BS
9184GEN_HANDLER(isel, 0x1F, 0x0F, 0xFF, 0x00000001, PPC_ISEL),
9185GEN_HANDLER(addi, 0x0E, 0xFF, 0xFF, 0x00000000, PPC_INTEGER),
9186GEN_HANDLER(addic, 0x0C, 0xFF, 0xFF, 0x00000000, PPC_INTEGER),
9187GEN_HANDLER2(addic_, "addic.", 0x0D, 0xFF, 0xFF, 0x00000000, PPC_INTEGER),
9188GEN_HANDLER(addis, 0x0F, 0xFF, 0xFF, 0x00000000, PPC_INTEGER),
9189GEN_HANDLER(mulhw, 0x1F, 0x0B, 0x02, 0x00000400, PPC_INTEGER),
9190GEN_HANDLER(mulhwu, 0x1F, 0x0B, 0x00, 0x00000400, PPC_INTEGER),
9191GEN_HANDLER(mullw, 0x1F, 0x0B, 0x07, 0x00000000, PPC_INTEGER),
9192GEN_HANDLER(mullwo, 0x1F, 0x0B, 0x17, 0x00000000, PPC_INTEGER),
9193GEN_HANDLER(mulli, 0x07, 0xFF, 0xFF, 0x00000000, PPC_INTEGER),
9194#if defined(TARGET_PPC64)
9195GEN_HANDLER(mulld, 0x1F, 0x09, 0x07, 0x00000000, PPC_64B),
9196#endif
9197GEN_HANDLER(neg, 0x1F, 0x08, 0x03, 0x0000F800, PPC_INTEGER),
9198GEN_HANDLER(nego, 0x1F, 0x08, 0x13, 0x0000F800, PPC_INTEGER),
9199GEN_HANDLER(subfic, 0x08, 0xFF, 0xFF, 0x00000000, PPC_INTEGER),
9200GEN_HANDLER2(andi_, "andi.", 0x1C, 0xFF, 0xFF, 0x00000000, PPC_INTEGER),
9201GEN_HANDLER2(andis_, "andis.", 0x1D, 0xFF, 0xFF, 0x00000000, PPC_INTEGER),
9202GEN_HANDLER(cntlzw, 0x1F, 0x1A, 0x00, 0x00000000, PPC_INTEGER),
9203GEN_HANDLER(or, 0x1F, 0x1C, 0x0D, 0x00000000, PPC_INTEGER),
9204GEN_HANDLER(xor, 0x1F, 0x1C, 0x09, 0x00000000, PPC_INTEGER),
9205GEN_HANDLER(ori, 0x18, 0xFF, 0xFF, 0x00000000, PPC_INTEGER),
9206GEN_HANDLER(oris, 0x19, 0xFF, 0xFF, 0x00000000, PPC_INTEGER),
9207GEN_HANDLER(xori, 0x1A, 0xFF, 0xFF, 0x00000000, PPC_INTEGER),
9208GEN_HANDLER(xoris, 0x1B, 0xFF, 0xFF, 0x00000000, PPC_INTEGER),
9209GEN_HANDLER(popcntb, 0x1F, 0x03, 0x03, 0x0000F801, PPC_POPCNTB),
eaabeef2 9210GEN_HANDLER(popcntw, 0x1F, 0x1A, 0x0b, 0x0000F801, PPC_POPCNTWD),
725bcec2 9211GEN_HANDLER_E(prtyw, 0x1F, 0x1A, 0x04, 0x0000F801, PPC_NONE, PPC2_ISA205),
5c55ff99 9212#if defined(TARGET_PPC64)
eaabeef2 9213GEN_HANDLER(popcntd, 0x1F, 0x1A, 0x0F, 0x0000F801, PPC_POPCNTWD),
5c55ff99 9214GEN_HANDLER(cntlzd, 0x1F, 0x1A, 0x01, 0x00000000, PPC_64B),
725bcec2 9215GEN_HANDLER_E(prtyd, 0x1F, 0x1A, 0x05, 0x0000F801, PPC_NONE, PPC2_ISA205),
5c55ff99
BS
9216#endif
9217GEN_HANDLER(rlwimi, 0x14, 0xFF, 0xFF, 0x00000000, PPC_INTEGER),
9218GEN_HANDLER(rlwinm, 0x15, 0xFF, 0xFF, 0x00000000, PPC_INTEGER),
9219GEN_HANDLER(rlwnm, 0x17, 0xFF, 0xFF, 0x00000000, PPC_INTEGER),
9220GEN_HANDLER(slw, 0x1F, 0x18, 0x00, 0x00000000, PPC_INTEGER),
9221GEN_HANDLER(sraw, 0x1F, 0x18, 0x18, 0x00000000, PPC_INTEGER),
9222GEN_HANDLER(srawi, 0x1F, 0x18, 0x19, 0x00000000, PPC_INTEGER),
9223GEN_HANDLER(srw, 0x1F, 0x18, 0x10, 0x00000000, PPC_INTEGER),
9224#if defined(TARGET_PPC64)
9225GEN_HANDLER(sld, 0x1F, 0x1B, 0x00, 0x00000000, PPC_64B),
9226GEN_HANDLER(srad, 0x1F, 0x1A, 0x18, 0x00000000, PPC_64B),
9227GEN_HANDLER2(sradi0, "sradi", 0x1F, 0x1A, 0x19, 0x00000000, PPC_64B),
9228GEN_HANDLER2(sradi1, "sradi", 0x1F, 0x1B, 0x19, 0x00000000, PPC_64B),
9229GEN_HANDLER(srd, 0x1F, 0x1B, 0x10, 0x00000000, PPC_64B),
9230#endif
9231GEN_HANDLER(frsqrtes, 0x3B, 0x1A, 0xFF, 0x001F07C0, PPC_FLOAT_FRSQRTES),
9232GEN_HANDLER(fsqrt, 0x3F, 0x16, 0xFF, 0x001F07C0, PPC_FLOAT_FSQRT),
9233GEN_HANDLER(fsqrts, 0x3B, 0x16, 0xFF, 0x001F07C0, PPC_FLOAT_FSQRT),
9234GEN_HANDLER(fcmpo, 0x3F, 0x00, 0x01, 0x00600001, PPC_FLOAT),
9235GEN_HANDLER(fcmpu, 0x3F, 0x00, 0x00, 0x00600001, PPC_FLOAT),
bf45a2e6 9236GEN_HANDLER(fabs, 0x3F, 0x08, 0x08, 0x001F0000, PPC_FLOAT),
5c55ff99 9237GEN_HANDLER(fmr, 0x3F, 0x08, 0x02, 0x001F0000, PPC_FLOAT),
bf45a2e6
AJ
9238GEN_HANDLER(fnabs, 0x3F, 0x08, 0x04, 0x001F0000, PPC_FLOAT),
9239GEN_HANDLER(fneg, 0x3F, 0x08, 0x01, 0x001F0000, PPC_FLOAT),
f0332888 9240GEN_HANDLER_E(fcpsgn, 0x3F, 0x08, 0x00, 0x00000000, PPC_NONE, PPC2_ISA205),
5c55ff99
BS
9241GEN_HANDLER(mcrfs, 0x3F, 0x00, 0x02, 0x0063F801, PPC_FLOAT),
9242GEN_HANDLER(mffs, 0x3F, 0x07, 0x12, 0x001FF800, PPC_FLOAT),
9243GEN_HANDLER(mtfsb0, 0x3F, 0x06, 0x02, 0x001FF800, PPC_FLOAT),
9244GEN_HANDLER(mtfsb1, 0x3F, 0x06, 0x01, 0x001FF800, PPC_FLOAT),
7d08d856
AJ
9245GEN_HANDLER(mtfsf, 0x3F, 0x07, 0x16, 0x00000000, PPC_FLOAT),
9246GEN_HANDLER(mtfsfi, 0x3F, 0x06, 0x04, 0x006e0800, PPC_FLOAT),
5c55ff99
BS
9247#if defined(TARGET_PPC64)
9248GEN_HANDLER(ld, 0x3A, 0xFF, 0xFF, 0x00000000, PPC_64B),
9249GEN_HANDLER(lq, 0x38, 0xFF, 0xFF, 0x00000000, PPC_64BX),
9250GEN_HANDLER(std, 0x3E, 0xFF, 0xFF, 0x00000000, PPC_64B),
9251#endif
9252GEN_HANDLER(lmw, 0x2E, 0xFF, 0xFF, 0x00000000, PPC_INTEGER),
9253GEN_HANDLER(stmw, 0x2F, 0xFF, 0xFF, 0x00000000, PPC_INTEGER),
9254GEN_HANDLER(lswi, 0x1F, 0x15, 0x12, 0x00000001, PPC_STRING),
9255GEN_HANDLER(lswx, 0x1F, 0x15, 0x10, 0x00000001, PPC_STRING),
9256GEN_HANDLER(stswi, 0x1F, 0x15, 0x16, 0x00000001, PPC_STRING),
9257GEN_HANDLER(stswx, 0x1F, 0x15, 0x14, 0x00000001, PPC_STRING),
9258GEN_HANDLER(eieio, 0x1F, 0x16, 0x1A, 0x03FFF801, PPC_MEM_EIEIO),
9259GEN_HANDLER(isync, 0x13, 0x16, 0x04, 0x03FFF801, PPC_MEM),
f844c817 9260GEN_HANDLER(lwarx, 0x1F, 0x14, 0x00, 0x00000000, PPC_RES),
5c55ff99
BS
9261GEN_HANDLER2(stwcx_, "stwcx.", 0x1F, 0x16, 0x04, 0x00000000, PPC_RES),
9262#if defined(TARGET_PPC64)
f844c817 9263GEN_HANDLER(ldarx, 0x1F, 0x14, 0x02, 0x00000000, PPC_64B),
5c55ff99
BS
9264GEN_HANDLER2(stdcx_, "stdcx.", 0x1F, 0x16, 0x06, 0x00000000, PPC_64B),
9265#endif
9266GEN_HANDLER(sync, 0x1F, 0x16, 0x12, 0x039FF801, PPC_MEM_SYNC),
9267GEN_HANDLER(wait, 0x1F, 0x1E, 0x01, 0x03FFF801, PPC_WAIT),
9268GEN_HANDLER(b, 0x12, 0xFF, 0xFF, 0x00000000, PPC_FLOW),
9269GEN_HANDLER(bc, 0x10, 0xFF, 0xFF, 0x00000000, PPC_FLOW),
9270GEN_HANDLER(bcctr, 0x13, 0x10, 0x10, 0x00000000, PPC_FLOW),
9271GEN_HANDLER(bclr, 0x13, 0x10, 0x00, 0x00000000, PPC_FLOW),
9272GEN_HANDLER(mcrf, 0x13, 0x00, 0xFF, 0x00000001, PPC_INTEGER),
9273GEN_HANDLER(rfi, 0x13, 0x12, 0x01, 0x03FF8001, PPC_FLOW),
9274#if defined(TARGET_PPC64)
9275GEN_HANDLER(rfid, 0x13, 0x12, 0x00, 0x03FF8001, PPC_64B),
9276GEN_HANDLER(hrfid, 0x13, 0x12, 0x08, 0x03FF8001, PPC_64H),
9277#endif
9278GEN_HANDLER(sc, 0x11, 0xFF, 0xFF, 0x03FFF01D, PPC_FLOW),
9279GEN_HANDLER(tw, 0x1F, 0x04, 0x00, 0x00000001, PPC_FLOW),
9280GEN_HANDLER(twi, 0x03, 0xFF, 0xFF, 0x00000000, PPC_FLOW),
9281#if defined(TARGET_PPC64)
9282GEN_HANDLER(td, 0x1F, 0x04, 0x02, 0x00000001, PPC_64B),
9283GEN_HANDLER(tdi, 0x02, 0xFF, 0xFF, 0x00000000, PPC_64B),
9284#endif
9285GEN_HANDLER(mcrxr, 0x1F, 0x00, 0x10, 0x007FF801, PPC_MISC),
9286GEN_HANDLER(mfcr, 0x1F, 0x13, 0x00, 0x00000801, PPC_MISC),
9287GEN_HANDLER(mfmsr, 0x1F, 0x13, 0x02, 0x001FF801, PPC_MISC),
9288GEN_HANDLER(mfspr, 0x1F, 0x13, 0x0A, 0x00000001, PPC_MISC),
9289GEN_HANDLER(mftb, 0x1F, 0x13, 0x0B, 0x00000001, PPC_MFTB),
9290GEN_HANDLER(mtcrf, 0x1F, 0x10, 0x04, 0x00000801, PPC_MISC),
9291#if defined(TARGET_PPC64)
9292GEN_HANDLER(mtmsrd, 0x1F, 0x12, 0x05, 0x001EF801, PPC_64B),
9293#endif
9294GEN_HANDLER(mtmsr, 0x1F, 0x12, 0x04, 0x001FF801, PPC_MISC),
9295GEN_HANDLER(mtspr, 0x1F, 0x13, 0x0E, 0x00000001, PPC_MISC),
9296GEN_HANDLER(dcbf, 0x1F, 0x16, 0x02, 0x03C00001, PPC_CACHE),
9297GEN_HANDLER(dcbi, 0x1F, 0x16, 0x0E, 0x03E00001, PPC_CACHE),
9298GEN_HANDLER(dcbst, 0x1F, 0x16, 0x01, 0x03E00001, PPC_CACHE),
9299GEN_HANDLER(dcbt, 0x1F, 0x16, 0x08, 0x02000001, PPC_CACHE),
9300GEN_HANDLER(dcbtst, 0x1F, 0x16, 0x07, 0x02000001, PPC_CACHE),
8e33944f 9301GEN_HANDLER(dcbz, 0x1F, 0x16, 0x1F, 0x03C00001, PPC_CACHE_DCBZ),
5c55ff99
BS
9302GEN_HANDLER(dst, 0x1F, 0x16, 0x0A, 0x01800001, PPC_ALTIVEC),
9303GEN_HANDLER(dstst, 0x1F, 0x16, 0x0B, 0x02000001, PPC_ALTIVEC),
9304GEN_HANDLER(dss, 0x1F, 0x16, 0x19, 0x019FF801, PPC_ALTIVEC),
9305GEN_HANDLER(icbi, 0x1F, 0x16, 0x1E, 0x03E00001, PPC_CACHE_ICBI),
9306GEN_HANDLER(dcba, 0x1F, 0x16, 0x17, 0x03E00001, PPC_CACHE_DCBA),
9307GEN_HANDLER(mfsr, 0x1F, 0x13, 0x12, 0x0010F801, PPC_SEGMENT),
9308GEN_HANDLER(mfsrin, 0x1F, 0x13, 0x14, 0x001F0001, PPC_SEGMENT),
9309GEN_HANDLER(mtsr, 0x1F, 0x12, 0x06, 0x0010F801, PPC_SEGMENT),
9310GEN_HANDLER(mtsrin, 0x1F, 0x12, 0x07, 0x001F0001, PPC_SEGMENT),
9311#if defined(TARGET_PPC64)
9312GEN_HANDLER2(mfsr_64b, "mfsr", 0x1F, 0x13, 0x12, 0x0010F801, PPC_SEGMENT_64B),
9313GEN_HANDLER2(mfsrin_64b, "mfsrin", 0x1F, 0x13, 0x14, 0x001F0001,
9314 PPC_SEGMENT_64B),
9315GEN_HANDLER2(mtsr_64b, "mtsr", 0x1F, 0x12, 0x06, 0x0010F801, PPC_SEGMENT_64B),
9316GEN_HANDLER2(mtsrin_64b, "mtsrin", 0x1F, 0x12, 0x07, 0x001F0001,
9317 PPC_SEGMENT_64B),
efdef95f
DG
9318GEN_HANDLER2(slbmte, "slbmte", 0x1F, 0x12, 0x0C, 0x001F0001, PPC_SEGMENT_64B),
9319GEN_HANDLER2(slbmfee, "slbmfee", 0x1F, 0x13, 0x1C, 0x001F0001, PPC_SEGMENT_64B),
9320GEN_HANDLER2(slbmfev, "slbmfev", 0x1F, 0x13, 0x1A, 0x001F0001, PPC_SEGMENT_64B),
5c55ff99
BS
9321#endif
9322GEN_HANDLER(tlbia, 0x1F, 0x12, 0x0B, 0x03FFFC01, PPC_MEM_TLBIA),
9323GEN_HANDLER(tlbiel, 0x1F, 0x12, 0x08, 0x03FF0001, PPC_MEM_TLBIE),
9324GEN_HANDLER(tlbie, 0x1F, 0x12, 0x09, 0x03FF0001, PPC_MEM_TLBIE),
9325GEN_HANDLER(tlbsync, 0x1F, 0x16, 0x11, 0x03FFF801, PPC_MEM_TLBSYNC),
9326#if defined(TARGET_PPC64)
9327GEN_HANDLER(slbia, 0x1F, 0x12, 0x0F, 0x03FFFC01, PPC_SLBI),
9328GEN_HANDLER(slbie, 0x1F, 0x12, 0x0D, 0x03FF0001, PPC_SLBI),
9329#endif
9330GEN_HANDLER(eciwx, 0x1F, 0x16, 0x0D, 0x00000001, PPC_EXTERN),
9331GEN_HANDLER(ecowx, 0x1F, 0x16, 0x09, 0x00000001, PPC_EXTERN),
9332GEN_HANDLER(abs, 0x1F, 0x08, 0x0B, 0x0000F800, PPC_POWER_BR),
9333GEN_HANDLER(abso, 0x1F, 0x08, 0x1B, 0x0000F800, PPC_POWER_BR),
9334GEN_HANDLER(clcs, 0x1F, 0x10, 0x13, 0x0000F800, PPC_POWER_BR),
9335GEN_HANDLER(div, 0x1F, 0x0B, 0x0A, 0x00000000, PPC_POWER_BR),
9336GEN_HANDLER(divo, 0x1F, 0x0B, 0x1A, 0x00000000, PPC_POWER_BR),
9337GEN_HANDLER(divs, 0x1F, 0x0B, 0x0B, 0x00000000, PPC_POWER_BR),
9338GEN_HANDLER(divso, 0x1F, 0x0B, 0x1B, 0x00000000, PPC_POWER_BR),
9339GEN_HANDLER(doz, 0x1F, 0x08, 0x08, 0x00000000, PPC_POWER_BR),
9340GEN_HANDLER(dozo, 0x1F, 0x08, 0x18, 0x00000000, PPC_POWER_BR),
9341GEN_HANDLER(dozi, 0x09, 0xFF, 0xFF, 0x00000000, PPC_POWER_BR),
9342GEN_HANDLER(lscbx, 0x1F, 0x15, 0x08, 0x00000000, PPC_POWER_BR),
9343GEN_HANDLER(maskg, 0x1F, 0x1D, 0x00, 0x00000000, PPC_POWER_BR),
9344GEN_HANDLER(maskir, 0x1F, 0x1D, 0x10, 0x00000000, PPC_POWER_BR),
9345GEN_HANDLER(mul, 0x1F, 0x0B, 0x03, 0x00000000, PPC_POWER_BR),
9346GEN_HANDLER(mulo, 0x1F, 0x0B, 0x13, 0x00000000, PPC_POWER_BR),
9347GEN_HANDLER(nabs, 0x1F, 0x08, 0x0F, 0x00000000, PPC_POWER_BR),
9348GEN_HANDLER(nabso, 0x1F, 0x08, 0x1F, 0x00000000, PPC_POWER_BR),
9349GEN_HANDLER(rlmi, 0x16, 0xFF, 0xFF, 0x00000000, PPC_POWER_BR),
9350GEN_HANDLER(rrib, 0x1F, 0x19, 0x10, 0x00000000, PPC_POWER_BR),
9351GEN_HANDLER(sle, 0x1F, 0x19, 0x04, 0x00000000, PPC_POWER_BR),
9352GEN_HANDLER(sleq, 0x1F, 0x19, 0x06, 0x00000000, PPC_POWER_BR),
9353GEN_HANDLER(sliq, 0x1F, 0x18, 0x05, 0x00000000, PPC_POWER_BR),
9354GEN_HANDLER(slliq, 0x1F, 0x18, 0x07, 0x00000000, PPC_POWER_BR),
9355GEN_HANDLER(sllq, 0x1F, 0x18, 0x06, 0x00000000, PPC_POWER_BR),
9356GEN_HANDLER(slq, 0x1F, 0x18, 0x04, 0x00000000, PPC_POWER_BR),
9357GEN_HANDLER(sraiq, 0x1F, 0x18, 0x1D, 0x00000000, PPC_POWER_BR),
9358GEN_HANDLER(sraq, 0x1F, 0x18, 0x1C, 0x00000000, PPC_POWER_BR),
9359GEN_HANDLER(sre, 0x1F, 0x19, 0x14, 0x00000000, PPC_POWER_BR),
9360GEN_HANDLER(srea, 0x1F, 0x19, 0x1C, 0x00000000, PPC_POWER_BR),
9361GEN_HANDLER(sreq, 0x1F, 0x19, 0x16, 0x00000000, PPC_POWER_BR),
9362GEN_HANDLER(sriq, 0x1F, 0x18, 0x15, 0x00000000, PPC_POWER_BR),
9363GEN_HANDLER(srliq, 0x1F, 0x18, 0x17, 0x00000000, PPC_POWER_BR),
9364GEN_HANDLER(srlq, 0x1F, 0x18, 0x16, 0x00000000, PPC_POWER_BR),
9365GEN_HANDLER(srq, 0x1F, 0x18, 0x14, 0x00000000, PPC_POWER_BR),
9366GEN_HANDLER(dsa, 0x1F, 0x14, 0x13, 0x03FFF801, PPC_602_SPEC),
9367GEN_HANDLER(esa, 0x1F, 0x14, 0x12, 0x03FFF801, PPC_602_SPEC),
9368GEN_HANDLER(mfrom, 0x1F, 0x09, 0x08, 0x03E0F801, PPC_602_SPEC),
9369GEN_HANDLER2(tlbld_6xx, "tlbld", 0x1F, 0x12, 0x1E, 0x03FF0001, PPC_6xx_TLB),
9370GEN_HANDLER2(tlbli_6xx, "tlbli", 0x1F, 0x12, 0x1F, 0x03FF0001, PPC_6xx_TLB),
9371GEN_HANDLER2(tlbld_74xx, "tlbld", 0x1F, 0x12, 0x1E, 0x03FF0001, PPC_74xx_TLB),
9372GEN_HANDLER2(tlbli_74xx, "tlbli", 0x1F, 0x12, 0x1F, 0x03FF0001, PPC_74xx_TLB),
9373GEN_HANDLER(clf, 0x1F, 0x16, 0x03, 0x03E00000, PPC_POWER),
9374GEN_HANDLER(cli, 0x1F, 0x16, 0x0F, 0x03E00000, PPC_POWER),
9375GEN_HANDLER(dclst, 0x1F, 0x16, 0x13, 0x03E00000, PPC_POWER),
9376GEN_HANDLER(mfsri, 0x1F, 0x13, 0x13, 0x00000001, PPC_POWER),
9377GEN_HANDLER(rac, 0x1F, 0x12, 0x19, 0x00000001, PPC_POWER),
9378GEN_HANDLER(rfsvc, 0x13, 0x12, 0x02, 0x03FFF0001, PPC_POWER),
9379GEN_HANDLER(lfq, 0x38, 0xFF, 0xFF, 0x00000003, PPC_POWER2),
9380GEN_HANDLER(lfqu, 0x39, 0xFF, 0xFF, 0x00000003, PPC_POWER2),
9381GEN_HANDLER(lfqux, 0x1F, 0x17, 0x19, 0x00000001, PPC_POWER2),
9382GEN_HANDLER(lfqx, 0x1F, 0x17, 0x18, 0x00000001, PPC_POWER2),
9383GEN_HANDLER(stfq, 0x3C, 0xFF, 0xFF, 0x00000003, PPC_POWER2),
9384GEN_HANDLER(stfqu, 0x3D, 0xFF, 0xFF, 0x00000003, PPC_POWER2),
9385GEN_HANDLER(stfqux, 0x1F, 0x17, 0x1D, 0x00000001, PPC_POWER2),
9386GEN_HANDLER(stfqx, 0x1F, 0x17, 0x1C, 0x00000001, PPC_POWER2),
9387GEN_HANDLER(mfapidi, 0x1F, 0x13, 0x08, 0x0000F801, PPC_MFAPIDI),
9388GEN_HANDLER(tlbiva, 0x1F, 0x12, 0x18, 0x03FFF801, PPC_TLBIVA),
9389GEN_HANDLER(mfdcr, 0x1F, 0x03, 0x0A, 0x00000001, PPC_DCR),
9390GEN_HANDLER(mtdcr, 0x1F, 0x03, 0x0E, 0x00000001, PPC_DCR),
9391GEN_HANDLER(mfdcrx, 0x1F, 0x03, 0x08, 0x00000000, PPC_DCRX),
9392GEN_HANDLER(mtdcrx, 0x1F, 0x03, 0x0C, 0x00000000, PPC_DCRX),
9393GEN_HANDLER(mfdcrux, 0x1F, 0x03, 0x09, 0x00000000, PPC_DCRUX),
9394GEN_HANDLER(mtdcrux, 0x1F, 0x03, 0x0D, 0x00000000, PPC_DCRUX),
9395GEN_HANDLER(dccci, 0x1F, 0x06, 0x0E, 0x03E00001, PPC_4xx_COMMON),
9396GEN_HANDLER(dcread, 0x1F, 0x06, 0x0F, 0x00000001, PPC_4xx_COMMON),
9397GEN_HANDLER2(icbt_40x, "icbt", 0x1F, 0x06, 0x08, 0x03E00001, PPC_40x_ICBT),
9398GEN_HANDLER(iccci, 0x1F, 0x06, 0x1E, 0x00000001, PPC_4xx_COMMON),
9399GEN_HANDLER(icread, 0x1F, 0x06, 0x1F, 0x03E00001, PPC_4xx_COMMON),
9400GEN_HANDLER2(rfci_40x, "rfci", 0x13, 0x13, 0x01, 0x03FF8001, PPC_40x_EXCP),
01662f3e 9401GEN_HANDLER_E(rfci, 0x13, 0x13, 0x01, 0x03FF8001, PPC_BOOKE, PPC2_BOOKE206),
5c55ff99
BS
9402GEN_HANDLER(rfdi, 0x13, 0x07, 0x01, 0x03FF8001, PPC_RFDI),
9403GEN_HANDLER(rfmci, 0x13, 0x06, 0x01, 0x03FF8001, PPC_RFMCI),
9404GEN_HANDLER2(tlbre_40x, "tlbre", 0x1F, 0x12, 0x1D, 0x00000001, PPC_40x_TLB),
9405GEN_HANDLER2(tlbsx_40x, "tlbsx", 0x1F, 0x12, 0x1C, 0x00000000, PPC_40x_TLB),
9406GEN_HANDLER2(tlbwe_40x, "tlbwe", 0x1F, 0x12, 0x1E, 0x00000001, PPC_40x_TLB),
9407GEN_HANDLER2(tlbre_440, "tlbre", 0x1F, 0x12, 0x1D, 0x00000001, PPC_BOOKE),
9408GEN_HANDLER2(tlbsx_440, "tlbsx", 0x1F, 0x12, 0x1C, 0x00000000, PPC_BOOKE),
9409GEN_HANDLER2(tlbwe_440, "tlbwe", 0x1F, 0x12, 0x1E, 0x00000001, PPC_BOOKE),
01662f3e
AG
9410GEN_HANDLER2_E(tlbre_booke206, "tlbre", 0x1F, 0x12, 0x1D, 0x00000001,
9411 PPC_NONE, PPC2_BOOKE206),
9412GEN_HANDLER2_E(tlbsx_booke206, "tlbsx", 0x1F, 0x12, 0x1C, 0x00000000,
9413 PPC_NONE, PPC2_BOOKE206),
9414GEN_HANDLER2_E(tlbwe_booke206, "tlbwe", 0x1F, 0x12, 0x1E, 0x00000001,
9415 PPC_NONE, PPC2_BOOKE206),
9416GEN_HANDLER2_E(tlbivax_booke206, "tlbivax", 0x1F, 0x12, 0x18, 0x00000001,
9417 PPC_NONE, PPC2_BOOKE206),
6d3db821
AG
9418GEN_HANDLER2_E(tlbilx_booke206, "tlbilx", 0x1F, 0x12, 0x00, 0x03800001,
9419 PPC_NONE, PPC2_BOOKE206),
d5d11a39
AG
9420GEN_HANDLER2_E(msgsnd, "msgsnd", 0x1F, 0x0E, 0x06, 0x03ff0001,
9421 PPC_NONE, PPC2_PRCNTL),
9e0b5cb1
AG
9422GEN_HANDLER2_E(msgclr, "msgclr", 0x1F, 0x0E, 0x07, 0x03ff0001,
9423 PPC_NONE, PPC2_PRCNTL),
5c55ff99 9424GEN_HANDLER(wrtee, 0x1F, 0x03, 0x04, 0x000FFC01, PPC_WRTEE),
fbe73008 9425GEN_HANDLER(wrteei, 0x1F, 0x03, 0x05, 0x000E7C01, PPC_WRTEE),
5c55ff99 9426GEN_HANDLER(dlmzb, 0x1F, 0x0E, 0x02, 0x00000000, PPC_440_SPEC),
01662f3e
AG
9427GEN_HANDLER_E(mbar, 0x1F, 0x16, 0x1a, 0x001FF801,
9428 PPC_BOOKE, PPC2_BOOKE206),
dcb2b9e1 9429GEN_HANDLER(msync_4xx, 0x1F, 0x16, 0x12, 0x03FFF801, PPC_BOOKE),
01662f3e
AG
9430GEN_HANDLER2_E(icbt_440, "icbt", 0x1F, 0x16, 0x00, 0x03E00001,
9431 PPC_BOOKE, PPC2_BOOKE206),
5c55ff99
BS
9432GEN_HANDLER(lvsl, 0x1f, 0x06, 0x00, 0x00000001, PPC_ALTIVEC),
9433GEN_HANDLER(lvsr, 0x1f, 0x06, 0x01, 0x00000001, PPC_ALTIVEC),
9434GEN_HANDLER(mfvscr, 0x04, 0x2, 0x18, 0x001ff800, PPC_ALTIVEC),
9435GEN_HANDLER(mtvscr, 0x04, 0x2, 0x19, 0x03ff0000, PPC_ALTIVEC),
9436GEN_HANDLER(vsldoi, 0x04, 0x16, 0xFF, 0x00000400, PPC_ALTIVEC),
9437GEN_HANDLER(vmladduhm, 0x04, 0x11, 0xFF, 0x00000000, PPC_ALTIVEC),
9438GEN_HANDLER2(evsel0, "evsel", 0x04, 0x1c, 0x09, 0x00000000, PPC_SPE),
9439GEN_HANDLER2(evsel1, "evsel", 0x04, 0x1d, 0x09, 0x00000000, PPC_SPE),
9440GEN_HANDLER2(evsel2, "evsel", 0x04, 0x1e, 0x09, 0x00000000, PPC_SPE),
9441GEN_HANDLER2(evsel3, "evsel", 0x04, 0x1f, 0x09, 0x00000000, PPC_SPE),
9442
9443#undef GEN_INT_ARITH_ADD
9444#undef GEN_INT_ARITH_ADD_CONST
9445#define GEN_INT_ARITH_ADD(name, opc3, add_ca, compute_ca, compute_ov) \
9446GEN_HANDLER(name, 0x1F, 0x0A, opc3, 0x00000000, PPC_INTEGER),
9447#define GEN_INT_ARITH_ADD_CONST(name, opc3, const_val, \
9448 add_ca, compute_ca, compute_ov) \
9449GEN_HANDLER(name, 0x1F, 0x0A, opc3, 0x0000F800, PPC_INTEGER),
9450GEN_INT_ARITH_ADD(add, 0x08, 0, 0, 0)
9451GEN_INT_ARITH_ADD(addo, 0x18, 0, 0, 1)
9452GEN_INT_ARITH_ADD(addc, 0x00, 0, 1, 0)
9453GEN_INT_ARITH_ADD(addco, 0x10, 0, 1, 1)
9454GEN_INT_ARITH_ADD(adde, 0x04, 1, 1, 0)
9455GEN_INT_ARITH_ADD(addeo, 0x14, 1, 1, 1)
9456GEN_INT_ARITH_ADD_CONST(addme, 0x07, -1LL, 1, 1, 0)
9457GEN_INT_ARITH_ADD_CONST(addmeo, 0x17, -1LL, 1, 1, 1)
9458GEN_INT_ARITH_ADD_CONST(addze, 0x06, 0, 1, 1, 0)
9459GEN_INT_ARITH_ADD_CONST(addzeo, 0x16, 0, 1, 1, 1)
9460
9461#undef GEN_INT_ARITH_DIVW
9462#define GEN_INT_ARITH_DIVW(name, opc3, sign, compute_ov) \
9463GEN_HANDLER(name, 0x1F, 0x0B, opc3, 0x00000000, PPC_INTEGER)
9464GEN_INT_ARITH_DIVW(divwu, 0x0E, 0, 0),
9465GEN_INT_ARITH_DIVW(divwuo, 0x1E, 0, 1),
9466GEN_INT_ARITH_DIVW(divw, 0x0F, 1, 0),
9467GEN_INT_ARITH_DIVW(divwo, 0x1F, 1, 1),
9468
9469#if defined(TARGET_PPC64)
9470#undef GEN_INT_ARITH_DIVD
9471#define GEN_INT_ARITH_DIVD(name, opc3, sign, compute_ov) \
9472GEN_HANDLER(name, 0x1F, 0x09, opc3, 0x00000000, PPC_64B)
9473GEN_INT_ARITH_DIVD(divdu, 0x0E, 0, 0),
9474GEN_INT_ARITH_DIVD(divduo, 0x1E, 0, 1),
9475GEN_INT_ARITH_DIVD(divd, 0x0F, 1, 0),
9476GEN_INT_ARITH_DIVD(divdo, 0x1F, 1, 1),
9477
9478#undef GEN_INT_ARITH_MUL_HELPER
9479#define GEN_INT_ARITH_MUL_HELPER(name, opc3) \
9480GEN_HANDLER(name, 0x1F, 0x09, opc3, 0x00000000, PPC_64B)
9481GEN_INT_ARITH_MUL_HELPER(mulhdu, 0x00),
9482GEN_INT_ARITH_MUL_HELPER(mulhd, 0x02),
9483GEN_INT_ARITH_MUL_HELPER(mulldo, 0x17),
9484#endif
9485
9486#undef GEN_INT_ARITH_SUBF
9487#undef GEN_INT_ARITH_SUBF_CONST
9488#define GEN_INT_ARITH_SUBF(name, opc3, add_ca, compute_ca, compute_ov) \
9489GEN_HANDLER(name, 0x1F, 0x08, opc3, 0x00000000, PPC_INTEGER),
9490#define GEN_INT_ARITH_SUBF_CONST(name, opc3, const_val, \
9491 add_ca, compute_ca, compute_ov) \
9492GEN_HANDLER(name, 0x1F, 0x08, opc3, 0x0000F800, PPC_INTEGER),
9493GEN_INT_ARITH_SUBF(subf, 0x01, 0, 0, 0)
9494GEN_INT_ARITH_SUBF(subfo, 0x11, 0, 0, 1)
9495GEN_INT_ARITH_SUBF(subfc, 0x00, 0, 1, 0)
9496GEN_INT_ARITH_SUBF(subfco, 0x10, 0, 1, 1)
9497GEN_INT_ARITH_SUBF(subfe, 0x04, 1, 1, 0)
9498GEN_INT_ARITH_SUBF(subfeo, 0x14, 1, 1, 1)
9499GEN_INT_ARITH_SUBF_CONST(subfme, 0x07, -1LL, 1, 1, 0)
9500GEN_INT_ARITH_SUBF_CONST(subfmeo, 0x17, -1LL, 1, 1, 1)
9501GEN_INT_ARITH_SUBF_CONST(subfze, 0x06, 0, 1, 1, 0)
9502GEN_INT_ARITH_SUBF_CONST(subfzeo, 0x16, 0, 1, 1, 1)
9503
9504#undef GEN_LOGICAL1
9505#undef GEN_LOGICAL2
9506#define GEN_LOGICAL2(name, tcg_op, opc, type) \
9507GEN_HANDLER(name, 0x1F, 0x1C, opc, 0x00000000, type)
9508#define GEN_LOGICAL1(name, tcg_op, opc, type) \
9509GEN_HANDLER(name, 0x1F, 0x1A, opc, 0x00000000, type)
9510GEN_LOGICAL2(and, tcg_gen_and_tl, 0x00, PPC_INTEGER),
9511GEN_LOGICAL2(andc, tcg_gen_andc_tl, 0x01, PPC_INTEGER),
9512GEN_LOGICAL2(eqv, tcg_gen_eqv_tl, 0x08, PPC_INTEGER),
9513GEN_LOGICAL1(extsb, tcg_gen_ext8s_tl, 0x1D, PPC_INTEGER),
9514GEN_LOGICAL1(extsh, tcg_gen_ext16s_tl, 0x1C, PPC_INTEGER),
9515GEN_LOGICAL2(nand, tcg_gen_nand_tl, 0x0E, PPC_INTEGER),
9516GEN_LOGICAL2(nor, tcg_gen_nor_tl, 0x03, PPC_INTEGER),
9517GEN_LOGICAL2(orc, tcg_gen_orc_tl, 0x0C, PPC_INTEGER),
9518#if defined(TARGET_PPC64)
9519GEN_LOGICAL1(extsw, tcg_gen_ext32s_tl, 0x1E, PPC_64B),
9520#endif
9521
9522#if defined(TARGET_PPC64)
9523#undef GEN_PPC64_R2
9524#undef GEN_PPC64_R4
9525#define GEN_PPC64_R2(name, opc1, opc2) \
9526GEN_HANDLER2(name##0, stringify(name), opc1, opc2, 0xFF, 0x00000000, PPC_64B),\
9527GEN_HANDLER2(name##1, stringify(name), opc1, opc2 | 0x10, 0xFF, 0x00000000, \
9528 PPC_64B)
9529#define GEN_PPC64_R4(name, opc1, opc2) \
9530GEN_HANDLER2(name##0, stringify(name), opc1, opc2, 0xFF, 0x00000000, PPC_64B),\
9531GEN_HANDLER2(name##1, stringify(name), opc1, opc2 | 0x01, 0xFF, 0x00000000, \
9532 PPC_64B), \
9533GEN_HANDLER2(name##2, stringify(name), opc1, opc2 | 0x10, 0xFF, 0x00000000, \
9534 PPC_64B), \
9535GEN_HANDLER2(name##3, stringify(name), opc1, opc2 | 0x11, 0xFF, 0x00000000, \
9536 PPC_64B)
9537GEN_PPC64_R4(rldicl, 0x1E, 0x00),
9538GEN_PPC64_R4(rldicr, 0x1E, 0x02),
9539GEN_PPC64_R4(rldic, 0x1E, 0x04),
9540GEN_PPC64_R2(rldcl, 0x1E, 0x08),
9541GEN_PPC64_R2(rldcr, 0x1E, 0x09),
9542GEN_PPC64_R4(rldimi, 0x1E, 0x06),
9543#endif
9544
9545#undef _GEN_FLOAT_ACB
9546#undef GEN_FLOAT_ACB
9547#undef _GEN_FLOAT_AB
9548#undef GEN_FLOAT_AB
9549#undef _GEN_FLOAT_AC
9550#undef GEN_FLOAT_AC
9551#undef GEN_FLOAT_B
9552#undef GEN_FLOAT_BS
9553#define _GEN_FLOAT_ACB(name, op, op1, op2, isfloat, set_fprf, type) \
9554GEN_HANDLER(f##name, op1, op2, 0xFF, 0x00000000, type)
9555#define GEN_FLOAT_ACB(name, op2, set_fprf, type) \
9556_GEN_FLOAT_ACB(name, name, 0x3F, op2, 0, set_fprf, type), \
9557_GEN_FLOAT_ACB(name##s, name, 0x3B, op2, 1, set_fprf, type)
9558#define _GEN_FLOAT_AB(name, op, op1, op2, inval, isfloat, set_fprf, type) \
9559GEN_HANDLER(f##name, op1, op2, 0xFF, inval, type)
9560#define GEN_FLOAT_AB(name, op2, inval, set_fprf, type) \
9561_GEN_FLOAT_AB(name, name, 0x3F, op2, inval, 0, set_fprf, type), \
9562_GEN_FLOAT_AB(name##s, name, 0x3B, op2, inval, 1, set_fprf, type)
9563#define _GEN_FLOAT_AC(name, op, op1, op2, inval, isfloat, set_fprf, type) \
9564GEN_HANDLER(f##name, op1, op2, 0xFF, inval, type)
9565#define GEN_FLOAT_AC(name, op2, inval, set_fprf, type) \
9566_GEN_FLOAT_AC(name, name, 0x3F, op2, inval, 0, set_fprf, type), \
9567_GEN_FLOAT_AC(name##s, name, 0x3B, op2, inval, 1, set_fprf, type)
9568#define GEN_FLOAT_B(name, op2, op3, set_fprf, type) \
9569GEN_HANDLER(f##name, 0x3F, op2, op3, 0x001F0000, type)
9570#define GEN_FLOAT_BS(name, op1, op2, set_fprf, type) \
9571GEN_HANDLER(f##name, op1, op2, 0xFF, 0x001F07C0, type)
9572
9573GEN_FLOAT_AB(add, 0x15, 0x000007C0, 1, PPC_FLOAT),
9574GEN_FLOAT_AB(div, 0x12, 0x000007C0, 1, PPC_FLOAT),
9575GEN_FLOAT_AC(mul, 0x19, 0x0000F800, 1, PPC_FLOAT),
9576GEN_FLOAT_BS(re, 0x3F, 0x18, 1, PPC_FLOAT_EXT),
9577GEN_FLOAT_BS(res, 0x3B, 0x18, 1, PPC_FLOAT_FRES),
9578GEN_FLOAT_BS(rsqrte, 0x3F, 0x1A, 1, PPC_FLOAT_FRSQRTE),
9579_GEN_FLOAT_ACB(sel, sel, 0x3F, 0x17, 0, 0, PPC_FLOAT_FSEL),
9580GEN_FLOAT_AB(sub, 0x14, 0x000007C0, 1, PPC_FLOAT),
9581GEN_FLOAT_ACB(madd, 0x1D, 1, PPC_FLOAT),
9582GEN_FLOAT_ACB(msub, 0x1C, 1, PPC_FLOAT),
9583GEN_FLOAT_ACB(nmadd, 0x1F, 1, PPC_FLOAT),
9584GEN_FLOAT_ACB(nmsub, 0x1E, 1, PPC_FLOAT),
9585GEN_FLOAT_B(ctiw, 0x0E, 0x00, 0, PPC_FLOAT),
9586GEN_FLOAT_B(ctiwz, 0x0F, 0x00, 0, PPC_FLOAT),
9587GEN_FLOAT_B(rsp, 0x0C, 0x00, 1, PPC_FLOAT),
9588#if defined(TARGET_PPC64)
9589GEN_FLOAT_B(cfid, 0x0E, 0x1A, 1, PPC_64B),
9590GEN_FLOAT_B(ctid, 0x0E, 0x19, 0, PPC_64B),
9591GEN_FLOAT_B(ctidz, 0x0F, 0x19, 0, PPC_64B),
9592#endif
9593GEN_FLOAT_B(rin, 0x08, 0x0C, 1, PPC_FLOAT_EXT),
9594GEN_FLOAT_B(riz, 0x08, 0x0D, 1, PPC_FLOAT_EXT),
9595GEN_FLOAT_B(rip, 0x08, 0x0E, 1, PPC_FLOAT_EXT),
9596GEN_FLOAT_B(rim, 0x08, 0x0F, 1, PPC_FLOAT_EXT),
5c55ff99
BS
9597
9598#undef GEN_LD
9599#undef GEN_LDU
9600#undef GEN_LDUX
cd6e9320 9601#undef GEN_LDX_E
5c55ff99
BS
9602#undef GEN_LDS
9603#define GEN_LD(name, ldop, opc, type) \
9604GEN_HANDLER(name, opc, 0xFF, 0xFF, 0x00000000, type),
9605#define GEN_LDU(name, ldop, opc, type) \
9606GEN_HANDLER(name##u, opc, 0xFF, 0xFF, 0x00000000, type),
9607#define GEN_LDUX(name, ldop, opc2, opc3, type) \
9608GEN_HANDLER(name##ux, 0x1F, opc2, opc3, 0x00000001, type),
cd6e9320
TH
9609#define GEN_LDX_E(name, ldop, opc2, opc3, type, type2) \
9610GEN_HANDLER_E(name##x, 0x1F, opc2, opc3, 0x00000001, type, type2),
5c55ff99
BS
9611#define GEN_LDS(name, ldop, op, type) \
9612GEN_LD(name, ldop, op | 0x20, type) \
9613GEN_LDU(name, ldop, op | 0x21, type) \
9614GEN_LDUX(name, ldop, 0x17, op | 0x01, type) \
9615GEN_LDX(name, ldop, 0x17, op | 0x00, type)
9616
9617GEN_LDS(lbz, ld8u, 0x02, PPC_INTEGER)
9618GEN_LDS(lha, ld16s, 0x0A, PPC_INTEGER)
9619GEN_LDS(lhz, ld16u, 0x08, PPC_INTEGER)
9620GEN_LDS(lwz, ld32u, 0x00, PPC_INTEGER)
9621#if defined(TARGET_PPC64)
9622GEN_LDUX(lwa, ld32s, 0x15, 0x0B, PPC_64B)
9623GEN_LDX(lwa, ld32s, 0x15, 0x0A, PPC_64B)
9624GEN_LDUX(ld, ld64, 0x15, 0x01, PPC_64B)
9625GEN_LDX(ld, ld64, 0x15, 0x00, PPC_64B)
cd6e9320 9626GEN_LDX_E(ldbr, ld64ur, 0x14, 0x10, PPC_NONE, PPC2_DBRX)
5c55ff99
BS
9627#endif
9628GEN_LDX(lhbr, ld16ur, 0x16, 0x18, PPC_INTEGER)
9629GEN_LDX(lwbr, ld32ur, 0x16, 0x10, PPC_INTEGER)
9630
9631#undef GEN_ST
9632#undef GEN_STU
9633#undef GEN_STUX
cd6e9320 9634#undef GEN_STX_E
5c55ff99
BS
9635#undef GEN_STS
9636#define GEN_ST(name, stop, opc, type) \
9637GEN_HANDLER(name, opc, 0xFF, 0xFF, 0x00000000, type),
9638#define GEN_STU(name, stop, opc, type) \
9639GEN_HANDLER(stop##u, opc, 0xFF, 0xFF, 0x00000000, type),
9640#define GEN_STUX(name, stop, opc2, opc3, type) \
9641GEN_HANDLER(name##ux, 0x1F, opc2, opc3, 0x00000001, type),
cd6e9320
TH
9642#define GEN_STX_E(name, stop, opc2, opc3, type, type2) \
9643GEN_HANDLER_E(name##x, 0x1F, opc2, opc3, 0x00000001, type, type2),
5c55ff99
BS
9644#define GEN_STS(name, stop, op, type) \
9645GEN_ST(name, stop, op | 0x20, type) \
9646GEN_STU(name, stop, op | 0x21, type) \
9647GEN_STUX(name, stop, 0x17, op | 0x01, type) \
9648GEN_STX(name, stop, 0x17, op | 0x00, type)
9649
9650GEN_STS(stb, st8, 0x06, PPC_INTEGER)
9651GEN_STS(sth, st16, 0x0C, PPC_INTEGER)
9652GEN_STS(stw, st32, 0x04, PPC_INTEGER)
9653#if defined(TARGET_PPC64)
9654GEN_STUX(std, st64, 0x15, 0x05, PPC_64B)
9655GEN_STX(std, st64, 0x15, 0x04, PPC_64B)
cd6e9320 9656GEN_STX_E(stdbr, st64r, 0x14, 0x14, PPC_NONE, PPC2_DBRX)
5c55ff99
BS
9657#endif
9658GEN_STX(sthbr, st16r, 0x16, 0x1C, PPC_INTEGER)
9659GEN_STX(stwbr, st32r, 0x16, 0x14, PPC_INTEGER)
9660
9661#undef GEN_LDF
9662#undef GEN_LDUF
9663#undef GEN_LDUXF
9664#undef GEN_LDXF
9665#undef GEN_LDFS
9666#define GEN_LDF(name, ldop, opc, type) \
9667GEN_HANDLER(name, opc, 0xFF, 0xFF, 0x00000000, type),
9668#define GEN_LDUF(name, ldop, opc, type) \
9669GEN_HANDLER(name##u, opc, 0xFF, 0xFF, 0x00000000, type),
9670#define GEN_LDUXF(name, ldop, opc, type) \
9671GEN_HANDLER(name##ux, 0x1F, 0x17, opc, 0x00000001, type),
9672#define GEN_LDXF(name, ldop, opc2, opc3, type) \
9673GEN_HANDLER(name##x, 0x1F, opc2, opc3, 0x00000001, type),
9674#define GEN_LDFS(name, ldop, op, type) \
9675GEN_LDF(name, ldop, op | 0x20, type) \
9676GEN_LDUF(name, ldop, op | 0x21, type) \
9677GEN_LDUXF(name, ldop, op | 0x01, type) \
9678GEN_LDXF(name, ldop, 0x17, op | 0x00, type)
9679
9680GEN_LDFS(lfd, ld64, 0x12, PPC_FLOAT)
9681GEN_LDFS(lfs, ld32fs, 0x10, PPC_FLOAT)
199f830d 9682GEN_HANDLER_E(lfiwax, 0x1f, 0x17, 0x1a, 0x00000001, PPC_NONE, PPC2_ISA205),
05050ee8
AJ
9683GEN_HANDLER_E(lfdp, 0x39, 0xFF, 0xFF, 0x00200003, PPC_NONE, PPC2_ISA205),
9684GEN_HANDLER_E(lfdpx, 0x1F, 0x17, 0x18, 0x00200001, PPC_NONE, PPC2_ISA205),
5c55ff99
BS
9685
9686#undef GEN_STF
9687#undef GEN_STUF
9688#undef GEN_STUXF
9689#undef GEN_STXF
9690#undef GEN_STFS
9691#define GEN_STF(name, stop, opc, type) \
9692GEN_HANDLER(name, opc, 0xFF, 0xFF, 0x00000000, type),
9693#define GEN_STUF(name, stop, opc, type) \
9694GEN_HANDLER(name##u, opc, 0xFF, 0xFF, 0x00000000, type),
9695#define GEN_STUXF(name, stop, opc, type) \
9696GEN_HANDLER(name##ux, 0x1F, 0x17, opc, 0x00000001, type),
9697#define GEN_STXF(name, stop, opc2, opc3, type) \
9698GEN_HANDLER(name##x, 0x1F, opc2, opc3, 0x00000001, type),
9699#define GEN_STFS(name, stop, op, type) \
9700GEN_STF(name, stop, op | 0x20, type) \
9701GEN_STUF(name, stop, op | 0x21, type) \
9702GEN_STUXF(name, stop, op | 0x01, type) \
9703GEN_STXF(name, stop, 0x17, op | 0x00, type)
9704
9705GEN_STFS(stfd, st64, 0x16, PPC_FLOAT)
9706GEN_STFS(stfs, st32fs, 0x14, PPC_FLOAT)
9707GEN_STXF(stfiw, st32fiw, 0x17, 0x1E, PPC_FLOAT_STFIWX)
44bc0c4d
AJ
9708GEN_HANDLER_E(stfdp, 0x3D, 0xFF, 0xFF, 0x00200003, PPC_NONE, PPC2_ISA205),
9709GEN_HANDLER_E(stfdpx, 0x1F, 0x17, 0x1C, 0x00200001, PPC_NONE, PPC2_ISA205),
5c55ff99
BS
9710
9711#undef GEN_CRLOGIC
9712#define GEN_CRLOGIC(name, tcg_op, opc) \
9713GEN_HANDLER(name, 0x13, 0x01, opc, 0x00000001, PPC_INTEGER)
9714GEN_CRLOGIC(crand, tcg_gen_and_i32, 0x08),
9715GEN_CRLOGIC(crandc, tcg_gen_andc_i32, 0x04),
9716GEN_CRLOGIC(creqv, tcg_gen_eqv_i32, 0x09),
9717GEN_CRLOGIC(crnand, tcg_gen_nand_i32, 0x07),
9718GEN_CRLOGIC(crnor, tcg_gen_nor_i32, 0x01),
9719GEN_CRLOGIC(cror, tcg_gen_or_i32, 0x0E),
9720GEN_CRLOGIC(crorc, tcg_gen_orc_i32, 0x0D),
9721GEN_CRLOGIC(crxor, tcg_gen_xor_i32, 0x06),
9722
9723#undef GEN_MAC_HANDLER
9724#define GEN_MAC_HANDLER(name, opc2, opc3) \
9725GEN_HANDLER(name, 0x04, opc2, opc3, 0x00000000, PPC_405_MAC)
9726GEN_MAC_HANDLER(macchw, 0x0C, 0x05),
9727GEN_MAC_HANDLER(macchwo, 0x0C, 0x15),
9728GEN_MAC_HANDLER(macchws, 0x0C, 0x07),
9729GEN_MAC_HANDLER(macchwso, 0x0C, 0x17),
9730GEN_MAC_HANDLER(macchwsu, 0x0C, 0x06),
9731GEN_MAC_HANDLER(macchwsuo, 0x0C, 0x16),
9732GEN_MAC_HANDLER(macchwu, 0x0C, 0x04),
9733GEN_MAC_HANDLER(macchwuo, 0x0C, 0x14),
9734GEN_MAC_HANDLER(machhw, 0x0C, 0x01),
9735GEN_MAC_HANDLER(machhwo, 0x0C, 0x11),
9736GEN_MAC_HANDLER(machhws, 0x0C, 0x03),
9737GEN_MAC_HANDLER(machhwso, 0x0C, 0x13),
9738GEN_MAC_HANDLER(machhwsu, 0x0C, 0x02),
9739GEN_MAC_HANDLER(machhwsuo, 0x0C, 0x12),
9740GEN_MAC_HANDLER(machhwu, 0x0C, 0x00),
9741GEN_MAC_HANDLER(machhwuo, 0x0C, 0x10),
9742GEN_MAC_HANDLER(maclhw, 0x0C, 0x0D),
9743GEN_MAC_HANDLER(maclhwo, 0x0C, 0x1D),
9744GEN_MAC_HANDLER(maclhws, 0x0C, 0x0F),
9745GEN_MAC_HANDLER(maclhwso, 0x0C, 0x1F),
9746GEN_MAC_HANDLER(maclhwu, 0x0C, 0x0C),
9747GEN_MAC_HANDLER(maclhwuo, 0x0C, 0x1C),
9748GEN_MAC_HANDLER(maclhwsu, 0x0C, 0x0E),
9749GEN_MAC_HANDLER(maclhwsuo, 0x0C, 0x1E),
9750GEN_MAC_HANDLER(nmacchw, 0x0E, 0x05),
9751GEN_MAC_HANDLER(nmacchwo, 0x0E, 0x15),
9752GEN_MAC_HANDLER(nmacchws, 0x0E, 0x07),
9753GEN_MAC_HANDLER(nmacchwso, 0x0E, 0x17),
9754GEN_MAC_HANDLER(nmachhw, 0x0E, 0x01),
9755GEN_MAC_HANDLER(nmachhwo, 0x0E, 0x11),
9756GEN_MAC_HANDLER(nmachhws, 0x0E, 0x03),
9757GEN_MAC_HANDLER(nmachhwso, 0x0E, 0x13),
9758GEN_MAC_HANDLER(nmaclhw, 0x0E, 0x0D),
9759GEN_MAC_HANDLER(nmaclhwo, 0x0E, 0x1D),
9760GEN_MAC_HANDLER(nmaclhws, 0x0E, 0x0F),
9761GEN_MAC_HANDLER(nmaclhwso, 0x0E, 0x1F),
9762GEN_MAC_HANDLER(mulchw, 0x08, 0x05),
9763GEN_MAC_HANDLER(mulchwu, 0x08, 0x04),
9764GEN_MAC_HANDLER(mulhhw, 0x08, 0x01),
9765GEN_MAC_HANDLER(mulhhwu, 0x08, 0x00),
9766GEN_MAC_HANDLER(mullhw, 0x08, 0x0D),
9767GEN_MAC_HANDLER(mullhwu, 0x08, 0x0C),
9768
9769#undef GEN_VR_LDX
9770#undef GEN_VR_STX
9771#undef GEN_VR_LVE
9772#undef GEN_VR_STVE
9773#define GEN_VR_LDX(name, opc2, opc3) \
9774GEN_HANDLER(name, 0x1F, opc2, opc3, 0x00000001, PPC_ALTIVEC)
9775#define GEN_VR_STX(name, opc2, opc3) \
9776GEN_HANDLER(st##name, 0x1F, opc2, opc3, 0x00000001, PPC_ALTIVEC)
9777#define GEN_VR_LVE(name, opc2, opc3) \
9778 GEN_HANDLER(lve##name, 0x1F, opc2, opc3, 0x00000001, PPC_ALTIVEC)
9779#define GEN_VR_STVE(name, opc2, opc3) \
9780 GEN_HANDLER(stve##name, 0x1F, opc2, opc3, 0x00000001, PPC_ALTIVEC)
9781GEN_VR_LDX(lvx, 0x07, 0x03),
9782GEN_VR_LDX(lvxl, 0x07, 0x0B),
9783GEN_VR_LVE(bx, 0x07, 0x00),
9784GEN_VR_LVE(hx, 0x07, 0x01),
9785GEN_VR_LVE(wx, 0x07, 0x02),
9786GEN_VR_STX(svx, 0x07, 0x07),
9787GEN_VR_STX(svxl, 0x07, 0x0F),
9788GEN_VR_STVE(bx, 0x07, 0x04),
9789GEN_VR_STVE(hx, 0x07, 0x05),
9790GEN_VR_STVE(wx, 0x07, 0x06),
9791
9792#undef GEN_VX_LOGICAL
9793#define GEN_VX_LOGICAL(name, tcg_op, opc2, opc3) \
9794GEN_HANDLER(name, 0x04, opc2, opc3, 0x00000000, PPC_ALTIVEC)
9795GEN_VX_LOGICAL(vand, tcg_gen_and_i64, 2, 16),
9796GEN_VX_LOGICAL(vandc, tcg_gen_andc_i64, 2, 17),
9797GEN_VX_LOGICAL(vor, tcg_gen_or_i64, 2, 18),
9798GEN_VX_LOGICAL(vxor, tcg_gen_xor_i64, 2, 19),
9799GEN_VX_LOGICAL(vnor, tcg_gen_nor_i64, 2, 20),
9800
9801#undef GEN_VXFORM
9802#define GEN_VXFORM(name, opc2, opc3) \
9803GEN_HANDLER(name, 0x04, opc2, opc3, 0x00000000, PPC_ALTIVEC)
9804GEN_VXFORM(vaddubm, 0, 0),
9805GEN_VXFORM(vadduhm, 0, 1),
9806GEN_VXFORM(vadduwm, 0, 2),
9807GEN_VXFORM(vsububm, 0, 16),
9808GEN_VXFORM(vsubuhm, 0, 17),
9809GEN_VXFORM(vsubuwm, 0, 18),
9810GEN_VXFORM(vmaxub, 1, 0),
9811GEN_VXFORM(vmaxuh, 1, 1),
9812GEN_VXFORM(vmaxuw, 1, 2),
9813GEN_VXFORM(vmaxsb, 1, 4),
9814GEN_VXFORM(vmaxsh, 1, 5),
9815GEN_VXFORM(vmaxsw, 1, 6),
9816GEN_VXFORM(vminub, 1, 8),
9817GEN_VXFORM(vminuh, 1, 9),
9818GEN_VXFORM(vminuw, 1, 10),
9819GEN_VXFORM(vminsb, 1, 12),
9820GEN_VXFORM(vminsh, 1, 13),
9821GEN_VXFORM(vminsw, 1, 14),
9822GEN_VXFORM(vavgub, 1, 16),
9823GEN_VXFORM(vavguh, 1, 17),
9824GEN_VXFORM(vavguw, 1, 18),
9825GEN_VXFORM(vavgsb, 1, 20),
9826GEN_VXFORM(vavgsh, 1, 21),
9827GEN_VXFORM(vavgsw, 1, 22),
9828GEN_VXFORM(vmrghb, 6, 0),
9829GEN_VXFORM(vmrghh, 6, 1),
9830GEN_VXFORM(vmrghw, 6, 2),
9831GEN_VXFORM(vmrglb, 6, 4),
9832GEN_VXFORM(vmrglh, 6, 5),
9833GEN_VXFORM(vmrglw, 6, 6),
9834GEN_VXFORM(vmuloub, 4, 0),
9835GEN_VXFORM(vmulouh, 4, 1),
9836GEN_VXFORM(vmulosb, 4, 4),
9837GEN_VXFORM(vmulosh, 4, 5),
9838GEN_VXFORM(vmuleub, 4, 8),
9839GEN_VXFORM(vmuleuh, 4, 9),
9840GEN_VXFORM(vmulesb, 4, 12),
9841GEN_VXFORM(vmulesh, 4, 13),
9842GEN_VXFORM(vslb, 2, 4),
9843GEN_VXFORM(vslh, 2, 5),
9844GEN_VXFORM(vslw, 2, 6),
9845GEN_VXFORM(vsrb, 2, 8),
9846GEN_VXFORM(vsrh, 2, 9),
9847GEN_VXFORM(vsrw, 2, 10),
9848GEN_VXFORM(vsrab, 2, 12),
9849GEN_VXFORM(vsrah, 2, 13),
9850GEN_VXFORM(vsraw, 2, 14),
9851GEN_VXFORM(vslo, 6, 16),
9852GEN_VXFORM(vsro, 6, 17),
9853GEN_VXFORM(vaddcuw, 0, 6),
9854GEN_VXFORM(vsubcuw, 0, 22),
9855GEN_VXFORM(vaddubs, 0, 8),
9856GEN_VXFORM(vadduhs, 0, 9),
9857GEN_VXFORM(vadduws, 0, 10),
9858GEN_VXFORM(vaddsbs, 0, 12),
9859GEN_VXFORM(vaddshs, 0, 13),
9860GEN_VXFORM(vaddsws, 0, 14),
9861GEN_VXFORM(vsububs, 0, 24),
9862GEN_VXFORM(vsubuhs, 0, 25),
9863GEN_VXFORM(vsubuws, 0, 26),
9864GEN_VXFORM(vsubsbs, 0, 28),
9865GEN_VXFORM(vsubshs, 0, 29),
9866GEN_VXFORM(vsubsws, 0, 30),
9867GEN_VXFORM(vrlb, 2, 0),
9868GEN_VXFORM(vrlh, 2, 1),
9869GEN_VXFORM(vrlw, 2, 2),
9870GEN_VXFORM(vsl, 2, 7),
9871GEN_VXFORM(vsr, 2, 11),
9872GEN_VXFORM(vpkuhum, 7, 0),
9873GEN_VXFORM(vpkuwum, 7, 1),
9874GEN_VXFORM(vpkuhus, 7, 2),
9875GEN_VXFORM(vpkuwus, 7, 3),
9876GEN_VXFORM(vpkshus, 7, 4),
9877GEN_VXFORM(vpkswus, 7, 5),
9878GEN_VXFORM(vpkshss, 7, 6),
9879GEN_VXFORM(vpkswss, 7, 7),
9880GEN_VXFORM(vpkpx, 7, 12),
9881GEN_VXFORM(vsum4ubs, 4, 24),
9882GEN_VXFORM(vsum4sbs, 4, 28),
9883GEN_VXFORM(vsum4shs, 4, 25),
9884GEN_VXFORM(vsum2sws, 4, 26),
9885GEN_VXFORM(vsumsws, 4, 30),
9886GEN_VXFORM(vaddfp, 5, 0),
9887GEN_VXFORM(vsubfp, 5, 1),
9888GEN_VXFORM(vmaxfp, 5, 16),
9889GEN_VXFORM(vminfp, 5, 17),
9890
9891#undef GEN_VXRFORM1
9892#undef GEN_VXRFORM
9893#define GEN_VXRFORM1(opname, name, str, opc2, opc3) \
9894 GEN_HANDLER2(name, str, 0x4, opc2, opc3, 0x00000000, PPC_ALTIVEC),
9895#define GEN_VXRFORM(name, opc2, opc3) \
9896 GEN_VXRFORM1(name, name, #name, opc2, opc3) \
9897 GEN_VXRFORM1(name##_dot, name##_, #name ".", opc2, (opc3 | (0x1 << 4)))
9898GEN_VXRFORM(vcmpequb, 3, 0)
9899GEN_VXRFORM(vcmpequh, 3, 1)
9900GEN_VXRFORM(vcmpequw, 3, 2)
9901GEN_VXRFORM(vcmpgtsb, 3, 12)
9902GEN_VXRFORM(vcmpgtsh, 3, 13)
9903GEN_VXRFORM(vcmpgtsw, 3, 14)
9904GEN_VXRFORM(vcmpgtub, 3, 8)
9905GEN_VXRFORM(vcmpgtuh, 3, 9)
9906GEN_VXRFORM(vcmpgtuw, 3, 10)
9907GEN_VXRFORM(vcmpeqfp, 3, 3)
9908GEN_VXRFORM(vcmpgefp, 3, 7)
9909GEN_VXRFORM(vcmpgtfp, 3, 11)
9910GEN_VXRFORM(vcmpbfp, 3, 15)
9911
9912#undef GEN_VXFORM_SIMM
9913#define GEN_VXFORM_SIMM(name, opc2, opc3) \
9914 GEN_HANDLER(name, 0x04, opc2, opc3, 0x00000000, PPC_ALTIVEC)
9915GEN_VXFORM_SIMM(vspltisb, 6, 12),
9916GEN_VXFORM_SIMM(vspltish, 6, 13),
9917GEN_VXFORM_SIMM(vspltisw, 6, 14),
9918
9919#undef GEN_VXFORM_NOA
9920#define GEN_VXFORM_NOA(name, opc2, opc3) \
9921 GEN_HANDLER(name, 0x04, opc2, opc3, 0x001f0000, PPC_ALTIVEC)
9922GEN_VXFORM_NOA(vupkhsb, 7, 8),
9923GEN_VXFORM_NOA(vupkhsh, 7, 9),
9924GEN_VXFORM_NOA(vupklsb, 7, 10),
9925GEN_VXFORM_NOA(vupklsh, 7, 11),
9926GEN_VXFORM_NOA(vupkhpx, 7, 13),
9927GEN_VXFORM_NOA(vupklpx, 7, 15),
9928GEN_VXFORM_NOA(vrefp, 5, 4),
9929GEN_VXFORM_NOA(vrsqrtefp, 5, 5),
0bffbc6c 9930GEN_VXFORM_NOA(vexptefp, 5, 6),
5c55ff99
BS
9931GEN_VXFORM_NOA(vlogefp, 5, 7),
9932GEN_VXFORM_NOA(vrfim, 5, 8),
9933GEN_VXFORM_NOA(vrfin, 5, 9),
9934GEN_VXFORM_NOA(vrfip, 5, 10),
9935GEN_VXFORM_NOA(vrfiz, 5, 11),
9936
9937#undef GEN_VXFORM_UIMM
9938#define GEN_VXFORM_UIMM(name, opc2, opc3) \
9939 GEN_HANDLER(name, 0x04, opc2, opc3, 0x00000000, PPC_ALTIVEC)
9940GEN_VXFORM_UIMM(vspltb, 6, 8),
9941GEN_VXFORM_UIMM(vsplth, 6, 9),
9942GEN_VXFORM_UIMM(vspltw, 6, 10),
9943GEN_VXFORM_UIMM(vcfux, 5, 12),
9944GEN_VXFORM_UIMM(vcfsx, 5, 13),
9945GEN_VXFORM_UIMM(vctuxs, 5, 14),
9946GEN_VXFORM_UIMM(vctsxs, 5, 15),
9947
9948#undef GEN_VAFORM_PAIRED
9949#define GEN_VAFORM_PAIRED(name0, name1, opc2) \
9950 GEN_HANDLER(name0##_##name1, 0x04, opc2, 0xFF, 0x00000000, PPC_ALTIVEC)
9951GEN_VAFORM_PAIRED(vmhaddshs, vmhraddshs, 16),
9952GEN_VAFORM_PAIRED(vmsumubm, vmsummbm, 18),
9953GEN_VAFORM_PAIRED(vmsumuhm, vmsumuhs, 19),
9954GEN_VAFORM_PAIRED(vmsumshm, vmsumshs, 20),
9955GEN_VAFORM_PAIRED(vsel, vperm, 21),
9956GEN_VAFORM_PAIRED(vmaddfp, vnmsubfp, 23),
9957
fa1832d7 9958GEN_HANDLER_E(lxsdx, 0x1F, 0x0C, 0x12, 0, PPC_NONE, PPC2_VSX),
304af367 9959GEN_HANDLER_E(lxvd2x, 0x1F, 0x0C, 0x1A, 0, PPC_NONE, PPC2_VSX),
ca03b467 9960GEN_HANDLER_E(lxvdsx, 0x1F, 0x0C, 0x0A, 0, PPC_NONE, PPC2_VSX),
897e61d1 9961GEN_HANDLER_E(lxvw4x, 0x1F, 0x0C, 0x18, 0, PPC_NONE, PPC2_VSX),
304af367 9962
9231ba9e 9963GEN_HANDLER_E(stxsdx, 0x1F, 0xC, 0x16, 0, PPC_NONE, PPC2_VSX),
fbed2478 9964GEN_HANDLER_E(stxvd2x, 0x1F, 0xC, 0x1E, 0, PPC_NONE, PPC2_VSX),
86e61ce3 9965GEN_HANDLER_E(stxvw4x, 0x1F, 0xC, 0x1C, 0, PPC_NONE, PPC2_VSX),
fbed2478 9966
df020ce0
TM
9967#undef GEN_XX2FORM
9968#define GEN_XX2FORM(name, opc2, opc3, fl2) \
9969GEN_HANDLER2_E(name, #name, 0x3C, opc2 | 0, opc3, 0, PPC_NONE, fl2), \
9970GEN_HANDLER2_E(name, #name, 0x3C, opc2 | 1, opc3, 0, PPC_NONE, fl2)
9971
9972#undef GEN_XX3FORM
9973#define GEN_XX3FORM(name, opc2, opc3, fl2) \
9974GEN_HANDLER2_E(name, #name, 0x3C, opc2 | 0, opc3, 0, PPC_NONE, fl2), \
9975GEN_HANDLER2_E(name, #name, 0x3C, opc2 | 1, opc3, 0, PPC_NONE, fl2), \
9976GEN_HANDLER2_E(name, #name, 0x3C, opc2 | 2, opc3, 0, PPC_NONE, fl2), \
9977GEN_HANDLER2_E(name, #name, 0x3C, opc2 | 3, opc3, 0, PPC_NONE, fl2)
9978
cd73f2c9
TM
9979#undef GEN_XX3FORM_DM
9980#define GEN_XX3FORM_DM(name, opc2, opc3) \
9981GEN_HANDLER2_E(name, #name, 0x3C, opc2|0x00, opc3|0x00, 0, PPC_NONE, PPC2_VSX),\
9982GEN_HANDLER2_E(name, #name, 0x3C, opc2|0x01, opc3|0x00, 0, PPC_NONE, PPC2_VSX),\
9983GEN_HANDLER2_E(name, #name, 0x3C, opc2|0x02, opc3|0x00, 0, PPC_NONE, PPC2_VSX),\
9984GEN_HANDLER2_E(name, #name, 0x3C, opc2|0x03, opc3|0x00, 0, PPC_NONE, PPC2_VSX),\
9985GEN_HANDLER2_E(name, #name, 0x3C, opc2|0x00, opc3|0x04, 0, PPC_NONE, PPC2_VSX),\
9986GEN_HANDLER2_E(name, #name, 0x3C, opc2|0x01, opc3|0x04, 0, PPC_NONE, PPC2_VSX),\
9987GEN_HANDLER2_E(name, #name, 0x3C, opc2|0x02, opc3|0x04, 0, PPC_NONE, PPC2_VSX),\
9988GEN_HANDLER2_E(name, #name, 0x3C, opc2|0x03, opc3|0x04, 0, PPC_NONE, PPC2_VSX),\
9989GEN_HANDLER2_E(name, #name, 0x3C, opc2|0x00, opc3|0x08, 0, PPC_NONE, PPC2_VSX),\
9990GEN_HANDLER2_E(name, #name, 0x3C, opc2|0x01, opc3|0x08, 0, PPC_NONE, PPC2_VSX),\
9991GEN_HANDLER2_E(name, #name, 0x3C, opc2|0x02, opc3|0x08, 0, PPC_NONE, PPC2_VSX),\
9992GEN_HANDLER2_E(name, #name, 0x3C, opc2|0x03, opc3|0x08, 0, PPC_NONE, PPC2_VSX),\
9993GEN_HANDLER2_E(name, #name, 0x3C, opc2|0x00, opc3|0x0C, 0, PPC_NONE, PPC2_VSX),\
9994GEN_HANDLER2_E(name, #name, 0x3C, opc2|0x01, opc3|0x0C, 0, PPC_NONE, PPC2_VSX),\
9995GEN_HANDLER2_E(name, #name, 0x3C, opc2|0x02, opc3|0x0C, 0, PPC_NONE, PPC2_VSX),\
9996GEN_HANDLER2_E(name, #name, 0x3C, opc2|0x03, opc3|0x0C, 0, PPC_NONE, PPC2_VSX)
9997
df020ce0
TM
9998GEN_XX2FORM(xsabsdp, 0x12, 0x15, PPC2_VSX),
9999GEN_XX2FORM(xsnabsdp, 0x12, 0x16, PPC2_VSX),
10000GEN_XX2FORM(xsnegdp, 0x12, 0x17, PPC2_VSX),
10001GEN_XX3FORM(xscpsgndp, 0x00, 0x16, PPC2_VSX),
10002
be574920
TM
10003GEN_XX2FORM(xvabsdp, 0x12, 0x1D, PPC2_VSX),
10004GEN_XX2FORM(xvnabsdp, 0x12, 0x1E, PPC2_VSX),
10005GEN_XX2FORM(xvnegdp, 0x12, 0x1F, PPC2_VSX),
10006GEN_XX3FORM(xvcpsgndp, 0x00, 0x1E, PPC2_VSX),
10007GEN_XX2FORM(xvabssp, 0x12, 0x19, PPC2_VSX),
10008GEN_XX2FORM(xvnabssp, 0x12, 0x1A, PPC2_VSX),
10009GEN_XX2FORM(xvnegsp, 0x12, 0x1B, PPC2_VSX),
10010GEN_XX3FORM(xvcpsgnsp, 0x00, 0x1A, PPC2_VSX),
79ca8a6a 10011
ee6e02c0
TM
10012GEN_XX3FORM(xsadddp, 0x00, 0x04, PPC2_VSX),
10013GEN_XX3FORM(xssubdp, 0x00, 0x05, PPC2_VSX),
5e591d88 10014GEN_XX3FORM(xsmuldp, 0x00, 0x06, PPC2_VSX),
4b98eeef 10015GEN_XX3FORM(xsdivdp, 0x00, 0x07, PPC2_VSX),
2009227f 10016GEN_XX2FORM(xsredp, 0x14, 0x05, PPC2_VSX),
d32404fe 10017GEN_XX2FORM(xssqrtdp, 0x16, 0x04, PPC2_VSX),
d3f9df8f 10018GEN_XX2FORM(xsrsqrtedp, 0x14, 0x04, PPC2_VSX),
ee6e02c0
TM
10019
10020GEN_XX3FORM(xvadddp, 0x00, 0x0C, PPC2_VSX),
10021GEN_XX3FORM(xvsubdp, 0x00, 0x0D, PPC2_VSX),
5e591d88 10022GEN_XX3FORM(xvmuldp, 0x00, 0x0E, PPC2_VSX),
4b98eeef 10023GEN_XX3FORM(xvdivdp, 0x00, 0x0F, PPC2_VSX),
2009227f 10024GEN_XX2FORM(xvredp, 0x14, 0x0D, PPC2_VSX),
d32404fe 10025GEN_XX2FORM(xvsqrtdp, 0x16, 0x0C, PPC2_VSX),
d3f9df8f 10026GEN_XX2FORM(xvrsqrtedp, 0x14, 0x0C, PPC2_VSX),
ee6e02c0
TM
10027
10028GEN_XX3FORM(xvaddsp, 0x00, 0x08, PPC2_VSX),
10029GEN_XX3FORM(xvsubsp, 0x00, 0x09, PPC2_VSX),
5e591d88 10030GEN_XX3FORM(xvmulsp, 0x00, 0x0A, PPC2_VSX),
4b98eeef 10031GEN_XX3FORM(xvdivsp, 0x00, 0x0B, PPC2_VSX),
2009227f 10032GEN_XX2FORM(xvresp, 0x14, 0x09, PPC2_VSX),
d32404fe 10033GEN_XX2FORM(xvsqrtsp, 0x16, 0x08, PPC2_VSX),
d3f9df8f 10034GEN_XX2FORM(xvrsqrtesp, 0x14, 0x08, PPC2_VSX),
ee6e02c0 10035
79ca8a6a
TM
10036#undef VSX_LOGICAL
10037#define VSX_LOGICAL(name, opc2, opc3, fl2) \
10038GEN_XX3FORM(name, opc2, opc3, fl2)
10039
10040VSX_LOGICAL(xxland, 0x8, 0x10, PPC2_VSX),
10041VSX_LOGICAL(xxlandc, 0x8, 0x11, PPC2_VSX),
10042VSX_LOGICAL(xxlor, 0x8, 0x12, PPC2_VSX),
10043VSX_LOGICAL(xxlxor, 0x8, 0x13, PPC2_VSX),
10044VSX_LOGICAL(xxlnor, 0x8, 0x14, PPC2_VSX),
ce577d2e
TM
10045GEN_XX3FORM(xxmrghw, 0x08, 0x02, PPC2_VSX),
10046GEN_XX3FORM(xxmrglw, 0x08, 0x06, PPC2_VSX),
76c15fe0 10047GEN_XX2FORM(xxspltw, 0x08, 0x0A, PPC2_VSX),
acc42968 10048GEN_XX3FORM_DM(xxsldwi, 0x08, 0x00),
79ca8a6a 10049
551e3ef7
TM
10050#define GEN_XXSEL_ROW(opc3) \
10051GEN_HANDLER2_E(xxsel, "xxsel", 0x3C, 0x18, opc3, 0, PPC_NONE, PPC2_VSX), \
10052GEN_HANDLER2_E(xxsel, "xxsel", 0x3C, 0x19, opc3, 0, PPC_NONE, PPC2_VSX), \
10053GEN_HANDLER2_E(xxsel, "xxsel", 0x3C, 0x1A, opc3, 0, PPC_NONE, PPC2_VSX), \
10054GEN_HANDLER2_E(xxsel, "xxsel", 0x3C, 0x1B, opc3, 0, PPC_NONE, PPC2_VSX), \
10055GEN_HANDLER2_E(xxsel, "xxsel", 0x3C, 0x1C, opc3, 0, PPC_NONE, PPC2_VSX), \
10056GEN_HANDLER2_E(xxsel, "xxsel", 0x3C, 0x1D, opc3, 0, PPC_NONE, PPC2_VSX), \
10057GEN_HANDLER2_E(xxsel, "xxsel", 0x3C, 0x1E, opc3, 0, PPC_NONE, PPC2_VSX), \
10058GEN_HANDLER2_E(xxsel, "xxsel", 0x3C, 0x1F, opc3, 0, PPC_NONE, PPC2_VSX), \
10059
10060GEN_XXSEL_ROW(0x00)
10061GEN_XXSEL_ROW(0x01)
10062GEN_XXSEL_ROW(0x02)
10063GEN_XXSEL_ROW(0x03)
10064GEN_XXSEL_ROW(0x04)
10065GEN_XXSEL_ROW(0x05)
10066GEN_XXSEL_ROW(0x06)
10067GEN_XXSEL_ROW(0x07)
10068GEN_XXSEL_ROW(0x08)
10069GEN_XXSEL_ROW(0x09)
10070GEN_XXSEL_ROW(0x0A)
10071GEN_XXSEL_ROW(0x0B)
10072GEN_XXSEL_ROW(0x0C)
10073GEN_XXSEL_ROW(0x0D)
10074GEN_XXSEL_ROW(0x0E)
10075GEN_XXSEL_ROW(0x0F)
10076GEN_XXSEL_ROW(0x10)
10077GEN_XXSEL_ROW(0x11)
10078GEN_XXSEL_ROW(0x12)
10079GEN_XXSEL_ROW(0x13)
10080GEN_XXSEL_ROW(0x14)
10081GEN_XXSEL_ROW(0x15)
10082GEN_XXSEL_ROW(0x16)
10083GEN_XXSEL_ROW(0x17)
10084GEN_XXSEL_ROW(0x18)
10085GEN_XXSEL_ROW(0x19)
10086GEN_XXSEL_ROW(0x1A)
10087GEN_XXSEL_ROW(0x1B)
10088GEN_XXSEL_ROW(0x1C)
10089GEN_XXSEL_ROW(0x1D)
10090GEN_XXSEL_ROW(0x1E)
10091GEN_XXSEL_ROW(0x1F)
10092
cd73f2c9
TM
10093GEN_XX3FORM_DM(xxpermdi, 0x08, 0x01),
10094
5c55ff99 10095#undef GEN_SPE
70560da7
FC
10096#define GEN_SPE(name0, name1, opc2, opc3, inval0, inval1, type) \
10097 GEN_OPCODE_DUAL(name0##_##name1, 0x04, opc2, opc3, inval0, inval1, type, PPC_NONE)
10098GEN_SPE(evaddw, speundef, 0x00, 0x08, 0x00000000, 0xFFFFFFFF, PPC_SPE),
10099GEN_SPE(evaddiw, speundef, 0x01, 0x08, 0x00000000, 0xFFFFFFFF, PPC_SPE),
10100GEN_SPE(evsubfw, speundef, 0x02, 0x08, 0x00000000, 0xFFFFFFFF, PPC_SPE),
10101GEN_SPE(evsubifw, speundef, 0x03, 0x08, 0x00000000, 0xFFFFFFFF, PPC_SPE),
10102GEN_SPE(evabs, evneg, 0x04, 0x08, 0x0000F800, 0x0000F800, PPC_SPE),
10103GEN_SPE(evextsb, evextsh, 0x05, 0x08, 0x0000F800, 0x0000F800, PPC_SPE),
10104GEN_SPE(evrndw, evcntlzw, 0x06, 0x08, 0x0000F800, 0x0000F800, PPC_SPE),
10105GEN_SPE(evcntlsw, brinc, 0x07, 0x08, 0x0000F800, 0x00000000, PPC_SPE),
10106GEN_SPE(evmra, speundef, 0x02, 0x13, 0x0000F800, 0xFFFFFFFF, PPC_SPE),
10107GEN_SPE(speundef, evand, 0x08, 0x08, 0xFFFFFFFF, 0x00000000, PPC_SPE),
10108GEN_SPE(evandc, speundef, 0x09, 0x08, 0x00000000, 0xFFFFFFFF, PPC_SPE),
10109GEN_SPE(evxor, evor, 0x0B, 0x08, 0x00000000, 0x00000000, PPC_SPE),
10110GEN_SPE(evnor, eveqv, 0x0C, 0x08, 0x00000000, 0x00000000, PPC_SPE),
10111GEN_SPE(evmwumi, evmwsmi, 0x0C, 0x11, 0x00000000, 0x00000000, PPC_SPE),
10112GEN_SPE(evmwumia, evmwsmia, 0x1C, 0x11, 0x00000000, 0x00000000, PPC_SPE),
10113GEN_SPE(evmwumiaa, evmwsmiaa, 0x0C, 0x15, 0x00000000, 0x00000000, PPC_SPE),
10114GEN_SPE(speundef, evorc, 0x0D, 0x08, 0xFFFFFFFF, 0x00000000, PPC_SPE),
10115GEN_SPE(evnand, speundef, 0x0F, 0x08, 0x00000000, 0xFFFFFFFF, PPC_SPE),
10116GEN_SPE(evsrwu, evsrws, 0x10, 0x08, 0x00000000, 0x00000000, PPC_SPE),
10117GEN_SPE(evsrwiu, evsrwis, 0x11, 0x08, 0x00000000, 0x00000000, PPC_SPE),
10118GEN_SPE(evslw, speundef, 0x12, 0x08, 0x00000000, 0xFFFFFFFF, PPC_SPE),
10119GEN_SPE(evslwi, speundef, 0x13, 0x08, 0x00000000, 0xFFFFFFFF, PPC_SPE),
10120GEN_SPE(evrlw, evsplati, 0x14, 0x08, 0x00000000, 0x0000F800, PPC_SPE),
10121GEN_SPE(evrlwi, evsplatfi, 0x15, 0x08, 0x00000000, 0x0000F800, PPC_SPE),
10122GEN_SPE(evmergehi, evmergelo, 0x16, 0x08, 0x00000000, 0x00000000, PPC_SPE),
10123GEN_SPE(evmergehilo, evmergelohi, 0x17, 0x08, 0x00000000, 0x00000000, PPC_SPE),
10124GEN_SPE(evcmpgtu, evcmpgts, 0x18, 0x08, 0x00600000, 0x00600000, PPC_SPE),
10125GEN_SPE(evcmpltu, evcmplts, 0x19, 0x08, 0x00600000, 0x00600000, PPC_SPE),
10126GEN_SPE(evcmpeq, speundef, 0x1A, 0x08, 0x00600000, 0xFFFFFFFF, PPC_SPE),
10127
10128GEN_SPE(evfsadd, evfssub, 0x00, 0x0A, 0x00000000, 0x00000000, PPC_SPE_SINGLE),
10129GEN_SPE(evfsabs, evfsnabs, 0x02, 0x0A, 0x0000F800, 0x0000F800, PPC_SPE_SINGLE),
10130GEN_SPE(evfsneg, speundef, 0x03, 0x0A, 0x0000F800, 0xFFFFFFFF, PPC_SPE_SINGLE),
10131GEN_SPE(evfsmul, evfsdiv, 0x04, 0x0A, 0x00000000, 0x00000000, PPC_SPE_SINGLE),
10132GEN_SPE(evfscmpgt, evfscmplt, 0x06, 0x0A, 0x00600000, 0x00600000, PPC_SPE_SINGLE),
10133GEN_SPE(evfscmpeq, speundef, 0x07, 0x0A, 0x00600000, 0xFFFFFFFF, PPC_SPE_SINGLE),
10134GEN_SPE(evfscfui, evfscfsi, 0x08, 0x0A, 0x00180000, 0x00180000, PPC_SPE_SINGLE),
10135GEN_SPE(evfscfuf, evfscfsf, 0x09, 0x0A, 0x00180000, 0x00180000, PPC_SPE_SINGLE),
10136GEN_SPE(evfsctui, evfsctsi, 0x0A, 0x0A, 0x00180000, 0x00180000, PPC_SPE_SINGLE),
10137GEN_SPE(evfsctuf, evfsctsf, 0x0B, 0x0A, 0x00180000, 0x00180000, PPC_SPE_SINGLE),
10138GEN_SPE(evfsctuiz, speundef, 0x0C, 0x0A, 0x00180000, 0xFFFFFFFF, PPC_SPE_SINGLE),
10139GEN_SPE(evfsctsiz, speundef, 0x0D, 0x0A, 0x00180000, 0xFFFFFFFF, PPC_SPE_SINGLE),
10140GEN_SPE(evfststgt, evfststlt, 0x0E, 0x0A, 0x00600000, 0x00600000, PPC_SPE_SINGLE),
10141GEN_SPE(evfststeq, speundef, 0x0F, 0x0A, 0x00600000, 0xFFFFFFFF, PPC_SPE_SINGLE),
10142
10143GEN_SPE(efsadd, efssub, 0x00, 0x0B, 0x00000000, 0x00000000, PPC_SPE_SINGLE),
10144GEN_SPE(efsabs, efsnabs, 0x02, 0x0B, 0x0000F800, 0x0000F800, PPC_SPE_SINGLE),
10145GEN_SPE(efsneg, speundef, 0x03, 0x0B, 0x0000F800, 0xFFFFFFFF, PPC_SPE_SINGLE),
10146GEN_SPE(efsmul, efsdiv, 0x04, 0x0B, 0x00000000, 0x00000000, PPC_SPE_SINGLE),
10147GEN_SPE(efscmpgt, efscmplt, 0x06, 0x0B, 0x00600000, 0x00600000, PPC_SPE_SINGLE),
10148GEN_SPE(efscmpeq, efscfd, 0x07, 0x0B, 0x00600000, 0x00180000, PPC_SPE_SINGLE),
10149GEN_SPE(efscfui, efscfsi, 0x08, 0x0B, 0x00180000, 0x00180000, PPC_SPE_SINGLE),
10150GEN_SPE(efscfuf, efscfsf, 0x09, 0x0B, 0x00180000, 0x00180000, PPC_SPE_SINGLE),
10151GEN_SPE(efsctui, efsctsi, 0x0A, 0x0B, 0x00180000, 0x00180000, PPC_SPE_SINGLE),
10152GEN_SPE(efsctuf, efsctsf, 0x0B, 0x0B, 0x00180000, 0x00180000, PPC_SPE_SINGLE),
10153GEN_SPE(efsctuiz, speundef, 0x0C, 0x0B, 0x00180000, 0xFFFFFFFF, PPC_SPE_SINGLE),
10154GEN_SPE(efsctsiz, speundef, 0x0D, 0x0B, 0x00180000, 0xFFFFFFFF, PPC_SPE_SINGLE),
10155GEN_SPE(efststgt, efststlt, 0x0E, 0x0B, 0x00600000, 0x00600000, PPC_SPE_SINGLE),
10156GEN_SPE(efststeq, speundef, 0x0F, 0x0B, 0x00600000, 0xFFFFFFFF, PPC_SPE_SINGLE),
10157
10158GEN_SPE(efdadd, efdsub, 0x10, 0x0B, 0x00000000, 0x00000000, PPC_SPE_DOUBLE),
10159GEN_SPE(efdcfuid, efdcfsid, 0x11, 0x0B, 0x00180000, 0x00180000, PPC_SPE_DOUBLE),
10160GEN_SPE(efdabs, efdnabs, 0x12, 0x0B, 0x0000F800, 0x0000F800, PPC_SPE_DOUBLE),
10161GEN_SPE(efdneg, speundef, 0x13, 0x0B, 0x0000F800, 0xFFFFFFFF, PPC_SPE_DOUBLE),
10162GEN_SPE(efdmul, efddiv, 0x14, 0x0B, 0x00000000, 0x00000000, PPC_SPE_DOUBLE),
10163GEN_SPE(efdctuidz, efdctsidz, 0x15, 0x0B, 0x00180000, 0x00180000, PPC_SPE_DOUBLE),
10164GEN_SPE(efdcmpgt, efdcmplt, 0x16, 0x0B, 0x00600000, 0x00600000, PPC_SPE_DOUBLE),
10165GEN_SPE(efdcmpeq, efdcfs, 0x17, 0x0B, 0x00600000, 0x00180000, PPC_SPE_DOUBLE),
10166GEN_SPE(efdcfui, efdcfsi, 0x18, 0x0B, 0x00180000, 0x00180000, PPC_SPE_DOUBLE),
10167GEN_SPE(efdcfuf, efdcfsf, 0x19, 0x0B, 0x00180000, 0x00180000, PPC_SPE_DOUBLE),
10168GEN_SPE(efdctui, efdctsi, 0x1A, 0x0B, 0x00180000, 0x00180000, PPC_SPE_DOUBLE),
10169GEN_SPE(efdctuf, efdctsf, 0x1B, 0x0B, 0x00180000, 0x00180000, PPC_SPE_DOUBLE),
10170GEN_SPE(efdctuiz, speundef, 0x1C, 0x0B, 0x00180000, 0xFFFFFFFF, PPC_SPE_DOUBLE),
10171GEN_SPE(efdctsiz, speundef, 0x1D, 0x0B, 0x00180000, 0xFFFFFFFF, PPC_SPE_DOUBLE),
10172GEN_SPE(efdtstgt, efdtstlt, 0x1E, 0x0B, 0x00600000, 0x00600000, PPC_SPE_DOUBLE),
10173GEN_SPE(efdtsteq, speundef, 0x1F, 0x0B, 0x00600000, 0xFFFFFFFF, PPC_SPE_DOUBLE),
5c55ff99
BS
10174
10175#undef GEN_SPEOP_LDST
10176#define GEN_SPEOP_LDST(name, opc2, sh) \
10177GEN_HANDLER(name, 0x04, opc2, 0x0C, 0x00000000, PPC_SPE)
10178GEN_SPEOP_LDST(evldd, 0x00, 3),
10179GEN_SPEOP_LDST(evldw, 0x01, 3),
10180GEN_SPEOP_LDST(evldh, 0x02, 3),
10181GEN_SPEOP_LDST(evlhhesplat, 0x04, 1),
10182GEN_SPEOP_LDST(evlhhousplat, 0x06, 1),
10183GEN_SPEOP_LDST(evlhhossplat, 0x07, 1),
10184GEN_SPEOP_LDST(evlwhe, 0x08, 2),
10185GEN_SPEOP_LDST(evlwhou, 0x0A, 2),
10186GEN_SPEOP_LDST(evlwhos, 0x0B, 2),
10187GEN_SPEOP_LDST(evlwwsplat, 0x0C, 2),
10188GEN_SPEOP_LDST(evlwhsplat, 0x0E, 2),
10189
10190GEN_SPEOP_LDST(evstdd, 0x10, 3),
10191GEN_SPEOP_LDST(evstdw, 0x11, 3),
10192GEN_SPEOP_LDST(evstdh, 0x12, 3),
10193GEN_SPEOP_LDST(evstwhe, 0x18, 2),
10194GEN_SPEOP_LDST(evstwho, 0x1A, 2),
10195GEN_SPEOP_LDST(evstwwe, 0x1C, 2),
10196GEN_SPEOP_LDST(evstwwo, 0x1E, 2),
10197};
10198
0411a972 10199#include "helper_regs.h"
a1389542 10200#include "translate_init.c"
79aceca5 10201
9a64fbe4 10202/*****************************************************************************/
3fc6c082 10203/* Misc PowerPC helpers */
878096ee
AF
10204void ppc_cpu_dump_state(CPUState *cs, FILE *f, fprintf_function cpu_fprintf,
10205 int flags)
79aceca5 10206{
3fc6c082
FB
10207#define RGPL 4
10208#define RFPL 4
3fc6c082 10209
878096ee
AF
10210 PowerPCCPU *cpu = POWERPC_CPU(cs);
10211 CPUPPCState *env = &cpu->env;
79aceca5
FB
10212 int i;
10213
90e189ec 10214 cpu_fprintf(f, "NIP " TARGET_FMT_lx " LR " TARGET_FMT_lx " CTR "
9a78eead 10215 TARGET_FMT_lx " XER " TARGET_FMT_lx "\n",
da91a00f 10216 env->nip, env->lr, env->ctr, cpu_read_xer(env));
90e189ec
BS
10217 cpu_fprintf(f, "MSR " TARGET_FMT_lx " HID0 " TARGET_FMT_lx " HF "
10218 TARGET_FMT_lx " idx %d\n", env->msr, env->spr[SPR_HID0],
10219 env->hflags, env->mmu_idx);
d9bce9d9 10220#if !defined(NO_TIMER_DUMP)
9a78eead 10221 cpu_fprintf(f, "TB %08" PRIu32 " %08" PRIu64
76a66253 10222#if !defined(CONFIG_USER_ONLY)
9a78eead 10223 " DECR %08" PRIu32
76a66253
JM
10224#endif
10225 "\n",
077fc206 10226 cpu_ppc_load_tbu(env), cpu_ppc_load_tbl(env)
76a66253
JM
10227#if !defined(CONFIG_USER_ONLY)
10228 , cpu_ppc_load_decr(env)
10229#endif
10230 );
077fc206 10231#endif
76a66253 10232 for (i = 0; i < 32; i++) {
3fc6c082
FB
10233 if ((i & (RGPL - 1)) == 0)
10234 cpu_fprintf(f, "GPR%02d", i);
b11ebf64 10235 cpu_fprintf(f, " %016" PRIx64, ppc_dump_gpr(env, i));
3fc6c082 10236 if ((i & (RGPL - 1)) == (RGPL - 1))
7fe48483 10237 cpu_fprintf(f, "\n");
76a66253 10238 }
3fc6c082 10239 cpu_fprintf(f, "CR ");
76a66253 10240 for (i = 0; i < 8; i++)
7fe48483
FB
10241 cpu_fprintf(f, "%01x", env->crf[i]);
10242 cpu_fprintf(f, " [");
76a66253
JM
10243 for (i = 0; i < 8; i++) {
10244 char a = '-';
10245 if (env->crf[i] & 0x08)
10246 a = 'L';
10247 else if (env->crf[i] & 0x04)
10248 a = 'G';
10249 else if (env->crf[i] & 0x02)
10250 a = 'E';
7fe48483 10251 cpu_fprintf(f, " %c%c", a, env->crf[i] & 0x01 ? 'O' : ' ');
76a66253 10252 }
90e189ec
BS
10253 cpu_fprintf(f, " ] RES " TARGET_FMT_lx "\n",
10254 env->reserve_addr);
3fc6c082
FB
10255 for (i = 0; i < 32; i++) {
10256 if ((i & (RFPL - 1)) == 0)
10257 cpu_fprintf(f, "FPR%02d", i);
26a76461 10258 cpu_fprintf(f, " %016" PRIx64, *((uint64_t *)&env->fpr[i]));
3fc6c082 10259 if ((i & (RFPL - 1)) == (RFPL - 1))
7fe48483 10260 cpu_fprintf(f, "\n");
79aceca5 10261 }
30304420 10262 cpu_fprintf(f, "FPSCR " TARGET_FMT_lx "\n", env->fpscr);
f2e63a42 10263#if !defined(CONFIG_USER_ONLY)
90dc8812
SW
10264 cpu_fprintf(f, " SRR0 " TARGET_FMT_lx " SRR1 " TARGET_FMT_lx
10265 " PVR " TARGET_FMT_lx " VRSAVE " TARGET_FMT_lx "\n",
10266 env->spr[SPR_SRR0], env->spr[SPR_SRR1],
10267 env->spr[SPR_PVR], env->spr[SPR_VRSAVE]);
10268
10269 cpu_fprintf(f, "SPRG0 " TARGET_FMT_lx " SPRG1 " TARGET_FMT_lx
10270 " SPRG2 " TARGET_FMT_lx " SPRG3 " TARGET_FMT_lx "\n",
10271 env->spr[SPR_SPRG0], env->spr[SPR_SPRG1],
10272 env->spr[SPR_SPRG2], env->spr[SPR_SPRG3]);
10273
10274 cpu_fprintf(f, "SPRG4 " TARGET_FMT_lx " SPRG5 " TARGET_FMT_lx
10275 " SPRG6 " TARGET_FMT_lx " SPRG7 " TARGET_FMT_lx "\n",
10276 env->spr[SPR_SPRG4], env->spr[SPR_SPRG5],
10277 env->spr[SPR_SPRG6], env->spr[SPR_SPRG7]);
10278
10279 if (env->excp_model == POWERPC_EXCP_BOOKE) {
10280 cpu_fprintf(f, "CSRR0 " TARGET_FMT_lx " CSRR1 " TARGET_FMT_lx
10281 " MCSRR0 " TARGET_FMT_lx " MCSRR1 " TARGET_FMT_lx "\n",
10282 env->spr[SPR_BOOKE_CSRR0], env->spr[SPR_BOOKE_CSRR1],
10283 env->spr[SPR_BOOKE_MCSRR0], env->spr[SPR_BOOKE_MCSRR1]);
10284
10285 cpu_fprintf(f, " TCR " TARGET_FMT_lx " TSR " TARGET_FMT_lx
10286 " ESR " TARGET_FMT_lx " DEAR " TARGET_FMT_lx "\n",
10287 env->spr[SPR_BOOKE_TCR], env->spr[SPR_BOOKE_TSR],
10288 env->spr[SPR_BOOKE_ESR], env->spr[SPR_BOOKE_DEAR]);
10289
10290 cpu_fprintf(f, " PIR " TARGET_FMT_lx " DECAR " TARGET_FMT_lx
10291 " IVPR " TARGET_FMT_lx " EPCR " TARGET_FMT_lx "\n",
10292 env->spr[SPR_BOOKE_PIR], env->spr[SPR_BOOKE_DECAR],
10293 env->spr[SPR_BOOKE_IVPR], env->spr[SPR_BOOKE_EPCR]);
10294
10295 cpu_fprintf(f, " MCSR " TARGET_FMT_lx " SPRG8 " TARGET_FMT_lx
10296 " EPR " TARGET_FMT_lx "\n",
10297 env->spr[SPR_BOOKE_MCSR], env->spr[SPR_BOOKE_SPRG8],
10298 env->spr[SPR_BOOKE_EPR]);
10299
10300 /* FSL-specific */
10301 cpu_fprintf(f, " MCAR " TARGET_FMT_lx " PID1 " TARGET_FMT_lx
10302 " PID2 " TARGET_FMT_lx " SVR " TARGET_FMT_lx "\n",
10303 env->spr[SPR_Exxx_MCAR], env->spr[SPR_BOOKE_PID1],
10304 env->spr[SPR_BOOKE_PID2], env->spr[SPR_E500_SVR]);
10305
10306 /*
10307 * IVORs are left out as they are large and do not change often --
10308 * they can be read with "p $ivor0", "p $ivor1", etc.
10309 */
10310 }
10311
697ab892
DG
10312#if defined(TARGET_PPC64)
10313 if (env->flags & POWERPC_FLAG_CFAR) {
10314 cpu_fprintf(f, " CFAR " TARGET_FMT_lx"\n", env->cfar);
10315 }
10316#endif
10317
90dc8812
SW
10318 switch (env->mmu_model) {
10319 case POWERPC_MMU_32B:
10320 case POWERPC_MMU_601:
10321 case POWERPC_MMU_SOFT_6xx:
10322 case POWERPC_MMU_SOFT_74xx:
10323#if defined(TARGET_PPC64)
90dc8812 10324 case POWERPC_MMU_64B:
ca480de6
AB
10325 case POWERPC_MMU_2_06:
10326 case POWERPC_MMU_2_06a:
10327 case POWERPC_MMU_2_06d:
90dc8812 10328#endif
ca480de6
AB
10329 cpu_fprintf(f, " SDR1 " TARGET_FMT_lx " DAR " TARGET_FMT_lx
10330 " DSISR " TARGET_FMT_lx "\n", env->spr[SPR_SDR1],
10331 env->spr[SPR_DAR], env->spr[SPR_DSISR]);
90dc8812 10332 break;
01662f3e 10333 case POWERPC_MMU_BOOKE206:
90dc8812
SW
10334 cpu_fprintf(f, " MAS0 " TARGET_FMT_lx " MAS1 " TARGET_FMT_lx
10335 " MAS2 " TARGET_FMT_lx " MAS3 " TARGET_FMT_lx "\n",
10336 env->spr[SPR_BOOKE_MAS0], env->spr[SPR_BOOKE_MAS1],
10337 env->spr[SPR_BOOKE_MAS2], env->spr[SPR_BOOKE_MAS3]);
10338
10339 cpu_fprintf(f, " MAS4 " TARGET_FMT_lx " MAS6 " TARGET_FMT_lx
10340 " MAS7 " TARGET_FMT_lx " PID " TARGET_FMT_lx "\n",
10341 env->spr[SPR_BOOKE_MAS4], env->spr[SPR_BOOKE_MAS6],
10342 env->spr[SPR_BOOKE_MAS7], env->spr[SPR_BOOKE_PID]);
10343
10344 cpu_fprintf(f, "MMUCFG " TARGET_FMT_lx " TLB0CFG " TARGET_FMT_lx
10345 " TLB1CFG " TARGET_FMT_lx "\n",
10346 env->spr[SPR_MMUCFG], env->spr[SPR_BOOKE_TLB0CFG],
10347 env->spr[SPR_BOOKE_TLB1CFG]);
10348 break;
10349 default:
10350 break;
10351 }
f2e63a42 10352#endif
79aceca5 10353
3fc6c082
FB
10354#undef RGPL
10355#undef RFPL
79aceca5
FB
10356}
10357
878096ee
AF
10358void ppc_cpu_dump_statistics(CPUState *cs, FILE*f,
10359 fprintf_function cpu_fprintf, int flags)
76a66253
JM
10360{
10361#if defined(DO_PPC_STATISTICS)
878096ee 10362 PowerPCCPU *cpu = POWERPC_CPU(cs);
c227f099 10363 opc_handler_t **t1, **t2, **t3, *handler;
76a66253
JM
10364 int op1, op2, op3;
10365
878096ee 10366 t1 = cpu->env.opcodes;
76a66253
JM
10367 for (op1 = 0; op1 < 64; op1++) {
10368 handler = t1[op1];
10369 if (is_indirect_opcode(handler)) {
10370 t2 = ind_table(handler);
10371 for (op2 = 0; op2 < 32; op2++) {
10372 handler = t2[op2];
10373 if (is_indirect_opcode(handler)) {
10374 t3 = ind_table(handler);
10375 for (op3 = 0; op3 < 32; op3++) {
10376 handler = t3[op3];
10377 if (handler->count == 0)
10378 continue;
10379 cpu_fprintf(f, "%02x %02x %02x (%02x %04d) %16s: "
0bfcd599 10380 "%016" PRIx64 " %" PRId64 "\n",
76a66253
JM
10381 op1, op2, op3, op1, (op3 << 5) | op2,
10382 handler->oname,
10383 handler->count, handler->count);
10384 }
10385 } else {
10386 if (handler->count == 0)
10387 continue;
10388 cpu_fprintf(f, "%02x %02x (%02x %04d) %16s: "
0bfcd599 10389 "%016" PRIx64 " %" PRId64 "\n",
76a66253
JM
10390 op1, op2, op1, op2, handler->oname,
10391 handler->count, handler->count);
10392 }
10393 }
10394 } else {
10395 if (handler->count == 0)
10396 continue;
0bfcd599
BS
10397 cpu_fprintf(f, "%02x (%02x ) %16s: %016" PRIx64
10398 " %" PRId64 "\n",
76a66253
JM
10399 op1, op1, handler->oname,
10400 handler->count, handler->count);
10401 }
10402 }
10403#endif
10404}
10405
9a64fbe4 10406/*****************************************************************************/
213fe1f5 10407static inline void gen_intermediate_code_internal(PowerPCCPU *cpu,
636aa200 10408 TranslationBlock *tb,
213fe1f5 10409 bool search_pc)
79aceca5 10410{
ed2803da 10411 CPUState *cs = CPU(cpu);
213fe1f5 10412 CPUPPCState *env = &cpu->env;
9fddaa0c 10413 DisasContext ctx, *ctxp = &ctx;
c227f099 10414 opc_handler_t **table, *handler;
0fa85d43 10415 target_ulong pc_start;
79aceca5 10416 uint16_t *gen_opc_end;
a1d1bb31 10417 CPUBreakpoint *bp;
79aceca5 10418 int j, lj = -1;
2e70f6ef
PB
10419 int num_insns;
10420 int max_insns;
79aceca5
FB
10421
10422 pc_start = tb->pc;
92414b31 10423 gen_opc_end = tcg_ctx.gen_opc_buf + OPC_MAX_SIZE;
046d6672 10424 ctx.nip = pc_start;
79aceca5 10425 ctx.tb = tb;
e1833e1f 10426 ctx.exception = POWERPC_EXCP_NONE;
3fc6c082 10427 ctx.spr_cb = env->spr_cb;
76db3ba4 10428 ctx.mem_idx = env->mmu_idx;
7d08d856
AJ
10429 ctx.insns_flags = env->insns_flags;
10430 ctx.insns_flags2 = env->insns_flags2;
76db3ba4
AJ
10431 ctx.access_type = -1;
10432 ctx.le_mode = env->hflags & (1 << MSR_LE) ? 1 : 0;
d9bce9d9 10433#if defined(TARGET_PPC64)
e42a61f1 10434 ctx.sf_mode = msr_is_64bit(env, env->msr);
697ab892 10435 ctx.has_cfar = !!(env->flags & POWERPC_FLAG_CFAR);
9a64fbe4 10436#endif
3cc62370 10437 ctx.fpu_enabled = msr_fp;
a9d9eb8f 10438 if ((env->flags & POWERPC_FLAG_SPE) && msr_spe)
d26bfc9a
JM
10439 ctx.spe_enabled = msr_spe;
10440 else
10441 ctx.spe_enabled = 0;
a9d9eb8f
JM
10442 if ((env->flags & POWERPC_FLAG_VRE) && msr_vr)
10443 ctx.altivec_enabled = msr_vr;
10444 else
10445 ctx.altivec_enabled = 0;
1f29871c
TM
10446 if ((env->flags & POWERPC_FLAG_VSX) && msr_vsx) {
10447 ctx.vsx_enabled = msr_vsx;
10448 } else {
10449 ctx.vsx_enabled = 0;
10450 }
d26bfc9a 10451 if ((env->flags & POWERPC_FLAG_SE) && msr_se)
8cbcb4fa 10452 ctx.singlestep_enabled = CPU_SINGLE_STEP;
d26bfc9a 10453 else
8cbcb4fa 10454 ctx.singlestep_enabled = 0;
d26bfc9a 10455 if ((env->flags & POWERPC_FLAG_BE) && msr_be)
8cbcb4fa 10456 ctx.singlestep_enabled |= CPU_BRANCH_STEP;
ed2803da 10457 if (unlikely(cs->singlestep_enabled)) {
8cbcb4fa 10458 ctx.singlestep_enabled |= GDBSTUB_SINGLE_STEP;
ed2803da 10459 }
3fc6c082 10460#if defined (DO_SINGLE_STEP) && 0
9a64fbe4
FB
10461 /* Single step trace mode */
10462 msr_se = 1;
10463#endif
2e70f6ef
PB
10464 num_insns = 0;
10465 max_insns = tb->cflags & CF_COUNT_MASK;
10466 if (max_insns == 0)
10467 max_insns = CF_COUNT_MASK;
10468
806f352d 10469 gen_tb_start();
9a64fbe4 10470 /* Set env in case of segfault during code fetch */
efd7f486
EV
10471 while (ctx.exception == POWERPC_EXCP_NONE
10472 && tcg_ctx.gen_opc_ptr < gen_opc_end) {
72cf2d4f
BS
10473 if (unlikely(!QTAILQ_EMPTY(&env->breakpoints))) {
10474 QTAILQ_FOREACH(bp, &env->breakpoints, entry) {
a1d1bb31 10475 if (bp->pc == ctx.nip) {
e06fcd75 10476 gen_debug_exception(ctxp);
ea4e754f
FB
10477 break;
10478 }
10479 }
10480 }
76a66253 10481 if (unlikely(search_pc)) {
92414b31 10482 j = tcg_ctx.gen_opc_ptr - tcg_ctx.gen_opc_buf;
79aceca5
FB
10483 if (lj < j) {
10484 lj++;
10485 while (lj < j)
ab1103de 10486 tcg_ctx.gen_opc_instr_start[lj++] = 0;
79aceca5 10487 }
25983cad 10488 tcg_ctx.gen_opc_pc[lj] = ctx.nip;
ab1103de 10489 tcg_ctx.gen_opc_instr_start[lj] = 1;
c9c99c22 10490 tcg_ctx.gen_opc_icount[lj] = num_insns;
79aceca5 10491 }
d12d51d5 10492 LOG_DISAS("----------------\n");
90e189ec 10493 LOG_DISAS("nip=" TARGET_FMT_lx " super=%d ir=%d\n",
d12d51d5 10494 ctx.nip, ctx.mem_idx, (int)msr_ir);
2e70f6ef
PB
10495 if (num_insns + 1 == max_insns && (tb->cflags & CF_LAST_IO))
10496 gen_io_start();
76db3ba4 10497 if (unlikely(ctx.le_mode)) {
2f5a189c 10498 ctx.opcode = bswap32(cpu_ldl_code(env, ctx.nip));
056401ea 10499 } else {
2f5a189c 10500 ctx.opcode = cpu_ldl_code(env, ctx.nip);
111bfab3 10501 }
d12d51d5 10502 LOG_DISAS("translate opcode %08x (%02x %02x %02x) (%s)\n",
9a64fbe4 10503 ctx.opcode, opc1(ctx.opcode), opc2(ctx.opcode),
476b6d16 10504 opc3(ctx.opcode), ctx.le_mode ? "little" : "big");
fdefe51c 10505 if (unlikely(qemu_loglevel_mask(CPU_LOG_TB_OP | CPU_LOG_TB_OP_OPT))) {
731c54f8 10506 tcg_gen_debug_insn_start(ctx.nip);
fdefe51c 10507 }
046d6672 10508 ctx.nip += 4;
3fc6c082 10509 table = env->opcodes;
2e70f6ef 10510 num_insns++;
79aceca5
FB
10511 handler = table[opc1(ctx.opcode)];
10512 if (is_indirect_opcode(handler)) {
10513 table = ind_table(handler);
10514 handler = table[opc2(ctx.opcode)];
10515 if (is_indirect_opcode(handler)) {
10516 table = ind_table(handler);
10517 handler = table[opc3(ctx.opcode)];
10518 }
10519 }
10520 /* Is opcode *REALLY* valid ? */
76a66253 10521 if (unlikely(handler->handler == &gen_invalid)) {
93fcfe39
AL
10522 if (qemu_log_enabled()) {
10523 qemu_log("invalid/unsupported opcode: "
90e189ec
BS
10524 "%02x - %02x - %02x (%08x) " TARGET_FMT_lx " %d\n",
10525 opc1(ctx.opcode), opc2(ctx.opcode),
10526 opc3(ctx.opcode), ctx.opcode, ctx.nip - 4, (int)msr_ir);
4b3686fa 10527 }
76a66253 10528 } else {
70560da7
FC
10529 uint32_t inval;
10530
10531 if (unlikely(handler->type & (PPC_SPE | PPC_SPE_SINGLE | PPC_SPE_DOUBLE) && Rc(ctx.opcode))) {
10532 inval = handler->inval2;
10533 } else {
10534 inval = handler->inval1;
10535 }
10536
10537 if (unlikely((ctx.opcode & inval) != 0)) {
93fcfe39
AL
10538 if (qemu_log_enabled()) {
10539 qemu_log("invalid bits: %08x for opcode: "
90e189ec 10540 "%02x - %02x - %02x (%08x) " TARGET_FMT_lx "\n",
70560da7 10541 ctx.opcode & inval, opc1(ctx.opcode),
90e189ec
BS
10542 opc2(ctx.opcode), opc3(ctx.opcode),
10543 ctx.opcode, ctx.nip - 4);
76a66253 10544 }
e06fcd75 10545 gen_inval_exception(ctxp, POWERPC_EXCP_INVAL_INVAL);
4b3686fa 10546 break;
79aceca5 10547 }
79aceca5 10548 }
4b3686fa 10549 (*(handler->handler))(&ctx);
76a66253
JM
10550#if defined(DO_PPC_STATISTICS)
10551 handler->count++;
10552#endif
9a64fbe4 10553 /* Check trace mode exceptions */
8cbcb4fa
AJ
10554 if (unlikely(ctx.singlestep_enabled & CPU_SINGLE_STEP &&
10555 (ctx.nip <= 0x100 || ctx.nip > 0xF00) &&
10556 ctx.exception != POWERPC_SYSCALL &&
10557 ctx.exception != POWERPC_EXCP_TRAP &&
10558 ctx.exception != POWERPC_EXCP_BRANCH)) {
e06fcd75 10559 gen_exception(ctxp, POWERPC_EXCP_TRACE);
d26bfc9a 10560 } else if (unlikely(((ctx.nip & (TARGET_PAGE_SIZE - 1)) == 0) ||
ed2803da 10561 (cs->singlestep_enabled) ||
1b530a6d 10562 singlestep ||
2e70f6ef 10563 num_insns >= max_insns)) {
d26bfc9a
JM
10564 /* if we reach a page boundary or are single stepping, stop
10565 * generation
10566 */
8dd4983c 10567 break;
76a66253 10568 }
3fc6c082 10569 }
2e70f6ef
PB
10570 if (tb->cflags & CF_LAST_IO)
10571 gen_io_end();
e1833e1f 10572 if (ctx.exception == POWERPC_EXCP_NONE) {
c1942362 10573 gen_goto_tb(&ctx, 0, ctx.nip);
e1833e1f 10574 } else if (ctx.exception != POWERPC_EXCP_BRANCH) {
ed2803da 10575 if (unlikely(cs->singlestep_enabled)) {
e06fcd75 10576 gen_debug_exception(ctxp);
8cbcb4fa 10577 }
76a66253 10578 /* Generate the return instruction */
57fec1fe 10579 tcg_gen_exit_tb(0);
9a64fbe4 10580 }
806f352d 10581 gen_tb_end(tb, num_insns);
efd7f486 10582 *tcg_ctx.gen_opc_ptr = INDEX_op_end;
76a66253 10583 if (unlikely(search_pc)) {
92414b31 10584 j = tcg_ctx.gen_opc_ptr - tcg_ctx.gen_opc_buf;
9a64fbe4
FB
10585 lj++;
10586 while (lj <= j)
ab1103de 10587 tcg_ctx.gen_opc_instr_start[lj++] = 0;
9a64fbe4 10588 } else {
046d6672 10589 tb->size = ctx.nip - pc_start;
2e70f6ef 10590 tb->icount = num_insns;
9a64fbe4 10591 }
d9bce9d9 10592#if defined(DEBUG_DISAS)
8fec2b8c 10593 if (qemu_loglevel_mask(CPU_LOG_TB_IN_ASM)) {
76a66253 10594 int flags;
237c0af0 10595 flags = env->bfd_mach;
76db3ba4 10596 flags |= ctx.le_mode << 16;
93fcfe39 10597 qemu_log("IN: %s\n", lookup_symbol(pc_start));
f4359b9f 10598 log_target_disas(env, pc_start, ctx.nip - pc_start, flags);
93fcfe39 10599 qemu_log("\n");
9fddaa0c 10600 }
79aceca5 10601#endif
79aceca5
FB
10602}
10603
1328c2bf 10604void gen_intermediate_code (CPUPPCState *env, struct TranslationBlock *tb)
79aceca5 10605{
213fe1f5 10606 gen_intermediate_code_internal(ppc_env_get_cpu(env), tb, false);
79aceca5
FB
10607}
10608
1328c2bf 10609void gen_intermediate_code_pc (CPUPPCState *env, struct TranslationBlock *tb)
79aceca5 10610{
213fe1f5 10611 gen_intermediate_code_internal(ppc_env_get_cpu(env), tb, true);
79aceca5 10612}
d2856f1a 10613
1328c2bf 10614void restore_state_to_opc(CPUPPCState *env, TranslationBlock *tb, int pc_pos)
d2856f1a 10615{
25983cad 10616 env->nip = tcg_ctx.gen_opc_pc[pc_pos];
d2856f1a 10617}
This page took 2.781095 seconds and 4 git commands to generate.