]> git.proxmox.com Git - qemu.git/blame - target-ppc/translate.c
target-ppc: emulate lfiwax instruction
[qemu.git] / target-ppc / translate.c
CommitLineData
79aceca5 1/*
3fc6c082 2 * PowerPC emulation for qemu: main translation routines.
5fafdf24 3 *
76a66253 4 * Copyright (c) 2003-2007 Jocelyn Mayer
90dc8812 5 * Copyright (C) 2011 Freescale Semiconductor, Inc.
79aceca5
FB
6 *
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2 of the License, or (at your option) any later version.
11 *
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
8167ee88 18 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
79aceca5 19 */
c6a1c22b 20
79aceca5 21#include "cpu.h"
76cad711 22#include "disas/disas.h"
57fec1fe 23#include "tcg-op.h"
1de7afc9 24#include "qemu/host-utils.h"
79aceca5 25
a7812ae4
PB
26#include "helper.h"
27#define GEN_HELPER 1
28#include "helper.h"
29
8cbcb4fa
AJ
30#define CPU_SINGLE_STEP 0x1
31#define CPU_BRANCH_STEP 0x2
32#define GDBSTUB_SINGLE_STEP 0x4
33
a750fc0b 34/* Include definitions for instructions classes and implementations flags */
9fddaa0c 35//#define PPC_DEBUG_DISAS
76a66253 36//#define DO_PPC_STATISTICS
79aceca5 37
d12d51d5 38#ifdef PPC_DEBUG_DISAS
93fcfe39 39# define LOG_DISAS(...) qemu_log_mask(CPU_LOG_TB_IN_ASM, ## __VA_ARGS__)
d12d51d5
AL
40#else
41# define LOG_DISAS(...) do { } while (0)
42#endif
a750fc0b
JM
43/*****************************************************************************/
44/* Code translation helpers */
c53be334 45
f78fb44e 46/* global register indexes */
a7812ae4 47static TCGv_ptr cpu_env;
1d542695 48static char cpu_reg_names[10*3 + 22*4 /* GPR */
f78fb44e 49#if !defined(TARGET_PPC64)
1d542695 50 + 10*4 + 22*5 /* SPE GPRh */
f78fb44e 51#endif
a5e26afa 52 + 10*4 + 22*5 /* FPR */
47e4661c
AJ
53 + 2*(10*6 + 22*7) /* AVRh, AVRl */
54 + 8*5 /* CRF */];
f78fb44e
AJ
55static TCGv cpu_gpr[32];
56#if !defined(TARGET_PPC64)
57static TCGv cpu_gprh[32];
58#endif
a7812ae4
PB
59static TCGv_i64 cpu_fpr[32];
60static TCGv_i64 cpu_avrh[32], cpu_avrl[32];
61static TCGv_i32 cpu_crf[8];
bd568f18 62static TCGv cpu_nip;
6527f6ea 63static TCGv cpu_msr;
cfdcd37a
AJ
64static TCGv cpu_ctr;
65static TCGv cpu_lr;
697ab892
DG
66#if defined(TARGET_PPC64)
67static TCGv cpu_cfar;
68#endif
da91a00f 69static TCGv cpu_xer, cpu_so, cpu_ov, cpu_ca;
cf360a32 70static TCGv cpu_reserve;
30304420 71static TCGv cpu_fpscr;
a7859e89 72static TCGv_i32 cpu_access_type;
f78fb44e 73
022c62cb 74#include "exec/gen-icount.h"
2e70f6ef
PB
75
76void ppc_translate_init(void)
77{
f78fb44e
AJ
78 int i;
79 char* p;
2dc766da 80 size_t cpu_reg_names_size;
b2437bf2 81 static int done_init = 0;
f78fb44e 82
2e70f6ef
PB
83 if (done_init)
84 return;
f78fb44e 85
a7812ae4 86 cpu_env = tcg_global_reg_new_ptr(TCG_AREG0, "env");
a7812ae4 87
f78fb44e 88 p = cpu_reg_names;
2dc766da 89 cpu_reg_names_size = sizeof(cpu_reg_names);
47e4661c
AJ
90
91 for (i = 0; i < 8; i++) {
2dc766da 92 snprintf(p, cpu_reg_names_size, "crf%d", i);
a7812ae4 93 cpu_crf[i] = tcg_global_mem_new_i32(TCG_AREG0,
1328c2bf 94 offsetof(CPUPPCState, crf[i]), p);
47e4661c 95 p += 5;
2dc766da 96 cpu_reg_names_size -= 5;
47e4661c
AJ
97 }
98
f78fb44e 99 for (i = 0; i < 32; i++) {
2dc766da 100 snprintf(p, cpu_reg_names_size, "r%d", i);
a7812ae4 101 cpu_gpr[i] = tcg_global_mem_new(TCG_AREG0,
1328c2bf 102 offsetof(CPUPPCState, gpr[i]), p);
f78fb44e 103 p += (i < 10) ? 3 : 4;
2dc766da 104 cpu_reg_names_size -= (i < 10) ? 3 : 4;
f78fb44e 105#if !defined(TARGET_PPC64)
2dc766da 106 snprintf(p, cpu_reg_names_size, "r%dH", i);
a7812ae4 107 cpu_gprh[i] = tcg_global_mem_new_i32(TCG_AREG0,
1328c2bf 108 offsetof(CPUPPCState, gprh[i]), p);
f78fb44e 109 p += (i < 10) ? 4 : 5;
2dc766da 110 cpu_reg_names_size -= (i < 10) ? 4 : 5;
f78fb44e 111#endif
1d542695 112
2dc766da 113 snprintf(p, cpu_reg_names_size, "fp%d", i);
a7812ae4 114 cpu_fpr[i] = tcg_global_mem_new_i64(TCG_AREG0,
1328c2bf 115 offsetof(CPUPPCState, fpr[i]), p);
ec1ac72d 116 p += (i < 10) ? 4 : 5;
2dc766da 117 cpu_reg_names_size -= (i < 10) ? 4 : 5;
a5e26afa 118
2dc766da 119 snprintf(p, cpu_reg_names_size, "avr%dH", i);
e2542fe2 120#ifdef HOST_WORDS_BIGENDIAN
fe1e5c53 121 cpu_avrh[i] = tcg_global_mem_new_i64(TCG_AREG0,
1328c2bf 122 offsetof(CPUPPCState, avr[i].u64[0]), p);
fe1e5c53 123#else
a7812ae4 124 cpu_avrh[i] = tcg_global_mem_new_i64(TCG_AREG0,
1328c2bf 125 offsetof(CPUPPCState, avr[i].u64[1]), p);
fe1e5c53 126#endif
1d542695 127 p += (i < 10) ? 6 : 7;
2dc766da 128 cpu_reg_names_size -= (i < 10) ? 6 : 7;
ec1ac72d 129
2dc766da 130 snprintf(p, cpu_reg_names_size, "avr%dL", i);
e2542fe2 131#ifdef HOST_WORDS_BIGENDIAN
fe1e5c53 132 cpu_avrl[i] = tcg_global_mem_new_i64(TCG_AREG0,
1328c2bf 133 offsetof(CPUPPCState, avr[i].u64[1]), p);
fe1e5c53 134#else
a7812ae4 135 cpu_avrl[i] = tcg_global_mem_new_i64(TCG_AREG0,
1328c2bf 136 offsetof(CPUPPCState, avr[i].u64[0]), p);
fe1e5c53 137#endif
1d542695 138 p += (i < 10) ? 6 : 7;
2dc766da 139 cpu_reg_names_size -= (i < 10) ? 6 : 7;
f78fb44e 140 }
f10dc08e 141
a7812ae4 142 cpu_nip = tcg_global_mem_new(TCG_AREG0,
1328c2bf 143 offsetof(CPUPPCState, nip), "nip");
bd568f18 144
6527f6ea 145 cpu_msr = tcg_global_mem_new(TCG_AREG0,
1328c2bf 146 offsetof(CPUPPCState, msr), "msr");
6527f6ea 147
a7812ae4 148 cpu_ctr = tcg_global_mem_new(TCG_AREG0,
1328c2bf 149 offsetof(CPUPPCState, ctr), "ctr");
cfdcd37a 150
a7812ae4 151 cpu_lr = tcg_global_mem_new(TCG_AREG0,
1328c2bf 152 offsetof(CPUPPCState, lr), "lr");
cfdcd37a 153
697ab892
DG
154#if defined(TARGET_PPC64)
155 cpu_cfar = tcg_global_mem_new(TCG_AREG0,
1328c2bf 156 offsetof(CPUPPCState, cfar), "cfar");
697ab892
DG
157#endif
158
a7812ae4 159 cpu_xer = tcg_global_mem_new(TCG_AREG0,
1328c2bf 160 offsetof(CPUPPCState, xer), "xer");
da91a00f
RH
161 cpu_so = tcg_global_mem_new(TCG_AREG0,
162 offsetof(CPUPPCState, so), "SO");
163 cpu_ov = tcg_global_mem_new(TCG_AREG0,
164 offsetof(CPUPPCState, ov), "OV");
165 cpu_ca = tcg_global_mem_new(TCG_AREG0,
166 offsetof(CPUPPCState, ca), "CA");
3d7b417e 167
cf360a32 168 cpu_reserve = tcg_global_mem_new(TCG_AREG0,
1328c2bf 169 offsetof(CPUPPCState, reserve_addr),
18b21a2f 170 "reserve_addr");
cf360a32 171
30304420
DG
172 cpu_fpscr = tcg_global_mem_new(TCG_AREG0,
173 offsetof(CPUPPCState, fpscr), "fpscr");
e1571908 174
a7859e89 175 cpu_access_type = tcg_global_mem_new_i32(TCG_AREG0,
1328c2bf 176 offsetof(CPUPPCState, access_type), "access_type");
a7859e89 177
f10dc08e 178 /* register helpers */
a7812ae4 179#define GEN_HELPER 2
f10dc08e
AJ
180#include "helper.h"
181
2e70f6ef
PB
182 done_init = 1;
183}
184
79aceca5
FB
185/* internal defines */
186typedef struct DisasContext {
187 struct TranslationBlock *tb;
0fa85d43 188 target_ulong nip;
79aceca5 189 uint32_t opcode;
9a64fbe4 190 uint32_t exception;
3cc62370
FB
191 /* Routine used to access memory */
192 int mem_idx;
76db3ba4 193 int access_type;
3cc62370 194 /* Translation flags */
76db3ba4 195 int le_mode;
d9bce9d9
JM
196#if defined(TARGET_PPC64)
197 int sf_mode;
697ab892 198 int has_cfar;
9a64fbe4 199#endif
3cc62370 200 int fpu_enabled;
a9d9eb8f 201 int altivec_enabled;
0487d6a8 202 int spe_enabled;
c227f099 203 ppc_spr_t *spr_cb; /* Needed to check rights for mfspr/mtspr */
ea4e754f 204 int singlestep_enabled;
79aceca5
FB
205} DisasContext;
206
79482e5a
RH
207/* True when active word size < size of target_long. */
208#ifdef TARGET_PPC64
209# define NARROW_MODE(C) (!(C)->sf_mode)
210#else
211# define NARROW_MODE(C) 0
212#endif
213
c227f099 214struct opc_handler_t {
70560da7
FC
215 /* invalid bits for instruction 1 (Rc(opcode) == 0) */
216 uint32_t inval1;
217 /* invalid bits for instruction 2 (Rc(opcode) == 1) */
218 uint32_t inval2;
9a64fbe4 219 /* instruction type */
0487d6a8 220 uint64_t type;
a5858d7a
AG
221 /* extended instruction type */
222 uint64_t type2;
79aceca5
FB
223 /* handler */
224 void (*handler)(DisasContext *ctx);
a750fc0b 225#if defined(DO_PPC_STATISTICS) || defined(PPC_DUMP_CPU)
b55266b5 226 const char *oname;
a750fc0b
JM
227#endif
228#if defined(DO_PPC_STATISTICS)
76a66253
JM
229 uint64_t count;
230#endif
3fc6c082 231};
79aceca5 232
636aa200 233static inline void gen_reset_fpstatus(void)
7c58044c 234{
8e703949 235 gen_helper_reset_fpstatus(cpu_env);
7c58044c
JM
236}
237
636aa200 238static inline void gen_compute_fprf(TCGv_i64 arg, int set_fprf, int set_rc)
7c58044c 239{
0f2f39c2 240 TCGv_i32 t0 = tcg_temp_new_i32();
af12906f 241
7c58044c
JM
242 if (set_fprf != 0) {
243 /* This case might be optimized later */
0f2f39c2 244 tcg_gen_movi_i32(t0, 1);
8e703949 245 gen_helper_compute_fprf(t0, cpu_env, arg, t0);
a7812ae4 246 if (unlikely(set_rc)) {
0f2f39c2 247 tcg_gen_mov_i32(cpu_crf[1], t0);
a7812ae4 248 }
8e703949 249 gen_helper_float_check_status(cpu_env);
7c58044c
JM
250 } else if (unlikely(set_rc)) {
251 /* We always need to compute fpcc */
0f2f39c2 252 tcg_gen_movi_i32(t0, 0);
8e703949 253 gen_helper_compute_fprf(t0, cpu_env, arg, t0);
0f2f39c2 254 tcg_gen_mov_i32(cpu_crf[1], t0);
7c58044c 255 }
af12906f 256
0f2f39c2 257 tcg_temp_free_i32(t0);
7c58044c
JM
258}
259
636aa200 260static inline void gen_set_access_type(DisasContext *ctx, int access_type)
a7859e89 261{
76db3ba4
AJ
262 if (ctx->access_type != access_type) {
263 tcg_gen_movi_i32(cpu_access_type, access_type);
264 ctx->access_type = access_type;
265 }
a7859e89
AJ
266}
267
636aa200 268static inline void gen_update_nip(DisasContext *ctx, target_ulong nip)
d9bce9d9 269{
e0c8f9ce
RH
270 if (NARROW_MODE(ctx)) {
271 nip = (uint32_t)nip;
272 }
273 tcg_gen_movi_tl(cpu_nip, nip);
d9bce9d9
JM
274}
275
636aa200 276static inline void gen_exception_err(DisasContext *ctx, uint32_t excp, uint32_t error)
e06fcd75
AJ
277{
278 TCGv_i32 t0, t1;
279 if (ctx->exception == POWERPC_EXCP_NONE) {
280 gen_update_nip(ctx, ctx->nip);
281 }
282 t0 = tcg_const_i32(excp);
283 t1 = tcg_const_i32(error);
e5f17ac6 284 gen_helper_raise_exception_err(cpu_env, t0, t1);
e06fcd75
AJ
285 tcg_temp_free_i32(t0);
286 tcg_temp_free_i32(t1);
287 ctx->exception = (excp);
288}
e1833e1f 289
636aa200 290static inline void gen_exception(DisasContext *ctx, uint32_t excp)
e06fcd75
AJ
291{
292 TCGv_i32 t0;
293 if (ctx->exception == POWERPC_EXCP_NONE) {
294 gen_update_nip(ctx, ctx->nip);
295 }
296 t0 = tcg_const_i32(excp);
e5f17ac6 297 gen_helper_raise_exception(cpu_env, t0);
e06fcd75
AJ
298 tcg_temp_free_i32(t0);
299 ctx->exception = (excp);
300}
e1833e1f 301
636aa200 302static inline void gen_debug_exception(DisasContext *ctx)
e06fcd75
AJ
303{
304 TCGv_i32 t0;
5518f3a6 305
ee2b3994
SB
306 if ((ctx->exception != POWERPC_EXCP_BRANCH) &&
307 (ctx->exception != POWERPC_EXCP_SYNC)) {
5518f3a6 308 gen_update_nip(ctx, ctx->nip);
ee2b3994 309 }
e06fcd75 310 t0 = tcg_const_i32(EXCP_DEBUG);
e5f17ac6 311 gen_helper_raise_exception(cpu_env, t0);
e06fcd75
AJ
312 tcg_temp_free_i32(t0);
313}
9a64fbe4 314
636aa200 315static inline void gen_inval_exception(DisasContext *ctx, uint32_t error)
e06fcd75
AJ
316{
317 gen_exception_err(ctx, POWERPC_EXCP_PROGRAM, POWERPC_EXCP_INVAL | error);
318}
a9d9eb8f 319
f24e5695 320/* Stop translation */
636aa200 321static inline void gen_stop_exception(DisasContext *ctx)
3fc6c082 322{
d9bce9d9 323 gen_update_nip(ctx, ctx->nip);
e1833e1f 324 ctx->exception = POWERPC_EXCP_STOP;
3fc6c082
FB
325}
326
f24e5695 327/* No need to update nip here, as execution flow will change */
636aa200 328static inline void gen_sync_exception(DisasContext *ctx)
2be0071f 329{
e1833e1f 330 ctx->exception = POWERPC_EXCP_SYNC;
2be0071f
FB
331}
332
79aceca5 333#define GEN_HANDLER(name, opc1, opc2, opc3, inval, type) \
a5858d7a
AG
334GEN_OPCODE(name, opc1, opc2, opc3, inval, type, PPC_NONE)
335
336#define GEN_HANDLER_E(name, opc1, opc2, opc3, inval, type, type2) \
337GEN_OPCODE(name, opc1, opc2, opc3, inval, type, type2)
79aceca5 338
c7697e1f 339#define GEN_HANDLER2(name, onam, opc1, opc2, opc3, inval, type) \
a5858d7a
AG
340GEN_OPCODE2(name, onam, opc1, opc2, opc3, inval, type, PPC_NONE)
341
342#define GEN_HANDLER2_E(name, onam, opc1, opc2, opc3, inval, type, type2) \
343GEN_OPCODE2(name, onam, opc1, opc2, opc3, inval, type, type2)
c7697e1f 344
c227f099 345typedef struct opcode_t {
79aceca5 346 unsigned char opc1, opc2, opc3;
1235fc06 347#if HOST_LONG_BITS == 64 /* Explicitly align to 64 bits */
18fba28c
FB
348 unsigned char pad[5];
349#else
350 unsigned char pad[1];
351#endif
c227f099 352 opc_handler_t handler;
b55266b5 353 const char *oname;
c227f099 354} opcode_t;
79aceca5 355
a750fc0b 356/*****************************************************************************/
79aceca5
FB
357/*** Instruction decoding ***/
358#define EXTRACT_HELPER(name, shift, nb) \
636aa200 359static inline uint32_t name(uint32_t opcode) \
79aceca5
FB
360{ \
361 return (opcode >> (shift)) & ((1 << (nb)) - 1); \
362}
363
364#define EXTRACT_SHELPER(name, shift, nb) \
636aa200 365static inline int32_t name(uint32_t opcode) \
79aceca5 366{ \
18fba28c 367 return (int16_t)((opcode >> (shift)) & ((1 << (nb)) - 1)); \
79aceca5
FB
368}
369
370/* Opcode part 1 */
371EXTRACT_HELPER(opc1, 26, 6);
372/* Opcode part 2 */
373EXTRACT_HELPER(opc2, 1, 5);
374/* Opcode part 3 */
375EXTRACT_HELPER(opc3, 6, 5);
376/* Update Cr0 flags */
377EXTRACT_HELPER(Rc, 0, 1);
378/* Destination */
379EXTRACT_HELPER(rD, 21, 5);
380/* Source */
381EXTRACT_HELPER(rS, 21, 5);
382/* First operand */
383EXTRACT_HELPER(rA, 16, 5);
384/* Second operand */
385EXTRACT_HELPER(rB, 11, 5);
386/* Third operand */
387EXTRACT_HELPER(rC, 6, 5);
388/*** Get CRn ***/
389EXTRACT_HELPER(crfD, 23, 3);
390EXTRACT_HELPER(crfS, 18, 3);
391EXTRACT_HELPER(crbD, 21, 5);
392EXTRACT_HELPER(crbA, 16, 5);
393EXTRACT_HELPER(crbB, 11, 5);
394/* SPR / TBL */
3fc6c082 395EXTRACT_HELPER(_SPR, 11, 10);
636aa200 396static inline uint32_t SPR(uint32_t opcode)
3fc6c082
FB
397{
398 uint32_t sprn = _SPR(opcode);
399
400 return ((sprn >> 5) & 0x1F) | ((sprn & 0x1F) << 5);
401}
79aceca5
FB
402/*** Get constants ***/
403EXTRACT_HELPER(IMM, 12, 8);
404/* 16 bits signed immediate value */
405EXTRACT_SHELPER(SIMM, 0, 16);
406/* 16 bits unsigned immediate value */
407EXTRACT_HELPER(UIMM, 0, 16);
21d21583
AJ
408/* 5 bits signed immediate value */
409EXTRACT_HELPER(SIMM5, 16, 5);
27a4edb3
AJ
410/* 5 bits signed immediate value */
411EXTRACT_HELPER(UIMM5, 16, 5);
79aceca5
FB
412/* Bit count */
413EXTRACT_HELPER(NB, 11, 5);
414/* Shift count */
415EXTRACT_HELPER(SH, 11, 5);
cd633b10
AJ
416/* Vector shift count */
417EXTRACT_HELPER(VSH, 6, 4);
79aceca5
FB
418/* Mask start */
419EXTRACT_HELPER(MB, 6, 5);
420/* Mask end */
421EXTRACT_HELPER(ME, 1, 5);
fb0eaffc
FB
422/* Trap operand */
423EXTRACT_HELPER(TO, 21, 5);
79aceca5
FB
424
425EXTRACT_HELPER(CRM, 12, 8);
426EXTRACT_HELPER(FM, 17, 8);
427EXTRACT_HELPER(SR, 16, 4);
e4bb997e 428EXTRACT_HELPER(FPIMM, 12, 4);
fb0eaffc 429
79aceca5
FB
430/*** Jump target decoding ***/
431/* Displacement */
432EXTRACT_SHELPER(d, 0, 16);
433/* Immediate address */
636aa200 434static inline target_ulong LI(uint32_t opcode)
79aceca5
FB
435{
436 return (opcode >> 0) & 0x03FFFFFC;
437}
438
636aa200 439static inline uint32_t BD(uint32_t opcode)
79aceca5
FB
440{
441 return (opcode >> 0) & 0xFFFC;
442}
443
444EXTRACT_HELPER(BO, 21, 5);
445EXTRACT_HELPER(BI, 16, 5);
446/* Absolute/relative address */
447EXTRACT_HELPER(AA, 1, 1);
448/* Link */
449EXTRACT_HELPER(LK, 0, 1);
450
451/* Create a mask between <start> and <end> bits */
636aa200 452static inline target_ulong MASK(uint32_t start, uint32_t end)
79aceca5 453{
76a66253 454 target_ulong ret;
79aceca5 455
76a66253
JM
456#if defined(TARGET_PPC64)
457 if (likely(start == 0)) {
6f2d8978 458 ret = UINT64_MAX << (63 - end);
76a66253 459 } else if (likely(end == 63)) {
6f2d8978 460 ret = UINT64_MAX >> start;
76a66253
JM
461 }
462#else
463 if (likely(start == 0)) {
6f2d8978 464 ret = UINT32_MAX << (31 - end);
76a66253 465 } else if (likely(end == 31)) {
6f2d8978 466 ret = UINT32_MAX >> start;
76a66253
JM
467 }
468#endif
469 else {
470 ret = (((target_ulong)(-1ULL)) >> (start)) ^
471 (((target_ulong)(-1ULL) >> (end)) >> 1);
472 if (unlikely(start > end))
473 return ~ret;
474 }
79aceca5
FB
475
476 return ret;
477}
478
a750fc0b 479/*****************************************************************************/
a750fc0b 480/* PowerPC instructions table */
933dc6eb 481
76a66253 482#if defined(DO_PPC_STATISTICS)
a5858d7a 483#define GEN_OPCODE(name, op1, op2, op3, invl, _typ, _typ2) \
5c55ff99 484{ \
79aceca5
FB
485 .opc1 = op1, \
486 .opc2 = op2, \
487 .opc3 = op3, \
18fba28c 488 .pad = { 0, }, \
79aceca5 489 .handler = { \
70560da7
FC
490 .inval1 = invl, \
491 .type = _typ, \
492 .type2 = _typ2, \
493 .handler = &gen_##name, \
494 .oname = stringify(name), \
495 }, \
496 .oname = stringify(name), \
497}
498#define GEN_OPCODE_DUAL(name, op1, op2, op3, invl1, invl2, _typ, _typ2) \
499{ \
500 .opc1 = op1, \
501 .opc2 = op2, \
502 .opc3 = op3, \
503 .pad = { 0, }, \
504 .handler = { \
505 .inval1 = invl1, \
506 .inval2 = invl2, \
9a64fbe4 507 .type = _typ, \
a5858d7a 508 .type2 = _typ2, \
79aceca5 509 .handler = &gen_##name, \
76a66253 510 .oname = stringify(name), \
79aceca5 511 }, \
3fc6c082 512 .oname = stringify(name), \
79aceca5 513}
a5858d7a 514#define GEN_OPCODE2(name, onam, op1, op2, op3, invl, _typ, _typ2) \
5c55ff99 515{ \
c7697e1f
JM
516 .opc1 = op1, \
517 .opc2 = op2, \
518 .opc3 = op3, \
519 .pad = { 0, }, \
520 .handler = { \
70560da7 521 .inval1 = invl, \
c7697e1f 522 .type = _typ, \
a5858d7a 523 .type2 = _typ2, \
c7697e1f
JM
524 .handler = &gen_##name, \
525 .oname = onam, \
526 }, \
527 .oname = onam, \
528}
76a66253 529#else
a5858d7a 530#define GEN_OPCODE(name, op1, op2, op3, invl, _typ, _typ2) \
5c55ff99 531{ \
c7697e1f
JM
532 .opc1 = op1, \
533 .opc2 = op2, \
534 .opc3 = op3, \
535 .pad = { 0, }, \
536 .handler = { \
70560da7
FC
537 .inval1 = invl, \
538 .type = _typ, \
539 .type2 = _typ2, \
540 .handler = &gen_##name, \
541 }, \
542 .oname = stringify(name), \
543}
544#define GEN_OPCODE_DUAL(name, op1, op2, op3, invl1, invl2, _typ, _typ2) \
545{ \
546 .opc1 = op1, \
547 .opc2 = op2, \
548 .opc3 = op3, \
549 .pad = { 0, }, \
550 .handler = { \
551 .inval1 = invl1, \
552 .inval2 = invl2, \
c7697e1f 553 .type = _typ, \
a5858d7a 554 .type2 = _typ2, \
c7697e1f 555 .handler = &gen_##name, \
5c55ff99
BS
556 }, \
557 .oname = stringify(name), \
558}
a5858d7a 559#define GEN_OPCODE2(name, onam, op1, op2, op3, invl, _typ, _typ2) \
5c55ff99
BS
560{ \
561 .opc1 = op1, \
562 .opc2 = op2, \
563 .opc3 = op3, \
564 .pad = { 0, }, \
565 .handler = { \
70560da7 566 .inval1 = invl, \
5c55ff99 567 .type = _typ, \
a5858d7a 568 .type2 = _typ2, \
5c55ff99
BS
569 .handler = &gen_##name, \
570 }, \
571 .oname = onam, \
572}
573#endif
2e610050 574
5c55ff99 575/* SPR load/store helpers */
636aa200 576static inline void gen_load_spr(TCGv t, int reg)
5c55ff99 577{
1328c2bf 578 tcg_gen_ld_tl(t, cpu_env, offsetof(CPUPPCState, spr[reg]));
5c55ff99 579}
2e610050 580
636aa200 581static inline void gen_store_spr(int reg, TCGv t)
5c55ff99 582{
1328c2bf 583 tcg_gen_st_tl(t, cpu_env, offsetof(CPUPPCState, spr[reg]));
5c55ff99 584}
2e610050 585
54623277 586/* Invalid instruction */
99e300ef 587static void gen_invalid(DisasContext *ctx)
9a64fbe4 588{
e06fcd75 589 gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL);
9a64fbe4
FB
590}
591
c227f099 592static opc_handler_t invalid_handler = {
70560da7
FC
593 .inval1 = 0xFFFFFFFF,
594 .inval2 = 0xFFFFFFFF,
9a64fbe4 595 .type = PPC_NONE,
a5858d7a 596 .type2 = PPC_NONE,
79aceca5
FB
597 .handler = gen_invalid,
598};
599
e1571908
AJ
600/*** Integer comparison ***/
601
636aa200 602static inline void gen_op_cmp(TCGv arg0, TCGv arg1, int s, int crf)
e1571908 603{
2fdcb629
RH
604 TCGv t0 = tcg_temp_new();
605 TCGv_i32 t1 = tcg_temp_new_i32();
e1571908 606
da91a00f 607 tcg_gen_trunc_tl_i32(cpu_crf[crf], cpu_so);
e1571908 608
2fdcb629
RH
609 tcg_gen_setcond_tl((s ? TCG_COND_LT: TCG_COND_LTU), t0, arg0, arg1);
610 tcg_gen_trunc_tl_i32(t1, t0);
611 tcg_gen_shli_i32(t1, t1, CRF_LT);
612 tcg_gen_or_i32(cpu_crf[crf], cpu_crf[crf], t1);
613
614 tcg_gen_setcond_tl((s ? TCG_COND_GT: TCG_COND_GTU), t0, arg0, arg1);
615 tcg_gen_trunc_tl_i32(t1, t0);
616 tcg_gen_shli_i32(t1, t1, CRF_GT);
617 tcg_gen_or_i32(cpu_crf[crf], cpu_crf[crf], t1);
618
619 tcg_gen_setcond_tl(TCG_COND_EQ, t0, arg0, arg1);
620 tcg_gen_trunc_tl_i32(t1, t0);
621 tcg_gen_shli_i32(t1, t1, CRF_EQ);
622 tcg_gen_or_i32(cpu_crf[crf], cpu_crf[crf], t1);
623
624 tcg_temp_free(t0);
625 tcg_temp_free_i32(t1);
e1571908
AJ
626}
627
636aa200 628static inline void gen_op_cmpi(TCGv arg0, target_ulong arg1, int s, int crf)
e1571908 629{
2fdcb629 630 TCGv t0 = tcg_const_tl(arg1);
ea363694
AJ
631 gen_op_cmp(arg0, t0, s, crf);
632 tcg_temp_free(t0);
e1571908
AJ
633}
634
636aa200 635static inline void gen_op_cmp32(TCGv arg0, TCGv arg1, int s, int crf)
e1571908 636{
ea363694 637 TCGv t0, t1;
2fdcb629
RH
638 t0 = tcg_temp_new();
639 t1 = tcg_temp_new();
e1571908 640 if (s) {
ea363694
AJ
641 tcg_gen_ext32s_tl(t0, arg0);
642 tcg_gen_ext32s_tl(t1, arg1);
e1571908 643 } else {
ea363694
AJ
644 tcg_gen_ext32u_tl(t0, arg0);
645 tcg_gen_ext32u_tl(t1, arg1);
e1571908 646 }
ea363694
AJ
647 gen_op_cmp(t0, t1, s, crf);
648 tcg_temp_free(t1);
649 tcg_temp_free(t0);
e1571908
AJ
650}
651
636aa200 652static inline void gen_op_cmpi32(TCGv arg0, target_ulong arg1, int s, int crf)
e1571908 653{
2fdcb629 654 TCGv t0 = tcg_const_tl(arg1);
ea363694
AJ
655 gen_op_cmp32(arg0, t0, s, crf);
656 tcg_temp_free(t0);
e1571908 657}
e1571908 658
636aa200 659static inline void gen_set_Rc0(DisasContext *ctx, TCGv reg)
e1571908 660{
02765534 661 if (NARROW_MODE(ctx)) {
e1571908 662 gen_op_cmpi32(reg, 0, 1, 0);
02765534 663 } else {
e1571908 664 gen_op_cmpi(reg, 0, 1, 0);
02765534 665 }
e1571908
AJ
666}
667
668/* cmp */
99e300ef 669static void gen_cmp(DisasContext *ctx)
e1571908 670{
02765534 671 if (NARROW_MODE(ctx) || !(ctx->opcode & 0x00200000)) {
e1571908
AJ
672 gen_op_cmp32(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rB(ctx->opcode)],
673 1, crfD(ctx->opcode));
02765534 674 } else {
e1571908
AJ
675 gen_op_cmp(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rB(ctx->opcode)],
676 1, crfD(ctx->opcode));
02765534 677 }
e1571908
AJ
678}
679
680/* cmpi */
99e300ef 681static void gen_cmpi(DisasContext *ctx)
e1571908 682{
02765534 683 if (NARROW_MODE(ctx) || !(ctx->opcode & 0x00200000)) {
e1571908
AJ
684 gen_op_cmpi32(cpu_gpr[rA(ctx->opcode)], SIMM(ctx->opcode),
685 1, crfD(ctx->opcode));
02765534 686 } else {
e1571908
AJ
687 gen_op_cmpi(cpu_gpr[rA(ctx->opcode)], SIMM(ctx->opcode),
688 1, crfD(ctx->opcode));
02765534 689 }
e1571908
AJ
690}
691
692/* cmpl */
99e300ef 693static void gen_cmpl(DisasContext *ctx)
e1571908 694{
02765534 695 if (NARROW_MODE(ctx) || !(ctx->opcode & 0x00200000)) {
e1571908
AJ
696 gen_op_cmp32(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rB(ctx->opcode)],
697 0, crfD(ctx->opcode));
02765534 698 } else {
e1571908
AJ
699 gen_op_cmp(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rB(ctx->opcode)],
700 0, crfD(ctx->opcode));
02765534 701 }
e1571908
AJ
702}
703
704/* cmpli */
99e300ef 705static void gen_cmpli(DisasContext *ctx)
e1571908 706{
02765534 707 if (NARROW_MODE(ctx) || !(ctx->opcode & 0x00200000)) {
e1571908
AJ
708 gen_op_cmpi32(cpu_gpr[rA(ctx->opcode)], UIMM(ctx->opcode),
709 0, crfD(ctx->opcode));
02765534 710 } else {
e1571908
AJ
711 gen_op_cmpi(cpu_gpr[rA(ctx->opcode)], UIMM(ctx->opcode),
712 0, crfD(ctx->opcode));
02765534 713 }
e1571908
AJ
714}
715
716/* isel (PowerPC 2.03 specification) */
99e300ef 717static void gen_isel(DisasContext *ctx)
e1571908
AJ
718{
719 int l1, l2;
720 uint32_t bi = rC(ctx->opcode);
721 uint32_t mask;
a7812ae4 722 TCGv_i32 t0;
e1571908
AJ
723
724 l1 = gen_new_label();
725 l2 = gen_new_label();
726
727 mask = 1 << (3 - (bi & 0x03));
a7812ae4 728 t0 = tcg_temp_new_i32();
fea0c503
AJ
729 tcg_gen_andi_i32(t0, cpu_crf[bi >> 2], mask);
730 tcg_gen_brcondi_i32(TCG_COND_EQ, t0, 0, l1);
e1571908
AJ
731 if (rA(ctx->opcode) == 0)
732 tcg_gen_movi_tl(cpu_gpr[rD(ctx->opcode)], 0);
733 else
734 tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)]);
735 tcg_gen_br(l2);
736 gen_set_label(l1);
737 tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rB(ctx->opcode)]);
738 gen_set_label(l2);
a7812ae4 739 tcg_temp_free_i32(t0);
e1571908
AJ
740}
741
fcfda20f
AJ
742/* cmpb: PowerPC 2.05 specification */
743static void gen_cmpb(DisasContext *ctx)
744{
745 gen_helper_cmpb(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)],
746 cpu_gpr[rB(ctx->opcode)]);
747}
748
79aceca5 749/*** Integer arithmetic ***/
79aceca5 750
636aa200
BS
751static inline void gen_op_arith_compute_ov(DisasContext *ctx, TCGv arg0,
752 TCGv arg1, TCGv arg2, int sub)
74637406 753{
ffe30937 754 TCGv t0 = tcg_temp_new();
79aceca5 755
8e7a6db9 756 tcg_gen_xor_tl(cpu_ov, arg0, arg2);
74637406 757 tcg_gen_xor_tl(t0, arg1, arg2);
ffe30937
RH
758 if (sub) {
759 tcg_gen_and_tl(cpu_ov, cpu_ov, t0);
760 } else {
761 tcg_gen_andc_tl(cpu_ov, cpu_ov, t0);
762 }
763 tcg_temp_free(t0);
02765534 764 if (NARROW_MODE(ctx)) {
ffe30937
RH
765 tcg_gen_ext32s_tl(cpu_ov, cpu_ov);
766 }
ffe30937
RH
767 tcg_gen_shri_tl(cpu_ov, cpu_ov, TARGET_LONG_BITS - 1);
768 tcg_gen_or_tl(cpu_so, cpu_so, cpu_ov);
79aceca5
FB
769}
770
74637406 771/* Common add function */
636aa200 772static inline void gen_op_arith_add(DisasContext *ctx, TCGv ret, TCGv arg1,
b5a73f8d
RH
773 TCGv arg2, bool add_ca, bool compute_ca,
774 bool compute_ov, bool compute_rc0)
74637406 775{
b5a73f8d 776 TCGv t0 = ret;
d9bce9d9 777
752d634e 778 if (compute_ca || compute_ov) {
146de60d 779 t0 = tcg_temp_new();
74637406 780 }
79aceca5 781
da91a00f 782 if (compute_ca) {
79482e5a 783 if (NARROW_MODE(ctx)) {
752d634e
RH
784 /* Caution: a non-obvious corner case of the spec is that we
785 must produce the *entire* 64-bit addition, but produce the
786 carry into bit 32. */
79482e5a 787 TCGv t1 = tcg_temp_new();
752d634e
RH
788 tcg_gen_xor_tl(t1, arg1, arg2); /* add without carry */
789 tcg_gen_add_tl(t0, arg1, arg2);
79482e5a
RH
790 if (add_ca) {
791 tcg_gen_add_tl(t0, t0, cpu_ca);
792 }
752d634e
RH
793 tcg_gen_xor_tl(cpu_ca, t0, t1); /* bits changed w/ carry */
794 tcg_temp_free(t1);
795 tcg_gen_shri_tl(cpu_ca, cpu_ca, 32); /* extract bit 32 */
796 tcg_gen_andi_tl(cpu_ca, cpu_ca, 1);
b5a73f8d 797 } else {
79482e5a
RH
798 TCGv zero = tcg_const_tl(0);
799 if (add_ca) {
800 tcg_gen_add2_tl(t0, cpu_ca, arg1, zero, cpu_ca, zero);
801 tcg_gen_add2_tl(t0, cpu_ca, t0, cpu_ca, arg2, zero);
802 } else {
803 tcg_gen_add2_tl(t0, cpu_ca, arg1, zero, arg2, zero);
804 }
805 tcg_temp_free(zero);
b5a73f8d 806 }
b5a73f8d
RH
807 } else {
808 tcg_gen_add_tl(t0, arg1, arg2);
809 if (add_ca) {
810 tcg_gen_add_tl(t0, t0, cpu_ca);
811 }
da91a00f 812 }
79aceca5 813
74637406
AJ
814 if (compute_ov) {
815 gen_op_arith_compute_ov(ctx, t0, arg1, arg2, 0);
816 }
b5a73f8d 817 if (unlikely(compute_rc0)) {
74637406 818 gen_set_Rc0(ctx, t0);
b5a73f8d 819 }
74637406 820
a7812ae4 821 if (!TCGV_EQUAL(t0, ret)) {
74637406
AJ
822 tcg_gen_mov_tl(ret, t0);
823 tcg_temp_free(t0);
824 }
39dd32ee 825}
74637406
AJ
826/* Add functions with two operands */
827#define GEN_INT_ARITH_ADD(name, opc3, add_ca, compute_ca, compute_ov) \
b5a73f8d 828static void glue(gen_, name)(DisasContext *ctx) \
74637406
AJ
829{ \
830 gen_op_arith_add(ctx, cpu_gpr[rD(ctx->opcode)], \
831 cpu_gpr[rA(ctx->opcode)], cpu_gpr[rB(ctx->opcode)], \
b5a73f8d 832 add_ca, compute_ca, compute_ov, Rc(ctx->opcode)); \
74637406
AJ
833}
834/* Add functions with one operand and one immediate */
835#define GEN_INT_ARITH_ADD_CONST(name, opc3, const_val, \
836 add_ca, compute_ca, compute_ov) \
b5a73f8d 837static void glue(gen_, name)(DisasContext *ctx) \
74637406 838{ \
b5a73f8d 839 TCGv t0 = tcg_const_tl(const_val); \
74637406
AJ
840 gen_op_arith_add(ctx, cpu_gpr[rD(ctx->opcode)], \
841 cpu_gpr[rA(ctx->opcode)], t0, \
b5a73f8d 842 add_ca, compute_ca, compute_ov, Rc(ctx->opcode)); \
74637406
AJ
843 tcg_temp_free(t0); \
844}
845
846/* add add. addo addo. */
847GEN_INT_ARITH_ADD(add, 0x08, 0, 0, 0)
848GEN_INT_ARITH_ADD(addo, 0x18, 0, 0, 1)
849/* addc addc. addco addco. */
850GEN_INT_ARITH_ADD(addc, 0x00, 0, 1, 0)
851GEN_INT_ARITH_ADD(addco, 0x10, 0, 1, 1)
852/* adde adde. addeo addeo. */
853GEN_INT_ARITH_ADD(adde, 0x04, 1, 1, 0)
854GEN_INT_ARITH_ADD(addeo, 0x14, 1, 1, 1)
855/* addme addme. addmeo addmeo. */
856GEN_INT_ARITH_ADD_CONST(addme, 0x07, -1LL, 1, 1, 0)
857GEN_INT_ARITH_ADD_CONST(addmeo, 0x17, -1LL, 1, 1, 1)
858/* addze addze. addzeo addzeo.*/
859GEN_INT_ARITH_ADD_CONST(addze, 0x06, 0, 1, 1, 0)
860GEN_INT_ARITH_ADD_CONST(addzeo, 0x16, 0, 1, 1, 1)
861/* addi */
99e300ef 862static void gen_addi(DisasContext *ctx)
d9bce9d9 863{
74637406
AJ
864 target_long simm = SIMM(ctx->opcode);
865
866 if (rA(ctx->opcode) == 0) {
867 /* li case */
868 tcg_gen_movi_tl(cpu_gpr[rD(ctx->opcode)], simm);
869 } else {
b5a73f8d
RH
870 tcg_gen_addi_tl(cpu_gpr[rD(ctx->opcode)],
871 cpu_gpr[rA(ctx->opcode)], simm);
74637406 872 }
d9bce9d9 873}
74637406 874/* addic addic.*/
b5a73f8d 875static inline void gen_op_addic(DisasContext *ctx, bool compute_rc0)
d9bce9d9 876{
b5a73f8d
RH
877 TCGv c = tcg_const_tl(SIMM(ctx->opcode));
878 gen_op_arith_add(ctx, cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)],
879 c, 0, 1, 0, compute_rc0);
880 tcg_temp_free(c);
d9bce9d9 881}
99e300ef
BS
882
883static void gen_addic(DisasContext *ctx)
d9bce9d9 884{
b5a73f8d 885 gen_op_addic(ctx, 0);
d9bce9d9 886}
e8eaa2c0
BS
887
888static void gen_addic_(DisasContext *ctx)
d9bce9d9 889{
b5a73f8d 890 gen_op_addic(ctx, 1);
d9bce9d9 891}
99e300ef 892
54623277 893/* addis */
99e300ef 894static void gen_addis(DisasContext *ctx)
d9bce9d9 895{
74637406
AJ
896 target_long simm = SIMM(ctx->opcode);
897
898 if (rA(ctx->opcode) == 0) {
899 /* lis case */
900 tcg_gen_movi_tl(cpu_gpr[rD(ctx->opcode)], simm << 16);
901 } else {
b5a73f8d
RH
902 tcg_gen_addi_tl(cpu_gpr[rD(ctx->opcode)],
903 cpu_gpr[rA(ctx->opcode)], simm << 16);
74637406 904 }
d9bce9d9 905}
74637406 906
636aa200
BS
907static inline void gen_op_arith_divw(DisasContext *ctx, TCGv ret, TCGv arg1,
908 TCGv arg2, int sign, int compute_ov)
d9bce9d9 909{
2ef1b120
AJ
910 int l1 = gen_new_label();
911 int l2 = gen_new_label();
a7812ae4
PB
912 TCGv_i32 t0 = tcg_temp_local_new_i32();
913 TCGv_i32 t1 = tcg_temp_local_new_i32();
74637406 914
2ef1b120
AJ
915 tcg_gen_trunc_tl_i32(t0, arg1);
916 tcg_gen_trunc_tl_i32(t1, arg2);
917 tcg_gen_brcondi_i32(TCG_COND_EQ, t1, 0, l1);
74637406 918 if (sign) {
2ef1b120
AJ
919 int l3 = gen_new_label();
920 tcg_gen_brcondi_i32(TCG_COND_NE, t1, -1, l3);
921 tcg_gen_brcondi_i32(TCG_COND_EQ, t0, INT32_MIN, l1);
74637406 922 gen_set_label(l3);
2ef1b120 923 tcg_gen_div_i32(t0, t0, t1);
74637406 924 } else {
2ef1b120 925 tcg_gen_divu_i32(t0, t0, t1);
74637406
AJ
926 }
927 if (compute_ov) {
da91a00f 928 tcg_gen_movi_tl(cpu_ov, 0);
74637406
AJ
929 }
930 tcg_gen_br(l2);
931 gen_set_label(l1);
932 if (sign) {
2ef1b120 933 tcg_gen_sari_i32(t0, t0, 31);
74637406
AJ
934 } else {
935 tcg_gen_movi_i32(t0, 0);
936 }
937 if (compute_ov) {
da91a00f
RH
938 tcg_gen_movi_tl(cpu_ov, 1);
939 tcg_gen_movi_tl(cpu_so, 1);
74637406
AJ
940 }
941 gen_set_label(l2);
2ef1b120 942 tcg_gen_extu_i32_tl(ret, t0);
a7812ae4
PB
943 tcg_temp_free_i32(t0);
944 tcg_temp_free_i32(t1);
74637406
AJ
945 if (unlikely(Rc(ctx->opcode) != 0))
946 gen_set_Rc0(ctx, ret);
d9bce9d9 947}
74637406
AJ
948/* Div functions */
949#define GEN_INT_ARITH_DIVW(name, opc3, sign, compute_ov) \
99e300ef 950static void glue(gen_, name)(DisasContext *ctx) \
74637406
AJ
951{ \
952 gen_op_arith_divw(ctx, cpu_gpr[rD(ctx->opcode)], \
953 cpu_gpr[rA(ctx->opcode)], cpu_gpr[rB(ctx->opcode)], \
954 sign, compute_ov); \
955}
956/* divwu divwu. divwuo divwuo. */
957GEN_INT_ARITH_DIVW(divwu, 0x0E, 0, 0);
958GEN_INT_ARITH_DIVW(divwuo, 0x1E, 0, 1);
959/* divw divw. divwo divwo. */
960GEN_INT_ARITH_DIVW(divw, 0x0F, 1, 0);
961GEN_INT_ARITH_DIVW(divwo, 0x1F, 1, 1);
d9bce9d9 962#if defined(TARGET_PPC64)
636aa200
BS
963static inline void gen_op_arith_divd(DisasContext *ctx, TCGv ret, TCGv arg1,
964 TCGv arg2, int sign, int compute_ov)
d9bce9d9 965{
2ef1b120
AJ
966 int l1 = gen_new_label();
967 int l2 = gen_new_label();
74637406
AJ
968
969 tcg_gen_brcondi_i64(TCG_COND_EQ, arg2, 0, l1);
970 if (sign) {
2ef1b120 971 int l3 = gen_new_label();
74637406
AJ
972 tcg_gen_brcondi_i64(TCG_COND_NE, arg2, -1, l3);
973 tcg_gen_brcondi_i64(TCG_COND_EQ, arg1, INT64_MIN, l1);
974 gen_set_label(l3);
74637406
AJ
975 tcg_gen_div_i64(ret, arg1, arg2);
976 } else {
977 tcg_gen_divu_i64(ret, arg1, arg2);
978 }
979 if (compute_ov) {
da91a00f 980 tcg_gen_movi_tl(cpu_ov, 0);
74637406
AJ
981 }
982 tcg_gen_br(l2);
983 gen_set_label(l1);
984 if (sign) {
985 tcg_gen_sari_i64(ret, arg1, 63);
986 } else {
987 tcg_gen_movi_i64(ret, 0);
988 }
989 if (compute_ov) {
da91a00f
RH
990 tcg_gen_movi_tl(cpu_ov, 1);
991 tcg_gen_movi_tl(cpu_so, 1);
74637406
AJ
992 }
993 gen_set_label(l2);
994 if (unlikely(Rc(ctx->opcode) != 0))
995 gen_set_Rc0(ctx, ret);
d9bce9d9 996}
74637406 997#define GEN_INT_ARITH_DIVD(name, opc3, sign, compute_ov) \
99e300ef 998static void glue(gen_, name)(DisasContext *ctx) \
74637406 999{ \
2ef1b120
AJ
1000 gen_op_arith_divd(ctx, cpu_gpr[rD(ctx->opcode)], \
1001 cpu_gpr[rA(ctx->opcode)], cpu_gpr[rB(ctx->opcode)], \
1002 sign, compute_ov); \
74637406
AJ
1003}
1004/* divwu divwu. divwuo divwuo. */
1005GEN_INT_ARITH_DIVD(divdu, 0x0E, 0, 0);
1006GEN_INT_ARITH_DIVD(divduo, 0x1E, 0, 1);
1007/* divw divw. divwo divwo. */
1008GEN_INT_ARITH_DIVD(divd, 0x0F, 1, 0);
1009GEN_INT_ARITH_DIVD(divdo, 0x1F, 1, 1);
d9bce9d9 1010#endif
74637406
AJ
1011
1012/* mulhw mulhw. */
99e300ef 1013static void gen_mulhw(DisasContext *ctx)
d9bce9d9 1014{
23ad1d5d
RH
1015 TCGv_i32 t0 = tcg_temp_new_i32();
1016 TCGv_i32 t1 = tcg_temp_new_i32();
74637406 1017
23ad1d5d
RH
1018 tcg_gen_trunc_tl_i32(t0, cpu_gpr[rA(ctx->opcode)]);
1019 tcg_gen_trunc_tl_i32(t1, cpu_gpr[rB(ctx->opcode)]);
1020 tcg_gen_muls2_i32(t0, t1, t0, t1);
1021 tcg_gen_extu_i32_tl(cpu_gpr[rD(ctx->opcode)], t1);
1022 tcg_temp_free_i32(t0);
1023 tcg_temp_free_i32(t1);
74637406
AJ
1024 if (unlikely(Rc(ctx->opcode) != 0))
1025 gen_set_Rc0(ctx, cpu_gpr[rD(ctx->opcode)]);
d9bce9d9 1026}
99e300ef 1027
54623277 1028/* mulhwu mulhwu. */
99e300ef 1029static void gen_mulhwu(DisasContext *ctx)
d9bce9d9 1030{
23ad1d5d
RH
1031 TCGv_i32 t0 = tcg_temp_new_i32();
1032 TCGv_i32 t1 = tcg_temp_new_i32();
74637406 1033
23ad1d5d
RH
1034 tcg_gen_trunc_tl_i32(t0, cpu_gpr[rA(ctx->opcode)]);
1035 tcg_gen_trunc_tl_i32(t1, cpu_gpr[rB(ctx->opcode)]);
1036 tcg_gen_mulu2_i32(t0, t1, t0, t1);
1037 tcg_gen_extu_i32_tl(cpu_gpr[rD(ctx->opcode)], t1);
1038 tcg_temp_free_i32(t0);
1039 tcg_temp_free_i32(t1);
74637406
AJ
1040 if (unlikely(Rc(ctx->opcode) != 0))
1041 gen_set_Rc0(ctx, cpu_gpr[rD(ctx->opcode)]);
d9bce9d9 1042}
99e300ef 1043
54623277 1044/* mullw mullw. */
99e300ef 1045static void gen_mullw(DisasContext *ctx)
d9bce9d9 1046{
74637406
AJ
1047 tcg_gen_mul_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)],
1048 cpu_gpr[rB(ctx->opcode)]);
1e4c090f 1049 tcg_gen_ext32s_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rD(ctx->opcode)]);
74637406
AJ
1050 if (unlikely(Rc(ctx->opcode) != 0))
1051 gen_set_Rc0(ctx, cpu_gpr[rD(ctx->opcode)]);
d9bce9d9 1052}
99e300ef 1053
54623277 1054/* mullwo mullwo. */
99e300ef 1055static void gen_mullwo(DisasContext *ctx)
d9bce9d9 1056{
e4a2c846
RH
1057 TCGv_i32 t0 = tcg_temp_new_i32();
1058 TCGv_i32 t1 = tcg_temp_new_i32();
74637406 1059
e4a2c846
RH
1060 tcg_gen_trunc_tl_i32(t0, cpu_gpr[rA(ctx->opcode)]);
1061 tcg_gen_trunc_tl_i32(t1, cpu_gpr[rB(ctx->opcode)]);
1062 tcg_gen_muls2_i32(t0, t1, t0, t1);
1063 tcg_gen_ext_i32_tl(cpu_gpr[rD(ctx->opcode)], t0);
1064
1065 tcg_gen_sari_i32(t0, t0, 31);
1066 tcg_gen_setcond_i32(TCG_COND_NE, t0, t0, t1);
1067 tcg_gen_extu_i32_tl(cpu_ov, t0);
1068 tcg_gen_or_tl(cpu_so, cpu_so, cpu_ov);
1069
1070 tcg_temp_free_i32(t0);
1071 tcg_temp_free_i32(t1);
74637406
AJ
1072 if (unlikely(Rc(ctx->opcode) != 0))
1073 gen_set_Rc0(ctx, cpu_gpr[rD(ctx->opcode)]);
d9bce9d9 1074}
99e300ef 1075
54623277 1076/* mulli */
99e300ef 1077static void gen_mulli(DisasContext *ctx)
d9bce9d9 1078{
74637406
AJ
1079 tcg_gen_muli_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)],
1080 SIMM(ctx->opcode));
d9bce9d9 1081}
23ad1d5d 1082
d9bce9d9 1083#if defined(TARGET_PPC64)
74637406 1084/* mulhd mulhd. */
23ad1d5d
RH
1085static void gen_mulhd(DisasContext *ctx)
1086{
1087 TCGv lo = tcg_temp_new();
1088 tcg_gen_muls2_tl(lo, cpu_gpr[rD(ctx->opcode)],
1089 cpu_gpr[rA(ctx->opcode)], cpu_gpr[rB(ctx->opcode)]);
1090 tcg_temp_free(lo);
1091 if (unlikely(Rc(ctx->opcode) != 0)) {
1092 gen_set_Rc0(ctx, cpu_gpr[rD(ctx->opcode)]);
1093 }
1094}
1095
74637406 1096/* mulhdu mulhdu. */
23ad1d5d
RH
1097static void gen_mulhdu(DisasContext *ctx)
1098{
1099 TCGv lo = tcg_temp_new();
1100 tcg_gen_mulu2_tl(lo, cpu_gpr[rD(ctx->opcode)],
1101 cpu_gpr[rA(ctx->opcode)], cpu_gpr[rB(ctx->opcode)]);
1102 tcg_temp_free(lo);
1103 if (unlikely(Rc(ctx->opcode) != 0)) {
1104 gen_set_Rc0(ctx, cpu_gpr[rD(ctx->opcode)]);
1105 }
1106}
99e300ef 1107
54623277 1108/* mulld mulld. */
99e300ef 1109static void gen_mulld(DisasContext *ctx)
d9bce9d9 1110{
74637406
AJ
1111 tcg_gen_mul_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)],
1112 cpu_gpr[rB(ctx->opcode)]);
1113 if (unlikely(Rc(ctx->opcode) != 0))
1114 gen_set_Rc0(ctx, cpu_gpr[rD(ctx->opcode)]);
d9bce9d9 1115}
d15f74fb 1116
74637406 1117/* mulldo mulldo. */
d15f74fb
BS
1118static void gen_mulldo(DisasContext *ctx)
1119{
1120 gen_helper_mulldo(cpu_gpr[rD(ctx->opcode)], cpu_env,
1121 cpu_gpr[rA(ctx->opcode)], cpu_gpr[rB(ctx->opcode)]);
1122 if (unlikely(Rc(ctx->opcode) != 0)) {
1123 gen_set_Rc0(ctx, cpu_gpr[rD(ctx->opcode)]);
1124 }
1125}
d9bce9d9 1126#endif
74637406 1127
74637406 1128/* Common subf function */
636aa200 1129static inline void gen_op_arith_subf(DisasContext *ctx, TCGv ret, TCGv arg1,
b5a73f8d
RH
1130 TCGv arg2, bool add_ca, bool compute_ca,
1131 bool compute_ov, bool compute_rc0)
79aceca5 1132{
b5a73f8d 1133 TCGv t0 = ret;
79aceca5 1134
752d634e 1135 if (compute_ca || compute_ov) {
b5a73f8d 1136 t0 = tcg_temp_new();
da91a00f 1137 }
74637406 1138
79482e5a
RH
1139 if (compute_ca) {
1140 /* dest = ~arg1 + arg2 [+ ca]. */
1141 if (NARROW_MODE(ctx)) {
752d634e
RH
1142 /* Caution: a non-obvious corner case of the spec is that we
1143 must produce the *entire* 64-bit addition, but produce the
1144 carry into bit 32. */
79482e5a 1145 TCGv inv1 = tcg_temp_new();
752d634e 1146 TCGv t1 = tcg_temp_new();
79482e5a 1147 tcg_gen_not_tl(inv1, arg1);
79482e5a 1148 if (add_ca) {
752d634e 1149 tcg_gen_add_tl(t0, arg2, cpu_ca);
79482e5a 1150 } else {
752d634e 1151 tcg_gen_addi_tl(t0, arg2, 1);
79482e5a 1152 }
752d634e 1153 tcg_gen_xor_tl(t1, arg2, inv1); /* add without carry */
79482e5a 1154 tcg_gen_add_tl(t0, t0, inv1);
752d634e
RH
1155 tcg_gen_xor_tl(cpu_ca, t0, t1); /* bits changes w/ carry */
1156 tcg_temp_free(t1);
1157 tcg_gen_shri_tl(cpu_ca, cpu_ca, 32); /* extract bit 32 */
1158 tcg_gen_andi_tl(cpu_ca, cpu_ca, 1);
79482e5a 1159 } else if (add_ca) {
08f4a0f7
RH
1160 TCGv zero, inv1 = tcg_temp_new();
1161 tcg_gen_not_tl(inv1, arg1);
b5a73f8d
RH
1162 zero = tcg_const_tl(0);
1163 tcg_gen_add2_tl(t0, cpu_ca, arg2, zero, cpu_ca, zero);
08f4a0f7 1164 tcg_gen_add2_tl(t0, cpu_ca, t0, cpu_ca, inv1, zero);
b5a73f8d 1165 tcg_temp_free(zero);
08f4a0f7 1166 tcg_temp_free(inv1);
b5a73f8d 1167 } else {
79482e5a 1168 tcg_gen_setcond_tl(TCG_COND_GEU, cpu_ca, arg2, arg1);
b5a73f8d 1169 tcg_gen_sub_tl(t0, arg2, arg1);
b5a73f8d 1170 }
79482e5a
RH
1171 } else if (add_ca) {
1172 /* Since we're ignoring carry-out, we can simplify the
1173 standard ~arg1 + arg2 + ca to arg2 - arg1 + ca - 1. */
1174 tcg_gen_sub_tl(t0, arg2, arg1);
1175 tcg_gen_add_tl(t0, t0, cpu_ca);
1176 tcg_gen_subi_tl(t0, t0, 1);
79aceca5 1177 } else {
b5a73f8d 1178 tcg_gen_sub_tl(t0, arg2, arg1);
74637406 1179 }
b5a73f8d 1180
74637406
AJ
1181 if (compute_ov) {
1182 gen_op_arith_compute_ov(ctx, t0, arg1, arg2, 1);
1183 }
b5a73f8d 1184 if (unlikely(compute_rc0)) {
74637406 1185 gen_set_Rc0(ctx, t0);
b5a73f8d 1186 }
74637406 1187
a7812ae4 1188 if (!TCGV_EQUAL(t0, ret)) {
74637406
AJ
1189 tcg_gen_mov_tl(ret, t0);
1190 tcg_temp_free(t0);
79aceca5 1191 }
79aceca5 1192}
74637406
AJ
1193/* Sub functions with Two operands functions */
1194#define GEN_INT_ARITH_SUBF(name, opc3, add_ca, compute_ca, compute_ov) \
b5a73f8d 1195static void glue(gen_, name)(DisasContext *ctx) \
74637406
AJ
1196{ \
1197 gen_op_arith_subf(ctx, cpu_gpr[rD(ctx->opcode)], \
1198 cpu_gpr[rA(ctx->opcode)], cpu_gpr[rB(ctx->opcode)], \
b5a73f8d 1199 add_ca, compute_ca, compute_ov, Rc(ctx->opcode)); \
74637406
AJ
1200}
1201/* Sub functions with one operand and one immediate */
1202#define GEN_INT_ARITH_SUBF_CONST(name, opc3, const_val, \
1203 add_ca, compute_ca, compute_ov) \
b5a73f8d 1204static void glue(gen_, name)(DisasContext *ctx) \
74637406 1205{ \
b5a73f8d 1206 TCGv t0 = tcg_const_tl(const_val); \
74637406
AJ
1207 gen_op_arith_subf(ctx, cpu_gpr[rD(ctx->opcode)], \
1208 cpu_gpr[rA(ctx->opcode)], t0, \
b5a73f8d 1209 add_ca, compute_ca, compute_ov, Rc(ctx->opcode)); \
74637406
AJ
1210 tcg_temp_free(t0); \
1211}
1212/* subf subf. subfo subfo. */
1213GEN_INT_ARITH_SUBF(subf, 0x01, 0, 0, 0)
1214GEN_INT_ARITH_SUBF(subfo, 0x11, 0, 0, 1)
1215/* subfc subfc. subfco subfco. */
1216GEN_INT_ARITH_SUBF(subfc, 0x00, 0, 1, 0)
1217GEN_INT_ARITH_SUBF(subfco, 0x10, 0, 1, 1)
1218/* subfe subfe. subfeo subfo. */
1219GEN_INT_ARITH_SUBF(subfe, 0x04, 1, 1, 0)
1220GEN_INT_ARITH_SUBF(subfeo, 0x14, 1, 1, 1)
1221/* subfme subfme. subfmeo subfmeo. */
1222GEN_INT_ARITH_SUBF_CONST(subfme, 0x07, -1LL, 1, 1, 0)
1223GEN_INT_ARITH_SUBF_CONST(subfmeo, 0x17, -1LL, 1, 1, 1)
1224/* subfze subfze. subfzeo subfzeo.*/
1225GEN_INT_ARITH_SUBF_CONST(subfze, 0x06, 0, 1, 1, 0)
1226GEN_INT_ARITH_SUBF_CONST(subfzeo, 0x16, 0, 1, 1, 1)
99e300ef 1227
54623277 1228/* subfic */
99e300ef 1229static void gen_subfic(DisasContext *ctx)
79aceca5 1230{
b5a73f8d
RH
1231 TCGv c = tcg_const_tl(SIMM(ctx->opcode));
1232 gen_op_arith_subf(ctx, cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)],
1233 c, 0, 1, 0, 0);
1234 tcg_temp_free(c);
79aceca5
FB
1235}
1236
fd3f0081
RH
1237/* neg neg. nego nego. */
1238static inline void gen_op_arith_neg(DisasContext *ctx, bool compute_ov)
1239{
1240 TCGv zero = tcg_const_tl(0);
1241 gen_op_arith_subf(ctx, cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)],
1242 zero, 0, 0, compute_ov, Rc(ctx->opcode));
1243 tcg_temp_free(zero);
1244}
1245
1246static void gen_neg(DisasContext *ctx)
1247{
1248 gen_op_arith_neg(ctx, 0);
1249}
1250
1251static void gen_nego(DisasContext *ctx)
1252{
1253 gen_op_arith_neg(ctx, 1);
1254}
1255
79aceca5 1256/*** Integer logical ***/
26d67362 1257#define GEN_LOGICAL2(name, tcg_op, opc, type) \
99e300ef 1258static void glue(gen_, name)(DisasContext *ctx) \
79aceca5 1259{ \
26d67362
AJ
1260 tcg_op(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)], \
1261 cpu_gpr[rB(ctx->opcode)]); \
76a66253 1262 if (unlikely(Rc(ctx->opcode) != 0)) \
26d67362 1263 gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]); \
79aceca5 1264}
79aceca5 1265
26d67362 1266#define GEN_LOGICAL1(name, tcg_op, opc, type) \
99e300ef 1267static void glue(gen_, name)(DisasContext *ctx) \
79aceca5 1268{ \
26d67362 1269 tcg_op(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)]); \
76a66253 1270 if (unlikely(Rc(ctx->opcode) != 0)) \
26d67362 1271 gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]); \
79aceca5
FB
1272}
1273
1274/* and & and. */
26d67362 1275GEN_LOGICAL2(and, tcg_gen_and_tl, 0x00, PPC_INTEGER);
79aceca5 1276/* andc & andc. */
26d67362 1277GEN_LOGICAL2(andc, tcg_gen_andc_tl, 0x01, PPC_INTEGER);
e8eaa2c0 1278
54623277 1279/* andi. */
e8eaa2c0 1280static void gen_andi_(DisasContext *ctx)
79aceca5 1281{
26d67362
AJ
1282 tcg_gen_andi_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)], UIMM(ctx->opcode));
1283 gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
79aceca5 1284}
e8eaa2c0 1285
54623277 1286/* andis. */
e8eaa2c0 1287static void gen_andis_(DisasContext *ctx)
79aceca5 1288{
26d67362
AJ
1289 tcg_gen_andi_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)], UIMM(ctx->opcode) << 16);
1290 gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
79aceca5 1291}
99e300ef 1292
54623277 1293/* cntlzw */
99e300ef 1294static void gen_cntlzw(DisasContext *ctx)
26d67362 1295{
a7812ae4 1296 gen_helper_cntlzw(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)]);
26d67362 1297 if (unlikely(Rc(ctx->opcode) != 0))
2e31f5d3 1298 gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
26d67362 1299}
79aceca5 1300/* eqv & eqv. */
26d67362 1301GEN_LOGICAL2(eqv, tcg_gen_eqv_tl, 0x08, PPC_INTEGER);
79aceca5 1302/* extsb & extsb. */
26d67362 1303GEN_LOGICAL1(extsb, tcg_gen_ext8s_tl, 0x1D, PPC_INTEGER);
79aceca5 1304/* extsh & extsh. */
26d67362 1305GEN_LOGICAL1(extsh, tcg_gen_ext16s_tl, 0x1C, PPC_INTEGER);
79aceca5 1306/* nand & nand. */
26d67362 1307GEN_LOGICAL2(nand, tcg_gen_nand_tl, 0x0E, PPC_INTEGER);
79aceca5 1308/* nor & nor. */
26d67362 1309GEN_LOGICAL2(nor, tcg_gen_nor_tl, 0x03, PPC_INTEGER);
99e300ef 1310
54623277 1311/* or & or. */
99e300ef 1312static void gen_or(DisasContext *ctx)
9a64fbe4 1313{
76a66253
JM
1314 int rs, ra, rb;
1315
1316 rs = rS(ctx->opcode);
1317 ra = rA(ctx->opcode);
1318 rb = rB(ctx->opcode);
1319 /* Optimisation for mr. ri case */
1320 if (rs != ra || rs != rb) {
26d67362
AJ
1321 if (rs != rb)
1322 tcg_gen_or_tl(cpu_gpr[ra], cpu_gpr[rs], cpu_gpr[rb]);
1323 else
1324 tcg_gen_mov_tl(cpu_gpr[ra], cpu_gpr[rs]);
76a66253 1325 if (unlikely(Rc(ctx->opcode) != 0))
26d67362 1326 gen_set_Rc0(ctx, cpu_gpr[ra]);
76a66253 1327 } else if (unlikely(Rc(ctx->opcode) != 0)) {
26d67362 1328 gen_set_Rc0(ctx, cpu_gpr[rs]);
c80f84e3
JM
1329#if defined(TARGET_PPC64)
1330 } else {
26d67362
AJ
1331 int prio = 0;
1332
c80f84e3
JM
1333 switch (rs) {
1334 case 1:
1335 /* Set process priority to low */
26d67362 1336 prio = 2;
c80f84e3
JM
1337 break;
1338 case 6:
1339 /* Set process priority to medium-low */
26d67362 1340 prio = 3;
c80f84e3
JM
1341 break;
1342 case 2:
1343 /* Set process priority to normal */
26d67362 1344 prio = 4;
c80f84e3 1345 break;
be147d08
JM
1346#if !defined(CONFIG_USER_ONLY)
1347 case 31:
76db3ba4 1348 if (ctx->mem_idx > 0) {
be147d08 1349 /* Set process priority to very low */
26d67362 1350 prio = 1;
be147d08
JM
1351 }
1352 break;
1353 case 5:
76db3ba4 1354 if (ctx->mem_idx > 0) {
be147d08 1355 /* Set process priority to medium-hight */
26d67362 1356 prio = 5;
be147d08
JM
1357 }
1358 break;
1359 case 3:
76db3ba4 1360 if (ctx->mem_idx > 0) {
be147d08 1361 /* Set process priority to high */
26d67362 1362 prio = 6;
be147d08
JM
1363 }
1364 break;
be147d08 1365 case 7:
76db3ba4 1366 if (ctx->mem_idx > 1) {
be147d08 1367 /* Set process priority to very high */
26d67362 1368 prio = 7;
be147d08
JM
1369 }
1370 break;
be147d08 1371#endif
c80f84e3
JM
1372 default:
1373 /* nop */
1374 break;
1375 }
26d67362 1376 if (prio) {
a7812ae4 1377 TCGv t0 = tcg_temp_new();
54cdcae6 1378 gen_load_spr(t0, SPR_PPR);
ea363694
AJ
1379 tcg_gen_andi_tl(t0, t0, ~0x001C000000000000ULL);
1380 tcg_gen_ori_tl(t0, t0, ((uint64_t)prio) << 50);
54cdcae6 1381 gen_store_spr(SPR_PPR, t0);
ea363694 1382 tcg_temp_free(t0);
26d67362 1383 }
c80f84e3 1384#endif
9a64fbe4 1385 }
9a64fbe4 1386}
79aceca5 1387/* orc & orc. */
26d67362 1388GEN_LOGICAL2(orc, tcg_gen_orc_tl, 0x0C, PPC_INTEGER);
99e300ef 1389
54623277 1390/* xor & xor. */
99e300ef 1391static void gen_xor(DisasContext *ctx)
9a64fbe4 1392{
9a64fbe4 1393 /* Optimisation for "set to zero" case */
26d67362 1394 if (rS(ctx->opcode) != rB(ctx->opcode))
312179c4 1395 tcg_gen_xor_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)], cpu_gpr[rB(ctx->opcode)]);
26d67362
AJ
1396 else
1397 tcg_gen_movi_tl(cpu_gpr[rA(ctx->opcode)], 0);
76a66253 1398 if (unlikely(Rc(ctx->opcode) != 0))
26d67362 1399 gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
9a64fbe4 1400}
99e300ef 1401
54623277 1402/* ori */
99e300ef 1403static void gen_ori(DisasContext *ctx)
79aceca5 1404{
76a66253 1405 target_ulong uimm = UIMM(ctx->opcode);
79aceca5 1406
9a64fbe4
FB
1407 if (rS(ctx->opcode) == rA(ctx->opcode) && uimm == 0) {
1408 /* NOP */
76a66253 1409 /* XXX: should handle special NOPs for POWER series */
9a64fbe4 1410 return;
76a66253 1411 }
26d67362 1412 tcg_gen_ori_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)], uimm);
79aceca5 1413}
99e300ef 1414
54623277 1415/* oris */
99e300ef 1416static void gen_oris(DisasContext *ctx)
79aceca5 1417{
76a66253 1418 target_ulong uimm = UIMM(ctx->opcode);
79aceca5 1419
9a64fbe4
FB
1420 if (rS(ctx->opcode) == rA(ctx->opcode) && uimm == 0) {
1421 /* NOP */
1422 return;
76a66253 1423 }
26d67362 1424 tcg_gen_ori_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)], uimm << 16);
79aceca5 1425}
99e300ef 1426
54623277 1427/* xori */
99e300ef 1428static void gen_xori(DisasContext *ctx)
79aceca5 1429{
76a66253 1430 target_ulong uimm = UIMM(ctx->opcode);
9a64fbe4
FB
1431
1432 if (rS(ctx->opcode) == rA(ctx->opcode) && uimm == 0) {
1433 /* NOP */
1434 return;
1435 }
26d67362 1436 tcg_gen_xori_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)], uimm);
79aceca5 1437}
99e300ef 1438
54623277 1439/* xoris */
99e300ef 1440static void gen_xoris(DisasContext *ctx)
79aceca5 1441{
76a66253 1442 target_ulong uimm = UIMM(ctx->opcode);
9a64fbe4
FB
1443
1444 if (rS(ctx->opcode) == rA(ctx->opcode) && uimm == 0) {
1445 /* NOP */
1446 return;
1447 }
26d67362 1448 tcg_gen_xori_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)], uimm << 16);
79aceca5 1449}
99e300ef 1450
54623277 1451/* popcntb : PowerPC 2.03 specification */
99e300ef 1452static void gen_popcntb(DisasContext *ctx)
d9bce9d9 1453{
eaabeef2
DG
1454 gen_helper_popcntb(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)]);
1455}
1456
1457static void gen_popcntw(DisasContext *ctx)
1458{
1459 gen_helper_popcntw(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)]);
1460}
1461
d9bce9d9 1462#if defined(TARGET_PPC64)
eaabeef2
DG
1463/* popcntd: PowerPC 2.06 specification */
1464static void gen_popcntd(DisasContext *ctx)
1465{
1466 gen_helper_popcntd(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)]);
d9bce9d9 1467}
eaabeef2 1468#endif
d9bce9d9 1469
725bcec2
AJ
1470/* prtyw: PowerPC 2.05 specification */
1471static void gen_prtyw(DisasContext *ctx)
1472{
1473 TCGv ra = cpu_gpr[rA(ctx->opcode)];
1474 TCGv rs = cpu_gpr[rS(ctx->opcode)];
1475 TCGv t0 = tcg_temp_new();
1476 tcg_gen_shri_tl(t0, rs, 16);
1477 tcg_gen_xor_tl(ra, rs, t0);
1478 tcg_gen_shri_tl(t0, ra, 8);
1479 tcg_gen_xor_tl(ra, ra, t0);
1480 tcg_gen_andi_tl(ra, ra, (target_ulong)0x100000001ULL);
1481 tcg_temp_free(t0);
1482}
1483
1484#if defined(TARGET_PPC64)
1485/* prtyd: PowerPC 2.05 specification */
1486static void gen_prtyd(DisasContext *ctx)
1487{
1488 TCGv ra = cpu_gpr[rA(ctx->opcode)];
1489 TCGv rs = cpu_gpr[rS(ctx->opcode)];
1490 TCGv t0 = tcg_temp_new();
1491 tcg_gen_shri_tl(t0, rs, 32);
1492 tcg_gen_xor_tl(ra, rs, t0);
1493 tcg_gen_shri_tl(t0, ra, 16);
1494 tcg_gen_xor_tl(ra, ra, t0);
1495 tcg_gen_shri_tl(t0, ra, 8);
1496 tcg_gen_xor_tl(ra, ra, t0);
1497 tcg_gen_andi_tl(ra, ra, 1);
1498 tcg_temp_free(t0);
1499}
1500#endif
1501
d9bce9d9
JM
1502#if defined(TARGET_PPC64)
1503/* extsw & extsw. */
26d67362 1504GEN_LOGICAL1(extsw, tcg_gen_ext32s_tl, 0x1E, PPC_64B);
99e300ef 1505
54623277 1506/* cntlzd */
99e300ef 1507static void gen_cntlzd(DisasContext *ctx)
26d67362 1508{
a7812ae4 1509 gen_helper_cntlzd(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)]);
26d67362
AJ
1510 if (unlikely(Rc(ctx->opcode) != 0))
1511 gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
1512}
d9bce9d9
JM
1513#endif
1514
79aceca5 1515/*** Integer rotate ***/
99e300ef 1516
54623277 1517/* rlwimi & rlwimi. */
99e300ef 1518static void gen_rlwimi(DisasContext *ctx)
79aceca5 1519{
76a66253 1520 uint32_t mb, me, sh;
79aceca5
FB
1521
1522 mb = MB(ctx->opcode);
1523 me = ME(ctx->opcode);
76a66253 1524 sh = SH(ctx->opcode);
d03ef511
AJ
1525 if (likely(sh == 0 && mb == 0 && me == 31)) {
1526 tcg_gen_ext32u_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)]);
1527 } else {
d03ef511 1528 target_ulong mask;
a7812ae4
PB
1529 TCGv t1;
1530 TCGv t0 = tcg_temp_new();
54843a58 1531#if defined(TARGET_PPC64)
a7812ae4
PB
1532 TCGv_i32 t2 = tcg_temp_new_i32();
1533 tcg_gen_trunc_i64_i32(t2, cpu_gpr[rS(ctx->opcode)]);
1534 tcg_gen_rotli_i32(t2, t2, sh);
1535 tcg_gen_extu_i32_i64(t0, t2);
1536 tcg_temp_free_i32(t2);
54843a58
AJ
1537#else
1538 tcg_gen_rotli_i32(t0, cpu_gpr[rS(ctx->opcode)], sh);
1539#endif
76a66253 1540#if defined(TARGET_PPC64)
d03ef511
AJ
1541 mb += 32;
1542 me += 32;
76a66253 1543#endif
d03ef511 1544 mask = MASK(mb, me);
a7812ae4 1545 t1 = tcg_temp_new();
d03ef511
AJ
1546 tcg_gen_andi_tl(t0, t0, mask);
1547 tcg_gen_andi_tl(t1, cpu_gpr[rA(ctx->opcode)], ~mask);
1548 tcg_gen_or_tl(cpu_gpr[rA(ctx->opcode)], t0, t1);
1549 tcg_temp_free(t0);
1550 tcg_temp_free(t1);
1551 }
76a66253 1552 if (unlikely(Rc(ctx->opcode) != 0))
d03ef511 1553 gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
79aceca5 1554}
99e300ef 1555
54623277 1556/* rlwinm & rlwinm. */
99e300ef 1557static void gen_rlwinm(DisasContext *ctx)
79aceca5
FB
1558{
1559 uint32_t mb, me, sh;
3b46e624 1560
79aceca5
FB
1561 sh = SH(ctx->opcode);
1562 mb = MB(ctx->opcode);
1563 me = ME(ctx->opcode);
d03ef511
AJ
1564
1565 if (likely(mb == 0 && me == (31 - sh))) {
1566 if (likely(sh == 0)) {
1567 tcg_gen_ext32u_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)]);
1568 } else {
a7812ae4 1569 TCGv t0 = tcg_temp_new();
d03ef511
AJ
1570 tcg_gen_ext32u_tl(t0, cpu_gpr[rS(ctx->opcode)]);
1571 tcg_gen_shli_tl(t0, t0, sh);
1572 tcg_gen_ext32u_tl(cpu_gpr[rA(ctx->opcode)], t0);
1573 tcg_temp_free(t0);
79aceca5 1574 }
d03ef511 1575 } else if (likely(sh != 0 && me == 31 && sh == (32 - mb))) {
a7812ae4 1576 TCGv t0 = tcg_temp_new();
d03ef511
AJ
1577 tcg_gen_ext32u_tl(t0, cpu_gpr[rS(ctx->opcode)]);
1578 tcg_gen_shri_tl(t0, t0, mb);
1579 tcg_gen_ext32u_tl(cpu_gpr[rA(ctx->opcode)], t0);
1580 tcg_temp_free(t0);
1581 } else {
a7812ae4 1582 TCGv t0 = tcg_temp_new();
54843a58 1583#if defined(TARGET_PPC64)
a7812ae4 1584 TCGv_i32 t1 = tcg_temp_new_i32();
54843a58
AJ
1585 tcg_gen_trunc_i64_i32(t1, cpu_gpr[rS(ctx->opcode)]);
1586 tcg_gen_rotli_i32(t1, t1, sh);
1587 tcg_gen_extu_i32_i64(t0, t1);
a7812ae4 1588 tcg_temp_free_i32(t1);
54843a58
AJ
1589#else
1590 tcg_gen_rotli_i32(t0, cpu_gpr[rS(ctx->opcode)], sh);
1591#endif
76a66253 1592#if defined(TARGET_PPC64)
d03ef511
AJ
1593 mb += 32;
1594 me += 32;
76a66253 1595#endif
d03ef511
AJ
1596 tcg_gen_andi_tl(cpu_gpr[rA(ctx->opcode)], t0, MASK(mb, me));
1597 tcg_temp_free(t0);
1598 }
76a66253 1599 if (unlikely(Rc(ctx->opcode) != 0))
d03ef511 1600 gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
79aceca5 1601}
99e300ef 1602
54623277 1603/* rlwnm & rlwnm. */
99e300ef 1604static void gen_rlwnm(DisasContext *ctx)
79aceca5
FB
1605{
1606 uint32_t mb, me;
54843a58
AJ
1607 TCGv t0;
1608#if defined(TARGET_PPC64)
a7812ae4 1609 TCGv_i32 t1, t2;
54843a58 1610#endif
79aceca5
FB
1611
1612 mb = MB(ctx->opcode);
1613 me = ME(ctx->opcode);
a7812ae4 1614 t0 = tcg_temp_new();
d03ef511 1615 tcg_gen_andi_tl(t0, cpu_gpr[rB(ctx->opcode)], 0x1f);
54843a58 1616#if defined(TARGET_PPC64)
a7812ae4
PB
1617 t1 = tcg_temp_new_i32();
1618 t2 = tcg_temp_new_i32();
54843a58
AJ
1619 tcg_gen_trunc_i64_i32(t1, cpu_gpr[rS(ctx->opcode)]);
1620 tcg_gen_trunc_i64_i32(t2, t0);
1621 tcg_gen_rotl_i32(t1, t1, t2);
1622 tcg_gen_extu_i32_i64(t0, t1);
a7812ae4
PB
1623 tcg_temp_free_i32(t1);
1624 tcg_temp_free_i32(t2);
54843a58
AJ
1625#else
1626 tcg_gen_rotl_i32(t0, cpu_gpr[rS(ctx->opcode)], t0);
1627#endif
76a66253
JM
1628 if (unlikely(mb != 0 || me != 31)) {
1629#if defined(TARGET_PPC64)
1630 mb += 32;
1631 me += 32;
1632#endif
54843a58 1633 tcg_gen_andi_tl(cpu_gpr[rA(ctx->opcode)], t0, MASK(mb, me));
d03ef511 1634 } else {
54843a58 1635 tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], t0);
79aceca5 1636 }
54843a58 1637 tcg_temp_free(t0);
76a66253 1638 if (unlikely(Rc(ctx->opcode) != 0))
d03ef511 1639 gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
79aceca5
FB
1640}
1641
d9bce9d9
JM
1642#if defined(TARGET_PPC64)
1643#define GEN_PPC64_R2(name, opc1, opc2) \
e8eaa2c0 1644static void glue(gen_, name##0)(DisasContext *ctx) \
d9bce9d9
JM
1645{ \
1646 gen_##name(ctx, 0); \
1647} \
e8eaa2c0
BS
1648 \
1649static void glue(gen_, name##1)(DisasContext *ctx) \
d9bce9d9
JM
1650{ \
1651 gen_##name(ctx, 1); \
1652}
1653#define GEN_PPC64_R4(name, opc1, opc2) \
e8eaa2c0 1654static void glue(gen_, name##0)(DisasContext *ctx) \
d9bce9d9
JM
1655{ \
1656 gen_##name(ctx, 0, 0); \
1657} \
e8eaa2c0
BS
1658 \
1659static void glue(gen_, name##1)(DisasContext *ctx) \
d9bce9d9
JM
1660{ \
1661 gen_##name(ctx, 0, 1); \
1662} \
e8eaa2c0
BS
1663 \
1664static void glue(gen_, name##2)(DisasContext *ctx) \
d9bce9d9
JM
1665{ \
1666 gen_##name(ctx, 1, 0); \
1667} \
e8eaa2c0
BS
1668 \
1669static void glue(gen_, name##3)(DisasContext *ctx) \
d9bce9d9
JM
1670{ \
1671 gen_##name(ctx, 1, 1); \
1672}
51789c41 1673
636aa200
BS
1674static inline void gen_rldinm(DisasContext *ctx, uint32_t mb, uint32_t me,
1675 uint32_t sh)
51789c41 1676{
d03ef511
AJ
1677 if (likely(sh != 0 && mb == 0 && me == (63 - sh))) {
1678 tcg_gen_shli_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)], sh);
1679 } else if (likely(sh != 0 && me == 63 && sh == (64 - mb))) {
1680 tcg_gen_shri_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)], mb);
1681 } else {
a7812ae4 1682 TCGv t0 = tcg_temp_new();
54843a58 1683 tcg_gen_rotli_tl(t0, cpu_gpr[rS(ctx->opcode)], sh);
d03ef511 1684 if (likely(mb == 0 && me == 63)) {
54843a58 1685 tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], t0);
d03ef511
AJ
1686 } else {
1687 tcg_gen_andi_tl(cpu_gpr[rA(ctx->opcode)], t0, MASK(mb, me));
51789c41 1688 }
d03ef511 1689 tcg_temp_free(t0);
51789c41 1690 }
51789c41 1691 if (unlikely(Rc(ctx->opcode) != 0))
d03ef511 1692 gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
51789c41 1693}
d9bce9d9 1694/* rldicl - rldicl. */
636aa200 1695static inline void gen_rldicl(DisasContext *ctx, int mbn, int shn)
d9bce9d9 1696{
51789c41 1697 uint32_t sh, mb;
d9bce9d9 1698
9d53c753
JM
1699 sh = SH(ctx->opcode) | (shn << 5);
1700 mb = MB(ctx->opcode) | (mbn << 5);
51789c41 1701 gen_rldinm(ctx, mb, 63, sh);
d9bce9d9 1702}
51789c41 1703GEN_PPC64_R4(rldicl, 0x1E, 0x00);
d9bce9d9 1704/* rldicr - rldicr. */
636aa200 1705static inline void gen_rldicr(DisasContext *ctx, int men, int shn)
d9bce9d9 1706{
51789c41 1707 uint32_t sh, me;
d9bce9d9 1708
9d53c753
JM
1709 sh = SH(ctx->opcode) | (shn << 5);
1710 me = MB(ctx->opcode) | (men << 5);
51789c41 1711 gen_rldinm(ctx, 0, me, sh);
d9bce9d9 1712}
51789c41 1713GEN_PPC64_R4(rldicr, 0x1E, 0x02);
d9bce9d9 1714/* rldic - rldic. */
636aa200 1715static inline void gen_rldic(DisasContext *ctx, int mbn, int shn)
d9bce9d9 1716{
51789c41 1717 uint32_t sh, mb;
d9bce9d9 1718
9d53c753
JM
1719 sh = SH(ctx->opcode) | (shn << 5);
1720 mb = MB(ctx->opcode) | (mbn << 5);
51789c41
JM
1721 gen_rldinm(ctx, mb, 63 - sh, sh);
1722}
1723GEN_PPC64_R4(rldic, 0x1E, 0x04);
1724
636aa200 1725static inline void gen_rldnm(DisasContext *ctx, uint32_t mb, uint32_t me)
51789c41 1726{
54843a58 1727 TCGv t0;
d03ef511
AJ
1728
1729 mb = MB(ctx->opcode);
1730 me = ME(ctx->opcode);
a7812ae4 1731 t0 = tcg_temp_new();
d03ef511 1732 tcg_gen_andi_tl(t0, cpu_gpr[rB(ctx->opcode)], 0x3f);
54843a58 1733 tcg_gen_rotl_tl(t0, cpu_gpr[rS(ctx->opcode)], t0);
51789c41 1734 if (unlikely(mb != 0 || me != 63)) {
54843a58
AJ
1735 tcg_gen_andi_tl(cpu_gpr[rA(ctx->opcode)], t0, MASK(mb, me));
1736 } else {
1737 tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], t0);
1738 }
1739 tcg_temp_free(t0);
51789c41 1740 if (unlikely(Rc(ctx->opcode) != 0))
d03ef511 1741 gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
d9bce9d9 1742}
51789c41 1743
d9bce9d9 1744/* rldcl - rldcl. */
636aa200 1745static inline void gen_rldcl(DisasContext *ctx, int mbn)
d9bce9d9 1746{
51789c41 1747 uint32_t mb;
d9bce9d9 1748
9d53c753 1749 mb = MB(ctx->opcode) | (mbn << 5);
51789c41 1750 gen_rldnm(ctx, mb, 63);
d9bce9d9 1751}
36081602 1752GEN_PPC64_R2(rldcl, 0x1E, 0x08);
d9bce9d9 1753/* rldcr - rldcr. */
636aa200 1754static inline void gen_rldcr(DisasContext *ctx, int men)
d9bce9d9 1755{
51789c41 1756 uint32_t me;
d9bce9d9 1757
9d53c753 1758 me = MB(ctx->opcode) | (men << 5);
51789c41 1759 gen_rldnm(ctx, 0, me);
d9bce9d9 1760}
36081602 1761GEN_PPC64_R2(rldcr, 0x1E, 0x09);
d9bce9d9 1762/* rldimi - rldimi. */
636aa200 1763static inline void gen_rldimi(DisasContext *ctx, int mbn, int shn)
d9bce9d9 1764{
271a916e 1765 uint32_t sh, mb, me;
d9bce9d9 1766
9d53c753
JM
1767 sh = SH(ctx->opcode) | (shn << 5);
1768 mb = MB(ctx->opcode) | (mbn << 5);
271a916e 1769 me = 63 - sh;
d03ef511
AJ
1770 if (unlikely(sh == 0 && mb == 0)) {
1771 tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)]);
1772 } else {
1773 TCGv t0, t1;
1774 target_ulong mask;
1775
a7812ae4 1776 t0 = tcg_temp_new();
54843a58 1777 tcg_gen_rotli_tl(t0, cpu_gpr[rS(ctx->opcode)], sh);
a7812ae4 1778 t1 = tcg_temp_new();
d03ef511
AJ
1779 mask = MASK(mb, me);
1780 tcg_gen_andi_tl(t0, t0, mask);
1781 tcg_gen_andi_tl(t1, cpu_gpr[rA(ctx->opcode)], ~mask);
1782 tcg_gen_or_tl(cpu_gpr[rA(ctx->opcode)], t0, t1);
1783 tcg_temp_free(t0);
1784 tcg_temp_free(t1);
51789c41 1785 }
51789c41 1786 if (unlikely(Rc(ctx->opcode) != 0))
d03ef511 1787 gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
d9bce9d9 1788}
36081602 1789GEN_PPC64_R4(rldimi, 0x1E, 0x06);
d9bce9d9
JM
1790#endif
1791
79aceca5 1792/*** Integer shift ***/
99e300ef 1793
54623277 1794/* slw & slw. */
99e300ef 1795static void gen_slw(DisasContext *ctx)
26d67362 1796{
7fd6bf7d 1797 TCGv t0, t1;
26d67362 1798
7fd6bf7d
AJ
1799 t0 = tcg_temp_new();
1800 /* AND rS with a mask that is 0 when rB >= 0x20 */
1801#if defined(TARGET_PPC64)
1802 tcg_gen_shli_tl(t0, cpu_gpr[rB(ctx->opcode)], 0x3a);
1803 tcg_gen_sari_tl(t0, t0, 0x3f);
1804#else
1805 tcg_gen_shli_tl(t0, cpu_gpr[rB(ctx->opcode)], 0x1a);
1806 tcg_gen_sari_tl(t0, t0, 0x1f);
1807#endif
1808 tcg_gen_andc_tl(t0, cpu_gpr[rS(ctx->opcode)], t0);
1809 t1 = tcg_temp_new();
1810 tcg_gen_andi_tl(t1, cpu_gpr[rB(ctx->opcode)], 0x1f);
1811 tcg_gen_shl_tl(cpu_gpr[rA(ctx->opcode)], t0, t1);
1812 tcg_temp_free(t1);
fea0c503 1813 tcg_temp_free(t0);
7fd6bf7d 1814 tcg_gen_ext32u_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rA(ctx->opcode)]);
26d67362
AJ
1815 if (unlikely(Rc(ctx->opcode) != 0))
1816 gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
1817}
99e300ef 1818
54623277 1819/* sraw & sraw. */
99e300ef 1820static void gen_sraw(DisasContext *ctx)
26d67362 1821{
d15f74fb 1822 gen_helper_sraw(cpu_gpr[rA(ctx->opcode)], cpu_env,
a7812ae4 1823 cpu_gpr[rS(ctx->opcode)], cpu_gpr[rB(ctx->opcode)]);
26d67362
AJ
1824 if (unlikely(Rc(ctx->opcode) != 0))
1825 gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
1826}
99e300ef 1827
54623277 1828/* srawi & srawi. */
99e300ef 1829static void gen_srawi(DisasContext *ctx)
79aceca5 1830{
26d67362 1831 int sh = SH(ctx->opcode);
ba4af3e4
RH
1832 TCGv dst = cpu_gpr[rA(ctx->opcode)];
1833 TCGv src = cpu_gpr[rS(ctx->opcode)];
1834 if (sh == 0) {
1835 tcg_gen_mov_tl(dst, src);
da91a00f 1836 tcg_gen_movi_tl(cpu_ca, 0);
26d67362 1837 } else {
ba4af3e4
RH
1838 TCGv t0;
1839 tcg_gen_ext32s_tl(dst, src);
1840 tcg_gen_andi_tl(cpu_ca, dst, (1ULL << sh) - 1);
1841 t0 = tcg_temp_new();
1842 tcg_gen_sari_tl(t0, dst, TARGET_LONG_BITS - 1);
1843 tcg_gen_and_tl(cpu_ca, cpu_ca, t0);
1844 tcg_temp_free(t0);
1845 tcg_gen_setcondi_tl(TCG_COND_NE, cpu_ca, cpu_ca, 0);
1846 tcg_gen_sari_tl(dst, dst, sh);
1847 }
1848 if (unlikely(Rc(ctx->opcode) != 0)) {
1849 gen_set_Rc0(ctx, dst);
d9bce9d9 1850 }
79aceca5 1851}
99e300ef 1852
54623277 1853/* srw & srw. */
99e300ef 1854static void gen_srw(DisasContext *ctx)
26d67362 1855{
fea0c503 1856 TCGv t0, t1;
d9bce9d9 1857
7fd6bf7d
AJ
1858 t0 = tcg_temp_new();
1859 /* AND rS with a mask that is 0 when rB >= 0x20 */
1860#if defined(TARGET_PPC64)
1861 tcg_gen_shli_tl(t0, cpu_gpr[rB(ctx->opcode)], 0x3a);
1862 tcg_gen_sari_tl(t0, t0, 0x3f);
1863#else
1864 tcg_gen_shli_tl(t0, cpu_gpr[rB(ctx->opcode)], 0x1a);
1865 tcg_gen_sari_tl(t0, t0, 0x1f);
1866#endif
1867 tcg_gen_andc_tl(t0, cpu_gpr[rS(ctx->opcode)], t0);
1868 tcg_gen_ext32u_tl(t0, t0);
a7812ae4 1869 t1 = tcg_temp_new();
7fd6bf7d
AJ
1870 tcg_gen_andi_tl(t1, cpu_gpr[rB(ctx->opcode)], 0x1f);
1871 tcg_gen_shr_tl(cpu_gpr[rA(ctx->opcode)], t0, t1);
fea0c503 1872 tcg_temp_free(t1);
fea0c503 1873 tcg_temp_free(t0);
26d67362
AJ
1874 if (unlikely(Rc(ctx->opcode) != 0))
1875 gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
1876}
54623277 1877
d9bce9d9
JM
1878#if defined(TARGET_PPC64)
1879/* sld & sld. */
99e300ef 1880static void gen_sld(DisasContext *ctx)
26d67362 1881{
7fd6bf7d 1882 TCGv t0, t1;
26d67362 1883
7fd6bf7d
AJ
1884 t0 = tcg_temp_new();
1885 /* AND rS with a mask that is 0 when rB >= 0x40 */
1886 tcg_gen_shli_tl(t0, cpu_gpr[rB(ctx->opcode)], 0x39);
1887 tcg_gen_sari_tl(t0, t0, 0x3f);
1888 tcg_gen_andc_tl(t0, cpu_gpr[rS(ctx->opcode)], t0);
1889 t1 = tcg_temp_new();
1890 tcg_gen_andi_tl(t1, cpu_gpr[rB(ctx->opcode)], 0x3f);
1891 tcg_gen_shl_tl(cpu_gpr[rA(ctx->opcode)], t0, t1);
1892 tcg_temp_free(t1);
fea0c503 1893 tcg_temp_free(t0);
26d67362
AJ
1894 if (unlikely(Rc(ctx->opcode) != 0))
1895 gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
1896}
99e300ef 1897
54623277 1898/* srad & srad. */
99e300ef 1899static void gen_srad(DisasContext *ctx)
26d67362 1900{
d15f74fb 1901 gen_helper_srad(cpu_gpr[rA(ctx->opcode)], cpu_env,
a7812ae4 1902 cpu_gpr[rS(ctx->opcode)], cpu_gpr[rB(ctx->opcode)]);
26d67362
AJ
1903 if (unlikely(Rc(ctx->opcode) != 0))
1904 gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
1905}
d9bce9d9 1906/* sradi & sradi. */
636aa200 1907static inline void gen_sradi(DisasContext *ctx, int n)
d9bce9d9 1908{
26d67362 1909 int sh = SH(ctx->opcode) + (n << 5);
ba4af3e4
RH
1910 TCGv dst = cpu_gpr[rA(ctx->opcode)];
1911 TCGv src = cpu_gpr[rS(ctx->opcode)];
1912 if (sh == 0) {
1913 tcg_gen_mov_tl(dst, src);
da91a00f 1914 tcg_gen_movi_tl(cpu_ca, 0);
26d67362 1915 } else {
ba4af3e4
RH
1916 TCGv t0;
1917 tcg_gen_andi_tl(cpu_ca, src, (1ULL << sh) - 1);
1918 t0 = tcg_temp_new();
1919 tcg_gen_sari_tl(t0, src, TARGET_LONG_BITS - 1);
1920 tcg_gen_and_tl(cpu_ca, cpu_ca, t0);
1921 tcg_temp_free(t0);
1922 tcg_gen_setcondi_tl(TCG_COND_NE, cpu_ca, cpu_ca, 0);
1923 tcg_gen_sari_tl(dst, src, sh);
1924 }
1925 if (unlikely(Rc(ctx->opcode) != 0)) {
1926 gen_set_Rc0(ctx, dst);
d9bce9d9 1927 }
d9bce9d9 1928}
e8eaa2c0
BS
1929
1930static void gen_sradi0(DisasContext *ctx)
d9bce9d9
JM
1931{
1932 gen_sradi(ctx, 0);
1933}
e8eaa2c0
BS
1934
1935static void gen_sradi1(DisasContext *ctx)
d9bce9d9
JM
1936{
1937 gen_sradi(ctx, 1);
1938}
99e300ef 1939
54623277 1940/* srd & srd. */
99e300ef 1941static void gen_srd(DisasContext *ctx)
26d67362 1942{
7fd6bf7d 1943 TCGv t0, t1;
26d67362 1944
7fd6bf7d
AJ
1945 t0 = tcg_temp_new();
1946 /* AND rS with a mask that is 0 when rB >= 0x40 */
1947 tcg_gen_shli_tl(t0, cpu_gpr[rB(ctx->opcode)], 0x39);
1948 tcg_gen_sari_tl(t0, t0, 0x3f);
1949 tcg_gen_andc_tl(t0, cpu_gpr[rS(ctx->opcode)], t0);
1950 t1 = tcg_temp_new();
1951 tcg_gen_andi_tl(t1, cpu_gpr[rB(ctx->opcode)], 0x3f);
1952 tcg_gen_shr_tl(cpu_gpr[rA(ctx->opcode)], t0, t1);
1953 tcg_temp_free(t1);
fea0c503 1954 tcg_temp_free(t0);
26d67362
AJ
1955 if (unlikely(Rc(ctx->opcode) != 0))
1956 gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
1957}
d9bce9d9 1958#endif
79aceca5
FB
1959
1960/*** Floating-Point arithmetic ***/
7c58044c 1961#define _GEN_FLOAT_ACB(name, op, op1, op2, isfloat, set_fprf, type) \
99e300ef 1962static void gen_f##name(DisasContext *ctx) \
9a64fbe4 1963{ \
76a66253 1964 if (unlikely(!ctx->fpu_enabled)) { \
e06fcd75 1965 gen_exception(ctx, POWERPC_EXCP_FPU); \
3cc62370
FB
1966 return; \
1967 } \
eb44b959
AJ
1968 /* NIP cannot be restored if the memory exception comes from an helper */ \
1969 gen_update_nip(ctx, ctx->nip - 4); \
7c58044c 1970 gen_reset_fpstatus(); \
8e703949
BS
1971 gen_helper_f##op(cpu_fpr[rD(ctx->opcode)], cpu_env, \
1972 cpu_fpr[rA(ctx->opcode)], \
af12906f 1973 cpu_fpr[rC(ctx->opcode)], cpu_fpr[rB(ctx->opcode)]); \
4ecc3190 1974 if (isfloat) { \
8e703949
BS
1975 gen_helper_frsp(cpu_fpr[rD(ctx->opcode)], cpu_env, \
1976 cpu_fpr[rD(ctx->opcode)]); \
4ecc3190 1977 } \
af12906f
AJ
1978 gen_compute_fprf(cpu_fpr[rD(ctx->opcode)], set_fprf, \
1979 Rc(ctx->opcode) != 0); \
9a64fbe4
FB
1980}
1981
7c58044c
JM
1982#define GEN_FLOAT_ACB(name, op2, set_fprf, type) \
1983_GEN_FLOAT_ACB(name, name, 0x3F, op2, 0, set_fprf, type); \
1984_GEN_FLOAT_ACB(name##s, name, 0x3B, op2, 1, set_fprf, type);
9a64fbe4 1985
7c58044c 1986#define _GEN_FLOAT_AB(name, op, op1, op2, inval, isfloat, set_fprf, type) \
99e300ef 1987static void gen_f##name(DisasContext *ctx) \
9a64fbe4 1988{ \
76a66253 1989 if (unlikely(!ctx->fpu_enabled)) { \
e06fcd75 1990 gen_exception(ctx, POWERPC_EXCP_FPU); \
3cc62370
FB
1991 return; \
1992 } \
eb44b959
AJ
1993 /* NIP cannot be restored if the memory exception comes from an helper */ \
1994 gen_update_nip(ctx, ctx->nip - 4); \
7c58044c 1995 gen_reset_fpstatus(); \
8e703949
BS
1996 gen_helper_f##op(cpu_fpr[rD(ctx->opcode)], cpu_env, \
1997 cpu_fpr[rA(ctx->opcode)], \
af12906f 1998 cpu_fpr[rB(ctx->opcode)]); \
4ecc3190 1999 if (isfloat) { \
8e703949
BS
2000 gen_helper_frsp(cpu_fpr[rD(ctx->opcode)], cpu_env, \
2001 cpu_fpr[rD(ctx->opcode)]); \
4ecc3190 2002 } \
af12906f
AJ
2003 gen_compute_fprf(cpu_fpr[rD(ctx->opcode)], \
2004 set_fprf, Rc(ctx->opcode) != 0); \
9a64fbe4 2005}
7c58044c
JM
2006#define GEN_FLOAT_AB(name, op2, inval, set_fprf, type) \
2007_GEN_FLOAT_AB(name, name, 0x3F, op2, inval, 0, set_fprf, type); \
2008_GEN_FLOAT_AB(name##s, name, 0x3B, op2, inval, 1, set_fprf, type);
9a64fbe4 2009
7c58044c 2010#define _GEN_FLOAT_AC(name, op, op1, op2, inval, isfloat, set_fprf, type) \
99e300ef 2011static void gen_f##name(DisasContext *ctx) \
9a64fbe4 2012{ \
76a66253 2013 if (unlikely(!ctx->fpu_enabled)) { \
e06fcd75 2014 gen_exception(ctx, POWERPC_EXCP_FPU); \
3cc62370
FB
2015 return; \
2016 } \
eb44b959
AJ
2017 /* NIP cannot be restored if the memory exception comes from an helper */ \
2018 gen_update_nip(ctx, ctx->nip - 4); \
7c58044c 2019 gen_reset_fpstatus(); \
8e703949
BS
2020 gen_helper_f##op(cpu_fpr[rD(ctx->opcode)], cpu_env, \
2021 cpu_fpr[rA(ctx->opcode)], \
2022 cpu_fpr[rC(ctx->opcode)]); \
4ecc3190 2023 if (isfloat) { \
8e703949
BS
2024 gen_helper_frsp(cpu_fpr[rD(ctx->opcode)], cpu_env, \
2025 cpu_fpr[rD(ctx->opcode)]); \
4ecc3190 2026 } \
af12906f
AJ
2027 gen_compute_fprf(cpu_fpr[rD(ctx->opcode)], \
2028 set_fprf, Rc(ctx->opcode) != 0); \
9a64fbe4 2029}
7c58044c
JM
2030#define GEN_FLOAT_AC(name, op2, inval, set_fprf, type) \
2031_GEN_FLOAT_AC(name, name, 0x3F, op2, inval, 0, set_fprf, type); \
2032_GEN_FLOAT_AC(name##s, name, 0x3B, op2, inval, 1, set_fprf, type);
9a64fbe4 2033
7c58044c 2034#define GEN_FLOAT_B(name, op2, op3, set_fprf, type) \
99e300ef 2035static void gen_f##name(DisasContext *ctx) \
9a64fbe4 2036{ \
76a66253 2037 if (unlikely(!ctx->fpu_enabled)) { \
e06fcd75 2038 gen_exception(ctx, POWERPC_EXCP_FPU); \
3cc62370
FB
2039 return; \
2040 } \
eb44b959
AJ
2041 /* NIP cannot be restored if the memory exception comes from an helper */ \
2042 gen_update_nip(ctx, ctx->nip - 4); \
7c58044c 2043 gen_reset_fpstatus(); \
8e703949
BS
2044 gen_helper_f##name(cpu_fpr[rD(ctx->opcode)], cpu_env, \
2045 cpu_fpr[rB(ctx->opcode)]); \
af12906f
AJ
2046 gen_compute_fprf(cpu_fpr[rD(ctx->opcode)], \
2047 set_fprf, Rc(ctx->opcode) != 0); \
79aceca5
FB
2048}
2049
7c58044c 2050#define GEN_FLOAT_BS(name, op1, op2, set_fprf, type) \
99e300ef 2051static void gen_f##name(DisasContext *ctx) \
9a64fbe4 2052{ \
76a66253 2053 if (unlikely(!ctx->fpu_enabled)) { \
e06fcd75 2054 gen_exception(ctx, POWERPC_EXCP_FPU); \
3cc62370
FB
2055 return; \
2056 } \
eb44b959
AJ
2057 /* NIP cannot be restored if the memory exception comes from an helper */ \
2058 gen_update_nip(ctx, ctx->nip - 4); \
7c58044c 2059 gen_reset_fpstatus(); \
8e703949
BS
2060 gen_helper_f##name(cpu_fpr[rD(ctx->opcode)], cpu_env, \
2061 cpu_fpr[rB(ctx->opcode)]); \
af12906f
AJ
2062 gen_compute_fprf(cpu_fpr[rD(ctx->opcode)], \
2063 set_fprf, Rc(ctx->opcode) != 0); \
79aceca5
FB
2064}
2065
9a64fbe4 2066/* fadd - fadds */
7c58044c 2067GEN_FLOAT_AB(add, 0x15, 0x000007C0, 1, PPC_FLOAT);
4ecc3190 2068/* fdiv - fdivs */
7c58044c 2069GEN_FLOAT_AB(div, 0x12, 0x000007C0, 1, PPC_FLOAT);
4ecc3190 2070/* fmul - fmuls */
7c58044c 2071GEN_FLOAT_AC(mul, 0x19, 0x0000F800, 1, PPC_FLOAT);
79aceca5 2072
d7e4b87e 2073/* fre */
7c58044c 2074GEN_FLOAT_BS(re, 0x3F, 0x18, 1, PPC_FLOAT_EXT);
d7e4b87e 2075
a750fc0b 2076/* fres */
7c58044c 2077GEN_FLOAT_BS(res, 0x3B, 0x18, 1, PPC_FLOAT_FRES);
79aceca5 2078
a750fc0b 2079/* frsqrte */
7c58044c
JM
2080GEN_FLOAT_BS(rsqrte, 0x3F, 0x1A, 1, PPC_FLOAT_FRSQRTE);
2081
2082/* frsqrtes */
99e300ef 2083static void gen_frsqrtes(DisasContext *ctx)
7c58044c 2084{
af12906f 2085 if (unlikely(!ctx->fpu_enabled)) {
e06fcd75 2086 gen_exception(ctx, POWERPC_EXCP_FPU);
af12906f
AJ
2087 return;
2088 }
eb44b959
AJ
2089 /* NIP cannot be restored if the memory exception comes from an helper */
2090 gen_update_nip(ctx, ctx->nip - 4);
af12906f 2091 gen_reset_fpstatus();
8e703949
BS
2092 gen_helper_frsqrte(cpu_fpr[rD(ctx->opcode)], cpu_env,
2093 cpu_fpr[rB(ctx->opcode)]);
2094 gen_helper_frsp(cpu_fpr[rD(ctx->opcode)], cpu_env,
2095 cpu_fpr[rD(ctx->opcode)]);
af12906f 2096 gen_compute_fprf(cpu_fpr[rD(ctx->opcode)], 1, Rc(ctx->opcode) != 0);
7c58044c 2097}
79aceca5 2098
a750fc0b 2099/* fsel */
7c58044c 2100_GEN_FLOAT_ACB(sel, sel, 0x3F, 0x17, 0, 0, PPC_FLOAT_FSEL);
4ecc3190 2101/* fsub - fsubs */
7c58044c 2102GEN_FLOAT_AB(sub, 0x14, 0x000007C0, 1, PPC_FLOAT);
79aceca5 2103/* Optional: */
99e300ef 2104
54623277 2105/* fsqrt */
99e300ef 2106static void gen_fsqrt(DisasContext *ctx)
c7d344af 2107{
76a66253 2108 if (unlikely(!ctx->fpu_enabled)) {
e06fcd75 2109 gen_exception(ctx, POWERPC_EXCP_FPU);
c7d344af
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();
8e703949
BS
2115 gen_helper_fsqrt(cpu_fpr[rD(ctx->opcode)], cpu_env,
2116 cpu_fpr[rB(ctx->opcode)]);
af12906f 2117 gen_compute_fprf(cpu_fpr[rD(ctx->opcode)], 1, Rc(ctx->opcode) != 0);
c7d344af 2118}
79aceca5 2119
99e300ef 2120static void gen_fsqrts(DisasContext *ctx)
79aceca5 2121{
76a66253 2122 if (unlikely(!ctx->fpu_enabled)) {
e06fcd75 2123 gen_exception(ctx, POWERPC_EXCP_FPU);
3cc62370
FB
2124 return;
2125 }
eb44b959
AJ
2126 /* NIP cannot be restored if the memory exception comes from an helper */
2127 gen_update_nip(ctx, ctx->nip - 4);
7c58044c 2128 gen_reset_fpstatus();
8e703949
BS
2129 gen_helper_fsqrt(cpu_fpr[rD(ctx->opcode)], cpu_env,
2130 cpu_fpr[rB(ctx->opcode)]);
2131 gen_helper_frsp(cpu_fpr[rD(ctx->opcode)], cpu_env,
2132 cpu_fpr[rD(ctx->opcode)]);
af12906f 2133 gen_compute_fprf(cpu_fpr[rD(ctx->opcode)], 1, Rc(ctx->opcode) != 0);
79aceca5
FB
2134}
2135
2136/*** Floating-Point multiply-and-add ***/
4ecc3190 2137/* fmadd - fmadds */
7c58044c 2138GEN_FLOAT_ACB(madd, 0x1D, 1, PPC_FLOAT);
4ecc3190 2139/* fmsub - fmsubs */
7c58044c 2140GEN_FLOAT_ACB(msub, 0x1C, 1, PPC_FLOAT);
4ecc3190 2141/* fnmadd - fnmadds */
7c58044c 2142GEN_FLOAT_ACB(nmadd, 0x1F, 1, PPC_FLOAT);
4ecc3190 2143/* fnmsub - fnmsubs */
7c58044c 2144GEN_FLOAT_ACB(nmsub, 0x1E, 1, PPC_FLOAT);
79aceca5
FB
2145
2146/*** Floating-Point round & convert ***/
2147/* fctiw */
7c58044c 2148GEN_FLOAT_B(ctiw, 0x0E, 0x00, 0, PPC_FLOAT);
79aceca5 2149/* fctiwz */
7c58044c 2150GEN_FLOAT_B(ctiwz, 0x0F, 0x00, 0, PPC_FLOAT);
79aceca5 2151/* frsp */
7c58044c 2152GEN_FLOAT_B(rsp, 0x0C, 0x00, 1, PPC_FLOAT);
426613db
JM
2153#if defined(TARGET_PPC64)
2154/* fcfid */
7c58044c 2155GEN_FLOAT_B(cfid, 0x0E, 0x1A, 1, PPC_64B);
426613db 2156/* fctid */
7c58044c 2157GEN_FLOAT_B(ctid, 0x0E, 0x19, 0, PPC_64B);
426613db 2158/* fctidz */
7c58044c 2159GEN_FLOAT_B(ctidz, 0x0F, 0x19, 0, PPC_64B);
426613db 2160#endif
79aceca5 2161
d7e4b87e 2162/* frin */
7c58044c 2163GEN_FLOAT_B(rin, 0x08, 0x0C, 1, PPC_FLOAT_EXT);
d7e4b87e 2164/* friz */
7c58044c 2165GEN_FLOAT_B(riz, 0x08, 0x0D, 1, PPC_FLOAT_EXT);
d7e4b87e 2166/* frip */
7c58044c 2167GEN_FLOAT_B(rip, 0x08, 0x0E, 1, PPC_FLOAT_EXT);
d7e4b87e 2168/* frim */
7c58044c 2169GEN_FLOAT_B(rim, 0x08, 0x0F, 1, PPC_FLOAT_EXT);
d7e4b87e 2170
79aceca5 2171/*** Floating-Point compare ***/
99e300ef 2172
54623277 2173/* fcmpo */
99e300ef 2174static void gen_fcmpo(DisasContext *ctx)
79aceca5 2175{
330c483b 2176 TCGv_i32 crf;
76a66253 2177 if (unlikely(!ctx->fpu_enabled)) {
e06fcd75 2178 gen_exception(ctx, POWERPC_EXCP_FPU);
3cc62370
FB
2179 return;
2180 }
eb44b959
AJ
2181 /* NIP cannot be restored if the memory exception comes from an helper */
2182 gen_update_nip(ctx, ctx->nip - 4);
7c58044c 2183 gen_reset_fpstatus();
9a819377 2184 crf = tcg_const_i32(crfD(ctx->opcode));
8e703949
BS
2185 gen_helper_fcmpo(cpu_env, cpu_fpr[rA(ctx->opcode)],
2186 cpu_fpr[rB(ctx->opcode)], crf);
330c483b 2187 tcg_temp_free_i32(crf);
8e703949 2188 gen_helper_float_check_status(cpu_env);
79aceca5
FB
2189}
2190
2191/* fcmpu */
99e300ef 2192static void gen_fcmpu(DisasContext *ctx)
79aceca5 2193{
330c483b 2194 TCGv_i32 crf;
76a66253 2195 if (unlikely(!ctx->fpu_enabled)) {
e06fcd75 2196 gen_exception(ctx, POWERPC_EXCP_FPU);
3cc62370
FB
2197 return;
2198 }
eb44b959
AJ
2199 /* NIP cannot be restored if the memory exception comes from an helper */
2200 gen_update_nip(ctx, ctx->nip - 4);
7c58044c 2201 gen_reset_fpstatus();
9a819377 2202 crf = tcg_const_i32(crfD(ctx->opcode));
8e703949
BS
2203 gen_helper_fcmpu(cpu_env, cpu_fpr[rA(ctx->opcode)],
2204 cpu_fpr[rB(ctx->opcode)], crf);
330c483b 2205 tcg_temp_free_i32(crf);
8e703949 2206 gen_helper_float_check_status(cpu_env);
79aceca5
FB
2207}
2208
9a64fbe4
FB
2209/*** Floating-point move ***/
2210/* fabs */
7c58044c 2211/* XXX: beware that fabs never checks for NaNs nor update FPSCR */
bf45a2e6
AJ
2212static void gen_fabs(DisasContext *ctx)
2213{
2214 if (unlikely(!ctx->fpu_enabled)) {
2215 gen_exception(ctx, POWERPC_EXCP_FPU);
2216 return;
2217 }
2218 tcg_gen_andi_i64(cpu_fpr[rD(ctx->opcode)], cpu_fpr[rB(ctx->opcode)],
2219 ~(1ULL << 63));
2220 gen_compute_fprf(cpu_fpr[rD(ctx->opcode)], 0, Rc(ctx->opcode) != 0);
2221}
9a64fbe4
FB
2222
2223/* fmr - fmr. */
7c58044c 2224/* XXX: beware that fmr never checks for NaNs nor update FPSCR */
99e300ef 2225static void gen_fmr(DisasContext *ctx)
9a64fbe4 2226{
76a66253 2227 if (unlikely(!ctx->fpu_enabled)) {
e06fcd75 2228 gen_exception(ctx, POWERPC_EXCP_FPU);
3cc62370
FB
2229 return;
2230 }
af12906f
AJ
2231 tcg_gen_mov_i64(cpu_fpr[rD(ctx->opcode)], cpu_fpr[rB(ctx->opcode)]);
2232 gen_compute_fprf(cpu_fpr[rD(ctx->opcode)], 0, Rc(ctx->opcode) != 0);
9a64fbe4
FB
2233}
2234
2235/* fnabs */
7c58044c 2236/* XXX: beware that fnabs never checks for NaNs nor update FPSCR */
bf45a2e6
AJ
2237static void gen_fnabs(DisasContext *ctx)
2238{
2239 if (unlikely(!ctx->fpu_enabled)) {
2240 gen_exception(ctx, POWERPC_EXCP_FPU);
2241 return;
2242 }
2243 tcg_gen_ori_i64(cpu_fpr[rD(ctx->opcode)], cpu_fpr[rB(ctx->opcode)],
2244 1ULL << 63);
2245 gen_compute_fprf(cpu_fpr[rD(ctx->opcode)], 0, Rc(ctx->opcode) != 0);
2246}
2247
9a64fbe4 2248/* fneg */
7c58044c 2249/* XXX: beware that fneg never checks for NaNs nor update FPSCR */
bf45a2e6
AJ
2250static void gen_fneg(DisasContext *ctx)
2251{
2252 if (unlikely(!ctx->fpu_enabled)) {
2253 gen_exception(ctx, POWERPC_EXCP_FPU);
2254 return;
2255 }
2256 tcg_gen_xori_i64(cpu_fpr[rD(ctx->opcode)], cpu_fpr[rB(ctx->opcode)],
2257 1ULL << 63);
2258 gen_compute_fprf(cpu_fpr[rD(ctx->opcode)], 0, Rc(ctx->opcode) != 0);
2259}
9a64fbe4 2260
f0332888
AJ
2261/* fcpsgn: PowerPC 2.05 specification */
2262/* XXX: beware that fcpsgn never checks for NaNs nor update FPSCR */
2263static void gen_fcpsgn(DisasContext *ctx)
2264{
2265 if (unlikely(!ctx->fpu_enabled)) {
2266 gen_exception(ctx, POWERPC_EXCP_FPU);
2267 return;
2268 }
2269 tcg_gen_deposit_i64(cpu_fpr[rD(ctx->opcode)], cpu_fpr[rA(ctx->opcode)],
2270 cpu_fpr[rB(ctx->opcode)], 0, 63);
2271 gen_compute_fprf(cpu_fpr[rD(ctx->opcode)], 0, Rc(ctx->opcode) != 0);
2272}
2273
79aceca5 2274/*** Floating-Point status & ctrl register ***/
99e300ef 2275
54623277 2276/* mcrfs */
99e300ef 2277static void gen_mcrfs(DisasContext *ctx)
79aceca5 2278{
30304420 2279 TCGv tmp = tcg_temp_new();
7c58044c
JM
2280 int bfa;
2281
76a66253 2282 if (unlikely(!ctx->fpu_enabled)) {
e06fcd75 2283 gen_exception(ctx, POWERPC_EXCP_FPU);
3cc62370
FB
2284 return;
2285 }
7c58044c 2286 bfa = 4 * (7 - crfS(ctx->opcode));
30304420
DG
2287 tcg_gen_shri_tl(tmp, cpu_fpscr, bfa);
2288 tcg_gen_trunc_tl_i32(cpu_crf[crfD(ctx->opcode)], tmp);
2289 tcg_temp_free(tmp);
e1571908 2290 tcg_gen_andi_i32(cpu_crf[crfD(ctx->opcode)], cpu_crf[crfD(ctx->opcode)], 0xf);
30304420 2291 tcg_gen_andi_tl(cpu_fpscr, cpu_fpscr, ~(0xF << bfa));
79aceca5
FB
2292}
2293
2294/* mffs */
99e300ef 2295static void gen_mffs(DisasContext *ctx)
79aceca5 2296{
76a66253 2297 if (unlikely(!ctx->fpu_enabled)) {
e06fcd75 2298 gen_exception(ctx, POWERPC_EXCP_FPU);
3cc62370
FB
2299 return;
2300 }
7c58044c 2301 gen_reset_fpstatus();
30304420 2302 tcg_gen_extu_tl_i64(cpu_fpr[rD(ctx->opcode)], cpu_fpscr);
af12906f 2303 gen_compute_fprf(cpu_fpr[rD(ctx->opcode)], 0, Rc(ctx->opcode) != 0);
79aceca5
FB
2304}
2305
2306/* mtfsb0 */
99e300ef 2307static void gen_mtfsb0(DisasContext *ctx)
79aceca5 2308{
fb0eaffc 2309 uint8_t crb;
3b46e624 2310
76a66253 2311 if (unlikely(!ctx->fpu_enabled)) {
e06fcd75 2312 gen_exception(ctx, POWERPC_EXCP_FPU);
3cc62370
FB
2313 return;
2314 }
6e35d524 2315 crb = 31 - crbD(ctx->opcode);
7c58044c 2316 gen_reset_fpstatus();
6e35d524 2317 if (likely(crb != FPSCR_FEX && crb != FPSCR_VX)) {
eb44b959
AJ
2318 TCGv_i32 t0;
2319 /* NIP cannot be restored if the memory exception comes from an helper */
2320 gen_update_nip(ctx, ctx->nip - 4);
2321 t0 = tcg_const_i32(crb);
8e703949 2322 gen_helper_fpscr_clrbit(cpu_env, t0);
6e35d524
AJ
2323 tcg_temp_free_i32(t0);
2324 }
7c58044c 2325 if (unlikely(Rc(ctx->opcode) != 0)) {
30304420
DG
2326 tcg_gen_trunc_tl_i32(cpu_crf[1], cpu_fpscr);
2327 tcg_gen_shri_i32(cpu_crf[1], cpu_crf[1], FPSCR_OX);
7c58044c 2328 }
79aceca5
FB
2329}
2330
2331/* mtfsb1 */
99e300ef 2332static void gen_mtfsb1(DisasContext *ctx)
79aceca5 2333{
fb0eaffc 2334 uint8_t crb;
3b46e624 2335
76a66253 2336 if (unlikely(!ctx->fpu_enabled)) {
e06fcd75 2337 gen_exception(ctx, POWERPC_EXCP_FPU);
3cc62370
FB
2338 return;
2339 }
6e35d524 2340 crb = 31 - crbD(ctx->opcode);
7c58044c
JM
2341 gen_reset_fpstatus();
2342 /* XXX: we pretend we can only do IEEE floating-point computations */
af12906f 2343 if (likely(crb != FPSCR_FEX && crb != FPSCR_VX && crb != FPSCR_NI)) {
eb44b959
AJ
2344 TCGv_i32 t0;
2345 /* NIP cannot be restored if the memory exception comes from an helper */
2346 gen_update_nip(ctx, ctx->nip - 4);
2347 t0 = tcg_const_i32(crb);
8e703949 2348 gen_helper_fpscr_setbit(cpu_env, t0);
0f2f39c2 2349 tcg_temp_free_i32(t0);
af12906f 2350 }
7c58044c 2351 if (unlikely(Rc(ctx->opcode) != 0)) {
30304420
DG
2352 tcg_gen_trunc_tl_i32(cpu_crf[1], cpu_fpscr);
2353 tcg_gen_shri_i32(cpu_crf[1], cpu_crf[1], FPSCR_OX);
7c58044c
JM
2354 }
2355 /* We can raise a differed exception */
8e703949 2356 gen_helper_float_check_status(cpu_env);
79aceca5
FB
2357}
2358
2359/* mtfsf */
99e300ef 2360static void gen_mtfsf(DisasContext *ctx)
79aceca5 2361{
0f2f39c2 2362 TCGv_i32 t0;
4911012d 2363 int L = ctx->opcode & 0x02000000;
af12906f 2364
76a66253 2365 if (unlikely(!ctx->fpu_enabled)) {
e06fcd75 2366 gen_exception(ctx, POWERPC_EXCP_FPU);
3cc62370
FB
2367 return;
2368 }
eb44b959
AJ
2369 /* NIP cannot be restored if the memory exception comes from an helper */
2370 gen_update_nip(ctx, ctx->nip - 4);
7c58044c 2371 gen_reset_fpstatus();
4911012d
BS
2372 if (L)
2373 t0 = tcg_const_i32(0xff);
2374 else
2375 t0 = tcg_const_i32(FM(ctx->opcode));
8e703949 2376 gen_helper_store_fpscr(cpu_env, cpu_fpr[rB(ctx->opcode)], t0);
0f2f39c2 2377 tcg_temp_free_i32(t0);
7c58044c 2378 if (unlikely(Rc(ctx->opcode) != 0)) {
30304420
DG
2379 tcg_gen_trunc_tl_i32(cpu_crf[1], cpu_fpscr);
2380 tcg_gen_shri_i32(cpu_crf[1], cpu_crf[1], FPSCR_OX);
7c58044c
JM
2381 }
2382 /* We can raise a differed exception */
8e703949 2383 gen_helper_float_check_status(cpu_env);
79aceca5
FB
2384}
2385
2386/* mtfsfi */
99e300ef 2387static void gen_mtfsfi(DisasContext *ctx)
79aceca5 2388{
7c58044c 2389 int bf, sh;
0f2f39c2
AJ
2390 TCGv_i64 t0;
2391 TCGv_i32 t1;
7c58044c 2392
76a66253 2393 if (unlikely(!ctx->fpu_enabled)) {
e06fcd75 2394 gen_exception(ctx, POWERPC_EXCP_FPU);
3cc62370
FB
2395 return;
2396 }
7c58044c
JM
2397 bf = crbD(ctx->opcode) >> 2;
2398 sh = 7 - bf;
eb44b959
AJ
2399 /* NIP cannot be restored if the memory exception comes from an helper */
2400 gen_update_nip(ctx, ctx->nip - 4);
7c58044c 2401 gen_reset_fpstatus();
0f2f39c2 2402 t0 = tcg_const_i64(FPIMM(ctx->opcode) << (4 * sh));
af12906f 2403 t1 = tcg_const_i32(1 << sh);
8e703949 2404 gen_helper_store_fpscr(cpu_env, t0, t1);
0f2f39c2
AJ
2405 tcg_temp_free_i64(t0);
2406 tcg_temp_free_i32(t1);
7c58044c 2407 if (unlikely(Rc(ctx->opcode) != 0)) {
30304420
DG
2408 tcg_gen_trunc_tl_i32(cpu_crf[1], cpu_fpscr);
2409 tcg_gen_shri_i32(cpu_crf[1], cpu_crf[1], FPSCR_OX);
7c58044c
JM
2410 }
2411 /* We can raise a differed exception */
8e703949 2412 gen_helper_float_check_status(cpu_env);
79aceca5
FB
2413}
2414
76a66253
JM
2415/*** Addressing modes ***/
2416/* Register indirect with immediate index : EA = (rA|0) + SIMM */
636aa200
BS
2417static inline void gen_addr_imm_index(DisasContext *ctx, TCGv EA,
2418 target_long maskl)
76a66253
JM
2419{
2420 target_long simm = SIMM(ctx->opcode);
2421
be147d08 2422 simm &= ~maskl;
76db3ba4 2423 if (rA(ctx->opcode) == 0) {
c791fe84
RH
2424 if (NARROW_MODE(ctx)) {
2425 simm = (uint32_t)simm;
2426 }
e2be8d8d 2427 tcg_gen_movi_tl(EA, simm);
76db3ba4 2428 } else if (likely(simm != 0)) {
e2be8d8d 2429 tcg_gen_addi_tl(EA, cpu_gpr[rA(ctx->opcode)], simm);
c791fe84 2430 if (NARROW_MODE(ctx)) {
76db3ba4
AJ
2431 tcg_gen_ext32u_tl(EA, EA);
2432 }
76db3ba4 2433 } else {
c791fe84 2434 if (NARROW_MODE(ctx)) {
76db3ba4 2435 tcg_gen_ext32u_tl(EA, cpu_gpr[rA(ctx->opcode)]);
c791fe84
RH
2436 } else {
2437 tcg_gen_mov_tl(EA, cpu_gpr[rA(ctx->opcode)]);
2438 }
76db3ba4 2439 }
76a66253
JM
2440}
2441
636aa200 2442static inline void gen_addr_reg_index(DisasContext *ctx, TCGv EA)
76a66253 2443{
76db3ba4 2444 if (rA(ctx->opcode) == 0) {
c791fe84 2445 if (NARROW_MODE(ctx)) {
76db3ba4 2446 tcg_gen_ext32u_tl(EA, cpu_gpr[rB(ctx->opcode)]);
c791fe84
RH
2447 } else {
2448 tcg_gen_mov_tl(EA, cpu_gpr[rB(ctx->opcode)]);
2449 }
76db3ba4 2450 } else {
e2be8d8d 2451 tcg_gen_add_tl(EA, cpu_gpr[rA(ctx->opcode)], cpu_gpr[rB(ctx->opcode)]);
c791fe84 2452 if (NARROW_MODE(ctx)) {
76db3ba4
AJ
2453 tcg_gen_ext32u_tl(EA, EA);
2454 }
76db3ba4 2455 }
76a66253
JM
2456}
2457
636aa200 2458static inline void gen_addr_register(DisasContext *ctx, TCGv EA)
76a66253 2459{
76db3ba4 2460 if (rA(ctx->opcode) == 0) {
e2be8d8d 2461 tcg_gen_movi_tl(EA, 0);
c791fe84
RH
2462 } else if (NARROW_MODE(ctx)) {
2463 tcg_gen_ext32u_tl(EA, cpu_gpr[rA(ctx->opcode)]);
76db3ba4 2464 } else {
c791fe84 2465 tcg_gen_mov_tl(EA, cpu_gpr[rA(ctx->opcode)]);
76db3ba4
AJ
2466 }
2467}
2468
636aa200
BS
2469static inline void gen_addr_add(DisasContext *ctx, TCGv ret, TCGv arg1,
2470 target_long val)
76db3ba4
AJ
2471{
2472 tcg_gen_addi_tl(ret, arg1, val);
c791fe84 2473 if (NARROW_MODE(ctx)) {
76db3ba4
AJ
2474 tcg_gen_ext32u_tl(ret, ret);
2475 }
76a66253
JM
2476}
2477
636aa200 2478static inline void gen_check_align(DisasContext *ctx, TCGv EA, int mask)
cf360a32
AJ
2479{
2480 int l1 = gen_new_label();
2481 TCGv t0 = tcg_temp_new();
2482 TCGv_i32 t1, t2;
2483 /* NIP cannot be restored if the memory exception comes from an helper */
2484 gen_update_nip(ctx, ctx->nip - 4);
2485 tcg_gen_andi_tl(t0, EA, mask);
2486 tcg_gen_brcondi_tl(TCG_COND_EQ, t0, 0, l1);
2487 t1 = tcg_const_i32(POWERPC_EXCP_ALIGN);
2488 t2 = tcg_const_i32(0);
e5f17ac6 2489 gen_helper_raise_exception_err(cpu_env, t1, t2);
cf360a32
AJ
2490 tcg_temp_free_i32(t1);
2491 tcg_temp_free_i32(t2);
2492 gen_set_label(l1);
2493 tcg_temp_free(t0);
2494}
2495
7863667f 2496/*** Integer load ***/
636aa200 2497static inline void gen_qemu_ld8u(DisasContext *ctx, TCGv arg1, TCGv arg2)
76db3ba4
AJ
2498{
2499 tcg_gen_qemu_ld8u(arg1, arg2, ctx->mem_idx);
2500}
2501
636aa200 2502static inline void gen_qemu_ld8s(DisasContext *ctx, TCGv arg1, TCGv arg2)
76db3ba4
AJ
2503{
2504 tcg_gen_qemu_ld8s(arg1, arg2, ctx->mem_idx);
2505}
2506
636aa200 2507static inline void gen_qemu_ld16u(DisasContext *ctx, TCGv arg1, TCGv arg2)
76db3ba4
AJ
2508{
2509 tcg_gen_qemu_ld16u(arg1, arg2, ctx->mem_idx);
2510 if (unlikely(ctx->le_mode)) {
fa3966a3 2511 tcg_gen_bswap16_tl(arg1, arg1);
76db3ba4 2512 }
b61f2753
AJ
2513}
2514
636aa200 2515static inline void gen_qemu_ld16s(DisasContext *ctx, TCGv arg1, TCGv arg2)
b61f2753 2516{
76db3ba4 2517 if (unlikely(ctx->le_mode)) {
76db3ba4 2518 tcg_gen_qemu_ld16u(arg1, arg2, ctx->mem_idx);
fa3966a3 2519 tcg_gen_bswap16_tl(arg1, arg1);
76db3ba4 2520 tcg_gen_ext16s_tl(arg1, arg1);
76db3ba4
AJ
2521 } else {
2522 tcg_gen_qemu_ld16s(arg1, arg2, ctx->mem_idx);
2523 }
b61f2753
AJ
2524}
2525
636aa200 2526static inline void gen_qemu_ld32u(DisasContext *ctx, TCGv arg1, TCGv arg2)
b61f2753 2527{
76db3ba4
AJ
2528 tcg_gen_qemu_ld32u(arg1, arg2, ctx->mem_idx);
2529 if (unlikely(ctx->le_mode)) {
fa3966a3 2530 tcg_gen_bswap32_tl(arg1, arg1);
76db3ba4 2531 }
b61f2753
AJ
2532}
2533
76db3ba4 2534#if defined(TARGET_PPC64)
636aa200 2535static inline void gen_qemu_ld32s(DisasContext *ctx, TCGv arg1, TCGv arg2)
b61f2753 2536{
a457e7ee 2537 if (unlikely(ctx->le_mode)) {
76db3ba4 2538 tcg_gen_qemu_ld32u(arg1, arg2, ctx->mem_idx);
fa3966a3
AJ
2539 tcg_gen_bswap32_tl(arg1, arg1);
2540 tcg_gen_ext32s_tl(arg1, arg1);
b61f2753 2541 } else
76db3ba4 2542 tcg_gen_qemu_ld32s(arg1, arg2, ctx->mem_idx);
b61f2753 2543}
76db3ba4 2544#endif
b61f2753 2545
636aa200 2546static inline void gen_qemu_ld64(DisasContext *ctx, TCGv_i64 arg1, TCGv arg2)
b61f2753 2547{
76db3ba4
AJ
2548 tcg_gen_qemu_ld64(arg1, arg2, ctx->mem_idx);
2549 if (unlikely(ctx->le_mode)) {
66896cb8 2550 tcg_gen_bswap64_i64(arg1, arg1);
76db3ba4 2551 }
b61f2753
AJ
2552}
2553
636aa200 2554static inline void gen_qemu_st8(DisasContext *ctx, TCGv arg1, TCGv arg2)
b61f2753 2555{
76db3ba4 2556 tcg_gen_qemu_st8(arg1, arg2, ctx->mem_idx);
b61f2753
AJ
2557}
2558
636aa200 2559static inline void gen_qemu_st16(DisasContext *ctx, TCGv arg1, TCGv arg2)
b61f2753 2560{
76db3ba4 2561 if (unlikely(ctx->le_mode)) {
76db3ba4
AJ
2562 TCGv t0 = tcg_temp_new();
2563 tcg_gen_ext16u_tl(t0, arg1);
fa3966a3 2564 tcg_gen_bswap16_tl(t0, t0);
76db3ba4
AJ
2565 tcg_gen_qemu_st16(t0, arg2, ctx->mem_idx);
2566 tcg_temp_free(t0);
76db3ba4
AJ
2567 } else {
2568 tcg_gen_qemu_st16(arg1, arg2, ctx->mem_idx);
2569 }
b61f2753
AJ
2570}
2571
636aa200 2572static inline void gen_qemu_st32(DisasContext *ctx, TCGv arg1, TCGv arg2)
b61f2753 2573{
76db3ba4 2574 if (unlikely(ctx->le_mode)) {
fa3966a3
AJ
2575 TCGv t0 = tcg_temp_new();
2576 tcg_gen_ext32u_tl(t0, arg1);
2577 tcg_gen_bswap32_tl(t0, t0);
76db3ba4
AJ
2578 tcg_gen_qemu_st32(t0, arg2, ctx->mem_idx);
2579 tcg_temp_free(t0);
76db3ba4
AJ
2580 } else {
2581 tcg_gen_qemu_st32(arg1, arg2, ctx->mem_idx);
2582 }
b61f2753
AJ
2583}
2584
636aa200 2585static inline void gen_qemu_st64(DisasContext *ctx, TCGv_i64 arg1, TCGv arg2)
b61f2753 2586{
76db3ba4 2587 if (unlikely(ctx->le_mode)) {
a7812ae4 2588 TCGv_i64 t0 = tcg_temp_new_i64();
66896cb8 2589 tcg_gen_bswap64_i64(t0, arg1);
76db3ba4 2590 tcg_gen_qemu_st64(t0, arg2, ctx->mem_idx);
a7812ae4 2591 tcg_temp_free_i64(t0);
b61f2753 2592 } else
76db3ba4 2593 tcg_gen_qemu_st64(arg1, arg2, ctx->mem_idx);
b61f2753
AJ
2594}
2595
0c8aacd4 2596#define GEN_LD(name, ldop, opc, type) \
99e300ef 2597static void glue(gen_, name)(DisasContext *ctx) \
79aceca5 2598{ \
76db3ba4
AJ
2599 TCGv EA; \
2600 gen_set_access_type(ctx, ACCESS_INT); \
2601 EA = tcg_temp_new(); \
2602 gen_addr_imm_index(ctx, EA, 0); \
2603 gen_qemu_##ldop(ctx, cpu_gpr[rD(ctx->opcode)], EA); \
b61f2753 2604 tcg_temp_free(EA); \
79aceca5
FB
2605}
2606
0c8aacd4 2607#define GEN_LDU(name, ldop, opc, type) \
99e300ef 2608static void glue(gen_, name##u)(DisasContext *ctx) \
79aceca5 2609{ \
b61f2753 2610 TCGv EA; \
76a66253
JM
2611 if (unlikely(rA(ctx->opcode) == 0 || \
2612 rA(ctx->opcode) == rD(ctx->opcode))) { \
e06fcd75 2613 gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL); \
9fddaa0c 2614 return; \
9a64fbe4 2615 } \
76db3ba4 2616 gen_set_access_type(ctx, ACCESS_INT); \
0c8aacd4 2617 EA = tcg_temp_new(); \
9d53c753 2618 if (type == PPC_64B) \
76db3ba4 2619 gen_addr_imm_index(ctx, EA, 0x03); \
9d53c753 2620 else \
76db3ba4
AJ
2621 gen_addr_imm_index(ctx, EA, 0); \
2622 gen_qemu_##ldop(ctx, cpu_gpr[rD(ctx->opcode)], EA); \
b61f2753
AJ
2623 tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], EA); \
2624 tcg_temp_free(EA); \
79aceca5
FB
2625}
2626
0c8aacd4 2627#define GEN_LDUX(name, ldop, opc2, opc3, type) \
99e300ef 2628static void glue(gen_, name##ux)(DisasContext *ctx) \
79aceca5 2629{ \
b61f2753 2630 TCGv EA; \
76a66253
JM
2631 if (unlikely(rA(ctx->opcode) == 0 || \
2632 rA(ctx->opcode) == rD(ctx->opcode))) { \
e06fcd75 2633 gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL); \
9fddaa0c 2634 return; \
9a64fbe4 2635 } \
76db3ba4 2636 gen_set_access_type(ctx, ACCESS_INT); \
0c8aacd4 2637 EA = tcg_temp_new(); \
76db3ba4
AJ
2638 gen_addr_reg_index(ctx, EA); \
2639 gen_qemu_##ldop(ctx, cpu_gpr[rD(ctx->opcode)], EA); \
b61f2753
AJ
2640 tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], EA); \
2641 tcg_temp_free(EA); \
79aceca5
FB
2642}
2643
cd6e9320 2644#define GEN_LDX_E(name, ldop, opc2, opc3, type, type2) \
99e300ef 2645static void glue(gen_, name##x)(DisasContext *ctx) \
79aceca5 2646{ \
76db3ba4
AJ
2647 TCGv EA; \
2648 gen_set_access_type(ctx, ACCESS_INT); \
2649 EA = tcg_temp_new(); \
2650 gen_addr_reg_index(ctx, EA); \
2651 gen_qemu_##ldop(ctx, cpu_gpr[rD(ctx->opcode)], EA); \
b61f2753 2652 tcg_temp_free(EA); \
79aceca5 2653}
cd6e9320
TH
2654#define GEN_LDX(name, ldop, opc2, opc3, type) \
2655 GEN_LDX_E(name, ldop, opc2, opc3, type, PPC_NONE)
79aceca5 2656
0c8aacd4
AJ
2657#define GEN_LDS(name, ldop, op, type) \
2658GEN_LD(name, ldop, op | 0x20, type); \
2659GEN_LDU(name, ldop, op | 0x21, type); \
2660GEN_LDUX(name, ldop, 0x17, op | 0x01, type); \
2661GEN_LDX(name, ldop, 0x17, op | 0x00, type)
79aceca5
FB
2662
2663/* lbz lbzu lbzux lbzx */
0c8aacd4 2664GEN_LDS(lbz, ld8u, 0x02, PPC_INTEGER);
79aceca5 2665/* lha lhau lhaux lhax */
0c8aacd4 2666GEN_LDS(lha, ld16s, 0x0A, PPC_INTEGER);
79aceca5 2667/* lhz lhzu lhzux lhzx */
0c8aacd4 2668GEN_LDS(lhz, ld16u, 0x08, PPC_INTEGER);
79aceca5 2669/* lwz lwzu lwzux lwzx */
0c8aacd4 2670GEN_LDS(lwz, ld32u, 0x00, PPC_INTEGER);
d9bce9d9 2671#if defined(TARGET_PPC64)
d9bce9d9 2672/* lwaux */
0c8aacd4 2673GEN_LDUX(lwa, ld32s, 0x15, 0x0B, PPC_64B);
d9bce9d9 2674/* lwax */
0c8aacd4 2675GEN_LDX(lwa, ld32s, 0x15, 0x0A, PPC_64B);
d9bce9d9 2676/* ldux */
0c8aacd4 2677GEN_LDUX(ld, ld64, 0x15, 0x01, PPC_64B);
d9bce9d9 2678/* ldx */
0c8aacd4 2679GEN_LDX(ld, ld64, 0x15, 0x00, PPC_64B);
99e300ef
BS
2680
2681static void gen_ld(DisasContext *ctx)
d9bce9d9 2682{
b61f2753 2683 TCGv EA;
d9bce9d9
JM
2684 if (Rc(ctx->opcode)) {
2685 if (unlikely(rA(ctx->opcode) == 0 ||
2686 rA(ctx->opcode) == rD(ctx->opcode))) {
e06fcd75 2687 gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL);
d9bce9d9
JM
2688 return;
2689 }
2690 }
76db3ba4 2691 gen_set_access_type(ctx, ACCESS_INT);
a7812ae4 2692 EA = tcg_temp_new();
76db3ba4 2693 gen_addr_imm_index(ctx, EA, 0x03);
d9bce9d9
JM
2694 if (ctx->opcode & 0x02) {
2695 /* lwa (lwau is undefined) */
76db3ba4 2696 gen_qemu_ld32s(ctx, cpu_gpr[rD(ctx->opcode)], EA);
d9bce9d9
JM
2697 } else {
2698 /* ld - ldu */
76db3ba4 2699 gen_qemu_ld64(ctx, cpu_gpr[rD(ctx->opcode)], EA);
d9bce9d9 2700 }
d9bce9d9 2701 if (Rc(ctx->opcode))
b61f2753
AJ
2702 tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], EA);
2703 tcg_temp_free(EA);
d9bce9d9 2704}
99e300ef 2705
54623277 2706/* lq */
99e300ef 2707static void gen_lq(DisasContext *ctx)
be147d08
JM
2708{
2709#if defined(CONFIG_USER_ONLY)
e06fcd75 2710 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
be147d08
JM
2711#else
2712 int ra, rd;
b61f2753 2713 TCGv EA;
be147d08
JM
2714
2715 /* Restore CPU state */
76db3ba4 2716 if (unlikely(ctx->mem_idx == 0)) {
e06fcd75 2717 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
be147d08
JM
2718 return;
2719 }
2720 ra = rA(ctx->opcode);
2721 rd = rD(ctx->opcode);
2722 if (unlikely((rd & 1) || rd == ra)) {
e06fcd75 2723 gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL);
be147d08
JM
2724 return;
2725 }
76db3ba4 2726 if (unlikely(ctx->le_mode)) {
be147d08 2727 /* Little-endian mode is not handled */
e06fcd75 2728 gen_exception_err(ctx, POWERPC_EXCP_ALIGN, POWERPC_EXCP_ALIGN_LE);
be147d08
JM
2729 return;
2730 }
76db3ba4 2731 gen_set_access_type(ctx, ACCESS_INT);
a7812ae4 2732 EA = tcg_temp_new();
76db3ba4
AJ
2733 gen_addr_imm_index(ctx, EA, 0x0F);
2734 gen_qemu_ld64(ctx, cpu_gpr[rd], EA);
2735 gen_addr_add(ctx, EA, EA, 8);
2736 gen_qemu_ld64(ctx, cpu_gpr[rd+1], EA);
b61f2753 2737 tcg_temp_free(EA);
be147d08
JM
2738#endif
2739}
d9bce9d9 2740#endif
79aceca5
FB
2741
2742/*** Integer store ***/
0c8aacd4 2743#define GEN_ST(name, stop, opc, type) \
99e300ef 2744static void glue(gen_, name)(DisasContext *ctx) \
79aceca5 2745{ \
76db3ba4
AJ
2746 TCGv EA; \
2747 gen_set_access_type(ctx, ACCESS_INT); \
2748 EA = tcg_temp_new(); \
2749 gen_addr_imm_index(ctx, EA, 0); \
2750 gen_qemu_##stop(ctx, cpu_gpr[rS(ctx->opcode)], EA); \
b61f2753 2751 tcg_temp_free(EA); \
79aceca5
FB
2752}
2753
0c8aacd4 2754#define GEN_STU(name, stop, opc, type) \
99e300ef 2755static void glue(gen_, stop##u)(DisasContext *ctx) \
79aceca5 2756{ \
b61f2753 2757 TCGv EA; \
76a66253 2758 if (unlikely(rA(ctx->opcode) == 0)) { \
e06fcd75 2759 gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL); \
9fddaa0c 2760 return; \
9a64fbe4 2761 } \
76db3ba4 2762 gen_set_access_type(ctx, ACCESS_INT); \
0c8aacd4 2763 EA = tcg_temp_new(); \
9d53c753 2764 if (type == PPC_64B) \
76db3ba4 2765 gen_addr_imm_index(ctx, EA, 0x03); \
9d53c753 2766 else \
76db3ba4
AJ
2767 gen_addr_imm_index(ctx, EA, 0); \
2768 gen_qemu_##stop(ctx, cpu_gpr[rS(ctx->opcode)], EA); \
b61f2753
AJ
2769 tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], EA); \
2770 tcg_temp_free(EA); \
79aceca5
FB
2771}
2772
0c8aacd4 2773#define GEN_STUX(name, stop, opc2, opc3, type) \
99e300ef 2774static void glue(gen_, name##ux)(DisasContext *ctx) \
79aceca5 2775{ \
b61f2753 2776 TCGv EA; \
76a66253 2777 if (unlikely(rA(ctx->opcode) == 0)) { \
e06fcd75 2778 gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL); \
9fddaa0c 2779 return; \
9a64fbe4 2780 } \
76db3ba4 2781 gen_set_access_type(ctx, ACCESS_INT); \
0c8aacd4 2782 EA = tcg_temp_new(); \
76db3ba4
AJ
2783 gen_addr_reg_index(ctx, EA); \
2784 gen_qemu_##stop(ctx, cpu_gpr[rS(ctx->opcode)], EA); \
b61f2753
AJ
2785 tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], EA); \
2786 tcg_temp_free(EA); \
79aceca5
FB
2787}
2788
cd6e9320
TH
2789#define GEN_STX_E(name, stop, opc2, opc3, type, type2) \
2790static void glue(gen_, name##x)(DisasContext *ctx) \
79aceca5 2791{ \
76db3ba4
AJ
2792 TCGv EA; \
2793 gen_set_access_type(ctx, ACCESS_INT); \
2794 EA = tcg_temp_new(); \
2795 gen_addr_reg_index(ctx, EA); \
2796 gen_qemu_##stop(ctx, cpu_gpr[rS(ctx->opcode)], EA); \
b61f2753 2797 tcg_temp_free(EA); \
79aceca5 2798}
cd6e9320
TH
2799#define GEN_STX(name, stop, opc2, opc3, type) \
2800 GEN_STX_E(name, stop, opc2, opc3, type, PPC_NONE)
79aceca5 2801
0c8aacd4
AJ
2802#define GEN_STS(name, stop, op, type) \
2803GEN_ST(name, stop, op | 0x20, type); \
2804GEN_STU(name, stop, op | 0x21, type); \
2805GEN_STUX(name, stop, 0x17, op | 0x01, type); \
2806GEN_STX(name, stop, 0x17, op | 0x00, type)
79aceca5
FB
2807
2808/* stb stbu stbux stbx */
0c8aacd4 2809GEN_STS(stb, st8, 0x06, PPC_INTEGER);
79aceca5 2810/* sth sthu sthux sthx */
0c8aacd4 2811GEN_STS(sth, st16, 0x0C, PPC_INTEGER);
79aceca5 2812/* stw stwu stwux stwx */
0c8aacd4 2813GEN_STS(stw, st32, 0x04, PPC_INTEGER);
d9bce9d9 2814#if defined(TARGET_PPC64)
0c8aacd4
AJ
2815GEN_STUX(std, st64, 0x15, 0x05, PPC_64B);
2816GEN_STX(std, st64, 0x15, 0x04, PPC_64B);
99e300ef
BS
2817
2818static void gen_std(DisasContext *ctx)
d9bce9d9 2819{
be147d08 2820 int rs;
b61f2753 2821 TCGv EA;
be147d08
JM
2822
2823 rs = rS(ctx->opcode);
2824 if ((ctx->opcode & 0x3) == 0x2) {
2825#if defined(CONFIG_USER_ONLY)
e06fcd75 2826 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
be147d08
JM
2827#else
2828 /* stq */
76db3ba4 2829 if (unlikely(ctx->mem_idx == 0)) {
e06fcd75 2830 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
be147d08
JM
2831 return;
2832 }
2833 if (unlikely(rs & 1)) {
e06fcd75 2834 gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL);
d9bce9d9
JM
2835 return;
2836 }
76db3ba4 2837 if (unlikely(ctx->le_mode)) {
be147d08 2838 /* Little-endian mode is not handled */
e06fcd75 2839 gen_exception_err(ctx, POWERPC_EXCP_ALIGN, POWERPC_EXCP_ALIGN_LE);
be147d08
JM
2840 return;
2841 }
76db3ba4 2842 gen_set_access_type(ctx, ACCESS_INT);
a7812ae4 2843 EA = tcg_temp_new();
76db3ba4
AJ
2844 gen_addr_imm_index(ctx, EA, 0x03);
2845 gen_qemu_st64(ctx, cpu_gpr[rs], EA);
2846 gen_addr_add(ctx, EA, EA, 8);
2847 gen_qemu_st64(ctx, cpu_gpr[rs+1], EA);
b61f2753 2848 tcg_temp_free(EA);
be147d08
JM
2849#endif
2850 } else {
2851 /* std / stdu */
2852 if (Rc(ctx->opcode)) {
2853 if (unlikely(rA(ctx->opcode) == 0)) {
e06fcd75 2854 gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL);
be147d08
JM
2855 return;
2856 }
2857 }
76db3ba4 2858 gen_set_access_type(ctx, ACCESS_INT);
a7812ae4 2859 EA = tcg_temp_new();
76db3ba4
AJ
2860 gen_addr_imm_index(ctx, EA, 0x03);
2861 gen_qemu_st64(ctx, cpu_gpr[rs], EA);
be147d08 2862 if (Rc(ctx->opcode))
b61f2753
AJ
2863 tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], EA);
2864 tcg_temp_free(EA);
d9bce9d9 2865 }
d9bce9d9
JM
2866}
2867#endif
79aceca5
FB
2868/*** Integer load and store with byte reverse ***/
2869/* lhbrx */
86178a57 2870static inline void gen_qemu_ld16ur(DisasContext *ctx, TCGv arg1, TCGv arg2)
b61f2753 2871{
76db3ba4
AJ
2872 tcg_gen_qemu_ld16u(arg1, arg2, ctx->mem_idx);
2873 if (likely(!ctx->le_mode)) {
fa3966a3 2874 tcg_gen_bswap16_tl(arg1, arg1);
76db3ba4 2875 }
b61f2753 2876}
0c8aacd4 2877GEN_LDX(lhbr, ld16ur, 0x16, 0x18, PPC_INTEGER);
b61f2753 2878
79aceca5 2879/* lwbrx */
86178a57 2880static inline void gen_qemu_ld32ur(DisasContext *ctx, TCGv arg1, TCGv arg2)
b61f2753 2881{
76db3ba4
AJ
2882 tcg_gen_qemu_ld32u(arg1, arg2, ctx->mem_idx);
2883 if (likely(!ctx->le_mode)) {
fa3966a3 2884 tcg_gen_bswap32_tl(arg1, arg1);
76db3ba4 2885 }
b61f2753 2886}
0c8aacd4 2887GEN_LDX(lwbr, ld32ur, 0x16, 0x10, PPC_INTEGER);
b61f2753 2888
cd6e9320
TH
2889#if defined(TARGET_PPC64)
2890/* ldbrx */
2891static inline void gen_qemu_ld64ur(DisasContext *ctx, TCGv arg1, TCGv arg2)
2892{
2893 tcg_gen_qemu_ld64(arg1, arg2, ctx->mem_idx);
2894 if (likely(!ctx->le_mode)) {
2895 tcg_gen_bswap64_tl(arg1, arg1);
2896 }
2897}
2898GEN_LDX_E(ldbr, ld64ur, 0x14, 0x10, PPC_NONE, PPC2_DBRX);
2899#endif /* TARGET_PPC64 */
2900
79aceca5 2901/* sthbrx */
86178a57 2902static inline void gen_qemu_st16r(DisasContext *ctx, TCGv arg1, TCGv arg2)
b61f2753 2903{
76db3ba4 2904 if (likely(!ctx->le_mode)) {
76db3ba4
AJ
2905 TCGv t0 = tcg_temp_new();
2906 tcg_gen_ext16u_tl(t0, arg1);
fa3966a3 2907 tcg_gen_bswap16_tl(t0, t0);
76db3ba4
AJ
2908 tcg_gen_qemu_st16(t0, arg2, ctx->mem_idx);
2909 tcg_temp_free(t0);
76db3ba4
AJ
2910 } else {
2911 tcg_gen_qemu_st16(arg1, arg2, ctx->mem_idx);
2912 }
b61f2753 2913}
0c8aacd4 2914GEN_STX(sthbr, st16r, 0x16, 0x1C, PPC_INTEGER);
b61f2753 2915
79aceca5 2916/* stwbrx */
86178a57 2917static inline void gen_qemu_st32r(DisasContext *ctx, TCGv arg1, TCGv arg2)
b61f2753 2918{
76db3ba4 2919 if (likely(!ctx->le_mode)) {
fa3966a3
AJ
2920 TCGv t0 = tcg_temp_new();
2921 tcg_gen_ext32u_tl(t0, arg1);
2922 tcg_gen_bswap32_tl(t0, t0);
76db3ba4
AJ
2923 tcg_gen_qemu_st32(t0, arg2, ctx->mem_idx);
2924 tcg_temp_free(t0);
76db3ba4
AJ
2925 } else {
2926 tcg_gen_qemu_st32(arg1, arg2, ctx->mem_idx);
2927 }
b61f2753 2928}
0c8aacd4 2929GEN_STX(stwbr, st32r, 0x16, 0x14, PPC_INTEGER);
79aceca5 2930
cd6e9320
TH
2931#if defined(TARGET_PPC64)
2932/* stdbrx */
2933static inline void gen_qemu_st64r(DisasContext *ctx, TCGv arg1, TCGv arg2)
2934{
2935 if (likely(!ctx->le_mode)) {
2936 TCGv t0 = tcg_temp_new();
2937 tcg_gen_bswap64_tl(t0, arg1);
2938 tcg_gen_qemu_st64(t0, arg2, ctx->mem_idx);
2939 tcg_temp_free(t0);
2940 } else {
2941 tcg_gen_qemu_st64(arg1, arg2, ctx->mem_idx);
2942 }
2943}
2944GEN_STX_E(stdbr, st64r, 0x14, 0x14, PPC_NONE, PPC2_DBRX);
2945#endif /* TARGET_PPC64 */
2946
79aceca5 2947/*** Integer load and store multiple ***/
99e300ef 2948
54623277 2949/* lmw */
99e300ef 2950static void gen_lmw(DisasContext *ctx)
79aceca5 2951{
76db3ba4
AJ
2952 TCGv t0;
2953 TCGv_i32 t1;
2954 gen_set_access_type(ctx, ACCESS_INT);
76a66253 2955 /* NIP cannot be restored if the memory exception comes from an helper */
d9bce9d9 2956 gen_update_nip(ctx, ctx->nip - 4);
76db3ba4
AJ
2957 t0 = tcg_temp_new();
2958 t1 = tcg_const_i32(rD(ctx->opcode));
2959 gen_addr_imm_index(ctx, t0, 0);
2f5a189c 2960 gen_helper_lmw(cpu_env, t0, t1);
ff4a62cd
AJ
2961 tcg_temp_free(t0);
2962 tcg_temp_free_i32(t1);
79aceca5
FB
2963}
2964
2965/* stmw */
99e300ef 2966static void gen_stmw(DisasContext *ctx)
79aceca5 2967{
76db3ba4
AJ
2968 TCGv t0;
2969 TCGv_i32 t1;
2970 gen_set_access_type(ctx, ACCESS_INT);
76a66253 2971 /* NIP cannot be restored if the memory exception comes from an helper */
d9bce9d9 2972 gen_update_nip(ctx, ctx->nip - 4);
76db3ba4
AJ
2973 t0 = tcg_temp_new();
2974 t1 = tcg_const_i32(rS(ctx->opcode));
2975 gen_addr_imm_index(ctx, t0, 0);
2f5a189c 2976 gen_helper_stmw(cpu_env, t0, t1);
ff4a62cd
AJ
2977 tcg_temp_free(t0);
2978 tcg_temp_free_i32(t1);
79aceca5
FB
2979}
2980
2981/*** Integer load and store strings ***/
54623277 2982
79aceca5 2983/* lswi */
3fc6c082 2984/* PowerPC32 specification says we must generate an exception if
9a64fbe4
FB
2985 * rA is in the range of registers to be loaded.
2986 * In an other hand, IBM says this is valid, but rA won't be loaded.
2987 * For now, I'll follow the spec...
2988 */
99e300ef 2989static void gen_lswi(DisasContext *ctx)
79aceca5 2990{
dfbc799d
AJ
2991 TCGv t0;
2992 TCGv_i32 t1, t2;
79aceca5
FB
2993 int nb = NB(ctx->opcode);
2994 int start = rD(ctx->opcode);
9a64fbe4 2995 int ra = rA(ctx->opcode);
79aceca5
FB
2996 int nr;
2997
2998 if (nb == 0)
2999 nb = 32;
3000 nr = nb / 4;
76a66253
JM
3001 if (unlikely(((start + nr) > 32 &&
3002 start <= ra && (start + nr - 32) > ra) ||
3003 ((start + nr) <= 32 && start <= ra && (start + nr) > ra))) {
e06fcd75 3004 gen_inval_exception(ctx, POWERPC_EXCP_INVAL_LSWX);
9fddaa0c 3005 return;
297d8e62 3006 }
76db3ba4 3007 gen_set_access_type(ctx, ACCESS_INT);
8dd4983c 3008 /* NIP cannot be restored if the memory exception comes from an helper */
d9bce9d9 3009 gen_update_nip(ctx, ctx->nip - 4);
dfbc799d 3010 t0 = tcg_temp_new();
76db3ba4 3011 gen_addr_register(ctx, t0);
dfbc799d
AJ
3012 t1 = tcg_const_i32(nb);
3013 t2 = tcg_const_i32(start);
2f5a189c 3014 gen_helper_lsw(cpu_env, t0, t1, t2);
dfbc799d
AJ
3015 tcg_temp_free(t0);
3016 tcg_temp_free_i32(t1);
3017 tcg_temp_free_i32(t2);
79aceca5
FB
3018}
3019
3020/* lswx */
99e300ef 3021static void gen_lswx(DisasContext *ctx)
79aceca5 3022{
76db3ba4
AJ
3023 TCGv t0;
3024 TCGv_i32 t1, t2, t3;
3025 gen_set_access_type(ctx, ACCESS_INT);
76a66253 3026 /* NIP cannot be restored if the memory exception comes from an helper */
d9bce9d9 3027 gen_update_nip(ctx, ctx->nip - 4);
76db3ba4
AJ
3028 t0 = tcg_temp_new();
3029 gen_addr_reg_index(ctx, t0);
3030 t1 = tcg_const_i32(rD(ctx->opcode));
3031 t2 = tcg_const_i32(rA(ctx->opcode));
3032 t3 = tcg_const_i32(rB(ctx->opcode));
2f5a189c 3033 gen_helper_lswx(cpu_env, t0, t1, t2, t3);
dfbc799d
AJ
3034 tcg_temp_free(t0);
3035 tcg_temp_free_i32(t1);
3036 tcg_temp_free_i32(t2);
3037 tcg_temp_free_i32(t3);
79aceca5
FB
3038}
3039
3040/* stswi */
99e300ef 3041static void gen_stswi(DisasContext *ctx)
79aceca5 3042{
76db3ba4
AJ
3043 TCGv t0;
3044 TCGv_i32 t1, t2;
4b3686fa 3045 int nb = NB(ctx->opcode);
76db3ba4 3046 gen_set_access_type(ctx, ACCESS_INT);
76a66253 3047 /* NIP cannot be restored if the memory exception comes from an helper */
d9bce9d9 3048 gen_update_nip(ctx, ctx->nip - 4);
76db3ba4
AJ
3049 t0 = tcg_temp_new();
3050 gen_addr_register(ctx, t0);
4b3686fa
FB
3051 if (nb == 0)
3052 nb = 32;
dfbc799d 3053 t1 = tcg_const_i32(nb);
76db3ba4 3054 t2 = tcg_const_i32(rS(ctx->opcode));
2f5a189c 3055 gen_helper_stsw(cpu_env, t0, t1, t2);
dfbc799d
AJ
3056 tcg_temp_free(t0);
3057 tcg_temp_free_i32(t1);
3058 tcg_temp_free_i32(t2);
79aceca5
FB
3059}
3060
3061/* stswx */
99e300ef 3062static void gen_stswx(DisasContext *ctx)
79aceca5 3063{
76db3ba4
AJ
3064 TCGv t0;
3065 TCGv_i32 t1, t2;
3066 gen_set_access_type(ctx, ACCESS_INT);
8dd4983c 3067 /* NIP cannot be restored if the memory exception comes from an helper */
5fafdf24 3068 gen_update_nip(ctx, ctx->nip - 4);
76db3ba4
AJ
3069 t0 = tcg_temp_new();
3070 gen_addr_reg_index(ctx, t0);
3071 t1 = tcg_temp_new_i32();
dfbc799d
AJ
3072 tcg_gen_trunc_tl_i32(t1, cpu_xer);
3073 tcg_gen_andi_i32(t1, t1, 0x7F);
76db3ba4 3074 t2 = tcg_const_i32(rS(ctx->opcode));
2f5a189c 3075 gen_helper_stsw(cpu_env, t0, t1, t2);
dfbc799d
AJ
3076 tcg_temp_free(t0);
3077 tcg_temp_free_i32(t1);
3078 tcg_temp_free_i32(t2);
79aceca5
FB
3079}
3080
3081/*** Memory synchronisation ***/
3082/* eieio */
99e300ef 3083static void gen_eieio(DisasContext *ctx)
79aceca5 3084{
79aceca5
FB
3085}
3086
3087/* isync */
99e300ef 3088static void gen_isync(DisasContext *ctx)
79aceca5 3089{
e06fcd75 3090 gen_stop_exception(ctx);
79aceca5
FB
3091}
3092
111bfab3 3093/* lwarx */
99e300ef 3094static void gen_lwarx(DisasContext *ctx)
79aceca5 3095{
76db3ba4 3096 TCGv t0;
18b21a2f 3097 TCGv gpr = cpu_gpr[rD(ctx->opcode)];
76db3ba4
AJ
3098 gen_set_access_type(ctx, ACCESS_RES);
3099 t0 = tcg_temp_local_new();
3100 gen_addr_reg_index(ctx, t0);
cf360a32 3101 gen_check_align(ctx, t0, 0x03);
18b21a2f 3102 gen_qemu_ld32u(ctx, gpr, t0);
cf360a32 3103 tcg_gen_mov_tl(cpu_reserve, t0);
1328c2bf 3104 tcg_gen_st_tl(gpr, cpu_env, offsetof(CPUPPCState, reserve_val));
cf360a32 3105 tcg_temp_free(t0);
79aceca5
FB
3106}
3107
4425265b
NF
3108#if defined(CONFIG_USER_ONLY)
3109static void gen_conditional_store (DisasContext *ctx, TCGv EA,
3110 int reg, int size)
3111{
3112 TCGv t0 = tcg_temp_new();
3113 uint32_t save_exception = ctx->exception;
3114
1328c2bf 3115 tcg_gen_st_tl(EA, cpu_env, offsetof(CPUPPCState, reserve_ea));
4425265b 3116 tcg_gen_movi_tl(t0, (size << 5) | reg);
1328c2bf 3117 tcg_gen_st_tl(t0, cpu_env, offsetof(CPUPPCState, reserve_info));
4425265b
NF
3118 tcg_temp_free(t0);
3119 gen_update_nip(ctx, ctx->nip-4);
3120 ctx->exception = POWERPC_EXCP_BRANCH;
3121 gen_exception(ctx, POWERPC_EXCP_STCX);
3122 ctx->exception = save_exception;
3123}
3124#endif
3125
79aceca5 3126/* stwcx. */
e8eaa2c0 3127static void gen_stwcx_(DisasContext *ctx)
79aceca5 3128{
76db3ba4
AJ
3129 TCGv t0;
3130 gen_set_access_type(ctx, ACCESS_RES);
3131 t0 = tcg_temp_local_new();
3132 gen_addr_reg_index(ctx, t0);
cf360a32 3133 gen_check_align(ctx, t0, 0x03);
4425265b
NF
3134#if defined(CONFIG_USER_ONLY)
3135 gen_conditional_store(ctx, t0, rS(ctx->opcode), 4);
3136#else
3137 {
3138 int l1;
3139
da91a00f 3140 tcg_gen_trunc_tl_i32(cpu_crf[0], cpu_so);
4425265b
NF
3141 l1 = gen_new_label();
3142 tcg_gen_brcond_tl(TCG_COND_NE, t0, cpu_reserve, l1);
3143 tcg_gen_ori_i32(cpu_crf[0], cpu_crf[0], 1 << CRF_EQ);
3144 gen_qemu_st32(ctx, cpu_gpr[rS(ctx->opcode)], t0);
3145 gen_set_label(l1);
3146 tcg_gen_movi_tl(cpu_reserve, -1);
3147 }
3148#endif
cf360a32 3149 tcg_temp_free(t0);
79aceca5
FB
3150}
3151
426613db 3152#if defined(TARGET_PPC64)
426613db 3153/* ldarx */
99e300ef 3154static void gen_ldarx(DisasContext *ctx)
426613db 3155{
76db3ba4 3156 TCGv t0;
18b21a2f 3157 TCGv gpr = cpu_gpr[rD(ctx->opcode)];
76db3ba4
AJ
3158 gen_set_access_type(ctx, ACCESS_RES);
3159 t0 = tcg_temp_local_new();
3160 gen_addr_reg_index(ctx, t0);
cf360a32 3161 gen_check_align(ctx, t0, 0x07);
18b21a2f 3162 gen_qemu_ld64(ctx, gpr, t0);
cf360a32 3163 tcg_gen_mov_tl(cpu_reserve, t0);
1328c2bf 3164 tcg_gen_st_tl(gpr, cpu_env, offsetof(CPUPPCState, reserve_val));
cf360a32 3165 tcg_temp_free(t0);
426613db
JM
3166}
3167
3168/* stdcx. */
e8eaa2c0 3169static void gen_stdcx_(DisasContext *ctx)
426613db 3170{
76db3ba4
AJ
3171 TCGv t0;
3172 gen_set_access_type(ctx, ACCESS_RES);
3173 t0 = tcg_temp_local_new();
3174 gen_addr_reg_index(ctx, t0);
cf360a32 3175 gen_check_align(ctx, t0, 0x07);
4425265b
NF
3176#if defined(CONFIG_USER_ONLY)
3177 gen_conditional_store(ctx, t0, rS(ctx->opcode), 8);
3178#else
3179 {
3180 int l1;
da91a00f 3181 tcg_gen_trunc_tl_i32(cpu_crf[0], cpu_so);
4425265b
NF
3182 l1 = gen_new_label();
3183 tcg_gen_brcond_tl(TCG_COND_NE, t0, cpu_reserve, l1);
3184 tcg_gen_ori_i32(cpu_crf[0], cpu_crf[0], 1 << CRF_EQ);
3185 gen_qemu_st64(ctx, cpu_gpr[rS(ctx->opcode)], t0);
3186 gen_set_label(l1);
3187 tcg_gen_movi_tl(cpu_reserve, -1);
3188 }
3189#endif
cf360a32 3190 tcg_temp_free(t0);
426613db
JM
3191}
3192#endif /* defined(TARGET_PPC64) */
3193
79aceca5 3194/* sync */
99e300ef 3195static void gen_sync(DisasContext *ctx)
79aceca5 3196{
79aceca5
FB
3197}
3198
0db1b20e 3199/* wait */
99e300ef 3200static void gen_wait(DisasContext *ctx)
0db1b20e 3201{
931ff272 3202 TCGv_i32 t0 = tcg_temp_new_i32();
259186a7
AF
3203 tcg_gen_st_i32(t0, cpu_env,
3204 -offsetof(PowerPCCPU, env) + offsetof(CPUState, halted));
931ff272 3205 tcg_temp_free_i32(t0);
0db1b20e 3206 /* Stop translation, as the CPU is supposed to sleep from now */
e06fcd75 3207 gen_exception_err(ctx, EXCP_HLT, 1);
0db1b20e
JM
3208}
3209
79aceca5 3210/*** Floating-point load ***/
a0d7d5a7 3211#define GEN_LDF(name, ldop, opc, type) \
99e300ef 3212static void glue(gen_, name)(DisasContext *ctx) \
79aceca5 3213{ \
a0d7d5a7 3214 TCGv EA; \
76a66253 3215 if (unlikely(!ctx->fpu_enabled)) { \
e06fcd75 3216 gen_exception(ctx, POWERPC_EXCP_FPU); \
4ecc3190
FB
3217 return; \
3218 } \
76db3ba4 3219 gen_set_access_type(ctx, ACCESS_FLOAT); \
a0d7d5a7 3220 EA = tcg_temp_new(); \
76db3ba4
AJ
3221 gen_addr_imm_index(ctx, EA, 0); \
3222 gen_qemu_##ldop(ctx, cpu_fpr[rD(ctx->opcode)], EA); \
a0d7d5a7 3223 tcg_temp_free(EA); \
79aceca5
FB
3224}
3225
a0d7d5a7 3226#define GEN_LDUF(name, ldop, opc, type) \
99e300ef 3227static void glue(gen_, name##u)(DisasContext *ctx) \
79aceca5 3228{ \
a0d7d5a7 3229 TCGv EA; \
76a66253 3230 if (unlikely(!ctx->fpu_enabled)) { \
e06fcd75 3231 gen_exception(ctx, POWERPC_EXCP_FPU); \
4ecc3190
FB
3232 return; \
3233 } \
76a66253 3234 if (unlikely(rA(ctx->opcode) == 0)) { \
e06fcd75 3235 gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL); \
9fddaa0c 3236 return; \
9a64fbe4 3237 } \
76db3ba4 3238 gen_set_access_type(ctx, ACCESS_FLOAT); \
a0d7d5a7 3239 EA = tcg_temp_new(); \
76db3ba4
AJ
3240 gen_addr_imm_index(ctx, EA, 0); \
3241 gen_qemu_##ldop(ctx, cpu_fpr[rD(ctx->opcode)], EA); \
a0d7d5a7
AJ
3242 tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], EA); \
3243 tcg_temp_free(EA); \
79aceca5
FB
3244}
3245
a0d7d5a7 3246#define GEN_LDUXF(name, ldop, opc, type) \
99e300ef 3247static void glue(gen_, name##ux)(DisasContext *ctx) \
79aceca5 3248{ \
a0d7d5a7 3249 TCGv EA; \
76a66253 3250 if (unlikely(!ctx->fpu_enabled)) { \
e06fcd75 3251 gen_exception(ctx, POWERPC_EXCP_FPU); \
4ecc3190
FB
3252 return; \
3253 } \
76a66253 3254 if (unlikely(rA(ctx->opcode) == 0)) { \
e06fcd75 3255 gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL); \
9fddaa0c 3256 return; \
9a64fbe4 3257 } \
76db3ba4 3258 gen_set_access_type(ctx, ACCESS_FLOAT); \
a0d7d5a7 3259 EA = tcg_temp_new(); \
76db3ba4
AJ
3260 gen_addr_reg_index(ctx, EA); \
3261 gen_qemu_##ldop(ctx, cpu_fpr[rD(ctx->opcode)], EA); \
a0d7d5a7
AJ
3262 tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], EA); \
3263 tcg_temp_free(EA); \
79aceca5
FB
3264}
3265
a0d7d5a7 3266#define GEN_LDXF(name, ldop, opc2, opc3, type) \
99e300ef 3267static void glue(gen_, name##x)(DisasContext *ctx) \
79aceca5 3268{ \
a0d7d5a7 3269 TCGv EA; \
76a66253 3270 if (unlikely(!ctx->fpu_enabled)) { \
e06fcd75 3271 gen_exception(ctx, POWERPC_EXCP_FPU); \
4ecc3190
FB
3272 return; \
3273 } \
76db3ba4 3274 gen_set_access_type(ctx, ACCESS_FLOAT); \
a0d7d5a7 3275 EA = tcg_temp_new(); \
76db3ba4
AJ
3276 gen_addr_reg_index(ctx, EA); \
3277 gen_qemu_##ldop(ctx, cpu_fpr[rD(ctx->opcode)], EA); \
a0d7d5a7 3278 tcg_temp_free(EA); \
79aceca5
FB
3279}
3280
a0d7d5a7
AJ
3281#define GEN_LDFS(name, ldop, op, type) \
3282GEN_LDF(name, ldop, op | 0x20, type); \
3283GEN_LDUF(name, ldop, op | 0x21, type); \
3284GEN_LDUXF(name, ldop, op | 0x01, type); \
3285GEN_LDXF(name, ldop, 0x17, op | 0x00, type)
3286
636aa200 3287static inline void gen_qemu_ld32fs(DisasContext *ctx, TCGv_i64 arg1, TCGv arg2)
a0d7d5a7
AJ
3288{
3289 TCGv t0 = tcg_temp_new();
3290 TCGv_i32 t1 = tcg_temp_new_i32();
76db3ba4 3291 gen_qemu_ld32u(ctx, t0, arg2);
a0d7d5a7
AJ
3292 tcg_gen_trunc_tl_i32(t1, t0);
3293 tcg_temp_free(t0);
8e703949 3294 gen_helper_float32_to_float64(arg1, cpu_env, t1);
a0d7d5a7
AJ
3295 tcg_temp_free_i32(t1);
3296}
79aceca5 3297
a0d7d5a7
AJ
3298 /* lfd lfdu lfdux lfdx */
3299GEN_LDFS(lfd, ld64, 0x12, PPC_FLOAT);
3300 /* lfs lfsu lfsux lfsx */
3301GEN_LDFS(lfs, ld32fs, 0x10, PPC_FLOAT);
79aceca5 3302
199f830d
AJ
3303/* lfiwax */
3304static void gen_lfiwax(DisasContext *ctx)
3305{
3306 TCGv EA;
3307 TCGv t0;
3308 if (unlikely(!ctx->fpu_enabled)) {
3309 gen_exception(ctx, POWERPC_EXCP_FPU);
3310 return;
3311 }
3312 gen_set_access_type(ctx, ACCESS_FLOAT);
3313 EA = tcg_temp_new();
3314 t0 = tcg_temp_new();
3315 gen_addr_reg_index(ctx, EA);
3316 gen_qemu_ld32u(ctx, t0, EA);
3317 tcg_gen_ext_tl_i64(cpu_fpr[rD(ctx->opcode)], t0);
3318 tcg_gen_ext32s_i64(cpu_fpr[rD(ctx->opcode)], cpu_fpr[rD(ctx->opcode)]);
3319 tcg_temp_free(EA);
3320 tcg_temp_free(t0);
3321}
3322
79aceca5 3323/*** Floating-point store ***/
a0d7d5a7 3324#define GEN_STF(name, stop, opc, type) \
99e300ef 3325static void glue(gen_, name)(DisasContext *ctx) \
79aceca5 3326{ \
a0d7d5a7 3327 TCGv EA; \
76a66253 3328 if (unlikely(!ctx->fpu_enabled)) { \
e06fcd75 3329 gen_exception(ctx, POWERPC_EXCP_FPU); \
4ecc3190
FB
3330 return; \
3331 } \
76db3ba4 3332 gen_set_access_type(ctx, ACCESS_FLOAT); \
a0d7d5a7 3333 EA = tcg_temp_new(); \
76db3ba4
AJ
3334 gen_addr_imm_index(ctx, EA, 0); \
3335 gen_qemu_##stop(ctx, cpu_fpr[rS(ctx->opcode)], EA); \
a0d7d5a7 3336 tcg_temp_free(EA); \
79aceca5
FB
3337}
3338
a0d7d5a7 3339#define GEN_STUF(name, stop, opc, type) \
99e300ef 3340static void glue(gen_, name##u)(DisasContext *ctx) \
79aceca5 3341{ \
a0d7d5a7 3342 TCGv EA; \
76a66253 3343 if (unlikely(!ctx->fpu_enabled)) { \
e06fcd75 3344 gen_exception(ctx, POWERPC_EXCP_FPU); \
4ecc3190
FB
3345 return; \
3346 } \
76a66253 3347 if (unlikely(rA(ctx->opcode) == 0)) { \
e06fcd75 3348 gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL); \
9fddaa0c 3349 return; \
9a64fbe4 3350 } \
76db3ba4 3351 gen_set_access_type(ctx, ACCESS_FLOAT); \
a0d7d5a7 3352 EA = tcg_temp_new(); \
76db3ba4
AJ
3353 gen_addr_imm_index(ctx, EA, 0); \
3354 gen_qemu_##stop(ctx, cpu_fpr[rS(ctx->opcode)], EA); \
a0d7d5a7
AJ
3355 tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], EA); \
3356 tcg_temp_free(EA); \
79aceca5
FB
3357}
3358
a0d7d5a7 3359#define GEN_STUXF(name, stop, opc, type) \
99e300ef 3360static void glue(gen_, name##ux)(DisasContext *ctx) \
79aceca5 3361{ \
a0d7d5a7 3362 TCGv EA; \
76a66253 3363 if (unlikely(!ctx->fpu_enabled)) { \
e06fcd75 3364 gen_exception(ctx, POWERPC_EXCP_FPU); \
4ecc3190
FB
3365 return; \
3366 } \
76a66253 3367 if (unlikely(rA(ctx->opcode) == 0)) { \
e06fcd75 3368 gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL); \
9fddaa0c 3369 return; \
9a64fbe4 3370 } \
76db3ba4 3371 gen_set_access_type(ctx, ACCESS_FLOAT); \
a0d7d5a7 3372 EA = tcg_temp_new(); \
76db3ba4
AJ
3373 gen_addr_reg_index(ctx, EA); \
3374 gen_qemu_##stop(ctx, cpu_fpr[rS(ctx->opcode)], EA); \
a0d7d5a7
AJ
3375 tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], EA); \
3376 tcg_temp_free(EA); \
79aceca5
FB
3377}
3378
a0d7d5a7 3379#define GEN_STXF(name, stop, opc2, opc3, type) \
99e300ef 3380static void glue(gen_, name##x)(DisasContext *ctx) \
79aceca5 3381{ \
a0d7d5a7 3382 TCGv EA; \
76a66253 3383 if (unlikely(!ctx->fpu_enabled)) { \
e06fcd75 3384 gen_exception(ctx, POWERPC_EXCP_FPU); \
4ecc3190
FB
3385 return; \
3386 } \
76db3ba4 3387 gen_set_access_type(ctx, ACCESS_FLOAT); \
a0d7d5a7 3388 EA = tcg_temp_new(); \
76db3ba4
AJ
3389 gen_addr_reg_index(ctx, EA); \
3390 gen_qemu_##stop(ctx, cpu_fpr[rS(ctx->opcode)], EA); \
a0d7d5a7 3391 tcg_temp_free(EA); \
79aceca5
FB
3392}
3393
a0d7d5a7
AJ
3394#define GEN_STFS(name, stop, op, type) \
3395GEN_STF(name, stop, op | 0x20, type); \
3396GEN_STUF(name, stop, op | 0x21, type); \
3397GEN_STUXF(name, stop, op | 0x01, type); \
3398GEN_STXF(name, stop, 0x17, op | 0x00, type)
3399
636aa200 3400static inline void gen_qemu_st32fs(DisasContext *ctx, TCGv_i64 arg1, TCGv arg2)
a0d7d5a7
AJ
3401{
3402 TCGv_i32 t0 = tcg_temp_new_i32();
3403 TCGv t1 = tcg_temp_new();
8e703949 3404 gen_helper_float64_to_float32(t0, cpu_env, arg1);
a0d7d5a7
AJ
3405 tcg_gen_extu_i32_tl(t1, t0);
3406 tcg_temp_free_i32(t0);
76db3ba4 3407 gen_qemu_st32(ctx, t1, arg2);
a0d7d5a7
AJ
3408 tcg_temp_free(t1);
3409}
79aceca5
FB
3410
3411/* stfd stfdu stfdux stfdx */
a0d7d5a7 3412GEN_STFS(stfd, st64, 0x16, PPC_FLOAT);
79aceca5 3413/* stfs stfsu stfsux stfsx */
a0d7d5a7 3414GEN_STFS(stfs, st32fs, 0x14, PPC_FLOAT);
79aceca5
FB
3415
3416/* Optional: */
636aa200 3417static inline void gen_qemu_st32fiw(DisasContext *ctx, TCGv_i64 arg1, TCGv arg2)
a0d7d5a7
AJ
3418{
3419 TCGv t0 = tcg_temp_new();
3420 tcg_gen_trunc_i64_tl(t0, arg1),
76db3ba4 3421 gen_qemu_st32(ctx, t0, arg2);
a0d7d5a7
AJ
3422 tcg_temp_free(t0);
3423}
79aceca5 3424/* stfiwx */
a0d7d5a7 3425GEN_STXF(stfiw, st32fiw, 0x17, 0x1E, PPC_FLOAT_STFIWX);
79aceca5 3426
697ab892
DG
3427static inline void gen_update_cfar(DisasContext *ctx, target_ulong nip)
3428{
3429#if defined(TARGET_PPC64)
3430 if (ctx->has_cfar)
3431 tcg_gen_movi_tl(cpu_cfar, nip);
3432#endif
3433}
3434
79aceca5 3435/*** Branch ***/
636aa200 3436static inline void gen_goto_tb(DisasContext *ctx, int n, target_ulong dest)
c1942362
FB
3437{
3438 TranslationBlock *tb;
3439 tb = ctx->tb;
e0c8f9ce 3440 if (NARROW_MODE(ctx)) {
a2ffb812 3441 dest = (uint32_t) dest;
e0c8f9ce 3442 }
57fec1fe 3443 if ((tb->pc & TARGET_PAGE_MASK) == (dest & TARGET_PAGE_MASK) &&
8cbcb4fa 3444 likely(!ctx->singlestep_enabled)) {
57fec1fe 3445 tcg_gen_goto_tb(n);
a2ffb812 3446 tcg_gen_movi_tl(cpu_nip, dest & ~3);
4b4a72e5 3447 tcg_gen_exit_tb((tcg_target_long)tb + n);
c1942362 3448 } else {
a2ffb812 3449 tcg_gen_movi_tl(cpu_nip, dest & ~3);
8cbcb4fa
AJ
3450 if (unlikely(ctx->singlestep_enabled)) {
3451 if ((ctx->singlestep_enabled &
bdc4e053 3452 (CPU_BRANCH_STEP | CPU_SINGLE_STEP)) &&
f0cc4aa8
JG
3453 (ctx->exception == POWERPC_EXCP_BRANCH ||
3454 ctx->exception == POWERPC_EXCP_TRACE)) {
8cbcb4fa
AJ
3455 target_ulong tmp = ctx->nip;
3456 ctx->nip = dest;
e06fcd75 3457 gen_exception(ctx, POWERPC_EXCP_TRACE);
8cbcb4fa
AJ
3458 ctx->nip = tmp;
3459 }
3460 if (ctx->singlestep_enabled & GDBSTUB_SINGLE_STEP) {
e06fcd75 3461 gen_debug_exception(ctx);
8cbcb4fa
AJ
3462 }
3463 }
57fec1fe 3464 tcg_gen_exit_tb(0);
c1942362 3465 }
c53be334
FB
3466}
3467
636aa200 3468static inline void gen_setlr(DisasContext *ctx, target_ulong nip)
e1833e1f 3469{
e0c8f9ce
RH
3470 if (NARROW_MODE(ctx)) {
3471 nip = (uint32_t)nip;
3472 }
3473 tcg_gen_movi_tl(cpu_lr, nip);
e1833e1f
JM
3474}
3475
79aceca5 3476/* b ba bl bla */
99e300ef 3477static void gen_b(DisasContext *ctx)
79aceca5 3478{
76a66253 3479 target_ulong li, target;
38a64f9d 3480
8cbcb4fa 3481 ctx->exception = POWERPC_EXCP_BRANCH;
38a64f9d 3482 /* sign extend LI */
e0c8f9ce
RH
3483 li = LI(ctx->opcode);
3484 li = (li ^ 0x02000000) - 0x02000000;
3485 if (likely(AA(ctx->opcode) == 0)) {
046d6672 3486 target = ctx->nip + li - 4;
e0c8f9ce 3487 } else {
9a64fbe4 3488 target = li;
e0c8f9ce
RH
3489 }
3490 if (LK(ctx->opcode)) {
e1833e1f 3491 gen_setlr(ctx, ctx->nip);
e0c8f9ce 3492 }
697ab892 3493 gen_update_cfar(ctx, ctx->nip);
c1942362 3494 gen_goto_tb(ctx, 0, target);
79aceca5
FB
3495}
3496
e98a6e40
FB
3497#define BCOND_IM 0
3498#define BCOND_LR 1
3499#define BCOND_CTR 2
3500
636aa200 3501static inline void gen_bcond(DisasContext *ctx, int type)
d9bce9d9 3502{
d9bce9d9 3503 uint32_t bo = BO(ctx->opcode);
05f92404 3504 int l1;
a2ffb812 3505 TCGv target;
e98a6e40 3506
8cbcb4fa 3507 ctx->exception = POWERPC_EXCP_BRANCH;
a2ffb812 3508 if (type == BCOND_LR || type == BCOND_CTR) {
a7812ae4 3509 target = tcg_temp_local_new();
a2ffb812
AJ
3510 if (type == BCOND_CTR)
3511 tcg_gen_mov_tl(target, cpu_ctr);
3512 else
3513 tcg_gen_mov_tl(target, cpu_lr);
d2e9fd8f 3514 } else {
3515 TCGV_UNUSED(target);
e98a6e40 3516 }
e1833e1f
JM
3517 if (LK(ctx->opcode))
3518 gen_setlr(ctx, ctx->nip);
a2ffb812
AJ
3519 l1 = gen_new_label();
3520 if ((bo & 0x4) == 0) {
3521 /* Decrement and test CTR */
a7812ae4 3522 TCGv temp = tcg_temp_new();
a2ffb812 3523 if (unlikely(type == BCOND_CTR)) {
e06fcd75 3524 gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL);
a2ffb812
AJ
3525 return;
3526 }
3527 tcg_gen_subi_tl(cpu_ctr, cpu_ctr, 1);
e0c8f9ce 3528 if (NARROW_MODE(ctx)) {
a2ffb812 3529 tcg_gen_ext32u_tl(temp, cpu_ctr);
e0c8f9ce 3530 } else {
a2ffb812 3531 tcg_gen_mov_tl(temp, cpu_ctr);
e0c8f9ce 3532 }
a2ffb812
AJ
3533 if (bo & 0x2) {
3534 tcg_gen_brcondi_tl(TCG_COND_NE, temp, 0, l1);
3535 } else {
3536 tcg_gen_brcondi_tl(TCG_COND_EQ, temp, 0, l1);
e98a6e40 3537 }
a7812ae4 3538 tcg_temp_free(temp);
a2ffb812
AJ
3539 }
3540 if ((bo & 0x10) == 0) {
3541 /* Test CR */
3542 uint32_t bi = BI(ctx->opcode);
3543 uint32_t mask = 1 << (3 - (bi & 0x03));
a7812ae4 3544 TCGv_i32 temp = tcg_temp_new_i32();
a2ffb812 3545
d9bce9d9 3546 if (bo & 0x8) {
a2ffb812
AJ
3547 tcg_gen_andi_i32(temp, cpu_crf[bi >> 2], mask);
3548 tcg_gen_brcondi_i32(TCG_COND_EQ, temp, 0, l1);
d9bce9d9 3549 } else {
a2ffb812
AJ
3550 tcg_gen_andi_i32(temp, cpu_crf[bi >> 2], mask);
3551 tcg_gen_brcondi_i32(TCG_COND_NE, temp, 0, l1);
d9bce9d9 3552 }
a7812ae4 3553 tcg_temp_free_i32(temp);
d9bce9d9 3554 }
697ab892 3555 gen_update_cfar(ctx, ctx->nip);
e98a6e40 3556 if (type == BCOND_IM) {
a2ffb812
AJ
3557 target_ulong li = (target_long)((int16_t)(BD(ctx->opcode)));
3558 if (likely(AA(ctx->opcode) == 0)) {
3559 gen_goto_tb(ctx, 0, ctx->nip + li - 4);
3560 } else {
3561 gen_goto_tb(ctx, 0, li);
3562 }
c53be334 3563 gen_set_label(l1);
c1942362 3564 gen_goto_tb(ctx, 1, ctx->nip);
e98a6e40 3565 } else {
e0c8f9ce 3566 if (NARROW_MODE(ctx)) {
a2ffb812 3567 tcg_gen_andi_tl(cpu_nip, target, (uint32_t)~3);
e0c8f9ce 3568 } else {
a2ffb812 3569 tcg_gen_andi_tl(cpu_nip, target, ~3);
e0c8f9ce 3570 }
a2ffb812
AJ
3571 tcg_gen_exit_tb(0);
3572 gen_set_label(l1);
e0c8f9ce 3573 gen_update_nip(ctx, ctx->nip);
57fec1fe 3574 tcg_gen_exit_tb(0);
08e46e54 3575 }
e98a6e40
FB
3576}
3577
99e300ef 3578static void gen_bc(DisasContext *ctx)
3b46e624 3579{
e98a6e40
FB
3580 gen_bcond(ctx, BCOND_IM);
3581}
3582
99e300ef 3583static void gen_bcctr(DisasContext *ctx)
3b46e624 3584{
e98a6e40
FB
3585 gen_bcond(ctx, BCOND_CTR);
3586}
3587
99e300ef 3588static void gen_bclr(DisasContext *ctx)
3b46e624 3589{
e98a6e40
FB
3590 gen_bcond(ctx, BCOND_LR);
3591}
79aceca5
FB
3592
3593/*** Condition register logical ***/
e1571908 3594#define GEN_CRLOGIC(name, tcg_op, opc) \
99e300ef 3595static void glue(gen_, name)(DisasContext *ctx) \
79aceca5 3596{ \
fc0d441e
JM
3597 uint8_t bitmask; \
3598 int sh; \
a7812ae4 3599 TCGv_i32 t0, t1; \
fc0d441e 3600 sh = (crbD(ctx->opcode) & 0x03) - (crbA(ctx->opcode) & 0x03); \
a7812ae4 3601 t0 = tcg_temp_new_i32(); \
fc0d441e 3602 if (sh > 0) \
fea0c503 3603 tcg_gen_shri_i32(t0, cpu_crf[crbA(ctx->opcode) >> 2], sh); \
fc0d441e 3604 else if (sh < 0) \
fea0c503 3605 tcg_gen_shli_i32(t0, cpu_crf[crbA(ctx->opcode) >> 2], -sh); \
e1571908 3606 else \
fea0c503 3607 tcg_gen_mov_i32(t0, cpu_crf[crbA(ctx->opcode) >> 2]); \
a7812ae4 3608 t1 = tcg_temp_new_i32(); \
fc0d441e
JM
3609 sh = (crbD(ctx->opcode) & 0x03) - (crbB(ctx->opcode) & 0x03); \
3610 if (sh > 0) \
fea0c503 3611 tcg_gen_shri_i32(t1, cpu_crf[crbB(ctx->opcode) >> 2], sh); \
fc0d441e 3612 else if (sh < 0) \
fea0c503 3613 tcg_gen_shli_i32(t1, cpu_crf[crbB(ctx->opcode) >> 2], -sh); \
e1571908 3614 else \
fea0c503
AJ
3615 tcg_gen_mov_i32(t1, cpu_crf[crbB(ctx->opcode) >> 2]); \
3616 tcg_op(t0, t0, t1); \
fc0d441e 3617 bitmask = 1 << (3 - (crbD(ctx->opcode) & 0x03)); \
fea0c503
AJ
3618 tcg_gen_andi_i32(t0, t0, bitmask); \
3619 tcg_gen_andi_i32(t1, cpu_crf[crbD(ctx->opcode) >> 2], ~bitmask); \
3620 tcg_gen_or_i32(cpu_crf[crbD(ctx->opcode) >> 2], t0, t1); \
a7812ae4
PB
3621 tcg_temp_free_i32(t0); \
3622 tcg_temp_free_i32(t1); \
79aceca5
FB
3623}
3624
3625/* crand */
e1571908 3626GEN_CRLOGIC(crand, tcg_gen_and_i32, 0x08);
79aceca5 3627/* crandc */
e1571908 3628GEN_CRLOGIC(crandc, tcg_gen_andc_i32, 0x04);
79aceca5 3629/* creqv */
e1571908 3630GEN_CRLOGIC(creqv, tcg_gen_eqv_i32, 0x09);
79aceca5 3631/* crnand */
e1571908 3632GEN_CRLOGIC(crnand, tcg_gen_nand_i32, 0x07);
79aceca5 3633/* crnor */
e1571908 3634GEN_CRLOGIC(crnor, tcg_gen_nor_i32, 0x01);
79aceca5 3635/* cror */
e1571908 3636GEN_CRLOGIC(cror, tcg_gen_or_i32, 0x0E);
79aceca5 3637/* crorc */
e1571908 3638GEN_CRLOGIC(crorc, tcg_gen_orc_i32, 0x0D);
79aceca5 3639/* crxor */
e1571908 3640GEN_CRLOGIC(crxor, tcg_gen_xor_i32, 0x06);
99e300ef 3641
54623277 3642/* mcrf */
99e300ef 3643static void gen_mcrf(DisasContext *ctx)
79aceca5 3644{
47e4661c 3645 tcg_gen_mov_i32(cpu_crf[crfD(ctx->opcode)], cpu_crf[crfS(ctx->opcode)]);
79aceca5
FB
3646}
3647
3648/*** System linkage ***/
99e300ef 3649
54623277 3650/* rfi (mem_idx only) */
99e300ef 3651static void gen_rfi(DisasContext *ctx)
79aceca5 3652{
9a64fbe4 3653#if defined(CONFIG_USER_ONLY)
e06fcd75 3654 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
9a64fbe4
FB
3655#else
3656 /* Restore CPU state */
76db3ba4 3657 if (unlikely(!ctx->mem_idx)) {
e06fcd75 3658 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
9fddaa0c 3659 return;
9a64fbe4 3660 }
697ab892 3661 gen_update_cfar(ctx, ctx->nip);
e5f17ac6 3662 gen_helper_rfi(cpu_env);
e06fcd75 3663 gen_sync_exception(ctx);
9a64fbe4 3664#endif
79aceca5
FB
3665}
3666
426613db 3667#if defined(TARGET_PPC64)
99e300ef 3668static void gen_rfid(DisasContext *ctx)
426613db
JM
3669{
3670#if defined(CONFIG_USER_ONLY)
e06fcd75 3671 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
426613db
JM
3672#else
3673 /* Restore CPU state */
76db3ba4 3674 if (unlikely(!ctx->mem_idx)) {
e06fcd75 3675 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
426613db
JM
3676 return;
3677 }
697ab892 3678 gen_update_cfar(ctx, ctx->nip);
e5f17ac6 3679 gen_helper_rfid(cpu_env);
e06fcd75 3680 gen_sync_exception(ctx);
426613db
JM
3681#endif
3682}
426613db 3683
99e300ef 3684static void gen_hrfid(DisasContext *ctx)
be147d08
JM
3685{
3686#if defined(CONFIG_USER_ONLY)
e06fcd75 3687 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
be147d08
JM
3688#else
3689 /* Restore CPU state */
76db3ba4 3690 if (unlikely(ctx->mem_idx <= 1)) {
e06fcd75 3691 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
be147d08
JM
3692 return;
3693 }
e5f17ac6 3694 gen_helper_hrfid(cpu_env);
e06fcd75 3695 gen_sync_exception(ctx);
be147d08
JM
3696#endif
3697}
3698#endif
3699
79aceca5 3700/* sc */
417bf010
JM
3701#if defined(CONFIG_USER_ONLY)
3702#define POWERPC_SYSCALL POWERPC_EXCP_SYSCALL_USER
3703#else
3704#define POWERPC_SYSCALL POWERPC_EXCP_SYSCALL
3705#endif
99e300ef 3706static void gen_sc(DisasContext *ctx)
79aceca5 3707{
e1833e1f
JM
3708 uint32_t lev;
3709
3710 lev = (ctx->opcode >> 5) & 0x7F;
e06fcd75 3711 gen_exception_err(ctx, POWERPC_SYSCALL, lev);
79aceca5
FB
3712}
3713
3714/*** Trap ***/
99e300ef 3715
54623277 3716/* tw */
99e300ef 3717static void gen_tw(DisasContext *ctx)
79aceca5 3718{
cab3bee2 3719 TCGv_i32 t0 = tcg_const_i32(TO(ctx->opcode));
db9a231d
AJ
3720 /* Update the nip since this might generate a trap exception */
3721 gen_update_nip(ctx, ctx->nip);
e5f17ac6
BS
3722 gen_helper_tw(cpu_env, cpu_gpr[rA(ctx->opcode)], cpu_gpr[rB(ctx->opcode)],
3723 t0);
cab3bee2 3724 tcg_temp_free_i32(t0);
79aceca5
FB
3725}
3726
3727/* twi */
99e300ef 3728static void gen_twi(DisasContext *ctx)
79aceca5 3729{
cab3bee2
AJ
3730 TCGv t0 = tcg_const_tl(SIMM(ctx->opcode));
3731 TCGv_i32 t1 = tcg_const_i32(TO(ctx->opcode));
db9a231d
AJ
3732 /* Update the nip since this might generate a trap exception */
3733 gen_update_nip(ctx, ctx->nip);
e5f17ac6 3734 gen_helper_tw(cpu_env, cpu_gpr[rA(ctx->opcode)], t0, t1);
cab3bee2
AJ
3735 tcg_temp_free(t0);
3736 tcg_temp_free_i32(t1);
79aceca5
FB
3737}
3738
d9bce9d9
JM
3739#if defined(TARGET_PPC64)
3740/* td */
99e300ef 3741static void gen_td(DisasContext *ctx)
d9bce9d9 3742{
cab3bee2 3743 TCGv_i32 t0 = tcg_const_i32(TO(ctx->opcode));
db9a231d
AJ
3744 /* Update the nip since this might generate a trap exception */
3745 gen_update_nip(ctx, ctx->nip);
e5f17ac6
BS
3746 gen_helper_td(cpu_env, cpu_gpr[rA(ctx->opcode)], cpu_gpr[rB(ctx->opcode)],
3747 t0);
cab3bee2 3748 tcg_temp_free_i32(t0);
d9bce9d9
JM
3749}
3750
3751/* tdi */
99e300ef 3752static void gen_tdi(DisasContext *ctx)
d9bce9d9 3753{
cab3bee2
AJ
3754 TCGv t0 = tcg_const_tl(SIMM(ctx->opcode));
3755 TCGv_i32 t1 = tcg_const_i32(TO(ctx->opcode));
db9a231d
AJ
3756 /* Update the nip since this might generate a trap exception */
3757 gen_update_nip(ctx, ctx->nip);
e5f17ac6 3758 gen_helper_td(cpu_env, cpu_gpr[rA(ctx->opcode)], t0, t1);
cab3bee2
AJ
3759 tcg_temp_free(t0);
3760 tcg_temp_free_i32(t1);
d9bce9d9
JM
3761}
3762#endif
3763
79aceca5 3764/*** Processor control ***/
99e300ef 3765
da91a00f
RH
3766static void gen_read_xer(TCGv dst)
3767{
3768 TCGv t0 = tcg_temp_new();
3769 TCGv t1 = tcg_temp_new();
3770 TCGv t2 = tcg_temp_new();
3771 tcg_gen_mov_tl(dst, cpu_xer);
3772 tcg_gen_shli_tl(t0, cpu_so, XER_SO);
3773 tcg_gen_shli_tl(t1, cpu_ov, XER_OV);
3774 tcg_gen_shli_tl(t2, cpu_ca, XER_CA);
3775 tcg_gen_or_tl(t0, t0, t1);
3776 tcg_gen_or_tl(dst, dst, t2);
3777 tcg_gen_or_tl(dst, dst, t0);
3778 tcg_temp_free(t0);
3779 tcg_temp_free(t1);
3780 tcg_temp_free(t2);
3781}
3782
3783static void gen_write_xer(TCGv src)
3784{
3785 tcg_gen_andi_tl(cpu_xer, src,
3786 ~((1u << XER_SO) | (1u << XER_OV) | (1u << XER_CA)));
3787 tcg_gen_shri_tl(cpu_so, src, XER_SO);
3788 tcg_gen_shri_tl(cpu_ov, src, XER_OV);
3789 tcg_gen_shri_tl(cpu_ca, src, XER_CA);
3790 tcg_gen_andi_tl(cpu_so, cpu_so, 1);
3791 tcg_gen_andi_tl(cpu_ov, cpu_ov, 1);
3792 tcg_gen_andi_tl(cpu_ca, cpu_ca, 1);
3793}
3794
54623277 3795/* mcrxr */
99e300ef 3796static void gen_mcrxr(DisasContext *ctx)
79aceca5 3797{
da91a00f
RH
3798 TCGv_i32 t0 = tcg_temp_new_i32();
3799 TCGv_i32 t1 = tcg_temp_new_i32();
3800 TCGv_i32 dst = cpu_crf[crfD(ctx->opcode)];
3801
3802 tcg_gen_trunc_tl_i32(t0, cpu_so);
3803 tcg_gen_trunc_tl_i32(t1, cpu_ov);
3804 tcg_gen_trunc_tl_i32(dst, cpu_ca);
3805 tcg_gen_shri_i32(t0, t0, 2);
3806 tcg_gen_shri_i32(t1, t1, 1);
3807 tcg_gen_or_i32(dst, dst, t0);
3808 tcg_gen_or_i32(dst, dst, t1);
3809 tcg_temp_free_i32(t0);
3810 tcg_temp_free_i32(t1);
3811
3812 tcg_gen_movi_tl(cpu_so, 0);
3813 tcg_gen_movi_tl(cpu_ov, 0);
3814 tcg_gen_movi_tl(cpu_ca, 0);
79aceca5
FB
3815}
3816
0cfe11ea 3817/* mfcr mfocrf */
99e300ef 3818static void gen_mfcr(DisasContext *ctx)
79aceca5 3819{
76a66253 3820 uint32_t crm, crn;
3b46e624 3821
76a66253
JM
3822 if (likely(ctx->opcode & 0x00100000)) {
3823 crm = CRM(ctx->opcode);
8dd640e4 3824 if (likely(crm && ((crm & (crm - 1)) == 0))) {
0cfe11ea 3825 crn = ctz32 (crm);
e1571908 3826 tcg_gen_extu_i32_tl(cpu_gpr[rD(ctx->opcode)], cpu_crf[7 - crn]);
0497d2f4
AJ
3827 tcg_gen_shli_tl(cpu_gpr[rD(ctx->opcode)],
3828 cpu_gpr[rD(ctx->opcode)], crn * 4);
76a66253 3829 }
d9bce9d9 3830 } else {
651721b2
AJ
3831 TCGv_i32 t0 = tcg_temp_new_i32();
3832 tcg_gen_mov_i32(t0, cpu_crf[0]);
3833 tcg_gen_shli_i32(t0, t0, 4);
3834 tcg_gen_or_i32(t0, t0, cpu_crf[1]);
3835 tcg_gen_shli_i32(t0, t0, 4);
3836 tcg_gen_or_i32(t0, t0, cpu_crf[2]);
3837 tcg_gen_shli_i32(t0, t0, 4);
3838 tcg_gen_or_i32(t0, t0, cpu_crf[3]);
3839 tcg_gen_shli_i32(t0, t0, 4);
3840 tcg_gen_or_i32(t0, t0, cpu_crf[4]);
3841 tcg_gen_shli_i32(t0, t0, 4);
3842 tcg_gen_or_i32(t0, t0, cpu_crf[5]);
3843 tcg_gen_shli_i32(t0, t0, 4);
3844 tcg_gen_or_i32(t0, t0, cpu_crf[6]);
3845 tcg_gen_shli_i32(t0, t0, 4);
3846 tcg_gen_or_i32(t0, t0, cpu_crf[7]);
3847 tcg_gen_extu_i32_tl(cpu_gpr[rD(ctx->opcode)], t0);
3848 tcg_temp_free_i32(t0);
d9bce9d9 3849 }
79aceca5
FB
3850}
3851
3852/* mfmsr */
99e300ef 3853static void gen_mfmsr(DisasContext *ctx)
79aceca5 3854{
9a64fbe4 3855#if defined(CONFIG_USER_ONLY)
e06fcd75 3856 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
9a64fbe4 3857#else
76db3ba4 3858 if (unlikely(!ctx->mem_idx)) {
e06fcd75 3859 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
9fddaa0c 3860 return;
9a64fbe4 3861 }
6527f6ea 3862 tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_msr);
9a64fbe4 3863#endif
79aceca5
FB
3864}
3865
7b13448f 3866static void spr_noaccess(void *opaque, int gprn, int sprn)
3fc6c082 3867{
7b13448f 3868#if 0
3fc6c082
FB
3869 sprn = ((sprn >> 5) & 0x1F) | ((sprn & 0x1F) << 5);
3870 printf("ERROR: try to access SPR %d !\n", sprn);
7b13448f 3871#endif
3fc6c082
FB
3872}
3873#define SPR_NOACCESS (&spr_noaccess)
3fc6c082 3874
79aceca5 3875/* mfspr */
636aa200 3876static inline void gen_op_mfspr(DisasContext *ctx)
79aceca5 3877{
45d827d2 3878 void (*read_cb)(void *opaque, int gprn, int sprn);
79aceca5
FB
3879 uint32_t sprn = SPR(ctx->opcode);
3880
3fc6c082 3881#if !defined(CONFIG_USER_ONLY)
76db3ba4 3882 if (ctx->mem_idx == 2)
be147d08 3883 read_cb = ctx->spr_cb[sprn].hea_read;
76db3ba4 3884 else if (ctx->mem_idx)
3fc6c082
FB
3885 read_cb = ctx->spr_cb[sprn].oea_read;
3886 else
9a64fbe4 3887#endif
3fc6c082 3888 read_cb = ctx->spr_cb[sprn].uea_read;
76a66253
JM
3889 if (likely(read_cb != NULL)) {
3890 if (likely(read_cb != SPR_NOACCESS)) {
45d827d2 3891 (*read_cb)(ctx, rD(ctx->opcode), sprn);
3fc6c082
FB
3892 } else {
3893 /* Privilege exception */
9fceefa7
JM
3894 /* This is a hack to avoid warnings when running Linux:
3895 * this OS breaks the PowerPC virtualisation model,
3896 * allowing userland application to read the PVR
3897 */
3898 if (sprn != SPR_PVR) {
93fcfe39 3899 qemu_log("Trying to read privileged spr %d %03x at "
90e189ec
BS
3900 TARGET_FMT_lx "\n", sprn, sprn, ctx->nip);
3901 printf("Trying to read privileged spr %d %03x at "
3902 TARGET_FMT_lx "\n", sprn, sprn, ctx->nip);
f24e5695 3903 }
e06fcd75 3904 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
79aceca5 3905 }
3fc6c082
FB
3906 } else {
3907 /* Not defined */
93fcfe39 3908 qemu_log("Trying to read invalid spr %d %03x at "
90e189ec
BS
3909 TARGET_FMT_lx "\n", sprn, sprn, ctx->nip);
3910 printf("Trying to read invalid spr %d %03x at " TARGET_FMT_lx "\n",
077fc206 3911 sprn, sprn, ctx->nip);
e06fcd75 3912 gen_inval_exception(ctx, POWERPC_EXCP_INVAL_SPR);
79aceca5 3913 }
79aceca5
FB
3914}
3915
99e300ef 3916static void gen_mfspr(DisasContext *ctx)
79aceca5 3917{
3fc6c082 3918 gen_op_mfspr(ctx);
76a66253 3919}
3fc6c082
FB
3920
3921/* mftb */
99e300ef 3922static void gen_mftb(DisasContext *ctx)
3fc6c082
FB
3923{
3924 gen_op_mfspr(ctx);
79aceca5
FB
3925}
3926
0cfe11ea 3927/* mtcrf mtocrf*/
99e300ef 3928static void gen_mtcrf(DisasContext *ctx)
79aceca5 3929{
76a66253 3930 uint32_t crm, crn;
3b46e624 3931
76a66253 3932 crm = CRM(ctx->opcode);
8dd640e4 3933 if (likely((ctx->opcode & 0x00100000))) {
3934 if (crm && ((crm & (crm - 1)) == 0)) {
3935 TCGv_i32 temp = tcg_temp_new_i32();
0cfe11ea 3936 crn = ctz32 (crm);
8dd640e4 3937 tcg_gen_trunc_tl_i32(temp, cpu_gpr[rS(ctx->opcode)]);
0cfe11ea
AJ
3938 tcg_gen_shri_i32(temp, temp, crn * 4);
3939 tcg_gen_andi_i32(cpu_crf[7 - crn], temp, 0xf);
8dd640e4 3940 tcg_temp_free_i32(temp);
3941 }
76a66253 3942 } else {
651721b2
AJ
3943 TCGv_i32 temp = tcg_temp_new_i32();
3944 tcg_gen_trunc_tl_i32(temp, cpu_gpr[rS(ctx->opcode)]);
3945 for (crn = 0 ; crn < 8 ; crn++) {
3946 if (crm & (1 << crn)) {
3947 tcg_gen_shri_i32(cpu_crf[7 - crn], temp, crn * 4);
3948 tcg_gen_andi_i32(cpu_crf[7 - crn], cpu_crf[7 - crn], 0xf);
3949 }
3950 }
a7812ae4 3951 tcg_temp_free_i32(temp);
76a66253 3952 }
79aceca5
FB
3953}
3954
3955/* mtmsr */
426613db 3956#if defined(TARGET_PPC64)
99e300ef 3957static void gen_mtmsrd(DisasContext *ctx)
426613db
JM
3958{
3959#if defined(CONFIG_USER_ONLY)
e06fcd75 3960 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
426613db 3961#else
76db3ba4 3962 if (unlikely(!ctx->mem_idx)) {
e06fcd75 3963 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
426613db
JM
3964 return;
3965 }
be147d08
JM
3966 if (ctx->opcode & 0x00010000) {
3967 /* Special form that does not need any synchronisation */
6527f6ea
AJ
3968 TCGv t0 = tcg_temp_new();
3969 tcg_gen_andi_tl(t0, cpu_gpr[rS(ctx->opcode)], (1 << MSR_RI) | (1 << MSR_EE));
3970 tcg_gen_andi_tl(cpu_msr, cpu_msr, ~((1 << MSR_RI) | (1 << MSR_EE)));
3971 tcg_gen_or_tl(cpu_msr, cpu_msr, t0);
3972 tcg_temp_free(t0);
be147d08 3973 } else {
056b05f8
JM
3974 /* XXX: we need to update nip before the store
3975 * if we enter power saving mode, we will exit the loop
3976 * directly from ppc_store_msr
3977 */
be147d08 3978 gen_update_nip(ctx, ctx->nip);
e5f17ac6 3979 gen_helper_store_msr(cpu_env, cpu_gpr[rS(ctx->opcode)]);
be147d08
JM
3980 /* Must stop the translation as machine state (may have) changed */
3981 /* Note that mtmsr is not always defined as context-synchronizing */
e06fcd75 3982 gen_stop_exception(ctx);
be147d08 3983 }
426613db
JM
3984#endif
3985}
3986#endif
3987
99e300ef 3988static void gen_mtmsr(DisasContext *ctx)
79aceca5 3989{
9a64fbe4 3990#if defined(CONFIG_USER_ONLY)
e06fcd75 3991 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
9a64fbe4 3992#else
76db3ba4 3993 if (unlikely(!ctx->mem_idx)) {
e06fcd75 3994 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
9fddaa0c 3995 return;
9a64fbe4 3996 }
be147d08
JM
3997 if (ctx->opcode & 0x00010000) {
3998 /* Special form that does not need any synchronisation */
6527f6ea
AJ
3999 TCGv t0 = tcg_temp_new();
4000 tcg_gen_andi_tl(t0, cpu_gpr[rS(ctx->opcode)], (1 << MSR_RI) | (1 << MSR_EE));
4001 tcg_gen_andi_tl(cpu_msr, cpu_msr, ~((1 << MSR_RI) | (1 << MSR_EE)));
4002 tcg_gen_or_tl(cpu_msr, cpu_msr, t0);
4003 tcg_temp_free(t0);
be147d08 4004 } else {
8018dc63
AG
4005 TCGv msr = tcg_temp_new();
4006
056b05f8
JM
4007 /* XXX: we need to update nip before the store
4008 * if we enter power saving mode, we will exit the loop
4009 * directly from ppc_store_msr
4010 */
be147d08 4011 gen_update_nip(ctx, ctx->nip);
d9bce9d9 4012#if defined(TARGET_PPC64)
8018dc63
AG
4013 tcg_gen_deposit_tl(msr, cpu_msr, cpu_gpr[rS(ctx->opcode)], 0, 32);
4014#else
4015 tcg_gen_mov_tl(msr, cpu_gpr[rS(ctx->opcode)]);
d9bce9d9 4016#endif
e5f17ac6 4017 gen_helper_store_msr(cpu_env, msr);
be147d08 4018 /* Must stop the translation as machine state (may have) changed */
6527f6ea 4019 /* Note that mtmsr is not always defined as context-synchronizing */
e06fcd75 4020 gen_stop_exception(ctx);
be147d08 4021 }
9a64fbe4 4022#endif
79aceca5
FB
4023}
4024
4025/* mtspr */
99e300ef 4026static void gen_mtspr(DisasContext *ctx)
79aceca5 4027{
45d827d2 4028 void (*write_cb)(void *opaque, int sprn, int gprn);
79aceca5
FB
4029 uint32_t sprn = SPR(ctx->opcode);
4030
3fc6c082 4031#if !defined(CONFIG_USER_ONLY)
76db3ba4 4032 if (ctx->mem_idx == 2)
be147d08 4033 write_cb = ctx->spr_cb[sprn].hea_write;
76db3ba4 4034 else if (ctx->mem_idx)
3fc6c082
FB
4035 write_cb = ctx->spr_cb[sprn].oea_write;
4036 else
9a64fbe4 4037#endif
3fc6c082 4038 write_cb = ctx->spr_cb[sprn].uea_write;
76a66253
JM
4039 if (likely(write_cb != NULL)) {
4040 if (likely(write_cb != SPR_NOACCESS)) {
45d827d2 4041 (*write_cb)(ctx, sprn, rS(ctx->opcode));
3fc6c082
FB
4042 } else {
4043 /* Privilege exception */
93fcfe39 4044 qemu_log("Trying to write privileged spr %d %03x at "
90e189ec
BS
4045 TARGET_FMT_lx "\n", sprn, sprn, ctx->nip);
4046 printf("Trying to write privileged spr %d %03x at " TARGET_FMT_lx
4047 "\n", sprn, sprn, ctx->nip);
e06fcd75 4048 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
76a66253 4049 }
3fc6c082
FB
4050 } else {
4051 /* Not defined */
93fcfe39 4052 qemu_log("Trying to write invalid spr %d %03x at "
90e189ec
BS
4053 TARGET_FMT_lx "\n", sprn, sprn, ctx->nip);
4054 printf("Trying to write invalid spr %d %03x at " TARGET_FMT_lx "\n",
077fc206 4055 sprn, sprn, ctx->nip);
e06fcd75 4056 gen_inval_exception(ctx, POWERPC_EXCP_INVAL_SPR);
79aceca5 4057 }
79aceca5
FB
4058}
4059
4060/*** Cache management ***/
99e300ef 4061
54623277 4062/* dcbf */
99e300ef 4063static void gen_dcbf(DisasContext *ctx)
79aceca5 4064{
dac454af 4065 /* XXX: specification says this is treated as a load by the MMU */
76db3ba4
AJ
4066 TCGv t0;
4067 gen_set_access_type(ctx, ACCESS_CACHE);
4068 t0 = tcg_temp_new();
4069 gen_addr_reg_index(ctx, t0);
4070 gen_qemu_ld8u(ctx, t0, t0);
fea0c503 4071 tcg_temp_free(t0);
79aceca5
FB
4072}
4073
4074/* dcbi (Supervisor only) */
99e300ef 4075static void gen_dcbi(DisasContext *ctx)
79aceca5 4076{
a541f297 4077#if defined(CONFIG_USER_ONLY)
e06fcd75 4078 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
a541f297 4079#else
b61f2753 4080 TCGv EA, val;
76db3ba4 4081 if (unlikely(!ctx->mem_idx)) {
e06fcd75 4082 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
9fddaa0c 4083 return;
9a64fbe4 4084 }
a7812ae4 4085 EA = tcg_temp_new();
76db3ba4
AJ
4086 gen_set_access_type(ctx, ACCESS_CACHE);
4087 gen_addr_reg_index(ctx, EA);
a7812ae4 4088 val = tcg_temp_new();
76a66253 4089 /* XXX: specification says this should be treated as a store by the MMU */
76db3ba4
AJ
4090 gen_qemu_ld8u(ctx, val, EA);
4091 gen_qemu_st8(ctx, val, EA);
b61f2753
AJ
4092 tcg_temp_free(val);
4093 tcg_temp_free(EA);
a541f297 4094#endif
79aceca5
FB
4095}
4096
4097/* dcdst */
99e300ef 4098static void gen_dcbst(DisasContext *ctx)
79aceca5 4099{
76a66253 4100 /* XXX: specification say this is treated as a load by the MMU */
76db3ba4
AJ
4101 TCGv t0;
4102 gen_set_access_type(ctx, ACCESS_CACHE);
4103 t0 = tcg_temp_new();
4104 gen_addr_reg_index(ctx, t0);
4105 gen_qemu_ld8u(ctx, t0, t0);
fea0c503 4106 tcg_temp_free(t0);
79aceca5
FB
4107}
4108
4109/* dcbt */
99e300ef 4110static void gen_dcbt(DisasContext *ctx)
79aceca5 4111{
0db1b20e 4112 /* interpreted as no-op */
76a66253
JM
4113 /* XXX: specification say this is treated as a load by the MMU
4114 * but does not generate any exception
4115 */
79aceca5
FB
4116}
4117
4118/* dcbtst */
99e300ef 4119static void gen_dcbtst(DisasContext *ctx)
79aceca5 4120{
0db1b20e 4121 /* interpreted as no-op */
76a66253
JM
4122 /* XXX: specification say this is treated as a load by the MMU
4123 * but does not generate any exception
4124 */
79aceca5
FB
4125}
4126
4127/* dcbz */
99e300ef 4128static void gen_dcbz(DisasContext *ctx)
79aceca5 4129{
8e33944f
AG
4130 TCGv tcgv_addr;
4131 TCGv_i32 tcgv_is_dcbzl;
4132 int is_dcbzl = ctx->opcode & 0x00200000 ? 1 : 0;
d63001d1 4133
76db3ba4 4134 gen_set_access_type(ctx, ACCESS_CACHE);
799a8c8d
AJ
4135 /* NIP cannot be restored if the memory exception comes from an helper */
4136 gen_update_nip(ctx, ctx->nip - 4);
8e33944f
AG
4137 tcgv_addr = tcg_temp_new();
4138 tcgv_is_dcbzl = tcg_const_i32(is_dcbzl);
4139
4140 gen_addr_reg_index(ctx, tcgv_addr);
4141 gen_helper_dcbz(cpu_env, tcgv_addr, tcgv_is_dcbzl);
4142
4143 tcg_temp_free(tcgv_addr);
4144 tcg_temp_free_i32(tcgv_is_dcbzl);
79aceca5
FB
4145}
4146
ae1c1a3d 4147/* dst / dstt */
99e300ef 4148static void gen_dst(DisasContext *ctx)
ae1c1a3d
AJ
4149{
4150 if (rA(ctx->opcode) == 0) {
4151 gen_inval_exception(ctx, POWERPC_EXCP_INVAL_LSWX);
4152 } else {
4153 /* interpreted as no-op */
4154 }
4155}
4156
4157/* dstst /dststt */
99e300ef 4158static void gen_dstst(DisasContext *ctx)
ae1c1a3d
AJ
4159{
4160 if (rA(ctx->opcode) == 0) {
4161 gen_inval_exception(ctx, POWERPC_EXCP_INVAL_LSWX);
4162 } else {
4163 /* interpreted as no-op */
4164 }
4165
4166}
4167
4168/* dss / dssall */
99e300ef 4169static void gen_dss(DisasContext *ctx)
ae1c1a3d
AJ
4170{
4171 /* interpreted as no-op */
4172}
4173
79aceca5 4174/* icbi */
99e300ef 4175static void gen_icbi(DisasContext *ctx)
79aceca5 4176{
76db3ba4
AJ
4177 TCGv t0;
4178 gen_set_access_type(ctx, ACCESS_CACHE);
30032c94
JM
4179 /* NIP cannot be restored if the memory exception comes from an helper */
4180 gen_update_nip(ctx, ctx->nip - 4);
76db3ba4
AJ
4181 t0 = tcg_temp_new();
4182 gen_addr_reg_index(ctx, t0);
2f5a189c 4183 gen_helper_icbi(cpu_env, t0);
37d269df 4184 tcg_temp_free(t0);
79aceca5
FB
4185}
4186
4187/* Optional: */
4188/* dcba */
99e300ef 4189static void gen_dcba(DisasContext *ctx)
79aceca5 4190{
0db1b20e
JM
4191 /* interpreted as no-op */
4192 /* XXX: specification say this is treated as a store by the MMU
4193 * but does not generate any exception
4194 */
79aceca5
FB
4195}
4196
4197/*** Segment register manipulation ***/
4198/* Supervisor only: */
99e300ef 4199
54623277 4200/* mfsr */
99e300ef 4201static void gen_mfsr(DisasContext *ctx)
79aceca5 4202{
9a64fbe4 4203#if defined(CONFIG_USER_ONLY)
e06fcd75 4204 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
9a64fbe4 4205#else
74d37793 4206 TCGv t0;
76db3ba4 4207 if (unlikely(!ctx->mem_idx)) {
e06fcd75 4208 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
9fddaa0c 4209 return;
9a64fbe4 4210 }
74d37793 4211 t0 = tcg_const_tl(SR(ctx->opcode));
c6c7cf05 4212 gen_helper_load_sr(cpu_gpr[rD(ctx->opcode)], cpu_env, t0);
74d37793 4213 tcg_temp_free(t0);
9a64fbe4 4214#endif
79aceca5
FB
4215}
4216
4217/* mfsrin */
99e300ef 4218static void gen_mfsrin(DisasContext *ctx)
79aceca5 4219{
9a64fbe4 4220#if defined(CONFIG_USER_ONLY)
e06fcd75 4221 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
9a64fbe4 4222#else
74d37793 4223 TCGv t0;
76db3ba4 4224 if (unlikely(!ctx->mem_idx)) {
e06fcd75 4225 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
9fddaa0c 4226 return;
9a64fbe4 4227 }
74d37793
AJ
4228 t0 = tcg_temp_new();
4229 tcg_gen_shri_tl(t0, cpu_gpr[rB(ctx->opcode)], 28);
4230 tcg_gen_andi_tl(t0, t0, 0xF);
c6c7cf05 4231 gen_helper_load_sr(cpu_gpr[rD(ctx->opcode)], cpu_env, t0);
74d37793 4232 tcg_temp_free(t0);
9a64fbe4 4233#endif
79aceca5
FB
4234}
4235
4236/* mtsr */
99e300ef 4237static void gen_mtsr(DisasContext *ctx)
79aceca5 4238{
9a64fbe4 4239#if defined(CONFIG_USER_ONLY)
e06fcd75 4240 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
9a64fbe4 4241#else
74d37793 4242 TCGv t0;
76db3ba4 4243 if (unlikely(!ctx->mem_idx)) {
e06fcd75 4244 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
9fddaa0c 4245 return;
9a64fbe4 4246 }
74d37793 4247 t0 = tcg_const_tl(SR(ctx->opcode));
c6c7cf05 4248 gen_helper_store_sr(cpu_env, t0, cpu_gpr[rS(ctx->opcode)]);
74d37793 4249 tcg_temp_free(t0);
9a64fbe4 4250#endif
79aceca5
FB
4251}
4252
4253/* mtsrin */
99e300ef 4254static void gen_mtsrin(DisasContext *ctx)
79aceca5 4255{
9a64fbe4 4256#if defined(CONFIG_USER_ONLY)
e06fcd75 4257 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
9a64fbe4 4258#else
74d37793 4259 TCGv t0;
76db3ba4 4260 if (unlikely(!ctx->mem_idx)) {
e06fcd75 4261 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
9fddaa0c 4262 return;
9a64fbe4 4263 }
74d37793
AJ
4264 t0 = tcg_temp_new();
4265 tcg_gen_shri_tl(t0, cpu_gpr[rB(ctx->opcode)], 28);
4266 tcg_gen_andi_tl(t0, t0, 0xF);
c6c7cf05 4267 gen_helper_store_sr(cpu_env, t0, cpu_gpr[rD(ctx->opcode)]);
74d37793 4268 tcg_temp_free(t0);
9a64fbe4 4269#endif
79aceca5
FB
4270}
4271
12de9a39
JM
4272#if defined(TARGET_PPC64)
4273/* Specific implementation for PowerPC 64 "bridge" emulation using SLB */
e8eaa2c0 4274
54623277 4275/* mfsr */
e8eaa2c0 4276static void gen_mfsr_64b(DisasContext *ctx)
12de9a39
JM
4277{
4278#if defined(CONFIG_USER_ONLY)
e06fcd75 4279 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
12de9a39 4280#else
74d37793 4281 TCGv t0;
76db3ba4 4282 if (unlikely(!ctx->mem_idx)) {
e06fcd75 4283 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
12de9a39
JM
4284 return;
4285 }
74d37793 4286 t0 = tcg_const_tl(SR(ctx->opcode));
c6c7cf05 4287 gen_helper_load_sr(cpu_gpr[rD(ctx->opcode)], cpu_env, t0);
74d37793 4288 tcg_temp_free(t0);
12de9a39
JM
4289#endif
4290}
4291
4292/* mfsrin */
e8eaa2c0 4293static void gen_mfsrin_64b(DisasContext *ctx)
12de9a39
JM
4294{
4295#if defined(CONFIG_USER_ONLY)
e06fcd75 4296 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
12de9a39 4297#else
74d37793 4298 TCGv t0;
76db3ba4 4299 if (unlikely(!ctx->mem_idx)) {
e06fcd75 4300 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
12de9a39
JM
4301 return;
4302 }
74d37793
AJ
4303 t0 = tcg_temp_new();
4304 tcg_gen_shri_tl(t0, cpu_gpr[rB(ctx->opcode)], 28);
4305 tcg_gen_andi_tl(t0, t0, 0xF);
c6c7cf05 4306 gen_helper_load_sr(cpu_gpr[rD(ctx->opcode)], cpu_env, t0);
74d37793 4307 tcg_temp_free(t0);
12de9a39
JM
4308#endif
4309}
4310
4311/* mtsr */
e8eaa2c0 4312static void gen_mtsr_64b(DisasContext *ctx)
12de9a39
JM
4313{
4314#if defined(CONFIG_USER_ONLY)
e06fcd75 4315 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
12de9a39 4316#else
74d37793 4317 TCGv t0;
76db3ba4 4318 if (unlikely(!ctx->mem_idx)) {
e06fcd75 4319 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
12de9a39
JM
4320 return;
4321 }
74d37793 4322 t0 = tcg_const_tl(SR(ctx->opcode));
c6c7cf05 4323 gen_helper_store_sr(cpu_env, t0, cpu_gpr[rS(ctx->opcode)]);
74d37793 4324 tcg_temp_free(t0);
12de9a39
JM
4325#endif
4326}
4327
4328/* mtsrin */
e8eaa2c0 4329static void gen_mtsrin_64b(DisasContext *ctx)
12de9a39
JM
4330{
4331#if defined(CONFIG_USER_ONLY)
e06fcd75 4332 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
12de9a39 4333#else
74d37793 4334 TCGv t0;
76db3ba4 4335 if (unlikely(!ctx->mem_idx)) {
e06fcd75 4336 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
12de9a39
JM
4337 return;
4338 }
74d37793
AJ
4339 t0 = tcg_temp_new();
4340 tcg_gen_shri_tl(t0, cpu_gpr[rB(ctx->opcode)], 28);
4341 tcg_gen_andi_tl(t0, t0, 0xF);
c6c7cf05 4342 gen_helper_store_sr(cpu_env, t0, cpu_gpr[rS(ctx->opcode)]);
74d37793 4343 tcg_temp_free(t0);
12de9a39
JM
4344#endif
4345}
f6b868fc
BS
4346
4347/* slbmte */
e8eaa2c0 4348static void gen_slbmte(DisasContext *ctx)
f6b868fc
BS
4349{
4350#if defined(CONFIG_USER_ONLY)
4351 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
4352#else
4353 if (unlikely(!ctx->mem_idx)) {
4354 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
4355 return;
4356 }
c6c7cf05
BS
4357 gen_helper_store_slb(cpu_env, cpu_gpr[rB(ctx->opcode)],
4358 cpu_gpr[rS(ctx->opcode)]);
f6b868fc
BS
4359#endif
4360}
4361
efdef95f
DG
4362static void gen_slbmfee(DisasContext *ctx)
4363{
4364#if defined(CONFIG_USER_ONLY)
4365 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
4366#else
4367 if (unlikely(!ctx->mem_idx)) {
4368 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
4369 return;
4370 }
c6c7cf05 4371 gen_helper_load_slb_esid(cpu_gpr[rS(ctx->opcode)], cpu_env,
efdef95f
DG
4372 cpu_gpr[rB(ctx->opcode)]);
4373#endif
4374}
4375
4376static void gen_slbmfev(DisasContext *ctx)
4377{
4378#if defined(CONFIG_USER_ONLY)
4379 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
4380#else
4381 if (unlikely(!ctx->mem_idx)) {
4382 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
4383 return;
4384 }
c6c7cf05 4385 gen_helper_load_slb_vsid(cpu_gpr[rS(ctx->opcode)], cpu_env,
efdef95f
DG
4386 cpu_gpr[rB(ctx->opcode)]);
4387#endif
4388}
12de9a39
JM
4389#endif /* defined(TARGET_PPC64) */
4390
79aceca5 4391/*** Lookaside buffer management ***/
76db3ba4 4392/* Optional & mem_idx only: */
99e300ef 4393
54623277 4394/* tlbia */
99e300ef 4395static void gen_tlbia(DisasContext *ctx)
79aceca5 4396{
9a64fbe4 4397#if defined(CONFIG_USER_ONLY)
e06fcd75 4398 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
9a64fbe4 4399#else
76db3ba4 4400 if (unlikely(!ctx->mem_idx)) {
e06fcd75 4401 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
9fddaa0c 4402 return;
9a64fbe4 4403 }
c6c7cf05 4404 gen_helper_tlbia(cpu_env);
9a64fbe4 4405#endif
79aceca5
FB
4406}
4407
bf14b1ce 4408/* tlbiel */
99e300ef 4409static void gen_tlbiel(DisasContext *ctx)
bf14b1ce
BS
4410{
4411#if defined(CONFIG_USER_ONLY)
4412 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
4413#else
4414 if (unlikely(!ctx->mem_idx)) {
4415 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
4416 return;
4417 }
c6c7cf05 4418 gen_helper_tlbie(cpu_env, cpu_gpr[rB(ctx->opcode)]);
bf14b1ce
BS
4419#endif
4420}
4421
79aceca5 4422/* tlbie */
99e300ef 4423static void gen_tlbie(DisasContext *ctx)
79aceca5 4424{
9a64fbe4 4425#if defined(CONFIG_USER_ONLY)
e06fcd75 4426 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
9a64fbe4 4427#else
76db3ba4 4428 if (unlikely(!ctx->mem_idx)) {
e06fcd75 4429 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
9fddaa0c 4430 return;
9a64fbe4 4431 }
9ca3f7f3 4432 if (NARROW_MODE(ctx)) {
74d37793
AJ
4433 TCGv t0 = tcg_temp_new();
4434 tcg_gen_ext32u_tl(t0, cpu_gpr[rB(ctx->opcode)]);
c6c7cf05 4435 gen_helper_tlbie(cpu_env, t0);
74d37793 4436 tcg_temp_free(t0);
9ca3f7f3 4437 } else {
c6c7cf05 4438 gen_helper_tlbie(cpu_env, cpu_gpr[rB(ctx->opcode)]);
9ca3f7f3 4439 }
9a64fbe4 4440#endif
79aceca5
FB
4441}
4442
4443/* tlbsync */
99e300ef 4444static void gen_tlbsync(DisasContext *ctx)
79aceca5 4445{
9a64fbe4 4446#if defined(CONFIG_USER_ONLY)
e06fcd75 4447 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
9a64fbe4 4448#else
76db3ba4 4449 if (unlikely(!ctx->mem_idx)) {
e06fcd75 4450 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
9fddaa0c 4451 return;
9a64fbe4
FB
4452 }
4453 /* This has no effect: it should ensure that all previous
4454 * tlbie have completed
4455 */
e06fcd75 4456 gen_stop_exception(ctx);
9a64fbe4 4457#endif
79aceca5
FB
4458}
4459
426613db
JM
4460#if defined(TARGET_PPC64)
4461/* slbia */
99e300ef 4462static void gen_slbia(DisasContext *ctx)
426613db
JM
4463{
4464#if defined(CONFIG_USER_ONLY)
e06fcd75 4465 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
426613db 4466#else
76db3ba4 4467 if (unlikely(!ctx->mem_idx)) {
e06fcd75 4468 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
426613db
JM
4469 return;
4470 }
c6c7cf05 4471 gen_helper_slbia(cpu_env);
426613db
JM
4472#endif
4473}
4474
4475/* slbie */
99e300ef 4476static void gen_slbie(DisasContext *ctx)
426613db
JM
4477{
4478#if defined(CONFIG_USER_ONLY)
e06fcd75 4479 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
426613db 4480#else
76db3ba4 4481 if (unlikely(!ctx->mem_idx)) {
e06fcd75 4482 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
426613db
JM
4483 return;
4484 }
c6c7cf05 4485 gen_helper_slbie(cpu_env, cpu_gpr[rB(ctx->opcode)]);
426613db
JM
4486#endif
4487}
4488#endif
4489
79aceca5
FB
4490/*** External control ***/
4491/* Optional: */
99e300ef 4492
54623277 4493/* eciwx */
99e300ef 4494static void gen_eciwx(DisasContext *ctx)
79aceca5 4495{
76db3ba4 4496 TCGv t0;
fa407c03 4497 /* Should check EAR[E] ! */
76db3ba4
AJ
4498 gen_set_access_type(ctx, ACCESS_EXT);
4499 t0 = tcg_temp_new();
4500 gen_addr_reg_index(ctx, t0);
fa407c03 4501 gen_check_align(ctx, t0, 0x03);
76db3ba4 4502 gen_qemu_ld32u(ctx, cpu_gpr[rD(ctx->opcode)], t0);
fa407c03 4503 tcg_temp_free(t0);
76a66253
JM
4504}
4505
4506/* ecowx */
99e300ef 4507static void gen_ecowx(DisasContext *ctx)
76a66253 4508{
76db3ba4 4509 TCGv t0;
fa407c03 4510 /* Should check EAR[E] ! */
76db3ba4
AJ
4511 gen_set_access_type(ctx, ACCESS_EXT);
4512 t0 = tcg_temp_new();
4513 gen_addr_reg_index(ctx, t0);
fa407c03 4514 gen_check_align(ctx, t0, 0x03);
76db3ba4 4515 gen_qemu_st32(ctx, cpu_gpr[rD(ctx->opcode)], t0);
fa407c03 4516 tcg_temp_free(t0);
76a66253
JM
4517}
4518
4519/* PowerPC 601 specific instructions */
99e300ef 4520
54623277 4521/* abs - abs. */
99e300ef 4522static void gen_abs(DisasContext *ctx)
76a66253 4523{
22e0e173
AJ
4524 int l1 = gen_new_label();
4525 int l2 = gen_new_label();
4526 tcg_gen_brcondi_tl(TCG_COND_GE, cpu_gpr[rA(ctx->opcode)], 0, l1);
4527 tcg_gen_neg_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)]);
4528 tcg_gen_br(l2);
4529 gen_set_label(l1);
4530 tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)]);
4531 gen_set_label(l2);
76a66253 4532 if (unlikely(Rc(ctx->opcode) != 0))
22e0e173 4533 gen_set_Rc0(ctx, cpu_gpr[rD(ctx->opcode)]);
76a66253
JM
4534}
4535
4536/* abso - abso. */
99e300ef 4537static void gen_abso(DisasContext *ctx)
76a66253 4538{
22e0e173
AJ
4539 int l1 = gen_new_label();
4540 int l2 = gen_new_label();
4541 int l3 = gen_new_label();
4542 /* Start with XER OV disabled, the most likely case */
da91a00f 4543 tcg_gen_movi_tl(cpu_ov, 0);
22e0e173
AJ
4544 tcg_gen_brcondi_tl(TCG_COND_GE, cpu_gpr[rA(ctx->opcode)], 0, l2);
4545 tcg_gen_brcondi_tl(TCG_COND_NE, cpu_gpr[rA(ctx->opcode)], 0x80000000, l1);
da91a00f
RH
4546 tcg_gen_movi_tl(cpu_ov, 1);
4547 tcg_gen_movi_tl(cpu_so, 1);
22e0e173
AJ
4548 tcg_gen_br(l2);
4549 gen_set_label(l1);
4550 tcg_gen_neg_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)]);
4551 tcg_gen_br(l3);
4552 gen_set_label(l2);
4553 tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)]);
4554 gen_set_label(l3);
76a66253 4555 if (unlikely(Rc(ctx->opcode) != 0))
22e0e173 4556 gen_set_Rc0(ctx, cpu_gpr[rD(ctx->opcode)]);
76a66253
JM
4557}
4558
4559/* clcs */
99e300ef 4560static void gen_clcs(DisasContext *ctx)
76a66253 4561{
22e0e173 4562 TCGv_i32 t0 = tcg_const_i32(rA(ctx->opcode));
d523dd00 4563 gen_helper_clcs(cpu_gpr[rD(ctx->opcode)], cpu_env, t0);
22e0e173 4564 tcg_temp_free_i32(t0);
c7697e1f 4565 /* Rc=1 sets CR0 to an undefined state */
76a66253
JM
4566}
4567
4568/* div - div. */
99e300ef 4569static void gen_div(DisasContext *ctx)
76a66253 4570{
d15f74fb
BS
4571 gen_helper_div(cpu_gpr[rD(ctx->opcode)], cpu_env, cpu_gpr[rA(ctx->opcode)],
4572 cpu_gpr[rB(ctx->opcode)]);
76a66253 4573 if (unlikely(Rc(ctx->opcode) != 0))
22e0e173 4574 gen_set_Rc0(ctx, cpu_gpr[rD(ctx->opcode)]);
76a66253
JM
4575}
4576
4577/* divo - divo. */
99e300ef 4578static void gen_divo(DisasContext *ctx)
76a66253 4579{
d15f74fb
BS
4580 gen_helper_divo(cpu_gpr[rD(ctx->opcode)], cpu_env, cpu_gpr[rA(ctx->opcode)],
4581 cpu_gpr[rB(ctx->opcode)]);
76a66253 4582 if (unlikely(Rc(ctx->opcode) != 0))
22e0e173 4583 gen_set_Rc0(ctx, cpu_gpr[rD(ctx->opcode)]);
76a66253
JM
4584}
4585
4586/* divs - divs. */
99e300ef 4587static void gen_divs(DisasContext *ctx)
76a66253 4588{
d15f74fb
BS
4589 gen_helper_divs(cpu_gpr[rD(ctx->opcode)], cpu_env, cpu_gpr[rA(ctx->opcode)],
4590 cpu_gpr[rB(ctx->opcode)]);
76a66253 4591 if (unlikely(Rc(ctx->opcode) != 0))
22e0e173 4592 gen_set_Rc0(ctx, cpu_gpr[rD(ctx->opcode)]);
76a66253
JM
4593}
4594
4595/* divso - divso. */
99e300ef 4596static void gen_divso(DisasContext *ctx)
76a66253 4597{
d15f74fb
BS
4598 gen_helper_divso(cpu_gpr[rD(ctx->opcode)], cpu_env,
4599 cpu_gpr[rA(ctx->opcode)], cpu_gpr[rB(ctx->opcode)]);
76a66253 4600 if (unlikely(Rc(ctx->opcode) != 0))
22e0e173 4601 gen_set_Rc0(ctx, cpu_gpr[rD(ctx->opcode)]);
76a66253
JM
4602}
4603
4604/* doz - doz. */
99e300ef 4605static void gen_doz(DisasContext *ctx)
76a66253 4606{
22e0e173
AJ
4607 int l1 = gen_new_label();
4608 int l2 = gen_new_label();
4609 tcg_gen_brcond_tl(TCG_COND_GE, cpu_gpr[rB(ctx->opcode)], cpu_gpr[rA(ctx->opcode)], l1);
4610 tcg_gen_sub_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rB(ctx->opcode)], cpu_gpr[rA(ctx->opcode)]);
4611 tcg_gen_br(l2);
4612 gen_set_label(l1);
4613 tcg_gen_movi_tl(cpu_gpr[rD(ctx->opcode)], 0);
4614 gen_set_label(l2);
76a66253 4615 if (unlikely(Rc(ctx->opcode) != 0))
22e0e173 4616 gen_set_Rc0(ctx, cpu_gpr[rD(ctx->opcode)]);
76a66253
JM
4617}
4618
4619/* dozo - dozo. */
99e300ef 4620static void gen_dozo(DisasContext *ctx)
76a66253 4621{
22e0e173
AJ
4622 int l1 = gen_new_label();
4623 int l2 = gen_new_label();
4624 TCGv t0 = tcg_temp_new();
4625 TCGv t1 = tcg_temp_new();
4626 TCGv t2 = tcg_temp_new();
4627 /* Start with XER OV disabled, the most likely case */
da91a00f 4628 tcg_gen_movi_tl(cpu_ov, 0);
22e0e173
AJ
4629 tcg_gen_brcond_tl(TCG_COND_GE, cpu_gpr[rB(ctx->opcode)], cpu_gpr[rA(ctx->opcode)], l1);
4630 tcg_gen_sub_tl(t0, cpu_gpr[rB(ctx->opcode)], cpu_gpr[rA(ctx->opcode)]);
4631 tcg_gen_xor_tl(t1, cpu_gpr[rB(ctx->opcode)], cpu_gpr[rA(ctx->opcode)]);
4632 tcg_gen_xor_tl(t2, cpu_gpr[rA(ctx->opcode)], t0);
4633 tcg_gen_andc_tl(t1, t1, t2);
4634 tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], t0);
4635 tcg_gen_brcondi_tl(TCG_COND_GE, t1, 0, l2);
da91a00f
RH
4636 tcg_gen_movi_tl(cpu_ov, 1);
4637 tcg_gen_movi_tl(cpu_so, 1);
22e0e173
AJ
4638 tcg_gen_br(l2);
4639 gen_set_label(l1);
4640 tcg_gen_movi_tl(cpu_gpr[rD(ctx->opcode)], 0);
4641 gen_set_label(l2);
4642 tcg_temp_free(t0);
4643 tcg_temp_free(t1);
4644 tcg_temp_free(t2);
76a66253 4645 if (unlikely(Rc(ctx->opcode) != 0))
22e0e173 4646 gen_set_Rc0(ctx, cpu_gpr[rD(ctx->opcode)]);
76a66253
JM
4647}
4648
4649/* dozi */
99e300ef 4650static void gen_dozi(DisasContext *ctx)
76a66253 4651{
22e0e173
AJ
4652 target_long simm = SIMM(ctx->opcode);
4653 int l1 = gen_new_label();
4654 int l2 = gen_new_label();
4655 tcg_gen_brcondi_tl(TCG_COND_LT, cpu_gpr[rA(ctx->opcode)], simm, l1);
4656 tcg_gen_subfi_tl(cpu_gpr[rD(ctx->opcode)], simm, cpu_gpr[rA(ctx->opcode)]);
4657 tcg_gen_br(l2);
4658 gen_set_label(l1);
4659 tcg_gen_movi_tl(cpu_gpr[rD(ctx->opcode)], 0);
4660 gen_set_label(l2);
4661 if (unlikely(Rc(ctx->opcode) != 0))
4662 gen_set_Rc0(ctx, cpu_gpr[rD(ctx->opcode)]);
76a66253
JM
4663}
4664
76a66253 4665/* lscbx - lscbx. */
99e300ef 4666static void gen_lscbx(DisasContext *ctx)
76a66253 4667{
bdb4b689
AJ
4668 TCGv t0 = tcg_temp_new();
4669 TCGv_i32 t1 = tcg_const_i32(rD(ctx->opcode));
4670 TCGv_i32 t2 = tcg_const_i32(rA(ctx->opcode));
4671 TCGv_i32 t3 = tcg_const_i32(rB(ctx->opcode));
76a66253 4672
76db3ba4 4673 gen_addr_reg_index(ctx, t0);
76a66253 4674 /* NIP cannot be restored if the memory exception comes from an helper */
d9bce9d9 4675 gen_update_nip(ctx, ctx->nip - 4);
2f5a189c 4676 gen_helper_lscbx(t0, cpu_env, t0, t1, t2, t3);
bdb4b689
AJ
4677 tcg_temp_free_i32(t1);
4678 tcg_temp_free_i32(t2);
4679 tcg_temp_free_i32(t3);
3d7b417e 4680 tcg_gen_andi_tl(cpu_xer, cpu_xer, ~0x7F);
bdb4b689 4681 tcg_gen_or_tl(cpu_xer, cpu_xer, t0);
76a66253 4682 if (unlikely(Rc(ctx->opcode) != 0))
bdb4b689
AJ
4683 gen_set_Rc0(ctx, t0);
4684 tcg_temp_free(t0);
76a66253
JM
4685}
4686
4687/* maskg - maskg. */
99e300ef 4688static void gen_maskg(DisasContext *ctx)
76a66253 4689{
22e0e173
AJ
4690 int l1 = gen_new_label();
4691 TCGv t0 = tcg_temp_new();
4692 TCGv t1 = tcg_temp_new();
4693 TCGv t2 = tcg_temp_new();
4694 TCGv t3 = tcg_temp_new();
4695 tcg_gen_movi_tl(t3, 0xFFFFFFFF);
4696 tcg_gen_andi_tl(t0, cpu_gpr[rB(ctx->opcode)], 0x1F);
4697 tcg_gen_andi_tl(t1, cpu_gpr[rS(ctx->opcode)], 0x1F);
4698 tcg_gen_addi_tl(t2, t0, 1);
4699 tcg_gen_shr_tl(t2, t3, t2);
4700 tcg_gen_shr_tl(t3, t3, t1);
4701 tcg_gen_xor_tl(cpu_gpr[rA(ctx->opcode)], t2, t3);
4702 tcg_gen_brcond_tl(TCG_COND_GE, t0, t1, l1);
4703 tcg_gen_neg_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rA(ctx->opcode)]);
4704 gen_set_label(l1);
4705 tcg_temp_free(t0);
4706 tcg_temp_free(t1);
4707 tcg_temp_free(t2);
4708 tcg_temp_free(t3);
76a66253 4709 if (unlikely(Rc(ctx->opcode) != 0))
22e0e173 4710 gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
76a66253
JM
4711}
4712
4713/* maskir - maskir. */
99e300ef 4714static void gen_maskir(DisasContext *ctx)
76a66253 4715{
22e0e173
AJ
4716 TCGv t0 = tcg_temp_new();
4717 TCGv t1 = tcg_temp_new();
4718 tcg_gen_and_tl(t0, cpu_gpr[rS(ctx->opcode)], cpu_gpr[rB(ctx->opcode)]);
4719 tcg_gen_andc_tl(t1, cpu_gpr[rA(ctx->opcode)], cpu_gpr[rB(ctx->opcode)]);
4720 tcg_gen_or_tl(cpu_gpr[rA(ctx->opcode)], t0, t1);
4721 tcg_temp_free(t0);
4722 tcg_temp_free(t1);
76a66253 4723 if (unlikely(Rc(ctx->opcode) != 0))
22e0e173 4724 gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
76a66253
JM
4725}
4726
4727/* mul - mul. */
99e300ef 4728static void gen_mul(DisasContext *ctx)
76a66253 4729{
22e0e173
AJ
4730 TCGv_i64 t0 = tcg_temp_new_i64();
4731 TCGv_i64 t1 = tcg_temp_new_i64();
4732 TCGv t2 = tcg_temp_new();
4733 tcg_gen_extu_tl_i64(t0, cpu_gpr[rA(ctx->opcode)]);
4734 tcg_gen_extu_tl_i64(t1, cpu_gpr[rB(ctx->opcode)]);
4735 tcg_gen_mul_i64(t0, t0, t1);
4736 tcg_gen_trunc_i64_tl(t2, t0);
4737 gen_store_spr(SPR_MQ, t2);
4738 tcg_gen_shri_i64(t1, t0, 32);
4739 tcg_gen_trunc_i64_tl(cpu_gpr[rD(ctx->opcode)], t1);
4740 tcg_temp_free_i64(t0);
4741 tcg_temp_free_i64(t1);
4742 tcg_temp_free(t2);
76a66253 4743 if (unlikely(Rc(ctx->opcode) != 0))
22e0e173 4744 gen_set_Rc0(ctx, cpu_gpr[rD(ctx->opcode)]);
76a66253
JM
4745}
4746
4747/* mulo - mulo. */
99e300ef 4748static void gen_mulo(DisasContext *ctx)
76a66253 4749{
22e0e173
AJ
4750 int l1 = gen_new_label();
4751 TCGv_i64 t0 = tcg_temp_new_i64();
4752 TCGv_i64 t1 = tcg_temp_new_i64();
4753 TCGv t2 = tcg_temp_new();
4754 /* Start with XER OV disabled, the most likely case */
da91a00f 4755 tcg_gen_movi_tl(cpu_ov, 0);
22e0e173
AJ
4756 tcg_gen_extu_tl_i64(t0, cpu_gpr[rA(ctx->opcode)]);
4757 tcg_gen_extu_tl_i64(t1, cpu_gpr[rB(ctx->opcode)]);
4758 tcg_gen_mul_i64(t0, t0, t1);
4759 tcg_gen_trunc_i64_tl(t2, t0);
4760 gen_store_spr(SPR_MQ, t2);
4761 tcg_gen_shri_i64(t1, t0, 32);
4762 tcg_gen_trunc_i64_tl(cpu_gpr[rD(ctx->opcode)], t1);
4763 tcg_gen_ext32s_i64(t1, t0);
4764 tcg_gen_brcond_i64(TCG_COND_EQ, t0, t1, l1);
da91a00f
RH
4765 tcg_gen_movi_tl(cpu_ov, 1);
4766 tcg_gen_movi_tl(cpu_so, 1);
22e0e173
AJ
4767 gen_set_label(l1);
4768 tcg_temp_free_i64(t0);
4769 tcg_temp_free_i64(t1);
4770 tcg_temp_free(t2);
76a66253 4771 if (unlikely(Rc(ctx->opcode) != 0))
22e0e173 4772 gen_set_Rc0(ctx, cpu_gpr[rD(ctx->opcode)]);
76a66253
JM
4773}
4774
4775/* nabs - nabs. */
99e300ef 4776static void gen_nabs(DisasContext *ctx)
76a66253 4777{
22e0e173
AJ
4778 int l1 = gen_new_label();
4779 int l2 = gen_new_label();
4780 tcg_gen_brcondi_tl(TCG_COND_GT, cpu_gpr[rA(ctx->opcode)], 0, l1);
4781 tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)]);
4782 tcg_gen_br(l2);
4783 gen_set_label(l1);
4784 tcg_gen_neg_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)]);
4785 gen_set_label(l2);
76a66253 4786 if (unlikely(Rc(ctx->opcode) != 0))
22e0e173 4787 gen_set_Rc0(ctx, cpu_gpr[rD(ctx->opcode)]);
76a66253
JM
4788}
4789
4790/* nabso - nabso. */
99e300ef 4791static void gen_nabso(DisasContext *ctx)
76a66253 4792{
22e0e173
AJ
4793 int l1 = gen_new_label();
4794 int l2 = gen_new_label();
4795 tcg_gen_brcondi_tl(TCG_COND_GT, cpu_gpr[rA(ctx->opcode)], 0, l1);
4796 tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)]);
4797 tcg_gen_br(l2);
4798 gen_set_label(l1);
4799 tcg_gen_neg_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)]);
4800 gen_set_label(l2);
4801 /* nabs never overflows */
da91a00f 4802 tcg_gen_movi_tl(cpu_ov, 0);
76a66253 4803 if (unlikely(Rc(ctx->opcode) != 0))
22e0e173 4804 gen_set_Rc0(ctx, cpu_gpr[rD(ctx->opcode)]);
76a66253
JM
4805}
4806
4807/* rlmi - rlmi. */
99e300ef 4808static void gen_rlmi(DisasContext *ctx)
76a66253 4809{
7487953d
AJ
4810 uint32_t mb = MB(ctx->opcode);
4811 uint32_t me = ME(ctx->opcode);
4812 TCGv t0 = tcg_temp_new();
4813 tcg_gen_andi_tl(t0, cpu_gpr[rB(ctx->opcode)], 0x1F);
4814 tcg_gen_rotl_tl(t0, cpu_gpr[rS(ctx->opcode)], t0);
4815 tcg_gen_andi_tl(t0, t0, MASK(mb, me));
4816 tcg_gen_andi_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rA(ctx->opcode)], ~MASK(mb, me));
4817 tcg_gen_or_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rA(ctx->opcode)], t0);
4818 tcg_temp_free(t0);
76a66253 4819 if (unlikely(Rc(ctx->opcode) != 0))
7487953d 4820 gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
76a66253
JM
4821}
4822
4823/* rrib - rrib. */
99e300ef 4824static void gen_rrib(DisasContext *ctx)
76a66253 4825{
7487953d
AJ
4826 TCGv t0 = tcg_temp_new();
4827 TCGv t1 = tcg_temp_new();
4828 tcg_gen_andi_tl(t0, cpu_gpr[rB(ctx->opcode)], 0x1F);
4829 tcg_gen_movi_tl(t1, 0x80000000);
4830 tcg_gen_shr_tl(t1, t1, t0);
4831 tcg_gen_shr_tl(t0, cpu_gpr[rS(ctx->opcode)], t0);
4832 tcg_gen_and_tl(t0, t0, t1);
4833 tcg_gen_andc_tl(t1, cpu_gpr[rA(ctx->opcode)], t1);
4834 tcg_gen_or_tl(cpu_gpr[rA(ctx->opcode)], t0, t1);
4835 tcg_temp_free(t0);
4836 tcg_temp_free(t1);
76a66253 4837 if (unlikely(Rc(ctx->opcode) != 0))
7487953d 4838 gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
76a66253
JM
4839}
4840
4841/* sle - sle. */
99e300ef 4842static void gen_sle(DisasContext *ctx)
76a66253 4843{
7487953d
AJ
4844 TCGv t0 = tcg_temp_new();
4845 TCGv t1 = tcg_temp_new();
4846 tcg_gen_andi_tl(t1, cpu_gpr[rB(ctx->opcode)], 0x1F);
4847 tcg_gen_shl_tl(t0, cpu_gpr[rS(ctx->opcode)], t1);
4848 tcg_gen_subfi_tl(t1, 32, t1);
4849 tcg_gen_shr_tl(t1, cpu_gpr[rS(ctx->opcode)], t1);
4850 tcg_gen_or_tl(t1, t0, t1);
4851 tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], t0);
4852 gen_store_spr(SPR_MQ, t1);
4853 tcg_temp_free(t0);
4854 tcg_temp_free(t1);
76a66253 4855 if (unlikely(Rc(ctx->opcode) != 0))
7487953d 4856 gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
76a66253
JM
4857}
4858
4859/* sleq - sleq. */
99e300ef 4860static void gen_sleq(DisasContext *ctx)
76a66253 4861{
7487953d
AJ
4862 TCGv t0 = tcg_temp_new();
4863 TCGv t1 = tcg_temp_new();
4864 TCGv t2 = tcg_temp_new();
4865 tcg_gen_andi_tl(t0, cpu_gpr[rB(ctx->opcode)], 0x1F);
4866 tcg_gen_movi_tl(t2, 0xFFFFFFFF);
4867 tcg_gen_shl_tl(t2, t2, t0);
4868 tcg_gen_rotl_tl(t0, cpu_gpr[rS(ctx->opcode)], t0);
4869 gen_load_spr(t1, SPR_MQ);
4870 gen_store_spr(SPR_MQ, t0);
4871 tcg_gen_and_tl(t0, t0, t2);
4872 tcg_gen_andc_tl(t1, t1, t2);
4873 tcg_gen_or_tl(cpu_gpr[rA(ctx->opcode)], t0, t1);
4874 tcg_temp_free(t0);
4875 tcg_temp_free(t1);
4876 tcg_temp_free(t2);
76a66253 4877 if (unlikely(Rc(ctx->opcode) != 0))
7487953d 4878 gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
76a66253
JM
4879}
4880
4881/* sliq - sliq. */
99e300ef 4882static void gen_sliq(DisasContext *ctx)
76a66253 4883{
7487953d
AJ
4884 int sh = SH(ctx->opcode);
4885 TCGv t0 = tcg_temp_new();
4886 TCGv t1 = tcg_temp_new();
4887 tcg_gen_shli_tl(t0, cpu_gpr[rS(ctx->opcode)], sh);
4888 tcg_gen_shri_tl(t1, cpu_gpr[rS(ctx->opcode)], 32 - sh);
4889 tcg_gen_or_tl(t1, t0, t1);
4890 tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], t0);
4891 gen_store_spr(SPR_MQ, t1);
4892 tcg_temp_free(t0);
4893 tcg_temp_free(t1);
76a66253 4894 if (unlikely(Rc(ctx->opcode) != 0))
7487953d 4895 gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
76a66253
JM
4896}
4897
4898/* slliq - slliq. */
99e300ef 4899static void gen_slliq(DisasContext *ctx)
76a66253 4900{
7487953d
AJ
4901 int sh = SH(ctx->opcode);
4902 TCGv t0 = tcg_temp_new();
4903 TCGv t1 = tcg_temp_new();
4904 tcg_gen_rotli_tl(t0, cpu_gpr[rS(ctx->opcode)], sh);
4905 gen_load_spr(t1, SPR_MQ);
4906 gen_store_spr(SPR_MQ, t0);
4907 tcg_gen_andi_tl(t0, t0, (0xFFFFFFFFU << sh));
4908 tcg_gen_andi_tl(t1, t1, ~(0xFFFFFFFFU << sh));
4909 tcg_gen_or_tl(cpu_gpr[rA(ctx->opcode)], t0, t1);
4910 tcg_temp_free(t0);
4911 tcg_temp_free(t1);
76a66253 4912 if (unlikely(Rc(ctx->opcode) != 0))
7487953d 4913 gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
76a66253
JM
4914}
4915
4916/* sllq - sllq. */
99e300ef 4917static void gen_sllq(DisasContext *ctx)
76a66253 4918{
7487953d
AJ
4919 int l1 = gen_new_label();
4920 int l2 = gen_new_label();
4921 TCGv t0 = tcg_temp_local_new();
4922 TCGv t1 = tcg_temp_local_new();
4923 TCGv t2 = tcg_temp_local_new();
4924 tcg_gen_andi_tl(t2, cpu_gpr[rB(ctx->opcode)], 0x1F);
4925 tcg_gen_movi_tl(t1, 0xFFFFFFFF);
4926 tcg_gen_shl_tl(t1, t1, t2);
4927 tcg_gen_andi_tl(t0, cpu_gpr[rB(ctx->opcode)], 0x20);
4928 tcg_gen_brcondi_tl(TCG_COND_EQ, t0, 0, l1);
4929 gen_load_spr(t0, SPR_MQ);
4930 tcg_gen_and_tl(cpu_gpr[rA(ctx->opcode)], t0, t1);
4931 tcg_gen_br(l2);
4932 gen_set_label(l1);
4933 tcg_gen_shl_tl(t0, cpu_gpr[rS(ctx->opcode)], t2);
4934 gen_load_spr(t2, SPR_MQ);
4935 tcg_gen_andc_tl(t1, t2, t1);
4936 tcg_gen_or_tl(cpu_gpr[rA(ctx->opcode)], t0, t1);
4937 gen_set_label(l2);
4938 tcg_temp_free(t0);
4939 tcg_temp_free(t1);
4940 tcg_temp_free(t2);
76a66253 4941 if (unlikely(Rc(ctx->opcode) != 0))
7487953d 4942 gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
76a66253
JM
4943}
4944
4945/* slq - slq. */
99e300ef 4946static void gen_slq(DisasContext *ctx)
76a66253 4947{
7487953d
AJ
4948 int l1 = gen_new_label();
4949 TCGv t0 = tcg_temp_new();
4950 TCGv t1 = tcg_temp_new();
4951 tcg_gen_andi_tl(t1, cpu_gpr[rB(ctx->opcode)], 0x1F);
4952 tcg_gen_shl_tl(t0, cpu_gpr[rS(ctx->opcode)], t1);
4953 tcg_gen_subfi_tl(t1, 32, t1);
4954 tcg_gen_shr_tl(t1, cpu_gpr[rS(ctx->opcode)], t1);
4955 tcg_gen_or_tl(t1, t0, t1);
4956 gen_store_spr(SPR_MQ, t1);
4957 tcg_gen_andi_tl(t1, cpu_gpr[rB(ctx->opcode)], 0x20);
4958 tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], t0);
4959 tcg_gen_brcondi_tl(TCG_COND_EQ, t1, 0, l1);
4960 tcg_gen_movi_tl(cpu_gpr[rA(ctx->opcode)], 0);
4961 gen_set_label(l1);
4962 tcg_temp_free(t0);
4963 tcg_temp_free(t1);
76a66253 4964 if (unlikely(Rc(ctx->opcode) != 0))
7487953d 4965 gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
76a66253
JM
4966}
4967
d9bce9d9 4968/* sraiq - sraiq. */
99e300ef 4969static void gen_sraiq(DisasContext *ctx)
76a66253 4970{
7487953d
AJ
4971 int sh = SH(ctx->opcode);
4972 int l1 = gen_new_label();
4973 TCGv t0 = tcg_temp_new();
4974 TCGv t1 = tcg_temp_new();
4975 tcg_gen_shri_tl(t0, cpu_gpr[rS(ctx->opcode)], sh);
4976 tcg_gen_shli_tl(t1, cpu_gpr[rS(ctx->opcode)], 32 - sh);
4977 tcg_gen_or_tl(t0, t0, t1);
4978 gen_store_spr(SPR_MQ, t0);
da91a00f 4979 tcg_gen_movi_tl(cpu_ca, 0);
7487953d
AJ
4980 tcg_gen_brcondi_tl(TCG_COND_EQ, t1, 0, l1);
4981 tcg_gen_brcondi_tl(TCG_COND_GE, cpu_gpr[rS(ctx->opcode)], 0, l1);
da91a00f 4982 tcg_gen_movi_tl(cpu_ca, 1);
7487953d
AJ
4983 gen_set_label(l1);
4984 tcg_gen_sari_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)], sh);
4985 tcg_temp_free(t0);
4986 tcg_temp_free(t1);
76a66253 4987 if (unlikely(Rc(ctx->opcode) != 0))
7487953d 4988 gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
76a66253
JM
4989}
4990
4991/* sraq - sraq. */
99e300ef 4992static void gen_sraq(DisasContext *ctx)
76a66253 4993{
7487953d
AJ
4994 int l1 = gen_new_label();
4995 int l2 = gen_new_label();
4996 TCGv t0 = tcg_temp_new();
4997 TCGv t1 = tcg_temp_local_new();
4998 TCGv t2 = tcg_temp_local_new();
4999 tcg_gen_andi_tl(t2, cpu_gpr[rB(ctx->opcode)], 0x1F);
5000 tcg_gen_shr_tl(t0, cpu_gpr[rS(ctx->opcode)], t2);
5001 tcg_gen_sar_tl(t1, cpu_gpr[rS(ctx->opcode)], t2);
5002 tcg_gen_subfi_tl(t2, 32, t2);
5003 tcg_gen_shl_tl(t2, cpu_gpr[rS(ctx->opcode)], t2);
5004 tcg_gen_or_tl(t0, t0, t2);
5005 gen_store_spr(SPR_MQ, t0);
5006 tcg_gen_andi_tl(t0, cpu_gpr[rB(ctx->opcode)], 0x20);
5007 tcg_gen_brcondi_tl(TCG_COND_EQ, t2, 0, l1);
5008 tcg_gen_mov_tl(t2, cpu_gpr[rS(ctx->opcode)]);
5009 tcg_gen_sari_tl(t1, cpu_gpr[rS(ctx->opcode)], 31);
5010 gen_set_label(l1);
5011 tcg_temp_free(t0);
5012 tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], t1);
da91a00f 5013 tcg_gen_movi_tl(cpu_ca, 0);
7487953d
AJ
5014 tcg_gen_brcondi_tl(TCG_COND_GE, t1, 0, l2);
5015 tcg_gen_brcondi_tl(TCG_COND_EQ, t2, 0, l2);
da91a00f 5016 tcg_gen_movi_tl(cpu_ca, 1);
7487953d
AJ
5017 gen_set_label(l2);
5018 tcg_temp_free(t1);
5019 tcg_temp_free(t2);
76a66253 5020 if (unlikely(Rc(ctx->opcode) != 0))
7487953d 5021 gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
76a66253
JM
5022}
5023
5024/* sre - sre. */
99e300ef 5025static void gen_sre(DisasContext *ctx)
76a66253 5026{
7487953d
AJ
5027 TCGv t0 = tcg_temp_new();
5028 TCGv t1 = tcg_temp_new();
5029 tcg_gen_andi_tl(t1, cpu_gpr[rB(ctx->opcode)], 0x1F);
5030 tcg_gen_shr_tl(t0, cpu_gpr[rS(ctx->opcode)], t1);
5031 tcg_gen_subfi_tl(t1, 32, t1);
5032 tcg_gen_shl_tl(t1, cpu_gpr[rS(ctx->opcode)], t1);
5033 tcg_gen_or_tl(t1, t0, t1);
5034 tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], t0);
5035 gen_store_spr(SPR_MQ, t1);
5036 tcg_temp_free(t0);
5037 tcg_temp_free(t1);
76a66253 5038 if (unlikely(Rc(ctx->opcode) != 0))
7487953d 5039 gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
76a66253
JM
5040}
5041
5042/* srea - srea. */
99e300ef 5043static void gen_srea(DisasContext *ctx)
76a66253 5044{
7487953d
AJ
5045 TCGv t0 = tcg_temp_new();
5046 TCGv t1 = tcg_temp_new();
5047 tcg_gen_andi_tl(t1, cpu_gpr[rB(ctx->opcode)], 0x1F);
5048 tcg_gen_rotr_tl(t0, cpu_gpr[rS(ctx->opcode)], t1);
5049 gen_store_spr(SPR_MQ, t0);
5050 tcg_gen_sar_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)], t1);
5051 tcg_temp_free(t0);
5052 tcg_temp_free(t1);
76a66253 5053 if (unlikely(Rc(ctx->opcode) != 0))
7487953d 5054 gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
76a66253
JM
5055}
5056
5057/* sreq */
99e300ef 5058static void gen_sreq(DisasContext *ctx)
76a66253 5059{
7487953d
AJ
5060 TCGv t0 = tcg_temp_new();
5061 TCGv t1 = tcg_temp_new();
5062 TCGv t2 = tcg_temp_new();
5063 tcg_gen_andi_tl(t0, cpu_gpr[rB(ctx->opcode)], 0x1F);
5064 tcg_gen_movi_tl(t1, 0xFFFFFFFF);
5065 tcg_gen_shr_tl(t1, t1, t0);
5066 tcg_gen_rotr_tl(t0, cpu_gpr[rS(ctx->opcode)], t0);
5067 gen_load_spr(t2, SPR_MQ);
5068 gen_store_spr(SPR_MQ, t0);
5069 tcg_gen_and_tl(t0, t0, t1);
5070 tcg_gen_andc_tl(t2, t2, t1);
5071 tcg_gen_or_tl(cpu_gpr[rA(ctx->opcode)], t0, t2);
5072 tcg_temp_free(t0);
5073 tcg_temp_free(t1);
5074 tcg_temp_free(t2);
76a66253 5075 if (unlikely(Rc(ctx->opcode) != 0))
7487953d 5076 gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
76a66253
JM
5077}
5078
5079/* sriq */
99e300ef 5080static void gen_sriq(DisasContext *ctx)
76a66253 5081{
7487953d
AJ
5082 int sh = SH(ctx->opcode);
5083 TCGv t0 = tcg_temp_new();
5084 TCGv t1 = tcg_temp_new();
5085 tcg_gen_shri_tl(t0, cpu_gpr[rS(ctx->opcode)], sh);
5086 tcg_gen_shli_tl(t1, cpu_gpr[rS(ctx->opcode)], 32 - sh);
5087 tcg_gen_or_tl(t1, t0, t1);
5088 tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], t0);
5089 gen_store_spr(SPR_MQ, t1);
5090 tcg_temp_free(t0);
5091 tcg_temp_free(t1);
76a66253 5092 if (unlikely(Rc(ctx->opcode) != 0))
7487953d 5093 gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
76a66253
JM
5094}
5095
5096/* srliq */
99e300ef 5097static void gen_srliq(DisasContext *ctx)
76a66253 5098{
7487953d
AJ
5099 int sh = SH(ctx->opcode);
5100 TCGv t0 = tcg_temp_new();
5101 TCGv t1 = tcg_temp_new();
5102 tcg_gen_rotri_tl(t0, cpu_gpr[rS(ctx->opcode)], sh);
5103 gen_load_spr(t1, SPR_MQ);
5104 gen_store_spr(SPR_MQ, t0);
5105 tcg_gen_andi_tl(t0, t0, (0xFFFFFFFFU >> sh));
5106 tcg_gen_andi_tl(t1, t1, ~(0xFFFFFFFFU >> sh));
5107 tcg_gen_or_tl(cpu_gpr[rA(ctx->opcode)], t0, t1);
5108 tcg_temp_free(t0);
5109 tcg_temp_free(t1);
76a66253 5110 if (unlikely(Rc(ctx->opcode) != 0))
7487953d 5111 gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
76a66253
JM
5112}
5113
5114/* srlq */
99e300ef 5115static void gen_srlq(DisasContext *ctx)
76a66253 5116{
7487953d
AJ
5117 int l1 = gen_new_label();
5118 int l2 = gen_new_label();
5119 TCGv t0 = tcg_temp_local_new();
5120 TCGv t1 = tcg_temp_local_new();
5121 TCGv t2 = tcg_temp_local_new();
5122 tcg_gen_andi_tl(t2, cpu_gpr[rB(ctx->opcode)], 0x1F);
5123 tcg_gen_movi_tl(t1, 0xFFFFFFFF);
5124 tcg_gen_shr_tl(t2, t1, t2);
5125 tcg_gen_andi_tl(t0, cpu_gpr[rB(ctx->opcode)], 0x20);
5126 tcg_gen_brcondi_tl(TCG_COND_EQ, t0, 0, l1);
5127 gen_load_spr(t0, SPR_MQ);
5128 tcg_gen_and_tl(cpu_gpr[rA(ctx->opcode)], t0, t2);
5129 tcg_gen_br(l2);
5130 gen_set_label(l1);
5131 tcg_gen_shr_tl(t0, cpu_gpr[rS(ctx->opcode)], t2);
5132 tcg_gen_and_tl(t0, t0, t2);
5133 gen_load_spr(t1, SPR_MQ);
5134 tcg_gen_andc_tl(t1, t1, t2);
5135 tcg_gen_or_tl(cpu_gpr[rA(ctx->opcode)], t0, t1);
5136 gen_set_label(l2);
5137 tcg_temp_free(t0);
5138 tcg_temp_free(t1);
5139 tcg_temp_free(t2);
76a66253 5140 if (unlikely(Rc(ctx->opcode) != 0))
7487953d 5141 gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
76a66253
JM
5142}
5143
5144/* srq */
99e300ef 5145static void gen_srq(DisasContext *ctx)
76a66253 5146{
7487953d
AJ
5147 int l1 = gen_new_label();
5148 TCGv t0 = tcg_temp_new();
5149 TCGv t1 = tcg_temp_new();
5150 tcg_gen_andi_tl(t1, cpu_gpr[rB(ctx->opcode)], 0x1F);
5151 tcg_gen_shr_tl(t0, cpu_gpr[rS(ctx->opcode)], t1);
5152 tcg_gen_subfi_tl(t1, 32, t1);
5153 tcg_gen_shl_tl(t1, cpu_gpr[rS(ctx->opcode)], t1);
5154 tcg_gen_or_tl(t1, t0, t1);
5155 gen_store_spr(SPR_MQ, t1);
5156 tcg_gen_andi_tl(t1, cpu_gpr[rB(ctx->opcode)], 0x20);
5157 tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], t0);
5158 tcg_gen_brcondi_tl(TCG_COND_EQ, t0, 0, l1);
5159 tcg_gen_movi_tl(cpu_gpr[rA(ctx->opcode)], 0);
5160 gen_set_label(l1);
5161 tcg_temp_free(t0);
5162 tcg_temp_free(t1);
76a66253 5163 if (unlikely(Rc(ctx->opcode) != 0))
7487953d 5164 gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
76a66253
JM
5165}
5166
5167/* PowerPC 602 specific instructions */
99e300ef 5168
54623277 5169/* dsa */
99e300ef 5170static void gen_dsa(DisasContext *ctx)
76a66253
JM
5171{
5172 /* XXX: TODO */
e06fcd75 5173 gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL);
76a66253
JM
5174}
5175
5176/* esa */
99e300ef 5177static void gen_esa(DisasContext *ctx)
76a66253
JM
5178{
5179 /* XXX: TODO */
e06fcd75 5180 gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL);
76a66253
JM
5181}
5182
5183/* mfrom */
99e300ef 5184static void gen_mfrom(DisasContext *ctx)
76a66253
JM
5185{
5186#if defined(CONFIG_USER_ONLY)
e06fcd75 5187 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
76a66253 5188#else
76db3ba4 5189 if (unlikely(!ctx->mem_idx)) {
e06fcd75 5190 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
76a66253
JM
5191 return;
5192 }
cf02a65c 5193 gen_helper_602_mfrom(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)]);
76a66253
JM
5194#endif
5195}
5196
5197/* 602 - 603 - G2 TLB management */
e8eaa2c0 5198
54623277 5199/* tlbld */
e8eaa2c0 5200static void gen_tlbld_6xx(DisasContext *ctx)
76a66253
JM
5201{
5202#if defined(CONFIG_USER_ONLY)
e06fcd75 5203 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
76a66253 5204#else
76db3ba4 5205 if (unlikely(!ctx->mem_idx)) {
e06fcd75 5206 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
76a66253
JM
5207 return;
5208 }
c6c7cf05 5209 gen_helper_6xx_tlbd(cpu_env, cpu_gpr[rB(ctx->opcode)]);
76a66253
JM
5210#endif
5211}
5212
5213/* tlbli */
e8eaa2c0 5214static void gen_tlbli_6xx(DisasContext *ctx)
76a66253
JM
5215{
5216#if defined(CONFIG_USER_ONLY)
e06fcd75 5217 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
76a66253 5218#else
76db3ba4 5219 if (unlikely(!ctx->mem_idx)) {
e06fcd75 5220 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
76a66253
JM
5221 return;
5222 }
c6c7cf05 5223 gen_helper_6xx_tlbi(cpu_env, cpu_gpr[rB(ctx->opcode)]);
76a66253
JM
5224#endif
5225}
5226
7dbe11ac 5227/* 74xx TLB management */
e8eaa2c0 5228
54623277 5229/* tlbld */
e8eaa2c0 5230static void gen_tlbld_74xx(DisasContext *ctx)
7dbe11ac
JM
5231{
5232#if defined(CONFIG_USER_ONLY)
e06fcd75 5233 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
7dbe11ac 5234#else
76db3ba4 5235 if (unlikely(!ctx->mem_idx)) {
e06fcd75 5236 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
7dbe11ac
JM
5237 return;
5238 }
c6c7cf05 5239 gen_helper_74xx_tlbd(cpu_env, cpu_gpr[rB(ctx->opcode)]);
7dbe11ac
JM
5240#endif
5241}
5242
5243/* tlbli */
e8eaa2c0 5244static void gen_tlbli_74xx(DisasContext *ctx)
7dbe11ac
JM
5245{
5246#if defined(CONFIG_USER_ONLY)
e06fcd75 5247 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
7dbe11ac 5248#else
76db3ba4 5249 if (unlikely(!ctx->mem_idx)) {
e06fcd75 5250 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
7dbe11ac
JM
5251 return;
5252 }
c6c7cf05 5253 gen_helper_74xx_tlbi(cpu_env, cpu_gpr[rB(ctx->opcode)]);
7dbe11ac
JM
5254#endif
5255}
5256
76a66253 5257/* POWER instructions not in PowerPC 601 */
99e300ef 5258
54623277 5259/* clf */
99e300ef 5260static void gen_clf(DisasContext *ctx)
76a66253
JM
5261{
5262 /* Cache line flush: implemented as no-op */
5263}
5264
5265/* cli */
99e300ef 5266static void gen_cli(DisasContext *ctx)
76a66253 5267{
7f75ffd3 5268 /* Cache line invalidate: privileged and treated as no-op */
76a66253 5269#if defined(CONFIG_USER_ONLY)
e06fcd75 5270 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
76a66253 5271#else
76db3ba4 5272 if (unlikely(!ctx->mem_idx)) {
e06fcd75 5273 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
76a66253
JM
5274 return;
5275 }
5276#endif
5277}
5278
5279/* dclst */
99e300ef 5280static void gen_dclst(DisasContext *ctx)
76a66253
JM
5281{
5282 /* Data cache line store: treated as no-op */
5283}
5284
99e300ef 5285static void gen_mfsri(DisasContext *ctx)
76a66253
JM
5286{
5287#if defined(CONFIG_USER_ONLY)
e06fcd75 5288 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
76a66253 5289#else
74d37793
AJ
5290 int ra = rA(ctx->opcode);
5291 int rd = rD(ctx->opcode);
5292 TCGv t0;
76db3ba4 5293 if (unlikely(!ctx->mem_idx)) {
e06fcd75 5294 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
76a66253
JM
5295 return;
5296 }
74d37793 5297 t0 = tcg_temp_new();
76db3ba4 5298 gen_addr_reg_index(ctx, t0);
74d37793
AJ
5299 tcg_gen_shri_tl(t0, t0, 28);
5300 tcg_gen_andi_tl(t0, t0, 0xF);
c6c7cf05 5301 gen_helper_load_sr(cpu_gpr[rd], cpu_env, t0);
74d37793 5302 tcg_temp_free(t0);
76a66253 5303 if (ra != 0 && ra != rd)
74d37793 5304 tcg_gen_mov_tl(cpu_gpr[ra], cpu_gpr[rd]);
76a66253
JM
5305#endif
5306}
5307
99e300ef 5308static void gen_rac(DisasContext *ctx)
76a66253
JM
5309{
5310#if defined(CONFIG_USER_ONLY)
e06fcd75 5311 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
76a66253 5312#else
22e0e173 5313 TCGv t0;
76db3ba4 5314 if (unlikely(!ctx->mem_idx)) {
e06fcd75 5315 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
76a66253
JM
5316 return;
5317 }
22e0e173 5318 t0 = tcg_temp_new();
76db3ba4 5319 gen_addr_reg_index(ctx, t0);
c6c7cf05 5320 gen_helper_rac(cpu_gpr[rD(ctx->opcode)], cpu_env, t0);
22e0e173 5321 tcg_temp_free(t0);
76a66253
JM
5322#endif
5323}
5324
99e300ef 5325static void gen_rfsvc(DisasContext *ctx)
76a66253
JM
5326{
5327#if defined(CONFIG_USER_ONLY)
e06fcd75 5328 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
76a66253 5329#else
76db3ba4 5330 if (unlikely(!ctx->mem_idx)) {
e06fcd75 5331 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
76a66253
JM
5332 return;
5333 }
e5f17ac6 5334 gen_helper_rfsvc(cpu_env);
e06fcd75 5335 gen_sync_exception(ctx);
76a66253
JM
5336#endif
5337}
5338
5339/* svc is not implemented for now */
5340
5341/* POWER2 specific instructions */
5342/* Quad manipulation (load/store two floats at a time) */
76a66253
JM
5343
5344/* lfq */
99e300ef 5345static void gen_lfq(DisasContext *ctx)
76a66253 5346{
01a4afeb 5347 int rd = rD(ctx->opcode);
76db3ba4
AJ
5348 TCGv t0;
5349 gen_set_access_type(ctx, ACCESS_FLOAT);
5350 t0 = tcg_temp_new();
5351 gen_addr_imm_index(ctx, t0, 0);
5352 gen_qemu_ld64(ctx, cpu_fpr[rd], t0);
5353 gen_addr_add(ctx, t0, t0, 8);
5354 gen_qemu_ld64(ctx, cpu_fpr[(rd + 1) % 32], t0);
01a4afeb 5355 tcg_temp_free(t0);
76a66253
JM
5356}
5357
5358/* lfqu */
99e300ef 5359static void gen_lfqu(DisasContext *ctx)
76a66253
JM
5360{
5361 int ra = rA(ctx->opcode);
01a4afeb 5362 int rd = rD(ctx->opcode);
76db3ba4
AJ
5363 TCGv t0, t1;
5364 gen_set_access_type(ctx, ACCESS_FLOAT);
5365 t0 = tcg_temp_new();
5366 t1 = tcg_temp_new();
5367 gen_addr_imm_index(ctx, t0, 0);
5368 gen_qemu_ld64(ctx, cpu_fpr[rd], t0);
5369 gen_addr_add(ctx, t1, t0, 8);
5370 gen_qemu_ld64(ctx, cpu_fpr[(rd + 1) % 32], t1);
76a66253 5371 if (ra != 0)
01a4afeb
AJ
5372 tcg_gen_mov_tl(cpu_gpr[ra], t0);
5373 tcg_temp_free(t0);
5374 tcg_temp_free(t1);
76a66253
JM
5375}
5376
5377/* lfqux */
99e300ef 5378static void gen_lfqux(DisasContext *ctx)
76a66253
JM
5379{
5380 int ra = rA(ctx->opcode);
01a4afeb 5381 int rd = rD(ctx->opcode);
76db3ba4
AJ
5382 gen_set_access_type(ctx, ACCESS_FLOAT);
5383 TCGv t0, t1;
5384 t0 = tcg_temp_new();
5385 gen_addr_reg_index(ctx, t0);
5386 gen_qemu_ld64(ctx, cpu_fpr[rd], t0);
5387 t1 = tcg_temp_new();
5388 gen_addr_add(ctx, t1, t0, 8);
5389 gen_qemu_ld64(ctx, cpu_fpr[(rd + 1) % 32], t1);
5390 tcg_temp_free(t1);
76a66253 5391 if (ra != 0)
01a4afeb
AJ
5392 tcg_gen_mov_tl(cpu_gpr[ra], t0);
5393 tcg_temp_free(t0);
76a66253
JM
5394}
5395
5396/* lfqx */
99e300ef 5397static void gen_lfqx(DisasContext *ctx)
76a66253 5398{
01a4afeb 5399 int rd = rD(ctx->opcode);
76db3ba4
AJ
5400 TCGv t0;
5401 gen_set_access_type(ctx, ACCESS_FLOAT);
5402 t0 = tcg_temp_new();
5403 gen_addr_reg_index(ctx, t0);
5404 gen_qemu_ld64(ctx, cpu_fpr[rd], t0);
5405 gen_addr_add(ctx, t0, t0, 8);
5406 gen_qemu_ld64(ctx, cpu_fpr[(rd + 1) % 32], t0);
01a4afeb 5407 tcg_temp_free(t0);
76a66253
JM
5408}
5409
5410/* stfq */
99e300ef 5411static void gen_stfq(DisasContext *ctx)
76a66253 5412{
01a4afeb 5413 int rd = rD(ctx->opcode);
76db3ba4
AJ
5414 TCGv t0;
5415 gen_set_access_type(ctx, ACCESS_FLOAT);
5416 t0 = tcg_temp_new();
5417 gen_addr_imm_index(ctx, t0, 0);
5418 gen_qemu_st64(ctx, cpu_fpr[rd], t0);
5419 gen_addr_add(ctx, t0, t0, 8);
5420 gen_qemu_st64(ctx, cpu_fpr[(rd + 1) % 32], t0);
01a4afeb 5421 tcg_temp_free(t0);
76a66253
JM
5422}
5423
5424/* stfqu */
99e300ef 5425static void gen_stfqu(DisasContext *ctx)
76a66253
JM
5426{
5427 int ra = rA(ctx->opcode);
01a4afeb 5428 int rd = rD(ctx->opcode);
76db3ba4
AJ
5429 TCGv t0, t1;
5430 gen_set_access_type(ctx, ACCESS_FLOAT);
5431 t0 = tcg_temp_new();
5432 gen_addr_imm_index(ctx, t0, 0);
5433 gen_qemu_st64(ctx, cpu_fpr[rd], t0);
5434 t1 = tcg_temp_new();
5435 gen_addr_add(ctx, t1, t0, 8);
5436 gen_qemu_st64(ctx, cpu_fpr[(rd + 1) % 32], t1);
5437 tcg_temp_free(t1);
76a66253 5438 if (ra != 0)
01a4afeb
AJ
5439 tcg_gen_mov_tl(cpu_gpr[ra], t0);
5440 tcg_temp_free(t0);
76a66253
JM
5441}
5442
5443/* stfqux */
99e300ef 5444static void gen_stfqux(DisasContext *ctx)
76a66253
JM
5445{
5446 int ra = rA(ctx->opcode);
01a4afeb 5447 int rd = rD(ctx->opcode);
76db3ba4
AJ
5448 TCGv t0, t1;
5449 gen_set_access_type(ctx, ACCESS_FLOAT);
5450 t0 = tcg_temp_new();
5451 gen_addr_reg_index(ctx, t0);
5452 gen_qemu_st64(ctx, cpu_fpr[rd], t0);
5453 t1 = tcg_temp_new();
5454 gen_addr_add(ctx, t1, t0, 8);
5455 gen_qemu_st64(ctx, cpu_fpr[(rd + 1) % 32], t1);
5456 tcg_temp_free(t1);
76a66253 5457 if (ra != 0)
01a4afeb
AJ
5458 tcg_gen_mov_tl(cpu_gpr[ra], t0);
5459 tcg_temp_free(t0);
76a66253
JM
5460}
5461
5462/* stfqx */
99e300ef 5463static void gen_stfqx(DisasContext *ctx)
76a66253 5464{
01a4afeb 5465 int rd = rD(ctx->opcode);
76db3ba4
AJ
5466 TCGv t0;
5467 gen_set_access_type(ctx, ACCESS_FLOAT);
5468 t0 = tcg_temp_new();
5469 gen_addr_reg_index(ctx, t0);
5470 gen_qemu_st64(ctx, cpu_fpr[rd], t0);
5471 gen_addr_add(ctx, t0, t0, 8);
5472 gen_qemu_st64(ctx, cpu_fpr[(rd + 1) % 32], t0);
01a4afeb 5473 tcg_temp_free(t0);
76a66253
JM
5474}
5475
5476/* BookE specific instructions */
99e300ef 5477
54623277 5478/* XXX: not implemented on 440 ? */
99e300ef 5479static void gen_mfapidi(DisasContext *ctx)
76a66253
JM
5480{
5481 /* XXX: TODO */
e06fcd75 5482 gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL);
76a66253
JM
5483}
5484
2662a059 5485/* XXX: not implemented on 440 ? */
99e300ef 5486static void gen_tlbiva(DisasContext *ctx)
76a66253
JM
5487{
5488#if defined(CONFIG_USER_ONLY)
e06fcd75 5489 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
76a66253 5490#else
74d37793 5491 TCGv t0;
76db3ba4 5492 if (unlikely(!ctx->mem_idx)) {
e06fcd75 5493 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
76a66253
JM
5494 return;
5495 }
ec72e276 5496 t0 = tcg_temp_new();
76db3ba4 5497 gen_addr_reg_index(ctx, t0);
c6c7cf05 5498 gen_helper_tlbie(cpu_env, cpu_gpr[rB(ctx->opcode)]);
74d37793 5499 tcg_temp_free(t0);
76a66253
JM
5500#endif
5501}
5502
5503/* All 405 MAC instructions are translated here */
636aa200
BS
5504static inline void gen_405_mulladd_insn(DisasContext *ctx, int opc2, int opc3,
5505 int ra, int rb, int rt, int Rc)
76a66253 5506{
182608d4
AJ
5507 TCGv t0, t1;
5508
a7812ae4
PB
5509 t0 = tcg_temp_local_new();
5510 t1 = tcg_temp_local_new();
182608d4 5511
76a66253
JM
5512 switch (opc3 & 0x0D) {
5513 case 0x05:
5514 /* macchw - macchw. - macchwo - macchwo. */
5515 /* macchws - macchws. - macchwso - macchwso. */
5516 /* nmacchw - nmacchw. - nmacchwo - nmacchwo. */
5517 /* nmacchws - nmacchws. - nmacchwso - nmacchwso. */
5518 /* mulchw - mulchw. */
182608d4
AJ
5519 tcg_gen_ext16s_tl(t0, cpu_gpr[ra]);
5520 tcg_gen_sari_tl(t1, cpu_gpr[rb], 16);
5521 tcg_gen_ext16s_tl(t1, t1);
76a66253
JM
5522 break;
5523 case 0x04:
5524 /* macchwu - macchwu. - macchwuo - macchwuo. */
5525 /* macchwsu - macchwsu. - macchwsuo - macchwsuo. */
5526 /* mulchwu - mulchwu. */
182608d4
AJ
5527 tcg_gen_ext16u_tl(t0, cpu_gpr[ra]);
5528 tcg_gen_shri_tl(t1, cpu_gpr[rb], 16);
5529 tcg_gen_ext16u_tl(t1, t1);
76a66253
JM
5530 break;
5531 case 0x01:
5532 /* machhw - machhw. - machhwo - machhwo. */
5533 /* machhws - machhws. - machhwso - machhwso. */
5534 /* nmachhw - nmachhw. - nmachhwo - nmachhwo. */
5535 /* nmachhws - nmachhws. - nmachhwso - nmachhwso. */
5536 /* mulhhw - mulhhw. */
182608d4
AJ
5537 tcg_gen_sari_tl(t0, cpu_gpr[ra], 16);
5538 tcg_gen_ext16s_tl(t0, t0);
5539 tcg_gen_sari_tl(t1, cpu_gpr[rb], 16);
5540 tcg_gen_ext16s_tl(t1, t1);
76a66253
JM
5541 break;
5542 case 0x00:
5543 /* machhwu - machhwu. - machhwuo - machhwuo. */
5544 /* machhwsu - machhwsu. - machhwsuo - machhwsuo. */
5545 /* mulhhwu - mulhhwu. */
182608d4
AJ
5546 tcg_gen_shri_tl(t0, cpu_gpr[ra], 16);
5547 tcg_gen_ext16u_tl(t0, t0);
5548 tcg_gen_shri_tl(t1, cpu_gpr[rb], 16);
5549 tcg_gen_ext16u_tl(t1, t1);
76a66253
JM
5550 break;
5551 case 0x0D:
5552 /* maclhw - maclhw. - maclhwo - maclhwo. */
5553 /* maclhws - maclhws. - maclhwso - maclhwso. */
5554 /* nmaclhw - nmaclhw. - nmaclhwo - nmaclhwo. */
5555 /* nmaclhws - nmaclhws. - nmaclhwso - nmaclhwso. */
5556 /* mullhw - mullhw. */
182608d4
AJ
5557 tcg_gen_ext16s_tl(t0, cpu_gpr[ra]);
5558 tcg_gen_ext16s_tl(t1, cpu_gpr[rb]);
76a66253
JM
5559 break;
5560 case 0x0C:
5561 /* maclhwu - maclhwu. - maclhwuo - maclhwuo. */
5562 /* maclhwsu - maclhwsu. - maclhwsuo - maclhwsuo. */
5563 /* mullhwu - mullhwu. */
182608d4
AJ
5564 tcg_gen_ext16u_tl(t0, cpu_gpr[ra]);
5565 tcg_gen_ext16u_tl(t1, cpu_gpr[rb]);
76a66253
JM
5566 break;
5567 }
76a66253 5568 if (opc2 & 0x04) {
182608d4
AJ
5569 /* (n)multiply-and-accumulate (0x0C / 0x0E) */
5570 tcg_gen_mul_tl(t1, t0, t1);
5571 if (opc2 & 0x02) {
5572 /* nmultiply-and-accumulate (0x0E) */
5573 tcg_gen_sub_tl(t0, cpu_gpr[rt], t1);
5574 } else {
5575 /* multiply-and-accumulate (0x0C) */
5576 tcg_gen_add_tl(t0, cpu_gpr[rt], t1);
5577 }
5578
5579 if (opc3 & 0x12) {
5580 /* Check overflow and/or saturate */
5581 int l1 = gen_new_label();
5582
5583 if (opc3 & 0x10) {
5584 /* Start with XER OV disabled, the most likely case */
da91a00f 5585 tcg_gen_movi_tl(cpu_ov, 0);
182608d4
AJ
5586 }
5587 if (opc3 & 0x01) {
5588 /* Signed */
5589 tcg_gen_xor_tl(t1, cpu_gpr[rt], t1);
5590 tcg_gen_brcondi_tl(TCG_COND_GE, t1, 0, l1);
5591 tcg_gen_xor_tl(t1, cpu_gpr[rt], t0);
5592 tcg_gen_brcondi_tl(TCG_COND_LT, t1, 0, l1);
bdc4e053 5593 if (opc3 & 0x02) {
182608d4
AJ
5594 /* Saturate */
5595 tcg_gen_sari_tl(t0, cpu_gpr[rt], 31);
5596 tcg_gen_xori_tl(t0, t0, 0x7fffffff);
5597 }
5598 } else {
5599 /* Unsigned */
5600 tcg_gen_brcond_tl(TCG_COND_GEU, t0, t1, l1);
bdc4e053 5601 if (opc3 & 0x02) {
182608d4
AJ
5602 /* Saturate */
5603 tcg_gen_movi_tl(t0, UINT32_MAX);
5604 }
5605 }
5606 if (opc3 & 0x10) {
5607 /* Check overflow */
da91a00f
RH
5608 tcg_gen_movi_tl(cpu_ov, 1);
5609 tcg_gen_movi_tl(cpu_so, 1);
182608d4
AJ
5610 }
5611 gen_set_label(l1);
5612 tcg_gen_mov_tl(cpu_gpr[rt], t0);
5613 }
5614 } else {
5615 tcg_gen_mul_tl(cpu_gpr[rt], t0, t1);
76a66253 5616 }
182608d4
AJ
5617 tcg_temp_free(t0);
5618 tcg_temp_free(t1);
76a66253
JM
5619 if (unlikely(Rc) != 0) {
5620 /* Update Rc0 */
182608d4 5621 gen_set_Rc0(ctx, cpu_gpr[rt]);
76a66253
JM
5622 }
5623}
5624
a750fc0b 5625#define GEN_MAC_HANDLER(name, opc2, opc3) \
99e300ef 5626static void glue(gen_, name)(DisasContext *ctx) \
76a66253
JM
5627{ \
5628 gen_405_mulladd_insn(ctx, opc2, opc3, rA(ctx->opcode), rB(ctx->opcode), \
5629 rD(ctx->opcode), Rc(ctx->opcode)); \
5630}
5631
5632/* macchw - macchw. */
a750fc0b 5633GEN_MAC_HANDLER(macchw, 0x0C, 0x05);
76a66253 5634/* macchwo - macchwo. */
a750fc0b 5635GEN_MAC_HANDLER(macchwo, 0x0C, 0x15);
76a66253 5636/* macchws - macchws. */
a750fc0b 5637GEN_MAC_HANDLER(macchws, 0x0C, 0x07);
76a66253 5638/* macchwso - macchwso. */
a750fc0b 5639GEN_MAC_HANDLER(macchwso, 0x0C, 0x17);
76a66253 5640/* macchwsu - macchwsu. */
a750fc0b 5641GEN_MAC_HANDLER(macchwsu, 0x0C, 0x06);
76a66253 5642/* macchwsuo - macchwsuo. */
a750fc0b 5643GEN_MAC_HANDLER(macchwsuo, 0x0C, 0x16);
76a66253 5644/* macchwu - macchwu. */
a750fc0b 5645GEN_MAC_HANDLER(macchwu, 0x0C, 0x04);
76a66253 5646/* macchwuo - macchwuo. */
a750fc0b 5647GEN_MAC_HANDLER(macchwuo, 0x0C, 0x14);
76a66253 5648/* machhw - machhw. */
a750fc0b 5649GEN_MAC_HANDLER(machhw, 0x0C, 0x01);
76a66253 5650/* machhwo - machhwo. */
a750fc0b 5651GEN_MAC_HANDLER(machhwo, 0x0C, 0x11);
76a66253 5652/* machhws - machhws. */
a750fc0b 5653GEN_MAC_HANDLER(machhws, 0x0C, 0x03);
76a66253 5654/* machhwso - machhwso. */
a750fc0b 5655GEN_MAC_HANDLER(machhwso, 0x0C, 0x13);
76a66253 5656/* machhwsu - machhwsu. */
a750fc0b 5657GEN_MAC_HANDLER(machhwsu, 0x0C, 0x02);
76a66253 5658/* machhwsuo - machhwsuo. */
a750fc0b 5659GEN_MAC_HANDLER(machhwsuo, 0x0C, 0x12);
76a66253 5660/* machhwu - machhwu. */
a750fc0b 5661GEN_MAC_HANDLER(machhwu, 0x0C, 0x00);
76a66253 5662/* machhwuo - machhwuo. */
a750fc0b 5663GEN_MAC_HANDLER(machhwuo, 0x0C, 0x10);
76a66253 5664/* maclhw - maclhw. */
a750fc0b 5665GEN_MAC_HANDLER(maclhw, 0x0C, 0x0D);
76a66253 5666/* maclhwo - maclhwo. */
a750fc0b 5667GEN_MAC_HANDLER(maclhwo, 0x0C, 0x1D);
76a66253 5668/* maclhws - maclhws. */
a750fc0b 5669GEN_MAC_HANDLER(maclhws, 0x0C, 0x0F);
76a66253 5670/* maclhwso - maclhwso. */
a750fc0b 5671GEN_MAC_HANDLER(maclhwso, 0x0C, 0x1F);
76a66253 5672/* maclhwu - maclhwu. */
a750fc0b 5673GEN_MAC_HANDLER(maclhwu, 0x0C, 0x0C);
76a66253 5674/* maclhwuo - maclhwuo. */
a750fc0b 5675GEN_MAC_HANDLER(maclhwuo, 0x0C, 0x1C);
76a66253 5676/* maclhwsu - maclhwsu. */
a750fc0b 5677GEN_MAC_HANDLER(maclhwsu, 0x0C, 0x0E);
76a66253 5678/* maclhwsuo - maclhwsuo. */
a750fc0b 5679GEN_MAC_HANDLER(maclhwsuo, 0x0C, 0x1E);
76a66253 5680/* nmacchw - nmacchw. */
a750fc0b 5681GEN_MAC_HANDLER(nmacchw, 0x0E, 0x05);
76a66253 5682/* nmacchwo - nmacchwo. */
a750fc0b 5683GEN_MAC_HANDLER(nmacchwo, 0x0E, 0x15);
76a66253 5684/* nmacchws - nmacchws. */
a750fc0b 5685GEN_MAC_HANDLER(nmacchws, 0x0E, 0x07);
76a66253 5686/* nmacchwso - nmacchwso. */
a750fc0b 5687GEN_MAC_HANDLER(nmacchwso, 0x0E, 0x17);
76a66253 5688/* nmachhw - nmachhw. */
a750fc0b 5689GEN_MAC_HANDLER(nmachhw, 0x0E, 0x01);
76a66253 5690/* nmachhwo - nmachhwo. */
a750fc0b 5691GEN_MAC_HANDLER(nmachhwo, 0x0E, 0x11);
76a66253 5692/* nmachhws - nmachhws. */
a750fc0b 5693GEN_MAC_HANDLER(nmachhws, 0x0E, 0x03);
76a66253 5694/* nmachhwso - nmachhwso. */
a750fc0b 5695GEN_MAC_HANDLER(nmachhwso, 0x0E, 0x13);
76a66253 5696/* nmaclhw - nmaclhw. */
a750fc0b 5697GEN_MAC_HANDLER(nmaclhw, 0x0E, 0x0D);
76a66253 5698/* nmaclhwo - nmaclhwo. */
a750fc0b 5699GEN_MAC_HANDLER(nmaclhwo, 0x0E, 0x1D);
76a66253 5700/* nmaclhws - nmaclhws. */
a750fc0b 5701GEN_MAC_HANDLER(nmaclhws, 0x0E, 0x0F);
76a66253 5702/* nmaclhwso - nmaclhwso. */
a750fc0b 5703GEN_MAC_HANDLER(nmaclhwso, 0x0E, 0x1F);
76a66253
JM
5704
5705/* mulchw - mulchw. */
a750fc0b 5706GEN_MAC_HANDLER(mulchw, 0x08, 0x05);
76a66253 5707/* mulchwu - mulchwu. */
a750fc0b 5708GEN_MAC_HANDLER(mulchwu, 0x08, 0x04);
76a66253 5709/* mulhhw - mulhhw. */
a750fc0b 5710GEN_MAC_HANDLER(mulhhw, 0x08, 0x01);
76a66253 5711/* mulhhwu - mulhhwu. */
a750fc0b 5712GEN_MAC_HANDLER(mulhhwu, 0x08, 0x00);
76a66253 5713/* mullhw - mullhw. */
a750fc0b 5714GEN_MAC_HANDLER(mullhw, 0x08, 0x0D);
76a66253 5715/* mullhwu - mullhwu. */
a750fc0b 5716GEN_MAC_HANDLER(mullhwu, 0x08, 0x0C);
76a66253
JM
5717
5718/* mfdcr */
99e300ef 5719static void gen_mfdcr(DisasContext *ctx)
76a66253
JM
5720{
5721#if defined(CONFIG_USER_ONLY)
e06fcd75 5722 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
76a66253 5723#else
06dca6a7 5724 TCGv dcrn;
76db3ba4 5725 if (unlikely(!ctx->mem_idx)) {
e06fcd75 5726 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
76a66253
JM
5727 return;
5728 }
06dca6a7
AJ
5729 /* NIP cannot be restored if the memory exception comes from an helper */
5730 gen_update_nip(ctx, ctx->nip - 4);
5731 dcrn = tcg_const_tl(SPR(ctx->opcode));
d0f1562d 5732 gen_helper_load_dcr(cpu_gpr[rD(ctx->opcode)], cpu_env, dcrn);
06dca6a7 5733 tcg_temp_free(dcrn);
76a66253
JM
5734#endif
5735}
5736
5737/* mtdcr */
99e300ef 5738static void gen_mtdcr(DisasContext *ctx)
76a66253
JM
5739{
5740#if defined(CONFIG_USER_ONLY)
e06fcd75 5741 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
76a66253 5742#else
06dca6a7 5743 TCGv dcrn;
76db3ba4 5744 if (unlikely(!ctx->mem_idx)) {
e06fcd75 5745 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
76a66253
JM
5746 return;
5747 }
06dca6a7
AJ
5748 /* NIP cannot be restored if the memory exception comes from an helper */
5749 gen_update_nip(ctx, ctx->nip - 4);
5750 dcrn = tcg_const_tl(SPR(ctx->opcode));
d0f1562d 5751 gen_helper_store_dcr(cpu_env, dcrn, cpu_gpr[rS(ctx->opcode)]);
06dca6a7 5752 tcg_temp_free(dcrn);
a42bd6cc
JM
5753#endif
5754}
5755
5756/* mfdcrx */
2662a059 5757/* XXX: not implemented on 440 ? */
99e300ef 5758static void gen_mfdcrx(DisasContext *ctx)
a42bd6cc
JM
5759{
5760#if defined(CONFIG_USER_ONLY)
e06fcd75 5761 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
a42bd6cc 5762#else
76db3ba4 5763 if (unlikely(!ctx->mem_idx)) {
e06fcd75 5764 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
a42bd6cc
JM
5765 return;
5766 }
06dca6a7
AJ
5767 /* NIP cannot be restored if the memory exception comes from an helper */
5768 gen_update_nip(ctx, ctx->nip - 4);
d0f1562d
BS
5769 gen_helper_load_dcr(cpu_gpr[rD(ctx->opcode)], cpu_env,
5770 cpu_gpr[rA(ctx->opcode)]);
a750fc0b 5771 /* Note: Rc update flag set leads to undefined state of Rc0 */
a42bd6cc
JM
5772#endif
5773}
5774
5775/* mtdcrx */
2662a059 5776/* XXX: not implemented on 440 ? */
99e300ef 5777static void gen_mtdcrx(DisasContext *ctx)
a42bd6cc
JM
5778{
5779#if defined(CONFIG_USER_ONLY)
e06fcd75 5780 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
a42bd6cc 5781#else
76db3ba4 5782 if (unlikely(!ctx->mem_idx)) {
e06fcd75 5783 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
a42bd6cc
JM
5784 return;
5785 }
06dca6a7
AJ
5786 /* NIP cannot be restored if the memory exception comes from an helper */
5787 gen_update_nip(ctx, ctx->nip - 4);
d0f1562d
BS
5788 gen_helper_store_dcr(cpu_env, cpu_gpr[rA(ctx->opcode)],
5789 cpu_gpr[rS(ctx->opcode)]);
a750fc0b 5790 /* Note: Rc update flag set leads to undefined state of Rc0 */
76a66253
JM
5791#endif
5792}
5793
a750fc0b 5794/* mfdcrux (PPC 460) : user-mode access to DCR */
99e300ef 5795static void gen_mfdcrux(DisasContext *ctx)
a750fc0b 5796{
06dca6a7
AJ
5797 /* NIP cannot be restored if the memory exception comes from an helper */
5798 gen_update_nip(ctx, ctx->nip - 4);
d0f1562d
BS
5799 gen_helper_load_dcr(cpu_gpr[rD(ctx->opcode)], cpu_env,
5800 cpu_gpr[rA(ctx->opcode)]);
a750fc0b
JM
5801 /* Note: Rc update flag set leads to undefined state of Rc0 */
5802}
5803
5804/* mtdcrux (PPC 460) : user-mode access to DCR */
99e300ef 5805static void gen_mtdcrux(DisasContext *ctx)
a750fc0b 5806{
06dca6a7
AJ
5807 /* NIP cannot be restored if the memory exception comes from an helper */
5808 gen_update_nip(ctx, ctx->nip - 4);
975e5463 5809 gen_helper_store_dcr(cpu_env, cpu_gpr[rA(ctx->opcode)],
d0f1562d 5810 cpu_gpr[rS(ctx->opcode)]);
a750fc0b
JM
5811 /* Note: Rc update flag set leads to undefined state of Rc0 */
5812}
5813
76a66253 5814/* dccci */
99e300ef 5815static void gen_dccci(DisasContext *ctx)
76a66253
JM
5816{
5817#if defined(CONFIG_USER_ONLY)
e06fcd75 5818 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
76a66253 5819#else
76db3ba4 5820 if (unlikely(!ctx->mem_idx)) {
e06fcd75 5821 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
76a66253
JM
5822 return;
5823 }
5824 /* interpreted as no-op */
5825#endif
5826}
5827
5828/* dcread */
99e300ef 5829static void gen_dcread(DisasContext *ctx)
76a66253
JM
5830{
5831#if defined(CONFIG_USER_ONLY)
e06fcd75 5832 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
76a66253 5833#else
b61f2753 5834 TCGv EA, val;
76db3ba4 5835 if (unlikely(!ctx->mem_idx)) {
e06fcd75 5836 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
76a66253
JM
5837 return;
5838 }
76db3ba4 5839 gen_set_access_type(ctx, ACCESS_CACHE);
a7812ae4 5840 EA = tcg_temp_new();
76db3ba4 5841 gen_addr_reg_index(ctx, EA);
a7812ae4 5842 val = tcg_temp_new();
76db3ba4 5843 gen_qemu_ld32u(ctx, val, EA);
b61f2753
AJ
5844 tcg_temp_free(val);
5845 tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], EA);
5846 tcg_temp_free(EA);
76a66253
JM
5847#endif
5848}
5849
5850/* icbt */
e8eaa2c0 5851static void gen_icbt_40x(DisasContext *ctx)
76a66253
JM
5852{
5853 /* interpreted as no-op */
5854 /* XXX: specification say this is treated as a load by the MMU
5855 * but does not generate any exception
5856 */
5857}
5858
5859/* iccci */
99e300ef 5860static void gen_iccci(DisasContext *ctx)
76a66253
JM
5861{
5862#if defined(CONFIG_USER_ONLY)
e06fcd75 5863 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
76a66253 5864#else
76db3ba4 5865 if (unlikely(!ctx->mem_idx)) {
e06fcd75 5866 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
76a66253
JM
5867 return;
5868 }
5869 /* interpreted as no-op */
5870#endif
5871}
5872
5873/* icread */
99e300ef 5874static void gen_icread(DisasContext *ctx)
76a66253
JM
5875{
5876#if defined(CONFIG_USER_ONLY)
e06fcd75 5877 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
76a66253 5878#else
76db3ba4 5879 if (unlikely(!ctx->mem_idx)) {
e06fcd75 5880 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
76a66253
JM
5881 return;
5882 }
5883 /* interpreted as no-op */
5884#endif
5885}
5886
76db3ba4 5887/* rfci (mem_idx only) */
e8eaa2c0 5888static void gen_rfci_40x(DisasContext *ctx)
a42bd6cc
JM
5889{
5890#if defined(CONFIG_USER_ONLY)
e06fcd75 5891 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
a42bd6cc 5892#else
76db3ba4 5893 if (unlikely(!ctx->mem_idx)) {
e06fcd75 5894 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
a42bd6cc
JM
5895 return;
5896 }
5897 /* Restore CPU state */
e5f17ac6 5898 gen_helper_40x_rfci(cpu_env);
e06fcd75 5899 gen_sync_exception(ctx);
a42bd6cc
JM
5900#endif
5901}
5902
99e300ef 5903static void gen_rfci(DisasContext *ctx)
a42bd6cc
JM
5904{
5905#if defined(CONFIG_USER_ONLY)
e06fcd75 5906 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
a42bd6cc 5907#else
76db3ba4 5908 if (unlikely(!ctx->mem_idx)) {
e06fcd75 5909 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
a42bd6cc
JM
5910 return;
5911 }
5912 /* Restore CPU state */
e5f17ac6 5913 gen_helper_rfci(cpu_env);
e06fcd75 5914 gen_sync_exception(ctx);
a42bd6cc
JM
5915#endif
5916}
5917
5918/* BookE specific */
99e300ef 5919
54623277 5920/* XXX: not implemented on 440 ? */
99e300ef 5921static void gen_rfdi(DisasContext *ctx)
76a66253
JM
5922{
5923#if defined(CONFIG_USER_ONLY)
e06fcd75 5924 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
76a66253 5925#else
76db3ba4 5926 if (unlikely(!ctx->mem_idx)) {
e06fcd75 5927 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
76a66253
JM
5928 return;
5929 }
5930 /* Restore CPU state */
e5f17ac6 5931 gen_helper_rfdi(cpu_env);
e06fcd75 5932 gen_sync_exception(ctx);
76a66253
JM
5933#endif
5934}
5935
2662a059 5936/* XXX: not implemented on 440 ? */
99e300ef 5937static void gen_rfmci(DisasContext *ctx)
a42bd6cc
JM
5938{
5939#if defined(CONFIG_USER_ONLY)
e06fcd75 5940 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
a42bd6cc 5941#else
76db3ba4 5942 if (unlikely(!ctx->mem_idx)) {
e06fcd75 5943 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
a42bd6cc
JM
5944 return;
5945 }
5946 /* Restore CPU state */
e5f17ac6 5947 gen_helper_rfmci(cpu_env);
e06fcd75 5948 gen_sync_exception(ctx);
a42bd6cc
JM
5949#endif
5950}
5eb7995e 5951
d9bce9d9 5952/* TLB management - PowerPC 405 implementation */
e8eaa2c0 5953
54623277 5954/* tlbre */
e8eaa2c0 5955static void gen_tlbre_40x(DisasContext *ctx)
76a66253
JM
5956{
5957#if defined(CONFIG_USER_ONLY)
e06fcd75 5958 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
76a66253 5959#else
76db3ba4 5960 if (unlikely(!ctx->mem_idx)) {
e06fcd75 5961 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
76a66253
JM
5962 return;
5963 }
5964 switch (rB(ctx->opcode)) {
5965 case 0:
c6c7cf05
BS
5966 gen_helper_4xx_tlbre_hi(cpu_gpr[rD(ctx->opcode)], cpu_env,
5967 cpu_gpr[rA(ctx->opcode)]);
76a66253
JM
5968 break;
5969 case 1:
c6c7cf05
BS
5970 gen_helper_4xx_tlbre_lo(cpu_gpr[rD(ctx->opcode)], cpu_env,
5971 cpu_gpr[rA(ctx->opcode)]);
76a66253
JM
5972 break;
5973 default:
e06fcd75 5974 gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL);
76a66253 5975 break;
9a64fbe4 5976 }
76a66253
JM
5977#endif
5978}
5979
d9bce9d9 5980/* tlbsx - tlbsx. */
e8eaa2c0 5981static void gen_tlbsx_40x(DisasContext *ctx)
76a66253
JM
5982{
5983#if defined(CONFIG_USER_ONLY)
e06fcd75 5984 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
76a66253 5985#else
74d37793 5986 TCGv t0;
76db3ba4 5987 if (unlikely(!ctx->mem_idx)) {
e06fcd75 5988 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
76a66253
JM
5989 return;
5990 }
74d37793 5991 t0 = tcg_temp_new();
76db3ba4 5992 gen_addr_reg_index(ctx, t0);
c6c7cf05 5993 gen_helper_4xx_tlbsx(cpu_gpr[rD(ctx->opcode)], cpu_env, t0);
74d37793
AJ
5994 tcg_temp_free(t0);
5995 if (Rc(ctx->opcode)) {
5996 int l1 = gen_new_label();
da91a00f 5997 tcg_gen_trunc_tl_i32(cpu_crf[0], cpu_so);
74d37793
AJ
5998 tcg_gen_brcondi_tl(TCG_COND_EQ, cpu_gpr[rD(ctx->opcode)], -1, l1);
5999 tcg_gen_ori_i32(cpu_crf[0], cpu_crf[0], 0x02);
6000 gen_set_label(l1);
6001 }
76a66253 6002#endif
79aceca5
FB
6003}
6004
76a66253 6005/* tlbwe */
e8eaa2c0 6006static void gen_tlbwe_40x(DisasContext *ctx)
79aceca5 6007{
76a66253 6008#if defined(CONFIG_USER_ONLY)
e06fcd75 6009 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
76a66253 6010#else
76db3ba4 6011 if (unlikely(!ctx->mem_idx)) {
e06fcd75 6012 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
76a66253
JM
6013 return;
6014 }
6015 switch (rB(ctx->opcode)) {
6016 case 0:
c6c7cf05
BS
6017 gen_helper_4xx_tlbwe_hi(cpu_env, cpu_gpr[rA(ctx->opcode)],
6018 cpu_gpr[rS(ctx->opcode)]);
76a66253
JM
6019 break;
6020 case 1:
c6c7cf05
BS
6021 gen_helper_4xx_tlbwe_lo(cpu_env, cpu_gpr[rA(ctx->opcode)],
6022 cpu_gpr[rS(ctx->opcode)]);
76a66253
JM
6023 break;
6024 default:
e06fcd75 6025 gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL);
76a66253 6026 break;
9a64fbe4 6027 }
76a66253
JM
6028#endif
6029}
6030
a4bb6c3e 6031/* TLB management - PowerPC 440 implementation */
e8eaa2c0 6032
54623277 6033/* tlbre */
e8eaa2c0 6034static void gen_tlbre_440(DisasContext *ctx)
5eb7995e
JM
6035{
6036#if defined(CONFIG_USER_ONLY)
e06fcd75 6037 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
5eb7995e 6038#else
76db3ba4 6039 if (unlikely(!ctx->mem_idx)) {
e06fcd75 6040 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
5eb7995e
JM
6041 return;
6042 }
6043 switch (rB(ctx->opcode)) {
6044 case 0:
5eb7995e 6045 case 1:
5eb7995e 6046 case 2:
74d37793
AJ
6047 {
6048 TCGv_i32 t0 = tcg_const_i32(rB(ctx->opcode));
c6c7cf05
BS
6049 gen_helper_440_tlbre(cpu_gpr[rD(ctx->opcode)], cpu_env,
6050 t0, cpu_gpr[rA(ctx->opcode)]);
74d37793
AJ
6051 tcg_temp_free_i32(t0);
6052 }
5eb7995e
JM
6053 break;
6054 default:
e06fcd75 6055 gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL);
5eb7995e
JM
6056 break;
6057 }
6058#endif
6059}
6060
6061/* tlbsx - tlbsx. */
e8eaa2c0 6062static void gen_tlbsx_440(DisasContext *ctx)
5eb7995e
JM
6063{
6064#if defined(CONFIG_USER_ONLY)
e06fcd75 6065 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
5eb7995e 6066#else
74d37793 6067 TCGv t0;
76db3ba4 6068 if (unlikely(!ctx->mem_idx)) {
e06fcd75 6069 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
5eb7995e
JM
6070 return;
6071 }
74d37793 6072 t0 = tcg_temp_new();
76db3ba4 6073 gen_addr_reg_index(ctx, t0);
c6c7cf05 6074 gen_helper_440_tlbsx(cpu_gpr[rD(ctx->opcode)], cpu_env, t0);
74d37793
AJ
6075 tcg_temp_free(t0);
6076 if (Rc(ctx->opcode)) {
6077 int l1 = gen_new_label();
da91a00f 6078 tcg_gen_trunc_tl_i32(cpu_crf[0], cpu_so);
74d37793
AJ
6079 tcg_gen_brcondi_tl(TCG_COND_EQ, cpu_gpr[rD(ctx->opcode)], -1, l1);
6080 tcg_gen_ori_i32(cpu_crf[0], cpu_crf[0], 0x02);
6081 gen_set_label(l1);
6082 }
5eb7995e
JM
6083#endif
6084}
6085
6086/* tlbwe */
e8eaa2c0 6087static void gen_tlbwe_440(DisasContext *ctx)
5eb7995e
JM
6088{
6089#if defined(CONFIG_USER_ONLY)
e06fcd75 6090 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
5eb7995e 6091#else
76db3ba4 6092 if (unlikely(!ctx->mem_idx)) {
e06fcd75 6093 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
5eb7995e
JM
6094 return;
6095 }
6096 switch (rB(ctx->opcode)) {
6097 case 0:
5eb7995e 6098 case 1:
5eb7995e 6099 case 2:
74d37793
AJ
6100 {
6101 TCGv_i32 t0 = tcg_const_i32(rB(ctx->opcode));
c6c7cf05
BS
6102 gen_helper_440_tlbwe(cpu_env, t0, cpu_gpr[rA(ctx->opcode)],
6103 cpu_gpr[rS(ctx->opcode)]);
74d37793
AJ
6104 tcg_temp_free_i32(t0);
6105 }
5eb7995e
JM
6106 break;
6107 default:
e06fcd75 6108 gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL);
5eb7995e
JM
6109 break;
6110 }
6111#endif
6112}
6113
01662f3e
AG
6114/* TLB management - PowerPC BookE 2.06 implementation */
6115
6116/* tlbre */
6117static void gen_tlbre_booke206(DisasContext *ctx)
6118{
6119#if defined(CONFIG_USER_ONLY)
6120 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
6121#else
6122 if (unlikely(!ctx->mem_idx)) {
6123 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
6124 return;
6125 }
6126
c6c7cf05 6127 gen_helper_booke206_tlbre(cpu_env);
01662f3e
AG
6128#endif
6129}
6130
6131/* tlbsx - tlbsx. */
6132static void gen_tlbsx_booke206(DisasContext *ctx)
6133{
6134#if defined(CONFIG_USER_ONLY)
6135 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
6136#else
6137 TCGv t0;
6138 if (unlikely(!ctx->mem_idx)) {
6139 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
6140 return;
6141 }
6142
6143 if (rA(ctx->opcode)) {
6144 t0 = tcg_temp_new();
6145 tcg_gen_mov_tl(t0, cpu_gpr[rD(ctx->opcode)]);
6146 } else {
6147 t0 = tcg_const_tl(0);
6148 }
6149
6150 tcg_gen_add_tl(t0, t0, cpu_gpr[rB(ctx->opcode)]);
c6c7cf05 6151 gen_helper_booke206_tlbsx(cpu_env, t0);
01662f3e
AG
6152#endif
6153}
6154
6155/* tlbwe */
6156static void gen_tlbwe_booke206(DisasContext *ctx)
6157{
6158#if defined(CONFIG_USER_ONLY)
6159 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
6160#else
6161 if (unlikely(!ctx->mem_idx)) {
6162 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
6163 return;
6164 }
3f162d11 6165 gen_update_nip(ctx, ctx->nip - 4);
c6c7cf05 6166 gen_helper_booke206_tlbwe(cpu_env);
01662f3e
AG
6167#endif
6168}
6169
6170static void gen_tlbivax_booke206(DisasContext *ctx)
6171{
6172#if defined(CONFIG_USER_ONLY)
6173 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
6174#else
6175 TCGv t0;
6176 if (unlikely(!ctx->mem_idx)) {
6177 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
6178 return;
6179 }
6180
6181 t0 = tcg_temp_new();
6182 gen_addr_reg_index(ctx, t0);
6183
c6c7cf05 6184 gen_helper_booke206_tlbivax(cpu_env, t0);
01662f3e
AG
6185#endif
6186}
6187
6d3db821
AG
6188static void gen_tlbilx_booke206(DisasContext *ctx)
6189{
6190#if defined(CONFIG_USER_ONLY)
6191 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
6192#else
6193 TCGv t0;
6194 if (unlikely(!ctx->mem_idx)) {
6195 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
6196 return;
6197 }
6198
6199 t0 = tcg_temp_new();
6200 gen_addr_reg_index(ctx, t0);
6201
6202 switch((ctx->opcode >> 21) & 0x3) {
6203 case 0:
c6c7cf05 6204 gen_helper_booke206_tlbilx0(cpu_env, t0);
6d3db821
AG
6205 break;
6206 case 1:
c6c7cf05 6207 gen_helper_booke206_tlbilx1(cpu_env, t0);
6d3db821
AG
6208 break;
6209 case 3:
c6c7cf05 6210 gen_helper_booke206_tlbilx3(cpu_env, t0);
6d3db821
AG
6211 break;
6212 default:
6213 gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL);
6214 break;
6215 }
6216
6217 tcg_temp_free(t0);
6218#endif
6219}
6220
01662f3e 6221
76a66253 6222/* wrtee */
99e300ef 6223static void gen_wrtee(DisasContext *ctx)
76a66253
JM
6224{
6225#if defined(CONFIG_USER_ONLY)
e06fcd75 6226 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
76a66253 6227#else
6527f6ea 6228 TCGv t0;
76db3ba4 6229 if (unlikely(!ctx->mem_idx)) {
e06fcd75 6230 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
76a66253
JM
6231 return;
6232 }
6527f6ea
AJ
6233 t0 = tcg_temp_new();
6234 tcg_gen_andi_tl(t0, cpu_gpr[rD(ctx->opcode)], (1 << MSR_EE));
6235 tcg_gen_andi_tl(cpu_msr, cpu_msr, ~(1 << MSR_EE));
6236 tcg_gen_or_tl(cpu_msr, cpu_msr, t0);
6237 tcg_temp_free(t0);
dee96f6c
JM
6238 /* Stop translation to have a chance to raise an exception
6239 * if we just set msr_ee to 1
6240 */
e06fcd75 6241 gen_stop_exception(ctx);
76a66253
JM
6242#endif
6243}
6244
6245/* wrteei */
99e300ef 6246static void gen_wrteei(DisasContext *ctx)
76a66253
JM
6247{
6248#if defined(CONFIG_USER_ONLY)
e06fcd75 6249 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
76a66253 6250#else
76db3ba4 6251 if (unlikely(!ctx->mem_idx)) {
e06fcd75 6252 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
76a66253
JM
6253 return;
6254 }
fbe73008 6255 if (ctx->opcode & 0x00008000) {
6527f6ea
AJ
6256 tcg_gen_ori_tl(cpu_msr, cpu_msr, (1 << MSR_EE));
6257 /* Stop translation to have a chance to raise an exception */
e06fcd75 6258 gen_stop_exception(ctx);
6527f6ea 6259 } else {
1b6e5f99 6260 tcg_gen_andi_tl(cpu_msr, cpu_msr, ~(1 << MSR_EE));
6527f6ea 6261 }
76a66253
JM
6262#endif
6263}
6264
08e46e54 6265/* PowerPC 440 specific instructions */
99e300ef 6266
54623277 6267/* dlmzb */
99e300ef 6268static void gen_dlmzb(DisasContext *ctx)
76a66253 6269{
ef0d51af 6270 TCGv_i32 t0 = tcg_const_i32(Rc(ctx->opcode));
d15f74fb
BS
6271 gen_helper_dlmzb(cpu_gpr[rA(ctx->opcode)], cpu_env,
6272 cpu_gpr[rS(ctx->opcode)], cpu_gpr[rB(ctx->opcode)], t0);
ef0d51af 6273 tcg_temp_free_i32(t0);
76a66253
JM
6274}
6275
6276/* mbar replaces eieio on 440 */
99e300ef 6277static void gen_mbar(DisasContext *ctx)
76a66253
JM
6278{
6279 /* interpreted as no-op */
6280}
6281
6282/* msync replaces sync on 440 */
dcb2b9e1 6283static void gen_msync_4xx(DisasContext *ctx)
76a66253
JM
6284{
6285 /* interpreted as no-op */
6286}
6287
6288/* icbt */
e8eaa2c0 6289static void gen_icbt_440(DisasContext *ctx)
76a66253
JM
6290{
6291 /* interpreted as no-op */
6292 /* XXX: specification say this is treated as a load by the MMU
6293 * but does not generate any exception
6294 */
79aceca5
FB
6295}
6296
9e0b5cb1
AG
6297/* Embedded.Processor Control */
6298
6299static void gen_msgclr(DisasContext *ctx)
6300{
6301#if defined(CONFIG_USER_ONLY)
6302 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
6303#else
6304 if (unlikely(ctx->mem_idx == 0)) {
6305 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
6306 return;
6307 }
6308
e5f17ac6 6309 gen_helper_msgclr(cpu_env, cpu_gpr[rB(ctx->opcode)]);
9e0b5cb1
AG
6310#endif
6311}
6312
d5d11a39
AG
6313static void gen_msgsnd(DisasContext *ctx)
6314{
6315#if defined(CONFIG_USER_ONLY)
6316 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
6317#else
6318 if (unlikely(ctx->mem_idx == 0)) {
6319 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
6320 return;
6321 }
6322
6323 gen_helper_msgsnd(cpu_gpr[rB(ctx->opcode)]);
6324#endif
6325}
6326
a9d9eb8f
JM
6327/*** Altivec vector extension ***/
6328/* Altivec registers moves */
a9d9eb8f 6329
636aa200 6330static inline TCGv_ptr gen_avr_ptr(int reg)
564e571a 6331{
e4704b3b 6332 TCGv_ptr r = tcg_temp_new_ptr();
564e571a
AJ
6333 tcg_gen_addi_ptr(r, cpu_env, offsetof(CPUPPCState, avr[reg]));
6334 return r;
6335}
6336
a9d9eb8f 6337#define GEN_VR_LDX(name, opc2, opc3) \
99e300ef 6338static void glue(gen_, name)(DisasContext *ctx) \
a9d9eb8f 6339{ \
fe1e5c53 6340 TCGv EA; \
a9d9eb8f 6341 if (unlikely(!ctx->altivec_enabled)) { \
e06fcd75 6342 gen_exception(ctx, POWERPC_EXCP_VPU); \
a9d9eb8f
JM
6343 return; \
6344 } \
76db3ba4 6345 gen_set_access_type(ctx, ACCESS_INT); \
fe1e5c53 6346 EA = tcg_temp_new(); \
76db3ba4 6347 gen_addr_reg_index(ctx, EA); \
fe1e5c53 6348 tcg_gen_andi_tl(EA, EA, ~0xf); \
76db3ba4
AJ
6349 if (ctx->le_mode) { \
6350 gen_qemu_ld64(ctx, cpu_avrl[rD(ctx->opcode)], EA); \
fe1e5c53 6351 tcg_gen_addi_tl(EA, EA, 8); \
76db3ba4 6352 gen_qemu_ld64(ctx, cpu_avrh[rD(ctx->opcode)], EA); \
fe1e5c53 6353 } else { \
76db3ba4 6354 gen_qemu_ld64(ctx, cpu_avrh[rD(ctx->opcode)], EA); \
fe1e5c53 6355 tcg_gen_addi_tl(EA, EA, 8); \
76db3ba4 6356 gen_qemu_ld64(ctx, cpu_avrl[rD(ctx->opcode)], EA); \
fe1e5c53
AJ
6357 } \
6358 tcg_temp_free(EA); \
a9d9eb8f
JM
6359}
6360
6361#define GEN_VR_STX(name, opc2, opc3) \
99e300ef 6362static void gen_st##name(DisasContext *ctx) \
a9d9eb8f 6363{ \
fe1e5c53 6364 TCGv EA; \
a9d9eb8f 6365 if (unlikely(!ctx->altivec_enabled)) { \
e06fcd75 6366 gen_exception(ctx, POWERPC_EXCP_VPU); \
a9d9eb8f
JM
6367 return; \
6368 } \
76db3ba4 6369 gen_set_access_type(ctx, ACCESS_INT); \
fe1e5c53 6370 EA = tcg_temp_new(); \
76db3ba4 6371 gen_addr_reg_index(ctx, EA); \
fe1e5c53 6372 tcg_gen_andi_tl(EA, EA, ~0xf); \
76db3ba4
AJ
6373 if (ctx->le_mode) { \
6374 gen_qemu_st64(ctx, cpu_avrl[rD(ctx->opcode)], EA); \
fe1e5c53 6375 tcg_gen_addi_tl(EA, EA, 8); \
76db3ba4 6376 gen_qemu_st64(ctx, cpu_avrh[rD(ctx->opcode)], EA); \
fe1e5c53 6377 } else { \
76db3ba4 6378 gen_qemu_st64(ctx, cpu_avrh[rD(ctx->opcode)], EA); \
fe1e5c53 6379 tcg_gen_addi_tl(EA, EA, 8); \
76db3ba4 6380 gen_qemu_st64(ctx, cpu_avrl[rD(ctx->opcode)], EA); \
fe1e5c53
AJ
6381 } \
6382 tcg_temp_free(EA); \
a9d9eb8f
JM
6383}
6384
cbfb6ae9 6385#define GEN_VR_LVE(name, opc2, opc3) \
99e300ef 6386static void gen_lve##name(DisasContext *ctx) \
cbfb6ae9
AJ
6387 { \
6388 TCGv EA; \
6389 TCGv_ptr rs; \
6390 if (unlikely(!ctx->altivec_enabled)) { \
6391 gen_exception(ctx, POWERPC_EXCP_VPU); \
6392 return; \
6393 } \
6394 gen_set_access_type(ctx, ACCESS_INT); \
6395 EA = tcg_temp_new(); \
6396 gen_addr_reg_index(ctx, EA); \
6397 rs = gen_avr_ptr(rS(ctx->opcode)); \
2f5a189c 6398 gen_helper_lve##name(cpu_env, rs, EA); \
cbfb6ae9
AJ
6399 tcg_temp_free(EA); \
6400 tcg_temp_free_ptr(rs); \
6401 }
6402
6403#define GEN_VR_STVE(name, opc2, opc3) \
99e300ef 6404static void gen_stve##name(DisasContext *ctx) \
cbfb6ae9
AJ
6405 { \
6406 TCGv EA; \
6407 TCGv_ptr rs; \
6408 if (unlikely(!ctx->altivec_enabled)) { \
6409 gen_exception(ctx, POWERPC_EXCP_VPU); \
6410 return; \
6411 } \
6412 gen_set_access_type(ctx, ACCESS_INT); \
6413 EA = tcg_temp_new(); \
6414 gen_addr_reg_index(ctx, EA); \
6415 rs = gen_avr_ptr(rS(ctx->opcode)); \
2f5a189c 6416 gen_helper_stve##name(cpu_env, rs, EA); \
cbfb6ae9
AJ
6417 tcg_temp_free(EA); \
6418 tcg_temp_free_ptr(rs); \
6419 }
6420
fe1e5c53 6421GEN_VR_LDX(lvx, 0x07, 0x03);
a9d9eb8f 6422/* As we don't emulate the cache, lvxl is stricly equivalent to lvx */
fe1e5c53 6423GEN_VR_LDX(lvxl, 0x07, 0x0B);
a9d9eb8f 6424
cbfb6ae9
AJ
6425GEN_VR_LVE(bx, 0x07, 0x00);
6426GEN_VR_LVE(hx, 0x07, 0x01);
6427GEN_VR_LVE(wx, 0x07, 0x02);
6428
fe1e5c53 6429GEN_VR_STX(svx, 0x07, 0x07);
a9d9eb8f 6430/* As we don't emulate the cache, stvxl is stricly equivalent to stvx */
fe1e5c53 6431GEN_VR_STX(svxl, 0x07, 0x0F);
a9d9eb8f 6432
cbfb6ae9
AJ
6433GEN_VR_STVE(bx, 0x07, 0x04);
6434GEN_VR_STVE(hx, 0x07, 0x05);
6435GEN_VR_STVE(wx, 0x07, 0x06);
6436
99e300ef 6437static void gen_lvsl(DisasContext *ctx)
bf8d8ded
AJ
6438{
6439 TCGv_ptr rd;
6440 TCGv EA;
6441 if (unlikely(!ctx->altivec_enabled)) {
6442 gen_exception(ctx, POWERPC_EXCP_VPU);
6443 return;
6444 }
6445 EA = tcg_temp_new();
6446 gen_addr_reg_index(ctx, EA);
6447 rd = gen_avr_ptr(rD(ctx->opcode));
6448 gen_helper_lvsl(rd, EA);
6449 tcg_temp_free(EA);
6450 tcg_temp_free_ptr(rd);
6451}
6452
99e300ef 6453static void gen_lvsr(DisasContext *ctx)
bf8d8ded
AJ
6454{
6455 TCGv_ptr rd;
6456 TCGv EA;
6457 if (unlikely(!ctx->altivec_enabled)) {
6458 gen_exception(ctx, POWERPC_EXCP_VPU);
6459 return;
6460 }
6461 EA = tcg_temp_new();
6462 gen_addr_reg_index(ctx, EA);
6463 rd = gen_avr_ptr(rD(ctx->opcode));
6464 gen_helper_lvsr(rd, EA);
6465 tcg_temp_free(EA);
6466 tcg_temp_free_ptr(rd);
6467}
6468
99e300ef 6469static void gen_mfvscr(DisasContext *ctx)
785f451b
AJ
6470{
6471 TCGv_i32 t;
6472 if (unlikely(!ctx->altivec_enabled)) {
6473 gen_exception(ctx, POWERPC_EXCP_VPU);
6474 return;
6475 }
6476 tcg_gen_movi_i64(cpu_avrh[rD(ctx->opcode)], 0);
6477 t = tcg_temp_new_i32();
1328c2bf 6478 tcg_gen_ld_i32(t, cpu_env, offsetof(CPUPPCState, vscr));
785f451b 6479 tcg_gen_extu_i32_i64(cpu_avrl[rD(ctx->opcode)], t);
fce5ecb7 6480 tcg_temp_free_i32(t);
785f451b
AJ
6481}
6482
99e300ef 6483static void gen_mtvscr(DisasContext *ctx)
785f451b 6484{
6e87b7c7 6485 TCGv_ptr p;
785f451b
AJ
6486 if (unlikely(!ctx->altivec_enabled)) {
6487 gen_exception(ctx, POWERPC_EXCP_VPU);
6488 return;
6489 }
6e87b7c7 6490 p = gen_avr_ptr(rD(ctx->opcode));
d15f74fb 6491 gen_helper_mtvscr(cpu_env, p);
6e87b7c7 6492 tcg_temp_free_ptr(p);
785f451b
AJ
6493}
6494
7a9b96cf
AJ
6495/* Logical operations */
6496#define GEN_VX_LOGICAL(name, tcg_op, opc2, opc3) \
99e300ef 6497static void glue(gen_, name)(DisasContext *ctx) \
7a9b96cf
AJ
6498{ \
6499 if (unlikely(!ctx->altivec_enabled)) { \
6500 gen_exception(ctx, POWERPC_EXCP_VPU); \
6501 return; \
6502 } \
6503 tcg_op(cpu_avrh[rD(ctx->opcode)], cpu_avrh[rA(ctx->opcode)], cpu_avrh[rB(ctx->opcode)]); \
6504 tcg_op(cpu_avrl[rD(ctx->opcode)], cpu_avrl[rA(ctx->opcode)], cpu_avrl[rB(ctx->opcode)]); \
6505}
6506
6507GEN_VX_LOGICAL(vand, tcg_gen_and_i64, 2, 16);
6508GEN_VX_LOGICAL(vandc, tcg_gen_andc_i64, 2, 17);
6509GEN_VX_LOGICAL(vor, tcg_gen_or_i64, 2, 18);
6510GEN_VX_LOGICAL(vxor, tcg_gen_xor_i64, 2, 19);
6511GEN_VX_LOGICAL(vnor, tcg_gen_nor_i64, 2, 20);
6512
8e27dd6f 6513#define GEN_VXFORM(name, opc2, opc3) \
99e300ef 6514static void glue(gen_, name)(DisasContext *ctx) \
8e27dd6f
AJ
6515{ \
6516 TCGv_ptr ra, rb, rd; \
6517 if (unlikely(!ctx->altivec_enabled)) { \
6518 gen_exception(ctx, POWERPC_EXCP_VPU); \
6519 return; \
6520 } \
6521 ra = gen_avr_ptr(rA(ctx->opcode)); \
6522 rb = gen_avr_ptr(rB(ctx->opcode)); \
6523 rd = gen_avr_ptr(rD(ctx->opcode)); \
6524 gen_helper_##name (rd, ra, rb); \
6525 tcg_temp_free_ptr(ra); \
6526 tcg_temp_free_ptr(rb); \
6527 tcg_temp_free_ptr(rd); \
6528}
6529
d15f74fb
BS
6530#define GEN_VXFORM_ENV(name, opc2, opc3) \
6531static void glue(gen_, name)(DisasContext *ctx) \
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)); \
54cddd21 6541 gen_helper_##name(cpu_env, rd, ra, rb); \
d15f74fb
BS
6542 tcg_temp_free_ptr(ra); \
6543 tcg_temp_free_ptr(rb); \
6544 tcg_temp_free_ptr(rd); \
6545}
6546
7872c51c
AJ
6547GEN_VXFORM(vaddubm, 0, 0);
6548GEN_VXFORM(vadduhm, 0, 1);
6549GEN_VXFORM(vadduwm, 0, 2);
6550GEN_VXFORM(vsububm, 0, 16);
6551GEN_VXFORM(vsubuhm, 0, 17);
6552GEN_VXFORM(vsubuwm, 0, 18);
e4039339
AJ
6553GEN_VXFORM(vmaxub, 1, 0);
6554GEN_VXFORM(vmaxuh, 1, 1);
6555GEN_VXFORM(vmaxuw, 1, 2);
6556GEN_VXFORM(vmaxsb, 1, 4);
6557GEN_VXFORM(vmaxsh, 1, 5);
6558GEN_VXFORM(vmaxsw, 1, 6);
6559GEN_VXFORM(vminub, 1, 8);
6560GEN_VXFORM(vminuh, 1, 9);
6561GEN_VXFORM(vminuw, 1, 10);
6562GEN_VXFORM(vminsb, 1, 12);
6563GEN_VXFORM(vminsh, 1, 13);
6564GEN_VXFORM(vminsw, 1, 14);
fab3cbe9
AJ
6565GEN_VXFORM(vavgub, 1, 16);
6566GEN_VXFORM(vavguh, 1, 17);
6567GEN_VXFORM(vavguw, 1, 18);
6568GEN_VXFORM(vavgsb, 1, 20);
6569GEN_VXFORM(vavgsh, 1, 21);
6570GEN_VXFORM(vavgsw, 1, 22);
3b430048
AJ
6571GEN_VXFORM(vmrghb, 6, 0);
6572GEN_VXFORM(vmrghh, 6, 1);
6573GEN_VXFORM(vmrghw, 6, 2);
6574GEN_VXFORM(vmrglb, 6, 4);
6575GEN_VXFORM(vmrglh, 6, 5);
6576GEN_VXFORM(vmrglw, 6, 6);
2c277908
AJ
6577GEN_VXFORM(vmuloub, 4, 0);
6578GEN_VXFORM(vmulouh, 4, 1);
6579GEN_VXFORM(vmulosb, 4, 4);
6580GEN_VXFORM(vmulosh, 4, 5);
6581GEN_VXFORM(vmuleub, 4, 8);
6582GEN_VXFORM(vmuleuh, 4, 9);
6583GEN_VXFORM(vmulesb, 4, 12);
6584GEN_VXFORM(vmulesh, 4, 13);
d79f0809
AJ
6585GEN_VXFORM(vslb, 2, 4);
6586GEN_VXFORM(vslh, 2, 5);
6587GEN_VXFORM(vslw, 2, 6);
07ef34c3
AJ
6588GEN_VXFORM(vsrb, 2, 8);
6589GEN_VXFORM(vsrh, 2, 9);
6590GEN_VXFORM(vsrw, 2, 10);
6591GEN_VXFORM(vsrab, 2, 12);
6592GEN_VXFORM(vsrah, 2, 13);
6593GEN_VXFORM(vsraw, 2, 14);
7b239bec
AJ
6594GEN_VXFORM(vslo, 6, 16);
6595GEN_VXFORM(vsro, 6, 17);
e343da72
AJ
6596GEN_VXFORM(vaddcuw, 0, 6);
6597GEN_VXFORM(vsubcuw, 0, 22);
d15f74fb
BS
6598GEN_VXFORM_ENV(vaddubs, 0, 8);
6599GEN_VXFORM_ENV(vadduhs, 0, 9);
6600GEN_VXFORM_ENV(vadduws, 0, 10);
6601GEN_VXFORM_ENV(vaddsbs, 0, 12);
6602GEN_VXFORM_ENV(vaddshs, 0, 13);
6603GEN_VXFORM_ENV(vaddsws, 0, 14);
6604GEN_VXFORM_ENV(vsububs, 0, 24);
6605GEN_VXFORM_ENV(vsubuhs, 0, 25);
6606GEN_VXFORM_ENV(vsubuws, 0, 26);
6607GEN_VXFORM_ENV(vsubsbs, 0, 28);
6608GEN_VXFORM_ENV(vsubshs, 0, 29);
6609GEN_VXFORM_ENV(vsubsws, 0, 30);
5e1d0985
AJ
6610GEN_VXFORM(vrlb, 2, 0);
6611GEN_VXFORM(vrlh, 2, 1);
6612GEN_VXFORM(vrlw, 2, 2);
d9430add
AJ
6613GEN_VXFORM(vsl, 2, 7);
6614GEN_VXFORM(vsr, 2, 11);
d15f74fb
BS
6615GEN_VXFORM_ENV(vpkuhum, 7, 0);
6616GEN_VXFORM_ENV(vpkuwum, 7, 1);
6617GEN_VXFORM_ENV(vpkuhus, 7, 2);
6618GEN_VXFORM_ENV(vpkuwus, 7, 3);
6619GEN_VXFORM_ENV(vpkshus, 7, 4);
6620GEN_VXFORM_ENV(vpkswus, 7, 5);
6621GEN_VXFORM_ENV(vpkshss, 7, 6);
6622GEN_VXFORM_ENV(vpkswss, 7, 7);
1dd9ffb9 6623GEN_VXFORM(vpkpx, 7, 12);
d15f74fb
BS
6624GEN_VXFORM_ENV(vsum4ubs, 4, 24);
6625GEN_VXFORM_ENV(vsum4sbs, 4, 28);
6626GEN_VXFORM_ENV(vsum4shs, 4, 25);
6627GEN_VXFORM_ENV(vsum2sws, 4, 26);
6628GEN_VXFORM_ENV(vsumsws, 4, 30);
6629GEN_VXFORM_ENV(vaddfp, 5, 0);
6630GEN_VXFORM_ENV(vsubfp, 5, 1);
6631GEN_VXFORM_ENV(vmaxfp, 5, 16);
6632GEN_VXFORM_ENV(vminfp, 5, 17);
fab3cbe9 6633
0cbcd906 6634#define GEN_VXRFORM1(opname, name, str, opc2, opc3) \
e8eaa2c0 6635static void glue(gen_, name)(DisasContext *ctx) \
0cbcd906
AJ
6636 { \
6637 TCGv_ptr ra, rb, rd; \
6638 if (unlikely(!ctx->altivec_enabled)) { \
6639 gen_exception(ctx, POWERPC_EXCP_VPU); \
6640 return; \
6641 } \
6642 ra = gen_avr_ptr(rA(ctx->opcode)); \
6643 rb = gen_avr_ptr(rB(ctx->opcode)); \
6644 rd = gen_avr_ptr(rD(ctx->opcode)); \
d15f74fb 6645 gen_helper_##opname(cpu_env, rd, ra, rb); \
0cbcd906
AJ
6646 tcg_temp_free_ptr(ra); \
6647 tcg_temp_free_ptr(rb); \
6648 tcg_temp_free_ptr(rd); \
6649 }
6650
6651#define GEN_VXRFORM(name, opc2, opc3) \
6652 GEN_VXRFORM1(name, name, #name, opc2, opc3) \
6653 GEN_VXRFORM1(name##_dot, name##_, #name ".", opc2, (opc3 | (0x1 << 4)))
6654
1add6e23
AJ
6655GEN_VXRFORM(vcmpequb, 3, 0)
6656GEN_VXRFORM(vcmpequh, 3, 1)
6657GEN_VXRFORM(vcmpequw, 3, 2)
6658GEN_VXRFORM(vcmpgtsb, 3, 12)
6659GEN_VXRFORM(vcmpgtsh, 3, 13)
6660GEN_VXRFORM(vcmpgtsw, 3, 14)
6661GEN_VXRFORM(vcmpgtub, 3, 8)
6662GEN_VXRFORM(vcmpgtuh, 3, 9)
6663GEN_VXRFORM(vcmpgtuw, 3, 10)
819ca121
AJ
6664GEN_VXRFORM(vcmpeqfp, 3, 3)
6665GEN_VXRFORM(vcmpgefp, 3, 7)
6666GEN_VXRFORM(vcmpgtfp, 3, 11)
6667GEN_VXRFORM(vcmpbfp, 3, 15)
1add6e23 6668
c026766b 6669#define GEN_VXFORM_SIMM(name, opc2, opc3) \
99e300ef 6670static void glue(gen_, name)(DisasContext *ctx) \
c026766b
AJ
6671 { \
6672 TCGv_ptr rd; \
6673 TCGv_i32 simm; \
6674 if (unlikely(!ctx->altivec_enabled)) { \
6675 gen_exception(ctx, POWERPC_EXCP_VPU); \
6676 return; \
6677 } \
6678 simm = tcg_const_i32(SIMM5(ctx->opcode)); \
6679 rd = gen_avr_ptr(rD(ctx->opcode)); \
6680 gen_helper_##name (rd, simm); \
6681 tcg_temp_free_i32(simm); \
6682 tcg_temp_free_ptr(rd); \
6683 }
6684
6685GEN_VXFORM_SIMM(vspltisb, 6, 12);
6686GEN_VXFORM_SIMM(vspltish, 6, 13);
6687GEN_VXFORM_SIMM(vspltisw, 6, 14);
6688
de5f2484 6689#define GEN_VXFORM_NOA(name, opc2, opc3) \
99e300ef 6690static void glue(gen_, name)(DisasContext *ctx) \
de5f2484
AJ
6691 { \
6692 TCGv_ptr rb, rd; \
6693 if (unlikely(!ctx->altivec_enabled)) { \
6694 gen_exception(ctx, POWERPC_EXCP_VPU); \
6695 return; \
6696 } \
6697 rb = gen_avr_ptr(rB(ctx->opcode)); \
6698 rd = gen_avr_ptr(rD(ctx->opcode)); \
6699 gen_helper_##name (rd, rb); \
6700 tcg_temp_free_ptr(rb); \
6701 tcg_temp_free_ptr(rd); \
6702 }
6703
d15f74fb
BS
6704#define GEN_VXFORM_NOA_ENV(name, opc2, opc3) \
6705static void glue(gen_, name)(DisasContext *ctx) \
6706 { \
6707 TCGv_ptr rb, rd; \
6708 \
6709 if (unlikely(!ctx->altivec_enabled)) { \
6710 gen_exception(ctx, POWERPC_EXCP_VPU); \
6711 return; \
6712 } \
6713 rb = gen_avr_ptr(rB(ctx->opcode)); \
6714 rd = gen_avr_ptr(rD(ctx->opcode)); \
6715 gen_helper_##name(cpu_env, rd, rb); \
6716 tcg_temp_free_ptr(rb); \
6717 tcg_temp_free_ptr(rd); \
6718 }
6719
6cf1c6e5
AJ
6720GEN_VXFORM_NOA(vupkhsb, 7, 8);
6721GEN_VXFORM_NOA(vupkhsh, 7, 9);
6722GEN_VXFORM_NOA(vupklsb, 7, 10);
6723GEN_VXFORM_NOA(vupklsh, 7, 11);
79f85c3a
AJ
6724GEN_VXFORM_NOA(vupkhpx, 7, 13);
6725GEN_VXFORM_NOA(vupklpx, 7, 15);
d15f74fb
BS
6726GEN_VXFORM_NOA_ENV(vrefp, 5, 4);
6727GEN_VXFORM_NOA_ENV(vrsqrtefp, 5, 5);
6728GEN_VXFORM_NOA_ENV(vexptefp, 5, 6);
6729GEN_VXFORM_NOA_ENV(vlogefp, 5, 7);
6730GEN_VXFORM_NOA_ENV(vrfim, 5, 8);
6731GEN_VXFORM_NOA_ENV(vrfin, 5, 9);
6732GEN_VXFORM_NOA_ENV(vrfip, 5, 10);
6733GEN_VXFORM_NOA_ENV(vrfiz, 5, 11);
79f85c3a 6734
21d21583 6735#define GEN_VXFORM_SIMM(name, opc2, opc3) \
99e300ef 6736static void glue(gen_, name)(DisasContext *ctx) \
21d21583
AJ
6737 { \
6738 TCGv_ptr rd; \
6739 TCGv_i32 simm; \
6740 if (unlikely(!ctx->altivec_enabled)) { \
6741 gen_exception(ctx, POWERPC_EXCP_VPU); \
6742 return; \
6743 } \
6744 simm = tcg_const_i32(SIMM5(ctx->opcode)); \
6745 rd = gen_avr_ptr(rD(ctx->opcode)); \
6746 gen_helper_##name (rd, simm); \
6747 tcg_temp_free_i32(simm); \
6748 tcg_temp_free_ptr(rd); \
6749 }
6750
27a4edb3 6751#define GEN_VXFORM_UIMM(name, opc2, opc3) \
99e300ef 6752static void glue(gen_, name)(DisasContext *ctx) \
27a4edb3
AJ
6753 { \
6754 TCGv_ptr rb, rd; \
6755 TCGv_i32 uimm; \
6756 if (unlikely(!ctx->altivec_enabled)) { \
6757 gen_exception(ctx, POWERPC_EXCP_VPU); \
6758 return; \
6759 } \
6760 uimm = tcg_const_i32(UIMM5(ctx->opcode)); \
6761 rb = gen_avr_ptr(rB(ctx->opcode)); \
6762 rd = gen_avr_ptr(rD(ctx->opcode)); \
6763 gen_helper_##name (rd, rb, uimm); \
6764 tcg_temp_free_i32(uimm); \
6765 tcg_temp_free_ptr(rb); \
6766 tcg_temp_free_ptr(rd); \
6767 }
6768
d15f74fb
BS
6769#define GEN_VXFORM_UIMM_ENV(name, opc2, opc3) \
6770static void glue(gen_, name)(DisasContext *ctx) \
6771 { \
6772 TCGv_ptr rb, rd; \
6773 TCGv_i32 uimm; \
6774 \
6775 if (unlikely(!ctx->altivec_enabled)) { \
6776 gen_exception(ctx, POWERPC_EXCP_VPU); \
6777 return; \
6778 } \
6779 uimm = tcg_const_i32(UIMM5(ctx->opcode)); \
6780 rb = gen_avr_ptr(rB(ctx->opcode)); \
6781 rd = gen_avr_ptr(rD(ctx->opcode)); \
6782 gen_helper_##name(cpu_env, rd, rb, uimm); \
6783 tcg_temp_free_i32(uimm); \
6784 tcg_temp_free_ptr(rb); \
6785 tcg_temp_free_ptr(rd); \
6786 }
6787
e4e6bee7
AJ
6788GEN_VXFORM_UIMM(vspltb, 6, 8);
6789GEN_VXFORM_UIMM(vsplth, 6, 9);
6790GEN_VXFORM_UIMM(vspltw, 6, 10);
d15f74fb
BS
6791GEN_VXFORM_UIMM_ENV(vcfux, 5, 12);
6792GEN_VXFORM_UIMM_ENV(vcfsx, 5, 13);
6793GEN_VXFORM_UIMM_ENV(vctuxs, 5, 14);
6794GEN_VXFORM_UIMM_ENV(vctsxs, 5, 15);
e4e6bee7 6795
99e300ef 6796static void gen_vsldoi(DisasContext *ctx)
cd633b10
AJ
6797{
6798 TCGv_ptr ra, rb, rd;
fce5ecb7 6799 TCGv_i32 sh;
cd633b10
AJ
6800 if (unlikely(!ctx->altivec_enabled)) {
6801 gen_exception(ctx, POWERPC_EXCP_VPU);
6802 return;
6803 }
6804 ra = gen_avr_ptr(rA(ctx->opcode));
6805 rb = gen_avr_ptr(rB(ctx->opcode));
6806 rd = gen_avr_ptr(rD(ctx->opcode));
6807 sh = tcg_const_i32(VSH(ctx->opcode));
6808 gen_helper_vsldoi (rd, ra, rb, sh);
6809 tcg_temp_free_ptr(ra);
6810 tcg_temp_free_ptr(rb);
6811 tcg_temp_free_ptr(rd);
fce5ecb7 6812 tcg_temp_free_i32(sh);
cd633b10
AJ
6813}
6814
707cec33 6815#define GEN_VAFORM_PAIRED(name0, name1, opc2) \
d15f74fb 6816static void glue(gen_, name0##_##name1)(DisasContext *ctx) \
707cec33
AJ
6817 { \
6818 TCGv_ptr ra, rb, rc, rd; \
6819 if (unlikely(!ctx->altivec_enabled)) { \
6820 gen_exception(ctx, POWERPC_EXCP_VPU); \
6821 return; \
6822 } \
6823 ra = gen_avr_ptr(rA(ctx->opcode)); \
6824 rb = gen_avr_ptr(rB(ctx->opcode)); \
6825 rc = gen_avr_ptr(rC(ctx->opcode)); \
6826 rd = gen_avr_ptr(rD(ctx->opcode)); \
6827 if (Rc(ctx->opcode)) { \
d15f74fb 6828 gen_helper_##name1(cpu_env, rd, ra, rb, rc); \
707cec33 6829 } else { \
d15f74fb 6830 gen_helper_##name0(cpu_env, rd, ra, rb, rc); \
707cec33
AJ
6831 } \
6832 tcg_temp_free_ptr(ra); \
6833 tcg_temp_free_ptr(rb); \
6834 tcg_temp_free_ptr(rc); \
6835 tcg_temp_free_ptr(rd); \
6836 }
6837
b161ae27
AJ
6838GEN_VAFORM_PAIRED(vmhaddshs, vmhraddshs, 16)
6839
99e300ef 6840static void gen_vmladduhm(DisasContext *ctx)
bcd2ee23
AJ
6841{
6842 TCGv_ptr ra, rb, rc, rd;
6843 if (unlikely(!ctx->altivec_enabled)) {
6844 gen_exception(ctx, POWERPC_EXCP_VPU);
6845 return;
6846 }
6847 ra = gen_avr_ptr(rA(ctx->opcode));
6848 rb = gen_avr_ptr(rB(ctx->opcode));
6849 rc = gen_avr_ptr(rC(ctx->opcode));
6850 rd = gen_avr_ptr(rD(ctx->opcode));
6851 gen_helper_vmladduhm(rd, ra, rb, rc);
6852 tcg_temp_free_ptr(ra);
6853 tcg_temp_free_ptr(rb);
6854 tcg_temp_free_ptr(rc);
6855 tcg_temp_free_ptr(rd);
6856}
6857
b04ae981 6858GEN_VAFORM_PAIRED(vmsumubm, vmsummbm, 18)
4d9903b6 6859GEN_VAFORM_PAIRED(vmsumuhm, vmsumuhs, 19)
eae07261 6860GEN_VAFORM_PAIRED(vmsumshm, vmsumshs, 20)
d1258698 6861GEN_VAFORM_PAIRED(vsel, vperm, 21)
35cf7c7e 6862GEN_VAFORM_PAIRED(vmaddfp, vnmsubfp, 23)
b04ae981 6863
0487d6a8 6864/*** SPE extension ***/
0487d6a8 6865/* Register moves */
3cd7d1dd 6866
a0e13900
FC
6867
6868static inline void gen_evmra(DisasContext *ctx)
6869{
6870
6871 if (unlikely(!ctx->spe_enabled)) {
27a69bb0 6872 gen_exception(ctx, POWERPC_EXCP_SPEU);
a0e13900
FC
6873 return;
6874 }
6875
6876#if defined(TARGET_PPC64)
6877 /* rD := rA */
6878 tcg_gen_mov_i64(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)]);
6879
6880 /* spe_acc := rA */
6881 tcg_gen_st_i64(cpu_gpr[rA(ctx->opcode)],
6882 cpu_env,
1328c2bf 6883 offsetof(CPUPPCState, spe_acc));
a0e13900
FC
6884#else
6885 TCGv_i64 tmp = tcg_temp_new_i64();
6886
6887 /* tmp := rA_lo + rA_hi << 32 */
6888 tcg_gen_concat_i32_i64(tmp, cpu_gpr[rA(ctx->opcode)], cpu_gprh[rA(ctx->opcode)]);
6889
6890 /* spe_acc := tmp */
1328c2bf 6891 tcg_gen_st_i64(tmp, cpu_env, offsetof(CPUPPCState, spe_acc));
a0e13900
FC
6892 tcg_temp_free_i64(tmp);
6893
6894 /* rD := rA */
6895 tcg_gen_mov_i32(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)]);
6896 tcg_gen_mov_i32(cpu_gprh[rD(ctx->opcode)], cpu_gprh[rA(ctx->opcode)]);
6897#endif
6898}
6899
636aa200
BS
6900static inline void gen_load_gpr64(TCGv_i64 t, int reg)
6901{
f78fb44e
AJ
6902#if defined(TARGET_PPC64)
6903 tcg_gen_mov_i64(t, cpu_gpr[reg]);
6904#else
36aa55dc 6905 tcg_gen_concat_i32_i64(t, cpu_gpr[reg], cpu_gprh[reg]);
3cd7d1dd 6906#endif
f78fb44e 6907}
3cd7d1dd 6908
636aa200
BS
6909static inline void gen_store_gpr64(int reg, TCGv_i64 t)
6910{
f78fb44e
AJ
6911#if defined(TARGET_PPC64)
6912 tcg_gen_mov_i64(cpu_gpr[reg], t);
6913#else
a7812ae4 6914 TCGv_i64 tmp = tcg_temp_new_i64();
f78fb44e 6915 tcg_gen_trunc_i64_i32(cpu_gpr[reg], t);
f78fb44e
AJ
6916 tcg_gen_shri_i64(tmp, t, 32);
6917 tcg_gen_trunc_i64_i32(cpu_gprh[reg], tmp);
a7812ae4 6918 tcg_temp_free_i64(tmp);
3cd7d1dd 6919#endif
f78fb44e 6920}
3cd7d1dd 6921
70560da7 6922#define GEN_SPE(name0, name1, opc2, opc3, inval0, inval1, type) \
99e300ef 6923static void glue(gen_, name0##_##name1)(DisasContext *ctx) \
0487d6a8
JM
6924{ \
6925 if (Rc(ctx->opcode)) \
6926 gen_##name1(ctx); \
6927 else \
6928 gen_##name0(ctx); \
6929}
6930
6931/* Handler for undefined SPE opcodes */
636aa200 6932static inline void gen_speundef(DisasContext *ctx)
0487d6a8 6933{
e06fcd75 6934 gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL);
0487d6a8
JM
6935}
6936
57951c27
AJ
6937/* SPE logic */
6938#if defined(TARGET_PPC64)
6939#define GEN_SPEOP_LOGIC2(name, tcg_op) \
636aa200 6940static inline void gen_##name(DisasContext *ctx) \
0487d6a8
JM
6941{ \
6942 if (unlikely(!ctx->spe_enabled)) { \
27a69bb0 6943 gen_exception(ctx, POWERPC_EXCP_SPEU); \
0487d6a8
JM
6944 return; \
6945 } \
57951c27
AJ
6946 tcg_op(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)], \
6947 cpu_gpr[rB(ctx->opcode)]); \
6948}
6949#else
6950#define GEN_SPEOP_LOGIC2(name, tcg_op) \
636aa200 6951static inline void gen_##name(DisasContext *ctx) \
57951c27
AJ
6952{ \
6953 if (unlikely(!ctx->spe_enabled)) { \
27a69bb0 6954 gen_exception(ctx, POWERPC_EXCP_SPEU); \
57951c27
AJ
6955 return; \
6956 } \
6957 tcg_op(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)], \
6958 cpu_gpr[rB(ctx->opcode)]); \
6959 tcg_op(cpu_gprh[rD(ctx->opcode)], cpu_gprh[rA(ctx->opcode)], \
6960 cpu_gprh[rB(ctx->opcode)]); \
0487d6a8 6961}
57951c27
AJ
6962#endif
6963
6964GEN_SPEOP_LOGIC2(evand, tcg_gen_and_tl);
6965GEN_SPEOP_LOGIC2(evandc, tcg_gen_andc_tl);
6966GEN_SPEOP_LOGIC2(evxor, tcg_gen_xor_tl);
6967GEN_SPEOP_LOGIC2(evor, tcg_gen_or_tl);
6968GEN_SPEOP_LOGIC2(evnor, tcg_gen_nor_tl);
6969GEN_SPEOP_LOGIC2(eveqv, tcg_gen_eqv_tl);
6970GEN_SPEOP_LOGIC2(evorc, tcg_gen_orc_tl);
6971GEN_SPEOP_LOGIC2(evnand, tcg_gen_nand_tl);
0487d6a8 6972
57951c27
AJ
6973/* SPE logic immediate */
6974#if defined(TARGET_PPC64)
6975#define GEN_SPEOP_TCG_LOGIC_IMM2(name, tcg_opi) \
636aa200 6976static inline void gen_##name(DisasContext *ctx) \
3d3a6a0a
AJ
6977{ \
6978 if (unlikely(!ctx->spe_enabled)) { \
27a69bb0 6979 gen_exception(ctx, POWERPC_EXCP_SPEU); \
3d3a6a0a
AJ
6980 return; \
6981 } \
a7812ae4
PB
6982 TCGv_i32 t0 = tcg_temp_local_new_i32(); \
6983 TCGv_i32 t1 = tcg_temp_local_new_i32(); \
6984 TCGv_i64 t2 = tcg_temp_local_new_i64(); \
57951c27
AJ
6985 tcg_gen_trunc_i64_i32(t0, cpu_gpr[rA(ctx->opcode)]); \
6986 tcg_opi(t0, t0, rB(ctx->opcode)); \
6987 tcg_gen_shri_i64(t2, cpu_gpr[rA(ctx->opcode)], 32); \
6988 tcg_gen_trunc_i64_i32(t1, t2); \
a7812ae4 6989 tcg_temp_free_i64(t2); \
57951c27
AJ
6990 tcg_opi(t1, t1, rB(ctx->opcode)); \
6991 tcg_gen_concat_i32_i64(cpu_gpr[rD(ctx->opcode)], t0, t1); \
a7812ae4
PB
6992 tcg_temp_free_i32(t0); \
6993 tcg_temp_free_i32(t1); \
3d3a6a0a 6994}
57951c27
AJ
6995#else
6996#define GEN_SPEOP_TCG_LOGIC_IMM2(name, tcg_opi) \
636aa200 6997static inline void gen_##name(DisasContext *ctx) \
0487d6a8
JM
6998{ \
6999 if (unlikely(!ctx->spe_enabled)) { \
27a69bb0 7000 gen_exception(ctx, POWERPC_EXCP_SPEU); \
0487d6a8
JM
7001 return; \
7002 } \
57951c27
AJ
7003 tcg_opi(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)], \
7004 rB(ctx->opcode)); \
7005 tcg_opi(cpu_gprh[rD(ctx->opcode)], cpu_gprh[rA(ctx->opcode)], \
7006 rB(ctx->opcode)); \
0487d6a8 7007}
57951c27
AJ
7008#endif
7009GEN_SPEOP_TCG_LOGIC_IMM2(evslwi, tcg_gen_shli_i32);
7010GEN_SPEOP_TCG_LOGIC_IMM2(evsrwiu, tcg_gen_shri_i32);
7011GEN_SPEOP_TCG_LOGIC_IMM2(evsrwis, tcg_gen_sari_i32);
7012GEN_SPEOP_TCG_LOGIC_IMM2(evrlwi, tcg_gen_rotli_i32);
0487d6a8 7013
57951c27
AJ
7014/* SPE arithmetic */
7015#if defined(TARGET_PPC64)
7016#define GEN_SPEOP_ARITH1(name, tcg_op) \
636aa200 7017static inline void gen_##name(DisasContext *ctx) \
0487d6a8
JM
7018{ \
7019 if (unlikely(!ctx->spe_enabled)) { \
27a69bb0 7020 gen_exception(ctx, POWERPC_EXCP_SPEU); \
0487d6a8
JM
7021 return; \
7022 } \
a7812ae4
PB
7023 TCGv_i32 t0 = tcg_temp_local_new_i32(); \
7024 TCGv_i32 t1 = tcg_temp_local_new_i32(); \
7025 TCGv_i64 t2 = tcg_temp_local_new_i64(); \
57951c27
AJ
7026 tcg_gen_trunc_i64_i32(t0, cpu_gpr[rA(ctx->opcode)]); \
7027 tcg_op(t0, t0); \
7028 tcg_gen_shri_i64(t2, cpu_gpr[rA(ctx->opcode)], 32); \
7029 tcg_gen_trunc_i64_i32(t1, t2); \
a7812ae4 7030 tcg_temp_free_i64(t2); \
57951c27
AJ
7031 tcg_op(t1, t1); \
7032 tcg_gen_concat_i32_i64(cpu_gpr[rD(ctx->opcode)], t0, t1); \
a7812ae4
PB
7033 tcg_temp_free_i32(t0); \
7034 tcg_temp_free_i32(t1); \
0487d6a8 7035}
57951c27 7036#else
a7812ae4 7037#define GEN_SPEOP_ARITH1(name, tcg_op) \
636aa200 7038static inline void gen_##name(DisasContext *ctx) \
57951c27
AJ
7039{ \
7040 if (unlikely(!ctx->spe_enabled)) { \
27a69bb0 7041 gen_exception(ctx, POWERPC_EXCP_SPEU); \
57951c27
AJ
7042 return; \
7043 } \
7044 tcg_op(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)]); \
7045 tcg_op(cpu_gprh[rD(ctx->opcode)], cpu_gprh[rA(ctx->opcode)]); \
7046}
7047#endif
0487d6a8 7048
636aa200 7049static inline void gen_op_evabs(TCGv_i32 ret, TCGv_i32 arg1)
57951c27
AJ
7050{
7051 int l1 = gen_new_label();
7052 int l2 = gen_new_label();
0487d6a8 7053
57951c27
AJ
7054 tcg_gen_brcondi_i32(TCG_COND_GE, arg1, 0, l1);
7055 tcg_gen_neg_i32(ret, arg1);
7056 tcg_gen_br(l2);
7057 gen_set_label(l1);
a7812ae4 7058 tcg_gen_mov_i32(ret, arg1);
57951c27
AJ
7059 gen_set_label(l2);
7060}
7061GEN_SPEOP_ARITH1(evabs, gen_op_evabs);
7062GEN_SPEOP_ARITH1(evneg, tcg_gen_neg_i32);
7063GEN_SPEOP_ARITH1(evextsb, tcg_gen_ext8s_i32);
7064GEN_SPEOP_ARITH1(evextsh, tcg_gen_ext16s_i32);
636aa200 7065static inline void gen_op_evrndw(TCGv_i32 ret, TCGv_i32 arg1)
0487d6a8 7066{
57951c27
AJ
7067 tcg_gen_addi_i32(ret, arg1, 0x8000);
7068 tcg_gen_ext16u_i32(ret, ret);
7069}
7070GEN_SPEOP_ARITH1(evrndw, gen_op_evrndw);
a7812ae4
PB
7071GEN_SPEOP_ARITH1(evcntlsw, gen_helper_cntlsw32);
7072GEN_SPEOP_ARITH1(evcntlzw, gen_helper_cntlzw32);
0487d6a8 7073
57951c27
AJ
7074#if defined(TARGET_PPC64)
7075#define GEN_SPEOP_ARITH2(name, tcg_op) \
636aa200 7076static inline void gen_##name(DisasContext *ctx) \
0487d6a8
JM
7077{ \
7078 if (unlikely(!ctx->spe_enabled)) { \
27a69bb0 7079 gen_exception(ctx, POWERPC_EXCP_SPEU); \
0487d6a8
JM
7080 return; \
7081 } \
a7812ae4
PB
7082 TCGv_i32 t0 = tcg_temp_local_new_i32(); \
7083 TCGv_i32 t1 = tcg_temp_local_new_i32(); \
7084 TCGv_i32 t2 = tcg_temp_local_new_i32(); \
501e23c4 7085 TCGv_i64 t3 = tcg_temp_local_new_i64(); \
57951c27
AJ
7086 tcg_gen_trunc_i64_i32(t0, cpu_gpr[rA(ctx->opcode)]); \
7087 tcg_gen_trunc_i64_i32(t2, cpu_gpr[rB(ctx->opcode)]); \
7088 tcg_op(t0, t0, t2); \
7089 tcg_gen_shri_i64(t3, cpu_gpr[rA(ctx->opcode)], 32); \
7090 tcg_gen_trunc_i64_i32(t1, t3); \
7091 tcg_gen_shri_i64(t3, cpu_gpr[rB(ctx->opcode)], 32); \
7092 tcg_gen_trunc_i64_i32(t2, t3); \
a7812ae4 7093 tcg_temp_free_i64(t3); \
57951c27 7094 tcg_op(t1, t1, t2); \
a7812ae4 7095 tcg_temp_free_i32(t2); \
57951c27 7096 tcg_gen_concat_i32_i64(cpu_gpr[rD(ctx->opcode)], t0, t1); \
a7812ae4
PB
7097 tcg_temp_free_i32(t0); \
7098 tcg_temp_free_i32(t1); \
0487d6a8 7099}
57951c27
AJ
7100#else
7101#define GEN_SPEOP_ARITH2(name, tcg_op) \
636aa200 7102static inline void gen_##name(DisasContext *ctx) \
0487d6a8
JM
7103{ \
7104 if (unlikely(!ctx->spe_enabled)) { \
27a69bb0 7105 gen_exception(ctx, POWERPC_EXCP_SPEU); \
0487d6a8
JM
7106 return; \
7107 } \
57951c27
AJ
7108 tcg_op(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)], \
7109 cpu_gpr[rB(ctx->opcode)]); \
7110 tcg_op(cpu_gprh[rD(ctx->opcode)], cpu_gprh[rA(ctx->opcode)], \
7111 cpu_gprh[rB(ctx->opcode)]); \
0487d6a8 7112}
57951c27 7113#endif
0487d6a8 7114
636aa200 7115static inline void gen_op_evsrwu(TCGv_i32 ret, TCGv_i32 arg1, TCGv_i32 arg2)
57951c27 7116{
a7812ae4 7117 TCGv_i32 t0;
57951c27 7118 int l1, l2;
0487d6a8 7119
57951c27
AJ
7120 l1 = gen_new_label();
7121 l2 = gen_new_label();
a7812ae4 7122 t0 = tcg_temp_local_new_i32();
57951c27
AJ
7123 /* No error here: 6 bits are used */
7124 tcg_gen_andi_i32(t0, arg2, 0x3F);
7125 tcg_gen_brcondi_i32(TCG_COND_GE, t0, 32, l1);
7126 tcg_gen_shr_i32(ret, arg1, t0);
7127 tcg_gen_br(l2);
7128 gen_set_label(l1);
7129 tcg_gen_movi_i32(ret, 0);
0aef4261 7130 gen_set_label(l2);
a7812ae4 7131 tcg_temp_free_i32(t0);
57951c27
AJ
7132}
7133GEN_SPEOP_ARITH2(evsrwu, gen_op_evsrwu);
636aa200 7134static inline void gen_op_evsrws(TCGv_i32 ret, TCGv_i32 arg1, TCGv_i32 arg2)
57951c27 7135{
a7812ae4 7136 TCGv_i32 t0;
57951c27
AJ
7137 int l1, l2;
7138
7139 l1 = gen_new_label();
7140 l2 = gen_new_label();
a7812ae4 7141 t0 = tcg_temp_local_new_i32();
57951c27
AJ
7142 /* No error here: 6 bits are used */
7143 tcg_gen_andi_i32(t0, arg2, 0x3F);
7144 tcg_gen_brcondi_i32(TCG_COND_GE, t0, 32, l1);
7145 tcg_gen_sar_i32(ret, arg1, t0);
7146 tcg_gen_br(l2);
7147 gen_set_label(l1);
7148 tcg_gen_movi_i32(ret, 0);
0aef4261 7149 gen_set_label(l2);
a7812ae4 7150 tcg_temp_free_i32(t0);
57951c27
AJ
7151}
7152GEN_SPEOP_ARITH2(evsrws, gen_op_evsrws);
636aa200 7153static inline void gen_op_evslw(TCGv_i32 ret, TCGv_i32 arg1, TCGv_i32 arg2)
57951c27 7154{
a7812ae4 7155 TCGv_i32 t0;
57951c27
AJ
7156 int l1, l2;
7157
7158 l1 = gen_new_label();
7159 l2 = gen_new_label();
a7812ae4 7160 t0 = tcg_temp_local_new_i32();
57951c27
AJ
7161 /* No error here: 6 bits are used */
7162 tcg_gen_andi_i32(t0, arg2, 0x3F);
7163 tcg_gen_brcondi_i32(TCG_COND_GE, t0, 32, l1);
7164 tcg_gen_shl_i32(ret, arg1, t0);
7165 tcg_gen_br(l2);
7166 gen_set_label(l1);
7167 tcg_gen_movi_i32(ret, 0);
e29ef9fa 7168 gen_set_label(l2);
a7812ae4 7169 tcg_temp_free_i32(t0);
57951c27
AJ
7170}
7171GEN_SPEOP_ARITH2(evslw, gen_op_evslw);
636aa200 7172static inline void gen_op_evrlw(TCGv_i32 ret, TCGv_i32 arg1, TCGv_i32 arg2)
57951c27 7173{
a7812ae4 7174 TCGv_i32 t0 = tcg_temp_new_i32();
57951c27
AJ
7175 tcg_gen_andi_i32(t0, arg2, 0x1F);
7176 tcg_gen_rotl_i32(ret, arg1, t0);
a7812ae4 7177 tcg_temp_free_i32(t0);
57951c27
AJ
7178}
7179GEN_SPEOP_ARITH2(evrlw, gen_op_evrlw);
636aa200 7180static inline void gen_evmergehi(DisasContext *ctx)
57951c27
AJ
7181{
7182 if (unlikely(!ctx->spe_enabled)) {
27a69bb0 7183 gen_exception(ctx, POWERPC_EXCP_SPEU);
57951c27
AJ
7184 return;
7185 }
7186#if defined(TARGET_PPC64)
a7812ae4
PB
7187 TCGv t0 = tcg_temp_new();
7188 TCGv t1 = tcg_temp_new();
57951c27
AJ
7189 tcg_gen_shri_tl(t0, cpu_gpr[rB(ctx->opcode)], 32);
7190 tcg_gen_andi_tl(t1, cpu_gpr[rA(ctx->opcode)], 0xFFFFFFFF0000000ULL);
7191 tcg_gen_or_tl(cpu_gpr[rD(ctx->opcode)], t0, t1);
7192 tcg_temp_free(t0);
7193 tcg_temp_free(t1);
7194#else
7195 tcg_gen_mov_i32(cpu_gpr[rD(ctx->opcode)], cpu_gprh[rB(ctx->opcode)]);
7196 tcg_gen_mov_i32(cpu_gprh[rD(ctx->opcode)], cpu_gprh[rA(ctx->opcode)]);
7197#endif
7198}
7199GEN_SPEOP_ARITH2(evaddw, tcg_gen_add_i32);
636aa200 7200static inline void gen_op_evsubf(TCGv_i32 ret, TCGv_i32 arg1, TCGv_i32 arg2)
0487d6a8 7201{
57951c27
AJ
7202 tcg_gen_sub_i32(ret, arg2, arg1);
7203}
7204GEN_SPEOP_ARITH2(evsubfw, gen_op_evsubf);
0487d6a8 7205
57951c27
AJ
7206/* SPE arithmetic immediate */
7207#if defined(TARGET_PPC64)
7208#define GEN_SPEOP_ARITH_IMM2(name, tcg_op) \
636aa200 7209static inline void gen_##name(DisasContext *ctx) \
57951c27
AJ
7210{ \
7211 if (unlikely(!ctx->spe_enabled)) { \
27a69bb0 7212 gen_exception(ctx, POWERPC_EXCP_SPEU); \
57951c27
AJ
7213 return; \
7214 } \
a7812ae4
PB
7215 TCGv_i32 t0 = tcg_temp_local_new_i32(); \
7216 TCGv_i32 t1 = tcg_temp_local_new_i32(); \
7217 TCGv_i64 t2 = tcg_temp_local_new_i64(); \
57951c27
AJ
7218 tcg_gen_trunc_i64_i32(t0, cpu_gpr[rB(ctx->opcode)]); \
7219 tcg_op(t0, t0, rA(ctx->opcode)); \
7220 tcg_gen_shri_i64(t2, cpu_gpr[rB(ctx->opcode)], 32); \
7221 tcg_gen_trunc_i64_i32(t1, t2); \
e06fcd75 7222 tcg_temp_free_i64(t2); \
57951c27
AJ
7223 tcg_op(t1, t1, rA(ctx->opcode)); \
7224 tcg_gen_concat_i32_i64(cpu_gpr[rD(ctx->opcode)], t0, t1); \
a7812ae4
PB
7225 tcg_temp_free_i32(t0); \
7226 tcg_temp_free_i32(t1); \
57951c27
AJ
7227}
7228#else
7229#define GEN_SPEOP_ARITH_IMM2(name, tcg_op) \
636aa200 7230static inline void gen_##name(DisasContext *ctx) \
57951c27
AJ
7231{ \
7232 if (unlikely(!ctx->spe_enabled)) { \
27a69bb0 7233 gen_exception(ctx, POWERPC_EXCP_SPEU); \
57951c27
AJ
7234 return; \
7235 } \
7236 tcg_op(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rB(ctx->opcode)], \
7237 rA(ctx->opcode)); \
7238 tcg_op(cpu_gprh[rD(ctx->opcode)], cpu_gprh[rB(ctx->opcode)], \
7239 rA(ctx->opcode)); \
7240}
7241#endif
7242GEN_SPEOP_ARITH_IMM2(evaddiw, tcg_gen_addi_i32);
7243GEN_SPEOP_ARITH_IMM2(evsubifw, tcg_gen_subi_i32);
7244
7245/* SPE comparison */
7246#if defined(TARGET_PPC64)
7247#define GEN_SPEOP_COMP(name, tcg_cond) \
636aa200 7248static inline void gen_##name(DisasContext *ctx) \
57951c27
AJ
7249{ \
7250 if (unlikely(!ctx->spe_enabled)) { \
27a69bb0 7251 gen_exception(ctx, POWERPC_EXCP_SPEU); \
57951c27
AJ
7252 return; \
7253 } \
7254 int l1 = gen_new_label(); \
7255 int l2 = gen_new_label(); \
7256 int l3 = gen_new_label(); \
7257 int l4 = gen_new_label(); \
a7812ae4
PB
7258 TCGv_i32 t0 = tcg_temp_local_new_i32(); \
7259 TCGv_i32 t1 = tcg_temp_local_new_i32(); \
7260 TCGv_i64 t2 = tcg_temp_local_new_i64(); \
57951c27
AJ
7261 tcg_gen_trunc_i64_i32(t0, cpu_gpr[rA(ctx->opcode)]); \
7262 tcg_gen_trunc_i64_i32(t1, cpu_gpr[rB(ctx->opcode)]); \
7263 tcg_gen_brcond_i32(tcg_cond, t0, t1, l1); \
a7812ae4 7264 tcg_gen_movi_i32(cpu_crf[crfD(ctx->opcode)], 0); \
57951c27
AJ
7265 tcg_gen_br(l2); \
7266 gen_set_label(l1); \
7267 tcg_gen_movi_i32(cpu_crf[crfD(ctx->opcode)], \
7268 CRF_CL | CRF_CH_OR_CL | CRF_CH_AND_CL); \
7269 gen_set_label(l2); \
7270 tcg_gen_shri_i64(t2, cpu_gpr[rA(ctx->opcode)], 32); \
7271 tcg_gen_trunc_i64_i32(t0, t2); \
7272 tcg_gen_shri_i64(t2, cpu_gpr[rB(ctx->opcode)], 32); \
7273 tcg_gen_trunc_i64_i32(t1, t2); \
a7812ae4 7274 tcg_temp_free_i64(t2); \
57951c27
AJ
7275 tcg_gen_brcond_i32(tcg_cond, t0, t1, l3); \
7276 tcg_gen_andi_i32(cpu_crf[crfD(ctx->opcode)], cpu_crf[crfD(ctx->opcode)], \
7277 ~(CRF_CH | CRF_CH_AND_CL)); \
7278 tcg_gen_br(l4); \
7279 gen_set_label(l3); \
7280 tcg_gen_ori_i32(cpu_crf[crfD(ctx->opcode)], cpu_crf[crfD(ctx->opcode)], \
7281 CRF_CH | CRF_CH_OR_CL); \
7282 gen_set_label(l4); \
a7812ae4
PB
7283 tcg_temp_free_i32(t0); \
7284 tcg_temp_free_i32(t1); \
57951c27
AJ
7285}
7286#else
7287#define GEN_SPEOP_COMP(name, tcg_cond) \
636aa200 7288static inline void gen_##name(DisasContext *ctx) \
57951c27
AJ
7289{ \
7290 if (unlikely(!ctx->spe_enabled)) { \
27a69bb0 7291 gen_exception(ctx, POWERPC_EXCP_SPEU); \
57951c27
AJ
7292 return; \
7293 } \
7294 int l1 = gen_new_label(); \
7295 int l2 = gen_new_label(); \
7296 int l3 = gen_new_label(); \
7297 int l4 = gen_new_label(); \
7298 \
7299 tcg_gen_brcond_i32(tcg_cond, cpu_gpr[rA(ctx->opcode)], \
7300 cpu_gpr[rB(ctx->opcode)], l1); \
7301 tcg_gen_movi_tl(cpu_crf[crfD(ctx->opcode)], 0); \
7302 tcg_gen_br(l2); \
7303 gen_set_label(l1); \
7304 tcg_gen_movi_i32(cpu_crf[crfD(ctx->opcode)], \
7305 CRF_CL | CRF_CH_OR_CL | CRF_CH_AND_CL); \
7306 gen_set_label(l2); \
7307 tcg_gen_brcond_i32(tcg_cond, cpu_gprh[rA(ctx->opcode)], \
7308 cpu_gprh[rB(ctx->opcode)], l3); \
7309 tcg_gen_andi_i32(cpu_crf[crfD(ctx->opcode)], cpu_crf[crfD(ctx->opcode)], \
7310 ~(CRF_CH | CRF_CH_AND_CL)); \
7311 tcg_gen_br(l4); \
7312 gen_set_label(l3); \
7313 tcg_gen_ori_i32(cpu_crf[crfD(ctx->opcode)], cpu_crf[crfD(ctx->opcode)], \
7314 CRF_CH | CRF_CH_OR_CL); \
7315 gen_set_label(l4); \
7316}
7317#endif
7318GEN_SPEOP_COMP(evcmpgtu, TCG_COND_GTU);
7319GEN_SPEOP_COMP(evcmpgts, TCG_COND_GT);
7320GEN_SPEOP_COMP(evcmpltu, TCG_COND_LTU);
7321GEN_SPEOP_COMP(evcmplts, TCG_COND_LT);
7322GEN_SPEOP_COMP(evcmpeq, TCG_COND_EQ);
7323
7324/* SPE misc */
636aa200 7325static inline void gen_brinc(DisasContext *ctx)
57951c27
AJ
7326{
7327 /* Note: brinc is usable even if SPE is disabled */
a7812ae4
PB
7328 gen_helper_brinc(cpu_gpr[rD(ctx->opcode)],
7329 cpu_gpr[rA(ctx->opcode)], cpu_gpr[rB(ctx->opcode)]);
0487d6a8 7330}
636aa200 7331static inline void gen_evmergelo(DisasContext *ctx)
57951c27
AJ
7332{
7333 if (unlikely(!ctx->spe_enabled)) {
27a69bb0 7334 gen_exception(ctx, POWERPC_EXCP_SPEU);
57951c27
AJ
7335 return;
7336 }
7337#if defined(TARGET_PPC64)
a7812ae4
PB
7338 TCGv t0 = tcg_temp_new();
7339 TCGv t1 = tcg_temp_new();
17d9b3af 7340 tcg_gen_ext32u_tl(t0, cpu_gpr[rB(ctx->opcode)]);
57951c27
AJ
7341 tcg_gen_shli_tl(t1, cpu_gpr[rA(ctx->opcode)], 32);
7342 tcg_gen_or_tl(cpu_gpr[rD(ctx->opcode)], t0, t1);
7343 tcg_temp_free(t0);
7344 tcg_temp_free(t1);
7345#else
57951c27 7346 tcg_gen_mov_i32(cpu_gprh[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)]);
33890b3e 7347 tcg_gen_mov_i32(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rB(ctx->opcode)]);
57951c27
AJ
7348#endif
7349}
636aa200 7350static inline void gen_evmergehilo(DisasContext *ctx)
57951c27
AJ
7351{
7352 if (unlikely(!ctx->spe_enabled)) {
27a69bb0 7353 gen_exception(ctx, POWERPC_EXCP_SPEU);
57951c27
AJ
7354 return;
7355 }
7356#if defined(TARGET_PPC64)
a7812ae4
PB
7357 TCGv t0 = tcg_temp_new();
7358 TCGv t1 = tcg_temp_new();
17d9b3af 7359 tcg_gen_ext32u_tl(t0, cpu_gpr[rB(ctx->opcode)]);
57951c27
AJ
7360 tcg_gen_andi_tl(t1, cpu_gpr[rA(ctx->opcode)], 0xFFFFFFFF0000000ULL);
7361 tcg_gen_or_tl(cpu_gpr[rD(ctx->opcode)], t0, t1);
7362 tcg_temp_free(t0);
7363 tcg_temp_free(t1);
7364#else
7365 tcg_gen_mov_i32(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rB(ctx->opcode)]);
7366 tcg_gen_mov_i32(cpu_gprh[rD(ctx->opcode)], cpu_gprh[rA(ctx->opcode)]);
7367#endif
7368}
636aa200 7369static inline void gen_evmergelohi(DisasContext *ctx)
57951c27
AJ
7370{
7371 if (unlikely(!ctx->spe_enabled)) {
27a69bb0 7372 gen_exception(ctx, POWERPC_EXCP_SPEU);
57951c27
AJ
7373 return;
7374 }
7375#if defined(TARGET_PPC64)
a7812ae4
PB
7376 TCGv t0 = tcg_temp_new();
7377 TCGv t1 = tcg_temp_new();
57951c27
AJ
7378 tcg_gen_shri_tl(t0, cpu_gpr[rB(ctx->opcode)], 32);
7379 tcg_gen_shli_tl(t1, cpu_gpr[rA(ctx->opcode)], 32);
7380 tcg_gen_or_tl(cpu_gpr[rD(ctx->opcode)], t0, t1);
7381 tcg_temp_free(t0);
7382 tcg_temp_free(t1);
7383#else
33890b3e
NF
7384 if (rD(ctx->opcode) == rA(ctx->opcode)) {
7385 TCGv_i32 tmp = tcg_temp_new_i32();
7386 tcg_gen_mov_i32(tmp, cpu_gpr[rA(ctx->opcode)]);
7387 tcg_gen_mov_i32(cpu_gpr[rD(ctx->opcode)], cpu_gprh[rB(ctx->opcode)]);
7388 tcg_gen_mov_i32(cpu_gprh[rD(ctx->opcode)], tmp);
7389 tcg_temp_free_i32(tmp);
7390 } else {
7391 tcg_gen_mov_i32(cpu_gpr[rD(ctx->opcode)], cpu_gprh[rB(ctx->opcode)]);
7392 tcg_gen_mov_i32(cpu_gprh[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)]);
7393 }
57951c27
AJ
7394#endif
7395}
636aa200 7396static inline void gen_evsplati(DisasContext *ctx)
57951c27 7397{
ae01847f 7398 uint64_t imm = ((int32_t)(rA(ctx->opcode) << 27)) >> 27;
0487d6a8 7399
57951c27 7400#if defined(TARGET_PPC64)
38d14952 7401 tcg_gen_movi_tl(cpu_gpr[rD(ctx->opcode)], (imm << 32) | imm);
57951c27
AJ
7402#else
7403 tcg_gen_movi_i32(cpu_gpr[rD(ctx->opcode)], imm);
7404 tcg_gen_movi_i32(cpu_gprh[rD(ctx->opcode)], imm);
7405#endif
7406}
636aa200 7407static inline void gen_evsplatfi(DisasContext *ctx)
0487d6a8 7408{
ae01847f 7409 uint64_t imm = rA(ctx->opcode) << 27;
0487d6a8 7410
57951c27 7411#if defined(TARGET_PPC64)
38d14952 7412 tcg_gen_movi_tl(cpu_gpr[rD(ctx->opcode)], (imm << 32) | imm);
57951c27
AJ
7413#else
7414 tcg_gen_movi_i32(cpu_gpr[rD(ctx->opcode)], imm);
7415 tcg_gen_movi_i32(cpu_gprh[rD(ctx->opcode)], imm);
7416#endif
0487d6a8
JM
7417}
7418
636aa200 7419static inline void gen_evsel(DisasContext *ctx)
57951c27
AJ
7420{
7421 int l1 = gen_new_label();
7422 int l2 = gen_new_label();
7423 int l3 = gen_new_label();
7424 int l4 = gen_new_label();
a7812ae4 7425 TCGv_i32 t0 = tcg_temp_local_new_i32();
57951c27 7426#if defined(TARGET_PPC64)
a7812ae4
PB
7427 TCGv t1 = tcg_temp_local_new();
7428 TCGv t2 = tcg_temp_local_new();
57951c27
AJ
7429#endif
7430 tcg_gen_andi_i32(t0, cpu_crf[ctx->opcode & 0x07], 1 << 3);
7431 tcg_gen_brcondi_i32(TCG_COND_EQ, t0, 0, l1);
7432#if defined(TARGET_PPC64)
7433 tcg_gen_andi_tl(t1, cpu_gpr[rA(ctx->opcode)], 0xFFFFFFFF00000000ULL);
7434#else
7435 tcg_gen_mov_tl(cpu_gprh[rD(ctx->opcode)], cpu_gprh[rA(ctx->opcode)]);
7436#endif
7437 tcg_gen_br(l2);
7438 gen_set_label(l1);
7439#if defined(TARGET_PPC64)
7440 tcg_gen_andi_tl(t1, cpu_gpr[rB(ctx->opcode)], 0xFFFFFFFF00000000ULL);
7441#else
7442 tcg_gen_mov_tl(cpu_gprh[rD(ctx->opcode)], cpu_gprh[rB(ctx->opcode)]);
7443#endif
7444 gen_set_label(l2);
7445 tcg_gen_andi_i32(t0, cpu_crf[ctx->opcode & 0x07], 1 << 2);
7446 tcg_gen_brcondi_i32(TCG_COND_EQ, t0, 0, l3);
7447#if defined(TARGET_PPC64)
17d9b3af 7448 tcg_gen_ext32u_tl(t2, cpu_gpr[rA(ctx->opcode)]);
57951c27
AJ
7449#else
7450 tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)]);
7451#endif
7452 tcg_gen_br(l4);
7453 gen_set_label(l3);
7454#if defined(TARGET_PPC64)
17d9b3af 7455 tcg_gen_ext32u_tl(t2, cpu_gpr[rB(ctx->opcode)]);
57951c27
AJ
7456#else
7457 tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rB(ctx->opcode)]);
7458#endif
7459 gen_set_label(l4);
a7812ae4 7460 tcg_temp_free_i32(t0);
57951c27
AJ
7461#if defined(TARGET_PPC64)
7462 tcg_gen_or_tl(cpu_gpr[rD(ctx->opcode)], t1, t2);
7463 tcg_temp_free(t1);
7464 tcg_temp_free(t2);
7465#endif
7466}
e8eaa2c0
BS
7467
7468static void gen_evsel0(DisasContext *ctx)
57951c27
AJ
7469{
7470 gen_evsel(ctx);
7471}
e8eaa2c0
BS
7472
7473static void gen_evsel1(DisasContext *ctx)
57951c27
AJ
7474{
7475 gen_evsel(ctx);
7476}
e8eaa2c0
BS
7477
7478static void gen_evsel2(DisasContext *ctx)
57951c27
AJ
7479{
7480 gen_evsel(ctx);
7481}
e8eaa2c0
BS
7482
7483static void gen_evsel3(DisasContext *ctx)
57951c27
AJ
7484{
7485 gen_evsel(ctx);
7486}
0487d6a8 7487
a0e13900
FC
7488/* Multiply */
7489
7490static inline void gen_evmwumi(DisasContext *ctx)
7491{
7492 TCGv_i64 t0, t1;
7493
7494 if (unlikely(!ctx->spe_enabled)) {
27a69bb0 7495 gen_exception(ctx, POWERPC_EXCP_SPEU);
a0e13900
FC
7496 return;
7497 }
7498
7499 t0 = tcg_temp_new_i64();
7500 t1 = tcg_temp_new_i64();
7501
7502 /* t0 := rA; t1 := rB */
7503#if defined(TARGET_PPC64)
7504 tcg_gen_ext32u_tl(t0, cpu_gpr[rA(ctx->opcode)]);
7505 tcg_gen_ext32u_tl(t1, cpu_gpr[rB(ctx->opcode)]);
7506#else
7507 tcg_gen_extu_tl_i64(t0, cpu_gpr[rA(ctx->opcode)]);
7508 tcg_gen_extu_tl_i64(t1, cpu_gpr[rB(ctx->opcode)]);
7509#endif
7510
7511 tcg_gen_mul_i64(t0, t0, t1); /* t0 := rA * rB */
7512
7513 gen_store_gpr64(rD(ctx->opcode), t0); /* rD := t0 */
7514
7515 tcg_temp_free_i64(t0);
7516 tcg_temp_free_i64(t1);
7517}
7518
7519static inline void gen_evmwumia(DisasContext *ctx)
7520{
7521 TCGv_i64 tmp;
7522
7523 if (unlikely(!ctx->spe_enabled)) {
27a69bb0 7524 gen_exception(ctx, POWERPC_EXCP_SPEU);
a0e13900
FC
7525 return;
7526 }
7527
7528 gen_evmwumi(ctx); /* rD := rA * rB */
7529
7530 tmp = tcg_temp_new_i64();
7531
7532 /* acc := rD */
7533 gen_load_gpr64(tmp, rD(ctx->opcode));
1328c2bf 7534 tcg_gen_st_i64(tmp, cpu_env, offsetof(CPUPPCState, spe_acc));
a0e13900
FC
7535 tcg_temp_free_i64(tmp);
7536}
7537
7538static inline void gen_evmwumiaa(DisasContext *ctx)
7539{
7540 TCGv_i64 acc;
7541 TCGv_i64 tmp;
7542
7543 if (unlikely(!ctx->spe_enabled)) {
27a69bb0 7544 gen_exception(ctx, POWERPC_EXCP_SPEU);
a0e13900
FC
7545 return;
7546 }
7547
7548 gen_evmwumi(ctx); /* rD := rA * rB */
7549
7550 acc = tcg_temp_new_i64();
7551 tmp = tcg_temp_new_i64();
7552
7553 /* tmp := rD */
7554 gen_load_gpr64(tmp, rD(ctx->opcode));
7555
7556 /* Load acc */
1328c2bf 7557 tcg_gen_ld_i64(acc, cpu_env, offsetof(CPUPPCState, spe_acc));
a0e13900
FC
7558
7559 /* acc := tmp + acc */
7560 tcg_gen_add_i64(acc, acc, tmp);
7561
7562 /* Store acc */
1328c2bf 7563 tcg_gen_st_i64(acc, cpu_env, offsetof(CPUPPCState, spe_acc));
a0e13900
FC
7564
7565 /* rD := acc */
7566 gen_store_gpr64(rD(ctx->opcode), acc);
7567
7568 tcg_temp_free_i64(acc);
7569 tcg_temp_free_i64(tmp);
7570}
7571
7572static inline void gen_evmwsmi(DisasContext *ctx)
7573{
7574 TCGv_i64 t0, t1;
7575
7576 if (unlikely(!ctx->spe_enabled)) {
27a69bb0 7577 gen_exception(ctx, POWERPC_EXCP_SPEU);
a0e13900
FC
7578 return;
7579 }
7580
7581 t0 = tcg_temp_new_i64();
7582 t1 = tcg_temp_new_i64();
7583
7584 /* t0 := rA; t1 := rB */
7585#if defined(TARGET_PPC64)
7586 tcg_gen_ext32s_tl(t0, cpu_gpr[rA(ctx->opcode)]);
7587 tcg_gen_ext32s_tl(t1, cpu_gpr[rB(ctx->opcode)]);
7588#else
7589 tcg_gen_ext_tl_i64(t0, cpu_gpr[rA(ctx->opcode)]);
7590 tcg_gen_ext_tl_i64(t1, cpu_gpr[rB(ctx->opcode)]);
7591#endif
7592
7593 tcg_gen_mul_i64(t0, t0, t1); /* t0 := rA * rB */
7594
7595 gen_store_gpr64(rD(ctx->opcode), t0); /* rD := t0 */
7596
7597 tcg_temp_free_i64(t0);
7598 tcg_temp_free_i64(t1);
7599}
7600
7601static inline void gen_evmwsmia(DisasContext *ctx)
7602{
7603 TCGv_i64 tmp;
7604
7605 gen_evmwsmi(ctx); /* rD := rA * rB */
7606
7607 tmp = tcg_temp_new_i64();
7608
7609 /* acc := rD */
7610 gen_load_gpr64(tmp, rD(ctx->opcode));
1328c2bf 7611 tcg_gen_st_i64(tmp, cpu_env, offsetof(CPUPPCState, spe_acc));
a0e13900
FC
7612
7613 tcg_temp_free_i64(tmp);
7614}
7615
7616static inline void gen_evmwsmiaa(DisasContext *ctx)
7617{
7618 TCGv_i64 acc = tcg_temp_new_i64();
7619 TCGv_i64 tmp = tcg_temp_new_i64();
7620
7621 gen_evmwsmi(ctx); /* rD := rA * rB */
7622
7623 acc = tcg_temp_new_i64();
7624 tmp = tcg_temp_new_i64();
7625
7626 /* tmp := rD */
7627 gen_load_gpr64(tmp, rD(ctx->opcode));
7628
7629 /* Load acc */
1328c2bf 7630 tcg_gen_ld_i64(acc, cpu_env, offsetof(CPUPPCState, spe_acc));
a0e13900
FC
7631
7632 /* acc := tmp + acc */
7633 tcg_gen_add_i64(acc, acc, tmp);
7634
7635 /* Store acc */
1328c2bf 7636 tcg_gen_st_i64(acc, cpu_env, offsetof(CPUPPCState, spe_acc));
a0e13900
FC
7637
7638 /* rD := acc */
7639 gen_store_gpr64(rD(ctx->opcode), acc);
7640
7641 tcg_temp_free_i64(acc);
7642 tcg_temp_free_i64(tmp);
7643}
7644
70560da7
FC
7645GEN_SPE(evaddw, speundef, 0x00, 0x08, 0x00000000, 0xFFFFFFFF, PPC_SPE); ////
7646GEN_SPE(evaddiw, speundef, 0x01, 0x08, 0x00000000, 0xFFFFFFFF, PPC_SPE);
7647GEN_SPE(evsubfw, speundef, 0x02, 0x08, 0x00000000, 0xFFFFFFFF, PPC_SPE); ////
7648GEN_SPE(evsubifw, speundef, 0x03, 0x08, 0x00000000, 0xFFFFFFFF, PPC_SPE);
7649GEN_SPE(evabs, evneg, 0x04, 0x08, 0x0000F800, 0x0000F800, PPC_SPE); ////
7650GEN_SPE(evextsb, evextsh, 0x05, 0x08, 0x0000F800, 0x0000F800, PPC_SPE); ////
7651GEN_SPE(evrndw, evcntlzw, 0x06, 0x08, 0x0000F800, 0x0000F800, PPC_SPE); ////
7652GEN_SPE(evcntlsw, brinc, 0x07, 0x08, 0x0000F800, 0x00000000, PPC_SPE); //
7653GEN_SPE(evmra, speundef, 0x02, 0x13, 0x0000F800, 0xFFFFFFFF, PPC_SPE);
7654GEN_SPE(speundef, evand, 0x08, 0x08, 0xFFFFFFFF, 0x00000000, PPC_SPE); ////
7655GEN_SPE(evandc, speundef, 0x09, 0x08, 0x00000000, 0xFFFFFFFF, PPC_SPE); ////
7656GEN_SPE(evxor, evor, 0x0B, 0x08, 0x00000000, 0x00000000, PPC_SPE); ////
7657GEN_SPE(evnor, eveqv, 0x0C, 0x08, 0x00000000, 0x00000000, PPC_SPE); ////
7658GEN_SPE(evmwumi, evmwsmi, 0x0C, 0x11, 0x00000000, 0x00000000, PPC_SPE);
7659GEN_SPE(evmwumia, evmwsmia, 0x1C, 0x11, 0x00000000, 0x00000000, PPC_SPE);
7660GEN_SPE(evmwumiaa, evmwsmiaa, 0x0C, 0x15, 0x00000000, 0x00000000, PPC_SPE);
7661GEN_SPE(speundef, evorc, 0x0D, 0x08, 0xFFFFFFFF, 0x00000000, PPC_SPE); ////
7662GEN_SPE(evnand, speundef, 0x0F, 0x08, 0x00000000, 0xFFFFFFFF, PPC_SPE); ////
7663GEN_SPE(evsrwu, evsrws, 0x10, 0x08, 0x00000000, 0x00000000, PPC_SPE); ////
7664GEN_SPE(evsrwiu, evsrwis, 0x11, 0x08, 0x00000000, 0x00000000, PPC_SPE);
7665GEN_SPE(evslw, speundef, 0x12, 0x08, 0x00000000, 0xFFFFFFFF, PPC_SPE); ////
7666GEN_SPE(evslwi, speundef, 0x13, 0x08, 0x00000000, 0xFFFFFFFF, PPC_SPE);
7667GEN_SPE(evrlw, evsplati, 0x14, 0x08, 0x00000000, 0x0000F800, PPC_SPE); //
7668GEN_SPE(evrlwi, evsplatfi, 0x15, 0x08, 0x00000000, 0x0000F800, PPC_SPE);
7669GEN_SPE(evmergehi, evmergelo, 0x16, 0x08, 0x00000000, 0x00000000, PPC_SPE); ////
7670GEN_SPE(evmergehilo, evmergelohi, 0x17, 0x08, 0x00000000, 0x00000000, PPC_SPE); ////
7671GEN_SPE(evcmpgtu, evcmpgts, 0x18, 0x08, 0x00600000, 0x00600000, PPC_SPE); ////
7672GEN_SPE(evcmpltu, evcmplts, 0x19, 0x08, 0x00600000, 0x00600000, PPC_SPE); ////
7673GEN_SPE(evcmpeq, speundef, 0x1A, 0x08, 0x00600000, 0xFFFFFFFF, PPC_SPE); ////
0487d6a8 7674
6a6ae23f 7675/* SPE load and stores */
636aa200 7676static inline void gen_addr_spe_imm_index(DisasContext *ctx, TCGv EA, int sh)
6a6ae23f
AJ
7677{
7678 target_ulong uimm = rB(ctx->opcode);
7679
76db3ba4 7680 if (rA(ctx->opcode) == 0) {
6a6ae23f 7681 tcg_gen_movi_tl(EA, uimm << sh);
76db3ba4 7682 } else {
6a6ae23f 7683 tcg_gen_addi_tl(EA, cpu_gpr[rA(ctx->opcode)], uimm << sh);
c791fe84 7684 if (NARROW_MODE(ctx)) {
76db3ba4
AJ
7685 tcg_gen_ext32u_tl(EA, EA);
7686 }
76db3ba4 7687 }
0487d6a8 7688}
6a6ae23f 7689
636aa200 7690static inline void gen_op_evldd(DisasContext *ctx, TCGv addr)
6a6ae23f
AJ
7691{
7692#if defined(TARGET_PPC64)
76db3ba4 7693 gen_qemu_ld64(ctx, cpu_gpr[rD(ctx->opcode)], addr);
6a6ae23f
AJ
7694#else
7695 TCGv_i64 t0 = tcg_temp_new_i64();
76db3ba4 7696 gen_qemu_ld64(ctx, t0, addr);
6a6ae23f
AJ
7697 tcg_gen_trunc_i64_i32(cpu_gpr[rD(ctx->opcode)], t0);
7698 tcg_gen_shri_i64(t0, t0, 32);
7699 tcg_gen_trunc_i64_i32(cpu_gprh[rD(ctx->opcode)], t0);
7700 tcg_temp_free_i64(t0);
7701#endif
0487d6a8 7702}
6a6ae23f 7703
636aa200 7704static inline void gen_op_evldw(DisasContext *ctx, TCGv addr)
6a6ae23f 7705{
0487d6a8 7706#if defined(TARGET_PPC64)
6a6ae23f 7707 TCGv t0 = tcg_temp_new();
76db3ba4 7708 gen_qemu_ld32u(ctx, t0, addr);
6a6ae23f 7709 tcg_gen_shli_tl(cpu_gpr[rD(ctx->opcode)], t0, 32);
76db3ba4
AJ
7710 gen_addr_add(ctx, addr, addr, 4);
7711 gen_qemu_ld32u(ctx, t0, addr);
6a6ae23f
AJ
7712 tcg_gen_or_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rD(ctx->opcode)], t0);
7713 tcg_temp_free(t0);
7714#else
76db3ba4
AJ
7715 gen_qemu_ld32u(ctx, cpu_gprh[rD(ctx->opcode)], addr);
7716 gen_addr_add(ctx, addr, addr, 4);
7717 gen_qemu_ld32u(ctx, cpu_gpr[rD(ctx->opcode)], addr);
6a6ae23f 7718#endif
0487d6a8 7719}
6a6ae23f 7720
636aa200 7721static inline void gen_op_evldh(DisasContext *ctx, TCGv addr)
6a6ae23f
AJ
7722{
7723 TCGv t0 = tcg_temp_new();
7724#if defined(TARGET_PPC64)
76db3ba4 7725 gen_qemu_ld16u(ctx, t0, addr);
6a6ae23f 7726 tcg_gen_shli_tl(cpu_gpr[rD(ctx->opcode)], t0, 48);
76db3ba4
AJ
7727 gen_addr_add(ctx, addr, addr, 2);
7728 gen_qemu_ld16u(ctx, t0, addr);
6a6ae23f
AJ
7729 tcg_gen_shli_tl(t0, t0, 32);
7730 tcg_gen_or_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rD(ctx->opcode)], t0);
76db3ba4
AJ
7731 gen_addr_add(ctx, addr, addr, 2);
7732 gen_qemu_ld16u(ctx, t0, addr);
6a6ae23f
AJ
7733 tcg_gen_shli_tl(t0, t0, 16);
7734 tcg_gen_or_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rD(ctx->opcode)], t0);
76db3ba4
AJ
7735 gen_addr_add(ctx, addr, addr, 2);
7736 gen_qemu_ld16u(ctx, t0, addr);
6a6ae23f 7737 tcg_gen_or_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rD(ctx->opcode)], t0);
0487d6a8 7738#else
76db3ba4 7739 gen_qemu_ld16u(ctx, t0, addr);
6a6ae23f 7740 tcg_gen_shli_tl(cpu_gprh[rD(ctx->opcode)], t0, 16);
76db3ba4
AJ
7741 gen_addr_add(ctx, addr, addr, 2);
7742 gen_qemu_ld16u(ctx, t0, addr);
6a6ae23f 7743 tcg_gen_or_tl(cpu_gprh[rD(ctx->opcode)], cpu_gprh[rD(ctx->opcode)], t0);
76db3ba4
AJ
7744 gen_addr_add(ctx, addr, addr, 2);
7745 gen_qemu_ld16u(ctx, t0, addr);
6a6ae23f 7746 tcg_gen_shli_tl(cpu_gprh[rD(ctx->opcode)], t0, 16);
76db3ba4
AJ
7747 gen_addr_add(ctx, addr, addr, 2);
7748 gen_qemu_ld16u(ctx, t0, addr);
6a6ae23f 7749 tcg_gen_or_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rD(ctx->opcode)], t0);
0487d6a8 7750#endif
6a6ae23f 7751 tcg_temp_free(t0);
0487d6a8
JM
7752}
7753
636aa200 7754static inline void gen_op_evlhhesplat(DisasContext *ctx, TCGv addr)
6a6ae23f
AJ
7755{
7756 TCGv t0 = tcg_temp_new();
76db3ba4 7757 gen_qemu_ld16u(ctx, t0, addr);
6a6ae23f
AJ
7758#if defined(TARGET_PPC64)
7759 tcg_gen_shli_tl(cpu_gpr[rD(ctx->opcode)], t0, 48);
7760 tcg_gen_shli_tl(t0, t0, 16);
7761 tcg_gen_or_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rD(ctx->opcode)], t0);
7762#else
7763 tcg_gen_shli_tl(t0, t0, 16);
7764 tcg_gen_mov_tl(cpu_gprh[rD(ctx->opcode)], t0);
7765 tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], t0);
7766#endif
7767 tcg_temp_free(t0);
0487d6a8
JM
7768}
7769
636aa200 7770static inline void gen_op_evlhhousplat(DisasContext *ctx, TCGv addr)
6a6ae23f
AJ
7771{
7772 TCGv t0 = tcg_temp_new();
76db3ba4 7773 gen_qemu_ld16u(ctx, t0, addr);
6a6ae23f
AJ
7774#if defined(TARGET_PPC64)
7775 tcg_gen_shli_tl(cpu_gpr[rD(ctx->opcode)], t0, 32);
7776 tcg_gen_or_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rD(ctx->opcode)], t0);
7777#else
7778 tcg_gen_mov_tl(cpu_gprh[rD(ctx->opcode)], t0);
7779 tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], t0);
7780#endif
7781 tcg_temp_free(t0);
0487d6a8
JM
7782}
7783
636aa200 7784static inline void gen_op_evlhhossplat(DisasContext *ctx, TCGv addr)
6a6ae23f
AJ
7785{
7786 TCGv t0 = tcg_temp_new();
76db3ba4 7787 gen_qemu_ld16s(ctx, t0, addr);
6a6ae23f
AJ
7788#if defined(TARGET_PPC64)
7789 tcg_gen_shli_tl(cpu_gpr[rD(ctx->opcode)], t0, 32);
7790 tcg_gen_ext32u_tl(t0, t0);
7791 tcg_gen_or_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rD(ctx->opcode)], t0);
7792#else
7793 tcg_gen_mov_tl(cpu_gprh[rD(ctx->opcode)], t0);
7794 tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], t0);
7795#endif
7796 tcg_temp_free(t0);
7797}
7798
636aa200 7799static inline void gen_op_evlwhe(DisasContext *ctx, TCGv addr)
6a6ae23f
AJ
7800{
7801 TCGv t0 = tcg_temp_new();
7802#if defined(TARGET_PPC64)
76db3ba4 7803 gen_qemu_ld16u(ctx, t0, addr);
6a6ae23f 7804 tcg_gen_shli_tl(cpu_gpr[rD(ctx->opcode)], t0, 48);
76db3ba4
AJ
7805 gen_addr_add(ctx, addr, addr, 2);
7806 gen_qemu_ld16u(ctx, t0, addr);
6a6ae23f
AJ
7807 tcg_gen_shli_tl(t0, t0, 16);
7808 tcg_gen_or_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rD(ctx->opcode)], t0);
7809#else
76db3ba4 7810 gen_qemu_ld16u(ctx, t0, addr);
6a6ae23f 7811 tcg_gen_shli_tl(cpu_gprh[rD(ctx->opcode)], t0, 16);
76db3ba4
AJ
7812 gen_addr_add(ctx, addr, addr, 2);
7813 gen_qemu_ld16u(ctx, t0, addr);
6a6ae23f
AJ
7814 tcg_gen_shli_tl(cpu_gpr[rD(ctx->opcode)], t0, 16);
7815#endif
7816 tcg_temp_free(t0);
7817}
7818
636aa200 7819static inline void gen_op_evlwhou(DisasContext *ctx, TCGv addr)
6a6ae23f
AJ
7820{
7821#if defined(TARGET_PPC64)
7822 TCGv t0 = tcg_temp_new();
76db3ba4
AJ
7823 gen_qemu_ld16u(ctx, cpu_gpr[rD(ctx->opcode)], addr);
7824 gen_addr_add(ctx, addr, addr, 2);
7825 gen_qemu_ld16u(ctx, t0, addr);
6a6ae23f
AJ
7826 tcg_gen_shli_tl(t0, t0, 32);
7827 tcg_gen_or_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rD(ctx->opcode)], t0);
7828 tcg_temp_free(t0);
7829#else
76db3ba4
AJ
7830 gen_qemu_ld16u(ctx, cpu_gprh[rD(ctx->opcode)], addr);
7831 gen_addr_add(ctx, addr, addr, 2);
7832 gen_qemu_ld16u(ctx, cpu_gpr[rD(ctx->opcode)], addr);
6a6ae23f
AJ
7833#endif
7834}
7835
636aa200 7836static inline void gen_op_evlwhos(DisasContext *ctx, TCGv addr)
6a6ae23f
AJ
7837{
7838#if defined(TARGET_PPC64)
7839 TCGv t0 = tcg_temp_new();
76db3ba4 7840 gen_qemu_ld16s(ctx, t0, addr);
6a6ae23f 7841 tcg_gen_ext32u_tl(cpu_gpr[rD(ctx->opcode)], t0);
76db3ba4
AJ
7842 gen_addr_add(ctx, addr, addr, 2);
7843 gen_qemu_ld16s(ctx, t0, addr);
6a6ae23f
AJ
7844 tcg_gen_shli_tl(t0, t0, 32);
7845 tcg_gen_or_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rD(ctx->opcode)], t0);
7846 tcg_temp_free(t0);
7847#else
76db3ba4
AJ
7848 gen_qemu_ld16s(ctx, cpu_gprh[rD(ctx->opcode)], addr);
7849 gen_addr_add(ctx, addr, addr, 2);
7850 gen_qemu_ld16s(ctx, cpu_gpr[rD(ctx->opcode)], addr);
6a6ae23f
AJ
7851#endif
7852}
7853
636aa200 7854static inline void gen_op_evlwwsplat(DisasContext *ctx, TCGv addr)
6a6ae23f
AJ
7855{
7856 TCGv t0 = tcg_temp_new();
76db3ba4 7857 gen_qemu_ld32u(ctx, t0, addr);
0487d6a8 7858#if defined(TARGET_PPC64)
6a6ae23f
AJ
7859 tcg_gen_shli_tl(cpu_gpr[rD(ctx->opcode)], t0, 32);
7860 tcg_gen_or_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rD(ctx->opcode)], t0);
7861#else
7862 tcg_gen_mov_tl(cpu_gprh[rD(ctx->opcode)], t0);
7863 tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], t0);
7864#endif
7865 tcg_temp_free(t0);
7866}
7867
636aa200 7868static inline void gen_op_evlwhsplat(DisasContext *ctx, TCGv addr)
6a6ae23f
AJ
7869{
7870 TCGv t0 = tcg_temp_new();
7871#if defined(TARGET_PPC64)
76db3ba4 7872 gen_qemu_ld16u(ctx, t0, addr);
6a6ae23f
AJ
7873 tcg_gen_shli_tl(cpu_gpr[rD(ctx->opcode)], t0, 48);
7874 tcg_gen_shli_tl(t0, t0, 32);
7875 tcg_gen_or_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rD(ctx->opcode)], t0);
76db3ba4
AJ
7876 gen_addr_add(ctx, addr, addr, 2);
7877 gen_qemu_ld16u(ctx, t0, addr);
6a6ae23f
AJ
7878 tcg_gen_or_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rD(ctx->opcode)], t0);
7879 tcg_gen_shli_tl(t0, t0, 16);
7880 tcg_gen_or_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rD(ctx->opcode)], t0);
7881#else
76db3ba4 7882 gen_qemu_ld16u(ctx, t0, addr);
6a6ae23f
AJ
7883 tcg_gen_shli_tl(cpu_gprh[rD(ctx->opcode)], t0, 16);
7884 tcg_gen_or_tl(cpu_gprh[rD(ctx->opcode)], cpu_gprh[rD(ctx->opcode)], t0);
76db3ba4
AJ
7885 gen_addr_add(ctx, addr, addr, 2);
7886 gen_qemu_ld16u(ctx, t0, addr);
6a6ae23f
AJ
7887 tcg_gen_shli_tl(cpu_gpr[rD(ctx->opcode)], t0, 16);
7888 tcg_gen_or_tl(cpu_gpr[rD(ctx->opcode)], cpu_gprh[rD(ctx->opcode)], t0);
0487d6a8 7889#endif
6a6ae23f
AJ
7890 tcg_temp_free(t0);
7891}
7892
636aa200 7893static inline void gen_op_evstdd(DisasContext *ctx, TCGv addr)
6a6ae23f
AJ
7894{
7895#if defined(TARGET_PPC64)
76db3ba4 7896 gen_qemu_st64(ctx, cpu_gpr[rS(ctx->opcode)], addr);
0487d6a8 7897#else
6a6ae23f
AJ
7898 TCGv_i64 t0 = tcg_temp_new_i64();
7899 tcg_gen_concat_i32_i64(t0, cpu_gpr[rS(ctx->opcode)], cpu_gprh[rS(ctx->opcode)]);
76db3ba4 7900 gen_qemu_st64(ctx, t0, addr);
6a6ae23f
AJ
7901 tcg_temp_free_i64(t0);
7902#endif
7903}
7904
636aa200 7905static inline void gen_op_evstdw(DisasContext *ctx, TCGv addr)
6a6ae23f 7906{
0487d6a8 7907#if defined(TARGET_PPC64)
6a6ae23f
AJ
7908 TCGv t0 = tcg_temp_new();
7909 tcg_gen_shri_tl(t0, cpu_gpr[rS(ctx->opcode)], 32);
76db3ba4 7910 gen_qemu_st32(ctx, t0, addr);
6a6ae23f
AJ
7911 tcg_temp_free(t0);
7912#else
76db3ba4 7913 gen_qemu_st32(ctx, cpu_gprh[rS(ctx->opcode)], addr);
6a6ae23f 7914#endif
76db3ba4
AJ
7915 gen_addr_add(ctx, addr, addr, 4);
7916 gen_qemu_st32(ctx, cpu_gpr[rS(ctx->opcode)], addr);
6a6ae23f
AJ
7917}
7918
636aa200 7919static inline void gen_op_evstdh(DisasContext *ctx, TCGv addr)
6a6ae23f
AJ
7920{
7921 TCGv t0 = tcg_temp_new();
7922#if defined(TARGET_PPC64)
7923 tcg_gen_shri_tl(t0, cpu_gpr[rS(ctx->opcode)], 48);
7924#else
7925 tcg_gen_shri_tl(t0, cpu_gprh[rS(ctx->opcode)], 16);
7926#endif
76db3ba4
AJ
7927 gen_qemu_st16(ctx, t0, addr);
7928 gen_addr_add(ctx, addr, addr, 2);
6a6ae23f
AJ
7929#if defined(TARGET_PPC64)
7930 tcg_gen_shri_tl(t0, cpu_gpr[rS(ctx->opcode)], 32);
76db3ba4 7931 gen_qemu_st16(ctx, t0, addr);
6a6ae23f 7932#else
76db3ba4 7933 gen_qemu_st16(ctx, cpu_gprh[rS(ctx->opcode)], addr);
6a6ae23f 7934#endif
76db3ba4 7935 gen_addr_add(ctx, addr, addr, 2);
6a6ae23f 7936 tcg_gen_shri_tl(t0, cpu_gpr[rS(ctx->opcode)], 16);
76db3ba4 7937 gen_qemu_st16(ctx, t0, addr);
6a6ae23f 7938 tcg_temp_free(t0);
76db3ba4
AJ
7939 gen_addr_add(ctx, addr, addr, 2);
7940 gen_qemu_st16(ctx, cpu_gpr[rS(ctx->opcode)], addr);
6a6ae23f
AJ
7941}
7942
636aa200 7943static inline void gen_op_evstwhe(DisasContext *ctx, TCGv addr)
6a6ae23f
AJ
7944{
7945 TCGv t0 = tcg_temp_new();
7946#if defined(TARGET_PPC64)
7947 tcg_gen_shri_tl(t0, cpu_gpr[rS(ctx->opcode)], 48);
7948#else
7949 tcg_gen_shri_tl(t0, cpu_gprh[rS(ctx->opcode)], 16);
7950#endif
76db3ba4
AJ
7951 gen_qemu_st16(ctx, t0, addr);
7952 gen_addr_add(ctx, addr, addr, 2);
6a6ae23f 7953 tcg_gen_shri_tl(t0, cpu_gpr[rS(ctx->opcode)], 16);
76db3ba4 7954 gen_qemu_st16(ctx, t0, addr);
6a6ae23f
AJ
7955 tcg_temp_free(t0);
7956}
7957
636aa200 7958static inline void gen_op_evstwho(DisasContext *ctx, TCGv addr)
6a6ae23f
AJ
7959{
7960#if defined(TARGET_PPC64)
7961 TCGv t0 = tcg_temp_new();
7962 tcg_gen_shri_tl(t0, cpu_gpr[rS(ctx->opcode)], 32);
76db3ba4 7963 gen_qemu_st16(ctx, t0, addr);
6a6ae23f
AJ
7964 tcg_temp_free(t0);
7965#else
76db3ba4 7966 gen_qemu_st16(ctx, cpu_gprh[rS(ctx->opcode)], addr);
6a6ae23f 7967#endif
76db3ba4
AJ
7968 gen_addr_add(ctx, addr, addr, 2);
7969 gen_qemu_st16(ctx, cpu_gpr[rS(ctx->opcode)], addr);
6a6ae23f
AJ
7970}
7971
636aa200 7972static inline void gen_op_evstwwe(DisasContext *ctx, TCGv addr)
6a6ae23f
AJ
7973{
7974#if defined(TARGET_PPC64)
7975 TCGv t0 = tcg_temp_new();
7976 tcg_gen_shri_tl(t0, cpu_gpr[rS(ctx->opcode)], 32);
76db3ba4 7977 gen_qemu_st32(ctx, t0, addr);
6a6ae23f
AJ
7978 tcg_temp_free(t0);
7979#else
76db3ba4 7980 gen_qemu_st32(ctx, cpu_gprh[rS(ctx->opcode)], addr);
6a6ae23f
AJ
7981#endif
7982}
7983
636aa200 7984static inline void gen_op_evstwwo(DisasContext *ctx, TCGv addr)
6a6ae23f 7985{
76db3ba4 7986 gen_qemu_st32(ctx, cpu_gpr[rS(ctx->opcode)], addr);
6a6ae23f
AJ
7987}
7988
7989#define GEN_SPEOP_LDST(name, opc2, sh) \
99e300ef 7990static void glue(gen_, name)(DisasContext *ctx) \
6a6ae23f
AJ
7991{ \
7992 TCGv t0; \
7993 if (unlikely(!ctx->spe_enabled)) { \
27a69bb0 7994 gen_exception(ctx, POWERPC_EXCP_SPEU); \
6a6ae23f
AJ
7995 return; \
7996 } \
76db3ba4 7997 gen_set_access_type(ctx, ACCESS_INT); \
6a6ae23f
AJ
7998 t0 = tcg_temp_new(); \
7999 if (Rc(ctx->opcode)) { \
76db3ba4 8000 gen_addr_spe_imm_index(ctx, t0, sh); \
6a6ae23f 8001 } else { \
76db3ba4 8002 gen_addr_reg_index(ctx, t0); \
6a6ae23f
AJ
8003 } \
8004 gen_op_##name(ctx, t0); \
8005 tcg_temp_free(t0); \
8006}
8007
8008GEN_SPEOP_LDST(evldd, 0x00, 3);
8009GEN_SPEOP_LDST(evldw, 0x01, 3);
8010GEN_SPEOP_LDST(evldh, 0x02, 3);
8011GEN_SPEOP_LDST(evlhhesplat, 0x04, 1);
8012GEN_SPEOP_LDST(evlhhousplat, 0x06, 1);
8013GEN_SPEOP_LDST(evlhhossplat, 0x07, 1);
8014GEN_SPEOP_LDST(evlwhe, 0x08, 2);
8015GEN_SPEOP_LDST(evlwhou, 0x0A, 2);
8016GEN_SPEOP_LDST(evlwhos, 0x0B, 2);
8017GEN_SPEOP_LDST(evlwwsplat, 0x0C, 2);
8018GEN_SPEOP_LDST(evlwhsplat, 0x0E, 2);
8019
8020GEN_SPEOP_LDST(evstdd, 0x10, 3);
8021GEN_SPEOP_LDST(evstdw, 0x11, 3);
8022GEN_SPEOP_LDST(evstdh, 0x12, 3);
8023GEN_SPEOP_LDST(evstwhe, 0x18, 2);
8024GEN_SPEOP_LDST(evstwho, 0x1A, 2);
8025GEN_SPEOP_LDST(evstwwe, 0x1C, 2);
8026GEN_SPEOP_LDST(evstwwo, 0x1E, 2);
0487d6a8
JM
8027
8028/* Multiply and add - TODO */
8029#if 0
70560da7
FC
8030GEN_SPE(speundef, evmhessf, 0x01, 0x10, 0xFFFFFFFF, 0x00000000, PPC_SPE);//
8031GEN_SPE(speundef, evmhossf, 0x03, 0x10, 0xFFFFFFFF, 0x00000000, PPC_SPE);
8032GEN_SPE(evmheumi, evmhesmi, 0x04, 0x10, 0x00000000, 0x00000000, PPC_SPE);
8033GEN_SPE(speundef, evmhesmf, 0x05, 0x10, 0xFFFFFFFF, 0x00000000, PPC_SPE);
8034GEN_SPE(evmhoumi, evmhosmi, 0x06, 0x10, 0x00000000, 0x00000000, PPC_SPE);
8035GEN_SPE(speundef, evmhosmf, 0x07, 0x10, 0xFFFFFFFF, 0x00000000, PPC_SPE);
8036GEN_SPE(speundef, evmhessfa, 0x11, 0x10, 0xFFFFFFFF, 0x00000000, PPC_SPE);
8037GEN_SPE(speundef, evmhossfa, 0x13, 0x10, 0xFFFFFFFF, 0x00000000, PPC_SPE);
8038GEN_SPE(evmheumia, evmhesmia, 0x14, 0x10, 0x00000000, 0x00000000, PPC_SPE);
8039GEN_SPE(speundef, evmhesmfa, 0x15, 0x10, 0xFFFFFFFF, 0x00000000, PPC_SPE);
8040GEN_SPE(evmhoumia, evmhosmia, 0x16, 0x10, 0x00000000, 0x00000000, PPC_SPE);
8041GEN_SPE(speundef, evmhosmfa, 0x17, 0x10, 0xFFFFFFFF, 0x00000000, PPC_SPE);
8042
8043GEN_SPE(speundef, evmwhssf, 0x03, 0x11, 0xFFFFFFFF, 0x00000000, PPC_SPE);
8044GEN_SPE(evmwlumi, speundef, 0x04, 0x11, 0x00000000, 0xFFFFFFFF, PPC_SPE);
8045GEN_SPE(evmwhumi, evmwhsmi, 0x06, 0x11, 0x00000000, 0x00000000, PPC_SPE);
8046GEN_SPE(speundef, evmwhsmf, 0x07, 0x11, 0xFFFFFFFF, 0x00000000, PPC_SPE);
8047GEN_SPE(speundef, evmwssf, 0x09, 0x11, 0xFFFFFFFF, 0x00000000, PPC_SPE);
8048GEN_SPE(speundef, evmwsmf, 0x0D, 0x11, 0xFFFFFFFF, 0x00000000, PPC_SPE);
8049GEN_SPE(speundef, evmwhssfa, 0x13, 0x11, 0xFFFFFFFF, 0x00000000, PPC_SPE);
8050GEN_SPE(evmwlumia, speundef, 0x14, 0x11, 0x00000000, 0xFFFFFFFF, PPC_SPE);
8051GEN_SPE(evmwhumia, evmwhsmia, 0x16, 0x11, 0x00000000, 0x00000000, PPC_SPE);
8052GEN_SPE(speundef, evmwhsmfa, 0x17, 0x11, 0xFFFFFFFF, 0x00000000, PPC_SPE);
8053GEN_SPE(speundef, evmwssfa, 0x19, 0x11, 0xFFFFFFFF, 0x00000000, PPC_SPE);
8054GEN_SPE(speundef, evmwsmfa, 0x1D, 0x11, 0xFFFFFFFF, 0x00000000, PPC_SPE);
8055
8056GEN_SPE(evadduiaaw, evaddsiaaw, 0x00, 0x13, 0x0000F800, 0x0000F800, PPC_SPE);
8057GEN_SPE(evsubfusiaaw, evsubfssiaaw, 0x01, 0x13, 0x0000F800, 0x0000F800, PPC_SPE);
8058GEN_SPE(evaddumiaaw, evaddsmiaaw, 0x04, 0x13, 0x0000F800, 0x0000F800, PPC_SPE);
8059GEN_SPE(evsubfumiaaw, evsubfsmiaaw, 0x05, 0x13, 0x0000F800, 0x0000F800, PPC_SPE);
8060GEN_SPE(evdivws, evdivwu, 0x06, 0x13, 0x00000000, 0x00000000, PPC_SPE);
8061
8062GEN_SPE(evmheusiaaw, evmhessiaaw, 0x00, 0x14, 0x00000000, 0x00000000, PPC_SPE);
8063GEN_SPE(speundef, evmhessfaaw, 0x01, 0x14, 0xFFFFFFFF, 0x00000000, PPC_SPE);
8064GEN_SPE(evmhousiaaw, evmhossiaaw, 0x02, 0x14, 0x00000000, 0x00000000, PPC_SPE);
8065GEN_SPE(speundef, evmhossfaaw, 0x03, 0x14, 0xFFFFFFFF, 0x00000000, PPC_SPE);
8066GEN_SPE(evmheumiaaw, evmhesmiaaw, 0x04, 0x14, 0x00000000, 0x00000000, PPC_SPE);
8067GEN_SPE(speundef, evmhesmfaaw, 0x05, 0x14, 0xFFFFFFFF, 0x00000000, PPC_SPE);
8068GEN_SPE(evmhoumiaaw, evmhosmiaaw, 0x06, 0x14, 0x00000000, 0x00000000, PPC_SPE);
8069GEN_SPE(speundef, evmhosmfaaw, 0x07, 0x14, 0xFFFFFFFF, 0x00000000, PPC_SPE);
8070GEN_SPE(evmhegumiaa, evmhegsmiaa, 0x14, 0x14, 0x00000000, 0x00000000, PPC_SPE);
8071GEN_SPE(speundef, evmhegsmfaa, 0x15, 0x14, 0xFFFFFFFF, 0x00000000, PPC_SPE);
8072GEN_SPE(evmhogumiaa, evmhogsmiaa, 0x16, 0x14, 0x00000000, 0x00000000, PPC_SPE);
8073GEN_SPE(speundef, evmhogsmfaa, 0x17, 0x14, 0xFFFFFFFF, 0x00000000, PPC_SPE);
8074
8075GEN_SPE(evmwlusiaaw, evmwlssiaaw, 0x00, 0x15, 0x00000000, 0x00000000, PPC_SPE);
8076GEN_SPE(evmwlumiaaw, evmwlsmiaaw, 0x04, 0x15, 0x00000000, 0x00000000, PPC_SPE);
8077GEN_SPE(speundef, evmwssfaa, 0x09, 0x15, 0xFFFFFFFF, 0x00000000, PPC_SPE);
8078GEN_SPE(speundef, evmwsmfaa, 0x0D, 0x15, 0xFFFFFFFF, 0x00000000, PPC_SPE);
8079
8080GEN_SPE(evmheusianw, evmhessianw, 0x00, 0x16, 0x00000000, 0x00000000, PPC_SPE);
8081GEN_SPE(speundef, evmhessfanw, 0x01, 0x16, 0xFFFFFFFF, 0x00000000, PPC_SPE);
8082GEN_SPE(evmhousianw, evmhossianw, 0x02, 0x16, 0x00000000, 0x00000000, PPC_SPE);
8083GEN_SPE(speundef, evmhossfanw, 0x03, 0x16, 0xFFFFFFFF, 0x00000000, PPC_SPE);
8084GEN_SPE(evmheumianw, evmhesmianw, 0x04, 0x16, 0x00000000, 0x00000000, PPC_SPE);
8085GEN_SPE(speundef, evmhesmfanw, 0x05, 0x16, 0xFFFFFFFF, 0x00000000, PPC_SPE);
8086GEN_SPE(evmhoumianw, evmhosmianw, 0x06, 0x16, 0x00000000, 0x00000000, PPC_SPE);
8087GEN_SPE(speundef, evmhosmfanw, 0x07, 0x16, 0xFFFFFFFF, 0x00000000, PPC_SPE);
8088GEN_SPE(evmhegumian, evmhegsmian, 0x14, 0x16, 0x00000000, 0x00000000, PPC_SPE);
8089GEN_SPE(speundef, evmhegsmfan, 0x15, 0x16, 0xFFFFFFFF, 0x00000000, PPC_SPE);
8090GEN_SPE(evmhigumian, evmhigsmian, 0x16, 0x16, 0x00000000, 0x00000000, PPC_SPE);
8091GEN_SPE(speundef, evmhogsmfan, 0x17, 0x16, 0xFFFFFFFF, 0x00000000, PPC_SPE);
8092
8093GEN_SPE(evmwlusianw, evmwlssianw, 0x00, 0x17, 0x00000000, 0x00000000, PPC_SPE);
8094GEN_SPE(evmwlumianw, evmwlsmianw, 0x04, 0x17, 0x00000000, 0x00000000, PPC_SPE);
8095GEN_SPE(speundef, evmwssfan, 0x09, 0x17, 0xFFFFFFFF, 0x00000000, PPC_SPE);
8096GEN_SPE(evmwumian, evmwsmian, 0x0C, 0x17, 0x00000000, 0x00000000, PPC_SPE);
8097GEN_SPE(speundef, evmwsmfan, 0x0D, 0x17, 0xFFFFFFFF, 0x00000000, PPC_SPE);
0487d6a8
JM
8098#endif
8099
8100/*** SPE floating-point extension ***/
1c97856d
AJ
8101#if defined(TARGET_PPC64)
8102#define GEN_SPEFPUOP_CONV_32_32(name) \
636aa200 8103static inline void gen_##name(DisasContext *ctx) \
0487d6a8 8104{ \
1c97856d
AJ
8105 TCGv_i32 t0; \
8106 TCGv t1; \
8107 t0 = tcg_temp_new_i32(); \
8108 tcg_gen_trunc_tl_i32(t0, cpu_gpr[rB(ctx->opcode)]); \
8e703949 8109 gen_helper_##name(t0, cpu_env, t0); \
1c97856d
AJ
8110 t1 = tcg_temp_new(); \
8111 tcg_gen_extu_i32_tl(t1, t0); \
8112 tcg_temp_free_i32(t0); \
8113 tcg_gen_andi_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rD(ctx->opcode)], \
8114 0xFFFFFFFF00000000ULL); \
8115 tcg_gen_or_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rD(ctx->opcode)], t1); \
8116 tcg_temp_free(t1); \
0487d6a8 8117}
1c97856d 8118#define GEN_SPEFPUOP_CONV_32_64(name) \
636aa200 8119static inline void gen_##name(DisasContext *ctx) \
1c97856d
AJ
8120{ \
8121 TCGv_i32 t0; \
8122 TCGv t1; \
8123 t0 = tcg_temp_new_i32(); \
8e703949 8124 gen_helper_##name(t0, cpu_env, cpu_gpr[rB(ctx->opcode)]); \
1c97856d
AJ
8125 t1 = tcg_temp_new(); \
8126 tcg_gen_extu_i32_tl(t1, t0); \
8127 tcg_temp_free_i32(t0); \
8128 tcg_gen_andi_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rD(ctx->opcode)], \
8129 0xFFFFFFFF00000000ULL); \
8130 tcg_gen_or_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rD(ctx->opcode)], t1); \
8131 tcg_temp_free(t1); \
8132}
8133#define GEN_SPEFPUOP_CONV_64_32(name) \
636aa200 8134static inline void gen_##name(DisasContext *ctx) \
1c97856d
AJ
8135{ \
8136 TCGv_i32 t0 = tcg_temp_new_i32(); \
8137 tcg_gen_trunc_tl_i32(t0, cpu_gpr[rB(ctx->opcode)]); \
8e703949 8138 gen_helper_##name(cpu_gpr[rD(ctx->opcode)], cpu_env, t0); \
1c97856d
AJ
8139 tcg_temp_free_i32(t0); \
8140}
8141#define GEN_SPEFPUOP_CONV_64_64(name) \
636aa200 8142static inline void gen_##name(DisasContext *ctx) \
1c97856d 8143{ \
8e703949
BS
8144 gen_helper_##name(cpu_gpr[rD(ctx->opcode)], cpu_env, \
8145 cpu_gpr[rB(ctx->opcode)]); \
1c97856d
AJ
8146}
8147#define GEN_SPEFPUOP_ARITH2_32_32(name) \
636aa200 8148static inline void gen_##name(DisasContext *ctx) \
57951c27 8149{ \
1c97856d
AJ
8150 TCGv_i32 t0, t1; \
8151 TCGv_i64 t2; \
57951c27 8152 if (unlikely(!ctx->spe_enabled)) { \
27a69bb0 8153 gen_exception(ctx, POWERPC_EXCP_SPEU); \
57951c27
AJ
8154 return; \
8155 } \
1c97856d
AJ
8156 t0 = tcg_temp_new_i32(); \
8157 t1 = tcg_temp_new_i32(); \
8158 tcg_gen_trunc_tl_i32(t0, cpu_gpr[rA(ctx->opcode)]); \
8159 tcg_gen_trunc_tl_i32(t1, cpu_gpr[rB(ctx->opcode)]); \
8e703949 8160 gen_helper_##name(t0, cpu_env, t0, t1); \
1c97856d
AJ
8161 tcg_temp_free_i32(t1); \
8162 t2 = tcg_temp_new(); \
8163 tcg_gen_extu_i32_tl(t2, t0); \
8164 tcg_temp_free_i32(t0); \
8165 tcg_gen_andi_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rD(ctx->opcode)], \
8166 0xFFFFFFFF00000000ULL); \
8167 tcg_gen_or_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rD(ctx->opcode)], t2); \
8168 tcg_temp_free(t2); \
57951c27 8169}
1c97856d 8170#define GEN_SPEFPUOP_ARITH2_64_64(name) \
636aa200 8171static inline void gen_##name(DisasContext *ctx) \
57951c27
AJ
8172{ \
8173 if (unlikely(!ctx->spe_enabled)) { \
27a69bb0 8174 gen_exception(ctx, POWERPC_EXCP_SPEU); \
57951c27
AJ
8175 return; \
8176 } \
8e703949
BS
8177 gen_helper_##name(cpu_gpr[rD(ctx->opcode)], cpu_env, \
8178 cpu_gpr[rA(ctx->opcode)], cpu_gpr[rB(ctx->opcode)]); \
57951c27 8179}
1c97856d 8180#define GEN_SPEFPUOP_COMP_32(name) \
636aa200 8181static inline void gen_##name(DisasContext *ctx) \
57951c27 8182{ \
1c97856d 8183 TCGv_i32 t0, t1; \
57951c27 8184 if (unlikely(!ctx->spe_enabled)) { \
27a69bb0 8185 gen_exception(ctx, POWERPC_EXCP_SPEU); \
57951c27
AJ
8186 return; \
8187 } \
1c97856d
AJ
8188 t0 = tcg_temp_new_i32(); \
8189 t1 = tcg_temp_new_i32(); \
8190 tcg_gen_trunc_tl_i32(t0, cpu_gpr[rA(ctx->opcode)]); \
8191 tcg_gen_trunc_tl_i32(t1, cpu_gpr[rB(ctx->opcode)]); \
8e703949 8192 gen_helper_##name(cpu_crf[crfD(ctx->opcode)], cpu_env, t0, t1); \
1c97856d
AJ
8193 tcg_temp_free_i32(t0); \
8194 tcg_temp_free_i32(t1); \
8195}
8196#define GEN_SPEFPUOP_COMP_64(name) \
636aa200 8197static inline void gen_##name(DisasContext *ctx) \
1c97856d
AJ
8198{ \
8199 if (unlikely(!ctx->spe_enabled)) { \
27a69bb0 8200 gen_exception(ctx, POWERPC_EXCP_SPEU); \
1c97856d
AJ
8201 return; \
8202 } \
8e703949 8203 gen_helper_##name(cpu_crf[crfD(ctx->opcode)], cpu_env, \
1c97856d
AJ
8204 cpu_gpr[rA(ctx->opcode)], cpu_gpr[rB(ctx->opcode)]); \
8205}
8206#else
8207#define GEN_SPEFPUOP_CONV_32_32(name) \
636aa200 8208static inline void gen_##name(DisasContext *ctx) \
1c97856d 8209{ \
8e703949
BS
8210 gen_helper_##name(cpu_gpr[rD(ctx->opcode)], cpu_env, \
8211 cpu_gpr[rB(ctx->opcode)]); \
57951c27 8212}
1c97856d 8213#define GEN_SPEFPUOP_CONV_32_64(name) \
636aa200 8214static inline void gen_##name(DisasContext *ctx) \
1c97856d
AJ
8215{ \
8216 TCGv_i64 t0 = tcg_temp_new_i64(); \
8217 gen_load_gpr64(t0, rB(ctx->opcode)); \
8e703949 8218 gen_helper_##name(cpu_gpr[rD(ctx->opcode)], cpu_env, t0); \
1c97856d
AJ
8219 tcg_temp_free_i64(t0); \
8220}
8221#define GEN_SPEFPUOP_CONV_64_32(name) \
636aa200 8222static inline void gen_##name(DisasContext *ctx) \
1c97856d
AJ
8223{ \
8224 TCGv_i64 t0 = tcg_temp_new_i64(); \
8e703949 8225 gen_helper_##name(t0, cpu_env, cpu_gpr[rB(ctx->opcode)]); \
1c97856d
AJ
8226 gen_store_gpr64(rD(ctx->opcode), t0); \
8227 tcg_temp_free_i64(t0); \
8228}
8229#define GEN_SPEFPUOP_CONV_64_64(name) \
636aa200 8230static inline void gen_##name(DisasContext *ctx) \
1c97856d
AJ
8231{ \
8232 TCGv_i64 t0 = tcg_temp_new_i64(); \
8233 gen_load_gpr64(t0, rB(ctx->opcode)); \
8e703949 8234 gen_helper_##name(t0, cpu_env, t0); \
1c97856d
AJ
8235 gen_store_gpr64(rD(ctx->opcode), t0); \
8236 tcg_temp_free_i64(t0); \
8237}
8238#define GEN_SPEFPUOP_ARITH2_32_32(name) \
636aa200 8239static inline void gen_##name(DisasContext *ctx) \
1c97856d
AJ
8240{ \
8241 if (unlikely(!ctx->spe_enabled)) { \
27a69bb0 8242 gen_exception(ctx, POWERPC_EXCP_SPEU); \
1c97856d
AJ
8243 return; \
8244 } \
8e703949 8245 gen_helper_##name(cpu_gpr[rD(ctx->opcode)], cpu_env, \
1c97856d
AJ
8246 cpu_gpr[rA(ctx->opcode)], cpu_gpr[rB(ctx->opcode)]); \
8247}
8248#define GEN_SPEFPUOP_ARITH2_64_64(name) \
636aa200 8249static inline void gen_##name(DisasContext *ctx) \
1c97856d
AJ
8250{ \
8251 TCGv_i64 t0, t1; \
8252 if (unlikely(!ctx->spe_enabled)) { \
27a69bb0 8253 gen_exception(ctx, POWERPC_EXCP_SPEU); \
1c97856d
AJ
8254 return; \
8255 } \
8256 t0 = tcg_temp_new_i64(); \
8257 t1 = tcg_temp_new_i64(); \
8258 gen_load_gpr64(t0, rA(ctx->opcode)); \
8259 gen_load_gpr64(t1, rB(ctx->opcode)); \
8e703949 8260 gen_helper_##name(t0, cpu_env, t0, t1); \
1c97856d
AJ
8261 gen_store_gpr64(rD(ctx->opcode), t0); \
8262 tcg_temp_free_i64(t0); \
8263 tcg_temp_free_i64(t1); \
8264}
8265#define GEN_SPEFPUOP_COMP_32(name) \
636aa200 8266static inline void gen_##name(DisasContext *ctx) \
1c97856d
AJ
8267{ \
8268 if (unlikely(!ctx->spe_enabled)) { \
27a69bb0 8269 gen_exception(ctx, POWERPC_EXCP_SPEU); \
1c97856d
AJ
8270 return; \
8271 } \
8e703949 8272 gen_helper_##name(cpu_crf[crfD(ctx->opcode)], cpu_env, \
1c97856d
AJ
8273 cpu_gpr[rA(ctx->opcode)], cpu_gpr[rB(ctx->opcode)]); \
8274}
8275#define GEN_SPEFPUOP_COMP_64(name) \
636aa200 8276static inline void gen_##name(DisasContext *ctx) \
1c97856d
AJ
8277{ \
8278 TCGv_i64 t0, t1; \
8279 if (unlikely(!ctx->spe_enabled)) { \
27a69bb0 8280 gen_exception(ctx, POWERPC_EXCP_SPEU); \
1c97856d
AJ
8281 return; \
8282 } \
8283 t0 = tcg_temp_new_i64(); \
8284 t1 = tcg_temp_new_i64(); \
8285 gen_load_gpr64(t0, rA(ctx->opcode)); \
8286 gen_load_gpr64(t1, rB(ctx->opcode)); \
8e703949 8287 gen_helper_##name(cpu_crf[crfD(ctx->opcode)], cpu_env, t0, t1); \
1c97856d
AJ
8288 tcg_temp_free_i64(t0); \
8289 tcg_temp_free_i64(t1); \
8290}
8291#endif
57951c27 8292
0487d6a8
JM
8293/* Single precision floating-point vectors operations */
8294/* Arithmetic */
1c97856d
AJ
8295GEN_SPEFPUOP_ARITH2_64_64(evfsadd);
8296GEN_SPEFPUOP_ARITH2_64_64(evfssub);
8297GEN_SPEFPUOP_ARITH2_64_64(evfsmul);
8298GEN_SPEFPUOP_ARITH2_64_64(evfsdiv);
636aa200 8299static inline void gen_evfsabs(DisasContext *ctx)
1c97856d
AJ
8300{
8301 if (unlikely(!ctx->spe_enabled)) {
27a69bb0 8302 gen_exception(ctx, POWERPC_EXCP_SPEU);
1c97856d
AJ
8303 return;
8304 }
8305#if defined(TARGET_PPC64)
6d5c34fa 8306 tcg_gen_andi_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)], ~0x8000000080000000LL);
1c97856d 8307#else
6d5c34fa
MP
8308 tcg_gen_andi_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)], ~0x80000000);
8309 tcg_gen_andi_tl(cpu_gprh[rD(ctx->opcode)], cpu_gprh[rA(ctx->opcode)], ~0x80000000);
1c97856d
AJ
8310#endif
8311}
636aa200 8312static inline void gen_evfsnabs(DisasContext *ctx)
1c97856d
AJ
8313{
8314 if (unlikely(!ctx->spe_enabled)) {
27a69bb0 8315 gen_exception(ctx, POWERPC_EXCP_SPEU);
1c97856d
AJ
8316 return;
8317 }
8318#if defined(TARGET_PPC64)
6d5c34fa 8319 tcg_gen_ori_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)], 0x8000000080000000LL);
1c97856d 8320#else
6d5c34fa
MP
8321 tcg_gen_ori_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)], 0x80000000);
8322 tcg_gen_ori_tl(cpu_gprh[rD(ctx->opcode)], cpu_gprh[rA(ctx->opcode)], 0x80000000);
1c97856d
AJ
8323#endif
8324}
636aa200 8325static inline void gen_evfsneg(DisasContext *ctx)
1c97856d
AJ
8326{
8327 if (unlikely(!ctx->spe_enabled)) {
27a69bb0 8328 gen_exception(ctx, POWERPC_EXCP_SPEU);
1c97856d
AJ
8329 return;
8330 }
8331#if defined(TARGET_PPC64)
6d5c34fa 8332 tcg_gen_xori_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)], 0x8000000080000000LL);
1c97856d 8333#else
6d5c34fa
MP
8334 tcg_gen_xori_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)], 0x80000000);
8335 tcg_gen_xori_tl(cpu_gprh[rD(ctx->opcode)], cpu_gprh[rA(ctx->opcode)], 0x80000000);
1c97856d
AJ
8336#endif
8337}
8338
0487d6a8 8339/* Conversion */
1c97856d
AJ
8340GEN_SPEFPUOP_CONV_64_64(evfscfui);
8341GEN_SPEFPUOP_CONV_64_64(evfscfsi);
8342GEN_SPEFPUOP_CONV_64_64(evfscfuf);
8343GEN_SPEFPUOP_CONV_64_64(evfscfsf);
8344GEN_SPEFPUOP_CONV_64_64(evfsctui);
8345GEN_SPEFPUOP_CONV_64_64(evfsctsi);
8346GEN_SPEFPUOP_CONV_64_64(evfsctuf);
8347GEN_SPEFPUOP_CONV_64_64(evfsctsf);
8348GEN_SPEFPUOP_CONV_64_64(evfsctuiz);
8349GEN_SPEFPUOP_CONV_64_64(evfsctsiz);
8350
0487d6a8 8351/* Comparison */
1c97856d
AJ
8352GEN_SPEFPUOP_COMP_64(evfscmpgt);
8353GEN_SPEFPUOP_COMP_64(evfscmplt);
8354GEN_SPEFPUOP_COMP_64(evfscmpeq);
8355GEN_SPEFPUOP_COMP_64(evfststgt);
8356GEN_SPEFPUOP_COMP_64(evfststlt);
8357GEN_SPEFPUOP_COMP_64(evfststeq);
0487d6a8
JM
8358
8359/* Opcodes definitions */
70560da7
FC
8360GEN_SPE(evfsadd, evfssub, 0x00, 0x0A, 0x00000000, 0x00000000, PPC_SPE_SINGLE); //
8361GEN_SPE(evfsabs, evfsnabs, 0x02, 0x0A, 0x0000F800, 0x0000F800, PPC_SPE_SINGLE); //
8362GEN_SPE(evfsneg, speundef, 0x03, 0x0A, 0x0000F800, 0xFFFFFFFF, PPC_SPE_SINGLE); //
8363GEN_SPE(evfsmul, evfsdiv, 0x04, 0x0A, 0x00000000, 0x00000000, PPC_SPE_SINGLE); //
8364GEN_SPE(evfscmpgt, evfscmplt, 0x06, 0x0A, 0x00600000, 0x00600000, PPC_SPE_SINGLE); //
8365GEN_SPE(evfscmpeq, speundef, 0x07, 0x0A, 0x00600000, 0xFFFFFFFF, PPC_SPE_SINGLE); //
8366GEN_SPE(evfscfui, evfscfsi, 0x08, 0x0A, 0x00180000, 0x00180000, PPC_SPE_SINGLE); //
8367GEN_SPE(evfscfuf, evfscfsf, 0x09, 0x0A, 0x00180000, 0x00180000, PPC_SPE_SINGLE); //
8368GEN_SPE(evfsctui, evfsctsi, 0x0A, 0x0A, 0x00180000, 0x00180000, PPC_SPE_SINGLE); //
8369GEN_SPE(evfsctuf, evfsctsf, 0x0B, 0x0A, 0x00180000, 0x00180000, PPC_SPE_SINGLE); //
8370GEN_SPE(evfsctuiz, speundef, 0x0C, 0x0A, 0x00180000, 0xFFFFFFFF, PPC_SPE_SINGLE); //
8371GEN_SPE(evfsctsiz, speundef, 0x0D, 0x0A, 0x00180000, 0xFFFFFFFF, PPC_SPE_SINGLE); //
8372GEN_SPE(evfststgt, evfststlt, 0x0E, 0x0A, 0x00600000, 0x00600000, PPC_SPE_SINGLE); //
8373GEN_SPE(evfststeq, speundef, 0x0F, 0x0A, 0x00600000, 0xFFFFFFFF, PPC_SPE_SINGLE); //
0487d6a8
JM
8374
8375/* Single precision floating-point operations */
8376/* Arithmetic */
1c97856d
AJ
8377GEN_SPEFPUOP_ARITH2_32_32(efsadd);
8378GEN_SPEFPUOP_ARITH2_32_32(efssub);
8379GEN_SPEFPUOP_ARITH2_32_32(efsmul);
8380GEN_SPEFPUOP_ARITH2_32_32(efsdiv);
636aa200 8381static inline void gen_efsabs(DisasContext *ctx)
1c97856d
AJ
8382{
8383 if (unlikely(!ctx->spe_enabled)) {
27a69bb0 8384 gen_exception(ctx, POWERPC_EXCP_SPEU);
1c97856d
AJ
8385 return;
8386 }
6d5c34fa 8387 tcg_gen_andi_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)], (target_long)~0x80000000LL);
1c97856d 8388}
636aa200 8389static inline void gen_efsnabs(DisasContext *ctx)
1c97856d
AJ
8390{
8391 if (unlikely(!ctx->spe_enabled)) {
27a69bb0 8392 gen_exception(ctx, POWERPC_EXCP_SPEU);
1c97856d
AJ
8393 return;
8394 }
6d5c34fa 8395 tcg_gen_ori_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)], 0x80000000);
1c97856d 8396}
636aa200 8397static inline void gen_efsneg(DisasContext *ctx)
1c97856d
AJ
8398{
8399 if (unlikely(!ctx->spe_enabled)) {
27a69bb0 8400 gen_exception(ctx, POWERPC_EXCP_SPEU);
1c97856d
AJ
8401 return;
8402 }
6d5c34fa 8403 tcg_gen_xori_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)], 0x80000000);
1c97856d
AJ
8404}
8405
0487d6a8 8406/* Conversion */
1c97856d
AJ
8407GEN_SPEFPUOP_CONV_32_32(efscfui);
8408GEN_SPEFPUOP_CONV_32_32(efscfsi);
8409GEN_SPEFPUOP_CONV_32_32(efscfuf);
8410GEN_SPEFPUOP_CONV_32_32(efscfsf);
8411GEN_SPEFPUOP_CONV_32_32(efsctui);
8412GEN_SPEFPUOP_CONV_32_32(efsctsi);
8413GEN_SPEFPUOP_CONV_32_32(efsctuf);
8414GEN_SPEFPUOP_CONV_32_32(efsctsf);
8415GEN_SPEFPUOP_CONV_32_32(efsctuiz);
8416GEN_SPEFPUOP_CONV_32_32(efsctsiz);
8417GEN_SPEFPUOP_CONV_32_64(efscfd);
8418
0487d6a8 8419/* Comparison */
1c97856d
AJ
8420GEN_SPEFPUOP_COMP_32(efscmpgt);
8421GEN_SPEFPUOP_COMP_32(efscmplt);
8422GEN_SPEFPUOP_COMP_32(efscmpeq);
8423GEN_SPEFPUOP_COMP_32(efststgt);
8424GEN_SPEFPUOP_COMP_32(efststlt);
8425GEN_SPEFPUOP_COMP_32(efststeq);
0487d6a8
JM
8426
8427/* Opcodes definitions */
70560da7
FC
8428GEN_SPE(efsadd, efssub, 0x00, 0x0B, 0x00000000, 0x00000000, PPC_SPE_SINGLE); //
8429GEN_SPE(efsabs, efsnabs, 0x02, 0x0B, 0x0000F800, 0x0000F800, PPC_SPE_SINGLE); //
8430GEN_SPE(efsneg, speundef, 0x03, 0x0B, 0x0000F800, 0xFFFFFFFF, PPC_SPE_SINGLE); //
8431GEN_SPE(efsmul, efsdiv, 0x04, 0x0B, 0x00000000, 0x00000000, PPC_SPE_SINGLE); //
8432GEN_SPE(efscmpgt, efscmplt, 0x06, 0x0B, 0x00600000, 0x00600000, PPC_SPE_SINGLE); //
8433GEN_SPE(efscmpeq, efscfd, 0x07, 0x0B, 0x00600000, 0x00180000, PPC_SPE_SINGLE); //
8434GEN_SPE(efscfui, efscfsi, 0x08, 0x0B, 0x00180000, 0x00180000, PPC_SPE_SINGLE); //
8435GEN_SPE(efscfuf, efscfsf, 0x09, 0x0B, 0x00180000, 0x00180000, PPC_SPE_SINGLE); //
8436GEN_SPE(efsctui, efsctsi, 0x0A, 0x0B, 0x00180000, 0x00180000, PPC_SPE_SINGLE); //
8437GEN_SPE(efsctuf, efsctsf, 0x0B, 0x0B, 0x00180000, 0x00180000, PPC_SPE_SINGLE); //
8438GEN_SPE(efsctuiz, speundef, 0x0C, 0x0B, 0x00180000, 0xFFFFFFFF, PPC_SPE_SINGLE); //
8439GEN_SPE(efsctsiz, speundef, 0x0D, 0x0B, 0x00180000, 0xFFFFFFFF, PPC_SPE_SINGLE); //
8440GEN_SPE(efststgt, efststlt, 0x0E, 0x0B, 0x00600000, 0x00600000, PPC_SPE_SINGLE); //
8441GEN_SPE(efststeq, speundef, 0x0F, 0x0B, 0x00600000, 0xFFFFFFFF, PPC_SPE_SINGLE); //
0487d6a8
JM
8442
8443/* Double precision floating-point operations */
8444/* Arithmetic */
1c97856d
AJ
8445GEN_SPEFPUOP_ARITH2_64_64(efdadd);
8446GEN_SPEFPUOP_ARITH2_64_64(efdsub);
8447GEN_SPEFPUOP_ARITH2_64_64(efdmul);
8448GEN_SPEFPUOP_ARITH2_64_64(efddiv);
636aa200 8449static inline void gen_efdabs(DisasContext *ctx)
1c97856d
AJ
8450{
8451 if (unlikely(!ctx->spe_enabled)) {
27a69bb0 8452 gen_exception(ctx, POWERPC_EXCP_SPEU);
1c97856d
AJ
8453 return;
8454 }
8455#if defined(TARGET_PPC64)
6d5c34fa 8456 tcg_gen_andi_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)], ~0x8000000000000000LL);
1c97856d 8457#else
6d5c34fa
MP
8458 tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)]);
8459 tcg_gen_andi_tl(cpu_gprh[rD(ctx->opcode)], cpu_gprh[rA(ctx->opcode)], ~0x80000000);
1c97856d
AJ
8460#endif
8461}
636aa200 8462static inline void gen_efdnabs(DisasContext *ctx)
1c97856d
AJ
8463{
8464 if (unlikely(!ctx->spe_enabled)) {
27a69bb0 8465 gen_exception(ctx, POWERPC_EXCP_SPEU);
1c97856d
AJ
8466 return;
8467 }
8468#if defined(TARGET_PPC64)
6d5c34fa 8469 tcg_gen_ori_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)], 0x8000000000000000LL);
1c97856d 8470#else
6d5c34fa
MP
8471 tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)]);
8472 tcg_gen_ori_tl(cpu_gprh[rD(ctx->opcode)], cpu_gprh[rA(ctx->opcode)], 0x80000000);
1c97856d
AJ
8473#endif
8474}
636aa200 8475static inline void gen_efdneg(DisasContext *ctx)
1c97856d
AJ
8476{
8477 if (unlikely(!ctx->spe_enabled)) {
27a69bb0 8478 gen_exception(ctx, POWERPC_EXCP_SPEU);
1c97856d
AJ
8479 return;
8480 }
8481#if defined(TARGET_PPC64)
6d5c34fa 8482 tcg_gen_xori_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)], 0x8000000000000000LL);
1c97856d 8483#else
6d5c34fa
MP
8484 tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)]);
8485 tcg_gen_xori_tl(cpu_gprh[rD(ctx->opcode)], cpu_gprh[rA(ctx->opcode)], 0x80000000);
1c97856d
AJ
8486#endif
8487}
8488
0487d6a8 8489/* Conversion */
1c97856d
AJ
8490GEN_SPEFPUOP_CONV_64_32(efdcfui);
8491GEN_SPEFPUOP_CONV_64_32(efdcfsi);
8492GEN_SPEFPUOP_CONV_64_32(efdcfuf);
8493GEN_SPEFPUOP_CONV_64_32(efdcfsf);
8494GEN_SPEFPUOP_CONV_32_64(efdctui);
8495GEN_SPEFPUOP_CONV_32_64(efdctsi);
8496GEN_SPEFPUOP_CONV_32_64(efdctuf);
8497GEN_SPEFPUOP_CONV_32_64(efdctsf);
8498GEN_SPEFPUOP_CONV_32_64(efdctuiz);
8499GEN_SPEFPUOP_CONV_32_64(efdctsiz);
8500GEN_SPEFPUOP_CONV_64_32(efdcfs);
8501GEN_SPEFPUOP_CONV_64_64(efdcfuid);
8502GEN_SPEFPUOP_CONV_64_64(efdcfsid);
8503GEN_SPEFPUOP_CONV_64_64(efdctuidz);
8504GEN_SPEFPUOP_CONV_64_64(efdctsidz);
0487d6a8 8505
0487d6a8 8506/* Comparison */
1c97856d
AJ
8507GEN_SPEFPUOP_COMP_64(efdcmpgt);
8508GEN_SPEFPUOP_COMP_64(efdcmplt);
8509GEN_SPEFPUOP_COMP_64(efdcmpeq);
8510GEN_SPEFPUOP_COMP_64(efdtstgt);
8511GEN_SPEFPUOP_COMP_64(efdtstlt);
8512GEN_SPEFPUOP_COMP_64(efdtsteq);
0487d6a8
JM
8513
8514/* Opcodes definitions */
70560da7
FC
8515GEN_SPE(efdadd, efdsub, 0x10, 0x0B, 0x00000000, 0x00000000, PPC_SPE_DOUBLE); //
8516GEN_SPE(efdcfuid, efdcfsid, 0x11, 0x0B, 0x00180000, 0x00180000, PPC_SPE_DOUBLE); //
8517GEN_SPE(efdabs, efdnabs, 0x12, 0x0B, 0x0000F800, 0x0000F800, PPC_SPE_DOUBLE); //
8518GEN_SPE(efdneg, speundef, 0x13, 0x0B, 0x0000F800, 0xFFFFFFFF, PPC_SPE_DOUBLE); //
8519GEN_SPE(efdmul, efddiv, 0x14, 0x0B, 0x00000000, 0x00000000, PPC_SPE_DOUBLE); //
8520GEN_SPE(efdctuidz, efdctsidz, 0x15, 0x0B, 0x00180000, 0x00180000, PPC_SPE_DOUBLE); //
8521GEN_SPE(efdcmpgt, efdcmplt, 0x16, 0x0B, 0x00600000, 0x00600000, PPC_SPE_DOUBLE); //
8522GEN_SPE(efdcmpeq, efdcfs, 0x17, 0x0B, 0x00600000, 0x00180000, PPC_SPE_DOUBLE); //
8523GEN_SPE(efdcfui, efdcfsi, 0x18, 0x0B, 0x00180000, 0x00180000, PPC_SPE_DOUBLE); //
8524GEN_SPE(efdcfuf, efdcfsf, 0x19, 0x0B, 0x00180000, 0x00180000, PPC_SPE_DOUBLE); //
8525GEN_SPE(efdctui, efdctsi, 0x1A, 0x0B, 0x00180000, 0x00180000, PPC_SPE_DOUBLE); //
8526GEN_SPE(efdctuf, efdctsf, 0x1B, 0x0B, 0x00180000, 0x00180000, PPC_SPE_DOUBLE); //
8527GEN_SPE(efdctuiz, speundef, 0x1C, 0x0B, 0x00180000, 0xFFFFFFFF, PPC_SPE_DOUBLE); //
8528GEN_SPE(efdctsiz, speundef, 0x1D, 0x0B, 0x00180000, 0xFFFFFFFF, PPC_SPE_DOUBLE); //
8529GEN_SPE(efdtstgt, efdtstlt, 0x1E, 0x0B, 0x00600000, 0x00600000, PPC_SPE_DOUBLE); //
8530GEN_SPE(efdtsteq, speundef, 0x1F, 0x0B, 0x00600000, 0xFFFFFFFF, PPC_SPE_DOUBLE); //
0487d6a8 8531
c227f099 8532static opcode_t opcodes[] = {
5c55ff99
BS
8533GEN_HANDLER(invalid, 0x00, 0x00, 0x00, 0xFFFFFFFF, PPC_NONE),
8534GEN_HANDLER(cmp, 0x1F, 0x00, 0x00, 0x00400000, PPC_INTEGER),
8535GEN_HANDLER(cmpi, 0x0B, 0xFF, 0xFF, 0x00400000, PPC_INTEGER),
8536GEN_HANDLER(cmpl, 0x1F, 0x00, 0x01, 0x00400000, PPC_INTEGER),
8537GEN_HANDLER(cmpli, 0x0A, 0xFF, 0xFF, 0x00400000, PPC_INTEGER),
fcfda20f 8538GEN_HANDLER_E(cmpb, 0x1F, 0x1C, 0x0F, 0x00000001, PPC_NONE, PPC2_ISA205),
5c55ff99
BS
8539GEN_HANDLER(isel, 0x1F, 0x0F, 0xFF, 0x00000001, PPC_ISEL),
8540GEN_HANDLER(addi, 0x0E, 0xFF, 0xFF, 0x00000000, PPC_INTEGER),
8541GEN_HANDLER(addic, 0x0C, 0xFF, 0xFF, 0x00000000, PPC_INTEGER),
8542GEN_HANDLER2(addic_, "addic.", 0x0D, 0xFF, 0xFF, 0x00000000, PPC_INTEGER),
8543GEN_HANDLER(addis, 0x0F, 0xFF, 0xFF, 0x00000000, PPC_INTEGER),
8544GEN_HANDLER(mulhw, 0x1F, 0x0B, 0x02, 0x00000400, PPC_INTEGER),
8545GEN_HANDLER(mulhwu, 0x1F, 0x0B, 0x00, 0x00000400, PPC_INTEGER),
8546GEN_HANDLER(mullw, 0x1F, 0x0B, 0x07, 0x00000000, PPC_INTEGER),
8547GEN_HANDLER(mullwo, 0x1F, 0x0B, 0x17, 0x00000000, PPC_INTEGER),
8548GEN_HANDLER(mulli, 0x07, 0xFF, 0xFF, 0x00000000, PPC_INTEGER),
8549#if defined(TARGET_PPC64)
8550GEN_HANDLER(mulld, 0x1F, 0x09, 0x07, 0x00000000, PPC_64B),
8551#endif
8552GEN_HANDLER(neg, 0x1F, 0x08, 0x03, 0x0000F800, PPC_INTEGER),
8553GEN_HANDLER(nego, 0x1F, 0x08, 0x13, 0x0000F800, PPC_INTEGER),
8554GEN_HANDLER(subfic, 0x08, 0xFF, 0xFF, 0x00000000, PPC_INTEGER),
8555GEN_HANDLER2(andi_, "andi.", 0x1C, 0xFF, 0xFF, 0x00000000, PPC_INTEGER),
8556GEN_HANDLER2(andis_, "andis.", 0x1D, 0xFF, 0xFF, 0x00000000, PPC_INTEGER),
8557GEN_HANDLER(cntlzw, 0x1F, 0x1A, 0x00, 0x00000000, PPC_INTEGER),
8558GEN_HANDLER(or, 0x1F, 0x1C, 0x0D, 0x00000000, PPC_INTEGER),
8559GEN_HANDLER(xor, 0x1F, 0x1C, 0x09, 0x00000000, PPC_INTEGER),
8560GEN_HANDLER(ori, 0x18, 0xFF, 0xFF, 0x00000000, PPC_INTEGER),
8561GEN_HANDLER(oris, 0x19, 0xFF, 0xFF, 0x00000000, PPC_INTEGER),
8562GEN_HANDLER(xori, 0x1A, 0xFF, 0xFF, 0x00000000, PPC_INTEGER),
8563GEN_HANDLER(xoris, 0x1B, 0xFF, 0xFF, 0x00000000, PPC_INTEGER),
8564GEN_HANDLER(popcntb, 0x1F, 0x03, 0x03, 0x0000F801, PPC_POPCNTB),
eaabeef2 8565GEN_HANDLER(popcntw, 0x1F, 0x1A, 0x0b, 0x0000F801, PPC_POPCNTWD),
725bcec2 8566GEN_HANDLER_E(prtyw, 0x1F, 0x1A, 0x04, 0x0000F801, PPC_NONE, PPC2_ISA205),
5c55ff99 8567#if defined(TARGET_PPC64)
eaabeef2 8568GEN_HANDLER(popcntd, 0x1F, 0x1A, 0x0F, 0x0000F801, PPC_POPCNTWD),
5c55ff99 8569GEN_HANDLER(cntlzd, 0x1F, 0x1A, 0x01, 0x00000000, PPC_64B),
725bcec2 8570GEN_HANDLER_E(prtyd, 0x1F, 0x1A, 0x05, 0x0000F801, PPC_NONE, PPC2_ISA205),
5c55ff99
BS
8571#endif
8572GEN_HANDLER(rlwimi, 0x14, 0xFF, 0xFF, 0x00000000, PPC_INTEGER),
8573GEN_HANDLER(rlwinm, 0x15, 0xFF, 0xFF, 0x00000000, PPC_INTEGER),
8574GEN_HANDLER(rlwnm, 0x17, 0xFF, 0xFF, 0x00000000, PPC_INTEGER),
8575GEN_HANDLER(slw, 0x1F, 0x18, 0x00, 0x00000000, PPC_INTEGER),
8576GEN_HANDLER(sraw, 0x1F, 0x18, 0x18, 0x00000000, PPC_INTEGER),
8577GEN_HANDLER(srawi, 0x1F, 0x18, 0x19, 0x00000000, PPC_INTEGER),
8578GEN_HANDLER(srw, 0x1F, 0x18, 0x10, 0x00000000, PPC_INTEGER),
8579#if defined(TARGET_PPC64)
8580GEN_HANDLER(sld, 0x1F, 0x1B, 0x00, 0x00000000, PPC_64B),
8581GEN_HANDLER(srad, 0x1F, 0x1A, 0x18, 0x00000000, PPC_64B),
8582GEN_HANDLER2(sradi0, "sradi", 0x1F, 0x1A, 0x19, 0x00000000, PPC_64B),
8583GEN_HANDLER2(sradi1, "sradi", 0x1F, 0x1B, 0x19, 0x00000000, PPC_64B),
8584GEN_HANDLER(srd, 0x1F, 0x1B, 0x10, 0x00000000, PPC_64B),
8585#endif
8586GEN_HANDLER(frsqrtes, 0x3B, 0x1A, 0xFF, 0x001F07C0, PPC_FLOAT_FRSQRTES),
8587GEN_HANDLER(fsqrt, 0x3F, 0x16, 0xFF, 0x001F07C0, PPC_FLOAT_FSQRT),
8588GEN_HANDLER(fsqrts, 0x3B, 0x16, 0xFF, 0x001F07C0, PPC_FLOAT_FSQRT),
8589GEN_HANDLER(fcmpo, 0x3F, 0x00, 0x01, 0x00600001, PPC_FLOAT),
8590GEN_HANDLER(fcmpu, 0x3F, 0x00, 0x00, 0x00600001, PPC_FLOAT),
bf45a2e6 8591GEN_HANDLER(fabs, 0x3F, 0x08, 0x08, 0x001F0000, PPC_FLOAT),
5c55ff99 8592GEN_HANDLER(fmr, 0x3F, 0x08, 0x02, 0x001F0000, PPC_FLOAT),
bf45a2e6
AJ
8593GEN_HANDLER(fnabs, 0x3F, 0x08, 0x04, 0x001F0000, PPC_FLOAT),
8594GEN_HANDLER(fneg, 0x3F, 0x08, 0x01, 0x001F0000, PPC_FLOAT),
f0332888 8595GEN_HANDLER_E(fcpsgn, 0x3F, 0x08, 0x00, 0x00000000, PPC_NONE, PPC2_ISA205),
5c55ff99
BS
8596GEN_HANDLER(mcrfs, 0x3F, 0x00, 0x02, 0x0063F801, PPC_FLOAT),
8597GEN_HANDLER(mffs, 0x3F, 0x07, 0x12, 0x001FF800, PPC_FLOAT),
8598GEN_HANDLER(mtfsb0, 0x3F, 0x06, 0x02, 0x001FF800, PPC_FLOAT),
8599GEN_HANDLER(mtfsb1, 0x3F, 0x06, 0x01, 0x001FF800, PPC_FLOAT),
8600GEN_HANDLER(mtfsf, 0x3F, 0x07, 0x16, 0x00010000, PPC_FLOAT),
8601GEN_HANDLER(mtfsfi, 0x3F, 0x06, 0x04, 0x006f0800, PPC_FLOAT),
8602#if defined(TARGET_PPC64)
8603GEN_HANDLER(ld, 0x3A, 0xFF, 0xFF, 0x00000000, PPC_64B),
8604GEN_HANDLER(lq, 0x38, 0xFF, 0xFF, 0x00000000, PPC_64BX),
8605GEN_HANDLER(std, 0x3E, 0xFF, 0xFF, 0x00000000, PPC_64B),
8606#endif
8607GEN_HANDLER(lmw, 0x2E, 0xFF, 0xFF, 0x00000000, PPC_INTEGER),
8608GEN_HANDLER(stmw, 0x2F, 0xFF, 0xFF, 0x00000000, PPC_INTEGER),
8609GEN_HANDLER(lswi, 0x1F, 0x15, 0x12, 0x00000001, PPC_STRING),
8610GEN_HANDLER(lswx, 0x1F, 0x15, 0x10, 0x00000001, PPC_STRING),
8611GEN_HANDLER(stswi, 0x1F, 0x15, 0x16, 0x00000001, PPC_STRING),
8612GEN_HANDLER(stswx, 0x1F, 0x15, 0x14, 0x00000001, PPC_STRING),
8613GEN_HANDLER(eieio, 0x1F, 0x16, 0x1A, 0x03FFF801, PPC_MEM_EIEIO),
8614GEN_HANDLER(isync, 0x13, 0x16, 0x04, 0x03FFF801, PPC_MEM),
f844c817 8615GEN_HANDLER(lwarx, 0x1F, 0x14, 0x00, 0x00000000, PPC_RES),
5c55ff99
BS
8616GEN_HANDLER2(stwcx_, "stwcx.", 0x1F, 0x16, 0x04, 0x00000000, PPC_RES),
8617#if defined(TARGET_PPC64)
f844c817 8618GEN_HANDLER(ldarx, 0x1F, 0x14, 0x02, 0x00000000, PPC_64B),
5c55ff99
BS
8619GEN_HANDLER2(stdcx_, "stdcx.", 0x1F, 0x16, 0x06, 0x00000000, PPC_64B),
8620#endif
8621GEN_HANDLER(sync, 0x1F, 0x16, 0x12, 0x039FF801, PPC_MEM_SYNC),
8622GEN_HANDLER(wait, 0x1F, 0x1E, 0x01, 0x03FFF801, PPC_WAIT),
8623GEN_HANDLER(b, 0x12, 0xFF, 0xFF, 0x00000000, PPC_FLOW),
8624GEN_HANDLER(bc, 0x10, 0xFF, 0xFF, 0x00000000, PPC_FLOW),
8625GEN_HANDLER(bcctr, 0x13, 0x10, 0x10, 0x00000000, PPC_FLOW),
8626GEN_HANDLER(bclr, 0x13, 0x10, 0x00, 0x00000000, PPC_FLOW),
8627GEN_HANDLER(mcrf, 0x13, 0x00, 0xFF, 0x00000001, PPC_INTEGER),
8628GEN_HANDLER(rfi, 0x13, 0x12, 0x01, 0x03FF8001, PPC_FLOW),
8629#if defined(TARGET_PPC64)
8630GEN_HANDLER(rfid, 0x13, 0x12, 0x00, 0x03FF8001, PPC_64B),
8631GEN_HANDLER(hrfid, 0x13, 0x12, 0x08, 0x03FF8001, PPC_64H),
8632#endif
8633GEN_HANDLER(sc, 0x11, 0xFF, 0xFF, 0x03FFF01D, PPC_FLOW),
8634GEN_HANDLER(tw, 0x1F, 0x04, 0x00, 0x00000001, PPC_FLOW),
8635GEN_HANDLER(twi, 0x03, 0xFF, 0xFF, 0x00000000, PPC_FLOW),
8636#if defined(TARGET_PPC64)
8637GEN_HANDLER(td, 0x1F, 0x04, 0x02, 0x00000001, PPC_64B),
8638GEN_HANDLER(tdi, 0x02, 0xFF, 0xFF, 0x00000000, PPC_64B),
8639#endif
8640GEN_HANDLER(mcrxr, 0x1F, 0x00, 0x10, 0x007FF801, PPC_MISC),
8641GEN_HANDLER(mfcr, 0x1F, 0x13, 0x00, 0x00000801, PPC_MISC),
8642GEN_HANDLER(mfmsr, 0x1F, 0x13, 0x02, 0x001FF801, PPC_MISC),
8643GEN_HANDLER(mfspr, 0x1F, 0x13, 0x0A, 0x00000001, PPC_MISC),
8644GEN_HANDLER(mftb, 0x1F, 0x13, 0x0B, 0x00000001, PPC_MFTB),
8645GEN_HANDLER(mtcrf, 0x1F, 0x10, 0x04, 0x00000801, PPC_MISC),
8646#if defined(TARGET_PPC64)
8647GEN_HANDLER(mtmsrd, 0x1F, 0x12, 0x05, 0x001EF801, PPC_64B),
8648#endif
8649GEN_HANDLER(mtmsr, 0x1F, 0x12, 0x04, 0x001FF801, PPC_MISC),
8650GEN_HANDLER(mtspr, 0x1F, 0x13, 0x0E, 0x00000001, PPC_MISC),
8651GEN_HANDLER(dcbf, 0x1F, 0x16, 0x02, 0x03C00001, PPC_CACHE),
8652GEN_HANDLER(dcbi, 0x1F, 0x16, 0x0E, 0x03E00001, PPC_CACHE),
8653GEN_HANDLER(dcbst, 0x1F, 0x16, 0x01, 0x03E00001, PPC_CACHE),
8654GEN_HANDLER(dcbt, 0x1F, 0x16, 0x08, 0x02000001, PPC_CACHE),
8655GEN_HANDLER(dcbtst, 0x1F, 0x16, 0x07, 0x02000001, PPC_CACHE),
8e33944f 8656GEN_HANDLER(dcbz, 0x1F, 0x16, 0x1F, 0x03C00001, PPC_CACHE_DCBZ),
5c55ff99
BS
8657GEN_HANDLER(dst, 0x1F, 0x16, 0x0A, 0x01800001, PPC_ALTIVEC),
8658GEN_HANDLER(dstst, 0x1F, 0x16, 0x0B, 0x02000001, PPC_ALTIVEC),
8659GEN_HANDLER(dss, 0x1F, 0x16, 0x19, 0x019FF801, PPC_ALTIVEC),
8660GEN_HANDLER(icbi, 0x1F, 0x16, 0x1E, 0x03E00001, PPC_CACHE_ICBI),
8661GEN_HANDLER(dcba, 0x1F, 0x16, 0x17, 0x03E00001, PPC_CACHE_DCBA),
8662GEN_HANDLER(mfsr, 0x1F, 0x13, 0x12, 0x0010F801, PPC_SEGMENT),
8663GEN_HANDLER(mfsrin, 0x1F, 0x13, 0x14, 0x001F0001, PPC_SEGMENT),
8664GEN_HANDLER(mtsr, 0x1F, 0x12, 0x06, 0x0010F801, PPC_SEGMENT),
8665GEN_HANDLER(mtsrin, 0x1F, 0x12, 0x07, 0x001F0001, PPC_SEGMENT),
8666#if defined(TARGET_PPC64)
8667GEN_HANDLER2(mfsr_64b, "mfsr", 0x1F, 0x13, 0x12, 0x0010F801, PPC_SEGMENT_64B),
8668GEN_HANDLER2(mfsrin_64b, "mfsrin", 0x1F, 0x13, 0x14, 0x001F0001,
8669 PPC_SEGMENT_64B),
8670GEN_HANDLER2(mtsr_64b, "mtsr", 0x1F, 0x12, 0x06, 0x0010F801, PPC_SEGMENT_64B),
8671GEN_HANDLER2(mtsrin_64b, "mtsrin", 0x1F, 0x12, 0x07, 0x001F0001,
8672 PPC_SEGMENT_64B),
efdef95f
DG
8673GEN_HANDLER2(slbmte, "slbmte", 0x1F, 0x12, 0x0C, 0x001F0001, PPC_SEGMENT_64B),
8674GEN_HANDLER2(slbmfee, "slbmfee", 0x1F, 0x13, 0x1C, 0x001F0001, PPC_SEGMENT_64B),
8675GEN_HANDLER2(slbmfev, "slbmfev", 0x1F, 0x13, 0x1A, 0x001F0001, PPC_SEGMENT_64B),
5c55ff99
BS
8676#endif
8677GEN_HANDLER(tlbia, 0x1F, 0x12, 0x0B, 0x03FFFC01, PPC_MEM_TLBIA),
8678GEN_HANDLER(tlbiel, 0x1F, 0x12, 0x08, 0x03FF0001, PPC_MEM_TLBIE),
8679GEN_HANDLER(tlbie, 0x1F, 0x12, 0x09, 0x03FF0001, PPC_MEM_TLBIE),
8680GEN_HANDLER(tlbsync, 0x1F, 0x16, 0x11, 0x03FFF801, PPC_MEM_TLBSYNC),
8681#if defined(TARGET_PPC64)
8682GEN_HANDLER(slbia, 0x1F, 0x12, 0x0F, 0x03FFFC01, PPC_SLBI),
8683GEN_HANDLER(slbie, 0x1F, 0x12, 0x0D, 0x03FF0001, PPC_SLBI),
8684#endif
8685GEN_HANDLER(eciwx, 0x1F, 0x16, 0x0D, 0x00000001, PPC_EXTERN),
8686GEN_HANDLER(ecowx, 0x1F, 0x16, 0x09, 0x00000001, PPC_EXTERN),
8687GEN_HANDLER(abs, 0x1F, 0x08, 0x0B, 0x0000F800, PPC_POWER_BR),
8688GEN_HANDLER(abso, 0x1F, 0x08, 0x1B, 0x0000F800, PPC_POWER_BR),
8689GEN_HANDLER(clcs, 0x1F, 0x10, 0x13, 0x0000F800, PPC_POWER_BR),
8690GEN_HANDLER(div, 0x1F, 0x0B, 0x0A, 0x00000000, PPC_POWER_BR),
8691GEN_HANDLER(divo, 0x1F, 0x0B, 0x1A, 0x00000000, PPC_POWER_BR),
8692GEN_HANDLER(divs, 0x1F, 0x0B, 0x0B, 0x00000000, PPC_POWER_BR),
8693GEN_HANDLER(divso, 0x1F, 0x0B, 0x1B, 0x00000000, PPC_POWER_BR),
8694GEN_HANDLER(doz, 0x1F, 0x08, 0x08, 0x00000000, PPC_POWER_BR),
8695GEN_HANDLER(dozo, 0x1F, 0x08, 0x18, 0x00000000, PPC_POWER_BR),
8696GEN_HANDLER(dozi, 0x09, 0xFF, 0xFF, 0x00000000, PPC_POWER_BR),
8697GEN_HANDLER(lscbx, 0x1F, 0x15, 0x08, 0x00000000, PPC_POWER_BR),
8698GEN_HANDLER(maskg, 0x1F, 0x1D, 0x00, 0x00000000, PPC_POWER_BR),
8699GEN_HANDLER(maskir, 0x1F, 0x1D, 0x10, 0x00000000, PPC_POWER_BR),
8700GEN_HANDLER(mul, 0x1F, 0x0B, 0x03, 0x00000000, PPC_POWER_BR),
8701GEN_HANDLER(mulo, 0x1F, 0x0B, 0x13, 0x00000000, PPC_POWER_BR),
8702GEN_HANDLER(nabs, 0x1F, 0x08, 0x0F, 0x00000000, PPC_POWER_BR),
8703GEN_HANDLER(nabso, 0x1F, 0x08, 0x1F, 0x00000000, PPC_POWER_BR),
8704GEN_HANDLER(rlmi, 0x16, 0xFF, 0xFF, 0x00000000, PPC_POWER_BR),
8705GEN_HANDLER(rrib, 0x1F, 0x19, 0x10, 0x00000000, PPC_POWER_BR),
8706GEN_HANDLER(sle, 0x1F, 0x19, 0x04, 0x00000000, PPC_POWER_BR),
8707GEN_HANDLER(sleq, 0x1F, 0x19, 0x06, 0x00000000, PPC_POWER_BR),
8708GEN_HANDLER(sliq, 0x1F, 0x18, 0x05, 0x00000000, PPC_POWER_BR),
8709GEN_HANDLER(slliq, 0x1F, 0x18, 0x07, 0x00000000, PPC_POWER_BR),
8710GEN_HANDLER(sllq, 0x1F, 0x18, 0x06, 0x00000000, PPC_POWER_BR),
8711GEN_HANDLER(slq, 0x1F, 0x18, 0x04, 0x00000000, PPC_POWER_BR),
8712GEN_HANDLER(sraiq, 0x1F, 0x18, 0x1D, 0x00000000, PPC_POWER_BR),
8713GEN_HANDLER(sraq, 0x1F, 0x18, 0x1C, 0x00000000, PPC_POWER_BR),
8714GEN_HANDLER(sre, 0x1F, 0x19, 0x14, 0x00000000, PPC_POWER_BR),
8715GEN_HANDLER(srea, 0x1F, 0x19, 0x1C, 0x00000000, PPC_POWER_BR),
8716GEN_HANDLER(sreq, 0x1F, 0x19, 0x16, 0x00000000, PPC_POWER_BR),
8717GEN_HANDLER(sriq, 0x1F, 0x18, 0x15, 0x00000000, PPC_POWER_BR),
8718GEN_HANDLER(srliq, 0x1F, 0x18, 0x17, 0x00000000, PPC_POWER_BR),
8719GEN_HANDLER(srlq, 0x1F, 0x18, 0x16, 0x00000000, PPC_POWER_BR),
8720GEN_HANDLER(srq, 0x1F, 0x18, 0x14, 0x00000000, PPC_POWER_BR),
8721GEN_HANDLER(dsa, 0x1F, 0x14, 0x13, 0x03FFF801, PPC_602_SPEC),
8722GEN_HANDLER(esa, 0x1F, 0x14, 0x12, 0x03FFF801, PPC_602_SPEC),
8723GEN_HANDLER(mfrom, 0x1F, 0x09, 0x08, 0x03E0F801, PPC_602_SPEC),
8724GEN_HANDLER2(tlbld_6xx, "tlbld", 0x1F, 0x12, 0x1E, 0x03FF0001, PPC_6xx_TLB),
8725GEN_HANDLER2(tlbli_6xx, "tlbli", 0x1F, 0x12, 0x1F, 0x03FF0001, PPC_6xx_TLB),
8726GEN_HANDLER2(tlbld_74xx, "tlbld", 0x1F, 0x12, 0x1E, 0x03FF0001, PPC_74xx_TLB),
8727GEN_HANDLER2(tlbli_74xx, "tlbli", 0x1F, 0x12, 0x1F, 0x03FF0001, PPC_74xx_TLB),
8728GEN_HANDLER(clf, 0x1F, 0x16, 0x03, 0x03E00000, PPC_POWER),
8729GEN_HANDLER(cli, 0x1F, 0x16, 0x0F, 0x03E00000, PPC_POWER),
8730GEN_HANDLER(dclst, 0x1F, 0x16, 0x13, 0x03E00000, PPC_POWER),
8731GEN_HANDLER(mfsri, 0x1F, 0x13, 0x13, 0x00000001, PPC_POWER),
8732GEN_HANDLER(rac, 0x1F, 0x12, 0x19, 0x00000001, PPC_POWER),
8733GEN_HANDLER(rfsvc, 0x13, 0x12, 0x02, 0x03FFF0001, PPC_POWER),
8734GEN_HANDLER(lfq, 0x38, 0xFF, 0xFF, 0x00000003, PPC_POWER2),
8735GEN_HANDLER(lfqu, 0x39, 0xFF, 0xFF, 0x00000003, PPC_POWER2),
8736GEN_HANDLER(lfqux, 0x1F, 0x17, 0x19, 0x00000001, PPC_POWER2),
8737GEN_HANDLER(lfqx, 0x1F, 0x17, 0x18, 0x00000001, PPC_POWER2),
8738GEN_HANDLER(stfq, 0x3C, 0xFF, 0xFF, 0x00000003, PPC_POWER2),
8739GEN_HANDLER(stfqu, 0x3D, 0xFF, 0xFF, 0x00000003, PPC_POWER2),
8740GEN_HANDLER(stfqux, 0x1F, 0x17, 0x1D, 0x00000001, PPC_POWER2),
8741GEN_HANDLER(stfqx, 0x1F, 0x17, 0x1C, 0x00000001, PPC_POWER2),
8742GEN_HANDLER(mfapidi, 0x1F, 0x13, 0x08, 0x0000F801, PPC_MFAPIDI),
8743GEN_HANDLER(tlbiva, 0x1F, 0x12, 0x18, 0x03FFF801, PPC_TLBIVA),
8744GEN_HANDLER(mfdcr, 0x1F, 0x03, 0x0A, 0x00000001, PPC_DCR),
8745GEN_HANDLER(mtdcr, 0x1F, 0x03, 0x0E, 0x00000001, PPC_DCR),
8746GEN_HANDLER(mfdcrx, 0x1F, 0x03, 0x08, 0x00000000, PPC_DCRX),
8747GEN_HANDLER(mtdcrx, 0x1F, 0x03, 0x0C, 0x00000000, PPC_DCRX),
8748GEN_HANDLER(mfdcrux, 0x1F, 0x03, 0x09, 0x00000000, PPC_DCRUX),
8749GEN_HANDLER(mtdcrux, 0x1F, 0x03, 0x0D, 0x00000000, PPC_DCRUX),
8750GEN_HANDLER(dccci, 0x1F, 0x06, 0x0E, 0x03E00001, PPC_4xx_COMMON),
8751GEN_HANDLER(dcread, 0x1F, 0x06, 0x0F, 0x00000001, PPC_4xx_COMMON),
8752GEN_HANDLER2(icbt_40x, "icbt", 0x1F, 0x06, 0x08, 0x03E00001, PPC_40x_ICBT),
8753GEN_HANDLER(iccci, 0x1F, 0x06, 0x1E, 0x00000001, PPC_4xx_COMMON),
8754GEN_HANDLER(icread, 0x1F, 0x06, 0x1F, 0x03E00001, PPC_4xx_COMMON),
8755GEN_HANDLER2(rfci_40x, "rfci", 0x13, 0x13, 0x01, 0x03FF8001, PPC_40x_EXCP),
01662f3e 8756GEN_HANDLER_E(rfci, 0x13, 0x13, 0x01, 0x03FF8001, PPC_BOOKE, PPC2_BOOKE206),
5c55ff99
BS
8757GEN_HANDLER(rfdi, 0x13, 0x07, 0x01, 0x03FF8001, PPC_RFDI),
8758GEN_HANDLER(rfmci, 0x13, 0x06, 0x01, 0x03FF8001, PPC_RFMCI),
8759GEN_HANDLER2(tlbre_40x, "tlbre", 0x1F, 0x12, 0x1D, 0x00000001, PPC_40x_TLB),
8760GEN_HANDLER2(tlbsx_40x, "tlbsx", 0x1F, 0x12, 0x1C, 0x00000000, PPC_40x_TLB),
8761GEN_HANDLER2(tlbwe_40x, "tlbwe", 0x1F, 0x12, 0x1E, 0x00000001, PPC_40x_TLB),
8762GEN_HANDLER2(tlbre_440, "tlbre", 0x1F, 0x12, 0x1D, 0x00000001, PPC_BOOKE),
8763GEN_HANDLER2(tlbsx_440, "tlbsx", 0x1F, 0x12, 0x1C, 0x00000000, PPC_BOOKE),
8764GEN_HANDLER2(tlbwe_440, "tlbwe", 0x1F, 0x12, 0x1E, 0x00000001, PPC_BOOKE),
01662f3e
AG
8765GEN_HANDLER2_E(tlbre_booke206, "tlbre", 0x1F, 0x12, 0x1D, 0x00000001,
8766 PPC_NONE, PPC2_BOOKE206),
8767GEN_HANDLER2_E(tlbsx_booke206, "tlbsx", 0x1F, 0x12, 0x1C, 0x00000000,
8768 PPC_NONE, PPC2_BOOKE206),
8769GEN_HANDLER2_E(tlbwe_booke206, "tlbwe", 0x1F, 0x12, 0x1E, 0x00000001,
8770 PPC_NONE, PPC2_BOOKE206),
8771GEN_HANDLER2_E(tlbivax_booke206, "tlbivax", 0x1F, 0x12, 0x18, 0x00000001,
8772 PPC_NONE, PPC2_BOOKE206),
6d3db821
AG
8773GEN_HANDLER2_E(tlbilx_booke206, "tlbilx", 0x1F, 0x12, 0x00, 0x03800001,
8774 PPC_NONE, PPC2_BOOKE206),
d5d11a39
AG
8775GEN_HANDLER2_E(msgsnd, "msgsnd", 0x1F, 0x0E, 0x06, 0x03ff0001,
8776 PPC_NONE, PPC2_PRCNTL),
9e0b5cb1
AG
8777GEN_HANDLER2_E(msgclr, "msgclr", 0x1F, 0x0E, 0x07, 0x03ff0001,
8778 PPC_NONE, PPC2_PRCNTL),
5c55ff99 8779GEN_HANDLER(wrtee, 0x1F, 0x03, 0x04, 0x000FFC01, PPC_WRTEE),
fbe73008 8780GEN_HANDLER(wrteei, 0x1F, 0x03, 0x05, 0x000E7C01, PPC_WRTEE),
5c55ff99 8781GEN_HANDLER(dlmzb, 0x1F, 0x0E, 0x02, 0x00000000, PPC_440_SPEC),
01662f3e
AG
8782GEN_HANDLER_E(mbar, 0x1F, 0x16, 0x1a, 0x001FF801,
8783 PPC_BOOKE, PPC2_BOOKE206),
dcb2b9e1 8784GEN_HANDLER(msync_4xx, 0x1F, 0x16, 0x12, 0x03FFF801, PPC_BOOKE),
01662f3e
AG
8785GEN_HANDLER2_E(icbt_440, "icbt", 0x1F, 0x16, 0x00, 0x03E00001,
8786 PPC_BOOKE, PPC2_BOOKE206),
5c55ff99
BS
8787GEN_HANDLER(lvsl, 0x1f, 0x06, 0x00, 0x00000001, PPC_ALTIVEC),
8788GEN_HANDLER(lvsr, 0x1f, 0x06, 0x01, 0x00000001, PPC_ALTIVEC),
8789GEN_HANDLER(mfvscr, 0x04, 0x2, 0x18, 0x001ff800, PPC_ALTIVEC),
8790GEN_HANDLER(mtvscr, 0x04, 0x2, 0x19, 0x03ff0000, PPC_ALTIVEC),
8791GEN_HANDLER(vsldoi, 0x04, 0x16, 0xFF, 0x00000400, PPC_ALTIVEC),
8792GEN_HANDLER(vmladduhm, 0x04, 0x11, 0xFF, 0x00000000, PPC_ALTIVEC),
8793GEN_HANDLER2(evsel0, "evsel", 0x04, 0x1c, 0x09, 0x00000000, PPC_SPE),
8794GEN_HANDLER2(evsel1, "evsel", 0x04, 0x1d, 0x09, 0x00000000, PPC_SPE),
8795GEN_HANDLER2(evsel2, "evsel", 0x04, 0x1e, 0x09, 0x00000000, PPC_SPE),
8796GEN_HANDLER2(evsel3, "evsel", 0x04, 0x1f, 0x09, 0x00000000, PPC_SPE),
8797
8798#undef GEN_INT_ARITH_ADD
8799#undef GEN_INT_ARITH_ADD_CONST
8800#define GEN_INT_ARITH_ADD(name, opc3, add_ca, compute_ca, compute_ov) \
8801GEN_HANDLER(name, 0x1F, 0x0A, opc3, 0x00000000, PPC_INTEGER),
8802#define GEN_INT_ARITH_ADD_CONST(name, opc3, const_val, \
8803 add_ca, compute_ca, compute_ov) \
8804GEN_HANDLER(name, 0x1F, 0x0A, opc3, 0x0000F800, PPC_INTEGER),
8805GEN_INT_ARITH_ADD(add, 0x08, 0, 0, 0)
8806GEN_INT_ARITH_ADD(addo, 0x18, 0, 0, 1)
8807GEN_INT_ARITH_ADD(addc, 0x00, 0, 1, 0)
8808GEN_INT_ARITH_ADD(addco, 0x10, 0, 1, 1)
8809GEN_INT_ARITH_ADD(adde, 0x04, 1, 1, 0)
8810GEN_INT_ARITH_ADD(addeo, 0x14, 1, 1, 1)
8811GEN_INT_ARITH_ADD_CONST(addme, 0x07, -1LL, 1, 1, 0)
8812GEN_INT_ARITH_ADD_CONST(addmeo, 0x17, -1LL, 1, 1, 1)
8813GEN_INT_ARITH_ADD_CONST(addze, 0x06, 0, 1, 1, 0)
8814GEN_INT_ARITH_ADD_CONST(addzeo, 0x16, 0, 1, 1, 1)
8815
8816#undef GEN_INT_ARITH_DIVW
8817#define GEN_INT_ARITH_DIVW(name, opc3, sign, compute_ov) \
8818GEN_HANDLER(name, 0x1F, 0x0B, opc3, 0x00000000, PPC_INTEGER)
8819GEN_INT_ARITH_DIVW(divwu, 0x0E, 0, 0),
8820GEN_INT_ARITH_DIVW(divwuo, 0x1E, 0, 1),
8821GEN_INT_ARITH_DIVW(divw, 0x0F, 1, 0),
8822GEN_INT_ARITH_DIVW(divwo, 0x1F, 1, 1),
8823
8824#if defined(TARGET_PPC64)
8825#undef GEN_INT_ARITH_DIVD
8826#define GEN_INT_ARITH_DIVD(name, opc3, sign, compute_ov) \
8827GEN_HANDLER(name, 0x1F, 0x09, opc3, 0x00000000, PPC_64B)
8828GEN_INT_ARITH_DIVD(divdu, 0x0E, 0, 0),
8829GEN_INT_ARITH_DIVD(divduo, 0x1E, 0, 1),
8830GEN_INT_ARITH_DIVD(divd, 0x0F, 1, 0),
8831GEN_INT_ARITH_DIVD(divdo, 0x1F, 1, 1),
8832
8833#undef GEN_INT_ARITH_MUL_HELPER
8834#define GEN_INT_ARITH_MUL_HELPER(name, opc3) \
8835GEN_HANDLER(name, 0x1F, 0x09, opc3, 0x00000000, PPC_64B)
8836GEN_INT_ARITH_MUL_HELPER(mulhdu, 0x00),
8837GEN_INT_ARITH_MUL_HELPER(mulhd, 0x02),
8838GEN_INT_ARITH_MUL_HELPER(mulldo, 0x17),
8839#endif
8840
8841#undef GEN_INT_ARITH_SUBF
8842#undef GEN_INT_ARITH_SUBF_CONST
8843#define GEN_INT_ARITH_SUBF(name, opc3, add_ca, compute_ca, compute_ov) \
8844GEN_HANDLER(name, 0x1F, 0x08, opc3, 0x00000000, PPC_INTEGER),
8845#define GEN_INT_ARITH_SUBF_CONST(name, opc3, const_val, \
8846 add_ca, compute_ca, compute_ov) \
8847GEN_HANDLER(name, 0x1F, 0x08, opc3, 0x0000F800, PPC_INTEGER),
8848GEN_INT_ARITH_SUBF(subf, 0x01, 0, 0, 0)
8849GEN_INT_ARITH_SUBF(subfo, 0x11, 0, 0, 1)
8850GEN_INT_ARITH_SUBF(subfc, 0x00, 0, 1, 0)
8851GEN_INT_ARITH_SUBF(subfco, 0x10, 0, 1, 1)
8852GEN_INT_ARITH_SUBF(subfe, 0x04, 1, 1, 0)
8853GEN_INT_ARITH_SUBF(subfeo, 0x14, 1, 1, 1)
8854GEN_INT_ARITH_SUBF_CONST(subfme, 0x07, -1LL, 1, 1, 0)
8855GEN_INT_ARITH_SUBF_CONST(subfmeo, 0x17, -1LL, 1, 1, 1)
8856GEN_INT_ARITH_SUBF_CONST(subfze, 0x06, 0, 1, 1, 0)
8857GEN_INT_ARITH_SUBF_CONST(subfzeo, 0x16, 0, 1, 1, 1)
8858
8859#undef GEN_LOGICAL1
8860#undef GEN_LOGICAL2
8861#define GEN_LOGICAL2(name, tcg_op, opc, type) \
8862GEN_HANDLER(name, 0x1F, 0x1C, opc, 0x00000000, type)
8863#define GEN_LOGICAL1(name, tcg_op, opc, type) \
8864GEN_HANDLER(name, 0x1F, 0x1A, opc, 0x00000000, type)
8865GEN_LOGICAL2(and, tcg_gen_and_tl, 0x00, PPC_INTEGER),
8866GEN_LOGICAL2(andc, tcg_gen_andc_tl, 0x01, PPC_INTEGER),
8867GEN_LOGICAL2(eqv, tcg_gen_eqv_tl, 0x08, PPC_INTEGER),
8868GEN_LOGICAL1(extsb, tcg_gen_ext8s_tl, 0x1D, PPC_INTEGER),
8869GEN_LOGICAL1(extsh, tcg_gen_ext16s_tl, 0x1C, PPC_INTEGER),
8870GEN_LOGICAL2(nand, tcg_gen_nand_tl, 0x0E, PPC_INTEGER),
8871GEN_LOGICAL2(nor, tcg_gen_nor_tl, 0x03, PPC_INTEGER),
8872GEN_LOGICAL2(orc, tcg_gen_orc_tl, 0x0C, PPC_INTEGER),
8873#if defined(TARGET_PPC64)
8874GEN_LOGICAL1(extsw, tcg_gen_ext32s_tl, 0x1E, PPC_64B),
8875#endif
8876
8877#if defined(TARGET_PPC64)
8878#undef GEN_PPC64_R2
8879#undef GEN_PPC64_R4
8880#define GEN_PPC64_R2(name, opc1, opc2) \
8881GEN_HANDLER2(name##0, stringify(name), opc1, opc2, 0xFF, 0x00000000, PPC_64B),\
8882GEN_HANDLER2(name##1, stringify(name), opc1, opc2 | 0x10, 0xFF, 0x00000000, \
8883 PPC_64B)
8884#define GEN_PPC64_R4(name, opc1, opc2) \
8885GEN_HANDLER2(name##0, stringify(name), opc1, opc2, 0xFF, 0x00000000, PPC_64B),\
8886GEN_HANDLER2(name##1, stringify(name), opc1, opc2 | 0x01, 0xFF, 0x00000000, \
8887 PPC_64B), \
8888GEN_HANDLER2(name##2, stringify(name), opc1, opc2 | 0x10, 0xFF, 0x00000000, \
8889 PPC_64B), \
8890GEN_HANDLER2(name##3, stringify(name), opc1, opc2 | 0x11, 0xFF, 0x00000000, \
8891 PPC_64B)
8892GEN_PPC64_R4(rldicl, 0x1E, 0x00),
8893GEN_PPC64_R4(rldicr, 0x1E, 0x02),
8894GEN_PPC64_R4(rldic, 0x1E, 0x04),
8895GEN_PPC64_R2(rldcl, 0x1E, 0x08),
8896GEN_PPC64_R2(rldcr, 0x1E, 0x09),
8897GEN_PPC64_R4(rldimi, 0x1E, 0x06),
8898#endif
8899
8900#undef _GEN_FLOAT_ACB
8901#undef GEN_FLOAT_ACB
8902#undef _GEN_FLOAT_AB
8903#undef GEN_FLOAT_AB
8904#undef _GEN_FLOAT_AC
8905#undef GEN_FLOAT_AC
8906#undef GEN_FLOAT_B
8907#undef GEN_FLOAT_BS
8908#define _GEN_FLOAT_ACB(name, op, op1, op2, isfloat, set_fprf, type) \
8909GEN_HANDLER(f##name, op1, op2, 0xFF, 0x00000000, type)
8910#define GEN_FLOAT_ACB(name, op2, set_fprf, type) \
8911_GEN_FLOAT_ACB(name, name, 0x3F, op2, 0, set_fprf, type), \
8912_GEN_FLOAT_ACB(name##s, name, 0x3B, op2, 1, set_fprf, type)
8913#define _GEN_FLOAT_AB(name, op, op1, op2, inval, isfloat, set_fprf, type) \
8914GEN_HANDLER(f##name, op1, op2, 0xFF, inval, type)
8915#define GEN_FLOAT_AB(name, op2, inval, set_fprf, type) \
8916_GEN_FLOAT_AB(name, name, 0x3F, op2, inval, 0, set_fprf, type), \
8917_GEN_FLOAT_AB(name##s, name, 0x3B, op2, inval, 1, set_fprf, type)
8918#define _GEN_FLOAT_AC(name, op, op1, op2, inval, isfloat, set_fprf, type) \
8919GEN_HANDLER(f##name, op1, op2, 0xFF, inval, type)
8920#define GEN_FLOAT_AC(name, op2, inval, set_fprf, type) \
8921_GEN_FLOAT_AC(name, name, 0x3F, op2, inval, 0, set_fprf, type), \
8922_GEN_FLOAT_AC(name##s, name, 0x3B, op2, inval, 1, set_fprf, type)
8923#define GEN_FLOAT_B(name, op2, op3, set_fprf, type) \
8924GEN_HANDLER(f##name, 0x3F, op2, op3, 0x001F0000, type)
8925#define GEN_FLOAT_BS(name, op1, op2, set_fprf, type) \
8926GEN_HANDLER(f##name, op1, op2, 0xFF, 0x001F07C0, type)
8927
8928GEN_FLOAT_AB(add, 0x15, 0x000007C0, 1, PPC_FLOAT),
8929GEN_FLOAT_AB(div, 0x12, 0x000007C0, 1, PPC_FLOAT),
8930GEN_FLOAT_AC(mul, 0x19, 0x0000F800, 1, PPC_FLOAT),
8931GEN_FLOAT_BS(re, 0x3F, 0x18, 1, PPC_FLOAT_EXT),
8932GEN_FLOAT_BS(res, 0x3B, 0x18, 1, PPC_FLOAT_FRES),
8933GEN_FLOAT_BS(rsqrte, 0x3F, 0x1A, 1, PPC_FLOAT_FRSQRTE),
8934_GEN_FLOAT_ACB(sel, sel, 0x3F, 0x17, 0, 0, PPC_FLOAT_FSEL),
8935GEN_FLOAT_AB(sub, 0x14, 0x000007C0, 1, PPC_FLOAT),
8936GEN_FLOAT_ACB(madd, 0x1D, 1, PPC_FLOAT),
8937GEN_FLOAT_ACB(msub, 0x1C, 1, PPC_FLOAT),
8938GEN_FLOAT_ACB(nmadd, 0x1F, 1, PPC_FLOAT),
8939GEN_FLOAT_ACB(nmsub, 0x1E, 1, PPC_FLOAT),
8940GEN_FLOAT_B(ctiw, 0x0E, 0x00, 0, PPC_FLOAT),
8941GEN_FLOAT_B(ctiwz, 0x0F, 0x00, 0, PPC_FLOAT),
8942GEN_FLOAT_B(rsp, 0x0C, 0x00, 1, PPC_FLOAT),
8943#if defined(TARGET_PPC64)
8944GEN_FLOAT_B(cfid, 0x0E, 0x1A, 1, PPC_64B),
8945GEN_FLOAT_B(ctid, 0x0E, 0x19, 0, PPC_64B),
8946GEN_FLOAT_B(ctidz, 0x0F, 0x19, 0, PPC_64B),
8947#endif
8948GEN_FLOAT_B(rin, 0x08, 0x0C, 1, PPC_FLOAT_EXT),
8949GEN_FLOAT_B(riz, 0x08, 0x0D, 1, PPC_FLOAT_EXT),
8950GEN_FLOAT_B(rip, 0x08, 0x0E, 1, PPC_FLOAT_EXT),
8951GEN_FLOAT_B(rim, 0x08, 0x0F, 1, PPC_FLOAT_EXT),
5c55ff99
BS
8952
8953#undef GEN_LD
8954#undef GEN_LDU
8955#undef GEN_LDUX
cd6e9320 8956#undef GEN_LDX_E
5c55ff99
BS
8957#undef GEN_LDS
8958#define GEN_LD(name, ldop, opc, type) \
8959GEN_HANDLER(name, opc, 0xFF, 0xFF, 0x00000000, type),
8960#define GEN_LDU(name, ldop, opc, type) \
8961GEN_HANDLER(name##u, opc, 0xFF, 0xFF, 0x00000000, type),
8962#define GEN_LDUX(name, ldop, opc2, opc3, type) \
8963GEN_HANDLER(name##ux, 0x1F, opc2, opc3, 0x00000001, type),
cd6e9320
TH
8964#define GEN_LDX_E(name, ldop, opc2, opc3, type, type2) \
8965GEN_HANDLER_E(name##x, 0x1F, opc2, opc3, 0x00000001, type, type2),
5c55ff99
BS
8966#define GEN_LDS(name, ldop, op, type) \
8967GEN_LD(name, ldop, op | 0x20, type) \
8968GEN_LDU(name, ldop, op | 0x21, type) \
8969GEN_LDUX(name, ldop, 0x17, op | 0x01, type) \
8970GEN_LDX(name, ldop, 0x17, op | 0x00, type)
8971
8972GEN_LDS(lbz, ld8u, 0x02, PPC_INTEGER)
8973GEN_LDS(lha, ld16s, 0x0A, PPC_INTEGER)
8974GEN_LDS(lhz, ld16u, 0x08, PPC_INTEGER)
8975GEN_LDS(lwz, ld32u, 0x00, PPC_INTEGER)
8976#if defined(TARGET_PPC64)
8977GEN_LDUX(lwa, ld32s, 0x15, 0x0B, PPC_64B)
8978GEN_LDX(lwa, ld32s, 0x15, 0x0A, PPC_64B)
8979GEN_LDUX(ld, ld64, 0x15, 0x01, PPC_64B)
8980GEN_LDX(ld, ld64, 0x15, 0x00, PPC_64B)
cd6e9320 8981GEN_LDX_E(ldbr, ld64ur, 0x14, 0x10, PPC_NONE, PPC2_DBRX)
5c55ff99
BS
8982#endif
8983GEN_LDX(lhbr, ld16ur, 0x16, 0x18, PPC_INTEGER)
8984GEN_LDX(lwbr, ld32ur, 0x16, 0x10, PPC_INTEGER)
8985
8986#undef GEN_ST
8987#undef GEN_STU
8988#undef GEN_STUX
cd6e9320 8989#undef GEN_STX_E
5c55ff99
BS
8990#undef GEN_STS
8991#define GEN_ST(name, stop, opc, type) \
8992GEN_HANDLER(name, opc, 0xFF, 0xFF, 0x00000000, type),
8993#define GEN_STU(name, stop, opc, type) \
8994GEN_HANDLER(stop##u, opc, 0xFF, 0xFF, 0x00000000, type),
8995#define GEN_STUX(name, stop, opc2, opc3, type) \
8996GEN_HANDLER(name##ux, 0x1F, opc2, opc3, 0x00000001, type),
cd6e9320
TH
8997#define GEN_STX_E(name, stop, opc2, opc3, type, type2) \
8998GEN_HANDLER_E(name##x, 0x1F, opc2, opc3, 0x00000001, type, type2),
5c55ff99
BS
8999#define GEN_STS(name, stop, op, type) \
9000GEN_ST(name, stop, op | 0x20, type) \
9001GEN_STU(name, stop, op | 0x21, type) \
9002GEN_STUX(name, stop, 0x17, op | 0x01, type) \
9003GEN_STX(name, stop, 0x17, op | 0x00, type)
9004
9005GEN_STS(stb, st8, 0x06, PPC_INTEGER)
9006GEN_STS(sth, st16, 0x0C, PPC_INTEGER)
9007GEN_STS(stw, st32, 0x04, PPC_INTEGER)
9008#if defined(TARGET_PPC64)
9009GEN_STUX(std, st64, 0x15, 0x05, PPC_64B)
9010GEN_STX(std, st64, 0x15, 0x04, PPC_64B)
cd6e9320 9011GEN_STX_E(stdbr, st64r, 0x14, 0x14, PPC_NONE, PPC2_DBRX)
5c55ff99
BS
9012#endif
9013GEN_STX(sthbr, st16r, 0x16, 0x1C, PPC_INTEGER)
9014GEN_STX(stwbr, st32r, 0x16, 0x14, PPC_INTEGER)
9015
9016#undef GEN_LDF
9017#undef GEN_LDUF
9018#undef GEN_LDUXF
9019#undef GEN_LDXF
9020#undef GEN_LDFS
9021#define GEN_LDF(name, ldop, opc, type) \
9022GEN_HANDLER(name, opc, 0xFF, 0xFF, 0x00000000, type),
9023#define GEN_LDUF(name, ldop, opc, type) \
9024GEN_HANDLER(name##u, opc, 0xFF, 0xFF, 0x00000000, type),
9025#define GEN_LDUXF(name, ldop, opc, type) \
9026GEN_HANDLER(name##ux, 0x1F, 0x17, opc, 0x00000001, type),
9027#define GEN_LDXF(name, ldop, opc2, opc3, type) \
9028GEN_HANDLER(name##x, 0x1F, opc2, opc3, 0x00000001, type),
9029#define GEN_LDFS(name, ldop, op, type) \
9030GEN_LDF(name, ldop, op | 0x20, type) \
9031GEN_LDUF(name, ldop, op | 0x21, type) \
9032GEN_LDUXF(name, ldop, op | 0x01, type) \
9033GEN_LDXF(name, ldop, 0x17, op | 0x00, type)
9034
9035GEN_LDFS(lfd, ld64, 0x12, PPC_FLOAT)
9036GEN_LDFS(lfs, ld32fs, 0x10, PPC_FLOAT)
199f830d 9037GEN_HANDLER_E(lfiwax, 0x1f, 0x17, 0x1a, 0x00000001, PPC_NONE, PPC2_ISA205),
5c55ff99
BS
9038
9039#undef GEN_STF
9040#undef GEN_STUF
9041#undef GEN_STUXF
9042#undef GEN_STXF
9043#undef GEN_STFS
9044#define GEN_STF(name, stop, opc, type) \
9045GEN_HANDLER(name, opc, 0xFF, 0xFF, 0x00000000, type),
9046#define GEN_STUF(name, stop, opc, type) \
9047GEN_HANDLER(name##u, opc, 0xFF, 0xFF, 0x00000000, type),
9048#define GEN_STUXF(name, stop, opc, type) \
9049GEN_HANDLER(name##ux, 0x1F, 0x17, opc, 0x00000001, type),
9050#define GEN_STXF(name, stop, opc2, opc3, type) \
9051GEN_HANDLER(name##x, 0x1F, opc2, opc3, 0x00000001, type),
9052#define GEN_STFS(name, stop, op, type) \
9053GEN_STF(name, stop, op | 0x20, type) \
9054GEN_STUF(name, stop, op | 0x21, type) \
9055GEN_STUXF(name, stop, op | 0x01, type) \
9056GEN_STXF(name, stop, 0x17, op | 0x00, type)
9057
9058GEN_STFS(stfd, st64, 0x16, PPC_FLOAT)
9059GEN_STFS(stfs, st32fs, 0x14, PPC_FLOAT)
9060GEN_STXF(stfiw, st32fiw, 0x17, 0x1E, PPC_FLOAT_STFIWX)
9061
9062#undef GEN_CRLOGIC
9063#define GEN_CRLOGIC(name, tcg_op, opc) \
9064GEN_HANDLER(name, 0x13, 0x01, opc, 0x00000001, PPC_INTEGER)
9065GEN_CRLOGIC(crand, tcg_gen_and_i32, 0x08),
9066GEN_CRLOGIC(crandc, tcg_gen_andc_i32, 0x04),
9067GEN_CRLOGIC(creqv, tcg_gen_eqv_i32, 0x09),
9068GEN_CRLOGIC(crnand, tcg_gen_nand_i32, 0x07),
9069GEN_CRLOGIC(crnor, tcg_gen_nor_i32, 0x01),
9070GEN_CRLOGIC(cror, tcg_gen_or_i32, 0x0E),
9071GEN_CRLOGIC(crorc, tcg_gen_orc_i32, 0x0D),
9072GEN_CRLOGIC(crxor, tcg_gen_xor_i32, 0x06),
9073
9074#undef GEN_MAC_HANDLER
9075#define GEN_MAC_HANDLER(name, opc2, opc3) \
9076GEN_HANDLER(name, 0x04, opc2, opc3, 0x00000000, PPC_405_MAC)
9077GEN_MAC_HANDLER(macchw, 0x0C, 0x05),
9078GEN_MAC_HANDLER(macchwo, 0x0C, 0x15),
9079GEN_MAC_HANDLER(macchws, 0x0C, 0x07),
9080GEN_MAC_HANDLER(macchwso, 0x0C, 0x17),
9081GEN_MAC_HANDLER(macchwsu, 0x0C, 0x06),
9082GEN_MAC_HANDLER(macchwsuo, 0x0C, 0x16),
9083GEN_MAC_HANDLER(macchwu, 0x0C, 0x04),
9084GEN_MAC_HANDLER(macchwuo, 0x0C, 0x14),
9085GEN_MAC_HANDLER(machhw, 0x0C, 0x01),
9086GEN_MAC_HANDLER(machhwo, 0x0C, 0x11),
9087GEN_MAC_HANDLER(machhws, 0x0C, 0x03),
9088GEN_MAC_HANDLER(machhwso, 0x0C, 0x13),
9089GEN_MAC_HANDLER(machhwsu, 0x0C, 0x02),
9090GEN_MAC_HANDLER(machhwsuo, 0x0C, 0x12),
9091GEN_MAC_HANDLER(machhwu, 0x0C, 0x00),
9092GEN_MAC_HANDLER(machhwuo, 0x0C, 0x10),
9093GEN_MAC_HANDLER(maclhw, 0x0C, 0x0D),
9094GEN_MAC_HANDLER(maclhwo, 0x0C, 0x1D),
9095GEN_MAC_HANDLER(maclhws, 0x0C, 0x0F),
9096GEN_MAC_HANDLER(maclhwso, 0x0C, 0x1F),
9097GEN_MAC_HANDLER(maclhwu, 0x0C, 0x0C),
9098GEN_MAC_HANDLER(maclhwuo, 0x0C, 0x1C),
9099GEN_MAC_HANDLER(maclhwsu, 0x0C, 0x0E),
9100GEN_MAC_HANDLER(maclhwsuo, 0x0C, 0x1E),
9101GEN_MAC_HANDLER(nmacchw, 0x0E, 0x05),
9102GEN_MAC_HANDLER(nmacchwo, 0x0E, 0x15),
9103GEN_MAC_HANDLER(nmacchws, 0x0E, 0x07),
9104GEN_MAC_HANDLER(nmacchwso, 0x0E, 0x17),
9105GEN_MAC_HANDLER(nmachhw, 0x0E, 0x01),
9106GEN_MAC_HANDLER(nmachhwo, 0x0E, 0x11),
9107GEN_MAC_HANDLER(nmachhws, 0x0E, 0x03),
9108GEN_MAC_HANDLER(nmachhwso, 0x0E, 0x13),
9109GEN_MAC_HANDLER(nmaclhw, 0x0E, 0x0D),
9110GEN_MAC_HANDLER(nmaclhwo, 0x0E, 0x1D),
9111GEN_MAC_HANDLER(nmaclhws, 0x0E, 0x0F),
9112GEN_MAC_HANDLER(nmaclhwso, 0x0E, 0x1F),
9113GEN_MAC_HANDLER(mulchw, 0x08, 0x05),
9114GEN_MAC_HANDLER(mulchwu, 0x08, 0x04),
9115GEN_MAC_HANDLER(mulhhw, 0x08, 0x01),
9116GEN_MAC_HANDLER(mulhhwu, 0x08, 0x00),
9117GEN_MAC_HANDLER(mullhw, 0x08, 0x0D),
9118GEN_MAC_HANDLER(mullhwu, 0x08, 0x0C),
9119
9120#undef GEN_VR_LDX
9121#undef GEN_VR_STX
9122#undef GEN_VR_LVE
9123#undef GEN_VR_STVE
9124#define GEN_VR_LDX(name, opc2, opc3) \
9125GEN_HANDLER(name, 0x1F, opc2, opc3, 0x00000001, PPC_ALTIVEC)
9126#define GEN_VR_STX(name, opc2, opc3) \
9127GEN_HANDLER(st##name, 0x1F, opc2, opc3, 0x00000001, PPC_ALTIVEC)
9128#define GEN_VR_LVE(name, opc2, opc3) \
9129 GEN_HANDLER(lve##name, 0x1F, opc2, opc3, 0x00000001, PPC_ALTIVEC)
9130#define GEN_VR_STVE(name, opc2, opc3) \
9131 GEN_HANDLER(stve##name, 0x1F, opc2, opc3, 0x00000001, PPC_ALTIVEC)
9132GEN_VR_LDX(lvx, 0x07, 0x03),
9133GEN_VR_LDX(lvxl, 0x07, 0x0B),
9134GEN_VR_LVE(bx, 0x07, 0x00),
9135GEN_VR_LVE(hx, 0x07, 0x01),
9136GEN_VR_LVE(wx, 0x07, 0x02),
9137GEN_VR_STX(svx, 0x07, 0x07),
9138GEN_VR_STX(svxl, 0x07, 0x0F),
9139GEN_VR_STVE(bx, 0x07, 0x04),
9140GEN_VR_STVE(hx, 0x07, 0x05),
9141GEN_VR_STVE(wx, 0x07, 0x06),
9142
9143#undef GEN_VX_LOGICAL
9144#define GEN_VX_LOGICAL(name, tcg_op, opc2, opc3) \
9145GEN_HANDLER(name, 0x04, opc2, opc3, 0x00000000, PPC_ALTIVEC)
9146GEN_VX_LOGICAL(vand, tcg_gen_and_i64, 2, 16),
9147GEN_VX_LOGICAL(vandc, tcg_gen_andc_i64, 2, 17),
9148GEN_VX_LOGICAL(vor, tcg_gen_or_i64, 2, 18),
9149GEN_VX_LOGICAL(vxor, tcg_gen_xor_i64, 2, 19),
9150GEN_VX_LOGICAL(vnor, tcg_gen_nor_i64, 2, 20),
9151
9152#undef GEN_VXFORM
9153#define GEN_VXFORM(name, opc2, opc3) \
9154GEN_HANDLER(name, 0x04, opc2, opc3, 0x00000000, PPC_ALTIVEC)
9155GEN_VXFORM(vaddubm, 0, 0),
9156GEN_VXFORM(vadduhm, 0, 1),
9157GEN_VXFORM(vadduwm, 0, 2),
9158GEN_VXFORM(vsububm, 0, 16),
9159GEN_VXFORM(vsubuhm, 0, 17),
9160GEN_VXFORM(vsubuwm, 0, 18),
9161GEN_VXFORM(vmaxub, 1, 0),
9162GEN_VXFORM(vmaxuh, 1, 1),
9163GEN_VXFORM(vmaxuw, 1, 2),
9164GEN_VXFORM(vmaxsb, 1, 4),
9165GEN_VXFORM(vmaxsh, 1, 5),
9166GEN_VXFORM(vmaxsw, 1, 6),
9167GEN_VXFORM(vminub, 1, 8),
9168GEN_VXFORM(vminuh, 1, 9),
9169GEN_VXFORM(vminuw, 1, 10),
9170GEN_VXFORM(vminsb, 1, 12),
9171GEN_VXFORM(vminsh, 1, 13),
9172GEN_VXFORM(vminsw, 1, 14),
9173GEN_VXFORM(vavgub, 1, 16),
9174GEN_VXFORM(vavguh, 1, 17),
9175GEN_VXFORM(vavguw, 1, 18),
9176GEN_VXFORM(vavgsb, 1, 20),
9177GEN_VXFORM(vavgsh, 1, 21),
9178GEN_VXFORM(vavgsw, 1, 22),
9179GEN_VXFORM(vmrghb, 6, 0),
9180GEN_VXFORM(vmrghh, 6, 1),
9181GEN_VXFORM(vmrghw, 6, 2),
9182GEN_VXFORM(vmrglb, 6, 4),
9183GEN_VXFORM(vmrglh, 6, 5),
9184GEN_VXFORM(vmrglw, 6, 6),
9185GEN_VXFORM(vmuloub, 4, 0),
9186GEN_VXFORM(vmulouh, 4, 1),
9187GEN_VXFORM(vmulosb, 4, 4),
9188GEN_VXFORM(vmulosh, 4, 5),
9189GEN_VXFORM(vmuleub, 4, 8),
9190GEN_VXFORM(vmuleuh, 4, 9),
9191GEN_VXFORM(vmulesb, 4, 12),
9192GEN_VXFORM(vmulesh, 4, 13),
9193GEN_VXFORM(vslb, 2, 4),
9194GEN_VXFORM(vslh, 2, 5),
9195GEN_VXFORM(vslw, 2, 6),
9196GEN_VXFORM(vsrb, 2, 8),
9197GEN_VXFORM(vsrh, 2, 9),
9198GEN_VXFORM(vsrw, 2, 10),
9199GEN_VXFORM(vsrab, 2, 12),
9200GEN_VXFORM(vsrah, 2, 13),
9201GEN_VXFORM(vsraw, 2, 14),
9202GEN_VXFORM(vslo, 6, 16),
9203GEN_VXFORM(vsro, 6, 17),
9204GEN_VXFORM(vaddcuw, 0, 6),
9205GEN_VXFORM(vsubcuw, 0, 22),
9206GEN_VXFORM(vaddubs, 0, 8),
9207GEN_VXFORM(vadduhs, 0, 9),
9208GEN_VXFORM(vadduws, 0, 10),
9209GEN_VXFORM(vaddsbs, 0, 12),
9210GEN_VXFORM(vaddshs, 0, 13),
9211GEN_VXFORM(vaddsws, 0, 14),
9212GEN_VXFORM(vsububs, 0, 24),
9213GEN_VXFORM(vsubuhs, 0, 25),
9214GEN_VXFORM(vsubuws, 0, 26),
9215GEN_VXFORM(vsubsbs, 0, 28),
9216GEN_VXFORM(vsubshs, 0, 29),
9217GEN_VXFORM(vsubsws, 0, 30),
9218GEN_VXFORM(vrlb, 2, 0),
9219GEN_VXFORM(vrlh, 2, 1),
9220GEN_VXFORM(vrlw, 2, 2),
9221GEN_VXFORM(vsl, 2, 7),
9222GEN_VXFORM(vsr, 2, 11),
9223GEN_VXFORM(vpkuhum, 7, 0),
9224GEN_VXFORM(vpkuwum, 7, 1),
9225GEN_VXFORM(vpkuhus, 7, 2),
9226GEN_VXFORM(vpkuwus, 7, 3),
9227GEN_VXFORM(vpkshus, 7, 4),
9228GEN_VXFORM(vpkswus, 7, 5),
9229GEN_VXFORM(vpkshss, 7, 6),
9230GEN_VXFORM(vpkswss, 7, 7),
9231GEN_VXFORM(vpkpx, 7, 12),
9232GEN_VXFORM(vsum4ubs, 4, 24),
9233GEN_VXFORM(vsum4sbs, 4, 28),
9234GEN_VXFORM(vsum4shs, 4, 25),
9235GEN_VXFORM(vsum2sws, 4, 26),
9236GEN_VXFORM(vsumsws, 4, 30),
9237GEN_VXFORM(vaddfp, 5, 0),
9238GEN_VXFORM(vsubfp, 5, 1),
9239GEN_VXFORM(vmaxfp, 5, 16),
9240GEN_VXFORM(vminfp, 5, 17),
9241
9242#undef GEN_VXRFORM1
9243#undef GEN_VXRFORM
9244#define GEN_VXRFORM1(opname, name, str, opc2, opc3) \
9245 GEN_HANDLER2(name, str, 0x4, opc2, opc3, 0x00000000, PPC_ALTIVEC),
9246#define GEN_VXRFORM(name, opc2, opc3) \
9247 GEN_VXRFORM1(name, name, #name, opc2, opc3) \
9248 GEN_VXRFORM1(name##_dot, name##_, #name ".", opc2, (opc3 | (0x1 << 4)))
9249GEN_VXRFORM(vcmpequb, 3, 0)
9250GEN_VXRFORM(vcmpequh, 3, 1)
9251GEN_VXRFORM(vcmpequw, 3, 2)
9252GEN_VXRFORM(vcmpgtsb, 3, 12)
9253GEN_VXRFORM(vcmpgtsh, 3, 13)
9254GEN_VXRFORM(vcmpgtsw, 3, 14)
9255GEN_VXRFORM(vcmpgtub, 3, 8)
9256GEN_VXRFORM(vcmpgtuh, 3, 9)
9257GEN_VXRFORM(vcmpgtuw, 3, 10)
9258GEN_VXRFORM(vcmpeqfp, 3, 3)
9259GEN_VXRFORM(vcmpgefp, 3, 7)
9260GEN_VXRFORM(vcmpgtfp, 3, 11)
9261GEN_VXRFORM(vcmpbfp, 3, 15)
9262
9263#undef GEN_VXFORM_SIMM
9264#define GEN_VXFORM_SIMM(name, opc2, opc3) \
9265 GEN_HANDLER(name, 0x04, opc2, opc3, 0x00000000, PPC_ALTIVEC)
9266GEN_VXFORM_SIMM(vspltisb, 6, 12),
9267GEN_VXFORM_SIMM(vspltish, 6, 13),
9268GEN_VXFORM_SIMM(vspltisw, 6, 14),
9269
9270#undef GEN_VXFORM_NOA
9271#define GEN_VXFORM_NOA(name, opc2, opc3) \
9272 GEN_HANDLER(name, 0x04, opc2, opc3, 0x001f0000, PPC_ALTIVEC)
9273GEN_VXFORM_NOA(vupkhsb, 7, 8),
9274GEN_VXFORM_NOA(vupkhsh, 7, 9),
9275GEN_VXFORM_NOA(vupklsb, 7, 10),
9276GEN_VXFORM_NOA(vupklsh, 7, 11),
9277GEN_VXFORM_NOA(vupkhpx, 7, 13),
9278GEN_VXFORM_NOA(vupklpx, 7, 15),
9279GEN_VXFORM_NOA(vrefp, 5, 4),
9280GEN_VXFORM_NOA(vrsqrtefp, 5, 5),
0bffbc6c 9281GEN_VXFORM_NOA(vexptefp, 5, 6),
5c55ff99
BS
9282GEN_VXFORM_NOA(vlogefp, 5, 7),
9283GEN_VXFORM_NOA(vrfim, 5, 8),
9284GEN_VXFORM_NOA(vrfin, 5, 9),
9285GEN_VXFORM_NOA(vrfip, 5, 10),
9286GEN_VXFORM_NOA(vrfiz, 5, 11),
9287
9288#undef GEN_VXFORM_UIMM
9289#define GEN_VXFORM_UIMM(name, opc2, opc3) \
9290 GEN_HANDLER(name, 0x04, opc2, opc3, 0x00000000, PPC_ALTIVEC)
9291GEN_VXFORM_UIMM(vspltb, 6, 8),
9292GEN_VXFORM_UIMM(vsplth, 6, 9),
9293GEN_VXFORM_UIMM(vspltw, 6, 10),
9294GEN_VXFORM_UIMM(vcfux, 5, 12),
9295GEN_VXFORM_UIMM(vcfsx, 5, 13),
9296GEN_VXFORM_UIMM(vctuxs, 5, 14),
9297GEN_VXFORM_UIMM(vctsxs, 5, 15),
9298
9299#undef GEN_VAFORM_PAIRED
9300#define GEN_VAFORM_PAIRED(name0, name1, opc2) \
9301 GEN_HANDLER(name0##_##name1, 0x04, opc2, 0xFF, 0x00000000, PPC_ALTIVEC)
9302GEN_VAFORM_PAIRED(vmhaddshs, vmhraddshs, 16),
9303GEN_VAFORM_PAIRED(vmsumubm, vmsummbm, 18),
9304GEN_VAFORM_PAIRED(vmsumuhm, vmsumuhs, 19),
9305GEN_VAFORM_PAIRED(vmsumshm, vmsumshs, 20),
9306GEN_VAFORM_PAIRED(vsel, vperm, 21),
9307GEN_VAFORM_PAIRED(vmaddfp, vnmsubfp, 23),
9308
9309#undef GEN_SPE
70560da7
FC
9310#define GEN_SPE(name0, name1, opc2, opc3, inval0, inval1, type) \
9311 GEN_OPCODE_DUAL(name0##_##name1, 0x04, opc2, opc3, inval0, inval1, type, PPC_NONE)
9312GEN_SPE(evaddw, speundef, 0x00, 0x08, 0x00000000, 0xFFFFFFFF, PPC_SPE),
9313GEN_SPE(evaddiw, speundef, 0x01, 0x08, 0x00000000, 0xFFFFFFFF, PPC_SPE),
9314GEN_SPE(evsubfw, speundef, 0x02, 0x08, 0x00000000, 0xFFFFFFFF, PPC_SPE),
9315GEN_SPE(evsubifw, speundef, 0x03, 0x08, 0x00000000, 0xFFFFFFFF, PPC_SPE),
9316GEN_SPE(evabs, evneg, 0x04, 0x08, 0x0000F800, 0x0000F800, PPC_SPE),
9317GEN_SPE(evextsb, evextsh, 0x05, 0x08, 0x0000F800, 0x0000F800, PPC_SPE),
9318GEN_SPE(evrndw, evcntlzw, 0x06, 0x08, 0x0000F800, 0x0000F800, PPC_SPE),
9319GEN_SPE(evcntlsw, brinc, 0x07, 0x08, 0x0000F800, 0x00000000, PPC_SPE),
9320GEN_SPE(evmra, speundef, 0x02, 0x13, 0x0000F800, 0xFFFFFFFF, PPC_SPE),
9321GEN_SPE(speundef, evand, 0x08, 0x08, 0xFFFFFFFF, 0x00000000, PPC_SPE),
9322GEN_SPE(evandc, speundef, 0x09, 0x08, 0x00000000, 0xFFFFFFFF, PPC_SPE),
9323GEN_SPE(evxor, evor, 0x0B, 0x08, 0x00000000, 0x00000000, PPC_SPE),
9324GEN_SPE(evnor, eveqv, 0x0C, 0x08, 0x00000000, 0x00000000, PPC_SPE),
9325GEN_SPE(evmwumi, evmwsmi, 0x0C, 0x11, 0x00000000, 0x00000000, PPC_SPE),
9326GEN_SPE(evmwumia, evmwsmia, 0x1C, 0x11, 0x00000000, 0x00000000, PPC_SPE),
9327GEN_SPE(evmwumiaa, evmwsmiaa, 0x0C, 0x15, 0x00000000, 0x00000000, PPC_SPE),
9328GEN_SPE(speundef, evorc, 0x0D, 0x08, 0xFFFFFFFF, 0x00000000, PPC_SPE),
9329GEN_SPE(evnand, speundef, 0x0F, 0x08, 0x00000000, 0xFFFFFFFF, PPC_SPE),
9330GEN_SPE(evsrwu, evsrws, 0x10, 0x08, 0x00000000, 0x00000000, PPC_SPE),
9331GEN_SPE(evsrwiu, evsrwis, 0x11, 0x08, 0x00000000, 0x00000000, PPC_SPE),
9332GEN_SPE(evslw, speundef, 0x12, 0x08, 0x00000000, 0xFFFFFFFF, PPC_SPE),
9333GEN_SPE(evslwi, speundef, 0x13, 0x08, 0x00000000, 0xFFFFFFFF, PPC_SPE),
9334GEN_SPE(evrlw, evsplati, 0x14, 0x08, 0x00000000, 0x0000F800, PPC_SPE),
9335GEN_SPE(evrlwi, evsplatfi, 0x15, 0x08, 0x00000000, 0x0000F800, PPC_SPE),
9336GEN_SPE(evmergehi, evmergelo, 0x16, 0x08, 0x00000000, 0x00000000, PPC_SPE),
9337GEN_SPE(evmergehilo, evmergelohi, 0x17, 0x08, 0x00000000, 0x00000000, PPC_SPE),
9338GEN_SPE(evcmpgtu, evcmpgts, 0x18, 0x08, 0x00600000, 0x00600000, PPC_SPE),
9339GEN_SPE(evcmpltu, evcmplts, 0x19, 0x08, 0x00600000, 0x00600000, PPC_SPE),
9340GEN_SPE(evcmpeq, speundef, 0x1A, 0x08, 0x00600000, 0xFFFFFFFF, PPC_SPE),
9341
9342GEN_SPE(evfsadd, evfssub, 0x00, 0x0A, 0x00000000, 0x00000000, PPC_SPE_SINGLE),
9343GEN_SPE(evfsabs, evfsnabs, 0x02, 0x0A, 0x0000F800, 0x0000F800, PPC_SPE_SINGLE),
9344GEN_SPE(evfsneg, speundef, 0x03, 0x0A, 0x0000F800, 0xFFFFFFFF, PPC_SPE_SINGLE),
9345GEN_SPE(evfsmul, evfsdiv, 0x04, 0x0A, 0x00000000, 0x00000000, PPC_SPE_SINGLE),
9346GEN_SPE(evfscmpgt, evfscmplt, 0x06, 0x0A, 0x00600000, 0x00600000, PPC_SPE_SINGLE),
9347GEN_SPE(evfscmpeq, speundef, 0x07, 0x0A, 0x00600000, 0xFFFFFFFF, PPC_SPE_SINGLE),
9348GEN_SPE(evfscfui, evfscfsi, 0x08, 0x0A, 0x00180000, 0x00180000, PPC_SPE_SINGLE),
9349GEN_SPE(evfscfuf, evfscfsf, 0x09, 0x0A, 0x00180000, 0x00180000, PPC_SPE_SINGLE),
9350GEN_SPE(evfsctui, evfsctsi, 0x0A, 0x0A, 0x00180000, 0x00180000, PPC_SPE_SINGLE),
9351GEN_SPE(evfsctuf, evfsctsf, 0x0B, 0x0A, 0x00180000, 0x00180000, PPC_SPE_SINGLE),
9352GEN_SPE(evfsctuiz, speundef, 0x0C, 0x0A, 0x00180000, 0xFFFFFFFF, PPC_SPE_SINGLE),
9353GEN_SPE(evfsctsiz, speundef, 0x0D, 0x0A, 0x00180000, 0xFFFFFFFF, PPC_SPE_SINGLE),
9354GEN_SPE(evfststgt, evfststlt, 0x0E, 0x0A, 0x00600000, 0x00600000, PPC_SPE_SINGLE),
9355GEN_SPE(evfststeq, speundef, 0x0F, 0x0A, 0x00600000, 0xFFFFFFFF, PPC_SPE_SINGLE),
9356
9357GEN_SPE(efsadd, efssub, 0x00, 0x0B, 0x00000000, 0x00000000, PPC_SPE_SINGLE),
9358GEN_SPE(efsabs, efsnabs, 0x02, 0x0B, 0x0000F800, 0x0000F800, PPC_SPE_SINGLE),
9359GEN_SPE(efsneg, speundef, 0x03, 0x0B, 0x0000F800, 0xFFFFFFFF, PPC_SPE_SINGLE),
9360GEN_SPE(efsmul, efsdiv, 0x04, 0x0B, 0x00000000, 0x00000000, PPC_SPE_SINGLE),
9361GEN_SPE(efscmpgt, efscmplt, 0x06, 0x0B, 0x00600000, 0x00600000, PPC_SPE_SINGLE),
9362GEN_SPE(efscmpeq, efscfd, 0x07, 0x0B, 0x00600000, 0x00180000, PPC_SPE_SINGLE),
9363GEN_SPE(efscfui, efscfsi, 0x08, 0x0B, 0x00180000, 0x00180000, PPC_SPE_SINGLE),
9364GEN_SPE(efscfuf, efscfsf, 0x09, 0x0B, 0x00180000, 0x00180000, PPC_SPE_SINGLE),
9365GEN_SPE(efsctui, efsctsi, 0x0A, 0x0B, 0x00180000, 0x00180000, PPC_SPE_SINGLE),
9366GEN_SPE(efsctuf, efsctsf, 0x0B, 0x0B, 0x00180000, 0x00180000, PPC_SPE_SINGLE),
9367GEN_SPE(efsctuiz, speundef, 0x0C, 0x0B, 0x00180000, 0xFFFFFFFF, PPC_SPE_SINGLE),
9368GEN_SPE(efsctsiz, speundef, 0x0D, 0x0B, 0x00180000, 0xFFFFFFFF, PPC_SPE_SINGLE),
9369GEN_SPE(efststgt, efststlt, 0x0E, 0x0B, 0x00600000, 0x00600000, PPC_SPE_SINGLE),
9370GEN_SPE(efststeq, speundef, 0x0F, 0x0B, 0x00600000, 0xFFFFFFFF, PPC_SPE_SINGLE),
9371
9372GEN_SPE(efdadd, efdsub, 0x10, 0x0B, 0x00000000, 0x00000000, PPC_SPE_DOUBLE),
9373GEN_SPE(efdcfuid, efdcfsid, 0x11, 0x0B, 0x00180000, 0x00180000, PPC_SPE_DOUBLE),
9374GEN_SPE(efdabs, efdnabs, 0x12, 0x0B, 0x0000F800, 0x0000F800, PPC_SPE_DOUBLE),
9375GEN_SPE(efdneg, speundef, 0x13, 0x0B, 0x0000F800, 0xFFFFFFFF, PPC_SPE_DOUBLE),
9376GEN_SPE(efdmul, efddiv, 0x14, 0x0B, 0x00000000, 0x00000000, PPC_SPE_DOUBLE),
9377GEN_SPE(efdctuidz, efdctsidz, 0x15, 0x0B, 0x00180000, 0x00180000, PPC_SPE_DOUBLE),
9378GEN_SPE(efdcmpgt, efdcmplt, 0x16, 0x0B, 0x00600000, 0x00600000, PPC_SPE_DOUBLE),
9379GEN_SPE(efdcmpeq, efdcfs, 0x17, 0x0B, 0x00600000, 0x00180000, PPC_SPE_DOUBLE),
9380GEN_SPE(efdcfui, efdcfsi, 0x18, 0x0B, 0x00180000, 0x00180000, PPC_SPE_DOUBLE),
9381GEN_SPE(efdcfuf, efdcfsf, 0x19, 0x0B, 0x00180000, 0x00180000, PPC_SPE_DOUBLE),
9382GEN_SPE(efdctui, efdctsi, 0x1A, 0x0B, 0x00180000, 0x00180000, PPC_SPE_DOUBLE),
9383GEN_SPE(efdctuf, efdctsf, 0x1B, 0x0B, 0x00180000, 0x00180000, PPC_SPE_DOUBLE),
9384GEN_SPE(efdctuiz, speundef, 0x1C, 0x0B, 0x00180000, 0xFFFFFFFF, PPC_SPE_DOUBLE),
9385GEN_SPE(efdctsiz, speundef, 0x1D, 0x0B, 0x00180000, 0xFFFFFFFF, PPC_SPE_DOUBLE),
9386GEN_SPE(efdtstgt, efdtstlt, 0x1E, 0x0B, 0x00600000, 0x00600000, PPC_SPE_DOUBLE),
9387GEN_SPE(efdtsteq, speundef, 0x1F, 0x0B, 0x00600000, 0xFFFFFFFF, PPC_SPE_DOUBLE),
5c55ff99
BS
9388
9389#undef GEN_SPEOP_LDST
9390#define GEN_SPEOP_LDST(name, opc2, sh) \
9391GEN_HANDLER(name, 0x04, opc2, 0x0C, 0x00000000, PPC_SPE)
9392GEN_SPEOP_LDST(evldd, 0x00, 3),
9393GEN_SPEOP_LDST(evldw, 0x01, 3),
9394GEN_SPEOP_LDST(evldh, 0x02, 3),
9395GEN_SPEOP_LDST(evlhhesplat, 0x04, 1),
9396GEN_SPEOP_LDST(evlhhousplat, 0x06, 1),
9397GEN_SPEOP_LDST(evlhhossplat, 0x07, 1),
9398GEN_SPEOP_LDST(evlwhe, 0x08, 2),
9399GEN_SPEOP_LDST(evlwhou, 0x0A, 2),
9400GEN_SPEOP_LDST(evlwhos, 0x0B, 2),
9401GEN_SPEOP_LDST(evlwwsplat, 0x0C, 2),
9402GEN_SPEOP_LDST(evlwhsplat, 0x0E, 2),
9403
9404GEN_SPEOP_LDST(evstdd, 0x10, 3),
9405GEN_SPEOP_LDST(evstdw, 0x11, 3),
9406GEN_SPEOP_LDST(evstdh, 0x12, 3),
9407GEN_SPEOP_LDST(evstwhe, 0x18, 2),
9408GEN_SPEOP_LDST(evstwho, 0x1A, 2),
9409GEN_SPEOP_LDST(evstwwe, 0x1C, 2),
9410GEN_SPEOP_LDST(evstwwo, 0x1E, 2),
9411};
9412
0411a972 9413#include "helper_regs.h"
a1389542 9414#include "translate_init.c"
79aceca5 9415
9a64fbe4 9416/*****************************************************************************/
3fc6c082 9417/* Misc PowerPC helpers */
1328c2bf 9418void cpu_dump_state (CPUPPCState *env, FILE *f, fprintf_function cpu_fprintf,
36081602 9419 int flags)
79aceca5 9420{
3fc6c082
FB
9421#define RGPL 4
9422#define RFPL 4
3fc6c082 9423
79aceca5
FB
9424 int i;
9425
29979a8d
AG
9426 cpu_synchronize_state(env);
9427
90e189ec 9428 cpu_fprintf(f, "NIP " TARGET_FMT_lx " LR " TARGET_FMT_lx " CTR "
9a78eead 9429 TARGET_FMT_lx " XER " TARGET_FMT_lx "\n",
da91a00f 9430 env->nip, env->lr, env->ctr, cpu_read_xer(env));
90e189ec
BS
9431 cpu_fprintf(f, "MSR " TARGET_FMT_lx " HID0 " TARGET_FMT_lx " HF "
9432 TARGET_FMT_lx " idx %d\n", env->msr, env->spr[SPR_HID0],
9433 env->hflags, env->mmu_idx);
d9bce9d9 9434#if !defined(NO_TIMER_DUMP)
9a78eead 9435 cpu_fprintf(f, "TB %08" PRIu32 " %08" PRIu64
76a66253 9436#if !defined(CONFIG_USER_ONLY)
9a78eead 9437 " DECR %08" PRIu32
76a66253
JM
9438#endif
9439 "\n",
077fc206 9440 cpu_ppc_load_tbu(env), cpu_ppc_load_tbl(env)
76a66253
JM
9441#if !defined(CONFIG_USER_ONLY)
9442 , cpu_ppc_load_decr(env)
9443#endif
9444 );
077fc206 9445#endif
76a66253 9446 for (i = 0; i < 32; i++) {
3fc6c082
FB
9447 if ((i & (RGPL - 1)) == 0)
9448 cpu_fprintf(f, "GPR%02d", i);
b11ebf64 9449 cpu_fprintf(f, " %016" PRIx64, ppc_dump_gpr(env, i));
3fc6c082 9450 if ((i & (RGPL - 1)) == (RGPL - 1))
7fe48483 9451 cpu_fprintf(f, "\n");
76a66253 9452 }
3fc6c082 9453 cpu_fprintf(f, "CR ");
76a66253 9454 for (i = 0; i < 8; i++)
7fe48483
FB
9455 cpu_fprintf(f, "%01x", env->crf[i]);
9456 cpu_fprintf(f, " [");
76a66253
JM
9457 for (i = 0; i < 8; i++) {
9458 char a = '-';
9459 if (env->crf[i] & 0x08)
9460 a = 'L';
9461 else if (env->crf[i] & 0x04)
9462 a = 'G';
9463 else if (env->crf[i] & 0x02)
9464 a = 'E';
7fe48483 9465 cpu_fprintf(f, " %c%c", a, env->crf[i] & 0x01 ? 'O' : ' ');
76a66253 9466 }
90e189ec
BS
9467 cpu_fprintf(f, " ] RES " TARGET_FMT_lx "\n",
9468 env->reserve_addr);
3fc6c082
FB
9469 for (i = 0; i < 32; i++) {
9470 if ((i & (RFPL - 1)) == 0)
9471 cpu_fprintf(f, "FPR%02d", i);
26a76461 9472 cpu_fprintf(f, " %016" PRIx64, *((uint64_t *)&env->fpr[i]));
3fc6c082 9473 if ((i & (RFPL - 1)) == (RFPL - 1))
7fe48483 9474 cpu_fprintf(f, "\n");
79aceca5 9475 }
30304420 9476 cpu_fprintf(f, "FPSCR " TARGET_FMT_lx "\n", env->fpscr);
f2e63a42 9477#if !defined(CONFIG_USER_ONLY)
90dc8812
SW
9478 cpu_fprintf(f, " SRR0 " TARGET_FMT_lx " SRR1 " TARGET_FMT_lx
9479 " PVR " TARGET_FMT_lx " VRSAVE " TARGET_FMT_lx "\n",
9480 env->spr[SPR_SRR0], env->spr[SPR_SRR1],
9481 env->spr[SPR_PVR], env->spr[SPR_VRSAVE]);
9482
9483 cpu_fprintf(f, "SPRG0 " TARGET_FMT_lx " SPRG1 " TARGET_FMT_lx
9484 " SPRG2 " TARGET_FMT_lx " SPRG3 " TARGET_FMT_lx "\n",
9485 env->spr[SPR_SPRG0], env->spr[SPR_SPRG1],
9486 env->spr[SPR_SPRG2], env->spr[SPR_SPRG3]);
9487
9488 cpu_fprintf(f, "SPRG4 " TARGET_FMT_lx " SPRG5 " TARGET_FMT_lx
9489 " SPRG6 " TARGET_FMT_lx " SPRG7 " TARGET_FMT_lx "\n",
9490 env->spr[SPR_SPRG4], env->spr[SPR_SPRG5],
9491 env->spr[SPR_SPRG6], env->spr[SPR_SPRG7]);
9492
9493 if (env->excp_model == POWERPC_EXCP_BOOKE) {
9494 cpu_fprintf(f, "CSRR0 " TARGET_FMT_lx " CSRR1 " TARGET_FMT_lx
9495 " MCSRR0 " TARGET_FMT_lx " MCSRR1 " TARGET_FMT_lx "\n",
9496 env->spr[SPR_BOOKE_CSRR0], env->spr[SPR_BOOKE_CSRR1],
9497 env->spr[SPR_BOOKE_MCSRR0], env->spr[SPR_BOOKE_MCSRR1]);
9498
9499 cpu_fprintf(f, " TCR " TARGET_FMT_lx " TSR " TARGET_FMT_lx
9500 " ESR " TARGET_FMT_lx " DEAR " TARGET_FMT_lx "\n",
9501 env->spr[SPR_BOOKE_TCR], env->spr[SPR_BOOKE_TSR],
9502 env->spr[SPR_BOOKE_ESR], env->spr[SPR_BOOKE_DEAR]);
9503
9504 cpu_fprintf(f, " PIR " TARGET_FMT_lx " DECAR " TARGET_FMT_lx
9505 " IVPR " TARGET_FMT_lx " EPCR " TARGET_FMT_lx "\n",
9506 env->spr[SPR_BOOKE_PIR], env->spr[SPR_BOOKE_DECAR],
9507 env->spr[SPR_BOOKE_IVPR], env->spr[SPR_BOOKE_EPCR]);
9508
9509 cpu_fprintf(f, " MCSR " TARGET_FMT_lx " SPRG8 " TARGET_FMT_lx
9510 " EPR " TARGET_FMT_lx "\n",
9511 env->spr[SPR_BOOKE_MCSR], env->spr[SPR_BOOKE_SPRG8],
9512 env->spr[SPR_BOOKE_EPR]);
9513
9514 /* FSL-specific */
9515 cpu_fprintf(f, " MCAR " TARGET_FMT_lx " PID1 " TARGET_FMT_lx
9516 " PID2 " TARGET_FMT_lx " SVR " TARGET_FMT_lx "\n",
9517 env->spr[SPR_Exxx_MCAR], env->spr[SPR_BOOKE_PID1],
9518 env->spr[SPR_BOOKE_PID2], env->spr[SPR_E500_SVR]);
9519
9520 /*
9521 * IVORs are left out as they are large and do not change often --
9522 * they can be read with "p $ivor0", "p $ivor1", etc.
9523 */
9524 }
9525
697ab892
DG
9526#if defined(TARGET_PPC64)
9527 if (env->flags & POWERPC_FLAG_CFAR) {
9528 cpu_fprintf(f, " CFAR " TARGET_FMT_lx"\n", env->cfar);
9529 }
9530#endif
9531
90dc8812
SW
9532 switch (env->mmu_model) {
9533 case POWERPC_MMU_32B:
9534 case POWERPC_MMU_601:
9535 case POWERPC_MMU_SOFT_6xx:
9536 case POWERPC_MMU_SOFT_74xx:
9537#if defined(TARGET_PPC64)
90dc8812
SW
9538 case POWERPC_MMU_64B:
9539#endif
9540 cpu_fprintf(f, " SDR1 " TARGET_FMT_lx "\n", env->spr[SPR_SDR1]);
9541 break;
01662f3e 9542 case POWERPC_MMU_BOOKE206:
90dc8812
SW
9543 cpu_fprintf(f, " MAS0 " TARGET_FMT_lx " MAS1 " TARGET_FMT_lx
9544 " MAS2 " TARGET_FMT_lx " MAS3 " TARGET_FMT_lx "\n",
9545 env->spr[SPR_BOOKE_MAS0], env->spr[SPR_BOOKE_MAS1],
9546 env->spr[SPR_BOOKE_MAS2], env->spr[SPR_BOOKE_MAS3]);
9547
9548 cpu_fprintf(f, " MAS4 " TARGET_FMT_lx " MAS6 " TARGET_FMT_lx
9549 " MAS7 " TARGET_FMT_lx " PID " TARGET_FMT_lx "\n",
9550 env->spr[SPR_BOOKE_MAS4], env->spr[SPR_BOOKE_MAS6],
9551 env->spr[SPR_BOOKE_MAS7], env->spr[SPR_BOOKE_PID]);
9552
9553 cpu_fprintf(f, "MMUCFG " TARGET_FMT_lx " TLB0CFG " TARGET_FMT_lx
9554 " TLB1CFG " TARGET_FMT_lx "\n",
9555 env->spr[SPR_MMUCFG], env->spr[SPR_BOOKE_TLB0CFG],
9556 env->spr[SPR_BOOKE_TLB1CFG]);
9557 break;
9558 default:
9559 break;
9560 }
f2e63a42 9561#endif
79aceca5 9562
3fc6c082
FB
9563#undef RGPL
9564#undef RFPL
79aceca5
FB
9565}
9566
1328c2bf 9567void cpu_dump_statistics (CPUPPCState *env, FILE*f, fprintf_function cpu_fprintf,
76a66253
JM
9568 int flags)
9569{
9570#if defined(DO_PPC_STATISTICS)
c227f099 9571 opc_handler_t **t1, **t2, **t3, *handler;
76a66253
JM
9572 int op1, op2, op3;
9573
9574 t1 = env->opcodes;
9575 for (op1 = 0; op1 < 64; op1++) {
9576 handler = t1[op1];
9577 if (is_indirect_opcode(handler)) {
9578 t2 = ind_table(handler);
9579 for (op2 = 0; op2 < 32; op2++) {
9580 handler = t2[op2];
9581 if (is_indirect_opcode(handler)) {
9582 t3 = ind_table(handler);
9583 for (op3 = 0; op3 < 32; op3++) {
9584 handler = t3[op3];
9585 if (handler->count == 0)
9586 continue;
9587 cpu_fprintf(f, "%02x %02x %02x (%02x %04d) %16s: "
0bfcd599 9588 "%016" PRIx64 " %" PRId64 "\n",
76a66253
JM
9589 op1, op2, op3, op1, (op3 << 5) | op2,
9590 handler->oname,
9591 handler->count, handler->count);
9592 }
9593 } else {
9594 if (handler->count == 0)
9595 continue;
9596 cpu_fprintf(f, "%02x %02x (%02x %04d) %16s: "
0bfcd599 9597 "%016" PRIx64 " %" PRId64 "\n",
76a66253
JM
9598 op1, op2, op1, op2, handler->oname,
9599 handler->count, handler->count);
9600 }
9601 }
9602 } else {
9603 if (handler->count == 0)
9604 continue;
0bfcd599
BS
9605 cpu_fprintf(f, "%02x (%02x ) %16s: %016" PRIx64
9606 " %" PRId64 "\n",
76a66253
JM
9607 op1, op1, handler->oname,
9608 handler->count, handler->count);
9609 }
9610 }
9611#endif
9612}
9613
9a64fbe4 9614/*****************************************************************************/
1328c2bf 9615static inline void gen_intermediate_code_internal(CPUPPCState *env,
636aa200
BS
9616 TranslationBlock *tb,
9617 int search_pc)
79aceca5 9618{
9fddaa0c 9619 DisasContext ctx, *ctxp = &ctx;
c227f099 9620 opc_handler_t **table, *handler;
0fa85d43 9621 target_ulong pc_start;
79aceca5 9622 uint16_t *gen_opc_end;
a1d1bb31 9623 CPUBreakpoint *bp;
79aceca5 9624 int j, lj = -1;
2e70f6ef
PB
9625 int num_insns;
9626 int max_insns;
79aceca5
FB
9627
9628 pc_start = tb->pc;
92414b31 9629 gen_opc_end = tcg_ctx.gen_opc_buf + OPC_MAX_SIZE;
046d6672 9630 ctx.nip = pc_start;
79aceca5 9631 ctx.tb = tb;
e1833e1f 9632 ctx.exception = POWERPC_EXCP_NONE;
3fc6c082 9633 ctx.spr_cb = env->spr_cb;
76db3ba4
AJ
9634 ctx.mem_idx = env->mmu_idx;
9635 ctx.access_type = -1;
9636 ctx.le_mode = env->hflags & (1 << MSR_LE) ? 1 : 0;
d9bce9d9 9637#if defined(TARGET_PPC64)
e42a61f1 9638 ctx.sf_mode = msr_is_64bit(env, env->msr);
697ab892 9639 ctx.has_cfar = !!(env->flags & POWERPC_FLAG_CFAR);
9a64fbe4 9640#endif
3cc62370 9641 ctx.fpu_enabled = msr_fp;
a9d9eb8f 9642 if ((env->flags & POWERPC_FLAG_SPE) && msr_spe)
d26bfc9a
JM
9643 ctx.spe_enabled = msr_spe;
9644 else
9645 ctx.spe_enabled = 0;
a9d9eb8f
JM
9646 if ((env->flags & POWERPC_FLAG_VRE) && msr_vr)
9647 ctx.altivec_enabled = msr_vr;
9648 else
9649 ctx.altivec_enabled = 0;
d26bfc9a 9650 if ((env->flags & POWERPC_FLAG_SE) && msr_se)
8cbcb4fa 9651 ctx.singlestep_enabled = CPU_SINGLE_STEP;
d26bfc9a 9652 else
8cbcb4fa 9653 ctx.singlestep_enabled = 0;
d26bfc9a 9654 if ((env->flags & POWERPC_FLAG_BE) && msr_be)
8cbcb4fa
AJ
9655 ctx.singlestep_enabled |= CPU_BRANCH_STEP;
9656 if (unlikely(env->singlestep_enabled))
9657 ctx.singlestep_enabled |= GDBSTUB_SINGLE_STEP;
3fc6c082 9658#if defined (DO_SINGLE_STEP) && 0
9a64fbe4
FB
9659 /* Single step trace mode */
9660 msr_se = 1;
9661#endif
2e70f6ef
PB
9662 num_insns = 0;
9663 max_insns = tb->cflags & CF_COUNT_MASK;
9664 if (max_insns == 0)
9665 max_insns = CF_COUNT_MASK;
9666
806f352d 9667 gen_tb_start();
9a64fbe4 9668 /* Set env in case of segfault during code fetch */
efd7f486
EV
9669 while (ctx.exception == POWERPC_EXCP_NONE
9670 && tcg_ctx.gen_opc_ptr < gen_opc_end) {
72cf2d4f
BS
9671 if (unlikely(!QTAILQ_EMPTY(&env->breakpoints))) {
9672 QTAILQ_FOREACH(bp, &env->breakpoints, entry) {
a1d1bb31 9673 if (bp->pc == ctx.nip) {
e06fcd75 9674 gen_debug_exception(ctxp);
ea4e754f
FB
9675 break;
9676 }
9677 }
9678 }
76a66253 9679 if (unlikely(search_pc)) {
92414b31 9680 j = tcg_ctx.gen_opc_ptr - tcg_ctx.gen_opc_buf;
79aceca5
FB
9681 if (lj < j) {
9682 lj++;
9683 while (lj < j)
ab1103de 9684 tcg_ctx.gen_opc_instr_start[lj++] = 0;
79aceca5 9685 }
25983cad 9686 tcg_ctx.gen_opc_pc[lj] = ctx.nip;
ab1103de 9687 tcg_ctx.gen_opc_instr_start[lj] = 1;
c9c99c22 9688 tcg_ctx.gen_opc_icount[lj] = num_insns;
79aceca5 9689 }
d12d51d5 9690 LOG_DISAS("----------------\n");
90e189ec 9691 LOG_DISAS("nip=" TARGET_FMT_lx " super=%d ir=%d\n",
d12d51d5 9692 ctx.nip, ctx.mem_idx, (int)msr_ir);
2e70f6ef
PB
9693 if (num_insns + 1 == max_insns && (tb->cflags & CF_LAST_IO))
9694 gen_io_start();
76db3ba4 9695 if (unlikely(ctx.le_mode)) {
2f5a189c 9696 ctx.opcode = bswap32(cpu_ldl_code(env, ctx.nip));
056401ea 9697 } else {
2f5a189c 9698 ctx.opcode = cpu_ldl_code(env, ctx.nip);
111bfab3 9699 }
d12d51d5 9700 LOG_DISAS("translate opcode %08x (%02x %02x %02x) (%s)\n",
9a64fbe4 9701 ctx.opcode, opc1(ctx.opcode), opc2(ctx.opcode),
476b6d16 9702 opc3(ctx.opcode), ctx.le_mode ? "little" : "big");
fdefe51c 9703 if (unlikely(qemu_loglevel_mask(CPU_LOG_TB_OP | CPU_LOG_TB_OP_OPT))) {
731c54f8 9704 tcg_gen_debug_insn_start(ctx.nip);
fdefe51c 9705 }
046d6672 9706 ctx.nip += 4;
3fc6c082 9707 table = env->opcodes;
2e70f6ef 9708 num_insns++;
79aceca5
FB
9709 handler = table[opc1(ctx.opcode)];
9710 if (is_indirect_opcode(handler)) {
9711 table = ind_table(handler);
9712 handler = table[opc2(ctx.opcode)];
9713 if (is_indirect_opcode(handler)) {
9714 table = ind_table(handler);
9715 handler = table[opc3(ctx.opcode)];
9716 }
9717 }
9718 /* Is opcode *REALLY* valid ? */
76a66253 9719 if (unlikely(handler->handler == &gen_invalid)) {
93fcfe39
AL
9720 if (qemu_log_enabled()) {
9721 qemu_log("invalid/unsupported opcode: "
90e189ec
BS
9722 "%02x - %02x - %02x (%08x) " TARGET_FMT_lx " %d\n",
9723 opc1(ctx.opcode), opc2(ctx.opcode),
9724 opc3(ctx.opcode), ctx.opcode, ctx.nip - 4, (int)msr_ir);
4b3686fa 9725 }
76a66253 9726 } else {
70560da7
FC
9727 uint32_t inval;
9728
9729 if (unlikely(handler->type & (PPC_SPE | PPC_SPE_SINGLE | PPC_SPE_DOUBLE) && Rc(ctx.opcode))) {
9730 inval = handler->inval2;
9731 } else {
9732 inval = handler->inval1;
9733 }
9734
9735 if (unlikely((ctx.opcode & inval) != 0)) {
93fcfe39
AL
9736 if (qemu_log_enabled()) {
9737 qemu_log("invalid bits: %08x for opcode: "
90e189ec 9738 "%02x - %02x - %02x (%08x) " TARGET_FMT_lx "\n",
70560da7 9739 ctx.opcode & inval, opc1(ctx.opcode),
90e189ec
BS
9740 opc2(ctx.opcode), opc3(ctx.opcode),
9741 ctx.opcode, ctx.nip - 4);
76a66253 9742 }
e06fcd75 9743 gen_inval_exception(ctxp, POWERPC_EXCP_INVAL_INVAL);
4b3686fa 9744 break;
79aceca5 9745 }
79aceca5 9746 }
4b3686fa 9747 (*(handler->handler))(&ctx);
76a66253
JM
9748#if defined(DO_PPC_STATISTICS)
9749 handler->count++;
9750#endif
9a64fbe4 9751 /* Check trace mode exceptions */
8cbcb4fa
AJ
9752 if (unlikely(ctx.singlestep_enabled & CPU_SINGLE_STEP &&
9753 (ctx.nip <= 0x100 || ctx.nip > 0xF00) &&
9754 ctx.exception != POWERPC_SYSCALL &&
9755 ctx.exception != POWERPC_EXCP_TRAP &&
9756 ctx.exception != POWERPC_EXCP_BRANCH)) {
e06fcd75 9757 gen_exception(ctxp, POWERPC_EXCP_TRACE);
d26bfc9a 9758 } else if (unlikely(((ctx.nip & (TARGET_PAGE_SIZE - 1)) == 0) ||
2e70f6ef 9759 (env->singlestep_enabled) ||
1b530a6d 9760 singlestep ||
2e70f6ef 9761 num_insns >= max_insns)) {
d26bfc9a
JM
9762 /* if we reach a page boundary or are single stepping, stop
9763 * generation
9764 */
8dd4983c 9765 break;
76a66253 9766 }
3fc6c082 9767 }
2e70f6ef
PB
9768 if (tb->cflags & CF_LAST_IO)
9769 gen_io_end();
e1833e1f 9770 if (ctx.exception == POWERPC_EXCP_NONE) {
c1942362 9771 gen_goto_tb(&ctx, 0, ctx.nip);
e1833e1f 9772 } else if (ctx.exception != POWERPC_EXCP_BRANCH) {
8cbcb4fa 9773 if (unlikely(env->singlestep_enabled)) {
e06fcd75 9774 gen_debug_exception(ctxp);
8cbcb4fa 9775 }
76a66253 9776 /* Generate the return instruction */
57fec1fe 9777 tcg_gen_exit_tb(0);
9a64fbe4 9778 }
806f352d 9779 gen_tb_end(tb, num_insns);
efd7f486 9780 *tcg_ctx.gen_opc_ptr = INDEX_op_end;
76a66253 9781 if (unlikely(search_pc)) {
92414b31 9782 j = tcg_ctx.gen_opc_ptr - tcg_ctx.gen_opc_buf;
9a64fbe4
FB
9783 lj++;
9784 while (lj <= j)
ab1103de 9785 tcg_ctx.gen_opc_instr_start[lj++] = 0;
9a64fbe4 9786 } else {
046d6672 9787 tb->size = ctx.nip - pc_start;
2e70f6ef 9788 tb->icount = num_insns;
9a64fbe4 9789 }
d9bce9d9 9790#if defined(DEBUG_DISAS)
8fec2b8c 9791 if (qemu_loglevel_mask(CPU_LOG_TB_IN_ASM)) {
76a66253 9792 int flags;
237c0af0 9793 flags = env->bfd_mach;
76db3ba4 9794 flags |= ctx.le_mode << 16;
93fcfe39 9795 qemu_log("IN: %s\n", lookup_symbol(pc_start));
f4359b9f 9796 log_target_disas(env, pc_start, ctx.nip - pc_start, flags);
93fcfe39 9797 qemu_log("\n");
9fddaa0c 9798 }
79aceca5 9799#endif
79aceca5
FB
9800}
9801
1328c2bf 9802void gen_intermediate_code (CPUPPCState *env, struct TranslationBlock *tb)
79aceca5 9803{
2cfc5f17 9804 gen_intermediate_code_internal(env, tb, 0);
79aceca5
FB
9805}
9806
1328c2bf 9807void gen_intermediate_code_pc (CPUPPCState *env, struct TranslationBlock *tb)
79aceca5 9808{
2cfc5f17 9809 gen_intermediate_code_internal(env, tb, 1);
79aceca5 9810}
d2856f1a 9811
1328c2bf 9812void restore_state_to_opc(CPUPPCState *env, TranslationBlock *tb, int pc_pos)
d2856f1a 9813{
25983cad 9814 env->nip = tcg_ctx.gen_opc_pc[pc_pos];
d2856f1a 9815}