]> git.proxmox.com Git - qemu.git/blame - target-ppc/translate.c
user: compile host-utils.c only once
[qemu.git] / target-ppc / translate.c
CommitLineData
79aceca5 1/*
3fc6c082 2 * PowerPC emulation for qemu: main translation routines.
5fafdf24 3 *
76a66253 4 * Copyright (c) 2003-2007 Jocelyn Mayer
79aceca5
FB
5 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
10 *
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
8167ee88 17 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
79aceca5 18 */
c6a1c22b
FB
19#include <stdarg.h>
20#include <stdlib.h>
21#include <stdio.h>
22#include <string.h>
23#include <inttypes.h>
24
79aceca5 25#include "cpu.h"
c6a1c22b 26#include "exec-all.h"
79aceca5 27#include "disas.h"
57fec1fe 28#include "tcg-op.h"
ca10f867 29#include "qemu-common.h"
0cfe11ea 30#include "host-utils.h"
79aceca5 31
a7812ae4
PB
32#include "helper.h"
33#define GEN_HELPER 1
34#include "helper.h"
35
8cbcb4fa
AJ
36#define CPU_SINGLE_STEP 0x1
37#define CPU_BRANCH_STEP 0x2
38#define GDBSTUB_SINGLE_STEP 0x4
39
a750fc0b 40/* Include definitions for instructions classes and implementations flags */
9fddaa0c 41//#define PPC_DEBUG_DISAS
76a66253 42//#define DO_PPC_STATISTICS
79aceca5 43
d12d51d5 44#ifdef PPC_DEBUG_DISAS
93fcfe39 45# define LOG_DISAS(...) qemu_log_mask(CPU_LOG_TB_IN_ASM, ## __VA_ARGS__)
d12d51d5
AL
46#else
47# define LOG_DISAS(...) do { } while (0)
48#endif
a750fc0b
JM
49/*****************************************************************************/
50/* Code translation helpers */
c53be334 51
f78fb44e 52/* global register indexes */
a7812ae4 53static TCGv_ptr cpu_env;
1d542695 54static char cpu_reg_names[10*3 + 22*4 /* GPR */
f78fb44e 55#if !defined(TARGET_PPC64)
1d542695 56 + 10*4 + 22*5 /* SPE GPRh */
f78fb44e 57#endif
a5e26afa 58 + 10*4 + 22*5 /* FPR */
47e4661c
AJ
59 + 2*(10*6 + 22*7) /* AVRh, AVRl */
60 + 8*5 /* CRF */];
f78fb44e
AJ
61static TCGv cpu_gpr[32];
62#if !defined(TARGET_PPC64)
63static TCGv cpu_gprh[32];
64#endif
a7812ae4
PB
65static TCGv_i64 cpu_fpr[32];
66static TCGv_i64 cpu_avrh[32], cpu_avrl[32];
67static TCGv_i32 cpu_crf[8];
bd568f18 68static TCGv cpu_nip;
6527f6ea 69static TCGv cpu_msr;
cfdcd37a
AJ
70static TCGv cpu_ctr;
71static TCGv cpu_lr;
3d7b417e 72static TCGv cpu_xer;
cf360a32 73static TCGv cpu_reserve;
a7812ae4 74static TCGv_i32 cpu_fpscr;
a7859e89 75static TCGv_i32 cpu_access_type;
f78fb44e 76
2e70f6ef
PB
77#include "gen-icount.h"
78
79void ppc_translate_init(void)
80{
f78fb44e
AJ
81 int i;
82 char* p;
2dc766da 83 size_t cpu_reg_names_size;
b2437bf2 84 static int done_init = 0;
f78fb44e 85
2e70f6ef
PB
86 if (done_init)
87 return;
f78fb44e 88
a7812ae4 89 cpu_env = tcg_global_reg_new_ptr(TCG_AREG0, "env");
a7812ae4 90
f78fb44e 91 p = cpu_reg_names;
2dc766da 92 cpu_reg_names_size = sizeof(cpu_reg_names);
47e4661c
AJ
93
94 for (i = 0; i < 8; i++) {
2dc766da 95 snprintf(p, cpu_reg_names_size, "crf%d", i);
a7812ae4
PB
96 cpu_crf[i] = tcg_global_mem_new_i32(TCG_AREG0,
97 offsetof(CPUState, crf[i]), p);
47e4661c 98 p += 5;
2dc766da 99 cpu_reg_names_size -= 5;
47e4661c
AJ
100 }
101
f78fb44e 102 for (i = 0; i < 32; i++) {
2dc766da 103 snprintf(p, cpu_reg_names_size, "r%d", i);
a7812ae4 104 cpu_gpr[i] = tcg_global_mem_new(TCG_AREG0,
f78fb44e
AJ
105 offsetof(CPUState, gpr[i]), p);
106 p += (i < 10) ? 3 : 4;
2dc766da 107 cpu_reg_names_size -= (i < 10) ? 3 : 4;
f78fb44e 108#if !defined(TARGET_PPC64)
2dc766da 109 snprintf(p, cpu_reg_names_size, "r%dH", i);
a7812ae4
PB
110 cpu_gprh[i] = tcg_global_mem_new_i32(TCG_AREG0,
111 offsetof(CPUState, gprh[i]), p);
f78fb44e 112 p += (i < 10) ? 4 : 5;
2dc766da 113 cpu_reg_names_size -= (i < 10) ? 4 : 5;
f78fb44e 114#endif
1d542695 115
2dc766da 116 snprintf(p, cpu_reg_names_size, "fp%d", i);
a7812ae4
PB
117 cpu_fpr[i] = tcg_global_mem_new_i64(TCG_AREG0,
118 offsetof(CPUState, fpr[i]), p);
ec1ac72d 119 p += (i < 10) ? 4 : 5;
2dc766da 120 cpu_reg_names_size -= (i < 10) ? 4 : 5;
a5e26afa 121
2dc766da 122 snprintf(p, cpu_reg_names_size, "avr%dH", i);
e2542fe2 123#ifdef HOST_WORDS_BIGENDIAN
fe1e5c53
AJ
124 cpu_avrh[i] = tcg_global_mem_new_i64(TCG_AREG0,
125 offsetof(CPUState, avr[i].u64[0]), p);
126#else
a7812ae4 127 cpu_avrh[i] = tcg_global_mem_new_i64(TCG_AREG0,
fe1e5c53
AJ
128 offsetof(CPUState, avr[i].u64[1]), p);
129#endif
1d542695 130 p += (i < 10) ? 6 : 7;
2dc766da 131 cpu_reg_names_size -= (i < 10) ? 6 : 7;
ec1ac72d 132
2dc766da 133 snprintf(p, cpu_reg_names_size, "avr%dL", i);
e2542fe2 134#ifdef HOST_WORDS_BIGENDIAN
fe1e5c53
AJ
135 cpu_avrl[i] = tcg_global_mem_new_i64(TCG_AREG0,
136 offsetof(CPUState, avr[i].u64[1]), p);
137#else
a7812ae4 138 cpu_avrl[i] = tcg_global_mem_new_i64(TCG_AREG0,
fe1e5c53
AJ
139 offsetof(CPUState, avr[i].u64[0]), p);
140#endif
1d542695 141 p += (i < 10) ? 6 : 7;
2dc766da 142 cpu_reg_names_size -= (i < 10) ? 6 : 7;
f78fb44e 143 }
f10dc08e 144
a7812ae4 145 cpu_nip = tcg_global_mem_new(TCG_AREG0,
bd568f18
AJ
146 offsetof(CPUState, nip), "nip");
147
6527f6ea
AJ
148 cpu_msr = tcg_global_mem_new(TCG_AREG0,
149 offsetof(CPUState, msr), "msr");
150
a7812ae4 151 cpu_ctr = tcg_global_mem_new(TCG_AREG0,
cfdcd37a
AJ
152 offsetof(CPUState, ctr), "ctr");
153
a7812ae4 154 cpu_lr = tcg_global_mem_new(TCG_AREG0,
cfdcd37a
AJ
155 offsetof(CPUState, lr), "lr");
156
a7812ae4 157 cpu_xer = tcg_global_mem_new(TCG_AREG0,
3d7b417e
AJ
158 offsetof(CPUState, xer), "xer");
159
cf360a32 160 cpu_reserve = tcg_global_mem_new(TCG_AREG0,
18b21a2f
NF
161 offsetof(CPUState, reserve_addr),
162 "reserve_addr");
cf360a32 163
a7812ae4
PB
164 cpu_fpscr = tcg_global_mem_new_i32(TCG_AREG0,
165 offsetof(CPUState, fpscr), "fpscr");
e1571908 166
a7859e89
AJ
167 cpu_access_type = tcg_global_mem_new_i32(TCG_AREG0,
168 offsetof(CPUState, access_type), "access_type");
169
f10dc08e 170 /* register helpers */
a7812ae4 171#define GEN_HELPER 2
f10dc08e
AJ
172#include "helper.h"
173
2e70f6ef
PB
174 done_init = 1;
175}
176
79aceca5
FB
177/* internal defines */
178typedef struct DisasContext {
179 struct TranslationBlock *tb;
0fa85d43 180 target_ulong nip;
79aceca5 181 uint32_t opcode;
9a64fbe4 182 uint32_t exception;
3cc62370
FB
183 /* Routine used to access memory */
184 int mem_idx;
76db3ba4 185 int access_type;
3cc62370 186 /* Translation flags */
76db3ba4 187 int le_mode;
d9bce9d9
JM
188#if defined(TARGET_PPC64)
189 int sf_mode;
9a64fbe4 190#endif
3cc62370 191 int fpu_enabled;
a9d9eb8f 192 int altivec_enabled;
0487d6a8 193 int spe_enabled;
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 3009 TCGv t0;
18b21a2f 3010 TCGv gpr = cpu_gpr[rD(ctx->opcode)];
76db3ba4
AJ
3011 gen_set_access_type(ctx, ACCESS_RES);
3012 t0 = tcg_temp_local_new();
3013 gen_addr_reg_index(ctx, t0);
cf360a32 3014 gen_check_align(ctx, t0, 0x03);
18b21a2f 3015 gen_qemu_ld32u(ctx, gpr, t0);
cf360a32 3016 tcg_gen_mov_tl(cpu_reserve, t0);
18b21a2f 3017 tcg_gen_st_tl(gpr, cpu_env, offsetof(CPUState, reserve_val));
cf360a32 3018 tcg_temp_free(t0);
79aceca5
FB
3019}
3020
4425265b
NF
3021#if defined(CONFIG_USER_ONLY)
3022static void gen_conditional_store (DisasContext *ctx, TCGv EA,
3023 int reg, int size)
3024{
3025 TCGv t0 = tcg_temp_new();
3026 uint32_t save_exception = ctx->exception;
3027
3028 tcg_gen_st_tl(EA, cpu_env, offsetof(CPUState, reserve_ea));
3029 tcg_gen_movi_tl(t0, (size << 5) | reg);
3030 tcg_gen_st_tl(t0, cpu_env, offsetof(CPUState, reserve_info));
3031 tcg_temp_free(t0);
3032 gen_update_nip(ctx, ctx->nip-4);
3033 ctx->exception = POWERPC_EXCP_BRANCH;
3034 gen_exception(ctx, POWERPC_EXCP_STCX);
3035 ctx->exception = save_exception;
3036}
3037#endif
3038
79aceca5 3039/* stwcx. */
e8eaa2c0 3040static void gen_stwcx_(DisasContext *ctx)
79aceca5 3041{
76db3ba4
AJ
3042 TCGv t0;
3043 gen_set_access_type(ctx, ACCESS_RES);
3044 t0 = tcg_temp_local_new();
3045 gen_addr_reg_index(ctx, t0);
cf360a32 3046 gen_check_align(ctx, t0, 0x03);
4425265b
NF
3047#if defined(CONFIG_USER_ONLY)
3048 gen_conditional_store(ctx, t0, rS(ctx->opcode), 4);
3049#else
3050 {
3051 int l1;
3052
3053 tcg_gen_trunc_tl_i32(cpu_crf[0], cpu_xer);
3054 tcg_gen_shri_i32(cpu_crf[0], cpu_crf[0], XER_SO);
3055 tcg_gen_andi_i32(cpu_crf[0], cpu_crf[0], 1);
3056 l1 = gen_new_label();
3057 tcg_gen_brcond_tl(TCG_COND_NE, t0, cpu_reserve, l1);
3058 tcg_gen_ori_i32(cpu_crf[0], cpu_crf[0], 1 << CRF_EQ);
3059 gen_qemu_st32(ctx, cpu_gpr[rS(ctx->opcode)], t0);
3060 gen_set_label(l1);
3061 tcg_gen_movi_tl(cpu_reserve, -1);
3062 }
3063#endif
cf360a32 3064 tcg_temp_free(t0);
79aceca5
FB
3065}
3066
426613db 3067#if defined(TARGET_PPC64)
426613db 3068/* ldarx */
99e300ef 3069static void gen_ldarx(DisasContext *ctx)
426613db 3070{
76db3ba4 3071 TCGv t0;
18b21a2f 3072 TCGv gpr = cpu_gpr[rD(ctx->opcode)];
76db3ba4
AJ
3073 gen_set_access_type(ctx, ACCESS_RES);
3074 t0 = tcg_temp_local_new();
3075 gen_addr_reg_index(ctx, t0);
cf360a32 3076 gen_check_align(ctx, t0, 0x07);
18b21a2f 3077 gen_qemu_ld64(ctx, gpr, t0);
cf360a32 3078 tcg_gen_mov_tl(cpu_reserve, t0);
18b21a2f 3079 tcg_gen_st_tl(gpr, cpu_env, offsetof(CPUState, reserve_val));
cf360a32 3080 tcg_temp_free(t0);
426613db
JM
3081}
3082
3083/* stdcx. */
e8eaa2c0 3084static void gen_stdcx_(DisasContext *ctx)
426613db 3085{
76db3ba4
AJ
3086 TCGv t0;
3087 gen_set_access_type(ctx, ACCESS_RES);
3088 t0 = tcg_temp_local_new();
3089 gen_addr_reg_index(ctx, t0);
cf360a32 3090 gen_check_align(ctx, t0, 0x07);
4425265b
NF
3091#if defined(CONFIG_USER_ONLY)
3092 gen_conditional_store(ctx, t0, rS(ctx->opcode), 8);
3093#else
3094 {
3095 int l1;
3096 tcg_gen_trunc_tl_i32(cpu_crf[0], cpu_xer);
3097 tcg_gen_shri_i32(cpu_crf[0], cpu_crf[0], XER_SO);
3098 tcg_gen_andi_i32(cpu_crf[0], cpu_crf[0], 1);
3099 l1 = gen_new_label();
3100 tcg_gen_brcond_tl(TCG_COND_NE, t0, cpu_reserve, l1);
3101 tcg_gen_ori_i32(cpu_crf[0], cpu_crf[0], 1 << CRF_EQ);
3102 gen_qemu_st64(ctx, cpu_gpr[rS(ctx->opcode)], t0);
3103 gen_set_label(l1);
3104 tcg_gen_movi_tl(cpu_reserve, -1);
3105 }
3106#endif
cf360a32 3107 tcg_temp_free(t0);
426613db
JM
3108}
3109#endif /* defined(TARGET_PPC64) */
3110
79aceca5 3111/* sync */
99e300ef 3112static void gen_sync(DisasContext *ctx)
79aceca5 3113{
79aceca5
FB
3114}
3115
0db1b20e 3116/* wait */
99e300ef 3117static void gen_wait(DisasContext *ctx)
0db1b20e 3118{
931ff272
AJ
3119 TCGv_i32 t0 = tcg_temp_new_i32();
3120 tcg_gen_st_i32(t0, cpu_env, offsetof(CPUState, halted));
3121 tcg_temp_free_i32(t0);
0db1b20e 3122 /* Stop translation, as the CPU is supposed to sleep from now */
e06fcd75 3123 gen_exception_err(ctx, EXCP_HLT, 1);
0db1b20e
JM
3124}
3125
79aceca5 3126/*** Floating-point load ***/
a0d7d5a7 3127#define GEN_LDF(name, ldop, opc, type) \
99e300ef 3128static void glue(gen_, name)(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 } \
76db3ba4 3135 gen_set_access_type(ctx, ACCESS_FLOAT); \
a0d7d5a7 3136 EA = tcg_temp_new(); \
76db3ba4
AJ
3137 gen_addr_imm_index(ctx, EA, 0); \
3138 gen_qemu_##ldop(ctx, cpu_fpr[rD(ctx->opcode)], EA); \
a0d7d5a7 3139 tcg_temp_free(EA); \
79aceca5
FB
3140}
3141
a0d7d5a7 3142#define GEN_LDUF(name, ldop, opc, type) \
99e300ef 3143static void glue(gen_, name##u)(DisasContext *ctx) \
79aceca5 3144{ \
a0d7d5a7 3145 TCGv EA; \
76a66253 3146 if (unlikely(!ctx->fpu_enabled)) { \
e06fcd75 3147 gen_exception(ctx, POWERPC_EXCP_FPU); \
4ecc3190
FB
3148 return; \
3149 } \
76a66253 3150 if (unlikely(rA(ctx->opcode) == 0)) { \
e06fcd75 3151 gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL); \
9fddaa0c 3152 return; \
9a64fbe4 3153 } \
76db3ba4 3154 gen_set_access_type(ctx, ACCESS_FLOAT); \
a0d7d5a7 3155 EA = tcg_temp_new(); \
76db3ba4
AJ
3156 gen_addr_imm_index(ctx, EA, 0); \
3157 gen_qemu_##ldop(ctx, cpu_fpr[rD(ctx->opcode)], EA); \
a0d7d5a7
AJ
3158 tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], EA); \
3159 tcg_temp_free(EA); \
79aceca5
FB
3160}
3161
a0d7d5a7 3162#define GEN_LDUXF(name, ldop, opc, type) \
99e300ef 3163static void glue(gen_, name##ux)(DisasContext *ctx) \
79aceca5 3164{ \
a0d7d5a7 3165 TCGv EA; \
76a66253 3166 if (unlikely(!ctx->fpu_enabled)) { \
e06fcd75 3167 gen_exception(ctx, POWERPC_EXCP_FPU); \
4ecc3190
FB
3168 return; \
3169 } \
76a66253 3170 if (unlikely(rA(ctx->opcode) == 0)) { \
e06fcd75 3171 gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL); \
9fddaa0c 3172 return; \
9a64fbe4 3173 } \
76db3ba4 3174 gen_set_access_type(ctx, ACCESS_FLOAT); \
a0d7d5a7 3175 EA = tcg_temp_new(); \
76db3ba4
AJ
3176 gen_addr_reg_index(ctx, EA); \
3177 gen_qemu_##ldop(ctx, cpu_fpr[rD(ctx->opcode)], EA); \
a0d7d5a7
AJ
3178 tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], EA); \
3179 tcg_temp_free(EA); \
79aceca5
FB
3180}
3181
a0d7d5a7 3182#define GEN_LDXF(name, ldop, opc2, opc3, type) \
99e300ef 3183static void glue(gen_, name##x)(DisasContext *ctx) \
79aceca5 3184{ \
a0d7d5a7 3185 TCGv EA; \
76a66253 3186 if (unlikely(!ctx->fpu_enabled)) { \
e06fcd75 3187 gen_exception(ctx, POWERPC_EXCP_FPU); \
4ecc3190
FB
3188 return; \
3189 } \
76db3ba4 3190 gen_set_access_type(ctx, ACCESS_FLOAT); \
a0d7d5a7 3191 EA = tcg_temp_new(); \
76db3ba4
AJ
3192 gen_addr_reg_index(ctx, EA); \
3193 gen_qemu_##ldop(ctx, cpu_fpr[rD(ctx->opcode)], EA); \
a0d7d5a7 3194 tcg_temp_free(EA); \
79aceca5
FB
3195}
3196
a0d7d5a7
AJ
3197#define GEN_LDFS(name, ldop, op, type) \
3198GEN_LDF(name, ldop, op | 0x20, type); \
3199GEN_LDUF(name, ldop, op | 0x21, type); \
3200GEN_LDUXF(name, ldop, op | 0x01, type); \
3201GEN_LDXF(name, ldop, 0x17, op | 0x00, type)
3202
76db3ba4 3203static always_inline void gen_qemu_ld32fs(DisasContext *ctx, TCGv_i64 arg1, TCGv arg2)
a0d7d5a7
AJ
3204{
3205 TCGv t0 = tcg_temp_new();
3206 TCGv_i32 t1 = tcg_temp_new_i32();
76db3ba4 3207 gen_qemu_ld32u(ctx, t0, arg2);
a0d7d5a7
AJ
3208 tcg_gen_trunc_tl_i32(t1, t0);
3209 tcg_temp_free(t0);
3210 gen_helper_float32_to_float64(arg1, t1);
3211 tcg_temp_free_i32(t1);
3212}
79aceca5 3213
a0d7d5a7
AJ
3214 /* lfd lfdu lfdux lfdx */
3215GEN_LDFS(lfd, ld64, 0x12, PPC_FLOAT);
3216 /* lfs lfsu lfsux lfsx */
3217GEN_LDFS(lfs, ld32fs, 0x10, PPC_FLOAT);
79aceca5
FB
3218
3219/*** Floating-point store ***/
a0d7d5a7 3220#define GEN_STF(name, stop, opc, type) \
99e300ef 3221static void glue(gen_, name)(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 } \
76db3ba4 3228 gen_set_access_type(ctx, ACCESS_FLOAT); \
a0d7d5a7 3229 EA = tcg_temp_new(); \
76db3ba4
AJ
3230 gen_addr_imm_index(ctx, EA, 0); \
3231 gen_qemu_##stop(ctx, cpu_fpr[rS(ctx->opcode)], EA); \
a0d7d5a7 3232 tcg_temp_free(EA); \
79aceca5
FB
3233}
3234
a0d7d5a7 3235#define GEN_STUF(name, stop, opc, type) \
99e300ef 3236static void glue(gen_, name##u)(DisasContext *ctx) \
79aceca5 3237{ \
a0d7d5a7 3238 TCGv EA; \
76a66253 3239 if (unlikely(!ctx->fpu_enabled)) { \
e06fcd75 3240 gen_exception(ctx, POWERPC_EXCP_FPU); \
4ecc3190
FB
3241 return; \
3242 } \
76a66253 3243 if (unlikely(rA(ctx->opcode) == 0)) { \
e06fcd75 3244 gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL); \
9fddaa0c 3245 return; \
9a64fbe4 3246 } \
76db3ba4 3247 gen_set_access_type(ctx, ACCESS_FLOAT); \
a0d7d5a7 3248 EA = tcg_temp_new(); \
76db3ba4
AJ
3249 gen_addr_imm_index(ctx, EA, 0); \
3250 gen_qemu_##stop(ctx, cpu_fpr[rS(ctx->opcode)], EA); \
a0d7d5a7
AJ
3251 tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], EA); \
3252 tcg_temp_free(EA); \
79aceca5
FB
3253}
3254
a0d7d5a7 3255#define GEN_STUXF(name, stop, opc, type) \
99e300ef 3256static void glue(gen_, name##ux)(DisasContext *ctx) \
79aceca5 3257{ \
a0d7d5a7 3258 TCGv EA; \
76a66253 3259 if (unlikely(!ctx->fpu_enabled)) { \
e06fcd75 3260 gen_exception(ctx, POWERPC_EXCP_FPU); \
4ecc3190
FB
3261 return; \
3262 } \
76a66253 3263 if (unlikely(rA(ctx->opcode) == 0)) { \
e06fcd75 3264 gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL); \
9fddaa0c 3265 return; \
9a64fbe4 3266 } \
76db3ba4 3267 gen_set_access_type(ctx, ACCESS_FLOAT); \
a0d7d5a7 3268 EA = tcg_temp_new(); \
76db3ba4
AJ
3269 gen_addr_reg_index(ctx, EA); \
3270 gen_qemu_##stop(ctx, cpu_fpr[rS(ctx->opcode)], EA); \
a0d7d5a7
AJ
3271 tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], EA); \
3272 tcg_temp_free(EA); \
79aceca5
FB
3273}
3274
a0d7d5a7 3275#define GEN_STXF(name, stop, opc2, opc3, type) \
99e300ef 3276static void glue(gen_, name##x)(DisasContext *ctx) \
79aceca5 3277{ \
a0d7d5a7 3278 TCGv EA; \
76a66253 3279 if (unlikely(!ctx->fpu_enabled)) { \
e06fcd75 3280 gen_exception(ctx, POWERPC_EXCP_FPU); \
4ecc3190
FB
3281 return; \
3282 } \
76db3ba4 3283 gen_set_access_type(ctx, ACCESS_FLOAT); \
a0d7d5a7 3284 EA = tcg_temp_new(); \
76db3ba4
AJ
3285 gen_addr_reg_index(ctx, EA); \
3286 gen_qemu_##stop(ctx, cpu_fpr[rS(ctx->opcode)], EA); \
a0d7d5a7 3287 tcg_temp_free(EA); \
79aceca5
FB
3288}
3289
a0d7d5a7
AJ
3290#define GEN_STFS(name, stop, op, type) \
3291GEN_STF(name, stop, op | 0x20, type); \
3292GEN_STUF(name, stop, op | 0x21, type); \
3293GEN_STUXF(name, stop, op | 0x01, type); \
3294GEN_STXF(name, stop, 0x17, op | 0x00, type)
3295
76db3ba4 3296static always_inline void gen_qemu_st32fs(DisasContext *ctx, TCGv_i64 arg1, TCGv arg2)
a0d7d5a7
AJ
3297{
3298 TCGv_i32 t0 = tcg_temp_new_i32();
3299 TCGv t1 = tcg_temp_new();
3300 gen_helper_float64_to_float32(t0, arg1);
3301 tcg_gen_extu_i32_tl(t1, t0);
3302 tcg_temp_free_i32(t0);
76db3ba4 3303 gen_qemu_st32(ctx, t1, arg2);
a0d7d5a7
AJ
3304 tcg_temp_free(t1);
3305}
79aceca5
FB
3306
3307/* stfd stfdu stfdux stfdx */
a0d7d5a7 3308GEN_STFS(stfd, st64, 0x16, PPC_FLOAT);
79aceca5 3309/* stfs stfsu stfsux stfsx */
a0d7d5a7 3310GEN_STFS(stfs, st32fs, 0x14, PPC_FLOAT);
79aceca5
FB
3311
3312/* Optional: */
76db3ba4 3313static always_inline void gen_qemu_st32fiw(DisasContext *ctx, TCGv_i64 arg1, TCGv arg2)
a0d7d5a7
AJ
3314{
3315 TCGv t0 = tcg_temp_new();
3316 tcg_gen_trunc_i64_tl(t0, arg1),
76db3ba4 3317 gen_qemu_st32(ctx, t0, arg2);
a0d7d5a7
AJ
3318 tcg_temp_free(t0);
3319}
79aceca5 3320/* stfiwx */
a0d7d5a7 3321GEN_STXF(stfiw, st32fiw, 0x17, 0x1E, PPC_FLOAT_STFIWX);
79aceca5
FB
3322
3323/*** Branch ***/
b068d6a7
JM
3324static always_inline void gen_goto_tb (DisasContext *ctx, int n,
3325 target_ulong dest)
c1942362
FB
3326{
3327 TranslationBlock *tb;
3328 tb = ctx->tb;
a2ffb812
AJ
3329#if defined(TARGET_PPC64)
3330 if (!ctx->sf_mode)
3331 dest = (uint32_t) dest;
3332#endif
57fec1fe 3333 if ((tb->pc & TARGET_PAGE_MASK) == (dest & TARGET_PAGE_MASK) &&
8cbcb4fa 3334 likely(!ctx->singlestep_enabled)) {
57fec1fe 3335 tcg_gen_goto_tb(n);
a2ffb812 3336 tcg_gen_movi_tl(cpu_nip, dest & ~3);
57fec1fe 3337 tcg_gen_exit_tb((long)tb + n);
c1942362 3338 } else {
a2ffb812 3339 tcg_gen_movi_tl(cpu_nip, dest & ~3);
8cbcb4fa
AJ
3340 if (unlikely(ctx->singlestep_enabled)) {
3341 if ((ctx->singlestep_enabled &
bdc4e053 3342 (CPU_BRANCH_STEP | CPU_SINGLE_STEP)) &&
8cbcb4fa
AJ
3343 ctx->exception == POWERPC_EXCP_BRANCH) {
3344 target_ulong tmp = ctx->nip;
3345 ctx->nip = dest;
e06fcd75 3346 gen_exception(ctx, POWERPC_EXCP_TRACE);
8cbcb4fa
AJ
3347 ctx->nip = tmp;
3348 }
3349 if (ctx->singlestep_enabled & GDBSTUB_SINGLE_STEP) {
e06fcd75 3350 gen_debug_exception(ctx);
8cbcb4fa
AJ
3351 }
3352 }
57fec1fe 3353 tcg_gen_exit_tb(0);
c1942362 3354 }
c53be334
FB
3355}
3356
b068d6a7 3357static always_inline void gen_setlr (DisasContext *ctx, target_ulong nip)
e1833e1f
JM
3358{
3359#if defined(TARGET_PPC64)
a2ffb812
AJ
3360 if (ctx->sf_mode == 0)
3361 tcg_gen_movi_tl(cpu_lr, (uint32_t)nip);
e1833e1f
JM
3362 else
3363#endif
a2ffb812 3364 tcg_gen_movi_tl(cpu_lr, nip);
e1833e1f
JM
3365}
3366
79aceca5 3367/* b ba bl bla */
99e300ef 3368static void gen_b(DisasContext *ctx)
79aceca5 3369{
76a66253 3370 target_ulong li, target;
38a64f9d 3371
8cbcb4fa 3372 ctx->exception = POWERPC_EXCP_BRANCH;
38a64f9d 3373 /* sign extend LI */
76a66253 3374#if defined(TARGET_PPC64)
d9bce9d9
JM
3375 if (ctx->sf_mode)
3376 li = ((int64_t)LI(ctx->opcode) << 38) >> 38;
3377 else
76a66253 3378#endif
d9bce9d9 3379 li = ((int32_t)LI(ctx->opcode) << 6) >> 6;
76a66253 3380 if (likely(AA(ctx->opcode) == 0))
046d6672 3381 target = ctx->nip + li - 4;
79aceca5 3382 else
9a64fbe4 3383 target = li;
e1833e1f
JM
3384 if (LK(ctx->opcode))
3385 gen_setlr(ctx, ctx->nip);
c1942362 3386 gen_goto_tb(ctx, 0, target);
79aceca5
FB
3387}
3388
e98a6e40
FB
3389#define BCOND_IM 0
3390#define BCOND_LR 1
3391#define BCOND_CTR 2
3392
b068d6a7 3393static always_inline void gen_bcond (DisasContext *ctx, int type)
d9bce9d9 3394{
d9bce9d9 3395 uint32_t bo = BO(ctx->opcode);
a2ffb812
AJ
3396 int l1 = gen_new_label();
3397 TCGv target;
e98a6e40 3398
8cbcb4fa 3399 ctx->exception = POWERPC_EXCP_BRANCH;
a2ffb812 3400 if (type == BCOND_LR || type == BCOND_CTR) {
a7812ae4 3401 target = tcg_temp_local_new();
a2ffb812
AJ
3402 if (type == BCOND_CTR)
3403 tcg_gen_mov_tl(target, cpu_ctr);
3404 else
3405 tcg_gen_mov_tl(target, cpu_lr);
d2e9fd8f 3406 } else {
3407 TCGV_UNUSED(target);
e98a6e40 3408 }
e1833e1f
JM
3409 if (LK(ctx->opcode))
3410 gen_setlr(ctx, ctx->nip);
a2ffb812
AJ
3411 l1 = gen_new_label();
3412 if ((bo & 0x4) == 0) {
3413 /* Decrement and test CTR */
a7812ae4 3414 TCGv temp = tcg_temp_new();
a2ffb812 3415 if (unlikely(type == BCOND_CTR)) {
e06fcd75 3416 gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL);
a2ffb812
AJ
3417 return;
3418 }
3419 tcg_gen_subi_tl(cpu_ctr, cpu_ctr, 1);
d9bce9d9 3420#if defined(TARGET_PPC64)
a2ffb812
AJ
3421 if (!ctx->sf_mode)
3422 tcg_gen_ext32u_tl(temp, cpu_ctr);
3423 else
d9bce9d9 3424#endif
a2ffb812
AJ
3425 tcg_gen_mov_tl(temp, cpu_ctr);
3426 if (bo & 0x2) {
3427 tcg_gen_brcondi_tl(TCG_COND_NE, temp, 0, l1);
3428 } else {
3429 tcg_gen_brcondi_tl(TCG_COND_EQ, temp, 0, l1);
e98a6e40 3430 }
a7812ae4 3431 tcg_temp_free(temp);
a2ffb812
AJ
3432 }
3433 if ((bo & 0x10) == 0) {
3434 /* Test CR */
3435 uint32_t bi = BI(ctx->opcode);
3436 uint32_t mask = 1 << (3 - (bi & 0x03));
a7812ae4 3437 TCGv_i32 temp = tcg_temp_new_i32();
a2ffb812 3438
d9bce9d9 3439 if (bo & 0x8) {
a2ffb812
AJ
3440 tcg_gen_andi_i32(temp, cpu_crf[bi >> 2], mask);
3441 tcg_gen_brcondi_i32(TCG_COND_EQ, temp, 0, l1);
d9bce9d9 3442 } else {
a2ffb812
AJ
3443 tcg_gen_andi_i32(temp, cpu_crf[bi >> 2], mask);
3444 tcg_gen_brcondi_i32(TCG_COND_NE, temp, 0, l1);
d9bce9d9 3445 }
a7812ae4 3446 tcg_temp_free_i32(temp);
d9bce9d9 3447 }
e98a6e40 3448 if (type == BCOND_IM) {
a2ffb812
AJ
3449 target_ulong li = (target_long)((int16_t)(BD(ctx->opcode)));
3450 if (likely(AA(ctx->opcode) == 0)) {
3451 gen_goto_tb(ctx, 0, ctx->nip + li - 4);
3452 } else {
3453 gen_goto_tb(ctx, 0, li);
3454 }
c53be334 3455 gen_set_label(l1);
c1942362 3456 gen_goto_tb(ctx, 1, ctx->nip);
e98a6e40 3457 } else {
d9bce9d9 3458#if defined(TARGET_PPC64)
a2ffb812
AJ
3459 if (!(ctx->sf_mode))
3460 tcg_gen_andi_tl(cpu_nip, target, (uint32_t)~3);
3461 else
3462#endif
3463 tcg_gen_andi_tl(cpu_nip, target, ~3);
3464 tcg_gen_exit_tb(0);
3465 gen_set_label(l1);
3466#if defined(TARGET_PPC64)
3467 if (!(ctx->sf_mode))
3468 tcg_gen_movi_tl(cpu_nip, (uint32_t)ctx->nip);
d9bce9d9
JM
3469 else
3470#endif
a2ffb812 3471 tcg_gen_movi_tl(cpu_nip, ctx->nip);
57fec1fe 3472 tcg_gen_exit_tb(0);
08e46e54 3473 }
e98a6e40
FB
3474}
3475
99e300ef 3476static void gen_bc(DisasContext *ctx)
3b46e624 3477{
e98a6e40
FB
3478 gen_bcond(ctx, BCOND_IM);
3479}
3480
99e300ef 3481static void gen_bcctr(DisasContext *ctx)
3b46e624 3482{
e98a6e40
FB
3483 gen_bcond(ctx, BCOND_CTR);
3484}
3485
99e300ef 3486static void gen_bclr(DisasContext *ctx)
3b46e624 3487{
e98a6e40
FB
3488 gen_bcond(ctx, BCOND_LR);
3489}
79aceca5
FB
3490
3491/*** Condition register logical ***/
e1571908 3492#define GEN_CRLOGIC(name, tcg_op, opc) \
99e300ef 3493static void glue(gen_, name)(DisasContext *ctx) \
79aceca5 3494{ \
fc0d441e
JM
3495 uint8_t bitmask; \
3496 int sh; \
a7812ae4 3497 TCGv_i32 t0, t1; \
fc0d441e 3498 sh = (crbD(ctx->opcode) & 0x03) - (crbA(ctx->opcode) & 0x03); \
a7812ae4 3499 t0 = tcg_temp_new_i32(); \
fc0d441e 3500 if (sh > 0) \
fea0c503 3501 tcg_gen_shri_i32(t0, cpu_crf[crbA(ctx->opcode) >> 2], sh); \
fc0d441e 3502 else if (sh < 0) \
fea0c503 3503 tcg_gen_shli_i32(t0, cpu_crf[crbA(ctx->opcode) >> 2], -sh); \
e1571908 3504 else \
fea0c503 3505 tcg_gen_mov_i32(t0, cpu_crf[crbA(ctx->opcode) >> 2]); \
a7812ae4 3506 t1 = tcg_temp_new_i32(); \
fc0d441e
JM
3507 sh = (crbD(ctx->opcode) & 0x03) - (crbB(ctx->opcode) & 0x03); \
3508 if (sh > 0) \
fea0c503 3509 tcg_gen_shri_i32(t1, cpu_crf[crbB(ctx->opcode) >> 2], sh); \
fc0d441e 3510 else if (sh < 0) \
fea0c503 3511 tcg_gen_shli_i32(t1, cpu_crf[crbB(ctx->opcode) >> 2], -sh); \
e1571908 3512 else \
fea0c503
AJ
3513 tcg_gen_mov_i32(t1, cpu_crf[crbB(ctx->opcode) >> 2]); \
3514 tcg_op(t0, t0, t1); \
fc0d441e 3515 bitmask = 1 << (3 - (crbD(ctx->opcode) & 0x03)); \
fea0c503
AJ
3516 tcg_gen_andi_i32(t0, t0, bitmask); \
3517 tcg_gen_andi_i32(t1, cpu_crf[crbD(ctx->opcode) >> 2], ~bitmask); \
3518 tcg_gen_or_i32(cpu_crf[crbD(ctx->opcode) >> 2], t0, t1); \
a7812ae4
PB
3519 tcg_temp_free_i32(t0); \
3520 tcg_temp_free_i32(t1); \
79aceca5
FB
3521}
3522
3523/* crand */
e1571908 3524GEN_CRLOGIC(crand, tcg_gen_and_i32, 0x08);
79aceca5 3525/* crandc */
e1571908 3526GEN_CRLOGIC(crandc, tcg_gen_andc_i32, 0x04);
79aceca5 3527/* creqv */
e1571908 3528GEN_CRLOGIC(creqv, tcg_gen_eqv_i32, 0x09);
79aceca5 3529/* crnand */
e1571908 3530GEN_CRLOGIC(crnand, tcg_gen_nand_i32, 0x07);
79aceca5 3531/* crnor */
e1571908 3532GEN_CRLOGIC(crnor, tcg_gen_nor_i32, 0x01);
79aceca5 3533/* cror */
e1571908 3534GEN_CRLOGIC(cror, tcg_gen_or_i32, 0x0E);
79aceca5 3535/* crorc */
e1571908 3536GEN_CRLOGIC(crorc, tcg_gen_orc_i32, 0x0D);
79aceca5 3537/* crxor */
e1571908 3538GEN_CRLOGIC(crxor, tcg_gen_xor_i32, 0x06);
99e300ef 3539
54623277 3540/* mcrf */
99e300ef 3541static void gen_mcrf(DisasContext *ctx)
79aceca5 3542{
47e4661c 3543 tcg_gen_mov_i32(cpu_crf[crfD(ctx->opcode)], cpu_crf[crfS(ctx->opcode)]);
79aceca5
FB
3544}
3545
3546/*** System linkage ***/
99e300ef 3547
54623277 3548/* rfi (mem_idx only) */
99e300ef 3549static void gen_rfi(DisasContext *ctx)
79aceca5 3550{
9a64fbe4 3551#if defined(CONFIG_USER_ONLY)
e06fcd75 3552 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
9a64fbe4
FB
3553#else
3554 /* Restore CPU state */
76db3ba4 3555 if (unlikely(!ctx->mem_idx)) {
e06fcd75 3556 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
9fddaa0c 3557 return;
9a64fbe4 3558 }
d72a19f7 3559 gen_helper_rfi();
e06fcd75 3560 gen_sync_exception(ctx);
9a64fbe4 3561#endif
79aceca5
FB
3562}
3563
426613db 3564#if defined(TARGET_PPC64)
99e300ef 3565static void gen_rfid(DisasContext *ctx)
426613db
JM
3566{
3567#if defined(CONFIG_USER_ONLY)
e06fcd75 3568 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
426613db
JM
3569#else
3570 /* Restore CPU state */
76db3ba4 3571 if (unlikely(!ctx->mem_idx)) {
e06fcd75 3572 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
426613db
JM
3573 return;
3574 }
d72a19f7 3575 gen_helper_rfid();
e06fcd75 3576 gen_sync_exception(ctx);
426613db
JM
3577#endif
3578}
426613db 3579
99e300ef 3580static void gen_hrfid(DisasContext *ctx)
be147d08
JM
3581{
3582#if defined(CONFIG_USER_ONLY)
e06fcd75 3583 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
be147d08
JM
3584#else
3585 /* Restore CPU state */
76db3ba4 3586 if (unlikely(ctx->mem_idx <= 1)) {
e06fcd75 3587 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
be147d08
JM
3588 return;
3589 }
d72a19f7 3590 gen_helper_hrfid();
e06fcd75 3591 gen_sync_exception(ctx);
be147d08
JM
3592#endif
3593}
3594#endif
3595
79aceca5 3596/* sc */
417bf010
JM
3597#if defined(CONFIG_USER_ONLY)
3598#define POWERPC_SYSCALL POWERPC_EXCP_SYSCALL_USER
3599#else
3600#define POWERPC_SYSCALL POWERPC_EXCP_SYSCALL
3601#endif
99e300ef 3602static void gen_sc(DisasContext *ctx)
79aceca5 3603{
e1833e1f
JM
3604 uint32_t lev;
3605
3606 lev = (ctx->opcode >> 5) & 0x7F;
e06fcd75 3607 gen_exception_err(ctx, POWERPC_SYSCALL, lev);
79aceca5
FB
3608}
3609
3610/*** Trap ***/
99e300ef 3611
54623277 3612/* tw */
99e300ef 3613static void gen_tw(DisasContext *ctx)
79aceca5 3614{
cab3bee2 3615 TCGv_i32 t0 = tcg_const_i32(TO(ctx->opcode));
a0ae05aa 3616 /* Update the nip since this might generate a trap exception */
d9bce9d9 3617 gen_update_nip(ctx, ctx->nip);
cab3bee2
AJ
3618 gen_helper_tw(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rB(ctx->opcode)], t0);
3619 tcg_temp_free_i32(t0);
79aceca5
FB
3620}
3621
3622/* twi */
99e300ef 3623static void gen_twi(DisasContext *ctx)
79aceca5 3624{
cab3bee2
AJ
3625 TCGv t0 = tcg_const_tl(SIMM(ctx->opcode));
3626 TCGv_i32 t1 = tcg_const_i32(TO(ctx->opcode));
d9bce9d9
JM
3627 /* Update the nip since this might generate a trap exception */
3628 gen_update_nip(ctx, ctx->nip);
cab3bee2
AJ
3629 gen_helper_tw(cpu_gpr[rA(ctx->opcode)], t0, t1);
3630 tcg_temp_free(t0);
3631 tcg_temp_free_i32(t1);
79aceca5
FB
3632}
3633
d9bce9d9
JM
3634#if defined(TARGET_PPC64)
3635/* td */
99e300ef 3636static void gen_td(DisasContext *ctx)
d9bce9d9 3637{
cab3bee2 3638 TCGv_i32 t0 = tcg_const_i32(TO(ctx->opcode));
d9bce9d9
JM
3639 /* Update the nip since this might generate a trap exception */
3640 gen_update_nip(ctx, ctx->nip);
cab3bee2
AJ
3641 gen_helper_td(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rB(ctx->opcode)], t0);
3642 tcg_temp_free_i32(t0);
d9bce9d9
JM
3643}
3644
3645/* tdi */
99e300ef 3646static void gen_tdi(DisasContext *ctx)
d9bce9d9 3647{
cab3bee2
AJ
3648 TCGv t0 = tcg_const_tl(SIMM(ctx->opcode));
3649 TCGv_i32 t1 = tcg_const_i32(TO(ctx->opcode));
d9bce9d9
JM
3650 /* Update the nip since this might generate a trap exception */
3651 gen_update_nip(ctx, ctx->nip);
cab3bee2
AJ
3652 gen_helper_td(cpu_gpr[rA(ctx->opcode)], t0, t1);
3653 tcg_temp_free(t0);
3654 tcg_temp_free_i32(t1);
d9bce9d9
JM
3655}
3656#endif
3657
79aceca5 3658/*** Processor control ***/
99e300ef 3659
54623277 3660/* mcrxr */
99e300ef 3661static void gen_mcrxr(DisasContext *ctx)
79aceca5 3662{
3d7b417e
AJ
3663 tcg_gen_trunc_tl_i32(cpu_crf[crfD(ctx->opcode)], cpu_xer);
3664 tcg_gen_shri_i32(cpu_crf[crfD(ctx->opcode)], cpu_crf[crfD(ctx->opcode)], XER_CA);
269f3e95 3665 tcg_gen_andi_tl(cpu_xer, cpu_xer, ~(1 << XER_SO | 1 << XER_OV | 1 << XER_CA));
79aceca5
FB
3666}
3667
0cfe11ea 3668/* mfcr mfocrf */
99e300ef 3669static void gen_mfcr(DisasContext *ctx)
79aceca5 3670{
76a66253 3671 uint32_t crm, crn;
3b46e624 3672
76a66253
JM
3673 if (likely(ctx->opcode & 0x00100000)) {
3674 crm = CRM(ctx->opcode);
8dd640e4 3675 if (likely(crm && ((crm & (crm - 1)) == 0))) {
0cfe11ea 3676 crn = ctz32 (crm);
e1571908 3677 tcg_gen_extu_i32_tl(cpu_gpr[rD(ctx->opcode)], cpu_crf[7 - crn]);
0497d2f4
AJ
3678 tcg_gen_shli_tl(cpu_gpr[rD(ctx->opcode)],
3679 cpu_gpr[rD(ctx->opcode)], crn * 4);
76a66253 3680 }
d9bce9d9 3681 } else {
651721b2
AJ
3682 TCGv_i32 t0 = tcg_temp_new_i32();
3683 tcg_gen_mov_i32(t0, cpu_crf[0]);
3684 tcg_gen_shli_i32(t0, t0, 4);
3685 tcg_gen_or_i32(t0, t0, cpu_crf[1]);
3686 tcg_gen_shli_i32(t0, t0, 4);
3687 tcg_gen_or_i32(t0, t0, cpu_crf[2]);
3688 tcg_gen_shli_i32(t0, t0, 4);
3689 tcg_gen_or_i32(t0, t0, cpu_crf[3]);
3690 tcg_gen_shli_i32(t0, t0, 4);
3691 tcg_gen_or_i32(t0, t0, cpu_crf[4]);
3692 tcg_gen_shli_i32(t0, t0, 4);
3693 tcg_gen_or_i32(t0, t0, cpu_crf[5]);
3694 tcg_gen_shli_i32(t0, t0, 4);
3695 tcg_gen_or_i32(t0, t0, cpu_crf[6]);
3696 tcg_gen_shli_i32(t0, t0, 4);
3697 tcg_gen_or_i32(t0, t0, cpu_crf[7]);
3698 tcg_gen_extu_i32_tl(cpu_gpr[rD(ctx->opcode)], t0);
3699 tcg_temp_free_i32(t0);
d9bce9d9 3700 }
79aceca5
FB
3701}
3702
3703/* mfmsr */
99e300ef 3704static void gen_mfmsr(DisasContext *ctx)
79aceca5 3705{
9a64fbe4 3706#if defined(CONFIG_USER_ONLY)
e06fcd75 3707 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
9a64fbe4 3708#else
76db3ba4 3709 if (unlikely(!ctx->mem_idx)) {
e06fcd75 3710 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
9fddaa0c 3711 return;
9a64fbe4 3712 }
6527f6ea 3713 tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_msr);
9a64fbe4 3714#endif
79aceca5
FB
3715}
3716
a11b8151 3717#if 1
6f2d8978 3718#define SPR_NOACCESS ((void *)(-1UL))
3fc6c082
FB
3719#else
3720static void spr_noaccess (void *opaque, int sprn)
3721{
3722 sprn = ((sprn >> 5) & 0x1F) | ((sprn & 0x1F) << 5);
3723 printf("ERROR: try to access SPR %d !\n", sprn);
3724}
3725#define SPR_NOACCESS (&spr_noaccess)
3726#endif
3727
79aceca5 3728/* mfspr */
b068d6a7 3729static always_inline void gen_op_mfspr (DisasContext *ctx)
79aceca5 3730{
45d827d2 3731 void (*read_cb)(void *opaque, int gprn, int sprn);
79aceca5
FB
3732 uint32_t sprn = SPR(ctx->opcode);
3733
3fc6c082 3734#if !defined(CONFIG_USER_ONLY)
76db3ba4 3735 if (ctx->mem_idx == 2)
be147d08 3736 read_cb = ctx->spr_cb[sprn].hea_read;
76db3ba4 3737 else if (ctx->mem_idx)
3fc6c082
FB
3738 read_cb = ctx->spr_cb[sprn].oea_read;
3739 else
9a64fbe4 3740#endif
3fc6c082 3741 read_cb = ctx->spr_cb[sprn].uea_read;
76a66253
JM
3742 if (likely(read_cb != NULL)) {
3743 if (likely(read_cb != SPR_NOACCESS)) {
45d827d2 3744 (*read_cb)(ctx, rD(ctx->opcode), sprn);
3fc6c082
FB
3745 } else {
3746 /* Privilege exception */
9fceefa7
JM
3747 /* This is a hack to avoid warnings when running Linux:
3748 * this OS breaks the PowerPC virtualisation model,
3749 * allowing userland application to read the PVR
3750 */
3751 if (sprn != SPR_PVR) {
93fcfe39 3752 qemu_log("Trying to read privileged spr %d %03x at "
077fc206 3753 ADDRX "\n", sprn, sprn, ctx->nip);
077fc206
JM
3754 printf("Trying to read privileged spr %d %03x at " ADDRX "\n",
3755 sprn, sprn, ctx->nip);
f24e5695 3756 }
e06fcd75 3757 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
79aceca5 3758 }
3fc6c082
FB
3759 } else {
3760 /* Not defined */
93fcfe39 3761 qemu_log("Trying to read invalid spr %d %03x at "
077fc206 3762 ADDRX "\n", sprn, sprn, ctx->nip);
077fc206
JM
3763 printf("Trying to read invalid spr %d %03x at " ADDRX "\n",
3764 sprn, sprn, ctx->nip);
e06fcd75 3765 gen_inval_exception(ctx, POWERPC_EXCP_INVAL_SPR);
79aceca5 3766 }
79aceca5
FB
3767}
3768
99e300ef 3769static void gen_mfspr(DisasContext *ctx)
79aceca5 3770{
3fc6c082 3771 gen_op_mfspr(ctx);
76a66253 3772}
3fc6c082
FB
3773
3774/* mftb */
99e300ef 3775static void gen_mftb(DisasContext *ctx)
3fc6c082
FB
3776{
3777 gen_op_mfspr(ctx);
79aceca5
FB
3778}
3779
0cfe11ea 3780/* mtcrf mtocrf*/
99e300ef 3781static void gen_mtcrf(DisasContext *ctx)
79aceca5 3782{
76a66253 3783 uint32_t crm, crn;
3b46e624 3784
76a66253 3785 crm = CRM(ctx->opcode);
8dd640e4 3786 if (likely((ctx->opcode & 0x00100000))) {
3787 if (crm && ((crm & (crm - 1)) == 0)) {
3788 TCGv_i32 temp = tcg_temp_new_i32();
0cfe11ea 3789 crn = ctz32 (crm);
8dd640e4 3790 tcg_gen_trunc_tl_i32(temp, cpu_gpr[rS(ctx->opcode)]);
0cfe11ea
AJ
3791 tcg_gen_shri_i32(temp, temp, crn * 4);
3792 tcg_gen_andi_i32(cpu_crf[7 - crn], temp, 0xf);
8dd640e4 3793 tcg_temp_free_i32(temp);
3794 }
76a66253 3795 } else {
651721b2
AJ
3796 TCGv_i32 temp = tcg_temp_new_i32();
3797 tcg_gen_trunc_tl_i32(temp, cpu_gpr[rS(ctx->opcode)]);
3798 for (crn = 0 ; crn < 8 ; crn++) {
3799 if (crm & (1 << crn)) {
3800 tcg_gen_shri_i32(cpu_crf[7 - crn], temp, crn * 4);
3801 tcg_gen_andi_i32(cpu_crf[7 - crn], cpu_crf[7 - crn], 0xf);
3802 }
3803 }
a7812ae4 3804 tcg_temp_free_i32(temp);
76a66253 3805 }
79aceca5
FB
3806}
3807
3808/* mtmsr */
426613db 3809#if defined(TARGET_PPC64)
99e300ef 3810static void gen_mtmsrd(DisasContext *ctx)
426613db
JM
3811{
3812#if defined(CONFIG_USER_ONLY)
e06fcd75 3813 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
426613db 3814#else
76db3ba4 3815 if (unlikely(!ctx->mem_idx)) {
e06fcd75 3816 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
426613db
JM
3817 return;
3818 }
be147d08
JM
3819 if (ctx->opcode & 0x00010000) {
3820 /* Special form that does not need any synchronisation */
6527f6ea
AJ
3821 TCGv t0 = tcg_temp_new();
3822 tcg_gen_andi_tl(t0, cpu_gpr[rS(ctx->opcode)], (1 << MSR_RI) | (1 << MSR_EE));
3823 tcg_gen_andi_tl(cpu_msr, cpu_msr, ~((1 << MSR_RI) | (1 << MSR_EE)));
3824 tcg_gen_or_tl(cpu_msr, cpu_msr, t0);
3825 tcg_temp_free(t0);
be147d08 3826 } else {
056b05f8
JM
3827 /* XXX: we need to update nip before the store
3828 * if we enter power saving mode, we will exit the loop
3829 * directly from ppc_store_msr
3830 */
be147d08 3831 gen_update_nip(ctx, ctx->nip);
6527f6ea 3832 gen_helper_store_msr(cpu_gpr[rS(ctx->opcode)]);
be147d08
JM
3833 /* Must stop the translation as machine state (may have) changed */
3834 /* Note that mtmsr is not always defined as context-synchronizing */
e06fcd75 3835 gen_stop_exception(ctx);
be147d08 3836 }
426613db
JM
3837#endif
3838}
3839#endif
3840
99e300ef 3841static void gen_mtmsr(DisasContext *ctx)
79aceca5 3842{
9a64fbe4 3843#if defined(CONFIG_USER_ONLY)
e06fcd75 3844 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
9a64fbe4 3845#else
76db3ba4 3846 if (unlikely(!ctx->mem_idx)) {
e06fcd75 3847 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
9fddaa0c 3848 return;
9a64fbe4 3849 }
be147d08
JM
3850 if (ctx->opcode & 0x00010000) {
3851 /* Special form that does not need any synchronisation */
6527f6ea
AJ
3852 TCGv t0 = tcg_temp_new();
3853 tcg_gen_andi_tl(t0, cpu_gpr[rS(ctx->opcode)], (1 << MSR_RI) | (1 << MSR_EE));
3854 tcg_gen_andi_tl(cpu_msr, cpu_msr, ~((1 << MSR_RI) | (1 << MSR_EE)));
3855 tcg_gen_or_tl(cpu_msr, cpu_msr, t0);
3856 tcg_temp_free(t0);
be147d08 3857 } else {
056b05f8
JM
3858 /* XXX: we need to update nip before the store
3859 * if we enter power saving mode, we will exit the loop
3860 * directly from ppc_store_msr
3861 */
be147d08 3862 gen_update_nip(ctx, ctx->nip);
d9bce9d9 3863#if defined(TARGET_PPC64)
6527f6ea
AJ
3864 if (!ctx->sf_mode) {
3865 TCGv t0 = tcg_temp_new();
3866 TCGv t1 = tcg_temp_new();
3867 tcg_gen_andi_tl(t0, cpu_msr, 0xFFFFFFFF00000000ULL);
3868 tcg_gen_ext32u_tl(t1, cpu_gpr[rS(ctx->opcode)]);
3869 tcg_gen_or_tl(t0, t0, t1);
3870 tcg_temp_free(t1);
3871 gen_helper_store_msr(t0);
3872 tcg_temp_free(t0);
3873 } else
d9bce9d9 3874#endif
6527f6ea 3875 gen_helper_store_msr(cpu_gpr[rS(ctx->opcode)]);
be147d08 3876 /* Must stop the translation as machine state (may have) changed */
6527f6ea 3877 /* Note that mtmsr is not always defined as context-synchronizing */
e06fcd75 3878 gen_stop_exception(ctx);
be147d08 3879 }
9a64fbe4 3880#endif
79aceca5
FB
3881}
3882
3883/* mtspr */
99e300ef 3884static void gen_mtspr(DisasContext *ctx)
79aceca5 3885{
45d827d2 3886 void (*write_cb)(void *opaque, int sprn, int gprn);
79aceca5
FB
3887 uint32_t sprn = SPR(ctx->opcode);
3888
3fc6c082 3889#if !defined(CONFIG_USER_ONLY)
76db3ba4 3890 if (ctx->mem_idx == 2)
be147d08 3891 write_cb = ctx->spr_cb[sprn].hea_write;
76db3ba4 3892 else if (ctx->mem_idx)
3fc6c082
FB
3893 write_cb = ctx->spr_cb[sprn].oea_write;
3894 else
9a64fbe4 3895#endif
3fc6c082 3896 write_cb = ctx->spr_cb[sprn].uea_write;
76a66253
JM
3897 if (likely(write_cb != NULL)) {
3898 if (likely(write_cb != SPR_NOACCESS)) {
45d827d2 3899 (*write_cb)(ctx, sprn, rS(ctx->opcode));
3fc6c082
FB
3900 } else {
3901 /* Privilege exception */
93fcfe39 3902 qemu_log("Trying to write privileged spr %d %03x at "
077fc206 3903 ADDRX "\n", sprn, sprn, ctx->nip);
077fc206
JM
3904 printf("Trying to write privileged spr %d %03x at " ADDRX "\n",
3905 sprn, sprn, ctx->nip);
e06fcd75 3906 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
76a66253 3907 }
3fc6c082
FB
3908 } else {
3909 /* Not defined */
93fcfe39 3910 qemu_log("Trying to write invalid spr %d %03x at "
077fc206 3911 ADDRX "\n", sprn, sprn, ctx->nip);
077fc206
JM
3912 printf("Trying to write invalid spr %d %03x at " ADDRX "\n",
3913 sprn, sprn, ctx->nip);
e06fcd75 3914 gen_inval_exception(ctx, POWERPC_EXCP_INVAL_SPR);
79aceca5 3915 }
79aceca5
FB
3916}
3917
3918/*** Cache management ***/
99e300ef 3919
54623277 3920/* dcbf */
99e300ef 3921static void gen_dcbf(DisasContext *ctx)
79aceca5 3922{
dac454af 3923 /* XXX: specification says 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/* dcbi (Supervisor only) */
99e300ef 3933static void gen_dcbi(DisasContext *ctx)
79aceca5 3934{
a541f297 3935#if defined(CONFIG_USER_ONLY)
e06fcd75 3936 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
a541f297 3937#else
b61f2753 3938 TCGv EA, val;
76db3ba4 3939 if (unlikely(!ctx->mem_idx)) {
e06fcd75 3940 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
9fddaa0c 3941 return;
9a64fbe4 3942 }
a7812ae4 3943 EA = tcg_temp_new();
76db3ba4
AJ
3944 gen_set_access_type(ctx, ACCESS_CACHE);
3945 gen_addr_reg_index(ctx, EA);
a7812ae4 3946 val = tcg_temp_new();
76a66253 3947 /* XXX: specification says this should be treated as a store by the MMU */
76db3ba4
AJ
3948 gen_qemu_ld8u(ctx, val, EA);
3949 gen_qemu_st8(ctx, val, EA);
b61f2753
AJ
3950 tcg_temp_free(val);
3951 tcg_temp_free(EA);
a541f297 3952#endif
79aceca5
FB
3953}
3954
3955/* dcdst */
99e300ef 3956static void gen_dcbst(DisasContext *ctx)
79aceca5 3957{
76a66253 3958 /* XXX: specification say this is treated as a load by the MMU */
76db3ba4
AJ
3959 TCGv t0;
3960 gen_set_access_type(ctx, ACCESS_CACHE);
3961 t0 = tcg_temp_new();
3962 gen_addr_reg_index(ctx, t0);
3963 gen_qemu_ld8u(ctx, t0, t0);
fea0c503 3964 tcg_temp_free(t0);
79aceca5
FB
3965}
3966
3967/* dcbt */
99e300ef 3968static void gen_dcbt(DisasContext *ctx)
79aceca5 3969{
0db1b20e 3970 /* interpreted as no-op */
76a66253
JM
3971 /* XXX: specification say this is treated as a load by the MMU
3972 * but does not generate any exception
3973 */
79aceca5
FB
3974}
3975
3976/* dcbtst */
99e300ef 3977static void gen_dcbtst(DisasContext *ctx)
79aceca5 3978{
0db1b20e 3979 /* interpreted as no-op */
76a66253
JM
3980 /* XXX: specification say this is treated as a load by the MMU
3981 * but does not generate any exception
3982 */
79aceca5
FB
3983}
3984
3985/* dcbz */
99e300ef 3986static void gen_dcbz(DisasContext *ctx)
79aceca5 3987{
76db3ba4
AJ
3988 TCGv t0;
3989 gen_set_access_type(ctx, ACCESS_CACHE);
799a8c8d
AJ
3990 /* NIP cannot be restored if the memory exception comes from an helper */
3991 gen_update_nip(ctx, ctx->nip - 4);
76db3ba4
AJ
3992 t0 = tcg_temp_new();
3993 gen_addr_reg_index(ctx, t0);
799a8c8d
AJ
3994 gen_helper_dcbz(t0);
3995 tcg_temp_free(t0);
d63001d1
JM
3996}
3997
e8eaa2c0 3998static void gen_dcbz_970(DisasContext *ctx)
d63001d1 3999{
76db3ba4
AJ
4000 TCGv t0;
4001 gen_set_access_type(ctx, ACCESS_CACHE);
799a8c8d
AJ
4002 /* NIP cannot be restored if the memory exception comes from an helper */
4003 gen_update_nip(ctx, ctx->nip - 4);
76db3ba4
AJ
4004 t0 = tcg_temp_new();
4005 gen_addr_reg_index(ctx, t0);
d63001d1 4006 if (ctx->opcode & 0x00200000)
799a8c8d 4007 gen_helper_dcbz(t0);
d63001d1 4008 else
799a8c8d
AJ
4009 gen_helper_dcbz_970(t0);
4010 tcg_temp_free(t0);
79aceca5
FB
4011}
4012
ae1c1a3d 4013/* dst / dstt */
99e300ef 4014static void gen_dst(DisasContext *ctx)
ae1c1a3d
AJ
4015{
4016 if (rA(ctx->opcode) == 0) {
4017 gen_inval_exception(ctx, POWERPC_EXCP_INVAL_LSWX);
4018 } else {
4019 /* interpreted as no-op */
4020 }
4021}
4022
4023/* dstst /dststt */
99e300ef 4024static void gen_dstst(DisasContext *ctx)
ae1c1a3d
AJ
4025{
4026 if (rA(ctx->opcode) == 0) {
4027 gen_inval_exception(ctx, POWERPC_EXCP_INVAL_LSWX);
4028 } else {
4029 /* interpreted as no-op */
4030 }
4031
4032}
4033
4034/* dss / dssall */
99e300ef 4035static void gen_dss(DisasContext *ctx)
ae1c1a3d
AJ
4036{
4037 /* interpreted as no-op */
4038}
4039
79aceca5 4040/* icbi */
99e300ef 4041static void gen_icbi(DisasContext *ctx)
79aceca5 4042{
76db3ba4
AJ
4043 TCGv t0;
4044 gen_set_access_type(ctx, ACCESS_CACHE);
30032c94
JM
4045 /* NIP cannot be restored if the memory exception comes from an helper */
4046 gen_update_nip(ctx, ctx->nip - 4);
76db3ba4
AJ
4047 t0 = tcg_temp_new();
4048 gen_addr_reg_index(ctx, t0);
37d269df
AJ
4049 gen_helper_icbi(t0);
4050 tcg_temp_free(t0);
79aceca5
FB
4051}
4052
4053/* Optional: */
4054/* dcba */
99e300ef 4055static void gen_dcba(DisasContext *ctx)
79aceca5 4056{
0db1b20e
JM
4057 /* interpreted as no-op */
4058 /* XXX: specification say this is treated as a store by the MMU
4059 * but does not generate any exception
4060 */
79aceca5
FB
4061}
4062
4063/*** Segment register manipulation ***/
4064/* Supervisor only: */
99e300ef 4065
54623277 4066/* mfsr */
99e300ef 4067static void gen_mfsr(DisasContext *ctx)
79aceca5 4068{
9a64fbe4 4069#if defined(CONFIG_USER_ONLY)
e06fcd75 4070 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
9a64fbe4 4071#else
74d37793 4072 TCGv t0;
76db3ba4 4073 if (unlikely(!ctx->mem_idx)) {
e06fcd75 4074 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
9fddaa0c 4075 return;
9a64fbe4 4076 }
74d37793
AJ
4077 t0 = tcg_const_tl(SR(ctx->opcode));
4078 gen_helper_load_sr(cpu_gpr[rD(ctx->opcode)], t0);
4079 tcg_temp_free(t0);
9a64fbe4 4080#endif
79aceca5
FB
4081}
4082
4083/* mfsrin */
99e300ef 4084static void gen_mfsrin(DisasContext *ctx)
79aceca5 4085{
9a64fbe4 4086#if defined(CONFIG_USER_ONLY)
e06fcd75 4087 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
9a64fbe4 4088#else
74d37793 4089 TCGv t0;
76db3ba4 4090 if (unlikely(!ctx->mem_idx)) {
e06fcd75 4091 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
9fddaa0c 4092 return;
9a64fbe4 4093 }
74d37793
AJ
4094 t0 = tcg_temp_new();
4095 tcg_gen_shri_tl(t0, cpu_gpr[rB(ctx->opcode)], 28);
4096 tcg_gen_andi_tl(t0, t0, 0xF);
4097 gen_helper_load_sr(cpu_gpr[rD(ctx->opcode)], t0);
4098 tcg_temp_free(t0);
9a64fbe4 4099#endif
79aceca5
FB
4100}
4101
4102/* mtsr */
99e300ef 4103static void gen_mtsr(DisasContext *ctx)
79aceca5 4104{
9a64fbe4 4105#if defined(CONFIG_USER_ONLY)
e06fcd75 4106 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
9a64fbe4 4107#else
74d37793 4108 TCGv t0;
76db3ba4 4109 if (unlikely(!ctx->mem_idx)) {
e06fcd75 4110 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
9fddaa0c 4111 return;
9a64fbe4 4112 }
74d37793
AJ
4113 t0 = tcg_const_tl(SR(ctx->opcode));
4114 gen_helper_store_sr(t0, cpu_gpr[rS(ctx->opcode)]);
4115 tcg_temp_free(t0);
9a64fbe4 4116#endif
79aceca5
FB
4117}
4118
4119/* mtsrin */
99e300ef 4120static void gen_mtsrin(DisasContext *ctx)
79aceca5 4121{
9a64fbe4 4122#if defined(CONFIG_USER_ONLY)
e06fcd75 4123 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
9a64fbe4 4124#else
74d37793 4125 TCGv t0;
76db3ba4 4126 if (unlikely(!ctx->mem_idx)) {
e06fcd75 4127 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
9fddaa0c 4128 return;
9a64fbe4 4129 }
74d37793
AJ
4130 t0 = tcg_temp_new();
4131 tcg_gen_shri_tl(t0, cpu_gpr[rB(ctx->opcode)], 28);
4132 tcg_gen_andi_tl(t0, t0, 0xF);
4133 gen_helper_store_sr(t0, cpu_gpr[rD(ctx->opcode)]);
4134 tcg_temp_free(t0);
9a64fbe4 4135#endif
79aceca5
FB
4136}
4137
12de9a39
JM
4138#if defined(TARGET_PPC64)
4139/* Specific implementation for PowerPC 64 "bridge" emulation using SLB */
e8eaa2c0 4140
54623277 4141/* mfsr */
e8eaa2c0 4142static void gen_mfsr_64b(DisasContext *ctx)
12de9a39
JM
4143{
4144#if defined(CONFIG_USER_ONLY)
e06fcd75 4145 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
12de9a39 4146#else
74d37793 4147 TCGv t0;
76db3ba4 4148 if (unlikely(!ctx->mem_idx)) {
e06fcd75 4149 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
12de9a39
JM
4150 return;
4151 }
74d37793 4152 t0 = tcg_const_tl(SR(ctx->opcode));
f6b868fc 4153 gen_helper_load_sr(cpu_gpr[rD(ctx->opcode)], t0);
74d37793 4154 tcg_temp_free(t0);
12de9a39
JM
4155#endif
4156}
4157
4158/* mfsrin */
e8eaa2c0 4159static void gen_mfsrin_64b(DisasContext *ctx)
12de9a39
JM
4160{
4161#if defined(CONFIG_USER_ONLY)
e06fcd75 4162 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
12de9a39 4163#else
74d37793 4164 TCGv t0;
76db3ba4 4165 if (unlikely(!ctx->mem_idx)) {
e06fcd75 4166 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
12de9a39
JM
4167 return;
4168 }
74d37793
AJ
4169 t0 = tcg_temp_new();
4170 tcg_gen_shri_tl(t0, cpu_gpr[rB(ctx->opcode)], 28);
4171 tcg_gen_andi_tl(t0, t0, 0xF);
f6b868fc 4172 gen_helper_load_sr(cpu_gpr[rD(ctx->opcode)], t0);
74d37793 4173 tcg_temp_free(t0);
12de9a39
JM
4174#endif
4175}
4176
4177/* mtsr */
e8eaa2c0 4178static void gen_mtsr_64b(DisasContext *ctx)
12de9a39
JM
4179{
4180#if defined(CONFIG_USER_ONLY)
e06fcd75 4181 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
12de9a39 4182#else
74d37793 4183 TCGv t0;
76db3ba4 4184 if (unlikely(!ctx->mem_idx)) {
e06fcd75 4185 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
12de9a39
JM
4186 return;
4187 }
74d37793 4188 t0 = tcg_const_tl(SR(ctx->opcode));
f6b868fc 4189 gen_helper_store_sr(t0, cpu_gpr[rS(ctx->opcode)]);
74d37793 4190 tcg_temp_free(t0);
12de9a39
JM
4191#endif
4192}
4193
4194/* mtsrin */
e8eaa2c0 4195static void gen_mtsrin_64b(DisasContext *ctx)
12de9a39
JM
4196{
4197#if defined(CONFIG_USER_ONLY)
e06fcd75 4198 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
12de9a39 4199#else
74d37793 4200 TCGv t0;
76db3ba4 4201 if (unlikely(!ctx->mem_idx)) {
e06fcd75 4202 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
12de9a39
JM
4203 return;
4204 }
74d37793
AJ
4205 t0 = tcg_temp_new();
4206 tcg_gen_shri_tl(t0, cpu_gpr[rB(ctx->opcode)], 28);
4207 tcg_gen_andi_tl(t0, t0, 0xF);
f6b868fc 4208 gen_helper_store_sr(t0, cpu_gpr[rS(ctx->opcode)]);
74d37793 4209 tcg_temp_free(t0);
12de9a39
JM
4210#endif
4211}
f6b868fc
BS
4212
4213/* slbmte */
e8eaa2c0 4214static void gen_slbmte(DisasContext *ctx)
f6b868fc
BS
4215{
4216#if defined(CONFIG_USER_ONLY)
4217 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
4218#else
4219 if (unlikely(!ctx->mem_idx)) {
4220 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
4221 return;
4222 }
4223 gen_helper_store_slb(cpu_gpr[rB(ctx->opcode)], cpu_gpr[rS(ctx->opcode)]);
4224#endif
4225}
4226
12de9a39
JM
4227#endif /* defined(TARGET_PPC64) */
4228
79aceca5 4229/*** Lookaside buffer management ***/
76db3ba4 4230/* Optional & mem_idx only: */
99e300ef 4231
54623277 4232/* tlbia */
99e300ef 4233static void gen_tlbia(DisasContext *ctx)
79aceca5 4234{
9a64fbe4 4235#if defined(CONFIG_USER_ONLY)
e06fcd75 4236 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
9a64fbe4 4237#else
76db3ba4 4238 if (unlikely(!ctx->mem_idx)) {
e06fcd75 4239 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
9fddaa0c 4240 return;
9a64fbe4 4241 }
74d37793 4242 gen_helper_tlbia();
9a64fbe4 4243#endif
79aceca5
FB
4244}
4245
bf14b1ce 4246/* tlbiel */
99e300ef 4247static void gen_tlbiel(DisasContext *ctx)
bf14b1ce
BS
4248{
4249#if defined(CONFIG_USER_ONLY)
4250 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
4251#else
4252 if (unlikely(!ctx->mem_idx)) {
4253 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
4254 return;
4255 }
4256 gen_helper_tlbie(cpu_gpr[rB(ctx->opcode)]);
4257#endif
4258}
4259
79aceca5 4260/* tlbie */
99e300ef 4261static void gen_tlbie(DisasContext *ctx)
79aceca5 4262{
9a64fbe4 4263#if defined(CONFIG_USER_ONLY)
e06fcd75 4264 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
9a64fbe4 4265#else
76db3ba4 4266 if (unlikely(!ctx->mem_idx)) {
e06fcd75 4267 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
9fddaa0c 4268 return;
9a64fbe4 4269 }
d9bce9d9 4270#if defined(TARGET_PPC64)
74d37793
AJ
4271 if (!ctx->sf_mode) {
4272 TCGv t0 = tcg_temp_new();
4273 tcg_gen_ext32u_tl(t0, cpu_gpr[rB(ctx->opcode)]);
4274 gen_helper_tlbie(t0);
4275 tcg_temp_free(t0);
4276 } else
d9bce9d9 4277#endif
74d37793 4278 gen_helper_tlbie(cpu_gpr[rB(ctx->opcode)]);
9a64fbe4 4279#endif
79aceca5
FB
4280}
4281
4282/* tlbsync */
99e300ef 4283static void gen_tlbsync(DisasContext *ctx)
79aceca5 4284{
9a64fbe4 4285#if defined(CONFIG_USER_ONLY)
e06fcd75 4286 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
9a64fbe4 4287#else
76db3ba4 4288 if (unlikely(!ctx->mem_idx)) {
e06fcd75 4289 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
9fddaa0c 4290 return;
9a64fbe4
FB
4291 }
4292 /* This has no effect: it should ensure that all previous
4293 * tlbie have completed
4294 */
e06fcd75 4295 gen_stop_exception(ctx);
9a64fbe4 4296#endif
79aceca5
FB
4297}
4298
426613db
JM
4299#if defined(TARGET_PPC64)
4300/* slbia */
99e300ef 4301static void gen_slbia(DisasContext *ctx)
426613db
JM
4302{
4303#if defined(CONFIG_USER_ONLY)
e06fcd75 4304 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
426613db 4305#else
76db3ba4 4306 if (unlikely(!ctx->mem_idx)) {
e06fcd75 4307 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
426613db
JM
4308 return;
4309 }
74d37793 4310 gen_helper_slbia();
426613db
JM
4311#endif
4312}
4313
4314/* slbie */
99e300ef 4315static void gen_slbie(DisasContext *ctx)
426613db
JM
4316{
4317#if defined(CONFIG_USER_ONLY)
e06fcd75 4318 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
426613db 4319#else
76db3ba4 4320 if (unlikely(!ctx->mem_idx)) {
e06fcd75 4321 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
426613db
JM
4322 return;
4323 }
74d37793 4324 gen_helper_slbie(cpu_gpr[rB(ctx->opcode)]);
426613db
JM
4325#endif
4326}
4327#endif
4328
79aceca5
FB
4329/*** External control ***/
4330/* Optional: */
99e300ef 4331
54623277 4332/* eciwx */
99e300ef 4333static void gen_eciwx(DisasContext *ctx)
79aceca5 4334{
76db3ba4 4335 TCGv t0;
fa407c03 4336 /* Should check EAR[E] ! */
76db3ba4
AJ
4337 gen_set_access_type(ctx, ACCESS_EXT);
4338 t0 = tcg_temp_new();
4339 gen_addr_reg_index(ctx, t0);
fa407c03 4340 gen_check_align(ctx, t0, 0x03);
76db3ba4 4341 gen_qemu_ld32u(ctx, cpu_gpr[rD(ctx->opcode)], t0);
fa407c03 4342 tcg_temp_free(t0);
76a66253
JM
4343}
4344
4345/* ecowx */
99e300ef 4346static void gen_ecowx(DisasContext *ctx)
76a66253 4347{
76db3ba4 4348 TCGv t0;
fa407c03 4349 /* Should check EAR[E] ! */
76db3ba4
AJ
4350 gen_set_access_type(ctx, ACCESS_EXT);
4351 t0 = tcg_temp_new();
4352 gen_addr_reg_index(ctx, t0);
fa407c03 4353 gen_check_align(ctx, t0, 0x03);
76db3ba4 4354 gen_qemu_st32(ctx, cpu_gpr[rD(ctx->opcode)], t0);
fa407c03 4355 tcg_temp_free(t0);
76a66253
JM
4356}
4357
4358/* PowerPC 601 specific instructions */
99e300ef 4359
54623277 4360/* abs - abs. */
99e300ef 4361static void gen_abs(DisasContext *ctx)
76a66253 4362{
22e0e173
AJ
4363 int l1 = gen_new_label();
4364 int l2 = gen_new_label();
4365 tcg_gen_brcondi_tl(TCG_COND_GE, cpu_gpr[rA(ctx->opcode)], 0, l1);
4366 tcg_gen_neg_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)]);
4367 tcg_gen_br(l2);
4368 gen_set_label(l1);
4369 tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)]);
4370 gen_set_label(l2);
76a66253 4371 if (unlikely(Rc(ctx->opcode) != 0))
22e0e173 4372 gen_set_Rc0(ctx, cpu_gpr[rD(ctx->opcode)]);
76a66253
JM
4373}
4374
4375/* abso - abso. */
99e300ef 4376static void gen_abso(DisasContext *ctx)
76a66253 4377{
22e0e173
AJ
4378 int l1 = gen_new_label();
4379 int l2 = gen_new_label();
4380 int l3 = gen_new_label();
4381 /* Start with XER OV disabled, the most likely case */
4382 tcg_gen_andi_tl(cpu_xer, cpu_xer, ~(1 << XER_OV));
4383 tcg_gen_brcondi_tl(TCG_COND_GE, cpu_gpr[rA(ctx->opcode)], 0, l2);
4384 tcg_gen_brcondi_tl(TCG_COND_NE, cpu_gpr[rA(ctx->opcode)], 0x80000000, l1);
4385 tcg_gen_ori_tl(cpu_xer, cpu_xer, (1 << XER_OV) | (1 << XER_SO));
4386 tcg_gen_br(l2);
4387 gen_set_label(l1);
4388 tcg_gen_neg_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)]);
4389 tcg_gen_br(l3);
4390 gen_set_label(l2);
4391 tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)]);
4392 gen_set_label(l3);
76a66253 4393 if (unlikely(Rc(ctx->opcode) != 0))
22e0e173 4394 gen_set_Rc0(ctx, cpu_gpr[rD(ctx->opcode)]);
76a66253
JM
4395}
4396
4397/* clcs */
99e300ef 4398static void gen_clcs(DisasContext *ctx)
76a66253 4399{
22e0e173
AJ
4400 TCGv_i32 t0 = tcg_const_i32(rA(ctx->opcode));
4401 gen_helper_clcs(cpu_gpr[rD(ctx->opcode)], t0);
4402 tcg_temp_free_i32(t0);
c7697e1f 4403 /* Rc=1 sets CR0 to an undefined state */
76a66253
JM
4404}
4405
4406/* div - div. */
99e300ef 4407static void gen_div(DisasContext *ctx)
76a66253 4408{
22e0e173 4409 gen_helper_div(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)], cpu_gpr[rB(ctx->opcode)]);
76a66253 4410 if (unlikely(Rc(ctx->opcode) != 0))
22e0e173 4411 gen_set_Rc0(ctx, cpu_gpr[rD(ctx->opcode)]);
76a66253
JM
4412}
4413
4414/* divo - divo. */
99e300ef 4415static void gen_divo(DisasContext *ctx)
76a66253 4416{
22e0e173 4417 gen_helper_divo(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)], cpu_gpr[rB(ctx->opcode)]);
76a66253 4418 if (unlikely(Rc(ctx->opcode) != 0))
22e0e173 4419 gen_set_Rc0(ctx, cpu_gpr[rD(ctx->opcode)]);
76a66253
JM
4420}
4421
4422/* divs - divs. */
99e300ef 4423static void gen_divs(DisasContext *ctx)
76a66253 4424{
22e0e173 4425 gen_helper_divs(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)], cpu_gpr[rB(ctx->opcode)]);
76a66253 4426 if (unlikely(Rc(ctx->opcode) != 0))
22e0e173 4427 gen_set_Rc0(ctx, cpu_gpr[rD(ctx->opcode)]);
76a66253
JM
4428}
4429
4430/* divso - divso. */
99e300ef 4431static void gen_divso(DisasContext *ctx)
76a66253 4432{
22e0e173 4433 gen_helper_divso(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)], cpu_gpr[rB(ctx->opcode)]);
76a66253 4434 if (unlikely(Rc(ctx->opcode) != 0))
22e0e173 4435 gen_set_Rc0(ctx, cpu_gpr[rD(ctx->opcode)]);
76a66253
JM
4436}
4437
4438/* doz - doz. */
99e300ef 4439static void gen_doz(DisasContext *ctx)
76a66253 4440{
22e0e173
AJ
4441 int l1 = gen_new_label();
4442 int l2 = gen_new_label();
4443 tcg_gen_brcond_tl(TCG_COND_GE, cpu_gpr[rB(ctx->opcode)], cpu_gpr[rA(ctx->opcode)], l1);
4444 tcg_gen_sub_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rB(ctx->opcode)], cpu_gpr[rA(ctx->opcode)]);
4445 tcg_gen_br(l2);
4446 gen_set_label(l1);
4447 tcg_gen_movi_tl(cpu_gpr[rD(ctx->opcode)], 0);
4448 gen_set_label(l2);
76a66253 4449 if (unlikely(Rc(ctx->opcode) != 0))
22e0e173 4450 gen_set_Rc0(ctx, cpu_gpr[rD(ctx->opcode)]);
76a66253
JM
4451}
4452
4453/* dozo - dozo. */
99e300ef 4454static void gen_dozo(DisasContext *ctx)
76a66253 4455{
22e0e173
AJ
4456 int l1 = gen_new_label();
4457 int l2 = gen_new_label();
4458 TCGv t0 = tcg_temp_new();
4459 TCGv t1 = tcg_temp_new();
4460 TCGv t2 = tcg_temp_new();
4461 /* Start with XER OV disabled, the most likely case */
4462 tcg_gen_andi_tl(cpu_xer, cpu_xer, ~(1 << XER_OV));
4463 tcg_gen_brcond_tl(TCG_COND_GE, cpu_gpr[rB(ctx->opcode)], cpu_gpr[rA(ctx->opcode)], l1);
4464 tcg_gen_sub_tl(t0, cpu_gpr[rB(ctx->opcode)], cpu_gpr[rA(ctx->opcode)]);
4465 tcg_gen_xor_tl(t1, cpu_gpr[rB(ctx->opcode)], cpu_gpr[rA(ctx->opcode)]);
4466 tcg_gen_xor_tl(t2, cpu_gpr[rA(ctx->opcode)], t0);
4467 tcg_gen_andc_tl(t1, t1, t2);
4468 tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], t0);
4469 tcg_gen_brcondi_tl(TCG_COND_GE, t1, 0, l2);
4470 tcg_gen_ori_tl(cpu_xer, cpu_xer, (1 << XER_OV) | (1 << XER_SO));
4471 tcg_gen_br(l2);
4472 gen_set_label(l1);
4473 tcg_gen_movi_tl(cpu_gpr[rD(ctx->opcode)], 0);
4474 gen_set_label(l2);
4475 tcg_temp_free(t0);
4476 tcg_temp_free(t1);
4477 tcg_temp_free(t2);
76a66253 4478 if (unlikely(Rc(ctx->opcode) != 0))
22e0e173 4479 gen_set_Rc0(ctx, cpu_gpr[rD(ctx->opcode)]);
76a66253
JM
4480}
4481
4482/* dozi */
99e300ef 4483static void gen_dozi(DisasContext *ctx)
76a66253 4484{
22e0e173
AJ
4485 target_long simm = SIMM(ctx->opcode);
4486 int l1 = gen_new_label();
4487 int l2 = gen_new_label();
4488 tcg_gen_brcondi_tl(TCG_COND_LT, cpu_gpr[rA(ctx->opcode)], simm, l1);
4489 tcg_gen_subfi_tl(cpu_gpr[rD(ctx->opcode)], simm, cpu_gpr[rA(ctx->opcode)]);
4490 tcg_gen_br(l2);
4491 gen_set_label(l1);
4492 tcg_gen_movi_tl(cpu_gpr[rD(ctx->opcode)], 0);
4493 gen_set_label(l2);
4494 if (unlikely(Rc(ctx->opcode) != 0))
4495 gen_set_Rc0(ctx, cpu_gpr[rD(ctx->opcode)]);
76a66253
JM
4496}
4497
76a66253 4498/* lscbx - lscbx. */
99e300ef 4499static void gen_lscbx(DisasContext *ctx)
76a66253 4500{
bdb4b689
AJ
4501 TCGv t0 = tcg_temp_new();
4502 TCGv_i32 t1 = tcg_const_i32(rD(ctx->opcode));
4503 TCGv_i32 t2 = tcg_const_i32(rA(ctx->opcode));
4504 TCGv_i32 t3 = tcg_const_i32(rB(ctx->opcode));
76a66253 4505
76db3ba4 4506 gen_addr_reg_index(ctx, t0);
76a66253 4507 /* NIP cannot be restored if the memory exception comes from an helper */
d9bce9d9 4508 gen_update_nip(ctx, ctx->nip - 4);
bdb4b689
AJ
4509 gen_helper_lscbx(t0, t0, t1, t2, t3);
4510 tcg_temp_free_i32(t1);
4511 tcg_temp_free_i32(t2);
4512 tcg_temp_free_i32(t3);
3d7b417e 4513 tcg_gen_andi_tl(cpu_xer, cpu_xer, ~0x7F);
bdb4b689 4514 tcg_gen_or_tl(cpu_xer, cpu_xer, t0);
76a66253 4515 if (unlikely(Rc(ctx->opcode) != 0))
bdb4b689
AJ
4516 gen_set_Rc0(ctx, t0);
4517 tcg_temp_free(t0);
76a66253
JM
4518}
4519
4520/* maskg - maskg. */
99e300ef 4521static void gen_maskg(DisasContext *ctx)
76a66253 4522{
22e0e173
AJ
4523 int l1 = gen_new_label();
4524 TCGv t0 = tcg_temp_new();
4525 TCGv t1 = tcg_temp_new();
4526 TCGv t2 = tcg_temp_new();
4527 TCGv t3 = tcg_temp_new();
4528 tcg_gen_movi_tl(t3, 0xFFFFFFFF);
4529 tcg_gen_andi_tl(t0, cpu_gpr[rB(ctx->opcode)], 0x1F);
4530 tcg_gen_andi_tl(t1, cpu_gpr[rS(ctx->opcode)], 0x1F);
4531 tcg_gen_addi_tl(t2, t0, 1);
4532 tcg_gen_shr_tl(t2, t3, t2);
4533 tcg_gen_shr_tl(t3, t3, t1);
4534 tcg_gen_xor_tl(cpu_gpr[rA(ctx->opcode)], t2, t3);
4535 tcg_gen_brcond_tl(TCG_COND_GE, t0, t1, l1);
4536 tcg_gen_neg_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rA(ctx->opcode)]);
4537 gen_set_label(l1);
4538 tcg_temp_free(t0);
4539 tcg_temp_free(t1);
4540 tcg_temp_free(t2);
4541 tcg_temp_free(t3);
76a66253 4542 if (unlikely(Rc(ctx->opcode) != 0))
22e0e173 4543 gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
76a66253
JM
4544}
4545
4546/* maskir - maskir. */
99e300ef 4547static void gen_maskir(DisasContext *ctx)
76a66253 4548{
22e0e173
AJ
4549 TCGv t0 = tcg_temp_new();
4550 TCGv t1 = tcg_temp_new();
4551 tcg_gen_and_tl(t0, cpu_gpr[rS(ctx->opcode)], cpu_gpr[rB(ctx->opcode)]);
4552 tcg_gen_andc_tl(t1, cpu_gpr[rA(ctx->opcode)], cpu_gpr[rB(ctx->opcode)]);
4553 tcg_gen_or_tl(cpu_gpr[rA(ctx->opcode)], t0, t1);
4554 tcg_temp_free(t0);
4555 tcg_temp_free(t1);
76a66253 4556 if (unlikely(Rc(ctx->opcode) != 0))
22e0e173 4557 gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
76a66253
JM
4558}
4559
4560/* mul - mul. */
99e300ef 4561static void gen_mul(DisasContext *ctx)
76a66253 4562{
22e0e173
AJ
4563 TCGv_i64 t0 = tcg_temp_new_i64();
4564 TCGv_i64 t1 = tcg_temp_new_i64();
4565 TCGv t2 = tcg_temp_new();
4566 tcg_gen_extu_tl_i64(t0, cpu_gpr[rA(ctx->opcode)]);
4567 tcg_gen_extu_tl_i64(t1, cpu_gpr[rB(ctx->opcode)]);
4568 tcg_gen_mul_i64(t0, t0, t1);
4569 tcg_gen_trunc_i64_tl(t2, t0);
4570 gen_store_spr(SPR_MQ, t2);
4571 tcg_gen_shri_i64(t1, t0, 32);
4572 tcg_gen_trunc_i64_tl(cpu_gpr[rD(ctx->opcode)], t1);
4573 tcg_temp_free_i64(t0);
4574 tcg_temp_free_i64(t1);
4575 tcg_temp_free(t2);
76a66253 4576 if (unlikely(Rc(ctx->opcode) != 0))
22e0e173 4577 gen_set_Rc0(ctx, cpu_gpr[rD(ctx->opcode)]);
76a66253
JM
4578}
4579
4580/* mulo - mulo. */
99e300ef 4581static void gen_mulo(DisasContext *ctx)
76a66253 4582{
22e0e173
AJ
4583 int l1 = gen_new_label();
4584 TCGv_i64 t0 = tcg_temp_new_i64();
4585 TCGv_i64 t1 = tcg_temp_new_i64();
4586 TCGv t2 = tcg_temp_new();
4587 /* Start with XER OV disabled, the most likely case */
4588 tcg_gen_andi_tl(cpu_xer, cpu_xer, ~(1 << XER_OV));
4589 tcg_gen_extu_tl_i64(t0, cpu_gpr[rA(ctx->opcode)]);
4590 tcg_gen_extu_tl_i64(t1, cpu_gpr[rB(ctx->opcode)]);
4591 tcg_gen_mul_i64(t0, t0, t1);
4592 tcg_gen_trunc_i64_tl(t2, t0);
4593 gen_store_spr(SPR_MQ, t2);
4594 tcg_gen_shri_i64(t1, t0, 32);
4595 tcg_gen_trunc_i64_tl(cpu_gpr[rD(ctx->opcode)], t1);
4596 tcg_gen_ext32s_i64(t1, t0);
4597 tcg_gen_brcond_i64(TCG_COND_EQ, t0, t1, l1);
4598 tcg_gen_ori_tl(cpu_xer, cpu_xer, (1 << XER_OV) | (1 << XER_SO));
4599 gen_set_label(l1);
4600 tcg_temp_free_i64(t0);
4601 tcg_temp_free_i64(t1);
4602 tcg_temp_free(t2);
76a66253 4603 if (unlikely(Rc(ctx->opcode) != 0))
22e0e173 4604 gen_set_Rc0(ctx, cpu_gpr[rD(ctx->opcode)]);
76a66253
JM
4605}
4606
4607/* nabs - nabs. */
99e300ef 4608static void gen_nabs(DisasContext *ctx)
76a66253 4609{
22e0e173
AJ
4610 int l1 = gen_new_label();
4611 int l2 = gen_new_label();
4612 tcg_gen_brcondi_tl(TCG_COND_GT, cpu_gpr[rA(ctx->opcode)], 0, l1);
4613 tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)]);
4614 tcg_gen_br(l2);
4615 gen_set_label(l1);
4616 tcg_gen_neg_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)]);
4617 gen_set_label(l2);
76a66253 4618 if (unlikely(Rc(ctx->opcode) != 0))
22e0e173 4619 gen_set_Rc0(ctx, cpu_gpr[rD(ctx->opcode)]);
76a66253
JM
4620}
4621
4622/* nabso - nabso. */
99e300ef 4623static void gen_nabso(DisasContext *ctx)
76a66253 4624{
22e0e173
AJ
4625 int l1 = gen_new_label();
4626 int l2 = gen_new_label();
4627 tcg_gen_brcondi_tl(TCG_COND_GT, cpu_gpr[rA(ctx->opcode)], 0, l1);
4628 tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)]);
4629 tcg_gen_br(l2);
4630 gen_set_label(l1);
4631 tcg_gen_neg_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)]);
4632 gen_set_label(l2);
4633 /* nabs never overflows */
4634 tcg_gen_andi_tl(cpu_xer, cpu_xer, ~(1 << XER_OV));
76a66253 4635 if (unlikely(Rc(ctx->opcode) != 0))
22e0e173 4636 gen_set_Rc0(ctx, cpu_gpr[rD(ctx->opcode)]);
76a66253
JM
4637}
4638
4639/* rlmi - rlmi. */
99e300ef 4640static void gen_rlmi(DisasContext *ctx)
76a66253 4641{
7487953d
AJ
4642 uint32_t mb = MB(ctx->opcode);
4643 uint32_t me = ME(ctx->opcode);
4644 TCGv t0 = tcg_temp_new();
4645 tcg_gen_andi_tl(t0, cpu_gpr[rB(ctx->opcode)], 0x1F);
4646 tcg_gen_rotl_tl(t0, cpu_gpr[rS(ctx->opcode)], t0);
4647 tcg_gen_andi_tl(t0, t0, MASK(mb, me));
4648 tcg_gen_andi_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rA(ctx->opcode)], ~MASK(mb, me));
4649 tcg_gen_or_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rA(ctx->opcode)], t0);
4650 tcg_temp_free(t0);
76a66253 4651 if (unlikely(Rc(ctx->opcode) != 0))
7487953d 4652 gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
76a66253
JM
4653}
4654
4655/* rrib - rrib. */
99e300ef 4656static void gen_rrib(DisasContext *ctx)
76a66253 4657{
7487953d
AJ
4658 TCGv t0 = tcg_temp_new();
4659 TCGv t1 = tcg_temp_new();
4660 tcg_gen_andi_tl(t0, cpu_gpr[rB(ctx->opcode)], 0x1F);
4661 tcg_gen_movi_tl(t1, 0x80000000);
4662 tcg_gen_shr_tl(t1, t1, t0);
4663 tcg_gen_shr_tl(t0, cpu_gpr[rS(ctx->opcode)], t0);
4664 tcg_gen_and_tl(t0, t0, t1);
4665 tcg_gen_andc_tl(t1, cpu_gpr[rA(ctx->opcode)], t1);
4666 tcg_gen_or_tl(cpu_gpr[rA(ctx->opcode)], t0, t1);
4667 tcg_temp_free(t0);
4668 tcg_temp_free(t1);
76a66253 4669 if (unlikely(Rc(ctx->opcode) != 0))
7487953d 4670 gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
76a66253
JM
4671}
4672
4673/* sle - sle. */
99e300ef 4674static void gen_sle(DisasContext *ctx)
76a66253 4675{
7487953d
AJ
4676 TCGv t0 = tcg_temp_new();
4677 TCGv t1 = tcg_temp_new();
4678 tcg_gen_andi_tl(t1, cpu_gpr[rB(ctx->opcode)], 0x1F);
4679 tcg_gen_shl_tl(t0, cpu_gpr[rS(ctx->opcode)], t1);
4680 tcg_gen_subfi_tl(t1, 32, t1);
4681 tcg_gen_shr_tl(t1, cpu_gpr[rS(ctx->opcode)], t1);
4682 tcg_gen_or_tl(t1, t0, t1);
4683 tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], t0);
4684 gen_store_spr(SPR_MQ, t1);
4685 tcg_temp_free(t0);
4686 tcg_temp_free(t1);
76a66253 4687 if (unlikely(Rc(ctx->opcode) != 0))
7487953d 4688 gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
76a66253
JM
4689}
4690
4691/* sleq - sleq. */
99e300ef 4692static void gen_sleq(DisasContext *ctx)
76a66253 4693{
7487953d
AJ
4694 TCGv t0 = tcg_temp_new();
4695 TCGv t1 = tcg_temp_new();
4696 TCGv t2 = tcg_temp_new();
4697 tcg_gen_andi_tl(t0, cpu_gpr[rB(ctx->opcode)], 0x1F);
4698 tcg_gen_movi_tl(t2, 0xFFFFFFFF);
4699 tcg_gen_shl_tl(t2, t2, t0);
4700 tcg_gen_rotl_tl(t0, cpu_gpr[rS(ctx->opcode)], t0);
4701 gen_load_spr(t1, SPR_MQ);
4702 gen_store_spr(SPR_MQ, t0);
4703 tcg_gen_and_tl(t0, t0, t2);
4704 tcg_gen_andc_tl(t1, t1, t2);
4705 tcg_gen_or_tl(cpu_gpr[rA(ctx->opcode)], t0, t1);
4706 tcg_temp_free(t0);
4707 tcg_temp_free(t1);
4708 tcg_temp_free(t2);
76a66253 4709 if (unlikely(Rc(ctx->opcode) != 0))
7487953d 4710 gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
76a66253
JM
4711}
4712
4713/* sliq - sliq. */
99e300ef 4714static void gen_sliq(DisasContext *ctx)
76a66253 4715{
7487953d
AJ
4716 int sh = SH(ctx->opcode);
4717 TCGv t0 = tcg_temp_new();
4718 TCGv t1 = tcg_temp_new();
4719 tcg_gen_shli_tl(t0, cpu_gpr[rS(ctx->opcode)], sh);
4720 tcg_gen_shri_tl(t1, cpu_gpr[rS(ctx->opcode)], 32 - sh);
4721 tcg_gen_or_tl(t1, t0, t1);
4722 tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], t0);
4723 gen_store_spr(SPR_MQ, t1);
4724 tcg_temp_free(t0);
4725 tcg_temp_free(t1);
76a66253 4726 if (unlikely(Rc(ctx->opcode) != 0))
7487953d 4727 gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
76a66253
JM
4728}
4729
4730/* slliq - slliq. */
99e300ef 4731static void gen_slliq(DisasContext *ctx)
76a66253 4732{
7487953d
AJ
4733 int sh = SH(ctx->opcode);
4734 TCGv t0 = tcg_temp_new();
4735 TCGv t1 = tcg_temp_new();
4736 tcg_gen_rotli_tl(t0, cpu_gpr[rS(ctx->opcode)], sh);
4737 gen_load_spr(t1, SPR_MQ);
4738 gen_store_spr(SPR_MQ, t0);
4739 tcg_gen_andi_tl(t0, t0, (0xFFFFFFFFU << sh));
4740 tcg_gen_andi_tl(t1, t1, ~(0xFFFFFFFFU << sh));
4741 tcg_gen_or_tl(cpu_gpr[rA(ctx->opcode)], t0, t1);
4742 tcg_temp_free(t0);
4743 tcg_temp_free(t1);
76a66253 4744 if (unlikely(Rc(ctx->opcode) != 0))
7487953d 4745 gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
76a66253
JM
4746}
4747
4748/* sllq - sllq. */
99e300ef 4749static void gen_sllq(DisasContext *ctx)
76a66253 4750{
7487953d
AJ
4751 int l1 = gen_new_label();
4752 int l2 = gen_new_label();
4753 TCGv t0 = tcg_temp_local_new();
4754 TCGv t1 = tcg_temp_local_new();
4755 TCGv t2 = tcg_temp_local_new();
4756 tcg_gen_andi_tl(t2, cpu_gpr[rB(ctx->opcode)], 0x1F);
4757 tcg_gen_movi_tl(t1, 0xFFFFFFFF);
4758 tcg_gen_shl_tl(t1, t1, t2);
4759 tcg_gen_andi_tl(t0, cpu_gpr[rB(ctx->opcode)], 0x20);
4760 tcg_gen_brcondi_tl(TCG_COND_EQ, t0, 0, l1);
4761 gen_load_spr(t0, SPR_MQ);
4762 tcg_gen_and_tl(cpu_gpr[rA(ctx->opcode)], t0, t1);
4763 tcg_gen_br(l2);
4764 gen_set_label(l1);
4765 tcg_gen_shl_tl(t0, cpu_gpr[rS(ctx->opcode)], t2);
4766 gen_load_spr(t2, SPR_MQ);
4767 tcg_gen_andc_tl(t1, t2, t1);
4768 tcg_gen_or_tl(cpu_gpr[rA(ctx->opcode)], t0, t1);
4769 gen_set_label(l2);
4770 tcg_temp_free(t0);
4771 tcg_temp_free(t1);
4772 tcg_temp_free(t2);
76a66253 4773 if (unlikely(Rc(ctx->opcode) != 0))
7487953d 4774 gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
76a66253
JM
4775}
4776
4777/* slq - slq. */
99e300ef 4778static void gen_slq(DisasContext *ctx)
76a66253 4779{
7487953d
AJ
4780 int l1 = gen_new_label();
4781 TCGv t0 = tcg_temp_new();
4782 TCGv t1 = tcg_temp_new();
4783 tcg_gen_andi_tl(t1, cpu_gpr[rB(ctx->opcode)], 0x1F);
4784 tcg_gen_shl_tl(t0, cpu_gpr[rS(ctx->opcode)], t1);
4785 tcg_gen_subfi_tl(t1, 32, t1);
4786 tcg_gen_shr_tl(t1, cpu_gpr[rS(ctx->opcode)], t1);
4787 tcg_gen_or_tl(t1, t0, t1);
4788 gen_store_spr(SPR_MQ, t1);
4789 tcg_gen_andi_tl(t1, cpu_gpr[rB(ctx->opcode)], 0x20);
4790 tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], t0);
4791 tcg_gen_brcondi_tl(TCG_COND_EQ, t1, 0, l1);
4792 tcg_gen_movi_tl(cpu_gpr[rA(ctx->opcode)], 0);
4793 gen_set_label(l1);
4794 tcg_temp_free(t0);
4795 tcg_temp_free(t1);
76a66253 4796 if (unlikely(Rc(ctx->opcode) != 0))
7487953d 4797 gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
76a66253
JM
4798}
4799
d9bce9d9 4800/* sraiq - sraiq. */
99e300ef 4801static void gen_sraiq(DisasContext *ctx)
76a66253 4802{
7487953d
AJ
4803 int sh = SH(ctx->opcode);
4804 int l1 = gen_new_label();
4805 TCGv t0 = tcg_temp_new();
4806 TCGv t1 = tcg_temp_new();
4807 tcg_gen_shri_tl(t0, cpu_gpr[rS(ctx->opcode)], sh);
4808 tcg_gen_shli_tl(t1, cpu_gpr[rS(ctx->opcode)], 32 - sh);
4809 tcg_gen_or_tl(t0, t0, t1);
4810 gen_store_spr(SPR_MQ, t0);
4811 tcg_gen_andi_tl(cpu_xer, cpu_xer, ~(1 << XER_CA));
4812 tcg_gen_brcondi_tl(TCG_COND_EQ, t1, 0, l1);
4813 tcg_gen_brcondi_tl(TCG_COND_GE, cpu_gpr[rS(ctx->opcode)], 0, l1);
4814 tcg_gen_ori_tl(cpu_xer, cpu_xer, (1 << XER_CA));
4815 gen_set_label(l1);
4816 tcg_gen_sari_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)], sh);
4817 tcg_temp_free(t0);
4818 tcg_temp_free(t1);
76a66253 4819 if (unlikely(Rc(ctx->opcode) != 0))
7487953d 4820 gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
76a66253
JM
4821}
4822
4823/* sraq - sraq. */
99e300ef 4824static void gen_sraq(DisasContext *ctx)
76a66253 4825{
7487953d
AJ
4826 int l1 = gen_new_label();
4827 int l2 = gen_new_label();
4828 TCGv t0 = tcg_temp_new();
4829 TCGv t1 = tcg_temp_local_new();
4830 TCGv t2 = tcg_temp_local_new();
4831 tcg_gen_andi_tl(t2, cpu_gpr[rB(ctx->opcode)], 0x1F);
4832 tcg_gen_shr_tl(t0, cpu_gpr[rS(ctx->opcode)], t2);
4833 tcg_gen_sar_tl(t1, cpu_gpr[rS(ctx->opcode)], t2);
4834 tcg_gen_subfi_tl(t2, 32, t2);
4835 tcg_gen_shl_tl(t2, cpu_gpr[rS(ctx->opcode)], t2);
4836 tcg_gen_or_tl(t0, t0, t2);
4837 gen_store_spr(SPR_MQ, t0);
4838 tcg_gen_andi_tl(t0, cpu_gpr[rB(ctx->opcode)], 0x20);
4839 tcg_gen_brcondi_tl(TCG_COND_EQ, t2, 0, l1);
4840 tcg_gen_mov_tl(t2, cpu_gpr[rS(ctx->opcode)]);
4841 tcg_gen_sari_tl(t1, cpu_gpr[rS(ctx->opcode)], 31);
4842 gen_set_label(l1);
4843 tcg_temp_free(t0);
4844 tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], t1);
4845 tcg_gen_andi_tl(cpu_xer, cpu_xer, ~(1 << XER_CA));
4846 tcg_gen_brcondi_tl(TCG_COND_GE, t1, 0, l2);
4847 tcg_gen_brcondi_tl(TCG_COND_EQ, t2, 0, l2);
4848 tcg_gen_ori_tl(cpu_xer, cpu_xer, (1 << XER_CA));
4849 gen_set_label(l2);
4850 tcg_temp_free(t1);
4851 tcg_temp_free(t2);
76a66253 4852 if (unlikely(Rc(ctx->opcode) != 0))
7487953d 4853 gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
76a66253
JM
4854}
4855
4856/* sre - sre. */
99e300ef 4857static void gen_sre(DisasContext *ctx)
76a66253 4858{
7487953d
AJ
4859 TCGv t0 = tcg_temp_new();
4860 TCGv t1 = tcg_temp_new();
4861 tcg_gen_andi_tl(t1, cpu_gpr[rB(ctx->opcode)], 0x1F);
4862 tcg_gen_shr_tl(t0, cpu_gpr[rS(ctx->opcode)], t1);
4863 tcg_gen_subfi_tl(t1, 32, t1);
4864 tcg_gen_shl_tl(t1, cpu_gpr[rS(ctx->opcode)], t1);
4865 tcg_gen_or_tl(t1, t0, t1);
4866 tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], t0);
4867 gen_store_spr(SPR_MQ, t1);
4868 tcg_temp_free(t0);
4869 tcg_temp_free(t1);
76a66253 4870 if (unlikely(Rc(ctx->opcode) != 0))
7487953d 4871 gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
76a66253
JM
4872}
4873
4874/* srea - srea. */
99e300ef 4875static void gen_srea(DisasContext *ctx)
76a66253 4876{
7487953d
AJ
4877 TCGv t0 = tcg_temp_new();
4878 TCGv t1 = tcg_temp_new();
4879 tcg_gen_andi_tl(t1, cpu_gpr[rB(ctx->opcode)], 0x1F);
4880 tcg_gen_rotr_tl(t0, cpu_gpr[rS(ctx->opcode)], t1);
4881 gen_store_spr(SPR_MQ, t0);
4882 tcg_gen_sar_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)], t1);
4883 tcg_temp_free(t0);
4884 tcg_temp_free(t1);
76a66253 4885 if (unlikely(Rc(ctx->opcode) != 0))
7487953d 4886 gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
76a66253
JM
4887}
4888
4889/* sreq */
99e300ef 4890static void gen_sreq(DisasContext *ctx)
76a66253 4891{
7487953d
AJ
4892 TCGv t0 = tcg_temp_new();
4893 TCGv t1 = tcg_temp_new();
4894 TCGv t2 = tcg_temp_new();
4895 tcg_gen_andi_tl(t0, cpu_gpr[rB(ctx->opcode)], 0x1F);
4896 tcg_gen_movi_tl(t1, 0xFFFFFFFF);
4897 tcg_gen_shr_tl(t1, t1, t0);
4898 tcg_gen_rotr_tl(t0, cpu_gpr[rS(ctx->opcode)], t0);
4899 gen_load_spr(t2, SPR_MQ);
4900 gen_store_spr(SPR_MQ, t0);
4901 tcg_gen_and_tl(t0, t0, t1);
4902 tcg_gen_andc_tl(t2, t2, t1);
4903 tcg_gen_or_tl(cpu_gpr[rA(ctx->opcode)], t0, t2);
4904 tcg_temp_free(t0);
4905 tcg_temp_free(t1);
4906 tcg_temp_free(t2);
76a66253 4907 if (unlikely(Rc(ctx->opcode) != 0))
7487953d 4908 gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
76a66253
JM
4909}
4910
4911/* sriq */
99e300ef 4912static void gen_sriq(DisasContext *ctx)
76a66253 4913{
7487953d
AJ
4914 int sh = SH(ctx->opcode);
4915 TCGv t0 = tcg_temp_new();
4916 TCGv t1 = tcg_temp_new();
4917 tcg_gen_shri_tl(t0, cpu_gpr[rS(ctx->opcode)], sh);
4918 tcg_gen_shli_tl(t1, cpu_gpr[rS(ctx->opcode)], 32 - sh);
4919 tcg_gen_or_tl(t1, t0, t1);
4920 tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], t0);
4921 gen_store_spr(SPR_MQ, t1);
4922 tcg_temp_free(t0);
4923 tcg_temp_free(t1);
76a66253 4924 if (unlikely(Rc(ctx->opcode) != 0))
7487953d 4925 gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
76a66253
JM
4926}
4927
4928/* srliq */
99e300ef 4929static void gen_srliq(DisasContext *ctx)
76a66253 4930{
7487953d
AJ
4931 int sh = SH(ctx->opcode);
4932 TCGv t0 = tcg_temp_new();
4933 TCGv t1 = tcg_temp_new();
4934 tcg_gen_rotri_tl(t0, cpu_gpr[rS(ctx->opcode)], sh);
4935 gen_load_spr(t1, SPR_MQ);
4936 gen_store_spr(SPR_MQ, t0);
4937 tcg_gen_andi_tl(t0, t0, (0xFFFFFFFFU >> sh));
4938 tcg_gen_andi_tl(t1, t1, ~(0xFFFFFFFFU >> sh));
4939 tcg_gen_or_tl(cpu_gpr[rA(ctx->opcode)], t0, t1);
4940 tcg_temp_free(t0);
4941 tcg_temp_free(t1);
76a66253 4942 if (unlikely(Rc(ctx->opcode) != 0))
7487953d 4943 gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
76a66253
JM
4944}
4945
4946/* srlq */
99e300ef 4947static void gen_srlq(DisasContext *ctx)
76a66253 4948{
7487953d
AJ
4949 int l1 = gen_new_label();
4950 int l2 = gen_new_label();
4951 TCGv t0 = tcg_temp_local_new();
4952 TCGv t1 = tcg_temp_local_new();
4953 TCGv t2 = tcg_temp_local_new();
4954 tcg_gen_andi_tl(t2, cpu_gpr[rB(ctx->opcode)], 0x1F);
4955 tcg_gen_movi_tl(t1, 0xFFFFFFFF);
4956 tcg_gen_shr_tl(t2, t1, t2);
4957 tcg_gen_andi_tl(t0, cpu_gpr[rB(ctx->opcode)], 0x20);
4958 tcg_gen_brcondi_tl(TCG_COND_EQ, t0, 0, l1);
4959 gen_load_spr(t0, SPR_MQ);
4960 tcg_gen_and_tl(cpu_gpr[rA(ctx->opcode)], t0, t2);
4961 tcg_gen_br(l2);
4962 gen_set_label(l1);
4963 tcg_gen_shr_tl(t0, cpu_gpr[rS(ctx->opcode)], t2);
4964 tcg_gen_and_tl(t0, t0, t2);
4965 gen_load_spr(t1, SPR_MQ);
4966 tcg_gen_andc_tl(t1, t1, t2);
4967 tcg_gen_or_tl(cpu_gpr[rA(ctx->opcode)], t0, t1);
4968 gen_set_label(l2);
4969 tcg_temp_free(t0);
4970 tcg_temp_free(t1);
4971 tcg_temp_free(t2);
76a66253 4972 if (unlikely(Rc(ctx->opcode) != 0))
7487953d 4973 gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
76a66253
JM
4974}
4975
4976/* srq */
99e300ef 4977static void gen_srq(DisasContext *ctx)
76a66253 4978{
7487953d
AJ
4979 int l1 = gen_new_label();
4980 TCGv t0 = tcg_temp_new();
4981 TCGv t1 = tcg_temp_new();
4982 tcg_gen_andi_tl(t1, cpu_gpr[rB(ctx->opcode)], 0x1F);
4983 tcg_gen_shr_tl(t0, cpu_gpr[rS(ctx->opcode)], t1);
4984 tcg_gen_subfi_tl(t1, 32, t1);
4985 tcg_gen_shl_tl(t1, cpu_gpr[rS(ctx->opcode)], t1);
4986 tcg_gen_or_tl(t1, t0, t1);
4987 gen_store_spr(SPR_MQ, t1);
4988 tcg_gen_andi_tl(t1, cpu_gpr[rB(ctx->opcode)], 0x20);
4989 tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], t0);
4990 tcg_gen_brcondi_tl(TCG_COND_EQ, t0, 0, l1);
4991 tcg_gen_movi_tl(cpu_gpr[rA(ctx->opcode)], 0);
4992 gen_set_label(l1);
4993 tcg_temp_free(t0);
4994 tcg_temp_free(t1);
76a66253 4995 if (unlikely(Rc(ctx->opcode) != 0))
7487953d 4996 gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
76a66253
JM
4997}
4998
4999/* PowerPC 602 specific instructions */
99e300ef 5000
54623277 5001/* dsa */
99e300ef 5002static void gen_dsa(DisasContext *ctx)
76a66253
JM
5003{
5004 /* XXX: TODO */
e06fcd75 5005 gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL);
76a66253
JM
5006}
5007
5008/* esa */
99e300ef 5009static void gen_esa(DisasContext *ctx)
76a66253
JM
5010{
5011 /* XXX: TODO */
e06fcd75 5012 gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL);
76a66253
JM
5013}
5014
5015/* mfrom */
99e300ef 5016static void gen_mfrom(DisasContext *ctx)
76a66253
JM
5017{
5018#if defined(CONFIG_USER_ONLY)
e06fcd75 5019 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
76a66253 5020#else
76db3ba4 5021 if (unlikely(!ctx->mem_idx)) {
e06fcd75 5022 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
76a66253
JM
5023 return;
5024 }
cf02a65c 5025 gen_helper_602_mfrom(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)]);
76a66253
JM
5026#endif
5027}
5028
5029/* 602 - 603 - G2 TLB management */
e8eaa2c0 5030
54623277 5031/* tlbld */
e8eaa2c0 5032static void gen_tlbld_6xx(DisasContext *ctx)
76a66253
JM
5033{
5034#if defined(CONFIG_USER_ONLY)
e06fcd75 5035 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
76a66253 5036#else
76db3ba4 5037 if (unlikely(!ctx->mem_idx)) {
e06fcd75 5038 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
76a66253
JM
5039 return;
5040 }
74d37793 5041 gen_helper_6xx_tlbd(cpu_gpr[rB(ctx->opcode)]);
76a66253
JM
5042#endif
5043}
5044
5045/* tlbli */
e8eaa2c0 5046static void gen_tlbli_6xx(DisasContext *ctx)
76a66253
JM
5047{
5048#if defined(CONFIG_USER_ONLY)
e06fcd75 5049 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
76a66253 5050#else
76db3ba4 5051 if (unlikely(!ctx->mem_idx)) {
e06fcd75 5052 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
76a66253
JM
5053 return;
5054 }
74d37793 5055 gen_helper_6xx_tlbi(cpu_gpr[rB(ctx->opcode)]);
76a66253
JM
5056#endif
5057}
5058
7dbe11ac 5059/* 74xx TLB management */
e8eaa2c0 5060
54623277 5061/* tlbld */
e8eaa2c0 5062static void gen_tlbld_74xx(DisasContext *ctx)
7dbe11ac
JM
5063{
5064#if defined(CONFIG_USER_ONLY)
e06fcd75 5065 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
7dbe11ac 5066#else
76db3ba4 5067 if (unlikely(!ctx->mem_idx)) {
e06fcd75 5068 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
7dbe11ac
JM
5069 return;
5070 }
74d37793 5071 gen_helper_74xx_tlbd(cpu_gpr[rB(ctx->opcode)]);
7dbe11ac
JM
5072#endif
5073}
5074
5075/* tlbli */
e8eaa2c0 5076static void gen_tlbli_74xx(DisasContext *ctx)
7dbe11ac
JM
5077{
5078#if defined(CONFIG_USER_ONLY)
e06fcd75 5079 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
7dbe11ac 5080#else
76db3ba4 5081 if (unlikely(!ctx->mem_idx)) {
e06fcd75 5082 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
7dbe11ac
JM
5083 return;
5084 }
74d37793 5085 gen_helper_74xx_tlbi(cpu_gpr[rB(ctx->opcode)]);
7dbe11ac
JM
5086#endif
5087}
5088
76a66253 5089/* POWER instructions not in PowerPC 601 */
99e300ef 5090
54623277 5091/* clf */
99e300ef 5092static void gen_clf(DisasContext *ctx)
76a66253
JM
5093{
5094 /* Cache line flush: implemented as no-op */
5095}
5096
5097/* cli */
99e300ef 5098static void gen_cli(DisasContext *ctx)
76a66253 5099{
7f75ffd3 5100 /* Cache line invalidate: privileged and treated as no-op */
76a66253 5101#if defined(CONFIG_USER_ONLY)
e06fcd75 5102 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
76a66253 5103#else
76db3ba4 5104 if (unlikely(!ctx->mem_idx)) {
e06fcd75 5105 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
76a66253
JM
5106 return;
5107 }
5108#endif
5109}
5110
5111/* dclst */
99e300ef 5112static void gen_dclst(DisasContext *ctx)
76a66253
JM
5113{
5114 /* Data cache line store: treated as no-op */
5115}
5116
99e300ef 5117static void gen_mfsri(DisasContext *ctx)
76a66253
JM
5118{
5119#if defined(CONFIG_USER_ONLY)
e06fcd75 5120 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
76a66253 5121#else
74d37793
AJ
5122 int ra = rA(ctx->opcode);
5123 int rd = rD(ctx->opcode);
5124 TCGv t0;
76db3ba4 5125 if (unlikely(!ctx->mem_idx)) {
e06fcd75 5126 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
76a66253
JM
5127 return;
5128 }
74d37793 5129 t0 = tcg_temp_new();
76db3ba4 5130 gen_addr_reg_index(ctx, t0);
74d37793
AJ
5131 tcg_gen_shri_tl(t0, t0, 28);
5132 tcg_gen_andi_tl(t0, t0, 0xF);
5133 gen_helper_load_sr(cpu_gpr[rd], t0);
5134 tcg_temp_free(t0);
76a66253 5135 if (ra != 0 && ra != rd)
74d37793 5136 tcg_gen_mov_tl(cpu_gpr[ra], cpu_gpr[rd]);
76a66253
JM
5137#endif
5138}
5139
99e300ef 5140static void gen_rac(DisasContext *ctx)
76a66253
JM
5141{
5142#if defined(CONFIG_USER_ONLY)
e06fcd75 5143 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
76a66253 5144#else
22e0e173 5145 TCGv t0;
76db3ba4 5146 if (unlikely(!ctx->mem_idx)) {
e06fcd75 5147 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
76a66253
JM
5148 return;
5149 }
22e0e173 5150 t0 = tcg_temp_new();
76db3ba4 5151 gen_addr_reg_index(ctx, t0);
22e0e173
AJ
5152 gen_helper_rac(cpu_gpr[rD(ctx->opcode)], t0);
5153 tcg_temp_free(t0);
76a66253
JM
5154#endif
5155}
5156
99e300ef 5157static void gen_rfsvc(DisasContext *ctx)
76a66253
JM
5158{
5159#if defined(CONFIG_USER_ONLY)
e06fcd75 5160 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
76a66253 5161#else
76db3ba4 5162 if (unlikely(!ctx->mem_idx)) {
e06fcd75 5163 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
76a66253
JM
5164 return;
5165 }
d72a19f7 5166 gen_helper_rfsvc();
e06fcd75 5167 gen_sync_exception(ctx);
76a66253
JM
5168#endif
5169}
5170
5171/* svc is not implemented for now */
5172
5173/* POWER2 specific instructions */
5174/* Quad manipulation (load/store two floats at a time) */
76a66253
JM
5175
5176/* lfq */
99e300ef 5177static void gen_lfq(DisasContext *ctx)
76a66253 5178{
01a4afeb 5179 int rd = rD(ctx->opcode);
76db3ba4
AJ
5180 TCGv t0;
5181 gen_set_access_type(ctx, ACCESS_FLOAT);
5182 t0 = tcg_temp_new();
5183 gen_addr_imm_index(ctx, t0, 0);
5184 gen_qemu_ld64(ctx, cpu_fpr[rd], t0);
5185 gen_addr_add(ctx, t0, t0, 8);
5186 gen_qemu_ld64(ctx, cpu_fpr[(rd + 1) % 32], t0);
01a4afeb 5187 tcg_temp_free(t0);
76a66253
JM
5188}
5189
5190/* lfqu */
99e300ef 5191static void gen_lfqu(DisasContext *ctx)
76a66253
JM
5192{
5193 int ra = rA(ctx->opcode);
01a4afeb 5194 int rd = rD(ctx->opcode);
76db3ba4
AJ
5195 TCGv t0, t1;
5196 gen_set_access_type(ctx, ACCESS_FLOAT);
5197 t0 = tcg_temp_new();
5198 t1 = tcg_temp_new();
5199 gen_addr_imm_index(ctx, t0, 0);
5200 gen_qemu_ld64(ctx, cpu_fpr[rd], t0);
5201 gen_addr_add(ctx, t1, t0, 8);
5202 gen_qemu_ld64(ctx, cpu_fpr[(rd + 1) % 32], t1);
76a66253 5203 if (ra != 0)
01a4afeb
AJ
5204 tcg_gen_mov_tl(cpu_gpr[ra], t0);
5205 tcg_temp_free(t0);
5206 tcg_temp_free(t1);
76a66253
JM
5207}
5208
5209/* lfqux */
99e300ef 5210static void gen_lfqux(DisasContext *ctx)
76a66253
JM
5211{
5212 int ra = rA(ctx->opcode);
01a4afeb 5213 int rd = rD(ctx->opcode);
76db3ba4
AJ
5214 gen_set_access_type(ctx, ACCESS_FLOAT);
5215 TCGv t0, t1;
5216 t0 = tcg_temp_new();
5217 gen_addr_reg_index(ctx, t0);
5218 gen_qemu_ld64(ctx, cpu_fpr[rd], t0);
5219 t1 = tcg_temp_new();
5220 gen_addr_add(ctx, t1, t0, 8);
5221 gen_qemu_ld64(ctx, cpu_fpr[(rd + 1) % 32], t1);
5222 tcg_temp_free(t1);
76a66253 5223 if (ra != 0)
01a4afeb
AJ
5224 tcg_gen_mov_tl(cpu_gpr[ra], t0);
5225 tcg_temp_free(t0);
76a66253
JM
5226}
5227
5228/* lfqx */
99e300ef 5229static void gen_lfqx(DisasContext *ctx)
76a66253 5230{
01a4afeb 5231 int rd = rD(ctx->opcode);
76db3ba4
AJ
5232 TCGv t0;
5233 gen_set_access_type(ctx, ACCESS_FLOAT);
5234 t0 = tcg_temp_new();
5235 gen_addr_reg_index(ctx, t0);
5236 gen_qemu_ld64(ctx, cpu_fpr[rd], t0);
5237 gen_addr_add(ctx, t0, t0, 8);
5238 gen_qemu_ld64(ctx, cpu_fpr[(rd + 1) % 32], t0);
01a4afeb 5239 tcg_temp_free(t0);
76a66253
JM
5240}
5241
5242/* stfq */
99e300ef 5243static void gen_stfq(DisasContext *ctx)
76a66253 5244{
01a4afeb 5245 int rd = rD(ctx->opcode);
76db3ba4
AJ
5246 TCGv t0;
5247 gen_set_access_type(ctx, ACCESS_FLOAT);
5248 t0 = tcg_temp_new();
5249 gen_addr_imm_index(ctx, t0, 0);
5250 gen_qemu_st64(ctx, cpu_fpr[rd], t0);
5251 gen_addr_add(ctx, t0, t0, 8);
5252 gen_qemu_st64(ctx, cpu_fpr[(rd + 1) % 32], t0);
01a4afeb 5253 tcg_temp_free(t0);
76a66253
JM
5254}
5255
5256/* stfqu */
99e300ef 5257static void gen_stfqu(DisasContext *ctx)
76a66253
JM
5258{
5259 int ra = rA(ctx->opcode);
01a4afeb 5260 int rd = rD(ctx->opcode);
76db3ba4
AJ
5261 TCGv t0, t1;
5262 gen_set_access_type(ctx, ACCESS_FLOAT);
5263 t0 = tcg_temp_new();
5264 gen_addr_imm_index(ctx, t0, 0);
5265 gen_qemu_st64(ctx, cpu_fpr[rd], t0);
5266 t1 = tcg_temp_new();
5267 gen_addr_add(ctx, t1, t0, 8);
5268 gen_qemu_st64(ctx, cpu_fpr[(rd + 1) % 32], t1);
5269 tcg_temp_free(t1);
76a66253 5270 if (ra != 0)
01a4afeb
AJ
5271 tcg_gen_mov_tl(cpu_gpr[ra], t0);
5272 tcg_temp_free(t0);
76a66253
JM
5273}
5274
5275/* stfqux */
99e300ef 5276static void gen_stfqux(DisasContext *ctx)
76a66253
JM
5277{
5278 int ra = rA(ctx->opcode);
01a4afeb 5279 int rd = rD(ctx->opcode);
76db3ba4
AJ
5280 TCGv t0, t1;
5281 gen_set_access_type(ctx, ACCESS_FLOAT);
5282 t0 = tcg_temp_new();
5283 gen_addr_reg_index(ctx, t0);
5284 gen_qemu_st64(ctx, cpu_fpr[rd], t0);
5285 t1 = tcg_temp_new();
5286 gen_addr_add(ctx, t1, t0, 8);
5287 gen_qemu_st64(ctx, cpu_fpr[(rd + 1) % 32], t1);
5288 tcg_temp_free(t1);
76a66253 5289 if (ra != 0)
01a4afeb
AJ
5290 tcg_gen_mov_tl(cpu_gpr[ra], t0);
5291 tcg_temp_free(t0);
76a66253
JM
5292}
5293
5294/* stfqx */
99e300ef 5295static void gen_stfqx(DisasContext *ctx)
76a66253 5296{
01a4afeb 5297 int rd = rD(ctx->opcode);
76db3ba4
AJ
5298 TCGv t0;
5299 gen_set_access_type(ctx, ACCESS_FLOAT);
5300 t0 = tcg_temp_new();
5301 gen_addr_reg_index(ctx, t0);
5302 gen_qemu_st64(ctx, cpu_fpr[rd], t0);
5303 gen_addr_add(ctx, t0, t0, 8);
5304 gen_qemu_st64(ctx, cpu_fpr[(rd + 1) % 32], t0);
01a4afeb 5305 tcg_temp_free(t0);
76a66253
JM
5306}
5307
5308/* BookE specific instructions */
99e300ef 5309
54623277 5310/* XXX: not implemented on 440 ? */
99e300ef 5311static void gen_mfapidi(DisasContext *ctx)
76a66253
JM
5312{
5313 /* XXX: TODO */
e06fcd75 5314 gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL);
76a66253
JM
5315}
5316
2662a059 5317/* XXX: not implemented on 440 ? */
99e300ef 5318static void gen_tlbiva(DisasContext *ctx)
76a66253
JM
5319{
5320#if defined(CONFIG_USER_ONLY)
e06fcd75 5321 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
76a66253 5322#else
74d37793 5323 TCGv t0;
76db3ba4 5324 if (unlikely(!ctx->mem_idx)) {
e06fcd75 5325 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
76a66253
JM
5326 return;
5327 }
ec72e276 5328 t0 = tcg_temp_new();
76db3ba4 5329 gen_addr_reg_index(ctx, t0);
74d37793
AJ
5330 gen_helper_tlbie(cpu_gpr[rB(ctx->opcode)]);
5331 tcg_temp_free(t0);
76a66253
JM
5332#endif
5333}
5334
5335/* All 405 MAC instructions are translated here */
b068d6a7
JM
5336static always_inline void gen_405_mulladd_insn (DisasContext *ctx,
5337 int opc2, int opc3,
5338 int ra, int rb, int rt, int Rc)
76a66253 5339{
182608d4
AJ
5340 TCGv t0, t1;
5341
a7812ae4
PB
5342 t0 = tcg_temp_local_new();
5343 t1 = tcg_temp_local_new();
182608d4 5344
76a66253
JM
5345 switch (opc3 & 0x0D) {
5346 case 0x05:
5347 /* macchw - macchw. - macchwo - macchwo. */
5348 /* macchws - macchws. - macchwso - macchwso. */
5349 /* nmacchw - nmacchw. - nmacchwo - nmacchwo. */
5350 /* nmacchws - nmacchws. - nmacchwso - nmacchwso. */
5351 /* mulchw - mulchw. */
182608d4
AJ
5352 tcg_gen_ext16s_tl(t0, cpu_gpr[ra]);
5353 tcg_gen_sari_tl(t1, cpu_gpr[rb], 16);
5354 tcg_gen_ext16s_tl(t1, t1);
76a66253
JM
5355 break;
5356 case 0x04:
5357 /* macchwu - macchwu. - macchwuo - macchwuo. */
5358 /* macchwsu - macchwsu. - macchwsuo - macchwsuo. */
5359 /* mulchwu - mulchwu. */
182608d4
AJ
5360 tcg_gen_ext16u_tl(t0, cpu_gpr[ra]);
5361 tcg_gen_shri_tl(t1, cpu_gpr[rb], 16);
5362 tcg_gen_ext16u_tl(t1, t1);
76a66253
JM
5363 break;
5364 case 0x01:
5365 /* machhw - machhw. - machhwo - machhwo. */
5366 /* machhws - machhws. - machhwso - machhwso. */
5367 /* nmachhw - nmachhw. - nmachhwo - nmachhwo. */
5368 /* nmachhws - nmachhws. - nmachhwso - nmachhwso. */
5369 /* mulhhw - mulhhw. */
182608d4
AJ
5370 tcg_gen_sari_tl(t0, cpu_gpr[ra], 16);
5371 tcg_gen_ext16s_tl(t0, t0);
5372 tcg_gen_sari_tl(t1, cpu_gpr[rb], 16);
5373 tcg_gen_ext16s_tl(t1, t1);
76a66253
JM
5374 break;
5375 case 0x00:
5376 /* machhwu - machhwu. - machhwuo - machhwuo. */
5377 /* machhwsu - machhwsu. - machhwsuo - machhwsuo. */
5378 /* mulhhwu - mulhhwu. */
182608d4
AJ
5379 tcg_gen_shri_tl(t0, cpu_gpr[ra], 16);
5380 tcg_gen_ext16u_tl(t0, t0);
5381 tcg_gen_shri_tl(t1, cpu_gpr[rb], 16);
5382 tcg_gen_ext16u_tl(t1, t1);
76a66253
JM
5383 break;
5384 case 0x0D:
5385 /* maclhw - maclhw. - maclhwo - maclhwo. */
5386 /* maclhws - maclhws. - maclhwso - maclhwso. */
5387 /* nmaclhw - nmaclhw. - nmaclhwo - nmaclhwo. */
5388 /* nmaclhws - nmaclhws. - nmaclhwso - nmaclhwso. */
5389 /* mullhw - mullhw. */
182608d4
AJ
5390 tcg_gen_ext16s_tl(t0, cpu_gpr[ra]);
5391 tcg_gen_ext16s_tl(t1, cpu_gpr[rb]);
76a66253
JM
5392 break;
5393 case 0x0C:
5394 /* maclhwu - maclhwu. - maclhwuo - maclhwuo. */
5395 /* maclhwsu - maclhwsu. - maclhwsuo - maclhwsuo. */
5396 /* mullhwu - mullhwu. */
182608d4
AJ
5397 tcg_gen_ext16u_tl(t0, cpu_gpr[ra]);
5398 tcg_gen_ext16u_tl(t1, cpu_gpr[rb]);
76a66253
JM
5399 break;
5400 }
76a66253 5401 if (opc2 & 0x04) {
182608d4
AJ
5402 /* (n)multiply-and-accumulate (0x0C / 0x0E) */
5403 tcg_gen_mul_tl(t1, t0, t1);
5404 if (opc2 & 0x02) {
5405 /* nmultiply-and-accumulate (0x0E) */
5406 tcg_gen_sub_tl(t0, cpu_gpr[rt], t1);
5407 } else {
5408 /* multiply-and-accumulate (0x0C) */
5409 tcg_gen_add_tl(t0, cpu_gpr[rt], t1);
5410 }
5411
5412 if (opc3 & 0x12) {
5413 /* Check overflow and/or saturate */
5414 int l1 = gen_new_label();
5415
5416 if (opc3 & 0x10) {
5417 /* Start with XER OV disabled, the most likely case */
5418 tcg_gen_andi_tl(cpu_xer, cpu_xer, ~(1 << XER_OV));
5419 }
5420 if (opc3 & 0x01) {
5421 /* Signed */
5422 tcg_gen_xor_tl(t1, cpu_gpr[rt], t1);
5423 tcg_gen_brcondi_tl(TCG_COND_GE, t1, 0, l1);
5424 tcg_gen_xor_tl(t1, cpu_gpr[rt], t0);
5425 tcg_gen_brcondi_tl(TCG_COND_LT, t1, 0, l1);
bdc4e053 5426 if (opc3 & 0x02) {
182608d4
AJ
5427 /* Saturate */
5428 tcg_gen_sari_tl(t0, cpu_gpr[rt], 31);
5429 tcg_gen_xori_tl(t0, t0, 0x7fffffff);
5430 }
5431 } else {
5432 /* Unsigned */
5433 tcg_gen_brcond_tl(TCG_COND_GEU, t0, t1, l1);
bdc4e053 5434 if (opc3 & 0x02) {
182608d4
AJ
5435 /* Saturate */
5436 tcg_gen_movi_tl(t0, UINT32_MAX);
5437 }
5438 }
5439 if (opc3 & 0x10) {
5440 /* Check overflow */
5441 tcg_gen_ori_tl(cpu_xer, cpu_xer, (1 << XER_OV) | (1 << XER_SO));
5442 }
5443 gen_set_label(l1);
5444 tcg_gen_mov_tl(cpu_gpr[rt], t0);
5445 }
5446 } else {
5447 tcg_gen_mul_tl(cpu_gpr[rt], t0, t1);
76a66253 5448 }
182608d4
AJ
5449 tcg_temp_free(t0);
5450 tcg_temp_free(t1);
76a66253
JM
5451 if (unlikely(Rc) != 0) {
5452 /* Update Rc0 */
182608d4 5453 gen_set_Rc0(ctx, cpu_gpr[rt]);
76a66253
JM
5454 }
5455}
5456
a750fc0b 5457#define GEN_MAC_HANDLER(name, opc2, opc3) \
99e300ef 5458static void glue(gen_, name)(DisasContext *ctx) \
76a66253
JM
5459{ \
5460 gen_405_mulladd_insn(ctx, opc2, opc3, rA(ctx->opcode), rB(ctx->opcode), \
5461 rD(ctx->opcode), Rc(ctx->opcode)); \
5462}
5463
5464/* macchw - macchw. */
a750fc0b 5465GEN_MAC_HANDLER(macchw, 0x0C, 0x05);
76a66253 5466/* macchwo - macchwo. */
a750fc0b 5467GEN_MAC_HANDLER(macchwo, 0x0C, 0x15);
76a66253 5468/* macchws - macchws. */
a750fc0b 5469GEN_MAC_HANDLER(macchws, 0x0C, 0x07);
76a66253 5470/* macchwso - macchwso. */
a750fc0b 5471GEN_MAC_HANDLER(macchwso, 0x0C, 0x17);
76a66253 5472/* macchwsu - macchwsu. */
a750fc0b 5473GEN_MAC_HANDLER(macchwsu, 0x0C, 0x06);
76a66253 5474/* macchwsuo - macchwsuo. */
a750fc0b 5475GEN_MAC_HANDLER(macchwsuo, 0x0C, 0x16);
76a66253 5476/* macchwu - macchwu. */
a750fc0b 5477GEN_MAC_HANDLER(macchwu, 0x0C, 0x04);
76a66253 5478/* macchwuo - macchwuo. */
a750fc0b 5479GEN_MAC_HANDLER(macchwuo, 0x0C, 0x14);
76a66253 5480/* machhw - machhw. */
a750fc0b 5481GEN_MAC_HANDLER(machhw, 0x0C, 0x01);
76a66253 5482/* machhwo - machhwo. */
a750fc0b 5483GEN_MAC_HANDLER(machhwo, 0x0C, 0x11);
76a66253 5484/* machhws - machhws. */
a750fc0b 5485GEN_MAC_HANDLER(machhws, 0x0C, 0x03);
76a66253 5486/* machhwso - machhwso. */
a750fc0b 5487GEN_MAC_HANDLER(machhwso, 0x0C, 0x13);
76a66253 5488/* machhwsu - machhwsu. */
a750fc0b 5489GEN_MAC_HANDLER(machhwsu, 0x0C, 0x02);
76a66253 5490/* machhwsuo - machhwsuo. */
a750fc0b 5491GEN_MAC_HANDLER(machhwsuo, 0x0C, 0x12);
76a66253 5492/* machhwu - machhwu. */
a750fc0b 5493GEN_MAC_HANDLER(machhwu, 0x0C, 0x00);
76a66253 5494/* machhwuo - machhwuo. */
a750fc0b 5495GEN_MAC_HANDLER(machhwuo, 0x0C, 0x10);
76a66253 5496/* maclhw - maclhw. */
a750fc0b 5497GEN_MAC_HANDLER(maclhw, 0x0C, 0x0D);
76a66253 5498/* maclhwo - maclhwo. */
a750fc0b 5499GEN_MAC_HANDLER(maclhwo, 0x0C, 0x1D);
76a66253 5500/* maclhws - maclhws. */
a750fc0b 5501GEN_MAC_HANDLER(maclhws, 0x0C, 0x0F);
76a66253 5502/* maclhwso - maclhwso. */
a750fc0b 5503GEN_MAC_HANDLER(maclhwso, 0x0C, 0x1F);
76a66253 5504/* maclhwu - maclhwu. */
a750fc0b 5505GEN_MAC_HANDLER(maclhwu, 0x0C, 0x0C);
76a66253 5506/* maclhwuo - maclhwuo. */
a750fc0b 5507GEN_MAC_HANDLER(maclhwuo, 0x0C, 0x1C);
76a66253 5508/* maclhwsu - maclhwsu. */
a750fc0b 5509GEN_MAC_HANDLER(maclhwsu, 0x0C, 0x0E);
76a66253 5510/* maclhwsuo - maclhwsuo. */
a750fc0b 5511GEN_MAC_HANDLER(maclhwsuo, 0x0C, 0x1E);
76a66253 5512/* nmacchw - nmacchw. */
a750fc0b 5513GEN_MAC_HANDLER(nmacchw, 0x0E, 0x05);
76a66253 5514/* nmacchwo - nmacchwo. */
a750fc0b 5515GEN_MAC_HANDLER(nmacchwo, 0x0E, 0x15);
76a66253 5516/* nmacchws - nmacchws. */
a750fc0b 5517GEN_MAC_HANDLER(nmacchws, 0x0E, 0x07);
76a66253 5518/* nmacchwso - nmacchwso. */
a750fc0b 5519GEN_MAC_HANDLER(nmacchwso, 0x0E, 0x17);
76a66253 5520/* nmachhw - nmachhw. */
a750fc0b 5521GEN_MAC_HANDLER(nmachhw, 0x0E, 0x01);
76a66253 5522/* nmachhwo - nmachhwo. */
a750fc0b 5523GEN_MAC_HANDLER(nmachhwo, 0x0E, 0x11);
76a66253 5524/* nmachhws - nmachhws. */
a750fc0b 5525GEN_MAC_HANDLER(nmachhws, 0x0E, 0x03);
76a66253 5526/* nmachhwso - nmachhwso. */
a750fc0b 5527GEN_MAC_HANDLER(nmachhwso, 0x0E, 0x13);
76a66253 5528/* nmaclhw - nmaclhw. */
a750fc0b 5529GEN_MAC_HANDLER(nmaclhw, 0x0E, 0x0D);
76a66253 5530/* nmaclhwo - nmaclhwo. */
a750fc0b 5531GEN_MAC_HANDLER(nmaclhwo, 0x0E, 0x1D);
76a66253 5532/* nmaclhws - nmaclhws. */
a750fc0b 5533GEN_MAC_HANDLER(nmaclhws, 0x0E, 0x0F);
76a66253 5534/* nmaclhwso - nmaclhwso. */
a750fc0b 5535GEN_MAC_HANDLER(nmaclhwso, 0x0E, 0x1F);
76a66253
JM
5536
5537/* mulchw - mulchw. */
a750fc0b 5538GEN_MAC_HANDLER(mulchw, 0x08, 0x05);
76a66253 5539/* mulchwu - mulchwu. */
a750fc0b 5540GEN_MAC_HANDLER(mulchwu, 0x08, 0x04);
76a66253 5541/* mulhhw - mulhhw. */
a750fc0b 5542GEN_MAC_HANDLER(mulhhw, 0x08, 0x01);
76a66253 5543/* mulhhwu - mulhhwu. */
a750fc0b 5544GEN_MAC_HANDLER(mulhhwu, 0x08, 0x00);
76a66253 5545/* mullhw - mullhw. */
a750fc0b 5546GEN_MAC_HANDLER(mullhw, 0x08, 0x0D);
76a66253 5547/* mullhwu - mullhwu. */
a750fc0b 5548GEN_MAC_HANDLER(mullhwu, 0x08, 0x0C);
76a66253
JM
5549
5550/* mfdcr */
99e300ef 5551static void gen_mfdcr(DisasContext *ctx)
76a66253
JM
5552{
5553#if defined(CONFIG_USER_ONLY)
e06fcd75 5554 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
76a66253 5555#else
06dca6a7 5556 TCGv dcrn;
76db3ba4 5557 if (unlikely(!ctx->mem_idx)) {
e06fcd75 5558 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
76a66253
JM
5559 return;
5560 }
06dca6a7
AJ
5561 /* NIP cannot be restored if the memory exception comes from an helper */
5562 gen_update_nip(ctx, ctx->nip - 4);
5563 dcrn = tcg_const_tl(SPR(ctx->opcode));
5564 gen_helper_load_dcr(cpu_gpr[rD(ctx->opcode)], dcrn);
5565 tcg_temp_free(dcrn);
76a66253
JM
5566#endif
5567}
5568
5569/* mtdcr */
99e300ef 5570static void gen_mtdcr(DisasContext *ctx)
76a66253
JM
5571{
5572#if defined(CONFIG_USER_ONLY)
e06fcd75 5573 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
76a66253 5574#else
06dca6a7 5575 TCGv dcrn;
76db3ba4 5576 if (unlikely(!ctx->mem_idx)) {
e06fcd75 5577 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
76a66253
JM
5578 return;
5579 }
06dca6a7
AJ
5580 /* NIP cannot be restored if the memory exception comes from an helper */
5581 gen_update_nip(ctx, ctx->nip - 4);
5582 dcrn = tcg_const_tl(SPR(ctx->opcode));
5583 gen_helper_store_dcr(dcrn, cpu_gpr[rS(ctx->opcode)]);
5584 tcg_temp_free(dcrn);
a42bd6cc
JM
5585#endif
5586}
5587
5588/* mfdcrx */
2662a059 5589/* XXX: not implemented on 440 ? */
99e300ef 5590static void gen_mfdcrx(DisasContext *ctx)
a42bd6cc
JM
5591{
5592#if defined(CONFIG_USER_ONLY)
e06fcd75 5593 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
a42bd6cc 5594#else
76db3ba4 5595 if (unlikely(!ctx->mem_idx)) {
e06fcd75 5596 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
a42bd6cc
JM
5597 return;
5598 }
06dca6a7
AJ
5599 /* NIP cannot be restored if the memory exception comes from an helper */
5600 gen_update_nip(ctx, ctx->nip - 4);
5601 gen_helper_load_dcr(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)]);
a750fc0b 5602 /* Note: Rc update flag set leads to undefined state of Rc0 */
a42bd6cc
JM
5603#endif
5604}
5605
5606/* mtdcrx */
2662a059 5607/* XXX: not implemented on 440 ? */
99e300ef 5608static void gen_mtdcrx(DisasContext *ctx)
a42bd6cc
JM
5609{
5610#if defined(CONFIG_USER_ONLY)
e06fcd75 5611 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
a42bd6cc 5612#else
76db3ba4 5613 if (unlikely(!ctx->mem_idx)) {
e06fcd75 5614 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
a42bd6cc
JM
5615 return;
5616 }
06dca6a7
AJ
5617 /* NIP cannot be restored if the memory exception comes from an helper */
5618 gen_update_nip(ctx, ctx->nip - 4);
5619 gen_helper_store_dcr(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)]);
a750fc0b 5620 /* Note: Rc update flag set leads to undefined state of Rc0 */
76a66253
JM
5621#endif
5622}
5623
a750fc0b 5624/* mfdcrux (PPC 460) : user-mode access to DCR */
99e300ef 5625static void gen_mfdcrux(DisasContext *ctx)
a750fc0b 5626{
06dca6a7
AJ
5627 /* NIP cannot be restored if the memory exception comes from an helper */
5628 gen_update_nip(ctx, ctx->nip - 4);
5629 gen_helper_load_dcr(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)]);
a750fc0b
JM
5630 /* Note: Rc update flag set leads to undefined state of Rc0 */
5631}
5632
5633/* mtdcrux (PPC 460) : user-mode access to DCR */
99e300ef 5634static void gen_mtdcrux(DisasContext *ctx)
a750fc0b 5635{
06dca6a7
AJ
5636 /* NIP cannot be restored if the memory exception comes from an helper */
5637 gen_update_nip(ctx, ctx->nip - 4);
5638 gen_helper_store_dcr(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)]);
a750fc0b
JM
5639 /* Note: Rc update flag set leads to undefined state of Rc0 */
5640}
5641
76a66253 5642/* dccci */
99e300ef 5643static void gen_dccci(DisasContext *ctx)
76a66253
JM
5644{
5645#if defined(CONFIG_USER_ONLY)
e06fcd75 5646 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
76a66253 5647#else
76db3ba4 5648 if (unlikely(!ctx->mem_idx)) {
e06fcd75 5649 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
76a66253
JM
5650 return;
5651 }
5652 /* interpreted as no-op */
5653#endif
5654}
5655
5656/* dcread */
99e300ef 5657static void gen_dcread(DisasContext *ctx)
76a66253
JM
5658{
5659#if defined(CONFIG_USER_ONLY)
e06fcd75 5660 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
76a66253 5661#else
b61f2753 5662 TCGv EA, val;
76db3ba4 5663 if (unlikely(!ctx->mem_idx)) {
e06fcd75 5664 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
76a66253
JM
5665 return;
5666 }
76db3ba4 5667 gen_set_access_type(ctx, ACCESS_CACHE);
a7812ae4 5668 EA = tcg_temp_new();
76db3ba4 5669 gen_addr_reg_index(ctx, EA);
a7812ae4 5670 val = tcg_temp_new();
76db3ba4 5671 gen_qemu_ld32u(ctx, val, EA);
b61f2753
AJ
5672 tcg_temp_free(val);
5673 tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], EA);
5674 tcg_temp_free(EA);
76a66253
JM
5675#endif
5676}
5677
5678/* icbt */
e8eaa2c0 5679static void gen_icbt_40x(DisasContext *ctx)
76a66253
JM
5680{
5681 /* interpreted as no-op */
5682 /* XXX: specification say this is treated as a load by the MMU
5683 * but does not generate any exception
5684 */
5685}
5686
5687/* iccci */
99e300ef 5688static void gen_iccci(DisasContext *ctx)
76a66253
JM
5689{
5690#if defined(CONFIG_USER_ONLY)
e06fcd75 5691 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
76a66253 5692#else
76db3ba4 5693 if (unlikely(!ctx->mem_idx)) {
e06fcd75 5694 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
76a66253
JM
5695 return;
5696 }
5697 /* interpreted as no-op */
5698#endif
5699}
5700
5701/* icread */
99e300ef 5702static void gen_icread(DisasContext *ctx)
76a66253
JM
5703{
5704#if defined(CONFIG_USER_ONLY)
e06fcd75 5705 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
76a66253 5706#else
76db3ba4 5707 if (unlikely(!ctx->mem_idx)) {
e06fcd75 5708 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
76a66253
JM
5709 return;
5710 }
5711 /* interpreted as no-op */
5712#endif
5713}
5714
76db3ba4 5715/* rfci (mem_idx only) */
e8eaa2c0 5716static void gen_rfci_40x(DisasContext *ctx)
a42bd6cc
JM
5717{
5718#if defined(CONFIG_USER_ONLY)
e06fcd75 5719 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
a42bd6cc 5720#else
76db3ba4 5721 if (unlikely(!ctx->mem_idx)) {
e06fcd75 5722 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
a42bd6cc
JM
5723 return;
5724 }
5725 /* Restore CPU state */
d72a19f7 5726 gen_helper_40x_rfci();
e06fcd75 5727 gen_sync_exception(ctx);
a42bd6cc
JM
5728#endif
5729}
5730
99e300ef 5731static void gen_rfci(DisasContext *ctx)
a42bd6cc
JM
5732{
5733#if defined(CONFIG_USER_ONLY)
e06fcd75 5734 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
a42bd6cc 5735#else
76db3ba4 5736 if (unlikely(!ctx->mem_idx)) {
e06fcd75 5737 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
a42bd6cc
JM
5738 return;
5739 }
5740 /* Restore CPU state */
d72a19f7 5741 gen_helper_rfci();
e06fcd75 5742 gen_sync_exception(ctx);
a42bd6cc
JM
5743#endif
5744}
5745
5746/* BookE specific */
99e300ef 5747
54623277 5748/* XXX: not implemented on 440 ? */
99e300ef 5749static void gen_rfdi(DisasContext *ctx)
76a66253
JM
5750{
5751#if defined(CONFIG_USER_ONLY)
e06fcd75 5752 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
76a66253 5753#else
76db3ba4 5754 if (unlikely(!ctx->mem_idx)) {
e06fcd75 5755 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
76a66253
JM
5756 return;
5757 }
5758 /* Restore CPU state */
d72a19f7 5759 gen_helper_rfdi();
e06fcd75 5760 gen_sync_exception(ctx);
76a66253
JM
5761#endif
5762}
5763
2662a059 5764/* XXX: not implemented on 440 ? */
99e300ef 5765static void gen_rfmci(DisasContext *ctx)
a42bd6cc
JM
5766{
5767#if defined(CONFIG_USER_ONLY)
e06fcd75 5768 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
a42bd6cc 5769#else
76db3ba4 5770 if (unlikely(!ctx->mem_idx)) {
e06fcd75 5771 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
a42bd6cc
JM
5772 return;
5773 }
5774 /* Restore CPU state */
d72a19f7 5775 gen_helper_rfmci();
e06fcd75 5776 gen_sync_exception(ctx);
a42bd6cc
JM
5777#endif
5778}
5eb7995e 5779
d9bce9d9 5780/* TLB management - PowerPC 405 implementation */
e8eaa2c0 5781
54623277 5782/* tlbre */
e8eaa2c0 5783static void gen_tlbre_40x(DisasContext *ctx)
76a66253
JM
5784{
5785#if defined(CONFIG_USER_ONLY)
e06fcd75 5786 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
76a66253 5787#else
76db3ba4 5788 if (unlikely(!ctx->mem_idx)) {
e06fcd75 5789 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
76a66253
JM
5790 return;
5791 }
5792 switch (rB(ctx->opcode)) {
5793 case 0:
74d37793 5794 gen_helper_4xx_tlbre_hi(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)]);
76a66253
JM
5795 break;
5796 case 1:
74d37793 5797 gen_helper_4xx_tlbre_lo(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)]);
76a66253
JM
5798 break;
5799 default:
e06fcd75 5800 gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL);
76a66253 5801 break;
9a64fbe4 5802 }
76a66253
JM
5803#endif
5804}
5805
d9bce9d9 5806/* tlbsx - tlbsx. */
e8eaa2c0 5807static void gen_tlbsx_40x(DisasContext *ctx)
76a66253
JM
5808{
5809#if defined(CONFIG_USER_ONLY)
e06fcd75 5810 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
76a66253 5811#else
74d37793 5812 TCGv t0;
76db3ba4 5813 if (unlikely(!ctx->mem_idx)) {
e06fcd75 5814 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
76a66253
JM
5815 return;
5816 }
74d37793 5817 t0 = tcg_temp_new();
76db3ba4 5818 gen_addr_reg_index(ctx, t0);
74d37793
AJ
5819 gen_helper_4xx_tlbsx(cpu_gpr[rD(ctx->opcode)], t0);
5820 tcg_temp_free(t0);
5821 if (Rc(ctx->opcode)) {
5822 int l1 = gen_new_label();
5823 tcg_gen_trunc_tl_i32(cpu_crf[0], cpu_xer);
5824 tcg_gen_shri_i32(cpu_crf[0], cpu_crf[0], XER_SO);
5825 tcg_gen_andi_i32(cpu_crf[0], cpu_crf[0], 1);
5826 tcg_gen_brcondi_tl(TCG_COND_EQ, cpu_gpr[rD(ctx->opcode)], -1, l1);
5827 tcg_gen_ori_i32(cpu_crf[0], cpu_crf[0], 0x02);
5828 gen_set_label(l1);
5829 }
76a66253 5830#endif
79aceca5
FB
5831}
5832
76a66253 5833/* tlbwe */
e8eaa2c0 5834static void gen_tlbwe_40x(DisasContext *ctx)
79aceca5 5835{
76a66253 5836#if defined(CONFIG_USER_ONLY)
e06fcd75 5837 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
76a66253 5838#else
76db3ba4 5839 if (unlikely(!ctx->mem_idx)) {
e06fcd75 5840 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
76a66253
JM
5841 return;
5842 }
5843 switch (rB(ctx->opcode)) {
5844 case 0:
74d37793 5845 gen_helper_4xx_tlbwe_hi(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)]);
76a66253
JM
5846 break;
5847 case 1:
74d37793 5848 gen_helper_4xx_tlbwe_lo(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)]);
76a66253
JM
5849 break;
5850 default:
e06fcd75 5851 gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL);
76a66253 5852 break;
9a64fbe4 5853 }
76a66253
JM
5854#endif
5855}
5856
a4bb6c3e 5857/* TLB management - PowerPC 440 implementation */
e8eaa2c0 5858
54623277 5859/* tlbre */
e8eaa2c0 5860static void gen_tlbre_440(DisasContext *ctx)
5eb7995e
JM
5861{
5862#if defined(CONFIG_USER_ONLY)
e06fcd75 5863 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
5eb7995e 5864#else
76db3ba4 5865 if (unlikely(!ctx->mem_idx)) {
e06fcd75 5866 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
5eb7995e
JM
5867 return;
5868 }
5869 switch (rB(ctx->opcode)) {
5870 case 0:
5eb7995e 5871 case 1:
5eb7995e 5872 case 2:
74d37793
AJ
5873 {
5874 TCGv_i32 t0 = tcg_const_i32(rB(ctx->opcode));
5875 gen_helper_440_tlbwe(t0, cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)]);
5876 tcg_temp_free_i32(t0);
5877 }
5eb7995e
JM
5878 break;
5879 default:
e06fcd75 5880 gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL);
5eb7995e
JM
5881 break;
5882 }
5883#endif
5884}
5885
5886/* tlbsx - tlbsx. */
e8eaa2c0 5887static void gen_tlbsx_440(DisasContext *ctx)
5eb7995e
JM
5888{
5889#if defined(CONFIG_USER_ONLY)
e06fcd75 5890 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
5eb7995e 5891#else
74d37793 5892 TCGv t0;
76db3ba4 5893 if (unlikely(!ctx->mem_idx)) {
e06fcd75 5894 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
5eb7995e
JM
5895 return;
5896 }
74d37793 5897 t0 = tcg_temp_new();
76db3ba4 5898 gen_addr_reg_index(ctx, t0);
74d37793
AJ
5899 gen_helper_440_tlbsx(cpu_gpr[rD(ctx->opcode)], t0);
5900 tcg_temp_free(t0);
5901 if (Rc(ctx->opcode)) {
5902 int l1 = gen_new_label();
5903 tcg_gen_trunc_tl_i32(cpu_crf[0], cpu_xer);
5904 tcg_gen_shri_i32(cpu_crf[0], cpu_crf[0], XER_SO);
5905 tcg_gen_andi_i32(cpu_crf[0], cpu_crf[0], 1);
5906 tcg_gen_brcondi_tl(TCG_COND_EQ, cpu_gpr[rD(ctx->opcode)], -1, l1);
5907 tcg_gen_ori_i32(cpu_crf[0], cpu_crf[0], 0x02);
5908 gen_set_label(l1);
5909 }
5eb7995e
JM
5910#endif
5911}
5912
5913/* tlbwe */
e8eaa2c0 5914static void gen_tlbwe_440(DisasContext *ctx)
5eb7995e
JM
5915{
5916#if defined(CONFIG_USER_ONLY)
e06fcd75 5917 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
5eb7995e 5918#else
76db3ba4 5919 if (unlikely(!ctx->mem_idx)) {
e06fcd75 5920 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
5eb7995e
JM
5921 return;
5922 }
5923 switch (rB(ctx->opcode)) {
5924 case 0:
5eb7995e 5925 case 1:
5eb7995e 5926 case 2:
74d37793
AJ
5927 {
5928 TCGv_i32 t0 = tcg_const_i32(rB(ctx->opcode));
5929 gen_helper_440_tlbwe(t0, cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)]);
5930 tcg_temp_free_i32(t0);
5931 }
5eb7995e
JM
5932 break;
5933 default:
e06fcd75 5934 gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL);
5eb7995e
JM
5935 break;
5936 }
5937#endif
5938}
5939
76a66253 5940/* wrtee */
99e300ef 5941static void gen_wrtee(DisasContext *ctx)
76a66253
JM
5942{
5943#if defined(CONFIG_USER_ONLY)
e06fcd75 5944 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
76a66253 5945#else
6527f6ea 5946 TCGv t0;
76db3ba4 5947 if (unlikely(!ctx->mem_idx)) {
e06fcd75 5948 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
76a66253
JM
5949 return;
5950 }
6527f6ea
AJ
5951 t0 = tcg_temp_new();
5952 tcg_gen_andi_tl(t0, cpu_gpr[rD(ctx->opcode)], (1 << MSR_EE));
5953 tcg_gen_andi_tl(cpu_msr, cpu_msr, ~(1 << MSR_EE));
5954 tcg_gen_or_tl(cpu_msr, cpu_msr, t0);
5955 tcg_temp_free(t0);
dee96f6c
JM
5956 /* Stop translation to have a chance to raise an exception
5957 * if we just set msr_ee to 1
5958 */
e06fcd75 5959 gen_stop_exception(ctx);
76a66253
JM
5960#endif
5961}
5962
5963/* wrteei */
99e300ef 5964static void gen_wrteei(DisasContext *ctx)
76a66253
JM
5965{
5966#if defined(CONFIG_USER_ONLY)
e06fcd75 5967 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
76a66253 5968#else
76db3ba4 5969 if (unlikely(!ctx->mem_idx)) {
e06fcd75 5970 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
76a66253
JM
5971 return;
5972 }
fbe73008 5973 if (ctx->opcode & 0x00008000) {
6527f6ea
AJ
5974 tcg_gen_ori_tl(cpu_msr, cpu_msr, (1 << MSR_EE));
5975 /* Stop translation to have a chance to raise an exception */
e06fcd75 5976 gen_stop_exception(ctx);
6527f6ea 5977 } else {
1b6e5f99 5978 tcg_gen_andi_tl(cpu_msr, cpu_msr, ~(1 << MSR_EE));
6527f6ea 5979 }
76a66253
JM
5980#endif
5981}
5982
08e46e54 5983/* PowerPC 440 specific instructions */
99e300ef 5984
54623277 5985/* dlmzb */
99e300ef 5986static void gen_dlmzb(DisasContext *ctx)
76a66253 5987{
ef0d51af
AJ
5988 TCGv_i32 t0 = tcg_const_i32(Rc(ctx->opcode));
5989 gen_helper_dlmzb(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)],
5990 cpu_gpr[rB(ctx->opcode)], t0);
5991 tcg_temp_free_i32(t0);
76a66253
JM
5992}
5993
5994/* mbar replaces eieio on 440 */
99e300ef 5995static void gen_mbar(DisasContext *ctx)
76a66253
JM
5996{
5997 /* interpreted as no-op */
5998}
5999
6000/* msync replaces sync on 440 */
99e300ef 6001static void gen_msync(DisasContext *ctx)
76a66253
JM
6002{
6003 /* interpreted as no-op */
6004}
6005
6006/* icbt */
e8eaa2c0 6007static void gen_icbt_440(DisasContext *ctx)
76a66253
JM
6008{
6009 /* interpreted as no-op */
6010 /* XXX: specification say this is treated as a load by the MMU
6011 * but does not generate any exception
6012 */
79aceca5
FB
6013}
6014
a9d9eb8f
JM
6015/*** Altivec vector extension ***/
6016/* Altivec registers moves */
a9d9eb8f 6017
564e571a
AJ
6018static always_inline TCGv_ptr gen_avr_ptr(int reg)
6019{
e4704b3b 6020 TCGv_ptr r = tcg_temp_new_ptr();
564e571a
AJ
6021 tcg_gen_addi_ptr(r, cpu_env, offsetof(CPUPPCState, avr[reg]));
6022 return r;
6023}
6024
a9d9eb8f 6025#define GEN_VR_LDX(name, opc2, opc3) \
99e300ef 6026static void glue(gen_, name)(DisasContext *ctx) \
a9d9eb8f 6027{ \
fe1e5c53 6028 TCGv EA; \
a9d9eb8f 6029 if (unlikely(!ctx->altivec_enabled)) { \
e06fcd75 6030 gen_exception(ctx, POWERPC_EXCP_VPU); \
a9d9eb8f
JM
6031 return; \
6032 } \
76db3ba4 6033 gen_set_access_type(ctx, ACCESS_INT); \
fe1e5c53 6034 EA = tcg_temp_new(); \
76db3ba4 6035 gen_addr_reg_index(ctx, EA); \
fe1e5c53 6036 tcg_gen_andi_tl(EA, EA, ~0xf); \
76db3ba4
AJ
6037 if (ctx->le_mode) { \
6038 gen_qemu_ld64(ctx, cpu_avrl[rD(ctx->opcode)], EA); \
fe1e5c53 6039 tcg_gen_addi_tl(EA, EA, 8); \
76db3ba4 6040 gen_qemu_ld64(ctx, cpu_avrh[rD(ctx->opcode)], EA); \
fe1e5c53 6041 } else { \
76db3ba4 6042 gen_qemu_ld64(ctx, cpu_avrh[rD(ctx->opcode)], EA); \
fe1e5c53 6043 tcg_gen_addi_tl(EA, EA, 8); \
76db3ba4 6044 gen_qemu_ld64(ctx, cpu_avrl[rD(ctx->opcode)], EA); \
fe1e5c53
AJ
6045 } \
6046 tcg_temp_free(EA); \
a9d9eb8f
JM
6047}
6048
6049#define GEN_VR_STX(name, opc2, opc3) \
99e300ef 6050static void gen_st##name(DisasContext *ctx) \
a9d9eb8f 6051{ \
fe1e5c53 6052 TCGv EA; \
a9d9eb8f 6053 if (unlikely(!ctx->altivec_enabled)) { \
e06fcd75 6054 gen_exception(ctx, POWERPC_EXCP_VPU); \
a9d9eb8f
JM
6055 return; \
6056 } \
76db3ba4 6057 gen_set_access_type(ctx, ACCESS_INT); \
fe1e5c53 6058 EA = tcg_temp_new(); \
76db3ba4 6059 gen_addr_reg_index(ctx, EA); \
fe1e5c53 6060 tcg_gen_andi_tl(EA, EA, ~0xf); \
76db3ba4
AJ
6061 if (ctx->le_mode) { \
6062 gen_qemu_st64(ctx, cpu_avrl[rD(ctx->opcode)], EA); \
fe1e5c53 6063 tcg_gen_addi_tl(EA, EA, 8); \
76db3ba4 6064 gen_qemu_st64(ctx, cpu_avrh[rD(ctx->opcode)], EA); \
fe1e5c53 6065 } else { \
76db3ba4 6066 gen_qemu_st64(ctx, cpu_avrh[rD(ctx->opcode)], EA); \
fe1e5c53 6067 tcg_gen_addi_tl(EA, EA, 8); \
76db3ba4 6068 gen_qemu_st64(ctx, cpu_avrl[rD(ctx->opcode)], EA); \
fe1e5c53
AJ
6069 } \
6070 tcg_temp_free(EA); \
a9d9eb8f
JM
6071}
6072
cbfb6ae9 6073#define GEN_VR_LVE(name, opc2, opc3) \
99e300ef 6074static void gen_lve##name(DisasContext *ctx) \
cbfb6ae9
AJ
6075 { \
6076 TCGv EA; \
6077 TCGv_ptr rs; \
6078 if (unlikely(!ctx->altivec_enabled)) { \
6079 gen_exception(ctx, POWERPC_EXCP_VPU); \
6080 return; \
6081 } \
6082 gen_set_access_type(ctx, ACCESS_INT); \
6083 EA = tcg_temp_new(); \
6084 gen_addr_reg_index(ctx, EA); \
6085 rs = gen_avr_ptr(rS(ctx->opcode)); \
6086 gen_helper_lve##name (rs, EA); \
6087 tcg_temp_free(EA); \
6088 tcg_temp_free_ptr(rs); \
6089 }
6090
6091#define GEN_VR_STVE(name, opc2, opc3) \
99e300ef 6092static void gen_stve##name(DisasContext *ctx) \
cbfb6ae9
AJ
6093 { \
6094 TCGv EA; \
6095 TCGv_ptr rs; \
6096 if (unlikely(!ctx->altivec_enabled)) { \
6097 gen_exception(ctx, POWERPC_EXCP_VPU); \
6098 return; \
6099 } \
6100 gen_set_access_type(ctx, ACCESS_INT); \
6101 EA = tcg_temp_new(); \
6102 gen_addr_reg_index(ctx, EA); \
6103 rs = gen_avr_ptr(rS(ctx->opcode)); \
6104 gen_helper_stve##name (rs, EA); \
6105 tcg_temp_free(EA); \
6106 tcg_temp_free_ptr(rs); \
6107 }
6108
fe1e5c53 6109GEN_VR_LDX(lvx, 0x07, 0x03);
a9d9eb8f 6110/* As we don't emulate the cache, lvxl is stricly equivalent to lvx */
fe1e5c53 6111GEN_VR_LDX(lvxl, 0x07, 0x0B);
a9d9eb8f 6112
cbfb6ae9
AJ
6113GEN_VR_LVE(bx, 0x07, 0x00);
6114GEN_VR_LVE(hx, 0x07, 0x01);
6115GEN_VR_LVE(wx, 0x07, 0x02);
6116
fe1e5c53 6117GEN_VR_STX(svx, 0x07, 0x07);
a9d9eb8f 6118/* As we don't emulate the cache, stvxl is stricly equivalent to stvx */
fe1e5c53 6119GEN_VR_STX(svxl, 0x07, 0x0F);
a9d9eb8f 6120
cbfb6ae9
AJ
6121GEN_VR_STVE(bx, 0x07, 0x04);
6122GEN_VR_STVE(hx, 0x07, 0x05);
6123GEN_VR_STVE(wx, 0x07, 0x06);
6124
99e300ef 6125static void gen_lvsl(DisasContext *ctx)
bf8d8ded
AJ
6126{
6127 TCGv_ptr rd;
6128 TCGv EA;
6129 if (unlikely(!ctx->altivec_enabled)) {
6130 gen_exception(ctx, POWERPC_EXCP_VPU);
6131 return;
6132 }
6133 EA = tcg_temp_new();
6134 gen_addr_reg_index(ctx, EA);
6135 rd = gen_avr_ptr(rD(ctx->opcode));
6136 gen_helper_lvsl(rd, EA);
6137 tcg_temp_free(EA);
6138 tcg_temp_free_ptr(rd);
6139}
6140
99e300ef 6141static void gen_lvsr(DisasContext *ctx)
bf8d8ded
AJ
6142{
6143 TCGv_ptr rd;
6144 TCGv EA;
6145 if (unlikely(!ctx->altivec_enabled)) {
6146 gen_exception(ctx, POWERPC_EXCP_VPU);
6147 return;
6148 }
6149 EA = tcg_temp_new();
6150 gen_addr_reg_index(ctx, EA);
6151 rd = gen_avr_ptr(rD(ctx->opcode));
6152 gen_helper_lvsr(rd, EA);
6153 tcg_temp_free(EA);
6154 tcg_temp_free_ptr(rd);
6155}
6156
99e300ef 6157static void gen_mfvscr(DisasContext *ctx)
785f451b
AJ
6158{
6159 TCGv_i32 t;
6160 if (unlikely(!ctx->altivec_enabled)) {
6161 gen_exception(ctx, POWERPC_EXCP_VPU);
6162 return;
6163 }
6164 tcg_gen_movi_i64(cpu_avrh[rD(ctx->opcode)], 0);
6165 t = tcg_temp_new_i32();
6166 tcg_gen_ld_i32(t, cpu_env, offsetof(CPUState, vscr));
6167 tcg_gen_extu_i32_i64(cpu_avrl[rD(ctx->opcode)], t);
fce5ecb7 6168 tcg_temp_free_i32(t);
785f451b
AJ
6169}
6170
99e300ef 6171static void gen_mtvscr(DisasContext *ctx)
785f451b 6172{
6e87b7c7 6173 TCGv_ptr p;
785f451b
AJ
6174 if (unlikely(!ctx->altivec_enabled)) {
6175 gen_exception(ctx, POWERPC_EXCP_VPU);
6176 return;
6177 }
6e87b7c7
AJ
6178 p = gen_avr_ptr(rD(ctx->opcode));
6179 gen_helper_mtvscr(p);
6180 tcg_temp_free_ptr(p);
785f451b
AJ
6181}
6182
7a9b96cf
AJ
6183/* Logical operations */
6184#define GEN_VX_LOGICAL(name, tcg_op, opc2, opc3) \
99e300ef 6185static void glue(gen_, name)(DisasContext *ctx) \
7a9b96cf
AJ
6186{ \
6187 if (unlikely(!ctx->altivec_enabled)) { \
6188 gen_exception(ctx, POWERPC_EXCP_VPU); \
6189 return; \
6190 } \
6191 tcg_op(cpu_avrh[rD(ctx->opcode)], cpu_avrh[rA(ctx->opcode)], cpu_avrh[rB(ctx->opcode)]); \
6192 tcg_op(cpu_avrl[rD(ctx->opcode)], cpu_avrl[rA(ctx->opcode)], cpu_avrl[rB(ctx->opcode)]); \
6193}
6194
6195GEN_VX_LOGICAL(vand, tcg_gen_and_i64, 2, 16);
6196GEN_VX_LOGICAL(vandc, tcg_gen_andc_i64, 2, 17);
6197GEN_VX_LOGICAL(vor, tcg_gen_or_i64, 2, 18);
6198GEN_VX_LOGICAL(vxor, tcg_gen_xor_i64, 2, 19);
6199GEN_VX_LOGICAL(vnor, tcg_gen_nor_i64, 2, 20);
6200
8e27dd6f 6201#define GEN_VXFORM(name, opc2, opc3) \
99e300ef 6202static void glue(gen_, name)(DisasContext *ctx) \
8e27dd6f
AJ
6203{ \
6204 TCGv_ptr ra, rb, rd; \
6205 if (unlikely(!ctx->altivec_enabled)) { \
6206 gen_exception(ctx, POWERPC_EXCP_VPU); \
6207 return; \
6208 } \
6209 ra = gen_avr_ptr(rA(ctx->opcode)); \
6210 rb = gen_avr_ptr(rB(ctx->opcode)); \
6211 rd = gen_avr_ptr(rD(ctx->opcode)); \
6212 gen_helper_##name (rd, ra, rb); \
6213 tcg_temp_free_ptr(ra); \
6214 tcg_temp_free_ptr(rb); \
6215 tcg_temp_free_ptr(rd); \
6216}
6217
7872c51c
AJ
6218GEN_VXFORM(vaddubm, 0, 0);
6219GEN_VXFORM(vadduhm, 0, 1);
6220GEN_VXFORM(vadduwm, 0, 2);
6221GEN_VXFORM(vsububm, 0, 16);
6222GEN_VXFORM(vsubuhm, 0, 17);
6223GEN_VXFORM(vsubuwm, 0, 18);
e4039339
AJ
6224GEN_VXFORM(vmaxub, 1, 0);
6225GEN_VXFORM(vmaxuh, 1, 1);
6226GEN_VXFORM(vmaxuw, 1, 2);
6227GEN_VXFORM(vmaxsb, 1, 4);
6228GEN_VXFORM(vmaxsh, 1, 5);
6229GEN_VXFORM(vmaxsw, 1, 6);
6230GEN_VXFORM(vminub, 1, 8);
6231GEN_VXFORM(vminuh, 1, 9);
6232GEN_VXFORM(vminuw, 1, 10);
6233GEN_VXFORM(vminsb, 1, 12);
6234GEN_VXFORM(vminsh, 1, 13);
6235GEN_VXFORM(vminsw, 1, 14);
fab3cbe9
AJ
6236GEN_VXFORM(vavgub, 1, 16);
6237GEN_VXFORM(vavguh, 1, 17);
6238GEN_VXFORM(vavguw, 1, 18);
6239GEN_VXFORM(vavgsb, 1, 20);
6240GEN_VXFORM(vavgsh, 1, 21);
6241GEN_VXFORM(vavgsw, 1, 22);
3b430048
AJ
6242GEN_VXFORM(vmrghb, 6, 0);
6243GEN_VXFORM(vmrghh, 6, 1);
6244GEN_VXFORM(vmrghw, 6, 2);
6245GEN_VXFORM(vmrglb, 6, 4);
6246GEN_VXFORM(vmrglh, 6, 5);
6247GEN_VXFORM(vmrglw, 6, 6);
2c277908
AJ
6248GEN_VXFORM(vmuloub, 4, 0);
6249GEN_VXFORM(vmulouh, 4, 1);
6250GEN_VXFORM(vmulosb, 4, 4);
6251GEN_VXFORM(vmulosh, 4, 5);
6252GEN_VXFORM(vmuleub, 4, 8);
6253GEN_VXFORM(vmuleuh, 4, 9);
6254GEN_VXFORM(vmulesb, 4, 12);
6255GEN_VXFORM(vmulesh, 4, 13);
d79f0809
AJ
6256GEN_VXFORM(vslb, 2, 4);
6257GEN_VXFORM(vslh, 2, 5);
6258GEN_VXFORM(vslw, 2, 6);
07ef34c3
AJ
6259GEN_VXFORM(vsrb, 2, 8);
6260GEN_VXFORM(vsrh, 2, 9);
6261GEN_VXFORM(vsrw, 2, 10);
6262GEN_VXFORM(vsrab, 2, 12);
6263GEN_VXFORM(vsrah, 2, 13);
6264GEN_VXFORM(vsraw, 2, 14);
7b239bec
AJ
6265GEN_VXFORM(vslo, 6, 16);
6266GEN_VXFORM(vsro, 6, 17);
e343da72
AJ
6267GEN_VXFORM(vaddcuw, 0, 6);
6268GEN_VXFORM(vsubcuw, 0, 22);
5ab09f33
AJ
6269GEN_VXFORM(vaddubs, 0, 8);
6270GEN_VXFORM(vadduhs, 0, 9);
6271GEN_VXFORM(vadduws, 0, 10);
6272GEN_VXFORM(vaddsbs, 0, 12);
6273GEN_VXFORM(vaddshs, 0, 13);
6274GEN_VXFORM(vaddsws, 0, 14);
6275GEN_VXFORM(vsububs, 0, 24);
6276GEN_VXFORM(vsubuhs, 0, 25);
6277GEN_VXFORM(vsubuws, 0, 26);
6278GEN_VXFORM(vsubsbs, 0, 28);
6279GEN_VXFORM(vsubshs, 0, 29);
6280GEN_VXFORM(vsubsws, 0, 30);
5e1d0985
AJ
6281GEN_VXFORM(vrlb, 2, 0);
6282GEN_VXFORM(vrlh, 2, 1);
6283GEN_VXFORM(vrlw, 2, 2);
d9430add
AJ
6284GEN_VXFORM(vsl, 2, 7);
6285GEN_VXFORM(vsr, 2, 11);
5335a145
AJ
6286GEN_VXFORM(vpkuhum, 7, 0);
6287GEN_VXFORM(vpkuwum, 7, 1);
6288GEN_VXFORM(vpkuhus, 7, 2);
6289GEN_VXFORM(vpkuwus, 7, 3);
6290GEN_VXFORM(vpkshus, 7, 4);
6291GEN_VXFORM(vpkswus, 7, 5);
6292GEN_VXFORM(vpkshss, 7, 6);
6293GEN_VXFORM(vpkswss, 7, 7);
1dd9ffb9 6294GEN_VXFORM(vpkpx, 7, 12);
8142cddd
AJ
6295GEN_VXFORM(vsum4ubs, 4, 24);
6296GEN_VXFORM(vsum4sbs, 4, 28);
6297GEN_VXFORM(vsum4shs, 4, 25);
6298GEN_VXFORM(vsum2sws, 4, 26);
6299GEN_VXFORM(vsumsws, 4, 30);
56fdd213
AJ
6300GEN_VXFORM(vaddfp, 5, 0);
6301GEN_VXFORM(vsubfp, 5, 1);
1536ff64
AJ
6302GEN_VXFORM(vmaxfp, 5, 16);
6303GEN_VXFORM(vminfp, 5, 17);
fab3cbe9 6304
0cbcd906 6305#define GEN_VXRFORM1(opname, name, str, opc2, opc3) \
e8eaa2c0 6306static void glue(gen_, name)(DisasContext *ctx) \
0cbcd906
AJ
6307 { \
6308 TCGv_ptr ra, rb, rd; \
6309 if (unlikely(!ctx->altivec_enabled)) { \
6310 gen_exception(ctx, POWERPC_EXCP_VPU); \
6311 return; \
6312 } \
6313 ra = gen_avr_ptr(rA(ctx->opcode)); \
6314 rb = gen_avr_ptr(rB(ctx->opcode)); \
6315 rd = gen_avr_ptr(rD(ctx->opcode)); \
6316 gen_helper_##opname (rd, ra, rb); \
6317 tcg_temp_free_ptr(ra); \
6318 tcg_temp_free_ptr(rb); \
6319 tcg_temp_free_ptr(rd); \
6320 }
6321
6322#define GEN_VXRFORM(name, opc2, opc3) \
6323 GEN_VXRFORM1(name, name, #name, opc2, opc3) \
6324 GEN_VXRFORM1(name##_dot, name##_, #name ".", opc2, (opc3 | (0x1 << 4)))
6325
1add6e23
AJ
6326GEN_VXRFORM(vcmpequb, 3, 0)
6327GEN_VXRFORM(vcmpequh, 3, 1)
6328GEN_VXRFORM(vcmpequw, 3, 2)
6329GEN_VXRFORM(vcmpgtsb, 3, 12)
6330GEN_VXRFORM(vcmpgtsh, 3, 13)
6331GEN_VXRFORM(vcmpgtsw, 3, 14)
6332GEN_VXRFORM(vcmpgtub, 3, 8)
6333GEN_VXRFORM(vcmpgtuh, 3, 9)
6334GEN_VXRFORM(vcmpgtuw, 3, 10)
819ca121
AJ
6335GEN_VXRFORM(vcmpeqfp, 3, 3)
6336GEN_VXRFORM(vcmpgefp, 3, 7)
6337GEN_VXRFORM(vcmpgtfp, 3, 11)
6338GEN_VXRFORM(vcmpbfp, 3, 15)
1add6e23 6339
c026766b 6340#define GEN_VXFORM_SIMM(name, opc2, opc3) \
99e300ef 6341static void glue(gen_, name)(DisasContext *ctx) \
c026766b
AJ
6342 { \
6343 TCGv_ptr rd; \
6344 TCGv_i32 simm; \
6345 if (unlikely(!ctx->altivec_enabled)) { \
6346 gen_exception(ctx, POWERPC_EXCP_VPU); \
6347 return; \
6348 } \
6349 simm = tcg_const_i32(SIMM5(ctx->opcode)); \
6350 rd = gen_avr_ptr(rD(ctx->opcode)); \
6351 gen_helper_##name (rd, simm); \
6352 tcg_temp_free_i32(simm); \
6353 tcg_temp_free_ptr(rd); \
6354 }
6355
6356GEN_VXFORM_SIMM(vspltisb, 6, 12);
6357GEN_VXFORM_SIMM(vspltish, 6, 13);
6358GEN_VXFORM_SIMM(vspltisw, 6, 14);
6359
de5f2484 6360#define GEN_VXFORM_NOA(name, opc2, opc3) \
99e300ef 6361static void glue(gen_, name)(DisasContext *ctx) \
de5f2484
AJ
6362 { \
6363 TCGv_ptr rb, rd; \
6364 if (unlikely(!ctx->altivec_enabled)) { \
6365 gen_exception(ctx, POWERPC_EXCP_VPU); \
6366 return; \
6367 } \
6368 rb = gen_avr_ptr(rB(ctx->opcode)); \
6369 rd = gen_avr_ptr(rD(ctx->opcode)); \
6370 gen_helper_##name (rd, rb); \
6371 tcg_temp_free_ptr(rb); \
6372 tcg_temp_free_ptr(rd); \
6373 }
6374
6cf1c6e5
AJ
6375GEN_VXFORM_NOA(vupkhsb, 7, 8);
6376GEN_VXFORM_NOA(vupkhsh, 7, 9);
6377GEN_VXFORM_NOA(vupklsb, 7, 10);
6378GEN_VXFORM_NOA(vupklsh, 7, 11);
79f85c3a
AJ
6379GEN_VXFORM_NOA(vupkhpx, 7, 13);
6380GEN_VXFORM_NOA(vupklpx, 7, 15);
bdfbac35 6381GEN_VXFORM_NOA(vrefp, 5, 4);
071fc3b1 6382GEN_VXFORM_NOA(vrsqrtefp, 5, 5);
b580763f 6383GEN_VXFORM_NOA(vlogefp, 5, 7);
f6b19645
AJ
6384GEN_VXFORM_NOA(vrfim, 5, 8);
6385GEN_VXFORM_NOA(vrfin, 5, 9);
6386GEN_VXFORM_NOA(vrfip, 5, 10);
6387GEN_VXFORM_NOA(vrfiz, 5, 11);
79f85c3a 6388
21d21583 6389#define GEN_VXFORM_SIMM(name, opc2, opc3) \
99e300ef 6390static void glue(gen_, name)(DisasContext *ctx) \
21d21583
AJ
6391 { \
6392 TCGv_ptr rd; \
6393 TCGv_i32 simm; \
6394 if (unlikely(!ctx->altivec_enabled)) { \
6395 gen_exception(ctx, POWERPC_EXCP_VPU); \
6396 return; \
6397 } \
6398 simm = tcg_const_i32(SIMM5(ctx->opcode)); \
6399 rd = gen_avr_ptr(rD(ctx->opcode)); \
6400 gen_helper_##name (rd, simm); \
6401 tcg_temp_free_i32(simm); \
6402 tcg_temp_free_ptr(rd); \
6403 }
6404
27a4edb3 6405#define GEN_VXFORM_UIMM(name, opc2, opc3) \
99e300ef 6406static void glue(gen_, name)(DisasContext *ctx) \
27a4edb3
AJ
6407 { \
6408 TCGv_ptr rb, rd; \
6409 TCGv_i32 uimm; \
6410 if (unlikely(!ctx->altivec_enabled)) { \
6411 gen_exception(ctx, POWERPC_EXCP_VPU); \
6412 return; \
6413 } \
6414 uimm = tcg_const_i32(UIMM5(ctx->opcode)); \
6415 rb = gen_avr_ptr(rB(ctx->opcode)); \
6416 rd = gen_avr_ptr(rD(ctx->opcode)); \
6417 gen_helper_##name (rd, rb, uimm); \
6418 tcg_temp_free_i32(uimm); \
6419 tcg_temp_free_ptr(rb); \
6420 tcg_temp_free_ptr(rd); \
6421 }
6422
e4e6bee7
AJ
6423GEN_VXFORM_UIMM(vspltb, 6, 8);
6424GEN_VXFORM_UIMM(vsplth, 6, 9);
6425GEN_VXFORM_UIMM(vspltw, 6, 10);
e140632e
AJ
6426GEN_VXFORM_UIMM(vcfux, 5, 12);
6427GEN_VXFORM_UIMM(vcfsx, 5, 13);
875b31db
AJ
6428GEN_VXFORM_UIMM(vctuxs, 5, 14);
6429GEN_VXFORM_UIMM(vctsxs, 5, 15);
e4e6bee7 6430
99e300ef 6431static void gen_vsldoi(DisasContext *ctx)
cd633b10
AJ
6432{
6433 TCGv_ptr ra, rb, rd;
fce5ecb7 6434 TCGv_i32 sh;
cd633b10
AJ
6435 if (unlikely(!ctx->altivec_enabled)) {
6436 gen_exception(ctx, POWERPC_EXCP_VPU);
6437 return;
6438 }
6439 ra = gen_avr_ptr(rA(ctx->opcode));
6440 rb = gen_avr_ptr(rB(ctx->opcode));
6441 rd = gen_avr_ptr(rD(ctx->opcode));
6442 sh = tcg_const_i32(VSH(ctx->opcode));
6443 gen_helper_vsldoi (rd, ra, rb, sh);
6444 tcg_temp_free_ptr(ra);
6445 tcg_temp_free_ptr(rb);
6446 tcg_temp_free_ptr(rd);
fce5ecb7 6447 tcg_temp_free_i32(sh);
cd633b10
AJ
6448}
6449
707cec33 6450#define GEN_VAFORM_PAIRED(name0, name1, opc2) \
99e300ef 6451static void glue(gen_, name0##_##name1)(DisasContext *ctx) \
707cec33
AJ
6452 { \
6453 TCGv_ptr ra, rb, rc, rd; \
6454 if (unlikely(!ctx->altivec_enabled)) { \
6455 gen_exception(ctx, POWERPC_EXCP_VPU); \
6456 return; \
6457 } \
6458 ra = gen_avr_ptr(rA(ctx->opcode)); \
6459 rb = gen_avr_ptr(rB(ctx->opcode)); \
6460 rc = gen_avr_ptr(rC(ctx->opcode)); \
6461 rd = gen_avr_ptr(rD(ctx->opcode)); \
6462 if (Rc(ctx->opcode)) { \
6463 gen_helper_##name1 (rd, ra, rb, rc); \
6464 } else { \
6465 gen_helper_##name0 (rd, ra, rb, rc); \
6466 } \
6467 tcg_temp_free_ptr(ra); \
6468 tcg_temp_free_ptr(rb); \
6469 tcg_temp_free_ptr(rc); \
6470 tcg_temp_free_ptr(rd); \
6471 }
6472
b161ae27
AJ
6473GEN_VAFORM_PAIRED(vmhaddshs, vmhraddshs, 16)
6474
99e300ef 6475static void gen_vmladduhm(DisasContext *ctx)
bcd2ee23
AJ
6476{
6477 TCGv_ptr ra, rb, rc, rd;
6478 if (unlikely(!ctx->altivec_enabled)) {
6479 gen_exception(ctx, POWERPC_EXCP_VPU);
6480 return;
6481 }
6482 ra = gen_avr_ptr(rA(ctx->opcode));
6483 rb = gen_avr_ptr(rB(ctx->opcode));
6484 rc = gen_avr_ptr(rC(ctx->opcode));
6485 rd = gen_avr_ptr(rD(ctx->opcode));
6486 gen_helper_vmladduhm(rd, ra, rb, rc);
6487 tcg_temp_free_ptr(ra);
6488 tcg_temp_free_ptr(rb);
6489 tcg_temp_free_ptr(rc);
6490 tcg_temp_free_ptr(rd);
6491}
6492
b04ae981 6493GEN_VAFORM_PAIRED(vmsumubm, vmsummbm, 18)
4d9903b6 6494GEN_VAFORM_PAIRED(vmsumuhm, vmsumuhs, 19)
eae07261 6495GEN_VAFORM_PAIRED(vmsumshm, vmsumshs, 20)
d1258698 6496GEN_VAFORM_PAIRED(vsel, vperm, 21)
35cf7c7e 6497GEN_VAFORM_PAIRED(vmaddfp, vnmsubfp, 23)
b04ae981 6498
0487d6a8 6499/*** SPE extension ***/
0487d6a8 6500/* Register moves */
3cd7d1dd 6501
a7812ae4 6502static always_inline void gen_load_gpr64(TCGv_i64 t, int reg) {
f78fb44e
AJ
6503#if defined(TARGET_PPC64)
6504 tcg_gen_mov_i64(t, cpu_gpr[reg]);
6505#else
36aa55dc 6506 tcg_gen_concat_i32_i64(t, cpu_gpr[reg], cpu_gprh[reg]);
3cd7d1dd 6507#endif
f78fb44e 6508}
3cd7d1dd 6509
a7812ae4 6510static always_inline void gen_store_gpr64(int reg, TCGv_i64 t) {
f78fb44e
AJ
6511#if defined(TARGET_PPC64)
6512 tcg_gen_mov_i64(cpu_gpr[reg], t);
6513#else
a7812ae4 6514 TCGv_i64 tmp = tcg_temp_new_i64();
f78fb44e 6515 tcg_gen_trunc_i64_i32(cpu_gpr[reg], t);
f78fb44e
AJ
6516 tcg_gen_shri_i64(tmp, t, 32);
6517 tcg_gen_trunc_i64_i32(cpu_gprh[reg], tmp);
a7812ae4 6518 tcg_temp_free_i64(tmp);
3cd7d1dd 6519#endif
f78fb44e 6520}
3cd7d1dd 6521
0487d6a8 6522#define GEN_SPE(name0, name1, opc2, opc3, inval, type) \
99e300ef 6523static void glue(gen_, name0##_##name1)(DisasContext *ctx) \
0487d6a8
JM
6524{ \
6525 if (Rc(ctx->opcode)) \
6526 gen_##name1(ctx); \
6527 else \
6528 gen_##name0(ctx); \
6529}
6530
6531/* Handler for undefined SPE opcodes */
b068d6a7 6532static always_inline void gen_speundef (DisasContext *ctx)
0487d6a8 6533{
e06fcd75 6534 gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL);
0487d6a8
JM
6535}
6536
57951c27
AJ
6537/* SPE logic */
6538#if defined(TARGET_PPC64)
6539#define GEN_SPEOP_LOGIC2(name, tcg_op) \
b068d6a7 6540static always_inline void gen_##name (DisasContext *ctx) \
0487d6a8
JM
6541{ \
6542 if (unlikely(!ctx->spe_enabled)) { \
e06fcd75 6543 gen_exception(ctx, POWERPC_EXCP_APU); \
0487d6a8
JM
6544 return; \
6545 } \
57951c27
AJ
6546 tcg_op(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)], \
6547 cpu_gpr[rB(ctx->opcode)]); \
6548}
6549#else
6550#define GEN_SPEOP_LOGIC2(name, tcg_op) \
6551static always_inline void gen_##name (DisasContext *ctx) \
6552{ \
6553 if (unlikely(!ctx->spe_enabled)) { \
e06fcd75 6554 gen_exception(ctx, POWERPC_EXCP_APU); \
57951c27
AJ
6555 return; \
6556 } \
6557 tcg_op(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)], \
6558 cpu_gpr[rB(ctx->opcode)]); \
6559 tcg_op(cpu_gprh[rD(ctx->opcode)], cpu_gprh[rA(ctx->opcode)], \
6560 cpu_gprh[rB(ctx->opcode)]); \
0487d6a8 6561}
57951c27
AJ
6562#endif
6563
6564GEN_SPEOP_LOGIC2(evand, tcg_gen_and_tl);
6565GEN_SPEOP_LOGIC2(evandc, tcg_gen_andc_tl);
6566GEN_SPEOP_LOGIC2(evxor, tcg_gen_xor_tl);
6567GEN_SPEOP_LOGIC2(evor, tcg_gen_or_tl);
6568GEN_SPEOP_LOGIC2(evnor, tcg_gen_nor_tl);
6569GEN_SPEOP_LOGIC2(eveqv, tcg_gen_eqv_tl);
6570GEN_SPEOP_LOGIC2(evorc, tcg_gen_orc_tl);
6571GEN_SPEOP_LOGIC2(evnand, tcg_gen_nand_tl);
0487d6a8 6572
57951c27
AJ
6573/* SPE logic immediate */
6574#if defined(TARGET_PPC64)
6575#define GEN_SPEOP_TCG_LOGIC_IMM2(name, tcg_opi) \
3d3a6a0a
AJ
6576static always_inline void gen_##name (DisasContext *ctx) \
6577{ \
6578 if (unlikely(!ctx->spe_enabled)) { \
e06fcd75 6579 gen_exception(ctx, POWERPC_EXCP_APU); \
3d3a6a0a
AJ
6580 return; \
6581 } \
a7812ae4
PB
6582 TCGv_i32 t0 = tcg_temp_local_new_i32(); \
6583 TCGv_i32 t1 = tcg_temp_local_new_i32(); \
6584 TCGv_i64 t2 = tcg_temp_local_new_i64(); \
57951c27
AJ
6585 tcg_gen_trunc_i64_i32(t0, cpu_gpr[rA(ctx->opcode)]); \
6586 tcg_opi(t0, t0, rB(ctx->opcode)); \
6587 tcg_gen_shri_i64(t2, cpu_gpr[rA(ctx->opcode)], 32); \
6588 tcg_gen_trunc_i64_i32(t1, t2); \
a7812ae4 6589 tcg_temp_free_i64(t2); \
57951c27
AJ
6590 tcg_opi(t1, t1, rB(ctx->opcode)); \
6591 tcg_gen_concat_i32_i64(cpu_gpr[rD(ctx->opcode)], t0, t1); \
a7812ae4
PB
6592 tcg_temp_free_i32(t0); \
6593 tcg_temp_free_i32(t1); \
3d3a6a0a 6594}
57951c27
AJ
6595#else
6596#define GEN_SPEOP_TCG_LOGIC_IMM2(name, tcg_opi) \
b068d6a7 6597static always_inline void gen_##name (DisasContext *ctx) \
0487d6a8
JM
6598{ \
6599 if (unlikely(!ctx->spe_enabled)) { \
e06fcd75 6600 gen_exception(ctx, POWERPC_EXCP_APU); \
0487d6a8
JM
6601 return; \
6602 } \
57951c27
AJ
6603 tcg_opi(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)], \
6604 rB(ctx->opcode)); \
6605 tcg_opi(cpu_gprh[rD(ctx->opcode)], cpu_gprh[rA(ctx->opcode)], \
6606 rB(ctx->opcode)); \
0487d6a8 6607}
57951c27
AJ
6608#endif
6609GEN_SPEOP_TCG_LOGIC_IMM2(evslwi, tcg_gen_shli_i32);
6610GEN_SPEOP_TCG_LOGIC_IMM2(evsrwiu, tcg_gen_shri_i32);
6611GEN_SPEOP_TCG_LOGIC_IMM2(evsrwis, tcg_gen_sari_i32);
6612GEN_SPEOP_TCG_LOGIC_IMM2(evrlwi, tcg_gen_rotli_i32);
0487d6a8 6613
57951c27
AJ
6614/* SPE arithmetic */
6615#if defined(TARGET_PPC64)
6616#define GEN_SPEOP_ARITH1(name, tcg_op) \
b068d6a7 6617static always_inline void gen_##name (DisasContext *ctx) \
0487d6a8
JM
6618{ \
6619 if (unlikely(!ctx->spe_enabled)) { \
e06fcd75 6620 gen_exception(ctx, POWERPC_EXCP_APU); \
0487d6a8
JM
6621 return; \
6622 } \
a7812ae4
PB
6623 TCGv_i32 t0 = tcg_temp_local_new_i32(); \
6624 TCGv_i32 t1 = tcg_temp_local_new_i32(); \
6625 TCGv_i64 t2 = tcg_temp_local_new_i64(); \
57951c27
AJ
6626 tcg_gen_trunc_i64_i32(t0, cpu_gpr[rA(ctx->opcode)]); \
6627 tcg_op(t0, t0); \
6628 tcg_gen_shri_i64(t2, cpu_gpr[rA(ctx->opcode)], 32); \
6629 tcg_gen_trunc_i64_i32(t1, t2); \
a7812ae4 6630 tcg_temp_free_i64(t2); \
57951c27
AJ
6631 tcg_op(t1, t1); \
6632 tcg_gen_concat_i32_i64(cpu_gpr[rD(ctx->opcode)], t0, t1); \
a7812ae4
PB
6633 tcg_temp_free_i32(t0); \
6634 tcg_temp_free_i32(t1); \
0487d6a8 6635}
57951c27 6636#else
a7812ae4 6637#define GEN_SPEOP_ARITH1(name, tcg_op) \
57951c27
AJ
6638static always_inline void gen_##name (DisasContext *ctx) \
6639{ \
6640 if (unlikely(!ctx->spe_enabled)) { \
e06fcd75 6641 gen_exception(ctx, POWERPC_EXCP_APU); \
57951c27
AJ
6642 return; \
6643 } \
6644 tcg_op(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)]); \
6645 tcg_op(cpu_gprh[rD(ctx->opcode)], cpu_gprh[rA(ctx->opcode)]); \
6646}
6647#endif
0487d6a8 6648
a7812ae4 6649static always_inline void gen_op_evabs (TCGv_i32 ret, TCGv_i32 arg1)
57951c27
AJ
6650{
6651 int l1 = gen_new_label();
6652 int l2 = gen_new_label();
0487d6a8 6653
57951c27
AJ
6654 tcg_gen_brcondi_i32(TCG_COND_GE, arg1, 0, l1);
6655 tcg_gen_neg_i32(ret, arg1);
6656 tcg_gen_br(l2);
6657 gen_set_label(l1);
a7812ae4 6658 tcg_gen_mov_i32(ret, arg1);
57951c27
AJ
6659 gen_set_label(l2);
6660}
6661GEN_SPEOP_ARITH1(evabs, gen_op_evabs);
6662GEN_SPEOP_ARITH1(evneg, tcg_gen_neg_i32);
6663GEN_SPEOP_ARITH1(evextsb, tcg_gen_ext8s_i32);
6664GEN_SPEOP_ARITH1(evextsh, tcg_gen_ext16s_i32);
a7812ae4 6665static always_inline void gen_op_evrndw (TCGv_i32 ret, TCGv_i32 arg1)
0487d6a8 6666{
57951c27
AJ
6667 tcg_gen_addi_i32(ret, arg1, 0x8000);
6668 tcg_gen_ext16u_i32(ret, ret);
6669}
6670GEN_SPEOP_ARITH1(evrndw, gen_op_evrndw);
a7812ae4
PB
6671GEN_SPEOP_ARITH1(evcntlsw, gen_helper_cntlsw32);
6672GEN_SPEOP_ARITH1(evcntlzw, gen_helper_cntlzw32);
0487d6a8 6673
57951c27
AJ
6674#if defined(TARGET_PPC64)
6675#define GEN_SPEOP_ARITH2(name, tcg_op) \
6676static always_inline void gen_##name (DisasContext *ctx) \
0487d6a8
JM
6677{ \
6678 if (unlikely(!ctx->spe_enabled)) { \
e06fcd75 6679 gen_exception(ctx, POWERPC_EXCP_APU); \
0487d6a8
JM
6680 return; \
6681 } \
a7812ae4
PB
6682 TCGv_i32 t0 = tcg_temp_local_new_i32(); \
6683 TCGv_i32 t1 = tcg_temp_local_new_i32(); \
6684 TCGv_i32 t2 = tcg_temp_local_new_i32(); \
501e23c4 6685 TCGv_i64 t3 = tcg_temp_local_new_i64(); \
57951c27
AJ
6686 tcg_gen_trunc_i64_i32(t0, cpu_gpr[rA(ctx->opcode)]); \
6687 tcg_gen_trunc_i64_i32(t2, cpu_gpr[rB(ctx->opcode)]); \
6688 tcg_op(t0, t0, t2); \
6689 tcg_gen_shri_i64(t3, cpu_gpr[rA(ctx->opcode)], 32); \
6690 tcg_gen_trunc_i64_i32(t1, t3); \
6691 tcg_gen_shri_i64(t3, cpu_gpr[rB(ctx->opcode)], 32); \
6692 tcg_gen_trunc_i64_i32(t2, t3); \
a7812ae4 6693 tcg_temp_free_i64(t3); \
57951c27 6694 tcg_op(t1, t1, t2); \
a7812ae4 6695 tcg_temp_free_i32(t2); \
57951c27 6696 tcg_gen_concat_i32_i64(cpu_gpr[rD(ctx->opcode)], t0, t1); \
a7812ae4
PB
6697 tcg_temp_free_i32(t0); \
6698 tcg_temp_free_i32(t1); \
0487d6a8 6699}
57951c27
AJ
6700#else
6701#define GEN_SPEOP_ARITH2(name, tcg_op) \
6702static always_inline void gen_##name (DisasContext *ctx) \
0487d6a8
JM
6703{ \
6704 if (unlikely(!ctx->spe_enabled)) { \
e06fcd75 6705 gen_exception(ctx, POWERPC_EXCP_APU); \
0487d6a8
JM
6706 return; \
6707 } \
57951c27
AJ
6708 tcg_op(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)], \
6709 cpu_gpr[rB(ctx->opcode)]); \
6710 tcg_op(cpu_gprh[rD(ctx->opcode)], cpu_gprh[rA(ctx->opcode)], \
6711 cpu_gprh[rB(ctx->opcode)]); \
0487d6a8 6712}
57951c27 6713#endif
0487d6a8 6714
a7812ae4 6715static always_inline void gen_op_evsrwu (TCGv_i32 ret, TCGv_i32 arg1, TCGv_i32 arg2)
57951c27 6716{
a7812ae4 6717 TCGv_i32 t0;
57951c27 6718 int l1, l2;
0487d6a8 6719
57951c27
AJ
6720 l1 = gen_new_label();
6721 l2 = gen_new_label();
a7812ae4 6722 t0 = tcg_temp_local_new_i32();
57951c27
AJ
6723 /* No error here: 6 bits are used */
6724 tcg_gen_andi_i32(t0, arg2, 0x3F);
6725 tcg_gen_brcondi_i32(TCG_COND_GE, t0, 32, l1);
6726 tcg_gen_shr_i32(ret, arg1, t0);
6727 tcg_gen_br(l2);
6728 gen_set_label(l1);
6729 tcg_gen_movi_i32(ret, 0);
6730 tcg_gen_br(l2);
a7812ae4 6731 tcg_temp_free_i32(t0);
57951c27
AJ
6732}
6733GEN_SPEOP_ARITH2(evsrwu, gen_op_evsrwu);
a7812ae4 6734static always_inline void gen_op_evsrws (TCGv_i32 ret, TCGv_i32 arg1, TCGv_i32 arg2)
57951c27 6735{
a7812ae4 6736 TCGv_i32 t0;
57951c27
AJ
6737 int l1, l2;
6738
6739 l1 = gen_new_label();
6740 l2 = gen_new_label();
a7812ae4 6741 t0 = tcg_temp_local_new_i32();
57951c27
AJ
6742 /* No error here: 6 bits are used */
6743 tcg_gen_andi_i32(t0, arg2, 0x3F);
6744 tcg_gen_brcondi_i32(TCG_COND_GE, t0, 32, l1);
6745 tcg_gen_sar_i32(ret, arg1, t0);
6746 tcg_gen_br(l2);
6747 gen_set_label(l1);
6748 tcg_gen_movi_i32(ret, 0);
6749 tcg_gen_br(l2);
a7812ae4 6750 tcg_temp_free_i32(t0);
57951c27
AJ
6751}
6752GEN_SPEOP_ARITH2(evsrws, gen_op_evsrws);
a7812ae4 6753static always_inline void gen_op_evslw (TCGv_i32 ret, TCGv_i32 arg1, TCGv_i32 arg2)
57951c27 6754{
a7812ae4 6755 TCGv_i32 t0;
57951c27
AJ
6756 int l1, l2;
6757
6758 l1 = gen_new_label();
6759 l2 = gen_new_label();
a7812ae4 6760 t0 = tcg_temp_local_new_i32();
57951c27
AJ
6761 /* No error here: 6 bits are used */
6762 tcg_gen_andi_i32(t0, arg2, 0x3F);
6763 tcg_gen_brcondi_i32(TCG_COND_GE, t0, 32, l1);
6764 tcg_gen_shl_i32(ret, arg1, t0);
6765 tcg_gen_br(l2);
6766 gen_set_label(l1);
6767 tcg_gen_movi_i32(ret, 0);
6768 tcg_gen_br(l2);
a7812ae4 6769 tcg_temp_free_i32(t0);
57951c27
AJ
6770}
6771GEN_SPEOP_ARITH2(evslw, gen_op_evslw);
a7812ae4 6772static always_inline void gen_op_evrlw (TCGv_i32 ret, TCGv_i32 arg1, TCGv_i32 arg2)
57951c27 6773{
a7812ae4 6774 TCGv_i32 t0 = tcg_temp_new_i32();
57951c27
AJ
6775 tcg_gen_andi_i32(t0, arg2, 0x1F);
6776 tcg_gen_rotl_i32(ret, arg1, t0);
a7812ae4 6777 tcg_temp_free_i32(t0);
57951c27
AJ
6778}
6779GEN_SPEOP_ARITH2(evrlw, gen_op_evrlw);
6780static always_inline void gen_evmergehi (DisasContext *ctx)
6781{
6782 if (unlikely(!ctx->spe_enabled)) {
e06fcd75 6783 gen_exception(ctx, POWERPC_EXCP_APU);
57951c27
AJ
6784 return;
6785 }
6786#if defined(TARGET_PPC64)
a7812ae4
PB
6787 TCGv t0 = tcg_temp_new();
6788 TCGv t1 = tcg_temp_new();
57951c27
AJ
6789 tcg_gen_shri_tl(t0, cpu_gpr[rB(ctx->opcode)], 32);
6790 tcg_gen_andi_tl(t1, cpu_gpr[rA(ctx->opcode)], 0xFFFFFFFF0000000ULL);
6791 tcg_gen_or_tl(cpu_gpr[rD(ctx->opcode)], t0, t1);
6792 tcg_temp_free(t0);
6793 tcg_temp_free(t1);
6794#else
6795 tcg_gen_mov_i32(cpu_gpr[rD(ctx->opcode)], cpu_gprh[rB(ctx->opcode)]);
6796 tcg_gen_mov_i32(cpu_gprh[rD(ctx->opcode)], cpu_gprh[rA(ctx->opcode)]);
6797#endif
6798}
6799GEN_SPEOP_ARITH2(evaddw, tcg_gen_add_i32);
a7812ae4 6800static always_inline void gen_op_evsubf (TCGv_i32 ret, TCGv_i32 arg1, TCGv_i32 arg2)
0487d6a8 6801{
57951c27
AJ
6802 tcg_gen_sub_i32(ret, arg2, arg1);
6803}
6804GEN_SPEOP_ARITH2(evsubfw, gen_op_evsubf);
0487d6a8 6805
57951c27
AJ
6806/* SPE arithmetic immediate */
6807#if defined(TARGET_PPC64)
6808#define GEN_SPEOP_ARITH_IMM2(name, tcg_op) \
6809static always_inline void gen_##name (DisasContext *ctx) \
6810{ \
6811 if (unlikely(!ctx->spe_enabled)) { \
e06fcd75 6812 gen_exception(ctx, POWERPC_EXCP_APU); \
57951c27
AJ
6813 return; \
6814 } \
a7812ae4
PB
6815 TCGv_i32 t0 = tcg_temp_local_new_i32(); \
6816 TCGv_i32 t1 = tcg_temp_local_new_i32(); \
6817 TCGv_i64 t2 = tcg_temp_local_new_i64(); \
57951c27
AJ
6818 tcg_gen_trunc_i64_i32(t0, cpu_gpr[rB(ctx->opcode)]); \
6819 tcg_op(t0, t0, rA(ctx->opcode)); \
6820 tcg_gen_shri_i64(t2, cpu_gpr[rB(ctx->opcode)], 32); \
6821 tcg_gen_trunc_i64_i32(t1, t2); \
e06fcd75 6822 tcg_temp_free_i64(t2); \
57951c27
AJ
6823 tcg_op(t1, t1, rA(ctx->opcode)); \
6824 tcg_gen_concat_i32_i64(cpu_gpr[rD(ctx->opcode)], t0, t1); \
a7812ae4
PB
6825 tcg_temp_free_i32(t0); \
6826 tcg_temp_free_i32(t1); \
57951c27
AJ
6827}
6828#else
6829#define GEN_SPEOP_ARITH_IMM2(name, tcg_op) \
6830static always_inline void gen_##name (DisasContext *ctx) \
6831{ \
6832 if (unlikely(!ctx->spe_enabled)) { \
e06fcd75 6833 gen_exception(ctx, POWERPC_EXCP_APU); \
57951c27
AJ
6834 return; \
6835 } \
6836 tcg_op(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rB(ctx->opcode)], \
6837 rA(ctx->opcode)); \
6838 tcg_op(cpu_gprh[rD(ctx->opcode)], cpu_gprh[rB(ctx->opcode)], \
6839 rA(ctx->opcode)); \
6840}
6841#endif
6842GEN_SPEOP_ARITH_IMM2(evaddiw, tcg_gen_addi_i32);
6843GEN_SPEOP_ARITH_IMM2(evsubifw, tcg_gen_subi_i32);
6844
6845/* SPE comparison */
6846#if defined(TARGET_PPC64)
6847#define GEN_SPEOP_COMP(name, tcg_cond) \
6848static always_inline void gen_##name (DisasContext *ctx) \
6849{ \
6850 if (unlikely(!ctx->spe_enabled)) { \
e06fcd75 6851 gen_exception(ctx, POWERPC_EXCP_APU); \
57951c27
AJ
6852 return; \
6853 } \
6854 int l1 = gen_new_label(); \
6855 int l2 = gen_new_label(); \
6856 int l3 = gen_new_label(); \
6857 int l4 = gen_new_label(); \
a7812ae4
PB
6858 TCGv_i32 t0 = tcg_temp_local_new_i32(); \
6859 TCGv_i32 t1 = tcg_temp_local_new_i32(); \
6860 TCGv_i64 t2 = tcg_temp_local_new_i64(); \
57951c27
AJ
6861 tcg_gen_trunc_i64_i32(t0, cpu_gpr[rA(ctx->opcode)]); \
6862 tcg_gen_trunc_i64_i32(t1, cpu_gpr[rB(ctx->opcode)]); \
6863 tcg_gen_brcond_i32(tcg_cond, t0, t1, l1); \
a7812ae4 6864 tcg_gen_movi_i32(cpu_crf[crfD(ctx->opcode)], 0); \
57951c27
AJ
6865 tcg_gen_br(l2); \
6866 gen_set_label(l1); \
6867 tcg_gen_movi_i32(cpu_crf[crfD(ctx->opcode)], \
6868 CRF_CL | CRF_CH_OR_CL | CRF_CH_AND_CL); \
6869 gen_set_label(l2); \
6870 tcg_gen_shri_i64(t2, cpu_gpr[rA(ctx->opcode)], 32); \
6871 tcg_gen_trunc_i64_i32(t0, t2); \
6872 tcg_gen_shri_i64(t2, cpu_gpr[rB(ctx->opcode)], 32); \
6873 tcg_gen_trunc_i64_i32(t1, t2); \
a7812ae4 6874 tcg_temp_free_i64(t2); \
57951c27
AJ
6875 tcg_gen_brcond_i32(tcg_cond, t0, t1, l3); \
6876 tcg_gen_andi_i32(cpu_crf[crfD(ctx->opcode)], cpu_crf[crfD(ctx->opcode)], \
6877 ~(CRF_CH | CRF_CH_AND_CL)); \
6878 tcg_gen_br(l4); \
6879 gen_set_label(l3); \
6880 tcg_gen_ori_i32(cpu_crf[crfD(ctx->opcode)], cpu_crf[crfD(ctx->opcode)], \
6881 CRF_CH | CRF_CH_OR_CL); \
6882 gen_set_label(l4); \
a7812ae4
PB
6883 tcg_temp_free_i32(t0); \
6884 tcg_temp_free_i32(t1); \
57951c27
AJ
6885}
6886#else
6887#define GEN_SPEOP_COMP(name, tcg_cond) \
6888static always_inline void gen_##name (DisasContext *ctx) \
6889{ \
6890 if (unlikely(!ctx->spe_enabled)) { \
e06fcd75 6891 gen_exception(ctx, POWERPC_EXCP_APU); \
57951c27
AJ
6892 return; \
6893 } \
6894 int l1 = gen_new_label(); \
6895 int l2 = gen_new_label(); \
6896 int l3 = gen_new_label(); \
6897 int l4 = gen_new_label(); \
6898 \
6899 tcg_gen_brcond_i32(tcg_cond, cpu_gpr[rA(ctx->opcode)], \
6900 cpu_gpr[rB(ctx->opcode)], l1); \
6901 tcg_gen_movi_tl(cpu_crf[crfD(ctx->opcode)], 0); \
6902 tcg_gen_br(l2); \
6903 gen_set_label(l1); \
6904 tcg_gen_movi_i32(cpu_crf[crfD(ctx->opcode)], \
6905 CRF_CL | CRF_CH_OR_CL | CRF_CH_AND_CL); \
6906 gen_set_label(l2); \
6907 tcg_gen_brcond_i32(tcg_cond, cpu_gprh[rA(ctx->opcode)], \
6908 cpu_gprh[rB(ctx->opcode)], l3); \
6909 tcg_gen_andi_i32(cpu_crf[crfD(ctx->opcode)], cpu_crf[crfD(ctx->opcode)], \
6910 ~(CRF_CH | CRF_CH_AND_CL)); \
6911 tcg_gen_br(l4); \
6912 gen_set_label(l3); \
6913 tcg_gen_ori_i32(cpu_crf[crfD(ctx->opcode)], cpu_crf[crfD(ctx->opcode)], \
6914 CRF_CH | CRF_CH_OR_CL); \
6915 gen_set_label(l4); \
6916}
6917#endif
6918GEN_SPEOP_COMP(evcmpgtu, TCG_COND_GTU);
6919GEN_SPEOP_COMP(evcmpgts, TCG_COND_GT);
6920GEN_SPEOP_COMP(evcmpltu, TCG_COND_LTU);
6921GEN_SPEOP_COMP(evcmplts, TCG_COND_LT);
6922GEN_SPEOP_COMP(evcmpeq, TCG_COND_EQ);
6923
6924/* SPE misc */
6925static always_inline void gen_brinc (DisasContext *ctx)
6926{
6927 /* Note: brinc is usable even if SPE is disabled */
a7812ae4
PB
6928 gen_helper_brinc(cpu_gpr[rD(ctx->opcode)],
6929 cpu_gpr[rA(ctx->opcode)], cpu_gpr[rB(ctx->opcode)]);
0487d6a8 6930}
57951c27
AJ
6931static always_inline void gen_evmergelo (DisasContext *ctx)
6932{
6933 if (unlikely(!ctx->spe_enabled)) {
e06fcd75 6934 gen_exception(ctx, POWERPC_EXCP_APU);
57951c27
AJ
6935 return;
6936 }
6937#if defined(TARGET_PPC64)
a7812ae4
PB
6938 TCGv t0 = tcg_temp_new();
6939 TCGv t1 = tcg_temp_new();
57951c27
AJ
6940 tcg_gen_andi_tl(t0, cpu_gpr[rB(ctx->opcode)], 0x00000000FFFFFFFFLL);
6941 tcg_gen_shli_tl(t1, cpu_gpr[rA(ctx->opcode)], 32);
6942 tcg_gen_or_tl(cpu_gpr[rD(ctx->opcode)], t0, t1);
6943 tcg_temp_free(t0);
6944 tcg_temp_free(t1);
6945#else
57951c27 6946 tcg_gen_mov_i32(cpu_gprh[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)]);
33890b3e 6947 tcg_gen_mov_i32(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rB(ctx->opcode)]);
57951c27
AJ
6948#endif
6949}
6950static always_inline void gen_evmergehilo (DisasContext *ctx)
6951{
6952 if (unlikely(!ctx->spe_enabled)) {
e06fcd75 6953 gen_exception(ctx, POWERPC_EXCP_APU);
57951c27
AJ
6954 return;
6955 }
6956#if defined(TARGET_PPC64)
a7812ae4
PB
6957 TCGv t0 = tcg_temp_new();
6958 TCGv t1 = tcg_temp_new();
57951c27
AJ
6959 tcg_gen_andi_tl(t0, cpu_gpr[rB(ctx->opcode)], 0x00000000FFFFFFFFLL);
6960 tcg_gen_andi_tl(t1, cpu_gpr[rA(ctx->opcode)], 0xFFFFFFFF0000000ULL);
6961 tcg_gen_or_tl(cpu_gpr[rD(ctx->opcode)], t0, t1);
6962 tcg_temp_free(t0);
6963 tcg_temp_free(t1);
6964#else
6965 tcg_gen_mov_i32(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rB(ctx->opcode)]);
6966 tcg_gen_mov_i32(cpu_gprh[rD(ctx->opcode)], cpu_gprh[rA(ctx->opcode)]);
6967#endif
6968}
6969static always_inline void gen_evmergelohi (DisasContext *ctx)
6970{
6971 if (unlikely(!ctx->spe_enabled)) {
e06fcd75 6972 gen_exception(ctx, POWERPC_EXCP_APU);
57951c27
AJ
6973 return;
6974 }
6975#if defined(TARGET_PPC64)
a7812ae4
PB
6976 TCGv t0 = tcg_temp_new();
6977 TCGv t1 = tcg_temp_new();
57951c27
AJ
6978 tcg_gen_shri_tl(t0, cpu_gpr[rB(ctx->opcode)], 32);
6979 tcg_gen_shli_tl(t1, cpu_gpr[rA(ctx->opcode)], 32);
6980 tcg_gen_or_tl(cpu_gpr[rD(ctx->opcode)], t0, t1);
6981 tcg_temp_free(t0);
6982 tcg_temp_free(t1);
6983#else
33890b3e
NF
6984 if (rD(ctx->opcode) == rA(ctx->opcode)) {
6985 TCGv_i32 tmp = tcg_temp_new_i32();
6986 tcg_gen_mov_i32(tmp, cpu_gpr[rA(ctx->opcode)]);
6987 tcg_gen_mov_i32(cpu_gpr[rD(ctx->opcode)], cpu_gprh[rB(ctx->opcode)]);
6988 tcg_gen_mov_i32(cpu_gprh[rD(ctx->opcode)], tmp);
6989 tcg_temp_free_i32(tmp);
6990 } else {
6991 tcg_gen_mov_i32(cpu_gpr[rD(ctx->opcode)], cpu_gprh[rB(ctx->opcode)]);
6992 tcg_gen_mov_i32(cpu_gprh[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)]);
6993 }
57951c27
AJ
6994#endif
6995}
6996static always_inline void gen_evsplati (DisasContext *ctx)
6997{
38d14952 6998 uint64_t imm = ((int32_t)(rA(ctx->opcode) << 11)) >> 27;
0487d6a8 6999
57951c27 7000#if defined(TARGET_PPC64)
38d14952 7001 tcg_gen_movi_tl(cpu_gpr[rD(ctx->opcode)], (imm << 32) | imm);
57951c27
AJ
7002#else
7003 tcg_gen_movi_i32(cpu_gpr[rD(ctx->opcode)], imm);
7004 tcg_gen_movi_i32(cpu_gprh[rD(ctx->opcode)], imm);
7005#endif
7006}
b068d6a7 7007static always_inline void gen_evsplatfi (DisasContext *ctx)
0487d6a8 7008{
38d14952 7009 uint64_t imm = rA(ctx->opcode) << 11;
0487d6a8 7010
57951c27 7011#if defined(TARGET_PPC64)
38d14952 7012 tcg_gen_movi_tl(cpu_gpr[rD(ctx->opcode)], (imm << 32) | imm);
57951c27
AJ
7013#else
7014 tcg_gen_movi_i32(cpu_gpr[rD(ctx->opcode)], imm);
7015 tcg_gen_movi_i32(cpu_gprh[rD(ctx->opcode)], imm);
7016#endif
0487d6a8
JM
7017}
7018
57951c27
AJ
7019static always_inline void gen_evsel (DisasContext *ctx)
7020{
7021 int l1 = gen_new_label();
7022 int l2 = gen_new_label();
7023 int l3 = gen_new_label();
7024 int l4 = gen_new_label();
a7812ae4 7025 TCGv_i32 t0 = tcg_temp_local_new_i32();
57951c27 7026#if defined(TARGET_PPC64)
a7812ae4
PB
7027 TCGv t1 = tcg_temp_local_new();
7028 TCGv t2 = tcg_temp_local_new();
57951c27
AJ
7029#endif
7030 tcg_gen_andi_i32(t0, cpu_crf[ctx->opcode & 0x07], 1 << 3);
7031 tcg_gen_brcondi_i32(TCG_COND_EQ, t0, 0, l1);
7032#if defined(TARGET_PPC64)
7033 tcg_gen_andi_tl(t1, cpu_gpr[rA(ctx->opcode)], 0xFFFFFFFF00000000ULL);
7034#else
7035 tcg_gen_mov_tl(cpu_gprh[rD(ctx->opcode)], cpu_gprh[rA(ctx->opcode)]);
7036#endif
7037 tcg_gen_br(l2);
7038 gen_set_label(l1);
7039#if defined(TARGET_PPC64)
7040 tcg_gen_andi_tl(t1, cpu_gpr[rB(ctx->opcode)], 0xFFFFFFFF00000000ULL);
7041#else
7042 tcg_gen_mov_tl(cpu_gprh[rD(ctx->opcode)], cpu_gprh[rB(ctx->opcode)]);
7043#endif
7044 gen_set_label(l2);
7045 tcg_gen_andi_i32(t0, cpu_crf[ctx->opcode & 0x07], 1 << 2);
7046 tcg_gen_brcondi_i32(TCG_COND_EQ, t0, 0, l3);
7047#if defined(TARGET_PPC64)
7048 tcg_gen_andi_tl(t2, cpu_gpr[rA(ctx->opcode)], 0x00000000FFFFFFFFULL);
7049#else
7050 tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)]);
7051#endif
7052 tcg_gen_br(l4);
7053 gen_set_label(l3);
7054#if defined(TARGET_PPC64)
7055 tcg_gen_andi_tl(t2, cpu_gpr[rB(ctx->opcode)], 0x00000000FFFFFFFFULL);
7056#else
7057 tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rB(ctx->opcode)]);
7058#endif
7059 gen_set_label(l4);
a7812ae4 7060 tcg_temp_free_i32(t0);
57951c27
AJ
7061#if defined(TARGET_PPC64)
7062 tcg_gen_or_tl(cpu_gpr[rD(ctx->opcode)], t1, t2);
7063 tcg_temp_free(t1);
7064 tcg_temp_free(t2);
7065#endif
7066}
e8eaa2c0
BS
7067
7068static void gen_evsel0(DisasContext *ctx)
57951c27
AJ
7069{
7070 gen_evsel(ctx);
7071}
e8eaa2c0
BS
7072
7073static void gen_evsel1(DisasContext *ctx)
57951c27
AJ
7074{
7075 gen_evsel(ctx);
7076}
e8eaa2c0
BS
7077
7078static void gen_evsel2(DisasContext *ctx)
57951c27
AJ
7079{
7080 gen_evsel(ctx);
7081}
e8eaa2c0
BS
7082
7083static void gen_evsel3(DisasContext *ctx)
57951c27
AJ
7084{
7085 gen_evsel(ctx);
7086}
0487d6a8
JM
7087
7088GEN_SPE(evaddw, speundef, 0x00, 0x08, 0x00000000, PPC_SPE); ////
7089GEN_SPE(evaddiw, speundef, 0x01, 0x08, 0x00000000, PPC_SPE);
7090GEN_SPE(evsubfw, speundef, 0x02, 0x08, 0x00000000, PPC_SPE); ////
7091GEN_SPE(evsubifw, speundef, 0x03, 0x08, 0x00000000, PPC_SPE);
7092GEN_SPE(evabs, evneg, 0x04, 0x08, 0x0000F800, PPC_SPE); ////
7093GEN_SPE(evextsb, evextsh, 0x05, 0x08, 0x0000F800, PPC_SPE); ////
7094GEN_SPE(evrndw, evcntlzw, 0x06, 0x08, 0x0000F800, PPC_SPE); ////
7095GEN_SPE(evcntlsw, brinc, 0x07, 0x08, 0x00000000, PPC_SPE); //
7096GEN_SPE(speundef, evand, 0x08, 0x08, 0x00000000, PPC_SPE); ////
7097GEN_SPE(evandc, speundef, 0x09, 0x08, 0x00000000, PPC_SPE); ////
7098GEN_SPE(evxor, evor, 0x0B, 0x08, 0x00000000, PPC_SPE); ////
7099GEN_SPE(evnor, eveqv, 0x0C, 0x08, 0x00000000, PPC_SPE); ////
7100GEN_SPE(speundef, evorc, 0x0D, 0x08, 0x00000000, PPC_SPE); ////
7101GEN_SPE(evnand, speundef, 0x0F, 0x08, 0x00000000, PPC_SPE); ////
7102GEN_SPE(evsrwu, evsrws, 0x10, 0x08, 0x00000000, PPC_SPE); ////
7103GEN_SPE(evsrwiu, evsrwis, 0x11, 0x08, 0x00000000, PPC_SPE);
7104GEN_SPE(evslw, speundef, 0x12, 0x08, 0x00000000, PPC_SPE); ////
7105GEN_SPE(evslwi, speundef, 0x13, 0x08, 0x00000000, PPC_SPE);
7106GEN_SPE(evrlw, evsplati, 0x14, 0x08, 0x00000000, PPC_SPE); //
7107GEN_SPE(evrlwi, evsplatfi, 0x15, 0x08, 0x00000000, PPC_SPE);
7108GEN_SPE(evmergehi, evmergelo, 0x16, 0x08, 0x00000000, PPC_SPE); ////
7109GEN_SPE(evmergehilo, evmergelohi, 0x17, 0x08, 0x00000000, PPC_SPE); ////
7110GEN_SPE(evcmpgtu, evcmpgts, 0x18, 0x08, 0x00600000, PPC_SPE); ////
7111GEN_SPE(evcmpltu, evcmplts, 0x19, 0x08, 0x00600000, PPC_SPE); ////
7112GEN_SPE(evcmpeq, speundef, 0x1A, 0x08, 0x00600000, PPC_SPE); ////
7113
6a6ae23f 7114/* SPE load and stores */
76db3ba4 7115static always_inline void gen_addr_spe_imm_index (DisasContext *ctx, TCGv EA, int sh)
6a6ae23f
AJ
7116{
7117 target_ulong uimm = rB(ctx->opcode);
7118
76db3ba4 7119 if (rA(ctx->opcode) == 0) {
6a6ae23f 7120 tcg_gen_movi_tl(EA, uimm << sh);
76db3ba4 7121 } else {
6a6ae23f 7122 tcg_gen_addi_tl(EA, cpu_gpr[rA(ctx->opcode)], uimm << sh);
76db3ba4
AJ
7123#if defined(TARGET_PPC64)
7124 if (!ctx->sf_mode) {
7125 tcg_gen_ext32u_tl(EA, EA);
7126 }
7127#endif
7128 }
0487d6a8 7129}
6a6ae23f
AJ
7130
7131static always_inline void gen_op_evldd(DisasContext *ctx, TCGv addr)
7132{
7133#if defined(TARGET_PPC64)
76db3ba4 7134 gen_qemu_ld64(ctx, cpu_gpr[rD(ctx->opcode)], addr);
6a6ae23f
AJ
7135#else
7136 TCGv_i64 t0 = tcg_temp_new_i64();
76db3ba4 7137 gen_qemu_ld64(ctx, t0, addr);
6a6ae23f
AJ
7138 tcg_gen_trunc_i64_i32(cpu_gpr[rD(ctx->opcode)], t0);
7139 tcg_gen_shri_i64(t0, t0, 32);
7140 tcg_gen_trunc_i64_i32(cpu_gprh[rD(ctx->opcode)], t0);
7141 tcg_temp_free_i64(t0);
7142#endif
0487d6a8 7143}
6a6ae23f
AJ
7144
7145static always_inline void gen_op_evldw(DisasContext *ctx, TCGv addr)
7146{
0487d6a8 7147#if defined(TARGET_PPC64)
6a6ae23f 7148 TCGv t0 = tcg_temp_new();
76db3ba4 7149 gen_qemu_ld32u(ctx, t0, addr);
6a6ae23f 7150 tcg_gen_shli_tl(cpu_gpr[rD(ctx->opcode)], t0, 32);
76db3ba4
AJ
7151 gen_addr_add(ctx, addr, addr, 4);
7152 gen_qemu_ld32u(ctx, t0, addr);
6a6ae23f
AJ
7153 tcg_gen_or_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rD(ctx->opcode)], t0);
7154 tcg_temp_free(t0);
7155#else
76db3ba4
AJ
7156 gen_qemu_ld32u(ctx, cpu_gprh[rD(ctx->opcode)], addr);
7157 gen_addr_add(ctx, addr, addr, 4);
7158 gen_qemu_ld32u(ctx, cpu_gpr[rD(ctx->opcode)], addr);
6a6ae23f 7159#endif
0487d6a8 7160}
6a6ae23f
AJ
7161
7162static always_inline void gen_op_evldh(DisasContext *ctx, TCGv addr)
7163{
7164 TCGv t0 = tcg_temp_new();
7165#if defined(TARGET_PPC64)
76db3ba4 7166 gen_qemu_ld16u(ctx, t0, addr);
6a6ae23f 7167 tcg_gen_shli_tl(cpu_gpr[rD(ctx->opcode)], t0, 48);
76db3ba4
AJ
7168 gen_addr_add(ctx, addr, addr, 2);
7169 gen_qemu_ld16u(ctx, t0, addr);
6a6ae23f
AJ
7170 tcg_gen_shli_tl(t0, t0, 32);
7171 tcg_gen_or_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rD(ctx->opcode)], t0);
76db3ba4
AJ
7172 gen_addr_add(ctx, addr, addr, 2);
7173 gen_qemu_ld16u(ctx, t0, addr);
6a6ae23f
AJ
7174 tcg_gen_shli_tl(t0, t0, 16);
7175 tcg_gen_or_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rD(ctx->opcode)], t0);
76db3ba4
AJ
7176 gen_addr_add(ctx, addr, addr, 2);
7177 gen_qemu_ld16u(ctx, t0, addr);
6a6ae23f 7178 tcg_gen_or_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rD(ctx->opcode)], t0);
0487d6a8 7179#else
76db3ba4 7180 gen_qemu_ld16u(ctx, t0, addr);
6a6ae23f 7181 tcg_gen_shli_tl(cpu_gprh[rD(ctx->opcode)], t0, 16);
76db3ba4
AJ
7182 gen_addr_add(ctx, addr, addr, 2);
7183 gen_qemu_ld16u(ctx, t0, addr);
6a6ae23f 7184 tcg_gen_or_tl(cpu_gprh[rD(ctx->opcode)], cpu_gprh[rD(ctx->opcode)], t0);
76db3ba4
AJ
7185 gen_addr_add(ctx, addr, addr, 2);
7186 gen_qemu_ld16u(ctx, t0, addr);
6a6ae23f 7187 tcg_gen_shli_tl(cpu_gprh[rD(ctx->opcode)], t0, 16);
76db3ba4
AJ
7188 gen_addr_add(ctx, addr, addr, 2);
7189 gen_qemu_ld16u(ctx, t0, addr);
6a6ae23f 7190 tcg_gen_or_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rD(ctx->opcode)], t0);
0487d6a8 7191#endif
6a6ae23f 7192 tcg_temp_free(t0);
0487d6a8
JM
7193}
7194
6a6ae23f
AJ
7195static always_inline void gen_op_evlhhesplat(DisasContext *ctx, TCGv addr)
7196{
7197 TCGv t0 = tcg_temp_new();
76db3ba4 7198 gen_qemu_ld16u(ctx, t0, addr);
6a6ae23f
AJ
7199#if defined(TARGET_PPC64)
7200 tcg_gen_shli_tl(cpu_gpr[rD(ctx->opcode)], t0, 48);
7201 tcg_gen_shli_tl(t0, t0, 16);
7202 tcg_gen_or_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rD(ctx->opcode)], t0);
7203#else
7204 tcg_gen_shli_tl(t0, t0, 16);
7205 tcg_gen_mov_tl(cpu_gprh[rD(ctx->opcode)], t0);
7206 tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], t0);
7207#endif
7208 tcg_temp_free(t0);
0487d6a8
JM
7209}
7210
6a6ae23f
AJ
7211static always_inline void gen_op_evlhhousplat(DisasContext *ctx, TCGv addr)
7212{
7213 TCGv t0 = tcg_temp_new();
76db3ba4 7214 gen_qemu_ld16u(ctx, t0, addr);
6a6ae23f
AJ
7215#if defined(TARGET_PPC64)
7216 tcg_gen_shli_tl(cpu_gpr[rD(ctx->opcode)], t0, 32);
7217 tcg_gen_or_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rD(ctx->opcode)], t0);
7218#else
7219 tcg_gen_mov_tl(cpu_gprh[rD(ctx->opcode)], t0);
7220 tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], t0);
7221#endif
7222 tcg_temp_free(t0);
0487d6a8
JM
7223}
7224
6a6ae23f
AJ
7225static always_inline void gen_op_evlhhossplat(DisasContext *ctx, TCGv addr)
7226{
7227 TCGv t0 = tcg_temp_new();
76db3ba4 7228 gen_qemu_ld16s(ctx, t0, addr);
6a6ae23f
AJ
7229#if defined(TARGET_PPC64)
7230 tcg_gen_shli_tl(cpu_gpr[rD(ctx->opcode)], t0, 32);
7231 tcg_gen_ext32u_tl(t0, t0);
7232 tcg_gen_or_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rD(ctx->opcode)], t0);
7233#else
7234 tcg_gen_mov_tl(cpu_gprh[rD(ctx->opcode)], t0);
7235 tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], t0);
7236#endif
7237 tcg_temp_free(t0);
7238}
7239
7240static always_inline void gen_op_evlwhe(DisasContext *ctx, TCGv addr)
7241{
7242 TCGv t0 = tcg_temp_new();
7243#if defined(TARGET_PPC64)
76db3ba4 7244 gen_qemu_ld16u(ctx, t0, addr);
6a6ae23f 7245 tcg_gen_shli_tl(cpu_gpr[rD(ctx->opcode)], t0, 48);
76db3ba4
AJ
7246 gen_addr_add(ctx, addr, addr, 2);
7247 gen_qemu_ld16u(ctx, t0, addr);
6a6ae23f
AJ
7248 tcg_gen_shli_tl(t0, t0, 16);
7249 tcg_gen_or_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rD(ctx->opcode)], t0);
7250#else
76db3ba4 7251 gen_qemu_ld16u(ctx, t0, addr);
6a6ae23f 7252 tcg_gen_shli_tl(cpu_gprh[rD(ctx->opcode)], t0, 16);
76db3ba4
AJ
7253 gen_addr_add(ctx, addr, addr, 2);
7254 gen_qemu_ld16u(ctx, t0, addr);
6a6ae23f
AJ
7255 tcg_gen_shli_tl(cpu_gpr[rD(ctx->opcode)], t0, 16);
7256#endif
7257 tcg_temp_free(t0);
7258}
7259
7260static always_inline void gen_op_evlwhou(DisasContext *ctx, TCGv addr)
7261{
7262#if defined(TARGET_PPC64)
7263 TCGv t0 = tcg_temp_new();
76db3ba4
AJ
7264 gen_qemu_ld16u(ctx, cpu_gpr[rD(ctx->opcode)], addr);
7265 gen_addr_add(ctx, addr, addr, 2);
7266 gen_qemu_ld16u(ctx, t0, addr);
6a6ae23f
AJ
7267 tcg_gen_shli_tl(t0, t0, 32);
7268 tcg_gen_or_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rD(ctx->opcode)], t0);
7269 tcg_temp_free(t0);
7270#else
76db3ba4
AJ
7271 gen_qemu_ld16u(ctx, cpu_gprh[rD(ctx->opcode)], addr);
7272 gen_addr_add(ctx, addr, addr, 2);
7273 gen_qemu_ld16u(ctx, cpu_gpr[rD(ctx->opcode)], addr);
6a6ae23f
AJ
7274#endif
7275}
7276
7277static always_inline void gen_op_evlwhos(DisasContext *ctx, TCGv addr)
7278{
7279#if defined(TARGET_PPC64)
7280 TCGv t0 = tcg_temp_new();
76db3ba4 7281 gen_qemu_ld16s(ctx, t0, addr);
6a6ae23f 7282 tcg_gen_ext32u_tl(cpu_gpr[rD(ctx->opcode)], t0);
76db3ba4
AJ
7283 gen_addr_add(ctx, addr, addr, 2);
7284 gen_qemu_ld16s(ctx, t0, addr);
6a6ae23f
AJ
7285 tcg_gen_shli_tl(t0, t0, 32);
7286 tcg_gen_or_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rD(ctx->opcode)], t0);
7287 tcg_temp_free(t0);
7288#else
76db3ba4
AJ
7289 gen_qemu_ld16s(ctx, cpu_gprh[rD(ctx->opcode)], addr);
7290 gen_addr_add(ctx, addr, addr, 2);
7291 gen_qemu_ld16s(ctx, cpu_gpr[rD(ctx->opcode)], addr);
6a6ae23f
AJ
7292#endif
7293}
7294
7295static always_inline void gen_op_evlwwsplat(DisasContext *ctx, TCGv addr)
7296{
7297 TCGv t0 = tcg_temp_new();
76db3ba4 7298 gen_qemu_ld32u(ctx, t0, addr);
0487d6a8 7299#if defined(TARGET_PPC64)
6a6ae23f
AJ
7300 tcg_gen_shli_tl(cpu_gpr[rD(ctx->opcode)], t0, 32);
7301 tcg_gen_or_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rD(ctx->opcode)], t0);
7302#else
7303 tcg_gen_mov_tl(cpu_gprh[rD(ctx->opcode)], t0);
7304 tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], t0);
7305#endif
7306 tcg_temp_free(t0);
7307}
7308
7309static always_inline void gen_op_evlwhsplat(DisasContext *ctx, TCGv addr)
7310{
7311 TCGv t0 = tcg_temp_new();
7312#if defined(TARGET_PPC64)
76db3ba4 7313 gen_qemu_ld16u(ctx, t0, addr);
6a6ae23f
AJ
7314 tcg_gen_shli_tl(cpu_gpr[rD(ctx->opcode)], t0, 48);
7315 tcg_gen_shli_tl(t0, t0, 32);
7316 tcg_gen_or_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rD(ctx->opcode)], t0);
76db3ba4
AJ
7317 gen_addr_add(ctx, addr, addr, 2);
7318 gen_qemu_ld16u(ctx, t0, addr);
6a6ae23f
AJ
7319 tcg_gen_or_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rD(ctx->opcode)], t0);
7320 tcg_gen_shli_tl(t0, t0, 16);
7321 tcg_gen_or_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rD(ctx->opcode)], t0);
7322#else
76db3ba4 7323 gen_qemu_ld16u(ctx, t0, addr);
6a6ae23f
AJ
7324 tcg_gen_shli_tl(cpu_gprh[rD(ctx->opcode)], t0, 16);
7325 tcg_gen_or_tl(cpu_gprh[rD(ctx->opcode)], cpu_gprh[rD(ctx->opcode)], t0);
76db3ba4
AJ
7326 gen_addr_add(ctx, addr, addr, 2);
7327 gen_qemu_ld16u(ctx, t0, addr);
6a6ae23f
AJ
7328 tcg_gen_shli_tl(cpu_gpr[rD(ctx->opcode)], t0, 16);
7329 tcg_gen_or_tl(cpu_gpr[rD(ctx->opcode)], cpu_gprh[rD(ctx->opcode)], t0);
0487d6a8 7330#endif
6a6ae23f
AJ
7331 tcg_temp_free(t0);
7332}
7333
7334static always_inline void gen_op_evstdd(DisasContext *ctx, TCGv addr)
7335{
7336#if defined(TARGET_PPC64)
76db3ba4 7337 gen_qemu_st64(ctx, cpu_gpr[rS(ctx->opcode)], addr);
0487d6a8 7338#else
6a6ae23f
AJ
7339 TCGv_i64 t0 = tcg_temp_new_i64();
7340 tcg_gen_concat_i32_i64(t0, cpu_gpr[rS(ctx->opcode)], cpu_gprh[rS(ctx->opcode)]);
76db3ba4 7341 gen_qemu_st64(ctx, t0, addr);
6a6ae23f
AJ
7342 tcg_temp_free_i64(t0);
7343#endif
7344}
7345
7346static always_inline void gen_op_evstdw(DisasContext *ctx, TCGv addr)
7347{
0487d6a8 7348#if defined(TARGET_PPC64)
6a6ae23f
AJ
7349 TCGv t0 = tcg_temp_new();
7350 tcg_gen_shri_tl(t0, cpu_gpr[rS(ctx->opcode)], 32);
76db3ba4 7351 gen_qemu_st32(ctx, t0, addr);
6a6ae23f
AJ
7352 tcg_temp_free(t0);
7353#else
76db3ba4 7354 gen_qemu_st32(ctx, cpu_gprh[rS(ctx->opcode)], addr);
6a6ae23f 7355#endif
76db3ba4
AJ
7356 gen_addr_add(ctx, addr, addr, 4);
7357 gen_qemu_st32(ctx, cpu_gpr[rS(ctx->opcode)], addr);
6a6ae23f
AJ
7358}
7359
7360static always_inline void gen_op_evstdh(DisasContext *ctx, TCGv addr)
7361{
7362 TCGv t0 = tcg_temp_new();
7363#if defined(TARGET_PPC64)
7364 tcg_gen_shri_tl(t0, cpu_gpr[rS(ctx->opcode)], 48);
7365#else
7366 tcg_gen_shri_tl(t0, cpu_gprh[rS(ctx->opcode)], 16);
7367#endif
76db3ba4
AJ
7368 gen_qemu_st16(ctx, t0, addr);
7369 gen_addr_add(ctx, addr, addr, 2);
6a6ae23f
AJ
7370#if defined(TARGET_PPC64)
7371 tcg_gen_shri_tl(t0, cpu_gpr[rS(ctx->opcode)], 32);
76db3ba4 7372 gen_qemu_st16(ctx, t0, addr);
6a6ae23f 7373#else
76db3ba4 7374 gen_qemu_st16(ctx, cpu_gprh[rS(ctx->opcode)], addr);
6a6ae23f 7375#endif
76db3ba4 7376 gen_addr_add(ctx, addr, addr, 2);
6a6ae23f 7377 tcg_gen_shri_tl(t0, cpu_gpr[rS(ctx->opcode)], 16);
76db3ba4 7378 gen_qemu_st16(ctx, t0, addr);
6a6ae23f 7379 tcg_temp_free(t0);
76db3ba4
AJ
7380 gen_addr_add(ctx, addr, addr, 2);
7381 gen_qemu_st16(ctx, cpu_gpr[rS(ctx->opcode)], addr);
6a6ae23f
AJ
7382}
7383
7384static always_inline void gen_op_evstwhe(DisasContext *ctx, TCGv addr)
7385{
7386 TCGv t0 = tcg_temp_new();
7387#if defined(TARGET_PPC64)
7388 tcg_gen_shri_tl(t0, cpu_gpr[rS(ctx->opcode)], 48);
7389#else
7390 tcg_gen_shri_tl(t0, cpu_gprh[rS(ctx->opcode)], 16);
7391#endif
76db3ba4
AJ
7392 gen_qemu_st16(ctx, t0, addr);
7393 gen_addr_add(ctx, addr, addr, 2);
6a6ae23f 7394 tcg_gen_shri_tl(t0, cpu_gpr[rS(ctx->opcode)], 16);
76db3ba4 7395 gen_qemu_st16(ctx, t0, addr);
6a6ae23f
AJ
7396 tcg_temp_free(t0);
7397}
7398
7399static always_inline void gen_op_evstwho(DisasContext *ctx, TCGv addr)
7400{
7401#if defined(TARGET_PPC64)
7402 TCGv t0 = tcg_temp_new();
7403 tcg_gen_shri_tl(t0, cpu_gpr[rS(ctx->opcode)], 32);
76db3ba4 7404 gen_qemu_st16(ctx, t0, addr);
6a6ae23f
AJ
7405 tcg_temp_free(t0);
7406#else
76db3ba4 7407 gen_qemu_st16(ctx, cpu_gprh[rS(ctx->opcode)], addr);
6a6ae23f 7408#endif
76db3ba4
AJ
7409 gen_addr_add(ctx, addr, addr, 2);
7410 gen_qemu_st16(ctx, cpu_gpr[rS(ctx->opcode)], addr);
6a6ae23f
AJ
7411}
7412
7413static always_inline void gen_op_evstwwe(DisasContext *ctx, TCGv addr)
7414{
7415#if defined(TARGET_PPC64)
7416 TCGv t0 = tcg_temp_new();
7417 tcg_gen_shri_tl(t0, cpu_gpr[rS(ctx->opcode)], 32);
76db3ba4 7418 gen_qemu_st32(ctx, t0, addr);
6a6ae23f
AJ
7419 tcg_temp_free(t0);
7420#else
76db3ba4 7421 gen_qemu_st32(ctx, cpu_gprh[rS(ctx->opcode)], addr);
6a6ae23f
AJ
7422#endif
7423}
7424
7425static always_inline void gen_op_evstwwo(DisasContext *ctx, TCGv addr)
7426{
76db3ba4 7427 gen_qemu_st32(ctx, cpu_gpr[rS(ctx->opcode)], addr);
6a6ae23f
AJ
7428}
7429
7430#define GEN_SPEOP_LDST(name, opc2, sh) \
99e300ef 7431static void glue(gen_, name)(DisasContext *ctx) \
6a6ae23f
AJ
7432{ \
7433 TCGv t0; \
7434 if (unlikely(!ctx->spe_enabled)) { \
e06fcd75 7435 gen_exception(ctx, POWERPC_EXCP_APU); \
6a6ae23f
AJ
7436 return; \
7437 } \
76db3ba4 7438 gen_set_access_type(ctx, ACCESS_INT); \
6a6ae23f
AJ
7439 t0 = tcg_temp_new(); \
7440 if (Rc(ctx->opcode)) { \
76db3ba4 7441 gen_addr_spe_imm_index(ctx, t0, sh); \
6a6ae23f 7442 } else { \
76db3ba4 7443 gen_addr_reg_index(ctx, t0); \
6a6ae23f
AJ
7444 } \
7445 gen_op_##name(ctx, t0); \
7446 tcg_temp_free(t0); \
7447}
7448
7449GEN_SPEOP_LDST(evldd, 0x00, 3);
7450GEN_SPEOP_LDST(evldw, 0x01, 3);
7451GEN_SPEOP_LDST(evldh, 0x02, 3);
7452GEN_SPEOP_LDST(evlhhesplat, 0x04, 1);
7453GEN_SPEOP_LDST(evlhhousplat, 0x06, 1);
7454GEN_SPEOP_LDST(evlhhossplat, 0x07, 1);
7455GEN_SPEOP_LDST(evlwhe, 0x08, 2);
7456GEN_SPEOP_LDST(evlwhou, 0x0A, 2);
7457GEN_SPEOP_LDST(evlwhos, 0x0B, 2);
7458GEN_SPEOP_LDST(evlwwsplat, 0x0C, 2);
7459GEN_SPEOP_LDST(evlwhsplat, 0x0E, 2);
7460
7461GEN_SPEOP_LDST(evstdd, 0x10, 3);
7462GEN_SPEOP_LDST(evstdw, 0x11, 3);
7463GEN_SPEOP_LDST(evstdh, 0x12, 3);
7464GEN_SPEOP_LDST(evstwhe, 0x18, 2);
7465GEN_SPEOP_LDST(evstwho, 0x1A, 2);
7466GEN_SPEOP_LDST(evstwwe, 0x1C, 2);
7467GEN_SPEOP_LDST(evstwwo, 0x1E, 2);
0487d6a8
JM
7468
7469/* Multiply and add - TODO */
7470#if 0
7471GEN_SPE(speundef, evmhessf, 0x01, 0x10, 0x00000000, PPC_SPE);
7472GEN_SPE(speundef, evmhossf, 0x03, 0x10, 0x00000000, PPC_SPE);
7473GEN_SPE(evmheumi, evmhesmi, 0x04, 0x10, 0x00000000, PPC_SPE);
7474GEN_SPE(speundef, evmhesmf, 0x05, 0x10, 0x00000000, PPC_SPE);
7475GEN_SPE(evmhoumi, evmhosmi, 0x06, 0x10, 0x00000000, PPC_SPE);
7476GEN_SPE(speundef, evmhosmf, 0x07, 0x10, 0x00000000, PPC_SPE);
7477GEN_SPE(speundef, evmhessfa, 0x11, 0x10, 0x00000000, PPC_SPE);
7478GEN_SPE(speundef, evmhossfa, 0x13, 0x10, 0x00000000, PPC_SPE);
7479GEN_SPE(evmheumia, evmhesmia, 0x14, 0x10, 0x00000000, PPC_SPE);
7480GEN_SPE(speundef, evmhesmfa, 0x15, 0x10, 0x00000000, PPC_SPE);
7481GEN_SPE(evmhoumia, evmhosmia, 0x16, 0x10, 0x00000000, PPC_SPE);
7482GEN_SPE(speundef, evmhosmfa, 0x17, 0x10, 0x00000000, PPC_SPE);
7483
7484GEN_SPE(speundef, evmwhssf, 0x03, 0x11, 0x00000000, PPC_SPE);
7485GEN_SPE(evmwlumi, speundef, 0x04, 0x11, 0x00000000, PPC_SPE);
7486GEN_SPE(evmwhumi, evmwhsmi, 0x06, 0x11, 0x00000000, PPC_SPE);
7487GEN_SPE(speundef, evmwhsmf, 0x07, 0x11, 0x00000000, PPC_SPE);
7488GEN_SPE(speundef, evmwssf, 0x09, 0x11, 0x00000000, PPC_SPE);
7489GEN_SPE(evmwumi, evmwsmi, 0x0C, 0x11, 0x00000000, PPC_SPE);
7490GEN_SPE(speundef, evmwsmf, 0x0D, 0x11, 0x00000000, PPC_SPE);
7491GEN_SPE(speundef, evmwhssfa, 0x13, 0x11, 0x00000000, PPC_SPE);
7492GEN_SPE(evmwlumia, speundef, 0x14, 0x11, 0x00000000, PPC_SPE);
7493GEN_SPE(evmwhumia, evmwhsmia, 0x16, 0x11, 0x00000000, PPC_SPE);
7494GEN_SPE(speundef, evmwhsmfa, 0x17, 0x11, 0x00000000, PPC_SPE);
7495GEN_SPE(speundef, evmwssfa, 0x19, 0x11, 0x00000000, PPC_SPE);
7496GEN_SPE(evmwumia, evmwsmia, 0x1C, 0x11, 0x00000000, PPC_SPE);
7497GEN_SPE(speundef, evmwsmfa, 0x1D, 0x11, 0x00000000, PPC_SPE);
7498
7499GEN_SPE(evadduiaaw, evaddsiaaw, 0x00, 0x13, 0x0000F800, PPC_SPE);
7500GEN_SPE(evsubfusiaaw, evsubfssiaaw, 0x01, 0x13, 0x0000F800, PPC_SPE);
7501GEN_SPE(evaddumiaaw, evaddsmiaaw, 0x04, 0x13, 0x0000F800, PPC_SPE);
7502GEN_SPE(evsubfumiaaw, evsubfsmiaaw, 0x05, 0x13, 0x0000F800, PPC_SPE);
7503GEN_SPE(evdivws, evdivwu, 0x06, 0x13, 0x00000000, PPC_SPE);
7504GEN_SPE(evmra, speundef, 0x07, 0x13, 0x0000F800, PPC_SPE);
7505
7506GEN_SPE(evmheusiaaw, evmhessiaaw, 0x00, 0x14, 0x00000000, PPC_SPE);
7507GEN_SPE(speundef, evmhessfaaw, 0x01, 0x14, 0x00000000, PPC_SPE);
7508GEN_SPE(evmhousiaaw, evmhossiaaw, 0x02, 0x14, 0x00000000, PPC_SPE);
7509GEN_SPE(speundef, evmhossfaaw, 0x03, 0x14, 0x00000000, PPC_SPE);
7510GEN_SPE(evmheumiaaw, evmhesmiaaw, 0x04, 0x14, 0x00000000, PPC_SPE);
7511GEN_SPE(speundef, evmhesmfaaw, 0x05, 0x14, 0x00000000, PPC_SPE);
7512GEN_SPE(evmhoumiaaw, evmhosmiaaw, 0x06, 0x14, 0x00000000, PPC_SPE);
7513GEN_SPE(speundef, evmhosmfaaw, 0x07, 0x14, 0x00000000, PPC_SPE);
7514GEN_SPE(evmhegumiaa, evmhegsmiaa, 0x14, 0x14, 0x00000000, PPC_SPE);
7515GEN_SPE(speundef, evmhegsmfaa, 0x15, 0x14, 0x00000000, PPC_SPE);
7516GEN_SPE(evmhogumiaa, evmhogsmiaa, 0x16, 0x14, 0x00000000, PPC_SPE);
7517GEN_SPE(speundef, evmhogsmfaa, 0x17, 0x14, 0x00000000, PPC_SPE);
7518
7519GEN_SPE(evmwlusiaaw, evmwlssiaaw, 0x00, 0x15, 0x00000000, PPC_SPE);
7520GEN_SPE(evmwlumiaaw, evmwlsmiaaw, 0x04, 0x15, 0x00000000, PPC_SPE);
7521GEN_SPE(speundef, evmwssfaa, 0x09, 0x15, 0x00000000, PPC_SPE);
7522GEN_SPE(evmwumiaa, evmwsmiaa, 0x0C, 0x15, 0x00000000, PPC_SPE);
7523GEN_SPE(speundef, evmwsmfaa, 0x0D, 0x15, 0x00000000, PPC_SPE);
7524
7525GEN_SPE(evmheusianw, evmhessianw, 0x00, 0x16, 0x00000000, PPC_SPE);
7526GEN_SPE(speundef, evmhessfanw, 0x01, 0x16, 0x00000000, PPC_SPE);
7527GEN_SPE(evmhousianw, evmhossianw, 0x02, 0x16, 0x00000000, PPC_SPE);
7528GEN_SPE(speundef, evmhossfanw, 0x03, 0x16, 0x00000000, PPC_SPE);
7529GEN_SPE(evmheumianw, evmhesmianw, 0x04, 0x16, 0x00000000, PPC_SPE);
7530GEN_SPE(speundef, evmhesmfanw, 0x05, 0x16, 0x00000000, PPC_SPE);
7531GEN_SPE(evmhoumianw, evmhosmianw, 0x06, 0x16, 0x00000000, PPC_SPE);
7532GEN_SPE(speundef, evmhosmfanw, 0x07, 0x16, 0x00000000, PPC_SPE);
7533GEN_SPE(evmhegumian, evmhegsmian, 0x14, 0x16, 0x00000000, PPC_SPE);
7534GEN_SPE(speundef, evmhegsmfan, 0x15, 0x16, 0x00000000, PPC_SPE);
7535GEN_SPE(evmhigumian, evmhigsmian, 0x16, 0x16, 0x00000000, PPC_SPE);
7536GEN_SPE(speundef, evmhogsmfan, 0x17, 0x16, 0x00000000, PPC_SPE);
7537
7538GEN_SPE(evmwlusianw, evmwlssianw, 0x00, 0x17, 0x00000000, PPC_SPE);
7539GEN_SPE(evmwlumianw, evmwlsmianw, 0x04, 0x17, 0x00000000, PPC_SPE);
7540GEN_SPE(speundef, evmwssfan, 0x09, 0x17, 0x00000000, PPC_SPE);
7541GEN_SPE(evmwumian, evmwsmian, 0x0C, 0x17, 0x00000000, PPC_SPE);
7542GEN_SPE(speundef, evmwsmfan, 0x0D, 0x17, 0x00000000, PPC_SPE);
7543#endif
7544
7545/*** SPE floating-point extension ***/
1c97856d
AJ
7546#if defined(TARGET_PPC64)
7547#define GEN_SPEFPUOP_CONV_32_32(name) \
b068d6a7 7548static always_inline void gen_##name (DisasContext *ctx) \
0487d6a8 7549{ \
1c97856d
AJ
7550 TCGv_i32 t0; \
7551 TCGv t1; \
7552 t0 = tcg_temp_new_i32(); \
7553 tcg_gen_trunc_tl_i32(t0, cpu_gpr[rB(ctx->opcode)]); \
7554 gen_helper_##name(t0, t0); \
7555 t1 = tcg_temp_new(); \
7556 tcg_gen_extu_i32_tl(t1, t0); \
7557 tcg_temp_free_i32(t0); \
7558 tcg_gen_andi_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rD(ctx->opcode)], \
7559 0xFFFFFFFF00000000ULL); \
7560 tcg_gen_or_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rD(ctx->opcode)], t1); \
7561 tcg_temp_free(t1); \
0487d6a8 7562}
1c97856d
AJ
7563#define GEN_SPEFPUOP_CONV_32_64(name) \
7564static always_inline void gen_##name (DisasContext *ctx) \
7565{ \
7566 TCGv_i32 t0; \
7567 TCGv t1; \
7568 t0 = tcg_temp_new_i32(); \
7569 gen_helper_##name(t0, cpu_gpr[rB(ctx->opcode)]); \
7570 t1 = tcg_temp_new(); \
7571 tcg_gen_extu_i32_tl(t1, t0); \
7572 tcg_temp_free_i32(t0); \
7573 tcg_gen_andi_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rD(ctx->opcode)], \
7574 0xFFFFFFFF00000000ULL); \
7575 tcg_gen_or_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rD(ctx->opcode)], t1); \
7576 tcg_temp_free(t1); \
7577}
7578#define GEN_SPEFPUOP_CONV_64_32(name) \
7579static always_inline void gen_##name (DisasContext *ctx) \
7580{ \
7581 TCGv_i32 t0 = tcg_temp_new_i32(); \
7582 tcg_gen_trunc_tl_i32(t0, cpu_gpr[rB(ctx->opcode)]); \
7583 gen_helper_##name(cpu_gpr[rD(ctx->opcode)], t0); \
7584 tcg_temp_free_i32(t0); \
7585}
7586#define GEN_SPEFPUOP_CONV_64_64(name) \
7587static always_inline void gen_##name (DisasContext *ctx) \
7588{ \
7589 gen_helper_##name(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rB(ctx->opcode)]); \
7590}
7591#define GEN_SPEFPUOP_ARITH2_32_32(name) \
57951c27
AJ
7592static always_inline void gen_##name (DisasContext *ctx) \
7593{ \
1c97856d
AJ
7594 TCGv_i32 t0, t1; \
7595 TCGv_i64 t2; \
57951c27 7596 if (unlikely(!ctx->spe_enabled)) { \
e06fcd75 7597 gen_exception(ctx, POWERPC_EXCP_APU); \
57951c27
AJ
7598 return; \
7599 } \
1c97856d
AJ
7600 t0 = tcg_temp_new_i32(); \
7601 t1 = tcg_temp_new_i32(); \
7602 tcg_gen_trunc_tl_i32(t0, cpu_gpr[rA(ctx->opcode)]); \
7603 tcg_gen_trunc_tl_i32(t1, cpu_gpr[rB(ctx->opcode)]); \
7604 gen_helper_##name(t0, t0, t1); \
7605 tcg_temp_free_i32(t1); \
7606 t2 = tcg_temp_new(); \
7607 tcg_gen_extu_i32_tl(t2, t0); \
7608 tcg_temp_free_i32(t0); \
7609 tcg_gen_andi_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rD(ctx->opcode)], \
7610 0xFFFFFFFF00000000ULL); \
7611 tcg_gen_or_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rD(ctx->opcode)], t2); \
7612 tcg_temp_free(t2); \
57951c27 7613}
1c97856d 7614#define GEN_SPEFPUOP_ARITH2_64_64(name) \
57951c27
AJ
7615static always_inline void gen_##name (DisasContext *ctx) \
7616{ \
7617 if (unlikely(!ctx->spe_enabled)) { \
e06fcd75 7618 gen_exception(ctx, POWERPC_EXCP_APU); \
57951c27
AJ
7619 return; \
7620 } \
1c97856d
AJ
7621 gen_helper_##name(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)], \
7622 cpu_gpr[rB(ctx->opcode)]); \
57951c27 7623}
1c97856d 7624#define GEN_SPEFPUOP_COMP_32(name) \
57951c27
AJ
7625static always_inline void gen_##name (DisasContext *ctx) \
7626{ \
1c97856d 7627 TCGv_i32 t0, t1; \
57951c27 7628 if (unlikely(!ctx->spe_enabled)) { \
e06fcd75 7629 gen_exception(ctx, POWERPC_EXCP_APU); \
57951c27
AJ
7630 return; \
7631 } \
1c97856d
AJ
7632 t0 = tcg_temp_new_i32(); \
7633 t1 = tcg_temp_new_i32(); \
7634 tcg_gen_trunc_tl_i32(t0, cpu_gpr[rA(ctx->opcode)]); \
7635 tcg_gen_trunc_tl_i32(t1, cpu_gpr[rB(ctx->opcode)]); \
7636 gen_helper_##name(cpu_crf[crfD(ctx->opcode)], t0, t1); \
7637 tcg_temp_free_i32(t0); \
7638 tcg_temp_free_i32(t1); \
7639}
7640#define GEN_SPEFPUOP_COMP_64(name) \
7641static always_inline void gen_##name (DisasContext *ctx) \
7642{ \
7643 if (unlikely(!ctx->spe_enabled)) { \
e06fcd75 7644 gen_exception(ctx, POWERPC_EXCP_APU); \
1c97856d
AJ
7645 return; \
7646 } \
7647 gen_helper_##name(cpu_crf[crfD(ctx->opcode)], \
7648 cpu_gpr[rA(ctx->opcode)], cpu_gpr[rB(ctx->opcode)]); \
7649}
7650#else
7651#define GEN_SPEFPUOP_CONV_32_32(name) \
7652static always_inline void gen_##name (DisasContext *ctx) \
7653{ \
7654 gen_helper_##name(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rB(ctx->opcode)]); \
57951c27 7655}
1c97856d
AJ
7656#define GEN_SPEFPUOP_CONV_32_64(name) \
7657static always_inline void gen_##name (DisasContext *ctx) \
7658{ \
7659 TCGv_i64 t0 = tcg_temp_new_i64(); \
7660 gen_load_gpr64(t0, rB(ctx->opcode)); \
7661 gen_helper_##name(cpu_gpr[rD(ctx->opcode)], t0); \
7662 tcg_temp_free_i64(t0); \
7663}
7664#define GEN_SPEFPUOP_CONV_64_32(name) \
7665static always_inline void gen_##name (DisasContext *ctx) \
7666{ \
7667 TCGv_i64 t0 = tcg_temp_new_i64(); \
7668 gen_helper_##name(t0, cpu_gpr[rB(ctx->opcode)]); \
7669 gen_store_gpr64(rD(ctx->opcode), t0); \
7670 tcg_temp_free_i64(t0); \
7671}
7672#define GEN_SPEFPUOP_CONV_64_64(name) \
7673static always_inline void gen_##name (DisasContext *ctx) \
7674{ \
7675 TCGv_i64 t0 = tcg_temp_new_i64(); \
7676 gen_load_gpr64(t0, rB(ctx->opcode)); \
7677 gen_helper_##name(t0, t0); \
7678 gen_store_gpr64(rD(ctx->opcode), t0); \
7679 tcg_temp_free_i64(t0); \
7680}
7681#define GEN_SPEFPUOP_ARITH2_32_32(name) \
7682static always_inline void gen_##name (DisasContext *ctx) \
7683{ \
7684 if (unlikely(!ctx->spe_enabled)) { \
e06fcd75 7685 gen_exception(ctx, POWERPC_EXCP_APU); \
1c97856d
AJ
7686 return; \
7687 } \
7688 gen_helper_##name(cpu_gpr[rD(ctx->opcode)], \
7689 cpu_gpr[rA(ctx->opcode)], cpu_gpr[rB(ctx->opcode)]); \
7690}
7691#define GEN_SPEFPUOP_ARITH2_64_64(name) \
7692static always_inline void gen_##name (DisasContext *ctx) \
7693{ \
7694 TCGv_i64 t0, t1; \
7695 if (unlikely(!ctx->spe_enabled)) { \
e06fcd75 7696 gen_exception(ctx, POWERPC_EXCP_APU); \
1c97856d
AJ
7697 return; \
7698 } \
7699 t0 = tcg_temp_new_i64(); \
7700 t1 = tcg_temp_new_i64(); \
7701 gen_load_gpr64(t0, rA(ctx->opcode)); \
7702 gen_load_gpr64(t1, rB(ctx->opcode)); \
7703 gen_helper_##name(t0, t0, t1); \
7704 gen_store_gpr64(rD(ctx->opcode), t0); \
7705 tcg_temp_free_i64(t0); \
7706 tcg_temp_free_i64(t1); \
7707}
7708#define GEN_SPEFPUOP_COMP_32(name) \
7709static always_inline void gen_##name (DisasContext *ctx) \
7710{ \
7711 if (unlikely(!ctx->spe_enabled)) { \
e06fcd75 7712 gen_exception(ctx, POWERPC_EXCP_APU); \
1c97856d
AJ
7713 return; \
7714 } \
7715 gen_helper_##name(cpu_crf[crfD(ctx->opcode)], \
7716 cpu_gpr[rA(ctx->opcode)], cpu_gpr[rB(ctx->opcode)]); \
7717}
7718#define GEN_SPEFPUOP_COMP_64(name) \
7719static always_inline void gen_##name (DisasContext *ctx) \
7720{ \
7721 TCGv_i64 t0, t1; \
7722 if (unlikely(!ctx->spe_enabled)) { \
e06fcd75 7723 gen_exception(ctx, POWERPC_EXCP_APU); \
1c97856d
AJ
7724 return; \
7725 } \
7726 t0 = tcg_temp_new_i64(); \
7727 t1 = tcg_temp_new_i64(); \
7728 gen_load_gpr64(t0, rA(ctx->opcode)); \
7729 gen_load_gpr64(t1, rB(ctx->opcode)); \
7730 gen_helper_##name(cpu_crf[crfD(ctx->opcode)], t0, t1); \
7731 tcg_temp_free_i64(t0); \
7732 tcg_temp_free_i64(t1); \
7733}
7734#endif
57951c27 7735
0487d6a8
JM
7736/* Single precision floating-point vectors operations */
7737/* Arithmetic */
1c97856d
AJ
7738GEN_SPEFPUOP_ARITH2_64_64(evfsadd);
7739GEN_SPEFPUOP_ARITH2_64_64(evfssub);
7740GEN_SPEFPUOP_ARITH2_64_64(evfsmul);
7741GEN_SPEFPUOP_ARITH2_64_64(evfsdiv);
7742static always_inline void gen_evfsabs (DisasContext *ctx)
7743{
7744 if (unlikely(!ctx->spe_enabled)) {
e06fcd75 7745 gen_exception(ctx, POWERPC_EXCP_APU);
1c97856d
AJ
7746 return;
7747 }
7748#if defined(TARGET_PPC64)
7749 tcg_gen_andi_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rA(ctx->opcode)], ~0x8000000080000000LL);
7750#else
7751 tcg_gen_andi_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rA(ctx->opcode)], ~0x80000000);
7752 tcg_gen_andi_tl(cpu_gprh[rA(ctx->opcode)], cpu_gprh[rA(ctx->opcode)], ~0x80000000);
7753#endif
7754}
7755static always_inline void gen_evfsnabs (DisasContext *ctx)
7756{
7757 if (unlikely(!ctx->spe_enabled)) {
e06fcd75 7758 gen_exception(ctx, POWERPC_EXCP_APU);
1c97856d
AJ
7759 return;
7760 }
7761#if defined(TARGET_PPC64)
7762 tcg_gen_ori_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rA(ctx->opcode)], 0x8000000080000000LL);
7763#else
7764 tcg_gen_ori_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rA(ctx->opcode)], 0x80000000);
7765 tcg_gen_ori_tl(cpu_gprh[rA(ctx->opcode)], cpu_gprh[rA(ctx->opcode)], 0x80000000);
7766#endif
7767}
7768static always_inline void gen_evfsneg (DisasContext *ctx)
7769{
7770 if (unlikely(!ctx->spe_enabled)) {
e06fcd75 7771 gen_exception(ctx, POWERPC_EXCP_APU);
1c97856d
AJ
7772 return;
7773 }
7774#if defined(TARGET_PPC64)
7775 tcg_gen_xori_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rA(ctx->opcode)], 0x8000000080000000LL);
7776#else
7777 tcg_gen_xori_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rA(ctx->opcode)], 0x80000000);
7778 tcg_gen_xori_tl(cpu_gprh[rA(ctx->opcode)], cpu_gprh[rA(ctx->opcode)], 0x80000000);
7779#endif
7780}
7781
0487d6a8 7782/* Conversion */
1c97856d
AJ
7783GEN_SPEFPUOP_CONV_64_64(evfscfui);
7784GEN_SPEFPUOP_CONV_64_64(evfscfsi);
7785GEN_SPEFPUOP_CONV_64_64(evfscfuf);
7786GEN_SPEFPUOP_CONV_64_64(evfscfsf);
7787GEN_SPEFPUOP_CONV_64_64(evfsctui);
7788GEN_SPEFPUOP_CONV_64_64(evfsctsi);
7789GEN_SPEFPUOP_CONV_64_64(evfsctuf);
7790GEN_SPEFPUOP_CONV_64_64(evfsctsf);
7791GEN_SPEFPUOP_CONV_64_64(evfsctuiz);
7792GEN_SPEFPUOP_CONV_64_64(evfsctsiz);
7793
0487d6a8 7794/* Comparison */
1c97856d
AJ
7795GEN_SPEFPUOP_COMP_64(evfscmpgt);
7796GEN_SPEFPUOP_COMP_64(evfscmplt);
7797GEN_SPEFPUOP_COMP_64(evfscmpeq);
7798GEN_SPEFPUOP_COMP_64(evfststgt);
7799GEN_SPEFPUOP_COMP_64(evfststlt);
7800GEN_SPEFPUOP_COMP_64(evfststeq);
0487d6a8
JM
7801
7802/* Opcodes definitions */
40569b7e
AJ
7803GEN_SPE(evfsadd, evfssub, 0x00, 0x0A, 0x00000000, PPC_SPE_SINGLE); //
7804GEN_SPE(evfsabs, evfsnabs, 0x02, 0x0A, 0x0000F800, PPC_SPE_SINGLE); //
7805GEN_SPE(evfsneg, speundef, 0x03, 0x0A, 0x0000F800, PPC_SPE_SINGLE); //
7806GEN_SPE(evfsmul, evfsdiv, 0x04, 0x0A, 0x00000000, PPC_SPE_SINGLE); //
7807GEN_SPE(evfscmpgt, evfscmplt, 0x06, 0x0A, 0x00600000, PPC_SPE_SINGLE); //
7808GEN_SPE(evfscmpeq, speundef, 0x07, 0x0A, 0x00600000, PPC_SPE_SINGLE); //
7809GEN_SPE(evfscfui, evfscfsi, 0x08, 0x0A, 0x00180000, PPC_SPE_SINGLE); //
7810GEN_SPE(evfscfuf, evfscfsf, 0x09, 0x0A, 0x00180000, PPC_SPE_SINGLE); //
7811GEN_SPE(evfsctui, evfsctsi, 0x0A, 0x0A, 0x00180000, PPC_SPE_SINGLE); //
7812GEN_SPE(evfsctuf, evfsctsf, 0x0B, 0x0A, 0x00180000, PPC_SPE_SINGLE); //
7813GEN_SPE(evfsctuiz, speundef, 0x0C, 0x0A, 0x00180000, PPC_SPE_SINGLE); //
7814GEN_SPE(evfsctsiz, speundef, 0x0D, 0x0A, 0x00180000, PPC_SPE_SINGLE); //
7815GEN_SPE(evfststgt, evfststlt, 0x0E, 0x0A, 0x00600000, PPC_SPE_SINGLE); //
7816GEN_SPE(evfststeq, speundef, 0x0F, 0x0A, 0x00600000, PPC_SPE_SINGLE); //
0487d6a8
JM
7817
7818/* Single precision floating-point operations */
7819/* Arithmetic */
1c97856d
AJ
7820GEN_SPEFPUOP_ARITH2_32_32(efsadd);
7821GEN_SPEFPUOP_ARITH2_32_32(efssub);
7822GEN_SPEFPUOP_ARITH2_32_32(efsmul);
7823GEN_SPEFPUOP_ARITH2_32_32(efsdiv);
7824static always_inline void gen_efsabs (DisasContext *ctx)
7825{
7826 if (unlikely(!ctx->spe_enabled)) {
e06fcd75 7827 gen_exception(ctx, POWERPC_EXCP_APU);
1c97856d
AJ
7828 return;
7829 }
7830 tcg_gen_andi_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rA(ctx->opcode)], (target_long)~0x80000000LL);
7831}
7832static always_inline void gen_efsnabs (DisasContext *ctx)
7833{
7834 if (unlikely(!ctx->spe_enabled)) {
e06fcd75 7835 gen_exception(ctx, POWERPC_EXCP_APU);
1c97856d
AJ
7836 return;
7837 }
7838 tcg_gen_ori_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rA(ctx->opcode)], 0x80000000);
7839}
7840static always_inline void gen_efsneg (DisasContext *ctx)
7841{
7842 if (unlikely(!ctx->spe_enabled)) {
e06fcd75 7843 gen_exception(ctx, POWERPC_EXCP_APU);
1c97856d
AJ
7844 return;
7845 }
7846 tcg_gen_xori_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rA(ctx->opcode)], 0x80000000);
7847}
7848
0487d6a8 7849/* Conversion */
1c97856d
AJ
7850GEN_SPEFPUOP_CONV_32_32(efscfui);
7851GEN_SPEFPUOP_CONV_32_32(efscfsi);
7852GEN_SPEFPUOP_CONV_32_32(efscfuf);
7853GEN_SPEFPUOP_CONV_32_32(efscfsf);
7854GEN_SPEFPUOP_CONV_32_32(efsctui);
7855GEN_SPEFPUOP_CONV_32_32(efsctsi);
7856GEN_SPEFPUOP_CONV_32_32(efsctuf);
7857GEN_SPEFPUOP_CONV_32_32(efsctsf);
7858GEN_SPEFPUOP_CONV_32_32(efsctuiz);
7859GEN_SPEFPUOP_CONV_32_32(efsctsiz);
7860GEN_SPEFPUOP_CONV_32_64(efscfd);
7861
0487d6a8 7862/* Comparison */
1c97856d
AJ
7863GEN_SPEFPUOP_COMP_32(efscmpgt);
7864GEN_SPEFPUOP_COMP_32(efscmplt);
7865GEN_SPEFPUOP_COMP_32(efscmpeq);
7866GEN_SPEFPUOP_COMP_32(efststgt);
7867GEN_SPEFPUOP_COMP_32(efststlt);
7868GEN_SPEFPUOP_COMP_32(efststeq);
0487d6a8
JM
7869
7870/* Opcodes definitions */
40569b7e
AJ
7871GEN_SPE(efsadd, efssub, 0x00, 0x0B, 0x00000000, PPC_SPE_SINGLE); //
7872GEN_SPE(efsabs, efsnabs, 0x02, 0x0B, 0x0000F800, PPC_SPE_SINGLE); //
7873GEN_SPE(efsneg, speundef, 0x03, 0x0B, 0x0000F800, PPC_SPE_SINGLE); //
7874GEN_SPE(efsmul, efsdiv, 0x04, 0x0B, 0x00000000, PPC_SPE_SINGLE); //
7875GEN_SPE(efscmpgt, efscmplt, 0x06, 0x0B, 0x00600000, PPC_SPE_SINGLE); //
7876GEN_SPE(efscmpeq, efscfd, 0x07, 0x0B, 0x00600000, PPC_SPE_SINGLE); //
7877GEN_SPE(efscfui, efscfsi, 0x08, 0x0B, 0x00180000, PPC_SPE_SINGLE); //
7878GEN_SPE(efscfuf, efscfsf, 0x09, 0x0B, 0x00180000, PPC_SPE_SINGLE); //
7879GEN_SPE(efsctui, efsctsi, 0x0A, 0x0B, 0x00180000, PPC_SPE_SINGLE); //
7880GEN_SPE(efsctuf, efsctsf, 0x0B, 0x0B, 0x00180000, PPC_SPE_SINGLE); //
7881GEN_SPE(efsctuiz, speundef, 0x0C, 0x0B, 0x00180000, PPC_SPE_SINGLE); //
7882GEN_SPE(efsctsiz, speundef, 0x0D, 0x0B, 0x00180000, PPC_SPE_SINGLE); //
7883GEN_SPE(efststgt, efststlt, 0x0E, 0x0B, 0x00600000, PPC_SPE_SINGLE); //
7884GEN_SPE(efststeq, speundef, 0x0F, 0x0B, 0x00600000, PPC_SPE_SINGLE); //
0487d6a8
JM
7885
7886/* Double precision floating-point operations */
7887/* Arithmetic */
1c97856d
AJ
7888GEN_SPEFPUOP_ARITH2_64_64(efdadd);
7889GEN_SPEFPUOP_ARITH2_64_64(efdsub);
7890GEN_SPEFPUOP_ARITH2_64_64(efdmul);
7891GEN_SPEFPUOP_ARITH2_64_64(efddiv);
7892static always_inline void gen_efdabs (DisasContext *ctx)
7893{
7894 if (unlikely(!ctx->spe_enabled)) {
e06fcd75 7895 gen_exception(ctx, POWERPC_EXCP_APU);
1c97856d
AJ
7896 return;
7897 }
7898#if defined(TARGET_PPC64)
7899 tcg_gen_andi_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rA(ctx->opcode)], ~0x8000000000000000LL);
7900#else
7901 tcg_gen_andi_tl(cpu_gprh[rA(ctx->opcode)], cpu_gprh[rA(ctx->opcode)], ~0x80000000);
7902#endif
7903}
7904static always_inline void gen_efdnabs (DisasContext *ctx)
7905{
7906 if (unlikely(!ctx->spe_enabled)) {
e06fcd75 7907 gen_exception(ctx, POWERPC_EXCP_APU);
1c97856d
AJ
7908 return;
7909 }
7910#if defined(TARGET_PPC64)
7911 tcg_gen_ori_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rA(ctx->opcode)], 0x8000000000000000LL);
7912#else
7913 tcg_gen_ori_tl(cpu_gprh[rA(ctx->opcode)], cpu_gprh[rA(ctx->opcode)], 0x80000000);
7914#endif
7915}
7916static always_inline void gen_efdneg (DisasContext *ctx)
7917{
7918 if (unlikely(!ctx->spe_enabled)) {
e06fcd75 7919 gen_exception(ctx, POWERPC_EXCP_APU);
1c97856d
AJ
7920 return;
7921 }
7922#if defined(TARGET_PPC64)
7923 tcg_gen_xori_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rA(ctx->opcode)], 0x8000000000000000LL);
7924#else
7925 tcg_gen_xori_tl(cpu_gprh[rA(ctx->opcode)], cpu_gprh[rA(ctx->opcode)], 0x80000000);
7926#endif
7927}
7928
0487d6a8 7929/* Conversion */
1c97856d
AJ
7930GEN_SPEFPUOP_CONV_64_32(efdcfui);
7931GEN_SPEFPUOP_CONV_64_32(efdcfsi);
7932GEN_SPEFPUOP_CONV_64_32(efdcfuf);
7933GEN_SPEFPUOP_CONV_64_32(efdcfsf);
7934GEN_SPEFPUOP_CONV_32_64(efdctui);
7935GEN_SPEFPUOP_CONV_32_64(efdctsi);
7936GEN_SPEFPUOP_CONV_32_64(efdctuf);
7937GEN_SPEFPUOP_CONV_32_64(efdctsf);
7938GEN_SPEFPUOP_CONV_32_64(efdctuiz);
7939GEN_SPEFPUOP_CONV_32_64(efdctsiz);
7940GEN_SPEFPUOP_CONV_64_32(efdcfs);
7941GEN_SPEFPUOP_CONV_64_64(efdcfuid);
7942GEN_SPEFPUOP_CONV_64_64(efdcfsid);
7943GEN_SPEFPUOP_CONV_64_64(efdctuidz);
7944GEN_SPEFPUOP_CONV_64_64(efdctsidz);
0487d6a8 7945
0487d6a8 7946/* Comparison */
1c97856d
AJ
7947GEN_SPEFPUOP_COMP_64(efdcmpgt);
7948GEN_SPEFPUOP_COMP_64(efdcmplt);
7949GEN_SPEFPUOP_COMP_64(efdcmpeq);
7950GEN_SPEFPUOP_COMP_64(efdtstgt);
7951GEN_SPEFPUOP_COMP_64(efdtstlt);
7952GEN_SPEFPUOP_COMP_64(efdtsteq);
0487d6a8
JM
7953
7954/* Opcodes definitions */
40569b7e
AJ
7955GEN_SPE(efdadd, efdsub, 0x10, 0x0B, 0x00000000, PPC_SPE_DOUBLE); //
7956GEN_SPE(efdcfuid, efdcfsid, 0x11, 0x0B, 0x00180000, PPC_SPE_DOUBLE); //
7957GEN_SPE(efdabs, efdnabs, 0x12, 0x0B, 0x0000F800, PPC_SPE_DOUBLE); //
7958GEN_SPE(efdneg, speundef, 0x13, 0x0B, 0x0000F800, PPC_SPE_DOUBLE); //
7959GEN_SPE(efdmul, efddiv, 0x14, 0x0B, 0x00000000, PPC_SPE_DOUBLE); //
7960GEN_SPE(efdctuidz, efdctsidz, 0x15, 0x0B, 0x00180000, PPC_SPE_DOUBLE); //
7961GEN_SPE(efdcmpgt, efdcmplt, 0x16, 0x0B, 0x00600000, PPC_SPE_DOUBLE); //
7962GEN_SPE(efdcmpeq, efdcfs, 0x17, 0x0B, 0x00600000, PPC_SPE_DOUBLE); //
7963GEN_SPE(efdcfui, efdcfsi, 0x18, 0x0B, 0x00180000, PPC_SPE_DOUBLE); //
7964GEN_SPE(efdcfuf, efdcfsf, 0x19, 0x0B, 0x00180000, PPC_SPE_DOUBLE); //
7965GEN_SPE(efdctui, efdctsi, 0x1A, 0x0B, 0x00180000, PPC_SPE_DOUBLE); //
7966GEN_SPE(efdctuf, efdctsf, 0x1B, 0x0B, 0x00180000, PPC_SPE_DOUBLE); //
7967GEN_SPE(efdctuiz, speundef, 0x1C, 0x0B, 0x00180000, PPC_SPE_DOUBLE); //
7968GEN_SPE(efdctsiz, speundef, 0x1D, 0x0B, 0x00180000, PPC_SPE_DOUBLE); //
7969GEN_SPE(efdtstgt, efdtstlt, 0x1E, 0x0B, 0x00600000, PPC_SPE_DOUBLE); //
7970GEN_SPE(efdtsteq, speundef, 0x1F, 0x0B, 0x00600000, PPC_SPE_DOUBLE); //
0487d6a8 7971
5c55ff99
BS
7972static opcode_t opcodes[] = {
7973GEN_HANDLER(invalid, 0x00, 0x00, 0x00, 0xFFFFFFFF, PPC_NONE),
7974GEN_HANDLER(cmp, 0x1F, 0x00, 0x00, 0x00400000, PPC_INTEGER),
7975GEN_HANDLER(cmpi, 0x0B, 0xFF, 0xFF, 0x00400000, PPC_INTEGER),
7976GEN_HANDLER(cmpl, 0x1F, 0x00, 0x01, 0x00400000, PPC_INTEGER),
7977GEN_HANDLER(cmpli, 0x0A, 0xFF, 0xFF, 0x00400000, PPC_INTEGER),
7978GEN_HANDLER(isel, 0x1F, 0x0F, 0xFF, 0x00000001, PPC_ISEL),
7979GEN_HANDLER(addi, 0x0E, 0xFF, 0xFF, 0x00000000, PPC_INTEGER),
7980GEN_HANDLER(addic, 0x0C, 0xFF, 0xFF, 0x00000000, PPC_INTEGER),
7981GEN_HANDLER2(addic_, "addic.", 0x0D, 0xFF, 0xFF, 0x00000000, PPC_INTEGER),
7982GEN_HANDLER(addis, 0x0F, 0xFF, 0xFF, 0x00000000, PPC_INTEGER),
7983GEN_HANDLER(mulhw, 0x1F, 0x0B, 0x02, 0x00000400, PPC_INTEGER),
7984GEN_HANDLER(mulhwu, 0x1F, 0x0B, 0x00, 0x00000400, PPC_INTEGER),
7985GEN_HANDLER(mullw, 0x1F, 0x0B, 0x07, 0x00000000, PPC_INTEGER),
7986GEN_HANDLER(mullwo, 0x1F, 0x0B, 0x17, 0x00000000, PPC_INTEGER),
7987GEN_HANDLER(mulli, 0x07, 0xFF, 0xFF, 0x00000000, PPC_INTEGER),
7988#if defined(TARGET_PPC64)
7989GEN_HANDLER(mulld, 0x1F, 0x09, 0x07, 0x00000000, PPC_64B),
7990#endif
7991GEN_HANDLER(neg, 0x1F, 0x08, 0x03, 0x0000F800, PPC_INTEGER),
7992GEN_HANDLER(nego, 0x1F, 0x08, 0x13, 0x0000F800, PPC_INTEGER),
7993GEN_HANDLER(subfic, 0x08, 0xFF, 0xFF, 0x00000000, PPC_INTEGER),
7994GEN_HANDLER2(andi_, "andi.", 0x1C, 0xFF, 0xFF, 0x00000000, PPC_INTEGER),
7995GEN_HANDLER2(andis_, "andis.", 0x1D, 0xFF, 0xFF, 0x00000000, PPC_INTEGER),
7996GEN_HANDLER(cntlzw, 0x1F, 0x1A, 0x00, 0x00000000, PPC_INTEGER),
7997GEN_HANDLER(or, 0x1F, 0x1C, 0x0D, 0x00000000, PPC_INTEGER),
7998GEN_HANDLER(xor, 0x1F, 0x1C, 0x09, 0x00000000, PPC_INTEGER),
7999GEN_HANDLER(ori, 0x18, 0xFF, 0xFF, 0x00000000, PPC_INTEGER),
8000GEN_HANDLER(oris, 0x19, 0xFF, 0xFF, 0x00000000, PPC_INTEGER),
8001GEN_HANDLER(xori, 0x1A, 0xFF, 0xFF, 0x00000000, PPC_INTEGER),
8002GEN_HANDLER(xoris, 0x1B, 0xFF, 0xFF, 0x00000000, PPC_INTEGER),
8003GEN_HANDLER(popcntb, 0x1F, 0x03, 0x03, 0x0000F801, PPC_POPCNTB),
8004#if defined(TARGET_PPC64)
8005GEN_HANDLER(cntlzd, 0x1F, 0x1A, 0x01, 0x00000000, PPC_64B),
8006#endif
8007GEN_HANDLER(rlwimi, 0x14, 0xFF, 0xFF, 0x00000000, PPC_INTEGER),
8008GEN_HANDLER(rlwinm, 0x15, 0xFF, 0xFF, 0x00000000, PPC_INTEGER),
8009GEN_HANDLER(rlwnm, 0x17, 0xFF, 0xFF, 0x00000000, PPC_INTEGER),
8010GEN_HANDLER(slw, 0x1F, 0x18, 0x00, 0x00000000, PPC_INTEGER),
8011GEN_HANDLER(sraw, 0x1F, 0x18, 0x18, 0x00000000, PPC_INTEGER),
8012GEN_HANDLER(srawi, 0x1F, 0x18, 0x19, 0x00000000, PPC_INTEGER),
8013GEN_HANDLER(srw, 0x1F, 0x18, 0x10, 0x00000000, PPC_INTEGER),
8014#if defined(TARGET_PPC64)
8015GEN_HANDLER(sld, 0x1F, 0x1B, 0x00, 0x00000000, PPC_64B),
8016GEN_HANDLER(srad, 0x1F, 0x1A, 0x18, 0x00000000, PPC_64B),
8017GEN_HANDLER2(sradi0, "sradi", 0x1F, 0x1A, 0x19, 0x00000000, PPC_64B),
8018GEN_HANDLER2(sradi1, "sradi", 0x1F, 0x1B, 0x19, 0x00000000, PPC_64B),
8019GEN_HANDLER(srd, 0x1F, 0x1B, 0x10, 0x00000000, PPC_64B),
8020#endif
8021GEN_HANDLER(frsqrtes, 0x3B, 0x1A, 0xFF, 0x001F07C0, PPC_FLOAT_FRSQRTES),
8022GEN_HANDLER(fsqrt, 0x3F, 0x16, 0xFF, 0x001F07C0, PPC_FLOAT_FSQRT),
8023GEN_HANDLER(fsqrts, 0x3B, 0x16, 0xFF, 0x001F07C0, PPC_FLOAT_FSQRT),
8024GEN_HANDLER(fcmpo, 0x3F, 0x00, 0x01, 0x00600001, PPC_FLOAT),
8025GEN_HANDLER(fcmpu, 0x3F, 0x00, 0x00, 0x00600001, PPC_FLOAT),
8026GEN_HANDLER(fmr, 0x3F, 0x08, 0x02, 0x001F0000, PPC_FLOAT),
8027GEN_HANDLER(mcrfs, 0x3F, 0x00, 0x02, 0x0063F801, PPC_FLOAT),
8028GEN_HANDLER(mffs, 0x3F, 0x07, 0x12, 0x001FF800, PPC_FLOAT),
8029GEN_HANDLER(mtfsb0, 0x3F, 0x06, 0x02, 0x001FF800, PPC_FLOAT),
8030GEN_HANDLER(mtfsb1, 0x3F, 0x06, 0x01, 0x001FF800, PPC_FLOAT),
8031GEN_HANDLER(mtfsf, 0x3F, 0x07, 0x16, 0x00010000, PPC_FLOAT),
8032GEN_HANDLER(mtfsfi, 0x3F, 0x06, 0x04, 0x006f0800, PPC_FLOAT),
8033#if defined(TARGET_PPC64)
8034GEN_HANDLER(ld, 0x3A, 0xFF, 0xFF, 0x00000000, PPC_64B),
8035GEN_HANDLER(lq, 0x38, 0xFF, 0xFF, 0x00000000, PPC_64BX),
8036GEN_HANDLER(std, 0x3E, 0xFF, 0xFF, 0x00000000, PPC_64B),
8037#endif
8038GEN_HANDLER(lmw, 0x2E, 0xFF, 0xFF, 0x00000000, PPC_INTEGER),
8039GEN_HANDLER(stmw, 0x2F, 0xFF, 0xFF, 0x00000000, PPC_INTEGER),
8040GEN_HANDLER(lswi, 0x1F, 0x15, 0x12, 0x00000001, PPC_STRING),
8041GEN_HANDLER(lswx, 0x1F, 0x15, 0x10, 0x00000001, PPC_STRING),
8042GEN_HANDLER(stswi, 0x1F, 0x15, 0x16, 0x00000001, PPC_STRING),
8043GEN_HANDLER(stswx, 0x1F, 0x15, 0x14, 0x00000001, PPC_STRING),
8044GEN_HANDLER(eieio, 0x1F, 0x16, 0x1A, 0x03FFF801, PPC_MEM_EIEIO),
8045GEN_HANDLER(isync, 0x13, 0x16, 0x04, 0x03FFF801, PPC_MEM),
8046GEN_HANDLER(lwarx, 0x1F, 0x14, 0x00, 0x00000001, PPC_RES),
8047GEN_HANDLER2(stwcx_, "stwcx.", 0x1F, 0x16, 0x04, 0x00000000, PPC_RES),
8048#if defined(TARGET_PPC64)
8049GEN_HANDLER(ldarx, 0x1F, 0x14, 0x02, 0x00000001, PPC_64B),
8050GEN_HANDLER2(stdcx_, "stdcx.", 0x1F, 0x16, 0x06, 0x00000000, PPC_64B),
8051#endif
8052GEN_HANDLER(sync, 0x1F, 0x16, 0x12, 0x039FF801, PPC_MEM_SYNC),
8053GEN_HANDLER(wait, 0x1F, 0x1E, 0x01, 0x03FFF801, PPC_WAIT),
8054GEN_HANDLER(b, 0x12, 0xFF, 0xFF, 0x00000000, PPC_FLOW),
8055GEN_HANDLER(bc, 0x10, 0xFF, 0xFF, 0x00000000, PPC_FLOW),
8056GEN_HANDLER(bcctr, 0x13, 0x10, 0x10, 0x00000000, PPC_FLOW),
8057GEN_HANDLER(bclr, 0x13, 0x10, 0x00, 0x00000000, PPC_FLOW),
8058GEN_HANDLER(mcrf, 0x13, 0x00, 0xFF, 0x00000001, PPC_INTEGER),
8059GEN_HANDLER(rfi, 0x13, 0x12, 0x01, 0x03FF8001, PPC_FLOW),
8060#if defined(TARGET_PPC64)
8061GEN_HANDLER(rfid, 0x13, 0x12, 0x00, 0x03FF8001, PPC_64B),
8062GEN_HANDLER(hrfid, 0x13, 0x12, 0x08, 0x03FF8001, PPC_64H),
8063#endif
8064GEN_HANDLER(sc, 0x11, 0xFF, 0xFF, 0x03FFF01D, PPC_FLOW),
8065GEN_HANDLER(tw, 0x1F, 0x04, 0x00, 0x00000001, PPC_FLOW),
8066GEN_HANDLER(twi, 0x03, 0xFF, 0xFF, 0x00000000, PPC_FLOW),
8067#if defined(TARGET_PPC64)
8068GEN_HANDLER(td, 0x1F, 0x04, 0x02, 0x00000001, PPC_64B),
8069GEN_HANDLER(tdi, 0x02, 0xFF, 0xFF, 0x00000000, PPC_64B),
8070#endif
8071GEN_HANDLER(mcrxr, 0x1F, 0x00, 0x10, 0x007FF801, PPC_MISC),
8072GEN_HANDLER(mfcr, 0x1F, 0x13, 0x00, 0x00000801, PPC_MISC),
8073GEN_HANDLER(mfmsr, 0x1F, 0x13, 0x02, 0x001FF801, PPC_MISC),
8074GEN_HANDLER(mfspr, 0x1F, 0x13, 0x0A, 0x00000001, PPC_MISC),
8075GEN_HANDLER(mftb, 0x1F, 0x13, 0x0B, 0x00000001, PPC_MFTB),
8076GEN_HANDLER(mtcrf, 0x1F, 0x10, 0x04, 0x00000801, PPC_MISC),
8077#if defined(TARGET_PPC64)
8078GEN_HANDLER(mtmsrd, 0x1F, 0x12, 0x05, 0x001EF801, PPC_64B),
8079#endif
8080GEN_HANDLER(mtmsr, 0x1F, 0x12, 0x04, 0x001FF801, PPC_MISC),
8081GEN_HANDLER(mtspr, 0x1F, 0x13, 0x0E, 0x00000001, PPC_MISC),
8082GEN_HANDLER(dcbf, 0x1F, 0x16, 0x02, 0x03C00001, PPC_CACHE),
8083GEN_HANDLER(dcbi, 0x1F, 0x16, 0x0E, 0x03E00001, PPC_CACHE),
8084GEN_HANDLER(dcbst, 0x1F, 0x16, 0x01, 0x03E00001, PPC_CACHE),
8085GEN_HANDLER(dcbt, 0x1F, 0x16, 0x08, 0x02000001, PPC_CACHE),
8086GEN_HANDLER(dcbtst, 0x1F, 0x16, 0x07, 0x02000001, PPC_CACHE),
8087GEN_HANDLER(dcbz, 0x1F, 0x16, 0x1F, 0x03E00001, PPC_CACHE_DCBZ),
8088GEN_HANDLER2(dcbz_970, "dcbz", 0x1F, 0x16, 0x1F, 0x03C00001, PPC_CACHE_DCBZT),
8089GEN_HANDLER(dst, 0x1F, 0x16, 0x0A, 0x01800001, PPC_ALTIVEC),
8090GEN_HANDLER(dstst, 0x1F, 0x16, 0x0B, 0x02000001, PPC_ALTIVEC),
8091GEN_HANDLER(dss, 0x1F, 0x16, 0x19, 0x019FF801, PPC_ALTIVEC),
8092GEN_HANDLER(icbi, 0x1F, 0x16, 0x1E, 0x03E00001, PPC_CACHE_ICBI),
8093GEN_HANDLER(dcba, 0x1F, 0x16, 0x17, 0x03E00001, PPC_CACHE_DCBA),
8094GEN_HANDLER(mfsr, 0x1F, 0x13, 0x12, 0x0010F801, PPC_SEGMENT),
8095GEN_HANDLER(mfsrin, 0x1F, 0x13, 0x14, 0x001F0001, PPC_SEGMENT),
8096GEN_HANDLER(mtsr, 0x1F, 0x12, 0x06, 0x0010F801, PPC_SEGMENT),
8097GEN_HANDLER(mtsrin, 0x1F, 0x12, 0x07, 0x001F0001, PPC_SEGMENT),
8098#if defined(TARGET_PPC64)
8099GEN_HANDLER2(mfsr_64b, "mfsr", 0x1F, 0x13, 0x12, 0x0010F801, PPC_SEGMENT_64B),
8100GEN_HANDLER2(mfsrin_64b, "mfsrin", 0x1F, 0x13, 0x14, 0x001F0001,
8101 PPC_SEGMENT_64B),
8102GEN_HANDLER2(mtsr_64b, "mtsr", 0x1F, 0x12, 0x06, 0x0010F801, PPC_SEGMENT_64B),
8103GEN_HANDLER2(mtsrin_64b, "mtsrin", 0x1F, 0x12, 0x07, 0x001F0001,
8104 PPC_SEGMENT_64B),
8105GEN_HANDLER2(slbmte, "slbmte", 0x1F, 0x12, 0x0C, 0x00000000, PPC_SEGMENT_64B),
8106#endif
8107GEN_HANDLER(tlbia, 0x1F, 0x12, 0x0B, 0x03FFFC01, PPC_MEM_TLBIA),
8108GEN_HANDLER(tlbiel, 0x1F, 0x12, 0x08, 0x03FF0001, PPC_MEM_TLBIE),
8109GEN_HANDLER(tlbie, 0x1F, 0x12, 0x09, 0x03FF0001, PPC_MEM_TLBIE),
8110GEN_HANDLER(tlbsync, 0x1F, 0x16, 0x11, 0x03FFF801, PPC_MEM_TLBSYNC),
8111#if defined(TARGET_PPC64)
8112GEN_HANDLER(slbia, 0x1F, 0x12, 0x0F, 0x03FFFC01, PPC_SLBI),
8113GEN_HANDLER(slbie, 0x1F, 0x12, 0x0D, 0x03FF0001, PPC_SLBI),
8114#endif
8115GEN_HANDLER(eciwx, 0x1F, 0x16, 0x0D, 0x00000001, PPC_EXTERN),
8116GEN_HANDLER(ecowx, 0x1F, 0x16, 0x09, 0x00000001, PPC_EXTERN),
8117GEN_HANDLER(abs, 0x1F, 0x08, 0x0B, 0x0000F800, PPC_POWER_BR),
8118GEN_HANDLER(abso, 0x1F, 0x08, 0x1B, 0x0000F800, PPC_POWER_BR),
8119GEN_HANDLER(clcs, 0x1F, 0x10, 0x13, 0x0000F800, PPC_POWER_BR),
8120GEN_HANDLER(div, 0x1F, 0x0B, 0x0A, 0x00000000, PPC_POWER_BR),
8121GEN_HANDLER(divo, 0x1F, 0x0B, 0x1A, 0x00000000, PPC_POWER_BR),
8122GEN_HANDLER(divs, 0x1F, 0x0B, 0x0B, 0x00000000, PPC_POWER_BR),
8123GEN_HANDLER(divso, 0x1F, 0x0B, 0x1B, 0x00000000, PPC_POWER_BR),
8124GEN_HANDLER(doz, 0x1F, 0x08, 0x08, 0x00000000, PPC_POWER_BR),
8125GEN_HANDLER(dozo, 0x1F, 0x08, 0x18, 0x00000000, PPC_POWER_BR),
8126GEN_HANDLER(dozi, 0x09, 0xFF, 0xFF, 0x00000000, PPC_POWER_BR),
8127GEN_HANDLER(lscbx, 0x1F, 0x15, 0x08, 0x00000000, PPC_POWER_BR),
8128GEN_HANDLER(maskg, 0x1F, 0x1D, 0x00, 0x00000000, PPC_POWER_BR),
8129GEN_HANDLER(maskir, 0x1F, 0x1D, 0x10, 0x00000000, PPC_POWER_BR),
8130GEN_HANDLER(mul, 0x1F, 0x0B, 0x03, 0x00000000, PPC_POWER_BR),
8131GEN_HANDLER(mulo, 0x1F, 0x0B, 0x13, 0x00000000, PPC_POWER_BR),
8132GEN_HANDLER(nabs, 0x1F, 0x08, 0x0F, 0x00000000, PPC_POWER_BR),
8133GEN_HANDLER(nabso, 0x1F, 0x08, 0x1F, 0x00000000, PPC_POWER_BR),
8134GEN_HANDLER(rlmi, 0x16, 0xFF, 0xFF, 0x00000000, PPC_POWER_BR),
8135GEN_HANDLER(rrib, 0x1F, 0x19, 0x10, 0x00000000, PPC_POWER_BR),
8136GEN_HANDLER(sle, 0x1F, 0x19, 0x04, 0x00000000, PPC_POWER_BR),
8137GEN_HANDLER(sleq, 0x1F, 0x19, 0x06, 0x00000000, PPC_POWER_BR),
8138GEN_HANDLER(sliq, 0x1F, 0x18, 0x05, 0x00000000, PPC_POWER_BR),
8139GEN_HANDLER(slliq, 0x1F, 0x18, 0x07, 0x00000000, PPC_POWER_BR),
8140GEN_HANDLER(sllq, 0x1F, 0x18, 0x06, 0x00000000, PPC_POWER_BR),
8141GEN_HANDLER(slq, 0x1F, 0x18, 0x04, 0x00000000, PPC_POWER_BR),
8142GEN_HANDLER(sraiq, 0x1F, 0x18, 0x1D, 0x00000000, PPC_POWER_BR),
8143GEN_HANDLER(sraq, 0x1F, 0x18, 0x1C, 0x00000000, PPC_POWER_BR),
8144GEN_HANDLER(sre, 0x1F, 0x19, 0x14, 0x00000000, PPC_POWER_BR),
8145GEN_HANDLER(srea, 0x1F, 0x19, 0x1C, 0x00000000, PPC_POWER_BR),
8146GEN_HANDLER(sreq, 0x1F, 0x19, 0x16, 0x00000000, PPC_POWER_BR),
8147GEN_HANDLER(sriq, 0x1F, 0x18, 0x15, 0x00000000, PPC_POWER_BR),
8148GEN_HANDLER(srliq, 0x1F, 0x18, 0x17, 0x00000000, PPC_POWER_BR),
8149GEN_HANDLER(srlq, 0x1F, 0x18, 0x16, 0x00000000, PPC_POWER_BR),
8150GEN_HANDLER(srq, 0x1F, 0x18, 0x14, 0x00000000, PPC_POWER_BR),
8151GEN_HANDLER(dsa, 0x1F, 0x14, 0x13, 0x03FFF801, PPC_602_SPEC),
8152GEN_HANDLER(esa, 0x1F, 0x14, 0x12, 0x03FFF801, PPC_602_SPEC),
8153GEN_HANDLER(mfrom, 0x1F, 0x09, 0x08, 0x03E0F801, PPC_602_SPEC),
8154GEN_HANDLER2(tlbld_6xx, "tlbld", 0x1F, 0x12, 0x1E, 0x03FF0001, PPC_6xx_TLB),
8155GEN_HANDLER2(tlbli_6xx, "tlbli", 0x1F, 0x12, 0x1F, 0x03FF0001, PPC_6xx_TLB),
8156GEN_HANDLER2(tlbld_74xx, "tlbld", 0x1F, 0x12, 0x1E, 0x03FF0001, PPC_74xx_TLB),
8157GEN_HANDLER2(tlbli_74xx, "tlbli", 0x1F, 0x12, 0x1F, 0x03FF0001, PPC_74xx_TLB),
8158GEN_HANDLER(clf, 0x1F, 0x16, 0x03, 0x03E00000, PPC_POWER),
8159GEN_HANDLER(cli, 0x1F, 0x16, 0x0F, 0x03E00000, PPC_POWER),
8160GEN_HANDLER(dclst, 0x1F, 0x16, 0x13, 0x03E00000, PPC_POWER),
8161GEN_HANDLER(mfsri, 0x1F, 0x13, 0x13, 0x00000001, PPC_POWER),
8162GEN_HANDLER(rac, 0x1F, 0x12, 0x19, 0x00000001, PPC_POWER),
8163GEN_HANDLER(rfsvc, 0x13, 0x12, 0x02, 0x03FFF0001, PPC_POWER),
8164GEN_HANDLER(lfq, 0x38, 0xFF, 0xFF, 0x00000003, PPC_POWER2),
8165GEN_HANDLER(lfqu, 0x39, 0xFF, 0xFF, 0x00000003, PPC_POWER2),
8166GEN_HANDLER(lfqux, 0x1F, 0x17, 0x19, 0x00000001, PPC_POWER2),
8167GEN_HANDLER(lfqx, 0x1F, 0x17, 0x18, 0x00000001, PPC_POWER2),
8168GEN_HANDLER(stfq, 0x3C, 0xFF, 0xFF, 0x00000003, PPC_POWER2),
8169GEN_HANDLER(stfqu, 0x3D, 0xFF, 0xFF, 0x00000003, PPC_POWER2),
8170GEN_HANDLER(stfqux, 0x1F, 0x17, 0x1D, 0x00000001, PPC_POWER2),
8171GEN_HANDLER(stfqx, 0x1F, 0x17, 0x1C, 0x00000001, PPC_POWER2),
8172GEN_HANDLER(mfapidi, 0x1F, 0x13, 0x08, 0x0000F801, PPC_MFAPIDI),
8173GEN_HANDLER(tlbiva, 0x1F, 0x12, 0x18, 0x03FFF801, PPC_TLBIVA),
8174GEN_HANDLER(mfdcr, 0x1F, 0x03, 0x0A, 0x00000001, PPC_DCR),
8175GEN_HANDLER(mtdcr, 0x1F, 0x03, 0x0E, 0x00000001, PPC_DCR),
8176GEN_HANDLER(mfdcrx, 0x1F, 0x03, 0x08, 0x00000000, PPC_DCRX),
8177GEN_HANDLER(mtdcrx, 0x1F, 0x03, 0x0C, 0x00000000, PPC_DCRX),
8178GEN_HANDLER(mfdcrux, 0x1F, 0x03, 0x09, 0x00000000, PPC_DCRUX),
8179GEN_HANDLER(mtdcrux, 0x1F, 0x03, 0x0D, 0x00000000, PPC_DCRUX),
8180GEN_HANDLER(dccci, 0x1F, 0x06, 0x0E, 0x03E00001, PPC_4xx_COMMON),
8181GEN_HANDLER(dcread, 0x1F, 0x06, 0x0F, 0x00000001, PPC_4xx_COMMON),
8182GEN_HANDLER2(icbt_40x, "icbt", 0x1F, 0x06, 0x08, 0x03E00001, PPC_40x_ICBT),
8183GEN_HANDLER(iccci, 0x1F, 0x06, 0x1E, 0x00000001, PPC_4xx_COMMON),
8184GEN_HANDLER(icread, 0x1F, 0x06, 0x1F, 0x03E00001, PPC_4xx_COMMON),
8185GEN_HANDLER2(rfci_40x, "rfci", 0x13, 0x13, 0x01, 0x03FF8001, PPC_40x_EXCP),
8186GEN_HANDLER(rfci, 0x13, 0x13, 0x01, 0x03FF8001, PPC_BOOKE),
8187GEN_HANDLER(rfdi, 0x13, 0x07, 0x01, 0x03FF8001, PPC_RFDI),
8188GEN_HANDLER(rfmci, 0x13, 0x06, 0x01, 0x03FF8001, PPC_RFMCI),
8189GEN_HANDLER2(tlbre_40x, "tlbre", 0x1F, 0x12, 0x1D, 0x00000001, PPC_40x_TLB),
8190GEN_HANDLER2(tlbsx_40x, "tlbsx", 0x1F, 0x12, 0x1C, 0x00000000, PPC_40x_TLB),
8191GEN_HANDLER2(tlbwe_40x, "tlbwe", 0x1F, 0x12, 0x1E, 0x00000001, PPC_40x_TLB),
8192GEN_HANDLER2(tlbre_440, "tlbre", 0x1F, 0x12, 0x1D, 0x00000001, PPC_BOOKE),
8193GEN_HANDLER2(tlbsx_440, "tlbsx", 0x1F, 0x12, 0x1C, 0x00000000, PPC_BOOKE),
8194GEN_HANDLER2(tlbwe_440, "tlbwe", 0x1F, 0x12, 0x1E, 0x00000001, PPC_BOOKE),
8195GEN_HANDLER(wrtee, 0x1F, 0x03, 0x04, 0x000FFC01, PPC_WRTEE),
fbe73008 8196GEN_HANDLER(wrteei, 0x1F, 0x03, 0x05, 0x000E7C01, PPC_WRTEE),
5c55ff99
BS
8197GEN_HANDLER(dlmzb, 0x1F, 0x0E, 0x02, 0x00000000, PPC_440_SPEC),
8198GEN_HANDLER(mbar, 0x1F, 0x16, 0x1a, 0x001FF801, PPC_BOOKE),
8199GEN_HANDLER(msync, 0x1F, 0x16, 0x12, 0x03FFF801, PPC_BOOKE),
8200GEN_HANDLER2(icbt_440, "icbt", 0x1F, 0x16, 0x00, 0x03E00001, PPC_BOOKE),
8201GEN_HANDLER(lvsl, 0x1f, 0x06, 0x00, 0x00000001, PPC_ALTIVEC),
8202GEN_HANDLER(lvsr, 0x1f, 0x06, 0x01, 0x00000001, PPC_ALTIVEC),
8203GEN_HANDLER(mfvscr, 0x04, 0x2, 0x18, 0x001ff800, PPC_ALTIVEC),
8204GEN_HANDLER(mtvscr, 0x04, 0x2, 0x19, 0x03ff0000, PPC_ALTIVEC),
8205GEN_HANDLER(vsldoi, 0x04, 0x16, 0xFF, 0x00000400, PPC_ALTIVEC),
8206GEN_HANDLER(vmladduhm, 0x04, 0x11, 0xFF, 0x00000000, PPC_ALTIVEC),
8207GEN_HANDLER2(evsel0, "evsel", 0x04, 0x1c, 0x09, 0x00000000, PPC_SPE),
8208GEN_HANDLER2(evsel1, "evsel", 0x04, 0x1d, 0x09, 0x00000000, PPC_SPE),
8209GEN_HANDLER2(evsel2, "evsel", 0x04, 0x1e, 0x09, 0x00000000, PPC_SPE),
8210GEN_HANDLER2(evsel3, "evsel", 0x04, 0x1f, 0x09, 0x00000000, PPC_SPE),
8211
8212#undef GEN_INT_ARITH_ADD
8213#undef GEN_INT_ARITH_ADD_CONST
8214#define GEN_INT_ARITH_ADD(name, opc3, add_ca, compute_ca, compute_ov) \
8215GEN_HANDLER(name, 0x1F, 0x0A, opc3, 0x00000000, PPC_INTEGER),
8216#define GEN_INT_ARITH_ADD_CONST(name, opc3, const_val, \
8217 add_ca, compute_ca, compute_ov) \
8218GEN_HANDLER(name, 0x1F, 0x0A, opc3, 0x0000F800, PPC_INTEGER),
8219GEN_INT_ARITH_ADD(add, 0x08, 0, 0, 0)
8220GEN_INT_ARITH_ADD(addo, 0x18, 0, 0, 1)
8221GEN_INT_ARITH_ADD(addc, 0x00, 0, 1, 0)
8222GEN_INT_ARITH_ADD(addco, 0x10, 0, 1, 1)
8223GEN_INT_ARITH_ADD(adde, 0x04, 1, 1, 0)
8224GEN_INT_ARITH_ADD(addeo, 0x14, 1, 1, 1)
8225GEN_INT_ARITH_ADD_CONST(addme, 0x07, -1LL, 1, 1, 0)
8226GEN_INT_ARITH_ADD_CONST(addmeo, 0x17, -1LL, 1, 1, 1)
8227GEN_INT_ARITH_ADD_CONST(addze, 0x06, 0, 1, 1, 0)
8228GEN_INT_ARITH_ADD_CONST(addzeo, 0x16, 0, 1, 1, 1)
8229
8230#undef GEN_INT_ARITH_DIVW
8231#define GEN_INT_ARITH_DIVW(name, opc3, sign, compute_ov) \
8232GEN_HANDLER(name, 0x1F, 0x0B, opc3, 0x00000000, PPC_INTEGER)
8233GEN_INT_ARITH_DIVW(divwu, 0x0E, 0, 0),
8234GEN_INT_ARITH_DIVW(divwuo, 0x1E, 0, 1),
8235GEN_INT_ARITH_DIVW(divw, 0x0F, 1, 0),
8236GEN_INT_ARITH_DIVW(divwo, 0x1F, 1, 1),
8237
8238#if defined(TARGET_PPC64)
8239#undef GEN_INT_ARITH_DIVD
8240#define GEN_INT_ARITH_DIVD(name, opc3, sign, compute_ov) \
8241GEN_HANDLER(name, 0x1F, 0x09, opc3, 0x00000000, PPC_64B)
8242GEN_INT_ARITH_DIVD(divdu, 0x0E, 0, 0),
8243GEN_INT_ARITH_DIVD(divduo, 0x1E, 0, 1),
8244GEN_INT_ARITH_DIVD(divd, 0x0F, 1, 0),
8245GEN_INT_ARITH_DIVD(divdo, 0x1F, 1, 1),
8246
8247#undef GEN_INT_ARITH_MUL_HELPER
8248#define GEN_INT_ARITH_MUL_HELPER(name, opc3) \
8249GEN_HANDLER(name, 0x1F, 0x09, opc3, 0x00000000, PPC_64B)
8250GEN_INT_ARITH_MUL_HELPER(mulhdu, 0x00),
8251GEN_INT_ARITH_MUL_HELPER(mulhd, 0x02),
8252GEN_INT_ARITH_MUL_HELPER(mulldo, 0x17),
8253#endif
8254
8255#undef GEN_INT_ARITH_SUBF
8256#undef GEN_INT_ARITH_SUBF_CONST
8257#define GEN_INT_ARITH_SUBF(name, opc3, add_ca, compute_ca, compute_ov) \
8258GEN_HANDLER(name, 0x1F, 0x08, opc3, 0x00000000, PPC_INTEGER),
8259#define GEN_INT_ARITH_SUBF_CONST(name, opc3, const_val, \
8260 add_ca, compute_ca, compute_ov) \
8261GEN_HANDLER(name, 0x1F, 0x08, opc3, 0x0000F800, PPC_INTEGER),
8262GEN_INT_ARITH_SUBF(subf, 0x01, 0, 0, 0)
8263GEN_INT_ARITH_SUBF(subfo, 0x11, 0, 0, 1)
8264GEN_INT_ARITH_SUBF(subfc, 0x00, 0, 1, 0)
8265GEN_INT_ARITH_SUBF(subfco, 0x10, 0, 1, 1)
8266GEN_INT_ARITH_SUBF(subfe, 0x04, 1, 1, 0)
8267GEN_INT_ARITH_SUBF(subfeo, 0x14, 1, 1, 1)
8268GEN_INT_ARITH_SUBF_CONST(subfme, 0x07, -1LL, 1, 1, 0)
8269GEN_INT_ARITH_SUBF_CONST(subfmeo, 0x17, -1LL, 1, 1, 1)
8270GEN_INT_ARITH_SUBF_CONST(subfze, 0x06, 0, 1, 1, 0)
8271GEN_INT_ARITH_SUBF_CONST(subfzeo, 0x16, 0, 1, 1, 1)
8272
8273#undef GEN_LOGICAL1
8274#undef GEN_LOGICAL2
8275#define GEN_LOGICAL2(name, tcg_op, opc, type) \
8276GEN_HANDLER(name, 0x1F, 0x1C, opc, 0x00000000, type)
8277#define GEN_LOGICAL1(name, tcg_op, opc, type) \
8278GEN_HANDLER(name, 0x1F, 0x1A, opc, 0x00000000, type)
8279GEN_LOGICAL2(and, tcg_gen_and_tl, 0x00, PPC_INTEGER),
8280GEN_LOGICAL2(andc, tcg_gen_andc_tl, 0x01, PPC_INTEGER),
8281GEN_LOGICAL2(eqv, tcg_gen_eqv_tl, 0x08, PPC_INTEGER),
8282GEN_LOGICAL1(extsb, tcg_gen_ext8s_tl, 0x1D, PPC_INTEGER),
8283GEN_LOGICAL1(extsh, tcg_gen_ext16s_tl, 0x1C, PPC_INTEGER),
8284GEN_LOGICAL2(nand, tcg_gen_nand_tl, 0x0E, PPC_INTEGER),
8285GEN_LOGICAL2(nor, tcg_gen_nor_tl, 0x03, PPC_INTEGER),
8286GEN_LOGICAL2(orc, tcg_gen_orc_tl, 0x0C, PPC_INTEGER),
8287#if defined(TARGET_PPC64)
8288GEN_LOGICAL1(extsw, tcg_gen_ext32s_tl, 0x1E, PPC_64B),
8289#endif
8290
8291#if defined(TARGET_PPC64)
8292#undef GEN_PPC64_R2
8293#undef GEN_PPC64_R4
8294#define GEN_PPC64_R2(name, opc1, opc2) \
8295GEN_HANDLER2(name##0, stringify(name), opc1, opc2, 0xFF, 0x00000000, PPC_64B),\
8296GEN_HANDLER2(name##1, stringify(name), opc1, opc2 | 0x10, 0xFF, 0x00000000, \
8297 PPC_64B)
8298#define GEN_PPC64_R4(name, opc1, opc2) \
8299GEN_HANDLER2(name##0, stringify(name), opc1, opc2, 0xFF, 0x00000000, PPC_64B),\
8300GEN_HANDLER2(name##1, stringify(name), opc1, opc2 | 0x01, 0xFF, 0x00000000, \
8301 PPC_64B), \
8302GEN_HANDLER2(name##2, stringify(name), opc1, opc2 | 0x10, 0xFF, 0x00000000, \
8303 PPC_64B), \
8304GEN_HANDLER2(name##3, stringify(name), opc1, opc2 | 0x11, 0xFF, 0x00000000, \
8305 PPC_64B)
8306GEN_PPC64_R4(rldicl, 0x1E, 0x00),
8307GEN_PPC64_R4(rldicr, 0x1E, 0x02),
8308GEN_PPC64_R4(rldic, 0x1E, 0x04),
8309GEN_PPC64_R2(rldcl, 0x1E, 0x08),
8310GEN_PPC64_R2(rldcr, 0x1E, 0x09),
8311GEN_PPC64_R4(rldimi, 0x1E, 0x06),
8312#endif
8313
8314#undef _GEN_FLOAT_ACB
8315#undef GEN_FLOAT_ACB
8316#undef _GEN_FLOAT_AB
8317#undef GEN_FLOAT_AB
8318#undef _GEN_FLOAT_AC
8319#undef GEN_FLOAT_AC
8320#undef GEN_FLOAT_B
8321#undef GEN_FLOAT_BS
8322#define _GEN_FLOAT_ACB(name, op, op1, op2, isfloat, set_fprf, type) \
8323GEN_HANDLER(f##name, op1, op2, 0xFF, 0x00000000, type)
8324#define GEN_FLOAT_ACB(name, op2, set_fprf, type) \
8325_GEN_FLOAT_ACB(name, name, 0x3F, op2, 0, set_fprf, type), \
8326_GEN_FLOAT_ACB(name##s, name, 0x3B, op2, 1, set_fprf, type)
8327#define _GEN_FLOAT_AB(name, op, op1, op2, inval, isfloat, set_fprf, type) \
8328GEN_HANDLER(f##name, op1, op2, 0xFF, inval, type)
8329#define GEN_FLOAT_AB(name, op2, inval, set_fprf, type) \
8330_GEN_FLOAT_AB(name, name, 0x3F, op2, inval, 0, set_fprf, type), \
8331_GEN_FLOAT_AB(name##s, name, 0x3B, op2, inval, 1, set_fprf, type)
8332#define _GEN_FLOAT_AC(name, op, op1, op2, inval, isfloat, set_fprf, type) \
8333GEN_HANDLER(f##name, op1, op2, 0xFF, inval, type)
8334#define GEN_FLOAT_AC(name, op2, inval, set_fprf, type) \
8335_GEN_FLOAT_AC(name, name, 0x3F, op2, inval, 0, set_fprf, type), \
8336_GEN_FLOAT_AC(name##s, name, 0x3B, op2, inval, 1, set_fprf, type)
8337#define GEN_FLOAT_B(name, op2, op3, set_fprf, type) \
8338GEN_HANDLER(f##name, 0x3F, op2, op3, 0x001F0000, type)
8339#define GEN_FLOAT_BS(name, op1, op2, set_fprf, type) \
8340GEN_HANDLER(f##name, op1, op2, 0xFF, 0x001F07C0, type)
8341
8342GEN_FLOAT_AB(add, 0x15, 0x000007C0, 1, PPC_FLOAT),
8343GEN_FLOAT_AB(div, 0x12, 0x000007C0, 1, PPC_FLOAT),
8344GEN_FLOAT_AC(mul, 0x19, 0x0000F800, 1, PPC_FLOAT),
8345GEN_FLOAT_BS(re, 0x3F, 0x18, 1, PPC_FLOAT_EXT),
8346GEN_FLOAT_BS(res, 0x3B, 0x18, 1, PPC_FLOAT_FRES),
8347GEN_FLOAT_BS(rsqrte, 0x3F, 0x1A, 1, PPC_FLOAT_FRSQRTE),
8348_GEN_FLOAT_ACB(sel, sel, 0x3F, 0x17, 0, 0, PPC_FLOAT_FSEL),
8349GEN_FLOAT_AB(sub, 0x14, 0x000007C0, 1, PPC_FLOAT),
8350GEN_FLOAT_ACB(madd, 0x1D, 1, PPC_FLOAT),
8351GEN_FLOAT_ACB(msub, 0x1C, 1, PPC_FLOAT),
8352GEN_FLOAT_ACB(nmadd, 0x1F, 1, PPC_FLOAT),
8353GEN_FLOAT_ACB(nmsub, 0x1E, 1, PPC_FLOAT),
8354GEN_FLOAT_B(ctiw, 0x0E, 0x00, 0, PPC_FLOAT),
8355GEN_FLOAT_B(ctiwz, 0x0F, 0x00, 0, PPC_FLOAT),
8356GEN_FLOAT_B(rsp, 0x0C, 0x00, 1, PPC_FLOAT),
8357#if defined(TARGET_PPC64)
8358GEN_FLOAT_B(cfid, 0x0E, 0x1A, 1, PPC_64B),
8359GEN_FLOAT_B(ctid, 0x0E, 0x19, 0, PPC_64B),
8360GEN_FLOAT_B(ctidz, 0x0F, 0x19, 0, PPC_64B),
8361#endif
8362GEN_FLOAT_B(rin, 0x08, 0x0C, 1, PPC_FLOAT_EXT),
8363GEN_FLOAT_B(riz, 0x08, 0x0D, 1, PPC_FLOAT_EXT),
8364GEN_FLOAT_B(rip, 0x08, 0x0E, 1, PPC_FLOAT_EXT),
8365GEN_FLOAT_B(rim, 0x08, 0x0F, 1, PPC_FLOAT_EXT),
8366GEN_FLOAT_B(abs, 0x08, 0x08, 0, PPC_FLOAT),
8367GEN_FLOAT_B(nabs, 0x08, 0x04, 0, PPC_FLOAT),
8368GEN_FLOAT_B(neg, 0x08, 0x01, 0, PPC_FLOAT),
8369
8370#undef GEN_LD
8371#undef GEN_LDU
8372#undef GEN_LDUX
8373#undef GEN_LDX
8374#undef GEN_LDS
8375#define GEN_LD(name, ldop, opc, type) \
8376GEN_HANDLER(name, opc, 0xFF, 0xFF, 0x00000000, type),
8377#define GEN_LDU(name, ldop, opc, type) \
8378GEN_HANDLER(name##u, opc, 0xFF, 0xFF, 0x00000000, type),
8379#define GEN_LDUX(name, ldop, opc2, opc3, type) \
8380GEN_HANDLER(name##ux, 0x1F, opc2, opc3, 0x00000001, type),
8381#define GEN_LDX(name, ldop, opc2, opc3, type) \
8382GEN_HANDLER(name##x, 0x1F, opc2, opc3, 0x00000001, type),
8383#define GEN_LDS(name, ldop, op, type) \
8384GEN_LD(name, ldop, op | 0x20, type) \
8385GEN_LDU(name, ldop, op | 0x21, type) \
8386GEN_LDUX(name, ldop, 0x17, op | 0x01, type) \
8387GEN_LDX(name, ldop, 0x17, op | 0x00, type)
8388
8389GEN_LDS(lbz, ld8u, 0x02, PPC_INTEGER)
8390GEN_LDS(lha, ld16s, 0x0A, PPC_INTEGER)
8391GEN_LDS(lhz, ld16u, 0x08, PPC_INTEGER)
8392GEN_LDS(lwz, ld32u, 0x00, PPC_INTEGER)
8393#if defined(TARGET_PPC64)
8394GEN_LDUX(lwa, ld32s, 0x15, 0x0B, PPC_64B)
8395GEN_LDX(lwa, ld32s, 0x15, 0x0A, PPC_64B)
8396GEN_LDUX(ld, ld64, 0x15, 0x01, PPC_64B)
8397GEN_LDX(ld, ld64, 0x15, 0x00, PPC_64B)
8398#endif
8399GEN_LDX(lhbr, ld16ur, 0x16, 0x18, PPC_INTEGER)
8400GEN_LDX(lwbr, ld32ur, 0x16, 0x10, PPC_INTEGER)
8401
8402#undef GEN_ST
8403#undef GEN_STU
8404#undef GEN_STUX
8405#undef GEN_STX
8406#undef GEN_STS
8407#define GEN_ST(name, stop, opc, type) \
8408GEN_HANDLER(name, opc, 0xFF, 0xFF, 0x00000000, type),
8409#define GEN_STU(name, stop, opc, type) \
8410GEN_HANDLER(stop##u, opc, 0xFF, 0xFF, 0x00000000, type),
8411#define GEN_STUX(name, stop, opc2, opc3, type) \
8412GEN_HANDLER(name##ux, 0x1F, opc2, opc3, 0x00000001, type),
8413#define GEN_STX(name, stop, opc2, opc3, type) \
8414GEN_HANDLER(name##x, 0x1F, opc2, opc3, 0x00000001, type),
8415#define GEN_STS(name, stop, op, type) \
8416GEN_ST(name, stop, op | 0x20, type) \
8417GEN_STU(name, stop, op | 0x21, type) \
8418GEN_STUX(name, stop, 0x17, op | 0x01, type) \
8419GEN_STX(name, stop, 0x17, op | 0x00, type)
8420
8421GEN_STS(stb, st8, 0x06, PPC_INTEGER)
8422GEN_STS(sth, st16, 0x0C, PPC_INTEGER)
8423GEN_STS(stw, st32, 0x04, PPC_INTEGER)
8424#if defined(TARGET_PPC64)
8425GEN_STUX(std, st64, 0x15, 0x05, PPC_64B)
8426GEN_STX(std, st64, 0x15, 0x04, PPC_64B)
8427#endif
8428GEN_STX(sthbr, st16r, 0x16, 0x1C, PPC_INTEGER)
8429GEN_STX(stwbr, st32r, 0x16, 0x14, PPC_INTEGER)
8430
8431#undef GEN_LDF
8432#undef GEN_LDUF
8433#undef GEN_LDUXF
8434#undef GEN_LDXF
8435#undef GEN_LDFS
8436#define GEN_LDF(name, ldop, opc, type) \
8437GEN_HANDLER(name, opc, 0xFF, 0xFF, 0x00000000, type),
8438#define GEN_LDUF(name, ldop, opc, type) \
8439GEN_HANDLER(name##u, opc, 0xFF, 0xFF, 0x00000000, type),
8440#define GEN_LDUXF(name, ldop, opc, type) \
8441GEN_HANDLER(name##ux, 0x1F, 0x17, opc, 0x00000001, type),
8442#define GEN_LDXF(name, ldop, opc2, opc3, type) \
8443GEN_HANDLER(name##x, 0x1F, opc2, opc3, 0x00000001, type),
8444#define GEN_LDFS(name, ldop, op, type) \
8445GEN_LDF(name, ldop, op | 0x20, type) \
8446GEN_LDUF(name, ldop, op | 0x21, type) \
8447GEN_LDUXF(name, ldop, op | 0x01, type) \
8448GEN_LDXF(name, ldop, 0x17, op | 0x00, type)
8449
8450GEN_LDFS(lfd, ld64, 0x12, PPC_FLOAT)
8451GEN_LDFS(lfs, ld32fs, 0x10, PPC_FLOAT)
8452
8453#undef GEN_STF
8454#undef GEN_STUF
8455#undef GEN_STUXF
8456#undef GEN_STXF
8457#undef GEN_STFS
8458#define GEN_STF(name, stop, opc, type) \
8459GEN_HANDLER(name, opc, 0xFF, 0xFF, 0x00000000, type),
8460#define GEN_STUF(name, stop, opc, type) \
8461GEN_HANDLER(name##u, opc, 0xFF, 0xFF, 0x00000000, type),
8462#define GEN_STUXF(name, stop, opc, type) \
8463GEN_HANDLER(name##ux, 0x1F, 0x17, opc, 0x00000001, type),
8464#define GEN_STXF(name, stop, opc2, opc3, type) \
8465GEN_HANDLER(name##x, 0x1F, opc2, opc3, 0x00000001, type),
8466#define GEN_STFS(name, stop, op, type) \
8467GEN_STF(name, stop, op | 0x20, type) \
8468GEN_STUF(name, stop, op | 0x21, type) \
8469GEN_STUXF(name, stop, op | 0x01, type) \
8470GEN_STXF(name, stop, 0x17, op | 0x00, type)
8471
8472GEN_STFS(stfd, st64, 0x16, PPC_FLOAT)
8473GEN_STFS(stfs, st32fs, 0x14, PPC_FLOAT)
8474GEN_STXF(stfiw, st32fiw, 0x17, 0x1E, PPC_FLOAT_STFIWX)
8475
8476#undef GEN_CRLOGIC
8477#define GEN_CRLOGIC(name, tcg_op, opc) \
8478GEN_HANDLER(name, 0x13, 0x01, opc, 0x00000001, PPC_INTEGER)
8479GEN_CRLOGIC(crand, tcg_gen_and_i32, 0x08),
8480GEN_CRLOGIC(crandc, tcg_gen_andc_i32, 0x04),
8481GEN_CRLOGIC(creqv, tcg_gen_eqv_i32, 0x09),
8482GEN_CRLOGIC(crnand, tcg_gen_nand_i32, 0x07),
8483GEN_CRLOGIC(crnor, tcg_gen_nor_i32, 0x01),
8484GEN_CRLOGIC(cror, tcg_gen_or_i32, 0x0E),
8485GEN_CRLOGIC(crorc, tcg_gen_orc_i32, 0x0D),
8486GEN_CRLOGIC(crxor, tcg_gen_xor_i32, 0x06),
8487
8488#undef GEN_MAC_HANDLER
8489#define GEN_MAC_HANDLER(name, opc2, opc3) \
8490GEN_HANDLER(name, 0x04, opc2, opc3, 0x00000000, PPC_405_MAC)
8491GEN_MAC_HANDLER(macchw, 0x0C, 0x05),
8492GEN_MAC_HANDLER(macchwo, 0x0C, 0x15),
8493GEN_MAC_HANDLER(macchws, 0x0C, 0x07),
8494GEN_MAC_HANDLER(macchwso, 0x0C, 0x17),
8495GEN_MAC_HANDLER(macchwsu, 0x0C, 0x06),
8496GEN_MAC_HANDLER(macchwsuo, 0x0C, 0x16),
8497GEN_MAC_HANDLER(macchwu, 0x0C, 0x04),
8498GEN_MAC_HANDLER(macchwuo, 0x0C, 0x14),
8499GEN_MAC_HANDLER(machhw, 0x0C, 0x01),
8500GEN_MAC_HANDLER(machhwo, 0x0C, 0x11),
8501GEN_MAC_HANDLER(machhws, 0x0C, 0x03),
8502GEN_MAC_HANDLER(machhwso, 0x0C, 0x13),
8503GEN_MAC_HANDLER(machhwsu, 0x0C, 0x02),
8504GEN_MAC_HANDLER(machhwsuo, 0x0C, 0x12),
8505GEN_MAC_HANDLER(machhwu, 0x0C, 0x00),
8506GEN_MAC_HANDLER(machhwuo, 0x0C, 0x10),
8507GEN_MAC_HANDLER(maclhw, 0x0C, 0x0D),
8508GEN_MAC_HANDLER(maclhwo, 0x0C, 0x1D),
8509GEN_MAC_HANDLER(maclhws, 0x0C, 0x0F),
8510GEN_MAC_HANDLER(maclhwso, 0x0C, 0x1F),
8511GEN_MAC_HANDLER(maclhwu, 0x0C, 0x0C),
8512GEN_MAC_HANDLER(maclhwuo, 0x0C, 0x1C),
8513GEN_MAC_HANDLER(maclhwsu, 0x0C, 0x0E),
8514GEN_MAC_HANDLER(maclhwsuo, 0x0C, 0x1E),
8515GEN_MAC_HANDLER(nmacchw, 0x0E, 0x05),
8516GEN_MAC_HANDLER(nmacchwo, 0x0E, 0x15),
8517GEN_MAC_HANDLER(nmacchws, 0x0E, 0x07),
8518GEN_MAC_HANDLER(nmacchwso, 0x0E, 0x17),
8519GEN_MAC_HANDLER(nmachhw, 0x0E, 0x01),
8520GEN_MAC_HANDLER(nmachhwo, 0x0E, 0x11),
8521GEN_MAC_HANDLER(nmachhws, 0x0E, 0x03),
8522GEN_MAC_HANDLER(nmachhwso, 0x0E, 0x13),
8523GEN_MAC_HANDLER(nmaclhw, 0x0E, 0x0D),
8524GEN_MAC_HANDLER(nmaclhwo, 0x0E, 0x1D),
8525GEN_MAC_HANDLER(nmaclhws, 0x0E, 0x0F),
8526GEN_MAC_HANDLER(nmaclhwso, 0x0E, 0x1F),
8527GEN_MAC_HANDLER(mulchw, 0x08, 0x05),
8528GEN_MAC_HANDLER(mulchwu, 0x08, 0x04),
8529GEN_MAC_HANDLER(mulhhw, 0x08, 0x01),
8530GEN_MAC_HANDLER(mulhhwu, 0x08, 0x00),
8531GEN_MAC_HANDLER(mullhw, 0x08, 0x0D),
8532GEN_MAC_HANDLER(mullhwu, 0x08, 0x0C),
8533
8534#undef GEN_VR_LDX
8535#undef GEN_VR_STX
8536#undef GEN_VR_LVE
8537#undef GEN_VR_STVE
8538#define GEN_VR_LDX(name, opc2, opc3) \
8539GEN_HANDLER(name, 0x1F, opc2, opc3, 0x00000001, PPC_ALTIVEC)
8540#define GEN_VR_STX(name, opc2, opc3) \
8541GEN_HANDLER(st##name, 0x1F, opc2, opc3, 0x00000001, PPC_ALTIVEC)
8542#define GEN_VR_LVE(name, opc2, opc3) \
8543 GEN_HANDLER(lve##name, 0x1F, opc2, opc3, 0x00000001, PPC_ALTIVEC)
8544#define GEN_VR_STVE(name, opc2, opc3) \
8545 GEN_HANDLER(stve##name, 0x1F, opc2, opc3, 0x00000001, PPC_ALTIVEC)
8546GEN_VR_LDX(lvx, 0x07, 0x03),
8547GEN_VR_LDX(lvxl, 0x07, 0x0B),
8548GEN_VR_LVE(bx, 0x07, 0x00),
8549GEN_VR_LVE(hx, 0x07, 0x01),
8550GEN_VR_LVE(wx, 0x07, 0x02),
8551GEN_VR_STX(svx, 0x07, 0x07),
8552GEN_VR_STX(svxl, 0x07, 0x0F),
8553GEN_VR_STVE(bx, 0x07, 0x04),
8554GEN_VR_STVE(hx, 0x07, 0x05),
8555GEN_VR_STVE(wx, 0x07, 0x06),
8556
8557#undef GEN_VX_LOGICAL
8558#define GEN_VX_LOGICAL(name, tcg_op, opc2, opc3) \
8559GEN_HANDLER(name, 0x04, opc2, opc3, 0x00000000, PPC_ALTIVEC)
8560GEN_VX_LOGICAL(vand, tcg_gen_and_i64, 2, 16),
8561GEN_VX_LOGICAL(vandc, tcg_gen_andc_i64, 2, 17),
8562GEN_VX_LOGICAL(vor, tcg_gen_or_i64, 2, 18),
8563GEN_VX_LOGICAL(vxor, tcg_gen_xor_i64, 2, 19),
8564GEN_VX_LOGICAL(vnor, tcg_gen_nor_i64, 2, 20),
8565
8566#undef GEN_VXFORM
8567#define GEN_VXFORM(name, opc2, opc3) \
8568GEN_HANDLER(name, 0x04, opc2, opc3, 0x00000000, PPC_ALTIVEC)
8569GEN_VXFORM(vaddubm, 0, 0),
8570GEN_VXFORM(vadduhm, 0, 1),
8571GEN_VXFORM(vadduwm, 0, 2),
8572GEN_VXFORM(vsububm, 0, 16),
8573GEN_VXFORM(vsubuhm, 0, 17),
8574GEN_VXFORM(vsubuwm, 0, 18),
8575GEN_VXFORM(vmaxub, 1, 0),
8576GEN_VXFORM(vmaxuh, 1, 1),
8577GEN_VXFORM(vmaxuw, 1, 2),
8578GEN_VXFORM(vmaxsb, 1, 4),
8579GEN_VXFORM(vmaxsh, 1, 5),
8580GEN_VXFORM(vmaxsw, 1, 6),
8581GEN_VXFORM(vminub, 1, 8),
8582GEN_VXFORM(vminuh, 1, 9),
8583GEN_VXFORM(vminuw, 1, 10),
8584GEN_VXFORM(vminsb, 1, 12),
8585GEN_VXFORM(vminsh, 1, 13),
8586GEN_VXFORM(vminsw, 1, 14),
8587GEN_VXFORM(vavgub, 1, 16),
8588GEN_VXFORM(vavguh, 1, 17),
8589GEN_VXFORM(vavguw, 1, 18),
8590GEN_VXFORM(vavgsb, 1, 20),
8591GEN_VXFORM(vavgsh, 1, 21),
8592GEN_VXFORM(vavgsw, 1, 22),
8593GEN_VXFORM(vmrghb, 6, 0),
8594GEN_VXFORM(vmrghh, 6, 1),
8595GEN_VXFORM(vmrghw, 6, 2),
8596GEN_VXFORM(vmrglb, 6, 4),
8597GEN_VXFORM(vmrglh, 6, 5),
8598GEN_VXFORM(vmrglw, 6, 6),
8599GEN_VXFORM(vmuloub, 4, 0),
8600GEN_VXFORM(vmulouh, 4, 1),
8601GEN_VXFORM(vmulosb, 4, 4),
8602GEN_VXFORM(vmulosh, 4, 5),
8603GEN_VXFORM(vmuleub, 4, 8),
8604GEN_VXFORM(vmuleuh, 4, 9),
8605GEN_VXFORM(vmulesb, 4, 12),
8606GEN_VXFORM(vmulesh, 4, 13),
8607GEN_VXFORM(vslb, 2, 4),
8608GEN_VXFORM(vslh, 2, 5),
8609GEN_VXFORM(vslw, 2, 6),
8610GEN_VXFORM(vsrb, 2, 8),
8611GEN_VXFORM(vsrh, 2, 9),
8612GEN_VXFORM(vsrw, 2, 10),
8613GEN_VXFORM(vsrab, 2, 12),
8614GEN_VXFORM(vsrah, 2, 13),
8615GEN_VXFORM(vsraw, 2, 14),
8616GEN_VXFORM(vslo, 6, 16),
8617GEN_VXFORM(vsro, 6, 17),
8618GEN_VXFORM(vaddcuw, 0, 6),
8619GEN_VXFORM(vsubcuw, 0, 22),
8620GEN_VXFORM(vaddubs, 0, 8),
8621GEN_VXFORM(vadduhs, 0, 9),
8622GEN_VXFORM(vadduws, 0, 10),
8623GEN_VXFORM(vaddsbs, 0, 12),
8624GEN_VXFORM(vaddshs, 0, 13),
8625GEN_VXFORM(vaddsws, 0, 14),
8626GEN_VXFORM(vsububs, 0, 24),
8627GEN_VXFORM(vsubuhs, 0, 25),
8628GEN_VXFORM(vsubuws, 0, 26),
8629GEN_VXFORM(vsubsbs, 0, 28),
8630GEN_VXFORM(vsubshs, 0, 29),
8631GEN_VXFORM(vsubsws, 0, 30),
8632GEN_VXFORM(vrlb, 2, 0),
8633GEN_VXFORM(vrlh, 2, 1),
8634GEN_VXFORM(vrlw, 2, 2),
8635GEN_VXFORM(vsl, 2, 7),
8636GEN_VXFORM(vsr, 2, 11),
8637GEN_VXFORM(vpkuhum, 7, 0),
8638GEN_VXFORM(vpkuwum, 7, 1),
8639GEN_VXFORM(vpkuhus, 7, 2),
8640GEN_VXFORM(vpkuwus, 7, 3),
8641GEN_VXFORM(vpkshus, 7, 4),
8642GEN_VXFORM(vpkswus, 7, 5),
8643GEN_VXFORM(vpkshss, 7, 6),
8644GEN_VXFORM(vpkswss, 7, 7),
8645GEN_VXFORM(vpkpx, 7, 12),
8646GEN_VXFORM(vsum4ubs, 4, 24),
8647GEN_VXFORM(vsum4sbs, 4, 28),
8648GEN_VXFORM(vsum4shs, 4, 25),
8649GEN_VXFORM(vsum2sws, 4, 26),
8650GEN_VXFORM(vsumsws, 4, 30),
8651GEN_VXFORM(vaddfp, 5, 0),
8652GEN_VXFORM(vsubfp, 5, 1),
8653GEN_VXFORM(vmaxfp, 5, 16),
8654GEN_VXFORM(vminfp, 5, 17),
8655
8656#undef GEN_VXRFORM1
8657#undef GEN_VXRFORM
8658#define GEN_VXRFORM1(opname, name, str, opc2, opc3) \
8659 GEN_HANDLER2(name, str, 0x4, opc2, opc3, 0x00000000, PPC_ALTIVEC),
8660#define GEN_VXRFORM(name, opc2, opc3) \
8661 GEN_VXRFORM1(name, name, #name, opc2, opc3) \
8662 GEN_VXRFORM1(name##_dot, name##_, #name ".", opc2, (opc3 | (0x1 << 4)))
8663GEN_VXRFORM(vcmpequb, 3, 0)
8664GEN_VXRFORM(vcmpequh, 3, 1)
8665GEN_VXRFORM(vcmpequw, 3, 2)
8666GEN_VXRFORM(vcmpgtsb, 3, 12)
8667GEN_VXRFORM(vcmpgtsh, 3, 13)
8668GEN_VXRFORM(vcmpgtsw, 3, 14)
8669GEN_VXRFORM(vcmpgtub, 3, 8)
8670GEN_VXRFORM(vcmpgtuh, 3, 9)
8671GEN_VXRFORM(vcmpgtuw, 3, 10)
8672GEN_VXRFORM(vcmpeqfp, 3, 3)
8673GEN_VXRFORM(vcmpgefp, 3, 7)
8674GEN_VXRFORM(vcmpgtfp, 3, 11)
8675GEN_VXRFORM(vcmpbfp, 3, 15)
8676
8677#undef GEN_VXFORM_SIMM
8678#define GEN_VXFORM_SIMM(name, opc2, opc3) \
8679 GEN_HANDLER(name, 0x04, opc2, opc3, 0x00000000, PPC_ALTIVEC)
8680GEN_VXFORM_SIMM(vspltisb, 6, 12),
8681GEN_VXFORM_SIMM(vspltish, 6, 13),
8682GEN_VXFORM_SIMM(vspltisw, 6, 14),
8683
8684#undef GEN_VXFORM_NOA
8685#define GEN_VXFORM_NOA(name, opc2, opc3) \
8686 GEN_HANDLER(name, 0x04, opc2, opc3, 0x001f0000, PPC_ALTIVEC)
8687GEN_VXFORM_NOA(vupkhsb, 7, 8),
8688GEN_VXFORM_NOA(vupkhsh, 7, 9),
8689GEN_VXFORM_NOA(vupklsb, 7, 10),
8690GEN_VXFORM_NOA(vupklsh, 7, 11),
8691GEN_VXFORM_NOA(vupkhpx, 7, 13),
8692GEN_VXFORM_NOA(vupklpx, 7, 15),
8693GEN_VXFORM_NOA(vrefp, 5, 4),
8694GEN_VXFORM_NOA(vrsqrtefp, 5, 5),
8695GEN_VXFORM_NOA(vlogefp, 5, 7),
8696GEN_VXFORM_NOA(vrfim, 5, 8),
8697GEN_VXFORM_NOA(vrfin, 5, 9),
8698GEN_VXFORM_NOA(vrfip, 5, 10),
8699GEN_VXFORM_NOA(vrfiz, 5, 11),
8700
8701#undef GEN_VXFORM_UIMM
8702#define GEN_VXFORM_UIMM(name, opc2, opc3) \
8703 GEN_HANDLER(name, 0x04, opc2, opc3, 0x00000000, PPC_ALTIVEC)
8704GEN_VXFORM_UIMM(vspltb, 6, 8),
8705GEN_VXFORM_UIMM(vsplth, 6, 9),
8706GEN_VXFORM_UIMM(vspltw, 6, 10),
8707GEN_VXFORM_UIMM(vcfux, 5, 12),
8708GEN_VXFORM_UIMM(vcfsx, 5, 13),
8709GEN_VXFORM_UIMM(vctuxs, 5, 14),
8710GEN_VXFORM_UIMM(vctsxs, 5, 15),
8711
8712#undef GEN_VAFORM_PAIRED
8713#define GEN_VAFORM_PAIRED(name0, name1, opc2) \
8714 GEN_HANDLER(name0##_##name1, 0x04, opc2, 0xFF, 0x00000000, PPC_ALTIVEC)
8715GEN_VAFORM_PAIRED(vmhaddshs, vmhraddshs, 16),
8716GEN_VAFORM_PAIRED(vmsumubm, vmsummbm, 18),
8717GEN_VAFORM_PAIRED(vmsumuhm, vmsumuhs, 19),
8718GEN_VAFORM_PAIRED(vmsumshm, vmsumshs, 20),
8719GEN_VAFORM_PAIRED(vsel, vperm, 21),
8720GEN_VAFORM_PAIRED(vmaddfp, vnmsubfp, 23),
8721
8722#undef GEN_SPE
8723#define GEN_SPE(name0, name1, opc2, opc3, inval, type) \
8724GEN_HANDLER(name0##_##name1, 0x04, opc2, opc3, inval, type)
8725GEN_SPE(evaddw, speundef, 0x00, 0x08, 0x00000000, PPC_SPE),
8726GEN_SPE(evaddiw, speundef, 0x01, 0x08, 0x00000000, PPC_SPE),
8727GEN_SPE(evsubfw, speundef, 0x02, 0x08, 0x00000000, PPC_SPE),
8728GEN_SPE(evsubifw, speundef, 0x03, 0x08, 0x00000000, PPC_SPE),
8729GEN_SPE(evabs, evneg, 0x04, 0x08, 0x0000F800, PPC_SPE),
8730GEN_SPE(evextsb, evextsh, 0x05, 0x08, 0x0000F800, PPC_SPE),
8731GEN_SPE(evrndw, evcntlzw, 0x06, 0x08, 0x0000F800, PPC_SPE),
8732GEN_SPE(evcntlsw, brinc, 0x07, 0x08, 0x00000000, PPC_SPE),
8733GEN_SPE(speundef, evand, 0x08, 0x08, 0x00000000, PPC_SPE),
8734GEN_SPE(evandc, speundef, 0x09, 0x08, 0x00000000, PPC_SPE),
8735GEN_SPE(evxor, evor, 0x0B, 0x08, 0x00000000, PPC_SPE),
8736GEN_SPE(evnor, eveqv, 0x0C, 0x08, 0x00000000, PPC_SPE),
8737GEN_SPE(speundef, evorc, 0x0D, 0x08, 0x00000000, PPC_SPE),
8738GEN_SPE(evnand, speundef, 0x0F, 0x08, 0x00000000, PPC_SPE),
8739GEN_SPE(evsrwu, evsrws, 0x10, 0x08, 0x00000000, PPC_SPE),
8740GEN_SPE(evsrwiu, evsrwis, 0x11, 0x08, 0x00000000, PPC_SPE),
8741GEN_SPE(evslw, speundef, 0x12, 0x08, 0x00000000, PPC_SPE),
8742GEN_SPE(evslwi, speundef, 0x13, 0x08, 0x00000000, PPC_SPE),
8743GEN_SPE(evrlw, evsplati, 0x14, 0x08, 0x00000000, PPC_SPE),
8744GEN_SPE(evrlwi, evsplatfi, 0x15, 0x08, 0x00000000, PPC_SPE),
8745GEN_SPE(evmergehi, evmergelo, 0x16, 0x08, 0x00000000, PPC_SPE),
8746GEN_SPE(evmergehilo, evmergelohi, 0x17, 0x08, 0x00000000, PPC_SPE),
8747GEN_SPE(evcmpgtu, evcmpgts, 0x18, 0x08, 0x00600000, PPC_SPE),
8748GEN_SPE(evcmpltu, evcmplts, 0x19, 0x08, 0x00600000, PPC_SPE),
8749GEN_SPE(evcmpeq, speundef, 0x1A, 0x08, 0x00600000, PPC_SPE),
8750
8751GEN_SPE(evfsadd, evfssub, 0x00, 0x0A, 0x00000000, PPC_SPE_SINGLE),
8752GEN_SPE(evfsabs, evfsnabs, 0x02, 0x0A, 0x0000F800, PPC_SPE_SINGLE),
8753GEN_SPE(evfsneg, speundef, 0x03, 0x0A, 0x0000F800, PPC_SPE_SINGLE),
8754GEN_SPE(evfsmul, evfsdiv, 0x04, 0x0A, 0x00000000, PPC_SPE_SINGLE),
8755GEN_SPE(evfscmpgt, evfscmplt, 0x06, 0x0A, 0x00600000, PPC_SPE_SINGLE),
8756GEN_SPE(evfscmpeq, speundef, 0x07, 0x0A, 0x00600000, PPC_SPE_SINGLE),
8757GEN_SPE(evfscfui, evfscfsi, 0x08, 0x0A, 0x00180000, PPC_SPE_SINGLE),
8758GEN_SPE(evfscfuf, evfscfsf, 0x09, 0x0A, 0x00180000, PPC_SPE_SINGLE),
8759GEN_SPE(evfsctui, evfsctsi, 0x0A, 0x0A, 0x00180000, PPC_SPE_SINGLE),
8760GEN_SPE(evfsctuf, evfsctsf, 0x0B, 0x0A, 0x00180000, PPC_SPE_SINGLE),
8761GEN_SPE(evfsctuiz, speundef, 0x0C, 0x0A, 0x00180000, PPC_SPE_SINGLE),
8762GEN_SPE(evfsctsiz, speundef, 0x0D, 0x0A, 0x00180000, PPC_SPE_SINGLE),
8763GEN_SPE(evfststgt, evfststlt, 0x0E, 0x0A, 0x00600000, PPC_SPE_SINGLE),
8764GEN_SPE(evfststeq, speundef, 0x0F, 0x0A, 0x00600000, PPC_SPE_SINGLE),
8765
8766GEN_SPE(efsadd, efssub, 0x00, 0x0B, 0x00000000, PPC_SPE_SINGLE),
8767GEN_SPE(efsabs, efsnabs, 0x02, 0x0B, 0x0000F800, PPC_SPE_SINGLE),
8768GEN_SPE(efsneg, speundef, 0x03, 0x0B, 0x0000F800, PPC_SPE_SINGLE),
8769GEN_SPE(efsmul, efsdiv, 0x04, 0x0B, 0x00000000, PPC_SPE_SINGLE),
8770GEN_SPE(efscmpgt, efscmplt, 0x06, 0x0B, 0x00600000, PPC_SPE_SINGLE),
8771GEN_SPE(efscmpeq, efscfd, 0x07, 0x0B, 0x00600000, PPC_SPE_SINGLE),
8772GEN_SPE(efscfui, efscfsi, 0x08, 0x0B, 0x00180000, PPC_SPE_SINGLE),
8773GEN_SPE(efscfuf, efscfsf, 0x09, 0x0B, 0x00180000, PPC_SPE_SINGLE),
8774GEN_SPE(efsctui, efsctsi, 0x0A, 0x0B, 0x00180000, PPC_SPE_SINGLE),
8775GEN_SPE(efsctuf, efsctsf, 0x0B, 0x0B, 0x00180000, PPC_SPE_SINGLE),
8776GEN_SPE(efsctuiz, speundef, 0x0C, 0x0B, 0x00180000, PPC_SPE_SINGLE),
8777GEN_SPE(efsctsiz, speundef, 0x0D, 0x0B, 0x00180000, PPC_SPE_SINGLE),
8778GEN_SPE(efststgt, efststlt, 0x0E, 0x0B, 0x00600000, PPC_SPE_SINGLE),
8779GEN_SPE(efststeq, speundef, 0x0F, 0x0B, 0x00600000, PPC_SPE_SINGLE),
8780
8781GEN_SPE(efdadd, efdsub, 0x10, 0x0B, 0x00000000, PPC_SPE_DOUBLE),
8782GEN_SPE(efdcfuid, efdcfsid, 0x11, 0x0B, 0x00180000, PPC_SPE_DOUBLE),
8783GEN_SPE(efdabs, efdnabs, 0x12, 0x0B, 0x0000F800, PPC_SPE_DOUBLE),
8784GEN_SPE(efdneg, speundef, 0x13, 0x0B, 0x0000F800, PPC_SPE_DOUBLE),
8785GEN_SPE(efdmul, efddiv, 0x14, 0x0B, 0x00000000, PPC_SPE_DOUBLE),
8786GEN_SPE(efdctuidz, efdctsidz, 0x15, 0x0B, 0x00180000, PPC_SPE_DOUBLE),
8787GEN_SPE(efdcmpgt, efdcmplt, 0x16, 0x0B, 0x00600000, PPC_SPE_DOUBLE),
8788GEN_SPE(efdcmpeq, efdcfs, 0x17, 0x0B, 0x00600000, PPC_SPE_DOUBLE),
8789GEN_SPE(efdcfui, efdcfsi, 0x18, 0x0B, 0x00180000, PPC_SPE_DOUBLE),
8790GEN_SPE(efdcfuf, efdcfsf, 0x19, 0x0B, 0x00180000, PPC_SPE_DOUBLE),
8791GEN_SPE(efdctui, efdctsi, 0x1A, 0x0B, 0x00180000, PPC_SPE_DOUBLE),
8792GEN_SPE(efdctuf, efdctsf, 0x1B, 0x0B, 0x00180000, PPC_SPE_DOUBLE),
8793GEN_SPE(efdctuiz, speundef, 0x1C, 0x0B, 0x00180000, PPC_SPE_DOUBLE),
8794GEN_SPE(efdctsiz, speundef, 0x1D, 0x0B, 0x00180000, PPC_SPE_DOUBLE),
8795GEN_SPE(efdtstgt, efdtstlt, 0x1E, 0x0B, 0x00600000, PPC_SPE_DOUBLE),
8796GEN_SPE(efdtsteq, speundef, 0x1F, 0x0B, 0x00600000, PPC_SPE_DOUBLE),
8797
8798#undef GEN_SPEOP_LDST
8799#define GEN_SPEOP_LDST(name, opc2, sh) \
8800GEN_HANDLER(name, 0x04, opc2, 0x0C, 0x00000000, PPC_SPE)
8801GEN_SPEOP_LDST(evldd, 0x00, 3),
8802GEN_SPEOP_LDST(evldw, 0x01, 3),
8803GEN_SPEOP_LDST(evldh, 0x02, 3),
8804GEN_SPEOP_LDST(evlhhesplat, 0x04, 1),
8805GEN_SPEOP_LDST(evlhhousplat, 0x06, 1),
8806GEN_SPEOP_LDST(evlhhossplat, 0x07, 1),
8807GEN_SPEOP_LDST(evlwhe, 0x08, 2),
8808GEN_SPEOP_LDST(evlwhou, 0x0A, 2),
8809GEN_SPEOP_LDST(evlwhos, 0x0B, 2),
8810GEN_SPEOP_LDST(evlwwsplat, 0x0C, 2),
8811GEN_SPEOP_LDST(evlwhsplat, 0x0E, 2),
8812
8813GEN_SPEOP_LDST(evstdd, 0x10, 3),
8814GEN_SPEOP_LDST(evstdw, 0x11, 3),
8815GEN_SPEOP_LDST(evstdh, 0x12, 3),
8816GEN_SPEOP_LDST(evstwhe, 0x18, 2),
8817GEN_SPEOP_LDST(evstwho, 0x1A, 2),
8818GEN_SPEOP_LDST(evstwwe, 0x1C, 2),
8819GEN_SPEOP_LDST(evstwwo, 0x1E, 2),
8820};
8821
3fc6c082 8822#include "translate_init.c"
0411a972 8823#include "helper_regs.h"
79aceca5 8824
9a64fbe4 8825/*****************************************************************************/
3fc6c082 8826/* Misc PowerPC helpers */
36081602
JM
8827void cpu_dump_state (CPUState *env, FILE *f,
8828 int (*cpu_fprintf)(FILE *f, const char *fmt, ...),
8829 int flags)
79aceca5 8830{
3fc6c082
FB
8831#define RGPL 4
8832#define RFPL 4
3fc6c082 8833
79aceca5
FB
8834 int i;
8835
077fc206 8836 cpu_fprintf(f, "NIP " ADDRX " LR " ADDRX " CTR " ADDRX " XER %08x\n",
3d7b417e 8837 env->nip, env->lr, env->ctr, env->xer);
6b542af7
JM
8838 cpu_fprintf(f, "MSR " ADDRX " HID0 " ADDRX " HF " ADDRX " idx %d\n",
8839 env->msr, env->spr[SPR_HID0], env->hflags, env->mmu_idx);
d9bce9d9 8840#if !defined(NO_TIMER_DUMP)
077fc206 8841 cpu_fprintf(f, "TB %08x %08x "
76a66253
JM
8842#if !defined(CONFIG_USER_ONLY)
8843 "DECR %08x"
8844#endif
8845 "\n",
077fc206 8846 cpu_ppc_load_tbu(env), cpu_ppc_load_tbl(env)
76a66253
JM
8847#if !defined(CONFIG_USER_ONLY)
8848 , cpu_ppc_load_decr(env)
8849#endif
8850 );
077fc206 8851#endif
76a66253 8852 for (i = 0; i < 32; i++) {
3fc6c082
FB
8853 if ((i & (RGPL - 1)) == 0)
8854 cpu_fprintf(f, "GPR%02d", i);
6b542af7 8855 cpu_fprintf(f, " " REGX, ppc_dump_gpr(env, i));
3fc6c082 8856 if ((i & (RGPL - 1)) == (RGPL - 1))
7fe48483 8857 cpu_fprintf(f, "\n");
76a66253 8858 }
3fc6c082 8859 cpu_fprintf(f, "CR ");
76a66253 8860 for (i = 0; i < 8; i++)
7fe48483
FB
8861 cpu_fprintf(f, "%01x", env->crf[i]);
8862 cpu_fprintf(f, " [");
76a66253
JM
8863 for (i = 0; i < 8; i++) {
8864 char a = '-';
8865 if (env->crf[i] & 0x08)
8866 a = 'L';
8867 else if (env->crf[i] & 0x04)
8868 a = 'G';
8869 else if (env->crf[i] & 0x02)
8870 a = 'E';
7fe48483 8871 cpu_fprintf(f, " %c%c", a, env->crf[i] & 0x01 ? 'O' : ' ');
76a66253 8872 }
18b21a2f 8873 cpu_fprintf(f, " ] RES " ADDRX "\n", env->reserve_addr);
3fc6c082
FB
8874 for (i = 0; i < 32; i++) {
8875 if ((i & (RFPL - 1)) == 0)
8876 cpu_fprintf(f, "FPR%02d", i);
26a76461 8877 cpu_fprintf(f, " %016" PRIx64, *((uint64_t *)&env->fpr[i]));
3fc6c082 8878 if ((i & (RFPL - 1)) == (RFPL - 1))
7fe48483 8879 cpu_fprintf(f, "\n");
79aceca5 8880 }
7889270a 8881 cpu_fprintf(f, "FPSCR %08x\n", env->fpscr);
f2e63a42 8882#if !defined(CONFIG_USER_ONLY)
6b542af7 8883 cpu_fprintf(f, "SRR0 " ADDRX " SRR1 " ADDRX " SDR1 " ADDRX "\n",
3fc6c082 8884 env->spr[SPR_SRR0], env->spr[SPR_SRR1], env->sdr1);
f2e63a42 8885#endif
79aceca5 8886
3fc6c082
FB
8887#undef RGPL
8888#undef RFPL
79aceca5
FB
8889}
8890
76a66253
JM
8891void cpu_dump_statistics (CPUState *env, FILE*f,
8892 int (*cpu_fprintf)(FILE *f, const char *fmt, ...),
8893 int flags)
8894{
8895#if defined(DO_PPC_STATISTICS)
8896 opc_handler_t **t1, **t2, **t3, *handler;
8897 int op1, op2, op3;
8898
8899 t1 = env->opcodes;
8900 for (op1 = 0; op1 < 64; op1++) {
8901 handler = t1[op1];
8902 if (is_indirect_opcode(handler)) {
8903 t2 = ind_table(handler);
8904 for (op2 = 0; op2 < 32; op2++) {
8905 handler = t2[op2];
8906 if (is_indirect_opcode(handler)) {
8907 t3 = ind_table(handler);
8908 for (op3 = 0; op3 < 32; op3++) {
8909 handler = t3[op3];
8910 if (handler->count == 0)
8911 continue;
8912 cpu_fprintf(f, "%02x %02x %02x (%02x %04d) %16s: "
8913 "%016llx %lld\n",
8914 op1, op2, op3, op1, (op3 << 5) | op2,
8915 handler->oname,
8916 handler->count, handler->count);
8917 }
8918 } else {
8919 if (handler->count == 0)
8920 continue;
8921 cpu_fprintf(f, "%02x %02x (%02x %04d) %16s: "
8922 "%016llx %lld\n",
8923 op1, op2, op1, op2, handler->oname,
8924 handler->count, handler->count);
8925 }
8926 }
8927 } else {
8928 if (handler->count == 0)
8929 continue;
8930 cpu_fprintf(f, "%02x (%02x ) %16s: %016llx %lld\n",
8931 op1, op1, handler->oname,
8932 handler->count, handler->count);
8933 }
8934 }
8935#endif
8936}
8937
9a64fbe4 8938/*****************************************************************************/
2cfc5f17
TS
8939static always_inline void gen_intermediate_code_internal (CPUState *env,
8940 TranslationBlock *tb,
8941 int search_pc)
79aceca5 8942{
9fddaa0c 8943 DisasContext ctx, *ctxp = &ctx;
79aceca5 8944 opc_handler_t **table, *handler;
0fa85d43 8945 target_ulong pc_start;
79aceca5 8946 uint16_t *gen_opc_end;
a1d1bb31 8947 CPUBreakpoint *bp;
79aceca5 8948 int j, lj = -1;
2e70f6ef
PB
8949 int num_insns;
8950 int max_insns;
79aceca5
FB
8951
8952 pc_start = tb->pc;
79aceca5 8953 gen_opc_end = gen_opc_buf + OPC_MAX_SIZE;
046d6672 8954 ctx.nip = pc_start;
79aceca5 8955 ctx.tb = tb;
e1833e1f 8956 ctx.exception = POWERPC_EXCP_NONE;
3fc6c082 8957 ctx.spr_cb = env->spr_cb;
76db3ba4
AJ
8958 ctx.mem_idx = env->mmu_idx;
8959 ctx.access_type = -1;
8960 ctx.le_mode = env->hflags & (1 << MSR_LE) ? 1 : 0;
d9bce9d9
JM
8961#if defined(TARGET_PPC64)
8962 ctx.sf_mode = msr_sf;
9a64fbe4 8963#endif
3cc62370 8964 ctx.fpu_enabled = msr_fp;
a9d9eb8f 8965 if ((env->flags & POWERPC_FLAG_SPE) && msr_spe)
d26bfc9a
JM
8966 ctx.spe_enabled = msr_spe;
8967 else
8968 ctx.spe_enabled = 0;
a9d9eb8f
JM
8969 if ((env->flags & POWERPC_FLAG_VRE) && msr_vr)
8970 ctx.altivec_enabled = msr_vr;
8971 else
8972 ctx.altivec_enabled = 0;
d26bfc9a 8973 if ((env->flags & POWERPC_FLAG_SE) && msr_se)
8cbcb4fa 8974 ctx.singlestep_enabled = CPU_SINGLE_STEP;
d26bfc9a 8975 else
8cbcb4fa 8976 ctx.singlestep_enabled = 0;
d26bfc9a 8977 if ((env->flags & POWERPC_FLAG_BE) && msr_be)
8cbcb4fa
AJ
8978 ctx.singlestep_enabled |= CPU_BRANCH_STEP;
8979 if (unlikely(env->singlestep_enabled))
8980 ctx.singlestep_enabled |= GDBSTUB_SINGLE_STEP;
3fc6c082 8981#if defined (DO_SINGLE_STEP) && 0
9a64fbe4
FB
8982 /* Single step trace mode */
8983 msr_se = 1;
8984#endif
2e70f6ef
PB
8985 num_insns = 0;
8986 max_insns = tb->cflags & CF_COUNT_MASK;
8987 if (max_insns == 0)
8988 max_insns = CF_COUNT_MASK;
8989
8990 gen_icount_start();
9a64fbe4 8991 /* Set env in case of segfault during code fetch */
e1833e1f 8992 while (ctx.exception == POWERPC_EXCP_NONE && gen_opc_ptr < gen_opc_end) {
c0ce998e
AL
8993 if (unlikely(!TAILQ_EMPTY(&env->breakpoints))) {
8994 TAILQ_FOREACH(bp, &env->breakpoints, entry) {
a1d1bb31 8995 if (bp->pc == ctx.nip) {
e06fcd75 8996 gen_debug_exception(ctxp);
ea4e754f
FB
8997 break;
8998 }
8999 }
9000 }
76a66253 9001 if (unlikely(search_pc)) {
79aceca5
FB
9002 j = gen_opc_ptr - gen_opc_buf;
9003 if (lj < j) {
9004 lj++;
9005 while (lj < j)
9006 gen_opc_instr_start[lj++] = 0;
79aceca5 9007 }
af4b6c54
AJ
9008 gen_opc_pc[lj] = ctx.nip;
9009 gen_opc_instr_start[lj] = 1;
9010 gen_opc_icount[lj] = num_insns;
79aceca5 9011 }
d12d51d5
AL
9012 LOG_DISAS("----------------\n");
9013 LOG_DISAS("nip=" ADDRX " super=%d ir=%d\n",
9014 ctx.nip, ctx.mem_idx, (int)msr_ir);
2e70f6ef
PB
9015 if (num_insns + 1 == max_insns && (tb->cflags & CF_LAST_IO))
9016 gen_io_start();
76db3ba4 9017 if (unlikely(ctx.le_mode)) {
056401ea
JM
9018 ctx.opcode = bswap32(ldl_code(ctx.nip));
9019 } else {
9020 ctx.opcode = ldl_code(ctx.nip);
111bfab3 9021 }
d12d51d5 9022 LOG_DISAS("translate opcode %08x (%02x %02x %02x) (%s)\n",
9a64fbe4 9023 ctx.opcode, opc1(ctx.opcode), opc2(ctx.opcode),
056401ea 9024 opc3(ctx.opcode), little_endian ? "little" : "big");
046d6672 9025 ctx.nip += 4;
3fc6c082 9026 table = env->opcodes;
2e70f6ef 9027 num_insns++;
79aceca5
FB
9028 handler = table[opc1(ctx.opcode)];
9029 if (is_indirect_opcode(handler)) {
9030 table = ind_table(handler);
9031 handler = table[opc2(ctx.opcode)];
9032 if (is_indirect_opcode(handler)) {
9033 table = ind_table(handler);
9034 handler = table[opc3(ctx.opcode)];
9035 }
9036 }
9037 /* Is opcode *REALLY* valid ? */
76a66253 9038 if (unlikely(handler->handler == &gen_invalid)) {
93fcfe39
AL
9039 if (qemu_log_enabled()) {
9040 qemu_log("invalid/unsupported opcode: "
9041 "%02x - %02x - %02x (%08x) " ADDRX " %d\n",
9042 opc1(ctx.opcode), opc2(ctx.opcode),
9043 opc3(ctx.opcode), ctx.opcode, ctx.nip - 4, (int)msr_ir);
4b3686fa
FB
9044 } else {
9045 printf("invalid/unsupported opcode: "
6b542af7 9046 "%02x - %02x - %02x (%08x) " ADDRX " %d\n",
4b3686fa 9047 opc1(ctx.opcode), opc2(ctx.opcode),
0411a972 9048 opc3(ctx.opcode), ctx.opcode, ctx.nip - 4, (int)msr_ir);
4b3686fa 9049 }
76a66253
JM
9050 } else {
9051 if (unlikely((ctx.opcode & handler->inval) != 0)) {
93fcfe39
AL
9052 if (qemu_log_enabled()) {
9053 qemu_log("invalid bits: %08x for opcode: "
9054 "%02x - %02x - %02x (%08x) " ADDRX "\n",
9055 ctx.opcode & handler->inval, opc1(ctx.opcode),
9056 opc2(ctx.opcode), opc3(ctx.opcode),
9057 ctx.opcode, ctx.nip - 4);
9a64fbe4
FB
9058 } else {
9059 printf("invalid bits: %08x for opcode: "
6b542af7 9060 "%02x - %02x - %02x (%08x) " ADDRX "\n",
76a66253
JM
9061 ctx.opcode & handler->inval, opc1(ctx.opcode),
9062 opc2(ctx.opcode), opc3(ctx.opcode),
046d6672 9063 ctx.opcode, ctx.nip - 4);
76a66253 9064 }
e06fcd75 9065 gen_inval_exception(ctxp, POWERPC_EXCP_INVAL_INVAL);
4b3686fa 9066 break;
79aceca5 9067 }
79aceca5 9068 }
4b3686fa 9069 (*(handler->handler))(&ctx);
76a66253
JM
9070#if defined(DO_PPC_STATISTICS)
9071 handler->count++;
9072#endif
9a64fbe4 9073 /* Check trace mode exceptions */
8cbcb4fa
AJ
9074 if (unlikely(ctx.singlestep_enabled & CPU_SINGLE_STEP &&
9075 (ctx.nip <= 0x100 || ctx.nip > 0xF00) &&
9076 ctx.exception != POWERPC_SYSCALL &&
9077 ctx.exception != POWERPC_EXCP_TRAP &&
9078 ctx.exception != POWERPC_EXCP_BRANCH)) {
e06fcd75 9079 gen_exception(ctxp, POWERPC_EXCP_TRACE);
d26bfc9a 9080 } else if (unlikely(((ctx.nip & (TARGET_PAGE_SIZE - 1)) == 0) ||
2e70f6ef 9081 (env->singlestep_enabled) ||
1b530a6d 9082 singlestep ||
2e70f6ef 9083 num_insns >= max_insns)) {
d26bfc9a
JM
9084 /* if we reach a page boundary or are single stepping, stop
9085 * generation
9086 */
8dd4983c 9087 break;
76a66253 9088 }
3fc6c082 9089 }
2e70f6ef
PB
9090 if (tb->cflags & CF_LAST_IO)
9091 gen_io_end();
e1833e1f 9092 if (ctx.exception == POWERPC_EXCP_NONE) {
c1942362 9093 gen_goto_tb(&ctx, 0, ctx.nip);
e1833e1f 9094 } else if (ctx.exception != POWERPC_EXCP_BRANCH) {
8cbcb4fa 9095 if (unlikely(env->singlestep_enabled)) {
e06fcd75 9096 gen_debug_exception(ctxp);
8cbcb4fa 9097 }
76a66253 9098 /* Generate the return instruction */
57fec1fe 9099 tcg_gen_exit_tb(0);
9a64fbe4 9100 }
2e70f6ef 9101 gen_icount_end(tb, num_insns);
79aceca5 9102 *gen_opc_ptr = INDEX_op_end;
76a66253 9103 if (unlikely(search_pc)) {
9a64fbe4
FB
9104 j = gen_opc_ptr - gen_opc_buf;
9105 lj++;
9106 while (lj <= j)
9107 gen_opc_instr_start[lj++] = 0;
9a64fbe4 9108 } else {
046d6672 9109 tb->size = ctx.nip - pc_start;
2e70f6ef 9110 tb->icount = num_insns;
9a64fbe4 9111 }
d9bce9d9 9112#if defined(DEBUG_DISAS)
93fcfe39
AL
9113 qemu_log_mask(CPU_LOG_TB_CPU, "---------------- excp: %04x\n", ctx.exception);
9114 log_cpu_state_mask(CPU_LOG_TB_CPU, env, 0);
8fec2b8c 9115 if (qemu_loglevel_mask(CPU_LOG_TB_IN_ASM)) {
76a66253 9116 int flags;
237c0af0 9117 flags = env->bfd_mach;
76db3ba4 9118 flags |= ctx.le_mode << 16;
93fcfe39
AL
9119 qemu_log("IN: %s\n", lookup_symbol(pc_start));
9120 log_target_disas(pc_start, ctx.nip - pc_start, flags);
9121 qemu_log("\n");
9fddaa0c 9122 }
79aceca5 9123#endif
79aceca5
FB
9124}
9125
2cfc5f17 9126void gen_intermediate_code (CPUState *env, struct TranslationBlock *tb)
79aceca5 9127{
2cfc5f17 9128 gen_intermediate_code_internal(env, tb, 0);
79aceca5
FB
9129}
9130
2cfc5f17 9131void gen_intermediate_code_pc (CPUState *env, struct TranslationBlock *tb)
79aceca5 9132{
2cfc5f17 9133 gen_intermediate_code_internal(env, tb, 1);
79aceca5 9134}
d2856f1a
AJ
9135
9136void gen_pc_load(CPUState *env, TranslationBlock *tb,
9137 unsigned long searched_pc, int pc_pos, void *puc)
9138{
d2856f1a 9139 env->nip = gen_opc_pc[pc_pos];
d2856f1a 9140}