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