]> git.ipfire.org Git - thirdparty/qemu.git/blame - target-ppc/translate.c
target-ppc: emulate cmpb instruction
[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
AJ
53 + 2*(10*6 + 22*7) /* AVRh, AVRl */
54 + 8*5 /* CRF */];
f78fb44e
AJ
55static TCGv cpu_gpr[32];
56#if !defined(TARGET_PPC64)
57static TCGv cpu_gprh[32];
58#endif
a7812ae4
PB
59static TCGv_i64 cpu_fpr[32];
60static TCGv_i64 cpu_avrh[32], cpu_avrl[32];
61static TCGv_i32 cpu_crf[8];
bd568f18 62static TCGv cpu_nip;
6527f6ea 63static TCGv cpu_msr;
cfdcd37a
AJ
64static TCGv cpu_ctr;
65static TCGv cpu_lr;
697ab892
DG
66#if defined(TARGET_PPC64)
67static TCGv cpu_cfar;
68#endif
da91a00f 69static TCGv cpu_xer, cpu_so, cpu_ov, cpu_ca;
cf360a32 70static TCGv cpu_reserve;
30304420 71static TCGv cpu_fpscr;
a7859e89 72static TCGv_i32 cpu_access_type;
f78fb44e 73
022c62cb 74#include "exec/gen-icount.h"
2e70f6ef
PB
75
76void ppc_translate_init(void)
77{
f78fb44e
AJ
78 int i;
79 char* p;
2dc766da 80 size_t cpu_reg_names_size;
b2437bf2 81 static int done_init = 0;
f78fb44e 82
2e70f6ef
PB
83 if (done_init)
84 return;
f78fb44e 85
a7812ae4 86 cpu_env = tcg_global_reg_new_ptr(TCG_AREG0, "env");
a7812ae4 87
f78fb44e 88 p = cpu_reg_names;
2dc766da 89 cpu_reg_names_size = sizeof(cpu_reg_names);
47e4661c
AJ
90
91 for (i = 0; i < 8; i++) {
2dc766da 92 snprintf(p, cpu_reg_names_size, "crf%d", i);
a7812ae4 93 cpu_crf[i] = tcg_global_mem_new_i32(TCG_AREG0,
1328c2bf 94 offsetof(CPUPPCState, crf[i]), p);
47e4661c 95 p += 5;
2dc766da 96 cpu_reg_names_size -= 5;
47e4661c
AJ
97 }
98
f78fb44e 99 for (i = 0; i < 32; i++) {
2dc766da 100 snprintf(p, cpu_reg_names_size, "r%d", i);
a7812ae4 101 cpu_gpr[i] = tcg_global_mem_new(TCG_AREG0,
1328c2bf 102 offsetof(CPUPPCState, gpr[i]), p);
f78fb44e 103 p += (i < 10) ? 3 : 4;
2dc766da 104 cpu_reg_names_size -= (i < 10) ? 3 : 4;
f78fb44e 105#if !defined(TARGET_PPC64)
2dc766da 106 snprintf(p, cpu_reg_names_size, "r%dH", i);
a7812ae4 107 cpu_gprh[i] = tcg_global_mem_new_i32(TCG_AREG0,
1328c2bf 108 offsetof(CPUPPCState, gprh[i]), p);
f78fb44e 109 p += (i < 10) ? 4 : 5;
2dc766da 110 cpu_reg_names_size -= (i < 10) ? 4 : 5;
f78fb44e 111#endif
1d542695 112
2dc766da 113 snprintf(p, cpu_reg_names_size, "fp%d", i);
a7812ae4 114 cpu_fpr[i] = tcg_global_mem_new_i64(TCG_AREG0,
1328c2bf 115 offsetof(CPUPPCState, fpr[i]), p);
ec1ac72d 116 p += (i < 10) ? 4 : 5;
2dc766da 117 cpu_reg_names_size -= (i < 10) ? 4 : 5;
a5e26afa 118
2dc766da 119 snprintf(p, cpu_reg_names_size, "avr%dH", i);
e2542fe2 120#ifdef HOST_WORDS_BIGENDIAN
fe1e5c53 121 cpu_avrh[i] = tcg_global_mem_new_i64(TCG_AREG0,
1328c2bf 122 offsetof(CPUPPCState, avr[i].u64[0]), p);
fe1e5c53 123#else
a7812ae4 124 cpu_avrh[i] = tcg_global_mem_new_i64(TCG_AREG0,
1328c2bf 125 offsetof(CPUPPCState, avr[i].u64[1]), p);
fe1e5c53 126#endif
1d542695 127 p += (i < 10) ? 6 : 7;
2dc766da 128 cpu_reg_names_size -= (i < 10) ? 6 : 7;
ec1ac72d 129
2dc766da 130 snprintf(p, cpu_reg_names_size, "avr%dL", i);
e2542fe2 131#ifdef HOST_WORDS_BIGENDIAN
fe1e5c53 132 cpu_avrl[i] = tcg_global_mem_new_i64(TCG_AREG0,
1328c2bf 133 offsetof(CPUPPCState, avr[i].u64[1]), p);
fe1e5c53 134#else
a7812ae4 135 cpu_avrl[i] = tcg_global_mem_new_i64(TCG_AREG0,
1328c2bf 136 offsetof(CPUPPCState, avr[i].u64[0]), p);
fe1e5c53 137#endif
1d542695 138 p += (i < 10) ? 6 : 7;
2dc766da 139 cpu_reg_names_size -= (i < 10) ? 6 : 7;
f78fb44e 140 }
f10dc08e 141
a7812ae4 142 cpu_nip = tcg_global_mem_new(TCG_AREG0,
1328c2bf 143 offsetof(CPUPPCState, nip), "nip");
bd568f18 144
6527f6ea 145 cpu_msr = tcg_global_mem_new(TCG_AREG0,
1328c2bf 146 offsetof(CPUPPCState, msr), "msr");
6527f6ea 147
a7812ae4 148 cpu_ctr = tcg_global_mem_new(TCG_AREG0,
1328c2bf 149 offsetof(CPUPPCState, ctr), "ctr");
cfdcd37a 150
a7812ae4 151 cpu_lr = tcg_global_mem_new(TCG_AREG0,
1328c2bf 152 offsetof(CPUPPCState, lr), "lr");
cfdcd37a 153
697ab892
DG
154#if defined(TARGET_PPC64)
155 cpu_cfar = tcg_global_mem_new(TCG_AREG0,
1328c2bf 156 offsetof(CPUPPCState, cfar), "cfar");
697ab892
DG
157#endif
158
a7812ae4 159 cpu_xer = tcg_global_mem_new(TCG_AREG0,
1328c2bf 160 offsetof(CPUPPCState, xer), "xer");
da91a00f
RH
161 cpu_so = tcg_global_mem_new(TCG_AREG0,
162 offsetof(CPUPPCState, so), "SO");
163 cpu_ov = tcg_global_mem_new(TCG_AREG0,
164 offsetof(CPUPPCState, ov), "OV");
165 cpu_ca = tcg_global_mem_new(TCG_AREG0,
166 offsetof(CPUPPCState, ca), "CA");
3d7b417e 167
cf360a32 168 cpu_reserve = tcg_global_mem_new(TCG_AREG0,
1328c2bf 169 offsetof(CPUPPCState, reserve_addr),
18b21a2f 170 "reserve_addr");
cf360a32 171
30304420
DG
172 cpu_fpscr = tcg_global_mem_new(TCG_AREG0,
173 offsetof(CPUPPCState, fpscr), "fpscr");
e1571908 174
a7859e89 175 cpu_access_type = tcg_global_mem_new_i32(TCG_AREG0,
1328c2bf 176 offsetof(CPUPPCState, access_type), "access_type");
a7859e89 177
f10dc08e 178 /* register helpers */
a7812ae4 179#define GEN_HELPER 2
f10dc08e
AJ
180#include "helper.h"
181
2e70f6ef
PB
182 done_init = 1;
183}
184
79aceca5
FB
185/* internal defines */
186typedef struct DisasContext {
187 struct TranslationBlock *tb;
0fa85d43 188 target_ulong nip;
79aceca5 189 uint32_t opcode;
9a64fbe4 190 uint32_t exception;
3cc62370
FB
191 /* Routine used to access memory */
192 int mem_idx;
76db3ba4 193 int access_type;
3cc62370 194 /* Translation flags */
76db3ba4 195 int le_mode;
d9bce9d9
JM
196#if defined(TARGET_PPC64)
197 int sf_mode;
697ab892 198 int has_cfar;
9a64fbe4 199#endif
3cc62370 200 int fpu_enabled;
a9d9eb8f 201 int altivec_enabled;
0487d6a8 202 int spe_enabled;
c227f099 203 ppc_spr_t *spr_cb; /* Needed to check rights for mfspr/mtspr */
ea4e754f 204 int singlestep_enabled;
79aceca5
FB
205} DisasContext;
206
79482e5a
RH
207/* True when active word size < size of target_long. */
208#ifdef TARGET_PPC64
209# define NARROW_MODE(C) (!(C)->sf_mode)
210#else
211# define NARROW_MODE(C) 0
212#endif
213
c227f099 214struct opc_handler_t {
70560da7
FC
215 /* invalid bits for instruction 1 (Rc(opcode) == 0) */
216 uint32_t inval1;
217 /* invalid bits for instruction 2 (Rc(opcode) == 1) */
218 uint32_t inval2;
9a64fbe4 219 /* instruction type */
0487d6a8 220 uint64_t type;
a5858d7a
AG
221 /* extended instruction type */
222 uint64_t type2;
79aceca5
FB
223 /* handler */
224 void (*handler)(DisasContext *ctx);
a750fc0b 225#if defined(DO_PPC_STATISTICS) || defined(PPC_DUMP_CPU)
b55266b5 226 const char *oname;
a750fc0b
JM
227#endif
228#if defined(DO_PPC_STATISTICS)
76a66253
JM
229 uint64_t count;
230#endif
3fc6c082 231};
79aceca5 232
636aa200 233static inline void gen_reset_fpstatus(void)
7c58044c 234{
8e703949 235 gen_helper_reset_fpstatus(cpu_env);
7c58044c
JM
236}
237
636aa200 238static inline void gen_compute_fprf(TCGv_i64 arg, int set_fprf, int set_rc)
7c58044c 239{
0f2f39c2 240 TCGv_i32 t0 = tcg_temp_new_i32();
af12906f 241
7c58044c
JM
242 if (set_fprf != 0) {
243 /* This case might be optimized later */
0f2f39c2 244 tcg_gen_movi_i32(t0, 1);
8e703949 245 gen_helper_compute_fprf(t0, cpu_env, arg, t0);
a7812ae4 246 if (unlikely(set_rc)) {
0f2f39c2 247 tcg_gen_mov_i32(cpu_crf[1], t0);
a7812ae4 248 }
8e703949 249 gen_helper_float_check_status(cpu_env);
7c58044c
JM
250 } else if (unlikely(set_rc)) {
251 /* We always need to compute fpcc */
0f2f39c2 252 tcg_gen_movi_i32(t0, 0);
8e703949 253 gen_helper_compute_fprf(t0, cpu_env, arg, t0);
0f2f39c2 254 tcg_gen_mov_i32(cpu_crf[1], t0);
7c58044c 255 }
af12906f 256
0f2f39c2 257 tcg_temp_free_i32(t0);
7c58044c
JM
258}
259
636aa200 260static inline void gen_set_access_type(DisasContext *ctx, int access_type)
a7859e89 261{
76db3ba4
AJ
262 if (ctx->access_type != access_type) {
263 tcg_gen_movi_i32(cpu_access_type, access_type);
264 ctx->access_type = access_type;
265 }
a7859e89
AJ
266}
267
636aa200 268static inline void gen_update_nip(DisasContext *ctx, target_ulong nip)
d9bce9d9 269{
e0c8f9ce
RH
270 if (NARROW_MODE(ctx)) {
271 nip = (uint32_t)nip;
272 }
273 tcg_gen_movi_tl(cpu_nip, nip);
d9bce9d9
JM
274}
275
636aa200 276static inline void gen_exception_err(DisasContext *ctx, uint32_t excp, uint32_t error)
e06fcd75
AJ
277{
278 TCGv_i32 t0, t1;
279 if (ctx->exception == POWERPC_EXCP_NONE) {
280 gen_update_nip(ctx, ctx->nip);
281 }
282 t0 = tcg_const_i32(excp);
283 t1 = tcg_const_i32(error);
e5f17ac6 284 gen_helper_raise_exception_err(cpu_env, t0, t1);
e06fcd75
AJ
285 tcg_temp_free_i32(t0);
286 tcg_temp_free_i32(t1);
287 ctx->exception = (excp);
288}
e1833e1f 289
636aa200 290static inline void gen_exception(DisasContext *ctx, uint32_t excp)
e06fcd75
AJ
291{
292 TCGv_i32 t0;
293 if (ctx->exception == POWERPC_EXCP_NONE) {
294 gen_update_nip(ctx, ctx->nip);
295 }
296 t0 = tcg_const_i32(excp);
e5f17ac6 297 gen_helper_raise_exception(cpu_env, t0);
e06fcd75
AJ
298 tcg_temp_free_i32(t0);
299 ctx->exception = (excp);
300}
e1833e1f 301
636aa200 302static inline void gen_debug_exception(DisasContext *ctx)
e06fcd75
AJ
303{
304 TCGv_i32 t0;
5518f3a6 305
ee2b3994
SB
306 if ((ctx->exception != POWERPC_EXCP_BRANCH) &&
307 (ctx->exception != POWERPC_EXCP_SYNC)) {
5518f3a6 308 gen_update_nip(ctx, ctx->nip);
ee2b3994 309 }
e06fcd75 310 t0 = tcg_const_i32(EXCP_DEBUG);
e5f17ac6 311 gen_helper_raise_exception(cpu_env, t0);
e06fcd75
AJ
312 tcg_temp_free_i32(t0);
313}
9a64fbe4 314
636aa200 315static inline void gen_inval_exception(DisasContext *ctx, uint32_t error)
e06fcd75
AJ
316{
317 gen_exception_err(ctx, POWERPC_EXCP_PROGRAM, POWERPC_EXCP_INVAL | error);
318}
a9d9eb8f 319
f24e5695 320/* Stop translation */
636aa200 321static inline void gen_stop_exception(DisasContext *ctx)
3fc6c082 322{
d9bce9d9 323 gen_update_nip(ctx, ctx->nip);
e1833e1f 324 ctx->exception = POWERPC_EXCP_STOP;
3fc6c082
FB
325}
326
f24e5695 327/* No need to update nip here, as execution flow will change */
636aa200 328static inline void gen_sync_exception(DisasContext *ctx)
2be0071f 329{
e1833e1f 330 ctx->exception = POWERPC_EXCP_SYNC;
2be0071f
FB
331}
332
79aceca5 333#define GEN_HANDLER(name, opc1, opc2, opc3, inval, type) \
a5858d7a
AG
334GEN_OPCODE(name, opc1, opc2, opc3, inval, type, PPC_NONE)
335
336#define GEN_HANDLER_E(name, opc1, opc2, opc3, inval, type, type2) \
337GEN_OPCODE(name, opc1, opc2, opc3, inval, type, type2)
79aceca5 338
c7697e1f 339#define GEN_HANDLER2(name, onam, opc1, opc2, opc3, inval, type) \
a5858d7a
AG
340GEN_OPCODE2(name, onam, opc1, opc2, opc3, inval, type, PPC_NONE)
341
342#define GEN_HANDLER2_E(name, onam, opc1, opc2, opc3, inval, type, type2) \
343GEN_OPCODE2(name, onam, opc1, opc2, opc3, inval, type, type2)
c7697e1f 344
c227f099 345typedef struct opcode_t {
79aceca5 346 unsigned char opc1, opc2, opc3;
1235fc06 347#if HOST_LONG_BITS == 64 /* Explicitly align to 64 bits */
18fba28c
FB
348 unsigned char pad[5];
349#else
350 unsigned char pad[1];
351#endif
c227f099 352 opc_handler_t handler;
b55266b5 353 const char *oname;
c227f099 354} opcode_t;
79aceca5 355
a750fc0b 356/*****************************************************************************/
79aceca5
FB
357/*** Instruction decoding ***/
358#define EXTRACT_HELPER(name, shift, nb) \
636aa200 359static inline uint32_t name(uint32_t opcode) \
79aceca5
FB
360{ \
361 return (opcode >> (shift)) & ((1 << (nb)) - 1); \
362}
363
364#define EXTRACT_SHELPER(name, shift, nb) \
636aa200 365static inline int32_t name(uint32_t opcode) \
79aceca5 366{ \
18fba28c 367 return (int16_t)((opcode >> (shift)) & ((1 << (nb)) - 1)); \
79aceca5
FB
368}
369
370/* Opcode part 1 */
371EXTRACT_HELPER(opc1, 26, 6);
372/* Opcode part 2 */
373EXTRACT_HELPER(opc2, 1, 5);
374/* Opcode part 3 */
375EXTRACT_HELPER(opc3, 6, 5);
376/* Update Cr0 flags */
377EXTRACT_HELPER(Rc, 0, 1);
378/* Destination */
379EXTRACT_HELPER(rD, 21, 5);
380/* Source */
381EXTRACT_HELPER(rS, 21, 5);
382/* First operand */
383EXTRACT_HELPER(rA, 16, 5);
384/* Second operand */
385EXTRACT_HELPER(rB, 11, 5);
386/* Third operand */
387EXTRACT_HELPER(rC, 6, 5);
388/*** Get CRn ***/
389EXTRACT_HELPER(crfD, 23, 3);
390EXTRACT_HELPER(crfS, 18, 3);
391EXTRACT_HELPER(crbD, 21, 5);
392EXTRACT_HELPER(crbA, 16, 5);
393EXTRACT_HELPER(crbB, 11, 5);
394/* SPR / TBL */
3fc6c082 395EXTRACT_HELPER(_SPR, 11, 10);
636aa200 396static inline uint32_t SPR(uint32_t opcode)
3fc6c082
FB
397{
398 uint32_t sprn = _SPR(opcode);
399
400 return ((sprn >> 5) & 0x1F) | ((sprn & 0x1F) << 5);
401}
79aceca5
FB
402/*** Get constants ***/
403EXTRACT_HELPER(IMM, 12, 8);
404/* 16 bits signed immediate value */
405EXTRACT_SHELPER(SIMM, 0, 16);
406/* 16 bits unsigned immediate value */
407EXTRACT_HELPER(UIMM, 0, 16);
21d21583
AJ
408/* 5 bits signed immediate value */
409EXTRACT_HELPER(SIMM5, 16, 5);
27a4edb3
AJ
410/* 5 bits signed immediate value */
411EXTRACT_HELPER(UIMM5, 16, 5);
79aceca5
FB
412/* Bit count */
413EXTRACT_HELPER(NB, 11, 5);
414/* Shift count */
415EXTRACT_HELPER(SH, 11, 5);
cd633b10
AJ
416/* Vector shift count */
417EXTRACT_HELPER(VSH, 6, 4);
79aceca5
FB
418/* Mask start */
419EXTRACT_HELPER(MB, 6, 5);
420/* Mask end */
421EXTRACT_HELPER(ME, 1, 5);
fb0eaffc
FB
422/* Trap operand */
423EXTRACT_HELPER(TO, 21, 5);
79aceca5
FB
424
425EXTRACT_HELPER(CRM, 12, 8);
426EXTRACT_HELPER(FM, 17, 8);
427EXTRACT_HELPER(SR, 16, 4);
e4bb997e 428EXTRACT_HELPER(FPIMM, 12, 4);
fb0eaffc 429
79aceca5
FB
430/*** Jump target decoding ***/
431/* Displacement */
432EXTRACT_SHELPER(d, 0, 16);
433/* Immediate address */
636aa200 434static inline target_ulong LI(uint32_t opcode)
79aceca5
FB
435{
436 return (opcode >> 0) & 0x03FFFFFC;
437}
438
636aa200 439static inline uint32_t BD(uint32_t opcode)
79aceca5
FB
440{
441 return (opcode >> 0) & 0xFFFC;
442}
443
444EXTRACT_HELPER(BO, 21, 5);
445EXTRACT_HELPER(BI, 16, 5);
446/* Absolute/relative address */
447EXTRACT_HELPER(AA, 1, 1);
448/* Link */
449EXTRACT_HELPER(LK, 0, 1);
450
451/* Create a mask between <start> and <end> bits */
636aa200 452static inline target_ulong MASK(uint32_t start, uint32_t end)
79aceca5 453{
76a66253 454 target_ulong ret;
79aceca5 455
76a66253
JM
456#if defined(TARGET_PPC64)
457 if (likely(start == 0)) {
6f2d8978 458 ret = UINT64_MAX << (63 - end);
76a66253 459 } else if (likely(end == 63)) {
6f2d8978 460 ret = UINT64_MAX >> start;
76a66253
JM
461 }
462#else
463 if (likely(start == 0)) {
6f2d8978 464 ret = UINT32_MAX << (31 - end);
76a66253 465 } else if (likely(end == 31)) {
6f2d8978 466 ret = UINT32_MAX >> start;
76a66253
JM
467 }
468#endif
469 else {
470 ret = (((target_ulong)(-1ULL)) >> (start)) ^
471 (((target_ulong)(-1ULL) >> (end)) >> 1);
472 if (unlikely(start > end))
473 return ~ret;
474 }
79aceca5
FB
475
476 return ret;
477}
478
a750fc0b 479/*****************************************************************************/
a750fc0b 480/* PowerPC instructions table */
933dc6eb 481
76a66253 482#if defined(DO_PPC_STATISTICS)
a5858d7a 483#define GEN_OPCODE(name, op1, op2, op3, invl, _typ, _typ2) \
5c55ff99 484{ \
79aceca5
FB
485 .opc1 = op1, \
486 .opc2 = op2, \
487 .opc3 = op3, \
18fba28c 488 .pad = { 0, }, \
79aceca5 489 .handler = { \
70560da7
FC
490 .inval1 = invl, \
491 .type = _typ, \
492 .type2 = _typ2, \
493 .handler = &gen_##name, \
494 .oname = stringify(name), \
495 }, \
496 .oname = stringify(name), \
497}
498#define GEN_OPCODE_DUAL(name, op1, op2, op3, invl1, invl2, _typ, _typ2) \
499{ \
500 .opc1 = op1, \
501 .opc2 = op2, \
502 .opc3 = op3, \
503 .pad = { 0, }, \
504 .handler = { \
505 .inval1 = invl1, \
506 .inval2 = invl2, \
9a64fbe4 507 .type = _typ, \
a5858d7a 508 .type2 = _typ2, \
79aceca5 509 .handler = &gen_##name, \
76a66253 510 .oname = stringify(name), \
79aceca5 511 }, \
3fc6c082 512 .oname = stringify(name), \
79aceca5 513}
a5858d7a 514#define GEN_OPCODE2(name, onam, op1, op2, op3, invl, _typ, _typ2) \
5c55ff99 515{ \
c7697e1f
JM
516 .opc1 = op1, \
517 .opc2 = op2, \
518 .opc3 = op3, \
519 .pad = { 0, }, \
520 .handler = { \
70560da7 521 .inval1 = invl, \
c7697e1f 522 .type = _typ, \
a5858d7a 523 .type2 = _typ2, \
c7697e1f
JM
524 .handler = &gen_##name, \
525 .oname = onam, \
526 }, \
527 .oname = onam, \
528}
76a66253 529#else
a5858d7a 530#define GEN_OPCODE(name, op1, op2, op3, invl, _typ, _typ2) \
5c55ff99 531{ \
c7697e1f
JM
532 .opc1 = op1, \
533 .opc2 = op2, \
534 .opc3 = op3, \
535 .pad = { 0, }, \
536 .handler = { \
70560da7
FC
537 .inval1 = invl, \
538 .type = _typ, \
539 .type2 = _typ2, \
540 .handler = &gen_##name, \
541 }, \
542 .oname = stringify(name), \
543}
544#define GEN_OPCODE_DUAL(name, op1, op2, op3, invl1, invl2, _typ, _typ2) \
545{ \
546 .opc1 = op1, \
547 .opc2 = op2, \
548 .opc3 = op3, \
549 .pad = { 0, }, \
550 .handler = { \
551 .inval1 = invl1, \
552 .inval2 = invl2, \
c7697e1f 553 .type = _typ, \
a5858d7a 554 .type2 = _typ2, \
c7697e1f 555 .handler = &gen_##name, \
5c55ff99
BS
556 }, \
557 .oname = stringify(name), \
558}
a5858d7a 559#define GEN_OPCODE2(name, onam, op1, op2, op3, invl, _typ, _typ2) \
5c55ff99
BS
560{ \
561 .opc1 = op1, \
562 .opc2 = op2, \
563 .opc3 = op3, \
564 .pad = { 0, }, \
565 .handler = { \
70560da7 566 .inval1 = invl, \
5c55ff99 567 .type = _typ, \
a5858d7a 568 .type2 = _typ2, \
5c55ff99
BS
569 .handler = &gen_##name, \
570 }, \
571 .oname = onam, \
572}
573#endif
2e610050 574
5c55ff99 575/* SPR load/store helpers */
636aa200 576static inline void gen_load_spr(TCGv t, int reg)
5c55ff99 577{
1328c2bf 578 tcg_gen_ld_tl(t, cpu_env, offsetof(CPUPPCState, spr[reg]));
5c55ff99 579}
2e610050 580
636aa200 581static inline void gen_store_spr(int reg, TCGv t)
5c55ff99 582{
1328c2bf 583 tcg_gen_st_tl(t, cpu_env, offsetof(CPUPPCState, spr[reg]));
5c55ff99 584}
2e610050 585
54623277 586/* Invalid instruction */
99e300ef 587static void gen_invalid(DisasContext *ctx)
9a64fbe4 588{
e06fcd75 589 gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL);
9a64fbe4
FB
590}
591
c227f099 592static opc_handler_t invalid_handler = {
70560da7
FC
593 .inval1 = 0xFFFFFFFF,
594 .inval2 = 0xFFFFFFFF,
9a64fbe4 595 .type = PPC_NONE,
a5858d7a 596 .type2 = PPC_NONE,
79aceca5
FB
597 .handler = gen_invalid,
598};
599
e1571908
AJ
600/*** Integer comparison ***/
601
636aa200 602static inline void gen_op_cmp(TCGv arg0, TCGv arg1, int s, int crf)
e1571908 603{
2fdcb629
RH
604 TCGv t0 = tcg_temp_new();
605 TCGv_i32 t1 = tcg_temp_new_i32();
e1571908 606
da91a00f 607 tcg_gen_trunc_tl_i32(cpu_crf[crf], cpu_so);
e1571908 608
2fdcb629
RH
609 tcg_gen_setcond_tl((s ? TCG_COND_LT: TCG_COND_LTU), t0, arg0, arg1);
610 tcg_gen_trunc_tl_i32(t1, t0);
611 tcg_gen_shli_i32(t1, t1, CRF_LT);
612 tcg_gen_or_i32(cpu_crf[crf], cpu_crf[crf], t1);
613
614 tcg_gen_setcond_tl((s ? TCG_COND_GT: TCG_COND_GTU), t0, arg0, arg1);
615 tcg_gen_trunc_tl_i32(t1, t0);
616 tcg_gen_shli_i32(t1, t1, CRF_GT);
617 tcg_gen_or_i32(cpu_crf[crf], cpu_crf[crf], t1);
618
619 tcg_gen_setcond_tl(TCG_COND_EQ, t0, arg0, arg1);
620 tcg_gen_trunc_tl_i32(t1, t0);
621 tcg_gen_shli_i32(t1, t1, CRF_EQ);
622 tcg_gen_or_i32(cpu_crf[crf], cpu_crf[crf], t1);
623
624 tcg_temp_free(t0);
625 tcg_temp_free_i32(t1);
e1571908
AJ
626}
627
636aa200 628static inline void gen_op_cmpi(TCGv arg0, target_ulong arg1, int s, int crf)
e1571908 629{
2fdcb629 630 TCGv t0 = tcg_const_tl(arg1);
ea363694
AJ
631 gen_op_cmp(arg0, t0, s, crf);
632 tcg_temp_free(t0);
e1571908
AJ
633}
634
636aa200 635static inline void gen_op_cmp32(TCGv arg0, TCGv arg1, int s, int crf)
e1571908 636{
ea363694 637 TCGv t0, t1;
2fdcb629
RH
638 t0 = tcg_temp_new();
639 t1 = tcg_temp_new();
e1571908 640 if (s) {
ea363694
AJ
641 tcg_gen_ext32s_tl(t0, arg0);
642 tcg_gen_ext32s_tl(t1, arg1);
e1571908 643 } else {
ea363694
AJ
644 tcg_gen_ext32u_tl(t0, arg0);
645 tcg_gen_ext32u_tl(t1, arg1);
e1571908 646 }
ea363694
AJ
647 gen_op_cmp(t0, t1, s, crf);
648 tcg_temp_free(t1);
649 tcg_temp_free(t0);
e1571908
AJ
650}
651
636aa200 652static inline void gen_op_cmpi32(TCGv arg0, target_ulong arg1, int s, int crf)
e1571908 653{
2fdcb629 654 TCGv t0 = tcg_const_tl(arg1);
ea363694
AJ
655 gen_op_cmp32(arg0, t0, s, crf);
656 tcg_temp_free(t0);
e1571908 657}
e1571908 658
636aa200 659static inline void gen_set_Rc0(DisasContext *ctx, TCGv reg)
e1571908 660{
02765534 661 if (NARROW_MODE(ctx)) {
e1571908 662 gen_op_cmpi32(reg, 0, 1, 0);
02765534 663 } else {
e1571908 664 gen_op_cmpi(reg, 0, 1, 0);
02765534 665 }
e1571908
AJ
666}
667
668/* cmp */
99e300ef 669static void gen_cmp(DisasContext *ctx)
e1571908 670{
02765534 671 if (NARROW_MODE(ctx) || !(ctx->opcode & 0x00200000)) {
e1571908
AJ
672 gen_op_cmp32(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rB(ctx->opcode)],
673 1, crfD(ctx->opcode));
02765534 674 } else {
e1571908
AJ
675 gen_op_cmp(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rB(ctx->opcode)],
676 1, crfD(ctx->opcode));
02765534 677 }
e1571908
AJ
678}
679
680/* cmpi */
99e300ef 681static void gen_cmpi(DisasContext *ctx)
e1571908 682{
02765534 683 if (NARROW_MODE(ctx) || !(ctx->opcode & 0x00200000)) {
e1571908
AJ
684 gen_op_cmpi32(cpu_gpr[rA(ctx->opcode)], SIMM(ctx->opcode),
685 1, crfD(ctx->opcode));
02765534 686 } else {
e1571908
AJ
687 gen_op_cmpi(cpu_gpr[rA(ctx->opcode)], SIMM(ctx->opcode),
688 1, crfD(ctx->opcode));
02765534 689 }
e1571908
AJ
690}
691
692/* cmpl */
99e300ef 693static void gen_cmpl(DisasContext *ctx)
e1571908 694{
02765534 695 if (NARROW_MODE(ctx) || !(ctx->opcode & 0x00200000)) {
e1571908
AJ
696 gen_op_cmp32(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rB(ctx->opcode)],
697 0, crfD(ctx->opcode));
02765534 698 } else {
e1571908
AJ
699 gen_op_cmp(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rB(ctx->opcode)],
700 0, crfD(ctx->opcode));
02765534 701 }
e1571908
AJ
702}
703
704/* cmpli */
99e300ef 705static void gen_cmpli(DisasContext *ctx)
e1571908 706{
02765534 707 if (NARROW_MODE(ctx) || !(ctx->opcode & 0x00200000)) {
e1571908
AJ
708 gen_op_cmpi32(cpu_gpr[rA(ctx->opcode)], UIMM(ctx->opcode),
709 0, crfD(ctx->opcode));
02765534 710 } else {
e1571908
AJ
711 gen_op_cmpi(cpu_gpr[rA(ctx->opcode)], UIMM(ctx->opcode),
712 0, crfD(ctx->opcode));
02765534 713 }
e1571908
AJ
714}
715
716/* isel (PowerPC 2.03 specification) */
99e300ef 717static void gen_isel(DisasContext *ctx)
e1571908
AJ
718{
719 int l1, l2;
720 uint32_t bi = rC(ctx->opcode);
721 uint32_t mask;
a7812ae4 722 TCGv_i32 t0;
e1571908
AJ
723
724 l1 = gen_new_label();
725 l2 = gen_new_label();
726
727 mask = 1 << (3 - (bi & 0x03));
a7812ae4 728 t0 = tcg_temp_new_i32();
fea0c503
AJ
729 tcg_gen_andi_i32(t0, cpu_crf[bi >> 2], mask);
730 tcg_gen_brcondi_i32(TCG_COND_EQ, t0, 0, l1);
e1571908
AJ
731 if (rA(ctx->opcode) == 0)
732 tcg_gen_movi_tl(cpu_gpr[rD(ctx->opcode)], 0);
733 else
734 tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)]);
735 tcg_gen_br(l2);
736 gen_set_label(l1);
737 tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rB(ctx->opcode)]);
738 gen_set_label(l2);
a7812ae4 739 tcg_temp_free_i32(t0);
e1571908
AJ
740}
741
fcfda20f
AJ
742/* cmpb: PowerPC 2.05 specification */
743static void gen_cmpb(DisasContext *ctx)
744{
745 gen_helper_cmpb(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)],
746 cpu_gpr[rB(ctx->opcode)]);
747}
748
79aceca5 749/*** Integer arithmetic ***/
79aceca5 750
636aa200
BS
751static inline void gen_op_arith_compute_ov(DisasContext *ctx, TCGv arg0,
752 TCGv arg1, TCGv arg2, int sub)
74637406 753{
ffe30937 754 TCGv t0 = tcg_temp_new();
79aceca5 755
8e7a6db9 756 tcg_gen_xor_tl(cpu_ov, arg0, arg2);
74637406 757 tcg_gen_xor_tl(t0, arg1, arg2);
ffe30937
RH
758 if (sub) {
759 tcg_gen_and_tl(cpu_ov, cpu_ov, t0);
760 } else {
761 tcg_gen_andc_tl(cpu_ov, cpu_ov, t0);
762 }
763 tcg_temp_free(t0);
02765534 764 if (NARROW_MODE(ctx)) {
ffe30937
RH
765 tcg_gen_ext32s_tl(cpu_ov, cpu_ov);
766 }
ffe30937
RH
767 tcg_gen_shri_tl(cpu_ov, cpu_ov, TARGET_LONG_BITS - 1);
768 tcg_gen_or_tl(cpu_so, cpu_so, cpu_ov);
79aceca5
FB
769}
770
74637406 771/* Common add function */
636aa200 772static inline void gen_op_arith_add(DisasContext *ctx, TCGv ret, TCGv arg1,
b5a73f8d
RH
773 TCGv arg2, bool add_ca, bool compute_ca,
774 bool compute_ov, bool compute_rc0)
74637406 775{
b5a73f8d 776 TCGv t0 = ret;
d9bce9d9 777
752d634e 778 if (compute_ca || compute_ov) {
146de60d 779 t0 = tcg_temp_new();
74637406 780 }
79aceca5 781
da91a00f 782 if (compute_ca) {
79482e5a 783 if (NARROW_MODE(ctx)) {
752d634e
RH
784 /* Caution: a non-obvious corner case of the spec is that we
785 must produce the *entire* 64-bit addition, but produce the
786 carry into bit 32. */
79482e5a 787 TCGv t1 = tcg_temp_new();
752d634e
RH
788 tcg_gen_xor_tl(t1, arg1, arg2); /* add without carry */
789 tcg_gen_add_tl(t0, arg1, arg2);
79482e5a
RH
790 if (add_ca) {
791 tcg_gen_add_tl(t0, t0, cpu_ca);
792 }
752d634e
RH
793 tcg_gen_xor_tl(cpu_ca, t0, t1); /* bits changed w/ carry */
794 tcg_temp_free(t1);
795 tcg_gen_shri_tl(cpu_ca, cpu_ca, 32); /* extract bit 32 */
796 tcg_gen_andi_tl(cpu_ca, cpu_ca, 1);
b5a73f8d 797 } else {
79482e5a
RH
798 TCGv zero = tcg_const_tl(0);
799 if (add_ca) {
800 tcg_gen_add2_tl(t0, cpu_ca, arg1, zero, cpu_ca, zero);
801 tcg_gen_add2_tl(t0, cpu_ca, t0, cpu_ca, arg2, zero);
802 } else {
803 tcg_gen_add2_tl(t0, cpu_ca, arg1, zero, arg2, zero);
804 }
805 tcg_temp_free(zero);
b5a73f8d 806 }
b5a73f8d
RH
807 } else {
808 tcg_gen_add_tl(t0, arg1, arg2);
809 if (add_ca) {
810 tcg_gen_add_tl(t0, t0, cpu_ca);
811 }
da91a00f 812 }
79aceca5 813
74637406
AJ
814 if (compute_ov) {
815 gen_op_arith_compute_ov(ctx, t0, arg1, arg2, 0);
816 }
b5a73f8d 817 if (unlikely(compute_rc0)) {
74637406 818 gen_set_Rc0(ctx, t0);
b5a73f8d 819 }
74637406 820
a7812ae4 821 if (!TCGV_EQUAL(t0, ret)) {
74637406
AJ
822 tcg_gen_mov_tl(ret, t0);
823 tcg_temp_free(t0);
824 }
39dd32ee 825}
74637406
AJ
826/* Add functions with two operands */
827#define GEN_INT_ARITH_ADD(name, opc3, add_ca, compute_ca, compute_ov) \
b5a73f8d 828static void glue(gen_, name)(DisasContext *ctx) \
74637406
AJ
829{ \
830 gen_op_arith_add(ctx, cpu_gpr[rD(ctx->opcode)], \
831 cpu_gpr[rA(ctx->opcode)], cpu_gpr[rB(ctx->opcode)], \
b5a73f8d 832 add_ca, compute_ca, compute_ov, Rc(ctx->opcode)); \
74637406
AJ
833}
834/* Add functions with one operand and one immediate */
835#define GEN_INT_ARITH_ADD_CONST(name, opc3, const_val, \
836 add_ca, compute_ca, compute_ov) \
b5a73f8d 837static void glue(gen_, name)(DisasContext *ctx) \
74637406 838{ \
b5a73f8d 839 TCGv t0 = tcg_const_tl(const_val); \
74637406
AJ
840 gen_op_arith_add(ctx, cpu_gpr[rD(ctx->opcode)], \
841 cpu_gpr[rA(ctx->opcode)], t0, \
b5a73f8d 842 add_ca, compute_ca, compute_ov, Rc(ctx->opcode)); \
74637406
AJ
843 tcg_temp_free(t0); \
844}
845
846/* add add. addo addo. */
847GEN_INT_ARITH_ADD(add, 0x08, 0, 0, 0)
848GEN_INT_ARITH_ADD(addo, 0x18, 0, 0, 1)
849/* addc addc. addco addco. */
850GEN_INT_ARITH_ADD(addc, 0x00, 0, 1, 0)
851GEN_INT_ARITH_ADD(addco, 0x10, 0, 1, 1)
852/* adde adde. addeo addeo. */
853GEN_INT_ARITH_ADD(adde, 0x04, 1, 1, 0)
854GEN_INT_ARITH_ADD(addeo, 0x14, 1, 1, 1)
855/* addme addme. addmeo addmeo. */
856GEN_INT_ARITH_ADD_CONST(addme, 0x07, -1LL, 1, 1, 0)
857GEN_INT_ARITH_ADD_CONST(addmeo, 0x17, -1LL, 1, 1, 1)
858/* addze addze. addzeo addzeo.*/
859GEN_INT_ARITH_ADD_CONST(addze, 0x06, 0, 1, 1, 0)
860GEN_INT_ARITH_ADD_CONST(addzeo, 0x16, 0, 1, 1, 1)
861/* addi */
99e300ef 862static void gen_addi(DisasContext *ctx)
d9bce9d9 863{
74637406
AJ
864 target_long simm = SIMM(ctx->opcode);
865
866 if (rA(ctx->opcode) == 0) {
867 /* li case */
868 tcg_gen_movi_tl(cpu_gpr[rD(ctx->opcode)], simm);
869 } else {
b5a73f8d
RH
870 tcg_gen_addi_tl(cpu_gpr[rD(ctx->opcode)],
871 cpu_gpr[rA(ctx->opcode)], simm);
74637406 872 }
d9bce9d9 873}
74637406 874/* addic addic.*/
b5a73f8d 875static inline void gen_op_addic(DisasContext *ctx, bool compute_rc0)
d9bce9d9 876{
b5a73f8d
RH
877 TCGv c = tcg_const_tl(SIMM(ctx->opcode));
878 gen_op_arith_add(ctx, cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)],
879 c, 0, 1, 0, compute_rc0);
880 tcg_temp_free(c);
d9bce9d9 881}
99e300ef
BS
882
883static void gen_addic(DisasContext *ctx)
d9bce9d9 884{
b5a73f8d 885 gen_op_addic(ctx, 0);
d9bce9d9 886}
e8eaa2c0
BS
887
888static void gen_addic_(DisasContext *ctx)
d9bce9d9 889{
b5a73f8d 890 gen_op_addic(ctx, 1);
d9bce9d9 891}
99e300ef 892
54623277 893/* addis */
99e300ef 894static void gen_addis(DisasContext *ctx)
d9bce9d9 895{
74637406
AJ
896 target_long simm = SIMM(ctx->opcode);
897
898 if (rA(ctx->opcode) == 0) {
899 /* lis case */
900 tcg_gen_movi_tl(cpu_gpr[rD(ctx->opcode)], simm << 16);
901 } else {
b5a73f8d
RH
902 tcg_gen_addi_tl(cpu_gpr[rD(ctx->opcode)],
903 cpu_gpr[rA(ctx->opcode)], simm << 16);
74637406 904 }
d9bce9d9 905}
74637406 906
636aa200
BS
907static inline void gen_op_arith_divw(DisasContext *ctx, TCGv ret, TCGv arg1,
908 TCGv arg2, int sign, int compute_ov)
d9bce9d9 909{
2ef1b120
AJ
910 int l1 = gen_new_label();
911 int l2 = gen_new_label();
a7812ae4
PB
912 TCGv_i32 t0 = tcg_temp_local_new_i32();
913 TCGv_i32 t1 = tcg_temp_local_new_i32();
74637406 914
2ef1b120
AJ
915 tcg_gen_trunc_tl_i32(t0, arg1);
916 tcg_gen_trunc_tl_i32(t1, arg2);
917 tcg_gen_brcondi_i32(TCG_COND_EQ, t1, 0, l1);
74637406 918 if (sign) {
2ef1b120
AJ
919 int l3 = gen_new_label();
920 tcg_gen_brcondi_i32(TCG_COND_NE, t1, -1, l3);
921 tcg_gen_brcondi_i32(TCG_COND_EQ, t0, INT32_MIN, l1);
74637406 922 gen_set_label(l3);
2ef1b120 923 tcg_gen_div_i32(t0, t0, t1);
74637406 924 } else {
2ef1b120 925 tcg_gen_divu_i32(t0, t0, t1);
74637406
AJ
926 }
927 if (compute_ov) {
da91a00f 928 tcg_gen_movi_tl(cpu_ov, 0);
74637406
AJ
929 }
930 tcg_gen_br(l2);
931 gen_set_label(l1);
932 if (sign) {
2ef1b120 933 tcg_gen_sari_i32(t0, t0, 31);
74637406
AJ
934 } else {
935 tcg_gen_movi_i32(t0, 0);
936 }
937 if (compute_ov) {
da91a00f
RH
938 tcg_gen_movi_tl(cpu_ov, 1);
939 tcg_gen_movi_tl(cpu_so, 1);
74637406
AJ
940 }
941 gen_set_label(l2);
2ef1b120 942 tcg_gen_extu_i32_tl(ret, t0);
a7812ae4
PB
943 tcg_temp_free_i32(t0);
944 tcg_temp_free_i32(t1);
74637406
AJ
945 if (unlikely(Rc(ctx->opcode) != 0))
946 gen_set_Rc0(ctx, ret);
d9bce9d9 947}
74637406
AJ
948/* Div functions */
949#define GEN_INT_ARITH_DIVW(name, opc3, sign, compute_ov) \
99e300ef 950static void glue(gen_, name)(DisasContext *ctx) \
74637406
AJ
951{ \
952 gen_op_arith_divw(ctx, cpu_gpr[rD(ctx->opcode)], \
953 cpu_gpr[rA(ctx->opcode)], cpu_gpr[rB(ctx->opcode)], \
954 sign, compute_ov); \
955}
956/* divwu divwu. divwuo divwuo. */
957GEN_INT_ARITH_DIVW(divwu, 0x0E, 0, 0);
958GEN_INT_ARITH_DIVW(divwuo, 0x1E, 0, 1);
959/* divw divw. divwo divwo. */
960GEN_INT_ARITH_DIVW(divw, 0x0F, 1, 0);
961GEN_INT_ARITH_DIVW(divwo, 0x1F, 1, 1);
d9bce9d9 962#if defined(TARGET_PPC64)
636aa200
BS
963static inline void gen_op_arith_divd(DisasContext *ctx, TCGv ret, TCGv arg1,
964 TCGv arg2, int sign, int compute_ov)
d9bce9d9 965{
2ef1b120
AJ
966 int l1 = gen_new_label();
967 int l2 = gen_new_label();
74637406
AJ
968
969 tcg_gen_brcondi_i64(TCG_COND_EQ, arg2, 0, l1);
970 if (sign) {
2ef1b120 971 int l3 = gen_new_label();
74637406
AJ
972 tcg_gen_brcondi_i64(TCG_COND_NE, arg2, -1, l3);
973 tcg_gen_brcondi_i64(TCG_COND_EQ, arg1, INT64_MIN, l1);
974 gen_set_label(l3);
74637406
AJ
975 tcg_gen_div_i64(ret, arg1, arg2);
976 } else {
977 tcg_gen_divu_i64(ret, arg1, arg2);
978 }
979 if (compute_ov) {
da91a00f 980 tcg_gen_movi_tl(cpu_ov, 0);
74637406
AJ
981 }
982 tcg_gen_br(l2);
983 gen_set_label(l1);
984 if (sign) {
985 tcg_gen_sari_i64(ret, arg1, 63);
986 } else {
987 tcg_gen_movi_i64(ret, 0);
988 }
989 if (compute_ov) {
da91a00f
RH
990 tcg_gen_movi_tl(cpu_ov, 1);
991 tcg_gen_movi_tl(cpu_so, 1);
74637406
AJ
992 }
993 gen_set_label(l2);
994 if (unlikely(Rc(ctx->opcode) != 0))
995 gen_set_Rc0(ctx, ret);
d9bce9d9 996}
74637406 997#define GEN_INT_ARITH_DIVD(name, opc3, sign, compute_ov) \
99e300ef 998static void glue(gen_, name)(DisasContext *ctx) \
74637406 999{ \
2ef1b120
AJ
1000 gen_op_arith_divd(ctx, cpu_gpr[rD(ctx->opcode)], \
1001 cpu_gpr[rA(ctx->opcode)], cpu_gpr[rB(ctx->opcode)], \
1002 sign, compute_ov); \
74637406
AJ
1003}
1004/* divwu divwu. divwuo divwuo. */
1005GEN_INT_ARITH_DIVD(divdu, 0x0E, 0, 0);
1006GEN_INT_ARITH_DIVD(divduo, 0x1E, 0, 1);
1007/* divw divw. divwo divwo. */
1008GEN_INT_ARITH_DIVD(divd, 0x0F, 1, 0);
1009GEN_INT_ARITH_DIVD(divdo, 0x1F, 1, 1);
d9bce9d9 1010#endif
74637406
AJ
1011
1012/* mulhw mulhw. */
99e300ef 1013static void gen_mulhw(DisasContext *ctx)
d9bce9d9 1014{
23ad1d5d
RH
1015 TCGv_i32 t0 = tcg_temp_new_i32();
1016 TCGv_i32 t1 = tcg_temp_new_i32();
74637406 1017
23ad1d5d
RH
1018 tcg_gen_trunc_tl_i32(t0, cpu_gpr[rA(ctx->opcode)]);
1019 tcg_gen_trunc_tl_i32(t1, cpu_gpr[rB(ctx->opcode)]);
1020 tcg_gen_muls2_i32(t0, t1, t0, t1);
1021 tcg_gen_extu_i32_tl(cpu_gpr[rD(ctx->opcode)], t1);
1022 tcg_temp_free_i32(t0);
1023 tcg_temp_free_i32(t1);
74637406
AJ
1024 if (unlikely(Rc(ctx->opcode) != 0))
1025 gen_set_Rc0(ctx, cpu_gpr[rD(ctx->opcode)]);
d9bce9d9 1026}
99e300ef 1027
54623277 1028/* mulhwu mulhwu. */
99e300ef 1029static void gen_mulhwu(DisasContext *ctx)
d9bce9d9 1030{
23ad1d5d
RH
1031 TCGv_i32 t0 = tcg_temp_new_i32();
1032 TCGv_i32 t1 = tcg_temp_new_i32();
74637406 1033
23ad1d5d
RH
1034 tcg_gen_trunc_tl_i32(t0, cpu_gpr[rA(ctx->opcode)]);
1035 tcg_gen_trunc_tl_i32(t1, cpu_gpr[rB(ctx->opcode)]);
1036 tcg_gen_mulu2_i32(t0, t1, t0, t1);
1037 tcg_gen_extu_i32_tl(cpu_gpr[rD(ctx->opcode)], t1);
1038 tcg_temp_free_i32(t0);
1039 tcg_temp_free_i32(t1);
74637406
AJ
1040 if (unlikely(Rc(ctx->opcode) != 0))
1041 gen_set_Rc0(ctx, cpu_gpr[rD(ctx->opcode)]);
d9bce9d9 1042}
99e300ef 1043
54623277 1044/* mullw mullw. */
99e300ef 1045static void gen_mullw(DisasContext *ctx)
d9bce9d9 1046{
74637406
AJ
1047 tcg_gen_mul_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)],
1048 cpu_gpr[rB(ctx->opcode)]);
1e4c090f 1049 tcg_gen_ext32s_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rD(ctx->opcode)]);
74637406
AJ
1050 if (unlikely(Rc(ctx->opcode) != 0))
1051 gen_set_Rc0(ctx, cpu_gpr[rD(ctx->opcode)]);
d9bce9d9 1052}
99e300ef 1053
54623277 1054/* mullwo mullwo. */
99e300ef 1055static void gen_mullwo(DisasContext *ctx)
d9bce9d9 1056{
e4a2c846
RH
1057 TCGv_i32 t0 = tcg_temp_new_i32();
1058 TCGv_i32 t1 = tcg_temp_new_i32();
74637406 1059
e4a2c846
RH
1060 tcg_gen_trunc_tl_i32(t0, cpu_gpr[rA(ctx->opcode)]);
1061 tcg_gen_trunc_tl_i32(t1, cpu_gpr[rB(ctx->opcode)]);
1062 tcg_gen_muls2_i32(t0, t1, t0, t1);
1063 tcg_gen_ext_i32_tl(cpu_gpr[rD(ctx->opcode)], t0);
1064
1065 tcg_gen_sari_i32(t0, t0, 31);
1066 tcg_gen_setcond_i32(TCG_COND_NE, t0, t0, t1);
1067 tcg_gen_extu_i32_tl(cpu_ov, t0);
1068 tcg_gen_or_tl(cpu_so, cpu_so, cpu_ov);
1069
1070 tcg_temp_free_i32(t0);
1071 tcg_temp_free_i32(t1);
74637406
AJ
1072 if (unlikely(Rc(ctx->opcode) != 0))
1073 gen_set_Rc0(ctx, cpu_gpr[rD(ctx->opcode)]);
d9bce9d9 1074}
99e300ef 1075
54623277 1076/* mulli */
99e300ef 1077static void gen_mulli(DisasContext *ctx)
d9bce9d9 1078{
74637406
AJ
1079 tcg_gen_muli_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)],
1080 SIMM(ctx->opcode));
d9bce9d9 1081}
23ad1d5d 1082
d9bce9d9 1083#if defined(TARGET_PPC64)
74637406 1084/* mulhd mulhd. */
23ad1d5d
RH
1085static void gen_mulhd(DisasContext *ctx)
1086{
1087 TCGv lo = tcg_temp_new();
1088 tcg_gen_muls2_tl(lo, cpu_gpr[rD(ctx->opcode)],
1089 cpu_gpr[rA(ctx->opcode)], cpu_gpr[rB(ctx->opcode)]);
1090 tcg_temp_free(lo);
1091 if (unlikely(Rc(ctx->opcode) != 0)) {
1092 gen_set_Rc0(ctx, cpu_gpr[rD(ctx->opcode)]);
1093 }
1094}
1095
74637406 1096/* mulhdu mulhdu. */
23ad1d5d
RH
1097static void gen_mulhdu(DisasContext *ctx)
1098{
1099 TCGv lo = tcg_temp_new();
1100 tcg_gen_mulu2_tl(lo, cpu_gpr[rD(ctx->opcode)],
1101 cpu_gpr[rA(ctx->opcode)], cpu_gpr[rB(ctx->opcode)]);
1102 tcg_temp_free(lo);
1103 if (unlikely(Rc(ctx->opcode) != 0)) {
1104 gen_set_Rc0(ctx, cpu_gpr[rD(ctx->opcode)]);
1105 }
1106}
99e300ef 1107
54623277 1108/* mulld mulld. */
99e300ef 1109static void gen_mulld(DisasContext *ctx)
d9bce9d9 1110{
74637406
AJ
1111 tcg_gen_mul_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)],
1112 cpu_gpr[rB(ctx->opcode)]);
1113 if (unlikely(Rc(ctx->opcode) != 0))
1114 gen_set_Rc0(ctx, cpu_gpr[rD(ctx->opcode)]);
d9bce9d9 1115}
d15f74fb 1116
74637406 1117/* mulldo mulldo. */
d15f74fb
BS
1118static void gen_mulldo(DisasContext *ctx)
1119{
1120 gen_helper_mulldo(cpu_gpr[rD(ctx->opcode)], cpu_env,
1121 cpu_gpr[rA(ctx->opcode)], cpu_gpr[rB(ctx->opcode)]);
1122 if (unlikely(Rc(ctx->opcode) != 0)) {
1123 gen_set_Rc0(ctx, cpu_gpr[rD(ctx->opcode)]);
1124 }
1125}
d9bce9d9 1126#endif
74637406 1127
74637406 1128/* Common subf function */
636aa200 1129static inline void gen_op_arith_subf(DisasContext *ctx, TCGv ret, TCGv arg1,
b5a73f8d
RH
1130 TCGv arg2, bool add_ca, bool compute_ca,
1131 bool compute_ov, bool compute_rc0)
79aceca5 1132{
b5a73f8d 1133 TCGv t0 = ret;
79aceca5 1134
752d634e 1135 if (compute_ca || compute_ov) {
b5a73f8d 1136 t0 = tcg_temp_new();
da91a00f 1137 }
74637406 1138
79482e5a
RH
1139 if (compute_ca) {
1140 /* dest = ~arg1 + arg2 [+ ca]. */
1141 if (NARROW_MODE(ctx)) {
752d634e
RH
1142 /* Caution: a non-obvious corner case of the spec is that we
1143 must produce the *entire* 64-bit addition, but produce the
1144 carry into bit 32. */
79482e5a 1145 TCGv inv1 = tcg_temp_new();
752d634e 1146 TCGv t1 = tcg_temp_new();
79482e5a 1147 tcg_gen_not_tl(inv1, arg1);
79482e5a 1148 if (add_ca) {
752d634e 1149 tcg_gen_add_tl(t0, arg2, cpu_ca);
79482e5a 1150 } else {
752d634e 1151 tcg_gen_addi_tl(t0, arg2, 1);
79482e5a 1152 }
752d634e 1153 tcg_gen_xor_tl(t1, arg2, inv1); /* add without carry */
79482e5a 1154 tcg_gen_add_tl(t0, t0, inv1);
752d634e
RH
1155 tcg_gen_xor_tl(cpu_ca, t0, t1); /* bits changes w/ carry */
1156 tcg_temp_free(t1);
1157 tcg_gen_shri_tl(cpu_ca, cpu_ca, 32); /* extract bit 32 */
1158 tcg_gen_andi_tl(cpu_ca, cpu_ca, 1);
79482e5a 1159 } else if (add_ca) {
08f4a0f7
RH
1160 TCGv zero, inv1 = tcg_temp_new();
1161 tcg_gen_not_tl(inv1, arg1);
b5a73f8d
RH
1162 zero = tcg_const_tl(0);
1163 tcg_gen_add2_tl(t0, cpu_ca, arg2, zero, cpu_ca, zero);
08f4a0f7 1164 tcg_gen_add2_tl(t0, cpu_ca, t0, cpu_ca, inv1, zero);
b5a73f8d 1165 tcg_temp_free(zero);
08f4a0f7 1166 tcg_temp_free(inv1);
b5a73f8d 1167 } else {
79482e5a 1168 tcg_gen_setcond_tl(TCG_COND_GEU, cpu_ca, arg2, arg1);
b5a73f8d 1169 tcg_gen_sub_tl(t0, arg2, arg1);
b5a73f8d 1170 }
79482e5a
RH
1171 } else if (add_ca) {
1172 /* Since we're ignoring carry-out, we can simplify the
1173 standard ~arg1 + arg2 + ca to arg2 - arg1 + ca - 1. */
1174 tcg_gen_sub_tl(t0, arg2, arg1);
1175 tcg_gen_add_tl(t0, t0, cpu_ca);
1176 tcg_gen_subi_tl(t0, t0, 1);
79aceca5 1177 } else {
b5a73f8d 1178 tcg_gen_sub_tl(t0, arg2, arg1);
74637406 1179 }
b5a73f8d 1180
74637406
AJ
1181 if (compute_ov) {
1182 gen_op_arith_compute_ov(ctx, t0, arg1, arg2, 1);
1183 }
b5a73f8d 1184 if (unlikely(compute_rc0)) {
74637406 1185 gen_set_Rc0(ctx, t0);
b5a73f8d 1186 }
74637406 1187
a7812ae4 1188 if (!TCGV_EQUAL(t0, ret)) {
74637406
AJ
1189 tcg_gen_mov_tl(ret, t0);
1190 tcg_temp_free(t0);
79aceca5 1191 }
79aceca5 1192}
74637406
AJ
1193/* Sub functions with Two operands functions */
1194#define GEN_INT_ARITH_SUBF(name, opc3, add_ca, compute_ca, compute_ov) \
b5a73f8d 1195static void glue(gen_, name)(DisasContext *ctx) \
74637406
AJ
1196{ \
1197 gen_op_arith_subf(ctx, cpu_gpr[rD(ctx->opcode)], \
1198 cpu_gpr[rA(ctx->opcode)], cpu_gpr[rB(ctx->opcode)], \
b5a73f8d 1199 add_ca, compute_ca, compute_ov, Rc(ctx->opcode)); \
74637406
AJ
1200}
1201/* Sub functions with one operand and one immediate */
1202#define GEN_INT_ARITH_SUBF_CONST(name, opc3, const_val, \
1203 add_ca, compute_ca, compute_ov) \
b5a73f8d 1204static void glue(gen_, name)(DisasContext *ctx) \
74637406 1205{ \
b5a73f8d 1206 TCGv t0 = tcg_const_tl(const_val); \
74637406
AJ
1207 gen_op_arith_subf(ctx, cpu_gpr[rD(ctx->opcode)], \
1208 cpu_gpr[rA(ctx->opcode)], t0, \
b5a73f8d 1209 add_ca, compute_ca, compute_ov, Rc(ctx->opcode)); \
74637406
AJ
1210 tcg_temp_free(t0); \
1211}
1212/* subf subf. subfo subfo. */
1213GEN_INT_ARITH_SUBF(subf, 0x01, 0, 0, 0)
1214GEN_INT_ARITH_SUBF(subfo, 0x11, 0, 0, 1)
1215/* subfc subfc. subfco subfco. */
1216GEN_INT_ARITH_SUBF(subfc, 0x00, 0, 1, 0)
1217GEN_INT_ARITH_SUBF(subfco, 0x10, 0, 1, 1)
1218/* subfe subfe. subfeo subfo. */
1219GEN_INT_ARITH_SUBF(subfe, 0x04, 1, 1, 0)
1220GEN_INT_ARITH_SUBF(subfeo, 0x14, 1, 1, 1)
1221/* subfme subfme. subfmeo subfmeo. */
1222GEN_INT_ARITH_SUBF_CONST(subfme, 0x07, -1LL, 1, 1, 0)
1223GEN_INT_ARITH_SUBF_CONST(subfmeo, 0x17, -1LL, 1, 1, 1)
1224/* subfze subfze. subfzeo subfzeo.*/
1225GEN_INT_ARITH_SUBF_CONST(subfze, 0x06, 0, 1, 1, 0)
1226GEN_INT_ARITH_SUBF_CONST(subfzeo, 0x16, 0, 1, 1, 1)
99e300ef 1227
54623277 1228/* subfic */
99e300ef 1229static void gen_subfic(DisasContext *ctx)
79aceca5 1230{
b5a73f8d
RH
1231 TCGv c = tcg_const_tl(SIMM(ctx->opcode));
1232 gen_op_arith_subf(ctx, cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)],
1233 c, 0, 1, 0, 0);
1234 tcg_temp_free(c);
79aceca5
FB
1235}
1236
fd3f0081
RH
1237/* neg neg. nego nego. */
1238static inline void gen_op_arith_neg(DisasContext *ctx, bool compute_ov)
1239{
1240 TCGv zero = tcg_const_tl(0);
1241 gen_op_arith_subf(ctx, cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)],
1242 zero, 0, 0, compute_ov, Rc(ctx->opcode));
1243 tcg_temp_free(zero);
1244}
1245
1246static void gen_neg(DisasContext *ctx)
1247{
1248 gen_op_arith_neg(ctx, 0);
1249}
1250
1251static void gen_nego(DisasContext *ctx)
1252{
1253 gen_op_arith_neg(ctx, 1);
1254}
1255
79aceca5 1256/*** Integer logical ***/
26d67362 1257#define GEN_LOGICAL2(name, tcg_op, opc, type) \
99e300ef 1258static void glue(gen_, name)(DisasContext *ctx) \
79aceca5 1259{ \
26d67362
AJ
1260 tcg_op(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)], \
1261 cpu_gpr[rB(ctx->opcode)]); \
76a66253 1262 if (unlikely(Rc(ctx->opcode) != 0)) \
26d67362 1263 gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]); \
79aceca5 1264}
79aceca5 1265
26d67362 1266#define GEN_LOGICAL1(name, tcg_op, opc, type) \
99e300ef 1267static void glue(gen_, name)(DisasContext *ctx) \
79aceca5 1268{ \
26d67362 1269 tcg_op(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)]); \
76a66253 1270 if (unlikely(Rc(ctx->opcode) != 0)) \
26d67362 1271 gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]); \
79aceca5
FB
1272}
1273
1274/* and & and. */
26d67362 1275GEN_LOGICAL2(and, tcg_gen_and_tl, 0x00, PPC_INTEGER);
79aceca5 1276/* andc & andc. */
26d67362 1277GEN_LOGICAL2(andc, tcg_gen_andc_tl, 0x01, PPC_INTEGER);
e8eaa2c0 1278
54623277 1279/* andi. */
e8eaa2c0 1280static void gen_andi_(DisasContext *ctx)
79aceca5 1281{
26d67362
AJ
1282 tcg_gen_andi_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)], UIMM(ctx->opcode));
1283 gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
79aceca5 1284}
e8eaa2c0 1285
54623277 1286/* andis. */
e8eaa2c0 1287static void gen_andis_(DisasContext *ctx)
79aceca5 1288{
26d67362
AJ
1289 tcg_gen_andi_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)], UIMM(ctx->opcode) << 16);
1290 gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
79aceca5 1291}
99e300ef 1292
54623277 1293/* cntlzw */
99e300ef 1294static void gen_cntlzw(DisasContext *ctx)
26d67362 1295{
a7812ae4 1296 gen_helper_cntlzw(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)]);
26d67362 1297 if (unlikely(Rc(ctx->opcode) != 0))
2e31f5d3 1298 gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
26d67362 1299}
79aceca5 1300/* eqv & eqv. */
26d67362 1301GEN_LOGICAL2(eqv, tcg_gen_eqv_tl, 0x08, PPC_INTEGER);
79aceca5 1302/* extsb & extsb. */
26d67362 1303GEN_LOGICAL1(extsb, tcg_gen_ext8s_tl, 0x1D, PPC_INTEGER);
79aceca5 1304/* extsh & extsh. */
26d67362 1305GEN_LOGICAL1(extsh, tcg_gen_ext16s_tl, 0x1C, PPC_INTEGER);
79aceca5 1306/* nand & nand. */
26d67362 1307GEN_LOGICAL2(nand, tcg_gen_nand_tl, 0x0E, PPC_INTEGER);
79aceca5 1308/* nor & nor. */
26d67362 1309GEN_LOGICAL2(nor, tcg_gen_nor_tl, 0x03, PPC_INTEGER);
99e300ef 1310
54623277 1311/* or & or. */
99e300ef 1312static void gen_or(DisasContext *ctx)
9a64fbe4 1313{
76a66253
JM
1314 int rs, ra, rb;
1315
1316 rs = rS(ctx->opcode);
1317 ra = rA(ctx->opcode);
1318 rb = rB(ctx->opcode);
1319 /* Optimisation for mr. ri case */
1320 if (rs != ra || rs != rb) {
26d67362
AJ
1321 if (rs != rb)
1322 tcg_gen_or_tl(cpu_gpr[ra], cpu_gpr[rs], cpu_gpr[rb]);
1323 else
1324 tcg_gen_mov_tl(cpu_gpr[ra], cpu_gpr[rs]);
76a66253 1325 if (unlikely(Rc(ctx->opcode) != 0))
26d67362 1326 gen_set_Rc0(ctx, cpu_gpr[ra]);
76a66253 1327 } else if (unlikely(Rc(ctx->opcode) != 0)) {
26d67362 1328 gen_set_Rc0(ctx, cpu_gpr[rs]);
c80f84e3
JM
1329#if defined(TARGET_PPC64)
1330 } else {
26d67362
AJ
1331 int prio = 0;
1332
c80f84e3
JM
1333 switch (rs) {
1334 case 1:
1335 /* Set process priority to low */
26d67362 1336 prio = 2;
c80f84e3
JM
1337 break;
1338 case 6:
1339 /* Set process priority to medium-low */
26d67362 1340 prio = 3;
c80f84e3
JM
1341 break;
1342 case 2:
1343 /* Set process priority to normal */
26d67362 1344 prio = 4;
c80f84e3 1345 break;
be147d08
JM
1346#if !defined(CONFIG_USER_ONLY)
1347 case 31:
76db3ba4 1348 if (ctx->mem_idx > 0) {
be147d08 1349 /* Set process priority to very low */
26d67362 1350 prio = 1;
be147d08
JM
1351 }
1352 break;
1353 case 5:
76db3ba4 1354 if (ctx->mem_idx > 0) {
be147d08 1355 /* Set process priority to medium-hight */
26d67362 1356 prio = 5;
be147d08
JM
1357 }
1358 break;
1359 case 3:
76db3ba4 1360 if (ctx->mem_idx > 0) {
be147d08 1361 /* Set process priority to high */
26d67362 1362 prio = 6;
be147d08
JM
1363 }
1364 break;
be147d08 1365 case 7:
76db3ba4 1366 if (ctx->mem_idx > 1) {
be147d08 1367 /* Set process priority to very high */
26d67362 1368 prio = 7;
be147d08
JM
1369 }
1370 break;
be147d08 1371#endif
c80f84e3
JM
1372 default:
1373 /* nop */
1374 break;
1375 }
26d67362 1376 if (prio) {
a7812ae4 1377 TCGv t0 = tcg_temp_new();
54cdcae6 1378 gen_load_spr(t0, SPR_PPR);
ea363694
AJ
1379 tcg_gen_andi_tl(t0, t0, ~0x001C000000000000ULL);
1380 tcg_gen_ori_tl(t0, t0, ((uint64_t)prio) << 50);
54cdcae6 1381 gen_store_spr(SPR_PPR, t0);
ea363694 1382 tcg_temp_free(t0);
26d67362 1383 }
c80f84e3 1384#endif
9a64fbe4 1385 }
9a64fbe4 1386}
79aceca5 1387/* orc & orc. */
26d67362 1388GEN_LOGICAL2(orc, tcg_gen_orc_tl, 0x0C, PPC_INTEGER);
99e300ef 1389
54623277 1390/* xor & xor. */
99e300ef 1391static void gen_xor(DisasContext *ctx)
9a64fbe4 1392{
9a64fbe4 1393 /* Optimisation for "set to zero" case */
26d67362 1394 if (rS(ctx->opcode) != rB(ctx->opcode))
312179c4 1395 tcg_gen_xor_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)], cpu_gpr[rB(ctx->opcode)]);
26d67362
AJ
1396 else
1397 tcg_gen_movi_tl(cpu_gpr[rA(ctx->opcode)], 0);
76a66253 1398 if (unlikely(Rc(ctx->opcode) != 0))
26d67362 1399 gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
9a64fbe4 1400}
99e300ef 1401
54623277 1402/* ori */
99e300ef 1403static void gen_ori(DisasContext *ctx)
79aceca5 1404{
76a66253 1405 target_ulong uimm = UIMM(ctx->opcode);
79aceca5 1406
9a64fbe4
FB
1407 if (rS(ctx->opcode) == rA(ctx->opcode) && uimm == 0) {
1408 /* NOP */
76a66253 1409 /* XXX: should handle special NOPs for POWER series */
9a64fbe4 1410 return;
76a66253 1411 }
26d67362 1412 tcg_gen_ori_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)], uimm);
79aceca5 1413}
99e300ef 1414
54623277 1415/* oris */
99e300ef 1416static void gen_oris(DisasContext *ctx)
79aceca5 1417{
76a66253 1418 target_ulong uimm = UIMM(ctx->opcode);
79aceca5 1419
9a64fbe4
FB
1420 if (rS(ctx->opcode) == rA(ctx->opcode) && uimm == 0) {
1421 /* NOP */
1422 return;
76a66253 1423 }
26d67362 1424 tcg_gen_ori_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)], uimm << 16);
79aceca5 1425}
99e300ef 1426
54623277 1427/* xori */
99e300ef 1428static void gen_xori(DisasContext *ctx)
79aceca5 1429{
76a66253 1430 target_ulong uimm = UIMM(ctx->opcode);
9a64fbe4
FB
1431
1432 if (rS(ctx->opcode) == rA(ctx->opcode) && uimm == 0) {
1433 /* NOP */
1434 return;
1435 }
26d67362 1436 tcg_gen_xori_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)], uimm);
79aceca5 1437}
99e300ef 1438
54623277 1439/* xoris */
99e300ef 1440static void gen_xoris(DisasContext *ctx)
79aceca5 1441{
76a66253 1442 target_ulong uimm = UIMM(ctx->opcode);
9a64fbe4
FB
1443
1444 if (rS(ctx->opcode) == rA(ctx->opcode) && uimm == 0) {
1445 /* NOP */
1446 return;
1447 }
26d67362 1448 tcg_gen_xori_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)], uimm << 16);
79aceca5 1449}
99e300ef 1450
54623277 1451/* popcntb : PowerPC 2.03 specification */
99e300ef 1452static void gen_popcntb(DisasContext *ctx)
d9bce9d9 1453{
eaabeef2
DG
1454 gen_helper_popcntb(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)]);
1455}
1456
1457static void gen_popcntw(DisasContext *ctx)
1458{
1459 gen_helper_popcntw(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)]);
1460}
1461
d9bce9d9 1462#if defined(TARGET_PPC64)
eaabeef2
DG
1463/* popcntd: PowerPC 2.06 specification */
1464static void gen_popcntd(DisasContext *ctx)
1465{
1466 gen_helper_popcntd(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)]);
d9bce9d9 1467}
eaabeef2 1468#endif
d9bce9d9
JM
1469
1470#if defined(TARGET_PPC64)
1471/* extsw & extsw. */
26d67362 1472GEN_LOGICAL1(extsw, tcg_gen_ext32s_tl, 0x1E, PPC_64B);
99e300ef 1473
54623277 1474/* cntlzd */
99e300ef 1475static void gen_cntlzd(DisasContext *ctx)
26d67362 1476{
a7812ae4 1477 gen_helper_cntlzd(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)]);
26d67362
AJ
1478 if (unlikely(Rc(ctx->opcode) != 0))
1479 gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
1480}
d9bce9d9
JM
1481#endif
1482
79aceca5 1483/*** Integer rotate ***/
99e300ef 1484
54623277 1485/* rlwimi & rlwimi. */
99e300ef 1486static void gen_rlwimi(DisasContext *ctx)
79aceca5 1487{
76a66253 1488 uint32_t mb, me, sh;
79aceca5
FB
1489
1490 mb = MB(ctx->opcode);
1491 me = ME(ctx->opcode);
76a66253 1492 sh = SH(ctx->opcode);
d03ef511
AJ
1493 if (likely(sh == 0 && mb == 0 && me == 31)) {
1494 tcg_gen_ext32u_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)]);
1495 } else {
d03ef511 1496 target_ulong mask;
a7812ae4
PB
1497 TCGv t1;
1498 TCGv t0 = tcg_temp_new();
54843a58 1499#if defined(TARGET_PPC64)
a7812ae4
PB
1500 TCGv_i32 t2 = tcg_temp_new_i32();
1501 tcg_gen_trunc_i64_i32(t2, cpu_gpr[rS(ctx->opcode)]);
1502 tcg_gen_rotli_i32(t2, t2, sh);
1503 tcg_gen_extu_i32_i64(t0, t2);
1504 tcg_temp_free_i32(t2);
54843a58
AJ
1505#else
1506 tcg_gen_rotli_i32(t0, cpu_gpr[rS(ctx->opcode)], sh);
1507#endif
76a66253 1508#if defined(TARGET_PPC64)
d03ef511
AJ
1509 mb += 32;
1510 me += 32;
76a66253 1511#endif
d03ef511 1512 mask = MASK(mb, me);
a7812ae4 1513 t1 = tcg_temp_new();
d03ef511
AJ
1514 tcg_gen_andi_tl(t0, t0, mask);
1515 tcg_gen_andi_tl(t1, cpu_gpr[rA(ctx->opcode)], ~mask);
1516 tcg_gen_or_tl(cpu_gpr[rA(ctx->opcode)], t0, t1);
1517 tcg_temp_free(t0);
1518 tcg_temp_free(t1);
1519 }
76a66253 1520 if (unlikely(Rc(ctx->opcode) != 0))
d03ef511 1521 gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
79aceca5 1522}
99e300ef 1523
54623277 1524/* rlwinm & rlwinm. */
99e300ef 1525static void gen_rlwinm(DisasContext *ctx)
79aceca5
FB
1526{
1527 uint32_t mb, me, sh;
3b46e624 1528
79aceca5
FB
1529 sh = SH(ctx->opcode);
1530 mb = MB(ctx->opcode);
1531 me = ME(ctx->opcode);
d03ef511
AJ
1532
1533 if (likely(mb == 0 && me == (31 - sh))) {
1534 if (likely(sh == 0)) {
1535 tcg_gen_ext32u_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)]);
1536 } else {
a7812ae4 1537 TCGv t0 = tcg_temp_new();
d03ef511
AJ
1538 tcg_gen_ext32u_tl(t0, cpu_gpr[rS(ctx->opcode)]);
1539 tcg_gen_shli_tl(t0, t0, sh);
1540 tcg_gen_ext32u_tl(cpu_gpr[rA(ctx->opcode)], t0);
1541 tcg_temp_free(t0);
79aceca5 1542 }
d03ef511 1543 } else if (likely(sh != 0 && me == 31 && sh == (32 - mb))) {
a7812ae4 1544 TCGv t0 = tcg_temp_new();
d03ef511
AJ
1545 tcg_gen_ext32u_tl(t0, cpu_gpr[rS(ctx->opcode)]);
1546 tcg_gen_shri_tl(t0, t0, mb);
1547 tcg_gen_ext32u_tl(cpu_gpr[rA(ctx->opcode)], t0);
1548 tcg_temp_free(t0);
1549 } else {
a7812ae4 1550 TCGv t0 = tcg_temp_new();
54843a58 1551#if defined(TARGET_PPC64)
a7812ae4 1552 TCGv_i32 t1 = tcg_temp_new_i32();
54843a58
AJ
1553 tcg_gen_trunc_i64_i32(t1, cpu_gpr[rS(ctx->opcode)]);
1554 tcg_gen_rotli_i32(t1, t1, sh);
1555 tcg_gen_extu_i32_i64(t0, t1);
a7812ae4 1556 tcg_temp_free_i32(t1);
54843a58
AJ
1557#else
1558 tcg_gen_rotli_i32(t0, cpu_gpr[rS(ctx->opcode)], sh);
1559#endif
76a66253 1560#if defined(TARGET_PPC64)
d03ef511
AJ
1561 mb += 32;
1562 me += 32;
76a66253 1563#endif
d03ef511
AJ
1564 tcg_gen_andi_tl(cpu_gpr[rA(ctx->opcode)], t0, MASK(mb, me));
1565 tcg_temp_free(t0);
1566 }
76a66253 1567 if (unlikely(Rc(ctx->opcode) != 0))
d03ef511 1568 gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
79aceca5 1569}
99e300ef 1570
54623277 1571/* rlwnm & rlwnm. */
99e300ef 1572static void gen_rlwnm(DisasContext *ctx)
79aceca5
FB
1573{
1574 uint32_t mb, me;
54843a58
AJ
1575 TCGv t0;
1576#if defined(TARGET_PPC64)
a7812ae4 1577 TCGv_i32 t1, t2;
54843a58 1578#endif
79aceca5
FB
1579
1580 mb = MB(ctx->opcode);
1581 me = ME(ctx->opcode);
a7812ae4 1582 t0 = tcg_temp_new();
d03ef511 1583 tcg_gen_andi_tl(t0, cpu_gpr[rB(ctx->opcode)], 0x1f);
54843a58 1584#if defined(TARGET_PPC64)
a7812ae4
PB
1585 t1 = tcg_temp_new_i32();
1586 t2 = tcg_temp_new_i32();
54843a58
AJ
1587 tcg_gen_trunc_i64_i32(t1, cpu_gpr[rS(ctx->opcode)]);
1588 tcg_gen_trunc_i64_i32(t2, t0);
1589 tcg_gen_rotl_i32(t1, t1, t2);
1590 tcg_gen_extu_i32_i64(t0, t1);
a7812ae4
PB
1591 tcg_temp_free_i32(t1);
1592 tcg_temp_free_i32(t2);
54843a58
AJ
1593#else
1594 tcg_gen_rotl_i32(t0, cpu_gpr[rS(ctx->opcode)], t0);
1595#endif
76a66253
JM
1596 if (unlikely(mb != 0 || me != 31)) {
1597#if defined(TARGET_PPC64)
1598 mb += 32;
1599 me += 32;
1600#endif
54843a58 1601 tcg_gen_andi_tl(cpu_gpr[rA(ctx->opcode)], t0, MASK(mb, me));
d03ef511 1602 } else {
54843a58 1603 tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], t0);
79aceca5 1604 }
54843a58 1605 tcg_temp_free(t0);
76a66253 1606 if (unlikely(Rc(ctx->opcode) != 0))
d03ef511 1607 gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
79aceca5
FB
1608}
1609
d9bce9d9
JM
1610#if defined(TARGET_PPC64)
1611#define GEN_PPC64_R2(name, opc1, opc2) \
e8eaa2c0 1612static void glue(gen_, name##0)(DisasContext *ctx) \
d9bce9d9
JM
1613{ \
1614 gen_##name(ctx, 0); \
1615} \
e8eaa2c0
BS
1616 \
1617static void glue(gen_, name##1)(DisasContext *ctx) \
d9bce9d9
JM
1618{ \
1619 gen_##name(ctx, 1); \
1620}
1621#define GEN_PPC64_R4(name, opc1, opc2) \
e8eaa2c0 1622static void glue(gen_, name##0)(DisasContext *ctx) \
d9bce9d9
JM
1623{ \
1624 gen_##name(ctx, 0, 0); \
1625} \
e8eaa2c0
BS
1626 \
1627static void glue(gen_, name##1)(DisasContext *ctx) \
d9bce9d9
JM
1628{ \
1629 gen_##name(ctx, 0, 1); \
1630} \
e8eaa2c0
BS
1631 \
1632static void glue(gen_, name##2)(DisasContext *ctx) \
d9bce9d9
JM
1633{ \
1634 gen_##name(ctx, 1, 0); \
1635} \
e8eaa2c0
BS
1636 \
1637static void glue(gen_, name##3)(DisasContext *ctx) \
d9bce9d9
JM
1638{ \
1639 gen_##name(ctx, 1, 1); \
1640}
51789c41 1641
636aa200
BS
1642static inline void gen_rldinm(DisasContext *ctx, uint32_t mb, uint32_t me,
1643 uint32_t sh)
51789c41 1644{
d03ef511
AJ
1645 if (likely(sh != 0 && mb == 0 && me == (63 - sh))) {
1646 tcg_gen_shli_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)], sh);
1647 } else if (likely(sh != 0 && me == 63 && sh == (64 - mb))) {
1648 tcg_gen_shri_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)], mb);
1649 } else {
a7812ae4 1650 TCGv t0 = tcg_temp_new();
54843a58 1651 tcg_gen_rotli_tl(t0, cpu_gpr[rS(ctx->opcode)], sh);
d03ef511 1652 if (likely(mb == 0 && me == 63)) {
54843a58 1653 tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], t0);
d03ef511
AJ
1654 } else {
1655 tcg_gen_andi_tl(cpu_gpr[rA(ctx->opcode)], t0, MASK(mb, me));
51789c41 1656 }
d03ef511 1657 tcg_temp_free(t0);
51789c41 1658 }
51789c41 1659 if (unlikely(Rc(ctx->opcode) != 0))
d03ef511 1660 gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
51789c41 1661}
d9bce9d9 1662/* rldicl - rldicl. */
636aa200 1663static inline void gen_rldicl(DisasContext *ctx, int mbn, int shn)
d9bce9d9 1664{
51789c41 1665 uint32_t sh, mb;
d9bce9d9 1666
9d53c753
JM
1667 sh = SH(ctx->opcode) | (shn << 5);
1668 mb = MB(ctx->opcode) | (mbn << 5);
51789c41 1669 gen_rldinm(ctx, mb, 63, sh);
d9bce9d9 1670}
51789c41 1671GEN_PPC64_R4(rldicl, 0x1E, 0x00);
d9bce9d9 1672/* rldicr - rldicr. */
636aa200 1673static inline void gen_rldicr(DisasContext *ctx, int men, int shn)
d9bce9d9 1674{
51789c41 1675 uint32_t sh, me;
d9bce9d9 1676
9d53c753
JM
1677 sh = SH(ctx->opcode) | (shn << 5);
1678 me = MB(ctx->opcode) | (men << 5);
51789c41 1679 gen_rldinm(ctx, 0, me, sh);
d9bce9d9 1680}
51789c41 1681GEN_PPC64_R4(rldicr, 0x1E, 0x02);
d9bce9d9 1682/* rldic - rldic. */
636aa200 1683static inline void gen_rldic(DisasContext *ctx, int mbn, int shn)
d9bce9d9 1684{
51789c41 1685 uint32_t sh, mb;
d9bce9d9 1686
9d53c753
JM
1687 sh = SH(ctx->opcode) | (shn << 5);
1688 mb = MB(ctx->opcode) | (mbn << 5);
51789c41
JM
1689 gen_rldinm(ctx, mb, 63 - sh, sh);
1690}
1691GEN_PPC64_R4(rldic, 0x1E, 0x04);
1692
636aa200 1693static inline void gen_rldnm(DisasContext *ctx, uint32_t mb, uint32_t me)
51789c41 1694{
54843a58 1695 TCGv t0;
d03ef511
AJ
1696
1697 mb = MB(ctx->opcode);
1698 me = ME(ctx->opcode);
a7812ae4 1699 t0 = tcg_temp_new();
d03ef511 1700 tcg_gen_andi_tl(t0, cpu_gpr[rB(ctx->opcode)], 0x3f);
54843a58 1701 tcg_gen_rotl_tl(t0, cpu_gpr[rS(ctx->opcode)], t0);
51789c41 1702 if (unlikely(mb != 0 || me != 63)) {
54843a58
AJ
1703 tcg_gen_andi_tl(cpu_gpr[rA(ctx->opcode)], t0, MASK(mb, me));
1704 } else {
1705 tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], t0);
1706 }
1707 tcg_temp_free(t0);
51789c41 1708 if (unlikely(Rc(ctx->opcode) != 0))
d03ef511 1709 gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
d9bce9d9 1710}
51789c41 1711
d9bce9d9 1712/* rldcl - rldcl. */
636aa200 1713static inline void gen_rldcl(DisasContext *ctx, int mbn)
d9bce9d9 1714{
51789c41 1715 uint32_t mb;
d9bce9d9 1716
9d53c753 1717 mb = MB(ctx->opcode) | (mbn << 5);
51789c41 1718 gen_rldnm(ctx, mb, 63);
d9bce9d9 1719}
36081602 1720GEN_PPC64_R2(rldcl, 0x1E, 0x08);
d9bce9d9 1721/* rldcr - rldcr. */
636aa200 1722static inline void gen_rldcr(DisasContext *ctx, int men)
d9bce9d9 1723{
51789c41 1724 uint32_t me;
d9bce9d9 1725
9d53c753 1726 me = MB(ctx->opcode) | (men << 5);
51789c41 1727 gen_rldnm(ctx, 0, me);
d9bce9d9 1728}
36081602 1729GEN_PPC64_R2(rldcr, 0x1E, 0x09);
d9bce9d9 1730/* rldimi - rldimi. */
636aa200 1731static inline void gen_rldimi(DisasContext *ctx, int mbn, int shn)
d9bce9d9 1732{
271a916e 1733 uint32_t sh, mb, me;
d9bce9d9 1734
9d53c753
JM
1735 sh = SH(ctx->opcode) | (shn << 5);
1736 mb = MB(ctx->opcode) | (mbn << 5);
271a916e 1737 me = 63 - sh;
d03ef511
AJ
1738 if (unlikely(sh == 0 && mb == 0)) {
1739 tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)]);
1740 } else {
1741 TCGv t0, t1;
1742 target_ulong mask;
1743
a7812ae4 1744 t0 = tcg_temp_new();
54843a58 1745 tcg_gen_rotli_tl(t0, cpu_gpr[rS(ctx->opcode)], sh);
a7812ae4 1746 t1 = tcg_temp_new();
d03ef511
AJ
1747 mask = MASK(mb, me);
1748 tcg_gen_andi_tl(t0, t0, mask);
1749 tcg_gen_andi_tl(t1, cpu_gpr[rA(ctx->opcode)], ~mask);
1750 tcg_gen_or_tl(cpu_gpr[rA(ctx->opcode)], t0, t1);
1751 tcg_temp_free(t0);
1752 tcg_temp_free(t1);
51789c41 1753 }
51789c41 1754 if (unlikely(Rc(ctx->opcode) != 0))
d03ef511 1755 gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
d9bce9d9 1756}
36081602 1757GEN_PPC64_R4(rldimi, 0x1E, 0x06);
d9bce9d9
JM
1758#endif
1759
79aceca5 1760/*** Integer shift ***/
99e300ef 1761
54623277 1762/* slw & slw. */
99e300ef 1763static void gen_slw(DisasContext *ctx)
26d67362 1764{
7fd6bf7d 1765 TCGv t0, t1;
26d67362 1766
7fd6bf7d
AJ
1767 t0 = tcg_temp_new();
1768 /* AND rS with a mask that is 0 when rB >= 0x20 */
1769#if defined(TARGET_PPC64)
1770 tcg_gen_shli_tl(t0, cpu_gpr[rB(ctx->opcode)], 0x3a);
1771 tcg_gen_sari_tl(t0, t0, 0x3f);
1772#else
1773 tcg_gen_shli_tl(t0, cpu_gpr[rB(ctx->opcode)], 0x1a);
1774 tcg_gen_sari_tl(t0, t0, 0x1f);
1775#endif
1776 tcg_gen_andc_tl(t0, cpu_gpr[rS(ctx->opcode)], t0);
1777 t1 = tcg_temp_new();
1778 tcg_gen_andi_tl(t1, cpu_gpr[rB(ctx->opcode)], 0x1f);
1779 tcg_gen_shl_tl(cpu_gpr[rA(ctx->opcode)], t0, t1);
1780 tcg_temp_free(t1);
fea0c503 1781 tcg_temp_free(t0);
7fd6bf7d 1782 tcg_gen_ext32u_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rA(ctx->opcode)]);
26d67362
AJ
1783 if (unlikely(Rc(ctx->opcode) != 0))
1784 gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
1785}
99e300ef 1786
54623277 1787/* sraw & sraw. */
99e300ef 1788static void gen_sraw(DisasContext *ctx)
26d67362 1789{
d15f74fb 1790 gen_helper_sraw(cpu_gpr[rA(ctx->opcode)], cpu_env,
a7812ae4 1791 cpu_gpr[rS(ctx->opcode)], cpu_gpr[rB(ctx->opcode)]);
26d67362
AJ
1792 if (unlikely(Rc(ctx->opcode) != 0))
1793 gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
1794}
99e300ef 1795
54623277 1796/* srawi & srawi. */
99e300ef 1797static void gen_srawi(DisasContext *ctx)
79aceca5 1798{
26d67362 1799 int sh = SH(ctx->opcode);
ba4af3e4
RH
1800 TCGv dst = cpu_gpr[rA(ctx->opcode)];
1801 TCGv src = cpu_gpr[rS(ctx->opcode)];
1802 if (sh == 0) {
1803 tcg_gen_mov_tl(dst, src);
da91a00f 1804 tcg_gen_movi_tl(cpu_ca, 0);
26d67362 1805 } else {
ba4af3e4
RH
1806 TCGv t0;
1807 tcg_gen_ext32s_tl(dst, src);
1808 tcg_gen_andi_tl(cpu_ca, dst, (1ULL << sh) - 1);
1809 t0 = tcg_temp_new();
1810 tcg_gen_sari_tl(t0, dst, TARGET_LONG_BITS - 1);
1811 tcg_gen_and_tl(cpu_ca, cpu_ca, t0);
1812 tcg_temp_free(t0);
1813 tcg_gen_setcondi_tl(TCG_COND_NE, cpu_ca, cpu_ca, 0);
1814 tcg_gen_sari_tl(dst, dst, sh);
1815 }
1816 if (unlikely(Rc(ctx->opcode) != 0)) {
1817 gen_set_Rc0(ctx, dst);
d9bce9d9 1818 }
79aceca5 1819}
99e300ef 1820
54623277 1821/* srw & srw. */
99e300ef 1822static void gen_srw(DisasContext *ctx)
26d67362 1823{
fea0c503 1824 TCGv t0, t1;
d9bce9d9 1825
7fd6bf7d
AJ
1826 t0 = tcg_temp_new();
1827 /* AND rS with a mask that is 0 when rB >= 0x20 */
1828#if defined(TARGET_PPC64)
1829 tcg_gen_shli_tl(t0, cpu_gpr[rB(ctx->opcode)], 0x3a);
1830 tcg_gen_sari_tl(t0, t0, 0x3f);
1831#else
1832 tcg_gen_shli_tl(t0, cpu_gpr[rB(ctx->opcode)], 0x1a);
1833 tcg_gen_sari_tl(t0, t0, 0x1f);
1834#endif
1835 tcg_gen_andc_tl(t0, cpu_gpr[rS(ctx->opcode)], t0);
1836 tcg_gen_ext32u_tl(t0, t0);
a7812ae4 1837 t1 = tcg_temp_new();
7fd6bf7d
AJ
1838 tcg_gen_andi_tl(t1, cpu_gpr[rB(ctx->opcode)], 0x1f);
1839 tcg_gen_shr_tl(cpu_gpr[rA(ctx->opcode)], t0, t1);
fea0c503 1840 tcg_temp_free(t1);
fea0c503 1841 tcg_temp_free(t0);
26d67362
AJ
1842 if (unlikely(Rc(ctx->opcode) != 0))
1843 gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
1844}
54623277 1845
d9bce9d9
JM
1846#if defined(TARGET_PPC64)
1847/* sld & sld. */
99e300ef 1848static void gen_sld(DisasContext *ctx)
26d67362 1849{
7fd6bf7d 1850 TCGv t0, t1;
26d67362 1851
7fd6bf7d
AJ
1852 t0 = tcg_temp_new();
1853 /* AND rS with a mask that is 0 when rB >= 0x40 */
1854 tcg_gen_shli_tl(t0, cpu_gpr[rB(ctx->opcode)], 0x39);
1855 tcg_gen_sari_tl(t0, t0, 0x3f);
1856 tcg_gen_andc_tl(t0, cpu_gpr[rS(ctx->opcode)], t0);
1857 t1 = tcg_temp_new();
1858 tcg_gen_andi_tl(t1, cpu_gpr[rB(ctx->opcode)], 0x3f);
1859 tcg_gen_shl_tl(cpu_gpr[rA(ctx->opcode)], t0, t1);
1860 tcg_temp_free(t1);
fea0c503 1861 tcg_temp_free(t0);
26d67362
AJ
1862 if (unlikely(Rc(ctx->opcode) != 0))
1863 gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
1864}
99e300ef 1865
54623277 1866/* srad & srad. */
99e300ef 1867static void gen_srad(DisasContext *ctx)
26d67362 1868{
d15f74fb 1869 gen_helper_srad(cpu_gpr[rA(ctx->opcode)], cpu_env,
a7812ae4 1870 cpu_gpr[rS(ctx->opcode)], cpu_gpr[rB(ctx->opcode)]);
26d67362
AJ
1871 if (unlikely(Rc(ctx->opcode) != 0))
1872 gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
1873}
d9bce9d9 1874/* sradi & sradi. */
636aa200 1875static inline void gen_sradi(DisasContext *ctx, int n)
d9bce9d9 1876{
26d67362 1877 int sh = SH(ctx->opcode) + (n << 5);
ba4af3e4
RH
1878 TCGv dst = cpu_gpr[rA(ctx->opcode)];
1879 TCGv src = cpu_gpr[rS(ctx->opcode)];
1880 if (sh == 0) {
1881 tcg_gen_mov_tl(dst, src);
da91a00f 1882 tcg_gen_movi_tl(cpu_ca, 0);
26d67362 1883 } else {
ba4af3e4
RH
1884 TCGv t0;
1885 tcg_gen_andi_tl(cpu_ca, src, (1ULL << sh) - 1);
1886 t0 = tcg_temp_new();
1887 tcg_gen_sari_tl(t0, src, TARGET_LONG_BITS - 1);
1888 tcg_gen_and_tl(cpu_ca, cpu_ca, t0);
1889 tcg_temp_free(t0);
1890 tcg_gen_setcondi_tl(TCG_COND_NE, cpu_ca, cpu_ca, 0);
1891 tcg_gen_sari_tl(dst, src, sh);
1892 }
1893 if (unlikely(Rc(ctx->opcode) != 0)) {
1894 gen_set_Rc0(ctx, dst);
d9bce9d9 1895 }
d9bce9d9 1896}
e8eaa2c0
BS
1897
1898static void gen_sradi0(DisasContext *ctx)
d9bce9d9
JM
1899{
1900 gen_sradi(ctx, 0);
1901}
e8eaa2c0
BS
1902
1903static void gen_sradi1(DisasContext *ctx)
d9bce9d9
JM
1904{
1905 gen_sradi(ctx, 1);
1906}
99e300ef 1907
54623277 1908/* srd & srd. */
99e300ef 1909static void gen_srd(DisasContext *ctx)
26d67362 1910{
7fd6bf7d 1911 TCGv t0, t1;
26d67362 1912
7fd6bf7d
AJ
1913 t0 = tcg_temp_new();
1914 /* AND rS with a mask that is 0 when rB >= 0x40 */
1915 tcg_gen_shli_tl(t0, cpu_gpr[rB(ctx->opcode)], 0x39);
1916 tcg_gen_sari_tl(t0, t0, 0x3f);
1917 tcg_gen_andc_tl(t0, cpu_gpr[rS(ctx->opcode)], t0);
1918 t1 = tcg_temp_new();
1919 tcg_gen_andi_tl(t1, cpu_gpr[rB(ctx->opcode)], 0x3f);
1920 tcg_gen_shr_tl(cpu_gpr[rA(ctx->opcode)], t0, t1);
1921 tcg_temp_free(t1);
fea0c503 1922 tcg_temp_free(t0);
26d67362
AJ
1923 if (unlikely(Rc(ctx->opcode) != 0))
1924 gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
1925}
d9bce9d9 1926#endif
79aceca5
FB
1927
1928/*** Floating-Point arithmetic ***/
7c58044c 1929#define _GEN_FLOAT_ACB(name, op, op1, op2, isfloat, set_fprf, type) \
99e300ef 1930static void gen_f##name(DisasContext *ctx) \
9a64fbe4 1931{ \
76a66253 1932 if (unlikely(!ctx->fpu_enabled)) { \
e06fcd75 1933 gen_exception(ctx, POWERPC_EXCP_FPU); \
3cc62370
FB
1934 return; \
1935 } \
eb44b959
AJ
1936 /* NIP cannot be restored if the memory exception comes from an helper */ \
1937 gen_update_nip(ctx, ctx->nip - 4); \
7c58044c 1938 gen_reset_fpstatus(); \
8e703949
BS
1939 gen_helper_f##op(cpu_fpr[rD(ctx->opcode)], cpu_env, \
1940 cpu_fpr[rA(ctx->opcode)], \
af12906f 1941 cpu_fpr[rC(ctx->opcode)], cpu_fpr[rB(ctx->opcode)]); \
4ecc3190 1942 if (isfloat) { \
8e703949
BS
1943 gen_helper_frsp(cpu_fpr[rD(ctx->opcode)], cpu_env, \
1944 cpu_fpr[rD(ctx->opcode)]); \
4ecc3190 1945 } \
af12906f
AJ
1946 gen_compute_fprf(cpu_fpr[rD(ctx->opcode)], set_fprf, \
1947 Rc(ctx->opcode) != 0); \
9a64fbe4
FB
1948}
1949
7c58044c
JM
1950#define GEN_FLOAT_ACB(name, op2, set_fprf, type) \
1951_GEN_FLOAT_ACB(name, name, 0x3F, op2, 0, set_fprf, type); \
1952_GEN_FLOAT_ACB(name##s, name, 0x3B, op2, 1, set_fprf, type);
9a64fbe4 1953
7c58044c 1954#define _GEN_FLOAT_AB(name, op, op1, op2, inval, isfloat, set_fprf, type) \
99e300ef 1955static void gen_f##name(DisasContext *ctx) \
9a64fbe4 1956{ \
76a66253 1957 if (unlikely(!ctx->fpu_enabled)) { \
e06fcd75 1958 gen_exception(ctx, POWERPC_EXCP_FPU); \
3cc62370
FB
1959 return; \
1960 } \
eb44b959
AJ
1961 /* NIP cannot be restored if the memory exception comes from an helper */ \
1962 gen_update_nip(ctx, ctx->nip - 4); \
7c58044c 1963 gen_reset_fpstatus(); \
8e703949
BS
1964 gen_helper_f##op(cpu_fpr[rD(ctx->opcode)], cpu_env, \
1965 cpu_fpr[rA(ctx->opcode)], \
af12906f 1966 cpu_fpr[rB(ctx->opcode)]); \
4ecc3190 1967 if (isfloat) { \
8e703949
BS
1968 gen_helper_frsp(cpu_fpr[rD(ctx->opcode)], cpu_env, \
1969 cpu_fpr[rD(ctx->opcode)]); \
4ecc3190 1970 } \
af12906f
AJ
1971 gen_compute_fprf(cpu_fpr[rD(ctx->opcode)], \
1972 set_fprf, Rc(ctx->opcode) != 0); \
9a64fbe4 1973}
7c58044c
JM
1974#define GEN_FLOAT_AB(name, op2, inval, set_fprf, type) \
1975_GEN_FLOAT_AB(name, name, 0x3F, op2, inval, 0, set_fprf, type); \
1976_GEN_FLOAT_AB(name##s, name, 0x3B, op2, inval, 1, set_fprf, type);
9a64fbe4 1977
7c58044c 1978#define _GEN_FLOAT_AC(name, op, op1, op2, inval, isfloat, set_fprf, type) \
99e300ef 1979static void gen_f##name(DisasContext *ctx) \
9a64fbe4 1980{ \
76a66253 1981 if (unlikely(!ctx->fpu_enabled)) { \
e06fcd75 1982 gen_exception(ctx, POWERPC_EXCP_FPU); \
3cc62370
FB
1983 return; \
1984 } \
eb44b959
AJ
1985 /* NIP cannot be restored if the memory exception comes from an helper */ \
1986 gen_update_nip(ctx, ctx->nip - 4); \
7c58044c 1987 gen_reset_fpstatus(); \
8e703949
BS
1988 gen_helper_f##op(cpu_fpr[rD(ctx->opcode)], cpu_env, \
1989 cpu_fpr[rA(ctx->opcode)], \
1990 cpu_fpr[rC(ctx->opcode)]); \
4ecc3190 1991 if (isfloat) { \
8e703949
BS
1992 gen_helper_frsp(cpu_fpr[rD(ctx->opcode)], cpu_env, \
1993 cpu_fpr[rD(ctx->opcode)]); \
4ecc3190 1994 } \
af12906f
AJ
1995 gen_compute_fprf(cpu_fpr[rD(ctx->opcode)], \
1996 set_fprf, Rc(ctx->opcode) != 0); \
9a64fbe4 1997}
7c58044c
JM
1998#define GEN_FLOAT_AC(name, op2, inval, set_fprf, type) \
1999_GEN_FLOAT_AC(name, name, 0x3F, op2, inval, 0, set_fprf, type); \
2000_GEN_FLOAT_AC(name##s, name, 0x3B, op2, inval, 1, set_fprf, type);
9a64fbe4 2001
7c58044c 2002#define GEN_FLOAT_B(name, op2, op3, set_fprf, type) \
99e300ef 2003static void gen_f##name(DisasContext *ctx) \
9a64fbe4 2004{ \
76a66253 2005 if (unlikely(!ctx->fpu_enabled)) { \
e06fcd75 2006 gen_exception(ctx, POWERPC_EXCP_FPU); \
3cc62370
FB
2007 return; \
2008 } \
eb44b959
AJ
2009 /* NIP cannot be restored if the memory exception comes from an helper */ \
2010 gen_update_nip(ctx, ctx->nip - 4); \
7c58044c 2011 gen_reset_fpstatus(); \
8e703949
BS
2012 gen_helper_f##name(cpu_fpr[rD(ctx->opcode)], cpu_env, \
2013 cpu_fpr[rB(ctx->opcode)]); \
af12906f
AJ
2014 gen_compute_fprf(cpu_fpr[rD(ctx->opcode)], \
2015 set_fprf, Rc(ctx->opcode) != 0); \
79aceca5
FB
2016}
2017
7c58044c 2018#define GEN_FLOAT_BS(name, op1, op2, set_fprf, type) \
99e300ef 2019static void gen_f##name(DisasContext *ctx) \
9a64fbe4 2020{ \
76a66253 2021 if (unlikely(!ctx->fpu_enabled)) { \
e06fcd75 2022 gen_exception(ctx, POWERPC_EXCP_FPU); \
3cc62370
FB
2023 return; \
2024 } \
eb44b959
AJ
2025 /* NIP cannot be restored if the memory exception comes from an helper */ \
2026 gen_update_nip(ctx, ctx->nip - 4); \
7c58044c 2027 gen_reset_fpstatus(); \
8e703949
BS
2028 gen_helper_f##name(cpu_fpr[rD(ctx->opcode)], cpu_env, \
2029 cpu_fpr[rB(ctx->opcode)]); \
af12906f
AJ
2030 gen_compute_fprf(cpu_fpr[rD(ctx->opcode)], \
2031 set_fprf, Rc(ctx->opcode) != 0); \
79aceca5
FB
2032}
2033
9a64fbe4 2034/* fadd - fadds */
7c58044c 2035GEN_FLOAT_AB(add, 0x15, 0x000007C0, 1, PPC_FLOAT);
4ecc3190 2036/* fdiv - fdivs */
7c58044c 2037GEN_FLOAT_AB(div, 0x12, 0x000007C0, 1, PPC_FLOAT);
4ecc3190 2038/* fmul - fmuls */
7c58044c 2039GEN_FLOAT_AC(mul, 0x19, 0x0000F800, 1, PPC_FLOAT);
79aceca5 2040
d7e4b87e 2041/* fre */
7c58044c 2042GEN_FLOAT_BS(re, 0x3F, 0x18, 1, PPC_FLOAT_EXT);
d7e4b87e 2043
a750fc0b 2044/* fres */
7c58044c 2045GEN_FLOAT_BS(res, 0x3B, 0x18, 1, PPC_FLOAT_FRES);
79aceca5 2046
a750fc0b 2047/* frsqrte */
7c58044c
JM
2048GEN_FLOAT_BS(rsqrte, 0x3F, 0x1A, 1, PPC_FLOAT_FRSQRTE);
2049
2050/* frsqrtes */
99e300ef 2051static void gen_frsqrtes(DisasContext *ctx)
7c58044c 2052{
af12906f 2053 if (unlikely(!ctx->fpu_enabled)) {
e06fcd75 2054 gen_exception(ctx, POWERPC_EXCP_FPU);
af12906f
AJ
2055 return;
2056 }
eb44b959
AJ
2057 /* NIP cannot be restored if the memory exception comes from an helper */
2058 gen_update_nip(ctx, ctx->nip - 4);
af12906f 2059 gen_reset_fpstatus();
8e703949
BS
2060 gen_helper_frsqrte(cpu_fpr[rD(ctx->opcode)], cpu_env,
2061 cpu_fpr[rB(ctx->opcode)]);
2062 gen_helper_frsp(cpu_fpr[rD(ctx->opcode)], cpu_env,
2063 cpu_fpr[rD(ctx->opcode)]);
af12906f 2064 gen_compute_fprf(cpu_fpr[rD(ctx->opcode)], 1, Rc(ctx->opcode) != 0);
7c58044c 2065}
79aceca5 2066
a750fc0b 2067/* fsel */
7c58044c 2068_GEN_FLOAT_ACB(sel, sel, 0x3F, 0x17, 0, 0, PPC_FLOAT_FSEL);
4ecc3190 2069/* fsub - fsubs */
7c58044c 2070GEN_FLOAT_AB(sub, 0x14, 0x000007C0, 1, PPC_FLOAT);
79aceca5 2071/* Optional: */
99e300ef 2072
54623277 2073/* fsqrt */
99e300ef 2074static void gen_fsqrt(DisasContext *ctx)
c7d344af 2075{
76a66253 2076 if (unlikely(!ctx->fpu_enabled)) {
e06fcd75 2077 gen_exception(ctx, POWERPC_EXCP_FPU);
c7d344af
FB
2078 return;
2079 }
eb44b959
AJ
2080 /* NIP cannot be restored if the memory exception comes from an helper */
2081 gen_update_nip(ctx, ctx->nip - 4);
7c58044c 2082 gen_reset_fpstatus();
8e703949
BS
2083 gen_helper_fsqrt(cpu_fpr[rD(ctx->opcode)], cpu_env,
2084 cpu_fpr[rB(ctx->opcode)]);
af12906f 2085 gen_compute_fprf(cpu_fpr[rD(ctx->opcode)], 1, Rc(ctx->opcode) != 0);
c7d344af 2086}
79aceca5 2087
99e300ef 2088static void gen_fsqrts(DisasContext *ctx)
79aceca5 2089{
76a66253 2090 if (unlikely(!ctx->fpu_enabled)) {
e06fcd75 2091 gen_exception(ctx, POWERPC_EXCP_FPU);
3cc62370
FB
2092 return;
2093 }
eb44b959
AJ
2094 /* NIP cannot be restored if the memory exception comes from an helper */
2095 gen_update_nip(ctx, ctx->nip - 4);
7c58044c 2096 gen_reset_fpstatus();
8e703949
BS
2097 gen_helper_fsqrt(cpu_fpr[rD(ctx->opcode)], cpu_env,
2098 cpu_fpr[rB(ctx->opcode)]);
2099 gen_helper_frsp(cpu_fpr[rD(ctx->opcode)], cpu_env,
2100 cpu_fpr[rD(ctx->opcode)]);
af12906f 2101 gen_compute_fprf(cpu_fpr[rD(ctx->opcode)], 1, Rc(ctx->opcode) != 0);
79aceca5
FB
2102}
2103
2104/*** Floating-Point multiply-and-add ***/
4ecc3190 2105/* fmadd - fmadds */
7c58044c 2106GEN_FLOAT_ACB(madd, 0x1D, 1, PPC_FLOAT);
4ecc3190 2107/* fmsub - fmsubs */
7c58044c 2108GEN_FLOAT_ACB(msub, 0x1C, 1, PPC_FLOAT);
4ecc3190 2109/* fnmadd - fnmadds */
7c58044c 2110GEN_FLOAT_ACB(nmadd, 0x1F, 1, PPC_FLOAT);
4ecc3190 2111/* fnmsub - fnmsubs */
7c58044c 2112GEN_FLOAT_ACB(nmsub, 0x1E, 1, PPC_FLOAT);
79aceca5
FB
2113
2114/*** Floating-Point round & convert ***/
2115/* fctiw */
7c58044c 2116GEN_FLOAT_B(ctiw, 0x0E, 0x00, 0, PPC_FLOAT);
79aceca5 2117/* fctiwz */
7c58044c 2118GEN_FLOAT_B(ctiwz, 0x0F, 0x00, 0, PPC_FLOAT);
79aceca5 2119/* frsp */
7c58044c 2120GEN_FLOAT_B(rsp, 0x0C, 0x00, 1, PPC_FLOAT);
426613db
JM
2121#if defined(TARGET_PPC64)
2122/* fcfid */
7c58044c 2123GEN_FLOAT_B(cfid, 0x0E, 0x1A, 1, PPC_64B);
426613db 2124/* fctid */
7c58044c 2125GEN_FLOAT_B(ctid, 0x0E, 0x19, 0, PPC_64B);
426613db 2126/* fctidz */
7c58044c 2127GEN_FLOAT_B(ctidz, 0x0F, 0x19, 0, PPC_64B);
426613db 2128#endif
79aceca5 2129
d7e4b87e 2130/* frin */
7c58044c 2131GEN_FLOAT_B(rin, 0x08, 0x0C, 1, PPC_FLOAT_EXT);
d7e4b87e 2132/* friz */
7c58044c 2133GEN_FLOAT_B(riz, 0x08, 0x0D, 1, PPC_FLOAT_EXT);
d7e4b87e 2134/* frip */
7c58044c 2135GEN_FLOAT_B(rip, 0x08, 0x0E, 1, PPC_FLOAT_EXT);
d7e4b87e 2136/* frim */
7c58044c 2137GEN_FLOAT_B(rim, 0x08, 0x0F, 1, PPC_FLOAT_EXT);
d7e4b87e 2138
79aceca5 2139/*** Floating-Point compare ***/
99e300ef 2140
54623277 2141/* fcmpo */
99e300ef 2142static void gen_fcmpo(DisasContext *ctx)
79aceca5 2143{
330c483b 2144 TCGv_i32 crf;
76a66253 2145 if (unlikely(!ctx->fpu_enabled)) {
e06fcd75 2146 gen_exception(ctx, POWERPC_EXCP_FPU);
3cc62370
FB
2147 return;
2148 }
eb44b959
AJ
2149 /* NIP cannot be restored if the memory exception comes from an helper */
2150 gen_update_nip(ctx, ctx->nip - 4);
7c58044c 2151 gen_reset_fpstatus();
9a819377 2152 crf = tcg_const_i32(crfD(ctx->opcode));
8e703949
BS
2153 gen_helper_fcmpo(cpu_env, cpu_fpr[rA(ctx->opcode)],
2154 cpu_fpr[rB(ctx->opcode)], crf);
330c483b 2155 tcg_temp_free_i32(crf);
8e703949 2156 gen_helper_float_check_status(cpu_env);
79aceca5
FB
2157}
2158
2159/* fcmpu */
99e300ef 2160static void gen_fcmpu(DisasContext *ctx)
79aceca5 2161{
330c483b 2162 TCGv_i32 crf;
76a66253 2163 if (unlikely(!ctx->fpu_enabled)) {
e06fcd75 2164 gen_exception(ctx, POWERPC_EXCP_FPU);
3cc62370
FB
2165 return;
2166 }
eb44b959
AJ
2167 /* NIP cannot be restored if the memory exception comes from an helper */
2168 gen_update_nip(ctx, ctx->nip - 4);
7c58044c 2169 gen_reset_fpstatus();
9a819377 2170 crf = tcg_const_i32(crfD(ctx->opcode));
8e703949
BS
2171 gen_helper_fcmpu(cpu_env, cpu_fpr[rA(ctx->opcode)],
2172 cpu_fpr[rB(ctx->opcode)], crf);
330c483b 2173 tcg_temp_free_i32(crf);
8e703949 2174 gen_helper_float_check_status(cpu_env);
79aceca5
FB
2175}
2176
9a64fbe4
FB
2177/*** Floating-point move ***/
2178/* fabs */
7c58044c 2179/* XXX: beware that fabs never checks for NaNs nor update FPSCR */
bf45a2e6
AJ
2180static void gen_fabs(DisasContext *ctx)
2181{
2182 if (unlikely(!ctx->fpu_enabled)) {
2183 gen_exception(ctx, POWERPC_EXCP_FPU);
2184 return;
2185 }
2186 tcg_gen_andi_i64(cpu_fpr[rD(ctx->opcode)], cpu_fpr[rB(ctx->opcode)],
2187 ~(1ULL << 63));
2188 gen_compute_fprf(cpu_fpr[rD(ctx->opcode)], 0, Rc(ctx->opcode) != 0);
2189}
9a64fbe4
FB
2190
2191/* fmr - fmr. */
7c58044c 2192/* XXX: beware that fmr never checks for NaNs nor update FPSCR */
99e300ef 2193static void gen_fmr(DisasContext *ctx)
9a64fbe4 2194{
76a66253 2195 if (unlikely(!ctx->fpu_enabled)) {
e06fcd75 2196 gen_exception(ctx, POWERPC_EXCP_FPU);
3cc62370
FB
2197 return;
2198 }
af12906f
AJ
2199 tcg_gen_mov_i64(cpu_fpr[rD(ctx->opcode)], cpu_fpr[rB(ctx->opcode)]);
2200 gen_compute_fprf(cpu_fpr[rD(ctx->opcode)], 0, Rc(ctx->opcode) != 0);
9a64fbe4
FB
2201}
2202
2203/* fnabs */
7c58044c 2204/* XXX: beware that fnabs never checks for NaNs nor update FPSCR */
bf45a2e6
AJ
2205static void gen_fnabs(DisasContext *ctx)
2206{
2207 if (unlikely(!ctx->fpu_enabled)) {
2208 gen_exception(ctx, POWERPC_EXCP_FPU);
2209 return;
2210 }
2211 tcg_gen_ori_i64(cpu_fpr[rD(ctx->opcode)], cpu_fpr[rB(ctx->opcode)],
2212 1ULL << 63);
2213 gen_compute_fprf(cpu_fpr[rD(ctx->opcode)], 0, Rc(ctx->opcode) != 0);
2214}
2215
9a64fbe4 2216/* fneg */
7c58044c 2217/* XXX: beware that fneg never checks for NaNs nor update FPSCR */
bf45a2e6
AJ
2218static void gen_fneg(DisasContext *ctx)
2219{
2220 if (unlikely(!ctx->fpu_enabled)) {
2221 gen_exception(ctx, POWERPC_EXCP_FPU);
2222 return;
2223 }
2224 tcg_gen_xori_i64(cpu_fpr[rD(ctx->opcode)], cpu_fpr[rB(ctx->opcode)],
2225 1ULL << 63);
2226 gen_compute_fprf(cpu_fpr[rD(ctx->opcode)], 0, Rc(ctx->opcode) != 0);
2227}
9a64fbe4 2228
79aceca5 2229/*** Floating-Point status & ctrl register ***/
99e300ef 2230
54623277 2231/* mcrfs */
99e300ef 2232static void gen_mcrfs(DisasContext *ctx)
79aceca5 2233{
30304420 2234 TCGv tmp = tcg_temp_new();
7c58044c
JM
2235 int bfa;
2236
76a66253 2237 if (unlikely(!ctx->fpu_enabled)) {
e06fcd75 2238 gen_exception(ctx, POWERPC_EXCP_FPU);
3cc62370
FB
2239 return;
2240 }
7c58044c 2241 bfa = 4 * (7 - crfS(ctx->opcode));
30304420
DG
2242 tcg_gen_shri_tl(tmp, cpu_fpscr, bfa);
2243 tcg_gen_trunc_tl_i32(cpu_crf[crfD(ctx->opcode)], tmp);
2244 tcg_temp_free(tmp);
e1571908 2245 tcg_gen_andi_i32(cpu_crf[crfD(ctx->opcode)], cpu_crf[crfD(ctx->opcode)], 0xf);
30304420 2246 tcg_gen_andi_tl(cpu_fpscr, cpu_fpscr, ~(0xF << bfa));
79aceca5
FB
2247}
2248
2249/* mffs */
99e300ef 2250static void gen_mffs(DisasContext *ctx)
79aceca5 2251{
76a66253 2252 if (unlikely(!ctx->fpu_enabled)) {
e06fcd75 2253 gen_exception(ctx, POWERPC_EXCP_FPU);
3cc62370
FB
2254 return;
2255 }
7c58044c 2256 gen_reset_fpstatus();
30304420 2257 tcg_gen_extu_tl_i64(cpu_fpr[rD(ctx->opcode)], cpu_fpscr);
af12906f 2258 gen_compute_fprf(cpu_fpr[rD(ctx->opcode)], 0, Rc(ctx->opcode) != 0);
79aceca5
FB
2259}
2260
2261/* mtfsb0 */
99e300ef 2262static void gen_mtfsb0(DisasContext *ctx)
79aceca5 2263{
fb0eaffc 2264 uint8_t crb;
3b46e624 2265
76a66253 2266 if (unlikely(!ctx->fpu_enabled)) {
e06fcd75 2267 gen_exception(ctx, POWERPC_EXCP_FPU);
3cc62370
FB
2268 return;
2269 }
6e35d524 2270 crb = 31 - crbD(ctx->opcode);
7c58044c 2271 gen_reset_fpstatus();
6e35d524 2272 if (likely(crb != FPSCR_FEX && crb != FPSCR_VX)) {
eb44b959
AJ
2273 TCGv_i32 t0;
2274 /* NIP cannot be restored if the memory exception comes from an helper */
2275 gen_update_nip(ctx, ctx->nip - 4);
2276 t0 = tcg_const_i32(crb);
8e703949 2277 gen_helper_fpscr_clrbit(cpu_env, t0);
6e35d524
AJ
2278 tcg_temp_free_i32(t0);
2279 }
7c58044c 2280 if (unlikely(Rc(ctx->opcode) != 0)) {
30304420
DG
2281 tcg_gen_trunc_tl_i32(cpu_crf[1], cpu_fpscr);
2282 tcg_gen_shri_i32(cpu_crf[1], cpu_crf[1], FPSCR_OX);
7c58044c 2283 }
79aceca5
FB
2284}
2285
2286/* mtfsb1 */
99e300ef 2287static void gen_mtfsb1(DisasContext *ctx)
79aceca5 2288{
fb0eaffc 2289 uint8_t crb;
3b46e624 2290
76a66253 2291 if (unlikely(!ctx->fpu_enabled)) {
e06fcd75 2292 gen_exception(ctx, POWERPC_EXCP_FPU);
3cc62370
FB
2293 return;
2294 }
6e35d524 2295 crb = 31 - crbD(ctx->opcode);
7c58044c
JM
2296 gen_reset_fpstatus();
2297 /* XXX: we pretend we can only do IEEE floating-point computations */
af12906f 2298 if (likely(crb != FPSCR_FEX && crb != FPSCR_VX && crb != FPSCR_NI)) {
eb44b959
AJ
2299 TCGv_i32 t0;
2300 /* NIP cannot be restored if the memory exception comes from an helper */
2301 gen_update_nip(ctx, ctx->nip - 4);
2302 t0 = tcg_const_i32(crb);
8e703949 2303 gen_helper_fpscr_setbit(cpu_env, t0);
0f2f39c2 2304 tcg_temp_free_i32(t0);
af12906f 2305 }
7c58044c 2306 if (unlikely(Rc(ctx->opcode) != 0)) {
30304420
DG
2307 tcg_gen_trunc_tl_i32(cpu_crf[1], cpu_fpscr);
2308 tcg_gen_shri_i32(cpu_crf[1], cpu_crf[1], FPSCR_OX);
7c58044c
JM
2309 }
2310 /* We can raise a differed exception */
8e703949 2311 gen_helper_float_check_status(cpu_env);
79aceca5
FB
2312}
2313
2314/* mtfsf */
99e300ef 2315static void gen_mtfsf(DisasContext *ctx)
79aceca5 2316{
0f2f39c2 2317 TCGv_i32 t0;
4911012d 2318 int L = ctx->opcode & 0x02000000;
af12906f 2319
76a66253 2320 if (unlikely(!ctx->fpu_enabled)) {
e06fcd75 2321 gen_exception(ctx, POWERPC_EXCP_FPU);
3cc62370
FB
2322 return;
2323 }
eb44b959
AJ
2324 /* NIP cannot be restored if the memory exception comes from an helper */
2325 gen_update_nip(ctx, ctx->nip - 4);
7c58044c 2326 gen_reset_fpstatus();
4911012d
BS
2327 if (L)
2328 t0 = tcg_const_i32(0xff);
2329 else
2330 t0 = tcg_const_i32(FM(ctx->opcode));
8e703949 2331 gen_helper_store_fpscr(cpu_env, cpu_fpr[rB(ctx->opcode)], t0);
0f2f39c2 2332 tcg_temp_free_i32(t0);
7c58044c 2333 if (unlikely(Rc(ctx->opcode) != 0)) {
30304420
DG
2334 tcg_gen_trunc_tl_i32(cpu_crf[1], cpu_fpscr);
2335 tcg_gen_shri_i32(cpu_crf[1], cpu_crf[1], FPSCR_OX);
7c58044c
JM
2336 }
2337 /* We can raise a differed exception */
8e703949 2338 gen_helper_float_check_status(cpu_env);
79aceca5
FB
2339}
2340
2341/* mtfsfi */
99e300ef 2342static void gen_mtfsfi(DisasContext *ctx)
79aceca5 2343{
7c58044c 2344 int bf, sh;
0f2f39c2
AJ
2345 TCGv_i64 t0;
2346 TCGv_i32 t1;
7c58044c 2347
76a66253 2348 if (unlikely(!ctx->fpu_enabled)) {
e06fcd75 2349 gen_exception(ctx, POWERPC_EXCP_FPU);
3cc62370
FB
2350 return;
2351 }
7c58044c
JM
2352 bf = crbD(ctx->opcode) >> 2;
2353 sh = 7 - bf;
eb44b959
AJ
2354 /* NIP cannot be restored if the memory exception comes from an helper */
2355 gen_update_nip(ctx, ctx->nip - 4);
7c58044c 2356 gen_reset_fpstatus();
0f2f39c2 2357 t0 = tcg_const_i64(FPIMM(ctx->opcode) << (4 * sh));
af12906f 2358 t1 = tcg_const_i32(1 << sh);
8e703949 2359 gen_helper_store_fpscr(cpu_env, t0, t1);
0f2f39c2
AJ
2360 tcg_temp_free_i64(t0);
2361 tcg_temp_free_i32(t1);
7c58044c 2362 if (unlikely(Rc(ctx->opcode) != 0)) {
30304420
DG
2363 tcg_gen_trunc_tl_i32(cpu_crf[1], cpu_fpscr);
2364 tcg_gen_shri_i32(cpu_crf[1], cpu_crf[1], FPSCR_OX);
7c58044c
JM
2365 }
2366 /* We can raise a differed exception */
8e703949 2367 gen_helper_float_check_status(cpu_env);
79aceca5
FB
2368}
2369
76a66253
JM
2370/*** Addressing modes ***/
2371/* Register indirect with immediate index : EA = (rA|0) + SIMM */
636aa200
BS
2372static inline void gen_addr_imm_index(DisasContext *ctx, TCGv EA,
2373 target_long maskl)
76a66253
JM
2374{
2375 target_long simm = SIMM(ctx->opcode);
2376
be147d08 2377 simm &= ~maskl;
76db3ba4 2378 if (rA(ctx->opcode) == 0) {
c791fe84
RH
2379 if (NARROW_MODE(ctx)) {
2380 simm = (uint32_t)simm;
2381 }
e2be8d8d 2382 tcg_gen_movi_tl(EA, simm);
76db3ba4 2383 } else if (likely(simm != 0)) {
e2be8d8d 2384 tcg_gen_addi_tl(EA, cpu_gpr[rA(ctx->opcode)], simm);
c791fe84 2385 if (NARROW_MODE(ctx)) {
76db3ba4
AJ
2386 tcg_gen_ext32u_tl(EA, EA);
2387 }
76db3ba4 2388 } else {
c791fe84 2389 if (NARROW_MODE(ctx)) {
76db3ba4 2390 tcg_gen_ext32u_tl(EA, cpu_gpr[rA(ctx->opcode)]);
c791fe84
RH
2391 } else {
2392 tcg_gen_mov_tl(EA, cpu_gpr[rA(ctx->opcode)]);
2393 }
76db3ba4 2394 }
76a66253
JM
2395}
2396
636aa200 2397static inline void gen_addr_reg_index(DisasContext *ctx, TCGv EA)
76a66253 2398{
76db3ba4 2399 if (rA(ctx->opcode) == 0) {
c791fe84 2400 if (NARROW_MODE(ctx)) {
76db3ba4 2401 tcg_gen_ext32u_tl(EA, cpu_gpr[rB(ctx->opcode)]);
c791fe84
RH
2402 } else {
2403 tcg_gen_mov_tl(EA, cpu_gpr[rB(ctx->opcode)]);
2404 }
76db3ba4 2405 } else {
e2be8d8d 2406 tcg_gen_add_tl(EA, cpu_gpr[rA(ctx->opcode)], cpu_gpr[rB(ctx->opcode)]);
c791fe84 2407 if (NARROW_MODE(ctx)) {
76db3ba4
AJ
2408 tcg_gen_ext32u_tl(EA, EA);
2409 }
76db3ba4 2410 }
76a66253
JM
2411}
2412
636aa200 2413static inline void gen_addr_register(DisasContext *ctx, TCGv EA)
76a66253 2414{
76db3ba4 2415 if (rA(ctx->opcode) == 0) {
e2be8d8d 2416 tcg_gen_movi_tl(EA, 0);
c791fe84
RH
2417 } else if (NARROW_MODE(ctx)) {
2418 tcg_gen_ext32u_tl(EA, cpu_gpr[rA(ctx->opcode)]);
76db3ba4 2419 } else {
c791fe84 2420 tcg_gen_mov_tl(EA, cpu_gpr[rA(ctx->opcode)]);
76db3ba4
AJ
2421 }
2422}
2423
636aa200
BS
2424static inline void gen_addr_add(DisasContext *ctx, TCGv ret, TCGv arg1,
2425 target_long val)
76db3ba4
AJ
2426{
2427 tcg_gen_addi_tl(ret, arg1, val);
c791fe84 2428 if (NARROW_MODE(ctx)) {
76db3ba4
AJ
2429 tcg_gen_ext32u_tl(ret, ret);
2430 }
76a66253
JM
2431}
2432
636aa200 2433static inline void gen_check_align(DisasContext *ctx, TCGv EA, int mask)
cf360a32
AJ
2434{
2435 int l1 = gen_new_label();
2436 TCGv t0 = tcg_temp_new();
2437 TCGv_i32 t1, t2;
2438 /* NIP cannot be restored if the memory exception comes from an helper */
2439 gen_update_nip(ctx, ctx->nip - 4);
2440 tcg_gen_andi_tl(t0, EA, mask);
2441 tcg_gen_brcondi_tl(TCG_COND_EQ, t0, 0, l1);
2442 t1 = tcg_const_i32(POWERPC_EXCP_ALIGN);
2443 t2 = tcg_const_i32(0);
e5f17ac6 2444 gen_helper_raise_exception_err(cpu_env, t1, t2);
cf360a32
AJ
2445 tcg_temp_free_i32(t1);
2446 tcg_temp_free_i32(t2);
2447 gen_set_label(l1);
2448 tcg_temp_free(t0);
2449}
2450
7863667f 2451/*** Integer load ***/
636aa200 2452static inline void gen_qemu_ld8u(DisasContext *ctx, TCGv arg1, TCGv arg2)
76db3ba4
AJ
2453{
2454 tcg_gen_qemu_ld8u(arg1, arg2, ctx->mem_idx);
2455}
2456
636aa200 2457static inline void gen_qemu_ld8s(DisasContext *ctx, TCGv arg1, TCGv arg2)
76db3ba4
AJ
2458{
2459 tcg_gen_qemu_ld8s(arg1, arg2, ctx->mem_idx);
2460}
2461
636aa200 2462static inline void gen_qemu_ld16u(DisasContext *ctx, TCGv arg1, TCGv arg2)
76db3ba4
AJ
2463{
2464 tcg_gen_qemu_ld16u(arg1, arg2, ctx->mem_idx);
2465 if (unlikely(ctx->le_mode)) {
fa3966a3 2466 tcg_gen_bswap16_tl(arg1, arg1);
76db3ba4 2467 }
b61f2753
AJ
2468}
2469
636aa200 2470static inline void gen_qemu_ld16s(DisasContext *ctx, TCGv arg1, TCGv arg2)
b61f2753 2471{
76db3ba4 2472 if (unlikely(ctx->le_mode)) {
76db3ba4 2473 tcg_gen_qemu_ld16u(arg1, arg2, ctx->mem_idx);
fa3966a3 2474 tcg_gen_bswap16_tl(arg1, arg1);
76db3ba4 2475 tcg_gen_ext16s_tl(arg1, arg1);
76db3ba4
AJ
2476 } else {
2477 tcg_gen_qemu_ld16s(arg1, arg2, ctx->mem_idx);
2478 }
b61f2753
AJ
2479}
2480
636aa200 2481static inline void gen_qemu_ld32u(DisasContext *ctx, TCGv arg1, TCGv arg2)
b61f2753 2482{
76db3ba4
AJ
2483 tcg_gen_qemu_ld32u(arg1, arg2, ctx->mem_idx);
2484 if (unlikely(ctx->le_mode)) {
fa3966a3 2485 tcg_gen_bswap32_tl(arg1, arg1);
76db3ba4 2486 }
b61f2753
AJ
2487}
2488
76db3ba4 2489#if defined(TARGET_PPC64)
636aa200 2490static inline void gen_qemu_ld32s(DisasContext *ctx, TCGv arg1, TCGv arg2)
b61f2753 2491{
a457e7ee 2492 if (unlikely(ctx->le_mode)) {
76db3ba4 2493 tcg_gen_qemu_ld32u(arg1, arg2, ctx->mem_idx);
fa3966a3
AJ
2494 tcg_gen_bswap32_tl(arg1, arg1);
2495 tcg_gen_ext32s_tl(arg1, arg1);
b61f2753 2496 } else
76db3ba4 2497 tcg_gen_qemu_ld32s(arg1, arg2, ctx->mem_idx);
b61f2753 2498}
76db3ba4 2499#endif
b61f2753 2500
636aa200 2501static inline void gen_qemu_ld64(DisasContext *ctx, TCGv_i64 arg1, TCGv arg2)
b61f2753 2502{
76db3ba4
AJ
2503 tcg_gen_qemu_ld64(arg1, arg2, ctx->mem_idx);
2504 if (unlikely(ctx->le_mode)) {
66896cb8 2505 tcg_gen_bswap64_i64(arg1, arg1);
76db3ba4 2506 }
b61f2753
AJ
2507}
2508
636aa200 2509static inline void gen_qemu_st8(DisasContext *ctx, TCGv arg1, TCGv arg2)
b61f2753 2510{
76db3ba4 2511 tcg_gen_qemu_st8(arg1, arg2, ctx->mem_idx);
b61f2753
AJ
2512}
2513
636aa200 2514static inline void gen_qemu_st16(DisasContext *ctx, TCGv arg1, TCGv arg2)
b61f2753 2515{
76db3ba4 2516 if (unlikely(ctx->le_mode)) {
76db3ba4
AJ
2517 TCGv t0 = tcg_temp_new();
2518 tcg_gen_ext16u_tl(t0, arg1);
fa3966a3 2519 tcg_gen_bswap16_tl(t0, t0);
76db3ba4
AJ
2520 tcg_gen_qemu_st16(t0, arg2, ctx->mem_idx);
2521 tcg_temp_free(t0);
76db3ba4
AJ
2522 } else {
2523 tcg_gen_qemu_st16(arg1, arg2, ctx->mem_idx);
2524 }
b61f2753
AJ
2525}
2526
636aa200 2527static inline void gen_qemu_st32(DisasContext *ctx, TCGv arg1, TCGv arg2)
b61f2753 2528{
76db3ba4 2529 if (unlikely(ctx->le_mode)) {
fa3966a3
AJ
2530 TCGv t0 = tcg_temp_new();
2531 tcg_gen_ext32u_tl(t0, arg1);
2532 tcg_gen_bswap32_tl(t0, t0);
76db3ba4
AJ
2533 tcg_gen_qemu_st32(t0, arg2, ctx->mem_idx);
2534 tcg_temp_free(t0);
76db3ba4
AJ
2535 } else {
2536 tcg_gen_qemu_st32(arg1, arg2, ctx->mem_idx);
2537 }
b61f2753
AJ
2538}
2539
636aa200 2540static inline void gen_qemu_st64(DisasContext *ctx, TCGv_i64 arg1, TCGv arg2)
b61f2753 2541{
76db3ba4 2542 if (unlikely(ctx->le_mode)) {
a7812ae4 2543 TCGv_i64 t0 = tcg_temp_new_i64();
66896cb8 2544 tcg_gen_bswap64_i64(t0, arg1);
76db3ba4 2545 tcg_gen_qemu_st64(t0, arg2, ctx->mem_idx);
a7812ae4 2546 tcg_temp_free_i64(t0);
b61f2753 2547 } else
76db3ba4 2548 tcg_gen_qemu_st64(arg1, arg2, ctx->mem_idx);
b61f2753
AJ
2549}
2550
0c8aacd4 2551#define GEN_LD(name, ldop, opc, type) \
99e300ef 2552static void glue(gen_, name)(DisasContext *ctx) \
79aceca5 2553{ \
76db3ba4
AJ
2554 TCGv EA; \
2555 gen_set_access_type(ctx, ACCESS_INT); \
2556 EA = tcg_temp_new(); \
2557 gen_addr_imm_index(ctx, EA, 0); \
2558 gen_qemu_##ldop(ctx, cpu_gpr[rD(ctx->opcode)], EA); \
b61f2753 2559 tcg_temp_free(EA); \
79aceca5
FB
2560}
2561
0c8aacd4 2562#define GEN_LDU(name, ldop, opc, type) \
99e300ef 2563static void glue(gen_, name##u)(DisasContext *ctx) \
79aceca5 2564{ \
b61f2753 2565 TCGv EA; \
76a66253
JM
2566 if (unlikely(rA(ctx->opcode) == 0 || \
2567 rA(ctx->opcode) == rD(ctx->opcode))) { \
e06fcd75 2568 gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL); \
9fddaa0c 2569 return; \
9a64fbe4 2570 } \
76db3ba4 2571 gen_set_access_type(ctx, ACCESS_INT); \
0c8aacd4 2572 EA = tcg_temp_new(); \
9d53c753 2573 if (type == PPC_64B) \
76db3ba4 2574 gen_addr_imm_index(ctx, EA, 0x03); \
9d53c753 2575 else \
76db3ba4
AJ
2576 gen_addr_imm_index(ctx, EA, 0); \
2577 gen_qemu_##ldop(ctx, cpu_gpr[rD(ctx->opcode)], EA); \
b61f2753
AJ
2578 tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], EA); \
2579 tcg_temp_free(EA); \
79aceca5
FB
2580}
2581
0c8aacd4 2582#define GEN_LDUX(name, ldop, opc2, opc3, type) \
99e300ef 2583static void glue(gen_, name##ux)(DisasContext *ctx) \
79aceca5 2584{ \
b61f2753 2585 TCGv EA; \
76a66253
JM
2586 if (unlikely(rA(ctx->opcode) == 0 || \
2587 rA(ctx->opcode) == rD(ctx->opcode))) { \
e06fcd75 2588 gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL); \
9fddaa0c 2589 return; \
9a64fbe4 2590 } \
76db3ba4 2591 gen_set_access_type(ctx, ACCESS_INT); \
0c8aacd4 2592 EA = tcg_temp_new(); \
76db3ba4
AJ
2593 gen_addr_reg_index(ctx, EA); \
2594 gen_qemu_##ldop(ctx, cpu_gpr[rD(ctx->opcode)], EA); \
b61f2753
AJ
2595 tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], EA); \
2596 tcg_temp_free(EA); \
79aceca5
FB
2597}
2598
cd6e9320 2599#define GEN_LDX_E(name, ldop, opc2, opc3, type, type2) \
99e300ef 2600static void glue(gen_, name##x)(DisasContext *ctx) \
79aceca5 2601{ \
76db3ba4
AJ
2602 TCGv EA; \
2603 gen_set_access_type(ctx, ACCESS_INT); \
2604 EA = tcg_temp_new(); \
2605 gen_addr_reg_index(ctx, EA); \
2606 gen_qemu_##ldop(ctx, cpu_gpr[rD(ctx->opcode)], EA); \
b61f2753 2607 tcg_temp_free(EA); \
79aceca5 2608}
cd6e9320
TH
2609#define GEN_LDX(name, ldop, opc2, opc3, type) \
2610 GEN_LDX_E(name, ldop, opc2, opc3, type, PPC_NONE)
79aceca5 2611
0c8aacd4
AJ
2612#define GEN_LDS(name, ldop, op, type) \
2613GEN_LD(name, ldop, op | 0x20, type); \
2614GEN_LDU(name, ldop, op | 0x21, type); \
2615GEN_LDUX(name, ldop, 0x17, op | 0x01, type); \
2616GEN_LDX(name, ldop, 0x17, op | 0x00, type)
79aceca5
FB
2617
2618/* lbz lbzu lbzux lbzx */
0c8aacd4 2619GEN_LDS(lbz, ld8u, 0x02, PPC_INTEGER);
79aceca5 2620/* lha lhau lhaux lhax */
0c8aacd4 2621GEN_LDS(lha, ld16s, 0x0A, PPC_INTEGER);
79aceca5 2622/* lhz lhzu lhzux lhzx */
0c8aacd4 2623GEN_LDS(lhz, ld16u, 0x08, PPC_INTEGER);
79aceca5 2624/* lwz lwzu lwzux lwzx */
0c8aacd4 2625GEN_LDS(lwz, ld32u, 0x00, PPC_INTEGER);
d9bce9d9 2626#if defined(TARGET_PPC64)
d9bce9d9 2627/* lwaux */
0c8aacd4 2628GEN_LDUX(lwa, ld32s, 0x15, 0x0B, PPC_64B);
d9bce9d9 2629/* lwax */
0c8aacd4 2630GEN_LDX(lwa, ld32s, 0x15, 0x0A, PPC_64B);
d9bce9d9 2631/* ldux */
0c8aacd4 2632GEN_LDUX(ld, ld64, 0x15, 0x01, PPC_64B);
d9bce9d9 2633/* ldx */
0c8aacd4 2634GEN_LDX(ld, ld64, 0x15, 0x00, PPC_64B);
99e300ef
BS
2635
2636static void gen_ld(DisasContext *ctx)
d9bce9d9 2637{
b61f2753 2638 TCGv EA;
d9bce9d9
JM
2639 if (Rc(ctx->opcode)) {
2640 if (unlikely(rA(ctx->opcode) == 0 ||
2641 rA(ctx->opcode) == rD(ctx->opcode))) {
e06fcd75 2642 gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL);
d9bce9d9
JM
2643 return;
2644 }
2645 }
76db3ba4 2646 gen_set_access_type(ctx, ACCESS_INT);
a7812ae4 2647 EA = tcg_temp_new();
76db3ba4 2648 gen_addr_imm_index(ctx, EA, 0x03);
d9bce9d9
JM
2649 if (ctx->opcode & 0x02) {
2650 /* lwa (lwau is undefined) */
76db3ba4 2651 gen_qemu_ld32s(ctx, cpu_gpr[rD(ctx->opcode)], EA);
d9bce9d9
JM
2652 } else {
2653 /* ld - ldu */
76db3ba4 2654 gen_qemu_ld64(ctx, cpu_gpr[rD(ctx->opcode)], EA);
d9bce9d9 2655 }
d9bce9d9 2656 if (Rc(ctx->opcode))
b61f2753
AJ
2657 tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], EA);
2658 tcg_temp_free(EA);
d9bce9d9 2659}
99e300ef 2660
54623277 2661/* lq */
99e300ef 2662static void gen_lq(DisasContext *ctx)
be147d08
JM
2663{
2664#if defined(CONFIG_USER_ONLY)
e06fcd75 2665 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
be147d08
JM
2666#else
2667 int ra, rd;
b61f2753 2668 TCGv EA;
be147d08
JM
2669
2670 /* Restore CPU state */
76db3ba4 2671 if (unlikely(ctx->mem_idx == 0)) {
e06fcd75 2672 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
be147d08
JM
2673 return;
2674 }
2675 ra = rA(ctx->opcode);
2676 rd = rD(ctx->opcode);
2677 if (unlikely((rd & 1) || rd == ra)) {
e06fcd75 2678 gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL);
be147d08
JM
2679 return;
2680 }
76db3ba4 2681 if (unlikely(ctx->le_mode)) {
be147d08 2682 /* Little-endian mode is not handled */
e06fcd75 2683 gen_exception_err(ctx, POWERPC_EXCP_ALIGN, POWERPC_EXCP_ALIGN_LE);
be147d08
JM
2684 return;
2685 }
76db3ba4 2686 gen_set_access_type(ctx, ACCESS_INT);
a7812ae4 2687 EA = tcg_temp_new();
76db3ba4
AJ
2688 gen_addr_imm_index(ctx, EA, 0x0F);
2689 gen_qemu_ld64(ctx, cpu_gpr[rd], EA);
2690 gen_addr_add(ctx, EA, EA, 8);
2691 gen_qemu_ld64(ctx, cpu_gpr[rd+1], EA);
b61f2753 2692 tcg_temp_free(EA);
be147d08
JM
2693#endif
2694}
d9bce9d9 2695#endif
79aceca5
FB
2696
2697/*** Integer store ***/
0c8aacd4 2698#define GEN_ST(name, stop, opc, type) \
99e300ef 2699static void glue(gen_, name)(DisasContext *ctx) \
79aceca5 2700{ \
76db3ba4
AJ
2701 TCGv EA; \
2702 gen_set_access_type(ctx, ACCESS_INT); \
2703 EA = tcg_temp_new(); \
2704 gen_addr_imm_index(ctx, EA, 0); \
2705 gen_qemu_##stop(ctx, cpu_gpr[rS(ctx->opcode)], EA); \
b61f2753 2706 tcg_temp_free(EA); \
79aceca5
FB
2707}
2708
0c8aacd4 2709#define GEN_STU(name, stop, opc, type) \
99e300ef 2710static void glue(gen_, stop##u)(DisasContext *ctx) \
79aceca5 2711{ \
b61f2753 2712 TCGv EA; \
76a66253 2713 if (unlikely(rA(ctx->opcode) == 0)) { \
e06fcd75 2714 gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL); \
9fddaa0c 2715 return; \
9a64fbe4 2716 } \
76db3ba4 2717 gen_set_access_type(ctx, ACCESS_INT); \
0c8aacd4 2718 EA = tcg_temp_new(); \
9d53c753 2719 if (type == PPC_64B) \
76db3ba4 2720 gen_addr_imm_index(ctx, EA, 0x03); \
9d53c753 2721 else \
76db3ba4
AJ
2722 gen_addr_imm_index(ctx, EA, 0); \
2723 gen_qemu_##stop(ctx, cpu_gpr[rS(ctx->opcode)], EA); \
b61f2753
AJ
2724 tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], EA); \
2725 tcg_temp_free(EA); \
79aceca5
FB
2726}
2727
0c8aacd4 2728#define GEN_STUX(name, stop, opc2, opc3, type) \
99e300ef 2729static void glue(gen_, name##ux)(DisasContext *ctx) \
79aceca5 2730{ \
b61f2753 2731 TCGv EA; \
76a66253 2732 if (unlikely(rA(ctx->opcode) == 0)) { \
e06fcd75 2733 gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL); \
9fddaa0c 2734 return; \
9a64fbe4 2735 } \
76db3ba4 2736 gen_set_access_type(ctx, ACCESS_INT); \
0c8aacd4 2737 EA = tcg_temp_new(); \
76db3ba4
AJ
2738 gen_addr_reg_index(ctx, EA); \
2739 gen_qemu_##stop(ctx, cpu_gpr[rS(ctx->opcode)], EA); \
b61f2753
AJ
2740 tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], EA); \
2741 tcg_temp_free(EA); \
79aceca5
FB
2742}
2743
cd6e9320
TH
2744#define GEN_STX_E(name, stop, opc2, opc3, type, type2) \
2745static void glue(gen_, name##x)(DisasContext *ctx) \
79aceca5 2746{ \
76db3ba4
AJ
2747 TCGv EA; \
2748 gen_set_access_type(ctx, ACCESS_INT); \
2749 EA = tcg_temp_new(); \
2750 gen_addr_reg_index(ctx, EA); \
2751 gen_qemu_##stop(ctx, cpu_gpr[rS(ctx->opcode)], EA); \
b61f2753 2752 tcg_temp_free(EA); \
79aceca5 2753}
cd6e9320
TH
2754#define GEN_STX(name, stop, opc2, opc3, type) \
2755 GEN_STX_E(name, stop, opc2, opc3, type, PPC_NONE)
79aceca5 2756
0c8aacd4
AJ
2757#define GEN_STS(name, stop, op, type) \
2758GEN_ST(name, stop, op | 0x20, type); \
2759GEN_STU(name, stop, op | 0x21, type); \
2760GEN_STUX(name, stop, 0x17, op | 0x01, type); \
2761GEN_STX(name, stop, 0x17, op | 0x00, type)
79aceca5
FB
2762
2763/* stb stbu stbux stbx */
0c8aacd4 2764GEN_STS(stb, st8, 0x06, PPC_INTEGER);
79aceca5 2765/* sth sthu sthux sthx */
0c8aacd4 2766GEN_STS(sth, st16, 0x0C, PPC_INTEGER);
79aceca5 2767/* stw stwu stwux stwx */
0c8aacd4 2768GEN_STS(stw, st32, 0x04, PPC_INTEGER);
d9bce9d9 2769#if defined(TARGET_PPC64)
0c8aacd4
AJ
2770GEN_STUX(std, st64, 0x15, 0x05, PPC_64B);
2771GEN_STX(std, st64, 0x15, 0x04, PPC_64B);
99e300ef
BS
2772
2773static void gen_std(DisasContext *ctx)
d9bce9d9 2774{
be147d08 2775 int rs;
b61f2753 2776 TCGv EA;
be147d08
JM
2777
2778 rs = rS(ctx->opcode);
2779 if ((ctx->opcode & 0x3) == 0x2) {
2780#if defined(CONFIG_USER_ONLY)
e06fcd75 2781 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
be147d08
JM
2782#else
2783 /* stq */
76db3ba4 2784 if (unlikely(ctx->mem_idx == 0)) {
e06fcd75 2785 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
be147d08
JM
2786 return;
2787 }
2788 if (unlikely(rs & 1)) {
e06fcd75 2789 gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL);
d9bce9d9
JM
2790 return;
2791 }
76db3ba4 2792 if (unlikely(ctx->le_mode)) {
be147d08 2793 /* Little-endian mode is not handled */
e06fcd75 2794 gen_exception_err(ctx, POWERPC_EXCP_ALIGN, POWERPC_EXCP_ALIGN_LE);
be147d08
JM
2795 return;
2796 }
76db3ba4 2797 gen_set_access_type(ctx, ACCESS_INT);
a7812ae4 2798 EA = tcg_temp_new();
76db3ba4
AJ
2799 gen_addr_imm_index(ctx, EA, 0x03);
2800 gen_qemu_st64(ctx, cpu_gpr[rs], EA);
2801 gen_addr_add(ctx, EA, EA, 8);
2802 gen_qemu_st64(ctx, cpu_gpr[rs+1], EA);
b61f2753 2803 tcg_temp_free(EA);
be147d08
JM
2804#endif
2805 } else {
2806 /* std / stdu */
2807 if (Rc(ctx->opcode)) {
2808 if (unlikely(rA(ctx->opcode) == 0)) {
e06fcd75 2809 gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL);
be147d08
JM
2810 return;
2811 }
2812 }
76db3ba4 2813 gen_set_access_type(ctx, ACCESS_INT);
a7812ae4 2814 EA = tcg_temp_new();
76db3ba4
AJ
2815 gen_addr_imm_index(ctx, EA, 0x03);
2816 gen_qemu_st64(ctx, cpu_gpr[rs], EA);
be147d08 2817 if (Rc(ctx->opcode))
b61f2753
AJ
2818 tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], EA);
2819 tcg_temp_free(EA);
d9bce9d9 2820 }
d9bce9d9
JM
2821}
2822#endif
79aceca5
FB
2823/*** Integer load and store with byte reverse ***/
2824/* lhbrx */
86178a57 2825static inline void gen_qemu_ld16ur(DisasContext *ctx, TCGv arg1, TCGv arg2)
b61f2753 2826{
76db3ba4
AJ
2827 tcg_gen_qemu_ld16u(arg1, arg2, ctx->mem_idx);
2828 if (likely(!ctx->le_mode)) {
fa3966a3 2829 tcg_gen_bswap16_tl(arg1, arg1);
76db3ba4 2830 }
b61f2753 2831}
0c8aacd4 2832GEN_LDX(lhbr, ld16ur, 0x16, 0x18, PPC_INTEGER);
b61f2753 2833
79aceca5 2834/* lwbrx */
86178a57 2835static inline void gen_qemu_ld32ur(DisasContext *ctx, TCGv arg1, TCGv arg2)
b61f2753 2836{
76db3ba4
AJ
2837 tcg_gen_qemu_ld32u(arg1, arg2, ctx->mem_idx);
2838 if (likely(!ctx->le_mode)) {
fa3966a3 2839 tcg_gen_bswap32_tl(arg1, arg1);
76db3ba4 2840 }
b61f2753 2841}
0c8aacd4 2842GEN_LDX(lwbr, ld32ur, 0x16, 0x10, PPC_INTEGER);
b61f2753 2843
cd6e9320
TH
2844#if defined(TARGET_PPC64)
2845/* ldbrx */
2846static inline void gen_qemu_ld64ur(DisasContext *ctx, TCGv arg1, TCGv arg2)
2847{
2848 tcg_gen_qemu_ld64(arg1, arg2, ctx->mem_idx);
2849 if (likely(!ctx->le_mode)) {
2850 tcg_gen_bswap64_tl(arg1, arg1);
2851 }
2852}
2853GEN_LDX_E(ldbr, ld64ur, 0x14, 0x10, PPC_NONE, PPC2_DBRX);
2854#endif /* TARGET_PPC64 */
2855
79aceca5 2856/* sthbrx */
86178a57 2857static inline void gen_qemu_st16r(DisasContext *ctx, TCGv arg1, TCGv arg2)
b61f2753 2858{
76db3ba4 2859 if (likely(!ctx->le_mode)) {
76db3ba4
AJ
2860 TCGv t0 = tcg_temp_new();
2861 tcg_gen_ext16u_tl(t0, arg1);
fa3966a3 2862 tcg_gen_bswap16_tl(t0, t0);
76db3ba4
AJ
2863 tcg_gen_qemu_st16(t0, arg2, ctx->mem_idx);
2864 tcg_temp_free(t0);
76db3ba4
AJ
2865 } else {
2866 tcg_gen_qemu_st16(arg1, arg2, ctx->mem_idx);
2867 }
b61f2753 2868}
0c8aacd4 2869GEN_STX(sthbr, st16r, 0x16, 0x1C, PPC_INTEGER);
b61f2753 2870
79aceca5 2871/* stwbrx */
86178a57 2872static inline void gen_qemu_st32r(DisasContext *ctx, TCGv arg1, TCGv arg2)
b61f2753 2873{
76db3ba4 2874 if (likely(!ctx->le_mode)) {
fa3966a3
AJ
2875 TCGv t0 = tcg_temp_new();
2876 tcg_gen_ext32u_tl(t0, arg1);
2877 tcg_gen_bswap32_tl(t0, t0);
76db3ba4
AJ
2878 tcg_gen_qemu_st32(t0, arg2, ctx->mem_idx);
2879 tcg_temp_free(t0);
76db3ba4
AJ
2880 } else {
2881 tcg_gen_qemu_st32(arg1, arg2, ctx->mem_idx);
2882 }
b61f2753 2883}
0c8aacd4 2884GEN_STX(stwbr, st32r, 0x16, 0x14, PPC_INTEGER);
79aceca5 2885
cd6e9320
TH
2886#if defined(TARGET_PPC64)
2887/* stdbrx */
2888static inline void gen_qemu_st64r(DisasContext *ctx, TCGv arg1, TCGv arg2)
2889{
2890 if (likely(!ctx->le_mode)) {
2891 TCGv t0 = tcg_temp_new();
2892 tcg_gen_bswap64_tl(t0, arg1);
2893 tcg_gen_qemu_st64(t0, arg2, ctx->mem_idx);
2894 tcg_temp_free(t0);
2895 } else {
2896 tcg_gen_qemu_st64(arg1, arg2, ctx->mem_idx);
2897 }
2898}
2899GEN_STX_E(stdbr, st64r, 0x14, 0x14, PPC_NONE, PPC2_DBRX);
2900#endif /* TARGET_PPC64 */
2901
79aceca5 2902/*** Integer load and store multiple ***/
99e300ef 2903
54623277 2904/* lmw */
99e300ef 2905static void gen_lmw(DisasContext *ctx)
79aceca5 2906{
76db3ba4
AJ
2907 TCGv t0;
2908 TCGv_i32 t1;
2909 gen_set_access_type(ctx, ACCESS_INT);
76a66253 2910 /* NIP cannot be restored if the memory exception comes from an helper */
d9bce9d9 2911 gen_update_nip(ctx, ctx->nip - 4);
76db3ba4
AJ
2912 t0 = tcg_temp_new();
2913 t1 = tcg_const_i32(rD(ctx->opcode));
2914 gen_addr_imm_index(ctx, t0, 0);
2f5a189c 2915 gen_helper_lmw(cpu_env, t0, t1);
ff4a62cd
AJ
2916 tcg_temp_free(t0);
2917 tcg_temp_free_i32(t1);
79aceca5
FB
2918}
2919
2920/* stmw */
99e300ef 2921static void gen_stmw(DisasContext *ctx)
79aceca5 2922{
76db3ba4
AJ
2923 TCGv t0;
2924 TCGv_i32 t1;
2925 gen_set_access_type(ctx, ACCESS_INT);
76a66253 2926 /* NIP cannot be restored if the memory exception comes from an helper */
d9bce9d9 2927 gen_update_nip(ctx, ctx->nip - 4);
76db3ba4
AJ
2928 t0 = tcg_temp_new();
2929 t1 = tcg_const_i32(rS(ctx->opcode));
2930 gen_addr_imm_index(ctx, t0, 0);
2f5a189c 2931 gen_helper_stmw(cpu_env, t0, t1);
ff4a62cd
AJ
2932 tcg_temp_free(t0);
2933 tcg_temp_free_i32(t1);
79aceca5
FB
2934}
2935
2936/*** Integer load and store strings ***/
54623277 2937
79aceca5 2938/* lswi */
3fc6c082 2939/* PowerPC32 specification says we must generate an exception if
9a64fbe4
FB
2940 * rA is in the range of registers to be loaded.
2941 * In an other hand, IBM says this is valid, but rA won't be loaded.
2942 * For now, I'll follow the spec...
2943 */
99e300ef 2944static void gen_lswi(DisasContext *ctx)
79aceca5 2945{
dfbc799d
AJ
2946 TCGv t0;
2947 TCGv_i32 t1, t2;
79aceca5
FB
2948 int nb = NB(ctx->opcode);
2949 int start = rD(ctx->opcode);
9a64fbe4 2950 int ra = rA(ctx->opcode);
79aceca5
FB
2951 int nr;
2952
2953 if (nb == 0)
2954 nb = 32;
2955 nr = nb / 4;
76a66253
JM
2956 if (unlikely(((start + nr) > 32 &&
2957 start <= ra && (start + nr - 32) > ra) ||
2958 ((start + nr) <= 32 && start <= ra && (start + nr) > ra))) {
e06fcd75 2959 gen_inval_exception(ctx, POWERPC_EXCP_INVAL_LSWX);
9fddaa0c 2960 return;
297d8e62 2961 }
76db3ba4 2962 gen_set_access_type(ctx, ACCESS_INT);
8dd4983c 2963 /* NIP cannot be restored if the memory exception comes from an helper */
d9bce9d9 2964 gen_update_nip(ctx, ctx->nip - 4);
dfbc799d 2965 t0 = tcg_temp_new();
76db3ba4 2966 gen_addr_register(ctx, t0);
dfbc799d
AJ
2967 t1 = tcg_const_i32(nb);
2968 t2 = tcg_const_i32(start);
2f5a189c 2969 gen_helper_lsw(cpu_env, t0, t1, t2);
dfbc799d
AJ
2970 tcg_temp_free(t0);
2971 tcg_temp_free_i32(t1);
2972 tcg_temp_free_i32(t2);
79aceca5
FB
2973}
2974
2975/* lswx */
99e300ef 2976static void gen_lswx(DisasContext *ctx)
79aceca5 2977{
76db3ba4
AJ
2978 TCGv t0;
2979 TCGv_i32 t1, t2, t3;
2980 gen_set_access_type(ctx, ACCESS_INT);
76a66253 2981 /* NIP cannot be restored if the memory exception comes from an helper */
d9bce9d9 2982 gen_update_nip(ctx, ctx->nip - 4);
76db3ba4
AJ
2983 t0 = tcg_temp_new();
2984 gen_addr_reg_index(ctx, t0);
2985 t1 = tcg_const_i32(rD(ctx->opcode));
2986 t2 = tcg_const_i32(rA(ctx->opcode));
2987 t3 = tcg_const_i32(rB(ctx->opcode));
2f5a189c 2988 gen_helper_lswx(cpu_env, t0, t1, t2, t3);
dfbc799d
AJ
2989 tcg_temp_free(t0);
2990 tcg_temp_free_i32(t1);
2991 tcg_temp_free_i32(t2);
2992 tcg_temp_free_i32(t3);
79aceca5
FB
2993}
2994
2995/* stswi */
99e300ef 2996static void gen_stswi(DisasContext *ctx)
79aceca5 2997{
76db3ba4
AJ
2998 TCGv t0;
2999 TCGv_i32 t1, t2;
4b3686fa 3000 int nb = NB(ctx->opcode);
76db3ba4 3001 gen_set_access_type(ctx, ACCESS_INT);
76a66253 3002 /* NIP cannot be restored if the memory exception comes from an helper */
d9bce9d9 3003 gen_update_nip(ctx, ctx->nip - 4);
76db3ba4
AJ
3004 t0 = tcg_temp_new();
3005 gen_addr_register(ctx, t0);
4b3686fa
FB
3006 if (nb == 0)
3007 nb = 32;
dfbc799d 3008 t1 = tcg_const_i32(nb);
76db3ba4 3009 t2 = tcg_const_i32(rS(ctx->opcode));
2f5a189c 3010 gen_helper_stsw(cpu_env, t0, t1, t2);
dfbc799d
AJ
3011 tcg_temp_free(t0);
3012 tcg_temp_free_i32(t1);
3013 tcg_temp_free_i32(t2);
79aceca5
FB
3014}
3015
3016/* stswx */
99e300ef 3017static void gen_stswx(DisasContext *ctx)
79aceca5 3018{
76db3ba4
AJ
3019 TCGv t0;
3020 TCGv_i32 t1, t2;
3021 gen_set_access_type(ctx, ACCESS_INT);
8dd4983c 3022 /* NIP cannot be restored if the memory exception comes from an helper */
5fafdf24 3023 gen_update_nip(ctx, ctx->nip - 4);
76db3ba4
AJ
3024 t0 = tcg_temp_new();
3025 gen_addr_reg_index(ctx, t0);
3026 t1 = tcg_temp_new_i32();
dfbc799d
AJ
3027 tcg_gen_trunc_tl_i32(t1, cpu_xer);
3028 tcg_gen_andi_i32(t1, t1, 0x7F);
76db3ba4 3029 t2 = tcg_const_i32(rS(ctx->opcode));
2f5a189c 3030 gen_helper_stsw(cpu_env, t0, t1, t2);
dfbc799d
AJ
3031 tcg_temp_free(t0);
3032 tcg_temp_free_i32(t1);
3033 tcg_temp_free_i32(t2);
79aceca5
FB
3034}
3035
3036/*** Memory synchronisation ***/
3037/* eieio */
99e300ef 3038static void gen_eieio(DisasContext *ctx)
79aceca5 3039{
79aceca5
FB
3040}
3041
3042/* isync */
99e300ef 3043static void gen_isync(DisasContext *ctx)
79aceca5 3044{
e06fcd75 3045 gen_stop_exception(ctx);
79aceca5
FB
3046}
3047
111bfab3 3048/* lwarx */
99e300ef 3049static void gen_lwarx(DisasContext *ctx)
79aceca5 3050{
76db3ba4 3051 TCGv t0;
18b21a2f 3052 TCGv gpr = cpu_gpr[rD(ctx->opcode)];
76db3ba4
AJ
3053 gen_set_access_type(ctx, ACCESS_RES);
3054 t0 = tcg_temp_local_new();
3055 gen_addr_reg_index(ctx, t0);
cf360a32 3056 gen_check_align(ctx, t0, 0x03);
18b21a2f 3057 gen_qemu_ld32u(ctx, gpr, t0);
cf360a32 3058 tcg_gen_mov_tl(cpu_reserve, t0);
1328c2bf 3059 tcg_gen_st_tl(gpr, cpu_env, offsetof(CPUPPCState, reserve_val));
cf360a32 3060 tcg_temp_free(t0);
79aceca5
FB
3061}
3062
4425265b
NF
3063#if defined(CONFIG_USER_ONLY)
3064static void gen_conditional_store (DisasContext *ctx, TCGv EA,
3065 int reg, int size)
3066{
3067 TCGv t0 = tcg_temp_new();
3068 uint32_t save_exception = ctx->exception;
3069
1328c2bf 3070 tcg_gen_st_tl(EA, cpu_env, offsetof(CPUPPCState, reserve_ea));
4425265b 3071 tcg_gen_movi_tl(t0, (size << 5) | reg);
1328c2bf 3072 tcg_gen_st_tl(t0, cpu_env, offsetof(CPUPPCState, reserve_info));
4425265b
NF
3073 tcg_temp_free(t0);
3074 gen_update_nip(ctx, ctx->nip-4);
3075 ctx->exception = POWERPC_EXCP_BRANCH;
3076 gen_exception(ctx, POWERPC_EXCP_STCX);
3077 ctx->exception = save_exception;
3078}
3079#endif
3080
79aceca5 3081/* stwcx. */
e8eaa2c0 3082static void gen_stwcx_(DisasContext *ctx)
79aceca5 3083{
76db3ba4
AJ
3084 TCGv t0;
3085 gen_set_access_type(ctx, ACCESS_RES);
3086 t0 = tcg_temp_local_new();
3087 gen_addr_reg_index(ctx, t0);
cf360a32 3088 gen_check_align(ctx, t0, 0x03);
4425265b
NF
3089#if defined(CONFIG_USER_ONLY)
3090 gen_conditional_store(ctx, t0, rS(ctx->opcode), 4);
3091#else
3092 {
3093 int l1;
3094
da91a00f 3095 tcg_gen_trunc_tl_i32(cpu_crf[0], cpu_so);
4425265b
NF
3096 l1 = gen_new_label();
3097 tcg_gen_brcond_tl(TCG_COND_NE, t0, cpu_reserve, l1);
3098 tcg_gen_ori_i32(cpu_crf[0], cpu_crf[0], 1 << CRF_EQ);
3099 gen_qemu_st32(ctx, cpu_gpr[rS(ctx->opcode)], t0);
3100 gen_set_label(l1);
3101 tcg_gen_movi_tl(cpu_reserve, -1);
3102 }
3103#endif
cf360a32 3104 tcg_temp_free(t0);
79aceca5
FB
3105}
3106
426613db 3107#if defined(TARGET_PPC64)
426613db 3108/* ldarx */
99e300ef 3109static void gen_ldarx(DisasContext *ctx)
426613db 3110{
76db3ba4 3111 TCGv t0;
18b21a2f 3112 TCGv gpr = cpu_gpr[rD(ctx->opcode)];
76db3ba4
AJ
3113 gen_set_access_type(ctx, ACCESS_RES);
3114 t0 = tcg_temp_local_new();
3115 gen_addr_reg_index(ctx, t0);
cf360a32 3116 gen_check_align(ctx, t0, 0x07);
18b21a2f 3117 gen_qemu_ld64(ctx, gpr, t0);
cf360a32 3118 tcg_gen_mov_tl(cpu_reserve, t0);
1328c2bf 3119 tcg_gen_st_tl(gpr, cpu_env, offsetof(CPUPPCState, reserve_val));
cf360a32 3120 tcg_temp_free(t0);
426613db
JM
3121}
3122
3123/* stdcx. */
e8eaa2c0 3124static void gen_stdcx_(DisasContext *ctx)
426613db 3125{
76db3ba4
AJ
3126 TCGv t0;
3127 gen_set_access_type(ctx, ACCESS_RES);
3128 t0 = tcg_temp_local_new();
3129 gen_addr_reg_index(ctx, t0);
cf360a32 3130 gen_check_align(ctx, t0, 0x07);
4425265b
NF
3131#if defined(CONFIG_USER_ONLY)
3132 gen_conditional_store(ctx, t0, rS(ctx->opcode), 8);
3133#else
3134 {
3135 int l1;
da91a00f 3136 tcg_gen_trunc_tl_i32(cpu_crf[0], cpu_so);
4425265b
NF
3137 l1 = gen_new_label();
3138 tcg_gen_brcond_tl(TCG_COND_NE, t0, cpu_reserve, l1);
3139 tcg_gen_ori_i32(cpu_crf[0], cpu_crf[0], 1 << CRF_EQ);
3140 gen_qemu_st64(ctx, cpu_gpr[rS(ctx->opcode)], t0);
3141 gen_set_label(l1);
3142 tcg_gen_movi_tl(cpu_reserve, -1);
3143 }
3144#endif
cf360a32 3145 tcg_temp_free(t0);
426613db
JM
3146}
3147#endif /* defined(TARGET_PPC64) */
3148
79aceca5 3149/* sync */
99e300ef 3150static void gen_sync(DisasContext *ctx)
79aceca5 3151{
79aceca5
FB
3152}
3153
0db1b20e 3154/* wait */
99e300ef 3155static void gen_wait(DisasContext *ctx)
0db1b20e 3156{
931ff272 3157 TCGv_i32 t0 = tcg_temp_new_i32();
259186a7
AF
3158 tcg_gen_st_i32(t0, cpu_env,
3159 -offsetof(PowerPCCPU, env) + offsetof(CPUState, halted));
931ff272 3160 tcg_temp_free_i32(t0);
0db1b20e 3161 /* Stop translation, as the CPU is supposed to sleep from now */
e06fcd75 3162 gen_exception_err(ctx, EXCP_HLT, 1);
0db1b20e
JM
3163}
3164
79aceca5 3165/*** Floating-point load ***/
a0d7d5a7 3166#define GEN_LDF(name, ldop, opc, type) \
99e300ef 3167static void glue(gen_, name)(DisasContext *ctx) \
79aceca5 3168{ \
a0d7d5a7 3169 TCGv EA; \
76a66253 3170 if (unlikely(!ctx->fpu_enabled)) { \
e06fcd75 3171 gen_exception(ctx, POWERPC_EXCP_FPU); \
4ecc3190
FB
3172 return; \
3173 } \
76db3ba4 3174 gen_set_access_type(ctx, ACCESS_FLOAT); \
a0d7d5a7 3175 EA = tcg_temp_new(); \
76db3ba4
AJ
3176 gen_addr_imm_index(ctx, EA, 0); \
3177 gen_qemu_##ldop(ctx, cpu_fpr[rD(ctx->opcode)], EA); \
a0d7d5a7 3178 tcg_temp_free(EA); \
79aceca5
FB
3179}
3180
a0d7d5a7 3181#define GEN_LDUF(name, ldop, opc, type) \
99e300ef 3182static void glue(gen_, name##u)(DisasContext *ctx) \
79aceca5 3183{ \
a0d7d5a7 3184 TCGv EA; \
76a66253 3185 if (unlikely(!ctx->fpu_enabled)) { \
e06fcd75 3186 gen_exception(ctx, POWERPC_EXCP_FPU); \
4ecc3190
FB
3187 return; \
3188 } \
76a66253 3189 if (unlikely(rA(ctx->opcode) == 0)) { \
e06fcd75 3190 gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL); \
9fddaa0c 3191 return; \
9a64fbe4 3192 } \
76db3ba4 3193 gen_set_access_type(ctx, ACCESS_FLOAT); \
a0d7d5a7 3194 EA = tcg_temp_new(); \
76db3ba4
AJ
3195 gen_addr_imm_index(ctx, EA, 0); \
3196 gen_qemu_##ldop(ctx, cpu_fpr[rD(ctx->opcode)], EA); \
a0d7d5a7
AJ
3197 tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], EA); \
3198 tcg_temp_free(EA); \
79aceca5
FB
3199}
3200
a0d7d5a7 3201#define GEN_LDUXF(name, ldop, opc, type) \
99e300ef 3202static void glue(gen_, name##ux)(DisasContext *ctx) \
79aceca5 3203{ \
a0d7d5a7 3204 TCGv EA; \
76a66253 3205 if (unlikely(!ctx->fpu_enabled)) { \
e06fcd75 3206 gen_exception(ctx, POWERPC_EXCP_FPU); \
4ecc3190
FB
3207 return; \
3208 } \
76a66253 3209 if (unlikely(rA(ctx->opcode) == 0)) { \
e06fcd75 3210 gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL); \
9fddaa0c 3211 return; \
9a64fbe4 3212 } \
76db3ba4 3213 gen_set_access_type(ctx, ACCESS_FLOAT); \
a0d7d5a7 3214 EA = tcg_temp_new(); \
76db3ba4
AJ
3215 gen_addr_reg_index(ctx, EA); \
3216 gen_qemu_##ldop(ctx, cpu_fpr[rD(ctx->opcode)], EA); \
a0d7d5a7
AJ
3217 tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], EA); \
3218 tcg_temp_free(EA); \
79aceca5
FB
3219}
3220
a0d7d5a7 3221#define GEN_LDXF(name, ldop, opc2, opc3, type) \
99e300ef 3222static void glue(gen_, name##x)(DisasContext *ctx) \
79aceca5 3223{ \
a0d7d5a7 3224 TCGv EA; \
76a66253 3225 if (unlikely(!ctx->fpu_enabled)) { \
e06fcd75 3226 gen_exception(ctx, POWERPC_EXCP_FPU); \
4ecc3190
FB
3227 return; \
3228 } \
76db3ba4 3229 gen_set_access_type(ctx, ACCESS_FLOAT); \
a0d7d5a7 3230 EA = tcg_temp_new(); \
76db3ba4
AJ
3231 gen_addr_reg_index(ctx, EA); \
3232 gen_qemu_##ldop(ctx, cpu_fpr[rD(ctx->opcode)], EA); \
a0d7d5a7 3233 tcg_temp_free(EA); \
79aceca5
FB
3234}
3235
a0d7d5a7
AJ
3236#define GEN_LDFS(name, ldop, op, type) \
3237GEN_LDF(name, ldop, op | 0x20, type); \
3238GEN_LDUF(name, ldop, op | 0x21, type); \
3239GEN_LDUXF(name, ldop, op | 0x01, type); \
3240GEN_LDXF(name, ldop, 0x17, op | 0x00, type)
3241
636aa200 3242static inline void gen_qemu_ld32fs(DisasContext *ctx, TCGv_i64 arg1, TCGv arg2)
a0d7d5a7
AJ
3243{
3244 TCGv t0 = tcg_temp_new();
3245 TCGv_i32 t1 = tcg_temp_new_i32();
76db3ba4 3246 gen_qemu_ld32u(ctx, t0, arg2);
a0d7d5a7
AJ
3247 tcg_gen_trunc_tl_i32(t1, t0);
3248 tcg_temp_free(t0);
8e703949 3249 gen_helper_float32_to_float64(arg1, cpu_env, t1);
a0d7d5a7
AJ
3250 tcg_temp_free_i32(t1);
3251}
79aceca5 3252
a0d7d5a7
AJ
3253 /* lfd lfdu lfdux lfdx */
3254GEN_LDFS(lfd, ld64, 0x12, PPC_FLOAT);
3255 /* lfs lfsu lfsux lfsx */
3256GEN_LDFS(lfs, ld32fs, 0x10, PPC_FLOAT);
79aceca5
FB
3257
3258/*** Floating-point store ***/
a0d7d5a7 3259#define GEN_STF(name, stop, opc, type) \
99e300ef 3260static void glue(gen_, name)(DisasContext *ctx) \
79aceca5 3261{ \
a0d7d5a7 3262 TCGv EA; \
76a66253 3263 if (unlikely(!ctx->fpu_enabled)) { \
e06fcd75 3264 gen_exception(ctx, POWERPC_EXCP_FPU); \
4ecc3190
FB
3265 return; \
3266 } \
76db3ba4 3267 gen_set_access_type(ctx, ACCESS_FLOAT); \
a0d7d5a7 3268 EA = tcg_temp_new(); \
76db3ba4
AJ
3269 gen_addr_imm_index(ctx, EA, 0); \
3270 gen_qemu_##stop(ctx, cpu_fpr[rS(ctx->opcode)], EA); \
a0d7d5a7 3271 tcg_temp_free(EA); \
79aceca5
FB
3272}
3273
a0d7d5a7 3274#define GEN_STUF(name, stop, opc, type) \
99e300ef 3275static void glue(gen_, name##u)(DisasContext *ctx) \
79aceca5 3276{ \
a0d7d5a7 3277 TCGv EA; \
76a66253 3278 if (unlikely(!ctx->fpu_enabled)) { \
e06fcd75 3279 gen_exception(ctx, POWERPC_EXCP_FPU); \
4ecc3190
FB
3280 return; \
3281 } \
76a66253 3282 if (unlikely(rA(ctx->opcode) == 0)) { \
e06fcd75 3283 gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL); \
9fddaa0c 3284 return; \
9a64fbe4 3285 } \
76db3ba4 3286 gen_set_access_type(ctx, ACCESS_FLOAT); \
a0d7d5a7 3287 EA = tcg_temp_new(); \
76db3ba4
AJ
3288 gen_addr_imm_index(ctx, EA, 0); \
3289 gen_qemu_##stop(ctx, cpu_fpr[rS(ctx->opcode)], EA); \
a0d7d5a7
AJ
3290 tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], EA); \
3291 tcg_temp_free(EA); \
79aceca5
FB
3292}
3293
a0d7d5a7 3294#define GEN_STUXF(name, stop, opc, type) \
99e300ef 3295static void glue(gen_, name##ux)(DisasContext *ctx) \
79aceca5 3296{ \
a0d7d5a7 3297 TCGv EA; \
76a66253 3298 if (unlikely(!ctx->fpu_enabled)) { \
e06fcd75 3299 gen_exception(ctx, POWERPC_EXCP_FPU); \
4ecc3190
FB
3300 return; \
3301 } \
76a66253 3302 if (unlikely(rA(ctx->opcode) == 0)) { \
e06fcd75 3303 gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL); \
9fddaa0c 3304 return; \
9a64fbe4 3305 } \
76db3ba4 3306 gen_set_access_type(ctx, ACCESS_FLOAT); \
a0d7d5a7 3307 EA = tcg_temp_new(); \
76db3ba4
AJ
3308 gen_addr_reg_index(ctx, EA); \
3309 gen_qemu_##stop(ctx, cpu_fpr[rS(ctx->opcode)], EA); \
a0d7d5a7
AJ
3310 tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], EA); \
3311 tcg_temp_free(EA); \
79aceca5
FB
3312}
3313
a0d7d5a7 3314#define GEN_STXF(name, stop, opc2, opc3, type) \
99e300ef 3315static void glue(gen_, name##x)(DisasContext *ctx) \
79aceca5 3316{ \
a0d7d5a7 3317 TCGv EA; \
76a66253 3318 if (unlikely(!ctx->fpu_enabled)) { \
e06fcd75 3319 gen_exception(ctx, POWERPC_EXCP_FPU); \
4ecc3190
FB
3320 return; \
3321 } \
76db3ba4 3322 gen_set_access_type(ctx, ACCESS_FLOAT); \
a0d7d5a7 3323 EA = tcg_temp_new(); \
76db3ba4
AJ
3324 gen_addr_reg_index(ctx, EA); \
3325 gen_qemu_##stop(ctx, cpu_fpr[rS(ctx->opcode)], EA); \
a0d7d5a7 3326 tcg_temp_free(EA); \
79aceca5
FB
3327}
3328
a0d7d5a7
AJ
3329#define GEN_STFS(name, stop, op, type) \
3330GEN_STF(name, stop, op | 0x20, type); \
3331GEN_STUF(name, stop, op | 0x21, type); \
3332GEN_STUXF(name, stop, op | 0x01, type); \
3333GEN_STXF(name, stop, 0x17, op | 0x00, type)
3334
636aa200 3335static inline void gen_qemu_st32fs(DisasContext *ctx, TCGv_i64 arg1, TCGv arg2)
a0d7d5a7
AJ
3336{
3337 TCGv_i32 t0 = tcg_temp_new_i32();
3338 TCGv t1 = tcg_temp_new();
8e703949 3339 gen_helper_float64_to_float32(t0, cpu_env, arg1);
a0d7d5a7
AJ
3340 tcg_gen_extu_i32_tl(t1, t0);
3341 tcg_temp_free_i32(t0);
76db3ba4 3342 gen_qemu_st32(ctx, t1, arg2);
a0d7d5a7
AJ
3343 tcg_temp_free(t1);
3344}
79aceca5
FB
3345
3346/* stfd stfdu stfdux stfdx */
a0d7d5a7 3347GEN_STFS(stfd, st64, 0x16, PPC_FLOAT);
79aceca5 3348/* stfs stfsu stfsux stfsx */
a0d7d5a7 3349GEN_STFS(stfs, st32fs, 0x14, PPC_FLOAT);
79aceca5
FB
3350
3351/* Optional: */
636aa200 3352static inline void gen_qemu_st32fiw(DisasContext *ctx, TCGv_i64 arg1, TCGv arg2)
a0d7d5a7
AJ
3353{
3354 TCGv t0 = tcg_temp_new();
3355 tcg_gen_trunc_i64_tl(t0, arg1),
76db3ba4 3356 gen_qemu_st32(ctx, t0, arg2);
a0d7d5a7
AJ
3357 tcg_temp_free(t0);
3358}
79aceca5 3359/* stfiwx */
a0d7d5a7 3360GEN_STXF(stfiw, st32fiw, 0x17, 0x1E, PPC_FLOAT_STFIWX);
79aceca5 3361
697ab892
DG
3362static inline void gen_update_cfar(DisasContext *ctx, target_ulong nip)
3363{
3364#if defined(TARGET_PPC64)
3365 if (ctx->has_cfar)
3366 tcg_gen_movi_tl(cpu_cfar, nip);
3367#endif
3368}
3369
79aceca5 3370/*** Branch ***/
636aa200 3371static inline void gen_goto_tb(DisasContext *ctx, int n, target_ulong dest)
c1942362
FB
3372{
3373 TranslationBlock *tb;
3374 tb = ctx->tb;
e0c8f9ce 3375 if (NARROW_MODE(ctx)) {
a2ffb812 3376 dest = (uint32_t) dest;
e0c8f9ce 3377 }
57fec1fe 3378 if ((tb->pc & TARGET_PAGE_MASK) == (dest & TARGET_PAGE_MASK) &&
8cbcb4fa 3379 likely(!ctx->singlestep_enabled)) {
57fec1fe 3380 tcg_gen_goto_tb(n);
a2ffb812 3381 tcg_gen_movi_tl(cpu_nip, dest & ~3);
4b4a72e5 3382 tcg_gen_exit_tb((tcg_target_long)tb + n);
c1942362 3383 } else {
a2ffb812 3384 tcg_gen_movi_tl(cpu_nip, dest & ~3);
8cbcb4fa
AJ
3385 if (unlikely(ctx->singlestep_enabled)) {
3386 if ((ctx->singlestep_enabled &
bdc4e053 3387 (CPU_BRANCH_STEP | CPU_SINGLE_STEP)) &&
f0cc4aa8
JG
3388 (ctx->exception == POWERPC_EXCP_BRANCH ||
3389 ctx->exception == POWERPC_EXCP_TRACE)) {
8cbcb4fa
AJ
3390 target_ulong tmp = ctx->nip;
3391 ctx->nip = dest;
e06fcd75 3392 gen_exception(ctx, POWERPC_EXCP_TRACE);
8cbcb4fa
AJ
3393 ctx->nip = tmp;
3394 }
3395 if (ctx->singlestep_enabled & GDBSTUB_SINGLE_STEP) {
e06fcd75 3396 gen_debug_exception(ctx);
8cbcb4fa
AJ
3397 }
3398 }
57fec1fe 3399 tcg_gen_exit_tb(0);
c1942362 3400 }
c53be334
FB
3401}
3402
636aa200 3403static inline void gen_setlr(DisasContext *ctx, target_ulong nip)
e1833e1f 3404{
e0c8f9ce
RH
3405 if (NARROW_MODE(ctx)) {
3406 nip = (uint32_t)nip;
3407 }
3408 tcg_gen_movi_tl(cpu_lr, nip);
e1833e1f
JM
3409}
3410
79aceca5 3411/* b ba bl bla */
99e300ef 3412static void gen_b(DisasContext *ctx)
79aceca5 3413{
76a66253 3414 target_ulong li, target;
38a64f9d 3415
8cbcb4fa 3416 ctx->exception = POWERPC_EXCP_BRANCH;
38a64f9d 3417 /* sign extend LI */
e0c8f9ce
RH
3418 li = LI(ctx->opcode);
3419 li = (li ^ 0x02000000) - 0x02000000;
3420 if (likely(AA(ctx->opcode) == 0)) {
046d6672 3421 target = ctx->nip + li - 4;
e0c8f9ce 3422 } else {
9a64fbe4 3423 target = li;
e0c8f9ce
RH
3424 }
3425 if (LK(ctx->opcode)) {
e1833e1f 3426 gen_setlr(ctx, ctx->nip);
e0c8f9ce 3427 }
697ab892 3428 gen_update_cfar(ctx, ctx->nip);
c1942362 3429 gen_goto_tb(ctx, 0, target);
79aceca5
FB
3430}
3431
e98a6e40
FB
3432#define BCOND_IM 0
3433#define BCOND_LR 1
3434#define BCOND_CTR 2
3435
636aa200 3436static inline void gen_bcond(DisasContext *ctx, int type)
d9bce9d9 3437{
d9bce9d9 3438 uint32_t bo = BO(ctx->opcode);
05f92404 3439 int l1;
a2ffb812 3440 TCGv target;
e98a6e40 3441
8cbcb4fa 3442 ctx->exception = POWERPC_EXCP_BRANCH;
a2ffb812 3443 if (type == BCOND_LR || type == BCOND_CTR) {
a7812ae4 3444 target = tcg_temp_local_new();
a2ffb812
AJ
3445 if (type == BCOND_CTR)
3446 tcg_gen_mov_tl(target, cpu_ctr);
3447 else
3448 tcg_gen_mov_tl(target, cpu_lr);
d2e9fd8f 3449 } else {
3450 TCGV_UNUSED(target);
e98a6e40 3451 }
e1833e1f
JM
3452 if (LK(ctx->opcode))
3453 gen_setlr(ctx, ctx->nip);
a2ffb812
AJ
3454 l1 = gen_new_label();
3455 if ((bo & 0x4) == 0) {
3456 /* Decrement and test CTR */
a7812ae4 3457 TCGv temp = tcg_temp_new();
a2ffb812 3458 if (unlikely(type == BCOND_CTR)) {
e06fcd75 3459 gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL);
a2ffb812
AJ
3460 return;
3461 }
3462 tcg_gen_subi_tl(cpu_ctr, cpu_ctr, 1);
e0c8f9ce 3463 if (NARROW_MODE(ctx)) {
a2ffb812 3464 tcg_gen_ext32u_tl(temp, cpu_ctr);
e0c8f9ce 3465 } else {
a2ffb812 3466 tcg_gen_mov_tl(temp, cpu_ctr);
e0c8f9ce 3467 }
a2ffb812
AJ
3468 if (bo & 0x2) {
3469 tcg_gen_brcondi_tl(TCG_COND_NE, temp, 0, l1);
3470 } else {
3471 tcg_gen_brcondi_tl(TCG_COND_EQ, temp, 0, l1);
e98a6e40 3472 }
a7812ae4 3473 tcg_temp_free(temp);
a2ffb812
AJ
3474 }
3475 if ((bo & 0x10) == 0) {
3476 /* Test CR */
3477 uint32_t bi = BI(ctx->opcode);
3478 uint32_t mask = 1 << (3 - (bi & 0x03));
a7812ae4 3479 TCGv_i32 temp = tcg_temp_new_i32();
a2ffb812 3480
d9bce9d9 3481 if (bo & 0x8) {
a2ffb812
AJ
3482 tcg_gen_andi_i32(temp, cpu_crf[bi >> 2], mask);
3483 tcg_gen_brcondi_i32(TCG_COND_EQ, temp, 0, l1);
d9bce9d9 3484 } else {
a2ffb812
AJ
3485 tcg_gen_andi_i32(temp, cpu_crf[bi >> 2], mask);
3486 tcg_gen_brcondi_i32(TCG_COND_NE, temp, 0, l1);
d9bce9d9 3487 }
a7812ae4 3488 tcg_temp_free_i32(temp);
d9bce9d9 3489 }
697ab892 3490 gen_update_cfar(ctx, ctx->nip);
e98a6e40 3491 if (type == BCOND_IM) {
a2ffb812
AJ
3492 target_ulong li = (target_long)((int16_t)(BD(ctx->opcode)));
3493 if (likely(AA(ctx->opcode) == 0)) {
3494 gen_goto_tb(ctx, 0, ctx->nip + li - 4);
3495 } else {
3496 gen_goto_tb(ctx, 0, li);
3497 }
c53be334 3498 gen_set_label(l1);
c1942362 3499 gen_goto_tb(ctx, 1, ctx->nip);
e98a6e40 3500 } else {
e0c8f9ce 3501 if (NARROW_MODE(ctx)) {
a2ffb812 3502 tcg_gen_andi_tl(cpu_nip, target, (uint32_t)~3);
e0c8f9ce 3503 } else {
a2ffb812 3504 tcg_gen_andi_tl(cpu_nip, target, ~3);
e0c8f9ce 3505 }
a2ffb812
AJ
3506 tcg_gen_exit_tb(0);
3507 gen_set_label(l1);
e0c8f9ce 3508 gen_update_nip(ctx, ctx->nip);
57fec1fe 3509 tcg_gen_exit_tb(0);
08e46e54 3510 }
e98a6e40
FB
3511}
3512
99e300ef 3513static void gen_bc(DisasContext *ctx)
3b46e624 3514{
e98a6e40
FB
3515 gen_bcond(ctx, BCOND_IM);
3516}
3517
99e300ef 3518static void gen_bcctr(DisasContext *ctx)
3b46e624 3519{
e98a6e40
FB
3520 gen_bcond(ctx, BCOND_CTR);
3521}
3522
99e300ef 3523static void gen_bclr(DisasContext *ctx)
3b46e624 3524{
e98a6e40
FB
3525 gen_bcond(ctx, BCOND_LR);
3526}
79aceca5
FB
3527
3528/*** Condition register logical ***/
e1571908 3529#define GEN_CRLOGIC(name, tcg_op, opc) \
99e300ef 3530static void glue(gen_, name)(DisasContext *ctx) \
79aceca5 3531{ \
fc0d441e
JM
3532 uint8_t bitmask; \
3533 int sh; \
a7812ae4 3534 TCGv_i32 t0, t1; \
fc0d441e 3535 sh = (crbD(ctx->opcode) & 0x03) - (crbA(ctx->opcode) & 0x03); \
a7812ae4 3536 t0 = tcg_temp_new_i32(); \
fc0d441e 3537 if (sh > 0) \
fea0c503 3538 tcg_gen_shri_i32(t0, cpu_crf[crbA(ctx->opcode) >> 2], sh); \
fc0d441e 3539 else if (sh < 0) \
fea0c503 3540 tcg_gen_shli_i32(t0, cpu_crf[crbA(ctx->opcode) >> 2], -sh); \
e1571908 3541 else \
fea0c503 3542 tcg_gen_mov_i32(t0, cpu_crf[crbA(ctx->opcode) >> 2]); \
a7812ae4 3543 t1 = tcg_temp_new_i32(); \
fc0d441e
JM
3544 sh = (crbD(ctx->opcode) & 0x03) - (crbB(ctx->opcode) & 0x03); \
3545 if (sh > 0) \
fea0c503 3546 tcg_gen_shri_i32(t1, cpu_crf[crbB(ctx->opcode) >> 2], sh); \
fc0d441e 3547 else if (sh < 0) \
fea0c503 3548 tcg_gen_shli_i32(t1, cpu_crf[crbB(ctx->opcode) >> 2], -sh); \
e1571908 3549 else \
fea0c503
AJ
3550 tcg_gen_mov_i32(t1, cpu_crf[crbB(ctx->opcode) >> 2]); \
3551 tcg_op(t0, t0, t1); \
fc0d441e 3552 bitmask = 1 << (3 - (crbD(ctx->opcode) & 0x03)); \
fea0c503
AJ
3553 tcg_gen_andi_i32(t0, t0, bitmask); \
3554 tcg_gen_andi_i32(t1, cpu_crf[crbD(ctx->opcode) >> 2], ~bitmask); \
3555 tcg_gen_or_i32(cpu_crf[crbD(ctx->opcode) >> 2], t0, t1); \
a7812ae4
PB
3556 tcg_temp_free_i32(t0); \
3557 tcg_temp_free_i32(t1); \
79aceca5
FB
3558}
3559
3560/* crand */
e1571908 3561GEN_CRLOGIC(crand, tcg_gen_and_i32, 0x08);
79aceca5 3562/* crandc */
e1571908 3563GEN_CRLOGIC(crandc, tcg_gen_andc_i32, 0x04);
79aceca5 3564/* creqv */
e1571908 3565GEN_CRLOGIC(creqv, tcg_gen_eqv_i32, 0x09);
79aceca5 3566/* crnand */
e1571908 3567GEN_CRLOGIC(crnand, tcg_gen_nand_i32, 0x07);
79aceca5 3568/* crnor */
e1571908 3569GEN_CRLOGIC(crnor, tcg_gen_nor_i32, 0x01);
79aceca5 3570/* cror */
e1571908 3571GEN_CRLOGIC(cror, tcg_gen_or_i32, 0x0E);
79aceca5 3572/* crorc */
e1571908 3573GEN_CRLOGIC(crorc, tcg_gen_orc_i32, 0x0D);
79aceca5 3574/* crxor */
e1571908 3575GEN_CRLOGIC(crxor, tcg_gen_xor_i32, 0x06);
99e300ef 3576
54623277 3577/* mcrf */
99e300ef 3578static void gen_mcrf(DisasContext *ctx)
79aceca5 3579{
47e4661c 3580 tcg_gen_mov_i32(cpu_crf[crfD(ctx->opcode)], cpu_crf[crfS(ctx->opcode)]);
79aceca5
FB
3581}
3582
3583/*** System linkage ***/
99e300ef 3584
54623277 3585/* rfi (mem_idx only) */
99e300ef 3586static void gen_rfi(DisasContext *ctx)
79aceca5 3587{
9a64fbe4 3588#if defined(CONFIG_USER_ONLY)
e06fcd75 3589 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
9a64fbe4
FB
3590#else
3591 /* Restore CPU state */
76db3ba4 3592 if (unlikely(!ctx->mem_idx)) {
e06fcd75 3593 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
9fddaa0c 3594 return;
9a64fbe4 3595 }
697ab892 3596 gen_update_cfar(ctx, ctx->nip);
e5f17ac6 3597 gen_helper_rfi(cpu_env);
e06fcd75 3598 gen_sync_exception(ctx);
9a64fbe4 3599#endif
79aceca5
FB
3600}
3601
426613db 3602#if defined(TARGET_PPC64)
99e300ef 3603static void gen_rfid(DisasContext *ctx)
426613db
JM
3604{
3605#if defined(CONFIG_USER_ONLY)
e06fcd75 3606 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
426613db
JM
3607#else
3608 /* Restore CPU state */
76db3ba4 3609 if (unlikely(!ctx->mem_idx)) {
e06fcd75 3610 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
426613db
JM
3611 return;
3612 }
697ab892 3613 gen_update_cfar(ctx, ctx->nip);
e5f17ac6 3614 gen_helper_rfid(cpu_env);
e06fcd75 3615 gen_sync_exception(ctx);
426613db
JM
3616#endif
3617}
426613db 3618
99e300ef 3619static void gen_hrfid(DisasContext *ctx)
be147d08
JM
3620{
3621#if defined(CONFIG_USER_ONLY)
e06fcd75 3622 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
be147d08
JM
3623#else
3624 /* Restore CPU state */
76db3ba4 3625 if (unlikely(ctx->mem_idx <= 1)) {
e06fcd75 3626 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
be147d08
JM
3627 return;
3628 }
e5f17ac6 3629 gen_helper_hrfid(cpu_env);
e06fcd75 3630 gen_sync_exception(ctx);
be147d08
JM
3631#endif
3632}
3633#endif
3634
79aceca5 3635/* sc */
417bf010
JM
3636#if defined(CONFIG_USER_ONLY)
3637#define POWERPC_SYSCALL POWERPC_EXCP_SYSCALL_USER
3638#else
3639#define POWERPC_SYSCALL POWERPC_EXCP_SYSCALL
3640#endif
99e300ef 3641static void gen_sc(DisasContext *ctx)
79aceca5 3642{
e1833e1f
JM
3643 uint32_t lev;
3644
3645 lev = (ctx->opcode >> 5) & 0x7F;
e06fcd75 3646 gen_exception_err(ctx, POWERPC_SYSCALL, lev);
79aceca5
FB
3647}
3648
3649/*** Trap ***/
99e300ef 3650
54623277 3651/* tw */
99e300ef 3652static void gen_tw(DisasContext *ctx)
79aceca5 3653{
cab3bee2 3654 TCGv_i32 t0 = tcg_const_i32(TO(ctx->opcode));
db9a231d
AJ
3655 /* Update the nip since this might generate a trap exception */
3656 gen_update_nip(ctx, ctx->nip);
e5f17ac6
BS
3657 gen_helper_tw(cpu_env, cpu_gpr[rA(ctx->opcode)], cpu_gpr[rB(ctx->opcode)],
3658 t0);
cab3bee2 3659 tcg_temp_free_i32(t0);
79aceca5
FB
3660}
3661
3662/* twi */
99e300ef 3663static void gen_twi(DisasContext *ctx)
79aceca5 3664{
cab3bee2
AJ
3665 TCGv t0 = tcg_const_tl(SIMM(ctx->opcode));
3666 TCGv_i32 t1 = tcg_const_i32(TO(ctx->opcode));
db9a231d
AJ
3667 /* Update the nip since this might generate a trap exception */
3668 gen_update_nip(ctx, ctx->nip);
e5f17ac6 3669 gen_helper_tw(cpu_env, cpu_gpr[rA(ctx->opcode)], t0, t1);
cab3bee2
AJ
3670 tcg_temp_free(t0);
3671 tcg_temp_free_i32(t1);
79aceca5
FB
3672}
3673
d9bce9d9
JM
3674#if defined(TARGET_PPC64)
3675/* td */
99e300ef 3676static void gen_td(DisasContext *ctx)
d9bce9d9 3677{
cab3bee2 3678 TCGv_i32 t0 = tcg_const_i32(TO(ctx->opcode));
db9a231d
AJ
3679 /* Update the nip since this might generate a trap exception */
3680 gen_update_nip(ctx, ctx->nip);
e5f17ac6
BS
3681 gen_helper_td(cpu_env, cpu_gpr[rA(ctx->opcode)], cpu_gpr[rB(ctx->opcode)],
3682 t0);
cab3bee2 3683 tcg_temp_free_i32(t0);
d9bce9d9
JM
3684}
3685
3686/* tdi */
99e300ef 3687static void gen_tdi(DisasContext *ctx)
d9bce9d9 3688{
cab3bee2
AJ
3689 TCGv t0 = tcg_const_tl(SIMM(ctx->opcode));
3690 TCGv_i32 t1 = tcg_const_i32(TO(ctx->opcode));
db9a231d
AJ
3691 /* Update the nip since this might generate a trap exception */
3692 gen_update_nip(ctx, ctx->nip);
e5f17ac6 3693 gen_helper_td(cpu_env, cpu_gpr[rA(ctx->opcode)], t0, t1);
cab3bee2
AJ
3694 tcg_temp_free(t0);
3695 tcg_temp_free_i32(t1);
d9bce9d9
JM
3696}
3697#endif
3698
79aceca5 3699/*** Processor control ***/
99e300ef 3700
da91a00f
RH
3701static void gen_read_xer(TCGv dst)
3702{
3703 TCGv t0 = tcg_temp_new();
3704 TCGv t1 = tcg_temp_new();
3705 TCGv t2 = tcg_temp_new();
3706 tcg_gen_mov_tl(dst, cpu_xer);
3707 tcg_gen_shli_tl(t0, cpu_so, XER_SO);
3708 tcg_gen_shli_tl(t1, cpu_ov, XER_OV);
3709 tcg_gen_shli_tl(t2, cpu_ca, XER_CA);
3710 tcg_gen_or_tl(t0, t0, t1);
3711 tcg_gen_or_tl(dst, dst, t2);
3712 tcg_gen_or_tl(dst, dst, t0);
3713 tcg_temp_free(t0);
3714 tcg_temp_free(t1);
3715 tcg_temp_free(t2);
3716}
3717
3718static void gen_write_xer(TCGv src)
3719{
3720 tcg_gen_andi_tl(cpu_xer, src,
3721 ~((1u << XER_SO) | (1u << XER_OV) | (1u << XER_CA)));
3722 tcg_gen_shri_tl(cpu_so, src, XER_SO);
3723 tcg_gen_shri_tl(cpu_ov, src, XER_OV);
3724 tcg_gen_shri_tl(cpu_ca, src, XER_CA);
3725 tcg_gen_andi_tl(cpu_so, cpu_so, 1);
3726 tcg_gen_andi_tl(cpu_ov, cpu_ov, 1);
3727 tcg_gen_andi_tl(cpu_ca, cpu_ca, 1);
3728}
3729
54623277 3730/* mcrxr */
99e300ef 3731static void gen_mcrxr(DisasContext *ctx)
79aceca5 3732{
da91a00f
RH
3733 TCGv_i32 t0 = tcg_temp_new_i32();
3734 TCGv_i32 t1 = tcg_temp_new_i32();
3735 TCGv_i32 dst = cpu_crf[crfD(ctx->opcode)];
3736
3737 tcg_gen_trunc_tl_i32(t0, cpu_so);
3738 tcg_gen_trunc_tl_i32(t1, cpu_ov);
3739 tcg_gen_trunc_tl_i32(dst, cpu_ca);
3740 tcg_gen_shri_i32(t0, t0, 2);
3741 tcg_gen_shri_i32(t1, t1, 1);
3742 tcg_gen_or_i32(dst, dst, t0);
3743 tcg_gen_or_i32(dst, dst, t1);
3744 tcg_temp_free_i32(t0);
3745 tcg_temp_free_i32(t1);
3746
3747 tcg_gen_movi_tl(cpu_so, 0);
3748 tcg_gen_movi_tl(cpu_ov, 0);
3749 tcg_gen_movi_tl(cpu_ca, 0);
79aceca5
FB
3750}
3751
0cfe11ea 3752/* mfcr mfocrf */
99e300ef 3753static void gen_mfcr(DisasContext *ctx)
79aceca5 3754{
76a66253 3755 uint32_t crm, crn;
3b46e624 3756
76a66253
JM
3757 if (likely(ctx->opcode & 0x00100000)) {
3758 crm = CRM(ctx->opcode);
8dd640e4 3759 if (likely(crm && ((crm & (crm - 1)) == 0))) {
0cfe11ea 3760 crn = ctz32 (crm);
e1571908 3761 tcg_gen_extu_i32_tl(cpu_gpr[rD(ctx->opcode)], cpu_crf[7 - crn]);
0497d2f4
AJ
3762 tcg_gen_shli_tl(cpu_gpr[rD(ctx->opcode)],
3763 cpu_gpr[rD(ctx->opcode)], crn * 4);
76a66253 3764 }
d9bce9d9 3765 } else {
651721b2
AJ
3766 TCGv_i32 t0 = tcg_temp_new_i32();
3767 tcg_gen_mov_i32(t0, cpu_crf[0]);
3768 tcg_gen_shli_i32(t0, t0, 4);
3769 tcg_gen_or_i32(t0, t0, cpu_crf[1]);
3770 tcg_gen_shli_i32(t0, t0, 4);
3771 tcg_gen_or_i32(t0, t0, cpu_crf[2]);
3772 tcg_gen_shli_i32(t0, t0, 4);
3773 tcg_gen_or_i32(t0, t0, cpu_crf[3]);
3774 tcg_gen_shli_i32(t0, t0, 4);
3775 tcg_gen_or_i32(t0, t0, cpu_crf[4]);
3776 tcg_gen_shli_i32(t0, t0, 4);
3777 tcg_gen_or_i32(t0, t0, cpu_crf[5]);
3778 tcg_gen_shli_i32(t0, t0, 4);
3779 tcg_gen_or_i32(t0, t0, cpu_crf[6]);
3780 tcg_gen_shli_i32(t0, t0, 4);
3781 tcg_gen_or_i32(t0, t0, cpu_crf[7]);
3782 tcg_gen_extu_i32_tl(cpu_gpr[rD(ctx->opcode)], t0);
3783 tcg_temp_free_i32(t0);
d9bce9d9 3784 }
79aceca5
FB
3785}
3786
3787/* mfmsr */
99e300ef 3788static void gen_mfmsr(DisasContext *ctx)
79aceca5 3789{
9a64fbe4 3790#if defined(CONFIG_USER_ONLY)
e06fcd75 3791 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
9a64fbe4 3792#else
76db3ba4 3793 if (unlikely(!ctx->mem_idx)) {
e06fcd75 3794 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
9fddaa0c 3795 return;
9a64fbe4 3796 }
6527f6ea 3797 tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_msr);
9a64fbe4 3798#endif
79aceca5
FB
3799}
3800
7b13448f 3801static void spr_noaccess(void *opaque, int gprn, int sprn)
3fc6c082 3802{
7b13448f 3803#if 0
3fc6c082
FB
3804 sprn = ((sprn >> 5) & 0x1F) | ((sprn & 0x1F) << 5);
3805 printf("ERROR: try to access SPR %d !\n", sprn);
7b13448f 3806#endif
3fc6c082
FB
3807}
3808#define SPR_NOACCESS (&spr_noaccess)
3fc6c082 3809
79aceca5 3810/* mfspr */
636aa200 3811static inline void gen_op_mfspr(DisasContext *ctx)
79aceca5 3812{
45d827d2 3813 void (*read_cb)(void *opaque, int gprn, int sprn);
79aceca5
FB
3814 uint32_t sprn = SPR(ctx->opcode);
3815
3fc6c082 3816#if !defined(CONFIG_USER_ONLY)
76db3ba4 3817 if (ctx->mem_idx == 2)
be147d08 3818 read_cb = ctx->spr_cb[sprn].hea_read;
76db3ba4 3819 else if (ctx->mem_idx)
3fc6c082
FB
3820 read_cb = ctx->spr_cb[sprn].oea_read;
3821 else
9a64fbe4 3822#endif
3fc6c082 3823 read_cb = ctx->spr_cb[sprn].uea_read;
76a66253
JM
3824 if (likely(read_cb != NULL)) {
3825 if (likely(read_cb != SPR_NOACCESS)) {
45d827d2 3826 (*read_cb)(ctx, rD(ctx->opcode), sprn);
3fc6c082
FB
3827 } else {
3828 /* Privilege exception */
9fceefa7
JM
3829 /* This is a hack to avoid warnings when running Linux:
3830 * this OS breaks the PowerPC virtualisation model,
3831 * allowing userland application to read the PVR
3832 */
3833 if (sprn != SPR_PVR) {
93fcfe39 3834 qemu_log("Trying to read privileged spr %d %03x at "
90e189ec
BS
3835 TARGET_FMT_lx "\n", sprn, sprn, ctx->nip);
3836 printf("Trying to read privileged spr %d %03x at "
3837 TARGET_FMT_lx "\n", sprn, sprn, ctx->nip);
f24e5695 3838 }
e06fcd75 3839 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
79aceca5 3840 }
3fc6c082
FB
3841 } else {
3842 /* Not defined */
93fcfe39 3843 qemu_log("Trying to read invalid spr %d %03x at "
90e189ec
BS
3844 TARGET_FMT_lx "\n", sprn, sprn, ctx->nip);
3845 printf("Trying to read invalid spr %d %03x at " TARGET_FMT_lx "\n",
077fc206 3846 sprn, sprn, ctx->nip);
e06fcd75 3847 gen_inval_exception(ctx, POWERPC_EXCP_INVAL_SPR);
79aceca5 3848 }
79aceca5
FB
3849}
3850
99e300ef 3851static void gen_mfspr(DisasContext *ctx)
79aceca5 3852{
3fc6c082 3853 gen_op_mfspr(ctx);
76a66253 3854}
3fc6c082
FB
3855
3856/* mftb */
99e300ef 3857static void gen_mftb(DisasContext *ctx)
3fc6c082
FB
3858{
3859 gen_op_mfspr(ctx);
79aceca5
FB
3860}
3861
0cfe11ea 3862/* mtcrf mtocrf*/
99e300ef 3863static void gen_mtcrf(DisasContext *ctx)
79aceca5 3864{
76a66253 3865 uint32_t crm, crn;
3b46e624 3866
76a66253 3867 crm = CRM(ctx->opcode);
8dd640e4 3868 if (likely((ctx->opcode & 0x00100000))) {
3869 if (crm && ((crm & (crm - 1)) == 0)) {
3870 TCGv_i32 temp = tcg_temp_new_i32();
0cfe11ea 3871 crn = ctz32 (crm);
8dd640e4 3872 tcg_gen_trunc_tl_i32(temp, cpu_gpr[rS(ctx->opcode)]);
0cfe11ea
AJ
3873 tcg_gen_shri_i32(temp, temp, crn * 4);
3874 tcg_gen_andi_i32(cpu_crf[7 - crn], temp, 0xf);
8dd640e4 3875 tcg_temp_free_i32(temp);
3876 }
76a66253 3877 } else {
651721b2
AJ
3878 TCGv_i32 temp = tcg_temp_new_i32();
3879 tcg_gen_trunc_tl_i32(temp, cpu_gpr[rS(ctx->opcode)]);
3880 for (crn = 0 ; crn < 8 ; crn++) {
3881 if (crm & (1 << crn)) {
3882 tcg_gen_shri_i32(cpu_crf[7 - crn], temp, crn * 4);
3883 tcg_gen_andi_i32(cpu_crf[7 - crn], cpu_crf[7 - crn], 0xf);
3884 }
3885 }
a7812ae4 3886 tcg_temp_free_i32(temp);
76a66253 3887 }
79aceca5
FB
3888}
3889
3890/* mtmsr */
426613db 3891#if defined(TARGET_PPC64)
99e300ef 3892static void gen_mtmsrd(DisasContext *ctx)
426613db
JM
3893{
3894#if defined(CONFIG_USER_ONLY)
e06fcd75 3895 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
426613db 3896#else
76db3ba4 3897 if (unlikely(!ctx->mem_idx)) {
e06fcd75 3898 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
426613db
JM
3899 return;
3900 }
be147d08
JM
3901 if (ctx->opcode & 0x00010000) {
3902 /* Special form that does not need any synchronisation */
6527f6ea
AJ
3903 TCGv t0 = tcg_temp_new();
3904 tcg_gen_andi_tl(t0, cpu_gpr[rS(ctx->opcode)], (1 << MSR_RI) | (1 << MSR_EE));
3905 tcg_gen_andi_tl(cpu_msr, cpu_msr, ~((1 << MSR_RI) | (1 << MSR_EE)));
3906 tcg_gen_or_tl(cpu_msr, cpu_msr, t0);
3907 tcg_temp_free(t0);
be147d08 3908 } else {
056b05f8
JM
3909 /* XXX: we need to update nip before the store
3910 * if we enter power saving mode, we will exit the loop
3911 * directly from ppc_store_msr
3912 */
be147d08 3913 gen_update_nip(ctx, ctx->nip);
e5f17ac6 3914 gen_helper_store_msr(cpu_env, cpu_gpr[rS(ctx->opcode)]);
be147d08
JM
3915 /* Must stop the translation as machine state (may have) changed */
3916 /* Note that mtmsr is not always defined as context-synchronizing */
e06fcd75 3917 gen_stop_exception(ctx);
be147d08 3918 }
426613db
JM
3919#endif
3920}
3921#endif
3922
99e300ef 3923static void gen_mtmsr(DisasContext *ctx)
79aceca5 3924{
9a64fbe4 3925#if defined(CONFIG_USER_ONLY)
e06fcd75 3926 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
9a64fbe4 3927#else
76db3ba4 3928 if (unlikely(!ctx->mem_idx)) {
e06fcd75 3929 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
9fddaa0c 3930 return;
9a64fbe4 3931 }
be147d08
JM
3932 if (ctx->opcode & 0x00010000) {
3933 /* Special form that does not need any synchronisation */
6527f6ea
AJ
3934 TCGv t0 = tcg_temp_new();
3935 tcg_gen_andi_tl(t0, cpu_gpr[rS(ctx->opcode)], (1 << MSR_RI) | (1 << MSR_EE));
3936 tcg_gen_andi_tl(cpu_msr, cpu_msr, ~((1 << MSR_RI) | (1 << MSR_EE)));
3937 tcg_gen_or_tl(cpu_msr, cpu_msr, t0);
3938 tcg_temp_free(t0);
be147d08 3939 } else {
8018dc63
AG
3940 TCGv msr = tcg_temp_new();
3941
056b05f8
JM
3942 /* XXX: we need to update nip before the store
3943 * if we enter power saving mode, we will exit the loop
3944 * directly from ppc_store_msr
3945 */
be147d08 3946 gen_update_nip(ctx, ctx->nip);
d9bce9d9 3947#if defined(TARGET_PPC64)
8018dc63
AG
3948 tcg_gen_deposit_tl(msr, cpu_msr, cpu_gpr[rS(ctx->opcode)], 0, 32);
3949#else
3950 tcg_gen_mov_tl(msr, cpu_gpr[rS(ctx->opcode)]);
d9bce9d9 3951#endif
e5f17ac6 3952 gen_helper_store_msr(cpu_env, msr);
be147d08 3953 /* Must stop the translation as machine state (may have) changed */
6527f6ea 3954 /* Note that mtmsr is not always defined as context-synchronizing */
e06fcd75 3955 gen_stop_exception(ctx);
be147d08 3956 }
9a64fbe4 3957#endif
79aceca5
FB
3958}
3959
3960/* mtspr */
99e300ef 3961static void gen_mtspr(DisasContext *ctx)
79aceca5 3962{
45d827d2 3963 void (*write_cb)(void *opaque, int sprn, int gprn);
79aceca5
FB
3964 uint32_t sprn = SPR(ctx->opcode);
3965
3fc6c082 3966#if !defined(CONFIG_USER_ONLY)
76db3ba4 3967 if (ctx->mem_idx == 2)
be147d08 3968 write_cb = ctx->spr_cb[sprn].hea_write;
76db3ba4 3969 else if (ctx->mem_idx)
3fc6c082
FB
3970 write_cb = ctx->spr_cb[sprn].oea_write;
3971 else
9a64fbe4 3972#endif
3fc6c082 3973 write_cb = ctx->spr_cb[sprn].uea_write;
76a66253
JM
3974 if (likely(write_cb != NULL)) {
3975 if (likely(write_cb != SPR_NOACCESS)) {
45d827d2 3976 (*write_cb)(ctx, sprn, rS(ctx->opcode));
3fc6c082
FB
3977 } else {
3978 /* Privilege exception */
93fcfe39 3979 qemu_log("Trying to write privileged spr %d %03x at "
90e189ec
BS
3980 TARGET_FMT_lx "\n", sprn, sprn, ctx->nip);
3981 printf("Trying to write privileged spr %d %03x at " TARGET_FMT_lx
3982 "\n", sprn, sprn, ctx->nip);
e06fcd75 3983 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
76a66253 3984 }
3fc6c082
FB
3985 } else {
3986 /* Not defined */
93fcfe39 3987 qemu_log("Trying to write invalid spr %d %03x at "
90e189ec
BS
3988 TARGET_FMT_lx "\n", sprn, sprn, ctx->nip);
3989 printf("Trying to write invalid spr %d %03x at " TARGET_FMT_lx "\n",
077fc206 3990 sprn, sprn, ctx->nip);
e06fcd75 3991 gen_inval_exception(ctx, POWERPC_EXCP_INVAL_SPR);
79aceca5 3992 }
79aceca5
FB
3993}
3994
3995/*** Cache management ***/
99e300ef 3996
54623277 3997/* dcbf */
99e300ef 3998static void gen_dcbf(DisasContext *ctx)
79aceca5 3999{
dac454af 4000 /* XXX: specification says this is treated as a load by the MMU */
76db3ba4
AJ
4001 TCGv t0;
4002 gen_set_access_type(ctx, ACCESS_CACHE);
4003 t0 = tcg_temp_new();
4004 gen_addr_reg_index(ctx, t0);
4005 gen_qemu_ld8u(ctx, t0, t0);
fea0c503 4006 tcg_temp_free(t0);
79aceca5
FB
4007}
4008
4009/* dcbi (Supervisor only) */
99e300ef 4010static void gen_dcbi(DisasContext *ctx)
79aceca5 4011{
a541f297 4012#if defined(CONFIG_USER_ONLY)
e06fcd75 4013 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
a541f297 4014#else
b61f2753 4015 TCGv EA, val;
76db3ba4 4016 if (unlikely(!ctx->mem_idx)) {
e06fcd75 4017 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
9fddaa0c 4018 return;
9a64fbe4 4019 }
a7812ae4 4020 EA = tcg_temp_new();
76db3ba4
AJ
4021 gen_set_access_type(ctx, ACCESS_CACHE);
4022 gen_addr_reg_index(ctx, EA);
a7812ae4 4023 val = tcg_temp_new();
76a66253 4024 /* XXX: specification says this should be treated as a store by the MMU */
76db3ba4
AJ
4025 gen_qemu_ld8u(ctx, val, EA);
4026 gen_qemu_st8(ctx, val, EA);
b61f2753
AJ
4027 tcg_temp_free(val);
4028 tcg_temp_free(EA);
a541f297 4029#endif
79aceca5
FB
4030}
4031
4032/* dcdst */
99e300ef 4033static void gen_dcbst(DisasContext *ctx)
79aceca5 4034{
76a66253 4035 /* XXX: specification say this is treated as a load by the MMU */
76db3ba4
AJ
4036 TCGv t0;
4037 gen_set_access_type(ctx, ACCESS_CACHE);
4038 t0 = tcg_temp_new();
4039 gen_addr_reg_index(ctx, t0);
4040 gen_qemu_ld8u(ctx, t0, t0);
fea0c503 4041 tcg_temp_free(t0);
79aceca5
FB
4042}
4043
4044/* dcbt */
99e300ef 4045static void gen_dcbt(DisasContext *ctx)
79aceca5 4046{
0db1b20e 4047 /* interpreted as no-op */
76a66253
JM
4048 /* XXX: specification say this is treated as a load by the MMU
4049 * but does not generate any exception
4050 */
79aceca5
FB
4051}
4052
4053/* dcbtst */
99e300ef 4054static void gen_dcbtst(DisasContext *ctx)
79aceca5 4055{
0db1b20e 4056 /* interpreted as no-op */
76a66253
JM
4057 /* XXX: specification say this is treated as a load by the MMU
4058 * but does not generate any exception
4059 */
79aceca5
FB
4060}
4061
4062/* dcbz */
99e300ef 4063static void gen_dcbz(DisasContext *ctx)
79aceca5 4064{
8e33944f
AG
4065 TCGv tcgv_addr;
4066 TCGv_i32 tcgv_is_dcbzl;
4067 int is_dcbzl = ctx->opcode & 0x00200000 ? 1 : 0;
d63001d1 4068
76db3ba4 4069 gen_set_access_type(ctx, ACCESS_CACHE);
799a8c8d
AJ
4070 /* NIP cannot be restored if the memory exception comes from an helper */
4071 gen_update_nip(ctx, ctx->nip - 4);
8e33944f
AG
4072 tcgv_addr = tcg_temp_new();
4073 tcgv_is_dcbzl = tcg_const_i32(is_dcbzl);
4074
4075 gen_addr_reg_index(ctx, tcgv_addr);
4076 gen_helper_dcbz(cpu_env, tcgv_addr, tcgv_is_dcbzl);
4077
4078 tcg_temp_free(tcgv_addr);
4079 tcg_temp_free_i32(tcgv_is_dcbzl);
79aceca5
FB
4080}
4081
ae1c1a3d 4082/* dst / dstt */
99e300ef 4083static void gen_dst(DisasContext *ctx)
ae1c1a3d
AJ
4084{
4085 if (rA(ctx->opcode) == 0) {
4086 gen_inval_exception(ctx, POWERPC_EXCP_INVAL_LSWX);
4087 } else {
4088 /* interpreted as no-op */
4089 }
4090}
4091
4092/* dstst /dststt */
99e300ef 4093static void gen_dstst(DisasContext *ctx)
ae1c1a3d
AJ
4094{
4095 if (rA(ctx->opcode) == 0) {
4096 gen_inval_exception(ctx, POWERPC_EXCP_INVAL_LSWX);
4097 } else {
4098 /* interpreted as no-op */
4099 }
4100
4101}
4102
4103/* dss / dssall */
99e300ef 4104static void gen_dss(DisasContext *ctx)
ae1c1a3d
AJ
4105{
4106 /* interpreted as no-op */
4107}
4108
79aceca5 4109/* icbi */
99e300ef 4110static void gen_icbi(DisasContext *ctx)
79aceca5 4111{
76db3ba4
AJ
4112 TCGv t0;
4113 gen_set_access_type(ctx, ACCESS_CACHE);
30032c94
JM
4114 /* NIP cannot be restored if the memory exception comes from an helper */
4115 gen_update_nip(ctx, ctx->nip - 4);
76db3ba4
AJ
4116 t0 = tcg_temp_new();
4117 gen_addr_reg_index(ctx, t0);
2f5a189c 4118 gen_helper_icbi(cpu_env, t0);
37d269df 4119 tcg_temp_free(t0);
79aceca5
FB
4120}
4121
4122/* Optional: */
4123/* dcba */
99e300ef 4124static void gen_dcba(DisasContext *ctx)
79aceca5 4125{
0db1b20e
JM
4126 /* interpreted as no-op */
4127 /* XXX: specification say this is treated as a store by the MMU
4128 * but does not generate any exception
4129 */
79aceca5
FB
4130}
4131
4132/*** Segment register manipulation ***/
4133/* Supervisor only: */
99e300ef 4134
54623277 4135/* mfsr */
99e300ef 4136static void gen_mfsr(DisasContext *ctx)
79aceca5 4137{
9a64fbe4 4138#if defined(CONFIG_USER_ONLY)
e06fcd75 4139 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
9a64fbe4 4140#else
74d37793 4141 TCGv t0;
76db3ba4 4142 if (unlikely(!ctx->mem_idx)) {
e06fcd75 4143 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
9fddaa0c 4144 return;
9a64fbe4 4145 }
74d37793 4146 t0 = tcg_const_tl(SR(ctx->opcode));
c6c7cf05 4147 gen_helper_load_sr(cpu_gpr[rD(ctx->opcode)], cpu_env, t0);
74d37793 4148 tcg_temp_free(t0);
9a64fbe4 4149#endif
79aceca5
FB
4150}
4151
4152/* mfsrin */
99e300ef 4153static void gen_mfsrin(DisasContext *ctx)
79aceca5 4154{
9a64fbe4 4155#if defined(CONFIG_USER_ONLY)
e06fcd75 4156 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
9a64fbe4 4157#else
74d37793 4158 TCGv t0;
76db3ba4 4159 if (unlikely(!ctx->mem_idx)) {
e06fcd75 4160 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
9fddaa0c 4161 return;
9a64fbe4 4162 }
74d37793
AJ
4163 t0 = tcg_temp_new();
4164 tcg_gen_shri_tl(t0, cpu_gpr[rB(ctx->opcode)], 28);
4165 tcg_gen_andi_tl(t0, t0, 0xF);
c6c7cf05 4166 gen_helper_load_sr(cpu_gpr[rD(ctx->opcode)], cpu_env, t0);
74d37793 4167 tcg_temp_free(t0);
9a64fbe4 4168#endif
79aceca5
FB
4169}
4170
4171/* mtsr */
99e300ef 4172static void gen_mtsr(DisasContext *ctx)
79aceca5 4173{
9a64fbe4 4174#if defined(CONFIG_USER_ONLY)
e06fcd75 4175 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
9a64fbe4 4176#else
74d37793 4177 TCGv t0;
76db3ba4 4178 if (unlikely(!ctx->mem_idx)) {
e06fcd75 4179 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
9fddaa0c 4180 return;
9a64fbe4 4181 }
74d37793 4182 t0 = tcg_const_tl(SR(ctx->opcode));
c6c7cf05 4183 gen_helper_store_sr(cpu_env, t0, cpu_gpr[rS(ctx->opcode)]);
74d37793 4184 tcg_temp_free(t0);
9a64fbe4 4185#endif
79aceca5
FB
4186}
4187
4188/* mtsrin */
99e300ef 4189static void gen_mtsrin(DisasContext *ctx)
79aceca5 4190{
9a64fbe4 4191#if defined(CONFIG_USER_ONLY)
e06fcd75 4192 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
9a64fbe4 4193#else
74d37793 4194 TCGv t0;
76db3ba4 4195 if (unlikely(!ctx->mem_idx)) {
e06fcd75 4196 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
9fddaa0c 4197 return;
9a64fbe4 4198 }
74d37793
AJ
4199 t0 = tcg_temp_new();
4200 tcg_gen_shri_tl(t0, cpu_gpr[rB(ctx->opcode)], 28);
4201 tcg_gen_andi_tl(t0, t0, 0xF);
c6c7cf05 4202 gen_helper_store_sr(cpu_env, t0, cpu_gpr[rD(ctx->opcode)]);
74d37793 4203 tcg_temp_free(t0);
9a64fbe4 4204#endif
79aceca5
FB
4205}
4206
12de9a39
JM
4207#if defined(TARGET_PPC64)
4208/* Specific implementation for PowerPC 64 "bridge" emulation using SLB */
e8eaa2c0 4209
54623277 4210/* mfsr */
e8eaa2c0 4211static void gen_mfsr_64b(DisasContext *ctx)
12de9a39
JM
4212{
4213#if defined(CONFIG_USER_ONLY)
e06fcd75 4214 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
12de9a39 4215#else
74d37793 4216 TCGv t0;
76db3ba4 4217 if (unlikely(!ctx->mem_idx)) {
e06fcd75 4218 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
12de9a39
JM
4219 return;
4220 }
74d37793 4221 t0 = tcg_const_tl(SR(ctx->opcode));
c6c7cf05 4222 gen_helper_load_sr(cpu_gpr[rD(ctx->opcode)], cpu_env, t0);
74d37793 4223 tcg_temp_free(t0);
12de9a39
JM
4224#endif
4225}
4226
4227/* mfsrin */
e8eaa2c0 4228static void gen_mfsrin_64b(DisasContext *ctx)
12de9a39
JM
4229{
4230#if defined(CONFIG_USER_ONLY)
e06fcd75 4231 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
12de9a39 4232#else
74d37793 4233 TCGv t0;
76db3ba4 4234 if (unlikely(!ctx->mem_idx)) {
e06fcd75 4235 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
12de9a39
JM
4236 return;
4237 }
74d37793
AJ
4238 t0 = tcg_temp_new();
4239 tcg_gen_shri_tl(t0, cpu_gpr[rB(ctx->opcode)], 28);
4240 tcg_gen_andi_tl(t0, t0, 0xF);
c6c7cf05 4241 gen_helper_load_sr(cpu_gpr[rD(ctx->opcode)], cpu_env, t0);
74d37793 4242 tcg_temp_free(t0);
12de9a39
JM
4243#endif
4244}
4245
4246/* mtsr */
e8eaa2c0 4247static void gen_mtsr_64b(DisasContext *ctx)
12de9a39
JM
4248{
4249#if defined(CONFIG_USER_ONLY)
e06fcd75 4250 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
12de9a39 4251#else
74d37793 4252 TCGv t0;
76db3ba4 4253 if (unlikely(!ctx->mem_idx)) {
e06fcd75 4254 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
12de9a39
JM
4255 return;
4256 }
74d37793 4257 t0 = tcg_const_tl(SR(ctx->opcode));
c6c7cf05 4258 gen_helper_store_sr(cpu_env, t0, cpu_gpr[rS(ctx->opcode)]);
74d37793 4259 tcg_temp_free(t0);
12de9a39
JM
4260#endif
4261}
4262
4263/* mtsrin */
e8eaa2c0 4264static void gen_mtsrin_64b(DisasContext *ctx)
12de9a39
JM
4265{
4266#if defined(CONFIG_USER_ONLY)
e06fcd75 4267 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
12de9a39 4268#else
74d37793 4269 TCGv t0;
76db3ba4 4270 if (unlikely(!ctx->mem_idx)) {
e06fcd75 4271 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
12de9a39
JM
4272 return;
4273 }
74d37793
AJ
4274 t0 = tcg_temp_new();
4275 tcg_gen_shri_tl(t0, cpu_gpr[rB(ctx->opcode)], 28);
4276 tcg_gen_andi_tl(t0, t0, 0xF);
c6c7cf05 4277 gen_helper_store_sr(cpu_env, t0, cpu_gpr[rS(ctx->opcode)]);
74d37793 4278 tcg_temp_free(t0);
12de9a39
JM
4279#endif
4280}
f6b868fc
BS
4281
4282/* slbmte */
e8eaa2c0 4283static void gen_slbmte(DisasContext *ctx)
f6b868fc
BS
4284{
4285#if defined(CONFIG_USER_ONLY)
4286 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
4287#else
4288 if (unlikely(!ctx->mem_idx)) {
4289 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
4290 return;
4291 }
c6c7cf05
BS
4292 gen_helper_store_slb(cpu_env, cpu_gpr[rB(ctx->opcode)],
4293 cpu_gpr[rS(ctx->opcode)]);
f6b868fc
BS
4294#endif
4295}
4296
efdef95f
DG
4297static void gen_slbmfee(DisasContext *ctx)
4298{
4299#if defined(CONFIG_USER_ONLY)
4300 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
4301#else
4302 if (unlikely(!ctx->mem_idx)) {
4303 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
4304 return;
4305 }
c6c7cf05 4306 gen_helper_load_slb_esid(cpu_gpr[rS(ctx->opcode)], cpu_env,
efdef95f
DG
4307 cpu_gpr[rB(ctx->opcode)]);
4308#endif
4309}
4310
4311static void gen_slbmfev(DisasContext *ctx)
4312{
4313#if defined(CONFIG_USER_ONLY)
4314 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
4315#else
4316 if (unlikely(!ctx->mem_idx)) {
4317 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
4318 return;
4319 }
c6c7cf05 4320 gen_helper_load_slb_vsid(cpu_gpr[rS(ctx->opcode)], cpu_env,
efdef95f
DG
4321 cpu_gpr[rB(ctx->opcode)]);
4322#endif
4323}
12de9a39
JM
4324#endif /* defined(TARGET_PPC64) */
4325
79aceca5 4326/*** Lookaside buffer management ***/
76db3ba4 4327/* Optional & mem_idx only: */
99e300ef 4328
54623277 4329/* tlbia */
99e300ef 4330static void gen_tlbia(DisasContext *ctx)
79aceca5 4331{
9a64fbe4 4332#if defined(CONFIG_USER_ONLY)
e06fcd75 4333 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
9a64fbe4 4334#else
76db3ba4 4335 if (unlikely(!ctx->mem_idx)) {
e06fcd75 4336 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
9fddaa0c 4337 return;
9a64fbe4 4338 }
c6c7cf05 4339 gen_helper_tlbia(cpu_env);
9a64fbe4 4340#endif
79aceca5
FB
4341}
4342
bf14b1ce 4343/* tlbiel */
99e300ef 4344static void gen_tlbiel(DisasContext *ctx)
bf14b1ce
BS
4345{
4346#if defined(CONFIG_USER_ONLY)
4347 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
4348#else
4349 if (unlikely(!ctx->mem_idx)) {
4350 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
4351 return;
4352 }
c6c7cf05 4353 gen_helper_tlbie(cpu_env, cpu_gpr[rB(ctx->opcode)]);
bf14b1ce
BS
4354#endif
4355}
4356
79aceca5 4357/* tlbie */
99e300ef 4358static void gen_tlbie(DisasContext *ctx)
79aceca5 4359{
9a64fbe4 4360#if defined(CONFIG_USER_ONLY)
e06fcd75 4361 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
9a64fbe4 4362#else
76db3ba4 4363 if (unlikely(!ctx->mem_idx)) {
e06fcd75 4364 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
9fddaa0c 4365 return;
9a64fbe4 4366 }
9ca3f7f3 4367 if (NARROW_MODE(ctx)) {
74d37793
AJ
4368 TCGv t0 = tcg_temp_new();
4369 tcg_gen_ext32u_tl(t0, cpu_gpr[rB(ctx->opcode)]);
c6c7cf05 4370 gen_helper_tlbie(cpu_env, t0);
74d37793 4371 tcg_temp_free(t0);
9ca3f7f3 4372 } else {
c6c7cf05 4373 gen_helper_tlbie(cpu_env, cpu_gpr[rB(ctx->opcode)]);
9ca3f7f3 4374 }
9a64fbe4 4375#endif
79aceca5
FB
4376}
4377
4378/* tlbsync */
99e300ef 4379static void gen_tlbsync(DisasContext *ctx)
79aceca5 4380{
9a64fbe4 4381#if defined(CONFIG_USER_ONLY)
e06fcd75 4382 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
9a64fbe4 4383#else
76db3ba4 4384 if (unlikely(!ctx->mem_idx)) {
e06fcd75 4385 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
9fddaa0c 4386 return;
9a64fbe4
FB
4387 }
4388 /* This has no effect: it should ensure that all previous
4389 * tlbie have completed
4390 */
e06fcd75 4391 gen_stop_exception(ctx);
9a64fbe4 4392#endif
79aceca5
FB
4393}
4394
426613db
JM
4395#if defined(TARGET_PPC64)
4396/* slbia */
99e300ef 4397static void gen_slbia(DisasContext *ctx)
426613db
JM
4398{
4399#if defined(CONFIG_USER_ONLY)
e06fcd75 4400 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
426613db 4401#else
76db3ba4 4402 if (unlikely(!ctx->mem_idx)) {
e06fcd75 4403 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
426613db
JM
4404 return;
4405 }
c6c7cf05 4406 gen_helper_slbia(cpu_env);
426613db
JM
4407#endif
4408}
4409
4410/* slbie */
99e300ef 4411static void gen_slbie(DisasContext *ctx)
426613db
JM
4412{
4413#if defined(CONFIG_USER_ONLY)
e06fcd75 4414 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
426613db 4415#else
76db3ba4 4416 if (unlikely(!ctx->mem_idx)) {
e06fcd75 4417 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
426613db
JM
4418 return;
4419 }
c6c7cf05 4420 gen_helper_slbie(cpu_env, cpu_gpr[rB(ctx->opcode)]);
426613db
JM
4421#endif
4422}
4423#endif
4424
79aceca5
FB
4425/*** External control ***/
4426/* Optional: */
99e300ef 4427
54623277 4428/* eciwx */
99e300ef 4429static void gen_eciwx(DisasContext *ctx)
79aceca5 4430{
76db3ba4 4431 TCGv t0;
fa407c03 4432 /* Should check EAR[E] ! */
76db3ba4
AJ
4433 gen_set_access_type(ctx, ACCESS_EXT);
4434 t0 = tcg_temp_new();
4435 gen_addr_reg_index(ctx, t0);
fa407c03 4436 gen_check_align(ctx, t0, 0x03);
76db3ba4 4437 gen_qemu_ld32u(ctx, cpu_gpr[rD(ctx->opcode)], t0);
fa407c03 4438 tcg_temp_free(t0);
76a66253
JM
4439}
4440
4441/* ecowx */
99e300ef 4442static void gen_ecowx(DisasContext *ctx)
76a66253 4443{
76db3ba4 4444 TCGv t0;
fa407c03 4445 /* Should check EAR[E] ! */
76db3ba4
AJ
4446 gen_set_access_type(ctx, ACCESS_EXT);
4447 t0 = tcg_temp_new();
4448 gen_addr_reg_index(ctx, t0);
fa407c03 4449 gen_check_align(ctx, t0, 0x03);
76db3ba4 4450 gen_qemu_st32(ctx, cpu_gpr[rD(ctx->opcode)], t0);
fa407c03 4451 tcg_temp_free(t0);
76a66253
JM
4452}
4453
4454/* PowerPC 601 specific instructions */
99e300ef 4455
54623277 4456/* abs - abs. */
99e300ef 4457static void gen_abs(DisasContext *ctx)
76a66253 4458{
22e0e173
AJ
4459 int l1 = gen_new_label();
4460 int l2 = gen_new_label();
4461 tcg_gen_brcondi_tl(TCG_COND_GE, cpu_gpr[rA(ctx->opcode)], 0, l1);
4462 tcg_gen_neg_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)]);
4463 tcg_gen_br(l2);
4464 gen_set_label(l1);
4465 tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)]);
4466 gen_set_label(l2);
76a66253 4467 if (unlikely(Rc(ctx->opcode) != 0))
22e0e173 4468 gen_set_Rc0(ctx, cpu_gpr[rD(ctx->opcode)]);
76a66253
JM
4469}
4470
4471/* abso - abso. */
99e300ef 4472static void gen_abso(DisasContext *ctx)
76a66253 4473{
22e0e173
AJ
4474 int l1 = gen_new_label();
4475 int l2 = gen_new_label();
4476 int l3 = gen_new_label();
4477 /* Start with XER OV disabled, the most likely case */
da91a00f 4478 tcg_gen_movi_tl(cpu_ov, 0);
22e0e173
AJ
4479 tcg_gen_brcondi_tl(TCG_COND_GE, cpu_gpr[rA(ctx->opcode)], 0, l2);
4480 tcg_gen_brcondi_tl(TCG_COND_NE, cpu_gpr[rA(ctx->opcode)], 0x80000000, l1);
da91a00f
RH
4481 tcg_gen_movi_tl(cpu_ov, 1);
4482 tcg_gen_movi_tl(cpu_so, 1);
22e0e173
AJ
4483 tcg_gen_br(l2);
4484 gen_set_label(l1);
4485 tcg_gen_neg_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)]);
4486 tcg_gen_br(l3);
4487 gen_set_label(l2);
4488 tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)]);
4489 gen_set_label(l3);
76a66253 4490 if (unlikely(Rc(ctx->opcode) != 0))
22e0e173 4491 gen_set_Rc0(ctx, cpu_gpr[rD(ctx->opcode)]);
76a66253
JM
4492}
4493
4494/* clcs */
99e300ef 4495static void gen_clcs(DisasContext *ctx)
76a66253 4496{
22e0e173 4497 TCGv_i32 t0 = tcg_const_i32(rA(ctx->opcode));
d523dd00 4498 gen_helper_clcs(cpu_gpr[rD(ctx->opcode)], cpu_env, t0);
22e0e173 4499 tcg_temp_free_i32(t0);
c7697e1f 4500 /* Rc=1 sets CR0 to an undefined state */
76a66253
JM
4501}
4502
4503/* div - div. */
99e300ef 4504static void gen_div(DisasContext *ctx)
76a66253 4505{
d15f74fb
BS
4506 gen_helper_div(cpu_gpr[rD(ctx->opcode)], cpu_env, cpu_gpr[rA(ctx->opcode)],
4507 cpu_gpr[rB(ctx->opcode)]);
76a66253 4508 if (unlikely(Rc(ctx->opcode) != 0))
22e0e173 4509 gen_set_Rc0(ctx, cpu_gpr[rD(ctx->opcode)]);
76a66253
JM
4510}
4511
4512/* divo - divo. */
99e300ef 4513static void gen_divo(DisasContext *ctx)
76a66253 4514{
d15f74fb
BS
4515 gen_helper_divo(cpu_gpr[rD(ctx->opcode)], cpu_env, cpu_gpr[rA(ctx->opcode)],
4516 cpu_gpr[rB(ctx->opcode)]);
76a66253 4517 if (unlikely(Rc(ctx->opcode) != 0))
22e0e173 4518 gen_set_Rc0(ctx, cpu_gpr[rD(ctx->opcode)]);
76a66253
JM
4519}
4520
4521/* divs - divs. */
99e300ef 4522static void gen_divs(DisasContext *ctx)
76a66253 4523{
d15f74fb
BS
4524 gen_helper_divs(cpu_gpr[rD(ctx->opcode)], cpu_env, cpu_gpr[rA(ctx->opcode)],
4525 cpu_gpr[rB(ctx->opcode)]);
76a66253 4526 if (unlikely(Rc(ctx->opcode) != 0))
22e0e173 4527 gen_set_Rc0(ctx, cpu_gpr[rD(ctx->opcode)]);
76a66253
JM
4528}
4529
4530/* divso - divso. */
99e300ef 4531static void gen_divso(DisasContext *ctx)
76a66253 4532{
d15f74fb
BS
4533 gen_helper_divso(cpu_gpr[rD(ctx->opcode)], cpu_env,
4534 cpu_gpr[rA(ctx->opcode)], cpu_gpr[rB(ctx->opcode)]);
76a66253 4535 if (unlikely(Rc(ctx->opcode) != 0))
22e0e173 4536 gen_set_Rc0(ctx, cpu_gpr[rD(ctx->opcode)]);
76a66253
JM
4537}
4538
4539/* doz - doz. */
99e300ef 4540static void gen_doz(DisasContext *ctx)
76a66253 4541{
22e0e173
AJ
4542 int l1 = gen_new_label();
4543 int l2 = gen_new_label();
4544 tcg_gen_brcond_tl(TCG_COND_GE, cpu_gpr[rB(ctx->opcode)], cpu_gpr[rA(ctx->opcode)], l1);
4545 tcg_gen_sub_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rB(ctx->opcode)], cpu_gpr[rA(ctx->opcode)]);
4546 tcg_gen_br(l2);
4547 gen_set_label(l1);
4548 tcg_gen_movi_tl(cpu_gpr[rD(ctx->opcode)], 0);
4549 gen_set_label(l2);
76a66253 4550 if (unlikely(Rc(ctx->opcode) != 0))
22e0e173 4551 gen_set_Rc0(ctx, cpu_gpr[rD(ctx->opcode)]);
76a66253
JM
4552}
4553
4554/* dozo - dozo. */
99e300ef 4555static void gen_dozo(DisasContext *ctx)
76a66253 4556{
22e0e173
AJ
4557 int l1 = gen_new_label();
4558 int l2 = gen_new_label();
4559 TCGv t0 = tcg_temp_new();
4560 TCGv t1 = tcg_temp_new();
4561 TCGv t2 = tcg_temp_new();
4562 /* Start with XER OV disabled, the most likely case */
da91a00f 4563 tcg_gen_movi_tl(cpu_ov, 0);
22e0e173
AJ
4564 tcg_gen_brcond_tl(TCG_COND_GE, cpu_gpr[rB(ctx->opcode)], cpu_gpr[rA(ctx->opcode)], l1);
4565 tcg_gen_sub_tl(t0, cpu_gpr[rB(ctx->opcode)], cpu_gpr[rA(ctx->opcode)]);
4566 tcg_gen_xor_tl(t1, cpu_gpr[rB(ctx->opcode)], cpu_gpr[rA(ctx->opcode)]);
4567 tcg_gen_xor_tl(t2, cpu_gpr[rA(ctx->opcode)], t0);
4568 tcg_gen_andc_tl(t1, t1, t2);
4569 tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], t0);
4570 tcg_gen_brcondi_tl(TCG_COND_GE, t1, 0, l2);
da91a00f
RH
4571 tcg_gen_movi_tl(cpu_ov, 1);
4572 tcg_gen_movi_tl(cpu_so, 1);
22e0e173
AJ
4573 tcg_gen_br(l2);
4574 gen_set_label(l1);
4575 tcg_gen_movi_tl(cpu_gpr[rD(ctx->opcode)], 0);
4576 gen_set_label(l2);
4577 tcg_temp_free(t0);
4578 tcg_temp_free(t1);
4579 tcg_temp_free(t2);
76a66253 4580 if (unlikely(Rc(ctx->opcode) != 0))
22e0e173 4581 gen_set_Rc0(ctx, cpu_gpr[rD(ctx->opcode)]);
76a66253
JM
4582}
4583
4584/* dozi */
99e300ef 4585static void gen_dozi(DisasContext *ctx)
76a66253 4586{
22e0e173
AJ
4587 target_long simm = SIMM(ctx->opcode);
4588 int l1 = gen_new_label();
4589 int l2 = gen_new_label();
4590 tcg_gen_brcondi_tl(TCG_COND_LT, cpu_gpr[rA(ctx->opcode)], simm, l1);
4591 tcg_gen_subfi_tl(cpu_gpr[rD(ctx->opcode)], simm, cpu_gpr[rA(ctx->opcode)]);
4592 tcg_gen_br(l2);
4593 gen_set_label(l1);
4594 tcg_gen_movi_tl(cpu_gpr[rD(ctx->opcode)], 0);
4595 gen_set_label(l2);
4596 if (unlikely(Rc(ctx->opcode) != 0))
4597 gen_set_Rc0(ctx, cpu_gpr[rD(ctx->opcode)]);
76a66253
JM
4598}
4599
76a66253 4600/* lscbx - lscbx. */
99e300ef 4601static void gen_lscbx(DisasContext *ctx)
76a66253 4602{
bdb4b689
AJ
4603 TCGv t0 = tcg_temp_new();
4604 TCGv_i32 t1 = tcg_const_i32(rD(ctx->opcode));
4605 TCGv_i32 t2 = tcg_const_i32(rA(ctx->opcode));
4606 TCGv_i32 t3 = tcg_const_i32(rB(ctx->opcode));
76a66253 4607
76db3ba4 4608 gen_addr_reg_index(ctx, t0);
76a66253 4609 /* NIP cannot be restored if the memory exception comes from an helper */
d9bce9d9 4610 gen_update_nip(ctx, ctx->nip - 4);
2f5a189c 4611 gen_helper_lscbx(t0, cpu_env, t0, t1, t2, t3);
bdb4b689
AJ
4612 tcg_temp_free_i32(t1);
4613 tcg_temp_free_i32(t2);
4614 tcg_temp_free_i32(t3);
3d7b417e 4615 tcg_gen_andi_tl(cpu_xer, cpu_xer, ~0x7F);
bdb4b689 4616 tcg_gen_or_tl(cpu_xer, cpu_xer, t0);
76a66253 4617 if (unlikely(Rc(ctx->opcode) != 0))
bdb4b689
AJ
4618 gen_set_Rc0(ctx, t0);
4619 tcg_temp_free(t0);
76a66253
JM
4620}
4621
4622/* maskg - maskg. */
99e300ef 4623static void gen_maskg(DisasContext *ctx)
76a66253 4624{
22e0e173
AJ
4625 int l1 = gen_new_label();
4626 TCGv t0 = tcg_temp_new();
4627 TCGv t1 = tcg_temp_new();
4628 TCGv t2 = tcg_temp_new();
4629 TCGv t3 = tcg_temp_new();
4630 tcg_gen_movi_tl(t3, 0xFFFFFFFF);
4631 tcg_gen_andi_tl(t0, cpu_gpr[rB(ctx->opcode)], 0x1F);
4632 tcg_gen_andi_tl(t1, cpu_gpr[rS(ctx->opcode)], 0x1F);
4633 tcg_gen_addi_tl(t2, t0, 1);
4634 tcg_gen_shr_tl(t2, t3, t2);
4635 tcg_gen_shr_tl(t3, t3, t1);
4636 tcg_gen_xor_tl(cpu_gpr[rA(ctx->opcode)], t2, t3);
4637 tcg_gen_brcond_tl(TCG_COND_GE, t0, t1, l1);
4638 tcg_gen_neg_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rA(ctx->opcode)]);
4639 gen_set_label(l1);
4640 tcg_temp_free(t0);
4641 tcg_temp_free(t1);
4642 tcg_temp_free(t2);
4643 tcg_temp_free(t3);
76a66253 4644 if (unlikely(Rc(ctx->opcode) != 0))
22e0e173 4645 gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
76a66253
JM
4646}
4647
4648/* maskir - maskir. */
99e300ef 4649static void gen_maskir(DisasContext *ctx)
76a66253 4650{
22e0e173
AJ
4651 TCGv t0 = tcg_temp_new();
4652 TCGv t1 = tcg_temp_new();
4653 tcg_gen_and_tl(t0, cpu_gpr[rS(ctx->opcode)], cpu_gpr[rB(ctx->opcode)]);
4654 tcg_gen_andc_tl(t1, cpu_gpr[rA(ctx->opcode)], cpu_gpr[rB(ctx->opcode)]);
4655 tcg_gen_or_tl(cpu_gpr[rA(ctx->opcode)], t0, t1);
4656 tcg_temp_free(t0);
4657 tcg_temp_free(t1);
76a66253 4658 if (unlikely(Rc(ctx->opcode) != 0))
22e0e173 4659 gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
76a66253
JM
4660}
4661
4662/* mul - mul. */
99e300ef 4663static void gen_mul(DisasContext *ctx)
76a66253 4664{
22e0e173
AJ
4665 TCGv_i64 t0 = tcg_temp_new_i64();
4666 TCGv_i64 t1 = tcg_temp_new_i64();
4667 TCGv t2 = tcg_temp_new();
4668 tcg_gen_extu_tl_i64(t0, cpu_gpr[rA(ctx->opcode)]);
4669 tcg_gen_extu_tl_i64(t1, cpu_gpr[rB(ctx->opcode)]);
4670 tcg_gen_mul_i64(t0, t0, t1);
4671 tcg_gen_trunc_i64_tl(t2, t0);
4672 gen_store_spr(SPR_MQ, t2);
4673 tcg_gen_shri_i64(t1, t0, 32);
4674 tcg_gen_trunc_i64_tl(cpu_gpr[rD(ctx->opcode)], t1);
4675 tcg_temp_free_i64(t0);
4676 tcg_temp_free_i64(t1);
4677 tcg_temp_free(t2);
76a66253 4678 if (unlikely(Rc(ctx->opcode) != 0))
22e0e173 4679 gen_set_Rc0(ctx, cpu_gpr[rD(ctx->opcode)]);
76a66253
JM
4680}
4681
4682/* mulo - mulo. */
99e300ef 4683static void gen_mulo(DisasContext *ctx)
76a66253 4684{
22e0e173
AJ
4685 int l1 = gen_new_label();
4686 TCGv_i64 t0 = tcg_temp_new_i64();
4687 TCGv_i64 t1 = tcg_temp_new_i64();
4688 TCGv t2 = tcg_temp_new();
4689 /* Start with XER OV disabled, the most likely case */
da91a00f 4690 tcg_gen_movi_tl(cpu_ov, 0);
22e0e173
AJ
4691 tcg_gen_extu_tl_i64(t0, cpu_gpr[rA(ctx->opcode)]);
4692 tcg_gen_extu_tl_i64(t1, cpu_gpr[rB(ctx->opcode)]);
4693 tcg_gen_mul_i64(t0, t0, t1);
4694 tcg_gen_trunc_i64_tl(t2, t0);
4695 gen_store_spr(SPR_MQ, t2);
4696 tcg_gen_shri_i64(t1, t0, 32);
4697 tcg_gen_trunc_i64_tl(cpu_gpr[rD(ctx->opcode)], t1);
4698 tcg_gen_ext32s_i64(t1, t0);
4699 tcg_gen_brcond_i64(TCG_COND_EQ, t0, t1, l1);
da91a00f
RH
4700 tcg_gen_movi_tl(cpu_ov, 1);
4701 tcg_gen_movi_tl(cpu_so, 1);
22e0e173
AJ
4702 gen_set_label(l1);
4703 tcg_temp_free_i64(t0);
4704 tcg_temp_free_i64(t1);
4705 tcg_temp_free(t2);
76a66253 4706 if (unlikely(Rc(ctx->opcode) != 0))
22e0e173 4707 gen_set_Rc0(ctx, cpu_gpr[rD(ctx->opcode)]);
76a66253
JM
4708}
4709
4710/* nabs - nabs. */
99e300ef 4711static void gen_nabs(DisasContext *ctx)
76a66253 4712{
22e0e173
AJ
4713 int l1 = gen_new_label();
4714 int l2 = gen_new_label();
4715 tcg_gen_brcondi_tl(TCG_COND_GT, cpu_gpr[rA(ctx->opcode)], 0, l1);
4716 tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)]);
4717 tcg_gen_br(l2);
4718 gen_set_label(l1);
4719 tcg_gen_neg_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)]);
4720 gen_set_label(l2);
76a66253 4721 if (unlikely(Rc(ctx->opcode) != 0))
22e0e173 4722 gen_set_Rc0(ctx, cpu_gpr[rD(ctx->opcode)]);
76a66253
JM
4723}
4724
4725/* nabso - nabso. */
99e300ef 4726static void gen_nabso(DisasContext *ctx)
76a66253 4727{
22e0e173
AJ
4728 int l1 = gen_new_label();
4729 int l2 = gen_new_label();
4730 tcg_gen_brcondi_tl(TCG_COND_GT, cpu_gpr[rA(ctx->opcode)], 0, l1);
4731 tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)]);
4732 tcg_gen_br(l2);
4733 gen_set_label(l1);
4734 tcg_gen_neg_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)]);
4735 gen_set_label(l2);
4736 /* nabs never overflows */
da91a00f 4737 tcg_gen_movi_tl(cpu_ov, 0);
76a66253 4738 if (unlikely(Rc(ctx->opcode) != 0))
22e0e173 4739 gen_set_Rc0(ctx, cpu_gpr[rD(ctx->opcode)]);
76a66253
JM
4740}
4741
4742/* rlmi - rlmi. */
99e300ef 4743static void gen_rlmi(DisasContext *ctx)
76a66253 4744{
7487953d
AJ
4745 uint32_t mb = MB(ctx->opcode);
4746 uint32_t me = ME(ctx->opcode);
4747 TCGv t0 = tcg_temp_new();
4748 tcg_gen_andi_tl(t0, cpu_gpr[rB(ctx->opcode)], 0x1F);
4749 tcg_gen_rotl_tl(t0, cpu_gpr[rS(ctx->opcode)], t0);
4750 tcg_gen_andi_tl(t0, t0, MASK(mb, me));
4751 tcg_gen_andi_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rA(ctx->opcode)], ~MASK(mb, me));
4752 tcg_gen_or_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rA(ctx->opcode)], t0);
4753 tcg_temp_free(t0);
76a66253 4754 if (unlikely(Rc(ctx->opcode) != 0))
7487953d 4755 gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
76a66253
JM
4756}
4757
4758/* rrib - rrib. */
99e300ef 4759static void gen_rrib(DisasContext *ctx)
76a66253 4760{
7487953d
AJ
4761 TCGv t0 = tcg_temp_new();
4762 TCGv t1 = tcg_temp_new();
4763 tcg_gen_andi_tl(t0, cpu_gpr[rB(ctx->opcode)], 0x1F);
4764 tcg_gen_movi_tl(t1, 0x80000000);
4765 tcg_gen_shr_tl(t1, t1, t0);
4766 tcg_gen_shr_tl(t0, cpu_gpr[rS(ctx->opcode)], t0);
4767 tcg_gen_and_tl(t0, t0, t1);
4768 tcg_gen_andc_tl(t1, cpu_gpr[rA(ctx->opcode)], t1);
4769 tcg_gen_or_tl(cpu_gpr[rA(ctx->opcode)], t0, t1);
4770 tcg_temp_free(t0);
4771 tcg_temp_free(t1);
76a66253 4772 if (unlikely(Rc(ctx->opcode) != 0))
7487953d 4773 gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
76a66253
JM
4774}
4775
4776/* sle - sle. */
99e300ef 4777static void gen_sle(DisasContext *ctx)
76a66253 4778{
7487953d
AJ
4779 TCGv t0 = tcg_temp_new();
4780 TCGv t1 = tcg_temp_new();
4781 tcg_gen_andi_tl(t1, cpu_gpr[rB(ctx->opcode)], 0x1F);
4782 tcg_gen_shl_tl(t0, cpu_gpr[rS(ctx->opcode)], t1);
4783 tcg_gen_subfi_tl(t1, 32, t1);
4784 tcg_gen_shr_tl(t1, cpu_gpr[rS(ctx->opcode)], t1);
4785 tcg_gen_or_tl(t1, t0, t1);
4786 tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], t0);
4787 gen_store_spr(SPR_MQ, t1);
4788 tcg_temp_free(t0);
4789 tcg_temp_free(t1);
76a66253 4790 if (unlikely(Rc(ctx->opcode) != 0))
7487953d 4791 gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
76a66253
JM
4792}
4793
4794/* sleq - sleq. */
99e300ef 4795static void gen_sleq(DisasContext *ctx)
76a66253 4796{
7487953d
AJ
4797 TCGv t0 = tcg_temp_new();
4798 TCGv t1 = tcg_temp_new();
4799 TCGv t2 = tcg_temp_new();
4800 tcg_gen_andi_tl(t0, cpu_gpr[rB(ctx->opcode)], 0x1F);
4801 tcg_gen_movi_tl(t2, 0xFFFFFFFF);
4802 tcg_gen_shl_tl(t2, t2, t0);
4803 tcg_gen_rotl_tl(t0, cpu_gpr[rS(ctx->opcode)], t0);
4804 gen_load_spr(t1, SPR_MQ);
4805 gen_store_spr(SPR_MQ, t0);
4806 tcg_gen_and_tl(t0, t0, t2);
4807 tcg_gen_andc_tl(t1, t1, t2);
4808 tcg_gen_or_tl(cpu_gpr[rA(ctx->opcode)], t0, t1);
4809 tcg_temp_free(t0);
4810 tcg_temp_free(t1);
4811 tcg_temp_free(t2);
76a66253 4812 if (unlikely(Rc(ctx->opcode) != 0))
7487953d 4813 gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
76a66253
JM
4814}
4815
4816/* sliq - sliq. */
99e300ef 4817static void gen_sliq(DisasContext *ctx)
76a66253 4818{
7487953d
AJ
4819 int sh = SH(ctx->opcode);
4820 TCGv t0 = tcg_temp_new();
4821 TCGv t1 = tcg_temp_new();
4822 tcg_gen_shli_tl(t0, cpu_gpr[rS(ctx->opcode)], sh);
4823 tcg_gen_shri_tl(t1, cpu_gpr[rS(ctx->opcode)], 32 - sh);
4824 tcg_gen_or_tl(t1, t0, t1);
4825 tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], t0);
4826 gen_store_spr(SPR_MQ, t1);
4827 tcg_temp_free(t0);
4828 tcg_temp_free(t1);
76a66253 4829 if (unlikely(Rc(ctx->opcode) != 0))
7487953d 4830 gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
76a66253
JM
4831}
4832
4833/* slliq - slliq. */
99e300ef 4834static void gen_slliq(DisasContext *ctx)
76a66253 4835{
7487953d
AJ
4836 int sh = SH(ctx->opcode);
4837 TCGv t0 = tcg_temp_new();
4838 TCGv t1 = tcg_temp_new();
4839 tcg_gen_rotli_tl(t0, cpu_gpr[rS(ctx->opcode)], sh);
4840 gen_load_spr(t1, SPR_MQ);
4841 gen_store_spr(SPR_MQ, t0);
4842 tcg_gen_andi_tl(t0, t0, (0xFFFFFFFFU << sh));
4843 tcg_gen_andi_tl(t1, t1, ~(0xFFFFFFFFU << sh));
4844 tcg_gen_or_tl(cpu_gpr[rA(ctx->opcode)], t0, t1);
4845 tcg_temp_free(t0);
4846 tcg_temp_free(t1);
76a66253 4847 if (unlikely(Rc(ctx->opcode) != 0))
7487953d 4848 gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
76a66253
JM
4849}
4850
4851/* sllq - sllq. */
99e300ef 4852static void gen_sllq(DisasContext *ctx)
76a66253 4853{
7487953d
AJ
4854 int l1 = gen_new_label();
4855 int l2 = gen_new_label();
4856 TCGv t0 = tcg_temp_local_new();
4857 TCGv t1 = tcg_temp_local_new();
4858 TCGv t2 = tcg_temp_local_new();
4859 tcg_gen_andi_tl(t2, cpu_gpr[rB(ctx->opcode)], 0x1F);
4860 tcg_gen_movi_tl(t1, 0xFFFFFFFF);
4861 tcg_gen_shl_tl(t1, t1, t2);
4862 tcg_gen_andi_tl(t0, cpu_gpr[rB(ctx->opcode)], 0x20);
4863 tcg_gen_brcondi_tl(TCG_COND_EQ, t0, 0, l1);
4864 gen_load_spr(t0, SPR_MQ);
4865 tcg_gen_and_tl(cpu_gpr[rA(ctx->opcode)], t0, t1);
4866 tcg_gen_br(l2);
4867 gen_set_label(l1);
4868 tcg_gen_shl_tl(t0, cpu_gpr[rS(ctx->opcode)], t2);
4869 gen_load_spr(t2, SPR_MQ);
4870 tcg_gen_andc_tl(t1, t2, t1);
4871 tcg_gen_or_tl(cpu_gpr[rA(ctx->opcode)], t0, t1);
4872 gen_set_label(l2);
4873 tcg_temp_free(t0);
4874 tcg_temp_free(t1);
4875 tcg_temp_free(t2);
76a66253 4876 if (unlikely(Rc(ctx->opcode) != 0))
7487953d 4877 gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
76a66253
JM
4878}
4879
4880/* slq - slq. */
99e300ef 4881static void gen_slq(DisasContext *ctx)
76a66253 4882{
7487953d
AJ
4883 int l1 = gen_new_label();
4884 TCGv t0 = tcg_temp_new();
4885 TCGv t1 = tcg_temp_new();
4886 tcg_gen_andi_tl(t1, cpu_gpr[rB(ctx->opcode)], 0x1F);
4887 tcg_gen_shl_tl(t0, cpu_gpr[rS(ctx->opcode)], t1);
4888 tcg_gen_subfi_tl(t1, 32, t1);
4889 tcg_gen_shr_tl(t1, cpu_gpr[rS(ctx->opcode)], t1);
4890 tcg_gen_or_tl(t1, t0, t1);
4891 gen_store_spr(SPR_MQ, t1);
4892 tcg_gen_andi_tl(t1, cpu_gpr[rB(ctx->opcode)], 0x20);
4893 tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], t0);
4894 tcg_gen_brcondi_tl(TCG_COND_EQ, t1, 0, l1);
4895 tcg_gen_movi_tl(cpu_gpr[rA(ctx->opcode)], 0);
4896 gen_set_label(l1);
4897 tcg_temp_free(t0);
4898 tcg_temp_free(t1);
76a66253 4899 if (unlikely(Rc(ctx->opcode) != 0))
7487953d 4900 gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
76a66253
JM
4901}
4902
d9bce9d9 4903/* sraiq - sraiq. */
99e300ef 4904static void gen_sraiq(DisasContext *ctx)
76a66253 4905{
7487953d
AJ
4906 int sh = SH(ctx->opcode);
4907 int l1 = gen_new_label();
4908 TCGv t0 = tcg_temp_new();
4909 TCGv t1 = tcg_temp_new();
4910 tcg_gen_shri_tl(t0, cpu_gpr[rS(ctx->opcode)], sh);
4911 tcg_gen_shli_tl(t1, cpu_gpr[rS(ctx->opcode)], 32 - sh);
4912 tcg_gen_or_tl(t0, t0, t1);
4913 gen_store_spr(SPR_MQ, t0);
da91a00f 4914 tcg_gen_movi_tl(cpu_ca, 0);
7487953d
AJ
4915 tcg_gen_brcondi_tl(TCG_COND_EQ, t1, 0, l1);
4916 tcg_gen_brcondi_tl(TCG_COND_GE, cpu_gpr[rS(ctx->opcode)], 0, l1);
da91a00f 4917 tcg_gen_movi_tl(cpu_ca, 1);
7487953d
AJ
4918 gen_set_label(l1);
4919 tcg_gen_sari_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)], sh);
4920 tcg_temp_free(t0);
4921 tcg_temp_free(t1);
76a66253 4922 if (unlikely(Rc(ctx->opcode) != 0))
7487953d 4923 gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
76a66253
JM
4924}
4925
4926/* sraq - sraq. */
99e300ef 4927static void gen_sraq(DisasContext *ctx)
76a66253 4928{
7487953d
AJ
4929 int l1 = gen_new_label();
4930 int l2 = gen_new_label();
4931 TCGv t0 = tcg_temp_new();
4932 TCGv t1 = tcg_temp_local_new();
4933 TCGv t2 = tcg_temp_local_new();
4934 tcg_gen_andi_tl(t2, cpu_gpr[rB(ctx->opcode)], 0x1F);
4935 tcg_gen_shr_tl(t0, cpu_gpr[rS(ctx->opcode)], t2);
4936 tcg_gen_sar_tl(t1, cpu_gpr[rS(ctx->opcode)], t2);
4937 tcg_gen_subfi_tl(t2, 32, t2);
4938 tcg_gen_shl_tl(t2, cpu_gpr[rS(ctx->opcode)], t2);
4939 tcg_gen_or_tl(t0, t0, t2);
4940 gen_store_spr(SPR_MQ, t0);
4941 tcg_gen_andi_tl(t0, cpu_gpr[rB(ctx->opcode)], 0x20);
4942 tcg_gen_brcondi_tl(TCG_COND_EQ, t2, 0, l1);
4943 tcg_gen_mov_tl(t2, cpu_gpr[rS(ctx->opcode)]);
4944 tcg_gen_sari_tl(t1, cpu_gpr[rS(ctx->opcode)], 31);
4945 gen_set_label(l1);
4946 tcg_temp_free(t0);
4947 tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], t1);
da91a00f 4948 tcg_gen_movi_tl(cpu_ca, 0);
7487953d
AJ
4949 tcg_gen_brcondi_tl(TCG_COND_GE, t1, 0, l2);
4950 tcg_gen_brcondi_tl(TCG_COND_EQ, t2, 0, l2);
da91a00f 4951 tcg_gen_movi_tl(cpu_ca, 1);
7487953d
AJ
4952 gen_set_label(l2);
4953 tcg_temp_free(t1);
4954 tcg_temp_free(t2);
76a66253 4955 if (unlikely(Rc(ctx->opcode) != 0))
7487953d 4956 gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
76a66253
JM
4957}
4958
4959/* sre - sre. */
99e300ef 4960static void gen_sre(DisasContext *ctx)
76a66253 4961{
7487953d
AJ
4962 TCGv t0 = tcg_temp_new();
4963 TCGv t1 = tcg_temp_new();
4964 tcg_gen_andi_tl(t1, cpu_gpr[rB(ctx->opcode)], 0x1F);
4965 tcg_gen_shr_tl(t0, cpu_gpr[rS(ctx->opcode)], t1);
4966 tcg_gen_subfi_tl(t1, 32, t1);
4967 tcg_gen_shl_tl(t1, cpu_gpr[rS(ctx->opcode)], t1);
4968 tcg_gen_or_tl(t1, t0, t1);
4969 tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], t0);
4970 gen_store_spr(SPR_MQ, t1);
4971 tcg_temp_free(t0);
4972 tcg_temp_free(t1);
76a66253 4973 if (unlikely(Rc(ctx->opcode) != 0))
7487953d 4974 gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
76a66253
JM
4975}
4976
4977/* srea - srea. */
99e300ef 4978static void gen_srea(DisasContext *ctx)
76a66253 4979{
7487953d
AJ
4980 TCGv t0 = tcg_temp_new();
4981 TCGv t1 = tcg_temp_new();
4982 tcg_gen_andi_tl(t1, cpu_gpr[rB(ctx->opcode)], 0x1F);
4983 tcg_gen_rotr_tl(t0, cpu_gpr[rS(ctx->opcode)], t1);
4984 gen_store_spr(SPR_MQ, t0);
4985 tcg_gen_sar_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)], t1);
4986 tcg_temp_free(t0);
4987 tcg_temp_free(t1);
76a66253 4988 if (unlikely(Rc(ctx->opcode) != 0))
7487953d 4989 gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
76a66253
JM
4990}
4991
4992/* sreq */
99e300ef 4993static void gen_sreq(DisasContext *ctx)
76a66253 4994{
7487953d
AJ
4995 TCGv t0 = tcg_temp_new();
4996 TCGv t1 = tcg_temp_new();
4997 TCGv t2 = tcg_temp_new();
4998 tcg_gen_andi_tl(t0, cpu_gpr[rB(ctx->opcode)], 0x1F);
4999 tcg_gen_movi_tl(t1, 0xFFFFFFFF);
5000 tcg_gen_shr_tl(t1, t1, t0);
5001 tcg_gen_rotr_tl(t0, cpu_gpr[rS(ctx->opcode)], t0);
5002 gen_load_spr(t2, SPR_MQ);
5003 gen_store_spr(SPR_MQ, t0);
5004 tcg_gen_and_tl(t0, t0, t1);
5005 tcg_gen_andc_tl(t2, t2, t1);
5006 tcg_gen_or_tl(cpu_gpr[rA(ctx->opcode)], t0, t2);
5007 tcg_temp_free(t0);
5008 tcg_temp_free(t1);
5009 tcg_temp_free(t2);
76a66253 5010 if (unlikely(Rc(ctx->opcode) != 0))
7487953d 5011 gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
76a66253
JM
5012}
5013
5014/* sriq */
99e300ef 5015static void gen_sriq(DisasContext *ctx)
76a66253 5016{
7487953d
AJ
5017 int sh = SH(ctx->opcode);
5018 TCGv t0 = tcg_temp_new();
5019 TCGv t1 = tcg_temp_new();
5020 tcg_gen_shri_tl(t0, cpu_gpr[rS(ctx->opcode)], sh);
5021 tcg_gen_shli_tl(t1, cpu_gpr[rS(ctx->opcode)], 32 - sh);
5022 tcg_gen_or_tl(t1, t0, t1);
5023 tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], t0);
5024 gen_store_spr(SPR_MQ, t1);
5025 tcg_temp_free(t0);
5026 tcg_temp_free(t1);
76a66253 5027 if (unlikely(Rc(ctx->opcode) != 0))
7487953d 5028 gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
76a66253
JM
5029}
5030
5031/* srliq */
99e300ef 5032static void gen_srliq(DisasContext *ctx)
76a66253 5033{
7487953d
AJ
5034 int sh = SH(ctx->opcode);
5035 TCGv t0 = tcg_temp_new();
5036 TCGv t1 = tcg_temp_new();
5037 tcg_gen_rotri_tl(t0, cpu_gpr[rS(ctx->opcode)], sh);
5038 gen_load_spr(t1, SPR_MQ);
5039 gen_store_spr(SPR_MQ, t0);
5040 tcg_gen_andi_tl(t0, t0, (0xFFFFFFFFU >> sh));
5041 tcg_gen_andi_tl(t1, t1, ~(0xFFFFFFFFU >> sh));
5042 tcg_gen_or_tl(cpu_gpr[rA(ctx->opcode)], t0, t1);
5043 tcg_temp_free(t0);
5044 tcg_temp_free(t1);
76a66253 5045 if (unlikely(Rc(ctx->opcode) != 0))
7487953d 5046 gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
76a66253
JM
5047}
5048
5049/* srlq */
99e300ef 5050static void gen_srlq(DisasContext *ctx)
76a66253 5051{
7487953d
AJ
5052 int l1 = gen_new_label();
5053 int l2 = gen_new_label();
5054 TCGv t0 = tcg_temp_local_new();
5055 TCGv t1 = tcg_temp_local_new();
5056 TCGv t2 = tcg_temp_local_new();
5057 tcg_gen_andi_tl(t2, cpu_gpr[rB(ctx->opcode)], 0x1F);
5058 tcg_gen_movi_tl(t1, 0xFFFFFFFF);
5059 tcg_gen_shr_tl(t2, t1, t2);
5060 tcg_gen_andi_tl(t0, cpu_gpr[rB(ctx->opcode)], 0x20);
5061 tcg_gen_brcondi_tl(TCG_COND_EQ, t0, 0, l1);
5062 gen_load_spr(t0, SPR_MQ);
5063 tcg_gen_and_tl(cpu_gpr[rA(ctx->opcode)], t0, t2);
5064 tcg_gen_br(l2);
5065 gen_set_label(l1);
5066 tcg_gen_shr_tl(t0, cpu_gpr[rS(ctx->opcode)], t2);
5067 tcg_gen_and_tl(t0, t0, t2);
5068 gen_load_spr(t1, SPR_MQ);
5069 tcg_gen_andc_tl(t1, t1, t2);
5070 tcg_gen_or_tl(cpu_gpr[rA(ctx->opcode)], t0, t1);
5071 gen_set_label(l2);
5072 tcg_temp_free(t0);
5073 tcg_temp_free(t1);
5074 tcg_temp_free(t2);
76a66253 5075 if (unlikely(Rc(ctx->opcode) != 0))
7487953d 5076 gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
76a66253
JM
5077}
5078
5079/* srq */
99e300ef 5080static void gen_srq(DisasContext *ctx)
76a66253 5081{
7487953d
AJ
5082 int l1 = gen_new_label();
5083 TCGv t0 = tcg_temp_new();
5084 TCGv t1 = tcg_temp_new();
5085 tcg_gen_andi_tl(t1, cpu_gpr[rB(ctx->opcode)], 0x1F);
5086 tcg_gen_shr_tl(t0, cpu_gpr[rS(ctx->opcode)], t1);
5087 tcg_gen_subfi_tl(t1, 32, t1);
5088 tcg_gen_shl_tl(t1, cpu_gpr[rS(ctx->opcode)], t1);
5089 tcg_gen_or_tl(t1, t0, t1);
5090 gen_store_spr(SPR_MQ, t1);
5091 tcg_gen_andi_tl(t1, cpu_gpr[rB(ctx->opcode)], 0x20);
5092 tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], t0);
5093 tcg_gen_brcondi_tl(TCG_COND_EQ, t0, 0, l1);
5094 tcg_gen_movi_tl(cpu_gpr[rA(ctx->opcode)], 0);
5095 gen_set_label(l1);
5096 tcg_temp_free(t0);
5097 tcg_temp_free(t1);
76a66253 5098 if (unlikely(Rc(ctx->opcode) != 0))
7487953d 5099 gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
76a66253
JM
5100}
5101
5102/* PowerPC 602 specific instructions */
99e300ef 5103
54623277 5104/* dsa */
99e300ef 5105static void gen_dsa(DisasContext *ctx)
76a66253
JM
5106{
5107 /* XXX: TODO */
e06fcd75 5108 gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL);
76a66253
JM
5109}
5110
5111/* esa */
99e300ef 5112static void gen_esa(DisasContext *ctx)
76a66253
JM
5113{
5114 /* XXX: TODO */
e06fcd75 5115 gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL);
76a66253
JM
5116}
5117
5118/* mfrom */
99e300ef 5119static void gen_mfrom(DisasContext *ctx)
76a66253
JM
5120{
5121#if defined(CONFIG_USER_ONLY)
e06fcd75 5122 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
76a66253 5123#else
76db3ba4 5124 if (unlikely(!ctx->mem_idx)) {
e06fcd75 5125 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
76a66253
JM
5126 return;
5127 }
cf02a65c 5128 gen_helper_602_mfrom(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)]);
76a66253
JM
5129#endif
5130}
5131
5132/* 602 - 603 - G2 TLB management */
e8eaa2c0 5133
54623277 5134/* tlbld */
e8eaa2c0 5135static void gen_tlbld_6xx(DisasContext *ctx)
76a66253
JM
5136{
5137#if defined(CONFIG_USER_ONLY)
e06fcd75 5138 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
76a66253 5139#else
76db3ba4 5140 if (unlikely(!ctx->mem_idx)) {
e06fcd75 5141 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
76a66253
JM
5142 return;
5143 }
c6c7cf05 5144 gen_helper_6xx_tlbd(cpu_env, cpu_gpr[rB(ctx->opcode)]);
76a66253
JM
5145#endif
5146}
5147
5148/* tlbli */
e8eaa2c0 5149static void gen_tlbli_6xx(DisasContext *ctx)
76a66253
JM
5150{
5151#if defined(CONFIG_USER_ONLY)
e06fcd75 5152 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
76a66253 5153#else
76db3ba4 5154 if (unlikely(!ctx->mem_idx)) {
e06fcd75 5155 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
76a66253
JM
5156 return;
5157 }
c6c7cf05 5158 gen_helper_6xx_tlbi(cpu_env, cpu_gpr[rB(ctx->opcode)]);
76a66253
JM
5159#endif
5160}
5161
7dbe11ac 5162/* 74xx TLB management */
e8eaa2c0 5163
54623277 5164/* tlbld */
e8eaa2c0 5165static void gen_tlbld_74xx(DisasContext *ctx)
7dbe11ac
JM
5166{
5167#if defined(CONFIG_USER_ONLY)
e06fcd75 5168 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
7dbe11ac 5169#else
76db3ba4 5170 if (unlikely(!ctx->mem_idx)) {
e06fcd75 5171 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
7dbe11ac
JM
5172 return;
5173 }
c6c7cf05 5174 gen_helper_74xx_tlbd(cpu_env, cpu_gpr[rB(ctx->opcode)]);
7dbe11ac
JM
5175#endif
5176}
5177
5178/* tlbli */
e8eaa2c0 5179static void gen_tlbli_74xx(DisasContext *ctx)
7dbe11ac
JM
5180{
5181#if defined(CONFIG_USER_ONLY)
e06fcd75 5182 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
7dbe11ac 5183#else
76db3ba4 5184 if (unlikely(!ctx->mem_idx)) {
e06fcd75 5185 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
7dbe11ac
JM
5186 return;
5187 }
c6c7cf05 5188 gen_helper_74xx_tlbi(cpu_env, cpu_gpr[rB(ctx->opcode)]);
7dbe11ac
JM
5189#endif
5190}
5191
76a66253 5192/* POWER instructions not in PowerPC 601 */
99e300ef 5193
54623277 5194/* clf */
99e300ef 5195static void gen_clf(DisasContext *ctx)
76a66253
JM
5196{
5197 /* Cache line flush: implemented as no-op */
5198}
5199
5200/* cli */
99e300ef 5201static void gen_cli(DisasContext *ctx)
76a66253 5202{
7f75ffd3 5203 /* Cache line invalidate: privileged and treated as no-op */
76a66253 5204#if defined(CONFIG_USER_ONLY)
e06fcd75 5205 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
76a66253 5206#else
76db3ba4 5207 if (unlikely(!ctx->mem_idx)) {
e06fcd75 5208 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
76a66253
JM
5209 return;
5210 }
5211#endif
5212}
5213
5214/* dclst */
99e300ef 5215static void gen_dclst(DisasContext *ctx)
76a66253
JM
5216{
5217 /* Data cache line store: treated as no-op */
5218}
5219
99e300ef 5220static void gen_mfsri(DisasContext *ctx)
76a66253
JM
5221{
5222#if defined(CONFIG_USER_ONLY)
e06fcd75 5223 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
76a66253 5224#else
74d37793
AJ
5225 int ra = rA(ctx->opcode);
5226 int rd = rD(ctx->opcode);
5227 TCGv t0;
76db3ba4 5228 if (unlikely(!ctx->mem_idx)) {
e06fcd75 5229 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
76a66253
JM
5230 return;
5231 }
74d37793 5232 t0 = tcg_temp_new();
76db3ba4 5233 gen_addr_reg_index(ctx, t0);
74d37793
AJ
5234 tcg_gen_shri_tl(t0, t0, 28);
5235 tcg_gen_andi_tl(t0, t0, 0xF);
c6c7cf05 5236 gen_helper_load_sr(cpu_gpr[rd], cpu_env, t0);
74d37793 5237 tcg_temp_free(t0);
76a66253 5238 if (ra != 0 && ra != rd)
74d37793 5239 tcg_gen_mov_tl(cpu_gpr[ra], cpu_gpr[rd]);
76a66253
JM
5240#endif
5241}
5242
99e300ef 5243static void gen_rac(DisasContext *ctx)
76a66253
JM
5244{
5245#if defined(CONFIG_USER_ONLY)
e06fcd75 5246 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
76a66253 5247#else
22e0e173 5248 TCGv t0;
76db3ba4 5249 if (unlikely(!ctx->mem_idx)) {
e06fcd75 5250 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
76a66253
JM
5251 return;
5252 }
22e0e173 5253 t0 = tcg_temp_new();
76db3ba4 5254 gen_addr_reg_index(ctx, t0);
c6c7cf05 5255 gen_helper_rac(cpu_gpr[rD(ctx->opcode)], cpu_env, t0);
22e0e173 5256 tcg_temp_free(t0);
76a66253
JM
5257#endif
5258}
5259
99e300ef 5260static void gen_rfsvc(DisasContext *ctx)
76a66253
JM
5261{
5262#if defined(CONFIG_USER_ONLY)
e06fcd75 5263 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
76a66253 5264#else
76db3ba4 5265 if (unlikely(!ctx->mem_idx)) {
e06fcd75 5266 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
76a66253
JM
5267 return;
5268 }
e5f17ac6 5269 gen_helper_rfsvc(cpu_env);
e06fcd75 5270 gen_sync_exception(ctx);
76a66253
JM
5271#endif
5272}
5273
5274/* svc is not implemented for now */
5275
5276/* POWER2 specific instructions */
5277/* Quad manipulation (load/store two floats at a time) */
76a66253
JM
5278
5279/* lfq */
99e300ef 5280static void gen_lfq(DisasContext *ctx)
76a66253 5281{
01a4afeb 5282 int rd = rD(ctx->opcode);
76db3ba4
AJ
5283 TCGv t0;
5284 gen_set_access_type(ctx, ACCESS_FLOAT);
5285 t0 = tcg_temp_new();
5286 gen_addr_imm_index(ctx, t0, 0);
5287 gen_qemu_ld64(ctx, cpu_fpr[rd], t0);
5288 gen_addr_add(ctx, t0, t0, 8);
5289 gen_qemu_ld64(ctx, cpu_fpr[(rd + 1) % 32], t0);
01a4afeb 5290 tcg_temp_free(t0);
76a66253
JM
5291}
5292
5293/* lfqu */
99e300ef 5294static void gen_lfqu(DisasContext *ctx)
76a66253
JM
5295{
5296 int ra = rA(ctx->opcode);
01a4afeb 5297 int rd = rD(ctx->opcode);
76db3ba4
AJ
5298 TCGv t0, t1;
5299 gen_set_access_type(ctx, ACCESS_FLOAT);
5300 t0 = tcg_temp_new();
5301 t1 = tcg_temp_new();
5302 gen_addr_imm_index(ctx, t0, 0);
5303 gen_qemu_ld64(ctx, cpu_fpr[rd], t0);
5304 gen_addr_add(ctx, t1, t0, 8);
5305 gen_qemu_ld64(ctx, cpu_fpr[(rd + 1) % 32], t1);
76a66253 5306 if (ra != 0)
01a4afeb
AJ
5307 tcg_gen_mov_tl(cpu_gpr[ra], t0);
5308 tcg_temp_free(t0);
5309 tcg_temp_free(t1);
76a66253
JM
5310}
5311
5312/* lfqux */
99e300ef 5313static void gen_lfqux(DisasContext *ctx)
76a66253
JM
5314{
5315 int ra = rA(ctx->opcode);
01a4afeb 5316 int rd = rD(ctx->opcode);
76db3ba4
AJ
5317 gen_set_access_type(ctx, ACCESS_FLOAT);
5318 TCGv t0, t1;
5319 t0 = tcg_temp_new();
5320 gen_addr_reg_index(ctx, t0);
5321 gen_qemu_ld64(ctx, cpu_fpr[rd], t0);
5322 t1 = tcg_temp_new();
5323 gen_addr_add(ctx, t1, t0, 8);
5324 gen_qemu_ld64(ctx, cpu_fpr[(rd + 1) % 32], t1);
5325 tcg_temp_free(t1);
76a66253 5326 if (ra != 0)
01a4afeb
AJ
5327 tcg_gen_mov_tl(cpu_gpr[ra], t0);
5328 tcg_temp_free(t0);
76a66253
JM
5329}
5330
5331/* lfqx */
99e300ef 5332static void gen_lfqx(DisasContext *ctx)
76a66253 5333{
01a4afeb 5334 int rd = rD(ctx->opcode);
76db3ba4
AJ
5335 TCGv t0;
5336 gen_set_access_type(ctx, ACCESS_FLOAT);
5337 t0 = tcg_temp_new();
5338 gen_addr_reg_index(ctx, t0);
5339 gen_qemu_ld64(ctx, cpu_fpr[rd], t0);
5340 gen_addr_add(ctx, t0, t0, 8);
5341 gen_qemu_ld64(ctx, cpu_fpr[(rd + 1) % 32], t0);
01a4afeb 5342 tcg_temp_free(t0);
76a66253
JM
5343}
5344
5345/* stfq */
99e300ef 5346static void gen_stfq(DisasContext *ctx)
76a66253 5347{
01a4afeb 5348 int rd = rD(ctx->opcode);
76db3ba4
AJ
5349 TCGv t0;
5350 gen_set_access_type(ctx, ACCESS_FLOAT);
5351 t0 = tcg_temp_new();
5352 gen_addr_imm_index(ctx, t0, 0);
5353 gen_qemu_st64(ctx, cpu_fpr[rd], t0);
5354 gen_addr_add(ctx, t0, t0, 8);
5355 gen_qemu_st64(ctx, cpu_fpr[(rd + 1) % 32], t0);
01a4afeb 5356 tcg_temp_free(t0);
76a66253
JM
5357}
5358
5359/* stfqu */
99e300ef 5360static void gen_stfqu(DisasContext *ctx)
76a66253
JM
5361{
5362 int ra = rA(ctx->opcode);
01a4afeb 5363 int rd = rD(ctx->opcode);
76db3ba4
AJ
5364 TCGv t0, t1;
5365 gen_set_access_type(ctx, ACCESS_FLOAT);
5366 t0 = tcg_temp_new();
5367 gen_addr_imm_index(ctx, t0, 0);
5368 gen_qemu_st64(ctx, cpu_fpr[rd], t0);
5369 t1 = tcg_temp_new();
5370 gen_addr_add(ctx, t1, t0, 8);
5371 gen_qemu_st64(ctx, cpu_fpr[(rd + 1) % 32], t1);
5372 tcg_temp_free(t1);
76a66253 5373 if (ra != 0)
01a4afeb
AJ
5374 tcg_gen_mov_tl(cpu_gpr[ra], t0);
5375 tcg_temp_free(t0);
76a66253
JM
5376}
5377
5378/* stfqux */
99e300ef 5379static void gen_stfqux(DisasContext *ctx)
76a66253
JM
5380{
5381 int ra = rA(ctx->opcode);
01a4afeb 5382 int rd = rD(ctx->opcode);
76db3ba4
AJ
5383 TCGv t0, t1;
5384 gen_set_access_type(ctx, ACCESS_FLOAT);
5385 t0 = tcg_temp_new();
5386 gen_addr_reg_index(ctx, t0);
5387 gen_qemu_st64(ctx, cpu_fpr[rd], t0);
5388 t1 = tcg_temp_new();
5389 gen_addr_add(ctx, t1, t0, 8);
5390 gen_qemu_st64(ctx, cpu_fpr[(rd + 1) % 32], t1);
5391 tcg_temp_free(t1);
76a66253 5392 if (ra != 0)
01a4afeb
AJ
5393 tcg_gen_mov_tl(cpu_gpr[ra], t0);
5394 tcg_temp_free(t0);
76a66253
JM
5395}
5396
5397/* stfqx */
99e300ef 5398static void gen_stfqx(DisasContext *ctx)
76a66253 5399{
01a4afeb 5400 int rd = rD(ctx->opcode);
76db3ba4
AJ
5401 TCGv t0;
5402 gen_set_access_type(ctx, ACCESS_FLOAT);
5403 t0 = tcg_temp_new();
5404 gen_addr_reg_index(ctx, t0);
5405 gen_qemu_st64(ctx, cpu_fpr[rd], t0);
5406 gen_addr_add(ctx, t0, t0, 8);
5407 gen_qemu_st64(ctx, cpu_fpr[(rd + 1) % 32], t0);
01a4afeb 5408 tcg_temp_free(t0);
76a66253
JM
5409}
5410
5411/* BookE specific instructions */
99e300ef 5412
54623277 5413/* XXX: not implemented on 440 ? */
99e300ef 5414static void gen_mfapidi(DisasContext *ctx)
76a66253
JM
5415{
5416 /* XXX: TODO */
e06fcd75 5417 gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL);
76a66253
JM
5418}
5419
2662a059 5420/* XXX: not implemented on 440 ? */
99e300ef 5421static void gen_tlbiva(DisasContext *ctx)
76a66253
JM
5422{
5423#if defined(CONFIG_USER_ONLY)
e06fcd75 5424 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
76a66253 5425#else
74d37793 5426 TCGv t0;
76db3ba4 5427 if (unlikely(!ctx->mem_idx)) {
e06fcd75 5428 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
76a66253
JM
5429 return;
5430 }
ec72e276 5431 t0 = tcg_temp_new();
76db3ba4 5432 gen_addr_reg_index(ctx, t0);
c6c7cf05 5433 gen_helper_tlbie(cpu_env, cpu_gpr[rB(ctx->opcode)]);
74d37793 5434 tcg_temp_free(t0);
76a66253
JM
5435#endif
5436}
5437
5438/* All 405 MAC instructions are translated here */
636aa200
BS
5439static inline void gen_405_mulladd_insn(DisasContext *ctx, int opc2, int opc3,
5440 int ra, int rb, int rt, int Rc)
76a66253 5441{
182608d4
AJ
5442 TCGv t0, t1;
5443
a7812ae4
PB
5444 t0 = tcg_temp_local_new();
5445 t1 = tcg_temp_local_new();
182608d4 5446
76a66253
JM
5447 switch (opc3 & 0x0D) {
5448 case 0x05:
5449 /* macchw - macchw. - macchwo - macchwo. */
5450 /* macchws - macchws. - macchwso - macchwso. */
5451 /* nmacchw - nmacchw. - nmacchwo - nmacchwo. */
5452 /* nmacchws - nmacchws. - nmacchwso - nmacchwso. */
5453 /* mulchw - mulchw. */
182608d4
AJ
5454 tcg_gen_ext16s_tl(t0, cpu_gpr[ra]);
5455 tcg_gen_sari_tl(t1, cpu_gpr[rb], 16);
5456 tcg_gen_ext16s_tl(t1, t1);
76a66253
JM
5457 break;
5458 case 0x04:
5459 /* macchwu - macchwu. - macchwuo - macchwuo. */
5460 /* macchwsu - macchwsu. - macchwsuo - macchwsuo. */
5461 /* mulchwu - mulchwu. */
182608d4
AJ
5462 tcg_gen_ext16u_tl(t0, cpu_gpr[ra]);
5463 tcg_gen_shri_tl(t1, cpu_gpr[rb], 16);
5464 tcg_gen_ext16u_tl(t1, t1);
76a66253
JM
5465 break;
5466 case 0x01:
5467 /* machhw - machhw. - machhwo - machhwo. */
5468 /* machhws - machhws. - machhwso - machhwso. */
5469 /* nmachhw - nmachhw. - nmachhwo - nmachhwo. */
5470 /* nmachhws - nmachhws. - nmachhwso - nmachhwso. */
5471 /* mulhhw - mulhhw. */
182608d4
AJ
5472 tcg_gen_sari_tl(t0, cpu_gpr[ra], 16);
5473 tcg_gen_ext16s_tl(t0, t0);
5474 tcg_gen_sari_tl(t1, cpu_gpr[rb], 16);
5475 tcg_gen_ext16s_tl(t1, t1);
76a66253
JM
5476 break;
5477 case 0x00:
5478 /* machhwu - machhwu. - machhwuo - machhwuo. */
5479 /* machhwsu - machhwsu. - machhwsuo - machhwsuo. */
5480 /* mulhhwu - mulhhwu. */
182608d4
AJ
5481 tcg_gen_shri_tl(t0, cpu_gpr[ra], 16);
5482 tcg_gen_ext16u_tl(t0, t0);
5483 tcg_gen_shri_tl(t1, cpu_gpr[rb], 16);
5484 tcg_gen_ext16u_tl(t1, t1);
76a66253
JM
5485 break;
5486 case 0x0D:
5487 /* maclhw - maclhw. - maclhwo - maclhwo. */
5488 /* maclhws - maclhws. - maclhwso - maclhwso. */
5489 /* nmaclhw - nmaclhw. - nmaclhwo - nmaclhwo. */
5490 /* nmaclhws - nmaclhws. - nmaclhwso - nmaclhwso. */
5491 /* mullhw - mullhw. */
182608d4
AJ
5492 tcg_gen_ext16s_tl(t0, cpu_gpr[ra]);
5493 tcg_gen_ext16s_tl(t1, cpu_gpr[rb]);
76a66253
JM
5494 break;
5495 case 0x0C:
5496 /* maclhwu - maclhwu. - maclhwuo - maclhwuo. */
5497 /* maclhwsu - maclhwsu. - maclhwsuo - maclhwsuo. */
5498 /* mullhwu - mullhwu. */
182608d4
AJ
5499 tcg_gen_ext16u_tl(t0, cpu_gpr[ra]);
5500 tcg_gen_ext16u_tl(t1, cpu_gpr[rb]);
76a66253
JM
5501 break;
5502 }
76a66253 5503 if (opc2 & 0x04) {
182608d4
AJ
5504 /* (n)multiply-and-accumulate (0x0C / 0x0E) */
5505 tcg_gen_mul_tl(t1, t0, t1);
5506 if (opc2 & 0x02) {
5507 /* nmultiply-and-accumulate (0x0E) */
5508 tcg_gen_sub_tl(t0, cpu_gpr[rt], t1);
5509 } else {
5510 /* multiply-and-accumulate (0x0C) */
5511 tcg_gen_add_tl(t0, cpu_gpr[rt], t1);
5512 }
5513
5514 if (opc3 & 0x12) {
5515 /* Check overflow and/or saturate */
5516 int l1 = gen_new_label();
5517
5518 if (opc3 & 0x10) {
5519 /* Start with XER OV disabled, the most likely case */
da91a00f 5520 tcg_gen_movi_tl(cpu_ov, 0);
182608d4
AJ
5521 }
5522 if (opc3 & 0x01) {
5523 /* Signed */
5524 tcg_gen_xor_tl(t1, cpu_gpr[rt], t1);
5525 tcg_gen_brcondi_tl(TCG_COND_GE, t1, 0, l1);
5526 tcg_gen_xor_tl(t1, cpu_gpr[rt], t0);
5527 tcg_gen_brcondi_tl(TCG_COND_LT, t1, 0, l1);
bdc4e053 5528 if (opc3 & 0x02) {
182608d4
AJ
5529 /* Saturate */
5530 tcg_gen_sari_tl(t0, cpu_gpr[rt], 31);
5531 tcg_gen_xori_tl(t0, t0, 0x7fffffff);
5532 }
5533 } else {
5534 /* Unsigned */
5535 tcg_gen_brcond_tl(TCG_COND_GEU, t0, t1, l1);
bdc4e053 5536 if (opc3 & 0x02) {
182608d4
AJ
5537 /* Saturate */
5538 tcg_gen_movi_tl(t0, UINT32_MAX);
5539 }
5540 }
5541 if (opc3 & 0x10) {
5542 /* Check overflow */
da91a00f
RH
5543 tcg_gen_movi_tl(cpu_ov, 1);
5544 tcg_gen_movi_tl(cpu_so, 1);
182608d4
AJ
5545 }
5546 gen_set_label(l1);
5547 tcg_gen_mov_tl(cpu_gpr[rt], t0);
5548 }
5549 } else {
5550 tcg_gen_mul_tl(cpu_gpr[rt], t0, t1);
76a66253 5551 }
182608d4
AJ
5552 tcg_temp_free(t0);
5553 tcg_temp_free(t1);
76a66253
JM
5554 if (unlikely(Rc) != 0) {
5555 /* Update Rc0 */
182608d4 5556 gen_set_Rc0(ctx, cpu_gpr[rt]);
76a66253
JM
5557 }
5558}
5559
a750fc0b 5560#define GEN_MAC_HANDLER(name, opc2, opc3) \
99e300ef 5561static void glue(gen_, name)(DisasContext *ctx) \
76a66253
JM
5562{ \
5563 gen_405_mulladd_insn(ctx, opc2, opc3, rA(ctx->opcode), rB(ctx->opcode), \
5564 rD(ctx->opcode), Rc(ctx->opcode)); \
5565}
5566
5567/* macchw - macchw. */
a750fc0b 5568GEN_MAC_HANDLER(macchw, 0x0C, 0x05);
76a66253 5569/* macchwo - macchwo. */
a750fc0b 5570GEN_MAC_HANDLER(macchwo, 0x0C, 0x15);
76a66253 5571/* macchws - macchws. */
a750fc0b 5572GEN_MAC_HANDLER(macchws, 0x0C, 0x07);
76a66253 5573/* macchwso - macchwso. */
a750fc0b 5574GEN_MAC_HANDLER(macchwso, 0x0C, 0x17);
76a66253 5575/* macchwsu - macchwsu. */
a750fc0b 5576GEN_MAC_HANDLER(macchwsu, 0x0C, 0x06);
76a66253 5577/* macchwsuo - macchwsuo. */
a750fc0b 5578GEN_MAC_HANDLER(macchwsuo, 0x0C, 0x16);
76a66253 5579/* macchwu - macchwu. */
a750fc0b 5580GEN_MAC_HANDLER(macchwu, 0x0C, 0x04);
76a66253 5581/* macchwuo - macchwuo. */
a750fc0b 5582GEN_MAC_HANDLER(macchwuo, 0x0C, 0x14);
76a66253 5583/* machhw - machhw. */
a750fc0b 5584GEN_MAC_HANDLER(machhw, 0x0C, 0x01);
76a66253 5585/* machhwo - machhwo. */
a750fc0b 5586GEN_MAC_HANDLER(machhwo, 0x0C, 0x11);
76a66253 5587/* machhws - machhws. */
a750fc0b 5588GEN_MAC_HANDLER(machhws, 0x0C, 0x03);
76a66253 5589/* machhwso - machhwso. */
a750fc0b 5590GEN_MAC_HANDLER(machhwso, 0x0C, 0x13);
76a66253 5591/* machhwsu - machhwsu. */
a750fc0b 5592GEN_MAC_HANDLER(machhwsu, 0x0C, 0x02);
76a66253 5593/* machhwsuo - machhwsuo. */
a750fc0b 5594GEN_MAC_HANDLER(machhwsuo, 0x0C, 0x12);
76a66253 5595/* machhwu - machhwu. */
a750fc0b 5596GEN_MAC_HANDLER(machhwu, 0x0C, 0x00);
76a66253 5597/* machhwuo - machhwuo. */
a750fc0b 5598GEN_MAC_HANDLER(machhwuo, 0x0C, 0x10);
76a66253 5599/* maclhw - maclhw. */
a750fc0b 5600GEN_MAC_HANDLER(maclhw, 0x0C, 0x0D);
76a66253 5601/* maclhwo - maclhwo. */
a750fc0b 5602GEN_MAC_HANDLER(maclhwo, 0x0C, 0x1D);
76a66253 5603/* maclhws - maclhws. */
a750fc0b 5604GEN_MAC_HANDLER(maclhws, 0x0C, 0x0F);
76a66253 5605/* maclhwso - maclhwso. */
a750fc0b 5606GEN_MAC_HANDLER(maclhwso, 0x0C, 0x1F);
76a66253 5607/* maclhwu - maclhwu. */
a750fc0b 5608GEN_MAC_HANDLER(maclhwu, 0x0C, 0x0C);
76a66253 5609/* maclhwuo - maclhwuo. */
a750fc0b 5610GEN_MAC_HANDLER(maclhwuo, 0x0C, 0x1C);
76a66253 5611/* maclhwsu - maclhwsu. */
a750fc0b 5612GEN_MAC_HANDLER(maclhwsu, 0x0C, 0x0E);
76a66253 5613/* maclhwsuo - maclhwsuo. */
a750fc0b 5614GEN_MAC_HANDLER(maclhwsuo, 0x0C, 0x1E);
76a66253 5615/* nmacchw - nmacchw. */
a750fc0b 5616GEN_MAC_HANDLER(nmacchw, 0x0E, 0x05);
76a66253 5617/* nmacchwo - nmacchwo. */
a750fc0b 5618GEN_MAC_HANDLER(nmacchwo, 0x0E, 0x15);
76a66253 5619/* nmacchws - nmacchws. */
a750fc0b 5620GEN_MAC_HANDLER(nmacchws, 0x0E, 0x07);
76a66253 5621/* nmacchwso - nmacchwso. */
a750fc0b 5622GEN_MAC_HANDLER(nmacchwso, 0x0E, 0x17);
76a66253 5623/* nmachhw - nmachhw. */
a750fc0b 5624GEN_MAC_HANDLER(nmachhw, 0x0E, 0x01);
76a66253 5625/* nmachhwo - nmachhwo. */
a750fc0b 5626GEN_MAC_HANDLER(nmachhwo, 0x0E, 0x11);
76a66253 5627/* nmachhws - nmachhws. */
a750fc0b 5628GEN_MAC_HANDLER(nmachhws, 0x0E, 0x03);
76a66253 5629/* nmachhwso - nmachhwso. */
a750fc0b 5630GEN_MAC_HANDLER(nmachhwso, 0x0E, 0x13);
76a66253 5631/* nmaclhw - nmaclhw. */
a750fc0b 5632GEN_MAC_HANDLER(nmaclhw, 0x0E, 0x0D);
76a66253 5633/* nmaclhwo - nmaclhwo. */
a750fc0b 5634GEN_MAC_HANDLER(nmaclhwo, 0x0E, 0x1D);
76a66253 5635/* nmaclhws - nmaclhws. */
a750fc0b 5636GEN_MAC_HANDLER(nmaclhws, 0x0E, 0x0F);
76a66253 5637/* nmaclhwso - nmaclhwso. */
a750fc0b 5638GEN_MAC_HANDLER(nmaclhwso, 0x0E, 0x1F);
76a66253
JM
5639
5640/* mulchw - mulchw. */
a750fc0b 5641GEN_MAC_HANDLER(mulchw, 0x08, 0x05);
76a66253 5642/* mulchwu - mulchwu. */
a750fc0b 5643GEN_MAC_HANDLER(mulchwu, 0x08, 0x04);
76a66253 5644/* mulhhw - mulhhw. */
a750fc0b 5645GEN_MAC_HANDLER(mulhhw, 0x08, 0x01);
76a66253 5646/* mulhhwu - mulhhwu. */
a750fc0b 5647GEN_MAC_HANDLER(mulhhwu, 0x08, 0x00);
76a66253 5648/* mullhw - mullhw. */
a750fc0b 5649GEN_MAC_HANDLER(mullhw, 0x08, 0x0D);
76a66253 5650/* mullhwu - mullhwu. */
a750fc0b 5651GEN_MAC_HANDLER(mullhwu, 0x08, 0x0C);
76a66253
JM
5652
5653/* mfdcr */
99e300ef 5654static void gen_mfdcr(DisasContext *ctx)
76a66253
JM
5655{
5656#if defined(CONFIG_USER_ONLY)
e06fcd75 5657 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
76a66253 5658#else
06dca6a7 5659 TCGv dcrn;
76db3ba4 5660 if (unlikely(!ctx->mem_idx)) {
e06fcd75 5661 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
76a66253
JM
5662 return;
5663 }
06dca6a7
AJ
5664 /* NIP cannot be restored if the memory exception comes from an helper */
5665 gen_update_nip(ctx, ctx->nip - 4);
5666 dcrn = tcg_const_tl(SPR(ctx->opcode));
d0f1562d 5667 gen_helper_load_dcr(cpu_gpr[rD(ctx->opcode)], cpu_env, dcrn);
06dca6a7 5668 tcg_temp_free(dcrn);
76a66253
JM
5669#endif
5670}
5671
5672/* mtdcr */
99e300ef 5673static void gen_mtdcr(DisasContext *ctx)
76a66253
JM
5674{
5675#if defined(CONFIG_USER_ONLY)
e06fcd75 5676 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
76a66253 5677#else
06dca6a7 5678 TCGv dcrn;
76db3ba4 5679 if (unlikely(!ctx->mem_idx)) {
e06fcd75 5680 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
76a66253
JM
5681 return;
5682 }
06dca6a7
AJ
5683 /* NIP cannot be restored if the memory exception comes from an helper */
5684 gen_update_nip(ctx, ctx->nip - 4);
5685 dcrn = tcg_const_tl(SPR(ctx->opcode));
d0f1562d 5686 gen_helper_store_dcr(cpu_env, dcrn, cpu_gpr[rS(ctx->opcode)]);
06dca6a7 5687 tcg_temp_free(dcrn);
a42bd6cc
JM
5688#endif
5689}
5690
5691/* mfdcrx */
2662a059 5692/* XXX: not implemented on 440 ? */
99e300ef 5693static void gen_mfdcrx(DisasContext *ctx)
a42bd6cc
JM
5694{
5695#if defined(CONFIG_USER_ONLY)
e06fcd75 5696 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
a42bd6cc 5697#else
76db3ba4 5698 if (unlikely(!ctx->mem_idx)) {
e06fcd75 5699 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
a42bd6cc
JM
5700 return;
5701 }
06dca6a7
AJ
5702 /* NIP cannot be restored if the memory exception comes from an helper */
5703 gen_update_nip(ctx, ctx->nip - 4);
d0f1562d
BS
5704 gen_helper_load_dcr(cpu_gpr[rD(ctx->opcode)], cpu_env,
5705 cpu_gpr[rA(ctx->opcode)]);
a750fc0b 5706 /* Note: Rc update flag set leads to undefined state of Rc0 */
a42bd6cc
JM
5707#endif
5708}
5709
5710/* mtdcrx */
2662a059 5711/* XXX: not implemented on 440 ? */
99e300ef 5712static void gen_mtdcrx(DisasContext *ctx)
a42bd6cc
JM
5713{
5714#if defined(CONFIG_USER_ONLY)
e06fcd75 5715 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
a42bd6cc 5716#else
76db3ba4 5717 if (unlikely(!ctx->mem_idx)) {
e06fcd75 5718 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
a42bd6cc
JM
5719 return;
5720 }
06dca6a7
AJ
5721 /* NIP cannot be restored if the memory exception comes from an helper */
5722 gen_update_nip(ctx, ctx->nip - 4);
d0f1562d
BS
5723 gen_helper_store_dcr(cpu_env, cpu_gpr[rA(ctx->opcode)],
5724 cpu_gpr[rS(ctx->opcode)]);
a750fc0b 5725 /* Note: Rc update flag set leads to undefined state of Rc0 */
76a66253
JM
5726#endif
5727}
5728
a750fc0b 5729/* mfdcrux (PPC 460) : user-mode access to DCR */
99e300ef 5730static void gen_mfdcrux(DisasContext *ctx)
a750fc0b 5731{
06dca6a7
AJ
5732 /* NIP cannot be restored if the memory exception comes from an helper */
5733 gen_update_nip(ctx, ctx->nip - 4);
d0f1562d
BS
5734 gen_helper_load_dcr(cpu_gpr[rD(ctx->opcode)], cpu_env,
5735 cpu_gpr[rA(ctx->opcode)]);
a750fc0b
JM
5736 /* Note: Rc update flag set leads to undefined state of Rc0 */
5737}
5738
5739/* mtdcrux (PPC 460) : user-mode access to DCR */
99e300ef 5740static void gen_mtdcrux(DisasContext *ctx)
a750fc0b 5741{
06dca6a7
AJ
5742 /* NIP cannot be restored if the memory exception comes from an helper */
5743 gen_update_nip(ctx, ctx->nip - 4);
975e5463 5744 gen_helper_store_dcr(cpu_env, cpu_gpr[rA(ctx->opcode)],
d0f1562d 5745 cpu_gpr[rS(ctx->opcode)]);
a750fc0b
JM
5746 /* Note: Rc update flag set leads to undefined state of Rc0 */
5747}
5748
76a66253 5749/* dccci */
99e300ef 5750static void gen_dccci(DisasContext *ctx)
76a66253
JM
5751{
5752#if defined(CONFIG_USER_ONLY)
e06fcd75 5753 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
76a66253 5754#else
76db3ba4 5755 if (unlikely(!ctx->mem_idx)) {
e06fcd75 5756 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
76a66253
JM
5757 return;
5758 }
5759 /* interpreted as no-op */
5760#endif
5761}
5762
5763/* dcread */
99e300ef 5764static void gen_dcread(DisasContext *ctx)
76a66253
JM
5765{
5766#if defined(CONFIG_USER_ONLY)
e06fcd75 5767 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
76a66253 5768#else
b61f2753 5769 TCGv EA, val;
76db3ba4 5770 if (unlikely(!ctx->mem_idx)) {
e06fcd75 5771 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
76a66253
JM
5772 return;
5773 }
76db3ba4 5774 gen_set_access_type(ctx, ACCESS_CACHE);
a7812ae4 5775 EA = tcg_temp_new();
76db3ba4 5776 gen_addr_reg_index(ctx, EA);
a7812ae4 5777 val = tcg_temp_new();
76db3ba4 5778 gen_qemu_ld32u(ctx, val, EA);
b61f2753
AJ
5779 tcg_temp_free(val);
5780 tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], EA);
5781 tcg_temp_free(EA);
76a66253
JM
5782#endif
5783}
5784
5785/* icbt */
e8eaa2c0 5786static void gen_icbt_40x(DisasContext *ctx)
76a66253
JM
5787{
5788 /* interpreted as no-op */
5789 /* XXX: specification say this is treated as a load by the MMU
5790 * but does not generate any exception
5791 */
5792}
5793
5794/* iccci */
99e300ef 5795static void gen_iccci(DisasContext *ctx)
76a66253
JM
5796{
5797#if defined(CONFIG_USER_ONLY)
e06fcd75 5798 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
76a66253 5799#else
76db3ba4 5800 if (unlikely(!ctx->mem_idx)) {
e06fcd75 5801 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
76a66253
JM
5802 return;
5803 }
5804 /* interpreted as no-op */
5805#endif
5806}
5807
5808/* icread */
99e300ef 5809static void gen_icread(DisasContext *ctx)
76a66253
JM
5810{
5811#if defined(CONFIG_USER_ONLY)
e06fcd75 5812 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
76a66253 5813#else
76db3ba4 5814 if (unlikely(!ctx->mem_idx)) {
e06fcd75 5815 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
76a66253
JM
5816 return;
5817 }
5818 /* interpreted as no-op */
5819#endif
5820}
5821
76db3ba4 5822/* rfci (mem_idx only) */
e8eaa2c0 5823static void gen_rfci_40x(DisasContext *ctx)
a42bd6cc
JM
5824{
5825#if defined(CONFIG_USER_ONLY)
e06fcd75 5826 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
a42bd6cc 5827#else
76db3ba4 5828 if (unlikely(!ctx->mem_idx)) {
e06fcd75 5829 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
a42bd6cc
JM
5830 return;
5831 }
5832 /* Restore CPU state */
e5f17ac6 5833 gen_helper_40x_rfci(cpu_env);
e06fcd75 5834 gen_sync_exception(ctx);
a42bd6cc
JM
5835#endif
5836}
5837
99e300ef 5838static void gen_rfci(DisasContext *ctx)
a42bd6cc
JM
5839{
5840#if defined(CONFIG_USER_ONLY)
e06fcd75 5841 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
a42bd6cc 5842#else
76db3ba4 5843 if (unlikely(!ctx->mem_idx)) {
e06fcd75 5844 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
a42bd6cc
JM
5845 return;
5846 }
5847 /* Restore CPU state */
e5f17ac6 5848 gen_helper_rfci(cpu_env);
e06fcd75 5849 gen_sync_exception(ctx);
a42bd6cc
JM
5850#endif
5851}
5852
5853/* BookE specific */
99e300ef 5854
54623277 5855/* XXX: not implemented on 440 ? */
99e300ef 5856static void gen_rfdi(DisasContext *ctx)
76a66253
JM
5857{
5858#if defined(CONFIG_USER_ONLY)
e06fcd75 5859 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
76a66253 5860#else
76db3ba4 5861 if (unlikely(!ctx->mem_idx)) {
e06fcd75 5862 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
76a66253
JM
5863 return;
5864 }
5865 /* Restore CPU state */
e5f17ac6 5866 gen_helper_rfdi(cpu_env);
e06fcd75 5867 gen_sync_exception(ctx);
76a66253
JM
5868#endif
5869}
5870
2662a059 5871/* XXX: not implemented on 440 ? */
99e300ef 5872static void gen_rfmci(DisasContext *ctx)
a42bd6cc
JM
5873{
5874#if defined(CONFIG_USER_ONLY)
e06fcd75 5875 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
a42bd6cc 5876#else
76db3ba4 5877 if (unlikely(!ctx->mem_idx)) {
e06fcd75 5878 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
a42bd6cc
JM
5879 return;
5880 }
5881 /* Restore CPU state */
e5f17ac6 5882 gen_helper_rfmci(cpu_env);
e06fcd75 5883 gen_sync_exception(ctx);
a42bd6cc
JM
5884#endif
5885}
5eb7995e 5886
d9bce9d9 5887/* TLB management - PowerPC 405 implementation */
e8eaa2c0 5888
54623277 5889/* tlbre */
e8eaa2c0 5890static void gen_tlbre_40x(DisasContext *ctx)
76a66253
JM
5891{
5892#if defined(CONFIG_USER_ONLY)
e06fcd75 5893 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
76a66253 5894#else
76db3ba4 5895 if (unlikely(!ctx->mem_idx)) {
e06fcd75 5896 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
76a66253
JM
5897 return;
5898 }
5899 switch (rB(ctx->opcode)) {
5900 case 0:
c6c7cf05
BS
5901 gen_helper_4xx_tlbre_hi(cpu_gpr[rD(ctx->opcode)], cpu_env,
5902 cpu_gpr[rA(ctx->opcode)]);
76a66253
JM
5903 break;
5904 case 1:
c6c7cf05
BS
5905 gen_helper_4xx_tlbre_lo(cpu_gpr[rD(ctx->opcode)], cpu_env,
5906 cpu_gpr[rA(ctx->opcode)]);
76a66253
JM
5907 break;
5908 default:
e06fcd75 5909 gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL);
76a66253 5910 break;
9a64fbe4 5911 }
76a66253
JM
5912#endif
5913}
5914
d9bce9d9 5915/* tlbsx - tlbsx. */
e8eaa2c0 5916static void gen_tlbsx_40x(DisasContext *ctx)
76a66253
JM
5917{
5918#if defined(CONFIG_USER_ONLY)
e06fcd75 5919 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
76a66253 5920#else
74d37793 5921 TCGv t0;
76db3ba4 5922 if (unlikely(!ctx->mem_idx)) {
e06fcd75 5923 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
76a66253
JM
5924 return;
5925 }
74d37793 5926 t0 = tcg_temp_new();
76db3ba4 5927 gen_addr_reg_index(ctx, t0);
c6c7cf05 5928 gen_helper_4xx_tlbsx(cpu_gpr[rD(ctx->opcode)], cpu_env, t0);
74d37793
AJ
5929 tcg_temp_free(t0);
5930 if (Rc(ctx->opcode)) {
5931 int l1 = gen_new_label();
da91a00f 5932 tcg_gen_trunc_tl_i32(cpu_crf[0], cpu_so);
74d37793
AJ
5933 tcg_gen_brcondi_tl(TCG_COND_EQ, cpu_gpr[rD(ctx->opcode)], -1, l1);
5934 tcg_gen_ori_i32(cpu_crf[0], cpu_crf[0], 0x02);
5935 gen_set_label(l1);
5936 }
76a66253 5937#endif
79aceca5
FB
5938}
5939
76a66253 5940/* tlbwe */
e8eaa2c0 5941static void gen_tlbwe_40x(DisasContext *ctx)
79aceca5 5942{
76a66253 5943#if defined(CONFIG_USER_ONLY)
e06fcd75 5944 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
76a66253 5945#else
76db3ba4 5946 if (unlikely(!ctx->mem_idx)) {
e06fcd75 5947 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
76a66253
JM
5948 return;
5949 }
5950 switch (rB(ctx->opcode)) {
5951 case 0:
c6c7cf05
BS
5952 gen_helper_4xx_tlbwe_hi(cpu_env, cpu_gpr[rA(ctx->opcode)],
5953 cpu_gpr[rS(ctx->opcode)]);
76a66253
JM
5954 break;
5955 case 1:
c6c7cf05
BS
5956 gen_helper_4xx_tlbwe_lo(cpu_env, cpu_gpr[rA(ctx->opcode)],
5957 cpu_gpr[rS(ctx->opcode)]);
76a66253
JM
5958 break;
5959 default:
e06fcd75 5960 gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL);
76a66253 5961 break;
9a64fbe4 5962 }
76a66253
JM
5963#endif
5964}
5965
a4bb6c3e 5966/* TLB management - PowerPC 440 implementation */
e8eaa2c0 5967
54623277 5968/* tlbre */
e8eaa2c0 5969static void gen_tlbre_440(DisasContext *ctx)
5eb7995e
JM
5970{
5971#if defined(CONFIG_USER_ONLY)
e06fcd75 5972 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
5eb7995e 5973#else
76db3ba4 5974 if (unlikely(!ctx->mem_idx)) {
e06fcd75 5975 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
5eb7995e
JM
5976 return;
5977 }
5978 switch (rB(ctx->opcode)) {
5979 case 0:
5eb7995e 5980 case 1:
5eb7995e 5981 case 2:
74d37793
AJ
5982 {
5983 TCGv_i32 t0 = tcg_const_i32(rB(ctx->opcode));
c6c7cf05
BS
5984 gen_helper_440_tlbre(cpu_gpr[rD(ctx->opcode)], cpu_env,
5985 t0, cpu_gpr[rA(ctx->opcode)]);
74d37793
AJ
5986 tcg_temp_free_i32(t0);
5987 }
5eb7995e
JM
5988 break;
5989 default:
e06fcd75 5990 gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL);
5eb7995e
JM
5991 break;
5992 }
5993#endif
5994}
5995
5996/* tlbsx - tlbsx. */
e8eaa2c0 5997static void gen_tlbsx_440(DisasContext *ctx)
5eb7995e
JM
5998{
5999#if defined(CONFIG_USER_ONLY)
e06fcd75 6000 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
5eb7995e 6001#else
74d37793 6002 TCGv t0;
76db3ba4 6003 if (unlikely(!ctx->mem_idx)) {
e06fcd75 6004 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
5eb7995e
JM
6005 return;
6006 }
74d37793 6007 t0 = tcg_temp_new();
76db3ba4 6008 gen_addr_reg_index(ctx, t0);
c6c7cf05 6009 gen_helper_440_tlbsx(cpu_gpr[rD(ctx->opcode)], cpu_env, t0);
74d37793
AJ
6010 tcg_temp_free(t0);
6011 if (Rc(ctx->opcode)) {
6012 int l1 = gen_new_label();
da91a00f 6013 tcg_gen_trunc_tl_i32(cpu_crf[0], cpu_so);
74d37793
AJ
6014 tcg_gen_brcondi_tl(TCG_COND_EQ, cpu_gpr[rD(ctx->opcode)], -1, l1);
6015 tcg_gen_ori_i32(cpu_crf[0], cpu_crf[0], 0x02);
6016 gen_set_label(l1);
6017 }
5eb7995e
JM
6018#endif
6019}
6020
6021/* tlbwe */
e8eaa2c0 6022static void gen_tlbwe_440(DisasContext *ctx)
5eb7995e
JM
6023{
6024#if defined(CONFIG_USER_ONLY)
e06fcd75 6025 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
5eb7995e 6026#else
76db3ba4 6027 if (unlikely(!ctx->mem_idx)) {
e06fcd75 6028 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
5eb7995e
JM
6029 return;
6030 }
6031 switch (rB(ctx->opcode)) {
6032 case 0:
5eb7995e 6033 case 1:
5eb7995e 6034 case 2:
74d37793
AJ
6035 {
6036 TCGv_i32 t0 = tcg_const_i32(rB(ctx->opcode));
c6c7cf05
BS
6037 gen_helper_440_tlbwe(cpu_env, t0, cpu_gpr[rA(ctx->opcode)],
6038 cpu_gpr[rS(ctx->opcode)]);
74d37793
AJ
6039 tcg_temp_free_i32(t0);
6040 }
5eb7995e
JM
6041 break;
6042 default:
e06fcd75 6043 gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL);
5eb7995e
JM
6044 break;
6045 }
6046#endif
6047}
6048
01662f3e
AG
6049/* TLB management - PowerPC BookE 2.06 implementation */
6050
6051/* tlbre */
6052static void gen_tlbre_booke206(DisasContext *ctx)
6053{
6054#if defined(CONFIG_USER_ONLY)
6055 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
6056#else
6057 if (unlikely(!ctx->mem_idx)) {
6058 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
6059 return;
6060 }
6061
c6c7cf05 6062 gen_helper_booke206_tlbre(cpu_env);
01662f3e
AG
6063#endif
6064}
6065
6066/* tlbsx - tlbsx. */
6067static void gen_tlbsx_booke206(DisasContext *ctx)
6068{
6069#if defined(CONFIG_USER_ONLY)
6070 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
6071#else
6072 TCGv t0;
6073 if (unlikely(!ctx->mem_idx)) {
6074 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
6075 return;
6076 }
6077
6078 if (rA(ctx->opcode)) {
6079 t0 = tcg_temp_new();
6080 tcg_gen_mov_tl(t0, cpu_gpr[rD(ctx->opcode)]);
6081 } else {
6082 t0 = tcg_const_tl(0);
6083 }
6084
6085 tcg_gen_add_tl(t0, t0, cpu_gpr[rB(ctx->opcode)]);
c6c7cf05 6086 gen_helper_booke206_tlbsx(cpu_env, t0);
01662f3e
AG
6087#endif
6088}
6089
6090/* tlbwe */
6091static void gen_tlbwe_booke206(DisasContext *ctx)
6092{
6093#if defined(CONFIG_USER_ONLY)
6094 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
6095#else
6096 if (unlikely(!ctx->mem_idx)) {
6097 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
6098 return;
6099 }
3f162d11 6100 gen_update_nip(ctx, ctx->nip - 4);
c6c7cf05 6101 gen_helper_booke206_tlbwe(cpu_env);
01662f3e
AG
6102#endif
6103}
6104
6105static void gen_tlbivax_booke206(DisasContext *ctx)
6106{
6107#if defined(CONFIG_USER_ONLY)
6108 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
6109#else
6110 TCGv t0;
6111 if (unlikely(!ctx->mem_idx)) {
6112 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
6113 return;
6114 }
6115
6116 t0 = tcg_temp_new();
6117 gen_addr_reg_index(ctx, t0);
6118
c6c7cf05 6119 gen_helper_booke206_tlbivax(cpu_env, t0);
01662f3e
AG
6120#endif
6121}
6122
6d3db821
AG
6123static void gen_tlbilx_booke206(DisasContext *ctx)
6124{
6125#if defined(CONFIG_USER_ONLY)
6126 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
6127#else
6128 TCGv t0;
6129 if (unlikely(!ctx->mem_idx)) {
6130 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
6131 return;
6132 }
6133
6134 t0 = tcg_temp_new();
6135 gen_addr_reg_index(ctx, t0);
6136
6137 switch((ctx->opcode >> 21) & 0x3) {
6138 case 0:
c6c7cf05 6139 gen_helper_booke206_tlbilx0(cpu_env, t0);
6d3db821
AG
6140 break;
6141 case 1:
c6c7cf05 6142 gen_helper_booke206_tlbilx1(cpu_env, t0);
6d3db821
AG
6143 break;
6144 case 3:
c6c7cf05 6145 gen_helper_booke206_tlbilx3(cpu_env, t0);
6d3db821
AG
6146 break;
6147 default:
6148 gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL);
6149 break;
6150 }
6151
6152 tcg_temp_free(t0);
6153#endif
6154}
6155
01662f3e 6156
76a66253 6157/* wrtee */
99e300ef 6158static void gen_wrtee(DisasContext *ctx)
76a66253
JM
6159{
6160#if defined(CONFIG_USER_ONLY)
e06fcd75 6161 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
76a66253 6162#else
6527f6ea 6163 TCGv t0;
76db3ba4 6164 if (unlikely(!ctx->mem_idx)) {
e06fcd75 6165 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
76a66253
JM
6166 return;
6167 }
6527f6ea
AJ
6168 t0 = tcg_temp_new();
6169 tcg_gen_andi_tl(t0, cpu_gpr[rD(ctx->opcode)], (1 << MSR_EE));
6170 tcg_gen_andi_tl(cpu_msr, cpu_msr, ~(1 << MSR_EE));
6171 tcg_gen_or_tl(cpu_msr, cpu_msr, t0);
6172 tcg_temp_free(t0);
dee96f6c
JM
6173 /* Stop translation to have a chance to raise an exception
6174 * if we just set msr_ee to 1
6175 */
e06fcd75 6176 gen_stop_exception(ctx);
76a66253
JM
6177#endif
6178}
6179
6180/* wrteei */
99e300ef 6181static void gen_wrteei(DisasContext *ctx)
76a66253
JM
6182{
6183#if defined(CONFIG_USER_ONLY)
e06fcd75 6184 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
76a66253 6185#else
76db3ba4 6186 if (unlikely(!ctx->mem_idx)) {
e06fcd75 6187 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
76a66253
JM
6188 return;
6189 }
fbe73008 6190 if (ctx->opcode & 0x00008000) {
6527f6ea
AJ
6191 tcg_gen_ori_tl(cpu_msr, cpu_msr, (1 << MSR_EE));
6192 /* Stop translation to have a chance to raise an exception */
e06fcd75 6193 gen_stop_exception(ctx);
6527f6ea 6194 } else {
1b6e5f99 6195 tcg_gen_andi_tl(cpu_msr, cpu_msr, ~(1 << MSR_EE));
6527f6ea 6196 }
76a66253
JM
6197#endif
6198}
6199
08e46e54 6200/* PowerPC 440 specific instructions */
99e300ef 6201
54623277 6202/* dlmzb */
99e300ef 6203static void gen_dlmzb(DisasContext *ctx)
76a66253 6204{
ef0d51af 6205 TCGv_i32 t0 = tcg_const_i32(Rc(ctx->opcode));
d15f74fb
BS
6206 gen_helper_dlmzb(cpu_gpr[rA(ctx->opcode)], cpu_env,
6207 cpu_gpr[rS(ctx->opcode)], cpu_gpr[rB(ctx->opcode)], t0);
ef0d51af 6208 tcg_temp_free_i32(t0);
76a66253
JM
6209}
6210
6211/* mbar replaces eieio on 440 */
99e300ef 6212static void gen_mbar(DisasContext *ctx)
76a66253
JM
6213{
6214 /* interpreted as no-op */
6215}
6216
6217/* msync replaces sync on 440 */
dcb2b9e1 6218static void gen_msync_4xx(DisasContext *ctx)
76a66253
JM
6219{
6220 /* interpreted as no-op */
6221}
6222
6223/* icbt */
e8eaa2c0 6224static void gen_icbt_440(DisasContext *ctx)
76a66253
JM
6225{
6226 /* interpreted as no-op */
6227 /* XXX: specification say this is treated as a load by the MMU
6228 * but does not generate any exception
6229 */
79aceca5
FB
6230}
6231
9e0b5cb1
AG
6232/* Embedded.Processor Control */
6233
6234static void gen_msgclr(DisasContext *ctx)
6235{
6236#if defined(CONFIG_USER_ONLY)
6237 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
6238#else
6239 if (unlikely(ctx->mem_idx == 0)) {
6240 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
6241 return;
6242 }
6243
e5f17ac6 6244 gen_helper_msgclr(cpu_env, cpu_gpr[rB(ctx->opcode)]);
9e0b5cb1
AG
6245#endif
6246}
6247
d5d11a39
AG
6248static void gen_msgsnd(DisasContext *ctx)
6249{
6250#if defined(CONFIG_USER_ONLY)
6251 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
6252#else
6253 if (unlikely(ctx->mem_idx == 0)) {
6254 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
6255 return;
6256 }
6257
6258 gen_helper_msgsnd(cpu_gpr[rB(ctx->opcode)]);
6259#endif
6260}
6261
a9d9eb8f
JM
6262/*** Altivec vector extension ***/
6263/* Altivec registers moves */
a9d9eb8f 6264
636aa200 6265static inline TCGv_ptr gen_avr_ptr(int reg)
564e571a 6266{
e4704b3b 6267 TCGv_ptr r = tcg_temp_new_ptr();
564e571a
AJ
6268 tcg_gen_addi_ptr(r, cpu_env, offsetof(CPUPPCState, avr[reg]));
6269 return r;
6270}
6271
a9d9eb8f 6272#define GEN_VR_LDX(name, opc2, opc3) \
99e300ef 6273static void glue(gen_, name)(DisasContext *ctx) \
a9d9eb8f 6274{ \
fe1e5c53 6275 TCGv EA; \
a9d9eb8f 6276 if (unlikely(!ctx->altivec_enabled)) { \
e06fcd75 6277 gen_exception(ctx, POWERPC_EXCP_VPU); \
a9d9eb8f
JM
6278 return; \
6279 } \
76db3ba4 6280 gen_set_access_type(ctx, ACCESS_INT); \
fe1e5c53 6281 EA = tcg_temp_new(); \
76db3ba4 6282 gen_addr_reg_index(ctx, EA); \
fe1e5c53 6283 tcg_gen_andi_tl(EA, EA, ~0xf); \
76db3ba4
AJ
6284 if (ctx->le_mode) { \
6285 gen_qemu_ld64(ctx, cpu_avrl[rD(ctx->opcode)], EA); \
fe1e5c53 6286 tcg_gen_addi_tl(EA, EA, 8); \
76db3ba4 6287 gen_qemu_ld64(ctx, cpu_avrh[rD(ctx->opcode)], EA); \
fe1e5c53 6288 } else { \
76db3ba4 6289 gen_qemu_ld64(ctx, cpu_avrh[rD(ctx->opcode)], EA); \
fe1e5c53 6290 tcg_gen_addi_tl(EA, EA, 8); \
76db3ba4 6291 gen_qemu_ld64(ctx, cpu_avrl[rD(ctx->opcode)], EA); \
fe1e5c53
AJ
6292 } \
6293 tcg_temp_free(EA); \
a9d9eb8f
JM
6294}
6295
6296#define GEN_VR_STX(name, opc2, opc3) \
99e300ef 6297static void gen_st##name(DisasContext *ctx) \
a9d9eb8f 6298{ \
fe1e5c53 6299 TCGv EA; \
a9d9eb8f 6300 if (unlikely(!ctx->altivec_enabled)) { \
e06fcd75 6301 gen_exception(ctx, POWERPC_EXCP_VPU); \
a9d9eb8f
JM
6302 return; \
6303 } \
76db3ba4 6304 gen_set_access_type(ctx, ACCESS_INT); \
fe1e5c53 6305 EA = tcg_temp_new(); \
76db3ba4 6306 gen_addr_reg_index(ctx, EA); \
fe1e5c53 6307 tcg_gen_andi_tl(EA, EA, ~0xf); \
76db3ba4
AJ
6308 if (ctx->le_mode) { \
6309 gen_qemu_st64(ctx, cpu_avrl[rD(ctx->opcode)], EA); \
fe1e5c53 6310 tcg_gen_addi_tl(EA, EA, 8); \
76db3ba4 6311 gen_qemu_st64(ctx, cpu_avrh[rD(ctx->opcode)], EA); \
fe1e5c53 6312 } else { \
76db3ba4 6313 gen_qemu_st64(ctx, cpu_avrh[rD(ctx->opcode)], EA); \
fe1e5c53 6314 tcg_gen_addi_tl(EA, EA, 8); \
76db3ba4 6315 gen_qemu_st64(ctx, cpu_avrl[rD(ctx->opcode)], EA); \
fe1e5c53
AJ
6316 } \
6317 tcg_temp_free(EA); \
a9d9eb8f
JM
6318}
6319
cbfb6ae9 6320#define GEN_VR_LVE(name, opc2, opc3) \
99e300ef 6321static void gen_lve##name(DisasContext *ctx) \
cbfb6ae9
AJ
6322 { \
6323 TCGv EA; \
6324 TCGv_ptr rs; \
6325 if (unlikely(!ctx->altivec_enabled)) { \
6326 gen_exception(ctx, POWERPC_EXCP_VPU); \
6327 return; \
6328 } \
6329 gen_set_access_type(ctx, ACCESS_INT); \
6330 EA = tcg_temp_new(); \
6331 gen_addr_reg_index(ctx, EA); \
6332 rs = gen_avr_ptr(rS(ctx->opcode)); \
2f5a189c 6333 gen_helper_lve##name(cpu_env, rs, EA); \
cbfb6ae9
AJ
6334 tcg_temp_free(EA); \
6335 tcg_temp_free_ptr(rs); \
6336 }
6337
6338#define GEN_VR_STVE(name, opc2, opc3) \
99e300ef 6339static void gen_stve##name(DisasContext *ctx) \
cbfb6ae9
AJ
6340 { \
6341 TCGv EA; \
6342 TCGv_ptr rs; \
6343 if (unlikely(!ctx->altivec_enabled)) { \
6344 gen_exception(ctx, POWERPC_EXCP_VPU); \
6345 return; \
6346 } \
6347 gen_set_access_type(ctx, ACCESS_INT); \
6348 EA = tcg_temp_new(); \
6349 gen_addr_reg_index(ctx, EA); \
6350 rs = gen_avr_ptr(rS(ctx->opcode)); \
2f5a189c 6351 gen_helper_stve##name(cpu_env, rs, EA); \
cbfb6ae9
AJ
6352 tcg_temp_free(EA); \
6353 tcg_temp_free_ptr(rs); \
6354 }
6355
fe1e5c53 6356GEN_VR_LDX(lvx, 0x07, 0x03);
a9d9eb8f 6357/* As we don't emulate the cache, lvxl is stricly equivalent to lvx */
fe1e5c53 6358GEN_VR_LDX(lvxl, 0x07, 0x0B);
a9d9eb8f 6359
cbfb6ae9
AJ
6360GEN_VR_LVE(bx, 0x07, 0x00);
6361GEN_VR_LVE(hx, 0x07, 0x01);
6362GEN_VR_LVE(wx, 0x07, 0x02);
6363
fe1e5c53 6364GEN_VR_STX(svx, 0x07, 0x07);
a9d9eb8f 6365/* As we don't emulate the cache, stvxl is stricly equivalent to stvx */
fe1e5c53 6366GEN_VR_STX(svxl, 0x07, 0x0F);
a9d9eb8f 6367
cbfb6ae9
AJ
6368GEN_VR_STVE(bx, 0x07, 0x04);
6369GEN_VR_STVE(hx, 0x07, 0x05);
6370GEN_VR_STVE(wx, 0x07, 0x06);
6371
99e300ef 6372static void gen_lvsl(DisasContext *ctx)
bf8d8ded
AJ
6373{
6374 TCGv_ptr rd;
6375 TCGv EA;
6376 if (unlikely(!ctx->altivec_enabled)) {
6377 gen_exception(ctx, POWERPC_EXCP_VPU);
6378 return;
6379 }
6380 EA = tcg_temp_new();
6381 gen_addr_reg_index(ctx, EA);
6382 rd = gen_avr_ptr(rD(ctx->opcode));
6383 gen_helper_lvsl(rd, EA);
6384 tcg_temp_free(EA);
6385 tcg_temp_free_ptr(rd);
6386}
6387
99e300ef 6388static void gen_lvsr(DisasContext *ctx)
bf8d8ded
AJ
6389{
6390 TCGv_ptr rd;
6391 TCGv EA;
6392 if (unlikely(!ctx->altivec_enabled)) {
6393 gen_exception(ctx, POWERPC_EXCP_VPU);
6394 return;
6395 }
6396 EA = tcg_temp_new();
6397 gen_addr_reg_index(ctx, EA);
6398 rd = gen_avr_ptr(rD(ctx->opcode));
6399 gen_helper_lvsr(rd, EA);
6400 tcg_temp_free(EA);
6401 tcg_temp_free_ptr(rd);
6402}
6403
99e300ef 6404static void gen_mfvscr(DisasContext *ctx)
785f451b
AJ
6405{
6406 TCGv_i32 t;
6407 if (unlikely(!ctx->altivec_enabled)) {
6408 gen_exception(ctx, POWERPC_EXCP_VPU);
6409 return;
6410 }
6411 tcg_gen_movi_i64(cpu_avrh[rD(ctx->opcode)], 0);
6412 t = tcg_temp_new_i32();
1328c2bf 6413 tcg_gen_ld_i32(t, cpu_env, offsetof(CPUPPCState, vscr));
785f451b 6414 tcg_gen_extu_i32_i64(cpu_avrl[rD(ctx->opcode)], t);
fce5ecb7 6415 tcg_temp_free_i32(t);
785f451b
AJ
6416}
6417
99e300ef 6418static void gen_mtvscr(DisasContext *ctx)
785f451b 6419{
6e87b7c7 6420 TCGv_ptr p;
785f451b
AJ
6421 if (unlikely(!ctx->altivec_enabled)) {
6422 gen_exception(ctx, POWERPC_EXCP_VPU);
6423 return;
6424 }
6e87b7c7 6425 p = gen_avr_ptr(rD(ctx->opcode));
d15f74fb 6426 gen_helper_mtvscr(cpu_env, p);
6e87b7c7 6427 tcg_temp_free_ptr(p);
785f451b
AJ
6428}
6429
7a9b96cf
AJ
6430/* Logical operations */
6431#define GEN_VX_LOGICAL(name, tcg_op, opc2, opc3) \
99e300ef 6432static void glue(gen_, name)(DisasContext *ctx) \
7a9b96cf
AJ
6433{ \
6434 if (unlikely(!ctx->altivec_enabled)) { \
6435 gen_exception(ctx, POWERPC_EXCP_VPU); \
6436 return; \
6437 } \
6438 tcg_op(cpu_avrh[rD(ctx->opcode)], cpu_avrh[rA(ctx->opcode)], cpu_avrh[rB(ctx->opcode)]); \
6439 tcg_op(cpu_avrl[rD(ctx->opcode)], cpu_avrl[rA(ctx->opcode)], cpu_avrl[rB(ctx->opcode)]); \
6440}
6441
6442GEN_VX_LOGICAL(vand, tcg_gen_and_i64, 2, 16);
6443GEN_VX_LOGICAL(vandc, tcg_gen_andc_i64, 2, 17);
6444GEN_VX_LOGICAL(vor, tcg_gen_or_i64, 2, 18);
6445GEN_VX_LOGICAL(vxor, tcg_gen_xor_i64, 2, 19);
6446GEN_VX_LOGICAL(vnor, tcg_gen_nor_i64, 2, 20);
6447
8e27dd6f 6448#define GEN_VXFORM(name, opc2, opc3) \
99e300ef 6449static void glue(gen_, name)(DisasContext *ctx) \
8e27dd6f
AJ
6450{ \
6451 TCGv_ptr ra, rb, rd; \
6452 if (unlikely(!ctx->altivec_enabled)) { \
6453 gen_exception(ctx, POWERPC_EXCP_VPU); \
6454 return; \
6455 } \
6456 ra = gen_avr_ptr(rA(ctx->opcode)); \
6457 rb = gen_avr_ptr(rB(ctx->opcode)); \
6458 rd = gen_avr_ptr(rD(ctx->opcode)); \
6459 gen_helper_##name (rd, ra, rb); \
6460 tcg_temp_free_ptr(ra); \
6461 tcg_temp_free_ptr(rb); \
6462 tcg_temp_free_ptr(rd); \
6463}
6464
d15f74fb
BS
6465#define GEN_VXFORM_ENV(name, opc2, opc3) \
6466static void glue(gen_, name)(DisasContext *ctx) \
6467{ \
6468 TCGv_ptr ra, rb, rd; \
6469 if (unlikely(!ctx->altivec_enabled)) { \
6470 gen_exception(ctx, POWERPC_EXCP_VPU); \
6471 return; \
6472 } \
6473 ra = gen_avr_ptr(rA(ctx->opcode)); \
6474 rb = gen_avr_ptr(rB(ctx->opcode)); \
6475 rd = gen_avr_ptr(rD(ctx->opcode)); \
54cddd21 6476 gen_helper_##name(cpu_env, rd, ra, rb); \
d15f74fb
BS
6477 tcg_temp_free_ptr(ra); \
6478 tcg_temp_free_ptr(rb); \
6479 tcg_temp_free_ptr(rd); \
6480}
6481
7872c51c
AJ
6482GEN_VXFORM(vaddubm, 0, 0);
6483GEN_VXFORM(vadduhm, 0, 1);
6484GEN_VXFORM(vadduwm, 0, 2);
6485GEN_VXFORM(vsububm, 0, 16);
6486GEN_VXFORM(vsubuhm, 0, 17);
6487GEN_VXFORM(vsubuwm, 0, 18);
e4039339
AJ
6488GEN_VXFORM(vmaxub, 1, 0);
6489GEN_VXFORM(vmaxuh, 1, 1);
6490GEN_VXFORM(vmaxuw, 1, 2);
6491GEN_VXFORM(vmaxsb, 1, 4);
6492GEN_VXFORM(vmaxsh, 1, 5);
6493GEN_VXFORM(vmaxsw, 1, 6);
6494GEN_VXFORM(vminub, 1, 8);
6495GEN_VXFORM(vminuh, 1, 9);
6496GEN_VXFORM(vminuw, 1, 10);
6497GEN_VXFORM(vminsb, 1, 12);
6498GEN_VXFORM(vminsh, 1, 13);
6499GEN_VXFORM(vminsw, 1, 14);
fab3cbe9
AJ
6500GEN_VXFORM(vavgub, 1, 16);
6501GEN_VXFORM(vavguh, 1, 17);
6502GEN_VXFORM(vavguw, 1, 18);
6503GEN_VXFORM(vavgsb, 1, 20);
6504GEN_VXFORM(vavgsh, 1, 21);
6505GEN_VXFORM(vavgsw, 1, 22);
3b430048
AJ
6506GEN_VXFORM(vmrghb, 6, 0);
6507GEN_VXFORM(vmrghh, 6, 1);
6508GEN_VXFORM(vmrghw, 6, 2);
6509GEN_VXFORM(vmrglb, 6, 4);
6510GEN_VXFORM(vmrglh, 6, 5);
6511GEN_VXFORM(vmrglw, 6, 6);
2c277908
AJ
6512GEN_VXFORM(vmuloub, 4, 0);
6513GEN_VXFORM(vmulouh, 4, 1);
6514GEN_VXFORM(vmulosb, 4, 4);
6515GEN_VXFORM(vmulosh, 4, 5);
6516GEN_VXFORM(vmuleub, 4, 8);
6517GEN_VXFORM(vmuleuh, 4, 9);
6518GEN_VXFORM(vmulesb, 4, 12);
6519GEN_VXFORM(vmulesh, 4, 13);
d79f0809
AJ
6520GEN_VXFORM(vslb, 2, 4);
6521GEN_VXFORM(vslh, 2, 5);
6522GEN_VXFORM(vslw, 2, 6);
07ef34c3
AJ
6523GEN_VXFORM(vsrb, 2, 8);
6524GEN_VXFORM(vsrh, 2, 9);
6525GEN_VXFORM(vsrw, 2, 10);
6526GEN_VXFORM(vsrab, 2, 12);
6527GEN_VXFORM(vsrah, 2, 13);
6528GEN_VXFORM(vsraw, 2, 14);
7b239bec
AJ
6529GEN_VXFORM(vslo, 6, 16);
6530GEN_VXFORM(vsro, 6, 17);
e343da72
AJ
6531GEN_VXFORM(vaddcuw, 0, 6);
6532GEN_VXFORM(vsubcuw, 0, 22);
d15f74fb
BS
6533GEN_VXFORM_ENV(vaddubs, 0, 8);
6534GEN_VXFORM_ENV(vadduhs, 0, 9);
6535GEN_VXFORM_ENV(vadduws, 0, 10);
6536GEN_VXFORM_ENV(vaddsbs, 0, 12);
6537GEN_VXFORM_ENV(vaddshs, 0, 13);
6538GEN_VXFORM_ENV(vaddsws, 0, 14);
6539GEN_VXFORM_ENV(vsububs, 0, 24);
6540GEN_VXFORM_ENV(vsubuhs, 0, 25);
6541GEN_VXFORM_ENV(vsubuws, 0, 26);
6542GEN_VXFORM_ENV(vsubsbs, 0, 28);
6543GEN_VXFORM_ENV(vsubshs, 0, 29);
6544GEN_VXFORM_ENV(vsubsws, 0, 30);
5e1d0985
AJ
6545GEN_VXFORM(vrlb, 2, 0);
6546GEN_VXFORM(vrlh, 2, 1);
6547GEN_VXFORM(vrlw, 2, 2);
d9430add
AJ
6548GEN_VXFORM(vsl, 2, 7);
6549GEN_VXFORM(vsr, 2, 11);
d15f74fb
BS
6550GEN_VXFORM_ENV(vpkuhum, 7, 0);
6551GEN_VXFORM_ENV(vpkuwum, 7, 1);
6552GEN_VXFORM_ENV(vpkuhus, 7, 2);
6553GEN_VXFORM_ENV(vpkuwus, 7, 3);
6554GEN_VXFORM_ENV(vpkshus, 7, 4);
6555GEN_VXFORM_ENV(vpkswus, 7, 5);
6556GEN_VXFORM_ENV(vpkshss, 7, 6);
6557GEN_VXFORM_ENV(vpkswss, 7, 7);
1dd9ffb9 6558GEN_VXFORM(vpkpx, 7, 12);
d15f74fb
BS
6559GEN_VXFORM_ENV(vsum4ubs, 4, 24);
6560GEN_VXFORM_ENV(vsum4sbs, 4, 28);
6561GEN_VXFORM_ENV(vsum4shs, 4, 25);
6562GEN_VXFORM_ENV(vsum2sws, 4, 26);
6563GEN_VXFORM_ENV(vsumsws, 4, 30);
6564GEN_VXFORM_ENV(vaddfp, 5, 0);
6565GEN_VXFORM_ENV(vsubfp, 5, 1);
6566GEN_VXFORM_ENV(vmaxfp, 5, 16);
6567GEN_VXFORM_ENV(vminfp, 5, 17);
fab3cbe9 6568
0cbcd906 6569#define GEN_VXRFORM1(opname, name, str, opc2, opc3) \
e8eaa2c0 6570static void glue(gen_, name)(DisasContext *ctx) \
0cbcd906
AJ
6571 { \
6572 TCGv_ptr ra, rb, rd; \
6573 if (unlikely(!ctx->altivec_enabled)) { \
6574 gen_exception(ctx, POWERPC_EXCP_VPU); \
6575 return; \
6576 } \
6577 ra = gen_avr_ptr(rA(ctx->opcode)); \
6578 rb = gen_avr_ptr(rB(ctx->opcode)); \
6579 rd = gen_avr_ptr(rD(ctx->opcode)); \
d15f74fb 6580 gen_helper_##opname(cpu_env, rd, ra, rb); \
0cbcd906
AJ
6581 tcg_temp_free_ptr(ra); \
6582 tcg_temp_free_ptr(rb); \
6583 tcg_temp_free_ptr(rd); \
6584 }
6585
6586#define GEN_VXRFORM(name, opc2, opc3) \
6587 GEN_VXRFORM1(name, name, #name, opc2, opc3) \
6588 GEN_VXRFORM1(name##_dot, name##_, #name ".", opc2, (opc3 | (0x1 << 4)))
6589
1add6e23
AJ
6590GEN_VXRFORM(vcmpequb, 3, 0)
6591GEN_VXRFORM(vcmpequh, 3, 1)
6592GEN_VXRFORM(vcmpequw, 3, 2)
6593GEN_VXRFORM(vcmpgtsb, 3, 12)
6594GEN_VXRFORM(vcmpgtsh, 3, 13)
6595GEN_VXRFORM(vcmpgtsw, 3, 14)
6596GEN_VXRFORM(vcmpgtub, 3, 8)
6597GEN_VXRFORM(vcmpgtuh, 3, 9)
6598GEN_VXRFORM(vcmpgtuw, 3, 10)
819ca121
AJ
6599GEN_VXRFORM(vcmpeqfp, 3, 3)
6600GEN_VXRFORM(vcmpgefp, 3, 7)
6601GEN_VXRFORM(vcmpgtfp, 3, 11)
6602GEN_VXRFORM(vcmpbfp, 3, 15)
1add6e23 6603
c026766b 6604#define GEN_VXFORM_SIMM(name, opc2, opc3) \
99e300ef 6605static void glue(gen_, name)(DisasContext *ctx) \
c026766b
AJ
6606 { \
6607 TCGv_ptr rd; \
6608 TCGv_i32 simm; \
6609 if (unlikely(!ctx->altivec_enabled)) { \
6610 gen_exception(ctx, POWERPC_EXCP_VPU); \
6611 return; \
6612 } \
6613 simm = tcg_const_i32(SIMM5(ctx->opcode)); \
6614 rd = gen_avr_ptr(rD(ctx->opcode)); \
6615 gen_helper_##name (rd, simm); \
6616 tcg_temp_free_i32(simm); \
6617 tcg_temp_free_ptr(rd); \
6618 }
6619
6620GEN_VXFORM_SIMM(vspltisb, 6, 12);
6621GEN_VXFORM_SIMM(vspltish, 6, 13);
6622GEN_VXFORM_SIMM(vspltisw, 6, 14);
6623
de5f2484 6624#define GEN_VXFORM_NOA(name, opc2, opc3) \
99e300ef 6625static void glue(gen_, name)(DisasContext *ctx) \
de5f2484
AJ
6626 { \
6627 TCGv_ptr rb, rd; \
6628 if (unlikely(!ctx->altivec_enabled)) { \
6629 gen_exception(ctx, POWERPC_EXCP_VPU); \
6630 return; \
6631 } \
6632 rb = gen_avr_ptr(rB(ctx->opcode)); \
6633 rd = gen_avr_ptr(rD(ctx->opcode)); \
6634 gen_helper_##name (rd, rb); \
6635 tcg_temp_free_ptr(rb); \
6636 tcg_temp_free_ptr(rd); \
6637 }
6638
d15f74fb
BS
6639#define GEN_VXFORM_NOA_ENV(name, opc2, opc3) \
6640static void glue(gen_, name)(DisasContext *ctx) \
6641 { \
6642 TCGv_ptr rb, rd; \
6643 \
6644 if (unlikely(!ctx->altivec_enabled)) { \
6645 gen_exception(ctx, POWERPC_EXCP_VPU); \
6646 return; \
6647 } \
6648 rb = gen_avr_ptr(rB(ctx->opcode)); \
6649 rd = gen_avr_ptr(rD(ctx->opcode)); \
6650 gen_helper_##name(cpu_env, rd, rb); \
6651 tcg_temp_free_ptr(rb); \
6652 tcg_temp_free_ptr(rd); \
6653 }
6654
6cf1c6e5
AJ
6655GEN_VXFORM_NOA(vupkhsb, 7, 8);
6656GEN_VXFORM_NOA(vupkhsh, 7, 9);
6657GEN_VXFORM_NOA(vupklsb, 7, 10);
6658GEN_VXFORM_NOA(vupklsh, 7, 11);
79f85c3a
AJ
6659GEN_VXFORM_NOA(vupkhpx, 7, 13);
6660GEN_VXFORM_NOA(vupklpx, 7, 15);
d15f74fb
BS
6661GEN_VXFORM_NOA_ENV(vrefp, 5, 4);
6662GEN_VXFORM_NOA_ENV(vrsqrtefp, 5, 5);
6663GEN_VXFORM_NOA_ENV(vexptefp, 5, 6);
6664GEN_VXFORM_NOA_ENV(vlogefp, 5, 7);
6665GEN_VXFORM_NOA_ENV(vrfim, 5, 8);
6666GEN_VXFORM_NOA_ENV(vrfin, 5, 9);
6667GEN_VXFORM_NOA_ENV(vrfip, 5, 10);
6668GEN_VXFORM_NOA_ENV(vrfiz, 5, 11);
79f85c3a 6669
21d21583 6670#define GEN_VXFORM_SIMM(name, opc2, opc3) \
99e300ef 6671static void glue(gen_, name)(DisasContext *ctx) \
21d21583
AJ
6672 { \
6673 TCGv_ptr rd; \
6674 TCGv_i32 simm; \
6675 if (unlikely(!ctx->altivec_enabled)) { \
6676 gen_exception(ctx, POWERPC_EXCP_VPU); \
6677 return; \
6678 } \
6679 simm = tcg_const_i32(SIMM5(ctx->opcode)); \
6680 rd = gen_avr_ptr(rD(ctx->opcode)); \
6681 gen_helper_##name (rd, simm); \
6682 tcg_temp_free_i32(simm); \
6683 tcg_temp_free_ptr(rd); \
6684 }
6685
27a4edb3 6686#define GEN_VXFORM_UIMM(name, opc2, opc3) \
99e300ef 6687static void glue(gen_, name)(DisasContext *ctx) \
27a4edb3
AJ
6688 { \
6689 TCGv_ptr rb, rd; \
6690 TCGv_i32 uimm; \
6691 if (unlikely(!ctx->altivec_enabled)) { \
6692 gen_exception(ctx, POWERPC_EXCP_VPU); \
6693 return; \
6694 } \
6695 uimm = tcg_const_i32(UIMM5(ctx->opcode)); \
6696 rb = gen_avr_ptr(rB(ctx->opcode)); \
6697 rd = gen_avr_ptr(rD(ctx->opcode)); \
6698 gen_helper_##name (rd, rb, uimm); \
6699 tcg_temp_free_i32(uimm); \
6700 tcg_temp_free_ptr(rb); \
6701 tcg_temp_free_ptr(rd); \
6702 }
6703
d15f74fb
BS
6704#define GEN_VXFORM_UIMM_ENV(name, opc2, opc3) \
6705static void glue(gen_, name)(DisasContext *ctx) \
6706 { \
6707 TCGv_ptr rb, rd; \
6708 TCGv_i32 uimm; \
6709 \
6710 if (unlikely(!ctx->altivec_enabled)) { \
6711 gen_exception(ctx, POWERPC_EXCP_VPU); \
6712 return; \
6713 } \
6714 uimm = tcg_const_i32(UIMM5(ctx->opcode)); \
6715 rb = gen_avr_ptr(rB(ctx->opcode)); \
6716 rd = gen_avr_ptr(rD(ctx->opcode)); \
6717 gen_helper_##name(cpu_env, rd, rb, uimm); \
6718 tcg_temp_free_i32(uimm); \
6719 tcg_temp_free_ptr(rb); \
6720 tcg_temp_free_ptr(rd); \
6721 }
6722
e4e6bee7
AJ
6723GEN_VXFORM_UIMM(vspltb, 6, 8);
6724GEN_VXFORM_UIMM(vsplth, 6, 9);
6725GEN_VXFORM_UIMM(vspltw, 6, 10);
d15f74fb
BS
6726GEN_VXFORM_UIMM_ENV(vcfux, 5, 12);
6727GEN_VXFORM_UIMM_ENV(vcfsx, 5, 13);
6728GEN_VXFORM_UIMM_ENV(vctuxs, 5, 14);
6729GEN_VXFORM_UIMM_ENV(vctsxs, 5, 15);
e4e6bee7 6730
99e300ef 6731static void gen_vsldoi(DisasContext *ctx)
cd633b10
AJ
6732{
6733 TCGv_ptr ra, rb, rd;
fce5ecb7 6734 TCGv_i32 sh;
cd633b10
AJ
6735 if (unlikely(!ctx->altivec_enabled)) {
6736 gen_exception(ctx, POWERPC_EXCP_VPU);
6737 return;
6738 }
6739 ra = gen_avr_ptr(rA(ctx->opcode));
6740 rb = gen_avr_ptr(rB(ctx->opcode));
6741 rd = gen_avr_ptr(rD(ctx->opcode));
6742 sh = tcg_const_i32(VSH(ctx->opcode));
6743 gen_helper_vsldoi (rd, ra, rb, sh);
6744 tcg_temp_free_ptr(ra);
6745 tcg_temp_free_ptr(rb);
6746 tcg_temp_free_ptr(rd);
fce5ecb7 6747 tcg_temp_free_i32(sh);
cd633b10
AJ
6748}
6749
707cec33 6750#define GEN_VAFORM_PAIRED(name0, name1, opc2) \
d15f74fb 6751static void glue(gen_, name0##_##name1)(DisasContext *ctx) \
707cec33
AJ
6752 { \
6753 TCGv_ptr ra, rb, rc, rd; \
6754 if (unlikely(!ctx->altivec_enabled)) { \
6755 gen_exception(ctx, POWERPC_EXCP_VPU); \
6756 return; \
6757 } \
6758 ra = gen_avr_ptr(rA(ctx->opcode)); \
6759 rb = gen_avr_ptr(rB(ctx->opcode)); \
6760 rc = gen_avr_ptr(rC(ctx->opcode)); \
6761 rd = gen_avr_ptr(rD(ctx->opcode)); \
6762 if (Rc(ctx->opcode)) { \
d15f74fb 6763 gen_helper_##name1(cpu_env, rd, ra, rb, rc); \
707cec33 6764 } else { \
d15f74fb 6765 gen_helper_##name0(cpu_env, rd, ra, rb, rc); \
707cec33
AJ
6766 } \
6767 tcg_temp_free_ptr(ra); \
6768 tcg_temp_free_ptr(rb); \
6769 tcg_temp_free_ptr(rc); \
6770 tcg_temp_free_ptr(rd); \
6771 }
6772
b161ae27
AJ
6773GEN_VAFORM_PAIRED(vmhaddshs, vmhraddshs, 16)
6774
99e300ef 6775static void gen_vmladduhm(DisasContext *ctx)
bcd2ee23
AJ
6776{
6777 TCGv_ptr ra, rb, rc, rd;
6778 if (unlikely(!ctx->altivec_enabled)) {
6779 gen_exception(ctx, POWERPC_EXCP_VPU);
6780 return;
6781 }
6782 ra = gen_avr_ptr(rA(ctx->opcode));
6783 rb = gen_avr_ptr(rB(ctx->opcode));
6784 rc = gen_avr_ptr(rC(ctx->opcode));
6785 rd = gen_avr_ptr(rD(ctx->opcode));
6786 gen_helper_vmladduhm(rd, ra, rb, rc);
6787 tcg_temp_free_ptr(ra);
6788 tcg_temp_free_ptr(rb);
6789 tcg_temp_free_ptr(rc);
6790 tcg_temp_free_ptr(rd);
6791}
6792
b04ae981 6793GEN_VAFORM_PAIRED(vmsumubm, vmsummbm, 18)
4d9903b6 6794GEN_VAFORM_PAIRED(vmsumuhm, vmsumuhs, 19)
eae07261 6795GEN_VAFORM_PAIRED(vmsumshm, vmsumshs, 20)
d1258698 6796GEN_VAFORM_PAIRED(vsel, vperm, 21)
35cf7c7e 6797GEN_VAFORM_PAIRED(vmaddfp, vnmsubfp, 23)
b04ae981 6798
0487d6a8 6799/*** SPE extension ***/
0487d6a8 6800/* Register moves */
3cd7d1dd 6801
a0e13900
FC
6802
6803static inline void gen_evmra(DisasContext *ctx)
6804{
6805
6806 if (unlikely(!ctx->spe_enabled)) {
27a69bb0 6807 gen_exception(ctx, POWERPC_EXCP_SPEU);
a0e13900
FC
6808 return;
6809 }
6810
6811#if defined(TARGET_PPC64)
6812 /* rD := rA */
6813 tcg_gen_mov_i64(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)]);
6814
6815 /* spe_acc := rA */
6816 tcg_gen_st_i64(cpu_gpr[rA(ctx->opcode)],
6817 cpu_env,
1328c2bf 6818 offsetof(CPUPPCState, spe_acc));
a0e13900
FC
6819#else
6820 TCGv_i64 tmp = tcg_temp_new_i64();
6821
6822 /* tmp := rA_lo + rA_hi << 32 */
6823 tcg_gen_concat_i32_i64(tmp, cpu_gpr[rA(ctx->opcode)], cpu_gprh[rA(ctx->opcode)]);
6824
6825 /* spe_acc := tmp */
1328c2bf 6826 tcg_gen_st_i64(tmp, cpu_env, offsetof(CPUPPCState, spe_acc));
a0e13900
FC
6827 tcg_temp_free_i64(tmp);
6828
6829 /* rD := rA */
6830 tcg_gen_mov_i32(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)]);
6831 tcg_gen_mov_i32(cpu_gprh[rD(ctx->opcode)], cpu_gprh[rA(ctx->opcode)]);
6832#endif
6833}
6834
636aa200
BS
6835static inline void gen_load_gpr64(TCGv_i64 t, int reg)
6836{
f78fb44e
AJ
6837#if defined(TARGET_PPC64)
6838 tcg_gen_mov_i64(t, cpu_gpr[reg]);
6839#else
36aa55dc 6840 tcg_gen_concat_i32_i64(t, cpu_gpr[reg], cpu_gprh[reg]);
3cd7d1dd 6841#endif
f78fb44e 6842}
3cd7d1dd 6843
636aa200
BS
6844static inline void gen_store_gpr64(int reg, TCGv_i64 t)
6845{
f78fb44e
AJ
6846#if defined(TARGET_PPC64)
6847 tcg_gen_mov_i64(cpu_gpr[reg], t);
6848#else
a7812ae4 6849 TCGv_i64 tmp = tcg_temp_new_i64();
f78fb44e 6850 tcg_gen_trunc_i64_i32(cpu_gpr[reg], t);
f78fb44e
AJ
6851 tcg_gen_shri_i64(tmp, t, 32);
6852 tcg_gen_trunc_i64_i32(cpu_gprh[reg], tmp);
a7812ae4 6853 tcg_temp_free_i64(tmp);
3cd7d1dd 6854#endif
f78fb44e 6855}
3cd7d1dd 6856
70560da7 6857#define GEN_SPE(name0, name1, opc2, opc3, inval0, inval1, type) \
99e300ef 6858static void glue(gen_, name0##_##name1)(DisasContext *ctx) \
0487d6a8
JM
6859{ \
6860 if (Rc(ctx->opcode)) \
6861 gen_##name1(ctx); \
6862 else \
6863 gen_##name0(ctx); \
6864}
6865
6866/* Handler for undefined SPE opcodes */
636aa200 6867static inline void gen_speundef(DisasContext *ctx)
0487d6a8 6868{
e06fcd75 6869 gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL);
0487d6a8
JM
6870}
6871
57951c27
AJ
6872/* SPE logic */
6873#if defined(TARGET_PPC64)
6874#define GEN_SPEOP_LOGIC2(name, tcg_op) \
636aa200 6875static inline void gen_##name(DisasContext *ctx) \
0487d6a8
JM
6876{ \
6877 if (unlikely(!ctx->spe_enabled)) { \
27a69bb0 6878 gen_exception(ctx, POWERPC_EXCP_SPEU); \
0487d6a8
JM
6879 return; \
6880 } \
57951c27
AJ
6881 tcg_op(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)], \
6882 cpu_gpr[rB(ctx->opcode)]); \
6883}
6884#else
6885#define GEN_SPEOP_LOGIC2(name, tcg_op) \
636aa200 6886static inline void gen_##name(DisasContext *ctx) \
57951c27
AJ
6887{ \
6888 if (unlikely(!ctx->spe_enabled)) { \
27a69bb0 6889 gen_exception(ctx, POWERPC_EXCP_SPEU); \
57951c27
AJ
6890 return; \
6891 } \
6892 tcg_op(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)], \
6893 cpu_gpr[rB(ctx->opcode)]); \
6894 tcg_op(cpu_gprh[rD(ctx->opcode)], cpu_gprh[rA(ctx->opcode)], \
6895 cpu_gprh[rB(ctx->opcode)]); \
0487d6a8 6896}
57951c27
AJ
6897#endif
6898
6899GEN_SPEOP_LOGIC2(evand, tcg_gen_and_tl);
6900GEN_SPEOP_LOGIC2(evandc, tcg_gen_andc_tl);
6901GEN_SPEOP_LOGIC2(evxor, tcg_gen_xor_tl);
6902GEN_SPEOP_LOGIC2(evor, tcg_gen_or_tl);
6903GEN_SPEOP_LOGIC2(evnor, tcg_gen_nor_tl);
6904GEN_SPEOP_LOGIC2(eveqv, tcg_gen_eqv_tl);
6905GEN_SPEOP_LOGIC2(evorc, tcg_gen_orc_tl);
6906GEN_SPEOP_LOGIC2(evnand, tcg_gen_nand_tl);
0487d6a8 6907
57951c27
AJ
6908/* SPE logic immediate */
6909#if defined(TARGET_PPC64)
6910#define GEN_SPEOP_TCG_LOGIC_IMM2(name, tcg_opi) \
636aa200 6911static inline void gen_##name(DisasContext *ctx) \
3d3a6a0a
AJ
6912{ \
6913 if (unlikely(!ctx->spe_enabled)) { \
27a69bb0 6914 gen_exception(ctx, POWERPC_EXCP_SPEU); \
3d3a6a0a
AJ
6915 return; \
6916 } \
a7812ae4
PB
6917 TCGv_i32 t0 = tcg_temp_local_new_i32(); \
6918 TCGv_i32 t1 = tcg_temp_local_new_i32(); \
6919 TCGv_i64 t2 = tcg_temp_local_new_i64(); \
57951c27
AJ
6920 tcg_gen_trunc_i64_i32(t0, cpu_gpr[rA(ctx->opcode)]); \
6921 tcg_opi(t0, t0, rB(ctx->opcode)); \
6922 tcg_gen_shri_i64(t2, cpu_gpr[rA(ctx->opcode)], 32); \
6923 tcg_gen_trunc_i64_i32(t1, t2); \
a7812ae4 6924 tcg_temp_free_i64(t2); \
57951c27
AJ
6925 tcg_opi(t1, t1, rB(ctx->opcode)); \
6926 tcg_gen_concat_i32_i64(cpu_gpr[rD(ctx->opcode)], t0, t1); \
a7812ae4
PB
6927 tcg_temp_free_i32(t0); \
6928 tcg_temp_free_i32(t1); \
3d3a6a0a 6929}
57951c27
AJ
6930#else
6931#define GEN_SPEOP_TCG_LOGIC_IMM2(name, tcg_opi) \
636aa200 6932static inline void gen_##name(DisasContext *ctx) \
0487d6a8
JM
6933{ \
6934 if (unlikely(!ctx->spe_enabled)) { \
27a69bb0 6935 gen_exception(ctx, POWERPC_EXCP_SPEU); \
0487d6a8
JM
6936 return; \
6937 } \
57951c27
AJ
6938 tcg_opi(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)], \
6939 rB(ctx->opcode)); \
6940 tcg_opi(cpu_gprh[rD(ctx->opcode)], cpu_gprh[rA(ctx->opcode)], \
6941 rB(ctx->opcode)); \
0487d6a8 6942}
57951c27
AJ
6943#endif
6944GEN_SPEOP_TCG_LOGIC_IMM2(evslwi, tcg_gen_shli_i32);
6945GEN_SPEOP_TCG_LOGIC_IMM2(evsrwiu, tcg_gen_shri_i32);
6946GEN_SPEOP_TCG_LOGIC_IMM2(evsrwis, tcg_gen_sari_i32);
6947GEN_SPEOP_TCG_LOGIC_IMM2(evrlwi, tcg_gen_rotli_i32);
0487d6a8 6948
57951c27
AJ
6949/* SPE arithmetic */
6950#if defined(TARGET_PPC64)
6951#define GEN_SPEOP_ARITH1(name, tcg_op) \
636aa200 6952static inline void gen_##name(DisasContext *ctx) \
0487d6a8
JM
6953{ \
6954 if (unlikely(!ctx->spe_enabled)) { \
27a69bb0 6955 gen_exception(ctx, POWERPC_EXCP_SPEU); \
0487d6a8
JM
6956 return; \
6957 } \
a7812ae4
PB
6958 TCGv_i32 t0 = tcg_temp_local_new_i32(); \
6959 TCGv_i32 t1 = tcg_temp_local_new_i32(); \
6960 TCGv_i64 t2 = tcg_temp_local_new_i64(); \
57951c27
AJ
6961 tcg_gen_trunc_i64_i32(t0, cpu_gpr[rA(ctx->opcode)]); \
6962 tcg_op(t0, t0); \
6963 tcg_gen_shri_i64(t2, cpu_gpr[rA(ctx->opcode)], 32); \
6964 tcg_gen_trunc_i64_i32(t1, t2); \
a7812ae4 6965 tcg_temp_free_i64(t2); \
57951c27
AJ
6966 tcg_op(t1, t1); \
6967 tcg_gen_concat_i32_i64(cpu_gpr[rD(ctx->opcode)], t0, t1); \
a7812ae4
PB
6968 tcg_temp_free_i32(t0); \
6969 tcg_temp_free_i32(t1); \
0487d6a8 6970}
57951c27 6971#else
a7812ae4 6972#define GEN_SPEOP_ARITH1(name, tcg_op) \
636aa200 6973static inline void gen_##name(DisasContext *ctx) \
57951c27
AJ
6974{ \
6975 if (unlikely(!ctx->spe_enabled)) { \
27a69bb0 6976 gen_exception(ctx, POWERPC_EXCP_SPEU); \
57951c27
AJ
6977 return; \
6978 } \
6979 tcg_op(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)]); \
6980 tcg_op(cpu_gprh[rD(ctx->opcode)], cpu_gprh[rA(ctx->opcode)]); \
6981}
6982#endif
0487d6a8 6983
636aa200 6984static inline void gen_op_evabs(TCGv_i32 ret, TCGv_i32 arg1)
57951c27
AJ
6985{
6986 int l1 = gen_new_label();
6987 int l2 = gen_new_label();
0487d6a8 6988
57951c27
AJ
6989 tcg_gen_brcondi_i32(TCG_COND_GE, arg1, 0, l1);
6990 tcg_gen_neg_i32(ret, arg1);
6991 tcg_gen_br(l2);
6992 gen_set_label(l1);
a7812ae4 6993 tcg_gen_mov_i32(ret, arg1);
57951c27
AJ
6994 gen_set_label(l2);
6995}
6996GEN_SPEOP_ARITH1(evabs, gen_op_evabs);
6997GEN_SPEOP_ARITH1(evneg, tcg_gen_neg_i32);
6998GEN_SPEOP_ARITH1(evextsb, tcg_gen_ext8s_i32);
6999GEN_SPEOP_ARITH1(evextsh, tcg_gen_ext16s_i32);
636aa200 7000static inline void gen_op_evrndw(TCGv_i32 ret, TCGv_i32 arg1)
0487d6a8 7001{
57951c27
AJ
7002 tcg_gen_addi_i32(ret, arg1, 0x8000);
7003 tcg_gen_ext16u_i32(ret, ret);
7004}
7005GEN_SPEOP_ARITH1(evrndw, gen_op_evrndw);
a7812ae4
PB
7006GEN_SPEOP_ARITH1(evcntlsw, gen_helper_cntlsw32);
7007GEN_SPEOP_ARITH1(evcntlzw, gen_helper_cntlzw32);
0487d6a8 7008
57951c27
AJ
7009#if defined(TARGET_PPC64)
7010#define GEN_SPEOP_ARITH2(name, tcg_op) \
636aa200 7011static inline void gen_##name(DisasContext *ctx) \
0487d6a8
JM
7012{ \
7013 if (unlikely(!ctx->spe_enabled)) { \
27a69bb0 7014 gen_exception(ctx, POWERPC_EXCP_SPEU); \
0487d6a8
JM
7015 return; \
7016 } \
a7812ae4
PB
7017 TCGv_i32 t0 = tcg_temp_local_new_i32(); \
7018 TCGv_i32 t1 = tcg_temp_local_new_i32(); \
7019 TCGv_i32 t2 = tcg_temp_local_new_i32(); \
501e23c4 7020 TCGv_i64 t3 = tcg_temp_local_new_i64(); \
57951c27
AJ
7021 tcg_gen_trunc_i64_i32(t0, cpu_gpr[rA(ctx->opcode)]); \
7022 tcg_gen_trunc_i64_i32(t2, cpu_gpr[rB(ctx->opcode)]); \
7023 tcg_op(t0, t0, t2); \
7024 tcg_gen_shri_i64(t3, cpu_gpr[rA(ctx->opcode)], 32); \
7025 tcg_gen_trunc_i64_i32(t1, t3); \
7026 tcg_gen_shri_i64(t3, cpu_gpr[rB(ctx->opcode)], 32); \
7027 tcg_gen_trunc_i64_i32(t2, t3); \
a7812ae4 7028 tcg_temp_free_i64(t3); \
57951c27 7029 tcg_op(t1, t1, t2); \
a7812ae4 7030 tcg_temp_free_i32(t2); \
57951c27 7031 tcg_gen_concat_i32_i64(cpu_gpr[rD(ctx->opcode)], t0, t1); \
a7812ae4
PB
7032 tcg_temp_free_i32(t0); \
7033 tcg_temp_free_i32(t1); \
0487d6a8 7034}
57951c27
AJ
7035#else
7036#define GEN_SPEOP_ARITH2(name, tcg_op) \
636aa200 7037static inline void gen_##name(DisasContext *ctx) \
0487d6a8
JM
7038{ \
7039 if (unlikely(!ctx->spe_enabled)) { \
27a69bb0 7040 gen_exception(ctx, POWERPC_EXCP_SPEU); \
0487d6a8
JM
7041 return; \
7042 } \
57951c27
AJ
7043 tcg_op(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)], \
7044 cpu_gpr[rB(ctx->opcode)]); \
7045 tcg_op(cpu_gprh[rD(ctx->opcode)], cpu_gprh[rA(ctx->opcode)], \
7046 cpu_gprh[rB(ctx->opcode)]); \
0487d6a8 7047}
57951c27 7048#endif
0487d6a8 7049
636aa200 7050static inline void gen_op_evsrwu(TCGv_i32 ret, TCGv_i32 arg1, TCGv_i32 arg2)
57951c27 7051{
a7812ae4 7052 TCGv_i32 t0;
57951c27 7053 int l1, l2;
0487d6a8 7054
57951c27
AJ
7055 l1 = gen_new_label();
7056 l2 = gen_new_label();
a7812ae4 7057 t0 = tcg_temp_local_new_i32();
57951c27
AJ
7058 /* No error here: 6 bits are used */
7059 tcg_gen_andi_i32(t0, arg2, 0x3F);
7060 tcg_gen_brcondi_i32(TCG_COND_GE, t0, 32, l1);
7061 tcg_gen_shr_i32(ret, arg1, t0);
7062 tcg_gen_br(l2);
7063 gen_set_label(l1);
7064 tcg_gen_movi_i32(ret, 0);
0aef4261 7065 gen_set_label(l2);
a7812ae4 7066 tcg_temp_free_i32(t0);
57951c27
AJ
7067}
7068GEN_SPEOP_ARITH2(evsrwu, gen_op_evsrwu);
636aa200 7069static inline void gen_op_evsrws(TCGv_i32 ret, TCGv_i32 arg1, TCGv_i32 arg2)
57951c27 7070{
a7812ae4 7071 TCGv_i32 t0;
57951c27
AJ
7072 int l1, l2;
7073
7074 l1 = gen_new_label();
7075 l2 = gen_new_label();
a7812ae4 7076 t0 = tcg_temp_local_new_i32();
57951c27
AJ
7077 /* No error here: 6 bits are used */
7078 tcg_gen_andi_i32(t0, arg2, 0x3F);
7079 tcg_gen_brcondi_i32(TCG_COND_GE, t0, 32, l1);
7080 tcg_gen_sar_i32(ret, arg1, t0);
7081 tcg_gen_br(l2);
7082 gen_set_label(l1);
7083 tcg_gen_movi_i32(ret, 0);
0aef4261 7084 gen_set_label(l2);
a7812ae4 7085 tcg_temp_free_i32(t0);
57951c27
AJ
7086}
7087GEN_SPEOP_ARITH2(evsrws, gen_op_evsrws);
636aa200 7088static inline void gen_op_evslw(TCGv_i32 ret, TCGv_i32 arg1, TCGv_i32 arg2)
57951c27 7089{
a7812ae4 7090 TCGv_i32 t0;
57951c27
AJ
7091 int l1, l2;
7092
7093 l1 = gen_new_label();
7094 l2 = gen_new_label();
a7812ae4 7095 t0 = tcg_temp_local_new_i32();
57951c27
AJ
7096 /* No error here: 6 bits are used */
7097 tcg_gen_andi_i32(t0, arg2, 0x3F);
7098 tcg_gen_brcondi_i32(TCG_COND_GE, t0, 32, l1);
7099 tcg_gen_shl_i32(ret, arg1, t0);
7100 tcg_gen_br(l2);
7101 gen_set_label(l1);
7102 tcg_gen_movi_i32(ret, 0);
e29ef9fa 7103 gen_set_label(l2);
a7812ae4 7104 tcg_temp_free_i32(t0);
57951c27
AJ
7105}
7106GEN_SPEOP_ARITH2(evslw, gen_op_evslw);
636aa200 7107static inline void gen_op_evrlw(TCGv_i32 ret, TCGv_i32 arg1, TCGv_i32 arg2)
57951c27 7108{
a7812ae4 7109 TCGv_i32 t0 = tcg_temp_new_i32();
57951c27
AJ
7110 tcg_gen_andi_i32(t0, arg2, 0x1F);
7111 tcg_gen_rotl_i32(ret, arg1, t0);
a7812ae4 7112 tcg_temp_free_i32(t0);
57951c27
AJ
7113}
7114GEN_SPEOP_ARITH2(evrlw, gen_op_evrlw);
636aa200 7115static inline void gen_evmergehi(DisasContext *ctx)
57951c27
AJ
7116{
7117 if (unlikely(!ctx->spe_enabled)) {
27a69bb0 7118 gen_exception(ctx, POWERPC_EXCP_SPEU);
57951c27
AJ
7119 return;
7120 }
7121#if defined(TARGET_PPC64)
a7812ae4
PB
7122 TCGv t0 = tcg_temp_new();
7123 TCGv t1 = tcg_temp_new();
57951c27
AJ
7124 tcg_gen_shri_tl(t0, cpu_gpr[rB(ctx->opcode)], 32);
7125 tcg_gen_andi_tl(t1, cpu_gpr[rA(ctx->opcode)], 0xFFFFFFFF0000000ULL);
7126 tcg_gen_or_tl(cpu_gpr[rD(ctx->opcode)], t0, t1);
7127 tcg_temp_free(t0);
7128 tcg_temp_free(t1);
7129#else
7130 tcg_gen_mov_i32(cpu_gpr[rD(ctx->opcode)], cpu_gprh[rB(ctx->opcode)]);
7131 tcg_gen_mov_i32(cpu_gprh[rD(ctx->opcode)], cpu_gprh[rA(ctx->opcode)]);
7132#endif
7133}
7134GEN_SPEOP_ARITH2(evaddw, tcg_gen_add_i32);
636aa200 7135static inline void gen_op_evsubf(TCGv_i32 ret, TCGv_i32 arg1, TCGv_i32 arg2)
0487d6a8 7136{
57951c27
AJ
7137 tcg_gen_sub_i32(ret, arg2, arg1);
7138}
7139GEN_SPEOP_ARITH2(evsubfw, gen_op_evsubf);
0487d6a8 7140
57951c27
AJ
7141/* SPE arithmetic immediate */
7142#if defined(TARGET_PPC64)
7143#define GEN_SPEOP_ARITH_IMM2(name, tcg_op) \
636aa200 7144static inline void gen_##name(DisasContext *ctx) \
57951c27
AJ
7145{ \
7146 if (unlikely(!ctx->spe_enabled)) { \
27a69bb0 7147 gen_exception(ctx, POWERPC_EXCP_SPEU); \
57951c27
AJ
7148 return; \
7149 } \
a7812ae4
PB
7150 TCGv_i32 t0 = tcg_temp_local_new_i32(); \
7151 TCGv_i32 t1 = tcg_temp_local_new_i32(); \
7152 TCGv_i64 t2 = tcg_temp_local_new_i64(); \
57951c27
AJ
7153 tcg_gen_trunc_i64_i32(t0, cpu_gpr[rB(ctx->opcode)]); \
7154 tcg_op(t0, t0, rA(ctx->opcode)); \
7155 tcg_gen_shri_i64(t2, cpu_gpr[rB(ctx->opcode)], 32); \
7156 tcg_gen_trunc_i64_i32(t1, t2); \
e06fcd75 7157 tcg_temp_free_i64(t2); \
57951c27
AJ
7158 tcg_op(t1, t1, rA(ctx->opcode)); \
7159 tcg_gen_concat_i32_i64(cpu_gpr[rD(ctx->opcode)], t0, t1); \
a7812ae4
PB
7160 tcg_temp_free_i32(t0); \
7161 tcg_temp_free_i32(t1); \
57951c27
AJ
7162}
7163#else
7164#define GEN_SPEOP_ARITH_IMM2(name, tcg_op) \
636aa200 7165static inline void gen_##name(DisasContext *ctx) \
57951c27
AJ
7166{ \
7167 if (unlikely(!ctx->spe_enabled)) { \
27a69bb0 7168 gen_exception(ctx, POWERPC_EXCP_SPEU); \
57951c27
AJ
7169 return; \
7170 } \
7171 tcg_op(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rB(ctx->opcode)], \
7172 rA(ctx->opcode)); \
7173 tcg_op(cpu_gprh[rD(ctx->opcode)], cpu_gprh[rB(ctx->opcode)], \
7174 rA(ctx->opcode)); \
7175}
7176#endif
7177GEN_SPEOP_ARITH_IMM2(evaddiw, tcg_gen_addi_i32);
7178GEN_SPEOP_ARITH_IMM2(evsubifw, tcg_gen_subi_i32);
7179
7180/* SPE comparison */
7181#if defined(TARGET_PPC64)
7182#define GEN_SPEOP_COMP(name, tcg_cond) \
636aa200 7183static inline void gen_##name(DisasContext *ctx) \
57951c27
AJ
7184{ \
7185 if (unlikely(!ctx->spe_enabled)) { \
27a69bb0 7186 gen_exception(ctx, POWERPC_EXCP_SPEU); \
57951c27
AJ
7187 return; \
7188 } \
7189 int l1 = gen_new_label(); \
7190 int l2 = gen_new_label(); \
7191 int l3 = gen_new_label(); \
7192 int l4 = gen_new_label(); \
a7812ae4
PB
7193 TCGv_i32 t0 = tcg_temp_local_new_i32(); \
7194 TCGv_i32 t1 = tcg_temp_local_new_i32(); \
7195 TCGv_i64 t2 = tcg_temp_local_new_i64(); \
57951c27
AJ
7196 tcg_gen_trunc_i64_i32(t0, cpu_gpr[rA(ctx->opcode)]); \
7197 tcg_gen_trunc_i64_i32(t1, cpu_gpr[rB(ctx->opcode)]); \
7198 tcg_gen_brcond_i32(tcg_cond, t0, t1, l1); \
a7812ae4 7199 tcg_gen_movi_i32(cpu_crf[crfD(ctx->opcode)], 0); \
57951c27
AJ
7200 tcg_gen_br(l2); \
7201 gen_set_label(l1); \
7202 tcg_gen_movi_i32(cpu_crf[crfD(ctx->opcode)], \
7203 CRF_CL | CRF_CH_OR_CL | CRF_CH_AND_CL); \
7204 gen_set_label(l2); \
7205 tcg_gen_shri_i64(t2, cpu_gpr[rA(ctx->opcode)], 32); \
7206 tcg_gen_trunc_i64_i32(t0, t2); \
7207 tcg_gen_shri_i64(t2, cpu_gpr[rB(ctx->opcode)], 32); \
7208 tcg_gen_trunc_i64_i32(t1, t2); \
a7812ae4 7209 tcg_temp_free_i64(t2); \
57951c27
AJ
7210 tcg_gen_brcond_i32(tcg_cond, t0, t1, l3); \
7211 tcg_gen_andi_i32(cpu_crf[crfD(ctx->opcode)], cpu_crf[crfD(ctx->opcode)], \
7212 ~(CRF_CH | CRF_CH_AND_CL)); \
7213 tcg_gen_br(l4); \
7214 gen_set_label(l3); \
7215 tcg_gen_ori_i32(cpu_crf[crfD(ctx->opcode)], cpu_crf[crfD(ctx->opcode)], \
7216 CRF_CH | CRF_CH_OR_CL); \
7217 gen_set_label(l4); \
a7812ae4
PB
7218 tcg_temp_free_i32(t0); \
7219 tcg_temp_free_i32(t1); \
57951c27
AJ
7220}
7221#else
7222#define GEN_SPEOP_COMP(name, tcg_cond) \
636aa200 7223static inline void gen_##name(DisasContext *ctx) \
57951c27
AJ
7224{ \
7225 if (unlikely(!ctx->spe_enabled)) { \
27a69bb0 7226 gen_exception(ctx, POWERPC_EXCP_SPEU); \
57951c27
AJ
7227 return; \
7228 } \
7229 int l1 = gen_new_label(); \
7230 int l2 = gen_new_label(); \
7231 int l3 = gen_new_label(); \
7232 int l4 = gen_new_label(); \
7233 \
7234 tcg_gen_brcond_i32(tcg_cond, cpu_gpr[rA(ctx->opcode)], \
7235 cpu_gpr[rB(ctx->opcode)], l1); \
7236 tcg_gen_movi_tl(cpu_crf[crfD(ctx->opcode)], 0); \
7237 tcg_gen_br(l2); \
7238 gen_set_label(l1); \
7239 tcg_gen_movi_i32(cpu_crf[crfD(ctx->opcode)], \
7240 CRF_CL | CRF_CH_OR_CL | CRF_CH_AND_CL); \
7241 gen_set_label(l2); \
7242 tcg_gen_brcond_i32(tcg_cond, cpu_gprh[rA(ctx->opcode)], \
7243 cpu_gprh[rB(ctx->opcode)], l3); \
7244 tcg_gen_andi_i32(cpu_crf[crfD(ctx->opcode)], cpu_crf[crfD(ctx->opcode)], \
7245 ~(CRF_CH | CRF_CH_AND_CL)); \
7246 tcg_gen_br(l4); \
7247 gen_set_label(l3); \
7248 tcg_gen_ori_i32(cpu_crf[crfD(ctx->opcode)], cpu_crf[crfD(ctx->opcode)], \
7249 CRF_CH | CRF_CH_OR_CL); \
7250 gen_set_label(l4); \
7251}
7252#endif
7253GEN_SPEOP_COMP(evcmpgtu, TCG_COND_GTU);
7254GEN_SPEOP_COMP(evcmpgts, TCG_COND_GT);
7255GEN_SPEOP_COMP(evcmpltu, TCG_COND_LTU);
7256GEN_SPEOP_COMP(evcmplts, TCG_COND_LT);
7257GEN_SPEOP_COMP(evcmpeq, TCG_COND_EQ);
7258
7259/* SPE misc */
636aa200 7260static inline void gen_brinc(DisasContext *ctx)
57951c27
AJ
7261{
7262 /* Note: brinc is usable even if SPE is disabled */
a7812ae4
PB
7263 gen_helper_brinc(cpu_gpr[rD(ctx->opcode)],
7264 cpu_gpr[rA(ctx->opcode)], cpu_gpr[rB(ctx->opcode)]);
0487d6a8 7265}
636aa200 7266static inline void gen_evmergelo(DisasContext *ctx)
57951c27
AJ
7267{
7268 if (unlikely(!ctx->spe_enabled)) {
27a69bb0 7269 gen_exception(ctx, POWERPC_EXCP_SPEU);
57951c27
AJ
7270 return;
7271 }
7272#if defined(TARGET_PPC64)
a7812ae4
PB
7273 TCGv t0 = tcg_temp_new();
7274 TCGv t1 = tcg_temp_new();
17d9b3af 7275 tcg_gen_ext32u_tl(t0, cpu_gpr[rB(ctx->opcode)]);
57951c27
AJ
7276 tcg_gen_shli_tl(t1, cpu_gpr[rA(ctx->opcode)], 32);
7277 tcg_gen_or_tl(cpu_gpr[rD(ctx->opcode)], t0, t1);
7278 tcg_temp_free(t0);
7279 tcg_temp_free(t1);
7280#else
57951c27 7281 tcg_gen_mov_i32(cpu_gprh[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)]);
33890b3e 7282 tcg_gen_mov_i32(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rB(ctx->opcode)]);
57951c27
AJ
7283#endif
7284}
636aa200 7285static inline void gen_evmergehilo(DisasContext *ctx)
57951c27
AJ
7286{
7287 if (unlikely(!ctx->spe_enabled)) {
27a69bb0 7288 gen_exception(ctx, POWERPC_EXCP_SPEU);
57951c27
AJ
7289 return;
7290 }
7291#if defined(TARGET_PPC64)
a7812ae4
PB
7292 TCGv t0 = tcg_temp_new();
7293 TCGv t1 = tcg_temp_new();
17d9b3af 7294 tcg_gen_ext32u_tl(t0, cpu_gpr[rB(ctx->opcode)]);
57951c27
AJ
7295 tcg_gen_andi_tl(t1, cpu_gpr[rA(ctx->opcode)], 0xFFFFFFFF0000000ULL);
7296 tcg_gen_or_tl(cpu_gpr[rD(ctx->opcode)], t0, t1);
7297 tcg_temp_free(t0);
7298 tcg_temp_free(t1);
7299#else
7300 tcg_gen_mov_i32(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rB(ctx->opcode)]);
7301 tcg_gen_mov_i32(cpu_gprh[rD(ctx->opcode)], cpu_gprh[rA(ctx->opcode)]);
7302#endif
7303}
636aa200 7304static inline void gen_evmergelohi(DisasContext *ctx)
57951c27
AJ
7305{
7306 if (unlikely(!ctx->spe_enabled)) {
27a69bb0 7307 gen_exception(ctx, POWERPC_EXCP_SPEU);
57951c27
AJ
7308 return;
7309 }
7310#if defined(TARGET_PPC64)
a7812ae4
PB
7311 TCGv t0 = tcg_temp_new();
7312 TCGv t1 = tcg_temp_new();
57951c27
AJ
7313 tcg_gen_shri_tl(t0, cpu_gpr[rB(ctx->opcode)], 32);
7314 tcg_gen_shli_tl(t1, cpu_gpr[rA(ctx->opcode)], 32);
7315 tcg_gen_or_tl(cpu_gpr[rD(ctx->opcode)], t0, t1);
7316 tcg_temp_free(t0);
7317 tcg_temp_free(t1);
7318#else
33890b3e
NF
7319 if (rD(ctx->opcode) == rA(ctx->opcode)) {
7320 TCGv_i32 tmp = tcg_temp_new_i32();
7321 tcg_gen_mov_i32(tmp, cpu_gpr[rA(ctx->opcode)]);
7322 tcg_gen_mov_i32(cpu_gpr[rD(ctx->opcode)], cpu_gprh[rB(ctx->opcode)]);
7323 tcg_gen_mov_i32(cpu_gprh[rD(ctx->opcode)], tmp);
7324 tcg_temp_free_i32(tmp);
7325 } else {
7326 tcg_gen_mov_i32(cpu_gpr[rD(ctx->opcode)], cpu_gprh[rB(ctx->opcode)]);
7327 tcg_gen_mov_i32(cpu_gprh[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)]);
7328 }
57951c27
AJ
7329#endif
7330}
636aa200 7331static inline void gen_evsplati(DisasContext *ctx)
57951c27 7332{
ae01847f 7333 uint64_t imm = ((int32_t)(rA(ctx->opcode) << 27)) >> 27;
0487d6a8 7334
57951c27 7335#if defined(TARGET_PPC64)
38d14952 7336 tcg_gen_movi_tl(cpu_gpr[rD(ctx->opcode)], (imm << 32) | imm);
57951c27
AJ
7337#else
7338 tcg_gen_movi_i32(cpu_gpr[rD(ctx->opcode)], imm);
7339 tcg_gen_movi_i32(cpu_gprh[rD(ctx->opcode)], imm);
7340#endif
7341}
636aa200 7342static inline void gen_evsplatfi(DisasContext *ctx)
0487d6a8 7343{
ae01847f 7344 uint64_t imm = rA(ctx->opcode) << 27;
0487d6a8 7345
57951c27 7346#if defined(TARGET_PPC64)
38d14952 7347 tcg_gen_movi_tl(cpu_gpr[rD(ctx->opcode)], (imm << 32) | imm);
57951c27
AJ
7348#else
7349 tcg_gen_movi_i32(cpu_gpr[rD(ctx->opcode)], imm);
7350 tcg_gen_movi_i32(cpu_gprh[rD(ctx->opcode)], imm);
7351#endif
0487d6a8
JM
7352}
7353
636aa200 7354static inline void gen_evsel(DisasContext *ctx)
57951c27
AJ
7355{
7356 int l1 = gen_new_label();
7357 int l2 = gen_new_label();
7358 int l3 = gen_new_label();
7359 int l4 = gen_new_label();
a7812ae4 7360 TCGv_i32 t0 = tcg_temp_local_new_i32();
57951c27 7361#if defined(TARGET_PPC64)
a7812ae4
PB
7362 TCGv t1 = tcg_temp_local_new();
7363 TCGv t2 = tcg_temp_local_new();
57951c27
AJ
7364#endif
7365 tcg_gen_andi_i32(t0, cpu_crf[ctx->opcode & 0x07], 1 << 3);
7366 tcg_gen_brcondi_i32(TCG_COND_EQ, t0, 0, l1);
7367#if defined(TARGET_PPC64)
7368 tcg_gen_andi_tl(t1, cpu_gpr[rA(ctx->opcode)], 0xFFFFFFFF00000000ULL);
7369#else
7370 tcg_gen_mov_tl(cpu_gprh[rD(ctx->opcode)], cpu_gprh[rA(ctx->opcode)]);
7371#endif
7372 tcg_gen_br(l2);
7373 gen_set_label(l1);
7374#if defined(TARGET_PPC64)
7375 tcg_gen_andi_tl(t1, cpu_gpr[rB(ctx->opcode)], 0xFFFFFFFF00000000ULL);
7376#else
7377 tcg_gen_mov_tl(cpu_gprh[rD(ctx->opcode)], cpu_gprh[rB(ctx->opcode)]);
7378#endif
7379 gen_set_label(l2);
7380 tcg_gen_andi_i32(t0, cpu_crf[ctx->opcode & 0x07], 1 << 2);
7381 tcg_gen_brcondi_i32(TCG_COND_EQ, t0, 0, l3);
7382#if defined(TARGET_PPC64)
17d9b3af 7383 tcg_gen_ext32u_tl(t2, cpu_gpr[rA(ctx->opcode)]);
57951c27
AJ
7384#else
7385 tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)]);
7386#endif
7387 tcg_gen_br(l4);
7388 gen_set_label(l3);
7389#if defined(TARGET_PPC64)
17d9b3af 7390 tcg_gen_ext32u_tl(t2, cpu_gpr[rB(ctx->opcode)]);
57951c27
AJ
7391#else
7392 tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rB(ctx->opcode)]);
7393#endif
7394 gen_set_label(l4);
a7812ae4 7395 tcg_temp_free_i32(t0);
57951c27
AJ
7396#if defined(TARGET_PPC64)
7397 tcg_gen_or_tl(cpu_gpr[rD(ctx->opcode)], t1, t2);
7398 tcg_temp_free(t1);
7399 tcg_temp_free(t2);
7400#endif
7401}
e8eaa2c0
BS
7402
7403static void gen_evsel0(DisasContext *ctx)
57951c27
AJ
7404{
7405 gen_evsel(ctx);
7406}
e8eaa2c0
BS
7407
7408static void gen_evsel1(DisasContext *ctx)
57951c27
AJ
7409{
7410 gen_evsel(ctx);
7411}
e8eaa2c0
BS
7412
7413static void gen_evsel2(DisasContext *ctx)
57951c27
AJ
7414{
7415 gen_evsel(ctx);
7416}
e8eaa2c0
BS
7417
7418static void gen_evsel3(DisasContext *ctx)
57951c27
AJ
7419{
7420 gen_evsel(ctx);
7421}
0487d6a8 7422
a0e13900
FC
7423/* Multiply */
7424
7425static inline void gen_evmwumi(DisasContext *ctx)
7426{
7427 TCGv_i64 t0, t1;
7428
7429 if (unlikely(!ctx->spe_enabled)) {
27a69bb0 7430 gen_exception(ctx, POWERPC_EXCP_SPEU);
a0e13900
FC
7431 return;
7432 }
7433
7434 t0 = tcg_temp_new_i64();
7435 t1 = tcg_temp_new_i64();
7436
7437 /* t0 := rA; t1 := rB */
7438#if defined(TARGET_PPC64)
7439 tcg_gen_ext32u_tl(t0, cpu_gpr[rA(ctx->opcode)]);
7440 tcg_gen_ext32u_tl(t1, cpu_gpr[rB(ctx->opcode)]);
7441#else
7442 tcg_gen_extu_tl_i64(t0, cpu_gpr[rA(ctx->opcode)]);
7443 tcg_gen_extu_tl_i64(t1, cpu_gpr[rB(ctx->opcode)]);
7444#endif
7445
7446 tcg_gen_mul_i64(t0, t0, t1); /* t0 := rA * rB */
7447
7448 gen_store_gpr64(rD(ctx->opcode), t0); /* rD := t0 */
7449
7450 tcg_temp_free_i64(t0);
7451 tcg_temp_free_i64(t1);
7452}
7453
7454static inline void gen_evmwumia(DisasContext *ctx)
7455{
7456 TCGv_i64 tmp;
7457
7458 if (unlikely(!ctx->spe_enabled)) {
27a69bb0 7459 gen_exception(ctx, POWERPC_EXCP_SPEU);
a0e13900
FC
7460 return;
7461 }
7462
7463 gen_evmwumi(ctx); /* rD := rA * rB */
7464
7465 tmp = tcg_temp_new_i64();
7466
7467 /* acc := rD */
7468 gen_load_gpr64(tmp, rD(ctx->opcode));
1328c2bf 7469 tcg_gen_st_i64(tmp, cpu_env, offsetof(CPUPPCState, spe_acc));
a0e13900
FC
7470 tcg_temp_free_i64(tmp);
7471}
7472
7473static inline void gen_evmwumiaa(DisasContext *ctx)
7474{
7475 TCGv_i64 acc;
7476 TCGv_i64 tmp;
7477
7478 if (unlikely(!ctx->spe_enabled)) {
27a69bb0 7479 gen_exception(ctx, POWERPC_EXCP_SPEU);
a0e13900
FC
7480 return;
7481 }
7482
7483 gen_evmwumi(ctx); /* rD := rA * rB */
7484
7485 acc = tcg_temp_new_i64();
7486 tmp = tcg_temp_new_i64();
7487
7488 /* tmp := rD */
7489 gen_load_gpr64(tmp, rD(ctx->opcode));
7490
7491 /* Load acc */
1328c2bf 7492 tcg_gen_ld_i64(acc, cpu_env, offsetof(CPUPPCState, spe_acc));
a0e13900
FC
7493
7494 /* acc := tmp + acc */
7495 tcg_gen_add_i64(acc, acc, tmp);
7496
7497 /* Store acc */
1328c2bf 7498 tcg_gen_st_i64(acc, cpu_env, offsetof(CPUPPCState, spe_acc));
a0e13900
FC
7499
7500 /* rD := acc */
7501 gen_store_gpr64(rD(ctx->opcode), acc);
7502
7503 tcg_temp_free_i64(acc);
7504 tcg_temp_free_i64(tmp);
7505}
7506
7507static inline void gen_evmwsmi(DisasContext *ctx)
7508{
7509 TCGv_i64 t0, t1;
7510
7511 if (unlikely(!ctx->spe_enabled)) {
27a69bb0 7512 gen_exception(ctx, POWERPC_EXCP_SPEU);
a0e13900
FC
7513 return;
7514 }
7515
7516 t0 = tcg_temp_new_i64();
7517 t1 = tcg_temp_new_i64();
7518
7519 /* t0 := rA; t1 := rB */
7520#if defined(TARGET_PPC64)
7521 tcg_gen_ext32s_tl(t0, cpu_gpr[rA(ctx->opcode)]);
7522 tcg_gen_ext32s_tl(t1, cpu_gpr[rB(ctx->opcode)]);
7523#else
7524 tcg_gen_ext_tl_i64(t0, cpu_gpr[rA(ctx->opcode)]);
7525 tcg_gen_ext_tl_i64(t1, cpu_gpr[rB(ctx->opcode)]);
7526#endif
7527
7528 tcg_gen_mul_i64(t0, t0, t1); /* t0 := rA * rB */
7529
7530 gen_store_gpr64(rD(ctx->opcode), t0); /* rD := t0 */
7531
7532 tcg_temp_free_i64(t0);
7533 tcg_temp_free_i64(t1);
7534}
7535
7536static inline void gen_evmwsmia(DisasContext *ctx)
7537{
7538 TCGv_i64 tmp;
7539
7540 gen_evmwsmi(ctx); /* rD := rA * rB */
7541
7542 tmp = tcg_temp_new_i64();
7543
7544 /* acc := rD */
7545 gen_load_gpr64(tmp, rD(ctx->opcode));
1328c2bf 7546 tcg_gen_st_i64(tmp, cpu_env, offsetof(CPUPPCState, spe_acc));
a0e13900
FC
7547
7548 tcg_temp_free_i64(tmp);
7549}
7550
7551static inline void gen_evmwsmiaa(DisasContext *ctx)
7552{
7553 TCGv_i64 acc = tcg_temp_new_i64();
7554 TCGv_i64 tmp = tcg_temp_new_i64();
7555
7556 gen_evmwsmi(ctx); /* rD := rA * rB */
7557
7558 acc = tcg_temp_new_i64();
7559 tmp = tcg_temp_new_i64();
7560
7561 /* tmp := rD */
7562 gen_load_gpr64(tmp, rD(ctx->opcode));
7563
7564 /* Load acc */
1328c2bf 7565 tcg_gen_ld_i64(acc, cpu_env, offsetof(CPUPPCState, spe_acc));
a0e13900
FC
7566
7567 /* acc := tmp + acc */
7568 tcg_gen_add_i64(acc, acc, tmp);
7569
7570 /* Store acc */
1328c2bf 7571 tcg_gen_st_i64(acc, cpu_env, offsetof(CPUPPCState, spe_acc));
a0e13900
FC
7572
7573 /* rD := acc */
7574 gen_store_gpr64(rD(ctx->opcode), acc);
7575
7576 tcg_temp_free_i64(acc);
7577 tcg_temp_free_i64(tmp);
7578}
7579
70560da7
FC
7580GEN_SPE(evaddw, speundef, 0x00, 0x08, 0x00000000, 0xFFFFFFFF, PPC_SPE); ////
7581GEN_SPE(evaddiw, speundef, 0x01, 0x08, 0x00000000, 0xFFFFFFFF, PPC_SPE);
7582GEN_SPE(evsubfw, speundef, 0x02, 0x08, 0x00000000, 0xFFFFFFFF, PPC_SPE); ////
7583GEN_SPE(evsubifw, speundef, 0x03, 0x08, 0x00000000, 0xFFFFFFFF, PPC_SPE);
7584GEN_SPE(evabs, evneg, 0x04, 0x08, 0x0000F800, 0x0000F800, PPC_SPE); ////
7585GEN_SPE(evextsb, evextsh, 0x05, 0x08, 0x0000F800, 0x0000F800, PPC_SPE); ////
7586GEN_SPE(evrndw, evcntlzw, 0x06, 0x08, 0x0000F800, 0x0000F800, PPC_SPE); ////
7587GEN_SPE(evcntlsw, brinc, 0x07, 0x08, 0x0000F800, 0x00000000, PPC_SPE); //
7588GEN_SPE(evmra, speundef, 0x02, 0x13, 0x0000F800, 0xFFFFFFFF, PPC_SPE);
7589GEN_SPE(speundef, evand, 0x08, 0x08, 0xFFFFFFFF, 0x00000000, PPC_SPE); ////
7590GEN_SPE(evandc, speundef, 0x09, 0x08, 0x00000000, 0xFFFFFFFF, PPC_SPE); ////
7591GEN_SPE(evxor, evor, 0x0B, 0x08, 0x00000000, 0x00000000, PPC_SPE); ////
7592GEN_SPE(evnor, eveqv, 0x0C, 0x08, 0x00000000, 0x00000000, PPC_SPE); ////
7593GEN_SPE(evmwumi, evmwsmi, 0x0C, 0x11, 0x00000000, 0x00000000, PPC_SPE);
7594GEN_SPE(evmwumia, evmwsmia, 0x1C, 0x11, 0x00000000, 0x00000000, PPC_SPE);
7595GEN_SPE(evmwumiaa, evmwsmiaa, 0x0C, 0x15, 0x00000000, 0x00000000, PPC_SPE);
7596GEN_SPE(speundef, evorc, 0x0D, 0x08, 0xFFFFFFFF, 0x00000000, PPC_SPE); ////
7597GEN_SPE(evnand, speundef, 0x0F, 0x08, 0x00000000, 0xFFFFFFFF, PPC_SPE); ////
7598GEN_SPE(evsrwu, evsrws, 0x10, 0x08, 0x00000000, 0x00000000, PPC_SPE); ////
7599GEN_SPE(evsrwiu, evsrwis, 0x11, 0x08, 0x00000000, 0x00000000, PPC_SPE);
7600GEN_SPE(evslw, speundef, 0x12, 0x08, 0x00000000, 0xFFFFFFFF, PPC_SPE); ////
7601GEN_SPE(evslwi, speundef, 0x13, 0x08, 0x00000000, 0xFFFFFFFF, PPC_SPE);
7602GEN_SPE(evrlw, evsplati, 0x14, 0x08, 0x00000000, 0x0000F800, PPC_SPE); //
7603GEN_SPE(evrlwi, evsplatfi, 0x15, 0x08, 0x00000000, 0x0000F800, PPC_SPE);
7604GEN_SPE(evmergehi, evmergelo, 0x16, 0x08, 0x00000000, 0x00000000, PPC_SPE); ////
7605GEN_SPE(evmergehilo, evmergelohi, 0x17, 0x08, 0x00000000, 0x00000000, PPC_SPE); ////
7606GEN_SPE(evcmpgtu, evcmpgts, 0x18, 0x08, 0x00600000, 0x00600000, PPC_SPE); ////
7607GEN_SPE(evcmpltu, evcmplts, 0x19, 0x08, 0x00600000, 0x00600000, PPC_SPE); ////
7608GEN_SPE(evcmpeq, speundef, 0x1A, 0x08, 0x00600000, 0xFFFFFFFF, PPC_SPE); ////
0487d6a8 7609
6a6ae23f 7610/* SPE load and stores */
636aa200 7611static inline void gen_addr_spe_imm_index(DisasContext *ctx, TCGv EA, int sh)
6a6ae23f
AJ
7612{
7613 target_ulong uimm = rB(ctx->opcode);
7614
76db3ba4 7615 if (rA(ctx->opcode) == 0) {
6a6ae23f 7616 tcg_gen_movi_tl(EA, uimm << sh);
76db3ba4 7617 } else {
6a6ae23f 7618 tcg_gen_addi_tl(EA, cpu_gpr[rA(ctx->opcode)], uimm << sh);
c791fe84 7619 if (NARROW_MODE(ctx)) {
76db3ba4
AJ
7620 tcg_gen_ext32u_tl(EA, EA);
7621 }
76db3ba4 7622 }
0487d6a8 7623}
6a6ae23f 7624
636aa200 7625static inline void gen_op_evldd(DisasContext *ctx, TCGv addr)
6a6ae23f
AJ
7626{
7627#if defined(TARGET_PPC64)
76db3ba4 7628 gen_qemu_ld64(ctx, cpu_gpr[rD(ctx->opcode)], addr);
6a6ae23f
AJ
7629#else
7630 TCGv_i64 t0 = tcg_temp_new_i64();
76db3ba4 7631 gen_qemu_ld64(ctx, t0, addr);
6a6ae23f
AJ
7632 tcg_gen_trunc_i64_i32(cpu_gpr[rD(ctx->opcode)], t0);
7633 tcg_gen_shri_i64(t0, t0, 32);
7634 tcg_gen_trunc_i64_i32(cpu_gprh[rD(ctx->opcode)], t0);
7635 tcg_temp_free_i64(t0);
7636#endif
0487d6a8 7637}
6a6ae23f 7638
636aa200 7639static inline void gen_op_evldw(DisasContext *ctx, TCGv addr)
6a6ae23f 7640{
0487d6a8 7641#if defined(TARGET_PPC64)
6a6ae23f 7642 TCGv t0 = tcg_temp_new();
76db3ba4 7643 gen_qemu_ld32u(ctx, t0, addr);
6a6ae23f 7644 tcg_gen_shli_tl(cpu_gpr[rD(ctx->opcode)], t0, 32);
76db3ba4
AJ
7645 gen_addr_add(ctx, addr, addr, 4);
7646 gen_qemu_ld32u(ctx, t0, addr);
6a6ae23f
AJ
7647 tcg_gen_or_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rD(ctx->opcode)], t0);
7648 tcg_temp_free(t0);
7649#else
76db3ba4
AJ
7650 gen_qemu_ld32u(ctx, cpu_gprh[rD(ctx->opcode)], addr);
7651 gen_addr_add(ctx, addr, addr, 4);
7652 gen_qemu_ld32u(ctx, cpu_gpr[rD(ctx->opcode)], addr);
6a6ae23f 7653#endif
0487d6a8 7654}
6a6ae23f 7655
636aa200 7656static inline void gen_op_evldh(DisasContext *ctx, TCGv addr)
6a6ae23f
AJ
7657{
7658 TCGv t0 = tcg_temp_new();
7659#if defined(TARGET_PPC64)
76db3ba4 7660 gen_qemu_ld16u(ctx, t0, addr);
6a6ae23f 7661 tcg_gen_shli_tl(cpu_gpr[rD(ctx->opcode)], t0, 48);
76db3ba4
AJ
7662 gen_addr_add(ctx, addr, addr, 2);
7663 gen_qemu_ld16u(ctx, t0, addr);
6a6ae23f
AJ
7664 tcg_gen_shli_tl(t0, t0, 32);
7665 tcg_gen_or_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rD(ctx->opcode)], t0);
76db3ba4
AJ
7666 gen_addr_add(ctx, addr, addr, 2);
7667 gen_qemu_ld16u(ctx, t0, addr);
6a6ae23f
AJ
7668 tcg_gen_shli_tl(t0, t0, 16);
7669 tcg_gen_or_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rD(ctx->opcode)], t0);
76db3ba4
AJ
7670 gen_addr_add(ctx, addr, addr, 2);
7671 gen_qemu_ld16u(ctx, t0, addr);
6a6ae23f 7672 tcg_gen_or_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rD(ctx->opcode)], t0);
0487d6a8 7673#else
76db3ba4 7674 gen_qemu_ld16u(ctx, t0, addr);
6a6ae23f 7675 tcg_gen_shli_tl(cpu_gprh[rD(ctx->opcode)], t0, 16);
76db3ba4
AJ
7676 gen_addr_add(ctx, addr, addr, 2);
7677 gen_qemu_ld16u(ctx, t0, addr);
6a6ae23f 7678 tcg_gen_or_tl(cpu_gprh[rD(ctx->opcode)], cpu_gprh[rD(ctx->opcode)], t0);
76db3ba4
AJ
7679 gen_addr_add(ctx, addr, addr, 2);
7680 gen_qemu_ld16u(ctx, t0, addr);
6a6ae23f 7681 tcg_gen_shli_tl(cpu_gprh[rD(ctx->opcode)], t0, 16);
76db3ba4
AJ
7682 gen_addr_add(ctx, addr, addr, 2);
7683 gen_qemu_ld16u(ctx, t0, addr);
6a6ae23f 7684 tcg_gen_or_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rD(ctx->opcode)], t0);
0487d6a8 7685#endif
6a6ae23f 7686 tcg_temp_free(t0);
0487d6a8
JM
7687}
7688
636aa200 7689static inline void gen_op_evlhhesplat(DisasContext *ctx, TCGv addr)
6a6ae23f
AJ
7690{
7691 TCGv t0 = tcg_temp_new();
76db3ba4 7692 gen_qemu_ld16u(ctx, t0, addr);
6a6ae23f
AJ
7693#if defined(TARGET_PPC64)
7694 tcg_gen_shli_tl(cpu_gpr[rD(ctx->opcode)], t0, 48);
7695 tcg_gen_shli_tl(t0, t0, 16);
7696 tcg_gen_or_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rD(ctx->opcode)], t0);
7697#else
7698 tcg_gen_shli_tl(t0, t0, 16);
7699 tcg_gen_mov_tl(cpu_gprh[rD(ctx->opcode)], t0);
7700 tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], t0);
7701#endif
7702 tcg_temp_free(t0);
0487d6a8
JM
7703}
7704
636aa200 7705static inline void gen_op_evlhhousplat(DisasContext *ctx, TCGv addr)
6a6ae23f
AJ
7706{
7707 TCGv t0 = tcg_temp_new();
76db3ba4 7708 gen_qemu_ld16u(ctx, t0, addr);
6a6ae23f
AJ
7709#if defined(TARGET_PPC64)
7710 tcg_gen_shli_tl(cpu_gpr[rD(ctx->opcode)], t0, 32);
7711 tcg_gen_or_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rD(ctx->opcode)], t0);
7712#else
7713 tcg_gen_mov_tl(cpu_gprh[rD(ctx->opcode)], t0);
7714 tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], t0);
7715#endif
7716 tcg_temp_free(t0);
0487d6a8
JM
7717}
7718
636aa200 7719static inline void gen_op_evlhhossplat(DisasContext *ctx, TCGv addr)
6a6ae23f
AJ
7720{
7721 TCGv t0 = tcg_temp_new();
76db3ba4 7722 gen_qemu_ld16s(ctx, t0, addr);
6a6ae23f
AJ
7723#if defined(TARGET_PPC64)
7724 tcg_gen_shli_tl(cpu_gpr[rD(ctx->opcode)], t0, 32);
7725 tcg_gen_ext32u_tl(t0, t0);
7726 tcg_gen_or_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rD(ctx->opcode)], t0);
7727#else
7728 tcg_gen_mov_tl(cpu_gprh[rD(ctx->opcode)], t0);
7729 tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], t0);
7730#endif
7731 tcg_temp_free(t0);
7732}
7733
636aa200 7734static inline void gen_op_evlwhe(DisasContext *ctx, TCGv addr)
6a6ae23f
AJ
7735{
7736 TCGv t0 = tcg_temp_new();
7737#if defined(TARGET_PPC64)
76db3ba4 7738 gen_qemu_ld16u(ctx, t0, addr);
6a6ae23f 7739 tcg_gen_shli_tl(cpu_gpr[rD(ctx->opcode)], t0, 48);
76db3ba4
AJ
7740 gen_addr_add(ctx, addr, addr, 2);
7741 gen_qemu_ld16u(ctx, t0, addr);
6a6ae23f
AJ
7742 tcg_gen_shli_tl(t0, t0, 16);
7743 tcg_gen_or_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rD(ctx->opcode)], t0);
7744#else
76db3ba4 7745 gen_qemu_ld16u(ctx, t0, addr);
6a6ae23f 7746 tcg_gen_shli_tl(cpu_gprh[rD(ctx->opcode)], t0, 16);
76db3ba4
AJ
7747 gen_addr_add(ctx, addr, addr, 2);
7748 gen_qemu_ld16u(ctx, t0, addr);
6a6ae23f
AJ
7749 tcg_gen_shli_tl(cpu_gpr[rD(ctx->opcode)], t0, 16);
7750#endif
7751 tcg_temp_free(t0);
7752}
7753
636aa200 7754static inline void gen_op_evlwhou(DisasContext *ctx, TCGv addr)
6a6ae23f
AJ
7755{
7756#if defined(TARGET_PPC64)
7757 TCGv t0 = tcg_temp_new();
76db3ba4
AJ
7758 gen_qemu_ld16u(ctx, cpu_gpr[rD(ctx->opcode)], addr);
7759 gen_addr_add(ctx, addr, addr, 2);
7760 gen_qemu_ld16u(ctx, t0, addr);
6a6ae23f
AJ
7761 tcg_gen_shli_tl(t0, t0, 32);
7762 tcg_gen_or_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rD(ctx->opcode)], t0);
7763 tcg_temp_free(t0);
7764#else
76db3ba4
AJ
7765 gen_qemu_ld16u(ctx, cpu_gprh[rD(ctx->opcode)], addr);
7766 gen_addr_add(ctx, addr, addr, 2);
7767 gen_qemu_ld16u(ctx, cpu_gpr[rD(ctx->opcode)], addr);
6a6ae23f
AJ
7768#endif
7769}
7770
636aa200 7771static inline void gen_op_evlwhos(DisasContext *ctx, TCGv addr)
6a6ae23f
AJ
7772{
7773#if defined(TARGET_PPC64)
7774 TCGv t0 = tcg_temp_new();
76db3ba4 7775 gen_qemu_ld16s(ctx, t0, addr);
6a6ae23f 7776 tcg_gen_ext32u_tl(cpu_gpr[rD(ctx->opcode)], t0);
76db3ba4
AJ
7777 gen_addr_add(ctx, addr, addr, 2);
7778 gen_qemu_ld16s(ctx, t0, addr);
6a6ae23f
AJ
7779 tcg_gen_shli_tl(t0, t0, 32);
7780 tcg_gen_or_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rD(ctx->opcode)], t0);
7781 tcg_temp_free(t0);
7782#else
76db3ba4
AJ
7783 gen_qemu_ld16s(ctx, cpu_gprh[rD(ctx->opcode)], addr);
7784 gen_addr_add(ctx, addr, addr, 2);
7785 gen_qemu_ld16s(ctx, cpu_gpr[rD(ctx->opcode)], addr);
6a6ae23f
AJ
7786#endif
7787}
7788
636aa200 7789static inline void gen_op_evlwwsplat(DisasContext *ctx, TCGv addr)
6a6ae23f
AJ
7790{
7791 TCGv t0 = tcg_temp_new();
76db3ba4 7792 gen_qemu_ld32u(ctx, t0, addr);
0487d6a8 7793#if defined(TARGET_PPC64)
6a6ae23f
AJ
7794 tcg_gen_shli_tl(cpu_gpr[rD(ctx->opcode)], t0, 32);
7795 tcg_gen_or_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rD(ctx->opcode)], t0);
7796#else
7797 tcg_gen_mov_tl(cpu_gprh[rD(ctx->opcode)], t0);
7798 tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], t0);
7799#endif
7800 tcg_temp_free(t0);
7801}
7802
636aa200 7803static inline void gen_op_evlwhsplat(DisasContext *ctx, TCGv addr)
6a6ae23f
AJ
7804{
7805 TCGv t0 = tcg_temp_new();
7806#if defined(TARGET_PPC64)
76db3ba4 7807 gen_qemu_ld16u(ctx, t0, addr);
6a6ae23f
AJ
7808 tcg_gen_shli_tl(cpu_gpr[rD(ctx->opcode)], t0, 48);
7809 tcg_gen_shli_tl(t0, t0, 32);
7810 tcg_gen_or_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rD(ctx->opcode)], t0);
76db3ba4
AJ
7811 gen_addr_add(ctx, addr, addr, 2);
7812 gen_qemu_ld16u(ctx, t0, addr);
6a6ae23f
AJ
7813 tcg_gen_or_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rD(ctx->opcode)], t0);
7814 tcg_gen_shli_tl(t0, t0, 16);
7815 tcg_gen_or_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rD(ctx->opcode)], t0);
7816#else
76db3ba4 7817 gen_qemu_ld16u(ctx, t0, addr);
6a6ae23f
AJ
7818 tcg_gen_shli_tl(cpu_gprh[rD(ctx->opcode)], t0, 16);
7819 tcg_gen_or_tl(cpu_gprh[rD(ctx->opcode)], cpu_gprh[rD(ctx->opcode)], t0);
76db3ba4
AJ
7820 gen_addr_add(ctx, addr, addr, 2);
7821 gen_qemu_ld16u(ctx, t0, addr);
6a6ae23f
AJ
7822 tcg_gen_shli_tl(cpu_gpr[rD(ctx->opcode)], t0, 16);
7823 tcg_gen_or_tl(cpu_gpr[rD(ctx->opcode)], cpu_gprh[rD(ctx->opcode)], t0);
0487d6a8 7824#endif
6a6ae23f
AJ
7825 tcg_temp_free(t0);
7826}
7827
636aa200 7828static inline void gen_op_evstdd(DisasContext *ctx, TCGv addr)
6a6ae23f
AJ
7829{
7830#if defined(TARGET_PPC64)
76db3ba4 7831 gen_qemu_st64(ctx, cpu_gpr[rS(ctx->opcode)], addr);
0487d6a8 7832#else
6a6ae23f
AJ
7833 TCGv_i64 t0 = tcg_temp_new_i64();
7834 tcg_gen_concat_i32_i64(t0, cpu_gpr[rS(ctx->opcode)], cpu_gprh[rS(ctx->opcode)]);
76db3ba4 7835 gen_qemu_st64(ctx, t0, addr);
6a6ae23f
AJ
7836 tcg_temp_free_i64(t0);
7837#endif
7838}
7839
636aa200 7840static inline void gen_op_evstdw(DisasContext *ctx, TCGv addr)
6a6ae23f 7841{
0487d6a8 7842#if defined(TARGET_PPC64)
6a6ae23f
AJ
7843 TCGv t0 = tcg_temp_new();
7844 tcg_gen_shri_tl(t0, cpu_gpr[rS(ctx->opcode)], 32);
76db3ba4 7845 gen_qemu_st32(ctx, t0, addr);
6a6ae23f
AJ
7846 tcg_temp_free(t0);
7847#else
76db3ba4 7848 gen_qemu_st32(ctx, cpu_gprh[rS(ctx->opcode)], addr);
6a6ae23f 7849#endif
76db3ba4
AJ
7850 gen_addr_add(ctx, addr, addr, 4);
7851 gen_qemu_st32(ctx, cpu_gpr[rS(ctx->opcode)], addr);
6a6ae23f
AJ
7852}
7853
636aa200 7854static inline void gen_op_evstdh(DisasContext *ctx, TCGv addr)
6a6ae23f
AJ
7855{
7856 TCGv t0 = tcg_temp_new();
7857#if defined(TARGET_PPC64)
7858 tcg_gen_shri_tl(t0, cpu_gpr[rS(ctx->opcode)], 48);
7859#else
7860 tcg_gen_shri_tl(t0, cpu_gprh[rS(ctx->opcode)], 16);
7861#endif
76db3ba4
AJ
7862 gen_qemu_st16(ctx, t0, addr);
7863 gen_addr_add(ctx, addr, addr, 2);
6a6ae23f
AJ
7864#if defined(TARGET_PPC64)
7865 tcg_gen_shri_tl(t0, cpu_gpr[rS(ctx->opcode)], 32);
76db3ba4 7866 gen_qemu_st16(ctx, t0, addr);
6a6ae23f 7867#else
76db3ba4 7868 gen_qemu_st16(ctx, cpu_gprh[rS(ctx->opcode)], addr);
6a6ae23f 7869#endif
76db3ba4 7870 gen_addr_add(ctx, addr, addr, 2);
6a6ae23f 7871 tcg_gen_shri_tl(t0, cpu_gpr[rS(ctx->opcode)], 16);
76db3ba4 7872 gen_qemu_st16(ctx, t0, addr);
6a6ae23f 7873 tcg_temp_free(t0);
76db3ba4
AJ
7874 gen_addr_add(ctx, addr, addr, 2);
7875 gen_qemu_st16(ctx, cpu_gpr[rS(ctx->opcode)], addr);
6a6ae23f
AJ
7876}
7877
636aa200 7878static inline void gen_op_evstwhe(DisasContext *ctx, TCGv addr)
6a6ae23f
AJ
7879{
7880 TCGv t0 = tcg_temp_new();
7881#if defined(TARGET_PPC64)
7882 tcg_gen_shri_tl(t0, cpu_gpr[rS(ctx->opcode)], 48);
7883#else
7884 tcg_gen_shri_tl(t0, cpu_gprh[rS(ctx->opcode)], 16);
7885#endif
76db3ba4
AJ
7886 gen_qemu_st16(ctx, t0, addr);
7887 gen_addr_add(ctx, addr, addr, 2);
6a6ae23f 7888 tcg_gen_shri_tl(t0, cpu_gpr[rS(ctx->opcode)], 16);
76db3ba4 7889 gen_qemu_st16(ctx, t0, addr);
6a6ae23f
AJ
7890 tcg_temp_free(t0);
7891}
7892
636aa200 7893static inline void gen_op_evstwho(DisasContext *ctx, TCGv addr)
6a6ae23f
AJ
7894{
7895#if defined(TARGET_PPC64)
7896 TCGv t0 = tcg_temp_new();
7897 tcg_gen_shri_tl(t0, cpu_gpr[rS(ctx->opcode)], 32);
76db3ba4 7898 gen_qemu_st16(ctx, t0, addr);
6a6ae23f
AJ
7899 tcg_temp_free(t0);
7900#else
76db3ba4 7901 gen_qemu_st16(ctx, cpu_gprh[rS(ctx->opcode)], addr);
6a6ae23f 7902#endif
76db3ba4
AJ
7903 gen_addr_add(ctx, addr, addr, 2);
7904 gen_qemu_st16(ctx, cpu_gpr[rS(ctx->opcode)], addr);
6a6ae23f
AJ
7905}
7906
636aa200 7907static inline void gen_op_evstwwe(DisasContext *ctx, TCGv addr)
6a6ae23f
AJ
7908{
7909#if defined(TARGET_PPC64)
7910 TCGv t0 = tcg_temp_new();
7911 tcg_gen_shri_tl(t0, cpu_gpr[rS(ctx->opcode)], 32);
76db3ba4 7912 gen_qemu_st32(ctx, t0, addr);
6a6ae23f
AJ
7913 tcg_temp_free(t0);
7914#else
76db3ba4 7915 gen_qemu_st32(ctx, cpu_gprh[rS(ctx->opcode)], addr);
6a6ae23f
AJ
7916#endif
7917}
7918
636aa200 7919static inline void gen_op_evstwwo(DisasContext *ctx, TCGv addr)
6a6ae23f 7920{
76db3ba4 7921 gen_qemu_st32(ctx, cpu_gpr[rS(ctx->opcode)], addr);
6a6ae23f
AJ
7922}
7923
7924#define GEN_SPEOP_LDST(name, opc2, sh) \
99e300ef 7925static void glue(gen_, name)(DisasContext *ctx) \
6a6ae23f
AJ
7926{ \
7927 TCGv t0; \
7928 if (unlikely(!ctx->spe_enabled)) { \
27a69bb0 7929 gen_exception(ctx, POWERPC_EXCP_SPEU); \
6a6ae23f
AJ
7930 return; \
7931 } \
76db3ba4 7932 gen_set_access_type(ctx, ACCESS_INT); \
6a6ae23f
AJ
7933 t0 = tcg_temp_new(); \
7934 if (Rc(ctx->opcode)) { \
76db3ba4 7935 gen_addr_spe_imm_index(ctx, t0, sh); \
6a6ae23f 7936 } else { \
76db3ba4 7937 gen_addr_reg_index(ctx, t0); \
6a6ae23f
AJ
7938 } \
7939 gen_op_##name(ctx, t0); \
7940 tcg_temp_free(t0); \
7941}
7942
7943GEN_SPEOP_LDST(evldd, 0x00, 3);
7944GEN_SPEOP_LDST(evldw, 0x01, 3);
7945GEN_SPEOP_LDST(evldh, 0x02, 3);
7946GEN_SPEOP_LDST(evlhhesplat, 0x04, 1);
7947GEN_SPEOP_LDST(evlhhousplat, 0x06, 1);
7948GEN_SPEOP_LDST(evlhhossplat, 0x07, 1);
7949GEN_SPEOP_LDST(evlwhe, 0x08, 2);
7950GEN_SPEOP_LDST(evlwhou, 0x0A, 2);
7951GEN_SPEOP_LDST(evlwhos, 0x0B, 2);
7952GEN_SPEOP_LDST(evlwwsplat, 0x0C, 2);
7953GEN_SPEOP_LDST(evlwhsplat, 0x0E, 2);
7954
7955GEN_SPEOP_LDST(evstdd, 0x10, 3);
7956GEN_SPEOP_LDST(evstdw, 0x11, 3);
7957GEN_SPEOP_LDST(evstdh, 0x12, 3);
7958GEN_SPEOP_LDST(evstwhe, 0x18, 2);
7959GEN_SPEOP_LDST(evstwho, 0x1A, 2);
7960GEN_SPEOP_LDST(evstwwe, 0x1C, 2);
7961GEN_SPEOP_LDST(evstwwo, 0x1E, 2);
0487d6a8
JM
7962
7963/* Multiply and add - TODO */
7964#if 0
70560da7
FC
7965GEN_SPE(speundef, evmhessf, 0x01, 0x10, 0xFFFFFFFF, 0x00000000, PPC_SPE);//
7966GEN_SPE(speundef, evmhossf, 0x03, 0x10, 0xFFFFFFFF, 0x00000000, PPC_SPE);
7967GEN_SPE(evmheumi, evmhesmi, 0x04, 0x10, 0x00000000, 0x00000000, PPC_SPE);
7968GEN_SPE(speundef, evmhesmf, 0x05, 0x10, 0xFFFFFFFF, 0x00000000, PPC_SPE);
7969GEN_SPE(evmhoumi, evmhosmi, 0x06, 0x10, 0x00000000, 0x00000000, PPC_SPE);
7970GEN_SPE(speundef, evmhosmf, 0x07, 0x10, 0xFFFFFFFF, 0x00000000, PPC_SPE);
7971GEN_SPE(speundef, evmhessfa, 0x11, 0x10, 0xFFFFFFFF, 0x00000000, PPC_SPE);
7972GEN_SPE(speundef, evmhossfa, 0x13, 0x10, 0xFFFFFFFF, 0x00000000, PPC_SPE);
7973GEN_SPE(evmheumia, evmhesmia, 0x14, 0x10, 0x00000000, 0x00000000, PPC_SPE);
7974GEN_SPE(speundef, evmhesmfa, 0x15, 0x10, 0xFFFFFFFF, 0x00000000, PPC_SPE);
7975GEN_SPE(evmhoumia, evmhosmia, 0x16, 0x10, 0x00000000, 0x00000000, PPC_SPE);
7976GEN_SPE(speundef, evmhosmfa, 0x17, 0x10, 0xFFFFFFFF, 0x00000000, PPC_SPE);
7977
7978GEN_SPE(speundef, evmwhssf, 0x03, 0x11, 0xFFFFFFFF, 0x00000000, PPC_SPE);
7979GEN_SPE(evmwlumi, speundef, 0x04, 0x11, 0x00000000, 0xFFFFFFFF, PPC_SPE);
7980GEN_SPE(evmwhumi, evmwhsmi, 0x06, 0x11, 0x00000000, 0x00000000, PPC_SPE);
7981GEN_SPE(speundef, evmwhsmf, 0x07, 0x11, 0xFFFFFFFF, 0x00000000, PPC_SPE);
7982GEN_SPE(speundef, evmwssf, 0x09, 0x11, 0xFFFFFFFF, 0x00000000, PPC_SPE);
7983GEN_SPE(speundef, evmwsmf, 0x0D, 0x11, 0xFFFFFFFF, 0x00000000, PPC_SPE);
7984GEN_SPE(speundef, evmwhssfa, 0x13, 0x11, 0xFFFFFFFF, 0x00000000, PPC_SPE);
7985GEN_SPE(evmwlumia, speundef, 0x14, 0x11, 0x00000000, 0xFFFFFFFF, PPC_SPE);
7986GEN_SPE(evmwhumia, evmwhsmia, 0x16, 0x11, 0x00000000, 0x00000000, PPC_SPE);
7987GEN_SPE(speundef, evmwhsmfa, 0x17, 0x11, 0xFFFFFFFF, 0x00000000, PPC_SPE);
7988GEN_SPE(speundef, evmwssfa, 0x19, 0x11, 0xFFFFFFFF, 0x00000000, PPC_SPE);
7989GEN_SPE(speundef, evmwsmfa, 0x1D, 0x11, 0xFFFFFFFF, 0x00000000, PPC_SPE);
7990
7991GEN_SPE(evadduiaaw, evaddsiaaw, 0x00, 0x13, 0x0000F800, 0x0000F800, PPC_SPE);
7992GEN_SPE(evsubfusiaaw, evsubfssiaaw, 0x01, 0x13, 0x0000F800, 0x0000F800, PPC_SPE);
7993GEN_SPE(evaddumiaaw, evaddsmiaaw, 0x04, 0x13, 0x0000F800, 0x0000F800, PPC_SPE);
7994GEN_SPE(evsubfumiaaw, evsubfsmiaaw, 0x05, 0x13, 0x0000F800, 0x0000F800, PPC_SPE);
7995GEN_SPE(evdivws, evdivwu, 0x06, 0x13, 0x00000000, 0x00000000, PPC_SPE);
7996
7997GEN_SPE(evmheusiaaw, evmhessiaaw, 0x00, 0x14, 0x00000000, 0x00000000, PPC_SPE);
7998GEN_SPE(speundef, evmhessfaaw, 0x01, 0x14, 0xFFFFFFFF, 0x00000000, PPC_SPE);
7999GEN_SPE(evmhousiaaw, evmhossiaaw, 0x02, 0x14, 0x00000000, 0x00000000, PPC_SPE);
8000GEN_SPE(speundef, evmhossfaaw, 0x03, 0x14, 0xFFFFFFFF, 0x00000000, PPC_SPE);
8001GEN_SPE(evmheumiaaw, evmhesmiaaw, 0x04, 0x14, 0x00000000, 0x00000000, PPC_SPE);
8002GEN_SPE(speundef, evmhesmfaaw, 0x05, 0x14, 0xFFFFFFFF, 0x00000000, PPC_SPE);
8003GEN_SPE(evmhoumiaaw, evmhosmiaaw, 0x06, 0x14, 0x00000000, 0x00000000, PPC_SPE);
8004GEN_SPE(speundef, evmhosmfaaw, 0x07, 0x14, 0xFFFFFFFF, 0x00000000, PPC_SPE);
8005GEN_SPE(evmhegumiaa, evmhegsmiaa, 0x14, 0x14, 0x00000000, 0x00000000, PPC_SPE);
8006GEN_SPE(speundef, evmhegsmfaa, 0x15, 0x14, 0xFFFFFFFF, 0x00000000, PPC_SPE);
8007GEN_SPE(evmhogumiaa, evmhogsmiaa, 0x16, 0x14, 0x00000000, 0x00000000, PPC_SPE);
8008GEN_SPE(speundef, evmhogsmfaa, 0x17, 0x14, 0xFFFFFFFF, 0x00000000, PPC_SPE);
8009
8010GEN_SPE(evmwlusiaaw, evmwlssiaaw, 0x00, 0x15, 0x00000000, 0x00000000, PPC_SPE);
8011GEN_SPE(evmwlumiaaw, evmwlsmiaaw, 0x04, 0x15, 0x00000000, 0x00000000, PPC_SPE);
8012GEN_SPE(speundef, evmwssfaa, 0x09, 0x15, 0xFFFFFFFF, 0x00000000, PPC_SPE);
8013GEN_SPE(speundef, evmwsmfaa, 0x0D, 0x15, 0xFFFFFFFF, 0x00000000, PPC_SPE);
8014
8015GEN_SPE(evmheusianw, evmhessianw, 0x00, 0x16, 0x00000000, 0x00000000, PPC_SPE);
8016GEN_SPE(speundef, evmhessfanw, 0x01, 0x16, 0xFFFFFFFF, 0x00000000, PPC_SPE);
8017GEN_SPE(evmhousianw, evmhossianw, 0x02, 0x16, 0x00000000, 0x00000000, PPC_SPE);
8018GEN_SPE(speundef, evmhossfanw, 0x03, 0x16, 0xFFFFFFFF, 0x00000000, PPC_SPE);
8019GEN_SPE(evmheumianw, evmhesmianw, 0x04, 0x16, 0x00000000, 0x00000000, PPC_SPE);
8020GEN_SPE(speundef, evmhesmfanw, 0x05, 0x16, 0xFFFFFFFF, 0x00000000, PPC_SPE);
8021GEN_SPE(evmhoumianw, evmhosmianw, 0x06, 0x16, 0x00000000, 0x00000000, PPC_SPE);
8022GEN_SPE(speundef, evmhosmfanw, 0x07, 0x16, 0xFFFFFFFF, 0x00000000, PPC_SPE);
8023GEN_SPE(evmhegumian, evmhegsmian, 0x14, 0x16, 0x00000000, 0x00000000, PPC_SPE);
8024GEN_SPE(speundef, evmhegsmfan, 0x15, 0x16, 0xFFFFFFFF, 0x00000000, PPC_SPE);
8025GEN_SPE(evmhigumian, evmhigsmian, 0x16, 0x16, 0x00000000, 0x00000000, PPC_SPE);
8026GEN_SPE(speundef, evmhogsmfan, 0x17, 0x16, 0xFFFFFFFF, 0x00000000, PPC_SPE);
8027
8028GEN_SPE(evmwlusianw, evmwlssianw, 0x00, 0x17, 0x00000000, 0x00000000, PPC_SPE);
8029GEN_SPE(evmwlumianw, evmwlsmianw, 0x04, 0x17, 0x00000000, 0x00000000, PPC_SPE);
8030GEN_SPE(speundef, evmwssfan, 0x09, 0x17, 0xFFFFFFFF, 0x00000000, PPC_SPE);
8031GEN_SPE(evmwumian, evmwsmian, 0x0C, 0x17, 0x00000000, 0x00000000, PPC_SPE);
8032GEN_SPE(speundef, evmwsmfan, 0x0D, 0x17, 0xFFFFFFFF, 0x00000000, PPC_SPE);
0487d6a8
JM
8033#endif
8034
8035/*** SPE floating-point extension ***/
1c97856d
AJ
8036#if defined(TARGET_PPC64)
8037#define GEN_SPEFPUOP_CONV_32_32(name) \
636aa200 8038static inline void gen_##name(DisasContext *ctx) \
0487d6a8 8039{ \
1c97856d
AJ
8040 TCGv_i32 t0; \
8041 TCGv t1; \
8042 t0 = tcg_temp_new_i32(); \
8043 tcg_gen_trunc_tl_i32(t0, cpu_gpr[rB(ctx->opcode)]); \
8e703949 8044 gen_helper_##name(t0, cpu_env, t0); \
1c97856d
AJ
8045 t1 = tcg_temp_new(); \
8046 tcg_gen_extu_i32_tl(t1, t0); \
8047 tcg_temp_free_i32(t0); \
8048 tcg_gen_andi_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rD(ctx->opcode)], \
8049 0xFFFFFFFF00000000ULL); \
8050 tcg_gen_or_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rD(ctx->opcode)], t1); \
8051 tcg_temp_free(t1); \
0487d6a8 8052}
1c97856d 8053#define GEN_SPEFPUOP_CONV_32_64(name) \
636aa200 8054static inline void gen_##name(DisasContext *ctx) \
1c97856d
AJ
8055{ \
8056 TCGv_i32 t0; \
8057 TCGv t1; \
8058 t0 = tcg_temp_new_i32(); \
8e703949 8059 gen_helper_##name(t0, cpu_env, cpu_gpr[rB(ctx->opcode)]); \
1c97856d
AJ
8060 t1 = tcg_temp_new(); \
8061 tcg_gen_extu_i32_tl(t1, t0); \
8062 tcg_temp_free_i32(t0); \
8063 tcg_gen_andi_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rD(ctx->opcode)], \
8064 0xFFFFFFFF00000000ULL); \
8065 tcg_gen_or_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rD(ctx->opcode)], t1); \
8066 tcg_temp_free(t1); \
8067}
8068#define GEN_SPEFPUOP_CONV_64_32(name) \
636aa200 8069static inline void gen_##name(DisasContext *ctx) \
1c97856d
AJ
8070{ \
8071 TCGv_i32 t0 = tcg_temp_new_i32(); \
8072 tcg_gen_trunc_tl_i32(t0, cpu_gpr[rB(ctx->opcode)]); \
8e703949 8073 gen_helper_##name(cpu_gpr[rD(ctx->opcode)], cpu_env, t0); \
1c97856d
AJ
8074 tcg_temp_free_i32(t0); \
8075}
8076#define GEN_SPEFPUOP_CONV_64_64(name) \
636aa200 8077static inline void gen_##name(DisasContext *ctx) \
1c97856d 8078{ \
8e703949
BS
8079 gen_helper_##name(cpu_gpr[rD(ctx->opcode)], cpu_env, \
8080 cpu_gpr[rB(ctx->opcode)]); \
1c97856d
AJ
8081}
8082#define GEN_SPEFPUOP_ARITH2_32_32(name) \
636aa200 8083static inline void gen_##name(DisasContext *ctx) \
57951c27 8084{ \
1c97856d
AJ
8085 TCGv_i32 t0, t1; \
8086 TCGv_i64 t2; \
57951c27 8087 if (unlikely(!ctx->spe_enabled)) { \
27a69bb0 8088 gen_exception(ctx, POWERPC_EXCP_SPEU); \
57951c27
AJ
8089 return; \
8090 } \
1c97856d
AJ
8091 t0 = tcg_temp_new_i32(); \
8092 t1 = tcg_temp_new_i32(); \
8093 tcg_gen_trunc_tl_i32(t0, cpu_gpr[rA(ctx->opcode)]); \
8094 tcg_gen_trunc_tl_i32(t1, cpu_gpr[rB(ctx->opcode)]); \
8e703949 8095 gen_helper_##name(t0, cpu_env, t0, t1); \
1c97856d
AJ
8096 tcg_temp_free_i32(t1); \
8097 t2 = tcg_temp_new(); \
8098 tcg_gen_extu_i32_tl(t2, t0); \
8099 tcg_temp_free_i32(t0); \
8100 tcg_gen_andi_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rD(ctx->opcode)], \
8101 0xFFFFFFFF00000000ULL); \
8102 tcg_gen_or_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rD(ctx->opcode)], t2); \
8103 tcg_temp_free(t2); \
57951c27 8104}
1c97856d 8105#define GEN_SPEFPUOP_ARITH2_64_64(name) \
636aa200 8106static inline void gen_##name(DisasContext *ctx) \
57951c27
AJ
8107{ \
8108 if (unlikely(!ctx->spe_enabled)) { \
27a69bb0 8109 gen_exception(ctx, POWERPC_EXCP_SPEU); \
57951c27
AJ
8110 return; \
8111 } \
8e703949
BS
8112 gen_helper_##name(cpu_gpr[rD(ctx->opcode)], cpu_env, \
8113 cpu_gpr[rA(ctx->opcode)], cpu_gpr[rB(ctx->opcode)]); \
57951c27 8114}
1c97856d 8115#define GEN_SPEFPUOP_COMP_32(name) \
636aa200 8116static inline void gen_##name(DisasContext *ctx) \
57951c27 8117{ \
1c97856d 8118 TCGv_i32 t0, t1; \
57951c27 8119 if (unlikely(!ctx->spe_enabled)) { \
27a69bb0 8120 gen_exception(ctx, POWERPC_EXCP_SPEU); \
57951c27
AJ
8121 return; \
8122 } \
1c97856d
AJ
8123 t0 = tcg_temp_new_i32(); \
8124 t1 = tcg_temp_new_i32(); \
8125 tcg_gen_trunc_tl_i32(t0, cpu_gpr[rA(ctx->opcode)]); \
8126 tcg_gen_trunc_tl_i32(t1, cpu_gpr[rB(ctx->opcode)]); \
8e703949 8127 gen_helper_##name(cpu_crf[crfD(ctx->opcode)], cpu_env, t0, t1); \
1c97856d
AJ
8128 tcg_temp_free_i32(t0); \
8129 tcg_temp_free_i32(t1); \
8130}
8131#define GEN_SPEFPUOP_COMP_64(name) \
636aa200 8132static inline void gen_##name(DisasContext *ctx) \
1c97856d
AJ
8133{ \
8134 if (unlikely(!ctx->spe_enabled)) { \
27a69bb0 8135 gen_exception(ctx, POWERPC_EXCP_SPEU); \
1c97856d
AJ
8136 return; \
8137 } \
8e703949 8138 gen_helper_##name(cpu_crf[crfD(ctx->opcode)], cpu_env, \
1c97856d
AJ
8139 cpu_gpr[rA(ctx->opcode)], cpu_gpr[rB(ctx->opcode)]); \
8140}
8141#else
8142#define GEN_SPEFPUOP_CONV_32_32(name) \
636aa200 8143static inline void gen_##name(DisasContext *ctx) \
1c97856d 8144{ \
8e703949
BS
8145 gen_helper_##name(cpu_gpr[rD(ctx->opcode)], cpu_env, \
8146 cpu_gpr[rB(ctx->opcode)]); \
57951c27 8147}
1c97856d 8148#define GEN_SPEFPUOP_CONV_32_64(name) \
636aa200 8149static inline void gen_##name(DisasContext *ctx) \
1c97856d
AJ
8150{ \
8151 TCGv_i64 t0 = tcg_temp_new_i64(); \
8152 gen_load_gpr64(t0, rB(ctx->opcode)); \
8e703949 8153 gen_helper_##name(cpu_gpr[rD(ctx->opcode)], cpu_env, t0); \
1c97856d
AJ
8154 tcg_temp_free_i64(t0); \
8155}
8156#define GEN_SPEFPUOP_CONV_64_32(name) \
636aa200 8157static inline void gen_##name(DisasContext *ctx) \
1c97856d
AJ
8158{ \
8159 TCGv_i64 t0 = tcg_temp_new_i64(); \
8e703949 8160 gen_helper_##name(t0, cpu_env, cpu_gpr[rB(ctx->opcode)]); \
1c97856d
AJ
8161 gen_store_gpr64(rD(ctx->opcode), t0); \
8162 tcg_temp_free_i64(t0); \
8163}
8164#define GEN_SPEFPUOP_CONV_64_64(name) \
636aa200 8165static inline void gen_##name(DisasContext *ctx) \
1c97856d
AJ
8166{ \
8167 TCGv_i64 t0 = tcg_temp_new_i64(); \
8168 gen_load_gpr64(t0, rB(ctx->opcode)); \
8e703949 8169 gen_helper_##name(t0, cpu_env, t0); \
1c97856d
AJ
8170 gen_store_gpr64(rD(ctx->opcode), t0); \
8171 tcg_temp_free_i64(t0); \
8172}
8173#define GEN_SPEFPUOP_ARITH2_32_32(name) \
636aa200 8174static inline void gen_##name(DisasContext *ctx) \
1c97856d
AJ
8175{ \
8176 if (unlikely(!ctx->spe_enabled)) { \
27a69bb0 8177 gen_exception(ctx, POWERPC_EXCP_SPEU); \
1c97856d
AJ
8178 return; \
8179 } \
8e703949 8180 gen_helper_##name(cpu_gpr[rD(ctx->opcode)], cpu_env, \
1c97856d
AJ
8181 cpu_gpr[rA(ctx->opcode)], cpu_gpr[rB(ctx->opcode)]); \
8182}
8183#define GEN_SPEFPUOP_ARITH2_64_64(name) \
636aa200 8184static inline void gen_##name(DisasContext *ctx) \
1c97856d
AJ
8185{ \
8186 TCGv_i64 t0, t1; \
8187 if (unlikely(!ctx->spe_enabled)) { \
27a69bb0 8188 gen_exception(ctx, POWERPC_EXCP_SPEU); \
1c97856d
AJ
8189 return; \
8190 } \
8191 t0 = tcg_temp_new_i64(); \
8192 t1 = tcg_temp_new_i64(); \
8193 gen_load_gpr64(t0, rA(ctx->opcode)); \
8194 gen_load_gpr64(t1, rB(ctx->opcode)); \
8e703949 8195 gen_helper_##name(t0, cpu_env, t0, t1); \
1c97856d
AJ
8196 gen_store_gpr64(rD(ctx->opcode), t0); \
8197 tcg_temp_free_i64(t0); \
8198 tcg_temp_free_i64(t1); \
8199}
8200#define GEN_SPEFPUOP_COMP_32(name) \
636aa200 8201static inline void gen_##name(DisasContext *ctx) \
1c97856d
AJ
8202{ \
8203 if (unlikely(!ctx->spe_enabled)) { \
27a69bb0 8204 gen_exception(ctx, POWERPC_EXCP_SPEU); \
1c97856d
AJ
8205 return; \
8206 } \
8e703949 8207 gen_helper_##name(cpu_crf[crfD(ctx->opcode)], cpu_env, \
1c97856d
AJ
8208 cpu_gpr[rA(ctx->opcode)], cpu_gpr[rB(ctx->opcode)]); \
8209}
8210#define GEN_SPEFPUOP_COMP_64(name) \
636aa200 8211static inline void gen_##name(DisasContext *ctx) \
1c97856d
AJ
8212{ \
8213 TCGv_i64 t0, t1; \
8214 if (unlikely(!ctx->spe_enabled)) { \
27a69bb0 8215 gen_exception(ctx, POWERPC_EXCP_SPEU); \
1c97856d
AJ
8216 return; \
8217 } \
8218 t0 = tcg_temp_new_i64(); \
8219 t1 = tcg_temp_new_i64(); \
8220 gen_load_gpr64(t0, rA(ctx->opcode)); \
8221 gen_load_gpr64(t1, rB(ctx->opcode)); \
8e703949 8222 gen_helper_##name(cpu_crf[crfD(ctx->opcode)], cpu_env, t0, t1); \
1c97856d
AJ
8223 tcg_temp_free_i64(t0); \
8224 tcg_temp_free_i64(t1); \
8225}
8226#endif
57951c27 8227
0487d6a8
JM
8228/* Single precision floating-point vectors operations */
8229/* Arithmetic */
1c97856d
AJ
8230GEN_SPEFPUOP_ARITH2_64_64(evfsadd);
8231GEN_SPEFPUOP_ARITH2_64_64(evfssub);
8232GEN_SPEFPUOP_ARITH2_64_64(evfsmul);
8233GEN_SPEFPUOP_ARITH2_64_64(evfsdiv);
636aa200 8234static inline void gen_evfsabs(DisasContext *ctx)
1c97856d
AJ
8235{
8236 if (unlikely(!ctx->spe_enabled)) {
27a69bb0 8237 gen_exception(ctx, POWERPC_EXCP_SPEU);
1c97856d
AJ
8238 return;
8239 }
8240#if defined(TARGET_PPC64)
6d5c34fa 8241 tcg_gen_andi_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)], ~0x8000000080000000LL);
1c97856d 8242#else
6d5c34fa
MP
8243 tcg_gen_andi_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)], ~0x80000000);
8244 tcg_gen_andi_tl(cpu_gprh[rD(ctx->opcode)], cpu_gprh[rA(ctx->opcode)], ~0x80000000);
1c97856d
AJ
8245#endif
8246}
636aa200 8247static inline void gen_evfsnabs(DisasContext *ctx)
1c97856d
AJ
8248{
8249 if (unlikely(!ctx->spe_enabled)) {
27a69bb0 8250 gen_exception(ctx, POWERPC_EXCP_SPEU);
1c97856d
AJ
8251 return;
8252 }
8253#if defined(TARGET_PPC64)
6d5c34fa 8254 tcg_gen_ori_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)], 0x8000000080000000LL);
1c97856d 8255#else
6d5c34fa
MP
8256 tcg_gen_ori_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)], 0x80000000);
8257 tcg_gen_ori_tl(cpu_gprh[rD(ctx->opcode)], cpu_gprh[rA(ctx->opcode)], 0x80000000);
1c97856d
AJ
8258#endif
8259}
636aa200 8260static inline void gen_evfsneg(DisasContext *ctx)
1c97856d
AJ
8261{
8262 if (unlikely(!ctx->spe_enabled)) {
27a69bb0 8263 gen_exception(ctx, POWERPC_EXCP_SPEU);
1c97856d
AJ
8264 return;
8265 }
8266#if defined(TARGET_PPC64)
6d5c34fa 8267 tcg_gen_xori_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)], 0x8000000080000000LL);
1c97856d 8268#else
6d5c34fa
MP
8269 tcg_gen_xori_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)], 0x80000000);
8270 tcg_gen_xori_tl(cpu_gprh[rD(ctx->opcode)], cpu_gprh[rA(ctx->opcode)], 0x80000000);
1c97856d
AJ
8271#endif
8272}
8273
0487d6a8 8274/* Conversion */
1c97856d
AJ
8275GEN_SPEFPUOP_CONV_64_64(evfscfui);
8276GEN_SPEFPUOP_CONV_64_64(evfscfsi);
8277GEN_SPEFPUOP_CONV_64_64(evfscfuf);
8278GEN_SPEFPUOP_CONV_64_64(evfscfsf);
8279GEN_SPEFPUOP_CONV_64_64(evfsctui);
8280GEN_SPEFPUOP_CONV_64_64(evfsctsi);
8281GEN_SPEFPUOP_CONV_64_64(evfsctuf);
8282GEN_SPEFPUOP_CONV_64_64(evfsctsf);
8283GEN_SPEFPUOP_CONV_64_64(evfsctuiz);
8284GEN_SPEFPUOP_CONV_64_64(evfsctsiz);
8285
0487d6a8 8286/* Comparison */
1c97856d
AJ
8287GEN_SPEFPUOP_COMP_64(evfscmpgt);
8288GEN_SPEFPUOP_COMP_64(evfscmplt);
8289GEN_SPEFPUOP_COMP_64(evfscmpeq);
8290GEN_SPEFPUOP_COMP_64(evfststgt);
8291GEN_SPEFPUOP_COMP_64(evfststlt);
8292GEN_SPEFPUOP_COMP_64(evfststeq);
0487d6a8
JM
8293
8294/* Opcodes definitions */
70560da7
FC
8295GEN_SPE(evfsadd, evfssub, 0x00, 0x0A, 0x00000000, 0x00000000, PPC_SPE_SINGLE); //
8296GEN_SPE(evfsabs, evfsnabs, 0x02, 0x0A, 0x0000F800, 0x0000F800, PPC_SPE_SINGLE); //
8297GEN_SPE(evfsneg, speundef, 0x03, 0x0A, 0x0000F800, 0xFFFFFFFF, PPC_SPE_SINGLE); //
8298GEN_SPE(evfsmul, evfsdiv, 0x04, 0x0A, 0x00000000, 0x00000000, PPC_SPE_SINGLE); //
8299GEN_SPE(evfscmpgt, evfscmplt, 0x06, 0x0A, 0x00600000, 0x00600000, PPC_SPE_SINGLE); //
8300GEN_SPE(evfscmpeq, speundef, 0x07, 0x0A, 0x00600000, 0xFFFFFFFF, PPC_SPE_SINGLE); //
8301GEN_SPE(evfscfui, evfscfsi, 0x08, 0x0A, 0x00180000, 0x00180000, PPC_SPE_SINGLE); //
8302GEN_SPE(evfscfuf, evfscfsf, 0x09, 0x0A, 0x00180000, 0x00180000, PPC_SPE_SINGLE); //
8303GEN_SPE(evfsctui, evfsctsi, 0x0A, 0x0A, 0x00180000, 0x00180000, PPC_SPE_SINGLE); //
8304GEN_SPE(evfsctuf, evfsctsf, 0x0B, 0x0A, 0x00180000, 0x00180000, PPC_SPE_SINGLE); //
8305GEN_SPE(evfsctuiz, speundef, 0x0C, 0x0A, 0x00180000, 0xFFFFFFFF, PPC_SPE_SINGLE); //
8306GEN_SPE(evfsctsiz, speundef, 0x0D, 0x0A, 0x00180000, 0xFFFFFFFF, PPC_SPE_SINGLE); //
8307GEN_SPE(evfststgt, evfststlt, 0x0E, 0x0A, 0x00600000, 0x00600000, PPC_SPE_SINGLE); //
8308GEN_SPE(evfststeq, speundef, 0x0F, 0x0A, 0x00600000, 0xFFFFFFFF, PPC_SPE_SINGLE); //
0487d6a8
JM
8309
8310/* Single precision floating-point operations */
8311/* Arithmetic */
1c97856d
AJ
8312GEN_SPEFPUOP_ARITH2_32_32(efsadd);
8313GEN_SPEFPUOP_ARITH2_32_32(efssub);
8314GEN_SPEFPUOP_ARITH2_32_32(efsmul);
8315GEN_SPEFPUOP_ARITH2_32_32(efsdiv);
636aa200 8316static inline void gen_efsabs(DisasContext *ctx)
1c97856d
AJ
8317{
8318 if (unlikely(!ctx->spe_enabled)) {
27a69bb0 8319 gen_exception(ctx, POWERPC_EXCP_SPEU);
1c97856d
AJ
8320 return;
8321 }
6d5c34fa 8322 tcg_gen_andi_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)], (target_long)~0x80000000LL);
1c97856d 8323}
636aa200 8324static inline void gen_efsnabs(DisasContext *ctx)
1c97856d
AJ
8325{
8326 if (unlikely(!ctx->spe_enabled)) {
27a69bb0 8327 gen_exception(ctx, POWERPC_EXCP_SPEU);
1c97856d
AJ
8328 return;
8329 }
6d5c34fa 8330 tcg_gen_ori_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)], 0x80000000);
1c97856d 8331}
636aa200 8332static inline void gen_efsneg(DisasContext *ctx)
1c97856d
AJ
8333{
8334 if (unlikely(!ctx->spe_enabled)) {
27a69bb0 8335 gen_exception(ctx, POWERPC_EXCP_SPEU);
1c97856d
AJ
8336 return;
8337 }
6d5c34fa 8338 tcg_gen_xori_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)], 0x80000000);
1c97856d
AJ
8339}
8340
0487d6a8 8341/* Conversion */
1c97856d
AJ
8342GEN_SPEFPUOP_CONV_32_32(efscfui);
8343GEN_SPEFPUOP_CONV_32_32(efscfsi);
8344GEN_SPEFPUOP_CONV_32_32(efscfuf);
8345GEN_SPEFPUOP_CONV_32_32(efscfsf);
8346GEN_SPEFPUOP_CONV_32_32(efsctui);
8347GEN_SPEFPUOP_CONV_32_32(efsctsi);
8348GEN_SPEFPUOP_CONV_32_32(efsctuf);
8349GEN_SPEFPUOP_CONV_32_32(efsctsf);
8350GEN_SPEFPUOP_CONV_32_32(efsctuiz);
8351GEN_SPEFPUOP_CONV_32_32(efsctsiz);
8352GEN_SPEFPUOP_CONV_32_64(efscfd);
8353
0487d6a8 8354/* Comparison */
1c97856d
AJ
8355GEN_SPEFPUOP_COMP_32(efscmpgt);
8356GEN_SPEFPUOP_COMP_32(efscmplt);
8357GEN_SPEFPUOP_COMP_32(efscmpeq);
8358GEN_SPEFPUOP_COMP_32(efststgt);
8359GEN_SPEFPUOP_COMP_32(efststlt);
8360GEN_SPEFPUOP_COMP_32(efststeq);
0487d6a8
JM
8361
8362/* Opcodes definitions */
70560da7
FC
8363GEN_SPE(efsadd, efssub, 0x00, 0x0B, 0x00000000, 0x00000000, PPC_SPE_SINGLE); //
8364GEN_SPE(efsabs, efsnabs, 0x02, 0x0B, 0x0000F800, 0x0000F800, PPC_SPE_SINGLE); //
8365GEN_SPE(efsneg, speundef, 0x03, 0x0B, 0x0000F800, 0xFFFFFFFF, PPC_SPE_SINGLE); //
8366GEN_SPE(efsmul, efsdiv, 0x04, 0x0B, 0x00000000, 0x00000000, PPC_SPE_SINGLE); //
8367GEN_SPE(efscmpgt, efscmplt, 0x06, 0x0B, 0x00600000, 0x00600000, PPC_SPE_SINGLE); //
8368GEN_SPE(efscmpeq, efscfd, 0x07, 0x0B, 0x00600000, 0x00180000, PPC_SPE_SINGLE); //
8369GEN_SPE(efscfui, efscfsi, 0x08, 0x0B, 0x00180000, 0x00180000, PPC_SPE_SINGLE); //
8370GEN_SPE(efscfuf, efscfsf, 0x09, 0x0B, 0x00180000, 0x00180000, PPC_SPE_SINGLE); //
8371GEN_SPE(efsctui, efsctsi, 0x0A, 0x0B, 0x00180000, 0x00180000, PPC_SPE_SINGLE); //
8372GEN_SPE(efsctuf, efsctsf, 0x0B, 0x0B, 0x00180000, 0x00180000, PPC_SPE_SINGLE); //
8373GEN_SPE(efsctuiz, speundef, 0x0C, 0x0B, 0x00180000, 0xFFFFFFFF, PPC_SPE_SINGLE); //
8374GEN_SPE(efsctsiz, speundef, 0x0D, 0x0B, 0x00180000, 0xFFFFFFFF, PPC_SPE_SINGLE); //
8375GEN_SPE(efststgt, efststlt, 0x0E, 0x0B, 0x00600000, 0x00600000, PPC_SPE_SINGLE); //
8376GEN_SPE(efststeq, speundef, 0x0F, 0x0B, 0x00600000, 0xFFFFFFFF, PPC_SPE_SINGLE); //
0487d6a8
JM
8377
8378/* Double precision floating-point operations */
8379/* Arithmetic */
1c97856d
AJ
8380GEN_SPEFPUOP_ARITH2_64_64(efdadd);
8381GEN_SPEFPUOP_ARITH2_64_64(efdsub);
8382GEN_SPEFPUOP_ARITH2_64_64(efdmul);
8383GEN_SPEFPUOP_ARITH2_64_64(efddiv);
636aa200 8384static inline void gen_efdabs(DisasContext *ctx)
1c97856d
AJ
8385{
8386 if (unlikely(!ctx->spe_enabled)) {
27a69bb0 8387 gen_exception(ctx, POWERPC_EXCP_SPEU);
1c97856d
AJ
8388 return;
8389 }
8390#if defined(TARGET_PPC64)
6d5c34fa 8391 tcg_gen_andi_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)], ~0x8000000000000000LL);
1c97856d 8392#else
6d5c34fa
MP
8393 tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)]);
8394 tcg_gen_andi_tl(cpu_gprh[rD(ctx->opcode)], cpu_gprh[rA(ctx->opcode)], ~0x80000000);
1c97856d
AJ
8395#endif
8396}
636aa200 8397static inline void gen_efdnabs(DisasContext *ctx)
1c97856d
AJ
8398{
8399 if (unlikely(!ctx->spe_enabled)) {
27a69bb0 8400 gen_exception(ctx, POWERPC_EXCP_SPEU);
1c97856d
AJ
8401 return;
8402 }
8403#if defined(TARGET_PPC64)
6d5c34fa 8404 tcg_gen_ori_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)], 0x8000000000000000LL);
1c97856d 8405#else
6d5c34fa
MP
8406 tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)]);
8407 tcg_gen_ori_tl(cpu_gprh[rD(ctx->opcode)], cpu_gprh[rA(ctx->opcode)], 0x80000000);
1c97856d
AJ
8408#endif
8409}
636aa200 8410static inline void gen_efdneg(DisasContext *ctx)
1c97856d
AJ
8411{
8412 if (unlikely(!ctx->spe_enabled)) {
27a69bb0 8413 gen_exception(ctx, POWERPC_EXCP_SPEU);
1c97856d
AJ
8414 return;
8415 }
8416#if defined(TARGET_PPC64)
6d5c34fa 8417 tcg_gen_xori_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)], 0x8000000000000000LL);
1c97856d 8418#else
6d5c34fa
MP
8419 tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)]);
8420 tcg_gen_xori_tl(cpu_gprh[rD(ctx->opcode)], cpu_gprh[rA(ctx->opcode)], 0x80000000);
1c97856d
AJ
8421#endif
8422}
8423
0487d6a8 8424/* Conversion */
1c97856d
AJ
8425GEN_SPEFPUOP_CONV_64_32(efdcfui);
8426GEN_SPEFPUOP_CONV_64_32(efdcfsi);
8427GEN_SPEFPUOP_CONV_64_32(efdcfuf);
8428GEN_SPEFPUOP_CONV_64_32(efdcfsf);
8429GEN_SPEFPUOP_CONV_32_64(efdctui);
8430GEN_SPEFPUOP_CONV_32_64(efdctsi);
8431GEN_SPEFPUOP_CONV_32_64(efdctuf);
8432GEN_SPEFPUOP_CONV_32_64(efdctsf);
8433GEN_SPEFPUOP_CONV_32_64(efdctuiz);
8434GEN_SPEFPUOP_CONV_32_64(efdctsiz);
8435GEN_SPEFPUOP_CONV_64_32(efdcfs);
8436GEN_SPEFPUOP_CONV_64_64(efdcfuid);
8437GEN_SPEFPUOP_CONV_64_64(efdcfsid);
8438GEN_SPEFPUOP_CONV_64_64(efdctuidz);
8439GEN_SPEFPUOP_CONV_64_64(efdctsidz);
0487d6a8 8440
0487d6a8 8441/* Comparison */
1c97856d
AJ
8442GEN_SPEFPUOP_COMP_64(efdcmpgt);
8443GEN_SPEFPUOP_COMP_64(efdcmplt);
8444GEN_SPEFPUOP_COMP_64(efdcmpeq);
8445GEN_SPEFPUOP_COMP_64(efdtstgt);
8446GEN_SPEFPUOP_COMP_64(efdtstlt);
8447GEN_SPEFPUOP_COMP_64(efdtsteq);
0487d6a8
JM
8448
8449/* Opcodes definitions */
70560da7
FC
8450GEN_SPE(efdadd, efdsub, 0x10, 0x0B, 0x00000000, 0x00000000, PPC_SPE_DOUBLE); //
8451GEN_SPE(efdcfuid, efdcfsid, 0x11, 0x0B, 0x00180000, 0x00180000, PPC_SPE_DOUBLE); //
8452GEN_SPE(efdabs, efdnabs, 0x12, 0x0B, 0x0000F800, 0x0000F800, PPC_SPE_DOUBLE); //
8453GEN_SPE(efdneg, speundef, 0x13, 0x0B, 0x0000F800, 0xFFFFFFFF, PPC_SPE_DOUBLE); //
8454GEN_SPE(efdmul, efddiv, 0x14, 0x0B, 0x00000000, 0x00000000, PPC_SPE_DOUBLE); //
8455GEN_SPE(efdctuidz, efdctsidz, 0x15, 0x0B, 0x00180000, 0x00180000, PPC_SPE_DOUBLE); //
8456GEN_SPE(efdcmpgt, efdcmplt, 0x16, 0x0B, 0x00600000, 0x00600000, PPC_SPE_DOUBLE); //
8457GEN_SPE(efdcmpeq, efdcfs, 0x17, 0x0B, 0x00600000, 0x00180000, PPC_SPE_DOUBLE); //
8458GEN_SPE(efdcfui, efdcfsi, 0x18, 0x0B, 0x00180000, 0x00180000, PPC_SPE_DOUBLE); //
8459GEN_SPE(efdcfuf, efdcfsf, 0x19, 0x0B, 0x00180000, 0x00180000, PPC_SPE_DOUBLE); //
8460GEN_SPE(efdctui, efdctsi, 0x1A, 0x0B, 0x00180000, 0x00180000, PPC_SPE_DOUBLE); //
8461GEN_SPE(efdctuf, efdctsf, 0x1B, 0x0B, 0x00180000, 0x00180000, PPC_SPE_DOUBLE); //
8462GEN_SPE(efdctuiz, speundef, 0x1C, 0x0B, 0x00180000, 0xFFFFFFFF, PPC_SPE_DOUBLE); //
8463GEN_SPE(efdctsiz, speundef, 0x1D, 0x0B, 0x00180000, 0xFFFFFFFF, PPC_SPE_DOUBLE); //
8464GEN_SPE(efdtstgt, efdtstlt, 0x1E, 0x0B, 0x00600000, 0x00600000, PPC_SPE_DOUBLE); //
8465GEN_SPE(efdtsteq, speundef, 0x1F, 0x0B, 0x00600000, 0xFFFFFFFF, PPC_SPE_DOUBLE); //
0487d6a8 8466
c227f099 8467static opcode_t opcodes[] = {
5c55ff99
BS
8468GEN_HANDLER(invalid, 0x00, 0x00, 0x00, 0xFFFFFFFF, PPC_NONE),
8469GEN_HANDLER(cmp, 0x1F, 0x00, 0x00, 0x00400000, PPC_INTEGER),
8470GEN_HANDLER(cmpi, 0x0B, 0xFF, 0xFF, 0x00400000, PPC_INTEGER),
8471GEN_HANDLER(cmpl, 0x1F, 0x00, 0x01, 0x00400000, PPC_INTEGER),
8472GEN_HANDLER(cmpli, 0x0A, 0xFF, 0xFF, 0x00400000, PPC_INTEGER),
fcfda20f 8473GEN_HANDLER_E(cmpb, 0x1F, 0x1C, 0x0F, 0x00000001, PPC_NONE, PPC2_ISA205),
5c55ff99
BS
8474GEN_HANDLER(isel, 0x1F, 0x0F, 0xFF, 0x00000001, PPC_ISEL),
8475GEN_HANDLER(addi, 0x0E, 0xFF, 0xFF, 0x00000000, PPC_INTEGER),
8476GEN_HANDLER(addic, 0x0C, 0xFF, 0xFF, 0x00000000, PPC_INTEGER),
8477GEN_HANDLER2(addic_, "addic.", 0x0D, 0xFF, 0xFF, 0x00000000, PPC_INTEGER),
8478GEN_HANDLER(addis, 0x0F, 0xFF, 0xFF, 0x00000000, PPC_INTEGER),
8479GEN_HANDLER(mulhw, 0x1F, 0x0B, 0x02, 0x00000400, PPC_INTEGER),
8480GEN_HANDLER(mulhwu, 0x1F, 0x0B, 0x00, 0x00000400, PPC_INTEGER),
8481GEN_HANDLER(mullw, 0x1F, 0x0B, 0x07, 0x00000000, PPC_INTEGER),
8482GEN_HANDLER(mullwo, 0x1F, 0x0B, 0x17, 0x00000000, PPC_INTEGER),
8483GEN_HANDLER(mulli, 0x07, 0xFF, 0xFF, 0x00000000, PPC_INTEGER),
8484#if defined(TARGET_PPC64)
8485GEN_HANDLER(mulld, 0x1F, 0x09, 0x07, 0x00000000, PPC_64B),
8486#endif
8487GEN_HANDLER(neg, 0x1F, 0x08, 0x03, 0x0000F800, PPC_INTEGER),
8488GEN_HANDLER(nego, 0x1F, 0x08, 0x13, 0x0000F800, PPC_INTEGER),
8489GEN_HANDLER(subfic, 0x08, 0xFF, 0xFF, 0x00000000, PPC_INTEGER),
8490GEN_HANDLER2(andi_, "andi.", 0x1C, 0xFF, 0xFF, 0x00000000, PPC_INTEGER),
8491GEN_HANDLER2(andis_, "andis.", 0x1D, 0xFF, 0xFF, 0x00000000, PPC_INTEGER),
8492GEN_HANDLER(cntlzw, 0x1F, 0x1A, 0x00, 0x00000000, PPC_INTEGER),
8493GEN_HANDLER(or, 0x1F, 0x1C, 0x0D, 0x00000000, PPC_INTEGER),
8494GEN_HANDLER(xor, 0x1F, 0x1C, 0x09, 0x00000000, PPC_INTEGER),
8495GEN_HANDLER(ori, 0x18, 0xFF, 0xFF, 0x00000000, PPC_INTEGER),
8496GEN_HANDLER(oris, 0x19, 0xFF, 0xFF, 0x00000000, PPC_INTEGER),
8497GEN_HANDLER(xori, 0x1A, 0xFF, 0xFF, 0x00000000, PPC_INTEGER),
8498GEN_HANDLER(xoris, 0x1B, 0xFF, 0xFF, 0x00000000, PPC_INTEGER),
8499GEN_HANDLER(popcntb, 0x1F, 0x03, 0x03, 0x0000F801, PPC_POPCNTB),
eaabeef2 8500GEN_HANDLER(popcntw, 0x1F, 0x1A, 0x0b, 0x0000F801, PPC_POPCNTWD),
5c55ff99 8501#if defined(TARGET_PPC64)
eaabeef2 8502GEN_HANDLER(popcntd, 0x1F, 0x1A, 0x0F, 0x0000F801, PPC_POPCNTWD),
5c55ff99
BS
8503GEN_HANDLER(cntlzd, 0x1F, 0x1A, 0x01, 0x00000000, PPC_64B),
8504#endif
8505GEN_HANDLER(rlwimi, 0x14, 0xFF, 0xFF, 0x00000000, PPC_INTEGER),
8506GEN_HANDLER(rlwinm, 0x15, 0xFF, 0xFF, 0x00000000, PPC_INTEGER),
8507GEN_HANDLER(rlwnm, 0x17, 0xFF, 0xFF, 0x00000000, PPC_INTEGER),
8508GEN_HANDLER(slw, 0x1F, 0x18, 0x00, 0x00000000, PPC_INTEGER),
8509GEN_HANDLER(sraw, 0x1F, 0x18, 0x18, 0x00000000, PPC_INTEGER),
8510GEN_HANDLER(srawi, 0x1F, 0x18, 0x19, 0x00000000, PPC_INTEGER),
8511GEN_HANDLER(srw, 0x1F, 0x18, 0x10, 0x00000000, PPC_INTEGER),
8512#if defined(TARGET_PPC64)
8513GEN_HANDLER(sld, 0x1F, 0x1B, 0x00, 0x00000000, PPC_64B),
8514GEN_HANDLER(srad, 0x1F, 0x1A, 0x18, 0x00000000, PPC_64B),
8515GEN_HANDLER2(sradi0, "sradi", 0x1F, 0x1A, 0x19, 0x00000000, PPC_64B),
8516GEN_HANDLER2(sradi1, "sradi", 0x1F, 0x1B, 0x19, 0x00000000, PPC_64B),
8517GEN_HANDLER(srd, 0x1F, 0x1B, 0x10, 0x00000000, PPC_64B),
8518#endif
8519GEN_HANDLER(frsqrtes, 0x3B, 0x1A, 0xFF, 0x001F07C0, PPC_FLOAT_FRSQRTES),
8520GEN_HANDLER(fsqrt, 0x3F, 0x16, 0xFF, 0x001F07C0, PPC_FLOAT_FSQRT),
8521GEN_HANDLER(fsqrts, 0x3B, 0x16, 0xFF, 0x001F07C0, PPC_FLOAT_FSQRT),
8522GEN_HANDLER(fcmpo, 0x3F, 0x00, 0x01, 0x00600001, PPC_FLOAT),
8523GEN_HANDLER(fcmpu, 0x3F, 0x00, 0x00, 0x00600001, PPC_FLOAT),
bf45a2e6 8524GEN_HANDLER(fabs, 0x3F, 0x08, 0x08, 0x001F0000, PPC_FLOAT),
5c55ff99 8525GEN_HANDLER(fmr, 0x3F, 0x08, 0x02, 0x001F0000, PPC_FLOAT),
bf45a2e6
AJ
8526GEN_HANDLER(fnabs, 0x3F, 0x08, 0x04, 0x001F0000, PPC_FLOAT),
8527GEN_HANDLER(fneg, 0x3F, 0x08, 0x01, 0x001F0000, PPC_FLOAT),
5c55ff99
BS
8528GEN_HANDLER(mcrfs, 0x3F, 0x00, 0x02, 0x0063F801, PPC_FLOAT),
8529GEN_HANDLER(mffs, 0x3F, 0x07, 0x12, 0x001FF800, PPC_FLOAT),
8530GEN_HANDLER(mtfsb0, 0x3F, 0x06, 0x02, 0x001FF800, PPC_FLOAT),
8531GEN_HANDLER(mtfsb1, 0x3F, 0x06, 0x01, 0x001FF800, PPC_FLOAT),
8532GEN_HANDLER(mtfsf, 0x3F, 0x07, 0x16, 0x00010000, PPC_FLOAT),
8533GEN_HANDLER(mtfsfi, 0x3F, 0x06, 0x04, 0x006f0800, PPC_FLOAT),
8534#if defined(TARGET_PPC64)
8535GEN_HANDLER(ld, 0x3A, 0xFF, 0xFF, 0x00000000, PPC_64B),
8536GEN_HANDLER(lq, 0x38, 0xFF, 0xFF, 0x00000000, PPC_64BX),
8537GEN_HANDLER(std, 0x3E, 0xFF, 0xFF, 0x00000000, PPC_64B),
8538#endif
8539GEN_HANDLER(lmw, 0x2E, 0xFF, 0xFF, 0x00000000, PPC_INTEGER),
8540GEN_HANDLER(stmw, 0x2F, 0xFF, 0xFF, 0x00000000, PPC_INTEGER),
8541GEN_HANDLER(lswi, 0x1F, 0x15, 0x12, 0x00000001, PPC_STRING),
8542GEN_HANDLER(lswx, 0x1F, 0x15, 0x10, 0x00000001, PPC_STRING),
8543GEN_HANDLER(stswi, 0x1F, 0x15, 0x16, 0x00000001, PPC_STRING),
8544GEN_HANDLER(stswx, 0x1F, 0x15, 0x14, 0x00000001, PPC_STRING),
8545GEN_HANDLER(eieio, 0x1F, 0x16, 0x1A, 0x03FFF801, PPC_MEM_EIEIO),
8546GEN_HANDLER(isync, 0x13, 0x16, 0x04, 0x03FFF801, PPC_MEM),
f844c817 8547GEN_HANDLER(lwarx, 0x1F, 0x14, 0x00, 0x00000000, PPC_RES),
5c55ff99
BS
8548GEN_HANDLER2(stwcx_, "stwcx.", 0x1F, 0x16, 0x04, 0x00000000, PPC_RES),
8549#if defined(TARGET_PPC64)
f844c817 8550GEN_HANDLER(ldarx, 0x1F, 0x14, 0x02, 0x00000000, PPC_64B),
5c55ff99
BS
8551GEN_HANDLER2(stdcx_, "stdcx.", 0x1F, 0x16, 0x06, 0x00000000, PPC_64B),
8552#endif
8553GEN_HANDLER(sync, 0x1F, 0x16, 0x12, 0x039FF801, PPC_MEM_SYNC),
8554GEN_HANDLER(wait, 0x1F, 0x1E, 0x01, 0x03FFF801, PPC_WAIT),
8555GEN_HANDLER(b, 0x12, 0xFF, 0xFF, 0x00000000, PPC_FLOW),
8556GEN_HANDLER(bc, 0x10, 0xFF, 0xFF, 0x00000000, PPC_FLOW),
8557GEN_HANDLER(bcctr, 0x13, 0x10, 0x10, 0x00000000, PPC_FLOW),
8558GEN_HANDLER(bclr, 0x13, 0x10, 0x00, 0x00000000, PPC_FLOW),
8559GEN_HANDLER(mcrf, 0x13, 0x00, 0xFF, 0x00000001, PPC_INTEGER),
8560GEN_HANDLER(rfi, 0x13, 0x12, 0x01, 0x03FF8001, PPC_FLOW),
8561#if defined(TARGET_PPC64)
8562GEN_HANDLER(rfid, 0x13, 0x12, 0x00, 0x03FF8001, PPC_64B),
8563GEN_HANDLER(hrfid, 0x13, 0x12, 0x08, 0x03FF8001, PPC_64H),
8564#endif
8565GEN_HANDLER(sc, 0x11, 0xFF, 0xFF, 0x03FFF01D, PPC_FLOW),
8566GEN_HANDLER(tw, 0x1F, 0x04, 0x00, 0x00000001, PPC_FLOW),
8567GEN_HANDLER(twi, 0x03, 0xFF, 0xFF, 0x00000000, PPC_FLOW),
8568#if defined(TARGET_PPC64)
8569GEN_HANDLER(td, 0x1F, 0x04, 0x02, 0x00000001, PPC_64B),
8570GEN_HANDLER(tdi, 0x02, 0xFF, 0xFF, 0x00000000, PPC_64B),
8571#endif
8572GEN_HANDLER(mcrxr, 0x1F, 0x00, 0x10, 0x007FF801, PPC_MISC),
8573GEN_HANDLER(mfcr, 0x1F, 0x13, 0x00, 0x00000801, PPC_MISC),
8574GEN_HANDLER(mfmsr, 0x1F, 0x13, 0x02, 0x001FF801, PPC_MISC),
8575GEN_HANDLER(mfspr, 0x1F, 0x13, 0x0A, 0x00000001, PPC_MISC),
8576GEN_HANDLER(mftb, 0x1F, 0x13, 0x0B, 0x00000001, PPC_MFTB),
8577GEN_HANDLER(mtcrf, 0x1F, 0x10, 0x04, 0x00000801, PPC_MISC),
8578#if defined(TARGET_PPC64)
8579GEN_HANDLER(mtmsrd, 0x1F, 0x12, 0x05, 0x001EF801, PPC_64B),
8580#endif
8581GEN_HANDLER(mtmsr, 0x1F, 0x12, 0x04, 0x001FF801, PPC_MISC),
8582GEN_HANDLER(mtspr, 0x1F, 0x13, 0x0E, 0x00000001, PPC_MISC),
8583GEN_HANDLER(dcbf, 0x1F, 0x16, 0x02, 0x03C00001, PPC_CACHE),
8584GEN_HANDLER(dcbi, 0x1F, 0x16, 0x0E, 0x03E00001, PPC_CACHE),
8585GEN_HANDLER(dcbst, 0x1F, 0x16, 0x01, 0x03E00001, PPC_CACHE),
8586GEN_HANDLER(dcbt, 0x1F, 0x16, 0x08, 0x02000001, PPC_CACHE),
8587GEN_HANDLER(dcbtst, 0x1F, 0x16, 0x07, 0x02000001, PPC_CACHE),
8e33944f 8588GEN_HANDLER(dcbz, 0x1F, 0x16, 0x1F, 0x03C00001, PPC_CACHE_DCBZ),
5c55ff99
BS
8589GEN_HANDLER(dst, 0x1F, 0x16, 0x0A, 0x01800001, PPC_ALTIVEC),
8590GEN_HANDLER(dstst, 0x1F, 0x16, 0x0B, 0x02000001, PPC_ALTIVEC),
8591GEN_HANDLER(dss, 0x1F, 0x16, 0x19, 0x019FF801, PPC_ALTIVEC),
8592GEN_HANDLER(icbi, 0x1F, 0x16, 0x1E, 0x03E00001, PPC_CACHE_ICBI),
8593GEN_HANDLER(dcba, 0x1F, 0x16, 0x17, 0x03E00001, PPC_CACHE_DCBA),
8594GEN_HANDLER(mfsr, 0x1F, 0x13, 0x12, 0x0010F801, PPC_SEGMENT),
8595GEN_HANDLER(mfsrin, 0x1F, 0x13, 0x14, 0x001F0001, PPC_SEGMENT),
8596GEN_HANDLER(mtsr, 0x1F, 0x12, 0x06, 0x0010F801, PPC_SEGMENT),
8597GEN_HANDLER(mtsrin, 0x1F, 0x12, 0x07, 0x001F0001, PPC_SEGMENT),
8598#if defined(TARGET_PPC64)
8599GEN_HANDLER2(mfsr_64b, "mfsr", 0x1F, 0x13, 0x12, 0x0010F801, PPC_SEGMENT_64B),
8600GEN_HANDLER2(mfsrin_64b, "mfsrin", 0x1F, 0x13, 0x14, 0x001F0001,
8601 PPC_SEGMENT_64B),
8602GEN_HANDLER2(mtsr_64b, "mtsr", 0x1F, 0x12, 0x06, 0x0010F801, PPC_SEGMENT_64B),
8603GEN_HANDLER2(mtsrin_64b, "mtsrin", 0x1F, 0x12, 0x07, 0x001F0001,
8604 PPC_SEGMENT_64B),
efdef95f
DG
8605GEN_HANDLER2(slbmte, "slbmte", 0x1F, 0x12, 0x0C, 0x001F0001, PPC_SEGMENT_64B),
8606GEN_HANDLER2(slbmfee, "slbmfee", 0x1F, 0x13, 0x1C, 0x001F0001, PPC_SEGMENT_64B),
8607GEN_HANDLER2(slbmfev, "slbmfev", 0x1F, 0x13, 0x1A, 0x001F0001, PPC_SEGMENT_64B),
5c55ff99
BS
8608#endif
8609GEN_HANDLER(tlbia, 0x1F, 0x12, 0x0B, 0x03FFFC01, PPC_MEM_TLBIA),
8610GEN_HANDLER(tlbiel, 0x1F, 0x12, 0x08, 0x03FF0001, PPC_MEM_TLBIE),
8611GEN_HANDLER(tlbie, 0x1F, 0x12, 0x09, 0x03FF0001, PPC_MEM_TLBIE),
8612GEN_HANDLER(tlbsync, 0x1F, 0x16, 0x11, 0x03FFF801, PPC_MEM_TLBSYNC),
8613#if defined(TARGET_PPC64)
8614GEN_HANDLER(slbia, 0x1F, 0x12, 0x0F, 0x03FFFC01, PPC_SLBI),
8615GEN_HANDLER(slbie, 0x1F, 0x12, 0x0D, 0x03FF0001, PPC_SLBI),
8616#endif
8617GEN_HANDLER(eciwx, 0x1F, 0x16, 0x0D, 0x00000001, PPC_EXTERN),
8618GEN_HANDLER(ecowx, 0x1F, 0x16, 0x09, 0x00000001, PPC_EXTERN),
8619GEN_HANDLER(abs, 0x1F, 0x08, 0x0B, 0x0000F800, PPC_POWER_BR),
8620GEN_HANDLER(abso, 0x1F, 0x08, 0x1B, 0x0000F800, PPC_POWER_BR),
8621GEN_HANDLER(clcs, 0x1F, 0x10, 0x13, 0x0000F800, PPC_POWER_BR),
8622GEN_HANDLER(div, 0x1F, 0x0B, 0x0A, 0x00000000, PPC_POWER_BR),
8623GEN_HANDLER(divo, 0x1F, 0x0B, 0x1A, 0x00000000, PPC_POWER_BR),
8624GEN_HANDLER(divs, 0x1F, 0x0B, 0x0B, 0x00000000, PPC_POWER_BR),
8625GEN_HANDLER(divso, 0x1F, 0x0B, 0x1B, 0x00000000, PPC_POWER_BR),
8626GEN_HANDLER(doz, 0x1F, 0x08, 0x08, 0x00000000, PPC_POWER_BR),
8627GEN_HANDLER(dozo, 0x1F, 0x08, 0x18, 0x00000000, PPC_POWER_BR),
8628GEN_HANDLER(dozi, 0x09, 0xFF, 0xFF, 0x00000000, PPC_POWER_BR),
8629GEN_HANDLER(lscbx, 0x1F, 0x15, 0x08, 0x00000000, PPC_POWER_BR),
8630GEN_HANDLER(maskg, 0x1F, 0x1D, 0x00, 0x00000000, PPC_POWER_BR),
8631GEN_HANDLER(maskir, 0x1F, 0x1D, 0x10, 0x00000000, PPC_POWER_BR),
8632GEN_HANDLER(mul, 0x1F, 0x0B, 0x03, 0x00000000, PPC_POWER_BR),
8633GEN_HANDLER(mulo, 0x1F, 0x0B, 0x13, 0x00000000, PPC_POWER_BR),
8634GEN_HANDLER(nabs, 0x1F, 0x08, 0x0F, 0x00000000, PPC_POWER_BR),
8635GEN_HANDLER(nabso, 0x1F, 0x08, 0x1F, 0x00000000, PPC_POWER_BR),
8636GEN_HANDLER(rlmi, 0x16, 0xFF, 0xFF, 0x00000000, PPC_POWER_BR),
8637GEN_HANDLER(rrib, 0x1F, 0x19, 0x10, 0x00000000, PPC_POWER_BR),
8638GEN_HANDLER(sle, 0x1F, 0x19, 0x04, 0x00000000, PPC_POWER_BR),
8639GEN_HANDLER(sleq, 0x1F, 0x19, 0x06, 0x00000000, PPC_POWER_BR),
8640GEN_HANDLER(sliq, 0x1F, 0x18, 0x05, 0x00000000, PPC_POWER_BR),
8641GEN_HANDLER(slliq, 0x1F, 0x18, 0x07, 0x00000000, PPC_POWER_BR),
8642GEN_HANDLER(sllq, 0x1F, 0x18, 0x06, 0x00000000, PPC_POWER_BR),
8643GEN_HANDLER(slq, 0x1F, 0x18, 0x04, 0x00000000, PPC_POWER_BR),
8644GEN_HANDLER(sraiq, 0x1F, 0x18, 0x1D, 0x00000000, PPC_POWER_BR),
8645GEN_HANDLER(sraq, 0x1F, 0x18, 0x1C, 0x00000000, PPC_POWER_BR),
8646GEN_HANDLER(sre, 0x1F, 0x19, 0x14, 0x00000000, PPC_POWER_BR),
8647GEN_HANDLER(srea, 0x1F, 0x19, 0x1C, 0x00000000, PPC_POWER_BR),
8648GEN_HANDLER(sreq, 0x1F, 0x19, 0x16, 0x00000000, PPC_POWER_BR),
8649GEN_HANDLER(sriq, 0x1F, 0x18, 0x15, 0x00000000, PPC_POWER_BR),
8650GEN_HANDLER(srliq, 0x1F, 0x18, 0x17, 0x00000000, PPC_POWER_BR),
8651GEN_HANDLER(srlq, 0x1F, 0x18, 0x16, 0x00000000, PPC_POWER_BR),
8652GEN_HANDLER(srq, 0x1F, 0x18, 0x14, 0x00000000, PPC_POWER_BR),
8653GEN_HANDLER(dsa, 0x1F, 0x14, 0x13, 0x03FFF801, PPC_602_SPEC),
8654GEN_HANDLER(esa, 0x1F, 0x14, 0x12, 0x03FFF801, PPC_602_SPEC),
8655GEN_HANDLER(mfrom, 0x1F, 0x09, 0x08, 0x03E0F801, PPC_602_SPEC),
8656GEN_HANDLER2(tlbld_6xx, "tlbld", 0x1F, 0x12, 0x1E, 0x03FF0001, PPC_6xx_TLB),
8657GEN_HANDLER2(tlbli_6xx, "tlbli", 0x1F, 0x12, 0x1F, 0x03FF0001, PPC_6xx_TLB),
8658GEN_HANDLER2(tlbld_74xx, "tlbld", 0x1F, 0x12, 0x1E, 0x03FF0001, PPC_74xx_TLB),
8659GEN_HANDLER2(tlbli_74xx, "tlbli", 0x1F, 0x12, 0x1F, 0x03FF0001, PPC_74xx_TLB),
8660GEN_HANDLER(clf, 0x1F, 0x16, 0x03, 0x03E00000, PPC_POWER),
8661GEN_HANDLER(cli, 0x1F, 0x16, 0x0F, 0x03E00000, PPC_POWER),
8662GEN_HANDLER(dclst, 0x1F, 0x16, 0x13, 0x03E00000, PPC_POWER),
8663GEN_HANDLER(mfsri, 0x1F, 0x13, 0x13, 0x00000001, PPC_POWER),
8664GEN_HANDLER(rac, 0x1F, 0x12, 0x19, 0x00000001, PPC_POWER),
8665GEN_HANDLER(rfsvc, 0x13, 0x12, 0x02, 0x03FFF0001, PPC_POWER),
8666GEN_HANDLER(lfq, 0x38, 0xFF, 0xFF, 0x00000003, PPC_POWER2),
8667GEN_HANDLER(lfqu, 0x39, 0xFF, 0xFF, 0x00000003, PPC_POWER2),
8668GEN_HANDLER(lfqux, 0x1F, 0x17, 0x19, 0x00000001, PPC_POWER2),
8669GEN_HANDLER(lfqx, 0x1F, 0x17, 0x18, 0x00000001, PPC_POWER2),
8670GEN_HANDLER(stfq, 0x3C, 0xFF, 0xFF, 0x00000003, PPC_POWER2),
8671GEN_HANDLER(stfqu, 0x3D, 0xFF, 0xFF, 0x00000003, PPC_POWER2),
8672GEN_HANDLER(stfqux, 0x1F, 0x17, 0x1D, 0x00000001, PPC_POWER2),
8673GEN_HANDLER(stfqx, 0x1F, 0x17, 0x1C, 0x00000001, PPC_POWER2),
8674GEN_HANDLER(mfapidi, 0x1F, 0x13, 0x08, 0x0000F801, PPC_MFAPIDI),
8675GEN_HANDLER(tlbiva, 0x1F, 0x12, 0x18, 0x03FFF801, PPC_TLBIVA),
8676GEN_HANDLER(mfdcr, 0x1F, 0x03, 0x0A, 0x00000001, PPC_DCR),
8677GEN_HANDLER(mtdcr, 0x1F, 0x03, 0x0E, 0x00000001, PPC_DCR),
8678GEN_HANDLER(mfdcrx, 0x1F, 0x03, 0x08, 0x00000000, PPC_DCRX),
8679GEN_HANDLER(mtdcrx, 0x1F, 0x03, 0x0C, 0x00000000, PPC_DCRX),
8680GEN_HANDLER(mfdcrux, 0x1F, 0x03, 0x09, 0x00000000, PPC_DCRUX),
8681GEN_HANDLER(mtdcrux, 0x1F, 0x03, 0x0D, 0x00000000, PPC_DCRUX),
8682GEN_HANDLER(dccci, 0x1F, 0x06, 0x0E, 0x03E00001, PPC_4xx_COMMON),
8683GEN_HANDLER(dcread, 0x1F, 0x06, 0x0F, 0x00000001, PPC_4xx_COMMON),
8684GEN_HANDLER2(icbt_40x, "icbt", 0x1F, 0x06, 0x08, 0x03E00001, PPC_40x_ICBT),
8685GEN_HANDLER(iccci, 0x1F, 0x06, 0x1E, 0x00000001, PPC_4xx_COMMON),
8686GEN_HANDLER(icread, 0x1F, 0x06, 0x1F, 0x03E00001, PPC_4xx_COMMON),
8687GEN_HANDLER2(rfci_40x, "rfci", 0x13, 0x13, 0x01, 0x03FF8001, PPC_40x_EXCP),
01662f3e 8688GEN_HANDLER_E(rfci, 0x13, 0x13, 0x01, 0x03FF8001, PPC_BOOKE, PPC2_BOOKE206),
5c55ff99
BS
8689GEN_HANDLER(rfdi, 0x13, 0x07, 0x01, 0x03FF8001, PPC_RFDI),
8690GEN_HANDLER(rfmci, 0x13, 0x06, 0x01, 0x03FF8001, PPC_RFMCI),
8691GEN_HANDLER2(tlbre_40x, "tlbre", 0x1F, 0x12, 0x1D, 0x00000001, PPC_40x_TLB),
8692GEN_HANDLER2(tlbsx_40x, "tlbsx", 0x1F, 0x12, 0x1C, 0x00000000, PPC_40x_TLB),
8693GEN_HANDLER2(tlbwe_40x, "tlbwe", 0x1F, 0x12, 0x1E, 0x00000001, PPC_40x_TLB),
8694GEN_HANDLER2(tlbre_440, "tlbre", 0x1F, 0x12, 0x1D, 0x00000001, PPC_BOOKE),
8695GEN_HANDLER2(tlbsx_440, "tlbsx", 0x1F, 0x12, 0x1C, 0x00000000, PPC_BOOKE),
8696GEN_HANDLER2(tlbwe_440, "tlbwe", 0x1F, 0x12, 0x1E, 0x00000001, PPC_BOOKE),
01662f3e
AG
8697GEN_HANDLER2_E(tlbre_booke206, "tlbre", 0x1F, 0x12, 0x1D, 0x00000001,
8698 PPC_NONE, PPC2_BOOKE206),
8699GEN_HANDLER2_E(tlbsx_booke206, "tlbsx", 0x1F, 0x12, 0x1C, 0x00000000,
8700 PPC_NONE, PPC2_BOOKE206),
8701GEN_HANDLER2_E(tlbwe_booke206, "tlbwe", 0x1F, 0x12, 0x1E, 0x00000001,
8702 PPC_NONE, PPC2_BOOKE206),
8703GEN_HANDLER2_E(tlbivax_booke206, "tlbivax", 0x1F, 0x12, 0x18, 0x00000001,
8704 PPC_NONE, PPC2_BOOKE206),
6d3db821
AG
8705GEN_HANDLER2_E(tlbilx_booke206, "tlbilx", 0x1F, 0x12, 0x00, 0x03800001,
8706 PPC_NONE, PPC2_BOOKE206),
d5d11a39
AG
8707GEN_HANDLER2_E(msgsnd, "msgsnd", 0x1F, 0x0E, 0x06, 0x03ff0001,
8708 PPC_NONE, PPC2_PRCNTL),
9e0b5cb1
AG
8709GEN_HANDLER2_E(msgclr, "msgclr", 0x1F, 0x0E, 0x07, 0x03ff0001,
8710 PPC_NONE, PPC2_PRCNTL),
5c55ff99 8711GEN_HANDLER(wrtee, 0x1F, 0x03, 0x04, 0x000FFC01, PPC_WRTEE),
fbe73008 8712GEN_HANDLER(wrteei, 0x1F, 0x03, 0x05, 0x000E7C01, PPC_WRTEE),
5c55ff99 8713GEN_HANDLER(dlmzb, 0x1F, 0x0E, 0x02, 0x00000000, PPC_440_SPEC),
01662f3e
AG
8714GEN_HANDLER_E(mbar, 0x1F, 0x16, 0x1a, 0x001FF801,
8715 PPC_BOOKE, PPC2_BOOKE206),
dcb2b9e1 8716GEN_HANDLER(msync_4xx, 0x1F, 0x16, 0x12, 0x03FFF801, PPC_BOOKE),
01662f3e
AG
8717GEN_HANDLER2_E(icbt_440, "icbt", 0x1F, 0x16, 0x00, 0x03E00001,
8718 PPC_BOOKE, PPC2_BOOKE206),
5c55ff99
BS
8719GEN_HANDLER(lvsl, 0x1f, 0x06, 0x00, 0x00000001, PPC_ALTIVEC),
8720GEN_HANDLER(lvsr, 0x1f, 0x06, 0x01, 0x00000001, PPC_ALTIVEC),
8721GEN_HANDLER(mfvscr, 0x04, 0x2, 0x18, 0x001ff800, PPC_ALTIVEC),
8722GEN_HANDLER(mtvscr, 0x04, 0x2, 0x19, 0x03ff0000, PPC_ALTIVEC),
8723GEN_HANDLER(vsldoi, 0x04, 0x16, 0xFF, 0x00000400, PPC_ALTIVEC),
8724GEN_HANDLER(vmladduhm, 0x04, 0x11, 0xFF, 0x00000000, PPC_ALTIVEC),
8725GEN_HANDLER2(evsel0, "evsel", 0x04, 0x1c, 0x09, 0x00000000, PPC_SPE),
8726GEN_HANDLER2(evsel1, "evsel", 0x04, 0x1d, 0x09, 0x00000000, PPC_SPE),
8727GEN_HANDLER2(evsel2, "evsel", 0x04, 0x1e, 0x09, 0x00000000, PPC_SPE),
8728GEN_HANDLER2(evsel3, "evsel", 0x04, 0x1f, 0x09, 0x00000000, PPC_SPE),
8729
8730#undef GEN_INT_ARITH_ADD
8731#undef GEN_INT_ARITH_ADD_CONST
8732#define GEN_INT_ARITH_ADD(name, opc3, add_ca, compute_ca, compute_ov) \
8733GEN_HANDLER(name, 0x1F, 0x0A, opc3, 0x00000000, PPC_INTEGER),
8734#define GEN_INT_ARITH_ADD_CONST(name, opc3, const_val, \
8735 add_ca, compute_ca, compute_ov) \
8736GEN_HANDLER(name, 0x1F, 0x0A, opc3, 0x0000F800, PPC_INTEGER),
8737GEN_INT_ARITH_ADD(add, 0x08, 0, 0, 0)
8738GEN_INT_ARITH_ADD(addo, 0x18, 0, 0, 1)
8739GEN_INT_ARITH_ADD(addc, 0x00, 0, 1, 0)
8740GEN_INT_ARITH_ADD(addco, 0x10, 0, 1, 1)
8741GEN_INT_ARITH_ADD(adde, 0x04, 1, 1, 0)
8742GEN_INT_ARITH_ADD(addeo, 0x14, 1, 1, 1)
8743GEN_INT_ARITH_ADD_CONST(addme, 0x07, -1LL, 1, 1, 0)
8744GEN_INT_ARITH_ADD_CONST(addmeo, 0x17, -1LL, 1, 1, 1)
8745GEN_INT_ARITH_ADD_CONST(addze, 0x06, 0, 1, 1, 0)
8746GEN_INT_ARITH_ADD_CONST(addzeo, 0x16, 0, 1, 1, 1)
8747
8748#undef GEN_INT_ARITH_DIVW
8749#define GEN_INT_ARITH_DIVW(name, opc3, sign, compute_ov) \
8750GEN_HANDLER(name, 0x1F, 0x0B, opc3, 0x00000000, PPC_INTEGER)
8751GEN_INT_ARITH_DIVW(divwu, 0x0E, 0, 0),
8752GEN_INT_ARITH_DIVW(divwuo, 0x1E, 0, 1),
8753GEN_INT_ARITH_DIVW(divw, 0x0F, 1, 0),
8754GEN_INT_ARITH_DIVW(divwo, 0x1F, 1, 1),
8755
8756#if defined(TARGET_PPC64)
8757#undef GEN_INT_ARITH_DIVD
8758#define GEN_INT_ARITH_DIVD(name, opc3, sign, compute_ov) \
8759GEN_HANDLER(name, 0x1F, 0x09, opc3, 0x00000000, PPC_64B)
8760GEN_INT_ARITH_DIVD(divdu, 0x0E, 0, 0),
8761GEN_INT_ARITH_DIVD(divduo, 0x1E, 0, 1),
8762GEN_INT_ARITH_DIVD(divd, 0x0F, 1, 0),
8763GEN_INT_ARITH_DIVD(divdo, 0x1F, 1, 1),
8764
8765#undef GEN_INT_ARITH_MUL_HELPER
8766#define GEN_INT_ARITH_MUL_HELPER(name, opc3) \
8767GEN_HANDLER(name, 0x1F, 0x09, opc3, 0x00000000, PPC_64B)
8768GEN_INT_ARITH_MUL_HELPER(mulhdu, 0x00),
8769GEN_INT_ARITH_MUL_HELPER(mulhd, 0x02),
8770GEN_INT_ARITH_MUL_HELPER(mulldo, 0x17),
8771#endif
8772
8773#undef GEN_INT_ARITH_SUBF
8774#undef GEN_INT_ARITH_SUBF_CONST
8775#define GEN_INT_ARITH_SUBF(name, opc3, add_ca, compute_ca, compute_ov) \
8776GEN_HANDLER(name, 0x1F, 0x08, opc3, 0x00000000, PPC_INTEGER),
8777#define GEN_INT_ARITH_SUBF_CONST(name, opc3, const_val, \
8778 add_ca, compute_ca, compute_ov) \
8779GEN_HANDLER(name, 0x1F, 0x08, opc3, 0x0000F800, PPC_INTEGER),
8780GEN_INT_ARITH_SUBF(subf, 0x01, 0, 0, 0)
8781GEN_INT_ARITH_SUBF(subfo, 0x11, 0, 0, 1)
8782GEN_INT_ARITH_SUBF(subfc, 0x00, 0, 1, 0)
8783GEN_INT_ARITH_SUBF(subfco, 0x10, 0, 1, 1)
8784GEN_INT_ARITH_SUBF(subfe, 0x04, 1, 1, 0)
8785GEN_INT_ARITH_SUBF(subfeo, 0x14, 1, 1, 1)
8786GEN_INT_ARITH_SUBF_CONST(subfme, 0x07, -1LL, 1, 1, 0)
8787GEN_INT_ARITH_SUBF_CONST(subfmeo, 0x17, -1LL, 1, 1, 1)
8788GEN_INT_ARITH_SUBF_CONST(subfze, 0x06, 0, 1, 1, 0)
8789GEN_INT_ARITH_SUBF_CONST(subfzeo, 0x16, 0, 1, 1, 1)
8790
8791#undef GEN_LOGICAL1
8792#undef GEN_LOGICAL2
8793#define GEN_LOGICAL2(name, tcg_op, opc, type) \
8794GEN_HANDLER(name, 0x1F, 0x1C, opc, 0x00000000, type)
8795#define GEN_LOGICAL1(name, tcg_op, opc, type) \
8796GEN_HANDLER(name, 0x1F, 0x1A, opc, 0x00000000, type)
8797GEN_LOGICAL2(and, tcg_gen_and_tl, 0x00, PPC_INTEGER),
8798GEN_LOGICAL2(andc, tcg_gen_andc_tl, 0x01, PPC_INTEGER),
8799GEN_LOGICAL2(eqv, tcg_gen_eqv_tl, 0x08, PPC_INTEGER),
8800GEN_LOGICAL1(extsb, tcg_gen_ext8s_tl, 0x1D, PPC_INTEGER),
8801GEN_LOGICAL1(extsh, tcg_gen_ext16s_tl, 0x1C, PPC_INTEGER),
8802GEN_LOGICAL2(nand, tcg_gen_nand_tl, 0x0E, PPC_INTEGER),
8803GEN_LOGICAL2(nor, tcg_gen_nor_tl, 0x03, PPC_INTEGER),
8804GEN_LOGICAL2(orc, tcg_gen_orc_tl, 0x0C, PPC_INTEGER),
8805#if defined(TARGET_PPC64)
8806GEN_LOGICAL1(extsw, tcg_gen_ext32s_tl, 0x1E, PPC_64B),
8807#endif
8808
8809#if defined(TARGET_PPC64)
8810#undef GEN_PPC64_R2
8811#undef GEN_PPC64_R4
8812#define GEN_PPC64_R2(name, opc1, opc2) \
8813GEN_HANDLER2(name##0, stringify(name), opc1, opc2, 0xFF, 0x00000000, PPC_64B),\
8814GEN_HANDLER2(name##1, stringify(name), opc1, opc2 | 0x10, 0xFF, 0x00000000, \
8815 PPC_64B)
8816#define GEN_PPC64_R4(name, opc1, opc2) \
8817GEN_HANDLER2(name##0, stringify(name), opc1, opc2, 0xFF, 0x00000000, PPC_64B),\
8818GEN_HANDLER2(name##1, stringify(name), opc1, opc2 | 0x01, 0xFF, 0x00000000, \
8819 PPC_64B), \
8820GEN_HANDLER2(name##2, stringify(name), opc1, opc2 | 0x10, 0xFF, 0x00000000, \
8821 PPC_64B), \
8822GEN_HANDLER2(name##3, stringify(name), opc1, opc2 | 0x11, 0xFF, 0x00000000, \
8823 PPC_64B)
8824GEN_PPC64_R4(rldicl, 0x1E, 0x00),
8825GEN_PPC64_R4(rldicr, 0x1E, 0x02),
8826GEN_PPC64_R4(rldic, 0x1E, 0x04),
8827GEN_PPC64_R2(rldcl, 0x1E, 0x08),
8828GEN_PPC64_R2(rldcr, 0x1E, 0x09),
8829GEN_PPC64_R4(rldimi, 0x1E, 0x06),
8830#endif
8831
8832#undef _GEN_FLOAT_ACB
8833#undef GEN_FLOAT_ACB
8834#undef _GEN_FLOAT_AB
8835#undef GEN_FLOAT_AB
8836#undef _GEN_FLOAT_AC
8837#undef GEN_FLOAT_AC
8838#undef GEN_FLOAT_B
8839#undef GEN_FLOAT_BS
8840#define _GEN_FLOAT_ACB(name, op, op1, op2, isfloat, set_fprf, type) \
8841GEN_HANDLER(f##name, op1, op2, 0xFF, 0x00000000, type)
8842#define GEN_FLOAT_ACB(name, op2, set_fprf, type) \
8843_GEN_FLOAT_ACB(name, name, 0x3F, op2, 0, set_fprf, type), \
8844_GEN_FLOAT_ACB(name##s, name, 0x3B, op2, 1, set_fprf, type)
8845#define _GEN_FLOAT_AB(name, op, op1, op2, inval, isfloat, set_fprf, type) \
8846GEN_HANDLER(f##name, op1, op2, 0xFF, inval, type)
8847#define GEN_FLOAT_AB(name, op2, inval, set_fprf, type) \
8848_GEN_FLOAT_AB(name, name, 0x3F, op2, inval, 0, set_fprf, type), \
8849_GEN_FLOAT_AB(name##s, name, 0x3B, op2, inval, 1, set_fprf, type)
8850#define _GEN_FLOAT_AC(name, op, op1, op2, inval, isfloat, set_fprf, type) \
8851GEN_HANDLER(f##name, op1, op2, 0xFF, inval, type)
8852#define GEN_FLOAT_AC(name, op2, inval, set_fprf, type) \
8853_GEN_FLOAT_AC(name, name, 0x3F, op2, inval, 0, set_fprf, type), \
8854_GEN_FLOAT_AC(name##s, name, 0x3B, op2, inval, 1, set_fprf, type)
8855#define GEN_FLOAT_B(name, op2, op3, set_fprf, type) \
8856GEN_HANDLER(f##name, 0x3F, op2, op3, 0x001F0000, type)
8857#define GEN_FLOAT_BS(name, op1, op2, set_fprf, type) \
8858GEN_HANDLER(f##name, op1, op2, 0xFF, 0x001F07C0, type)
8859
8860GEN_FLOAT_AB(add, 0x15, 0x000007C0, 1, PPC_FLOAT),
8861GEN_FLOAT_AB(div, 0x12, 0x000007C0, 1, PPC_FLOAT),
8862GEN_FLOAT_AC(mul, 0x19, 0x0000F800, 1, PPC_FLOAT),
8863GEN_FLOAT_BS(re, 0x3F, 0x18, 1, PPC_FLOAT_EXT),
8864GEN_FLOAT_BS(res, 0x3B, 0x18, 1, PPC_FLOAT_FRES),
8865GEN_FLOAT_BS(rsqrte, 0x3F, 0x1A, 1, PPC_FLOAT_FRSQRTE),
8866_GEN_FLOAT_ACB(sel, sel, 0x3F, 0x17, 0, 0, PPC_FLOAT_FSEL),
8867GEN_FLOAT_AB(sub, 0x14, 0x000007C0, 1, PPC_FLOAT),
8868GEN_FLOAT_ACB(madd, 0x1D, 1, PPC_FLOAT),
8869GEN_FLOAT_ACB(msub, 0x1C, 1, PPC_FLOAT),
8870GEN_FLOAT_ACB(nmadd, 0x1F, 1, PPC_FLOAT),
8871GEN_FLOAT_ACB(nmsub, 0x1E, 1, PPC_FLOAT),
8872GEN_FLOAT_B(ctiw, 0x0E, 0x00, 0, PPC_FLOAT),
8873GEN_FLOAT_B(ctiwz, 0x0F, 0x00, 0, PPC_FLOAT),
8874GEN_FLOAT_B(rsp, 0x0C, 0x00, 1, PPC_FLOAT),
8875#if defined(TARGET_PPC64)
8876GEN_FLOAT_B(cfid, 0x0E, 0x1A, 1, PPC_64B),
8877GEN_FLOAT_B(ctid, 0x0E, 0x19, 0, PPC_64B),
8878GEN_FLOAT_B(ctidz, 0x0F, 0x19, 0, PPC_64B),
8879#endif
8880GEN_FLOAT_B(rin, 0x08, 0x0C, 1, PPC_FLOAT_EXT),
8881GEN_FLOAT_B(riz, 0x08, 0x0D, 1, PPC_FLOAT_EXT),
8882GEN_FLOAT_B(rip, 0x08, 0x0E, 1, PPC_FLOAT_EXT),
8883GEN_FLOAT_B(rim, 0x08, 0x0F, 1, PPC_FLOAT_EXT),
5c55ff99
BS
8884
8885#undef GEN_LD
8886#undef GEN_LDU
8887#undef GEN_LDUX
cd6e9320 8888#undef GEN_LDX_E
5c55ff99
BS
8889#undef GEN_LDS
8890#define GEN_LD(name, ldop, opc, type) \
8891GEN_HANDLER(name, opc, 0xFF, 0xFF, 0x00000000, type),
8892#define GEN_LDU(name, ldop, opc, type) \
8893GEN_HANDLER(name##u, opc, 0xFF, 0xFF, 0x00000000, type),
8894#define GEN_LDUX(name, ldop, opc2, opc3, type) \
8895GEN_HANDLER(name##ux, 0x1F, opc2, opc3, 0x00000001, type),
cd6e9320
TH
8896#define GEN_LDX_E(name, ldop, opc2, opc3, type, type2) \
8897GEN_HANDLER_E(name##x, 0x1F, opc2, opc3, 0x00000001, type, type2),
5c55ff99
BS
8898#define GEN_LDS(name, ldop, op, type) \
8899GEN_LD(name, ldop, op | 0x20, type) \
8900GEN_LDU(name, ldop, op | 0x21, type) \
8901GEN_LDUX(name, ldop, 0x17, op | 0x01, type) \
8902GEN_LDX(name, ldop, 0x17, op | 0x00, type)
8903
8904GEN_LDS(lbz, ld8u, 0x02, PPC_INTEGER)
8905GEN_LDS(lha, ld16s, 0x0A, PPC_INTEGER)
8906GEN_LDS(lhz, ld16u, 0x08, PPC_INTEGER)
8907GEN_LDS(lwz, ld32u, 0x00, PPC_INTEGER)
8908#if defined(TARGET_PPC64)
8909GEN_LDUX(lwa, ld32s, 0x15, 0x0B, PPC_64B)
8910GEN_LDX(lwa, ld32s, 0x15, 0x0A, PPC_64B)
8911GEN_LDUX(ld, ld64, 0x15, 0x01, PPC_64B)
8912GEN_LDX(ld, ld64, 0x15, 0x00, PPC_64B)
cd6e9320 8913GEN_LDX_E(ldbr, ld64ur, 0x14, 0x10, PPC_NONE, PPC2_DBRX)
5c55ff99
BS
8914#endif
8915GEN_LDX(lhbr, ld16ur, 0x16, 0x18, PPC_INTEGER)
8916GEN_LDX(lwbr, ld32ur, 0x16, 0x10, PPC_INTEGER)
8917
8918#undef GEN_ST
8919#undef GEN_STU
8920#undef GEN_STUX
cd6e9320 8921#undef GEN_STX_E
5c55ff99
BS
8922#undef GEN_STS
8923#define GEN_ST(name, stop, opc, type) \
8924GEN_HANDLER(name, opc, 0xFF, 0xFF, 0x00000000, type),
8925#define GEN_STU(name, stop, opc, type) \
8926GEN_HANDLER(stop##u, opc, 0xFF, 0xFF, 0x00000000, type),
8927#define GEN_STUX(name, stop, opc2, opc3, type) \
8928GEN_HANDLER(name##ux, 0x1F, opc2, opc3, 0x00000001, type),
cd6e9320
TH
8929#define GEN_STX_E(name, stop, opc2, opc3, type, type2) \
8930GEN_HANDLER_E(name##x, 0x1F, opc2, opc3, 0x00000001, type, type2),
5c55ff99
BS
8931#define GEN_STS(name, stop, op, type) \
8932GEN_ST(name, stop, op | 0x20, type) \
8933GEN_STU(name, stop, op | 0x21, type) \
8934GEN_STUX(name, stop, 0x17, op | 0x01, type) \
8935GEN_STX(name, stop, 0x17, op | 0x00, type)
8936
8937GEN_STS(stb, st8, 0x06, PPC_INTEGER)
8938GEN_STS(sth, st16, 0x0C, PPC_INTEGER)
8939GEN_STS(stw, st32, 0x04, PPC_INTEGER)
8940#if defined(TARGET_PPC64)
8941GEN_STUX(std, st64, 0x15, 0x05, PPC_64B)
8942GEN_STX(std, st64, 0x15, 0x04, PPC_64B)
cd6e9320 8943GEN_STX_E(stdbr, st64r, 0x14, 0x14, PPC_NONE, PPC2_DBRX)
5c55ff99
BS
8944#endif
8945GEN_STX(sthbr, st16r, 0x16, 0x1C, PPC_INTEGER)
8946GEN_STX(stwbr, st32r, 0x16, 0x14, PPC_INTEGER)
8947
8948#undef GEN_LDF
8949#undef GEN_LDUF
8950#undef GEN_LDUXF
8951#undef GEN_LDXF
8952#undef GEN_LDFS
8953#define GEN_LDF(name, ldop, opc, type) \
8954GEN_HANDLER(name, opc, 0xFF, 0xFF, 0x00000000, type),
8955#define GEN_LDUF(name, ldop, opc, type) \
8956GEN_HANDLER(name##u, opc, 0xFF, 0xFF, 0x00000000, type),
8957#define GEN_LDUXF(name, ldop, opc, type) \
8958GEN_HANDLER(name##ux, 0x1F, 0x17, opc, 0x00000001, type),
8959#define GEN_LDXF(name, ldop, opc2, opc3, type) \
8960GEN_HANDLER(name##x, 0x1F, opc2, opc3, 0x00000001, type),
8961#define GEN_LDFS(name, ldop, op, type) \
8962GEN_LDF(name, ldop, op | 0x20, type) \
8963GEN_LDUF(name, ldop, op | 0x21, type) \
8964GEN_LDUXF(name, ldop, op | 0x01, type) \
8965GEN_LDXF(name, ldop, 0x17, op | 0x00, type)
8966
8967GEN_LDFS(lfd, ld64, 0x12, PPC_FLOAT)
8968GEN_LDFS(lfs, ld32fs, 0x10, PPC_FLOAT)
8969
8970#undef GEN_STF
8971#undef GEN_STUF
8972#undef GEN_STUXF
8973#undef GEN_STXF
8974#undef GEN_STFS
8975#define GEN_STF(name, stop, opc, type) \
8976GEN_HANDLER(name, opc, 0xFF, 0xFF, 0x00000000, type),
8977#define GEN_STUF(name, stop, opc, type) \
8978GEN_HANDLER(name##u, opc, 0xFF, 0xFF, 0x00000000, type),
8979#define GEN_STUXF(name, stop, opc, type) \
8980GEN_HANDLER(name##ux, 0x1F, 0x17, opc, 0x00000001, type),
8981#define GEN_STXF(name, stop, opc2, opc3, type) \
8982GEN_HANDLER(name##x, 0x1F, opc2, opc3, 0x00000001, type),
8983#define GEN_STFS(name, stop, op, type) \
8984GEN_STF(name, stop, op | 0x20, type) \
8985GEN_STUF(name, stop, op | 0x21, type) \
8986GEN_STUXF(name, stop, op | 0x01, type) \
8987GEN_STXF(name, stop, 0x17, op | 0x00, type)
8988
8989GEN_STFS(stfd, st64, 0x16, PPC_FLOAT)
8990GEN_STFS(stfs, st32fs, 0x14, PPC_FLOAT)
8991GEN_STXF(stfiw, st32fiw, 0x17, 0x1E, PPC_FLOAT_STFIWX)
8992
8993#undef GEN_CRLOGIC
8994#define GEN_CRLOGIC(name, tcg_op, opc) \
8995GEN_HANDLER(name, 0x13, 0x01, opc, 0x00000001, PPC_INTEGER)
8996GEN_CRLOGIC(crand, tcg_gen_and_i32, 0x08),
8997GEN_CRLOGIC(crandc, tcg_gen_andc_i32, 0x04),
8998GEN_CRLOGIC(creqv, tcg_gen_eqv_i32, 0x09),
8999GEN_CRLOGIC(crnand, tcg_gen_nand_i32, 0x07),
9000GEN_CRLOGIC(crnor, tcg_gen_nor_i32, 0x01),
9001GEN_CRLOGIC(cror, tcg_gen_or_i32, 0x0E),
9002GEN_CRLOGIC(crorc, tcg_gen_orc_i32, 0x0D),
9003GEN_CRLOGIC(crxor, tcg_gen_xor_i32, 0x06),
9004
9005#undef GEN_MAC_HANDLER
9006#define GEN_MAC_HANDLER(name, opc2, opc3) \
9007GEN_HANDLER(name, 0x04, opc2, opc3, 0x00000000, PPC_405_MAC)
9008GEN_MAC_HANDLER(macchw, 0x0C, 0x05),
9009GEN_MAC_HANDLER(macchwo, 0x0C, 0x15),
9010GEN_MAC_HANDLER(macchws, 0x0C, 0x07),
9011GEN_MAC_HANDLER(macchwso, 0x0C, 0x17),
9012GEN_MAC_HANDLER(macchwsu, 0x0C, 0x06),
9013GEN_MAC_HANDLER(macchwsuo, 0x0C, 0x16),
9014GEN_MAC_HANDLER(macchwu, 0x0C, 0x04),
9015GEN_MAC_HANDLER(macchwuo, 0x0C, 0x14),
9016GEN_MAC_HANDLER(machhw, 0x0C, 0x01),
9017GEN_MAC_HANDLER(machhwo, 0x0C, 0x11),
9018GEN_MAC_HANDLER(machhws, 0x0C, 0x03),
9019GEN_MAC_HANDLER(machhwso, 0x0C, 0x13),
9020GEN_MAC_HANDLER(machhwsu, 0x0C, 0x02),
9021GEN_MAC_HANDLER(machhwsuo, 0x0C, 0x12),
9022GEN_MAC_HANDLER(machhwu, 0x0C, 0x00),
9023GEN_MAC_HANDLER(machhwuo, 0x0C, 0x10),
9024GEN_MAC_HANDLER(maclhw, 0x0C, 0x0D),
9025GEN_MAC_HANDLER(maclhwo, 0x0C, 0x1D),
9026GEN_MAC_HANDLER(maclhws, 0x0C, 0x0F),
9027GEN_MAC_HANDLER(maclhwso, 0x0C, 0x1F),
9028GEN_MAC_HANDLER(maclhwu, 0x0C, 0x0C),
9029GEN_MAC_HANDLER(maclhwuo, 0x0C, 0x1C),
9030GEN_MAC_HANDLER(maclhwsu, 0x0C, 0x0E),
9031GEN_MAC_HANDLER(maclhwsuo, 0x0C, 0x1E),
9032GEN_MAC_HANDLER(nmacchw, 0x0E, 0x05),
9033GEN_MAC_HANDLER(nmacchwo, 0x0E, 0x15),
9034GEN_MAC_HANDLER(nmacchws, 0x0E, 0x07),
9035GEN_MAC_HANDLER(nmacchwso, 0x0E, 0x17),
9036GEN_MAC_HANDLER(nmachhw, 0x0E, 0x01),
9037GEN_MAC_HANDLER(nmachhwo, 0x0E, 0x11),
9038GEN_MAC_HANDLER(nmachhws, 0x0E, 0x03),
9039GEN_MAC_HANDLER(nmachhwso, 0x0E, 0x13),
9040GEN_MAC_HANDLER(nmaclhw, 0x0E, 0x0D),
9041GEN_MAC_HANDLER(nmaclhwo, 0x0E, 0x1D),
9042GEN_MAC_HANDLER(nmaclhws, 0x0E, 0x0F),
9043GEN_MAC_HANDLER(nmaclhwso, 0x0E, 0x1F),
9044GEN_MAC_HANDLER(mulchw, 0x08, 0x05),
9045GEN_MAC_HANDLER(mulchwu, 0x08, 0x04),
9046GEN_MAC_HANDLER(mulhhw, 0x08, 0x01),
9047GEN_MAC_HANDLER(mulhhwu, 0x08, 0x00),
9048GEN_MAC_HANDLER(mullhw, 0x08, 0x0D),
9049GEN_MAC_HANDLER(mullhwu, 0x08, 0x0C),
9050
9051#undef GEN_VR_LDX
9052#undef GEN_VR_STX
9053#undef GEN_VR_LVE
9054#undef GEN_VR_STVE
9055#define GEN_VR_LDX(name, opc2, opc3) \
9056GEN_HANDLER(name, 0x1F, opc2, opc3, 0x00000001, PPC_ALTIVEC)
9057#define GEN_VR_STX(name, opc2, opc3) \
9058GEN_HANDLER(st##name, 0x1F, opc2, opc3, 0x00000001, PPC_ALTIVEC)
9059#define GEN_VR_LVE(name, opc2, opc3) \
9060 GEN_HANDLER(lve##name, 0x1F, opc2, opc3, 0x00000001, PPC_ALTIVEC)
9061#define GEN_VR_STVE(name, opc2, opc3) \
9062 GEN_HANDLER(stve##name, 0x1F, opc2, opc3, 0x00000001, PPC_ALTIVEC)
9063GEN_VR_LDX(lvx, 0x07, 0x03),
9064GEN_VR_LDX(lvxl, 0x07, 0x0B),
9065GEN_VR_LVE(bx, 0x07, 0x00),
9066GEN_VR_LVE(hx, 0x07, 0x01),
9067GEN_VR_LVE(wx, 0x07, 0x02),
9068GEN_VR_STX(svx, 0x07, 0x07),
9069GEN_VR_STX(svxl, 0x07, 0x0F),
9070GEN_VR_STVE(bx, 0x07, 0x04),
9071GEN_VR_STVE(hx, 0x07, 0x05),
9072GEN_VR_STVE(wx, 0x07, 0x06),
9073
9074#undef GEN_VX_LOGICAL
9075#define GEN_VX_LOGICAL(name, tcg_op, opc2, opc3) \
9076GEN_HANDLER(name, 0x04, opc2, opc3, 0x00000000, PPC_ALTIVEC)
9077GEN_VX_LOGICAL(vand, tcg_gen_and_i64, 2, 16),
9078GEN_VX_LOGICAL(vandc, tcg_gen_andc_i64, 2, 17),
9079GEN_VX_LOGICAL(vor, tcg_gen_or_i64, 2, 18),
9080GEN_VX_LOGICAL(vxor, tcg_gen_xor_i64, 2, 19),
9081GEN_VX_LOGICAL(vnor, tcg_gen_nor_i64, 2, 20),
9082
9083#undef GEN_VXFORM
9084#define GEN_VXFORM(name, opc2, opc3) \
9085GEN_HANDLER(name, 0x04, opc2, opc3, 0x00000000, PPC_ALTIVEC)
9086GEN_VXFORM(vaddubm, 0, 0),
9087GEN_VXFORM(vadduhm, 0, 1),
9088GEN_VXFORM(vadduwm, 0, 2),
9089GEN_VXFORM(vsububm, 0, 16),
9090GEN_VXFORM(vsubuhm, 0, 17),
9091GEN_VXFORM(vsubuwm, 0, 18),
9092GEN_VXFORM(vmaxub, 1, 0),
9093GEN_VXFORM(vmaxuh, 1, 1),
9094GEN_VXFORM(vmaxuw, 1, 2),
9095GEN_VXFORM(vmaxsb, 1, 4),
9096GEN_VXFORM(vmaxsh, 1, 5),
9097GEN_VXFORM(vmaxsw, 1, 6),
9098GEN_VXFORM(vminub, 1, 8),
9099GEN_VXFORM(vminuh, 1, 9),
9100GEN_VXFORM(vminuw, 1, 10),
9101GEN_VXFORM(vminsb, 1, 12),
9102GEN_VXFORM(vminsh, 1, 13),
9103GEN_VXFORM(vminsw, 1, 14),
9104GEN_VXFORM(vavgub, 1, 16),
9105GEN_VXFORM(vavguh, 1, 17),
9106GEN_VXFORM(vavguw, 1, 18),
9107GEN_VXFORM(vavgsb, 1, 20),
9108GEN_VXFORM(vavgsh, 1, 21),
9109GEN_VXFORM(vavgsw, 1, 22),
9110GEN_VXFORM(vmrghb, 6, 0),
9111GEN_VXFORM(vmrghh, 6, 1),
9112GEN_VXFORM(vmrghw, 6, 2),
9113GEN_VXFORM(vmrglb, 6, 4),
9114GEN_VXFORM(vmrglh, 6, 5),
9115GEN_VXFORM(vmrglw, 6, 6),
9116GEN_VXFORM(vmuloub, 4, 0),
9117GEN_VXFORM(vmulouh, 4, 1),
9118GEN_VXFORM(vmulosb, 4, 4),
9119GEN_VXFORM(vmulosh, 4, 5),
9120GEN_VXFORM(vmuleub, 4, 8),
9121GEN_VXFORM(vmuleuh, 4, 9),
9122GEN_VXFORM(vmulesb, 4, 12),
9123GEN_VXFORM(vmulesh, 4, 13),
9124GEN_VXFORM(vslb, 2, 4),
9125GEN_VXFORM(vslh, 2, 5),
9126GEN_VXFORM(vslw, 2, 6),
9127GEN_VXFORM(vsrb, 2, 8),
9128GEN_VXFORM(vsrh, 2, 9),
9129GEN_VXFORM(vsrw, 2, 10),
9130GEN_VXFORM(vsrab, 2, 12),
9131GEN_VXFORM(vsrah, 2, 13),
9132GEN_VXFORM(vsraw, 2, 14),
9133GEN_VXFORM(vslo, 6, 16),
9134GEN_VXFORM(vsro, 6, 17),
9135GEN_VXFORM(vaddcuw, 0, 6),
9136GEN_VXFORM(vsubcuw, 0, 22),
9137GEN_VXFORM(vaddubs, 0, 8),
9138GEN_VXFORM(vadduhs, 0, 9),
9139GEN_VXFORM(vadduws, 0, 10),
9140GEN_VXFORM(vaddsbs, 0, 12),
9141GEN_VXFORM(vaddshs, 0, 13),
9142GEN_VXFORM(vaddsws, 0, 14),
9143GEN_VXFORM(vsububs, 0, 24),
9144GEN_VXFORM(vsubuhs, 0, 25),
9145GEN_VXFORM(vsubuws, 0, 26),
9146GEN_VXFORM(vsubsbs, 0, 28),
9147GEN_VXFORM(vsubshs, 0, 29),
9148GEN_VXFORM(vsubsws, 0, 30),
9149GEN_VXFORM(vrlb, 2, 0),
9150GEN_VXFORM(vrlh, 2, 1),
9151GEN_VXFORM(vrlw, 2, 2),
9152GEN_VXFORM(vsl, 2, 7),
9153GEN_VXFORM(vsr, 2, 11),
9154GEN_VXFORM(vpkuhum, 7, 0),
9155GEN_VXFORM(vpkuwum, 7, 1),
9156GEN_VXFORM(vpkuhus, 7, 2),
9157GEN_VXFORM(vpkuwus, 7, 3),
9158GEN_VXFORM(vpkshus, 7, 4),
9159GEN_VXFORM(vpkswus, 7, 5),
9160GEN_VXFORM(vpkshss, 7, 6),
9161GEN_VXFORM(vpkswss, 7, 7),
9162GEN_VXFORM(vpkpx, 7, 12),
9163GEN_VXFORM(vsum4ubs, 4, 24),
9164GEN_VXFORM(vsum4sbs, 4, 28),
9165GEN_VXFORM(vsum4shs, 4, 25),
9166GEN_VXFORM(vsum2sws, 4, 26),
9167GEN_VXFORM(vsumsws, 4, 30),
9168GEN_VXFORM(vaddfp, 5, 0),
9169GEN_VXFORM(vsubfp, 5, 1),
9170GEN_VXFORM(vmaxfp, 5, 16),
9171GEN_VXFORM(vminfp, 5, 17),
9172
9173#undef GEN_VXRFORM1
9174#undef GEN_VXRFORM
9175#define GEN_VXRFORM1(opname, name, str, opc2, opc3) \
9176 GEN_HANDLER2(name, str, 0x4, opc2, opc3, 0x00000000, PPC_ALTIVEC),
9177#define GEN_VXRFORM(name, opc2, opc3) \
9178 GEN_VXRFORM1(name, name, #name, opc2, opc3) \
9179 GEN_VXRFORM1(name##_dot, name##_, #name ".", opc2, (opc3 | (0x1 << 4)))
9180GEN_VXRFORM(vcmpequb, 3, 0)
9181GEN_VXRFORM(vcmpequh, 3, 1)
9182GEN_VXRFORM(vcmpequw, 3, 2)
9183GEN_VXRFORM(vcmpgtsb, 3, 12)
9184GEN_VXRFORM(vcmpgtsh, 3, 13)
9185GEN_VXRFORM(vcmpgtsw, 3, 14)
9186GEN_VXRFORM(vcmpgtub, 3, 8)
9187GEN_VXRFORM(vcmpgtuh, 3, 9)
9188GEN_VXRFORM(vcmpgtuw, 3, 10)
9189GEN_VXRFORM(vcmpeqfp, 3, 3)
9190GEN_VXRFORM(vcmpgefp, 3, 7)
9191GEN_VXRFORM(vcmpgtfp, 3, 11)
9192GEN_VXRFORM(vcmpbfp, 3, 15)
9193
9194#undef GEN_VXFORM_SIMM
9195#define GEN_VXFORM_SIMM(name, opc2, opc3) \
9196 GEN_HANDLER(name, 0x04, opc2, opc3, 0x00000000, PPC_ALTIVEC)
9197GEN_VXFORM_SIMM(vspltisb, 6, 12),
9198GEN_VXFORM_SIMM(vspltish, 6, 13),
9199GEN_VXFORM_SIMM(vspltisw, 6, 14),
9200
9201#undef GEN_VXFORM_NOA
9202#define GEN_VXFORM_NOA(name, opc2, opc3) \
9203 GEN_HANDLER(name, 0x04, opc2, opc3, 0x001f0000, PPC_ALTIVEC)
9204GEN_VXFORM_NOA(vupkhsb, 7, 8),
9205GEN_VXFORM_NOA(vupkhsh, 7, 9),
9206GEN_VXFORM_NOA(vupklsb, 7, 10),
9207GEN_VXFORM_NOA(vupklsh, 7, 11),
9208GEN_VXFORM_NOA(vupkhpx, 7, 13),
9209GEN_VXFORM_NOA(vupklpx, 7, 15),
9210GEN_VXFORM_NOA(vrefp, 5, 4),
9211GEN_VXFORM_NOA(vrsqrtefp, 5, 5),
0bffbc6c 9212GEN_VXFORM_NOA(vexptefp, 5, 6),
5c55ff99
BS
9213GEN_VXFORM_NOA(vlogefp, 5, 7),
9214GEN_VXFORM_NOA(vrfim, 5, 8),
9215GEN_VXFORM_NOA(vrfin, 5, 9),
9216GEN_VXFORM_NOA(vrfip, 5, 10),
9217GEN_VXFORM_NOA(vrfiz, 5, 11),
9218
9219#undef GEN_VXFORM_UIMM
9220#define GEN_VXFORM_UIMM(name, opc2, opc3) \
9221 GEN_HANDLER(name, 0x04, opc2, opc3, 0x00000000, PPC_ALTIVEC)
9222GEN_VXFORM_UIMM(vspltb, 6, 8),
9223GEN_VXFORM_UIMM(vsplth, 6, 9),
9224GEN_VXFORM_UIMM(vspltw, 6, 10),
9225GEN_VXFORM_UIMM(vcfux, 5, 12),
9226GEN_VXFORM_UIMM(vcfsx, 5, 13),
9227GEN_VXFORM_UIMM(vctuxs, 5, 14),
9228GEN_VXFORM_UIMM(vctsxs, 5, 15),
9229
9230#undef GEN_VAFORM_PAIRED
9231#define GEN_VAFORM_PAIRED(name0, name1, opc2) \
9232 GEN_HANDLER(name0##_##name1, 0x04, opc2, 0xFF, 0x00000000, PPC_ALTIVEC)
9233GEN_VAFORM_PAIRED(vmhaddshs, vmhraddshs, 16),
9234GEN_VAFORM_PAIRED(vmsumubm, vmsummbm, 18),
9235GEN_VAFORM_PAIRED(vmsumuhm, vmsumuhs, 19),
9236GEN_VAFORM_PAIRED(vmsumshm, vmsumshs, 20),
9237GEN_VAFORM_PAIRED(vsel, vperm, 21),
9238GEN_VAFORM_PAIRED(vmaddfp, vnmsubfp, 23),
9239
9240#undef GEN_SPE
70560da7
FC
9241#define GEN_SPE(name0, name1, opc2, opc3, inval0, inval1, type) \
9242 GEN_OPCODE_DUAL(name0##_##name1, 0x04, opc2, opc3, inval0, inval1, type, PPC_NONE)
9243GEN_SPE(evaddw, speundef, 0x00, 0x08, 0x00000000, 0xFFFFFFFF, PPC_SPE),
9244GEN_SPE(evaddiw, speundef, 0x01, 0x08, 0x00000000, 0xFFFFFFFF, PPC_SPE),
9245GEN_SPE(evsubfw, speundef, 0x02, 0x08, 0x00000000, 0xFFFFFFFF, PPC_SPE),
9246GEN_SPE(evsubifw, speundef, 0x03, 0x08, 0x00000000, 0xFFFFFFFF, PPC_SPE),
9247GEN_SPE(evabs, evneg, 0x04, 0x08, 0x0000F800, 0x0000F800, PPC_SPE),
9248GEN_SPE(evextsb, evextsh, 0x05, 0x08, 0x0000F800, 0x0000F800, PPC_SPE),
9249GEN_SPE(evrndw, evcntlzw, 0x06, 0x08, 0x0000F800, 0x0000F800, PPC_SPE),
9250GEN_SPE(evcntlsw, brinc, 0x07, 0x08, 0x0000F800, 0x00000000, PPC_SPE),
9251GEN_SPE(evmra, speundef, 0x02, 0x13, 0x0000F800, 0xFFFFFFFF, PPC_SPE),
9252GEN_SPE(speundef, evand, 0x08, 0x08, 0xFFFFFFFF, 0x00000000, PPC_SPE),
9253GEN_SPE(evandc, speundef, 0x09, 0x08, 0x00000000, 0xFFFFFFFF, PPC_SPE),
9254GEN_SPE(evxor, evor, 0x0B, 0x08, 0x00000000, 0x00000000, PPC_SPE),
9255GEN_SPE(evnor, eveqv, 0x0C, 0x08, 0x00000000, 0x00000000, PPC_SPE),
9256GEN_SPE(evmwumi, evmwsmi, 0x0C, 0x11, 0x00000000, 0x00000000, PPC_SPE),
9257GEN_SPE(evmwumia, evmwsmia, 0x1C, 0x11, 0x00000000, 0x00000000, PPC_SPE),
9258GEN_SPE(evmwumiaa, evmwsmiaa, 0x0C, 0x15, 0x00000000, 0x00000000, PPC_SPE),
9259GEN_SPE(speundef, evorc, 0x0D, 0x08, 0xFFFFFFFF, 0x00000000, PPC_SPE),
9260GEN_SPE(evnand, speundef, 0x0F, 0x08, 0x00000000, 0xFFFFFFFF, PPC_SPE),
9261GEN_SPE(evsrwu, evsrws, 0x10, 0x08, 0x00000000, 0x00000000, PPC_SPE),
9262GEN_SPE(evsrwiu, evsrwis, 0x11, 0x08, 0x00000000, 0x00000000, PPC_SPE),
9263GEN_SPE(evslw, speundef, 0x12, 0x08, 0x00000000, 0xFFFFFFFF, PPC_SPE),
9264GEN_SPE(evslwi, speundef, 0x13, 0x08, 0x00000000, 0xFFFFFFFF, PPC_SPE),
9265GEN_SPE(evrlw, evsplati, 0x14, 0x08, 0x00000000, 0x0000F800, PPC_SPE),
9266GEN_SPE(evrlwi, evsplatfi, 0x15, 0x08, 0x00000000, 0x0000F800, PPC_SPE),
9267GEN_SPE(evmergehi, evmergelo, 0x16, 0x08, 0x00000000, 0x00000000, PPC_SPE),
9268GEN_SPE(evmergehilo, evmergelohi, 0x17, 0x08, 0x00000000, 0x00000000, PPC_SPE),
9269GEN_SPE(evcmpgtu, evcmpgts, 0x18, 0x08, 0x00600000, 0x00600000, PPC_SPE),
9270GEN_SPE(evcmpltu, evcmplts, 0x19, 0x08, 0x00600000, 0x00600000, PPC_SPE),
9271GEN_SPE(evcmpeq, speundef, 0x1A, 0x08, 0x00600000, 0xFFFFFFFF, PPC_SPE),
9272
9273GEN_SPE(evfsadd, evfssub, 0x00, 0x0A, 0x00000000, 0x00000000, PPC_SPE_SINGLE),
9274GEN_SPE(evfsabs, evfsnabs, 0x02, 0x0A, 0x0000F800, 0x0000F800, PPC_SPE_SINGLE),
9275GEN_SPE(evfsneg, speundef, 0x03, 0x0A, 0x0000F800, 0xFFFFFFFF, PPC_SPE_SINGLE),
9276GEN_SPE(evfsmul, evfsdiv, 0x04, 0x0A, 0x00000000, 0x00000000, PPC_SPE_SINGLE),
9277GEN_SPE(evfscmpgt, evfscmplt, 0x06, 0x0A, 0x00600000, 0x00600000, PPC_SPE_SINGLE),
9278GEN_SPE(evfscmpeq, speundef, 0x07, 0x0A, 0x00600000, 0xFFFFFFFF, PPC_SPE_SINGLE),
9279GEN_SPE(evfscfui, evfscfsi, 0x08, 0x0A, 0x00180000, 0x00180000, PPC_SPE_SINGLE),
9280GEN_SPE(evfscfuf, evfscfsf, 0x09, 0x0A, 0x00180000, 0x00180000, PPC_SPE_SINGLE),
9281GEN_SPE(evfsctui, evfsctsi, 0x0A, 0x0A, 0x00180000, 0x00180000, PPC_SPE_SINGLE),
9282GEN_SPE(evfsctuf, evfsctsf, 0x0B, 0x0A, 0x00180000, 0x00180000, PPC_SPE_SINGLE),
9283GEN_SPE(evfsctuiz, speundef, 0x0C, 0x0A, 0x00180000, 0xFFFFFFFF, PPC_SPE_SINGLE),
9284GEN_SPE(evfsctsiz, speundef, 0x0D, 0x0A, 0x00180000, 0xFFFFFFFF, PPC_SPE_SINGLE),
9285GEN_SPE(evfststgt, evfststlt, 0x0E, 0x0A, 0x00600000, 0x00600000, PPC_SPE_SINGLE),
9286GEN_SPE(evfststeq, speundef, 0x0F, 0x0A, 0x00600000, 0xFFFFFFFF, PPC_SPE_SINGLE),
9287
9288GEN_SPE(efsadd, efssub, 0x00, 0x0B, 0x00000000, 0x00000000, PPC_SPE_SINGLE),
9289GEN_SPE(efsabs, efsnabs, 0x02, 0x0B, 0x0000F800, 0x0000F800, PPC_SPE_SINGLE),
9290GEN_SPE(efsneg, speundef, 0x03, 0x0B, 0x0000F800, 0xFFFFFFFF, PPC_SPE_SINGLE),
9291GEN_SPE(efsmul, efsdiv, 0x04, 0x0B, 0x00000000, 0x00000000, PPC_SPE_SINGLE),
9292GEN_SPE(efscmpgt, efscmplt, 0x06, 0x0B, 0x00600000, 0x00600000, PPC_SPE_SINGLE),
9293GEN_SPE(efscmpeq, efscfd, 0x07, 0x0B, 0x00600000, 0x00180000, PPC_SPE_SINGLE),
9294GEN_SPE(efscfui, efscfsi, 0x08, 0x0B, 0x00180000, 0x00180000, PPC_SPE_SINGLE),
9295GEN_SPE(efscfuf, efscfsf, 0x09, 0x0B, 0x00180000, 0x00180000, PPC_SPE_SINGLE),
9296GEN_SPE(efsctui, efsctsi, 0x0A, 0x0B, 0x00180000, 0x00180000, PPC_SPE_SINGLE),
9297GEN_SPE(efsctuf, efsctsf, 0x0B, 0x0B, 0x00180000, 0x00180000, PPC_SPE_SINGLE),
9298GEN_SPE(efsctuiz, speundef, 0x0C, 0x0B, 0x00180000, 0xFFFFFFFF, PPC_SPE_SINGLE),
9299GEN_SPE(efsctsiz, speundef, 0x0D, 0x0B, 0x00180000, 0xFFFFFFFF, PPC_SPE_SINGLE),
9300GEN_SPE(efststgt, efststlt, 0x0E, 0x0B, 0x00600000, 0x00600000, PPC_SPE_SINGLE),
9301GEN_SPE(efststeq, speundef, 0x0F, 0x0B, 0x00600000, 0xFFFFFFFF, PPC_SPE_SINGLE),
9302
9303GEN_SPE(efdadd, efdsub, 0x10, 0x0B, 0x00000000, 0x00000000, PPC_SPE_DOUBLE),
9304GEN_SPE(efdcfuid, efdcfsid, 0x11, 0x0B, 0x00180000, 0x00180000, PPC_SPE_DOUBLE),
9305GEN_SPE(efdabs, efdnabs, 0x12, 0x0B, 0x0000F800, 0x0000F800, PPC_SPE_DOUBLE),
9306GEN_SPE(efdneg, speundef, 0x13, 0x0B, 0x0000F800, 0xFFFFFFFF, PPC_SPE_DOUBLE),
9307GEN_SPE(efdmul, efddiv, 0x14, 0x0B, 0x00000000, 0x00000000, PPC_SPE_DOUBLE),
9308GEN_SPE(efdctuidz, efdctsidz, 0x15, 0x0B, 0x00180000, 0x00180000, PPC_SPE_DOUBLE),
9309GEN_SPE(efdcmpgt, efdcmplt, 0x16, 0x0B, 0x00600000, 0x00600000, PPC_SPE_DOUBLE),
9310GEN_SPE(efdcmpeq, efdcfs, 0x17, 0x0B, 0x00600000, 0x00180000, PPC_SPE_DOUBLE),
9311GEN_SPE(efdcfui, efdcfsi, 0x18, 0x0B, 0x00180000, 0x00180000, PPC_SPE_DOUBLE),
9312GEN_SPE(efdcfuf, efdcfsf, 0x19, 0x0B, 0x00180000, 0x00180000, PPC_SPE_DOUBLE),
9313GEN_SPE(efdctui, efdctsi, 0x1A, 0x0B, 0x00180000, 0x00180000, PPC_SPE_DOUBLE),
9314GEN_SPE(efdctuf, efdctsf, 0x1B, 0x0B, 0x00180000, 0x00180000, PPC_SPE_DOUBLE),
9315GEN_SPE(efdctuiz, speundef, 0x1C, 0x0B, 0x00180000, 0xFFFFFFFF, PPC_SPE_DOUBLE),
9316GEN_SPE(efdctsiz, speundef, 0x1D, 0x0B, 0x00180000, 0xFFFFFFFF, PPC_SPE_DOUBLE),
9317GEN_SPE(efdtstgt, efdtstlt, 0x1E, 0x0B, 0x00600000, 0x00600000, PPC_SPE_DOUBLE),
9318GEN_SPE(efdtsteq, speundef, 0x1F, 0x0B, 0x00600000, 0xFFFFFFFF, PPC_SPE_DOUBLE),
5c55ff99
BS
9319
9320#undef GEN_SPEOP_LDST
9321#define GEN_SPEOP_LDST(name, opc2, sh) \
9322GEN_HANDLER(name, 0x04, opc2, 0x0C, 0x00000000, PPC_SPE)
9323GEN_SPEOP_LDST(evldd, 0x00, 3),
9324GEN_SPEOP_LDST(evldw, 0x01, 3),
9325GEN_SPEOP_LDST(evldh, 0x02, 3),
9326GEN_SPEOP_LDST(evlhhesplat, 0x04, 1),
9327GEN_SPEOP_LDST(evlhhousplat, 0x06, 1),
9328GEN_SPEOP_LDST(evlhhossplat, 0x07, 1),
9329GEN_SPEOP_LDST(evlwhe, 0x08, 2),
9330GEN_SPEOP_LDST(evlwhou, 0x0A, 2),
9331GEN_SPEOP_LDST(evlwhos, 0x0B, 2),
9332GEN_SPEOP_LDST(evlwwsplat, 0x0C, 2),
9333GEN_SPEOP_LDST(evlwhsplat, 0x0E, 2),
9334
9335GEN_SPEOP_LDST(evstdd, 0x10, 3),
9336GEN_SPEOP_LDST(evstdw, 0x11, 3),
9337GEN_SPEOP_LDST(evstdh, 0x12, 3),
9338GEN_SPEOP_LDST(evstwhe, 0x18, 2),
9339GEN_SPEOP_LDST(evstwho, 0x1A, 2),
9340GEN_SPEOP_LDST(evstwwe, 0x1C, 2),
9341GEN_SPEOP_LDST(evstwwo, 0x1E, 2),
9342};
9343
0411a972 9344#include "helper_regs.h"
a1389542 9345#include "translate_init.c"
79aceca5 9346
9a64fbe4 9347/*****************************************************************************/
3fc6c082 9348/* Misc PowerPC helpers */
1328c2bf 9349void cpu_dump_state (CPUPPCState *env, FILE *f, fprintf_function cpu_fprintf,
36081602 9350 int flags)
79aceca5 9351{
3fc6c082
FB
9352#define RGPL 4
9353#define RFPL 4
3fc6c082 9354
79aceca5
FB
9355 int i;
9356
29979a8d
AG
9357 cpu_synchronize_state(env);
9358
90e189ec 9359 cpu_fprintf(f, "NIP " TARGET_FMT_lx " LR " TARGET_FMT_lx " CTR "
9a78eead 9360 TARGET_FMT_lx " XER " TARGET_FMT_lx "\n",
da91a00f 9361 env->nip, env->lr, env->ctr, cpu_read_xer(env));
90e189ec
BS
9362 cpu_fprintf(f, "MSR " TARGET_FMT_lx " HID0 " TARGET_FMT_lx " HF "
9363 TARGET_FMT_lx " idx %d\n", env->msr, env->spr[SPR_HID0],
9364 env->hflags, env->mmu_idx);
d9bce9d9 9365#if !defined(NO_TIMER_DUMP)
9a78eead 9366 cpu_fprintf(f, "TB %08" PRIu32 " %08" PRIu64
76a66253 9367#if !defined(CONFIG_USER_ONLY)
9a78eead 9368 " DECR %08" PRIu32
76a66253
JM
9369#endif
9370 "\n",
077fc206 9371 cpu_ppc_load_tbu(env), cpu_ppc_load_tbl(env)
76a66253
JM
9372#if !defined(CONFIG_USER_ONLY)
9373 , cpu_ppc_load_decr(env)
9374#endif
9375 );
077fc206 9376#endif
76a66253 9377 for (i = 0; i < 32; i++) {
3fc6c082
FB
9378 if ((i & (RGPL - 1)) == 0)
9379 cpu_fprintf(f, "GPR%02d", i);
b11ebf64 9380 cpu_fprintf(f, " %016" PRIx64, ppc_dump_gpr(env, i));
3fc6c082 9381 if ((i & (RGPL - 1)) == (RGPL - 1))
7fe48483 9382 cpu_fprintf(f, "\n");
76a66253 9383 }
3fc6c082 9384 cpu_fprintf(f, "CR ");
76a66253 9385 for (i = 0; i < 8; i++)
7fe48483
FB
9386 cpu_fprintf(f, "%01x", env->crf[i]);
9387 cpu_fprintf(f, " [");
76a66253
JM
9388 for (i = 0; i < 8; i++) {
9389 char a = '-';
9390 if (env->crf[i] & 0x08)
9391 a = 'L';
9392 else if (env->crf[i] & 0x04)
9393 a = 'G';
9394 else if (env->crf[i] & 0x02)
9395 a = 'E';
7fe48483 9396 cpu_fprintf(f, " %c%c", a, env->crf[i] & 0x01 ? 'O' : ' ');
76a66253 9397 }
90e189ec
BS
9398 cpu_fprintf(f, " ] RES " TARGET_FMT_lx "\n",
9399 env->reserve_addr);
3fc6c082
FB
9400 for (i = 0; i < 32; i++) {
9401 if ((i & (RFPL - 1)) == 0)
9402 cpu_fprintf(f, "FPR%02d", i);
26a76461 9403 cpu_fprintf(f, " %016" PRIx64, *((uint64_t *)&env->fpr[i]));
3fc6c082 9404 if ((i & (RFPL - 1)) == (RFPL - 1))
7fe48483 9405 cpu_fprintf(f, "\n");
79aceca5 9406 }
30304420 9407 cpu_fprintf(f, "FPSCR " TARGET_FMT_lx "\n", env->fpscr);
f2e63a42 9408#if !defined(CONFIG_USER_ONLY)
90dc8812
SW
9409 cpu_fprintf(f, " SRR0 " TARGET_FMT_lx " SRR1 " TARGET_FMT_lx
9410 " PVR " TARGET_FMT_lx " VRSAVE " TARGET_FMT_lx "\n",
9411 env->spr[SPR_SRR0], env->spr[SPR_SRR1],
9412 env->spr[SPR_PVR], env->spr[SPR_VRSAVE]);
9413
9414 cpu_fprintf(f, "SPRG0 " TARGET_FMT_lx " SPRG1 " TARGET_FMT_lx
9415 " SPRG2 " TARGET_FMT_lx " SPRG3 " TARGET_FMT_lx "\n",
9416 env->spr[SPR_SPRG0], env->spr[SPR_SPRG1],
9417 env->spr[SPR_SPRG2], env->spr[SPR_SPRG3]);
9418
9419 cpu_fprintf(f, "SPRG4 " TARGET_FMT_lx " SPRG5 " TARGET_FMT_lx
9420 " SPRG6 " TARGET_FMT_lx " SPRG7 " TARGET_FMT_lx "\n",
9421 env->spr[SPR_SPRG4], env->spr[SPR_SPRG5],
9422 env->spr[SPR_SPRG6], env->spr[SPR_SPRG7]);
9423
9424 if (env->excp_model == POWERPC_EXCP_BOOKE) {
9425 cpu_fprintf(f, "CSRR0 " TARGET_FMT_lx " CSRR1 " TARGET_FMT_lx
9426 " MCSRR0 " TARGET_FMT_lx " MCSRR1 " TARGET_FMT_lx "\n",
9427 env->spr[SPR_BOOKE_CSRR0], env->spr[SPR_BOOKE_CSRR1],
9428 env->spr[SPR_BOOKE_MCSRR0], env->spr[SPR_BOOKE_MCSRR1]);
9429
9430 cpu_fprintf(f, " TCR " TARGET_FMT_lx " TSR " TARGET_FMT_lx
9431 " ESR " TARGET_FMT_lx " DEAR " TARGET_FMT_lx "\n",
9432 env->spr[SPR_BOOKE_TCR], env->spr[SPR_BOOKE_TSR],
9433 env->spr[SPR_BOOKE_ESR], env->spr[SPR_BOOKE_DEAR]);
9434
9435 cpu_fprintf(f, " PIR " TARGET_FMT_lx " DECAR " TARGET_FMT_lx
9436 " IVPR " TARGET_FMT_lx " EPCR " TARGET_FMT_lx "\n",
9437 env->spr[SPR_BOOKE_PIR], env->spr[SPR_BOOKE_DECAR],
9438 env->spr[SPR_BOOKE_IVPR], env->spr[SPR_BOOKE_EPCR]);
9439
9440 cpu_fprintf(f, " MCSR " TARGET_FMT_lx " SPRG8 " TARGET_FMT_lx
9441 " EPR " TARGET_FMT_lx "\n",
9442 env->spr[SPR_BOOKE_MCSR], env->spr[SPR_BOOKE_SPRG8],
9443 env->spr[SPR_BOOKE_EPR]);
9444
9445 /* FSL-specific */
9446 cpu_fprintf(f, " MCAR " TARGET_FMT_lx " PID1 " TARGET_FMT_lx
9447 " PID2 " TARGET_FMT_lx " SVR " TARGET_FMT_lx "\n",
9448 env->spr[SPR_Exxx_MCAR], env->spr[SPR_BOOKE_PID1],
9449 env->spr[SPR_BOOKE_PID2], env->spr[SPR_E500_SVR]);
9450
9451 /*
9452 * IVORs are left out as they are large and do not change often --
9453 * they can be read with "p $ivor0", "p $ivor1", etc.
9454 */
9455 }
9456
697ab892
DG
9457#if defined(TARGET_PPC64)
9458 if (env->flags & POWERPC_FLAG_CFAR) {
9459 cpu_fprintf(f, " CFAR " TARGET_FMT_lx"\n", env->cfar);
9460 }
9461#endif
9462
90dc8812
SW
9463 switch (env->mmu_model) {
9464 case POWERPC_MMU_32B:
9465 case POWERPC_MMU_601:
9466 case POWERPC_MMU_SOFT_6xx:
9467 case POWERPC_MMU_SOFT_74xx:
9468#if defined(TARGET_PPC64)
90dc8812
SW
9469 case POWERPC_MMU_64B:
9470#endif
9471 cpu_fprintf(f, " SDR1 " TARGET_FMT_lx "\n", env->spr[SPR_SDR1]);
9472 break;
01662f3e 9473 case POWERPC_MMU_BOOKE206:
90dc8812
SW
9474 cpu_fprintf(f, " MAS0 " TARGET_FMT_lx " MAS1 " TARGET_FMT_lx
9475 " MAS2 " TARGET_FMT_lx " MAS3 " TARGET_FMT_lx "\n",
9476 env->spr[SPR_BOOKE_MAS0], env->spr[SPR_BOOKE_MAS1],
9477 env->spr[SPR_BOOKE_MAS2], env->spr[SPR_BOOKE_MAS3]);
9478
9479 cpu_fprintf(f, " MAS4 " TARGET_FMT_lx " MAS6 " TARGET_FMT_lx
9480 " MAS7 " TARGET_FMT_lx " PID " TARGET_FMT_lx "\n",
9481 env->spr[SPR_BOOKE_MAS4], env->spr[SPR_BOOKE_MAS6],
9482 env->spr[SPR_BOOKE_MAS7], env->spr[SPR_BOOKE_PID]);
9483
9484 cpu_fprintf(f, "MMUCFG " TARGET_FMT_lx " TLB0CFG " TARGET_FMT_lx
9485 " TLB1CFG " TARGET_FMT_lx "\n",
9486 env->spr[SPR_MMUCFG], env->spr[SPR_BOOKE_TLB0CFG],
9487 env->spr[SPR_BOOKE_TLB1CFG]);
9488 break;
9489 default:
9490 break;
9491 }
f2e63a42 9492#endif
79aceca5 9493
3fc6c082
FB
9494#undef RGPL
9495#undef RFPL
79aceca5
FB
9496}
9497
1328c2bf 9498void cpu_dump_statistics (CPUPPCState *env, FILE*f, fprintf_function cpu_fprintf,
76a66253
JM
9499 int flags)
9500{
9501#if defined(DO_PPC_STATISTICS)
c227f099 9502 opc_handler_t **t1, **t2, **t3, *handler;
76a66253
JM
9503 int op1, op2, op3;
9504
9505 t1 = env->opcodes;
9506 for (op1 = 0; op1 < 64; op1++) {
9507 handler = t1[op1];
9508 if (is_indirect_opcode(handler)) {
9509 t2 = ind_table(handler);
9510 for (op2 = 0; op2 < 32; op2++) {
9511 handler = t2[op2];
9512 if (is_indirect_opcode(handler)) {
9513 t3 = ind_table(handler);
9514 for (op3 = 0; op3 < 32; op3++) {
9515 handler = t3[op3];
9516 if (handler->count == 0)
9517 continue;
9518 cpu_fprintf(f, "%02x %02x %02x (%02x %04d) %16s: "
0bfcd599 9519 "%016" PRIx64 " %" PRId64 "\n",
76a66253
JM
9520 op1, op2, op3, op1, (op3 << 5) | op2,
9521 handler->oname,
9522 handler->count, handler->count);
9523 }
9524 } else {
9525 if (handler->count == 0)
9526 continue;
9527 cpu_fprintf(f, "%02x %02x (%02x %04d) %16s: "
0bfcd599 9528 "%016" PRIx64 " %" PRId64 "\n",
76a66253
JM
9529 op1, op2, op1, op2, handler->oname,
9530 handler->count, handler->count);
9531 }
9532 }
9533 } else {
9534 if (handler->count == 0)
9535 continue;
0bfcd599
BS
9536 cpu_fprintf(f, "%02x (%02x ) %16s: %016" PRIx64
9537 " %" PRId64 "\n",
76a66253
JM
9538 op1, op1, handler->oname,
9539 handler->count, handler->count);
9540 }
9541 }
9542#endif
9543}
9544
9a64fbe4 9545/*****************************************************************************/
1328c2bf 9546static inline void gen_intermediate_code_internal(CPUPPCState *env,
636aa200
BS
9547 TranslationBlock *tb,
9548 int search_pc)
79aceca5 9549{
9fddaa0c 9550 DisasContext ctx, *ctxp = &ctx;
c227f099 9551 opc_handler_t **table, *handler;
0fa85d43 9552 target_ulong pc_start;
79aceca5 9553 uint16_t *gen_opc_end;
a1d1bb31 9554 CPUBreakpoint *bp;
79aceca5 9555 int j, lj = -1;
2e70f6ef
PB
9556 int num_insns;
9557 int max_insns;
79aceca5
FB
9558
9559 pc_start = tb->pc;
92414b31 9560 gen_opc_end = tcg_ctx.gen_opc_buf + OPC_MAX_SIZE;
046d6672 9561 ctx.nip = pc_start;
79aceca5 9562 ctx.tb = tb;
e1833e1f 9563 ctx.exception = POWERPC_EXCP_NONE;
3fc6c082 9564 ctx.spr_cb = env->spr_cb;
76db3ba4
AJ
9565 ctx.mem_idx = env->mmu_idx;
9566 ctx.access_type = -1;
9567 ctx.le_mode = env->hflags & (1 << MSR_LE) ? 1 : 0;
d9bce9d9 9568#if defined(TARGET_PPC64)
e42a61f1 9569 ctx.sf_mode = msr_is_64bit(env, env->msr);
697ab892 9570 ctx.has_cfar = !!(env->flags & POWERPC_FLAG_CFAR);
9a64fbe4 9571#endif
3cc62370 9572 ctx.fpu_enabled = msr_fp;
a9d9eb8f 9573 if ((env->flags & POWERPC_FLAG_SPE) && msr_spe)
d26bfc9a
JM
9574 ctx.spe_enabled = msr_spe;
9575 else
9576 ctx.spe_enabled = 0;
a9d9eb8f
JM
9577 if ((env->flags & POWERPC_FLAG_VRE) && msr_vr)
9578 ctx.altivec_enabled = msr_vr;
9579 else
9580 ctx.altivec_enabled = 0;
d26bfc9a 9581 if ((env->flags & POWERPC_FLAG_SE) && msr_se)
8cbcb4fa 9582 ctx.singlestep_enabled = CPU_SINGLE_STEP;
d26bfc9a 9583 else
8cbcb4fa 9584 ctx.singlestep_enabled = 0;
d26bfc9a 9585 if ((env->flags & POWERPC_FLAG_BE) && msr_be)
8cbcb4fa
AJ
9586 ctx.singlestep_enabled |= CPU_BRANCH_STEP;
9587 if (unlikely(env->singlestep_enabled))
9588 ctx.singlestep_enabled |= GDBSTUB_SINGLE_STEP;
3fc6c082 9589#if defined (DO_SINGLE_STEP) && 0
9a64fbe4
FB
9590 /* Single step trace mode */
9591 msr_se = 1;
9592#endif
2e70f6ef
PB
9593 num_insns = 0;
9594 max_insns = tb->cflags & CF_COUNT_MASK;
9595 if (max_insns == 0)
9596 max_insns = CF_COUNT_MASK;
9597
806f352d 9598 gen_tb_start();
9a64fbe4 9599 /* Set env in case of segfault during code fetch */
efd7f486
EV
9600 while (ctx.exception == POWERPC_EXCP_NONE
9601 && tcg_ctx.gen_opc_ptr < gen_opc_end) {
72cf2d4f
BS
9602 if (unlikely(!QTAILQ_EMPTY(&env->breakpoints))) {
9603 QTAILQ_FOREACH(bp, &env->breakpoints, entry) {
a1d1bb31 9604 if (bp->pc == ctx.nip) {
e06fcd75 9605 gen_debug_exception(ctxp);
ea4e754f
FB
9606 break;
9607 }
9608 }
9609 }
76a66253 9610 if (unlikely(search_pc)) {
92414b31 9611 j = tcg_ctx.gen_opc_ptr - tcg_ctx.gen_opc_buf;
79aceca5
FB
9612 if (lj < j) {
9613 lj++;
9614 while (lj < j)
ab1103de 9615 tcg_ctx.gen_opc_instr_start[lj++] = 0;
79aceca5 9616 }
25983cad 9617 tcg_ctx.gen_opc_pc[lj] = ctx.nip;
ab1103de 9618 tcg_ctx.gen_opc_instr_start[lj] = 1;
c9c99c22 9619 tcg_ctx.gen_opc_icount[lj] = num_insns;
79aceca5 9620 }
d12d51d5 9621 LOG_DISAS("----------------\n");
90e189ec 9622 LOG_DISAS("nip=" TARGET_FMT_lx " super=%d ir=%d\n",
d12d51d5 9623 ctx.nip, ctx.mem_idx, (int)msr_ir);
2e70f6ef
PB
9624 if (num_insns + 1 == max_insns && (tb->cflags & CF_LAST_IO))
9625 gen_io_start();
76db3ba4 9626 if (unlikely(ctx.le_mode)) {
2f5a189c 9627 ctx.opcode = bswap32(cpu_ldl_code(env, ctx.nip));
056401ea 9628 } else {
2f5a189c 9629 ctx.opcode = cpu_ldl_code(env, ctx.nip);
111bfab3 9630 }
d12d51d5 9631 LOG_DISAS("translate opcode %08x (%02x %02x %02x) (%s)\n",
9a64fbe4 9632 ctx.opcode, opc1(ctx.opcode), opc2(ctx.opcode),
476b6d16 9633 opc3(ctx.opcode), ctx.le_mode ? "little" : "big");
fdefe51c 9634 if (unlikely(qemu_loglevel_mask(CPU_LOG_TB_OP | CPU_LOG_TB_OP_OPT))) {
731c54f8 9635 tcg_gen_debug_insn_start(ctx.nip);
fdefe51c 9636 }
046d6672 9637 ctx.nip += 4;
3fc6c082 9638 table = env->opcodes;
2e70f6ef 9639 num_insns++;
79aceca5
FB
9640 handler = table[opc1(ctx.opcode)];
9641 if (is_indirect_opcode(handler)) {
9642 table = ind_table(handler);
9643 handler = table[opc2(ctx.opcode)];
9644 if (is_indirect_opcode(handler)) {
9645 table = ind_table(handler);
9646 handler = table[opc3(ctx.opcode)];
9647 }
9648 }
9649 /* Is opcode *REALLY* valid ? */
76a66253 9650 if (unlikely(handler->handler == &gen_invalid)) {
93fcfe39
AL
9651 if (qemu_log_enabled()) {
9652 qemu_log("invalid/unsupported opcode: "
90e189ec
BS
9653 "%02x - %02x - %02x (%08x) " TARGET_FMT_lx " %d\n",
9654 opc1(ctx.opcode), opc2(ctx.opcode),
9655 opc3(ctx.opcode), ctx.opcode, ctx.nip - 4, (int)msr_ir);
4b3686fa 9656 }
76a66253 9657 } else {
70560da7
FC
9658 uint32_t inval;
9659
9660 if (unlikely(handler->type & (PPC_SPE | PPC_SPE_SINGLE | PPC_SPE_DOUBLE) && Rc(ctx.opcode))) {
9661 inval = handler->inval2;
9662 } else {
9663 inval = handler->inval1;
9664 }
9665
9666 if (unlikely((ctx.opcode & inval) != 0)) {
93fcfe39
AL
9667 if (qemu_log_enabled()) {
9668 qemu_log("invalid bits: %08x for opcode: "
90e189ec 9669 "%02x - %02x - %02x (%08x) " TARGET_FMT_lx "\n",
70560da7 9670 ctx.opcode & inval, opc1(ctx.opcode),
90e189ec
BS
9671 opc2(ctx.opcode), opc3(ctx.opcode),
9672 ctx.opcode, ctx.nip - 4);
76a66253 9673 }
e06fcd75 9674 gen_inval_exception(ctxp, POWERPC_EXCP_INVAL_INVAL);
4b3686fa 9675 break;
79aceca5 9676 }
79aceca5 9677 }
4b3686fa 9678 (*(handler->handler))(&ctx);
76a66253
JM
9679#if defined(DO_PPC_STATISTICS)
9680 handler->count++;
9681#endif
9a64fbe4 9682 /* Check trace mode exceptions */
8cbcb4fa
AJ
9683 if (unlikely(ctx.singlestep_enabled & CPU_SINGLE_STEP &&
9684 (ctx.nip <= 0x100 || ctx.nip > 0xF00) &&
9685 ctx.exception != POWERPC_SYSCALL &&
9686 ctx.exception != POWERPC_EXCP_TRAP &&
9687 ctx.exception != POWERPC_EXCP_BRANCH)) {
e06fcd75 9688 gen_exception(ctxp, POWERPC_EXCP_TRACE);
d26bfc9a 9689 } else if (unlikely(((ctx.nip & (TARGET_PAGE_SIZE - 1)) == 0) ||
2e70f6ef 9690 (env->singlestep_enabled) ||
1b530a6d 9691 singlestep ||
2e70f6ef 9692 num_insns >= max_insns)) {
d26bfc9a
JM
9693 /* if we reach a page boundary or are single stepping, stop
9694 * generation
9695 */
8dd4983c 9696 break;
76a66253 9697 }
3fc6c082 9698 }
2e70f6ef
PB
9699 if (tb->cflags & CF_LAST_IO)
9700 gen_io_end();
e1833e1f 9701 if (ctx.exception == POWERPC_EXCP_NONE) {
c1942362 9702 gen_goto_tb(&ctx, 0, ctx.nip);
e1833e1f 9703 } else if (ctx.exception != POWERPC_EXCP_BRANCH) {
8cbcb4fa 9704 if (unlikely(env->singlestep_enabled)) {
e06fcd75 9705 gen_debug_exception(ctxp);
8cbcb4fa 9706 }
76a66253 9707 /* Generate the return instruction */
57fec1fe 9708 tcg_gen_exit_tb(0);
9a64fbe4 9709 }
806f352d 9710 gen_tb_end(tb, num_insns);
efd7f486 9711 *tcg_ctx.gen_opc_ptr = INDEX_op_end;
76a66253 9712 if (unlikely(search_pc)) {
92414b31 9713 j = tcg_ctx.gen_opc_ptr - tcg_ctx.gen_opc_buf;
9a64fbe4
FB
9714 lj++;
9715 while (lj <= j)
ab1103de 9716 tcg_ctx.gen_opc_instr_start[lj++] = 0;
9a64fbe4 9717 } else {
046d6672 9718 tb->size = ctx.nip - pc_start;
2e70f6ef 9719 tb->icount = num_insns;
9a64fbe4 9720 }
d9bce9d9 9721#if defined(DEBUG_DISAS)
8fec2b8c 9722 if (qemu_loglevel_mask(CPU_LOG_TB_IN_ASM)) {
76a66253 9723 int flags;
237c0af0 9724 flags = env->bfd_mach;
76db3ba4 9725 flags |= ctx.le_mode << 16;
93fcfe39 9726 qemu_log("IN: %s\n", lookup_symbol(pc_start));
f4359b9f 9727 log_target_disas(env, pc_start, ctx.nip - pc_start, flags);
93fcfe39 9728 qemu_log("\n");
9fddaa0c 9729 }
79aceca5 9730#endif
79aceca5
FB
9731}
9732
1328c2bf 9733void gen_intermediate_code (CPUPPCState *env, struct TranslationBlock *tb)
79aceca5 9734{
2cfc5f17 9735 gen_intermediate_code_internal(env, tb, 0);
79aceca5
FB
9736}
9737
1328c2bf 9738void gen_intermediate_code_pc (CPUPPCState *env, struct TranslationBlock *tb)
79aceca5 9739{
2cfc5f17 9740 gen_intermediate_code_internal(env, tb, 1);
79aceca5 9741}
d2856f1a 9742
1328c2bf 9743void restore_state_to_opc(CPUPPCState *env, TranslationBlock *tb, int pc_pos)
d2856f1a 9744{
25983cad 9745 env->nip = tcg_ctx.gen_opc_pc[pc_pos];
d2856f1a 9746}