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