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