]> git.proxmox.com Git - qemu.git/blame - target-ppc/translate.c
ppc: Split off timebase helpers
[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"
79aceca5 22#include "disas.h"
57fec1fe 23#include "tcg-op.h"
0cfe11ea 24#include "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;
a7812ae4 71static TCGv_i32 cpu_fpscr;
a7859e89 72static TCGv_i32 cpu_access_type;
f78fb44e 73
2e70f6ef
PB
74#include "gen-icount.h"
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
a7812ae4 166 cpu_fpscr = tcg_global_mem_new_i32(TCG_AREG0,
1328c2bf 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{
7c58044c
JM
2305 int bfa;
2306
76a66253 2307 if (unlikely(!ctx->fpu_enabled)) {
e06fcd75 2308 gen_exception(ctx, POWERPC_EXCP_FPU);
3cc62370
FB
2309 return;
2310 }
7c58044c 2311 bfa = 4 * (7 - crfS(ctx->opcode));
e1571908
AJ
2312 tcg_gen_shri_i32(cpu_crf[crfD(ctx->opcode)], cpu_fpscr, bfa);
2313 tcg_gen_andi_i32(cpu_crf[crfD(ctx->opcode)], cpu_crf[crfD(ctx->opcode)], 0xf);
af12906f 2314 tcg_gen_andi_i32(cpu_fpscr, cpu_fpscr, ~(0xF << bfa));
79aceca5
FB
2315}
2316
2317/* mffs */
99e300ef 2318static void gen_mffs(DisasContext *ctx)
79aceca5 2319{
76a66253 2320 if (unlikely(!ctx->fpu_enabled)) {
e06fcd75 2321 gen_exception(ctx, POWERPC_EXCP_FPU);
3cc62370
FB
2322 return;
2323 }
7c58044c 2324 gen_reset_fpstatus();
af12906f
AJ
2325 tcg_gen_extu_i32_i64(cpu_fpr[rD(ctx->opcode)], cpu_fpscr);
2326 gen_compute_fprf(cpu_fpr[rD(ctx->opcode)], 0, Rc(ctx->opcode) != 0);
79aceca5
FB
2327}
2328
2329/* mtfsb0 */
99e300ef 2330static void gen_mtfsb0(DisasContext *ctx)
79aceca5 2331{
fb0eaffc 2332 uint8_t crb;
3b46e624 2333
76a66253 2334 if (unlikely(!ctx->fpu_enabled)) {
e06fcd75 2335 gen_exception(ctx, POWERPC_EXCP_FPU);
3cc62370
FB
2336 return;
2337 }
6e35d524 2338 crb = 31 - crbD(ctx->opcode);
7c58044c 2339 gen_reset_fpstatus();
6e35d524 2340 if (likely(crb != FPSCR_FEX && crb != FPSCR_VX)) {
eb44b959
AJ
2341 TCGv_i32 t0;
2342 /* NIP cannot be restored if the memory exception comes from an helper */
2343 gen_update_nip(ctx, ctx->nip - 4);
2344 t0 = tcg_const_i32(crb);
8e703949 2345 gen_helper_fpscr_clrbit(cpu_env, t0);
6e35d524
AJ
2346 tcg_temp_free_i32(t0);
2347 }
7c58044c 2348 if (unlikely(Rc(ctx->opcode) != 0)) {
e1571908 2349 tcg_gen_shri_i32(cpu_crf[1], cpu_fpscr, FPSCR_OX);
7c58044c 2350 }
79aceca5
FB
2351}
2352
2353/* mtfsb1 */
99e300ef 2354static void gen_mtfsb1(DisasContext *ctx)
79aceca5 2355{
fb0eaffc 2356 uint8_t crb;
3b46e624 2357
76a66253 2358 if (unlikely(!ctx->fpu_enabled)) {
e06fcd75 2359 gen_exception(ctx, POWERPC_EXCP_FPU);
3cc62370
FB
2360 return;
2361 }
6e35d524 2362 crb = 31 - crbD(ctx->opcode);
7c58044c
JM
2363 gen_reset_fpstatus();
2364 /* XXX: we pretend we can only do IEEE floating-point computations */
af12906f 2365 if (likely(crb != FPSCR_FEX && crb != FPSCR_VX && crb != FPSCR_NI)) {
eb44b959
AJ
2366 TCGv_i32 t0;
2367 /* NIP cannot be restored if the memory exception comes from an helper */
2368 gen_update_nip(ctx, ctx->nip - 4);
2369 t0 = tcg_const_i32(crb);
8e703949 2370 gen_helper_fpscr_setbit(cpu_env, t0);
0f2f39c2 2371 tcg_temp_free_i32(t0);
af12906f 2372 }
7c58044c 2373 if (unlikely(Rc(ctx->opcode) != 0)) {
e1571908 2374 tcg_gen_shri_i32(cpu_crf[1], cpu_fpscr, FPSCR_OX);
7c58044c
JM
2375 }
2376 /* We can raise a differed exception */
8e703949 2377 gen_helper_float_check_status(cpu_env);
79aceca5
FB
2378}
2379
2380/* mtfsf */
99e300ef 2381static void gen_mtfsf(DisasContext *ctx)
79aceca5 2382{
0f2f39c2 2383 TCGv_i32 t0;
4911012d 2384 int L = ctx->opcode & 0x02000000;
af12906f 2385
76a66253 2386 if (unlikely(!ctx->fpu_enabled)) {
e06fcd75 2387 gen_exception(ctx, POWERPC_EXCP_FPU);
3cc62370
FB
2388 return;
2389 }
eb44b959
AJ
2390 /* NIP cannot be restored if the memory exception comes from an helper */
2391 gen_update_nip(ctx, ctx->nip - 4);
7c58044c 2392 gen_reset_fpstatus();
4911012d
BS
2393 if (L)
2394 t0 = tcg_const_i32(0xff);
2395 else
2396 t0 = tcg_const_i32(FM(ctx->opcode));
8e703949 2397 gen_helper_store_fpscr(cpu_env, cpu_fpr[rB(ctx->opcode)], t0);
0f2f39c2 2398 tcg_temp_free_i32(t0);
7c58044c 2399 if (unlikely(Rc(ctx->opcode) != 0)) {
e1571908 2400 tcg_gen_shri_i32(cpu_crf[1], cpu_fpscr, FPSCR_OX);
7c58044c
JM
2401 }
2402 /* We can raise a differed exception */
8e703949 2403 gen_helper_float_check_status(cpu_env);
79aceca5
FB
2404}
2405
2406/* mtfsfi */
99e300ef 2407static void gen_mtfsfi(DisasContext *ctx)
79aceca5 2408{
7c58044c 2409 int bf, sh;
0f2f39c2
AJ
2410 TCGv_i64 t0;
2411 TCGv_i32 t1;
7c58044c 2412
76a66253 2413 if (unlikely(!ctx->fpu_enabled)) {
e06fcd75 2414 gen_exception(ctx, POWERPC_EXCP_FPU);
3cc62370
FB
2415 return;
2416 }
7c58044c
JM
2417 bf = crbD(ctx->opcode) >> 2;
2418 sh = 7 - bf;
eb44b959
AJ
2419 /* NIP cannot be restored if the memory exception comes from an helper */
2420 gen_update_nip(ctx, ctx->nip - 4);
7c58044c 2421 gen_reset_fpstatus();
0f2f39c2 2422 t0 = tcg_const_i64(FPIMM(ctx->opcode) << (4 * sh));
af12906f 2423 t1 = tcg_const_i32(1 << sh);
8e703949 2424 gen_helper_store_fpscr(cpu_env, t0, t1);
0f2f39c2
AJ
2425 tcg_temp_free_i64(t0);
2426 tcg_temp_free_i32(t1);
7c58044c 2427 if (unlikely(Rc(ctx->opcode) != 0)) {
e1571908 2428 tcg_gen_shri_i32(cpu_crf[1], cpu_fpscr, FPSCR_OX);
7c58044c
JM
2429 }
2430 /* We can raise a differed exception */
8e703949 2431 gen_helper_float_check_status(cpu_env);
79aceca5
FB
2432}
2433
76a66253
JM
2434/*** Addressing modes ***/
2435/* Register indirect with immediate index : EA = (rA|0) + SIMM */
636aa200
BS
2436static inline void gen_addr_imm_index(DisasContext *ctx, TCGv EA,
2437 target_long maskl)
76a66253
JM
2438{
2439 target_long simm = SIMM(ctx->opcode);
2440
be147d08 2441 simm &= ~maskl;
76db3ba4
AJ
2442 if (rA(ctx->opcode) == 0) {
2443#if defined(TARGET_PPC64)
2444 if (!ctx->sf_mode) {
2445 tcg_gen_movi_tl(EA, (uint32_t)simm);
2446 } else
2447#endif
e2be8d8d 2448 tcg_gen_movi_tl(EA, simm);
76db3ba4 2449 } else if (likely(simm != 0)) {
e2be8d8d 2450 tcg_gen_addi_tl(EA, cpu_gpr[rA(ctx->opcode)], simm);
76db3ba4
AJ
2451#if defined(TARGET_PPC64)
2452 if (!ctx->sf_mode) {
2453 tcg_gen_ext32u_tl(EA, EA);
2454 }
2455#endif
2456 } else {
2457#if defined(TARGET_PPC64)
2458 if (!ctx->sf_mode) {
2459 tcg_gen_ext32u_tl(EA, cpu_gpr[rA(ctx->opcode)]);
2460 } else
2461#endif
e2be8d8d 2462 tcg_gen_mov_tl(EA, cpu_gpr[rA(ctx->opcode)]);
76db3ba4 2463 }
76a66253
JM
2464}
2465
636aa200 2466static inline void gen_addr_reg_index(DisasContext *ctx, TCGv EA)
76a66253 2467{
76db3ba4
AJ
2468 if (rA(ctx->opcode) == 0) {
2469#if defined(TARGET_PPC64)
2470 if (!ctx->sf_mode) {
2471 tcg_gen_ext32u_tl(EA, cpu_gpr[rB(ctx->opcode)]);
2472 } else
2473#endif
e2be8d8d 2474 tcg_gen_mov_tl(EA, cpu_gpr[rB(ctx->opcode)]);
76db3ba4 2475 } else {
e2be8d8d 2476 tcg_gen_add_tl(EA, cpu_gpr[rA(ctx->opcode)], cpu_gpr[rB(ctx->opcode)]);
76db3ba4
AJ
2477#if defined(TARGET_PPC64)
2478 if (!ctx->sf_mode) {
2479 tcg_gen_ext32u_tl(EA, EA);
2480 }
2481#endif
2482 }
76a66253
JM
2483}
2484
636aa200 2485static inline void gen_addr_register(DisasContext *ctx, TCGv EA)
76a66253 2486{
76db3ba4 2487 if (rA(ctx->opcode) == 0) {
e2be8d8d 2488 tcg_gen_movi_tl(EA, 0);
76db3ba4
AJ
2489 } else {
2490#if defined(TARGET_PPC64)
2491 if (!ctx->sf_mode) {
2492 tcg_gen_ext32u_tl(EA, cpu_gpr[rA(ctx->opcode)]);
2493 } else
2494#endif
2495 tcg_gen_mov_tl(EA, cpu_gpr[rA(ctx->opcode)]);
2496 }
2497}
2498
636aa200
BS
2499static inline void gen_addr_add(DisasContext *ctx, TCGv ret, TCGv arg1,
2500 target_long val)
76db3ba4
AJ
2501{
2502 tcg_gen_addi_tl(ret, arg1, val);
2503#if defined(TARGET_PPC64)
2504 if (!ctx->sf_mode) {
2505 tcg_gen_ext32u_tl(ret, ret);
2506 }
2507#endif
76a66253
JM
2508}
2509
636aa200 2510static inline void gen_check_align(DisasContext *ctx, TCGv EA, int mask)
cf360a32
AJ
2511{
2512 int l1 = gen_new_label();
2513 TCGv t0 = tcg_temp_new();
2514 TCGv_i32 t1, t2;
2515 /* NIP cannot be restored if the memory exception comes from an helper */
2516 gen_update_nip(ctx, ctx->nip - 4);
2517 tcg_gen_andi_tl(t0, EA, mask);
2518 tcg_gen_brcondi_tl(TCG_COND_EQ, t0, 0, l1);
2519 t1 = tcg_const_i32(POWERPC_EXCP_ALIGN);
2520 t2 = tcg_const_i32(0);
e5f17ac6 2521 gen_helper_raise_exception_err(cpu_env, t1, t2);
cf360a32
AJ
2522 tcg_temp_free_i32(t1);
2523 tcg_temp_free_i32(t2);
2524 gen_set_label(l1);
2525 tcg_temp_free(t0);
2526}
2527
7863667f 2528/*** Integer load ***/
636aa200 2529static inline void gen_qemu_ld8u(DisasContext *ctx, TCGv arg1, TCGv arg2)
76db3ba4
AJ
2530{
2531 tcg_gen_qemu_ld8u(arg1, arg2, ctx->mem_idx);
2532}
2533
636aa200 2534static inline void gen_qemu_ld8s(DisasContext *ctx, TCGv arg1, TCGv arg2)
76db3ba4
AJ
2535{
2536 tcg_gen_qemu_ld8s(arg1, arg2, ctx->mem_idx);
2537}
2538
636aa200 2539static inline void gen_qemu_ld16u(DisasContext *ctx, TCGv arg1, TCGv arg2)
76db3ba4
AJ
2540{
2541 tcg_gen_qemu_ld16u(arg1, arg2, ctx->mem_idx);
2542 if (unlikely(ctx->le_mode)) {
fa3966a3 2543 tcg_gen_bswap16_tl(arg1, arg1);
76db3ba4 2544 }
b61f2753
AJ
2545}
2546
636aa200 2547static inline void gen_qemu_ld16s(DisasContext *ctx, TCGv arg1, TCGv arg2)
b61f2753 2548{
76db3ba4 2549 if (unlikely(ctx->le_mode)) {
76db3ba4 2550 tcg_gen_qemu_ld16u(arg1, arg2, ctx->mem_idx);
fa3966a3 2551 tcg_gen_bswap16_tl(arg1, arg1);
76db3ba4 2552 tcg_gen_ext16s_tl(arg1, arg1);
76db3ba4
AJ
2553 } else {
2554 tcg_gen_qemu_ld16s(arg1, arg2, ctx->mem_idx);
2555 }
b61f2753
AJ
2556}
2557
636aa200 2558static inline void gen_qemu_ld32u(DisasContext *ctx, TCGv arg1, TCGv arg2)
b61f2753 2559{
76db3ba4
AJ
2560 tcg_gen_qemu_ld32u(arg1, arg2, ctx->mem_idx);
2561 if (unlikely(ctx->le_mode)) {
fa3966a3 2562 tcg_gen_bswap32_tl(arg1, arg1);
76db3ba4 2563 }
b61f2753
AJ
2564}
2565
76db3ba4 2566#if defined(TARGET_PPC64)
636aa200 2567static inline void gen_qemu_ld32s(DisasContext *ctx, TCGv arg1, TCGv arg2)
b61f2753 2568{
a457e7ee 2569 if (unlikely(ctx->le_mode)) {
76db3ba4 2570 tcg_gen_qemu_ld32u(arg1, arg2, ctx->mem_idx);
fa3966a3
AJ
2571 tcg_gen_bswap32_tl(arg1, arg1);
2572 tcg_gen_ext32s_tl(arg1, arg1);
b61f2753 2573 } else
76db3ba4 2574 tcg_gen_qemu_ld32s(arg1, arg2, ctx->mem_idx);
b61f2753 2575}
76db3ba4 2576#endif
b61f2753 2577
636aa200 2578static inline void gen_qemu_ld64(DisasContext *ctx, TCGv_i64 arg1, TCGv arg2)
b61f2753 2579{
76db3ba4
AJ
2580 tcg_gen_qemu_ld64(arg1, arg2, ctx->mem_idx);
2581 if (unlikely(ctx->le_mode)) {
66896cb8 2582 tcg_gen_bswap64_i64(arg1, arg1);
76db3ba4 2583 }
b61f2753
AJ
2584}
2585
636aa200 2586static inline void gen_qemu_st8(DisasContext *ctx, TCGv arg1, TCGv arg2)
b61f2753 2587{
76db3ba4 2588 tcg_gen_qemu_st8(arg1, arg2, ctx->mem_idx);
b61f2753
AJ
2589}
2590
636aa200 2591static inline void gen_qemu_st16(DisasContext *ctx, TCGv arg1, TCGv arg2)
b61f2753 2592{
76db3ba4 2593 if (unlikely(ctx->le_mode)) {
76db3ba4
AJ
2594 TCGv t0 = tcg_temp_new();
2595 tcg_gen_ext16u_tl(t0, arg1);
fa3966a3 2596 tcg_gen_bswap16_tl(t0, t0);
76db3ba4
AJ
2597 tcg_gen_qemu_st16(t0, arg2, ctx->mem_idx);
2598 tcg_temp_free(t0);
76db3ba4
AJ
2599 } else {
2600 tcg_gen_qemu_st16(arg1, arg2, ctx->mem_idx);
2601 }
b61f2753
AJ
2602}
2603
636aa200 2604static inline void gen_qemu_st32(DisasContext *ctx, TCGv arg1, TCGv arg2)
b61f2753 2605{
76db3ba4 2606 if (unlikely(ctx->le_mode)) {
fa3966a3
AJ
2607 TCGv t0 = tcg_temp_new();
2608 tcg_gen_ext32u_tl(t0, arg1);
2609 tcg_gen_bswap32_tl(t0, t0);
76db3ba4
AJ
2610 tcg_gen_qemu_st32(t0, arg2, ctx->mem_idx);
2611 tcg_temp_free(t0);
76db3ba4
AJ
2612 } else {
2613 tcg_gen_qemu_st32(arg1, arg2, ctx->mem_idx);
2614 }
b61f2753
AJ
2615}
2616
636aa200 2617static inline void gen_qemu_st64(DisasContext *ctx, TCGv_i64 arg1, TCGv arg2)
b61f2753 2618{
76db3ba4 2619 if (unlikely(ctx->le_mode)) {
a7812ae4 2620 TCGv_i64 t0 = tcg_temp_new_i64();
66896cb8 2621 tcg_gen_bswap64_i64(t0, arg1);
76db3ba4 2622 tcg_gen_qemu_st64(t0, arg2, ctx->mem_idx);
a7812ae4 2623 tcg_temp_free_i64(t0);
b61f2753 2624 } else
76db3ba4 2625 tcg_gen_qemu_st64(arg1, arg2, ctx->mem_idx);
b61f2753
AJ
2626}
2627
0c8aacd4 2628#define GEN_LD(name, ldop, opc, type) \
99e300ef 2629static void glue(gen_, name)(DisasContext *ctx) \
79aceca5 2630{ \
76db3ba4
AJ
2631 TCGv EA; \
2632 gen_set_access_type(ctx, ACCESS_INT); \
2633 EA = tcg_temp_new(); \
2634 gen_addr_imm_index(ctx, EA, 0); \
2635 gen_qemu_##ldop(ctx, cpu_gpr[rD(ctx->opcode)], EA); \
b61f2753 2636 tcg_temp_free(EA); \
79aceca5
FB
2637}
2638
0c8aacd4 2639#define GEN_LDU(name, ldop, opc, type) \
99e300ef 2640static void glue(gen_, name##u)(DisasContext *ctx) \
79aceca5 2641{ \
b61f2753 2642 TCGv EA; \
76a66253
JM
2643 if (unlikely(rA(ctx->opcode) == 0 || \
2644 rA(ctx->opcode) == rD(ctx->opcode))) { \
e06fcd75 2645 gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL); \
9fddaa0c 2646 return; \
9a64fbe4 2647 } \
76db3ba4 2648 gen_set_access_type(ctx, ACCESS_INT); \
0c8aacd4 2649 EA = tcg_temp_new(); \
9d53c753 2650 if (type == PPC_64B) \
76db3ba4 2651 gen_addr_imm_index(ctx, EA, 0x03); \
9d53c753 2652 else \
76db3ba4
AJ
2653 gen_addr_imm_index(ctx, EA, 0); \
2654 gen_qemu_##ldop(ctx, cpu_gpr[rD(ctx->opcode)], EA); \
b61f2753
AJ
2655 tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], EA); \
2656 tcg_temp_free(EA); \
79aceca5
FB
2657}
2658
0c8aacd4 2659#define GEN_LDUX(name, ldop, opc2, opc3, type) \
99e300ef 2660static void glue(gen_, name##ux)(DisasContext *ctx) \
79aceca5 2661{ \
b61f2753 2662 TCGv EA; \
76a66253
JM
2663 if (unlikely(rA(ctx->opcode) == 0 || \
2664 rA(ctx->opcode) == rD(ctx->opcode))) { \
e06fcd75 2665 gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL); \
9fddaa0c 2666 return; \
9a64fbe4 2667 } \
76db3ba4 2668 gen_set_access_type(ctx, ACCESS_INT); \
0c8aacd4 2669 EA = tcg_temp_new(); \
76db3ba4
AJ
2670 gen_addr_reg_index(ctx, EA); \
2671 gen_qemu_##ldop(ctx, cpu_gpr[rD(ctx->opcode)], EA); \
b61f2753
AJ
2672 tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], EA); \
2673 tcg_temp_free(EA); \
79aceca5
FB
2674}
2675
cd6e9320 2676#define GEN_LDX_E(name, ldop, opc2, opc3, type, type2) \
99e300ef 2677static void glue(gen_, name##x)(DisasContext *ctx) \
79aceca5 2678{ \
76db3ba4
AJ
2679 TCGv EA; \
2680 gen_set_access_type(ctx, ACCESS_INT); \
2681 EA = tcg_temp_new(); \
2682 gen_addr_reg_index(ctx, EA); \
2683 gen_qemu_##ldop(ctx, cpu_gpr[rD(ctx->opcode)], EA); \
b61f2753 2684 tcg_temp_free(EA); \
79aceca5 2685}
cd6e9320
TH
2686#define GEN_LDX(name, ldop, opc2, opc3, type) \
2687 GEN_LDX_E(name, ldop, opc2, opc3, type, PPC_NONE)
79aceca5 2688
0c8aacd4
AJ
2689#define GEN_LDS(name, ldop, op, type) \
2690GEN_LD(name, ldop, op | 0x20, type); \
2691GEN_LDU(name, ldop, op | 0x21, type); \
2692GEN_LDUX(name, ldop, 0x17, op | 0x01, type); \
2693GEN_LDX(name, ldop, 0x17, op | 0x00, type)
79aceca5
FB
2694
2695/* lbz lbzu lbzux lbzx */
0c8aacd4 2696GEN_LDS(lbz, ld8u, 0x02, PPC_INTEGER);
79aceca5 2697/* lha lhau lhaux lhax */
0c8aacd4 2698GEN_LDS(lha, ld16s, 0x0A, PPC_INTEGER);
79aceca5 2699/* lhz lhzu lhzux lhzx */
0c8aacd4 2700GEN_LDS(lhz, ld16u, 0x08, PPC_INTEGER);
79aceca5 2701/* lwz lwzu lwzux lwzx */
0c8aacd4 2702GEN_LDS(lwz, ld32u, 0x00, PPC_INTEGER);
d9bce9d9 2703#if defined(TARGET_PPC64)
d9bce9d9 2704/* lwaux */
0c8aacd4 2705GEN_LDUX(lwa, ld32s, 0x15, 0x0B, PPC_64B);
d9bce9d9 2706/* lwax */
0c8aacd4 2707GEN_LDX(lwa, ld32s, 0x15, 0x0A, PPC_64B);
d9bce9d9 2708/* ldux */
0c8aacd4 2709GEN_LDUX(ld, ld64, 0x15, 0x01, PPC_64B);
d9bce9d9 2710/* ldx */
0c8aacd4 2711GEN_LDX(ld, ld64, 0x15, 0x00, PPC_64B);
99e300ef
BS
2712
2713static void gen_ld(DisasContext *ctx)
d9bce9d9 2714{
b61f2753 2715 TCGv EA;
d9bce9d9
JM
2716 if (Rc(ctx->opcode)) {
2717 if (unlikely(rA(ctx->opcode) == 0 ||
2718 rA(ctx->opcode) == rD(ctx->opcode))) {
e06fcd75 2719 gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL);
d9bce9d9
JM
2720 return;
2721 }
2722 }
76db3ba4 2723 gen_set_access_type(ctx, ACCESS_INT);
a7812ae4 2724 EA = tcg_temp_new();
76db3ba4 2725 gen_addr_imm_index(ctx, EA, 0x03);
d9bce9d9
JM
2726 if (ctx->opcode & 0x02) {
2727 /* lwa (lwau is undefined) */
76db3ba4 2728 gen_qemu_ld32s(ctx, cpu_gpr[rD(ctx->opcode)], EA);
d9bce9d9
JM
2729 } else {
2730 /* ld - ldu */
76db3ba4 2731 gen_qemu_ld64(ctx, cpu_gpr[rD(ctx->opcode)], EA);
d9bce9d9 2732 }
d9bce9d9 2733 if (Rc(ctx->opcode))
b61f2753
AJ
2734 tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], EA);
2735 tcg_temp_free(EA);
d9bce9d9 2736}
99e300ef 2737
54623277 2738/* lq */
99e300ef 2739static void gen_lq(DisasContext *ctx)
be147d08
JM
2740{
2741#if defined(CONFIG_USER_ONLY)
e06fcd75 2742 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
be147d08
JM
2743#else
2744 int ra, rd;
b61f2753 2745 TCGv EA;
be147d08
JM
2746
2747 /* Restore CPU state */
76db3ba4 2748 if (unlikely(ctx->mem_idx == 0)) {
e06fcd75 2749 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
be147d08
JM
2750 return;
2751 }
2752 ra = rA(ctx->opcode);
2753 rd = rD(ctx->opcode);
2754 if (unlikely((rd & 1) || rd == ra)) {
e06fcd75 2755 gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL);
be147d08
JM
2756 return;
2757 }
76db3ba4 2758 if (unlikely(ctx->le_mode)) {
be147d08 2759 /* Little-endian mode is not handled */
e06fcd75 2760 gen_exception_err(ctx, POWERPC_EXCP_ALIGN, POWERPC_EXCP_ALIGN_LE);
be147d08
JM
2761 return;
2762 }
76db3ba4 2763 gen_set_access_type(ctx, ACCESS_INT);
a7812ae4 2764 EA = tcg_temp_new();
76db3ba4
AJ
2765 gen_addr_imm_index(ctx, EA, 0x0F);
2766 gen_qemu_ld64(ctx, cpu_gpr[rd], EA);
2767 gen_addr_add(ctx, EA, EA, 8);
2768 gen_qemu_ld64(ctx, cpu_gpr[rd+1], EA);
b61f2753 2769 tcg_temp_free(EA);
be147d08
JM
2770#endif
2771}
d9bce9d9 2772#endif
79aceca5
FB
2773
2774/*** Integer store ***/
0c8aacd4 2775#define GEN_ST(name, stop, opc, type) \
99e300ef 2776static void glue(gen_, name)(DisasContext *ctx) \
79aceca5 2777{ \
76db3ba4
AJ
2778 TCGv EA; \
2779 gen_set_access_type(ctx, ACCESS_INT); \
2780 EA = tcg_temp_new(); \
2781 gen_addr_imm_index(ctx, EA, 0); \
2782 gen_qemu_##stop(ctx, cpu_gpr[rS(ctx->opcode)], EA); \
b61f2753 2783 tcg_temp_free(EA); \
79aceca5
FB
2784}
2785
0c8aacd4 2786#define GEN_STU(name, stop, opc, type) \
99e300ef 2787static void glue(gen_, stop##u)(DisasContext *ctx) \
79aceca5 2788{ \
b61f2753 2789 TCGv EA; \
76a66253 2790 if (unlikely(rA(ctx->opcode) == 0)) { \
e06fcd75 2791 gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL); \
9fddaa0c 2792 return; \
9a64fbe4 2793 } \
76db3ba4 2794 gen_set_access_type(ctx, ACCESS_INT); \
0c8aacd4 2795 EA = tcg_temp_new(); \
9d53c753 2796 if (type == PPC_64B) \
76db3ba4 2797 gen_addr_imm_index(ctx, EA, 0x03); \
9d53c753 2798 else \
76db3ba4
AJ
2799 gen_addr_imm_index(ctx, EA, 0); \
2800 gen_qemu_##stop(ctx, cpu_gpr[rS(ctx->opcode)], EA); \
b61f2753
AJ
2801 tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], EA); \
2802 tcg_temp_free(EA); \
79aceca5
FB
2803}
2804
0c8aacd4 2805#define GEN_STUX(name, stop, opc2, opc3, type) \
99e300ef 2806static void glue(gen_, name##ux)(DisasContext *ctx) \
79aceca5 2807{ \
b61f2753 2808 TCGv EA; \
76a66253 2809 if (unlikely(rA(ctx->opcode) == 0)) { \
e06fcd75 2810 gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL); \
9fddaa0c 2811 return; \
9a64fbe4 2812 } \
76db3ba4 2813 gen_set_access_type(ctx, ACCESS_INT); \
0c8aacd4 2814 EA = tcg_temp_new(); \
76db3ba4
AJ
2815 gen_addr_reg_index(ctx, EA); \
2816 gen_qemu_##stop(ctx, cpu_gpr[rS(ctx->opcode)], EA); \
b61f2753
AJ
2817 tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], EA); \
2818 tcg_temp_free(EA); \
79aceca5
FB
2819}
2820
cd6e9320
TH
2821#define GEN_STX_E(name, stop, opc2, opc3, type, type2) \
2822static void glue(gen_, name##x)(DisasContext *ctx) \
79aceca5 2823{ \
76db3ba4
AJ
2824 TCGv EA; \
2825 gen_set_access_type(ctx, ACCESS_INT); \
2826 EA = tcg_temp_new(); \
2827 gen_addr_reg_index(ctx, EA); \
2828 gen_qemu_##stop(ctx, cpu_gpr[rS(ctx->opcode)], EA); \
b61f2753 2829 tcg_temp_free(EA); \
79aceca5 2830}
cd6e9320
TH
2831#define GEN_STX(name, stop, opc2, opc3, type) \
2832 GEN_STX_E(name, stop, opc2, opc3, type, PPC_NONE)
79aceca5 2833
0c8aacd4
AJ
2834#define GEN_STS(name, stop, op, type) \
2835GEN_ST(name, stop, op | 0x20, type); \
2836GEN_STU(name, stop, op | 0x21, type); \
2837GEN_STUX(name, stop, 0x17, op | 0x01, type); \
2838GEN_STX(name, stop, 0x17, op | 0x00, type)
79aceca5
FB
2839
2840/* stb stbu stbux stbx */
0c8aacd4 2841GEN_STS(stb, st8, 0x06, PPC_INTEGER);
79aceca5 2842/* sth sthu sthux sthx */
0c8aacd4 2843GEN_STS(sth, st16, 0x0C, PPC_INTEGER);
79aceca5 2844/* stw stwu stwux stwx */
0c8aacd4 2845GEN_STS(stw, st32, 0x04, PPC_INTEGER);
d9bce9d9 2846#if defined(TARGET_PPC64)
0c8aacd4
AJ
2847GEN_STUX(std, st64, 0x15, 0x05, PPC_64B);
2848GEN_STX(std, st64, 0x15, 0x04, PPC_64B);
99e300ef
BS
2849
2850static void gen_std(DisasContext *ctx)
d9bce9d9 2851{
be147d08 2852 int rs;
b61f2753 2853 TCGv EA;
be147d08
JM
2854
2855 rs = rS(ctx->opcode);
2856 if ((ctx->opcode & 0x3) == 0x2) {
2857#if defined(CONFIG_USER_ONLY)
e06fcd75 2858 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
be147d08
JM
2859#else
2860 /* stq */
76db3ba4 2861 if (unlikely(ctx->mem_idx == 0)) {
e06fcd75 2862 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
be147d08
JM
2863 return;
2864 }
2865 if (unlikely(rs & 1)) {
e06fcd75 2866 gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL);
d9bce9d9
JM
2867 return;
2868 }
76db3ba4 2869 if (unlikely(ctx->le_mode)) {
be147d08 2870 /* Little-endian mode is not handled */
e06fcd75 2871 gen_exception_err(ctx, POWERPC_EXCP_ALIGN, POWERPC_EXCP_ALIGN_LE);
be147d08
JM
2872 return;
2873 }
76db3ba4 2874 gen_set_access_type(ctx, ACCESS_INT);
a7812ae4 2875 EA = tcg_temp_new();
76db3ba4
AJ
2876 gen_addr_imm_index(ctx, EA, 0x03);
2877 gen_qemu_st64(ctx, cpu_gpr[rs], EA);
2878 gen_addr_add(ctx, EA, EA, 8);
2879 gen_qemu_st64(ctx, cpu_gpr[rs+1], EA);
b61f2753 2880 tcg_temp_free(EA);
be147d08
JM
2881#endif
2882 } else {
2883 /* std / stdu */
2884 if (Rc(ctx->opcode)) {
2885 if (unlikely(rA(ctx->opcode) == 0)) {
e06fcd75 2886 gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL);
be147d08
JM
2887 return;
2888 }
2889 }
76db3ba4 2890 gen_set_access_type(ctx, ACCESS_INT);
a7812ae4 2891 EA = tcg_temp_new();
76db3ba4
AJ
2892 gen_addr_imm_index(ctx, EA, 0x03);
2893 gen_qemu_st64(ctx, cpu_gpr[rs], EA);
be147d08 2894 if (Rc(ctx->opcode))
b61f2753
AJ
2895 tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], EA);
2896 tcg_temp_free(EA);
d9bce9d9 2897 }
d9bce9d9
JM
2898}
2899#endif
79aceca5
FB
2900/*** Integer load and store with byte reverse ***/
2901/* lhbrx */
86178a57 2902static inline void gen_qemu_ld16ur(DisasContext *ctx, TCGv arg1, TCGv arg2)
b61f2753 2903{
76db3ba4
AJ
2904 tcg_gen_qemu_ld16u(arg1, arg2, ctx->mem_idx);
2905 if (likely(!ctx->le_mode)) {
fa3966a3 2906 tcg_gen_bswap16_tl(arg1, arg1);
76db3ba4 2907 }
b61f2753 2908}
0c8aacd4 2909GEN_LDX(lhbr, ld16ur, 0x16, 0x18, PPC_INTEGER);
b61f2753 2910
79aceca5 2911/* lwbrx */
86178a57 2912static inline void gen_qemu_ld32ur(DisasContext *ctx, TCGv arg1, TCGv arg2)
b61f2753 2913{
76db3ba4
AJ
2914 tcg_gen_qemu_ld32u(arg1, arg2, ctx->mem_idx);
2915 if (likely(!ctx->le_mode)) {
fa3966a3 2916 tcg_gen_bswap32_tl(arg1, arg1);
76db3ba4 2917 }
b61f2753 2918}
0c8aacd4 2919GEN_LDX(lwbr, ld32ur, 0x16, 0x10, PPC_INTEGER);
b61f2753 2920
cd6e9320
TH
2921#if defined(TARGET_PPC64)
2922/* ldbrx */
2923static inline void gen_qemu_ld64ur(DisasContext *ctx, TCGv arg1, TCGv arg2)
2924{
2925 tcg_gen_qemu_ld64(arg1, arg2, ctx->mem_idx);
2926 if (likely(!ctx->le_mode)) {
2927 tcg_gen_bswap64_tl(arg1, arg1);
2928 }
2929}
2930GEN_LDX_E(ldbr, ld64ur, 0x14, 0x10, PPC_NONE, PPC2_DBRX);
2931#endif /* TARGET_PPC64 */
2932
79aceca5 2933/* sthbrx */
86178a57 2934static inline void gen_qemu_st16r(DisasContext *ctx, TCGv arg1, TCGv arg2)
b61f2753 2935{
76db3ba4 2936 if (likely(!ctx->le_mode)) {
76db3ba4
AJ
2937 TCGv t0 = tcg_temp_new();
2938 tcg_gen_ext16u_tl(t0, arg1);
fa3966a3 2939 tcg_gen_bswap16_tl(t0, t0);
76db3ba4
AJ
2940 tcg_gen_qemu_st16(t0, arg2, ctx->mem_idx);
2941 tcg_temp_free(t0);
76db3ba4
AJ
2942 } else {
2943 tcg_gen_qemu_st16(arg1, arg2, ctx->mem_idx);
2944 }
b61f2753 2945}
0c8aacd4 2946GEN_STX(sthbr, st16r, 0x16, 0x1C, PPC_INTEGER);
b61f2753 2947
79aceca5 2948/* stwbrx */
86178a57 2949static inline void gen_qemu_st32r(DisasContext *ctx, TCGv arg1, TCGv arg2)
b61f2753 2950{
76db3ba4 2951 if (likely(!ctx->le_mode)) {
fa3966a3
AJ
2952 TCGv t0 = tcg_temp_new();
2953 tcg_gen_ext32u_tl(t0, arg1);
2954 tcg_gen_bswap32_tl(t0, t0);
76db3ba4
AJ
2955 tcg_gen_qemu_st32(t0, arg2, ctx->mem_idx);
2956 tcg_temp_free(t0);
76db3ba4
AJ
2957 } else {
2958 tcg_gen_qemu_st32(arg1, arg2, ctx->mem_idx);
2959 }
b61f2753 2960}
0c8aacd4 2961GEN_STX(stwbr, st32r, 0x16, 0x14, PPC_INTEGER);
79aceca5 2962
cd6e9320
TH
2963#if defined(TARGET_PPC64)
2964/* stdbrx */
2965static inline void gen_qemu_st64r(DisasContext *ctx, TCGv arg1, TCGv arg2)
2966{
2967 if (likely(!ctx->le_mode)) {
2968 TCGv t0 = tcg_temp_new();
2969 tcg_gen_bswap64_tl(t0, arg1);
2970 tcg_gen_qemu_st64(t0, arg2, ctx->mem_idx);
2971 tcg_temp_free(t0);
2972 } else {
2973 tcg_gen_qemu_st64(arg1, arg2, ctx->mem_idx);
2974 }
2975}
2976GEN_STX_E(stdbr, st64r, 0x14, 0x14, PPC_NONE, PPC2_DBRX);
2977#endif /* TARGET_PPC64 */
2978
79aceca5 2979/*** Integer load and store multiple ***/
99e300ef 2980
54623277 2981/* lmw */
99e300ef 2982static void gen_lmw(DisasContext *ctx)
79aceca5 2983{
76db3ba4
AJ
2984 TCGv t0;
2985 TCGv_i32 t1;
2986 gen_set_access_type(ctx, ACCESS_INT);
76a66253 2987 /* NIP cannot be restored if the memory exception comes from an helper */
d9bce9d9 2988 gen_update_nip(ctx, ctx->nip - 4);
76db3ba4
AJ
2989 t0 = tcg_temp_new();
2990 t1 = tcg_const_i32(rD(ctx->opcode));
2991 gen_addr_imm_index(ctx, t0, 0);
ff4a62cd
AJ
2992 gen_helper_lmw(t0, t1);
2993 tcg_temp_free(t0);
2994 tcg_temp_free_i32(t1);
79aceca5
FB
2995}
2996
2997/* stmw */
99e300ef 2998static void gen_stmw(DisasContext *ctx)
79aceca5 2999{
76db3ba4
AJ
3000 TCGv t0;
3001 TCGv_i32 t1;
3002 gen_set_access_type(ctx, ACCESS_INT);
76a66253 3003 /* NIP cannot be restored if the memory exception comes from an helper */
d9bce9d9 3004 gen_update_nip(ctx, ctx->nip - 4);
76db3ba4
AJ
3005 t0 = tcg_temp_new();
3006 t1 = tcg_const_i32(rS(ctx->opcode));
3007 gen_addr_imm_index(ctx, t0, 0);
ff4a62cd
AJ
3008 gen_helper_stmw(t0, t1);
3009 tcg_temp_free(t0);
3010 tcg_temp_free_i32(t1);
79aceca5
FB
3011}
3012
3013/*** Integer load and store strings ***/
54623277 3014
79aceca5 3015/* lswi */
3fc6c082 3016/* PowerPC32 specification says we must generate an exception if
9a64fbe4
FB
3017 * rA is in the range of registers to be loaded.
3018 * In an other hand, IBM says this is valid, but rA won't be loaded.
3019 * For now, I'll follow the spec...
3020 */
99e300ef 3021static void gen_lswi(DisasContext *ctx)
79aceca5 3022{
dfbc799d
AJ
3023 TCGv t0;
3024 TCGv_i32 t1, t2;
79aceca5
FB
3025 int nb = NB(ctx->opcode);
3026 int start = rD(ctx->opcode);
9a64fbe4 3027 int ra = rA(ctx->opcode);
79aceca5
FB
3028 int nr;
3029
3030 if (nb == 0)
3031 nb = 32;
3032 nr = nb / 4;
76a66253
JM
3033 if (unlikely(((start + nr) > 32 &&
3034 start <= ra && (start + nr - 32) > ra) ||
3035 ((start + nr) <= 32 && start <= ra && (start + nr) > ra))) {
e06fcd75 3036 gen_inval_exception(ctx, POWERPC_EXCP_INVAL_LSWX);
9fddaa0c 3037 return;
297d8e62 3038 }
76db3ba4 3039 gen_set_access_type(ctx, ACCESS_INT);
8dd4983c 3040 /* NIP cannot be restored if the memory exception comes from an helper */
d9bce9d9 3041 gen_update_nip(ctx, ctx->nip - 4);
dfbc799d 3042 t0 = tcg_temp_new();
76db3ba4 3043 gen_addr_register(ctx, t0);
dfbc799d
AJ
3044 t1 = tcg_const_i32(nb);
3045 t2 = tcg_const_i32(start);
3046 gen_helper_lsw(t0, t1, t2);
3047 tcg_temp_free(t0);
3048 tcg_temp_free_i32(t1);
3049 tcg_temp_free_i32(t2);
79aceca5
FB
3050}
3051
3052/* lswx */
99e300ef 3053static void gen_lswx(DisasContext *ctx)
79aceca5 3054{
76db3ba4
AJ
3055 TCGv t0;
3056 TCGv_i32 t1, t2, t3;
3057 gen_set_access_type(ctx, ACCESS_INT);
76a66253 3058 /* NIP cannot be restored if the memory exception comes from an helper */
d9bce9d9 3059 gen_update_nip(ctx, ctx->nip - 4);
76db3ba4
AJ
3060 t0 = tcg_temp_new();
3061 gen_addr_reg_index(ctx, t0);
3062 t1 = tcg_const_i32(rD(ctx->opcode));
3063 t2 = tcg_const_i32(rA(ctx->opcode));
3064 t3 = tcg_const_i32(rB(ctx->opcode));
dfbc799d
AJ
3065 gen_helper_lswx(t0, t1, t2, t3);
3066 tcg_temp_free(t0);
3067 tcg_temp_free_i32(t1);
3068 tcg_temp_free_i32(t2);
3069 tcg_temp_free_i32(t3);
79aceca5
FB
3070}
3071
3072/* stswi */
99e300ef 3073static void gen_stswi(DisasContext *ctx)
79aceca5 3074{
76db3ba4
AJ
3075 TCGv t0;
3076 TCGv_i32 t1, t2;
4b3686fa 3077 int nb = NB(ctx->opcode);
76db3ba4 3078 gen_set_access_type(ctx, ACCESS_INT);
76a66253 3079 /* NIP cannot be restored if the memory exception comes from an helper */
d9bce9d9 3080 gen_update_nip(ctx, ctx->nip - 4);
76db3ba4
AJ
3081 t0 = tcg_temp_new();
3082 gen_addr_register(ctx, t0);
4b3686fa
FB
3083 if (nb == 0)
3084 nb = 32;
dfbc799d 3085 t1 = tcg_const_i32(nb);
76db3ba4 3086 t2 = tcg_const_i32(rS(ctx->opcode));
dfbc799d
AJ
3087 gen_helper_stsw(t0, t1, t2);
3088 tcg_temp_free(t0);
3089 tcg_temp_free_i32(t1);
3090 tcg_temp_free_i32(t2);
79aceca5
FB
3091}
3092
3093/* stswx */
99e300ef 3094static void gen_stswx(DisasContext *ctx)
79aceca5 3095{
76db3ba4
AJ
3096 TCGv t0;
3097 TCGv_i32 t1, t2;
3098 gen_set_access_type(ctx, ACCESS_INT);
8dd4983c 3099 /* NIP cannot be restored if the memory exception comes from an helper */
5fafdf24 3100 gen_update_nip(ctx, ctx->nip - 4);
76db3ba4
AJ
3101 t0 = tcg_temp_new();
3102 gen_addr_reg_index(ctx, t0);
3103 t1 = tcg_temp_new_i32();
dfbc799d
AJ
3104 tcg_gen_trunc_tl_i32(t1, cpu_xer);
3105 tcg_gen_andi_i32(t1, t1, 0x7F);
76db3ba4 3106 t2 = tcg_const_i32(rS(ctx->opcode));
dfbc799d
AJ
3107 gen_helper_stsw(t0, t1, t2);
3108 tcg_temp_free(t0);
3109 tcg_temp_free_i32(t1);
3110 tcg_temp_free_i32(t2);
79aceca5
FB
3111}
3112
3113/*** Memory synchronisation ***/
3114/* eieio */
99e300ef 3115static void gen_eieio(DisasContext *ctx)
79aceca5 3116{
79aceca5
FB
3117}
3118
3119/* isync */
99e300ef 3120static void gen_isync(DisasContext *ctx)
79aceca5 3121{
e06fcd75 3122 gen_stop_exception(ctx);
79aceca5
FB
3123}
3124
111bfab3 3125/* lwarx */
99e300ef 3126static void gen_lwarx(DisasContext *ctx)
79aceca5 3127{
76db3ba4 3128 TCGv t0;
18b21a2f 3129 TCGv gpr = cpu_gpr[rD(ctx->opcode)];
76db3ba4
AJ
3130 gen_set_access_type(ctx, ACCESS_RES);
3131 t0 = tcg_temp_local_new();
3132 gen_addr_reg_index(ctx, t0);
cf360a32 3133 gen_check_align(ctx, t0, 0x03);
18b21a2f 3134 gen_qemu_ld32u(ctx, gpr, t0);
cf360a32 3135 tcg_gen_mov_tl(cpu_reserve, t0);
1328c2bf 3136 tcg_gen_st_tl(gpr, cpu_env, offsetof(CPUPPCState, reserve_val));
cf360a32 3137 tcg_temp_free(t0);
79aceca5
FB
3138}
3139
4425265b
NF
3140#if defined(CONFIG_USER_ONLY)
3141static void gen_conditional_store (DisasContext *ctx, TCGv EA,
3142 int reg, int size)
3143{
3144 TCGv t0 = tcg_temp_new();
3145 uint32_t save_exception = ctx->exception;
3146
1328c2bf 3147 tcg_gen_st_tl(EA, cpu_env, offsetof(CPUPPCState, reserve_ea));
4425265b 3148 tcg_gen_movi_tl(t0, (size << 5) | reg);
1328c2bf 3149 tcg_gen_st_tl(t0, cpu_env, offsetof(CPUPPCState, reserve_info));
4425265b
NF
3150 tcg_temp_free(t0);
3151 gen_update_nip(ctx, ctx->nip-4);
3152 ctx->exception = POWERPC_EXCP_BRANCH;
3153 gen_exception(ctx, POWERPC_EXCP_STCX);
3154 ctx->exception = save_exception;
3155}
3156#endif
3157
79aceca5 3158/* stwcx. */
e8eaa2c0 3159static void gen_stwcx_(DisasContext *ctx)
79aceca5 3160{
76db3ba4
AJ
3161 TCGv t0;
3162 gen_set_access_type(ctx, ACCESS_RES);
3163 t0 = tcg_temp_local_new();
3164 gen_addr_reg_index(ctx, t0);
cf360a32 3165 gen_check_align(ctx, t0, 0x03);
4425265b
NF
3166#if defined(CONFIG_USER_ONLY)
3167 gen_conditional_store(ctx, t0, rS(ctx->opcode), 4);
3168#else
3169 {
3170 int l1;
3171
3172 tcg_gen_trunc_tl_i32(cpu_crf[0], cpu_xer);
3173 tcg_gen_shri_i32(cpu_crf[0], cpu_crf[0], XER_SO);
3174 tcg_gen_andi_i32(cpu_crf[0], cpu_crf[0], 1);
3175 l1 = gen_new_label();
3176 tcg_gen_brcond_tl(TCG_COND_NE, t0, cpu_reserve, l1);
3177 tcg_gen_ori_i32(cpu_crf[0], cpu_crf[0], 1 << CRF_EQ);
3178 gen_qemu_st32(ctx, cpu_gpr[rS(ctx->opcode)], t0);
3179 gen_set_label(l1);
3180 tcg_gen_movi_tl(cpu_reserve, -1);
3181 }
3182#endif
cf360a32 3183 tcg_temp_free(t0);
79aceca5
FB
3184}
3185
426613db 3186#if defined(TARGET_PPC64)
426613db 3187/* ldarx */
99e300ef 3188static void gen_ldarx(DisasContext *ctx)
426613db 3189{
76db3ba4 3190 TCGv t0;
18b21a2f 3191 TCGv gpr = cpu_gpr[rD(ctx->opcode)];
76db3ba4
AJ
3192 gen_set_access_type(ctx, ACCESS_RES);
3193 t0 = tcg_temp_local_new();
3194 gen_addr_reg_index(ctx, t0);
cf360a32 3195 gen_check_align(ctx, t0, 0x07);
18b21a2f 3196 gen_qemu_ld64(ctx, gpr, t0);
cf360a32 3197 tcg_gen_mov_tl(cpu_reserve, t0);
1328c2bf 3198 tcg_gen_st_tl(gpr, cpu_env, offsetof(CPUPPCState, reserve_val));
cf360a32 3199 tcg_temp_free(t0);
426613db
JM
3200}
3201
3202/* stdcx. */
e8eaa2c0 3203static void gen_stdcx_(DisasContext *ctx)
426613db 3204{
76db3ba4
AJ
3205 TCGv t0;
3206 gen_set_access_type(ctx, ACCESS_RES);
3207 t0 = tcg_temp_local_new();
3208 gen_addr_reg_index(ctx, t0);
cf360a32 3209 gen_check_align(ctx, t0, 0x07);
4425265b
NF
3210#if defined(CONFIG_USER_ONLY)
3211 gen_conditional_store(ctx, t0, rS(ctx->opcode), 8);
3212#else
3213 {
3214 int l1;
3215 tcg_gen_trunc_tl_i32(cpu_crf[0], cpu_xer);
3216 tcg_gen_shri_i32(cpu_crf[0], cpu_crf[0], XER_SO);
3217 tcg_gen_andi_i32(cpu_crf[0], cpu_crf[0], 1);
3218 l1 = gen_new_label();
3219 tcg_gen_brcond_tl(TCG_COND_NE, t0, cpu_reserve, l1);
3220 tcg_gen_ori_i32(cpu_crf[0], cpu_crf[0], 1 << CRF_EQ);
3221 gen_qemu_st64(ctx, cpu_gpr[rS(ctx->opcode)], t0);
3222 gen_set_label(l1);
3223 tcg_gen_movi_tl(cpu_reserve, -1);
3224 }
3225#endif
cf360a32 3226 tcg_temp_free(t0);
426613db
JM
3227}
3228#endif /* defined(TARGET_PPC64) */
3229
79aceca5 3230/* sync */
99e300ef 3231static void gen_sync(DisasContext *ctx)
79aceca5 3232{
79aceca5
FB
3233}
3234
0db1b20e 3235/* wait */
99e300ef 3236static void gen_wait(DisasContext *ctx)
0db1b20e 3237{
931ff272 3238 TCGv_i32 t0 = tcg_temp_new_i32();
1328c2bf 3239 tcg_gen_st_i32(t0, cpu_env, offsetof(CPUPPCState, halted));
931ff272 3240 tcg_temp_free_i32(t0);
0db1b20e 3241 /* Stop translation, as the CPU is supposed to sleep from now */
e06fcd75 3242 gen_exception_err(ctx, EXCP_HLT, 1);
0db1b20e
JM
3243}
3244
79aceca5 3245/*** Floating-point load ***/
a0d7d5a7 3246#define GEN_LDF(name, ldop, opc, type) \
99e300ef 3247static void glue(gen_, name)(DisasContext *ctx) \
79aceca5 3248{ \
a0d7d5a7 3249 TCGv EA; \
76a66253 3250 if (unlikely(!ctx->fpu_enabled)) { \
e06fcd75 3251 gen_exception(ctx, POWERPC_EXCP_FPU); \
4ecc3190
FB
3252 return; \
3253 } \
76db3ba4 3254 gen_set_access_type(ctx, ACCESS_FLOAT); \
a0d7d5a7 3255 EA = tcg_temp_new(); \
76db3ba4
AJ
3256 gen_addr_imm_index(ctx, EA, 0); \
3257 gen_qemu_##ldop(ctx, cpu_fpr[rD(ctx->opcode)], EA); \
a0d7d5a7 3258 tcg_temp_free(EA); \
79aceca5
FB
3259}
3260
a0d7d5a7 3261#define GEN_LDUF(name, ldop, opc, type) \
99e300ef 3262static void glue(gen_, name##u)(DisasContext *ctx) \
79aceca5 3263{ \
a0d7d5a7 3264 TCGv EA; \
76a66253 3265 if (unlikely(!ctx->fpu_enabled)) { \
e06fcd75 3266 gen_exception(ctx, POWERPC_EXCP_FPU); \
4ecc3190
FB
3267 return; \
3268 } \
76a66253 3269 if (unlikely(rA(ctx->opcode) == 0)) { \
e06fcd75 3270 gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL); \
9fddaa0c 3271 return; \
9a64fbe4 3272 } \
76db3ba4 3273 gen_set_access_type(ctx, ACCESS_FLOAT); \
a0d7d5a7 3274 EA = tcg_temp_new(); \
76db3ba4
AJ
3275 gen_addr_imm_index(ctx, EA, 0); \
3276 gen_qemu_##ldop(ctx, cpu_fpr[rD(ctx->opcode)], EA); \
a0d7d5a7
AJ
3277 tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], EA); \
3278 tcg_temp_free(EA); \
79aceca5
FB
3279}
3280
a0d7d5a7 3281#define GEN_LDUXF(name, ldop, opc, type) \
99e300ef 3282static void glue(gen_, name##ux)(DisasContext *ctx) \
79aceca5 3283{ \
a0d7d5a7 3284 TCGv EA; \
76a66253 3285 if (unlikely(!ctx->fpu_enabled)) { \
e06fcd75 3286 gen_exception(ctx, POWERPC_EXCP_FPU); \
4ecc3190
FB
3287 return; \
3288 } \
76a66253 3289 if (unlikely(rA(ctx->opcode) == 0)) { \
e06fcd75 3290 gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL); \
9fddaa0c 3291 return; \
9a64fbe4 3292 } \
76db3ba4 3293 gen_set_access_type(ctx, ACCESS_FLOAT); \
a0d7d5a7 3294 EA = tcg_temp_new(); \
76db3ba4
AJ
3295 gen_addr_reg_index(ctx, EA); \
3296 gen_qemu_##ldop(ctx, cpu_fpr[rD(ctx->opcode)], EA); \
a0d7d5a7
AJ
3297 tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], EA); \
3298 tcg_temp_free(EA); \
79aceca5
FB
3299}
3300
a0d7d5a7 3301#define GEN_LDXF(name, ldop, opc2, opc3, type) \
99e300ef 3302static void glue(gen_, name##x)(DisasContext *ctx) \
79aceca5 3303{ \
a0d7d5a7 3304 TCGv EA; \
76a66253 3305 if (unlikely(!ctx->fpu_enabled)) { \
e06fcd75 3306 gen_exception(ctx, POWERPC_EXCP_FPU); \
4ecc3190
FB
3307 return; \
3308 } \
76db3ba4 3309 gen_set_access_type(ctx, ACCESS_FLOAT); \
a0d7d5a7 3310 EA = tcg_temp_new(); \
76db3ba4
AJ
3311 gen_addr_reg_index(ctx, EA); \
3312 gen_qemu_##ldop(ctx, cpu_fpr[rD(ctx->opcode)], EA); \
a0d7d5a7 3313 tcg_temp_free(EA); \
79aceca5
FB
3314}
3315
a0d7d5a7
AJ
3316#define GEN_LDFS(name, ldop, op, type) \
3317GEN_LDF(name, ldop, op | 0x20, type); \
3318GEN_LDUF(name, ldop, op | 0x21, type); \
3319GEN_LDUXF(name, ldop, op | 0x01, type); \
3320GEN_LDXF(name, ldop, 0x17, op | 0x00, type)
3321
636aa200 3322static inline void gen_qemu_ld32fs(DisasContext *ctx, TCGv_i64 arg1, TCGv arg2)
a0d7d5a7
AJ
3323{
3324 TCGv t0 = tcg_temp_new();
3325 TCGv_i32 t1 = tcg_temp_new_i32();
76db3ba4 3326 gen_qemu_ld32u(ctx, t0, arg2);
a0d7d5a7
AJ
3327 tcg_gen_trunc_tl_i32(t1, t0);
3328 tcg_temp_free(t0);
8e703949 3329 gen_helper_float32_to_float64(arg1, cpu_env, t1);
a0d7d5a7
AJ
3330 tcg_temp_free_i32(t1);
3331}
79aceca5 3332
a0d7d5a7
AJ
3333 /* lfd lfdu lfdux lfdx */
3334GEN_LDFS(lfd, ld64, 0x12, PPC_FLOAT);
3335 /* lfs lfsu lfsux lfsx */
3336GEN_LDFS(lfs, ld32fs, 0x10, PPC_FLOAT);
79aceca5
FB
3337
3338/*** Floating-point store ***/
a0d7d5a7 3339#define GEN_STF(name, stop, opc, type) \
99e300ef 3340static void glue(gen_, name)(DisasContext *ctx) \
79aceca5 3341{ \
a0d7d5a7 3342 TCGv EA; \
76a66253 3343 if (unlikely(!ctx->fpu_enabled)) { \
e06fcd75 3344 gen_exception(ctx, POWERPC_EXCP_FPU); \
4ecc3190
FB
3345 return; \
3346 } \
76db3ba4 3347 gen_set_access_type(ctx, ACCESS_FLOAT); \
a0d7d5a7 3348 EA = tcg_temp_new(); \
76db3ba4
AJ
3349 gen_addr_imm_index(ctx, EA, 0); \
3350 gen_qemu_##stop(ctx, cpu_fpr[rS(ctx->opcode)], EA); \
a0d7d5a7 3351 tcg_temp_free(EA); \
79aceca5
FB
3352}
3353
a0d7d5a7 3354#define GEN_STUF(name, stop, opc, type) \
99e300ef 3355static void glue(gen_, name##u)(DisasContext *ctx) \
79aceca5 3356{ \
a0d7d5a7 3357 TCGv EA; \
76a66253 3358 if (unlikely(!ctx->fpu_enabled)) { \
e06fcd75 3359 gen_exception(ctx, POWERPC_EXCP_FPU); \
4ecc3190
FB
3360 return; \
3361 } \
76a66253 3362 if (unlikely(rA(ctx->opcode) == 0)) { \
e06fcd75 3363 gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL); \
9fddaa0c 3364 return; \
9a64fbe4 3365 } \
76db3ba4 3366 gen_set_access_type(ctx, ACCESS_FLOAT); \
a0d7d5a7 3367 EA = tcg_temp_new(); \
76db3ba4
AJ
3368 gen_addr_imm_index(ctx, EA, 0); \
3369 gen_qemu_##stop(ctx, cpu_fpr[rS(ctx->opcode)], EA); \
a0d7d5a7
AJ
3370 tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], EA); \
3371 tcg_temp_free(EA); \
79aceca5
FB
3372}
3373
a0d7d5a7 3374#define GEN_STUXF(name, stop, opc, type) \
99e300ef 3375static void glue(gen_, name##ux)(DisasContext *ctx) \
79aceca5 3376{ \
a0d7d5a7 3377 TCGv EA; \
76a66253 3378 if (unlikely(!ctx->fpu_enabled)) { \
e06fcd75 3379 gen_exception(ctx, POWERPC_EXCP_FPU); \
4ecc3190
FB
3380 return; \
3381 } \
76a66253 3382 if (unlikely(rA(ctx->opcode) == 0)) { \
e06fcd75 3383 gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL); \
9fddaa0c 3384 return; \
9a64fbe4 3385 } \
76db3ba4 3386 gen_set_access_type(ctx, ACCESS_FLOAT); \
a0d7d5a7 3387 EA = tcg_temp_new(); \
76db3ba4
AJ
3388 gen_addr_reg_index(ctx, EA); \
3389 gen_qemu_##stop(ctx, cpu_fpr[rS(ctx->opcode)], EA); \
a0d7d5a7
AJ
3390 tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], EA); \
3391 tcg_temp_free(EA); \
79aceca5
FB
3392}
3393
a0d7d5a7 3394#define GEN_STXF(name, stop, opc2, opc3, type) \
99e300ef 3395static void glue(gen_, name##x)(DisasContext *ctx) \
79aceca5 3396{ \
a0d7d5a7 3397 TCGv EA; \
76a66253 3398 if (unlikely(!ctx->fpu_enabled)) { \
e06fcd75 3399 gen_exception(ctx, POWERPC_EXCP_FPU); \
4ecc3190
FB
3400 return; \
3401 } \
76db3ba4 3402 gen_set_access_type(ctx, ACCESS_FLOAT); \
a0d7d5a7 3403 EA = tcg_temp_new(); \
76db3ba4
AJ
3404 gen_addr_reg_index(ctx, EA); \
3405 gen_qemu_##stop(ctx, cpu_fpr[rS(ctx->opcode)], EA); \
a0d7d5a7 3406 tcg_temp_free(EA); \
79aceca5
FB
3407}
3408
a0d7d5a7
AJ
3409#define GEN_STFS(name, stop, op, type) \
3410GEN_STF(name, stop, op | 0x20, type); \
3411GEN_STUF(name, stop, op | 0x21, type); \
3412GEN_STUXF(name, stop, op | 0x01, type); \
3413GEN_STXF(name, stop, 0x17, op | 0x00, type)
3414
636aa200 3415static inline void gen_qemu_st32fs(DisasContext *ctx, TCGv_i64 arg1, TCGv arg2)
a0d7d5a7
AJ
3416{
3417 TCGv_i32 t0 = tcg_temp_new_i32();
3418 TCGv t1 = tcg_temp_new();
8e703949 3419 gen_helper_float64_to_float32(t0, cpu_env, arg1);
a0d7d5a7
AJ
3420 tcg_gen_extu_i32_tl(t1, t0);
3421 tcg_temp_free_i32(t0);
76db3ba4 3422 gen_qemu_st32(ctx, t1, arg2);
a0d7d5a7
AJ
3423 tcg_temp_free(t1);
3424}
79aceca5
FB
3425
3426/* stfd stfdu stfdux stfdx */
a0d7d5a7 3427GEN_STFS(stfd, st64, 0x16, PPC_FLOAT);
79aceca5 3428/* stfs stfsu stfsux stfsx */
a0d7d5a7 3429GEN_STFS(stfs, st32fs, 0x14, PPC_FLOAT);
79aceca5
FB
3430
3431/* Optional: */
636aa200 3432static inline void gen_qemu_st32fiw(DisasContext *ctx, TCGv_i64 arg1, TCGv arg2)
a0d7d5a7
AJ
3433{
3434 TCGv t0 = tcg_temp_new();
3435 tcg_gen_trunc_i64_tl(t0, arg1),
76db3ba4 3436 gen_qemu_st32(ctx, t0, arg2);
a0d7d5a7
AJ
3437 tcg_temp_free(t0);
3438}
79aceca5 3439/* stfiwx */
a0d7d5a7 3440GEN_STXF(stfiw, st32fiw, 0x17, 0x1E, PPC_FLOAT_STFIWX);
79aceca5 3441
697ab892
DG
3442static inline void gen_update_cfar(DisasContext *ctx, target_ulong nip)
3443{
3444#if defined(TARGET_PPC64)
3445 if (ctx->has_cfar)
3446 tcg_gen_movi_tl(cpu_cfar, nip);
3447#endif
3448}
3449
79aceca5 3450/*** Branch ***/
636aa200 3451static inline void gen_goto_tb(DisasContext *ctx, int n, target_ulong dest)
c1942362
FB
3452{
3453 TranslationBlock *tb;
3454 tb = ctx->tb;
a2ffb812
AJ
3455#if defined(TARGET_PPC64)
3456 if (!ctx->sf_mode)
3457 dest = (uint32_t) dest;
3458#endif
57fec1fe 3459 if ((tb->pc & TARGET_PAGE_MASK) == (dest & TARGET_PAGE_MASK) &&
8cbcb4fa 3460 likely(!ctx->singlestep_enabled)) {
57fec1fe 3461 tcg_gen_goto_tb(n);
a2ffb812 3462 tcg_gen_movi_tl(cpu_nip, dest & ~3);
4b4a72e5 3463 tcg_gen_exit_tb((tcg_target_long)tb + n);
c1942362 3464 } else {
a2ffb812 3465 tcg_gen_movi_tl(cpu_nip, dest & ~3);
8cbcb4fa
AJ
3466 if (unlikely(ctx->singlestep_enabled)) {
3467 if ((ctx->singlestep_enabled &
bdc4e053 3468 (CPU_BRANCH_STEP | CPU_SINGLE_STEP)) &&
8cbcb4fa
AJ
3469 ctx->exception == POWERPC_EXCP_BRANCH) {
3470 target_ulong tmp = ctx->nip;
3471 ctx->nip = dest;
e06fcd75 3472 gen_exception(ctx, POWERPC_EXCP_TRACE);
8cbcb4fa
AJ
3473 ctx->nip = tmp;
3474 }
3475 if (ctx->singlestep_enabled & GDBSTUB_SINGLE_STEP) {
e06fcd75 3476 gen_debug_exception(ctx);
8cbcb4fa
AJ
3477 }
3478 }
57fec1fe 3479 tcg_gen_exit_tb(0);
c1942362 3480 }
c53be334
FB
3481}
3482
636aa200 3483static inline void gen_setlr(DisasContext *ctx, target_ulong nip)
e1833e1f
JM
3484{
3485#if defined(TARGET_PPC64)
a2ffb812
AJ
3486 if (ctx->sf_mode == 0)
3487 tcg_gen_movi_tl(cpu_lr, (uint32_t)nip);
e1833e1f
JM
3488 else
3489#endif
a2ffb812 3490 tcg_gen_movi_tl(cpu_lr, nip);
e1833e1f
JM
3491}
3492
79aceca5 3493/* b ba bl bla */
99e300ef 3494static void gen_b(DisasContext *ctx)
79aceca5 3495{
76a66253 3496 target_ulong li, target;
38a64f9d 3497
8cbcb4fa 3498 ctx->exception = POWERPC_EXCP_BRANCH;
38a64f9d 3499 /* sign extend LI */
76a66253 3500#if defined(TARGET_PPC64)
d9bce9d9
JM
3501 if (ctx->sf_mode)
3502 li = ((int64_t)LI(ctx->opcode) << 38) >> 38;
3503 else
76a66253 3504#endif
d9bce9d9 3505 li = ((int32_t)LI(ctx->opcode) << 6) >> 6;
76a66253 3506 if (likely(AA(ctx->opcode) == 0))
046d6672 3507 target = ctx->nip + li - 4;
79aceca5 3508 else
9a64fbe4 3509 target = li;
e1833e1f
JM
3510 if (LK(ctx->opcode))
3511 gen_setlr(ctx, ctx->nip);
697ab892 3512 gen_update_cfar(ctx, ctx->nip);
c1942362 3513 gen_goto_tb(ctx, 0, target);
79aceca5
FB
3514}
3515
e98a6e40
FB
3516#define BCOND_IM 0
3517#define BCOND_LR 1
3518#define BCOND_CTR 2
3519
636aa200 3520static inline void gen_bcond(DisasContext *ctx, int type)
d9bce9d9 3521{
d9bce9d9 3522 uint32_t bo = BO(ctx->opcode);
05f92404 3523 int l1;
a2ffb812 3524 TCGv target;
e98a6e40 3525
8cbcb4fa 3526 ctx->exception = POWERPC_EXCP_BRANCH;
a2ffb812 3527 if (type == BCOND_LR || type == BCOND_CTR) {
a7812ae4 3528 target = tcg_temp_local_new();
a2ffb812
AJ
3529 if (type == BCOND_CTR)
3530 tcg_gen_mov_tl(target, cpu_ctr);
3531 else
3532 tcg_gen_mov_tl(target, cpu_lr);
d2e9fd8f 3533 } else {
3534 TCGV_UNUSED(target);
e98a6e40 3535 }
e1833e1f
JM
3536 if (LK(ctx->opcode))
3537 gen_setlr(ctx, ctx->nip);
a2ffb812
AJ
3538 l1 = gen_new_label();
3539 if ((bo & 0x4) == 0) {
3540 /* Decrement and test CTR */
a7812ae4 3541 TCGv temp = tcg_temp_new();
a2ffb812 3542 if (unlikely(type == BCOND_CTR)) {
e06fcd75 3543 gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL);
a2ffb812
AJ
3544 return;
3545 }
3546 tcg_gen_subi_tl(cpu_ctr, cpu_ctr, 1);
d9bce9d9 3547#if defined(TARGET_PPC64)
a2ffb812
AJ
3548 if (!ctx->sf_mode)
3549 tcg_gen_ext32u_tl(temp, cpu_ctr);
3550 else
d9bce9d9 3551#endif
a2ffb812
AJ
3552 tcg_gen_mov_tl(temp, cpu_ctr);
3553 if (bo & 0x2) {
3554 tcg_gen_brcondi_tl(TCG_COND_NE, temp, 0, l1);
3555 } else {
3556 tcg_gen_brcondi_tl(TCG_COND_EQ, temp, 0, l1);
e98a6e40 3557 }
a7812ae4 3558 tcg_temp_free(temp);
a2ffb812
AJ
3559 }
3560 if ((bo & 0x10) == 0) {
3561 /* Test CR */
3562 uint32_t bi = BI(ctx->opcode);
3563 uint32_t mask = 1 << (3 - (bi & 0x03));
a7812ae4 3564 TCGv_i32 temp = tcg_temp_new_i32();
a2ffb812 3565
d9bce9d9 3566 if (bo & 0x8) {
a2ffb812
AJ
3567 tcg_gen_andi_i32(temp, cpu_crf[bi >> 2], mask);
3568 tcg_gen_brcondi_i32(TCG_COND_EQ, temp, 0, l1);
d9bce9d9 3569 } else {
a2ffb812
AJ
3570 tcg_gen_andi_i32(temp, cpu_crf[bi >> 2], mask);
3571 tcg_gen_brcondi_i32(TCG_COND_NE, temp, 0, l1);
d9bce9d9 3572 }
a7812ae4 3573 tcg_temp_free_i32(temp);
d9bce9d9 3574 }
697ab892 3575 gen_update_cfar(ctx, ctx->nip);
e98a6e40 3576 if (type == BCOND_IM) {
a2ffb812
AJ
3577 target_ulong li = (target_long)((int16_t)(BD(ctx->opcode)));
3578 if (likely(AA(ctx->opcode) == 0)) {
3579 gen_goto_tb(ctx, 0, ctx->nip + li - 4);
3580 } else {
3581 gen_goto_tb(ctx, 0, li);
3582 }
c53be334 3583 gen_set_label(l1);
c1942362 3584 gen_goto_tb(ctx, 1, ctx->nip);
e98a6e40 3585 } else {
d9bce9d9 3586#if defined(TARGET_PPC64)
a2ffb812
AJ
3587 if (!(ctx->sf_mode))
3588 tcg_gen_andi_tl(cpu_nip, target, (uint32_t)~3);
3589 else
3590#endif
3591 tcg_gen_andi_tl(cpu_nip, target, ~3);
3592 tcg_gen_exit_tb(0);
3593 gen_set_label(l1);
3594#if defined(TARGET_PPC64)
3595 if (!(ctx->sf_mode))
3596 tcg_gen_movi_tl(cpu_nip, (uint32_t)ctx->nip);
d9bce9d9
JM
3597 else
3598#endif
a2ffb812 3599 tcg_gen_movi_tl(cpu_nip, ctx->nip);
57fec1fe 3600 tcg_gen_exit_tb(0);
08e46e54 3601 }
e98a6e40
FB
3602}
3603
99e300ef 3604static void gen_bc(DisasContext *ctx)
3b46e624 3605{
e98a6e40
FB
3606 gen_bcond(ctx, BCOND_IM);
3607}
3608
99e300ef 3609static void gen_bcctr(DisasContext *ctx)
3b46e624 3610{
e98a6e40
FB
3611 gen_bcond(ctx, BCOND_CTR);
3612}
3613
99e300ef 3614static void gen_bclr(DisasContext *ctx)
3b46e624 3615{
e98a6e40
FB
3616 gen_bcond(ctx, BCOND_LR);
3617}
79aceca5
FB
3618
3619/*** Condition register logical ***/
e1571908 3620#define GEN_CRLOGIC(name, tcg_op, opc) \
99e300ef 3621static void glue(gen_, name)(DisasContext *ctx) \
79aceca5 3622{ \
fc0d441e
JM
3623 uint8_t bitmask; \
3624 int sh; \
a7812ae4 3625 TCGv_i32 t0, t1; \
fc0d441e 3626 sh = (crbD(ctx->opcode) & 0x03) - (crbA(ctx->opcode) & 0x03); \
a7812ae4 3627 t0 = tcg_temp_new_i32(); \
fc0d441e 3628 if (sh > 0) \
fea0c503 3629 tcg_gen_shri_i32(t0, cpu_crf[crbA(ctx->opcode) >> 2], sh); \
fc0d441e 3630 else if (sh < 0) \
fea0c503 3631 tcg_gen_shli_i32(t0, cpu_crf[crbA(ctx->opcode) >> 2], -sh); \
e1571908 3632 else \
fea0c503 3633 tcg_gen_mov_i32(t0, cpu_crf[crbA(ctx->opcode) >> 2]); \
a7812ae4 3634 t1 = tcg_temp_new_i32(); \
fc0d441e
JM
3635 sh = (crbD(ctx->opcode) & 0x03) - (crbB(ctx->opcode) & 0x03); \
3636 if (sh > 0) \
fea0c503 3637 tcg_gen_shri_i32(t1, cpu_crf[crbB(ctx->opcode) >> 2], sh); \
fc0d441e 3638 else if (sh < 0) \
fea0c503 3639 tcg_gen_shli_i32(t1, cpu_crf[crbB(ctx->opcode) >> 2], -sh); \
e1571908 3640 else \
fea0c503
AJ
3641 tcg_gen_mov_i32(t1, cpu_crf[crbB(ctx->opcode) >> 2]); \
3642 tcg_op(t0, t0, t1); \
fc0d441e 3643 bitmask = 1 << (3 - (crbD(ctx->opcode) & 0x03)); \
fea0c503
AJ
3644 tcg_gen_andi_i32(t0, t0, bitmask); \
3645 tcg_gen_andi_i32(t1, cpu_crf[crbD(ctx->opcode) >> 2], ~bitmask); \
3646 tcg_gen_or_i32(cpu_crf[crbD(ctx->opcode) >> 2], t0, t1); \
a7812ae4
PB
3647 tcg_temp_free_i32(t0); \
3648 tcg_temp_free_i32(t1); \
79aceca5
FB
3649}
3650
3651/* crand */
e1571908 3652GEN_CRLOGIC(crand, tcg_gen_and_i32, 0x08);
79aceca5 3653/* crandc */
e1571908 3654GEN_CRLOGIC(crandc, tcg_gen_andc_i32, 0x04);
79aceca5 3655/* creqv */
e1571908 3656GEN_CRLOGIC(creqv, tcg_gen_eqv_i32, 0x09);
79aceca5 3657/* crnand */
e1571908 3658GEN_CRLOGIC(crnand, tcg_gen_nand_i32, 0x07);
79aceca5 3659/* crnor */
e1571908 3660GEN_CRLOGIC(crnor, tcg_gen_nor_i32, 0x01);
79aceca5 3661/* cror */
e1571908 3662GEN_CRLOGIC(cror, tcg_gen_or_i32, 0x0E);
79aceca5 3663/* crorc */
e1571908 3664GEN_CRLOGIC(crorc, tcg_gen_orc_i32, 0x0D);
79aceca5 3665/* crxor */
e1571908 3666GEN_CRLOGIC(crxor, tcg_gen_xor_i32, 0x06);
99e300ef 3667
54623277 3668/* mcrf */
99e300ef 3669static void gen_mcrf(DisasContext *ctx)
79aceca5 3670{
47e4661c 3671 tcg_gen_mov_i32(cpu_crf[crfD(ctx->opcode)], cpu_crf[crfS(ctx->opcode)]);
79aceca5
FB
3672}
3673
3674/*** System linkage ***/
99e300ef 3675
54623277 3676/* rfi (mem_idx only) */
99e300ef 3677static void gen_rfi(DisasContext *ctx)
79aceca5 3678{
9a64fbe4 3679#if defined(CONFIG_USER_ONLY)
e06fcd75 3680 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
9a64fbe4
FB
3681#else
3682 /* Restore CPU state */
76db3ba4 3683 if (unlikely(!ctx->mem_idx)) {
e06fcd75 3684 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
9fddaa0c 3685 return;
9a64fbe4 3686 }
697ab892 3687 gen_update_cfar(ctx, ctx->nip);
e5f17ac6 3688 gen_helper_rfi(cpu_env);
e06fcd75 3689 gen_sync_exception(ctx);
9a64fbe4 3690#endif
79aceca5
FB
3691}
3692
426613db 3693#if defined(TARGET_PPC64)
99e300ef 3694static void gen_rfid(DisasContext *ctx)
426613db
JM
3695{
3696#if defined(CONFIG_USER_ONLY)
e06fcd75 3697 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
426613db
JM
3698#else
3699 /* Restore CPU state */
76db3ba4 3700 if (unlikely(!ctx->mem_idx)) {
e06fcd75 3701 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
426613db
JM
3702 return;
3703 }
697ab892 3704 gen_update_cfar(ctx, ctx->nip);
e5f17ac6 3705 gen_helper_rfid(cpu_env);
e06fcd75 3706 gen_sync_exception(ctx);
426613db
JM
3707#endif
3708}
426613db 3709
99e300ef 3710static void gen_hrfid(DisasContext *ctx)
be147d08
JM
3711{
3712#if defined(CONFIG_USER_ONLY)
e06fcd75 3713 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
be147d08
JM
3714#else
3715 /* Restore CPU state */
76db3ba4 3716 if (unlikely(ctx->mem_idx <= 1)) {
e06fcd75 3717 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
be147d08
JM
3718 return;
3719 }
e5f17ac6 3720 gen_helper_hrfid(cpu_env);
e06fcd75 3721 gen_sync_exception(ctx);
be147d08
JM
3722#endif
3723}
3724#endif
3725
79aceca5 3726/* sc */
417bf010
JM
3727#if defined(CONFIG_USER_ONLY)
3728#define POWERPC_SYSCALL POWERPC_EXCP_SYSCALL_USER
3729#else
3730#define POWERPC_SYSCALL POWERPC_EXCP_SYSCALL
3731#endif
99e300ef 3732static void gen_sc(DisasContext *ctx)
79aceca5 3733{
e1833e1f
JM
3734 uint32_t lev;
3735
3736 lev = (ctx->opcode >> 5) & 0x7F;
e06fcd75 3737 gen_exception_err(ctx, POWERPC_SYSCALL, lev);
79aceca5
FB
3738}
3739
3740/*** Trap ***/
99e300ef 3741
54623277 3742/* tw */
99e300ef 3743static void gen_tw(DisasContext *ctx)
79aceca5 3744{
cab3bee2 3745 TCGv_i32 t0 = tcg_const_i32(TO(ctx->opcode));
db9a231d
AJ
3746 /* Update the nip since this might generate a trap exception */
3747 gen_update_nip(ctx, ctx->nip);
e5f17ac6
BS
3748 gen_helper_tw(cpu_env, cpu_gpr[rA(ctx->opcode)], cpu_gpr[rB(ctx->opcode)],
3749 t0);
cab3bee2 3750 tcg_temp_free_i32(t0);
79aceca5
FB
3751}
3752
3753/* twi */
99e300ef 3754static void gen_twi(DisasContext *ctx)
79aceca5 3755{
cab3bee2
AJ
3756 TCGv t0 = tcg_const_tl(SIMM(ctx->opcode));
3757 TCGv_i32 t1 = tcg_const_i32(TO(ctx->opcode));
db9a231d
AJ
3758 /* Update the nip since this might generate a trap exception */
3759 gen_update_nip(ctx, ctx->nip);
e5f17ac6 3760 gen_helper_tw(cpu_env, cpu_gpr[rA(ctx->opcode)], t0, t1);
cab3bee2
AJ
3761 tcg_temp_free(t0);
3762 tcg_temp_free_i32(t1);
79aceca5
FB
3763}
3764
d9bce9d9
JM
3765#if defined(TARGET_PPC64)
3766/* td */
99e300ef 3767static void gen_td(DisasContext *ctx)
d9bce9d9 3768{
cab3bee2 3769 TCGv_i32 t0 = tcg_const_i32(TO(ctx->opcode));
db9a231d
AJ
3770 /* Update the nip since this might generate a trap exception */
3771 gen_update_nip(ctx, ctx->nip);
e5f17ac6
BS
3772 gen_helper_td(cpu_env, cpu_gpr[rA(ctx->opcode)], cpu_gpr[rB(ctx->opcode)],
3773 t0);
cab3bee2 3774 tcg_temp_free_i32(t0);
d9bce9d9
JM
3775}
3776
3777/* tdi */
99e300ef 3778static void gen_tdi(DisasContext *ctx)
d9bce9d9 3779{
cab3bee2
AJ
3780 TCGv t0 = tcg_const_tl(SIMM(ctx->opcode));
3781 TCGv_i32 t1 = tcg_const_i32(TO(ctx->opcode));
db9a231d
AJ
3782 /* Update the nip since this might generate a trap exception */
3783 gen_update_nip(ctx, ctx->nip);
e5f17ac6 3784 gen_helper_td(cpu_env, cpu_gpr[rA(ctx->opcode)], t0, t1);
cab3bee2
AJ
3785 tcg_temp_free(t0);
3786 tcg_temp_free_i32(t1);
d9bce9d9
JM
3787}
3788#endif
3789
79aceca5 3790/*** Processor control ***/
99e300ef 3791
54623277 3792/* mcrxr */
99e300ef 3793static void gen_mcrxr(DisasContext *ctx)
79aceca5 3794{
3d7b417e
AJ
3795 tcg_gen_trunc_tl_i32(cpu_crf[crfD(ctx->opcode)], cpu_xer);
3796 tcg_gen_shri_i32(cpu_crf[crfD(ctx->opcode)], cpu_crf[crfD(ctx->opcode)], XER_CA);
269f3e95 3797 tcg_gen_andi_tl(cpu_xer, cpu_xer, ~(1 << XER_SO | 1 << XER_OV | 1 << XER_CA));
79aceca5
FB
3798}
3799
0cfe11ea 3800/* mfcr mfocrf */
99e300ef 3801static void gen_mfcr(DisasContext *ctx)
79aceca5 3802{
76a66253 3803 uint32_t crm, crn;
3b46e624 3804
76a66253
JM
3805 if (likely(ctx->opcode & 0x00100000)) {
3806 crm = CRM(ctx->opcode);
8dd640e4 3807 if (likely(crm && ((crm & (crm - 1)) == 0))) {
0cfe11ea 3808 crn = ctz32 (crm);
e1571908 3809 tcg_gen_extu_i32_tl(cpu_gpr[rD(ctx->opcode)], cpu_crf[7 - crn]);
0497d2f4
AJ
3810 tcg_gen_shli_tl(cpu_gpr[rD(ctx->opcode)],
3811 cpu_gpr[rD(ctx->opcode)], crn * 4);
76a66253 3812 }
d9bce9d9 3813 } else {
651721b2
AJ
3814 TCGv_i32 t0 = tcg_temp_new_i32();
3815 tcg_gen_mov_i32(t0, cpu_crf[0]);
3816 tcg_gen_shli_i32(t0, t0, 4);
3817 tcg_gen_or_i32(t0, t0, cpu_crf[1]);
3818 tcg_gen_shli_i32(t0, t0, 4);
3819 tcg_gen_or_i32(t0, t0, cpu_crf[2]);
3820 tcg_gen_shli_i32(t0, t0, 4);
3821 tcg_gen_or_i32(t0, t0, cpu_crf[3]);
3822 tcg_gen_shli_i32(t0, t0, 4);
3823 tcg_gen_or_i32(t0, t0, cpu_crf[4]);
3824 tcg_gen_shli_i32(t0, t0, 4);
3825 tcg_gen_or_i32(t0, t0, cpu_crf[5]);
3826 tcg_gen_shli_i32(t0, t0, 4);
3827 tcg_gen_or_i32(t0, t0, cpu_crf[6]);
3828 tcg_gen_shli_i32(t0, t0, 4);
3829 tcg_gen_or_i32(t0, t0, cpu_crf[7]);
3830 tcg_gen_extu_i32_tl(cpu_gpr[rD(ctx->opcode)], t0);
3831 tcg_temp_free_i32(t0);
d9bce9d9 3832 }
79aceca5
FB
3833}
3834
3835/* mfmsr */
99e300ef 3836static void gen_mfmsr(DisasContext *ctx)
79aceca5 3837{
9a64fbe4 3838#if defined(CONFIG_USER_ONLY)
e06fcd75 3839 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
9a64fbe4 3840#else
76db3ba4 3841 if (unlikely(!ctx->mem_idx)) {
e06fcd75 3842 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
9fddaa0c 3843 return;
9a64fbe4 3844 }
6527f6ea 3845 tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_msr);
9a64fbe4 3846#endif
79aceca5
FB
3847}
3848
7b13448f 3849static void spr_noaccess(void *opaque, int gprn, int sprn)
3fc6c082 3850{
7b13448f 3851#if 0
3fc6c082
FB
3852 sprn = ((sprn >> 5) & 0x1F) | ((sprn & 0x1F) << 5);
3853 printf("ERROR: try to access SPR %d !\n", sprn);
7b13448f 3854#endif
3fc6c082
FB
3855}
3856#define SPR_NOACCESS (&spr_noaccess)
3fc6c082 3857
79aceca5 3858/* mfspr */
636aa200 3859static inline void gen_op_mfspr(DisasContext *ctx)
79aceca5 3860{
45d827d2 3861 void (*read_cb)(void *opaque, int gprn, int sprn);
79aceca5
FB
3862 uint32_t sprn = SPR(ctx->opcode);
3863
3fc6c082 3864#if !defined(CONFIG_USER_ONLY)
76db3ba4 3865 if (ctx->mem_idx == 2)
be147d08 3866 read_cb = ctx->spr_cb[sprn].hea_read;
76db3ba4 3867 else if (ctx->mem_idx)
3fc6c082
FB
3868 read_cb = ctx->spr_cb[sprn].oea_read;
3869 else
9a64fbe4 3870#endif
3fc6c082 3871 read_cb = ctx->spr_cb[sprn].uea_read;
76a66253
JM
3872 if (likely(read_cb != NULL)) {
3873 if (likely(read_cb != SPR_NOACCESS)) {
45d827d2 3874 (*read_cb)(ctx, rD(ctx->opcode), sprn);
3fc6c082
FB
3875 } else {
3876 /* Privilege exception */
9fceefa7
JM
3877 /* This is a hack to avoid warnings when running Linux:
3878 * this OS breaks the PowerPC virtualisation model,
3879 * allowing userland application to read the PVR
3880 */
3881 if (sprn != SPR_PVR) {
93fcfe39 3882 qemu_log("Trying to read privileged spr %d %03x at "
90e189ec
BS
3883 TARGET_FMT_lx "\n", sprn, sprn, ctx->nip);
3884 printf("Trying to read privileged spr %d %03x at "
3885 TARGET_FMT_lx "\n", sprn, sprn, ctx->nip);
f24e5695 3886 }
e06fcd75 3887 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
79aceca5 3888 }
3fc6c082
FB
3889 } else {
3890 /* Not defined */
93fcfe39 3891 qemu_log("Trying to read invalid spr %d %03x at "
90e189ec
BS
3892 TARGET_FMT_lx "\n", sprn, sprn, ctx->nip);
3893 printf("Trying to read invalid spr %d %03x at " TARGET_FMT_lx "\n",
077fc206 3894 sprn, sprn, ctx->nip);
e06fcd75 3895 gen_inval_exception(ctx, POWERPC_EXCP_INVAL_SPR);
79aceca5 3896 }
79aceca5
FB
3897}
3898
99e300ef 3899static void gen_mfspr(DisasContext *ctx)
79aceca5 3900{
3fc6c082 3901 gen_op_mfspr(ctx);
76a66253 3902}
3fc6c082
FB
3903
3904/* mftb */
99e300ef 3905static void gen_mftb(DisasContext *ctx)
3fc6c082
FB
3906{
3907 gen_op_mfspr(ctx);
79aceca5
FB
3908}
3909
0cfe11ea 3910/* mtcrf mtocrf*/
99e300ef 3911static void gen_mtcrf(DisasContext *ctx)
79aceca5 3912{
76a66253 3913 uint32_t crm, crn;
3b46e624 3914
76a66253 3915 crm = CRM(ctx->opcode);
8dd640e4 3916 if (likely((ctx->opcode & 0x00100000))) {
3917 if (crm && ((crm & (crm - 1)) == 0)) {
3918 TCGv_i32 temp = tcg_temp_new_i32();
0cfe11ea 3919 crn = ctz32 (crm);
8dd640e4 3920 tcg_gen_trunc_tl_i32(temp, cpu_gpr[rS(ctx->opcode)]);
0cfe11ea
AJ
3921 tcg_gen_shri_i32(temp, temp, crn * 4);
3922 tcg_gen_andi_i32(cpu_crf[7 - crn], temp, 0xf);
8dd640e4 3923 tcg_temp_free_i32(temp);
3924 }
76a66253 3925 } else {
651721b2
AJ
3926 TCGv_i32 temp = tcg_temp_new_i32();
3927 tcg_gen_trunc_tl_i32(temp, cpu_gpr[rS(ctx->opcode)]);
3928 for (crn = 0 ; crn < 8 ; crn++) {
3929 if (crm & (1 << crn)) {
3930 tcg_gen_shri_i32(cpu_crf[7 - crn], temp, crn * 4);
3931 tcg_gen_andi_i32(cpu_crf[7 - crn], cpu_crf[7 - crn], 0xf);
3932 }
3933 }
a7812ae4 3934 tcg_temp_free_i32(temp);
76a66253 3935 }
79aceca5
FB
3936}
3937
3938/* mtmsr */
426613db 3939#if defined(TARGET_PPC64)
99e300ef 3940static void gen_mtmsrd(DisasContext *ctx)
426613db
JM
3941{
3942#if defined(CONFIG_USER_ONLY)
e06fcd75 3943 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
426613db 3944#else
76db3ba4 3945 if (unlikely(!ctx->mem_idx)) {
e06fcd75 3946 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
426613db
JM
3947 return;
3948 }
be147d08
JM
3949 if (ctx->opcode & 0x00010000) {
3950 /* Special form that does not need any synchronisation */
6527f6ea
AJ
3951 TCGv t0 = tcg_temp_new();
3952 tcg_gen_andi_tl(t0, cpu_gpr[rS(ctx->opcode)], (1 << MSR_RI) | (1 << MSR_EE));
3953 tcg_gen_andi_tl(cpu_msr, cpu_msr, ~((1 << MSR_RI) | (1 << MSR_EE)));
3954 tcg_gen_or_tl(cpu_msr, cpu_msr, t0);
3955 tcg_temp_free(t0);
be147d08 3956 } else {
056b05f8
JM
3957 /* XXX: we need to update nip before the store
3958 * if we enter power saving mode, we will exit the loop
3959 * directly from ppc_store_msr
3960 */
be147d08 3961 gen_update_nip(ctx, ctx->nip);
e5f17ac6 3962 gen_helper_store_msr(cpu_env, cpu_gpr[rS(ctx->opcode)]);
be147d08
JM
3963 /* Must stop the translation as machine state (may have) changed */
3964 /* Note that mtmsr is not always defined as context-synchronizing */
e06fcd75 3965 gen_stop_exception(ctx);
be147d08 3966 }
426613db
JM
3967#endif
3968}
3969#endif
3970
99e300ef 3971static void gen_mtmsr(DisasContext *ctx)
79aceca5 3972{
9a64fbe4 3973#if defined(CONFIG_USER_ONLY)
e06fcd75 3974 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
9a64fbe4 3975#else
76db3ba4 3976 if (unlikely(!ctx->mem_idx)) {
e06fcd75 3977 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
9fddaa0c 3978 return;
9a64fbe4 3979 }
be147d08
JM
3980 if (ctx->opcode & 0x00010000) {
3981 /* Special form that does not need any synchronisation */
6527f6ea
AJ
3982 TCGv t0 = tcg_temp_new();
3983 tcg_gen_andi_tl(t0, cpu_gpr[rS(ctx->opcode)], (1 << MSR_RI) | (1 << MSR_EE));
3984 tcg_gen_andi_tl(cpu_msr, cpu_msr, ~((1 << MSR_RI) | (1 << MSR_EE)));
3985 tcg_gen_or_tl(cpu_msr, cpu_msr, t0);
3986 tcg_temp_free(t0);
be147d08 3987 } else {
8018dc63
AG
3988 TCGv msr = tcg_temp_new();
3989
056b05f8
JM
3990 /* XXX: we need to update nip before the store
3991 * if we enter power saving mode, we will exit the loop
3992 * directly from ppc_store_msr
3993 */
be147d08 3994 gen_update_nip(ctx, ctx->nip);
d9bce9d9 3995#if defined(TARGET_PPC64)
8018dc63
AG
3996 tcg_gen_deposit_tl(msr, cpu_msr, cpu_gpr[rS(ctx->opcode)], 0, 32);
3997#else
3998 tcg_gen_mov_tl(msr, cpu_gpr[rS(ctx->opcode)]);
d9bce9d9 3999#endif
e5f17ac6 4000 gen_helper_store_msr(cpu_env, msr);
be147d08 4001 /* Must stop the translation as machine state (may have) changed */
6527f6ea 4002 /* Note that mtmsr is not always defined as context-synchronizing */
e06fcd75 4003 gen_stop_exception(ctx);
be147d08 4004 }
9a64fbe4 4005#endif
79aceca5
FB
4006}
4007
4008/* mtspr */
99e300ef 4009static void gen_mtspr(DisasContext *ctx)
79aceca5 4010{
45d827d2 4011 void (*write_cb)(void *opaque, int sprn, int gprn);
79aceca5
FB
4012 uint32_t sprn = SPR(ctx->opcode);
4013
3fc6c082 4014#if !defined(CONFIG_USER_ONLY)
76db3ba4 4015 if (ctx->mem_idx == 2)
be147d08 4016 write_cb = ctx->spr_cb[sprn].hea_write;
76db3ba4 4017 else if (ctx->mem_idx)
3fc6c082
FB
4018 write_cb = ctx->spr_cb[sprn].oea_write;
4019 else
9a64fbe4 4020#endif
3fc6c082 4021 write_cb = ctx->spr_cb[sprn].uea_write;
76a66253
JM
4022 if (likely(write_cb != NULL)) {
4023 if (likely(write_cb != SPR_NOACCESS)) {
45d827d2 4024 (*write_cb)(ctx, sprn, rS(ctx->opcode));
3fc6c082
FB
4025 } else {
4026 /* Privilege exception */
93fcfe39 4027 qemu_log("Trying to write privileged spr %d %03x at "
90e189ec
BS
4028 TARGET_FMT_lx "\n", sprn, sprn, ctx->nip);
4029 printf("Trying to write privileged spr %d %03x at " TARGET_FMT_lx
4030 "\n", sprn, sprn, ctx->nip);
e06fcd75 4031 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
76a66253 4032 }
3fc6c082
FB
4033 } else {
4034 /* Not defined */
93fcfe39 4035 qemu_log("Trying to write invalid spr %d %03x at "
90e189ec
BS
4036 TARGET_FMT_lx "\n", sprn, sprn, ctx->nip);
4037 printf("Trying to write invalid spr %d %03x at " TARGET_FMT_lx "\n",
077fc206 4038 sprn, sprn, ctx->nip);
e06fcd75 4039 gen_inval_exception(ctx, POWERPC_EXCP_INVAL_SPR);
79aceca5 4040 }
79aceca5
FB
4041}
4042
4043/*** Cache management ***/
99e300ef 4044
54623277 4045/* dcbf */
99e300ef 4046static void gen_dcbf(DisasContext *ctx)
79aceca5 4047{
dac454af 4048 /* XXX: specification says this is treated as a load by the MMU */
76db3ba4
AJ
4049 TCGv t0;
4050 gen_set_access_type(ctx, ACCESS_CACHE);
4051 t0 = tcg_temp_new();
4052 gen_addr_reg_index(ctx, t0);
4053 gen_qemu_ld8u(ctx, t0, t0);
fea0c503 4054 tcg_temp_free(t0);
79aceca5
FB
4055}
4056
4057/* dcbi (Supervisor only) */
99e300ef 4058static void gen_dcbi(DisasContext *ctx)
79aceca5 4059{
a541f297 4060#if defined(CONFIG_USER_ONLY)
e06fcd75 4061 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
a541f297 4062#else
b61f2753 4063 TCGv EA, val;
76db3ba4 4064 if (unlikely(!ctx->mem_idx)) {
e06fcd75 4065 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
9fddaa0c 4066 return;
9a64fbe4 4067 }
a7812ae4 4068 EA = tcg_temp_new();
76db3ba4
AJ
4069 gen_set_access_type(ctx, ACCESS_CACHE);
4070 gen_addr_reg_index(ctx, EA);
a7812ae4 4071 val = tcg_temp_new();
76a66253 4072 /* XXX: specification says this should be treated as a store by the MMU */
76db3ba4
AJ
4073 gen_qemu_ld8u(ctx, val, EA);
4074 gen_qemu_st8(ctx, val, EA);
b61f2753
AJ
4075 tcg_temp_free(val);
4076 tcg_temp_free(EA);
a541f297 4077#endif
79aceca5
FB
4078}
4079
4080/* dcdst */
99e300ef 4081static void gen_dcbst(DisasContext *ctx)
79aceca5 4082{
76a66253 4083 /* XXX: specification say this is treated as a load by the MMU */
76db3ba4
AJ
4084 TCGv t0;
4085 gen_set_access_type(ctx, ACCESS_CACHE);
4086 t0 = tcg_temp_new();
4087 gen_addr_reg_index(ctx, t0);
4088 gen_qemu_ld8u(ctx, t0, t0);
fea0c503 4089 tcg_temp_free(t0);
79aceca5
FB
4090}
4091
4092/* dcbt */
99e300ef 4093static void gen_dcbt(DisasContext *ctx)
79aceca5 4094{
0db1b20e 4095 /* interpreted as no-op */
76a66253
JM
4096 /* XXX: specification say this is treated as a load by the MMU
4097 * but does not generate any exception
4098 */
79aceca5
FB
4099}
4100
4101/* dcbtst */
99e300ef 4102static void gen_dcbtst(DisasContext *ctx)
79aceca5 4103{
0db1b20e 4104 /* interpreted as no-op */
76a66253
JM
4105 /* XXX: specification say this is treated as a load by the MMU
4106 * but does not generate any exception
4107 */
79aceca5
FB
4108}
4109
4110/* dcbz */
99e300ef 4111static void gen_dcbz(DisasContext *ctx)
79aceca5 4112{
76db3ba4
AJ
4113 TCGv t0;
4114 gen_set_access_type(ctx, ACCESS_CACHE);
799a8c8d
AJ
4115 /* NIP cannot be restored if the memory exception comes from an helper */
4116 gen_update_nip(ctx, ctx->nip - 4);
76db3ba4
AJ
4117 t0 = tcg_temp_new();
4118 gen_addr_reg_index(ctx, t0);
799a8c8d
AJ
4119 gen_helper_dcbz(t0);
4120 tcg_temp_free(t0);
d63001d1
JM
4121}
4122
e8eaa2c0 4123static void gen_dcbz_970(DisasContext *ctx)
d63001d1 4124{
76db3ba4
AJ
4125 TCGv t0;
4126 gen_set_access_type(ctx, ACCESS_CACHE);
799a8c8d
AJ
4127 /* NIP cannot be restored if the memory exception comes from an helper */
4128 gen_update_nip(ctx, ctx->nip - 4);
76db3ba4
AJ
4129 t0 = tcg_temp_new();
4130 gen_addr_reg_index(ctx, t0);
d63001d1 4131 if (ctx->opcode & 0x00200000)
799a8c8d 4132 gen_helper_dcbz(t0);
d63001d1 4133 else
799a8c8d
AJ
4134 gen_helper_dcbz_970(t0);
4135 tcg_temp_free(t0);
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);
37d269df
AJ
4174 gen_helper_icbi(t0);
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
AJ
4553 TCGv_i32 t0 = tcg_const_i32(rA(ctx->opcode));
4554 gen_helper_clcs(cpu_gpr[rD(ctx->opcode)], t0);
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);
bdb4b689
AJ
4666 gen_helper_lscbx(t0, t0, t1, t2, t3);
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));
5720 gen_helper_load_dcr(cpu_gpr[rD(ctx->opcode)], dcrn);
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));
5739 gen_helper_store_dcr(dcrn, cpu_gpr[rS(ctx->opcode)]);
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);
5757 gen_helper_load_dcr(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)]);
a750fc0b 5758 /* Note: Rc update flag set leads to undefined state of Rc0 */
a42bd6cc
JM
5759#endif
5760}
5761
5762/* mtdcrx */
2662a059 5763/* XXX: not implemented on 440 ? */
99e300ef 5764static void gen_mtdcrx(DisasContext *ctx)
a42bd6cc
JM
5765{
5766#if defined(CONFIG_USER_ONLY)
e06fcd75 5767 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
a42bd6cc 5768#else
76db3ba4 5769 if (unlikely(!ctx->mem_idx)) {
e06fcd75 5770 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
a42bd6cc
JM
5771 return;
5772 }
06dca6a7
AJ
5773 /* NIP cannot be restored if the memory exception comes from an helper */
5774 gen_update_nip(ctx, ctx->nip - 4);
5775 gen_helper_store_dcr(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)]);
a750fc0b 5776 /* Note: Rc update flag set leads to undefined state of Rc0 */
76a66253
JM
5777#endif
5778}
5779
a750fc0b 5780/* mfdcrux (PPC 460) : user-mode access to DCR */
99e300ef 5781static void gen_mfdcrux(DisasContext *ctx)
a750fc0b 5782{
06dca6a7
AJ
5783 /* NIP cannot be restored if the memory exception comes from an helper */
5784 gen_update_nip(ctx, ctx->nip - 4);
5785 gen_helper_load_dcr(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)]);
a750fc0b
JM
5786 /* Note: Rc update flag set leads to undefined state of Rc0 */
5787}
5788
5789/* mtdcrux (PPC 460) : user-mode access to DCR */
99e300ef 5790static void gen_mtdcrux(DisasContext *ctx)
a750fc0b 5791{
06dca6a7
AJ
5792 /* NIP cannot be restored if the memory exception comes from an helper */
5793 gen_update_nip(ctx, ctx->nip - 4);
5794 gen_helper_store_dcr(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)]);
a750fc0b
JM
5795 /* Note: Rc update flag set leads to undefined state of Rc0 */
5796}
5797
76a66253 5798/* dccci */
99e300ef 5799static void gen_dccci(DisasContext *ctx)
76a66253
JM
5800{
5801#if defined(CONFIG_USER_ONLY)
e06fcd75 5802 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
76a66253 5803#else
76db3ba4 5804 if (unlikely(!ctx->mem_idx)) {
e06fcd75 5805 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
76a66253
JM
5806 return;
5807 }
5808 /* interpreted as no-op */
5809#endif
5810}
5811
5812/* dcread */
99e300ef 5813static void gen_dcread(DisasContext *ctx)
76a66253
JM
5814{
5815#if defined(CONFIG_USER_ONLY)
e06fcd75 5816 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
76a66253 5817#else
b61f2753 5818 TCGv EA, val;
76db3ba4 5819 if (unlikely(!ctx->mem_idx)) {
e06fcd75 5820 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
76a66253
JM
5821 return;
5822 }
76db3ba4 5823 gen_set_access_type(ctx, ACCESS_CACHE);
a7812ae4 5824 EA = tcg_temp_new();
76db3ba4 5825 gen_addr_reg_index(ctx, EA);
a7812ae4 5826 val = tcg_temp_new();
76db3ba4 5827 gen_qemu_ld32u(ctx, val, EA);
b61f2753
AJ
5828 tcg_temp_free(val);
5829 tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], EA);
5830 tcg_temp_free(EA);
76a66253
JM
5831#endif
5832}
5833
5834/* icbt */
e8eaa2c0 5835static void gen_icbt_40x(DisasContext *ctx)
76a66253
JM
5836{
5837 /* interpreted as no-op */
5838 /* XXX: specification say this is treated as a load by the MMU
5839 * but does not generate any exception
5840 */
5841}
5842
5843/* iccci */
99e300ef 5844static void gen_iccci(DisasContext *ctx)
76a66253
JM
5845{
5846#if defined(CONFIG_USER_ONLY)
e06fcd75 5847 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
76a66253 5848#else
76db3ba4 5849 if (unlikely(!ctx->mem_idx)) {
e06fcd75 5850 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
76a66253
JM
5851 return;
5852 }
5853 /* interpreted as no-op */
5854#endif
5855}
5856
5857/* icread */
99e300ef 5858static void gen_icread(DisasContext *ctx)
76a66253
JM
5859{
5860#if defined(CONFIG_USER_ONLY)
e06fcd75 5861 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
76a66253 5862#else
76db3ba4 5863 if (unlikely(!ctx->mem_idx)) {
e06fcd75 5864 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
76a66253
JM
5865 return;
5866 }
5867 /* interpreted as no-op */
5868#endif
5869}
5870
76db3ba4 5871/* rfci (mem_idx only) */
e8eaa2c0 5872static void gen_rfci_40x(DisasContext *ctx)
a42bd6cc
JM
5873{
5874#if defined(CONFIG_USER_ONLY)
e06fcd75 5875 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
a42bd6cc 5876#else
76db3ba4 5877 if (unlikely(!ctx->mem_idx)) {
e06fcd75 5878 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
a42bd6cc
JM
5879 return;
5880 }
5881 /* Restore CPU state */
e5f17ac6 5882 gen_helper_40x_rfci(cpu_env);
e06fcd75 5883 gen_sync_exception(ctx);
a42bd6cc
JM
5884#endif
5885}
5886
99e300ef 5887static void gen_rfci(DisasContext *ctx)
a42bd6cc
JM
5888{
5889#if defined(CONFIG_USER_ONLY)
e06fcd75 5890 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
a42bd6cc 5891#else
76db3ba4 5892 if (unlikely(!ctx->mem_idx)) {
e06fcd75 5893 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
a42bd6cc
JM
5894 return;
5895 }
5896 /* Restore CPU state */
e5f17ac6 5897 gen_helper_rfci(cpu_env);
e06fcd75 5898 gen_sync_exception(ctx);
a42bd6cc
JM
5899#endif
5900}
5901
5902/* BookE specific */
99e300ef 5903
54623277 5904/* XXX: not implemented on 440 ? */
99e300ef 5905static void gen_rfdi(DisasContext *ctx)
76a66253
JM
5906{
5907#if defined(CONFIG_USER_ONLY)
e06fcd75 5908 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
76a66253 5909#else
76db3ba4 5910 if (unlikely(!ctx->mem_idx)) {
e06fcd75 5911 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
76a66253
JM
5912 return;
5913 }
5914 /* Restore CPU state */
e5f17ac6 5915 gen_helper_rfdi(cpu_env);
e06fcd75 5916 gen_sync_exception(ctx);
76a66253
JM
5917#endif
5918}
5919
2662a059 5920/* XXX: not implemented on 440 ? */
99e300ef 5921static void gen_rfmci(DisasContext *ctx)
a42bd6cc
JM
5922{
5923#if defined(CONFIG_USER_ONLY)
e06fcd75 5924 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
a42bd6cc 5925#else
76db3ba4 5926 if (unlikely(!ctx->mem_idx)) {
e06fcd75 5927 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
a42bd6cc
JM
5928 return;
5929 }
5930 /* Restore CPU state */
e5f17ac6 5931 gen_helper_rfmci(cpu_env);
e06fcd75 5932 gen_sync_exception(ctx);
a42bd6cc
JM
5933#endif
5934}
5eb7995e 5935
d9bce9d9 5936/* TLB management - PowerPC 405 implementation */
e8eaa2c0 5937
54623277 5938/* tlbre */
e8eaa2c0 5939static void gen_tlbre_40x(DisasContext *ctx)
76a66253
JM
5940{
5941#if defined(CONFIG_USER_ONLY)
e06fcd75 5942 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
76a66253 5943#else
76db3ba4 5944 if (unlikely(!ctx->mem_idx)) {
e06fcd75 5945 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
76a66253
JM
5946 return;
5947 }
5948 switch (rB(ctx->opcode)) {
5949 case 0:
c6c7cf05
BS
5950 gen_helper_4xx_tlbre_hi(cpu_gpr[rD(ctx->opcode)], cpu_env,
5951 cpu_gpr[rA(ctx->opcode)]);
76a66253
JM
5952 break;
5953 case 1:
c6c7cf05
BS
5954 gen_helper_4xx_tlbre_lo(cpu_gpr[rD(ctx->opcode)], cpu_env,
5955 cpu_gpr[rA(ctx->opcode)]);
76a66253
JM
5956 break;
5957 default:
e06fcd75 5958 gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL);
76a66253 5959 break;
9a64fbe4 5960 }
76a66253
JM
5961#endif
5962}
5963
d9bce9d9 5964/* tlbsx - tlbsx. */
e8eaa2c0 5965static void gen_tlbsx_40x(DisasContext *ctx)
76a66253
JM
5966{
5967#if defined(CONFIG_USER_ONLY)
e06fcd75 5968 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
76a66253 5969#else
74d37793 5970 TCGv t0;
76db3ba4 5971 if (unlikely(!ctx->mem_idx)) {
e06fcd75 5972 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
76a66253
JM
5973 return;
5974 }
74d37793 5975 t0 = tcg_temp_new();
76db3ba4 5976 gen_addr_reg_index(ctx, t0);
c6c7cf05 5977 gen_helper_4xx_tlbsx(cpu_gpr[rD(ctx->opcode)], cpu_env, t0);
74d37793
AJ
5978 tcg_temp_free(t0);
5979 if (Rc(ctx->opcode)) {
5980 int l1 = gen_new_label();
5981 tcg_gen_trunc_tl_i32(cpu_crf[0], cpu_xer);
5982 tcg_gen_shri_i32(cpu_crf[0], cpu_crf[0], XER_SO);
5983 tcg_gen_andi_i32(cpu_crf[0], cpu_crf[0], 1);
5984 tcg_gen_brcondi_tl(TCG_COND_EQ, cpu_gpr[rD(ctx->opcode)], -1, l1);
5985 tcg_gen_ori_i32(cpu_crf[0], cpu_crf[0], 0x02);
5986 gen_set_label(l1);
5987 }
76a66253 5988#endif
79aceca5
FB
5989}
5990
76a66253 5991/* tlbwe */
e8eaa2c0 5992static void gen_tlbwe_40x(DisasContext *ctx)
79aceca5 5993{
76a66253 5994#if defined(CONFIG_USER_ONLY)
e06fcd75 5995 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
76a66253 5996#else
76db3ba4 5997 if (unlikely(!ctx->mem_idx)) {
e06fcd75 5998 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
76a66253
JM
5999 return;
6000 }
6001 switch (rB(ctx->opcode)) {
6002 case 0:
c6c7cf05
BS
6003 gen_helper_4xx_tlbwe_hi(cpu_env, cpu_gpr[rA(ctx->opcode)],
6004 cpu_gpr[rS(ctx->opcode)]);
76a66253
JM
6005 break;
6006 case 1:
c6c7cf05
BS
6007 gen_helper_4xx_tlbwe_lo(cpu_env, cpu_gpr[rA(ctx->opcode)],
6008 cpu_gpr[rS(ctx->opcode)]);
76a66253
JM
6009 break;
6010 default:
e06fcd75 6011 gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL);
76a66253 6012 break;
9a64fbe4 6013 }
76a66253
JM
6014#endif
6015}
6016
a4bb6c3e 6017/* TLB management - PowerPC 440 implementation */
e8eaa2c0 6018
54623277 6019/* tlbre */
e8eaa2c0 6020static void gen_tlbre_440(DisasContext *ctx)
5eb7995e
JM
6021{
6022#if defined(CONFIG_USER_ONLY)
e06fcd75 6023 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
5eb7995e 6024#else
76db3ba4 6025 if (unlikely(!ctx->mem_idx)) {
e06fcd75 6026 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
5eb7995e
JM
6027 return;
6028 }
6029 switch (rB(ctx->opcode)) {
6030 case 0:
5eb7995e 6031 case 1:
5eb7995e 6032 case 2:
74d37793
AJ
6033 {
6034 TCGv_i32 t0 = tcg_const_i32(rB(ctx->opcode));
c6c7cf05
BS
6035 gen_helper_440_tlbre(cpu_gpr[rD(ctx->opcode)], cpu_env,
6036 t0, cpu_gpr[rA(ctx->opcode)]);
74d37793
AJ
6037 tcg_temp_free_i32(t0);
6038 }
5eb7995e
JM
6039 break;
6040 default:
e06fcd75 6041 gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL);
5eb7995e
JM
6042 break;
6043 }
6044#endif
6045}
6046
6047/* tlbsx - tlbsx. */
e8eaa2c0 6048static void gen_tlbsx_440(DisasContext *ctx)
5eb7995e
JM
6049{
6050#if defined(CONFIG_USER_ONLY)
e06fcd75 6051 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
5eb7995e 6052#else
74d37793 6053 TCGv t0;
76db3ba4 6054 if (unlikely(!ctx->mem_idx)) {
e06fcd75 6055 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
5eb7995e
JM
6056 return;
6057 }
74d37793 6058 t0 = tcg_temp_new();
76db3ba4 6059 gen_addr_reg_index(ctx, t0);
c6c7cf05 6060 gen_helper_440_tlbsx(cpu_gpr[rD(ctx->opcode)], cpu_env, t0);
74d37793
AJ
6061 tcg_temp_free(t0);
6062 if (Rc(ctx->opcode)) {
6063 int l1 = gen_new_label();
6064 tcg_gen_trunc_tl_i32(cpu_crf[0], cpu_xer);
6065 tcg_gen_shri_i32(cpu_crf[0], cpu_crf[0], XER_SO);
6066 tcg_gen_andi_i32(cpu_crf[0], cpu_crf[0], 1);
6067 tcg_gen_brcondi_tl(TCG_COND_EQ, cpu_gpr[rD(ctx->opcode)], -1, l1);
6068 tcg_gen_ori_i32(cpu_crf[0], cpu_crf[0], 0x02);
6069 gen_set_label(l1);
6070 }
5eb7995e
JM
6071#endif
6072}
6073
6074/* tlbwe */
e8eaa2c0 6075static void gen_tlbwe_440(DisasContext *ctx)
5eb7995e
JM
6076{
6077#if defined(CONFIG_USER_ONLY)
e06fcd75 6078 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
5eb7995e 6079#else
76db3ba4 6080 if (unlikely(!ctx->mem_idx)) {
e06fcd75 6081 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
5eb7995e
JM
6082 return;
6083 }
6084 switch (rB(ctx->opcode)) {
6085 case 0:
5eb7995e 6086 case 1:
5eb7995e 6087 case 2:
74d37793
AJ
6088 {
6089 TCGv_i32 t0 = tcg_const_i32(rB(ctx->opcode));
c6c7cf05
BS
6090 gen_helper_440_tlbwe(cpu_env, t0, cpu_gpr[rA(ctx->opcode)],
6091 cpu_gpr[rS(ctx->opcode)]);
74d37793
AJ
6092 tcg_temp_free_i32(t0);
6093 }
5eb7995e
JM
6094 break;
6095 default:
e06fcd75 6096 gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL);
5eb7995e
JM
6097 break;
6098 }
6099#endif
6100}
6101
01662f3e
AG
6102/* TLB management - PowerPC BookE 2.06 implementation */
6103
6104/* tlbre */
6105static void gen_tlbre_booke206(DisasContext *ctx)
6106{
6107#if defined(CONFIG_USER_ONLY)
6108 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
6109#else
6110 if (unlikely(!ctx->mem_idx)) {
6111 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
6112 return;
6113 }
6114
c6c7cf05 6115 gen_helper_booke206_tlbre(cpu_env);
01662f3e
AG
6116#endif
6117}
6118
6119/* tlbsx - tlbsx. */
6120static void gen_tlbsx_booke206(DisasContext *ctx)
6121{
6122#if defined(CONFIG_USER_ONLY)
6123 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
6124#else
6125 TCGv t0;
6126 if (unlikely(!ctx->mem_idx)) {
6127 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
6128 return;
6129 }
6130
6131 if (rA(ctx->opcode)) {
6132 t0 = tcg_temp_new();
6133 tcg_gen_mov_tl(t0, cpu_gpr[rD(ctx->opcode)]);
6134 } else {
6135 t0 = tcg_const_tl(0);
6136 }
6137
6138 tcg_gen_add_tl(t0, t0, cpu_gpr[rB(ctx->opcode)]);
c6c7cf05 6139 gen_helper_booke206_tlbsx(cpu_env, t0);
01662f3e
AG
6140#endif
6141}
6142
6143/* tlbwe */
6144static void gen_tlbwe_booke206(DisasContext *ctx)
6145{
6146#if defined(CONFIG_USER_ONLY)
6147 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
6148#else
6149 if (unlikely(!ctx->mem_idx)) {
6150 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
6151 return;
6152 }
3f162d11 6153 gen_update_nip(ctx, ctx->nip - 4);
c6c7cf05 6154 gen_helper_booke206_tlbwe(cpu_env);
01662f3e
AG
6155#endif
6156}
6157
6158static void gen_tlbivax_booke206(DisasContext *ctx)
6159{
6160#if defined(CONFIG_USER_ONLY)
6161 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
6162#else
6163 TCGv t0;
6164 if (unlikely(!ctx->mem_idx)) {
6165 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
6166 return;
6167 }
6168
6169 t0 = tcg_temp_new();
6170 gen_addr_reg_index(ctx, t0);
6171
c6c7cf05 6172 gen_helper_booke206_tlbivax(cpu_env, t0);
01662f3e
AG
6173#endif
6174}
6175
6d3db821
AG
6176static void gen_tlbilx_booke206(DisasContext *ctx)
6177{
6178#if defined(CONFIG_USER_ONLY)
6179 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
6180#else
6181 TCGv t0;
6182 if (unlikely(!ctx->mem_idx)) {
6183 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
6184 return;
6185 }
6186
6187 t0 = tcg_temp_new();
6188 gen_addr_reg_index(ctx, t0);
6189
6190 switch((ctx->opcode >> 21) & 0x3) {
6191 case 0:
c6c7cf05 6192 gen_helper_booke206_tlbilx0(cpu_env, t0);
6d3db821
AG
6193 break;
6194 case 1:
c6c7cf05 6195 gen_helper_booke206_tlbilx1(cpu_env, t0);
6d3db821
AG
6196 break;
6197 case 3:
c6c7cf05 6198 gen_helper_booke206_tlbilx3(cpu_env, t0);
6d3db821
AG
6199 break;
6200 default:
6201 gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL);
6202 break;
6203 }
6204
6205 tcg_temp_free(t0);
6206#endif
6207}
6208
01662f3e 6209
76a66253 6210/* wrtee */
99e300ef 6211static void gen_wrtee(DisasContext *ctx)
76a66253
JM
6212{
6213#if defined(CONFIG_USER_ONLY)
e06fcd75 6214 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
76a66253 6215#else
6527f6ea 6216 TCGv t0;
76db3ba4 6217 if (unlikely(!ctx->mem_idx)) {
e06fcd75 6218 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
76a66253
JM
6219 return;
6220 }
6527f6ea
AJ
6221 t0 = tcg_temp_new();
6222 tcg_gen_andi_tl(t0, cpu_gpr[rD(ctx->opcode)], (1 << MSR_EE));
6223 tcg_gen_andi_tl(cpu_msr, cpu_msr, ~(1 << MSR_EE));
6224 tcg_gen_or_tl(cpu_msr, cpu_msr, t0);
6225 tcg_temp_free(t0);
dee96f6c
JM
6226 /* Stop translation to have a chance to raise an exception
6227 * if we just set msr_ee to 1
6228 */
e06fcd75 6229 gen_stop_exception(ctx);
76a66253
JM
6230#endif
6231}
6232
6233/* wrteei */
99e300ef 6234static void gen_wrteei(DisasContext *ctx)
76a66253
JM
6235{
6236#if defined(CONFIG_USER_ONLY)
e06fcd75 6237 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
76a66253 6238#else
76db3ba4 6239 if (unlikely(!ctx->mem_idx)) {
e06fcd75 6240 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
76a66253
JM
6241 return;
6242 }
fbe73008 6243 if (ctx->opcode & 0x00008000) {
6527f6ea
AJ
6244 tcg_gen_ori_tl(cpu_msr, cpu_msr, (1 << MSR_EE));
6245 /* Stop translation to have a chance to raise an exception */
e06fcd75 6246 gen_stop_exception(ctx);
6527f6ea 6247 } else {
1b6e5f99 6248 tcg_gen_andi_tl(cpu_msr, cpu_msr, ~(1 << MSR_EE));
6527f6ea 6249 }
76a66253
JM
6250#endif
6251}
6252
08e46e54 6253/* PowerPC 440 specific instructions */
99e300ef 6254
54623277 6255/* dlmzb */
99e300ef 6256static void gen_dlmzb(DisasContext *ctx)
76a66253 6257{
ef0d51af 6258 TCGv_i32 t0 = tcg_const_i32(Rc(ctx->opcode));
d15f74fb
BS
6259 gen_helper_dlmzb(cpu_gpr[rA(ctx->opcode)], cpu_env,
6260 cpu_gpr[rS(ctx->opcode)], cpu_gpr[rB(ctx->opcode)], t0);
ef0d51af 6261 tcg_temp_free_i32(t0);
76a66253
JM
6262}
6263
6264/* mbar replaces eieio on 440 */
99e300ef 6265static void gen_mbar(DisasContext *ctx)
76a66253
JM
6266{
6267 /* interpreted as no-op */
6268}
6269
6270/* msync replaces sync on 440 */
dcb2b9e1 6271static void gen_msync_4xx(DisasContext *ctx)
76a66253
JM
6272{
6273 /* interpreted as no-op */
6274}
6275
6276/* icbt */
e8eaa2c0 6277static void gen_icbt_440(DisasContext *ctx)
76a66253
JM
6278{
6279 /* interpreted as no-op */
6280 /* XXX: specification say this is treated as a load by the MMU
6281 * but does not generate any exception
6282 */
79aceca5
FB
6283}
6284
9e0b5cb1
AG
6285/* Embedded.Processor Control */
6286
6287static void gen_msgclr(DisasContext *ctx)
6288{
6289#if defined(CONFIG_USER_ONLY)
6290 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
6291#else
6292 if (unlikely(ctx->mem_idx == 0)) {
6293 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
6294 return;
6295 }
6296
e5f17ac6 6297 gen_helper_msgclr(cpu_env, cpu_gpr[rB(ctx->opcode)]);
9e0b5cb1
AG
6298#endif
6299}
6300
d5d11a39
AG
6301static void gen_msgsnd(DisasContext *ctx)
6302{
6303#if defined(CONFIG_USER_ONLY)
6304 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
6305#else
6306 if (unlikely(ctx->mem_idx == 0)) {
6307 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
6308 return;
6309 }
6310
6311 gen_helper_msgsnd(cpu_gpr[rB(ctx->opcode)]);
6312#endif
6313}
6314
a9d9eb8f
JM
6315/*** Altivec vector extension ***/
6316/* Altivec registers moves */
a9d9eb8f 6317
636aa200 6318static inline TCGv_ptr gen_avr_ptr(int reg)
564e571a 6319{
e4704b3b 6320 TCGv_ptr r = tcg_temp_new_ptr();
564e571a
AJ
6321 tcg_gen_addi_ptr(r, cpu_env, offsetof(CPUPPCState, avr[reg]));
6322 return r;
6323}
6324
a9d9eb8f 6325#define GEN_VR_LDX(name, opc2, opc3) \
99e300ef 6326static void glue(gen_, name)(DisasContext *ctx) \
a9d9eb8f 6327{ \
fe1e5c53 6328 TCGv EA; \
a9d9eb8f 6329 if (unlikely(!ctx->altivec_enabled)) { \
e06fcd75 6330 gen_exception(ctx, POWERPC_EXCP_VPU); \
a9d9eb8f
JM
6331 return; \
6332 } \
76db3ba4 6333 gen_set_access_type(ctx, ACCESS_INT); \
fe1e5c53 6334 EA = tcg_temp_new(); \
76db3ba4 6335 gen_addr_reg_index(ctx, EA); \
fe1e5c53 6336 tcg_gen_andi_tl(EA, EA, ~0xf); \
76db3ba4
AJ
6337 if (ctx->le_mode) { \
6338 gen_qemu_ld64(ctx, cpu_avrl[rD(ctx->opcode)], EA); \
fe1e5c53 6339 tcg_gen_addi_tl(EA, EA, 8); \
76db3ba4 6340 gen_qemu_ld64(ctx, cpu_avrh[rD(ctx->opcode)], EA); \
fe1e5c53 6341 } else { \
76db3ba4 6342 gen_qemu_ld64(ctx, cpu_avrh[rD(ctx->opcode)], EA); \
fe1e5c53 6343 tcg_gen_addi_tl(EA, EA, 8); \
76db3ba4 6344 gen_qemu_ld64(ctx, cpu_avrl[rD(ctx->opcode)], EA); \
fe1e5c53
AJ
6345 } \
6346 tcg_temp_free(EA); \
a9d9eb8f
JM
6347}
6348
6349#define GEN_VR_STX(name, opc2, opc3) \
99e300ef 6350static void gen_st##name(DisasContext *ctx) \
a9d9eb8f 6351{ \
fe1e5c53 6352 TCGv EA; \
a9d9eb8f 6353 if (unlikely(!ctx->altivec_enabled)) { \
e06fcd75 6354 gen_exception(ctx, POWERPC_EXCP_VPU); \
a9d9eb8f
JM
6355 return; \
6356 } \
76db3ba4 6357 gen_set_access_type(ctx, ACCESS_INT); \
fe1e5c53 6358 EA = tcg_temp_new(); \
76db3ba4 6359 gen_addr_reg_index(ctx, EA); \
fe1e5c53 6360 tcg_gen_andi_tl(EA, EA, ~0xf); \
76db3ba4
AJ
6361 if (ctx->le_mode) { \
6362 gen_qemu_st64(ctx, cpu_avrl[rD(ctx->opcode)], EA); \
fe1e5c53 6363 tcg_gen_addi_tl(EA, EA, 8); \
76db3ba4 6364 gen_qemu_st64(ctx, cpu_avrh[rD(ctx->opcode)], EA); \
fe1e5c53 6365 } else { \
76db3ba4 6366 gen_qemu_st64(ctx, cpu_avrh[rD(ctx->opcode)], EA); \
fe1e5c53 6367 tcg_gen_addi_tl(EA, EA, 8); \
76db3ba4 6368 gen_qemu_st64(ctx, cpu_avrl[rD(ctx->opcode)], EA); \
fe1e5c53
AJ
6369 } \
6370 tcg_temp_free(EA); \
a9d9eb8f
JM
6371}
6372
cbfb6ae9 6373#define GEN_VR_LVE(name, opc2, opc3) \
99e300ef 6374static void gen_lve##name(DisasContext *ctx) \
cbfb6ae9
AJ
6375 { \
6376 TCGv EA; \
6377 TCGv_ptr rs; \
6378 if (unlikely(!ctx->altivec_enabled)) { \
6379 gen_exception(ctx, POWERPC_EXCP_VPU); \
6380 return; \
6381 } \
6382 gen_set_access_type(ctx, ACCESS_INT); \
6383 EA = tcg_temp_new(); \
6384 gen_addr_reg_index(ctx, EA); \
6385 rs = gen_avr_ptr(rS(ctx->opcode)); \
6386 gen_helper_lve##name (rs, EA); \
6387 tcg_temp_free(EA); \
6388 tcg_temp_free_ptr(rs); \
6389 }
6390
6391#define GEN_VR_STVE(name, opc2, opc3) \
99e300ef 6392static void gen_stve##name(DisasContext *ctx) \
cbfb6ae9
AJ
6393 { \
6394 TCGv EA; \
6395 TCGv_ptr rs; \
6396 if (unlikely(!ctx->altivec_enabled)) { \
6397 gen_exception(ctx, POWERPC_EXCP_VPU); \
6398 return; \
6399 } \
6400 gen_set_access_type(ctx, ACCESS_INT); \
6401 EA = tcg_temp_new(); \
6402 gen_addr_reg_index(ctx, EA); \
6403 rs = gen_avr_ptr(rS(ctx->opcode)); \
6404 gen_helper_stve##name (rs, EA); \
6405 tcg_temp_free(EA); \
6406 tcg_temp_free_ptr(rs); \
6407 }
6408
fe1e5c53 6409GEN_VR_LDX(lvx, 0x07, 0x03);
a9d9eb8f 6410/* As we don't emulate the cache, lvxl is stricly equivalent to lvx */
fe1e5c53 6411GEN_VR_LDX(lvxl, 0x07, 0x0B);
a9d9eb8f 6412
cbfb6ae9
AJ
6413GEN_VR_LVE(bx, 0x07, 0x00);
6414GEN_VR_LVE(hx, 0x07, 0x01);
6415GEN_VR_LVE(wx, 0x07, 0x02);
6416
fe1e5c53 6417GEN_VR_STX(svx, 0x07, 0x07);
a9d9eb8f 6418/* As we don't emulate the cache, stvxl is stricly equivalent to stvx */
fe1e5c53 6419GEN_VR_STX(svxl, 0x07, 0x0F);
a9d9eb8f 6420
cbfb6ae9
AJ
6421GEN_VR_STVE(bx, 0x07, 0x04);
6422GEN_VR_STVE(hx, 0x07, 0x05);
6423GEN_VR_STVE(wx, 0x07, 0x06);
6424
99e300ef 6425static void gen_lvsl(DisasContext *ctx)
bf8d8ded
AJ
6426{
6427 TCGv_ptr rd;
6428 TCGv EA;
6429 if (unlikely(!ctx->altivec_enabled)) {
6430 gen_exception(ctx, POWERPC_EXCP_VPU);
6431 return;
6432 }
6433 EA = tcg_temp_new();
6434 gen_addr_reg_index(ctx, EA);
6435 rd = gen_avr_ptr(rD(ctx->opcode));
6436 gen_helper_lvsl(rd, EA);
6437 tcg_temp_free(EA);
6438 tcg_temp_free_ptr(rd);
6439}
6440
99e300ef 6441static void gen_lvsr(DisasContext *ctx)
bf8d8ded
AJ
6442{
6443 TCGv_ptr rd;
6444 TCGv EA;
6445 if (unlikely(!ctx->altivec_enabled)) {
6446 gen_exception(ctx, POWERPC_EXCP_VPU);
6447 return;
6448 }
6449 EA = tcg_temp_new();
6450 gen_addr_reg_index(ctx, EA);
6451 rd = gen_avr_ptr(rD(ctx->opcode));
6452 gen_helper_lvsr(rd, EA);
6453 tcg_temp_free(EA);
6454 tcg_temp_free_ptr(rd);
6455}
6456
99e300ef 6457static void gen_mfvscr(DisasContext *ctx)
785f451b
AJ
6458{
6459 TCGv_i32 t;
6460 if (unlikely(!ctx->altivec_enabled)) {
6461 gen_exception(ctx, POWERPC_EXCP_VPU);
6462 return;
6463 }
6464 tcg_gen_movi_i64(cpu_avrh[rD(ctx->opcode)], 0);
6465 t = tcg_temp_new_i32();
1328c2bf 6466 tcg_gen_ld_i32(t, cpu_env, offsetof(CPUPPCState, vscr));
785f451b 6467 tcg_gen_extu_i32_i64(cpu_avrl[rD(ctx->opcode)], t);
fce5ecb7 6468 tcg_temp_free_i32(t);
785f451b
AJ
6469}
6470
99e300ef 6471static void gen_mtvscr(DisasContext *ctx)
785f451b 6472{
6e87b7c7 6473 TCGv_ptr p;
785f451b
AJ
6474 if (unlikely(!ctx->altivec_enabled)) {
6475 gen_exception(ctx, POWERPC_EXCP_VPU);
6476 return;
6477 }
6e87b7c7 6478 p = gen_avr_ptr(rD(ctx->opcode));
d15f74fb 6479 gen_helper_mtvscr(cpu_env, p);
6e87b7c7 6480 tcg_temp_free_ptr(p);
785f451b
AJ
6481}
6482
7a9b96cf
AJ
6483/* Logical operations */
6484#define GEN_VX_LOGICAL(name, tcg_op, opc2, opc3) \
99e300ef 6485static void glue(gen_, name)(DisasContext *ctx) \
7a9b96cf
AJ
6486{ \
6487 if (unlikely(!ctx->altivec_enabled)) { \
6488 gen_exception(ctx, POWERPC_EXCP_VPU); \
6489 return; \
6490 } \
6491 tcg_op(cpu_avrh[rD(ctx->opcode)], cpu_avrh[rA(ctx->opcode)], cpu_avrh[rB(ctx->opcode)]); \
6492 tcg_op(cpu_avrl[rD(ctx->opcode)], cpu_avrl[rA(ctx->opcode)], cpu_avrl[rB(ctx->opcode)]); \
6493}
6494
6495GEN_VX_LOGICAL(vand, tcg_gen_and_i64, 2, 16);
6496GEN_VX_LOGICAL(vandc, tcg_gen_andc_i64, 2, 17);
6497GEN_VX_LOGICAL(vor, tcg_gen_or_i64, 2, 18);
6498GEN_VX_LOGICAL(vxor, tcg_gen_xor_i64, 2, 19);
6499GEN_VX_LOGICAL(vnor, tcg_gen_nor_i64, 2, 20);
6500
8e27dd6f 6501#define GEN_VXFORM(name, opc2, opc3) \
99e300ef 6502static void glue(gen_, name)(DisasContext *ctx) \
8e27dd6f
AJ
6503{ \
6504 TCGv_ptr ra, rb, rd; \
6505 if (unlikely(!ctx->altivec_enabled)) { \
6506 gen_exception(ctx, POWERPC_EXCP_VPU); \
6507 return; \
6508 } \
6509 ra = gen_avr_ptr(rA(ctx->opcode)); \
6510 rb = gen_avr_ptr(rB(ctx->opcode)); \
6511 rd = gen_avr_ptr(rD(ctx->opcode)); \
6512 gen_helper_##name (rd, ra, rb); \
6513 tcg_temp_free_ptr(ra); \
6514 tcg_temp_free_ptr(rb); \
6515 tcg_temp_free_ptr(rd); \
6516}
6517
d15f74fb
BS
6518#define GEN_VXFORM_ENV(name, opc2, opc3) \
6519static void glue(gen_, name)(DisasContext *ctx) \
6520{ \
6521 TCGv_ptr ra, rb, rd; \
6522 if (unlikely(!ctx->altivec_enabled)) { \
6523 gen_exception(ctx, POWERPC_EXCP_VPU); \
6524 return; \
6525 } \
6526 ra = gen_avr_ptr(rA(ctx->opcode)); \
6527 rb = gen_avr_ptr(rB(ctx->opcode)); \
6528 rd = gen_avr_ptr(rD(ctx->opcode)); \
6529 gen_helper_##name(rd, cpu_env, ra, rb); \
6530 tcg_temp_free_ptr(ra); \
6531 tcg_temp_free_ptr(rb); \
6532 tcg_temp_free_ptr(rd); \
6533}
6534
7872c51c
AJ
6535GEN_VXFORM(vaddubm, 0, 0);
6536GEN_VXFORM(vadduhm, 0, 1);
6537GEN_VXFORM(vadduwm, 0, 2);
6538GEN_VXFORM(vsububm, 0, 16);
6539GEN_VXFORM(vsubuhm, 0, 17);
6540GEN_VXFORM(vsubuwm, 0, 18);
e4039339
AJ
6541GEN_VXFORM(vmaxub, 1, 0);
6542GEN_VXFORM(vmaxuh, 1, 1);
6543GEN_VXFORM(vmaxuw, 1, 2);
6544GEN_VXFORM(vmaxsb, 1, 4);
6545GEN_VXFORM(vmaxsh, 1, 5);
6546GEN_VXFORM(vmaxsw, 1, 6);
6547GEN_VXFORM(vminub, 1, 8);
6548GEN_VXFORM(vminuh, 1, 9);
6549GEN_VXFORM(vminuw, 1, 10);
6550GEN_VXFORM(vminsb, 1, 12);
6551GEN_VXFORM(vminsh, 1, 13);
6552GEN_VXFORM(vminsw, 1, 14);
fab3cbe9
AJ
6553GEN_VXFORM(vavgub, 1, 16);
6554GEN_VXFORM(vavguh, 1, 17);
6555GEN_VXFORM(vavguw, 1, 18);
6556GEN_VXFORM(vavgsb, 1, 20);
6557GEN_VXFORM(vavgsh, 1, 21);
6558GEN_VXFORM(vavgsw, 1, 22);
3b430048
AJ
6559GEN_VXFORM(vmrghb, 6, 0);
6560GEN_VXFORM(vmrghh, 6, 1);
6561GEN_VXFORM(vmrghw, 6, 2);
6562GEN_VXFORM(vmrglb, 6, 4);
6563GEN_VXFORM(vmrglh, 6, 5);
6564GEN_VXFORM(vmrglw, 6, 6);
2c277908
AJ
6565GEN_VXFORM(vmuloub, 4, 0);
6566GEN_VXFORM(vmulouh, 4, 1);
6567GEN_VXFORM(vmulosb, 4, 4);
6568GEN_VXFORM(vmulosh, 4, 5);
6569GEN_VXFORM(vmuleub, 4, 8);
6570GEN_VXFORM(vmuleuh, 4, 9);
6571GEN_VXFORM(vmulesb, 4, 12);
6572GEN_VXFORM(vmulesh, 4, 13);
d79f0809
AJ
6573GEN_VXFORM(vslb, 2, 4);
6574GEN_VXFORM(vslh, 2, 5);
6575GEN_VXFORM(vslw, 2, 6);
07ef34c3
AJ
6576GEN_VXFORM(vsrb, 2, 8);
6577GEN_VXFORM(vsrh, 2, 9);
6578GEN_VXFORM(vsrw, 2, 10);
6579GEN_VXFORM(vsrab, 2, 12);
6580GEN_VXFORM(vsrah, 2, 13);
6581GEN_VXFORM(vsraw, 2, 14);
7b239bec
AJ
6582GEN_VXFORM(vslo, 6, 16);
6583GEN_VXFORM(vsro, 6, 17);
e343da72
AJ
6584GEN_VXFORM(vaddcuw, 0, 6);
6585GEN_VXFORM(vsubcuw, 0, 22);
d15f74fb
BS
6586GEN_VXFORM_ENV(vaddubs, 0, 8);
6587GEN_VXFORM_ENV(vadduhs, 0, 9);
6588GEN_VXFORM_ENV(vadduws, 0, 10);
6589GEN_VXFORM_ENV(vaddsbs, 0, 12);
6590GEN_VXFORM_ENV(vaddshs, 0, 13);
6591GEN_VXFORM_ENV(vaddsws, 0, 14);
6592GEN_VXFORM_ENV(vsububs, 0, 24);
6593GEN_VXFORM_ENV(vsubuhs, 0, 25);
6594GEN_VXFORM_ENV(vsubuws, 0, 26);
6595GEN_VXFORM_ENV(vsubsbs, 0, 28);
6596GEN_VXFORM_ENV(vsubshs, 0, 29);
6597GEN_VXFORM_ENV(vsubsws, 0, 30);
5e1d0985
AJ
6598GEN_VXFORM(vrlb, 2, 0);
6599GEN_VXFORM(vrlh, 2, 1);
6600GEN_VXFORM(vrlw, 2, 2);
d9430add
AJ
6601GEN_VXFORM(vsl, 2, 7);
6602GEN_VXFORM(vsr, 2, 11);
d15f74fb
BS
6603GEN_VXFORM_ENV(vpkuhum, 7, 0);
6604GEN_VXFORM_ENV(vpkuwum, 7, 1);
6605GEN_VXFORM_ENV(vpkuhus, 7, 2);
6606GEN_VXFORM_ENV(vpkuwus, 7, 3);
6607GEN_VXFORM_ENV(vpkshus, 7, 4);
6608GEN_VXFORM_ENV(vpkswus, 7, 5);
6609GEN_VXFORM_ENV(vpkshss, 7, 6);
6610GEN_VXFORM_ENV(vpkswss, 7, 7);
1dd9ffb9 6611GEN_VXFORM(vpkpx, 7, 12);
d15f74fb
BS
6612GEN_VXFORM_ENV(vsum4ubs, 4, 24);
6613GEN_VXFORM_ENV(vsum4sbs, 4, 28);
6614GEN_VXFORM_ENV(vsum4shs, 4, 25);
6615GEN_VXFORM_ENV(vsum2sws, 4, 26);
6616GEN_VXFORM_ENV(vsumsws, 4, 30);
6617GEN_VXFORM_ENV(vaddfp, 5, 0);
6618GEN_VXFORM_ENV(vsubfp, 5, 1);
6619GEN_VXFORM_ENV(vmaxfp, 5, 16);
6620GEN_VXFORM_ENV(vminfp, 5, 17);
fab3cbe9 6621
0cbcd906 6622#define GEN_VXRFORM1(opname, name, str, opc2, opc3) \
e8eaa2c0 6623static void glue(gen_, name)(DisasContext *ctx) \
0cbcd906
AJ
6624 { \
6625 TCGv_ptr ra, rb, rd; \
6626 if (unlikely(!ctx->altivec_enabled)) { \
6627 gen_exception(ctx, POWERPC_EXCP_VPU); \
6628 return; \
6629 } \
6630 ra = gen_avr_ptr(rA(ctx->opcode)); \
6631 rb = gen_avr_ptr(rB(ctx->opcode)); \
6632 rd = gen_avr_ptr(rD(ctx->opcode)); \
d15f74fb 6633 gen_helper_##opname(cpu_env, rd, ra, rb); \
0cbcd906
AJ
6634 tcg_temp_free_ptr(ra); \
6635 tcg_temp_free_ptr(rb); \
6636 tcg_temp_free_ptr(rd); \
6637 }
6638
6639#define GEN_VXRFORM(name, opc2, opc3) \
6640 GEN_VXRFORM1(name, name, #name, opc2, opc3) \
6641 GEN_VXRFORM1(name##_dot, name##_, #name ".", opc2, (opc3 | (0x1 << 4)))
6642
1add6e23
AJ
6643GEN_VXRFORM(vcmpequb, 3, 0)
6644GEN_VXRFORM(vcmpequh, 3, 1)
6645GEN_VXRFORM(vcmpequw, 3, 2)
6646GEN_VXRFORM(vcmpgtsb, 3, 12)
6647GEN_VXRFORM(vcmpgtsh, 3, 13)
6648GEN_VXRFORM(vcmpgtsw, 3, 14)
6649GEN_VXRFORM(vcmpgtub, 3, 8)
6650GEN_VXRFORM(vcmpgtuh, 3, 9)
6651GEN_VXRFORM(vcmpgtuw, 3, 10)
819ca121
AJ
6652GEN_VXRFORM(vcmpeqfp, 3, 3)
6653GEN_VXRFORM(vcmpgefp, 3, 7)
6654GEN_VXRFORM(vcmpgtfp, 3, 11)
6655GEN_VXRFORM(vcmpbfp, 3, 15)
1add6e23 6656
c026766b 6657#define GEN_VXFORM_SIMM(name, opc2, opc3) \
99e300ef 6658static void glue(gen_, name)(DisasContext *ctx) \
c026766b
AJ
6659 { \
6660 TCGv_ptr rd; \
6661 TCGv_i32 simm; \
6662 if (unlikely(!ctx->altivec_enabled)) { \
6663 gen_exception(ctx, POWERPC_EXCP_VPU); \
6664 return; \
6665 } \
6666 simm = tcg_const_i32(SIMM5(ctx->opcode)); \
6667 rd = gen_avr_ptr(rD(ctx->opcode)); \
6668 gen_helper_##name (rd, simm); \
6669 tcg_temp_free_i32(simm); \
6670 tcg_temp_free_ptr(rd); \
6671 }
6672
6673GEN_VXFORM_SIMM(vspltisb, 6, 12);
6674GEN_VXFORM_SIMM(vspltish, 6, 13);
6675GEN_VXFORM_SIMM(vspltisw, 6, 14);
6676
de5f2484 6677#define GEN_VXFORM_NOA(name, opc2, opc3) \
99e300ef 6678static void glue(gen_, name)(DisasContext *ctx) \
de5f2484
AJ
6679 { \
6680 TCGv_ptr rb, rd; \
6681 if (unlikely(!ctx->altivec_enabled)) { \
6682 gen_exception(ctx, POWERPC_EXCP_VPU); \
6683 return; \
6684 } \
6685 rb = gen_avr_ptr(rB(ctx->opcode)); \
6686 rd = gen_avr_ptr(rD(ctx->opcode)); \
6687 gen_helper_##name (rd, rb); \
6688 tcg_temp_free_ptr(rb); \
6689 tcg_temp_free_ptr(rd); \
6690 }
6691
d15f74fb
BS
6692#define GEN_VXFORM_NOA_ENV(name, opc2, opc3) \
6693static void glue(gen_, name)(DisasContext *ctx) \
6694 { \
6695 TCGv_ptr rb, rd; \
6696 \
6697 if (unlikely(!ctx->altivec_enabled)) { \
6698 gen_exception(ctx, POWERPC_EXCP_VPU); \
6699 return; \
6700 } \
6701 rb = gen_avr_ptr(rB(ctx->opcode)); \
6702 rd = gen_avr_ptr(rD(ctx->opcode)); \
6703 gen_helper_##name(cpu_env, rd, rb); \
6704 tcg_temp_free_ptr(rb); \
6705 tcg_temp_free_ptr(rd); \
6706 }
6707
6cf1c6e5
AJ
6708GEN_VXFORM_NOA(vupkhsb, 7, 8);
6709GEN_VXFORM_NOA(vupkhsh, 7, 9);
6710GEN_VXFORM_NOA(vupklsb, 7, 10);
6711GEN_VXFORM_NOA(vupklsh, 7, 11);
79f85c3a
AJ
6712GEN_VXFORM_NOA(vupkhpx, 7, 13);
6713GEN_VXFORM_NOA(vupklpx, 7, 15);
d15f74fb
BS
6714GEN_VXFORM_NOA_ENV(vrefp, 5, 4);
6715GEN_VXFORM_NOA_ENV(vrsqrtefp, 5, 5);
6716GEN_VXFORM_NOA_ENV(vexptefp, 5, 6);
6717GEN_VXFORM_NOA_ENV(vlogefp, 5, 7);
6718GEN_VXFORM_NOA_ENV(vrfim, 5, 8);
6719GEN_VXFORM_NOA_ENV(vrfin, 5, 9);
6720GEN_VXFORM_NOA_ENV(vrfip, 5, 10);
6721GEN_VXFORM_NOA_ENV(vrfiz, 5, 11);
79f85c3a 6722
21d21583 6723#define GEN_VXFORM_SIMM(name, opc2, opc3) \
99e300ef 6724static void glue(gen_, name)(DisasContext *ctx) \
21d21583
AJ
6725 { \
6726 TCGv_ptr rd; \
6727 TCGv_i32 simm; \
6728 if (unlikely(!ctx->altivec_enabled)) { \
6729 gen_exception(ctx, POWERPC_EXCP_VPU); \
6730 return; \
6731 } \
6732 simm = tcg_const_i32(SIMM5(ctx->opcode)); \
6733 rd = gen_avr_ptr(rD(ctx->opcode)); \
6734 gen_helper_##name (rd, simm); \
6735 tcg_temp_free_i32(simm); \
6736 tcg_temp_free_ptr(rd); \
6737 }
6738
27a4edb3 6739#define GEN_VXFORM_UIMM(name, opc2, opc3) \
99e300ef 6740static void glue(gen_, name)(DisasContext *ctx) \
27a4edb3
AJ
6741 { \
6742 TCGv_ptr rb, rd; \
6743 TCGv_i32 uimm; \
6744 if (unlikely(!ctx->altivec_enabled)) { \
6745 gen_exception(ctx, POWERPC_EXCP_VPU); \
6746 return; \
6747 } \
6748 uimm = tcg_const_i32(UIMM5(ctx->opcode)); \
6749 rb = gen_avr_ptr(rB(ctx->opcode)); \
6750 rd = gen_avr_ptr(rD(ctx->opcode)); \
6751 gen_helper_##name (rd, rb, uimm); \
6752 tcg_temp_free_i32(uimm); \
6753 tcg_temp_free_ptr(rb); \
6754 tcg_temp_free_ptr(rd); \
6755 }
6756
d15f74fb
BS
6757#define GEN_VXFORM_UIMM_ENV(name, opc2, opc3) \
6758static void glue(gen_, name)(DisasContext *ctx) \
6759 { \
6760 TCGv_ptr rb, rd; \
6761 TCGv_i32 uimm; \
6762 \
6763 if (unlikely(!ctx->altivec_enabled)) { \
6764 gen_exception(ctx, POWERPC_EXCP_VPU); \
6765 return; \
6766 } \
6767 uimm = tcg_const_i32(UIMM5(ctx->opcode)); \
6768 rb = gen_avr_ptr(rB(ctx->opcode)); \
6769 rd = gen_avr_ptr(rD(ctx->opcode)); \
6770 gen_helper_##name(cpu_env, rd, rb, uimm); \
6771 tcg_temp_free_i32(uimm); \
6772 tcg_temp_free_ptr(rb); \
6773 tcg_temp_free_ptr(rd); \
6774 }
6775
e4e6bee7
AJ
6776GEN_VXFORM_UIMM(vspltb, 6, 8);
6777GEN_VXFORM_UIMM(vsplth, 6, 9);
6778GEN_VXFORM_UIMM(vspltw, 6, 10);
d15f74fb
BS
6779GEN_VXFORM_UIMM_ENV(vcfux, 5, 12);
6780GEN_VXFORM_UIMM_ENV(vcfsx, 5, 13);
6781GEN_VXFORM_UIMM_ENV(vctuxs, 5, 14);
6782GEN_VXFORM_UIMM_ENV(vctsxs, 5, 15);
e4e6bee7 6783
99e300ef 6784static void gen_vsldoi(DisasContext *ctx)
cd633b10
AJ
6785{
6786 TCGv_ptr ra, rb, rd;
fce5ecb7 6787 TCGv_i32 sh;
cd633b10
AJ
6788 if (unlikely(!ctx->altivec_enabled)) {
6789 gen_exception(ctx, POWERPC_EXCP_VPU);
6790 return;
6791 }
6792 ra = gen_avr_ptr(rA(ctx->opcode));
6793 rb = gen_avr_ptr(rB(ctx->opcode));
6794 rd = gen_avr_ptr(rD(ctx->opcode));
6795 sh = tcg_const_i32(VSH(ctx->opcode));
6796 gen_helper_vsldoi (rd, ra, rb, sh);
6797 tcg_temp_free_ptr(ra);
6798 tcg_temp_free_ptr(rb);
6799 tcg_temp_free_ptr(rd);
fce5ecb7 6800 tcg_temp_free_i32(sh);
cd633b10
AJ
6801}
6802
707cec33 6803#define GEN_VAFORM_PAIRED(name0, name1, opc2) \
d15f74fb 6804static void glue(gen_, name0##_##name1)(DisasContext *ctx) \
707cec33
AJ
6805 { \
6806 TCGv_ptr ra, rb, rc, rd; \
6807 if (unlikely(!ctx->altivec_enabled)) { \
6808 gen_exception(ctx, POWERPC_EXCP_VPU); \
6809 return; \
6810 } \
6811 ra = gen_avr_ptr(rA(ctx->opcode)); \
6812 rb = gen_avr_ptr(rB(ctx->opcode)); \
6813 rc = gen_avr_ptr(rC(ctx->opcode)); \
6814 rd = gen_avr_ptr(rD(ctx->opcode)); \
6815 if (Rc(ctx->opcode)) { \
d15f74fb 6816 gen_helper_##name1(cpu_env, rd, ra, rb, rc); \
707cec33 6817 } else { \
d15f74fb 6818 gen_helper_##name0(cpu_env, rd, ra, rb, rc); \
707cec33
AJ
6819 } \
6820 tcg_temp_free_ptr(ra); \
6821 tcg_temp_free_ptr(rb); \
6822 tcg_temp_free_ptr(rc); \
6823 tcg_temp_free_ptr(rd); \
6824 }
6825
b161ae27
AJ
6826GEN_VAFORM_PAIRED(vmhaddshs, vmhraddshs, 16)
6827
99e300ef 6828static void gen_vmladduhm(DisasContext *ctx)
bcd2ee23
AJ
6829{
6830 TCGv_ptr ra, rb, rc, rd;
6831 if (unlikely(!ctx->altivec_enabled)) {
6832 gen_exception(ctx, POWERPC_EXCP_VPU);
6833 return;
6834 }
6835 ra = gen_avr_ptr(rA(ctx->opcode));
6836 rb = gen_avr_ptr(rB(ctx->opcode));
6837 rc = gen_avr_ptr(rC(ctx->opcode));
6838 rd = gen_avr_ptr(rD(ctx->opcode));
6839 gen_helper_vmladduhm(rd, ra, rb, rc);
6840 tcg_temp_free_ptr(ra);
6841 tcg_temp_free_ptr(rb);
6842 tcg_temp_free_ptr(rc);
6843 tcg_temp_free_ptr(rd);
6844}
6845
b04ae981 6846GEN_VAFORM_PAIRED(vmsumubm, vmsummbm, 18)
4d9903b6 6847GEN_VAFORM_PAIRED(vmsumuhm, vmsumuhs, 19)
eae07261 6848GEN_VAFORM_PAIRED(vmsumshm, vmsumshs, 20)
d1258698 6849GEN_VAFORM_PAIRED(vsel, vperm, 21)
35cf7c7e 6850GEN_VAFORM_PAIRED(vmaddfp, vnmsubfp, 23)
b04ae981 6851
0487d6a8 6852/*** SPE extension ***/
0487d6a8 6853/* Register moves */
3cd7d1dd 6854
a0e13900
FC
6855
6856static inline void gen_evmra(DisasContext *ctx)
6857{
6858
6859 if (unlikely(!ctx->spe_enabled)) {
27a69bb0 6860 gen_exception(ctx, POWERPC_EXCP_SPEU);
a0e13900
FC
6861 return;
6862 }
6863
6864#if defined(TARGET_PPC64)
6865 /* rD := rA */
6866 tcg_gen_mov_i64(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)]);
6867
6868 /* spe_acc := rA */
6869 tcg_gen_st_i64(cpu_gpr[rA(ctx->opcode)],
6870 cpu_env,
1328c2bf 6871 offsetof(CPUPPCState, spe_acc));
a0e13900
FC
6872#else
6873 TCGv_i64 tmp = tcg_temp_new_i64();
6874
6875 /* tmp := rA_lo + rA_hi << 32 */
6876 tcg_gen_concat_i32_i64(tmp, cpu_gpr[rA(ctx->opcode)], cpu_gprh[rA(ctx->opcode)]);
6877
6878 /* spe_acc := tmp */
1328c2bf 6879 tcg_gen_st_i64(tmp, cpu_env, offsetof(CPUPPCState, spe_acc));
a0e13900
FC
6880 tcg_temp_free_i64(tmp);
6881
6882 /* rD := rA */
6883 tcg_gen_mov_i32(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)]);
6884 tcg_gen_mov_i32(cpu_gprh[rD(ctx->opcode)], cpu_gprh[rA(ctx->opcode)]);
6885#endif
6886}
6887
636aa200
BS
6888static inline void gen_load_gpr64(TCGv_i64 t, int reg)
6889{
f78fb44e
AJ
6890#if defined(TARGET_PPC64)
6891 tcg_gen_mov_i64(t, cpu_gpr[reg]);
6892#else
36aa55dc 6893 tcg_gen_concat_i32_i64(t, cpu_gpr[reg], cpu_gprh[reg]);
3cd7d1dd 6894#endif
f78fb44e 6895}
3cd7d1dd 6896
636aa200
BS
6897static inline void gen_store_gpr64(int reg, TCGv_i64 t)
6898{
f78fb44e
AJ
6899#if defined(TARGET_PPC64)
6900 tcg_gen_mov_i64(cpu_gpr[reg], t);
6901#else
a7812ae4 6902 TCGv_i64 tmp = tcg_temp_new_i64();
f78fb44e 6903 tcg_gen_trunc_i64_i32(cpu_gpr[reg], t);
f78fb44e
AJ
6904 tcg_gen_shri_i64(tmp, t, 32);
6905 tcg_gen_trunc_i64_i32(cpu_gprh[reg], tmp);
a7812ae4 6906 tcg_temp_free_i64(tmp);
3cd7d1dd 6907#endif
f78fb44e 6908}
3cd7d1dd 6909
70560da7 6910#define GEN_SPE(name0, name1, opc2, opc3, inval0, inval1, type) \
99e300ef 6911static void glue(gen_, name0##_##name1)(DisasContext *ctx) \
0487d6a8
JM
6912{ \
6913 if (Rc(ctx->opcode)) \
6914 gen_##name1(ctx); \
6915 else \
6916 gen_##name0(ctx); \
6917}
6918
6919/* Handler for undefined SPE opcodes */
636aa200 6920static inline void gen_speundef(DisasContext *ctx)
0487d6a8 6921{
e06fcd75 6922 gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL);
0487d6a8
JM
6923}
6924
57951c27
AJ
6925/* SPE logic */
6926#if defined(TARGET_PPC64)
6927#define GEN_SPEOP_LOGIC2(name, tcg_op) \
636aa200 6928static inline void gen_##name(DisasContext *ctx) \
0487d6a8
JM
6929{ \
6930 if (unlikely(!ctx->spe_enabled)) { \
27a69bb0 6931 gen_exception(ctx, POWERPC_EXCP_SPEU); \
0487d6a8
JM
6932 return; \
6933 } \
57951c27
AJ
6934 tcg_op(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)], \
6935 cpu_gpr[rB(ctx->opcode)]); \
6936}
6937#else
6938#define GEN_SPEOP_LOGIC2(name, tcg_op) \
636aa200 6939static inline void gen_##name(DisasContext *ctx) \
57951c27
AJ
6940{ \
6941 if (unlikely(!ctx->spe_enabled)) { \
27a69bb0 6942 gen_exception(ctx, POWERPC_EXCP_SPEU); \
57951c27
AJ
6943 return; \
6944 } \
6945 tcg_op(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)], \
6946 cpu_gpr[rB(ctx->opcode)]); \
6947 tcg_op(cpu_gprh[rD(ctx->opcode)], cpu_gprh[rA(ctx->opcode)], \
6948 cpu_gprh[rB(ctx->opcode)]); \
0487d6a8 6949}
57951c27
AJ
6950#endif
6951
6952GEN_SPEOP_LOGIC2(evand, tcg_gen_and_tl);
6953GEN_SPEOP_LOGIC2(evandc, tcg_gen_andc_tl);
6954GEN_SPEOP_LOGIC2(evxor, tcg_gen_xor_tl);
6955GEN_SPEOP_LOGIC2(evor, tcg_gen_or_tl);
6956GEN_SPEOP_LOGIC2(evnor, tcg_gen_nor_tl);
6957GEN_SPEOP_LOGIC2(eveqv, tcg_gen_eqv_tl);
6958GEN_SPEOP_LOGIC2(evorc, tcg_gen_orc_tl);
6959GEN_SPEOP_LOGIC2(evnand, tcg_gen_nand_tl);
0487d6a8 6960
57951c27
AJ
6961/* SPE logic immediate */
6962#if defined(TARGET_PPC64)
6963#define GEN_SPEOP_TCG_LOGIC_IMM2(name, tcg_opi) \
636aa200 6964static inline void gen_##name(DisasContext *ctx) \
3d3a6a0a
AJ
6965{ \
6966 if (unlikely(!ctx->spe_enabled)) { \
27a69bb0 6967 gen_exception(ctx, POWERPC_EXCP_SPEU); \
3d3a6a0a
AJ
6968 return; \
6969 } \
a7812ae4
PB
6970 TCGv_i32 t0 = tcg_temp_local_new_i32(); \
6971 TCGv_i32 t1 = tcg_temp_local_new_i32(); \
6972 TCGv_i64 t2 = tcg_temp_local_new_i64(); \
57951c27
AJ
6973 tcg_gen_trunc_i64_i32(t0, cpu_gpr[rA(ctx->opcode)]); \
6974 tcg_opi(t0, t0, rB(ctx->opcode)); \
6975 tcg_gen_shri_i64(t2, cpu_gpr[rA(ctx->opcode)], 32); \
6976 tcg_gen_trunc_i64_i32(t1, t2); \
a7812ae4 6977 tcg_temp_free_i64(t2); \
57951c27
AJ
6978 tcg_opi(t1, t1, rB(ctx->opcode)); \
6979 tcg_gen_concat_i32_i64(cpu_gpr[rD(ctx->opcode)], t0, t1); \
a7812ae4
PB
6980 tcg_temp_free_i32(t0); \
6981 tcg_temp_free_i32(t1); \
3d3a6a0a 6982}
57951c27
AJ
6983#else
6984#define GEN_SPEOP_TCG_LOGIC_IMM2(name, tcg_opi) \
636aa200 6985static inline void gen_##name(DisasContext *ctx) \
0487d6a8
JM
6986{ \
6987 if (unlikely(!ctx->spe_enabled)) { \
27a69bb0 6988 gen_exception(ctx, POWERPC_EXCP_SPEU); \
0487d6a8
JM
6989 return; \
6990 } \
57951c27
AJ
6991 tcg_opi(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)], \
6992 rB(ctx->opcode)); \
6993 tcg_opi(cpu_gprh[rD(ctx->opcode)], cpu_gprh[rA(ctx->opcode)], \
6994 rB(ctx->opcode)); \
0487d6a8 6995}
57951c27
AJ
6996#endif
6997GEN_SPEOP_TCG_LOGIC_IMM2(evslwi, tcg_gen_shli_i32);
6998GEN_SPEOP_TCG_LOGIC_IMM2(evsrwiu, tcg_gen_shri_i32);
6999GEN_SPEOP_TCG_LOGIC_IMM2(evsrwis, tcg_gen_sari_i32);
7000GEN_SPEOP_TCG_LOGIC_IMM2(evrlwi, tcg_gen_rotli_i32);
0487d6a8 7001
57951c27
AJ
7002/* SPE arithmetic */
7003#if defined(TARGET_PPC64)
7004#define GEN_SPEOP_ARITH1(name, tcg_op) \
636aa200 7005static inline void gen_##name(DisasContext *ctx) \
0487d6a8
JM
7006{ \
7007 if (unlikely(!ctx->spe_enabled)) { \
27a69bb0 7008 gen_exception(ctx, POWERPC_EXCP_SPEU); \
0487d6a8
JM
7009 return; \
7010 } \
a7812ae4
PB
7011 TCGv_i32 t0 = tcg_temp_local_new_i32(); \
7012 TCGv_i32 t1 = tcg_temp_local_new_i32(); \
7013 TCGv_i64 t2 = tcg_temp_local_new_i64(); \
57951c27
AJ
7014 tcg_gen_trunc_i64_i32(t0, cpu_gpr[rA(ctx->opcode)]); \
7015 tcg_op(t0, t0); \
7016 tcg_gen_shri_i64(t2, cpu_gpr[rA(ctx->opcode)], 32); \
7017 tcg_gen_trunc_i64_i32(t1, t2); \
a7812ae4 7018 tcg_temp_free_i64(t2); \
57951c27
AJ
7019 tcg_op(t1, t1); \
7020 tcg_gen_concat_i32_i64(cpu_gpr[rD(ctx->opcode)], t0, t1); \
a7812ae4
PB
7021 tcg_temp_free_i32(t0); \
7022 tcg_temp_free_i32(t1); \
0487d6a8 7023}
57951c27 7024#else
a7812ae4 7025#define GEN_SPEOP_ARITH1(name, tcg_op) \
636aa200 7026static inline void gen_##name(DisasContext *ctx) \
57951c27
AJ
7027{ \
7028 if (unlikely(!ctx->spe_enabled)) { \
27a69bb0 7029 gen_exception(ctx, POWERPC_EXCP_SPEU); \
57951c27
AJ
7030 return; \
7031 } \
7032 tcg_op(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)]); \
7033 tcg_op(cpu_gprh[rD(ctx->opcode)], cpu_gprh[rA(ctx->opcode)]); \
7034}
7035#endif
0487d6a8 7036
636aa200 7037static inline void gen_op_evabs(TCGv_i32 ret, TCGv_i32 arg1)
57951c27
AJ
7038{
7039 int l1 = gen_new_label();
7040 int l2 = gen_new_label();
0487d6a8 7041
57951c27
AJ
7042 tcg_gen_brcondi_i32(TCG_COND_GE, arg1, 0, l1);
7043 tcg_gen_neg_i32(ret, arg1);
7044 tcg_gen_br(l2);
7045 gen_set_label(l1);
a7812ae4 7046 tcg_gen_mov_i32(ret, arg1);
57951c27
AJ
7047 gen_set_label(l2);
7048}
7049GEN_SPEOP_ARITH1(evabs, gen_op_evabs);
7050GEN_SPEOP_ARITH1(evneg, tcg_gen_neg_i32);
7051GEN_SPEOP_ARITH1(evextsb, tcg_gen_ext8s_i32);
7052GEN_SPEOP_ARITH1(evextsh, tcg_gen_ext16s_i32);
636aa200 7053static inline void gen_op_evrndw(TCGv_i32 ret, TCGv_i32 arg1)
0487d6a8 7054{
57951c27
AJ
7055 tcg_gen_addi_i32(ret, arg1, 0x8000);
7056 tcg_gen_ext16u_i32(ret, ret);
7057}
7058GEN_SPEOP_ARITH1(evrndw, gen_op_evrndw);
a7812ae4
PB
7059GEN_SPEOP_ARITH1(evcntlsw, gen_helper_cntlsw32);
7060GEN_SPEOP_ARITH1(evcntlzw, gen_helper_cntlzw32);
0487d6a8 7061
57951c27
AJ
7062#if defined(TARGET_PPC64)
7063#define GEN_SPEOP_ARITH2(name, tcg_op) \
636aa200 7064static inline void gen_##name(DisasContext *ctx) \
0487d6a8
JM
7065{ \
7066 if (unlikely(!ctx->spe_enabled)) { \
27a69bb0 7067 gen_exception(ctx, POWERPC_EXCP_SPEU); \
0487d6a8
JM
7068 return; \
7069 } \
a7812ae4
PB
7070 TCGv_i32 t0 = tcg_temp_local_new_i32(); \
7071 TCGv_i32 t1 = tcg_temp_local_new_i32(); \
7072 TCGv_i32 t2 = tcg_temp_local_new_i32(); \
501e23c4 7073 TCGv_i64 t3 = tcg_temp_local_new_i64(); \
57951c27
AJ
7074 tcg_gen_trunc_i64_i32(t0, cpu_gpr[rA(ctx->opcode)]); \
7075 tcg_gen_trunc_i64_i32(t2, cpu_gpr[rB(ctx->opcode)]); \
7076 tcg_op(t0, t0, t2); \
7077 tcg_gen_shri_i64(t3, cpu_gpr[rA(ctx->opcode)], 32); \
7078 tcg_gen_trunc_i64_i32(t1, t3); \
7079 tcg_gen_shri_i64(t3, cpu_gpr[rB(ctx->opcode)], 32); \
7080 tcg_gen_trunc_i64_i32(t2, t3); \
a7812ae4 7081 tcg_temp_free_i64(t3); \
57951c27 7082 tcg_op(t1, t1, t2); \
a7812ae4 7083 tcg_temp_free_i32(t2); \
57951c27 7084 tcg_gen_concat_i32_i64(cpu_gpr[rD(ctx->opcode)], t0, t1); \
a7812ae4
PB
7085 tcg_temp_free_i32(t0); \
7086 tcg_temp_free_i32(t1); \
0487d6a8 7087}
57951c27
AJ
7088#else
7089#define GEN_SPEOP_ARITH2(name, tcg_op) \
636aa200 7090static inline void gen_##name(DisasContext *ctx) \
0487d6a8
JM
7091{ \
7092 if (unlikely(!ctx->spe_enabled)) { \
27a69bb0 7093 gen_exception(ctx, POWERPC_EXCP_SPEU); \
0487d6a8
JM
7094 return; \
7095 } \
57951c27
AJ
7096 tcg_op(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)], \
7097 cpu_gpr[rB(ctx->opcode)]); \
7098 tcg_op(cpu_gprh[rD(ctx->opcode)], cpu_gprh[rA(ctx->opcode)], \
7099 cpu_gprh[rB(ctx->opcode)]); \
0487d6a8 7100}
57951c27 7101#endif
0487d6a8 7102
636aa200 7103static inline void gen_op_evsrwu(TCGv_i32 ret, TCGv_i32 arg1, TCGv_i32 arg2)
57951c27 7104{
a7812ae4 7105 TCGv_i32 t0;
57951c27 7106 int l1, l2;
0487d6a8 7107
57951c27
AJ
7108 l1 = gen_new_label();
7109 l2 = gen_new_label();
a7812ae4 7110 t0 = tcg_temp_local_new_i32();
57951c27
AJ
7111 /* No error here: 6 bits are used */
7112 tcg_gen_andi_i32(t0, arg2, 0x3F);
7113 tcg_gen_brcondi_i32(TCG_COND_GE, t0, 32, l1);
7114 tcg_gen_shr_i32(ret, arg1, t0);
7115 tcg_gen_br(l2);
7116 gen_set_label(l1);
7117 tcg_gen_movi_i32(ret, 0);
0aef4261 7118 gen_set_label(l2);
a7812ae4 7119 tcg_temp_free_i32(t0);
57951c27
AJ
7120}
7121GEN_SPEOP_ARITH2(evsrwu, gen_op_evsrwu);
636aa200 7122static inline void gen_op_evsrws(TCGv_i32 ret, TCGv_i32 arg1, TCGv_i32 arg2)
57951c27 7123{
a7812ae4 7124 TCGv_i32 t0;
57951c27
AJ
7125 int l1, l2;
7126
7127 l1 = gen_new_label();
7128 l2 = gen_new_label();
a7812ae4 7129 t0 = tcg_temp_local_new_i32();
57951c27
AJ
7130 /* No error here: 6 bits are used */
7131 tcg_gen_andi_i32(t0, arg2, 0x3F);
7132 tcg_gen_brcondi_i32(TCG_COND_GE, t0, 32, l1);
7133 tcg_gen_sar_i32(ret, arg1, t0);
7134 tcg_gen_br(l2);
7135 gen_set_label(l1);
7136 tcg_gen_movi_i32(ret, 0);
0aef4261 7137 gen_set_label(l2);
a7812ae4 7138 tcg_temp_free_i32(t0);
57951c27
AJ
7139}
7140GEN_SPEOP_ARITH2(evsrws, gen_op_evsrws);
636aa200 7141static inline void gen_op_evslw(TCGv_i32 ret, TCGv_i32 arg1, TCGv_i32 arg2)
57951c27 7142{
a7812ae4 7143 TCGv_i32 t0;
57951c27
AJ
7144 int l1, l2;
7145
7146 l1 = gen_new_label();
7147 l2 = gen_new_label();
a7812ae4 7148 t0 = tcg_temp_local_new_i32();
57951c27
AJ
7149 /* No error here: 6 bits are used */
7150 tcg_gen_andi_i32(t0, arg2, 0x3F);
7151 tcg_gen_brcondi_i32(TCG_COND_GE, t0, 32, l1);
7152 tcg_gen_shl_i32(ret, arg1, t0);
7153 tcg_gen_br(l2);
7154 gen_set_label(l1);
7155 tcg_gen_movi_i32(ret, 0);
e29ef9fa 7156 gen_set_label(l2);
a7812ae4 7157 tcg_temp_free_i32(t0);
57951c27
AJ
7158}
7159GEN_SPEOP_ARITH2(evslw, gen_op_evslw);
636aa200 7160static inline void gen_op_evrlw(TCGv_i32 ret, TCGv_i32 arg1, TCGv_i32 arg2)
57951c27 7161{
a7812ae4 7162 TCGv_i32 t0 = tcg_temp_new_i32();
57951c27
AJ
7163 tcg_gen_andi_i32(t0, arg2, 0x1F);
7164 tcg_gen_rotl_i32(ret, arg1, t0);
a7812ae4 7165 tcg_temp_free_i32(t0);
57951c27
AJ
7166}
7167GEN_SPEOP_ARITH2(evrlw, gen_op_evrlw);
636aa200 7168static inline void gen_evmergehi(DisasContext *ctx)
57951c27
AJ
7169{
7170 if (unlikely(!ctx->spe_enabled)) {
27a69bb0 7171 gen_exception(ctx, POWERPC_EXCP_SPEU);
57951c27
AJ
7172 return;
7173 }
7174#if defined(TARGET_PPC64)
a7812ae4
PB
7175 TCGv t0 = tcg_temp_new();
7176 TCGv t1 = tcg_temp_new();
57951c27
AJ
7177 tcg_gen_shri_tl(t0, cpu_gpr[rB(ctx->opcode)], 32);
7178 tcg_gen_andi_tl(t1, cpu_gpr[rA(ctx->opcode)], 0xFFFFFFFF0000000ULL);
7179 tcg_gen_or_tl(cpu_gpr[rD(ctx->opcode)], t0, t1);
7180 tcg_temp_free(t0);
7181 tcg_temp_free(t1);
7182#else
7183 tcg_gen_mov_i32(cpu_gpr[rD(ctx->opcode)], cpu_gprh[rB(ctx->opcode)]);
7184 tcg_gen_mov_i32(cpu_gprh[rD(ctx->opcode)], cpu_gprh[rA(ctx->opcode)]);
7185#endif
7186}
7187GEN_SPEOP_ARITH2(evaddw, tcg_gen_add_i32);
636aa200 7188static inline void gen_op_evsubf(TCGv_i32 ret, TCGv_i32 arg1, TCGv_i32 arg2)
0487d6a8 7189{
57951c27
AJ
7190 tcg_gen_sub_i32(ret, arg2, arg1);
7191}
7192GEN_SPEOP_ARITH2(evsubfw, gen_op_evsubf);
0487d6a8 7193
57951c27
AJ
7194/* SPE arithmetic immediate */
7195#if defined(TARGET_PPC64)
7196#define GEN_SPEOP_ARITH_IMM2(name, tcg_op) \
636aa200 7197static inline void gen_##name(DisasContext *ctx) \
57951c27
AJ
7198{ \
7199 if (unlikely(!ctx->spe_enabled)) { \
27a69bb0 7200 gen_exception(ctx, POWERPC_EXCP_SPEU); \
57951c27
AJ
7201 return; \
7202 } \
a7812ae4
PB
7203 TCGv_i32 t0 = tcg_temp_local_new_i32(); \
7204 TCGv_i32 t1 = tcg_temp_local_new_i32(); \
7205 TCGv_i64 t2 = tcg_temp_local_new_i64(); \
57951c27
AJ
7206 tcg_gen_trunc_i64_i32(t0, cpu_gpr[rB(ctx->opcode)]); \
7207 tcg_op(t0, t0, rA(ctx->opcode)); \
7208 tcg_gen_shri_i64(t2, cpu_gpr[rB(ctx->opcode)], 32); \
7209 tcg_gen_trunc_i64_i32(t1, t2); \
e06fcd75 7210 tcg_temp_free_i64(t2); \
57951c27
AJ
7211 tcg_op(t1, t1, rA(ctx->opcode)); \
7212 tcg_gen_concat_i32_i64(cpu_gpr[rD(ctx->opcode)], t0, t1); \
a7812ae4
PB
7213 tcg_temp_free_i32(t0); \
7214 tcg_temp_free_i32(t1); \
57951c27
AJ
7215}
7216#else
7217#define GEN_SPEOP_ARITH_IMM2(name, tcg_op) \
636aa200 7218static inline void gen_##name(DisasContext *ctx) \
57951c27
AJ
7219{ \
7220 if (unlikely(!ctx->spe_enabled)) { \
27a69bb0 7221 gen_exception(ctx, POWERPC_EXCP_SPEU); \
57951c27
AJ
7222 return; \
7223 } \
7224 tcg_op(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rB(ctx->opcode)], \
7225 rA(ctx->opcode)); \
7226 tcg_op(cpu_gprh[rD(ctx->opcode)], cpu_gprh[rB(ctx->opcode)], \
7227 rA(ctx->opcode)); \
7228}
7229#endif
7230GEN_SPEOP_ARITH_IMM2(evaddiw, tcg_gen_addi_i32);
7231GEN_SPEOP_ARITH_IMM2(evsubifw, tcg_gen_subi_i32);
7232
7233/* SPE comparison */
7234#if defined(TARGET_PPC64)
7235#define GEN_SPEOP_COMP(name, tcg_cond) \
636aa200 7236static inline void gen_##name(DisasContext *ctx) \
57951c27
AJ
7237{ \
7238 if (unlikely(!ctx->spe_enabled)) { \
27a69bb0 7239 gen_exception(ctx, POWERPC_EXCP_SPEU); \
57951c27
AJ
7240 return; \
7241 } \
7242 int l1 = gen_new_label(); \
7243 int l2 = gen_new_label(); \
7244 int l3 = gen_new_label(); \
7245 int l4 = gen_new_label(); \
a7812ae4
PB
7246 TCGv_i32 t0 = tcg_temp_local_new_i32(); \
7247 TCGv_i32 t1 = tcg_temp_local_new_i32(); \
7248 TCGv_i64 t2 = tcg_temp_local_new_i64(); \
57951c27
AJ
7249 tcg_gen_trunc_i64_i32(t0, cpu_gpr[rA(ctx->opcode)]); \
7250 tcg_gen_trunc_i64_i32(t1, cpu_gpr[rB(ctx->opcode)]); \
7251 tcg_gen_brcond_i32(tcg_cond, t0, t1, l1); \
a7812ae4 7252 tcg_gen_movi_i32(cpu_crf[crfD(ctx->opcode)], 0); \
57951c27
AJ
7253 tcg_gen_br(l2); \
7254 gen_set_label(l1); \
7255 tcg_gen_movi_i32(cpu_crf[crfD(ctx->opcode)], \
7256 CRF_CL | CRF_CH_OR_CL | CRF_CH_AND_CL); \
7257 gen_set_label(l2); \
7258 tcg_gen_shri_i64(t2, cpu_gpr[rA(ctx->opcode)], 32); \
7259 tcg_gen_trunc_i64_i32(t0, t2); \
7260 tcg_gen_shri_i64(t2, cpu_gpr[rB(ctx->opcode)], 32); \
7261 tcg_gen_trunc_i64_i32(t1, t2); \
a7812ae4 7262 tcg_temp_free_i64(t2); \
57951c27
AJ
7263 tcg_gen_brcond_i32(tcg_cond, t0, t1, l3); \
7264 tcg_gen_andi_i32(cpu_crf[crfD(ctx->opcode)], cpu_crf[crfD(ctx->opcode)], \
7265 ~(CRF_CH | CRF_CH_AND_CL)); \
7266 tcg_gen_br(l4); \
7267 gen_set_label(l3); \
7268 tcg_gen_ori_i32(cpu_crf[crfD(ctx->opcode)], cpu_crf[crfD(ctx->opcode)], \
7269 CRF_CH | CRF_CH_OR_CL); \
7270 gen_set_label(l4); \
a7812ae4
PB
7271 tcg_temp_free_i32(t0); \
7272 tcg_temp_free_i32(t1); \
57951c27
AJ
7273}
7274#else
7275#define GEN_SPEOP_COMP(name, tcg_cond) \
636aa200 7276static inline void gen_##name(DisasContext *ctx) \
57951c27
AJ
7277{ \
7278 if (unlikely(!ctx->spe_enabled)) { \
27a69bb0 7279 gen_exception(ctx, POWERPC_EXCP_SPEU); \
57951c27
AJ
7280 return; \
7281 } \
7282 int l1 = gen_new_label(); \
7283 int l2 = gen_new_label(); \
7284 int l3 = gen_new_label(); \
7285 int l4 = gen_new_label(); \
7286 \
7287 tcg_gen_brcond_i32(tcg_cond, cpu_gpr[rA(ctx->opcode)], \
7288 cpu_gpr[rB(ctx->opcode)], l1); \
7289 tcg_gen_movi_tl(cpu_crf[crfD(ctx->opcode)], 0); \
7290 tcg_gen_br(l2); \
7291 gen_set_label(l1); \
7292 tcg_gen_movi_i32(cpu_crf[crfD(ctx->opcode)], \
7293 CRF_CL | CRF_CH_OR_CL | CRF_CH_AND_CL); \
7294 gen_set_label(l2); \
7295 tcg_gen_brcond_i32(tcg_cond, cpu_gprh[rA(ctx->opcode)], \
7296 cpu_gprh[rB(ctx->opcode)], l3); \
7297 tcg_gen_andi_i32(cpu_crf[crfD(ctx->opcode)], cpu_crf[crfD(ctx->opcode)], \
7298 ~(CRF_CH | CRF_CH_AND_CL)); \
7299 tcg_gen_br(l4); \
7300 gen_set_label(l3); \
7301 tcg_gen_ori_i32(cpu_crf[crfD(ctx->opcode)], cpu_crf[crfD(ctx->opcode)], \
7302 CRF_CH | CRF_CH_OR_CL); \
7303 gen_set_label(l4); \
7304}
7305#endif
7306GEN_SPEOP_COMP(evcmpgtu, TCG_COND_GTU);
7307GEN_SPEOP_COMP(evcmpgts, TCG_COND_GT);
7308GEN_SPEOP_COMP(evcmpltu, TCG_COND_LTU);
7309GEN_SPEOP_COMP(evcmplts, TCG_COND_LT);
7310GEN_SPEOP_COMP(evcmpeq, TCG_COND_EQ);
7311
7312/* SPE misc */
636aa200 7313static inline void gen_brinc(DisasContext *ctx)
57951c27
AJ
7314{
7315 /* Note: brinc is usable even if SPE is disabled */
a7812ae4
PB
7316 gen_helper_brinc(cpu_gpr[rD(ctx->opcode)],
7317 cpu_gpr[rA(ctx->opcode)], cpu_gpr[rB(ctx->opcode)]);
0487d6a8 7318}
636aa200 7319static inline void gen_evmergelo(DisasContext *ctx)
57951c27
AJ
7320{
7321 if (unlikely(!ctx->spe_enabled)) {
27a69bb0 7322 gen_exception(ctx, POWERPC_EXCP_SPEU);
57951c27
AJ
7323 return;
7324 }
7325#if defined(TARGET_PPC64)
a7812ae4
PB
7326 TCGv t0 = tcg_temp_new();
7327 TCGv t1 = tcg_temp_new();
17d9b3af 7328 tcg_gen_ext32u_tl(t0, cpu_gpr[rB(ctx->opcode)]);
57951c27
AJ
7329 tcg_gen_shli_tl(t1, cpu_gpr[rA(ctx->opcode)], 32);
7330 tcg_gen_or_tl(cpu_gpr[rD(ctx->opcode)], t0, t1);
7331 tcg_temp_free(t0);
7332 tcg_temp_free(t1);
7333#else
57951c27 7334 tcg_gen_mov_i32(cpu_gprh[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)]);
33890b3e 7335 tcg_gen_mov_i32(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rB(ctx->opcode)]);
57951c27
AJ
7336#endif
7337}
636aa200 7338static inline void gen_evmergehilo(DisasContext *ctx)
57951c27
AJ
7339{
7340 if (unlikely(!ctx->spe_enabled)) {
27a69bb0 7341 gen_exception(ctx, POWERPC_EXCP_SPEU);
57951c27
AJ
7342 return;
7343 }
7344#if defined(TARGET_PPC64)
a7812ae4
PB
7345 TCGv t0 = tcg_temp_new();
7346 TCGv t1 = tcg_temp_new();
17d9b3af 7347 tcg_gen_ext32u_tl(t0, cpu_gpr[rB(ctx->opcode)]);
57951c27
AJ
7348 tcg_gen_andi_tl(t1, cpu_gpr[rA(ctx->opcode)], 0xFFFFFFFF0000000ULL);
7349 tcg_gen_or_tl(cpu_gpr[rD(ctx->opcode)], t0, t1);
7350 tcg_temp_free(t0);
7351 tcg_temp_free(t1);
7352#else
7353 tcg_gen_mov_i32(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rB(ctx->opcode)]);
7354 tcg_gen_mov_i32(cpu_gprh[rD(ctx->opcode)], cpu_gprh[rA(ctx->opcode)]);
7355#endif
7356}
636aa200 7357static inline void gen_evmergelohi(DisasContext *ctx)
57951c27
AJ
7358{
7359 if (unlikely(!ctx->spe_enabled)) {
27a69bb0 7360 gen_exception(ctx, POWERPC_EXCP_SPEU);
57951c27
AJ
7361 return;
7362 }
7363#if defined(TARGET_PPC64)
a7812ae4
PB
7364 TCGv t0 = tcg_temp_new();
7365 TCGv t1 = tcg_temp_new();
57951c27
AJ
7366 tcg_gen_shri_tl(t0, cpu_gpr[rB(ctx->opcode)], 32);
7367 tcg_gen_shli_tl(t1, cpu_gpr[rA(ctx->opcode)], 32);
7368 tcg_gen_or_tl(cpu_gpr[rD(ctx->opcode)], t0, t1);
7369 tcg_temp_free(t0);
7370 tcg_temp_free(t1);
7371#else
33890b3e
NF
7372 if (rD(ctx->opcode) == rA(ctx->opcode)) {
7373 TCGv_i32 tmp = tcg_temp_new_i32();
7374 tcg_gen_mov_i32(tmp, cpu_gpr[rA(ctx->opcode)]);
7375 tcg_gen_mov_i32(cpu_gpr[rD(ctx->opcode)], cpu_gprh[rB(ctx->opcode)]);
7376 tcg_gen_mov_i32(cpu_gprh[rD(ctx->opcode)], tmp);
7377 tcg_temp_free_i32(tmp);
7378 } else {
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)], cpu_gpr[rA(ctx->opcode)]);
7381 }
57951c27
AJ
7382#endif
7383}
636aa200 7384static inline void gen_evsplati(DisasContext *ctx)
57951c27 7385{
ae01847f 7386 uint64_t imm = ((int32_t)(rA(ctx->opcode) << 27)) >> 27;
0487d6a8 7387
57951c27 7388#if defined(TARGET_PPC64)
38d14952 7389 tcg_gen_movi_tl(cpu_gpr[rD(ctx->opcode)], (imm << 32) | imm);
57951c27
AJ
7390#else
7391 tcg_gen_movi_i32(cpu_gpr[rD(ctx->opcode)], imm);
7392 tcg_gen_movi_i32(cpu_gprh[rD(ctx->opcode)], imm);
7393#endif
7394}
636aa200 7395static inline void gen_evsplatfi(DisasContext *ctx)
0487d6a8 7396{
ae01847f 7397 uint64_t imm = rA(ctx->opcode) << 27;
0487d6a8 7398
57951c27 7399#if defined(TARGET_PPC64)
38d14952 7400 tcg_gen_movi_tl(cpu_gpr[rD(ctx->opcode)], (imm << 32) | imm);
57951c27
AJ
7401#else
7402 tcg_gen_movi_i32(cpu_gpr[rD(ctx->opcode)], imm);
7403 tcg_gen_movi_i32(cpu_gprh[rD(ctx->opcode)], imm);
7404#endif
0487d6a8
JM
7405}
7406
636aa200 7407static inline void gen_evsel(DisasContext *ctx)
57951c27
AJ
7408{
7409 int l1 = gen_new_label();
7410 int l2 = gen_new_label();
7411 int l3 = gen_new_label();
7412 int l4 = gen_new_label();
a7812ae4 7413 TCGv_i32 t0 = tcg_temp_local_new_i32();
57951c27 7414#if defined(TARGET_PPC64)
a7812ae4
PB
7415 TCGv t1 = tcg_temp_local_new();
7416 TCGv t2 = tcg_temp_local_new();
57951c27
AJ
7417#endif
7418 tcg_gen_andi_i32(t0, cpu_crf[ctx->opcode & 0x07], 1 << 3);
7419 tcg_gen_brcondi_i32(TCG_COND_EQ, t0, 0, l1);
7420#if defined(TARGET_PPC64)
7421 tcg_gen_andi_tl(t1, cpu_gpr[rA(ctx->opcode)], 0xFFFFFFFF00000000ULL);
7422#else
7423 tcg_gen_mov_tl(cpu_gprh[rD(ctx->opcode)], cpu_gprh[rA(ctx->opcode)]);
7424#endif
7425 tcg_gen_br(l2);
7426 gen_set_label(l1);
7427#if defined(TARGET_PPC64)
7428 tcg_gen_andi_tl(t1, cpu_gpr[rB(ctx->opcode)], 0xFFFFFFFF00000000ULL);
7429#else
7430 tcg_gen_mov_tl(cpu_gprh[rD(ctx->opcode)], cpu_gprh[rB(ctx->opcode)]);
7431#endif
7432 gen_set_label(l2);
7433 tcg_gen_andi_i32(t0, cpu_crf[ctx->opcode & 0x07], 1 << 2);
7434 tcg_gen_brcondi_i32(TCG_COND_EQ, t0, 0, l3);
7435#if defined(TARGET_PPC64)
17d9b3af 7436 tcg_gen_ext32u_tl(t2, cpu_gpr[rA(ctx->opcode)]);
57951c27
AJ
7437#else
7438 tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)]);
7439#endif
7440 tcg_gen_br(l4);
7441 gen_set_label(l3);
7442#if defined(TARGET_PPC64)
17d9b3af 7443 tcg_gen_ext32u_tl(t2, cpu_gpr[rB(ctx->opcode)]);
57951c27
AJ
7444#else
7445 tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rB(ctx->opcode)]);
7446#endif
7447 gen_set_label(l4);
a7812ae4 7448 tcg_temp_free_i32(t0);
57951c27
AJ
7449#if defined(TARGET_PPC64)
7450 tcg_gen_or_tl(cpu_gpr[rD(ctx->opcode)], t1, t2);
7451 tcg_temp_free(t1);
7452 tcg_temp_free(t2);
7453#endif
7454}
e8eaa2c0
BS
7455
7456static void gen_evsel0(DisasContext *ctx)
57951c27
AJ
7457{
7458 gen_evsel(ctx);
7459}
e8eaa2c0
BS
7460
7461static void gen_evsel1(DisasContext *ctx)
57951c27
AJ
7462{
7463 gen_evsel(ctx);
7464}
e8eaa2c0
BS
7465
7466static void gen_evsel2(DisasContext *ctx)
57951c27
AJ
7467{
7468 gen_evsel(ctx);
7469}
e8eaa2c0
BS
7470
7471static void gen_evsel3(DisasContext *ctx)
57951c27
AJ
7472{
7473 gen_evsel(ctx);
7474}
0487d6a8 7475
a0e13900
FC
7476/* Multiply */
7477
7478static inline void gen_evmwumi(DisasContext *ctx)
7479{
7480 TCGv_i64 t0, t1;
7481
7482 if (unlikely(!ctx->spe_enabled)) {
27a69bb0 7483 gen_exception(ctx, POWERPC_EXCP_SPEU);
a0e13900
FC
7484 return;
7485 }
7486
7487 t0 = tcg_temp_new_i64();
7488 t1 = tcg_temp_new_i64();
7489
7490 /* t0 := rA; t1 := rB */
7491#if defined(TARGET_PPC64)
7492 tcg_gen_ext32u_tl(t0, cpu_gpr[rA(ctx->opcode)]);
7493 tcg_gen_ext32u_tl(t1, cpu_gpr[rB(ctx->opcode)]);
7494#else
7495 tcg_gen_extu_tl_i64(t0, cpu_gpr[rA(ctx->opcode)]);
7496 tcg_gen_extu_tl_i64(t1, cpu_gpr[rB(ctx->opcode)]);
7497#endif
7498
7499 tcg_gen_mul_i64(t0, t0, t1); /* t0 := rA * rB */
7500
7501 gen_store_gpr64(rD(ctx->opcode), t0); /* rD := t0 */
7502
7503 tcg_temp_free_i64(t0);
7504 tcg_temp_free_i64(t1);
7505}
7506
7507static inline void gen_evmwumia(DisasContext *ctx)
7508{
7509 TCGv_i64 tmp;
7510
7511 if (unlikely(!ctx->spe_enabled)) {
27a69bb0 7512 gen_exception(ctx, POWERPC_EXCP_SPEU);
a0e13900
FC
7513 return;
7514 }
7515
7516 gen_evmwumi(ctx); /* rD := rA * rB */
7517
7518 tmp = tcg_temp_new_i64();
7519
7520 /* acc := rD */
7521 gen_load_gpr64(tmp, rD(ctx->opcode));
1328c2bf 7522 tcg_gen_st_i64(tmp, cpu_env, offsetof(CPUPPCState, spe_acc));
a0e13900
FC
7523 tcg_temp_free_i64(tmp);
7524}
7525
7526static inline void gen_evmwumiaa(DisasContext *ctx)
7527{
7528 TCGv_i64 acc;
7529 TCGv_i64 tmp;
7530
7531 if (unlikely(!ctx->spe_enabled)) {
27a69bb0 7532 gen_exception(ctx, POWERPC_EXCP_SPEU);
a0e13900
FC
7533 return;
7534 }
7535
7536 gen_evmwumi(ctx); /* rD := rA * rB */
7537
7538 acc = tcg_temp_new_i64();
7539 tmp = tcg_temp_new_i64();
7540
7541 /* tmp := rD */
7542 gen_load_gpr64(tmp, rD(ctx->opcode));
7543
7544 /* Load acc */
1328c2bf 7545 tcg_gen_ld_i64(acc, cpu_env, offsetof(CPUPPCState, spe_acc));
a0e13900
FC
7546
7547 /* acc := tmp + acc */
7548 tcg_gen_add_i64(acc, acc, tmp);
7549
7550 /* Store acc */
1328c2bf 7551 tcg_gen_st_i64(acc, cpu_env, offsetof(CPUPPCState, spe_acc));
a0e13900
FC
7552
7553 /* rD := acc */
7554 gen_store_gpr64(rD(ctx->opcode), acc);
7555
7556 tcg_temp_free_i64(acc);
7557 tcg_temp_free_i64(tmp);
7558}
7559
7560static inline void gen_evmwsmi(DisasContext *ctx)
7561{
7562 TCGv_i64 t0, t1;
7563
7564 if (unlikely(!ctx->spe_enabled)) {
27a69bb0 7565 gen_exception(ctx, POWERPC_EXCP_SPEU);
a0e13900
FC
7566 return;
7567 }
7568
7569 t0 = tcg_temp_new_i64();
7570 t1 = tcg_temp_new_i64();
7571
7572 /* t0 := rA; t1 := rB */
7573#if defined(TARGET_PPC64)
7574 tcg_gen_ext32s_tl(t0, cpu_gpr[rA(ctx->opcode)]);
7575 tcg_gen_ext32s_tl(t1, cpu_gpr[rB(ctx->opcode)]);
7576#else
7577 tcg_gen_ext_tl_i64(t0, cpu_gpr[rA(ctx->opcode)]);
7578 tcg_gen_ext_tl_i64(t1, cpu_gpr[rB(ctx->opcode)]);
7579#endif
7580
7581 tcg_gen_mul_i64(t0, t0, t1); /* t0 := rA * rB */
7582
7583 gen_store_gpr64(rD(ctx->opcode), t0); /* rD := t0 */
7584
7585 tcg_temp_free_i64(t0);
7586 tcg_temp_free_i64(t1);
7587}
7588
7589static inline void gen_evmwsmia(DisasContext *ctx)
7590{
7591 TCGv_i64 tmp;
7592
7593 gen_evmwsmi(ctx); /* rD := rA * rB */
7594
7595 tmp = tcg_temp_new_i64();
7596
7597 /* acc := rD */
7598 gen_load_gpr64(tmp, rD(ctx->opcode));
1328c2bf 7599 tcg_gen_st_i64(tmp, cpu_env, offsetof(CPUPPCState, spe_acc));
a0e13900
FC
7600
7601 tcg_temp_free_i64(tmp);
7602}
7603
7604static inline void gen_evmwsmiaa(DisasContext *ctx)
7605{
7606 TCGv_i64 acc = tcg_temp_new_i64();
7607 TCGv_i64 tmp = tcg_temp_new_i64();
7608
7609 gen_evmwsmi(ctx); /* rD := rA * rB */
7610
7611 acc = tcg_temp_new_i64();
7612 tmp = tcg_temp_new_i64();
7613
7614 /* tmp := rD */
7615 gen_load_gpr64(tmp, rD(ctx->opcode));
7616
7617 /* Load acc */
1328c2bf 7618 tcg_gen_ld_i64(acc, cpu_env, offsetof(CPUPPCState, spe_acc));
a0e13900
FC
7619
7620 /* acc := tmp + acc */
7621 tcg_gen_add_i64(acc, acc, tmp);
7622
7623 /* Store acc */
1328c2bf 7624 tcg_gen_st_i64(acc, cpu_env, offsetof(CPUPPCState, spe_acc));
a0e13900
FC
7625
7626 /* rD := acc */
7627 gen_store_gpr64(rD(ctx->opcode), acc);
7628
7629 tcg_temp_free_i64(acc);
7630 tcg_temp_free_i64(tmp);
7631}
7632
70560da7
FC
7633GEN_SPE(evaddw, speundef, 0x00, 0x08, 0x00000000, 0xFFFFFFFF, PPC_SPE); ////
7634GEN_SPE(evaddiw, speundef, 0x01, 0x08, 0x00000000, 0xFFFFFFFF, PPC_SPE);
7635GEN_SPE(evsubfw, speundef, 0x02, 0x08, 0x00000000, 0xFFFFFFFF, PPC_SPE); ////
7636GEN_SPE(evsubifw, speundef, 0x03, 0x08, 0x00000000, 0xFFFFFFFF, PPC_SPE);
7637GEN_SPE(evabs, evneg, 0x04, 0x08, 0x0000F800, 0x0000F800, PPC_SPE); ////
7638GEN_SPE(evextsb, evextsh, 0x05, 0x08, 0x0000F800, 0x0000F800, PPC_SPE); ////
7639GEN_SPE(evrndw, evcntlzw, 0x06, 0x08, 0x0000F800, 0x0000F800, PPC_SPE); ////
7640GEN_SPE(evcntlsw, brinc, 0x07, 0x08, 0x0000F800, 0x00000000, PPC_SPE); //
7641GEN_SPE(evmra, speundef, 0x02, 0x13, 0x0000F800, 0xFFFFFFFF, PPC_SPE);
7642GEN_SPE(speundef, evand, 0x08, 0x08, 0xFFFFFFFF, 0x00000000, PPC_SPE); ////
7643GEN_SPE(evandc, speundef, 0x09, 0x08, 0x00000000, 0xFFFFFFFF, PPC_SPE); ////
7644GEN_SPE(evxor, evor, 0x0B, 0x08, 0x00000000, 0x00000000, PPC_SPE); ////
7645GEN_SPE(evnor, eveqv, 0x0C, 0x08, 0x00000000, 0x00000000, PPC_SPE); ////
7646GEN_SPE(evmwumi, evmwsmi, 0x0C, 0x11, 0x00000000, 0x00000000, PPC_SPE);
7647GEN_SPE(evmwumia, evmwsmia, 0x1C, 0x11, 0x00000000, 0x00000000, PPC_SPE);
7648GEN_SPE(evmwumiaa, evmwsmiaa, 0x0C, 0x15, 0x00000000, 0x00000000, PPC_SPE);
7649GEN_SPE(speundef, evorc, 0x0D, 0x08, 0xFFFFFFFF, 0x00000000, PPC_SPE); ////
7650GEN_SPE(evnand, speundef, 0x0F, 0x08, 0x00000000, 0xFFFFFFFF, PPC_SPE); ////
7651GEN_SPE(evsrwu, evsrws, 0x10, 0x08, 0x00000000, 0x00000000, PPC_SPE); ////
7652GEN_SPE(evsrwiu, evsrwis, 0x11, 0x08, 0x00000000, 0x00000000, PPC_SPE);
7653GEN_SPE(evslw, speundef, 0x12, 0x08, 0x00000000, 0xFFFFFFFF, PPC_SPE); ////
7654GEN_SPE(evslwi, speundef, 0x13, 0x08, 0x00000000, 0xFFFFFFFF, PPC_SPE);
7655GEN_SPE(evrlw, evsplati, 0x14, 0x08, 0x00000000, 0x0000F800, PPC_SPE); //
7656GEN_SPE(evrlwi, evsplatfi, 0x15, 0x08, 0x00000000, 0x0000F800, PPC_SPE);
7657GEN_SPE(evmergehi, evmergelo, 0x16, 0x08, 0x00000000, 0x00000000, PPC_SPE); ////
7658GEN_SPE(evmergehilo, evmergelohi, 0x17, 0x08, 0x00000000, 0x00000000, PPC_SPE); ////
7659GEN_SPE(evcmpgtu, evcmpgts, 0x18, 0x08, 0x00600000, 0x00600000, PPC_SPE); ////
7660GEN_SPE(evcmpltu, evcmplts, 0x19, 0x08, 0x00600000, 0x00600000, PPC_SPE); ////
7661GEN_SPE(evcmpeq, speundef, 0x1A, 0x08, 0x00600000, 0xFFFFFFFF, PPC_SPE); ////
0487d6a8 7662
6a6ae23f 7663/* SPE load and stores */
636aa200 7664static inline void gen_addr_spe_imm_index(DisasContext *ctx, TCGv EA, int sh)
6a6ae23f
AJ
7665{
7666 target_ulong uimm = rB(ctx->opcode);
7667
76db3ba4 7668 if (rA(ctx->opcode) == 0) {
6a6ae23f 7669 tcg_gen_movi_tl(EA, uimm << sh);
76db3ba4 7670 } else {
6a6ae23f 7671 tcg_gen_addi_tl(EA, cpu_gpr[rA(ctx->opcode)], uimm << sh);
76db3ba4
AJ
7672#if defined(TARGET_PPC64)
7673 if (!ctx->sf_mode) {
7674 tcg_gen_ext32u_tl(EA, EA);
7675 }
7676#endif
7677 }
0487d6a8 7678}
6a6ae23f 7679
636aa200 7680static inline void gen_op_evldd(DisasContext *ctx, TCGv addr)
6a6ae23f
AJ
7681{
7682#if defined(TARGET_PPC64)
76db3ba4 7683 gen_qemu_ld64(ctx, cpu_gpr[rD(ctx->opcode)], addr);
6a6ae23f
AJ
7684#else
7685 TCGv_i64 t0 = tcg_temp_new_i64();
76db3ba4 7686 gen_qemu_ld64(ctx, t0, addr);
6a6ae23f
AJ
7687 tcg_gen_trunc_i64_i32(cpu_gpr[rD(ctx->opcode)], t0);
7688 tcg_gen_shri_i64(t0, t0, 32);
7689 tcg_gen_trunc_i64_i32(cpu_gprh[rD(ctx->opcode)], t0);
7690 tcg_temp_free_i64(t0);
7691#endif
0487d6a8 7692}
6a6ae23f 7693
636aa200 7694static inline void gen_op_evldw(DisasContext *ctx, TCGv addr)
6a6ae23f 7695{
0487d6a8 7696#if defined(TARGET_PPC64)
6a6ae23f 7697 TCGv t0 = tcg_temp_new();
76db3ba4 7698 gen_qemu_ld32u(ctx, t0, addr);
6a6ae23f 7699 tcg_gen_shli_tl(cpu_gpr[rD(ctx->opcode)], t0, 32);
76db3ba4
AJ
7700 gen_addr_add(ctx, addr, addr, 4);
7701 gen_qemu_ld32u(ctx, t0, addr);
6a6ae23f
AJ
7702 tcg_gen_or_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rD(ctx->opcode)], t0);
7703 tcg_temp_free(t0);
7704#else
76db3ba4
AJ
7705 gen_qemu_ld32u(ctx, cpu_gprh[rD(ctx->opcode)], addr);
7706 gen_addr_add(ctx, addr, addr, 4);
7707 gen_qemu_ld32u(ctx, cpu_gpr[rD(ctx->opcode)], addr);
6a6ae23f 7708#endif
0487d6a8 7709}
6a6ae23f 7710
636aa200 7711static inline void gen_op_evldh(DisasContext *ctx, TCGv addr)
6a6ae23f
AJ
7712{
7713 TCGv t0 = tcg_temp_new();
7714#if defined(TARGET_PPC64)
76db3ba4 7715 gen_qemu_ld16u(ctx, t0, addr);
6a6ae23f 7716 tcg_gen_shli_tl(cpu_gpr[rD(ctx->opcode)], t0, 48);
76db3ba4
AJ
7717 gen_addr_add(ctx, addr, addr, 2);
7718 gen_qemu_ld16u(ctx, t0, addr);
6a6ae23f
AJ
7719 tcg_gen_shli_tl(t0, t0, 32);
7720 tcg_gen_or_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rD(ctx->opcode)], t0);
76db3ba4
AJ
7721 gen_addr_add(ctx, addr, addr, 2);
7722 gen_qemu_ld16u(ctx, t0, addr);
6a6ae23f
AJ
7723 tcg_gen_shli_tl(t0, t0, 16);
7724 tcg_gen_or_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rD(ctx->opcode)], t0);
76db3ba4
AJ
7725 gen_addr_add(ctx, addr, addr, 2);
7726 gen_qemu_ld16u(ctx, t0, addr);
6a6ae23f 7727 tcg_gen_or_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rD(ctx->opcode)], t0);
0487d6a8 7728#else
76db3ba4 7729 gen_qemu_ld16u(ctx, t0, addr);
6a6ae23f 7730 tcg_gen_shli_tl(cpu_gprh[rD(ctx->opcode)], t0, 16);
76db3ba4
AJ
7731 gen_addr_add(ctx, addr, addr, 2);
7732 gen_qemu_ld16u(ctx, t0, addr);
6a6ae23f 7733 tcg_gen_or_tl(cpu_gprh[rD(ctx->opcode)], cpu_gprh[rD(ctx->opcode)], t0);
76db3ba4
AJ
7734 gen_addr_add(ctx, addr, addr, 2);
7735 gen_qemu_ld16u(ctx, t0, addr);
6a6ae23f 7736 tcg_gen_shli_tl(cpu_gprh[rD(ctx->opcode)], t0, 16);
76db3ba4
AJ
7737 gen_addr_add(ctx, addr, addr, 2);
7738 gen_qemu_ld16u(ctx, t0, addr);
6a6ae23f 7739 tcg_gen_or_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rD(ctx->opcode)], t0);
0487d6a8 7740#endif
6a6ae23f 7741 tcg_temp_free(t0);
0487d6a8
JM
7742}
7743
636aa200 7744static inline void gen_op_evlhhesplat(DisasContext *ctx, TCGv addr)
6a6ae23f
AJ
7745{
7746 TCGv t0 = tcg_temp_new();
76db3ba4 7747 gen_qemu_ld16u(ctx, t0, addr);
6a6ae23f
AJ
7748#if defined(TARGET_PPC64)
7749 tcg_gen_shli_tl(cpu_gpr[rD(ctx->opcode)], t0, 48);
7750 tcg_gen_shli_tl(t0, t0, 16);
7751 tcg_gen_or_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rD(ctx->opcode)], t0);
7752#else
7753 tcg_gen_shli_tl(t0, t0, 16);
7754 tcg_gen_mov_tl(cpu_gprh[rD(ctx->opcode)], t0);
7755 tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], t0);
7756#endif
7757 tcg_temp_free(t0);
0487d6a8
JM
7758}
7759
636aa200 7760static inline void gen_op_evlhhousplat(DisasContext *ctx, TCGv addr)
6a6ae23f
AJ
7761{
7762 TCGv t0 = tcg_temp_new();
76db3ba4 7763 gen_qemu_ld16u(ctx, t0, addr);
6a6ae23f
AJ
7764#if defined(TARGET_PPC64)
7765 tcg_gen_shli_tl(cpu_gpr[rD(ctx->opcode)], t0, 32);
7766 tcg_gen_or_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rD(ctx->opcode)], t0);
7767#else
7768 tcg_gen_mov_tl(cpu_gprh[rD(ctx->opcode)], t0);
7769 tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], t0);
7770#endif
7771 tcg_temp_free(t0);
0487d6a8
JM
7772}
7773
636aa200 7774static inline void gen_op_evlhhossplat(DisasContext *ctx, TCGv addr)
6a6ae23f
AJ
7775{
7776 TCGv t0 = tcg_temp_new();
76db3ba4 7777 gen_qemu_ld16s(ctx, t0, addr);
6a6ae23f
AJ
7778#if defined(TARGET_PPC64)
7779 tcg_gen_shli_tl(cpu_gpr[rD(ctx->opcode)], t0, 32);
7780 tcg_gen_ext32u_tl(t0, t0);
7781 tcg_gen_or_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rD(ctx->opcode)], t0);
7782#else
7783 tcg_gen_mov_tl(cpu_gprh[rD(ctx->opcode)], t0);
7784 tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], t0);
7785#endif
7786 tcg_temp_free(t0);
7787}
7788
636aa200 7789static inline void gen_op_evlwhe(DisasContext *ctx, TCGv addr)
6a6ae23f
AJ
7790{
7791 TCGv t0 = tcg_temp_new();
7792#if defined(TARGET_PPC64)
76db3ba4 7793 gen_qemu_ld16u(ctx, t0, addr);
6a6ae23f 7794 tcg_gen_shli_tl(cpu_gpr[rD(ctx->opcode)], t0, 48);
76db3ba4
AJ
7795 gen_addr_add(ctx, addr, addr, 2);
7796 gen_qemu_ld16u(ctx, t0, addr);
6a6ae23f
AJ
7797 tcg_gen_shli_tl(t0, t0, 16);
7798 tcg_gen_or_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rD(ctx->opcode)], t0);
7799#else
76db3ba4 7800 gen_qemu_ld16u(ctx, t0, addr);
6a6ae23f 7801 tcg_gen_shli_tl(cpu_gprh[rD(ctx->opcode)], t0, 16);
76db3ba4
AJ
7802 gen_addr_add(ctx, addr, addr, 2);
7803 gen_qemu_ld16u(ctx, t0, addr);
6a6ae23f
AJ
7804 tcg_gen_shli_tl(cpu_gpr[rD(ctx->opcode)], t0, 16);
7805#endif
7806 tcg_temp_free(t0);
7807}
7808
636aa200 7809static inline void gen_op_evlwhou(DisasContext *ctx, TCGv addr)
6a6ae23f
AJ
7810{
7811#if defined(TARGET_PPC64)
7812 TCGv t0 = tcg_temp_new();
76db3ba4
AJ
7813 gen_qemu_ld16u(ctx, cpu_gpr[rD(ctx->opcode)], addr);
7814 gen_addr_add(ctx, addr, addr, 2);
7815 gen_qemu_ld16u(ctx, t0, addr);
6a6ae23f
AJ
7816 tcg_gen_shli_tl(t0, t0, 32);
7817 tcg_gen_or_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rD(ctx->opcode)], t0);
7818 tcg_temp_free(t0);
7819#else
76db3ba4
AJ
7820 gen_qemu_ld16u(ctx, cpu_gprh[rD(ctx->opcode)], addr);
7821 gen_addr_add(ctx, addr, addr, 2);
7822 gen_qemu_ld16u(ctx, cpu_gpr[rD(ctx->opcode)], addr);
6a6ae23f
AJ
7823#endif
7824}
7825
636aa200 7826static inline void gen_op_evlwhos(DisasContext *ctx, TCGv addr)
6a6ae23f
AJ
7827{
7828#if defined(TARGET_PPC64)
7829 TCGv t0 = tcg_temp_new();
76db3ba4 7830 gen_qemu_ld16s(ctx, t0, addr);
6a6ae23f 7831 tcg_gen_ext32u_tl(cpu_gpr[rD(ctx->opcode)], t0);
76db3ba4
AJ
7832 gen_addr_add(ctx, addr, addr, 2);
7833 gen_qemu_ld16s(ctx, t0, addr);
6a6ae23f
AJ
7834 tcg_gen_shli_tl(t0, t0, 32);
7835 tcg_gen_or_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rD(ctx->opcode)], t0);
7836 tcg_temp_free(t0);
7837#else
76db3ba4
AJ
7838 gen_qemu_ld16s(ctx, cpu_gprh[rD(ctx->opcode)], addr);
7839 gen_addr_add(ctx, addr, addr, 2);
7840 gen_qemu_ld16s(ctx, cpu_gpr[rD(ctx->opcode)], addr);
6a6ae23f
AJ
7841#endif
7842}
7843
636aa200 7844static inline void gen_op_evlwwsplat(DisasContext *ctx, TCGv addr)
6a6ae23f
AJ
7845{
7846 TCGv t0 = tcg_temp_new();
76db3ba4 7847 gen_qemu_ld32u(ctx, t0, addr);
0487d6a8 7848#if defined(TARGET_PPC64)
6a6ae23f
AJ
7849 tcg_gen_shli_tl(cpu_gpr[rD(ctx->opcode)], t0, 32);
7850 tcg_gen_or_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rD(ctx->opcode)], t0);
7851#else
7852 tcg_gen_mov_tl(cpu_gprh[rD(ctx->opcode)], t0);
7853 tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], t0);
7854#endif
7855 tcg_temp_free(t0);
7856}
7857
636aa200 7858static inline void gen_op_evlwhsplat(DisasContext *ctx, TCGv addr)
6a6ae23f
AJ
7859{
7860 TCGv t0 = tcg_temp_new();
7861#if defined(TARGET_PPC64)
76db3ba4 7862 gen_qemu_ld16u(ctx, t0, addr);
6a6ae23f
AJ
7863 tcg_gen_shli_tl(cpu_gpr[rD(ctx->opcode)], t0, 48);
7864 tcg_gen_shli_tl(t0, t0, 32);
7865 tcg_gen_or_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rD(ctx->opcode)], t0);
76db3ba4
AJ
7866 gen_addr_add(ctx, addr, addr, 2);
7867 gen_qemu_ld16u(ctx, t0, addr);
6a6ae23f
AJ
7868 tcg_gen_or_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rD(ctx->opcode)], t0);
7869 tcg_gen_shli_tl(t0, t0, 16);
7870 tcg_gen_or_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rD(ctx->opcode)], t0);
7871#else
76db3ba4 7872 gen_qemu_ld16u(ctx, t0, addr);
6a6ae23f
AJ
7873 tcg_gen_shli_tl(cpu_gprh[rD(ctx->opcode)], t0, 16);
7874 tcg_gen_or_tl(cpu_gprh[rD(ctx->opcode)], cpu_gprh[rD(ctx->opcode)], t0);
76db3ba4
AJ
7875 gen_addr_add(ctx, addr, addr, 2);
7876 gen_qemu_ld16u(ctx, t0, addr);
6a6ae23f
AJ
7877 tcg_gen_shli_tl(cpu_gpr[rD(ctx->opcode)], t0, 16);
7878 tcg_gen_or_tl(cpu_gpr[rD(ctx->opcode)], cpu_gprh[rD(ctx->opcode)], t0);
0487d6a8 7879#endif
6a6ae23f
AJ
7880 tcg_temp_free(t0);
7881}
7882
636aa200 7883static inline void gen_op_evstdd(DisasContext *ctx, TCGv addr)
6a6ae23f
AJ
7884{
7885#if defined(TARGET_PPC64)
76db3ba4 7886 gen_qemu_st64(ctx, cpu_gpr[rS(ctx->opcode)], addr);
0487d6a8 7887#else
6a6ae23f
AJ
7888 TCGv_i64 t0 = tcg_temp_new_i64();
7889 tcg_gen_concat_i32_i64(t0, cpu_gpr[rS(ctx->opcode)], cpu_gprh[rS(ctx->opcode)]);
76db3ba4 7890 gen_qemu_st64(ctx, t0, addr);
6a6ae23f
AJ
7891 tcg_temp_free_i64(t0);
7892#endif
7893}
7894
636aa200 7895static inline void gen_op_evstdw(DisasContext *ctx, TCGv addr)
6a6ae23f 7896{
0487d6a8 7897#if defined(TARGET_PPC64)
6a6ae23f
AJ
7898 TCGv t0 = tcg_temp_new();
7899 tcg_gen_shri_tl(t0, cpu_gpr[rS(ctx->opcode)], 32);
76db3ba4 7900 gen_qemu_st32(ctx, t0, addr);
6a6ae23f
AJ
7901 tcg_temp_free(t0);
7902#else
76db3ba4 7903 gen_qemu_st32(ctx, cpu_gprh[rS(ctx->opcode)], addr);
6a6ae23f 7904#endif
76db3ba4
AJ
7905 gen_addr_add(ctx, addr, addr, 4);
7906 gen_qemu_st32(ctx, cpu_gpr[rS(ctx->opcode)], addr);
6a6ae23f
AJ
7907}
7908
636aa200 7909static inline void gen_op_evstdh(DisasContext *ctx, TCGv addr)
6a6ae23f
AJ
7910{
7911 TCGv t0 = tcg_temp_new();
7912#if defined(TARGET_PPC64)
7913 tcg_gen_shri_tl(t0, cpu_gpr[rS(ctx->opcode)], 48);
7914#else
7915 tcg_gen_shri_tl(t0, cpu_gprh[rS(ctx->opcode)], 16);
7916#endif
76db3ba4
AJ
7917 gen_qemu_st16(ctx, t0, addr);
7918 gen_addr_add(ctx, addr, addr, 2);
6a6ae23f
AJ
7919#if defined(TARGET_PPC64)
7920 tcg_gen_shri_tl(t0, cpu_gpr[rS(ctx->opcode)], 32);
76db3ba4 7921 gen_qemu_st16(ctx, t0, addr);
6a6ae23f 7922#else
76db3ba4 7923 gen_qemu_st16(ctx, cpu_gprh[rS(ctx->opcode)], addr);
6a6ae23f 7924#endif
76db3ba4 7925 gen_addr_add(ctx, addr, addr, 2);
6a6ae23f 7926 tcg_gen_shri_tl(t0, cpu_gpr[rS(ctx->opcode)], 16);
76db3ba4 7927 gen_qemu_st16(ctx, t0, addr);
6a6ae23f 7928 tcg_temp_free(t0);
76db3ba4
AJ
7929 gen_addr_add(ctx, addr, addr, 2);
7930 gen_qemu_st16(ctx, cpu_gpr[rS(ctx->opcode)], addr);
6a6ae23f
AJ
7931}
7932
636aa200 7933static inline void gen_op_evstwhe(DisasContext *ctx, TCGv addr)
6a6ae23f
AJ
7934{
7935 TCGv t0 = tcg_temp_new();
7936#if defined(TARGET_PPC64)
7937 tcg_gen_shri_tl(t0, cpu_gpr[rS(ctx->opcode)], 48);
7938#else
7939 tcg_gen_shri_tl(t0, cpu_gprh[rS(ctx->opcode)], 16);
7940#endif
76db3ba4
AJ
7941 gen_qemu_st16(ctx, t0, addr);
7942 gen_addr_add(ctx, addr, addr, 2);
6a6ae23f 7943 tcg_gen_shri_tl(t0, cpu_gpr[rS(ctx->opcode)], 16);
76db3ba4 7944 gen_qemu_st16(ctx, t0, addr);
6a6ae23f
AJ
7945 tcg_temp_free(t0);
7946}
7947
636aa200 7948static inline void gen_op_evstwho(DisasContext *ctx, TCGv addr)
6a6ae23f
AJ
7949{
7950#if defined(TARGET_PPC64)
7951 TCGv t0 = tcg_temp_new();
7952 tcg_gen_shri_tl(t0, cpu_gpr[rS(ctx->opcode)], 32);
76db3ba4 7953 gen_qemu_st16(ctx, t0, addr);
6a6ae23f
AJ
7954 tcg_temp_free(t0);
7955#else
76db3ba4 7956 gen_qemu_st16(ctx, cpu_gprh[rS(ctx->opcode)], addr);
6a6ae23f 7957#endif
76db3ba4
AJ
7958 gen_addr_add(ctx, addr, addr, 2);
7959 gen_qemu_st16(ctx, cpu_gpr[rS(ctx->opcode)], addr);
6a6ae23f
AJ
7960}
7961
636aa200 7962static inline void gen_op_evstwwe(DisasContext *ctx, TCGv addr)
6a6ae23f
AJ
7963{
7964#if defined(TARGET_PPC64)
7965 TCGv t0 = tcg_temp_new();
7966 tcg_gen_shri_tl(t0, cpu_gpr[rS(ctx->opcode)], 32);
76db3ba4 7967 gen_qemu_st32(ctx, t0, addr);
6a6ae23f
AJ
7968 tcg_temp_free(t0);
7969#else
76db3ba4 7970 gen_qemu_st32(ctx, cpu_gprh[rS(ctx->opcode)], addr);
6a6ae23f
AJ
7971#endif
7972}
7973
636aa200 7974static inline void gen_op_evstwwo(DisasContext *ctx, TCGv addr)
6a6ae23f 7975{
76db3ba4 7976 gen_qemu_st32(ctx, cpu_gpr[rS(ctx->opcode)], addr);
6a6ae23f
AJ
7977}
7978
7979#define GEN_SPEOP_LDST(name, opc2, sh) \
99e300ef 7980static void glue(gen_, name)(DisasContext *ctx) \
6a6ae23f
AJ
7981{ \
7982 TCGv t0; \
7983 if (unlikely(!ctx->spe_enabled)) { \
27a69bb0 7984 gen_exception(ctx, POWERPC_EXCP_SPEU); \
6a6ae23f
AJ
7985 return; \
7986 } \
76db3ba4 7987 gen_set_access_type(ctx, ACCESS_INT); \
6a6ae23f
AJ
7988 t0 = tcg_temp_new(); \
7989 if (Rc(ctx->opcode)) { \
76db3ba4 7990 gen_addr_spe_imm_index(ctx, t0, sh); \
6a6ae23f 7991 } else { \
76db3ba4 7992 gen_addr_reg_index(ctx, t0); \
6a6ae23f
AJ
7993 } \
7994 gen_op_##name(ctx, t0); \
7995 tcg_temp_free(t0); \
7996}
7997
7998GEN_SPEOP_LDST(evldd, 0x00, 3);
7999GEN_SPEOP_LDST(evldw, 0x01, 3);
8000GEN_SPEOP_LDST(evldh, 0x02, 3);
8001GEN_SPEOP_LDST(evlhhesplat, 0x04, 1);
8002GEN_SPEOP_LDST(evlhhousplat, 0x06, 1);
8003GEN_SPEOP_LDST(evlhhossplat, 0x07, 1);
8004GEN_SPEOP_LDST(evlwhe, 0x08, 2);
8005GEN_SPEOP_LDST(evlwhou, 0x0A, 2);
8006GEN_SPEOP_LDST(evlwhos, 0x0B, 2);
8007GEN_SPEOP_LDST(evlwwsplat, 0x0C, 2);
8008GEN_SPEOP_LDST(evlwhsplat, 0x0E, 2);
8009
8010GEN_SPEOP_LDST(evstdd, 0x10, 3);
8011GEN_SPEOP_LDST(evstdw, 0x11, 3);
8012GEN_SPEOP_LDST(evstdh, 0x12, 3);
8013GEN_SPEOP_LDST(evstwhe, 0x18, 2);
8014GEN_SPEOP_LDST(evstwho, 0x1A, 2);
8015GEN_SPEOP_LDST(evstwwe, 0x1C, 2);
8016GEN_SPEOP_LDST(evstwwo, 0x1E, 2);
0487d6a8
JM
8017
8018/* Multiply and add - TODO */
8019#if 0
70560da7
FC
8020GEN_SPE(speundef, evmhessf, 0x01, 0x10, 0xFFFFFFFF, 0x00000000, PPC_SPE);//
8021GEN_SPE(speundef, evmhossf, 0x03, 0x10, 0xFFFFFFFF, 0x00000000, PPC_SPE);
8022GEN_SPE(evmheumi, evmhesmi, 0x04, 0x10, 0x00000000, 0x00000000, PPC_SPE);
8023GEN_SPE(speundef, evmhesmf, 0x05, 0x10, 0xFFFFFFFF, 0x00000000, PPC_SPE);
8024GEN_SPE(evmhoumi, evmhosmi, 0x06, 0x10, 0x00000000, 0x00000000, PPC_SPE);
8025GEN_SPE(speundef, evmhosmf, 0x07, 0x10, 0xFFFFFFFF, 0x00000000, PPC_SPE);
8026GEN_SPE(speundef, evmhessfa, 0x11, 0x10, 0xFFFFFFFF, 0x00000000, PPC_SPE);
8027GEN_SPE(speundef, evmhossfa, 0x13, 0x10, 0xFFFFFFFF, 0x00000000, PPC_SPE);
8028GEN_SPE(evmheumia, evmhesmia, 0x14, 0x10, 0x00000000, 0x00000000, PPC_SPE);
8029GEN_SPE(speundef, evmhesmfa, 0x15, 0x10, 0xFFFFFFFF, 0x00000000, PPC_SPE);
8030GEN_SPE(evmhoumia, evmhosmia, 0x16, 0x10, 0x00000000, 0x00000000, PPC_SPE);
8031GEN_SPE(speundef, evmhosmfa, 0x17, 0x10, 0xFFFFFFFF, 0x00000000, PPC_SPE);
8032
8033GEN_SPE(speundef, evmwhssf, 0x03, 0x11, 0xFFFFFFFF, 0x00000000, PPC_SPE);
8034GEN_SPE(evmwlumi, speundef, 0x04, 0x11, 0x00000000, 0xFFFFFFFF, PPC_SPE);
8035GEN_SPE(evmwhumi, evmwhsmi, 0x06, 0x11, 0x00000000, 0x00000000, PPC_SPE);
8036GEN_SPE(speundef, evmwhsmf, 0x07, 0x11, 0xFFFFFFFF, 0x00000000, PPC_SPE);
8037GEN_SPE(speundef, evmwssf, 0x09, 0x11, 0xFFFFFFFF, 0x00000000, PPC_SPE);
8038GEN_SPE(speundef, evmwsmf, 0x0D, 0x11, 0xFFFFFFFF, 0x00000000, PPC_SPE);
8039GEN_SPE(speundef, evmwhssfa, 0x13, 0x11, 0xFFFFFFFF, 0x00000000, PPC_SPE);
8040GEN_SPE(evmwlumia, speundef, 0x14, 0x11, 0x00000000, 0xFFFFFFFF, PPC_SPE);
8041GEN_SPE(evmwhumia, evmwhsmia, 0x16, 0x11, 0x00000000, 0x00000000, PPC_SPE);
8042GEN_SPE(speundef, evmwhsmfa, 0x17, 0x11, 0xFFFFFFFF, 0x00000000, PPC_SPE);
8043GEN_SPE(speundef, evmwssfa, 0x19, 0x11, 0xFFFFFFFF, 0x00000000, PPC_SPE);
8044GEN_SPE(speundef, evmwsmfa, 0x1D, 0x11, 0xFFFFFFFF, 0x00000000, PPC_SPE);
8045
8046GEN_SPE(evadduiaaw, evaddsiaaw, 0x00, 0x13, 0x0000F800, 0x0000F800, PPC_SPE);
8047GEN_SPE(evsubfusiaaw, evsubfssiaaw, 0x01, 0x13, 0x0000F800, 0x0000F800, PPC_SPE);
8048GEN_SPE(evaddumiaaw, evaddsmiaaw, 0x04, 0x13, 0x0000F800, 0x0000F800, PPC_SPE);
8049GEN_SPE(evsubfumiaaw, evsubfsmiaaw, 0x05, 0x13, 0x0000F800, 0x0000F800, PPC_SPE);
8050GEN_SPE(evdivws, evdivwu, 0x06, 0x13, 0x00000000, 0x00000000, PPC_SPE);
8051
8052GEN_SPE(evmheusiaaw, evmhessiaaw, 0x00, 0x14, 0x00000000, 0x00000000, PPC_SPE);
8053GEN_SPE(speundef, evmhessfaaw, 0x01, 0x14, 0xFFFFFFFF, 0x00000000, PPC_SPE);
8054GEN_SPE(evmhousiaaw, evmhossiaaw, 0x02, 0x14, 0x00000000, 0x00000000, PPC_SPE);
8055GEN_SPE(speundef, evmhossfaaw, 0x03, 0x14, 0xFFFFFFFF, 0x00000000, PPC_SPE);
8056GEN_SPE(evmheumiaaw, evmhesmiaaw, 0x04, 0x14, 0x00000000, 0x00000000, PPC_SPE);
8057GEN_SPE(speundef, evmhesmfaaw, 0x05, 0x14, 0xFFFFFFFF, 0x00000000, PPC_SPE);
8058GEN_SPE(evmhoumiaaw, evmhosmiaaw, 0x06, 0x14, 0x00000000, 0x00000000, PPC_SPE);
8059GEN_SPE(speundef, evmhosmfaaw, 0x07, 0x14, 0xFFFFFFFF, 0x00000000, PPC_SPE);
8060GEN_SPE(evmhegumiaa, evmhegsmiaa, 0x14, 0x14, 0x00000000, 0x00000000, PPC_SPE);
8061GEN_SPE(speundef, evmhegsmfaa, 0x15, 0x14, 0xFFFFFFFF, 0x00000000, PPC_SPE);
8062GEN_SPE(evmhogumiaa, evmhogsmiaa, 0x16, 0x14, 0x00000000, 0x00000000, PPC_SPE);
8063GEN_SPE(speundef, evmhogsmfaa, 0x17, 0x14, 0xFFFFFFFF, 0x00000000, PPC_SPE);
8064
8065GEN_SPE(evmwlusiaaw, evmwlssiaaw, 0x00, 0x15, 0x00000000, 0x00000000, PPC_SPE);
8066GEN_SPE(evmwlumiaaw, evmwlsmiaaw, 0x04, 0x15, 0x00000000, 0x00000000, PPC_SPE);
8067GEN_SPE(speundef, evmwssfaa, 0x09, 0x15, 0xFFFFFFFF, 0x00000000, PPC_SPE);
8068GEN_SPE(speundef, evmwsmfaa, 0x0D, 0x15, 0xFFFFFFFF, 0x00000000, PPC_SPE);
8069
8070GEN_SPE(evmheusianw, evmhessianw, 0x00, 0x16, 0x00000000, 0x00000000, PPC_SPE);
8071GEN_SPE(speundef, evmhessfanw, 0x01, 0x16, 0xFFFFFFFF, 0x00000000, PPC_SPE);
8072GEN_SPE(evmhousianw, evmhossianw, 0x02, 0x16, 0x00000000, 0x00000000, PPC_SPE);
8073GEN_SPE(speundef, evmhossfanw, 0x03, 0x16, 0xFFFFFFFF, 0x00000000, PPC_SPE);
8074GEN_SPE(evmheumianw, evmhesmianw, 0x04, 0x16, 0x00000000, 0x00000000, PPC_SPE);
8075GEN_SPE(speundef, evmhesmfanw, 0x05, 0x16, 0xFFFFFFFF, 0x00000000, PPC_SPE);
8076GEN_SPE(evmhoumianw, evmhosmianw, 0x06, 0x16, 0x00000000, 0x00000000, PPC_SPE);
8077GEN_SPE(speundef, evmhosmfanw, 0x07, 0x16, 0xFFFFFFFF, 0x00000000, PPC_SPE);
8078GEN_SPE(evmhegumian, evmhegsmian, 0x14, 0x16, 0x00000000, 0x00000000, PPC_SPE);
8079GEN_SPE(speundef, evmhegsmfan, 0x15, 0x16, 0xFFFFFFFF, 0x00000000, PPC_SPE);
8080GEN_SPE(evmhigumian, evmhigsmian, 0x16, 0x16, 0x00000000, 0x00000000, PPC_SPE);
8081GEN_SPE(speundef, evmhogsmfan, 0x17, 0x16, 0xFFFFFFFF, 0x00000000, PPC_SPE);
8082
8083GEN_SPE(evmwlusianw, evmwlssianw, 0x00, 0x17, 0x00000000, 0x00000000, PPC_SPE);
8084GEN_SPE(evmwlumianw, evmwlsmianw, 0x04, 0x17, 0x00000000, 0x00000000, PPC_SPE);
8085GEN_SPE(speundef, evmwssfan, 0x09, 0x17, 0xFFFFFFFF, 0x00000000, PPC_SPE);
8086GEN_SPE(evmwumian, evmwsmian, 0x0C, 0x17, 0x00000000, 0x00000000, PPC_SPE);
8087GEN_SPE(speundef, evmwsmfan, 0x0D, 0x17, 0xFFFFFFFF, 0x00000000, PPC_SPE);
0487d6a8
JM
8088#endif
8089
8090/*** SPE floating-point extension ***/
1c97856d
AJ
8091#if defined(TARGET_PPC64)
8092#define GEN_SPEFPUOP_CONV_32_32(name) \
636aa200 8093static inline void gen_##name(DisasContext *ctx) \
0487d6a8 8094{ \
1c97856d
AJ
8095 TCGv_i32 t0; \
8096 TCGv t1; \
8097 t0 = tcg_temp_new_i32(); \
8098 tcg_gen_trunc_tl_i32(t0, cpu_gpr[rB(ctx->opcode)]); \
8e703949 8099 gen_helper_##name(t0, cpu_env, t0); \
1c97856d
AJ
8100 t1 = tcg_temp_new(); \
8101 tcg_gen_extu_i32_tl(t1, t0); \
8102 tcg_temp_free_i32(t0); \
8103 tcg_gen_andi_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rD(ctx->opcode)], \
8104 0xFFFFFFFF00000000ULL); \
8105 tcg_gen_or_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rD(ctx->opcode)], t1); \
8106 tcg_temp_free(t1); \
0487d6a8 8107}
1c97856d 8108#define GEN_SPEFPUOP_CONV_32_64(name) \
636aa200 8109static inline void gen_##name(DisasContext *ctx) \
1c97856d
AJ
8110{ \
8111 TCGv_i32 t0; \
8112 TCGv t1; \
8113 t0 = tcg_temp_new_i32(); \
8e703949 8114 gen_helper_##name(t0, cpu_env, cpu_gpr[rB(ctx->opcode)]); \
1c97856d
AJ
8115 t1 = tcg_temp_new(); \
8116 tcg_gen_extu_i32_tl(t1, t0); \
8117 tcg_temp_free_i32(t0); \
8118 tcg_gen_andi_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rD(ctx->opcode)], \
8119 0xFFFFFFFF00000000ULL); \
8120 tcg_gen_or_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rD(ctx->opcode)], t1); \
8121 tcg_temp_free(t1); \
8122}
8123#define GEN_SPEFPUOP_CONV_64_32(name) \
636aa200 8124static inline void gen_##name(DisasContext *ctx) \
1c97856d
AJ
8125{ \
8126 TCGv_i32 t0 = tcg_temp_new_i32(); \
8127 tcg_gen_trunc_tl_i32(t0, cpu_gpr[rB(ctx->opcode)]); \
8e703949 8128 gen_helper_##name(cpu_gpr[rD(ctx->opcode)], cpu_env, t0); \
1c97856d
AJ
8129 tcg_temp_free_i32(t0); \
8130}
8131#define GEN_SPEFPUOP_CONV_64_64(name) \
636aa200 8132static inline void gen_##name(DisasContext *ctx) \
1c97856d 8133{ \
8e703949
BS
8134 gen_helper_##name(cpu_gpr[rD(ctx->opcode)], cpu_env, \
8135 cpu_gpr[rB(ctx->opcode)]); \
1c97856d
AJ
8136}
8137#define GEN_SPEFPUOP_ARITH2_32_32(name) \
636aa200 8138static inline void gen_##name(DisasContext *ctx) \
57951c27 8139{ \
1c97856d
AJ
8140 TCGv_i32 t0, t1; \
8141 TCGv_i64 t2; \
57951c27 8142 if (unlikely(!ctx->spe_enabled)) { \
27a69bb0 8143 gen_exception(ctx, POWERPC_EXCP_SPEU); \
57951c27
AJ
8144 return; \
8145 } \
1c97856d
AJ
8146 t0 = tcg_temp_new_i32(); \
8147 t1 = tcg_temp_new_i32(); \
8148 tcg_gen_trunc_tl_i32(t0, cpu_gpr[rA(ctx->opcode)]); \
8149 tcg_gen_trunc_tl_i32(t1, cpu_gpr[rB(ctx->opcode)]); \
8e703949 8150 gen_helper_##name(t0, cpu_env, t0, t1); \
1c97856d
AJ
8151 tcg_temp_free_i32(t1); \
8152 t2 = tcg_temp_new(); \
8153 tcg_gen_extu_i32_tl(t2, t0); \
8154 tcg_temp_free_i32(t0); \
8155 tcg_gen_andi_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rD(ctx->opcode)], \
8156 0xFFFFFFFF00000000ULL); \
8157 tcg_gen_or_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rD(ctx->opcode)], t2); \
8158 tcg_temp_free(t2); \
57951c27 8159}
1c97856d 8160#define GEN_SPEFPUOP_ARITH2_64_64(name) \
636aa200 8161static inline void gen_##name(DisasContext *ctx) \
57951c27
AJ
8162{ \
8163 if (unlikely(!ctx->spe_enabled)) { \
27a69bb0 8164 gen_exception(ctx, POWERPC_EXCP_SPEU); \
57951c27
AJ
8165 return; \
8166 } \
8e703949
BS
8167 gen_helper_##name(cpu_gpr[rD(ctx->opcode)], cpu_env, \
8168 cpu_gpr[rA(ctx->opcode)], cpu_gpr[rB(ctx->opcode)]); \
57951c27 8169}
1c97856d 8170#define GEN_SPEFPUOP_COMP_32(name) \
636aa200 8171static inline void gen_##name(DisasContext *ctx) \
57951c27 8172{ \
1c97856d 8173 TCGv_i32 t0, t1; \
57951c27 8174 if (unlikely(!ctx->spe_enabled)) { \
27a69bb0 8175 gen_exception(ctx, POWERPC_EXCP_SPEU); \
57951c27
AJ
8176 return; \
8177 } \
1c97856d
AJ
8178 t0 = tcg_temp_new_i32(); \
8179 t1 = tcg_temp_new_i32(); \
8180 tcg_gen_trunc_tl_i32(t0, cpu_gpr[rA(ctx->opcode)]); \
8181 tcg_gen_trunc_tl_i32(t1, cpu_gpr[rB(ctx->opcode)]); \
8e703949 8182 gen_helper_##name(cpu_crf[crfD(ctx->opcode)], cpu_env, t0, t1); \
1c97856d
AJ
8183 tcg_temp_free_i32(t0); \
8184 tcg_temp_free_i32(t1); \
8185}
8186#define GEN_SPEFPUOP_COMP_64(name) \
636aa200 8187static inline void gen_##name(DisasContext *ctx) \
1c97856d
AJ
8188{ \
8189 if (unlikely(!ctx->spe_enabled)) { \
27a69bb0 8190 gen_exception(ctx, POWERPC_EXCP_SPEU); \
1c97856d
AJ
8191 return; \
8192 } \
8e703949 8193 gen_helper_##name(cpu_crf[crfD(ctx->opcode)], cpu_env, \
1c97856d
AJ
8194 cpu_gpr[rA(ctx->opcode)], cpu_gpr[rB(ctx->opcode)]); \
8195}
8196#else
8197#define GEN_SPEFPUOP_CONV_32_32(name) \
636aa200 8198static inline void gen_##name(DisasContext *ctx) \
1c97856d 8199{ \
8e703949
BS
8200 gen_helper_##name(cpu_gpr[rD(ctx->opcode)], cpu_env, \
8201 cpu_gpr[rB(ctx->opcode)]); \
57951c27 8202}
1c97856d 8203#define GEN_SPEFPUOP_CONV_32_64(name) \
636aa200 8204static inline void gen_##name(DisasContext *ctx) \
1c97856d
AJ
8205{ \
8206 TCGv_i64 t0 = tcg_temp_new_i64(); \
8207 gen_load_gpr64(t0, rB(ctx->opcode)); \
8e703949 8208 gen_helper_##name(cpu_gpr[rD(ctx->opcode)], cpu_env, t0); \
1c97856d
AJ
8209 tcg_temp_free_i64(t0); \
8210}
8211#define GEN_SPEFPUOP_CONV_64_32(name) \
636aa200 8212static inline void gen_##name(DisasContext *ctx) \
1c97856d
AJ
8213{ \
8214 TCGv_i64 t0 = tcg_temp_new_i64(); \
8e703949 8215 gen_helper_##name(t0, cpu_env, cpu_gpr[rB(ctx->opcode)]); \
1c97856d
AJ
8216 gen_store_gpr64(rD(ctx->opcode), t0); \
8217 tcg_temp_free_i64(t0); \
8218}
8219#define GEN_SPEFPUOP_CONV_64_64(name) \
636aa200 8220static inline void gen_##name(DisasContext *ctx) \
1c97856d
AJ
8221{ \
8222 TCGv_i64 t0 = tcg_temp_new_i64(); \
8223 gen_load_gpr64(t0, rB(ctx->opcode)); \
8e703949 8224 gen_helper_##name(t0, cpu_env, t0); \
1c97856d
AJ
8225 gen_store_gpr64(rD(ctx->opcode), t0); \
8226 tcg_temp_free_i64(t0); \
8227}
8228#define GEN_SPEFPUOP_ARITH2_32_32(name) \
636aa200 8229static inline void gen_##name(DisasContext *ctx) \
1c97856d
AJ
8230{ \
8231 if (unlikely(!ctx->spe_enabled)) { \
27a69bb0 8232 gen_exception(ctx, POWERPC_EXCP_SPEU); \
1c97856d
AJ
8233 return; \
8234 } \
8e703949 8235 gen_helper_##name(cpu_gpr[rD(ctx->opcode)], cpu_env, \
1c97856d
AJ
8236 cpu_gpr[rA(ctx->opcode)], cpu_gpr[rB(ctx->opcode)]); \
8237}
8238#define GEN_SPEFPUOP_ARITH2_64_64(name) \
636aa200 8239static inline void gen_##name(DisasContext *ctx) \
1c97856d
AJ
8240{ \
8241 TCGv_i64 t0, t1; \
8242 if (unlikely(!ctx->spe_enabled)) { \
27a69bb0 8243 gen_exception(ctx, POWERPC_EXCP_SPEU); \
1c97856d
AJ
8244 return; \
8245 } \
8246 t0 = tcg_temp_new_i64(); \
8247 t1 = tcg_temp_new_i64(); \
8248 gen_load_gpr64(t0, rA(ctx->opcode)); \
8249 gen_load_gpr64(t1, rB(ctx->opcode)); \
8e703949 8250 gen_helper_##name(t0, cpu_env, t0, t1); \
1c97856d
AJ
8251 gen_store_gpr64(rD(ctx->opcode), t0); \
8252 tcg_temp_free_i64(t0); \
8253 tcg_temp_free_i64(t1); \
8254}
8255#define GEN_SPEFPUOP_COMP_32(name) \
636aa200 8256static inline void gen_##name(DisasContext *ctx) \
1c97856d
AJ
8257{ \
8258 if (unlikely(!ctx->spe_enabled)) { \
27a69bb0 8259 gen_exception(ctx, POWERPC_EXCP_SPEU); \
1c97856d
AJ
8260 return; \
8261 } \
8e703949 8262 gen_helper_##name(cpu_crf[crfD(ctx->opcode)], cpu_env, \
1c97856d
AJ
8263 cpu_gpr[rA(ctx->opcode)], cpu_gpr[rB(ctx->opcode)]); \
8264}
8265#define GEN_SPEFPUOP_COMP_64(name) \
636aa200 8266static inline void gen_##name(DisasContext *ctx) \
1c97856d
AJ
8267{ \
8268 TCGv_i64 t0, t1; \
8269 if (unlikely(!ctx->spe_enabled)) { \
27a69bb0 8270 gen_exception(ctx, POWERPC_EXCP_SPEU); \
1c97856d
AJ
8271 return; \
8272 } \
8273 t0 = tcg_temp_new_i64(); \
8274 t1 = tcg_temp_new_i64(); \
8275 gen_load_gpr64(t0, rA(ctx->opcode)); \
8276 gen_load_gpr64(t1, rB(ctx->opcode)); \
8e703949 8277 gen_helper_##name(cpu_crf[crfD(ctx->opcode)], cpu_env, t0, t1); \
1c97856d
AJ
8278 tcg_temp_free_i64(t0); \
8279 tcg_temp_free_i64(t1); \
8280}
8281#endif
57951c27 8282
0487d6a8
JM
8283/* Single precision floating-point vectors operations */
8284/* Arithmetic */
1c97856d
AJ
8285GEN_SPEFPUOP_ARITH2_64_64(evfsadd);
8286GEN_SPEFPUOP_ARITH2_64_64(evfssub);
8287GEN_SPEFPUOP_ARITH2_64_64(evfsmul);
8288GEN_SPEFPUOP_ARITH2_64_64(evfsdiv);
636aa200 8289static inline void gen_evfsabs(DisasContext *ctx)
1c97856d
AJ
8290{
8291 if (unlikely(!ctx->spe_enabled)) {
27a69bb0 8292 gen_exception(ctx, POWERPC_EXCP_SPEU);
1c97856d
AJ
8293 return;
8294 }
8295#if defined(TARGET_PPC64)
6d5c34fa 8296 tcg_gen_andi_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)], ~0x8000000080000000LL);
1c97856d 8297#else
6d5c34fa
MP
8298 tcg_gen_andi_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)], ~0x80000000);
8299 tcg_gen_andi_tl(cpu_gprh[rD(ctx->opcode)], cpu_gprh[rA(ctx->opcode)], ~0x80000000);
1c97856d
AJ
8300#endif
8301}
636aa200 8302static inline void gen_evfsnabs(DisasContext *ctx)
1c97856d
AJ
8303{
8304 if (unlikely(!ctx->spe_enabled)) {
27a69bb0 8305 gen_exception(ctx, POWERPC_EXCP_SPEU);
1c97856d
AJ
8306 return;
8307 }
8308#if defined(TARGET_PPC64)
6d5c34fa 8309 tcg_gen_ori_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)], 0x8000000080000000LL);
1c97856d 8310#else
6d5c34fa
MP
8311 tcg_gen_ori_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)], 0x80000000);
8312 tcg_gen_ori_tl(cpu_gprh[rD(ctx->opcode)], cpu_gprh[rA(ctx->opcode)], 0x80000000);
1c97856d
AJ
8313#endif
8314}
636aa200 8315static inline void gen_evfsneg(DisasContext *ctx)
1c97856d
AJ
8316{
8317 if (unlikely(!ctx->spe_enabled)) {
27a69bb0 8318 gen_exception(ctx, POWERPC_EXCP_SPEU);
1c97856d
AJ
8319 return;
8320 }
8321#if defined(TARGET_PPC64)
6d5c34fa 8322 tcg_gen_xori_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)], 0x8000000080000000LL);
1c97856d 8323#else
6d5c34fa
MP
8324 tcg_gen_xori_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)], 0x80000000);
8325 tcg_gen_xori_tl(cpu_gprh[rD(ctx->opcode)], cpu_gprh[rA(ctx->opcode)], 0x80000000);
1c97856d
AJ
8326#endif
8327}
8328
0487d6a8 8329/* Conversion */
1c97856d
AJ
8330GEN_SPEFPUOP_CONV_64_64(evfscfui);
8331GEN_SPEFPUOP_CONV_64_64(evfscfsi);
8332GEN_SPEFPUOP_CONV_64_64(evfscfuf);
8333GEN_SPEFPUOP_CONV_64_64(evfscfsf);
8334GEN_SPEFPUOP_CONV_64_64(evfsctui);
8335GEN_SPEFPUOP_CONV_64_64(evfsctsi);
8336GEN_SPEFPUOP_CONV_64_64(evfsctuf);
8337GEN_SPEFPUOP_CONV_64_64(evfsctsf);
8338GEN_SPEFPUOP_CONV_64_64(evfsctuiz);
8339GEN_SPEFPUOP_CONV_64_64(evfsctsiz);
8340
0487d6a8 8341/* Comparison */
1c97856d
AJ
8342GEN_SPEFPUOP_COMP_64(evfscmpgt);
8343GEN_SPEFPUOP_COMP_64(evfscmplt);
8344GEN_SPEFPUOP_COMP_64(evfscmpeq);
8345GEN_SPEFPUOP_COMP_64(evfststgt);
8346GEN_SPEFPUOP_COMP_64(evfststlt);
8347GEN_SPEFPUOP_COMP_64(evfststeq);
0487d6a8
JM
8348
8349/* Opcodes definitions */
70560da7
FC
8350GEN_SPE(evfsadd, evfssub, 0x00, 0x0A, 0x00000000, 0x00000000, PPC_SPE_SINGLE); //
8351GEN_SPE(evfsabs, evfsnabs, 0x02, 0x0A, 0x0000F800, 0x0000F800, PPC_SPE_SINGLE); //
8352GEN_SPE(evfsneg, speundef, 0x03, 0x0A, 0x0000F800, 0xFFFFFFFF, PPC_SPE_SINGLE); //
8353GEN_SPE(evfsmul, evfsdiv, 0x04, 0x0A, 0x00000000, 0x00000000, PPC_SPE_SINGLE); //
8354GEN_SPE(evfscmpgt, evfscmplt, 0x06, 0x0A, 0x00600000, 0x00600000, PPC_SPE_SINGLE); //
8355GEN_SPE(evfscmpeq, speundef, 0x07, 0x0A, 0x00600000, 0xFFFFFFFF, PPC_SPE_SINGLE); //
8356GEN_SPE(evfscfui, evfscfsi, 0x08, 0x0A, 0x00180000, 0x00180000, PPC_SPE_SINGLE); //
8357GEN_SPE(evfscfuf, evfscfsf, 0x09, 0x0A, 0x00180000, 0x00180000, PPC_SPE_SINGLE); //
8358GEN_SPE(evfsctui, evfsctsi, 0x0A, 0x0A, 0x00180000, 0x00180000, PPC_SPE_SINGLE); //
8359GEN_SPE(evfsctuf, evfsctsf, 0x0B, 0x0A, 0x00180000, 0x00180000, PPC_SPE_SINGLE); //
8360GEN_SPE(evfsctuiz, speundef, 0x0C, 0x0A, 0x00180000, 0xFFFFFFFF, PPC_SPE_SINGLE); //
8361GEN_SPE(evfsctsiz, speundef, 0x0D, 0x0A, 0x00180000, 0xFFFFFFFF, PPC_SPE_SINGLE); //
8362GEN_SPE(evfststgt, evfststlt, 0x0E, 0x0A, 0x00600000, 0x00600000, PPC_SPE_SINGLE); //
8363GEN_SPE(evfststeq, speundef, 0x0F, 0x0A, 0x00600000, 0xFFFFFFFF, PPC_SPE_SINGLE); //
0487d6a8
JM
8364
8365/* Single precision floating-point operations */
8366/* Arithmetic */
1c97856d
AJ
8367GEN_SPEFPUOP_ARITH2_32_32(efsadd);
8368GEN_SPEFPUOP_ARITH2_32_32(efssub);
8369GEN_SPEFPUOP_ARITH2_32_32(efsmul);
8370GEN_SPEFPUOP_ARITH2_32_32(efsdiv);
636aa200 8371static inline void gen_efsabs(DisasContext *ctx)
1c97856d
AJ
8372{
8373 if (unlikely(!ctx->spe_enabled)) {
27a69bb0 8374 gen_exception(ctx, POWERPC_EXCP_SPEU);
1c97856d
AJ
8375 return;
8376 }
6d5c34fa 8377 tcg_gen_andi_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)], (target_long)~0x80000000LL);
1c97856d 8378}
636aa200 8379static inline void gen_efsnabs(DisasContext *ctx)
1c97856d
AJ
8380{
8381 if (unlikely(!ctx->spe_enabled)) {
27a69bb0 8382 gen_exception(ctx, POWERPC_EXCP_SPEU);
1c97856d
AJ
8383 return;
8384 }
6d5c34fa 8385 tcg_gen_ori_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)], 0x80000000);
1c97856d 8386}
636aa200 8387static inline void gen_efsneg(DisasContext *ctx)
1c97856d
AJ
8388{
8389 if (unlikely(!ctx->spe_enabled)) {
27a69bb0 8390 gen_exception(ctx, POWERPC_EXCP_SPEU);
1c97856d
AJ
8391 return;
8392 }
6d5c34fa 8393 tcg_gen_xori_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)], 0x80000000);
1c97856d
AJ
8394}
8395
0487d6a8 8396/* Conversion */
1c97856d
AJ
8397GEN_SPEFPUOP_CONV_32_32(efscfui);
8398GEN_SPEFPUOP_CONV_32_32(efscfsi);
8399GEN_SPEFPUOP_CONV_32_32(efscfuf);
8400GEN_SPEFPUOP_CONV_32_32(efscfsf);
8401GEN_SPEFPUOP_CONV_32_32(efsctui);
8402GEN_SPEFPUOP_CONV_32_32(efsctsi);
8403GEN_SPEFPUOP_CONV_32_32(efsctuf);
8404GEN_SPEFPUOP_CONV_32_32(efsctsf);
8405GEN_SPEFPUOP_CONV_32_32(efsctuiz);
8406GEN_SPEFPUOP_CONV_32_32(efsctsiz);
8407GEN_SPEFPUOP_CONV_32_64(efscfd);
8408
0487d6a8 8409/* Comparison */
1c97856d
AJ
8410GEN_SPEFPUOP_COMP_32(efscmpgt);
8411GEN_SPEFPUOP_COMP_32(efscmplt);
8412GEN_SPEFPUOP_COMP_32(efscmpeq);
8413GEN_SPEFPUOP_COMP_32(efststgt);
8414GEN_SPEFPUOP_COMP_32(efststlt);
8415GEN_SPEFPUOP_COMP_32(efststeq);
0487d6a8
JM
8416
8417/* Opcodes definitions */
70560da7
FC
8418GEN_SPE(efsadd, efssub, 0x00, 0x0B, 0x00000000, 0x00000000, PPC_SPE_SINGLE); //
8419GEN_SPE(efsabs, efsnabs, 0x02, 0x0B, 0x0000F800, 0x0000F800, PPC_SPE_SINGLE); //
8420GEN_SPE(efsneg, speundef, 0x03, 0x0B, 0x0000F800, 0xFFFFFFFF, PPC_SPE_SINGLE); //
8421GEN_SPE(efsmul, efsdiv, 0x04, 0x0B, 0x00000000, 0x00000000, PPC_SPE_SINGLE); //
8422GEN_SPE(efscmpgt, efscmplt, 0x06, 0x0B, 0x00600000, 0x00600000, PPC_SPE_SINGLE); //
8423GEN_SPE(efscmpeq, efscfd, 0x07, 0x0B, 0x00600000, 0x00180000, PPC_SPE_SINGLE); //
8424GEN_SPE(efscfui, efscfsi, 0x08, 0x0B, 0x00180000, 0x00180000, PPC_SPE_SINGLE); //
8425GEN_SPE(efscfuf, efscfsf, 0x09, 0x0B, 0x00180000, 0x00180000, PPC_SPE_SINGLE); //
8426GEN_SPE(efsctui, efsctsi, 0x0A, 0x0B, 0x00180000, 0x00180000, PPC_SPE_SINGLE); //
8427GEN_SPE(efsctuf, efsctsf, 0x0B, 0x0B, 0x00180000, 0x00180000, PPC_SPE_SINGLE); //
8428GEN_SPE(efsctuiz, speundef, 0x0C, 0x0B, 0x00180000, 0xFFFFFFFF, PPC_SPE_SINGLE); //
8429GEN_SPE(efsctsiz, speundef, 0x0D, 0x0B, 0x00180000, 0xFFFFFFFF, PPC_SPE_SINGLE); //
8430GEN_SPE(efststgt, efststlt, 0x0E, 0x0B, 0x00600000, 0x00600000, PPC_SPE_SINGLE); //
8431GEN_SPE(efststeq, speundef, 0x0F, 0x0B, 0x00600000, 0xFFFFFFFF, PPC_SPE_SINGLE); //
0487d6a8
JM
8432
8433/* Double precision floating-point operations */
8434/* Arithmetic */
1c97856d
AJ
8435GEN_SPEFPUOP_ARITH2_64_64(efdadd);
8436GEN_SPEFPUOP_ARITH2_64_64(efdsub);
8437GEN_SPEFPUOP_ARITH2_64_64(efdmul);
8438GEN_SPEFPUOP_ARITH2_64_64(efddiv);
636aa200 8439static inline void gen_efdabs(DisasContext *ctx)
1c97856d
AJ
8440{
8441 if (unlikely(!ctx->spe_enabled)) {
27a69bb0 8442 gen_exception(ctx, POWERPC_EXCP_SPEU);
1c97856d
AJ
8443 return;
8444 }
8445#if defined(TARGET_PPC64)
6d5c34fa 8446 tcg_gen_andi_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)], ~0x8000000000000000LL);
1c97856d 8447#else
6d5c34fa
MP
8448 tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)]);
8449 tcg_gen_andi_tl(cpu_gprh[rD(ctx->opcode)], cpu_gprh[rA(ctx->opcode)], ~0x80000000);
1c97856d
AJ
8450#endif
8451}
636aa200 8452static inline void gen_efdnabs(DisasContext *ctx)
1c97856d
AJ
8453{
8454 if (unlikely(!ctx->spe_enabled)) {
27a69bb0 8455 gen_exception(ctx, POWERPC_EXCP_SPEU);
1c97856d
AJ
8456 return;
8457 }
8458#if defined(TARGET_PPC64)
6d5c34fa 8459 tcg_gen_ori_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)], 0x8000000000000000LL);
1c97856d 8460#else
6d5c34fa
MP
8461 tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)]);
8462 tcg_gen_ori_tl(cpu_gprh[rD(ctx->opcode)], cpu_gprh[rA(ctx->opcode)], 0x80000000);
1c97856d
AJ
8463#endif
8464}
636aa200 8465static inline void gen_efdneg(DisasContext *ctx)
1c97856d
AJ
8466{
8467 if (unlikely(!ctx->spe_enabled)) {
27a69bb0 8468 gen_exception(ctx, POWERPC_EXCP_SPEU);
1c97856d
AJ
8469 return;
8470 }
8471#if defined(TARGET_PPC64)
6d5c34fa 8472 tcg_gen_xori_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)], 0x8000000000000000LL);
1c97856d 8473#else
6d5c34fa
MP
8474 tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)]);
8475 tcg_gen_xori_tl(cpu_gprh[rD(ctx->opcode)], cpu_gprh[rA(ctx->opcode)], 0x80000000);
1c97856d
AJ
8476#endif
8477}
8478
0487d6a8 8479/* Conversion */
1c97856d
AJ
8480GEN_SPEFPUOP_CONV_64_32(efdcfui);
8481GEN_SPEFPUOP_CONV_64_32(efdcfsi);
8482GEN_SPEFPUOP_CONV_64_32(efdcfuf);
8483GEN_SPEFPUOP_CONV_64_32(efdcfsf);
8484GEN_SPEFPUOP_CONV_32_64(efdctui);
8485GEN_SPEFPUOP_CONV_32_64(efdctsi);
8486GEN_SPEFPUOP_CONV_32_64(efdctuf);
8487GEN_SPEFPUOP_CONV_32_64(efdctsf);
8488GEN_SPEFPUOP_CONV_32_64(efdctuiz);
8489GEN_SPEFPUOP_CONV_32_64(efdctsiz);
8490GEN_SPEFPUOP_CONV_64_32(efdcfs);
8491GEN_SPEFPUOP_CONV_64_64(efdcfuid);
8492GEN_SPEFPUOP_CONV_64_64(efdcfsid);
8493GEN_SPEFPUOP_CONV_64_64(efdctuidz);
8494GEN_SPEFPUOP_CONV_64_64(efdctsidz);
0487d6a8 8495
0487d6a8 8496/* Comparison */
1c97856d
AJ
8497GEN_SPEFPUOP_COMP_64(efdcmpgt);
8498GEN_SPEFPUOP_COMP_64(efdcmplt);
8499GEN_SPEFPUOP_COMP_64(efdcmpeq);
8500GEN_SPEFPUOP_COMP_64(efdtstgt);
8501GEN_SPEFPUOP_COMP_64(efdtstlt);
8502GEN_SPEFPUOP_COMP_64(efdtsteq);
0487d6a8
JM
8503
8504/* Opcodes definitions */
70560da7
FC
8505GEN_SPE(efdadd, efdsub, 0x10, 0x0B, 0x00000000, 0x00000000, PPC_SPE_DOUBLE); //
8506GEN_SPE(efdcfuid, efdcfsid, 0x11, 0x0B, 0x00180000, 0x00180000, PPC_SPE_DOUBLE); //
8507GEN_SPE(efdabs, efdnabs, 0x12, 0x0B, 0x0000F800, 0x0000F800, PPC_SPE_DOUBLE); //
8508GEN_SPE(efdneg, speundef, 0x13, 0x0B, 0x0000F800, 0xFFFFFFFF, PPC_SPE_DOUBLE); //
8509GEN_SPE(efdmul, efddiv, 0x14, 0x0B, 0x00000000, 0x00000000, PPC_SPE_DOUBLE); //
8510GEN_SPE(efdctuidz, efdctsidz, 0x15, 0x0B, 0x00180000, 0x00180000, PPC_SPE_DOUBLE); //
8511GEN_SPE(efdcmpgt, efdcmplt, 0x16, 0x0B, 0x00600000, 0x00600000, PPC_SPE_DOUBLE); //
8512GEN_SPE(efdcmpeq, efdcfs, 0x17, 0x0B, 0x00600000, 0x00180000, PPC_SPE_DOUBLE); //
8513GEN_SPE(efdcfui, efdcfsi, 0x18, 0x0B, 0x00180000, 0x00180000, PPC_SPE_DOUBLE); //
8514GEN_SPE(efdcfuf, efdcfsf, 0x19, 0x0B, 0x00180000, 0x00180000, PPC_SPE_DOUBLE); //
8515GEN_SPE(efdctui, efdctsi, 0x1A, 0x0B, 0x00180000, 0x00180000, PPC_SPE_DOUBLE); //
8516GEN_SPE(efdctuf, efdctsf, 0x1B, 0x0B, 0x00180000, 0x00180000, PPC_SPE_DOUBLE); //
8517GEN_SPE(efdctuiz, speundef, 0x1C, 0x0B, 0x00180000, 0xFFFFFFFF, PPC_SPE_DOUBLE); //
8518GEN_SPE(efdctsiz, speundef, 0x1D, 0x0B, 0x00180000, 0xFFFFFFFF, PPC_SPE_DOUBLE); //
8519GEN_SPE(efdtstgt, efdtstlt, 0x1E, 0x0B, 0x00600000, 0x00600000, PPC_SPE_DOUBLE); //
8520GEN_SPE(efdtsteq, speundef, 0x1F, 0x0B, 0x00600000, 0xFFFFFFFF, PPC_SPE_DOUBLE); //
0487d6a8 8521
c227f099 8522static opcode_t opcodes[] = {
5c55ff99
BS
8523GEN_HANDLER(invalid, 0x00, 0x00, 0x00, 0xFFFFFFFF, PPC_NONE),
8524GEN_HANDLER(cmp, 0x1F, 0x00, 0x00, 0x00400000, PPC_INTEGER),
8525GEN_HANDLER(cmpi, 0x0B, 0xFF, 0xFF, 0x00400000, PPC_INTEGER),
8526GEN_HANDLER(cmpl, 0x1F, 0x00, 0x01, 0x00400000, PPC_INTEGER),
8527GEN_HANDLER(cmpli, 0x0A, 0xFF, 0xFF, 0x00400000, PPC_INTEGER),
8528GEN_HANDLER(isel, 0x1F, 0x0F, 0xFF, 0x00000001, PPC_ISEL),
8529GEN_HANDLER(addi, 0x0E, 0xFF, 0xFF, 0x00000000, PPC_INTEGER),
8530GEN_HANDLER(addic, 0x0C, 0xFF, 0xFF, 0x00000000, PPC_INTEGER),
8531GEN_HANDLER2(addic_, "addic.", 0x0D, 0xFF, 0xFF, 0x00000000, PPC_INTEGER),
8532GEN_HANDLER(addis, 0x0F, 0xFF, 0xFF, 0x00000000, PPC_INTEGER),
8533GEN_HANDLER(mulhw, 0x1F, 0x0B, 0x02, 0x00000400, PPC_INTEGER),
8534GEN_HANDLER(mulhwu, 0x1F, 0x0B, 0x00, 0x00000400, PPC_INTEGER),
8535GEN_HANDLER(mullw, 0x1F, 0x0B, 0x07, 0x00000000, PPC_INTEGER),
8536GEN_HANDLER(mullwo, 0x1F, 0x0B, 0x17, 0x00000000, PPC_INTEGER),
8537GEN_HANDLER(mulli, 0x07, 0xFF, 0xFF, 0x00000000, PPC_INTEGER),
8538#if defined(TARGET_PPC64)
8539GEN_HANDLER(mulld, 0x1F, 0x09, 0x07, 0x00000000, PPC_64B),
8540#endif
8541GEN_HANDLER(neg, 0x1F, 0x08, 0x03, 0x0000F800, PPC_INTEGER),
8542GEN_HANDLER(nego, 0x1F, 0x08, 0x13, 0x0000F800, PPC_INTEGER),
8543GEN_HANDLER(subfic, 0x08, 0xFF, 0xFF, 0x00000000, PPC_INTEGER),
8544GEN_HANDLER2(andi_, "andi.", 0x1C, 0xFF, 0xFF, 0x00000000, PPC_INTEGER),
8545GEN_HANDLER2(andis_, "andis.", 0x1D, 0xFF, 0xFF, 0x00000000, PPC_INTEGER),
8546GEN_HANDLER(cntlzw, 0x1F, 0x1A, 0x00, 0x00000000, PPC_INTEGER),
8547GEN_HANDLER(or, 0x1F, 0x1C, 0x0D, 0x00000000, PPC_INTEGER),
8548GEN_HANDLER(xor, 0x1F, 0x1C, 0x09, 0x00000000, PPC_INTEGER),
8549GEN_HANDLER(ori, 0x18, 0xFF, 0xFF, 0x00000000, PPC_INTEGER),
8550GEN_HANDLER(oris, 0x19, 0xFF, 0xFF, 0x00000000, PPC_INTEGER),
8551GEN_HANDLER(xori, 0x1A, 0xFF, 0xFF, 0x00000000, PPC_INTEGER),
8552GEN_HANDLER(xoris, 0x1B, 0xFF, 0xFF, 0x00000000, PPC_INTEGER),
8553GEN_HANDLER(popcntb, 0x1F, 0x03, 0x03, 0x0000F801, PPC_POPCNTB),
eaabeef2 8554GEN_HANDLER(popcntw, 0x1F, 0x1A, 0x0b, 0x0000F801, PPC_POPCNTWD),
5c55ff99 8555#if defined(TARGET_PPC64)
eaabeef2 8556GEN_HANDLER(popcntd, 0x1F, 0x1A, 0x0F, 0x0000F801, PPC_POPCNTWD),
5c55ff99
BS
8557GEN_HANDLER(cntlzd, 0x1F, 0x1A, 0x01, 0x00000000, PPC_64B),
8558#endif
8559GEN_HANDLER(rlwimi, 0x14, 0xFF, 0xFF, 0x00000000, PPC_INTEGER),
8560GEN_HANDLER(rlwinm, 0x15, 0xFF, 0xFF, 0x00000000, PPC_INTEGER),
8561GEN_HANDLER(rlwnm, 0x17, 0xFF, 0xFF, 0x00000000, PPC_INTEGER),
8562GEN_HANDLER(slw, 0x1F, 0x18, 0x00, 0x00000000, PPC_INTEGER),
8563GEN_HANDLER(sraw, 0x1F, 0x18, 0x18, 0x00000000, PPC_INTEGER),
8564GEN_HANDLER(srawi, 0x1F, 0x18, 0x19, 0x00000000, PPC_INTEGER),
8565GEN_HANDLER(srw, 0x1F, 0x18, 0x10, 0x00000000, PPC_INTEGER),
8566#if defined(TARGET_PPC64)
8567GEN_HANDLER(sld, 0x1F, 0x1B, 0x00, 0x00000000, PPC_64B),
8568GEN_HANDLER(srad, 0x1F, 0x1A, 0x18, 0x00000000, PPC_64B),
8569GEN_HANDLER2(sradi0, "sradi", 0x1F, 0x1A, 0x19, 0x00000000, PPC_64B),
8570GEN_HANDLER2(sradi1, "sradi", 0x1F, 0x1B, 0x19, 0x00000000, PPC_64B),
8571GEN_HANDLER(srd, 0x1F, 0x1B, 0x10, 0x00000000, PPC_64B),
8572#endif
8573GEN_HANDLER(frsqrtes, 0x3B, 0x1A, 0xFF, 0x001F07C0, PPC_FLOAT_FRSQRTES),
8574GEN_HANDLER(fsqrt, 0x3F, 0x16, 0xFF, 0x001F07C0, PPC_FLOAT_FSQRT),
8575GEN_HANDLER(fsqrts, 0x3B, 0x16, 0xFF, 0x001F07C0, PPC_FLOAT_FSQRT),
8576GEN_HANDLER(fcmpo, 0x3F, 0x00, 0x01, 0x00600001, PPC_FLOAT),
8577GEN_HANDLER(fcmpu, 0x3F, 0x00, 0x00, 0x00600001, PPC_FLOAT),
8578GEN_HANDLER(fmr, 0x3F, 0x08, 0x02, 0x001F0000, PPC_FLOAT),
8579GEN_HANDLER(mcrfs, 0x3F, 0x00, 0x02, 0x0063F801, PPC_FLOAT),
8580GEN_HANDLER(mffs, 0x3F, 0x07, 0x12, 0x001FF800, PPC_FLOAT),
8581GEN_HANDLER(mtfsb0, 0x3F, 0x06, 0x02, 0x001FF800, PPC_FLOAT),
8582GEN_HANDLER(mtfsb1, 0x3F, 0x06, 0x01, 0x001FF800, PPC_FLOAT),
8583GEN_HANDLER(mtfsf, 0x3F, 0x07, 0x16, 0x00010000, PPC_FLOAT),
8584GEN_HANDLER(mtfsfi, 0x3F, 0x06, 0x04, 0x006f0800, PPC_FLOAT),
8585#if defined(TARGET_PPC64)
8586GEN_HANDLER(ld, 0x3A, 0xFF, 0xFF, 0x00000000, PPC_64B),
8587GEN_HANDLER(lq, 0x38, 0xFF, 0xFF, 0x00000000, PPC_64BX),
8588GEN_HANDLER(std, 0x3E, 0xFF, 0xFF, 0x00000000, PPC_64B),
8589#endif
8590GEN_HANDLER(lmw, 0x2E, 0xFF, 0xFF, 0x00000000, PPC_INTEGER),
8591GEN_HANDLER(stmw, 0x2F, 0xFF, 0xFF, 0x00000000, PPC_INTEGER),
8592GEN_HANDLER(lswi, 0x1F, 0x15, 0x12, 0x00000001, PPC_STRING),
8593GEN_HANDLER(lswx, 0x1F, 0x15, 0x10, 0x00000001, PPC_STRING),
8594GEN_HANDLER(stswi, 0x1F, 0x15, 0x16, 0x00000001, PPC_STRING),
8595GEN_HANDLER(stswx, 0x1F, 0x15, 0x14, 0x00000001, PPC_STRING),
8596GEN_HANDLER(eieio, 0x1F, 0x16, 0x1A, 0x03FFF801, PPC_MEM_EIEIO),
8597GEN_HANDLER(isync, 0x13, 0x16, 0x04, 0x03FFF801, PPC_MEM),
f844c817 8598GEN_HANDLER(lwarx, 0x1F, 0x14, 0x00, 0x00000000, PPC_RES),
5c55ff99
BS
8599GEN_HANDLER2(stwcx_, "stwcx.", 0x1F, 0x16, 0x04, 0x00000000, PPC_RES),
8600#if defined(TARGET_PPC64)
f844c817 8601GEN_HANDLER(ldarx, 0x1F, 0x14, 0x02, 0x00000000, PPC_64B),
5c55ff99
BS
8602GEN_HANDLER2(stdcx_, "stdcx.", 0x1F, 0x16, 0x06, 0x00000000, PPC_64B),
8603#endif
8604GEN_HANDLER(sync, 0x1F, 0x16, 0x12, 0x039FF801, PPC_MEM_SYNC),
8605GEN_HANDLER(wait, 0x1F, 0x1E, 0x01, 0x03FFF801, PPC_WAIT),
8606GEN_HANDLER(b, 0x12, 0xFF, 0xFF, 0x00000000, PPC_FLOW),
8607GEN_HANDLER(bc, 0x10, 0xFF, 0xFF, 0x00000000, PPC_FLOW),
8608GEN_HANDLER(bcctr, 0x13, 0x10, 0x10, 0x00000000, PPC_FLOW),
8609GEN_HANDLER(bclr, 0x13, 0x10, 0x00, 0x00000000, PPC_FLOW),
8610GEN_HANDLER(mcrf, 0x13, 0x00, 0xFF, 0x00000001, PPC_INTEGER),
8611GEN_HANDLER(rfi, 0x13, 0x12, 0x01, 0x03FF8001, PPC_FLOW),
8612#if defined(TARGET_PPC64)
8613GEN_HANDLER(rfid, 0x13, 0x12, 0x00, 0x03FF8001, PPC_64B),
8614GEN_HANDLER(hrfid, 0x13, 0x12, 0x08, 0x03FF8001, PPC_64H),
8615#endif
8616GEN_HANDLER(sc, 0x11, 0xFF, 0xFF, 0x03FFF01D, PPC_FLOW),
8617GEN_HANDLER(tw, 0x1F, 0x04, 0x00, 0x00000001, PPC_FLOW),
8618GEN_HANDLER(twi, 0x03, 0xFF, 0xFF, 0x00000000, PPC_FLOW),
8619#if defined(TARGET_PPC64)
8620GEN_HANDLER(td, 0x1F, 0x04, 0x02, 0x00000001, PPC_64B),
8621GEN_HANDLER(tdi, 0x02, 0xFF, 0xFF, 0x00000000, PPC_64B),
8622#endif
8623GEN_HANDLER(mcrxr, 0x1F, 0x00, 0x10, 0x007FF801, PPC_MISC),
8624GEN_HANDLER(mfcr, 0x1F, 0x13, 0x00, 0x00000801, PPC_MISC),
8625GEN_HANDLER(mfmsr, 0x1F, 0x13, 0x02, 0x001FF801, PPC_MISC),
8626GEN_HANDLER(mfspr, 0x1F, 0x13, 0x0A, 0x00000001, PPC_MISC),
8627GEN_HANDLER(mftb, 0x1F, 0x13, 0x0B, 0x00000001, PPC_MFTB),
8628GEN_HANDLER(mtcrf, 0x1F, 0x10, 0x04, 0x00000801, PPC_MISC),
8629#if defined(TARGET_PPC64)
8630GEN_HANDLER(mtmsrd, 0x1F, 0x12, 0x05, 0x001EF801, PPC_64B),
8631#endif
8632GEN_HANDLER(mtmsr, 0x1F, 0x12, 0x04, 0x001FF801, PPC_MISC),
8633GEN_HANDLER(mtspr, 0x1F, 0x13, 0x0E, 0x00000001, PPC_MISC),
8634GEN_HANDLER(dcbf, 0x1F, 0x16, 0x02, 0x03C00001, PPC_CACHE),
8635GEN_HANDLER(dcbi, 0x1F, 0x16, 0x0E, 0x03E00001, PPC_CACHE),
8636GEN_HANDLER(dcbst, 0x1F, 0x16, 0x01, 0x03E00001, PPC_CACHE),
8637GEN_HANDLER(dcbt, 0x1F, 0x16, 0x08, 0x02000001, PPC_CACHE),
8638GEN_HANDLER(dcbtst, 0x1F, 0x16, 0x07, 0x02000001, PPC_CACHE),
8639GEN_HANDLER(dcbz, 0x1F, 0x16, 0x1F, 0x03E00001, PPC_CACHE_DCBZ),
8640GEN_HANDLER2(dcbz_970, "dcbz", 0x1F, 0x16, 0x1F, 0x03C00001, PPC_CACHE_DCBZT),
8641GEN_HANDLER(dst, 0x1F, 0x16, 0x0A, 0x01800001, PPC_ALTIVEC),
8642GEN_HANDLER(dstst, 0x1F, 0x16, 0x0B, 0x02000001, PPC_ALTIVEC),
8643GEN_HANDLER(dss, 0x1F, 0x16, 0x19, 0x019FF801, PPC_ALTIVEC),
8644GEN_HANDLER(icbi, 0x1F, 0x16, 0x1E, 0x03E00001, PPC_CACHE_ICBI),
8645GEN_HANDLER(dcba, 0x1F, 0x16, 0x17, 0x03E00001, PPC_CACHE_DCBA),
8646GEN_HANDLER(mfsr, 0x1F, 0x13, 0x12, 0x0010F801, PPC_SEGMENT),
8647GEN_HANDLER(mfsrin, 0x1F, 0x13, 0x14, 0x001F0001, PPC_SEGMENT),
8648GEN_HANDLER(mtsr, 0x1F, 0x12, 0x06, 0x0010F801, PPC_SEGMENT),
8649GEN_HANDLER(mtsrin, 0x1F, 0x12, 0x07, 0x001F0001, PPC_SEGMENT),
8650#if defined(TARGET_PPC64)
8651GEN_HANDLER2(mfsr_64b, "mfsr", 0x1F, 0x13, 0x12, 0x0010F801, PPC_SEGMENT_64B),
8652GEN_HANDLER2(mfsrin_64b, "mfsrin", 0x1F, 0x13, 0x14, 0x001F0001,
8653 PPC_SEGMENT_64B),
8654GEN_HANDLER2(mtsr_64b, "mtsr", 0x1F, 0x12, 0x06, 0x0010F801, PPC_SEGMENT_64B),
8655GEN_HANDLER2(mtsrin_64b, "mtsrin", 0x1F, 0x12, 0x07, 0x001F0001,
8656 PPC_SEGMENT_64B),
efdef95f
DG
8657GEN_HANDLER2(slbmte, "slbmte", 0x1F, 0x12, 0x0C, 0x001F0001, PPC_SEGMENT_64B),
8658GEN_HANDLER2(slbmfee, "slbmfee", 0x1F, 0x13, 0x1C, 0x001F0001, PPC_SEGMENT_64B),
8659GEN_HANDLER2(slbmfev, "slbmfev", 0x1F, 0x13, 0x1A, 0x001F0001, PPC_SEGMENT_64B),
5c55ff99
BS
8660#endif
8661GEN_HANDLER(tlbia, 0x1F, 0x12, 0x0B, 0x03FFFC01, PPC_MEM_TLBIA),
8662GEN_HANDLER(tlbiel, 0x1F, 0x12, 0x08, 0x03FF0001, PPC_MEM_TLBIE),
8663GEN_HANDLER(tlbie, 0x1F, 0x12, 0x09, 0x03FF0001, PPC_MEM_TLBIE),
8664GEN_HANDLER(tlbsync, 0x1F, 0x16, 0x11, 0x03FFF801, PPC_MEM_TLBSYNC),
8665#if defined(TARGET_PPC64)
8666GEN_HANDLER(slbia, 0x1F, 0x12, 0x0F, 0x03FFFC01, PPC_SLBI),
8667GEN_HANDLER(slbie, 0x1F, 0x12, 0x0D, 0x03FF0001, PPC_SLBI),
8668#endif
8669GEN_HANDLER(eciwx, 0x1F, 0x16, 0x0D, 0x00000001, PPC_EXTERN),
8670GEN_HANDLER(ecowx, 0x1F, 0x16, 0x09, 0x00000001, PPC_EXTERN),
8671GEN_HANDLER(abs, 0x1F, 0x08, 0x0B, 0x0000F800, PPC_POWER_BR),
8672GEN_HANDLER(abso, 0x1F, 0x08, 0x1B, 0x0000F800, PPC_POWER_BR),
8673GEN_HANDLER(clcs, 0x1F, 0x10, 0x13, 0x0000F800, PPC_POWER_BR),
8674GEN_HANDLER(div, 0x1F, 0x0B, 0x0A, 0x00000000, PPC_POWER_BR),
8675GEN_HANDLER(divo, 0x1F, 0x0B, 0x1A, 0x00000000, PPC_POWER_BR),
8676GEN_HANDLER(divs, 0x1F, 0x0B, 0x0B, 0x00000000, PPC_POWER_BR),
8677GEN_HANDLER(divso, 0x1F, 0x0B, 0x1B, 0x00000000, PPC_POWER_BR),
8678GEN_HANDLER(doz, 0x1F, 0x08, 0x08, 0x00000000, PPC_POWER_BR),
8679GEN_HANDLER(dozo, 0x1F, 0x08, 0x18, 0x00000000, PPC_POWER_BR),
8680GEN_HANDLER(dozi, 0x09, 0xFF, 0xFF, 0x00000000, PPC_POWER_BR),
8681GEN_HANDLER(lscbx, 0x1F, 0x15, 0x08, 0x00000000, PPC_POWER_BR),
8682GEN_HANDLER(maskg, 0x1F, 0x1D, 0x00, 0x00000000, PPC_POWER_BR),
8683GEN_HANDLER(maskir, 0x1F, 0x1D, 0x10, 0x00000000, PPC_POWER_BR),
8684GEN_HANDLER(mul, 0x1F, 0x0B, 0x03, 0x00000000, PPC_POWER_BR),
8685GEN_HANDLER(mulo, 0x1F, 0x0B, 0x13, 0x00000000, PPC_POWER_BR),
8686GEN_HANDLER(nabs, 0x1F, 0x08, 0x0F, 0x00000000, PPC_POWER_BR),
8687GEN_HANDLER(nabso, 0x1F, 0x08, 0x1F, 0x00000000, PPC_POWER_BR),
8688GEN_HANDLER(rlmi, 0x16, 0xFF, 0xFF, 0x00000000, PPC_POWER_BR),
8689GEN_HANDLER(rrib, 0x1F, 0x19, 0x10, 0x00000000, PPC_POWER_BR),
8690GEN_HANDLER(sle, 0x1F, 0x19, 0x04, 0x00000000, PPC_POWER_BR),
8691GEN_HANDLER(sleq, 0x1F, 0x19, 0x06, 0x00000000, PPC_POWER_BR),
8692GEN_HANDLER(sliq, 0x1F, 0x18, 0x05, 0x00000000, PPC_POWER_BR),
8693GEN_HANDLER(slliq, 0x1F, 0x18, 0x07, 0x00000000, PPC_POWER_BR),
8694GEN_HANDLER(sllq, 0x1F, 0x18, 0x06, 0x00000000, PPC_POWER_BR),
8695GEN_HANDLER(slq, 0x1F, 0x18, 0x04, 0x00000000, PPC_POWER_BR),
8696GEN_HANDLER(sraiq, 0x1F, 0x18, 0x1D, 0x00000000, PPC_POWER_BR),
8697GEN_HANDLER(sraq, 0x1F, 0x18, 0x1C, 0x00000000, PPC_POWER_BR),
8698GEN_HANDLER(sre, 0x1F, 0x19, 0x14, 0x00000000, PPC_POWER_BR),
8699GEN_HANDLER(srea, 0x1F, 0x19, 0x1C, 0x00000000, PPC_POWER_BR),
8700GEN_HANDLER(sreq, 0x1F, 0x19, 0x16, 0x00000000, PPC_POWER_BR),
8701GEN_HANDLER(sriq, 0x1F, 0x18, 0x15, 0x00000000, PPC_POWER_BR),
8702GEN_HANDLER(srliq, 0x1F, 0x18, 0x17, 0x00000000, PPC_POWER_BR),
8703GEN_HANDLER(srlq, 0x1F, 0x18, 0x16, 0x00000000, PPC_POWER_BR),
8704GEN_HANDLER(srq, 0x1F, 0x18, 0x14, 0x00000000, PPC_POWER_BR),
8705GEN_HANDLER(dsa, 0x1F, 0x14, 0x13, 0x03FFF801, PPC_602_SPEC),
8706GEN_HANDLER(esa, 0x1F, 0x14, 0x12, 0x03FFF801, PPC_602_SPEC),
8707GEN_HANDLER(mfrom, 0x1F, 0x09, 0x08, 0x03E0F801, PPC_602_SPEC),
8708GEN_HANDLER2(tlbld_6xx, "tlbld", 0x1F, 0x12, 0x1E, 0x03FF0001, PPC_6xx_TLB),
8709GEN_HANDLER2(tlbli_6xx, "tlbli", 0x1F, 0x12, 0x1F, 0x03FF0001, PPC_6xx_TLB),
8710GEN_HANDLER2(tlbld_74xx, "tlbld", 0x1F, 0x12, 0x1E, 0x03FF0001, PPC_74xx_TLB),
8711GEN_HANDLER2(tlbli_74xx, "tlbli", 0x1F, 0x12, 0x1F, 0x03FF0001, PPC_74xx_TLB),
8712GEN_HANDLER(clf, 0x1F, 0x16, 0x03, 0x03E00000, PPC_POWER),
8713GEN_HANDLER(cli, 0x1F, 0x16, 0x0F, 0x03E00000, PPC_POWER),
8714GEN_HANDLER(dclst, 0x1F, 0x16, 0x13, 0x03E00000, PPC_POWER),
8715GEN_HANDLER(mfsri, 0x1F, 0x13, 0x13, 0x00000001, PPC_POWER),
8716GEN_HANDLER(rac, 0x1F, 0x12, 0x19, 0x00000001, PPC_POWER),
8717GEN_HANDLER(rfsvc, 0x13, 0x12, 0x02, 0x03FFF0001, PPC_POWER),
8718GEN_HANDLER(lfq, 0x38, 0xFF, 0xFF, 0x00000003, PPC_POWER2),
8719GEN_HANDLER(lfqu, 0x39, 0xFF, 0xFF, 0x00000003, PPC_POWER2),
8720GEN_HANDLER(lfqux, 0x1F, 0x17, 0x19, 0x00000001, PPC_POWER2),
8721GEN_HANDLER(lfqx, 0x1F, 0x17, 0x18, 0x00000001, PPC_POWER2),
8722GEN_HANDLER(stfq, 0x3C, 0xFF, 0xFF, 0x00000003, PPC_POWER2),
8723GEN_HANDLER(stfqu, 0x3D, 0xFF, 0xFF, 0x00000003, PPC_POWER2),
8724GEN_HANDLER(stfqux, 0x1F, 0x17, 0x1D, 0x00000001, PPC_POWER2),
8725GEN_HANDLER(stfqx, 0x1F, 0x17, 0x1C, 0x00000001, PPC_POWER2),
8726GEN_HANDLER(mfapidi, 0x1F, 0x13, 0x08, 0x0000F801, PPC_MFAPIDI),
8727GEN_HANDLER(tlbiva, 0x1F, 0x12, 0x18, 0x03FFF801, PPC_TLBIVA),
8728GEN_HANDLER(mfdcr, 0x1F, 0x03, 0x0A, 0x00000001, PPC_DCR),
8729GEN_HANDLER(mtdcr, 0x1F, 0x03, 0x0E, 0x00000001, PPC_DCR),
8730GEN_HANDLER(mfdcrx, 0x1F, 0x03, 0x08, 0x00000000, PPC_DCRX),
8731GEN_HANDLER(mtdcrx, 0x1F, 0x03, 0x0C, 0x00000000, PPC_DCRX),
8732GEN_HANDLER(mfdcrux, 0x1F, 0x03, 0x09, 0x00000000, PPC_DCRUX),
8733GEN_HANDLER(mtdcrux, 0x1F, 0x03, 0x0D, 0x00000000, PPC_DCRUX),
8734GEN_HANDLER(dccci, 0x1F, 0x06, 0x0E, 0x03E00001, PPC_4xx_COMMON),
8735GEN_HANDLER(dcread, 0x1F, 0x06, 0x0F, 0x00000001, PPC_4xx_COMMON),
8736GEN_HANDLER2(icbt_40x, "icbt", 0x1F, 0x06, 0x08, 0x03E00001, PPC_40x_ICBT),
8737GEN_HANDLER(iccci, 0x1F, 0x06, 0x1E, 0x00000001, PPC_4xx_COMMON),
8738GEN_HANDLER(icread, 0x1F, 0x06, 0x1F, 0x03E00001, PPC_4xx_COMMON),
8739GEN_HANDLER2(rfci_40x, "rfci", 0x13, 0x13, 0x01, 0x03FF8001, PPC_40x_EXCP),
01662f3e 8740GEN_HANDLER_E(rfci, 0x13, 0x13, 0x01, 0x03FF8001, PPC_BOOKE, PPC2_BOOKE206),
5c55ff99
BS
8741GEN_HANDLER(rfdi, 0x13, 0x07, 0x01, 0x03FF8001, PPC_RFDI),
8742GEN_HANDLER(rfmci, 0x13, 0x06, 0x01, 0x03FF8001, PPC_RFMCI),
8743GEN_HANDLER2(tlbre_40x, "tlbre", 0x1F, 0x12, 0x1D, 0x00000001, PPC_40x_TLB),
8744GEN_HANDLER2(tlbsx_40x, "tlbsx", 0x1F, 0x12, 0x1C, 0x00000000, PPC_40x_TLB),
8745GEN_HANDLER2(tlbwe_40x, "tlbwe", 0x1F, 0x12, 0x1E, 0x00000001, PPC_40x_TLB),
8746GEN_HANDLER2(tlbre_440, "tlbre", 0x1F, 0x12, 0x1D, 0x00000001, PPC_BOOKE),
8747GEN_HANDLER2(tlbsx_440, "tlbsx", 0x1F, 0x12, 0x1C, 0x00000000, PPC_BOOKE),
8748GEN_HANDLER2(tlbwe_440, "tlbwe", 0x1F, 0x12, 0x1E, 0x00000001, PPC_BOOKE),
01662f3e
AG
8749GEN_HANDLER2_E(tlbre_booke206, "tlbre", 0x1F, 0x12, 0x1D, 0x00000001,
8750 PPC_NONE, PPC2_BOOKE206),
8751GEN_HANDLER2_E(tlbsx_booke206, "tlbsx", 0x1F, 0x12, 0x1C, 0x00000000,
8752 PPC_NONE, PPC2_BOOKE206),
8753GEN_HANDLER2_E(tlbwe_booke206, "tlbwe", 0x1F, 0x12, 0x1E, 0x00000001,
8754 PPC_NONE, PPC2_BOOKE206),
8755GEN_HANDLER2_E(tlbivax_booke206, "tlbivax", 0x1F, 0x12, 0x18, 0x00000001,
8756 PPC_NONE, PPC2_BOOKE206),
6d3db821
AG
8757GEN_HANDLER2_E(tlbilx_booke206, "tlbilx", 0x1F, 0x12, 0x00, 0x03800001,
8758 PPC_NONE, PPC2_BOOKE206),
d5d11a39
AG
8759GEN_HANDLER2_E(msgsnd, "msgsnd", 0x1F, 0x0E, 0x06, 0x03ff0001,
8760 PPC_NONE, PPC2_PRCNTL),
9e0b5cb1
AG
8761GEN_HANDLER2_E(msgclr, "msgclr", 0x1F, 0x0E, 0x07, 0x03ff0001,
8762 PPC_NONE, PPC2_PRCNTL),
5c55ff99 8763GEN_HANDLER(wrtee, 0x1F, 0x03, 0x04, 0x000FFC01, PPC_WRTEE),
fbe73008 8764GEN_HANDLER(wrteei, 0x1F, 0x03, 0x05, 0x000E7C01, PPC_WRTEE),
5c55ff99 8765GEN_HANDLER(dlmzb, 0x1F, 0x0E, 0x02, 0x00000000, PPC_440_SPEC),
01662f3e
AG
8766GEN_HANDLER_E(mbar, 0x1F, 0x16, 0x1a, 0x001FF801,
8767 PPC_BOOKE, PPC2_BOOKE206),
dcb2b9e1 8768GEN_HANDLER(msync_4xx, 0x1F, 0x16, 0x12, 0x03FFF801, PPC_BOOKE),
01662f3e
AG
8769GEN_HANDLER2_E(icbt_440, "icbt", 0x1F, 0x16, 0x00, 0x03E00001,
8770 PPC_BOOKE, PPC2_BOOKE206),
5c55ff99
BS
8771GEN_HANDLER(lvsl, 0x1f, 0x06, 0x00, 0x00000001, PPC_ALTIVEC),
8772GEN_HANDLER(lvsr, 0x1f, 0x06, 0x01, 0x00000001, PPC_ALTIVEC),
8773GEN_HANDLER(mfvscr, 0x04, 0x2, 0x18, 0x001ff800, PPC_ALTIVEC),
8774GEN_HANDLER(mtvscr, 0x04, 0x2, 0x19, 0x03ff0000, PPC_ALTIVEC),
8775GEN_HANDLER(vsldoi, 0x04, 0x16, 0xFF, 0x00000400, PPC_ALTIVEC),
8776GEN_HANDLER(vmladduhm, 0x04, 0x11, 0xFF, 0x00000000, PPC_ALTIVEC),
8777GEN_HANDLER2(evsel0, "evsel", 0x04, 0x1c, 0x09, 0x00000000, PPC_SPE),
8778GEN_HANDLER2(evsel1, "evsel", 0x04, 0x1d, 0x09, 0x00000000, PPC_SPE),
8779GEN_HANDLER2(evsel2, "evsel", 0x04, 0x1e, 0x09, 0x00000000, PPC_SPE),
8780GEN_HANDLER2(evsel3, "evsel", 0x04, 0x1f, 0x09, 0x00000000, PPC_SPE),
8781
8782#undef GEN_INT_ARITH_ADD
8783#undef GEN_INT_ARITH_ADD_CONST
8784#define GEN_INT_ARITH_ADD(name, opc3, add_ca, compute_ca, compute_ov) \
8785GEN_HANDLER(name, 0x1F, 0x0A, opc3, 0x00000000, PPC_INTEGER),
8786#define GEN_INT_ARITH_ADD_CONST(name, opc3, const_val, \
8787 add_ca, compute_ca, compute_ov) \
8788GEN_HANDLER(name, 0x1F, 0x0A, opc3, 0x0000F800, PPC_INTEGER),
8789GEN_INT_ARITH_ADD(add, 0x08, 0, 0, 0)
8790GEN_INT_ARITH_ADD(addo, 0x18, 0, 0, 1)
8791GEN_INT_ARITH_ADD(addc, 0x00, 0, 1, 0)
8792GEN_INT_ARITH_ADD(addco, 0x10, 0, 1, 1)
8793GEN_INT_ARITH_ADD(adde, 0x04, 1, 1, 0)
8794GEN_INT_ARITH_ADD(addeo, 0x14, 1, 1, 1)
8795GEN_INT_ARITH_ADD_CONST(addme, 0x07, -1LL, 1, 1, 0)
8796GEN_INT_ARITH_ADD_CONST(addmeo, 0x17, -1LL, 1, 1, 1)
8797GEN_INT_ARITH_ADD_CONST(addze, 0x06, 0, 1, 1, 0)
8798GEN_INT_ARITH_ADD_CONST(addzeo, 0x16, 0, 1, 1, 1)
8799
8800#undef GEN_INT_ARITH_DIVW
8801#define GEN_INT_ARITH_DIVW(name, opc3, sign, compute_ov) \
8802GEN_HANDLER(name, 0x1F, 0x0B, opc3, 0x00000000, PPC_INTEGER)
8803GEN_INT_ARITH_DIVW(divwu, 0x0E, 0, 0),
8804GEN_INT_ARITH_DIVW(divwuo, 0x1E, 0, 1),
8805GEN_INT_ARITH_DIVW(divw, 0x0F, 1, 0),
8806GEN_INT_ARITH_DIVW(divwo, 0x1F, 1, 1),
8807
8808#if defined(TARGET_PPC64)
8809#undef GEN_INT_ARITH_DIVD
8810#define GEN_INT_ARITH_DIVD(name, opc3, sign, compute_ov) \
8811GEN_HANDLER(name, 0x1F, 0x09, opc3, 0x00000000, PPC_64B)
8812GEN_INT_ARITH_DIVD(divdu, 0x0E, 0, 0),
8813GEN_INT_ARITH_DIVD(divduo, 0x1E, 0, 1),
8814GEN_INT_ARITH_DIVD(divd, 0x0F, 1, 0),
8815GEN_INT_ARITH_DIVD(divdo, 0x1F, 1, 1),
8816
8817#undef GEN_INT_ARITH_MUL_HELPER
8818#define GEN_INT_ARITH_MUL_HELPER(name, opc3) \
8819GEN_HANDLER(name, 0x1F, 0x09, opc3, 0x00000000, PPC_64B)
8820GEN_INT_ARITH_MUL_HELPER(mulhdu, 0x00),
8821GEN_INT_ARITH_MUL_HELPER(mulhd, 0x02),
8822GEN_INT_ARITH_MUL_HELPER(mulldo, 0x17),
8823#endif
8824
8825#undef GEN_INT_ARITH_SUBF
8826#undef GEN_INT_ARITH_SUBF_CONST
8827#define GEN_INT_ARITH_SUBF(name, opc3, add_ca, compute_ca, compute_ov) \
8828GEN_HANDLER(name, 0x1F, 0x08, opc3, 0x00000000, PPC_INTEGER),
8829#define GEN_INT_ARITH_SUBF_CONST(name, opc3, const_val, \
8830 add_ca, compute_ca, compute_ov) \
8831GEN_HANDLER(name, 0x1F, 0x08, opc3, 0x0000F800, PPC_INTEGER),
8832GEN_INT_ARITH_SUBF(subf, 0x01, 0, 0, 0)
8833GEN_INT_ARITH_SUBF(subfo, 0x11, 0, 0, 1)
8834GEN_INT_ARITH_SUBF(subfc, 0x00, 0, 1, 0)
8835GEN_INT_ARITH_SUBF(subfco, 0x10, 0, 1, 1)
8836GEN_INT_ARITH_SUBF(subfe, 0x04, 1, 1, 0)
8837GEN_INT_ARITH_SUBF(subfeo, 0x14, 1, 1, 1)
8838GEN_INT_ARITH_SUBF_CONST(subfme, 0x07, -1LL, 1, 1, 0)
8839GEN_INT_ARITH_SUBF_CONST(subfmeo, 0x17, -1LL, 1, 1, 1)
8840GEN_INT_ARITH_SUBF_CONST(subfze, 0x06, 0, 1, 1, 0)
8841GEN_INT_ARITH_SUBF_CONST(subfzeo, 0x16, 0, 1, 1, 1)
8842
8843#undef GEN_LOGICAL1
8844#undef GEN_LOGICAL2
8845#define GEN_LOGICAL2(name, tcg_op, opc, type) \
8846GEN_HANDLER(name, 0x1F, 0x1C, opc, 0x00000000, type)
8847#define GEN_LOGICAL1(name, tcg_op, opc, type) \
8848GEN_HANDLER(name, 0x1F, 0x1A, opc, 0x00000000, type)
8849GEN_LOGICAL2(and, tcg_gen_and_tl, 0x00, PPC_INTEGER),
8850GEN_LOGICAL2(andc, tcg_gen_andc_tl, 0x01, PPC_INTEGER),
8851GEN_LOGICAL2(eqv, tcg_gen_eqv_tl, 0x08, PPC_INTEGER),
8852GEN_LOGICAL1(extsb, tcg_gen_ext8s_tl, 0x1D, PPC_INTEGER),
8853GEN_LOGICAL1(extsh, tcg_gen_ext16s_tl, 0x1C, PPC_INTEGER),
8854GEN_LOGICAL2(nand, tcg_gen_nand_tl, 0x0E, PPC_INTEGER),
8855GEN_LOGICAL2(nor, tcg_gen_nor_tl, 0x03, PPC_INTEGER),
8856GEN_LOGICAL2(orc, tcg_gen_orc_tl, 0x0C, PPC_INTEGER),
8857#if defined(TARGET_PPC64)
8858GEN_LOGICAL1(extsw, tcg_gen_ext32s_tl, 0x1E, PPC_64B),
8859#endif
8860
8861#if defined(TARGET_PPC64)
8862#undef GEN_PPC64_R2
8863#undef GEN_PPC64_R4
8864#define GEN_PPC64_R2(name, opc1, opc2) \
8865GEN_HANDLER2(name##0, stringify(name), opc1, opc2, 0xFF, 0x00000000, PPC_64B),\
8866GEN_HANDLER2(name##1, stringify(name), opc1, opc2 | 0x10, 0xFF, 0x00000000, \
8867 PPC_64B)
8868#define GEN_PPC64_R4(name, opc1, opc2) \
8869GEN_HANDLER2(name##0, stringify(name), opc1, opc2, 0xFF, 0x00000000, PPC_64B),\
8870GEN_HANDLER2(name##1, stringify(name), opc1, opc2 | 0x01, 0xFF, 0x00000000, \
8871 PPC_64B), \
8872GEN_HANDLER2(name##2, stringify(name), opc1, opc2 | 0x10, 0xFF, 0x00000000, \
8873 PPC_64B), \
8874GEN_HANDLER2(name##3, stringify(name), opc1, opc2 | 0x11, 0xFF, 0x00000000, \
8875 PPC_64B)
8876GEN_PPC64_R4(rldicl, 0x1E, 0x00),
8877GEN_PPC64_R4(rldicr, 0x1E, 0x02),
8878GEN_PPC64_R4(rldic, 0x1E, 0x04),
8879GEN_PPC64_R2(rldcl, 0x1E, 0x08),
8880GEN_PPC64_R2(rldcr, 0x1E, 0x09),
8881GEN_PPC64_R4(rldimi, 0x1E, 0x06),
8882#endif
8883
8884#undef _GEN_FLOAT_ACB
8885#undef GEN_FLOAT_ACB
8886#undef _GEN_FLOAT_AB
8887#undef GEN_FLOAT_AB
8888#undef _GEN_FLOAT_AC
8889#undef GEN_FLOAT_AC
8890#undef GEN_FLOAT_B
8891#undef GEN_FLOAT_BS
8892#define _GEN_FLOAT_ACB(name, op, op1, op2, isfloat, set_fprf, type) \
8893GEN_HANDLER(f##name, op1, op2, 0xFF, 0x00000000, type)
8894#define GEN_FLOAT_ACB(name, op2, set_fprf, type) \
8895_GEN_FLOAT_ACB(name, name, 0x3F, op2, 0, set_fprf, type), \
8896_GEN_FLOAT_ACB(name##s, name, 0x3B, op2, 1, set_fprf, type)
8897#define _GEN_FLOAT_AB(name, op, op1, op2, inval, isfloat, set_fprf, type) \
8898GEN_HANDLER(f##name, op1, op2, 0xFF, inval, type)
8899#define GEN_FLOAT_AB(name, op2, inval, set_fprf, type) \
8900_GEN_FLOAT_AB(name, name, 0x3F, op2, inval, 0, set_fprf, type), \
8901_GEN_FLOAT_AB(name##s, name, 0x3B, op2, inval, 1, set_fprf, type)
8902#define _GEN_FLOAT_AC(name, op, op1, op2, inval, isfloat, set_fprf, type) \
8903GEN_HANDLER(f##name, op1, op2, 0xFF, inval, type)
8904#define GEN_FLOAT_AC(name, op2, inval, set_fprf, type) \
8905_GEN_FLOAT_AC(name, name, 0x3F, op2, inval, 0, set_fprf, type), \
8906_GEN_FLOAT_AC(name##s, name, 0x3B, op2, inval, 1, set_fprf, type)
8907#define GEN_FLOAT_B(name, op2, op3, set_fprf, type) \
8908GEN_HANDLER(f##name, 0x3F, op2, op3, 0x001F0000, type)
8909#define GEN_FLOAT_BS(name, op1, op2, set_fprf, type) \
8910GEN_HANDLER(f##name, op1, op2, 0xFF, 0x001F07C0, type)
8911
8912GEN_FLOAT_AB(add, 0x15, 0x000007C0, 1, PPC_FLOAT),
8913GEN_FLOAT_AB(div, 0x12, 0x000007C0, 1, PPC_FLOAT),
8914GEN_FLOAT_AC(mul, 0x19, 0x0000F800, 1, PPC_FLOAT),
8915GEN_FLOAT_BS(re, 0x3F, 0x18, 1, PPC_FLOAT_EXT),
8916GEN_FLOAT_BS(res, 0x3B, 0x18, 1, PPC_FLOAT_FRES),
8917GEN_FLOAT_BS(rsqrte, 0x3F, 0x1A, 1, PPC_FLOAT_FRSQRTE),
8918_GEN_FLOAT_ACB(sel, sel, 0x3F, 0x17, 0, 0, PPC_FLOAT_FSEL),
8919GEN_FLOAT_AB(sub, 0x14, 0x000007C0, 1, PPC_FLOAT),
8920GEN_FLOAT_ACB(madd, 0x1D, 1, PPC_FLOAT),
8921GEN_FLOAT_ACB(msub, 0x1C, 1, PPC_FLOAT),
8922GEN_FLOAT_ACB(nmadd, 0x1F, 1, PPC_FLOAT),
8923GEN_FLOAT_ACB(nmsub, 0x1E, 1, PPC_FLOAT),
8924GEN_FLOAT_B(ctiw, 0x0E, 0x00, 0, PPC_FLOAT),
8925GEN_FLOAT_B(ctiwz, 0x0F, 0x00, 0, PPC_FLOAT),
8926GEN_FLOAT_B(rsp, 0x0C, 0x00, 1, PPC_FLOAT),
8927#if defined(TARGET_PPC64)
8928GEN_FLOAT_B(cfid, 0x0E, 0x1A, 1, PPC_64B),
8929GEN_FLOAT_B(ctid, 0x0E, 0x19, 0, PPC_64B),
8930GEN_FLOAT_B(ctidz, 0x0F, 0x19, 0, PPC_64B),
8931#endif
8932GEN_FLOAT_B(rin, 0x08, 0x0C, 1, PPC_FLOAT_EXT),
8933GEN_FLOAT_B(riz, 0x08, 0x0D, 1, PPC_FLOAT_EXT),
8934GEN_FLOAT_B(rip, 0x08, 0x0E, 1, PPC_FLOAT_EXT),
8935GEN_FLOAT_B(rim, 0x08, 0x0F, 1, PPC_FLOAT_EXT),
8936GEN_FLOAT_B(abs, 0x08, 0x08, 0, PPC_FLOAT),
8937GEN_FLOAT_B(nabs, 0x08, 0x04, 0, PPC_FLOAT),
8938GEN_FLOAT_B(neg, 0x08, 0x01, 0, PPC_FLOAT),
8939
8940#undef GEN_LD
8941#undef GEN_LDU
8942#undef GEN_LDUX
cd6e9320 8943#undef GEN_LDX_E
5c55ff99
BS
8944#undef GEN_LDS
8945#define GEN_LD(name, ldop, opc, type) \
8946GEN_HANDLER(name, opc, 0xFF, 0xFF, 0x00000000, type),
8947#define GEN_LDU(name, ldop, opc, type) \
8948GEN_HANDLER(name##u, opc, 0xFF, 0xFF, 0x00000000, type),
8949#define GEN_LDUX(name, ldop, opc2, opc3, type) \
8950GEN_HANDLER(name##ux, 0x1F, opc2, opc3, 0x00000001, type),
cd6e9320
TH
8951#define GEN_LDX_E(name, ldop, opc2, opc3, type, type2) \
8952GEN_HANDLER_E(name##x, 0x1F, opc2, opc3, 0x00000001, type, type2),
5c55ff99
BS
8953#define GEN_LDS(name, ldop, op, type) \
8954GEN_LD(name, ldop, op | 0x20, type) \
8955GEN_LDU(name, ldop, op | 0x21, type) \
8956GEN_LDUX(name, ldop, 0x17, op | 0x01, type) \
8957GEN_LDX(name, ldop, 0x17, op | 0x00, type)
8958
8959GEN_LDS(lbz, ld8u, 0x02, PPC_INTEGER)
8960GEN_LDS(lha, ld16s, 0x0A, PPC_INTEGER)
8961GEN_LDS(lhz, ld16u, 0x08, PPC_INTEGER)
8962GEN_LDS(lwz, ld32u, 0x00, PPC_INTEGER)
8963#if defined(TARGET_PPC64)
8964GEN_LDUX(lwa, ld32s, 0x15, 0x0B, PPC_64B)
8965GEN_LDX(lwa, ld32s, 0x15, 0x0A, PPC_64B)
8966GEN_LDUX(ld, ld64, 0x15, 0x01, PPC_64B)
8967GEN_LDX(ld, ld64, 0x15, 0x00, PPC_64B)
cd6e9320 8968GEN_LDX_E(ldbr, ld64ur, 0x14, 0x10, PPC_NONE, PPC2_DBRX)
5c55ff99
BS
8969#endif
8970GEN_LDX(lhbr, ld16ur, 0x16, 0x18, PPC_INTEGER)
8971GEN_LDX(lwbr, ld32ur, 0x16, 0x10, PPC_INTEGER)
8972
8973#undef GEN_ST
8974#undef GEN_STU
8975#undef GEN_STUX
cd6e9320 8976#undef GEN_STX_E
5c55ff99
BS
8977#undef GEN_STS
8978#define GEN_ST(name, stop, opc, type) \
8979GEN_HANDLER(name, opc, 0xFF, 0xFF, 0x00000000, type),
8980#define GEN_STU(name, stop, opc, type) \
8981GEN_HANDLER(stop##u, opc, 0xFF, 0xFF, 0x00000000, type),
8982#define GEN_STUX(name, stop, opc2, opc3, type) \
8983GEN_HANDLER(name##ux, 0x1F, opc2, opc3, 0x00000001, type),
cd6e9320
TH
8984#define GEN_STX_E(name, stop, opc2, opc3, type, type2) \
8985GEN_HANDLER_E(name##x, 0x1F, opc2, opc3, 0x00000001, type, type2),
5c55ff99
BS
8986#define GEN_STS(name, stop, op, type) \
8987GEN_ST(name, stop, op | 0x20, type) \
8988GEN_STU(name, stop, op | 0x21, type) \
8989GEN_STUX(name, stop, 0x17, op | 0x01, type) \
8990GEN_STX(name, stop, 0x17, op | 0x00, type)
8991
8992GEN_STS(stb, st8, 0x06, PPC_INTEGER)
8993GEN_STS(sth, st16, 0x0C, PPC_INTEGER)
8994GEN_STS(stw, st32, 0x04, PPC_INTEGER)
8995#if defined(TARGET_PPC64)
8996GEN_STUX(std, st64, 0x15, 0x05, PPC_64B)
8997GEN_STX(std, st64, 0x15, 0x04, PPC_64B)
cd6e9320 8998GEN_STX_E(stdbr, st64r, 0x14, 0x14, PPC_NONE, PPC2_DBRX)
5c55ff99
BS
8999#endif
9000GEN_STX(sthbr, st16r, 0x16, 0x1C, PPC_INTEGER)
9001GEN_STX(stwbr, st32r, 0x16, 0x14, PPC_INTEGER)
9002
9003#undef GEN_LDF
9004#undef GEN_LDUF
9005#undef GEN_LDUXF
9006#undef GEN_LDXF
9007#undef GEN_LDFS
9008#define GEN_LDF(name, ldop, opc, type) \
9009GEN_HANDLER(name, opc, 0xFF, 0xFF, 0x00000000, type),
9010#define GEN_LDUF(name, ldop, opc, type) \
9011GEN_HANDLER(name##u, opc, 0xFF, 0xFF, 0x00000000, type),
9012#define GEN_LDUXF(name, ldop, opc, type) \
9013GEN_HANDLER(name##ux, 0x1F, 0x17, opc, 0x00000001, type),
9014#define GEN_LDXF(name, ldop, opc2, opc3, type) \
9015GEN_HANDLER(name##x, 0x1F, opc2, opc3, 0x00000001, type),
9016#define GEN_LDFS(name, ldop, op, type) \
9017GEN_LDF(name, ldop, op | 0x20, type) \
9018GEN_LDUF(name, ldop, op | 0x21, type) \
9019GEN_LDUXF(name, ldop, op | 0x01, type) \
9020GEN_LDXF(name, ldop, 0x17, op | 0x00, type)
9021
9022GEN_LDFS(lfd, ld64, 0x12, PPC_FLOAT)
9023GEN_LDFS(lfs, ld32fs, 0x10, PPC_FLOAT)
9024
9025#undef GEN_STF
9026#undef GEN_STUF
9027#undef GEN_STUXF
9028#undef GEN_STXF
9029#undef GEN_STFS
9030#define GEN_STF(name, stop, opc, type) \
9031GEN_HANDLER(name, opc, 0xFF, 0xFF, 0x00000000, type),
9032#define GEN_STUF(name, stop, opc, type) \
9033GEN_HANDLER(name##u, opc, 0xFF, 0xFF, 0x00000000, type),
9034#define GEN_STUXF(name, stop, opc, type) \
9035GEN_HANDLER(name##ux, 0x1F, 0x17, opc, 0x00000001, type),
9036#define GEN_STXF(name, stop, opc2, opc3, type) \
9037GEN_HANDLER(name##x, 0x1F, opc2, opc3, 0x00000001, type),
9038#define GEN_STFS(name, stop, op, type) \
9039GEN_STF(name, stop, op | 0x20, type) \
9040GEN_STUF(name, stop, op | 0x21, type) \
9041GEN_STUXF(name, stop, op | 0x01, type) \
9042GEN_STXF(name, stop, 0x17, op | 0x00, type)
9043
9044GEN_STFS(stfd, st64, 0x16, PPC_FLOAT)
9045GEN_STFS(stfs, st32fs, 0x14, PPC_FLOAT)
9046GEN_STXF(stfiw, st32fiw, 0x17, 0x1E, PPC_FLOAT_STFIWX)
9047
9048#undef GEN_CRLOGIC
9049#define GEN_CRLOGIC(name, tcg_op, opc) \
9050GEN_HANDLER(name, 0x13, 0x01, opc, 0x00000001, PPC_INTEGER)
9051GEN_CRLOGIC(crand, tcg_gen_and_i32, 0x08),
9052GEN_CRLOGIC(crandc, tcg_gen_andc_i32, 0x04),
9053GEN_CRLOGIC(creqv, tcg_gen_eqv_i32, 0x09),
9054GEN_CRLOGIC(crnand, tcg_gen_nand_i32, 0x07),
9055GEN_CRLOGIC(crnor, tcg_gen_nor_i32, 0x01),
9056GEN_CRLOGIC(cror, tcg_gen_or_i32, 0x0E),
9057GEN_CRLOGIC(crorc, tcg_gen_orc_i32, 0x0D),
9058GEN_CRLOGIC(crxor, tcg_gen_xor_i32, 0x06),
9059
9060#undef GEN_MAC_HANDLER
9061#define GEN_MAC_HANDLER(name, opc2, opc3) \
9062GEN_HANDLER(name, 0x04, opc2, opc3, 0x00000000, PPC_405_MAC)
9063GEN_MAC_HANDLER(macchw, 0x0C, 0x05),
9064GEN_MAC_HANDLER(macchwo, 0x0C, 0x15),
9065GEN_MAC_HANDLER(macchws, 0x0C, 0x07),
9066GEN_MAC_HANDLER(macchwso, 0x0C, 0x17),
9067GEN_MAC_HANDLER(macchwsu, 0x0C, 0x06),
9068GEN_MAC_HANDLER(macchwsuo, 0x0C, 0x16),
9069GEN_MAC_HANDLER(macchwu, 0x0C, 0x04),
9070GEN_MAC_HANDLER(macchwuo, 0x0C, 0x14),
9071GEN_MAC_HANDLER(machhw, 0x0C, 0x01),
9072GEN_MAC_HANDLER(machhwo, 0x0C, 0x11),
9073GEN_MAC_HANDLER(machhws, 0x0C, 0x03),
9074GEN_MAC_HANDLER(machhwso, 0x0C, 0x13),
9075GEN_MAC_HANDLER(machhwsu, 0x0C, 0x02),
9076GEN_MAC_HANDLER(machhwsuo, 0x0C, 0x12),
9077GEN_MAC_HANDLER(machhwu, 0x0C, 0x00),
9078GEN_MAC_HANDLER(machhwuo, 0x0C, 0x10),
9079GEN_MAC_HANDLER(maclhw, 0x0C, 0x0D),
9080GEN_MAC_HANDLER(maclhwo, 0x0C, 0x1D),
9081GEN_MAC_HANDLER(maclhws, 0x0C, 0x0F),
9082GEN_MAC_HANDLER(maclhwso, 0x0C, 0x1F),
9083GEN_MAC_HANDLER(maclhwu, 0x0C, 0x0C),
9084GEN_MAC_HANDLER(maclhwuo, 0x0C, 0x1C),
9085GEN_MAC_HANDLER(maclhwsu, 0x0C, 0x0E),
9086GEN_MAC_HANDLER(maclhwsuo, 0x0C, 0x1E),
9087GEN_MAC_HANDLER(nmacchw, 0x0E, 0x05),
9088GEN_MAC_HANDLER(nmacchwo, 0x0E, 0x15),
9089GEN_MAC_HANDLER(nmacchws, 0x0E, 0x07),
9090GEN_MAC_HANDLER(nmacchwso, 0x0E, 0x17),
9091GEN_MAC_HANDLER(nmachhw, 0x0E, 0x01),
9092GEN_MAC_HANDLER(nmachhwo, 0x0E, 0x11),
9093GEN_MAC_HANDLER(nmachhws, 0x0E, 0x03),
9094GEN_MAC_HANDLER(nmachhwso, 0x0E, 0x13),
9095GEN_MAC_HANDLER(nmaclhw, 0x0E, 0x0D),
9096GEN_MAC_HANDLER(nmaclhwo, 0x0E, 0x1D),
9097GEN_MAC_HANDLER(nmaclhws, 0x0E, 0x0F),
9098GEN_MAC_HANDLER(nmaclhwso, 0x0E, 0x1F),
9099GEN_MAC_HANDLER(mulchw, 0x08, 0x05),
9100GEN_MAC_HANDLER(mulchwu, 0x08, 0x04),
9101GEN_MAC_HANDLER(mulhhw, 0x08, 0x01),
9102GEN_MAC_HANDLER(mulhhwu, 0x08, 0x00),
9103GEN_MAC_HANDLER(mullhw, 0x08, 0x0D),
9104GEN_MAC_HANDLER(mullhwu, 0x08, 0x0C),
9105
9106#undef GEN_VR_LDX
9107#undef GEN_VR_STX
9108#undef GEN_VR_LVE
9109#undef GEN_VR_STVE
9110#define GEN_VR_LDX(name, opc2, opc3) \
9111GEN_HANDLER(name, 0x1F, opc2, opc3, 0x00000001, PPC_ALTIVEC)
9112#define GEN_VR_STX(name, opc2, opc3) \
9113GEN_HANDLER(st##name, 0x1F, opc2, opc3, 0x00000001, PPC_ALTIVEC)
9114#define GEN_VR_LVE(name, opc2, opc3) \
9115 GEN_HANDLER(lve##name, 0x1F, opc2, opc3, 0x00000001, PPC_ALTIVEC)
9116#define GEN_VR_STVE(name, opc2, opc3) \
9117 GEN_HANDLER(stve##name, 0x1F, opc2, opc3, 0x00000001, PPC_ALTIVEC)
9118GEN_VR_LDX(lvx, 0x07, 0x03),
9119GEN_VR_LDX(lvxl, 0x07, 0x0B),
9120GEN_VR_LVE(bx, 0x07, 0x00),
9121GEN_VR_LVE(hx, 0x07, 0x01),
9122GEN_VR_LVE(wx, 0x07, 0x02),
9123GEN_VR_STX(svx, 0x07, 0x07),
9124GEN_VR_STX(svxl, 0x07, 0x0F),
9125GEN_VR_STVE(bx, 0x07, 0x04),
9126GEN_VR_STVE(hx, 0x07, 0x05),
9127GEN_VR_STVE(wx, 0x07, 0x06),
9128
9129#undef GEN_VX_LOGICAL
9130#define GEN_VX_LOGICAL(name, tcg_op, opc2, opc3) \
9131GEN_HANDLER(name, 0x04, opc2, opc3, 0x00000000, PPC_ALTIVEC)
9132GEN_VX_LOGICAL(vand, tcg_gen_and_i64, 2, 16),
9133GEN_VX_LOGICAL(vandc, tcg_gen_andc_i64, 2, 17),
9134GEN_VX_LOGICAL(vor, tcg_gen_or_i64, 2, 18),
9135GEN_VX_LOGICAL(vxor, tcg_gen_xor_i64, 2, 19),
9136GEN_VX_LOGICAL(vnor, tcg_gen_nor_i64, 2, 20),
9137
9138#undef GEN_VXFORM
9139#define GEN_VXFORM(name, opc2, opc3) \
9140GEN_HANDLER(name, 0x04, opc2, opc3, 0x00000000, PPC_ALTIVEC)
9141GEN_VXFORM(vaddubm, 0, 0),
9142GEN_VXFORM(vadduhm, 0, 1),
9143GEN_VXFORM(vadduwm, 0, 2),
9144GEN_VXFORM(vsububm, 0, 16),
9145GEN_VXFORM(vsubuhm, 0, 17),
9146GEN_VXFORM(vsubuwm, 0, 18),
9147GEN_VXFORM(vmaxub, 1, 0),
9148GEN_VXFORM(vmaxuh, 1, 1),
9149GEN_VXFORM(vmaxuw, 1, 2),
9150GEN_VXFORM(vmaxsb, 1, 4),
9151GEN_VXFORM(vmaxsh, 1, 5),
9152GEN_VXFORM(vmaxsw, 1, 6),
9153GEN_VXFORM(vminub, 1, 8),
9154GEN_VXFORM(vminuh, 1, 9),
9155GEN_VXFORM(vminuw, 1, 10),
9156GEN_VXFORM(vminsb, 1, 12),
9157GEN_VXFORM(vminsh, 1, 13),
9158GEN_VXFORM(vminsw, 1, 14),
9159GEN_VXFORM(vavgub, 1, 16),
9160GEN_VXFORM(vavguh, 1, 17),
9161GEN_VXFORM(vavguw, 1, 18),
9162GEN_VXFORM(vavgsb, 1, 20),
9163GEN_VXFORM(vavgsh, 1, 21),
9164GEN_VXFORM(vavgsw, 1, 22),
9165GEN_VXFORM(vmrghb, 6, 0),
9166GEN_VXFORM(vmrghh, 6, 1),
9167GEN_VXFORM(vmrghw, 6, 2),
9168GEN_VXFORM(vmrglb, 6, 4),
9169GEN_VXFORM(vmrglh, 6, 5),
9170GEN_VXFORM(vmrglw, 6, 6),
9171GEN_VXFORM(vmuloub, 4, 0),
9172GEN_VXFORM(vmulouh, 4, 1),
9173GEN_VXFORM(vmulosb, 4, 4),
9174GEN_VXFORM(vmulosh, 4, 5),
9175GEN_VXFORM(vmuleub, 4, 8),
9176GEN_VXFORM(vmuleuh, 4, 9),
9177GEN_VXFORM(vmulesb, 4, 12),
9178GEN_VXFORM(vmulesh, 4, 13),
9179GEN_VXFORM(vslb, 2, 4),
9180GEN_VXFORM(vslh, 2, 5),
9181GEN_VXFORM(vslw, 2, 6),
9182GEN_VXFORM(vsrb, 2, 8),
9183GEN_VXFORM(vsrh, 2, 9),
9184GEN_VXFORM(vsrw, 2, 10),
9185GEN_VXFORM(vsrab, 2, 12),
9186GEN_VXFORM(vsrah, 2, 13),
9187GEN_VXFORM(vsraw, 2, 14),
9188GEN_VXFORM(vslo, 6, 16),
9189GEN_VXFORM(vsro, 6, 17),
9190GEN_VXFORM(vaddcuw, 0, 6),
9191GEN_VXFORM(vsubcuw, 0, 22),
9192GEN_VXFORM(vaddubs, 0, 8),
9193GEN_VXFORM(vadduhs, 0, 9),
9194GEN_VXFORM(vadduws, 0, 10),
9195GEN_VXFORM(vaddsbs, 0, 12),
9196GEN_VXFORM(vaddshs, 0, 13),
9197GEN_VXFORM(vaddsws, 0, 14),
9198GEN_VXFORM(vsububs, 0, 24),
9199GEN_VXFORM(vsubuhs, 0, 25),
9200GEN_VXFORM(vsubuws, 0, 26),
9201GEN_VXFORM(vsubsbs, 0, 28),
9202GEN_VXFORM(vsubshs, 0, 29),
9203GEN_VXFORM(vsubsws, 0, 30),
9204GEN_VXFORM(vrlb, 2, 0),
9205GEN_VXFORM(vrlh, 2, 1),
9206GEN_VXFORM(vrlw, 2, 2),
9207GEN_VXFORM(vsl, 2, 7),
9208GEN_VXFORM(vsr, 2, 11),
9209GEN_VXFORM(vpkuhum, 7, 0),
9210GEN_VXFORM(vpkuwum, 7, 1),
9211GEN_VXFORM(vpkuhus, 7, 2),
9212GEN_VXFORM(vpkuwus, 7, 3),
9213GEN_VXFORM(vpkshus, 7, 4),
9214GEN_VXFORM(vpkswus, 7, 5),
9215GEN_VXFORM(vpkshss, 7, 6),
9216GEN_VXFORM(vpkswss, 7, 7),
9217GEN_VXFORM(vpkpx, 7, 12),
9218GEN_VXFORM(vsum4ubs, 4, 24),
9219GEN_VXFORM(vsum4sbs, 4, 28),
9220GEN_VXFORM(vsum4shs, 4, 25),
9221GEN_VXFORM(vsum2sws, 4, 26),
9222GEN_VXFORM(vsumsws, 4, 30),
9223GEN_VXFORM(vaddfp, 5, 0),
9224GEN_VXFORM(vsubfp, 5, 1),
9225GEN_VXFORM(vmaxfp, 5, 16),
9226GEN_VXFORM(vminfp, 5, 17),
9227
9228#undef GEN_VXRFORM1
9229#undef GEN_VXRFORM
9230#define GEN_VXRFORM1(opname, name, str, opc2, opc3) \
9231 GEN_HANDLER2(name, str, 0x4, opc2, opc3, 0x00000000, PPC_ALTIVEC),
9232#define GEN_VXRFORM(name, opc2, opc3) \
9233 GEN_VXRFORM1(name, name, #name, opc2, opc3) \
9234 GEN_VXRFORM1(name##_dot, name##_, #name ".", opc2, (opc3 | (0x1 << 4)))
9235GEN_VXRFORM(vcmpequb, 3, 0)
9236GEN_VXRFORM(vcmpequh, 3, 1)
9237GEN_VXRFORM(vcmpequw, 3, 2)
9238GEN_VXRFORM(vcmpgtsb, 3, 12)
9239GEN_VXRFORM(vcmpgtsh, 3, 13)
9240GEN_VXRFORM(vcmpgtsw, 3, 14)
9241GEN_VXRFORM(vcmpgtub, 3, 8)
9242GEN_VXRFORM(vcmpgtuh, 3, 9)
9243GEN_VXRFORM(vcmpgtuw, 3, 10)
9244GEN_VXRFORM(vcmpeqfp, 3, 3)
9245GEN_VXRFORM(vcmpgefp, 3, 7)
9246GEN_VXRFORM(vcmpgtfp, 3, 11)
9247GEN_VXRFORM(vcmpbfp, 3, 15)
9248
9249#undef GEN_VXFORM_SIMM
9250#define GEN_VXFORM_SIMM(name, opc2, opc3) \
9251 GEN_HANDLER(name, 0x04, opc2, opc3, 0x00000000, PPC_ALTIVEC)
9252GEN_VXFORM_SIMM(vspltisb, 6, 12),
9253GEN_VXFORM_SIMM(vspltish, 6, 13),
9254GEN_VXFORM_SIMM(vspltisw, 6, 14),
9255
9256#undef GEN_VXFORM_NOA
9257#define GEN_VXFORM_NOA(name, opc2, opc3) \
9258 GEN_HANDLER(name, 0x04, opc2, opc3, 0x001f0000, PPC_ALTIVEC)
9259GEN_VXFORM_NOA(vupkhsb, 7, 8),
9260GEN_VXFORM_NOA(vupkhsh, 7, 9),
9261GEN_VXFORM_NOA(vupklsb, 7, 10),
9262GEN_VXFORM_NOA(vupklsh, 7, 11),
9263GEN_VXFORM_NOA(vupkhpx, 7, 13),
9264GEN_VXFORM_NOA(vupklpx, 7, 15),
9265GEN_VXFORM_NOA(vrefp, 5, 4),
9266GEN_VXFORM_NOA(vrsqrtefp, 5, 5),
0bffbc6c 9267GEN_VXFORM_NOA(vexptefp, 5, 6),
5c55ff99
BS
9268GEN_VXFORM_NOA(vlogefp, 5, 7),
9269GEN_VXFORM_NOA(vrfim, 5, 8),
9270GEN_VXFORM_NOA(vrfin, 5, 9),
9271GEN_VXFORM_NOA(vrfip, 5, 10),
9272GEN_VXFORM_NOA(vrfiz, 5, 11),
9273
9274#undef GEN_VXFORM_UIMM
9275#define GEN_VXFORM_UIMM(name, opc2, opc3) \
9276 GEN_HANDLER(name, 0x04, opc2, opc3, 0x00000000, PPC_ALTIVEC)
9277GEN_VXFORM_UIMM(vspltb, 6, 8),
9278GEN_VXFORM_UIMM(vsplth, 6, 9),
9279GEN_VXFORM_UIMM(vspltw, 6, 10),
9280GEN_VXFORM_UIMM(vcfux, 5, 12),
9281GEN_VXFORM_UIMM(vcfsx, 5, 13),
9282GEN_VXFORM_UIMM(vctuxs, 5, 14),
9283GEN_VXFORM_UIMM(vctsxs, 5, 15),
9284
9285#undef GEN_VAFORM_PAIRED
9286#define GEN_VAFORM_PAIRED(name0, name1, opc2) \
9287 GEN_HANDLER(name0##_##name1, 0x04, opc2, 0xFF, 0x00000000, PPC_ALTIVEC)
9288GEN_VAFORM_PAIRED(vmhaddshs, vmhraddshs, 16),
9289GEN_VAFORM_PAIRED(vmsumubm, vmsummbm, 18),
9290GEN_VAFORM_PAIRED(vmsumuhm, vmsumuhs, 19),
9291GEN_VAFORM_PAIRED(vmsumshm, vmsumshs, 20),
9292GEN_VAFORM_PAIRED(vsel, vperm, 21),
9293GEN_VAFORM_PAIRED(vmaddfp, vnmsubfp, 23),
9294
9295#undef GEN_SPE
70560da7
FC
9296#define GEN_SPE(name0, name1, opc2, opc3, inval0, inval1, type) \
9297 GEN_OPCODE_DUAL(name0##_##name1, 0x04, opc2, opc3, inval0, inval1, type, PPC_NONE)
9298GEN_SPE(evaddw, speundef, 0x00, 0x08, 0x00000000, 0xFFFFFFFF, PPC_SPE),
9299GEN_SPE(evaddiw, speundef, 0x01, 0x08, 0x00000000, 0xFFFFFFFF, PPC_SPE),
9300GEN_SPE(evsubfw, speundef, 0x02, 0x08, 0x00000000, 0xFFFFFFFF, PPC_SPE),
9301GEN_SPE(evsubifw, speundef, 0x03, 0x08, 0x00000000, 0xFFFFFFFF, PPC_SPE),
9302GEN_SPE(evabs, evneg, 0x04, 0x08, 0x0000F800, 0x0000F800, PPC_SPE),
9303GEN_SPE(evextsb, evextsh, 0x05, 0x08, 0x0000F800, 0x0000F800, PPC_SPE),
9304GEN_SPE(evrndw, evcntlzw, 0x06, 0x08, 0x0000F800, 0x0000F800, PPC_SPE),
9305GEN_SPE(evcntlsw, brinc, 0x07, 0x08, 0x0000F800, 0x00000000, PPC_SPE),
9306GEN_SPE(evmra, speundef, 0x02, 0x13, 0x0000F800, 0xFFFFFFFF, PPC_SPE),
9307GEN_SPE(speundef, evand, 0x08, 0x08, 0xFFFFFFFF, 0x00000000, PPC_SPE),
9308GEN_SPE(evandc, speundef, 0x09, 0x08, 0x00000000, 0xFFFFFFFF, PPC_SPE),
9309GEN_SPE(evxor, evor, 0x0B, 0x08, 0x00000000, 0x00000000, PPC_SPE),
9310GEN_SPE(evnor, eveqv, 0x0C, 0x08, 0x00000000, 0x00000000, PPC_SPE),
9311GEN_SPE(evmwumi, evmwsmi, 0x0C, 0x11, 0x00000000, 0x00000000, PPC_SPE),
9312GEN_SPE(evmwumia, evmwsmia, 0x1C, 0x11, 0x00000000, 0x00000000, PPC_SPE),
9313GEN_SPE(evmwumiaa, evmwsmiaa, 0x0C, 0x15, 0x00000000, 0x00000000, PPC_SPE),
9314GEN_SPE(speundef, evorc, 0x0D, 0x08, 0xFFFFFFFF, 0x00000000, PPC_SPE),
9315GEN_SPE(evnand, speundef, 0x0F, 0x08, 0x00000000, 0xFFFFFFFF, PPC_SPE),
9316GEN_SPE(evsrwu, evsrws, 0x10, 0x08, 0x00000000, 0x00000000, PPC_SPE),
9317GEN_SPE(evsrwiu, evsrwis, 0x11, 0x08, 0x00000000, 0x00000000, PPC_SPE),
9318GEN_SPE(evslw, speundef, 0x12, 0x08, 0x00000000, 0xFFFFFFFF, PPC_SPE),
9319GEN_SPE(evslwi, speundef, 0x13, 0x08, 0x00000000, 0xFFFFFFFF, PPC_SPE),
9320GEN_SPE(evrlw, evsplati, 0x14, 0x08, 0x00000000, 0x0000F800, PPC_SPE),
9321GEN_SPE(evrlwi, evsplatfi, 0x15, 0x08, 0x00000000, 0x0000F800, PPC_SPE),
9322GEN_SPE(evmergehi, evmergelo, 0x16, 0x08, 0x00000000, 0x00000000, PPC_SPE),
9323GEN_SPE(evmergehilo, evmergelohi, 0x17, 0x08, 0x00000000, 0x00000000, PPC_SPE),
9324GEN_SPE(evcmpgtu, evcmpgts, 0x18, 0x08, 0x00600000, 0x00600000, PPC_SPE),
9325GEN_SPE(evcmpltu, evcmplts, 0x19, 0x08, 0x00600000, 0x00600000, PPC_SPE),
9326GEN_SPE(evcmpeq, speundef, 0x1A, 0x08, 0x00600000, 0xFFFFFFFF, PPC_SPE),
9327
9328GEN_SPE(evfsadd, evfssub, 0x00, 0x0A, 0x00000000, 0x00000000, PPC_SPE_SINGLE),
9329GEN_SPE(evfsabs, evfsnabs, 0x02, 0x0A, 0x0000F800, 0x0000F800, PPC_SPE_SINGLE),
9330GEN_SPE(evfsneg, speundef, 0x03, 0x0A, 0x0000F800, 0xFFFFFFFF, PPC_SPE_SINGLE),
9331GEN_SPE(evfsmul, evfsdiv, 0x04, 0x0A, 0x00000000, 0x00000000, PPC_SPE_SINGLE),
9332GEN_SPE(evfscmpgt, evfscmplt, 0x06, 0x0A, 0x00600000, 0x00600000, PPC_SPE_SINGLE),
9333GEN_SPE(evfscmpeq, speundef, 0x07, 0x0A, 0x00600000, 0xFFFFFFFF, PPC_SPE_SINGLE),
9334GEN_SPE(evfscfui, evfscfsi, 0x08, 0x0A, 0x00180000, 0x00180000, PPC_SPE_SINGLE),
9335GEN_SPE(evfscfuf, evfscfsf, 0x09, 0x0A, 0x00180000, 0x00180000, PPC_SPE_SINGLE),
9336GEN_SPE(evfsctui, evfsctsi, 0x0A, 0x0A, 0x00180000, 0x00180000, PPC_SPE_SINGLE),
9337GEN_SPE(evfsctuf, evfsctsf, 0x0B, 0x0A, 0x00180000, 0x00180000, PPC_SPE_SINGLE),
9338GEN_SPE(evfsctuiz, speundef, 0x0C, 0x0A, 0x00180000, 0xFFFFFFFF, PPC_SPE_SINGLE),
9339GEN_SPE(evfsctsiz, speundef, 0x0D, 0x0A, 0x00180000, 0xFFFFFFFF, PPC_SPE_SINGLE),
9340GEN_SPE(evfststgt, evfststlt, 0x0E, 0x0A, 0x00600000, 0x00600000, PPC_SPE_SINGLE),
9341GEN_SPE(evfststeq, speundef, 0x0F, 0x0A, 0x00600000, 0xFFFFFFFF, PPC_SPE_SINGLE),
9342
9343GEN_SPE(efsadd, efssub, 0x00, 0x0B, 0x00000000, 0x00000000, PPC_SPE_SINGLE),
9344GEN_SPE(efsabs, efsnabs, 0x02, 0x0B, 0x0000F800, 0x0000F800, PPC_SPE_SINGLE),
9345GEN_SPE(efsneg, speundef, 0x03, 0x0B, 0x0000F800, 0xFFFFFFFF, PPC_SPE_SINGLE),
9346GEN_SPE(efsmul, efsdiv, 0x04, 0x0B, 0x00000000, 0x00000000, PPC_SPE_SINGLE),
9347GEN_SPE(efscmpgt, efscmplt, 0x06, 0x0B, 0x00600000, 0x00600000, PPC_SPE_SINGLE),
9348GEN_SPE(efscmpeq, efscfd, 0x07, 0x0B, 0x00600000, 0x00180000, PPC_SPE_SINGLE),
9349GEN_SPE(efscfui, efscfsi, 0x08, 0x0B, 0x00180000, 0x00180000, PPC_SPE_SINGLE),
9350GEN_SPE(efscfuf, efscfsf, 0x09, 0x0B, 0x00180000, 0x00180000, PPC_SPE_SINGLE),
9351GEN_SPE(efsctui, efsctsi, 0x0A, 0x0B, 0x00180000, 0x00180000, PPC_SPE_SINGLE),
9352GEN_SPE(efsctuf, efsctsf, 0x0B, 0x0B, 0x00180000, 0x00180000, PPC_SPE_SINGLE),
9353GEN_SPE(efsctuiz, speundef, 0x0C, 0x0B, 0x00180000, 0xFFFFFFFF, PPC_SPE_SINGLE),
9354GEN_SPE(efsctsiz, speundef, 0x0D, 0x0B, 0x00180000, 0xFFFFFFFF, PPC_SPE_SINGLE),
9355GEN_SPE(efststgt, efststlt, 0x0E, 0x0B, 0x00600000, 0x00600000, PPC_SPE_SINGLE),
9356GEN_SPE(efststeq, speundef, 0x0F, 0x0B, 0x00600000, 0xFFFFFFFF, PPC_SPE_SINGLE),
9357
9358GEN_SPE(efdadd, efdsub, 0x10, 0x0B, 0x00000000, 0x00000000, PPC_SPE_DOUBLE),
9359GEN_SPE(efdcfuid, efdcfsid, 0x11, 0x0B, 0x00180000, 0x00180000, PPC_SPE_DOUBLE),
9360GEN_SPE(efdabs, efdnabs, 0x12, 0x0B, 0x0000F800, 0x0000F800, PPC_SPE_DOUBLE),
9361GEN_SPE(efdneg, speundef, 0x13, 0x0B, 0x0000F800, 0xFFFFFFFF, PPC_SPE_DOUBLE),
9362GEN_SPE(efdmul, efddiv, 0x14, 0x0B, 0x00000000, 0x00000000, PPC_SPE_DOUBLE),
9363GEN_SPE(efdctuidz, efdctsidz, 0x15, 0x0B, 0x00180000, 0x00180000, PPC_SPE_DOUBLE),
9364GEN_SPE(efdcmpgt, efdcmplt, 0x16, 0x0B, 0x00600000, 0x00600000, PPC_SPE_DOUBLE),
9365GEN_SPE(efdcmpeq, efdcfs, 0x17, 0x0B, 0x00600000, 0x00180000, PPC_SPE_DOUBLE),
9366GEN_SPE(efdcfui, efdcfsi, 0x18, 0x0B, 0x00180000, 0x00180000, PPC_SPE_DOUBLE),
9367GEN_SPE(efdcfuf, efdcfsf, 0x19, 0x0B, 0x00180000, 0x00180000, PPC_SPE_DOUBLE),
9368GEN_SPE(efdctui, efdctsi, 0x1A, 0x0B, 0x00180000, 0x00180000, PPC_SPE_DOUBLE),
9369GEN_SPE(efdctuf, efdctsf, 0x1B, 0x0B, 0x00180000, 0x00180000, PPC_SPE_DOUBLE),
9370GEN_SPE(efdctuiz, speundef, 0x1C, 0x0B, 0x00180000, 0xFFFFFFFF, PPC_SPE_DOUBLE),
9371GEN_SPE(efdctsiz, speundef, 0x1D, 0x0B, 0x00180000, 0xFFFFFFFF, PPC_SPE_DOUBLE),
9372GEN_SPE(efdtstgt, efdtstlt, 0x1E, 0x0B, 0x00600000, 0x00600000, PPC_SPE_DOUBLE),
9373GEN_SPE(efdtsteq, speundef, 0x1F, 0x0B, 0x00600000, 0xFFFFFFFF, PPC_SPE_DOUBLE),
5c55ff99
BS
9374
9375#undef GEN_SPEOP_LDST
9376#define GEN_SPEOP_LDST(name, opc2, sh) \
9377GEN_HANDLER(name, 0x04, opc2, 0x0C, 0x00000000, PPC_SPE)
9378GEN_SPEOP_LDST(evldd, 0x00, 3),
9379GEN_SPEOP_LDST(evldw, 0x01, 3),
9380GEN_SPEOP_LDST(evldh, 0x02, 3),
9381GEN_SPEOP_LDST(evlhhesplat, 0x04, 1),
9382GEN_SPEOP_LDST(evlhhousplat, 0x06, 1),
9383GEN_SPEOP_LDST(evlhhossplat, 0x07, 1),
9384GEN_SPEOP_LDST(evlwhe, 0x08, 2),
9385GEN_SPEOP_LDST(evlwhou, 0x0A, 2),
9386GEN_SPEOP_LDST(evlwhos, 0x0B, 2),
9387GEN_SPEOP_LDST(evlwwsplat, 0x0C, 2),
9388GEN_SPEOP_LDST(evlwhsplat, 0x0E, 2),
9389
9390GEN_SPEOP_LDST(evstdd, 0x10, 3),
9391GEN_SPEOP_LDST(evstdw, 0x11, 3),
9392GEN_SPEOP_LDST(evstdh, 0x12, 3),
9393GEN_SPEOP_LDST(evstwhe, 0x18, 2),
9394GEN_SPEOP_LDST(evstwho, 0x1A, 2),
9395GEN_SPEOP_LDST(evstwwe, 0x1C, 2),
9396GEN_SPEOP_LDST(evstwwo, 0x1E, 2),
9397};
9398
0411a972 9399#include "helper_regs.h"
a1389542 9400#include "translate_init.c"
79aceca5 9401
9a64fbe4 9402/*****************************************************************************/
3fc6c082 9403/* Misc PowerPC helpers */
1328c2bf 9404void cpu_dump_state (CPUPPCState *env, FILE *f, fprintf_function cpu_fprintf,
36081602 9405 int flags)
79aceca5 9406{
3fc6c082
FB
9407#define RGPL 4
9408#define RFPL 4
3fc6c082 9409
79aceca5
FB
9410 int i;
9411
29979a8d
AG
9412 cpu_synchronize_state(env);
9413
90e189ec 9414 cpu_fprintf(f, "NIP " TARGET_FMT_lx " LR " TARGET_FMT_lx " CTR "
9a78eead
SW
9415 TARGET_FMT_lx " XER " TARGET_FMT_lx "\n",
9416 env->nip, env->lr, env->ctr, env->xer);
90e189ec
BS
9417 cpu_fprintf(f, "MSR " TARGET_FMT_lx " HID0 " TARGET_FMT_lx " HF "
9418 TARGET_FMT_lx " idx %d\n", env->msr, env->spr[SPR_HID0],
9419 env->hflags, env->mmu_idx);
d9bce9d9 9420#if !defined(NO_TIMER_DUMP)
9a78eead 9421 cpu_fprintf(f, "TB %08" PRIu32 " %08" PRIu64
76a66253 9422#if !defined(CONFIG_USER_ONLY)
9a78eead 9423 " DECR %08" PRIu32
76a66253
JM
9424#endif
9425 "\n",
077fc206 9426 cpu_ppc_load_tbu(env), cpu_ppc_load_tbl(env)
76a66253
JM
9427#if !defined(CONFIG_USER_ONLY)
9428 , cpu_ppc_load_decr(env)
9429#endif
9430 );
077fc206 9431#endif
76a66253 9432 for (i = 0; i < 32; i++) {
3fc6c082
FB
9433 if ((i & (RGPL - 1)) == 0)
9434 cpu_fprintf(f, "GPR%02d", i);
b11ebf64 9435 cpu_fprintf(f, " %016" PRIx64, ppc_dump_gpr(env, i));
3fc6c082 9436 if ((i & (RGPL - 1)) == (RGPL - 1))
7fe48483 9437 cpu_fprintf(f, "\n");
76a66253 9438 }
3fc6c082 9439 cpu_fprintf(f, "CR ");
76a66253 9440 for (i = 0; i < 8; i++)
7fe48483
FB
9441 cpu_fprintf(f, "%01x", env->crf[i]);
9442 cpu_fprintf(f, " [");
76a66253
JM
9443 for (i = 0; i < 8; i++) {
9444 char a = '-';
9445 if (env->crf[i] & 0x08)
9446 a = 'L';
9447 else if (env->crf[i] & 0x04)
9448 a = 'G';
9449 else if (env->crf[i] & 0x02)
9450 a = 'E';
7fe48483 9451 cpu_fprintf(f, " %c%c", a, env->crf[i] & 0x01 ? 'O' : ' ');
76a66253 9452 }
90e189ec
BS
9453 cpu_fprintf(f, " ] RES " TARGET_FMT_lx "\n",
9454 env->reserve_addr);
3fc6c082
FB
9455 for (i = 0; i < 32; i++) {
9456 if ((i & (RFPL - 1)) == 0)
9457 cpu_fprintf(f, "FPR%02d", i);
26a76461 9458 cpu_fprintf(f, " %016" PRIx64, *((uint64_t *)&env->fpr[i]));
3fc6c082 9459 if ((i & (RFPL - 1)) == (RFPL - 1))
7fe48483 9460 cpu_fprintf(f, "\n");
79aceca5 9461 }
7889270a 9462 cpu_fprintf(f, "FPSCR %08x\n", env->fpscr);
f2e63a42 9463#if !defined(CONFIG_USER_ONLY)
90dc8812
SW
9464 cpu_fprintf(f, " SRR0 " TARGET_FMT_lx " SRR1 " TARGET_FMT_lx
9465 " PVR " TARGET_FMT_lx " VRSAVE " TARGET_FMT_lx "\n",
9466 env->spr[SPR_SRR0], env->spr[SPR_SRR1],
9467 env->spr[SPR_PVR], env->spr[SPR_VRSAVE]);
9468
9469 cpu_fprintf(f, "SPRG0 " TARGET_FMT_lx " SPRG1 " TARGET_FMT_lx
9470 " SPRG2 " TARGET_FMT_lx " SPRG3 " TARGET_FMT_lx "\n",
9471 env->spr[SPR_SPRG0], env->spr[SPR_SPRG1],
9472 env->spr[SPR_SPRG2], env->spr[SPR_SPRG3]);
9473
9474 cpu_fprintf(f, "SPRG4 " TARGET_FMT_lx " SPRG5 " TARGET_FMT_lx
9475 " SPRG6 " TARGET_FMT_lx " SPRG7 " TARGET_FMT_lx "\n",
9476 env->spr[SPR_SPRG4], env->spr[SPR_SPRG5],
9477 env->spr[SPR_SPRG6], env->spr[SPR_SPRG7]);
9478
9479 if (env->excp_model == POWERPC_EXCP_BOOKE) {
9480 cpu_fprintf(f, "CSRR0 " TARGET_FMT_lx " CSRR1 " TARGET_FMT_lx
9481 " MCSRR0 " TARGET_FMT_lx " MCSRR1 " TARGET_FMT_lx "\n",
9482 env->spr[SPR_BOOKE_CSRR0], env->spr[SPR_BOOKE_CSRR1],
9483 env->spr[SPR_BOOKE_MCSRR0], env->spr[SPR_BOOKE_MCSRR1]);
9484
9485 cpu_fprintf(f, " TCR " TARGET_FMT_lx " TSR " TARGET_FMT_lx
9486 " ESR " TARGET_FMT_lx " DEAR " TARGET_FMT_lx "\n",
9487 env->spr[SPR_BOOKE_TCR], env->spr[SPR_BOOKE_TSR],
9488 env->spr[SPR_BOOKE_ESR], env->spr[SPR_BOOKE_DEAR]);
9489
9490 cpu_fprintf(f, " PIR " TARGET_FMT_lx " DECAR " TARGET_FMT_lx
9491 " IVPR " TARGET_FMT_lx " EPCR " TARGET_FMT_lx "\n",
9492 env->spr[SPR_BOOKE_PIR], env->spr[SPR_BOOKE_DECAR],
9493 env->spr[SPR_BOOKE_IVPR], env->spr[SPR_BOOKE_EPCR]);
9494
9495 cpu_fprintf(f, " MCSR " TARGET_FMT_lx " SPRG8 " TARGET_FMT_lx
9496 " EPR " TARGET_FMT_lx "\n",
9497 env->spr[SPR_BOOKE_MCSR], env->spr[SPR_BOOKE_SPRG8],
9498 env->spr[SPR_BOOKE_EPR]);
9499
9500 /* FSL-specific */
9501 cpu_fprintf(f, " MCAR " TARGET_FMT_lx " PID1 " TARGET_FMT_lx
9502 " PID2 " TARGET_FMT_lx " SVR " TARGET_FMT_lx "\n",
9503 env->spr[SPR_Exxx_MCAR], env->spr[SPR_BOOKE_PID1],
9504 env->spr[SPR_BOOKE_PID2], env->spr[SPR_E500_SVR]);
9505
9506 /*
9507 * IVORs are left out as they are large and do not change often --
9508 * they can be read with "p $ivor0", "p $ivor1", etc.
9509 */
9510 }
9511
697ab892
DG
9512#if defined(TARGET_PPC64)
9513 if (env->flags & POWERPC_FLAG_CFAR) {
9514 cpu_fprintf(f, " CFAR " TARGET_FMT_lx"\n", env->cfar);
9515 }
9516#endif
9517
90dc8812
SW
9518 switch (env->mmu_model) {
9519 case POWERPC_MMU_32B:
9520 case POWERPC_MMU_601:
9521 case POWERPC_MMU_SOFT_6xx:
9522 case POWERPC_MMU_SOFT_74xx:
9523#if defined(TARGET_PPC64)
9524 case POWERPC_MMU_620:
9525 case POWERPC_MMU_64B:
9526#endif
9527 cpu_fprintf(f, " SDR1 " TARGET_FMT_lx "\n", env->spr[SPR_SDR1]);
9528 break;
01662f3e 9529 case POWERPC_MMU_BOOKE206:
90dc8812
SW
9530 cpu_fprintf(f, " MAS0 " TARGET_FMT_lx " MAS1 " TARGET_FMT_lx
9531 " MAS2 " TARGET_FMT_lx " MAS3 " TARGET_FMT_lx "\n",
9532 env->spr[SPR_BOOKE_MAS0], env->spr[SPR_BOOKE_MAS1],
9533 env->spr[SPR_BOOKE_MAS2], env->spr[SPR_BOOKE_MAS3]);
9534
9535 cpu_fprintf(f, " MAS4 " TARGET_FMT_lx " MAS6 " TARGET_FMT_lx
9536 " MAS7 " TARGET_FMT_lx " PID " TARGET_FMT_lx "\n",
9537 env->spr[SPR_BOOKE_MAS4], env->spr[SPR_BOOKE_MAS6],
9538 env->spr[SPR_BOOKE_MAS7], env->spr[SPR_BOOKE_PID]);
9539
9540 cpu_fprintf(f, "MMUCFG " TARGET_FMT_lx " TLB0CFG " TARGET_FMT_lx
9541 " TLB1CFG " TARGET_FMT_lx "\n",
9542 env->spr[SPR_MMUCFG], env->spr[SPR_BOOKE_TLB0CFG],
9543 env->spr[SPR_BOOKE_TLB1CFG]);
9544 break;
9545 default:
9546 break;
9547 }
f2e63a42 9548#endif
79aceca5 9549
3fc6c082
FB
9550#undef RGPL
9551#undef RFPL
79aceca5
FB
9552}
9553
1328c2bf 9554void cpu_dump_statistics (CPUPPCState *env, FILE*f, fprintf_function cpu_fprintf,
76a66253
JM
9555 int flags)
9556{
9557#if defined(DO_PPC_STATISTICS)
c227f099 9558 opc_handler_t **t1, **t2, **t3, *handler;
76a66253
JM
9559 int op1, op2, op3;
9560
9561 t1 = env->opcodes;
9562 for (op1 = 0; op1 < 64; op1++) {
9563 handler = t1[op1];
9564 if (is_indirect_opcode(handler)) {
9565 t2 = ind_table(handler);
9566 for (op2 = 0; op2 < 32; op2++) {
9567 handler = t2[op2];
9568 if (is_indirect_opcode(handler)) {
9569 t3 = ind_table(handler);
9570 for (op3 = 0; op3 < 32; op3++) {
9571 handler = t3[op3];
9572 if (handler->count == 0)
9573 continue;
9574 cpu_fprintf(f, "%02x %02x %02x (%02x %04d) %16s: "
0bfcd599 9575 "%016" PRIx64 " %" PRId64 "\n",
76a66253
JM
9576 op1, op2, op3, op1, (op3 << 5) | op2,
9577 handler->oname,
9578 handler->count, handler->count);
9579 }
9580 } else {
9581 if (handler->count == 0)
9582 continue;
9583 cpu_fprintf(f, "%02x %02x (%02x %04d) %16s: "
0bfcd599 9584 "%016" PRIx64 " %" PRId64 "\n",
76a66253
JM
9585 op1, op2, op1, op2, handler->oname,
9586 handler->count, handler->count);
9587 }
9588 }
9589 } else {
9590 if (handler->count == 0)
9591 continue;
0bfcd599
BS
9592 cpu_fprintf(f, "%02x (%02x ) %16s: %016" PRIx64
9593 " %" PRId64 "\n",
76a66253
JM
9594 op1, op1, handler->oname,
9595 handler->count, handler->count);
9596 }
9597 }
9598#endif
9599}
9600
9a64fbe4 9601/*****************************************************************************/
1328c2bf 9602static inline void gen_intermediate_code_internal(CPUPPCState *env,
636aa200
BS
9603 TranslationBlock *tb,
9604 int search_pc)
79aceca5 9605{
9fddaa0c 9606 DisasContext ctx, *ctxp = &ctx;
c227f099 9607 opc_handler_t **table, *handler;
0fa85d43 9608 target_ulong pc_start;
79aceca5 9609 uint16_t *gen_opc_end;
a1d1bb31 9610 CPUBreakpoint *bp;
79aceca5 9611 int j, lj = -1;
2e70f6ef
PB
9612 int num_insns;
9613 int max_insns;
79aceca5
FB
9614
9615 pc_start = tb->pc;
79aceca5 9616 gen_opc_end = gen_opc_buf + OPC_MAX_SIZE;
046d6672 9617 ctx.nip = pc_start;
79aceca5 9618 ctx.tb = tb;
e1833e1f 9619 ctx.exception = POWERPC_EXCP_NONE;
3fc6c082 9620 ctx.spr_cb = env->spr_cb;
76db3ba4
AJ
9621 ctx.mem_idx = env->mmu_idx;
9622 ctx.access_type = -1;
9623 ctx.le_mode = env->hflags & (1 << MSR_LE) ? 1 : 0;
d9bce9d9
JM
9624#if defined(TARGET_PPC64)
9625 ctx.sf_mode = msr_sf;
697ab892 9626 ctx.has_cfar = !!(env->flags & POWERPC_FLAG_CFAR);
9a64fbe4 9627#endif
3cc62370 9628 ctx.fpu_enabled = msr_fp;
a9d9eb8f 9629 if ((env->flags & POWERPC_FLAG_SPE) && msr_spe)
d26bfc9a
JM
9630 ctx.spe_enabled = msr_spe;
9631 else
9632 ctx.spe_enabled = 0;
a9d9eb8f
JM
9633 if ((env->flags & POWERPC_FLAG_VRE) && msr_vr)
9634 ctx.altivec_enabled = msr_vr;
9635 else
9636 ctx.altivec_enabled = 0;
d26bfc9a 9637 if ((env->flags & POWERPC_FLAG_SE) && msr_se)
8cbcb4fa 9638 ctx.singlestep_enabled = CPU_SINGLE_STEP;
d26bfc9a 9639 else
8cbcb4fa 9640 ctx.singlestep_enabled = 0;
d26bfc9a 9641 if ((env->flags & POWERPC_FLAG_BE) && msr_be)
8cbcb4fa
AJ
9642 ctx.singlestep_enabled |= CPU_BRANCH_STEP;
9643 if (unlikely(env->singlestep_enabled))
9644 ctx.singlestep_enabled |= GDBSTUB_SINGLE_STEP;
3fc6c082 9645#if defined (DO_SINGLE_STEP) && 0
9a64fbe4
FB
9646 /* Single step trace mode */
9647 msr_se = 1;
9648#endif
2e70f6ef
PB
9649 num_insns = 0;
9650 max_insns = tb->cflags & CF_COUNT_MASK;
9651 if (max_insns == 0)
9652 max_insns = CF_COUNT_MASK;
9653
9654 gen_icount_start();
9a64fbe4 9655 /* Set env in case of segfault during code fetch */
e1833e1f 9656 while (ctx.exception == POWERPC_EXCP_NONE && gen_opc_ptr < gen_opc_end) {
72cf2d4f
BS
9657 if (unlikely(!QTAILQ_EMPTY(&env->breakpoints))) {
9658 QTAILQ_FOREACH(bp, &env->breakpoints, entry) {
a1d1bb31 9659 if (bp->pc == ctx.nip) {
e06fcd75 9660 gen_debug_exception(ctxp);
ea4e754f
FB
9661 break;
9662 }
9663 }
9664 }
76a66253 9665 if (unlikely(search_pc)) {
79aceca5
FB
9666 j = gen_opc_ptr - gen_opc_buf;
9667 if (lj < j) {
9668 lj++;
9669 while (lj < j)
9670 gen_opc_instr_start[lj++] = 0;
79aceca5 9671 }
af4b6c54
AJ
9672 gen_opc_pc[lj] = ctx.nip;
9673 gen_opc_instr_start[lj] = 1;
9674 gen_opc_icount[lj] = num_insns;
79aceca5 9675 }
d12d51d5 9676 LOG_DISAS("----------------\n");
90e189ec 9677 LOG_DISAS("nip=" TARGET_FMT_lx " super=%d ir=%d\n",
d12d51d5 9678 ctx.nip, ctx.mem_idx, (int)msr_ir);
2e70f6ef
PB
9679 if (num_insns + 1 == max_insns && (tb->cflags & CF_LAST_IO))
9680 gen_io_start();
76db3ba4 9681 if (unlikely(ctx.le_mode)) {
056401ea
JM
9682 ctx.opcode = bswap32(ldl_code(ctx.nip));
9683 } else {
9684 ctx.opcode = ldl_code(ctx.nip);
111bfab3 9685 }
d12d51d5 9686 LOG_DISAS("translate opcode %08x (%02x %02x %02x) (%s)\n",
9a64fbe4 9687 ctx.opcode, opc1(ctx.opcode), opc2(ctx.opcode),
056401ea 9688 opc3(ctx.opcode), little_endian ? "little" : "big");
731c54f8
AJ
9689 if (unlikely(qemu_loglevel_mask(CPU_LOG_TB_OP)))
9690 tcg_gen_debug_insn_start(ctx.nip);
046d6672 9691 ctx.nip += 4;
3fc6c082 9692 table = env->opcodes;
2e70f6ef 9693 num_insns++;
79aceca5
FB
9694 handler = table[opc1(ctx.opcode)];
9695 if (is_indirect_opcode(handler)) {
9696 table = ind_table(handler);
9697 handler = table[opc2(ctx.opcode)];
9698 if (is_indirect_opcode(handler)) {
9699 table = ind_table(handler);
9700 handler = table[opc3(ctx.opcode)];
9701 }
9702 }
9703 /* Is opcode *REALLY* valid ? */
76a66253 9704 if (unlikely(handler->handler == &gen_invalid)) {
93fcfe39
AL
9705 if (qemu_log_enabled()) {
9706 qemu_log("invalid/unsupported opcode: "
90e189ec
BS
9707 "%02x - %02x - %02x (%08x) " TARGET_FMT_lx " %d\n",
9708 opc1(ctx.opcode), opc2(ctx.opcode),
9709 opc3(ctx.opcode), ctx.opcode, ctx.nip - 4, (int)msr_ir);
4b3686fa 9710 }
76a66253 9711 } else {
70560da7
FC
9712 uint32_t inval;
9713
9714 if (unlikely(handler->type & (PPC_SPE | PPC_SPE_SINGLE | PPC_SPE_DOUBLE) && Rc(ctx.opcode))) {
9715 inval = handler->inval2;
9716 } else {
9717 inval = handler->inval1;
9718 }
9719
9720 if (unlikely((ctx.opcode & inval) != 0)) {
93fcfe39
AL
9721 if (qemu_log_enabled()) {
9722 qemu_log("invalid bits: %08x for opcode: "
90e189ec 9723 "%02x - %02x - %02x (%08x) " TARGET_FMT_lx "\n",
70560da7 9724 ctx.opcode & inval, opc1(ctx.opcode),
90e189ec
BS
9725 opc2(ctx.opcode), opc3(ctx.opcode),
9726 ctx.opcode, ctx.nip - 4);
76a66253 9727 }
e06fcd75 9728 gen_inval_exception(ctxp, POWERPC_EXCP_INVAL_INVAL);
4b3686fa 9729 break;
79aceca5 9730 }
79aceca5 9731 }
4b3686fa 9732 (*(handler->handler))(&ctx);
76a66253
JM
9733#if defined(DO_PPC_STATISTICS)
9734 handler->count++;
9735#endif
9a64fbe4 9736 /* Check trace mode exceptions */
8cbcb4fa
AJ
9737 if (unlikely(ctx.singlestep_enabled & CPU_SINGLE_STEP &&
9738 (ctx.nip <= 0x100 || ctx.nip > 0xF00) &&
9739 ctx.exception != POWERPC_SYSCALL &&
9740 ctx.exception != POWERPC_EXCP_TRAP &&
9741 ctx.exception != POWERPC_EXCP_BRANCH)) {
e06fcd75 9742 gen_exception(ctxp, POWERPC_EXCP_TRACE);
d26bfc9a 9743 } else if (unlikely(((ctx.nip & (TARGET_PAGE_SIZE - 1)) == 0) ||
2e70f6ef 9744 (env->singlestep_enabled) ||
1b530a6d 9745 singlestep ||
2e70f6ef 9746 num_insns >= max_insns)) {
d26bfc9a
JM
9747 /* if we reach a page boundary or are single stepping, stop
9748 * generation
9749 */
8dd4983c 9750 break;
76a66253 9751 }
3fc6c082 9752 }
2e70f6ef
PB
9753 if (tb->cflags & CF_LAST_IO)
9754 gen_io_end();
e1833e1f 9755 if (ctx.exception == POWERPC_EXCP_NONE) {
c1942362 9756 gen_goto_tb(&ctx, 0, ctx.nip);
e1833e1f 9757 } else if (ctx.exception != POWERPC_EXCP_BRANCH) {
8cbcb4fa 9758 if (unlikely(env->singlestep_enabled)) {
e06fcd75 9759 gen_debug_exception(ctxp);
8cbcb4fa 9760 }
76a66253 9761 /* Generate the return instruction */
57fec1fe 9762 tcg_gen_exit_tb(0);
9a64fbe4 9763 }
2e70f6ef 9764 gen_icount_end(tb, num_insns);
79aceca5 9765 *gen_opc_ptr = INDEX_op_end;
76a66253 9766 if (unlikely(search_pc)) {
9a64fbe4
FB
9767 j = gen_opc_ptr - gen_opc_buf;
9768 lj++;
9769 while (lj <= j)
9770 gen_opc_instr_start[lj++] = 0;
9a64fbe4 9771 } else {
046d6672 9772 tb->size = ctx.nip - pc_start;
2e70f6ef 9773 tb->icount = num_insns;
9a64fbe4 9774 }
d9bce9d9 9775#if defined(DEBUG_DISAS)
8fec2b8c 9776 if (qemu_loglevel_mask(CPU_LOG_TB_IN_ASM)) {
76a66253 9777 int flags;
237c0af0 9778 flags = env->bfd_mach;
76db3ba4 9779 flags |= ctx.le_mode << 16;
93fcfe39
AL
9780 qemu_log("IN: %s\n", lookup_symbol(pc_start));
9781 log_target_disas(pc_start, ctx.nip - pc_start, flags);
9782 qemu_log("\n");
9fddaa0c 9783 }
79aceca5 9784#endif
79aceca5
FB
9785}
9786
1328c2bf 9787void gen_intermediate_code (CPUPPCState *env, struct TranslationBlock *tb)
79aceca5 9788{
2cfc5f17 9789 gen_intermediate_code_internal(env, tb, 0);
79aceca5
FB
9790}
9791
1328c2bf 9792void gen_intermediate_code_pc (CPUPPCState *env, struct TranslationBlock *tb)
79aceca5 9793{
2cfc5f17 9794 gen_intermediate_code_internal(env, tb, 1);
79aceca5 9795}
d2856f1a 9796
1328c2bf 9797void restore_state_to_opc(CPUPPCState *env, TranslationBlock *tb, int pc_pos)
d2856f1a 9798{
d2856f1a 9799 env->nip = gen_opc_pc[pc_pos];
d2856f1a 9800}