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