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