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