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