]> git.ipfire.org Git - thirdparty/qemu.git/blame - target-ppc/translate.c
target-ppc: Add ISA 2.06 divweu[o] Instructions
[thirdparty/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);
98d1eb27
TM
987
988/* div[wd]eu[o][.] */
989#define GEN_DIVE(name, hlpr, compute_ov) \
990static void gen_##name(DisasContext *ctx) \
991{ \
992 TCGv_i32 t0 = tcg_const_i32(compute_ov); \
993 gen_helper_##hlpr(cpu_gpr[rD(ctx->opcode)], cpu_env, \
994 cpu_gpr[rA(ctx->opcode)], cpu_gpr[rB(ctx->opcode)], t0); \
995 tcg_temp_free_i32(t0); \
996 if (unlikely(Rc(ctx->opcode) != 0)) { \
997 gen_set_Rc0(ctx, cpu_gpr[rD(ctx->opcode)]); \
998 } \
999}
1000
6a4fda33
TM
1001GEN_DIVE(divweu, divweu, 0);
1002GEN_DIVE(divweuo, divweu, 1);
1003
d9bce9d9 1004#if defined(TARGET_PPC64)
636aa200
BS
1005static inline void gen_op_arith_divd(DisasContext *ctx, TCGv ret, TCGv arg1,
1006 TCGv arg2, int sign, int compute_ov)
d9bce9d9 1007{
2ef1b120
AJ
1008 int l1 = gen_new_label();
1009 int l2 = gen_new_label();
74637406
AJ
1010
1011 tcg_gen_brcondi_i64(TCG_COND_EQ, arg2, 0, l1);
1012 if (sign) {
2ef1b120 1013 int l3 = gen_new_label();
74637406
AJ
1014 tcg_gen_brcondi_i64(TCG_COND_NE, arg2, -1, l3);
1015 tcg_gen_brcondi_i64(TCG_COND_EQ, arg1, INT64_MIN, l1);
1016 gen_set_label(l3);
74637406
AJ
1017 tcg_gen_div_i64(ret, arg1, arg2);
1018 } else {
1019 tcg_gen_divu_i64(ret, arg1, arg2);
1020 }
1021 if (compute_ov) {
da91a00f 1022 tcg_gen_movi_tl(cpu_ov, 0);
74637406
AJ
1023 }
1024 tcg_gen_br(l2);
1025 gen_set_label(l1);
1026 if (sign) {
1027 tcg_gen_sari_i64(ret, arg1, 63);
1028 } else {
1029 tcg_gen_movi_i64(ret, 0);
1030 }
1031 if (compute_ov) {
da91a00f
RH
1032 tcg_gen_movi_tl(cpu_ov, 1);
1033 tcg_gen_movi_tl(cpu_so, 1);
74637406
AJ
1034 }
1035 gen_set_label(l2);
1036 if (unlikely(Rc(ctx->opcode) != 0))
1037 gen_set_Rc0(ctx, ret);
d9bce9d9 1038}
74637406 1039#define GEN_INT_ARITH_DIVD(name, opc3, sign, compute_ov) \
99e300ef 1040static void glue(gen_, name)(DisasContext *ctx) \
74637406 1041{ \
2ef1b120
AJ
1042 gen_op_arith_divd(ctx, cpu_gpr[rD(ctx->opcode)], \
1043 cpu_gpr[rA(ctx->opcode)], cpu_gpr[rB(ctx->opcode)], \
1044 sign, compute_ov); \
74637406
AJ
1045}
1046/* divwu divwu. divwuo divwuo. */
1047GEN_INT_ARITH_DIVD(divdu, 0x0E, 0, 0);
1048GEN_INT_ARITH_DIVD(divduo, 0x1E, 0, 1);
1049/* divw divw. divwo divwo. */
1050GEN_INT_ARITH_DIVD(divd, 0x0F, 1, 0);
1051GEN_INT_ARITH_DIVD(divdo, 0x1F, 1, 1);
98d1eb27
TM
1052
1053GEN_DIVE(divdeu, divdeu, 0);
1054GEN_DIVE(divdeuo, divdeu, 1);
e44259b6
TM
1055GEN_DIVE(divde, divde, 0);
1056GEN_DIVE(divdeo, divde, 1);
d9bce9d9 1057#endif
74637406
AJ
1058
1059/* mulhw mulhw. */
99e300ef 1060static void gen_mulhw(DisasContext *ctx)
d9bce9d9 1061{
23ad1d5d
RH
1062 TCGv_i32 t0 = tcg_temp_new_i32();
1063 TCGv_i32 t1 = tcg_temp_new_i32();
74637406 1064
23ad1d5d
RH
1065 tcg_gen_trunc_tl_i32(t0, cpu_gpr[rA(ctx->opcode)]);
1066 tcg_gen_trunc_tl_i32(t1, cpu_gpr[rB(ctx->opcode)]);
1067 tcg_gen_muls2_i32(t0, t1, t0, t1);
1068 tcg_gen_extu_i32_tl(cpu_gpr[rD(ctx->opcode)], t1);
1069 tcg_temp_free_i32(t0);
1070 tcg_temp_free_i32(t1);
74637406
AJ
1071 if (unlikely(Rc(ctx->opcode) != 0))
1072 gen_set_Rc0(ctx, cpu_gpr[rD(ctx->opcode)]);
d9bce9d9 1073}
99e300ef 1074
54623277 1075/* mulhwu mulhwu. */
99e300ef 1076static void gen_mulhwu(DisasContext *ctx)
d9bce9d9 1077{
23ad1d5d
RH
1078 TCGv_i32 t0 = tcg_temp_new_i32();
1079 TCGv_i32 t1 = tcg_temp_new_i32();
74637406 1080
23ad1d5d
RH
1081 tcg_gen_trunc_tl_i32(t0, cpu_gpr[rA(ctx->opcode)]);
1082 tcg_gen_trunc_tl_i32(t1, cpu_gpr[rB(ctx->opcode)]);
1083 tcg_gen_mulu2_i32(t0, t1, t0, t1);
1084 tcg_gen_extu_i32_tl(cpu_gpr[rD(ctx->opcode)], t1);
1085 tcg_temp_free_i32(t0);
1086 tcg_temp_free_i32(t1);
74637406
AJ
1087 if (unlikely(Rc(ctx->opcode) != 0))
1088 gen_set_Rc0(ctx, cpu_gpr[rD(ctx->opcode)]);
d9bce9d9 1089}
99e300ef 1090
54623277 1091/* mullw mullw. */
99e300ef 1092static void gen_mullw(DisasContext *ctx)
d9bce9d9 1093{
74637406
AJ
1094 tcg_gen_mul_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)],
1095 cpu_gpr[rB(ctx->opcode)]);
1e4c090f 1096 tcg_gen_ext32s_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rD(ctx->opcode)]);
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/* mullwo mullwo. */
99e300ef 1102static void gen_mullwo(DisasContext *ctx)
d9bce9d9 1103{
e4a2c846
RH
1104 TCGv_i32 t0 = tcg_temp_new_i32();
1105 TCGv_i32 t1 = tcg_temp_new_i32();
74637406 1106
e4a2c846
RH
1107 tcg_gen_trunc_tl_i32(t0, cpu_gpr[rA(ctx->opcode)]);
1108 tcg_gen_trunc_tl_i32(t1, cpu_gpr[rB(ctx->opcode)]);
1109 tcg_gen_muls2_i32(t0, t1, t0, t1);
1110 tcg_gen_ext_i32_tl(cpu_gpr[rD(ctx->opcode)], t0);
1111
1112 tcg_gen_sari_i32(t0, t0, 31);
1113 tcg_gen_setcond_i32(TCG_COND_NE, t0, t0, t1);
1114 tcg_gen_extu_i32_tl(cpu_ov, t0);
1115 tcg_gen_or_tl(cpu_so, cpu_so, cpu_ov);
1116
1117 tcg_temp_free_i32(t0);
1118 tcg_temp_free_i32(t1);
74637406
AJ
1119 if (unlikely(Rc(ctx->opcode) != 0))
1120 gen_set_Rc0(ctx, cpu_gpr[rD(ctx->opcode)]);
d9bce9d9 1121}
99e300ef 1122
54623277 1123/* mulli */
99e300ef 1124static void gen_mulli(DisasContext *ctx)
d9bce9d9 1125{
74637406
AJ
1126 tcg_gen_muli_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)],
1127 SIMM(ctx->opcode));
d9bce9d9 1128}
23ad1d5d 1129
d9bce9d9 1130#if defined(TARGET_PPC64)
74637406 1131/* mulhd mulhd. */
23ad1d5d
RH
1132static void gen_mulhd(DisasContext *ctx)
1133{
1134 TCGv lo = tcg_temp_new();
1135 tcg_gen_muls2_tl(lo, cpu_gpr[rD(ctx->opcode)],
1136 cpu_gpr[rA(ctx->opcode)], cpu_gpr[rB(ctx->opcode)]);
1137 tcg_temp_free(lo);
1138 if (unlikely(Rc(ctx->opcode) != 0)) {
1139 gen_set_Rc0(ctx, cpu_gpr[rD(ctx->opcode)]);
1140 }
1141}
1142
74637406 1143/* mulhdu mulhdu. */
23ad1d5d
RH
1144static void gen_mulhdu(DisasContext *ctx)
1145{
1146 TCGv lo = tcg_temp_new();
1147 tcg_gen_mulu2_tl(lo, cpu_gpr[rD(ctx->opcode)],
1148 cpu_gpr[rA(ctx->opcode)], cpu_gpr[rB(ctx->opcode)]);
1149 tcg_temp_free(lo);
1150 if (unlikely(Rc(ctx->opcode) != 0)) {
1151 gen_set_Rc0(ctx, cpu_gpr[rD(ctx->opcode)]);
1152 }
1153}
99e300ef 1154
54623277 1155/* mulld mulld. */
99e300ef 1156static void gen_mulld(DisasContext *ctx)
d9bce9d9 1157{
74637406
AJ
1158 tcg_gen_mul_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)],
1159 cpu_gpr[rB(ctx->opcode)]);
1160 if (unlikely(Rc(ctx->opcode) != 0))
1161 gen_set_Rc0(ctx, cpu_gpr[rD(ctx->opcode)]);
d9bce9d9 1162}
d15f74fb 1163
74637406 1164/* mulldo mulldo. */
d15f74fb
BS
1165static void gen_mulldo(DisasContext *ctx)
1166{
1167 gen_helper_mulldo(cpu_gpr[rD(ctx->opcode)], cpu_env,
1168 cpu_gpr[rA(ctx->opcode)], cpu_gpr[rB(ctx->opcode)]);
1169 if (unlikely(Rc(ctx->opcode) != 0)) {
1170 gen_set_Rc0(ctx, cpu_gpr[rD(ctx->opcode)]);
1171 }
1172}
d9bce9d9 1173#endif
74637406 1174
74637406 1175/* Common subf function */
636aa200 1176static inline void gen_op_arith_subf(DisasContext *ctx, TCGv ret, TCGv arg1,
b5a73f8d
RH
1177 TCGv arg2, bool add_ca, bool compute_ca,
1178 bool compute_ov, bool compute_rc0)
79aceca5 1179{
b5a73f8d 1180 TCGv t0 = ret;
79aceca5 1181
752d634e 1182 if (compute_ca || compute_ov) {
b5a73f8d 1183 t0 = tcg_temp_new();
da91a00f 1184 }
74637406 1185
79482e5a
RH
1186 if (compute_ca) {
1187 /* dest = ~arg1 + arg2 [+ ca]. */
1188 if (NARROW_MODE(ctx)) {
752d634e
RH
1189 /* Caution: a non-obvious corner case of the spec is that we
1190 must produce the *entire* 64-bit addition, but produce the
1191 carry into bit 32. */
79482e5a 1192 TCGv inv1 = tcg_temp_new();
752d634e 1193 TCGv t1 = tcg_temp_new();
79482e5a 1194 tcg_gen_not_tl(inv1, arg1);
79482e5a 1195 if (add_ca) {
752d634e 1196 tcg_gen_add_tl(t0, arg2, cpu_ca);
79482e5a 1197 } else {
752d634e 1198 tcg_gen_addi_tl(t0, arg2, 1);
79482e5a 1199 }
752d634e 1200 tcg_gen_xor_tl(t1, arg2, inv1); /* add without carry */
79482e5a 1201 tcg_gen_add_tl(t0, t0, inv1);
752d634e
RH
1202 tcg_gen_xor_tl(cpu_ca, t0, t1); /* bits changes w/ carry */
1203 tcg_temp_free(t1);
1204 tcg_gen_shri_tl(cpu_ca, cpu_ca, 32); /* extract bit 32 */
1205 tcg_gen_andi_tl(cpu_ca, cpu_ca, 1);
79482e5a 1206 } else if (add_ca) {
08f4a0f7
RH
1207 TCGv zero, inv1 = tcg_temp_new();
1208 tcg_gen_not_tl(inv1, arg1);
b5a73f8d
RH
1209 zero = tcg_const_tl(0);
1210 tcg_gen_add2_tl(t0, cpu_ca, arg2, zero, cpu_ca, zero);
08f4a0f7 1211 tcg_gen_add2_tl(t0, cpu_ca, t0, cpu_ca, inv1, zero);
b5a73f8d 1212 tcg_temp_free(zero);
08f4a0f7 1213 tcg_temp_free(inv1);
b5a73f8d 1214 } else {
79482e5a 1215 tcg_gen_setcond_tl(TCG_COND_GEU, cpu_ca, arg2, arg1);
b5a73f8d 1216 tcg_gen_sub_tl(t0, arg2, arg1);
b5a73f8d 1217 }
79482e5a
RH
1218 } else if (add_ca) {
1219 /* Since we're ignoring carry-out, we can simplify the
1220 standard ~arg1 + arg2 + ca to arg2 - arg1 + ca - 1. */
1221 tcg_gen_sub_tl(t0, arg2, arg1);
1222 tcg_gen_add_tl(t0, t0, cpu_ca);
1223 tcg_gen_subi_tl(t0, t0, 1);
79aceca5 1224 } else {
b5a73f8d 1225 tcg_gen_sub_tl(t0, arg2, arg1);
74637406 1226 }
b5a73f8d 1227
74637406
AJ
1228 if (compute_ov) {
1229 gen_op_arith_compute_ov(ctx, t0, arg1, arg2, 1);
1230 }
b5a73f8d 1231 if (unlikely(compute_rc0)) {
74637406 1232 gen_set_Rc0(ctx, t0);
b5a73f8d 1233 }
74637406 1234
a7812ae4 1235 if (!TCGV_EQUAL(t0, ret)) {
74637406
AJ
1236 tcg_gen_mov_tl(ret, t0);
1237 tcg_temp_free(t0);
79aceca5 1238 }
79aceca5 1239}
74637406
AJ
1240/* Sub functions with Two operands functions */
1241#define GEN_INT_ARITH_SUBF(name, opc3, add_ca, compute_ca, compute_ov) \
b5a73f8d 1242static void glue(gen_, name)(DisasContext *ctx) \
74637406
AJ
1243{ \
1244 gen_op_arith_subf(ctx, cpu_gpr[rD(ctx->opcode)], \
1245 cpu_gpr[rA(ctx->opcode)], cpu_gpr[rB(ctx->opcode)], \
b5a73f8d 1246 add_ca, compute_ca, compute_ov, Rc(ctx->opcode)); \
74637406
AJ
1247}
1248/* Sub functions with one operand and one immediate */
1249#define GEN_INT_ARITH_SUBF_CONST(name, opc3, const_val, \
1250 add_ca, compute_ca, compute_ov) \
b5a73f8d 1251static void glue(gen_, name)(DisasContext *ctx) \
74637406 1252{ \
b5a73f8d 1253 TCGv t0 = tcg_const_tl(const_val); \
74637406
AJ
1254 gen_op_arith_subf(ctx, cpu_gpr[rD(ctx->opcode)], \
1255 cpu_gpr[rA(ctx->opcode)], t0, \
b5a73f8d 1256 add_ca, compute_ca, compute_ov, Rc(ctx->opcode)); \
74637406
AJ
1257 tcg_temp_free(t0); \
1258}
1259/* subf subf. subfo subfo. */
1260GEN_INT_ARITH_SUBF(subf, 0x01, 0, 0, 0)
1261GEN_INT_ARITH_SUBF(subfo, 0x11, 0, 0, 1)
1262/* subfc subfc. subfco subfco. */
1263GEN_INT_ARITH_SUBF(subfc, 0x00, 0, 1, 0)
1264GEN_INT_ARITH_SUBF(subfco, 0x10, 0, 1, 1)
1265/* subfe subfe. subfeo subfo. */
1266GEN_INT_ARITH_SUBF(subfe, 0x04, 1, 1, 0)
1267GEN_INT_ARITH_SUBF(subfeo, 0x14, 1, 1, 1)
1268/* subfme subfme. subfmeo subfmeo. */
1269GEN_INT_ARITH_SUBF_CONST(subfme, 0x07, -1LL, 1, 1, 0)
1270GEN_INT_ARITH_SUBF_CONST(subfmeo, 0x17, -1LL, 1, 1, 1)
1271/* subfze subfze. subfzeo subfzeo.*/
1272GEN_INT_ARITH_SUBF_CONST(subfze, 0x06, 0, 1, 1, 0)
1273GEN_INT_ARITH_SUBF_CONST(subfzeo, 0x16, 0, 1, 1, 1)
99e300ef 1274
54623277 1275/* subfic */
99e300ef 1276static void gen_subfic(DisasContext *ctx)
79aceca5 1277{
b5a73f8d
RH
1278 TCGv c = tcg_const_tl(SIMM(ctx->opcode));
1279 gen_op_arith_subf(ctx, cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)],
1280 c, 0, 1, 0, 0);
1281 tcg_temp_free(c);
79aceca5
FB
1282}
1283
fd3f0081
RH
1284/* neg neg. nego nego. */
1285static inline void gen_op_arith_neg(DisasContext *ctx, bool compute_ov)
1286{
1287 TCGv zero = tcg_const_tl(0);
1288 gen_op_arith_subf(ctx, cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)],
1289 zero, 0, 0, compute_ov, Rc(ctx->opcode));
1290 tcg_temp_free(zero);
1291}
1292
1293static void gen_neg(DisasContext *ctx)
1294{
1295 gen_op_arith_neg(ctx, 0);
1296}
1297
1298static void gen_nego(DisasContext *ctx)
1299{
1300 gen_op_arith_neg(ctx, 1);
1301}
1302
79aceca5 1303/*** Integer logical ***/
26d67362 1304#define GEN_LOGICAL2(name, tcg_op, opc, type) \
99e300ef 1305static void glue(gen_, name)(DisasContext *ctx) \
79aceca5 1306{ \
26d67362
AJ
1307 tcg_op(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)], \
1308 cpu_gpr[rB(ctx->opcode)]); \
76a66253 1309 if (unlikely(Rc(ctx->opcode) != 0)) \
26d67362 1310 gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]); \
79aceca5 1311}
79aceca5 1312
26d67362 1313#define GEN_LOGICAL1(name, tcg_op, opc, type) \
99e300ef 1314static void glue(gen_, name)(DisasContext *ctx) \
79aceca5 1315{ \
26d67362 1316 tcg_op(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)]); \
76a66253 1317 if (unlikely(Rc(ctx->opcode) != 0)) \
26d67362 1318 gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]); \
79aceca5
FB
1319}
1320
1321/* and & and. */
26d67362 1322GEN_LOGICAL2(and, tcg_gen_and_tl, 0x00, PPC_INTEGER);
79aceca5 1323/* andc & andc. */
26d67362 1324GEN_LOGICAL2(andc, tcg_gen_andc_tl, 0x01, PPC_INTEGER);
e8eaa2c0 1325
54623277 1326/* andi. */
e8eaa2c0 1327static void gen_andi_(DisasContext *ctx)
79aceca5 1328{
26d67362
AJ
1329 tcg_gen_andi_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)], UIMM(ctx->opcode));
1330 gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
79aceca5 1331}
e8eaa2c0 1332
54623277 1333/* andis. */
e8eaa2c0 1334static void gen_andis_(DisasContext *ctx)
79aceca5 1335{
26d67362
AJ
1336 tcg_gen_andi_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)], UIMM(ctx->opcode) << 16);
1337 gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
79aceca5 1338}
99e300ef 1339
54623277 1340/* cntlzw */
99e300ef 1341static void gen_cntlzw(DisasContext *ctx)
26d67362 1342{
a7812ae4 1343 gen_helper_cntlzw(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)]);
26d67362 1344 if (unlikely(Rc(ctx->opcode) != 0))
2e31f5d3 1345 gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
26d67362 1346}
79aceca5 1347/* eqv & eqv. */
26d67362 1348GEN_LOGICAL2(eqv, tcg_gen_eqv_tl, 0x08, PPC_INTEGER);
79aceca5 1349/* extsb & extsb. */
26d67362 1350GEN_LOGICAL1(extsb, tcg_gen_ext8s_tl, 0x1D, PPC_INTEGER);
79aceca5 1351/* extsh & extsh. */
26d67362 1352GEN_LOGICAL1(extsh, tcg_gen_ext16s_tl, 0x1C, PPC_INTEGER);
79aceca5 1353/* nand & nand. */
26d67362 1354GEN_LOGICAL2(nand, tcg_gen_nand_tl, 0x0E, PPC_INTEGER);
79aceca5 1355/* nor & nor. */
26d67362 1356GEN_LOGICAL2(nor, tcg_gen_nor_tl, 0x03, PPC_INTEGER);
99e300ef 1357
54623277 1358/* or & or. */
99e300ef 1359static void gen_or(DisasContext *ctx)
9a64fbe4 1360{
76a66253
JM
1361 int rs, ra, rb;
1362
1363 rs = rS(ctx->opcode);
1364 ra = rA(ctx->opcode);
1365 rb = rB(ctx->opcode);
1366 /* Optimisation for mr. ri case */
1367 if (rs != ra || rs != rb) {
26d67362
AJ
1368 if (rs != rb)
1369 tcg_gen_or_tl(cpu_gpr[ra], cpu_gpr[rs], cpu_gpr[rb]);
1370 else
1371 tcg_gen_mov_tl(cpu_gpr[ra], cpu_gpr[rs]);
76a66253 1372 if (unlikely(Rc(ctx->opcode) != 0))
26d67362 1373 gen_set_Rc0(ctx, cpu_gpr[ra]);
76a66253 1374 } else if (unlikely(Rc(ctx->opcode) != 0)) {
26d67362 1375 gen_set_Rc0(ctx, cpu_gpr[rs]);
c80f84e3
JM
1376#if defined(TARGET_PPC64)
1377 } else {
26d67362
AJ
1378 int prio = 0;
1379
c80f84e3
JM
1380 switch (rs) {
1381 case 1:
1382 /* Set process priority to low */
26d67362 1383 prio = 2;
c80f84e3
JM
1384 break;
1385 case 6:
1386 /* Set process priority to medium-low */
26d67362 1387 prio = 3;
c80f84e3
JM
1388 break;
1389 case 2:
1390 /* Set process priority to normal */
26d67362 1391 prio = 4;
c80f84e3 1392 break;
be147d08
JM
1393#if !defined(CONFIG_USER_ONLY)
1394 case 31:
76db3ba4 1395 if (ctx->mem_idx > 0) {
be147d08 1396 /* Set process priority to very low */
26d67362 1397 prio = 1;
be147d08
JM
1398 }
1399 break;
1400 case 5:
76db3ba4 1401 if (ctx->mem_idx > 0) {
be147d08 1402 /* Set process priority to medium-hight */
26d67362 1403 prio = 5;
be147d08
JM
1404 }
1405 break;
1406 case 3:
76db3ba4 1407 if (ctx->mem_idx > 0) {
be147d08 1408 /* Set process priority to high */
26d67362 1409 prio = 6;
be147d08
JM
1410 }
1411 break;
be147d08 1412 case 7:
76db3ba4 1413 if (ctx->mem_idx > 1) {
be147d08 1414 /* Set process priority to very high */
26d67362 1415 prio = 7;
be147d08
JM
1416 }
1417 break;
be147d08 1418#endif
c80f84e3
JM
1419 default:
1420 /* nop */
1421 break;
1422 }
26d67362 1423 if (prio) {
a7812ae4 1424 TCGv t0 = tcg_temp_new();
54cdcae6 1425 gen_load_spr(t0, SPR_PPR);
ea363694
AJ
1426 tcg_gen_andi_tl(t0, t0, ~0x001C000000000000ULL);
1427 tcg_gen_ori_tl(t0, t0, ((uint64_t)prio) << 50);
54cdcae6 1428 gen_store_spr(SPR_PPR, t0);
ea363694 1429 tcg_temp_free(t0);
26d67362 1430 }
c80f84e3 1431#endif
9a64fbe4 1432 }
9a64fbe4 1433}
79aceca5 1434/* orc & orc. */
26d67362 1435GEN_LOGICAL2(orc, tcg_gen_orc_tl, 0x0C, PPC_INTEGER);
99e300ef 1436
54623277 1437/* xor & xor. */
99e300ef 1438static void gen_xor(DisasContext *ctx)
9a64fbe4 1439{
9a64fbe4 1440 /* Optimisation for "set to zero" case */
26d67362 1441 if (rS(ctx->opcode) != rB(ctx->opcode))
312179c4 1442 tcg_gen_xor_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)], cpu_gpr[rB(ctx->opcode)]);
26d67362
AJ
1443 else
1444 tcg_gen_movi_tl(cpu_gpr[rA(ctx->opcode)], 0);
76a66253 1445 if (unlikely(Rc(ctx->opcode) != 0))
26d67362 1446 gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
9a64fbe4 1447}
99e300ef 1448
54623277 1449/* ori */
99e300ef 1450static void gen_ori(DisasContext *ctx)
79aceca5 1451{
76a66253 1452 target_ulong uimm = UIMM(ctx->opcode);
79aceca5 1453
9a64fbe4
FB
1454 if (rS(ctx->opcode) == rA(ctx->opcode) && uimm == 0) {
1455 /* NOP */
76a66253 1456 /* XXX: should handle special NOPs for POWER series */
9a64fbe4 1457 return;
76a66253 1458 }
26d67362 1459 tcg_gen_ori_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)], uimm);
79aceca5 1460}
99e300ef 1461
54623277 1462/* oris */
99e300ef 1463static void gen_oris(DisasContext *ctx)
79aceca5 1464{
76a66253 1465 target_ulong uimm = UIMM(ctx->opcode);
79aceca5 1466
9a64fbe4
FB
1467 if (rS(ctx->opcode) == rA(ctx->opcode) && uimm == 0) {
1468 /* NOP */
1469 return;
76a66253 1470 }
26d67362 1471 tcg_gen_ori_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)], uimm << 16);
79aceca5 1472}
99e300ef 1473
54623277 1474/* xori */
99e300ef 1475static void gen_xori(DisasContext *ctx)
79aceca5 1476{
76a66253 1477 target_ulong uimm = UIMM(ctx->opcode);
9a64fbe4
FB
1478
1479 if (rS(ctx->opcode) == rA(ctx->opcode) && uimm == 0) {
1480 /* NOP */
1481 return;
1482 }
26d67362 1483 tcg_gen_xori_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)], uimm);
79aceca5 1484}
99e300ef 1485
54623277 1486/* xoris */
99e300ef 1487static void gen_xoris(DisasContext *ctx)
79aceca5 1488{
76a66253 1489 target_ulong uimm = UIMM(ctx->opcode);
9a64fbe4
FB
1490
1491 if (rS(ctx->opcode) == rA(ctx->opcode) && uimm == 0) {
1492 /* NOP */
1493 return;
1494 }
26d67362 1495 tcg_gen_xori_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)], uimm << 16);
79aceca5 1496}
99e300ef 1497
54623277 1498/* popcntb : PowerPC 2.03 specification */
99e300ef 1499static void gen_popcntb(DisasContext *ctx)
d9bce9d9 1500{
eaabeef2
DG
1501 gen_helper_popcntb(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)]);
1502}
1503
1504static void gen_popcntw(DisasContext *ctx)
1505{
1506 gen_helper_popcntw(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)]);
1507}
1508
d9bce9d9 1509#if defined(TARGET_PPC64)
eaabeef2
DG
1510/* popcntd: PowerPC 2.06 specification */
1511static void gen_popcntd(DisasContext *ctx)
1512{
1513 gen_helper_popcntd(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)]);
d9bce9d9 1514}
eaabeef2 1515#endif
d9bce9d9 1516
725bcec2
AJ
1517/* prtyw: PowerPC 2.05 specification */
1518static void gen_prtyw(DisasContext *ctx)
1519{
1520 TCGv ra = cpu_gpr[rA(ctx->opcode)];
1521 TCGv rs = cpu_gpr[rS(ctx->opcode)];
1522 TCGv t0 = tcg_temp_new();
1523 tcg_gen_shri_tl(t0, rs, 16);
1524 tcg_gen_xor_tl(ra, rs, t0);
1525 tcg_gen_shri_tl(t0, ra, 8);
1526 tcg_gen_xor_tl(ra, ra, t0);
1527 tcg_gen_andi_tl(ra, ra, (target_ulong)0x100000001ULL);
1528 tcg_temp_free(t0);
1529}
1530
1531#if defined(TARGET_PPC64)
1532/* prtyd: PowerPC 2.05 specification */
1533static void gen_prtyd(DisasContext *ctx)
1534{
1535 TCGv ra = cpu_gpr[rA(ctx->opcode)];
1536 TCGv rs = cpu_gpr[rS(ctx->opcode)];
1537 TCGv t0 = tcg_temp_new();
1538 tcg_gen_shri_tl(t0, rs, 32);
1539 tcg_gen_xor_tl(ra, rs, t0);
1540 tcg_gen_shri_tl(t0, ra, 16);
1541 tcg_gen_xor_tl(ra, ra, t0);
1542 tcg_gen_shri_tl(t0, ra, 8);
1543 tcg_gen_xor_tl(ra, ra, t0);
1544 tcg_gen_andi_tl(ra, ra, 1);
1545 tcg_temp_free(t0);
1546}
1547#endif
1548
86ba37ed
TM
1549#if defined(TARGET_PPC64)
1550/* bpermd */
1551static void gen_bpermd(DisasContext *ctx)
1552{
1553 gen_helper_bpermd(cpu_gpr[rA(ctx->opcode)],
1554 cpu_gpr[rS(ctx->opcode)], cpu_gpr[rB(ctx->opcode)]);
1555}
1556#endif
1557
d9bce9d9
JM
1558#if defined(TARGET_PPC64)
1559/* extsw & extsw. */
26d67362 1560GEN_LOGICAL1(extsw, tcg_gen_ext32s_tl, 0x1E, PPC_64B);
99e300ef 1561
54623277 1562/* cntlzd */
99e300ef 1563static void gen_cntlzd(DisasContext *ctx)
26d67362 1564{
a7812ae4 1565 gen_helper_cntlzd(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)]);
26d67362
AJ
1566 if (unlikely(Rc(ctx->opcode) != 0))
1567 gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
1568}
d9bce9d9
JM
1569#endif
1570
79aceca5 1571/*** Integer rotate ***/
99e300ef 1572
54623277 1573/* rlwimi & rlwimi. */
99e300ef 1574static void gen_rlwimi(DisasContext *ctx)
79aceca5 1575{
76a66253 1576 uint32_t mb, me, sh;
79aceca5
FB
1577
1578 mb = MB(ctx->opcode);
1579 me = ME(ctx->opcode);
76a66253 1580 sh = SH(ctx->opcode);
d03ef511
AJ
1581 if (likely(sh == 0 && mb == 0 && me == 31)) {
1582 tcg_gen_ext32u_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)]);
1583 } else {
d03ef511 1584 target_ulong mask;
a7812ae4
PB
1585 TCGv t1;
1586 TCGv t0 = tcg_temp_new();
54843a58 1587#if defined(TARGET_PPC64)
a7812ae4
PB
1588 TCGv_i32 t2 = tcg_temp_new_i32();
1589 tcg_gen_trunc_i64_i32(t2, cpu_gpr[rS(ctx->opcode)]);
1590 tcg_gen_rotli_i32(t2, t2, sh);
1591 tcg_gen_extu_i32_i64(t0, t2);
1592 tcg_temp_free_i32(t2);
54843a58
AJ
1593#else
1594 tcg_gen_rotli_i32(t0, cpu_gpr[rS(ctx->opcode)], sh);
1595#endif
76a66253 1596#if defined(TARGET_PPC64)
d03ef511
AJ
1597 mb += 32;
1598 me += 32;
76a66253 1599#endif
d03ef511 1600 mask = MASK(mb, me);
a7812ae4 1601 t1 = tcg_temp_new();
d03ef511
AJ
1602 tcg_gen_andi_tl(t0, t0, mask);
1603 tcg_gen_andi_tl(t1, cpu_gpr[rA(ctx->opcode)], ~mask);
1604 tcg_gen_or_tl(cpu_gpr[rA(ctx->opcode)], t0, t1);
1605 tcg_temp_free(t0);
1606 tcg_temp_free(t1);
1607 }
76a66253 1608 if (unlikely(Rc(ctx->opcode) != 0))
d03ef511 1609 gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
79aceca5 1610}
99e300ef 1611
54623277 1612/* rlwinm & rlwinm. */
99e300ef 1613static void gen_rlwinm(DisasContext *ctx)
79aceca5
FB
1614{
1615 uint32_t mb, me, sh;
3b46e624 1616
79aceca5
FB
1617 sh = SH(ctx->opcode);
1618 mb = MB(ctx->opcode);
1619 me = ME(ctx->opcode);
d03ef511
AJ
1620
1621 if (likely(mb == 0 && me == (31 - sh))) {
1622 if (likely(sh == 0)) {
1623 tcg_gen_ext32u_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)]);
1624 } else {
a7812ae4 1625 TCGv t0 = tcg_temp_new();
d03ef511
AJ
1626 tcg_gen_ext32u_tl(t0, cpu_gpr[rS(ctx->opcode)]);
1627 tcg_gen_shli_tl(t0, t0, sh);
1628 tcg_gen_ext32u_tl(cpu_gpr[rA(ctx->opcode)], t0);
1629 tcg_temp_free(t0);
79aceca5 1630 }
d03ef511 1631 } else if (likely(sh != 0 && me == 31 && sh == (32 - mb))) {
a7812ae4 1632 TCGv t0 = tcg_temp_new();
d03ef511
AJ
1633 tcg_gen_ext32u_tl(t0, cpu_gpr[rS(ctx->opcode)]);
1634 tcg_gen_shri_tl(t0, t0, mb);
1635 tcg_gen_ext32u_tl(cpu_gpr[rA(ctx->opcode)], t0);
1636 tcg_temp_free(t0);
1637 } else {
a7812ae4 1638 TCGv t0 = tcg_temp_new();
54843a58 1639#if defined(TARGET_PPC64)
a7812ae4 1640 TCGv_i32 t1 = tcg_temp_new_i32();
54843a58
AJ
1641 tcg_gen_trunc_i64_i32(t1, cpu_gpr[rS(ctx->opcode)]);
1642 tcg_gen_rotli_i32(t1, t1, sh);
1643 tcg_gen_extu_i32_i64(t0, t1);
a7812ae4 1644 tcg_temp_free_i32(t1);
54843a58
AJ
1645#else
1646 tcg_gen_rotli_i32(t0, cpu_gpr[rS(ctx->opcode)], sh);
1647#endif
76a66253 1648#if defined(TARGET_PPC64)
d03ef511
AJ
1649 mb += 32;
1650 me += 32;
76a66253 1651#endif
d03ef511
AJ
1652 tcg_gen_andi_tl(cpu_gpr[rA(ctx->opcode)], t0, MASK(mb, me));
1653 tcg_temp_free(t0);
1654 }
76a66253 1655 if (unlikely(Rc(ctx->opcode) != 0))
d03ef511 1656 gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
79aceca5 1657}
99e300ef 1658
54623277 1659/* rlwnm & rlwnm. */
99e300ef 1660static void gen_rlwnm(DisasContext *ctx)
79aceca5
FB
1661{
1662 uint32_t mb, me;
54843a58
AJ
1663 TCGv t0;
1664#if defined(TARGET_PPC64)
a7812ae4 1665 TCGv_i32 t1, t2;
54843a58 1666#endif
79aceca5
FB
1667
1668 mb = MB(ctx->opcode);
1669 me = ME(ctx->opcode);
a7812ae4 1670 t0 = tcg_temp_new();
d03ef511 1671 tcg_gen_andi_tl(t0, cpu_gpr[rB(ctx->opcode)], 0x1f);
54843a58 1672#if defined(TARGET_PPC64)
a7812ae4
PB
1673 t1 = tcg_temp_new_i32();
1674 t2 = tcg_temp_new_i32();
54843a58
AJ
1675 tcg_gen_trunc_i64_i32(t1, cpu_gpr[rS(ctx->opcode)]);
1676 tcg_gen_trunc_i64_i32(t2, t0);
1677 tcg_gen_rotl_i32(t1, t1, t2);
1678 tcg_gen_extu_i32_i64(t0, t1);
a7812ae4
PB
1679 tcg_temp_free_i32(t1);
1680 tcg_temp_free_i32(t2);
54843a58
AJ
1681#else
1682 tcg_gen_rotl_i32(t0, cpu_gpr[rS(ctx->opcode)], t0);
1683#endif
76a66253
JM
1684 if (unlikely(mb != 0 || me != 31)) {
1685#if defined(TARGET_PPC64)
1686 mb += 32;
1687 me += 32;
1688#endif
54843a58 1689 tcg_gen_andi_tl(cpu_gpr[rA(ctx->opcode)], t0, MASK(mb, me));
d03ef511 1690 } else {
54843a58 1691 tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], t0);
79aceca5 1692 }
54843a58 1693 tcg_temp_free(t0);
76a66253 1694 if (unlikely(Rc(ctx->opcode) != 0))
d03ef511 1695 gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
79aceca5
FB
1696}
1697
d9bce9d9
JM
1698#if defined(TARGET_PPC64)
1699#define GEN_PPC64_R2(name, opc1, opc2) \
e8eaa2c0 1700static void glue(gen_, name##0)(DisasContext *ctx) \
d9bce9d9
JM
1701{ \
1702 gen_##name(ctx, 0); \
1703} \
e8eaa2c0
BS
1704 \
1705static void glue(gen_, name##1)(DisasContext *ctx) \
d9bce9d9
JM
1706{ \
1707 gen_##name(ctx, 1); \
1708}
1709#define GEN_PPC64_R4(name, opc1, opc2) \
e8eaa2c0 1710static void glue(gen_, name##0)(DisasContext *ctx) \
d9bce9d9
JM
1711{ \
1712 gen_##name(ctx, 0, 0); \
1713} \
e8eaa2c0
BS
1714 \
1715static void glue(gen_, name##1)(DisasContext *ctx) \
d9bce9d9
JM
1716{ \
1717 gen_##name(ctx, 0, 1); \
1718} \
e8eaa2c0
BS
1719 \
1720static void glue(gen_, name##2)(DisasContext *ctx) \
d9bce9d9
JM
1721{ \
1722 gen_##name(ctx, 1, 0); \
1723} \
e8eaa2c0
BS
1724 \
1725static void glue(gen_, name##3)(DisasContext *ctx) \
d9bce9d9
JM
1726{ \
1727 gen_##name(ctx, 1, 1); \
1728}
51789c41 1729
636aa200
BS
1730static inline void gen_rldinm(DisasContext *ctx, uint32_t mb, uint32_t me,
1731 uint32_t sh)
51789c41 1732{
d03ef511
AJ
1733 if (likely(sh != 0 && mb == 0 && me == (63 - sh))) {
1734 tcg_gen_shli_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)], sh);
1735 } else if (likely(sh != 0 && me == 63 && sh == (64 - mb))) {
1736 tcg_gen_shri_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)], mb);
1737 } else {
a7812ae4 1738 TCGv t0 = tcg_temp_new();
54843a58 1739 tcg_gen_rotli_tl(t0, cpu_gpr[rS(ctx->opcode)], sh);
d03ef511 1740 if (likely(mb == 0 && me == 63)) {
54843a58 1741 tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], t0);
d03ef511
AJ
1742 } else {
1743 tcg_gen_andi_tl(cpu_gpr[rA(ctx->opcode)], t0, MASK(mb, me));
51789c41 1744 }
d03ef511 1745 tcg_temp_free(t0);
51789c41 1746 }
51789c41 1747 if (unlikely(Rc(ctx->opcode) != 0))
d03ef511 1748 gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
51789c41 1749}
d9bce9d9 1750/* rldicl - rldicl. */
636aa200 1751static inline void gen_rldicl(DisasContext *ctx, int mbn, int shn)
d9bce9d9 1752{
51789c41 1753 uint32_t sh, mb;
d9bce9d9 1754
9d53c753
JM
1755 sh = SH(ctx->opcode) | (shn << 5);
1756 mb = MB(ctx->opcode) | (mbn << 5);
51789c41 1757 gen_rldinm(ctx, mb, 63, sh);
d9bce9d9 1758}
51789c41 1759GEN_PPC64_R4(rldicl, 0x1E, 0x00);
d9bce9d9 1760/* rldicr - rldicr. */
636aa200 1761static inline void gen_rldicr(DisasContext *ctx, int men, int shn)
d9bce9d9 1762{
51789c41 1763 uint32_t sh, me;
d9bce9d9 1764
9d53c753
JM
1765 sh = SH(ctx->opcode) | (shn << 5);
1766 me = MB(ctx->opcode) | (men << 5);
51789c41 1767 gen_rldinm(ctx, 0, me, sh);
d9bce9d9 1768}
51789c41 1769GEN_PPC64_R4(rldicr, 0x1E, 0x02);
d9bce9d9 1770/* rldic - rldic. */
636aa200 1771static inline void gen_rldic(DisasContext *ctx, int mbn, int shn)
d9bce9d9 1772{
51789c41 1773 uint32_t sh, mb;
d9bce9d9 1774
9d53c753
JM
1775 sh = SH(ctx->opcode) | (shn << 5);
1776 mb = MB(ctx->opcode) | (mbn << 5);
51789c41
JM
1777 gen_rldinm(ctx, mb, 63 - sh, sh);
1778}
1779GEN_PPC64_R4(rldic, 0x1E, 0x04);
1780
636aa200 1781static inline void gen_rldnm(DisasContext *ctx, uint32_t mb, uint32_t me)
51789c41 1782{
54843a58 1783 TCGv t0;
d03ef511 1784
a7812ae4 1785 t0 = tcg_temp_new();
d03ef511 1786 tcg_gen_andi_tl(t0, cpu_gpr[rB(ctx->opcode)], 0x3f);
54843a58 1787 tcg_gen_rotl_tl(t0, cpu_gpr[rS(ctx->opcode)], t0);
51789c41 1788 if (unlikely(mb != 0 || me != 63)) {
54843a58
AJ
1789 tcg_gen_andi_tl(cpu_gpr[rA(ctx->opcode)], t0, MASK(mb, me));
1790 } else {
1791 tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], t0);
1792 }
1793 tcg_temp_free(t0);
51789c41 1794 if (unlikely(Rc(ctx->opcode) != 0))
d03ef511 1795 gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
d9bce9d9 1796}
51789c41 1797
d9bce9d9 1798/* rldcl - rldcl. */
636aa200 1799static inline void gen_rldcl(DisasContext *ctx, int mbn)
d9bce9d9 1800{
51789c41 1801 uint32_t mb;
d9bce9d9 1802
9d53c753 1803 mb = MB(ctx->opcode) | (mbn << 5);
51789c41 1804 gen_rldnm(ctx, mb, 63);
d9bce9d9 1805}
36081602 1806GEN_PPC64_R2(rldcl, 0x1E, 0x08);
d9bce9d9 1807/* rldcr - rldcr. */
636aa200 1808static inline void gen_rldcr(DisasContext *ctx, int men)
d9bce9d9 1809{
51789c41 1810 uint32_t me;
d9bce9d9 1811
9d53c753 1812 me = MB(ctx->opcode) | (men << 5);
51789c41 1813 gen_rldnm(ctx, 0, me);
d9bce9d9 1814}
36081602 1815GEN_PPC64_R2(rldcr, 0x1E, 0x09);
d9bce9d9 1816/* rldimi - rldimi. */
636aa200 1817static inline void gen_rldimi(DisasContext *ctx, int mbn, int shn)
d9bce9d9 1818{
271a916e 1819 uint32_t sh, mb, me;
d9bce9d9 1820
9d53c753
JM
1821 sh = SH(ctx->opcode) | (shn << 5);
1822 mb = MB(ctx->opcode) | (mbn << 5);
271a916e 1823 me = 63 - sh;
d03ef511
AJ
1824 if (unlikely(sh == 0 && mb == 0)) {
1825 tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)]);
1826 } else {
1827 TCGv t0, t1;
1828 target_ulong mask;
1829
a7812ae4 1830 t0 = tcg_temp_new();
54843a58 1831 tcg_gen_rotli_tl(t0, cpu_gpr[rS(ctx->opcode)], sh);
a7812ae4 1832 t1 = tcg_temp_new();
d03ef511
AJ
1833 mask = MASK(mb, me);
1834 tcg_gen_andi_tl(t0, t0, mask);
1835 tcg_gen_andi_tl(t1, cpu_gpr[rA(ctx->opcode)], ~mask);
1836 tcg_gen_or_tl(cpu_gpr[rA(ctx->opcode)], t0, t1);
1837 tcg_temp_free(t0);
1838 tcg_temp_free(t1);
51789c41 1839 }
51789c41 1840 if (unlikely(Rc(ctx->opcode) != 0))
d03ef511 1841 gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
d9bce9d9 1842}
36081602 1843GEN_PPC64_R4(rldimi, 0x1E, 0x06);
d9bce9d9
JM
1844#endif
1845
79aceca5 1846/*** Integer shift ***/
99e300ef 1847
54623277 1848/* slw & slw. */
99e300ef 1849static void gen_slw(DisasContext *ctx)
26d67362 1850{
7fd6bf7d 1851 TCGv t0, t1;
26d67362 1852
7fd6bf7d
AJ
1853 t0 = tcg_temp_new();
1854 /* AND rS with a mask that is 0 when rB >= 0x20 */
1855#if defined(TARGET_PPC64)
1856 tcg_gen_shli_tl(t0, cpu_gpr[rB(ctx->opcode)], 0x3a);
1857 tcg_gen_sari_tl(t0, t0, 0x3f);
1858#else
1859 tcg_gen_shli_tl(t0, cpu_gpr[rB(ctx->opcode)], 0x1a);
1860 tcg_gen_sari_tl(t0, t0, 0x1f);
1861#endif
1862 tcg_gen_andc_tl(t0, cpu_gpr[rS(ctx->opcode)], t0);
1863 t1 = tcg_temp_new();
1864 tcg_gen_andi_tl(t1, cpu_gpr[rB(ctx->opcode)], 0x1f);
1865 tcg_gen_shl_tl(cpu_gpr[rA(ctx->opcode)], t0, t1);
1866 tcg_temp_free(t1);
fea0c503 1867 tcg_temp_free(t0);
7fd6bf7d 1868 tcg_gen_ext32u_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rA(ctx->opcode)]);
26d67362
AJ
1869 if (unlikely(Rc(ctx->opcode) != 0))
1870 gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
1871}
99e300ef 1872
54623277 1873/* sraw & sraw. */
99e300ef 1874static void gen_sraw(DisasContext *ctx)
26d67362 1875{
d15f74fb 1876 gen_helper_sraw(cpu_gpr[rA(ctx->opcode)], cpu_env,
a7812ae4 1877 cpu_gpr[rS(ctx->opcode)], cpu_gpr[rB(ctx->opcode)]);
26d67362
AJ
1878 if (unlikely(Rc(ctx->opcode) != 0))
1879 gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
1880}
99e300ef 1881
54623277 1882/* srawi & srawi. */
99e300ef 1883static void gen_srawi(DisasContext *ctx)
79aceca5 1884{
26d67362 1885 int sh = SH(ctx->opcode);
ba4af3e4
RH
1886 TCGv dst = cpu_gpr[rA(ctx->opcode)];
1887 TCGv src = cpu_gpr[rS(ctx->opcode)];
1888 if (sh == 0) {
1889 tcg_gen_mov_tl(dst, src);
da91a00f 1890 tcg_gen_movi_tl(cpu_ca, 0);
26d67362 1891 } else {
ba4af3e4
RH
1892 TCGv t0;
1893 tcg_gen_ext32s_tl(dst, src);
1894 tcg_gen_andi_tl(cpu_ca, dst, (1ULL << sh) - 1);
1895 t0 = tcg_temp_new();
1896 tcg_gen_sari_tl(t0, dst, TARGET_LONG_BITS - 1);
1897 tcg_gen_and_tl(cpu_ca, cpu_ca, t0);
1898 tcg_temp_free(t0);
1899 tcg_gen_setcondi_tl(TCG_COND_NE, cpu_ca, cpu_ca, 0);
1900 tcg_gen_sari_tl(dst, dst, sh);
1901 }
1902 if (unlikely(Rc(ctx->opcode) != 0)) {
1903 gen_set_Rc0(ctx, dst);
d9bce9d9 1904 }
79aceca5 1905}
99e300ef 1906
54623277 1907/* srw & srw. */
99e300ef 1908static void gen_srw(DisasContext *ctx)
26d67362 1909{
fea0c503 1910 TCGv t0, t1;
d9bce9d9 1911
7fd6bf7d
AJ
1912 t0 = tcg_temp_new();
1913 /* AND rS with a mask that is 0 when rB >= 0x20 */
1914#if defined(TARGET_PPC64)
1915 tcg_gen_shli_tl(t0, cpu_gpr[rB(ctx->opcode)], 0x3a);
1916 tcg_gen_sari_tl(t0, t0, 0x3f);
1917#else
1918 tcg_gen_shli_tl(t0, cpu_gpr[rB(ctx->opcode)], 0x1a);
1919 tcg_gen_sari_tl(t0, t0, 0x1f);
1920#endif
1921 tcg_gen_andc_tl(t0, cpu_gpr[rS(ctx->opcode)], t0);
1922 tcg_gen_ext32u_tl(t0, t0);
a7812ae4 1923 t1 = tcg_temp_new();
7fd6bf7d
AJ
1924 tcg_gen_andi_tl(t1, cpu_gpr[rB(ctx->opcode)], 0x1f);
1925 tcg_gen_shr_tl(cpu_gpr[rA(ctx->opcode)], t0, t1);
fea0c503 1926 tcg_temp_free(t1);
fea0c503 1927 tcg_temp_free(t0);
26d67362
AJ
1928 if (unlikely(Rc(ctx->opcode) != 0))
1929 gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
1930}
54623277 1931
d9bce9d9
JM
1932#if defined(TARGET_PPC64)
1933/* sld & sld. */
99e300ef 1934static void gen_sld(DisasContext *ctx)
26d67362 1935{
7fd6bf7d 1936 TCGv t0, t1;
26d67362 1937
7fd6bf7d
AJ
1938 t0 = tcg_temp_new();
1939 /* AND rS with a mask that is 0 when rB >= 0x40 */
1940 tcg_gen_shli_tl(t0, cpu_gpr[rB(ctx->opcode)], 0x39);
1941 tcg_gen_sari_tl(t0, t0, 0x3f);
1942 tcg_gen_andc_tl(t0, cpu_gpr[rS(ctx->opcode)], t0);
1943 t1 = tcg_temp_new();
1944 tcg_gen_andi_tl(t1, cpu_gpr[rB(ctx->opcode)], 0x3f);
1945 tcg_gen_shl_tl(cpu_gpr[rA(ctx->opcode)], t0, t1);
1946 tcg_temp_free(t1);
fea0c503 1947 tcg_temp_free(t0);
26d67362
AJ
1948 if (unlikely(Rc(ctx->opcode) != 0))
1949 gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
1950}
99e300ef 1951
54623277 1952/* srad & srad. */
99e300ef 1953static void gen_srad(DisasContext *ctx)
26d67362 1954{
d15f74fb 1955 gen_helper_srad(cpu_gpr[rA(ctx->opcode)], cpu_env,
a7812ae4 1956 cpu_gpr[rS(ctx->opcode)], cpu_gpr[rB(ctx->opcode)]);
26d67362
AJ
1957 if (unlikely(Rc(ctx->opcode) != 0))
1958 gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
1959}
d9bce9d9 1960/* sradi & sradi. */
636aa200 1961static inline void gen_sradi(DisasContext *ctx, int n)
d9bce9d9 1962{
26d67362 1963 int sh = SH(ctx->opcode) + (n << 5);
ba4af3e4
RH
1964 TCGv dst = cpu_gpr[rA(ctx->opcode)];
1965 TCGv src = cpu_gpr[rS(ctx->opcode)];
1966 if (sh == 0) {
1967 tcg_gen_mov_tl(dst, src);
da91a00f 1968 tcg_gen_movi_tl(cpu_ca, 0);
26d67362 1969 } else {
ba4af3e4
RH
1970 TCGv t0;
1971 tcg_gen_andi_tl(cpu_ca, src, (1ULL << sh) - 1);
1972 t0 = tcg_temp_new();
1973 tcg_gen_sari_tl(t0, src, TARGET_LONG_BITS - 1);
1974 tcg_gen_and_tl(cpu_ca, cpu_ca, t0);
1975 tcg_temp_free(t0);
1976 tcg_gen_setcondi_tl(TCG_COND_NE, cpu_ca, cpu_ca, 0);
1977 tcg_gen_sari_tl(dst, src, sh);
1978 }
1979 if (unlikely(Rc(ctx->opcode) != 0)) {
1980 gen_set_Rc0(ctx, dst);
d9bce9d9 1981 }
d9bce9d9 1982}
e8eaa2c0
BS
1983
1984static void gen_sradi0(DisasContext *ctx)
d9bce9d9
JM
1985{
1986 gen_sradi(ctx, 0);
1987}
e8eaa2c0
BS
1988
1989static void gen_sradi1(DisasContext *ctx)
d9bce9d9
JM
1990{
1991 gen_sradi(ctx, 1);
1992}
99e300ef 1993
54623277 1994/* srd & srd. */
99e300ef 1995static void gen_srd(DisasContext *ctx)
26d67362 1996{
7fd6bf7d 1997 TCGv t0, t1;
26d67362 1998
7fd6bf7d
AJ
1999 t0 = tcg_temp_new();
2000 /* AND rS with a mask that is 0 when rB >= 0x40 */
2001 tcg_gen_shli_tl(t0, cpu_gpr[rB(ctx->opcode)], 0x39);
2002 tcg_gen_sari_tl(t0, t0, 0x3f);
2003 tcg_gen_andc_tl(t0, cpu_gpr[rS(ctx->opcode)], t0);
2004 t1 = tcg_temp_new();
2005 tcg_gen_andi_tl(t1, cpu_gpr[rB(ctx->opcode)], 0x3f);
2006 tcg_gen_shr_tl(cpu_gpr[rA(ctx->opcode)], t0, t1);
2007 tcg_temp_free(t1);
fea0c503 2008 tcg_temp_free(t0);
26d67362
AJ
2009 if (unlikely(Rc(ctx->opcode) != 0))
2010 gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
2011}
d9bce9d9 2012#endif
79aceca5
FB
2013
2014/*** Floating-Point arithmetic ***/
7c58044c 2015#define _GEN_FLOAT_ACB(name, op, op1, op2, isfloat, set_fprf, type) \
99e300ef 2016static void gen_f##name(DisasContext *ctx) \
9a64fbe4 2017{ \
76a66253 2018 if (unlikely(!ctx->fpu_enabled)) { \
e06fcd75 2019 gen_exception(ctx, POWERPC_EXCP_FPU); \
3cc62370
FB
2020 return; \
2021 } \
eb44b959
AJ
2022 /* NIP cannot be restored if the memory exception comes from an helper */ \
2023 gen_update_nip(ctx, ctx->nip - 4); \
7c58044c 2024 gen_reset_fpstatus(); \
8e703949
BS
2025 gen_helper_f##op(cpu_fpr[rD(ctx->opcode)], cpu_env, \
2026 cpu_fpr[rA(ctx->opcode)], \
af12906f 2027 cpu_fpr[rC(ctx->opcode)], cpu_fpr[rB(ctx->opcode)]); \
4ecc3190 2028 if (isfloat) { \
8e703949
BS
2029 gen_helper_frsp(cpu_fpr[rD(ctx->opcode)], cpu_env, \
2030 cpu_fpr[rD(ctx->opcode)]); \
4ecc3190 2031 } \
af12906f
AJ
2032 gen_compute_fprf(cpu_fpr[rD(ctx->opcode)], set_fprf, \
2033 Rc(ctx->opcode) != 0); \
9a64fbe4
FB
2034}
2035
7c58044c
JM
2036#define GEN_FLOAT_ACB(name, op2, set_fprf, type) \
2037_GEN_FLOAT_ACB(name, name, 0x3F, op2, 0, set_fprf, type); \
2038_GEN_FLOAT_ACB(name##s, name, 0x3B, op2, 1, set_fprf, type);
9a64fbe4 2039
7c58044c 2040#define _GEN_FLOAT_AB(name, op, op1, op2, inval, isfloat, set_fprf, type) \
99e300ef 2041static void gen_f##name(DisasContext *ctx) \
9a64fbe4 2042{ \
76a66253 2043 if (unlikely(!ctx->fpu_enabled)) { \
e06fcd75 2044 gen_exception(ctx, POWERPC_EXCP_FPU); \
3cc62370
FB
2045 return; \
2046 } \
eb44b959
AJ
2047 /* NIP cannot be restored if the memory exception comes from an helper */ \
2048 gen_update_nip(ctx, ctx->nip - 4); \
7c58044c 2049 gen_reset_fpstatus(); \
8e703949
BS
2050 gen_helper_f##op(cpu_fpr[rD(ctx->opcode)], cpu_env, \
2051 cpu_fpr[rA(ctx->opcode)], \
af12906f 2052 cpu_fpr[rB(ctx->opcode)]); \
4ecc3190 2053 if (isfloat) { \
8e703949
BS
2054 gen_helper_frsp(cpu_fpr[rD(ctx->opcode)], cpu_env, \
2055 cpu_fpr[rD(ctx->opcode)]); \
4ecc3190 2056 } \
af12906f
AJ
2057 gen_compute_fprf(cpu_fpr[rD(ctx->opcode)], \
2058 set_fprf, Rc(ctx->opcode) != 0); \
9a64fbe4 2059}
7c58044c
JM
2060#define GEN_FLOAT_AB(name, op2, inval, set_fprf, type) \
2061_GEN_FLOAT_AB(name, name, 0x3F, op2, inval, 0, set_fprf, type); \
2062_GEN_FLOAT_AB(name##s, name, 0x3B, op2, inval, 1, set_fprf, type);
9a64fbe4 2063
7c58044c 2064#define _GEN_FLOAT_AC(name, op, op1, op2, inval, isfloat, set_fprf, type) \
99e300ef 2065static void gen_f##name(DisasContext *ctx) \
9a64fbe4 2066{ \
76a66253 2067 if (unlikely(!ctx->fpu_enabled)) { \
e06fcd75 2068 gen_exception(ctx, POWERPC_EXCP_FPU); \
3cc62370
FB
2069 return; \
2070 } \
eb44b959
AJ
2071 /* NIP cannot be restored if the memory exception comes from an helper */ \
2072 gen_update_nip(ctx, ctx->nip - 4); \
7c58044c 2073 gen_reset_fpstatus(); \
8e703949
BS
2074 gen_helper_f##op(cpu_fpr[rD(ctx->opcode)], cpu_env, \
2075 cpu_fpr[rA(ctx->opcode)], \
2076 cpu_fpr[rC(ctx->opcode)]); \
4ecc3190 2077 if (isfloat) { \
8e703949
BS
2078 gen_helper_frsp(cpu_fpr[rD(ctx->opcode)], cpu_env, \
2079 cpu_fpr[rD(ctx->opcode)]); \
4ecc3190 2080 } \
af12906f
AJ
2081 gen_compute_fprf(cpu_fpr[rD(ctx->opcode)], \
2082 set_fprf, Rc(ctx->opcode) != 0); \
9a64fbe4 2083}
7c58044c
JM
2084#define GEN_FLOAT_AC(name, op2, inval, set_fprf, type) \
2085_GEN_FLOAT_AC(name, name, 0x3F, op2, inval, 0, set_fprf, type); \
2086_GEN_FLOAT_AC(name##s, name, 0x3B, op2, inval, 1, set_fprf, type);
9a64fbe4 2087
7c58044c 2088#define GEN_FLOAT_B(name, op2, op3, set_fprf, type) \
99e300ef 2089static void gen_f##name(DisasContext *ctx) \
9a64fbe4 2090{ \
76a66253 2091 if (unlikely(!ctx->fpu_enabled)) { \
e06fcd75 2092 gen_exception(ctx, POWERPC_EXCP_FPU); \
3cc62370
FB
2093 return; \
2094 } \
eb44b959
AJ
2095 /* NIP cannot be restored if the memory exception comes from an helper */ \
2096 gen_update_nip(ctx, ctx->nip - 4); \
7c58044c 2097 gen_reset_fpstatus(); \
8e703949
BS
2098 gen_helper_f##name(cpu_fpr[rD(ctx->opcode)], cpu_env, \
2099 cpu_fpr[rB(ctx->opcode)]); \
af12906f
AJ
2100 gen_compute_fprf(cpu_fpr[rD(ctx->opcode)], \
2101 set_fprf, Rc(ctx->opcode) != 0); \
79aceca5
FB
2102}
2103
7c58044c 2104#define GEN_FLOAT_BS(name, op1, op2, set_fprf, type) \
99e300ef 2105static void gen_f##name(DisasContext *ctx) \
9a64fbe4 2106{ \
76a66253 2107 if (unlikely(!ctx->fpu_enabled)) { \
e06fcd75 2108 gen_exception(ctx, POWERPC_EXCP_FPU); \
3cc62370
FB
2109 return; \
2110 } \
eb44b959
AJ
2111 /* NIP cannot be restored if the memory exception comes from an helper */ \
2112 gen_update_nip(ctx, ctx->nip - 4); \
7c58044c 2113 gen_reset_fpstatus(); \
8e703949
BS
2114 gen_helper_f##name(cpu_fpr[rD(ctx->opcode)], cpu_env, \
2115 cpu_fpr[rB(ctx->opcode)]); \
af12906f
AJ
2116 gen_compute_fprf(cpu_fpr[rD(ctx->opcode)], \
2117 set_fprf, Rc(ctx->opcode) != 0); \
79aceca5
FB
2118}
2119
9a64fbe4 2120/* fadd - fadds */
7c58044c 2121GEN_FLOAT_AB(add, 0x15, 0x000007C0, 1, PPC_FLOAT);
4ecc3190 2122/* fdiv - fdivs */
7c58044c 2123GEN_FLOAT_AB(div, 0x12, 0x000007C0, 1, PPC_FLOAT);
4ecc3190 2124/* fmul - fmuls */
7c58044c 2125GEN_FLOAT_AC(mul, 0x19, 0x0000F800, 1, PPC_FLOAT);
79aceca5 2126
d7e4b87e 2127/* fre */
7c58044c 2128GEN_FLOAT_BS(re, 0x3F, 0x18, 1, PPC_FLOAT_EXT);
d7e4b87e 2129
a750fc0b 2130/* fres */
7c58044c 2131GEN_FLOAT_BS(res, 0x3B, 0x18, 1, PPC_FLOAT_FRES);
79aceca5 2132
a750fc0b 2133/* frsqrte */
7c58044c
JM
2134GEN_FLOAT_BS(rsqrte, 0x3F, 0x1A, 1, PPC_FLOAT_FRSQRTE);
2135
2136/* frsqrtes */
99e300ef 2137static void gen_frsqrtes(DisasContext *ctx)
7c58044c 2138{
af12906f 2139 if (unlikely(!ctx->fpu_enabled)) {
e06fcd75 2140 gen_exception(ctx, POWERPC_EXCP_FPU);
af12906f
AJ
2141 return;
2142 }
eb44b959
AJ
2143 /* NIP cannot be restored if the memory exception comes from an helper */
2144 gen_update_nip(ctx, ctx->nip - 4);
af12906f 2145 gen_reset_fpstatus();
8e703949
BS
2146 gen_helper_frsqrte(cpu_fpr[rD(ctx->opcode)], cpu_env,
2147 cpu_fpr[rB(ctx->opcode)]);
2148 gen_helper_frsp(cpu_fpr[rD(ctx->opcode)], cpu_env,
2149 cpu_fpr[rD(ctx->opcode)]);
af12906f 2150 gen_compute_fprf(cpu_fpr[rD(ctx->opcode)], 1, Rc(ctx->opcode) != 0);
7c58044c 2151}
79aceca5 2152
a750fc0b 2153/* fsel */
7c58044c 2154_GEN_FLOAT_ACB(sel, sel, 0x3F, 0x17, 0, 0, PPC_FLOAT_FSEL);
4ecc3190 2155/* fsub - fsubs */
7c58044c 2156GEN_FLOAT_AB(sub, 0x14, 0x000007C0, 1, PPC_FLOAT);
79aceca5 2157/* Optional: */
99e300ef 2158
54623277 2159/* fsqrt */
99e300ef 2160static void gen_fsqrt(DisasContext *ctx)
c7d344af 2161{
76a66253 2162 if (unlikely(!ctx->fpu_enabled)) {
e06fcd75 2163 gen_exception(ctx, POWERPC_EXCP_FPU);
c7d344af
FB
2164 return;
2165 }
eb44b959
AJ
2166 /* NIP cannot be restored if the memory exception comes from an helper */
2167 gen_update_nip(ctx, ctx->nip - 4);
7c58044c 2168 gen_reset_fpstatus();
8e703949
BS
2169 gen_helper_fsqrt(cpu_fpr[rD(ctx->opcode)], cpu_env,
2170 cpu_fpr[rB(ctx->opcode)]);
af12906f 2171 gen_compute_fprf(cpu_fpr[rD(ctx->opcode)], 1, Rc(ctx->opcode) != 0);
c7d344af 2172}
79aceca5 2173
99e300ef 2174static void gen_fsqrts(DisasContext *ctx)
79aceca5 2175{
76a66253 2176 if (unlikely(!ctx->fpu_enabled)) {
e06fcd75 2177 gen_exception(ctx, POWERPC_EXCP_FPU);
3cc62370
FB
2178 return;
2179 }
eb44b959
AJ
2180 /* NIP cannot be restored if the memory exception comes from an helper */
2181 gen_update_nip(ctx, ctx->nip - 4);
7c58044c 2182 gen_reset_fpstatus();
8e703949
BS
2183 gen_helper_fsqrt(cpu_fpr[rD(ctx->opcode)], cpu_env,
2184 cpu_fpr[rB(ctx->opcode)]);
2185 gen_helper_frsp(cpu_fpr[rD(ctx->opcode)], cpu_env,
2186 cpu_fpr[rD(ctx->opcode)]);
af12906f 2187 gen_compute_fprf(cpu_fpr[rD(ctx->opcode)], 1, Rc(ctx->opcode) != 0);
79aceca5
FB
2188}
2189
2190/*** Floating-Point multiply-and-add ***/
4ecc3190 2191/* fmadd - fmadds */
7c58044c 2192GEN_FLOAT_ACB(madd, 0x1D, 1, PPC_FLOAT);
4ecc3190 2193/* fmsub - fmsubs */
7c58044c 2194GEN_FLOAT_ACB(msub, 0x1C, 1, PPC_FLOAT);
4ecc3190 2195/* fnmadd - fnmadds */
7c58044c 2196GEN_FLOAT_ACB(nmadd, 0x1F, 1, PPC_FLOAT);
4ecc3190 2197/* fnmsub - fnmsubs */
7c58044c 2198GEN_FLOAT_ACB(nmsub, 0x1E, 1, PPC_FLOAT);
79aceca5
FB
2199
2200/*** Floating-Point round & convert ***/
2201/* fctiw */
7c58044c 2202GEN_FLOAT_B(ctiw, 0x0E, 0x00, 0, PPC_FLOAT);
79aceca5 2203/* fctiwz */
7c58044c 2204GEN_FLOAT_B(ctiwz, 0x0F, 0x00, 0, PPC_FLOAT);
79aceca5 2205/* frsp */
7c58044c 2206GEN_FLOAT_B(rsp, 0x0C, 0x00, 1, PPC_FLOAT);
426613db
JM
2207#if defined(TARGET_PPC64)
2208/* fcfid */
7c58044c 2209GEN_FLOAT_B(cfid, 0x0E, 0x1A, 1, PPC_64B);
426613db 2210/* fctid */
7c58044c 2211GEN_FLOAT_B(ctid, 0x0E, 0x19, 0, PPC_64B);
426613db 2212/* fctidz */
7c58044c 2213GEN_FLOAT_B(ctidz, 0x0F, 0x19, 0, PPC_64B);
426613db 2214#endif
79aceca5 2215
d7e4b87e 2216/* frin */
7c58044c 2217GEN_FLOAT_B(rin, 0x08, 0x0C, 1, PPC_FLOAT_EXT);
d7e4b87e 2218/* friz */
7c58044c 2219GEN_FLOAT_B(riz, 0x08, 0x0D, 1, PPC_FLOAT_EXT);
d7e4b87e 2220/* frip */
7c58044c 2221GEN_FLOAT_B(rip, 0x08, 0x0E, 1, PPC_FLOAT_EXT);
d7e4b87e 2222/* frim */
7c58044c 2223GEN_FLOAT_B(rim, 0x08, 0x0F, 1, PPC_FLOAT_EXT);
d7e4b87e 2224
79aceca5 2225/*** Floating-Point compare ***/
99e300ef 2226
54623277 2227/* fcmpo */
99e300ef 2228static void gen_fcmpo(DisasContext *ctx)
79aceca5 2229{
330c483b 2230 TCGv_i32 crf;
76a66253 2231 if (unlikely(!ctx->fpu_enabled)) {
e06fcd75 2232 gen_exception(ctx, POWERPC_EXCP_FPU);
3cc62370
FB
2233 return;
2234 }
eb44b959
AJ
2235 /* NIP cannot be restored if the memory exception comes from an helper */
2236 gen_update_nip(ctx, ctx->nip - 4);
7c58044c 2237 gen_reset_fpstatus();
9a819377 2238 crf = tcg_const_i32(crfD(ctx->opcode));
8e703949
BS
2239 gen_helper_fcmpo(cpu_env, cpu_fpr[rA(ctx->opcode)],
2240 cpu_fpr[rB(ctx->opcode)], crf);
330c483b 2241 tcg_temp_free_i32(crf);
8e703949 2242 gen_helper_float_check_status(cpu_env);
79aceca5
FB
2243}
2244
2245/* fcmpu */
99e300ef 2246static void gen_fcmpu(DisasContext *ctx)
79aceca5 2247{
330c483b 2248 TCGv_i32 crf;
76a66253 2249 if (unlikely(!ctx->fpu_enabled)) {
e06fcd75 2250 gen_exception(ctx, POWERPC_EXCP_FPU);
3cc62370
FB
2251 return;
2252 }
eb44b959
AJ
2253 /* NIP cannot be restored if the memory exception comes from an helper */
2254 gen_update_nip(ctx, ctx->nip - 4);
7c58044c 2255 gen_reset_fpstatus();
9a819377 2256 crf = tcg_const_i32(crfD(ctx->opcode));
8e703949
BS
2257 gen_helper_fcmpu(cpu_env, cpu_fpr[rA(ctx->opcode)],
2258 cpu_fpr[rB(ctx->opcode)], crf);
330c483b 2259 tcg_temp_free_i32(crf);
8e703949 2260 gen_helper_float_check_status(cpu_env);
79aceca5
FB
2261}
2262
9a64fbe4
FB
2263/*** Floating-point move ***/
2264/* fabs */
7c58044c 2265/* XXX: beware that fabs never checks for NaNs nor update FPSCR */
bf45a2e6
AJ
2266static void gen_fabs(DisasContext *ctx)
2267{
2268 if (unlikely(!ctx->fpu_enabled)) {
2269 gen_exception(ctx, POWERPC_EXCP_FPU);
2270 return;
2271 }
2272 tcg_gen_andi_i64(cpu_fpr[rD(ctx->opcode)], cpu_fpr[rB(ctx->opcode)],
2273 ~(1ULL << 63));
2274 gen_compute_fprf(cpu_fpr[rD(ctx->opcode)], 0, Rc(ctx->opcode) != 0);
2275}
9a64fbe4
FB
2276
2277/* fmr - fmr. */
7c58044c 2278/* XXX: beware that fmr never checks for NaNs nor update FPSCR */
99e300ef 2279static void gen_fmr(DisasContext *ctx)
9a64fbe4 2280{
76a66253 2281 if (unlikely(!ctx->fpu_enabled)) {
e06fcd75 2282 gen_exception(ctx, POWERPC_EXCP_FPU);
3cc62370
FB
2283 return;
2284 }
af12906f
AJ
2285 tcg_gen_mov_i64(cpu_fpr[rD(ctx->opcode)], cpu_fpr[rB(ctx->opcode)]);
2286 gen_compute_fprf(cpu_fpr[rD(ctx->opcode)], 0, Rc(ctx->opcode) != 0);
9a64fbe4
FB
2287}
2288
2289/* fnabs */
7c58044c 2290/* XXX: beware that fnabs never checks for NaNs nor update FPSCR */
bf45a2e6
AJ
2291static void gen_fnabs(DisasContext *ctx)
2292{
2293 if (unlikely(!ctx->fpu_enabled)) {
2294 gen_exception(ctx, POWERPC_EXCP_FPU);
2295 return;
2296 }
2297 tcg_gen_ori_i64(cpu_fpr[rD(ctx->opcode)], cpu_fpr[rB(ctx->opcode)],
2298 1ULL << 63);
2299 gen_compute_fprf(cpu_fpr[rD(ctx->opcode)], 0, Rc(ctx->opcode) != 0);
2300}
2301
9a64fbe4 2302/* fneg */
7c58044c 2303/* XXX: beware that fneg never checks for NaNs nor update FPSCR */
bf45a2e6
AJ
2304static void gen_fneg(DisasContext *ctx)
2305{
2306 if (unlikely(!ctx->fpu_enabled)) {
2307 gen_exception(ctx, POWERPC_EXCP_FPU);
2308 return;
2309 }
2310 tcg_gen_xori_i64(cpu_fpr[rD(ctx->opcode)], cpu_fpr[rB(ctx->opcode)],
2311 1ULL << 63);
2312 gen_compute_fprf(cpu_fpr[rD(ctx->opcode)], 0, Rc(ctx->opcode) != 0);
2313}
9a64fbe4 2314
f0332888
AJ
2315/* fcpsgn: PowerPC 2.05 specification */
2316/* XXX: beware that fcpsgn never checks for NaNs nor update FPSCR */
2317static void gen_fcpsgn(DisasContext *ctx)
2318{
2319 if (unlikely(!ctx->fpu_enabled)) {
2320 gen_exception(ctx, POWERPC_EXCP_FPU);
2321 return;
2322 }
2323 tcg_gen_deposit_i64(cpu_fpr[rD(ctx->opcode)], cpu_fpr[rA(ctx->opcode)],
2324 cpu_fpr[rB(ctx->opcode)], 0, 63);
2325 gen_compute_fprf(cpu_fpr[rD(ctx->opcode)], 0, Rc(ctx->opcode) != 0);
2326}
2327
097ec5d8
TM
2328static void gen_fmrgew(DisasContext *ctx)
2329{
2330 TCGv_i64 b0;
2331 if (unlikely(!ctx->fpu_enabled)) {
2332 gen_exception(ctx, POWERPC_EXCP_FPU);
2333 return;
2334 }
2335 b0 = tcg_temp_new_i64();
2336 tcg_gen_shri_i64(b0, cpu_fpr[rB(ctx->opcode)], 32);
2337 tcg_gen_deposit_i64(cpu_fpr[rD(ctx->opcode)], cpu_fpr[rA(ctx->opcode)],
2338 b0, 0, 32);
2339 tcg_temp_free_i64(b0);
2340}
2341
2342static void gen_fmrgow(DisasContext *ctx)
2343{
2344 if (unlikely(!ctx->fpu_enabled)) {
2345 gen_exception(ctx, POWERPC_EXCP_FPU);
2346 return;
2347 }
2348 tcg_gen_deposit_i64(cpu_fpr[rD(ctx->opcode)],
2349 cpu_fpr[rB(ctx->opcode)],
2350 cpu_fpr[rA(ctx->opcode)],
2351 32, 32);
2352}
2353
79aceca5 2354/*** Floating-Point status & ctrl register ***/
99e300ef 2355
54623277 2356/* mcrfs */
99e300ef 2357static void gen_mcrfs(DisasContext *ctx)
79aceca5 2358{
30304420 2359 TCGv tmp = tcg_temp_new();
7c58044c
JM
2360 int bfa;
2361
76a66253 2362 if (unlikely(!ctx->fpu_enabled)) {
e06fcd75 2363 gen_exception(ctx, POWERPC_EXCP_FPU);
3cc62370
FB
2364 return;
2365 }
7c58044c 2366 bfa = 4 * (7 - crfS(ctx->opcode));
30304420
DG
2367 tcg_gen_shri_tl(tmp, cpu_fpscr, bfa);
2368 tcg_gen_trunc_tl_i32(cpu_crf[crfD(ctx->opcode)], tmp);
2369 tcg_temp_free(tmp);
e1571908 2370 tcg_gen_andi_i32(cpu_crf[crfD(ctx->opcode)], cpu_crf[crfD(ctx->opcode)], 0xf);
30304420 2371 tcg_gen_andi_tl(cpu_fpscr, cpu_fpscr, ~(0xF << bfa));
79aceca5
FB
2372}
2373
2374/* mffs */
99e300ef 2375static void gen_mffs(DisasContext *ctx)
79aceca5 2376{
76a66253 2377 if (unlikely(!ctx->fpu_enabled)) {
e06fcd75 2378 gen_exception(ctx, POWERPC_EXCP_FPU);
3cc62370
FB
2379 return;
2380 }
7c58044c 2381 gen_reset_fpstatus();
30304420 2382 tcg_gen_extu_tl_i64(cpu_fpr[rD(ctx->opcode)], cpu_fpscr);
af12906f 2383 gen_compute_fprf(cpu_fpr[rD(ctx->opcode)], 0, Rc(ctx->opcode) != 0);
79aceca5
FB
2384}
2385
2386/* mtfsb0 */
99e300ef 2387static void gen_mtfsb0(DisasContext *ctx)
79aceca5 2388{
fb0eaffc 2389 uint8_t crb;
3b46e624 2390
76a66253 2391 if (unlikely(!ctx->fpu_enabled)) {
e06fcd75 2392 gen_exception(ctx, POWERPC_EXCP_FPU);
3cc62370
FB
2393 return;
2394 }
6e35d524 2395 crb = 31 - crbD(ctx->opcode);
7c58044c 2396 gen_reset_fpstatus();
6e35d524 2397 if (likely(crb != FPSCR_FEX && crb != FPSCR_VX)) {
eb44b959
AJ
2398 TCGv_i32 t0;
2399 /* NIP cannot be restored if the memory exception comes from an helper */
2400 gen_update_nip(ctx, ctx->nip - 4);
2401 t0 = tcg_const_i32(crb);
8e703949 2402 gen_helper_fpscr_clrbit(cpu_env, t0);
6e35d524
AJ
2403 tcg_temp_free_i32(t0);
2404 }
7c58044c 2405 if (unlikely(Rc(ctx->opcode) != 0)) {
30304420
DG
2406 tcg_gen_trunc_tl_i32(cpu_crf[1], cpu_fpscr);
2407 tcg_gen_shri_i32(cpu_crf[1], cpu_crf[1], FPSCR_OX);
7c58044c 2408 }
79aceca5
FB
2409}
2410
2411/* mtfsb1 */
99e300ef 2412static void gen_mtfsb1(DisasContext *ctx)
79aceca5 2413{
fb0eaffc 2414 uint8_t crb;
3b46e624 2415
76a66253 2416 if (unlikely(!ctx->fpu_enabled)) {
e06fcd75 2417 gen_exception(ctx, POWERPC_EXCP_FPU);
3cc62370
FB
2418 return;
2419 }
6e35d524 2420 crb = 31 - crbD(ctx->opcode);
7c58044c
JM
2421 gen_reset_fpstatus();
2422 /* XXX: we pretend we can only do IEEE floating-point computations */
af12906f 2423 if (likely(crb != FPSCR_FEX && crb != FPSCR_VX && crb != FPSCR_NI)) {
eb44b959
AJ
2424 TCGv_i32 t0;
2425 /* NIP cannot be restored if the memory exception comes from an helper */
2426 gen_update_nip(ctx, ctx->nip - 4);
2427 t0 = tcg_const_i32(crb);
8e703949 2428 gen_helper_fpscr_setbit(cpu_env, t0);
0f2f39c2 2429 tcg_temp_free_i32(t0);
af12906f 2430 }
7c58044c 2431 if (unlikely(Rc(ctx->opcode) != 0)) {
30304420
DG
2432 tcg_gen_trunc_tl_i32(cpu_crf[1], cpu_fpscr);
2433 tcg_gen_shri_i32(cpu_crf[1], cpu_crf[1], FPSCR_OX);
7c58044c
JM
2434 }
2435 /* We can raise a differed exception */
8e703949 2436 gen_helper_float_check_status(cpu_env);
79aceca5
FB
2437}
2438
2439/* mtfsf */
99e300ef 2440static void gen_mtfsf(DisasContext *ctx)
79aceca5 2441{
0f2f39c2 2442 TCGv_i32 t0;
7d08d856 2443 int flm, l, w;
af12906f 2444
76a66253 2445 if (unlikely(!ctx->fpu_enabled)) {
e06fcd75 2446 gen_exception(ctx, POWERPC_EXCP_FPU);
3cc62370
FB
2447 return;
2448 }
7d08d856
AJ
2449 flm = FPFLM(ctx->opcode);
2450 l = FPL(ctx->opcode);
2451 w = FPW(ctx->opcode);
2452 if (unlikely(w & !(ctx->insns_flags2 & PPC2_ISA205))) {
2453 gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL);
2454 return;
2455 }
eb44b959
AJ
2456 /* NIP cannot be restored if the memory exception comes from an helper */
2457 gen_update_nip(ctx, ctx->nip - 4);
7c58044c 2458 gen_reset_fpstatus();
7d08d856
AJ
2459 if (l) {
2460 t0 = tcg_const_i32((ctx->insns_flags2 & PPC2_ISA205) ? 0xffff : 0xff);
2461 } else {
2462 t0 = tcg_const_i32(flm << (w * 8));
2463 }
8e703949 2464 gen_helper_store_fpscr(cpu_env, cpu_fpr[rB(ctx->opcode)], t0);
0f2f39c2 2465 tcg_temp_free_i32(t0);
7c58044c 2466 if (unlikely(Rc(ctx->opcode) != 0)) {
30304420
DG
2467 tcg_gen_trunc_tl_i32(cpu_crf[1], cpu_fpscr);
2468 tcg_gen_shri_i32(cpu_crf[1], cpu_crf[1], FPSCR_OX);
7c58044c
JM
2469 }
2470 /* We can raise a differed exception */
8e703949 2471 gen_helper_float_check_status(cpu_env);
79aceca5
FB
2472}
2473
2474/* mtfsfi */
99e300ef 2475static void gen_mtfsfi(DisasContext *ctx)
79aceca5 2476{
7d08d856 2477 int bf, sh, w;
0f2f39c2
AJ
2478 TCGv_i64 t0;
2479 TCGv_i32 t1;
7c58044c 2480
76a66253 2481 if (unlikely(!ctx->fpu_enabled)) {
e06fcd75 2482 gen_exception(ctx, POWERPC_EXCP_FPU);
3cc62370
FB
2483 return;
2484 }
7d08d856
AJ
2485 w = FPW(ctx->opcode);
2486 bf = FPBF(ctx->opcode);
2487 if (unlikely(w & !(ctx->insns_flags2 & PPC2_ISA205))) {
2488 gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL);
2489 return;
2490 }
2491 sh = (8 * w) + 7 - bf;
eb44b959
AJ
2492 /* NIP cannot be restored if the memory exception comes from an helper */
2493 gen_update_nip(ctx, ctx->nip - 4);
7c58044c 2494 gen_reset_fpstatus();
7d08d856 2495 t0 = tcg_const_i64(((uint64_t)FPIMM(ctx->opcode)) << (4 * sh));
af12906f 2496 t1 = tcg_const_i32(1 << sh);
8e703949 2497 gen_helper_store_fpscr(cpu_env, t0, t1);
0f2f39c2
AJ
2498 tcg_temp_free_i64(t0);
2499 tcg_temp_free_i32(t1);
7c58044c 2500 if (unlikely(Rc(ctx->opcode) != 0)) {
30304420
DG
2501 tcg_gen_trunc_tl_i32(cpu_crf[1], cpu_fpscr);
2502 tcg_gen_shri_i32(cpu_crf[1], cpu_crf[1], FPSCR_OX);
7c58044c
JM
2503 }
2504 /* We can raise a differed exception */
8e703949 2505 gen_helper_float_check_status(cpu_env);
79aceca5
FB
2506}
2507
76a66253
JM
2508/*** Addressing modes ***/
2509/* Register indirect with immediate index : EA = (rA|0) + SIMM */
636aa200
BS
2510static inline void gen_addr_imm_index(DisasContext *ctx, TCGv EA,
2511 target_long maskl)
76a66253
JM
2512{
2513 target_long simm = SIMM(ctx->opcode);
2514
be147d08 2515 simm &= ~maskl;
76db3ba4 2516 if (rA(ctx->opcode) == 0) {
c791fe84
RH
2517 if (NARROW_MODE(ctx)) {
2518 simm = (uint32_t)simm;
2519 }
e2be8d8d 2520 tcg_gen_movi_tl(EA, simm);
76db3ba4 2521 } else if (likely(simm != 0)) {
e2be8d8d 2522 tcg_gen_addi_tl(EA, cpu_gpr[rA(ctx->opcode)], simm);
c791fe84 2523 if (NARROW_MODE(ctx)) {
76db3ba4
AJ
2524 tcg_gen_ext32u_tl(EA, EA);
2525 }
76db3ba4 2526 } else {
c791fe84 2527 if (NARROW_MODE(ctx)) {
76db3ba4 2528 tcg_gen_ext32u_tl(EA, cpu_gpr[rA(ctx->opcode)]);
c791fe84
RH
2529 } else {
2530 tcg_gen_mov_tl(EA, cpu_gpr[rA(ctx->opcode)]);
2531 }
76db3ba4 2532 }
76a66253
JM
2533}
2534
636aa200 2535static inline void gen_addr_reg_index(DisasContext *ctx, TCGv EA)
76a66253 2536{
76db3ba4 2537 if (rA(ctx->opcode) == 0) {
c791fe84 2538 if (NARROW_MODE(ctx)) {
76db3ba4 2539 tcg_gen_ext32u_tl(EA, cpu_gpr[rB(ctx->opcode)]);
c791fe84
RH
2540 } else {
2541 tcg_gen_mov_tl(EA, cpu_gpr[rB(ctx->opcode)]);
2542 }
76db3ba4 2543 } else {
e2be8d8d 2544 tcg_gen_add_tl(EA, cpu_gpr[rA(ctx->opcode)], cpu_gpr[rB(ctx->opcode)]);
c791fe84 2545 if (NARROW_MODE(ctx)) {
76db3ba4
AJ
2546 tcg_gen_ext32u_tl(EA, EA);
2547 }
76db3ba4 2548 }
76a66253
JM
2549}
2550
636aa200 2551static inline void gen_addr_register(DisasContext *ctx, TCGv EA)
76a66253 2552{
76db3ba4 2553 if (rA(ctx->opcode) == 0) {
e2be8d8d 2554 tcg_gen_movi_tl(EA, 0);
c791fe84
RH
2555 } else if (NARROW_MODE(ctx)) {
2556 tcg_gen_ext32u_tl(EA, cpu_gpr[rA(ctx->opcode)]);
76db3ba4 2557 } else {
c791fe84 2558 tcg_gen_mov_tl(EA, cpu_gpr[rA(ctx->opcode)]);
76db3ba4
AJ
2559 }
2560}
2561
636aa200
BS
2562static inline void gen_addr_add(DisasContext *ctx, TCGv ret, TCGv arg1,
2563 target_long val)
76db3ba4
AJ
2564{
2565 tcg_gen_addi_tl(ret, arg1, val);
c791fe84 2566 if (NARROW_MODE(ctx)) {
76db3ba4
AJ
2567 tcg_gen_ext32u_tl(ret, ret);
2568 }
76a66253
JM
2569}
2570
636aa200 2571static inline void gen_check_align(DisasContext *ctx, TCGv EA, int mask)
cf360a32
AJ
2572{
2573 int l1 = gen_new_label();
2574 TCGv t0 = tcg_temp_new();
2575 TCGv_i32 t1, t2;
2576 /* NIP cannot be restored if the memory exception comes from an helper */
2577 gen_update_nip(ctx, ctx->nip - 4);
2578 tcg_gen_andi_tl(t0, EA, mask);
2579 tcg_gen_brcondi_tl(TCG_COND_EQ, t0, 0, l1);
2580 t1 = tcg_const_i32(POWERPC_EXCP_ALIGN);
2581 t2 = tcg_const_i32(0);
e5f17ac6 2582 gen_helper_raise_exception_err(cpu_env, t1, t2);
cf360a32
AJ
2583 tcg_temp_free_i32(t1);
2584 tcg_temp_free_i32(t2);
2585 gen_set_label(l1);
2586 tcg_temp_free(t0);
2587}
2588
7863667f 2589/*** Integer load ***/
636aa200 2590static inline void gen_qemu_ld8u(DisasContext *ctx, TCGv arg1, TCGv arg2)
76db3ba4
AJ
2591{
2592 tcg_gen_qemu_ld8u(arg1, arg2, ctx->mem_idx);
2593}
2594
636aa200 2595static inline void gen_qemu_ld8s(DisasContext *ctx, TCGv arg1, TCGv arg2)
76db3ba4
AJ
2596{
2597 tcg_gen_qemu_ld8s(arg1, arg2, ctx->mem_idx);
2598}
2599
636aa200 2600static inline void gen_qemu_ld16u(DisasContext *ctx, TCGv arg1, TCGv arg2)
76db3ba4
AJ
2601{
2602 tcg_gen_qemu_ld16u(arg1, arg2, ctx->mem_idx);
2603 if (unlikely(ctx->le_mode)) {
fa3966a3 2604 tcg_gen_bswap16_tl(arg1, arg1);
76db3ba4 2605 }
b61f2753
AJ
2606}
2607
636aa200 2608static inline void gen_qemu_ld16s(DisasContext *ctx, TCGv arg1, TCGv arg2)
b61f2753 2609{
76db3ba4 2610 if (unlikely(ctx->le_mode)) {
76db3ba4 2611 tcg_gen_qemu_ld16u(arg1, arg2, ctx->mem_idx);
fa3966a3 2612 tcg_gen_bswap16_tl(arg1, arg1);
76db3ba4 2613 tcg_gen_ext16s_tl(arg1, arg1);
76db3ba4
AJ
2614 } else {
2615 tcg_gen_qemu_ld16s(arg1, arg2, ctx->mem_idx);
2616 }
b61f2753
AJ
2617}
2618
636aa200 2619static inline void gen_qemu_ld32u(DisasContext *ctx, TCGv arg1, TCGv arg2)
b61f2753 2620{
76db3ba4
AJ
2621 tcg_gen_qemu_ld32u(arg1, arg2, ctx->mem_idx);
2622 if (unlikely(ctx->le_mode)) {
fa3966a3 2623 tcg_gen_bswap32_tl(arg1, arg1);
76db3ba4 2624 }
b61f2753
AJ
2625}
2626
f976b09e
AG
2627static void gen_qemu_ld32u_i64(DisasContext *ctx, TCGv_i64 val, TCGv addr)
2628{
2629 TCGv tmp = tcg_temp_new();
2630 gen_qemu_ld32u(ctx, tmp, addr);
2631 tcg_gen_extu_tl_i64(val, tmp);
2632 tcg_temp_free(tmp);
2633}
2634
636aa200 2635static inline void gen_qemu_ld32s(DisasContext *ctx, TCGv arg1, TCGv arg2)
b61f2753 2636{
a457e7ee 2637 if (unlikely(ctx->le_mode)) {
76db3ba4 2638 tcg_gen_qemu_ld32u(arg1, arg2, ctx->mem_idx);
fa3966a3
AJ
2639 tcg_gen_bswap32_tl(arg1, arg1);
2640 tcg_gen_ext32s_tl(arg1, arg1);
b61f2753 2641 } else
76db3ba4 2642 tcg_gen_qemu_ld32s(arg1, arg2, ctx->mem_idx);
b61f2753
AJ
2643}
2644
cac7f0ba
TM
2645static void gen_qemu_ld32s_i64(DisasContext *ctx, TCGv_i64 val, TCGv addr)
2646{
2647 TCGv tmp = tcg_temp_new();
2648 gen_qemu_ld32s(ctx, tmp, addr);
2649 tcg_gen_ext_tl_i64(val, tmp);
2650 tcg_temp_free(tmp);
2651}
2652
636aa200 2653static inline void gen_qemu_ld64(DisasContext *ctx, TCGv_i64 arg1, TCGv arg2)
b61f2753 2654{
76db3ba4
AJ
2655 tcg_gen_qemu_ld64(arg1, arg2, ctx->mem_idx);
2656 if (unlikely(ctx->le_mode)) {
66896cb8 2657 tcg_gen_bswap64_i64(arg1, arg1);
76db3ba4 2658 }
b61f2753
AJ
2659}
2660
636aa200 2661static inline void gen_qemu_st8(DisasContext *ctx, TCGv arg1, TCGv arg2)
b61f2753 2662{
76db3ba4 2663 tcg_gen_qemu_st8(arg1, arg2, ctx->mem_idx);
b61f2753
AJ
2664}
2665
636aa200 2666static inline void gen_qemu_st16(DisasContext *ctx, TCGv arg1, TCGv arg2)
b61f2753 2667{
76db3ba4 2668 if (unlikely(ctx->le_mode)) {
76db3ba4
AJ
2669 TCGv t0 = tcg_temp_new();
2670 tcg_gen_ext16u_tl(t0, arg1);
fa3966a3 2671 tcg_gen_bswap16_tl(t0, t0);
76db3ba4
AJ
2672 tcg_gen_qemu_st16(t0, arg2, ctx->mem_idx);
2673 tcg_temp_free(t0);
76db3ba4
AJ
2674 } else {
2675 tcg_gen_qemu_st16(arg1, arg2, ctx->mem_idx);
2676 }
b61f2753
AJ
2677}
2678
636aa200 2679static inline void gen_qemu_st32(DisasContext *ctx, TCGv arg1, TCGv arg2)
b61f2753 2680{
76db3ba4 2681 if (unlikely(ctx->le_mode)) {
fa3966a3
AJ
2682 TCGv t0 = tcg_temp_new();
2683 tcg_gen_ext32u_tl(t0, arg1);
2684 tcg_gen_bswap32_tl(t0, t0);
76db3ba4
AJ
2685 tcg_gen_qemu_st32(t0, arg2, ctx->mem_idx);
2686 tcg_temp_free(t0);
76db3ba4
AJ
2687 } else {
2688 tcg_gen_qemu_st32(arg1, arg2, ctx->mem_idx);
2689 }
b61f2753
AJ
2690}
2691
f976b09e
AG
2692static void gen_qemu_st32_i64(DisasContext *ctx, TCGv_i64 val, TCGv addr)
2693{
2694 TCGv tmp = tcg_temp_new();
2695 tcg_gen_trunc_i64_tl(tmp, val);
2696 gen_qemu_st32(ctx, tmp, addr);
2697 tcg_temp_free(tmp);
2698}
2699
636aa200 2700static inline void gen_qemu_st64(DisasContext *ctx, TCGv_i64 arg1, TCGv arg2)
b61f2753 2701{
76db3ba4 2702 if (unlikely(ctx->le_mode)) {
a7812ae4 2703 TCGv_i64 t0 = tcg_temp_new_i64();
66896cb8 2704 tcg_gen_bswap64_i64(t0, arg1);
76db3ba4 2705 tcg_gen_qemu_st64(t0, arg2, ctx->mem_idx);
a7812ae4 2706 tcg_temp_free_i64(t0);
b61f2753 2707 } else
76db3ba4 2708 tcg_gen_qemu_st64(arg1, arg2, ctx->mem_idx);
b61f2753
AJ
2709}
2710
0c8aacd4 2711#define GEN_LD(name, ldop, opc, type) \
99e300ef 2712static void glue(gen_, name)(DisasContext *ctx) \
79aceca5 2713{ \
76db3ba4
AJ
2714 TCGv EA; \
2715 gen_set_access_type(ctx, ACCESS_INT); \
2716 EA = tcg_temp_new(); \
2717 gen_addr_imm_index(ctx, EA, 0); \
2718 gen_qemu_##ldop(ctx, cpu_gpr[rD(ctx->opcode)], EA); \
b61f2753 2719 tcg_temp_free(EA); \
79aceca5
FB
2720}
2721
0c8aacd4 2722#define GEN_LDU(name, ldop, opc, type) \
99e300ef 2723static void glue(gen_, name##u)(DisasContext *ctx) \
79aceca5 2724{ \
b61f2753 2725 TCGv EA; \
76a66253
JM
2726 if (unlikely(rA(ctx->opcode) == 0 || \
2727 rA(ctx->opcode) == rD(ctx->opcode))) { \
e06fcd75 2728 gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL); \
9fddaa0c 2729 return; \
9a64fbe4 2730 } \
76db3ba4 2731 gen_set_access_type(ctx, ACCESS_INT); \
0c8aacd4 2732 EA = tcg_temp_new(); \
9d53c753 2733 if (type == PPC_64B) \
76db3ba4 2734 gen_addr_imm_index(ctx, EA, 0x03); \
9d53c753 2735 else \
76db3ba4
AJ
2736 gen_addr_imm_index(ctx, EA, 0); \
2737 gen_qemu_##ldop(ctx, cpu_gpr[rD(ctx->opcode)], EA); \
b61f2753
AJ
2738 tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], EA); \
2739 tcg_temp_free(EA); \
79aceca5
FB
2740}
2741
0c8aacd4 2742#define GEN_LDUX(name, ldop, opc2, opc3, type) \
99e300ef 2743static void glue(gen_, name##ux)(DisasContext *ctx) \
79aceca5 2744{ \
b61f2753 2745 TCGv EA; \
76a66253
JM
2746 if (unlikely(rA(ctx->opcode) == 0 || \
2747 rA(ctx->opcode) == rD(ctx->opcode))) { \
e06fcd75 2748 gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL); \
9fddaa0c 2749 return; \
9a64fbe4 2750 } \
76db3ba4 2751 gen_set_access_type(ctx, ACCESS_INT); \
0c8aacd4 2752 EA = tcg_temp_new(); \
76db3ba4
AJ
2753 gen_addr_reg_index(ctx, EA); \
2754 gen_qemu_##ldop(ctx, cpu_gpr[rD(ctx->opcode)], EA); \
b61f2753
AJ
2755 tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], EA); \
2756 tcg_temp_free(EA); \
79aceca5
FB
2757}
2758
cd6e9320 2759#define GEN_LDX_E(name, ldop, opc2, opc3, type, type2) \
99e300ef 2760static void glue(gen_, name##x)(DisasContext *ctx) \
79aceca5 2761{ \
76db3ba4
AJ
2762 TCGv EA; \
2763 gen_set_access_type(ctx, ACCESS_INT); \
2764 EA = tcg_temp_new(); \
2765 gen_addr_reg_index(ctx, EA); \
2766 gen_qemu_##ldop(ctx, cpu_gpr[rD(ctx->opcode)], EA); \
b61f2753 2767 tcg_temp_free(EA); \
79aceca5 2768}
cd6e9320
TH
2769#define GEN_LDX(name, ldop, opc2, opc3, type) \
2770 GEN_LDX_E(name, ldop, opc2, opc3, type, PPC_NONE)
79aceca5 2771
0c8aacd4
AJ
2772#define GEN_LDS(name, ldop, op, type) \
2773GEN_LD(name, ldop, op | 0x20, type); \
2774GEN_LDU(name, ldop, op | 0x21, type); \
2775GEN_LDUX(name, ldop, 0x17, op | 0x01, type); \
2776GEN_LDX(name, ldop, 0x17, op | 0x00, type)
79aceca5
FB
2777
2778/* lbz lbzu lbzux lbzx */
0c8aacd4 2779GEN_LDS(lbz, ld8u, 0x02, PPC_INTEGER);
79aceca5 2780/* lha lhau lhaux lhax */
0c8aacd4 2781GEN_LDS(lha, ld16s, 0x0A, PPC_INTEGER);
79aceca5 2782/* lhz lhzu lhzux lhzx */
0c8aacd4 2783GEN_LDS(lhz, ld16u, 0x08, PPC_INTEGER);
79aceca5 2784/* lwz lwzu lwzux lwzx */
0c8aacd4 2785GEN_LDS(lwz, ld32u, 0x00, PPC_INTEGER);
d9bce9d9 2786#if defined(TARGET_PPC64)
d9bce9d9 2787/* lwaux */
0c8aacd4 2788GEN_LDUX(lwa, ld32s, 0x15, 0x0B, PPC_64B);
d9bce9d9 2789/* lwax */
0c8aacd4 2790GEN_LDX(lwa, ld32s, 0x15, 0x0A, PPC_64B);
d9bce9d9 2791/* ldux */
0c8aacd4 2792GEN_LDUX(ld, ld64, 0x15, 0x01, PPC_64B);
d9bce9d9 2793/* ldx */
0c8aacd4 2794GEN_LDX(ld, ld64, 0x15, 0x00, PPC_64B);
99e300ef
BS
2795
2796static void gen_ld(DisasContext *ctx)
d9bce9d9 2797{
b61f2753 2798 TCGv EA;
d9bce9d9
JM
2799 if (Rc(ctx->opcode)) {
2800 if (unlikely(rA(ctx->opcode) == 0 ||
2801 rA(ctx->opcode) == rD(ctx->opcode))) {
e06fcd75 2802 gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL);
d9bce9d9
JM
2803 return;
2804 }
2805 }
76db3ba4 2806 gen_set_access_type(ctx, ACCESS_INT);
a7812ae4 2807 EA = tcg_temp_new();
76db3ba4 2808 gen_addr_imm_index(ctx, EA, 0x03);
d9bce9d9
JM
2809 if (ctx->opcode & 0x02) {
2810 /* lwa (lwau is undefined) */
76db3ba4 2811 gen_qemu_ld32s(ctx, cpu_gpr[rD(ctx->opcode)], EA);
d9bce9d9
JM
2812 } else {
2813 /* ld - ldu */
76db3ba4 2814 gen_qemu_ld64(ctx, cpu_gpr[rD(ctx->opcode)], EA);
d9bce9d9 2815 }
d9bce9d9 2816 if (Rc(ctx->opcode))
b61f2753
AJ
2817 tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], EA);
2818 tcg_temp_free(EA);
d9bce9d9 2819}
99e300ef 2820
54623277 2821/* lq */
99e300ef 2822static void gen_lq(DisasContext *ctx)
be147d08
JM
2823{
2824#if defined(CONFIG_USER_ONLY)
e06fcd75 2825 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
be147d08
JM
2826#else
2827 int ra, rd;
b61f2753 2828 TCGv EA;
be147d08
JM
2829
2830 /* Restore CPU state */
76db3ba4 2831 if (unlikely(ctx->mem_idx == 0)) {
e06fcd75 2832 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
be147d08
JM
2833 return;
2834 }
2835 ra = rA(ctx->opcode);
2836 rd = rD(ctx->opcode);
2837 if (unlikely((rd & 1) || rd == ra)) {
e06fcd75 2838 gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL);
be147d08
JM
2839 return;
2840 }
76db3ba4 2841 if (unlikely(ctx->le_mode)) {
be147d08 2842 /* Little-endian mode is not handled */
e06fcd75 2843 gen_exception_err(ctx, POWERPC_EXCP_ALIGN, POWERPC_EXCP_ALIGN_LE);
be147d08
JM
2844 return;
2845 }
76db3ba4 2846 gen_set_access_type(ctx, ACCESS_INT);
a7812ae4 2847 EA = tcg_temp_new();
76db3ba4
AJ
2848 gen_addr_imm_index(ctx, EA, 0x0F);
2849 gen_qemu_ld64(ctx, cpu_gpr[rd], EA);
2850 gen_addr_add(ctx, EA, EA, 8);
2851 gen_qemu_ld64(ctx, cpu_gpr[rd+1], EA);
b61f2753 2852 tcg_temp_free(EA);
be147d08
JM
2853#endif
2854}
d9bce9d9 2855#endif
79aceca5
FB
2856
2857/*** Integer store ***/
0c8aacd4 2858#define GEN_ST(name, stop, opc, type) \
99e300ef 2859static void glue(gen_, name)(DisasContext *ctx) \
79aceca5 2860{ \
76db3ba4
AJ
2861 TCGv EA; \
2862 gen_set_access_type(ctx, ACCESS_INT); \
2863 EA = tcg_temp_new(); \
2864 gen_addr_imm_index(ctx, EA, 0); \
2865 gen_qemu_##stop(ctx, cpu_gpr[rS(ctx->opcode)], EA); \
b61f2753 2866 tcg_temp_free(EA); \
79aceca5
FB
2867}
2868
0c8aacd4 2869#define GEN_STU(name, stop, opc, type) \
99e300ef 2870static void glue(gen_, stop##u)(DisasContext *ctx) \
79aceca5 2871{ \
b61f2753 2872 TCGv EA; \
76a66253 2873 if (unlikely(rA(ctx->opcode) == 0)) { \
e06fcd75 2874 gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL); \
9fddaa0c 2875 return; \
9a64fbe4 2876 } \
76db3ba4 2877 gen_set_access_type(ctx, ACCESS_INT); \
0c8aacd4 2878 EA = tcg_temp_new(); \
9d53c753 2879 if (type == PPC_64B) \
76db3ba4 2880 gen_addr_imm_index(ctx, EA, 0x03); \
9d53c753 2881 else \
76db3ba4
AJ
2882 gen_addr_imm_index(ctx, EA, 0); \
2883 gen_qemu_##stop(ctx, cpu_gpr[rS(ctx->opcode)], EA); \
b61f2753
AJ
2884 tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], EA); \
2885 tcg_temp_free(EA); \
79aceca5
FB
2886}
2887
0c8aacd4 2888#define GEN_STUX(name, stop, opc2, opc3, type) \
99e300ef 2889static void glue(gen_, name##ux)(DisasContext *ctx) \
79aceca5 2890{ \
b61f2753 2891 TCGv EA; \
76a66253 2892 if (unlikely(rA(ctx->opcode) == 0)) { \
e06fcd75 2893 gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL); \
9fddaa0c 2894 return; \
9a64fbe4 2895 } \
76db3ba4 2896 gen_set_access_type(ctx, ACCESS_INT); \
0c8aacd4 2897 EA = tcg_temp_new(); \
76db3ba4
AJ
2898 gen_addr_reg_index(ctx, EA); \
2899 gen_qemu_##stop(ctx, cpu_gpr[rS(ctx->opcode)], EA); \
b61f2753
AJ
2900 tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], EA); \
2901 tcg_temp_free(EA); \
79aceca5
FB
2902}
2903
cd6e9320
TH
2904#define GEN_STX_E(name, stop, opc2, opc3, type, type2) \
2905static void glue(gen_, name##x)(DisasContext *ctx) \
79aceca5 2906{ \
76db3ba4
AJ
2907 TCGv EA; \
2908 gen_set_access_type(ctx, ACCESS_INT); \
2909 EA = tcg_temp_new(); \
2910 gen_addr_reg_index(ctx, EA); \
2911 gen_qemu_##stop(ctx, cpu_gpr[rS(ctx->opcode)], EA); \
b61f2753 2912 tcg_temp_free(EA); \
79aceca5 2913}
cd6e9320
TH
2914#define GEN_STX(name, stop, opc2, opc3, type) \
2915 GEN_STX_E(name, stop, opc2, opc3, type, PPC_NONE)
79aceca5 2916
0c8aacd4
AJ
2917#define GEN_STS(name, stop, op, type) \
2918GEN_ST(name, stop, op | 0x20, type); \
2919GEN_STU(name, stop, op | 0x21, type); \
2920GEN_STUX(name, stop, 0x17, op | 0x01, type); \
2921GEN_STX(name, stop, 0x17, op | 0x00, type)
79aceca5
FB
2922
2923/* stb stbu stbux stbx */
0c8aacd4 2924GEN_STS(stb, st8, 0x06, PPC_INTEGER);
79aceca5 2925/* sth sthu sthux sthx */
0c8aacd4 2926GEN_STS(sth, st16, 0x0C, PPC_INTEGER);
79aceca5 2927/* stw stwu stwux stwx */
0c8aacd4 2928GEN_STS(stw, st32, 0x04, PPC_INTEGER);
d9bce9d9 2929#if defined(TARGET_PPC64)
0c8aacd4
AJ
2930GEN_STUX(std, st64, 0x15, 0x05, PPC_64B);
2931GEN_STX(std, st64, 0x15, 0x04, PPC_64B);
99e300ef
BS
2932
2933static void gen_std(DisasContext *ctx)
d9bce9d9 2934{
be147d08 2935 int rs;
b61f2753 2936 TCGv EA;
be147d08
JM
2937
2938 rs = rS(ctx->opcode);
2939 if ((ctx->opcode & 0x3) == 0x2) {
2940#if defined(CONFIG_USER_ONLY)
e06fcd75 2941 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
be147d08
JM
2942#else
2943 /* stq */
76db3ba4 2944 if (unlikely(ctx->mem_idx == 0)) {
e06fcd75 2945 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
be147d08
JM
2946 return;
2947 }
2948 if (unlikely(rs & 1)) {
e06fcd75 2949 gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL);
d9bce9d9
JM
2950 return;
2951 }
76db3ba4 2952 if (unlikely(ctx->le_mode)) {
be147d08 2953 /* Little-endian mode is not handled */
e06fcd75 2954 gen_exception_err(ctx, POWERPC_EXCP_ALIGN, POWERPC_EXCP_ALIGN_LE);
be147d08
JM
2955 return;
2956 }
76db3ba4 2957 gen_set_access_type(ctx, ACCESS_INT);
a7812ae4 2958 EA = tcg_temp_new();
76db3ba4
AJ
2959 gen_addr_imm_index(ctx, EA, 0x03);
2960 gen_qemu_st64(ctx, cpu_gpr[rs], EA);
2961 gen_addr_add(ctx, EA, EA, 8);
2962 gen_qemu_st64(ctx, cpu_gpr[rs+1], EA);
b61f2753 2963 tcg_temp_free(EA);
be147d08
JM
2964#endif
2965 } else {
2966 /* std / stdu */
2967 if (Rc(ctx->opcode)) {
2968 if (unlikely(rA(ctx->opcode) == 0)) {
e06fcd75 2969 gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL);
be147d08
JM
2970 return;
2971 }
2972 }
76db3ba4 2973 gen_set_access_type(ctx, ACCESS_INT);
a7812ae4 2974 EA = tcg_temp_new();
76db3ba4
AJ
2975 gen_addr_imm_index(ctx, EA, 0x03);
2976 gen_qemu_st64(ctx, cpu_gpr[rs], EA);
be147d08 2977 if (Rc(ctx->opcode))
b61f2753
AJ
2978 tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], EA);
2979 tcg_temp_free(EA);
d9bce9d9 2980 }
d9bce9d9
JM
2981}
2982#endif
79aceca5
FB
2983/*** Integer load and store with byte reverse ***/
2984/* lhbrx */
86178a57 2985static inline void gen_qemu_ld16ur(DisasContext *ctx, TCGv arg1, TCGv arg2)
b61f2753 2986{
76db3ba4
AJ
2987 tcg_gen_qemu_ld16u(arg1, arg2, ctx->mem_idx);
2988 if (likely(!ctx->le_mode)) {
fa3966a3 2989 tcg_gen_bswap16_tl(arg1, arg1);
76db3ba4 2990 }
b61f2753 2991}
0c8aacd4 2992GEN_LDX(lhbr, ld16ur, 0x16, 0x18, PPC_INTEGER);
b61f2753 2993
79aceca5 2994/* lwbrx */
86178a57 2995static inline void gen_qemu_ld32ur(DisasContext *ctx, TCGv arg1, TCGv arg2)
b61f2753 2996{
76db3ba4
AJ
2997 tcg_gen_qemu_ld32u(arg1, arg2, ctx->mem_idx);
2998 if (likely(!ctx->le_mode)) {
fa3966a3 2999 tcg_gen_bswap32_tl(arg1, arg1);
76db3ba4 3000 }
b61f2753 3001}
0c8aacd4 3002GEN_LDX(lwbr, ld32ur, 0x16, 0x10, PPC_INTEGER);
b61f2753 3003
cd6e9320
TH
3004#if defined(TARGET_PPC64)
3005/* ldbrx */
3006static inline void gen_qemu_ld64ur(DisasContext *ctx, TCGv arg1, TCGv arg2)
3007{
3008 tcg_gen_qemu_ld64(arg1, arg2, ctx->mem_idx);
3009 if (likely(!ctx->le_mode)) {
3010 tcg_gen_bswap64_tl(arg1, arg1);
3011 }
3012}
3013GEN_LDX_E(ldbr, ld64ur, 0x14, 0x10, PPC_NONE, PPC2_DBRX);
3014#endif /* TARGET_PPC64 */
3015
79aceca5 3016/* sthbrx */
86178a57 3017static inline void gen_qemu_st16r(DisasContext *ctx, TCGv arg1, TCGv arg2)
b61f2753 3018{
76db3ba4 3019 if (likely(!ctx->le_mode)) {
76db3ba4
AJ
3020 TCGv t0 = tcg_temp_new();
3021 tcg_gen_ext16u_tl(t0, arg1);
fa3966a3 3022 tcg_gen_bswap16_tl(t0, t0);
76db3ba4
AJ
3023 tcg_gen_qemu_st16(t0, arg2, ctx->mem_idx);
3024 tcg_temp_free(t0);
76db3ba4
AJ
3025 } else {
3026 tcg_gen_qemu_st16(arg1, arg2, ctx->mem_idx);
3027 }
b61f2753 3028}
0c8aacd4 3029GEN_STX(sthbr, st16r, 0x16, 0x1C, PPC_INTEGER);
b61f2753 3030
79aceca5 3031/* stwbrx */
86178a57 3032static inline void gen_qemu_st32r(DisasContext *ctx, TCGv arg1, TCGv arg2)
b61f2753 3033{
76db3ba4 3034 if (likely(!ctx->le_mode)) {
fa3966a3
AJ
3035 TCGv t0 = tcg_temp_new();
3036 tcg_gen_ext32u_tl(t0, arg1);
3037 tcg_gen_bswap32_tl(t0, t0);
76db3ba4
AJ
3038 tcg_gen_qemu_st32(t0, arg2, ctx->mem_idx);
3039 tcg_temp_free(t0);
76db3ba4
AJ
3040 } else {
3041 tcg_gen_qemu_st32(arg1, arg2, ctx->mem_idx);
3042 }
b61f2753 3043}
0c8aacd4 3044GEN_STX(stwbr, st32r, 0x16, 0x14, PPC_INTEGER);
79aceca5 3045
cd6e9320
TH
3046#if defined(TARGET_PPC64)
3047/* stdbrx */
3048static inline void gen_qemu_st64r(DisasContext *ctx, TCGv arg1, TCGv arg2)
3049{
3050 if (likely(!ctx->le_mode)) {
3051 TCGv t0 = tcg_temp_new();
3052 tcg_gen_bswap64_tl(t0, arg1);
3053 tcg_gen_qemu_st64(t0, arg2, ctx->mem_idx);
3054 tcg_temp_free(t0);
3055 } else {
3056 tcg_gen_qemu_st64(arg1, arg2, ctx->mem_idx);
3057 }
3058}
3059GEN_STX_E(stdbr, st64r, 0x14, 0x14, PPC_NONE, PPC2_DBRX);
3060#endif /* TARGET_PPC64 */
3061
79aceca5 3062/*** Integer load and store multiple ***/
99e300ef 3063
54623277 3064/* lmw */
99e300ef 3065static void gen_lmw(DisasContext *ctx)
79aceca5 3066{
76db3ba4
AJ
3067 TCGv t0;
3068 TCGv_i32 t1;
3069 gen_set_access_type(ctx, ACCESS_INT);
76a66253 3070 /* NIP cannot be restored if the memory exception comes from an helper */
d9bce9d9 3071 gen_update_nip(ctx, ctx->nip - 4);
76db3ba4
AJ
3072 t0 = tcg_temp_new();
3073 t1 = tcg_const_i32(rD(ctx->opcode));
3074 gen_addr_imm_index(ctx, t0, 0);
2f5a189c 3075 gen_helper_lmw(cpu_env, t0, t1);
ff4a62cd
AJ
3076 tcg_temp_free(t0);
3077 tcg_temp_free_i32(t1);
79aceca5
FB
3078}
3079
3080/* stmw */
99e300ef 3081static void gen_stmw(DisasContext *ctx)
79aceca5 3082{
76db3ba4
AJ
3083 TCGv t0;
3084 TCGv_i32 t1;
3085 gen_set_access_type(ctx, ACCESS_INT);
76a66253 3086 /* NIP cannot be restored if the memory exception comes from an helper */
d9bce9d9 3087 gen_update_nip(ctx, ctx->nip - 4);
76db3ba4
AJ
3088 t0 = tcg_temp_new();
3089 t1 = tcg_const_i32(rS(ctx->opcode));
3090 gen_addr_imm_index(ctx, t0, 0);
2f5a189c 3091 gen_helper_stmw(cpu_env, t0, t1);
ff4a62cd
AJ
3092 tcg_temp_free(t0);
3093 tcg_temp_free_i32(t1);
79aceca5
FB
3094}
3095
3096/*** Integer load and store strings ***/
54623277 3097
79aceca5 3098/* lswi */
3fc6c082 3099/* PowerPC32 specification says we must generate an exception if
9a64fbe4
FB
3100 * rA is in the range of registers to be loaded.
3101 * In an other hand, IBM says this is valid, but rA won't be loaded.
3102 * For now, I'll follow the spec...
3103 */
99e300ef 3104static void gen_lswi(DisasContext *ctx)
79aceca5 3105{
dfbc799d
AJ
3106 TCGv t0;
3107 TCGv_i32 t1, t2;
79aceca5
FB
3108 int nb = NB(ctx->opcode);
3109 int start = rD(ctx->opcode);
9a64fbe4 3110 int ra = rA(ctx->opcode);
79aceca5
FB
3111 int nr;
3112
3113 if (nb == 0)
3114 nb = 32;
3115 nr = nb / 4;
76a66253
JM
3116 if (unlikely(((start + nr) > 32 &&
3117 start <= ra && (start + nr - 32) > ra) ||
3118 ((start + nr) <= 32 && start <= ra && (start + nr) > ra))) {
e06fcd75 3119 gen_inval_exception(ctx, POWERPC_EXCP_INVAL_LSWX);
9fddaa0c 3120 return;
297d8e62 3121 }
76db3ba4 3122 gen_set_access_type(ctx, ACCESS_INT);
8dd4983c 3123 /* NIP cannot be restored if the memory exception comes from an helper */
d9bce9d9 3124 gen_update_nip(ctx, ctx->nip - 4);
dfbc799d 3125 t0 = tcg_temp_new();
76db3ba4 3126 gen_addr_register(ctx, t0);
dfbc799d
AJ
3127 t1 = tcg_const_i32(nb);
3128 t2 = tcg_const_i32(start);
2f5a189c 3129 gen_helper_lsw(cpu_env, t0, t1, t2);
dfbc799d
AJ
3130 tcg_temp_free(t0);
3131 tcg_temp_free_i32(t1);
3132 tcg_temp_free_i32(t2);
79aceca5
FB
3133}
3134
3135/* lswx */
99e300ef 3136static void gen_lswx(DisasContext *ctx)
79aceca5 3137{
76db3ba4
AJ
3138 TCGv t0;
3139 TCGv_i32 t1, t2, t3;
3140 gen_set_access_type(ctx, ACCESS_INT);
76a66253 3141 /* NIP cannot be restored if the memory exception comes from an helper */
d9bce9d9 3142 gen_update_nip(ctx, ctx->nip - 4);
76db3ba4
AJ
3143 t0 = tcg_temp_new();
3144 gen_addr_reg_index(ctx, t0);
3145 t1 = tcg_const_i32(rD(ctx->opcode));
3146 t2 = tcg_const_i32(rA(ctx->opcode));
3147 t3 = tcg_const_i32(rB(ctx->opcode));
2f5a189c 3148 gen_helper_lswx(cpu_env, t0, t1, t2, t3);
dfbc799d
AJ
3149 tcg_temp_free(t0);
3150 tcg_temp_free_i32(t1);
3151 tcg_temp_free_i32(t2);
3152 tcg_temp_free_i32(t3);
79aceca5
FB
3153}
3154
3155/* stswi */
99e300ef 3156static void gen_stswi(DisasContext *ctx)
79aceca5 3157{
76db3ba4
AJ
3158 TCGv t0;
3159 TCGv_i32 t1, t2;
4b3686fa 3160 int nb = NB(ctx->opcode);
76db3ba4 3161 gen_set_access_type(ctx, ACCESS_INT);
76a66253 3162 /* NIP cannot be restored if the memory exception comes from an helper */
d9bce9d9 3163 gen_update_nip(ctx, ctx->nip - 4);
76db3ba4
AJ
3164 t0 = tcg_temp_new();
3165 gen_addr_register(ctx, t0);
4b3686fa
FB
3166 if (nb == 0)
3167 nb = 32;
dfbc799d 3168 t1 = tcg_const_i32(nb);
76db3ba4 3169 t2 = tcg_const_i32(rS(ctx->opcode));
2f5a189c 3170 gen_helper_stsw(cpu_env, t0, t1, t2);
dfbc799d
AJ
3171 tcg_temp_free(t0);
3172 tcg_temp_free_i32(t1);
3173 tcg_temp_free_i32(t2);
79aceca5
FB
3174}
3175
3176/* stswx */
99e300ef 3177static void gen_stswx(DisasContext *ctx)
79aceca5 3178{
76db3ba4
AJ
3179 TCGv t0;
3180 TCGv_i32 t1, t2;
3181 gen_set_access_type(ctx, ACCESS_INT);
8dd4983c 3182 /* NIP cannot be restored if the memory exception comes from an helper */
5fafdf24 3183 gen_update_nip(ctx, ctx->nip - 4);
76db3ba4
AJ
3184 t0 = tcg_temp_new();
3185 gen_addr_reg_index(ctx, t0);
3186 t1 = tcg_temp_new_i32();
dfbc799d
AJ
3187 tcg_gen_trunc_tl_i32(t1, cpu_xer);
3188 tcg_gen_andi_i32(t1, t1, 0x7F);
76db3ba4 3189 t2 = tcg_const_i32(rS(ctx->opcode));
2f5a189c 3190 gen_helper_stsw(cpu_env, t0, t1, t2);
dfbc799d
AJ
3191 tcg_temp_free(t0);
3192 tcg_temp_free_i32(t1);
3193 tcg_temp_free_i32(t2);
79aceca5
FB
3194}
3195
3196/*** Memory synchronisation ***/
3197/* eieio */
99e300ef 3198static void gen_eieio(DisasContext *ctx)
79aceca5 3199{
79aceca5
FB
3200}
3201
3202/* isync */
99e300ef 3203static void gen_isync(DisasContext *ctx)
79aceca5 3204{
e06fcd75 3205 gen_stop_exception(ctx);
79aceca5
FB
3206}
3207
111bfab3 3208/* lwarx */
99e300ef 3209static void gen_lwarx(DisasContext *ctx)
79aceca5 3210{
76db3ba4 3211 TCGv t0;
18b21a2f 3212 TCGv gpr = cpu_gpr[rD(ctx->opcode)];
76db3ba4
AJ
3213 gen_set_access_type(ctx, ACCESS_RES);
3214 t0 = tcg_temp_local_new();
3215 gen_addr_reg_index(ctx, t0);
cf360a32 3216 gen_check_align(ctx, t0, 0x03);
18b21a2f 3217 gen_qemu_ld32u(ctx, gpr, t0);
cf360a32 3218 tcg_gen_mov_tl(cpu_reserve, t0);
1328c2bf 3219 tcg_gen_st_tl(gpr, cpu_env, offsetof(CPUPPCState, reserve_val));
cf360a32 3220 tcg_temp_free(t0);
79aceca5
FB
3221}
3222
4425265b
NF
3223#if defined(CONFIG_USER_ONLY)
3224static void gen_conditional_store (DisasContext *ctx, TCGv EA,
3225 int reg, int size)
3226{
3227 TCGv t0 = tcg_temp_new();
3228 uint32_t save_exception = ctx->exception;
3229
1328c2bf 3230 tcg_gen_st_tl(EA, cpu_env, offsetof(CPUPPCState, reserve_ea));
4425265b 3231 tcg_gen_movi_tl(t0, (size << 5) | reg);
1328c2bf 3232 tcg_gen_st_tl(t0, cpu_env, offsetof(CPUPPCState, reserve_info));
4425265b
NF
3233 tcg_temp_free(t0);
3234 gen_update_nip(ctx, ctx->nip-4);
3235 ctx->exception = POWERPC_EXCP_BRANCH;
3236 gen_exception(ctx, POWERPC_EXCP_STCX);
3237 ctx->exception = save_exception;
3238}
3239#endif
3240
79aceca5 3241/* stwcx. */
e8eaa2c0 3242static void gen_stwcx_(DisasContext *ctx)
79aceca5 3243{
76db3ba4
AJ
3244 TCGv t0;
3245 gen_set_access_type(ctx, ACCESS_RES);
3246 t0 = tcg_temp_local_new();
3247 gen_addr_reg_index(ctx, t0);
cf360a32 3248 gen_check_align(ctx, t0, 0x03);
4425265b
NF
3249#if defined(CONFIG_USER_ONLY)
3250 gen_conditional_store(ctx, t0, rS(ctx->opcode), 4);
3251#else
3252 {
3253 int l1;
3254
da91a00f 3255 tcg_gen_trunc_tl_i32(cpu_crf[0], cpu_so);
4425265b
NF
3256 l1 = gen_new_label();
3257 tcg_gen_brcond_tl(TCG_COND_NE, t0, cpu_reserve, l1);
3258 tcg_gen_ori_i32(cpu_crf[0], cpu_crf[0], 1 << CRF_EQ);
3259 gen_qemu_st32(ctx, cpu_gpr[rS(ctx->opcode)], t0);
3260 gen_set_label(l1);
3261 tcg_gen_movi_tl(cpu_reserve, -1);
3262 }
3263#endif
cf360a32 3264 tcg_temp_free(t0);
79aceca5
FB
3265}
3266
426613db 3267#if defined(TARGET_PPC64)
426613db 3268/* ldarx */
99e300ef 3269static void gen_ldarx(DisasContext *ctx)
426613db 3270{
76db3ba4 3271 TCGv t0;
18b21a2f 3272 TCGv gpr = cpu_gpr[rD(ctx->opcode)];
76db3ba4
AJ
3273 gen_set_access_type(ctx, ACCESS_RES);
3274 t0 = tcg_temp_local_new();
3275 gen_addr_reg_index(ctx, t0);
cf360a32 3276 gen_check_align(ctx, t0, 0x07);
18b21a2f 3277 gen_qemu_ld64(ctx, gpr, t0);
cf360a32 3278 tcg_gen_mov_tl(cpu_reserve, t0);
1328c2bf 3279 tcg_gen_st_tl(gpr, cpu_env, offsetof(CPUPPCState, reserve_val));
cf360a32 3280 tcg_temp_free(t0);
426613db
JM
3281}
3282
3283/* stdcx. */
e8eaa2c0 3284static void gen_stdcx_(DisasContext *ctx)
426613db 3285{
76db3ba4
AJ
3286 TCGv t0;
3287 gen_set_access_type(ctx, ACCESS_RES);
3288 t0 = tcg_temp_local_new();
3289 gen_addr_reg_index(ctx, t0);
cf360a32 3290 gen_check_align(ctx, t0, 0x07);
4425265b
NF
3291#if defined(CONFIG_USER_ONLY)
3292 gen_conditional_store(ctx, t0, rS(ctx->opcode), 8);
3293#else
3294 {
3295 int l1;
da91a00f 3296 tcg_gen_trunc_tl_i32(cpu_crf[0], cpu_so);
4425265b
NF
3297 l1 = gen_new_label();
3298 tcg_gen_brcond_tl(TCG_COND_NE, t0, cpu_reserve, l1);
3299 tcg_gen_ori_i32(cpu_crf[0], cpu_crf[0], 1 << CRF_EQ);
3300 gen_qemu_st64(ctx, cpu_gpr[rS(ctx->opcode)], t0);
3301 gen_set_label(l1);
3302 tcg_gen_movi_tl(cpu_reserve, -1);
3303 }
3304#endif
cf360a32 3305 tcg_temp_free(t0);
426613db
JM
3306}
3307#endif /* defined(TARGET_PPC64) */
3308
79aceca5 3309/* sync */
99e300ef 3310static void gen_sync(DisasContext *ctx)
79aceca5 3311{
79aceca5
FB
3312}
3313
0db1b20e 3314/* wait */
99e300ef 3315static void gen_wait(DisasContext *ctx)
0db1b20e 3316{
931ff272 3317 TCGv_i32 t0 = tcg_temp_new_i32();
259186a7
AF
3318 tcg_gen_st_i32(t0, cpu_env,
3319 -offsetof(PowerPCCPU, env) + offsetof(CPUState, halted));
931ff272 3320 tcg_temp_free_i32(t0);
0db1b20e 3321 /* Stop translation, as the CPU is supposed to sleep from now */
e06fcd75 3322 gen_exception_err(ctx, EXCP_HLT, 1);
0db1b20e
JM
3323}
3324
79aceca5 3325/*** Floating-point load ***/
a0d7d5a7 3326#define GEN_LDF(name, ldop, opc, type) \
99e300ef 3327static void glue(gen_, name)(DisasContext *ctx) \
79aceca5 3328{ \
a0d7d5a7 3329 TCGv EA; \
76a66253 3330 if (unlikely(!ctx->fpu_enabled)) { \
e06fcd75 3331 gen_exception(ctx, POWERPC_EXCP_FPU); \
4ecc3190
FB
3332 return; \
3333 } \
76db3ba4 3334 gen_set_access_type(ctx, ACCESS_FLOAT); \
a0d7d5a7 3335 EA = tcg_temp_new(); \
76db3ba4
AJ
3336 gen_addr_imm_index(ctx, EA, 0); \
3337 gen_qemu_##ldop(ctx, cpu_fpr[rD(ctx->opcode)], EA); \
a0d7d5a7 3338 tcg_temp_free(EA); \
79aceca5
FB
3339}
3340
a0d7d5a7 3341#define GEN_LDUF(name, ldop, opc, type) \
99e300ef 3342static void glue(gen_, name##u)(DisasContext *ctx) \
79aceca5 3343{ \
a0d7d5a7 3344 TCGv EA; \
76a66253 3345 if (unlikely(!ctx->fpu_enabled)) { \
e06fcd75 3346 gen_exception(ctx, POWERPC_EXCP_FPU); \
4ecc3190
FB
3347 return; \
3348 } \
76a66253 3349 if (unlikely(rA(ctx->opcode) == 0)) { \
e06fcd75 3350 gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL); \
9fddaa0c 3351 return; \
9a64fbe4 3352 } \
76db3ba4 3353 gen_set_access_type(ctx, ACCESS_FLOAT); \
a0d7d5a7 3354 EA = tcg_temp_new(); \
76db3ba4
AJ
3355 gen_addr_imm_index(ctx, EA, 0); \
3356 gen_qemu_##ldop(ctx, cpu_fpr[rD(ctx->opcode)], EA); \
a0d7d5a7
AJ
3357 tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], EA); \
3358 tcg_temp_free(EA); \
79aceca5
FB
3359}
3360
a0d7d5a7 3361#define GEN_LDUXF(name, ldop, opc, type) \
99e300ef 3362static void glue(gen_, name##ux)(DisasContext *ctx) \
79aceca5 3363{ \
a0d7d5a7 3364 TCGv EA; \
76a66253 3365 if (unlikely(!ctx->fpu_enabled)) { \
e06fcd75 3366 gen_exception(ctx, POWERPC_EXCP_FPU); \
4ecc3190
FB
3367 return; \
3368 } \
76a66253 3369 if (unlikely(rA(ctx->opcode) == 0)) { \
e06fcd75 3370 gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL); \
9fddaa0c 3371 return; \
9a64fbe4 3372 } \
76db3ba4 3373 gen_set_access_type(ctx, ACCESS_FLOAT); \
a0d7d5a7 3374 EA = tcg_temp_new(); \
76db3ba4
AJ
3375 gen_addr_reg_index(ctx, EA); \
3376 gen_qemu_##ldop(ctx, cpu_fpr[rD(ctx->opcode)], EA); \
a0d7d5a7
AJ
3377 tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], EA); \
3378 tcg_temp_free(EA); \
79aceca5
FB
3379}
3380
a0d7d5a7 3381#define GEN_LDXF(name, ldop, opc2, opc3, type) \
99e300ef 3382static void glue(gen_, name##x)(DisasContext *ctx) \
79aceca5 3383{ \
a0d7d5a7 3384 TCGv EA; \
76a66253 3385 if (unlikely(!ctx->fpu_enabled)) { \
e06fcd75 3386 gen_exception(ctx, POWERPC_EXCP_FPU); \
4ecc3190
FB
3387 return; \
3388 } \
76db3ba4 3389 gen_set_access_type(ctx, ACCESS_FLOAT); \
a0d7d5a7 3390 EA = tcg_temp_new(); \
76db3ba4
AJ
3391 gen_addr_reg_index(ctx, EA); \
3392 gen_qemu_##ldop(ctx, cpu_fpr[rD(ctx->opcode)], EA); \
a0d7d5a7 3393 tcg_temp_free(EA); \
79aceca5
FB
3394}
3395
a0d7d5a7
AJ
3396#define GEN_LDFS(name, ldop, op, type) \
3397GEN_LDF(name, ldop, op | 0x20, type); \
3398GEN_LDUF(name, ldop, op | 0x21, type); \
3399GEN_LDUXF(name, ldop, op | 0x01, type); \
3400GEN_LDXF(name, ldop, 0x17, op | 0x00, type)
3401
636aa200 3402static inline void gen_qemu_ld32fs(DisasContext *ctx, TCGv_i64 arg1, TCGv arg2)
a0d7d5a7
AJ
3403{
3404 TCGv t0 = tcg_temp_new();
3405 TCGv_i32 t1 = tcg_temp_new_i32();
76db3ba4 3406 gen_qemu_ld32u(ctx, t0, arg2);
a0d7d5a7
AJ
3407 tcg_gen_trunc_tl_i32(t1, t0);
3408 tcg_temp_free(t0);
8e703949 3409 gen_helper_float32_to_float64(arg1, cpu_env, t1);
a0d7d5a7
AJ
3410 tcg_temp_free_i32(t1);
3411}
79aceca5 3412
a0d7d5a7
AJ
3413 /* lfd lfdu lfdux lfdx */
3414GEN_LDFS(lfd, ld64, 0x12, PPC_FLOAT);
3415 /* lfs lfsu lfsux lfsx */
3416GEN_LDFS(lfs, ld32fs, 0x10, PPC_FLOAT);
79aceca5 3417
05050ee8
AJ
3418/* lfdp */
3419static void gen_lfdp(DisasContext *ctx)
3420{
3421 TCGv EA;
3422 if (unlikely(!ctx->fpu_enabled)) {
3423 gen_exception(ctx, POWERPC_EXCP_FPU);
3424 return;
3425 }
3426 gen_set_access_type(ctx, ACCESS_FLOAT);
3427 EA = tcg_temp_new();
3428 gen_addr_imm_index(ctx, EA, 0); \
3429 if (unlikely(ctx->le_mode)) {
3430 gen_qemu_ld64(ctx, cpu_fpr[rD(ctx->opcode) + 1], EA);
3431 tcg_gen_addi_tl(EA, EA, 8);
3432 gen_qemu_ld64(ctx, cpu_fpr[rD(ctx->opcode)], EA);
3433 } else {
3434 gen_qemu_ld64(ctx, cpu_fpr[rD(ctx->opcode)], EA);
3435 tcg_gen_addi_tl(EA, EA, 8);
3436 gen_qemu_ld64(ctx, cpu_fpr[rD(ctx->opcode) + 1], EA);
3437 }
3438 tcg_temp_free(EA);
3439}
3440
3441/* lfdpx */
3442static void gen_lfdpx(DisasContext *ctx)
3443{
3444 TCGv EA;
3445 if (unlikely(!ctx->fpu_enabled)) {
3446 gen_exception(ctx, POWERPC_EXCP_FPU);
3447 return;
3448 }
3449 gen_set_access_type(ctx, ACCESS_FLOAT);
3450 EA = tcg_temp_new();
3451 gen_addr_reg_index(ctx, EA);
3452 if (unlikely(ctx->le_mode)) {
3453 gen_qemu_ld64(ctx, cpu_fpr[rD(ctx->opcode) + 1], EA);
3454 tcg_gen_addi_tl(EA, EA, 8);
3455 gen_qemu_ld64(ctx, cpu_fpr[rD(ctx->opcode)], EA);
3456 } else {
3457 gen_qemu_ld64(ctx, cpu_fpr[rD(ctx->opcode)], EA);
3458 tcg_gen_addi_tl(EA, EA, 8);
3459 gen_qemu_ld64(ctx, cpu_fpr[rD(ctx->opcode) + 1], EA);
3460 }
3461 tcg_temp_free(EA);
3462}
3463
199f830d
AJ
3464/* lfiwax */
3465static void gen_lfiwax(DisasContext *ctx)
3466{
3467 TCGv EA;
3468 TCGv t0;
3469 if (unlikely(!ctx->fpu_enabled)) {
3470 gen_exception(ctx, POWERPC_EXCP_FPU);
3471 return;
3472 }
3473 gen_set_access_type(ctx, ACCESS_FLOAT);
3474 EA = tcg_temp_new();
3475 t0 = tcg_temp_new();
3476 gen_addr_reg_index(ctx, EA);
909eedb7 3477 gen_qemu_ld32s(ctx, t0, EA);
199f830d 3478 tcg_gen_ext_tl_i64(cpu_fpr[rD(ctx->opcode)], t0);
199f830d
AJ
3479 tcg_temp_free(EA);
3480 tcg_temp_free(t0);
3481}
3482
79aceca5 3483/*** Floating-point store ***/
a0d7d5a7 3484#define GEN_STF(name, stop, opc, type) \
99e300ef 3485static void glue(gen_, name)(DisasContext *ctx) \
79aceca5 3486{ \
a0d7d5a7 3487 TCGv EA; \
76a66253 3488 if (unlikely(!ctx->fpu_enabled)) { \
e06fcd75 3489 gen_exception(ctx, POWERPC_EXCP_FPU); \
4ecc3190
FB
3490 return; \
3491 } \
76db3ba4 3492 gen_set_access_type(ctx, ACCESS_FLOAT); \
a0d7d5a7 3493 EA = tcg_temp_new(); \
76db3ba4
AJ
3494 gen_addr_imm_index(ctx, EA, 0); \
3495 gen_qemu_##stop(ctx, cpu_fpr[rS(ctx->opcode)], EA); \
a0d7d5a7 3496 tcg_temp_free(EA); \
79aceca5
FB
3497}
3498
a0d7d5a7 3499#define GEN_STUF(name, stop, opc, type) \
99e300ef 3500static void glue(gen_, name##u)(DisasContext *ctx) \
79aceca5 3501{ \
a0d7d5a7 3502 TCGv EA; \
76a66253 3503 if (unlikely(!ctx->fpu_enabled)) { \
e06fcd75 3504 gen_exception(ctx, POWERPC_EXCP_FPU); \
4ecc3190
FB
3505 return; \
3506 } \
76a66253 3507 if (unlikely(rA(ctx->opcode) == 0)) { \
e06fcd75 3508 gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL); \
9fddaa0c 3509 return; \
9a64fbe4 3510 } \
76db3ba4 3511 gen_set_access_type(ctx, ACCESS_FLOAT); \
a0d7d5a7 3512 EA = tcg_temp_new(); \
76db3ba4
AJ
3513 gen_addr_imm_index(ctx, EA, 0); \
3514 gen_qemu_##stop(ctx, cpu_fpr[rS(ctx->opcode)], EA); \
a0d7d5a7
AJ
3515 tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], EA); \
3516 tcg_temp_free(EA); \
79aceca5
FB
3517}
3518
a0d7d5a7 3519#define GEN_STUXF(name, stop, opc, type) \
99e300ef 3520static void glue(gen_, name##ux)(DisasContext *ctx) \
79aceca5 3521{ \
a0d7d5a7 3522 TCGv EA; \
76a66253 3523 if (unlikely(!ctx->fpu_enabled)) { \
e06fcd75 3524 gen_exception(ctx, POWERPC_EXCP_FPU); \
4ecc3190
FB
3525 return; \
3526 } \
76a66253 3527 if (unlikely(rA(ctx->opcode) == 0)) { \
e06fcd75 3528 gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL); \
9fddaa0c 3529 return; \
9a64fbe4 3530 } \
76db3ba4 3531 gen_set_access_type(ctx, ACCESS_FLOAT); \
a0d7d5a7 3532 EA = tcg_temp_new(); \
76db3ba4
AJ
3533 gen_addr_reg_index(ctx, EA); \
3534 gen_qemu_##stop(ctx, cpu_fpr[rS(ctx->opcode)], EA); \
a0d7d5a7
AJ
3535 tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], EA); \
3536 tcg_temp_free(EA); \
79aceca5
FB
3537}
3538
a0d7d5a7 3539#define GEN_STXF(name, stop, opc2, opc3, type) \
99e300ef 3540static void glue(gen_, name##x)(DisasContext *ctx) \
79aceca5 3541{ \
a0d7d5a7 3542 TCGv EA; \
76a66253 3543 if (unlikely(!ctx->fpu_enabled)) { \
e06fcd75 3544 gen_exception(ctx, POWERPC_EXCP_FPU); \
4ecc3190
FB
3545 return; \
3546 } \
76db3ba4 3547 gen_set_access_type(ctx, ACCESS_FLOAT); \
a0d7d5a7 3548 EA = tcg_temp_new(); \
76db3ba4
AJ
3549 gen_addr_reg_index(ctx, EA); \
3550 gen_qemu_##stop(ctx, cpu_fpr[rS(ctx->opcode)], EA); \
a0d7d5a7 3551 tcg_temp_free(EA); \
79aceca5
FB
3552}
3553
a0d7d5a7
AJ
3554#define GEN_STFS(name, stop, op, type) \
3555GEN_STF(name, stop, op | 0x20, type); \
3556GEN_STUF(name, stop, op | 0x21, type); \
3557GEN_STUXF(name, stop, op | 0x01, type); \
3558GEN_STXF(name, stop, 0x17, op | 0x00, type)
3559
636aa200 3560static inline void gen_qemu_st32fs(DisasContext *ctx, TCGv_i64 arg1, TCGv arg2)
a0d7d5a7
AJ
3561{
3562 TCGv_i32 t0 = tcg_temp_new_i32();
3563 TCGv t1 = tcg_temp_new();
8e703949 3564 gen_helper_float64_to_float32(t0, cpu_env, arg1);
a0d7d5a7
AJ
3565 tcg_gen_extu_i32_tl(t1, t0);
3566 tcg_temp_free_i32(t0);
76db3ba4 3567 gen_qemu_st32(ctx, t1, arg2);
a0d7d5a7
AJ
3568 tcg_temp_free(t1);
3569}
79aceca5
FB
3570
3571/* stfd stfdu stfdux stfdx */
a0d7d5a7 3572GEN_STFS(stfd, st64, 0x16, PPC_FLOAT);
79aceca5 3573/* stfs stfsu stfsux stfsx */
a0d7d5a7 3574GEN_STFS(stfs, st32fs, 0x14, PPC_FLOAT);
79aceca5 3575
44bc0c4d
AJ
3576/* stfdp */
3577static void gen_stfdp(DisasContext *ctx)
3578{
3579 TCGv EA;
3580 if (unlikely(!ctx->fpu_enabled)) {
3581 gen_exception(ctx, POWERPC_EXCP_FPU);
3582 return;
3583 }
3584 gen_set_access_type(ctx, ACCESS_FLOAT);
3585 EA = tcg_temp_new();
3586 gen_addr_imm_index(ctx, EA, 0); \
3587 if (unlikely(ctx->le_mode)) {
3588 gen_qemu_st64(ctx, cpu_fpr[rD(ctx->opcode) + 1], EA);
3589 tcg_gen_addi_tl(EA, EA, 8);
3590 gen_qemu_st64(ctx, cpu_fpr[rD(ctx->opcode)], EA);
3591 } else {
3592 gen_qemu_st64(ctx, cpu_fpr[rD(ctx->opcode)], EA);
3593 tcg_gen_addi_tl(EA, EA, 8);
3594 gen_qemu_st64(ctx, cpu_fpr[rD(ctx->opcode) + 1], EA);
3595 }
3596 tcg_temp_free(EA);
3597}
3598
3599/* stfdpx */
3600static void gen_stfdpx(DisasContext *ctx)
3601{
3602 TCGv EA;
3603 if (unlikely(!ctx->fpu_enabled)) {
3604 gen_exception(ctx, POWERPC_EXCP_FPU);
3605 return;
3606 }
3607 gen_set_access_type(ctx, ACCESS_FLOAT);
3608 EA = tcg_temp_new();
3609 gen_addr_reg_index(ctx, EA);
3610 if (unlikely(ctx->le_mode)) {
3611 gen_qemu_st64(ctx, cpu_fpr[rD(ctx->opcode) + 1], EA);
3612 tcg_gen_addi_tl(EA, EA, 8);
3613 gen_qemu_st64(ctx, cpu_fpr[rD(ctx->opcode)], EA);
3614 } else {
3615 gen_qemu_st64(ctx, cpu_fpr[rD(ctx->opcode)], EA);
3616 tcg_gen_addi_tl(EA, EA, 8);
3617 gen_qemu_st64(ctx, cpu_fpr[rD(ctx->opcode) + 1], EA);
3618 }
3619 tcg_temp_free(EA);
3620}
3621
79aceca5 3622/* Optional: */
636aa200 3623static inline void gen_qemu_st32fiw(DisasContext *ctx, TCGv_i64 arg1, TCGv arg2)
a0d7d5a7
AJ
3624{
3625 TCGv t0 = tcg_temp_new();
3626 tcg_gen_trunc_i64_tl(t0, arg1),
76db3ba4 3627 gen_qemu_st32(ctx, t0, arg2);
a0d7d5a7
AJ
3628 tcg_temp_free(t0);
3629}
79aceca5 3630/* stfiwx */
a0d7d5a7 3631GEN_STXF(stfiw, st32fiw, 0x17, 0x1E, PPC_FLOAT_STFIWX);
79aceca5 3632
697ab892
DG
3633static inline void gen_update_cfar(DisasContext *ctx, target_ulong nip)
3634{
3635#if defined(TARGET_PPC64)
3636 if (ctx->has_cfar)
3637 tcg_gen_movi_tl(cpu_cfar, nip);
3638#endif
3639}
3640
79aceca5 3641/*** Branch ***/
636aa200 3642static inline void gen_goto_tb(DisasContext *ctx, int n, target_ulong dest)
c1942362
FB
3643{
3644 TranslationBlock *tb;
3645 tb = ctx->tb;
e0c8f9ce 3646 if (NARROW_MODE(ctx)) {
a2ffb812 3647 dest = (uint32_t) dest;
e0c8f9ce 3648 }
57fec1fe 3649 if ((tb->pc & TARGET_PAGE_MASK) == (dest & TARGET_PAGE_MASK) &&
8cbcb4fa 3650 likely(!ctx->singlestep_enabled)) {
57fec1fe 3651 tcg_gen_goto_tb(n);
a2ffb812 3652 tcg_gen_movi_tl(cpu_nip, dest & ~3);
8cfd0495 3653 tcg_gen_exit_tb((uintptr_t)tb + n);
c1942362 3654 } else {
a2ffb812 3655 tcg_gen_movi_tl(cpu_nip, dest & ~3);
8cbcb4fa
AJ
3656 if (unlikely(ctx->singlestep_enabled)) {
3657 if ((ctx->singlestep_enabled &
bdc4e053 3658 (CPU_BRANCH_STEP | CPU_SINGLE_STEP)) &&
f0cc4aa8
JG
3659 (ctx->exception == POWERPC_EXCP_BRANCH ||
3660 ctx->exception == POWERPC_EXCP_TRACE)) {
8cbcb4fa
AJ
3661 target_ulong tmp = ctx->nip;
3662 ctx->nip = dest;
e06fcd75 3663 gen_exception(ctx, POWERPC_EXCP_TRACE);
8cbcb4fa
AJ
3664 ctx->nip = tmp;
3665 }
3666 if (ctx->singlestep_enabled & GDBSTUB_SINGLE_STEP) {
e06fcd75 3667 gen_debug_exception(ctx);
8cbcb4fa
AJ
3668 }
3669 }
57fec1fe 3670 tcg_gen_exit_tb(0);
c1942362 3671 }
c53be334
FB
3672}
3673
636aa200 3674static inline void gen_setlr(DisasContext *ctx, target_ulong nip)
e1833e1f 3675{
e0c8f9ce
RH
3676 if (NARROW_MODE(ctx)) {
3677 nip = (uint32_t)nip;
3678 }
3679 tcg_gen_movi_tl(cpu_lr, nip);
e1833e1f
JM
3680}
3681
79aceca5 3682/* b ba bl bla */
99e300ef 3683static void gen_b(DisasContext *ctx)
79aceca5 3684{
76a66253 3685 target_ulong li, target;
38a64f9d 3686
8cbcb4fa 3687 ctx->exception = POWERPC_EXCP_BRANCH;
38a64f9d 3688 /* sign extend LI */
e0c8f9ce
RH
3689 li = LI(ctx->opcode);
3690 li = (li ^ 0x02000000) - 0x02000000;
3691 if (likely(AA(ctx->opcode) == 0)) {
046d6672 3692 target = ctx->nip + li - 4;
e0c8f9ce 3693 } else {
9a64fbe4 3694 target = li;
e0c8f9ce
RH
3695 }
3696 if (LK(ctx->opcode)) {
e1833e1f 3697 gen_setlr(ctx, ctx->nip);
e0c8f9ce 3698 }
697ab892 3699 gen_update_cfar(ctx, ctx->nip);
c1942362 3700 gen_goto_tb(ctx, 0, target);
79aceca5
FB
3701}
3702
e98a6e40
FB
3703#define BCOND_IM 0
3704#define BCOND_LR 1
3705#define BCOND_CTR 2
3706
636aa200 3707static inline void gen_bcond(DisasContext *ctx, int type)
d9bce9d9 3708{
d9bce9d9 3709 uint32_t bo = BO(ctx->opcode);
05f92404 3710 int l1;
a2ffb812 3711 TCGv target;
e98a6e40 3712
8cbcb4fa 3713 ctx->exception = POWERPC_EXCP_BRANCH;
a2ffb812 3714 if (type == BCOND_LR || type == BCOND_CTR) {
a7812ae4 3715 target = tcg_temp_local_new();
a2ffb812
AJ
3716 if (type == BCOND_CTR)
3717 tcg_gen_mov_tl(target, cpu_ctr);
3718 else
3719 tcg_gen_mov_tl(target, cpu_lr);
d2e9fd8f 3720 } else {
3721 TCGV_UNUSED(target);
e98a6e40 3722 }
e1833e1f
JM
3723 if (LK(ctx->opcode))
3724 gen_setlr(ctx, ctx->nip);
a2ffb812
AJ
3725 l1 = gen_new_label();
3726 if ((bo & 0x4) == 0) {
3727 /* Decrement and test CTR */
a7812ae4 3728 TCGv temp = tcg_temp_new();
a2ffb812 3729 if (unlikely(type == BCOND_CTR)) {
e06fcd75 3730 gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL);
a2ffb812
AJ
3731 return;
3732 }
3733 tcg_gen_subi_tl(cpu_ctr, cpu_ctr, 1);
e0c8f9ce 3734 if (NARROW_MODE(ctx)) {
a2ffb812 3735 tcg_gen_ext32u_tl(temp, cpu_ctr);
e0c8f9ce 3736 } else {
a2ffb812 3737 tcg_gen_mov_tl(temp, cpu_ctr);
e0c8f9ce 3738 }
a2ffb812
AJ
3739 if (bo & 0x2) {
3740 tcg_gen_brcondi_tl(TCG_COND_NE, temp, 0, l1);
3741 } else {
3742 tcg_gen_brcondi_tl(TCG_COND_EQ, temp, 0, l1);
e98a6e40 3743 }
a7812ae4 3744 tcg_temp_free(temp);
a2ffb812
AJ
3745 }
3746 if ((bo & 0x10) == 0) {
3747 /* Test CR */
3748 uint32_t bi = BI(ctx->opcode);
3749 uint32_t mask = 1 << (3 - (bi & 0x03));
a7812ae4 3750 TCGv_i32 temp = tcg_temp_new_i32();
a2ffb812 3751
d9bce9d9 3752 if (bo & 0x8) {
a2ffb812
AJ
3753 tcg_gen_andi_i32(temp, cpu_crf[bi >> 2], mask);
3754 tcg_gen_brcondi_i32(TCG_COND_EQ, temp, 0, l1);
d9bce9d9 3755 } else {
a2ffb812
AJ
3756 tcg_gen_andi_i32(temp, cpu_crf[bi >> 2], mask);
3757 tcg_gen_brcondi_i32(TCG_COND_NE, temp, 0, l1);
d9bce9d9 3758 }
a7812ae4 3759 tcg_temp_free_i32(temp);
d9bce9d9 3760 }
697ab892 3761 gen_update_cfar(ctx, ctx->nip);
e98a6e40 3762 if (type == BCOND_IM) {
a2ffb812
AJ
3763 target_ulong li = (target_long)((int16_t)(BD(ctx->opcode)));
3764 if (likely(AA(ctx->opcode) == 0)) {
3765 gen_goto_tb(ctx, 0, ctx->nip + li - 4);
3766 } else {
3767 gen_goto_tb(ctx, 0, li);
3768 }
c53be334 3769 gen_set_label(l1);
c1942362 3770 gen_goto_tb(ctx, 1, ctx->nip);
e98a6e40 3771 } else {
e0c8f9ce 3772 if (NARROW_MODE(ctx)) {
a2ffb812 3773 tcg_gen_andi_tl(cpu_nip, target, (uint32_t)~3);
e0c8f9ce 3774 } else {
a2ffb812 3775 tcg_gen_andi_tl(cpu_nip, target, ~3);
e0c8f9ce 3776 }
a2ffb812
AJ
3777 tcg_gen_exit_tb(0);
3778 gen_set_label(l1);
e0c8f9ce 3779 gen_update_nip(ctx, ctx->nip);
57fec1fe 3780 tcg_gen_exit_tb(0);
08e46e54 3781 }
e98a6e40
FB
3782}
3783
99e300ef 3784static void gen_bc(DisasContext *ctx)
3b46e624 3785{
e98a6e40
FB
3786 gen_bcond(ctx, BCOND_IM);
3787}
3788
99e300ef 3789static void gen_bcctr(DisasContext *ctx)
3b46e624 3790{
e98a6e40
FB
3791 gen_bcond(ctx, BCOND_CTR);
3792}
3793
99e300ef 3794static void gen_bclr(DisasContext *ctx)
3b46e624 3795{
e98a6e40
FB
3796 gen_bcond(ctx, BCOND_LR);
3797}
79aceca5
FB
3798
3799/*** Condition register logical ***/
e1571908 3800#define GEN_CRLOGIC(name, tcg_op, opc) \
99e300ef 3801static void glue(gen_, name)(DisasContext *ctx) \
79aceca5 3802{ \
fc0d441e
JM
3803 uint8_t bitmask; \
3804 int sh; \
a7812ae4 3805 TCGv_i32 t0, t1; \
fc0d441e 3806 sh = (crbD(ctx->opcode) & 0x03) - (crbA(ctx->opcode) & 0x03); \
a7812ae4 3807 t0 = tcg_temp_new_i32(); \
fc0d441e 3808 if (sh > 0) \
fea0c503 3809 tcg_gen_shri_i32(t0, cpu_crf[crbA(ctx->opcode) >> 2], sh); \
fc0d441e 3810 else if (sh < 0) \
fea0c503 3811 tcg_gen_shli_i32(t0, cpu_crf[crbA(ctx->opcode) >> 2], -sh); \
e1571908 3812 else \
fea0c503 3813 tcg_gen_mov_i32(t0, cpu_crf[crbA(ctx->opcode) >> 2]); \
a7812ae4 3814 t1 = tcg_temp_new_i32(); \
fc0d441e
JM
3815 sh = (crbD(ctx->opcode) & 0x03) - (crbB(ctx->opcode) & 0x03); \
3816 if (sh > 0) \
fea0c503 3817 tcg_gen_shri_i32(t1, cpu_crf[crbB(ctx->opcode) >> 2], sh); \
fc0d441e 3818 else if (sh < 0) \
fea0c503 3819 tcg_gen_shli_i32(t1, cpu_crf[crbB(ctx->opcode) >> 2], -sh); \
e1571908 3820 else \
fea0c503
AJ
3821 tcg_gen_mov_i32(t1, cpu_crf[crbB(ctx->opcode) >> 2]); \
3822 tcg_op(t0, t0, t1); \
fc0d441e 3823 bitmask = 1 << (3 - (crbD(ctx->opcode) & 0x03)); \
fea0c503
AJ
3824 tcg_gen_andi_i32(t0, t0, bitmask); \
3825 tcg_gen_andi_i32(t1, cpu_crf[crbD(ctx->opcode) >> 2], ~bitmask); \
3826 tcg_gen_or_i32(cpu_crf[crbD(ctx->opcode) >> 2], t0, t1); \
a7812ae4
PB
3827 tcg_temp_free_i32(t0); \
3828 tcg_temp_free_i32(t1); \
79aceca5
FB
3829}
3830
3831/* crand */
e1571908 3832GEN_CRLOGIC(crand, tcg_gen_and_i32, 0x08);
79aceca5 3833/* crandc */
e1571908 3834GEN_CRLOGIC(crandc, tcg_gen_andc_i32, 0x04);
79aceca5 3835/* creqv */
e1571908 3836GEN_CRLOGIC(creqv, tcg_gen_eqv_i32, 0x09);
79aceca5 3837/* crnand */
e1571908 3838GEN_CRLOGIC(crnand, tcg_gen_nand_i32, 0x07);
79aceca5 3839/* crnor */
e1571908 3840GEN_CRLOGIC(crnor, tcg_gen_nor_i32, 0x01);
79aceca5 3841/* cror */
e1571908 3842GEN_CRLOGIC(cror, tcg_gen_or_i32, 0x0E);
79aceca5 3843/* crorc */
e1571908 3844GEN_CRLOGIC(crorc, tcg_gen_orc_i32, 0x0D);
79aceca5 3845/* crxor */
e1571908 3846GEN_CRLOGIC(crxor, tcg_gen_xor_i32, 0x06);
99e300ef 3847
54623277 3848/* mcrf */
99e300ef 3849static void gen_mcrf(DisasContext *ctx)
79aceca5 3850{
47e4661c 3851 tcg_gen_mov_i32(cpu_crf[crfD(ctx->opcode)], cpu_crf[crfS(ctx->opcode)]);
79aceca5
FB
3852}
3853
3854/*** System linkage ***/
99e300ef 3855
54623277 3856/* rfi (mem_idx only) */
99e300ef 3857static void gen_rfi(DisasContext *ctx)
79aceca5 3858{
9a64fbe4 3859#if defined(CONFIG_USER_ONLY)
e06fcd75 3860 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
9a64fbe4
FB
3861#else
3862 /* Restore CPU state */
76db3ba4 3863 if (unlikely(!ctx->mem_idx)) {
e06fcd75 3864 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
9fddaa0c 3865 return;
9a64fbe4 3866 }
697ab892 3867 gen_update_cfar(ctx, ctx->nip);
e5f17ac6 3868 gen_helper_rfi(cpu_env);
e06fcd75 3869 gen_sync_exception(ctx);
9a64fbe4 3870#endif
79aceca5
FB
3871}
3872
426613db 3873#if defined(TARGET_PPC64)
99e300ef 3874static void gen_rfid(DisasContext *ctx)
426613db
JM
3875{
3876#if defined(CONFIG_USER_ONLY)
e06fcd75 3877 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
426613db
JM
3878#else
3879 /* Restore CPU state */
76db3ba4 3880 if (unlikely(!ctx->mem_idx)) {
e06fcd75 3881 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
426613db
JM
3882 return;
3883 }
697ab892 3884 gen_update_cfar(ctx, ctx->nip);
e5f17ac6 3885 gen_helper_rfid(cpu_env);
e06fcd75 3886 gen_sync_exception(ctx);
426613db
JM
3887#endif
3888}
426613db 3889
99e300ef 3890static void gen_hrfid(DisasContext *ctx)
be147d08
JM
3891{
3892#if defined(CONFIG_USER_ONLY)
e06fcd75 3893 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
be147d08
JM
3894#else
3895 /* Restore CPU state */
76db3ba4 3896 if (unlikely(ctx->mem_idx <= 1)) {
e06fcd75 3897 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
be147d08
JM
3898 return;
3899 }
e5f17ac6 3900 gen_helper_hrfid(cpu_env);
e06fcd75 3901 gen_sync_exception(ctx);
be147d08
JM
3902#endif
3903}
3904#endif
3905
79aceca5 3906/* sc */
417bf010
JM
3907#if defined(CONFIG_USER_ONLY)
3908#define POWERPC_SYSCALL POWERPC_EXCP_SYSCALL_USER
3909#else
3910#define POWERPC_SYSCALL POWERPC_EXCP_SYSCALL
3911#endif
99e300ef 3912static void gen_sc(DisasContext *ctx)
79aceca5 3913{
e1833e1f
JM
3914 uint32_t lev;
3915
3916 lev = (ctx->opcode >> 5) & 0x7F;
e06fcd75 3917 gen_exception_err(ctx, POWERPC_SYSCALL, lev);
79aceca5
FB
3918}
3919
3920/*** Trap ***/
99e300ef 3921
54623277 3922/* tw */
99e300ef 3923static void gen_tw(DisasContext *ctx)
79aceca5 3924{
cab3bee2 3925 TCGv_i32 t0 = tcg_const_i32(TO(ctx->opcode));
db9a231d
AJ
3926 /* Update the nip since this might generate a trap exception */
3927 gen_update_nip(ctx, ctx->nip);
e5f17ac6
BS
3928 gen_helper_tw(cpu_env, cpu_gpr[rA(ctx->opcode)], cpu_gpr[rB(ctx->opcode)],
3929 t0);
cab3bee2 3930 tcg_temp_free_i32(t0);
79aceca5
FB
3931}
3932
3933/* twi */
99e300ef 3934static void gen_twi(DisasContext *ctx)
79aceca5 3935{
cab3bee2
AJ
3936 TCGv t0 = tcg_const_tl(SIMM(ctx->opcode));
3937 TCGv_i32 t1 = tcg_const_i32(TO(ctx->opcode));
db9a231d
AJ
3938 /* Update the nip since this might generate a trap exception */
3939 gen_update_nip(ctx, ctx->nip);
e5f17ac6 3940 gen_helper_tw(cpu_env, cpu_gpr[rA(ctx->opcode)], t0, t1);
cab3bee2
AJ
3941 tcg_temp_free(t0);
3942 tcg_temp_free_i32(t1);
79aceca5
FB
3943}
3944
d9bce9d9
JM
3945#if defined(TARGET_PPC64)
3946/* td */
99e300ef 3947static void gen_td(DisasContext *ctx)
d9bce9d9 3948{
cab3bee2 3949 TCGv_i32 t0 = tcg_const_i32(TO(ctx->opcode));
db9a231d
AJ
3950 /* Update the nip since this might generate a trap exception */
3951 gen_update_nip(ctx, ctx->nip);
e5f17ac6
BS
3952 gen_helper_td(cpu_env, cpu_gpr[rA(ctx->opcode)], cpu_gpr[rB(ctx->opcode)],
3953 t0);
cab3bee2 3954 tcg_temp_free_i32(t0);
d9bce9d9
JM
3955}
3956
3957/* tdi */
99e300ef 3958static void gen_tdi(DisasContext *ctx)
d9bce9d9 3959{
cab3bee2
AJ
3960 TCGv t0 = tcg_const_tl(SIMM(ctx->opcode));
3961 TCGv_i32 t1 = tcg_const_i32(TO(ctx->opcode));
db9a231d
AJ
3962 /* Update the nip since this might generate a trap exception */
3963 gen_update_nip(ctx, ctx->nip);
e5f17ac6 3964 gen_helper_td(cpu_env, cpu_gpr[rA(ctx->opcode)], t0, t1);
cab3bee2
AJ
3965 tcg_temp_free(t0);
3966 tcg_temp_free_i32(t1);
d9bce9d9
JM
3967}
3968#endif
3969
79aceca5 3970/*** Processor control ***/
99e300ef 3971
da91a00f
RH
3972static void gen_read_xer(TCGv dst)
3973{
3974 TCGv t0 = tcg_temp_new();
3975 TCGv t1 = tcg_temp_new();
3976 TCGv t2 = tcg_temp_new();
3977 tcg_gen_mov_tl(dst, cpu_xer);
3978 tcg_gen_shli_tl(t0, cpu_so, XER_SO);
3979 tcg_gen_shli_tl(t1, cpu_ov, XER_OV);
3980 tcg_gen_shli_tl(t2, cpu_ca, XER_CA);
3981 tcg_gen_or_tl(t0, t0, t1);
3982 tcg_gen_or_tl(dst, dst, t2);
3983 tcg_gen_or_tl(dst, dst, t0);
3984 tcg_temp_free(t0);
3985 tcg_temp_free(t1);
3986 tcg_temp_free(t2);
3987}
3988
3989static void gen_write_xer(TCGv src)
3990{
3991 tcg_gen_andi_tl(cpu_xer, src,
3992 ~((1u << XER_SO) | (1u << XER_OV) | (1u << XER_CA)));
3993 tcg_gen_shri_tl(cpu_so, src, XER_SO);
3994 tcg_gen_shri_tl(cpu_ov, src, XER_OV);
3995 tcg_gen_shri_tl(cpu_ca, src, XER_CA);
3996 tcg_gen_andi_tl(cpu_so, cpu_so, 1);
3997 tcg_gen_andi_tl(cpu_ov, cpu_ov, 1);
3998 tcg_gen_andi_tl(cpu_ca, cpu_ca, 1);
3999}
4000
54623277 4001/* mcrxr */
99e300ef 4002static void gen_mcrxr(DisasContext *ctx)
79aceca5 4003{
da91a00f
RH
4004 TCGv_i32 t0 = tcg_temp_new_i32();
4005 TCGv_i32 t1 = tcg_temp_new_i32();
4006 TCGv_i32 dst = cpu_crf[crfD(ctx->opcode)];
4007
4008 tcg_gen_trunc_tl_i32(t0, cpu_so);
4009 tcg_gen_trunc_tl_i32(t1, cpu_ov);
4010 tcg_gen_trunc_tl_i32(dst, cpu_ca);
4011 tcg_gen_shri_i32(t0, t0, 2);
4012 tcg_gen_shri_i32(t1, t1, 1);
4013 tcg_gen_or_i32(dst, dst, t0);
4014 tcg_gen_or_i32(dst, dst, t1);
4015 tcg_temp_free_i32(t0);
4016 tcg_temp_free_i32(t1);
4017
4018 tcg_gen_movi_tl(cpu_so, 0);
4019 tcg_gen_movi_tl(cpu_ov, 0);
4020 tcg_gen_movi_tl(cpu_ca, 0);
79aceca5
FB
4021}
4022
0cfe11ea 4023/* mfcr mfocrf */
99e300ef 4024static void gen_mfcr(DisasContext *ctx)
79aceca5 4025{
76a66253 4026 uint32_t crm, crn;
3b46e624 4027
76a66253
JM
4028 if (likely(ctx->opcode & 0x00100000)) {
4029 crm = CRM(ctx->opcode);
8dd640e4 4030 if (likely(crm && ((crm & (crm - 1)) == 0))) {
0cfe11ea 4031 crn = ctz32 (crm);
e1571908 4032 tcg_gen_extu_i32_tl(cpu_gpr[rD(ctx->opcode)], cpu_crf[7 - crn]);
0497d2f4
AJ
4033 tcg_gen_shli_tl(cpu_gpr[rD(ctx->opcode)],
4034 cpu_gpr[rD(ctx->opcode)], crn * 4);
76a66253 4035 }
d9bce9d9 4036 } else {
651721b2
AJ
4037 TCGv_i32 t0 = tcg_temp_new_i32();
4038 tcg_gen_mov_i32(t0, cpu_crf[0]);
4039 tcg_gen_shli_i32(t0, t0, 4);
4040 tcg_gen_or_i32(t0, t0, cpu_crf[1]);
4041 tcg_gen_shli_i32(t0, t0, 4);
4042 tcg_gen_or_i32(t0, t0, cpu_crf[2]);
4043 tcg_gen_shli_i32(t0, t0, 4);
4044 tcg_gen_or_i32(t0, t0, cpu_crf[3]);
4045 tcg_gen_shli_i32(t0, t0, 4);
4046 tcg_gen_or_i32(t0, t0, cpu_crf[4]);
4047 tcg_gen_shli_i32(t0, t0, 4);
4048 tcg_gen_or_i32(t0, t0, cpu_crf[5]);
4049 tcg_gen_shli_i32(t0, t0, 4);
4050 tcg_gen_or_i32(t0, t0, cpu_crf[6]);
4051 tcg_gen_shli_i32(t0, t0, 4);
4052 tcg_gen_or_i32(t0, t0, cpu_crf[7]);
4053 tcg_gen_extu_i32_tl(cpu_gpr[rD(ctx->opcode)], t0);
4054 tcg_temp_free_i32(t0);
d9bce9d9 4055 }
79aceca5
FB
4056}
4057
4058/* mfmsr */
99e300ef 4059static void gen_mfmsr(DisasContext *ctx)
79aceca5 4060{
9a64fbe4 4061#if defined(CONFIG_USER_ONLY)
e06fcd75 4062 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
9a64fbe4 4063#else
76db3ba4 4064 if (unlikely(!ctx->mem_idx)) {
e06fcd75 4065 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
9fddaa0c 4066 return;
9a64fbe4 4067 }
6527f6ea 4068 tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_msr);
9a64fbe4 4069#endif
79aceca5
FB
4070}
4071
7b13448f 4072static void spr_noaccess(void *opaque, int gprn, int sprn)
3fc6c082 4073{
7b13448f 4074#if 0
3fc6c082
FB
4075 sprn = ((sprn >> 5) & 0x1F) | ((sprn & 0x1F) << 5);
4076 printf("ERROR: try to access SPR %d !\n", sprn);
7b13448f 4077#endif
3fc6c082
FB
4078}
4079#define SPR_NOACCESS (&spr_noaccess)
3fc6c082 4080
79aceca5 4081/* mfspr */
636aa200 4082static inline void gen_op_mfspr(DisasContext *ctx)
79aceca5 4083{
45d827d2 4084 void (*read_cb)(void *opaque, int gprn, int sprn);
79aceca5
FB
4085 uint32_t sprn = SPR(ctx->opcode);
4086
3fc6c082 4087#if !defined(CONFIG_USER_ONLY)
76db3ba4 4088 if (ctx->mem_idx == 2)
be147d08 4089 read_cb = ctx->spr_cb[sprn].hea_read;
76db3ba4 4090 else if (ctx->mem_idx)
3fc6c082
FB
4091 read_cb = ctx->spr_cb[sprn].oea_read;
4092 else
9a64fbe4 4093#endif
3fc6c082 4094 read_cb = ctx->spr_cb[sprn].uea_read;
76a66253
JM
4095 if (likely(read_cb != NULL)) {
4096 if (likely(read_cb != SPR_NOACCESS)) {
45d827d2 4097 (*read_cb)(ctx, rD(ctx->opcode), sprn);
3fc6c082
FB
4098 } else {
4099 /* Privilege exception */
9fceefa7
JM
4100 /* This is a hack to avoid warnings when running Linux:
4101 * this OS breaks the PowerPC virtualisation model,
4102 * allowing userland application to read the PVR
4103 */
4104 if (sprn != SPR_PVR) {
c05541ee
AB
4105 qemu_log("Trying to read privileged spr %d (0x%03x) at "
4106 TARGET_FMT_lx "\n", sprn, sprn, ctx->nip - 4);
4107 printf("Trying to read privileged spr %d (0x%03x) at "
4108 TARGET_FMT_lx "\n", sprn, sprn, ctx->nip - 4);
f24e5695 4109 }
e06fcd75 4110 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
79aceca5 4111 }
3fc6c082
FB
4112 } else {
4113 /* Not defined */
c05541ee
AB
4114 qemu_log("Trying to read invalid spr %d (0x%03x) at "
4115 TARGET_FMT_lx "\n", sprn, sprn, ctx->nip - 4);
4116 printf("Trying to read invalid spr %d (0x%03x) at "
4117 TARGET_FMT_lx "\n", sprn, sprn, ctx->nip - 4);
e06fcd75 4118 gen_inval_exception(ctx, POWERPC_EXCP_INVAL_SPR);
79aceca5 4119 }
79aceca5
FB
4120}
4121
99e300ef 4122static void gen_mfspr(DisasContext *ctx)
79aceca5 4123{
3fc6c082 4124 gen_op_mfspr(ctx);
76a66253 4125}
3fc6c082
FB
4126
4127/* mftb */
99e300ef 4128static void gen_mftb(DisasContext *ctx)
3fc6c082
FB
4129{
4130 gen_op_mfspr(ctx);
79aceca5
FB
4131}
4132
0cfe11ea 4133/* mtcrf mtocrf*/
99e300ef 4134static void gen_mtcrf(DisasContext *ctx)
79aceca5 4135{
76a66253 4136 uint32_t crm, crn;
3b46e624 4137
76a66253 4138 crm = CRM(ctx->opcode);
8dd640e4 4139 if (likely((ctx->opcode & 0x00100000))) {
4140 if (crm && ((crm & (crm - 1)) == 0)) {
4141 TCGv_i32 temp = tcg_temp_new_i32();
0cfe11ea 4142 crn = ctz32 (crm);
8dd640e4 4143 tcg_gen_trunc_tl_i32(temp, cpu_gpr[rS(ctx->opcode)]);
0cfe11ea
AJ
4144 tcg_gen_shri_i32(temp, temp, crn * 4);
4145 tcg_gen_andi_i32(cpu_crf[7 - crn], temp, 0xf);
8dd640e4 4146 tcg_temp_free_i32(temp);
4147 }
76a66253 4148 } else {
651721b2
AJ
4149 TCGv_i32 temp = tcg_temp_new_i32();
4150 tcg_gen_trunc_tl_i32(temp, cpu_gpr[rS(ctx->opcode)]);
4151 for (crn = 0 ; crn < 8 ; crn++) {
4152 if (crm & (1 << crn)) {
4153 tcg_gen_shri_i32(cpu_crf[7 - crn], temp, crn * 4);
4154 tcg_gen_andi_i32(cpu_crf[7 - crn], cpu_crf[7 - crn], 0xf);
4155 }
4156 }
a7812ae4 4157 tcg_temp_free_i32(temp);
76a66253 4158 }
79aceca5
FB
4159}
4160
4161/* mtmsr */
426613db 4162#if defined(TARGET_PPC64)
99e300ef 4163static void gen_mtmsrd(DisasContext *ctx)
426613db
JM
4164{
4165#if defined(CONFIG_USER_ONLY)
e06fcd75 4166 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
426613db 4167#else
76db3ba4 4168 if (unlikely(!ctx->mem_idx)) {
e06fcd75 4169 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
426613db
JM
4170 return;
4171 }
be147d08
JM
4172 if (ctx->opcode & 0x00010000) {
4173 /* Special form that does not need any synchronisation */
6527f6ea
AJ
4174 TCGv t0 = tcg_temp_new();
4175 tcg_gen_andi_tl(t0, cpu_gpr[rS(ctx->opcode)], (1 << MSR_RI) | (1 << MSR_EE));
4176 tcg_gen_andi_tl(cpu_msr, cpu_msr, ~((1 << MSR_RI) | (1 << MSR_EE)));
4177 tcg_gen_or_tl(cpu_msr, cpu_msr, t0);
4178 tcg_temp_free(t0);
be147d08 4179 } else {
056b05f8
JM
4180 /* XXX: we need to update nip before the store
4181 * if we enter power saving mode, we will exit the loop
4182 * directly from ppc_store_msr
4183 */
be147d08 4184 gen_update_nip(ctx, ctx->nip);
e5f17ac6 4185 gen_helper_store_msr(cpu_env, cpu_gpr[rS(ctx->opcode)]);
be147d08
JM
4186 /* Must stop the translation as machine state (may have) changed */
4187 /* Note that mtmsr is not always defined as context-synchronizing */
e06fcd75 4188 gen_stop_exception(ctx);
be147d08 4189 }
426613db
JM
4190#endif
4191}
4192#endif
4193
99e300ef 4194static void gen_mtmsr(DisasContext *ctx)
79aceca5 4195{
9a64fbe4 4196#if defined(CONFIG_USER_ONLY)
e06fcd75 4197 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
9a64fbe4 4198#else
76db3ba4 4199 if (unlikely(!ctx->mem_idx)) {
e06fcd75 4200 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
9fddaa0c 4201 return;
9a64fbe4 4202 }
be147d08
JM
4203 if (ctx->opcode & 0x00010000) {
4204 /* Special form that does not need any synchronisation */
6527f6ea
AJ
4205 TCGv t0 = tcg_temp_new();
4206 tcg_gen_andi_tl(t0, cpu_gpr[rS(ctx->opcode)], (1 << MSR_RI) | (1 << MSR_EE));
4207 tcg_gen_andi_tl(cpu_msr, cpu_msr, ~((1 << MSR_RI) | (1 << MSR_EE)));
4208 tcg_gen_or_tl(cpu_msr, cpu_msr, t0);
4209 tcg_temp_free(t0);
be147d08 4210 } else {
8018dc63
AG
4211 TCGv msr = tcg_temp_new();
4212
056b05f8
JM
4213 /* XXX: we need to update nip before the store
4214 * if we enter power saving mode, we will exit the loop
4215 * directly from ppc_store_msr
4216 */
be147d08 4217 gen_update_nip(ctx, ctx->nip);
d9bce9d9 4218#if defined(TARGET_PPC64)
8018dc63
AG
4219 tcg_gen_deposit_tl(msr, cpu_msr, cpu_gpr[rS(ctx->opcode)], 0, 32);
4220#else
4221 tcg_gen_mov_tl(msr, cpu_gpr[rS(ctx->opcode)]);
d9bce9d9 4222#endif
e5f17ac6 4223 gen_helper_store_msr(cpu_env, msr);
be147d08 4224 /* Must stop the translation as machine state (may have) changed */
6527f6ea 4225 /* Note that mtmsr is not always defined as context-synchronizing */
e06fcd75 4226 gen_stop_exception(ctx);
be147d08 4227 }
9a64fbe4 4228#endif
79aceca5
FB
4229}
4230
4231/* mtspr */
99e300ef 4232static void gen_mtspr(DisasContext *ctx)
79aceca5 4233{
45d827d2 4234 void (*write_cb)(void *opaque, int sprn, int gprn);
79aceca5
FB
4235 uint32_t sprn = SPR(ctx->opcode);
4236
3fc6c082 4237#if !defined(CONFIG_USER_ONLY)
76db3ba4 4238 if (ctx->mem_idx == 2)
be147d08 4239 write_cb = ctx->spr_cb[sprn].hea_write;
76db3ba4 4240 else if (ctx->mem_idx)
3fc6c082
FB
4241 write_cb = ctx->spr_cb[sprn].oea_write;
4242 else
9a64fbe4 4243#endif
3fc6c082 4244 write_cb = ctx->spr_cb[sprn].uea_write;
76a66253
JM
4245 if (likely(write_cb != NULL)) {
4246 if (likely(write_cb != SPR_NOACCESS)) {
45d827d2 4247 (*write_cb)(ctx, sprn, rS(ctx->opcode));
3fc6c082
FB
4248 } else {
4249 /* Privilege exception */
c05541ee
AB
4250 qemu_log("Trying to write privileged spr %d (0x%03x) at "
4251 TARGET_FMT_lx "\n", sprn, sprn, ctx->nip - 4);
4252 printf("Trying to write privileged spr %d (0x%03x) at "
4253 TARGET_FMT_lx "\n", sprn, sprn, ctx->nip - 4);
e06fcd75 4254 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
76a66253 4255 }
3fc6c082
FB
4256 } else {
4257 /* Not defined */
c05541ee
AB
4258 qemu_log("Trying to write invalid spr %d (0x%03x) at "
4259 TARGET_FMT_lx "\n", sprn, sprn, ctx->nip - 4);
4260 printf("Trying to write invalid spr %d (0x%03x) at "
4261 TARGET_FMT_lx "\n", sprn, sprn, ctx->nip - 4);
e06fcd75 4262 gen_inval_exception(ctx, POWERPC_EXCP_INVAL_SPR);
79aceca5 4263 }
79aceca5
FB
4264}
4265
4266/*** Cache management ***/
99e300ef 4267
54623277 4268/* dcbf */
99e300ef 4269static void gen_dcbf(DisasContext *ctx)
79aceca5 4270{
dac454af 4271 /* XXX: specification says this is treated as a load by the MMU */
76db3ba4
AJ
4272 TCGv t0;
4273 gen_set_access_type(ctx, ACCESS_CACHE);
4274 t0 = tcg_temp_new();
4275 gen_addr_reg_index(ctx, t0);
4276 gen_qemu_ld8u(ctx, t0, t0);
fea0c503 4277 tcg_temp_free(t0);
79aceca5
FB
4278}
4279
4280/* dcbi (Supervisor only) */
99e300ef 4281static void gen_dcbi(DisasContext *ctx)
79aceca5 4282{
a541f297 4283#if defined(CONFIG_USER_ONLY)
e06fcd75 4284 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
a541f297 4285#else
b61f2753 4286 TCGv EA, val;
76db3ba4 4287 if (unlikely(!ctx->mem_idx)) {
e06fcd75 4288 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
9fddaa0c 4289 return;
9a64fbe4 4290 }
a7812ae4 4291 EA = tcg_temp_new();
76db3ba4
AJ
4292 gen_set_access_type(ctx, ACCESS_CACHE);
4293 gen_addr_reg_index(ctx, EA);
a7812ae4 4294 val = tcg_temp_new();
76a66253 4295 /* XXX: specification says this should be treated as a store by the MMU */
76db3ba4
AJ
4296 gen_qemu_ld8u(ctx, val, EA);
4297 gen_qemu_st8(ctx, val, EA);
b61f2753
AJ
4298 tcg_temp_free(val);
4299 tcg_temp_free(EA);
a541f297 4300#endif
79aceca5
FB
4301}
4302
4303/* dcdst */
99e300ef 4304static void gen_dcbst(DisasContext *ctx)
79aceca5 4305{
76a66253 4306 /* XXX: specification say this is treated as a load by the MMU */
76db3ba4
AJ
4307 TCGv t0;
4308 gen_set_access_type(ctx, ACCESS_CACHE);
4309 t0 = tcg_temp_new();
4310 gen_addr_reg_index(ctx, t0);
4311 gen_qemu_ld8u(ctx, t0, t0);
fea0c503 4312 tcg_temp_free(t0);
79aceca5
FB
4313}
4314
4315/* dcbt */
99e300ef 4316static void gen_dcbt(DisasContext *ctx)
79aceca5 4317{
0db1b20e 4318 /* interpreted as no-op */
76a66253
JM
4319 /* XXX: specification say this is treated as a load by the MMU
4320 * but does not generate any exception
4321 */
79aceca5
FB
4322}
4323
4324/* dcbtst */
99e300ef 4325static void gen_dcbtst(DisasContext *ctx)
79aceca5 4326{
0db1b20e 4327 /* interpreted as no-op */
76a66253
JM
4328 /* XXX: specification say this is treated as a load by the MMU
4329 * but does not generate any exception
4330 */
79aceca5
FB
4331}
4332
4333/* dcbz */
99e300ef 4334static void gen_dcbz(DisasContext *ctx)
79aceca5 4335{
8e33944f
AG
4336 TCGv tcgv_addr;
4337 TCGv_i32 tcgv_is_dcbzl;
4338 int is_dcbzl = ctx->opcode & 0x00200000 ? 1 : 0;
d63001d1 4339
76db3ba4 4340 gen_set_access_type(ctx, ACCESS_CACHE);
799a8c8d
AJ
4341 /* NIP cannot be restored if the memory exception comes from an helper */
4342 gen_update_nip(ctx, ctx->nip - 4);
8e33944f
AG
4343 tcgv_addr = tcg_temp_new();
4344 tcgv_is_dcbzl = tcg_const_i32(is_dcbzl);
4345
4346 gen_addr_reg_index(ctx, tcgv_addr);
4347 gen_helper_dcbz(cpu_env, tcgv_addr, tcgv_is_dcbzl);
4348
4349 tcg_temp_free(tcgv_addr);
4350 tcg_temp_free_i32(tcgv_is_dcbzl);
79aceca5
FB
4351}
4352
ae1c1a3d 4353/* dst / dstt */
99e300ef 4354static void gen_dst(DisasContext *ctx)
ae1c1a3d
AJ
4355{
4356 if (rA(ctx->opcode) == 0) {
4357 gen_inval_exception(ctx, POWERPC_EXCP_INVAL_LSWX);
4358 } else {
4359 /* interpreted as no-op */
4360 }
4361}
4362
4363/* dstst /dststt */
99e300ef 4364static void gen_dstst(DisasContext *ctx)
ae1c1a3d
AJ
4365{
4366 if (rA(ctx->opcode) == 0) {
4367 gen_inval_exception(ctx, POWERPC_EXCP_INVAL_LSWX);
4368 } else {
4369 /* interpreted as no-op */
4370 }
4371
4372}
4373
4374/* dss / dssall */
99e300ef 4375static void gen_dss(DisasContext *ctx)
ae1c1a3d
AJ
4376{
4377 /* interpreted as no-op */
4378}
4379
79aceca5 4380/* icbi */
99e300ef 4381static void gen_icbi(DisasContext *ctx)
79aceca5 4382{
76db3ba4
AJ
4383 TCGv t0;
4384 gen_set_access_type(ctx, ACCESS_CACHE);
30032c94
JM
4385 /* NIP cannot be restored if the memory exception comes from an helper */
4386 gen_update_nip(ctx, ctx->nip - 4);
76db3ba4
AJ
4387 t0 = tcg_temp_new();
4388 gen_addr_reg_index(ctx, t0);
2f5a189c 4389 gen_helper_icbi(cpu_env, t0);
37d269df 4390 tcg_temp_free(t0);
79aceca5
FB
4391}
4392
4393/* Optional: */
4394/* dcba */
99e300ef 4395static void gen_dcba(DisasContext *ctx)
79aceca5 4396{
0db1b20e
JM
4397 /* interpreted as no-op */
4398 /* XXX: specification say this is treated as a store by the MMU
4399 * but does not generate any exception
4400 */
79aceca5
FB
4401}
4402
4403/*** Segment register manipulation ***/
4404/* Supervisor only: */
99e300ef 4405
54623277 4406/* mfsr */
99e300ef 4407static void gen_mfsr(DisasContext *ctx)
79aceca5 4408{
9a64fbe4 4409#if defined(CONFIG_USER_ONLY)
e06fcd75 4410 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
9a64fbe4 4411#else
74d37793 4412 TCGv t0;
76db3ba4 4413 if (unlikely(!ctx->mem_idx)) {
e06fcd75 4414 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
9fddaa0c 4415 return;
9a64fbe4 4416 }
74d37793 4417 t0 = tcg_const_tl(SR(ctx->opcode));
c6c7cf05 4418 gen_helper_load_sr(cpu_gpr[rD(ctx->opcode)], cpu_env, t0);
74d37793 4419 tcg_temp_free(t0);
9a64fbe4 4420#endif
79aceca5
FB
4421}
4422
4423/* mfsrin */
99e300ef 4424static void gen_mfsrin(DisasContext *ctx)
79aceca5 4425{
9a64fbe4 4426#if defined(CONFIG_USER_ONLY)
e06fcd75 4427 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
9a64fbe4 4428#else
74d37793 4429 TCGv t0;
76db3ba4 4430 if (unlikely(!ctx->mem_idx)) {
e06fcd75 4431 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
9fddaa0c 4432 return;
9a64fbe4 4433 }
74d37793
AJ
4434 t0 = tcg_temp_new();
4435 tcg_gen_shri_tl(t0, cpu_gpr[rB(ctx->opcode)], 28);
4436 tcg_gen_andi_tl(t0, t0, 0xF);
c6c7cf05 4437 gen_helper_load_sr(cpu_gpr[rD(ctx->opcode)], cpu_env, t0);
74d37793 4438 tcg_temp_free(t0);
9a64fbe4 4439#endif
79aceca5
FB
4440}
4441
4442/* mtsr */
99e300ef 4443static void gen_mtsr(DisasContext *ctx)
79aceca5 4444{
9a64fbe4 4445#if defined(CONFIG_USER_ONLY)
e06fcd75 4446 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
9a64fbe4 4447#else
74d37793 4448 TCGv t0;
76db3ba4 4449 if (unlikely(!ctx->mem_idx)) {
e06fcd75 4450 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
9fddaa0c 4451 return;
9a64fbe4 4452 }
74d37793 4453 t0 = tcg_const_tl(SR(ctx->opcode));
c6c7cf05 4454 gen_helper_store_sr(cpu_env, t0, cpu_gpr[rS(ctx->opcode)]);
74d37793 4455 tcg_temp_free(t0);
9a64fbe4 4456#endif
79aceca5
FB
4457}
4458
4459/* mtsrin */
99e300ef 4460static void gen_mtsrin(DisasContext *ctx)
79aceca5 4461{
9a64fbe4 4462#if defined(CONFIG_USER_ONLY)
e06fcd75 4463 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
9a64fbe4 4464#else
74d37793 4465 TCGv t0;
76db3ba4 4466 if (unlikely(!ctx->mem_idx)) {
e06fcd75 4467 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
9fddaa0c 4468 return;
9a64fbe4 4469 }
74d37793
AJ
4470 t0 = tcg_temp_new();
4471 tcg_gen_shri_tl(t0, cpu_gpr[rB(ctx->opcode)], 28);
4472 tcg_gen_andi_tl(t0, t0, 0xF);
c6c7cf05 4473 gen_helper_store_sr(cpu_env, t0, cpu_gpr[rD(ctx->opcode)]);
74d37793 4474 tcg_temp_free(t0);
9a64fbe4 4475#endif
79aceca5
FB
4476}
4477
12de9a39
JM
4478#if defined(TARGET_PPC64)
4479/* Specific implementation for PowerPC 64 "bridge" emulation using SLB */
e8eaa2c0 4480
54623277 4481/* mfsr */
e8eaa2c0 4482static void gen_mfsr_64b(DisasContext *ctx)
12de9a39
JM
4483{
4484#if defined(CONFIG_USER_ONLY)
e06fcd75 4485 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
12de9a39 4486#else
74d37793 4487 TCGv t0;
76db3ba4 4488 if (unlikely(!ctx->mem_idx)) {
e06fcd75 4489 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
12de9a39
JM
4490 return;
4491 }
74d37793 4492 t0 = tcg_const_tl(SR(ctx->opcode));
c6c7cf05 4493 gen_helper_load_sr(cpu_gpr[rD(ctx->opcode)], cpu_env, t0);
74d37793 4494 tcg_temp_free(t0);
12de9a39
JM
4495#endif
4496}
4497
4498/* mfsrin */
e8eaa2c0 4499static void gen_mfsrin_64b(DisasContext *ctx)
12de9a39
JM
4500{
4501#if defined(CONFIG_USER_ONLY)
e06fcd75 4502 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
12de9a39 4503#else
74d37793 4504 TCGv t0;
76db3ba4 4505 if (unlikely(!ctx->mem_idx)) {
e06fcd75 4506 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
12de9a39
JM
4507 return;
4508 }
74d37793
AJ
4509 t0 = tcg_temp_new();
4510 tcg_gen_shri_tl(t0, cpu_gpr[rB(ctx->opcode)], 28);
4511 tcg_gen_andi_tl(t0, t0, 0xF);
c6c7cf05 4512 gen_helper_load_sr(cpu_gpr[rD(ctx->opcode)], cpu_env, t0);
74d37793 4513 tcg_temp_free(t0);
12de9a39
JM
4514#endif
4515}
4516
4517/* mtsr */
e8eaa2c0 4518static void gen_mtsr_64b(DisasContext *ctx)
12de9a39
JM
4519{
4520#if defined(CONFIG_USER_ONLY)
e06fcd75 4521 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
12de9a39 4522#else
74d37793 4523 TCGv t0;
76db3ba4 4524 if (unlikely(!ctx->mem_idx)) {
e06fcd75 4525 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
12de9a39
JM
4526 return;
4527 }
74d37793 4528 t0 = tcg_const_tl(SR(ctx->opcode));
c6c7cf05 4529 gen_helper_store_sr(cpu_env, t0, cpu_gpr[rS(ctx->opcode)]);
74d37793 4530 tcg_temp_free(t0);
12de9a39
JM
4531#endif
4532}
4533
4534/* mtsrin */
e8eaa2c0 4535static void gen_mtsrin_64b(DisasContext *ctx)
12de9a39
JM
4536{
4537#if defined(CONFIG_USER_ONLY)
e06fcd75 4538 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
12de9a39 4539#else
74d37793 4540 TCGv t0;
76db3ba4 4541 if (unlikely(!ctx->mem_idx)) {
e06fcd75 4542 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
12de9a39
JM
4543 return;
4544 }
74d37793
AJ
4545 t0 = tcg_temp_new();
4546 tcg_gen_shri_tl(t0, cpu_gpr[rB(ctx->opcode)], 28);
4547 tcg_gen_andi_tl(t0, t0, 0xF);
c6c7cf05 4548 gen_helper_store_sr(cpu_env, t0, cpu_gpr[rS(ctx->opcode)]);
74d37793 4549 tcg_temp_free(t0);
12de9a39
JM
4550#endif
4551}
f6b868fc
BS
4552
4553/* slbmte */
e8eaa2c0 4554static void gen_slbmte(DisasContext *ctx)
f6b868fc
BS
4555{
4556#if defined(CONFIG_USER_ONLY)
4557 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
4558#else
4559 if (unlikely(!ctx->mem_idx)) {
4560 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
4561 return;
4562 }
c6c7cf05
BS
4563 gen_helper_store_slb(cpu_env, cpu_gpr[rB(ctx->opcode)],
4564 cpu_gpr[rS(ctx->opcode)]);
f6b868fc
BS
4565#endif
4566}
4567
efdef95f
DG
4568static void gen_slbmfee(DisasContext *ctx)
4569{
4570#if defined(CONFIG_USER_ONLY)
4571 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
4572#else
4573 if (unlikely(!ctx->mem_idx)) {
4574 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
4575 return;
4576 }
c6c7cf05 4577 gen_helper_load_slb_esid(cpu_gpr[rS(ctx->opcode)], cpu_env,
efdef95f
DG
4578 cpu_gpr[rB(ctx->opcode)]);
4579#endif
4580}
4581
4582static void gen_slbmfev(DisasContext *ctx)
4583{
4584#if defined(CONFIG_USER_ONLY)
4585 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
4586#else
4587 if (unlikely(!ctx->mem_idx)) {
4588 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
4589 return;
4590 }
c6c7cf05 4591 gen_helper_load_slb_vsid(cpu_gpr[rS(ctx->opcode)], cpu_env,
efdef95f
DG
4592 cpu_gpr[rB(ctx->opcode)]);
4593#endif
4594}
12de9a39
JM
4595#endif /* defined(TARGET_PPC64) */
4596
79aceca5 4597/*** Lookaside buffer management ***/
76db3ba4 4598/* Optional & mem_idx only: */
99e300ef 4599
54623277 4600/* tlbia */
99e300ef 4601static void gen_tlbia(DisasContext *ctx)
79aceca5 4602{
9a64fbe4 4603#if defined(CONFIG_USER_ONLY)
e06fcd75 4604 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
9a64fbe4 4605#else
76db3ba4 4606 if (unlikely(!ctx->mem_idx)) {
e06fcd75 4607 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
9fddaa0c 4608 return;
9a64fbe4 4609 }
c6c7cf05 4610 gen_helper_tlbia(cpu_env);
9a64fbe4 4611#endif
79aceca5
FB
4612}
4613
bf14b1ce 4614/* tlbiel */
99e300ef 4615static void gen_tlbiel(DisasContext *ctx)
bf14b1ce
BS
4616{
4617#if defined(CONFIG_USER_ONLY)
4618 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
4619#else
4620 if (unlikely(!ctx->mem_idx)) {
4621 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
4622 return;
4623 }
c6c7cf05 4624 gen_helper_tlbie(cpu_env, cpu_gpr[rB(ctx->opcode)]);
bf14b1ce
BS
4625#endif
4626}
4627
79aceca5 4628/* tlbie */
99e300ef 4629static void gen_tlbie(DisasContext *ctx)
79aceca5 4630{
9a64fbe4 4631#if defined(CONFIG_USER_ONLY)
e06fcd75 4632 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
9a64fbe4 4633#else
76db3ba4 4634 if (unlikely(!ctx->mem_idx)) {
e06fcd75 4635 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
9fddaa0c 4636 return;
9a64fbe4 4637 }
9ca3f7f3 4638 if (NARROW_MODE(ctx)) {
74d37793
AJ
4639 TCGv t0 = tcg_temp_new();
4640 tcg_gen_ext32u_tl(t0, cpu_gpr[rB(ctx->opcode)]);
c6c7cf05 4641 gen_helper_tlbie(cpu_env, t0);
74d37793 4642 tcg_temp_free(t0);
9ca3f7f3 4643 } else {
c6c7cf05 4644 gen_helper_tlbie(cpu_env, cpu_gpr[rB(ctx->opcode)]);
9ca3f7f3 4645 }
9a64fbe4 4646#endif
79aceca5
FB
4647}
4648
4649/* tlbsync */
99e300ef 4650static void gen_tlbsync(DisasContext *ctx)
79aceca5 4651{
9a64fbe4 4652#if defined(CONFIG_USER_ONLY)
e06fcd75 4653 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
9a64fbe4 4654#else
76db3ba4 4655 if (unlikely(!ctx->mem_idx)) {
e06fcd75 4656 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
9fddaa0c 4657 return;
9a64fbe4
FB
4658 }
4659 /* This has no effect: it should ensure that all previous
4660 * tlbie have completed
4661 */
e06fcd75 4662 gen_stop_exception(ctx);
9a64fbe4 4663#endif
79aceca5
FB
4664}
4665
426613db
JM
4666#if defined(TARGET_PPC64)
4667/* slbia */
99e300ef 4668static void gen_slbia(DisasContext *ctx)
426613db
JM
4669{
4670#if defined(CONFIG_USER_ONLY)
e06fcd75 4671 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
426613db 4672#else
76db3ba4 4673 if (unlikely(!ctx->mem_idx)) {
e06fcd75 4674 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
426613db
JM
4675 return;
4676 }
c6c7cf05 4677 gen_helper_slbia(cpu_env);
426613db
JM
4678#endif
4679}
4680
4681/* slbie */
99e300ef 4682static void gen_slbie(DisasContext *ctx)
426613db
JM
4683{
4684#if defined(CONFIG_USER_ONLY)
e06fcd75 4685 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
426613db 4686#else
76db3ba4 4687 if (unlikely(!ctx->mem_idx)) {
e06fcd75 4688 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
426613db
JM
4689 return;
4690 }
c6c7cf05 4691 gen_helper_slbie(cpu_env, cpu_gpr[rB(ctx->opcode)]);
426613db
JM
4692#endif
4693}
4694#endif
4695
79aceca5
FB
4696/*** External control ***/
4697/* Optional: */
99e300ef 4698
54623277 4699/* eciwx */
99e300ef 4700static void gen_eciwx(DisasContext *ctx)
79aceca5 4701{
76db3ba4 4702 TCGv t0;
fa407c03 4703 /* Should check EAR[E] ! */
76db3ba4
AJ
4704 gen_set_access_type(ctx, ACCESS_EXT);
4705 t0 = tcg_temp_new();
4706 gen_addr_reg_index(ctx, t0);
fa407c03 4707 gen_check_align(ctx, t0, 0x03);
76db3ba4 4708 gen_qemu_ld32u(ctx, cpu_gpr[rD(ctx->opcode)], t0);
fa407c03 4709 tcg_temp_free(t0);
76a66253
JM
4710}
4711
4712/* ecowx */
99e300ef 4713static void gen_ecowx(DisasContext *ctx)
76a66253 4714{
76db3ba4 4715 TCGv t0;
fa407c03 4716 /* Should check EAR[E] ! */
76db3ba4
AJ
4717 gen_set_access_type(ctx, ACCESS_EXT);
4718 t0 = tcg_temp_new();
4719 gen_addr_reg_index(ctx, t0);
fa407c03 4720 gen_check_align(ctx, t0, 0x03);
76db3ba4 4721 gen_qemu_st32(ctx, cpu_gpr[rD(ctx->opcode)], t0);
fa407c03 4722 tcg_temp_free(t0);
76a66253
JM
4723}
4724
4725/* PowerPC 601 specific instructions */
99e300ef 4726
54623277 4727/* abs - abs. */
99e300ef 4728static void gen_abs(DisasContext *ctx)
76a66253 4729{
22e0e173
AJ
4730 int l1 = gen_new_label();
4731 int l2 = gen_new_label();
4732 tcg_gen_brcondi_tl(TCG_COND_GE, cpu_gpr[rA(ctx->opcode)], 0, l1);
4733 tcg_gen_neg_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)]);
4734 tcg_gen_br(l2);
4735 gen_set_label(l1);
4736 tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)]);
4737 gen_set_label(l2);
76a66253 4738 if (unlikely(Rc(ctx->opcode) != 0))
22e0e173 4739 gen_set_Rc0(ctx, cpu_gpr[rD(ctx->opcode)]);
76a66253
JM
4740}
4741
4742/* abso - abso. */
99e300ef 4743static void gen_abso(DisasContext *ctx)
76a66253 4744{
22e0e173
AJ
4745 int l1 = gen_new_label();
4746 int l2 = gen_new_label();
4747 int l3 = gen_new_label();
4748 /* Start with XER OV disabled, the most likely case */
da91a00f 4749 tcg_gen_movi_tl(cpu_ov, 0);
22e0e173
AJ
4750 tcg_gen_brcondi_tl(TCG_COND_GE, cpu_gpr[rA(ctx->opcode)], 0, l2);
4751 tcg_gen_brcondi_tl(TCG_COND_NE, cpu_gpr[rA(ctx->opcode)], 0x80000000, l1);
da91a00f
RH
4752 tcg_gen_movi_tl(cpu_ov, 1);
4753 tcg_gen_movi_tl(cpu_so, 1);
22e0e173
AJ
4754 tcg_gen_br(l2);
4755 gen_set_label(l1);
4756 tcg_gen_neg_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)]);
4757 tcg_gen_br(l3);
4758 gen_set_label(l2);
4759 tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)]);
4760 gen_set_label(l3);
76a66253 4761 if (unlikely(Rc(ctx->opcode) != 0))
22e0e173 4762 gen_set_Rc0(ctx, cpu_gpr[rD(ctx->opcode)]);
76a66253
JM
4763}
4764
4765/* clcs */
99e300ef 4766static void gen_clcs(DisasContext *ctx)
76a66253 4767{
22e0e173 4768 TCGv_i32 t0 = tcg_const_i32(rA(ctx->opcode));
d523dd00 4769 gen_helper_clcs(cpu_gpr[rD(ctx->opcode)], cpu_env, t0);
22e0e173 4770 tcg_temp_free_i32(t0);
c7697e1f 4771 /* Rc=1 sets CR0 to an undefined state */
76a66253
JM
4772}
4773
4774/* div - div. */
99e300ef 4775static void gen_div(DisasContext *ctx)
76a66253 4776{
d15f74fb
BS
4777 gen_helper_div(cpu_gpr[rD(ctx->opcode)], cpu_env, cpu_gpr[rA(ctx->opcode)],
4778 cpu_gpr[rB(ctx->opcode)]);
76a66253 4779 if (unlikely(Rc(ctx->opcode) != 0))
22e0e173 4780 gen_set_Rc0(ctx, cpu_gpr[rD(ctx->opcode)]);
76a66253
JM
4781}
4782
4783/* divo - divo. */
99e300ef 4784static void gen_divo(DisasContext *ctx)
76a66253 4785{
d15f74fb
BS
4786 gen_helper_divo(cpu_gpr[rD(ctx->opcode)], cpu_env, cpu_gpr[rA(ctx->opcode)],
4787 cpu_gpr[rB(ctx->opcode)]);
76a66253 4788 if (unlikely(Rc(ctx->opcode) != 0))
22e0e173 4789 gen_set_Rc0(ctx, cpu_gpr[rD(ctx->opcode)]);
76a66253
JM
4790}
4791
4792/* divs - divs. */
99e300ef 4793static void gen_divs(DisasContext *ctx)
76a66253 4794{
d15f74fb
BS
4795 gen_helper_divs(cpu_gpr[rD(ctx->opcode)], cpu_env, cpu_gpr[rA(ctx->opcode)],
4796 cpu_gpr[rB(ctx->opcode)]);
76a66253 4797 if (unlikely(Rc(ctx->opcode) != 0))
22e0e173 4798 gen_set_Rc0(ctx, cpu_gpr[rD(ctx->opcode)]);
76a66253
JM
4799}
4800
4801/* divso - divso. */
99e300ef 4802static void gen_divso(DisasContext *ctx)
76a66253 4803{
d15f74fb
BS
4804 gen_helper_divso(cpu_gpr[rD(ctx->opcode)], cpu_env,
4805 cpu_gpr[rA(ctx->opcode)], cpu_gpr[rB(ctx->opcode)]);
76a66253 4806 if (unlikely(Rc(ctx->opcode) != 0))
22e0e173 4807 gen_set_Rc0(ctx, cpu_gpr[rD(ctx->opcode)]);
76a66253
JM
4808}
4809
4810/* doz - doz. */
99e300ef 4811static void gen_doz(DisasContext *ctx)
76a66253 4812{
22e0e173
AJ
4813 int l1 = gen_new_label();
4814 int l2 = gen_new_label();
4815 tcg_gen_brcond_tl(TCG_COND_GE, cpu_gpr[rB(ctx->opcode)], cpu_gpr[rA(ctx->opcode)], l1);
4816 tcg_gen_sub_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rB(ctx->opcode)], cpu_gpr[rA(ctx->opcode)]);
4817 tcg_gen_br(l2);
4818 gen_set_label(l1);
4819 tcg_gen_movi_tl(cpu_gpr[rD(ctx->opcode)], 0);
4820 gen_set_label(l2);
76a66253 4821 if (unlikely(Rc(ctx->opcode) != 0))
22e0e173 4822 gen_set_Rc0(ctx, cpu_gpr[rD(ctx->opcode)]);
76a66253
JM
4823}
4824
4825/* dozo - dozo. */
99e300ef 4826static void gen_dozo(DisasContext *ctx)
76a66253 4827{
22e0e173
AJ
4828 int l1 = gen_new_label();
4829 int l2 = gen_new_label();
4830 TCGv t0 = tcg_temp_new();
4831 TCGv t1 = tcg_temp_new();
4832 TCGv t2 = tcg_temp_new();
4833 /* Start with XER OV disabled, the most likely case */
da91a00f 4834 tcg_gen_movi_tl(cpu_ov, 0);
22e0e173
AJ
4835 tcg_gen_brcond_tl(TCG_COND_GE, cpu_gpr[rB(ctx->opcode)], cpu_gpr[rA(ctx->opcode)], l1);
4836 tcg_gen_sub_tl(t0, cpu_gpr[rB(ctx->opcode)], cpu_gpr[rA(ctx->opcode)]);
4837 tcg_gen_xor_tl(t1, cpu_gpr[rB(ctx->opcode)], cpu_gpr[rA(ctx->opcode)]);
4838 tcg_gen_xor_tl(t2, cpu_gpr[rA(ctx->opcode)], t0);
4839 tcg_gen_andc_tl(t1, t1, t2);
4840 tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], t0);
4841 tcg_gen_brcondi_tl(TCG_COND_GE, t1, 0, l2);
da91a00f
RH
4842 tcg_gen_movi_tl(cpu_ov, 1);
4843 tcg_gen_movi_tl(cpu_so, 1);
22e0e173
AJ
4844 tcg_gen_br(l2);
4845 gen_set_label(l1);
4846 tcg_gen_movi_tl(cpu_gpr[rD(ctx->opcode)], 0);
4847 gen_set_label(l2);
4848 tcg_temp_free(t0);
4849 tcg_temp_free(t1);
4850 tcg_temp_free(t2);
76a66253 4851 if (unlikely(Rc(ctx->opcode) != 0))
22e0e173 4852 gen_set_Rc0(ctx, cpu_gpr[rD(ctx->opcode)]);
76a66253
JM
4853}
4854
4855/* dozi */
99e300ef 4856static void gen_dozi(DisasContext *ctx)
76a66253 4857{
22e0e173
AJ
4858 target_long simm = SIMM(ctx->opcode);
4859 int l1 = gen_new_label();
4860 int l2 = gen_new_label();
4861 tcg_gen_brcondi_tl(TCG_COND_LT, cpu_gpr[rA(ctx->opcode)], simm, l1);
4862 tcg_gen_subfi_tl(cpu_gpr[rD(ctx->opcode)], simm, cpu_gpr[rA(ctx->opcode)]);
4863 tcg_gen_br(l2);
4864 gen_set_label(l1);
4865 tcg_gen_movi_tl(cpu_gpr[rD(ctx->opcode)], 0);
4866 gen_set_label(l2);
4867 if (unlikely(Rc(ctx->opcode) != 0))
4868 gen_set_Rc0(ctx, cpu_gpr[rD(ctx->opcode)]);
76a66253
JM
4869}
4870
76a66253 4871/* lscbx - lscbx. */
99e300ef 4872static void gen_lscbx(DisasContext *ctx)
76a66253 4873{
bdb4b689
AJ
4874 TCGv t0 = tcg_temp_new();
4875 TCGv_i32 t1 = tcg_const_i32(rD(ctx->opcode));
4876 TCGv_i32 t2 = tcg_const_i32(rA(ctx->opcode));
4877 TCGv_i32 t3 = tcg_const_i32(rB(ctx->opcode));
76a66253 4878
76db3ba4 4879 gen_addr_reg_index(ctx, t0);
76a66253 4880 /* NIP cannot be restored if the memory exception comes from an helper */
d9bce9d9 4881 gen_update_nip(ctx, ctx->nip - 4);
2f5a189c 4882 gen_helper_lscbx(t0, cpu_env, t0, t1, t2, t3);
bdb4b689
AJ
4883 tcg_temp_free_i32(t1);
4884 tcg_temp_free_i32(t2);
4885 tcg_temp_free_i32(t3);
3d7b417e 4886 tcg_gen_andi_tl(cpu_xer, cpu_xer, ~0x7F);
bdb4b689 4887 tcg_gen_or_tl(cpu_xer, cpu_xer, t0);
76a66253 4888 if (unlikely(Rc(ctx->opcode) != 0))
bdb4b689
AJ
4889 gen_set_Rc0(ctx, t0);
4890 tcg_temp_free(t0);
76a66253
JM
4891}
4892
4893/* maskg - maskg. */
99e300ef 4894static void gen_maskg(DisasContext *ctx)
76a66253 4895{
22e0e173
AJ
4896 int l1 = gen_new_label();
4897 TCGv t0 = tcg_temp_new();
4898 TCGv t1 = tcg_temp_new();
4899 TCGv t2 = tcg_temp_new();
4900 TCGv t3 = tcg_temp_new();
4901 tcg_gen_movi_tl(t3, 0xFFFFFFFF);
4902 tcg_gen_andi_tl(t0, cpu_gpr[rB(ctx->opcode)], 0x1F);
4903 tcg_gen_andi_tl(t1, cpu_gpr[rS(ctx->opcode)], 0x1F);
4904 tcg_gen_addi_tl(t2, t0, 1);
4905 tcg_gen_shr_tl(t2, t3, t2);
4906 tcg_gen_shr_tl(t3, t3, t1);
4907 tcg_gen_xor_tl(cpu_gpr[rA(ctx->opcode)], t2, t3);
4908 tcg_gen_brcond_tl(TCG_COND_GE, t0, t1, l1);
4909 tcg_gen_neg_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rA(ctx->opcode)]);
4910 gen_set_label(l1);
4911 tcg_temp_free(t0);
4912 tcg_temp_free(t1);
4913 tcg_temp_free(t2);
4914 tcg_temp_free(t3);
76a66253 4915 if (unlikely(Rc(ctx->opcode) != 0))
22e0e173 4916 gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
76a66253
JM
4917}
4918
4919/* maskir - maskir. */
99e300ef 4920static void gen_maskir(DisasContext *ctx)
76a66253 4921{
22e0e173
AJ
4922 TCGv t0 = tcg_temp_new();
4923 TCGv t1 = tcg_temp_new();
4924 tcg_gen_and_tl(t0, cpu_gpr[rS(ctx->opcode)], cpu_gpr[rB(ctx->opcode)]);
4925 tcg_gen_andc_tl(t1, cpu_gpr[rA(ctx->opcode)], cpu_gpr[rB(ctx->opcode)]);
4926 tcg_gen_or_tl(cpu_gpr[rA(ctx->opcode)], t0, t1);
4927 tcg_temp_free(t0);
4928 tcg_temp_free(t1);
76a66253 4929 if (unlikely(Rc(ctx->opcode) != 0))
22e0e173 4930 gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
76a66253
JM
4931}
4932
4933/* mul - mul. */
99e300ef 4934static void gen_mul(DisasContext *ctx)
76a66253 4935{
22e0e173
AJ
4936 TCGv_i64 t0 = tcg_temp_new_i64();
4937 TCGv_i64 t1 = tcg_temp_new_i64();
4938 TCGv t2 = tcg_temp_new();
4939 tcg_gen_extu_tl_i64(t0, cpu_gpr[rA(ctx->opcode)]);
4940 tcg_gen_extu_tl_i64(t1, cpu_gpr[rB(ctx->opcode)]);
4941 tcg_gen_mul_i64(t0, t0, t1);
4942 tcg_gen_trunc_i64_tl(t2, t0);
4943 gen_store_spr(SPR_MQ, t2);
4944 tcg_gen_shri_i64(t1, t0, 32);
4945 tcg_gen_trunc_i64_tl(cpu_gpr[rD(ctx->opcode)], t1);
4946 tcg_temp_free_i64(t0);
4947 tcg_temp_free_i64(t1);
4948 tcg_temp_free(t2);
76a66253 4949 if (unlikely(Rc(ctx->opcode) != 0))
22e0e173 4950 gen_set_Rc0(ctx, cpu_gpr[rD(ctx->opcode)]);
76a66253
JM
4951}
4952
4953/* mulo - mulo. */
99e300ef 4954static void gen_mulo(DisasContext *ctx)
76a66253 4955{
22e0e173
AJ
4956 int l1 = gen_new_label();
4957 TCGv_i64 t0 = tcg_temp_new_i64();
4958 TCGv_i64 t1 = tcg_temp_new_i64();
4959 TCGv t2 = tcg_temp_new();
4960 /* Start with XER OV disabled, the most likely case */
da91a00f 4961 tcg_gen_movi_tl(cpu_ov, 0);
22e0e173
AJ
4962 tcg_gen_extu_tl_i64(t0, cpu_gpr[rA(ctx->opcode)]);
4963 tcg_gen_extu_tl_i64(t1, cpu_gpr[rB(ctx->opcode)]);
4964 tcg_gen_mul_i64(t0, t0, t1);
4965 tcg_gen_trunc_i64_tl(t2, t0);
4966 gen_store_spr(SPR_MQ, t2);
4967 tcg_gen_shri_i64(t1, t0, 32);
4968 tcg_gen_trunc_i64_tl(cpu_gpr[rD(ctx->opcode)], t1);
4969 tcg_gen_ext32s_i64(t1, t0);
4970 tcg_gen_brcond_i64(TCG_COND_EQ, t0, t1, l1);
da91a00f
RH
4971 tcg_gen_movi_tl(cpu_ov, 1);
4972 tcg_gen_movi_tl(cpu_so, 1);
22e0e173
AJ
4973 gen_set_label(l1);
4974 tcg_temp_free_i64(t0);
4975 tcg_temp_free_i64(t1);
4976 tcg_temp_free(t2);
76a66253 4977 if (unlikely(Rc(ctx->opcode) != 0))
22e0e173 4978 gen_set_Rc0(ctx, cpu_gpr[rD(ctx->opcode)]);
76a66253
JM
4979}
4980
4981/* nabs - nabs. */
99e300ef 4982static void gen_nabs(DisasContext *ctx)
76a66253 4983{
22e0e173
AJ
4984 int l1 = gen_new_label();
4985 int l2 = gen_new_label();
4986 tcg_gen_brcondi_tl(TCG_COND_GT, cpu_gpr[rA(ctx->opcode)], 0, l1);
4987 tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)]);
4988 tcg_gen_br(l2);
4989 gen_set_label(l1);
4990 tcg_gen_neg_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)]);
4991 gen_set_label(l2);
76a66253 4992 if (unlikely(Rc(ctx->opcode) != 0))
22e0e173 4993 gen_set_Rc0(ctx, cpu_gpr[rD(ctx->opcode)]);
76a66253
JM
4994}
4995
4996/* nabso - nabso. */
99e300ef 4997static void gen_nabso(DisasContext *ctx)
76a66253 4998{
22e0e173
AJ
4999 int l1 = gen_new_label();
5000 int l2 = gen_new_label();
5001 tcg_gen_brcondi_tl(TCG_COND_GT, cpu_gpr[rA(ctx->opcode)], 0, l1);
5002 tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)]);
5003 tcg_gen_br(l2);
5004 gen_set_label(l1);
5005 tcg_gen_neg_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)]);
5006 gen_set_label(l2);
5007 /* nabs never overflows */
da91a00f 5008 tcg_gen_movi_tl(cpu_ov, 0);
76a66253 5009 if (unlikely(Rc(ctx->opcode) != 0))
22e0e173 5010 gen_set_Rc0(ctx, cpu_gpr[rD(ctx->opcode)]);
76a66253
JM
5011}
5012
5013/* rlmi - rlmi. */
99e300ef 5014static void gen_rlmi(DisasContext *ctx)
76a66253 5015{
7487953d
AJ
5016 uint32_t mb = MB(ctx->opcode);
5017 uint32_t me = ME(ctx->opcode);
5018 TCGv t0 = tcg_temp_new();
5019 tcg_gen_andi_tl(t0, cpu_gpr[rB(ctx->opcode)], 0x1F);
5020 tcg_gen_rotl_tl(t0, cpu_gpr[rS(ctx->opcode)], t0);
5021 tcg_gen_andi_tl(t0, t0, MASK(mb, me));
5022 tcg_gen_andi_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rA(ctx->opcode)], ~MASK(mb, me));
5023 tcg_gen_or_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rA(ctx->opcode)], t0);
5024 tcg_temp_free(t0);
76a66253 5025 if (unlikely(Rc(ctx->opcode) != 0))
7487953d 5026 gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
76a66253
JM
5027}
5028
5029/* rrib - rrib. */
99e300ef 5030static void gen_rrib(DisasContext *ctx)
76a66253 5031{
7487953d
AJ
5032 TCGv t0 = tcg_temp_new();
5033 TCGv t1 = tcg_temp_new();
5034 tcg_gen_andi_tl(t0, cpu_gpr[rB(ctx->opcode)], 0x1F);
5035 tcg_gen_movi_tl(t1, 0x80000000);
5036 tcg_gen_shr_tl(t1, t1, t0);
5037 tcg_gen_shr_tl(t0, cpu_gpr[rS(ctx->opcode)], t0);
5038 tcg_gen_and_tl(t0, t0, t1);
5039 tcg_gen_andc_tl(t1, cpu_gpr[rA(ctx->opcode)], t1);
5040 tcg_gen_or_tl(cpu_gpr[rA(ctx->opcode)], t0, t1);
5041 tcg_temp_free(t0);
5042 tcg_temp_free(t1);
76a66253 5043 if (unlikely(Rc(ctx->opcode) != 0))
7487953d 5044 gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
76a66253
JM
5045}
5046
5047/* sle - sle. */
99e300ef 5048static void gen_sle(DisasContext *ctx)
76a66253 5049{
7487953d
AJ
5050 TCGv t0 = tcg_temp_new();
5051 TCGv t1 = tcg_temp_new();
5052 tcg_gen_andi_tl(t1, cpu_gpr[rB(ctx->opcode)], 0x1F);
5053 tcg_gen_shl_tl(t0, cpu_gpr[rS(ctx->opcode)], t1);
5054 tcg_gen_subfi_tl(t1, 32, t1);
5055 tcg_gen_shr_tl(t1, cpu_gpr[rS(ctx->opcode)], t1);
5056 tcg_gen_or_tl(t1, t0, t1);
5057 tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], t0);
5058 gen_store_spr(SPR_MQ, t1);
5059 tcg_temp_free(t0);
5060 tcg_temp_free(t1);
76a66253 5061 if (unlikely(Rc(ctx->opcode) != 0))
7487953d 5062 gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
76a66253
JM
5063}
5064
5065/* sleq - sleq. */
99e300ef 5066static void gen_sleq(DisasContext *ctx)
76a66253 5067{
7487953d
AJ
5068 TCGv t0 = tcg_temp_new();
5069 TCGv t1 = tcg_temp_new();
5070 TCGv t2 = tcg_temp_new();
5071 tcg_gen_andi_tl(t0, cpu_gpr[rB(ctx->opcode)], 0x1F);
5072 tcg_gen_movi_tl(t2, 0xFFFFFFFF);
5073 tcg_gen_shl_tl(t2, t2, t0);
5074 tcg_gen_rotl_tl(t0, cpu_gpr[rS(ctx->opcode)], t0);
5075 gen_load_spr(t1, SPR_MQ);
5076 gen_store_spr(SPR_MQ, t0);
5077 tcg_gen_and_tl(t0, t0, t2);
5078 tcg_gen_andc_tl(t1, t1, t2);
5079 tcg_gen_or_tl(cpu_gpr[rA(ctx->opcode)], t0, t1);
5080 tcg_temp_free(t0);
5081 tcg_temp_free(t1);
5082 tcg_temp_free(t2);
76a66253 5083 if (unlikely(Rc(ctx->opcode) != 0))
7487953d 5084 gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
76a66253
JM
5085}
5086
5087/* sliq - sliq. */
99e300ef 5088static void gen_sliq(DisasContext *ctx)
76a66253 5089{
7487953d
AJ
5090 int sh = SH(ctx->opcode);
5091 TCGv t0 = tcg_temp_new();
5092 TCGv t1 = tcg_temp_new();
5093 tcg_gen_shli_tl(t0, cpu_gpr[rS(ctx->opcode)], sh);
5094 tcg_gen_shri_tl(t1, cpu_gpr[rS(ctx->opcode)], 32 - sh);
5095 tcg_gen_or_tl(t1, t0, t1);
5096 tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], t0);
5097 gen_store_spr(SPR_MQ, t1);
5098 tcg_temp_free(t0);
5099 tcg_temp_free(t1);
76a66253 5100 if (unlikely(Rc(ctx->opcode) != 0))
7487953d 5101 gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
76a66253
JM
5102}
5103
5104/* slliq - slliq. */
99e300ef 5105static void gen_slliq(DisasContext *ctx)
76a66253 5106{
7487953d
AJ
5107 int sh = SH(ctx->opcode);
5108 TCGv t0 = tcg_temp_new();
5109 TCGv t1 = tcg_temp_new();
5110 tcg_gen_rotli_tl(t0, cpu_gpr[rS(ctx->opcode)], sh);
5111 gen_load_spr(t1, SPR_MQ);
5112 gen_store_spr(SPR_MQ, t0);
5113 tcg_gen_andi_tl(t0, t0, (0xFFFFFFFFU << sh));
5114 tcg_gen_andi_tl(t1, t1, ~(0xFFFFFFFFU << sh));
5115 tcg_gen_or_tl(cpu_gpr[rA(ctx->opcode)], t0, t1);
5116 tcg_temp_free(t0);
5117 tcg_temp_free(t1);
76a66253 5118 if (unlikely(Rc(ctx->opcode) != 0))
7487953d 5119 gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
76a66253
JM
5120}
5121
5122/* sllq - sllq. */
99e300ef 5123static void gen_sllq(DisasContext *ctx)
76a66253 5124{
7487953d
AJ
5125 int l1 = gen_new_label();
5126 int l2 = gen_new_label();
5127 TCGv t0 = tcg_temp_local_new();
5128 TCGv t1 = tcg_temp_local_new();
5129 TCGv t2 = tcg_temp_local_new();
5130 tcg_gen_andi_tl(t2, cpu_gpr[rB(ctx->opcode)], 0x1F);
5131 tcg_gen_movi_tl(t1, 0xFFFFFFFF);
5132 tcg_gen_shl_tl(t1, t1, t2);
5133 tcg_gen_andi_tl(t0, cpu_gpr[rB(ctx->opcode)], 0x20);
5134 tcg_gen_brcondi_tl(TCG_COND_EQ, t0, 0, l1);
5135 gen_load_spr(t0, SPR_MQ);
5136 tcg_gen_and_tl(cpu_gpr[rA(ctx->opcode)], t0, t1);
5137 tcg_gen_br(l2);
5138 gen_set_label(l1);
5139 tcg_gen_shl_tl(t0, cpu_gpr[rS(ctx->opcode)], t2);
5140 gen_load_spr(t2, SPR_MQ);
5141 tcg_gen_andc_tl(t1, t2, t1);
5142 tcg_gen_or_tl(cpu_gpr[rA(ctx->opcode)], t0, t1);
5143 gen_set_label(l2);
5144 tcg_temp_free(t0);
5145 tcg_temp_free(t1);
5146 tcg_temp_free(t2);
76a66253 5147 if (unlikely(Rc(ctx->opcode) != 0))
7487953d 5148 gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
76a66253
JM
5149}
5150
5151/* slq - slq. */
99e300ef 5152static void gen_slq(DisasContext *ctx)
76a66253 5153{
7487953d
AJ
5154 int l1 = gen_new_label();
5155 TCGv t0 = tcg_temp_new();
5156 TCGv t1 = tcg_temp_new();
5157 tcg_gen_andi_tl(t1, cpu_gpr[rB(ctx->opcode)], 0x1F);
5158 tcg_gen_shl_tl(t0, cpu_gpr[rS(ctx->opcode)], t1);
5159 tcg_gen_subfi_tl(t1, 32, t1);
5160 tcg_gen_shr_tl(t1, cpu_gpr[rS(ctx->opcode)], t1);
5161 tcg_gen_or_tl(t1, t0, t1);
5162 gen_store_spr(SPR_MQ, t1);
5163 tcg_gen_andi_tl(t1, cpu_gpr[rB(ctx->opcode)], 0x20);
5164 tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], t0);
5165 tcg_gen_brcondi_tl(TCG_COND_EQ, t1, 0, l1);
5166 tcg_gen_movi_tl(cpu_gpr[rA(ctx->opcode)], 0);
5167 gen_set_label(l1);
5168 tcg_temp_free(t0);
5169 tcg_temp_free(t1);
76a66253 5170 if (unlikely(Rc(ctx->opcode) != 0))
7487953d 5171 gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
76a66253
JM
5172}
5173
d9bce9d9 5174/* sraiq - sraiq. */
99e300ef 5175static void gen_sraiq(DisasContext *ctx)
76a66253 5176{
7487953d
AJ
5177 int sh = SH(ctx->opcode);
5178 int l1 = gen_new_label();
5179 TCGv t0 = tcg_temp_new();
5180 TCGv t1 = tcg_temp_new();
5181 tcg_gen_shri_tl(t0, cpu_gpr[rS(ctx->opcode)], sh);
5182 tcg_gen_shli_tl(t1, cpu_gpr[rS(ctx->opcode)], 32 - sh);
5183 tcg_gen_or_tl(t0, t0, t1);
5184 gen_store_spr(SPR_MQ, t0);
da91a00f 5185 tcg_gen_movi_tl(cpu_ca, 0);
7487953d
AJ
5186 tcg_gen_brcondi_tl(TCG_COND_EQ, t1, 0, l1);
5187 tcg_gen_brcondi_tl(TCG_COND_GE, cpu_gpr[rS(ctx->opcode)], 0, l1);
da91a00f 5188 tcg_gen_movi_tl(cpu_ca, 1);
7487953d
AJ
5189 gen_set_label(l1);
5190 tcg_gen_sari_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)], sh);
5191 tcg_temp_free(t0);
5192 tcg_temp_free(t1);
76a66253 5193 if (unlikely(Rc(ctx->opcode) != 0))
7487953d 5194 gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
76a66253
JM
5195}
5196
5197/* sraq - sraq. */
99e300ef 5198static void gen_sraq(DisasContext *ctx)
76a66253 5199{
7487953d
AJ
5200 int l1 = gen_new_label();
5201 int l2 = gen_new_label();
5202 TCGv t0 = tcg_temp_new();
5203 TCGv t1 = tcg_temp_local_new();
5204 TCGv t2 = tcg_temp_local_new();
5205 tcg_gen_andi_tl(t2, cpu_gpr[rB(ctx->opcode)], 0x1F);
5206 tcg_gen_shr_tl(t0, cpu_gpr[rS(ctx->opcode)], t2);
5207 tcg_gen_sar_tl(t1, cpu_gpr[rS(ctx->opcode)], t2);
5208 tcg_gen_subfi_tl(t2, 32, t2);
5209 tcg_gen_shl_tl(t2, cpu_gpr[rS(ctx->opcode)], t2);
5210 tcg_gen_or_tl(t0, t0, t2);
5211 gen_store_spr(SPR_MQ, t0);
5212 tcg_gen_andi_tl(t0, cpu_gpr[rB(ctx->opcode)], 0x20);
5213 tcg_gen_brcondi_tl(TCG_COND_EQ, t2, 0, l1);
5214 tcg_gen_mov_tl(t2, cpu_gpr[rS(ctx->opcode)]);
5215 tcg_gen_sari_tl(t1, cpu_gpr[rS(ctx->opcode)], 31);
5216 gen_set_label(l1);
5217 tcg_temp_free(t0);
5218 tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], t1);
da91a00f 5219 tcg_gen_movi_tl(cpu_ca, 0);
7487953d
AJ
5220 tcg_gen_brcondi_tl(TCG_COND_GE, t1, 0, l2);
5221 tcg_gen_brcondi_tl(TCG_COND_EQ, t2, 0, l2);
da91a00f 5222 tcg_gen_movi_tl(cpu_ca, 1);
7487953d
AJ
5223 gen_set_label(l2);
5224 tcg_temp_free(t1);
5225 tcg_temp_free(t2);
76a66253 5226 if (unlikely(Rc(ctx->opcode) != 0))
7487953d 5227 gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
76a66253
JM
5228}
5229
5230/* sre - sre. */
99e300ef 5231static void gen_sre(DisasContext *ctx)
76a66253 5232{
7487953d
AJ
5233 TCGv t0 = tcg_temp_new();
5234 TCGv t1 = tcg_temp_new();
5235 tcg_gen_andi_tl(t1, cpu_gpr[rB(ctx->opcode)], 0x1F);
5236 tcg_gen_shr_tl(t0, cpu_gpr[rS(ctx->opcode)], t1);
5237 tcg_gen_subfi_tl(t1, 32, t1);
5238 tcg_gen_shl_tl(t1, cpu_gpr[rS(ctx->opcode)], t1);
5239 tcg_gen_or_tl(t1, t0, t1);
5240 tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], t0);
5241 gen_store_spr(SPR_MQ, t1);
5242 tcg_temp_free(t0);
5243 tcg_temp_free(t1);
76a66253 5244 if (unlikely(Rc(ctx->opcode) != 0))
7487953d 5245 gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
76a66253
JM
5246}
5247
5248/* srea - srea. */
99e300ef 5249static void gen_srea(DisasContext *ctx)
76a66253 5250{
7487953d
AJ
5251 TCGv t0 = tcg_temp_new();
5252 TCGv t1 = tcg_temp_new();
5253 tcg_gen_andi_tl(t1, cpu_gpr[rB(ctx->opcode)], 0x1F);
5254 tcg_gen_rotr_tl(t0, cpu_gpr[rS(ctx->opcode)], t1);
5255 gen_store_spr(SPR_MQ, t0);
5256 tcg_gen_sar_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)], t1);
5257 tcg_temp_free(t0);
5258 tcg_temp_free(t1);
76a66253 5259 if (unlikely(Rc(ctx->opcode) != 0))
7487953d 5260 gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
76a66253
JM
5261}
5262
5263/* sreq */
99e300ef 5264static void gen_sreq(DisasContext *ctx)
76a66253 5265{
7487953d
AJ
5266 TCGv t0 = tcg_temp_new();
5267 TCGv t1 = tcg_temp_new();
5268 TCGv t2 = tcg_temp_new();
5269 tcg_gen_andi_tl(t0, cpu_gpr[rB(ctx->opcode)], 0x1F);
5270 tcg_gen_movi_tl(t1, 0xFFFFFFFF);
5271 tcg_gen_shr_tl(t1, t1, t0);
5272 tcg_gen_rotr_tl(t0, cpu_gpr[rS(ctx->opcode)], t0);
5273 gen_load_spr(t2, SPR_MQ);
5274 gen_store_spr(SPR_MQ, t0);
5275 tcg_gen_and_tl(t0, t0, t1);
5276 tcg_gen_andc_tl(t2, t2, t1);
5277 tcg_gen_or_tl(cpu_gpr[rA(ctx->opcode)], t0, t2);
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/* sriq */
99e300ef 5286static void gen_sriq(DisasContext *ctx)
76a66253 5287{
7487953d
AJ
5288 int sh = SH(ctx->opcode);
5289 TCGv t0 = tcg_temp_new();
5290 TCGv t1 = tcg_temp_new();
5291 tcg_gen_shri_tl(t0, cpu_gpr[rS(ctx->opcode)], sh);
5292 tcg_gen_shli_tl(t1, cpu_gpr[rS(ctx->opcode)], 32 - sh);
5293 tcg_gen_or_tl(t1, t0, t1);
5294 tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], t0);
5295 gen_store_spr(SPR_MQ, t1);
5296 tcg_temp_free(t0);
5297 tcg_temp_free(t1);
76a66253 5298 if (unlikely(Rc(ctx->opcode) != 0))
7487953d 5299 gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
76a66253
JM
5300}
5301
5302/* srliq */
99e300ef 5303static void gen_srliq(DisasContext *ctx)
76a66253 5304{
7487953d
AJ
5305 int sh = SH(ctx->opcode);
5306 TCGv t0 = tcg_temp_new();
5307 TCGv t1 = tcg_temp_new();
5308 tcg_gen_rotri_tl(t0, cpu_gpr[rS(ctx->opcode)], sh);
5309 gen_load_spr(t1, SPR_MQ);
5310 gen_store_spr(SPR_MQ, t0);
5311 tcg_gen_andi_tl(t0, t0, (0xFFFFFFFFU >> sh));
5312 tcg_gen_andi_tl(t1, t1, ~(0xFFFFFFFFU >> sh));
5313 tcg_gen_or_tl(cpu_gpr[rA(ctx->opcode)], t0, t1);
5314 tcg_temp_free(t0);
5315 tcg_temp_free(t1);
76a66253 5316 if (unlikely(Rc(ctx->opcode) != 0))
7487953d 5317 gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
76a66253
JM
5318}
5319
5320/* srlq */
99e300ef 5321static void gen_srlq(DisasContext *ctx)
76a66253 5322{
7487953d
AJ
5323 int l1 = gen_new_label();
5324 int l2 = gen_new_label();
5325 TCGv t0 = tcg_temp_local_new();
5326 TCGv t1 = tcg_temp_local_new();
5327 TCGv t2 = tcg_temp_local_new();
5328 tcg_gen_andi_tl(t2, cpu_gpr[rB(ctx->opcode)], 0x1F);
5329 tcg_gen_movi_tl(t1, 0xFFFFFFFF);
5330 tcg_gen_shr_tl(t2, t1, t2);
5331 tcg_gen_andi_tl(t0, cpu_gpr[rB(ctx->opcode)], 0x20);
5332 tcg_gen_brcondi_tl(TCG_COND_EQ, t0, 0, l1);
5333 gen_load_spr(t0, SPR_MQ);
5334 tcg_gen_and_tl(cpu_gpr[rA(ctx->opcode)], t0, t2);
5335 tcg_gen_br(l2);
5336 gen_set_label(l1);
5337 tcg_gen_shr_tl(t0, cpu_gpr[rS(ctx->opcode)], t2);
5338 tcg_gen_and_tl(t0, t0, t2);
5339 gen_load_spr(t1, SPR_MQ);
5340 tcg_gen_andc_tl(t1, t1, t2);
5341 tcg_gen_or_tl(cpu_gpr[rA(ctx->opcode)], t0, t1);
5342 gen_set_label(l2);
5343 tcg_temp_free(t0);
5344 tcg_temp_free(t1);
5345 tcg_temp_free(t2);
76a66253 5346 if (unlikely(Rc(ctx->opcode) != 0))
7487953d 5347 gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
76a66253
JM
5348}
5349
5350/* srq */
99e300ef 5351static void gen_srq(DisasContext *ctx)
76a66253 5352{
7487953d
AJ
5353 int l1 = gen_new_label();
5354 TCGv t0 = tcg_temp_new();
5355 TCGv t1 = tcg_temp_new();
5356 tcg_gen_andi_tl(t1, cpu_gpr[rB(ctx->opcode)], 0x1F);
5357 tcg_gen_shr_tl(t0, cpu_gpr[rS(ctx->opcode)], t1);
5358 tcg_gen_subfi_tl(t1, 32, t1);
5359 tcg_gen_shl_tl(t1, cpu_gpr[rS(ctx->opcode)], t1);
5360 tcg_gen_or_tl(t1, t0, t1);
5361 gen_store_spr(SPR_MQ, t1);
5362 tcg_gen_andi_tl(t1, cpu_gpr[rB(ctx->opcode)], 0x20);
5363 tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], t0);
5364 tcg_gen_brcondi_tl(TCG_COND_EQ, t0, 0, l1);
5365 tcg_gen_movi_tl(cpu_gpr[rA(ctx->opcode)], 0);
5366 gen_set_label(l1);
5367 tcg_temp_free(t0);
5368 tcg_temp_free(t1);
76a66253 5369 if (unlikely(Rc(ctx->opcode) != 0))
7487953d 5370 gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
76a66253
JM
5371}
5372
5373/* PowerPC 602 specific instructions */
99e300ef 5374
54623277 5375/* dsa */
99e300ef 5376static void gen_dsa(DisasContext *ctx)
76a66253
JM
5377{
5378 /* XXX: TODO */
e06fcd75 5379 gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL);
76a66253
JM
5380}
5381
5382/* esa */
99e300ef 5383static void gen_esa(DisasContext *ctx)
76a66253
JM
5384{
5385 /* XXX: TODO */
e06fcd75 5386 gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL);
76a66253
JM
5387}
5388
5389/* mfrom */
99e300ef 5390static void gen_mfrom(DisasContext *ctx)
76a66253
JM
5391{
5392#if defined(CONFIG_USER_ONLY)
e06fcd75 5393 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
76a66253 5394#else
76db3ba4 5395 if (unlikely(!ctx->mem_idx)) {
e06fcd75 5396 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
76a66253
JM
5397 return;
5398 }
cf02a65c 5399 gen_helper_602_mfrom(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)]);
76a66253
JM
5400#endif
5401}
5402
5403/* 602 - 603 - G2 TLB management */
e8eaa2c0 5404
54623277 5405/* tlbld */
e8eaa2c0 5406static void gen_tlbld_6xx(DisasContext *ctx)
76a66253
JM
5407{
5408#if defined(CONFIG_USER_ONLY)
e06fcd75 5409 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
76a66253 5410#else
76db3ba4 5411 if (unlikely(!ctx->mem_idx)) {
e06fcd75 5412 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
76a66253
JM
5413 return;
5414 }
c6c7cf05 5415 gen_helper_6xx_tlbd(cpu_env, cpu_gpr[rB(ctx->opcode)]);
76a66253
JM
5416#endif
5417}
5418
5419/* tlbli */
e8eaa2c0 5420static void gen_tlbli_6xx(DisasContext *ctx)
76a66253
JM
5421{
5422#if defined(CONFIG_USER_ONLY)
e06fcd75 5423 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
76a66253 5424#else
76db3ba4 5425 if (unlikely(!ctx->mem_idx)) {
e06fcd75 5426 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
76a66253
JM
5427 return;
5428 }
c6c7cf05 5429 gen_helper_6xx_tlbi(cpu_env, cpu_gpr[rB(ctx->opcode)]);
76a66253
JM
5430#endif
5431}
5432
7dbe11ac 5433/* 74xx TLB management */
e8eaa2c0 5434
54623277 5435/* tlbld */
e8eaa2c0 5436static void gen_tlbld_74xx(DisasContext *ctx)
7dbe11ac
JM
5437{
5438#if defined(CONFIG_USER_ONLY)
e06fcd75 5439 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
7dbe11ac 5440#else
76db3ba4 5441 if (unlikely(!ctx->mem_idx)) {
e06fcd75 5442 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
7dbe11ac
JM
5443 return;
5444 }
c6c7cf05 5445 gen_helper_74xx_tlbd(cpu_env, cpu_gpr[rB(ctx->opcode)]);
7dbe11ac
JM
5446#endif
5447}
5448
5449/* tlbli */
e8eaa2c0 5450static void gen_tlbli_74xx(DisasContext *ctx)
7dbe11ac
JM
5451{
5452#if defined(CONFIG_USER_ONLY)
e06fcd75 5453 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
7dbe11ac 5454#else
76db3ba4 5455 if (unlikely(!ctx->mem_idx)) {
e06fcd75 5456 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
7dbe11ac
JM
5457 return;
5458 }
c6c7cf05 5459 gen_helper_74xx_tlbi(cpu_env, cpu_gpr[rB(ctx->opcode)]);
7dbe11ac
JM
5460#endif
5461}
5462
76a66253 5463/* POWER instructions not in PowerPC 601 */
99e300ef 5464
54623277 5465/* clf */
99e300ef 5466static void gen_clf(DisasContext *ctx)
76a66253
JM
5467{
5468 /* Cache line flush: implemented as no-op */
5469}
5470
5471/* cli */
99e300ef 5472static void gen_cli(DisasContext *ctx)
76a66253 5473{
7f75ffd3 5474 /* Cache line invalidate: privileged and treated as no-op */
76a66253 5475#if defined(CONFIG_USER_ONLY)
e06fcd75 5476 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
76a66253 5477#else
76db3ba4 5478 if (unlikely(!ctx->mem_idx)) {
e06fcd75 5479 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
76a66253
JM
5480 return;
5481 }
5482#endif
5483}
5484
5485/* dclst */
99e300ef 5486static void gen_dclst(DisasContext *ctx)
76a66253
JM
5487{
5488 /* Data cache line store: treated as no-op */
5489}
5490
99e300ef 5491static void gen_mfsri(DisasContext *ctx)
76a66253
JM
5492{
5493#if defined(CONFIG_USER_ONLY)
e06fcd75 5494 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
76a66253 5495#else
74d37793
AJ
5496 int ra = rA(ctx->opcode);
5497 int rd = rD(ctx->opcode);
5498 TCGv t0;
76db3ba4 5499 if (unlikely(!ctx->mem_idx)) {
e06fcd75 5500 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
76a66253
JM
5501 return;
5502 }
74d37793 5503 t0 = tcg_temp_new();
76db3ba4 5504 gen_addr_reg_index(ctx, t0);
74d37793
AJ
5505 tcg_gen_shri_tl(t0, t0, 28);
5506 tcg_gen_andi_tl(t0, t0, 0xF);
c6c7cf05 5507 gen_helper_load_sr(cpu_gpr[rd], cpu_env, t0);
74d37793 5508 tcg_temp_free(t0);
76a66253 5509 if (ra != 0 && ra != rd)
74d37793 5510 tcg_gen_mov_tl(cpu_gpr[ra], cpu_gpr[rd]);
76a66253
JM
5511#endif
5512}
5513
99e300ef 5514static void gen_rac(DisasContext *ctx)
76a66253
JM
5515{
5516#if defined(CONFIG_USER_ONLY)
e06fcd75 5517 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
76a66253 5518#else
22e0e173 5519 TCGv t0;
76db3ba4 5520 if (unlikely(!ctx->mem_idx)) {
e06fcd75 5521 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
76a66253
JM
5522 return;
5523 }
22e0e173 5524 t0 = tcg_temp_new();
76db3ba4 5525 gen_addr_reg_index(ctx, t0);
c6c7cf05 5526 gen_helper_rac(cpu_gpr[rD(ctx->opcode)], cpu_env, t0);
22e0e173 5527 tcg_temp_free(t0);
76a66253
JM
5528#endif
5529}
5530
99e300ef 5531static void gen_rfsvc(DisasContext *ctx)
76a66253
JM
5532{
5533#if defined(CONFIG_USER_ONLY)
e06fcd75 5534 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
76a66253 5535#else
76db3ba4 5536 if (unlikely(!ctx->mem_idx)) {
e06fcd75 5537 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
76a66253
JM
5538 return;
5539 }
e5f17ac6 5540 gen_helper_rfsvc(cpu_env);
e06fcd75 5541 gen_sync_exception(ctx);
76a66253
JM
5542#endif
5543}
5544
5545/* svc is not implemented for now */
5546
5547/* POWER2 specific instructions */
5548/* Quad manipulation (load/store two floats at a time) */
76a66253
JM
5549
5550/* lfq */
99e300ef 5551static void gen_lfq(DisasContext *ctx)
76a66253 5552{
01a4afeb 5553 int rd = rD(ctx->opcode);
76db3ba4
AJ
5554 TCGv t0;
5555 gen_set_access_type(ctx, ACCESS_FLOAT);
5556 t0 = tcg_temp_new();
5557 gen_addr_imm_index(ctx, t0, 0);
5558 gen_qemu_ld64(ctx, cpu_fpr[rd], t0);
5559 gen_addr_add(ctx, t0, t0, 8);
5560 gen_qemu_ld64(ctx, cpu_fpr[(rd + 1) % 32], t0);
01a4afeb 5561 tcg_temp_free(t0);
76a66253
JM
5562}
5563
5564/* lfqu */
99e300ef 5565static void gen_lfqu(DisasContext *ctx)
76a66253
JM
5566{
5567 int ra = rA(ctx->opcode);
01a4afeb 5568 int rd = rD(ctx->opcode);
76db3ba4
AJ
5569 TCGv t0, t1;
5570 gen_set_access_type(ctx, ACCESS_FLOAT);
5571 t0 = tcg_temp_new();
5572 t1 = tcg_temp_new();
5573 gen_addr_imm_index(ctx, t0, 0);
5574 gen_qemu_ld64(ctx, cpu_fpr[rd], t0);
5575 gen_addr_add(ctx, t1, t0, 8);
5576 gen_qemu_ld64(ctx, cpu_fpr[(rd + 1) % 32], t1);
76a66253 5577 if (ra != 0)
01a4afeb
AJ
5578 tcg_gen_mov_tl(cpu_gpr[ra], t0);
5579 tcg_temp_free(t0);
5580 tcg_temp_free(t1);
76a66253
JM
5581}
5582
5583/* lfqux */
99e300ef 5584static void gen_lfqux(DisasContext *ctx)
76a66253
JM
5585{
5586 int ra = rA(ctx->opcode);
01a4afeb 5587 int rd = rD(ctx->opcode);
76db3ba4
AJ
5588 gen_set_access_type(ctx, ACCESS_FLOAT);
5589 TCGv t0, t1;
5590 t0 = tcg_temp_new();
5591 gen_addr_reg_index(ctx, t0);
5592 gen_qemu_ld64(ctx, cpu_fpr[rd], t0);
5593 t1 = tcg_temp_new();
5594 gen_addr_add(ctx, t1, t0, 8);
5595 gen_qemu_ld64(ctx, cpu_fpr[(rd + 1) % 32], t1);
5596 tcg_temp_free(t1);
76a66253 5597 if (ra != 0)
01a4afeb
AJ
5598 tcg_gen_mov_tl(cpu_gpr[ra], t0);
5599 tcg_temp_free(t0);
76a66253
JM
5600}
5601
5602/* lfqx */
99e300ef 5603static void gen_lfqx(DisasContext *ctx)
76a66253 5604{
01a4afeb 5605 int rd = rD(ctx->opcode);
76db3ba4
AJ
5606 TCGv t0;
5607 gen_set_access_type(ctx, ACCESS_FLOAT);
5608 t0 = tcg_temp_new();
5609 gen_addr_reg_index(ctx, t0);
5610 gen_qemu_ld64(ctx, cpu_fpr[rd], t0);
5611 gen_addr_add(ctx, t0, t0, 8);
5612 gen_qemu_ld64(ctx, cpu_fpr[(rd + 1) % 32], t0);
01a4afeb 5613 tcg_temp_free(t0);
76a66253
JM
5614}
5615
5616/* stfq */
99e300ef 5617static void gen_stfq(DisasContext *ctx)
76a66253 5618{
01a4afeb 5619 int rd = rD(ctx->opcode);
76db3ba4
AJ
5620 TCGv t0;
5621 gen_set_access_type(ctx, ACCESS_FLOAT);
5622 t0 = tcg_temp_new();
5623 gen_addr_imm_index(ctx, t0, 0);
5624 gen_qemu_st64(ctx, cpu_fpr[rd], t0);
5625 gen_addr_add(ctx, t0, t0, 8);
5626 gen_qemu_st64(ctx, cpu_fpr[(rd + 1) % 32], t0);
01a4afeb 5627 tcg_temp_free(t0);
76a66253
JM
5628}
5629
5630/* stfqu */
99e300ef 5631static void gen_stfqu(DisasContext *ctx)
76a66253
JM
5632{
5633 int ra = rA(ctx->opcode);
01a4afeb 5634 int rd = rD(ctx->opcode);
76db3ba4
AJ
5635 TCGv t0, t1;
5636 gen_set_access_type(ctx, ACCESS_FLOAT);
5637 t0 = tcg_temp_new();
5638 gen_addr_imm_index(ctx, t0, 0);
5639 gen_qemu_st64(ctx, cpu_fpr[rd], t0);
5640 t1 = tcg_temp_new();
5641 gen_addr_add(ctx, t1, t0, 8);
5642 gen_qemu_st64(ctx, cpu_fpr[(rd + 1) % 32], t1);
5643 tcg_temp_free(t1);
76a66253 5644 if (ra != 0)
01a4afeb
AJ
5645 tcg_gen_mov_tl(cpu_gpr[ra], t0);
5646 tcg_temp_free(t0);
76a66253
JM
5647}
5648
5649/* stfqux */
99e300ef 5650static void gen_stfqux(DisasContext *ctx)
76a66253
JM
5651{
5652 int ra = rA(ctx->opcode);
01a4afeb 5653 int rd = rD(ctx->opcode);
76db3ba4
AJ
5654 TCGv t0, t1;
5655 gen_set_access_type(ctx, ACCESS_FLOAT);
5656 t0 = tcg_temp_new();
5657 gen_addr_reg_index(ctx, t0);
5658 gen_qemu_st64(ctx, cpu_fpr[rd], t0);
5659 t1 = tcg_temp_new();
5660 gen_addr_add(ctx, t1, t0, 8);
5661 gen_qemu_st64(ctx, cpu_fpr[(rd + 1) % 32], t1);
5662 tcg_temp_free(t1);
76a66253 5663 if (ra != 0)
01a4afeb
AJ
5664 tcg_gen_mov_tl(cpu_gpr[ra], t0);
5665 tcg_temp_free(t0);
76a66253
JM
5666}
5667
5668/* stfqx */
99e300ef 5669static void gen_stfqx(DisasContext *ctx)
76a66253 5670{
01a4afeb 5671 int rd = rD(ctx->opcode);
76db3ba4
AJ
5672 TCGv t0;
5673 gen_set_access_type(ctx, ACCESS_FLOAT);
5674 t0 = tcg_temp_new();
5675 gen_addr_reg_index(ctx, t0);
5676 gen_qemu_st64(ctx, cpu_fpr[rd], t0);
5677 gen_addr_add(ctx, t0, t0, 8);
5678 gen_qemu_st64(ctx, cpu_fpr[(rd + 1) % 32], t0);
01a4afeb 5679 tcg_temp_free(t0);
76a66253
JM
5680}
5681
5682/* BookE specific instructions */
99e300ef 5683
54623277 5684/* XXX: not implemented on 440 ? */
99e300ef 5685static void gen_mfapidi(DisasContext *ctx)
76a66253
JM
5686{
5687 /* XXX: TODO */
e06fcd75 5688 gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL);
76a66253
JM
5689}
5690
2662a059 5691/* XXX: not implemented on 440 ? */
99e300ef 5692static void gen_tlbiva(DisasContext *ctx)
76a66253
JM
5693{
5694#if defined(CONFIG_USER_ONLY)
e06fcd75 5695 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
76a66253 5696#else
74d37793 5697 TCGv t0;
76db3ba4 5698 if (unlikely(!ctx->mem_idx)) {
e06fcd75 5699 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
76a66253
JM
5700 return;
5701 }
ec72e276 5702 t0 = tcg_temp_new();
76db3ba4 5703 gen_addr_reg_index(ctx, t0);
c6c7cf05 5704 gen_helper_tlbie(cpu_env, cpu_gpr[rB(ctx->opcode)]);
74d37793 5705 tcg_temp_free(t0);
76a66253
JM
5706#endif
5707}
5708
5709/* All 405 MAC instructions are translated here */
636aa200
BS
5710static inline void gen_405_mulladd_insn(DisasContext *ctx, int opc2, int opc3,
5711 int ra, int rb, int rt, int Rc)
76a66253 5712{
182608d4
AJ
5713 TCGv t0, t1;
5714
a7812ae4
PB
5715 t0 = tcg_temp_local_new();
5716 t1 = tcg_temp_local_new();
182608d4 5717
76a66253
JM
5718 switch (opc3 & 0x0D) {
5719 case 0x05:
5720 /* macchw - macchw. - macchwo - macchwo. */
5721 /* macchws - macchws. - macchwso - macchwso. */
5722 /* nmacchw - nmacchw. - nmacchwo - nmacchwo. */
5723 /* nmacchws - nmacchws. - nmacchwso - nmacchwso. */
5724 /* mulchw - mulchw. */
182608d4
AJ
5725 tcg_gen_ext16s_tl(t0, cpu_gpr[ra]);
5726 tcg_gen_sari_tl(t1, cpu_gpr[rb], 16);
5727 tcg_gen_ext16s_tl(t1, t1);
76a66253
JM
5728 break;
5729 case 0x04:
5730 /* macchwu - macchwu. - macchwuo - macchwuo. */
5731 /* macchwsu - macchwsu. - macchwsuo - macchwsuo. */
5732 /* mulchwu - mulchwu. */
182608d4
AJ
5733 tcg_gen_ext16u_tl(t0, cpu_gpr[ra]);
5734 tcg_gen_shri_tl(t1, cpu_gpr[rb], 16);
5735 tcg_gen_ext16u_tl(t1, t1);
76a66253
JM
5736 break;
5737 case 0x01:
5738 /* machhw - machhw. - machhwo - machhwo. */
5739 /* machhws - machhws. - machhwso - machhwso. */
5740 /* nmachhw - nmachhw. - nmachhwo - nmachhwo. */
5741 /* nmachhws - nmachhws. - nmachhwso - nmachhwso. */
5742 /* mulhhw - mulhhw. */
182608d4
AJ
5743 tcg_gen_sari_tl(t0, cpu_gpr[ra], 16);
5744 tcg_gen_ext16s_tl(t0, t0);
5745 tcg_gen_sari_tl(t1, cpu_gpr[rb], 16);
5746 tcg_gen_ext16s_tl(t1, t1);
76a66253
JM
5747 break;
5748 case 0x00:
5749 /* machhwu - machhwu. - machhwuo - machhwuo. */
5750 /* machhwsu - machhwsu. - machhwsuo - machhwsuo. */
5751 /* mulhhwu - mulhhwu. */
182608d4
AJ
5752 tcg_gen_shri_tl(t0, cpu_gpr[ra], 16);
5753 tcg_gen_ext16u_tl(t0, t0);
5754 tcg_gen_shri_tl(t1, cpu_gpr[rb], 16);
5755 tcg_gen_ext16u_tl(t1, t1);
76a66253
JM
5756 break;
5757 case 0x0D:
5758 /* maclhw - maclhw. - maclhwo - maclhwo. */
5759 /* maclhws - maclhws. - maclhwso - maclhwso. */
5760 /* nmaclhw - nmaclhw. - nmaclhwo - nmaclhwo. */
5761 /* nmaclhws - nmaclhws. - nmaclhwso - nmaclhwso. */
5762 /* mullhw - mullhw. */
182608d4
AJ
5763 tcg_gen_ext16s_tl(t0, cpu_gpr[ra]);
5764 tcg_gen_ext16s_tl(t1, cpu_gpr[rb]);
76a66253
JM
5765 break;
5766 case 0x0C:
5767 /* maclhwu - maclhwu. - maclhwuo - maclhwuo. */
5768 /* maclhwsu - maclhwsu. - maclhwsuo - maclhwsuo. */
5769 /* mullhwu - mullhwu. */
182608d4
AJ
5770 tcg_gen_ext16u_tl(t0, cpu_gpr[ra]);
5771 tcg_gen_ext16u_tl(t1, cpu_gpr[rb]);
76a66253
JM
5772 break;
5773 }
76a66253 5774 if (opc2 & 0x04) {
182608d4
AJ
5775 /* (n)multiply-and-accumulate (0x0C / 0x0E) */
5776 tcg_gen_mul_tl(t1, t0, t1);
5777 if (opc2 & 0x02) {
5778 /* nmultiply-and-accumulate (0x0E) */
5779 tcg_gen_sub_tl(t0, cpu_gpr[rt], t1);
5780 } else {
5781 /* multiply-and-accumulate (0x0C) */
5782 tcg_gen_add_tl(t0, cpu_gpr[rt], t1);
5783 }
5784
5785 if (opc3 & 0x12) {
5786 /* Check overflow and/or saturate */
5787 int l1 = gen_new_label();
5788
5789 if (opc3 & 0x10) {
5790 /* Start with XER OV disabled, the most likely case */
da91a00f 5791 tcg_gen_movi_tl(cpu_ov, 0);
182608d4
AJ
5792 }
5793 if (opc3 & 0x01) {
5794 /* Signed */
5795 tcg_gen_xor_tl(t1, cpu_gpr[rt], t1);
5796 tcg_gen_brcondi_tl(TCG_COND_GE, t1, 0, l1);
5797 tcg_gen_xor_tl(t1, cpu_gpr[rt], t0);
5798 tcg_gen_brcondi_tl(TCG_COND_LT, t1, 0, l1);
bdc4e053 5799 if (opc3 & 0x02) {
182608d4
AJ
5800 /* Saturate */
5801 tcg_gen_sari_tl(t0, cpu_gpr[rt], 31);
5802 tcg_gen_xori_tl(t0, t0, 0x7fffffff);
5803 }
5804 } else {
5805 /* Unsigned */
5806 tcg_gen_brcond_tl(TCG_COND_GEU, t0, t1, l1);
bdc4e053 5807 if (opc3 & 0x02) {
182608d4
AJ
5808 /* Saturate */
5809 tcg_gen_movi_tl(t0, UINT32_MAX);
5810 }
5811 }
5812 if (opc3 & 0x10) {
5813 /* Check overflow */
da91a00f
RH
5814 tcg_gen_movi_tl(cpu_ov, 1);
5815 tcg_gen_movi_tl(cpu_so, 1);
182608d4
AJ
5816 }
5817 gen_set_label(l1);
5818 tcg_gen_mov_tl(cpu_gpr[rt], t0);
5819 }
5820 } else {
5821 tcg_gen_mul_tl(cpu_gpr[rt], t0, t1);
76a66253 5822 }
182608d4
AJ
5823 tcg_temp_free(t0);
5824 tcg_temp_free(t1);
76a66253
JM
5825 if (unlikely(Rc) != 0) {
5826 /* Update Rc0 */
182608d4 5827 gen_set_Rc0(ctx, cpu_gpr[rt]);
76a66253
JM
5828 }
5829}
5830
a750fc0b 5831#define GEN_MAC_HANDLER(name, opc2, opc3) \
99e300ef 5832static void glue(gen_, name)(DisasContext *ctx) \
76a66253
JM
5833{ \
5834 gen_405_mulladd_insn(ctx, opc2, opc3, rA(ctx->opcode), rB(ctx->opcode), \
5835 rD(ctx->opcode), Rc(ctx->opcode)); \
5836}
5837
5838/* macchw - macchw. */
a750fc0b 5839GEN_MAC_HANDLER(macchw, 0x0C, 0x05);
76a66253 5840/* macchwo - macchwo. */
a750fc0b 5841GEN_MAC_HANDLER(macchwo, 0x0C, 0x15);
76a66253 5842/* macchws - macchws. */
a750fc0b 5843GEN_MAC_HANDLER(macchws, 0x0C, 0x07);
76a66253 5844/* macchwso - macchwso. */
a750fc0b 5845GEN_MAC_HANDLER(macchwso, 0x0C, 0x17);
76a66253 5846/* macchwsu - macchwsu. */
a750fc0b 5847GEN_MAC_HANDLER(macchwsu, 0x0C, 0x06);
76a66253 5848/* macchwsuo - macchwsuo. */
a750fc0b 5849GEN_MAC_HANDLER(macchwsuo, 0x0C, 0x16);
76a66253 5850/* macchwu - macchwu. */
a750fc0b 5851GEN_MAC_HANDLER(macchwu, 0x0C, 0x04);
76a66253 5852/* macchwuo - macchwuo. */
a750fc0b 5853GEN_MAC_HANDLER(macchwuo, 0x0C, 0x14);
76a66253 5854/* machhw - machhw. */
a750fc0b 5855GEN_MAC_HANDLER(machhw, 0x0C, 0x01);
76a66253 5856/* machhwo - machhwo. */
a750fc0b 5857GEN_MAC_HANDLER(machhwo, 0x0C, 0x11);
76a66253 5858/* machhws - machhws. */
a750fc0b 5859GEN_MAC_HANDLER(machhws, 0x0C, 0x03);
76a66253 5860/* machhwso - machhwso. */
a750fc0b 5861GEN_MAC_HANDLER(machhwso, 0x0C, 0x13);
76a66253 5862/* machhwsu - machhwsu. */
a750fc0b 5863GEN_MAC_HANDLER(machhwsu, 0x0C, 0x02);
76a66253 5864/* machhwsuo - machhwsuo. */
a750fc0b 5865GEN_MAC_HANDLER(machhwsuo, 0x0C, 0x12);
76a66253 5866/* machhwu - machhwu. */
a750fc0b 5867GEN_MAC_HANDLER(machhwu, 0x0C, 0x00);
76a66253 5868/* machhwuo - machhwuo. */
a750fc0b 5869GEN_MAC_HANDLER(machhwuo, 0x0C, 0x10);
76a66253 5870/* maclhw - maclhw. */
a750fc0b 5871GEN_MAC_HANDLER(maclhw, 0x0C, 0x0D);
76a66253 5872/* maclhwo - maclhwo. */
a750fc0b 5873GEN_MAC_HANDLER(maclhwo, 0x0C, 0x1D);
76a66253 5874/* maclhws - maclhws. */
a750fc0b 5875GEN_MAC_HANDLER(maclhws, 0x0C, 0x0F);
76a66253 5876/* maclhwso - maclhwso. */
a750fc0b 5877GEN_MAC_HANDLER(maclhwso, 0x0C, 0x1F);
76a66253 5878/* maclhwu - maclhwu. */
a750fc0b 5879GEN_MAC_HANDLER(maclhwu, 0x0C, 0x0C);
76a66253 5880/* maclhwuo - maclhwuo. */
a750fc0b 5881GEN_MAC_HANDLER(maclhwuo, 0x0C, 0x1C);
76a66253 5882/* maclhwsu - maclhwsu. */
a750fc0b 5883GEN_MAC_HANDLER(maclhwsu, 0x0C, 0x0E);
76a66253 5884/* maclhwsuo - maclhwsuo. */
a750fc0b 5885GEN_MAC_HANDLER(maclhwsuo, 0x0C, 0x1E);
76a66253 5886/* nmacchw - nmacchw. */
a750fc0b 5887GEN_MAC_HANDLER(nmacchw, 0x0E, 0x05);
76a66253 5888/* nmacchwo - nmacchwo. */
a750fc0b 5889GEN_MAC_HANDLER(nmacchwo, 0x0E, 0x15);
76a66253 5890/* nmacchws - nmacchws. */
a750fc0b 5891GEN_MAC_HANDLER(nmacchws, 0x0E, 0x07);
76a66253 5892/* nmacchwso - nmacchwso. */
a750fc0b 5893GEN_MAC_HANDLER(nmacchwso, 0x0E, 0x17);
76a66253 5894/* nmachhw - nmachhw. */
a750fc0b 5895GEN_MAC_HANDLER(nmachhw, 0x0E, 0x01);
76a66253 5896/* nmachhwo - nmachhwo. */
a750fc0b 5897GEN_MAC_HANDLER(nmachhwo, 0x0E, 0x11);
76a66253 5898/* nmachhws - nmachhws. */
a750fc0b 5899GEN_MAC_HANDLER(nmachhws, 0x0E, 0x03);
76a66253 5900/* nmachhwso - nmachhwso. */
a750fc0b 5901GEN_MAC_HANDLER(nmachhwso, 0x0E, 0x13);
76a66253 5902/* nmaclhw - nmaclhw. */
a750fc0b 5903GEN_MAC_HANDLER(nmaclhw, 0x0E, 0x0D);
76a66253 5904/* nmaclhwo - nmaclhwo. */
a750fc0b 5905GEN_MAC_HANDLER(nmaclhwo, 0x0E, 0x1D);
76a66253 5906/* nmaclhws - nmaclhws. */
a750fc0b 5907GEN_MAC_HANDLER(nmaclhws, 0x0E, 0x0F);
76a66253 5908/* nmaclhwso - nmaclhwso. */
a750fc0b 5909GEN_MAC_HANDLER(nmaclhwso, 0x0E, 0x1F);
76a66253
JM
5910
5911/* mulchw - mulchw. */
a750fc0b 5912GEN_MAC_HANDLER(mulchw, 0x08, 0x05);
76a66253 5913/* mulchwu - mulchwu. */
a750fc0b 5914GEN_MAC_HANDLER(mulchwu, 0x08, 0x04);
76a66253 5915/* mulhhw - mulhhw. */
a750fc0b 5916GEN_MAC_HANDLER(mulhhw, 0x08, 0x01);
76a66253 5917/* mulhhwu - mulhhwu. */
a750fc0b 5918GEN_MAC_HANDLER(mulhhwu, 0x08, 0x00);
76a66253 5919/* mullhw - mullhw. */
a750fc0b 5920GEN_MAC_HANDLER(mullhw, 0x08, 0x0D);
76a66253 5921/* mullhwu - mullhwu. */
a750fc0b 5922GEN_MAC_HANDLER(mullhwu, 0x08, 0x0C);
76a66253
JM
5923
5924/* mfdcr */
99e300ef 5925static void gen_mfdcr(DisasContext *ctx)
76a66253
JM
5926{
5927#if defined(CONFIG_USER_ONLY)
e06fcd75 5928 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
76a66253 5929#else
06dca6a7 5930 TCGv dcrn;
76db3ba4 5931 if (unlikely(!ctx->mem_idx)) {
e06fcd75 5932 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
76a66253
JM
5933 return;
5934 }
06dca6a7
AJ
5935 /* NIP cannot be restored if the memory exception comes from an helper */
5936 gen_update_nip(ctx, ctx->nip - 4);
5937 dcrn = tcg_const_tl(SPR(ctx->opcode));
d0f1562d 5938 gen_helper_load_dcr(cpu_gpr[rD(ctx->opcode)], cpu_env, dcrn);
06dca6a7 5939 tcg_temp_free(dcrn);
76a66253
JM
5940#endif
5941}
5942
5943/* mtdcr */
99e300ef 5944static void gen_mtdcr(DisasContext *ctx)
76a66253
JM
5945{
5946#if defined(CONFIG_USER_ONLY)
e06fcd75 5947 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
76a66253 5948#else
06dca6a7 5949 TCGv dcrn;
76db3ba4 5950 if (unlikely(!ctx->mem_idx)) {
e06fcd75 5951 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
76a66253
JM
5952 return;
5953 }
06dca6a7
AJ
5954 /* NIP cannot be restored if the memory exception comes from an helper */
5955 gen_update_nip(ctx, ctx->nip - 4);
5956 dcrn = tcg_const_tl(SPR(ctx->opcode));
d0f1562d 5957 gen_helper_store_dcr(cpu_env, dcrn, cpu_gpr[rS(ctx->opcode)]);
06dca6a7 5958 tcg_temp_free(dcrn);
a42bd6cc
JM
5959#endif
5960}
5961
5962/* mfdcrx */
2662a059 5963/* XXX: not implemented on 440 ? */
99e300ef 5964static void gen_mfdcrx(DisasContext *ctx)
a42bd6cc
JM
5965{
5966#if defined(CONFIG_USER_ONLY)
e06fcd75 5967 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
a42bd6cc 5968#else
76db3ba4 5969 if (unlikely(!ctx->mem_idx)) {
e06fcd75 5970 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
a42bd6cc
JM
5971 return;
5972 }
06dca6a7
AJ
5973 /* NIP cannot be restored if the memory exception comes from an helper */
5974 gen_update_nip(ctx, ctx->nip - 4);
d0f1562d
BS
5975 gen_helper_load_dcr(cpu_gpr[rD(ctx->opcode)], cpu_env,
5976 cpu_gpr[rA(ctx->opcode)]);
a750fc0b 5977 /* Note: Rc update flag set leads to undefined state of Rc0 */
a42bd6cc
JM
5978#endif
5979}
5980
5981/* mtdcrx */
2662a059 5982/* XXX: not implemented on 440 ? */
99e300ef 5983static void gen_mtdcrx(DisasContext *ctx)
a42bd6cc
JM
5984{
5985#if defined(CONFIG_USER_ONLY)
e06fcd75 5986 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
a42bd6cc 5987#else
76db3ba4 5988 if (unlikely(!ctx->mem_idx)) {
e06fcd75 5989 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
a42bd6cc
JM
5990 return;
5991 }
06dca6a7
AJ
5992 /* NIP cannot be restored if the memory exception comes from an helper */
5993 gen_update_nip(ctx, ctx->nip - 4);
d0f1562d
BS
5994 gen_helper_store_dcr(cpu_env, cpu_gpr[rA(ctx->opcode)],
5995 cpu_gpr[rS(ctx->opcode)]);
a750fc0b 5996 /* Note: Rc update flag set leads to undefined state of Rc0 */
76a66253
JM
5997#endif
5998}
5999
a750fc0b 6000/* mfdcrux (PPC 460) : user-mode access to DCR */
99e300ef 6001static void gen_mfdcrux(DisasContext *ctx)
a750fc0b 6002{
06dca6a7
AJ
6003 /* NIP cannot be restored if the memory exception comes from an helper */
6004 gen_update_nip(ctx, ctx->nip - 4);
d0f1562d
BS
6005 gen_helper_load_dcr(cpu_gpr[rD(ctx->opcode)], cpu_env,
6006 cpu_gpr[rA(ctx->opcode)]);
a750fc0b
JM
6007 /* Note: Rc update flag set leads to undefined state of Rc0 */
6008}
6009
6010/* mtdcrux (PPC 460) : user-mode access to DCR */
99e300ef 6011static void gen_mtdcrux(DisasContext *ctx)
a750fc0b 6012{
06dca6a7
AJ
6013 /* NIP cannot be restored if the memory exception comes from an helper */
6014 gen_update_nip(ctx, ctx->nip - 4);
975e5463 6015 gen_helper_store_dcr(cpu_env, cpu_gpr[rA(ctx->opcode)],
d0f1562d 6016 cpu_gpr[rS(ctx->opcode)]);
a750fc0b
JM
6017 /* Note: Rc update flag set leads to undefined state of Rc0 */
6018}
6019
76a66253 6020/* dccci */
99e300ef 6021static void gen_dccci(DisasContext *ctx)
76a66253
JM
6022{
6023#if defined(CONFIG_USER_ONLY)
e06fcd75 6024 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
76a66253 6025#else
76db3ba4 6026 if (unlikely(!ctx->mem_idx)) {
e06fcd75 6027 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
76a66253
JM
6028 return;
6029 }
6030 /* interpreted as no-op */
6031#endif
6032}
6033
6034/* dcread */
99e300ef 6035static void gen_dcread(DisasContext *ctx)
76a66253
JM
6036{
6037#if defined(CONFIG_USER_ONLY)
e06fcd75 6038 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
76a66253 6039#else
b61f2753 6040 TCGv EA, val;
76db3ba4 6041 if (unlikely(!ctx->mem_idx)) {
e06fcd75 6042 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
76a66253
JM
6043 return;
6044 }
76db3ba4 6045 gen_set_access_type(ctx, ACCESS_CACHE);
a7812ae4 6046 EA = tcg_temp_new();
76db3ba4 6047 gen_addr_reg_index(ctx, EA);
a7812ae4 6048 val = tcg_temp_new();
76db3ba4 6049 gen_qemu_ld32u(ctx, val, EA);
b61f2753
AJ
6050 tcg_temp_free(val);
6051 tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], EA);
6052 tcg_temp_free(EA);
76a66253
JM
6053#endif
6054}
6055
6056/* icbt */
e8eaa2c0 6057static void gen_icbt_40x(DisasContext *ctx)
76a66253
JM
6058{
6059 /* interpreted as no-op */
6060 /* XXX: specification say this is treated as a load by the MMU
6061 * but does not generate any exception
6062 */
6063}
6064
6065/* iccci */
99e300ef 6066static void gen_iccci(DisasContext *ctx)
76a66253
JM
6067{
6068#if defined(CONFIG_USER_ONLY)
e06fcd75 6069 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
76a66253 6070#else
76db3ba4 6071 if (unlikely(!ctx->mem_idx)) {
e06fcd75 6072 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
76a66253
JM
6073 return;
6074 }
6075 /* interpreted as no-op */
6076#endif
6077}
6078
6079/* icread */
99e300ef 6080static void gen_icread(DisasContext *ctx)
76a66253
JM
6081{
6082#if defined(CONFIG_USER_ONLY)
e06fcd75 6083 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
76a66253 6084#else
76db3ba4 6085 if (unlikely(!ctx->mem_idx)) {
e06fcd75 6086 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
76a66253
JM
6087 return;
6088 }
6089 /* interpreted as no-op */
6090#endif
6091}
6092
76db3ba4 6093/* rfci (mem_idx only) */
e8eaa2c0 6094static void gen_rfci_40x(DisasContext *ctx)
a42bd6cc
JM
6095{
6096#if defined(CONFIG_USER_ONLY)
e06fcd75 6097 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
a42bd6cc 6098#else
76db3ba4 6099 if (unlikely(!ctx->mem_idx)) {
e06fcd75 6100 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
a42bd6cc
JM
6101 return;
6102 }
6103 /* Restore CPU state */
e5f17ac6 6104 gen_helper_40x_rfci(cpu_env);
e06fcd75 6105 gen_sync_exception(ctx);
a42bd6cc
JM
6106#endif
6107}
6108
99e300ef 6109static void gen_rfci(DisasContext *ctx)
a42bd6cc
JM
6110{
6111#if defined(CONFIG_USER_ONLY)
e06fcd75 6112 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
a42bd6cc 6113#else
76db3ba4 6114 if (unlikely(!ctx->mem_idx)) {
e06fcd75 6115 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
a42bd6cc
JM
6116 return;
6117 }
6118 /* Restore CPU state */
e5f17ac6 6119 gen_helper_rfci(cpu_env);
e06fcd75 6120 gen_sync_exception(ctx);
a42bd6cc
JM
6121#endif
6122}
6123
6124/* BookE specific */
99e300ef 6125
54623277 6126/* XXX: not implemented on 440 ? */
99e300ef 6127static void gen_rfdi(DisasContext *ctx)
76a66253
JM
6128{
6129#if defined(CONFIG_USER_ONLY)
e06fcd75 6130 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
76a66253 6131#else
76db3ba4 6132 if (unlikely(!ctx->mem_idx)) {
e06fcd75 6133 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
76a66253
JM
6134 return;
6135 }
6136 /* Restore CPU state */
e5f17ac6 6137 gen_helper_rfdi(cpu_env);
e06fcd75 6138 gen_sync_exception(ctx);
76a66253
JM
6139#endif
6140}
6141
2662a059 6142/* XXX: not implemented on 440 ? */
99e300ef 6143static void gen_rfmci(DisasContext *ctx)
a42bd6cc
JM
6144{
6145#if defined(CONFIG_USER_ONLY)
e06fcd75 6146 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
a42bd6cc 6147#else
76db3ba4 6148 if (unlikely(!ctx->mem_idx)) {
e06fcd75 6149 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
a42bd6cc
JM
6150 return;
6151 }
6152 /* Restore CPU state */
e5f17ac6 6153 gen_helper_rfmci(cpu_env);
e06fcd75 6154 gen_sync_exception(ctx);
a42bd6cc
JM
6155#endif
6156}
5eb7995e 6157
d9bce9d9 6158/* TLB management - PowerPC 405 implementation */
e8eaa2c0 6159
54623277 6160/* tlbre */
e8eaa2c0 6161static void gen_tlbre_40x(DisasContext *ctx)
76a66253
JM
6162{
6163#if defined(CONFIG_USER_ONLY)
e06fcd75 6164 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
76a66253 6165#else
76db3ba4 6166 if (unlikely(!ctx->mem_idx)) {
e06fcd75 6167 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
76a66253
JM
6168 return;
6169 }
6170 switch (rB(ctx->opcode)) {
6171 case 0:
c6c7cf05
BS
6172 gen_helper_4xx_tlbre_hi(cpu_gpr[rD(ctx->opcode)], cpu_env,
6173 cpu_gpr[rA(ctx->opcode)]);
76a66253
JM
6174 break;
6175 case 1:
c6c7cf05
BS
6176 gen_helper_4xx_tlbre_lo(cpu_gpr[rD(ctx->opcode)], cpu_env,
6177 cpu_gpr[rA(ctx->opcode)]);
76a66253
JM
6178 break;
6179 default:
e06fcd75 6180 gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL);
76a66253 6181 break;
9a64fbe4 6182 }
76a66253
JM
6183#endif
6184}
6185
d9bce9d9 6186/* tlbsx - tlbsx. */
e8eaa2c0 6187static void gen_tlbsx_40x(DisasContext *ctx)
76a66253
JM
6188{
6189#if defined(CONFIG_USER_ONLY)
e06fcd75 6190 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
76a66253 6191#else
74d37793 6192 TCGv t0;
76db3ba4 6193 if (unlikely(!ctx->mem_idx)) {
e06fcd75 6194 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
76a66253
JM
6195 return;
6196 }
74d37793 6197 t0 = tcg_temp_new();
76db3ba4 6198 gen_addr_reg_index(ctx, t0);
c6c7cf05 6199 gen_helper_4xx_tlbsx(cpu_gpr[rD(ctx->opcode)], cpu_env, t0);
74d37793
AJ
6200 tcg_temp_free(t0);
6201 if (Rc(ctx->opcode)) {
6202 int l1 = gen_new_label();
da91a00f 6203 tcg_gen_trunc_tl_i32(cpu_crf[0], cpu_so);
74d37793
AJ
6204 tcg_gen_brcondi_tl(TCG_COND_EQ, cpu_gpr[rD(ctx->opcode)], -1, l1);
6205 tcg_gen_ori_i32(cpu_crf[0], cpu_crf[0], 0x02);
6206 gen_set_label(l1);
6207 }
76a66253 6208#endif
79aceca5
FB
6209}
6210
76a66253 6211/* tlbwe */
e8eaa2c0 6212static void gen_tlbwe_40x(DisasContext *ctx)
79aceca5 6213{
76a66253 6214#if defined(CONFIG_USER_ONLY)
e06fcd75 6215 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
76a66253 6216#else
76db3ba4 6217 if (unlikely(!ctx->mem_idx)) {
e06fcd75 6218 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
76a66253
JM
6219 return;
6220 }
6221 switch (rB(ctx->opcode)) {
6222 case 0:
c6c7cf05
BS
6223 gen_helper_4xx_tlbwe_hi(cpu_env, cpu_gpr[rA(ctx->opcode)],
6224 cpu_gpr[rS(ctx->opcode)]);
76a66253
JM
6225 break;
6226 case 1:
c6c7cf05
BS
6227 gen_helper_4xx_tlbwe_lo(cpu_env, cpu_gpr[rA(ctx->opcode)],
6228 cpu_gpr[rS(ctx->opcode)]);
76a66253
JM
6229 break;
6230 default:
e06fcd75 6231 gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL);
76a66253 6232 break;
9a64fbe4 6233 }
76a66253
JM
6234#endif
6235}
6236
a4bb6c3e 6237/* TLB management - PowerPC 440 implementation */
e8eaa2c0 6238
54623277 6239/* tlbre */
e8eaa2c0 6240static void gen_tlbre_440(DisasContext *ctx)
5eb7995e
JM
6241{
6242#if defined(CONFIG_USER_ONLY)
e06fcd75 6243 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
5eb7995e 6244#else
76db3ba4 6245 if (unlikely(!ctx->mem_idx)) {
e06fcd75 6246 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
5eb7995e
JM
6247 return;
6248 }
6249 switch (rB(ctx->opcode)) {
6250 case 0:
5eb7995e 6251 case 1:
5eb7995e 6252 case 2:
74d37793
AJ
6253 {
6254 TCGv_i32 t0 = tcg_const_i32(rB(ctx->opcode));
c6c7cf05
BS
6255 gen_helper_440_tlbre(cpu_gpr[rD(ctx->opcode)], cpu_env,
6256 t0, cpu_gpr[rA(ctx->opcode)]);
74d37793
AJ
6257 tcg_temp_free_i32(t0);
6258 }
5eb7995e
JM
6259 break;
6260 default:
e06fcd75 6261 gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL);
5eb7995e
JM
6262 break;
6263 }
6264#endif
6265}
6266
6267/* tlbsx - tlbsx. */
e8eaa2c0 6268static void gen_tlbsx_440(DisasContext *ctx)
5eb7995e
JM
6269{
6270#if defined(CONFIG_USER_ONLY)
e06fcd75 6271 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
5eb7995e 6272#else
74d37793 6273 TCGv t0;
76db3ba4 6274 if (unlikely(!ctx->mem_idx)) {
e06fcd75 6275 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
5eb7995e
JM
6276 return;
6277 }
74d37793 6278 t0 = tcg_temp_new();
76db3ba4 6279 gen_addr_reg_index(ctx, t0);
c6c7cf05 6280 gen_helper_440_tlbsx(cpu_gpr[rD(ctx->opcode)], cpu_env, t0);
74d37793
AJ
6281 tcg_temp_free(t0);
6282 if (Rc(ctx->opcode)) {
6283 int l1 = gen_new_label();
da91a00f 6284 tcg_gen_trunc_tl_i32(cpu_crf[0], cpu_so);
74d37793
AJ
6285 tcg_gen_brcondi_tl(TCG_COND_EQ, cpu_gpr[rD(ctx->opcode)], -1, l1);
6286 tcg_gen_ori_i32(cpu_crf[0], cpu_crf[0], 0x02);
6287 gen_set_label(l1);
6288 }
5eb7995e
JM
6289#endif
6290}
6291
6292/* tlbwe */
e8eaa2c0 6293static void gen_tlbwe_440(DisasContext *ctx)
5eb7995e
JM
6294{
6295#if defined(CONFIG_USER_ONLY)
e06fcd75 6296 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
5eb7995e 6297#else
76db3ba4 6298 if (unlikely(!ctx->mem_idx)) {
e06fcd75 6299 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
5eb7995e
JM
6300 return;
6301 }
6302 switch (rB(ctx->opcode)) {
6303 case 0:
5eb7995e 6304 case 1:
5eb7995e 6305 case 2:
74d37793
AJ
6306 {
6307 TCGv_i32 t0 = tcg_const_i32(rB(ctx->opcode));
c6c7cf05
BS
6308 gen_helper_440_tlbwe(cpu_env, t0, cpu_gpr[rA(ctx->opcode)],
6309 cpu_gpr[rS(ctx->opcode)]);
74d37793
AJ
6310 tcg_temp_free_i32(t0);
6311 }
5eb7995e
JM
6312 break;
6313 default:
e06fcd75 6314 gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL);
5eb7995e
JM
6315 break;
6316 }
6317#endif
6318}
6319
01662f3e
AG
6320/* TLB management - PowerPC BookE 2.06 implementation */
6321
6322/* tlbre */
6323static void gen_tlbre_booke206(DisasContext *ctx)
6324{
6325#if defined(CONFIG_USER_ONLY)
6326 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
6327#else
6328 if (unlikely(!ctx->mem_idx)) {
6329 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
6330 return;
6331 }
6332
c6c7cf05 6333 gen_helper_booke206_tlbre(cpu_env);
01662f3e
AG
6334#endif
6335}
6336
6337/* tlbsx - tlbsx. */
6338static void gen_tlbsx_booke206(DisasContext *ctx)
6339{
6340#if defined(CONFIG_USER_ONLY)
6341 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
6342#else
6343 TCGv t0;
6344 if (unlikely(!ctx->mem_idx)) {
6345 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
6346 return;
6347 }
6348
6349 if (rA(ctx->opcode)) {
6350 t0 = tcg_temp_new();
6351 tcg_gen_mov_tl(t0, cpu_gpr[rD(ctx->opcode)]);
6352 } else {
6353 t0 = tcg_const_tl(0);
6354 }
6355
6356 tcg_gen_add_tl(t0, t0, cpu_gpr[rB(ctx->opcode)]);
c6c7cf05 6357 gen_helper_booke206_tlbsx(cpu_env, t0);
01662f3e
AG
6358#endif
6359}
6360
6361/* tlbwe */
6362static void gen_tlbwe_booke206(DisasContext *ctx)
6363{
6364#if defined(CONFIG_USER_ONLY)
6365 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
6366#else
6367 if (unlikely(!ctx->mem_idx)) {
6368 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
6369 return;
6370 }
3f162d11 6371 gen_update_nip(ctx, ctx->nip - 4);
c6c7cf05 6372 gen_helper_booke206_tlbwe(cpu_env);
01662f3e
AG
6373#endif
6374}
6375
6376static void gen_tlbivax_booke206(DisasContext *ctx)
6377{
6378#if defined(CONFIG_USER_ONLY)
6379 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
6380#else
6381 TCGv t0;
6382 if (unlikely(!ctx->mem_idx)) {
6383 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
6384 return;
6385 }
6386
6387 t0 = tcg_temp_new();
6388 gen_addr_reg_index(ctx, t0);
6389
c6c7cf05 6390 gen_helper_booke206_tlbivax(cpu_env, t0);
01662f3e
AG
6391#endif
6392}
6393
6d3db821
AG
6394static void gen_tlbilx_booke206(DisasContext *ctx)
6395{
6396#if defined(CONFIG_USER_ONLY)
6397 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
6398#else
6399 TCGv t0;
6400 if (unlikely(!ctx->mem_idx)) {
6401 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
6402 return;
6403 }
6404
6405 t0 = tcg_temp_new();
6406 gen_addr_reg_index(ctx, t0);
6407
6408 switch((ctx->opcode >> 21) & 0x3) {
6409 case 0:
c6c7cf05 6410 gen_helper_booke206_tlbilx0(cpu_env, t0);
6d3db821
AG
6411 break;
6412 case 1:
c6c7cf05 6413 gen_helper_booke206_tlbilx1(cpu_env, t0);
6d3db821
AG
6414 break;
6415 case 3:
c6c7cf05 6416 gen_helper_booke206_tlbilx3(cpu_env, t0);
6d3db821
AG
6417 break;
6418 default:
6419 gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL);
6420 break;
6421 }
6422
6423 tcg_temp_free(t0);
6424#endif
6425}
6426
01662f3e 6427
76a66253 6428/* wrtee */
99e300ef 6429static void gen_wrtee(DisasContext *ctx)
76a66253
JM
6430{
6431#if defined(CONFIG_USER_ONLY)
e06fcd75 6432 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
76a66253 6433#else
6527f6ea 6434 TCGv t0;
76db3ba4 6435 if (unlikely(!ctx->mem_idx)) {
e06fcd75 6436 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
76a66253
JM
6437 return;
6438 }
6527f6ea
AJ
6439 t0 = tcg_temp_new();
6440 tcg_gen_andi_tl(t0, cpu_gpr[rD(ctx->opcode)], (1 << MSR_EE));
6441 tcg_gen_andi_tl(cpu_msr, cpu_msr, ~(1 << MSR_EE));
6442 tcg_gen_or_tl(cpu_msr, cpu_msr, t0);
6443 tcg_temp_free(t0);
dee96f6c
JM
6444 /* Stop translation to have a chance to raise an exception
6445 * if we just set msr_ee to 1
6446 */
e06fcd75 6447 gen_stop_exception(ctx);
76a66253
JM
6448#endif
6449}
6450
6451/* wrteei */
99e300ef 6452static void gen_wrteei(DisasContext *ctx)
76a66253
JM
6453{
6454#if defined(CONFIG_USER_ONLY)
e06fcd75 6455 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
76a66253 6456#else
76db3ba4 6457 if (unlikely(!ctx->mem_idx)) {
e06fcd75 6458 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
76a66253
JM
6459 return;
6460 }
fbe73008 6461 if (ctx->opcode & 0x00008000) {
6527f6ea
AJ
6462 tcg_gen_ori_tl(cpu_msr, cpu_msr, (1 << MSR_EE));
6463 /* Stop translation to have a chance to raise an exception */
e06fcd75 6464 gen_stop_exception(ctx);
6527f6ea 6465 } else {
1b6e5f99 6466 tcg_gen_andi_tl(cpu_msr, cpu_msr, ~(1 << MSR_EE));
6527f6ea 6467 }
76a66253
JM
6468#endif
6469}
6470
08e46e54 6471/* PowerPC 440 specific instructions */
99e300ef 6472
54623277 6473/* dlmzb */
99e300ef 6474static void gen_dlmzb(DisasContext *ctx)
76a66253 6475{
ef0d51af 6476 TCGv_i32 t0 = tcg_const_i32(Rc(ctx->opcode));
d15f74fb
BS
6477 gen_helper_dlmzb(cpu_gpr[rA(ctx->opcode)], cpu_env,
6478 cpu_gpr[rS(ctx->opcode)], cpu_gpr[rB(ctx->opcode)], t0);
ef0d51af 6479 tcg_temp_free_i32(t0);
76a66253
JM
6480}
6481
6482/* mbar replaces eieio on 440 */
99e300ef 6483static void gen_mbar(DisasContext *ctx)
76a66253
JM
6484{
6485 /* interpreted as no-op */
6486}
6487
6488/* msync replaces sync on 440 */
dcb2b9e1 6489static void gen_msync_4xx(DisasContext *ctx)
76a66253
JM
6490{
6491 /* interpreted as no-op */
6492}
6493
6494/* icbt */
e8eaa2c0 6495static void gen_icbt_440(DisasContext *ctx)
76a66253
JM
6496{
6497 /* interpreted as no-op */
6498 /* XXX: specification say this is treated as a load by the MMU
6499 * but does not generate any exception
6500 */
79aceca5
FB
6501}
6502
9e0b5cb1
AG
6503/* Embedded.Processor Control */
6504
6505static void gen_msgclr(DisasContext *ctx)
6506{
6507#if defined(CONFIG_USER_ONLY)
6508 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
6509#else
6510 if (unlikely(ctx->mem_idx == 0)) {
6511 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
6512 return;
6513 }
6514
e5f17ac6 6515 gen_helper_msgclr(cpu_env, cpu_gpr[rB(ctx->opcode)]);
9e0b5cb1
AG
6516#endif
6517}
6518
d5d11a39
AG
6519static void gen_msgsnd(DisasContext *ctx)
6520{
6521#if defined(CONFIG_USER_ONLY)
6522 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
6523#else
6524 if (unlikely(ctx->mem_idx == 0)) {
6525 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
6526 return;
6527 }
6528
6529 gen_helper_msgsnd(cpu_gpr[rB(ctx->opcode)]);
6530#endif
6531}
6532
a9d9eb8f
JM
6533/*** Altivec vector extension ***/
6534/* Altivec registers moves */
a9d9eb8f 6535
636aa200 6536static inline TCGv_ptr gen_avr_ptr(int reg)
564e571a 6537{
e4704b3b 6538 TCGv_ptr r = tcg_temp_new_ptr();
564e571a
AJ
6539 tcg_gen_addi_ptr(r, cpu_env, offsetof(CPUPPCState, avr[reg]));
6540 return r;
6541}
6542
a9d9eb8f 6543#define GEN_VR_LDX(name, opc2, opc3) \
99e300ef 6544static void glue(gen_, name)(DisasContext *ctx) \
a9d9eb8f 6545{ \
fe1e5c53 6546 TCGv EA; \
a9d9eb8f 6547 if (unlikely(!ctx->altivec_enabled)) { \
e06fcd75 6548 gen_exception(ctx, POWERPC_EXCP_VPU); \
a9d9eb8f
JM
6549 return; \
6550 } \
76db3ba4 6551 gen_set_access_type(ctx, ACCESS_INT); \
fe1e5c53 6552 EA = tcg_temp_new(); \
76db3ba4 6553 gen_addr_reg_index(ctx, EA); \
fe1e5c53 6554 tcg_gen_andi_tl(EA, EA, ~0xf); \
76db3ba4
AJ
6555 if (ctx->le_mode) { \
6556 gen_qemu_ld64(ctx, cpu_avrl[rD(ctx->opcode)], EA); \
fe1e5c53 6557 tcg_gen_addi_tl(EA, EA, 8); \
76db3ba4 6558 gen_qemu_ld64(ctx, cpu_avrh[rD(ctx->opcode)], EA); \
fe1e5c53 6559 } else { \
76db3ba4 6560 gen_qemu_ld64(ctx, cpu_avrh[rD(ctx->opcode)], EA); \
fe1e5c53 6561 tcg_gen_addi_tl(EA, EA, 8); \
76db3ba4 6562 gen_qemu_ld64(ctx, cpu_avrl[rD(ctx->opcode)], EA); \
fe1e5c53
AJ
6563 } \
6564 tcg_temp_free(EA); \
a9d9eb8f
JM
6565}
6566
6567#define GEN_VR_STX(name, opc2, opc3) \
99e300ef 6568static void gen_st##name(DisasContext *ctx) \
a9d9eb8f 6569{ \
fe1e5c53 6570 TCGv EA; \
a9d9eb8f 6571 if (unlikely(!ctx->altivec_enabled)) { \
e06fcd75 6572 gen_exception(ctx, POWERPC_EXCP_VPU); \
a9d9eb8f
JM
6573 return; \
6574 } \
76db3ba4 6575 gen_set_access_type(ctx, ACCESS_INT); \
fe1e5c53 6576 EA = tcg_temp_new(); \
76db3ba4 6577 gen_addr_reg_index(ctx, EA); \
fe1e5c53 6578 tcg_gen_andi_tl(EA, EA, ~0xf); \
76db3ba4
AJ
6579 if (ctx->le_mode) { \
6580 gen_qemu_st64(ctx, cpu_avrl[rD(ctx->opcode)], EA); \
fe1e5c53 6581 tcg_gen_addi_tl(EA, EA, 8); \
76db3ba4 6582 gen_qemu_st64(ctx, cpu_avrh[rD(ctx->opcode)], EA); \
fe1e5c53 6583 } else { \
76db3ba4 6584 gen_qemu_st64(ctx, cpu_avrh[rD(ctx->opcode)], EA); \
fe1e5c53 6585 tcg_gen_addi_tl(EA, EA, 8); \
76db3ba4 6586 gen_qemu_st64(ctx, cpu_avrl[rD(ctx->opcode)], EA); \
fe1e5c53
AJ
6587 } \
6588 tcg_temp_free(EA); \
a9d9eb8f
JM
6589}
6590
cbfb6ae9 6591#define GEN_VR_LVE(name, opc2, opc3) \
99e300ef 6592static void gen_lve##name(DisasContext *ctx) \
cbfb6ae9
AJ
6593 { \
6594 TCGv EA; \
6595 TCGv_ptr rs; \
6596 if (unlikely(!ctx->altivec_enabled)) { \
6597 gen_exception(ctx, POWERPC_EXCP_VPU); \
6598 return; \
6599 } \
6600 gen_set_access_type(ctx, ACCESS_INT); \
6601 EA = tcg_temp_new(); \
6602 gen_addr_reg_index(ctx, EA); \
6603 rs = gen_avr_ptr(rS(ctx->opcode)); \
2f5a189c 6604 gen_helper_lve##name(cpu_env, rs, EA); \
cbfb6ae9
AJ
6605 tcg_temp_free(EA); \
6606 tcg_temp_free_ptr(rs); \
6607 }
6608
6609#define GEN_VR_STVE(name, opc2, opc3) \
99e300ef 6610static void gen_stve##name(DisasContext *ctx) \
cbfb6ae9
AJ
6611 { \
6612 TCGv EA; \
6613 TCGv_ptr rs; \
6614 if (unlikely(!ctx->altivec_enabled)) { \
6615 gen_exception(ctx, POWERPC_EXCP_VPU); \
6616 return; \
6617 } \
6618 gen_set_access_type(ctx, ACCESS_INT); \
6619 EA = tcg_temp_new(); \
6620 gen_addr_reg_index(ctx, EA); \
6621 rs = gen_avr_ptr(rS(ctx->opcode)); \
2f5a189c 6622 gen_helper_stve##name(cpu_env, rs, EA); \
cbfb6ae9
AJ
6623 tcg_temp_free(EA); \
6624 tcg_temp_free_ptr(rs); \
6625 }
6626
fe1e5c53 6627GEN_VR_LDX(lvx, 0x07, 0x03);
a9d9eb8f 6628/* As we don't emulate the cache, lvxl is stricly equivalent to lvx */
fe1e5c53 6629GEN_VR_LDX(lvxl, 0x07, 0x0B);
a9d9eb8f 6630
cbfb6ae9
AJ
6631GEN_VR_LVE(bx, 0x07, 0x00);
6632GEN_VR_LVE(hx, 0x07, 0x01);
6633GEN_VR_LVE(wx, 0x07, 0x02);
6634
fe1e5c53 6635GEN_VR_STX(svx, 0x07, 0x07);
a9d9eb8f 6636/* As we don't emulate the cache, stvxl is stricly equivalent to stvx */
fe1e5c53 6637GEN_VR_STX(svxl, 0x07, 0x0F);
a9d9eb8f 6638
cbfb6ae9
AJ
6639GEN_VR_STVE(bx, 0x07, 0x04);
6640GEN_VR_STVE(hx, 0x07, 0x05);
6641GEN_VR_STVE(wx, 0x07, 0x06);
6642
99e300ef 6643static void gen_lvsl(DisasContext *ctx)
bf8d8ded
AJ
6644{
6645 TCGv_ptr rd;
6646 TCGv EA;
6647 if (unlikely(!ctx->altivec_enabled)) {
6648 gen_exception(ctx, POWERPC_EXCP_VPU);
6649 return;
6650 }
6651 EA = tcg_temp_new();
6652 gen_addr_reg_index(ctx, EA);
6653 rd = gen_avr_ptr(rD(ctx->opcode));
6654 gen_helper_lvsl(rd, EA);
6655 tcg_temp_free(EA);
6656 tcg_temp_free_ptr(rd);
6657}
6658
99e300ef 6659static void gen_lvsr(DisasContext *ctx)
bf8d8ded
AJ
6660{
6661 TCGv_ptr rd;
6662 TCGv EA;
6663 if (unlikely(!ctx->altivec_enabled)) {
6664 gen_exception(ctx, POWERPC_EXCP_VPU);
6665 return;
6666 }
6667 EA = tcg_temp_new();
6668 gen_addr_reg_index(ctx, EA);
6669 rd = gen_avr_ptr(rD(ctx->opcode));
6670 gen_helper_lvsr(rd, EA);
6671 tcg_temp_free(EA);
6672 tcg_temp_free_ptr(rd);
6673}
6674
99e300ef 6675static void gen_mfvscr(DisasContext *ctx)
785f451b
AJ
6676{
6677 TCGv_i32 t;
6678 if (unlikely(!ctx->altivec_enabled)) {
6679 gen_exception(ctx, POWERPC_EXCP_VPU);
6680 return;
6681 }
6682 tcg_gen_movi_i64(cpu_avrh[rD(ctx->opcode)], 0);
6683 t = tcg_temp_new_i32();
1328c2bf 6684 tcg_gen_ld_i32(t, cpu_env, offsetof(CPUPPCState, vscr));
785f451b 6685 tcg_gen_extu_i32_i64(cpu_avrl[rD(ctx->opcode)], t);
fce5ecb7 6686 tcg_temp_free_i32(t);
785f451b
AJ
6687}
6688
99e300ef 6689static void gen_mtvscr(DisasContext *ctx)
785f451b 6690{
6e87b7c7 6691 TCGv_ptr p;
785f451b
AJ
6692 if (unlikely(!ctx->altivec_enabled)) {
6693 gen_exception(ctx, POWERPC_EXCP_VPU);
6694 return;
6695 }
6e87b7c7 6696 p = gen_avr_ptr(rD(ctx->opcode));
d15f74fb 6697 gen_helper_mtvscr(cpu_env, p);
6e87b7c7 6698 tcg_temp_free_ptr(p);
785f451b
AJ
6699}
6700
7a9b96cf
AJ
6701/* Logical operations */
6702#define GEN_VX_LOGICAL(name, tcg_op, opc2, opc3) \
99e300ef 6703static void glue(gen_, name)(DisasContext *ctx) \
7a9b96cf
AJ
6704{ \
6705 if (unlikely(!ctx->altivec_enabled)) { \
6706 gen_exception(ctx, POWERPC_EXCP_VPU); \
6707 return; \
6708 } \
6709 tcg_op(cpu_avrh[rD(ctx->opcode)], cpu_avrh[rA(ctx->opcode)], cpu_avrh[rB(ctx->opcode)]); \
6710 tcg_op(cpu_avrl[rD(ctx->opcode)], cpu_avrl[rA(ctx->opcode)], cpu_avrl[rB(ctx->opcode)]); \
6711}
6712
6713GEN_VX_LOGICAL(vand, tcg_gen_and_i64, 2, 16);
6714GEN_VX_LOGICAL(vandc, tcg_gen_andc_i64, 2, 17);
6715GEN_VX_LOGICAL(vor, tcg_gen_or_i64, 2, 18);
6716GEN_VX_LOGICAL(vxor, tcg_gen_xor_i64, 2, 19);
6717GEN_VX_LOGICAL(vnor, tcg_gen_nor_i64, 2, 20);
6718
8e27dd6f 6719#define GEN_VXFORM(name, opc2, opc3) \
99e300ef 6720static void glue(gen_, name)(DisasContext *ctx) \
8e27dd6f
AJ
6721{ \
6722 TCGv_ptr ra, rb, rd; \
6723 if (unlikely(!ctx->altivec_enabled)) { \
6724 gen_exception(ctx, POWERPC_EXCP_VPU); \
6725 return; \
6726 } \
6727 ra = gen_avr_ptr(rA(ctx->opcode)); \
6728 rb = gen_avr_ptr(rB(ctx->opcode)); \
6729 rd = gen_avr_ptr(rD(ctx->opcode)); \
6730 gen_helper_##name (rd, ra, rb); \
6731 tcg_temp_free_ptr(ra); \
6732 tcg_temp_free_ptr(rb); \
6733 tcg_temp_free_ptr(rd); \
6734}
6735
d15f74fb
BS
6736#define GEN_VXFORM_ENV(name, opc2, opc3) \
6737static void glue(gen_, name)(DisasContext *ctx) \
6738{ \
6739 TCGv_ptr ra, rb, rd; \
6740 if (unlikely(!ctx->altivec_enabled)) { \
6741 gen_exception(ctx, POWERPC_EXCP_VPU); \
6742 return; \
6743 } \
6744 ra = gen_avr_ptr(rA(ctx->opcode)); \
6745 rb = gen_avr_ptr(rB(ctx->opcode)); \
6746 rd = gen_avr_ptr(rD(ctx->opcode)); \
54cddd21 6747 gen_helper_##name(cpu_env, rd, ra, rb); \
d15f74fb
BS
6748 tcg_temp_free_ptr(ra); \
6749 tcg_temp_free_ptr(rb); \
6750 tcg_temp_free_ptr(rd); \
6751}
6752
7872c51c
AJ
6753GEN_VXFORM(vaddubm, 0, 0);
6754GEN_VXFORM(vadduhm, 0, 1);
6755GEN_VXFORM(vadduwm, 0, 2);
6756GEN_VXFORM(vsububm, 0, 16);
6757GEN_VXFORM(vsubuhm, 0, 17);
6758GEN_VXFORM(vsubuwm, 0, 18);
e4039339
AJ
6759GEN_VXFORM(vmaxub, 1, 0);
6760GEN_VXFORM(vmaxuh, 1, 1);
6761GEN_VXFORM(vmaxuw, 1, 2);
6762GEN_VXFORM(vmaxsb, 1, 4);
6763GEN_VXFORM(vmaxsh, 1, 5);
6764GEN_VXFORM(vmaxsw, 1, 6);
6765GEN_VXFORM(vminub, 1, 8);
6766GEN_VXFORM(vminuh, 1, 9);
6767GEN_VXFORM(vminuw, 1, 10);
6768GEN_VXFORM(vminsb, 1, 12);
6769GEN_VXFORM(vminsh, 1, 13);
6770GEN_VXFORM(vminsw, 1, 14);
fab3cbe9
AJ
6771GEN_VXFORM(vavgub, 1, 16);
6772GEN_VXFORM(vavguh, 1, 17);
6773GEN_VXFORM(vavguw, 1, 18);
6774GEN_VXFORM(vavgsb, 1, 20);
6775GEN_VXFORM(vavgsh, 1, 21);
6776GEN_VXFORM(vavgsw, 1, 22);
3b430048
AJ
6777GEN_VXFORM(vmrghb, 6, 0);
6778GEN_VXFORM(vmrghh, 6, 1);
6779GEN_VXFORM(vmrghw, 6, 2);
6780GEN_VXFORM(vmrglb, 6, 4);
6781GEN_VXFORM(vmrglh, 6, 5);
6782GEN_VXFORM(vmrglw, 6, 6);
2c277908
AJ
6783GEN_VXFORM(vmuloub, 4, 0);
6784GEN_VXFORM(vmulouh, 4, 1);
6785GEN_VXFORM(vmulosb, 4, 4);
6786GEN_VXFORM(vmulosh, 4, 5);
6787GEN_VXFORM(vmuleub, 4, 8);
6788GEN_VXFORM(vmuleuh, 4, 9);
6789GEN_VXFORM(vmulesb, 4, 12);
6790GEN_VXFORM(vmulesh, 4, 13);
d79f0809
AJ
6791GEN_VXFORM(vslb, 2, 4);
6792GEN_VXFORM(vslh, 2, 5);
6793GEN_VXFORM(vslw, 2, 6);
07ef34c3
AJ
6794GEN_VXFORM(vsrb, 2, 8);
6795GEN_VXFORM(vsrh, 2, 9);
6796GEN_VXFORM(vsrw, 2, 10);
6797GEN_VXFORM(vsrab, 2, 12);
6798GEN_VXFORM(vsrah, 2, 13);
6799GEN_VXFORM(vsraw, 2, 14);
7b239bec
AJ
6800GEN_VXFORM(vslo, 6, 16);
6801GEN_VXFORM(vsro, 6, 17);
e343da72
AJ
6802GEN_VXFORM(vaddcuw, 0, 6);
6803GEN_VXFORM(vsubcuw, 0, 22);
d15f74fb
BS
6804GEN_VXFORM_ENV(vaddubs, 0, 8);
6805GEN_VXFORM_ENV(vadduhs, 0, 9);
6806GEN_VXFORM_ENV(vadduws, 0, 10);
6807GEN_VXFORM_ENV(vaddsbs, 0, 12);
6808GEN_VXFORM_ENV(vaddshs, 0, 13);
6809GEN_VXFORM_ENV(vaddsws, 0, 14);
6810GEN_VXFORM_ENV(vsububs, 0, 24);
6811GEN_VXFORM_ENV(vsubuhs, 0, 25);
6812GEN_VXFORM_ENV(vsubuws, 0, 26);
6813GEN_VXFORM_ENV(vsubsbs, 0, 28);
6814GEN_VXFORM_ENV(vsubshs, 0, 29);
6815GEN_VXFORM_ENV(vsubsws, 0, 30);
5e1d0985
AJ
6816GEN_VXFORM(vrlb, 2, 0);
6817GEN_VXFORM(vrlh, 2, 1);
6818GEN_VXFORM(vrlw, 2, 2);
d9430add
AJ
6819GEN_VXFORM(vsl, 2, 7);
6820GEN_VXFORM(vsr, 2, 11);
d15f74fb
BS
6821GEN_VXFORM_ENV(vpkuhum, 7, 0);
6822GEN_VXFORM_ENV(vpkuwum, 7, 1);
6823GEN_VXFORM_ENV(vpkuhus, 7, 2);
6824GEN_VXFORM_ENV(vpkuwus, 7, 3);
6825GEN_VXFORM_ENV(vpkshus, 7, 4);
6826GEN_VXFORM_ENV(vpkswus, 7, 5);
6827GEN_VXFORM_ENV(vpkshss, 7, 6);
6828GEN_VXFORM_ENV(vpkswss, 7, 7);
1dd9ffb9 6829GEN_VXFORM(vpkpx, 7, 12);
d15f74fb
BS
6830GEN_VXFORM_ENV(vsum4ubs, 4, 24);
6831GEN_VXFORM_ENV(vsum4sbs, 4, 28);
6832GEN_VXFORM_ENV(vsum4shs, 4, 25);
6833GEN_VXFORM_ENV(vsum2sws, 4, 26);
6834GEN_VXFORM_ENV(vsumsws, 4, 30);
6835GEN_VXFORM_ENV(vaddfp, 5, 0);
6836GEN_VXFORM_ENV(vsubfp, 5, 1);
6837GEN_VXFORM_ENV(vmaxfp, 5, 16);
6838GEN_VXFORM_ENV(vminfp, 5, 17);
fab3cbe9 6839
0cbcd906 6840#define GEN_VXRFORM1(opname, name, str, opc2, opc3) \
e8eaa2c0 6841static void glue(gen_, name)(DisasContext *ctx) \
0cbcd906
AJ
6842 { \
6843 TCGv_ptr ra, rb, rd; \
6844 if (unlikely(!ctx->altivec_enabled)) { \
6845 gen_exception(ctx, POWERPC_EXCP_VPU); \
6846 return; \
6847 } \
6848 ra = gen_avr_ptr(rA(ctx->opcode)); \
6849 rb = gen_avr_ptr(rB(ctx->opcode)); \
6850 rd = gen_avr_ptr(rD(ctx->opcode)); \
d15f74fb 6851 gen_helper_##opname(cpu_env, rd, ra, rb); \
0cbcd906
AJ
6852 tcg_temp_free_ptr(ra); \
6853 tcg_temp_free_ptr(rb); \
6854 tcg_temp_free_ptr(rd); \
6855 }
6856
6857#define GEN_VXRFORM(name, opc2, opc3) \
6858 GEN_VXRFORM1(name, name, #name, opc2, opc3) \
6859 GEN_VXRFORM1(name##_dot, name##_, #name ".", opc2, (opc3 | (0x1 << 4)))
6860
1add6e23
AJ
6861GEN_VXRFORM(vcmpequb, 3, 0)
6862GEN_VXRFORM(vcmpequh, 3, 1)
6863GEN_VXRFORM(vcmpequw, 3, 2)
6864GEN_VXRFORM(vcmpgtsb, 3, 12)
6865GEN_VXRFORM(vcmpgtsh, 3, 13)
6866GEN_VXRFORM(vcmpgtsw, 3, 14)
6867GEN_VXRFORM(vcmpgtub, 3, 8)
6868GEN_VXRFORM(vcmpgtuh, 3, 9)
6869GEN_VXRFORM(vcmpgtuw, 3, 10)
819ca121
AJ
6870GEN_VXRFORM(vcmpeqfp, 3, 3)
6871GEN_VXRFORM(vcmpgefp, 3, 7)
6872GEN_VXRFORM(vcmpgtfp, 3, 11)
6873GEN_VXRFORM(vcmpbfp, 3, 15)
1add6e23 6874
c026766b 6875#define GEN_VXFORM_SIMM(name, opc2, opc3) \
99e300ef 6876static void glue(gen_, name)(DisasContext *ctx) \
c026766b
AJ
6877 { \
6878 TCGv_ptr rd; \
6879 TCGv_i32 simm; \
6880 if (unlikely(!ctx->altivec_enabled)) { \
6881 gen_exception(ctx, POWERPC_EXCP_VPU); \
6882 return; \
6883 } \
6884 simm = tcg_const_i32(SIMM5(ctx->opcode)); \
6885 rd = gen_avr_ptr(rD(ctx->opcode)); \
6886 gen_helper_##name (rd, simm); \
6887 tcg_temp_free_i32(simm); \
6888 tcg_temp_free_ptr(rd); \
6889 }
6890
6891GEN_VXFORM_SIMM(vspltisb, 6, 12);
6892GEN_VXFORM_SIMM(vspltish, 6, 13);
6893GEN_VXFORM_SIMM(vspltisw, 6, 14);
6894
de5f2484 6895#define GEN_VXFORM_NOA(name, opc2, opc3) \
99e300ef 6896static void glue(gen_, name)(DisasContext *ctx) \
de5f2484
AJ
6897 { \
6898 TCGv_ptr rb, rd; \
6899 if (unlikely(!ctx->altivec_enabled)) { \
6900 gen_exception(ctx, POWERPC_EXCP_VPU); \
6901 return; \
6902 } \
6903 rb = gen_avr_ptr(rB(ctx->opcode)); \
6904 rd = gen_avr_ptr(rD(ctx->opcode)); \
6905 gen_helper_##name (rd, rb); \
6906 tcg_temp_free_ptr(rb); \
6907 tcg_temp_free_ptr(rd); \
6908 }
6909
d15f74fb
BS
6910#define GEN_VXFORM_NOA_ENV(name, opc2, opc3) \
6911static void glue(gen_, name)(DisasContext *ctx) \
6912 { \
6913 TCGv_ptr rb, rd; \
6914 \
6915 if (unlikely(!ctx->altivec_enabled)) { \
6916 gen_exception(ctx, POWERPC_EXCP_VPU); \
6917 return; \
6918 } \
6919 rb = gen_avr_ptr(rB(ctx->opcode)); \
6920 rd = gen_avr_ptr(rD(ctx->opcode)); \
6921 gen_helper_##name(cpu_env, rd, rb); \
6922 tcg_temp_free_ptr(rb); \
6923 tcg_temp_free_ptr(rd); \
6924 }
6925
6cf1c6e5
AJ
6926GEN_VXFORM_NOA(vupkhsb, 7, 8);
6927GEN_VXFORM_NOA(vupkhsh, 7, 9);
6928GEN_VXFORM_NOA(vupklsb, 7, 10);
6929GEN_VXFORM_NOA(vupklsh, 7, 11);
79f85c3a
AJ
6930GEN_VXFORM_NOA(vupkhpx, 7, 13);
6931GEN_VXFORM_NOA(vupklpx, 7, 15);
d15f74fb
BS
6932GEN_VXFORM_NOA_ENV(vrefp, 5, 4);
6933GEN_VXFORM_NOA_ENV(vrsqrtefp, 5, 5);
6934GEN_VXFORM_NOA_ENV(vexptefp, 5, 6);
6935GEN_VXFORM_NOA_ENV(vlogefp, 5, 7);
6936GEN_VXFORM_NOA_ENV(vrfim, 5, 8);
6937GEN_VXFORM_NOA_ENV(vrfin, 5, 9);
6938GEN_VXFORM_NOA_ENV(vrfip, 5, 10);
6939GEN_VXFORM_NOA_ENV(vrfiz, 5, 11);
79f85c3a 6940
21d21583 6941#define GEN_VXFORM_SIMM(name, opc2, opc3) \
99e300ef 6942static void glue(gen_, name)(DisasContext *ctx) \
21d21583
AJ
6943 { \
6944 TCGv_ptr rd; \
6945 TCGv_i32 simm; \
6946 if (unlikely(!ctx->altivec_enabled)) { \
6947 gen_exception(ctx, POWERPC_EXCP_VPU); \
6948 return; \
6949 } \
6950 simm = tcg_const_i32(SIMM5(ctx->opcode)); \
6951 rd = gen_avr_ptr(rD(ctx->opcode)); \
6952 gen_helper_##name (rd, simm); \
6953 tcg_temp_free_i32(simm); \
6954 tcg_temp_free_ptr(rd); \
6955 }
6956
27a4edb3 6957#define GEN_VXFORM_UIMM(name, opc2, opc3) \
99e300ef 6958static void glue(gen_, name)(DisasContext *ctx) \
27a4edb3
AJ
6959 { \
6960 TCGv_ptr rb, rd; \
6961 TCGv_i32 uimm; \
6962 if (unlikely(!ctx->altivec_enabled)) { \
6963 gen_exception(ctx, POWERPC_EXCP_VPU); \
6964 return; \
6965 } \
6966 uimm = tcg_const_i32(UIMM5(ctx->opcode)); \
6967 rb = gen_avr_ptr(rB(ctx->opcode)); \
6968 rd = gen_avr_ptr(rD(ctx->opcode)); \
6969 gen_helper_##name (rd, rb, uimm); \
6970 tcg_temp_free_i32(uimm); \
6971 tcg_temp_free_ptr(rb); \
6972 tcg_temp_free_ptr(rd); \
6973 }
6974
d15f74fb
BS
6975#define GEN_VXFORM_UIMM_ENV(name, opc2, opc3) \
6976static void glue(gen_, name)(DisasContext *ctx) \
6977 { \
6978 TCGv_ptr rb, rd; \
6979 TCGv_i32 uimm; \
6980 \
6981 if (unlikely(!ctx->altivec_enabled)) { \
6982 gen_exception(ctx, POWERPC_EXCP_VPU); \
6983 return; \
6984 } \
6985 uimm = tcg_const_i32(UIMM5(ctx->opcode)); \
6986 rb = gen_avr_ptr(rB(ctx->opcode)); \
6987 rd = gen_avr_ptr(rD(ctx->opcode)); \
6988 gen_helper_##name(cpu_env, rd, rb, uimm); \
6989 tcg_temp_free_i32(uimm); \
6990 tcg_temp_free_ptr(rb); \
6991 tcg_temp_free_ptr(rd); \
6992 }
6993
e4e6bee7
AJ
6994GEN_VXFORM_UIMM(vspltb, 6, 8);
6995GEN_VXFORM_UIMM(vsplth, 6, 9);
6996GEN_VXFORM_UIMM(vspltw, 6, 10);
d15f74fb
BS
6997GEN_VXFORM_UIMM_ENV(vcfux, 5, 12);
6998GEN_VXFORM_UIMM_ENV(vcfsx, 5, 13);
6999GEN_VXFORM_UIMM_ENV(vctuxs, 5, 14);
7000GEN_VXFORM_UIMM_ENV(vctsxs, 5, 15);
e4e6bee7 7001
99e300ef 7002static void gen_vsldoi(DisasContext *ctx)
cd633b10
AJ
7003{
7004 TCGv_ptr ra, rb, rd;
fce5ecb7 7005 TCGv_i32 sh;
cd633b10
AJ
7006 if (unlikely(!ctx->altivec_enabled)) {
7007 gen_exception(ctx, POWERPC_EXCP_VPU);
7008 return;
7009 }
7010 ra = gen_avr_ptr(rA(ctx->opcode));
7011 rb = gen_avr_ptr(rB(ctx->opcode));
7012 rd = gen_avr_ptr(rD(ctx->opcode));
7013 sh = tcg_const_i32(VSH(ctx->opcode));
7014 gen_helper_vsldoi (rd, ra, rb, sh);
7015 tcg_temp_free_ptr(ra);
7016 tcg_temp_free_ptr(rb);
7017 tcg_temp_free_ptr(rd);
fce5ecb7 7018 tcg_temp_free_i32(sh);
cd633b10
AJ
7019}
7020
707cec33 7021#define GEN_VAFORM_PAIRED(name0, name1, opc2) \
d15f74fb 7022static void glue(gen_, name0##_##name1)(DisasContext *ctx) \
707cec33
AJ
7023 { \
7024 TCGv_ptr ra, rb, rc, rd; \
7025 if (unlikely(!ctx->altivec_enabled)) { \
7026 gen_exception(ctx, POWERPC_EXCP_VPU); \
7027 return; \
7028 } \
7029 ra = gen_avr_ptr(rA(ctx->opcode)); \
7030 rb = gen_avr_ptr(rB(ctx->opcode)); \
7031 rc = gen_avr_ptr(rC(ctx->opcode)); \
7032 rd = gen_avr_ptr(rD(ctx->opcode)); \
7033 if (Rc(ctx->opcode)) { \
d15f74fb 7034 gen_helper_##name1(cpu_env, rd, ra, rb, rc); \
707cec33 7035 } else { \
d15f74fb 7036 gen_helper_##name0(cpu_env, rd, ra, rb, rc); \
707cec33
AJ
7037 } \
7038 tcg_temp_free_ptr(ra); \
7039 tcg_temp_free_ptr(rb); \
7040 tcg_temp_free_ptr(rc); \
7041 tcg_temp_free_ptr(rd); \
7042 }
7043
b161ae27
AJ
7044GEN_VAFORM_PAIRED(vmhaddshs, vmhraddshs, 16)
7045
99e300ef 7046static void gen_vmladduhm(DisasContext *ctx)
bcd2ee23
AJ
7047{
7048 TCGv_ptr ra, rb, rc, rd;
7049 if (unlikely(!ctx->altivec_enabled)) {
7050 gen_exception(ctx, POWERPC_EXCP_VPU);
7051 return;
7052 }
7053 ra = gen_avr_ptr(rA(ctx->opcode));
7054 rb = gen_avr_ptr(rB(ctx->opcode));
7055 rc = gen_avr_ptr(rC(ctx->opcode));
7056 rd = gen_avr_ptr(rD(ctx->opcode));
7057 gen_helper_vmladduhm(rd, ra, rb, rc);
7058 tcg_temp_free_ptr(ra);
7059 tcg_temp_free_ptr(rb);
7060 tcg_temp_free_ptr(rc);
7061 tcg_temp_free_ptr(rd);
7062}
7063
b04ae981 7064GEN_VAFORM_PAIRED(vmsumubm, vmsummbm, 18)
4d9903b6 7065GEN_VAFORM_PAIRED(vmsumuhm, vmsumuhs, 19)
eae07261 7066GEN_VAFORM_PAIRED(vmsumshm, vmsumshs, 20)
d1258698 7067GEN_VAFORM_PAIRED(vsel, vperm, 21)
35cf7c7e 7068GEN_VAFORM_PAIRED(vmaddfp, vnmsubfp, 23)
b04ae981 7069
472b24ce
TM
7070/*** VSX extension ***/
7071
7072static inline TCGv_i64 cpu_vsrh(int n)
7073{
7074 if (n < 32) {
7075 return cpu_fpr[n];
7076 } else {
7077 return cpu_avrh[n-32];
7078 }
7079}
7080
7081static inline TCGv_i64 cpu_vsrl(int n)
7082{
7083 if (n < 32) {
7084 return cpu_vsr[n];
7085 } else {
7086 return cpu_avrl[n-32];
7087 }
7088}
7089
e072fe79
TM
7090#define VSX_LOAD_SCALAR(name, operation) \
7091static void gen_##name(DisasContext *ctx) \
7092{ \
7093 TCGv EA; \
7094 if (unlikely(!ctx->vsx_enabled)) { \
7095 gen_exception(ctx, POWERPC_EXCP_VSXU); \
7096 return; \
7097 } \
7098 gen_set_access_type(ctx, ACCESS_INT); \
7099 EA = tcg_temp_new(); \
7100 gen_addr_reg_index(ctx, EA); \
7101 gen_qemu_##operation(ctx, cpu_vsrh(xT(ctx->opcode)), EA); \
7102 /* NOTE: cpu_vsrl is undefined */ \
7103 tcg_temp_free(EA); \
7104}
7105
7106VSX_LOAD_SCALAR(lxsdx, ld64)
cac7f0ba
TM
7107VSX_LOAD_SCALAR(lxsiwax, ld32s_i64)
7108VSX_LOAD_SCALAR(lxsiwzx, ld32u_i64)
7109VSX_LOAD_SCALAR(lxsspx, ld32fs)
fa1832d7 7110
304af367
TM
7111static void gen_lxvd2x(DisasContext *ctx)
7112{
7113 TCGv EA;
7114 if (unlikely(!ctx->vsx_enabled)) {
7115 gen_exception(ctx, POWERPC_EXCP_VSXU);
7116 return;
7117 }
7118 gen_set_access_type(ctx, ACCESS_INT);
7119 EA = tcg_temp_new();
7120 gen_addr_reg_index(ctx, EA);
7121 gen_qemu_ld64(ctx, cpu_vsrh(xT(ctx->opcode)), EA);
7122 tcg_gen_addi_tl(EA, EA, 8);
7123 gen_qemu_ld64(ctx, cpu_vsrl(xT(ctx->opcode)), EA);
7124 tcg_temp_free(EA);
7125}
7126
ca03b467
TM
7127static void gen_lxvdsx(DisasContext *ctx)
7128{
7129 TCGv EA;
7130 if (unlikely(!ctx->vsx_enabled)) {
7131 gen_exception(ctx, POWERPC_EXCP_VSXU);
7132 return;
7133 }
7134 gen_set_access_type(ctx, ACCESS_INT);
7135 EA = tcg_temp_new();
7136 gen_addr_reg_index(ctx, EA);
7137 gen_qemu_ld64(ctx, cpu_vsrh(xT(ctx->opcode)), EA);
f976b09e 7138 tcg_gen_mov_i64(cpu_vsrl(xT(ctx->opcode)), cpu_vsrh(xT(ctx->opcode)));
ca03b467
TM
7139 tcg_temp_free(EA);
7140}
7141
897e61d1
TM
7142static void gen_lxvw4x(DisasContext *ctx)
7143{
f976b09e
AG
7144 TCGv EA;
7145 TCGv_i64 tmp;
897e61d1
TM
7146 TCGv_i64 xth = cpu_vsrh(xT(ctx->opcode));
7147 TCGv_i64 xtl = cpu_vsrl(xT(ctx->opcode));
7148 if (unlikely(!ctx->vsx_enabled)) {
7149 gen_exception(ctx, POWERPC_EXCP_VSXU);
7150 return;
7151 }
7152 gen_set_access_type(ctx, ACCESS_INT);
7153 EA = tcg_temp_new();
f976b09e
AG
7154 tmp = tcg_temp_new_i64();
7155
897e61d1 7156 gen_addr_reg_index(ctx, EA);
f976b09e 7157 gen_qemu_ld32u_i64(ctx, tmp, EA);
897e61d1 7158 tcg_gen_addi_tl(EA, EA, 4);
f976b09e 7159 gen_qemu_ld32u_i64(ctx, xth, EA);
897e61d1
TM
7160 tcg_gen_deposit_i64(xth, xth, tmp, 32, 32);
7161
7162 tcg_gen_addi_tl(EA, EA, 4);
f976b09e 7163 gen_qemu_ld32u_i64(ctx, tmp, EA);
897e61d1 7164 tcg_gen_addi_tl(EA, EA, 4);
f976b09e 7165 gen_qemu_ld32u_i64(ctx, xtl, EA);
897e61d1
TM
7166 tcg_gen_deposit_i64(xtl, xtl, tmp, 32, 32);
7167
7168 tcg_temp_free(EA);
f976b09e 7169 tcg_temp_free_i64(tmp);
897e61d1
TM
7170}
7171
f026da78
TM
7172#define VSX_STORE_SCALAR(name, operation) \
7173static void gen_##name(DisasContext *ctx) \
7174{ \
7175 TCGv EA; \
7176 if (unlikely(!ctx->vsx_enabled)) { \
7177 gen_exception(ctx, POWERPC_EXCP_VSXU); \
7178 return; \
7179 } \
7180 gen_set_access_type(ctx, ACCESS_INT); \
7181 EA = tcg_temp_new(); \
7182 gen_addr_reg_index(ctx, EA); \
7183 gen_qemu_##operation(ctx, cpu_vsrh(xS(ctx->opcode)), EA); \
7184 tcg_temp_free(EA); \
9231ba9e
TM
7185}
7186
f026da78 7187VSX_STORE_SCALAR(stxsdx, st64)
e16a626b
TM
7188VSX_STORE_SCALAR(stxsiwx, st32_i64)
7189VSX_STORE_SCALAR(stxsspx, st32fs)
f026da78 7190
fbed2478
TM
7191static void gen_stxvd2x(DisasContext *ctx)
7192{
7193 TCGv EA;
7194 if (unlikely(!ctx->vsx_enabled)) {
7195 gen_exception(ctx, POWERPC_EXCP_VSXU);
7196 return;
7197 }
7198 gen_set_access_type(ctx, ACCESS_INT);
7199 EA = tcg_temp_new();
7200 gen_addr_reg_index(ctx, EA);
7201 gen_qemu_st64(ctx, cpu_vsrh(xS(ctx->opcode)), EA);
7202 tcg_gen_addi_tl(EA, EA, 8);
7203 gen_qemu_st64(ctx, cpu_vsrl(xS(ctx->opcode)), EA);
7204 tcg_temp_free(EA);
7205}
7206
86e61ce3
TM
7207static void gen_stxvw4x(DisasContext *ctx)
7208{
f976b09e
AG
7209 TCGv_i64 tmp;
7210 TCGv EA;
86e61ce3
TM
7211 if (unlikely(!ctx->vsx_enabled)) {
7212 gen_exception(ctx, POWERPC_EXCP_VSXU);
7213 return;
7214 }
7215 gen_set_access_type(ctx, ACCESS_INT);
7216 EA = tcg_temp_new();
7217 gen_addr_reg_index(ctx, EA);
f976b09e 7218 tmp = tcg_temp_new_i64();
86e61ce3
TM
7219
7220 tcg_gen_shri_i64(tmp, cpu_vsrh(xS(ctx->opcode)), 32);
f976b09e 7221 gen_qemu_st32_i64(ctx, tmp, EA);
86e61ce3 7222 tcg_gen_addi_tl(EA, EA, 4);
f976b09e 7223 gen_qemu_st32_i64(ctx, cpu_vsrh(xS(ctx->opcode)), EA);
86e61ce3
TM
7224
7225 tcg_gen_shri_i64(tmp, cpu_vsrl(xS(ctx->opcode)), 32);
7226 tcg_gen_addi_tl(EA, EA, 4);
f976b09e 7227 gen_qemu_st32_i64(ctx, tmp, EA);
86e61ce3 7228 tcg_gen_addi_tl(EA, EA, 4);
f976b09e 7229 gen_qemu_st32_i64(ctx, cpu_vsrl(xS(ctx->opcode)), EA);
86e61ce3
TM
7230
7231 tcg_temp_free(EA);
f976b09e 7232 tcg_temp_free_i64(tmp);
86e61ce3
TM
7233}
7234
f5c0f7f9
TM
7235#define MV_VSRW(name, tcgop1, tcgop2, target, source) \
7236static void gen_##name(DisasContext *ctx) \
7237{ \
7238 if (xS(ctx->opcode) < 32) { \
7239 if (unlikely(!ctx->fpu_enabled)) { \
7240 gen_exception(ctx, POWERPC_EXCP_FPU); \
7241 return; \
7242 } \
7243 } else { \
7244 if (unlikely(!ctx->altivec_enabled)) { \
7245 gen_exception(ctx, POWERPC_EXCP_VPU); \
7246 return; \
7247 } \
7248 } \
7249 TCGv_i64 tmp = tcg_temp_new_i64(); \
7250 tcg_gen_##tcgop1(tmp, source); \
7251 tcg_gen_##tcgop2(target, tmp); \
7252 tcg_temp_free_i64(tmp); \
7253}
7254
7255
7256MV_VSRW(mfvsrwz, ext32u_i64, trunc_i64_tl, cpu_gpr[rA(ctx->opcode)], \
7257 cpu_vsrh(xS(ctx->opcode)))
7258MV_VSRW(mtvsrwa, extu_tl_i64, ext32s_i64, cpu_vsrh(xT(ctx->opcode)), \
7259 cpu_gpr[rA(ctx->opcode)])
7260MV_VSRW(mtvsrwz, extu_tl_i64, ext32u_i64, cpu_vsrh(xT(ctx->opcode)), \
7261 cpu_gpr[rA(ctx->opcode)])
7262
7263#if defined(TARGET_PPC64)
7264#define MV_VSRD(name, target, source) \
7265static void gen_##name(DisasContext *ctx) \
7266{ \
7267 if (xS(ctx->opcode) < 32) { \
7268 if (unlikely(!ctx->fpu_enabled)) { \
7269 gen_exception(ctx, POWERPC_EXCP_FPU); \
7270 return; \
7271 } \
7272 } else { \
7273 if (unlikely(!ctx->altivec_enabled)) { \
7274 gen_exception(ctx, POWERPC_EXCP_VPU); \
7275 return; \
7276 } \
7277 } \
7278 tcg_gen_mov_i64(target, source); \
7279}
7280
7281MV_VSRD(mfvsrd, cpu_gpr[rA(ctx->opcode)], cpu_vsrh(xS(ctx->opcode)))
7282MV_VSRD(mtvsrd, cpu_vsrh(xT(ctx->opcode)), cpu_gpr[rA(ctx->opcode)])
7283
7284#endif
7285
cd73f2c9
TM
7286static void gen_xxpermdi(DisasContext *ctx)
7287{
7288 if (unlikely(!ctx->vsx_enabled)) {
7289 gen_exception(ctx, POWERPC_EXCP_VSXU);
7290 return;
7291 }
7292
7293 if ((DM(ctx->opcode) & 2) == 0) {
7294 tcg_gen_mov_i64(cpu_vsrh(xT(ctx->opcode)), cpu_vsrh(xA(ctx->opcode)));
7295 } else {
7296 tcg_gen_mov_i64(cpu_vsrh(xT(ctx->opcode)), cpu_vsrl(xA(ctx->opcode)));
7297 }
7298 if ((DM(ctx->opcode) & 1) == 0) {
7299 tcg_gen_mov_i64(cpu_vsrl(xT(ctx->opcode)), cpu_vsrh(xB(ctx->opcode)));
7300 } else {
7301 tcg_gen_mov_i64(cpu_vsrl(xT(ctx->opcode)), cpu_vsrl(xB(ctx->opcode)));
7302 }
7303}
7304
df020ce0
TM
7305#define OP_ABS 1
7306#define OP_NABS 2
7307#define OP_NEG 3
7308#define OP_CPSGN 4
7309#define SGN_MASK_DP 0x8000000000000000ul
7310#define SGN_MASK_SP 0x8000000080000000ul
7311
7312#define VSX_SCALAR_MOVE(name, op, sgn_mask) \
7313static void glue(gen_, name)(DisasContext * ctx) \
7314 { \
7315 TCGv_i64 xb, sgm; \
7316 if (unlikely(!ctx->vsx_enabled)) { \
7317 gen_exception(ctx, POWERPC_EXCP_VSXU); \
7318 return; \
7319 } \
f976b09e
AG
7320 xb = tcg_temp_new_i64(); \
7321 sgm = tcg_temp_new_i64(); \
df020ce0
TM
7322 tcg_gen_mov_i64(xb, cpu_vsrh(xB(ctx->opcode))); \
7323 tcg_gen_movi_i64(sgm, sgn_mask); \
7324 switch (op) { \
7325 case OP_ABS: { \
7326 tcg_gen_andc_i64(xb, xb, sgm); \
7327 break; \
7328 } \
7329 case OP_NABS: { \
7330 tcg_gen_or_i64(xb, xb, sgm); \
7331 break; \
7332 } \
7333 case OP_NEG: { \
7334 tcg_gen_xor_i64(xb, xb, sgm); \
7335 break; \
7336 } \
7337 case OP_CPSGN: { \
f976b09e 7338 TCGv_i64 xa = tcg_temp_new_i64(); \
df020ce0
TM
7339 tcg_gen_mov_i64(xa, cpu_vsrh(xA(ctx->opcode))); \
7340 tcg_gen_and_i64(xa, xa, sgm); \
7341 tcg_gen_andc_i64(xb, xb, sgm); \
7342 tcg_gen_or_i64(xb, xb, xa); \
f976b09e 7343 tcg_temp_free_i64(xa); \
df020ce0
TM
7344 break; \
7345 } \
7346 } \
7347 tcg_gen_mov_i64(cpu_vsrh(xT(ctx->opcode)), xb); \
f976b09e
AG
7348 tcg_temp_free_i64(xb); \
7349 tcg_temp_free_i64(sgm); \
df020ce0
TM
7350 }
7351
7352VSX_SCALAR_MOVE(xsabsdp, OP_ABS, SGN_MASK_DP)
7353VSX_SCALAR_MOVE(xsnabsdp, OP_NABS, SGN_MASK_DP)
7354VSX_SCALAR_MOVE(xsnegdp, OP_NEG, SGN_MASK_DP)
7355VSX_SCALAR_MOVE(xscpsgndp, OP_CPSGN, SGN_MASK_DP)
7356
be574920
TM
7357#define VSX_VECTOR_MOVE(name, op, sgn_mask) \
7358static void glue(gen_, name)(DisasContext * ctx) \
7359 { \
7360 TCGv_i64 xbh, xbl, sgm; \
7361 if (unlikely(!ctx->vsx_enabled)) { \
7362 gen_exception(ctx, POWERPC_EXCP_VSXU); \
7363 return; \
7364 } \
f976b09e
AG
7365 xbh = tcg_temp_new_i64(); \
7366 xbl = tcg_temp_new_i64(); \
7367 sgm = tcg_temp_new_i64(); \
be574920
TM
7368 tcg_gen_mov_i64(xbh, cpu_vsrh(xB(ctx->opcode))); \
7369 tcg_gen_mov_i64(xbl, cpu_vsrl(xB(ctx->opcode))); \
7370 tcg_gen_movi_i64(sgm, sgn_mask); \
7371 switch (op) { \
7372 case OP_ABS: { \
7373 tcg_gen_andc_i64(xbh, xbh, sgm); \
7374 tcg_gen_andc_i64(xbl, xbl, sgm); \
7375 break; \
7376 } \
7377 case OP_NABS: { \
7378 tcg_gen_or_i64(xbh, xbh, sgm); \
7379 tcg_gen_or_i64(xbl, xbl, sgm); \
7380 break; \
7381 } \
7382 case OP_NEG: { \
7383 tcg_gen_xor_i64(xbh, xbh, sgm); \
7384 tcg_gen_xor_i64(xbl, xbl, sgm); \
7385 break; \
7386 } \
7387 case OP_CPSGN: { \
f976b09e
AG
7388 TCGv_i64 xah = tcg_temp_new_i64(); \
7389 TCGv_i64 xal = tcg_temp_new_i64(); \
be574920
TM
7390 tcg_gen_mov_i64(xah, cpu_vsrh(xA(ctx->opcode))); \
7391 tcg_gen_mov_i64(xal, cpu_vsrl(xA(ctx->opcode))); \
7392 tcg_gen_and_i64(xah, xah, sgm); \
7393 tcg_gen_and_i64(xal, xal, sgm); \
7394 tcg_gen_andc_i64(xbh, xbh, sgm); \
7395 tcg_gen_andc_i64(xbl, xbl, sgm); \
7396 tcg_gen_or_i64(xbh, xbh, xah); \
7397 tcg_gen_or_i64(xbl, xbl, xal); \
f976b09e
AG
7398 tcg_temp_free_i64(xah); \
7399 tcg_temp_free_i64(xal); \
be574920
TM
7400 break; \
7401 } \
7402 } \
7403 tcg_gen_mov_i64(cpu_vsrh(xT(ctx->opcode)), xbh); \
7404 tcg_gen_mov_i64(cpu_vsrl(xT(ctx->opcode)), xbl); \
f976b09e
AG
7405 tcg_temp_free_i64(xbh); \
7406 tcg_temp_free_i64(xbl); \
7407 tcg_temp_free_i64(sgm); \
be574920
TM
7408 }
7409
7410VSX_VECTOR_MOVE(xvabsdp, OP_ABS, SGN_MASK_DP)
7411VSX_VECTOR_MOVE(xvnabsdp, OP_NABS, SGN_MASK_DP)
7412VSX_VECTOR_MOVE(xvnegdp, OP_NEG, SGN_MASK_DP)
7413VSX_VECTOR_MOVE(xvcpsgndp, OP_CPSGN, SGN_MASK_DP)
7414VSX_VECTOR_MOVE(xvabssp, OP_ABS, SGN_MASK_SP)
7415VSX_VECTOR_MOVE(xvnabssp, OP_NABS, SGN_MASK_SP)
7416VSX_VECTOR_MOVE(xvnegsp, OP_NEG, SGN_MASK_SP)
7417VSX_VECTOR_MOVE(xvcpsgnsp, OP_CPSGN, SGN_MASK_SP)
7418
3c3cbbdc
TM
7419#define GEN_VSX_HELPER_2(name, op1, op2, inval, type) \
7420static void gen_##name(DisasContext * ctx) \
7421{ \
7422 TCGv_i32 opc; \
7423 if (unlikely(!ctx->vsx_enabled)) { \
7424 gen_exception(ctx, POWERPC_EXCP_VSXU); \
7425 return; \
7426 } \
7427 /* NIP cannot be restored if the memory exception comes from an helper */ \
7428 gen_update_nip(ctx, ctx->nip - 4); \
7429 opc = tcg_const_i32(ctx->opcode); \
7430 gen_helper_##name(cpu_env, opc); \
7431 tcg_temp_free_i32(opc); \
7432}
be574920 7433
3d1140bf
TM
7434#define GEN_VSX_HELPER_XT_XB_ENV(name, op1, op2, inval, type) \
7435static void gen_##name(DisasContext * ctx) \
7436{ \
7437 if (unlikely(!ctx->vsx_enabled)) { \
7438 gen_exception(ctx, POWERPC_EXCP_VSXU); \
7439 return; \
7440 } \
7441 /* NIP cannot be restored if the exception comes */ \
7442 /* from a helper. */ \
7443 gen_update_nip(ctx, ctx->nip - 4); \
7444 \
7445 gen_helper_##name(cpu_vsrh(xT(ctx->opcode)), cpu_env, \
7446 cpu_vsrh(xB(ctx->opcode))); \
7447}
7448
ee6e02c0
TM
7449GEN_VSX_HELPER_2(xsadddp, 0x00, 0x04, 0, PPC2_VSX)
7450GEN_VSX_HELPER_2(xssubdp, 0x00, 0x05, 0, PPC2_VSX)
5e591d88 7451GEN_VSX_HELPER_2(xsmuldp, 0x00, 0x06, 0, PPC2_VSX)
4b98eeef 7452GEN_VSX_HELPER_2(xsdivdp, 0x00, 0x07, 0, PPC2_VSX)
2009227f 7453GEN_VSX_HELPER_2(xsredp, 0x14, 0x05, 0, PPC2_VSX)
d32404fe 7454GEN_VSX_HELPER_2(xssqrtdp, 0x16, 0x04, 0, PPC2_VSX)
d3f9df8f 7455GEN_VSX_HELPER_2(xsrsqrtedp, 0x14, 0x04, 0, PPC2_VSX)
bc80838f 7456GEN_VSX_HELPER_2(xstdivdp, 0x14, 0x07, 0, PPC2_VSX)
5cb151ac 7457GEN_VSX_HELPER_2(xstsqrtdp, 0x14, 0x06, 0, PPC2_VSX)
595c6eef
TM
7458GEN_VSX_HELPER_2(xsmaddadp, 0x04, 0x04, 0, PPC2_VSX)
7459GEN_VSX_HELPER_2(xsmaddmdp, 0x04, 0x05, 0, PPC2_VSX)
7460GEN_VSX_HELPER_2(xsmsubadp, 0x04, 0x06, 0, PPC2_VSX)
7461GEN_VSX_HELPER_2(xsmsubmdp, 0x04, 0x07, 0, PPC2_VSX)
7462GEN_VSX_HELPER_2(xsnmaddadp, 0x04, 0x14, 0, PPC2_VSX)
7463GEN_VSX_HELPER_2(xsnmaddmdp, 0x04, 0x15, 0, PPC2_VSX)
7464GEN_VSX_HELPER_2(xsnmsubadp, 0x04, 0x16, 0, PPC2_VSX)
7465GEN_VSX_HELPER_2(xsnmsubmdp, 0x04, 0x17, 0, PPC2_VSX)
4f17e9c7
TM
7466GEN_VSX_HELPER_2(xscmpodp, 0x0C, 0x05, 0, PPC2_VSX)
7467GEN_VSX_HELPER_2(xscmpudp, 0x0C, 0x04, 0, PPC2_VSX)
959e9c9d
TM
7468GEN_VSX_HELPER_2(xsmaxdp, 0x00, 0x14, 0, PPC2_VSX)
7469GEN_VSX_HELPER_2(xsmindp, 0x00, 0x15, 0, PPC2_VSX)
ed8ac568 7470GEN_VSX_HELPER_2(xscvdpsp, 0x12, 0x10, 0, PPC2_VSX)
7ee19fb9 7471GEN_VSX_HELPER_XT_XB_ENV(xscvdpspn, 0x16, 0x10, 0, PPC2_VSX207)
ed8ac568 7472GEN_VSX_HELPER_2(xscvspdp, 0x12, 0x14, 0, PPC2_VSX)
7ee19fb9 7473GEN_VSX_HELPER_XT_XB_ENV(xscvspdpn, 0x16, 0x14, 0, PPC2_VSX207)
5177d2ca
TM
7474GEN_VSX_HELPER_2(xscvdpsxds, 0x10, 0x15, 0, PPC2_VSX)
7475GEN_VSX_HELPER_2(xscvdpsxws, 0x10, 0x05, 0, PPC2_VSX)
7476GEN_VSX_HELPER_2(xscvdpuxds, 0x10, 0x14, 0, PPC2_VSX)
7477GEN_VSX_HELPER_2(xscvdpuxws, 0x10, 0x04, 0, PPC2_VSX)
7478GEN_VSX_HELPER_2(xscvsxddp, 0x10, 0x17, 0, PPC2_VSX)
7479GEN_VSX_HELPER_2(xscvuxddp, 0x10, 0x16, 0, PPC2_VSX)
88e33d08
TM
7480GEN_VSX_HELPER_2(xsrdpi, 0x12, 0x04, 0, PPC2_VSX)
7481GEN_VSX_HELPER_2(xsrdpic, 0x16, 0x06, 0, PPC2_VSX)
7482GEN_VSX_HELPER_2(xsrdpim, 0x12, 0x07, 0, PPC2_VSX)
7483GEN_VSX_HELPER_2(xsrdpip, 0x12, 0x06, 0, PPC2_VSX)
7484GEN_VSX_HELPER_2(xsrdpiz, 0x12, 0x05, 0, PPC2_VSX)
3d1140bf 7485GEN_VSX_HELPER_XT_XB_ENV(xsrsp, 0x12, 0x11, 0, PPC2_VSX207)
ee6e02c0 7486
3fd0aadf
TM
7487GEN_VSX_HELPER_2(xsaddsp, 0x00, 0x00, 0, PPC2_VSX207)
7488GEN_VSX_HELPER_2(xssubsp, 0x00, 0x01, 0, PPC2_VSX207)
ab9408a2 7489GEN_VSX_HELPER_2(xsmulsp, 0x00, 0x02, 0, PPC2_VSX207)
b24d0b47 7490GEN_VSX_HELPER_2(xsdivsp, 0x00, 0x03, 0, PPC2_VSX207)
2c0c52ae 7491GEN_VSX_HELPER_2(xsresp, 0x14, 0x01, 0, PPC2_VSX207)
cea4e574 7492GEN_VSX_HELPER_2(xssqrtsp, 0x16, 0x00, 0, PPC2_VSX207)
968e76bc 7493GEN_VSX_HELPER_2(xsrsqrtesp, 0x14, 0x00, 0, PPC2_VSX207)
f53f81e0
TM
7494GEN_VSX_HELPER_2(xsmaddasp, 0x04, 0x00, 0, PPC2_VSX207)
7495GEN_VSX_HELPER_2(xsmaddmsp, 0x04, 0x01, 0, PPC2_VSX207)
7496GEN_VSX_HELPER_2(xsmsubasp, 0x04, 0x02, 0, PPC2_VSX207)
7497GEN_VSX_HELPER_2(xsmsubmsp, 0x04, 0x03, 0, PPC2_VSX207)
7498GEN_VSX_HELPER_2(xsnmaddasp, 0x04, 0x10, 0, PPC2_VSX207)
7499GEN_VSX_HELPER_2(xsnmaddmsp, 0x04, 0x11, 0, PPC2_VSX207)
7500GEN_VSX_HELPER_2(xsnmsubasp, 0x04, 0x12, 0, PPC2_VSX207)
7501GEN_VSX_HELPER_2(xsnmsubmsp, 0x04, 0x13, 0, PPC2_VSX207)
74698350
TM
7502GEN_VSX_HELPER_2(xscvsxdsp, 0x10, 0x13, 0, PPC2_VSX207)
7503GEN_VSX_HELPER_2(xscvuxdsp, 0x10, 0x12, 0, PPC2_VSX207)
3fd0aadf 7504
ee6e02c0
TM
7505GEN_VSX_HELPER_2(xvadddp, 0x00, 0x0C, 0, PPC2_VSX)
7506GEN_VSX_HELPER_2(xvsubdp, 0x00, 0x0D, 0, PPC2_VSX)
5e591d88 7507GEN_VSX_HELPER_2(xvmuldp, 0x00, 0x0E, 0, PPC2_VSX)
4b98eeef 7508GEN_VSX_HELPER_2(xvdivdp, 0x00, 0x0F, 0, PPC2_VSX)
2009227f 7509GEN_VSX_HELPER_2(xvredp, 0x14, 0x0D, 0, PPC2_VSX)
d32404fe 7510GEN_VSX_HELPER_2(xvsqrtdp, 0x16, 0x0C, 0, PPC2_VSX)
d3f9df8f 7511GEN_VSX_HELPER_2(xvrsqrtedp, 0x14, 0x0C, 0, PPC2_VSX)
bc80838f 7512GEN_VSX_HELPER_2(xvtdivdp, 0x14, 0x0F, 0, PPC2_VSX)
5cb151ac 7513GEN_VSX_HELPER_2(xvtsqrtdp, 0x14, 0x0E, 0, PPC2_VSX)
595c6eef
TM
7514GEN_VSX_HELPER_2(xvmaddadp, 0x04, 0x0C, 0, PPC2_VSX)
7515GEN_VSX_HELPER_2(xvmaddmdp, 0x04, 0x0D, 0, PPC2_VSX)
7516GEN_VSX_HELPER_2(xvmsubadp, 0x04, 0x0E, 0, PPC2_VSX)
7517GEN_VSX_HELPER_2(xvmsubmdp, 0x04, 0x0F, 0, PPC2_VSX)
7518GEN_VSX_HELPER_2(xvnmaddadp, 0x04, 0x1C, 0, PPC2_VSX)
7519GEN_VSX_HELPER_2(xvnmaddmdp, 0x04, 0x1D, 0, PPC2_VSX)
7520GEN_VSX_HELPER_2(xvnmsubadp, 0x04, 0x1E, 0, PPC2_VSX)
7521GEN_VSX_HELPER_2(xvnmsubmdp, 0x04, 0x1F, 0, PPC2_VSX)
959e9c9d
TM
7522GEN_VSX_HELPER_2(xvmaxdp, 0x00, 0x1C, 0, PPC2_VSX)
7523GEN_VSX_HELPER_2(xvmindp, 0x00, 0x1D, 0, PPC2_VSX)
354a6dec
TM
7524GEN_VSX_HELPER_2(xvcmpeqdp, 0x0C, 0x0C, 0, PPC2_VSX)
7525GEN_VSX_HELPER_2(xvcmpgtdp, 0x0C, 0x0D, 0, PPC2_VSX)
7526GEN_VSX_HELPER_2(xvcmpgedp, 0x0C, 0x0E, 0, PPC2_VSX)
ed8ac568 7527GEN_VSX_HELPER_2(xvcvdpsp, 0x12, 0x18, 0, PPC2_VSX)
5177d2ca
TM
7528GEN_VSX_HELPER_2(xvcvdpsxds, 0x10, 0x1D, 0, PPC2_VSX)
7529GEN_VSX_HELPER_2(xvcvdpsxws, 0x10, 0x0D, 0, PPC2_VSX)
7530GEN_VSX_HELPER_2(xvcvdpuxds, 0x10, 0x1C, 0, PPC2_VSX)
7531GEN_VSX_HELPER_2(xvcvdpuxws, 0x10, 0x0C, 0, PPC2_VSX)
7532GEN_VSX_HELPER_2(xvcvsxddp, 0x10, 0x1F, 0, PPC2_VSX)
7533GEN_VSX_HELPER_2(xvcvuxddp, 0x10, 0x1E, 0, PPC2_VSX)
7534GEN_VSX_HELPER_2(xvcvsxwdp, 0x10, 0x0F, 0, PPC2_VSX)
7535GEN_VSX_HELPER_2(xvcvuxwdp, 0x10, 0x0E, 0, PPC2_VSX)
88e33d08
TM
7536GEN_VSX_HELPER_2(xvrdpi, 0x12, 0x0C, 0, PPC2_VSX)
7537GEN_VSX_HELPER_2(xvrdpic, 0x16, 0x0E, 0, PPC2_VSX)
7538GEN_VSX_HELPER_2(xvrdpim, 0x12, 0x0F, 0, PPC2_VSX)
7539GEN_VSX_HELPER_2(xvrdpip, 0x12, 0x0E, 0, PPC2_VSX)
7540GEN_VSX_HELPER_2(xvrdpiz, 0x12, 0x0D, 0, PPC2_VSX)
ee6e02c0
TM
7541
7542GEN_VSX_HELPER_2(xvaddsp, 0x00, 0x08, 0, PPC2_VSX)
7543GEN_VSX_HELPER_2(xvsubsp, 0x00, 0x09, 0, PPC2_VSX)
5e591d88 7544GEN_VSX_HELPER_2(xvmulsp, 0x00, 0x0A, 0, PPC2_VSX)
4b98eeef 7545GEN_VSX_HELPER_2(xvdivsp, 0x00, 0x0B, 0, PPC2_VSX)
2009227f 7546GEN_VSX_HELPER_2(xvresp, 0x14, 0x09, 0, PPC2_VSX)
d32404fe 7547GEN_VSX_HELPER_2(xvsqrtsp, 0x16, 0x08, 0, PPC2_VSX)
d3f9df8f 7548GEN_VSX_HELPER_2(xvrsqrtesp, 0x14, 0x08, 0, PPC2_VSX)
bc80838f 7549GEN_VSX_HELPER_2(xvtdivsp, 0x14, 0x0B, 0, PPC2_VSX)
5cb151ac 7550GEN_VSX_HELPER_2(xvtsqrtsp, 0x14, 0x0A, 0, PPC2_VSX)
595c6eef
TM
7551GEN_VSX_HELPER_2(xvmaddasp, 0x04, 0x08, 0, PPC2_VSX)
7552GEN_VSX_HELPER_2(xvmaddmsp, 0x04, 0x09, 0, PPC2_VSX)
7553GEN_VSX_HELPER_2(xvmsubasp, 0x04, 0x0A, 0, PPC2_VSX)
7554GEN_VSX_HELPER_2(xvmsubmsp, 0x04, 0x0B, 0, PPC2_VSX)
7555GEN_VSX_HELPER_2(xvnmaddasp, 0x04, 0x18, 0, PPC2_VSX)
7556GEN_VSX_HELPER_2(xvnmaddmsp, 0x04, 0x19, 0, PPC2_VSX)
7557GEN_VSX_HELPER_2(xvnmsubasp, 0x04, 0x1A, 0, PPC2_VSX)
7558GEN_VSX_HELPER_2(xvnmsubmsp, 0x04, 0x1B, 0, PPC2_VSX)
959e9c9d
TM
7559GEN_VSX_HELPER_2(xvmaxsp, 0x00, 0x18, 0, PPC2_VSX)
7560GEN_VSX_HELPER_2(xvminsp, 0x00, 0x19, 0, PPC2_VSX)
354a6dec
TM
7561GEN_VSX_HELPER_2(xvcmpeqsp, 0x0C, 0x08, 0, PPC2_VSX)
7562GEN_VSX_HELPER_2(xvcmpgtsp, 0x0C, 0x09, 0, PPC2_VSX)
7563GEN_VSX_HELPER_2(xvcmpgesp, 0x0C, 0x0A, 0, PPC2_VSX)
ed8ac568 7564GEN_VSX_HELPER_2(xvcvspdp, 0x12, 0x1C, 0, PPC2_VSX)
5177d2ca
TM
7565GEN_VSX_HELPER_2(xvcvspsxds, 0x10, 0x19, 0, PPC2_VSX)
7566GEN_VSX_HELPER_2(xvcvspsxws, 0x10, 0x09, 0, PPC2_VSX)
7567GEN_VSX_HELPER_2(xvcvspuxds, 0x10, 0x18, 0, PPC2_VSX)
7568GEN_VSX_HELPER_2(xvcvspuxws, 0x10, 0x08, 0, PPC2_VSX)
7569GEN_VSX_HELPER_2(xvcvsxdsp, 0x10, 0x1B, 0, PPC2_VSX)
7570GEN_VSX_HELPER_2(xvcvuxdsp, 0x10, 0x1A, 0, PPC2_VSX)
7571GEN_VSX_HELPER_2(xvcvsxwsp, 0x10, 0x0B, 0, PPC2_VSX)
7572GEN_VSX_HELPER_2(xvcvuxwsp, 0x10, 0x0A, 0, PPC2_VSX)
88e33d08
TM
7573GEN_VSX_HELPER_2(xvrspi, 0x12, 0x08, 0, PPC2_VSX)
7574GEN_VSX_HELPER_2(xvrspic, 0x16, 0x0A, 0, PPC2_VSX)
7575GEN_VSX_HELPER_2(xvrspim, 0x12, 0x0B, 0, PPC2_VSX)
7576GEN_VSX_HELPER_2(xvrspip, 0x12, 0x0A, 0, PPC2_VSX)
7577GEN_VSX_HELPER_2(xvrspiz, 0x12, 0x09, 0, PPC2_VSX)
ee6e02c0 7578
79ca8a6a
TM
7579#define VSX_LOGICAL(name, tcg_op) \
7580static void glue(gen_, name)(DisasContext * ctx) \
7581 { \
7582 if (unlikely(!ctx->vsx_enabled)) { \
7583 gen_exception(ctx, POWERPC_EXCP_VSXU); \
7584 return; \
7585 } \
7586 tcg_op(cpu_vsrh(xT(ctx->opcode)), cpu_vsrh(xA(ctx->opcode)), \
7587 cpu_vsrh(xB(ctx->opcode))); \
7588 tcg_op(cpu_vsrl(xT(ctx->opcode)), cpu_vsrl(xA(ctx->opcode)), \
7589 cpu_vsrl(xB(ctx->opcode))); \
7590 }
7591
f976b09e
AG
7592VSX_LOGICAL(xxland, tcg_gen_and_i64)
7593VSX_LOGICAL(xxlandc, tcg_gen_andc_i64)
7594VSX_LOGICAL(xxlor, tcg_gen_or_i64)
7595VSX_LOGICAL(xxlxor, tcg_gen_xor_i64)
7596VSX_LOGICAL(xxlnor, tcg_gen_nor_i64)
67a33f37
TM
7597VSX_LOGICAL(xxleqv, tcg_gen_eqv_i64)
7598VSX_LOGICAL(xxlnand, tcg_gen_nand_i64)
7599VSX_LOGICAL(xxlorc, tcg_gen_orc_i64)
df020ce0 7600
ce577d2e
TM
7601#define VSX_XXMRG(name, high) \
7602static void glue(gen_, name)(DisasContext * ctx) \
7603 { \
7604 TCGv_i64 a0, a1, b0, b1; \
7605 if (unlikely(!ctx->vsx_enabled)) { \
7606 gen_exception(ctx, POWERPC_EXCP_VSXU); \
7607 return; \
7608 } \
f976b09e
AG
7609 a0 = tcg_temp_new_i64(); \
7610 a1 = tcg_temp_new_i64(); \
7611 b0 = tcg_temp_new_i64(); \
7612 b1 = tcg_temp_new_i64(); \
ce577d2e
TM
7613 if (high) { \
7614 tcg_gen_mov_i64(a0, cpu_vsrh(xA(ctx->opcode))); \
7615 tcg_gen_mov_i64(a1, cpu_vsrh(xA(ctx->opcode))); \
7616 tcg_gen_mov_i64(b0, cpu_vsrh(xB(ctx->opcode))); \
7617 tcg_gen_mov_i64(b1, cpu_vsrh(xB(ctx->opcode))); \
7618 } else { \
7619 tcg_gen_mov_i64(a0, cpu_vsrl(xA(ctx->opcode))); \
7620 tcg_gen_mov_i64(a1, cpu_vsrl(xA(ctx->opcode))); \
7621 tcg_gen_mov_i64(b0, cpu_vsrl(xB(ctx->opcode))); \
7622 tcg_gen_mov_i64(b1, cpu_vsrl(xB(ctx->opcode))); \
7623 } \
7624 tcg_gen_shri_i64(a0, a0, 32); \
7625 tcg_gen_shri_i64(b0, b0, 32); \
7626 tcg_gen_deposit_i64(cpu_vsrh(xT(ctx->opcode)), \
7627 b0, a0, 32, 32); \
7628 tcg_gen_deposit_i64(cpu_vsrl(xT(ctx->opcode)), \
7629 b1, a1, 32, 32); \
f976b09e
AG
7630 tcg_temp_free_i64(a0); \
7631 tcg_temp_free_i64(a1); \
7632 tcg_temp_free_i64(b0); \
7633 tcg_temp_free_i64(b1); \
ce577d2e
TM
7634 }
7635
7636VSX_XXMRG(xxmrghw, 1)
7637VSX_XXMRG(xxmrglw, 0)
7638
551e3ef7
TM
7639static void gen_xxsel(DisasContext * ctx)
7640{
7641 TCGv_i64 a, b, c;
7642 if (unlikely(!ctx->vsx_enabled)) {
7643 gen_exception(ctx, POWERPC_EXCP_VSXU);
7644 return;
7645 }
f976b09e
AG
7646 a = tcg_temp_new_i64();
7647 b = tcg_temp_new_i64();
7648 c = tcg_temp_new_i64();
551e3ef7
TM
7649
7650 tcg_gen_mov_i64(a, cpu_vsrh(xA(ctx->opcode)));
7651 tcg_gen_mov_i64(b, cpu_vsrh(xB(ctx->opcode)));
7652 tcg_gen_mov_i64(c, cpu_vsrh(xC(ctx->opcode)));
7653
7654 tcg_gen_and_i64(b, b, c);
7655 tcg_gen_andc_i64(a, a, c);
7656 tcg_gen_or_i64(cpu_vsrh(xT(ctx->opcode)), a, b);
7657
7658 tcg_gen_mov_i64(a, cpu_vsrl(xA(ctx->opcode)));
7659 tcg_gen_mov_i64(b, cpu_vsrl(xB(ctx->opcode)));
7660 tcg_gen_mov_i64(c, cpu_vsrl(xC(ctx->opcode)));
7661
7662 tcg_gen_and_i64(b, b, c);
7663 tcg_gen_andc_i64(a, a, c);
7664 tcg_gen_or_i64(cpu_vsrl(xT(ctx->opcode)), a, b);
7665
f976b09e
AG
7666 tcg_temp_free_i64(a);
7667 tcg_temp_free_i64(b);
7668 tcg_temp_free_i64(c);
551e3ef7
TM
7669}
7670
76c15fe0
TM
7671static void gen_xxspltw(DisasContext *ctx)
7672{
7673 TCGv_i64 b, b2;
7674 TCGv_i64 vsr = (UIM(ctx->opcode) & 2) ?
7675 cpu_vsrl(xB(ctx->opcode)) :
7676 cpu_vsrh(xB(ctx->opcode));
7677
7678 if (unlikely(!ctx->vsx_enabled)) {
7679 gen_exception(ctx, POWERPC_EXCP_VSXU);
7680 return;
7681 }
7682
f976b09e
AG
7683 b = tcg_temp_new_i64();
7684 b2 = tcg_temp_new_i64();
76c15fe0
TM
7685
7686 if (UIM(ctx->opcode) & 1) {
7687 tcg_gen_ext32u_i64(b, vsr);
7688 } else {
7689 tcg_gen_shri_i64(b, vsr, 32);
7690 }
7691
7692 tcg_gen_shli_i64(b2, b, 32);
7693 tcg_gen_or_i64(cpu_vsrh(xT(ctx->opcode)), b, b2);
7694 tcg_gen_mov_i64(cpu_vsrl(xT(ctx->opcode)), cpu_vsrh(xT(ctx->opcode)));
7695
f976b09e
AG
7696 tcg_temp_free_i64(b);
7697 tcg_temp_free_i64(b2);
76c15fe0
TM
7698}
7699
acc42968
TM
7700static void gen_xxsldwi(DisasContext *ctx)
7701{
7702 TCGv_i64 xth, xtl;
7703 if (unlikely(!ctx->vsx_enabled)) {
7704 gen_exception(ctx, POWERPC_EXCP_VSXU);
7705 return;
7706 }
f976b09e
AG
7707 xth = tcg_temp_new_i64();
7708 xtl = tcg_temp_new_i64();
acc42968
TM
7709
7710 switch (SHW(ctx->opcode)) {
7711 case 0: {
7712 tcg_gen_mov_i64(xth, cpu_vsrh(xA(ctx->opcode)));
7713 tcg_gen_mov_i64(xtl, cpu_vsrl(xA(ctx->opcode)));
7714 break;
7715 }
7716 case 1: {
f976b09e 7717 TCGv_i64 t0 = tcg_temp_new_i64();
acc42968
TM
7718 tcg_gen_mov_i64(xth, cpu_vsrh(xA(ctx->opcode)));
7719 tcg_gen_shli_i64(xth, xth, 32);
7720 tcg_gen_mov_i64(t0, cpu_vsrl(xA(ctx->opcode)));
7721 tcg_gen_shri_i64(t0, t0, 32);
7722 tcg_gen_or_i64(xth, xth, t0);
7723 tcg_gen_mov_i64(xtl, cpu_vsrl(xA(ctx->opcode)));
7724 tcg_gen_shli_i64(xtl, xtl, 32);
7725 tcg_gen_mov_i64(t0, cpu_vsrh(xB(ctx->opcode)));
7726 tcg_gen_shri_i64(t0, t0, 32);
7727 tcg_gen_or_i64(xtl, xtl, t0);
f976b09e 7728 tcg_temp_free_i64(t0);
acc42968
TM
7729 break;
7730 }
7731 case 2: {
7732 tcg_gen_mov_i64(xth, cpu_vsrl(xA(ctx->opcode)));
7733 tcg_gen_mov_i64(xtl, cpu_vsrh(xB(ctx->opcode)));
7734 break;
7735 }
7736 case 3: {
f976b09e 7737 TCGv_i64 t0 = tcg_temp_new_i64();
acc42968
TM
7738 tcg_gen_mov_i64(xth, cpu_vsrl(xA(ctx->opcode)));
7739 tcg_gen_shli_i64(xth, xth, 32);
7740 tcg_gen_mov_i64(t0, cpu_vsrh(xB(ctx->opcode)));
7741 tcg_gen_shri_i64(t0, t0, 32);
7742 tcg_gen_or_i64(xth, xth, t0);
7743 tcg_gen_mov_i64(xtl, cpu_vsrh(xB(ctx->opcode)));
7744 tcg_gen_shli_i64(xtl, xtl, 32);
7745 tcg_gen_mov_i64(t0, cpu_vsrl(xB(ctx->opcode)));
7746 tcg_gen_shri_i64(t0, t0, 32);
7747 tcg_gen_or_i64(xtl, xtl, t0);
f976b09e 7748 tcg_temp_free_i64(t0);
acc42968
TM
7749 break;
7750 }
7751 }
7752
7753 tcg_gen_mov_i64(cpu_vsrh(xT(ctx->opcode)), xth);
7754 tcg_gen_mov_i64(cpu_vsrl(xT(ctx->opcode)), xtl);
7755
f976b09e
AG
7756 tcg_temp_free_i64(xth);
7757 tcg_temp_free_i64(xtl);
acc42968
TM
7758}
7759
ce577d2e 7760
0487d6a8 7761/*** SPE extension ***/
0487d6a8 7762/* Register moves */
3cd7d1dd 7763
a0e13900
FC
7764static inline void gen_evmra(DisasContext *ctx)
7765{
7766
7767 if (unlikely(!ctx->spe_enabled)) {
27a69bb0 7768 gen_exception(ctx, POWERPC_EXCP_SPEU);
a0e13900
FC
7769 return;
7770 }
7771
7772#if defined(TARGET_PPC64)
7773 /* rD := rA */
7774 tcg_gen_mov_i64(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)]);
7775
7776 /* spe_acc := rA */
7777 tcg_gen_st_i64(cpu_gpr[rA(ctx->opcode)],
7778 cpu_env,
1328c2bf 7779 offsetof(CPUPPCState, spe_acc));
a0e13900
FC
7780#else
7781 TCGv_i64 tmp = tcg_temp_new_i64();
7782
7783 /* tmp := rA_lo + rA_hi << 32 */
7784 tcg_gen_concat_i32_i64(tmp, cpu_gpr[rA(ctx->opcode)], cpu_gprh[rA(ctx->opcode)]);
7785
7786 /* spe_acc := tmp */
1328c2bf 7787 tcg_gen_st_i64(tmp, cpu_env, offsetof(CPUPPCState, spe_acc));
a0e13900
FC
7788 tcg_temp_free_i64(tmp);
7789
7790 /* rD := rA */
7791 tcg_gen_mov_i32(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)]);
7792 tcg_gen_mov_i32(cpu_gprh[rD(ctx->opcode)], cpu_gprh[rA(ctx->opcode)]);
7793#endif
7794}
7795
636aa200
BS
7796static inline void gen_load_gpr64(TCGv_i64 t, int reg)
7797{
f78fb44e
AJ
7798#if defined(TARGET_PPC64)
7799 tcg_gen_mov_i64(t, cpu_gpr[reg]);
7800#else
36aa55dc 7801 tcg_gen_concat_i32_i64(t, cpu_gpr[reg], cpu_gprh[reg]);
3cd7d1dd 7802#endif
f78fb44e 7803}
3cd7d1dd 7804
636aa200
BS
7805static inline void gen_store_gpr64(int reg, TCGv_i64 t)
7806{
f78fb44e
AJ
7807#if defined(TARGET_PPC64)
7808 tcg_gen_mov_i64(cpu_gpr[reg], t);
7809#else
a7812ae4 7810 TCGv_i64 tmp = tcg_temp_new_i64();
f78fb44e 7811 tcg_gen_trunc_i64_i32(cpu_gpr[reg], t);
f78fb44e
AJ
7812 tcg_gen_shri_i64(tmp, t, 32);
7813 tcg_gen_trunc_i64_i32(cpu_gprh[reg], tmp);
a7812ae4 7814 tcg_temp_free_i64(tmp);
3cd7d1dd 7815#endif
f78fb44e 7816}
3cd7d1dd 7817
70560da7 7818#define GEN_SPE(name0, name1, opc2, opc3, inval0, inval1, type) \
99e300ef 7819static void glue(gen_, name0##_##name1)(DisasContext *ctx) \
0487d6a8
JM
7820{ \
7821 if (Rc(ctx->opcode)) \
7822 gen_##name1(ctx); \
7823 else \
7824 gen_##name0(ctx); \
7825}
7826
7827/* Handler for undefined SPE opcodes */
636aa200 7828static inline void gen_speundef(DisasContext *ctx)
0487d6a8 7829{
e06fcd75 7830 gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL);
0487d6a8
JM
7831}
7832
57951c27
AJ
7833/* SPE logic */
7834#if defined(TARGET_PPC64)
7835#define GEN_SPEOP_LOGIC2(name, tcg_op) \
636aa200 7836static inline void gen_##name(DisasContext *ctx) \
0487d6a8
JM
7837{ \
7838 if (unlikely(!ctx->spe_enabled)) { \
27a69bb0 7839 gen_exception(ctx, POWERPC_EXCP_SPEU); \
0487d6a8
JM
7840 return; \
7841 } \
57951c27
AJ
7842 tcg_op(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)], \
7843 cpu_gpr[rB(ctx->opcode)]); \
7844}
7845#else
7846#define GEN_SPEOP_LOGIC2(name, tcg_op) \
636aa200 7847static inline void gen_##name(DisasContext *ctx) \
57951c27
AJ
7848{ \
7849 if (unlikely(!ctx->spe_enabled)) { \
27a69bb0 7850 gen_exception(ctx, POWERPC_EXCP_SPEU); \
57951c27
AJ
7851 return; \
7852 } \
7853 tcg_op(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)], \
7854 cpu_gpr[rB(ctx->opcode)]); \
7855 tcg_op(cpu_gprh[rD(ctx->opcode)], cpu_gprh[rA(ctx->opcode)], \
7856 cpu_gprh[rB(ctx->opcode)]); \
0487d6a8 7857}
57951c27
AJ
7858#endif
7859
7860GEN_SPEOP_LOGIC2(evand, tcg_gen_and_tl);
7861GEN_SPEOP_LOGIC2(evandc, tcg_gen_andc_tl);
7862GEN_SPEOP_LOGIC2(evxor, tcg_gen_xor_tl);
7863GEN_SPEOP_LOGIC2(evor, tcg_gen_or_tl);
7864GEN_SPEOP_LOGIC2(evnor, tcg_gen_nor_tl);
7865GEN_SPEOP_LOGIC2(eveqv, tcg_gen_eqv_tl);
7866GEN_SPEOP_LOGIC2(evorc, tcg_gen_orc_tl);
7867GEN_SPEOP_LOGIC2(evnand, tcg_gen_nand_tl);
0487d6a8 7868
57951c27
AJ
7869/* SPE logic immediate */
7870#if defined(TARGET_PPC64)
7871#define GEN_SPEOP_TCG_LOGIC_IMM2(name, tcg_opi) \
636aa200 7872static inline void gen_##name(DisasContext *ctx) \
3d3a6a0a
AJ
7873{ \
7874 if (unlikely(!ctx->spe_enabled)) { \
27a69bb0 7875 gen_exception(ctx, POWERPC_EXCP_SPEU); \
3d3a6a0a
AJ
7876 return; \
7877 } \
a7812ae4
PB
7878 TCGv_i32 t0 = tcg_temp_local_new_i32(); \
7879 TCGv_i32 t1 = tcg_temp_local_new_i32(); \
7880 TCGv_i64 t2 = tcg_temp_local_new_i64(); \
57951c27
AJ
7881 tcg_gen_trunc_i64_i32(t0, cpu_gpr[rA(ctx->opcode)]); \
7882 tcg_opi(t0, t0, rB(ctx->opcode)); \
7883 tcg_gen_shri_i64(t2, cpu_gpr[rA(ctx->opcode)], 32); \
7884 tcg_gen_trunc_i64_i32(t1, t2); \
a7812ae4 7885 tcg_temp_free_i64(t2); \
57951c27
AJ
7886 tcg_opi(t1, t1, rB(ctx->opcode)); \
7887 tcg_gen_concat_i32_i64(cpu_gpr[rD(ctx->opcode)], t0, t1); \
a7812ae4
PB
7888 tcg_temp_free_i32(t0); \
7889 tcg_temp_free_i32(t1); \
3d3a6a0a 7890}
57951c27
AJ
7891#else
7892#define GEN_SPEOP_TCG_LOGIC_IMM2(name, tcg_opi) \
636aa200 7893static inline void gen_##name(DisasContext *ctx) \
0487d6a8
JM
7894{ \
7895 if (unlikely(!ctx->spe_enabled)) { \
27a69bb0 7896 gen_exception(ctx, POWERPC_EXCP_SPEU); \
0487d6a8
JM
7897 return; \
7898 } \
57951c27
AJ
7899 tcg_opi(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)], \
7900 rB(ctx->opcode)); \
7901 tcg_opi(cpu_gprh[rD(ctx->opcode)], cpu_gprh[rA(ctx->opcode)], \
7902 rB(ctx->opcode)); \
0487d6a8 7903}
57951c27
AJ
7904#endif
7905GEN_SPEOP_TCG_LOGIC_IMM2(evslwi, tcg_gen_shli_i32);
7906GEN_SPEOP_TCG_LOGIC_IMM2(evsrwiu, tcg_gen_shri_i32);
7907GEN_SPEOP_TCG_LOGIC_IMM2(evsrwis, tcg_gen_sari_i32);
7908GEN_SPEOP_TCG_LOGIC_IMM2(evrlwi, tcg_gen_rotli_i32);
0487d6a8 7909
57951c27
AJ
7910/* SPE arithmetic */
7911#if defined(TARGET_PPC64)
7912#define GEN_SPEOP_ARITH1(name, tcg_op) \
636aa200 7913static inline void gen_##name(DisasContext *ctx) \
0487d6a8
JM
7914{ \
7915 if (unlikely(!ctx->spe_enabled)) { \
27a69bb0 7916 gen_exception(ctx, POWERPC_EXCP_SPEU); \
0487d6a8
JM
7917 return; \
7918 } \
a7812ae4
PB
7919 TCGv_i32 t0 = tcg_temp_local_new_i32(); \
7920 TCGv_i32 t1 = tcg_temp_local_new_i32(); \
7921 TCGv_i64 t2 = tcg_temp_local_new_i64(); \
57951c27
AJ
7922 tcg_gen_trunc_i64_i32(t0, cpu_gpr[rA(ctx->opcode)]); \
7923 tcg_op(t0, t0); \
7924 tcg_gen_shri_i64(t2, cpu_gpr[rA(ctx->opcode)], 32); \
7925 tcg_gen_trunc_i64_i32(t1, t2); \
a7812ae4 7926 tcg_temp_free_i64(t2); \
57951c27
AJ
7927 tcg_op(t1, t1); \
7928 tcg_gen_concat_i32_i64(cpu_gpr[rD(ctx->opcode)], t0, t1); \
a7812ae4
PB
7929 tcg_temp_free_i32(t0); \
7930 tcg_temp_free_i32(t1); \
0487d6a8 7931}
57951c27 7932#else
a7812ae4 7933#define GEN_SPEOP_ARITH1(name, tcg_op) \
636aa200 7934static inline void gen_##name(DisasContext *ctx) \
57951c27
AJ
7935{ \
7936 if (unlikely(!ctx->spe_enabled)) { \
27a69bb0 7937 gen_exception(ctx, POWERPC_EXCP_SPEU); \
57951c27
AJ
7938 return; \
7939 } \
7940 tcg_op(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)]); \
7941 tcg_op(cpu_gprh[rD(ctx->opcode)], cpu_gprh[rA(ctx->opcode)]); \
7942}
7943#endif
0487d6a8 7944
636aa200 7945static inline void gen_op_evabs(TCGv_i32 ret, TCGv_i32 arg1)
57951c27
AJ
7946{
7947 int l1 = gen_new_label();
7948 int l2 = gen_new_label();
0487d6a8 7949
57951c27
AJ
7950 tcg_gen_brcondi_i32(TCG_COND_GE, arg1, 0, l1);
7951 tcg_gen_neg_i32(ret, arg1);
7952 tcg_gen_br(l2);
7953 gen_set_label(l1);
a7812ae4 7954 tcg_gen_mov_i32(ret, arg1);
57951c27
AJ
7955 gen_set_label(l2);
7956}
7957GEN_SPEOP_ARITH1(evabs, gen_op_evabs);
7958GEN_SPEOP_ARITH1(evneg, tcg_gen_neg_i32);
7959GEN_SPEOP_ARITH1(evextsb, tcg_gen_ext8s_i32);
7960GEN_SPEOP_ARITH1(evextsh, tcg_gen_ext16s_i32);
636aa200 7961static inline void gen_op_evrndw(TCGv_i32 ret, TCGv_i32 arg1)
0487d6a8 7962{
57951c27
AJ
7963 tcg_gen_addi_i32(ret, arg1, 0x8000);
7964 tcg_gen_ext16u_i32(ret, ret);
7965}
7966GEN_SPEOP_ARITH1(evrndw, gen_op_evrndw);
a7812ae4
PB
7967GEN_SPEOP_ARITH1(evcntlsw, gen_helper_cntlsw32);
7968GEN_SPEOP_ARITH1(evcntlzw, gen_helper_cntlzw32);
0487d6a8 7969
57951c27
AJ
7970#if defined(TARGET_PPC64)
7971#define GEN_SPEOP_ARITH2(name, tcg_op) \
636aa200 7972static inline void gen_##name(DisasContext *ctx) \
0487d6a8
JM
7973{ \
7974 if (unlikely(!ctx->spe_enabled)) { \
27a69bb0 7975 gen_exception(ctx, POWERPC_EXCP_SPEU); \
0487d6a8
JM
7976 return; \
7977 } \
a7812ae4
PB
7978 TCGv_i32 t0 = tcg_temp_local_new_i32(); \
7979 TCGv_i32 t1 = tcg_temp_local_new_i32(); \
7980 TCGv_i32 t2 = tcg_temp_local_new_i32(); \
501e23c4 7981 TCGv_i64 t3 = tcg_temp_local_new_i64(); \
57951c27
AJ
7982 tcg_gen_trunc_i64_i32(t0, cpu_gpr[rA(ctx->opcode)]); \
7983 tcg_gen_trunc_i64_i32(t2, cpu_gpr[rB(ctx->opcode)]); \
7984 tcg_op(t0, t0, t2); \
7985 tcg_gen_shri_i64(t3, cpu_gpr[rA(ctx->opcode)], 32); \
7986 tcg_gen_trunc_i64_i32(t1, t3); \
7987 tcg_gen_shri_i64(t3, cpu_gpr[rB(ctx->opcode)], 32); \
7988 tcg_gen_trunc_i64_i32(t2, t3); \
a7812ae4 7989 tcg_temp_free_i64(t3); \
57951c27 7990 tcg_op(t1, t1, t2); \
a7812ae4 7991 tcg_temp_free_i32(t2); \
57951c27 7992 tcg_gen_concat_i32_i64(cpu_gpr[rD(ctx->opcode)], t0, t1); \
a7812ae4
PB
7993 tcg_temp_free_i32(t0); \
7994 tcg_temp_free_i32(t1); \
0487d6a8 7995}
57951c27
AJ
7996#else
7997#define GEN_SPEOP_ARITH2(name, tcg_op) \
636aa200 7998static inline void gen_##name(DisasContext *ctx) \
0487d6a8
JM
7999{ \
8000 if (unlikely(!ctx->spe_enabled)) { \
27a69bb0 8001 gen_exception(ctx, POWERPC_EXCP_SPEU); \
0487d6a8
JM
8002 return; \
8003 } \
57951c27
AJ
8004 tcg_op(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)], \
8005 cpu_gpr[rB(ctx->opcode)]); \
8006 tcg_op(cpu_gprh[rD(ctx->opcode)], cpu_gprh[rA(ctx->opcode)], \
8007 cpu_gprh[rB(ctx->opcode)]); \
0487d6a8 8008}
57951c27 8009#endif
0487d6a8 8010
636aa200 8011static inline void gen_op_evsrwu(TCGv_i32 ret, TCGv_i32 arg1, TCGv_i32 arg2)
57951c27 8012{
a7812ae4 8013 TCGv_i32 t0;
57951c27 8014 int l1, l2;
0487d6a8 8015
57951c27
AJ
8016 l1 = gen_new_label();
8017 l2 = gen_new_label();
a7812ae4 8018 t0 = tcg_temp_local_new_i32();
57951c27
AJ
8019 /* No error here: 6 bits are used */
8020 tcg_gen_andi_i32(t0, arg2, 0x3F);
8021 tcg_gen_brcondi_i32(TCG_COND_GE, t0, 32, l1);
8022 tcg_gen_shr_i32(ret, arg1, t0);
8023 tcg_gen_br(l2);
8024 gen_set_label(l1);
8025 tcg_gen_movi_i32(ret, 0);
0aef4261 8026 gen_set_label(l2);
a7812ae4 8027 tcg_temp_free_i32(t0);
57951c27
AJ
8028}
8029GEN_SPEOP_ARITH2(evsrwu, gen_op_evsrwu);
636aa200 8030static inline void gen_op_evsrws(TCGv_i32 ret, TCGv_i32 arg1, TCGv_i32 arg2)
57951c27 8031{
a7812ae4 8032 TCGv_i32 t0;
57951c27
AJ
8033 int l1, l2;
8034
8035 l1 = gen_new_label();
8036 l2 = gen_new_label();
a7812ae4 8037 t0 = tcg_temp_local_new_i32();
57951c27
AJ
8038 /* No error here: 6 bits are used */
8039 tcg_gen_andi_i32(t0, arg2, 0x3F);
8040 tcg_gen_brcondi_i32(TCG_COND_GE, t0, 32, l1);
8041 tcg_gen_sar_i32(ret, arg1, t0);
8042 tcg_gen_br(l2);
8043 gen_set_label(l1);
8044 tcg_gen_movi_i32(ret, 0);
0aef4261 8045 gen_set_label(l2);
a7812ae4 8046 tcg_temp_free_i32(t0);
57951c27
AJ
8047}
8048GEN_SPEOP_ARITH2(evsrws, gen_op_evsrws);
636aa200 8049static inline void gen_op_evslw(TCGv_i32 ret, TCGv_i32 arg1, TCGv_i32 arg2)
57951c27 8050{
a7812ae4 8051 TCGv_i32 t0;
57951c27
AJ
8052 int l1, l2;
8053
8054 l1 = gen_new_label();
8055 l2 = gen_new_label();
a7812ae4 8056 t0 = tcg_temp_local_new_i32();
57951c27
AJ
8057 /* No error here: 6 bits are used */
8058 tcg_gen_andi_i32(t0, arg2, 0x3F);
8059 tcg_gen_brcondi_i32(TCG_COND_GE, t0, 32, l1);
8060 tcg_gen_shl_i32(ret, arg1, t0);
8061 tcg_gen_br(l2);
8062 gen_set_label(l1);
8063 tcg_gen_movi_i32(ret, 0);
e29ef9fa 8064 gen_set_label(l2);
a7812ae4 8065 tcg_temp_free_i32(t0);
57951c27
AJ
8066}
8067GEN_SPEOP_ARITH2(evslw, gen_op_evslw);
636aa200 8068static inline void gen_op_evrlw(TCGv_i32 ret, TCGv_i32 arg1, TCGv_i32 arg2)
57951c27 8069{
a7812ae4 8070 TCGv_i32 t0 = tcg_temp_new_i32();
57951c27
AJ
8071 tcg_gen_andi_i32(t0, arg2, 0x1F);
8072 tcg_gen_rotl_i32(ret, arg1, t0);
a7812ae4 8073 tcg_temp_free_i32(t0);
57951c27
AJ
8074}
8075GEN_SPEOP_ARITH2(evrlw, gen_op_evrlw);
636aa200 8076static inline void gen_evmergehi(DisasContext *ctx)
57951c27
AJ
8077{
8078 if (unlikely(!ctx->spe_enabled)) {
27a69bb0 8079 gen_exception(ctx, POWERPC_EXCP_SPEU);
57951c27
AJ
8080 return;
8081 }
8082#if defined(TARGET_PPC64)
a7812ae4
PB
8083 TCGv t0 = tcg_temp_new();
8084 TCGv t1 = tcg_temp_new();
57951c27
AJ
8085 tcg_gen_shri_tl(t0, cpu_gpr[rB(ctx->opcode)], 32);
8086 tcg_gen_andi_tl(t1, cpu_gpr[rA(ctx->opcode)], 0xFFFFFFFF0000000ULL);
8087 tcg_gen_or_tl(cpu_gpr[rD(ctx->opcode)], t0, t1);
8088 tcg_temp_free(t0);
8089 tcg_temp_free(t1);
8090#else
8091 tcg_gen_mov_i32(cpu_gpr[rD(ctx->opcode)], cpu_gprh[rB(ctx->opcode)]);
8092 tcg_gen_mov_i32(cpu_gprh[rD(ctx->opcode)], cpu_gprh[rA(ctx->opcode)]);
8093#endif
8094}
8095GEN_SPEOP_ARITH2(evaddw, tcg_gen_add_i32);
636aa200 8096static inline void gen_op_evsubf(TCGv_i32 ret, TCGv_i32 arg1, TCGv_i32 arg2)
0487d6a8 8097{
57951c27
AJ
8098 tcg_gen_sub_i32(ret, arg2, arg1);
8099}
8100GEN_SPEOP_ARITH2(evsubfw, gen_op_evsubf);
0487d6a8 8101
57951c27
AJ
8102/* SPE arithmetic immediate */
8103#if defined(TARGET_PPC64)
8104#define GEN_SPEOP_ARITH_IMM2(name, tcg_op) \
636aa200 8105static inline void gen_##name(DisasContext *ctx) \
57951c27
AJ
8106{ \
8107 if (unlikely(!ctx->spe_enabled)) { \
27a69bb0 8108 gen_exception(ctx, POWERPC_EXCP_SPEU); \
57951c27
AJ
8109 return; \
8110 } \
a7812ae4
PB
8111 TCGv_i32 t0 = tcg_temp_local_new_i32(); \
8112 TCGv_i32 t1 = tcg_temp_local_new_i32(); \
8113 TCGv_i64 t2 = tcg_temp_local_new_i64(); \
57951c27
AJ
8114 tcg_gen_trunc_i64_i32(t0, cpu_gpr[rB(ctx->opcode)]); \
8115 tcg_op(t0, t0, rA(ctx->opcode)); \
8116 tcg_gen_shri_i64(t2, cpu_gpr[rB(ctx->opcode)], 32); \
8117 tcg_gen_trunc_i64_i32(t1, t2); \
e06fcd75 8118 tcg_temp_free_i64(t2); \
57951c27
AJ
8119 tcg_op(t1, t1, rA(ctx->opcode)); \
8120 tcg_gen_concat_i32_i64(cpu_gpr[rD(ctx->opcode)], t0, t1); \
a7812ae4
PB
8121 tcg_temp_free_i32(t0); \
8122 tcg_temp_free_i32(t1); \
57951c27
AJ
8123}
8124#else
8125#define GEN_SPEOP_ARITH_IMM2(name, tcg_op) \
636aa200 8126static inline void gen_##name(DisasContext *ctx) \
57951c27
AJ
8127{ \
8128 if (unlikely(!ctx->spe_enabled)) { \
27a69bb0 8129 gen_exception(ctx, POWERPC_EXCP_SPEU); \
57951c27
AJ
8130 return; \
8131 } \
8132 tcg_op(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rB(ctx->opcode)], \
8133 rA(ctx->opcode)); \
8134 tcg_op(cpu_gprh[rD(ctx->opcode)], cpu_gprh[rB(ctx->opcode)], \
8135 rA(ctx->opcode)); \
8136}
8137#endif
8138GEN_SPEOP_ARITH_IMM2(evaddiw, tcg_gen_addi_i32);
8139GEN_SPEOP_ARITH_IMM2(evsubifw, tcg_gen_subi_i32);
8140
8141/* SPE comparison */
8142#if defined(TARGET_PPC64)
8143#define GEN_SPEOP_COMP(name, tcg_cond) \
636aa200 8144static inline void gen_##name(DisasContext *ctx) \
57951c27
AJ
8145{ \
8146 if (unlikely(!ctx->spe_enabled)) { \
27a69bb0 8147 gen_exception(ctx, POWERPC_EXCP_SPEU); \
57951c27
AJ
8148 return; \
8149 } \
8150 int l1 = gen_new_label(); \
8151 int l2 = gen_new_label(); \
8152 int l3 = gen_new_label(); \
8153 int l4 = gen_new_label(); \
a7812ae4
PB
8154 TCGv_i32 t0 = tcg_temp_local_new_i32(); \
8155 TCGv_i32 t1 = tcg_temp_local_new_i32(); \
8156 TCGv_i64 t2 = tcg_temp_local_new_i64(); \
57951c27
AJ
8157 tcg_gen_trunc_i64_i32(t0, cpu_gpr[rA(ctx->opcode)]); \
8158 tcg_gen_trunc_i64_i32(t1, cpu_gpr[rB(ctx->opcode)]); \
8159 tcg_gen_brcond_i32(tcg_cond, t0, t1, l1); \
a7812ae4 8160 tcg_gen_movi_i32(cpu_crf[crfD(ctx->opcode)], 0); \
57951c27
AJ
8161 tcg_gen_br(l2); \
8162 gen_set_label(l1); \
8163 tcg_gen_movi_i32(cpu_crf[crfD(ctx->opcode)], \
8164 CRF_CL | CRF_CH_OR_CL | CRF_CH_AND_CL); \
8165 gen_set_label(l2); \
8166 tcg_gen_shri_i64(t2, cpu_gpr[rA(ctx->opcode)], 32); \
8167 tcg_gen_trunc_i64_i32(t0, t2); \
8168 tcg_gen_shri_i64(t2, cpu_gpr[rB(ctx->opcode)], 32); \
8169 tcg_gen_trunc_i64_i32(t1, t2); \
a7812ae4 8170 tcg_temp_free_i64(t2); \
57951c27
AJ
8171 tcg_gen_brcond_i32(tcg_cond, t0, t1, l3); \
8172 tcg_gen_andi_i32(cpu_crf[crfD(ctx->opcode)], cpu_crf[crfD(ctx->opcode)], \
8173 ~(CRF_CH | CRF_CH_AND_CL)); \
8174 tcg_gen_br(l4); \
8175 gen_set_label(l3); \
8176 tcg_gen_ori_i32(cpu_crf[crfD(ctx->opcode)], cpu_crf[crfD(ctx->opcode)], \
8177 CRF_CH | CRF_CH_OR_CL); \
8178 gen_set_label(l4); \
a7812ae4
PB
8179 tcg_temp_free_i32(t0); \
8180 tcg_temp_free_i32(t1); \
57951c27
AJ
8181}
8182#else
8183#define GEN_SPEOP_COMP(name, tcg_cond) \
636aa200 8184static inline void gen_##name(DisasContext *ctx) \
57951c27
AJ
8185{ \
8186 if (unlikely(!ctx->spe_enabled)) { \
27a69bb0 8187 gen_exception(ctx, POWERPC_EXCP_SPEU); \
57951c27
AJ
8188 return; \
8189 } \
8190 int l1 = gen_new_label(); \
8191 int l2 = gen_new_label(); \
8192 int l3 = gen_new_label(); \
8193 int l4 = gen_new_label(); \
8194 \
8195 tcg_gen_brcond_i32(tcg_cond, cpu_gpr[rA(ctx->opcode)], \
8196 cpu_gpr[rB(ctx->opcode)], l1); \
8197 tcg_gen_movi_tl(cpu_crf[crfD(ctx->opcode)], 0); \
8198 tcg_gen_br(l2); \
8199 gen_set_label(l1); \
8200 tcg_gen_movi_i32(cpu_crf[crfD(ctx->opcode)], \
8201 CRF_CL | CRF_CH_OR_CL | CRF_CH_AND_CL); \
8202 gen_set_label(l2); \
8203 tcg_gen_brcond_i32(tcg_cond, cpu_gprh[rA(ctx->opcode)], \
8204 cpu_gprh[rB(ctx->opcode)], l3); \
8205 tcg_gen_andi_i32(cpu_crf[crfD(ctx->opcode)], cpu_crf[crfD(ctx->opcode)], \
8206 ~(CRF_CH | CRF_CH_AND_CL)); \
8207 tcg_gen_br(l4); \
8208 gen_set_label(l3); \
8209 tcg_gen_ori_i32(cpu_crf[crfD(ctx->opcode)], cpu_crf[crfD(ctx->opcode)], \
8210 CRF_CH | CRF_CH_OR_CL); \
8211 gen_set_label(l4); \
8212}
8213#endif
8214GEN_SPEOP_COMP(evcmpgtu, TCG_COND_GTU);
8215GEN_SPEOP_COMP(evcmpgts, TCG_COND_GT);
8216GEN_SPEOP_COMP(evcmpltu, TCG_COND_LTU);
8217GEN_SPEOP_COMP(evcmplts, TCG_COND_LT);
8218GEN_SPEOP_COMP(evcmpeq, TCG_COND_EQ);
8219
8220/* SPE misc */
636aa200 8221static inline void gen_brinc(DisasContext *ctx)
57951c27
AJ
8222{
8223 /* Note: brinc is usable even if SPE is disabled */
a7812ae4
PB
8224 gen_helper_brinc(cpu_gpr[rD(ctx->opcode)],
8225 cpu_gpr[rA(ctx->opcode)], cpu_gpr[rB(ctx->opcode)]);
0487d6a8 8226}
636aa200 8227static inline void gen_evmergelo(DisasContext *ctx)
57951c27
AJ
8228{
8229 if (unlikely(!ctx->spe_enabled)) {
27a69bb0 8230 gen_exception(ctx, POWERPC_EXCP_SPEU);
57951c27
AJ
8231 return;
8232 }
8233#if defined(TARGET_PPC64)
a7812ae4
PB
8234 TCGv t0 = tcg_temp_new();
8235 TCGv t1 = tcg_temp_new();
17d9b3af 8236 tcg_gen_ext32u_tl(t0, cpu_gpr[rB(ctx->opcode)]);
57951c27
AJ
8237 tcg_gen_shli_tl(t1, cpu_gpr[rA(ctx->opcode)], 32);
8238 tcg_gen_or_tl(cpu_gpr[rD(ctx->opcode)], t0, t1);
8239 tcg_temp_free(t0);
8240 tcg_temp_free(t1);
8241#else
57951c27 8242 tcg_gen_mov_i32(cpu_gprh[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)]);
33890b3e 8243 tcg_gen_mov_i32(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rB(ctx->opcode)]);
57951c27
AJ
8244#endif
8245}
636aa200 8246static inline void gen_evmergehilo(DisasContext *ctx)
57951c27
AJ
8247{
8248 if (unlikely(!ctx->spe_enabled)) {
27a69bb0 8249 gen_exception(ctx, POWERPC_EXCP_SPEU);
57951c27
AJ
8250 return;
8251 }
8252#if defined(TARGET_PPC64)
a7812ae4
PB
8253 TCGv t0 = tcg_temp_new();
8254 TCGv t1 = tcg_temp_new();
17d9b3af 8255 tcg_gen_ext32u_tl(t0, cpu_gpr[rB(ctx->opcode)]);
57951c27
AJ
8256 tcg_gen_andi_tl(t1, cpu_gpr[rA(ctx->opcode)], 0xFFFFFFFF0000000ULL);
8257 tcg_gen_or_tl(cpu_gpr[rD(ctx->opcode)], t0, t1);
8258 tcg_temp_free(t0);
8259 tcg_temp_free(t1);
8260#else
8261 tcg_gen_mov_i32(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rB(ctx->opcode)]);
8262 tcg_gen_mov_i32(cpu_gprh[rD(ctx->opcode)], cpu_gprh[rA(ctx->opcode)]);
8263#endif
8264}
636aa200 8265static inline void gen_evmergelohi(DisasContext *ctx)
57951c27
AJ
8266{
8267 if (unlikely(!ctx->spe_enabled)) {
27a69bb0 8268 gen_exception(ctx, POWERPC_EXCP_SPEU);
57951c27
AJ
8269 return;
8270 }
8271#if defined(TARGET_PPC64)
a7812ae4
PB
8272 TCGv t0 = tcg_temp_new();
8273 TCGv t1 = tcg_temp_new();
57951c27
AJ
8274 tcg_gen_shri_tl(t0, cpu_gpr[rB(ctx->opcode)], 32);
8275 tcg_gen_shli_tl(t1, cpu_gpr[rA(ctx->opcode)], 32);
8276 tcg_gen_or_tl(cpu_gpr[rD(ctx->opcode)], t0, t1);
8277 tcg_temp_free(t0);
8278 tcg_temp_free(t1);
8279#else
33890b3e
NF
8280 if (rD(ctx->opcode) == rA(ctx->opcode)) {
8281 TCGv_i32 tmp = tcg_temp_new_i32();
8282 tcg_gen_mov_i32(tmp, cpu_gpr[rA(ctx->opcode)]);
8283 tcg_gen_mov_i32(cpu_gpr[rD(ctx->opcode)], cpu_gprh[rB(ctx->opcode)]);
8284 tcg_gen_mov_i32(cpu_gprh[rD(ctx->opcode)], tmp);
8285 tcg_temp_free_i32(tmp);
8286 } else {
8287 tcg_gen_mov_i32(cpu_gpr[rD(ctx->opcode)], cpu_gprh[rB(ctx->opcode)]);
8288 tcg_gen_mov_i32(cpu_gprh[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)]);
8289 }
57951c27
AJ
8290#endif
8291}
636aa200 8292static inline void gen_evsplati(DisasContext *ctx)
57951c27 8293{
ae01847f 8294 uint64_t imm = ((int32_t)(rA(ctx->opcode) << 27)) >> 27;
0487d6a8 8295
57951c27 8296#if defined(TARGET_PPC64)
38d14952 8297 tcg_gen_movi_tl(cpu_gpr[rD(ctx->opcode)], (imm << 32) | imm);
57951c27
AJ
8298#else
8299 tcg_gen_movi_i32(cpu_gpr[rD(ctx->opcode)], imm);
8300 tcg_gen_movi_i32(cpu_gprh[rD(ctx->opcode)], imm);
8301#endif
8302}
636aa200 8303static inline void gen_evsplatfi(DisasContext *ctx)
0487d6a8 8304{
ae01847f 8305 uint64_t imm = rA(ctx->opcode) << 27;
0487d6a8 8306
57951c27 8307#if defined(TARGET_PPC64)
38d14952 8308 tcg_gen_movi_tl(cpu_gpr[rD(ctx->opcode)], (imm << 32) | imm);
57951c27
AJ
8309#else
8310 tcg_gen_movi_i32(cpu_gpr[rD(ctx->opcode)], imm);
8311 tcg_gen_movi_i32(cpu_gprh[rD(ctx->opcode)], imm);
8312#endif
0487d6a8
JM
8313}
8314
636aa200 8315static inline void gen_evsel(DisasContext *ctx)
57951c27
AJ
8316{
8317 int l1 = gen_new_label();
8318 int l2 = gen_new_label();
8319 int l3 = gen_new_label();
8320 int l4 = gen_new_label();
a7812ae4 8321 TCGv_i32 t0 = tcg_temp_local_new_i32();
57951c27 8322#if defined(TARGET_PPC64)
a7812ae4
PB
8323 TCGv t1 = tcg_temp_local_new();
8324 TCGv t2 = tcg_temp_local_new();
57951c27
AJ
8325#endif
8326 tcg_gen_andi_i32(t0, cpu_crf[ctx->opcode & 0x07], 1 << 3);
8327 tcg_gen_brcondi_i32(TCG_COND_EQ, t0, 0, l1);
8328#if defined(TARGET_PPC64)
8329 tcg_gen_andi_tl(t1, cpu_gpr[rA(ctx->opcode)], 0xFFFFFFFF00000000ULL);
8330#else
8331 tcg_gen_mov_tl(cpu_gprh[rD(ctx->opcode)], cpu_gprh[rA(ctx->opcode)]);
8332#endif
8333 tcg_gen_br(l2);
8334 gen_set_label(l1);
8335#if defined(TARGET_PPC64)
8336 tcg_gen_andi_tl(t1, cpu_gpr[rB(ctx->opcode)], 0xFFFFFFFF00000000ULL);
8337#else
8338 tcg_gen_mov_tl(cpu_gprh[rD(ctx->opcode)], cpu_gprh[rB(ctx->opcode)]);
8339#endif
8340 gen_set_label(l2);
8341 tcg_gen_andi_i32(t0, cpu_crf[ctx->opcode & 0x07], 1 << 2);
8342 tcg_gen_brcondi_i32(TCG_COND_EQ, t0, 0, l3);
8343#if defined(TARGET_PPC64)
17d9b3af 8344 tcg_gen_ext32u_tl(t2, cpu_gpr[rA(ctx->opcode)]);
57951c27
AJ
8345#else
8346 tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)]);
8347#endif
8348 tcg_gen_br(l4);
8349 gen_set_label(l3);
8350#if defined(TARGET_PPC64)
17d9b3af 8351 tcg_gen_ext32u_tl(t2, cpu_gpr[rB(ctx->opcode)]);
57951c27
AJ
8352#else
8353 tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rB(ctx->opcode)]);
8354#endif
8355 gen_set_label(l4);
a7812ae4 8356 tcg_temp_free_i32(t0);
57951c27
AJ
8357#if defined(TARGET_PPC64)
8358 tcg_gen_or_tl(cpu_gpr[rD(ctx->opcode)], t1, t2);
8359 tcg_temp_free(t1);
8360 tcg_temp_free(t2);
8361#endif
8362}
e8eaa2c0
BS
8363
8364static void gen_evsel0(DisasContext *ctx)
57951c27
AJ
8365{
8366 gen_evsel(ctx);
8367}
e8eaa2c0
BS
8368
8369static void gen_evsel1(DisasContext *ctx)
57951c27
AJ
8370{
8371 gen_evsel(ctx);
8372}
e8eaa2c0
BS
8373
8374static void gen_evsel2(DisasContext *ctx)
57951c27
AJ
8375{
8376 gen_evsel(ctx);
8377}
e8eaa2c0
BS
8378
8379static void gen_evsel3(DisasContext *ctx)
57951c27
AJ
8380{
8381 gen_evsel(ctx);
8382}
0487d6a8 8383
a0e13900
FC
8384/* Multiply */
8385
8386static inline void gen_evmwumi(DisasContext *ctx)
8387{
8388 TCGv_i64 t0, t1;
8389
8390 if (unlikely(!ctx->spe_enabled)) {
27a69bb0 8391 gen_exception(ctx, POWERPC_EXCP_SPEU);
a0e13900
FC
8392 return;
8393 }
8394
8395 t0 = tcg_temp_new_i64();
8396 t1 = tcg_temp_new_i64();
8397
8398 /* t0 := rA; t1 := rB */
8399#if defined(TARGET_PPC64)
8400 tcg_gen_ext32u_tl(t0, cpu_gpr[rA(ctx->opcode)]);
8401 tcg_gen_ext32u_tl(t1, cpu_gpr[rB(ctx->opcode)]);
8402#else
8403 tcg_gen_extu_tl_i64(t0, cpu_gpr[rA(ctx->opcode)]);
8404 tcg_gen_extu_tl_i64(t1, cpu_gpr[rB(ctx->opcode)]);
8405#endif
8406
8407 tcg_gen_mul_i64(t0, t0, t1); /* t0 := rA * rB */
8408
8409 gen_store_gpr64(rD(ctx->opcode), t0); /* rD := t0 */
8410
8411 tcg_temp_free_i64(t0);
8412 tcg_temp_free_i64(t1);
8413}
8414
8415static inline void gen_evmwumia(DisasContext *ctx)
8416{
8417 TCGv_i64 tmp;
8418
8419 if (unlikely(!ctx->spe_enabled)) {
27a69bb0 8420 gen_exception(ctx, POWERPC_EXCP_SPEU);
a0e13900
FC
8421 return;
8422 }
8423
8424 gen_evmwumi(ctx); /* rD := rA * rB */
8425
8426 tmp = tcg_temp_new_i64();
8427
8428 /* acc := rD */
8429 gen_load_gpr64(tmp, rD(ctx->opcode));
1328c2bf 8430 tcg_gen_st_i64(tmp, cpu_env, offsetof(CPUPPCState, spe_acc));
a0e13900
FC
8431 tcg_temp_free_i64(tmp);
8432}
8433
8434static inline void gen_evmwumiaa(DisasContext *ctx)
8435{
8436 TCGv_i64 acc;
8437 TCGv_i64 tmp;
8438
8439 if (unlikely(!ctx->spe_enabled)) {
27a69bb0 8440 gen_exception(ctx, POWERPC_EXCP_SPEU);
a0e13900
FC
8441 return;
8442 }
8443
8444 gen_evmwumi(ctx); /* rD := rA * rB */
8445
8446 acc = tcg_temp_new_i64();
8447 tmp = tcg_temp_new_i64();
8448
8449 /* tmp := rD */
8450 gen_load_gpr64(tmp, rD(ctx->opcode));
8451
8452 /* Load acc */
1328c2bf 8453 tcg_gen_ld_i64(acc, cpu_env, offsetof(CPUPPCState, spe_acc));
a0e13900
FC
8454
8455 /* acc := tmp + acc */
8456 tcg_gen_add_i64(acc, acc, tmp);
8457
8458 /* Store acc */
1328c2bf 8459 tcg_gen_st_i64(acc, cpu_env, offsetof(CPUPPCState, spe_acc));
a0e13900
FC
8460
8461 /* rD := acc */
8462 gen_store_gpr64(rD(ctx->opcode), acc);
8463
8464 tcg_temp_free_i64(acc);
8465 tcg_temp_free_i64(tmp);
8466}
8467
8468static inline void gen_evmwsmi(DisasContext *ctx)
8469{
8470 TCGv_i64 t0, t1;
8471
8472 if (unlikely(!ctx->spe_enabled)) {
27a69bb0 8473 gen_exception(ctx, POWERPC_EXCP_SPEU);
a0e13900
FC
8474 return;
8475 }
8476
8477 t0 = tcg_temp_new_i64();
8478 t1 = tcg_temp_new_i64();
8479
8480 /* t0 := rA; t1 := rB */
8481#if defined(TARGET_PPC64)
8482 tcg_gen_ext32s_tl(t0, cpu_gpr[rA(ctx->opcode)]);
8483 tcg_gen_ext32s_tl(t1, cpu_gpr[rB(ctx->opcode)]);
8484#else
8485 tcg_gen_ext_tl_i64(t0, cpu_gpr[rA(ctx->opcode)]);
8486 tcg_gen_ext_tl_i64(t1, cpu_gpr[rB(ctx->opcode)]);
8487#endif
8488
8489 tcg_gen_mul_i64(t0, t0, t1); /* t0 := rA * rB */
8490
8491 gen_store_gpr64(rD(ctx->opcode), t0); /* rD := t0 */
8492
8493 tcg_temp_free_i64(t0);
8494 tcg_temp_free_i64(t1);
8495}
8496
8497static inline void gen_evmwsmia(DisasContext *ctx)
8498{
8499 TCGv_i64 tmp;
8500
8501 gen_evmwsmi(ctx); /* rD := rA * rB */
8502
8503 tmp = tcg_temp_new_i64();
8504
8505 /* acc := rD */
8506 gen_load_gpr64(tmp, rD(ctx->opcode));
1328c2bf 8507 tcg_gen_st_i64(tmp, cpu_env, offsetof(CPUPPCState, spe_acc));
a0e13900
FC
8508
8509 tcg_temp_free_i64(tmp);
8510}
8511
8512static inline void gen_evmwsmiaa(DisasContext *ctx)
8513{
8514 TCGv_i64 acc = tcg_temp_new_i64();
8515 TCGv_i64 tmp = tcg_temp_new_i64();
8516
8517 gen_evmwsmi(ctx); /* rD := rA * rB */
8518
8519 acc = tcg_temp_new_i64();
8520 tmp = tcg_temp_new_i64();
8521
8522 /* tmp := rD */
8523 gen_load_gpr64(tmp, rD(ctx->opcode));
8524
8525 /* Load acc */
1328c2bf 8526 tcg_gen_ld_i64(acc, cpu_env, offsetof(CPUPPCState, spe_acc));
a0e13900
FC
8527
8528 /* acc := tmp + acc */
8529 tcg_gen_add_i64(acc, acc, tmp);
8530
8531 /* Store acc */
1328c2bf 8532 tcg_gen_st_i64(acc, cpu_env, offsetof(CPUPPCState, spe_acc));
a0e13900
FC
8533
8534 /* rD := acc */
8535 gen_store_gpr64(rD(ctx->opcode), acc);
8536
8537 tcg_temp_free_i64(acc);
8538 tcg_temp_free_i64(tmp);
8539}
8540
70560da7
FC
8541GEN_SPE(evaddw, speundef, 0x00, 0x08, 0x00000000, 0xFFFFFFFF, PPC_SPE); ////
8542GEN_SPE(evaddiw, speundef, 0x01, 0x08, 0x00000000, 0xFFFFFFFF, PPC_SPE);
8543GEN_SPE(evsubfw, speundef, 0x02, 0x08, 0x00000000, 0xFFFFFFFF, PPC_SPE); ////
8544GEN_SPE(evsubifw, speundef, 0x03, 0x08, 0x00000000, 0xFFFFFFFF, PPC_SPE);
8545GEN_SPE(evabs, evneg, 0x04, 0x08, 0x0000F800, 0x0000F800, PPC_SPE); ////
8546GEN_SPE(evextsb, evextsh, 0x05, 0x08, 0x0000F800, 0x0000F800, PPC_SPE); ////
8547GEN_SPE(evrndw, evcntlzw, 0x06, 0x08, 0x0000F800, 0x0000F800, PPC_SPE); ////
8548GEN_SPE(evcntlsw, brinc, 0x07, 0x08, 0x0000F800, 0x00000000, PPC_SPE); //
8549GEN_SPE(evmra, speundef, 0x02, 0x13, 0x0000F800, 0xFFFFFFFF, PPC_SPE);
8550GEN_SPE(speundef, evand, 0x08, 0x08, 0xFFFFFFFF, 0x00000000, PPC_SPE); ////
8551GEN_SPE(evandc, speundef, 0x09, 0x08, 0x00000000, 0xFFFFFFFF, PPC_SPE); ////
8552GEN_SPE(evxor, evor, 0x0B, 0x08, 0x00000000, 0x00000000, PPC_SPE); ////
8553GEN_SPE(evnor, eveqv, 0x0C, 0x08, 0x00000000, 0x00000000, PPC_SPE); ////
8554GEN_SPE(evmwumi, evmwsmi, 0x0C, 0x11, 0x00000000, 0x00000000, PPC_SPE);
8555GEN_SPE(evmwumia, evmwsmia, 0x1C, 0x11, 0x00000000, 0x00000000, PPC_SPE);
8556GEN_SPE(evmwumiaa, evmwsmiaa, 0x0C, 0x15, 0x00000000, 0x00000000, PPC_SPE);
8557GEN_SPE(speundef, evorc, 0x0D, 0x08, 0xFFFFFFFF, 0x00000000, PPC_SPE); ////
8558GEN_SPE(evnand, speundef, 0x0F, 0x08, 0x00000000, 0xFFFFFFFF, PPC_SPE); ////
8559GEN_SPE(evsrwu, evsrws, 0x10, 0x08, 0x00000000, 0x00000000, PPC_SPE); ////
8560GEN_SPE(evsrwiu, evsrwis, 0x11, 0x08, 0x00000000, 0x00000000, PPC_SPE);
8561GEN_SPE(evslw, speundef, 0x12, 0x08, 0x00000000, 0xFFFFFFFF, PPC_SPE); ////
8562GEN_SPE(evslwi, speundef, 0x13, 0x08, 0x00000000, 0xFFFFFFFF, PPC_SPE);
8563GEN_SPE(evrlw, evsplati, 0x14, 0x08, 0x00000000, 0x0000F800, PPC_SPE); //
8564GEN_SPE(evrlwi, evsplatfi, 0x15, 0x08, 0x00000000, 0x0000F800, PPC_SPE);
8565GEN_SPE(evmergehi, evmergelo, 0x16, 0x08, 0x00000000, 0x00000000, PPC_SPE); ////
8566GEN_SPE(evmergehilo, evmergelohi, 0x17, 0x08, 0x00000000, 0x00000000, PPC_SPE); ////
8567GEN_SPE(evcmpgtu, evcmpgts, 0x18, 0x08, 0x00600000, 0x00600000, PPC_SPE); ////
8568GEN_SPE(evcmpltu, evcmplts, 0x19, 0x08, 0x00600000, 0x00600000, PPC_SPE); ////
8569GEN_SPE(evcmpeq, speundef, 0x1A, 0x08, 0x00600000, 0xFFFFFFFF, PPC_SPE); ////
0487d6a8 8570
6a6ae23f 8571/* SPE load and stores */
636aa200 8572static inline void gen_addr_spe_imm_index(DisasContext *ctx, TCGv EA, int sh)
6a6ae23f
AJ
8573{
8574 target_ulong uimm = rB(ctx->opcode);
8575
76db3ba4 8576 if (rA(ctx->opcode) == 0) {
6a6ae23f 8577 tcg_gen_movi_tl(EA, uimm << sh);
76db3ba4 8578 } else {
6a6ae23f 8579 tcg_gen_addi_tl(EA, cpu_gpr[rA(ctx->opcode)], uimm << sh);
c791fe84 8580 if (NARROW_MODE(ctx)) {
76db3ba4
AJ
8581 tcg_gen_ext32u_tl(EA, EA);
8582 }
76db3ba4 8583 }
0487d6a8 8584}
6a6ae23f 8585
636aa200 8586static inline void gen_op_evldd(DisasContext *ctx, TCGv addr)
6a6ae23f
AJ
8587{
8588#if defined(TARGET_PPC64)
76db3ba4 8589 gen_qemu_ld64(ctx, cpu_gpr[rD(ctx->opcode)], addr);
6a6ae23f
AJ
8590#else
8591 TCGv_i64 t0 = tcg_temp_new_i64();
76db3ba4 8592 gen_qemu_ld64(ctx, t0, addr);
6a6ae23f
AJ
8593 tcg_gen_trunc_i64_i32(cpu_gpr[rD(ctx->opcode)], t0);
8594 tcg_gen_shri_i64(t0, t0, 32);
8595 tcg_gen_trunc_i64_i32(cpu_gprh[rD(ctx->opcode)], t0);
8596 tcg_temp_free_i64(t0);
8597#endif
0487d6a8 8598}
6a6ae23f 8599
636aa200 8600static inline void gen_op_evldw(DisasContext *ctx, TCGv addr)
6a6ae23f 8601{
0487d6a8 8602#if defined(TARGET_PPC64)
6a6ae23f 8603 TCGv t0 = tcg_temp_new();
76db3ba4 8604 gen_qemu_ld32u(ctx, t0, addr);
6a6ae23f 8605 tcg_gen_shli_tl(cpu_gpr[rD(ctx->opcode)], t0, 32);
76db3ba4
AJ
8606 gen_addr_add(ctx, addr, addr, 4);
8607 gen_qemu_ld32u(ctx, t0, addr);
6a6ae23f
AJ
8608 tcg_gen_or_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rD(ctx->opcode)], t0);
8609 tcg_temp_free(t0);
8610#else
76db3ba4
AJ
8611 gen_qemu_ld32u(ctx, cpu_gprh[rD(ctx->opcode)], addr);
8612 gen_addr_add(ctx, addr, addr, 4);
8613 gen_qemu_ld32u(ctx, cpu_gpr[rD(ctx->opcode)], addr);
6a6ae23f 8614#endif
0487d6a8 8615}
6a6ae23f 8616
636aa200 8617static inline void gen_op_evldh(DisasContext *ctx, TCGv addr)
6a6ae23f
AJ
8618{
8619 TCGv t0 = tcg_temp_new();
8620#if defined(TARGET_PPC64)
76db3ba4 8621 gen_qemu_ld16u(ctx, t0, addr);
6a6ae23f 8622 tcg_gen_shli_tl(cpu_gpr[rD(ctx->opcode)], t0, 48);
76db3ba4
AJ
8623 gen_addr_add(ctx, addr, addr, 2);
8624 gen_qemu_ld16u(ctx, t0, addr);
6a6ae23f
AJ
8625 tcg_gen_shli_tl(t0, t0, 32);
8626 tcg_gen_or_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rD(ctx->opcode)], t0);
76db3ba4
AJ
8627 gen_addr_add(ctx, addr, addr, 2);
8628 gen_qemu_ld16u(ctx, t0, addr);
6a6ae23f
AJ
8629 tcg_gen_shli_tl(t0, t0, 16);
8630 tcg_gen_or_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rD(ctx->opcode)], t0);
76db3ba4
AJ
8631 gen_addr_add(ctx, addr, addr, 2);
8632 gen_qemu_ld16u(ctx, t0, addr);
6a6ae23f 8633 tcg_gen_or_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rD(ctx->opcode)], t0);
0487d6a8 8634#else
76db3ba4 8635 gen_qemu_ld16u(ctx, t0, addr);
6a6ae23f 8636 tcg_gen_shli_tl(cpu_gprh[rD(ctx->opcode)], t0, 16);
76db3ba4
AJ
8637 gen_addr_add(ctx, addr, addr, 2);
8638 gen_qemu_ld16u(ctx, t0, addr);
6a6ae23f 8639 tcg_gen_or_tl(cpu_gprh[rD(ctx->opcode)], cpu_gprh[rD(ctx->opcode)], t0);
76db3ba4
AJ
8640 gen_addr_add(ctx, addr, addr, 2);
8641 gen_qemu_ld16u(ctx, t0, addr);
6a6ae23f 8642 tcg_gen_shli_tl(cpu_gprh[rD(ctx->opcode)], t0, 16);
76db3ba4
AJ
8643 gen_addr_add(ctx, addr, addr, 2);
8644 gen_qemu_ld16u(ctx, t0, addr);
6a6ae23f 8645 tcg_gen_or_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rD(ctx->opcode)], t0);
0487d6a8 8646#endif
6a6ae23f 8647 tcg_temp_free(t0);
0487d6a8
JM
8648}
8649
636aa200 8650static inline void gen_op_evlhhesplat(DisasContext *ctx, TCGv addr)
6a6ae23f
AJ
8651{
8652 TCGv t0 = tcg_temp_new();
76db3ba4 8653 gen_qemu_ld16u(ctx, t0, addr);
6a6ae23f
AJ
8654#if defined(TARGET_PPC64)
8655 tcg_gen_shli_tl(cpu_gpr[rD(ctx->opcode)], t0, 48);
8656 tcg_gen_shli_tl(t0, t0, 16);
8657 tcg_gen_or_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rD(ctx->opcode)], t0);
8658#else
8659 tcg_gen_shli_tl(t0, t0, 16);
8660 tcg_gen_mov_tl(cpu_gprh[rD(ctx->opcode)], t0);
8661 tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], t0);
8662#endif
8663 tcg_temp_free(t0);
0487d6a8
JM
8664}
8665
636aa200 8666static inline void gen_op_evlhhousplat(DisasContext *ctx, TCGv addr)
6a6ae23f
AJ
8667{
8668 TCGv t0 = tcg_temp_new();
76db3ba4 8669 gen_qemu_ld16u(ctx, t0, addr);
6a6ae23f
AJ
8670#if defined(TARGET_PPC64)
8671 tcg_gen_shli_tl(cpu_gpr[rD(ctx->opcode)], t0, 32);
8672 tcg_gen_or_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rD(ctx->opcode)], t0);
8673#else
8674 tcg_gen_mov_tl(cpu_gprh[rD(ctx->opcode)], t0);
8675 tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], t0);
8676#endif
8677 tcg_temp_free(t0);
0487d6a8
JM
8678}
8679
636aa200 8680static inline void gen_op_evlhhossplat(DisasContext *ctx, TCGv addr)
6a6ae23f
AJ
8681{
8682 TCGv t0 = tcg_temp_new();
76db3ba4 8683 gen_qemu_ld16s(ctx, t0, addr);
6a6ae23f
AJ
8684#if defined(TARGET_PPC64)
8685 tcg_gen_shli_tl(cpu_gpr[rD(ctx->opcode)], t0, 32);
8686 tcg_gen_ext32u_tl(t0, t0);
8687 tcg_gen_or_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rD(ctx->opcode)], t0);
8688#else
8689 tcg_gen_mov_tl(cpu_gprh[rD(ctx->opcode)], t0);
8690 tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], t0);
8691#endif
8692 tcg_temp_free(t0);
8693}
8694
636aa200 8695static inline void gen_op_evlwhe(DisasContext *ctx, TCGv addr)
6a6ae23f
AJ
8696{
8697 TCGv t0 = tcg_temp_new();
8698#if defined(TARGET_PPC64)
76db3ba4 8699 gen_qemu_ld16u(ctx, t0, addr);
6a6ae23f 8700 tcg_gen_shli_tl(cpu_gpr[rD(ctx->opcode)], t0, 48);
76db3ba4
AJ
8701 gen_addr_add(ctx, addr, addr, 2);
8702 gen_qemu_ld16u(ctx, t0, addr);
6a6ae23f
AJ
8703 tcg_gen_shli_tl(t0, t0, 16);
8704 tcg_gen_or_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rD(ctx->opcode)], t0);
8705#else
76db3ba4 8706 gen_qemu_ld16u(ctx, t0, addr);
6a6ae23f 8707 tcg_gen_shli_tl(cpu_gprh[rD(ctx->opcode)], t0, 16);
76db3ba4
AJ
8708 gen_addr_add(ctx, addr, addr, 2);
8709 gen_qemu_ld16u(ctx, t0, addr);
6a6ae23f
AJ
8710 tcg_gen_shli_tl(cpu_gpr[rD(ctx->opcode)], t0, 16);
8711#endif
8712 tcg_temp_free(t0);
8713}
8714
636aa200 8715static inline void gen_op_evlwhou(DisasContext *ctx, TCGv addr)
6a6ae23f
AJ
8716{
8717#if defined(TARGET_PPC64)
8718 TCGv t0 = tcg_temp_new();
76db3ba4
AJ
8719 gen_qemu_ld16u(ctx, cpu_gpr[rD(ctx->opcode)], addr);
8720 gen_addr_add(ctx, addr, addr, 2);
8721 gen_qemu_ld16u(ctx, t0, addr);
6a6ae23f
AJ
8722 tcg_gen_shli_tl(t0, t0, 32);
8723 tcg_gen_or_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rD(ctx->opcode)], t0);
8724 tcg_temp_free(t0);
8725#else
76db3ba4
AJ
8726 gen_qemu_ld16u(ctx, cpu_gprh[rD(ctx->opcode)], addr);
8727 gen_addr_add(ctx, addr, addr, 2);
8728 gen_qemu_ld16u(ctx, cpu_gpr[rD(ctx->opcode)], addr);
6a6ae23f
AJ
8729#endif
8730}
8731
636aa200 8732static inline void gen_op_evlwhos(DisasContext *ctx, TCGv addr)
6a6ae23f
AJ
8733{
8734#if defined(TARGET_PPC64)
8735 TCGv t0 = tcg_temp_new();
76db3ba4 8736 gen_qemu_ld16s(ctx, t0, addr);
6a6ae23f 8737 tcg_gen_ext32u_tl(cpu_gpr[rD(ctx->opcode)], t0);
76db3ba4
AJ
8738 gen_addr_add(ctx, addr, addr, 2);
8739 gen_qemu_ld16s(ctx, t0, addr);
6a6ae23f
AJ
8740 tcg_gen_shli_tl(t0, t0, 32);
8741 tcg_gen_or_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rD(ctx->opcode)], t0);
8742 tcg_temp_free(t0);
8743#else
76db3ba4
AJ
8744 gen_qemu_ld16s(ctx, cpu_gprh[rD(ctx->opcode)], addr);
8745 gen_addr_add(ctx, addr, addr, 2);
8746 gen_qemu_ld16s(ctx, cpu_gpr[rD(ctx->opcode)], addr);
6a6ae23f
AJ
8747#endif
8748}
8749
636aa200 8750static inline void gen_op_evlwwsplat(DisasContext *ctx, TCGv addr)
6a6ae23f
AJ
8751{
8752 TCGv t0 = tcg_temp_new();
76db3ba4 8753 gen_qemu_ld32u(ctx, t0, addr);
0487d6a8 8754#if defined(TARGET_PPC64)
6a6ae23f
AJ
8755 tcg_gen_shli_tl(cpu_gpr[rD(ctx->opcode)], t0, 32);
8756 tcg_gen_or_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rD(ctx->opcode)], t0);
8757#else
8758 tcg_gen_mov_tl(cpu_gprh[rD(ctx->opcode)], t0);
8759 tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], t0);
8760#endif
8761 tcg_temp_free(t0);
8762}
8763
636aa200 8764static inline void gen_op_evlwhsplat(DisasContext *ctx, TCGv addr)
6a6ae23f
AJ
8765{
8766 TCGv t0 = tcg_temp_new();
8767#if defined(TARGET_PPC64)
76db3ba4 8768 gen_qemu_ld16u(ctx, t0, addr);
6a6ae23f
AJ
8769 tcg_gen_shli_tl(cpu_gpr[rD(ctx->opcode)], t0, 48);
8770 tcg_gen_shli_tl(t0, t0, 32);
8771 tcg_gen_or_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rD(ctx->opcode)], t0);
76db3ba4
AJ
8772 gen_addr_add(ctx, addr, addr, 2);
8773 gen_qemu_ld16u(ctx, t0, addr);
6a6ae23f
AJ
8774 tcg_gen_or_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rD(ctx->opcode)], t0);
8775 tcg_gen_shli_tl(t0, t0, 16);
8776 tcg_gen_or_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rD(ctx->opcode)], t0);
8777#else
76db3ba4 8778 gen_qemu_ld16u(ctx, t0, addr);
6a6ae23f
AJ
8779 tcg_gen_shli_tl(cpu_gprh[rD(ctx->opcode)], t0, 16);
8780 tcg_gen_or_tl(cpu_gprh[rD(ctx->opcode)], cpu_gprh[rD(ctx->opcode)], t0);
76db3ba4
AJ
8781 gen_addr_add(ctx, addr, addr, 2);
8782 gen_qemu_ld16u(ctx, t0, addr);
6a6ae23f
AJ
8783 tcg_gen_shli_tl(cpu_gpr[rD(ctx->opcode)], t0, 16);
8784 tcg_gen_or_tl(cpu_gpr[rD(ctx->opcode)], cpu_gprh[rD(ctx->opcode)], t0);
0487d6a8 8785#endif
6a6ae23f
AJ
8786 tcg_temp_free(t0);
8787}
8788
636aa200 8789static inline void gen_op_evstdd(DisasContext *ctx, TCGv addr)
6a6ae23f
AJ
8790{
8791#if defined(TARGET_PPC64)
76db3ba4 8792 gen_qemu_st64(ctx, cpu_gpr[rS(ctx->opcode)], addr);
0487d6a8 8793#else
6a6ae23f
AJ
8794 TCGv_i64 t0 = tcg_temp_new_i64();
8795 tcg_gen_concat_i32_i64(t0, cpu_gpr[rS(ctx->opcode)], cpu_gprh[rS(ctx->opcode)]);
76db3ba4 8796 gen_qemu_st64(ctx, t0, addr);
6a6ae23f
AJ
8797 tcg_temp_free_i64(t0);
8798#endif
8799}
8800
636aa200 8801static inline void gen_op_evstdw(DisasContext *ctx, TCGv addr)
6a6ae23f 8802{
0487d6a8 8803#if defined(TARGET_PPC64)
6a6ae23f
AJ
8804 TCGv t0 = tcg_temp_new();
8805 tcg_gen_shri_tl(t0, cpu_gpr[rS(ctx->opcode)], 32);
76db3ba4 8806 gen_qemu_st32(ctx, t0, addr);
6a6ae23f
AJ
8807 tcg_temp_free(t0);
8808#else
76db3ba4 8809 gen_qemu_st32(ctx, cpu_gprh[rS(ctx->opcode)], addr);
6a6ae23f 8810#endif
76db3ba4
AJ
8811 gen_addr_add(ctx, addr, addr, 4);
8812 gen_qemu_st32(ctx, cpu_gpr[rS(ctx->opcode)], addr);
6a6ae23f
AJ
8813}
8814
636aa200 8815static inline void gen_op_evstdh(DisasContext *ctx, TCGv addr)
6a6ae23f
AJ
8816{
8817 TCGv t0 = tcg_temp_new();
8818#if defined(TARGET_PPC64)
8819 tcg_gen_shri_tl(t0, cpu_gpr[rS(ctx->opcode)], 48);
8820#else
8821 tcg_gen_shri_tl(t0, cpu_gprh[rS(ctx->opcode)], 16);
8822#endif
76db3ba4
AJ
8823 gen_qemu_st16(ctx, t0, addr);
8824 gen_addr_add(ctx, addr, addr, 2);
6a6ae23f
AJ
8825#if defined(TARGET_PPC64)
8826 tcg_gen_shri_tl(t0, cpu_gpr[rS(ctx->opcode)], 32);
76db3ba4 8827 gen_qemu_st16(ctx, t0, addr);
6a6ae23f 8828#else
76db3ba4 8829 gen_qemu_st16(ctx, cpu_gprh[rS(ctx->opcode)], addr);
6a6ae23f 8830#endif
76db3ba4 8831 gen_addr_add(ctx, addr, addr, 2);
6a6ae23f 8832 tcg_gen_shri_tl(t0, cpu_gpr[rS(ctx->opcode)], 16);
76db3ba4 8833 gen_qemu_st16(ctx, t0, addr);
6a6ae23f 8834 tcg_temp_free(t0);
76db3ba4
AJ
8835 gen_addr_add(ctx, addr, addr, 2);
8836 gen_qemu_st16(ctx, cpu_gpr[rS(ctx->opcode)], addr);
6a6ae23f
AJ
8837}
8838
636aa200 8839static inline void gen_op_evstwhe(DisasContext *ctx, TCGv addr)
6a6ae23f
AJ
8840{
8841 TCGv t0 = tcg_temp_new();
8842#if defined(TARGET_PPC64)
8843 tcg_gen_shri_tl(t0, cpu_gpr[rS(ctx->opcode)], 48);
8844#else
8845 tcg_gen_shri_tl(t0, cpu_gprh[rS(ctx->opcode)], 16);
8846#endif
76db3ba4
AJ
8847 gen_qemu_st16(ctx, t0, addr);
8848 gen_addr_add(ctx, addr, addr, 2);
6a6ae23f 8849 tcg_gen_shri_tl(t0, cpu_gpr[rS(ctx->opcode)], 16);
76db3ba4 8850 gen_qemu_st16(ctx, t0, addr);
6a6ae23f
AJ
8851 tcg_temp_free(t0);
8852}
8853
636aa200 8854static inline void gen_op_evstwho(DisasContext *ctx, TCGv addr)
6a6ae23f
AJ
8855{
8856#if defined(TARGET_PPC64)
8857 TCGv t0 = tcg_temp_new();
8858 tcg_gen_shri_tl(t0, cpu_gpr[rS(ctx->opcode)], 32);
76db3ba4 8859 gen_qemu_st16(ctx, t0, addr);
6a6ae23f
AJ
8860 tcg_temp_free(t0);
8861#else
76db3ba4 8862 gen_qemu_st16(ctx, cpu_gprh[rS(ctx->opcode)], addr);
6a6ae23f 8863#endif
76db3ba4
AJ
8864 gen_addr_add(ctx, addr, addr, 2);
8865 gen_qemu_st16(ctx, cpu_gpr[rS(ctx->opcode)], addr);
6a6ae23f
AJ
8866}
8867
636aa200 8868static inline void gen_op_evstwwe(DisasContext *ctx, TCGv addr)
6a6ae23f
AJ
8869{
8870#if defined(TARGET_PPC64)
8871 TCGv t0 = tcg_temp_new();
8872 tcg_gen_shri_tl(t0, cpu_gpr[rS(ctx->opcode)], 32);
76db3ba4 8873 gen_qemu_st32(ctx, t0, addr);
6a6ae23f
AJ
8874 tcg_temp_free(t0);
8875#else
76db3ba4 8876 gen_qemu_st32(ctx, cpu_gprh[rS(ctx->opcode)], addr);
6a6ae23f
AJ
8877#endif
8878}
8879
636aa200 8880static inline void gen_op_evstwwo(DisasContext *ctx, TCGv addr)
6a6ae23f 8881{
76db3ba4 8882 gen_qemu_st32(ctx, cpu_gpr[rS(ctx->opcode)], addr);
6a6ae23f
AJ
8883}
8884
8885#define GEN_SPEOP_LDST(name, opc2, sh) \
99e300ef 8886static void glue(gen_, name)(DisasContext *ctx) \
6a6ae23f
AJ
8887{ \
8888 TCGv t0; \
8889 if (unlikely(!ctx->spe_enabled)) { \
27a69bb0 8890 gen_exception(ctx, POWERPC_EXCP_SPEU); \
6a6ae23f
AJ
8891 return; \
8892 } \
76db3ba4 8893 gen_set_access_type(ctx, ACCESS_INT); \
6a6ae23f
AJ
8894 t0 = tcg_temp_new(); \
8895 if (Rc(ctx->opcode)) { \
76db3ba4 8896 gen_addr_spe_imm_index(ctx, t0, sh); \
6a6ae23f 8897 } else { \
76db3ba4 8898 gen_addr_reg_index(ctx, t0); \
6a6ae23f
AJ
8899 } \
8900 gen_op_##name(ctx, t0); \
8901 tcg_temp_free(t0); \
8902}
8903
8904GEN_SPEOP_LDST(evldd, 0x00, 3);
8905GEN_SPEOP_LDST(evldw, 0x01, 3);
8906GEN_SPEOP_LDST(evldh, 0x02, 3);
8907GEN_SPEOP_LDST(evlhhesplat, 0x04, 1);
8908GEN_SPEOP_LDST(evlhhousplat, 0x06, 1);
8909GEN_SPEOP_LDST(evlhhossplat, 0x07, 1);
8910GEN_SPEOP_LDST(evlwhe, 0x08, 2);
8911GEN_SPEOP_LDST(evlwhou, 0x0A, 2);
8912GEN_SPEOP_LDST(evlwhos, 0x0B, 2);
8913GEN_SPEOP_LDST(evlwwsplat, 0x0C, 2);
8914GEN_SPEOP_LDST(evlwhsplat, 0x0E, 2);
8915
8916GEN_SPEOP_LDST(evstdd, 0x10, 3);
8917GEN_SPEOP_LDST(evstdw, 0x11, 3);
8918GEN_SPEOP_LDST(evstdh, 0x12, 3);
8919GEN_SPEOP_LDST(evstwhe, 0x18, 2);
8920GEN_SPEOP_LDST(evstwho, 0x1A, 2);
8921GEN_SPEOP_LDST(evstwwe, 0x1C, 2);
8922GEN_SPEOP_LDST(evstwwo, 0x1E, 2);
0487d6a8
JM
8923
8924/* Multiply and add - TODO */
8925#if 0
70560da7
FC
8926GEN_SPE(speundef, evmhessf, 0x01, 0x10, 0xFFFFFFFF, 0x00000000, PPC_SPE);//
8927GEN_SPE(speundef, evmhossf, 0x03, 0x10, 0xFFFFFFFF, 0x00000000, PPC_SPE);
8928GEN_SPE(evmheumi, evmhesmi, 0x04, 0x10, 0x00000000, 0x00000000, PPC_SPE);
8929GEN_SPE(speundef, evmhesmf, 0x05, 0x10, 0xFFFFFFFF, 0x00000000, PPC_SPE);
8930GEN_SPE(evmhoumi, evmhosmi, 0x06, 0x10, 0x00000000, 0x00000000, PPC_SPE);
8931GEN_SPE(speundef, evmhosmf, 0x07, 0x10, 0xFFFFFFFF, 0x00000000, PPC_SPE);
8932GEN_SPE(speundef, evmhessfa, 0x11, 0x10, 0xFFFFFFFF, 0x00000000, PPC_SPE);
8933GEN_SPE(speundef, evmhossfa, 0x13, 0x10, 0xFFFFFFFF, 0x00000000, PPC_SPE);
8934GEN_SPE(evmheumia, evmhesmia, 0x14, 0x10, 0x00000000, 0x00000000, PPC_SPE);
8935GEN_SPE(speundef, evmhesmfa, 0x15, 0x10, 0xFFFFFFFF, 0x00000000, PPC_SPE);
8936GEN_SPE(evmhoumia, evmhosmia, 0x16, 0x10, 0x00000000, 0x00000000, PPC_SPE);
8937GEN_SPE(speundef, evmhosmfa, 0x17, 0x10, 0xFFFFFFFF, 0x00000000, PPC_SPE);
8938
8939GEN_SPE(speundef, evmwhssf, 0x03, 0x11, 0xFFFFFFFF, 0x00000000, PPC_SPE);
8940GEN_SPE(evmwlumi, speundef, 0x04, 0x11, 0x00000000, 0xFFFFFFFF, PPC_SPE);
8941GEN_SPE(evmwhumi, evmwhsmi, 0x06, 0x11, 0x00000000, 0x00000000, PPC_SPE);
8942GEN_SPE(speundef, evmwhsmf, 0x07, 0x11, 0xFFFFFFFF, 0x00000000, PPC_SPE);
8943GEN_SPE(speundef, evmwssf, 0x09, 0x11, 0xFFFFFFFF, 0x00000000, PPC_SPE);
8944GEN_SPE(speundef, evmwsmf, 0x0D, 0x11, 0xFFFFFFFF, 0x00000000, PPC_SPE);
8945GEN_SPE(speundef, evmwhssfa, 0x13, 0x11, 0xFFFFFFFF, 0x00000000, PPC_SPE);
8946GEN_SPE(evmwlumia, speundef, 0x14, 0x11, 0x00000000, 0xFFFFFFFF, PPC_SPE);
8947GEN_SPE(evmwhumia, evmwhsmia, 0x16, 0x11, 0x00000000, 0x00000000, PPC_SPE);
8948GEN_SPE(speundef, evmwhsmfa, 0x17, 0x11, 0xFFFFFFFF, 0x00000000, PPC_SPE);
8949GEN_SPE(speundef, evmwssfa, 0x19, 0x11, 0xFFFFFFFF, 0x00000000, PPC_SPE);
8950GEN_SPE(speundef, evmwsmfa, 0x1D, 0x11, 0xFFFFFFFF, 0x00000000, PPC_SPE);
8951
8952GEN_SPE(evadduiaaw, evaddsiaaw, 0x00, 0x13, 0x0000F800, 0x0000F800, PPC_SPE);
8953GEN_SPE(evsubfusiaaw, evsubfssiaaw, 0x01, 0x13, 0x0000F800, 0x0000F800, PPC_SPE);
8954GEN_SPE(evaddumiaaw, evaddsmiaaw, 0x04, 0x13, 0x0000F800, 0x0000F800, PPC_SPE);
8955GEN_SPE(evsubfumiaaw, evsubfsmiaaw, 0x05, 0x13, 0x0000F800, 0x0000F800, PPC_SPE);
8956GEN_SPE(evdivws, evdivwu, 0x06, 0x13, 0x00000000, 0x00000000, PPC_SPE);
8957
8958GEN_SPE(evmheusiaaw, evmhessiaaw, 0x00, 0x14, 0x00000000, 0x00000000, PPC_SPE);
8959GEN_SPE(speundef, evmhessfaaw, 0x01, 0x14, 0xFFFFFFFF, 0x00000000, PPC_SPE);
8960GEN_SPE(evmhousiaaw, evmhossiaaw, 0x02, 0x14, 0x00000000, 0x00000000, PPC_SPE);
8961GEN_SPE(speundef, evmhossfaaw, 0x03, 0x14, 0xFFFFFFFF, 0x00000000, PPC_SPE);
8962GEN_SPE(evmheumiaaw, evmhesmiaaw, 0x04, 0x14, 0x00000000, 0x00000000, PPC_SPE);
8963GEN_SPE(speundef, evmhesmfaaw, 0x05, 0x14, 0xFFFFFFFF, 0x00000000, PPC_SPE);
8964GEN_SPE(evmhoumiaaw, evmhosmiaaw, 0x06, 0x14, 0x00000000, 0x00000000, PPC_SPE);
8965GEN_SPE(speundef, evmhosmfaaw, 0x07, 0x14, 0xFFFFFFFF, 0x00000000, PPC_SPE);
8966GEN_SPE(evmhegumiaa, evmhegsmiaa, 0x14, 0x14, 0x00000000, 0x00000000, PPC_SPE);
8967GEN_SPE(speundef, evmhegsmfaa, 0x15, 0x14, 0xFFFFFFFF, 0x00000000, PPC_SPE);
8968GEN_SPE(evmhogumiaa, evmhogsmiaa, 0x16, 0x14, 0x00000000, 0x00000000, PPC_SPE);
8969GEN_SPE(speundef, evmhogsmfaa, 0x17, 0x14, 0xFFFFFFFF, 0x00000000, PPC_SPE);
8970
8971GEN_SPE(evmwlusiaaw, evmwlssiaaw, 0x00, 0x15, 0x00000000, 0x00000000, PPC_SPE);
8972GEN_SPE(evmwlumiaaw, evmwlsmiaaw, 0x04, 0x15, 0x00000000, 0x00000000, PPC_SPE);
8973GEN_SPE(speundef, evmwssfaa, 0x09, 0x15, 0xFFFFFFFF, 0x00000000, PPC_SPE);
8974GEN_SPE(speundef, evmwsmfaa, 0x0D, 0x15, 0xFFFFFFFF, 0x00000000, PPC_SPE);
8975
8976GEN_SPE(evmheusianw, evmhessianw, 0x00, 0x16, 0x00000000, 0x00000000, PPC_SPE);
8977GEN_SPE(speundef, evmhessfanw, 0x01, 0x16, 0xFFFFFFFF, 0x00000000, PPC_SPE);
8978GEN_SPE(evmhousianw, evmhossianw, 0x02, 0x16, 0x00000000, 0x00000000, PPC_SPE);
8979GEN_SPE(speundef, evmhossfanw, 0x03, 0x16, 0xFFFFFFFF, 0x00000000, PPC_SPE);
8980GEN_SPE(evmheumianw, evmhesmianw, 0x04, 0x16, 0x00000000, 0x00000000, PPC_SPE);
8981GEN_SPE(speundef, evmhesmfanw, 0x05, 0x16, 0xFFFFFFFF, 0x00000000, PPC_SPE);
8982GEN_SPE(evmhoumianw, evmhosmianw, 0x06, 0x16, 0x00000000, 0x00000000, PPC_SPE);
8983GEN_SPE(speundef, evmhosmfanw, 0x07, 0x16, 0xFFFFFFFF, 0x00000000, PPC_SPE);
8984GEN_SPE(evmhegumian, evmhegsmian, 0x14, 0x16, 0x00000000, 0x00000000, PPC_SPE);
8985GEN_SPE(speundef, evmhegsmfan, 0x15, 0x16, 0xFFFFFFFF, 0x00000000, PPC_SPE);
8986GEN_SPE(evmhigumian, evmhigsmian, 0x16, 0x16, 0x00000000, 0x00000000, PPC_SPE);
8987GEN_SPE(speundef, evmhogsmfan, 0x17, 0x16, 0xFFFFFFFF, 0x00000000, PPC_SPE);
8988
8989GEN_SPE(evmwlusianw, evmwlssianw, 0x00, 0x17, 0x00000000, 0x00000000, PPC_SPE);
8990GEN_SPE(evmwlumianw, evmwlsmianw, 0x04, 0x17, 0x00000000, 0x00000000, PPC_SPE);
8991GEN_SPE(speundef, evmwssfan, 0x09, 0x17, 0xFFFFFFFF, 0x00000000, PPC_SPE);
8992GEN_SPE(evmwumian, evmwsmian, 0x0C, 0x17, 0x00000000, 0x00000000, PPC_SPE);
8993GEN_SPE(speundef, evmwsmfan, 0x0D, 0x17, 0xFFFFFFFF, 0x00000000, PPC_SPE);
0487d6a8
JM
8994#endif
8995
8996/*** SPE floating-point extension ***/
1c97856d
AJ
8997#if defined(TARGET_PPC64)
8998#define GEN_SPEFPUOP_CONV_32_32(name) \
636aa200 8999static inline void gen_##name(DisasContext *ctx) \
0487d6a8 9000{ \
1c97856d
AJ
9001 TCGv_i32 t0; \
9002 TCGv t1; \
9003 t0 = tcg_temp_new_i32(); \
9004 tcg_gen_trunc_tl_i32(t0, cpu_gpr[rB(ctx->opcode)]); \
8e703949 9005 gen_helper_##name(t0, cpu_env, t0); \
1c97856d
AJ
9006 t1 = tcg_temp_new(); \
9007 tcg_gen_extu_i32_tl(t1, t0); \
9008 tcg_temp_free_i32(t0); \
9009 tcg_gen_andi_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rD(ctx->opcode)], \
9010 0xFFFFFFFF00000000ULL); \
9011 tcg_gen_or_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rD(ctx->opcode)], t1); \
9012 tcg_temp_free(t1); \
0487d6a8 9013}
1c97856d 9014#define GEN_SPEFPUOP_CONV_32_64(name) \
636aa200 9015static inline void gen_##name(DisasContext *ctx) \
1c97856d
AJ
9016{ \
9017 TCGv_i32 t0; \
9018 TCGv t1; \
9019 t0 = tcg_temp_new_i32(); \
8e703949 9020 gen_helper_##name(t0, cpu_env, cpu_gpr[rB(ctx->opcode)]); \
1c97856d
AJ
9021 t1 = tcg_temp_new(); \
9022 tcg_gen_extu_i32_tl(t1, t0); \
9023 tcg_temp_free_i32(t0); \
9024 tcg_gen_andi_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rD(ctx->opcode)], \
9025 0xFFFFFFFF00000000ULL); \
9026 tcg_gen_or_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rD(ctx->opcode)], t1); \
9027 tcg_temp_free(t1); \
9028}
9029#define GEN_SPEFPUOP_CONV_64_32(name) \
636aa200 9030static inline void gen_##name(DisasContext *ctx) \
1c97856d
AJ
9031{ \
9032 TCGv_i32 t0 = tcg_temp_new_i32(); \
9033 tcg_gen_trunc_tl_i32(t0, cpu_gpr[rB(ctx->opcode)]); \
8e703949 9034 gen_helper_##name(cpu_gpr[rD(ctx->opcode)], cpu_env, t0); \
1c97856d
AJ
9035 tcg_temp_free_i32(t0); \
9036}
9037#define GEN_SPEFPUOP_CONV_64_64(name) \
636aa200 9038static inline void gen_##name(DisasContext *ctx) \
1c97856d 9039{ \
8e703949
BS
9040 gen_helper_##name(cpu_gpr[rD(ctx->opcode)], cpu_env, \
9041 cpu_gpr[rB(ctx->opcode)]); \
1c97856d
AJ
9042}
9043#define GEN_SPEFPUOP_ARITH2_32_32(name) \
636aa200 9044static inline void gen_##name(DisasContext *ctx) \
57951c27 9045{ \
1c97856d
AJ
9046 TCGv_i32 t0, t1; \
9047 TCGv_i64 t2; \
57951c27 9048 if (unlikely(!ctx->spe_enabled)) { \
27a69bb0 9049 gen_exception(ctx, POWERPC_EXCP_SPEU); \
57951c27
AJ
9050 return; \
9051 } \
1c97856d
AJ
9052 t0 = tcg_temp_new_i32(); \
9053 t1 = tcg_temp_new_i32(); \
9054 tcg_gen_trunc_tl_i32(t0, cpu_gpr[rA(ctx->opcode)]); \
9055 tcg_gen_trunc_tl_i32(t1, cpu_gpr[rB(ctx->opcode)]); \
8e703949 9056 gen_helper_##name(t0, cpu_env, t0, t1); \
1c97856d
AJ
9057 tcg_temp_free_i32(t1); \
9058 t2 = tcg_temp_new(); \
9059 tcg_gen_extu_i32_tl(t2, t0); \
9060 tcg_temp_free_i32(t0); \
9061 tcg_gen_andi_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rD(ctx->opcode)], \
9062 0xFFFFFFFF00000000ULL); \
9063 tcg_gen_or_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rD(ctx->opcode)], t2); \
9064 tcg_temp_free(t2); \
57951c27 9065}
1c97856d 9066#define GEN_SPEFPUOP_ARITH2_64_64(name) \
636aa200 9067static inline void gen_##name(DisasContext *ctx) \
57951c27
AJ
9068{ \
9069 if (unlikely(!ctx->spe_enabled)) { \
27a69bb0 9070 gen_exception(ctx, POWERPC_EXCP_SPEU); \
57951c27
AJ
9071 return; \
9072 } \
8e703949
BS
9073 gen_helper_##name(cpu_gpr[rD(ctx->opcode)], cpu_env, \
9074 cpu_gpr[rA(ctx->opcode)], cpu_gpr[rB(ctx->opcode)]); \
57951c27 9075}
1c97856d 9076#define GEN_SPEFPUOP_COMP_32(name) \
636aa200 9077static inline void gen_##name(DisasContext *ctx) \
57951c27 9078{ \
1c97856d 9079 TCGv_i32 t0, t1; \
57951c27 9080 if (unlikely(!ctx->spe_enabled)) { \
27a69bb0 9081 gen_exception(ctx, POWERPC_EXCP_SPEU); \
57951c27
AJ
9082 return; \
9083 } \
1c97856d
AJ
9084 t0 = tcg_temp_new_i32(); \
9085 t1 = tcg_temp_new_i32(); \
9086 tcg_gen_trunc_tl_i32(t0, cpu_gpr[rA(ctx->opcode)]); \
9087 tcg_gen_trunc_tl_i32(t1, cpu_gpr[rB(ctx->opcode)]); \
8e703949 9088 gen_helper_##name(cpu_crf[crfD(ctx->opcode)], cpu_env, t0, t1); \
1c97856d
AJ
9089 tcg_temp_free_i32(t0); \
9090 tcg_temp_free_i32(t1); \
9091}
9092#define GEN_SPEFPUOP_COMP_64(name) \
636aa200 9093static inline void gen_##name(DisasContext *ctx) \
1c97856d
AJ
9094{ \
9095 if (unlikely(!ctx->spe_enabled)) { \
27a69bb0 9096 gen_exception(ctx, POWERPC_EXCP_SPEU); \
1c97856d
AJ
9097 return; \
9098 } \
8e703949 9099 gen_helper_##name(cpu_crf[crfD(ctx->opcode)], cpu_env, \
1c97856d
AJ
9100 cpu_gpr[rA(ctx->opcode)], cpu_gpr[rB(ctx->opcode)]); \
9101}
9102#else
9103#define GEN_SPEFPUOP_CONV_32_32(name) \
636aa200 9104static inline void gen_##name(DisasContext *ctx) \
1c97856d 9105{ \
8e703949
BS
9106 gen_helper_##name(cpu_gpr[rD(ctx->opcode)], cpu_env, \
9107 cpu_gpr[rB(ctx->opcode)]); \
57951c27 9108}
1c97856d 9109#define GEN_SPEFPUOP_CONV_32_64(name) \
636aa200 9110static inline void gen_##name(DisasContext *ctx) \
1c97856d
AJ
9111{ \
9112 TCGv_i64 t0 = tcg_temp_new_i64(); \
9113 gen_load_gpr64(t0, rB(ctx->opcode)); \
8e703949 9114 gen_helper_##name(cpu_gpr[rD(ctx->opcode)], cpu_env, t0); \
1c97856d
AJ
9115 tcg_temp_free_i64(t0); \
9116}
9117#define GEN_SPEFPUOP_CONV_64_32(name) \
636aa200 9118static inline void gen_##name(DisasContext *ctx) \
1c97856d
AJ
9119{ \
9120 TCGv_i64 t0 = tcg_temp_new_i64(); \
8e703949 9121 gen_helper_##name(t0, cpu_env, cpu_gpr[rB(ctx->opcode)]); \
1c97856d
AJ
9122 gen_store_gpr64(rD(ctx->opcode), t0); \
9123 tcg_temp_free_i64(t0); \
9124}
9125#define GEN_SPEFPUOP_CONV_64_64(name) \
636aa200 9126static inline void gen_##name(DisasContext *ctx) \
1c97856d
AJ
9127{ \
9128 TCGv_i64 t0 = tcg_temp_new_i64(); \
9129 gen_load_gpr64(t0, rB(ctx->opcode)); \
8e703949 9130 gen_helper_##name(t0, cpu_env, t0); \
1c97856d
AJ
9131 gen_store_gpr64(rD(ctx->opcode), t0); \
9132 tcg_temp_free_i64(t0); \
9133}
9134#define GEN_SPEFPUOP_ARITH2_32_32(name) \
636aa200 9135static inline void gen_##name(DisasContext *ctx) \
1c97856d
AJ
9136{ \
9137 if (unlikely(!ctx->spe_enabled)) { \
27a69bb0 9138 gen_exception(ctx, POWERPC_EXCP_SPEU); \
1c97856d
AJ
9139 return; \
9140 } \
8e703949 9141 gen_helper_##name(cpu_gpr[rD(ctx->opcode)], cpu_env, \
1c97856d
AJ
9142 cpu_gpr[rA(ctx->opcode)], cpu_gpr[rB(ctx->opcode)]); \
9143}
9144#define GEN_SPEFPUOP_ARITH2_64_64(name) \
636aa200 9145static inline void gen_##name(DisasContext *ctx) \
1c97856d
AJ
9146{ \
9147 TCGv_i64 t0, t1; \
9148 if (unlikely(!ctx->spe_enabled)) { \
27a69bb0 9149 gen_exception(ctx, POWERPC_EXCP_SPEU); \
1c97856d
AJ
9150 return; \
9151 } \
9152 t0 = tcg_temp_new_i64(); \
9153 t1 = tcg_temp_new_i64(); \
9154 gen_load_gpr64(t0, rA(ctx->opcode)); \
9155 gen_load_gpr64(t1, rB(ctx->opcode)); \
8e703949 9156 gen_helper_##name(t0, cpu_env, t0, t1); \
1c97856d
AJ
9157 gen_store_gpr64(rD(ctx->opcode), t0); \
9158 tcg_temp_free_i64(t0); \
9159 tcg_temp_free_i64(t1); \
9160}
9161#define GEN_SPEFPUOP_COMP_32(name) \
636aa200 9162static inline void gen_##name(DisasContext *ctx) \
1c97856d
AJ
9163{ \
9164 if (unlikely(!ctx->spe_enabled)) { \
27a69bb0 9165 gen_exception(ctx, POWERPC_EXCP_SPEU); \
1c97856d
AJ
9166 return; \
9167 } \
8e703949 9168 gen_helper_##name(cpu_crf[crfD(ctx->opcode)], cpu_env, \
1c97856d
AJ
9169 cpu_gpr[rA(ctx->opcode)], cpu_gpr[rB(ctx->opcode)]); \
9170}
9171#define GEN_SPEFPUOP_COMP_64(name) \
636aa200 9172static inline void gen_##name(DisasContext *ctx) \
1c97856d
AJ
9173{ \
9174 TCGv_i64 t0, t1; \
9175 if (unlikely(!ctx->spe_enabled)) { \
27a69bb0 9176 gen_exception(ctx, POWERPC_EXCP_SPEU); \
1c97856d
AJ
9177 return; \
9178 } \
9179 t0 = tcg_temp_new_i64(); \
9180 t1 = tcg_temp_new_i64(); \
9181 gen_load_gpr64(t0, rA(ctx->opcode)); \
9182 gen_load_gpr64(t1, rB(ctx->opcode)); \
8e703949 9183 gen_helper_##name(cpu_crf[crfD(ctx->opcode)], cpu_env, t0, t1); \
1c97856d
AJ
9184 tcg_temp_free_i64(t0); \
9185 tcg_temp_free_i64(t1); \
9186}
9187#endif
57951c27 9188
0487d6a8
JM
9189/* Single precision floating-point vectors operations */
9190/* Arithmetic */
1c97856d
AJ
9191GEN_SPEFPUOP_ARITH2_64_64(evfsadd);
9192GEN_SPEFPUOP_ARITH2_64_64(evfssub);
9193GEN_SPEFPUOP_ARITH2_64_64(evfsmul);
9194GEN_SPEFPUOP_ARITH2_64_64(evfsdiv);
636aa200 9195static inline void gen_evfsabs(DisasContext *ctx)
1c97856d
AJ
9196{
9197 if (unlikely(!ctx->spe_enabled)) {
27a69bb0 9198 gen_exception(ctx, POWERPC_EXCP_SPEU);
1c97856d
AJ
9199 return;
9200 }
9201#if defined(TARGET_PPC64)
6d5c34fa 9202 tcg_gen_andi_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)], ~0x8000000080000000LL);
1c97856d 9203#else
6d5c34fa
MP
9204 tcg_gen_andi_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)], ~0x80000000);
9205 tcg_gen_andi_tl(cpu_gprh[rD(ctx->opcode)], cpu_gprh[rA(ctx->opcode)], ~0x80000000);
1c97856d
AJ
9206#endif
9207}
636aa200 9208static inline void gen_evfsnabs(DisasContext *ctx)
1c97856d
AJ
9209{
9210 if (unlikely(!ctx->spe_enabled)) {
27a69bb0 9211 gen_exception(ctx, POWERPC_EXCP_SPEU);
1c97856d
AJ
9212 return;
9213 }
9214#if defined(TARGET_PPC64)
6d5c34fa 9215 tcg_gen_ori_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)], 0x8000000080000000LL);
1c97856d 9216#else
6d5c34fa
MP
9217 tcg_gen_ori_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)], 0x80000000);
9218 tcg_gen_ori_tl(cpu_gprh[rD(ctx->opcode)], cpu_gprh[rA(ctx->opcode)], 0x80000000);
1c97856d
AJ
9219#endif
9220}
636aa200 9221static inline void gen_evfsneg(DisasContext *ctx)
1c97856d
AJ
9222{
9223 if (unlikely(!ctx->spe_enabled)) {
27a69bb0 9224 gen_exception(ctx, POWERPC_EXCP_SPEU);
1c97856d
AJ
9225 return;
9226 }
9227#if defined(TARGET_PPC64)
6d5c34fa 9228 tcg_gen_xori_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)], 0x8000000080000000LL);
1c97856d 9229#else
6d5c34fa
MP
9230 tcg_gen_xori_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)], 0x80000000);
9231 tcg_gen_xori_tl(cpu_gprh[rD(ctx->opcode)], cpu_gprh[rA(ctx->opcode)], 0x80000000);
1c97856d
AJ
9232#endif
9233}
9234
0487d6a8 9235/* Conversion */
1c97856d
AJ
9236GEN_SPEFPUOP_CONV_64_64(evfscfui);
9237GEN_SPEFPUOP_CONV_64_64(evfscfsi);
9238GEN_SPEFPUOP_CONV_64_64(evfscfuf);
9239GEN_SPEFPUOP_CONV_64_64(evfscfsf);
9240GEN_SPEFPUOP_CONV_64_64(evfsctui);
9241GEN_SPEFPUOP_CONV_64_64(evfsctsi);
9242GEN_SPEFPUOP_CONV_64_64(evfsctuf);
9243GEN_SPEFPUOP_CONV_64_64(evfsctsf);
9244GEN_SPEFPUOP_CONV_64_64(evfsctuiz);
9245GEN_SPEFPUOP_CONV_64_64(evfsctsiz);
9246
0487d6a8 9247/* Comparison */
1c97856d
AJ
9248GEN_SPEFPUOP_COMP_64(evfscmpgt);
9249GEN_SPEFPUOP_COMP_64(evfscmplt);
9250GEN_SPEFPUOP_COMP_64(evfscmpeq);
9251GEN_SPEFPUOP_COMP_64(evfststgt);
9252GEN_SPEFPUOP_COMP_64(evfststlt);
9253GEN_SPEFPUOP_COMP_64(evfststeq);
0487d6a8
JM
9254
9255/* Opcodes definitions */
70560da7
FC
9256GEN_SPE(evfsadd, evfssub, 0x00, 0x0A, 0x00000000, 0x00000000, PPC_SPE_SINGLE); //
9257GEN_SPE(evfsabs, evfsnabs, 0x02, 0x0A, 0x0000F800, 0x0000F800, PPC_SPE_SINGLE); //
9258GEN_SPE(evfsneg, speundef, 0x03, 0x0A, 0x0000F800, 0xFFFFFFFF, PPC_SPE_SINGLE); //
9259GEN_SPE(evfsmul, evfsdiv, 0x04, 0x0A, 0x00000000, 0x00000000, PPC_SPE_SINGLE); //
9260GEN_SPE(evfscmpgt, evfscmplt, 0x06, 0x0A, 0x00600000, 0x00600000, PPC_SPE_SINGLE); //
9261GEN_SPE(evfscmpeq, speundef, 0x07, 0x0A, 0x00600000, 0xFFFFFFFF, PPC_SPE_SINGLE); //
9262GEN_SPE(evfscfui, evfscfsi, 0x08, 0x0A, 0x00180000, 0x00180000, PPC_SPE_SINGLE); //
9263GEN_SPE(evfscfuf, evfscfsf, 0x09, 0x0A, 0x00180000, 0x00180000, PPC_SPE_SINGLE); //
9264GEN_SPE(evfsctui, evfsctsi, 0x0A, 0x0A, 0x00180000, 0x00180000, PPC_SPE_SINGLE); //
9265GEN_SPE(evfsctuf, evfsctsf, 0x0B, 0x0A, 0x00180000, 0x00180000, PPC_SPE_SINGLE); //
9266GEN_SPE(evfsctuiz, speundef, 0x0C, 0x0A, 0x00180000, 0xFFFFFFFF, PPC_SPE_SINGLE); //
9267GEN_SPE(evfsctsiz, speundef, 0x0D, 0x0A, 0x00180000, 0xFFFFFFFF, PPC_SPE_SINGLE); //
9268GEN_SPE(evfststgt, evfststlt, 0x0E, 0x0A, 0x00600000, 0x00600000, PPC_SPE_SINGLE); //
9269GEN_SPE(evfststeq, speundef, 0x0F, 0x0A, 0x00600000, 0xFFFFFFFF, PPC_SPE_SINGLE); //
0487d6a8
JM
9270
9271/* Single precision floating-point operations */
9272/* Arithmetic */
1c97856d
AJ
9273GEN_SPEFPUOP_ARITH2_32_32(efsadd);
9274GEN_SPEFPUOP_ARITH2_32_32(efssub);
9275GEN_SPEFPUOP_ARITH2_32_32(efsmul);
9276GEN_SPEFPUOP_ARITH2_32_32(efsdiv);
636aa200 9277static inline void gen_efsabs(DisasContext *ctx)
1c97856d
AJ
9278{
9279 if (unlikely(!ctx->spe_enabled)) {
27a69bb0 9280 gen_exception(ctx, POWERPC_EXCP_SPEU);
1c97856d
AJ
9281 return;
9282 }
6d5c34fa 9283 tcg_gen_andi_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)], (target_long)~0x80000000LL);
1c97856d 9284}
636aa200 9285static inline void gen_efsnabs(DisasContext *ctx)
1c97856d
AJ
9286{
9287 if (unlikely(!ctx->spe_enabled)) {
27a69bb0 9288 gen_exception(ctx, POWERPC_EXCP_SPEU);
1c97856d
AJ
9289 return;
9290 }
6d5c34fa 9291 tcg_gen_ori_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)], 0x80000000);
1c97856d 9292}
636aa200 9293static inline void gen_efsneg(DisasContext *ctx)
1c97856d
AJ
9294{
9295 if (unlikely(!ctx->spe_enabled)) {
27a69bb0 9296 gen_exception(ctx, POWERPC_EXCP_SPEU);
1c97856d
AJ
9297 return;
9298 }
6d5c34fa 9299 tcg_gen_xori_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)], 0x80000000);
1c97856d
AJ
9300}
9301
0487d6a8 9302/* Conversion */
1c97856d
AJ
9303GEN_SPEFPUOP_CONV_32_32(efscfui);
9304GEN_SPEFPUOP_CONV_32_32(efscfsi);
9305GEN_SPEFPUOP_CONV_32_32(efscfuf);
9306GEN_SPEFPUOP_CONV_32_32(efscfsf);
9307GEN_SPEFPUOP_CONV_32_32(efsctui);
9308GEN_SPEFPUOP_CONV_32_32(efsctsi);
9309GEN_SPEFPUOP_CONV_32_32(efsctuf);
9310GEN_SPEFPUOP_CONV_32_32(efsctsf);
9311GEN_SPEFPUOP_CONV_32_32(efsctuiz);
9312GEN_SPEFPUOP_CONV_32_32(efsctsiz);
9313GEN_SPEFPUOP_CONV_32_64(efscfd);
9314
0487d6a8 9315/* Comparison */
1c97856d
AJ
9316GEN_SPEFPUOP_COMP_32(efscmpgt);
9317GEN_SPEFPUOP_COMP_32(efscmplt);
9318GEN_SPEFPUOP_COMP_32(efscmpeq);
9319GEN_SPEFPUOP_COMP_32(efststgt);
9320GEN_SPEFPUOP_COMP_32(efststlt);
9321GEN_SPEFPUOP_COMP_32(efststeq);
0487d6a8
JM
9322
9323/* Opcodes definitions */
70560da7
FC
9324GEN_SPE(efsadd, efssub, 0x00, 0x0B, 0x00000000, 0x00000000, PPC_SPE_SINGLE); //
9325GEN_SPE(efsabs, efsnabs, 0x02, 0x0B, 0x0000F800, 0x0000F800, PPC_SPE_SINGLE); //
9326GEN_SPE(efsneg, speundef, 0x03, 0x0B, 0x0000F800, 0xFFFFFFFF, PPC_SPE_SINGLE); //
9327GEN_SPE(efsmul, efsdiv, 0x04, 0x0B, 0x00000000, 0x00000000, PPC_SPE_SINGLE); //
9328GEN_SPE(efscmpgt, efscmplt, 0x06, 0x0B, 0x00600000, 0x00600000, PPC_SPE_SINGLE); //
9329GEN_SPE(efscmpeq, efscfd, 0x07, 0x0B, 0x00600000, 0x00180000, PPC_SPE_SINGLE); //
9330GEN_SPE(efscfui, efscfsi, 0x08, 0x0B, 0x00180000, 0x00180000, PPC_SPE_SINGLE); //
9331GEN_SPE(efscfuf, efscfsf, 0x09, 0x0B, 0x00180000, 0x00180000, PPC_SPE_SINGLE); //
9332GEN_SPE(efsctui, efsctsi, 0x0A, 0x0B, 0x00180000, 0x00180000, PPC_SPE_SINGLE); //
9333GEN_SPE(efsctuf, efsctsf, 0x0B, 0x0B, 0x00180000, 0x00180000, PPC_SPE_SINGLE); //
9334GEN_SPE(efsctuiz, speundef, 0x0C, 0x0B, 0x00180000, 0xFFFFFFFF, PPC_SPE_SINGLE); //
9335GEN_SPE(efsctsiz, speundef, 0x0D, 0x0B, 0x00180000, 0xFFFFFFFF, PPC_SPE_SINGLE); //
9336GEN_SPE(efststgt, efststlt, 0x0E, 0x0B, 0x00600000, 0x00600000, PPC_SPE_SINGLE); //
9337GEN_SPE(efststeq, speundef, 0x0F, 0x0B, 0x00600000, 0xFFFFFFFF, PPC_SPE_SINGLE); //
0487d6a8
JM
9338
9339/* Double precision floating-point operations */
9340/* Arithmetic */
1c97856d
AJ
9341GEN_SPEFPUOP_ARITH2_64_64(efdadd);
9342GEN_SPEFPUOP_ARITH2_64_64(efdsub);
9343GEN_SPEFPUOP_ARITH2_64_64(efdmul);
9344GEN_SPEFPUOP_ARITH2_64_64(efddiv);
636aa200 9345static inline void gen_efdabs(DisasContext *ctx)
1c97856d
AJ
9346{
9347 if (unlikely(!ctx->spe_enabled)) {
27a69bb0 9348 gen_exception(ctx, POWERPC_EXCP_SPEU);
1c97856d
AJ
9349 return;
9350 }
9351#if defined(TARGET_PPC64)
6d5c34fa 9352 tcg_gen_andi_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)], ~0x8000000000000000LL);
1c97856d 9353#else
6d5c34fa
MP
9354 tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)]);
9355 tcg_gen_andi_tl(cpu_gprh[rD(ctx->opcode)], cpu_gprh[rA(ctx->opcode)], ~0x80000000);
1c97856d
AJ
9356#endif
9357}
636aa200 9358static inline void gen_efdnabs(DisasContext *ctx)
1c97856d
AJ
9359{
9360 if (unlikely(!ctx->spe_enabled)) {
27a69bb0 9361 gen_exception(ctx, POWERPC_EXCP_SPEU);
1c97856d
AJ
9362 return;
9363 }
9364#if defined(TARGET_PPC64)
6d5c34fa 9365 tcg_gen_ori_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)], 0x8000000000000000LL);
1c97856d 9366#else
6d5c34fa
MP
9367 tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)]);
9368 tcg_gen_ori_tl(cpu_gprh[rD(ctx->opcode)], cpu_gprh[rA(ctx->opcode)], 0x80000000);
1c97856d
AJ
9369#endif
9370}
636aa200 9371static inline void gen_efdneg(DisasContext *ctx)
1c97856d
AJ
9372{
9373 if (unlikely(!ctx->spe_enabled)) {
27a69bb0 9374 gen_exception(ctx, POWERPC_EXCP_SPEU);
1c97856d
AJ
9375 return;
9376 }
9377#if defined(TARGET_PPC64)
6d5c34fa 9378 tcg_gen_xori_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)], 0x8000000000000000LL);
1c97856d 9379#else
6d5c34fa
MP
9380 tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)]);
9381 tcg_gen_xori_tl(cpu_gprh[rD(ctx->opcode)], cpu_gprh[rA(ctx->opcode)], 0x80000000);
1c97856d
AJ
9382#endif
9383}
9384
0487d6a8 9385/* Conversion */
1c97856d
AJ
9386GEN_SPEFPUOP_CONV_64_32(efdcfui);
9387GEN_SPEFPUOP_CONV_64_32(efdcfsi);
9388GEN_SPEFPUOP_CONV_64_32(efdcfuf);
9389GEN_SPEFPUOP_CONV_64_32(efdcfsf);
9390GEN_SPEFPUOP_CONV_32_64(efdctui);
9391GEN_SPEFPUOP_CONV_32_64(efdctsi);
9392GEN_SPEFPUOP_CONV_32_64(efdctuf);
9393GEN_SPEFPUOP_CONV_32_64(efdctsf);
9394GEN_SPEFPUOP_CONV_32_64(efdctuiz);
9395GEN_SPEFPUOP_CONV_32_64(efdctsiz);
9396GEN_SPEFPUOP_CONV_64_32(efdcfs);
9397GEN_SPEFPUOP_CONV_64_64(efdcfuid);
9398GEN_SPEFPUOP_CONV_64_64(efdcfsid);
9399GEN_SPEFPUOP_CONV_64_64(efdctuidz);
9400GEN_SPEFPUOP_CONV_64_64(efdctsidz);
0487d6a8 9401
0487d6a8 9402/* Comparison */
1c97856d
AJ
9403GEN_SPEFPUOP_COMP_64(efdcmpgt);
9404GEN_SPEFPUOP_COMP_64(efdcmplt);
9405GEN_SPEFPUOP_COMP_64(efdcmpeq);
9406GEN_SPEFPUOP_COMP_64(efdtstgt);
9407GEN_SPEFPUOP_COMP_64(efdtstlt);
9408GEN_SPEFPUOP_COMP_64(efdtsteq);
0487d6a8
JM
9409
9410/* Opcodes definitions */
70560da7
FC
9411GEN_SPE(efdadd, efdsub, 0x10, 0x0B, 0x00000000, 0x00000000, PPC_SPE_DOUBLE); //
9412GEN_SPE(efdcfuid, efdcfsid, 0x11, 0x0B, 0x00180000, 0x00180000, PPC_SPE_DOUBLE); //
9413GEN_SPE(efdabs, efdnabs, 0x12, 0x0B, 0x0000F800, 0x0000F800, PPC_SPE_DOUBLE); //
9414GEN_SPE(efdneg, speundef, 0x13, 0x0B, 0x0000F800, 0xFFFFFFFF, PPC_SPE_DOUBLE); //
9415GEN_SPE(efdmul, efddiv, 0x14, 0x0B, 0x00000000, 0x00000000, PPC_SPE_DOUBLE); //
9416GEN_SPE(efdctuidz, efdctsidz, 0x15, 0x0B, 0x00180000, 0x00180000, PPC_SPE_DOUBLE); //
9417GEN_SPE(efdcmpgt, efdcmplt, 0x16, 0x0B, 0x00600000, 0x00600000, PPC_SPE_DOUBLE); //
9418GEN_SPE(efdcmpeq, efdcfs, 0x17, 0x0B, 0x00600000, 0x00180000, PPC_SPE_DOUBLE); //
9419GEN_SPE(efdcfui, efdcfsi, 0x18, 0x0B, 0x00180000, 0x00180000, PPC_SPE_DOUBLE); //
9420GEN_SPE(efdcfuf, efdcfsf, 0x19, 0x0B, 0x00180000, 0x00180000, PPC_SPE_DOUBLE); //
9421GEN_SPE(efdctui, efdctsi, 0x1A, 0x0B, 0x00180000, 0x00180000, PPC_SPE_DOUBLE); //
9422GEN_SPE(efdctuf, efdctsf, 0x1B, 0x0B, 0x00180000, 0x00180000, PPC_SPE_DOUBLE); //
9423GEN_SPE(efdctuiz, speundef, 0x1C, 0x0B, 0x00180000, 0xFFFFFFFF, PPC_SPE_DOUBLE); //
9424GEN_SPE(efdctsiz, speundef, 0x1D, 0x0B, 0x00180000, 0xFFFFFFFF, PPC_SPE_DOUBLE); //
9425GEN_SPE(efdtstgt, efdtstlt, 0x1E, 0x0B, 0x00600000, 0x00600000, PPC_SPE_DOUBLE); //
9426GEN_SPE(efdtsteq, speundef, 0x1F, 0x0B, 0x00600000, 0xFFFFFFFF, PPC_SPE_DOUBLE); //
0487d6a8 9427
c227f099 9428static opcode_t opcodes[] = {
5c55ff99
BS
9429GEN_HANDLER(invalid, 0x00, 0x00, 0x00, 0xFFFFFFFF, PPC_NONE),
9430GEN_HANDLER(cmp, 0x1F, 0x00, 0x00, 0x00400000, PPC_INTEGER),
9431GEN_HANDLER(cmpi, 0x0B, 0xFF, 0xFF, 0x00400000, PPC_INTEGER),
9432GEN_HANDLER(cmpl, 0x1F, 0x00, 0x01, 0x00400000, PPC_INTEGER),
9433GEN_HANDLER(cmpli, 0x0A, 0xFF, 0xFF, 0x00400000, PPC_INTEGER),
fcfda20f 9434GEN_HANDLER_E(cmpb, 0x1F, 0x1C, 0x0F, 0x00000001, PPC_NONE, PPC2_ISA205),
5c55ff99
BS
9435GEN_HANDLER(isel, 0x1F, 0x0F, 0xFF, 0x00000001, PPC_ISEL),
9436GEN_HANDLER(addi, 0x0E, 0xFF, 0xFF, 0x00000000, PPC_INTEGER),
9437GEN_HANDLER(addic, 0x0C, 0xFF, 0xFF, 0x00000000, PPC_INTEGER),
9438GEN_HANDLER2(addic_, "addic.", 0x0D, 0xFF, 0xFF, 0x00000000, PPC_INTEGER),
9439GEN_HANDLER(addis, 0x0F, 0xFF, 0xFF, 0x00000000, PPC_INTEGER),
9440GEN_HANDLER(mulhw, 0x1F, 0x0B, 0x02, 0x00000400, PPC_INTEGER),
9441GEN_HANDLER(mulhwu, 0x1F, 0x0B, 0x00, 0x00000400, PPC_INTEGER),
9442GEN_HANDLER(mullw, 0x1F, 0x0B, 0x07, 0x00000000, PPC_INTEGER),
9443GEN_HANDLER(mullwo, 0x1F, 0x0B, 0x17, 0x00000000, PPC_INTEGER),
9444GEN_HANDLER(mulli, 0x07, 0xFF, 0xFF, 0x00000000, PPC_INTEGER),
9445#if defined(TARGET_PPC64)
9446GEN_HANDLER(mulld, 0x1F, 0x09, 0x07, 0x00000000, PPC_64B),
9447#endif
9448GEN_HANDLER(neg, 0x1F, 0x08, 0x03, 0x0000F800, PPC_INTEGER),
9449GEN_HANDLER(nego, 0x1F, 0x08, 0x13, 0x0000F800, PPC_INTEGER),
9450GEN_HANDLER(subfic, 0x08, 0xFF, 0xFF, 0x00000000, PPC_INTEGER),
9451GEN_HANDLER2(andi_, "andi.", 0x1C, 0xFF, 0xFF, 0x00000000, PPC_INTEGER),
9452GEN_HANDLER2(andis_, "andis.", 0x1D, 0xFF, 0xFF, 0x00000000, PPC_INTEGER),
9453GEN_HANDLER(cntlzw, 0x1F, 0x1A, 0x00, 0x00000000, PPC_INTEGER),
9454GEN_HANDLER(or, 0x1F, 0x1C, 0x0D, 0x00000000, PPC_INTEGER),
9455GEN_HANDLER(xor, 0x1F, 0x1C, 0x09, 0x00000000, PPC_INTEGER),
9456GEN_HANDLER(ori, 0x18, 0xFF, 0xFF, 0x00000000, PPC_INTEGER),
9457GEN_HANDLER(oris, 0x19, 0xFF, 0xFF, 0x00000000, PPC_INTEGER),
9458GEN_HANDLER(xori, 0x1A, 0xFF, 0xFF, 0x00000000, PPC_INTEGER),
9459GEN_HANDLER(xoris, 0x1B, 0xFF, 0xFF, 0x00000000, PPC_INTEGER),
9460GEN_HANDLER(popcntb, 0x1F, 0x03, 0x03, 0x0000F801, PPC_POPCNTB),
eaabeef2 9461GEN_HANDLER(popcntw, 0x1F, 0x1A, 0x0b, 0x0000F801, PPC_POPCNTWD),
725bcec2 9462GEN_HANDLER_E(prtyw, 0x1F, 0x1A, 0x04, 0x0000F801, PPC_NONE, PPC2_ISA205),
5c55ff99 9463#if defined(TARGET_PPC64)
eaabeef2 9464GEN_HANDLER(popcntd, 0x1F, 0x1A, 0x0F, 0x0000F801, PPC_POPCNTWD),
5c55ff99 9465GEN_HANDLER(cntlzd, 0x1F, 0x1A, 0x01, 0x00000000, PPC_64B),
725bcec2 9466GEN_HANDLER_E(prtyd, 0x1F, 0x1A, 0x05, 0x0000F801, PPC_NONE, PPC2_ISA205),
86ba37ed 9467GEN_HANDLER_E(bpermd, 0x1F, 0x1C, 0x07, 0x00000001, PPC_NONE, PPC2_PERM_ISA206),
5c55ff99
BS
9468#endif
9469GEN_HANDLER(rlwimi, 0x14, 0xFF, 0xFF, 0x00000000, PPC_INTEGER),
9470GEN_HANDLER(rlwinm, 0x15, 0xFF, 0xFF, 0x00000000, PPC_INTEGER),
9471GEN_HANDLER(rlwnm, 0x17, 0xFF, 0xFF, 0x00000000, PPC_INTEGER),
9472GEN_HANDLER(slw, 0x1F, 0x18, 0x00, 0x00000000, PPC_INTEGER),
9473GEN_HANDLER(sraw, 0x1F, 0x18, 0x18, 0x00000000, PPC_INTEGER),
9474GEN_HANDLER(srawi, 0x1F, 0x18, 0x19, 0x00000000, PPC_INTEGER),
9475GEN_HANDLER(srw, 0x1F, 0x18, 0x10, 0x00000000, PPC_INTEGER),
9476#if defined(TARGET_PPC64)
9477GEN_HANDLER(sld, 0x1F, 0x1B, 0x00, 0x00000000, PPC_64B),
9478GEN_HANDLER(srad, 0x1F, 0x1A, 0x18, 0x00000000, PPC_64B),
9479GEN_HANDLER2(sradi0, "sradi", 0x1F, 0x1A, 0x19, 0x00000000, PPC_64B),
9480GEN_HANDLER2(sradi1, "sradi", 0x1F, 0x1B, 0x19, 0x00000000, PPC_64B),
9481GEN_HANDLER(srd, 0x1F, 0x1B, 0x10, 0x00000000, PPC_64B),
9482#endif
9483GEN_HANDLER(frsqrtes, 0x3B, 0x1A, 0xFF, 0x001F07C0, PPC_FLOAT_FRSQRTES),
9484GEN_HANDLER(fsqrt, 0x3F, 0x16, 0xFF, 0x001F07C0, PPC_FLOAT_FSQRT),
9485GEN_HANDLER(fsqrts, 0x3B, 0x16, 0xFF, 0x001F07C0, PPC_FLOAT_FSQRT),
9486GEN_HANDLER(fcmpo, 0x3F, 0x00, 0x01, 0x00600001, PPC_FLOAT),
9487GEN_HANDLER(fcmpu, 0x3F, 0x00, 0x00, 0x00600001, PPC_FLOAT),
bf45a2e6 9488GEN_HANDLER(fabs, 0x3F, 0x08, 0x08, 0x001F0000, PPC_FLOAT),
5c55ff99 9489GEN_HANDLER(fmr, 0x3F, 0x08, 0x02, 0x001F0000, PPC_FLOAT),
bf45a2e6
AJ
9490GEN_HANDLER(fnabs, 0x3F, 0x08, 0x04, 0x001F0000, PPC_FLOAT),
9491GEN_HANDLER(fneg, 0x3F, 0x08, 0x01, 0x001F0000, PPC_FLOAT),
f0332888 9492GEN_HANDLER_E(fcpsgn, 0x3F, 0x08, 0x00, 0x00000000, PPC_NONE, PPC2_ISA205),
097ec5d8
TM
9493GEN_HANDLER_E(fmrgew, 0x3F, 0x06, 0x1E, 0x00000001, PPC_NONE, PPC2_VSX207),
9494GEN_HANDLER_E(fmrgow, 0x3F, 0x06, 0x1A, 0x00000001, PPC_NONE, PPC2_VSX207),
5c55ff99
BS
9495GEN_HANDLER(mcrfs, 0x3F, 0x00, 0x02, 0x0063F801, PPC_FLOAT),
9496GEN_HANDLER(mffs, 0x3F, 0x07, 0x12, 0x001FF800, PPC_FLOAT),
9497GEN_HANDLER(mtfsb0, 0x3F, 0x06, 0x02, 0x001FF800, PPC_FLOAT),
9498GEN_HANDLER(mtfsb1, 0x3F, 0x06, 0x01, 0x001FF800, PPC_FLOAT),
7d08d856
AJ
9499GEN_HANDLER(mtfsf, 0x3F, 0x07, 0x16, 0x00000000, PPC_FLOAT),
9500GEN_HANDLER(mtfsfi, 0x3F, 0x06, 0x04, 0x006e0800, PPC_FLOAT),
5c55ff99
BS
9501#if defined(TARGET_PPC64)
9502GEN_HANDLER(ld, 0x3A, 0xFF, 0xFF, 0x00000000, PPC_64B),
9503GEN_HANDLER(lq, 0x38, 0xFF, 0xFF, 0x00000000, PPC_64BX),
9504GEN_HANDLER(std, 0x3E, 0xFF, 0xFF, 0x00000000, PPC_64B),
9505#endif
9506GEN_HANDLER(lmw, 0x2E, 0xFF, 0xFF, 0x00000000, PPC_INTEGER),
9507GEN_HANDLER(stmw, 0x2F, 0xFF, 0xFF, 0x00000000, PPC_INTEGER),
9508GEN_HANDLER(lswi, 0x1F, 0x15, 0x12, 0x00000001, PPC_STRING),
9509GEN_HANDLER(lswx, 0x1F, 0x15, 0x10, 0x00000001, PPC_STRING),
9510GEN_HANDLER(stswi, 0x1F, 0x15, 0x16, 0x00000001, PPC_STRING),
9511GEN_HANDLER(stswx, 0x1F, 0x15, 0x14, 0x00000001, PPC_STRING),
9512GEN_HANDLER(eieio, 0x1F, 0x16, 0x1A, 0x03FFF801, PPC_MEM_EIEIO),
9513GEN_HANDLER(isync, 0x13, 0x16, 0x04, 0x03FFF801, PPC_MEM),
f844c817 9514GEN_HANDLER(lwarx, 0x1F, 0x14, 0x00, 0x00000000, PPC_RES),
5c55ff99
BS
9515GEN_HANDLER2(stwcx_, "stwcx.", 0x1F, 0x16, 0x04, 0x00000000, PPC_RES),
9516#if defined(TARGET_PPC64)
f844c817 9517GEN_HANDLER(ldarx, 0x1F, 0x14, 0x02, 0x00000000, PPC_64B),
5c55ff99
BS
9518GEN_HANDLER2(stdcx_, "stdcx.", 0x1F, 0x16, 0x06, 0x00000000, PPC_64B),
9519#endif
9520GEN_HANDLER(sync, 0x1F, 0x16, 0x12, 0x039FF801, PPC_MEM_SYNC),
9521GEN_HANDLER(wait, 0x1F, 0x1E, 0x01, 0x03FFF801, PPC_WAIT),
9522GEN_HANDLER(b, 0x12, 0xFF, 0xFF, 0x00000000, PPC_FLOW),
9523GEN_HANDLER(bc, 0x10, 0xFF, 0xFF, 0x00000000, PPC_FLOW),
9524GEN_HANDLER(bcctr, 0x13, 0x10, 0x10, 0x00000000, PPC_FLOW),
9525GEN_HANDLER(bclr, 0x13, 0x10, 0x00, 0x00000000, PPC_FLOW),
9526GEN_HANDLER(mcrf, 0x13, 0x00, 0xFF, 0x00000001, PPC_INTEGER),
9527GEN_HANDLER(rfi, 0x13, 0x12, 0x01, 0x03FF8001, PPC_FLOW),
9528#if defined(TARGET_PPC64)
9529GEN_HANDLER(rfid, 0x13, 0x12, 0x00, 0x03FF8001, PPC_64B),
9530GEN_HANDLER(hrfid, 0x13, 0x12, 0x08, 0x03FF8001, PPC_64H),
9531#endif
9532GEN_HANDLER(sc, 0x11, 0xFF, 0xFF, 0x03FFF01D, PPC_FLOW),
9533GEN_HANDLER(tw, 0x1F, 0x04, 0x00, 0x00000001, PPC_FLOW),
9534GEN_HANDLER(twi, 0x03, 0xFF, 0xFF, 0x00000000, PPC_FLOW),
9535#if defined(TARGET_PPC64)
9536GEN_HANDLER(td, 0x1F, 0x04, 0x02, 0x00000001, PPC_64B),
9537GEN_HANDLER(tdi, 0x02, 0xFF, 0xFF, 0x00000000, PPC_64B),
9538#endif
9539GEN_HANDLER(mcrxr, 0x1F, 0x00, 0x10, 0x007FF801, PPC_MISC),
9540GEN_HANDLER(mfcr, 0x1F, 0x13, 0x00, 0x00000801, PPC_MISC),
9541GEN_HANDLER(mfmsr, 0x1F, 0x13, 0x02, 0x001FF801, PPC_MISC),
9542GEN_HANDLER(mfspr, 0x1F, 0x13, 0x0A, 0x00000001, PPC_MISC),
9543GEN_HANDLER(mftb, 0x1F, 0x13, 0x0B, 0x00000001, PPC_MFTB),
9544GEN_HANDLER(mtcrf, 0x1F, 0x10, 0x04, 0x00000801, PPC_MISC),
9545#if defined(TARGET_PPC64)
9546GEN_HANDLER(mtmsrd, 0x1F, 0x12, 0x05, 0x001EF801, PPC_64B),
9547#endif
9548GEN_HANDLER(mtmsr, 0x1F, 0x12, 0x04, 0x001FF801, PPC_MISC),
9549GEN_HANDLER(mtspr, 0x1F, 0x13, 0x0E, 0x00000001, PPC_MISC),
9550GEN_HANDLER(dcbf, 0x1F, 0x16, 0x02, 0x03C00001, PPC_CACHE),
9551GEN_HANDLER(dcbi, 0x1F, 0x16, 0x0E, 0x03E00001, PPC_CACHE),
9552GEN_HANDLER(dcbst, 0x1F, 0x16, 0x01, 0x03E00001, PPC_CACHE),
9553GEN_HANDLER(dcbt, 0x1F, 0x16, 0x08, 0x02000001, PPC_CACHE),
9554GEN_HANDLER(dcbtst, 0x1F, 0x16, 0x07, 0x02000001, PPC_CACHE),
8e33944f 9555GEN_HANDLER(dcbz, 0x1F, 0x16, 0x1F, 0x03C00001, PPC_CACHE_DCBZ),
5c55ff99
BS
9556GEN_HANDLER(dst, 0x1F, 0x16, 0x0A, 0x01800001, PPC_ALTIVEC),
9557GEN_HANDLER(dstst, 0x1F, 0x16, 0x0B, 0x02000001, PPC_ALTIVEC),
9558GEN_HANDLER(dss, 0x1F, 0x16, 0x19, 0x019FF801, PPC_ALTIVEC),
9559GEN_HANDLER(icbi, 0x1F, 0x16, 0x1E, 0x03E00001, PPC_CACHE_ICBI),
9560GEN_HANDLER(dcba, 0x1F, 0x16, 0x17, 0x03E00001, PPC_CACHE_DCBA),
9561GEN_HANDLER(mfsr, 0x1F, 0x13, 0x12, 0x0010F801, PPC_SEGMENT),
9562GEN_HANDLER(mfsrin, 0x1F, 0x13, 0x14, 0x001F0001, PPC_SEGMENT),
9563GEN_HANDLER(mtsr, 0x1F, 0x12, 0x06, 0x0010F801, PPC_SEGMENT),
9564GEN_HANDLER(mtsrin, 0x1F, 0x12, 0x07, 0x001F0001, PPC_SEGMENT),
9565#if defined(TARGET_PPC64)
9566GEN_HANDLER2(mfsr_64b, "mfsr", 0x1F, 0x13, 0x12, 0x0010F801, PPC_SEGMENT_64B),
9567GEN_HANDLER2(mfsrin_64b, "mfsrin", 0x1F, 0x13, 0x14, 0x001F0001,
9568 PPC_SEGMENT_64B),
9569GEN_HANDLER2(mtsr_64b, "mtsr", 0x1F, 0x12, 0x06, 0x0010F801, PPC_SEGMENT_64B),
9570GEN_HANDLER2(mtsrin_64b, "mtsrin", 0x1F, 0x12, 0x07, 0x001F0001,
9571 PPC_SEGMENT_64B),
efdef95f
DG
9572GEN_HANDLER2(slbmte, "slbmte", 0x1F, 0x12, 0x0C, 0x001F0001, PPC_SEGMENT_64B),
9573GEN_HANDLER2(slbmfee, "slbmfee", 0x1F, 0x13, 0x1C, 0x001F0001, PPC_SEGMENT_64B),
9574GEN_HANDLER2(slbmfev, "slbmfev", 0x1F, 0x13, 0x1A, 0x001F0001, PPC_SEGMENT_64B),
5c55ff99
BS
9575#endif
9576GEN_HANDLER(tlbia, 0x1F, 0x12, 0x0B, 0x03FFFC01, PPC_MEM_TLBIA),
9577GEN_HANDLER(tlbiel, 0x1F, 0x12, 0x08, 0x03FF0001, PPC_MEM_TLBIE),
9578GEN_HANDLER(tlbie, 0x1F, 0x12, 0x09, 0x03FF0001, PPC_MEM_TLBIE),
9579GEN_HANDLER(tlbsync, 0x1F, 0x16, 0x11, 0x03FFF801, PPC_MEM_TLBSYNC),
9580#if defined(TARGET_PPC64)
9581GEN_HANDLER(slbia, 0x1F, 0x12, 0x0F, 0x03FFFC01, PPC_SLBI),
9582GEN_HANDLER(slbie, 0x1F, 0x12, 0x0D, 0x03FF0001, PPC_SLBI),
9583#endif
9584GEN_HANDLER(eciwx, 0x1F, 0x16, 0x0D, 0x00000001, PPC_EXTERN),
9585GEN_HANDLER(ecowx, 0x1F, 0x16, 0x09, 0x00000001, PPC_EXTERN),
9586GEN_HANDLER(abs, 0x1F, 0x08, 0x0B, 0x0000F800, PPC_POWER_BR),
9587GEN_HANDLER(abso, 0x1F, 0x08, 0x1B, 0x0000F800, PPC_POWER_BR),
9588GEN_HANDLER(clcs, 0x1F, 0x10, 0x13, 0x0000F800, PPC_POWER_BR),
9589GEN_HANDLER(div, 0x1F, 0x0B, 0x0A, 0x00000000, PPC_POWER_BR),
9590GEN_HANDLER(divo, 0x1F, 0x0B, 0x1A, 0x00000000, PPC_POWER_BR),
9591GEN_HANDLER(divs, 0x1F, 0x0B, 0x0B, 0x00000000, PPC_POWER_BR),
9592GEN_HANDLER(divso, 0x1F, 0x0B, 0x1B, 0x00000000, PPC_POWER_BR),
9593GEN_HANDLER(doz, 0x1F, 0x08, 0x08, 0x00000000, PPC_POWER_BR),
9594GEN_HANDLER(dozo, 0x1F, 0x08, 0x18, 0x00000000, PPC_POWER_BR),
9595GEN_HANDLER(dozi, 0x09, 0xFF, 0xFF, 0x00000000, PPC_POWER_BR),
9596GEN_HANDLER(lscbx, 0x1F, 0x15, 0x08, 0x00000000, PPC_POWER_BR),
9597GEN_HANDLER(maskg, 0x1F, 0x1D, 0x00, 0x00000000, PPC_POWER_BR),
9598GEN_HANDLER(maskir, 0x1F, 0x1D, 0x10, 0x00000000, PPC_POWER_BR),
9599GEN_HANDLER(mul, 0x1F, 0x0B, 0x03, 0x00000000, PPC_POWER_BR),
9600GEN_HANDLER(mulo, 0x1F, 0x0B, 0x13, 0x00000000, PPC_POWER_BR),
9601GEN_HANDLER(nabs, 0x1F, 0x08, 0x0F, 0x00000000, PPC_POWER_BR),
9602GEN_HANDLER(nabso, 0x1F, 0x08, 0x1F, 0x00000000, PPC_POWER_BR),
9603GEN_HANDLER(rlmi, 0x16, 0xFF, 0xFF, 0x00000000, PPC_POWER_BR),
9604GEN_HANDLER(rrib, 0x1F, 0x19, 0x10, 0x00000000, PPC_POWER_BR),
9605GEN_HANDLER(sle, 0x1F, 0x19, 0x04, 0x00000000, PPC_POWER_BR),
9606GEN_HANDLER(sleq, 0x1F, 0x19, 0x06, 0x00000000, PPC_POWER_BR),
9607GEN_HANDLER(sliq, 0x1F, 0x18, 0x05, 0x00000000, PPC_POWER_BR),
9608GEN_HANDLER(slliq, 0x1F, 0x18, 0x07, 0x00000000, PPC_POWER_BR),
9609GEN_HANDLER(sllq, 0x1F, 0x18, 0x06, 0x00000000, PPC_POWER_BR),
9610GEN_HANDLER(slq, 0x1F, 0x18, 0x04, 0x00000000, PPC_POWER_BR),
9611GEN_HANDLER(sraiq, 0x1F, 0x18, 0x1D, 0x00000000, PPC_POWER_BR),
9612GEN_HANDLER(sraq, 0x1F, 0x18, 0x1C, 0x00000000, PPC_POWER_BR),
9613GEN_HANDLER(sre, 0x1F, 0x19, 0x14, 0x00000000, PPC_POWER_BR),
9614GEN_HANDLER(srea, 0x1F, 0x19, 0x1C, 0x00000000, PPC_POWER_BR),
9615GEN_HANDLER(sreq, 0x1F, 0x19, 0x16, 0x00000000, PPC_POWER_BR),
9616GEN_HANDLER(sriq, 0x1F, 0x18, 0x15, 0x00000000, PPC_POWER_BR),
9617GEN_HANDLER(srliq, 0x1F, 0x18, 0x17, 0x00000000, PPC_POWER_BR),
9618GEN_HANDLER(srlq, 0x1F, 0x18, 0x16, 0x00000000, PPC_POWER_BR),
9619GEN_HANDLER(srq, 0x1F, 0x18, 0x14, 0x00000000, PPC_POWER_BR),
9620GEN_HANDLER(dsa, 0x1F, 0x14, 0x13, 0x03FFF801, PPC_602_SPEC),
9621GEN_HANDLER(esa, 0x1F, 0x14, 0x12, 0x03FFF801, PPC_602_SPEC),
9622GEN_HANDLER(mfrom, 0x1F, 0x09, 0x08, 0x03E0F801, PPC_602_SPEC),
9623GEN_HANDLER2(tlbld_6xx, "tlbld", 0x1F, 0x12, 0x1E, 0x03FF0001, PPC_6xx_TLB),
9624GEN_HANDLER2(tlbli_6xx, "tlbli", 0x1F, 0x12, 0x1F, 0x03FF0001, PPC_6xx_TLB),
9625GEN_HANDLER2(tlbld_74xx, "tlbld", 0x1F, 0x12, 0x1E, 0x03FF0001, PPC_74xx_TLB),
9626GEN_HANDLER2(tlbli_74xx, "tlbli", 0x1F, 0x12, 0x1F, 0x03FF0001, PPC_74xx_TLB),
9627GEN_HANDLER(clf, 0x1F, 0x16, 0x03, 0x03E00000, PPC_POWER),
9628GEN_HANDLER(cli, 0x1F, 0x16, 0x0F, 0x03E00000, PPC_POWER),
9629GEN_HANDLER(dclst, 0x1F, 0x16, 0x13, 0x03E00000, PPC_POWER),
9630GEN_HANDLER(mfsri, 0x1F, 0x13, 0x13, 0x00000001, PPC_POWER),
9631GEN_HANDLER(rac, 0x1F, 0x12, 0x19, 0x00000001, PPC_POWER),
9632GEN_HANDLER(rfsvc, 0x13, 0x12, 0x02, 0x03FFF0001, PPC_POWER),
9633GEN_HANDLER(lfq, 0x38, 0xFF, 0xFF, 0x00000003, PPC_POWER2),
9634GEN_HANDLER(lfqu, 0x39, 0xFF, 0xFF, 0x00000003, PPC_POWER2),
9635GEN_HANDLER(lfqux, 0x1F, 0x17, 0x19, 0x00000001, PPC_POWER2),
9636GEN_HANDLER(lfqx, 0x1F, 0x17, 0x18, 0x00000001, PPC_POWER2),
9637GEN_HANDLER(stfq, 0x3C, 0xFF, 0xFF, 0x00000003, PPC_POWER2),
9638GEN_HANDLER(stfqu, 0x3D, 0xFF, 0xFF, 0x00000003, PPC_POWER2),
9639GEN_HANDLER(stfqux, 0x1F, 0x17, 0x1D, 0x00000001, PPC_POWER2),
9640GEN_HANDLER(stfqx, 0x1F, 0x17, 0x1C, 0x00000001, PPC_POWER2),
9641GEN_HANDLER(mfapidi, 0x1F, 0x13, 0x08, 0x0000F801, PPC_MFAPIDI),
9642GEN_HANDLER(tlbiva, 0x1F, 0x12, 0x18, 0x03FFF801, PPC_TLBIVA),
9643GEN_HANDLER(mfdcr, 0x1F, 0x03, 0x0A, 0x00000001, PPC_DCR),
9644GEN_HANDLER(mtdcr, 0x1F, 0x03, 0x0E, 0x00000001, PPC_DCR),
9645GEN_HANDLER(mfdcrx, 0x1F, 0x03, 0x08, 0x00000000, PPC_DCRX),
9646GEN_HANDLER(mtdcrx, 0x1F, 0x03, 0x0C, 0x00000000, PPC_DCRX),
9647GEN_HANDLER(mfdcrux, 0x1F, 0x03, 0x09, 0x00000000, PPC_DCRUX),
9648GEN_HANDLER(mtdcrux, 0x1F, 0x03, 0x0D, 0x00000000, PPC_DCRUX),
9649GEN_HANDLER(dccci, 0x1F, 0x06, 0x0E, 0x03E00001, PPC_4xx_COMMON),
9650GEN_HANDLER(dcread, 0x1F, 0x06, 0x0F, 0x00000001, PPC_4xx_COMMON),
9651GEN_HANDLER2(icbt_40x, "icbt", 0x1F, 0x06, 0x08, 0x03E00001, PPC_40x_ICBT),
9652GEN_HANDLER(iccci, 0x1F, 0x06, 0x1E, 0x00000001, PPC_4xx_COMMON),
9653GEN_HANDLER(icread, 0x1F, 0x06, 0x1F, 0x03E00001, PPC_4xx_COMMON),
9654GEN_HANDLER2(rfci_40x, "rfci", 0x13, 0x13, 0x01, 0x03FF8001, PPC_40x_EXCP),
01662f3e 9655GEN_HANDLER_E(rfci, 0x13, 0x13, 0x01, 0x03FF8001, PPC_BOOKE, PPC2_BOOKE206),
5c55ff99
BS
9656GEN_HANDLER(rfdi, 0x13, 0x07, 0x01, 0x03FF8001, PPC_RFDI),
9657GEN_HANDLER(rfmci, 0x13, 0x06, 0x01, 0x03FF8001, PPC_RFMCI),
9658GEN_HANDLER2(tlbre_40x, "tlbre", 0x1F, 0x12, 0x1D, 0x00000001, PPC_40x_TLB),
9659GEN_HANDLER2(tlbsx_40x, "tlbsx", 0x1F, 0x12, 0x1C, 0x00000000, PPC_40x_TLB),
9660GEN_HANDLER2(tlbwe_40x, "tlbwe", 0x1F, 0x12, 0x1E, 0x00000001, PPC_40x_TLB),
9661GEN_HANDLER2(tlbre_440, "tlbre", 0x1F, 0x12, 0x1D, 0x00000001, PPC_BOOKE),
9662GEN_HANDLER2(tlbsx_440, "tlbsx", 0x1F, 0x12, 0x1C, 0x00000000, PPC_BOOKE),
9663GEN_HANDLER2(tlbwe_440, "tlbwe", 0x1F, 0x12, 0x1E, 0x00000001, PPC_BOOKE),
01662f3e
AG
9664GEN_HANDLER2_E(tlbre_booke206, "tlbre", 0x1F, 0x12, 0x1D, 0x00000001,
9665 PPC_NONE, PPC2_BOOKE206),
9666GEN_HANDLER2_E(tlbsx_booke206, "tlbsx", 0x1F, 0x12, 0x1C, 0x00000000,
9667 PPC_NONE, PPC2_BOOKE206),
9668GEN_HANDLER2_E(tlbwe_booke206, "tlbwe", 0x1F, 0x12, 0x1E, 0x00000001,
9669 PPC_NONE, PPC2_BOOKE206),
9670GEN_HANDLER2_E(tlbivax_booke206, "tlbivax", 0x1F, 0x12, 0x18, 0x00000001,
9671 PPC_NONE, PPC2_BOOKE206),
6d3db821
AG
9672GEN_HANDLER2_E(tlbilx_booke206, "tlbilx", 0x1F, 0x12, 0x00, 0x03800001,
9673 PPC_NONE, PPC2_BOOKE206),
d5d11a39
AG
9674GEN_HANDLER2_E(msgsnd, "msgsnd", 0x1F, 0x0E, 0x06, 0x03ff0001,
9675 PPC_NONE, PPC2_PRCNTL),
9e0b5cb1
AG
9676GEN_HANDLER2_E(msgclr, "msgclr", 0x1F, 0x0E, 0x07, 0x03ff0001,
9677 PPC_NONE, PPC2_PRCNTL),
5c55ff99 9678GEN_HANDLER(wrtee, 0x1F, 0x03, 0x04, 0x000FFC01, PPC_WRTEE),
fbe73008 9679GEN_HANDLER(wrteei, 0x1F, 0x03, 0x05, 0x000E7C01, PPC_WRTEE),
5c55ff99 9680GEN_HANDLER(dlmzb, 0x1F, 0x0E, 0x02, 0x00000000, PPC_440_SPEC),
01662f3e
AG
9681GEN_HANDLER_E(mbar, 0x1F, 0x16, 0x1a, 0x001FF801,
9682 PPC_BOOKE, PPC2_BOOKE206),
dcb2b9e1 9683GEN_HANDLER(msync_4xx, 0x1F, 0x16, 0x12, 0x03FFF801, PPC_BOOKE),
01662f3e
AG
9684GEN_HANDLER2_E(icbt_440, "icbt", 0x1F, 0x16, 0x00, 0x03E00001,
9685 PPC_BOOKE, PPC2_BOOKE206),
5c55ff99
BS
9686GEN_HANDLER(lvsl, 0x1f, 0x06, 0x00, 0x00000001, PPC_ALTIVEC),
9687GEN_HANDLER(lvsr, 0x1f, 0x06, 0x01, 0x00000001, PPC_ALTIVEC),
9688GEN_HANDLER(mfvscr, 0x04, 0x2, 0x18, 0x001ff800, PPC_ALTIVEC),
9689GEN_HANDLER(mtvscr, 0x04, 0x2, 0x19, 0x03ff0000, PPC_ALTIVEC),
9690GEN_HANDLER(vsldoi, 0x04, 0x16, 0xFF, 0x00000400, PPC_ALTIVEC),
9691GEN_HANDLER(vmladduhm, 0x04, 0x11, 0xFF, 0x00000000, PPC_ALTIVEC),
9692GEN_HANDLER2(evsel0, "evsel", 0x04, 0x1c, 0x09, 0x00000000, PPC_SPE),
9693GEN_HANDLER2(evsel1, "evsel", 0x04, 0x1d, 0x09, 0x00000000, PPC_SPE),
9694GEN_HANDLER2(evsel2, "evsel", 0x04, 0x1e, 0x09, 0x00000000, PPC_SPE),
9695GEN_HANDLER2(evsel3, "evsel", 0x04, 0x1f, 0x09, 0x00000000, PPC_SPE),
9696
9697#undef GEN_INT_ARITH_ADD
9698#undef GEN_INT_ARITH_ADD_CONST
9699#define GEN_INT_ARITH_ADD(name, opc3, add_ca, compute_ca, compute_ov) \
9700GEN_HANDLER(name, 0x1F, 0x0A, opc3, 0x00000000, PPC_INTEGER),
9701#define GEN_INT_ARITH_ADD_CONST(name, opc3, const_val, \
9702 add_ca, compute_ca, compute_ov) \
9703GEN_HANDLER(name, 0x1F, 0x0A, opc3, 0x0000F800, PPC_INTEGER),
9704GEN_INT_ARITH_ADD(add, 0x08, 0, 0, 0)
9705GEN_INT_ARITH_ADD(addo, 0x18, 0, 0, 1)
9706GEN_INT_ARITH_ADD(addc, 0x00, 0, 1, 0)
9707GEN_INT_ARITH_ADD(addco, 0x10, 0, 1, 1)
9708GEN_INT_ARITH_ADD(adde, 0x04, 1, 1, 0)
9709GEN_INT_ARITH_ADD(addeo, 0x14, 1, 1, 1)
9710GEN_INT_ARITH_ADD_CONST(addme, 0x07, -1LL, 1, 1, 0)
9711GEN_INT_ARITH_ADD_CONST(addmeo, 0x17, -1LL, 1, 1, 1)
9712GEN_INT_ARITH_ADD_CONST(addze, 0x06, 0, 1, 1, 0)
9713GEN_INT_ARITH_ADD_CONST(addzeo, 0x16, 0, 1, 1, 1)
9714
9715#undef GEN_INT_ARITH_DIVW
9716#define GEN_INT_ARITH_DIVW(name, opc3, sign, compute_ov) \
9717GEN_HANDLER(name, 0x1F, 0x0B, opc3, 0x00000000, PPC_INTEGER)
9718GEN_INT_ARITH_DIVW(divwu, 0x0E, 0, 0),
9719GEN_INT_ARITH_DIVW(divwuo, 0x1E, 0, 1),
9720GEN_INT_ARITH_DIVW(divw, 0x0F, 1, 0),
9721GEN_INT_ARITH_DIVW(divwo, 0x1F, 1, 1),
6a4fda33
TM
9722GEN_HANDLER_E(divweu, 0x1F, 0x0B, 0x0C, 0, PPC_NONE, PPC2_DIVE_ISA206),
9723GEN_HANDLER_E(divweuo, 0x1F, 0x0B, 0x1C, 0, PPC_NONE, PPC2_DIVE_ISA206),
5c55ff99
BS
9724
9725#if defined(TARGET_PPC64)
9726#undef GEN_INT_ARITH_DIVD
9727#define GEN_INT_ARITH_DIVD(name, opc3, sign, compute_ov) \
9728GEN_HANDLER(name, 0x1F, 0x09, opc3, 0x00000000, PPC_64B)
9729GEN_INT_ARITH_DIVD(divdu, 0x0E, 0, 0),
9730GEN_INT_ARITH_DIVD(divduo, 0x1E, 0, 1),
9731GEN_INT_ARITH_DIVD(divd, 0x0F, 1, 0),
9732GEN_INT_ARITH_DIVD(divdo, 0x1F, 1, 1),
9733
98d1eb27
TM
9734GEN_HANDLER_E(divdeu, 0x1F, 0x09, 0x0C, 0, PPC_NONE, PPC2_DIVE_ISA206),
9735GEN_HANDLER_E(divdeuo, 0x1F, 0x09, 0x1C, 0, PPC_NONE, PPC2_DIVE_ISA206),
e44259b6
TM
9736GEN_HANDLER_E(divde, 0x1F, 0x09, 0x0D, 0, PPC_NONE, PPC2_DIVE_ISA206),
9737GEN_HANDLER_E(divdeo, 0x1F, 0x09, 0x1D, 0, PPC_NONE, PPC2_DIVE_ISA206),
98d1eb27 9738
5c55ff99
BS
9739#undef GEN_INT_ARITH_MUL_HELPER
9740#define GEN_INT_ARITH_MUL_HELPER(name, opc3) \
9741GEN_HANDLER(name, 0x1F, 0x09, opc3, 0x00000000, PPC_64B)
9742GEN_INT_ARITH_MUL_HELPER(mulhdu, 0x00),
9743GEN_INT_ARITH_MUL_HELPER(mulhd, 0x02),
9744GEN_INT_ARITH_MUL_HELPER(mulldo, 0x17),
9745#endif
9746
9747#undef GEN_INT_ARITH_SUBF
9748#undef GEN_INT_ARITH_SUBF_CONST
9749#define GEN_INT_ARITH_SUBF(name, opc3, add_ca, compute_ca, compute_ov) \
9750GEN_HANDLER(name, 0x1F, 0x08, opc3, 0x00000000, PPC_INTEGER),
9751#define GEN_INT_ARITH_SUBF_CONST(name, opc3, const_val, \
9752 add_ca, compute_ca, compute_ov) \
9753GEN_HANDLER(name, 0x1F, 0x08, opc3, 0x0000F800, PPC_INTEGER),
9754GEN_INT_ARITH_SUBF(subf, 0x01, 0, 0, 0)
9755GEN_INT_ARITH_SUBF(subfo, 0x11, 0, 0, 1)
9756GEN_INT_ARITH_SUBF(subfc, 0x00, 0, 1, 0)
9757GEN_INT_ARITH_SUBF(subfco, 0x10, 0, 1, 1)
9758GEN_INT_ARITH_SUBF(subfe, 0x04, 1, 1, 0)
9759GEN_INT_ARITH_SUBF(subfeo, 0x14, 1, 1, 1)
9760GEN_INT_ARITH_SUBF_CONST(subfme, 0x07, -1LL, 1, 1, 0)
9761GEN_INT_ARITH_SUBF_CONST(subfmeo, 0x17, -1LL, 1, 1, 1)
9762GEN_INT_ARITH_SUBF_CONST(subfze, 0x06, 0, 1, 1, 0)
9763GEN_INT_ARITH_SUBF_CONST(subfzeo, 0x16, 0, 1, 1, 1)
9764
9765#undef GEN_LOGICAL1
9766#undef GEN_LOGICAL2
9767#define GEN_LOGICAL2(name, tcg_op, opc, type) \
9768GEN_HANDLER(name, 0x1F, 0x1C, opc, 0x00000000, type)
9769#define GEN_LOGICAL1(name, tcg_op, opc, type) \
9770GEN_HANDLER(name, 0x1F, 0x1A, opc, 0x00000000, type)
9771GEN_LOGICAL2(and, tcg_gen_and_tl, 0x00, PPC_INTEGER),
9772GEN_LOGICAL2(andc, tcg_gen_andc_tl, 0x01, PPC_INTEGER),
9773GEN_LOGICAL2(eqv, tcg_gen_eqv_tl, 0x08, PPC_INTEGER),
9774GEN_LOGICAL1(extsb, tcg_gen_ext8s_tl, 0x1D, PPC_INTEGER),
9775GEN_LOGICAL1(extsh, tcg_gen_ext16s_tl, 0x1C, PPC_INTEGER),
9776GEN_LOGICAL2(nand, tcg_gen_nand_tl, 0x0E, PPC_INTEGER),
9777GEN_LOGICAL2(nor, tcg_gen_nor_tl, 0x03, PPC_INTEGER),
9778GEN_LOGICAL2(orc, tcg_gen_orc_tl, 0x0C, PPC_INTEGER),
9779#if defined(TARGET_PPC64)
9780GEN_LOGICAL1(extsw, tcg_gen_ext32s_tl, 0x1E, PPC_64B),
9781#endif
9782
9783#if defined(TARGET_PPC64)
9784#undef GEN_PPC64_R2
9785#undef GEN_PPC64_R4
9786#define GEN_PPC64_R2(name, opc1, opc2) \
9787GEN_HANDLER2(name##0, stringify(name), opc1, opc2, 0xFF, 0x00000000, PPC_64B),\
9788GEN_HANDLER2(name##1, stringify(name), opc1, opc2 | 0x10, 0xFF, 0x00000000, \
9789 PPC_64B)
9790#define GEN_PPC64_R4(name, opc1, opc2) \
9791GEN_HANDLER2(name##0, stringify(name), opc1, opc2, 0xFF, 0x00000000, PPC_64B),\
9792GEN_HANDLER2(name##1, stringify(name), opc1, opc2 | 0x01, 0xFF, 0x00000000, \
9793 PPC_64B), \
9794GEN_HANDLER2(name##2, stringify(name), opc1, opc2 | 0x10, 0xFF, 0x00000000, \
9795 PPC_64B), \
9796GEN_HANDLER2(name##3, stringify(name), opc1, opc2 | 0x11, 0xFF, 0x00000000, \
9797 PPC_64B)
9798GEN_PPC64_R4(rldicl, 0x1E, 0x00),
9799GEN_PPC64_R4(rldicr, 0x1E, 0x02),
9800GEN_PPC64_R4(rldic, 0x1E, 0x04),
9801GEN_PPC64_R2(rldcl, 0x1E, 0x08),
9802GEN_PPC64_R2(rldcr, 0x1E, 0x09),
9803GEN_PPC64_R4(rldimi, 0x1E, 0x06),
9804#endif
9805
9806#undef _GEN_FLOAT_ACB
9807#undef GEN_FLOAT_ACB
9808#undef _GEN_FLOAT_AB
9809#undef GEN_FLOAT_AB
9810#undef _GEN_FLOAT_AC
9811#undef GEN_FLOAT_AC
9812#undef GEN_FLOAT_B
9813#undef GEN_FLOAT_BS
9814#define _GEN_FLOAT_ACB(name, op, op1, op2, isfloat, set_fprf, type) \
9815GEN_HANDLER(f##name, op1, op2, 0xFF, 0x00000000, type)
9816#define GEN_FLOAT_ACB(name, op2, set_fprf, type) \
9817_GEN_FLOAT_ACB(name, name, 0x3F, op2, 0, set_fprf, type), \
9818_GEN_FLOAT_ACB(name##s, name, 0x3B, op2, 1, set_fprf, type)
9819#define _GEN_FLOAT_AB(name, op, op1, op2, inval, isfloat, set_fprf, type) \
9820GEN_HANDLER(f##name, op1, op2, 0xFF, inval, type)
9821#define GEN_FLOAT_AB(name, op2, inval, set_fprf, type) \
9822_GEN_FLOAT_AB(name, name, 0x3F, op2, inval, 0, set_fprf, type), \
9823_GEN_FLOAT_AB(name##s, name, 0x3B, op2, inval, 1, set_fprf, type)
9824#define _GEN_FLOAT_AC(name, op, op1, op2, inval, isfloat, set_fprf, type) \
9825GEN_HANDLER(f##name, op1, op2, 0xFF, inval, type)
9826#define GEN_FLOAT_AC(name, op2, inval, set_fprf, type) \
9827_GEN_FLOAT_AC(name, name, 0x3F, op2, inval, 0, set_fprf, type), \
9828_GEN_FLOAT_AC(name##s, name, 0x3B, op2, inval, 1, set_fprf, type)
9829#define GEN_FLOAT_B(name, op2, op3, set_fprf, type) \
9830GEN_HANDLER(f##name, 0x3F, op2, op3, 0x001F0000, type)
9831#define GEN_FLOAT_BS(name, op1, op2, set_fprf, type) \
9832GEN_HANDLER(f##name, op1, op2, 0xFF, 0x001F07C0, type)
9833
9834GEN_FLOAT_AB(add, 0x15, 0x000007C0, 1, PPC_FLOAT),
9835GEN_FLOAT_AB(div, 0x12, 0x000007C0, 1, PPC_FLOAT),
9836GEN_FLOAT_AC(mul, 0x19, 0x0000F800, 1, PPC_FLOAT),
9837GEN_FLOAT_BS(re, 0x3F, 0x18, 1, PPC_FLOAT_EXT),
9838GEN_FLOAT_BS(res, 0x3B, 0x18, 1, PPC_FLOAT_FRES),
9839GEN_FLOAT_BS(rsqrte, 0x3F, 0x1A, 1, PPC_FLOAT_FRSQRTE),
9840_GEN_FLOAT_ACB(sel, sel, 0x3F, 0x17, 0, 0, PPC_FLOAT_FSEL),
9841GEN_FLOAT_AB(sub, 0x14, 0x000007C0, 1, PPC_FLOAT),
9842GEN_FLOAT_ACB(madd, 0x1D, 1, PPC_FLOAT),
9843GEN_FLOAT_ACB(msub, 0x1C, 1, PPC_FLOAT),
9844GEN_FLOAT_ACB(nmadd, 0x1F, 1, PPC_FLOAT),
9845GEN_FLOAT_ACB(nmsub, 0x1E, 1, PPC_FLOAT),
9846GEN_FLOAT_B(ctiw, 0x0E, 0x00, 0, PPC_FLOAT),
9847GEN_FLOAT_B(ctiwz, 0x0F, 0x00, 0, PPC_FLOAT),
9848GEN_FLOAT_B(rsp, 0x0C, 0x00, 1, PPC_FLOAT),
9849#if defined(TARGET_PPC64)
9850GEN_FLOAT_B(cfid, 0x0E, 0x1A, 1, PPC_64B),
9851GEN_FLOAT_B(ctid, 0x0E, 0x19, 0, PPC_64B),
9852GEN_FLOAT_B(ctidz, 0x0F, 0x19, 0, PPC_64B),
9853#endif
9854GEN_FLOAT_B(rin, 0x08, 0x0C, 1, PPC_FLOAT_EXT),
9855GEN_FLOAT_B(riz, 0x08, 0x0D, 1, PPC_FLOAT_EXT),
9856GEN_FLOAT_B(rip, 0x08, 0x0E, 1, PPC_FLOAT_EXT),
9857GEN_FLOAT_B(rim, 0x08, 0x0F, 1, PPC_FLOAT_EXT),
5c55ff99
BS
9858
9859#undef GEN_LD
9860#undef GEN_LDU
9861#undef GEN_LDUX
cd6e9320 9862#undef GEN_LDX_E
5c55ff99
BS
9863#undef GEN_LDS
9864#define GEN_LD(name, ldop, opc, type) \
9865GEN_HANDLER(name, opc, 0xFF, 0xFF, 0x00000000, type),
9866#define GEN_LDU(name, ldop, opc, type) \
9867GEN_HANDLER(name##u, opc, 0xFF, 0xFF, 0x00000000, type),
9868#define GEN_LDUX(name, ldop, opc2, opc3, type) \
9869GEN_HANDLER(name##ux, 0x1F, opc2, opc3, 0x00000001, type),
cd6e9320
TH
9870#define GEN_LDX_E(name, ldop, opc2, opc3, type, type2) \
9871GEN_HANDLER_E(name##x, 0x1F, opc2, opc3, 0x00000001, type, type2),
5c55ff99
BS
9872#define GEN_LDS(name, ldop, op, type) \
9873GEN_LD(name, ldop, op | 0x20, type) \
9874GEN_LDU(name, ldop, op | 0x21, type) \
9875GEN_LDUX(name, ldop, 0x17, op | 0x01, type) \
9876GEN_LDX(name, ldop, 0x17, op | 0x00, type)
9877
9878GEN_LDS(lbz, ld8u, 0x02, PPC_INTEGER)
9879GEN_LDS(lha, ld16s, 0x0A, PPC_INTEGER)
9880GEN_LDS(lhz, ld16u, 0x08, PPC_INTEGER)
9881GEN_LDS(lwz, ld32u, 0x00, PPC_INTEGER)
9882#if defined(TARGET_PPC64)
9883GEN_LDUX(lwa, ld32s, 0x15, 0x0B, PPC_64B)
9884GEN_LDX(lwa, ld32s, 0x15, 0x0A, PPC_64B)
9885GEN_LDUX(ld, ld64, 0x15, 0x01, PPC_64B)
9886GEN_LDX(ld, ld64, 0x15, 0x00, PPC_64B)
cd6e9320 9887GEN_LDX_E(ldbr, ld64ur, 0x14, 0x10, PPC_NONE, PPC2_DBRX)
5c55ff99
BS
9888#endif
9889GEN_LDX(lhbr, ld16ur, 0x16, 0x18, PPC_INTEGER)
9890GEN_LDX(lwbr, ld32ur, 0x16, 0x10, PPC_INTEGER)
9891
9892#undef GEN_ST
9893#undef GEN_STU
9894#undef GEN_STUX
cd6e9320 9895#undef GEN_STX_E
5c55ff99
BS
9896#undef GEN_STS
9897#define GEN_ST(name, stop, opc, type) \
9898GEN_HANDLER(name, opc, 0xFF, 0xFF, 0x00000000, type),
9899#define GEN_STU(name, stop, opc, type) \
9900GEN_HANDLER(stop##u, opc, 0xFF, 0xFF, 0x00000000, type),
9901#define GEN_STUX(name, stop, opc2, opc3, type) \
9902GEN_HANDLER(name##ux, 0x1F, opc2, opc3, 0x00000001, type),
cd6e9320
TH
9903#define GEN_STX_E(name, stop, opc2, opc3, type, type2) \
9904GEN_HANDLER_E(name##x, 0x1F, opc2, opc3, 0x00000001, type, type2),
5c55ff99
BS
9905#define GEN_STS(name, stop, op, type) \
9906GEN_ST(name, stop, op | 0x20, type) \
9907GEN_STU(name, stop, op | 0x21, type) \
9908GEN_STUX(name, stop, 0x17, op | 0x01, type) \
9909GEN_STX(name, stop, 0x17, op | 0x00, type)
9910
9911GEN_STS(stb, st8, 0x06, PPC_INTEGER)
9912GEN_STS(sth, st16, 0x0C, PPC_INTEGER)
9913GEN_STS(stw, st32, 0x04, PPC_INTEGER)
9914#if defined(TARGET_PPC64)
9915GEN_STUX(std, st64, 0x15, 0x05, PPC_64B)
9916GEN_STX(std, st64, 0x15, 0x04, PPC_64B)
cd6e9320 9917GEN_STX_E(stdbr, st64r, 0x14, 0x14, PPC_NONE, PPC2_DBRX)
5c55ff99
BS
9918#endif
9919GEN_STX(sthbr, st16r, 0x16, 0x1C, PPC_INTEGER)
9920GEN_STX(stwbr, st32r, 0x16, 0x14, PPC_INTEGER)
9921
9922#undef GEN_LDF
9923#undef GEN_LDUF
9924#undef GEN_LDUXF
9925#undef GEN_LDXF
9926#undef GEN_LDFS
9927#define GEN_LDF(name, ldop, opc, type) \
9928GEN_HANDLER(name, opc, 0xFF, 0xFF, 0x00000000, type),
9929#define GEN_LDUF(name, ldop, opc, type) \
9930GEN_HANDLER(name##u, opc, 0xFF, 0xFF, 0x00000000, type),
9931#define GEN_LDUXF(name, ldop, opc, type) \
9932GEN_HANDLER(name##ux, 0x1F, 0x17, opc, 0x00000001, type),
9933#define GEN_LDXF(name, ldop, opc2, opc3, type) \
9934GEN_HANDLER(name##x, 0x1F, opc2, opc3, 0x00000001, type),
9935#define GEN_LDFS(name, ldop, op, type) \
9936GEN_LDF(name, ldop, op | 0x20, type) \
9937GEN_LDUF(name, ldop, op | 0x21, type) \
9938GEN_LDUXF(name, ldop, op | 0x01, type) \
9939GEN_LDXF(name, ldop, 0x17, op | 0x00, type)
9940
9941GEN_LDFS(lfd, ld64, 0x12, PPC_FLOAT)
9942GEN_LDFS(lfs, ld32fs, 0x10, PPC_FLOAT)
199f830d 9943GEN_HANDLER_E(lfiwax, 0x1f, 0x17, 0x1a, 0x00000001, PPC_NONE, PPC2_ISA205),
05050ee8
AJ
9944GEN_HANDLER_E(lfdp, 0x39, 0xFF, 0xFF, 0x00200003, PPC_NONE, PPC2_ISA205),
9945GEN_HANDLER_E(lfdpx, 0x1F, 0x17, 0x18, 0x00200001, PPC_NONE, PPC2_ISA205),
5c55ff99
BS
9946
9947#undef GEN_STF
9948#undef GEN_STUF
9949#undef GEN_STUXF
9950#undef GEN_STXF
9951#undef GEN_STFS
9952#define GEN_STF(name, stop, opc, type) \
9953GEN_HANDLER(name, opc, 0xFF, 0xFF, 0x00000000, type),
9954#define GEN_STUF(name, stop, opc, type) \
9955GEN_HANDLER(name##u, opc, 0xFF, 0xFF, 0x00000000, type),
9956#define GEN_STUXF(name, stop, opc, type) \
9957GEN_HANDLER(name##ux, 0x1F, 0x17, opc, 0x00000001, type),
9958#define GEN_STXF(name, stop, opc2, opc3, type) \
9959GEN_HANDLER(name##x, 0x1F, opc2, opc3, 0x00000001, type),
9960#define GEN_STFS(name, stop, op, type) \
9961GEN_STF(name, stop, op | 0x20, type) \
9962GEN_STUF(name, stop, op | 0x21, type) \
9963GEN_STUXF(name, stop, op | 0x01, type) \
9964GEN_STXF(name, stop, 0x17, op | 0x00, type)
9965
9966GEN_STFS(stfd, st64, 0x16, PPC_FLOAT)
9967GEN_STFS(stfs, st32fs, 0x14, PPC_FLOAT)
9968GEN_STXF(stfiw, st32fiw, 0x17, 0x1E, PPC_FLOAT_STFIWX)
44bc0c4d
AJ
9969GEN_HANDLER_E(stfdp, 0x3D, 0xFF, 0xFF, 0x00200003, PPC_NONE, PPC2_ISA205),
9970GEN_HANDLER_E(stfdpx, 0x1F, 0x17, 0x1C, 0x00200001, PPC_NONE, PPC2_ISA205),
5c55ff99
BS
9971
9972#undef GEN_CRLOGIC
9973#define GEN_CRLOGIC(name, tcg_op, opc) \
9974GEN_HANDLER(name, 0x13, 0x01, opc, 0x00000001, PPC_INTEGER)
9975GEN_CRLOGIC(crand, tcg_gen_and_i32, 0x08),
9976GEN_CRLOGIC(crandc, tcg_gen_andc_i32, 0x04),
9977GEN_CRLOGIC(creqv, tcg_gen_eqv_i32, 0x09),
9978GEN_CRLOGIC(crnand, tcg_gen_nand_i32, 0x07),
9979GEN_CRLOGIC(crnor, tcg_gen_nor_i32, 0x01),
9980GEN_CRLOGIC(cror, tcg_gen_or_i32, 0x0E),
9981GEN_CRLOGIC(crorc, tcg_gen_orc_i32, 0x0D),
9982GEN_CRLOGIC(crxor, tcg_gen_xor_i32, 0x06),
9983
9984#undef GEN_MAC_HANDLER
9985#define GEN_MAC_HANDLER(name, opc2, opc3) \
9986GEN_HANDLER(name, 0x04, opc2, opc3, 0x00000000, PPC_405_MAC)
9987GEN_MAC_HANDLER(macchw, 0x0C, 0x05),
9988GEN_MAC_HANDLER(macchwo, 0x0C, 0x15),
9989GEN_MAC_HANDLER(macchws, 0x0C, 0x07),
9990GEN_MAC_HANDLER(macchwso, 0x0C, 0x17),
9991GEN_MAC_HANDLER(macchwsu, 0x0C, 0x06),
9992GEN_MAC_HANDLER(macchwsuo, 0x0C, 0x16),
9993GEN_MAC_HANDLER(macchwu, 0x0C, 0x04),
9994GEN_MAC_HANDLER(macchwuo, 0x0C, 0x14),
9995GEN_MAC_HANDLER(machhw, 0x0C, 0x01),
9996GEN_MAC_HANDLER(machhwo, 0x0C, 0x11),
9997GEN_MAC_HANDLER(machhws, 0x0C, 0x03),
9998GEN_MAC_HANDLER(machhwso, 0x0C, 0x13),
9999GEN_MAC_HANDLER(machhwsu, 0x0C, 0x02),
10000GEN_MAC_HANDLER(machhwsuo, 0x0C, 0x12),
10001GEN_MAC_HANDLER(machhwu, 0x0C, 0x00),
10002GEN_MAC_HANDLER(machhwuo, 0x0C, 0x10),
10003GEN_MAC_HANDLER(maclhw, 0x0C, 0x0D),
10004GEN_MAC_HANDLER(maclhwo, 0x0C, 0x1D),
10005GEN_MAC_HANDLER(maclhws, 0x0C, 0x0F),
10006GEN_MAC_HANDLER(maclhwso, 0x0C, 0x1F),
10007GEN_MAC_HANDLER(maclhwu, 0x0C, 0x0C),
10008GEN_MAC_HANDLER(maclhwuo, 0x0C, 0x1C),
10009GEN_MAC_HANDLER(maclhwsu, 0x0C, 0x0E),
10010GEN_MAC_HANDLER(maclhwsuo, 0x0C, 0x1E),
10011GEN_MAC_HANDLER(nmacchw, 0x0E, 0x05),
10012GEN_MAC_HANDLER(nmacchwo, 0x0E, 0x15),
10013GEN_MAC_HANDLER(nmacchws, 0x0E, 0x07),
10014GEN_MAC_HANDLER(nmacchwso, 0x0E, 0x17),
10015GEN_MAC_HANDLER(nmachhw, 0x0E, 0x01),
10016GEN_MAC_HANDLER(nmachhwo, 0x0E, 0x11),
10017GEN_MAC_HANDLER(nmachhws, 0x0E, 0x03),
10018GEN_MAC_HANDLER(nmachhwso, 0x0E, 0x13),
10019GEN_MAC_HANDLER(nmaclhw, 0x0E, 0x0D),
10020GEN_MAC_HANDLER(nmaclhwo, 0x0E, 0x1D),
10021GEN_MAC_HANDLER(nmaclhws, 0x0E, 0x0F),
10022GEN_MAC_HANDLER(nmaclhwso, 0x0E, 0x1F),
10023GEN_MAC_HANDLER(mulchw, 0x08, 0x05),
10024GEN_MAC_HANDLER(mulchwu, 0x08, 0x04),
10025GEN_MAC_HANDLER(mulhhw, 0x08, 0x01),
10026GEN_MAC_HANDLER(mulhhwu, 0x08, 0x00),
10027GEN_MAC_HANDLER(mullhw, 0x08, 0x0D),
10028GEN_MAC_HANDLER(mullhwu, 0x08, 0x0C),
10029
10030#undef GEN_VR_LDX
10031#undef GEN_VR_STX
10032#undef GEN_VR_LVE
10033#undef GEN_VR_STVE
10034#define GEN_VR_LDX(name, opc2, opc3) \
10035GEN_HANDLER(name, 0x1F, opc2, opc3, 0x00000001, PPC_ALTIVEC)
10036#define GEN_VR_STX(name, opc2, opc3) \
10037GEN_HANDLER(st##name, 0x1F, opc2, opc3, 0x00000001, PPC_ALTIVEC)
10038#define GEN_VR_LVE(name, opc2, opc3) \
10039 GEN_HANDLER(lve##name, 0x1F, opc2, opc3, 0x00000001, PPC_ALTIVEC)
10040#define GEN_VR_STVE(name, opc2, opc3) \
10041 GEN_HANDLER(stve##name, 0x1F, opc2, opc3, 0x00000001, PPC_ALTIVEC)
10042GEN_VR_LDX(lvx, 0x07, 0x03),
10043GEN_VR_LDX(lvxl, 0x07, 0x0B),
10044GEN_VR_LVE(bx, 0x07, 0x00),
10045GEN_VR_LVE(hx, 0x07, 0x01),
10046GEN_VR_LVE(wx, 0x07, 0x02),
10047GEN_VR_STX(svx, 0x07, 0x07),
10048GEN_VR_STX(svxl, 0x07, 0x0F),
10049GEN_VR_STVE(bx, 0x07, 0x04),
10050GEN_VR_STVE(hx, 0x07, 0x05),
10051GEN_VR_STVE(wx, 0x07, 0x06),
10052
10053#undef GEN_VX_LOGICAL
10054#define GEN_VX_LOGICAL(name, tcg_op, opc2, opc3) \
10055GEN_HANDLER(name, 0x04, opc2, opc3, 0x00000000, PPC_ALTIVEC)
10056GEN_VX_LOGICAL(vand, tcg_gen_and_i64, 2, 16),
10057GEN_VX_LOGICAL(vandc, tcg_gen_andc_i64, 2, 17),
10058GEN_VX_LOGICAL(vor, tcg_gen_or_i64, 2, 18),
10059GEN_VX_LOGICAL(vxor, tcg_gen_xor_i64, 2, 19),
10060GEN_VX_LOGICAL(vnor, tcg_gen_nor_i64, 2, 20),
10061
10062#undef GEN_VXFORM
10063#define GEN_VXFORM(name, opc2, opc3) \
10064GEN_HANDLER(name, 0x04, opc2, opc3, 0x00000000, PPC_ALTIVEC)
10065GEN_VXFORM(vaddubm, 0, 0),
10066GEN_VXFORM(vadduhm, 0, 1),
10067GEN_VXFORM(vadduwm, 0, 2),
10068GEN_VXFORM(vsububm, 0, 16),
10069GEN_VXFORM(vsubuhm, 0, 17),
10070GEN_VXFORM(vsubuwm, 0, 18),
10071GEN_VXFORM(vmaxub, 1, 0),
10072GEN_VXFORM(vmaxuh, 1, 1),
10073GEN_VXFORM(vmaxuw, 1, 2),
10074GEN_VXFORM(vmaxsb, 1, 4),
10075GEN_VXFORM(vmaxsh, 1, 5),
10076GEN_VXFORM(vmaxsw, 1, 6),
10077GEN_VXFORM(vminub, 1, 8),
10078GEN_VXFORM(vminuh, 1, 9),
10079GEN_VXFORM(vminuw, 1, 10),
10080GEN_VXFORM(vminsb, 1, 12),
10081GEN_VXFORM(vminsh, 1, 13),
10082GEN_VXFORM(vminsw, 1, 14),
10083GEN_VXFORM(vavgub, 1, 16),
10084GEN_VXFORM(vavguh, 1, 17),
10085GEN_VXFORM(vavguw, 1, 18),
10086GEN_VXFORM(vavgsb, 1, 20),
10087GEN_VXFORM(vavgsh, 1, 21),
10088GEN_VXFORM(vavgsw, 1, 22),
10089GEN_VXFORM(vmrghb, 6, 0),
10090GEN_VXFORM(vmrghh, 6, 1),
10091GEN_VXFORM(vmrghw, 6, 2),
10092GEN_VXFORM(vmrglb, 6, 4),
10093GEN_VXFORM(vmrglh, 6, 5),
10094GEN_VXFORM(vmrglw, 6, 6),
10095GEN_VXFORM(vmuloub, 4, 0),
10096GEN_VXFORM(vmulouh, 4, 1),
10097GEN_VXFORM(vmulosb, 4, 4),
10098GEN_VXFORM(vmulosh, 4, 5),
10099GEN_VXFORM(vmuleub, 4, 8),
10100GEN_VXFORM(vmuleuh, 4, 9),
10101GEN_VXFORM(vmulesb, 4, 12),
10102GEN_VXFORM(vmulesh, 4, 13),
10103GEN_VXFORM(vslb, 2, 4),
10104GEN_VXFORM(vslh, 2, 5),
10105GEN_VXFORM(vslw, 2, 6),
10106GEN_VXFORM(vsrb, 2, 8),
10107GEN_VXFORM(vsrh, 2, 9),
10108GEN_VXFORM(vsrw, 2, 10),
10109GEN_VXFORM(vsrab, 2, 12),
10110GEN_VXFORM(vsrah, 2, 13),
10111GEN_VXFORM(vsraw, 2, 14),
10112GEN_VXFORM(vslo, 6, 16),
10113GEN_VXFORM(vsro, 6, 17),
10114GEN_VXFORM(vaddcuw, 0, 6),
10115GEN_VXFORM(vsubcuw, 0, 22),
10116GEN_VXFORM(vaddubs, 0, 8),
10117GEN_VXFORM(vadduhs, 0, 9),
10118GEN_VXFORM(vadduws, 0, 10),
10119GEN_VXFORM(vaddsbs, 0, 12),
10120GEN_VXFORM(vaddshs, 0, 13),
10121GEN_VXFORM(vaddsws, 0, 14),
10122GEN_VXFORM(vsububs, 0, 24),
10123GEN_VXFORM(vsubuhs, 0, 25),
10124GEN_VXFORM(vsubuws, 0, 26),
10125GEN_VXFORM(vsubsbs, 0, 28),
10126GEN_VXFORM(vsubshs, 0, 29),
10127GEN_VXFORM(vsubsws, 0, 30),
10128GEN_VXFORM(vrlb, 2, 0),
10129GEN_VXFORM(vrlh, 2, 1),
10130GEN_VXFORM(vrlw, 2, 2),
10131GEN_VXFORM(vsl, 2, 7),
10132GEN_VXFORM(vsr, 2, 11),
10133GEN_VXFORM(vpkuhum, 7, 0),
10134GEN_VXFORM(vpkuwum, 7, 1),
10135GEN_VXFORM(vpkuhus, 7, 2),
10136GEN_VXFORM(vpkuwus, 7, 3),
10137GEN_VXFORM(vpkshus, 7, 4),
10138GEN_VXFORM(vpkswus, 7, 5),
10139GEN_VXFORM(vpkshss, 7, 6),
10140GEN_VXFORM(vpkswss, 7, 7),
10141GEN_VXFORM(vpkpx, 7, 12),
10142GEN_VXFORM(vsum4ubs, 4, 24),
10143GEN_VXFORM(vsum4sbs, 4, 28),
10144GEN_VXFORM(vsum4shs, 4, 25),
10145GEN_VXFORM(vsum2sws, 4, 26),
10146GEN_VXFORM(vsumsws, 4, 30),
10147GEN_VXFORM(vaddfp, 5, 0),
10148GEN_VXFORM(vsubfp, 5, 1),
10149GEN_VXFORM(vmaxfp, 5, 16),
10150GEN_VXFORM(vminfp, 5, 17),
10151
10152#undef GEN_VXRFORM1
10153#undef GEN_VXRFORM
10154#define GEN_VXRFORM1(opname, name, str, opc2, opc3) \
10155 GEN_HANDLER2(name, str, 0x4, opc2, opc3, 0x00000000, PPC_ALTIVEC),
10156#define GEN_VXRFORM(name, opc2, opc3) \
10157 GEN_VXRFORM1(name, name, #name, opc2, opc3) \
10158 GEN_VXRFORM1(name##_dot, name##_, #name ".", opc2, (opc3 | (0x1 << 4)))
10159GEN_VXRFORM(vcmpequb, 3, 0)
10160GEN_VXRFORM(vcmpequh, 3, 1)
10161GEN_VXRFORM(vcmpequw, 3, 2)
10162GEN_VXRFORM(vcmpgtsb, 3, 12)
10163GEN_VXRFORM(vcmpgtsh, 3, 13)
10164GEN_VXRFORM(vcmpgtsw, 3, 14)
10165GEN_VXRFORM(vcmpgtub, 3, 8)
10166GEN_VXRFORM(vcmpgtuh, 3, 9)
10167GEN_VXRFORM(vcmpgtuw, 3, 10)
10168GEN_VXRFORM(vcmpeqfp, 3, 3)
10169GEN_VXRFORM(vcmpgefp, 3, 7)
10170GEN_VXRFORM(vcmpgtfp, 3, 11)
10171GEN_VXRFORM(vcmpbfp, 3, 15)
10172
10173#undef GEN_VXFORM_SIMM
10174#define GEN_VXFORM_SIMM(name, opc2, opc3) \
10175 GEN_HANDLER(name, 0x04, opc2, opc3, 0x00000000, PPC_ALTIVEC)
10176GEN_VXFORM_SIMM(vspltisb, 6, 12),
10177GEN_VXFORM_SIMM(vspltish, 6, 13),
10178GEN_VXFORM_SIMM(vspltisw, 6, 14),
10179
10180#undef GEN_VXFORM_NOA
10181#define GEN_VXFORM_NOA(name, opc2, opc3) \
10182 GEN_HANDLER(name, 0x04, opc2, opc3, 0x001f0000, PPC_ALTIVEC)
10183GEN_VXFORM_NOA(vupkhsb, 7, 8),
10184GEN_VXFORM_NOA(vupkhsh, 7, 9),
10185GEN_VXFORM_NOA(vupklsb, 7, 10),
10186GEN_VXFORM_NOA(vupklsh, 7, 11),
10187GEN_VXFORM_NOA(vupkhpx, 7, 13),
10188GEN_VXFORM_NOA(vupklpx, 7, 15),
10189GEN_VXFORM_NOA(vrefp, 5, 4),
10190GEN_VXFORM_NOA(vrsqrtefp, 5, 5),
0bffbc6c 10191GEN_VXFORM_NOA(vexptefp, 5, 6),
5c55ff99
BS
10192GEN_VXFORM_NOA(vlogefp, 5, 7),
10193GEN_VXFORM_NOA(vrfim, 5, 8),
10194GEN_VXFORM_NOA(vrfin, 5, 9),
10195GEN_VXFORM_NOA(vrfip, 5, 10),
10196GEN_VXFORM_NOA(vrfiz, 5, 11),
10197
10198#undef GEN_VXFORM_UIMM
10199#define GEN_VXFORM_UIMM(name, opc2, opc3) \
10200 GEN_HANDLER(name, 0x04, opc2, opc3, 0x00000000, PPC_ALTIVEC)
10201GEN_VXFORM_UIMM(vspltb, 6, 8),
10202GEN_VXFORM_UIMM(vsplth, 6, 9),
10203GEN_VXFORM_UIMM(vspltw, 6, 10),
10204GEN_VXFORM_UIMM(vcfux, 5, 12),
10205GEN_VXFORM_UIMM(vcfsx, 5, 13),
10206GEN_VXFORM_UIMM(vctuxs, 5, 14),
10207GEN_VXFORM_UIMM(vctsxs, 5, 15),
10208
10209#undef GEN_VAFORM_PAIRED
10210#define GEN_VAFORM_PAIRED(name0, name1, opc2) \
10211 GEN_HANDLER(name0##_##name1, 0x04, opc2, 0xFF, 0x00000000, PPC_ALTIVEC)
10212GEN_VAFORM_PAIRED(vmhaddshs, vmhraddshs, 16),
10213GEN_VAFORM_PAIRED(vmsumubm, vmsummbm, 18),
10214GEN_VAFORM_PAIRED(vmsumuhm, vmsumuhs, 19),
10215GEN_VAFORM_PAIRED(vmsumshm, vmsumshs, 20),
10216GEN_VAFORM_PAIRED(vsel, vperm, 21),
10217GEN_VAFORM_PAIRED(vmaddfp, vnmsubfp, 23),
10218
fa1832d7 10219GEN_HANDLER_E(lxsdx, 0x1F, 0x0C, 0x12, 0, PPC_NONE, PPC2_VSX),
cac7f0ba
TM
10220GEN_HANDLER_E(lxsiwax, 0x1F, 0x0C, 0x02, 0, PPC_NONE, PPC2_VSX207),
10221GEN_HANDLER_E(lxsiwzx, 0x1F, 0x0C, 0x00, 0, PPC_NONE, PPC2_VSX207),
10222GEN_HANDLER_E(lxsspx, 0x1F, 0x0C, 0x10, 0, PPC_NONE, PPC2_VSX207),
304af367 10223GEN_HANDLER_E(lxvd2x, 0x1F, 0x0C, 0x1A, 0, PPC_NONE, PPC2_VSX),
ca03b467 10224GEN_HANDLER_E(lxvdsx, 0x1F, 0x0C, 0x0A, 0, PPC_NONE, PPC2_VSX),
897e61d1 10225GEN_HANDLER_E(lxvw4x, 0x1F, 0x0C, 0x18, 0, PPC_NONE, PPC2_VSX),
304af367 10226
9231ba9e 10227GEN_HANDLER_E(stxsdx, 0x1F, 0xC, 0x16, 0, PPC_NONE, PPC2_VSX),
e16a626b
TM
10228GEN_HANDLER_E(stxsiwx, 0x1F, 0xC, 0x04, 0, PPC_NONE, PPC2_VSX207),
10229GEN_HANDLER_E(stxsspx, 0x1F, 0xC, 0x14, 0, PPC_NONE, PPC2_VSX207),
fbed2478 10230GEN_HANDLER_E(stxvd2x, 0x1F, 0xC, 0x1E, 0, PPC_NONE, PPC2_VSX),
86e61ce3 10231GEN_HANDLER_E(stxvw4x, 0x1F, 0xC, 0x1C, 0, PPC_NONE, PPC2_VSX),
fbed2478 10232
f5c0f7f9
TM
10233GEN_HANDLER_E(mfvsrwz, 0x1F, 0x13, 0x03, 0x0000F800, PPC_NONE, PPC2_VSX207),
10234GEN_HANDLER_E(mtvsrwa, 0x1F, 0x13, 0x06, 0x0000F800, PPC_NONE, PPC2_VSX207),
10235GEN_HANDLER_E(mtvsrwz, 0x1F, 0x13, 0x07, 0x0000F800, PPC_NONE, PPC2_VSX207),
10236#if defined(TARGET_PPC64)
10237GEN_HANDLER_E(mfvsrd, 0x1F, 0x13, 0x01, 0x0000F800, PPC_NONE, PPC2_VSX207),
10238GEN_HANDLER_E(mtvsrd, 0x1F, 0x13, 0x05, 0x0000F800, PPC_NONE, PPC2_VSX207),
10239#endif
10240
df020ce0
TM
10241#undef GEN_XX2FORM
10242#define GEN_XX2FORM(name, opc2, opc3, fl2) \
10243GEN_HANDLER2_E(name, #name, 0x3C, opc2 | 0, opc3, 0, PPC_NONE, fl2), \
10244GEN_HANDLER2_E(name, #name, 0x3C, opc2 | 1, opc3, 0, PPC_NONE, fl2)
10245
10246#undef GEN_XX3FORM
10247#define GEN_XX3FORM(name, opc2, opc3, fl2) \
10248GEN_HANDLER2_E(name, #name, 0x3C, opc2 | 0, opc3, 0, PPC_NONE, fl2), \
10249GEN_HANDLER2_E(name, #name, 0x3C, opc2 | 1, opc3, 0, PPC_NONE, fl2), \
10250GEN_HANDLER2_E(name, #name, 0x3C, opc2 | 2, opc3, 0, PPC_NONE, fl2), \
10251GEN_HANDLER2_E(name, #name, 0x3C, opc2 | 3, opc3, 0, PPC_NONE, fl2)
10252
354a6dec
TM
10253#undef GEN_XX3_RC_FORM
10254#define GEN_XX3_RC_FORM(name, opc2, opc3, fl2) \
10255GEN_HANDLER2_E(name, #name, 0x3C, opc2 | 0x00, opc3 | 0x00, 0, PPC_NONE, fl2), \
10256GEN_HANDLER2_E(name, #name, 0x3C, opc2 | 0x01, opc3 | 0x00, 0, PPC_NONE, fl2), \
10257GEN_HANDLER2_E(name, #name, 0x3C, opc2 | 0x02, opc3 | 0x00, 0, PPC_NONE, fl2), \
10258GEN_HANDLER2_E(name, #name, 0x3C, opc2 | 0x03, opc3 | 0x00, 0, PPC_NONE, fl2), \
10259GEN_HANDLER2_E(name, #name, 0x3C, opc2 | 0x00, opc3 | 0x10, 0, PPC_NONE, fl2), \
10260GEN_HANDLER2_E(name, #name, 0x3C, opc2 | 0x01, opc3 | 0x10, 0, PPC_NONE, fl2), \
10261GEN_HANDLER2_E(name, #name, 0x3C, opc2 | 0x02, opc3 | 0x10, 0, PPC_NONE, fl2), \
10262GEN_HANDLER2_E(name, #name, 0x3C, opc2 | 0x03, opc3 | 0x10, 0, PPC_NONE, fl2)
10263
cd73f2c9
TM
10264#undef GEN_XX3FORM_DM
10265#define GEN_XX3FORM_DM(name, opc2, opc3) \
10266GEN_HANDLER2_E(name, #name, 0x3C, opc2|0x00, opc3|0x00, 0, PPC_NONE, PPC2_VSX),\
10267GEN_HANDLER2_E(name, #name, 0x3C, opc2|0x01, opc3|0x00, 0, PPC_NONE, PPC2_VSX),\
10268GEN_HANDLER2_E(name, #name, 0x3C, opc2|0x02, opc3|0x00, 0, PPC_NONE, PPC2_VSX),\
10269GEN_HANDLER2_E(name, #name, 0x3C, opc2|0x03, opc3|0x00, 0, PPC_NONE, PPC2_VSX),\
10270GEN_HANDLER2_E(name, #name, 0x3C, opc2|0x00, opc3|0x04, 0, PPC_NONE, PPC2_VSX),\
10271GEN_HANDLER2_E(name, #name, 0x3C, opc2|0x01, opc3|0x04, 0, PPC_NONE, PPC2_VSX),\
10272GEN_HANDLER2_E(name, #name, 0x3C, opc2|0x02, opc3|0x04, 0, PPC_NONE, PPC2_VSX),\
10273GEN_HANDLER2_E(name, #name, 0x3C, opc2|0x03, opc3|0x04, 0, PPC_NONE, PPC2_VSX),\
10274GEN_HANDLER2_E(name, #name, 0x3C, opc2|0x00, opc3|0x08, 0, PPC_NONE, PPC2_VSX),\
10275GEN_HANDLER2_E(name, #name, 0x3C, opc2|0x01, opc3|0x08, 0, PPC_NONE, PPC2_VSX),\
10276GEN_HANDLER2_E(name, #name, 0x3C, opc2|0x02, opc3|0x08, 0, PPC_NONE, PPC2_VSX),\
10277GEN_HANDLER2_E(name, #name, 0x3C, opc2|0x03, opc3|0x08, 0, PPC_NONE, PPC2_VSX),\
10278GEN_HANDLER2_E(name, #name, 0x3C, opc2|0x00, opc3|0x0C, 0, PPC_NONE, PPC2_VSX),\
10279GEN_HANDLER2_E(name, #name, 0x3C, opc2|0x01, opc3|0x0C, 0, PPC_NONE, PPC2_VSX),\
10280GEN_HANDLER2_E(name, #name, 0x3C, opc2|0x02, opc3|0x0C, 0, PPC_NONE, PPC2_VSX),\
10281GEN_HANDLER2_E(name, #name, 0x3C, opc2|0x03, opc3|0x0C, 0, PPC_NONE, PPC2_VSX)
10282
df020ce0
TM
10283GEN_XX2FORM(xsabsdp, 0x12, 0x15, PPC2_VSX),
10284GEN_XX2FORM(xsnabsdp, 0x12, 0x16, PPC2_VSX),
10285GEN_XX2FORM(xsnegdp, 0x12, 0x17, PPC2_VSX),
10286GEN_XX3FORM(xscpsgndp, 0x00, 0x16, PPC2_VSX),
10287
be574920
TM
10288GEN_XX2FORM(xvabsdp, 0x12, 0x1D, PPC2_VSX),
10289GEN_XX2FORM(xvnabsdp, 0x12, 0x1E, PPC2_VSX),
10290GEN_XX2FORM(xvnegdp, 0x12, 0x1F, PPC2_VSX),
10291GEN_XX3FORM(xvcpsgndp, 0x00, 0x1E, PPC2_VSX),
10292GEN_XX2FORM(xvabssp, 0x12, 0x19, PPC2_VSX),
10293GEN_XX2FORM(xvnabssp, 0x12, 0x1A, PPC2_VSX),
10294GEN_XX2FORM(xvnegsp, 0x12, 0x1B, PPC2_VSX),
10295GEN_XX3FORM(xvcpsgnsp, 0x00, 0x1A, PPC2_VSX),
79ca8a6a 10296
ee6e02c0
TM
10297GEN_XX3FORM(xsadddp, 0x00, 0x04, PPC2_VSX),
10298GEN_XX3FORM(xssubdp, 0x00, 0x05, PPC2_VSX),
5e591d88 10299GEN_XX3FORM(xsmuldp, 0x00, 0x06, PPC2_VSX),
4b98eeef 10300GEN_XX3FORM(xsdivdp, 0x00, 0x07, PPC2_VSX),
2009227f 10301GEN_XX2FORM(xsredp, 0x14, 0x05, PPC2_VSX),
d32404fe 10302GEN_XX2FORM(xssqrtdp, 0x16, 0x04, PPC2_VSX),
d3f9df8f 10303GEN_XX2FORM(xsrsqrtedp, 0x14, 0x04, PPC2_VSX),
bc80838f 10304GEN_XX3FORM(xstdivdp, 0x14, 0x07, PPC2_VSX),
5cb151ac 10305GEN_XX2FORM(xstsqrtdp, 0x14, 0x06, PPC2_VSX),
595c6eef
TM
10306GEN_XX3FORM(xsmaddadp, 0x04, 0x04, PPC2_VSX),
10307GEN_XX3FORM(xsmaddmdp, 0x04, 0x05, PPC2_VSX),
10308GEN_XX3FORM(xsmsubadp, 0x04, 0x06, PPC2_VSX),
10309GEN_XX3FORM(xsmsubmdp, 0x04, 0x07, PPC2_VSX),
10310GEN_XX3FORM(xsnmaddadp, 0x04, 0x14, PPC2_VSX),
10311GEN_XX3FORM(xsnmaddmdp, 0x04, 0x15, PPC2_VSX),
10312GEN_XX3FORM(xsnmsubadp, 0x04, 0x16, PPC2_VSX),
10313GEN_XX3FORM(xsnmsubmdp, 0x04, 0x17, PPC2_VSX),
4f17e9c7
TM
10314GEN_XX2FORM(xscmpodp, 0x0C, 0x05, PPC2_VSX),
10315GEN_XX2FORM(xscmpudp, 0x0C, 0x04, PPC2_VSX),
959e9c9d
TM
10316GEN_XX3FORM(xsmaxdp, 0x00, 0x14, PPC2_VSX),
10317GEN_XX3FORM(xsmindp, 0x00, 0x15, PPC2_VSX),
ed8ac568 10318GEN_XX2FORM(xscvdpsp, 0x12, 0x10, PPC2_VSX),
7ee19fb9 10319GEN_XX2FORM(xscvdpspn, 0x16, 0x10, PPC2_VSX207),
ed8ac568 10320GEN_XX2FORM(xscvspdp, 0x12, 0x14, PPC2_VSX),
7ee19fb9 10321GEN_XX2FORM(xscvspdpn, 0x16, 0x14, PPC2_VSX207),
5177d2ca
TM
10322GEN_XX2FORM(xscvdpsxds, 0x10, 0x15, PPC2_VSX),
10323GEN_XX2FORM(xscvdpsxws, 0x10, 0x05, PPC2_VSX),
10324GEN_XX2FORM(xscvdpuxds, 0x10, 0x14, PPC2_VSX),
10325GEN_XX2FORM(xscvdpuxws, 0x10, 0x04, PPC2_VSX),
10326GEN_XX2FORM(xscvsxddp, 0x10, 0x17, PPC2_VSX),
10327GEN_XX2FORM(xscvuxddp, 0x10, 0x16, PPC2_VSX),
88e33d08
TM
10328GEN_XX2FORM(xsrdpi, 0x12, 0x04, PPC2_VSX),
10329GEN_XX2FORM(xsrdpic, 0x16, 0x06, PPC2_VSX),
10330GEN_XX2FORM(xsrdpim, 0x12, 0x07, PPC2_VSX),
10331GEN_XX2FORM(xsrdpip, 0x12, 0x06, PPC2_VSX),
10332GEN_XX2FORM(xsrdpiz, 0x12, 0x05, PPC2_VSX),
ee6e02c0 10333
3fd0aadf
TM
10334GEN_XX3FORM(xsaddsp, 0x00, 0x00, PPC2_VSX207),
10335GEN_XX3FORM(xssubsp, 0x00, 0x01, PPC2_VSX207),
ab9408a2 10336GEN_XX3FORM(xsmulsp, 0x00, 0x02, PPC2_VSX207),
b24d0b47 10337GEN_XX3FORM(xsdivsp, 0x00, 0x03, PPC2_VSX207),
2c0c52ae 10338GEN_XX2FORM(xsresp, 0x14, 0x01, PPC2_VSX207),
3d1140bf 10339GEN_XX2FORM(xsrsp, 0x12, 0x11, PPC2_VSX207),
cea4e574 10340GEN_XX2FORM(xssqrtsp, 0x16, 0x00, PPC2_VSX207),
968e76bc 10341GEN_XX2FORM(xsrsqrtesp, 0x14, 0x00, PPC2_VSX207),
f53f81e0
TM
10342GEN_XX3FORM(xsmaddasp, 0x04, 0x00, PPC2_VSX207),
10343GEN_XX3FORM(xsmaddmsp, 0x04, 0x01, PPC2_VSX207),
10344GEN_XX3FORM(xsmsubasp, 0x04, 0x02, PPC2_VSX207),
10345GEN_XX3FORM(xsmsubmsp, 0x04, 0x03, PPC2_VSX207),
10346GEN_XX3FORM(xsnmaddasp, 0x04, 0x10, PPC2_VSX207),
10347GEN_XX3FORM(xsnmaddmsp, 0x04, 0x11, PPC2_VSX207),
10348GEN_XX3FORM(xsnmsubasp, 0x04, 0x12, PPC2_VSX207),
10349GEN_XX3FORM(xsnmsubmsp, 0x04, 0x13, PPC2_VSX207),
74698350
TM
10350GEN_XX2FORM(xscvsxdsp, 0x10, 0x13, PPC2_VSX207),
10351GEN_XX2FORM(xscvuxdsp, 0x10, 0x12, PPC2_VSX207),
3fd0aadf 10352
ee6e02c0
TM
10353GEN_XX3FORM(xvadddp, 0x00, 0x0C, PPC2_VSX),
10354GEN_XX3FORM(xvsubdp, 0x00, 0x0D, PPC2_VSX),
5e591d88 10355GEN_XX3FORM(xvmuldp, 0x00, 0x0E, PPC2_VSX),
4b98eeef 10356GEN_XX3FORM(xvdivdp, 0x00, 0x0F, PPC2_VSX),
2009227f 10357GEN_XX2FORM(xvredp, 0x14, 0x0D, PPC2_VSX),
d32404fe 10358GEN_XX2FORM(xvsqrtdp, 0x16, 0x0C, PPC2_VSX),
d3f9df8f 10359GEN_XX2FORM(xvrsqrtedp, 0x14, 0x0C, PPC2_VSX),
bc80838f 10360GEN_XX3FORM(xvtdivdp, 0x14, 0x0F, PPC2_VSX),
5cb151ac 10361GEN_XX2FORM(xvtsqrtdp, 0x14, 0x0E, PPC2_VSX),
595c6eef
TM
10362GEN_XX3FORM(xvmaddadp, 0x04, 0x0C, PPC2_VSX),
10363GEN_XX3FORM(xvmaddmdp, 0x04, 0x0D, PPC2_VSX),
10364GEN_XX3FORM(xvmsubadp, 0x04, 0x0E, PPC2_VSX),
10365GEN_XX3FORM(xvmsubmdp, 0x04, 0x0F, PPC2_VSX),
10366GEN_XX3FORM(xvnmaddadp, 0x04, 0x1C, PPC2_VSX),
10367GEN_XX3FORM(xvnmaddmdp, 0x04, 0x1D, PPC2_VSX),
10368GEN_XX3FORM(xvnmsubadp, 0x04, 0x1E, PPC2_VSX),
10369GEN_XX3FORM(xvnmsubmdp, 0x04, 0x1F, PPC2_VSX),
959e9c9d
TM
10370GEN_XX3FORM(xvmaxdp, 0x00, 0x1C, PPC2_VSX),
10371GEN_XX3FORM(xvmindp, 0x00, 0x1D, PPC2_VSX),
354a6dec
TM
10372GEN_XX3_RC_FORM(xvcmpeqdp, 0x0C, 0x0C, PPC2_VSX),
10373GEN_XX3_RC_FORM(xvcmpgtdp, 0x0C, 0x0D, PPC2_VSX),
10374GEN_XX3_RC_FORM(xvcmpgedp, 0x0C, 0x0E, PPC2_VSX),
ed8ac568 10375GEN_XX2FORM(xvcvdpsp, 0x12, 0x18, PPC2_VSX),
5177d2ca
TM
10376GEN_XX2FORM(xvcvdpsxds, 0x10, 0x1D, PPC2_VSX),
10377GEN_XX2FORM(xvcvdpsxws, 0x10, 0x0D, PPC2_VSX),
10378GEN_XX2FORM(xvcvdpuxds, 0x10, 0x1C, PPC2_VSX),
10379GEN_XX2FORM(xvcvdpuxws, 0x10, 0x0C, PPC2_VSX),
10380GEN_XX2FORM(xvcvsxddp, 0x10, 0x1F, PPC2_VSX),
10381GEN_XX2FORM(xvcvuxddp, 0x10, 0x1E, PPC2_VSX),
10382GEN_XX2FORM(xvcvsxwdp, 0x10, 0x0F, PPC2_VSX),
10383GEN_XX2FORM(xvcvuxwdp, 0x10, 0x0E, PPC2_VSX),
88e33d08
TM
10384GEN_XX2FORM(xvrdpi, 0x12, 0x0C, PPC2_VSX),
10385GEN_XX2FORM(xvrdpic, 0x16, 0x0E, PPC2_VSX),
10386GEN_XX2FORM(xvrdpim, 0x12, 0x0F, PPC2_VSX),
10387GEN_XX2FORM(xvrdpip, 0x12, 0x0E, PPC2_VSX),
10388GEN_XX2FORM(xvrdpiz, 0x12, 0x0D, PPC2_VSX),
ee6e02c0
TM
10389
10390GEN_XX3FORM(xvaddsp, 0x00, 0x08, PPC2_VSX),
10391GEN_XX3FORM(xvsubsp, 0x00, 0x09, PPC2_VSX),
5e591d88 10392GEN_XX3FORM(xvmulsp, 0x00, 0x0A, PPC2_VSX),
4b98eeef 10393GEN_XX3FORM(xvdivsp, 0x00, 0x0B, PPC2_VSX),
2009227f 10394GEN_XX2FORM(xvresp, 0x14, 0x09, PPC2_VSX),
d32404fe 10395GEN_XX2FORM(xvsqrtsp, 0x16, 0x08, PPC2_VSX),
d3f9df8f 10396GEN_XX2FORM(xvrsqrtesp, 0x14, 0x08, PPC2_VSX),
bc80838f 10397GEN_XX3FORM(xvtdivsp, 0x14, 0x0B, PPC2_VSX),
5cb151ac 10398GEN_XX2FORM(xvtsqrtsp, 0x14, 0x0A, PPC2_VSX),
595c6eef
TM
10399GEN_XX3FORM(xvmaddasp, 0x04, 0x08, PPC2_VSX),
10400GEN_XX3FORM(xvmaddmsp, 0x04, 0x09, PPC2_VSX),
10401GEN_XX3FORM(xvmsubasp, 0x04, 0x0A, PPC2_VSX),
10402GEN_XX3FORM(xvmsubmsp, 0x04, 0x0B, PPC2_VSX),
10403GEN_XX3FORM(xvnmaddasp, 0x04, 0x18, PPC2_VSX),
10404GEN_XX3FORM(xvnmaddmsp, 0x04, 0x19, PPC2_VSX),
10405GEN_XX3FORM(xvnmsubasp, 0x04, 0x1A, PPC2_VSX),
10406GEN_XX3FORM(xvnmsubmsp, 0x04, 0x1B, PPC2_VSX),
959e9c9d
TM
10407GEN_XX3FORM(xvmaxsp, 0x00, 0x18, PPC2_VSX),
10408GEN_XX3FORM(xvminsp, 0x00, 0x19, PPC2_VSX),
354a6dec
TM
10409GEN_XX3_RC_FORM(xvcmpeqsp, 0x0C, 0x08, PPC2_VSX),
10410GEN_XX3_RC_FORM(xvcmpgtsp, 0x0C, 0x09, PPC2_VSX),
10411GEN_XX3_RC_FORM(xvcmpgesp, 0x0C, 0x0A, PPC2_VSX),
ed8ac568 10412GEN_XX2FORM(xvcvspdp, 0x12, 0x1C, PPC2_VSX),
5177d2ca
TM
10413GEN_XX2FORM(xvcvspsxds, 0x10, 0x19, PPC2_VSX),
10414GEN_XX2FORM(xvcvspsxws, 0x10, 0x09, PPC2_VSX),
10415GEN_XX2FORM(xvcvspuxds, 0x10, 0x18, PPC2_VSX),
10416GEN_XX2FORM(xvcvspuxws, 0x10, 0x08, PPC2_VSX),
10417GEN_XX2FORM(xvcvsxdsp, 0x10, 0x1B, PPC2_VSX),
10418GEN_XX2FORM(xvcvuxdsp, 0x10, 0x1A, PPC2_VSX),
10419GEN_XX2FORM(xvcvsxwsp, 0x10, 0x0B, PPC2_VSX),
10420GEN_XX2FORM(xvcvuxwsp, 0x10, 0x0A, PPC2_VSX),
88e33d08
TM
10421GEN_XX2FORM(xvrspi, 0x12, 0x08, PPC2_VSX),
10422GEN_XX2FORM(xvrspic, 0x16, 0x0A, PPC2_VSX),
10423GEN_XX2FORM(xvrspim, 0x12, 0x0B, PPC2_VSX),
10424GEN_XX2FORM(xvrspip, 0x12, 0x0A, PPC2_VSX),
10425GEN_XX2FORM(xvrspiz, 0x12, 0x09, PPC2_VSX),
ee6e02c0 10426
79ca8a6a
TM
10427#undef VSX_LOGICAL
10428#define VSX_LOGICAL(name, opc2, opc3, fl2) \
10429GEN_XX3FORM(name, opc2, opc3, fl2)
10430
10431VSX_LOGICAL(xxland, 0x8, 0x10, PPC2_VSX),
10432VSX_LOGICAL(xxlandc, 0x8, 0x11, PPC2_VSX),
10433VSX_LOGICAL(xxlor, 0x8, 0x12, PPC2_VSX),
10434VSX_LOGICAL(xxlxor, 0x8, 0x13, PPC2_VSX),
10435VSX_LOGICAL(xxlnor, 0x8, 0x14, PPC2_VSX),
67a33f37
TM
10436VSX_LOGICAL(xxleqv, 0x8, 0x17, PPC2_VSX207),
10437VSX_LOGICAL(xxlnand, 0x8, 0x16, PPC2_VSX207),
10438VSX_LOGICAL(xxlorc, 0x8, 0x15, PPC2_VSX207),
ce577d2e
TM
10439GEN_XX3FORM(xxmrghw, 0x08, 0x02, PPC2_VSX),
10440GEN_XX3FORM(xxmrglw, 0x08, 0x06, PPC2_VSX),
76c15fe0 10441GEN_XX2FORM(xxspltw, 0x08, 0x0A, PPC2_VSX),
acc42968 10442GEN_XX3FORM_DM(xxsldwi, 0x08, 0x00),
79ca8a6a 10443
551e3ef7
TM
10444#define GEN_XXSEL_ROW(opc3) \
10445GEN_HANDLER2_E(xxsel, "xxsel", 0x3C, 0x18, opc3, 0, PPC_NONE, PPC2_VSX), \
10446GEN_HANDLER2_E(xxsel, "xxsel", 0x3C, 0x19, opc3, 0, PPC_NONE, PPC2_VSX), \
10447GEN_HANDLER2_E(xxsel, "xxsel", 0x3C, 0x1A, opc3, 0, PPC_NONE, PPC2_VSX), \
10448GEN_HANDLER2_E(xxsel, "xxsel", 0x3C, 0x1B, opc3, 0, PPC_NONE, PPC2_VSX), \
10449GEN_HANDLER2_E(xxsel, "xxsel", 0x3C, 0x1C, opc3, 0, PPC_NONE, PPC2_VSX), \
10450GEN_HANDLER2_E(xxsel, "xxsel", 0x3C, 0x1D, opc3, 0, PPC_NONE, PPC2_VSX), \
10451GEN_HANDLER2_E(xxsel, "xxsel", 0x3C, 0x1E, opc3, 0, PPC_NONE, PPC2_VSX), \
10452GEN_HANDLER2_E(xxsel, "xxsel", 0x3C, 0x1F, opc3, 0, PPC_NONE, PPC2_VSX), \
10453
10454GEN_XXSEL_ROW(0x00)
10455GEN_XXSEL_ROW(0x01)
10456GEN_XXSEL_ROW(0x02)
10457GEN_XXSEL_ROW(0x03)
10458GEN_XXSEL_ROW(0x04)
10459GEN_XXSEL_ROW(0x05)
10460GEN_XXSEL_ROW(0x06)
10461GEN_XXSEL_ROW(0x07)
10462GEN_XXSEL_ROW(0x08)
10463GEN_XXSEL_ROW(0x09)
10464GEN_XXSEL_ROW(0x0A)
10465GEN_XXSEL_ROW(0x0B)
10466GEN_XXSEL_ROW(0x0C)
10467GEN_XXSEL_ROW(0x0D)
10468GEN_XXSEL_ROW(0x0E)
10469GEN_XXSEL_ROW(0x0F)
10470GEN_XXSEL_ROW(0x10)
10471GEN_XXSEL_ROW(0x11)
10472GEN_XXSEL_ROW(0x12)
10473GEN_XXSEL_ROW(0x13)
10474GEN_XXSEL_ROW(0x14)
10475GEN_XXSEL_ROW(0x15)
10476GEN_XXSEL_ROW(0x16)
10477GEN_XXSEL_ROW(0x17)
10478GEN_XXSEL_ROW(0x18)
10479GEN_XXSEL_ROW(0x19)
10480GEN_XXSEL_ROW(0x1A)
10481GEN_XXSEL_ROW(0x1B)
10482GEN_XXSEL_ROW(0x1C)
10483GEN_XXSEL_ROW(0x1D)
10484GEN_XXSEL_ROW(0x1E)
10485GEN_XXSEL_ROW(0x1F)
10486
cd73f2c9
TM
10487GEN_XX3FORM_DM(xxpermdi, 0x08, 0x01),
10488
5c55ff99 10489#undef GEN_SPE
70560da7
FC
10490#define GEN_SPE(name0, name1, opc2, opc3, inval0, inval1, type) \
10491 GEN_OPCODE_DUAL(name0##_##name1, 0x04, opc2, opc3, inval0, inval1, type, PPC_NONE)
10492GEN_SPE(evaddw, speundef, 0x00, 0x08, 0x00000000, 0xFFFFFFFF, PPC_SPE),
10493GEN_SPE(evaddiw, speundef, 0x01, 0x08, 0x00000000, 0xFFFFFFFF, PPC_SPE),
10494GEN_SPE(evsubfw, speundef, 0x02, 0x08, 0x00000000, 0xFFFFFFFF, PPC_SPE),
10495GEN_SPE(evsubifw, speundef, 0x03, 0x08, 0x00000000, 0xFFFFFFFF, PPC_SPE),
10496GEN_SPE(evabs, evneg, 0x04, 0x08, 0x0000F800, 0x0000F800, PPC_SPE),
10497GEN_SPE(evextsb, evextsh, 0x05, 0x08, 0x0000F800, 0x0000F800, PPC_SPE),
10498GEN_SPE(evrndw, evcntlzw, 0x06, 0x08, 0x0000F800, 0x0000F800, PPC_SPE),
10499GEN_SPE(evcntlsw, brinc, 0x07, 0x08, 0x0000F800, 0x00000000, PPC_SPE),
10500GEN_SPE(evmra, speundef, 0x02, 0x13, 0x0000F800, 0xFFFFFFFF, PPC_SPE),
10501GEN_SPE(speundef, evand, 0x08, 0x08, 0xFFFFFFFF, 0x00000000, PPC_SPE),
10502GEN_SPE(evandc, speundef, 0x09, 0x08, 0x00000000, 0xFFFFFFFF, PPC_SPE),
10503GEN_SPE(evxor, evor, 0x0B, 0x08, 0x00000000, 0x00000000, PPC_SPE),
10504GEN_SPE(evnor, eveqv, 0x0C, 0x08, 0x00000000, 0x00000000, PPC_SPE),
10505GEN_SPE(evmwumi, evmwsmi, 0x0C, 0x11, 0x00000000, 0x00000000, PPC_SPE),
10506GEN_SPE(evmwumia, evmwsmia, 0x1C, 0x11, 0x00000000, 0x00000000, PPC_SPE),
10507GEN_SPE(evmwumiaa, evmwsmiaa, 0x0C, 0x15, 0x00000000, 0x00000000, PPC_SPE),
10508GEN_SPE(speundef, evorc, 0x0D, 0x08, 0xFFFFFFFF, 0x00000000, PPC_SPE),
10509GEN_SPE(evnand, speundef, 0x0F, 0x08, 0x00000000, 0xFFFFFFFF, PPC_SPE),
10510GEN_SPE(evsrwu, evsrws, 0x10, 0x08, 0x00000000, 0x00000000, PPC_SPE),
10511GEN_SPE(evsrwiu, evsrwis, 0x11, 0x08, 0x00000000, 0x00000000, PPC_SPE),
10512GEN_SPE(evslw, speundef, 0x12, 0x08, 0x00000000, 0xFFFFFFFF, PPC_SPE),
10513GEN_SPE(evslwi, speundef, 0x13, 0x08, 0x00000000, 0xFFFFFFFF, PPC_SPE),
10514GEN_SPE(evrlw, evsplati, 0x14, 0x08, 0x00000000, 0x0000F800, PPC_SPE),
10515GEN_SPE(evrlwi, evsplatfi, 0x15, 0x08, 0x00000000, 0x0000F800, PPC_SPE),
10516GEN_SPE(evmergehi, evmergelo, 0x16, 0x08, 0x00000000, 0x00000000, PPC_SPE),
10517GEN_SPE(evmergehilo, evmergelohi, 0x17, 0x08, 0x00000000, 0x00000000, PPC_SPE),
10518GEN_SPE(evcmpgtu, evcmpgts, 0x18, 0x08, 0x00600000, 0x00600000, PPC_SPE),
10519GEN_SPE(evcmpltu, evcmplts, 0x19, 0x08, 0x00600000, 0x00600000, PPC_SPE),
10520GEN_SPE(evcmpeq, speundef, 0x1A, 0x08, 0x00600000, 0xFFFFFFFF, PPC_SPE),
10521
10522GEN_SPE(evfsadd, evfssub, 0x00, 0x0A, 0x00000000, 0x00000000, PPC_SPE_SINGLE),
10523GEN_SPE(evfsabs, evfsnabs, 0x02, 0x0A, 0x0000F800, 0x0000F800, PPC_SPE_SINGLE),
10524GEN_SPE(evfsneg, speundef, 0x03, 0x0A, 0x0000F800, 0xFFFFFFFF, PPC_SPE_SINGLE),
10525GEN_SPE(evfsmul, evfsdiv, 0x04, 0x0A, 0x00000000, 0x00000000, PPC_SPE_SINGLE),
10526GEN_SPE(evfscmpgt, evfscmplt, 0x06, 0x0A, 0x00600000, 0x00600000, PPC_SPE_SINGLE),
10527GEN_SPE(evfscmpeq, speundef, 0x07, 0x0A, 0x00600000, 0xFFFFFFFF, PPC_SPE_SINGLE),
10528GEN_SPE(evfscfui, evfscfsi, 0x08, 0x0A, 0x00180000, 0x00180000, PPC_SPE_SINGLE),
10529GEN_SPE(evfscfuf, evfscfsf, 0x09, 0x0A, 0x00180000, 0x00180000, PPC_SPE_SINGLE),
10530GEN_SPE(evfsctui, evfsctsi, 0x0A, 0x0A, 0x00180000, 0x00180000, PPC_SPE_SINGLE),
10531GEN_SPE(evfsctuf, evfsctsf, 0x0B, 0x0A, 0x00180000, 0x00180000, PPC_SPE_SINGLE),
10532GEN_SPE(evfsctuiz, speundef, 0x0C, 0x0A, 0x00180000, 0xFFFFFFFF, PPC_SPE_SINGLE),
10533GEN_SPE(evfsctsiz, speundef, 0x0D, 0x0A, 0x00180000, 0xFFFFFFFF, PPC_SPE_SINGLE),
10534GEN_SPE(evfststgt, evfststlt, 0x0E, 0x0A, 0x00600000, 0x00600000, PPC_SPE_SINGLE),
10535GEN_SPE(evfststeq, speundef, 0x0F, 0x0A, 0x00600000, 0xFFFFFFFF, PPC_SPE_SINGLE),
10536
10537GEN_SPE(efsadd, efssub, 0x00, 0x0B, 0x00000000, 0x00000000, PPC_SPE_SINGLE),
10538GEN_SPE(efsabs, efsnabs, 0x02, 0x0B, 0x0000F800, 0x0000F800, PPC_SPE_SINGLE),
10539GEN_SPE(efsneg, speundef, 0x03, 0x0B, 0x0000F800, 0xFFFFFFFF, PPC_SPE_SINGLE),
10540GEN_SPE(efsmul, efsdiv, 0x04, 0x0B, 0x00000000, 0x00000000, PPC_SPE_SINGLE),
10541GEN_SPE(efscmpgt, efscmplt, 0x06, 0x0B, 0x00600000, 0x00600000, PPC_SPE_SINGLE),
10542GEN_SPE(efscmpeq, efscfd, 0x07, 0x0B, 0x00600000, 0x00180000, PPC_SPE_SINGLE),
10543GEN_SPE(efscfui, efscfsi, 0x08, 0x0B, 0x00180000, 0x00180000, PPC_SPE_SINGLE),
10544GEN_SPE(efscfuf, efscfsf, 0x09, 0x0B, 0x00180000, 0x00180000, PPC_SPE_SINGLE),
10545GEN_SPE(efsctui, efsctsi, 0x0A, 0x0B, 0x00180000, 0x00180000, PPC_SPE_SINGLE),
10546GEN_SPE(efsctuf, efsctsf, 0x0B, 0x0B, 0x00180000, 0x00180000, PPC_SPE_SINGLE),
10547GEN_SPE(efsctuiz, speundef, 0x0C, 0x0B, 0x00180000, 0xFFFFFFFF, PPC_SPE_SINGLE),
10548GEN_SPE(efsctsiz, speundef, 0x0D, 0x0B, 0x00180000, 0xFFFFFFFF, PPC_SPE_SINGLE),
10549GEN_SPE(efststgt, efststlt, 0x0E, 0x0B, 0x00600000, 0x00600000, PPC_SPE_SINGLE),
10550GEN_SPE(efststeq, speundef, 0x0F, 0x0B, 0x00600000, 0xFFFFFFFF, PPC_SPE_SINGLE),
10551
10552GEN_SPE(efdadd, efdsub, 0x10, 0x0B, 0x00000000, 0x00000000, PPC_SPE_DOUBLE),
10553GEN_SPE(efdcfuid, efdcfsid, 0x11, 0x0B, 0x00180000, 0x00180000, PPC_SPE_DOUBLE),
10554GEN_SPE(efdabs, efdnabs, 0x12, 0x0B, 0x0000F800, 0x0000F800, PPC_SPE_DOUBLE),
10555GEN_SPE(efdneg, speundef, 0x13, 0x0B, 0x0000F800, 0xFFFFFFFF, PPC_SPE_DOUBLE),
10556GEN_SPE(efdmul, efddiv, 0x14, 0x0B, 0x00000000, 0x00000000, PPC_SPE_DOUBLE),
10557GEN_SPE(efdctuidz, efdctsidz, 0x15, 0x0B, 0x00180000, 0x00180000, PPC_SPE_DOUBLE),
10558GEN_SPE(efdcmpgt, efdcmplt, 0x16, 0x0B, 0x00600000, 0x00600000, PPC_SPE_DOUBLE),
10559GEN_SPE(efdcmpeq, efdcfs, 0x17, 0x0B, 0x00600000, 0x00180000, PPC_SPE_DOUBLE),
10560GEN_SPE(efdcfui, efdcfsi, 0x18, 0x0B, 0x00180000, 0x00180000, PPC_SPE_DOUBLE),
10561GEN_SPE(efdcfuf, efdcfsf, 0x19, 0x0B, 0x00180000, 0x00180000, PPC_SPE_DOUBLE),
10562GEN_SPE(efdctui, efdctsi, 0x1A, 0x0B, 0x00180000, 0x00180000, PPC_SPE_DOUBLE),
10563GEN_SPE(efdctuf, efdctsf, 0x1B, 0x0B, 0x00180000, 0x00180000, PPC_SPE_DOUBLE),
10564GEN_SPE(efdctuiz, speundef, 0x1C, 0x0B, 0x00180000, 0xFFFFFFFF, PPC_SPE_DOUBLE),
10565GEN_SPE(efdctsiz, speundef, 0x1D, 0x0B, 0x00180000, 0xFFFFFFFF, PPC_SPE_DOUBLE),
10566GEN_SPE(efdtstgt, efdtstlt, 0x1E, 0x0B, 0x00600000, 0x00600000, PPC_SPE_DOUBLE),
10567GEN_SPE(efdtsteq, speundef, 0x1F, 0x0B, 0x00600000, 0xFFFFFFFF, PPC_SPE_DOUBLE),
5c55ff99
BS
10568
10569#undef GEN_SPEOP_LDST
10570#define GEN_SPEOP_LDST(name, opc2, sh) \
10571GEN_HANDLER(name, 0x04, opc2, 0x0C, 0x00000000, PPC_SPE)
10572GEN_SPEOP_LDST(evldd, 0x00, 3),
10573GEN_SPEOP_LDST(evldw, 0x01, 3),
10574GEN_SPEOP_LDST(evldh, 0x02, 3),
10575GEN_SPEOP_LDST(evlhhesplat, 0x04, 1),
10576GEN_SPEOP_LDST(evlhhousplat, 0x06, 1),
10577GEN_SPEOP_LDST(evlhhossplat, 0x07, 1),
10578GEN_SPEOP_LDST(evlwhe, 0x08, 2),
10579GEN_SPEOP_LDST(evlwhou, 0x0A, 2),
10580GEN_SPEOP_LDST(evlwhos, 0x0B, 2),
10581GEN_SPEOP_LDST(evlwwsplat, 0x0C, 2),
10582GEN_SPEOP_LDST(evlwhsplat, 0x0E, 2),
10583
10584GEN_SPEOP_LDST(evstdd, 0x10, 3),
10585GEN_SPEOP_LDST(evstdw, 0x11, 3),
10586GEN_SPEOP_LDST(evstdh, 0x12, 3),
10587GEN_SPEOP_LDST(evstwhe, 0x18, 2),
10588GEN_SPEOP_LDST(evstwho, 0x1A, 2),
10589GEN_SPEOP_LDST(evstwwe, 0x1C, 2),
10590GEN_SPEOP_LDST(evstwwo, 0x1E, 2),
10591};
10592
0411a972 10593#include "helper_regs.h"
a1389542 10594#include "translate_init.c"
79aceca5 10595
9a64fbe4 10596/*****************************************************************************/
3fc6c082 10597/* Misc PowerPC helpers */
878096ee
AF
10598void ppc_cpu_dump_state(CPUState *cs, FILE *f, fprintf_function cpu_fprintf,
10599 int flags)
79aceca5 10600{
3fc6c082
FB
10601#define RGPL 4
10602#define RFPL 4
3fc6c082 10603
878096ee
AF
10604 PowerPCCPU *cpu = POWERPC_CPU(cs);
10605 CPUPPCState *env = &cpu->env;
79aceca5
FB
10606 int i;
10607
90e189ec 10608 cpu_fprintf(f, "NIP " TARGET_FMT_lx " LR " TARGET_FMT_lx " CTR "
9a78eead 10609 TARGET_FMT_lx " XER " TARGET_FMT_lx "\n",
da91a00f 10610 env->nip, env->lr, env->ctr, cpu_read_xer(env));
90e189ec
BS
10611 cpu_fprintf(f, "MSR " TARGET_FMT_lx " HID0 " TARGET_FMT_lx " HF "
10612 TARGET_FMT_lx " idx %d\n", env->msr, env->spr[SPR_HID0],
10613 env->hflags, env->mmu_idx);
d9bce9d9 10614#if !defined(NO_TIMER_DUMP)
9a78eead 10615 cpu_fprintf(f, "TB %08" PRIu32 " %08" PRIu64
76a66253 10616#if !defined(CONFIG_USER_ONLY)
9a78eead 10617 " DECR %08" PRIu32
76a66253
JM
10618#endif
10619 "\n",
077fc206 10620 cpu_ppc_load_tbu(env), cpu_ppc_load_tbl(env)
76a66253
JM
10621#if !defined(CONFIG_USER_ONLY)
10622 , cpu_ppc_load_decr(env)
10623#endif
10624 );
077fc206 10625#endif
76a66253 10626 for (i = 0; i < 32; i++) {
3fc6c082
FB
10627 if ((i & (RGPL - 1)) == 0)
10628 cpu_fprintf(f, "GPR%02d", i);
b11ebf64 10629 cpu_fprintf(f, " %016" PRIx64, ppc_dump_gpr(env, i));
3fc6c082 10630 if ((i & (RGPL - 1)) == (RGPL - 1))
7fe48483 10631 cpu_fprintf(f, "\n");
76a66253 10632 }
3fc6c082 10633 cpu_fprintf(f, "CR ");
76a66253 10634 for (i = 0; i < 8; i++)
7fe48483
FB
10635 cpu_fprintf(f, "%01x", env->crf[i]);
10636 cpu_fprintf(f, " [");
76a66253
JM
10637 for (i = 0; i < 8; i++) {
10638 char a = '-';
10639 if (env->crf[i] & 0x08)
10640 a = 'L';
10641 else if (env->crf[i] & 0x04)
10642 a = 'G';
10643 else if (env->crf[i] & 0x02)
10644 a = 'E';
7fe48483 10645 cpu_fprintf(f, " %c%c", a, env->crf[i] & 0x01 ? 'O' : ' ');
76a66253 10646 }
90e189ec
BS
10647 cpu_fprintf(f, " ] RES " TARGET_FMT_lx "\n",
10648 env->reserve_addr);
3fc6c082
FB
10649 for (i = 0; i < 32; i++) {
10650 if ((i & (RFPL - 1)) == 0)
10651 cpu_fprintf(f, "FPR%02d", i);
26a76461 10652 cpu_fprintf(f, " %016" PRIx64, *((uint64_t *)&env->fpr[i]));
3fc6c082 10653 if ((i & (RFPL - 1)) == (RFPL - 1))
7fe48483 10654 cpu_fprintf(f, "\n");
79aceca5 10655 }
30304420 10656 cpu_fprintf(f, "FPSCR " TARGET_FMT_lx "\n", env->fpscr);
f2e63a42 10657#if !defined(CONFIG_USER_ONLY)
90dc8812
SW
10658 cpu_fprintf(f, " SRR0 " TARGET_FMT_lx " SRR1 " TARGET_FMT_lx
10659 " PVR " TARGET_FMT_lx " VRSAVE " TARGET_FMT_lx "\n",
10660 env->spr[SPR_SRR0], env->spr[SPR_SRR1],
10661 env->spr[SPR_PVR], env->spr[SPR_VRSAVE]);
10662
10663 cpu_fprintf(f, "SPRG0 " TARGET_FMT_lx " SPRG1 " TARGET_FMT_lx
10664 " SPRG2 " TARGET_FMT_lx " SPRG3 " TARGET_FMT_lx "\n",
10665 env->spr[SPR_SPRG0], env->spr[SPR_SPRG1],
10666 env->spr[SPR_SPRG2], env->spr[SPR_SPRG3]);
10667
10668 cpu_fprintf(f, "SPRG4 " TARGET_FMT_lx " SPRG5 " TARGET_FMT_lx
10669 " SPRG6 " TARGET_FMT_lx " SPRG7 " TARGET_FMT_lx "\n",
10670 env->spr[SPR_SPRG4], env->spr[SPR_SPRG5],
10671 env->spr[SPR_SPRG6], env->spr[SPR_SPRG7]);
10672
10673 if (env->excp_model == POWERPC_EXCP_BOOKE) {
10674 cpu_fprintf(f, "CSRR0 " TARGET_FMT_lx " CSRR1 " TARGET_FMT_lx
10675 " MCSRR0 " TARGET_FMT_lx " MCSRR1 " TARGET_FMT_lx "\n",
10676 env->spr[SPR_BOOKE_CSRR0], env->spr[SPR_BOOKE_CSRR1],
10677 env->spr[SPR_BOOKE_MCSRR0], env->spr[SPR_BOOKE_MCSRR1]);
10678
10679 cpu_fprintf(f, " TCR " TARGET_FMT_lx " TSR " TARGET_FMT_lx
10680 " ESR " TARGET_FMT_lx " DEAR " TARGET_FMT_lx "\n",
10681 env->spr[SPR_BOOKE_TCR], env->spr[SPR_BOOKE_TSR],
10682 env->spr[SPR_BOOKE_ESR], env->spr[SPR_BOOKE_DEAR]);
10683
10684 cpu_fprintf(f, " PIR " TARGET_FMT_lx " DECAR " TARGET_FMT_lx
10685 " IVPR " TARGET_FMT_lx " EPCR " TARGET_FMT_lx "\n",
10686 env->spr[SPR_BOOKE_PIR], env->spr[SPR_BOOKE_DECAR],
10687 env->spr[SPR_BOOKE_IVPR], env->spr[SPR_BOOKE_EPCR]);
10688
10689 cpu_fprintf(f, " MCSR " TARGET_FMT_lx " SPRG8 " TARGET_FMT_lx
10690 " EPR " TARGET_FMT_lx "\n",
10691 env->spr[SPR_BOOKE_MCSR], env->spr[SPR_BOOKE_SPRG8],
10692 env->spr[SPR_BOOKE_EPR]);
10693
10694 /* FSL-specific */
10695 cpu_fprintf(f, " MCAR " TARGET_FMT_lx " PID1 " TARGET_FMT_lx
10696 " PID2 " TARGET_FMT_lx " SVR " TARGET_FMT_lx "\n",
10697 env->spr[SPR_Exxx_MCAR], env->spr[SPR_BOOKE_PID1],
10698 env->spr[SPR_BOOKE_PID2], env->spr[SPR_E500_SVR]);
10699
10700 /*
10701 * IVORs are left out as they are large and do not change often --
10702 * they can be read with "p $ivor0", "p $ivor1", etc.
10703 */
10704 }
10705
697ab892
DG
10706#if defined(TARGET_PPC64)
10707 if (env->flags & POWERPC_FLAG_CFAR) {
10708 cpu_fprintf(f, " CFAR " TARGET_FMT_lx"\n", env->cfar);
10709 }
10710#endif
10711
90dc8812
SW
10712 switch (env->mmu_model) {
10713 case POWERPC_MMU_32B:
10714 case POWERPC_MMU_601:
10715 case POWERPC_MMU_SOFT_6xx:
10716 case POWERPC_MMU_SOFT_74xx:
10717#if defined(TARGET_PPC64)
90dc8812 10718 case POWERPC_MMU_64B:
ca480de6
AB
10719 case POWERPC_MMU_2_06:
10720 case POWERPC_MMU_2_06a:
10721 case POWERPC_MMU_2_06d:
90dc8812 10722#endif
ca480de6
AB
10723 cpu_fprintf(f, " SDR1 " TARGET_FMT_lx " DAR " TARGET_FMT_lx
10724 " DSISR " TARGET_FMT_lx "\n", env->spr[SPR_SDR1],
10725 env->spr[SPR_DAR], env->spr[SPR_DSISR]);
90dc8812 10726 break;
01662f3e 10727 case POWERPC_MMU_BOOKE206:
90dc8812
SW
10728 cpu_fprintf(f, " MAS0 " TARGET_FMT_lx " MAS1 " TARGET_FMT_lx
10729 " MAS2 " TARGET_FMT_lx " MAS3 " TARGET_FMT_lx "\n",
10730 env->spr[SPR_BOOKE_MAS0], env->spr[SPR_BOOKE_MAS1],
10731 env->spr[SPR_BOOKE_MAS2], env->spr[SPR_BOOKE_MAS3]);
10732
10733 cpu_fprintf(f, " MAS4 " TARGET_FMT_lx " MAS6 " TARGET_FMT_lx
10734 " MAS7 " TARGET_FMT_lx " PID " TARGET_FMT_lx "\n",
10735 env->spr[SPR_BOOKE_MAS4], env->spr[SPR_BOOKE_MAS6],
10736 env->spr[SPR_BOOKE_MAS7], env->spr[SPR_BOOKE_PID]);
10737
10738 cpu_fprintf(f, "MMUCFG " TARGET_FMT_lx " TLB0CFG " TARGET_FMT_lx
10739 " TLB1CFG " TARGET_FMT_lx "\n",
10740 env->spr[SPR_MMUCFG], env->spr[SPR_BOOKE_TLB0CFG],
10741 env->spr[SPR_BOOKE_TLB1CFG]);
10742 break;
10743 default:
10744 break;
10745 }
f2e63a42 10746#endif
79aceca5 10747
3fc6c082
FB
10748#undef RGPL
10749#undef RFPL
79aceca5
FB
10750}
10751
878096ee
AF
10752void ppc_cpu_dump_statistics(CPUState *cs, FILE*f,
10753 fprintf_function cpu_fprintf, int flags)
76a66253
JM
10754{
10755#if defined(DO_PPC_STATISTICS)
878096ee 10756 PowerPCCPU *cpu = POWERPC_CPU(cs);
c227f099 10757 opc_handler_t **t1, **t2, **t3, *handler;
76a66253
JM
10758 int op1, op2, op3;
10759
878096ee 10760 t1 = cpu->env.opcodes;
76a66253
JM
10761 for (op1 = 0; op1 < 64; op1++) {
10762 handler = t1[op1];
10763 if (is_indirect_opcode(handler)) {
10764 t2 = ind_table(handler);
10765 for (op2 = 0; op2 < 32; op2++) {
10766 handler = t2[op2];
10767 if (is_indirect_opcode(handler)) {
10768 t3 = ind_table(handler);
10769 for (op3 = 0; op3 < 32; op3++) {
10770 handler = t3[op3];
10771 if (handler->count == 0)
10772 continue;
10773 cpu_fprintf(f, "%02x %02x %02x (%02x %04d) %16s: "
0bfcd599 10774 "%016" PRIx64 " %" PRId64 "\n",
76a66253
JM
10775 op1, op2, op3, op1, (op3 << 5) | op2,
10776 handler->oname,
10777 handler->count, handler->count);
10778 }
10779 } else {
10780 if (handler->count == 0)
10781 continue;
10782 cpu_fprintf(f, "%02x %02x (%02x %04d) %16s: "
0bfcd599 10783 "%016" PRIx64 " %" PRId64 "\n",
76a66253
JM
10784 op1, op2, op1, op2, handler->oname,
10785 handler->count, handler->count);
10786 }
10787 }
10788 } else {
10789 if (handler->count == 0)
10790 continue;
0bfcd599
BS
10791 cpu_fprintf(f, "%02x (%02x ) %16s: %016" PRIx64
10792 " %" PRId64 "\n",
76a66253
JM
10793 op1, op1, handler->oname,
10794 handler->count, handler->count);
10795 }
10796 }
10797#endif
10798}
10799
9a64fbe4 10800/*****************************************************************************/
213fe1f5 10801static inline void gen_intermediate_code_internal(PowerPCCPU *cpu,
636aa200 10802 TranslationBlock *tb,
213fe1f5 10803 bool search_pc)
79aceca5 10804{
ed2803da 10805 CPUState *cs = CPU(cpu);
213fe1f5 10806 CPUPPCState *env = &cpu->env;
9fddaa0c 10807 DisasContext ctx, *ctxp = &ctx;
c227f099 10808 opc_handler_t **table, *handler;
0fa85d43 10809 target_ulong pc_start;
79aceca5 10810 uint16_t *gen_opc_end;
a1d1bb31 10811 CPUBreakpoint *bp;
79aceca5 10812 int j, lj = -1;
2e70f6ef
PB
10813 int num_insns;
10814 int max_insns;
79aceca5
FB
10815
10816 pc_start = tb->pc;
92414b31 10817 gen_opc_end = tcg_ctx.gen_opc_buf + OPC_MAX_SIZE;
046d6672 10818 ctx.nip = pc_start;
79aceca5 10819 ctx.tb = tb;
e1833e1f 10820 ctx.exception = POWERPC_EXCP_NONE;
3fc6c082 10821 ctx.spr_cb = env->spr_cb;
76db3ba4 10822 ctx.mem_idx = env->mmu_idx;
7d08d856
AJ
10823 ctx.insns_flags = env->insns_flags;
10824 ctx.insns_flags2 = env->insns_flags2;
76db3ba4
AJ
10825 ctx.access_type = -1;
10826 ctx.le_mode = env->hflags & (1 << MSR_LE) ? 1 : 0;
d9bce9d9 10827#if defined(TARGET_PPC64)
e42a61f1 10828 ctx.sf_mode = msr_is_64bit(env, env->msr);
697ab892 10829 ctx.has_cfar = !!(env->flags & POWERPC_FLAG_CFAR);
9a64fbe4 10830#endif
3cc62370 10831 ctx.fpu_enabled = msr_fp;
a9d9eb8f 10832 if ((env->flags & POWERPC_FLAG_SPE) && msr_spe)
d26bfc9a
JM
10833 ctx.spe_enabled = msr_spe;
10834 else
10835 ctx.spe_enabled = 0;
a9d9eb8f
JM
10836 if ((env->flags & POWERPC_FLAG_VRE) && msr_vr)
10837 ctx.altivec_enabled = msr_vr;
10838 else
10839 ctx.altivec_enabled = 0;
1f29871c
TM
10840 if ((env->flags & POWERPC_FLAG_VSX) && msr_vsx) {
10841 ctx.vsx_enabled = msr_vsx;
10842 } else {
10843 ctx.vsx_enabled = 0;
10844 }
d26bfc9a 10845 if ((env->flags & POWERPC_FLAG_SE) && msr_se)
8cbcb4fa 10846 ctx.singlestep_enabled = CPU_SINGLE_STEP;
d26bfc9a 10847 else
8cbcb4fa 10848 ctx.singlestep_enabled = 0;
d26bfc9a 10849 if ((env->flags & POWERPC_FLAG_BE) && msr_be)
8cbcb4fa 10850 ctx.singlestep_enabled |= CPU_BRANCH_STEP;
ed2803da 10851 if (unlikely(cs->singlestep_enabled)) {
8cbcb4fa 10852 ctx.singlestep_enabled |= GDBSTUB_SINGLE_STEP;
ed2803da 10853 }
3fc6c082 10854#if defined (DO_SINGLE_STEP) && 0
9a64fbe4
FB
10855 /* Single step trace mode */
10856 msr_se = 1;
10857#endif
2e70f6ef
PB
10858 num_insns = 0;
10859 max_insns = tb->cflags & CF_COUNT_MASK;
10860 if (max_insns == 0)
10861 max_insns = CF_COUNT_MASK;
10862
806f352d 10863 gen_tb_start();
9a64fbe4 10864 /* Set env in case of segfault during code fetch */
efd7f486
EV
10865 while (ctx.exception == POWERPC_EXCP_NONE
10866 && tcg_ctx.gen_opc_ptr < gen_opc_end) {
72cf2d4f
BS
10867 if (unlikely(!QTAILQ_EMPTY(&env->breakpoints))) {
10868 QTAILQ_FOREACH(bp, &env->breakpoints, entry) {
a1d1bb31 10869 if (bp->pc == ctx.nip) {
e06fcd75 10870 gen_debug_exception(ctxp);
ea4e754f
FB
10871 break;
10872 }
10873 }
10874 }
76a66253 10875 if (unlikely(search_pc)) {
92414b31 10876 j = tcg_ctx.gen_opc_ptr - tcg_ctx.gen_opc_buf;
79aceca5
FB
10877 if (lj < j) {
10878 lj++;
10879 while (lj < j)
ab1103de 10880 tcg_ctx.gen_opc_instr_start[lj++] = 0;
79aceca5 10881 }
25983cad 10882 tcg_ctx.gen_opc_pc[lj] = ctx.nip;
ab1103de 10883 tcg_ctx.gen_opc_instr_start[lj] = 1;
c9c99c22 10884 tcg_ctx.gen_opc_icount[lj] = num_insns;
79aceca5 10885 }
d12d51d5 10886 LOG_DISAS("----------------\n");
90e189ec 10887 LOG_DISAS("nip=" TARGET_FMT_lx " super=%d ir=%d\n",
d12d51d5 10888 ctx.nip, ctx.mem_idx, (int)msr_ir);
2e70f6ef
PB
10889 if (num_insns + 1 == max_insns && (tb->cflags & CF_LAST_IO))
10890 gen_io_start();
76db3ba4 10891 if (unlikely(ctx.le_mode)) {
2f5a189c 10892 ctx.opcode = bswap32(cpu_ldl_code(env, ctx.nip));
056401ea 10893 } else {
2f5a189c 10894 ctx.opcode = cpu_ldl_code(env, ctx.nip);
111bfab3 10895 }
d12d51d5 10896 LOG_DISAS("translate opcode %08x (%02x %02x %02x) (%s)\n",
9a64fbe4 10897 ctx.opcode, opc1(ctx.opcode), opc2(ctx.opcode),
476b6d16 10898 opc3(ctx.opcode), ctx.le_mode ? "little" : "big");
fdefe51c 10899 if (unlikely(qemu_loglevel_mask(CPU_LOG_TB_OP | CPU_LOG_TB_OP_OPT))) {
731c54f8 10900 tcg_gen_debug_insn_start(ctx.nip);
fdefe51c 10901 }
046d6672 10902 ctx.nip += 4;
3fc6c082 10903 table = env->opcodes;
2e70f6ef 10904 num_insns++;
79aceca5
FB
10905 handler = table[opc1(ctx.opcode)];
10906 if (is_indirect_opcode(handler)) {
10907 table = ind_table(handler);
10908 handler = table[opc2(ctx.opcode)];
10909 if (is_indirect_opcode(handler)) {
10910 table = ind_table(handler);
10911 handler = table[opc3(ctx.opcode)];
10912 }
10913 }
10914 /* Is opcode *REALLY* valid ? */
76a66253 10915 if (unlikely(handler->handler == &gen_invalid)) {
93fcfe39
AL
10916 if (qemu_log_enabled()) {
10917 qemu_log("invalid/unsupported opcode: "
90e189ec
BS
10918 "%02x - %02x - %02x (%08x) " TARGET_FMT_lx " %d\n",
10919 opc1(ctx.opcode), opc2(ctx.opcode),
10920 opc3(ctx.opcode), ctx.opcode, ctx.nip - 4, (int)msr_ir);
4b3686fa 10921 }
76a66253 10922 } else {
70560da7
FC
10923 uint32_t inval;
10924
10925 if (unlikely(handler->type & (PPC_SPE | PPC_SPE_SINGLE | PPC_SPE_DOUBLE) && Rc(ctx.opcode))) {
10926 inval = handler->inval2;
10927 } else {
10928 inval = handler->inval1;
10929 }
10930
10931 if (unlikely((ctx.opcode & inval) != 0)) {
93fcfe39
AL
10932 if (qemu_log_enabled()) {
10933 qemu_log("invalid bits: %08x for opcode: "
90e189ec 10934 "%02x - %02x - %02x (%08x) " TARGET_FMT_lx "\n",
70560da7 10935 ctx.opcode & inval, opc1(ctx.opcode),
90e189ec
BS
10936 opc2(ctx.opcode), opc3(ctx.opcode),
10937 ctx.opcode, ctx.nip - 4);
76a66253 10938 }
e06fcd75 10939 gen_inval_exception(ctxp, POWERPC_EXCP_INVAL_INVAL);
4b3686fa 10940 break;
79aceca5 10941 }
79aceca5 10942 }
4b3686fa 10943 (*(handler->handler))(&ctx);
76a66253
JM
10944#if defined(DO_PPC_STATISTICS)
10945 handler->count++;
10946#endif
9a64fbe4 10947 /* Check trace mode exceptions */
8cbcb4fa
AJ
10948 if (unlikely(ctx.singlestep_enabled & CPU_SINGLE_STEP &&
10949 (ctx.nip <= 0x100 || ctx.nip > 0xF00) &&
10950 ctx.exception != POWERPC_SYSCALL &&
10951 ctx.exception != POWERPC_EXCP_TRAP &&
10952 ctx.exception != POWERPC_EXCP_BRANCH)) {
e06fcd75 10953 gen_exception(ctxp, POWERPC_EXCP_TRACE);
d26bfc9a 10954 } else if (unlikely(((ctx.nip & (TARGET_PAGE_SIZE - 1)) == 0) ||
ed2803da 10955 (cs->singlestep_enabled) ||
1b530a6d 10956 singlestep ||
2e70f6ef 10957 num_insns >= max_insns)) {
d26bfc9a
JM
10958 /* if we reach a page boundary or are single stepping, stop
10959 * generation
10960 */
8dd4983c 10961 break;
76a66253 10962 }
3fc6c082 10963 }
2e70f6ef
PB
10964 if (tb->cflags & CF_LAST_IO)
10965 gen_io_end();
e1833e1f 10966 if (ctx.exception == POWERPC_EXCP_NONE) {
c1942362 10967 gen_goto_tb(&ctx, 0, ctx.nip);
e1833e1f 10968 } else if (ctx.exception != POWERPC_EXCP_BRANCH) {
ed2803da 10969 if (unlikely(cs->singlestep_enabled)) {
e06fcd75 10970 gen_debug_exception(ctxp);
8cbcb4fa 10971 }
76a66253 10972 /* Generate the return instruction */
57fec1fe 10973 tcg_gen_exit_tb(0);
9a64fbe4 10974 }
806f352d 10975 gen_tb_end(tb, num_insns);
efd7f486 10976 *tcg_ctx.gen_opc_ptr = INDEX_op_end;
76a66253 10977 if (unlikely(search_pc)) {
92414b31 10978 j = tcg_ctx.gen_opc_ptr - tcg_ctx.gen_opc_buf;
9a64fbe4
FB
10979 lj++;
10980 while (lj <= j)
ab1103de 10981 tcg_ctx.gen_opc_instr_start[lj++] = 0;
9a64fbe4 10982 } else {
046d6672 10983 tb->size = ctx.nip - pc_start;
2e70f6ef 10984 tb->icount = num_insns;
9a64fbe4 10985 }
d9bce9d9 10986#if defined(DEBUG_DISAS)
8fec2b8c 10987 if (qemu_loglevel_mask(CPU_LOG_TB_IN_ASM)) {
76a66253 10988 int flags;
237c0af0 10989 flags = env->bfd_mach;
76db3ba4 10990 flags |= ctx.le_mode << 16;
93fcfe39 10991 qemu_log("IN: %s\n", lookup_symbol(pc_start));
f4359b9f 10992 log_target_disas(env, pc_start, ctx.nip - pc_start, flags);
93fcfe39 10993 qemu_log("\n");
9fddaa0c 10994 }
79aceca5 10995#endif
79aceca5
FB
10996}
10997
1328c2bf 10998void gen_intermediate_code (CPUPPCState *env, struct TranslationBlock *tb)
79aceca5 10999{
213fe1f5 11000 gen_intermediate_code_internal(ppc_env_get_cpu(env), tb, false);
79aceca5
FB
11001}
11002
1328c2bf 11003void gen_intermediate_code_pc (CPUPPCState *env, struct TranslationBlock *tb)
79aceca5 11004{
213fe1f5 11005 gen_intermediate_code_internal(ppc_env_get_cpu(env), tb, true);
79aceca5 11006}
d2856f1a 11007
1328c2bf 11008void restore_state_to_opc(CPUPPCState *env, TranslationBlock *tb, int pc_pos)
d2856f1a 11009{
25983cad 11010 env->nip = tcg_ctx.gen_opc_pc[pc_pos];
d2856f1a 11011}