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