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