]> git.proxmox.com Git - qemu.git/blame - target-ppc/translate.c
cpu: Turn cpu_dump_{state,statistics}() into CPUState hooks
[qemu.git] / target-ppc / translate.c
CommitLineData
79aceca5 1/*
3fc6c082 2 * PowerPC emulation for qemu: main translation routines.
5fafdf24 3 *
76a66253 4 * Copyright (c) 2003-2007 Jocelyn Mayer
90dc8812 5 * Copyright (C) 2011 Freescale Semiconductor, Inc.
79aceca5
FB
6 *
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2 of the License, or (at your option) any later version.
11 *
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
8167ee88 18 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
79aceca5 19 */
c6a1c22b 20
79aceca5 21#include "cpu.h"
76cad711 22#include "disas/disas.h"
57fec1fe 23#include "tcg-op.h"
1de7afc9 24#include "qemu/host-utils.h"
79aceca5 25
a7812ae4
PB
26#include "helper.h"
27#define GEN_HELPER 1
28#include "helper.h"
29
8cbcb4fa
AJ
30#define CPU_SINGLE_STEP 0x1
31#define CPU_BRANCH_STEP 0x2
32#define GDBSTUB_SINGLE_STEP 0x4
33
a750fc0b 34/* Include definitions for instructions classes and implementations flags */
9fddaa0c 35//#define PPC_DEBUG_DISAS
76a66253 36//#define DO_PPC_STATISTICS
79aceca5 37
d12d51d5 38#ifdef PPC_DEBUG_DISAS
93fcfe39 39# define LOG_DISAS(...) qemu_log_mask(CPU_LOG_TB_IN_ASM, ## __VA_ARGS__)
d12d51d5
AL
40#else
41# define LOG_DISAS(...) do { } while (0)
42#endif
a750fc0b
JM
43/*****************************************************************************/
44/* Code translation helpers */
c53be334 45
f78fb44e 46/* global register indexes */
a7812ae4 47static TCGv_ptr cpu_env;
1d542695 48static char cpu_reg_names[10*3 + 22*4 /* GPR */
f78fb44e 49#if !defined(TARGET_PPC64)
1d542695 50 + 10*4 + 22*5 /* SPE GPRh */
f78fb44e 51#endif
a5e26afa 52 + 10*4 + 22*5 /* FPR */
47e4661c
AJ
53 + 2*(10*6 + 22*7) /* AVRh, AVRl */
54 + 8*5 /* CRF */];
f78fb44e
AJ
55static TCGv cpu_gpr[32];
56#if !defined(TARGET_PPC64)
57static TCGv cpu_gprh[32];
58#endif
a7812ae4
PB
59static TCGv_i64 cpu_fpr[32];
60static TCGv_i64 cpu_avrh[32], cpu_avrl[32];
61static TCGv_i32 cpu_crf[8];
bd568f18 62static TCGv cpu_nip;
6527f6ea 63static TCGv cpu_msr;
cfdcd37a
AJ
64static TCGv cpu_ctr;
65static TCGv cpu_lr;
697ab892
DG
66#if defined(TARGET_PPC64)
67static TCGv cpu_cfar;
68#endif
da91a00f 69static TCGv cpu_xer, cpu_so, cpu_ov, cpu_ca;
cf360a32 70static TCGv cpu_reserve;
30304420 71static TCGv cpu_fpscr;
a7859e89 72static TCGv_i32 cpu_access_type;
f78fb44e 73
022c62cb 74#include "exec/gen-icount.h"
2e70f6ef
PB
75
76void ppc_translate_init(void)
77{
f78fb44e
AJ
78 int i;
79 char* p;
2dc766da 80 size_t cpu_reg_names_size;
b2437bf2 81 static int done_init = 0;
f78fb44e 82
2e70f6ef
PB
83 if (done_init)
84 return;
f78fb44e 85
a7812ae4 86 cpu_env = tcg_global_reg_new_ptr(TCG_AREG0, "env");
a7812ae4 87
f78fb44e 88 p = cpu_reg_names;
2dc766da 89 cpu_reg_names_size = sizeof(cpu_reg_names);
47e4661c
AJ
90
91 for (i = 0; i < 8; i++) {
2dc766da 92 snprintf(p, cpu_reg_names_size, "crf%d", i);
a7812ae4 93 cpu_crf[i] = tcg_global_mem_new_i32(TCG_AREG0,
1328c2bf 94 offsetof(CPUPPCState, crf[i]), p);
47e4661c 95 p += 5;
2dc766da 96 cpu_reg_names_size -= 5;
47e4661c
AJ
97 }
98
f78fb44e 99 for (i = 0; i < 32; i++) {
2dc766da 100 snprintf(p, cpu_reg_names_size, "r%d", i);
a7812ae4 101 cpu_gpr[i] = tcg_global_mem_new(TCG_AREG0,
1328c2bf 102 offsetof(CPUPPCState, gpr[i]), p);
f78fb44e 103 p += (i < 10) ? 3 : 4;
2dc766da 104 cpu_reg_names_size -= (i < 10) ? 3 : 4;
f78fb44e 105#if !defined(TARGET_PPC64)
2dc766da 106 snprintf(p, cpu_reg_names_size, "r%dH", i);
a7812ae4 107 cpu_gprh[i] = tcg_global_mem_new_i32(TCG_AREG0,
1328c2bf 108 offsetof(CPUPPCState, gprh[i]), p);
f78fb44e 109 p += (i < 10) ? 4 : 5;
2dc766da 110 cpu_reg_names_size -= (i < 10) ? 4 : 5;
f78fb44e 111#endif
1d542695 112
2dc766da 113 snprintf(p, cpu_reg_names_size, "fp%d", i);
a7812ae4 114 cpu_fpr[i] = tcg_global_mem_new_i64(TCG_AREG0,
1328c2bf 115 offsetof(CPUPPCState, fpr[i]), p);
ec1ac72d 116 p += (i < 10) ? 4 : 5;
2dc766da 117 cpu_reg_names_size -= (i < 10) ? 4 : 5;
a5e26afa 118
2dc766da 119 snprintf(p, cpu_reg_names_size, "avr%dH", i);
e2542fe2 120#ifdef HOST_WORDS_BIGENDIAN
fe1e5c53 121 cpu_avrh[i] = tcg_global_mem_new_i64(TCG_AREG0,
1328c2bf 122 offsetof(CPUPPCState, avr[i].u64[0]), p);
fe1e5c53 123#else
a7812ae4 124 cpu_avrh[i] = tcg_global_mem_new_i64(TCG_AREG0,
1328c2bf 125 offsetof(CPUPPCState, avr[i].u64[1]), p);
fe1e5c53 126#endif
1d542695 127 p += (i < 10) ? 6 : 7;
2dc766da 128 cpu_reg_names_size -= (i < 10) ? 6 : 7;
ec1ac72d 129
2dc766da 130 snprintf(p, cpu_reg_names_size, "avr%dL", i);
e2542fe2 131#ifdef HOST_WORDS_BIGENDIAN
fe1e5c53 132 cpu_avrl[i] = tcg_global_mem_new_i64(TCG_AREG0,
1328c2bf 133 offsetof(CPUPPCState, avr[i].u64[1]), p);
fe1e5c53 134#else
a7812ae4 135 cpu_avrl[i] = tcg_global_mem_new_i64(TCG_AREG0,
1328c2bf 136 offsetof(CPUPPCState, avr[i].u64[0]), p);
fe1e5c53 137#endif
1d542695 138 p += (i < 10) ? 6 : 7;
2dc766da 139 cpu_reg_names_size -= (i < 10) ? 6 : 7;
f78fb44e 140 }
f10dc08e 141
a7812ae4 142 cpu_nip = tcg_global_mem_new(TCG_AREG0,
1328c2bf 143 offsetof(CPUPPCState, nip), "nip");
bd568f18 144
6527f6ea 145 cpu_msr = tcg_global_mem_new(TCG_AREG0,
1328c2bf 146 offsetof(CPUPPCState, msr), "msr");
6527f6ea 147
a7812ae4 148 cpu_ctr = tcg_global_mem_new(TCG_AREG0,
1328c2bf 149 offsetof(CPUPPCState, ctr), "ctr");
cfdcd37a 150
a7812ae4 151 cpu_lr = tcg_global_mem_new(TCG_AREG0,
1328c2bf 152 offsetof(CPUPPCState, lr), "lr");
cfdcd37a 153
697ab892
DG
154#if defined(TARGET_PPC64)
155 cpu_cfar = tcg_global_mem_new(TCG_AREG0,
1328c2bf 156 offsetof(CPUPPCState, cfar), "cfar");
697ab892
DG
157#endif
158
a7812ae4 159 cpu_xer = tcg_global_mem_new(TCG_AREG0,
1328c2bf 160 offsetof(CPUPPCState, xer), "xer");
da91a00f
RH
161 cpu_so = tcg_global_mem_new(TCG_AREG0,
162 offsetof(CPUPPCState, so), "SO");
163 cpu_ov = tcg_global_mem_new(TCG_AREG0,
164 offsetof(CPUPPCState, ov), "OV");
165 cpu_ca = tcg_global_mem_new(TCG_AREG0,
166 offsetof(CPUPPCState, ca), "CA");
3d7b417e 167
cf360a32 168 cpu_reserve = tcg_global_mem_new(TCG_AREG0,
1328c2bf 169 offsetof(CPUPPCState, reserve_addr),
18b21a2f 170 "reserve_addr");
cf360a32 171
30304420
DG
172 cpu_fpscr = tcg_global_mem_new(TCG_AREG0,
173 offsetof(CPUPPCState, fpscr), "fpscr");
e1571908 174
a7859e89 175 cpu_access_type = tcg_global_mem_new_i32(TCG_AREG0,
1328c2bf 176 offsetof(CPUPPCState, access_type), "access_type");
a7859e89 177
f10dc08e 178 /* register helpers */
a7812ae4 179#define GEN_HELPER 2
f10dc08e
AJ
180#include "helper.h"
181
2e70f6ef
PB
182 done_init = 1;
183}
184
79aceca5
FB
185/* internal defines */
186typedef struct DisasContext {
187 struct TranslationBlock *tb;
0fa85d43 188 target_ulong nip;
79aceca5 189 uint32_t opcode;
9a64fbe4 190 uint32_t exception;
3cc62370
FB
191 /* Routine used to access memory */
192 int mem_idx;
76db3ba4 193 int access_type;
3cc62370 194 /* Translation flags */
76db3ba4 195 int le_mode;
d9bce9d9
JM
196#if defined(TARGET_PPC64)
197 int sf_mode;
697ab892 198 int has_cfar;
9a64fbe4 199#endif
3cc62370 200 int fpu_enabled;
a9d9eb8f 201 int altivec_enabled;
0487d6a8 202 int spe_enabled;
c227f099 203 ppc_spr_t *spr_cb; /* Needed to check rights for mfspr/mtspr */
ea4e754f 204 int singlestep_enabled;
7d08d856
AJ
205 uint64_t insns_flags;
206 uint64_t insns_flags2;
79aceca5
FB
207} DisasContext;
208
79482e5a
RH
209/* True when active word size < size of target_long. */
210#ifdef TARGET_PPC64
211# define NARROW_MODE(C) (!(C)->sf_mode)
212#else
213# define NARROW_MODE(C) 0
214#endif
215
c227f099 216struct opc_handler_t {
70560da7
FC
217 /* invalid bits for instruction 1 (Rc(opcode) == 0) */
218 uint32_t inval1;
219 /* invalid bits for instruction 2 (Rc(opcode) == 1) */
220 uint32_t inval2;
9a64fbe4 221 /* instruction type */
0487d6a8 222 uint64_t type;
a5858d7a
AG
223 /* extended instruction type */
224 uint64_t type2;
79aceca5
FB
225 /* handler */
226 void (*handler)(DisasContext *ctx);
a750fc0b 227#if defined(DO_PPC_STATISTICS) || defined(PPC_DUMP_CPU)
b55266b5 228 const char *oname;
a750fc0b
JM
229#endif
230#if defined(DO_PPC_STATISTICS)
76a66253
JM
231 uint64_t count;
232#endif
3fc6c082 233};
79aceca5 234
636aa200 235static inline void gen_reset_fpstatus(void)
7c58044c 236{
8e703949 237 gen_helper_reset_fpstatus(cpu_env);
7c58044c
JM
238}
239
636aa200 240static inline void gen_compute_fprf(TCGv_i64 arg, int set_fprf, int set_rc)
7c58044c 241{
0f2f39c2 242 TCGv_i32 t0 = tcg_temp_new_i32();
af12906f 243
7c58044c
JM
244 if (set_fprf != 0) {
245 /* This case might be optimized later */
0f2f39c2 246 tcg_gen_movi_i32(t0, 1);
8e703949 247 gen_helper_compute_fprf(t0, cpu_env, arg, t0);
a7812ae4 248 if (unlikely(set_rc)) {
0f2f39c2 249 tcg_gen_mov_i32(cpu_crf[1], t0);
a7812ae4 250 }
8e703949 251 gen_helper_float_check_status(cpu_env);
7c58044c
JM
252 } else if (unlikely(set_rc)) {
253 /* We always need to compute fpcc */
0f2f39c2 254 tcg_gen_movi_i32(t0, 0);
8e703949 255 gen_helper_compute_fprf(t0, cpu_env, arg, t0);
0f2f39c2 256 tcg_gen_mov_i32(cpu_crf[1], t0);
7c58044c 257 }
af12906f 258
0f2f39c2 259 tcg_temp_free_i32(t0);
7c58044c
JM
260}
261
636aa200 262static inline void gen_set_access_type(DisasContext *ctx, int access_type)
a7859e89 263{
76db3ba4
AJ
264 if (ctx->access_type != access_type) {
265 tcg_gen_movi_i32(cpu_access_type, access_type);
266 ctx->access_type = access_type;
267 }
a7859e89
AJ
268}
269
636aa200 270static inline void gen_update_nip(DisasContext *ctx, target_ulong nip)
d9bce9d9 271{
e0c8f9ce
RH
272 if (NARROW_MODE(ctx)) {
273 nip = (uint32_t)nip;
274 }
275 tcg_gen_movi_tl(cpu_nip, nip);
d9bce9d9
JM
276}
277
636aa200 278static inline void gen_exception_err(DisasContext *ctx, uint32_t excp, uint32_t error)
e06fcd75
AJ
279{
280 TCGv_i32 t0, t1;
281 if (ctx->exception == POWERPC_EXCP_NONE) {
282 gen_update_nip(ctx, ctx->nip);
283 }
284 t0 = tcg_const_i32(excp);
285 t1 = tcg_const_i32(error);
e5f17ac6 286 gen_helper_raise_exception_err(cpu_env, t0, t1);
e06fcd75
AJ
287 tcg_temp_free_i32(t0);
288 tcg_temp_free_i32(t1);
289 ctx->exception = (excp);
290}
e1833e1f 291
636aa200 292static inline void gen_exception(DisasContext *ctx, uint32_t excp)
e06fcd75
AJ
293{
294 TCGv_i32 t0;
295 if (ctx->exception == POWERPC_EXCP_NONE) {
296 gen_update_nip(ctx, ctx->nip);
297 }
298 t0 = tcg_const_i32(excp);
e5f17ac6 299 gen_helper_raise_exception(cpu_env, t0);
e06fcd75
AJ
300 tcg_temp_free_i32(t0);
301 ctx->exception = (excp);
302}
e1833e1f 303
636aa200 304static inline void gen_debug_exception(DisasContext *ctx)
e06fcd75
AJ
305{
306 TCGv_i32 t0;
5518f3a6 307
ee2b3994
SB
308 if ((ctx->exception != POWERPC_EXCP_BRANCH) &&
309 (ctx->exception != POWERPC_EXCP_SYNC)) {
5518f3a6 310 gen_update_nip(ctx, ctx->nip);
ee2b3994 311 }
e06fcd75 312 t0 = tcg_const_i32(EXCP_DEBUG);
e5f17ac6 313 gen_helper_raise_exception(cpu_env, t0);
e06fcd75
AJ
314 tcg_temp_free_i32(t0);
315}
9a64fbe4 316
636aa200 317static inline void gen_inval_exception(DisasContext *ctx, uint32_t error)
e06fcd75
AJ
318{
319 gen_exception_err(ctx, POWERPC_EXCP_PROGRAM, POWERPC_EXCP_INVAL | error);
320}
a9d9eb8f 321
f24e5695 322/* Stop translation */
636aa200 323static inline void gen_stop_exception(DisasContext *ctx)
3fc6c082 324{
d9bce9d9 325 gen_update_nip(ctx, ctx->nip);
e1833e1f 326 ctx->exception = POWERPC_EXCP_STOP;
3fc6c082
FB
327}
328
f24e5695 329/* No need to update nip here, as execution flow will change */
636aa200 330static inline void gen_sync_exception(DisasContext *ctx)
2be0071f 331{
e1833e1f 332 ctx->exception = POWERPC_EXCP_SYNC;
2be0071f
FB
333}
334
79aceca5 335#define GEN_HANDLER(name, opc1, opc2, opc3, inval, type) \
a5858d7a
AG
336GEN_OPCODE(name, opc1, opc2, opc3, inval, type, PPC_NONE)
337
338#define GEN_HANDLER_E(name, opc1, opc2, opc3, inval, type, type2) \
339GEN_OPCODE(name, opc1, opc2, opc3, inval, type, type2)
79aceca5 340
c7697e1f 341#define GEN_HANDLER2(name, onam, opc1, opc2, opc3, inval, type) \
a5858d7a
AG
342GEN_OPCODE2(name, onam, opc1, opc2, opc3, inval, type, PPC_NONE)
343
344#define GEN_HANDLER2_E(name, onam, opc1, opc2, opc3, inval, type, type2) \
345GEN_OPCODE2(name, onam, opc1, opc2, opc3, inval, type, type2)
c7697e1f 346
c227f099 347typedef struct opcode_t {
79aceca5 348 unsigned char opc1, opc2, opc3;
1235fc06 349#if HOST_LONG_BITS == 64 /* Explicitly align to 64 bits */
18fba28c
FB
350 unsigned char pad[5];
351#else
352 unsigned char pad[1];
353#endif
c227f099 354 opc_handler_t handler;
b55266b5 355 const char *oname;
c227f099 356} opcode_t;
79aceca5 357
a750fc0b 358/*****************************************************************************/
79aceca5
FB
359/*** Instruction decoding ***/
360#define EXTRACT_HELPER(name, shift, nb) \
636aa200 361static inline uint32_t name(uint32_t opcode) \
79aceca5
FB
362{ \
363 return (opcode >> (shift)) & ((1 << (nb)) - 1); \
364}
365
366#define EXTRACT_SHELPER(name, shift, nb) \
636aa200 367static inline int32_t name(uint32_t opcode) \
79aceca5 368{ \
18fba28c 369 return (int16_t)((opcode >> (shift)) & ((1 << (nb)) - 1)); \
79aceca5
FB
370}
371
372/* Opcode part 1 */
373EXTRACT_HELPER(opc1, 26, 6);
374/* Opcode part 2 */
375EXTRACT_HELPER(opc2, 1, 5);
376/* Opcode part 3 */
377EXTRACT_HELPER(opc3, 6, 5);
378/* Update Cr0 flags */
379EXTRACT_HELPER(Rc, 0, 1);
380/* Destination */
381EXTRACT_HELPER(rD, 21, 5);
382/* Source */
383EXTRACT_HELPER(rS, 21, 5);
384/* First operand */
385EXTRACT_HELPER(rA, 16, 5);
386/* Second operand */
387EXTRACT_HELPER(rB, 11, 5);
388/* Third operand */
389EXTRACT_HELPER(rC, 6, 5);
390/*** Get CRn ***/
391EXTRACT_HELPER(crfD, 23, 3);
392EXTRACT_HELPER(crfS, 18, 3);
393EXTRACT_HELPER(crbD, 21, 5);
394EXTRACT_HELPER(crbA, 16, 5);
395EXTRACT_HELPER(crbB, 11, 5);
396/* SPR / TBL */
3fc6c082 397EXTRACT_HELPER(_SPR, 11, 10);
636aa200 398static inline uint32_t SPR(uint32_t opcode)
3fc6c082
FB
399{
400 uint32_t sprn = _SPR(opcode);
401
402 return ((sprn >> 5) & 0x1F) | ((sprn & 0x1F) << 5);
403}
79aceca5
FB
404/*** Get constants ***/
405EXTRACT_HELPER(IMM, 12, 8);
406/* 16 bits signed immediate value */
407EXTRACT_SHELPER(SIMM, 0, 16);
408/* 16 bits unsigned immediate value */
409EXTRACT_HELPER(UIMM, 0, 16);
21d21583
AJ
410/* 5 bits signed immediate value */
411EXTRACT_HELPER(SIMM5, 16, 5);
27a4edb3
AJ
412/* 5 bits signed immediate value */
413EXTRACT_HELPER(UIMM5, 16, 5);
79aceca5
FB
414/* Bit count */
415EXTRACT_HELPER(NB, 11, 5);
416/* Shift count */
417EXTRACT_HELPER(SH, 11, 5);
cd633b10
AJ
418/* Vector shift count */
419EXTRACT_HELPER(VSH, 6, 4);
79aceca5
FB
420/* Mask start */
421EXTRACT_HELPER(MB, 6, 5);
422/* Mask end */
423EXTRACT_HELPER(ME, 1, 5);
fb0eaffc
FB
424/* Trap operand */
425EXTRACT_HELPER(TO, 21, 5);
79aceca5
FB
426
427EXTRACT_HELPER(CRM, 12, 8);
79aceca5 428EXTRACT_HELPER(SR, 16, 4);
7d08d856
AJ
429
430/* mtfsf/mtfsfi */
431EXTRACT_HELPER(FPBF, 19, 3);
e4bb997e 432EXTRACT_HELPER(FPIMM, 12, 4);
7d08d856
AJ
433EXTRACT_HELPER(FPL, 21, 1);
434EXTRACT_HELPER(FPFLM, 17, 8);
435EXTRACT_HELPER(FPW, 16, 1);
fb0eaffc 436
79aceca5
FB
437/*** Jump target decoding ***/
438/* Displacement */
439EXTRACT_SHELPER(d, 0, 16);
440/* Immediate address */
636aa200 441static inline target_ulong LI(uint32_t opcode)
79aceca5
FB
442{
443 return (opcode >> 0) & 0x03FFFFFC;
444}
445
636aa200 446static inline uint32_t BD(uint32_t opcode)
79aceca5
FB
447{
448 return (opcode >> 0) & 0xFFFC;
449}
450
451EXTRACT_HELPER(BO, 21, 5);
452EXTRACT_HELPER(BI, 16, 5);
453/* Absolute/relative address */
454EXTRACT_HELPER(AA, 1, 1);
455/* Link */
456EXTRACT_HELPER(LK, 0, 1);
457
458/* Create a mask between <start> and <end> bits */
636aa200 459static inline target_ulong MASK(uint32_t start, uint32_t end)
79aceca5 460{
76a66253 461 target_ulong ret;
79aceca5 462
76a66253
JM
463#if defined(TARGET_PPC64)
464 if (likely(start == 0)) {
6f2d8978 465 ret = UINT64_MAX << (63 - end);
76a66253 466 } else if (likely(end == 63)) {
6f2d8978 467 ret = UINT64_MAX >> start;
76a66253
JM
468 }
469#else
470 if (likely(start == 0)) {
6f2d8978 471 ret = UINT32_MAX << (31 - end);
76a66253 472 } else if (likely(end == 31)) {
6f2d8978 473 ret = UINT32_MAX >> start;
76a66253
JM
474 }
475#endif
476 else {
477 ret = (((target_ulong)(-1ULL)) >> (start)) ^
478 (((target_ulong)(-1ULL) >> (end)) >> 1);
479 if (unlikely(start > end))
480 return ~ret;
481 }
79aceca5
FB
482
483 return ret;
484}
485
a750fc0b 486/*****************************************************************************/
a750fc0b 487/* PowerPC instructions table */
933dc6eb 488
76a66253 489#if defined(DO_PPC_STATISTICS)
a5858d7a 490#define GEN_OPCODE(name, op1, op2, op3, invl, _typ, _typ2) \
5c55ff99 491{ \
79aceca5
FB
492 .opc1 = op1, \
493 .opc2 = op2, \
494 .opc3 = op3, \
18fba28c 495 .pad = { 0, }, \
79aceca5 496 .handler = { \
70560da7
FC
497 .inval1 = invl, \
498 .type = _typ, \
499 .type2 = _typ2, \
500 .handler = &gen_##name, \
501 .oname = stringify(name), \
502 }, \
503 .oname = stringify(name), \
504}
505#define GEN_OPCODE_DUAL(name, op1, op2, op3, invl1, invl2, _typ, _typ2) \
506{ \
507 .opc1 = op1, \
508 .opc2 = op2, \
509 .opc3 = op3, \
510 .pad = { 0, }, \
511 .handler = { \
512 .inval1 = invl1, \
513 .inval2 = invl2, \
9a64fbe4 514 .type = _typ, \
a5858d7a 515 .type2 = _typ2, \
79aceca5 516 .handler = &gen_##name, \
76a66253 517 .oname = stringify(name), \
79aceca5 518 }, \
3fc6c082 519 .oname = stringify(name), \
79aceca5 520}
a5858d7a 521#define GEN_OPCODE2(name, onam, op1, op2, op3, invl, _typ, _typ2) \
5c55ff99 522{ \
c7697e1f
JM
523 .opc1 = op1, \
524 .opc2 = op2, \
525 .opc3 = op3, \
526 .pad = { 0, }, \
527 .handler = { \
70560da7 528 .inval1 = invl, \
c7697e1f 529 .type = _typ, \
a5858d7a 530 .type2 = _typ2, \
c7697e1f
JM
531 .handler = &gen_##name, \
532 .oname = onam, \
533 }, \
534 .oname = onam, \
535}
76a66253 536#else
a5858d7a 537#define GEN_OPCODE(name, op1, op2, op3, invl, _typ, _typ2) \
5c55ff99 538{ \
c7697e1f
JM
539 .opc1 = op1, \
540 .opc2 = op2, \
541 .opc3 = op3, \
542 .pad = { 0, }, \
543 .handler = { \
70560da7
FC
544 .inval1 = invl, \
545 .type = _typ, \
546 .type2 = _typ2, \
547 .handler = &gen_##name, \
548 }, \
549 .oname = stringify(name), \
550}
551#define GEN_OPCODE_DUAL(name, op1, op2, op3, invl1, invl2, _typ, _typ2) \
552{ \
553 .opc1 = op1, \
554 .opc2 = op2, \
555 .opc3 = op3, \
556 .pad = { 0, }, \
557 .handler = { \
558 .inval1 = invl1, \
559 .inval2 = invl2, \
c7697e1f 560 .type = _typ, \
a5858d7a 561 .type2 = _typ2, \
c7697e1f 562 .handler = &gen_##name, \
5c55ff99
BS
563 }, \
564 .oname = stringify(name), \
565}
a5858d7a 566#define GEN_OPCODE2(name, onam, op1, op2, op3, invl, _typ, _typ2) \
5c55ff99
BS
567{ \
568 .opc1 = op1, \
569 .opc2 = op2, \
570 .opc3 = op3, \
571 .pad = { 0, }, \
572 .handler = { \
70560da7 573 .inval1 = invl, \
5c55ff99 574 .type = _typ, \
a5858d7a 575 .type2 = _typ2, \
5c55ff99
BS
576 .handler = &gen_##name, \
577 }, \
578 .oname = onam, \
579}
580#endif
2e610050 581
5c55ff99 582/* SPR load/store helpers */
636aa200 583static inline void gen_load_spr(TCGv t, int reg)
5c55ff99 584{
1328c2bf 585 tcg_gen_ld_tl(t, cpu_env, offsetof(CPUPPCState, spr[reg]));
5c55ff99 586}
2e610050 587
636aa200 588static inline void gen_store_spr(int reg, TCGv t)
5c55ff99 589{
1328c2bf 590 tcg_gen_st_tl(t, cpu_env, offsetof(CPUPPCState, spr[reg]));
5c55ff99 591}
2e610050 592
54623277 593/* Invalid instruction */
99e300ef 594static void gen_invalid(DisasContext *ctx)
9a64fbe4 595{
e06fcd75 596 gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL);
9a64fbe4
FB
597}
598
c227f099 599static opc_handler_t invalid_handler = {
70560da7
FC
600 .inval1 = 0xFFFFFFFF,
601 .inval2 = 0xFFFFFFFF,
9a64fbe4 602 .type = PPC_NONE,
a5858d7a 603 .type2 = PPC_NONE,
79aceca5
FB
604 .handler = gen_invalid,
605};
606
e1571908
AJ
607/*** Integer comparison ***/
608
636aa200 609static inline void gen_op_cmp(TCGv arg0, TCGv arg1, int s, int crf)
e1571908 610{
2fdcb629
RH
611 TCGv t0 = tcg_temp_new();
612 TCGv_i32 t1 = tcg_temp_new_i32();
e1571908 613
da91a00f 614 tcg_gen_trunc_tl_i32(cpu_crf[crf], cpu_so);
e1571908 615
2fdcb629
RH
616 tcg_gen_setcond_tl((s ? TCG_COND_LT: TCG_COND_LTU), t0, arg0, arg1);
617 tcg_gen_trunc_tl_i32(t1, t0);
618 tcg_gen_shli_i32(t1, t1, CRF_LT);
619 tcg_gen_or_i32(cpu_crf[crf], cpu_crf[crf], t1);
620
621 tcg_gen_setcond_tl((s ? TCG_COND_GT: TCG_COND_GTU), t0, arg0, arg1);
622 tcg_gen_trunc_tl_i32(t1, t0);
623 tcg_gen_shli_i32(t1, t1, CRF_GT);
624 tcg_gen_or_i32(cpu_crf[crf], cpu_crf[crf], t1);
625
626 tcg_gen_setcond_tl(TCG_COND_EQ, t0, arg0, arg1);
627 tcg_gen_trunc_tl_i32(t1, t0);
628 tcg_gen_shli_i32(t1, t1, CRF_EQ);
629 tcg_gen_or_i32(cpu_crf[crf], cpu_crf[crf], t1);
630
631 tcg_temp_free(t0);
632 tcg_temp_free_i32(t1);
e1571908
AJ
633}
634
636aa200 635static inline void gen_op_cmpi(TCGv arg0, target_ulong arg1, int s, int crf)
e1571908 636{
2fdcb629 637 TCGv t0 = tcg_const_tl(arg1);
ea363694
AJ
638 gen_op_cmp(arg0, t0, s, crf);
639 tcg_temp_free(t0);
e1571908
AJ
640}
641
636aa200 642static inline void gen_op_cmp32(TCGv arg0, TCGv arg1, int s, int crf)
e1571908 643{
ea363694 644 TCGv t0, t1;
2fdcb629
RH
645 t0 = tcg_temp_new();
646 t1 = tcg_temp_new();
e1571908 647 if (s) {
ea363694
AJ
648 tcg_gen_ext32s_tl(t0, arg0);
649 tcg_gen_ext32s_tl(t1, arg1);
e1571908 650 } else {
ea363694
AJ
651 tcg_gen_ext32u_tl(t0, arg0);
652 tcg_gen_ext32u_tl(t1, arg1);
e1571908 653 }
ea363694
AJ
654 gen_op_cmp(t0, t1, s, crf);
655 tcg_temp_free(t1);
656 tcg_temp_free(t0);
e1571908
AJ
657}
658
636aa200 659static inline void gen_op_cmpi32(TCGv arg0, target_ulong arg1, int s, int crf)
e1571908 660{
2fdcb629 661 TCGv t0 = tcg_const_tl(arg1);
ea363694
AJ
662 gen_op_cmp32(arg0, t0, s, crf);
663 tcg_temp_free(t0);
e1571908 664}
e1571908 665
636aa200 666static inline void gen_set_Rc0(DisasContext *ctx, TCGv reg)
e1571908 667{
02765534 668 if (NARROW_MODE(ctx)) {
e1571908 669 gen_op_cmpi32(reg, 0, 1, 0);
02765534 670 } else {
e1571908 671 gen_op_cmpi(reg, 0, 1, 0);
02765534 672 }
e1571908
AJ
673}
674
675/* cmp */
99e300ef 676static void gen_cmp(DisasContext *ctx)
e1571908 677{
36f48d9c 678 if ((ctx->opcode & 0x00200000) && (ctx->insns_flags & PPC_64B)) {
e1571908
AJ
679 gen_op_cmp(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rB(ctx->opcode)],
680 1, crfD(ctx->opcode));
36f48d9c
AG
681 } else {
682 gen_op_cmp32(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rB(ctx->opcode)],
683 1, crfD(ctx->opcode));
02765534 684 }
e1571908
AJ
685}
686
687/* cmpi */
99e300ef 688static void gen_cmpi(DisasContext *ctx)
e1571908 689{
36f48d9c 690 if ((ctx->opcode & 0x00200000) && (ctx->insns_flags & PPC_64B)) {
e1571908
AJ
691 gen_op_cmpi(cpu_gpr[rA(ctx->opcode)], SIMM(ctx->opcode),
692 1, crfD(ctx->opcode));
36f48d9c
AG
693 } else {
694 gen_op_cmpi32(cpu_gpr[rA(ctx->opcode)], SIMM(ctx->opcode),
695 1, crfD(ctx->opcode));
02765534 696 }
e1571908
AJ
697}
698
699/* cmpl */
99e300ef 700static void gen_cmpl(DisasContext *ctx)
e1571908 701{
36f48d9c 702 if ((ctx->opcode & 0x00200000) && (ctx->insns_flags & PPC_64B)) {
e1571908
AJ
703 gen_op_cmp(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rB(ctx->opcode)],
704 0, crfD(ctx->opcode));
36f48d9c
AG
705 } else {
706 gen_op_cmp32(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rB(ctx->opcode)],
707 0, crfD(ctx->opcode));
02765534 708 }
e1571908
AJ
709}
710
711/* cmpli */
99e300ef 712static void gen_cmpli(DisasContext *ctx)
e1571908 713{
36f48d9c 714 if ((ctx->opcode & 0x00200000) && (ctx->insns_flags & PPC_64B)) {
e1571908
AJ
715 gen_op_cmpi(cpu_gpr[rA(ctx->opcode)], UIMM(ctx->opcode),
716 0, crfD(ctx->opcode));
36f48d9c
AG
717 } else {
718 gen_op_cmpi32(cpu_gpr[rA(ctx->opcode)], UIMM(ctx->opcode),
719 0, crfD(ctx->opcode));
02765534 720 }
e1571908
AJ
721}
722
723/* isel (PowerPC 2.03 specification) */
99e300ef 724static void gen_isel(DisasContext *ctx)
e1571908
AJ
725{
726 int l1, l2;
727 uint32_t bi = rC(ctx->opcode);
728 uint32_t mask;
a7812ae4 729 TCGv_i32 t0;
e1571908
AJ
730
731 l1 = gen_new_label();
732 l2 = gen_new_label();
733
734 mask = 1 << (3 - (bi & 0x03));
a7812ae4 735 t0 = tcg_temp_new_i32();
fea0c503
AJ
736 tcg_gen_andi_i32(t0, cpu_crf[bi >> 2], mask);
737 tcg_gen_brcondi_i32(TCG_COND_EQ, t0, 0, l1);
e1571908
AJ
738 if (rA(ctx->opcode) == 0)
739 tcg_gen_movi_tl(cpu_gpr[rD(ctx->opcode)], 0);
740 else
741 tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)]);
742 tcg_gen_br(l2);
743 gen_set_label(l1);
744 tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rB(ctx->opcode)]);
745 gen_set_label(l2);
a7812ae4 746 tcg_temp_free_i32(t0);
e1571908
AJ
747}
748
fcfda20f
AJ
749/* cmpb: PowerPC 2.05 specification */
750static void gen_cmpb(DisasContext *ctx)
751{
752 gen_helper_cmpb(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)],
753 cpu_gpr[rB(ctx->opcode)]);
754}
755
79aceca5 756/*** Integer arithmetic ***/
79aceca5 757
636aa200
BS
758static inline void gen_op_arith_compute_ov(DisasContext *ctx, TCGv arg0,
759 TCGv arg1, TCGv arg2, int sub)
74637406 760{
ffe30937 761 TCGv t0 = tcg_temp_new();
79aceca5 762
8e7a6db9 763 tcg_gen_xor_tl(cpu_ov, arg0, arg2);
74637406 764 tcg_gen_xor_tl(t0, arg1, arg2);
ffe30937
RH
765 if (sub) {
766 tcg_gen_and_tl(cpu_ov, cpu_ov, t0);
767 } else {
768 tcg_gen_andc_tl(cpu_ov, cpu_ov, t0);
769 }
770 tcg_temp_free(t0);
02765534 771 if (NARROW_MODE(ctx)) {
ffe30937
RH
772 tcg_gen_ext32s_tl(cpu_ov, cpu_ov);
773 }
ffe30937
RH
774 tcg_gen_shri_tl(cpu_ov, cpu_ov, TARGET_LONG_BITS - 1);
775 tcg_gen_or_tl(cpu_so, cpu_so, cpu_ov);
79aceca5
FB
776}
777
74637406 778/* Common add function */
636aa200 779static inline void gen_op_arith_add(DisasContext *ctx, TCGv ret, TCGv arg1,
b5a73f8d
RH
780 TCGv arg2, bool add_ca, bool compute_ca,
781 bool compute_ov, bool compute_rc0)
74637406 782{
b5a73f8d 783 TCGv t0 = ret;
d9bce9d9 784
752d634e 785 if (compute_ca || compute_ov) {
146de60d 786 t0 = tcg_temp_new();
74637406 787 }
79aceca5 788
da91a00f 789 if (compute_ca) {
79482e5a 790 if (NARROW_MODE(ctx)) {
752d634e
RH
791 /* Caution: a non-obvious corner case of the spec is that we
792 must produce the *entire* 64-bit addition, but produce the
793 carry into bit 32. */
79482e5a 794 TCGv t1 = tcg_temp_new();
752d634e
RH
795 tcg_gen_xor_tl(t1, arg1, arg2); /* add without carry */
796 tcg_gen_add_tl(t0, arg1, arg2);
79482e5a
RH
797 if (add_ca) {
798 tcg_gen_add_tl(t0, t0, cpu_ca);
799 }
752d634e
RH
800 tcg_gen_xor_tl(cpu_ca, t0, t1); /* bits changed w/ carry */
801 tcg_temp_free(t1);
802 tcg_gen_shri_tl(cpu_ca, cpu_ca, 32); /* extract bit 32 */
803 tcg_gen_andi_tl(cpu_ca, cpu_ca, 1);
b5a73f8d 804 } else {
79482e5a
RH
805 TCGv zero = tcg_const_tl(0);
806 if (add_ca) {
807 tcg_gen_add2_tl(t0, cpu_ca, arg1, zero, cpu_ca, zero);
808 tcg_gen_add2_tl(t0, cpu_ca, t0, cpu_ca, arg2, zero);
809 } else {
810 tcg_gen_add2_tl(t0, cpu_ca, arg1, zero, arg2, zero);
811 }
812 tcg_temp_free(zero);
b5a73f8d 813 }
b5a73f8d
RH
814 } else {
815 tcg_gen_add_tl(t0, arg1, arg2);
816 if (add_ca) {
817 tcg_gen_add_tl(t0, t0, cpu_ca);
818 }
da91a00f 819 }
79aceca5 820
74637406
AJ
821 if (compute_ov) {
822 gen_op_arith_compute_ov(ctx, t0, arg1, arg2, 0);
823 }
b5a73f8d 824 if (unlikely(compute_rc0)) {
74637406 825 gen_set_Rc0(ctx, t0);
b5a73f8d 826 }
74637406 827
a7812ae4 828 if (!TCGV_EQUAL(t0, ret)) {
74637406
AJ
829 tcg_gen_mov_tl(ret, t0);
830 tcg_temp_free(t0);
831 }
39dd32ee 832}
74637406
AJ
833/* Add functions with two operands */
834#define GEN_INT_ARITH_ADD(name, opc3, add_ca, compute_ca, compute_ov) \
b5a73f8d 835static void glue(gen_, name)(DisasContext *ctx) \
74637406
AJ
836{ \
837 gen_op_arith_add(ctx, cpu_gpr[rD(ctx->opcode)], \
838 cpu_gpr[rA(ctx->opcode)], cpu_gpr[rB(ctx->opcode)], \
b5a73f8d 839 add_ca, compute_ca, compute_ov, Rc(ctx->opcode)); \
74637406
AJ
840}
841/* Add functions with one operand and one immediate */
842#define GEN_INT_ARITH_ADD_CONST(name, opc3, const_val, \
843 add_ca, compute_ca, compute_ov) \
b5a73f8d 844static void glue(gen_, name)(DisasContext *ctx) \
74637406 845{ \
b5a73f8d 846 TCGv t0 = tcg_const_tl(const_val); \
74637406
AJ
847 gen_op_arith_add(ctx, cpu_gpr[rD(ctx->opcode)], \
848 cpu_gpr[rA(ctx->opcode)], t0, \
b5a73f8d 849 add_ca, compute_ca, compute_ov, Rc(ctx->opcode)); \
74637406
AJ
850 tcg_temp_free(t0); \
851}
852
853/* add add. addo addo. */
854GEN_INT_ARITH_ADD(add, 0x08, 0, 0, 0)
855GEN_INT_ARITH_ADD(addo, 0x18, 0, 0, 1)
856/* addc addc. addco addco. */
857GEN_INT_ARITH_ADD(addc, 0x00, 0, 1, 0)
858GEN_INT_ARITH_ADD(addco, 0x10, 0, 1, 1)
859/* adde adde. addeo addeo. */
860GEN_INT_ARITH_ADD(adde, 0x04, 1, 1, 0)
861GEN_INT_ARITH_ADD(addeo, 0x14, 1, 1, 1)
862/* addme addme. addmeo addmeo. */
863GEN_INT_ARITH_ADD_CONST(addme, 0x07, -1LL, 1, 1, 0)
864GEN_INT_ARITH_ADD_CONST(addmeo, 0x17, -1LL, 1, 1, 1)
865/* addze addze. addzeo addzeo.*/
866GEN_INT_ARITH_ADD_CONST(addze, 0x06, 0, 1, 1, 0)
867GEN_INT_ARITH_ADD_CONST(addzeo, 0x16, 0, 1, 1, 1)
868/* addi */
99e300ef 869static void gen_addi(DisasContext *ctx)
d9bce9d9 870{
74637406
AJ
871 target_long simm = SIMM(ctx->opcode);
872
873 if (rA(ctx->opcode) == 0) {
874 /* li case */
875 tcg_gen_movi_tl(cpu_gpr[rD(ctx->opcode)], simm);
876 } else {
b5a73f8d
RH
877 tcg_gen_addi_tl(cpu_gpr[rD(ctx->opcode)],
878 cpu_gpr[rA(ctx->opcode)], simm);
74637406 879 }
d9bce9d9 880}
74637406 881/* addic addic.*/
b5a73f8d 882static inline void gen_op_addic(DisasContext *ctx, bool compute_rc0)
d9bce9d9 883{
b5a73f8d
RH
884 TCGv c = tcg_const_tl(SIMM(ctx->opcode));
885 gen_op_arith_add(ctx, cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)],
886 c, 0, 1, 0, compute_rc0);
887 tcg_temp_free(c);
d9bce9d9 888}
99e300ef
BS
889
890static void gen_addic(DisasContext *ctx)
d9bce9d9 891{
b5a73f8d 892 gen_op_addic(ctx, 0);
d9bce9d9 893}
e8eaa2c0
BS
894
895static void gen_addic_(DisasContext *ctx)
d9bce9d9 896{
b5a73f8d 897 gen_op_addic(ctx, 1);
d9bce9d9 898}
99e300ef 899
54623277 900/* addis */
99e300ef 901static void gen_addis(DisasContext *ctx)
d9bce9d9 902{
74637406
AJ
903 target_long simm = SIMM(ctx->opcode);
904
905 if (rA(ctx->opcode) == 0) {
906 /* lis case */
907 tcg_gen_movi_tl(cpu_gpr[rD(ctx->opcode)], simm << 16);
908 } else {
b5a73f8d
RH
909 tcg_gen_addi_tl(cpu_gpr[rD(ctx->opcode)],
910 cpu_gpr[rA(ctx->opcode)], simm << 16);
74637406 911 }
d9bce9d9 912}
74637406 913
636aa200
BS
914static inline void gen_op_arith_divw(DisasContext *ctx, TCGv ret, TCGv arg1,
915 TCGv arg2, int sign, int compute_ov)
d9bce9d9 916{
2ef1b120
AJ
917 int l1 = gen_new_label();
918 int l2 = gen_new_label();
a7812ae4
PB
919 TCGv_i32 t0 = tcg_temp_local_new_i32();
920 TCGv_i32 t1 = tcg_temp_local_new_i32();
74637406 921
2ef1b120
AJ
922 tcg_gen_trunc_tl_i32(t0, arg1);
923 tcg_gen_trunc_tl_i32(t1, arg2);
924 tcg_gen_brcondi_i32(TCG_COND_EQ, t1, 0, l1);
74637406 925 if (sign) {
2ef1b120
AJ
926 int l3 = gen_new_label();
927 tcg_gen_brcondi_i32(TCG_COND_NE, t1, -1, l3);
928 tcg_gen_brcondi_i32(TCG_COND_EQ, t0, INT32_MIN, l1);
74637406 929 gen_set_label(l3);
2ef1b120 930 tcg_gen_div_i32(t0, t0, t1);
74637406 931 } else {
2ef1b120 932 tcg_gen_divu_i32(t0, t0, t1);
74637406
AJ
933 }
934 if (compute_ov) {
da91a00f 935 tcg_gen_movi_tl(cpu_ov, 0);
74637406
AJ
936 }
937 tcg_gen_br(l2);
938 gen_set_label(l1);
939 if (sign) {
2ef1b120 940 tcg_gen_sari_i32(t0, t0, 31);
74637406
AJ
941 } else {
942 tcg_gen_movi_i32(t0, 0);
943 }
944 if (compute_ov) {
da91a00f
RH
945 tcg_gen_movi_tl(cpu_ov, 1);
946 tcg_gen_movi_tl(cpu_so, 1);
74637406
AJ
947 }
948 gen_set_label(l2);
2ef1b120 949 tcg_gen_extu_i32_tl(ret, t0);
a7812ae4
PB
950 tcg_temp_free_i32(t0);
951 tcg_temp_free_i32(t1);
74637406
AJ
952 if (unlikely(Rc(ctx->opcode) != 0))
953 gen_set_Rc0(ctx, ret);
d9bce9d9 954}
74637406
AJ
955/* Div functions */
956#define GEN_INT_ARITH_DIVW(name, opc3, sign, compute_ov) \
99e300ef 957static void glue(gen_, name)(DisasContext *ctx) \
74637406
AJ
958{ \
959 gen_op_arith_divw(ctx, cpu_gpr[rD(ctx->opcode)], \
960 cpu_gpr[rA(ctx->opcode)], cpu_gpr[rB(ctx->opcode)], \
961 sign, compute_ov); \
962}
963/* divwu divwu. divwuo divwuo. */
964GEN_INT_ARITH_DIVW(divwu, 0x0E, 0, 0);
965GEN_INT_ARITH_DIVW(divwuo, 0x1E, 0, 1);
966/* divw divw. divwo divwo. */
967GEN_INT_ARITH_DIVW(divw, 0x0F, 1, 0);
968GEN_INT_ARITH_DIVW(divwo, 0x1F, 1, 1);
d9bce9d9 969#if defined(TARGET_PPC64)
636aa200
BS
970static inline void gen_op_arith_divd(DisasContext *ctx, TCGv ret, TCGv arg1,
971 TCGv arg2, int sign, int compute_ov)
d9bce9d9 972{
2ef1b120
AJ
973 int l1 = gen_new_label();
974 int l2 = gen_new_label();
74637406
AJ
975
976 tcg_gen_brcondi_i64(TCG_COND_EQ, arg2, 0, l1);
977 if (sign) {
2ef1b120 978 int l3 = gen_new_label();
74637406
AJ
979 tcg_gen_brcondi_i64(TCG_COND_NE, arg2, -1, l3);
980 tcg_gen_brcondi_i64(TCG_COND_EQ, arg1, INT64_MIN, l1);
981 gen_set_label(l3);
74637406
AJ
982 tcg_gen_div_i64(ret, arg1, arg2);
983 } else {
984 tcg_gen_divu_i64(ret, arg1, arg2);
985 }
986 if (compute_ov) {
da91a00f 987 tcg_gen_movi_tl(cpu_ov, 0);
74637406
AJ
988 }
989 tcg_gen_br(l2);
990 gen_set_label(l1);
991 if (sign) {
992 tcg_gen_sari_i64(ret, arg1, 63);
993 } else {
994 tcg_gen_movi_i64(ret, 0);
995 }
996 if (compute_ov) {
da91a00f
RH
997 tcg_gen_movi_tl(cpu_ov, 1);
998 tcg_gen_movi_tl(cpu_so, 1);
74637406
AJ
999 }
1000 gen_set_label(l2);
1001 if (unlikely(Rc(ctx->opcode) != 0))
1002 gen_set_Rc0(ctx, ret);
d9bce9d9 1003}
74637406 1004#define GEN_INT_ARITH_DIVD(name, opc3, sign, compute_ov) \
99e300ef 1005static void glue(gen_, name)(DisasContext *ctx) \
74637406 1006{ \
2ef1b120
AJ
1007 gen_op_arith_divd(ctx, cpu_gpr[rD(ctx->opcode)], \
1008 cpu_gpr[rA(ctx->opcode)], cpu_gpr[rB(ctx->opcode)], \
1009 sign, compute_ov); \
74637406
AJ
1010}
1011/* divwu divwu. divwuo divwuo. */
1012GEN_INT_ARITH_DIVD(divdu, 0x0E, 0, 0);
1013GEN_INT_ARITH_DIVD(divduo, 0x1E, 0, 1);
1014/* divw divw. divwo divwo. */
1015GEN_INT_ARITH_DIVD(divd, 0x0F, 1, 0);
1016GEN_INT_ARITH_DIVD(divdo, 0x1F, 1, 1);
d9bce9d9 1017#endif
74637406
AJ
1018
1019/* mulhw mulhw. */
99e300ef 1020static void gen_mulhw(DisasContext *ctx)
d9bce9d9 1021{
23ad1d5d
RH
1022 TCGv_i32 t0 = tcg_temp_new_i32();
1023 TCGv_i32 t1 = tcg_temp_new_i32();
74637406 1024
23ad1d5d
RH
1025 tcg_gen_trunc_tl_i32(t0, cpu_gpr[rA(ctx->opcode)]);
1026 tcg_gen_trunc_tl_i32(t1, cpu_gpr[rB(ctx->opcode)]);
1027 tcg_gen_muls2_i32(t0, t1, t0, t1);
1028 tcg_gen_extu_i32_tl(cpu_gpr[rD(ctx->opcode)], t1);
1029 tcg_temp_free_i32(t0);
1030 tcg_temp_free_i32(t1);
74637406
AJ
1031 if (unlikely(Rc(ctx->opcode) != 0))
1032 gen_set_Rc0(ctx, cpu_gpr[rD(ctx->opcode)]);
d9bce9d9 1033}
99e300ef 1034
54623277 1035/* mulhwu mulhwu. */
99e300ef 1036static void gen_mulhwu(DisasContext *ctx)
d9bce9d9 1037{
23ad1d5d
RH
1038 TCGv_i32 t0 = tcg_temp_new_i32();
1039 TCGv_i32 t1 = tcg_temp_new_i32();
74637406 1040
23ad1d5d
RH
1041 tcg_gen_trunc_tl_i32(t0, cpu_gpr[rA(ctx->opcode)]);
1042 tcg_gen_trunc_tl_i32(t1, cpu_gpr[rB(ctx->opcode)]);
1043 tcg_gen_mulu2_i32(t0, t1, t0, t1);
1044 tcg_gen_extu_i32_tl(cpu_gpr[rD(ctx->opcode)], t1);
1045 tcg_temp_free_i32(t0);
1046 tcg_temp_free_i32(t1);
74637406
AJ
1047 if (unlikely(Rc(ctx->opcode) != 0))
1048 gen_set_Rc0(ctx, cpu_gpr[rD(ctx->opcode)]);
d9bce9d9 1049}
99e300ef 1050
54623277 1051/* mullw mullw. */
99e300ef 1052static void gen_mullw(DisasContext *ctx)
d9bce9d9 1053{
74637406
AJ
1054 tcg_gen_mul_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)],
1055 cpu_gpr[rB(ctx->opcode)]);
1e4c090f 1056 tcg_gen_ext32s_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rD(ctx->opcode)]);
74637406
AJ
1057 if (unlikely(Rc(ctx->opcode) != 0))
1058 gen_set_Rc0(ctx, cpu_gpr[rD(ctx->opcode)]);
d9bce9d9 1059}
99e300ef 1060
54623277 1061/* mullwo mullwo. */
99e300ef 1062static void gen_mullwo(DisasContext *ctx)
d9bce9d9 1063{
e4a2c846
RH
1064 TCGv_i32 t0 = tcg_temp_new_i32();
1065 TCGv_i32 t1 = tcg_temp_new_i32();
74637406 1066
e4a2c846
RH
1067 tcg_gen_trunc_tl_i32(t0, cpu_gpr[rA(ctx->opcode)]);
1068 tcg_gen_trunc_tl_i32(t1, cpu_gpr[rB(ctx->opcode)]);
1069 tcg_gen_muls2_i32(t0, t1, t0, t1);
1070 tcg_gen_ext_i32_tl(cpu_gpr[rD(ctx->opcode)], t0);
1071
1072 tcg_gen_sari_i32(t0, t0, 31);
1073 tcg_gen_setcond_i32(TCG_COND_NE, t0, t0, t1);
1074 tcg_gen_extu_i32_tl(cpu_ov, t0);
1075 tcg_gen_or_tl(cpu_so, cpu_so, cpu_ov);
1076
1077 tcg_temp_free_i32(t0);
1078 tcg_temp_free_i32(t1);
74637406
AJ
1079 if (unlikely(Rc(ctx->opcode) != 0))
1080 gen_set_Rc0(ctx, cpu_gpr[rD(ctx->opcode)]);
d9bce9d9 1081}
99e300ef 1082
54623277 1083/* mulli */
99e300ef 1084static void gen_mulli(DisasContext *ctx)
d9bce9d9 1085{
74637406
AJ
1086 tcg_gen_muli_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)],
1087 SIMM(ctx->opcode));
d9bce9d9 1088}
23ad1d5d 1089
d9bce9d9 1090#if defined(TARGET_PPC64)
74637406 1091/* mulhd mulhd. */
23ad1d5d
RH
1092static void gen_mulhd(DisasContext *ctx)
1093{
1094 TCGv lo = tcg_temp_new();
1095 tcg_gen_muls2_tl(lo, cpu_gpr[rD(ctx->opcode)],
1096 cpu_gpr[rA(ctx->opcode)], cpu_gpr[rB(ctx->opcode)]);
1097 tcg_temp_free(lo);
1098 if (unlikely(Rc(ctx->opcode) != 0)) {
1099 gen_set_Rc0(ctx, cpu_gpr[rD(ctx->opcode)]);
1100 }
1101}
1102
74637406 1103/* mulhdu mulhdu. */
23ad1d5d
RH
1104static void gen_mulhdu(DisasContext *ctx)
1105{
1106 TCGv lo = tcg_temp_new();
1107 tcg_gen_mulu2_tl(lo, cpu_gpr[rD(ctx->opcode)],
1108 cpu_gpr[rA(ctx->opcode)], cpu_gpr[rB(ctx->opcode)]);
1109 tcg_temp_free(lo);
1110 if (unlikely(Rc(ctx->opcode) != 0)) {
1111 gen_set_Rc0(ctx, cpu_gpr[rD(ctx->opcode)]);
1112 }
1113}
99e300ef 1114
54623277 1115/* mulld mulld. */
99e300ef 1116static void gen_mulld(DisasContext *ctx)
d9bce9d9 1117{
74637406
AJ
1118 tcg_gen_mul_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)],
1119 cpu_gpr[rB(ctx->opcode)]);
1120 if (unlikely(Rc(ctx->opcode) != 0))
1121 gen_set_Rc0(ctx, cpu_gpr[rD(ctx->opcode)]);
d9bce9d9 1122}
d15f74fb 1123
74637406 1124/* mulldo mulldo. */
d15f74fb
BS
1125static void gen_mulldo(DisasContext *ctx)
1126{
1127 gen_helper_mulldo(cpu_gpr[rD(ctx->opcode)], cpu_env,
1128 cpu_gpr[rA(ctx->opcode)], cpu_gpr[rB(ctx->opcode)]);
1129 if (unlikely(Rc(ctx->opcode) != 0)) {
1130 gen_set_Rc0(ctx, cpu_gpr[rD(ctx->opcode)]);
1131 }
1132}
d9bce9d9 1133#endif
74637406 1134
74637406 1135/* Common subf function */
636aa200 1136static inline void gen_op_arith_subf(DisasContext *ctx, TCGv ret, TCGv arg1,
b5a73f8d
RH
1137 TCGv arg2, bool add_ca, bool compute_ca,
1138 bool compute_ov, bool compute_rc0)
79aceca5 1139{
b5a73f8d 1140 TCGv t0 = ret;
79aceca5 1141
752d634e 1142 if (compute_ca || compute_ov) {
b5a73f8d 1143 t0 = tcg_temp_new();
da91a00f 1144 }
74637406 1145
79482e5a
RH
1146 if (compute_ca) {
1147 /* dest = ~arg1 + arg2 [+ ca]. */
1148 if (NARROW_MODE(ctx)) {
752d634e
RH
1149 /* Caution: a non-obvious corner case of the spec is that we
1150 must produce the *entire* 64-bit addition, but produce the
1151 carry into bit 32. */
79482e5a 1152 TCGv inv1 = tcg_temp_new();
752d634e 1153 TCGv t1 = tcg_temp_new();
79482e5a 1154 tcg_gen_not_tl(inv1, arg1);
79482e5a 1155 if (add_ca) {
752d634e 1156 tcg_gen_add_tl(t0, arg2, cpu_ca);
79482e5a 1157 } else {
752d634e 1158 tcg_gen_addi_tl(t0, arg2, 1);
79482e5a 1159 }
752d634e 1160 tcg_gen_xor_tl(t1, arg2, inv1); /* add without carry */
79482e5a 1161 tcg_gen_add_tl(t0, t0, inv1);
752d634e
RH
1162 tcg_gen_xor_tl(cpu_ca, t0, t1); /* bits changes w/ carry */
1163 tcg_temp_free(t1);
1164 tcg_gen_shri_tl(cpu_ca, cpu_ca, 32); /* extract bit 32 */
1165 tcg_gen_andi_tl(cpu_ca, cpu_ca, 1);
79482e5a 1166 } else if (add_ca) {
08f4a0f7
RH
1167 TCGv zero, inv1 = tcg_temp_new();
1168 tcg_gen_not_tl(inv1, arg1);
b5a73f8d
RH
1169 zero = tcg_const_tl(0);
1170 tcg_gen_add2_tl(t0, cpu_ca, arg2, zero, cpu_ca, zero);
08f4a0f7 1171 tcg_gen_add2_tl(t0, cpu_ca, t0, cpu_ca, inv1, zero);
b5a73f8d 1172 tcg_temp_free(zero);
08f4a0f7 1173 tcg_temp_free(inv1);
b5a73f8d 1174 } else {
79482e5a 1175 tcg_gen_setcond_tl(TCG_COND_GEU, cpu_ca, arg2, arg1);
b5a73f8d 1176 tcg_gen_sub_tl(t0, arg2, arg1);
b5a73f8d 1177 }
79482e5a
RH
1178 } else if (add_ca) {
1179 /* Since we're ignoring carry-out, we can simplify the
1180 standard ~arg1 + arg2 + ca to arg2 - arg1 + ca - 1. */
1181 tcg_gen_sub_tl(t0, arg2, arg1);
1182 tcg_gen_add_tl(t0, t0, cpu_ca);
1183 tcg_gen_subi_tl(t0, t0, 1);
79aceca5 1184 } else {
b5a73f8d 1185 tcg_gen_sub_tl(t0, arg2, arg1);
74637406 1186 }
b5a73f8d 1187
74637406
AJ
1188 if (compute_ov) {
1189 gen_op_arith_compute_ov(ctx, t0, arg1, arg2, 1);
1190 }
b5a73f8d 1191 if (unlikely(compute_rc0)) {
74637406 1192 gen_set_Rc0(ctx, t0);
b5a73f8d 1193 }
74637406 1194
a7812ae4 1195 if (!TCGV_EQUAL(t0, ret)) {
74637406
AJ
1196 tcg_gen_mov_tl(ret, t0);
1197 tcg_temp_free(t0);
79aceca5 1198 }
79aceca5 1199}
74637406
AJ
1200/* Sub functions with Two operands functions */
1201#define GEN_INT_ARITH_SUBF(name, opc3, add_ca, compute_ca, compute_ov) \
b5a73f8d 1202static void glue(gen_, name)(DisasContext *ctx) \
74637406
AJ
1203{ \
1204 gen_op_arith_subf(ctx, cpu_gpr[rD(ctx->opcode)], \
1205 cpu_gpr[rA(ctx->opcode)], cpu_gpr[rB(ctx->opcode)], \
b5a73f8d 1206 add_ca, compute_ca, compute_ov, Rc(ctx->opcode)); \
74637406
AJ
1207}
1208/* Sub functions with one operand and one immediate */
1209#define GEN_INT_ARITH_SUBF_CONST(name, opc3, const_val, \
1210 add_ca, compute_ca, compute_ov) \
b5a73f8d 1211static void glue(gen_, name)(DisasContext *ctx) \
74637406 1212{ \
b5a73f8d 1213 TCGv t0 = tcg_const_tl(const_val); \
74637406
AJ
1214 gen_op_arith_subf(ctx, cpu_gpr[rD(ctx->opcode)], \
1215 cpu_gpr[rA(ctx->opcode)], t0, \
b5a73f8d 1216 add_ca, compute_ca, compute_ov, Rc(ctx->opcode)); \
74637406
AJ
1217 tcg_temp_free(t0); \
1218}
1219/* subf subf. subfo subfo. */
1220GEN_INT_ARITH_SUBF(subf, 0x01, 0, 0, 0)
1221GEN_INT_ARITH_SUBF(subfo, 0x11, 0, 0, 1)
1222/* subfc subfc. subfco subfco. */
1223GEN_INT_ARITH_SUBF(subfc, 0x00, 0, 1, 0)
1224GEN_INT_ARITH_SUBF(subfco, 0x10, 0, 1, 1)
1225/* subfe subfe. subfeo subfo. */
1226GEN_INT_ARITH_SUBF(subfe, 0x04, 1, 1, 0)
1227GEN_INT_ARITH_SUBF(subfeo, 0x14, 1, 1, 1)
1228/* subfme subfme. subfmeo subfmeo. */
1229GEN_INT_ARITH_SUBF_CONST(subfme, 0x07, -1LL, 1, 1, 0)
1230GEN_INT_ARITH_SUBF_CONST(subfmeo, 0x17, -1LL, 1, 1, 1)
1231/* subfze subfze. subfzeo subfzeo.*/
1232GEN_INT_ARITH_SUBF_CONST(subfze, 0x06, 0, 1, 1, 0)
1233GEN_INT_ARITH_SUBF_CONST(subfzeo, 0x16, 0, 1, 1, 1)
99e300ef 1234
54623277 1235/* subfic */
99e300ef 1236static void gen_subfic(DisasContext *ctx)
79aceca5 1237{
b5a73f8d
RH
1238 TCGv c = tcg_const_tl(SIMM(ctx->opcode));
1239 gen_op_arith_subf(ctx, cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)],
1240 c, 0, 1, 0, 0);
1241 tcg_temp_free(c);
79aceca5
FB
1242}
1243
fd3f0081
RH
1244/* neg neg. nego nego. */
1245static inline void gen_op_arith_neg(DisasContext *ctx, bool compute_ov)
1246{
1247 TCGv zero = tcg_const_tl(0);
1248 gen_op_arith_subf(ctx, cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)],
1249 zero, 0, 0, compute_ov, Rc(ctx->opcode));
1250 tcg_temp_free(zero);
1251}
1252
1253static void gen_neg(DisasContext *ctx)
1254{
1255 gen_op_arith_neg(ctx, 0);
1256}
1257
1258static void gen_nego(DisasContext *ctx)
1259{
1260 gen_op_arith_neg(ctx, 1);
1261}
1262
79aceca5 1263/*** Integer logical ***/
26d67362 1264#define GEN_LOGICAL2(name, tcg_op, opc, type) \
99e300ef 1265static void glue(gen_, name)(DisasContext *ctx) \
79aceca5 1266{ \
26d67362
AJ
1267 tcg_op(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)], \
1268 cpu_gpr[rB(ctx->opcode)]); \
76a66253 1269 if (unlikely(Rc(ctx->opcode) != 0)) \
26d67362 1270 gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]); \
79aceca5 1271}
79aceca5 1272
26d67362 1273#define GEN_LOGICAL1(name, tcg_op, opc, type) \
99e300ef 1274static void glue(gen_, name)(DisasContext *ctx) \
79aceca5 1275{ \
26d67362 1276 tcg_op(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)]); \
76a66253 1277 if (unlikely(Rc(ctx->opcode) != 0)) \
26d67362 1278 gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]); \
79aceca5
FB
1279}
1280
1281/* and & and. */
26d67362 1282GEN_LOGICAL2(and, tcg_gen_and_tl, 0x00, PPC_INTEGER);
79aceca5 1283/* andc & andc. */
26d67362 1284GEN_LOGICAL2(andc, tcg_gen_andc_tl, 0x01, PPC_INTEGER);
e8eaa2c0 1285
54623277 1286/* andi. */
e8eaa2c0 1287static void gen_andi_(DisasContext *ctx)
79aceca5 1288{
26d67362
AJ
1289 tcg_gen_andi_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)], UIMM(ctx->opcode));
1290 gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
79aceca5 1291}
e8eaa2c0 1292
54623277 1293/* andis. */
e8eaa2c0 1294static void gen_andis_(DisasContext *ctx)
79aceca5 1295{
26d67362
AJ
1296 tcg_gen_andi_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)], UIMM(ctx->opcode) << 16);
1297 gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
79aceca5 1298}
99e300ef 1299
54623277 1300/* cntlzw */
99e300ef 1301static void gen_cntlzw(DisasContext *ctx)
26d67362 1302{
a7812ae4 1303 gen_helper_cntlzw(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)]);
26d67362 1304 if (unlikely(Rc(ctx->opcode) != 0))
2e31f5d3 1305 gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
26d67362 1306}
79aceca5 1307/* eqv & eqv. */
26d67362 1308GEN_LOGICAL2(eqv, tcg_gen_eqv_tl, 0x08, PPC_INTEGER);
79aceca5 1309/* extsb & extsb. */
26d67362 1310GEN_LOGICAL1(extsb, tcg_gen_ext8s_tl, 0x1D, PPC_INTEGER);
79aceca5 1311/* extsh & extsh. */
26d67362 1312GEN_LOGICAL1(extsh, tcg_gen_ext16s_tl, 0x1C, PPC_INTEGER);
79aceca5 1313/* nand & nand. */
26d67362 1314GEN_LOGICAL2(nand, tcg_gen_nand_tl, 0x0E, PPC_INTEGER);
79aceca5 1315/* nor & nor. */
26d67362 1316GEN_LOGICAL2(nor, tcg_gen_nor_tl, 0x03, PPC_INTEGER);
99e300ef 1317
54623277 1318/* or & or. */
99e300ef 1319static void gen_or(DisasContext *ctx)
9a64fbe4 1320{
76a66253
JM
1321 int rs, ra, rb;
1322
1323 rs = rS(ctx->opcode);
1324 ra = rA(ctx->opcode);
1325 rb = rB(ctx->opcode);
1326 /* Optimisation for mr. ri case */
1327 if (rs != ra || rs != rb) {
26d67362
AJ
1328 if (rs != rb)
1329 tcg_gen_or_tl(cpu_gpr[ra], cpu_gpr[rs], cpu_gpr[rb]);
1330 else
1331 tcg_gen_mov_tl(cpu_gpr[ra], cpu_gpr[rs]);
76a66253 1332 if (unlikely(Rc(ctx->opcode) != 0))
26d67362 1333 gen_set_Rc0(ctx, cpu_gpr[ra]);
76a66253 1334 } else if (unlikely(Rc(ctx->opcode) != 0)) {
26d67362 1335 gen_set_Rc0(ctx, cpu_gpr[rs]);
c80f84e3
JM
1336#if defined(TARGET_PPC64)
1337 } else {
26d67362
AJ
1338 int prio = 0;
1339
c80f84e3
JM
1340 switch (rs) {
1341 case 1:
1342 /* Set process priority to low */
26d67362 1343 prio = 2;
c80f84e3
JM
1344 break;
1345 case 6:
1346 /* Set process priority to medium-low */
26d67362 1347 prio = 3;
c80f84e3
JM
1348 break;
1349 case 2:
1350 /* Set process priority to normal */
26d67362 1351 prio = 4;
c80f84e3 1352 break;
be147d08
JM
1353#if !defined(CONFIG_USER_ONLY)
1354 case 31:
76db3ba4 1355 if (ctx->mem_idx > 0) {
be147d08 1356 /* Set process priority to very low */
26d67362 1357 prio = 1;
be147d08
JM
1358 }
1359 break;
1360 case 5:
76db3ba4 1361 if (ctx->mem_idx > 0) {
be147d08 1362 /* Set process priority to medium-hight */
26d67362 1363 prio = 5;
be147d08
JM
1364 }
1365 break;
1366 case 3:
76db3ba4 1367 if (ctx->mem_idx > 0) {
be147d08 1368 /* Set process priority to high */
26d67362 1369 prio = 6;
be147d08
JM
1370 }
1371 break;
be147d08 1372 case 7:
76db3ba4 1373 if (ctx->mem_idx > 1) {
be147d08 1374 /* Set process priority to very high */
26d67362 1375 prio = 7;
be147d08
JM
1376 }
1377 break;
be147d08 1378#endif
c80f84e3
JM
1379 default:
1380 /* nop */
1381 break;
1382 }
26d67362 1383 if (prio) {
a7812ae4 1384 TCGv t0 = tcg_temp_new();
54cdcae6 1385 gen_load_spr(t0, SPR_PPR);
ea363694
AJ
1386 tcg_gen_andi_tl(t0, t0, ~0x001C000000000000ULL);
1387 tcg_gen_ori_tl(t0, t0, ((uint64_t)prio) << 50);
54cdcae6 1388 gen_store_spr(SPR_PPR, t0);
ea363694 1389 tcg_temp_free(t0);
26d67362 1390 }
c80f84e3 1391#endif
9a64fbe4 1392 }
9a64fbe4 1393}
79aceca5 1394/* orc & orc. */
26d67362 1395GEN_LOGICAL2(orc, tcg_gen_orc_tl, 0x0C, PPC_INTEGER);
99e300ef 1396
54623277 1397/* xor & xor. */
99e300ef 1398static void gen_xor(DisasContext *ctx)
9a64fbe4 1399{
9a64fbe4 1400 /* Optimisation for "set to zero" case */
26d67362 1401 if (rS(ctx->opcode) != rB(ctx->opcode))
312179c4 1402 tcg_gen_xor_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)], cpu_gpr[rB(ctx->opcode)]);
26d67362
AJ
1403 else
1404 tcg_gen_movi_tl(cpu_gpr[rA(ctx->opcode)], 0);
76a66253 1405 if (unlikely(Rc(ctx->opcode) != 0))
26d67362 1406 gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
9a64fbe4 1407}
99e300ef 1408
54623277 1409/* ori */
99e300ef 1410static void gen_ori(DisasContext *ctx)
79aceca5 1411{
76a66253 1412 target_ulong uimm = UIMM(ctx->opcode);
79aceca5 1413
9a64fbe4
FB
1414 if (rS(ctx->opcode) == rA(ctx->opcode) && uimm == 0) {
1415 /* NOP */
76a66253 1416 /* XXX: should handle special NOPs for POWER series */
9a64fbe4 1417 return;
76a66253 1418 }
26d67362 1419 tcg_gen_ori_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)], uimm);
79aceca5 1420}
99e300ef 1421
54623277 1422/* oris */
99e300ef 1423static void gen_oris(DisasContext *ctx)
79aceca5 1424{
76a66253 1425 target_ulong uimm = UIMM(ctx->opcode);
79aceca5 1426
9a64fbe4
FB
1427 if (rS(ctx->opcode) == rA(ctx->opcode) && uimm == 0) {
1428 /* NOP */
1429 return;
76a66253 1430 }
26d67362 1431 tcg_gen_ori_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)], uimm << 16);
79aceca5 1432}
99e300ef 1433
54623277 1434/* xori */
99e300ef 1435static void gen_xori(DisasContext *ctx)
79aceca5 1436{
76a66253 1437 target_ulong uimm = UIMM(ctx->opcode);
9a64fbe4
FB
1438
1439 if (rS(ctx->opcode) == rA(ctx->opcode) && uimm == 0) {
1440 /* NOP */
1441 return;
1442 }
26d67362 1443 tcg_gen_xori_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)], uimm);
79aceca5 1444}
99e300ef 1445
54623277 1446/* xoris */
99e300ef 1447static void gen_xoris(DisasContext *ctx)
79aceca5 1448{
76a66253 1449 target_ulong uimm = UIMM(ctx->opcode);
9a64fbe4
FB
1450
1451 if (rS(ctx->opcode) == rA(ctx->opcode) && uimm == 0) {
1452 /* NOP */
1453 return;
1454 }
26d67362 1455 tcg_gen_xori_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)], uimm << 16);
79aceca5 1456}
99e300ef 1457
54623277 1458/* popcntb : PowerPC 2.03 specification */
99e300ef 1459static void gen_popcntb(DisasContext *ctx)
d9bce9d9 1460{
eaabeef2
DG
1461 gen_helper_popcntb(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)]);
1462}
1463
1464static void gen_popcntw(DisasContext *ctx)
1465{
1466 gen_helper_popcntw(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)]);
1467}
1468
d9bce9d9 1469#if defined(TARGET_PPC64)
eaabeef2
DG
1470/* popcntd: PowerPC 2.06 specification */
1471static void gen_popcntd(DisasContext *ctx)
1472{
1473 gen_helper_popcntd(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)]);
d9bce9d9 1474}
eaabeef2 1475#endif
d9bce9d9 1476
725bcec2
AJ
1477/* prtyw: PowerPC 2.05 specification */
1478static void gen_prtyw(DisasContext *ctx)
1479{
1480 TCGv ra = cpu_gpr[rA(ctx->opcode)];
1481 TCGv rs = cpu_gpr[rS(ctx->opcode)];
1482 TCGv t0 = tcg_temp_new();
1483 tcg_gen_shri_tl(t0, rs, 16);
1484 tcg_gen_xor_tl(ra, rs, t0);
1485 tcg_gen_shri_tl(t0, ra, 8);
1486 tcg_gen_xor_tl(ra, ra, t0);
1487 tcg_gen_andi_tl(ra, ra, (target_ulong)0x100000001ULL);
1488 tcg_temp_free(t0);
1489}
1490
1491#if defined(TARGET_PPC64)
1492/* prtyd: PowerPC 2.05 specification */
1493static void gen_prtyd(DisasContext *ctx)
1494{
1495 TCGv ra = cpu_gpr[rA(ctx->opcode)];
1496 TCGv rs = cpu_gpr[rS(ctx->opcode)];
1497 TCGv t0 = tcg_temp_new();
1498 tcg_gen_shri_tl(t0, rs, 32);
1499 tcg_gen_xor_tl(ra, rs, t0);
1500 tcg_gen_shri_tl(t0, ra, 16);
1501 tcg_gen_xor_tl(ra, ra, t0);
1502 tcg_gen_shri_tl(t0, ra, 8);
1503 tcg_gen_xor_tl(ra, ra, t0);
1504 tcg_gen_andi_tl(ra, ra, 1);
1505 tcg_temp_free(t0);
1506}
1507#endif
1508
d9bce9d9
JM
1509#if defined(TARGET_PPC64)
1510/* extsw & extsw. */
26d67362 1511GEN_LOGICAL1(extsw, tcg_gen_ext32s_tl, 0x1E, PPC_64B);
99e300ef 1512
54623277 1513/* cntlzd */
99e300ef 1514static void gen_cntlzd(DisasContext *ctx)
26d67362 1515{
a7812ae4 1516 gen_helper_cntlzd(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)]);
26d67362
AJ
1517 if (unlikely(Rc(ctx->opcode) != 0))
1518 gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
1519}
d9bce9d9
JM
1520#endif
1521
79aceca5 1522/*** Integer rotate ***/
99e300ef 1523
54623277 1524/* rlwimi & rlwimi. */
99e300ef 1525static void gen_rlwimi(DisasContext *ctx)
79aceca5 1526{
76a66253 1527 uint32_t mb, me, sh;
79aceca5
FB
1528
1529 mb = MB(ctx->opcode);
1530 me = ME(ctx->opcode);
76a66253 1531 sh = SH(ctx->opcode);
d03ef511
AJ
1532 if (likely(sh == 0 && mb == 0 && me == 31)) {
1533 tcg_gen_ext32u_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)]);
1534 } else {
d03ef511 1535 target_ulong mask;
a7812ae4
PB
1536 TCGv t1;
1537 TCGv t0 = tcg_temp_new();
54843a58 1538#if defined(TARGET_PPC64)
a7812ae4
PB
1539 TCGv_i32 t2 = tcg_temp_new_i32();
1540 tcg_gen_trunc_i64_i32(t2, cpu_gpr[rS(ctx->opcode)]);
1541 tcg_gen_rotli_i32(t2, t2, sh);
1542 tcg_gen_extu_i32_i64(t0, t2);
1543 tcg_temp_free_i32(t2);
54843a58
AJ
1544#else
1545 tcg_gen_rotli_i32(t0, cpu_gpr[rS(ctx->opcode)], sh);
1546#endif
76a66253 1547#if defined(TARGET_PPC64)
d03ef511
AJ
1548 mb += 32;
1549 me += 32;
76a66253 1550#endif
d03ef511 1551 mask = MASK(mb, me);
a7812ae4 1552 t1 = tcg_temp_new();
d03ef511
AJ
1553 tcg_gen_andi_tl(t0, t0, mask);
1554 tcg_gen_andi_tl(t1, cpu_gpr[rA(ctx->opcode)], ~mask);
1555 tcg_gen_or_tl(cpu_gpr[rA(ctx->opcode)], t0, t1);
1556 tcg_temp_free(t0);
1557 tcg_temp_free(t1);
1558 }
76a66253 1559 if (unlikely(Rc(ctx->opcode) != 0))
d03ef511 1560 gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
79aceca5 1561}
99e300ef 1562
54623277 1563/* rlwinm & rlwinm. */
99e300ef 1564static void gen_rlwinm(DisasContext *ctx)
79aceca5
FB
1565{
1566 uint32_t mb, me, sh;
3b46e624 1567
79aceca5
FB
1568 sh = SH(ctx->opcode);
1569 mb = MB(ctx->opcode);
1570 me = ME(ctx->opcode);
d03ef511
AJ
1571
1572 if (likely(mb == 0 && me == (31 - sh))) {
1573 if (likely(sh == 0)) {
1574 tcg_gen_ext32u_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)]);
1575 } else {
a7812ae4 1576 TCGv t0 = tcg_temp_new();
d03ef511
AJ
1577 tcg_gen_ext32u_tl(t0, cpu_gpr[rS(ctx->opcode)]);
1578 tcg_gen_shli_tl(t0, t0, sh);
1579 tcg_gen_ext32u_tl(cpu_gpr[rA(ctx->opcode)], t0);
1580 tcg_temp_free(t0);
79aceca5 1581 }
d03ef511 1582 } else if (likely(sh != 0 && me == 31 && sh == (32 - mb))) {
a7812ae4 1583 TCGv t0 = tcg_temp_new();
d03ef511
AJ
1584 tcg_gen_ext32u_tl(t0, cpu_gpr[rS(ctx->opcode)]);
1585 tcg_gen_shri_tl(t0, t0, mb);
1586 tcg_gen_ext32u_tl(cpu_gpr[rA(ctx->opcode)], t0);
1587 tcg_temp_free(t0);
1588 } else {
a7812ae4 1589 TCGv t0 = tcg_temp_new();
54843a58 1590#if defined(TARGET_PPC64)
a7812ae4 1591 TCGv_i32 t1 = tcg_temp_new_i32();
54843a58
AJ
1592 tcg_gen_trunc_i64_i32(t1, cpu_gpr[rS(ctx->opcode)]);
1593 tcg_gen_rotli_i32(t1, t1, sh);
1594 tcg_gen_extu_i32_i64(t0, t1);
a7812ae4 1595 tcg_temp_free_i32(t1);
54843a58
AJ
1596#else
1597 tcg_gen_rotli_i32(t0, cpu_gpr[rS(ctx->opcode)], sh);
1598#endif
76a66253 1599#if defined(TARGET_PPC64)
d03ef511
AJ
1600 mb += 32;
1601 me += 32;
76a66253 1602#endif
d03ef511
AJ
1603 tcg_gen_andi_tl(cpu_gpr[rA(ctx->opcode)], t0, MASK(mb, me));
1604 tcg_temp_free(t0);
1605 }
76a66253 1606 if (unlikely(Rc(ctx->opcode) != 0))
d03ef511 1607 gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
79aceca5 1608}
99e300ef 1609
54623277 1610/* rlwnm & rlwnm. */
99e300ef 1611static void gen_rlwnm(DisasContext *ctx)
79aceca5
FB
1612{
1613 uint32_t mb, me;
54843a58
AJ
1614 TCGv t0;
1615#if defined(TARGET_PPC64)
a7812ae4 1616 TCGv_i32 t1, t2;
54843a58 1617#endif
79aceca5
FB
1618
1619 mb = MB(ctx->opcode);
1620 me = ME(ctx->opcode);
a7812ae4 1621 t0 = tcg_temp_new();
d03ef511 1622 tcg_gen_andi_tl(t0, cpu_gpr[rB(ctx->opcode)], 0x1f);
54843a58 1623#if defined(TARGET_PPC64)
a7812ae4
PB
1624 t1 = tcg_temp_new_i32();
1625 t2 = tcg_temp_new_i32();
54843a58
AJ
1626 tcg_gen_trunc_i64_i32(t1, cpu_gpr[rS(ctx->opcode)]);
1627 tcg_gen_trunc_i64_i32(t2, t0);
1628 tcg_gen_rotl_i32(t1, t1, t2);
1629 tcg_gen_extu_i32_i64(t0, t1);
a7812ae4
PB
1630 tcg_temp_free_i32(t1);
1631 tcg_temp_free_i32(t2);
54843a58
AJ
1632#else
1633 tcg_gen_rotl_i32(t0, cpu_gpr[rS(ctx->opcode)], t0);
1634#endif
76a66253
JM
1635 if (unlikely(mb != 0 || me != 31)) {
1636#if defined(TARGET_PPC64)
1637 mb += 32;
1638 me += 32;
1639#endif
54843a58 1640 tcg_gen_andi_tl(cpu_gpr[rA(ctx->opcode)], t0, MASK(mb, me));
d03ef511 1641 } else {
54843a58 1642 tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], t0);
79aceca5 1643 }
54843a58 1644 tcg_temp_free(t0);
76a66253 1645 if (unlikely(Rc(ctx->opcode) != 0))
d03ef511 1646 gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
79aceca5
FB
1647}
1648
d9bce9d9
JM
1649#if defined(TARGET_PPC64)
1650#define GEN_PPC64_R2(name, opc1, opc2) \
e8eaa2c0 1651static void glue(gen_, name##0)(DisasContext *ctx) \
d9bce9d9
JM
1652{ \
1653 gen_##name(ctx, 0); \
1654} \
e8eaa2c0
BS
1655 \
1656static void glue(gen_, name##1)(DisasContext *ctx) \
d9bce9d9
JM
1657{ \
1658 gen_##name(ctx, 1); \
1659}
1660#define GEN_PPC64_R4(name, opc1, opc2) \
e8eaa2c0 1661static void glue(gen_, name##0)(DisasContext *ctx) \
d9bce9d9
JM
1662{ \
1663 gen_##name(ctx, 0, 0); \
1664} \
e8eaa2c0
BS
1665 \
1666static void glue(gen_, name##1)(DisasContext *ctx) \
d9bce9d9
JM
1667{ \
1668 gen_##name(ctx, 0, 1); \
1669} \
e8eaa2c0
BS
1670 \
1671static void glue(gen_, name##2)(DisasContext *ctx) \
d9bce9d9
JM
1672{ \
1673 gen_##name(ctx, 1, 0); \
1674} \
e8eaa2c0
BS
1675 \
1676static void glue(gen_, name##3)(DisasContext *ctx) \
d9bce9d9
JM
1677{ \
1678 gen_##name(ctx, 1, 1); \
1679}
51789c41 1680
636aa200
BS
1681static inline void gen_rldinm(DisasContext *ctx, uint32_t mb, uint32_t me,
1682 uint32_t sh)
51789c41 1683{
d03ef511
AJ
1684 if (likely(sh != 0 && mb == 0 && me == (63 - sh))) {
1685 tcg_gen_shli_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)], sh);
1686 } else if (likely(sh != 0 && me == 63 && sh == (64 - mb))) {
1687 tcg_gen_shri_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)], mb);
1688 } else {
a7812ae4 1689 TCGv t0 = tcg_temp_new();
54843a58 1690 tcg_gen_rotli_tl(t0, cpu_gpr[rS(ctx->opcode)], sh);
d03ef511 1691 if (likely(mb == 0 && me == 63)) {
54843a58 1692 tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], t0);
d03ef511
AJ
1693 } else {
1694 tcg_gen_andi_tl(cpu_gpr[rA(ctx->opcode)], t0, MASK(mb, me));
51789c41 1695 }
d03ef511 1696 tcg_temp_free(t0);
51789c41 1697 }
51789c41 1698 if (unlikely(Rc(ctx->opcode) != 0))
d03ef511 1699 gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
51789c41 1700}
d9bce9d9 1701/* rldicl - rldicl. */
636aa200 1702static inline void gen_rldicl(DisasContext *ctx, int mbn, int shn)
d9bce9d9 1703{
51789c41 1704 uint32_t sh, mb;
d9bce9d9 1705
9d53c753
JM
1706 sh = SH(ctx->opcode) | (shn << 5);
1707 mb = MB(ctx->opcode) | (mbn << 5);
51789c41 1708 gen_rldinm(ctx, mb, 63, sh);
d9bce9d9 1709}
51789c41 1710GEN_PPC64_R4(rldicl, 0x1E, 0x00);
d9bce9d9 1711/* rldicr - rldicr. */
636aa200 1712static inline void gen_rldicr(DisasContext *ctx, int men, int shn)
d9bce9d9 1713{
51789c41 1714 uint32_t sh, me;
d9bce9d9 1715
9d53c753
JM
1716 sh = SH(ctx->opcode) | (shn << 5);
1717 me = MB(ctx->opcode) | (men << 5);
51789c41 1718 gen_rldinm(ctx, 0, me, sh);
d9bce9d9 1719}
51789c41 1720GEN_PPC64_R4(rldicr, 0x1E, 0x02);
d9bce9d9 1721/* rldic - rldic. */
636aa200 1722static inline void gen_rldic(DisasContext *ctx, int mbn, int shn)
d9bce9d9 1723{
51789c41 1724 uint32_t sh, mb;
d9bce9d9 1725
9d53c753
JM
1726 sh = SH(ctx->opcode) | (shn << 5);
1727 mb = MB(ctx->opcode) | (mbn << 5);
51789c41
JM
1728 gen_rldinm(ctx, mb, 63 - sh, sh);
1729}
1730GEN_PPC64_R4(rldic, 0x1E, 0x04);
1731
636aa200 1732static inline void gen_rldnm(DisasContext *ctx, uint32_t mb, uint32_t me)
51789c41 1733{
54843a58 1734 TCGv t0;
d03ef511 1735
a7812ae4 1736 t0 = tcg_temp_new();
d03ef511 1737 tcg_gen_andi_tl(t0, cpu_gpr[rB(ctx->opcode)], 0x3f);
54843a58 1738 tcg_gen_rotl_tl(t0, cpu_gpr[rS(ctx->opcode)], t0);
51789c41 1739 if (unlikely(mb != 0 || me != 63)) {
54843a58
AJ
1740 tcg_gen_andi_tl(cpu_gpr[rA(ctx->opcode)], t0, MASK(mb, me));
1741 } else {
1742 tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], t0);
1743 }
1744 tcg_temp_free(t0);
51789c41 1745 if (unlikely(Rc(ctx->opcode) != 0))
d03ef511 1746 gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
d9bce9d9 1747}
51789c41 1748
d9bce9d9 1749/* rldcl - rldcl. */
636aa200 1750static inline void gen_rldcl(DisasContext *ctx, int mbn)
d9bce9d9 1751{
51789c41 1752 uint32_t mb;
d9bce9d9 1753
9d53c753 1754 mb = MB(ctx->opcode) | (mbn << 5);
51789c41 1755 gen_rldnm(ctx, mb, 63);
d9bce9d9 1756}
36081602 1757GEN_PPC64_R2(rldcl, 0x1E, 0x08);
d9bce9d9 1758/* rldcr - rldcr. */
636aa200 1759static inline void gen_rldcr(DisasContext *ctx, int men)
d9bce9d9 1760{
51789c41 1761 uint32_t me;
d9bce9d9 1762
9d53c753 1763 me = MB(ctx->opcode) | (men << 5);
51789c41 1764 gen_rldnm(ctx, 0, me);
d9bce9d9 1765}
36081602 1766GEN_PPC64_R2(rldcr, 0x1E, 0x09);
d9bce9d9 1767/* rldimi - rldimi. */
636aa200 1768static inline void gen_rldimi(DisasContext *ctx, int mbn, int shn)
d9bce9d9 1769{
271a916e 1770 uint32_t sh, mb, me;
d9bce9d9 1771
9d53c753
JM
1772 sh = SH(ctx->opcode) | (shn << 5);
1773 mb = MB(ctx->opcode) | (mbn << 5);
271a916e 1774 me = 63 - sh;
d03ef511
AJ
1775 if (unlikely(sh == 0 && mb == 0)) {
1776 tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)]);
1777 } else {
1778 TCGv t0, t1;
1779 target_ulong mask;
1780
a7812ae4 1781 t0 = tcg_temp_new();
54843a58 1782 tcg_gen_rotli_tl(t0, cpu_gpr[rS(ctx->opcode)], sh);
a7812ae4 1783 t1 = tcg_temp_new();
d03ef511
AJ
1784 mask = MASK(mb, me);
1785 tcg_gen_andi_tl(t0, t0, mask);
1786 tcg_gen_andi_tl(t1, cpu_gpr[rA(ctx->opcode)], ~mask);
1787 tcg_gen_or_tl(cpu_gpr[rA(ctx->opcode)], t0, t1);
1788 tcg_temp_free(t0);
1789 tcg_temp_free(t1);
51789c41 1790 }
51789c41 1791 if (unlikely(Rc(ctx->opcode) != 0))
d03ef511 1792 gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
d9bce9d9 1793}
36081602 1794GEN_PPC64_R4(rldimi, 0x1E, 0x06);
d9bce9d9
JM
1795#endif
1796
79aceca5 1797/*** Integer shift ***/
99e300ef 1798
54623277 1799/* slw & slw. */
99e300ef 1800static void gen_slw(DisasContext *ctx)
26d67362 1801{
7fd6bf7d 1802 TCGv t0, t1;
26d67362 1803
7fd6bf7d
AJ
1804 t0 = tcg_temp_new();
1805 /* AND rS with a mask that is 0 when rB >= 0x20 */
1806#if defined(TARGET_PPC64)
1807 tcg_gen_shli_tl(t0, cpu_gpr[rB(ctx->opcode)], 0x3a);
1808 tcg_gen_sari_tl(t0, t0, 0x3f);
1809#else
1810 tcg_gen_shli_tl(t0, cpu_gpr[rB(ctx->opcode)], 0x1a);
1811 tcg_gen_sari_tl(t0, t0, 0x1f);
1812#endif
1813 tcg_gen_andc_tl(t0, cpu_gpr[rS(ctx->opcode)], t0);
1814 t1 = tcg_temp_new();
1815 tcg_gen_andi_tl(t1, cpu_gpr[rB(ctx->opcode)], 0x1f);
1816 tcg_gen_shl_tl(cpu_gpr[rA(ctx->opcode)], t0, t1);
1817 tcg_temp_free(t1);
fea0c503 1818 tcg_temp_free(t0);
7fd6bf7d 1819 tcg_gen_ext32u_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rA(ctx->opcode)]);
26d67362
AJ
1820 if (unlikely(Rc(ctx->opcode) != 0))
1821 gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
1822}
99e300ef 1823
54623277 1824/* sraw & sraw. */
99e300ef 1825static void gen_sraw(DisasContext *ctx)
26d67362 1826{
d15f74fb 1827 gen_helper_sraw(cpu_gpr[rA(ctx->opcode)], cpu_env,
a7812ae4 1828 cpu_gpr[rS(ctx->opcode)], cpu_gpr[rB(ctx->opcode)]);
26d67362
AJ
1829 if (unlikely(Rc(ctx->opcode) != 0))
1830 gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
1831}
99e300ef 1832
54623277 1833/* srawi & srawi. */
99e300ef 1834static void gen_srawi(DisasContext *ctx)
79aceca5 1835{
26d67362 1836 int sh = SH(ctx->opcode);
ba4af3e4
RH
1837 TCGv dst = cpu_gpr[rA(ctx->opcode)];
1838 TCGv src = cpu_gpr[rS(ctx->opcode)];
1839 if (sh == 0) {
1840 tcg_gen_mov_tl(dst, src);
da91a00f 1841 tcg_gen_movi_tl(cpu_ca, 0);
26d67362 1842 } else {
ba4af3e4
RH
1843 TCGv t0;
1844 tcg_gen_ext32s_tl(dst, src);
1845 tcg_gen_andi_tl(cpu_ca, dst, (1ULL << sh) - 1);
1846 t0 = tcg_temp_new();
1847 tcg_gen_sari_tl(t0, dst, TARGET_LONG_BITS - 1);
1848 tcg_gen_and_tl(cpu_ca, cpu_ca, t0);
1849 tcg_temp_free(t0);
1850 tcg_gen_setcondi_tl(TCG_COND_NE, cpu_ca, cpu_ca, 0);
1851 tcg_gen_sari_tl(dst, dst, sh);
1852 }
1853 if (unlikely(Rc(ctx->opcode) != 0)) {
1854 gen_set_Rc0(ctx, dst);
d9bce9d9 1855 }
79aceca5 1856}
99e300ef 1857
54623277 1858/* srw & srw. */
99e300ef 1859static void gen_srw(DisasContext *ctx)
26d67362 1860{
fea0c503 1861 TCGv t0, t1;
d9bce9d9 1862
7fd6bf7d
AJ
1863 t0 = tcg_temp_new();
1864 /* AND rS with a mask that is 0 when rB >= 0x20 */
1865#if defined(TARGET_PPC64)
1866 tcg_gen_shli_tl(t0, cpu_gpr[rB(ctx->opcode)], 0x3a);
1867 tcg_gen_sari_tl(t0, t0, 0x3f);
1868#else
1869 tcg_gen_shli_tl(t0, cpu_gpr[rB(ctx->opcode)], 0x1a);
1870 tcg_gen_sari_tl(t0, t0, 0x1f);
1871#endif
1872 tcg_gen_andc_tl(t0, cpu_gpr[rS(ctx->opcode)], t0);
1873 tcg_gen_ext32u_tl(t0, t0);
a7812ae4 1874 t1 = tcg_temp_new();
7fd6bf7d
AJ
1875 tcg_gen_andi_tl(t1, cpu_gpr[rB(ctx->opcode)], 0x1f);
1876 tcg_gen_shr_tl(cpu_gpr[rA(ctx->opcode)], t0, t1);
fea0c503 1877 tcg_temp_free(t1);
fea0c503 1878 tcg_temp_free(t0);
26d67362
AJ
1879 if (unlikely(Rc(ctx->opcode) != 0))
1880 gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
1881}
54623277 1882
d9bce9d9
JM
1883#if defined(TARGET_PPC64)
1884/* sld & sld. */
99e300ef 1885static void gen_sld(DisasContext *ctx)
26d67362 1886{
7fd6bf7d 1887 TCGv t0, t1;
26d67362 1888
7fd6bf7d
AJ
1889 t0 = tcg_temp_new();
1890 /* AND rS with a mask that is 0 when rB >= 0x40 */
1891 tcg_gen_shli_tl(t0, cpu_gpr[rB(ctx->opcode)], 0x39);
1892 tcg_gen_sari_tl(t0, t0, 0x3f);
1893 tcg_gen_andc_tl(t0, cpu_gpr[rS(ctx->opcode)], t0);
1894 t1 = tcg_temp_new();
1895 tcg_gen_andi_tl(t1, cpu_gpr[rB(ctx->opcode)], 0x3f);
1896 tcg_gen_shl_tl(cpu_gpr[rA(ctx->opcode)], t0, t1);
1897 tcg_temp_free(t1);
fea0c503 1898 tcg_temp_free(t0);
26d67362
AJ
1899 if (unlikely(Rc(ctx->opcode) != 0))
1900 gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
1901}
99e300ef 1902
54623277 1903/* srad & srad. */
99e300ef 1904static void gen_srad(DisasContext *ctx)
26d67362 1905{
d15f74fb 1906 gen_helper_srad(cpu_gpr[rA(ctx->opcode)], cpu_env,
a7812ae4 1907 cpu_gpr[rS(ctx->opcode)], cpu_gpr[rB(ctx->opcode)]);
26d67362
AJ
1908 if (unlikely(Rc(ctx->opcode) != 0))
1909 gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
1910}
d9bce9d9 1911/* sradi & sradi. */
636aa200 1912static inline void gen_sradi(DisasContext *ctx, int n)
d9bce9d9 1913{
26d67362 1914 int sh = SH(ctx->opcode) + (n << 5);
ba4af3e4
RH
1915 TCGv dst = cpu_gpr[rA(ctx->opcode)];
1916 TCGv src = cpu_gpr[rS(ctx->opcode)];
1917 if (sh == 0) {
1918 tcg_gen_mov_tl(dst, src);
da91a00f 1919 tcg_gen_movi_tl(cpu_ca, 0);
26d67362 1920 } else {
ba4af3e4
RH
1921 TCGv t0;
1922 tcg_gen_andi_tl(cpu_ca, src, (1ULL << sh) - 1);
1923 t0 = tcg_temp_new();
1924 tcg_gen_sari_tl(t0, src, TARGET_LONG_BITS - 1);
1925 tcg_gen_and_tl(cpu_ca, cpu_ca, t0);
1926 tcg_temp_free(t0);
1927 tcg_gen_setcondi_tl(TCG_COND_NE, cpu_ca, cpu_ca, 0);
1928 tcg_gen_sari_tl(dst, src, sh);
1929 }
1930 if (unlikely(Rc(ctx->opcode) != 0)) {
1931 gen_set_Rc0(ctx, dst);
d9bce9d9 1932 }
d9bce9d9 1933}
e8eaa2c0
BS
1934
1935static void gen_sradi0(DisasContext *ctx)
d9bce9d9
JM
1936{
1937 gen_sradi(ctx, 0);
1938}
e8eaa2c0
BS
1939
1940static void gen_sradi1(DisasContext *ctx)
d9bce9d9
JM
1941{
1942 gen_sradi(ctx, 1);
1943}
99e300ef 1944
54623277 1945/* srd & srd. */
99e300ef 1946static void gen_srd(DisasContext *ctx)
26d67362 1947{
7fd6bf7d 1948 TCGv t0, t1;
26d67362 1949
7fd6bf7d
AJ
1950 t0 = tcg_temp_new();
1951 /* AND rS with a mask that is 0 when rB >= 0x40 */
1952 tcg_gen_shli_tl(t0, cpu_gpr[rB(ctx->opcode)], 0x39);
1953 tcg_gen_sari_tl(t0, t0, 0x3f);
1954 tcg_gen_andc_tl(t0, cpu_gpr[rS(ctx->opcode)], t0);
1955 t1 = tcg_temp_new();
1956 tcg_gen_andi_tl(t1, cpu_gpr[rB(ctx->opcode)], 0x3f);
1957 tcg_gen_shr_tl(cpu_gpr[rA(ctx->opcode)], t0, t1);
1958 tcg_temp_free(t1);
fea0c503 1959 tcg_temp_free(t0);
26d67362
AJ
1960 if (unlikely(Rc(ctx->opcode) != 0))
1961 gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
1962}
d9bce9d9 1963#endif
79aceca5
FB
1964
1965/*** Floating-Point arithmetic ***/
7c58044c 1966#define _GEN_FLOAT_ACB(name, op, op1, op2, isfloat, set_fprf, type) \
99e300ef 1967static void gen_f##name(DisasContext *ctx) \
9a64fbe4 1968{ \
76a66253 1969 if (unlikely(!ctx->fpu_enabled)) { \
e06fcd75 1970 gen_exception(ctx, POWERPC_EXCP_FPU); \
3cc62370
FB
1971 return; \
1972 } \
eb44b959
AJ
1973 /* NIP cannot be restored if the memory exception comes from an helper */ \
1974 gen_update_nip(ctx, ctx->nip - 4); \
7c58044c 1975 gen_reset_fpstatus(); \
8e703949
BS
1976 gen_helper_f##op(cpu_fpr[rD(ctx->opcode)], cpu_env, \
1977 cpu_fpr[rA(ctx->opcode)], \
af12906f 1978 cpu_fpr[rC(ctx->opcode)], cpu_fpr[rB(ctx->opcode)]); \
4ecc3190 1979 if (isfloat) { \
8e703949
BS
1980 gen_helper_frsp(cpu_fpr[rD(ctx->opcode)], cpu_env, \
1981 cpu_fpr[rD(ctx->opcode)]); \
4ecc3190 1982 } \
af12906f
AJ
1983 gen_compute_fprf(cpu_fpr[rD(ctx->opcode)], set_fprf, \
1984 Rc(ctx->opcode) != 0); \
9a64fbe4
FB
1985}
1986
7c58044c
JM
1987#define GEN_FLOAT_ACB(name, op2, set_fprf, type) \
1988_GEN_FLOAT_ACB(name, name, 0x3F, op2, 0, set_fprf, type); \
1989_GEN_FLOAT_ACB(name##s, name, 0x3B, op2, 1, set_fprf, type);
9a64fbe4 1990
7c58044c 1991#define _GEN_FLOAT_AB(name, op, op1, op2, inval, isfloat, set_fprf, type) \
99e300ef 1992static void gen_f##name(DisasContext *ctx) \
9a64fbe4 1993{ \
76a66253 1994 if (unlikely(!ctx->fpu_enabled)) { \
e06fcd75 1995 gen_exception(ctx, POWERPC_EXCP_FPU); \
3cc62370
FB
1996 return; \
1997 } \
eb44b959
AJ
1998 /* NIP cannot be restored if the memory exception comes from an helper */ \
1999 gen_update_nip(ctx, ctx->nip - 4); \
7c58044c 2000 gen_reset_fpstatus(); \
8e703949
BS
2001 gen_helper_f##op(cpu_fpr[rD(ctx->opcode)], cpu_env, \
2002 cpu_fpr[rA(ctx->opcode)], \
af12906f 2003 cpu_fpr[rB(ctx->opcode)]); \
4ecc3190 2004 if (isfloat) { \
8e703949
BS
2005 gen_helper_frsp(cpu_fpr[rD(ctx->opcode)], cpu_env, \
2006 cpu_fpr[rD(ctx->opcode)]); \
4ecc3190 2007 } \
af12906f
AJ
2008 gen_compute_fprf(cpu_fpr[rD(ctx->opcode)], \
2009 set_fprf, Rc(ctx->opcode) != 0); \
9a64fbe4 2010}
7c58044c
JM
2011#define GEN_FLOAT_AB(name, op2, inval, set_fprf, type) \
2012_GEN_FLOAT_AB(name, name, 0x3F, op2, inval, 0, set_fprf, type); \
2013_GEN_FLOAT_AB(name##s, name, 0x3B, op2, inval, 1, set_fprf, type);
9a64fbe4 2014
7c58044c 2015#define _GEN_FLOAT_AC(name, op, op1, op2, inval, isfloat, set_fprf, type) \
99e300ef 2016static void gen_f##name(DisasContext *ctx) \
9a64fbe4 2017{ \
76a66253 2018 if (unlikely(!ctx->fpu_enabled)) { \
e06fcd75 2019 gen_exception(ctx, POWERPC_EXCP_FPU); \
3cc62370
FB
2020 return; \
2021 } \
eb44b959
AJ
2022 /* NIP cannot be restored if the memory exception comes from an helper */ \
2023 gen_update_nip(ctx, ctx->nip - 4); \
7c58044c 2024 gen_reset_fpstatus(); \
8e703949
BS
2025 gen_helper_f##op(cpu_fpr[rD(ctx->opcode)], cpu_env, \
2026 cpu_fpr[rA(ctx->opcode)], \
2027 cpu_fpr[rC(ctx->opcode)]); \
4ecc3190 2028 if (isfloat) { \
8e703949
BS
2029 gen_helper_frsp(cpu_fpr[rD(ctx->opcode)], cpu_env, \
2030 cpu_fpr[rD(ctx->opcode)]); \
4ecc3190 2031 } \
af12906f
AJ
2032 gen_compute_fprf(cpu_fpr[rD(ctx->opcode)], \
2033 set_fprf, Rc(ctx->opcode) != 0); \
9a64fbe4 2034}
7c58044c
JM
2035#define GEN_FLOAT_AC(name, op2, inval, set_fprf, type) \
2036_GEN_FLOAT_AC(name, name, 0x3F, op2, inval, 0, set_fprf, type); \
2037_GEN_FLOAT_AC(name##s, name, 0x3B, op2, inval, 1, set_fprf, type);
9a64fbe4 2038
7c58044c 2039#define GEN_FLOAT_B(name, op2, op3, set_fprf, type) \
99e300ef 2040static void gen_f##name(DisasContext *ctx) \
9a64fbe4 2041{ \
76a66253 2042 if (unlikely(!ctx->fpu_enabled)) { \
e06fcd75 2043 gen_exception(ctx, POWERPC_EXCP_FPU); \
3cc62370
FB
2044 return; \
2045 } \
eb44b959
AJ
2046 /* NIP cannot be restored if the memory exception comes from an helper */ \
2047 gen_update_nip(ctx, ctx->nip - 4); \
7c58044c 2048 gen_reset_fpstatus(); \
8e703949
BS
2049 gen_helper_f##name(cpu_fpr[rD(ctx->opcode)], cpu_env, \
2050 cpu_fpr[rB(ctx->opcode)]); \
af12906f
AJ
2051 gen_compute_fprf(cpu_fpr[rD(ctx->opcode)], \
2052 set_fprf, Rc(ctx->opcode) != 0); \
79aceca5
FB
2053}
2054
7c58044c 2055#define GEN_FLOAT_BS(name, op1, op2, set_fprf, type) \
99e300ef 2056static void gen_f##name(DisasContext *ctx) \
9a64fbe4 2057{ \
76a66253 2058 if (unlikely(!ctx->fpu_enabled)) { \
e06fcd75 2059 gen_exception(ctx, POWERPC_EXCP_FPU); \
3cc62370
FB
2060 return; \
2061 } \
eb44b959
AJ
2062 /* NIP cannot be restored if the memory exception comes from an helper */ \
2063 gen_update_nip(ctx, ctx->nip - 4); \
7c58044c 2064 gen_reset_fpstatus(); \
8e703949
BS
2065 gen_helper_f##name(cpu_fpr[rD(ctx->opcode)], cpu_env, \
2066 cpu_fpr[rB(ctx->opcode)]); \
af12906f
AJ
2067 gen_compute_fprf(cpu_fpr[rD(ctx->opcode)], \
2068 set_fprf, Rc(ctx->opcode) != 0); \
79aceca5
FB
2069}
2070
9a64fbe4 2071/* fadd - fadds */
7c58044c 2072GEN_FLOAT_AB(add, 0x15, 0x000007C0, 1, PPC_FLOAT);
4ecc3190 2073/* fdiv - fdivs */
7c58044c 2074GEN_FLOAT_AB(div, 0x12, 0x000007C0, 1, PPC_FLOAT);
4ecc3190 2075/* fmul - fmuls */
7c58044c 2076GEN_FLOAT_AC(mul, 0x19, 0x0000F800, 1, PPC_FLOAT);
79aceca5 2077
d7e4b87e 2078/* fre */
7c58044c 2079GEN_FLOAT_BS(re, 0x3F, 0x18, 1, PPC_FLOAT_EXT);
d7e4b87e 2080
a750fc0b 2081/* fres */
7c58044c 2082GEN_FLOAT_BS(res, 0x3B, 0x18, 1, PPC_FLOAT_FRES);
79aceca5 2083
a750fc0b 2084/* frsqrte */
7c58044c
JM
2085GEN_FLOAT_BS(rsqrte, 0x3F, 0x1A, 1, PPC_FLOAT_FRSQRTE);
2086
2087/* frsqrtes */
99e300ef 2088static void gen_frsqrtes(DisasContext *ctx)
7c58044c 2089{
af12906f 2090 if (unlikely(!ctx->fpu_enabled)) {
e06fcd75 2091 gen_exception(ctx, POWERPC_EXCP_FPU);
af12906f
AJ
2092 return;
2093 }
eb44b959
AJ
2094 /* NIP cannot be restored if the memory exception comes from an helper */
2095 gen_update_nip(ctx, ctx->nip - 4);
af12906f 2096 gen_reset_fpstatus();
8e703949
BS
2097 gen_helper_frsqrte(cpu_fpr[rD(ctx->opcode)], cpu_env,
2098 cpu_fpr[rB(ctx->opcode)]);
2099 gen_helper_frsp(cpu_fpr[rD(ctx->opcode)], cpu_env,
2100 cpu_fpr[rD(ctx->opcode)]);
af12906f 2101 gen_compute_fprf(cpu_fpr[rD(ctx->opcode)], 1, Rc(ctx->opcode) != 0);
7c58044c 2102}
79aceca5 2103
a750fc0b 2104/* fsel */
7c58044c 2105_GEN_FLOAT_ACB(sel, sel, 0x3F, 0x17, 0, 0, PPC_FLOAT_FSEL);
4ecc3190 2106/* fsub - fsubs */
7c58044c 2107GEN_FLOAT_AB(sub, 0x14, 0x000007C0, 1, PPC_FLOAT);
79aceca5 2108/* Optional: */
99e300ef 2109
54623277 2110/* fsqrt */
99e300ef 2111static void gen_fsqrt(DisasContext *ctx)
c7d344af 2112{
76a66253 2113 if (unlikely(!ctx->fpu_enabled)) {
e06fcd75 2114 gen_exception(ctx, POWERPC_EXCP_FPU);
c7d344af
FB
2115 return;
2116 }
eb44b959
AJ
2117 /* NIP cannot be restored if the memory exception comes from an helper */
2118 gen_update_nip(ctx, ctx->nip - 4);
7c58044c 2119 gen_reset_fpstatus();
8e703949
BS
2120 gen_helper_fsqrt(cpu_fpr[rD(ctx->opcode)], cpu_env,
2121 cpu_fpr[rB(ctx->opcode)]);
af12906f 2122 gen_compute_fprf(cpu_fpr[rD(ctx->opcode)], 1, Rc(ctx->opcode) != 0);
c7d344af 2123}
79aceca5 2124
99e300ef 2125static void gen_fsqrts(DisasContext *ctx)
79aceca5 2126{
76a66253 2127 if (unlikely(!ctx->fpu_enabled)) {
e06fcd75 2128 gen_exception(ctx, POWERPC_EXCP_FPU);
3cc62370
FB
2129 return;
2130 }
eb44b959
AJ
2131 /* NIP cannot be restored if the memory exception comes from an helper */
2132 gen_update_nip(ctx, ctx->nip - 4);
7c58044c 2133 gen_reset_fpstatus();
8e703949
BS
2134 gen_helper_fsqrt(cpu_fpr[rD(ctx->opcode)], cpu_env,
2135 cpu_fpr[rB(ctx->opcode)]);
2136 gen_helper_frsp(cpu_fpr[rD(ctx->opcode)], cpu_env,
2137 cpu_fpr[rD(ctx->opcode)]);
af12906f 2138 gen_compute_fprf(cpu_fpr[rD(ctx->opcode)], 1, Rc(ctx->opcode) != 0);
79aceca5
FB
2139}
2140
2141/*** Floating-Point multiply-and-add ***/
4ecc3190 2142/* fmadd - fmadds */
7c58044c 2143GEN_FLOAT_ACB(madd, 0x1D, 1, PPC_FLOAT);
4ecc3190 2144/* fmsub - fmsubs */
7c58044c 2145GEN_FLOAT_ACB(msub, 0x1C, 1, PPC_FLOAT);
4ecc3190 2146/* fnmadd - fnmadds */
7c58044c 2147GEN_FLOAT_ACB(nmadd, 0x1F, 1, PPC_FLOAT);
4ecc3190 2148/* fnmsub - fnmsubs */
7c58044c 2149GEN_FLOAT_ACB(nmsub, 0x1E, 1, PPC_FLOAT);
79aceca5
FB
2150
2151/*** Floating-Point round & convert ***/
2152/* fctiw */
7c58044c 2153GEN_FLOAT_B(ctiw, 0x0E, 0x00, 0, PPC_FLOAT);
79aceca5 2154/* fctiwz */
7c58044c 2155GEN_FLOAT_B(ctiwz, 0x0F, 0x00, 0, PPC_FLOAT);
79aceca5 2156/* frsp */
7c58044c 2157GEN_FLOAT_B(rsp, 0x0C, 0x00, 1, PPC_FLOAT);
426613db
JM
2158#if defined(TARGET_PPC64)
2159/* fcfid */
7c58044c 2160GEN_FLOAT_B(cfid, 0x0E, 0x1A, 1, PPC_64B);
426613db 2161/* fctid */
7c58044c 2162GEN_FLOAT_B(ctid, 0x0E, 0x19, 0, PPC_64B);
426613db 2163/* fctidz */
7c58044c 2164GEN_FLOAT_B(ctidz, 0x0F, 0x19, 0, PPC_64B);
426613db 2165#endif
79aceca5 2166
d7e4b87e 2167/* frin */
7c58044c 2168GEN_FLOAT_B(rin, 0x08, 0x0C, 1, PPC_FLOAT_EXT);
d7e4b87e 2169/* friz */
7c58044c 2170GEN_FLOAT_B(riz, 0x08, 0x0D, 1, PPC_FLOAT_EXT);
d7e4b87e 2171/* frip */
7c58044c 2172GEN_FLOAT_B(rip, 0x08, 0x0E, 1, PPC_FLOAT_EXT);
d7e4b87e 2173/* frim */
7c58044c 2174GEN_FLOAT_B(rim, 0x08, 0x0F, 1, PPC_FLOAT_EXT);
d7e4b87e 2175
79aceca5 2176/*** Floating-Point compare ***/
99e300ef 2177
54623277 2178/* fcmpo */
99e300ef 2179static void gen_fcmpo(DisasContext *ctx)
79aceca5 2180{
330c483b 2181 TCGv_i32 crf;
76a66253 2182 if (unlikely(!ctx->fpu_enabled)) {
e06fcd75 2183 gen_exception(ctx, POWERPC_EXCP_FPU);
3cc62370
FB
2184 return;
2185 }
eb44b959
AJ
2186 /* NIP cannot be restored if the memory exception comes from an helper */
2187 gen_update_nip(ctx, ctx->nip - 4);
7c58044c 2188 gen_reset_fpstatus();
9a819377 2189 crf = tcg_const_i32(crfD(ctx->opcode));
8e703949
BS
2190 gen_helper_fcmpo(cpu_env, cpu_fpr[rA(ctx->opcode)],
2191 cpu_fpr[rB(ctx->opcode)], crf);
330c483b 2192 tcg_temp_free_i32(crf);
8e703949 2193 gen_helper_float_check_status(cpu_env);
79aceca5
FB
2194}
2195
2196/* fcmpu */
99e300ef 2197static void gen_fcmpu(DisasContext *ctx)
79aceca5 2198{
330c483b 2199 TCGv_i32 crf;
76a66253 2200 if (unlikely(!ctx->fpu_enabled)) {
e06fcd75 2201 gen_exception(ctx, POWERPC_EXCP_FPU);
3cc62370
FB
2202 return;
2203 }
eb44b959
AJ
2204 /* NIP cannot be restored if the memory exception comes from an helper */
2205 gen_update_nip(ctx, ctx->nip - 4);
7c58044c 2206 gen_reset_fpstatus();
9a819377 2207 crf = tcg_const_i32(crfD(ctx->opcode));
8e703949
BS
2208 gen_helper_fcmpu(cpu_env, cpu_fpr[rA(ctx->opcode)],
2209 cpu_fpr[rB(ctx->opcode)], crf);
330c483b 2210 tcg_temp_free_i32(crf);
8e703949 2211 gen_helper_float_check_status(cpu_env);
79aceca5
FB
2212}
2213
9a64fbe4
FB
2214/*** Floating-point move ***/
2215/* fabs */
7c58044c 2216/* XXX: beware that fabs never checks for NaNs nor update FPSCR */
bf45a2e6
AJ
2217static void gen_fabs(DisasContext *ctx)
2218{
2219 if (unlikely(!ctx->fpu_enabled)) {
2220 gen_exception(ctx, POWERPC_EXCP_FPU);
2221 return;
2222 }
2223 tcg_gen_andi_i64(cpu_fpr[rD(ctx->opcode)], cpu_fpr[rB(ctx->opcode)],
2224 ~(1ULL << 63));
2225 gen_compute_fprf(cpu_fpr[rD(ctx->opcode)], 0, Rc(ctx->opcode) != 0);
2226}
9a64fbe4
FB
2227
2228/* fmr - fmr. */
7c58044c 2229/* XXX: beware that fmr never checks for NaNs nor update FPSCR */
99e300ef 2230static void gen_fmr(DisasContext *ctx)
9a64fbe4 2231{
76a66253 2232 if (unlikely(!ctx->fpu_enabled)) {
e06fcd75 2233 gen_exception(ctx, POWERPC_EXCP_FPU);
3cc62370
FB
2234 return;
2235 }
af12906f
AJ
2236 tcg_gen_mov_i64(cpu_fpr[rD(ctx->opcode)], cpu_fpr[rB(ctx->opcode)]);
2237 gen_compute_fprf(cpu_fpr[rD(ctx->opcode)], 0, Rc(ctx->opcode) != 0);
9a64fbe4
FB
2238}
2239
2240/* fnabs */
7c58044c 2241/* XXX: beware that fnabs never checks for NaNs nor update FPSCR */
bf45a2e6
AJ
2242static void gen_fnabs(DisasContext *ctx)
2243{
2244 if (unlikely(!ctx->fpu_enabled)) {
2245 gen_exception(ctx, POWERPC_EXCP_FPU);
2246 return;
2247 }
2248 tcg_gen_ori_i64(cpu_fpr[rD(ctx->opcode)], cpu_fpr[rB(ctx->opcode)],
2249 1ULL << 63);
2250 gen_compute_fprf(cpu_fpr[rD(ctx->opcode)], 0, Rc(ctx->opcode) != 0);
2251}
2252
9a64fbe4 2253/* fneg */
7c58044c 2254/* XXX: beware that fneg never checks for NaNs nor update FPSCR */
bf45a2e6
AJ
2255static void gen_fneg(DisasContext *ctx)
2256{
2257 if (unlikely(!ctx->fpu_enabled)) {
2258 gen_exception(ctx, POWERPC_EXCP_FPU);
2259 return;
2260 }
2261 tcg_gen_xori_i64(cpu_fpr[rD(ctx->opcode)], cpu_fpr[rB(ctx->opcode)],
2262 1ULL << 63);
2263 gen_compute_fprf(cpu_fpr[rD(ctx->opcode)], 0, Rc(ctx->opcode) != 0);
2264}
9a64fbe4 2265
f0332888
AJ
2266/* fcpsgn: PowerPC 2.05 specification */
2267/* XXX: beware that fcpsgn never checks for NaNs nor update FPSCR */
2268static void gen_fcpsgn(DisasContext *ctx)
2269{
2270 if (unlikely(!ctx->fpu_enabled)) {
2271 gen_exception(ctx, POWERPC_EXCP_FPU);
2272 return;
2273 }
2274 tcg_gen_deposit_i64(cpu_fpr[rD(ctx->opcode)], cpu_fpr[rA(ctx->opcode)],
2275 cpu_fpr[rB(ctx->opcode)], 0, 63);
2276 gen_compute_fprf(cpu_fpr[rD(ctx->opcode)], 0, Rc(ctx->opcode) != 0);
2277}
2278
79aceca5 2279/*** Floating-Point status & ctrl register ***/
99e300ef 2280
54623277 2281/* mcrfs */
99e300ef 2282static void gen_mcrfs(DisasContext *ctx)
79aceca5 2283{
30304420 2284 TCGv tmp = tcg_temp_new();
7c58044c
JM
2285 int bfa;
2286
76a66253 2287 if (unlikely(!ctx->fpu_enabled)) {
e06fcd75 2288 gen_exception(ctx, POWERPC_EXCP_FPU);
3cc62370
FB
2289 return;
2290 }
7c58044c 2291 bfa = 4 * (7 - crfS(ctx->opcode));
30304420
DG
2292 tcg_gen_shri_tl(tmp, cpu_fpscr, bfa);
2293 tcg_gen_trunc_tl_i32(cpu_crf[crfD(ctx->opcode)], tmp);
2294 tcg_temp_free(tmp);
e1571908 2295 tcg_gen_andi_i32(cpu_crf[crfD(ctx->opcode)], cpu_crf[crfD(ctx->opcode)], 0xf);
30304420 2296 tcg_gen_andi_tl(cpu_fpscr, cpu_fpscr, ~(0xF << bfa));
79aceca5
FB
2297}
2298
2299/* mffs */
99e300ef 2300static void gen_mffs(DisasContext *ctx)
79aceca5 2301{
76a66253 2302 if (unlikely(!ctx->fpu_enabled)) {
e06fcd75 2303 gen_exception(ctx, POWERPC_EXCP_FPU);
3cc62370
FB
2304 return;
2305 }
7c58044c 2306 gen_reset_fpstatus();
30304420 2307 tcg_gen_extu_tl_i64(cpu_fpr[rD(ctx->opcode)], cpu_fpscr);
af12906f 2308 gen_compute_fprf(cpu_fpr[rD(ctx->opcode)], 0, Rc(ctx->opcode) != 0);
79aceca5
FB
2309}
2310
2311/* mtfsb0 */
99e300ef 2312static void gen_mtfsb0(DisasContext *ctx)
79aceca5 2313{
fb0eaffc 2314 uint8_t crb;
3b46e624 2315
76a66253 2316 if (unlikely(!ctx->fpu_enabled)) {
e06fcd75 2317 gen_exception(ctx, POWERPC_EXCP_FPU);
3cc62370
FB
2318 return;
2319 }
6e35d524 2320 crb = 31 - crbD(ctx->opcode);
7c58044c 2321 gen_reset_fpstatus();
6e35d524 2322 if (likely(crb != FPSCR_FEX && crb != FPSCR_VX)) {
eb44b959
AJ
2323 TCGv_i32 t0;
2324 /* NIP cannot be restored if the memory exception comes from an helper */
2325 gen_update_nip(ctx, ctx->nip - 4);
2326 t0 = tcg_const_i32(crb);
8e703949 2327 gen_helper_fpscr_clrbit(cpu_env, t0);
6e35d524
AJ
2328 tcg_temp_free_i32(t0);
2329 }
7c58044c 2330 if (unlikely(Rc(ctx->opcode) != 0)) {
30304420
DG
2331 tcg_gen_trunc_tl_i32(cpu_crf[1], cpu_fpscr);
2332 tcg_gen_shri_i32(cpu_crf[1], cpu_crf[1], FPSCR_OX);
7c58044c 2333 }
79aceca5
FB
2334}
2335
2336/* mtfsb1 */
99e300ef 2337static void gen_mtfsb1(DisasContext *ctx)
79aceca5 2338{
fb0eaffc 2339 uint8_t crb;
3b46e624 2340
76a66253 2341 if (unlikely(!ctx->fpu_enabled)) {
e06fcd75 2342 gen_exception(ctx, POWERPC_EXCP_FPU);
3cc62370
FB
2343 return;
2344 }
6e35d524 2345 crb = 31 - crbD(ctx->opcode);
7c58044c
JM
2346 gen_reset_fpstatus();
2347 /* XXX: we pretend we can only do IEEE floating-point computations */
af12906f 2348 if (likely(crb != FPSCR_FEX && crb != FPSCR_VX && crb != FPSCR_NI)) {
eb44b959
AJ
2349 TCGv_i32 t0;
2350 /* NIP cannot be restored if the memory exception comes from an helper */
2351 gen_update_nip(ctx, ctx->nip - 4);
2352 t0 = tcg_const_i32(crb);
8e703949 2353 gen_helper_fpscr_setbit(cpu_env, t0);
0f2f39c2 2354 tcg_temp_free_i32(t0);
af12906f 2355 }
7c58044c 2356 if (unlikely(Rc(ctx->opcode) != 0)) {
30304420
DG
2357 tcg_gen_trunc_tl_i32(cpu_crf[1], cpu_fpscr);
2358 tcg_gen_shri_i32(cpu_crf[1], cpu_crf[1], FPSCR_OX);
7c58044c
JM
2359 }
2360 /* We can raise a differed exception */
8e703949 2361 gen_helper_float_check_status(cpu_env);
79aceca5
FB
2362}
2363
2364/* mtfsf */
99e300ef 2365static void gen_mtfsf(DisasContext *ctx)
79aceca5 2366{
0f2f39c2 2367 TCGv_i32 t0;
7d08d856 2368 int flm, l, w;
af12906f 2369
76a66253 2370 if (unlikely(!ctx->fpu_enabled)) {
e06fcd75 2371 gen_exception(ctx, POWERPC_EXCP_FPU);
3cc62370
FB
2372 return;
2373 }
7d08d856
AJ
2374 flm = FPFLM(ctx->opcode);
2375 l = FPL(ctx->opcode);
2376 w = FPW(ctx->opcode);
2377 if (unlikely(w & !(ctx->insns_flags2 & PPC2_ISA205))) {
2378 gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL);
2379 return;
2380 }
eb44b959
AJ
2381 /* NIP cannot be restored if the memory exception comes from an helper */
2382 gen_update_nip(ctx, ctx->nip - 4);
7c58044c 2383 gen_reset_fpstatus();
7d08d856
AJ
2384 if (l) {
2385 t0 = tcg_const_i32((ctx->insns_flags2 & PPC2_ISA205) ? 0xffff : 0xff);
2386 } else {
2387 t0 = tcg_const_i32(flm << (w * 8));
2388 }
8e703949 2389 gen_helper_store_fpscr(cpu_env, cpu_fpr[rB(ctx->opcode)], t0);
0f2f39c2 2390 tcg_temp_free_i32(t0);
7c58044c 2391 if (unlikely(Rc(ctx->opcode) != 0)) {
30304420
DG
2392 tcg_gen_trunc_tl_i32(cpu_crf[1], cpu_fpscr);
2393 tcg_gen_shri_i32(cpu_crf[1], cpu_crf[1], FPSCR_OX);
7c58044c
JM
2394 }
2395 /* We can raise a differed exception */
8e703949 2396 gen_helper_float_check_status(cpu_env);
79aceca5
FB
2397}
2398
2399/* mtfsfi */
99e300ef 2400static void gen_mtfsfi(DisasContext *ctx)
79aceca5 2401{
7d08d856 2402 int bf, sh, w;
0f2f39c2
AJ
2403 TCGv_i64 t0;
2404 TCGv_i32 t1;
7c58044c 2405
76a66253 2406 if (unlikely(!ctx->fpu_enabled)) {
e06fcd75 2407 gen_exception(ctx, POWERPC_EXCP_FPU);
3cc62370
FB
2408 return;
2409 }
7d08d856
AJ
2410 w = FPW(ctx->opcode);
2411 bf = FPBF(ctx->opcode);
2412 if (unlikely(w & !(ctx->insns_flags2 & PPC2_ISA205))) {
2413 gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL);
2414 return;
2415 }
2416 sh = (8 * w) + 7 - bf;
eb44b959
AJ
2417 /* NIP cannot be restored if the memory exception comes from an helper */
2418 gen_update_nip(ctx, ctx->nip - 4);
7c58044c 2419 gen_reset_fpstatus();
7d08d856 2420 t0 = tcg_const_i64(((uint64_t)FPIMM(ctx->opcode)) << (4 * sh));
af12906f 2421 t1 = tcg_const_i32(1 << sh);
8e703949 2422 gen_helper_store_fpscr(cpu_env, t0, t1);
0f2f39c2
AJ
2423 tcg_temp_free_i64(t0);
2424 tcg_temp_free_i32(t1);
7c58044c 2425 if (unlikely(Rc(ctx->opcode) != 0)) {
30304420
DG
2426 tcg_gen_trunc_tl_i32(cpu_crf[1], cpu_fpscr);
2427 tcg_gen_shri_i32(cpu_crf[1], cpu_crf[1], FPSCR_OX);
7c58044c
JM
2428 }
2429 /* We can raise a differed exception */
8e703949 2430 gen_helper_float_check_status(cpu_env);
79aceca5
FB
2431}
2432
76a66253
JM
2433/*** Addressing modes ***/
2434/* Register indirect with immediate index : EA = (rA|0) + SIMM */
636aa200
BS
2435static inline void gen_addr_imm_index(DisasContext *ctx, TCGv EA,
2436 target_long maskl)
76a66253
JM
2437{
2438 target_long simm = SIMM(ctx->opcode);
2439
be147d08 2440 simm &= ~maskl;
76db3ba4 2441 if (rA(ctx->opcode) == 0) {
c791fe84
RH
2442 if (NARROW_MODE(ctx)) {
2443 simm = (uint32_t)simm;
2444 }
e2be8d8d 2445 tcg_gen_movi_tl(EA, simm);
76db3ba4 2446 } else if (likely(simm != 0)) {
e2be8d8d 2447 tcg_gen_addi_tl(EA, cpu_gpr[rA(ctx->opcode)], simm);
c791fe84 2448 if (NARROW_MODE(ctx)) {
76db3ba4
AJ
2449 tcg_gen_ext32u_tl(EA, EA);
2450 }
76db3ba4 2451 } else {
c791fe84 2452 if (NARROW_MODE(ctx)) {
76db3ba4 2453 tcg_gen_ext32u_tl(EA, cpu_gpr[rA(ctx->opcode)]);
c791fe84
RH
2454 } else {
2455 tcg_gen_mov_tl(EA, cpu_gpr[rA(ctx->opcode)]);
2456 }
76db3ba4 2457 }
76a66253
JM
2458}
2459
636aa200 2460static inline void gen_addr_reg_index(DisasContext *ctx, TCGv EA)
76a66253 2461{
76db3ba4 2462 if (rA(ctx->opcode) == 0) {
c791fe84 2463 if (NARROW_MODE(ctx)) {
76db3ba4 2464 tcg_gen_ext32u_tl(EA, cpu_gpr[rB(ctx->opcode)]);
c791fe84
RH
2465 } else {
2466 tcg_gen_mov_tl(EA, cpu_gpr[rB(ctx->opcode)]);
2467 }
76db3ba4 2468 } else {
e2be8d8d 2469 tcg_gen_add_tl(EA, cpu_gpr[rA(ctx->opcode)], cpu_gpr[rB(ctx->opcode)]);
c791fe84 2470 if (NARROW_MODE(ctx)) {
76db3ba4
AJ
2471 tcg_gen_ext32u_tl(EA, EA);
2472 }
76db3ba4 2473 }
76a66253
JM
2474}
2475
636aa200 2476static inline void gen_addr_register(DisasContext *ctx, TCGv EA)
76a66253 2477{
76db3ba4 2478 if (rA(ctx->opcode) == 0) {
e2be8d8d 2479 tcg_gen_movi_tl(EA, 0);
c791fe84
RH
2480 } else if (NARROW_MODE(ctx)) {
2481 tcg_gen_ext32u_tl(EA, cpu_gpr[rA(ctx->opcode)]);
76db3ba4 2482 } else {
c791fe84 2483 tcg_gen_mov_tl(EA, cpu_gpr[rA(ctx->opcode)]);
76db3ba4
AJ
2484 }
2485}
2486
636aa200
BS
2487static inline void gen_addr_add(DisasContext *ctx, TCGv ret, TCGv arg1,
2488 target_long val)
76db3ba4
AJ
2489{
2490 tcg_gen_addi_tl(ret, arg1, val);
c791fe84 2491 if (NARROW_MODE(ctx)) {
76db3ba4
AJ
2492 tcg_gen_ext32u_tl(ret, ret);
2493 }
76a66253
JM
2494}
2495
636aa200 2496static inline void gen_check_align(DisasContext *ctx, TCGv EA, int mask)
cf360a32
AJ
2497{
2498 int l1 = gen_new_label();
2499 TCGv t0 = tcg_temp_new();
2500 TCGv_i32 t1, t2;
2501 /* NIP cannot be restored if the memory exception comes from an helper */
2502 gen_update_nip(ctx, ctx->nip - 4);
2503 tcg_gen_andi_tl(t0, EA, mask);
2504 tcg_gen_brcondi_tl(TCG_COND_EQ, t0, 0, l1);
2505 t1 = tcg_const_i32(POWERPC_EXCP_ALIGN);
2506 t2 = tcg_const_i32(0);
e5f17ac6 2507 gen_helper_raise_exception_err(cpu_env, t1, t2);
cf360a32
AJ
2508 tcg_temp_free_i32(t1);
2509 tcg_temp_free_i32(t2);
2510 gen_set_label(l1);
2511 tcg_temp_free(t0);
2512}
2513
7863667f 2514/*** Integer load ***/
636aa200 2515static inline void gen_qemu_ld8u(DisasContext *ctx, TCGv arg1, TCGv arg2)
76db3ba4
AJ
2516{
2517 tcg_gen_qemu_ld8u(arg1, arg2, ctx->mem_idx);
2518}
2519
636aa200 2520static inline void gen_qemu_ld8s(DisasContext *ctx, TCGv arg1, TCGv arg2)
76db3ba4
AJ
2521{
2522 tcg_gen_qemu_ld8s(arg1, arg2, ctx->mem_idx);
2523}
2524
636aa200 2525static inline void gen_qemu_ld16u(DisasContext *ctx, TCGv arg1, TCGv arg2)
76db3ba4
AJ
2526{
2527 tcg_gen_qemu_ld16u(arg1, arg2, ctx->mem_idx);
2528 if (unlikely(ctx->le_mode)) {
fa3966a3 2529 tcg_gen_bswap16_tl(arg1, arg1);
76db3ba4 2530 }
b61f2753
AJ
2531}
2532
636aa200 2533static inline void gen_qemu_ld16s(DisasContext *ctx, TCGv arg1, TCGv arg2)
b61f2753 2534{
76db3ba4 2535 if (unlikely(ctx->le_mode)) {
76db3ba4 2536 tcg_gen_qemu_ld16u(arg1, arg2, ctx->mem_idx);
fa3966a3 2537 tcg_gen_bswap16_tl(arg1, arg1);
76db3ba4 2538 tcg_gen_ext16s_tl(arg1, arg1);
76db3ba4
AJ
2539 } else {
2540 tcg_gen_qemu_ld16s(arg1, arg2, ctx->mem_idx);
2541 }
b61f2753
AJ
2542}
2543
636aa200 2544static inline void gen_qemu_ld32u(DisasContext *ctx, TCGv arg1, TCGv arg2)
b61f2753 2545{
76db3ba4
AJ
2546 tcg_gen_qemu_ld32u(arg1, arg2, ctx->mem_idx);
2547 if (unlikely(ctx->le_mode)) {
fa3966a3 2548 tcg_gen_bswap32_tl(arg1, arg1);
76db3ba4 2549 }
b61f2753
AJ
2550}
2551
636aa200 2552static inline void gen_qemu_ld32s(DisasContext *ctx, TCGv arg1, TCGv arg2)
b61f2753 2553{
a457e7ee 2554 if (unlikely(ctx->le_mode)) {
76db3ba4 2555 tcg_gen_qemu_ld32u(arg1, arg2, ctx->mem_idx);
fa3966a3
AJ
2556 tcg_gen_bswap32_tl(arg1, arg1);
2557 tcg_gen_ext32s_tl(arg1, arg1);
b61f2753 2558 } else
76db3ba4 2559 tcg_gen_qemu_ld32s(arg1, arg2, ctx->mem_idx);
b61f2753
AJ
2560}
2561
636aa200 2562static inline void gen_qemu_ld64(DisasContext *ctx, TCGv_i64 arg1, TCGv arg2)
b61f2753 2563{
76db3ba4
AJ
2564 tcg_gen_qemu_ld64(arg1, arg2, ctx->mem_idx);
2565 if (unlikely(ctx->le_mode)) {
66896cb8 2566 tcg_gen_bswap64_i64(arg1, arg1);
76db3ba4 2567 }
b61f2753
AJ
2568}
2569
636aa200 2570static inline void gen_qemu_st8(DisasContext *ctx, TCGv arg1, TCGv arg2)
b61f2753 2571{
76db3ba4 2572 tcg_gen_qemu_st8(arg1, arg2, ctx->mem_idx);
b61f2753
AJ
2573}
2574
636aa200 2575static inline void gen_qemu_st16(DisasContext *ctx, TCGv arg1, TCGv arg2)
b61f2753 2576{
76db3ba4 2577 if (unlikely(ctx->le_mode)) {
76db3ba4
AJ
2578 TCGv t0 = tcg_temp_new();
2579 tcg_gen_ext16u_tl(t0, arg1);
fa3966a3 2580 tcg_gen_bswap16_tl(t0, t0);
76db3ba4
AJ
2581 tcg_gen_qemu_st16(t0, arg2, ctx->mem_idx);
2582 tcg_temp_free(t0);
76db3ba4
AJ
2583 } else {
2584 tcg_gen_qemu_st16(arg1, arg2, ctx->mem_idx);
2585 }
b61f2753
AJ
2586}
2587
636aa200 2588static inline void gen_qemu_st32(DisasContext *ctx, TCGv arg1, TCGv arg2)
b61f2753 2589{
76db3ba4 2590 if (unlikely(ctx->le_mode)) {
fa3966a3
AJ
2591 TCGv t0 = tcg_temp_new();
2592 tcg_gen_ext32u_tl(t0, arg1);
2593 tcg_gen_bswap32_tl(t0, t0);
76db3ba4
AJ
2594 tcg_gen_qemu_st32(t0, arg2, ctx->mem_idx);
2595 tcg_temp_free(t0);
76db3ba4
AJ
2596 } else {
2597 tcg_gen_qemu_st32(arg1, arg2, ctx->mem_idx);
2598 }
b61f2753
AJ
2599}
2600
636aa200 2601static inline void gen_qemu_st64(DisasContext *ctx, TCGv_i64 arg1, TCGv arg2)
b61f2753 2602{
76db3ba4 2603 if (unlikely(ctx->le_mode)) {
a7812ae4 2604 TCGv_i64 t0 = tcg_temp_new_i64();
66896cb8 2605 tcg_gen_bswap64_i64(t0, arg1);
76db3ba4 2606 tcg_gen_qemu_st64(t0, arg2, ctx->mem_idx);
a7812ae4 2607 tcg_temp_free_i64(t0);
b61f2753 2608 } else
76db3ba4 2609 tcg_gen_qemu_st64(arg1, arg2, ctx->mem_idx);
b61f2753
AJ
2610}
2611
0c8aacd4 2612#define GEN_LD(name, ldop, opc, type) \
99e300ef 2613static void glue(gen_, name)(DisasContext *ctx) \
79aceca5 2614{ \
76db3ba4
AJ
2615 TCGv EA; \
2616 gen_set_access_type(ctx, ACCESS_INT); \
2617 EA = tcg_temp_new(); \
2618 gen_addr_imm_index(ctx, EA, 0); \
2619 gen_qemu_##ldop(ctx, cpu_gpr[rD(ctx->opcode)], EA); \
b61f2753 2620 tcg_temp_free(EA); \
79aceca5
FB
2621}
2622
0c8aacd4 2623#define GEN_LDU(name, ldop, opc, type) \
99e300ef 2624static void glue(gen_, name##u)(DisasContext *ctx) \
79aceca5 2625{ \
b61f2753 2626 TCGv EA; \
76a66253
JM
2627 if (unlikely(rA(ctx->opcode) == 0 || \
2628 rA(ctx->opcode) == rD(ctx->opcode))) { \
e06fcd75 2629 gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL); \
9fddaa0c 2630 return; \
9a64fbe4 2631 } \
76db3ba4 2632 gen_set_access_type(ctx, ACCESS_INT); \
0c8aacd4 2633 EA = tcg_temp_new(); \
9d53c753 2634 if (type == PPC_64B) \
76db3ba4 2635 gen_addr_imm_index(ctx, EA, 0x03); \
9d53c753 2636 else \
76db3ba4
AJ
2637 gen_addr_imm_index(ctx, EA, 0); \
2638 gen_qemu_##ldop(ctx, cpu_gpr[rD(ctx->opcode)], EA); \
b61f2753
AJ
2639 tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], EA); \
2640 tcg_temp_free(EA); \
79aceca5
FB
2641}
2642
0c8aacd4 2643#define GEN_LDUX(name, ldop, opc2, opc3, type) \
99e300ef 2644static void glue(gen_, name##ux)(DisasContext *ctx) \
79aceca5 2645{ \
b61f2753 2646 TCGv EA; \
76a66253
JM
2647 if (unlikely(rA(ctx->opcode) == 0 || \
2648 rA(ctx->opcode) == rD(ctx->opcode))) { \
e06fcd75 2649 gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL); \
9fddaa0c 2650 return; \
9a64fbe4 2651 } \
76db3ba4 2652 gen_set_access_type(ctx, ACCESS_INT); \
0c8aacd4 2653 EA = tcg_temp_new(); \
76db3ba4
AJ
2654 gen_addr_reg_index(ctx, EA); \
2655 gen_qemu_##ldop(ctx, cpu_gpr[rD(ctx->opcode)], EA); \
b61f2753
AJ
2656 tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], EA); \
2657 tcg_temp_free(EA); \
79aceca5
FB
2658}
2659
cd6e9320 2660#define GEN_LDX_E(name, ldop, opc2, opc3, type, type2) \
99e300ef 2661static void glue(gen_, name##x)(DisasContext *ctx) \
79aceca5 2662{ \
76db3ba4
AJ
2663 TCGv EA; \
2664 gen_set_access_type(ctx, ACCESS_INT); \
2665 EA = tcg_temp_new(); \
2666 gen_addr_reg_index(ctx, EA); \
2667 gen_qemu_##ldop(ctx, cpu_gpr[rD(ctx->opcode)], EA); \
b61f2753 2668 tcg_temp_free(EA); \
79aceca5 2669}
cd6e9320
TH
2670#define GEN_LDX(name, ldop, opc2, opc3, type) \
2671 GEN_LDX_E(name, ldop, opc2, opc3, type, PPC_NONE)
79aceca5 2672
0c8aacd4
AJ
2673#define GEN_LDS(name, ldop, op, type) \
2674GEN_LD(name, ldop, op | 0x20, type); \
2675GEN_LDU(name, ldop, op | 0x21, type); \
2676GEN_LDUX(name, ldop, 0x17, op | 0x01, type); \
2677GEN_LDX(name, ldop, 0x17, op | 0x00, type)
79aceca5
FB
2678
2679/* lbz lbzu lbzux lbzx */
0c8aacd4 2680GEN_LDS(lbz, ld8u, 0x02, PPC_INTEGER);
79aceca5 2681/* lha lhau lhaux lhax */
0c8aacd4 2682GEN_LDS(lha, ld16s, 0x0A, PPC_INTEGER);
79aceca5 2683/* lhz lhzu lhzux lhzx */
0c8aacd4 2684GEN_LDS(lhz, ld16u, 0x08, PPC_INTEGER);
79aceca5 2685/* lwz lwzu lwzux lwzx */
0c8aacd4 2686GEN_LDS(lwz, ld32u, 0x00, PPC_INTEGER);
d9bce9d9 2687#if defined(TARGET_PPC64)
d9bce9d9 2688/* lwaux */
0c8aacd4 2689GEN_LDUX(lwa, ld32s, 0x15, 0x0B, PPC_64B);
d9bce9d9 2690/* lwax */
0c8aacd4 2691GEN_LDX(lwa, ld32s, 0x15, 0x0A, PPC_64B);
d9bce9d9 2692/* ldux */
0c8aacd4 2693GEN_LDUX(ld, ld64, 0x15, 0x01, PPC_64B);
d9bce9d9 2694/* ldx */
0c8aacd4 2695GEN_LDX(ld, ld64, 0x15, 0x00, PPC_64B);
99e300ef
BS
2696
2697static void gen_ld(DisasContext *ctx)
d9bce9d9 2698{
b61f2753 2699 TCGv EA;
d9bce9d9
JM
2700 if (Rc(ctx->opcode)) {
2701 if (unlikely(rA(ctx->opcode) == 0 ||
2702 rA(ctx->opcode) == rD(ctx->opcode))) {
e06fcd75 2703 gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL);
d9bce9d9
JM
2704 return;
2705 }
2706 }
76db3ba4 2707 gen_set_access_type(ctx, ACCESS_INT);
a7812ae4 2708 EA = tcg_temp_new();
76db3ba4 2709 gen_addr_imm_index(ctx, EA, 0x03);
d9bce9d9
JM
2710 if (ctx->opcode & 0x02) {
2711 /* lwa (lwau is undefined) */
76db3ba4 2712 gen_qemu_ld32s(ctx, cpu_gpr[rD(ctx->opcode)], EA);
d9bce9d9
JM
2713 } else {
2714 /* ld - ldu */
76db3ba4 2715 gen_qemu_ld64(ctx, cpu_gpr[rD(ctx->opcode)], EA);
d9bce9d9 2716 }
d9bce9d9 2717 if (Rc(ctx->opcode))
b61f2753
AJ
2718 tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], EA);
2719 tcg_temp_free(EA);
d9bce9d9 2720}
99e300ef 2721
54623277 2722/* lq */
99e300ef 2723static void gen_lq(DisasContext *ctx)
be147d08
JM
2724{
2725#if defined(CONFIG_USER_ONLY)
e06fcd75 2726 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
be147d08
JM
2727#else
2728 int ra, rd;
b61f2753 2729 TCGv EA;
be147d08
JM
2730
2731 /* Restore CPU state */
76db3ba4 2732 if (unlikely(ctx->mem_idx == 0)) {
e06fcd75 2733 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
be147d08
JM
2734 return;
2735 }
2736 ra = rA(ctx->opcode);
2737 rd = rD(ctx->opcode);
2738 if (unlikely((rd & 1) || rd == ra)) {
e06fcd75 2739 gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL);
be147d08
JM
2740 return;
2741 }
76db3ba4 2742 if (unlikely(ctx->le_mode)) {
be147d08 2743 /* Little-endian mode is not handled */
e06fcd75 2744 gen_exception_err(ctx, POWERPC_EXCP_ALIGN, POWERPC_EXCP_ALIGN_LE);
be147d08
JM
2745 return;
2746 }
76db3ba4 2747 gen_set_access_type(ctx, ACCESS_INT);
a7812ae4 2748 EA = tcg_temp_new();
76db3ba4
AJ
2749 gen_addr_imm_index(ctx, EA, 0x0F);
2750 gen_qemu_ld64(ctx, cpu_gpr[rd], EA);
2751 gen_addr_add(ctx, EA, EA, 8);
2752 gen_qemu_ld64(ctx, cpu_gpr[rd+1], EA);
b61f2753 2753 tcg_temp_free(EA);
be147d08
JM
2754#endif
2755}
d9bce9d9 2756#endif
79aceca5
FB
2757
2758/*** Integer store ***/
0c8aacd4 2759#define GEN_ST(name, stop, opc, type) \
99e300ef 2760static void glue(gen_, name)(DisasContext *ctx) \
79aceca5 2761{ \
76db3ba4
AJ
2762 TCGv EA; \
2763 gen_set_access_type(ctx, ACCESS_INT); \
2764 EA = tcg_temp_new(); \
2765 gen_addr_imm_index(ctx, EA, 0); \
2766 gen_qemu_##stop(ctx, cpu_gpr[rS(ctx->opcode)], EA); \
b61f2753 2767 tcg_temp_free(EA); \
79aceca5
FB
2768}
2769
0c8aacd4 2770#define GEN_STU(name, stop, opc, type) \
99e300ef 2771static void glue(gen_, stop##u)(DisasContext *ctx) \
79aceca5 2772{ \
b61f2753 2773 TCGv EA; \
76a66253 2774 if (unlikely(rA(ctx->opcode) == 0)) { \
e06fcd75 2775 gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL); \
9fddaa0c 2776 return; \
9a64fbe4 2777 } \
76db3ba4 2778 gen_set_access_type(ctx, ACCESS_INT); \
0c8aacd4 2779 EA = tcg_temp_new(); \
9d53c753 2780 if (type == PPC_64B) \
76db3ba4 2781 gen_addr_imm_index(ctx, EA, 0x03); \
9d53c753 2782 else \
76db3ba4
AJ
2783 gen_addr_imm_index(ctx, EA, 0); \
2784 gen_qemu_##stop(ctx, cpu_gpr[rS(ctx->opcode)], EA); \
b61f2753
AJ
2785 tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], EA); \
2786 tcg_temp_free(EA); \
79aceca5
FB
2787}
2788
0c8aacd4 2789#define GEN_STUX(name, stop, opc2, opc3, type) \
99e300ef 2790static void glue(gen_, name##ux)(DisasContext *ctx) \
79aceca5 2791{ \
b61f2753 2792 TCGv EA; \
76a66253 2793 if (unlikely(rA(ctx->opcode) == 0)) { \
e06fcd75 2794 gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL); \
9fddaa0c 2795 return; \
9a64fbe4 2796 } \
76db3ba4 2797 gen_set_access_type(ctx, ACCESS_INT); \
0c8aacd4 2798 EA = tcg_temp_new(); \
76db3ba4
AJ
2799 gen_addr_reg_index(ctx, EA); \
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
cd6e9320
TH
2805#define GEN_STX_E(name, stop, opc2, opc3, type, type2) \
2806static void glue(gen_, name##x)(DisasContext *ctx) \
79aceca5 2807{ \
76db3ba4
AJ
2808 TCGv EA; \
2809 gen_set_access_type(ctx, ACCESS_INT); \
2810 EA = tcg_temp_new(); \
2811 gen_addr_reg_index(ctx, EA); \
2812 gen_qemu_##stop(ctx, cpu_gpr[rS(ctx->opcode)], EA); \
b61f2753 2813 tcg_temp_free(EA); \
79aceca5 2814}
cd6e9320
TH
2815#define GEN_STX(name, stop, opc2, opc3, type) \
2816 GEN_STX_E(name, stop, opc2, opc3, type, PPC_NONE)
79aceca5 2817
0c8aacd4
AJ
2818#define GEN_STS(name, stop, op, type) \
2819GEN_ST(name, stop, op | 0x20, type); \
2820GEN_STU(name, stop, op | 0x21, type); \
2821GEN_STUX(name, stop, 0x17, op | 0x01, type); \
2822GEN_STX(name, stop, 0x17, op | 0x00, type)
79aceca5
FB
2823
2824/* stb stbu stbux stbx */
0c8aacd4 2825GEN_STS(stb, st8, 0x06, PPC_INTEGER);
79aceca5 2826/* sth sthu sthux sthx */
0c8aacd4 2827GEN_STS(sth, st16, 0x0C, PPC_INTEGER);
79aceca5 2828/* stw stwu stwux stwx */
0c8aacd4 2829GEN_STS(stw, st32, 0x04, PPC_INTEGER);
d9bce9d9 2830#if defined(TARGET_PPC64)
0c8aacd4
AJ
2831GEN_STUX(std, st64, 0x15, 0x05, PPC_64B);
2832GEN_STX(std, st64, 0x15, 0x04, PPC_64B);
99e300ef
BS
2833
2834static void gen_std(DisasContext *ctx)
d9bce9d9 2835{
be147d08 2836 int rs;
b61f2753 2837 TCGv EA;
be147d08
JM
2838
2839 rs = rS(ctx->opcode);
2840 if ((ctx->opcode & 0x3) == 0x2) {
2841#if defined(CONFIG_USER_ONLY)
e06fcd75 2842 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
be147d08
JM
2843#else
2844 /* stq */
76db3ba4 2845 if (unlikely(ctx->mem_idx == 0)) {
e06fcd75 2846 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
be147d08
JM
2847 return;
2848 }
2849 if (unlikely(rs & 1)) {
e06fcd75 2850 gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL);
d9bce9d9
JM
2851 return;
2852 }
76db3ba4 2853 if (unlikely(ctx->le_mode)) {
be147d08 2854 /* Little-endian mode is not handled */
e06fcd75 2855 gen_exception_err(ctx, POWERPC_EXCP_ALIGN, POWERPC_EXCP_ALIGN_LE);
be147d08
JM
2856 return;
2857 }
76db3ba4 2858 gen_set_access_type(ctx, ACCESS_INT);
a7812ae4 2859 EA = tcg_temp_new();
76db3ba4
AJ
2860 gen_addr_imm_index(ctx, EA, 0x03);
2861 gen_qemu_st64(ctx, cpu_gpr[rs], EA);
2862 gen_addr_add(ctx, EA, EA, 8);
2863 gen_qemu_st64(ctx, cpu_gpr[rs+1], EA);
b61f2753 2864 tcg_temp_free(EA);
be147d08
JM
2865#endif
2866 } else {
2867 /* std / stdu */
2868 if (Rc(ctx->opcode)) {
2869 if (unlikely(rA(ctx->opcode) == 0)) {
e06fcd75 2870 gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL);
be147d08
JM
2871 return;
2872 }
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);
be147d08 2878 if (Rc(ctx->opcode))
b61f2753
AJ
2879 tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], EA);
2880 tcg_temp_free(EA);
d9bce9d9 2881 }
d9bce9d9
JM
2882}
2883#endif
79aceca5
FB
2884/*** Integer load and store with byte reverse ***/
2885/* lhbrx */
86178a57 2886static inline void gen_qemu_ld16ur(DisasContext *ctx, TCGv arg1, TCGv arg2)
b61f2753 2887{
76db3ba4
AJ
2888 tcg_gen_qemu_ld16u(arg1, arg2, ctx->mem_idx);
2889 if (likely(!ctx->le_mode)) {
fa3966a3 2890 tcg_gen_bswap16_tl(arg1, arg1);
76db3ba4 2891 }
b61f2753 2892}
0c8aacd4 2893GEN_LDX(lhbr, ld16ur, 0x16, 0x18, PPC_INTEGER);
b61f2753 2894
79aceca5 2895/* lwbrx */
86178a57 2896static inline void gen_qemu_ld32ur(DisasContext *ctx, TCGv arg1, TCGv arg2)
b61f2753 2897{
76db3ba4
AJ
2898 tcg_gen_qemu_ld32u(arg1, arg2, ctx->mem_idx);
2899 if (likely(!ctx->le_mode)) {
fa3966a3 2900 tcg_gen_bswap32_tl(arg1, arg1);
76db3ba4 2901 }
b61f2753 2902}
0c8aacd4 2903GEN_LDX(lwbr, ld32ur, 0x16, 0x10, PPC_INTEGER);
b61f2753 2904
cd6e9320
TH
2905#if defined(TARGET_PPC64)
2906/* ldbrx */
2907static inline void gen_qemu_ld64ur(DisasContext *ctx, TCGv arg1, TCGv arg2)
2908{
2909 tcg_gen_qemu_ld64(arg1, arg2, ctx->mem_idx);
2910 if (likely(!ctx->le_mode)) {
2911 tcg_gen_bswap64_tl(arg1, arg1);
2912 }
2913}
2914GEN_LDX_E(ldbr, ld64ur, 0x14, 0x10, PPC_NONE, PPC2_DBRX);
2915#endif /* TARGET_PPC64 */
2916
79aceca5 2917/* sthbrx */
86178a57 2918static inline void gen_qemu_st16r(DisasContext *ctx, TCGv arg1, TCGv arg2)
b61f2753 2919{
76db3ba4 2920 if (likely(!ctx->le_mode)) {
76db3ba4
AJ
2921 TCGv t0 = tcg_temp_new();
2922 tcg_gen_ext16u_tl(t0, arg1);
fa3966a3 2923 tcg_gen_bswap16_tl(t0, t0);
76db3ba4
AJ
2924 tcg_gen_qemu_st16(t0, arg2, ctx->mem_idx);
2925 tcg_temp_free(t0);
76db3ba4
AJ
2926 } else {
2927 tcg_gen_qemu_st16(arg1, arg2, ctx->mem_idx);
2928 }
b61f2753 2929}
0c8aacd4 2930GEN_STX(sthbr, st16r, 0x16, 0x1C, PPC_INTEGER);
b61f2753 2931
79aceca5 2932/* stwbrx */
86178a57 2933static inline void gen_qemu_st32r(DisasContext *ctx, TCGv arg1, TCGv arg2)
b61f2753 2934{
76db3ba4 2935 if (likely(!ctx->le_mode)) {
fa3966a3
AJ
2936 TCGv t0 = tcg_temp_new();
2937 tcg_gen_ext32u_tl(t0, arg1);
2938 tcg_gen_bswap32_tl(t0, t0);
76db3ba4
AJ
2939 tcg_gen_qemu_st32(t0, arg2, ctx->mem_idx);
2940 tcg_temp_free(t0);
76db3ba4
AJ
2941 } else {
2942 tcg_gen_qemu_st32(arg1, arg2, ctx->mem_idx);
2943 }
b61f2753 2944}
0c8aacd4 2945GEN_STX(stwbr, st32r, 0x16, 0x14, PPC_INTEGER);
79aceca5 2946
cd6e9320
TH
2947#if defined(TARGET_PPC64)
2948/* stdbrx */
2949static inline void gen_qemu_st64r(DisasContext *ctx, TCGv arg1, TCGv arg2)
2950{
2951 if (likely(!ctx->le_mode)) {
2952 TCGv t0 = tcg_temp_new();
2953 tcg_gen_bswap64_tl(t0, arg1);
2954 tcg_gen_qemu_st64(t0, arg2, ctx->mem_idx);
2955 tcg_temp_free(t0);
2956 } else {
2957 tcg_gen_qemu_st64(arg1, arg2, ctx->mem_idx);
2958 }
2959}
2960GEN_STX_E(stdbr, st64r, 0x14, 0x14, PPC_NONE, PPC2_DBRX);
2961#endif /* TARGET_PPC64 */
2962
79aceca5 2963/*** Integer load and store multiple ***/
99e300ef 2964
54623277 2965/* lmw */
99e300ef 2966static void gen_lmw(DisasContext *ctx)
79aceca5 2967{
76db3ba4
AJ
2968 TCGv t0;
2969 TCGv_i32 t1;
2970 gen_set_access_type(ctx, ACCESS_INT);
76a66253 2971 /* NIP cannot be restored if the memory exception comes from an helper */
d9bce9d9 2972 gen_update_nip(ctx, ctx->nip - 4);
76db3ba4
AJ
2973 t0 = tcg_temp_new();
2974 t1 = tcg_const_i32(rD(ctx->opcode));
2975 gen_addr_imm_index(ctx, t0, 0);
2f5a189c 2976 gen_helper_lmw(cpu_env, t0, t1);
ff4a62cd
AJ
2977 tcg_temp_free(t0);
2978 tcg_temp_free_i32(t1);
79aceca5
FB
2979}
2980
2981/* stmw */
99e300ef 2982static void gen_stmw(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(rS(ctx->opcode));
2991 gen_addr_imm_index(ctx, t0, 0);
2f5a189c 2992 gen_helper_stmw(cpu_env, t0, t1);
ff4a62cd
AJ
2993 tcg_temp_free(t0);
2994 tcg_temp_free_i32(t1);
79aceca5
FB
2995}
2996
2997/*** Integer load and store strings ***/
54623277 2998
79aceca5 2999/* lswi */
3fc6c082 3000/* PowerPC32 specification says we must generate an exception if
9a64fbe4
FB
3001 * rA is in the range of registers to be loaded.
3002 * In an other hand, IBM says this is valid, but rA won't be loaded.
3003 * For now, I'll follow the spec...
3004 */
99e300ef 3005static void gen_lswi(DisasContext *ctx)
79aceca5 3006{
dfbc799d
AJ
3007 TCGv t0;
3008 TCGv_i32 t1, t2;
79aceca5
FB
3009 int nb = NB(ctx->opcode);
3010 int start = rD(ctx->opcode);
9a64fbe4 3011 int ra = rA(ctx->opcode);
79aceca5
FB
3012 int nr;
3013
3014 if (nb == 0)
3015 nb = 32;
3016 nr = nb / 4;
76a66253
JM
3017 if (unlikely(((start + nr) > 32 &&
3018 start <= ra && (start + nr - 32) > ra) ||
3019 ((start + nr) <= 32 && start <= ra && (start + nr) > ra))) {
e06fcd75 3020 gen_inval_exception(ctx, POWERPC_EXCP_INVAL_LSWX);
9fddaa0c 3021 return;
297d8e62 3022 }
76db3ba4 3023 gen_set_access_type(ctx, ACCESS_INT);
8dd4983c 3024 /* NIP cannot be restored if the memory exception comes from an helper */
d9bce9d9 3025 gen_update_nip(ctx, ctx->nip - 4);
dfbc799d 3026 t0 = tcg_temp_new();
76db3ba4 3027 gen_addr_register(ctx, t0);
dfbc799d
AJ
3028 t1 = tcg_const_i32(nb);
3029 t2 = tcg_const_i32(start);
2f5a189c 3030 gen_helper_lsw(cpu_env, t0, t1, t2);
dfbc799d
AJ
3031 tcg_temp_free(t0);
3032 tcg_temp_free_i32(t1);
3033 tcg_temp_free_i32(t2);
79aceca5
FB
3034}
3035
3036/* lswx */
99e300ef 3037static void gen_lswx(DisasContext *ctx)
79aceca5 3038{
76db3ba4
AJ
3039 TCGv t0;
3040 TCGv_i32 t1, t2, t3;
3041 gen_set_access_type(ctx, ACCESS_INT);
76a66253 3042 /* NIP cannot be restored if the memory exception comes from an helper */
d9bce9d9 3043 gen_update_nip(ctx, ctx->nip - 4);
76db3ba4
AJ
3044 t0 = tcg_temp_new();
3045 gen_addr_reg_index(ctx, t0);
3046 t1 = tcg_const_i32(rD(ctx->opcode));
3047 t2 = tcg_const_i32(rA(ctx->opcode));
3048 t3 = tcg_const_i32(rB(ctx->opcode));
2f5a189c 3049 gen_helper_lswx(cpu_env, t0, t1, t2, t3);
dfbc799d
AJ
3050 tcg_temp_free(t0);
3051 tcg_temp_free_i32(t1);
3052 tcg_temp_free_i32(t2);
3053 tcg_temp_free_i32(t3);
79aceca5
FB
3054}
3055
3056/* stswi */
99e300ef 3057static void gen_stswi(DisasContext *ctx)
79aceca5 3058{
76db3ba4
AJ
3059 TCGv t0;
3060 TCGv_i32 t1, t2;
4b3686fa 3061 int nb = NB(ctx->opcode);
76db3ba4 3062 gen_set_access_type(ctx, ACCESS_INT);
76a66253 3063 /* NIP cannot be restored if the memory exception comes from an helper */
d9bce9d9 3064 gen_update_nip(ctx, ctx->nip - 4);
76db3ba4
AJ
3065 t0 = tcg_temp_new();
3066 gen_addr_register(ctx, t0);
4b3686fa
FB
3067 if (nb == 0)
3068 nb = 32;
dfbc799d 3069 t1 = tcg_const_i32(nb);
76db3ba4 3070 t2 = tcg_const_i32(rS(ctx->opcode));
2f5a189c 3071 gen_helper_stsw(cpu_env, t0, t1, t2);
dfbc799d
AJ
3072 tcg_temp_free(t0);
3073 tcg_temp_free_i32(t1);
3074 tcg_temp_free_i32(t2);
79aceca5
FB
3075}
3076
3077/* stswx */
99e300ef 3078static void gen_stswx(DisasContext *ctx)
79aceca5 3079{
76db3ba4
AJ
3080 TCGv t0;
3081 TCGv_i32 t1, t2;
3082 gen_set_access_type(ctx, ACCESS_INT);
8dd4983c 3083 /* NIP cannot be restored if the memory exception comes from an helper */
5fafdf24 3084 gen_update_nip(ctx, ctx->nip - 4);
76db3ba4
AJ
3085 t0 = tcg_temp_new();
3086 gen_addr_reg_index(ctx, t0);
3087 t1 = tcg_temp_new_i32();
dfbc799d
AJ
3088 tcg_gen_trunc_tl_i32(t1, cpu_xer);
3089 tcg_gen_andi_i32(t1, t1, 0x7F);
76db3ba4 3090 t2 = tcg_const_i32(rS(ctx->opcode));
2f5a189c 3091 gen_helper_stsw(cpu_env, t0, t1, t2);
dfbc799d
AJ
3092 tcg_temp_free(t0);
3093 tcg_temp_free_i32(t1);
3094 tcg_temp_free_i32(t2);
79aceca5
FB
3095}
3096
3097/*** Memory synchronisation ***/
3098/* eieio */
99e300ef 3099static void gen_eieio(DisasContext *ctx)
79aceca5 3100{
79aceca5
FB
3101}
3102
3103/* isync */
99e300ef 3104static void gen_isync(DisasContext *ctx)
79aceca5 3105{
e06fcd75 3106 gen_stop_exception(ctx);
79aceca5
FB
3107}
3108
111bfab3 3109/* lwarx */
99e300ef 3110static void gen_lwarx(DisasContext *ctx)
79aceca5 3111{
76db3ba4 3112 TCGv t0;
18b21a2f 3113 TCGv gpr = cpu_gpr[rD(ctx->opcode)];
76db3ba4
AJ
3114 gen_set_access_type(ctx, ACCESS_RES);
3115 t0 = tcg_temp_local_new();
3116 gen_addr_reg_index(ctx, t0);
cf360a32 3117 gen_check_align(ctx, t0, 0x03);
18b21a2f 3118 gen_qemu_ld32u(ctx, gpr, t0);
cf360a32 3119 tcg_gen_mov_tl(cpu_reserve, t0);
1328c2bf 3120 tcg_gen_st_tl(gpr, cpu_env, offsetof(CPUPPCState, reserve_val));
cf360a32 3121 tcg_temp_free(t0);
79aceca5
FB
3122}
3123
4425265b
NF
3124#if defined(CONFIG_USER_ONLY)
3125static void gen_conditional_store (DisasContext *ctx, TCGv EA,
3126 int reg, int size)
3127{
3128 TCGv t0 = tcg_temp_new();
3129 uint32_t save_exception = ctx->exception;
3130
1328c2bf 3131 tcg_gen_st_tl(EA, cpu_env, offsetof(CPUPPCState, reserve_ea));
4425265b 3132 tcg_gen_movi_tl(t0, (size << 5) | reg);
1328c2bf 3133 tcg_gen_st_tl(t0, cpu_env, offsetof(CPUPPCState, reserve_info));
4425265b
NF
3134 tcg_temp_free(t0);
3135 gen_update_nip(ctx, ctx->nip-4);
3136 ctx->exception = POWERPC_EXCP_BRANCH;
3137 gen_exception(ctx, POWERPC_EXCP_STCX);
3138 ctx->exception = save_exception;
3139}
3140#endif
3141
79aceca5 3142/* stwcx. */
e8eaa2c0 3143static void gen_stwcx_(DisasContext *ctx)
79aceca5 3144{
76db3ba4
AJ
3145 TCGv t0;
3146 gen_set_access_type(ctx, ACCESS_RES);
3147 t0 = tcg_temp_local_new();
3148 gen_addr_reg_index(ctx, t0);
cf360a32 3149 gen_check_align(ctx, t0, 0x03);
4425265b
NF
3150#if defined(CONFIG_USER_ONLY)
3151 gen_conditional_store(ctx, t0, rS(ctx->opcode), 4);
3152#else
3153 {
3154 int l1;
3155
da91a00f 3156 tcg_gen_trunc_tl_i32(cpu_crf[0], cpu_so);
4425265b
NF
3157 l1 = gen_new_label();
3158 tcg_gen_brcond_tl(TCG_COND_NE, t0, cpu_reserve, l1);
3159 tcg_gen_ori_i32(cpu_crf[0], cpu_crf[0], 1 << CRF_EQ);
3160 gen_qemu_st32(ctx, cpu_gpr[rS(ctx->opcode)], t0);
3161 gen_set_label(l1);
3162 tcg_gen_movi_tl(cpu_reserve, -1);
3163 }
3164#endif
cf360a32 3165 tcg_temp_free(t0);
79aceca5
FB
3166}
3167
426613db 3168#if defined(TARGET_PPC64)
426613db 3169/* ldarx */
99e300ef 3170static void gen_ldarx(DisasContext *ctx)
426613db 3171{
76db3ba4 3172 TCGv t0;
18b21a2f 3173 TCGv gpr = cpu_gpr[rD(ctx->opcode)];
76db3ba4
AJ
3174 gen_set_access_type(ctx, ACCESS_RES);
3175 t0 = tcg_temp_local_new();
3176 gen_addr_reg_index(ctx, t0);
cf360a32 3177 gen_check_align(ctx, t0, 0x07);
18b21a2f 3178 gen_qemu_ld64(ctx, gpr, t0);
cf360a32 3179 tcg_gen_mov_tl(cpu_reserve, t0);
1328c2bf 3180 tcg_gen_st_tl(gpr, cpu_env, offsetof(CPUPPCState, reserve_val));
cf360a32 3181 tcg_temp_free(t0);
426613db
JM
3182}
3183
3184/* stdcx. */
e8eaa2c0 3185static void gen_stdcx_(DisasContext *ctx)
426613db 3186{
76db3ba4
AJ
3187 TCGv t0;
3188 gen_set_access_type(ctx, ACCESS_RES);
3189 t0 = tcg_temp_local_new();
3190 gen_addr_reg_index(ctx, t0);
cf360a32 3191 gen_check_align(ctx, t0, 0x07);
4425265b
NF
3192#if defined(CONFIG_USER_ONLY)
3193 gen_conditional_store(ctx, t0, rS(ctx->opcode), 8);
3194#else
3195 {
3196 int l1;
da91a00f 3197 tcg_gen_trunc_tl_i32(cpu_crf[0], cpu_so);
4425265b
NF
3198 l1 = gen_new_label();
3199 tcg_gen_brcond_tl(TCG_COND_NE, t0, cpu_reserve, l1);
3200 tcg_gen_ori_i32(cpu_crf[0], cpu_crf[0], 1 << CRF_EQ);
3201 gen_qemu_st64(ctx, cpu_gpr[rS(ctx->opcode)], t0);
3202 gen_set_label(l1);
3203 tcg_gen_movi_tl(cpu_reserve, -1);
3204 }
3205#endif
cf360a32 3206 tcg_temp_free(t0);
426613db
JM
3207}
3208#endif /* defined(TARGET_PPC64) */
3209
79aceca5 3210/* sync */
99e300ef 3211static void gen_sync(DisasContext *ctx)
79aceca5 3212{
79aceca5
FB
3213}
3214
0db1b20e 3215/* wait */
99e300ef 3216static void gen_wait(DisasContext *ctx)
0db1b20e 3217{
931ff272 3218 TCGv_i32 t0 = tcg_temp_new_i32();
259186a7
AF
3219 tcg_gen_st_i32(t0, cpu_env,
3220 -offsetof(PowerPCCPU, env) + offsetof(CPUState, halted));
931ff272 3221 tcg_temp_free_i32(t0);
0db1b20e 3222 /* Stop translation, as the CPU is supposed to sleep from now */
e06fcd75 3223 gen_exception_err(ctx, EXCP_HLT, 1);
0db1b20e
JM
3224}
3225
79aceca5 3226/*** Floating-point load ***/
a0d7d5a7 3227#define GEN_LDF(name, ldop, opc, type) \
99e300ef 3228static void glue(gen_, name)(DisasContext *ctx) \
79aceca5 3229{ \
a0d7d5a7 3230 TCGv EA; \
76a66253 3231 if (unlikely(!ctx->fpu_enabled)) { \
e06fcd75 3232 gen_exception(ctx, POWERPC_EXCP_FPU); \
4ecc3190
FB
3233 return; \
3234 } \
76db3ba4 3235 gen_set_access_type(ctx, ACCESS_FLOAT); \
a0d7d5a7 3236 EA = tcg_temp_new(); \
76db3ba4
AJ
3237 gen_addr_imm_index(ctx, EA, 0); \
3238 gen_qemu_##ldop(ctx, cpu_fpr[rD(ctx->opcode)], EA); \
a0d7d5a7 3239 tcg_temp_free(EA); \
79aceca5
FB
3240}
3241
a0d7d5a7 3242#define GEN_LDUF(name, ldop, opc, type) \
99e300ef 3243static void glue(gen_, name##u)(DisasContext *ctx) \
79aceca5 3244{ \
a0d7d5a7 3245 TCGv EA; \
76a66253 3246 if (unlikely(!ctx->fpu_enabled)) { \
e06fcd75 3247 gen_exception(ctx, POWERPC_EXCP_FPU); \
4ecc3190
FB
3248 return; \
3249 } \
76a66253 3250 if (unlikely(rA(ctx->opcode) == 0)) { \
e06fcd75 3251 gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL); \
9fddaa0c 3252 return; \
9a64fbe4 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
AJ
3258 tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], EA); \
3259 tcg_temp_free(EA); \
79aceca5
FB
3260}
3261
a0d7d5a7 3262#define GEN_LDUXF(name, ldop, opc, type) \
99e300ef 3263static void glue(gen_, name##ux)(DisasContext *ctx) \
79aceca5 3264{ \
a0d7d5a7 3265 TCGv EA; \
76a66253 3266 if (unlikely(!ctx->fpu_enabled)) { \
e06fcd75 3267 gen_exception(ctx, POWERPC_EXCP_FPU); \
4ecc3190
FB
3268 return; \
3269 } \
76a66253 3270 if (unlikely(rA(ctx->opcode) == 0)) { \
e06fcd75 3271 gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL); \
9fddaa0c 3272 return; \
9a64fbe4 3273 } \
76db3ba4 3274 gen_set_access_type(ctx, ACCESS_FLOAT); \
a0d7d5a7 3275 EA = tcg_temp_new(); \
76db3ba4
AJ
3276 gen_addr_reg_index(ctx, EA); \
3277 gen_qemu_##ldop(ctx, cpu_fpr[rD(ctx->opcode)], EA); \
a0d7d5a7
AJ
3278 tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], EA); \
3279 tcg_temp_free(EA); \
79aceca5
FB
3280}
3281
a0d7d5a7 3282#define GEN_LDXF(name, ldop, opc2, opc3, type) \
99e300ef 3283static void glue(gen_, name##x)(DisasContext *ctx) \
79aceca5 3284{ \
a0d7d5a7 3285 TCGv EA; \
76a66253 3286 if (unlikely(!ctx->fpu_enabled)) { \
e06fcd75 3287 gen_exception(ctx, POWERPC_EXCP_FPU); \
4ecc3190
FB
3288 return; \
3289 } \
76db3ba4 3290 gen_set_access_type(ctx, ACCESS_FLOAT); \
a0d7d5a7 3291 EA = tcg_temp_new(); \
76db3ba4
AJ
3292 gen_addr_reg_index(ctx, EA); \
3293 gen_qemu_##ldop(ctx, cpu_fpr[rD(ctx->opcode)], EA); \
a0d7d5a7 3294 tcg_temp_free(EA); \
79aceca5
FB
3295}
3296
a0d7d5a7
AJ
3297#define GEN_LDFS(name, ldop, op, type) \
3298GEN_LDF(name, ldop, op | 0x20, type); \
3299GEN_LDUF(name, ldop, op | 0x21, type); \
3300GEN_LDUXF(name, ldop, op | 0x01, type); \
3301GEN_LDXF(name, ldop, 0x17, op | 0x00, type)
3302
636aa200 3303static inline void gen_qemu_ld32fs(DisasContext *ctx, TCGv_i64 arg1, TCGv arg2)
a0d7d5a7
AJ
3304{
3305 TCGv t0 = tcg_temp_new();
3306 TCGv_i32 t1 = tcg_temp_new_i32();
76db3ba4 3307 gen_qemu_ld32u(ctx, t0, arg2);
a0d7d5a7
AJ
3308 tcg_gen_trunc_tl_i32(t1, t0);
3309 tcg_temp_free(t0);
8e703949 3310 gen_helper_float32_to_float64(arg1, cpu_env, t1);
a0d7d5a7
AJ
3311 tcg_temp_free_i32(t1);
3312}
79aceca5 3313
a0d7d5a7
AJ
3314 /* lfd lfdu lfdux lfdx */
3315GEN_LDFS(lfd, ld64, 0x12, PPC_FLOAT);
3316 /* lfs lfsu lfsux lfsx */
3317GEN_LDFS(lfs, ld32fs, 0x10, PPC_FLOAT);
79aceca5 3318
05050ee8
AJ
3319/* lfdp */
3320static void gen_lfdp(DisasContext *ctx)
3321{
3322 TCGv EA;
3323 if (unlikely(!ctx->fpu_enabled)) {
3324 gen_exception(ctx, POWERPC_EXCP_FPU);
3325 return;
3326 }
3327 gen_set_access_type(ctx, ACCESS_FLOAT);
3328 EA = tcg_temp_new();
3329 gen_addr_imm_index(ctx, EA, 0); \
3330 if (unlikely(ctx->le_mode)) {
3331 gen_qemu_ld64(ctx, cpu_fpr[rD(ctx->opcode) + 1], EA);
3332 tcg_gen_addi_tl(EA, EA, 8);
3333 gen_qemu_ld64(ctx, cpu_fpr[rD(ctx->opcode)], EA);
3334 } else {
3335 gen_qemu_ld64(ctx, cpu_fpr[rD(ctx->opcode)], EA);
3336 tcg_gen_addi_tl(EA, EA, 8);
3337 gen_qemu_ld64(ctx, cpu_fpr[rD(ctx->opcode) + 1], EA);
3338 }
3339 tcg_temp_free(EA);
3340}
3341
3342/* lfdpx */
3343static void gen_lfdpx(DisasContext *ctx)
3344{
3345 TCGv EA;
3346 if (unlikely(!ctx->fpu_enabled)) {
3347 gen_exception(ctx, POWERPC_EXCP_FPU);
3348 return;
3349 }
3350 gen_set_access_type(ctx, ACCESS_FLOAT);
3351 EA = tcg_temp_new();
3352 gen_addr_reg_index(ctx, EA);
3353 if (unlikely(ctx->le_mode)) {
3354 gen_qemu_ld64(ctx, cpu_fpr[rD(ctx->opcode) + 1], EA);
3355 tcg_gen_addi_tl(EA, EA, 8);
3356 gen_qemu_ld64(ctx, cpu_fpr[rD(ctx->opcode)], EA);
3357 } else {
3358 gen_qemu_ld64(ctx, cpu_fpr[rD(ctx->opcode)], EA);
3359 tcg_gen_addi_tl(EA, EA, 8);
3360 gen_qemu_ld64(ctx, cpu_fpr[rD(ctx->opcode) + 1], EA);
3361 }
3362 tcg_temp_free(EA);
3363}
3364
199f830d
AJ
3365/* lfiwax */
3366static void gen_lfiwax(DisasContext *ctx)
3367{
3368 TCGv EA;
3369 TCGv t0;
3370 if (unlikely(!ctx->fpu_enabled)) {
3371 gen_exception(ctx, POWERPC_EXCP_FPU);
3372 return;
3373 }
3374 gen_set_access_type(ctx, ACCESS_FLOAT);
3375 EA = tcg_temp_new();
3376 t0 = tcg_temp_new();
3377 gen_addr_reg_index(ctx, EA);
909eedb7 3378 gen_qemu_ld32s(ctx, t0, EA);
199f830d 3379 tcg_gen_ext_tl_i64(cpu_fpr[rD(ctx->opcode)], t0);
199f830d
AJ
3380 tcg_temp_free(EA);
3381 tcg_temp_free(t0);
3382}
3383
79aceca5 3384/*** Floating-point store ***/
a0d7d5a7 3385#define GEN_STF(name, stop, opc, type) \
99e300ef 3386static void glue(gen_, name)(DisasContext *ctx) \
79aceca5 3387{ \
a0d7d5a7 3388 TCGv EA; \
76a66253 3389 if (unlikely(!ctx->fpu_enabled)) { \
e06fcd75 3390 gen_exception(ctx, POWERPC_EXCP_FPU); \
4ecc3190
FB
3391 return; \
3392 } \
76db3ba4 3393 gen_set_access_type(ctx, ACCESS_FLOAT); \
a0d7d5a7 3394 EA = tcg_temp_new(); \
76db3ba4
AJ
3395 gen_addr_imm_index(ctx, EA, 0); \
3396 gen_qemu_##stop(ctx, cpu_fpr[rS(ctx->opcode)], EA); \
a0d7d5a7 3397 tcg_temp_free(EA); \
79aceca5
FB
3398}
3399
a0d7d5a7 3400#define GEN_STUF(name, stop, opc, type) \
99e300ef 3401static void glue(gen_, name##u)(DisasContext *ctx) \
79aceca5 3402{ \
a0d7d5a7 3403 TCGv EA; \
76a66253 3404 if (unlikely(!ctx->fpu_enabled)) { \
e06fcd75 3405 gen_exception(ctx, POWERPC_EXCP_FPU); \
4ecc3190
FB
3406 return; \
3407 } \
76a66253 3408 if (unlikely(rA(ctx->opcode) == 0)) { \
e06fcd75 3409 gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL); \
9fddaa0c 3410 return; \
9a64fbe4 3411 } \
76db3ba4 3412 gen_set_access_type(ctx, ACCESS_FLOAT); \
a0d7d5a7 3413 EA = tcg_temp_new(); \
76db3ba4
AJ
3414 gen_addr_imm_index(ctx, EA, 0); \
3415 gen_qemu_##stop(ctx, cpu_fpr[rS(ctx->opcode)], EA); \
a0d7d5a7
AJ
3416 tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], EA); \
3417 tcg_temp_free(EA); \
79aceca5
FB
3418}
3419
a0d7d5a7 3420#define GEN_STUXF(name, stop, opc, type) \
99e300ef 3421static void glue(gen_, name##ux)(DisasContext *ctx) \
79aceca5 3422{ \
a0d7d5a7 3423 TCGv EA; \
76a66253 3424 if (unlikely(!ctx->fpu_enabled)) { \
e06fcd75 3425 gen_exception(ctx, POWERPC_EXCP_FPU); \
4ecc3190
FB
3426 return; \
3427 } \
76a66253 3428 if (unlikely(rA(ctx->opcode) == 0)) { \
e06fcd75 3429 gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL); \
9fddaa0c 3430 return; \
9a64fbe4 3431 } \
76db3ba4 3432 gen_set_access_type(ctx, ACCESS_FLOAT); \
a0d7d5a7 3433 EA = tcg_temp_new(); \
76db3ba4
AJ
3434 gen_addr_reg_index(ctx, EA); \
3435 gen_qemu_##stop(ctx, cpu_fpr[rS(ctx->opcode)], EA); \
a0d7d5a7
AJ
3436 tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], EA); \
3437 tcg_temp_free(EA); \
79aceca5
FB
3438}
3439
a0d7d5a7 3440#define GEN_STXF(name, stop, opc2, opc3, type) \
99e300ef 3441static void glue(gen_, name##x)(DisasContext *ctx) \
79aceca5 3442{ \
a0d7d5a7 3443 TCGv EA; \
76a66253 3444 if (unlikely(!ctx->fpu_enabled)) { \
e06fcd75 3445 gen_exception(ctx, POWERPC_EXCP_FPU); \
4ecc3190
FB
3446 return; \
3447 } \
76db3ba4 3448 gen_set_access_type(ctx, ACCESS_FLOAT); \
a0d7d5a7 3449 EA = tcg_temp_new(); \
76db3ba4
AJ
3450 gen_addr_reg_index(ctx, EA); \
3451 gen_qemu_##stop(ctx, cpu_fpr[rS(ctx->opcode)], EA); \
a0d7d5a7 3452 tcg_temp_free(EA); \
79aceca5
FB
3453}
3454
a0d7d5a7
AJ
3455#define GEN_STFS(name, stop, op, type) \
3456GEN_STF(name, stop, op | 0x20, type); \
3457GEN_STUF(name, stop, op | 0x21, type); \
3458GEN_STUXF(name, stop, op | 0x01, type); \
3459GEN_STXF(name, stop, 0x17, op | 0x00, type)
3460
636aa200 3461static inline void gen_qemu_st32fs(DisasContext *ctx, TCGv_i64 arg1, TCGv arg2)
a0d7d5a7
AJ
3462{
3463 TCGv_i32 t0 = tcg_temp_new_i32();
3464 TCGv t1 = tcg_temp_new();
8e703949 3465 gen_helper_float64_to_float32(t0, cpu_env, arg1);
a0d7d5a7
AJ
3466 tcg_gen_extu_i32_tl(t1, t0);
3467 tcg_temp_free_i32(t0);
76db3ba4 3468 gen_qemu_st32(ctx, t1, arg2);
a0d7d5a7
AJ
3469 tcg_temp_free(t1);
3470}
79aceca5
FB
3471
3472/* stfd stfdu stfdux stfdx */
a0d7d5a7 3473GEN_STFS(stfd, st64, 0x16, PPC_FLOAT);
79aceca5 3474/* stfs stfsu stfsux stfsx */
a0d7d5a7 3475GEN_STFS(stfs, st32fs, 0x14, PPC_FLOAT);
79aceca5 3476
44bc0c4d
AJ
3477/* stfdp */
3478static void gen_stfdp(DisasContext *ctx)
3479{
3480 TCGv EA;
3481 if (unlikely(!ctx->fpu_enabled)) {
3482 gen_exception(ctx, POWERPC_EXCP_FPU);
3483 return;
3484 }
3485 gen_set_access_type(ctx, ACCESS_FLOAT);
3486 EA = tcg_temp_new();
3487 gen_addr_imm_index(ctx, EA, 0); \
3488 if (unlikely(ctx->le_mode)) {
3489 gen_qemu_st64(ctx, cpu_fpr[rD(ctx->opcode) + 1], EA);
3490 tcg_gen_addi_tl(EA, EA, 8);
3491 gen_qemu_st64(ctx, cpu_fpr[rD(ctx->opcode)], EA);
3492 } else {
3493 gen_qemu_st64(ctx, cpu_fpr[rD(ctx->opcode)], EA);
3494 tcg_gen_addi_tl(EA, EA, 8);
3495 gen_qemu_st64(ctx, cpu_fpr[rD(ctx->opcode) + 1], EA);
3496 }
3497 tcg_temp_free(EA);
3498}
3499
3500/* stfdpx */
3501static void gen_stfdpx(DisasContext *ctx)
3502{
3503 TCGv EA;
3504 if (unlikely(!ctx->fpu_enabled)) {
3505 gen_exception(ctx, POWERPC_EXCP_FPU);
3506 return;
3507 }
3508 gen_set_access_type(ctx, ACCESS_FLOAT);
3509 EA = tcg_temp_new();
3510 gen_addr_reg_index(ctx, EA);
3511 if (unlikely(ctx->le_mode)) {
3512 gen_qemu_st64(ctx, cpu_fpr[rD(ctx->opcode) + 1], EA);
3513 tcg_gen_addi_tl(EA, EA, 8);
3514 gen_qemu_st64(ctx, cpu_fpr[rD(ctx->opcode)], EA);
3515 } else {
3516 gen_qemu_st64(ctx, cpu_fpr[rD(ctx->opcode)], EA);
3517 tcg_gen_addi_tl(EA, EA, 8);
3518 gen_qemu_st64(ctx, cpu_fpr[rD(ctx->opcode) + 1], EA);
3519 }
3520 tcg_temp_free(EA);
3521}
3522
79aceca5 3523/* Optional: */
636aa200 3524static inline void gen_qemu_st32fiw(DisasContext *ctx, TCGv_i64 arg1, TCGv arg2)
a0d7d5a7
AJ
3525{
3526 TCGv t0 = tcg_temp_new();
3527 tcg_gen_trunc_i64_tl(t0, arg1),
76db3ba4 3528 gen_qemu_st32(ctx, t0, arg2);
a0d7d5a7
AJ
3529 tcg_temp_free(t0);
3530}
79aceca5 3531/* stfiwx */
a0d7d5a7 3532GEN_STXF(stfiw, st32fiw, 0x17, 0x1E, PPC_FLOAT_STFIWX);
79aceca5 3533
697ab892
DG
3534static inline void gen_update_cfar(DisasContext *ctx, target_ulong nip)
3535{
3536#if defined(TARGET_PPC64)
3537 if (ctx->has_cfar)
3538 tcg_gen_movi_tl(cpu_cfar, nip);
3539#endif
3540}
3541
79aceca5 3542/*** Branch ***/
636aa200 3543static inline void gen_goto_tb(DisasContext *ctx, int n, target_ulong dest)
c1942362
FB
3544{
3545 TranslationBlock *tb;
3546 tb = ctx->tb;
e0c8f9ce 3547 if (NARROW_MODE(ctx)) {
a2ffb812 3548 dest = (uint32_t) dest;
e0c8f9ce 3549 }
57fec1fe 3550 if ((tb->pc & TARGET_PAGE_MASK) == (dest & TARGET_PAGE_MASK) &&
8cbcb4fa 3551 likely(!ctx->singlestep_enabled)) {
57fec1fe 3552 tcg_gen_goto_tb(n);
a2ffb812 3553 tcg_gen_movi_tl(cpu_nip, dest & ~3);
4b4a72e5 3554 tcg_gen_exit_tb((tcg_target_long)tb + n);
c1942362 3555 } else {
a2ffb812 3556 tcg_gen_movi_tl(cpu_nip, dest & ~3);
8cbcb4fa
AJ
3557 if (unlikely(ctx->singlestep_enabled)) {
3558 if ((ctx->singlestep_enabled &
bdc4e053 3559 (CPU_BRANCH_STEP | CPU_SINGLE_STEP)) &&
f0cc4aa8
JG
3560 (ctx->exception == POWERPC_EXCP_BRANCH ||
3561 ctx->exception == POWERPC_EXCP_TRACE)) {
8cbcb4fa
AJ
3562 target_ulong tmp = ctx->nip;
3563 ctx->nip = dest;
e06fcd75 3564 gen_exception(ctx, POWERPC_EXCP_TRACE);
8cbcb4fa
AJ
3565 ctx->nip = tmp;
3566 }
3567 if (ctx->singlestep_enabled & GDBSTUB_SINGLE_STEP) {
e06fcd75 3568 gen_debug_exception(ctx);
8cbcb4fa
AJ
3569 }
3570 }
57fec1fe 3571 tcg_gen_exit_tb(0);
c1942362 3572 }
c53be334
FB
3573}
3574
636aa200 3575static inline void gen_setlr(DisasContext *ctx, target_ulong nip)
e1833e1f 3576{
e0c8f9ce
RH
3577 if (NARROW_MODE(ctx)) {
3578 nip = (uint32_t)nip;
3579 }
3580 tcg_gen_movi_tl(cpu_lr, nip);
e1833e1f
JM
3581}
3582
79aceca5 3583/* b ba bl bla */
99e300ef 3584static void gen_b(DisasContext *ctx)
79aceca5 3585{
76a66253 3586 target_ulong li, target;
38a64f9d 3587
8cbcb4fa 3588 ctx->exception = POWERPC_EXCP_BRANCH;
38a64f9d 3589 /* sign extend LI */
e0c8f9ce
RH
3590 li = LI(ctx->opcode);
3591 li = (li ^ 0x02000000) - 0x02000000;
3592 if (likely(AA(ctx->opcode) == 0)) {
046d6672 3593 target = ctx->nip + li - 4;
e0c8f9ce 3594 } else {
9a64fbe4 3595 target = li;
e0c8f9ce
RH
3596 }
3597 if (LK(ctx->opcode)) {
e1833e1f 3598 gen_setlr(ctx, ctx->nip);
e0c8f9ce 3599 }
697ab892 3600 gen_update_cfar(ctx, ctx->nip);
c1942362 3601 gen_goto_tb(ctx, 0, target);
79aceca5
FB
3602}
3603
e98a6e40
FB
3604#define BCOND_IM 0
3605#define BCOND_LR 1
3606#define BCOND_CTR 2
3607
636aa200 3608static inline void gen_bcond(DisasContext *ctx, int type)
d9bce9d9 3609{
d9bce9d9 3610 uint32_t bo = BO(ctx->opcode);
05f92404 3611 int l1;
a2ffb812 3612 TCGv target;
e98a6e40 3613
8cbcb4fa 3614 ctx->exception = POWERPC_EXCP_BRANCH;
a2ffb812 3615 if (type == BCOND_LR || type == BCOND_CTR) {
a7812ae4 3616 target = tcg_temp_local_new();
a2ffb812
AJ
3617 if (type == BCOND_CTR)
3618 tcg_gen_mov_tl(target, cpu_ctr);
3619 else
3620 tcg_gen_mov_tl(target, cpu_lr);
d2e9fd8f 3621 } else {
3622 TCGV_UNUSED(target);
e98a6e40 3623 }
e1833e1f
JM
3624 if (LK(ctx->opcode))
3625 gen_setlr(ctx, ctx->nip);
a2ffb812
AJ
3626 l1 = gen_new_label();
3627 if ((bo & 0x4) == 0) {
3628 /* Decrement and test CTR */
a7812ae4 3629 TCGv temp = tcg_temp_new();
a2ffb812 3630 if (unlikely(type == BCOND_CTR)) {
e06fcd75 3631 gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL);
a2ffb812
AJ
3632 return;
3633 }
3634 tcg_gen_subi_tl(cpu_ctr, cpu_ctr, 1);
e0c8f9ce 3635 if (NARROW_MODE(ctx)) {
a2ffb812 3636 tcg_gen_ext32u_tl(temp, cpu_ctr);
e0c8f9ce 3637 } else {
a2ffb812 3638 tcg_gen_mov_tl(temp, cpu_ctr);
e0c8f9ce 3639 }
a2ffb812
AJ
3640 if (bo & 0x2) {
3641 tcg_gen_brcondi_tl(TCG_COND_NE, temp, 0, l1);
3642 } else {
3643 tcg_gen_brcondi_tl(TCG_COND_EQ, temp, 0, l1);
e98a6e40 3644 }
a7812ae4 3645 tcg_temp_free(temp);
a2ffb812
AJ
3646 }
3647 if ((bo & 0x10) == 0) {
3648 /* Test CR */
3649 uint32_t bi = BI(ctx->opcode);
3650 uint32_t mask = 1 << (3 - (bi & 0x03));
a7812ae4 3651 TCGv_i32 temp = tcg_temp_new_i32();
a2ffb812 3652
d9bce9d9 3653 if (bo & 0x8) {
a2ffb812
AJ
3654 tcg_gen_andi_i32(temp, cpu_crf[bi >> 2], mask);
3655 tcg_gen_brcondi_i32(TCG_COND_EQ, temp, 0, l1);
d9bce9d9 3656 } else {
a2ffb812
AJ
3657 tcg_gen_andi_i32(temp, cpu_crf[bi >> 2], mask);
3658 tcg_gen_brcondi_i32(TCG_COND_NE, temp, 0, l1);
d9bce9d9 3659 }
a7812ae4 3660 tcg_temp_free_i32(temp);
d9bce9d9 3661 }
697ab892 3662 gen_update_cfar(ctx, ctx->nip);
e98a6e40 3663 if (type == BCOND_IM) {
a2ffb812
AJ
3664 target_ulong li = (target_long)((int16_t)(BD(ctx->opcode)));
3665 if (likely(AA(ctx->opcode) == 0)) {
3666 gen_goto_tb(ctx, 0, ctx->nip + li - 4);
3667 } else {
3668 gen_goto_tb(ctx, 0, li);
3669 }
c53be334 3670 gen_set_label(l1);
c1942362 3671 gen_goto_tb(ctx, 1, ctx->nip);
e98a6e40 3672 } else {
e0c8f9ce 3673 if (NARROW_MODE(ctx)) {
a2ffb812 3674 tcg_gen_andi_tl(cpu_nip, target, (uint32_t)~3);
e0c8f9ce 3675 } else {
a2ffb812 3676 tcg_gen_andi_tl(cpu_nip, target, ~3);
e0c8f9ce 3677 }
a2ffb812
AJ
3678 tcg_gen_exit_tb(0);
3679 gen_set_label(l1);
e0c8f9ce 3680 gen_update_nip(ctx, ctx->nip);
57fec1fe 3681 tcg_gen_exit_tb(0);
08e46e54 3682 }
e98a6e40
FB
3683}
3684
99e300ef 3685static void gen_bc(DisasContext *ctx)
3b46e624 3686{
e98a6e40
FB
3687 gen_bcond(ctx, BCOND_IM);
3688}
3689
99e300ef 3690static void gen_bcctr(DisasContext *ctx)
3b46e624 3691{
e98a6e40
FB
3692 gen_bcond(ctx, BCOND_CTR);
3693}
3694
99e300ef 3695static void gen_bclr(DisasContext *ctx)
3b46e624 3696{
e98a6e40
FB
3697 gen_bcond(ctx, BCOND_LR);
3698}
79aceca5
FB
3699
3700/*** Condition register logical ***/
e1571908 3701#define GEN_CRLOGIC(name, tcg_op, opc) \
99e300ef 3702static void glue(gen_, name)(DisasContext *ctx) \
79aceca5 3703{ \
fc0d441e
JM
3704 uint8_t bitmask; \
3705 int sh; \
a7812ae4 3706 TCGv_i32 t0, t1; \
fc0d441e 3707 sh = (crbD(ctx->opcode) & 0x03) - (crbA(ctx->opcode) & 0x03); \
a7812ae4 3708 t0 = tcg_temp_new_i32(); \
fc0d441e 3709 if (sh > 0) \
fea0c503 3710 tcg_gen_shri_i32(t0, cpu_crf[crbA(ctx->opcode) >> 2], sh); \
fc0d441e 3711 else if (sh < 0) \
fea0c503 3712 tcg_gen_shli_i32(t0, cpu_crf[crbA(ctx->opcode) >> 2], -sh); \
e1571908 3713 else \
fea0c503 3714 tcg_gen_mov_i32(t0, cpu_crf[crbA(ctx->opcode) >> 2]); \
a7812ae4 3715 t1 = tcg_temp_new_i32(); \
fc0d441e
JM
3716 sh = (crbD(ctx->opcode) & 0x03) - (crbB(ctx->opcode) & 0x03); \
3717 if (sh > 0) \
fea0c503 3718 tcg_gen_shri_i32(t1, cpu_crf[crbB(ctx->opcode) >> 2], sh); \
fc0d441e 3719 else if (sh < 0) \
fea0c503 3720 tcg_gen_shli_i32(t1, cpu_crf[crbB(ctx->opcode) >> 2], -sh); \
e1571908 3721 else \
fea0c503
AJ
3722 tcg_gen_mov_i32(t1, cpu_crf[crbB(ctx->opcode) >> 2]); \
3723 tcg_op(t0, t0, t1); \
fc0d441e 3724 bitmask = 1 << (3 - (crbD(ctx->opcode) & 0x03)); \
fea0c503
AJ
3725 tcg_gen_andi_i32(t0, t0, bitmask); \
3726 tcg_gen_andi_i32(t1, cpu_crf[crbD(ctx->opcode) >> 2], ~bitmask); \
3727 tcg_gen_or_i32(cpu_crf[crbD(ctx->opcode) >> 2], t0, t1); \
a7812ae4
PB
3728 tcg_temp_free_i32(t0); \
3729 tcg_temp_free_i32(t1); \
79aceca5
FB
3730}
3731
3732/* crand */
e1571908 3733GEN_CRLOGIC(crand, tcg_gen_and_i32, 0x08);
79aceca5 3734/* crandc */
e1571908 3735GEN_CRLOGIC(crandc, tcg_gen_andc_i32, 0x04);
79aceca5 3736/* creqv */
e1571908 3737GEN_CRLOGIC(creqv, tcg_gen_eqv_i32, 0x09);
79aceca5 3738/* crnand */
e1571908 3739GEN_CRLOGIC(crnand, tcg_gen_nand_i32, 0x07);
79aceca5 3740/* crnor */
e1571908 3741GEN_CRLOGIC(crnor, tcg_gen_nor_i32, 0x01);
79aceca5 3742/* cror */
e1571908 3743GEN_CRLOGIC(cror, tcg_gen_or_i32, 0x0E);
79aceca5 3744/* crorc */
e1571908 3745GEN_CRLOGIC(crorc, tcg_gen_orc_i32, 0x0D);
79aceca5 3746/* crxor */
e1571908 3747GEN_CRLOGIC(crxor, tcg_gen_xor_i32, 0x06);
99e300ef 3748
54623277 3749/* mcrf */
99e300ef 3750static void gen_mcrf(DisasContext *ctx)
79aceca5 3751{
47e4661c 3752 tcg_gen_mov_i32(cpu_crf[crfD(ctx->opcode)], cpu_crf[crfS(ctx->opcode)]);
79aceca5
FB
3753}
3754
3755/*** System linkage ***/
99e300ef 3756
54623277 3757/* rfi (mem_idx only) */
99e300ef 3758static void gen_rfi(DisasContext *ctx)
79aceca5 3759{
9a64fbe4 3760#if defined(CONFIG_USER_ONLY)
e06fcd75 3761 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
9a64fbe4
FB
3762#else
3763 /* Restore CPU state */
76db3ba4 3764 if (unlikely(!ctx->mem_idx)) {
e06fcd75 3765 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
9fddaa0c 3766 return;
9a64fbe4 3767 }
697ab892 3768 gen_update_cfar(ctx, ctx->nip);
e5f17ac6 3769 gen_helper_rfi(cpu_env);
e06fcd75 3770 gen_sync_exception(ctx);
9a64fbe4 3771#endif
79aceca5
FB
3772}
3773
426613db 3774#if defined(TARGET_PPC64)
99e300ef 3775static void gen_rfid(DisasContext *ctx)
426613db
JM
3776{
3777#if defined(CONFIG_USER_ONLY)
e06fcd75 3778 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
426613db
JM
3779#else
3780 /* Restore CPU state */
76db3ba4 3781 if (unlikely(!ctx->mem_idx)) {
e06fcd75 3782 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
426613db
JM
3783 return;
3784 }
697ab892 3785 gen_update_cfar(ctx, ctx->nip);
e5f17ac6 3786 gen_helper_rfid(cpu_env);
e06fcd75 3787 gen_sync_exception(ctx);
426613db
JM
3788#endif
3789}
426613db 3790
99e300ef 3791static void gen_hrfid(DisasContext *ctx)
be147d08
JM
3792{
3793#if defined(CONFIG_USER_ONLY)
e06fcd75 3794 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
be147d08
JM
3795#else
3796 /* Restore CPU state */
76db3ba4 3797 if (unlikely(ctx->mem_idx <= 1)) {
e06fcd75 3798 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
be147d08
JM
3799 return;
3800 }
e5f17ac6 3801 gen_helper_hrfid(cpu_env);
e06fcd75 3802 gen_sync_exception(ctx);
be147d08
JM
3803#endif
3804}
3805#endif
3806
79aceca5 3807/* sc */
417bf010
JM
3808#if defined(CONFIG_USER_ONLY)
3809#define POWERPC_SYSCALL POWERPC_EXCP_SYSCALL_USER
3810#else
3811#define POWERPC_SYSCALL POWERPC_EXCP_SYSCALL
3812#endif
99e300ef 3813static void gen_sc(DisasContext *ctx)
79aceca5 3814{
e1833e1f
JM
3815 uint32_t lev;
3816
3817 lev = (ctx->opcode >> 5) & 0x7F;
e06fcd75 3818 gen_exception_err(ctx, POWERPC_SYSCALL, lev);
79aceca5
FB
3819}
3820
3821/*** Trap ***/
99e300ef 3822
54623277 3823/* tw */
99e300ef 3824static void gen_tw(DisasContext *ctx)
79aceca5 3825{
cab3bee2 3826 TCGv_i32 t0 = tcg_const_i32(TO(ctx->opcode));
db9a231d
AJ
3827 /* Update the nip since this might generate a trap exception */
3828 gen_update_nip(ctx, ctx->nip);
e5f17ac6
BS
3829 gen_helper_tw(cpu_env, cpu_gpr[rA(ctx->opcode)], cpu_gpr[rB(ctx->opcode)],
3830 t0);
cab3bee2 3831 tcg_temp_free_i32(t0);
79aceca5
FB
3832}
3833
3834/* twi */
99e300ef 3835static void gen_twi(DisasContext *ctx)
79aceca5 3836{
cab3bee2
AJ
3837 TCGv t0 = tcg_const_tl(SIMM(ctx->opcode));
3838 TCGv_i32 t1 = tcg_const_i32(TO(ctx->opcode));
db9a231d
AJ
3839 /* Update the nip since this might generate a trap exception */
3840 gen_update_nip(ctx, ctx->nip);
e5f17ac6 3841 gen_helper_tw(cpu_env, cpu_gpr[rA(ctx->opcode)], t0, t1);
cab3bee2
AJ
3842 tcg_temp_free(t0);
3843 tcg_temp_free_i32(t1);
79aceca5
FB
3844}
3845
d9bce9d9
JM
3846#if defined(TARGET_PPC64)
3847/* td */
99e300ef 3848static void gen_td(DisasContext *ctx)
d9bce9d9 3849{
cab3bee2 3850 TCGv_i32 t0 = tcg_const_i32(TO(ctx->opcode));
db9a231d
AJ
3851 /* Update the nip since this might generate a trap exception */
3852 gen_update_nip(ctx, ctx->nip);
e5f17ac6
BS
3853 gen_helper_td(cpu_env, cpu_gpr[rA(ctx->opcode)], cpu_gpr[rB(ctx->opcode)],
3854 t0);
cab3bee2 3855 tcg_temp_free_i32(t0);
d9bce9d9
JM
3856}
3857
3858/* tdi */
99e300ef 3859static void gen_tdi(DisasContext *ctx)
d9bce9d9 3860{
cab3bee2
AJ
3861 TCGv t0 = tcg_const_tl(SIMM(ctx->opcode));
3862 TCGv_i32 t1 = tcg_const_i32(TO(ctx->opcode));
db9a231d
AJ
3863 /* Update the nip since this might generate a trap exception */
3864 gen_update_nip(ctx, ctx->nip);
e5f17ac6 3865 gen_helper_td(cpu_env, cpu_gpr[rA(ctx->opcode)], t0, t1);
cab3bee2
AJ
3866 tcg_temp_free(t0);
3867 tcg_temp_free_i32(t1);
d9bce9d9
JM
3868}
3869#endif
3870
79aceca5 3871/*** Processor control ***/
99e300ef 3872
da91a00f
RH
3873static void gen_read_xer(TCGv dst)
3874{
3875 TCGv t0 = tcg_temp_new();
3876 TCGv t1 = tcg_temp_new();
3877 TCGv t2 = tcg_temp_new();
3878 tcg_gen_mov_tl(dst, cpu_xer);
3879 tcg_gen_shli_tl(t0, cpu_so, XER_SO);
3880 tcg_gen_shli_tl(t1, cpu_ov, XER_OV);
3881 tcg_gen_shli_tl(t2, cpu_ca, XER_CA);
3882 tcg_gen_or_tl(t0, t0, t1);
3883 tcg_gen_or_tl(dst, dst, t2);
3884 tcg_gen_or_tl(dst, dst, t0);
3885 tcg_temp_free(t0);
3886 tcg_temp_free(t1);
3887 tcg_temp_free(t2);
3888}
3889
3890static void gen_write_xer(TCGv src)
3891{
3892 tcg_gen_andi_tl(cpu_xer, src,
3893 ~((1u << XER_SO) | (1u << XER_OV) | (1u << XER_CA)));
3894 tcg_gen_shri_tl(cpu_so, src, XER_SO);
3895 tcg_gen_shri_tl(cpu_ov, src, XER_OV);
3896 tcg_gen_shri_tl(cpu_ca, src, XER_CA);
3897 tcg_gen_andi_tl(cpu_so, cpu_so, 1);
3898 tcg_gen_andi_tl(cpu_ov, cpu_ov, 1);
3899 tcg_gen_andi_tl(cpu_ca, cpu_ca, 1);
3900}
3901
54623277 3902/* mcrxr */
99e300ef 3903static void gen_mcrxr(DisasContext *ctx)
79aceca5 3904{
da91a00f
RH
3905 TCGv_i32 t0 = tcg_temp_new_i32();
3906 TCGv_i32 t1 = tcg_temp_new_i32();
3907 TCGv_i32 dst = cpu_crf[crfD(ctx->opcode)];
3908
3909 tcg_gen_trunc_tl_i32(t0, cpu_so);
3910 tcg_gen_trunc_tl_i32(t1, cpu_ov);
3911 tcg_gen_trunc_tl_i32(dst, cpu_ca);
3912 tcg_gen_shri_i32(t0, t0, 2);
3913 tcg_gen_shri_i32(t1, t1, 1);
3914 tcg_gen_or_i32(dst, dst, t0);
3915 tcg_gen_or_i32(dst, dst, t1);
3916 tcg_temp_free_i32(t0);
3917 tcg_temp_free_i32(t1);
3918
3919 tcg_gen_movi_tl(cpu_so, 0);
3920 tcg_gen_movi_tl(cpu_ov, 0);
3921 tcg_gen_movi_tl(cpu_ca, 0);
79aceca5
FB
3922}
3923
0cfe11ea 3924/* mfcr mfocrf */
99e300ef 3925static void gen_mfcr(DisasContext *ctx)
79aceca5 3926{
76a66253 3927 uint32_t crm, crn;
3b46e624 3928
76a66253
JM
3929 if (likely(ctx->opcode & 0x00100000)) {
3930 crm = CRM(ctx->opcode);
8dd640e4 3931 if (likely(crm && ((crm & (crm - 1)) == 0))) {
0cfe11ea 3932 crn = ctz32 (crm);
e1571908 3933 tcg_gen_extu_i32_tl(cpu_gpr[rD(ctx->opcode)], cpu_crf[7 - crn]);
0497d2f4
AJ
3934 tcg_gen_shli_tl(cpu_gpr[rD(ctx->opcode)],
3935 cpu_gpr[rD(ctx->opcode)], crn * 4);
76a66253 3936 }
d9bce9d9 3937 } else {
651721b2
AJ
3938 TCGv_i32 t0 = tcg_temp_new_i32();
3939 tcg_gen_mov_i32(t0, cpu_crf[0]);
3940 tcg_gen_shli_i32(t0, t0, 4);
3941 tcg_gen_or_i32(t0, t0, cpu_crf[1]);
3942 tcg_gen_shli_i32(t0, t0, 4);
3943 tcg_gen_or_i32(t0, t0, cpu_crf[2]);
3944 tcg_gen_shli_i32(t0, t0, 4);
3945 tcg_gen_or_i32(t0, t0, cpu_crf[3]);
3946 tcg_gen_shli_i32(t0, t0, 4);
3947 tcg_gen_or_i32(t0, t0, cpu_crf[4]);
3948 tcg_gen_shli_i32(t0, t0, 4);
3949 tcg_gen_or_i32(t0, t0, cpu_crf[5]);
3950 tcg_gen_shli_i32(t0, t0, 4);
3951 tcg_gen_or_i32(t0, t0, cpu_crf[6]);
3952 tcg_gen_shli_i32(t0, t0, 4);
3953 tcg_gen_or_i32(t0, t0, cpu_crf[7]);
3954 tcg_gen_extu_i32_tl(cpu_gpr[rD(ctx->opcode)], t0);
3955 tcg_temp_free_i32(t0);
d9bce9d9 3956 }
79aceca5
FB
3957}
3958
3959/* mfmsr */
99e300ef 3960static void gen_mfmsr(DisasContext *ctx)
79aceca5 3961{
9a64fbe4 3962#if defined(CONFIG_USER_ONLY)
e06fcd75 3963 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
9a64fbe4 3964#else
76db3ba4 3965 if (unlikely(!ctx->mem_idx)) {
e06fcd75 3966 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
9fddaa0c 3967 return;
9a64fbe4 3968 }
6527f6ea 3969 tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_msr);
9a64fbe4 3970#endif
79aceca5
FB
3971}
3972
7b13448f 3973static void spr_noaccess(void *opaque, int gprn, int sprn)
3fc6c082 3974{
7b13448f 3975#if 0
3fc6c082
FB
3976 sprn = ((sprn >> 5) & 0x1F) | ((sprn & 0x1F) << 5);
3977 printf("ERROR: try to access SPR %d !\n", sprn);
7b13448f 3978#endif
3fc6c082
FB
3979}
3980#define SPR_NOACCESS (&spr_noaccess)
3fc6c082 3981
79aceca5 3982/* mfspr */
636aa200 3983static inline void gen_op_mfspr(DisasContext *ctx)
79aceca5 3984{
45d827d2 3985 void (*read_cb)(void *opaque, int gprn, int sprn);
79aceca5
FB
3986 uint32_t sprn = SPR(ctx->opcode);
3987
3fc6c082 3988#if !defined(CONFIG_USER_ONLY)
76db3ba4 3989 if (ctx->mem_idx == 2)
be147d08 3990 read_cb = ctx->spr_cb[sprn].hea_read;
76db3ba4 3991 else if (ctx->mem_idx)
3fc6c082
FB
3992 read_cb = ctx->spr_cb[sprn].oea_read;
3993 else
9a64fbe4 3994#endif
3fc6c082 3995 read_cb = ctx->spr_cb[sprn].uea_read;
76a66253
JM
3996 if (likely(read_cb != NULL)) {
3997 if (likely(read_cb != SPR_NOACCESS)) {
45d827d2 3998 (*read_cb)(ctx, rD(ctx->opcode), sprn);
3fc6c082
FB
3999 } else {
4000 /* Privilege exception */
9fceefa7
JM
4001 /* This is a hack to avoid warnings when running Linux:
4002 * this OS breaks the PowerPC virtualisation model,
4003 * allowing userland application to read the PVR
4004 */
4005 if (sprn != SPR_PVR) {
c05541ee
AB
4006 qemu_log("Trying to read privileged spr %d (0x%03x) at "
4007 TARGET_FMT_lx "\n", sprn, sprn, ctx->nip - 4);
4008 printf("Trying to read privileged spr %d (0x%03x) at "
4009 TARGET_FMT_lx "\n", sprn, sprn, ctx->nip - 4);
f24e5695 4010 }
e06fcd75 4011 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
79aceca5 4012 }
3fc6c082
FB
4013 } else {
4014 /* Not defined */
c05541ee
AB
4015 qemu_log("Trying to read invalid spr %d (0x%03x) at "
4016 TARGET_FMT_lx "\n", sprn, sprn, ctx->nip - 4);
4017 printf("Trying to read invalid spr %d (0x%03x) at "
4018 TARGET_FMT_lx "\n", sprn, sprn, ctx->nip - 4);
e06fcd75 4019 gen_inval_exception(ctx, POWERPC_EXCP_INVAL_SPR);
79aceca5 4020 }
79aceca5
FB
4021}
4022
99e300ef 4023static void gen_mfspr(DisasContext *ctx)
79aceca5 4024{
3fc6c082 4025 gen_op_mfspr(ctx);
76a66253 4026}
3fc6c082
FB
4027
4028/* mftb */
99e300ef 4029static void gen_mftb(DisasContext *ctx)
3fc6c082
FB
4030{
4031 gen_op_mfspr(ctx);
79aceca5
FB
4032}
4033
0cfe11ea 4034/* mtcrf mtocrf*/
99e300ef 4035static void gen_mtcrf(DisasContext *ctx)
79aceca5 4036{
76a66253 4037 uint32_t crm, crn;
3b46e624 4038
76a66253 4039 crm = CRM(ctx->opcode);
8dd640e4 4040 if (likely((ctx->opcode & 0x00100000))) {
4041 if (crm && ((crm & (crm - 1)) == 0)) {
4042 TCGv_i32 temp = tcg_temp_new_i32();
0cfe11ea 4043 crn = ctz32 (crm);
8dd640e4 4044 tcg_gen_trunc_tl_i32(temp, cpu_gpr[rS(ctx->opcode)]);
0cfe11ea
AJ
4045 tcg_gen_shri_i32(temp, temp, crn * 4);
4046 tcg_gen_andi_i32(cpu_crf[7 - crn], temp, 0xf);
8dd640e4 4047 tcg_temp_free_i32(temp);
4048 }
76a66253 4049 } else {
651721b2
AJ
4050 TCGv_i32 temp = tcg_temp_new_i32();
4051 tcg_gen_trunc_tl_i32(temp, cpu_gpr[rS(ctx->opcode)]);
4052 for (crn = 0 ; crn < 8 ; crn++) {
4053 if (crm & (1 << crn)) {
4054 tcg_gen_shri_i32(cpu_crf[7 - crn], temp, crn * 4);
4055 tcg_gen_andi_i32(cpu_crf[7 - crn], cpu_crf[7 - crn], 0xf);
4056 }
4057 }
a7812ae4 4058 tcg_temp_free_i32(temp);
76a66253 4059 }
79aceca5
FB
4060}
4061
4062/* mtmsr */
426613db 4063#if defined(TARGET_PPC64)
99e300ef 4064static void gen_mtmsrd(DisasContext *ctx)
426613db
JM
4065{
4066#if defined(CONFIG_USER_ONLY)
e06fcd75 4067 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
426613db 4068#else
76db3ba4 4069 if (unlikely(!ctx->mem_idx)) {
e06fcd75 4070 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
426613db
JM
4071 return;
4072 }
be147d08
JM
4073 if (ctx->opcode & 0x00010000) {
4074 /* Special form that does not need any synchronisation */
6527f6ea
AJ
4075 TCGv t0 = tcg_temp_new();
4076 tcg_gen_andi_tl(t0, cpu_gpr[rS(ctx->opcode)], (1 << MSR_RI) | (1 << MSR_EE));
4077 tcg_gen_andi_tl(cpu_msr, cpu_msr, ~((1 << MSR_RI) | (1 << MSR_EE)));
4078 tcg_gen_or_tl(cpu_msr, cpu_msr, t0);
4079 tcg_temp_free(t0);
be147d08 4080 } else {
056b05f8
JM
4081 /* XXX: we need to update nip before the store
4082 * if we enter power saving mode, we will exit the loop
4083 * directly from ppc_store_msr
4084 */
be147d08 4085 gen_update_nip(ctx, ctx->nip);
e5f17ac6 4086 gen_helper_store_msr(cpu_env, cpu_gpr[rS(ctx->opcode)]);
be147d08
JM
4087 /* Must stop the translation as machine state (may have) changed */
4088 /* Note that mtmsr is not always defined as context-synchronizing */
e06fcd75 4089 gen_stop_exception(ctx);
be147d08 4090 }
426613db
JM
4091#endif
4092}
4093#endif
4094
99e300ef 4095static void gen_mtmsr(DisasContext *ctx)
79aceca5 4096{
9a64fbe4 4097#if defined(CONFIG_USER_ONLY)
e06fcd75 4098 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
9a64fbe4 4099#else
76db3ba4 4100 if (unlikely(!ctx->mem_idx)) {
e06fcd75 4101 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
9fddaa0c 4102 return;
9a64fbe4 4103 }
be147d08
JM
4104 if (ctx->opcode & 0x00010000) {
4105 /* Special form that does not need any synchronisation */
6527f6ea
AJ
4106 TCGv t0 = tcg_temp_new();
4107 tcg_gen_andi_tl(t0, cpu_gpr[rS(ctx->opcode)], (1 << MSR_RI) | (1 << MSR_EE));
4108 tcg_gen_andi_tl(cpu_msr, cpu_msr, ~((1 << MSR_RI) | (1 << MSR_EE)));
4109 tcg_gen_or_tl(cpu_msr, cpu_msr, t0);
4110 tcg_temp_free(t0);
be147d08 4111 } else {
8018dc63
AG
4112 TCGv msr = tcg_temp_new();
4113
056b05f8
JM
4114 /* XXX: we need to update nip before the store
4115 * if we enter power saving mode, we will exit the loop
4116 * directly from ppc_store_msr
4117 */
be147d08 4118 gen_update_nip(ctx, ctx->nip);
d9bce9d9 4119#if defined(TARGET_PPC64)
8018dc63
AG
4120 tcg_gen_deposit_tl(msr, cpu_msr, cpu_gpr[rS(ctx->opcode)], 0, 32);
4121#else
4122 tcg_gen_mov_tl(msr, cpu_gpr[rS(ctx->opcode)]);
d9bce9d9 4123#endif
e5f17ac6 4124 gen_helper_store_msr(cpu_env, msr);
be147d08 4125 /* Must stop the translation as machine state (may have) changed */
6527f6ea 4126 /* Note that mtmsr is not always defined as context-synchronizing */
e06fcd75 4127 gen_stop_exception(ctx);
be147d08 4128 }
9a64fbe4 4129#endif
79aceca5
FB
4130}
4131
4132/* mtspr */
99e300ef 4133static void gen_mtspr(DisasContext *ctx)
79aceca5 4134{
45d827d2 4135 void (*write_cb)(void *opaque, int sprn, int gprn);
79aceca5
FB
4136 uint32_t sprn = SPR(ctx->opcode);
4137
3fc6c082 4138#if !defined(CONFIG_USER_ONLY)
76db3ba4 4139 if (ctx->mem_idx == 2)
be147d08 4140 write_cb = ctx->spr_cb[sprn].hea_write;
76db3ba4 4141 else if (ctx->mem_idx)
3fc6c082
FB
4142 write_cb = ctx->spr_cb[sprn].oea_write;
4143 else
9a64fbe4 4144#endif
3fc6c082 4145 write_cb = ctx->spr_cb[sprn].uea_write;
76a66253
JM
4146 if (likely(write_cb != NULL)) {
4147 if (likely(write_cb != SPR_NOACCESS)) {
45d827d2 4148 (*write_cb)(ctx, sprn, rS(ctx->opcode));
3fc6c082
FB
4149 } else {
4150 /* Privilege exception */
c05541ee
AB
4151 qemu_log("Trying to write privileged spr %d (0x%03x) at "
4152 TARGET_FMT_lx "\n", sprn, sprn, ctx->nip - 4);
4153 printf("Trying to write privileged spr %d (0x%03x) at "
4154 TARGET_FMT_lx "\n", sprn, sprn, ctx->nip - 4);
e06fcd75 4155 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
76a66253 4156 }
3fc6c082
FB
4157 } else {
4158 /* Not defined */
c05541ee
AB
4159 qemu_log("Trying to write invalid spr %d (0x%03x) at "
4160 TARGET_FMT_lx "\n", sprn, sprn, ctx->nip - 4);
4161 printf("Trying to write invalid spr %d (0x%03x) at "
4162 TARGET_FMT_lx "\n", sprn, sprn, ctx->nip - 4);
e06fcd75 4163 gen_inval_exception(ctx, POWERPC_EXCP_INVAL_SPR);
79aceca5 4164 }
79aceca5
FB
4165}
4166
4167/*** Cache management ***/
99e300ef 4168
54623277 4169/* dcbf */
99e300ef 4170static void gen_dcbf(DisasContext *ctx)
79aceca5 4171{
dac454af 4172 /* XXX: specification says this is treated as a load by the MMU */
76db3ba4
AJ
4173 TCGv t0;
4174 gen_set_access_type(ctx, ACCESS_CACHE);
4175 t0 = tcg_temp_new();
4176 gen_addr_reg_index(ctx, t0);
4177 gen_qemu_ld8u(ctx, t0, t0);
fea0c503 4178 tcg_temp_free(t0);
79aceca5
FB
4179}
4180
4181/* dcbi (Supervisor only) */
99e300ef 4182static void gen_dcbi(DisasContext *ctx)
79aceca5 4183{
a541f297 4184#if defined(CONFIG_USER_ONLY)
e06fcd75 4185 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
a541f297 4186#else
b61f2753 4187 TCGv EA, val;
76db3ba4 4188 if (unlikely(!ctx->mem_idx)) {
e06fcd75 4189 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
9fddaa0c 4190 return;
9a64fbe4 4191 }
a7812ae4 4192 EA = tcg_temp_new();
76db3ba4
AJ
4193 gen_set_access_type(ctx, ACCESS_CACHE);
4194 gen_addr_reg_index(ctx, EA);
a7812ae4 4195 val = tcg_temp_new();
76a66253 4196 /* XXX: specification says this should be treated as a store by the MMU */
76db3ba4
AJ
4197 gen_qemu_ld8u(ctx, val, EA);
4198 gen_qemu_st8(ctx, val, EA);
b61f2753
AJ
4199 tcg_temp_free(val);
4200 tcg_temp_free(EA);
a541f297 4201#endif
79aceca5
FB
4202}
4203
4204/* dcdst */
99e300ef 4205static void gen_dcbst(DisasContext *ctx)
79aceca5 4206{
76a66253 4207 /* XXX: specification say this is treated as a load by the MMU */
76db3ba4
AJ
4208 TCGv t0;
4209 gen_set_access_type(ctx, ACCESS_CACHE);
4210 t0 = tcg_temp_new();
4211 gen_addr_reg_index(ctx, t0);
4212 gen_qemu_ld8u(ctx, t0, t0);
fea0c503 4213 tcg_temp_free(t0);
79aceca5
FB
4214}
4215
4216/* dcbt */
99e300ef 4217static void gen_dcbt(DisasContext *ctx)
79aceca5 4218{
0db1b20e 4219 /* interpreted as no-op */
76a66253
JM
4220 /* XXX: specification say this is treated as a load by the MMU
4221 * but does not generate any exception
4222 */
79aceca5
FB
4223}
4224
4225/* dcbtst */
99e300ef 4226static void gen_dcbtst(DisasContext *ctx)
79aceca5 4227{
0db1b20e 4228 /* interpreted as no-op */
76a66253
JM
4229 /* XXX: specification say this is treated as a load by the MMU
4230 * but does not generate any exception
4231 */
79aceca5
FB
4232}
4233
4234/* dcbz */
99e300ef 4235static void gen_dcbz(DisasContext *ctx)
79aceca5 4236{
8e33944f
AG
4237 TCGv tcgv_addr;
4238 TCGv_i32 tcgv_is_dcbzl;
4239 int is_dcbzl = ctx->opcode & 0x00200000 ? 1 : 0;
d63001d1 4240
76db3ba4 4241 gen_set_access_type(ctx, ACCESS_CACHE);
799a8c8d
AJ
4242 /* NIP cannot be restored if the memory exception comes from an helper */
4243 gen_update_nip(ctx, ctx->nip - 4);
8e33944f
AG
4244 tcgv_addr = tcg_temp_new();
4245 tcgv_is_dcbzl = tcg_const_i32(is_dcbzl);
4246
4247 gen_addr_reg_index(ctx, tcgv_addr);
4248 gen_helper_dcbz(cpu_env, tcgv_addr, tcgv_is_dcbzl);
4249
4250 tcg_temp_free(tcgv_addr);
4251 tcg_temp_free_i32(tcgv_is_dcbzl);
79aceca5
FB
4252}
4253
ae1c1a3d 4254/* dst / dstt */
99e300ef 4255static void gen_dst(DisasContext *ctx)
ae1c1a3d
AJ
4256{
4257 if (rA(ctx->opcode) == 0) {
4258 gen_inval_exception(ctx, POWERPC_EXCP_INVAL_LSWX);
4259 } else {
4260 /* interpreted as no-op */
4261 }
4262}
4263
4264/* dstst /dststt */
99e300ef 4265static void gen_dstst(DisasContext *ctx)
ae1c1a3d
AJ
4266{
4267 if (rA(ctx->opcode) == 0) {
4268 gen_inval_exception(ctx, POWERPC_EXCP_INVAL_LSWX);
4269 } else {
4270 /* interpreted as no-op */
4271 }
4272
4273}
4274
4275/* dss / dssall */
99e300ef 4276static void gen_dss(DisasContext *ctx)
ae1c1a3d
AJ
4277{
4278 /* interpreted as no-op */
4279}
4280
79aceca5 4281/* icbi */
99e300ef 4282static void gen_icbi(DisasContext *ctx)
79aceca5 4283{
76db3ba4
AJ
4284 TCGv t0;
4285 gen_set_access_type(ctx, ACCESS_CACHE);
30032c94
JM
4286 /* NIP cannot be restored if the memory exception comes from an helper */
4287 gen_update_nip(ctx, ctx->nip - 4);
76db3ba4
AJ
4288 t0 = tcg_temp_new();
4289 gen_addr_reg_index(ctx, t0);
2f5a189c 4290 gen_helper_icbi(cpu_env, t0);
37d269df 4291 tcg_temp_free(t0);
79aceca5
FB
4292}
4293
4294/* Optional: */
4295/* dcba */
99e300ef 4296static void gen_dcba(DisasContext *ctx)
79aceca5 4297{
0db1b20e
JM
4298 /* interpreted as no-op */
4299 /* XXX: specification say this is treated as a store by the MMU
4300 * but does not generate any exception
4301 */
79aceca5
FB
4302}
4303
4304/*** Segment register manipulation ***/
4305/* Supervisor only: */
99e300ef 4306
54623277 4307/* mfsr */
99e300ef 4308static void gen_mfsr(DisasContext *ctx)
79aceca5 4309{
9a64fbe4 4310#if defined(CONFIG_USER_ONLY)
e06fcd75 4311 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
9a64fbe4 4312#else
74d37793 4313 TCGv t0;
76db3ba4 4314 if (unlikely(!ctx->mem_idx)) {
e06fcd75 4315 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
9fddaa0c 4316 return;
9a64fbe4 4317 }
74d37793 4318 t0 = tcg_const_tl(SR(ctx->opcode));
c6c7cf05 4319 gen_helper_load_sr(cpu_gpr[rD(ctx->opcode)], cpu_env, t0);
74d37793 4320 tcg_temp_free(t0);
9a64fbe4 4321#endif
79aceca5
FB
4322}
4323
4324/* mfsrin */
99e300ef 4325static void gen_mfsrin(DisasContext *ctx)
79aceca5 4326{
9a64fbe4 4327#if defined(CONFIG_USER_ONLY)
e06fcd75 4328 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
9a64fbe4 4329#else
74d37793 4330 TCGv t0;
76db3ba4 4331 if (unlikely(!ctx->mem_idx)) {
e06fcd75 4332 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
9fddaa0c 4333 return;
9a64fbe4 4334 }
74d37793
AJ
4335 t0 = tcg_temp_new();
4336 tcg_gen_shri_tl(t0, cpu_gpr[rB(ctx->opcode)], 28);
4337 tcg_gen_andi_tl(t0, t0, 0xF);
c6c7cf05 4338 gen_helper_load_sr(cpu_gpr[rD(ctx->opcode)], cpu_env, t0);
74d37793 4339 tcg_temp_free(t0);
9a64fbe4 4340#endif
79aceca5
FB
4341}
4342
4343/* mtsr */
99e300ef 4344static void gen_mtsr(DisasContext *ctx)
79aceca5 4345{
9a64fbe4 4346#if defined(CONFIG_USER_ONLY)
e06fcd75 4347 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
9a64fbe4 4348#else
74d37793 4349 TCGv t0;
76db3ba4 4350 if (unlikely(!ctx->mem_idx)) {
e06fcd75 4351 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
9fddaa0c 4352 return;
9a64fbe4 4353 }
74d37793 4354 t0 = tcg_const_tl(SR(ctx->opcode));
c6c7cf05 4355 gen_helper_store_sr(cpu_env, t0, cpu_gpr[rS(ctx->opcode)]);
74d37793 4356 tcg_temp_free(t0);
9a64fbe4 4357#endif
79aceca5
FB
4358}
4359
4360/* mtsrin */
99e300ef 4361static void gen_mtsrin(DisasContext *ctx)
79aceca5 4362{
9a64fbe4 4363#if defined(CONFIG_USER_ONLY)
e06fcd75 4364 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
9a64fbe4 4365#else
74d37793 4366 TCGv t0;
76db3ba4 4367 if (unlikely(!ctx->mem_idx)) {
e06fcd75 4368 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
9fddaa0c 4369 return;
9a64fbe4 4370 }
74d37793
AJ
4371 t0 = tcg_temp_new();
4372 tcg_gen_shri_tl(t0, cpu_gpr[rB(ctx->opcode)], 28);
4373 tcg_gen_andi_tl(t0, t0, 0xF);
c6c7cf05 4374 gen_helper_store_sr(cpu_env, t0, cpu_gpr[rD(ctx->opcode)]);
74d37793 4375 tcg_temp_free(t0);
9a64fbe4 4376#endif
79aceca5
FB
4377}
4378
12de9a39
JM
4379#if defined(TARGET_PPC64)
4380/* Specific implementation for PowerPC 64 "bridge" emulation using SLB */
e8eaa2c0 4381
54623277 4382/* mfsr */
e8eaa2c0 4383static void gen_mfsr_64b(DisasContext *ctx)
12de9a39
JM
4384{
4385#if defined(CONFIG_USER_ONLY)
e06fcd75 4386 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
12de9a39 4387#else
74d37793 4388 TCGv t0;
76db3ba4 4389 if (unlikely(!ctx->mem_idx)) {
e06fcd75 4390 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
12de9a39
JM
4391 return;
4392 }
74d37793 4393 t0 = tcg_const_tl(SR(ctx->opcode));
c6c7cf05 4394 gen_helper_load_sr(cpu_gpr[rD(ctx->opcode)], cpu_env, t0);
74d37793 4395 tcg_temp_free(t0);
12de9a39
JM
4396#endif
4397}
4398
4399/* mfsrin */
e8eaa2c0 4400static void gen_mfsrin_64b(DisasContext *ctx)
12de9a39
JM
4401{
4402#if defined(CONFIG_USER_ONLY)
e06fcd75 4403 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
12de9a39 4404#else
74d37793 4405 TCGv t0;
76db3ba4 4406 if (unlikely(!ctx->mem_idx)) {
e06fcd75 4407 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
12de9a39
JM
4408 return;
4409 }
74d37793
AJ
4410 t0 = tcg_temp_new();
4411 tcg_gen_shri_tl(t0, cpu_gpr[rB(ctx->opcode)], 28);
4412 tcg_gen_andi_tl(t0, t0, 0xF);
c6c7cf05 4413 gen_helper_load_sr(cpu_gpr[rD(ctx->opcode)], cpu_env, t0);
74d37793 4414 tcg_temp_free(t0);
12de9a39
JM
4415#endif
4416}
4417
4418/* mtsr */
e8eaa2c0 4419static void gen_mtsr_64b(DisasContext *ctx)
12de9a39
JM
4420{
4421#if defined(CONFIG_USER_ONLY)
e06fcd75 4422 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
12de9a39 4423#else
74d37793 4424 TCGv t0;
76db3ba4 4425 if (unlikely(!ctx->mem_idx)) {
e06fcd75 4426 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
12de9a39
JM
4427 return;
4428 }
74d37793 4429 t0 = tcg_const_tl(SR(ctx->opcode));
c6c7cf05 4430 gen_helper_store_sr(cpu_env, t0, cpu_gpr[rS(ctx->opcode)]);
74d37793 4431 tcg_temp_free(t0);
12de9a39
JM
4432#endif
4433}
4434
4435/* mtsrin */
e8eaa2c0 4436static void gen_mtsrin_64b(DisasContext *ctx)
12de9a39
JM
4437{
4438#if defined(CONFIG_USER_ONLY)
e06fcd75 4439 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
12de9a39 4440#else
74d37793 4441 TCGv t0;
76db3ba4 4442 if (unlikely(!ctx->mem_idx)) {
e06fcd75 4443 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
12de9a39
JM
4444 return;
4445 }
74d37793
AJ
4446 t0 = tcg_temp_new();
4447 tcg_gen_shri_tl(t0, cpu_gpr[rB(ctx->opcode)], 28);
4448 tcg_gen_andi_tl(t0, t0, 0xF);
c6c7cf05 4449 gen_helper_store_sr(cpu_env, t0, cpu_gpr[rS(ctx->opcode)]);
74d37793 4450 tcg_temp_free(t0);
12de9a39
JM
4451#endif
4452}
f6b868fc
BS
4453
4454/* slbmte */
e8eaa2c0 4455static void gen_slbmte(DisasContext *ctx)
f6b868fc
BS
4456{
4457#if defined(CONFIG_USER_ONLY)
4458 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
4459#else
4460 if (unlikely(!ctx->mem_idx)) {
4461 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
4462 return;
4463 }
c6c7cf05
BS
4464 gen_helper_store_slb(cpu_env, cpu_gpr[rB(ctx->opcode)],
4465 cpu_gpr[rS(ctx->opcode)]);
f6b868fc
BS
4466#endif
4467}
4468
efdef95f
DG
4469static void gen_slbmfee(DisasContext *ctx)
4470{
4471#if defined(CONFIG_USER_ONLY)
4472 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
4473#else
4474 if (unlikely(!ctx->mem_idx)) {
4475 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
4476 return;
4477 }
c6c7cf05 4478 gen_helper_load_slb_esid(cpu_gpr[rS(ctx->opcode)], cpu_env,
efdef95f
DG
4479 cpu_gpr[rB(ctx->opcode)]);
4480#endif
4481}
4482
4483static void gen_slbmfev(DisasContext *ctx)
4484{
4485#if defined(CONFIG_USER_ONLY)
4486 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
4487#else
4488 if (unlikely(!ctx->mem_idx)) {
4489 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
4490 return;
4491 }
c6c7cf05 4492 gen_helper_load_slb_vsid(cpu_gpr[rS(ctx->opcode)], cpu_env,
efdef95f
DG
4493 cpu_gpr[rB(ctx->opcode)]);
4494#endif
4495}
12de9a39
JM
4496#endif /* defined(TARGET_PPC64) */
4497
79aceca5 4498/*** Lookaside buffer management ***/
76db3ba4 4499/* Optional & mem_idx only: */
99e300ef 4500
54623277 4501/* tlbia */
99e300ef 4502static void gen_tlbia(DisasContext *ctx)
79aceca5 4503{
9a64fbe4 4504#if defined(CONFIG_USER_ONLY)
e06fcd75 4505 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
9a64fbe4 4506#else
76db3ba4 4507 if (unlikely(!ctx->mem_idx)) {
e06fcd75 4508 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
9fddaa0c 4509 return;
9a64fbe4 4510 }
c6c7cf05 4511 gen_helper_tlbia(cpu_env);
9a64fbe4 4512#endif
79aceca5
FB
4513}
4514
bf14b1ce 4515/* tlbiel */
99e300ef 4516static void gen_tlbiel(DisasContext *ctx)
bf14b1ce
BS
4517{
4518#if defined(CONFIG_USER_ONLY)
4519 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
4520#else
4521 if (unlikely(!ctx->mem_idx)) {
4522 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
4523 return;
4524 }
c6c7cf05 4525 gen_helper_tlbie(cpu_env, cpu_gpr[rB(ctx->opcode)]);
bf14b1ce
BS
4526#endif
4527}
4528
79aceca5 4529/* tlbie */
99e300ef 4530static void gen_tlbie(DisasContext *ctx)
79aceca5 4531{
9a64fbe4 4532#if defined(CONFIG_USER_ONLY)
e06fcd75 4533 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
9a64fbe4 4534#else
76db3ba4 4535 if (unlikely(!ctx->mem_idx)) {
e06fcd75 4536 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
9fddaa0c 4537 return;
9a64fbe4 4538 }
9ca3f7f3 4539 if (NARROW_MODE(ctx)) {
74d37793
AJ
4540 TCGv t0 = tcg_temp_new();
4541 tcg_gen_ext32u_tl(t0, cpu_gpr[rB(ctx->opcode)]);
c6c7cf05 4542 gen_helper_tlbie(cpu_env, t0);
74d37793 4543 tcg_temp_free(t0);
9ca3f7f3 4544 } else {
c6c7cf05 4545 gen_helper_tlbie(cpu_env, cpu_gpr[rB(ctx->opcode)]);
9ca3f7f3 4546 }
9a64fbe4 4547#endif
79aceca5
FB
4548}
4549
4550/* tlbsync */
99e300ef 4551static void gen_tlbsync(DisasContext *ctx)
79aceca5 4552{
9a64fbe4 4553#if defined(CONFIG_USER_ONLY)
e06fcd75 4554 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
9a64fbe4 4555#else
76db3ba4 4556 if (unlikely(!ctx->mem_idx)) {
e06fcd75 4557 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
9fddaa0c 4558 return;
9a64fbe4
FB
4559 }
4560 /* This has no effect: it should ensure that all previous
4561 * tlbie have completed
4562 */
e06fcd75 4563 gen_stop_exception(ctx);
9a64fbe4 4564#endif
79aceca5
FB
4565}
4566
426613db
JM
4567#if defined(TARGET_PPC64)
4568/* slbia */
99e300ef 4569static void gen_slbia(DisasContext *ctx)
426613db
JM
4570{
4571#if defined(CONFIG_USER_ONLY)
e06fcd75 4572 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
426613db 4573#else
76db3ba4 4574 if (unlikely(!ctx->mem_idx)) {
e06fcd75 4575 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
426613db
JM
4576 return;
4577 }
c6c7cf05 4578 gen_helper_slbia(cpu_env);
426613db
JM
4579#endif
4580}
4581
4582/* slbie */
99e300ef 4583static void gen_slbie(DisasContext *ctx)
426613db
JM
4584{
4585#if defined(CONFIG_USER_ONLY)
e06fcd75 4586 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
426613db 4587#else
76db3ba4 4588 if (unlikely(!ctx->mem_idx)) {
e06fcd75 4589 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
426613db
JM
4590 return;
4591 }
c6c7cf05 4592 gen_helper_slbie(cpu_env, cpu_gpr[rB(ctx->opcode)]);
426613db
JM
4593#endif
4594}
4595#endif
4596
79aceca5
FB
4597/*** External control ***/
4598/* Optional: */
99e300ef 4599
54623277 4600/* eciwx */
99e300ef 4601static void gen_eciwx(DisasContext *ctx)
79aceca5 4602{
76db3ba4 4603 TCGv t0;
fa407c03 4604 /* Should check EAR[E] ! */
76db3ba4
AJ
4605 gen_set_access_type(ctx, ACCESS_EXT);
4606 t0 = tcg_temp_new();
4607 gen_addr_reg_index(ctx, t0);
fa407c03 4608 gen_check_align(ctx, t0, 0x03);
76db3ba4 4609 gen_qemu_ld32u(ctx, cpu_gpr[rD(ctx->opcode)], t0);
fa407c03 4610 tcg_temp_free(t0);
76a66253
JM
4611}
4612
4613/* ecowx */
99e300ef 4614static void gen_ecowx(DisasContext *ctx)
76a66253 4615{
76db3ba4 4616 TCGv t0;
fa407c03 4617 /* Should check EAR[E] ! */
76db3ba4
AJ
4618 gen_set_access_type(ctx, ACCESS_EXT);
4619 t0 = tcg_temp_new();
4620 gen_addr_reg_index(ctx, t0);
fa407c03 4621 gen_check_align(ctx, t0, 0x03);
76db3ba4 4622 gen_qemu_st32(ctx, cpu_gpr[rD(ctx->opcode)], t0);
fa407c03 4623 tcg_temp_free(t0);
76a66253
JM
4624}
4625
4626/* PowerPC 601 specific instructions */
99e300ef 4627
54623277 4628/* abs - abs. */
99e300ef 4629static void gen_abs(DisasContext *ctx)
76a66253 4630{
22e0e173
AJ
4631 int l1 = gen_new_label();
4632 int l2 = gen_new_label();
4633 tcg_gen_brcondi_tl(TCG_COND_GE, cpu_gpr[rA(ctx->opcode)], 0, l1);
4634 tcg_gen_neg_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)]);
4635 tcg_gen_br(l2);
4636 gen_set_label(l1);
4637 tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)]);
4638 gen_set_label(l2);
76a66253 4639 if (unlikely(Rc(ctx->opcode) != 0))
22e0e173 4640 gen_set_Rc0(ctx, cpu_gpr[rD(ctx->opcode)]);
76a66253
JM
4641}
4642
4643/* abso - abso. */
99e300ef 4644static void gen_abso(DisasContext *ctx)
76a66253 4645{
22e0e173
AJ
4646 int l1 = gen_new_label();
4647 int l2 = gen_new_label();
4648 int l3 = gen_new_label();
4649 /* Start with XER OV disabled, the most likely case */
da91a00f 4650 tcg_gen_movi_tl(cpu_ov, 0);
22e0e173
AJ
4651 tcg_gen_brcondi_tl(TCG_COND_GE, cpu_gpr[rA(ctx->opcode)], 0, l2);
4652 tcg_gen_brcondi_tl(TCG_COND_NE, cpu_gpr[rA(ctx->opcode)], 0x80000000, l1);
da91a00f
RH
4653 tcg_gen_movi_tl(cpu_ov, 1);
4654 tcg_gen_movi_tl(cpu_so, 1);
22e0e173
AJ
4655 tcg_gen_br(l2);
4656 gen_set_label(l1);
4657 tcg_gen_neg_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)]);
4658 tcg_gen_br(l3);
4659 gen_set_label(l2);
4660 tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)]);
4661 gen_set_label(l3);
76a66253 4662 if (unlikely(Rc(ctx->opcode) != 0))
22e0e173 4663 gen_set_Rc0(ctx, cpu_gpr[rD(ctx->opcode)]);
76a66253
JM
4664}
4665
4666/* clcs */
99e300ef 4667static void gen_clcs(DisasContext *ctx)
76a66253 4668{
22e0e173 4669 TCGv_i32 t0 = tcg_const_i32(rA(ctx->opcode));
d523dd00 4670 gen_helper_clcs(cpu_gpr[rD(ctx->opcode)], cpu_env, t0);
22e0e173 4671 tcg_temp_free_i32(t0);
c7697e1f 4672 /* Rc=1 sets CR0 to an undefined state */
76a66253
JM
4673}
4674
4675/* div - div. */
99e300ef 4676static void gen_div(DisasContext *ctx)
76a66253 4677{
d15f74fb
BS
4678 gen_helper_div(cpu_gpr[rD(ctx->opcode)], cpu_env, cpu_gpr[rA(ctx->opcode)],
4679 cpu_gpr[rB(ctx->opcode)]);
76a66253 4680 if (unlikely(Rc(ctx->opcode) != 0))
22e0e173 4681 gen_set_Rc0(ctx, cpu_gpr[rD(ctx->opcode)]);
76a66253
JM
4682}
4683
4684/* divo - divo. */
99e300ef 4685static void gen_divo(DisasContext *ctx)
76a66253 4686{
d15f74fb
BS
4687 gen_helper_divo(cpu_gpr[rD(ctx->opcode)], cpu_env, cpu_gpr[rA(ctx->opcode)],
4688 cpu_gpr[rB(ctx->opcode)]);
76a66253 4689 if (unlikely(Rc(ctx->opcode) != 0))
22e0e173 4690 gen_set_Rc0(ctx, cpu_gpr[rD(ctx->opcode)]);
76a66253
JM
4691}
4692
4693/* divs - divs. */
99e300ef 4694static void gen_divs(DisasContext *ctx)
76a66253 4695{
d15f74fb
BS
4696 gen_helper_divs(cpu_gpr[rD(ctx->opcode)], cpu_env, cpu_gpr[rA(ctx->opcode)],
4697 cpu_gpr[rB(ctx->opcode)]);
76a66253 4698 if (unlikely(Rc(ctx->opcode) != 0))
22e0e173 4699 gen_set_Rc0(ctx, cpu_gpr[rD(ctx->opcode)]);
76a66253
JM
4700}
4701
4702/* divso - divso. */
99e300ef 4703static void gen_divso(DisasContext *ctx)
76a66253 4704{
d15f74fb
BS
4705 gen_helper_divso(cpu_gpr[rD(ctx->opcode)], cpu_env,
4706 cpu_gpr[rA(ctx->opcode)], cpu_gpr[rB(ctx->opcode)]);
76a66253 4707 if (unlikely(Rc(ctx->opcode) != 0))
22e0e173 4708 gen_set_Rc0(ctx, cpu_gpr[rD(ctx->opcode)]);
76a66253
JM
4709}
4710
4711/* doz - doz. */
99e300ef 4712static void gen_doz(DisasContext *ctx)
76a66253 4713{
22e0e173
AJ
4714 int l1 = gen_new_label();
4715 int l2 = gen_new_label();
4716 tcg_gen_brcond_tl(TCG_COND_GE, cpu_gpr[rB(ctx->opcode)], cpu_gpr[rA(ctx->opcode)], l1);
4717 tcg_gen_sub_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rB(ctx->opcode)], cpu_gpr[rA(ctx->opcode)]);
4718 tcg_gen_br(l2);
4719 gen_set_label(l1);
4720 tcg_gen_movi_tl(cpu_gpr[rD(ctx->opcode)], 0);
4721 gen_set_label(l2);
76a66253 4722 if (unlikely(Rc(ctx->opcode) != 0))
22e0e173 4723 gen_set_Rc0(ctx, cpu_gpr[rD(ctx->opcode)]);
76a66253
JM
4724}
4725
4726/* dozo - dozo. */
99e300ef 4727static void gen_dozo(DisasContext *ctx)
76a66253 4728{
22e0e173
AJ
4729 int l1 = gen_new_label();
4730 int l2 = gen_new_label();
4731 TCGv t0 = tcg_temp_new();
4732 TCGv t1 = tcg_temp_new();
4733 TCGv t2 = tcg_temp_new();
4734 /* Start with XER OV disabled, the most likely case */
da91a00f 4735 tcg_gen_movi_tl(cpu_ov, 0);
22e0e173
AJ
4736 tcg_gen_brcond_tl(TCG_COND_GE, cpu_gpr[rB(ctx->opcode)], cpu_gpr[rA(ctx->opcode)], l1);
4737 tcg_gen_sub_tl(t0, cpu_gpr[rB(ctx->opcode)], cpu_gpr[rA(ctx->opcode)]);
4738 tcg_gen_xor_tl(t1, cpu_gpr[rB(ctx->opcode)], cpu_gpr[rA(ctx->opcode)]);
4739 tcg_gen_xor_tl(t2, cpu_gpr[rA(ctx->opcode)], t0);
4740 tcg_gen_andc_tl(t1, t1, t2);
4741 tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], t0);
4742 tcg_gen_brcondi_tl(TCG_COND_GE, t1, 0, l2);
da91a00f
RH
4743 tcg_gen_movi_tl(cpu_ov, 1);
4744 tcg_gen_movi_tl(cpu_so, 1);
22e0e173
AJ
4745 tcg_gen_br(l2);
4746 gen_set_label(l1);
4747 tcg_gen_movi_tl(cpu_gpr[rD(ctx->opcode)], 0);
4748 gen_set_label(l2);
4749 tcg_temp_free(t0);
4750 tcg_temp_free(t1);
4751 tcg_temp_free(t2);
76a66253 4752 if (unlikely(Rc(ctx->opcode) != 0))
22e0e173 4753 gen_set_Rc0(ctx, cpu_gpr[rD(ctx->opcode)]);
76a66253
JM
4754}
4755
4756/* dozi */
99e300ef 4757static void gen_dozi(DisasContext *ctx)
76a66253 4758{
22e0e173
AJ
4759 target_long simm = SIMM(ctx->opcode);
4760 int l1 = gen_new_label();
4761 int l2 = gen_new_label();
4762 tcg_gen_brcondi_tl(TCG_COND_LT, cpu_gpr[rA(ctx->opcode)], simm, l1);
4763 tcg_gen_subfi_tl(cpu_gpr[rD(ctx->opcode)], simm, cpu_gpr[rA(ctx->opcode)]);
4764 tcg_gen_br(l2);
4765 gen_set_label(l1);
4766 tcg_gen_movi_tl(cpu_gpr[rD(ctx->opcode)], 0);
4767 gen_set_label(l2);
4768 if (unlikely(Rc(ctx->opcode) != 0))
4769 gen_set_Rc0(ctx, cpu_gpr[rD(ctx->opcode)]);
76a66253
JM
4770}
4771
76a66253 4772/* lscbx - lscbx. */
99e300ef 4773static void gen_lscbx(DisasContext *ctx)
76a66253 4774{
bdb4b689
AJ
4775 TCGv t0 = tcg_temp_new();
4776 TCGv_i32 t1 = tcg_const_i32(rD(ctx->opcode));
4777 TCGv_i32 t2 = tcg_const_i32(rA(ctx->opcode));
4778 TCGv_i32 t3 = tcg_const_i32(rB(ctx->opcode));
76a66253 4779
76db3ba4 4780 gen_addr_reg_index(ctx, t0);
76a66253 4781 /* NIP cannot be restored if the memory exception comes from an helper */
d9bce9d9 4782 gen_update_nip(ctx, ctx->nip - 4);
2f5a189c 4783 gen_helper_lscbx(t0, cpu_env, t0, t1, t2, t3);
bdb4b689
AJ
4784 tcg_temp_free_i32(t1);
4785 tcg_temp_free_i32(t2);
4786 tcg_temp_free_i32(t3);
3d7b417e 4787 tcg_gen_andi_tl(cpu_xer, cpu_xer, ~0x7F);
bdb4b689 4788 tcg_gen_or_tl(cpu_xer, cpu_xer, t0);
76a66253 4789 if (unlikely(Rc(ctx->opcode) != 0))
bdb4b689
AJ
4790 gen_set_Rc0(ctx, t0);
4791 tcg_temp_free(t0);
76a66253
JM
4792}
4793
4794/* maskg - maskg. */
99e300ef 4795static void gen_maskg(DisasContext *ctx)
76a66253 4796{
22e0e173
AJ
4797 int l1 = gen_new_label();
4798 TCGv t0 = tcg_temp_new();
4799 TCGv t1 = tcg_temp_new();
4800 TCGv t2 = tcg_temp_new();
4801 TCGv t3 = tcg_temp_new();
4802 tcg_gen_movi_tl(t3, 0xFFFFFFFF);
4803 tcg_gen_andi_tl(t0, cpu_gpr[rB(ctx->opcode)], 0x1F);
4804 tcg_gen_andi_tl(t1, cpu_gpr[rS(ctx->opcode)], 0x1F);
4805 tcg_gen_addi_tl(t2, t0, 1);
4806 tcg_gen_shr_tl(t2, t3, t2);
4807 tcg_gen_shr_tl(t3, t3, t1);
4808 tcg_gen_xor_tl(cpu_gpr[rA(ctx->opcode)], t2, t3);
4809 tcg_gen_brcond_tl(TCG_COND_GE, t0, t1, l1);
4810 tcg_gen_neg_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rA(ctx->opcode)]);
4811 gen_set_label(l1);
4812 tcg_temp_free(t0);
4813 tcg_temp_free(t1);
4814 tcg_temp_free(t2);
4815 tcg_temp_free(t3);
76a66253 4816 if (unlikely(Rc(ctx->opcode) != 0))
22e0e173 4817 gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
76a66253
JM
4818}
4819
4820/* maskir - maskir. */
99e300ef 4821static void gen_maskir(DisasContext *ctx)
76a66253 4822{
22e0e173
AJ
4823 TCGv t0 = tcg_temp_new();
4824 TCGv t1 = tcg_temp_new();
4825 tcg_gen_and_tl(t0, cpu_gpr[rS(ctx->opcode)], cpu_gpr[rB(ctx->opcode)]);
4826 tcg_gen_andc_tl(t1, cpu_gpr[rA(ctx->opcode)], cpu_gpr[rB(ctx->opcode)]);
4827 tcg_gen_or_tl(cpu_gpr[rA(ctx->opcode)], t0, t1);
4828 tcg_temp_free(t0);
4829 tcg_temp_free(t1);
76a66253 4830 if (unlikely(Rc(ctx->opcode) != 0))
22e0e173 4831 gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
76a66253
JM
4832}
4833
4834/* mul - mul. */
99e300ef 4835static void gen_mul(DisasContext *ctx)
76a66253 4836{
22e0e173
AJ
4837 TCGv_i64 t0 = tcg_temp_new_i64();
4838 TCGv_i64 t1 = tcg_temp_new_i64();
4839 TCGv t2 = tcg_temp_new();
4840 tcg_gen_extu_tl_i64(t0, cpu_gpr[rA(ctx->opcode)]);
4841 tcg_gen_extu_tl_i64(t1, cpu_gpr[rB(ctx->opcode)]);
4842 tcg_gen_mul_i64(t0, t0, t1);
4843 tcg_gen_trunc_i64_tl(t2, t0);
4844 gen_store_spr(SPR_MQ, t2);
4845 tcg_gen_shri_i64(t1, t0, 32);
4846 tcg_gen_trunc_i64_tl(cpu_gpr[rD(ctx->opcode)], t1);
4847 tcg_temp_free_i64(t0);
4848 tcg_temp_free_i64(t1);
4849 tcg_temp_free(t2);
76a66253 4850 if (unlikely(Rc(ctx->opcode) != 0))
22e0e173 4851 gen_set_Rc0(ctx, cpu_gpr[rD(ctx->opcode)]);
76a66253
JM
4852}
4853
4854/* mulo - mulo. */
99e300ef 4855static void gen_mulo(DisasContext *ctx)
76a66253 4856{
22e0e173
AJ
4857 int l1 = gen_new_label();
4858 TCGv_i64 t0 = tcg_temp_new_i64();
4859 TCGv_i64 t1 = tcg_temp_new_i64();
4860 TCGv t2 = tcg_temp_new();
4861 /* Start with XER OV disabled, the most likely case */
da91a00f 4862 tcg_gen_movi_tl(cpu_ov, 0);
22e0e173
AJ
4863 tcg_gen_extu_tl_i64(t0, cpu_gpr[rA(ctx->opcode)]);
4864 tcg_gen_extu_tl_i64(t1, cpu_gpr[rB(ctx->opcode)]);
4865 tcg_gen_mul_i64(t0, t0, t1);
4866 tcg_gen_trunc_i64_tl(t2, t0);
4867 gen_store_spr(SPR_MQ, t2);
4868 tcg_gen_shri_i64(t1, t0, 32);
4869 tcg_gen_trunc_i64_tl(cpu_gpr[rD(ctx->opcode)], t1);
4870 tcg_gen_ext32s_i64(t1, t0);
4871 tcg_gen_brcond_i64(TCG_COND_EQ, t0, t1, l1);
da91a00f
RH
4872 tcg_gen_movi_tl(cpu_ov, 1);
4873 tcg_gen_movi_tl(cpu_so, 1);
22e0e173
AJ
4874 gen_set_label(l1);
4875 tcg_temp_free_i64(t0);
4876 tcg_temp_free_i64(t1);
4877 tcg_temp_free(t2);
76a66253 4878 if (unlikely(Rc(ctx->opcode) != 0))
22e0e173 4879 gen_set_Rc0(ctx, cpu_gpr[rD(ctx->opcode)]);
76a66253
JM
4880}
4881
4882/* nabs - nabs. */
99e300ef 4883static void gen_nabs(DisasContext *ctx)
76a66253 4884{
22e0e173
AJ
4885 int l1 = gen_new_label();
4886 int l2 = gen_new_label();
4887 tcg_gen_brcondi_tl(TCG_COND_GT, cpu_gpr[rA(ctx->opcode)], 0, l1);
4888 tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)]);
4889 tcg_gen_br(l2);
4890 gen_set_label(l1);
4891 tcg_gen_neg_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)]);
4892 gen_set_label(l2);
76a66253 4893 if (unlikely(Rc(ctx->opcode) != 0))
22e0e173 4894 gen_set_Rc0(ctx, cpu_gpr[rD(ctx->opcode)]);
76a66253
JM
4895}
4896
4897/* nabso - nabso. */
99e300ef 4898static void gen_nabso(DisasContext *ctx)
76a66253 4899{
22e0e173
AJ
4900 int l1 = gen_new_label();
4901 int l2 = gen_new_label();
4902 tcg_gen_brcondi_tl(TCG_COND_GT, cpu_gpr[rA(ctx->opcode)], 0, l1);
4903 tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)]);
4904 tcg_gen_br(l2);
4905 gen_set_label(l1);
4906 tcg_gen_neg_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)]);
4907 gen_set_label(l2);
4908 /* nabs never overflows */
da91a00f 4909 tcg_gen_movi_tl(cpu_ov, 0);
76a66253 4910 if (unlikely(Rc(ctx->opcode) != 0))
22e0e173 4911 gen_set_Rc0(ctx, cpu_gpr[rD(ctx->opcode)]);
76a66253
JM
4912}
4913
4914/* rlmi - rlmi. */
99e300ef 4915static void gen_rlmi(DisasContext *ctx)
76a66253 4916{
7487953d
AJ
4917 uint32_t mb = MB(ctx->opcode);
4918 uint32_t me = ME(ctx->opcode);
4919 TCGv t0 = tcg_temp_new();
4920 tcg_gen_andi_tl(t0, cpu_gpr[rB(ctx->opcode)], 0x1F);
4921 tcg_gen_rotl_tl(t0, cpu_gpr[rS(ctx->opcode)], t0);
4922 tcg_gen_andi_tl(t0, t0, MASK(mb, me));
4923 tcg_gen_andi_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rA(ctx->opcode)], ~MASK(mb, me));
4924 tcg_gen_or_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rA(ctx->opcode)], t0);
4925 tcg_temp_free(t0);
76a66253 4926 if (unlikely(Rc(ctx->opcode) != 0))
7487953d 4927 gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
76a66253
JM
4928}
4929
4930/* rrib - rrib. */
99e300ef 4931static void gen_rrib(DisasContext *ctx)
76a66253 4932{
7487953d
AJ
4933 TCGv t0 = tcg_temp_new();
4934 TCGv t1 = tcg_temp_new();
4935 tcg_gen_andi_tl(t0, cpu_gpr[rB(ctx->opcode)], 0x1F);
4936 tcg_gen_movi_tl(t1, 0x80000000);
4937 tcg_gen_shr_tl(t1, t1, t0);
4938 tcg_gen_shr_tl(t0, cpu_gpr[rS(ctx->opcode)], t0);
4939 tcg_gen_and_tl(t0, t0, t1);
4940 tcg_gen_andc_tl(t1, cpu_gpr[rA(ctx->opcode)], t1);
4941 tcg_gen_or_tl(cpu_gpr[rA(ctx->opcode)], t0, t1);
4942 tcg_temp_free(t0);
4943 tcg_temp_free(t1);
76a66253 4944 if (unlikely(Rc(ctx->opcode) != 0))
7487953d 4945 gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
76a66253
JM
4946}
4947
4948/* sle - sle. */
99e300ef 4949static void gen_sle(DisasContext *ctx)
76a66253 4950{
7487953d
AJ
4951 TCGv t0 = tcg_temp_new();
4952 TCGv t1 = tcg_temp_new();
4953 tcg_gen_andi_tl(t1, cpu_gpr[rB(ctx->opcode)], 0x1F);
4954 tcg_gen_shl_tl(t0, cpu_gpr[rS(ctx->opcode)], t1);
4955 tcg_gen_subfi_tl(t1, 32, t1);
4956 tcg_gen_shr_tl(t1, cpu_gpr[rS(ctx->opcode)], t1);
4957 tcg_gen_or_tl(t1, t0, t1);
4958 tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], t0);
4959 gen_store_spr(SPR_MQ, t1);
4960 tcg_temp_free(t0);
4961 tcg_temp_free(t1);
76a66253 4962 if (unlikely(Rc(ctx->opcode) != 0))
7487953d 4963 gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
76a66253
JM
4964}
4965
4966/* sleq - sleq. */
99e300ef 4967static void gen_sleq(DisasContext *ctx)
76a66253 4968{
7487953d
AJ
4969 TCGv t0 = tcg_temp_new();
4970 TCGv t1 = tcg_temp_new();
4971 TCGv t2 = tcg_temp_new();
4972 tcg_gen_andi_tl(t0, cpu_gpr[rB(ctx->opcode)], 0x1F);
4973 tcg_gen_movi_tl(t2, 0xFFFFFFFF);
4974 tcg_gen_shl_tl(t2, t2, t0);
4975 tcg_gen_rotl_tl(t0, cpu_gpr[rS(ctx->opcode)], t0);
4976 gen_load_spr(t1, SPR_MQ);
4977 gen_store_spr(SPR_MQ, t0);
4978 tcg_gen_and_tl(t0, t0, t2);
4979 tcg_gen_andc_tl(t1, t1, t2);
4980 tcg_gen_or_tl(cpu_gpr[rA(ctx->opcode)], t0, t1);
4981 tcg_temp_free(t0);
4982 tcg_temp_free(t1);
4983 tcg_temp_free(t2);
76a66253 4984 if (unlikely(Rc(ctx->opcode) != 0))
7487953d 4985 gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
76a66253
JM
4986}
4987
4988/* sliq - sliq. */
99e300ef 4989static void gen_sliq(DisasContext *ctx)
76a66253 4990{
7487953d
AJ
4991 int sh = SH(ctx->opcode);
4992 TCGv t0 = tcg_temp_new();
4993 TCGv t1 = tcg_temp_new();
4994 tcg_gen_shli_tl(t0, cpu_gpr[rS(ctx->opcode)], sh);
4995 tcg_gen_shri_tl(t1, cpu_gpr[rS(ctx->opcode)], 32 - sh);
4996 tcg_gen_or_tl(t1, t0, t1);
4997 tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], t0);
4998 gen_store_spr(SPR_MQ, t1);
4999 tcg_temp_free(t0);
5000 tcg_temp_free(t1);
76a66253 5001 if (unlikely(Rc(ctx->opcode) != 0))
7487953d 5002 gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
76a66253
JM
5003}
5004
5005/* slliq - slliq. */
99e300ef 5006static void gen_slliq(DisasContext *ctx)
76a66253 5007{
7487953d
AJ
5008 int sh = SH(ctx->opcode);
5009 TCGv t0 = tcg_temp_new();
5010 TCGv t1 = tcg_temp_new();
5011 tcg_gen_rotli_tl(t0, cpu_gpr[rS(ctx->opcode)], sh);
5012 gen_load_spr(t1, SPR_MQ);
5013 gen_store_spr(SPR_MQ, t0);
5014 tcg_gen_andi_tl(t0, t0, (0xFFFFFFFFU << sh));
5015 tcg_gen_andi_tl(t1, t1, ~(0xFFFFFFFFU << sh));
5016 tcg_gen_or_tl(cpu_gpr[rA(ctx->opcode)], t0, t1);
5017 tcg_temp_free(t0);
5018 tcg_temp_free(t1);
76a66253 5019 if (unlikely(Rc(ctx->opcode) != 0))
7487953d 5020 gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
76a66253
JM
5021}
5022
5023/* sllq - sllq. */
99e300ef 5024static void gen_sllq(DisasContext *ctx)
76a66253 5025{
7487953d
AJ
5026 int l1 = gen_new_label();
5027 int l2 = gen_new_label();
5028 TCGv t0 = tcg_temp_local_new();
5029 TCGv t1 = tcg_temp_local_new();
5030 TCGv t2 = tcg_temp_local_new();
5031 tcg_gen_andi_tl(t2, cpu_gpr[rB(ctx->opcode)], 0x1F);
5032 tcg_gen_movi_tl(t1, 0xFFFFFFFF);
5033 tcg_gen_shl_tl(t1, t1, t2);
5034 tcg_gen_andi_tl(t0, cpu_gpr[rB(ctx->opcode)], 0x20);
5035 tcg_gen_brcondi_tl(TCG_COND_EQ, t0, 0, l1);
5036 gen_load_spr(t0, SPR_MQ);
5037 tcg_gen_and_tl(cpu_gpr[rA(ctx->opcode)], t0, t1);
5038 tcg_gen_br(l2);
5039 gen_set_label(l1);
5040 tcg_gen_shl_tl(t0, cpu_gpr[rS(ctx->opcode)], t2);
5041 gen_load_spr(t2, SPR_MQ);
5042 tcg_gen_andc_tl(t1, t2, t1);
5043 tcg_gen_or_tl(cpu_gpr[rA(ctx->opcode)], t0, t1);
5044 gen_set_label(l2);
5045 tcg_temp_free(t0);
5046 tcg_temp_free(t1);
5047 tcg_temp_free(t2);
76a66253 5048 if (unlikely(Rc(ctx->opcode) != 0))
7487953d 5049 gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
76a66253
JM
5050}
5051
5052/* slq - slq. */
99e300ef 5053static void gen_slq(DisasContext *ctx)
76a66253 5054{
7487953d
AJ
5055 int l1 = gen_new_label();
5056 TCGv t0 = tcg_temp_new();
5057 TCGv t1 = tcg_temp_new();
5058 tcg_gen_andi_tl(t1, cpu_gpr[rB(ctx->opcode)], 0x1F);
5059 tcg_gen_shl_tl(t0, cpu_gpr[rS(ctx->opcode)], t1);
5060 tcg_gen_subfi_tl(t1, 32, t1);
5061 tcg_gen_shr_tl(t1, cpu_gpr[rS(ctx->opcode)], t1);
5062 tcg_gen_or_tl(t1, t0, t1);
5063 gen_store_spr(SPR_MQ, t1);
5064 tcg_gen_andi_tl(t1, cpu_gpr[rB(ctx->opcode)], 0x20);
5065 tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], t0);
5066 tcg_gen_brcondi_tl(TCG_COND_EQ, t1, 0, l1);
5067 tcg_gen_movi_tl(cpu_gpr[rA(ctx->opcode)], 0);
5068 gen_set_label(l1);
5069 tcg_temp_free(t0);
5070 tcg_temp_free(t1);
76a66253 5071 if (unlikely(Rc(ctx->opcode) != 0))
7487953d 5072 gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
76a66253
JM
5073}
5074
d9bce9d9 5075/* sraiq - sraiq. */
99e300ef 5076static void gen_sraiq(DisasContext *ctx)
76a66253 5077{
7487953d
AJ
5078 int sh = SH(ctx->opcode);
5079 int l1 = gen_new_label();
5080 TCGv t0 = tcg_temp_new();
5081 TCGv t1 = tcg_temp_new();
5082 tcg_gen_shri_tl(t0, cpu_gpr[rS(ctx->opcode)], sh);
5083 tcg_gen_shli_tl(t1, cpu_gpr[rS(ctx->opcode)], 32 - sh);
5084 tcg_gen_or_tl(t0, t0, t1);
5085 gen_store_spr(SPR_MQ, t0);
da91a00f 5086 tcg_gen_movi_tl(cpu_ca, 0);
7487953d
AJ
5087 tcg_gen_brcondi_tl(TCG_COND_EQ, t1, 0, l1);
5088 tcg_gen_brcondi_tl(TCG_COND_GE, cpu_gpr[rS(ctx->opcode)], 0, l1);
da91a00f 5089 tcg_gen_movi_tl(cpu_ca, 1);
7487953d
AJ
5090 gen_set_label(l1);
5091 tcg_gen_sari_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)], sh);
5092 tcg_temp_free(t0);
5093 tcg_temp_free(t1);
76a66253 5094 if (unlikely(Rc(ctx->opcode) != 0))
7487953d 5095 gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
76a66253
JM
5096}
5097
5098/* sraq - sraq. */
99e300ef 5099static void gen_sraq(DisasContext *ctx)
76a66253 5100{
7487953d
AJ
5101 int l1 = gen_new_label();
5102 int l2 = gen_new_label();
5103 TCGv t0 = tcg_temp_new();
5104 TCGv t1 = tcg_temp_local_new();
5105 TCGv t2 = tcg_temp_local_new();
5106 tcg_gen_andi_tl(t2, cpu_gpr[rB(ctx->opcode)], 0x1F);
5107 tcg_gen_shr_tl(t0, cpu_gpr[rS(ctx->opcode)], t2);
5108 tcg_gen_sar_tl(t1, cpu_gpr[rS(ctx->opcode)], t2);
5109 tcg_gen_subfi_tl(t2, 32, t2);
5110 tcg_gen_shl_tl(t2, cpu_gpr[rS(ctx->opcode)], t2);
5111 tcg_gen_or_tl(t0, t0, t2);
5112 gen_store_spr(SPR_MQ, t0);
5113 tcg_gen_andi_tl(t0, cpu_gpr[rB(ctx->opcode)], 0x20);
5114 tcg_gen_brcondi_tl(TCG_COND_EQ, t2, 0, l1);
5115 tcg_gen_mov_tl(t2, cpu_gpr[rS(ctx->opcode)]);
5116 tcg_gen_sari_tl(t1, cpu_gpr[rS(ctx->opcode)], 31);
5117 gen_set_label(l1);
5118 tcg_temp_free(t0);
5119 tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], t1);
da91a00f 5120 tcg_gen_movi_tl(cpu_ca, 0);
7487953d
AJ
5121 tcg_gen_brcondi_tl(TCG_COND_GE, t1, 0, l2);
5122 tcg_gen_brcondi_tl(TCG_COND_EQ, t2, 0, l2);
da91a00f 5123 tcg_gen_movi_tl(cpu_ca, 1);
7487953d
AJ
5124 gen_set_label(l2);
5125 tcg_temp_free(t1);
5126 tcg_temp_free(t2);
76a66253 5127 if (unlikely(Rc(ctx->opcode) != 0))
7487953d 5128 gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
76a66253
JM
5129}
5130
5131/* sre - sre. */
99e300ef 5132static void gen_sre(DisasContext *ctx)
76a66253 5133{
7487953d
AJ
5134 TCGv t0 = tcg_temp_new();
5135 TCGv t1 = tcg_temp_new();
5136 tcg_gen_andi_tl(t1, cpu_gpr[rB(ctx->opcode)], 0x1F);
5137 tcg_gen_shr_tl(t0, cpu_gpr[rS(ctx->opcode)], t1);
5138 tcg_gen_subfi_tl(t1, 32, t1);
5139 tcg_gen_shl_tl(t1, cpu_gpr[rS(ctx->opcode)], t1);
5140 tcg_gen_or_tl(t1, t0, t1);
5141 tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], t0);
5142 gen_store_spr(SPR_MQ, t1);
5143 tcg_temp_free(t0);
5144 tcg_temp_free(t1);
76a66253 5145 if (unlikely(Rc(ctx->opcode) != 0))
7487953d 5146 gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
76a66253
JM
5147}
5148
5149/* srea - srea. */
99e300ef 5150static void gen_srea(DisasContext *ctx)
76a66253 5151{
7487953d
AJ
5152 TCGv t0 = tcg_temp_new();
5153 TCGv t1 = tcg_temp_new();
5154 tcg_gen_andi_tl(t1, cpu_gpr[rB(ctx->opcode)], 0x1F);
5155 tcg_gen_rotr_tl(t0, cpu_gpr[rS(ctx->opcode)], t1);
5156 gen_store_spr(SPR_MQ, t0);
5157 tcg_gen_sar_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)], t1);
5158 tcg_temp_free(t0);
5159 tcg_temp_free(t1);
76a66253 5160 if (unlikely(Rc(ctx->opcode) != 0))
7487953d 5161 gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
76a66253
JM
5162}
5163
5164/* sreq */
99e300ef 5165static void gen_sreq(DisasContext *ctx)
76a66253 5166{
7487953d
AJ
5167 TCGv t0 = tcg_temp_new();
5168 TCGv t1 = tcg_temp_new();
5169 TCGv t2 = tcg_temp_new();
5170 tcg_gen_andi_tl(t0, cpu_gpr[rB(ctx->opcode)], 0x1F);
5171 tcg_gen_movi_tl(t1, 0xFFFFFFFF);
5172 tcg_gen_shr_tl(t1, t1, t0);
5173 tcg_gen_rotr_tl(t0, cpu_gpr[rS(ctx->opcode)], t0);
5174 gen_load_spr(t2, SPR_MQ);
5175 gen_store_spr(SPR_MQ, t0);
5176 tcg_gen_and_tl(t0, t0, t1);
5177 tcg_gen_andc_tl(t2, t2, t1);
5178 tcg_gen_or_tl(cpu_gpr[rA(ctx->opcode)], t0, t2);
5179 tcg_temp_free(t0);
5180 tcg_temp_free(t1);
5181 tcg_temp_free(t2);
76a66253 5182 if (unlikely(Rc(ctx->opcode) != 0))
7487953d 5183 gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
76a66253
JM
5184}
5185
5186/* sriq */
99e300ef 5187static void gen_sriq(DisasContext *ctx)
76a66253 5188{
7487953d
AJ
5189 int sh = SH(ctx->opcode);
5190 TCGv t0 = tcg_temp_new();
5191 TCGv t1 = tcg_temp_new();
5192 tcg_gen_shri_tl(t0, cpu_gpr[rS(ctx->opcode)], sh);
5193 tcg_gen_shli_tl(t1, cpu_gpr[rS(ctx->opcode)], 32 - sh);
5194 tcg_gen_or_tl(t1, t0, t1);
5195 tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], t0);
5196 gen_store_spr(SPR_MQ, t1);
5197 tcg_temp_free(t0);
5198 tcg_temp_free(t1);
76a66253 5199 if (unlikely(Rc(ctx->opcode) != 0))
7487953d 5200 gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
76a66253
JM
5201}
5202
5203/* srliq */
99e300ef 5204static void gen_srliq(DisasContext *ctx)
76a66253 5205{
7487953d
AJ
5206 int sh = SH(ctx->opcode);
5207 TCGv t0 = tcg_temp_new();
5208 TCGv t1 = tcg_temp_new();
5209 tcg_gen_rotri_tl(t0, cpu_gpr[rS(ctx->opcode)], sh);
5210 gen_load_spr(t1, SPR_MQ);
5211 gen_store_spr(SPR_MQ, t0);
5212 tcg_gen_andi_tl(t0, t0, (0xFFFFFFFFU >> sh));
5213 tcg_gen_andi_tl(t1, t1, ~(0xFFFFFFFFU >> sh));
5214 tcg_gen_or_tl(cpu_gpr[rA(ctx->opcode)], t0, t1);
5215 tcg_temp_free(t0);
5216 tcg_temp_free(t1);
76a66253 5217 if (unlikely(Rc(ctx->opcode) != 0))
7487953d 5218 gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
76a66253
JM
5219}
5220
5221/* srlq */
99e300ef 5222static void gen_srlq(DisasContext *ctx)
76a66253 5223{
7487953d
AJ
5224 int l1 = gen_new_label();
5225 int l2 = gen_new_label();
5226 TCGv t0 = tcg_temp_local_new();
5227 TCGv t1 = tcg_temp_local_new();
5228 TCGv t2 = tcg_temp_local_new();
5229 tcg_gen_andi_tl(t2, cpu_gpr[rB(ctx->opcode)], 0x1F);
5230 tcg_gen_movi_tl(t1, 0xFFFFFFFF);
5231 tcg_gen_shr_tl(t2, t1, t2);
5232 tcg_gen_andi_tl(t0, cpu_gpr[rB(ctx->opcode)], 0x20);
5233 tcg_gen_brcondi_tl(TCG_COND_EQ, t0, 0, l1);
5234 gen_load_spr(t0, SPR_MQ);
5235 tcg_gen_and_tl(cpu_gpr[rA(ctx->opcode)], t0, t2);
5236 tcg_gen_br(l2);
5237 gen_set_label(l1);
5238 tcg_gen_shr_tl(t0, cpu_gpr[rS(ctx->opcode)], t2);
5239 tcg_gen_and_tl(t0, t0, t2);
5240 gen_load_spr(t1, SPR_MQ);
5241 tcg_gen_andc_tl(t1, t1, t2);
5242 tcg_gen_or_tl(cpu_gpr[rA(ctx->opcode)], t0, t1);
5243 gen_set_label(l2);
5244 tcg_temp_free(t0);
5245 tcg_temp_free(t1);
5246 tcg_temp_free(t2);
76a66253 5247 if (unlikely(Rc(ctx->opcode) != 0))
7487953d 5248 gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
76a66253
JM
5249}
5250
5251/* srq */
99e300ef 5252static void gen_srq(DisasContext *ctx)
76a66253 5253{
7487953d
AJ
5254 int l1 = gen_new_label();
5255 TCGv t0 = tcg_temp_new();
5256 TCGv t1 = tcg_temp_new();
5257 tcg_gen_andi_tl(t1, cpu_gpr[rB(ctx->opcode)], 0x1F);
5258 tcg_gen_shr_tl(t0, cpu_gpr[rS(ctx->opcode)], t1);
5259 tcg_gen_subfi_tl(t1, 32, t1);
5260 tcg_gen_shl_tl(t1, cpu_gpr[rS(ctx->opcode)], t1);
5261 tcg_gen_or_tl(t1, t0, t1);
5262 gen_store_spr(SPR_MQ, t1);
5263 tcg_gen_andi_tl(t1, cpu_gpr[rB(ctx->opcode)], 0x20);
5264 tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], t0);
5265 tcg_gen_brcondi_tl(TCG_COND_EQ, t0, 0, l1);
5266 tcg_gen_movi_tl(cpu_gpr[rA(ctx->opcode)], 0);
5267 gen_set_label(l1);
5268 tcg_temp_free(t0);
5269 tcg_temp_free(t1);
76a66253 5270 if (unlikely(Rc(ctx->opcode) != 0))
7487953d 5271 gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
76a66253
JM
5272}
5273
5274/* PowerPC 602 specific instructions */
99e300ef 5275
54623277 5276/* dsa */
99e300ef 5277static void gen_dsa(DisasContext *ctx)
76a66253
JM
5278{
5279 /* XXX: TODO */
e06fcd75 5280 gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL);
76a66253
JM
5281}
5282
5283/* esa */
99e300ef 5284static void gen_esa(DisasContext *ctx)
76a66253
JM
5285{
5286 /* XXX: TODO */
e06fcd75 5287 gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL);
76a66253
JM
5288}
5289
5290/* mfrom */
99e300ef 5291static void gen_mfrom(DisasContext *ctx)
76a66253
JM
5292{
5293#if defined(CONFIG_USER_ONLY)
e06fcd75 5294 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
76a66253 5295#else
76db3ba4 5296 if (unlikely(!ctx->mem_idx)) {
e06fcd75 5297 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
76a66253
JM
5298 return;
5299 }
cf02a65c 5300 gen_helper_602_mfrom(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)]);
76a66253
JM
5301#endif
5302}
5303
5304/* 602 - 603 - G2 TLB management */
e8eaa2c0 5305
54623277 5306/* tlbld */
e8eaa2c0 5307static void gen_tlbld_6xx(DisasContext *ctx)
76a66253
JM
5308{
5309#if defined(CONFIG_USER_ONLY)
e06fcd75 5310 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
76a66253 5311#else
76db3ba4 5312 if (unlikely(!ctx->mem_idx)) {
e06fcd75 5313 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
76a66253
JM
5314 return;
5315 }
c6c7cf05 5316 gen_helper_6xx_tlbd(cpu_env, cpu_gpr[rB(ctx->opcode)]);
76a66253
JM
5317#endif
5318}
5319
5320/* tlbli */
e8eaa2c0 5321static void gen_tlbli_6xx(DisasContext *ctx)
76a66253
JM
5322{
5323#if defined(CONFIG_USER_ONLY)
e06fcd75 5324 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
76a66253 5325#else
76db3ba4 5326 if (unlikely(!ctx->mem_idx)) {
e06fcd75 5327 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
76a66253
JM
5328 return;
5329 }
c6c7cf05 5330 gen_helper_6xx_tlbi(cpu_env, cpu_gpr[rB(ctx->opcode)]);
76a66253
JM
5331#endif
5332}
5333
7dbe11ac 5334/* 74xx TLB management */
e8eaa2c0 5335
54623277 5336/* tlbld */
e8eaa2c0 5337static void gen_tlbld_74xx(DisasContext *ctx)
7dbe11ac
JM
5338{
5339#if defined(CONFIG_USER_ONLY)
e06fcd75 5340 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
7dbe11ac 5341#else
76db3ba4 5342 if (unlikely(!ctx->mem_idx)) {
e06fcd75 5343 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
7dbe11ac
JM
5344 return;
5345 }
c6c7cf05 5346 gen_helper_74xx_tlbd(cpu_env, cpu_gpr[rB(ctx->opcode)]);
7dbe11ac
JM
5347#endif
5348}
5349
5350/* tlbli */
e8eaa2c0 5351static void gen_tlbli_74xx(DisasContext *ctx)
7dbe11ac
JM
5352{
5353#if defined(CONFIG_USER_ONLY)
e06fcd75 5354 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
7dbe11ac 5355#else
76db3ba4 5356 if (unlikely(!ctx->mem_idx)) {
e06fcd75 5357 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
7dbe11ac
JM
5358 return;
5359 }
c6c7cf05 5360 gen_helper_74xx_tlbi(cpu_env, cpu_gpr[rB(ctx->opcode)]);
7dbe11ac
JM
5361#endif
5362}
5363
76a66253 5364/* POWER instructions not in PowerPC 601 */
99e300ef 5365
54623277 5366/* clf */
99e300ef 5367static void gen_clf(DisasContext *ctx)
76a66253
JM
5368{
5369 /* Cache line flush: implemented as no-op */
5370}
5371
5372/* cli */
99e300ef 5373static void gen_cli(DisasContext *ctx)
76a66253 5374{
7f75ffd3 5375 /* Cache line invalidate: privileged and treated as no-op */
76a66253 5376#if defined(CONFIG_USER_ONLY)
e06fcd75 5377 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
76a66253 5378#else
76db3ba4 5379 if (unlikely(!ctx->mem_idx)) {
e06fcd75 5380 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
76a66253
JM
5381 return;
5382 }
5383#endif
5384}
5385
5386/* dclst */
99e300ef 5387static void gen_dclst(DisasContext *ctx)
76a66253
JM
5388{
5389 /* Data cache line store: treated as no-op */
5390}
5391
99e300ef 5392static void gen_mfsri(DisasContext *ctx)
76a66253
JM
5393{
5394#if defined(CONFIG_USER_ONLY)
e06fcd75 5395 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
76a66253 5396#else
74d37793
AJ
5397 int ra = rA(ctx->opcode);
5398 int rd = rD(ctx->opcode);
5399 TCGv t0;
76db3ba4 5400 if (unlikely(!ctx->mem_idx)) {
e06fcd75 5401 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
76a66253
JM
5402 return;
5403 }
74d37793 5404 t0 = tcg_temp_new();
76db3ba4 5405 gen_addr_reg_index(ctx, t0);
74d37793
AJ
5406 tcg_gen_shri_tl(t0, t0, 28);
5407 tcg_gen_andi_tl(t0, t0, 0xF);
c6c7cf05 5408 gen_helper_load_sr(cpu_gpr[rd], cpu_env, t0);
74d37793 5409 tcg_temp_free(t0);
76a66253 5410 if (ra != 0 && ra != rd)
74d37793 5411 tcg_gen_mov_tl(cpu_gpr[ra], cpu_gpr[rd]);
76a66253
JM
5412#endif
5413}
5414
99e300ef 5415static void gen_rac(DisasContext *ctx)
76a66253
JM
5416{
5417#if defined(CONFIG_USER_ONLY)
e06fcd75 5418 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
76a66253 5419#else
22e0e173 5420 TCGv t0;
76db3ba4 5421 if (unlikely(!ctx->mem_idx)) {
e06fcd75 5422 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
76a66253
JM
5423 return;
5424 }
22e0e173 5425 t0 = tcg_temp_new();
76db3ba4 5426 gen_addr_reg_index(ctx, t0);
c6c7cf05 5427 gen_helper_rac(cpu_gpr[rD(ctx->opcode)], cpu_env, t0);
22e0e173 5428 tcg_temp_free(t0);
76a66253
JM
5429#endif
5430}
5431
99e300ef 5432static void gen_rfsvc(DisasContext *ctx)
76a66253
JM
5433{
5434#if defined(CONFIG_USER_ONLY)
e06fcd75 5435 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
76a66253 5436#else
76db3ba4 5437 if (unlikely(!ctx->mem_idx)) {
e06fcd75 5438 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
76a66253
JM
5439 return;
5440 }
e5f17ac6 5441 gen_helper_rfsvc(cpu_env);
e06fcd75 5442 gen_sync_exception(ctx);
76a66253
JM
5443#endif
5444}
5445
5446/* svc is not implemented for now */
5447
5448/* POWER2 specific instructions */
5449/* Quad manipulation (load/store two floats at a time) */
76a66253
JM
5450
5451/* lfq */
99e300ef 5452static void gen_lfq(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_imm_index(ctx, t0, 0);
5459 gen_qemu_ld64(ctx, cpu_fpr[rd], t0);
5460 gen_addr_add(ctx, t0, t0, 8);
5461 gen_qemu_ld64(ctx, cpu_fpr[(rd + 1) % 32], t0);
01a4afeb 5462 tcg_temp_free(t0);
76a66253
JM
5463}
5464
5465/* lfqu */
99e300ef 5466static void gen_lfqu(DisasContext *ctx)
76a66253
JM
5467{
5468 int ra = rA(ctx->opcode);
01a4afeb 5469 int rd = rD(ctx->opcode);
76db3ba4
AJ
5470 TCGv t0, t1;
5471 gen_set_access_type(ctx, ACCESS_FLOAT);
5472 t0 = tcg_temp_new();
5473 t1 = tcg_temp_new();
5474 gen_addr_imm_index(ctx, t0, 0);
5475 gen_qemu_ld64(ctx, cpu_fpr[rd], t0);
5476 gen_addr_add(ctx, t1, t0, 8);
5477 gen_qemu_ld64(ctx, cpu_fpr[(rd + 1) % 32], t1);
76a66253 5478 if (ra != 0)
01a4afeb
AJ
5479 tcg_gen_mov_tl(cpu_gpr[ra], t0);
5480 tcg_temp_free(t0);
5481 tcg_temp_free(t1);
76a66253
JM
5482}
5483
5484/* lfqux */
99e300ef 5485static void gen_lfqux(DisasContext *ctx)
76a66253
JM
5486{
5487 int ra = rA(ctx->opcode);
01a4afeb 5488 int rd = rD(ctx->opcode);
76db3ba4
AJ
5489 gen_set_access_type(ctx, ACCESS_FLOAT);
5490 TCGv t0, t1;
5491 t0 = tcg_temp_new();
5492 gen_addr_reg_index(ctx, t0);
5493 gen_qemu_ld64(ctx, cpu_fpr[rd], t0);
5494 t1 = tcg_temp_new();
5495 gen_addr_add(ctx, t1, t0, 8);
5496 gen_qemu_ld64(ctx, cpu_fpr[(rd + 1) % 32], t1);
5497 tcg_temp_free(t1);
76a66253 5498 if (ra != 0)
01a4afeb
AJ
5499 tcg_gen_mov_tl(cpu_gpr[ra], t0);
5500 tcg_temp_free(t0);
76a66253
JM
5501}
5502
5503/* lfqx */
99e300ef 5504static void gen_lfqx(DisasContext *ctx)
76a66253 5505{
01a4afeb 5506 int rd = rD(ctx->opcode);
76db3ba4
AJ
5507 TCGv t0;
5508 gen_set_access_type(ctx, ACCESS_FLOAT);
5509 t0 = tcg_temp_new();
5510 gen_addr_reg_index(ctx, t0);
5511 gen_qemu_ld64(ctx, cpu_fpr[rd], t0);
5512 gen_addr_add(ctx, t0, t0, 8);
5513 gen_qemu_ld64(ctx, cpu_fpr[(rd + 1) % 32], t0);
01a4afeb 5514 tcg_temp_free(t0);
76a66253
JM
5515}
5516
5517/* stfq */
99e300ef 5518static void gen_stfq(DisasContext *ctx)
76a66253 5519{
01a4afeb 5520 int rd = rD(ctx->opcode);
76db3ba4
AJ
5521 TCGv t0;
5522 gen_set_access_type(ctx, ACCESS_FLOAT);
5523 t0 = tcg_temp_new();
5524 gen_addr_imm_index(ctx, t0, 0);
5525 gen_qemu_st64(ctx, cpu_fpr[rd], t0);
5526 gen_addr_add(ctx, t0, t0, 8);
5527 gen_qemu_st64(ctx, cpu_fpr[(rd + 1) % 32], t0);
01a4afeb 5528 tcg_temp_free(t0);
76a66253
JM
5529}
5530
5531/* stfqu */
99e300ef 5532static void gen_stfqu(DisasContext *ctx)
76a66253
JM
5533{
5534 int ra = rA(ctx->opcode);
01a4afeb 5535 int rd = rD(ctx->opcode);
76db3ba4
AJ
5536 TCGv t0, t1;
5537 gen_set_access_type(ctx, ACCESS_FLOAT);
5538 t0 = tcg_temp_new();
5539 gen_addr_imm_index(ctx, t0, 0);
5540 gen_qemu_st64(ctx, cpu_fpr[rd], t0);
5541 t1 = tcg_temp_new();
5542 gen_addr_add(ctx, t1, t0, 8);
5543 gen_qemu_st64(ctx, cpu_fpr[(rd + 1) % 32], t1);
5544 tcg_temp_free(t1);
76a66253 5545 if (ra != 0)
01a4afeb
AJ
5546 tcg_gen_mov_tl(cpu_gpr[ra], t0);
5547 tcg_temp_free(t0);
76a66253
JM
5548}
5549
5550/* stfqux */
99e300ef 5551static void gen_stfqux(DisasContext *ctx)
76a66253
JM
5552{
5553 int ra = rA(ctx->opcode);
01a4afeb 5554 int rd = rD(ctx->opcode);
76db3ba4
AJ
5555 TCGv t0, t1;
5556 gen_set_access_type(ctx, ACCESS_FLOAT);
5557 t0 = tcg_temp_new();
5558 gen_addr_reg_index(ctx, t0);
5559 gen_qemu_st64(ctx, cpu_fpr[rd], t0);
5560 t1 = tcg_temp_new();
5561 gen_addr_add(ctx, t1, t0, 8);
5562 gen_qemu_st64(ctx, cpu_fpr[(rd + 1) % 32], t1);
5563 tcg_temp_free(t1);
76a66253 5564 if (ra != 0)
01a4afeb
AJ
5565 tcg_gen_mov_tl(cpu_gpr[ra], t0);
5566 tcg_temp_free(t0);
76a66253
JM
5567}
5568
5569/* stfqx */
99e300ef 5570static void gen_stfqx(DisasContext *ctx)
76a66253 5571{
01a4afeb 5572 int rd = rD(ctx->opcode);
76db3ba4
AJ
5573 TCGv t0;
5574 gen_set_access_type(ctx, ACCESS_FLOAT);
5575 t0 = tcg_temp_new();
5576 gen_addr_reg_index(ctx, t0);
5577 gen_qemu_st64(ctx, cpu_fpr[rd], t0);
5578 gen_addr_add(ctx, t0, t0, 8);
5579 gen_qemu_st64(ctx, cpu_fpr[(rd + 1) % 32], t0);
01a4afeb 5580 tcg_temp_free(t0);
76a66253
JM
5581}
5582
5583/* BookE specific instructions */
99e300ef 5584
54623277 5585/* XXX: not implemented on 440 ? */
99e300ef 5586static void gen_mfapidi(DisasContext *ctx)
76a66253
JM
5587{
5588 /* XXX: TODO */
e06fcd75 5589 gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL);
76a66253
JM
5590}
5591
2662a059 5592/* XXX: not implemented on 440 ? */
99e300ef 5593static void gen_tlbiva(DisasContext *ctx)
76a66253
JM
5594{
5595#if defined(CONFIG_USER_ONLY)
e06fcd75 5596 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
76a66253 5597#else
74d37793 5598 TCGv t0;
76db3ba4 5599 if (unlikely(!ctx->mem_idx)) {
e06fcd75 5600 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
76a66253
JM
5601 return;
5602 }
ec72e276 5603 t0 = tcg_temp_new();
76db3ba4 5604 gen_addr_reg_index(ctx, t0);
c6c7cf05 5605 gen_helper_tlbie(cpu_env, cpu_gpr[rB(ctx->opcode)]);
74d37793 5606 tcg_temp_free(t0);
76a66253
JM
5607#endif
5608}
5609
5610/* All 405 MAC instructions are translated here */
636aa200
BS
5611static inline void gen_405_mulladd_insn(DisasContext *ctx, int opc2, int opc3,
5612 int ra, int rb, int rt, int Rc)
76a66253 5613{
182608d4
AJ
5614 TCGv t0, t1;
5615
a7812ae4
PB
5616 t0 = tcg_temp_local_new();
5617 t1 = tcg_temp_local_new();
182608d4 5618
76a66253
JM
5619 switch (opc3 & 0x0D) {
5620 case 0x05:
5621 /* macchw - macchw. - macchwo - macchwo. */
5622 /* macchws - macchws. - macchwso - macchwso. */
5623 /* nmacchw - nmacchw. - nmacchwo - nmacchwo. */
5624 /* nmacchws - nmacchws. - nmacchwso - nmacchwso. */
5625 /* mulchw - mulchw. */
182608d4
AJ
5626 tcg_gen_ext16s_tl(t0, cpu_gpr[ra]);
5627 tcg_gen_sari_tl(t1, cpu_gpr[rb], 16);
5628 tcg_gen_ext16s_tl(t1, t1);
76a66253
JM
5629 break;
5630 case 0x04:
5631 /* macchwu - macchwu. - macchwuo - macchwuo. */
5632 /* macchwsu - macchwsu. - macchwsuo - macchwsuo. */
5633 /* mulchwu - mulchwu. */
182608d4
AJ
5634 tcg_gen_ext16u_tl(t0, cpu_gpr[ra]);
5635 tcg_gen_shri_tl(t1, cpu_gpr[rb], 16);
5636 tcg_gen_ext16u_tl(t1, t1);
76a66253
JM
5637 break;
5638 case 0x01:
5639 /* machhw - machhw. - machhwo - machhwo. */
5640 /* machhws - machhws. - machhwso - machhwso. */
5641 /* nmachhw - nmachhw. - nmachhwo - nmachhwo. */
5642 /* nmachhws - nmachhws. - nmachhwso - nmachhwso. */
5643 /* mulhhw - mulhhw. */
182608d4
AJ
5644 tcg_gen_sari_tl(t0, cpu_gpr[ra], 16);
5645 tcg_gen_ext16s_tl(t0, t0);
5646 tcg_gen_sari_tl(t1, cpu_gpr[rb], 16);
5647 tcg_gen_ext16s_tl(t1, t1);
76a66253
JM
5648 break;
5649 case 0x00:
5650 /* machhwu - machhwu. - machhwuo - machhwuo. */
5651 /* machhwsu - machhwsu. - machhwsuo - machhwsuo. */
5652 /* mulhhwu - mulhhwu. */
182608d4
AJ
5653 tcg_gen_shri_tl(t0, cpu_gpr[ra], 16);
5654 tcg_gen_ext16u_tl(t0, t0);
5655 tcg_gen_shri_tl(t1, cpu_gpr[rb], 16);
5656 tcg_gen_ext16u_tl(t1, t1);
76a66253
JM
5657 break;
5658 case 0x0D:
5659 /* maclhw - maclhw. - maclhwo - maclhwo. */
5660 /* maclhws - maclhws. - maclhwso - maclhwso. */
5661 /* nmaclhw - nmaclhw. - nmaclhwo - nmaclhwo. */
5662 /* nmaclhws - nmaclhws. - nmaclhwso - nmaclhwso. */
5663 /* mullhw - mullhw. */
182608d4
AJ
5664 tcg_gen_ext16s_tl(t0, cpu_gpr[ra]);
5665 tcg_gen_ext16s_tl(t1, cpu_gpr[rb]);
76a66253
JM
5666 break;
5667 case 0x0C:
5668 /* maclhwu - maclhwu. - maclhwuo - maclhwuo. */
5669 /* maclhwsu - maclhwsu. - maclhwsuo - maclhwsuo. */
5670 /* mullhwu - mullhwu. */
182608d4
AJ
5671 tcg_gen_ext16u_tl(t0, cpu_gpr[ra]);
5672 tcg_gen_ext16u_tl(t1, cpu_gpr[rb]);
76a66253
JM
5673 break;
5674 }
76a66253 5675 if (opc2 & 0x04) {
182608d4
AJ
5676 /* (n)multiply-and-accumulate (0x0C / 0x0E) */
5677 tcg_gen_mul_tl(t1, t0, t1);
5678 if (opc2 & 0x02) {
5679 /* nmultiply-and-accumulate (0x0E) */
5680 tcg_gen_sub_tl(t0, cpu_gpr[rt], t1);
5681 } else {
5682 /* multiply-and-accumulate (0x0C) */
5683 tcg_gen_add_tl(t0, cpu_gpr[rt], t1);
5684 }
5685
5686 if (opc3 & 0x12) {
5687 /* Check overflow and/or saturate */
5688 int l1 = gen_new_label();
5689
5690 if (opc3 & 0x10) {
5691 /* Start with XER OV disabled, the most likely case */
da91a00f 5692 tcg_gen_movi_tl(cpu_ov, 0);
182608d4
AJ
5693 }
5694 if (opc3 & 0x01) {
5695 /* Signed */
5696 tcg_gen_xor_tl(t1, cpu_gpr[rt], t1);
5697 tcg_gen_brcondi_tl(TCG_COND_GE, t1, 0, l1);
5698 tcg_gen_xor_tl(t1, cpu_gpr[rt], t0);
5699 tcg_gen_brcondi_tl(TCG_COND_LT, t1, 0, l1);
bdc4e053 5700 if (opc3 & 0x02) {
182608d4
AJ
5701 /* Saturate */
5702 tcg_gen_sari_tl(t0, cpu_gpr[rt], 31);
5703 tcg_gen_xori_tl(t0, t0, 0x7fffffff);
5704 }
5705 } else {
5706 /* Unsigned */
5707 tcg_gen_brcond_tl(TCG_COND_GEU, t0, t1, l1);
bdc4e053 5708 if (opc3 & 0x02) {
182608d4
AJ
5709 /* Saturate */
5710 tcg_gen_movi_tl(t0, UINT32_MAX);
5711 }
5712 }
5713 if (opc3 & 0x10) {
5714 /* Check overflow */
da91a00f
RH
5715 tcg_gen_movi_tl(cpu_ov, 1);
5716 tcg_gen_movi_tl(cpu_so, 1);
182608d4
AJ
5717 }
5718 gen_set_label(l1);
5719 tcg_gen_mov_tl(cpu_gpr[rt], t0);
5720 }
5721 } else {
5722 tcg_gen_mul_tl(cpu_gpr[rt], t0, t1);
76a66253 5723 }
182608d4
AJ
5724 tcg_temp_free(t0);
5725 tcg_temp_free(t1);
76a66253
JM
5726 if (unlikely(Rc) != 0) {
5727 /* Update Rc0 */
182608d4 5728 gen_set_Rc0(ctx, cpu_gpr[rt]);
76a66253
JM
5729 }
5730}
5731
a750fc0b 5732#define GEN_MAC_HANDLER(name, opc2, opc3) \
99e300ef 5733static void glue(gen_, name)(DisasContext *ctx) \
76a66253
JM
5734{ \
5735 gen_405_mulladd_insn(ctx, opc2, opc3, rA(ctx->opcode), rB(ctx->opcode), \
5736 rD(ctx->opcode), Rc(ctx->opcode)); \
5737}
5738
5739/* macchw - macchw. */
a750fc0b 5740GEN_MAC_HANDLER(macchw, 0x0C, 0x05);
76a66253 5741/* macchwo - macchwo. */
a750fc0b 5742GEN_MAC_HANDLER(macchwo, 0x0C, 0x15);
76a66253 5743/* macchws - macchws. */
a750fc0b 5744GEN_MAC_HANDLER(macchws, 0x0C, 0x07);
76a66253 5745/* macchwso - macchwso. */
a750fc0b 5746GEN_MAC_HANDLER(macchwso, 0x0C, 0x17);
76a66253 5747/* macchwsu - macchwsu. */
a750fc0b 5748GEN_MAC_HANDLER(macchwsu, 0x0C, 0x06);
76a66253 5749/* macchwsuo - macchwsuo. */
a750fc0b 5750GEN_MAC_HANDLER(macchwsuo, 0x0C, 0x16);
76a66253 5751/* macchwu - macchwu. */
a750fc0b 5752GEN_MAC_HANDLER(macchwu, 0x0C, 0x04);
76a66253 5753/* macchwuo - macchwuo. */
a750fc0b 5754GEN_MAC_HANDLER(macchwuo, 0x0C, 0x14);
76a66253 5755/* machhw - machhw. */
a750fc0b 5756GEN_MAC_HANDLER(machhw, 0x0C, 0x01);
76a66253 5757/* machhwo - machhwo. */
a750fc0b 5758GEN_MAC_HANDLER(machhwo, 0x0C, 0x11);
76a66253 5759/* machhws - machhws. */
a750fc0b 5760GEN_MAC_HANDLER(machhws, 0x0C, 0x03);
76a66253 5761/* machhwso - machhwso. */
a750fc0b 5762GEN_MAC_HANDLER(machhwso, 0x0C, 0x13);
76a66253 5763/* machhwsu - machhwsu. */
a750fc0b 5764GEN_MAC_HANDLER(machhwsu, 0x0C, 0x02);
76a66253 5765/* machhwsuo - machhwsuo. */
a750fc0b 5766GEN_MAC_HANDLER(machhwsuo, 0x0C, 0x12);
76a66253 5767/* machhwu - machhwu. */
a750fc0b 5768GEN_MAC_HANDLER(machhwu, 0x0C, 0x00);
76a66253 5769/* machhwuo - machhwuo. */
a750fc0b 5770GEN_MAC_HANDLER(machhwuo, 0x0C, 0x10);
76a66253 5771/* maclhw - maclhw. */
a750fc0b 5772GEN_MAC_HANDLER(maclhw, 0x0C, 0x0D);
76a66253 5773/* maclhwo - maclhwo. */
a750fc0b 5774GEN_MAC_HANDLER(maclhwo, 0x0C, 0x1D);
76a66253 5775/* maclhws - maclhws. */
a750fc0b 5776GEN_MAC_HANDLER(maclhws, 0x0C, 0x0F);
76a66253 5777/* maclhwso - maclhwso. */
a750fc0b 5778GEN_MAC_HANDLER(maclhwso, 0x0C, 0x1F);
76a66253 5779/* maclhwu - maclhwu. */
a750fc0b 5780GEN_MAC_HANDLER(maclhwu, 0x0C, 0x0C);
76a66253 5781/* maclhwuo - maclhwuo. */
a750fc0b 5782GEN_MAC_HANDLER(maclhwuo, 0x0C, 0x1C);
76a66253 5783/* maclhwsu - maclhwsu. */
a750fc0b 5784GEN_MAC_HANDLER(maclhwsu, 0x0C, 0x0E);
76a66253 5785/* maclhwsuo - maclhwsuo. */
a750fc0b 5786GEN_MAC_HANDLER(maclhwsuo, 0x0C, 0x1E);
76a66253 5787/* nmacchw - nmacchw. */
a750fc0b 5788GEN_MAC_HANDLER(nmacchw, 0x0E, 0x05);
76a66253 5789/* nmacchwo - nmacchwo. */
a750fc0b 5790GEN_MAC_HANDLER(nmacchwo, 0x0E, 0x15);
76a66253 5791/* nmacchws - nmacchws. */
a750fc0b 5792GEN_MAC_HANDLER(nmacchws, 0x0E, 0x07);
76a66253 5793/* nmacchwso - nmacchwso. */
a750fc0b 5794GEN_MAC_HANDLER(nmacchwso, 0x0E, 0x17);
76a66253 5795/* nmachhw - nmachhw. */
a750fc0b 5796GEN_MAC_HANDLER(nmachhw, 0x0E, 0x01);
76a66253 5797/* nmachhwo - nmachhwo. */
a750fc0b 5798GEN_MAC_HANDLER(nmachhwo, 0x0E, 0x11);
76a66253 5799/* nmachhws - nmachhws. */
a750fc0b 5800GEN_MAC_HANDLER(nmachhws, 0x0E, 0x03);
76a66253 5801/* nmachhwso - nmachhwso. */
a750fc0b 5802GEN_MAC_HANDLER(nmachhwso, 0x0E, 0x13);
76a66253 5803/* nmaclhw - nmaclhw. */
a750fc0b 5804GEN_MAC_HANDLER(nmaclhw, 0x0E, 0x0D);
76a66253 5805/* nmaclhwo - nmaclhwo. */
a750fc0b 5806GEN_MAC_HANDLER(nmaclhwo, 0x0E, 0x1D);
76a66253 5807/* nmaclhws - nmaclhws. */
a750fc0b 5808GEN_MAC_HANDLER(nmaclhws, 0x0E, 0x0F);
76a66253 5809/* nmaclhwso - nmaclhwso. */
a750fc0b 5810GEN_MAC_HANDLER(nmaclhwso, 0x0E, 0x1F);
76a66253
JM
5811
5812/* mulchw - mulchw. */
a750fc0b 5813GEN_MAC_HANDLER(mulchw, 0x08, 0x05);
76a66253 5814/* mulchwu - mulchwu. */
a750fc0b 5815GEN_MAC_HANDLER(mulchwu, 0x08, 0x04);
76a66253 5816/* mulhhw - mulhhw. */
a750fc0b 5817GEN_MAC_HANDLER(mulhhw, 0x08, 0x01);
76a66253 5818/* mulhhwu - mulhhwu. */
a750fc0b 5819GEN_MAC_HANDLER(mulhhwu, 0x08, 0x00);
76a66253 5820/* mullhw - mullhw. */
a750fc0b 5821GEN_MAC_HANDLER(mullhw, 0x08, 0x0D);
76a66253 5822/* mullhwu - mullhwu. */
a750fc0b 5823GEN_MAC_HANDLER(mullhwu, 0x08, 0x0C);
76a66253
JM
5824
5825/* mfdcr */
99e300ef 5826static void gen_mfdcr(DisasContext *ctx)
76a66253
JM
5827{
5828#if defined(CONFIG_USER_ONLY)
e06fcd75 5829 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
76a66253 5830#else
06dca6a7 5831 TCGv dcrn;
76db3ba4 5832 if (unlikely(!ctx->mem_idx)) {
e06fcd75 5833 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
76a66253
JM
5834 return;
5835 }
06dca6a7
AJ
5836 /* NIP cannot be restored if the memory exception comes from an helper */
5837 gen_update_nip(ctx, ctx->nip - 4);
5838 dcrn = tcg_const_tl(SPR(ctx->opcode));
d0f1562d 5839 gen_helper_load_dcr(cpu_gpr[rD(ctx->opcode)], cpu_env, dcrn);
06dca6a7 5840 tcg_temp_free(dcrn);
76a66253
JM
5841#endif
5842}
5843
5844/* mtdcr */
99e300ef 5845static void gen_mtdcr(DisasContext *ctx)
76a66253
JM
5846{
5847#if defined(CONFIG_USER_ONLY)
e06fcd75 5848 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
76a66253 5849#else
06dca6a7 5850 TCGv dcrn;
76db3ba4 5851 if (unlikely(!ctx->mem_idx)) {
e06fcd75 5852 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
76a66253
JM
5853 return;
5854 }
06dca6a7
AJ
5855 /* NIP cannot be restored if the memory exception comes from an helper */
5856 gen_update_nip(ctx, ctx->nip - 4);
5857 dcrn = tcg_const_tl(SPR(ctx->opcode));
d0f1562d 5858 gen_helper_store_dcr(cpu_env, dcrn, cpu_gpr[rS(ctx->opcode)]);
06dca6a7 5859 tcg_temp_free(dcrn);
a42bd6cc
JM
5860#endif
5861}
5862
5863/* mfdcrx */
2662a059 5864/* XXX: not implemented on 440 ? */
99e300ef 5865static void gen_mfdcrx(DisasContext *ctx)
a42bd6cc
JM
5866{
5867#if defined(CONFIG_USER_ONLY)
e06fcd75 5868 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
a42bd6cc 5869#else
76db3ba4 5870 if (unlikely(!ctx->mem_idx)) {
e06fcd75 5871 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
a42bd6cc
JM
5872 return;
5873 }
06dca6a7
AJ
5874 /* NIP cannot be restored if the memory exception comes from an helper */
5875 gen_update_nip(ctx, ctx->nip - 4);
d0f1562d
BS
5876 gen_helper_load_dcr(cpu_gpr[rD(ctx->opcode)], cpu_env,
5877 cpu_gpr[rA(ctx->opcode)]);
a750fc0b 5878 /* Note: Rc update flag set leads to undefined state of Rc0 */
a42bd6cc
JM
5879#endif
5880}
5881
5882/* mtdcrx */
2662a059 5883/* XXX: not implemented on 440 ? */
99e300ef 5884static void gen_mtdcrx(DisasContext *ctx)
a42bd6cc
JM
5885{
5886#if defined(CONFIG_USER_ONLY)
e06fcd75 5887 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
a42bd6cc 5888#else
76db3ba4 5889 if (unlikely(!ctx->mem_idx)) {
e06fcd75 5890 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
a42bd6cc
JM
5891 return;
5892 }
06dca6a7
AJ
5893 /* NIP cannot be restored if the memory exception comes from an helper */
5894 gen_update_nip(ctx, ctx->nip - 4);
d0f1562d
BS
5895 gen_helper_store_dcr(cpu_env, cpu_gpr[rA(ctx->opcode)],
5896 cpu_gpr[rS(ctx->opcode)]);
a750fc0b 5897 /* Note: Rc update flag set leads to undefined state of Rc0 */
76a66253
JM
5898#endif
5899}
5900
a750fc0b 5901/* mfdcrux (PPC 460) : user-mode access to DCR */
99e300ef 5902static void gen_mfdcrux(DisasContext *ctx)
a750fc0b 5903{
06dca6a7
AJ
5904 /* NIP cannot be restored if the memory exception comes from an helper */
5905 gen_update_nip(ctx, ctx->nip - 4);
d0f1562d
BS
5906 gen_helper_load_dcr(cpu_gpr[rD(ctx->opcode)], cpu_env,
5907 cpu_gpr[rA(ctx->opcode)]);
a750fc0b
JM
5908 /* Note: Rc update flag set leads to undefined state of Rc0 */
5909}
5910
5911/* mtdcrux (PPC 460) : user-mode access to DCR */
99e300ef 5912static void gen_mtdcrux(DisasContext *ctx)
a750fc0b 5913{
06dca6a7
AJ
5914 /* NIP cannot be restored if the memory exception comes from an helper */
5915 gen_update_nip(ctx, ctx->nip - 4);
975e5463 5916 gen_helper_store_dcr(cpu_env, cpu_gpr[rA(ctx->opcode)],
d0f1562d 5917 cpu_gpr[rS(ctx->opcode)]);
a750fc0b
JM
5918 /* Note: Rc update flag set leads to undefined state of Rc0 */
5919}
5920
76a66253 5921/* dccci */
99e300ef 5922static void gen_dccci(DisasContext *ctx)
76a66253
JM
5923{
5924#if defined(CONFIG_USER_ONLY)
e06fcd75 5925 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
76a66253 5926#else
76db3ba4 5927 if (unlikely(!ctx->mem_idx)) {
e06fcd75 5928 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
76a66253
JM
5929 return;
5930 }
5931 /* interpreted as no-op */
5932#endif
5933}
5934
5935/* dcread */
99e300ef 5936static void gen_dcread(DisasContext *ctx)
76a66253
JM
5937{
5938#if defined(CONFIG_USER_ONLY)
e06fcd75 5939 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
76a66253 5940#else
b61f2753 5941 TCGv EA, val;
76db3ba4 5942 if (unlikely(!ctx->mem_idx)) {
e06fcd75 5943 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
76a66253
JM
5944 return;
5945 }
76db3ba4 5946 gen_set_access_type(ctx, ACCESS_CACHE);
a7812ae4 5947 EA = tcg_temp_new();
76db3ba4 5948 gen_addr_reg_index(ctx, EA);
a7812ae4 5949 val = tcg_temp_new();
76db3ba4 5950 gen_qemu_ld32u(ctx, val, EA);
b61f2753
AJ
5951 tcg_temp_free(val);
5952 tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], EA);
5953 tcg_temp_free(EA);
76a66253
JM
5954#endif
5955}
5956
5957/* icbt */
e8eaa2c0 5958static void gen_icbt_40x(DisasContext *ctx)
76a66253
JM
5959{
5960 /* interpreted as no-op */
5961 /* XXX: specification say this is treated as a load by the MMU
5962 * but does not generate any exception
5963 */
5964}
5965
5966/* iccci */
99e300ef 5967static void gen_iccci(DisasContext *ctx)
76a66253
JM
5968{
5969#if defined(CONFIG_USER_ONLY)
e06fcd75 5970 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
76a66253 5971#else
76db3ba4 5972 if (unlikely(!ctx->mem_idx)) {
e06fcd75 5973 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
76a66253
JM
5974 return;
5975 }
5976 /* interpreted as no-op */
5977#endif
5978}
5979
5980/* icread */
99e300ef 5981static void gen_icread(DisasContext *ctx)
76a66253
JM
5982{
5983#if defined(CONFIG_USER_ONLY)
e06fcd75 5984 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
76a66253 5985#else
76db3ba4 5986 if (unlikely(!ctx->mem_idx)) {
e06fcd75 5987 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
76a66253
JM
5988 return;
5989 }
5990 /* interpreted as no-op */
5991#endif
5992}
5993
76db3ba4 5994/* rfci (mem_idx only) */
e8eaa2c0 5995static void gen_rfci_40x(DisasContext *ctx)
a42bd6cc
JM
5996{
5997#if defined(CONFIG_USER_ONLY)
e06fcd75 5998 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
a42bd6cc 5999#else
76db3ba4 6000 if (unlikely(!ctx->mem_idx)) {
e06fcd75 6001 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
a42bd6cc
JM
6002 return;
6003 }
6004 /* Restore CPU state */
e5f17ac6 6005 gen_helper_40x_rfci(cpu_env);
e06fcd75 6006 gen_sync_exception(ctx);
a42bd6cc
JM
6007#endif
6008}
6009
99e300ef 6010static void gen_rfci(DisasContext *ctx)
a42bd6cc
JM
6011{
6012#if defined(CONFIG_USER_ONLY)
e06fcd75 6013 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
a42bd6cc 6014#else
76db3ba4 6015 if (unlikely(!ctx->mem_idx)) {
e06fcd75 6016 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
a42bd6cc
JM
6017 return;
6018 }
6019 /* Restore CPU state */
e5f17ac6 6020 gen_helper_rfci(cpu_env);
e06fcd75 6021 gen_sync_exception(ctx);
a42bd6cc
JM
6022#endif
6023}
6024
6025/* BookE specific */
99e300ef 6026
54623277 6027/* XXX: not implemented on 440 ? */
99e300ef 6028static void gen_rfdi(DisasContext *ctx)
76a66253
JM
6029{
6030#if defined(CONFIG_USER_ONLY)
e06fcd75 6031 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
76a66253 6032#else
76db3ba4 6033 if (unlikely(!ctx->mem_idx)) {
e06fcd75 6034 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
76a66253
JM
6035 return;
6036 }
6037 /* Restore CPU state */
e5f17ac6 6038 gen_helper_rfdi(cpu_env);
e06fcd75 6039 gen_sync_exception(ctx);
76a66253
JM
6040#endif
6041}
6042
2662a059 6043/* XXX: not implemented on 440 ? */
99e300ef 6044static void gen_rfmci(DisasContext *ctx)
a42bd6cc
JM
6045{
6046#if defined(CONFIG_USER_ONLY)
e06fcd75 6047 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
a42bd6cc 6048#else
76db3ba4 6049 if (unlikely(!ctx->mem_idx)) {
e06fcd75 6050 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
a42bd6cc
JM
6051 return;
6052 }
6053 /* Restore CPU state */
e5f17ac6 6054 gen_helper_rfmci(cpu_env);
e06fcd75 6055 gen_sync_exception(ctx);
a42bd6cc
JM
6056#endif
6057}
5eb7995e 6058
d9bce9d9 6059/* TLB management - PowerPC 405 implementation */
e8eaa2c0 6060
54623277 6061/* tlbre */
e8eaa2c0 6062static void gen_tlbre_40x(DisasContext *ctx)
76a66253
JM
6063{
6064#if defined(CONFIG_USER_ONLY)
e06fcd75 6065 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
76a66253 6066#else
76db3ba4 6067 if (unlikely(!ctx->mem_idx)) {
e06fcd75 6068 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
76a66253
JM
6069 return;
6070 }
6071 switch (rB(ctx->opcode)) {
6072 case 0:
c6c7cf05
BS
6073 gen_helper_4xx_tlbre_hi(cpu_gpr[rD(ctx->opcode)], cpu_env,
6074 cpu_gpr[rA(ctx->opcode)]);
76a66253
JM
6075 break;
6076 case 1:
c6c7cf05
BS
6077 gen_helper_4xx_tlbre_lo(cpu_gpr[rD(ctx->opcode)], cpu_env,
6078 cpu_gpr[rA(ctx->opcode)]);
76a66253
JM
6079 break;
6080 default:
e06fcd75 6081 gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL);
76a66253 6082 break;
9a64fbe4 6083 }
76a66253
JM
6084#endif
6085}
6086
d9bce9d9 6087/* tlbsx - tlbsx. */
e8eaa2c0 6088static void gen_tlbsx_40x(DisasContext *ctx)
76a66253
JM
6089{
6090#if defined(CONFIG_USER_ONLY)
e06fcd75 6091 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
76a66253 6092#else
74d37793 6093 TCGv t0;
76db3ba4 6094 if (unlikely(!ctx->mem_idx)) {
e06fcd75 6095 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
76a66253
JM
6096 return;
6097 }
74d37793 6098 t0 = tcg_temp_new();
76db3ba4 6099 gen_addr_reg_index(ctx, t0);
c6c7cf05 6100 gen_helper_4xx_tlbsx(cpu_gpr[rD(ctx->opcode)], cpu_env, t0);
74d37793
AJ
6101 tcg_temp_free(t0);
6102 if (Rc(ctx->opcode)) {
6103 int l1 = gen_new_label();
da91a00f 6104 tcg_gen_trunc_tl_i32(cpu_crf[0], cpu_so);
74d37793
AJ
6105 tcg_gen_brcondi_tl(TCG_COND_EQ, cpu_gpr[rD(ctx->opcode)], -1, l1);
6106 tcg_gen_ori_i32(cpu_crf[0], cpu_crf[0], 0x02);
6107 gen_set_label(l1);
6108 }
76a66253 6109#endif
79aceca5
FB
6110}
6111
76a66253 6112/* tlbwe */
e8eaa2c0 6113static void gen_tlbwe_40x(DisasContext *ctx)
79aceca5 6114{
76a66253 6115#if defined(CONFIG_USER_ONLY)
e06fcd75 6116 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
76a66253 6117#else
76db3ba4 6118 if (unlikely(!ctx->mem_idx)) {
e06fcd75 6119 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
76a66253
JM
6120 return;
6121 }
6122 switch (rB(ctx->opcode)) {
6123 case 0:
c6c7cf05
BS
6124 gen_helper_4xx_tlbwe_hi(cpu_env, cpu_gpr[rA(ctx->opcode)],
6125 cpu_gpr[rS(ctx->opcode)]);
76a66253
JM
6126 break;
6127 case 1:
c6c7cf05
BS
6128 gen_helper_4xx_tlbwe_lo(cpu_env, cpu_gpr[rA(ctx->opcode)],
6129 cpu_gpr[rS(ctx->opcode)]);
76a66253
JM
6130 break;
6131 default:
e06fcd75 6132 gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL);
76a66253 6133 break;
9a64fbe4 6134 }
76a66253
JM
6135#endif
6136}
6137
a4bb6c3e 6138/* TLB management - PowerPC 440 implementation */
e8eaa2c0 6139
54623277 6140/* tlbre */
e8eaa2c0 6141static void gen_tlbre_440(DisasContext *ctx)
5eb7995e
JM
6142{
6143#if defined(CONFIG_USER_ONLY)
e06fcd75 6144 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
5eb7995e 6145#else
76db3ba4 6146 if (unlikely(!ctx->mem_idx)) {
e06fcd75 6147 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
5eb7995e
JM
6148 return;
6149 }
6150 switch (rB(ctx->opcode)) {
6151 case 0:
5eb7995e 6152 case 1:
5eb7995e 6153 case 2:
74d37793
AJ
6154 {
6155 TCGv_i32 t0 = tcg_const_i32(rB(ctx->opcode));
c6c7cf05
BS
6156 gen_helper_440_tlbre(cpu_gpr[rD(ctx->opcode)], cpu_env,
6157 t0, cpu_gpr[rA(ctx->opcode)]);
74d37793
AJ
6158 tcg_temp_free_i32(t0);
6159 }
5eb7995e
JM
6160 break;
6161 default:
e06fcd75 6162 gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL);
5eb7995e
JM
6163 break;
6164 }
6165#endif
6166}
6167
6168/* tlbsx - tlbsx. */
e8eaa2c0 6169static void gen_tlbsx_440(DisasContext *ctx)
5eb7995e
JM
6170{
6171#if defined(CONFIG_USER_ONLY)
e06fcd75 6172 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
5eb7995e 6173#else
74d37793 6174 TCGv t0;
76db3ba4 6175 if (unlikely(!ctx->mem_idx)) {
e06fcd75 6176 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
5eb7995e
JM
6177 return;
6178 }
74d37793 6179 t0 = tcg_temp_new();
76db3ba4 6180 gen_addr_reg_index(ctx, t0);
c6c7cf05 6181 gen_helper_440_tlbsx(cpu_gpr[rD(ctx->opcode)], cpu_env, t0);
74d37793
AJ
6182 tcg_temp_free(t0);
6183 if (Rc(ctx->opcode)) {
6184 int l1 = gen_new_label();
da91a00f 6185 tcg_gen_trunc_tl_i32(cpu_crf[0], cpu_so);
74d37793
AJ
6186 tcg_gen_brcondi_tl(TCG_COND_EQ, cpu_gpr[rD(ctx->opcode)], -1, l1);
6187 tcg_gen_ori_i32(cpu_crf[0], cpu_crf[0], 0x02);
6188 gen_set_label(l1);
6189 }
5eb7995e
JM
6190#endif
6191}
6192
6193/* tlbwe */
e8eaa2c0 6194static void gen_tlbwe_440(DisasContext *ctx)
5eb7995e
JM
6195{
6196#if defined(CONFIG_USER_ONLY)
e06fcd75 6197 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
5eb7995e 6198#else
76db3ba4 6199 if (unlikely(!ctx->mem_idx)) {
e06fcd75 6200 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
5eb7995e
JM
6201 return;
6202 }
6203 switch (rB(ctx->opcode)) {
6204 case 0:
5eb7995e 6205 case 1:
5eb7995e 6206 case 2:
74d37793
AJ
6207 {
6208 TCGv_i32 t0 = tcg_const_i32(rB(ctx->opcode));
c6c7cf05
BS
6209 gen_helper_440_tlbwe(cpu_env, t0, cpu_gpr[rA(ctx->opcode)],
6210 cpu_gpr[rS(ctx->opcode)]);
74d37793
AJ
6211 tcg_temp_free_i32(t0);
6212 }
5eb7995e
JM
6213 break;
6214 default:
e06fcd75 6215 gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL);
5eb7995e
JM
6216 break;
6217 }
6218#endif
6219}
6220
01662f3e
AG
6221/* TLB management - PowerPC BookE 2.06 implementation */
6222
6223/* tlbre */
6224static void gen_tlbre_booke206(DisasContext *ctx)
6225{
6226#if defined(CONFIG_USER_ONLY)
6227 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
6228#else
6229 if (unlikely(!ctx->mem_idx)) {
6230 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
6231 return;
6232 }
6233
c6c7cf05 6234 gen_helper_booke206_tlbre(cpu_env);
01662f3e
AG
6235#endif
6236}
6237
6238/* tlbsx - tlbsx. */
6239static void gen_tlbsx_booke206(DisasContext *ctx)
6240{
6241#if defined(CONFIG_USER_ONLY)
6242 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
6243#else
6244 TCGv t0;
6245 if (unlikely(!ctx->mem_idx)) {
6246 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
6247 return;
6248 }
6249
6250 if (rA(ctx->opcode)) {
6251 t0 = tcg_temp_new();
6252 tcg_gen_mov_tl(t0, cpu_gpr[rD(ctx->opcode)]);
6253 } else {
6254 t0 = tcg_const_tl(0);
6255 }
6256
6257 tcg_gen_add_tl(t0, t0, cpu_gpr[rB(ctx->opcode)]);
c6c7cf05 6258 gen_helper_booke206_tlbsx(cpu_env, t0);
01662f3e
AG
6259#endif
6260}
6261
6262/* tlbwe */
6263static void gen_tlbwe_booke206(DisasContext *ctx)
6264{
6265#if defined(CONFIG_USER_ONLY)
6266 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
6267#else
6268 if (unlikely(!ctx->mem_idx)) {
6269 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
6270 return;
6271 }
3f162d11 6272 gen_update_nip(ctx, ctx->nip - 4);
c6c7cf05 6273 gen_helper_booke206_tlbwe(cpu_env);
01662f3e
AG
6274#endif
6275}
6276
6277static void gen_tlbivax_booke206(DisasContext *ctx)
6278{
6279#if defined(CONFIG_USER_ONLY)
6280 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
6281#else
6282 TCGv t0;
6283 if (unlikely(!ctx->mem_idx)) {
6284 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
6285 return;
6286 }
6287
6288 t0 = tcg_temp_new();
6289 gen_addr_reg_index(ctx, t0);
6290
c6c7cf05 6291 gen_helper_booke206_tlbivax(cpu_env, t0);
01662f3e
AG
6292#endif
6293}
6294
6d3db821
AG
6295static void gen_tlbilx_booke206(DisasContext *ctx)
6296{
6297#if defined(CONFIG_USER_ONLY)
6298 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
6299#else
6300 TCGv t0;
6301 if (unlikely(!ctx->mem_idx)) {
6302 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
6303 return;
6304 }
6305
6306 t0 = tcg_temp_new();
6307 gen_addr_reg_index(ctx, t0);
6308
6309 switch((ctx->opcode >> 21) & 0x3) {
6310 case 0:
c6c7cf05 6311 gen_helper_booke206_tlbilx0(cpu_env, t0);
6d3db821
AG
6312 break;
6313 case 1:
c6c7cf05 6314 gen_helper_booke206_tlbilx1(cpu_env, t0);
6d3db821
AG
6315 break;
6316 case 3:
c6c7cf05 6317 gen_helper_booke206_tlbilx3(cpu_env, t0);
6d3db821
AG
6318 break;
6319 default:
6320 gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL);
6321 break;
6322 }
6323
6324 tcg_temp_free(t0);
6325#endif
6326}
6327
01662f3e 6328
76a66253 6329/* wrtee */
99e300ef 6330static void gen_wrtee(DisasContext *ctx)
76a66253
JM
6331{
6332#if defined(CONFIG_USER_ONLY)
e06fcd75 6333 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
76a66253 6334#else
6527f6ea 6335 TCGv t0;
76db3ba4 6336 if (unlikely(!ctx->mem_idx)) {
e06fcd75 6337 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
76a66253
JM
6338 return;
6339 }
6527f6ea
AJ
6340 t0 = tcg_temp_new();
6341 tcg_gen_andi_tl(t0, cpu_gpr[rD(ctx->opcode)], (1 << MSR_EE));
6342 tcg_gen_andi_tl(cpu_msr, cpu_msr, ~(1 << MSR_EE));
6343 tcg_gen_or_tl(cpu_msr, cpu_msr, t0);
6344 tcg_temp_free(t0);
dee96f6c
JM
6345 /* Stop translation to have a chance to raise an exception
6346 * if we just set msr_ee to 1
6347 */
e06fcd75 6348 gen_stop_exception(ctx);
76a66253
JM
6349#endif
6350}
6351
6352/* wrteei */
99e300ef 6353static void gen_wrteei(DisasContext *ctx)
76a66253
JM
6354{
6355#if defined(CONFIG_USER_ONLY)
e06fcd75 6356 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
76a66253 6357#else
76db3ba4 6358 if (unlikely(!ctx->mem_idx)) {
e06fcd75 6359 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
76a66253
JM
6360 return;
6361 }
fbe73008 6362 if (ctx->opcode & 0x00008000) {
6527f6ea
AJ
6363 tcg_gen_ori_tl(cpu_msr, cpu_msr, (1 << MSR_EE));
6364 /* Stop translation to have a chance to raise an exception */
e06fcd75 6365 gen_stop_exception(ctx);
6527f6ea 6366 } else {
1b6e5f99 6367 tcg_gen_andi_tl(cpu_msr, cpu_msr, ~(1 << MSR_EE));
6527f6ea 6368 }
76a66253
JM
6369#endif
6370}
6371
08e46e54 6372/* PowerPC 440 specific instructions */
99e300ef 6373
54623277 6374/* dlmzb */
99e300ef 6375static void gen_dlmzb(DisasContext *ctx)
76a66253 6376{
ef0d51af 6377 TCGv_i32 t0 = tcg_const_i32(Rc(ctx->opcode));
d15f74fb
BS
6378 gen_helper_dlmzb(cpu_gpr[rA(ctx->opcode)], cpu_env,
6379 cpu_gpr[rS(ctx->opcode)], cpu_gpr[rB(ctx->opcode)], t0);
ef0d51af 6380 tcg_temp_free_i32(t0);
76a66253
JM
6381}
6382
6383/* mbar replaces eieio on 440 */
99e300ef 6384static void gen_mbar(DisasContext *ctx)
76a66253
JM
6385{
6386 /* interpreted as no-op */
6387}
6388
6389/* msync replaces sync on 440 */
dcb2b9e1 6390static void gen_msync_4xx(DisasContext *ctx)
76a66253
JM
6391{
6392 /* interpreted as no-op */
6393}
6394
6395/* icbt */
e8eaa2c0 6396static void gen_icbt_440(DisasContext *ctx)
76a66253
JM
6397{
6398 /* interpreted as no-op */
6399 /* XXX: specification say this is treated as a load by the MMU
6400 * but does not generate any exception
6401 */
79aceca5
FB
6402}
6403
9e0b5cb1
AG
6404/* Embedded.Processor Control */
6405
6406static void gen_msgclr(DisasContext *ctx)
6407{
6408#if defined(CONFIG_USER_ONLY)
6409 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
6410#else
6411 if (unlikely(ctx->mem_idx == 0)) {
6412 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
6413 return;
6414 }
6415
e5f17ac6 6416 gen_helper_msgclr(cpu_env, cpu_gpr[rB(ctx->opcode)]);
9e0b5cb1
AG
6417#endif
6418}
6419
d5d11a39
AG
6420static void gen_msgsnd(DisasContext *ctx)
6421{
6422#if defined(CONFIG_USER_ONLY)
6423 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
6424#else
6425 if (unlikely(ctx->mem_idx == 0)) {
6426 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
6427 return;
6428 }
6429
6430 gen_helper_msgsnd(cpu_gpr[rB(ctx->opcode)]);
6431#endif
6432}
6433
a9d9eb8f
JM
6434/*** Altivec vector extension ***/
6435/* Altivec registers moves */
a9d9eb8f 6436
636aa200 6437static inline TCGv_ptr gen_avr_ptr(int reg)
564e571a 6438{
e4704b3b 6439 TCGv_ptr r = tcg_temp_new_ptr();
564e571a
AJ
6440 tcg_gen_addi_ptr(r, cpu_env, offsetof(CPUPPCState, avr[reg]));
6441 return r;
6442}
6443
a9d9eb8f 6444#define GEN_VR_LDX(name, opc2, opc3) \
99e300ef 6445static void glue(gen_, name)(DisasContext *ctx) \
a9d9eb8f 6446{ \
fe1e5c53 6447 TCGv EA; \
a9d9eb8f 6448 if (unlikely(!ctx->altivec_enabled)) { \
e06fcd75 6449 gen_exception(ctx, POWERPC_EXCP_VPU); \
a9d9eb8f
JM
6450 return; \
6451 } \
76db3ba4 6452 gen_set_access_type(ctx, ACCESS_INT); \
fe1e5c53 6453 EA = tcg_temp_new(); \
76db3ba4 6454 gen_addr_reg_index(ctx, EA); \
fe1e5c53 6455 tcg_gen_andi_tl(EA, EA, ~0xf); \
76db3ba4
AJ
6456 if (ctx->le_mode) { \
6457 gen_qemu_ld64(ctx, cpu_avrl[rD(ctx->opcode)], EA); \
fe1e5c53 6458 tcg_gen_addi_tl(EA, EA, 8); \
76db3ba4 6459 gen_qemu_ld64(ctx, cpu_avrh[rD(ctx->opcode)], EA); \
fe1e5c53 6460 } else { \
76db3ba4 6461 gen_qemu_ld64(ctx, cpu_avrh[rD(ctx->opcode)], EA); \
fe1e5c53 6462 tcg_gen_addi_tl(EA, EA, 8); \
76db3ba4 6463 gen_qemu_ld64(ctx, cpu_avrl[rD(ctx->opcode)], EA); \
fe1e5c53
AJ
6464 } \
6465 tcg_temp_free(EA); \
a9d9eb8f
JM
6466}
6467
6468#define GEN_VR_STX(name, opc2, opc3) \
99e300ef 6469static void gen_st##name(DisasContext *ctx) \
a9d9eb8f 6470{ \
fe1e5c53 6471 TCGv EA; \
a9d9eb8f 6472 if (unlikely(!ctx->altivec_enabled)) { \
e06fcd75 6473 gen_exception(ctx, POWERPC_EXCP_VPU); \
a9d9eb8f
JM
6474 return; \
6475 } \
76db3ba4 6476 gen_set_access_type(ctx, ACCESS_INT); \
fe1e5c53 6477 EA = tcg_temp_new(); \
76db3ba4 6478 gen_addr_reg_index(ctx, EA); \
fe1e5c53 6479 tcg_gen_andi_tl(EA, EA, ~0xf); \
76db3ba4
AJ
6480 if (ctx->le_mode) { \
6481 gen_qemu_st64(ctx, cpu_avrl[rD(ctx->opcode)], EA); \
fe1e5c53 6482 tcg_gen_addi_tl(EA, EA, 8); \
76db3ba4 6483 gen_qemu_st64(ctx, cpu_avrh[rD(ctx->opcode)], EA); \
fe1e5c53 6484 } else { \
76db3ba4 6485 gen_qemu_st64(ctx, cpu_avrh[rD(ctx->opcode)], EA); \
fe1e5c53 6486 tcg_gen_addi_tl(EA, EA, 8); \
76db3ba4 6487 gen_qemu_st64(ctx, cpu_avrl[rD(ctx->opcode)], EA); \
fe1e5c53
AJ
6488 } \
6489 tcg_temp_free(EA); \
a9d9eb8f
JM
6490}
6491
cbfb6ae9 6492#define GEN_VR_LVE(name, opc2, opc3) \
99e300ef 6493static void gen_lve##name(DisasContext *ctx) \
cbfb6ae9
AJ
6494 { \
6495 TCGv EA; \
6496 TCGv_ptr rs; \
6497 if (unlikely(!ctx->altivec_enabled)) { \
6498 gen_exception(ctx, POWERPC_EXCP_VPU); \
6499 return; \
6500 } \
6501 gen_set_access_type(ctx, ACCESS_INT); \
6502 EA = tcg_temp_new(); \
6503 gen_addr_reg_index(ctx, EA); \
6504 rs = gen_avr_ptr(rS(ctx->opcode)); \
2f5a189c 6505 gen_helper_lve##name(cpu_env, rs, EA); \
cbfb6ae9
AJ
6506 tcg_temp_free(EA); \
6507 tcg_temp_free_ptr(rs); \
6508 }
6509
6510#define GEN_VR_STVE(name, opc2, opc3) \
99e300ef 6511static void gen_stve##name(DisasContext *ctx) \
cbfb6ae9
AJ
6512 { \
6513 TCGv EA; \
6514 TCGv_ptr rs; \
6515 if (unlikely(!ctx->altivec_enabled)) { \
6516 gen_exception(ctx, POWERPC_EXCP_VPU); \
6517 return; \
6518 } \
6519 gen_set_access_type(ctx, ACCESS_INT); \
6520 EA = tcg_temp_new(); \
6521 gen_addr_reg_index(ctx, EA); \
6522 rs = gen_avr_ptr(rS(ctx->opcode)); \
2f5a189c 6523 gen_helper_stve##name(cpu_env, rs, EA); \
cbfb6ae9
AJ
6524 tcg_temp_free(EA); \
6525 tcg_temp_free_ptr(rs); \
6526 }
6527
fe1e5c53 6528GEN_VR_LDX(lvx, 0x07, 0x03);
a9d9eb8f 6529/* As we don't emulate the cache, lvxl is stricly equivalent to lvx */
fe1e5c53 6530GEN_VR_LDX(lvxl, 0x07, 0x0B);
a9d9eb8f 6531
cbfb6ae9
AJ
6532GEN_VR_LVE(bx, 0x07, 0x00);
6533GEN_VR_LVE(hx, 0x07, 0x01);
6534GEN_VR_LVE(wx, 0x07, 0x02);
6535
fe1e5c53 6536GEN_VR_STX(svx, 0x07, 0x07);
a9d9eb8f 6537/* As we don't emulate the cache, stvxl is stricly equivalent to stvx */
fe1e5c53 6538GEN_VR_STX(svxl, 0x07, 0x0F);
a9d9eb8f 6539
cbfb6ae9
AJ
6540GEN_VR_STVE(bx, 0x07, 0x04);
6541GEN_VR_STVE(hx, 0x07, 0x05);
6542GEN_VR_STVE(wx, 0x07, 0x06);
6543
99e300ef 6544static void gen_lvsl(DisasContext *ctx)
bf8d8ded
AJ
6545{
6546 TCGv_ptr rd;
6547 TCGv EA;
6548 if (unlikely(!ctx->altivec_enabled)) {
6549 gen_exception(ctx, POWERPC_EXCP_VPU);
6550 return;
6551 }
6552 EA = tcg_temp_new();
6553 gen_addr_reg_index(ctx, EA);
6554 rd = gen_avr_ptr(rD(ctx->opcode));
6555 gen_helper_lvsl(rd, EA);
6556 tcg_temp_free(EA);
6557 tcg_temp_free_ptr(rd);
6558}
6559
99e300ef 6560static void gen_lvsr(DisasContext *ctx)
bf8d8ded
AJ
6561{
6562 TCGv_ptr rd;
6563 TCGv EA;
6564 if (unlikely(!ctx->altivec_enabled)) {
6565 gen_exception(ctx, POWERPC_EXCP_VPU);
6566 return;
6567 }
6568 EA = tcg_temp_new();
6569 gen_addr_reg_index(ctx, EA);
6570 rd = gen_avr_ptr(rD(ctx->opcode));
6571 gen_helper_lvsr(rd, EA);
6572 tcg_temp_free(EA);
6573 tcg_temp_free_ptr(rd);
6574}
6575
99e300ef 6576static void gen_mfvscr(DisasContext *ctx)
785f451b
AJ
6577{
6578 TCGv_i32 t;
6579 if (unlikely(!ctx->altivec_enabled)) {
6580 gen_exception(ctx, POWERPC_EXCP_VPU);
6581 return;
6582 }
6583 tcg_gen_movi_i64(cpu_avrh[rD(ctx->opcode)], 0);
6584 t = tcg_temp_new_i32();
1328c2bf 6585 tcg_gen_ld_i32(t, cpu_env, offsetof(CPUPPCState, vscr));
785f451b 6586 tcg_gen_extu_i32_i64(cpu_avrl[rD(ctx->opcode)], t);
fce5ecb7 6587 tcg_temp_free_i32(t);
785f451b
AJ
6588}
6589
99e300ef 6590static void gen_mtvscr(DisasContext *ctx)
785f451b 6591{
6e87b7c7 6592 TCGv_ptr p;
785f451b
AJ
6593 if (unlikely(!ctx->altivec_enabled)) {
6594 gen_exception(ctx, POWERPC_EXCP_VPU);
6595 return;
6596 }
6e87b7c7 6597 p = gen_avr_ptr(rD(ctx->opcode));
d15f74fb 6598 gen_helper_mtvscr(cpu_env, p);
6e87b7c7 6599 tcg_temp_free_ptr(p);
785f451b
AJ
6600}
6601
7a9b96cf
AJ
6602/* Logical operations */
6603#define GEN_VX_LOGICAL(name, tcg_op, opc2, opc3) \
99e300ef 6604static void glue(gen_, name)(DisasContext *ctx) \
7a9b96cf
AJ
6605{ \
6606 if (unlikely(!ctx->altivec_enabled)) { \
6607 gen_exception(ctx, POWERPC_EXCP_VPU); \
6608 return; \
6609 } \
6610 tcg_op(cpu_avrh[rD(ctx->opcode)], cpu_avrh[rA(ctx->opcode)], cpu_avrh[rB(ctx->opcode)]); \
6611 tcg_op(cpu_avrl[rD(ctx->opcode)], cpu_avrl[rA(ctx->opcode)], cpu_avrl[rB(ctx->opcode)]); \
6612}
6613
6614GEN_VX_LOGICAL(vand, tcg_gen_and_i64, 2, 16);
6615GEN_VX_LOGICAL(vandc, tcg_gen_andc_i64, 2, 17);
6616GEN_VX_LOGICAL(vor, tcg_gen_or_i64, 2, 18);
6617GEN_VX_LOGICAL(vxor, tcg_gen_xor_i64, 2, 19);
6618GEN_VX_LOGICAL(vnor, tcg_gen_nor_i64, 2, 20);
6619
8e27dd6f 6620#define GEN_VXFORM(name, opc2, opc3) \
99e300ef 6621static void glue(gen_, name)(DisasContext *ctx) \
8e27dd6f
AJ
6622{ \
6623 TCGv_ptr ra, rb, rd; \
6624 if (unlikely(!ctx->altivec_enabled)) { \
6625 gen_exception(ctx, POWERPC_EXCP_VPU); \
6626 return; \
6627 } \
6628 ra = gen_avr_ptr(rA(ctx->opcode)); \
6629 rb = gen_avr_ptr(rB(ctx->opcode)); \
6630 rd = gen_avr_ptr(rD(ctx->opcode)); \
6631 gen_helper_##name (rd, ra, rb); \
6632 tcg_temp_free_ptr(ra); \
6633 tcg_temp_free_ptr(rb); \
6634 tcg_temp_free_ptr(rd); \
6635}
6636
d15f74fb
BS
6637#define GEN_VXFORM_ENV(name, opc2, opc3) \
6638static void glue(gen_, name)(DisasContext *ctx) \
6639{ \
6640 TCGv_ptr ra, rb, rd; \
6641 if (unlikely(!ctx->altivec_enabled)) { \
6642 gen_exception(ctx, POWERPC_EXCP_VPU); \
6643 return; \
6644 } \
6645 ra = gen_avr_ptr(rA(ctx->opcode)); \
6646 rb = gen_avr_ptr(rB(ctx->opcode)); \
6647 rd = gen_avr_ptr(rD(ctx->opcode)); \
54cddd21 6648 gen_helper_##name(cpu_env, rd, ra, rb); \
d15f74fb
BS
6649 tcg_temp_free_ptr(ra); \
6650 tcg_temp_free_ptr(rb); \
6651 tcg_temp_free_ptr(rd); \
6652}
6653
7872c51c
AJ
6654GEN_VXFORM(vaddubm, 0, 0);
6655GEN_VXFORM(vadduhm, 0, 1);
6656GEN_VXFORM(vadduwm, 0, 2);
6657GEN_VXFORM(vsububm, 0, 16);
6658GEN_VXFORM(vsubuhm, 0, 17);
6659GEN_VXFORM(vsubuwm, 0, 18);
e4039339
AJ
6660GEN_VXFORM(vmaxub, 1, 0);
6661GEN_VXFORM(vmaxuh, 1, 1);
6662GEN_VXFORM(vmaxuw, 1, 2);
6663GEN_VXFORM(vmaxsb, 1, 4);
6664GEN_VXFORM(vmaxsh, 1, 5);
6665GEN_VXFORM(vmaxsw, 1, 6);
6666GEN_VXFORM(vminub, 1, 8);
6667GEN_VXFORM(vminuh, 1, 9);
6668GEN_VXFORM(vminuw, 1, 10);
6669GEN_VXFORM(vminsb, 1, 12);
6670GEN_VXFORM(vminsh, 1, 13);
6671GEN_VXFORM(vminsw, 1, 14);
fab3cbe9
AJ
6672GEN_VXFORM(vavgub, 1, 16);
6673GEN_VXFORM(vavguh, 1, 17);
6674GEN_VXFORM(vavguw, 1, 18);
6675GEN_VXFORM(vavgsb, 1, 20);
6676GEN_VXFORM(vavgsh, 1, 21);
6677GEN_VXFORM(vavgsw, 1, 22);
3b430048
AJ
6678GEN_VXFORM(vmrghb, 6, 0);
6679GEN_VXFORM(vmrghh, 6, 1);
6680GEN_VXFORM(vmrghw, 6, 2);
6681GEN_VXFORM(vmrglb, 6, 4);
6682GEN_VXFORM(vmrglh, 6, 5);
6683GEN_VXFORM(vmrglw, 6, 6);
2c277908
AJ
6684GEN_VXFORM(vmuloub, 4, 0);
6685GEN_VXFORM(vmulouh, 4, 1);
6686GEN_VXFORM(vmulosb, 4, 4);
6687GEN_VXFORM(vmulosh, 4, 5);
6688GEN_VXFORM(vmuleub, 4, 8);
6689GEN_VXFORM(vmuleuh, 4, 9);
6690GEN_VXFORM(vmulesb, 4, 12);
6691GEN_VXFORM(vmulesh, 4, 13);
d79f0809
AJ
6692GEN_VXFORM(vslb, 2, 4);
6693GEN_VXFORM(vslh, 2, 5);
6694GEN_VXFORM(vslw, 2, 6);
07ef34c3
AJ
6695GEN_VXFORM(vsrb, 2, 8);
6696GEN_VXFORM(vsrh, 2, 9);
6697GEN_VXFORM(vsrw, 2, 10);
6698GEN_VXFORM(vsrab, 2, 12);
6699GEN_VXFORM(vsrah, 2, 13);
6700GEN_VXFORM(vsraw, 2, 14);
7b239bec
AJ
6701GEN_VXFORM(vslo, 6, 16);
6702GEN_VXFORM(vsro, 6, 17);
e343da72
AJ
6703GEN_VXFORM(vaddcuw, 0, 6);
6704GEN_VXFORM(vsubcuw, 0, 22);
d15f74fb
BS
6705GEN_VXFORM_ENV(vaddubs, 0, 8);
6706GEN_VXFORM_ENV(vadduhs, 0, 9);
6707GEN_VXFORM_ENV(vadduws, 0, 10);
6708GEN_VXFORM_ENV(vaddsbs, 0, 12);
6709GEN_VXFORM_ENV(vaddshs, 0, 13);
6710GEN_VXFORM_ENV(vaddsws, 0, 14);
6711GEN_VXFORM_ENV(vsububs, 0, 24);
6712GEN_VXFORM_ENV(vsubuhs, 0, 25);
6713GEN_VXFORM_ENV(vsubuws, 0, 26);
6714GEN_VXFORM_ENV(vsubsbs, 0, 28);
6715GEN_VXFORM_ENV(vsubshs, 0, 29);
6716GEN_VXFORM_ENV(vsubsws, 0, 30);
5e1d0985
AJ
6717GEN_VXFORM(vrlb, 2, 0);
6718GEN_VXFORM(vrlh, 2, 1);
6719GEN_VXFORM(vrlw, 2, 2);
d9430add
AJ
6720GEN_VXFORM(vsl, 2, 7);
6721GEN_VXFORM(vsr, 2, 11);
d15f74fb
BS
6722GEN_VXFORM_ENV(vpkuhum, 7, 0);
6723GEN_VXFORM_ENV(vpkuwum, 7, 1);
6724GEN_VXFORM_ENV(vpkuhus, 7, 2);
6725GEN_VXFORM_ENV(vpkuwus, 7, 3);
6726GEN_VXFORM_ENV(vpkshus, 7, 4);
6727GEN_VXFORM_ENV(vpkswus, 7, 5);
6728GEN_VXFORM_ENV(vpkshss, 7, 6);
6729GEN_VXFORM_ENV(vpkswss, 7, 7);
1dd9ffb9 6730GEN_VXFORM(vpkpx, 7, 12);
d15f74fb
BS
6731GEN_VXFORM_ENV(vsum4ubs, 4, 24);
6732GEN_VXFORM_ENV(vsum4sbs, 4, 28);
6733GEN_VXFORM_ENV(vsum4shs, 4, 25);
6734GEN_VXFORM_ENV(vsum2sws, 4, 26);
6735GEN_VXFORM_ENV(vsumsws, 4, 30);
6736GEN_VXFORM_ENV(vaddfp, 5, 0);
6737GEN_VXFORM_ENV(vsubfp, 5, 1);
6738GEN_VXFORM_ENV(vmaxfp, 5, 16);
6739GEN_VXFORM_ENV(vminfp, 5, 17);
fab3cbe9 6740
0cbcd906 6741#define GEN_VXRFORM1(opname, name, str, opc2, opc3) \
e8eaa2c0 6742static void glue(gen_, name)(DisasContext *ctx) \
0cbcd906
AJ
6743 { \
6744 TCGv_ptr ra, rb, rd; \
6745 if (unlikely(!ctx->altivec_enabled)) { \
6746 gen_exception(ctx, POWERPC_EXCP_VPU); \
6747 return; \
6748 } \
6749 ra = gen_avr_ptr(rA(ctx->opcode)); \
6750 rb = gen_avr_ptr(rB(ctx->opcode)); \
6751 rd = gen_avr_ptr(rD(ctx->opcode)); \
d15f74fb 6752 gen_helper_##opname(cpu_env, rd, ra, rb); \
0cbcd906
AJ
6753 tcg_temp_free_ptr(ra); \
6754 tcg_temp_free_ptr(rb); \
6755 tcg_temp_free_ptr(rd); \
6756 }
6757
6758#define GEN_VXRFORM(name, opc2, opc3) \
6759 GEN_VXRFORM1(name, name, #name, opc2, opc3) \
6760 GEN_VXRFORM1(name##_dot, name##_, #name ".", opc2, (opc3 | (0x1 << 4)))
6761
1add6e23
AJ
6762GEN_VXRFORM(vcmpequb, 3, 0)
6763GEN_VXRFORM(vcmpequh, 3, 1)
6764GEN_VXRFORM(vcmpequw, 3, 2)
6765GEN_VXRFORM(vcmpgtsb, 3, 12)
6766GEN_VXRFORM(vcmpgtsh, 3, 13)
6767GEN_VXRFORM(vcmpgtsw, 3, 14)
6768GEN_VXRFORM(vcmpgtub, 3, 8)
6769GEN_VXRFORM(vcmpgtuh, 3, 9)
6770GEN_VXRFORM(vcmpgtuw, 3, 10)
819ca121
AJ
6771GEN_VXRFORM(vcmpeqfp, 3, 3)
6772GEN_VXRFORM(vcmpgefp, 3, 7)
6773GEN_VXRFORM(vcmpgtfp, 3, 11)
6774GEN_VXRFORM(vcmpbfp, 3, 15)
1add6e23 6775
c026766b 6776#define GEN_VXFORM_SIMM(name, opc2, opc3) \
99e300ef 6777static void glue(gen_, name)(DisasContext *ctx) \
c026766b
AJ
6778 { \
6779 TCGv_ptr rd; \
6780 TCGv_i32 simm; \
6781 if (unlikely(!ctx->altivec_enabled)) { \
6782 gen_exception(ctx, POWERPC_EXCP_VPU); \
6783 return; \
6784 } \
6785 simm = tcg_const_i32(SIMM5(ctx->opcode)); \
6786 rd = gen_avr_ptr(rD(ctx->opcode)); \
6787 gen_helper_##name (rd, simm); \
6788 tcg_temp_free_i32(simm); \
6789 tcg_temp_free_ptr(rd); \
6790 }
6791
6792GEN_VXFORM_SIMM(vspltisb, 6, 12);
6793GEN_VXFORM_SIMM(vspltish, 6, 13);
6794GEN_VXFORM_SIMM(vspltisw, 6, 14);
6795
de5f2484 6796#define GEN_VXFORM_NOA(name, opc2, opc3) \
99e300ef 6797static void glue(gen_, name)(DisasContext *ctx) \
de5f2484
AJ
6798 { \
6799 TCGv_ptr rb, rd; \
6800 if (unlikely(!ctx->altivec_enabled)) { \
6801 gen_exception(ctx, POWERPC_EXCP_VPU); \
6802 return; \
6803 } \
6804 rb = gen_avr_ptr(rB(ctx->opcode)); \
6805 rd = gen_avr_ptr(rD(ctx->opcode)); \
6806 gen_helper_##name (rd, rb); \
6807 tcg_temp_free_ptr(rb); \
6808 tcg_temp_free_ptr(rd); \
6809 }
6810
d15f74fb
BS
6811#define GEN_VXFORM_NOA_ENV(name, opc2, opc3) \
6812static void glue(gen_, name)(DisasContext *ctx) \
6813 { \
6814 TCGv_ptr rb, rd; \
6815 \
6816 if (unlikely(!ctx->altivec_enabled)) { \
6817 gen_exception(ctx, POWERPC_EXCP_VPU); \
6818 return; \
6819 } \
6820 rb = gen_avr_ptr(rB(ctx->opcode)); \
6821 rd = gen_avr_ptr(rD(ctx->opcode)); \
6822 gen_helper_##name(cpu_env, rd, rb); \
6823 tcg_temp_free_ptr(rb); \
6824 tcg_temp_free_ptr(rd); \
6825 }
6826
6cf1c6e5
AJ
6827GEN_VXFORM_NOA(vupkhsb, 7, 8);
6828GEN_VXFORM_NOA(vupkhsh, 7, 9);
6829GEN_VXFORM_NOA(vupklsb, 7, 10);
6830GEN_VXFORM_NOA(vupklsh, 7, 11);
79f85c3a
AJ
6831GEN_VXFORM_NOA(vupkhpx, 7, 13);
6832GEN_VXFORM_NOA(vupklpx, 7, 15);
d15f74fb
BS
6833GEN_VXFORM_NOA_ENV(vrefp, 5, 4);
6834GEN_VXFORM_NOA_ENV(vrsqrtefp, 5, 5);
6835GEN_VXFORM_NOA_ENV(vexptefp, 5, 6);
6836GEN_VXFORM_NOA_ENV(vlogefp, 5, 7);
6837GEN_VXFORM_NOA_ENV(vrfim, 5, 8);
6838GEN_VXFORM_NOA_ENV(vrfin, 5, 9);
6839GEN_VXFORM_NOA_ENV(vrfip, 5, 10);
6840GEN_VXFORM_NOA_ENV(vrfiz, 5, 11);
79f85c3a 6841
21d21583 6842#define GEN_VXFORM_SIMM(name, opc2, opc3) \
99e300ef 6843static void glue(gen_, name)(DisasContext *ctx) \
21d21583
AJ
6844 { \
6845 TCGv_ptr rd; \
6846 TCGv_i32 simm; \
6847 if (unlikely(!ctx->altivec_enabled)) { \
6848 gen_exception(ctx, POWERPC_EXCP_VPU); \
6849 return; \
6850 } \
6851 simm = tcg_const_i32(SIMM5(ctx->opcode)); \
6852 rd = gen_avr_ptr(rD(ctx->opcode)); \
6853 gen_helper_##name (rd, simm); \
6854 tcg_temp_free_i32(simm); \
6855 tcg_temp_free_ptr(rd); \
6856 }
6857
27a4edb3 6858#define GEN_VXFORM_UIMM(name, opc2, opc3) \
99e300ef 6859static void glue(gen_, name)(DisasContext *ctx) \
27a4edb3
AJ
6860 { \
6861 TCGv_ptr rb, rd; \
6862 TCGv_i32 uimm; \
6863 if (unlikely(!ctx->altivec_enabled)) { \
6864 gen_exception(ctx, POWERPC_EXCP_VPU); \
6865 return; \
6866 } \
6867 uimm = tcg_const_i32(UIMM5(ctx->opcode)); \
6868 rb = gen_avr_ptr(rB(ctx->opcode)); \
6869 rd = gen_avr_ptr(rD(ctx->opcode)); \
6870 gen_helper_##name (rd, rb, uimm); \
6871 tcg_temp_free_i32(uimm); \
6872 tcg_temp_free_ptr(rb); \
6873 tcg_temp_free_ptr(rd); \
6874 }
6875
d15f74fb
BS
6876#define GEN_VXFORM_UIMM_ENV(name, opc2, opc3) \
6877static void glue(gen_, name)(DisasContext *ctx) \
6878 { \
6879 TCGv_ptr rb, rd; \
6880 TCGv_i32 uimm; \
6881 \
6882 if (unlikely(!ctx->altivec_enabled)) { \
6883 gen_exception(ctx, POWERPC_EXCP_VPU); \
6884 return; \
6885 } \
6886 uimm = tcg_const_i32(UIMM5(ctx->opcode)); \
6887 rb = gen_avr_ptr(rB(ctx->opcode)); \
6888 rd = gen_avr_ptr(rD(ctx->opcode)); \
6889 gen_helper_##name(cpu_env, rd, rb, uimm); \
6890 tcg_temp_free_i32(uimm); \
6891 tcg_temp_free_ptr(rb); \
6892 tcg_temp_free_ptr(rd); \
6893 }
6894
e4e6bee7
AJ
6895GEN_VXFORM_UIMM(vspltb, 6, 8);
6896GEN_VXFORM_UIMM(vsplth, 6, 9);
6897GEN_VXFORM_UIMM(vspltw, 6, 10);
d15f74fb
BS
6898GEN_VXFORM_UIMM_ENV(vcfux, 5, 12);
6899GEN_VXFORM_UIMM_ENV(vcfsx, 5, 13);
6900GEN_VXFORM_UIMM_ENV(vctuxs, 5, 14);
6901GEN_VXFORM_UIMM_ENV(vctsxs, 5, 15);
e4e6bee7 6902
99e300ef 6903static void gen_vsldoi(DisasContext *ctx)
cd633b10
AJ
6904{
6905 TCGv_ptr ra, rb, rd;
fce5ecb7 6906 TCGv_i32 sh;
cd633b10
AJ
6907 if (unlikely(!ctx->altivec_enabled)) {
6908 gen_exception(ctx, POWERPC_EXCP_VPU);
6909 return;
6910 }
6911 ra = gen_avr_ptr(rA(ctx->opcode));
6912 rb = gen_avr_ptr(rB(ctx->opcode));
6913 rd = gen_avr_ptr(rD(ctx->opcode));
6914 sh = tcg_const_i32(VSH(ctx->opcode));
6915 gen_helper_vsldoi (rd, ra, rb, sh);
6916 tcg_temp_free_ptr(ra);
6917 tcg_temp_free_ptr(rb);
6918 tcg_temp_free_ptr(rd);
fce5ecb7 6919 tcg_temp_free_i32(sh);
cd633b10
AJ
6920}
6921
707cec33 6922#define GEN_VAFORM_PAIRED(name0, name1, opc2) \
d15f74fb 6923static void glue(gen_, name0##_##name1)(DisasContext *ctx) \
707cec33
AJ
6924 { \
6925 TCGv_ptr ra, rb, rc, rd; \
6926 if (unlikely(!ctx->altivec_enabled)) { \
6927 gen_exception(ctx, POWERPC_EXCP_VPU); \
6928 return; \
6929 } \
6930 ra = gen_avr_ptr(rA(ctx->opcode)); \
6931 rb = gen_avr_ptr(rB(ctx->opcode)); \
6932 rc = gen_avr_ptr(rC(ctx->opcode)); \
6933 rd = gen_avr_ptr(rD(ctx->opcode)); \
6934 if (Rc(ctx->opcode)) { \
d15f74fb 6935 gen_helper_##name1(cpu_env, rd, ra, rb, rc); \
707cec33 6936 } else { \
d15f74fb 6937 gen_helper_##name0(cpu_env, rd, ra, rb, rc); \
707cec33
AJ
6938 } \
6939 tcg_temp_free_ptr(ra); \
6940 tcg_temp_free_ptr(rb); \
6941 tcg_temp_free_ptr(rc); \
6942 tcg_temp_free_ptr(rd); \
6943 }
6944
b161ae27
AJ
6945GEN_VAFORM_PAIRED(vmhaddshs, vmhraddshs, 16)
6946
99e300ef 6947static void gen_vmladduhm(DisasContext *ctx)
bcd2ee23
AJ
6948{
6949 TCGv_ptr ra, rb, rc, rd;
6950 if (unlikely(!ctx->altivec_enabled)) {
6951 gen_exception(ctx, POWERPC_EXCP_VPU);
6952 return;
6953 }
6954 ra = gen_avr_ptr(rA(ctx->opcode));
6955 rb = gen_avr_ptr(rB(ctx->opcode));
6956 rc = gen_avr_ptr(rC(ctx->opcode));
6957 rd = gen_avr_ptr(rD(ctx->opcode));
6958 gen_helper_vmladduhm(rd, ra, rb, rc);
6959 tcg_temp_free_ptr(ra);
6960 tcg_temp_free_ptr(rb);
6961 tcg_temp_free_ptr(rc);
6962 tcg_temp_free_ptr(rd);
6963}
6964
b04ae981 6965GEN_VAFORM_PAIRED(vmsumubm, vmsummbm, 18)
4d9903b6 6966GEN_VAFORM_PAIRED(vmsumuhm, vmsumuhs, 19)
eae07261 6967GEN_VAFORM_PAIRED(vmsumshm, vmsumshs, 20)
d1258698 6968GEN_VAFORM_PAIRED(vsel, vperm, 21)
35cf7c7e 6969GEN_VAFORM_PAIRED(vmaddfp, vnmsubfp, 23)
b04ae981 6970
0487d6a8 6971/*** SPE extension ***/
0487d6a8 6972/* Register moves */
3cd7d1dd 6973
a0e13900
FC
6974
6975static inline void gen_evmra(DisasContext *ctx)
6976{
6977
6978 if (unlikely(!ctx->spe_enabled)) {
27a69bb0 6979 gen_exception(ctx, POWERPC_EXCP_SPEU);
a0e13900
FC
6980 return;
6981 }
6982
6983#if defined(TARGET_PPC64)
6984 /* rD := rA */
6985 tcg_gen_mov_i64(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)]);
6986
6987 /* spe_acc := rA */
6988 tcg_gen_st_i64(cpu_gpr[rA(ctx->opcode)],
6989 cpu_env,
1328c2bf 6990 offsetof(CPUPPCState, spe_acc));
a0e13900
FC
6991#else
6992 TCGv_i64 tmp = tcg_temp_new_i64();
6993
6994 /* tmp := rA_lo + rA_hi << 32 */
6995 tcg_gen_concat_i32_i64(tmp, cpu_gpr[rA(ctx->opcode)], cpu_gprh[rA(ctx->opcode)]);
6996
6997 /* spe_acc := tmp */
1328c2bf 6998 tcg_gen_st_i64(tmp, cpu_env, offsetof(CPUPPCState, spe_acc));
a0e13900
FC
6999 tcg_temp_free_i64(tmp);
7000
7001 /* rD := rA */
7002 tcg_gen_mov_i32(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)]);
7003 tcg_gen_mov_i32(cpu_gprh[rD(ctx->opcode)], cpu_gprh[rA(ctx->opcode)]);
7004#endif
7005}
7006
636aa200
BS
7007static inline void gen_load_gpr64(TCGv_i64 t, int reg)
7008{
f78fb44e
AJ
7009#if defined(TARGET_PPC64)
7010 tcg_gen_mov_i64(t, cpu_gpr[reg]);
7011#else
36aa55dc 7012 tcg_gen_concat_i32_i64(t, cpu_gpr[reg], cpu_gprh[reg]);
3cd7d1dd 7013#endif
f78fb44e 7014}
3cd7d1dd 7015
636aa200
BS
7016static inline void gen_store_gpr64(int reg, TCGv_i64 t)
7017{
f78fb44e
AJ
7018#if defined(TARGET_PPC64)
7019 tcg_gen_mov_i64(cpu_gpr[reg], t);
7020#else
a7812ae4 7021 TCGv_i64 tmp = tcg_temp_new_i64();
f78fb44e 7022 tcg_gen_trunc_i64_i32(cpu_gpr[reg], t);
f78fb44e
AJ
7023 tcg_gen_shri_i64(tmp, t, 32);
7024 tcg_gen_trunc_i64_i32(cpu_gprh[reg], tmp);
a7812ae4 7025 tcg_temp_free_i64(tmp);
3cd7d1dd 7026#endif
f78fb44e 7027}
3cd7d1dd 7028
70560da7 7029#define GEN_SPE(name0, name1, opc2, opc3, inval0, inval1, type) \
99e300ef 7030static void glue(gen_, name0##_##name1)(DisasContext *ctx) \
0487d6a8
JM
7031{ \
7032 if (Rc(ctx->opcode)) \
7033 gen_##name1(ctx); \
7034 else \
7035 gen_##name0(ctx); \
7036}
7037
7038/* Handler for undefined SPE opcodes */
636aa200 7039static inline void gen_speundef(DisasContext *ctx)
0487d6a8 7040{
e06fcd75 7041 gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL);
0487d6a8
JM
7042}
7043
57951c27
AJ
7044/* SPE logic */
7045#if defined(TARGET_PPC64)
7046#define GEN_SPEOP_LOGIC2(name, tcg_op) \
636aa200 7047static inline void gen_##name(DisasContext *ctx) \
0487d6a8
JM
7048{ \
7049 if (unlikely(!ctx->spe_enabled)) { \
27a69bb0 7050 gen_exception(ctx, POWERPC_EXCP_SPEU); \
0487d6a8
JM
7051 return; \
7052 } \
57951c27
AJ
7053 tcg_op(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)], \
7054 cpu_gpr[rB(ctx->opcode)]); \
7055}
7056#else
7057#define GEN_SPEOP_LOGIC2(name, tcg_op) \
636aa200 7058static inline void gen_##name(DisasContext *ctx) \
57951c27
AJ
7059{ \
7060 if (unlikely(!ctx->spe_enabled)) { \
27a69bb0 7061 gen_exception(ctx, POWERPC_EXCP_SPEU); \
57951c27
AJ
7062 return; \
7063 } \
7064 tcg_op(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)], \
7065 cpu_gpr[rB(ctx->opcode)]); \
7066 tcg_op(cpu_gprh[rD(ctx->opcode)], cpu_gprh[rA(ctx->opcode)], \
7067 cpu_gprh[rB(ctx->opcode)]); \
0487d6a8 7068}
57951c27
AJ
7069#endif
7070
7071GEN_SPEOP_LOGIC2(evand, tcg_gen_and_tl);
7072GEN_SPEOP_LOGIC2(evandc, tcg_gen_andc_tl);
7073GEN_SPEOP_LOGIC2(evxor, tcg_gen_xor_tl);
7074GEN_SPEOP_LOGIC2(evor, tcg_gen_or_tl);
7075GEN_SPEOP_LOGIC2(evnor, tcg_gen_nor_tl);
7076GEN_SPEOP_LOGIC2(eveqv, tcg_gen_eqv_tl);
7077GEN_SPEOP_LOGIC2(evorc, tcg_gen_orc_tl);
7078GEN_SPEOP_LOGIC2(evnand, tcg_gen_nand_tl);
0487d6a8 7079
57951c27
AJ
7080/* SPE logic immediate */
7081#if defined(TARGET_PPC64)
7082#define GEN_SPEOP_TCG_LOGIC_IMM2(name, tcg_opi) \
636aa200 7083static inline void gen_##name(DisasContext *ctx) \
3d3a6a0a
AJ
7084{ \
7085 if (unlikely(!ctx->spe_enabled)) { \
27a69bb0 7086 gen_exception(ctx, POWERPC_EXCP_SPEU); \
3d3a6a0a
AJ
7087 return; \
7088 } \
a7812ae4
PB
7089 TCGv_i32 t0 = tcg_temp_local_new_i32(); \
7090 TCGv_i32 t1 = tcg_temp_local_new_i32(); \
7091 TCGv_i64 t2 = tcg_temp_local_new_i64(); \
57951c27
AJ
7092 tcg_gen_trunc_i64_i32(t0, cpu_gpr[rA(ctx->opcode)]); \
7093 tcg_opi(t0, t0, rB(ctx->opcode)); \
7094 tcg_gen_shri_i64(t2, cpu_gpr[rA(ctx->opcode)], 32); \
7095 tcg_gen_trunc_i64_i32(t1, t2); \
a7812ae4 7096 tcg_temp_free_i64(t2); \
57951c27
AJ
7097 tcg_opi(t1, t1, rB(ctx->opcode)); \
7098 tcg_gen_concat_i32_i64(cpu_gpr[rD(ctx->opcode)], t0, t1); \
a7812ae4
PB
7099 tcg_temp_free_i32(t0); \
7100 tcg_temp_free_i32(t1); \
3d3a6a0a 7101}
57951c27
AJ
7102#else
7103#define GEN_SPEOP_TCG_LOGIC_IMM2(name, tcg_opi) \
636aa200 7104static inline void gen_##name(DisasContext *ctx) \
0487d6a8
JM
7105{ \
7106 if (unlikely(!ctx->spe_enabled)) { \
27a69bb0 7107 gen_exception(ctx, POWERPC_EXCP_SPEU); \
0487d6a8
JM
7108 return; \
7109 } \
57951c27
AJ
7110 tcg_opi(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)], \
7111 rB(ctx->opcode)); \
7112 tcg_opi(cpu_gprh[rD(ctx->opcode)], cpu_gprh[rA(ctx->opcode)], \
7113 rB(ctx->opcode)); \
0487d6a8 7114}
57951c27
AJ
7115#endif
7116GEN_SPEOP_TCG_LOGIC_IMM2(evslwi, tcg_gen_shli_i32);
7117GEN_SPEOP_TCG_LOGIC_IMM2(evsrwiu, tcg_gen_shri_i32);
7118GEN_SPEOP_TCG_LOGIC_IMM2(evsrwis, tcg_gen_sari_i32);
7119GEN_SPEOP_TCG_LOGIC_IMM2(evrlwi, tcg_gen_rotli_i32);
0487d6a8 7120
57951c27
AJ
7121/* SPE arithmetic */
7122#if defined(TARGET_PPC64)
7123#define GEN_SPEOP_ARITH1(name, tcg_op) \
636aa200 7124static inline void gen_##name(DisasContext *ctx) \
0487d6a8
JM
7125{ \
7126 if (unlikely(!ctx->spe_enabled)) { \
27a69bb0 7127 gen_exception(ctx, POWERPC_EXCP_SPEU); \
0487d6a8
JM
7128 return; \
7129 } \
a7812ae4
PB
7130 TCGv_i32 t0 = tcg_temp_local_new_i32(); \
7131 TCGv_i32 t1 = tcg_temp_local_new_i32(); \
7132 TCGv_i64 t2 = tcg_temp_local_new_i64(); \
57951c27
AJ
7133 tcg_gen_trunc_i64_i32(t0, cpu_gpr[rA(ctx->opcode)]); \
7134 tcg_op(t0, t0); \
7135 tcg_gen_shri_i64(t2, cpu_gpr[rA(ctx->opcode)], 32); \
7136 tcg_gen_trunc_i64_i32(t1, t2); \
a7812ae4 7137 tcg_temp_free_i64(t2); \
57951c27
AJ
7138 tcg_op(t1, t1); \
7139 tcg_gen_concat_i32_i64(cpu_gpr[rD(ctx->opcode)], t0, t1); \
a7812ae4
PB
7140 tcg_temp_free_i32(t0); \
7141 tcg_temp_free_i32(t1); \
0487d6a8 7142}
57951c27 7143#else
a7812ae4 7144#define GEN_SPEOP_ARITH1(name, tcg_op) \
636aa200 7145static inline void gen_##name(DisasContext *ctx) \
57951c27
AJ
7146{ \
7147 if (unlikely(!ctx->spe_enabled)) { \
27a69bb0 7148 gen_exception(ctx, POWERPC_EXCP_SPEU); \
57951c27
AJ
7149 return; \
7150 } \
7151 tcg_op(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)]); \
7152 tcg_op(cpu_gprh[rD(ctx->opcode)], cpu_gprh[rA(ctx->opcode)]); \
7153}
7154#endif
0487d6a8 7155
636aa200 7156static inline void gen_op_evabs(TCGv_i32 ret, TCGv_i32 arg1)
57951c27
AJ
7157{
7158 int l1 = gen_new_label();
7159 int l2 = gen_new_label();
0487d6a8 7160
57951c27
AJ
7161 tcg_gen_brcondi_i32(TCG_COND_GE, arg1, 0, l1);
7162 tcg_gen_neg_i32(ret, arg1);
7163 tcg_gen_br(l2);
7164 gen_set_label(l1);
a7812ae4 7165 tcg_gen_mov_i32(ret, arg1);
57951c27
AJ
7166 gen_set_label(l2);
7167}
7168GEN_SPEOP_ARITH1(evabs, gen_op_evabs);
7169GEN_SPEOP_ARITH1(evneg, tcg_gen_neg_i32);
7170GEN_SPEOP_ARITH1(evextsb, tcg_gen_ext8s_i32);
7171GEN_SPEOP_ARITH1(evextsh, tcg_gen_ext16s_i32);
636aa200 7172static inline void gen_op_evrndw(TCGv_i32 ret, TCGv_i32 arg1)
0487d6a8 7173{
57951c27
AJ
7174 tcg_gen_addi_i32(ret, arg1, 0x8000);
7175 tcg_gen_ext16u_i32(ret, ret);
7176}
7177GEN_SPEOP_ARITH1(evrndw, gen_op_evrndw);
a7812ae4
PB
7178GEN_SPEOP_ARITH1(evcntlsw, gen_helper_cntlsw32);
7179GEN_SPEOP_ARITH1(evcntlzw, gen_helper_cntlzw32);
0487d6a8 7180
57951c27
AJ
7181#if defined(TARGET_PPC64)
7182#define GEN_SPEOP_ARITH2(name, tcg_op) \
636aa200 7183static inline void gen_##name(DisasContext *ctx) \
0487d6a8
JM
7184{ \
7185 if (unlikely(!ctx->spe_enabled)) { \
27a69bb0 7186 gen_exception(ctx, POWERPC_EXCP_SPEU); \
0487d6a8
JM
7187 return; \
7188 } \
a7812ae4
PB
7189 TCGv_i32 t0 = tcg_temp_local_new_i32(); \
7190 TCGv_i32 t1 = tcg_temp_local_new_i32(); \
7191 TCGv_i32 t2 = tcg_temp_local_new_i32(); \
501e23c4 7192 TCGv_i64 t3 = tcg_temp_local_new_i64(); \
57951c27
AJ
7193 tcg_gen_trunc_i64_i32(t0, cpu_gpr[rA(ctx->opcode)]); \
7194 tcg_gen_trunc_i64_i32(t2, cpu_gpr[rB(ctx->opcode)]); \
7195 tcg_op(t0, t0, t2); \
7196 tcg_gen_shri_i64(t3, cpu_gpr[rA(ctx->opcode)], 32); \
7197 tcg_gen_trunc_i64_i32(t1, t3); \
7198 tcg_gen_shri_i64(t3, cpu_gpr[rB(ctx->opcode)], 32); \
7199 tcg_gen_trunc_i64_i32(t2, t3); \
a7812ae4 7200 tcg_temp_free_i64(t3); \
57951c27 7201 tcg_op(t1, t1, t2); \
a7812ae4 7202 tcg_temp_free_i32(t2); \
57951c27 7203 tcg_gen_concat_i32_i64(cpu_gpr[rD(ctx->opcode)], t0, t1); \
a7812ae4
PB
7204 tcg_temp_free_i32(t0); \
7205 tcg_temp_free_i32(t1); \
0487d6a8 7206}
57951c27
AJ
7207#else
7208#define GEN_SPEOP_ARITH2(name, tcg_op) \
636aa200 7209static inline void gen_##name(DisasContext *ctx) \
0487d6a8
JM
7210{ \
7211 if (unlikely(!ctx->spe_enabled)) { \
27a69bb0 7212 gen_exception(ctx, POWERPC_EXCP_SPEU); \
0487d6a8
JM
7213 return; \
7214 } \
57951c27
AJ
7215 tcg_op(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)], \
7216 cpu_gpr[rB(ctx->opcode)]); \
7217 tcg_op(cpu_gprh[rD(ctx->opcode)], cpu_gprh[rA(ctx->opcode)], \
7218 cpu_gprh[rB(ctx->opcode)]); \
0487d6a8 7219}
57951c27 7220#endif
0487d6a8 7221
636aa200 7222static inline void gen_op_evsrwu(TCGv_i32 ret, TCGv_i32 arg1, TCGv_i32 arg2)
57951c27 7223{
a7812ae4 7224 TCGv_i32 t0;
57951c27 7225 int l1, l2;
0487d6a8 7226
57951c27
AJ
7227 l1 = gen_new_label();
7228 l2 = gen_new_label();
a7812ae4 7229 t0 = tcg_temp_local_new_i32();
57951c27
AJ
7230 /* No error here: 6 bits are used */
7231 tcg_gen_andi_i32(t0, arg2, 0x3F);
7232 tcg_gen_brcondi_i32(TCG_COND_GE, t0, 32, l1);
7233 tcg_gen_shr_i32(ret, arg1, t0);
7234 tcg_gen_br(l2);
7235 gen_set_label(l1);
7236 tcg_gen_movi_i32(ret, 0);
0aef4261 7237 gen_set_label(l2);
a7812ae4 7238 tcg_temp_free_i32(t0);
57951c27
AJ
7239}
7240GEN_SPEOP_ARITH2(evsrwu, gen_op_evsrwu);
636aa200 7241static inline void gen_op_evsrws(TCGv_i32 ret, TCGv_i32 arg1, TCGv_i32 arg2)
57951c27 7242{
a7812ae4 7243 TCGv_i32 t0;
57951c27
AJ
7244 int l1, l2;
7245
7246 l1 = gen_new_label();
7247 l2 = gen_new_label();
a7812ae4 7248 t0 = tcg_temp_local_new_i32();
57951c27
AJ
7249 /* No error here: 6 bits are used */
7250 tcg_gen_andi_i32(t0, arg2, 0x3F);
7251 tcg_gen_brcondi_i32(TCG_COND_GE, t0, 32, l1);
7252 tcg_gen_sar_i32(ret, arg1, t0);
7253 tcg_gen_br(l2);
7254 gen_set_label(l1);
7255 tcg_gen_movi_i32(ret, 0);
0aef4261 7256 gen_set_label(l2);
a7812ae4 7257 tcg_temp_free_i32(t0);
57951c27
AJ
7258}
7259GEN_SPEOP_ARITH2(evsrws, gen_op_evsrws);
636aa200 7260static inline void gen_op_evslw(TCGv_i32 ret, TCGv_i32 arg1, TCGv_i32 arg2)
57951c27 7261{
a7812ae4 7262 TCGv_i32 t0;
57951c27
AJ
7263 int l1, l2;
7264
7265 l1 = gen_new_label();
7266 l2 = gen_new_label();
a7812ae4 7267 t0 = tcg_temp_local_new_i32();
57951c27
AJ
7268 /* No error here: 6 bits are used */
7269 tcg_gen_andi_i32(t0, arg2, 0x3F);
7270 tcg_gen_brcondi_i32(TCG_COND_GE, t0, 32, l1);
7271 tcg_gen_shl_i32(ret, arg1, t0);
7272 tcg_gen_br(l2);
7273 gen_set_label(l1);
7274 tcg_gen_movi_i32(ret, 0);
e29ef9fa 7275 gen_set_label(l2);
a7812ae4 7276 tcg_temp_free_i32(t0);
57951c27
AJ
7277}
7278GEN_SPEOP_ARITH2(evslw, gen_op_evslw);
636aa200 7279static inline void gen_op_evrlw(TCGv_i32 ret, TCGv_i32 arg1, TCGv_i32 arg2)
57951c27 7280{
a7812ae4 7281 TCGv_i32 t0 = tcg_temp_new_i32();
57951c27
AJ
7282 tcg_gen_andi_i32(t0, arg2, 0x1F);
7283 tcg_gen_rotl_i32(ret, arg1, t0);
a7812ae4 7284 tcg_temp_free_i32(t0);
57951c27
AJ
7285}
7286GEN_SPEOP_ARITH2(evrlw, gen_op_evrlw);
636aa200 7287static inline void gen_evmergehi(DisasContext *ctx)
57951c27
AJ
7288{
7289 if (unlikely(!ctx->spe_enabled)) {
27a69bb0 7290 gen_exception(ctx, POWERPC_EXCP_SPEU);
57951c27
AJ
7291 return;
7292 }
7293#if defined(TARGET_PPC64)
a7812ae4
PB
7294 TCGv t0 = tcg_temp_new();
7295 TCGv t1 = tcg_temp_new();
57951c27
AJ
7296 tcg_gen_shri_tl(t0, cpu_gpr[rB(ctx->opcode)], 32);
7297 tcg_gen_andi_tl(t1, cpu_gpr[rA(ctx->opcode)], 0xFFFFFFFF0000000ULL);
7298 tcg_gen_or_tl(cpu_gpr[rD(ctx->opcode)], t0, t1);
7299 tcg_temp_free(t0);
7300 tcg_temp_free(t1);
7301#else
7302 tcg_gen_mov_i32(cpu_gpr[rD(ctx->opcode)], cpu_gprh[rB(ctx->opcode)]);
7303 tcg_gen_mov_i32(cpu_gprh[rD(ctx->opcode)], cpu_gprh[rA(ctx->opcode)]);
7304#endif
7305}
7306GEN_SPEOP_ARITH2(evaddw, tcg_gen_add_i32);
636aa200 7307static inline void gen_op_evsubf(TCGv_i32 ret, TCGv_i32 arg1, TCGv_i32 arg2)
0487d6a8 7308{
57951c27
AJ
7309 tcg_gen_sub_i32(ret, arg2, arg1);
7310}
7311GEN_SPEOP_ARITH2(evsubfw, gen_op_evsubf);
0487d6a8 7312
57951c27
AJ
7313/* SPE arithmetic immediate */
7314#if defined(TARGET_PPC64)
7315#define GEN_SPEOP_ARITH_IMM2(name, tcg_op) \
636aa200 7316static inline void gen_##name(DisasContext *ctx) \
57951c27
AJ
7317{ \
7318 if (unlikely(!ctx->spe_enabled)) { \
27a69bb0 7319 gen_exception(ctx, POWERPC_EXCP_SPEU); \
57951c27
AJ
7320 return; \
7321 } \
a7812ae4
PB
7322 TCGv_i32 t0 = tcg_temp_local_new_i32(); \
7323 TCGv_i32 t1 = tcg_temp_local_new_i32(); \
7324 TCGv_i64 t2 = tcg_temp_local_new_i64(); \
57951c27
AJ
7325 tcg_gen_trunc_i64_i32(t0, cpu_gpr[rB(ctx->opcode)]); \
7326 tcg_op(t0, t0, rA(ctx->opcode)); \
7327 tcg_gen_shri_i64(t2, cpu_gpr[rB(ctx->opcode)], 32); \
7328 tcg_gen_trunc_i64_i32(t1, t2); \
e06fcd75 7329 tcg_temp_free_i64(t2); \
57951c27
AJ
7330 tcg_op(t1, t1, rA(ctx->opcode)); \
7331 tcg_gen_concat_i32_i64(cpu_gpr[rD(ctx->opcode)], t0, t1); \
a7812ae4
PB
7332 tcg_temp_free_i32(t0); \
7333 tcg_temp_free_i32(t1); \
57951c27
AJ
7334}
7335#else
7336#define GEN_SPEOP_ARITH_IMM2(name, tcg_op) \
636aa200 7337static inline void gen_##name(DisasContext *ctx) \
57951c27
AJ
7338{ \
7339 if (unlikely(!ctx->spe_enabled)) { \
27a69bb0 7340 gen_exception(ctx, POWERPC_EXCP_SPEU); \
57951c27
AJ
7341 return; \
7342 } \
7343 tcg_op(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rB(ctx->opcode)], \
7344 rA(ctx->opcode)); \
7345 tcg_op(cpu_gprh[rD(ctx->opcode)], cpu_gprh[rB(ctx->opcode)], \
7346 rA(ctx->opcode)); \
7347}
7348#endif
7349GEN_SPEOP_ARITH_IMM2(evaddiw, tcg_gen_addi_i32);
7350GEN_SPEOP_ARITH_IMM2(evsubifw, tcg_gen_subi_i32);
7351
7352/* SPE comparison */
7353#if defined(TARGET_PPC64)
7354#define GEN_SPEOP_COMP(name, tcg_cond) \
636aa200 7355static inline void gen_##name(DisasContext *ctx) \
57951c27
AJ
7356{ \
7357 if (unlikely(!ctx->spe_enabled)) { \
27a69bb0 7358 gen_exception(ctx, POWERPC_EXCP_SPEU); \
57951c27
AJ
7359 return; \
7360 } \
7361 int l1 = gen_new_label(); \
7362 int l2 = gen_new_label(); \
7363 int l3 = gen_new_label(); \
7364 int l4 = gen_new_label(); \
a7812ae4
PB
7365 TCGv_i32 t0 = tcg_temp_local_new_i32(); \
7366 TCGv_i32 t1 = tcg_temp_local_new_i32(); \
7367 TCGv_i64 t2 = tcg_temp_local_new_i64(); \
57951c27
AJ
7368 tcg_gen_trunc_i64_i32(t0, cpu_gpr[rA(ctx->opcode)]); \
7369 tcg_gen_trunc_i64_i32(t1, cpu_gpr[rB(ctx->opcode)]); \
7370 tcg_gen_brcond_i32(tcg_cond, t0, t1, l1); \
a7812ae4 7371 tcg_gen_movi_i32(cpu_crf[crfD(ctx->opcode)], 0); \
57951c27
AJ
7372 tcg_gen_br(l2); \
7373 gen_set_label(l1); \
7374 tcg_gen_movi_i32(cpu_crf[crfD(ctx->opcode)], \
7375 CRF_CL | CRF_CH_OR_CL | CRF_CH_AND_CL); \
7376 gen_set_label(l2); \
7377 tcg_gen_shri_i64(t2, cpu_gpr[rA(ctx->opcode)], 32); \
7378 tcg_gen_trunc_i64_i32(t0, t2); \
7379 tcg_gen_shri_i64(t2, cpu_gpr[rB(ctx->opcode)], 32); \
7380 tcg_gen_trunc_i64_i32(t1, t2); \
a7812ae4 7381 tcg_temp_free_i64(t2); \
57951c27
AJ
7382 tcg_gen_brcond_i32(tcg_cond, t0, t1, l3); \
7383 tcg_gen_andi_i32(cpu_crf[crfD(ctx->opcode)], cpu_crf[crfD(ctx->opcode)], \
7384 ~(CRF_CH | CRF_CH_AND_CL)); \
7385 tcg_gen_br(l4); \
7386 gen_set_label(l3); \
7387 tcg_gen_ori_i32(cpu_crf[crfD(ctx->opcode)], cpu_crf[crfD(ctx->opcode)], \
7388 CRF_CH | CRF_CH_OR_CL); \
7389 gen_set_label(l4); \
a7812ae4
PB
7390 tcg_temp_free_i32(t0); \
7391 tcg_temp_free_i32(t1); \
57951c27
AJ
7392}
7393#else
7394#define GEN_SPEOP_COMP(name, tcg_cond) \
636aa200 7395static inline void gen_##name(DisasContext *ctx) \
57951c27
AJ
7396{ \
7397 if (unlikely(!ctx->spe_enabled)) { \
27a69bb0 7398 gen_exception(ctx, POWERPC_EXCP_SPEU); \
57951c27
AJ
7399 return; \
7400 } \
7401 int l1 = gen_new_label(); \
7402 int l2 = gen_new_label(); \
7403 int l3 = gen_new_label(); \
7404 int l4 = gen_new_label(); \
7405 \
7406 tcg_gen_brcond_i32(tcg_cond, cpu_gpr[rA(ctx->opcode)], \
7407 cpu_gpr[rB(ctx->opcode)], l1); \
7408 tcg_gen_movi_tl(cpu_crf[crfD(ctx->opcode)], 0); \
7409 tcg_gen_br(l2); \
7410 gen_set_label(l1); \
7411 tcg_gen_movi_i32(cpu_crf[crfD(ctx->opcode)], \
7412 CRF_CL | CRF_CH_OR_CL | CRF_CH_AND_CL); \
7413 gen_set_label(l2); \
7414 tcg_gen_brcond_i32(tcg_cond, cpu_gprh[rA(ctx->opcode)], \
7415 cpu_gprh[rB(ctx->opcode)], l3); \
7416 tcg_gen_andi_i32(cpu_crf[crfD(ctx->opcode)], cpu_crf[crfD(ctx->opcode)], \
7417 ~(CRF_CH | CRF_CH_AND_CL)); \
7418 tcg_gen_br(l4); \
7419 gen_set_label(l3); \
7420 tcg_gen_ori_i32(cpu_crf[crfD(ctx->opcode)], cpu_crf[crfD(ctx->opcode)], \
7421 CRF_CH | CRF_CH_OR_CL); \
7422 gen_set_label(l4); \
7423}
7424#endif
7425GEN_SPEOP_COMP(evcmpgtu, TCG_COND_GTU);
7426GEN_SPEOP_COMP(evcmpgts, TCG_COND_GT);
7427GEN_SPEOP_COMP(evcmpltu, TCG_COND_LTU);
7428GEN_SPEOP_COMP(evcmplts, TCG_COND_LT);
7429GEN_SPEOP_COMP(evcmpeq, TCG_COND_EQ);
7430
7431/* SPE misc */
636aa200 7432static inline void gen_brinc(DisasContext *ctx)
57951c27
AJ
7433{
7434 /* Note: brinc is usable even if SPE is disabled */
a7812ae4
PB
7435 gen_helper_brinc(cpu_gpr[rD(ctx->opcode)],
7436 cpu_gpr[rA(ctx->opcode)], cpu_gpr[rB(ctx->opcode)]);
0487d6a8 7437}
636aa200 7438static inline void gen_evmergelo(DisasContext *ctx)
57951c27
AJ
7439{
7440 if (unlikely(!ctx->spe_enabled)) {
27a69bb0 7441 gen_exception(ctx, POWERPC_EXCP_SPEU);
57951c27
AJ
7442 return;
7443 }
7444#if defined(TARGET_PPC64)
a7812ae4
PB
7445 TCGv t0 = tcg_temp_new();
7446 TCGv t1 = tcg_temp_new();
17d9b3af 7447 tcg_gen_ext32u_tl(t0, cpu_gpr[rB(ctx->opcode)]);
57951c27
AJ
7448 tcg_gen_shli_tl(t1, cpu_gpr[rA(ctx->opcode)], 32);
7449 tcg_gen_or_tl(cpu_gpr[rD(ctx->opcode)], t0, t1);
7450 tcg_temp_free(t0);
7451 tcg_temp_free(t1);
7452#else
57951c27 7453 tcg_gen_mov_i32(cpu_gprh[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)]);
33890b3e 7454 tcg_gen_mov_i32(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rB(ctx->opcode)]);
57951c27
AJ
7455#endif
7456}
636aa200 7457static inline void gen_evmergehilo(DisasContext *ctx)
57951c27
AJ
7458{
7459 if (unlikely(!ctx->spe_enabled)) {
27a69bb0 7460 gen_exception(ctx, POWERPC_EXCP_SPEU);
57951c27
AJ
7461 return;
7462 }
7463#if defined(TARGET_PPC64)
a7812ae4
PB
7464 TCGv t0 = tcg_temp_new();
7465 TCGv t1 = tcg_temp_new();
17d9b3af 7466 tcg_gen_ext32u_tl(t0, cpu_gpr[rB(ctx->opcode)]);
57951c27
AJ
7467 tcg_gen_andi_tl(t1, cpu_gpr[rA(ctx->opcode)], 0xFFFFFFFF0000000ULL);
7468 tcg_gen_or_tl(cpu_gpr[rD(ctx->opcode)], t0, t1);
7469 tcg_temp_free(t0);
7470 tcg_temp_free(t1);
7471#else
7472 tcg_gen_mov_i32(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rB(ctx->opcode)]);
7473 tcg_gen_mov_i32(cpu_gprh[rD(ctx->opcode)], cpu_gprh[rA(ctx->opcode)]);
7474#endif
7475}
636aa200 7476static inline void gen_evmergelohi(DisasContext *ctx)
57951c27
AJ
7477{
7478 if (unlikely(!ctx->spe_enabled)) {
27a69bb0 7479 gen_exception(ctx, POWERPC_EXCP_SPEU);
57951c27
AJ
7480 return;
7481 }
7482#if defined(TARGET_PPC64)
a7812ae4
PB
7483 TCGv t0 = tcg_temp_new();
7484 TCGv t1 = tcg_temp_new();
57951c27
AJ
7485 tcg_gen_shri_tl(t0, cpu_gpr[rB(ctx->opcode)], 32);
7486 tcg_gen_shli_tl(t1, cpu_gpr[rA(ctx->opcode)], 32);
7487 tcg_gen_or_tl(cpu_gpr[rD(ctx->opcode)], t0, t1);
7488 tcg_temp_free(t0);
7489 tcg_temp_free(t1);
7490#else
33890b3e
NF
7491 if (rD(ctx->opcode) == rA(ctx->opcode)) {
7492 TCGv_i32 tmp = tcg_temp_new_i32();
7493 tcg_gen_mov_i32(tmp, cpu_gpr[rA(ctx->opcode)]);
7494 tcg_gen_mov_i32(cpu_gpr[rD(ctx->opcode)], cpu_gprh[rB(ctx->opcode)]);
7495 tcg_gen_mov_i32(cpu_gprh[rD(ctx->opcode)], tmp);
7496 tcg_temp_free_i32(tmp);
7497 } else {
7498 tcg_gen_mov_i32(cpu_gpr[rD(ctx->opcode)], cpu_gprh[rB(ctx->opcode)]);
7499 tcg_gen_mov_i32(cpu_gprh[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)]);
7500 }
57951c27
AJ
7501#endif
7502}
636aa200 7503static inline void gen_evsplati(DisasContext *ctx)
57951c27 7504{
ae01847f 7505 uint64_t imm = ((int32_t)(rA(ctx->opcode) << 27)) >> 27;
0487d6a8 7506
57951c27 7507#if defined(TARGET_PPC64)
38d14952 7508 tcg_gen_movi_tl(cpu_gpr[rD(ctx->opcode)], (imm << 32) | imm);
57951c27
AJ
7509#else
7510 tcg_gen_movi_i32(cpu_gpr[rD(ctx->opcode)], imm);
7511 tcg_gen_movi_i32(cpu_gprh[rD(ctx->opcode)], imm);
7512#endif
7513}
636aa200 7514static inline void gen_evsplatfi(DisasContext *ctx)
0487d6a8 7515{
ae01847f 7516 uint64_t imm = rA(ctx->opcode) << 27;
0487d6a8 7517
57951c27 7518#if defined(TARGET_PPC64)
38d14952 7519 tcg_gen_movi_tl(cpu_gpr[rD(ctx->opcode)], (imm << 32) | imm);
57951c27
AJ
7520#else
7521 tcg_gen_movi_i32(cpu_gpr[rD(ctx->opcode)], imm);
7522 tcg_gen_movi_i32(cpu_gprh[rD(ctx->opcode)], imm);
7523#endif
0487d6a8
JM
7524}
7525
636aa200 7526static inline void gen_evsel(DisasContext *ctx)
57951c27
AJ
7527{
7528 int l1 = gen_new_label();
7529 int l2 = gen_new_label();
7530 int l3 = gen_new_label();
7531 int l4 = gen_new_label();
a7812ae4 7532 TCGv_i32 t0 = tcg_temp_local_new_i32();
57951c27 7533#if defined(TARGET_PPC64)
a7812ae4
PB
7534 TCGv t1 = tcg_temp_local_new();
7535 TCGv t2 = tcg_temp_local_new();
57951c27
AJ
7536#endif
7537 tcg_gen_andi_i32(t0, cpu_crf[ctx->opcode & 0x07], 1 << 3);
7538 tcg_gen_brcondi_i32(TCG_COND_EQ, t0, 0, l1);
7539#if defined(TARGET_PPC64)
7540 tcg_gen_andi_tl(t1, cpu_gpr[rA(ctx->opcode)], 0xFFFFFFFF00000000ULL);
7541#else
7542 tcg_gen_mov_tl(cpu_gprh[rD(ctx->opcode)], cpu_gprh[rA(ctx->opcode)]);
7543#endif
7544 tcg_gen_br(l2);
7545 gen_set_label(l1);
7546#if defined(TARGET_PPC64)
7547 tcg_gen_andi_tl(t1, cpu_gpr[rB(ctx->opcode)], 0xFFFFFFFF00000000ULL);
7548#else
7549 tcg_gen_mov_tl(cpu_gprh[rD(ctx->opcode)], cpu_gprh[rB(ctx->opcode)]);
7550#endif
7551 gen_set_label(l2);
7552 tcg_gen_andi_i32(t0, cpu_crf[ctx->opcode & 0x07], 1 << 2);
7553 tcg_gen_brcondi_i32(TCG_COND_EQ, t0, 0, l3);
7554#if defined(TARGET_PPC64)
17d9b3af 7555 tcg_gen_ext32u_tl(t2, cpu_gpr[rA(ctx->opcode)]);
57951c27
AJ
7556#else
7557 tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)]);
7558#endif
7559 tcg_gen_br(l4);
7560 gen_set_label(l3);
7561#if defined(TARGET_PPC64)
17d9b3af 7562 tcg_gen_ext32u_tl(t2, cpu_gpr[rB(ctx->opcode)]);
57951c27
AJ
7563#else
7564 tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rB(ctx->opcode)]);
7565#endif
7566 gen_set_label(l4);
a7812ae4 7567 tcg_temp_free_i32(t0);
57951c27
AJ
7568#if defined(TARGET_PPC64)
7569 tcg_gen_or_tl(cpu_gpr[rD(ctx->opcode)], t1, t2);
7570 tcg_temp_free(t1);
7571 tcg_temp_free(t2);
7572#endif
7573}
e8eaa2c0
BS
7574
7575static void gen_evsel0(DisasContext *ctx)
57951c27
AJ
7576{
7577 gen_evsel(ctx);
7578}
e8eaa2c0
BS
7579
7580static void gen_evsel1(DisasContext *ctx)
57951c27
AJ
7581{
7582 gen_evsel(ctx);
7583}
e8eaa2c0
BS
7584
7585static void gen_evsel2(DisasContext *ctx)
57951c27
AJ
7586{
7587 gen_evsel(ctx);
7588}
e8eaa2c0
BS
7589
7590static void gen_evsel3(DisasContext *ctx)
57951c27
AJ
7591{
7592 gen_evsel(ctx);
7593}
0487d6a8 7594
a0e13900
FC
7595/* Multiply */
7596
7597static inline void gen_evmwumi(DisasContext *ctx)
7598{
7599 TCGv_i64 t0, t1;
7600
7601 if (unlikely(!ctx->spe_enabled)) {
27a69bb0 7602 gen_exception(ctx, POWERPC_EXCP_SPEU);
a0e13900
FC
7603 return;
7604 }
7605
7606 t0 = tcg_temp_new_i64();
7607 t1 = tcg_temp_new_i64();
7608
7609 /* t0 := rA; t1 := rB */
7610#if defined(TARGET_PPC64)
7611 tcg_gen_ext32u_tl(t0, cpu_gpr[rA(ctx->opcode)]);
7612 tcg_gen_ext32u_tl(t1, cpu_gpr[rB(ctx->opcode)]);
7613#else
7614 tcg_gen_extu_tl_i64(t0, cpu_gpr[rA(ctx->opcode)]);
7615 tcg_gen_extu_tl_i64(t1, cpu_gpr[rB(ctx->opcode)]);
7616#endif
7617
7618 tcg_gen_mul_i64(t0, t0, t1); /* t0 := rA * rB */
7619
7620 gen_store_gpr64(rD(ctx->opcode), t0); /* rD := t0 */
7621
7622 tcg_temp_free_i64(t0);
7623 tcg_temp_free_i64(t1);
7624}
7625
7626static inline void gen_evmwumia(DisasContext *ctx)
7627{
7628 TCGv_i64 tmp;
7629
7630 if (unlikely(!ctx->spe_enabled)) {
27a69bb0 7631 gen_exception(ctx, POWERPC_EXCP_SPEU);
a0e13900
FC
7632 return;
7633 }
7634
7635 gen_evmwumi(ctx); /* rD := rA * rB */
7636
7637 tmp = tcg_temp_new_i64();
7638
7639 /* acc := rD */
7640 gen_load_gpr64(tmp, rD(ctx->opcode));
1328c2bf 7641 tcg_gen_st_i64(tmp, cpu_env, offsetof(CPUPPCState, spe_acc));
a0e13900
FC
7642 tcg_temp_free_i64(tmp);
7643}
7644
7645static inline void gen_evmwumiaa(DisasContext *ctx)
7646{
7647 TCGv_i64 acc;
7648 TCGv_i64 tmp;
7649
7650 if (unlikely(!ctx->spe_enabled)) {
27a69bb0 7651 gen_exception(ctx, POWERPC_EXCP_SPEU);
a0e13900
FC
7652 return;
7653 }
7654
7655 gen_evmwumi(ctx); /* rD := rA * rB */
7656
7657 acc = tcg_temp_new_i64();
7658 tmp = tcg_temp_new_i64();
7659
7660 /* tmp := rD */
7661 gen_load_gpr64(tmp, rD(ctx->opcode));
7662
7663 /* Load acc */
1328c2bf 7664 tcg_gen_ld_i64(acc, cpu_env, offsetof(CPUPPCState, spe_acc));
a0e13900
FC
7665
7666 /* acc := tmp + acc */
7667 tcg_gen_add_i64(acc, acc, tmp);
7668
7669 /* Store acc */
1328c2bf 7670 tcg_gen_st_i64(acc, cpu_env, offsetof(CPUPPCState, spe_acc));
a0e13900
FC
7671
7672 /* rD := acc */
7673 gen_store_gpr64(rD(ctx->opcode), acc);
7674
7675 tcg_temp_free_i64(acc);
7676 tcg_temp_free_i64(tmp);
7677}
7678
7679static inline void gen_evmwsmi(DisasContext *ctx)
7680{
7681 TCGv_i64 t0, t1;
7682
7683 if (unlikely(!ctx->spe_enabled)) {
27a69bb0 7684 gen_exception(ctx, POWERPC_EXCP_SPEU);
a0e13900
FC
7685 return;
7686 }
7687
7688 t0 = tcg_temp_new_i64();
7689 t1 = tcg_temp_new_i64();
7690
7691 /* t0 := rA; t1 := rB */
7692#if defined(TARGET_PPC64)
7693 tcg_gen_ext32s_tl(t0, cpu_gpr[rA(ctx->opcode)]);
7694 tcg_gen_ext32s_tl(t1, cpu_gpr[rB(ctx->opcode)]);
7695#else
7696 tcg_gen_ext_tl_i64(t0, cpu_gpr[rA(ctx->opcode)]);
7697 tcg_gen_ext_tl_i64(t1, cpu_gpr[rB(ctx->opcode)]);
7698#endif
7699
7700 tcg_gen_mul_i64(t0, t0, t1); /* t0 := rA * rB */
7701
7702 gen_store_gpr64(rD(ctx->opcode), t0); /* rD := t0 */
7703
7704 tcg_temp_free_i64(t0);
7705 tcg_temp_free_i64(t1);
7706}
7707
7708static inline void gen_evmwsmia(DisasContext *ctx)
7709{
7710 TCGv_i64 tmp;
7711
7712 gen_evmwsmi(ctx); /* rD := rA * rB */
7713
7714 tmp = tcg_temp_new_i64();
7715
7716 /* acc := rD */
7717 gen_load_gpr64(tmp, rD(ctx->opcode));
1328c2bf 7718 tcg_gen_st_i64(tmp, cpu_env, offsetof(CPUPPCState, spe_acc));
a0e13900
FC
7719
7720 tcg_temp_free_i64(tmp);
7721}
7722
7723static inline void gen_evmwsmiaa(DisasContext *ctx)
7724{
7725 TCGv_i64 acc = tcg_temp_new_i64();
7726 TCGv_i64 tmp = tcg_temp_new_i64();
7727
7728 gen_evmwsmi(ctx); /* rD := rA * rB */
7729
7730 acc = tcg_temp_new_i64();
7731 tmp = tcg_temp_new_i64();
7732
7733 /* tmp := rD */
7734 gen_load_gpr64(tmp, rD(ctx->opcode));
7735
7736 /* Load acc */
1328c2bf 7737 tcg_gen_ld_i64(acc, cpu_env, offsetof(CPUPPCState, spe_acc));
a0e13900
FC
7738
7739 /* acc := tmp + acc */
7740 tcg_gen_add_i64(acc, acc, tmp);
7741
7742 /* Store acc */
1328c2bf 7743 tcg_gen_st_i64(acc, cpu_env, offsetof(CPUPPCState, spe_acc));
a0e13900
FC
7744
7745 /* rD := acc */
7746 gen_store_gpr64(rD(ctx->opcode), acc);
7747
7748 tcg_temp_free_i64(acc);
7749 tcg_temp_free_i64(tmp);
7750}
7751
70560da7
FC
7752GEN_SPE(evaddw, speundef, 0x00, 0x08, 0x00000000, 0xFFFFFFFF, PPC_SPE); ////
7753GEN_SPE(evaddiw, speundef, 0x01, 0x08, 0x00000000, 0xFFFFFFFF, PPC_SPE);
7754GEN_SPE(evsubfw, speundef, 0x02, 0x08, 0x00000000, 0xFFFFFFFF, PPC_SPE); ////
7755GEN_SPE(evsubifw, speundef, 0x03, 0x08, 0x00000000, 0xFFFFFFFF, PPC_SPE);
7756GEN_SPE(evabs, evneg, 0x04, 0x08, 0x0000F800, 0x0000F800, PPC_SPE); ////
7757GEN_SPE(evextsb, evextsh, 0x05, 0x08, 0x0000F800, 0x0000F800, PPC_SPE); ////
7758GEN_SPE(evrndw, evcntlzw, 0x06, 0x08, 0x0000F800, 0x0000F800, PPC_SPE); ////
7759GEN_SPE(evcntlsw, brinc, 0x07, 0x08, 0x0000F800, 0x00000000, PPC_SPE); //
7760GEN_SPE(evmra, speundef, 0x02, 0x13, 0x0000F800, 0xFFFFFFFF, PPC_SPE);
7761GEN_SPE(speundef, evand, 0x08, 0x08, 0xFFFFFFFF, 0x00000000, PPC_SPE); ////
7762GEN_SPE(evandc, speundef, 0x09, 0x08, 0x00000000, 0xFFFFFFFF, PPC_SPE); ////
7763GEN_SPE(evxor, evor, 0x0B, 0x08, 0x00000000, 0x00000000, PPC_SPE); ////
7764GEN_SPE(evnor, eveqv, 0x0C, 0x08, 0x00000000, 0x00000000, PPC_SPE); ////
7765GEN_SPE(evmwumi, evmwsmi, 0x0C, 0x11, 0x00000000, 0x00000000, PPC_SPE);
7766GEN_SPE(evmwumia, evmwsmia, 0x1C, 0x11, 0x00000000, 0x00000000, PPC_SPE);
7767GEN_SPE(evmwumiaa, evmwsmiaa, 0x0C, 0x15, 0x00000000, 0x00000000, PPC_SPE);
7768GEN_SPE(speundef, evorc, 0x0D, 0x08, 0xFFFFFFFF, 0x00000000, PPC_SPE); ////
7769GEN_SPE(evnand, speundef, 0x0F, 0x08, 0x00000000, 0xFFFFFFFF, PPC_SPE); ////
7770GEN_SPE(evsrwu, evsrws, 0x10, 0x08, 0x00000000, 0x00000000, PPC_SPE); ////
7771GEN_SPE(evsrwiu, evsrwis, 0x11, 0x08, 0x00000000, 0x00000000, PPC_SPE);
7772GEN_SPE(evslw, speundef, 0x12, 0x08, 0x00000000, 0xFFFFFFFF, PPC_SPE); ////
7773GEN_SPE(evslwi, speundef, 0x13, 0x08, 0x00000000, 0xFFFFFFFF, PPC_SPE);
7774GEN_SPE(evrlw, evsplati, 0x14, 0x08, 0x00000000, 0x0000F800, PPC_SPE); //
7775GEN_SPE(evrlwi, evsplatfi, 0x15, 0x08, 0x00000000, 0x0000F800, PPC_SPE);
7776GEN_SPE(evmergehi, evmergelo, 0x16, 0x08, 0x00000000, 0x00000000, PPC_SPE); ////
7777GEN_SPE(evmergehilo, evmergelohi, 0x17, 0x08, 0x00000000, 0x00000000, PPC_SPE); ////
7778GEN_SPE(evcmpgtu, evcmpgts, 0x18, 0x08, 0x00600000, 0x00600000, PPC_SPE); ////
7779GEN_SPE(evcmpltu, evcmplts, 0x19, 0x08, 0x00600000, 0x00600000, PPC_SPE); ////
7780GEN_SPE(evcmpeq, speundef, 0x1A, 0x08, 0x00600000, 0xFFFFFFFF, PPC_SPE); ////
0487d6a8 7781
6a6ae23f 7782/* SPE load and stores */
636aa200 7783static inline void gen_addr_spe_imm_index(DisasContext *ctx, TCGv EA, int sh)
6a6ae23f
AJ
7784{
7785 target_ulong uimm = rB(ctx->opcode);
7786
76db3ba4 7787 if (rA(ctx->opcode) == 0) {
6a6ae23f 7788 tcg_gen_movi_tl(EA, uimm << sh);
76db3ba4 7789 } else {
6a6ae23f 7790 tcg_gen_addi_tl(EA, cpu_gpr[rA(ctx->opcode)], uimm << sh);
c791fe84 7791 if (NARROW_MODE(ctx)) {
76db3ba4
AJ
7792 tcg_gen_ext32u_tl(EA, EA);
7793 }
76db3ba4 7794 }
0487d6a8 7795}
6a6ae23f 7796
636aa200 7797static inline void gen_op_evldd(DisasContext *ctx, TCGv addr)
6a6ae23f
AJ
7798{
7799#if defined(TARGET_PPC64)
76db3ba4 7800 gen_qemu_ld64(ctx, cpu_gpr[rD(ctx->opcode)], addr);
6a6ae23f
AJ
7801#else
7802 TCGv_i64 t0 = tcg_temp_new_i64();
76db3ba4 7803 gen_qemu_ld64(ctx, t0, addr);
6a6ae23f
AJ
7804 tcg_gen_trunc_i64_i32(cpu_gpr[rD(ctx->opcode)], t0);
7805 tcg_gen_shri_i64(t0, t0, 32);
7806 tcg_gen_trunc_i64_i32(cpu_gprh[rD(ctx->opcode)], t0);
7807 tcg_temp_free_i64(t0);
7808#endif
0487d6a8 7809}
6a6ae23f 7810
636aa200 7811static inline void gen_op_evldw(DisasContext *ctx, TCGv addr)
6a6ae23f 7812{
0487d6a8 7813#if defined(TARGET_PPC64)
6a6ae23f 7814 TCGv t0 = tcg_temp_new();
76db3ba4 7815 gen_qemu_ld32u(ctx, t0, addr);
6a6ae23f 7816 tcg_gen_shli_tl(cpu_gpr[rD(ctx->opcode)], t0, 32);
76db3ba4
AJ
7817 gen_addr_add(ctx, addr, addr, 4);
7818 gen_qemu_ld32u(ctx, t0, addr);
6a6ae23f
AJ
7819 tcg_gen_or_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rD(ctx->opcode)], t0);
7820 tcg_temp_free(t0);
7821#else
76db3ba4
AJ
7822 gen_qemu_ld32u(ctx, cpu_gprh[rD(ctx->opcode)], addr);
7823 gen_addr_add(ctx, addr, addr, 4);
7824 gen_qemu_ld32u(ctx, cpu_gpr[rD(ctx->opcode)], addr);
6a6ae23f 7825#endif
0487d6a8 7826}
6a6ae23f 7827
636aa200 7828static inline void gen_op_evldh(DisasContext *ctx, TCGv addr)
6a6ae23f
AJ
7829{
7830 TCGv t0 = tcg_temp_new();
7831#if defined(TARGET_PPC64)
76db3ba4 7832 gen_qemu_ld16u(ctx, t0, addr);
6a6ae23f 7833 tcg_gen_shli_tl(cpu_gpr[rD(ctx->opcode)], t0, 48);
76db3ba4
AJ
7834 gen_addr_add(ctx, addr, addr, 2);
7835 gen_qemu_ld16u(ctx, t0, addr);
6a6ae23f
AJ
7836 tcg_gen_shli_tl(t0, t0, 32);
7837 tcg_gen_or_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rD(ctx->opcode)], t0);
76db3ba4
AJ
7838 gen_addr_add(ctx, addr, addr, 2);
7839 gen_qemu_ld16u(ctx, t0, addr);
6a6ae23f
AJ
7840 tcg_gen_shli_tl(t0, t0, 16);
7841 tcg_gen_or_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rD(ctx->opcode)], t0);
76db3ba4
AJ
7842 gen_addr_add(ctx, addr, addr, 2);
7843 gen_qemu_ld16u(ctx, t0, addr);
6a6ae23f 7844 tcg_gen_or_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rD(ctx->opcode)], t0);
0487d6a8 7845#else
76db3ba4 7846 gen_qemu_ld16u(ctx, t0, addr);
6a6ae23f 7847 tcg_gen_shli_tl(cpu_gprh[rD(ctx->opcode)], t0, 16);
76db3ba4
AJ
7848 gen_addr_add(ctx, addr, addr, 2);
7849 gen_qemu_ld16u(ctx, t0, addr);
6a6ae23f 7850 tcg_gen_or_tl(cpu_gprh[rD(ctx->opcode)], cpu_gprh[rD(ctx->opcode)], t0);
76db3ba4
AJ
7851 gen_addr_add(ctx, addr, addr, 2);
7852 gen_qemu_ld16u(ctx, t0, addr);
6a6ae23f 7853 tcg_gen_shli_tl(cpu_gprh[rD(ctx->opcode)], t0, 16);
76db3ba4
AJ
7854 gen_addr_add(ctx, addr, addr, 2);
7855 gen_qemu_ld16u(ctx, t0, addr);
6a6ae23f 7856 tcg_gen_or_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rD(ctx->opcode)], t0);
0487d6a8 7857#endif
6a6ae23f 7858 tcg_temp_free(t0);
0487d6a8
JM
7859}
7860
636aa200 7861static inline void gen_op_evlhhesplat(DisasContext *ctx, TCGv addr)
6a6ae23f
AJ
7862{
7863 TCGv t0 = tcg_temp_new();
76db3ba4 7864 gen_qemu_ld16u(ctx, t0, addr);
6a6ae23f
AJ
7865#if defined(TARGET_PPC64)
7866 tcg_gen_shli_tl(cpu_gpr[rD(ctx->opcode)], t0, 48);
7867 tcg_gen_shli_tl(t0, t0, 16);
7868 tcg_gen_or_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rD(ctx->opcode)], t0);
7869#else
7870 tcg_gen_shli_tl(t0, t0, 16);
7871 tcg_gen_mov_tl(cpu_gprh[rD(ctx->opcode)], t0);
7872 tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], t0);
7873#endif
7874 tcg_temp_free(t0);
0487d6a8
JM
7875}
7876
636aa200 7877static inline void gen_op_evlhhousplat(DisasContext *ctx, TCGv addr)
6a6ae23f
AJ
7878{
7879 TCGv t0 = tcg_temp_new();
76db3ba4 7880 gen_qemu_ld16u(ctx, t0, addr);
6a6ae23f
AJ
7881#if defined(TARGET_PPC64)
7882 tcg_gen_shli_tl(cpu_gpr[rD(ctx->opcode)], t0, 32);
7883 tcg_gen_or_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rD(ctx->opcode)], t0);
7884#else
7885 tcg_gen_mov_tl(cpu_gprh[rD(ctx->opcode)], t0);
7886 tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], t0);
7887#endif
7888 tcg_temp_free(t0);
0487d6a8
JM
7889}
7890
636aa200 7891static inline void gen_op_evlhhossplat(DisasContext *ctx, TCGv addr)
6a6ae23f
AJ
7892{
7893 TCGv t0 = tcg_temp_new();
76db3ba4 7894 gen_qemu_ld16s(ctx, t0, addr);
6a6ae23f
AJ
7895#if defined(TARGET_PPC64)
7896 tcg_gen_shli_tl(cpu_gpr[rD(ctx->opcode)], t0, 32);
7897 tcg_gen_ext32u_tl(t0, t0);
7898 tcg_gen_or_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rD(ctx->opcode)], t0);
7899#else
7900 tcg_gen_mov_tl(cpu_gprh[rD(ctx->opcode)], t0);
7901 tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], t0);
7902#endif
7903 tcg_temp_free(t0);
7904}
7905
636aa200 7906static inline void gen_op_evlwhe(DisasContext *ctx, TCGv addr)
6a6ae23f
AJ
7907{
7908 TCGv t0 = tcg_temp_new();
7909#if defined(TARGET_PPC64)
76db3ba4 7910 gen_qemu_ld16u(ctx, t0, addr);
6a6ae23f 7911 tcg_gen_shli_tl(cpu_gpr[rD(ctx->opcode)], t0, 48);
76db3ba4
AJ
7912 gen_addr_add(ctx, addr, addr, 2);
7913 gen_qemu_ld16u(ctx, t0, addr);
6a6ae23f
AJ
7914 tcg_gen_shli_tl(t0, t0, 16);
7915 tcg_gen_or_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rD(ctx->opcode)], t0);
7916#else
76db3ba4 7917 gen_qemu_ld16u(ctx, t0, addr);
6a6ae23f 7918 tcg_gen_shli_tl(cpu_gprh[rD(ctx->opcode)], t0, 16);
76db3ba4
AJ
7919 gen_addr_add(ctx, addr, addr, 2);
7920 gen_qemu_ld16u(ctx, t0, addr);
6a6ae23f
AJ
7921 tcg_gen_shli_tl(cpu_gpr[rD(ctx->opcode)], t0, 16);
7922#endif
7923 tcg_temp_free(t0);
7924}
7925
636aa200 7926static inline void gen_op_evlwhou(DisasContext *ctx, TCGv addr)
6a6ae23f
AJ
7927{
7928#if defined(TARGET_PPC64)
7929 TCGv t0 = tcg_temp_new();
76db3ba4
AJ
7930 gen_qemu_ld16u(ctx, cpu_gpr[rD(ctx->opcode)], addr);
7931 gen_addr_add(ctx, addr, addr, 2);
7932 gen_qemu_ld16u(ctx, t0, addr);
6a6ae23f
AJ
7933 tcg_gen_shli_tl(t0, t0, 32);
7934 tcg_gen_or_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rD(ctx->opcode)], t0);
7935 tcg_temp_free(t0);
7936#else
76db3ba4
AJ
7937 gen_qemu_ld16u(ctx, cpu_gprh[rD(ctx->opcode)], addr);
7938 gen_addr_add(ctx, addr, addr, 2);
7939 gen_qemu_ld16u(ctx, cpu_gpr[rD(ctx->opcode)], addr);
6a6ae23f
AJ
7940#endif
7941}
7942
636aa200 7943static inline void gen_op_evlwhos(DisasContext *ctx, TCGv addr)
6a6ae23f
AJ
7944{
7945#if defined(TARGET_PPC64)
7946 TCGv t0 = tcg_temp_new();
76db3ba4 7947 gen_qemu_ld16s(ctx, t0, addr);
6a6ae23f 7948 tcg_gen_ext32u_tl(cpu_gpr[rD(ctx->opcode)], t0);
76db3ba4
AJ
7949 gen_addr_add(ctx, addr, addr, 2);
7950 gen_qemu_ld16s(ctx, t0, addr);
6a6ae23f
AJ
7951 tcg_gen_shli_tl(t0, t0, 32);
7952 tcg_gen_or_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rD(ctx->opcode)], t0);
7953 tcg_temp_free(t0);
7954#else
76db3ba4
AJ
7955 gen_qemu_ld16s(ctx, cpu_gprh[rD(ctx->opcode)], addr);
7956 gen_addr_add(ctx, addr, addr, 2);
7957 gen_qemu_ld16s(ctx, cpu_gpr[rD(ctx->opcode)], addr);
6a6ae23f
AJ
7958#endif
7959}
7960
636aa200 7961static inline void gen_op_evlwwsplat(DisasContext *ctx, TCGv addr)
6a6ae23f
AJ
7962{
7963 TCGv t0 = tcg_temp_new();
76db3ba4 7964 gen_qemu_ld32u(ctx, t0, addr);
0487d6a8 7965#if defined(TARGET_PPC64)
6a6ae23f
AJ
7966 tcg_gen_shli_tl(cpu_gpr[rD(ctx->opcode)], t0, 32);
7967 tcg_gen_or_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rD(ctx->opcode)], t0);
7968#else
7969 tcg_gen_mov_tl(cpu_gprh[rD(ctx->opcode)], t0);
7970 tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], t0);
7971#endif
7972 tcg_temp_free(t0);
7973}
7974
636aa200 7975static inline void gen_op_evlwhsplat(DisasContext *ctx, TCGv addr)
6a6ae23f
AJ
7976{
7977 TCGv t0 = tcg_temp_new();
7978#if defined(TARGET_PPC64)
76db3ba4 7979 gen_qemu_ld16u(ctx, t0, addr);
6a6ae23f
AJ
7980 tcg_gen_shli_tl(cpu_gpr[rD(ctx->opcode)], t0, 48);
7981 tcg_gen_shli_tl(t0, t0, 32);
7982 tcg_gen_or_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rD(ctx->opcode)], t0);
76db3ba4
AJ
7983 gen_addr_add(ctx, addr, addr, 2);
7984 gen_qemu_ld16u(ctx, t0, addr);
6a6ae23f
AJ
7985 tcg_gen_or_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rD(ctx->opcode)], t0);
7986 tcg_gen_shli_tl(t0, t0, 16);
7987 tcg_gen_or_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rD(ctx->opcode)], t0);
7988#else
76db3ba4 7989 gen_qemu_ld16u(ctx, t0, addr);
6a6ae23f
AJ
7990 tcg_gen_shli_tl(cpu_gprh[rD(ctx->opcode)], t0, 16);
7991 tcg_gen_or_tl(cpu_gprh[rD(ctx->opcode)], cpu_gprh[rD(ctx->opcode)], t0);
76db3ba4
AJ
7992 gen_addr_add(ctx, addr, addr, 2);
7993 gen_qemu_ld16u(ctx, t0, addr);
6a6ae23f
AJ
7994 tcg_gen_shli_tl(cpu_gpr[rD(ctx->opcode)], t0, 16);
7995 tcg_gen_or_tl(cpu_gpr[rD(ctx->opcode)], cpu_gprh[rD(ctx->opcode)], t0);
0487d6a8 7996#endif
6a6ae23f
AJ
7997 tcg_temp_free(t0);
7998}
7999
636aa200 8000static inline void gen_op_evstdd(DisasContext *ctx, TCGv addr)
6a6ae23f
AJ
8001{
8002#if defined(TARGET_PPC64)
76db3ba4 8003 gen_qemu_st64(ctx, cpu_gpr[rS(ctx->opcode)], addr);
0487d6a8 8004#else
6a6ae23f
AJ
8005 TCGv_i64 t0 = tcg_temp_new_i64();
8006 tcg_gen_concat_i32_i64(t0, cpu_gpr[rS(ctx->opcode)], cpu_gprh[rS(ctx->opcode)]);
76db3ba4 8007 gen_qemu_st64(ctx, t0, addr);
6a6ae23f
AJ
8008 tcg_temp_free_i64(t0);
8009#endif
8010}
8011
636aa200 8012static inline void gen_op_evstdw(DisasContext *ctx, TCGv addr)
6a6ae23f 8013{
0487d6a8 8014#if defined(TARGET_PPC64)
6a6ae23f
AJ
8015 TCGv t0 = tcg_temp_new();
8016 tcg_gen_shri_tl(t0, cpu_gpr[rS(ctx->opcode)], 32);
76db3ba4 8017 gen_qemu_st32(ctx, t0, addr);
6a6ae23f
AJ
8018 tcg_temp_free(t0);
8019#else
76db3ba4 8020 gen_qemu_st32(ctx, cpu_gprh[rS(ctx->opcode)], addr);
6a6ae23f 8021#endif
76db3ba4
AJ
8022 gen_addr_add(ctx, addr, addr, 4);
8023 gen_qemu_st32(ctx, cpu_gpr[rS(ctx->opcode)], addr);
6a6ae23f
AJ
8024}
8025
636aa200 8026static inline void gen_op_evstdh(DisasContext *ctx, TCGv addr)
6a6ae23f
AJ
8027{
8028 TCGv t0 = tcg_temp_new();
8029#if defined(TARGET_PPC64)
8030 tcg_gen_shri_tl(t0, cpu_gpr[rS(ctx->opcode)], 48);
8031#else
8032 tcg_gen_shri_tl(t0, cpu_gprh[rS(ctx->opcode)], 16);
8033#endif
76db3ba4
AJ
8034 gen_qemu_st16(ctx, t0, addr);
8035 gen_addr_add(ctx, addr, addr, 2);
6a6ae23f
AJ
8036#if defined(TARGET_PPC64)
8037 tcg_gen_shri_tl(t0, cpu_gpr[rS(ctx->opcode)], 32);
76db3ba4 8038 gen_qemu_st16(ctx, t0, addr);
6a6ae23f 8039#else
76db3ba4 8040 gen_qemu_st16(ctx, cpu_gprh[rS(ctx->opcode)], addr);
6a6ae23f 8041#endif
76db3ba4 8042 gen_addr_add(ctx, addr, addr, 2);
6a6ae23f 8043 tcg_gen_shri_tl(t0, cpu_gpr[rS(ctx->opcode)], 16);
76db3ba4 8044 gen_qemu_st16(ctx, t0, addr);
6a6ae23f 8045 tcg_temp_free(t0);
76db3ba4
AJ
8046 gen_addr_add(ctx, addr, addr, 2);
8047 gen_qemu_st16(ctx, cpu_gpr[rS(ctx->opcode)], addr);
6a6ae23f
AJ
8048}
8049
636aa200 8050static inline void gen_op_evstwhe(DisasContext *ctx, TCGv addr)
6a6ae23f
AJ
8051{
8052 TCGv t0 = tcg_temp_new();
8053#if defined(TARGET_PPC64)
8054 tcg_gen_shri_tl(t0, cpu_gpr[rS(ctx->opcode)], 48);
8055#else
8056 tcg_gen_shri_tl(t0, cpu_gprh[rS(ctx->opcode)], 16);
8057#endif
76db3ba4
AJ
8058 gen_qemu_st16(ctx, t0, addr);
8059 gen_addr_add(ctx, addr, addr, 2);
6a6ae23f 8060 tcg_gen_shri_tl(t0, cpu_gpr[rS(ctx->opcode)], 16);
76db3ba4 8061 gen_qemu_st16(ctx, t0, addr);
6a6ae23f
AJ
8062 tcg_temp_free(t0);
8063}
8064
636aa200 8065static inline void gen_op_evstwho(DisasContext *ctx, TCGv addr)
6a6ae23f
AJ
8066{
8067#if defined(TARGET_PPC64)
8068 TCGv t0 = tcg_temp_new();
8069 tcg_gen_shri_tl(t0, cpu_gpr[rS(ctx->opcode)], 32);
76db3ba4 8070 gen_qemu_st16(ctx, t0, addr);
6a6ae23f
AJ
8071 tcg_temp_free(t0);
8072#else
76db3ba4 8073 gen_qemu_st16(ctx, cpu_gprh[rS(ctx->opcode)], addr);
6a6ae23f 8074#endif
76db3ba4
AJ
8075 gen_addr_add(ctx, addr, addr, 2);
8076 gen_qemu_st16(ctx, cpu_gpr[rS(ctx->opcode)], addr);
6a6ae23f
AJ
8077}
8078
636aa200 8079static inline void gen_op_evstwwe(DisasContext *ctx, TCGv addr)
6a6ae23f
AJ
8080{
8081#if defined(TARGET_PPC64)
8082 TCGv t0 = tcg_temp_new();
8083 tcg_gen_shri_tl(t0, cpu_gpr[rS(ctx->opcode)], 32);
76db3ba4 8084 gen_qemu_st32(ctx, t0, addr);
6a6ae23f
AJ
8085 tcg_temp_free(t0);
8086#else
76db3ba4 8087 gen_qemu_st32(ctx, cpu_gprh[rS(ctx->opcode)], addr);
6a6ae23f
AJ
8088#endif
8089}
8090
636aa200 8091static inline void gen_op_evstwwo(DisasContext *ctx, TCGv addr)
6a6ae23f 8092{
76db3ba4 8093 gen_qemu_st32(ctx, cpu_gpr[rS(ctx->opcode)], addr);
6a6ae23f
AJ
8094}
8095
8096#define GEN_SPEOP_LDST(name, opc2, sh) \
99e300ef 8097static void glue(gen_, name)(DisasContext *ctx) \
6a6ae23f
AJ
8098{ \
8099 TCGv t0; \
8100 if (unlikely(!ctx->spe_enabled)) { \
27a69bb0 8101 gen_exception(ctx, POWERPC_EXCP_SPEU); \
6a6ae23f
AJ
8102 return; \
8103 } \
76db3ba4 8104 gen_set_access_type(ctx, ACCESS_INT); \
6a6ae23f
AJ
8105 t0 = tcg_temp_new(); \
8106 if (Rc(ctx->opcode)) { \
76db3ba4 8107 gen_addr_spe_imm_index(ctx, t0, sh); \
6a6ae23f 8108 } else { \
76db3ba4 8109 gen_addr_reg_index(ctx, t0); \
6a6ae23f
AJ
8110 } \
8111 gen_op_##name(ctx, t0); \
8112 tcg_temp_free(t0); \
8113}
8114
8115GEN_SPEOP_LDST(evldd, 0x00, 3);
8116GEN_SPEOP_LDST(evldw, 0x01, 3);
8117GEN_SPEOP_LDST(evldh, 0x02, 3);
8118GEN_SPEOP_LDST(evlhhesplat, 0x04, 1);
8119GEN_SPEOP_LDST(evlhhousplat, 0x06, 1);
8120GEN_SPEOP_LDST(evlhhossplat, 0x07, 1);
8121GEN_SPEOP_LDST(evlwhe, 0x08, 2);
8122GEN_SPEOP_LDST(evlwhou, 0x0A, 2);
8123GEN_SPEOP_LDST(evlwhos, 0x0B, 2);
8124GEN_SPEOP_LDST(evlwwsplat, 0x0C, 2);
8125GEN_SPEOP_LDST(evlwhsplat, 0x0E, 2);
8126
8127GEN_SPEOP_LDST(evstdd, 0x10, 3);
8128GEN_SPEOP_LDST(evstdw, 0x11, 3);
8129GEN_SPEOP_LDST(evstdh, 0x12, 3);
8130GEN_SPEOP_LDST(evstwhe, 0x18, 2);
8131GEN_SPEOP_LDST(evstwho, 0x1A, 2);
8132GEN_SPEOP_LDST(evstwwe, 0x1C, 2);
8133GEN_SPEOP_LDST(evstwwo, 0x1E, 2);
0487d6a8
JM
8134
8135/* Multiply and add - TODO */
8136#if 0
70560da7
FC
8137GEN_SPE(speundef, evmhessf, 0x01, 0x10, 0xFFFFFFFF, 0x00000000, PPC_SPE);//
8138GEN_SPE(speundef, evmhossf, 0x03, 0x10, 0xFFFFFFFF, 0x00000000, PPC_SPE);
8139GEN_SPE(evmheumi, evmhesmi, 0x04, 0x10, 0x00000000, 0x00000000, PPC_SPE);
8140GEN_SPE(speundef, evmhesmf, 0x05, 0x10, 0xFFFFFFFF, 0x00000000, PPC_SPE);
8141GEN_SPE(evmhoumi, evmhosmi, 0x06, 0x10, 0x00000000, 0x00000000, PPC_SPE);
8142GEN_SPE(speundef, evmhosmf, 0x07, 0x10, 0xFFFFFFFF, 0x00000000, PPC_SPE);
8143GEN_SPE(speundef, evmhessfa, 0x11, 0x10, 0xFFFFFFFF, 0x00000000, PPC_SPE);
8144GEN_SPE(speundef, evmhossfa, 0x13, 0x10, 0xFFFFFFFF, 0x00000000, PPC_SPE);
8145GEN_SPE(evmheumia, evmhesmia, 0x14, 0x10, 0x00000000, 0x00000000, PPC_SPE);
8146GEN_SPE(speundef, evmhesmfa, 0x15, 0x10, 0xFFFFFFFF, 0x00000000, PPC_SPE);
8147GEN_SPE(evmhoumia, evmhosmia, 0x16, 0x10, 0x00000000, 0x00000000, PPC_SPE);
8148GEN_SPE(speundef, evmhosmfa, 0x17, 0x10, 0xFFFFFFFF, 0x00000000, PPC_SPE);
8149
8150GEN_SPE(speundef, evmwhssf, 0x03, 0x11, 0xFFFFFFFF, 0x00000000, PPC_SPE);
8151GEN_SPE(evmwlumi, speundef, 0x04, 0x11, 0x00000000, 0xFFFFFFFF, PPC_SPE);
8152GEN_SPE(evmwhumi, evmwhsmi, 0x06, 0x11, 0x00000000, 0x00000000, PPC_SPE);
8153GEN_SPE(speundef, evmwhsmf, 0x07, 0x11, 0xFFFFFFFF, 0x00000000, PPC_SPE);
8154GEN_SPE(speundef, evmwssf, 0x09, 0x11, 0xFFFFFFFF, 0x00000000, PPC_SPE);
8155GEN_SPE(speundef, evmwsmf, 0x0D, 0x11, 0xFFFFFFFF, 0x00000000, PPC_SPE);
8156GEN_SPE(speundef, evmwhssfa, 0x13, 0x11, 0xFFFFFFFF, 0x00000000, PPC_SPE);
8157GEN_SPE(evmwlumia, speundef, 0x14, 0x11, 0x00000000, 0xFFFFFFFF, PPC_SPE);
8158GEN_SPE(evmwhumia, evmwhsmia, 0x16, 0x11, 0x00000000, 0x00000000, PPC_SPE);
8159GEN_SPE(speundef, evmwhsmfa, 0x17, 0x11, 0xFFFFFFFF, 0x00000000, PPC_SPE);
8160GEN_SPE(speundef, evmwssfa, 0x19, 0x11, 0xFFFFFFFF, 0x00000000, PPC_SPE);
8161GEN_SPE(speundef, evmwsmfa, 0x1D, 0x11, 0xFFFFFFFF, 0x00000000, PPC_SPE);
8162
8163GEN_SPE(evadduiaaw, evaddsiaaw, 0x00, 0x13, 0x0000F800, 0x0000F800, PPC_SPE);
8164GEN_SPE(evsubfusiaaw, evsubfssiaaw, 0x01, 0x13, 0x0000F800, 0x0000F800, PPC_SPE);
8165GEN_SPE(evaddumiaaw, evaddsmiaaw, 0x04, 0x13, 0x0000F800, 0x0000F800, PPC_SPE);
8166GEN_SPE(evsubfumiaaw, evsubfsmiaaw, 0x05, 0x13, 0x0000F800, 0x0000F800, PPC_SPE);
8167GEN_SPE(evdivws, evdivwu, 0x06, 0x13, 0x00000000, 0x00000000, PPC_SPE);
8168
8169GEN_SPE(evmheusiaaw, evmhessiaaw, 0x00, 0x14, 0x00000000, 0x00000000, PPC_SPE);
8170GEN_SPE(speundef, evmhessfaaw, 0x01, 0x14, 0xFFFFFFFF, 0x00000000, PPC_SPE);
8171GEN_SPE(evmhousiaaw, evmhossiaaw, 0x02, 0x14, 0x00000000, 0x00000000, PPC_SPE);
8172GEN_SPE(speundef, evmhossfaaw, 0x03, 0x14, 0xFFFFFFFF, 0x00000000, PPC_SPE);
8173GEN_SPE(evmheumiaaw, evmhesmiaaw, 0x04, 0x14, 0x00000000, 0x00000000, PPC_SPE);
8174GEN_SPE(speundef, evmhesmfaaw, 0x05, 0x14, 0xFFFFFFFF, 0x00000000, PPC_SPE);
8175GEN_SPE(evmhoumiaaw, evmhosmiaaw, 0x06, 0x14, 0x00000000, 0x00000000, PPC_SPE);
8176GEN_SPE(speundef, evmhosmfaaw, 0x07, 0x14, 0xFFFFFFFF, 0x00000000, PPC_SPE);
8177GEN_SPE(evmhegumiaa, evmhegsmiaa, 0x14, 0x14, 0x00000000, 0x00000000, PPC_SPE);
8178GEN_SPE(speundef, evmhegsmfaa, 0x15, 0x14, 0xFFFFFFFF, 0x00000000, PPC_SPE);
8179GEN_SPE(evmhogumiaa, evmhogsmiaa, 0x16, 0x14, 0x00000000, 0x00000000, PPC_SPE);
8180GEN_SPE(speundef, evmhogsmfaa, 0x17, 0x14, 0xFFFFFFFF, 0x00000000, PPC_SPE);
8181
8182GEN_SPE(evmwlusiaaw, evmwlssiaaw, 0x00, 0x15, 0x00000000, 0x00000000, PPC_SPE);
8183GEN_SPE(evmwlumiaaw, evmwlsmiaaw, 0x04, 0x15, 0x00000000, 0x00000000, PPC_SPE);
8184GEN_SPE(speundef, evmwssfaa, 0x09, 0x15, 0xFFFFFFFF, 0x00000000, PPC_SPE);
8185GEN_SPE(speundef, evmwsmfaa, 0x0D, 0x15, 0xFFFFFFFF, 0x00000000, PPC_SPE);
8186
8187GEN_SPE(evmheusianw, evmhessianw, 0x00, 0x16, 0x00000000, 0x00000000, PPC_SPE);
8188GEN_SPE(speundef, evmhessfanw, 0x01, 0x16, 0xFFFFFFFF, 0x00000000, PPC_SPE);
8189GEN_SPE(evmhousianw, evmhossianw, 0x02, 0x16, 0x00000000, 0x00000000, PPC_SPE);
8190GEN_SPE(speundef, evmhossfanw, 0x03, 0x16, 0xFFFFFFFF, 0x00000000, PPC_SPE);
8191GEN_SPE(evmheumianw, evmhesmianw, 0x04, 0x16, 0x00000000, 0x00000000, PPC_SPE);
8192GEN_SPE(speundef, evmhesmfanw, 0x05, 0x16, 0xFFFFFFFF, 0x00000000, PPC_SPE);
8193GEN_SPE(evmhoumianw, evmhosmianw, 0x06, 0x16, 0x00000000, 0x00000000, PPC_SPE);
8194GEN_SPE(speundef, evmhosmfanw, 0x07, 0x16, 0xFFFFFFFF, 0x00000000, PPC_SPE);
8195GEN_SPE(evmhegumian, evmhegsmian, 0x14, 0x16, 0x00000000, 0x00000000, PPC_SPE);
8196GEN_SPE(speundef, evmhegsmfan, 0x15, 0x16, 0xFFFFFFFF, 0x00000000, PPC_SPE);
8197GEN_SPE(evmhigumian, evmhigsmian, 0x16, 0x16, 0x00000000, 0x00000000, PPC_SPE);
8198GEN_SPE(speundef, evmhogsmfan, 0x17, 0x16, 0xFFFFFFFF, 0x00000000, PPC_SPE);
8199
8200GEN_SPE(evmwlusianw, evmwlssianw, 0x00, 0x17, 0x00000000, 0x00000000, PPC_SPE);
8201GEN_SPE(evmwlumianw, evmwlsmianw, 0x04, 0x17, 0x00000000, 0x00000000, PPC_SPE);
8202GEN_SPE(speundef, evmwssfan, 0x09, 0x17, 0xFFFFFFFF, 0x00000000, PPC_SPE);
8203GEN_SPE(evmwumian, evmwsmian, 0x0C, 0x17, 0x00000000, 0x00000000, PPC_SPE);
8204GEN_SPE(speundef, evmwsmfan, 0x0D, 0x17, 0xFFFFFFFF, 0x00000000, PPC_SPE);
0487d6a8
JM
8205#endif
8206
8207/*** SPE floating-point extension ***/
1c97856d
AJ
8208#if defined(TARGET_PPC64)
8209#define GEN_SPEFPUOP_CONV_32_32(name) \
636aa200 8210static inline void gen_##name(DisasContext *ctx) \
0487d6a8 8211{ \
1c97856d
AJ
8212 TCGv_i32 t0; \
8213 TCGv t1; \
8214 t0 = tcg_temp_new_i32(); \
8215 tcg_gen_trunc_tl_i32(t0, cpu_gpr[rB(ctx->opcode)]); \
8e703949 8216 gen_helper_##name(t0, cpu_env, t0); \
1c97856d
AJ
8217 t1 = tcg_temp_new(); \
8218 tcg_gen_extu_i32_tl(t1, t0); \
8219 tcg_temp_free_i32(t0); \
8220 tcg_gen_andi_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rD(ctx->opcode)], \
8221 0xFFFFFFFF00000000ULL); \
8222 tcg_gen_or_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rD(ctx->opcode)], t1); \
8223 tcg_temp_free(t1); \
0487d6a8 8224}
1c97856d 8225#define GEN_SPEFPUOP_CONV_32_64(name) \
636aa200 8226static inline void gen_##name(DisasContext *ctx) \
1c97856d
AJ
8227{ \
8228 TCGv_i32 t0; \
8229 TCGv t1; \
8230 t0 = tcg_temp_new_i32(); \
8e703949 8231 gen_helper_##name(t0, cpu_env, cpu_gpr[rB(ctx->opcode)]); \
1c97856d
AJ
8232 t1 = tcg_temp_new(); \
8233 tcg_gen_extu_i32_tl(t1, t0); \
8234 tcg_temp_free_i32(t0); \
8235 tcg_gen_andi_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rD(ctx->opcode)], \
8236 0xFFFFFFFF00000000ULL); \
8237 tcg_gen_or_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rD(ctx->opcode)], t1); \
8238 tcg_temp_free(t1); \
8239}
8240#define GEN_SPEFPUOP_CONV_64_32(name) \
636aa200 8241static inline void gen_##name(DisasContext *ctx) \
1c97856d
AJ
8242{ \
8243 TCGv_i32 t0 = tcg_temp_new_i32(); \
8244 tcg_gen_trunc_tl_i32(t0, cpu_gpr[rB(ctx->opcode)]); \
8e703949 8245 gen_helper_##name(cpu_gpr[rD(ctx->opcode)], cpu_env, t0); \
1c97856d
AJ
8246 tcg_temp_free_i32(t0); \
8247}
8248#define GEN_SPEFPUOP_CONV_64_64(name) \
636aa200 8249static inline void gen_##name(DisasContext *ctx) \
1c97856d 8250{ \
8e703949
BS
8251 gen_helper_##name(cpu_gpr[rD(ctx->opcode)], cpu_env, \
8252 cpu_gpr[rB(ctx->opcode)]); \
1c97856d
AJ
8253}
8254#define GEN_SPEFPUOP_ARITH2_32_32(name) \
636aa200 8255static inline void gen_##name(DisasContext *ctx) \
57951c27 8256{ \
1c97856d
AJ
8257 TCGv_i32 t0, t1; \
8258 TCGv_i64 t2; \
57951c27 8259 if (unlikely(!ctx->spe_enabled)) { \
27a69bb0 8260 gen_exception(ctx, POWERPC_EXCP_SPEU); \
57951c27
AJ
8261 return; \
8262 } \
1c97856d
AJ
8263 t0 = tcg_temp_new_i32(); \
8264 t1 = tcg_temp_new_i32(); \
8265 tcg_gen_trunc_tl_i32(t0, cpu_gpr[rA(ctx->opcode)]); \
8266 tcg_gen_trunc_tl_i32(t1, cpu_gpr[rB(ctx->opcode)]); \
8e703949 8267 gen_helper_##name(t0, cpu_env, t0, t1); \
1c97856d
AJ
8268 tcg_temp_free_i32(t1); \
8269 t2 = tcg_temp_new(); \
8270 tcg_gen_extu_i32_tl(t2, t0); \
8271 tcg_temp_free_i32(t0); \
8272 tcg_gen_andi_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rD(ctx->opcode)], \
8273 0xFFFFFFFF00000000ULL); \
8274 tcg_gen_or_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rD(ctx->opcode)], t2); \
8275 tcg_temp_free(t2); \
57951c27 8276}
1c97856d 8277#define GEN_SPEFPUOP_ARITH2_64_64(name) \
636aa200 8278static inline void gen_##name(DisasContext *ctx) \
57951c27
AJ
8279{ \
8280 if (unlikely(!ctx->spe_enabled)) { \
27a69bb0 8281 gen_exception(ctx, POWERPC_EXCP_SPEU); \
57951c27
AJ
8282 return; \
8283 } \
8e703949
BS
8284 gen_helper_##name(cpu_gpr[rD(ctx->opcode)], cpu_env, \
8285 cpu_gpr[rA(ctx->opcode)], cpu_gpr[rB(ctx->opcode)]); \
57951c27 8286}
1c97856d 8287#define GEN_SPEFPUOP_COMP_32(name) \
636aa200 8288static inline void gen_##name(DisasContext *ctx) \
57951c27 8289{ \
1c97856d 8290 TCGv_i32 t0, t1; \
57951c27 8291 if (unlikely(!ctx->spe_enabled)) { \
27a69bb0 8292 gen_exception(ctx, POWERPC_EXCP_SPEU); \
57951c27
AJ
8293 return; \
8294 } \
1c97856d
AJ
8295 t0 = tcg_temp_new_i32(); \
8296 t1 = tcg_temp_new_i32(); \
8297 tcg_gen_trunc_tl_i32(t0, cpu_gpr[rA(ctx->opcode)]); \
8298 tcg_gen_trunc_tl_i32(t1, cpu_gpr[rB(ctx->opcode)]); \
8e703949 8299 gen_helper_##name(cpu_crf[crfD(ctx->opcode)], cpu_env, t0, t1); \
1c97856d
AJ
8300 tcg_temp_free_i32(t0); \
8301 tcg_temp_free_i32(t1); \
8302}
8303#define GEN_SPEFPUOP_COMP_64(name) \
636aa200 8304static inline void gen_##name(DisasContext *ctx) \
1c97856d
AJ
8305{ \
8306 if (unlikely(!ctx->spe_enabled)) { \
27a69bb0 8307 gen_exception(ctx, POWERPC_EXCP_SPEU); \
1c97856d
AJ
8308 return; \
8309 } \
8e703949 8310 gen_helper_##name(cpu_crf[crfD(ctx->opcode)], cpu_env, \
1c97856d
AJ
8311 cpu_gpr[rA(ctx->opcode)], cpu_gpr[rB(ctx->opcode)]); \
8312}
8313#else
8314#define GEN_SPEFPUOP_CONV_32_32(name) \
636aa200 8315static inline void gen_##name(DisasContext *ctx) \
1c97856d 8316{ \
8e703949
BS
8317 gen_helper_##name(cpu_gpr[rD(ctx->opcode)], cpu_env, \
8318 cpu_gpr[rB(ctx->opcode)]); \
57951c27 8319}
1c97856d 8320#define GEN_SPEFPUOP_CONV_32_64(name) \
636aa200 8321static inline void gen_##name(DisasContext *ctx) \
1c97856d
AJ
8322{ \
8323 TCGv_i64 t0 = tcg_temp_new_i64(); \
8324 gen_load_gpr64(t0, rB(ctx->opcode)); \
8e703949 8325 gen_helper_##name(cpu_gpr[rD(ctx->opcode)], cpu_env, t0); \
1c97856d
AJ
8326 tcg_temp_free_i64(t0); \
8327}
8328#define GEN_SPEFPUOP_CONV_64_32(name) \
636aa200 8329static inline void gen_##name(DisasContext *ctx) \
1c97856d
AJ
8330{ \
8331 TCGv_i64 t0 = tcg_temp_new_i64(); \
8e703949 8332 gen_helper_##name(t0, cpu_env, cpu_gpr[rB(ctx->opcode)]); \
1c97856d
AJ
8333 gen_store_gpr64(rD(ctx->opcode), t0); \
8334 tcg_temp_free_i64(t0); \
8335}
8336#define GEN_SPEFPUOP_CONV_64_64(name) \
636aa200 8337static inline void gen_##name(DisasContext *ctx) \
1c97856d
AJ
8338{ \
8339 TCGv_i64 t0 = tcg_temp_new_i64(); \
8340 gen_load_gpr64(t0, rB(ctx->opcode)); \
8e703949 8341 gen_helper_##name(t0, cpu_env, t0); \
1c97856d
AJ
8342 gen_store_gpr64(rD(ctx->opcode), t0); \
8343 tcg_temp_free_i64(t0); \
8344}
8345#define GEN_SPEFPUOP_ARITH2_32_32(name) \
636aa200 8346static inline void gen_##name(DisasContext *ctx) \
1c97856d
AJ
8347{ \
8348 if (unlikely(!ctx->spe_enabled)) { \
27a69bb0 8349 gen_exception(ctx, POWERPC_EXCP_SPEU); \
1c97856d
AJ
8350 return; \
8351 } \
8e703949 8352 gen_helper_##name(cpu_gpr[rD(ctx->opcode)], cpu_env, \
1c97856d
AJ
8353 cpu_gpr[rA(ctx->opcode)], cpu_gpr[rB(ctx->opcode)]); \
8354}
8355#define GEN_SPEFPUOP_ARITH2_64_64(name) \
636aa200 8356static inline void gen_##name(DisasContext *ctx) \
1c97856d
AJ
8357{ \
8358 TCGv_i64 t0, t1; \
8359 if (unlikely(!ctx->spe_enabled)) { \
27a69bb0 8360 gen_exception(ctx, POWERPC_EXCP_SPEU); \
1c97856d
AJ
8361 return; \
8362 } \
8363 t0 = tcg_temp_new_i64(); \
8364 t1 = tcg_temp_new_i64(); \
8365 gen_load_gpr64(t0, rA(ctx->opcode)); \
8366 gen_load_gpr64(t1, rB(ctx->opcode)); \
8e703949 8367 gen_helper_##name(t0, cpu_env, t0, t1); \
1c97856d
AJ
8368 gen_store_gpr64(rD(ctx->opcode), t0); \
8369 tcg_temp_free_i64(t0); \
8370 tcg_temp_free_i64(t1); \
8371}
8372#define GEN_SPEFPUOP_COMP_32(name) \
636aa200 8373static inline void gen_##name(DisasContext *ctx) \
1c97856d
AJ
8374{ \
8375 if (unlikely(!ctx->spe_enabled)) { \
27a69bb0 8376 gen_exception(ctx, POWERPC_EXCP_SPEU); \
1c97856d
AJ
8377 return; \
8378 } \
8e703949 8379 gen_helper_##name(cpu_crf[crfD(ctx->opcode)], cpu_env, \
1c97856d
AJ
8380 cpu_gpr[rA(ctx->opcode)], cpu_gpr[rB(ctx->opcode)]); \
8381}
8382#define GEN_SPEFPUOP_COMP_64(name) \
636aa200 8383static inline void gen_##name(DisasContext *ctx) \
1c97856d
AJ
8384{ \
8385 TCGv_i64 t0, t1; \
8386 if (unlikely(!ctx->spe_enabled)) { \
27a69bb0 8387 gen_exception(ctx, POWERPC_EXCP_SPEU); \
1c97856d
AJ
8388 return; \
8389 } \
8390 t0 = tcg_temp_new_i64(); \
8391 t1 = tcg_temp_new_i64(); \
8392 gen_load_gpr64(t0, rA(ctx->opcode)); \
8393 gen_load_gpr64(t1, rB(ctx->opcode)); \
8e703949 8394 gen_helper_##name(cpu_crf[crfD(ctx->opcode)], cpu_env, t0, t1); \
1c97856d
AJ
8395 tcg_temp_free_i64(t0); \
8396 tcg_temp_free_i64(t1); \
8397}
8398#endif
57951c27 8399
0487d6a8
JM
8400/* Single precision floating-point vectors operations */
8401/* Arithmetic */
1c97856d
AJ
8402GEN_SPEFPUOP_ARITH2_64_64(evfsadd);
8403GEN_SPEFPUOP_ARITH2_64_64(evfssub);
8404GEN_SPEFPUOP_ARITH2_64_64(evfsmul);
8405GEN_SPEFPUOP_ARITH2_64_64(evfsdiv);
636aa200 8406static inline void gen_evfsabs(DisasContext *ctx)
1c97856d
AJ
8407{
8408 if (unlikely(!ctx->spe_enabled)) {
27a69bb0 8409 gen_exception(ctx, POWERPC_EXCP_SPEU);
1c97856d
AJ
8410 return;
8411 }
8412#if defined(TARGET_PPC64)
6d5c34fa 8413 tcg_gen_andi_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)], ~0x8000000080000000LL);
1c97856d 8414#else
6d5c34fa
MP
8415 tcg_gen_andi_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)], ~0x80000000);
8416 tcg_gen_andi_tl(cpu_gprh[rD(ctx->opcode)], cpu_gprh[rA(ctx->opcode)], ~0x80000000);
1c97856d
AJ
8417#endif
8418}
636aa200 8419static inline void gen_evfsnabs(DisasContext *ctx)
1c97856d
AJ
8420{
8421 if (unlikely(!ctx->spe_enabled)) {
27a69bb0 8422 gen_exception(ctx, POWERPC_EXCP_SPEU);
1c97856d
AJ
8423 return;
8424 }
8425#if defined(TARGET_PPC64)
6d5c34fa 8426 tcg_gen_ori_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)], 0x8000000080000000LL);
1c97856d 8427#else
6d5c34fa
MP
8428 tcg_gen_ori_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)], 0x80000000);
8429 tcg_gen_ori_tl(cpu_gprh[rD(ctx->opcode)], cpu_gprh[rA(ctx->opcode)], 0x80000000);
1c97856d
AJ
8430#endif
8431}
636aa200 8432static inline void gen_evfsneg(DisasContext *ctx)
1c97856d
AJ
8433{
8434 if (unlikely(!ctx->spe_enabled)) {
27a69bb0 8435 gen_exception(ctx, POWERPC_EXCP_SPEU);
1c97856d
AJ
8436 return;
8437 }
8438#if defined(TARGET_PPC64)
6d5c34fa 8439 tcg_gen_xori_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)], 0x8000000080000000LL);
1c97856d 8440#else
6d5c34fa
MP
8441 tcg_gen_xori_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)], 0x80000000);
8442 tcg_gen_xori_tl(cpu_gprh[rD(ctx->opcode)], cpu_gprh[rA(ctx->opcode)], 0x80000000);
1c97856d
AJ
8443#endif
8444}
8445
0487d6a8 8446/* Conversion */
1c97856d
AJ
8447GEN_SPEFPUOP_CONV_64_64(evfscfui);
8448GEN_SPEFPUOP_CONV_64_64(evfscfsi);
8449GEN_SPEFPUOP_CONV_64_64(evfscfuf);
8450GEN_SPEFPUOP_CONV_64_64(evfscfsf);
8451GEN_SPEFPUOP_CONV_64_64(evfsctui);
8452GEN_SPEFPUOP_CONV_64_64(evfsctsi);
8453GEN_SPEFPUOP_CONV_64_64(evfsctuf);
8454GEN_SPEFPUOP_CONV_64_64(evfsctsf);
8455GEN_SPEFPUOP_CONV_64_64(evfsctuiz);
8456GEN_SPEFPUOP_CONV_64_64(evfsctsiz);
8457
0487d6a8 8458/* Comparison */
1c97856d
AJ
8459GEN_SPEFPUOP_COMP_64(evfscmpgt);
8460GEN_SPEFPUOP_COMP_64(evfscmplt);
8461GEN_SPEFPUOP_COMP_64(evfscmpeq);
8462GEN_SPEFPUOP_COMP_64(evfststgt);
8463GEN_SPEFPUOP_COMP_64(evfststlt);
8464GEN_SPEFPUOP_COMP_64(evfststeq);
0487d6a8
JM
8465
8466/* Opcodes definitions */
70560da7
FC
8467GEN_SPE(evfsadd, evfssub, 0x00, 0x0A, 0x00000000, 0x00000000, PPC_SPE_SINGLE); //
8468GEN_SPE(evfsabs, evfsnabs, 0x02, 0x0A, 0x0000F800, 0x0000F800, PPC_SPE_SINGLE); //
8469GEN_SPE(evfsneg, speundef, 0x03, 0x0A, 0x0000F800, 0xFFFFFFFF, PPC_SPE_SINGLE); //
8470GEN_SPE(evfsmul, evfsdiv, 0x04, 0x0A, 0x00000000, 0x00000000, PPC_SPE_SINGLE); //
8471GEN_SPE(evfscmpgt, evfscmplt, 0x06, 0x0A, 0x00600000, 0x00600000, PPC_SPE_SINGLE); //
8472GEN_SPE(evfscmpeq, speundef, 0x07, 0x0A, 0x00600000, 0xFFFFFFFF, PPC_SPE_SINGLE); //
8473GEN_SPE(evfscfui, evfscfsi, 0x08, 0x0A, 0x00180000, 0x00180000, PPC_SPE_SINGLE); //
8474GEN_SPE(evfscfuf, evfscfsf, 0x09, 0x0A, 0x00180000, 0x00180000, PPC_SPE_SINGLE); //
8475GEN_SPE(evfsctui, evfsctsi, 0x0A, 0x0A, 0x00180000, 0x00180000, PPC_SPE_SINGLE); //
8476GEN_SPE(evfsctuf, evfsctsf, 0x0B, 0x0A, 0x00180000, 0x00180000, PPC_SPE_SINGLE); //
8477GEN_SPE(evfsctuiz, speundef, 0x0C, 0x0A, 0x00180000, 0xFFFFFFFF, PPC_SPE_SINGLE); //
8478GEN_SPE(evfsctsiz, speundef, 0x0D, 0x0A, 0x00180000, 0xFFFFFFFF, PPC_SPE_SINGLE); //
8479GEN_SPE(evfststgt, evfststlt, 0x0E, 0x0A, 0x00600000, 0x00600000, PPC_SPE_SINGLE); //
8480GEN_SPE(evfststeq, speundef, 0x0F, 0x0A, 0x00600000, 0xFFFFFFFF, PPC_SPE_SINGLE); //
0487d6a8
JM
8481
8482/* Single precision floating-point operations */
8483/* Arithmetic */
1c97856d
AJ
8484GEN_SPEFPUOP_ARITH2_32_32(efsadd);
8485GEN_SPEFPUOP_ARITH2_32_32(efssub);
8486GEN_SPEFPUOP_ARITH2_32_32(efsmul);
8487GEN_SPEFPUOP_ARITH2_32_32(efsdiv);
636aa200 8488static inline void gen_efsabs(DisasContext *ctx)
1c97856d
AJ
8489{
8490 if (unlikely(!ctx->spe_enabled)) {
27a69bb0 8491 gen_exception(ctx, POWERPC_EXCP_SPEU);
1c97856d
AJ
8492 return;
8493 }
6d5c34fa 8494 tcg_gen_andi_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)], (target_long)~0x80000000LL);
1c97856d 8495}
636aa200 8496static inline void gen_efsnabs(DisasContext *ctx)
1c97856d
AJ
8497{
8498 if (unlikely(!ctx->spe_enabled)) {
27a69bb0 8499 gen_exception(ctx, POWERPC_EXCP_SPEU);
1c97856d
AJ
8500 return;
8501 }
6d5c34fa 8502 tcg_gen_ori_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)], 0x80000000);
1c97856d 8503}
636aa200 8504static inline void gen_efsneg(DisasContext *ctx)
1c97856d
AJ
8505{
8506 if (unlikely(!ctx->spe_enabled)) {
27a69bb0 8507 gen_exception(ctx, POWERPC_EXCP_SPEU);
1c97856d
AJ
8508 return;
8509 }
6d5c34fa 8510 tcg_gen_xori_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)], 0x80000000);
1c97856d
AJ
8511}
8512
0487d6a8 8513/* Conversion */
1c97856d
AJ
8514GEN_SPEFPUOP_CONV_32_32(efscfui);
8515GEN_SPEFPUOP_CONV_32_32(efscfsi);
8516GEN_SPEFPUOP_CONV_32_32(efscfuf);
8517GEN_SPEFPUOP_CONV_32_32(efscfsf);
8518GEN_SPEFPUOP_CONV_32_32(efsctui);
8519GEN_SPEFPUOP_CONV_32_32(efsctsi);
8520GEN_SPEFPUOP_CONV_32_32(efsctuf);
8521GEN_SPEFPUOP_CONV_32_32(efsctsf);
8522GEN_SPEFPUOP_CONV_32_32(efsctuiz);
8523GEN_SPEFPUOP_CONV_32_32(efsctsiz);
8524GEN_SPEFPUOP_CONV_32_64(efscfd);
8525
0487d6a8 8526/* Comparison */
1c97856d
AJ
8527GEN_SPEFPUOP_COMP_32(efscmpgt);
8528GEN_SPEFPUOP_COMP_32(efscmplt);
8529GEN_SPEFPUOP_COMP_32(efscmpeq);
8530GEN_SPEFPUOP_COMP_32(efststgt);
8531GEN_SPEFPUOP_COMP_32(efststlt);
8532GEN_SPEFPUOP_COMP_32(efststeq);
0487d6a8
JM
8533
8534/* Opcodes definitions */
70560da7
FC
8535GEN_SPE(efsadd, efssub, 0x00, 0x0B, 0x00000000, 0x00000000, PPC_SPE_SINGLE); //
8536GEN_SPE(efsabs, efsnabs, 0x02, 0x0B, 0x0000F800, 0x0000F800, PPC_SPE_SINGLE); //
8537GEN_SPE(efsneg, speundef, 0x03, 0x0B, 0x0000F800, 0xFFFFFFFF, PPC_SPE_SINGLE); //
8538GEN_SPE(efsmul, efsdiv, 0x04, 0x0B, 0x00000000, 0x00000000, PPC_SPE_SINGLE); //
8539GEN_SPE(efscmpgt, efscmplt, 0x06, 0x0B, 0x00600000, 0x00600000, PPC_SPE_SINGLE); //
8540GEN_SPE(efscmpeq, efscfd, 0x07, 0x0B, 0x00600000, 0x00180000, PPC_SPE_SINGLE); //
8541GEN_SPE(efscfui, efscfsi, 0x08, 0x0B, 0x00180000, 0x00180000, PPC_SPE_SINGLE); //
8542GEN_SPE(efscfuf, efscfsf, 0x09, 0x0B, 0x00180000, 0x00180000, PPC_SPE_SINGLE); //
8543GEN_SPE(efsctui, efsctsi, 0x0A, 0x0B, 0x00180000, 0x00180000, PPC_SPE_SINGLE); //
8544GEN_SPE(efsctuf, efsctsf, 0x0B, 0x0B, 0x00180000, 0x00180000, PPC_SPE_SINGLE); //
8545GEN_SPE(efsctuiz, speundef, 0x0C, 0x0B, 0x00180000, 0xFFFFFFFF, PPC_SPE_SINGLE); //
8546GEN_SPE(efsctsiz, speundef, 0x0D, 0x0B, 0x00180000, 0xFFFFFFFF, PPC_SPE_SINGLE); //
8547GEN_SPE(efststgt, efststlt, 0x0E, 0x0B, 0x00600000, 0x00600000, PPC_SPE_SINGLE); //
8548GEN_SPE(efststeq, speundef, 0x0F, 0x0B, 0x00600000, 0xFFFFFFFF, PPC_SPE_SINGLE); //
0487d6a8
JM
8549
8550/* Double precision floating-point operations */
8551/* Arithmetic */
1c97856d
AJ
8552GEN_SPEFPUOP_ARITH2_64_64(efdadd);
8553GEN_SPEFPUOP_ARITH2_64_64(efdsub);
8554GEN_SPEFPUOP_ARITH2_64_64(efdmul);
8555GEN_SPEFPUOP_ARITH2_64_64(efddiv);
636aa200 8556static inline void gen_efdabs(DisasContext *ctx)
1c97856d
AJ
8557{
8558 if (unlikely(!ctx->spe_enabled)) {
27a69bb0 8559 gen_exception(ctx, POWERPC_EXCP_SPEU);
1c97856d
AJ
8560 return;
8561 }
8562#if defined(TARGET_PPC64)
6d5c34fa 8563 tcg_gen_andi_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)], ~0x8000000000000000LL);
1c97856d 8564#else
6d5c34fa
MP
8565 tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)]);
8566 tcg_gen_andi_tl(cpu_gprh[rD(ctx->opcode)], cpu_gprh[rA(ctx->opcode)], ~0x80000000);
1c97856d
AJ
8567#endif
8568}
636aa200 8569static inline void gen_efdnabs(DisasContext *ctx)
1c97856d
AJ
8570{
8571 if (unlikely(!ctx->spe_enabled)) {
27a69bb0 8572 gen_exception(ctx, POWERPC_EXCP_SPEU);
1c97856d
AJ
8573 return;
8574 }
8575#if defined(TARGET_PPC64)
6d5c34fa 8576 tcg_gen_ori_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)], 0x8000000000000000LL);
1c97856d 8577#else
6d5c34fa
MP
8578 tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)]);
8579 tcg_gen_ori_tl(cpu_gprh[rD(ctx->opcode)], cpu_gprh[rA(ctx->opcode)], 0x80000000);
1c97856d
AJ
8580#endif
8581}
636aa200 8582static inline void gen_efdneg(DisasContext *ctx)
1c97856d
AJ
8583{
8584 if (unlikely(!ctx->spe_enabled)) {
27a69bb0 8585 gen_exception(ctx, POWERPC_EXCP_SPEU);
1c97856d
AJ
8586 return;
8587 }
8588#if defined(TARGET_PPC64)
6d5c34fa 8589 tcg_gen_xori_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)], 0x8000000000000000LL);
1c97856d 8590#else
6d5c34fa
MP
8591 tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)]);
8592 tcg_gen_xori_tl(cpu_gprh[rD(ctx->opcode)], cpu_gprh[rA(ctx->opcode)], 0x80000000);
1c97856d
AJ
8593#endif
8594}
8595
0487d6a8 8596/* Conversion */
1c97856d
AJ
8597GEN_SPEFPUOP_CONV_64_32(efdcfui);
8598GEN_SPEFPUOP_CONV_64_32(efdcfsi);
8599GEN_SPEFPUOP_CONV_64_32(efdcfuf);
8600GEN_SPEFPUOP_CONV_64_32(efdcfsf);
8601GEN_SPEFPUOP_CONV_32_64(efdctui);
8602GEN_SPEFPUOP_CONV_32_64(efdctsi);
8603GEN_SPEFPUOP_CONV_32_64(efdctuf);
8604GEN_SPEFPUOP_CONV_32_64(efdctsf);
8605GEN_SPEFPUOP_CONV_32_64(efdctuiz);
8606GEN_SPEFPUOP_CONV_32_64(efdctsiz);
8607GEN_SPEFPUOP_CONV_64_32(efdcfs);
8608GEN_SPEFPUOP_CONV_64_64(efdcfuid);
8609GEN_SPEFPUOP_CONV_64_64(efdcfsid);
8610GEN_SPEFPUOP_CONV_64_64(efdctuidz);
8611GEN_SPEFPUOP_CONV_64_64(efdctsidz);
0487d6a8 8612
0487d6a8 8613/* Comparison */
1c97856d
AJ
8614GEN_SPEFPUOP_COMP_64(efdcmpgt);
8615GEN_SPEFPUOP_COMP_64(efdcmplt);
8616GEN_SPEFPUOP_COMP_64(efdcmpeq);
8617GEN_SPEFPUOP_COMP_64(efdtstgt);
8618GEN_SPEFPUOP_COMP_64(efdtstlt);
8619GEN_SPEFPUOP_COMP_64(efdtsteq);
0487d6a8
JM
8620
8621/* Opcodes definitions */
70560da7
FC
8622GEN_SPE(efdadd, efdsub, 0x10, 0x0B, 0x00000000, 0x00000000, PPC_SPE_DOUBLE); //
8623GEN_SPE(efdcfuid, efdcfsid, 0x11, 0x0B, 0x00180000, 0x00180000, PPC_SPE_DOUBLE); //
8624GEN_SPE(efdabs, efdnabs, 0x12, 0x0B, 0x0000F800, 0x0000F800, PPC_SPE_DOUBLE); //
8625GEN_SPE(efdneg, speundef, 0x13, 0x0B, 0x0000F800, 0xFFFFFFFF, PPC_SPE_DOUBLE); //
8626GEN_SPE(efdmul, efddiv, 0x14, 0x0B, 0x00000000, 0x00000000, PPC_SPE_DOUBLE); //
8627GEN_SPE(efdctuidz, efdctsidz, 0x15, 0x0B, 0x00180000, 0x00180000, PPC_SPE_DOUBLE); //
8628GEN_SPE(efdcmpgt, efdcmplt, 0x16, 0x0B, 0x00600000, 0x00600000, PPC_SPE_DOUBLE); //
8629GEN_SPE(efdcmpeq, efdcfs, 0x17, 0x0B, 0x00600000, 0x00180000, PPC_SPE_DOUBLE); //
8630GEN_SPE(efdcfui, efdcfsi, 0x18, 0x0B, 0x00180000, 0x00180000, PPC_SPE_DOUBLE); //
8631GEN_SPE(efdcfuf, efdcfsf, 0x19, 0x0B, 0x00180000, 0x00180000, PPC_SPE_DOUBLE); //
8632GEN_SPE(efdctui, efdctsi, 0x1A, 0x0B, 0x00180000, 0x00180000, PPC_SPE_DOUBLE); //
8633GEN_SPE(efdctuf, efdctsf, 0x1B, 0x0B, 0x00180000, 0x00180000, PPC_SPE_DOUBLE); //
8634GEN_SPE(efdctuiz, speundef, 0x1C, 0x0B, 0x00180000, 0xFFFFFFFF, PPC_SPE_DOUBLE); //
8635GEN_SPE(efdctsiz, speundef, 0x1D, 0x0B, 0x00180000, 0xFFFFFFFF, PPC_SPE_DOUBLE); //
8636GEN_SPE(efdtstgt, efdtstlt, 0x1E, 0x0B, 0x00600000, 0x00600000, PPC_SPE_DOUBLE); //
8637GEN_SPE(efdtsteq, speundef, 0x1F, 0x0B, 0x00600000, 0xFFFFFFFF, PPC_SPE_DOUBLE); //
0487d6a8 8638
c227f099 8639static opcode_t opcodes[] = {
5c55ff99
BS
8640GEN_HANDLER(invalid, 0x00, 0x00, 0x00, 0xFFFFFFFF, PPC_NONE),
8641GEN_HANDLER(cmp, 0x1F, 0x00, 0x00, 0x00400000, PPC_INTEGER),
8642GEN_HANDLER(cmpi, 0x0B, 0xFF, 0xFF, 0x00400000, PPC_INTEGER),
8643GEN_HANDLER(cmpl, 0x1F, 0x00, 0x01, 0x00400000, PPC_INTEGER),
8644GEN_HANDLER(cmpli, 0x0A, 0xFF, 0xFF, 0x00400000, PPC_INTEGER),
fcfda20f 8645GEN_HANDLER_E(cmpb, 0x1F, 0x1C, 0x0F, 0x00000001, PPC_NONE, PPC2_ISA205),
5c55ff99
BS
8646GEN_HANDLER(isel, 0x1F, 0x0F, 0xFF, 0x00000001, PPC_ISEL),
8647GEN_HANDLER(addi, 0x0E, 0xFF, 0xFF, 0x00000000, PPC_INTEGER),
8648GEN_HANDLER(addic, 0x0C, 0xFF, 0xFF, 0x00000000, PPC_INTEGER),
8649GEN_HANDLER2(addic_, "addic.", 0x0D, 0xFF, 0xFF, 0x00000000, PPC_INTEGER),
8650GEN_HANDLER(addis, 0x0F, 0xFF, 0xFF, 0x00000000, PPC_INTEGER),
8651GEN_HANDLER(mulhw, 0x1F, 0x0B, 0x02, 0x00000400, PPC_INTEGER),
8652GEN_HANDLER(mulhwu, 0x1F, 0x0B, 0x00, 0x00000400, PPC_INTEGER),
8653GEN_HANDLER(mullw, 0x1F, 0x0B, 0x07, 0x00000000, PPC_INTEGER),
8654GEN_HANDLER(mullwo, 0x1F, 0x0B, 0x17, 0x00000000, PPC_INTEGER),
8655GEN_HANDLER(mulli, 0x07, 0xFF, 0xFF, 0x00000000, PPC_INTEGER),
8656#if defined(TARGET_PPC64)
8657GEN_HANDLER(mulld, 0x1F, 0x09, 0x07, 0x00000000, PPC_64B),
8658#endif
8659GEN_HANDLER(neg, 0x1F, 0x08, 0x03, 0x0000F800, PPC_INTEGER),
8660GEN_HANDLER(nego, 0x1F, 0x08, 0x13, 0x0000F800, PPC_INTEGER),
8661GEN_HANDLER(subfic, 0x08, 0xFF, 0xFF, 0x00000000, PPC_INTEGER),
8662GEN_HANDLER2(andi_, "andi.", 0x1C, 0xFF, 0xFF, 0x00000000, PPC_INTEGER),
8663GEN_HANDLER2(andis_, "andis.", 0x1D, 0xFF, 0xFF, 0x00000000, PPC_INTEGER),
8664GEN_HANDLER(cntlzw, 0x1F, 0x1A, 0x00, 0x00000000, PPC_INTEGER),
8665GEN_HANDLER(or, 0x1F, 0x1C, 0x0D, 0x00000000, PPC_INTEGER),
8666GEN_HANDLER(xor, 0x1F, 0x1C, 0x09, 0x00000000, PPC_INTEGER),
8667GEN_HANDLER(ori, 0x18, 0xFF, 0xFF, 0x00000000, PPC_INTEGER),
8668GEN_HANDLER(oris, 0x19, 0xFF, 0xFF, 0x00000000, PPC_INTEGER),
8669GEN_HANDLER(xori, 0x1A, 0xFF, 0xFF, 0x00000000, PPC_INTEGER),
8670GEN_HANDLER(xoris, 0x1B, 0xFF, 0xFF, 0x00000000, PPC_INTEGER),
8671GEN_HANDLER(popcntb, 0x1F, 0x03, 0x03, 0x0000F801, PPC_POPCNTB),
eaabeef2 8672GEN_HANDLER(popcntw, 0x1F, 0x1A, 0x0b, 0x0000F801, PPC_POPCNTWD),
725bcec2 8673GEN_HANDLER_E(prtyw, 0x1F, 0x1A, 0x04, 0x0000F801, PPC_NONE, PPC2_ISA205),
5c55ff99 8674#if defined(TARGET_PPC64)
eaabeef2 8675GEN_HANDLER(popcntd, 0x1F, 0x1A, 0x0F, 0x0000F801, PPC_POPCNTWD),
5c55ff99 8676GEN_HANDLER(cntlzd, 0x1F, 0x1A, 0x01, 0x00000000, PPC_64B),
725bcec2 8677GEN_HANDLER_E(prtyd, 0x1F, 0x1A, 0x05, 0x0000F801, PPC_NONE, PPC2_ISA205),
5c55ff99
BS
8678#endif
8679GEN_HANDLER(rlwimi, 0x14, 0xFF, 0xFF, 0x00000000, PPC_INTEGER),
8680GEN_HANDLER(rlwinm, 0x15, 0xFF, 0xFF, 0x00000000, PPC_INTEGER),
8681GEN_HANDLER(rlwnm, 0x17, 0xFF, 0xFF, 0x00000000, PPC_INTEGER),
8682GEN_HANDLER(slw, 0x1F, 0x18, 0x00, 0x00000000, PPC_INTEGER),
8683GEN_HANDLER(sraw, 0x1F, 0x18, 0x18, 0x00000000, PPC_INTEGER),
8684GEN_HANDLER(srawi, 0x1F, 0x18, 0x19, 0x00000000, PPC_INTEGER),
8685GEN_HANDLER(srw, 0x1F, 0x18, 0x10, 0x00000000, PPC_INTEGER),
8686#if defined(TARGET_PPC64)
8687GEN_HANDLER(sld, 0x1F, 0x1B, 0x00, 0x00000000, PPC_64B),
8688GEN_HANDLER(srad, 0x1F, 0x1A, 0x18, 0x00000000, PPC_64B),
8689GEN_HANDLER2(sradi0, "sradi", 0x1F, 0x1A, 0x19, 0x00000000, PPC_64B),
8690GEN_HANDLER2(sradi1, "sradi", 0x1F, 0x1B, 0x19, 0x00000000, PPC_64B),
8691GEN_HANDLER(srd, 0x1F, 0x1B, 0x10, 0x00000000, PPC_64B),
8692#endif
8693GEN_HANDLER(frsqrtes, 0x3B, 0x1A, 0xFF, 0x001F07C0, PPC_FLOAT_FRSQRTES),
8694GEN_HANDLER(fsqrt, 0x3F, 0x16, 0xFF, 0x001F07C0, PPC_FLOAT_FSQRT),
8695GEN_HANDLER(fsqrts, 0x3B, 0x16, 0xFF, 0x001F07C0, PPC_FLOAT_FSQRT),
8696GEN_HANDLER(fcmpo, 0x3F, 0x00, 0x01, 0x00600001, PPC_FLOAT),
8697GEN_HANDLER(fcmpu, 0x3F, 0x00, 0x00, 0x00600001, PPC_FLOAT),
bf45a2e6 8698GEN_HANDLER(fabs, 0x3F, 0x08, 0x08, 0x001F0000, PPC_FLOAT),
5c55ff99 8699GEN_HANDLER(fmr, 0x3F, 0x08, 0x02, 0x001F0000, PPC_FLOAT),
bf45a2e6
AJ
8700GEN_HANDLER(fnabs, 0x3F, 0x08, 0x04, 0x001F0000, PPC_FLOAT),
8701GEN_HANDLER(fneg, 0x3F, 0x08, 0x01, 0x001F0000, PPC_FLOAT),
f0332888 8702GEN_HANDLER_E(fcpsgn, 0x3F, 0x08, 0x00, 0x00000000, PPC_NONE, PPC2_ISA205),
5c55ff99
BS
8703GEN_HANDLER(mcrfs, 0x3F, 0x00, 0x02, 0x0063F801, PPC_FLOAT),
8704GEN_HANDLER(mffs, 0x3F, 0x07, 0x12, 0x001FF800, PPC_FLOAT),
8705GEN_HANDLER(mtfsb0, 0x3F, 0x06, 0x02, 0x001FF800, PPC_FLOAT),
8706GEN_HANDLER(mtfsb1, 0x3F, 0x06, 0x01, 0x001FF800, PPC_FLOAT),
7d08d856
AJ
8707GEN_HANDLER(mtfsf, 0x3F, 0x07, 0x16, 0x00000000, PPC_FLOAT),
8708GEN_HANDLER(mtfsfi, 0x3F, 0x06, 0x04, 0x006e0800, PPC_FLOAT),
5c55ff99
BS
8709#if defined(TARGET_PPC64)
8710GEN_HANDLER(ld, 0x3A, 0xFF, 0xFF, 0x00000000, PPC_64B),
8711GEN_HANDLER(lq, 0x38, 0xFF, 0xFF, 0x00000000, PPC_64BX),
8712GEN_HANDLER(std, 0x3E, 0xFF, 0xFF, 0x00000000, PPC_64B),
8713#endif
8714GEN_HANDLER(lmw, 0x2E, 0xFF, 0xFF, 0x00000000, PPC_INTEGER),
8715GEN_HANDLER(stmw, 0x2F, 0xFF, 0xFF, 0x00000000, PPC_INTEGER),
8716GEN_HANDLER(lswi, 0x1F, 0x15, 0x12, 0x00000001, PPC_STRING),
8717GEN_HANDLER(lswx, 0x1F, 0x15, 0x10, 0x00000001, PPC_STRING),
8718GEN_HANDLER(stswi, 0x1F, 0x15, 0x16, 0x00000001, PPC_STRING),
8719GEN_HANDLER(stswx, 0x1F, 0x15, 0x14, 0x00000001, PPC_STRING),
8720GEN_HANDLER(eieio, 0x1F, 0x16, 0x1A, 0x03FFF801, PPC_MEM_EIEIO),
8721GEN_HANDLER(isync, 0x13, 0x16, 0x04, 0x03FFF801, PPC_MEM),
f844c817 8722GEN_HANDLER(lwarx, 0x1F, 0x14, 0x00, 0x00000000, PPC_RES),
5c55ff99
BS
8723GEN_HANDLER2(stwcx_, "stwcx.", 0x1F, 0x16, 0x04, 0x00000000, PPC_RES),
8724#if defined(TARGET_PPC64)
f844c817 8725GEN_HANDLER(ldarx, 0x1F, 0x14, 0x02, 0x00000000, PPC_64B),
5c55ff99
BS
8726GEN_HANDLER2(stdcx_, "stdcx.", 0x1F, 0x16, 0x06, 0x00000000, PPC_64B),
8727#endif
8728GEN_HANDLER(sync, 0x1F, 0x16, 0x12, 0x039FF801, PPC_MEM_SYNC),
8729GEN_HANDLER(wait, 0x1F, 0x1E, 0x01, 0x03FFF801, PPC_WAIT),
8730GEN_HANDLER(b, 0x12, 0xFF, 0xFF, 0x00000000, PPC_FLOW),
8731GEN_HANDLER(bc, 0x10, 0xFF, 0xFF, 0x00000000, PPC_FLOW),
8732GEN_HANDLER(bcctr, 0x13, 0x10, 0x10, 0x00000000, PPC_FLOW),
8733GEN_HANDLER(bclr, 0x13, 0x10, 0x00, 0x00000000, PPC_FLOW),
8734GEN_HANDLER(mcrf, 0x13, 0x00, 0xFF, 0x00000001, PPC_INTEGER),
8735GEN_HANDLER(rfi, 0x13, 0x12, 0x01, 0x03FF8001, PPC_FLOW),
8736#if defined(TARGET_PPC64)
8737GEN_HANDLER(rfid, 0x13, 0x12, 0x00, 0x03FF8001, PPC_64B),
8738GEN_HANDLER(hrfid, 0x13, 0x12, 0x08, 0x03FF8001, PPC_64H),
8739#endif
8740GEN_HANDLER(sc, 0x11, 0xFF, 0xFF, 0x03FFF01D, PPC_FLOW),
8741GEN_HANDLER(tw, 0x1F, 0x04, 0x00, 0x00000001, PPC_FLOW),
8742GEN_HANDLER(twi, 0x03, 0xFF, 0xFF, 0x00000000, PPC_FLOW),
8743#if defined(TARGET_PPC64)
8744GEN_HANDLER(td, 0x1F, 0x04, 0x02, 0x00000001, PPC_64B),
8745GEN_HANDLER(tdi, 0x02, 0xFF, 0xFF, 0x00000000, PPC_64B),
8746#endif
8747GEN_HANDLER(mcrxr, 0x1F, 0x00, 0x10, 0x007FF801, PPC_MISC),
8748GEN_HANDLER(mfcr, 0x1F, 0x13, 0x00, 0x00000801, PPC_MISC),
8749GEN_HANDLER(mfmsr, 0x1F, 0x13, 0x02, 0x001FF801, PPC_MISC),
8750GEN_HANDLER(mfspr, 0x1F, 0x13, 0x0A, 0x00000001, PPC_MISC),
8751GEN_HANDLER(mftb, 0x1F, 0x13, 0x0B, 0x00000001, PPC_MFTB),
8752GEN_HANDLER(mtcrf, 0x1F, 0x10, 0x04, 0x00000801, PPC_MISC),
8753#if defined(TARGET_PPC64)
8754GEN_HANDLER(mtmsrd, 0x1F, 0x12, 0x05, 0x001EF801, PPC_64B),
8755#endif
8756GEN_HANDLER(mtmsr, 0x1F, 0x12, 0x04, 0x001FF801, PPC_MISC),
8757GEN_HANDLER(mtspr, 0x1F, 0x13, 0x0E, 0x00000001, PPC_MISC),
8758GEN_HANDLER(dcbf, 0x1F, 0x16, 0x02, 0x03C00001, PPC_CACHE),
8759GEN_HANDLER(dcbi, 0x1F, 0x16, 0x0E, 0x03E00001, PPC_CACHE),
8760GEN_HANDLER(dcbst, 0x1F, 0x16, 0x01, 0x03E00001, PPC_CACHE),
8761GEN_HANDLER(dcbt, 0x1F, 0x16, 0x08, 0x02000001, PPC_CACHE),
8762GEN_HANDLER(dcbtst, 0x1F, 0x16, 0x07, 0x02000001, PPC_CACHE),
8e33944f 8763GEN_HANDLER(dcbz, 0x1F, 0x16, 0x1F, 0x03C00001, PPC_CACHE_DCBZ),
5c55ff99
BS
8764GEN_HANDLER(dst, 0x1F, 0x16, 0x0A, 0x01800001, PPC_ALTIVEC),
8765GEN_HANDLER(dstst, 0x1F, 0x16, 0x0B, 0x02000001, PPC_ALTIVEC),
8766GEN_HANDLER(dss, 0x1F, 0x16, 0x19, 0x019FF801, PPC_ALTIVEC),
8767GEN_HANDLER(icbi, 0x1F, 0x16, 0x1E, 0x03E00001, PPC_CACHE_ICBI),
8768GEN_HANDLER(dcba, 0x1F, 0x16, 0x17, 0x03E00001, PPC_CACHE_DCBA),
8769GEN_HANDLER(mfsr, 0x1F, 0x13, 0x12, 0x0010F801, PPC_SEGMENT),
8770GEN_HANDLER(mfsrin, 0x1F, 0x13, 0x14, 0x001F0001, PPC_SEGMENT),
8771GEN_HANDLER(mtsr, 0x1F, 0x12, 0x06, 0x0010F801, PPC_SEGMENT),
8772GEN_HANDLER(mtsrin, 0x1F, 0x12, 0x07, 0x001F0001, PPC_SEGMENT),
8773#if defined(TARGET_PPC64)
8774GEN_HANDLER2(mfsr_64b, "mfsr", 0x1F, 0x13, 0x12, 0x0010F801, PPC_SEGMENT_64B),
8775GEN_HANDLER2(mfsrin_64b, "mfsrin", 0x1F, 0x13, 0x14, 0x001F0001,
8776 PPC_SEGMENT_64B),
8777GEN_HANDLER2(mtsr_64b, "mtsr", 0x1F, 0x12, 0x06, 0x0010F801, PPC_SEGMENT_64B),
8778GEN_HANDLER2(mtsrin_64b, "mtsrin", 0x1F, 0x12, 0x07, 0x001F0001,
8779 PPC_SEGMENT_64B),
efdef95f
DG
8780GEN_HANDLER2(slbmte, "slbmte", 0x1F, 0x12, 0x0C, 0x001F0001, PPC_SEGMENT_64B),
8781GEN_HANDLER2(slbmfee, "slbmfee", 0x1F, 0x13, 0x1C, 0x001F0001, PPC_SEGMENT_64B),
8782GEN_HANDLER2(slbmfev, "slbmfev", 0x1F, 0x13, 0x1A, 0x001F0001, PPC_SEGMENT_64B),
5c55ff99
BS
8783#endif
8784GEN_HANDLER(tlbia, 0x1F, 0x12, 0x0B, 0x03FFFC01, PPC_MEM_TLBIA),
8785GEN_HANDLER(tlbiel, 0x1F, 0x12, 0x08, 0x03FF0001, PPC_MEM_TLBIE),
8786GEN_HANDLER(tlbie, 0x1F, 0x12, 0x09, 0x03FF0001, PPC_MEM_TLBIE),
8787GEN_HANDLER(tlbsync, 0x1F, 0x16, 0x11, 0x03FFF801, PPC_MEM_TLBSYNC),
8788#if defined(TARGET_PPC64)
8789GEN_HANDLER(slbia, 0x1F, 0x12, 0x0F, 0x03FFFC01, PPC_SLBI),
8790GEN_HANDLER(slbie, 0x1F, 0x12, 0x0D, 0x03FF0001, PPC_SLBI),
8791#endif
8792GEN_HANDLER(eciwx, 0x1F, 0x16, 0x0D, 0x00000001, PPC_EXTERN),
8793GEN_HANDLER(ecowx, 0x1F, 0x16, 0x09, 0x00000001, PPC_EXTERN),
8794GEN_HANDLER(abs, 0x1F, 0x08, 0x0B, 0x0000F800, PPC_POWER_BR),
8795GEN_HANDLER(abso, 0x1F, 0x08, 0x1B, 0x0000F800, PPC_POWER_BR),
8796GEN_HANDLER(clcs, 0x1F, 0x10, 0x13, 0x0000F800, PPC_POWER_BR),
8797GEN_HANDLER(div, 0x1F, 0x0B, 0x0A, 0x00000000, PPC_POWER_BR),
8798GEN_HANDLER(divo, 0x1F, 0x0B, 0x1A, 0x00000000, PPC_POWER_BR),
8799GEN_HANDLER(divs, 0x1F, 0x0B, 0x0B, 0x00000000, PPC_POWER_BR),
8800GEN_HANDLER(divso, 0x1F, 0x0B, 0x1B, 0x00000000, PPC_POWER_BR),
8801GEN_HANDLER(doz, 0x1F, 0x08, 0x08, 0x00000000, PPC_POWER_BR),
8802GEN_HANDLER(dozo, 0x1F, 0x08, 0x18, 0x00000000, PPC_POWER_BR),
8803GEN_HANDLER(dozi, 0x09, 0xFF, 0xFF, 0x00000000, PPC_POWER_BR),
8804GEN_HANDLER(lscbx, 0x1F, 0x15, 0x08, 0x00000000, PPC_POWER_BR),
8805GEN_HANDLER(maskg, 0x1F, 0x1D, 0x00, 0x00000000, PPC_POWER_BR),
8806GEN_HANDLER(maskir, 0x1F, 0x1D, 0x10, 0x00000000, PPC_POWER_BR),
8807GEN_HANDLER(mul, 0x1F, 0x0B, 0x03, 0x00000000, PPC_POWER_BR),
8808GEN_HANDLER(mulo, 0x1F, 0x0B, 0x13, 0x00000000, PPC_POWER_BR),
8809GEN_HANDLER(nabs, 0x1F, 0x08, 0x0F, 0x00000000, PPC_POWER_BR),
8810GEN_HANDLER(nabso, 0x1F, 0x08, 0x1F, 0x00000000, PPC_POWER_BR),
8811GEN_HANDLER(rlmi, 0x16, 0xFF, 0xFF, 0x00000000, PPC_POWER_BR),
8812GEN_HANDLER(rrib, 0x1F, 0x19, 0x10, 0x00000000, PPC_POWER_BR),
8813GEN_HANDLER(sle, 0x1F, 0x19, 0x04, 0x00000000, PPC_POWER_BR),
8814GEN_HANDLER(sleq, 0x1F, 0x19, 0x06, 0x00000000, PPC_POWER_BR),
8815GEN_HANDLER(sliq, 0x1F, 0x18, 0x05, 0x00000000, PPC_POWER_BR),
8816GEN_HANDLER(slliq, 0x1F, 0x18, 0x07, 0x00000000, PPC_POWER_BR),
8817GEN_HANDLER(sllq, 0x1F, 0x18, 0x06, 0x00000000, PPC_POWER_BR),
8818GEN_HANDLER(slq, 0x1F, 0x18, 0x04, 0x00000000, PPC_POWER_BR),
8819GEN_HANDLER(sraiq, 0x1F, 0x18, 0x1D, 0x00000000, PPC_POWER_BR),
8820GEN_HANDLER(sraq, 0x1F, 0x18, 0x1C, 0x00000000, PPC_POWER_BR),
8821GEN_HANDLER(sre, 0x1F, 0x19, 0x14, 0x00000000, PPC_POWER_BR),
8822GEN_HANDLER(srea, 0x1F, 0x19, 0x1C, 0x00000000, PPC_POWER_BR),
8823GEN_HANDLER(sreq, 0x1F, 0x19, 0x16, 0x00000000, PPC_POWER_BR),
8824GEN_HANDLER(sriq, 0x1F, 0x18, 0x15, 0x00000000, PPC_POWER_BR),
8825GEN_HANDLER(srliq, 0x1F, 0x18, 0x17, 0x00000000, PPC_POWER_BR),
8826GEN_HANDLER(srlq, 0x1F, 0x18, 0x16, 0x00000000, PPC_POWER_BR),
8827GEN_HANDLER(srq, 0x1F, 0x18, 0x14, 0x00000000, PPC_POWER_BR),
8828GEN_HANDLER(dsa, 0x1F, 0x14, 0x13, 0x03FFF801, PPC_602_SPEC),
8829GEN_HANDLER(esa, 0x1F, 0x14, 0x12, 0x03FFF801, PPC_602_SPEC),
8830GEN_HANDLER(mfrom, 0x1F, 0x09, 0x08, 0x03E0F801, PPC_602_SPEC),
8831GEN_HANDLER2(tlbld_6xx, "tlbld", 0x1F, 0x12, 0x1E, 0x03FF0001, PPC_6xx_TLB),
8832GEN_HANDLER2(tlbli_6xx, "tlbli", 0x1F, 0x12, 0x1F, 0x03FF0001, PPC_6xx_TLB),
8833GEN_HANDLER2(tlbld_74xx, "tlbld", 0x1F, 0x12, 0x1E, 0x03FF0001, PPC_74xx_TLB),
8834GEN_HANDLER2(tlbli_74xx, "tlbli", 0x1F, 0x12, 0x1F, 0x03FF0001, PPC_74xx_TLB),
8835GEN_HANDLER(clf, 0x1F, 0x16, 0x03, 0x03E00000, PPC_POWER),
8836GEN_HANDLER(cli, 0x1F, 0x16, 0x0F, 0x03E00000, PPC_POWER),
8837GEN_HANDLER(dclst, 0x1F, 0x16, 0x13, 0x03E00000, PPC_POWER),
8838GEN_HANDLER(mfsri, 0x1F, 0x13, 0x13, 0x00000001, PPC_POWER),
8839GEN_HANDLER(rac, 0x1F, 0x12, 0x19, 0x00000001, PPC_POWER),
8840GEN_HANDLER(rfsvc, 0x13, 0x12, 0x02, 0x03FFF0001, PPC_POWER),
8841GEN_HANDLER(lfq, 0x38, 0xFF, 0xFF, 0x00000003, PPC_POWER2),
8842GEN_HANDLER(lfqu, 0x39, 0xFF, 0xFF, 0x00000003, PPC_POWER2),
8843GEN_HANDLER(lfqux, 0x1F, 0x17, 0x19, 0x00000001, PPC_POWER2),
8844GEN_HANDLER(lfqx, 0x1F, 0x17, 0x18, 0x00000001, PPC_POWER2),
8845GEN_HANDLER(stfq, 0x3C, 0xFF, 0xFF, 0x00000003, PPC_POWER2),
8846GEN_HANDLER(stfqu, 0x3D, 0xFF, 0xFF, 0x00000003, PPC_POWER2),
8847GEN_HANDLER(stfqux, 0x1F, 0x17, 0x1D, 0x00000001, PPC_POWER2),
8848GEN_HANDLER(stfqx, 0x1F, 0x17, 0x1C, 0x00000001, PPC_POWER2),
8849GEN_HANDLER(mfapidi, 0x1F, 0x13, 0x08, 0x0000F801, PPC_MFAPIDI),
8850GEN_HANDLER(tlbiva, 0x1F, 0x12, 0x18, 0x03FFF801, PPC_TLBIVA),
8851GEN_HANDLER(mfdcr, 0x1F, 0x03, 0x0A, 0x00000001, PPC_DCR),
8852GEN_HANDLER(mtdcr, 0x1F, 0x03, 0x0E, 0x00000001, PPC_DCR),
8853GEN_HANDLER(mfdcrx, 0x1F, 0x03, 0x08, 0x00000000, PPC_DCRX),
8854GEN_HANDLER(mtdcrx, 0x1F, 0x03, 0x0C, 0x00000000, PPC_DCRX),
8855GEN_HANDLER(mfdcrux, 0x1F, 0x03, 0x09, 0x00000000, PPC_DCRUX),
8856GEN_HANDLER(mtdcrux, 0x1F, 0x03, 0x0D, 0x00000000, PPC_DCRUX),
8857GEN_HANDLER(dccci, 0x1F, 0x06, 0x0E, 0x03E00001, PPC_4xx_COMMON),
8858GEN_HANDLER(dcread, 0x1F, 0x06, 0x0F, 0x00000001, PPC_4xx_COMMON),
8859GEN_HANDLER2(icbt_40x, "icbt", 0x1F, 0x06, 0x08, 0x03E00001, PPC_40x_ICBT),
8860GEN_HANDLER(iccci, 0x1F, 0x06, 0x1E, 0x00000001, PPC_4xx_COMMON),
8861GEN_HANDLER(icread, 0x1F, 0x06, 0x1F, 0x03E00001, PPC_4xx_COMMON),
8862GEN_HANDLER2(rfci_40x, "rfci", 0x13, 0x13, 0x01, 0x03FF8001, PPC_40x_EXCP),
01662f3e 8863GEN_HANDLER_E(rfci, 0x13, 0x13, 0x01, 0x03FF8001, PPC_BOOKE, PPC2_BOOKE206),
5c55ff99
BS
8864GEN_HANDLER(rfdi, 0x13, 0x07, 0x01, 0x03FF8001, PPC_RFDI),
8865GEN_HANDLER(rfmci, 0x13, 0x06, 0x01, 0x03FF8001, PPC_RFMCI),
8866GEN_HANDLER2(tlbre_40x, "tlbre", 0x1F, 0x12, 0x1D, 0x00000001, PPC_40x_TLB),
8867GEN_HANDLER2(tlbsx_40x, "tlbsx", 0x1F, 0x12, 0x1C, 0x00000000, PPC_40x_TLB),
8868GEN_HANDLER2(tlbwe_40x, "tlbwe", 0x1F, 0x12, 0x1E, 0x00000001, PPC_40x_TLB),
8869GEN_HANDLER2(tlbre_440, "tlbre", 0x1F, 0x12, 0x1D, 0x00000001, PPC_BOOKE),
8870GEN_HANDLER2(tlbsx_440, "tlbsx", 0x1F, 0x12, 0x1C, 0x00000000, PPC_BOOKE),
8871GEN_HANDLER2(tlbwe_440, "tlbwe", 0x1F, 0x12, 0x1E, 0x00000001, PPC_BOOKE),
01662f3e
AG
8872GEN_HANDLER2_E(tlbre_booke206, "tlbre", 0x1F, 0x12, 0x1D, 0x00000001,
8873 PPC_NONE, PPC2_BOOKE206),
8874GEN_HANDLER2_E(tlbsx_booke206, "tlbsx", 0x1F, 0x12, 0x1C, 0x00000000,
8875 PPC_NONE, PPC2_BOOKE206),
8876GEN_HANDLER2_E(tlbwe_booke206, "tlbwe", 0x1F, 0x12, 0x1E, 0x00000001,
8877 PPC_NONE, PPC2_BOOKE206),
8878GEN_HANDLER2_E(tlbivax_booke206, "tlbivax", 0x1F, 0x12, 0x18, 0x00000001,
8879 PPC_NONE, PPC2_BOOKE206),
6d3db821
AG
8880GEN_HANDLER2_E(tlbilx_booke206, "tlbilx", 0x1F, 0x12, 0x00, 0x03800001,
8881 PPC_NONE, PPC2_BOOKE206),
d5d11a39
AG
8882GEN_HANDLER2_E(msgsnd, "msgsnd", 0x1F, 0x0E, 0x06, 0x03ff0001,
8883 PPC_NONE, PPC2_PRCNTL),
9e0b5cb1
AG
8884GEN_HANDLER2_E(msgclr, "msgclr", 0x1F, 0x0E, 0x07, 0x03ff0001,
8885 PPC_NONE, PPC2_PRCNTL),
5c55ff99 8886GEN_HANDLER(wrtee, 0x1F, 0x03, 0x04, 0x000FFC01, PPC_WRTEE),
fbe73008 8887GEN_HANDLER(wrteei, 0x1F, 0x03, 0x05, 0x000E7C01, PPC_WRTEE),
5c55ff99 8888GEN_HANDLER(dlmzb, 0x1F, 0x0E, 0x02, 0x00000000, PPC_440_SPEC),
01662f3e
AG
8889GEN_HANDLER_E(mbar, 0x1F, 0x16, 0x1a, 0x001FF801,
8890 PPC_BOOKE, PPC2_BOOKE206),
dcb2b9e1 8891GEN_HANDLER(msync_4xx, 0x1F, 0x16, 0x12, 0x03FFF801, PPC_BOOKE),
01662f3e
AG
8892GEN_HANDLER2_E(icbt_440, "icbt", 0x1F, 0x16, 0x00, 0x03E00001,
8893 PPC_BOOKE, PPC2_BOOKE206),
5c55ff99
BS
8894GEN_HANDLER(lvsl, 0x1f, 0x06, 0x00, 0x00000001, PPC_ALTIVEC),
8895GEN_HANDLER(lvsr, 0x1f, 0x06, 0x01, 0x00000001, PPC_ALTIVEC),
8896GEN_HANDLER(mfvscr, 0x04, 0x2, 0x18, 0x001ff800, PPC_ALTIVEC),
8897GEN_HANDLER(mtvscr, 0x04, 0x2, 0x19, 0x03ff0000, PPC_ALTIVEC),
8898GEN_HANDLER(vsldoi, 0x04, 0x16, 0xFF, 0x00000400, PPC_ALTIVEC),
8899GEN_HANDLER(vmladduhm, 0x04, 0x11, 0xFF, 0x00000000, PPC_ALTIVEC),
8900GEN_HANDLER2(evsel0, "evsel", 0x04, 0x1c, 0x09, 0x00000000, PPC_SPE),
8901GEN_HANDLER2(evsel1, "evsel", 0x04, 0x1d, 0x09, 0x00000000, PPC_SPE),
8902GEN_HANDLER2(evsel2, "evsel", 0x04, 0x1e, 0x09, 0x00000000, PPC_SPE),
8903GEN_HANDLER2(evsel3, "evsel", 0x04, 0x1f, 0x09, 0x00000000, PPC_SPE),
8904
8905#undef GEN_INT_ARITH_ADD
8906#undef GEN_INT_ARITH_ADD_CONST
8907#define GEN_INT_ARITH_ADD(name, opc3, add_ca, compute_ca, compute_ov) \
8908GEN_HANDLER(name, 0x1F, 0x0A, opc3, 0x00000000, PPC_INTEGER),
8909#define GEN_INT_ARITH_ADD_CONST(name, opc3, const_val, \
8910 add_ca, compute_ca, compute_ov) \
8911GEN_HANDLER(name, 0x1F, 0x0A, opc3, 0x0000F800, PPC_INTEGER),
8912GEN_INT_ARITH_ADD(add, 0x08, 0, 0, 0)
8913GEN_INT_ARITH_ADD(addo, 0x18, 0, 0, 1)
8914GEN_INT_ARITH_ADD(addc, 0x00, 0, 1, 0)
8915GEN_INT_ARITH_ADD(addco, 0x10, 0, 1, 1)
8916GEN_INT_ARITH_ADD(adde, 0x04, 1, 1, 0)
8917GEN_INT_ARITH_ADD(addeo, 0x14, 1, 1, 1)
8918GEN_INT_ARITH_ADD_CONST(addme, 0x07, -1LL, 1, 1, 0)
8919GEN_INT_ARITH_ADD_CONST(addmeo, 0x17, -1LL, 1, 1, 1)
8920GEN_INT_ARITH_ADD_CONST(addze, 0x06, 0, 1, 1, 0)
8921GEN_INT_ARITH_ADD_CONST(addzeo, 0x16, 0, 1, 1, 1)
8922
8923#undef GEN_INT_ARITH_DIVW
8924#define GEN_INT_ARITH_DIVW(name, opc3, sign, compute_ov) \
8925GEN_HANDLER(name, 0x1F, 0x0B, opc3, 0x00000000, PPC_INTEGER)
8926GEN_INT_ARITH_DIVW(divwu, 0x0E, 0, 0),
8927GEN_INT_ARITH_DIVW(divwuo, 0x1E, 0, 1),
8928GEN_INT_ARITH_DIVW(divw, 0x0F, 1, 0),
8929GEN_INT_ARITH_DIVW(divwo, 0x1F, 1, 1),
8930
8931#if defined(TARGET_PPC64)
8932#undef GEN_INT_ARITH_DIVD
8933#define GEN_INT_ARITH_DIVD(name, opc3, sign, compute_ov) \
8934GEN_HANDLER(name, 0x1F, 0x09, opc3, 0x00000000, PPC_64B)
8935GEN_INT_ARITH_DIVD(divdu, 0x0E, 0, 0),
8936GEN_INT_ARITH_DIVD(divduo, 0x1E, 0, 1),
8937GEN_INT_ARITH_DIVD(divd, 0x0F, 1, 0),
8938GEN_INT_ARITH_DIVD(divdo, 0x1F, 1, 1),
8939
8940#undef GEN_INT_ARITH_MUL_HELPER
8941#define GEN_INT_ARITH_MUL_HELPER(name, opc3) \
8942GEN_HANDLER(name, 0x1F, 0x09, opc3, 0x00000000, PPC_64B)
8943GEN_INT_ARITH_MUL_HELPER(mulhdu, 0x00),
8944GEN_INT_ARITH_MUL_HELPER(mulhd, 0x02),
8945GEN_INT_ARITH_MUL_HELPER(mulldo, 0x17),
8946#endif
8947
8948#undef GEN_INT_ARITH_SUBF
8949#undef GEN_INT_ARITH_SUBF_CONST
8950#define GEN_INT_ARITH_SUBF(name, opc3, add_ca, compute_ca, compute_ov) \
8951GEN_HANDLER(name, 0x1F, 0x08, opc3, 0x00000000, PPC_INTEGER),
8952#define GEN_INT_ARITH_SUBF_CONST(name, opc3, const_val, \
8953 add_ca, compute_ca, compute_ov) \
8954GEN_HANDLER(name, 0x1F, 0x08, opc3, 0x0000F800, PPC_INTEGER),
8955GEN_INT_ARITH_SUBF(subf, 0x01, 0, 0, 0)
8956GEN_INT_ARITH_SUBF(subfo, 0x11, 0, 0, 1)
8957GEN_INT_ARITH_SUBF(subfc, 0x00, 0, 1, 0)
8958GEN_INT_ARITH_SUBF(subfco, 0x10, 0, 1, 1)
8959GEN_INT_ARITH_SUBF(subfe, 0x04, 1, 1, 0)
8960GEN_INT_ARITH_SUBF(subfeo, 0x14, 1, 1, 1)
8961GEN_INT_ARITH_SUBF_CONST(subfme, 0x07, -1LL, 1, 1, 0)
8962GEN_INT_ARITH_SUBF_CONST(subfmeo, 0x17, -1LL, 1, 1, 1)
8963GEN_INT_ARITH_SUBF_CONST(subfze, 0x06, 0, 1, 1, 0)
8964GEN_INT_ARITH_SUBF_CONST(subfzeo, 0x16, 0, 1, 1, 1)
8965
8966#undef GEN_LOGICAL1
8967#undef GEN_LOGICAL2
8968#define GEN_LOGICAL2(name, tcg_op, opc, type) \
8969GEN_HANDLER(name, 0x1F, 0x1C, opc, 0x00000000, type)
8970#define GEN_LOGICAL1(name, tcg_op, opc, type) \
8971GEN_HANDLER(name, 0x1F, 0x1A, opc, 0x00000000, type)
8972GEN_LOGICAL2(and, tcg_gen_and_tl, 0x00, PPC_INTEGER),
8973GEN_LOGICAL2(andc, tcg_gen_andc_tl, 0x01, PPC_INTEGER),
8974GEN_LOGICAL2(eqv, tcg_gen_eqv_tl, 0x08, PPC_INTEGER),
8975GEN_LOGICAL1(extsb, tcg_gen_ext8s_tl, 0x1D, PPC_INTEGER),
8976GEN_LOGICAL1(extsh, tcg_gen_ext16s_tl, 0x1C, PPC_INTEGER),
8977GEN_LOGICAL2(nand, tcg_gen_nand_tl, 0x0E, PPC_INTEGER),
8978GEN_LOGICAL2(nor, tcg_gen_nor_tl, 0x03, PPC_INTEGER),
8979GEN_LOGICAL2(orc, tcg_gen_orc_tl, 0x0C, PPC_INTEGER),
8980#if defined(TARGET_PPC64)
8981GEN_LOGICAL1(extsw, tcg_gen_ext32s_tl, 0x1E, PPC_64B),
8982#endif
8983
8984#if defined(TARGET_PPC64)
8985#undef GEN_PPC64_R2
8986#undef GEN_PPC64_R4
8987#define GEN_PPC64_R2(name, opc1, opc2) \
8988GEN_HANDLER2(name##0, stringify(name), opc1, opc2, 0xFF, 0x00000000, PPC_64B),\
8989GEN_HANDLER2(name##1, stringify(name), opc1, opc2 | 0x10, 0xFF, 0x00000000, \
8990 PPC_64B)
8991#define GEN_PPC64_R4(name, opc1, opc2) \
8992GEN_HANDLER2(name##0, stringify(name), opc1, opc2, 0xFF, 0x00000000, PPC_64B),\
8993GEN_HANDLER2(name##1, stringify(name), opc1, opc2 | 0x01, 0xFF, 0x00000000, \
8994 PPC_64B), \
8995GEN_HANDLER2(name##2, stringify(name), opc1, opc2 | 0x10, 0xFF, 0x00000000, \
8996 PPC_64B), \
8997GEN_HANDLER2(name##3, stringify(name), opc1, opc2 | 0x11, 0xFF, 0x00000000, \
8998 PPC_64B)
8999GEN_PPC64_R4(rldicl, 0x1E, 0x00),
9000GEN_PPC64_R4(rldicr, 0x1E, 0x02),
9001GEN_PPC64_R4(rldic, 0x1E, 0x04),
9002GEN_PPC64_R2(rldcl, 0x1E, 0x08),
9003GEN_PPC64_R2(rldcr, 0x1E, 0x09),
9004GEN_PPC64_R4(rldimi, 0x1E, 0x06),
9005#endif
9006
9007#undef _GEN_FLOAT_ACB
9008#undef GEN_FLOAT_ACB
9009#undef _GEN_FLOAT_AB
9010#undef GEN_FLOAT_AB
9011#undef _GEN_FLOAT_AC
9012#undef GEN_FLOAT_AC
9013#undef GEN_FLOAT_B
9014#undef GEN_FLOAT_BS
9015#define _GEN_FLOAT_ACB(name, op, op1, op2, isfloat, set_fprf, type) \
9016GEN_HANDLER(f##name, op1, op2, 0xFF, 0x00000000, type)
9017#define GEN_FLOAT_ACB(name, op2, set_fprf, type) \
9018_GEN_FLOAT_ACB(name, name, 0x3F, op2, 0, set_fprf, type), \
9019_GEN_FLOAT_ACB(name##s, name, 0x3B, op2, 1, set_fprf, type)
9020#define _GEN_FLOAT_AB(name, op, op1, op2, inval, isfloat, set_fprf, type) \
9021GEN_HANDLER(f##name, op1, op2, 0xFF, inval, type)
9022#define GEN_FLOAT_AB(name, op2, inval, set_fprf, type) \
9023_GEN_FLOAT_AB(name, name, 0x3F, op2, inval, 0, set_fprf, type), \
9024_GEN_FLOAT_AB(name##s, name, 0x3B, op2, inval, 1, set_fprf, type)
9025#define _GEN_FLOAT_AC(name, op, op1, op2, inval, isfloat, set_fprf, type) \
9026GEN_HANDLER(f##name, op1, op2, 0xFF, inval, type)
9027#define GEN_FLOAT_AC(name, op2, inval, set_fprf, type) \
9028_GEN_FLOAT_AC(name, name, 0x3F, op2, inval, 0, set_fprf, type), \
9029_GEN_FLOAT_AC(name##s, name, 0x3B, op2, inval, 1, set_fprf, type)
9030#define GEN_FLOAT_B(name, op2, op3, set_fprf, type) \
9031GEN_HANDLER(f##name, 0x3F, op2, op3, 0x001F0000, type)
9032#define GEN_FLOAT_BS(name, op1, op2, set_fprf, type) \
9033GEN_HANDLER(f##name, op1, op2, 0xFF, 0x001F07C0, type)
9034
9035GEN_FLOAT_AB(add, 0x15, 0x000007C0, 1, PPC_FLOAT),
9036GEN_FLOAT_AB(div, 0x12, 0x000007C0, 1, PPC_FLOAT),
9037GEN_FLOAT_AC(mul, 0x19, 0x0000F800, 1, PPC_FLOAT),
9038GEN_FLOAT_BS(re, 0x3F, 0x18, 1, PPC_FLOAT_EXT),
9039GEN_FLOAT_BS(res, 0x3B, 0x18, 1, PPC_FLOAT_FRES),
9040GEN_FLOAT_BS(rsqrte, 0x3F, 0x1A, 1, PPC_FLOAT_FRSQRTE),
9041_GEN_FLOAT_ACB(sel, sel, 0x3F, 0x17, 0, 0, PPC_FLOAT_FSEL),
9042GEN_FLOAT_AB(sub, 0x14, 0x000007C0, 1, PPC_FLOAT),
9043GEN_FLOAT_ACB(madd, 0x1D, 1, PPC_FLOAT),
9044GEN_FLOAT_ACB(msub, 0x1C, 1, PPC_FLOAT),
9045GEN_FLOAT_ACB(nmadd, 0x1F, 1, PPC_FLOAT),
9046GEN_FLOAT_ACB(nmsub, 0x1E, 1, PPC_FLOAT),
9047GEN_FLOAT_B(ctiw, 0x0E, 0x00, 0, PPC_FLOAT),
9048GEN_FLOAT_B(ctiwz, 0x0F, 0x00, 0, PPC_FLOAT),
9049GEN_FLOAT_B(rsp, 0x0C, 0x00, 1, PPC_FLOAT),
9050#if defined(TARGET_PPC64)
9051GEN_FLOAT_B(cfid, 0x0E, 0x1A, 1, PPC_64B),
9052GEN_FLOAT_B(ctid, 0x0E, 0x19, 0, PPC_64B),
9053GEN_FLOAT_B(ctidz, 0x0F, 0x19, 0, PPC_64B),
9054#endif
9055GEN_FLOAT_B(rin, 0x08, 0x0C, 1, PPC_FLOAT_EXT),
9056GEN_FLOAT_B(riz, 0x08, 0x0D, 1, PPC_FLOAT_EXT),
9057GEN_FLOAT_B(rip, 0x08, 0x0E, 1, PPC_FLOAT_EXT),
9058GEN_FLOAT_B(rim, 0x08, 0x0F, 1, PPC_FLOAT_EXT),
5c55ff99
BS
9059
9060#undef GEN_LD
9061#undef GEN_LDU
9062#undef GEN_LDUX
cd6e9320 9063#undef GEN_LDX_E
5c55ff99
BS
9064#undef GEN_LDS
9065#define GEN_LD(name, ldop, opc, type) \
9066GEN_HANDLER(name, opc, 0xFF, 0xFF, 0x00000000, type),
9067#define GEN_LDU(name, ldop, opc, type) \
9068GEN_HANDLER(name##u, opc, 0xFF, 0xFF, 0x00000000, type),
9069#define GEN_LDUX(name, ldop, opc2, opc3, type) \
9070GEN_HANDLER(name##ux, 0x1F, opc2, opc3, 0x00000001, type),
cd6e9320
TH
9071#define GEN_LDX_E(name, ldop, opc2, opc3, type, type2) \
9072GEN_HANDLER_E(name##x, 0x1F, opc2, opc3, 0x00000001, type, type2),
5c55ff99
BS
9073#define GEN_LDS(name, ldop, op, type) \
9074GEN_LD(name, ldop, op | 0x20, type) \
9075GEN_LDU(name, ldop, op | 0x21, type) \
9076GEN_LDUX(name, ldop, 0x17, op | 0x01, type) \
9077GEN_LDX(name, ldop, 0x17, op | 0x00, type)
9078
9079GEN_LDS(lbz, ld8u, 0x02, PPC_INTEGER)
9080GEN_LDS(lha, ld16s, 0x0A, PPC_INTEGER)
9081GEN_LDS(lhz, ld16u, 0x08, PPC_INTEGER)
9082GEN_LDS(lwz, ld32u, 0x00, PPC_INTEGER)
9083#if defined(TARGET_PPC64)
9084GEN_LDUX(lwa, ld32s, 0x15, 0x0B, PPC_64B)
9085GEN_LDX(lwa, ld32s, 0x15, 0x0A, PPC_64B)
9086GEN_LDUX(ld, ld64, 0x15, 0x01, PPC_64B)
9087GEN_LDX(ld, ld64, 0x15, 0x00, PPC_64B)
cd6e9320 9088GEN_LDX_E(ldbr, ld64ur, 0x14, 0x10, PPC_NONE, PPC2_DBRX)
5c55ff99
BS
9089#endif
9090GEN_LDX(lhbr, ld16ur, 0x16, 0x18, PPC_INTEGER)
9091GEN_LDX(lwbr, ld32ur, 0x16, 0x10, PPC_INTEGER)
9092
9093#undef GEN_ST
9094#undef GEN_STU
9095#undef GEN_STUX
cd6e9320 9096#undef GEN_STX_E
5c55ff99
BS
9097#undef GEN_STS
9098#define GEN_ST(name, stop, opc, type) \
9099GEN_HANDLER(name, opc, 0xFF, 0xFF, 0x00000000, type),
9100#define GEN_STU(name, stop, opc, type) \
9101GEN_HANDLER(stop##u, opc, 0xFF, 0xFF, 0x00000000, type),
9102#define GEN_STUX(name, stop, opc2, opc3, type) \
9103GEN_HANDLER(name##ux, 0x1F, opc2, opc3, 0x00000001, type),
cd6e9320
TH
9104#define GEN_STX_E(name, stop, opc2, opc3, type, type2) \
9105GEN_HANDLER_E(name##x, 0x1F, opc2, opc3, 0x00000001, type, type2),
5c55ff99
BS
9106#define GEN_STS(name, stop, op, type) \
9107GEN_ST(name, stop, op | 0x20, type) \
9108GEN_STU(name, stop, op | 0x21, type) \
9109GEN_STUX(name, stop, 0x17, op | 0x01, type) \
9110GEN_STX(name, stop, 0x17, op | 0x00, type)
9111
9112GEN_STS(stb, st8, 0x06, PPC_INTEGER)
9113GEN_STS(sth, st16, 0x0C, PPC_INTEGER)
9114GEN_STS(stw, st32, 0x04, PPC_INTEGER)
9115#if defined(TARGET_PPC64)
9116GEN_STUX(std, st64, 0x15, 0x05, PPC_64B)
9117GEN_STX(std, st64, 0x15, 0x04, PPC_64B)
cd6e9320 9118GEN_STX_E(stdbr, st64r, 0x14, 0x14, PPC_NONE, PPC2_DBRX)
5c55ff99
BS
9119#endif
9120GEN_STX(sthbr, st16r, 0x16, 0x1C, PPC_INTEGER)
9121GEN_STX(stwbr, st32r, 0x16, 0x14, PPC_INTEGER)
9122
9123#undef GEN_LDF
9124#undef GEN_LDUF
9125#undef GEN_LDUXF
9126#undef GEN_LDXF
9127#undef GEN_LDFS
9128#define GEN_LDF(name, ldop, opc, type) \
9129GEN_HANDLER(name, opc, 0xFF, 0xFF, 0x00000000, type),
9130#define GEN_LDUF(name, ldop, opc, type) \
9131GEN_HANDLER(name##u, opc, 0xFF, 0xFF, 0x00000000, type),
9132#define GEN_LDUXF(name, ldop, opc, type) \
9133GEN_HANDLER(name##ux, 0x1F, 0x17, opc, 0x00000001, type),
9134#define GEN_LDXF(name, ldop, opc2, opc3, type) \
9135GEN_HANDLER(name##x, 0x1F, opc2, opc3, 0x00000001, type),
9136#define GEN_LDFS(name, ldop, op, type) \
9137GEN_LDF(name, ldop, op | 0x20, type) \
9138GEN_LDUF(name, ldop, op | 0x21, type) \
9139GEN_LDUXF(name, ldop, op | 0x01, type) \
9140GEN_LDXF(name, ldop, 0x17, op | 0x00, type)
9141
9142GEN_LDFS(lfd, ld64, 0x12, PPC_FLOAT)
9143GEN_LDFS(lfs, ld32fs, 0x10, PPC_FLOAT)
199f830d 9144GEN_HANDLER_E(lfiwax, 0x1f, 0x17, 0x1a, 0x00000001, PPC_NONE, PPC2_ISA205),
05050ee8
AJ
9145GEN_HANDLER_E(lfdp, 0x39, 0xFF, 0xFF, 0x00200003, PPC_NONE, PPC2_ISA205),
9146GEN_HANDLER_E(lfdpx, 0x1F, 0x17, 0x18, 0x00200001, PPC_NONE, PPC2_ISA205),
5c55ff99
BS
9147
9148#undef GEN_STF
9149#undef GEN_STUF
9150#undef GEN_STUXF
9151#undef GEN_STXF
9152#undef GEN_STFS
9153#define GEN_STF(name, stop, opc, type) \
9154GEN_HANDLER(name, opc, 0xFF, 0xFF, 0x00000000, type),
9155#define GEN_STUF(name, stop, opc, type) \
9156GEN_HANDLER(name##u, opc, 0xFF, 0xFF, 0x00000000, type),
9157#define GEN_STUXF(name, stop, opc, type) \
9158GEN_HANDLER(name##ux, 0x1F, 0x17, opc, 0x00000001, type),
9159#define GEN_STXF(name, stop, opc2, opc3, type) \
9160GEN_HANDLER(name##x, 0x1F, opc2, opc3, 0x00000001, type),
9161#define GEN_STFS(name, stop, op, type) \
9162GEN_STF(name, stop, op | 0x20, type) \
9163GEN_STUF(name, stop, op | 0x21, type) \
9164GEN_STUXF(name, stop, op | 0x01, type) \
9165GEN_STXF(name, stop, 0x17, op | 0x00, type)
9166
9167GEN_STFS(stfd, st64, 0x16, PPC_FLOAT)
9168GEN_STFS(stfs, st32fs, 0x14, PPC_FLOAT)
9169GEN_STXF(stfiw, st32fiw, 0x17, 0x1E, PPC_FLOAT_STFIWX)
44bc0c4d
AJ
9170GEN_HANDLER_E(stfdp, 0x3D, 0xFF, 0xFF, 0x00200003, PPC_NONE, PPC2_ISA205),
9171GEN_HANDLER_E(stfdpx, 0x1F, 0x17, 0x1C, 0x00200001, PPC_NONE, PPC2_ISA205),
5c55ff99
BS
9172
9173#undef GEN_CRLOGIC
9174#define GEN_CRLOGIC(name, tcg_op, opc) \
9175GEN_HANDLER(name, 0x13, 0x01, opc, 0x00000001, PPC_INTEGER)
9176GEN_CRLOGIC(crand, tcg_gen_and_i32, 0x08),
9177GEN_CRLOGIC(crandc, tcg_gen_andc_i32, 0x04),
9178GEN_CRLOGIC(creqv, tcg_gen_eqv_i32, 0x09),
9179GEN_CRLOGIC(crnand, tcg_gen_nand_i32, 0x07),
9180GEN_CRLOGIC(crnor, tcg_gen_nor_i32, 0x01),
9181GEN_CRLOGIC(cror, tcg_gen_or_i32, 0x0E),
9182GEN_CRLOGIC(crorc, tcg_gen_orc_i32, 0x0D),
9183GEN_CRLOGIC(crxor, tcg_gen_xor_i32, 0x06),
9184
9185#undef GEN_MAC_HANDLER
9186#define GEN_MAC_HANDLER(name, opc2, opc3) \
9187GEN_HANDLER(name, 0x04, opc2, opc3, 0x00000000, PPC_405_MAC)
9188GEN_MAC_HANDLER(macchw, 0x0C, 0x05),
9189GEN_MAC_HANDLER(macchwo, 0x0C, 0x15),
9190GEN_MAC_HANDLER(macchws, 0x0C, 0x07),
9191GEN_MAC_HANDLER(macchwso, 0x0C, 0x17),
9192GEN_MAC_HANDLER(macchwsu, 0x0C, 0x06),
9193GEN_MAC_HANDLER(macchwsuo, 0x0C, 0x16),
9194GEN_MAC_HANDLER(macchwu, 0x0C, 0x04),
9195GEN_MAC_HANDLER(macchwuo, 0x0C, 0x14),
9196GEN_MAC_HANDLER(machhw, 0x0C, 0x01),
9197GEN_MAC_HANDLER(machhwo, 0x0C, 0x11),
9198GEN_MAC_HANDLER(machhws, 0x0C, 0x03),
9199GEN_MAC_HANDLER(machhwso, 0x0C, 0x13),
9200GEN_MAC_HANDLER(machhwsu, 0x0C, 0x02),
9201GEN_MAC_HANDLER(machhwsuo, 0x0C, 0x12),
9202GEN_MAC_HANDLER(machhwu, 0x0C, 0x00),
9203GEN_MAC_HANDLER(machhwuo, 0x0C, 0x10),
9204GEN_MAC_HANDLER(maclhw, 0x0C, 0x0D),
9205GEN_MAC_HANDLER(maclhwo, 0x0C, 0x1D),
9206GEN_MAC_HANDLER(maclhws, 0x0C, 0x0F),
9207GEN_MAC_HANDLER(maclhwso, 0x0C, 0x1F),
9208GEN_MAC_HANDLER(maclhwu, 0x0C, 0x0C),
9209GEN_MAC_HANDLER(maclhwuo, 0x0C, 0x1C),
9210GEN_MAC_HANDLER(maclhwsu, 0x0C, 0x0E),
9211GEN_MAC_HANDLER(maclhwsuo, 0x0C, 0x1E),
9212GEN_MAC_HANDLER(nmacchw, 0x0E, 0x05),
9213GEN_MAC_HANDLER(nmacchwo, 0x0E, 0x15),
9214GEN_MAC_HANDLER(nmacchws, 0x0E, 0x07),
9215GEN_MAC_HANDLER(nmacchwso, 0x0E, 0x17),
9216GEN_MAC_HANDLER(nmachhw, 0x0E, 0x01),
9217GEN_MAC_HANDLER(nmachhwo, 0x0E, 0x11),
9218GEN_MAC_HANDLER(nmachhws, 0x0E, 0x03),
9219GEN_MAC_HANDLER(nmachhwso, 0x0E, 0x13),
9220GEN_MAC_HANDLER(nmaclhw, 0x0E, 0x0D),
9221GEN_MAC_HANDLER(nmaclhwo, 0x0E, 0x1D),
9222GEN_MAC_HANDLER(nmaclhws, 0x0E, 0x0F),
9223GEN_MAC_HANDLER(nmaclhwso, 0x0E, 0x1F),
9224GEN_MAC_HANDLER(mulchw, 0x08, 0x05),
9225GEN_MAC_HANDLER(mulchwu, 0x08, 0x04),
9226GEN_MAC_HANDLER(mulhhw, 0x08, 0x01),
9227GEN_MAC_HANDLER(mulhhwu, 0x08, 0x00),
9228GEN_MAC_HANDLER(mullhw, 0x08, 0x0D),
9229GEN_MAC_HANDLER(mullhwu, 0x08, 0x0C),
9230
9231#undef GEN_VR_LDX
9232#undef GEN_VR_STX
9233#undef GEN_VR_LVE
9234#undef GEN_VR_STVE
9235#define GEN_VR_LDX(name, opc2, opc3) \
9236GEN_HANDLER(name, 0x1F, opc2, opc3, 0x00000001, PPC_ALTIVEC)
9237#define GEN_VR_STX(name, opc2, opc3) \
9238GEN_HANDLER(st##name, 0x1F, opc2, opc3, 0x00000001, PPC_ALTIVEC)
9239#define GEN_VR_LVE(name, opc2, opc3) \
9240 GEN_HANDLER(lve##name, 0x1F, opc2, opc3, 0x00000001, PPC_ALTIVEC)
9241#define GEN_VR_STVE(name, opc2, opc3) \
9242 GEN_HANDLER(stve##name, 0x1F, opc2, opc3, 0x00000001, PPC_ALTIVEC)
9243GEN_VR_LDX(lvx, 0x07, 0x03),
9244GEN_VR_LDX(lvxl, 0x07, 0x0B),
9245GEN_VR_LVE(bx, 0x07, 0x00),
9246GEN_VR_LVE(hx, 0x07, 0x01),
9247GEN_VR_LVE(wx, 0x07, 0x02),
9248GEN_VR_STX(svx, 0x07, 0x07),
9249GEN_VR_STX(svxl, 0x07, 0x0F),
9250GEN_VR_STVE(bx, 0x07, 0x04),
9251GEN_VR_STVE(hx, 0x07, 0x05),
9252GEN_VR_STVE(wx, 0x07, 0x06),
9253
9254#undef GEN_VX_LOGICAL
9255#define GEN_VX_LOGICAL(name, tcg_op, opc2, opc3) \
9256GEN_HANDLER(name, 0x04, opc2, opc3, 0x00000000, PPC_ALTIVEC)
9257GEN_VX_LOGICAL(vand, tcg_gen_and_i64, 2, 16),
9258GEN_VX_LOGICAL(vandc, tcg_gen_andc_i64, 2, 17),
9259GEN_VX_LOGICAL(vor, tcg_gen_or_i64, 2, 18),
9260GEN_VX_LOGICAL(vxor, tcg_gen_xor_i64, 2, 19),
9261GEN_VX_LOGICAL(vnor, tcg_gen_nor_i64, 2, 20),
9262
9263#undef GEN_VXFORM
9264#define GEN_VXFORM(name, opc2, opc3) \
9265GEN_HANDLER(name, 0x04, opc2, opc3, 0x00000000, PPC_ALTIVEC)
9266GEN_VXFORM(vaddubm, 0, 0),
9267GEN_VXFORM(vadduhm, 0, 1),
9268GEN_VXFORM(vadduwm, 0, 2),
9269GEN_VXFORM(vsububm, 0, 16),
9270GEN_VXFORM(vsubuhm, 0, 17),
9271GEN_VXFORM(vsubuwm, 0, 18),
9272GEN_VXFORM(vmaxub, 1, 0),
9273GEN_VXFORM(vmaxuh, 1, 1),
9274GEN_VXFORM(vmaxuw, 1, 2),
9275GEN_VXFORM(vmaxsb, 1, 4),
9276GEN_VXFORM(vmaxsh, 1, 5),
9277GEN_VXFORM(vmaxsw, 1, 6),
9278GEN_VXFORM(vminub, 1, 8),
9279GEN_VXFORM(vminuh, 1, 9),
9280GEN_VXFORM(vminuw, 1, 10),
9281GEN_VXFORM(vminsb, 1, 12),
9282GEN_VXFORM(vminsh, 1, 13),
9283GEN_VXFORM(vminsw, 1, 14),
9284GEN_VXFORM(vavgub, 1, 16),
9285GEN_VXFORM(vavguh, 1, 17),
9286GEN_VXFORM(vavguw, 1, 18),
9287GEN_VXFORM(vavgsb, 1, 20),
9288GEN_VXFORM(vavgsh, 1, 21),
9289GEN_VXFORM(vavgsw, 1, 22),
9290GEN_VXFORM(vmrghb, 6, 0),
9291GEN_VXFORM(vmrghh, 6, 1),
9292GEN_VXFORM(vmrghw, 6, 2),
9293GEN_VXFORM(vmrglb, 6, 4),
9294GEN_VXFORM(vmrglh, 6, 5),
9295GEN_VXFORM(vmrglw, 6, 6),
9296GEN_VXFORM(vmuloub, 4, 0),
9297GEN_VXFORM(vmulouh, 4, 1),
9298GEN_VXFORM(vmulosb, 4, 4),
9299GEN_VXFORM(vmulosh, 4, 5),
9300GEN_VXFORM(vmuleub, 4, 8),
9301GEN_VXFORM(vmuleuh, 4, 9),
9302GEN_VXFORM(vmulesb, 4, 12),
9303GEN_VXFORM(vmulesh, 4, 13),
9304GEN_VXFORM(vslb, 2, 4),
9305GEN_VXFORM(vslh, 2, 5),
9306GEN_VXFORM(vslw, 2, 6),
9307GEN_VXFORM(vsrb, 2, 8),
9308GEN_VXFORM(vsrh, 2, 9),
9309GEN_VXFORM(vsrw, 2, 10),
9310GEN_VXFORM(vsrab, 2, 12),
9311GEN_VXFORM(vsrah, 2, 13),
9312GEN_VXFORM(vsraw, 2, 14),
9313GEN_VXFORM(vslo, 6, 16),
9314GEN_VXFORM(vsro, 6, 17),
9315GEN_VXFORM(vaddcuw, 0, 6),
9316GEN_VXFORM(vsubcuw, 0, 22),
9317GEN_VXFORM(vaddubs, 0, 8),
9318GEN_VXFORM(vadduhs, 0, 9),
9319GEN_VXFORM(vadduws, 0, 10),
9320GEN_VXFORM(vaddsbs, 0, 12),
9321GEN_VXFORM(vaddshs, 0, 13),
9322GEN_VXFORM(vaddsws, 0, 14),
9323GEN_VXFORM(vsububs, 0, 24),
9324GEN_VXFORM(vsubuhs, 0, 25),
9325GEN_VXFORM(vsubuws, 0, 26),
9326GEN_VXFORM(vsubsbs, 0, 28),
9327GEN_VXFORM(vsubshs, 0, 29),
9328GEN_VXFORM(vsubsws, 0, 30),
9329GEN_VXFORM(vrlb, 2, 0),
9330GEN_VXFORM(vrlh, 2, 1),
9331GEN_VXFORM(vrlw, 2, 2),
9332GEN_VXFORM(vsl, 2, 7),
9333GEN_VXFORM(vsr, 2, 11),
9334GEN_VXFORM(vpkuhum, 7, 0),
9335GEN_VXFORM(vpkuwum, 7, 1),
9336GEN_VXFORM(vpkuhus, 7, 2),
9337GEN_VXFORM(vpkuwus, 7, 3),
9338GEN_VXFORM(vpkshus, 7, 4),
9339GEN_VXFORM(vpkswus, 7, 5),
9340GEN_VXFORM(vpkshss, 7, 6),
9341GEN_VXFORM(vpkswss, 7, 7),
9342GEN_VXFORM(vpkpx, 7, 12),
9343GEN_VXFORM(vsum4ubs, 4, 24),
9344GEN_VXFORM(vsum4sbs, 4, 28),
9345GEN_VXFORM(vsum4shs, 4, 25),
9346GEN_VXFORM(vsum2sws, 4, 26),
9347GEN_VXFORM(vsumsws, 4, 30),
9348GEN_VXFORM(vaddfp, 5, 0),
9349GEN_VXFORM(vsubfp, 5, 1),
9350GEN_VXFORM(vmaxfp, 5, 16),
9351GEN_VXFORM(vminfp, 5, 17),
9352
9353#undef GEN_VXRFORM1
9354#undef GEN_VXRFORM
9355#define GEN_VXRFORM1(opname, name, str, opc2, opc3) \
9356 GEN_HANDLER2(name, str, 0x4, opc2, opc3, 0x00000000, PPC_ALTIVEC),
9357#define GEN_VXRFORM(name, opc2, opc3) \
9358 GEN_VXRFORM1(name, name, #name, opc2, opc3) \
9359 GEN_VXRFORM1(name##_dot, name##_, #name ".", opc2, (opc3 | (0x1 << 4)))
9360GEN_VXRFORM(vcmpequb, 3, 0)
9361GEN_VXRFORM(vcmpequh, 3, 1)
9362GEN_VXRFORM(vcmpequw, 3, 2)
9363GEN_VXRFORM(vcmpgtsb, 3, 12)
9364GEN_VXRFORM(vcmpgtsh, 3, 13)
9365GEN_VXRFORM(vcmpgtsw, 3, 14)
9366GEN_VXRFORM(vcmpgtub, 3, 8)
9367GEN_VXRFORM(vcmpgtuh, 3, 9)
9368GEN_VXRFORM(vcmpgtuw, 3, 10)
9369GEN_VXRFORM(vcmpeqfp, 3, 3)
9370GEN_VXRFORM(vcmpgefp, 3, 7)
9371GEN_VXRFORM(vcmpgtfp, 3, 11)
9372GEN_VXRFORM(vcmpbfp, 3, 15)
9373
9374#undef GEN_VXFORM_SIMM
9375#define GEN_VXFORM_SIMM(name, opc2, opc3) \
9376 GEN_HANDLER(name, 0x04, opc2, opc3, 0x00000000, PPC_ALTIVEC)
9377GEN_VXFORM_SIMM(vspltisb, 6, 12),
9378GEN_VXFORM_SIMM(vspltish, 6, 13),
9379GEN_VXFORM_SIMM(vspltisw, 6, 14),
9380
9381#undef GEN_VXFORM_NOA
9382#define GEN_VXFORM_NOA(name, opc2, opc3) \
9383 GEN_HANDLER(name, 0x04, opc2, opc3, 0x001f0000, PPC_ALTIVEC)
9384GEN_VXFORM_NOA(vupkhsb, 7, 8),
9385GEN_VXFORM_NOA(vupkhsh, 7, 9),
9386GEN_VXFORM_NOA(vupklsb, 7, 10),
9387GEN_VXFORM_NOA(vupklsh, 7, 11),
9388GEN_VXFORM_NOA(vupkhpx, 7, 13),
9389GEN_VXFORM_NOA(vupklpx, 7, 15),
9390GEN_VXFORM_NOA(vrefp, 5, 4),
9391GEN_VXFORM_NOA(vrsqrtefp, 5, 5),
0bffbc6c 9392GEN_VXFORM_NOA(vexptefp, 5, 6),
5c55ff99
BS
9393GEN_VXFORM_NOA(vlogefp, 5, 7),
9394GEN_VXFORM_NOA(vrfim, 5, 8),
9395GEN_VXFORM_NOA(vrfin, 5, 9),
9396GEN_VXFORM_NOA(vrfip, 5, 10),
9397GEN_VXFORM_NOA(vrfiz, 5, 11),
9398
9399#undef GEN_VXFORM_UIMM
9400#define GEN_VXFORM_UIMM(name, opc2, opc3) \
9401 GEN_HANDLER(name, 0x04, opc2, opc3, 0x00000000, PPC_ALTIVEC)
9402GEN_VXFORM_UIMM(vspltb, 6, 8),
9403GEN_VXFORM_UIMM(vsplth, 6, 9),
9404GEN_VXFORM_UIMM(vspltw, 6, 10),
9405GEN_VXFORM_UIMM(vcfux, 5, 12),
9406GEN_VXFORM_UIMM(vcfsx, 5, 13),
9407GEN_VXFORM_UIMM(vctuxs, 5, 14),
9408GEN_VXFORM_UIMM(vctsxs, 5, 15),
9409
9410#undef GEN_VAFORM_PAIRED
9411#define GEN_VAFORM_PAIRED(name0, name1, opc2) \
9412 GEN_HANDLER(name0##_##name1, 0x04, opc2, 0xFF, 0x00000000, PPC_ALTIVEC)
9413GEN_VAFORM_PAIRED(vmhaddshs, vmhraddshs, 16),
9414GEN_VAFORM_PAIRED(vmsumubm, vmsummbm, 18),
9415GEN_VAFORM_PAIRED(vmsumuhm, vmsumuhs, 19),
9416GEN_VAFORM_PAIRED(vmsumshm, vmsumshs, 20),
9417GEN_VAFORM_PAIRED(vsel, vperm, 21),
9418GEN_VAFORM_PAIRED(vmaddfp, vnmsubfp, 23),
9419
9420#undef GEN_SPE
70560da7
FC
9421#define GEN_SPE(name0, name1, opc2, opc3, inval0, inval1, type) \
9422 GEN_OPCODE_DUAL(name0##_##name1, 0x04, opc2, opc3, inval0, inval1, type, PPC_NONE)
9423GEN_SPE(evaddw, speundef, 0x00, 0x08, 0x00000000, 0xFFFFFFFF, PPC_SPE),
9424GEN_SPE(evaddiw, speundef, 0x01, 0x08, 0x00000000, 0xFFFFFFFF, PPC_SPE),
9425GEN_SPE(evsubfw, speundef, 0x02, 0x08, 0x00000000, 0xFFFFFFFF, PPC_SPE),
9426GEN_SPE(evsubifw, speundef, 0x03, 0x08, 0x00000000, 0xFFFFFFFF, PPC_SPE),
9427GEN_SPE(evabs, evneg, 0x04, 0x08, 0x0000F800, 0x0000F800, PPC_SPE),
9428GEN_SPE(evextsb, evextsh, 0x05, 0x08, 0x0000F800, 0x0000F800, PPC_SPE),
9429GEN_SPE(evrndw, evcntlzw, 0x06, 0x08, 0x0000F800, 0x0000F800, PPC_SPE),
9430GEN_SPE(evcntlsw, brinc, 0x07, 0x08, 0x0000F800, 0x00000000, PPC_SPE),
9431GEN_SPE(evmra, speundef, 0x02, 0x13, 0x0000F800, 0xFFFFFFFF, PPC_SPE),
9432GEN_SPE(speundef, evand, 0x08, 0x08, 0xFFFFFFFF, 0x00000000, PPC_SPE),
9433GEN_SPE(evandc, speundef, 0x09, 0x08, 0x00000000, 0xFFFFFFFF, PPC_SPE),
9434GEN_SPE(evxor, evor, 0x0B, 0x08, 0x00000000, 0x00000000, PPC_SPE),
9435GEN_SPE(evnor, eveqv, 0x0C, 0x08, 0x00000000, 0x00000000, PPC_SPE),
9436GEN_SPE(evmwumi, evmwsmi, 0x0C, 0x11, 0x00000000, 0x00000000, PPC_SPE),
9437GEN_SPE(evmwumia, evmwsmia, 0x1C, 0x11, 0x00000000, 0x00000000, PPC_SPE),
9438GEN_SPE(evmwumiaa, evmwsmiaa, 0x0C, 0x15, 0x00000000, 0x00000000, PPC_SPE),
9439GEN_SPE(speundef, evorc, 0x0D, 0x08, 0xFFFFFFFF, 0x00000000, PPC_SPE),
9440GEN_SPE(evnand, speundef, 0x0F, 0x08, 0x00000000, 0xFFFFFFFF, PPC_SPE),
9441GEN_SPE(evsrwu, evsrws, 0x10, 0x08, 0x00000000, 0x00000000, PPC_SPE),
9442GEN_SPE(evsrwiu, evsrwis, 0x11, 0x08, 0x00000000, 0x00000000, PPC_SPE),
9443GEN_SPE(evslw, speundef, 0x12, 0x08, 0x00000000, 0xFFFFFFFF, PPC_SPE),
9444GEN_SPE(evslwi, speundef, 0x13, 0x08, 0x00000000, 0xFFFFFFFF, PPC_SPE),
9445GEN_SPE(evrlw, evsplati, 0x14, 0x08, 0x00000000, 0x0000F800, PPC_SPE),
9446GEN_SPE(evrlwi, evsplatfi, 0x15, 0x08, 0x00000000, 0x0000F800, PPC_SPE),
9447GEN_SPE(evmergehi, evmergelo, 0x16, 0x08, 0x00000000, 0x00000000, PPC_SPE),
9448GEN_SPE(evmergehilo, evmergelohi, 0x17, 0x08, 0x00000000, 0x00000000, PPC_SPE),
9449GEN_SPE(evcmpgtu, evcmpgts, 0x18, 0x08, 0x00600000, 0x00600000, PPC_SPE),
9450GEN_SPE(evcmpltu, evcmplts, 0x19, 0x08, 0x00600000, 0x00600000, PPC_SPE),
9451GEN_SPE(evcmpeq, speundef, 0x1A, 0x08, 0x00600000, 0xFFFFFFFF, PPC_SPE),
9452
9453GEN_SPE(evfsadd, evfssub, 0x00, 0x0A, 0x00000000, 0x00000000, PPC_SPE_SINGLE),
9454GEN_SPE(evfsabs, evfsnabs, 0x02, 0x0A, 0x0000F800, 0x0000F800, PPC_SPE_SINGLE),
9455GEN_SPE(evfsneg, speundef, 0x03, 0x0A, 0x0000F800, 0xFFFFFFFF, PPC_SPE_SINGLE),
9456GEN_SPE(evfsmul, evfsdiv, 0x04, 0x0A, 0x00000000, 0x00000000, PPC_SPE_SINGLE),
9457GEN_SPE(evfscmpgt, evfscmplt, 0x06, 0x0A, 0x00600000, 0x00600000, PPC_SPE_SINGLE),
9458GEN_SPE(evfscmpeq, speundef, 0x07, 0x0A, 0x00600000, 0xFFFFFFFF, PPC_SPE_SINGLE),
9459GEN_SPE(evfscfui, evfscfsi, 0x08, 0x0A, 0x00180000, 0x00180000, PPC_SPE_SINGLE),
9460GEN_SPE(evfscfuf, evfscfsf, 0x09, 0x0A, 0x00180000, 0x00180000, PPC_SPE_SINGLE),
9461GEN_SPE(evfsctui, evfsctsi, 0x0A, 0x0A, 0x00180000, 0x00180000, PPC_SPE_SINGLE),
9462GEN_SPE(evfsctuf, evfsctsf, 0x0B, 0x0A, 0x00180000, 0x00180000, PPC_SPE_SINGLE),
9463GEN_SPE(evfsctuiz, speundef, 0x0C, 0x0A, 0x00180000, 0xFFFFFFFF, PPC_SPE_SINGLE),
9464GEN_SPE(evfsctsiz, speundef, 0x0D, 0x0A, 0x00180000, 0xFFFFFFFF, PPC_SPE_SINGLE),
9465GEN_SPE(evfststgt, evfststlt, 0x0E, 0x0A, 0x00600000, 0x00600000, PPC_SPE_SINGLE),
9466GEN_SPE(evfststeq, speundef, 0x0F, 0x0A, 0x00600000, 0xFFFFFFFF, PPC_SPE_SINGLE),
9467
9468GEN_SPE(efsadd, efssub, 0x00, 0x0B, 0x00000000, 0x00000000, PPC_SPE_SINGLE),
9469GEN_SPE(efsabs, efsnabs, 0x02, 0x0B, 0x0000F800, 0x0000F800, PPC_SPE_SINGLE),
9470GEN_SPE(efsneg, speundef, 0x03, 0x0B, 0x0000F800, 0xFFFFFFFF, PPC_SPE_SINGLE),
9471GEN_SPE(efsmul, efsdiv, 0x04, 0x0B, 0x00000000, 0x00000000, PPC_SPE_SINGLE),
9472GEN_SPE(efscmpgt, efscmplt, 0x06, 0x0B, 0x00600000, 0x00600000, PPC_SPE_SINGLE),
9473GEN_SPE(efscmpeq, efscfd, 0x07, 0x0B, 0x00600000, 0x00180000, PPC_SPE_SINGLE),
9474GEN_SPE(efscfui, efscfsi, 0x08, 0x0B, 0x00180000, 0x00180000, PPC_SPE_SINGLE),
9475GEN_SPE(efscfuf, efscfsf, 0x09, 0x0B, 0x00180000, 0x00180000, PPC_SPE_SINGLE),
9476GEN_SPE(efsctui, efsctsi, 0x0A, 0x0B, 0x00180000, 0x00180000, PPC_SPE_SINGLE),
9477GEN_SPE(efsctuf, efsctsf, 0x0B, 0x0B, 0x00180000, 0x00180000, PPC_SPE_SINGLE),
9478GEN_SPE(efsctuiz, speundef, 0x0C, 0x0B, 0x00180000, 0xFFFFFFFF, PPC_SPE_SINGLE),
9479GEN_SPE(efsctsiz, speundef, 0x0D, 0x0B, 0x00180000, 0xFFFFFFFF, PPC_SPE_SINGLE),
9480GEN_SPE(efststgt, efststlt, 0x0E, 0x0B, 0x00600000, 0x00600000, PPC_SPE_SINGLE),
9481GEN_SPE(efststeq, speundef, 0x0F, 0x0B, 0x00600000, 0xFFFFFFFF, PPC_SPE_SINGLE),
9482
9483GEN_SPE(efdadd, efdsub, 0x10, 0x0B, 0x00000000, 0x00000000, PPC_SPE_DOUBLE),
9484GEN_SPE(efdcfuid, efdcfsid, 0x11, 0x0B, 0x00180000, 0x00180000, PPC_SPE_DOUBLE),
9485GEN_SPE(efdabs, efdnabs, 0x12, 0x0B, 0x0000F800, 0x0000F800, PPC_SPE_DOUBLE),
9486GEN_SPE(efdneg, speundef, 0x13, 0x0B, 0x0000F800, 0xFFFFFFFF, PPC_SPE_DOUBLE),
9487GEN_SPE(efdmul, efddiv, 0x14, 0x0B, 0x00000000, 0x00000000, PPC_SPE_DOUBLE),
9488GEN_SPE(efdctuidz, efdctsidz, 0x15, 0x0B, 0x00180000, 0x00180000, PPC_SPE_DOUBLE),
9489GEN_SPE(efdcmpgt, efdcmplt, 0x16, 0x0B, 0x00600000, 0x00600000, PPC_SPE_DOUBLE),
9490GEN_SPE(efdcmpeq, efdcfs, 0x17, 0x0B, 0x00600000, 0x00180000, PPC_SPE_DOUBLE),
9491GEN_SPE(efdcfui, efdcfsi, 0x18, 0x0B, 0x00180000, 0x00180000, PPC_SPE_DOUBLE),
9492GEN_SPE(efdcfuf, efdcfsf, 0x19, 0x0B, 0x00180000, 0x00180000, PPC_SPE_DOUBLE),
9493GEN_SPE(efdctui, efdctsi, 0x1A, 0x0B, 0x00180000, 0x00180000, PPC_SPE_DOUBLE),
9494GEN_SPE(efdctuf, efdctsf, 0x1B, 0x0B, 0x00180000, 0x00180000, PPC_SPE_DOUBLE),
9495GEN_SPE(efdctuiz, speundef, 0x1C, 0x0B, 0x00180000, 0xFFFFFFFF, PPC_SPE_DOUBLE),
9496GEN_SPE(efdctsiz, speundef, 0x1D, 0x0B, 0x00180000, 0xFFFFFFFF, PPC_SPE_DOUBLE),
9497GEN_SPE(efdtstgt, efdtstlt, 0x1E, 0x0B, 0x00600000, 0x00600000, PPC_SPE_DOUBLE),
9498GEN_SPE(efdtsteq, speundef, 0x1F, 0x0B, 0x00600000, 0xFFFFFFFF, PPC_SPE_DOUBLE),
5c55ff99
BS
9499
9500#undef GEN_SPEOP_LDST
9501#define GEN_SPEOP_LDST(name, opc2, sh) \
9502GEN_HANDLER(name, 0x04, opc2, 0x0C, 0x00000000, PPC_SPE)
9503GEN_SPEOP_LDST(evldd, 0x00, 3),
9504GEN_SPEOP_LDST(evldw, 0x01, 3),
9505GEN_SPEOP_LDST(evldh, 0x02, 3),
9506GEN_SPEOP_LDST(evlhhesplat, 0x04, 1),
9507GEN_SPEOP_LDST(evlhhousplat, 0x06, 1),
9508GEN_SPEOP_LDST(evlhhossplat, 0x07, 1),
9509GEN_SPEOP_LDST(evlwhe, 0x08, 2),
9510GEN_SPEOP_LDST(evlwhou, 0x0A, 2),
9511GEN_SPEOP_LDST(evlwhos, 0x0B, 2),
9512GEN_SPEOP_LDST(evlwwsplat, 0x0C, 2),
9513GEN_SPEOP_LDST(evlwhsplat, 0x0E, 2),
9514
9515GEN_SPEOP_LDST(evstdd, 0x10, 3),
9516GEN_SPEOP_LDST(evstdw, 0x11, 3),
9517GEN_SPEOP_LDST(evstdh, 0x12, 3),
9518GEN_SPEOP_LDST(evstwhe, 0x18, 2),
9519GEN_SPEOP_LDST(evstwho, 0x1A, 2),
9520GEN_SPEOP_LDST(evstwwe, 0x1C, 2),
9521GEN_SPEOP_LDST(evstwwo, 0x1E, 2),
9522};
9523
0411a972 9524#include "helper_regs.h"
a1389542 9525#include "translate_init.c"
79aceca5 9526
9a64fbe4 9527/*****************************************************************************/
3fc6c082 9528/* Misc PowerPC helpers */
878096ee
AF
9529void ppc_cpu_dump_state(CPUState *cs, FILE *f, fprintf_function cpu_fprintf,
9530 int flags)
79aceca5 9531{
3fc6c082
FB
9532#define RGPL 4
9533#define RFPL 4
3fc6c082 9534
878096ee
AF
9535 PowerPCCPU *cpu = POWERPC_CPU(cs);
9536 CPUPPCState *env = &cpu->env;
79aceca5
FB
9537 int i;
9538
878096ee 9539 cpu_synchronize_state(cs);
29979a8d 9540
90e189ec 9541 cpu_fprintf(f, "NIP " TARGET_FMT_lx " LR " TARGET_FMT_lx " CTR "
9a78eead 9542 TARGET_FMT_lx " XER " TARGET_FMT_lx "\n",
da91a00f 9543 env->nip, env->lr, env->ctr, cpu_read_xer(env));
90e189ec
BS
9544 cpu_fprintf(f, "MSR " TARGET_FMT_lx " HID0 " TARGET_FMT_lx " HF "
9545 TARGET_FMT_lx " idx %d\n", env->msr, env->spr[SPR_HID0],
9546 env->hflags, env->mmu_idx);
d9bce9d9 9547#if !defined(NO_TIMER_DUMP)
9a78eead 9548 cpu_fprintf(f, "TB %08" PRIu32 " %08" PRIu64
76a66253 9549#if !defined(CONFIG_USER_ONLY)
9a78eead 9550 " DECR %08" PRIu32
76a66253
JM
9551#endif
9552 "\n",
077fc206 9553 cpu_ppc_load_tbu(env), cpu_ppc_load_tbl(env)
76a66253
JM
9554#if !defined(CONFIG_USER_ONLY)
9555 , cpu_ppc_load_decr(env)
9556#endif
9557 );
077fc206 9558#endif
76a66253 9559 for (i = 0; i < 32; i++) {
3fc6c082
FB
9560 if ((i & (RGPL - 1)) == 0)
9561 cpu_fprintf(f, "GPR%02d", i);
b11ebf64 9562 cpu_fprintf(f, " %016" PRIx64, ppc_dump_gpr(env, i));
3fc6c082 9563 if ((i & (RGPL - 1)) == (RGPL - 1))
7fe48483 9564 cpu_fprintf(f, "\n");
76a66253 9565 }
3fc6c082 9566 cpu_fprintf(f, "CR ");
76a66253 9567 for (i = 0; i < 8; i++)
7fe48483
FB
9568 cpu_fprintf(f, "%01x", env->crf[i]);
9569 cpu_fprintf(f, " [");
76a66253
JM
9570 for (i = 0; i < 8; i++) {
9571 char a = '-';
9572 if (env->crf[i] & 0x08)
9573 a = 'L';
9574 else if (env->crf[i] & 0x04)
9575 a = 'G';
9576 else if (env->crf[i] & 0x02)
9577 a = 'E';
7fe48483 9578 cpu_fprintf(f, " %c%c", a, env->crf[i] & 0x01 ? 'O' : ' ');
76a66253 9579 }
90e189ec
BS
9580 cpu_fprintf(f, " ] RES " TARGET_FMT_lx "\n",
9581 env->reserve_addr);
3fc6c082
FB
9582 for (i = 0; i < 32; i++) {
9583 if ((i & (RFPL - 1)) == 0)
9584 cpu_fprintf(f, "FPR%02d", i);
26a76461 9585 cpu_fprintf(f, " %016" PRIx64, *((uint64_t *)&env->fpr[i]));
3fc6c082 9586 if ((i & (RFPL - 1)) == (RFPL - 1))
7fe48483 9587 cpu_fprintf(f, "\n");
79aceca5 9588 }
30304420 9589 cpu_fprintf(f, "FPSCR " TARGET_FMT_lx "\n", env->fpscr);
f2e63a42 9590#if !defined(CONFIG_USER_ONLY)
90dc8812
SW
9591 cpu_fprintf(f, " SRR0 " TARGET_FMT_lx " SRR1 " TARGET_FMT_lx
9592 " PVR " TARGET_FMT_lx " VRSAVE " TARGET_FMT_lx "\n",
9593 env->spr[SPR_SRR0], env->spr[SPR_SRR1],
9594 env->spr[SPR_PVR], env->spr[SPR_VRSAVE]);
9595
9596 cpu_fprintf(f, "SPRG0 " TARGET_FMT_lx " SPRG1 " TARGET_FMT_lx
9597 " SPRG2 " TARGET_FMT_lx " SPRG3 " TARGET_FMT_lx "\n",
9598 env->spr[SPR_SPRG0], env->spr[SPR_SPRG1],
9599 env->spr[SPR_SPRG2], env->spr[SPR_SPRG3]);
9600
9601 cpu_fprintf(f, "SPRG4 " TARGET_FMT_lx " SPRG5 " TARGET_FMT_lx
9602 " SPRG6 " TARGET_FMT_lx " SPRG7 " TARGET_FMT_lx "\n",
9603 env->spr[SPR_SPRG4], env->spr[SPR_SPRG5],
9604 env->spr[SPR_SPRG6], env->spr[SPR_SPRG7]);
9605
9606 if (env->excp_model == POWERPC_EXCP_BOOKE) {
9607 cpu_fprintf(f, "CSRR0 " TARGET_FMT_lx " CSRR1 " TARGET_FMT_lx
9608 " MCSRR0 " TARGET_FMT_lx " MCSRR1 " TARGET_FMT_lx "\n",
9609 env->spr[SPR_BOOKE_CSRR0], env->spr[SPR_BOOKE_CSRR1],
9610 env->spr[SPR_BOOKE_MCSRR0], env->spr[SPR_BOOKE_MCSRR1]);
9611
9612 cpu_fprintf(f, " TCR " TARGET_FMT_lx " TSR " TARGET_FMT_lx
9613 " ESR " TARGET_FMT_lx " DEAR " TARGET_FMT_lx "\n",
9614 env->spr[SPR_BOOKE_TCR], env->spr[SPR_BOOKE_TSR],
9615 env->spr[SPR_BOOKE_ESR], env->spr[SPR_BOOKE_DEAR]);
9616
9617 cpu_fprintf(f, " PIR " TARGET_FMT_lx " DECAR " TARGET_FMT_lx
9618 " IVPR " TARGET_FMT_lx " EPCR " TARGET_FMT_lx "\n",
9619 env->spr[SPR_BOOKE_PIR], env->spr[SPR_BOOKE_DECAR],
9620 env->spr[SPR_BOOKE_IVPR], env->spr[SPR_BOOKE_EPCR]);
9621
9622 cpu_fprintf(f, " MCSR " TARGET_FMT_lx " SPRG8 " TARGET_FMT_lx
9623 " EPR " TARGET_FMT_lx "\n",
9624 env->spr[SPR_BOOKE_MCSR], env->spr[SPR_BOOKE_SPRG8],
9625 env->spr[SPR_BOOKE_EPR]);
9626
9627 /* FSL-specific */
9628 cpu_fprintf(f, " MCAR " TARGET_FMT_lx " PID1 " TARGET_FMT_lx
9629 " PID2 " TARGET_FMT_lx " SVR " TARGET_FMT_lx "\n",
9630 env->spr[SPR_Exxx_MCAR], env->spr[SPR_BOOKE_PID1],
9631 env->spr[SPR_BOOKE_PID2], env->spr[SPR_E500_SVR]);
9632
9633 /*
9634 * IVORs are left out as they are large and do not change often --
9635 * they can be read with "p $ivor0", "p $ivor1", etc.
9636 */
9637 }
9638
697ab892
DG
9639#if defined(TARGET_PPC64)
9640 if (env->flags & POWERPC_FLAG_CFAR) {
9641 cpu_fprintf(f, " CFAR " TARGET_FMT_lx"\n", env->cfar);
9642 }
9643#endif
9644
90dc8812
SW
9645 switch (env->mmu_model) {
9646 case POWERPC_MMU_32B:
9647 case POWERPC_MMU_601:
9648 case POWERPC_MMU_SOFT_6xx:
9649 case POWERPC_MMU_SOFT_74xx:
9650#if defined(TARGET_PPC64)
90dc8812
SW
9651 case POWERPC_MMU_64B:
9652#endif
9653 cpu_fprintf(f, " SDR1 " TARGET_FMT_lx "\n", env->spr[SPR_SDR1]);
9654 break;
01662f3e 9655 case POWERPC_MMU_BOOKE206:
90dc8812
SW
9656 cpu_fprintf(f, " MAS0 " TARGET_FMT_lx " MAS1 " TARGET_FMT_lx
9657 " MAS2 " TARGET_FMT_lx " MAS3 " TARGET_FMT_lx "\n",
9658 env->spr[SPR_BOOKE_MAS0], env->spr[SPR_BOOKE_MAS1],
9659 env->spr[SPR_BOOKE_MAS2], env->spr[SPR_BOOKE_MAS3]);
9660
9661 cpu_fprintf(f, " MAS4 " TARGET_FMT_lx " MAS6 " TARGET_FMT_lx
9662 " MAS7 " TARGET_FMT_lx " PID " TARGET_FMT_lx "\n",
9663 env->spr[SPR_BOOKE_MAS4], env->spr[SPR_BOOKE_MAS6],
9664 env->spr[SPR_BOOKE_MAS7], env->spr[SPR_BOOKE_PID]);
9665
9666 cpu_fprintf(f, "MMUCFG " TARGET_FMT_lx " TLB0CFG " TARGET_FMT_lx
9667 " TLB1CFG " TARGET_FMT_lx "\n",
9668 env->spr[SPR_MMUCFG], env->spr[SPR_BOOKE_TLB0CFG],
9669 env->spr[SPR_BOOKE_TLB1CFG]);
9670 break;
9671 default:
9672 break;
9673 }
f2e63a42 9674#endif
79aceca5 9675
3fc6c082
FB
9676#undef RGPL
9677#undef RFPL
79aceca5
FB
9678}
9679
878096ee
AF
9680void ppc_cpu_dump_statistics(CPUState *cs, FILE*f,
9681 fprintf_function cpu_fprintf, int flags)
76a66253
JM
9682{
9683#if defined(DO_PPC_STATISTICS)
878096ee 9684 PowerPCCPU *cpu = POWERPC_CPU(cs);
c227f099 9685 opc_handler_t **t1, **t2, **t3, *handler;
76a66253
JM
9686 int op1, op2, op3;
9687
878096ee 9688 t1 = cpu->env.opcodes;
76a66253
JM
9689 for (op1 = 0; op1 < 64; op1++) {
9690 handler = t1[op1];
9691 if (is_indirect_opcode(handler)) {
9692 t2 = ind_table(handler);
9693 for (op2 = 0; op2 < 32; op2++) {
9694 handler = t2[op2];
9695 if (is_indirect_opcode(handler)) {
9696 t3 = ind_table(handler);
9697 for (op3 = 0; op3 < 32; op3++) {
9698 handler = t3[op3];
9699 if (handler->count == 0)
9700 continue;
9701 cpu_fprintf(f, "%02x %02x %02x (%02x %04d) %16s: "
0bfcd599 9702 "%016" PRIx64 " %" PRId64 "\n",
76a66253
JM
9703 op1, op2, op3, op1, (op3 << 5) | op2,
9704 handler->oname,
9705 handler->count, handler->count);
9706 }
9707 } else {
9708 if (handler->count == 0)
9709 continue;
9710 cpu_fprintf(f, "%02x %02x (%02x %04d) %16s: "
0bfcd599 9711 "%016" PRIx64 " %" PRId64 "\n",
76a66253
JM
9712 op1, op2, op1, op2, handler->oname,
9713 handler->count, handler->count);
9714 }
9715 }
9716 } else {
9717 if (handler->count == 0)
9718 continue;
0bfcd599
BS
9719 cpu_fprintf(f, "%02x (%02x ) %16s: %016" PRIx64
9720 " %" PRId64 "\n",
76a66253
JM
9721 op1, op1, handler->oname,
9722 handler->count, handler->count);
9723 }
9724 }
9725#endif
9726}
9727
9a64fbe4 9728/*****************************************************************************/
1328c2bf 9729static inline void gen_intermediate_code_internal(CPUPPCState *env,
636aa200
BS
9730 TranslationBlock *tb,
9731 int search_pc)
79aceca5 9732{
9fddaa0c 9733 DisasContext ctx, *ctxp = &ctx;
c227f099 9734 opc_handler_t **table, *handler;
0fa85d43 9735 target_ulong pc_start;
79aceca5 9736 uint16_t *gen_opc_end;
a1d1bb31 9737 CPUBreakpoint *bp;
79aceca5 9738 int j, lj = -1;
2e70f6ef
PB
9739 int num_insns;
9740 int max_insns;
79aceca5
FB
9741
9742 pc_start = tb->pc;
92414b31 9743 gen_opc_end = tcg_ctx.gen_opc_buf + OPC_MAX_SIZE;
046d6672 9744 ctx.nip = pc_start;
79aceca5 9745 ctx.tb = tb;
e1833e1f 9746 ctx.exception = POWERPC_EXCP_NONE;
3fc6c082 9747 ctx.spr_cb = env->spr_cb;
76db3ba4 9748 ctx.mem_idx = env->mmu_idx;
7d08d856
AJ
9749 ctx.insns_flags = env->insns_flags;
9750 ctx.insns_flags2 = env->insns_flags2;
76db3ba4
AJ
9751 ctx.access_type = -1;
9752 ctx.le_mode = env->hflags & (1 << MSR_LE) ? 1 : 0;
d9bce9d9 9753#if defined(TARGET_PPC64)
e42a61f1 9754 ctx.sf_mode = msr_is_64bit(env, env->msr);
697ab892 9755 ctx.has_cfar = !!(env->flags & POWERPC_FLAG_CFAR);
9a64fbe4 9756#endif
3cc62370 9757 ctx.fpu_enabled = msr_fp;
a9d9eb8f 9758 if ((env->flags & POWERPC_FLAG_SPE) && msr_spe)
d26bfc9a
JM
9759 ctx.spe_enabled = msr_spe;
9760 else
9761 ctx.spe_enabled = 0;
a9d9eb8f
JM
9762 if ((env->flags & POWERPC_FLAG_VRE) && msr_vr)
9763 ctx.altivec_enabled = msr_vr;
9764 else
9765 ctx.altivec_enabled = 0;
d26bfc9a 9766 if ((env->flags & POWERPC_FLAG_SE) && msr_se)
8cbcb4fa 9767 ctx.singlestep_enabled = CPU_SINGLE_STEP;
d26bfc9a 9768 else
8cbcb4fa 9769 ctx.singlestep_enabled = 0;
d26bfc9a 9770 if ((env->flags & POWERPC_FLAG_BE) && msr_be)
8cbcb4fa
AJ
9771 ctx.singlestep_enabled |= CPU_BRANCH_STEP;
9772 if (unlikely(env->singlestep_enabled))
9773 ctx.singlestep_enabled |= GDBSTUB_SINGLE_STEP;
3fc6c082 9774#if defined (DO_SINGLE_STEP) && 0
9a64fbe4
FB
9775 /* Single step trace mode */
9776 msr_se = 1;
9777#endif
2e70f6ef
PB
9778 num_insns = 0;
9779 max_insns = tb->cflags & CF_COUNT_MASK;
9780 if (max_insns == 0)
9781 max_insns = CF_COUNT_MASK;
9782
806f352d 9783 gen_tb_start();
9a64fbe4 9784 /* Set env in case of segfault during code fetch */
efd7f486
EV
9785 while (ctx.exception == POWERPC_EXCP_NONE
9786 && tcg_ctx.gen_opc_ptr < gen_opc_end) {
72cf2d4f
BS
9787 if (unlikely(!QTAILQ_EMPTY(&env->breakpoints))) {
9788 QTAILQ_FOREACH(bp, &env->breakpoints, entry) {
a1d1bb31 9789 if (bp->pc == ctx.nip) {
e06fcd75 9790 gen_debug_exception(ctxp);
ea4e754f
FB
9791 break;
9792 }
9793 }
9794 }
76a66253 9795 if (unlikely(search_pc)) {
92414b31 9796 j = tcg_ctx.gen_opc_ptr - tcg_ctx.gen_opc_buf;
79aceca5
FB
9797 if (lj < j) {
9798 lj++;
9799 while (lj < j)
ab1103de 9800 tcg_ctx.gen_opc_instr_start[lj++] = 0;
79aceca5 9801 }
25983cad 9802 tcg_ctx.gen_opc_pc[lj] = ctx.nip;
ab1103de 9803 tcg_ctx.gen_opc_instr_start[lj] = 1;
c9c99c22 9804 tcg_ctx.gen_opc_icount[lj] = num_insns;
79aceca5 9805 }
d12d51d5 9806 LOG_DISAS("----------------\n");
90e189ec 9807 LOG_DISAS("nip=" TARGET_FMT_lx " super=%d ir=%d\n",
d12d51d5 9808 ctx.nip, ctx.mem_idx, (int)msr_ir);
2e70f6ef
PB
9809 if (num_insns + 1 == max_insns && (tb->cflags & CF_LAST_IO))
9810 gen_io_start();
76db3ba4 9811 if (unlikely(ctx.le_mode)) {
2f5a189c 9812 ctx.opcode = bswap32(cpu_ldl_code(env, ctx.nip));
056401ea 9813 } else {
2f5a189c 9814 ctx.opcode = cpu_ldl_code(env, ctx.nip);
111bfab3 9815 }
d12d51d5 9816 LOG_DISAS("translate opcode %08x (%02x %02x %02x) (%s)\n",
9a64fbe4 9817 ctx.opcode, opc1(ctx.opcode), opc2(ctx.opcode),
476b6d16 9818 opc3(ctx.opcode), ctx.le_mode ? "little" : "big");
fdefe51c 9819 if (unlikely(qemu_loglevel_mask(CPU_LOG_TB_OP | CPU_LOG_TB_OP_OPT))) {
731c54f8 9820 tcg_gen_debug_insn_start(ctx.nip);
fdefe51c 9821 }
046d6672 9822 ctx.nip += 4;
3fc6c082 9823 table = env->opcodes;
2e70f6ef 9824 num_insns++;
79aceca5
FB
9825 handler = table[opc1(ctx.opcode)];
9826 if (is_indirect_opcode(handler)) {
9827 table = ind_table(handler);
9828 handler = table[opc2(ctx.opcode)];
9829 if (is_indirect_opcode(handler)) {
9830 table = ind_table(handler);
9831 handler = table[opc3(ctx.opcode)];
9832 }
9833 }
9834 /* Is opcode *REALLY* valid ? */
76a66253 9835 if (unlikely(handler->handler == &gen_invalid)) {
93fcfe39
AL
9836 if (qemu_log_enabled()) {
9837 qemu_log("invalid/unsupported opcode: "
90e189ec
BS
9838 "%02x - %02x - %02x (%08x) " TARGET_FMT_lx " %d\n",
9839 opc1(ctx.opcode), opc2(ctx.opcode),
9840 opc3(ctx.opcode), ctx.opcode, ctx.nip - 4, (int)msr_ir);
4b3686fa 9841 }
76a66253 9842 } else {
70560da7
FC
9843 uint32_t inval;
9844
9845 if (unlikely(handler->type & (PPC_SPE | PPC_SPE_SINGLE | PPC_SPE_DOUBLE) && Rc(ctx.opcode))) {
9846 inval = handler->inval2;
9847 } else {
9848 inval = handler->inval1;
9849 }
9850
9851 if (unlikely((ctx.opcode & inval) != 0)) {
93fcfe39
AL
9852 if (qemu_log_enabled()) {
9853 qemu_log("invalid bits: %08x for opcode: "
90e189ec 9854 "%02x - %02x - %02x (%08x) " TARGET_FMT_lx "\n",
70560da7 9855 ctx.opcode & inval, opc1(ctx.opcode),
90e189ec
BS
9856 opc2(ctx.opcode), opc3(ctx.opcode),
9857 ctx.opcode, ctx.nip - 4);
76a66253 9858 }
e06fcd75 9859 gen_inval_exception(ctxp, POWERPC_EXCP_INVAL_INVAL);
4b3686fa 9860 break;
79aceca5 9861 }
79aceca5 9862 }
4b3686fa 9863 (*(handler->handler))(&ctx);
76a66253
JM
9864#if defined(DO_PPC_STATISTICS)
9865 handler->count++;
9866#endif
9a64fbe4 9867 /* Check trace mode exceptions */
8cbcb4fa
AJ
9868 if (unlikely(ctx.singlestep_enabled & CPU_SINGLE_STEP &&
9869 (ctx.nip <= 0x100 || ctx.nip > 0xF00) &&
9870 ctx.exception != POWERPC_SYSCALL &&
9871 ctx.exception != POWERPC_EXCP_TRAP &&
9872 ctx.exception != POWERPC_EXCP_BRANCH)) {
e06fcd75 9873 gen_exception(ctxp, POWERPC_EXCP_TRACE);
d26bfc9a 9874 } else if (unlikely(((ctx.nip & (TARGET_PAGE_SIZE - 1)) == 0) ||
2e70f6ef 9875 (env->singlestep_enabled) ||
1b530a6d 9876 singlestep ||
2e70f6ef 9877 num_insns >= max_insns)) {
d26bfc9a
JM
9878 /* if we reach a page boundary or are single stepping, stop
9879 * generation
9880 */
8dd4983c 9881 break;
76a66253 9882 }
3fc6c082 9883 }
2e70f6ef
PB
9884 if (tb->cflags & CF_LAST_IO)
9885 gen_io_end();
e1833e1f 9886 if (ctx.exception == POWERPC_EXCP_NONE) {
c1942362 9887 gen_goto_tb(&ctx, 0, ctx.nip);
e1833e1f 9888 } else if (ctx.exception != POWERPC_EXCP_BRANCH) {
8cbcb4fa 9889 if (unlikely(env->singlestep_enabled)) {
e06fcd75 9890 gen_debug_exception(ctxp);
8cbcb4fa 9891 }
76a66253 9892 /* Generate the return instruction */
57fec1fe 9893 tcg_gen_exit_tb(0);
9a64fbe4 9894 }
806f352d 9895 gen_tb_end(tb, num_insns);
efd7f486 9896 *tcg_ctx.gen_opc_ptr = INDEX_op_end;
76a66253 9897 if (unlikely(search_pc)) {
92414b31 9898 j = tcg_ctx.gen_opc_ptr - tcg_ctx.gen_opc_buf;
9a64fbe4
FB
9899 lj++;
9900 while (lj <= j)
ab1103de 9901 tcg_ctx.gen_opc_instr_start[lj++] = 0;
9a64fbe4 9902 } else {
046d6672 9903 tb->size = ctx.nip - pc_start;
2e70f6ef 9904 tb->icount = num_insns;
9a64fbe4 9905 }
d9bce9d9 9906#if defined(DEBUG_DISAS)
8fec2b8c 9907 if (qemu_loglevel_mask(CPU_LOG_TB_IN_ASM)) {
76a66253 9908 int flags;
237c0af0 9909 flags = env->bfd_mach;
76db3ba4 9910 flags |= ctx.le_mode << 16;
93fcfe39 9911 qemu_log("IN: %s\n", lookup_symbol(pc_start));
f4359b9f 9912 log_target_disas(env, pc_start, ctx.nip - pc_start, flags);
93fcfe39 9913 qemu_log("\n");
9fddaa0c 9914 }
79aceca5 9915#endif
79aceca5
FB
9916}
9917
1328c2bf 9918void gen_intermediate_code (CPUPPCState *env, struct TranslationBlock *tb)
79aceca5 9919{
2cfc5f17 9920 gen_intermediate_code_internal(env, tb, 0);
79aceca5
FB
9921}
9922
1328c2bf 9923void gen_intermediate_code_pc (CPUPPCState *env, struct TranslationBlock *tb)
79aceca5 9924{
2cfc5f17 9925 gen_intermediate_code_internal(env, tb, 1);
79aceca5 9926}
d2856f1a 9927
1328c2bf 9928void restore_state_to_opc(CPUPPCState *env, TranslationBlock *tb, int pc_pos)
d2856f1a 9929{
25983cad 9930 env->nip = tcg_ctx.gen_opc_pc[pc_pos];
d2856f1a 9931}