]> git.proxmox.com Git - mirror_qemu.git/blame - target-ppc/translate.c
target-ppc: convert lscbx instruction to TCG
[mirror_qemu.git] / target-ppc / translate.c
CommitLineData
79aceca5 1/*
3fc6c082 2 * PowerPC emulation for qemu: main translation routines.
5fafdf24 3 *
76a66253 4 * Copyright (c) 2003-2007 Jocelyn Mayer
79aceca5
FB
5 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
10 *
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 */
c6a1c22b
FB
20#include <stdarg.h>
21#include <stdlib.h>
22#include <stdio.h>
23#include <string.h>
24#include <inttypes.h>
25
79aceca5 26#include "cpu.h"
c6a1c22b 27#include "exec-all.h"
79aceca5 28#include "disas.h"
57fec1fe 29#include "tcg-op.h"
ca10f867 30#include "qemu-common.h"
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 */
79aceca5 41//#define DO_SINGLE_STEP
9fddaa0c 42//#define PPC_DEBUG_DISAS
76a66253 43//#define DO_PPC_STATISTICS
7c58044c 44//#define OPTIMIZE_FPRF_UPDATE
79aceca5 45
a750fc0b
JM
46/*****************************************************************************/
47/* Code translation helpers */
c53be334 48
f78fb44e 49/* global register indexes */
a7812ae4 50static TCGv_ptr cpu_env;
1d542695 51static char cpu_reg_names[10*3 + 22*4 /* GPR */
f78fb44e 52#if !defined(TARGET_PPC64)
1d542695 53 + 10*4 + 22*5 /* SPE GPRh */
f78fb44e 54#endif
a5e26afa 55 + 10*4 + 22*5 /* FPR */
47e4661c
AJ
56 + 2*(10*6 + 22*7) /* AVRh, AVRl */
57 + 8*5 /* CRF */];
f78fb44e
AJ
58static TCGv cpu_gpr[32];
59#if !defined(TARGET_PPC64)
60static TCGv cpu_gprh[32];
61#endif
a7812ae4
PB
62static TCGv_i64 cpu_fpr[32];
63static TCGv_i64 cpu_avrh[32], cpu_avrl[32];
64static TCGv_i32 cpu_crf[8];
bd568f18 65static TCGv cpu_nip;
cfdcd37a
AJ
66static TCGv cpu_ctr;
67static TCGv cpu_lr;
3d7b417e 68static TCGv cpu_xer;
a7812ae4 69static TCGv_i32 cpu_fpscr;
a7859e89 70static TCGv_i32 cpu_access_type;
f78fb44e
AJ
71
72/* dyngen register indexes */
73static TCGv cpu_T[3];
2e70f6ef
PB
74
75#include "gen-icount.h"
76
77void ppc_translate_init(void)
78{
f78fb44e
AJ
79 int i;
80 char* p;
b2437bf2 81 static int done_init = 0;
f78fb44e 82
2e70f6ef
PB
83 if (done_init)
84 return;
f78fb44e 85
a7812ae4 86 cpu_env = tcg_global_reg_new_ptr(TCG_AREG0, "env");
1c73fe5b 87#if TARGET_LONG_BITS > HOST_LONG_BITS
a7812ae4
PB
88 cpu_T[0] = tcg_global_mem_new(TCG_AREG0, offsetof(CPUState, t0), "T0");
89 cpu_T[1] = tcg_global_mem_new(TCG_AREG0, offsetof(CPUState, t1), "T1");
90 cpu_T[2] = tcg_global_mem_new(TCG_AREG0, offsetof(CPUState, t2), "T2");
1c73fe5b 91#else
a7812ae4
PB
92 cpu_T[0] = tcg_global_reg_new(TCG_AREG1, "T0");
93 cpu_T[1] = tcg_global_reg_new(TCG_AREG2, "T1");
4870167d
AJ
94#ifdef HOST_I386
95 /* XXX: This is a temporary workaround for i386.
96 * On i386 qemu_st32 runs out of registers.
97 * The proper fix is to remove cpu_T.
98 */
a7812ae4 99 cpu_T[2] = tcg_global_mem_new(TCG_AREG0, offsetof(CPUState, t2), "T2");
4870167d 100#else
a7812ae4 101 cpu_T[2] = tcg_global_reg_new(TCG_AREG3, "T2");
1c73fe5b 102#endif
a7812ae4
PB
103#endif
104
f78fb44e 105 p = cpu_reg_names;
47e4661c
AJ
106
107 for (i = 0; i < 8; i++) {
108 sprintf(p, "crf%d", i);
a7812ae4
PB
109 cpu_crf[i] = tcg_global_mem_new_i32(TCG_AREG0,
110 offsetof(CPUState, crf[i]), p);
47e4661c
AJ
111 p += 5;
112 }
113
f78fb44e
AJ
114 for (i = 0; i < 32; i++) {
115 sprintf(p, "r%d", i);
a7812ae4 116 cpu_gpr[i] = tcg_global_mem_new(TCG_AREG0,
f78fb44e
AJ
117 offsetof(CPUState, gpr[i]), p);
118 p += (i < 10) ? 3 : 4;
119#if !defined(TARGET_PPC64)
120 sprintf(p, "r%dH", i);
a7812ae4
PB
121 cpu_gprh[i] = tcg_global_mem_new_i32(TCG_AREG0,
122 offsetof(CPUState, gprh[i]), p);
f78fb44e
AJ
123 p += (i < 10) ? 4 : 5;
124#endif
1d542695 125
a5e26afa 126 sprintf(p, "fp%d", i);
a7812ae4
PB
127 cpu_fpr[i] = tcg_global_mem_new_i64(TCG_AREG0,
128 offsetof(CPUState, fpr[i]), p);
ec1ac72d 129 p += (i < 10) ? 4 : 5;
a5e26afa 130
1d542695 131 sprintf(p, "avr%dH", i);
fe1e5c53
AJ
132#ifdef WORDS_BIGENDIAN
133 cpu_avrh[i] = tcg_global_mem_new_i64(TCG_AREG0,
134 offsetof(CPUState, avr[i].u64[0]), p);
135#else
a7812ae4 136 cpu_avrh[i] = tcg_global_mem_new_i64(TCG_AREG0,
fe1e5c53
AJ
137 offsetof(CPUState, avr[i].u64[1]), p);
138#endif
1d542695 139 p += (i < 10) ? 6 : 7;
ec1ac72d 140
1d542695 141 sprintf(p, "avr%dL", i);
fe1e5c53
AJ
142#ifdef WORDS_BIGENDIAN
143 cpu_avrl[i] = tcg_global_mem_new_i64(TCG_AREG0,
144 offsetof(CPUState, avr[i].u64[1]), p);
145#else
a7812ae4 146 cpu_avrl[i] = tcg_global_mem_new_i64(TCG_AREG0,
fe1e5c53
AJ
147 offsetof(CPUState, avr[i].u64[0]), p);
148#endif
1d542695 149 p += (i < 10) ? 6 : 7;
f78fb44e 150 }
f10dc08e 151
a7812ae4 152 cpu_nip = tcg_global_mem_new(TCG_AREG0,
bd568f18
AJ
153 offsetof(CPUState, nip), "nip");
154
a7812ae4 155 cpu_ctr = tcg_global_mem_new(TCG_AREG0,
cfdcd37a
AJ
156 offsetof(CPUState, ctr), "ctr");
157
a7812ae4 158 cpu_lr = tcg_global_mem_new(TCG_AREG0,
cfdcd37a
AJ
159 offsetof(CPUState, lr), "lr");
160
a7812ae4 161 cpu_xer = tcg_global_mem_new(TCG_AREG0,
3d7b417e
AJ
162 offsetof(CPUState, xer), "xer");
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
7c58044c
JM
177#if defined(OPTIMIZE_FPRF_UPDATE)
178static uint16_t *gen_fprf_buf[OPC_BUF_SIZE];
179static uint16_t **gen_fprf_ptr;
180#endif
79aceca5 181
79aceca5
FB
182/* internal defines */
183typedef struct DisasContext {
184 struct TranslationBlock *tb;
0fa85d43 185 target_ulong nip;
79aceca5 186 uint32_t opcode;
9a64fbe4 187 uint32_t exception;
3cc62370
FB
188 /* Routine used to access memory */
189 int mem_idx;
190 /* Translation flags */
9a64fbe4 191#if !defined(CONFIG_USER_ONLY)
79aceca5 192 int supervisor;
d9bce9d9
JM
193#endif
194#if defined(TARGET_PPC64)
195 int sf_mode;
9a64fbe4 196#endif
3cc62370 197 int fpu_enabled;
a9d9eb8f 198 int altivec_enabled;
0487d6a8 199 int spe_enabled;
3fc6c082 200 ppc_spr_t *spr_cb; /* Needed to check rights for mfspr/mtspr */
ea4e754f 201 int singlestep_enabled;
79aceca5
FB
202} DisasContext;
203
3fc6c082 204struct opc_handler_t {
79aceca5
FB
205 /* invalid bits */
206 uint32_t inval;
9a64fbe4 207 /* instruction type */
0487d6a8 208 uint64_t type;
79aceca5
FB
209 /* handler */
210 void (*handler)(DisasContext *ctx);
a750fc0b 211#if defined(DO_PPC_STATISTICS) || defined(PPC_DUMP_CPU)
b55266b5 212 const char *oname;
a750fc0b
JM
213#endif
214#if defined(DO_PPC_STATISTICS)
76a66253
JM
215 uint64_t count;
216#endif
3fc6c082 217};
79aceca5 218
7c58044c
JM
219static always_inline void gen_reset_fpstatus (void)
220{
221#ifdef CONFIG_SOFTFLOAT
222 gen_op_reset_fpstatus();
223#endif
224}
225
0f2f39c2 226static always_inline void gen_compute_fprf (TCGv_i64 arg, int set_fprf, int set_rc)
7c58044c 227{
0f2f39c2 228 TCGv_i32 t0 = tcg_temp_new_i32();
af12906f 229
7c58044c
JM
230 if (set_fprf != 0) {
231 /* This case might be optimized later */
232#if defined(OPTIMIZE_FPRF_UPDATE)
233 *gen_fprf_ptr++ = gen_opc_ptr;
234#endif
0f2f39c2 235 tcg_gen_movi_i32(t0, 1);
af12906f 236 gen_helper_compute_fprf(t0, arg, t0);
a7812ae4 237 if (unlikely(set_rc)) {
0f2f39c2 238 tcg_gen_mov_i32(cpu_crf[1], t0);
a7812ae4 239 }
af12906f 240 gen_helper_float_check_status();
7c58044c
JM
241 } else if (unlikely(set_rc)) {
242 /* We always need to compute fpcc */
0f2f39c2 243 tcg_gen_movi_i32(t0, 0);
af12906f 244 gen_helper_compute_fprf(t0, arg, t0);
0f2f39c2 245 tcg_gen_mov_i32(cpu_crf[1], t0);
7c58044c 246 if (set_fprf)
af12906f 247 gen_helper_float_check_status();
7c58044c 248 }
af12906f 249
0f2f39c2 250 tcg_temp_free_i32(t0);
7c58044c
JM
251}
252
253static always_inline void gen_optimize_fprf (void)
254{
255#if defined(OPTIMIZE_FPRF_UPDATE)
256 uint16_t **ptr;
257
258 for (ptr = gen_fprf_buf; ptr != (gen_fprf_ptr - 1); ptr++)
259 *ptr = INDEX_op_nop1;
260 gen_fprf_ptr = gen_fprf_buf;
261#endif
262}
263
a7859e89
AJ
264static always_inline void gen_set_access_type(int access_type)
265{
266 tcg_gen_movi_i32(cpu_access_type, access_type);
267}
268
b068d6a7 269static always_inline void gen_update_nip (DisasContext *ctx, target_ulong nip)
d9bce9d9
JM
270{
271#if defined(TARGET_PPC64)
272 if (ctx->sf_mode)
bd568f18 273 tcg_gen_movi_tl(cpu_nip, nip);
d9bce9d9
JM
274 else
275#endif
bd568f18 276 tcg_gen_movi_tl(cpu_nip, (uint32_t)nip);
d9bce9d9
JM
277}
278
e1833e1f 279#define GEN_EXCP(ctx, excp, error) \
79aceca5 280do { \
64adab3f
AJ
281 TCGv_i32 t0 = tcg_const_i32(excp); \
282 TCGv_i32 t1 = tcg_const_i32(error); \
e1833e1f 283 if ((ctx)->exception == POWERPC_EXCP_NONE) { \
d9bce9d9 284 gen_update_nip(ctx, (ctx)->nip); \
9fddaa0c 285 } \
64adab3f
AJ
286 gen_helper_raise_exception_err(t0, t1); \
287 tcg_temp_free_i32(t0); \
288 tcg_temp_free_i32(t1); \
9fddaa0c 289 ctx->exception = (excp); \
79aceca5
FB
290} while (0)
291
e1833e1f
JM
292#define GEN_EXCP_INVAL(ctx) \
293GEN_EXCP((ctx), POWERPC_EXCP_PROGRAM, \
294 POWERPC_EXCP_INVAL | POWERPC_EXCP_INVAL_INVAL)
9fddaa0c 295
e1833e1f
JM
296#define GEN_EXCP_PRIVOPC(ctx) \
297GEN_EXCP((ctx), POWERPC_EXCP_PROGRAM, \
298 POWERPC_EXCP_INVAL | POWERPC_EXCP_PRIV_OPC)
9a64fbe4 299
e1833e1f
JM
300#define GEN_EXCP_PRIVREG(ctx) \
301GEN_EXCP((ctx), POWERPC_EXCP_PROGRAM, \
302 POWERPC_EXCP_INVAL | POWERPC_EXCP_PRIV_REG)
303
304#define GEN_EXCP_NO_FP(ctx) \
305GEN_EXCP(ctx, POWERPC_EXCP_FPU, 0)
306
307#define GEN_EXCP_NO_AP(ctx) \
308GEN_EXCP(ctx, POWERPC_EXCP_APU, 0)
9a64fbe4 309
a9d9eb8f
JM
310#define GEN_EXCP_NO_VR(ctx) \
311GEN_EXCP(ctx, POWERPC_EXCP_VPU, 0)
312
f24e5695 313/* Stop translation */
b068d6a7 314static always_inline void GEN_STOP (DisasContext *ctx)
3fc6c082 315{
d9bce9d9 316 gen_update_nip(ctx, ctx->nip);
e1833e1f 317 ctx->exception = POWERPC_EXCP_STOP;
3fc6c082
FB
318}
319
f24e5695 320/* No need to update nip here, as execution flow will change */
b068d6a7 321static always_inline void GEN_SYNC (DisasContext *ctx)
2be0071f 322{
e1833e1f 323 ctx->exception = POWERPC_EXCP_SYNC;
2be0071f
FB
324}
325
79aceca5
FB
326#define GEN_HANDLER(name, opc1, opc2, opc3, inval, type) \
327static void gen_##name (DisasContext *ctx); \
328GEN_OPCODE(name, opc1, opc2, opc3, inval, type); \
329static void gen_##name (DisasContext *ctx)
330
c7697e1f
JM
331#define GEN_HANDLER2(name, onam, opc1, opc2, opc3, inval, type) \
332static void gen_##name (DisasContext *ctx); \
333GEN_OPCODE2(name, onam, opc1, opc2, opc3, inval, type); \
334static void gen_##name (DisasContext *ctx)
335
79aceca5
FB
336typedef struct opcode_t {
337 unsigned char opc1, opc2, opc3;
1235fc06 338#if HOST_LONG_BITS == 64 /* Explicitly align to 64 bits */
18fba28c
FB
339 unsigned char pad[5];
340#else
341 unsigned char pad[1];
342#endif
79aceca5 343 opc_handler_t handler;
b55266b5 344 const char *oname;
79aceca5
FB
345} opcode_t;
346
a750fc0b 347/*****************************************************************************/
79aceca5
FB
348/*** Instruction decoding ***/
349#define EXTRACT_HELPER(name, shift, nb) \
b068d6a7 350static always_inline uint32_t name (uint32_t opcode) \
79aceca5
FB
351{ \
352 return (opcode >> (shift)) & ((1 << (nb)) - 1); \
353}
354
355#define EXTRACT_SHELPER(name, shift, nb) \
b068d6a7 356static always_inline int32_t name (uint32_t opcode) \
79aceca5 357{ \
18fba28c 358 return (int16_t)((opcode >> (shift)) & ((1 << (nb)) - 1)); \
79aceca5
FB
359}
360
361/* Opcode part 1 */
362EXTRACT_HELPER(opc1, 26, 6);
363/* Opcode part 2 */
364EXTRACT_HELPER(opc2, 1, 5);
365/* Opcode part 3 */
366EXTRACT_HELPER(opc3, 6, 5);
367/* Update Cr0 flags */
368EXTRACT_HELPER(Rc, 0, 1);
369/* Destination */
370EXTRACT_HELPER(rD, 21, 5);
371/* Source */
372EXTRACT_HELPER(rS, 21, 5);
373/* First operand */
374EXTRACT_HELPER(rA, 16, 5);
375/* Second operand */
376EXTRACT_HELPER(rB, 11, 5);
377/* Third operand */
378EXTRACT_HELPER(rC, 6, 5);
379/*** Get CRn ***/
380EXTRACT_HELPER(crfD, 23, 3);
381EXTRACT_HELPER(crfS, 18, 3);
382EXTRACT_HELPER(crbD, 21, 5);
383EXTRACT_HELPER(crbA, 16, 5);
384EXTRACT_HELPER(crbB, 11, 5);
385/* SPR / TBL */
3fc6c082 386EXTRACT_HELPER(_SPR, 11, 10);
b068d6a7 387static always_inline uint32_t SPR (uint32_t opcode)
3fc6c082
FB
388{
389 uint32_t sprn = _SPR(opcode);
390
391 return ((sprn >> 5) & 0x1F) | ((sprn & 0x1F) << 5);
392}
79aceca5
FB
393/*** Get constants ***/
394EXTRACT_HELPER(IMM, 12, 8);
395/* 16 bits signed immediate value */
396EXTRACT_SHELPER(SIMM, 0, 16);
397/* 16 bits unsigned immediate value */
398EXTRACT_HELPER(UIMM, 0, 16);
399/* Bit count */
400EXTRACT_HELPER(NB, 11, 5);
401/* Shift count */
402EXTRACT_HELPER(SH, 11, 5);
403/* Mask start */
404EXTRACT_HELPER(MB, 6, 5);
405/* Mask end */
406EXTRACT_HELPER(ME, 1, 5);
fb0eaffc
FB
407/* Trap operand */
408EXTRACT_HELPER(TO, 21, 5);
79aceca5
FB
409
410EXTRACT_HELPER(CRM, 12, 8);
411EXTRACT_HELPER(FM, 17, 8);
412EXTRACT_HELPER(SR, 16, 4);
e4bb997e 413EXTRACT_HELPER(FPIMM, 12, 4);
fb0eaffc 414
79aceca5
FB
415/*** Jump target decoding ***/
416/* Displacement */
417EXTRACT_SHELPER(d, 0, 16);
418/* Immediate address */
b068d6a7 419static always_inline target_ulong LI (uint32_t opcode)
79aceca5
FB
420{
421 return (opcode >> 0) & 0x03FFFFFC;
422}
423
b068d6a7 424static always_inline uint32_t BD (uint32_t opcode)
79aceca5
FB
425{
426 return (opcode >> 0) & 0xFFFC;
427}
428
429EXTRACT_HELPER(BO, 21, 5);
430EXTRACT_HELPER(BI, 16, 5);
431/* Absolute/relative address */
432EXTRACT_HELPER(AA, 1, 1);
433/* Link */
434EXTRACT_HELPER(LK, 0, 1);
435
436/* Create a mask between <start> and <end> bits */
b068d6a7 437static always_inline target_ulong MASK (uint32_t start, uint32_t end)
79aceca5 438{
76a66253 439 target_ulong ret;
79aceca5 440
76a66253
JM
441#if defined(TARGET_PPC64)
442 if (likely(start == 0)) {
6f2d8978 443 ret = UINT64_MAX << (63 - end);
76a66253 444 } else if (likely(end == 63)) {
6f2d8978 445 ret = UINT64_MAX >> start;
76a66253
JM
446 }
447#else
448 if (likely(start == 0)) {
6f2d8978 449 ret = UINT32_MAX << (31 - end);
76a66253 450 } else if (likely(end == 31)) {
6f2d8978 451 ret = UINT32_MAX >> start;
76a66253
JM
452 }
453#endif
454 else {
455 ret = (((target_ulong)(-1ULL)) >> (start)) ^
456 (((target_ulong)(-1ULL) >> (end)) >> 1);
457 if (unlikely(start > end))
458 return ~ret;
459 }
79aceca5
FB
460
461 return ret;
462}
463
a750fc0b
JM
464/*****************************************************************************/
465/* PowerPC Instructions types definitions */
466enum {
1b413d55 467 PPC_NONE = 0x0000000000000000ULL,
12de9a39 468 /* PowerPC base instructions set */
1b413d55
JM
469 PPC_INSNS_BASE = 0x0000000000000001ULL,
470 /* integer operations instructions */
a750fc0b 471#define PPC_INTEGER PPC_INSNS_BASE
1b413d55 472 /* flow control instructions */
a750fc0b 473#define PPC_FLOW PPC_INSNS_BASE
1b413d55 474 /* virtual memory instructions */
a750fc0b 475#define PPC_MEM PPC_INSNS_BASE
1b413d55 476 /* ld/st with reservation instructions */
a750fc0b 477#define PPC_RES PPC_INSNS_BASE
1b413d55 478 /* spr/msr access instructions */
a750fc0b 479#define PPC_MISC PPC_INSNS_BASE
1b413d55
JM
480 /* Deprecated instruction sets */
481 /* Original POWER instruction set */
f610349f 482 PPC_POWER = 0x0000000000000002ULL,
1b413d55 483 /* POWER2 instruction set extension */
f610349f 484 PPC_POWER2 = 0x0000000000000004ULL,
1b413d55 485 /* Power RTC support */
f610349f 486 PPC_POWER_RTC = 0x0000000000000008ULL,
1b413d55 487 /* Power-to-PowerPC bridge (601) */
f610349f 488 PPC_POWER_BR = 0x0000000000000010ULL,
1b413d55 489 /* 64 bits PowerPC instruction set */
f610349f 490 PPC_64B = 0x0000000000000020ULL,
1b413d55 491 /* New 64 bits extensions (PowerPC 2.0x) */
f610349f 492 PPC_64BX = 0x0000000000000040ULL,
1b413d55 493 /* 64 bits hypervisor extensions */
f610349f 494 PPC_64H = 0x0000000000000080ULL,
1b413d55 495 /* New wait instruction (PowerPC 2.0x) */
f610349f 496 PPC_WAIT = 0x0000000000000100ULL,
1b413d55 497 /* Time base mftb instruction */
f610349f 498 PPC_MFTB = 0x0000000000000200ULL,
1b413d55
JM
499
500 /* Fixed-point unit extensions */
501 /* PowerPC 602 specific */
f610349f 502 PPC_602_SPEC = 0x0000000000000400ULL,
05332d70
JM
503 /* isel instruction */
504 PPC_ISEL = 0x0000000000000800ULL,
505 /* popcntb instruction */
506 PPC_POPCNTB = 0x0000000000001000ULL,
507 /* string load / store */
508 PPC_STRING = 0x0000000000002000ULL,
1b413d55
JM
509
510 /* Floating-point unit extensions */
511 /* Optional floating point instructions */
512 PPC_FLOAT = 0x0000000000010000ULL,
513 /* New floating-point extensions (PowerPC 2.0x) */
514 PPC_FLOAT_EXT = 0x0000000000020000ULL,
515 PPC_FLOAT_FSQRT = 0x0000000000040000ULL,
516 PPC_FLOAT_FRES = 0x0000000000080000ULL,
517 PPC_FLOAT_FRSQRTE = 0x0000000000100000ULL,
518 PPC_FLOAT_FRSQRTES = 0x0000000000200000ULL,
519 PPC_FLOAT_FSEL = 0x0000000000400000ULL,
520 PPC_FLOAT_STFIWX = 0x0000000000800000ULL,
521
522 /* Vector/SIMD extensions */
523 /* Altivec support */
524 PPC_ALTIVEC = 0x0000000001000000ULL,
1b413d55 525 /* PowerPC 2.03 SPE extension */
05332d70 526 PPC_SPE = 0x0000000002000000ULL,
1b413d55 527 /* PowerPC 2.03 SPE floating-point extension */
05332d70 528 PPC_SPEFPU = 0x0000000004000000ULL,
1b413d55 529
12de9a39 530 /* Optional memory control instructions */
1b413d55
JM
531 PPC_MEM_TLBIA = 0x0000000010000000ULL,
532 PPC_MEM_TLBIE = 0x0000000020000000ULL,
533 PPC_MEM_TLBSYNC = 0x0000000040000000ULL,
534 /* sync instruction */
535 PPC_MEM_SYNC = 0x0000000080000000ULL,
536 /* eieio instruction */
537 PPC_MEM_EIEIO = 0x0000000100000000ULL,
538
539 /* Cache control instructions */
c8623f2e 540 PPC_CACHE = 0x0000000200000000ULL,
1b413d55 541 /* icbi instruction */
05332d70 542 PPC_CACHE_ICBI = 0x0000000400000000ULL,
1b413d55 543 /* dcbz instruction with fixed cache line size */
05332d70 544 PPC_CACHE_DCBZ = 0x0000000800000000ULL,
1b413d55 545 /* dcbz instruction with tunable cache line size */
05332d70 546 PPC_CACHE_DCBZT = 0x0000001000000000ULL,
1b413d55 547 /* dcba instruction */
05332d70
JM
548 PPC_CACHE_DCBA = 0x0000002000000000ULL,
549 /* Freescale cache locking instructions */
550 PPC_CACHE_LOCK = 0x0000004000000000ULL,
1b413d55
JM
551
552 /* MMU related extensions */
553 /* external control instructions */
05332d70 554 PPC_EXTERN = 0x0000010000000000ULL,
1b413d55 555 /* segment register access instructions */
05332d70 556 PPC_SEGMENT = 0x0000020000000000ULL,
1b413d55 557 /* PowerPC 6xx TLB management instructions */
05332d70 558 PPC_6xx_TLB = 0x0000040000000000ULL,
1b413d55 559 /* PowerPC 74xx TLB management instructions */
05332d70 560 PPC_74xx_TLB = 0x0000080000000000ULL,
1b413d55 561 /* PowerPC 40x TLB management instructions */
05332d70 562 PPC_40x_TLB = 0x0000100000000000ULL,
1b413d55 563 /* segment register access instructions for PowerPC 64 "bridge" */
05332d70 564 PPC_SEGMENT_64B = 0x0000200000000000ULL,
1b413d55 565 /* SLB management */
05332d70 566 PPC_SLBI = 0x0000400000000000ULL,
1b413d55 567
12de9a39 568 /* Embedded PowerPC dedicated instructions */
05332d70 569 PPC_WRTEE = 0x0001000000000000ULL,
12de9a39 570 /* PowerPC 40x exception model */
05332d70 571 PPC_40x_EXCP = 0x0002000000000000ULL,
12de9a39 572 /* PowerPC 405 Mac instructions */
05332d70 573 PPC_405_MAC = 0x0004000000000000ULL,
12de9a39 574 /* PowerPC 440 specific instructions */
05332d70 575 PPC_440_SPEC = 0x0008000000000000ULL,
12de9a39 576 /* BookE (embedded) PowerPC specification */
05332d70
JM
577 PPC_BOOKE = 0x0010000000000000ULL,
578 /* mfapidi instruction */
579 PPC_MFAPIDI = 0x0020000000000000ULL,
580 /* tlbiva instruction */
581 PPC_TLBIVA = 0x0040000000000000ULL,
582 /* tlbivax instruction */
583 PPC_TLBIVAX = 0x0080000000000000ULL,
12de9a39 584 /* PowerPC 4xx dedicated instructions */
05332d70 585 PPC_4xx_COMMON = 0x0100000000000000ULL,
12de9a39 586 /* PowerPC 40x ibct instructions */
05332d70 587 PPC_40x_ICBT = 0x0200000000000000ULL,
12de9a39 588 /* rfmci is not implemented in all BookE PowerPC */
05332d70
JM
589 PPC_RFMCI = 0x0400000000000000ULL,
590 /* rfdi instruction */
591 PPC_RFDI = 0x0800000000000000ULL,
592 /* DCR accesses */
593 PPC_DCR = 0x1000000000000000ULL,
594 /* DCR extended accesse */
595 PPC_DCRX = 0x2000000000000000ULL,
12de9a39 596 /* user-mode DCR access, implemented in PowerPC 460 */
05332d70 597 PPC_DCRUX = 0x4000000000000000ULL,
a750fc0b
JM
598};
599
600/*****************************************************************************/
601/* PowerPC instructions table */
3fc6c082
FB
602#if HOST_LONG_BITS == 64
603#define OPC_ALIGN 8
604#else
605#define OPC_ALIGN 4
606#endif
1b039c09 607#if defined(__APPLE__)
d9bce9d9 608#define OPCODES_SECTION \
3fc6c082 609 __attribute__ ((section("__TEXT,__opcodes"), unused, aligned (OPC_ALIGN) ))
933dc6eb 610#else
d9bce9d9 611#define OPCODES_SECTION \
3fc6c082 612 __attribute__ ((section(".opcodes"), unused, aligned (OPC_ALIGN) ))
933dc6eb
FB
613#endif
614
76a66253 615#if defined(DO_PPC_STATISTICS)
79aceca5 616#define GEN_OPCODE(name, op1, op2, op3, invl, _typ) \
18fba28c 617OPCODES_SECTION opcode_t opc_##name = { \
79aceca5
FB
618 .opc1 = op1, \
619 .opc2 = op2, \
620 .opc3 = op3, \
18fba28c 621 .pad = { 0, }, \
79aceca5
FB
622 .handler = { \
623 .inval = invl, \
9a64fbe4 624 .type = _typ, \
79aceca5 625 .handler = &gen_##name, \
76a66253 626 .oname = stringify(name), \
79aceca5 627 }, \
3fc6c082 628 .oname = stringify(name), \
79aceca5 629}
c7697e1f
JM
630#define GEN_OPCODE2(name, onam, op1, op2, op3, invl, _typ) \
631OPCODES_SECTION opcode_t opc_##name = { \
632 .opc1 = op1, \
633 .opc2 = op2, \
634 .opc3 = op3, \
635 .pad = { 0, }, \
636 .handler = { \
637 .inval = invl, \
638 .type = _typ, \
639 .handler = &gen_##name, \
640 .oname = onam, \
641 }, \
642 .oname = onam, \
643}
76a66253
JM
644#else
645#define GEN_OPCODE(name, op1, op2, op3, invl, _typ) \
646OPCODES_SECTION opcode_t opc_##name = { \
647 .opc1 = op1, \
648 .opc2 = op2, \
649 .opc3 = op3, \
650 .pad = { 0, }, \
651 .handler = { \
652 .inval = invl, \
653 .type = _typ, \
654 .handler = &gen_##name, \
655 }, \
656 .oname = stringify(name), \
657}
c7697e1f
JM
658#define GEN_OPCODE2(name, onam, op1, op2, op3, invl, _typ) \
659OPCODES_SECTION opcode_t opc_##name = { \
660 .opc1 = op1, \
661 .opc2 = op2, \
662 .opc3 = op3, \
663 .pad = { 0, }, \
664 .handler = { \
665 .inval = invl, \
666 .type = _typ, \
667 .handler = &gen_##name, \
668 }, \
669 .oname = onam, \
670}
76a66253 671#endif
79aceca5
FB
672
673#define GEN_OPCODE_MARK(name) \
18fba28c 674OPCODES_SECTION opcode_t opc_##name = { \
79aceca5
FB
675 .opc1 = 0xFF, \
676 .opc2 = 0xFF, \
677 .opc3 = 0xFF, \
18fba28c 678 .pad = { 0, }, \
79aceca5
FB
679 .handler = { \
680 .inval = 0x00000000, \
9a64fbe4 681 .type = 0x00, \
79aceca5
FB
682 .handler = NULL, \
683 }, \
3fc6c082 684 .oname = stringify(name), \
79aceca5
FB
685}
686
687/* Start opcode list */
688GEN_OPCODE_MARK(start);
689
690/* Invalid instruction */
9a64fbe4
FB
691GEN_HANDLER(invalid, 0x00, 0x00, 0x00, 0xFFFFFFFF, PPC_NONE)
692{
e1833e1f 693 GEN_EXCP_INVAL(ctx);
9a64fbe4
FB
694}
695
79aceca5
FB
696static opc_handler_t invalid_handler = {
697 .inval = 0xFFFFFFFF,
9a64fbe4 698 .type = PPC_NONE,
79aceca5
FB
699 .handler = gen_invalid,
700};
701
e1571908
AJ
702/*** Integer comparison ***/
703
ea363694 704static always_inline void gen_op_cmp(TCGv arg0, TCGv arg1, int s, int crf)
e1571908
AJ
705{
706 int l1, l2, l3;
707
269f3e95
AJ
708 tcg_gen_trunc_tl_i32(cpu_crf[crf], cpu_xer);
709 tcg_gen_shri_i32(cpu_crf[crf], cpu_crf[crf], XER_SO);
e1571908
AJ
710 tcg_gen_andi_i32(cpu_crf[crf], cpu_crf[crf], 1);
711
712 l1 = gen_new_label();
713 l2 = gen_new_label();
714 l3 = gen_new_label();
715 if (s) {
ea363694
AJ
716 tcg_gen_brcond_tl(TCG_COND_LT, arg0, arg1, l1);
717 tcg_gen_brcond_tl(TCG_COND_GT, arg0, arg1, l2);
e1571908 718 } else {
ea363694
AJ
719 tcg_gen_brcond_tl(TCG_COND_LTU, arg0, arg1, l1);
720 tcg_gen_brcond_tl(TCG_COND_GTU, arg0, arg1, l2);
e1571908
AJ
721 }
722 tcg_gen_ori_i32(cpu_crf[crf], cpu_crf[crf], 1 << CRF_EQ);
723 tcg_gen_br(l3);
724 gen_set_label(l1);
725 tcg_gen_ori_i32(cpu_crf[crf], cpu_crf[crf], 1 << CRF_LT);
726 tcg_gen_br(l3);
727 gen_set_label(l2);
728 tcg_gen_ori_i32(cpu_crf[crf], cpu_crf[crf], 1 << CRF_GT);
729 gen_set_label(l3);
730}
731
ea363694 732static always_inline void gen_op_cmpi(TCGv arg0, target_ulong arg1, int s, int crf)
e1571908 733{
ea363694
AJ
734 TCGv t0 = tcg_const_local_tl(arg1);
735 gen_op_cmp(arg0, t0, s, crf);
736 tcg_temp_free(t0);
e1571908
AJ
737}
738
739#if defined(TARGET_PPC64)
ea363694 740static always_inline void gen_op_cmp32(TCGv arg0, TCGv arg1, int s, int crf)
e1571908 741{
ea363694 742 TCGv t0, t1;
a7812ae4
PB
743 t0 = tcg_temp_local_new();
744 t1 = tcg_temp_local_new();
e1571908 745 if (s) {
ea363694
AJ
746 tcg_gen_ext32s_tl(t0, arg0);
747 tcg_gen_ext32s_tl(t1, arg1);
e1571908 748 } else {
ea363694
AJ
749 tcg_gen_ext32u_tl(t0, arg0);
750 tcg_gen_ext32u_tl(t1, arg1);
e1571908 751 }
ea363694
AJ
752 gen_op_cmp(t0, t1, s, crf);
753 tcg_temp_free(t1);
754 tcg_temp_free(t0);
e1571908
AJ
755}
756
ea363694 757static always_inline void gen_op_cmpi32(TCGv arg0, target_ulong arg1, int s, int crf)
e1571908 758{
ea363694
AJ
759 TCGv t0 = tcg_const_local_tl(arg1);
760 gen_op_cmp32(arg0, t0, s, crf);
761 tcg_temp_free(t0);
e1571908
AJ
762}
763#endif
764
765static always_inline void gen_set_Rc0 (DisasContext *ctx, TCGv reg)
766{
767#if defined(TARGET_PPC64)
768 if (!(ctx->sf_mode))
769 gen_op_cmpi32(reg, 0, 1, 0);
770 else
771#endif
772 gen_op_cmpi(reg, 0, 1, 0);
773}
774
775/* cmp */
776GEN_HANDLER(cmp, 0x1F, 0x00, 0x00, 0x00400000, PPC_INTEGER)
777{
778#if defined(TARGET_PPC64)
779 if (!(ctx->sf_mode && (ctx->opcode & 0x00200000)))
780 gen_op_cmp32(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rB(ctx->opcode)],
781 1, crfD(ctx->opcode));
782 else
783#endif
784 gen_op_cmp(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rB(ctx->opcode)],
785 1, crfD(ctx->opcode));
786}
787
788/* cmpi */
789GEN_HANDLER(cmpi, 0x0B, 0xFF, 0xFF, 0x00400000, PPC_INTEGER)
790{
791#if defined(TARGET_PPC64)
792 if (!(ctx->sf_mode && (ctx->opcode & 0x00200000)))
793 gen_op_cmpi32(cpu_gpr[rA(ctx->opcode)], SIMM(ctx->opcode),
794 1, crfD(ctx->opcode));
795 else
796#endif
797 gen_op_cmpi(cpu_gpr[rA(ctx->opcode)], SIMM(ctx->opcode),
798 1, crfD(ctx->opcode));
799}
800
801/* cmpl */
802GEN_HANDLER(cmpl, 0x1F, 0x00, 0x01, 0x00400000, PPC_INTEGER)
803{
804#if defined(TARGET_PPC64)
805 if (!(ctx->sf_mode && (ctx->opcode & 0x00200000)))
806 gen_op_cmp32(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rB(ctx->opcode)],
807 0, crfD(ctx->opcode));
808 else
809#endif
810 gen_op_cmp(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rB(ctx->opcode)],
811 0, crfD(ctx->opcode));
812}
813
814/* cmpli */
815GEN_HANDLER(cmpli, 0x0A, 0xFF, 0xFF, 0x00400000, PPC_INTEGER)
816{
817#if defined(TARGET_PPC64)
818 if (!(ctx->sf_mode && (ctx->opcode & 0x00200000)))
819 gen_op_cmpi32(cpu_gpr[rA(ctx->opcode)], UIMM(ctx->opcode),
820 0, crfD(ctx->opcode));
821 else
822#endif
823 gen_op_cmpi(cpu_gpr[rA(ctx->opcode)], UIMM(ctx->opcode),
824 0, crfD(ctx->opcode));
825}
826
827/* isel (PowerPC 2.03 specification) */
828GEN_HANDLER(isel, 0x1F, 0x0F, 0xFF, 0x00000001, PPC_ISEL)
829{
830 int l1, l2;
831 uint32_t bi = rC(ctx->opcode);
832 uint32_t mask;
a7812ae4 833 TCGv_i32 t0;
e1571908
AJ
834
835 l1 = gen_new_label();
836 l2 = gen_new_label();
837
838 mask = 1 << (3 - (bi & 0x03));
a7812ae4 839 t0 = tcg_temp_new_i32();
fea0c503
AJ
840 tcg_gen_andi_i32(t0, cpu_crf[bi >> 2], mask);
841 tcg_gen_brcondi_i32(TCG_COND_EQ, t0, 0, l1);
e1571908
AJ
842 if (rA(ctx->opcode) == 0)
843 tcg_gen_movi_tl(cpu_gpr[rD(ctx->opcode)], 0);
844 else
845 tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)]);
846 tcg_gen_br(l2);
847 gen_set_label(l1);
848 tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rB(ctx->opcode)]);
849 gen_set_label(l2);
a7812ae4 850 tcg_temp_free_i32(t0);
e1571908
AJ
851}
852
79aceca5 853/*** Integer arithmetic ***/
79aceca5 854
74637406
AJ
855static always_inline void gen_op_arith_compute_ov(DisasContext *ctx, TCGv arg0, TCGv arg1, TCGv arg2, int sub)
856{
857 int l1;
858 TCGv t0;
79aceca5 859
74637406
AJ
860 l1 = gen_new_label();
861 /* Start with XER OV disabled, the most likely case */
862 tcg_gen_andi_tl(cpu_xer, cpu_xer, ~(1 << XER_OV));
a7812ae4 863 t0 = tcg_temp_local_new();
74637406
AJ
864 tcg_gen_xor_tl(t0, arg0, arg1);
865#if defined(TARGET_PPC64)
866 if (!ctx->sf_mode)
867 tcg_gen_ext32s_tl(t0, t0);
868#endif
869 if (sub)
870 tcg_gen_brcondi_tl(TCG_COND_LT, t0, 0, l1);
871 else
872 tcg_gen_brcondi_tl(TCG_COND_GE, t0, 0, l1);
873 tcg_gen_xor_tl(t0, arg1, arg2);
874#if defined(TARGET_PPC64)
875 if (!ctx->sf_mode)
876 tcg_gen_ext32s_tl(t0, t0);
877#endif
878 if (sub)
879 tcg_gen_brcondi_tl(TCG_COND_GE, t0, 0, l1);
880 else
881 tcg_gen_brcondi_tl(TCG_COND_LT, t0, 0, l1);
882 tcg_gen_ori_tl(cpu_xer, cpu_xer, (1 << XER_OV) | (1 << XER_SO));
883 gen_set_label(l1);
884 tcg_temp_free(t0);
79aceca5
FB
885}
886
74637406
AJ
887static always_inline void gen_op_arith_compute_ca(DisasContext *ctx, TCGv arg1, TCGv arg2, int sub)
888{
889 int l1 = gen_new_label();
d9bce9d9
JM
890
891#if defined(TARGET_PPC64)
74637406
AJ
892 if (!(ctx->sf_mode)) {
893 TCGv t0, t1;
a7812ae4
PB
894 t0 = tcg_temp_new();
895 t1 = tcg_temp_new();
d9bce9d9 896
74637406
AJ
897 tcg_gen_ext32u_tl(t0, arg1);
898 tcg_gen_ext32u_tl(t1, arg2);
899 if (sub) {
900 tcg_gen_brcond_tl(TCG_COND_GTU, t0, t1, l1);
bdc4e053 901 } else {
74637406
AJ
902 tcg_gen_brcond_tl(TCG_COND_GEU, t0, t1, l1);
903 }
a9730017
AJ
904 tcg_gen_ori_tl(cpu_xer, cpu_xer, 1 << XER_CA);
905 gen_set_label(l1);
906 tcg_temp_free(t0);
907 tcg_temp_free(t1);
74637406
AJ
908 } else
909#endif
a9730017
AJ
910 {
911 if (sub) {
912 tcg_gen_brcond_tl(TCG_COND_GTU, arg1, arg2, l1);
913 } else {
914 tcg_gen_brcond_tl(TCG_COND_GEU, arg1, arg2, l1);
915 }
916 tcg_gen_ori_tl(cpu_xer, cpu_xer, 1 << XER_CA);
917 gen_set_label(l1);
74637406 918 }
d9bce9d9
JM
919}
920
74637406
AJ
921/* Common add function */
922static always_inline void gen_op_arith_add(DisasContext *ctx, TCGv ret, TCGv arg1, TCGv arg2,
923 int add_ca, int compute_ca, int compute_ov)
924{
925 TCGv t0, t1;
d9bce9d9 926
74637406 927 if ((!compute_ca && !compute_ov) ||
a7812ae4 928 (!TCGV_EQUAL(ret,arg1) && !TCGV_EQUAL(ret, arg2))) {
74637406
AJ
929 t0 = ret;
930 } else {
a7812ae4 931 t0 = tcg_temp_local_new();
74637406 932 }
79aceca5 933
74637406 934 if (add_ca) {
a7812ae4 935 t1 = tcg_temp_local_new();
74637406
AJ
936 tcg_gen_andi_tl(t1, cpu_xer, (1 << XER_CA));
937 tcg_gen_shri_tl(t1, t1, XER_CA);
938 }
79aceca5 939
74637406
AJ
940 if (compute_ca && compute_ov) {
941 /* Start with XER CA and OV disabled, the most likely case */
942 tcg_gen_andi_tl(cpu_xer, cpu_xer, ~((1 << XER_CA) | (1 << XER_OV)));
943 } else if (compute_ca) {
944 /* Start with XER CA disabled, the most likely case */
945 tcg_gen_andi_tl(cpu_xer, cpu_xer, ~(1 << XER_CA));
946 } else if (compute_ov) {
947 /* Start with XER OV disabled, the most likely case */
948 tcg_gen_andi_tl(cpu_xer, cpu_xer, ~(1 << XER_OV));
949 }
79aceca5 950
74637406
AJ
951 tcg_gen_add_tl(t0, arg1, arg2);
952
953 if (compute_ca) {
954 gen_op_arith_compute_ca(ctx, t0, arg1, 0);
955 }
956 if (add_ca) {
957 tcg_gen_add_tl(t0, t0, t1);
958 gen_op_arith_compute_ca(ctx, t0, t1, 0);
959 tcg_temp_free(t1);
960 }
961 if (compute_ov) {
962 gen_op_arith_compute_ov(ctx, t0, arg1, arg2, 0);
963 }
964
965 if (unlikely(Rc(ctx->opcode) != 0))
966 gen_set_Rc0(ctx, t0);
967
a7812ae4 968 if (!TCGV_EQUAL(t0, ret)) {
74637406
AJ
969 tcg_gen_mov_tl(ret, t0);
970 tcg_temp_free(t0);
971 }
39dd32ee 972}
74637406
AJ
973/* Add functions with two operands */
974#define GEN_INT_ARITH_ADD(name, opc3, add_ca, compute_ca, compute_ov) \
975GEN_HANDLER(name, 0x1F, 0x0A, opc3, 0x00000000, PPC_INTEGER) \
976{ \
977 gen_op_arith_add(ctx, cpu_gpr[rD(ctx->opcode)], \
978 cpu_gpr[rA(ctx->opcode)], cpu_gpr[rB(ctx->opcode)], \
979 add_ca, compute_ca, compute_ov); \
980}
981/* Add functions with one operand and one immediate */
982#define GEN_INT_ARITH_ADD_CONST(name, opc3, const_val, \
983 add_ca, compute_ca, compute_ov) \
984GEN_HANDLER(name, 0x1F, 0x0A, opc3, 0x0000F800, PPC_INTEGER) \
985{ \
986 TCGv t0 = tcg_const_local_tl(const_val); \
987 gen_op_arith_add(ctx, cpu_gpr[rD(ctx->opcode)], \
988 cpu_gpr[rA(ctx->opcode)], t0, \
989 add_ca, compute_ca, compute_ov); \
990 tcg_temp_free(t0); \
991}
992
993/* add add. addo addo. */
994GEN_INT_ARITH_ADD(add, 0x08, 0, 0, 0)
995GEN_INT_ARITH_ADD(addo, 0x18, 0, 0, 1)
996/* addc addc. addco addco. */
997GEN_INT_ARITH_ADD(addc, 0x00, 0, 1, 0)
998GEN_INT_ARITH_ADD(addco, 0x10, 0, 1, 1)
999/* adde adde. addeo addeo. */
1000GEN_INT_ARITH_ADD(adde, 0x04, 1, 1, 0)
1001GEN_INT_ARITH_ADD(addeo, 0x14, 1, 1, 1)
1002/* addme addme. addmeo addmeo. */
1003GEN_INT_ARITH_ADD_CONST(addme, 0x07, -1LL, 1, 1, 0)
1004GEN_INT_ARITH_ADD_CONST(addmeo, 0x17, -1LL, 1, 1, 1)
1005/* addze addze. addzeo addzeo.*/
1006GEN_INT_ARITH_ADD_CONST(addze, 0x06, 0, 1, 1, 0)
1007GEN_INT_ARITH_ADD_CONST(addzeo, 0x16, 0, 1, 1, 1)
1008/* addi */
1009GEN_HANDLER(addi, 0x0E, 0xFF, 0xFF, 0x00000000, PPC_INTEGER)
d9bce9d9 1010{
74637406
AJ
1011 target_long simm = SIMM(ctx->opcode);
1012
1013 if (rA(ctx->opcode) == 0) {
1014 /* li case */
1015 tcg_gen_movi_tl(cpu_gpr[rD(ctx->opcode)], simm);
1016 } else {
1017 tcg_gen_addi_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)], simm);
1018 }
d9bce9d9 1019}
74637406
AJ
1020/* addic addic.*/
1021static always_inline void gen_op_addic (DisasContext *ctx, TCGv ret, TCGv arg1,
1022 int compute_Rc0)
d9bce9d9 1023{
74637406
AJ
1024 target_long simm = SIMM(ctx->opcode);
1025
1026 /* Start with XER CA and OV disabled, the most likely case */
1027 tcg_gen_andi_tl(cpu_xer, cpu_xer, ~(1 << XER_CA));
1028
1029 if (likely(simm != 0)) {
a7812ae4 1030 TCGv t0 = tcg_temp_local_new();
74637406
AJ
1031 tcg_gen_addi_tl(t0, arg1, simm);
1032 gen_op_arith_compute_ca(ctx, t0, arg1, 0);
1033 tcg_gen_mov_tl(ret, t0);
1034 tcg_temp_free(t0);
1035 } else {
1036 tcg_gen_mov_tl(ret, arg1);
1037 }
1038 if (compute_Rc0) {
1039 gen_set_Rc0(ctx, ret);
1040 }
d9bce9d9 1041}
74637406 1042GEN_HANDLER(addic, 0x0C, 0xFF, 0xFF, 0x00000000, PPC_INTEGER)
d9bce9d9 1043{
74637406 1044 gen_op_addic(ctx, cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)], 0);
d9bce9d9 1045}
74637406 1046GEN_HANDLER2(addic_, "addic.", 0x0D, 0xFF, 0xFF, 0x00000000, PPC_INTEGER)
d9bce9d9 1047{
74637406 1048 gen_op_addic(ctx, cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)], 1);
d9bce9d9 1049}
74637406
AJ
1050/* addis */
1051GEN_HANDLER(addis, 0x0F, 0xFF, 0xFF, 0x00000000, PPC_INTEGER)
d9bce9d9 1052{
74637406
AJ
1053 target_long simm = SIMM(ctx->opcode);
1054
1055 if (rA(ctx->opcode) == 0) {
1056 /* lis case */
1057 tcg_gen_movi_tl(cpu_gpr[rD(ctx->opcode)], simm << 16);
1058 } else {
1059 tcg_gen_addi_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)], simm << 16);
1060 }
d9bce9d9 1061}
74637406
AJ
1062
1063static always_inline void gen_op_arith_divw (DisasContext *ctx, TCGv ret, TCGv arg1, TCGv arg2,
1064 int sign, int compute_ov)
d9bce9d9 1065{
2ef1b120
AJ
1066 int l1 = gen_new_label();
1067 int l2 = gen_new_label();
a7812ae4
PB
1068 TCGv_i32 t0 = tcg_temp_local_new_i32();
1069 TCGv_i32 t1 = tcg_temp_local_new_i32();
74637406 1070
2ef1b120
AJ
1071 tcg_gen_trunc_tl_i32(t0, arg1);
1072 tcg_gen_trunc_tl_i32(t1, arg2);
1073 tcg_gen_brcondi_i32(TCG_COND_EQ, t1, 0, l1);
74637406 1074 if (sign) {
2ef1b120
AJ
1075 int l3 = gen_new_label();
1076 tcg_gen_brcondi_i32(TCG_COND_NE, t1, -1, l3);
1077 tcg_gen_brcondi_i32(TCG_COND_EQ, t0, INT32_MIN, l1);
74637406 1078 gen_set_label(l3);
2ef1b120 1079 tcg_gen_div_i32(t0, t0, t1);
74637406 1080 } else {
2ef1b120 1081 tcg_gen_divu_i32(t0, t0, t1);
74637406
AJ
1082 }
1083 if (compute_ov) {
1084 tcg_gen_andi_tl(cpu_xer, cpu_xer, ~(1 << XER_OV));
1085 }
1086 tcg_gen_br(l2);
1087 gen_set_label(l1);
1088 if (sign) {
2ef1b120 1089 tcg_gen_sari_i32(t0, t0, 31);
74637406
AJ
1090 } else {
1091 tcg_gen_movi_i32(t0, 0);
1092 }
1093 if (compute_ov) {
1094 tcg_gen_ori_tl(cpu_xer, cpu_xer, (1 << XER_OV) | (1 << XER_SO));
1095 }
1096 gen_set_label(l2);
2ef1b120 1097 tcg_gen_extu_i32_tl(ret, t0);
a7812ae4
PB
1098 tcg_temp_free_i32(t0);
1099 tcg_temp_free_i32(t1);
74637406
AJ
1100 if (unlikely(Rc(ctx->opcode) != 0))
1101 gen_set_Rc0(ctx, ret);
d9bce9d9 1102}
74637406
AJ
1103/* Div functions */
1104#define GEN_INT_ARITH_DIVW(name, opc3, sign, compute_ov) \
1105GEN_HANDLER(name, 0x1F, 0x0B, opc3, 0x00000000, PPC_INTEGER) \
1106{ \
1107 gen_op_arith_divw(ctx, cpu_gpr[rD(ctx->opcode)], \
1108 cpu_gpr[rA(ctx->opcode)], cpu_gpr[rB(ctx->opcode)], \
1109 sign, compute_ov); \
1110}
1111/* divwu divwu. divwuo divwuo. */
1112GEN_INT_ARITH_DIVW(divwu, 0x0E, 0, 0);
1113GEN_INT_ARITH_DIVW(divwuo, 0x1E, 0, 1);
1114/* divw divw. divwo divwo. */
1115GEN_INT_ARITH_DIVW(divw, 0x0F, 1, 0);
1116GEN_INT_ARITH_DIVW(divwo, 0x1F, 1, 1);
d9bce9d9 1117#if defined(TARGET_PPC64)
2ef1b120
AJ
1118static always_inline void gen_op_arith_divd (DisasContext *ctx, TCGv ret, TCGv arg1, TCGv arg2,
1119 int sign, int compute_ov)
d9bce9d9 1120{
2ef1b120
AJ
1121 int l1 = gen_new_label();
1122 int l2 = gen_new_label();
74637406
AJ
1123
1124 tcg_gen_brcondi_i64(TCG_COND_EQ, arg2, 0, l1);
1125 if (sign) {
2ef1b120 1126 int l3 = gen_new_label();
74637406
AJ
1127 tcg_gen_brcondi_i64(TCG_COND_NE, arg2, -1, l3);
1128 tcg_gen_brcondi_i64(TCG_COND_EQ, arg1, INT64_MIN, l1);
1129 gen_set_label(l3);
74637406
AJ
1130 tcg_gen_div_i64(ret, arg1, arg2);
1131 } else {
1132 tcg_gen_divu_i64(ret, arg1, arg2);
1133 }
1134 if (compute_ov) {
1135 tcg_gen_andi_tl(cpu_xer, cpu_xer, ~(1 << XER_OV));
1136 }
1137 tcg_gen_br(l2);
1138 gen_set_label(l1);
1139 if (sign) {
1140 tcg_gen_sari_i64(ret, arg1, 63);
1141 } else {
1142 tcg_gen_movi_i64(ret, 0);
1143 }
1144 if (compute_ov) {
1145 tcg_gen_ori_tl(cpu_xer, cpu_xer, (1 << XER_OV) | (1 << XER_SO));
1146 }
1147 gen_set_label(l2);
1148 if (unlikely(Rc(ctx->opcode) != 0))
1149 gen_set_Rc0(ctx, ret);
d9bce9d9 1150}
74637406
AJ
1151#define GEN_INT_ARITH_DIVD(name, opc3, sign, compute_ov) \
1152GEN_HANDLER(name, 0x1F, 0x09, opc3, 0x00000000, PPC_64B) \
1153{ \
2ef1b120
AJ
1154 gen_op_arith_divd(ctx, cpu_gpr[rD(ctx->opcode)], \
1155 cpu_gpr[rA(ctx->opcode)], cpu_gpr[rB(ctx->opcode)], \
1156 sign, compute_ov); \
74637406
AJ
1157}
1158/* divwu divwu. divwuo divwuo. */
1159GEN_INT_ARITH_DIVD(divdu, 0x0E, 0, 0);
1160GEN_INT_ARITH_DIVD(divduo, 0x1E, 0, 1);
1161/* divw divw. divwo divwo. */
1162GEN_INT_ARITH_DIVD(divd, 0x0F, 1, 0);
1163GEN_INT_ARITH_DIVD(divdo, 0x1F, 1, 1);
d9bce9d9 1164#endif
74637406
AJ
1165
1166/* mulhw mulhw. */
1167GEN_HANDLER(mulhw, 0x1F, 0x0B, 0x02, 0x00000400, PPC_INTEGER)
d9bce9d9 1168{
a7812ae4 1169 TCGv_i64 t0, t1;
74637406 1170
a7812ae4
PB
1171 t0 = tcg_temp_new_i64();
1172 t1 = tcg_temp_new_i64();
74637406
AJ
1173#if defined(TARGET_PPC64)
1174 tcg_gen_ext32s_tl(t0, cpu_gpr[rA(ctx->opcode)]);
1175 tcg_gen_ext32s_tl(t1, cpu_gpr[rB(ctx->opcode)]);
1176 tcg_gen_mul_i64(t0, t0, t1);
1177 tcg_gen_shri_i64(cpu_gpr[rD(ctx->opcode)], t0, 32);
1178#else
1179 tcg_gen_ext_tl_i64(t0, cpu_gpr[rA(ctx->opcode)]);
1180 tcg_gen_ext_tl_i64(t1, cpu_gpr[rB(ctx->opcode)]);
1181 tcg_gen_mul_i64(t0, t0, t1);
1182 tcg_gen_shri_i64(t0, t0, 32);
1183 tcg_gen_trunc_i64_tl(cpu_gpr[rD(ctx->opcode)], t0);
1184#endif
a7812ae4
PB
1185 tcg_temp_free_i64(t0);
1186 tcg_temp_free_i64(t1);
74637406
AJ
1187 if (unlikely(Rc(ctx->opcode) != 0))
1188 gen_set_Rc0(ctx, cpu_gpr[rD(ctx->opcode)]);
d9bce9d9 1189}
74637406
AJ
1190/* mulhwu mulhwu. */
1191GEN_HANDLER(mulhwu, 0x1F, 0x0B, 0x00, 0x00000400, PPC_INTEGER)
d9bce9d9 1192{
a7812ae4 1193 TCGv_i64 t0, t1;
74637406 1194
a7812ae4
PB
1195 t0 = tcg_temp_new_i64();
1196 t1 = tcg_temp_new_i64();
d9bce9d9 1197#if defined(TARGET_PPC64)
74637406
AJ
1198 tcg_gen_ext32u_i64(t0, cpu_gpr[rA(ctx->opcode)]);
1199 tcg_gen_ext32u_i64(t1, cpu_gpr[rB(ctx->opcode)]);
1200 tcg_gen_mul_i64(t0, t0, t1);
1201 tcg_gen_shri_i64(cpu_gpr[rD(ctx->opcode)], t0, 32);
1202#else
1203 tcg_gen_extu_tl_i64(t0, cpu_gpr[rA(ctx->opcode)]);
1204 tcg_gen_extu_tl_i64(t1, cpu_gpr[rB(ctx->opcode)]);
1205 tcg_gen_mul_i64(t0, t0, t1);
1206 tcg_gen_shri_i64(t0, t0, 32);
1207 tcg_gen_trunc_i64_tl(cpu_gpr[rD(ctx->opcode)], t0);
1208#endif
a7812ae4
PB
1209 tcg_temp_free_i64(t0);
1210 tcg_temp_free_i64(t1);
74637406
AJ
1211 if (unlikely(Rc(ctx->opcode) != 0))
1212 gen_set_Rc0(ctx, cpu_gpr[rD(ctx->opcode)]);
d9bce9d9 1213}
74637406
AJ
1214/* mullw mullw. */
1215GEN_HANDLER(mullw, 0x1F, 0x0B, 0x07, 0x00000000, PPC_INTEGER)
d9bce9d9 1216{
74637406
AJ
1217 tcg_gen_mul_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)],
1218 cpu_gpr[rB(ctx->opcode)]);
1e4c090f 1219 tcg_gen_ext32s_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rD(ctx->opcode)]);
74637406
AJ
1220 if (unlikely(Rc(ctx->opcode) != 0))
1221 gen_set_Rc0(ctx, cpu_gpr[rD(ctx->opcode)]);
d9bce9d9 1222}
74637406
AJ
1223/* mullwo mullwo. */
1224GEN_HANDLER(mullwo, 0x1F, 0x0B, 0x17, 0x00000000, PPC_INTEGER)
d9bce9d9 1225{
74637406 1226 int l1;
a7812ae4 1227 TCGv_i64 t0, t1;
74637406 1228
a7812ae4
PB
1229 t0 = tcg_temp_new_i64();
1230 t1 = tcg_temp_new_i64();
74637406
AJ
1231 l1 = gen_new_label();
1232 /* Start with XER OV disabled, the most likely case */
1233 tcg_gen_andi_tl(cpu_xer, cpu_xer, ~(1 << XER_OV));
1234#if defined(TARGET_PPC64)
1235 tcg_gen_ext32s_i64(t0, cpu_gpr[rA(ctx->opcode)]);
1236 tcg_gen_ext32s_i64(t1, cpu_gpr[rB(ctx->opcode)]);
1237#else
1238 tcg_gen_ext_tl_i64(t0, cpu_gpr[rA(ctx->opcode)]);
1239 tcg_gen_ext_tl_i64(t1, cpu_gpr[rB(ctx->opcode)]);
d9bce9d9 1240#endif
74637406
AJ
1241 tcg_gen_mul_i64(t0, t0, t1);
1242#if defined(TARGET_PPC64)
1243 tcg_gen_ext32s_i64(cpu_gpr[rD(ctx->opcode)], t0);
1244 tcg_gen_brcond_i64(TCG_COND_EQ, t0, cpu_gpr[rD(ctx->opcode)], l1);
1245#else
1246 tcg_gen_trunc_i64_tl(cpu_gpr[rD(ctx->opcode)], t0);
1247 tcg_gen_ext32s_i64(t1, t0);
1248 tcg_gen_brcond_i64(TCG_COND_EQ, t0, t1, l1);
1249#endif
1250 tcg_gen_ori_tl(cpu_xer, cpu_xer, (1 << XER_OV) | (1 << XER_SO));
1251 gen_set_label(l1);
a7812ae4
PB
1252 tcg_temp_free_i64(t0);
1253 tcg_temp_free_i64(t1);
74637406
AJ
1254 if (unlikely(Rc(ctx->opcode) != 0))
1255 gen_set_Rc0(ctx, cpu_gpr[rD(ctx->opcode)]);
d9bce9d9 1256}
74637406
AJ
1257/* mulli */
1258GEN_HANDLER(mulli, 0x07, 0xFF, 0xFF, 0x00000000, PPC_INTEGER)
d9bce9d9 1259{
74637406
AJ
1260 tcg_gen_muli_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)],
1261 SIMM(ctx->opcode));
d9bce9d9
JM
1262}
1263#if defined(TARGET_PPC64)
74637406
AJ
1264#define GEN_INT_ARITH_MUL_HELPER(name, opc3) \
1265GEN_HANDLER(name, 0x1F, 0x09, opc3, 0x00000000, PPC_64B) \
1266{ \
a7812ae4 1267 gen_helper_##name (cpu_gpr[rD(ctx->opcode)], \
74637406
AJ
1268 cpu_gpr[rA(ctx->opcode)], cpu_gpr[rB(ctx->opcode)]); \
1269 if (unlikely(Rc(ctx->opcode) != 0)) \
1270 gen_set_Rc0(ctx, cpu_gpr[rD(ctx->opcode)]); \
d9bce9d9 1271}
74637406
AJ
1272/* mulhd mulhd. */
1273GEN_INT_ARITH_MUL_HELPER(mulhdu, 0x00);
1274/* mulhdu mulhdu. */
1275GEN_INT_ARITH_MUL_HELPER(mulhd, 0x02);
1276/* mulld mulld. */
1277GEN_HANDLER(mulld, 0x1F, 0x09, 0x07, 0x00000000, PPC_64B)
d9bce9d9 1278{
74637406
AJ
1279 tcg_gen_mul_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)],
1280 cpu_gpr[rB(ctx->opcode)]);
1281 if (unlikely(Rc(ctx->opcode) != 0))
1282 gen_set_Rc0(ctx, cpu_gpr[rD(ctx->opcode)]);
d9bce9d9 1283}
74637406
AJ
1284/* mulldo mulldo. */
1285GEN_INT_ARITH_MUL_HELPER(mulldo, 0x17);
d9bce9d9 1286#endif
74637406
AJ
1287
1288/* neg neg. nego nego. */
ec6469a3 1289static always_inline void gen_op_arith_neg (DisasContext *ctx, TCGv ret, TCGv arg1, int ov_check)
d9bce9d9 1290{
ec6469a3
AJ
1291 int l1 = gen_new_label();
1292 int l2 = gen_new_label();
a7812ae4 1293 TCGv t0 = tcg_temp_local_new();
d9bce9d9 1294#if defined(TARGET_PPC64)
74637406 1295 if (ctx->sf_mode) {
741a7444 1296 tcg_gen_mov_tl(t0, arg1);
ec6469a3
AJ
1297 tcg_gen_brcondi_tl(TCG_COND_EQ, t0, INT64_MIN, l1);
1298 } else
1299#endif
1300 {
1301 tcg_gen_ext32s_tl(t0, arg1);
74637406
AJ
1302 tcg_gen_brcondi_tl(TCG_COND_EQ, t0, INT32_MIN, l1);
1303 }
74637406
AJ
1304 tcg_gen_neg_tl(ret, arg1);
1305 if (ov_check) {
1306 tcg_gen_andi_tl(cpu_xer, cpu_xer, ~(1 << XER_OV));
1307 }
1308 tcg_gen_br(l2);
1309 gen_set_label(l1);
ec6469a3 1310 tcg_gen_mov_tl(ret, t0);
74637406
AJ
1311 if (ov_check) {
1312 tcg_gen_ori_tl(cpu_xer, cpu_xer, (1 << XER_OV) | (1 << XER_SO));
1313 }
1314 gen_set_label(l2);
ec6469a3 1315 tcg_temp_free(t0);
74637406
AJ
1316 if (unlikely(Rc(ctx->opcode) != 0))
1317 gen_set_Rc0(ctx, ret);
1318}
1319GEN_HANDLER(neg, 0x1F, 0x08, 0x03, 0x0000F800, PPC_INTEGER)
d9bce9d9 1320{
ec6469a3 1321 gen_op_arith_neg(ctx, cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)], 0);
d9bce9d9 1322}
74637406 1323GEN_HANDLER(nego, 0x1F, 0x08, 0x13, 0x0000F800, PPC_INTEGER)
79aceca5 1324{
ec6469a3 1325 gen_op_arith_neg(ctx, cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)], 1);
79aceca5 1326}
74637406
AJ
1327
1328/* Common subf function */
1329static always_inline void gen_op_arith_subf(DisasContext *ctx, TCGv ret, TCGv arg1, TCGv arg2,
1330 int add_ca, int compute_ca, int compute_ov)
79aceca5 1331{
74637406 1332 TCGv t0, t1;
76a66253 1333
74637406 1334 if ((!compute_ca && !compute_ov) ||
a7812ae4 1335 (!TCGV_EQUAL(ret, arg1) && !TCGV_EQUAL(ret, arg2))) {
74637406 1336 t0 = ret;
e864cabd 1337 } else {
a7812ae4 1338 t0 = tcg_temp_local_new();
d9bce9d9 1339 }
76a66253 1340
74637406 1341 if (add_ca) {
a7812ae4 1342 t1 = tcg_temp_local_new();
74637406
AJ
1343 tcg_gen_andi_tl(t1, cpu_xer, (1 << XER_CA));
1344 tcg_gen_shri_tl(t1, t1, XER_CA);
d9bce9d9 1345 }
79aceca5 1346
74637406
AJ
1347 if (compute_ca && compute_ov) {
1348 /* Start with XER CA and OV disabled, the most likely case */
1349 tcg_gen_andi_tl(cpu_xer, cpu_xer, ~((1 << XER_CA) | (1 << XER_OV)));
1350 } else if (compute_ca) {
1351 /* Start with XER CA disabled, the most likely case */
1352 tcg_gen_andi_tl(cpu_xer, cpu_xer, ~(1 << XER_CA));
1353 } else if (compute_ov) {
1354 /* Start with XER OV disabled, the most likely case */
1355 tcg_gen_andi_tl(cpu_xer, cpu_xer, ~(1 << XER_OV));
1356 }
1357
1358 if (add_ca) {
1359 tcg_gen_not_tl(t0, arg1);
1360 tcg_gen_add_tl(t0, t0, arg2);
1361 gen_op_arith_compute_ca(ctx, t0, arg2, 0);
1362 tcg_gen_add_tl(t0, t0, t1);
1363 gen_op_arith_compute_ca(ctx, t0, t1, 0);
1364 tcg_temp_free(t1);
79aceca5 1365 } else {
74637406
AJ
1366 tcg_gen_sub_tl(t0, arg2, arg1);
1367 if (compute_ca) {
1368 gen_op_arith_compute_ca(ctx, t0, arg2, 1);
1369 }
1370 }
1371 if (compute_ov) {
1372 gen_op_arith_compute_ov(ctx, t0, arg1, arg2, 1);
1373 }
1374
1375 if (unlikely(Rc(ctx->opcode) != 0))
1376 gen_set_Rc0(ctx, t0);
1377
a7812ae4 1378 if (!TCGV_EQUAL(t0, ret)) {
74637406
AJ
1379 tcg_gen_mov_tl(ret, t0);
1380 tcg_temp_free(t0);
79aceca5 1381 }
79aceca5 1382}
74637406
AJ
1383/* Sub functions with Two operands functions */
1384#define GEN_INT_ARITH_SUBF(name, opc3, add_ca, compute_ca, compute_ov) \
1385GEN_HANDLER(name, 0x1F, 0x08, opc3, 0x00000000, PPC_INTEGER) \
1386{ \
1387 gen_op_arith_subf(ctx, cpu_gpr[rD(ctx->opcode)], \
1388 cpu_gpr[rA(ctx->opcode)], cpu_gpr[rB(ctx->opcode)], \
1389 add_ca, compute_ca, compute_ov); \
1390}
1391/* Sub functions with one operand and one immediate */
1392#define GEN_INT_ARITH_SUBF_CONST(name, opc3, const_val, \
1393 add_ca, compute_ca, compute_ov) \
1394GEN_HANDLER(name, 0x1F, 0x08, opc3, 0x0000F800, PPC_INTEGER) \
1395{ \
1396 TCGv t0 = tcg_const_local_tl(const_val); \
1397 gen_op_arith_subf(ctx, cpu_gpr[rD(ctx->opcode)], \
1398 cpu_gpr[rA(ctx->opcode)], t0, \
1399 add_ca, compute_ca, compute_ov); \
1400 tcg_temp_free(t0); \
1401}
1402/* subf subf. subfo subfo. */
1403GEN_INT_ARITH_SUBF(subf, 0x01, 0, 0, 0)
1404GEN_INT_ARITH_SUBF(subfo, 0x11, 0, 0, 1)
1405/* subfc subfc. subfco subfco. */
1406GEN_INT_ARITH_SUBF(subfc, 0x00, 0, 1, 0)
1407GEN_INT_ARITH_SUBF(subfco, 0x10, 0, 1, 1)
1408/* subfe subfe. subfeo subfo. */
1409GEN_INT_ARITH_SUBF(subfe, 0x04, 1, 1, 0)
1410GEN_INT_ARITH_SUBF(subfeo, 0x14, 1, 1, 1)
1411/* subfme subfme. subfmeo subfmeo. */
1412GEN_INT_ARITH_SUBF_CONST(subfme, 0x07, -1LL, 1, 1, 0)
1413GEN_INT_ARITH_SUBF_CONST(subfmeo, 0x17, -1LL, 1, 1, 1)
1414/* subfze subfze. subfzeo subfzeo.*/
1415GEN_INT_ARITH_SUBF_CONST(subfze, 0x06, 0, 1, 1, 0)
1416GEN_INT_ARITH_SUBF_CONST(subfzeo, 0x16, 0, 1, 1, 1)
79aceca5
FB
1417/* subfic */
1418GEN_HANDLER(subfic, 0x08, 0xFF, 0xFF, 0x00000000, PPC_INTEGER)
1419{
74637406
AJ
1420 /* Start with XER CA and OV disabled, the most likely case */
1421 tcg_gen_andi_tl(cpu_xer, cpu_xer, ~(1 << XER_CA));
a7812ae4 1422 TCGv t0 = tcg_temp_local_new();
74637406
AJ
1423 TCGv t1 = tcg_const_local_tl(SIMM(ctx->opcode));
1424 tcg_gen_sub_tl(t0, t1, cpu_gpr[rA(ctx->opcode)]);
1425 gen_op_arith_compute_ca(ctx, t0, t1, 1);
1426 tcg_temp_free(t1);
1427 tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], t0);
1428 tcg_temp_free(t0);
79aceca5
FB
1429}
1430
79aceca5 1431/*** Integer logical ***/
26d67362
AJ
1432#define GEN_LOGICAL2(name, tcg_op, opc, type) \
1433GEN_HANDLER(name, 0x1F, 0x1C, opc, 0x00000000, type) \
79aceca5 1434{ \
26d67362
AJ
1435 tcg_op(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)], \
1436 cpu_gpr[rB(ctx->opcode)]); \
76a66253 1437 if (unlikely(Rc(ctx->opcode) != 0)) \
26d67362 1438 gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]); \
79aceca5 1439}
79aceca5 1440
26d67362 1441#define GEN_LOGICAL1(name, tcg_op, opc, type) \
d9bce9d9 1442GEN_HANDLER(name, 0x1F, 0x1A, opc, 0x00000000, type) \
79aceca5 1443{ \
26d67362 1444 tcg_op(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)]); \
76a66253 1445 if (unlikely(Rc(ctx->opcode) != 0)) \
26d67362 1446 gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]); \
79aceca5
FB
1447}
1448
1449/* and & and. */
26d67362 1450GEN_LOGICAL2(and, tcg_gen_and_tl, 0x00, PPC_INTEGER);
79aceca5 1451/* andc & andc. */
26d67362 1452GEN_LOGICAL2(andc, tcg_gen_andc_tl, 0x01, PPC_INTEGER);
79aceca5 1453/* andi. */
c7697e1f 1454GEN_HANDLER2(andi_, "andi.", 0x1C, 0xFF, 0xFF, 0x00000000, PPC_INTEGER)
79aceca5 1455{
26d67362
AJ
1456 tcg_gen_andi_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)], UIMM(ctx->opcode));
1457 gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
79aceca5
FB
1458}
1459/* andis. */
c7697e1f 1460GEN_HANDLER2(andis_, "andis.", 0x1D, 0xFF, 0xFF, 0x00000000, PPC_INTEGER)
79aceca5 1461{
26d67362
AJ
1462 tcg_gen_andi_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)], UIMM(ctx->opcode) << 16);
1463 gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
79aceca5 1464}
79aceca5 1465/* cntlzw */
26d67362
AJ
1466GEN_HANDLER(cntlzw, 0x1F, 0x1A, 0x00, 0x00000000, PPC_INTEGER)
1467{
a7812ae4 1468 gen_helper_cntlzw(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)]);
26d67362 1469 if (unlikely(Rc(ctx->opcode) != 0))
2e31f5d3 1470 gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
26d67362 1471}
79aceca5 1472/* eqv & eqv. */
26d67362 1473GEN_LOGICAL2(eqv, tcg_gen_eqv_tl, 0x08, PPC_INTEGER);
79aceca5 1474/* extsb & extsb. */
26d67362 1475GEN_LOGICAL1(extsb, tcg_gen_ext8s_tl, 0x1D, PPC_INTEGER);
79aceca5 1476/* extsh & extsh. */
26d67362 1477GEN_LOGICAL1(extsh, tcg_gen_ext16s_tl, 0x1C, PPC_INTEGER);
79aceca5 1478/* nand & nand. */
26d67362 1479GEN_LOGICAL2(nand, tcg_gen_nand_tl, 0x0E, PPC_INTEGER);
79aceca5 1480/* nor & nor. */
26d67362 1481GEN_LOGICAL2(nor, tcg_gen_nor_tl, 0x03, PPC_INTEGER);
79aceca5 1482/* or & or. */
9a64fbe4
FB
1483GEN_HANDLER(or, 0x1F, 0x1C, 0x0D, 0x00000000, PPC_INTEGER)
1484{
76a66253
JM
1485 int rs, ra, rb;
1486
1487 rs = rS(ctx->opcode);
1488 ra = rA(ctx->opcode);
1489 rb = rB(ctx->opcode);
1490 /* Optimisation for mr. ri case */
1491 if (rs != ra || rs != rb) {
26d67362
AJ
1492 if (rs != rb)
1493 tcg_gen_or_tl(cpu_gpr[ra], cpu_gpr[rs], cpu_gpr[rb]);
1494 else
1495 tcg_gen_mov_tl(cpu_gpr[ra], cpu_gpr[rs]);
76a66253 1496 if (unlikely(Rc(ctx->opcode) != 0))
26d67362 1497 gen_set_Rc0(ctx, cpu_gpr[ra]);
76a66253 1498 } else if (unlikely(Rc(ctx->opcode) != 0)) {
26d67362 1499 gen_set_Rc0(ctx, cpu_gpr[rs]);
c80f84e3
JM
1500#if defined(TARGET_PPC64)
1501 } else {
26d67362
AJ
1502 int prio = 0;
1503
c80f84e3
JM
1504 switch (rs) {
1505 case 1:
1506 /* Set process priority to low */
26d67362 1507 prio = 2;
c80f84e3
JM
1508 break;
1509 case 6:
1510 /* Set process priority to medium-low */
26d67362 1511 prio = 3;
c80f84e3
JM
1512 break;
1513 case 2:
1514 /* Set process priority to normal */
26d67362 1515 prio = 4;
c80f84e3 1516 break;
be147d08
JM
1517#if !defined(CONFIG_USER_ONLY)
1518 case 31:
1519 if (ctx->supervisor > 0) {
1520 /* Set process priority to very low */
26d67362 1521 prio = 1;
be147d08
JM
1522 }
1523 break;
1524 case 5:
1525 if (ctx->supervisor > 0) {
1526 /* Set process priority to medium-hight */
26d67362 1527 prio = 5;
be147d08
JM
1528 }
1529 break;
1530 case 3:
1531 if (ctx->supervisor > 0) {
1532 /* Set process priority to high */
26d67362 1533 prio = 6;
be147d08
JM
1534 }
1535 break;
be147d08
JM
1536 case 7:
1537 if (ctx->supervisor > 1) {
1538 /* Set process priority to very high */
26d67362 1539 prio = 7;
be147d08
JM
1540 }
1541 break;
be147d08 1542#endif
c80f84e3
JM
1543 default:
1544 /* nop */
1545 break;
1546 }
26d67362 1547 if (prio) {
a7812ae4 1548 TCGv t0 = tcg_temp_new();
ea363694
AJ
1549 tcg_gen_ld_tl(t0, cpu_env, offsetof(CPUState, spr[SPR_PPR]));
1550 tcg_gen_andi_tl(t0, t0, ~0x001C000000000000ULL);
1551 tcg_gen_ori_tl(t0, t0, ((uint64_t)prio) << 50);
1552 tcg_gen_st_tl(t0, cpu_env, offsetof(CPUState, spr[SPR_PPR]));
1553 tcg_temp_free(t0);
26d67362 1554 }
c80f84e3 1555#endif
9a64fbe4 1556 }
9a64fbe4 1557}
79aceca5 1558/* orc & orc. */
26d67362 1559GEN_LOGICAL2(orc, tcg_gen_orc_tl, 0x0C, PPC_INTEGER);
79aceca5 1560/* xor & xor. */
9a64fbe4
FB
1561GEN_HANDLER(xor, 0x1F, 0x1C, 0x09, 0x00000000, PPC_INTEGER)
1562{
9a64fbe4 1563 /* Optimisation for "set to zero" case */
26d67362 1564 if (rS(ctx->opcode) != rB(ctx->opcode))
312179c4 1565 tcg_gen_xor_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)], cpu_gpr[rB(ctx->opcode)]);
26d67362
AJ
1566 else
1567 tcg_gen_movi_tl(cpu_gpr[rA(ctx->opcode)], 0);
76a66253 1568 if (unlikely(Rc(ctx->opcode) != 0))
26d67362 1569 gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
9a64fbe4 1570}
79aceca5
FB
1571/* ori */
1572GEN_HANDLER(ori, 0x18, 0xFF, 0xFF, 0x00000000, PPC_INTEGER)
1573{
76a66253 1574 target_ulong uimm = UIMM(ctx->opcode);
79aceca5 1575
9a64fbe4
FB
1576 if (rS(ctx->opcode) == rA(ctx->opcode) && uimm == 0) {
1577 /* NOP */
76a66253 1578 /* XXX: should handle special NOPs for POWER series */
9a64fbe4 1579 return;
76a66253 1580 }
26d67362 1581 tcg_gen_ori_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)], uimm);
79aceca5
FB
1582}
1583/* oris */
1584GEN_HANDLER(oris, 0x19, 0xFF, 0xFF, 0x00000000, PPC_INTEGER)
1585{
76a66253 1586 target_ulong uimm = UIMM(ctx->opcode);
79aceca5 1587
9a64fbe4
FB
1588 if (rS(ctx->opcode) == rA(ctx->opcode) && uimm == 0) {
1589 /* NOP */
1590 return;
76a66253 1591 }
26d67362 1592 tcg_gen_ori_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)], uimm << 16);
79aceca5
FB
1593}
1594/* xori */
1595GEN_HANDLER(xori, 0x1A, 0xFF, 0xFF, 0x00000000, PPC_INTEGER)
1596{
76a66253 1597 target_ulong uimm = UIMM(ctx->opcode);
9a64fbe4
FB
1598
1599 if (rS(ctx->opcode) == rA(ctx->opcode) && uimm == 0) {
1600 /* NOP */
1601 return;
1602 }
26d67362 1603 tcg_gen_xori_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)], uimm);
79aceca5 1604}
79aceca5
FB
1605/* xoris */
1606GEN_HANDLER(xoris, 0x1B, 0xFF, 0xFF, 0x00000000, PPC_INTEGER)
1607{
76a66253 1608 target_ulong uimm = UIMM(ctx->opcode);
9a64fbe4
FB
1609
1610 if (rS(ctx->opcode) == rA(ctx->opcode) && uimm == 0) {
1611 /* NOP */
1612 return;
1613 }
26d67362 1614 tcg_gen_xori_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)], uimm << 16);
79aceca5 1615}
d9bce9d9 1616/* popcntb : PowerPC 2.03 specification */
05332d70 1617GEN_HANDLER(popcntb, 0x1F, 0x03, 0x03, 0x0000F801, PPC_POPCNTB)
d9bce9d9 1618{
d9bce9d9
JM
1619#if defined(TARGET_PPC64)
1620 if (ctx->sf_mode)
a7812ae4 1621 gen_helper_popcntb_64(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)]);
d9bce9d9
JM
1622 else
1623#endif
a7812ae4 1624 gen_helper_popcntb(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)]);
d9bce9d9
JM
1625}
1626
1627#if defined(TARGET_PPC64)
1628/* extsw & extsw. */
26d67362 1629GEN_LOGICAL1(extsw, tcg_gen_ext32s_tl, 0x1E, PPC_64B);
d9bce9d9 1630/* cntlzd */
26d67362
AJ
1631GEN_HANDLER(cntlzd, 0x1F, 0x1A, 0x01, 0x00000000, PPC_64B)
1632{
a7812ae4 1633 gen_helper_cntlzd(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)]);
26d67362
AJ
1634 if (unlikely(Rc(ctx->opcode) != 0))
1635 gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
1636}
d9bce9d9
JM
1637#endif
1638
79aceca5
FB
1639/*** Integer rotate ***/
1640/* rlwimi & rlwimi. */
1641GEN_HANDLER(rlwimi, 0x14, 0xFF, 0xFF, 0x00000000, PPC_INTEGER)
1642{
76a66253 1643 uint32_t mb, me, sh;
79aceca5
FB
1644
1645 mb = MB(ctx->opcode);
1646 me = ME(ctx->opcode);
76a66253 1647 sh = SH(ctx->opcode);
d03ef511
AJ
1648 if (likely(sh == 0 && mb == 0 && me == 31)) {
1649 tcg_gen_ext32u_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)]);
1650 } else {
d03ef511 1651 target_ulong mask;
a7812ae4
PB
1652 TCGv t1;
1653 TCGv t0 = tcg_temp_new();
54843a58 1654#if defined(TARGET_PPC64)
a7812ae4
PB
1655 TCGv_i32 t2 = tcg_temp_new_i32();
1656 tcg_gen_trunc_i64_i32(t2, cpu_gpr[rS(ctx->opcode)]);
1657 tcg_gen_rotli_i32(t2, t2, sh);
1658 tcg_gen_extu_i32_i64(t0, t2);
1659 tcg_temp_free_i32(t2);
54843a58
AJ
1660#else
1661 tcg_gen_rotli_i32(t0, cpu_gpr[rS(ctx->opcode)], sh);
1662#endif
76a66253 1663#if defined(TARGET_PPC64)
d03ef511
AJ
1664 mb += 32;
1665 me += 32;
76a66253 1666#endif
d03ef511 1667 mask = MASK(mb, me);
a7812ae4 1668 t1 = tcg_temp_new();
d03ef511
AJ
1669 tcg_gen_andi_tl(t0, t0, mask);
1670 tcg_gen_andi_tl(t1, cpu_gpr[rA(ctx->opcode)], ~mask);
1671 tcg_gen_or_tl(cpu_gpr[rA(ctx->opcode)], t0, t1);
1672 tcg_temp_free(t0);
1673 tcg_temp_free(t1);
1674 }
76a66253 1675 if (unlikely(Rc(ctx->opcode) != 0))
d03ef511 1676 gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
79aceca5
FB
1677}
1678/* rlwinm & rlwinm. */
1679GEN_HANDLER(rlwinm, 0x15, 0xFF, 0xFF, 0x00000000, PPC_INTEGER)
1680{
1681 uint32_t mb, me, sh;
3b46e624 1682
79aceca5
FB
1683 sh = SH(ctx->opcode);
1684 mb = MB(ctx->opcode);
1685 me = ME(ctx->opcode);
d03ef511
AJ
1686
1687 if (likely(mb == 0 && me == (31 - sh))) {
1688 if (likely(sh == 0)) {
1689 tcg_gen_ext32u_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)]);
1690 } else {
a7812ae4 1691 TCGv t0 = tcg_temp_new();
d03ef511
AJ
1692 tcg_gen_ext32u_tl(t0, cpu_gpr[rS(ctx->opcode)]);
1693 tcg_gen_shli_tl(t0, t0, sh);
1694 tcg_gen_ext32u_tl(cpu_gpr[rA(ctx->opcode)], t0);
1695 tcg_temp_free(t0);
79aceca5 1696 }
d03ef511 1697 } else if (likely(sh != 0 && me == 31 && sh == (32 - mb))) {
a7812ae4 1698 TCGv t0 = tcg_temp_new();
d03ef511
AJ
1699 tcg_gen_ext32u_tl(t0, cpu_gpr[rS(ctx->opcode)]);
1700 tcg_gen_shri_tl(t0, t0, mb);
1701 tcg_gen_ext32u_tl(cpu_gpr[rA(ctx->opcode)], t0);
1702 tcg_temp_free(t0);
1703 } else {
a7812ae4 1704 TCGv t0 = tcg_temp_new();
54843a58 1705#if defined(TARGET_PPC64)
a7812ae4 1706 TCGv_i32 t1 = tcg_temp_new_i32();
54843a58
AJ
1707 tcg_gen_trunc_i64_i32(t1, cpu_gpr[rS(ctx->opcode)]);
1708 tcg_gen_rotli_i32(t1, t1, sh);
1709 tcg_gen_extu_i32_i64(t0, t1);
a7812ae4 1710 tcg_temp_free_i32(t1);
54843a58
AJ
1711#else
1712 tcg_gen_rotli_i32(t0, cpu_gpr[rS(ctx->opcode)], sh);
1713#endif
76a66253 1714#if defined(TARGET_PPC64)
d03ef511
AJ
1715 mb += 32;
1716 me += 32;
76a66253 1717#endif
d03ef511
AJ
1718 tcg_gen_andi_tl(cpu_gpr[rA(ctx->opcode)], t0, MASK(mb, me));
1719 tcg_temp_free(t0);
1720 }
76a66253 1721 if (unlikely(Rc(ctx->opcode) != 0))
d03ef511 1722 gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
79aceca5
FB
1723}
1724/* rlwnm & rlwnm. */
1725GEN_HANDLER(rlwnm, 0x17, 0xFF, 0xFF, 0x00000000, PPC_INTEGER)
1726{
1727 uint32_t mb, me;
54843a58
AJ
1728 TCGv t0;
1729#if defined(TARGET_PPC64)
a7812ae4 1730 TCGv_i32 t1, t2;
54843a58 1731#endif
79aceca5
FB
1732
1733 mb = MB(ctx->opcode);
1734 me = ME(ctx->opcode);
a7812ae4 1735 t0 = tcg_temp_new();
d03ef511 1736 tcg_gen_andi_tl(t0, cpu_gpr[rB(ctx->opcode)], 0x1f);
54843a58 1737#if defined(TARGET_PPC64)
a7812ae4
PB
1738 t1 = tcg_temp_new_i32();
1739 t2 = tcg_temp_new_i32();
54843a58
AJ
1740 tcg_gen_trunc_i64_i32(t1, cpu_gpr[rS(ctx->opcode)]);
1741 tcg_gen_trunc_i64_i32(t2, t0);
1742 tcg_gen_rotl_i32(t1, t1, t2);
1743 tcg_gen_extu_i32_i64(t0, t1);
a7812ae4
PB
1744 tcg_temp_free_i32(t1);
1745 tcg_temp_free_i32(t2);
54843a58
AJ
1746#else
1747 tcg_gen_rotl_i32(t0, cpu_gpr[rS(ctx->opcode)], t0);
1748#endif
76a66253
JM
1749 if (unlikely(mb != 0 || me != 31)) {
1750#if defined(TARGET_PPC64)
1751 mb += 32;
1752 me += 32;
1753#endif
54843a58 1754 tcg_gen_andi_tl(cpu_gpr[rA(ctx->opcode)], t0, MASK(mb, me));
d03ef511 1755 } else {
54843a58 1756 tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], t0);
79aceca5 1757 }
54843a58 1758 tcg_temp_free(t0);
76a66253 1759 if (unlikely(Rc(ctx->opcode) != 0))
d03ef511 1760 gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
79aceca5
FB
1761}
1762
d9bce9d9
JM
1763#if defined(TARGET_PPC64)
1764#define GEN_PPC64_R2(name, opc1, opc2) \
c7697e1f 1765GEN_HANDLER2(name##0, stringify(name), opc1, opc2, 0xFF, 0x00000000, PPC_64B) \
d9bce9d9
JM
1766{ \
1767 gen_##name(ctx, 0); \
1768} \
c7697e1f
JM
1769GEN_HANDLER2(name##1, stringify(name), opc1, opc2 | 0x10, 0xFF, 0x00000000, \
1770 PPC_64B) \
d9bce9d9
JM
1771{ \
1772 gen_##name(ctx, 1); \
1773}
1774#define GEN_PPC64_R4(name, opc1, opc2) \
c7697e1f 1775GEN_HANDLER2(name##0, stringify(name), opc1, opc2, 0xFF, 0x00000000, PPC_64B) \
d9bce9d9
JM
1776{ \
1777 gen_##name(ctx, 0, 0); \
1778} \
c7697e1f
JM
1779GEN_HANDLER2(name##1, stringify(name), opc1, opc2 | 0x01, 0xFF, 0x00000000, \
1780 PPC_64B) \
d9bce9d9
JM
1781{ \
1782 gen_##name(ctx, 0, 1); \
1783} \
c7697e1f
JM
1784GEN_HANDLER2(name##2, stringify(name), opc1, opc2 | 0x10, 0xFF, 0x00000000, \
1785 PPC_64B) \
d9bce9d9
JM
1786{ \
1787 gen_##name(ctx, 1, 0); \
1788} \
c7697e1f
JM
1789GEN_HANDLER2(name##3, stringify(name), opc1, opc2 | 0x11, 0xFF, 0x00000000, \
1790 PPC_64B) \
d9bce9d9
JM
1791{ \
1792 gen_##name(ctx, 1, 1); \
1793}
51789c41 1794
b068d6a7
JM
1795static always_inline void gen_rldinm (DisasContext *ctx, uint32_t mb,
1796 uint32_t me, uint32_t sh)
51789c41 1797{
d03ef511
AJ
1798 if (likely(sh != 0 && mb == 0 && me == (63 - sh))) {
1799 tcg_gen_shli_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)], sh);
1800 } else if (likely(sh != 0 && me == 63 && sh == (64 - mb))) {
1801 tcg_gen_shri_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)], mb);
1802 } else {
a7812ae4 1803 TCGv t0 = tcg_temp_new();
54843a58 1804 tcg_gen_rotli_tl(t0, cpu_gpr[rS(ctx->opcode)], sh);
d03ef511 1805 if (likely(mb == 0 && me == 63)) {
54843a58 1806 tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], t0);
d03ef511
AJ
1807 } else {
1808 tcg_gen_andi_tl(cpu_gpr[rA(ctx->opcode)], t0, MASK(mb, me));
51789c41 1809 }
d03ef511 1810 tcg_temp_free(t0);
51789c41 1811 }
51789c41 1812 if (unlikely(Rc(ctx->opcode) != 0))
d03ef511 1813 gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
51789c41 1814}
d9bce9d9 1815/* rldicl - rldicl. */
b068d6a7 1816static always_inline void gen_rldicl (DisasContext *ctx, int mbn, int shn)
d9bce9d9 1817{
51789c41 1818 uint32_t sh, mb;
d9bce9d9 1819
9d53c753
JM
1820 sh = SH(ctx->opcode) | (shn << 5);
1821 mb = MB(ctx->opcode) | (mbn << 5);
51789c41 1822 gen_rldinm(ctx, mb, 63, sh);
d9bce9d9 1823}
51789c41 1824GEN_PPC64_R4(rldicl, 0x1E, 0x00);
d9bce9d9 1825/* rldicr - rldicr. */
b068d6a7 1826static always_inline void gen_rldicr (DisasContext *ctx, int men, int shn)
d9bce9d9 1827{
51789c41 1828 uint32_t sh, me;
d9bce9d9 1829
9d53c753
JM
1830 sh = SH(ctx->opcode) | (shn << 5);
1831 me = MB(ctx->opcode) | (men << 5);
51789c41 1832 gen_rldinm(ctx, 0, me, sh);
d9bce9d9 1833}
51789c41 1834GEN_PPC64_R4(rldicr, 0x1E, 0x02);
d9bce9d9 1835/* rldic - rldic. */
b068d6a7 1836static always_inline void gen_rldic (DisasContext *ctx, int mbn, int shn)
d9bce9d9 1837{
51789c41 1838 uint32_t sh, mb;
d9bce9d9 1839
9d53c753
JM
1840 sh = SH(ctx->opcode) | (shn << 5);
1841 mb = MB(ctx->opcode) | (mbn << 5);
51789c41
JM
1842 gen_rldinm(ctx, mb, 63 - sh, sh);
1843}
1844GEN_PPC64_R4(rldic, 0x1E, 0x04);
1845
b068d6a7
JM
1846static always_inline void gen_rldnm (DisasContext *ctx, uint32_t mb,
1847 uint32_t me)
51789c41 1848{
54843a58 1849 TCGv t0;
d03ef511
AJ
1850
1851 mb = MB(ctx->opcode);
1852 me = ME(ctx->opcode);
a7812ae4 1853 t0 = tcg_temp_new();
d03ef511 1854 tcg_gen_andi_tl(t0, cpu_gpr[rB(ctx->opcode)], 0x3f);
54843a58 1855 tcg_gen_rotl_tl(t0, cpu_gpr[rS(ctx->opcode)], t0);
51789c41 1856 if (unlikely(mb != 0 || me != 63)) {
54843a58
AJ
1857 tcg_gen_andi_tl(cpu_gpr[rA(ctx->opcode)], t0, MASK(mb, me));
1858 } else {
1859 tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], t0);
1860 }
1861 tcg_temp_free(t0);
51789c41 1862 if (unlikely(Rc(ctx->opcode) != 0))
d03ef511 1863 gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
d9bce9d9 1864}
51789c41 1865
d9bce9d9 1866/* rldcl - rldcl. */
b068d6a7 1867static always_inline void gen_rldcl (DisasContext *ctx, int mbn)
d9bce9d9 1868{
51789c41 1869 uint32_t mb;
d9bce9d9 1870
9d53c753 1871 mb = MB(ctx->opcode) | (mbn << 5);
51789c41 1872 gen_rldnm(ctx, mb, 63);
d9bce9d9 1873}
36081602 1874GEN_PPC64_R2(rldcl, 0x1E, 0x08);
d9bce9d9 1875/* rldcr - rldcr. */
b068d6a7 1876static always_inline void gen_rldcr (DisasContext *ctx, int men)
d9bce9d9 1877{
51789c41 1878 uint32_t me;
d9bce9d9 1879
9d53c753 1880 me = MB(ctx->opcode) | (men << 5);
51789c41 1881 gen_rldnm(ctx, 0, me);
d9bce9d9 1882}
36081602 1883GEN_PPC64_R2(rldcr, 0x1E, 0x09);
d9bce9d9 1884/* rldimi - rldimi. */
b068d6a7 1885static always_inline void gen_rldimi (DisasContext *ctx, int mbn, int shn)
d9bce9d9 1886{
271a916e 1887 uint32_t sh, mb, me;
d9bce9d9 1888
9d53c753
JM
1889 sh = SH(ctx->opcode) | (shn << 5);
1890 mb = MB(ctx->opcode) | (mbn << 5);
271a916e 1891 me = 63 - sh;
d03ef511
AJ
1892 if (unlikely(sh == 0 && mb == 0)) {
1893 tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)]);
1894 } else {
1895 TCGv t0, t1;
1896 target_ulong mask;
1897
a7812ae4 1898 t0 = tcg_temp_new();
54843a58 1899 tcg_gen_rotli_tl(t0, cpu_gpr[rS(ctx->opcode)], sh);
a7812ae4 1900 t1 = tcg_temp_new();
d03ef511
AJ
1901 mask = MASK(mb, me);
1902 tcg_gen_andi_tl(t0, t0, mask);
1903 tcg_gen_andi_tl(t1, cpu_gpr[rA(ctx->opcode)], ~mask);
1904 tcg_gen_or_tl(cpu_gpr[rA(ctx->opcode)], t0, t1);
1905 tcg_temp_free(t0);
1906 tcg_temp_free(t1);
51789c41 1907 }
51789c41 1908 if (unlikely(Rc(ctx->opcode) != 0))
d03ef511 1909 gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
d9bce9d9 1910}
36081602 1911GEN_PPC64_R4(rldimi, 0x1E, 0x06);
d9bce9d9
JM
1912#endif
1913
79aceca5
FB
1914/*** Integer shift ***/
1915/* slw & slw. */
26d67362
AJ
1916GEN_HANDLER(slw, 0x1F, 0x18, 0x00, 0x00000000, PPC_INTEGER)
1917{
fea0c503 1918 TCGv t0;
26d67362
AJ
1919 int l1, l2;
1920 l1 = gen_new_label();
1921 l2 = gen_new_label();
1922
a7812ae4 1923 t0 = tcg_temp_local_new();
0cfe58cd
AJ
1924 tcg_gen_andi_tl(t0, cpu_gpr[rB(ctx->opcode)], 0x3f);
1925 tcg_gen_brcondi_tl(TCG_COND_LT, t0, 0x20, l1);
26d67362
AJ
1926 tcg_gen_movi_tl(cpu_gpr[rA(ctx->opcode)], 0);
1927 tcg_gen_br(l2);
1928 gen_set_label(l1);
fea0c503 1929 tcg_gen_shl_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)], t0);
26d67362
AJ
1930 tcg_gen_ext32u_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rA(ctx->opcode)]);
1931 gen_set_label(l2);
fea0c503 1932 tcg_temp_free(t0);
26d67362
AJ
1933 if (unlikely(Rc(ctx->opcode) != 0))
1934 gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
1935}
79aceca5 1936/* sraw & sraw. */
26d67362
AJ
1937GEN_HANDLER(sraw, 0x1F, 0x18, 0x18, 0x00000000, PPC_INTEGER)
1938{
a7812ae4
PB
1939 gen_helper_sraw(cpu_gpr[rA(ctx->opcode)],
1940 cpu_gpr[rS(ctx->opcode)], cpu_gpr[rB(ctx->opcode)]);
26d67362
AJ
1941 if (unlikely(Rc(ctx->opcode) != 0))
1942 gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
1943}
79aceca5
FB
1944/* srawi & srawi. */
1945GEN_HANDLER(srawi, 0x1F, 0x18, 0x19, 0x00000000, PPC_INTEGER)
1946{
26d67362
AJ
1947 int sh = SH(ctx->opcode);
1948 if (sh != 0) {
1949 int l1, l2;
fea0c503 1950 TCGv t0;
26d67362
AJ
1951 l1 = gen_new_label();
1952 l2 = gen_new_label();
a7812ae4 1953 t0 = tcg_temp_local_new();
fea0c503
AJ
1954 tcg_gen_ext32s_tl(t0, cpu_gpr[rS(ctx->opcode)]);
1955 tcg_gen_brcondi_tl(TCG_COND_GE, t0, 0, l1);
1956 tcg_gen_andi_tl(t0, cpu_gpr[rS(ctx->opcode)], (1ULL << sh) - 1);
1957 tcg_gen_brcondi_tl(TCG_COND_EQ, t0, 0, l1);
269f3e95 1958 tcg_gen_ori_tl(cpu_xer, cpu_xer, 1 << XER_CA);
26d67362
AJ
1959 tcg_gen_br(l2);
1960 gen_set_label(l1);
269f3e95 1961 tcg_gen_andi_tl(cpu_xer, cpu_xer, ~(1 << XER_CA));
26d67362 1962 gen_set_label(l2);
fea0c503
AJ
1963 tcg_gen_ext32s_tl(t0, cpu_gpr[rS(ctx->opcode)]);
1964 tcg_gen_sari_tl(cpu_gpr[rA(ctx->opcode)], t0, sh);
1965 tcg_temp_free(t0);
26d67362
AJ
1966 } else {
1967 tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)]);
269f3e95 1968 tcg_gen_andi_tl(cpu_xer, cpu_xer, ~(1 << XER_CA));
d9bce9d9 1969 }
76a66253 1970 if (unlikely(Rc(ctx->opcode) != 0))
26d67362 1971 gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
79aceca5
FB
1972}
1973/* srw & srw. */
26d67362
AJ
1974GEN_HANDLER(srw, 0x1F, 0x18, 0x10, 0x00000000, PPC_INTEGER)
1975{
fea0c503 1976 TCGv t0, t1;
26d67362
AJ
1977 int l1, l2;
1978 l1 = gen_new_label();
1979 l2 = gen_new_label();
d9bce9d9 1980
a7812ae4 1981 t0 = tcg_temp_local_new();
0cfe58cd
AJ
1982 tcg_gen_andi_tl(t0, cpu_gpr[rB(ctx->opcode)], 0x3f);
1983 tcg_gen_brcondi_tl(TCG_COND_LT, t0, 0x20, l1);
26d67362
AJ
1984 tcg_gen_movi_tl(cpu_gpr[rA(ctx->opcode)], 0);
1985 tcg_gen_br(l2);
1986 gen_set_label(l1);
a7812ae4 1987 t1 = tcg_temp_new();
fea0c503
AJ
1988 tcg_gen_ext32u_tl(t1, cpu_gpr[rS(ctx->opcode)]);
1989 tcg_gen_shr_tl(cpu_gpr[rA(ctx->opcode)], t1, t0);
1990 tcg_temp_free(t1);
26d67362 1991 gen_set_label(l2);
fea0c503 1992 tcg_temp_free(t0);
26d67362
AJ
1993 if (unlikely(Rc(ctx->opcode) != 0))
1994 gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
1995}
d9bce9d9
JM
1996#if defined(TARGET_PPC64)
1997/* sld & sld. */
26d67362
AJ
1998GEN_HANDLER(sld, 0x1F, 0x1B, 0x00, 0x00000000, PPC_64B)
1999{
fea0c503 2000 TCGv t0;
26d67362
AJ
2001 int l1, l2;
2002 l1 = gen_new_label();
2003 l2 = gen_new_label();
2004
a7812ae4 2005 t0 = tcg_temp_local_new();
0cfe58cd
AJ
2006 tcg_gen_andi_tl(t0, cpu_gpr[rB(ctx->opcode)], 0x7f);
2007 tcg_gen_brcondi_tl(TCG_COND_LT, t0, 0x40, l1);
26d67362
AJ
2008 tcg_gen_movi_tl(cpu_gpr[rA(ctx->opcode)], 0);
2009 tcg_gen_br(l2);
2010 gen_set_label(l1);
fea0c503 2011 tcg_gen_shl_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)], t0);
26d67362 2012 gen_set_label(l2);
fea0c503 2013 tcg_temp_free(t0);
26d67362
AJ
2014 if (unlikely(Rc(ctx->opcode) != 0))
2015 gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
2016}
d9bce9d9 2017/* srad & srad. */
26d67362
AJ
2018GEN_HANDLER(srad, 0x1F, 0x1A, 0x18, 0x00000000, PPC_64B)
2019{
a7812ae4
PB
2020 gen_helper_srad(cpu_gpr[rA(ctx->opcode)],
2021 cpu_gpr[rS(ctx->opcode)], cpu_gpr[rB(ctx->opcode)]);
26d67362
AJ
2022 if (unlikely(Rc(ctx->opcode) != 0))
2023 gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
2024}
d9bce9d9 2025/* sradi & sradi. */
b068d6a7 2026static always_inline void gen_sradi (DisasContext *ctx, int n)
d9bce9d9 2027{
26d67362 2028 int sh = SH(ctx->opcode) + (n << 5);
d9bce9d9 2029 if (sh != 0) {
26d67362 2030 int l1, l2;
fea0c503 2031 TCGv t0;
26d67362
AJ
2032 l1 = gen_new_label();
2033 l2 = gen_new_label();
a7812ae4 2034 t0 = tcg_temp_local_new();
26d67362 2035 tcg_gen_brcondi_tl(TCG_COND_GE, cpu_gpr[rS(ctx->opcode)], 0, l1);
fea0c503
AJ
2036 tcg_gen_andi_tl(t0, cpu_gpr[rS(ctx->opcode)], (1ULL << sh) - 1);
2037 tcg_gen_brcondi_tl(TCG_COND_EQ, t0, 0, l1);
269f3e95 2038 tcg_gen_ori_tl(cpu_xer, cpu_xer, 1 << XER_CA);
26d67362
AJ
2039 tcg_gen_br(l2);
2040 gen_set_label(l1);
269f3e95 2041 tcg_gen_andi_tl(cpu_xer, cpu_xer, ~(1 << XER_CA));
26d67362 2042 gen_set_label(l2);
a9730017 2043 tcg_temp_free(t0);
26d67362
AJ
2044 tcg_gen_sari_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)], sh);
2045 } else {
2046 tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)]);
269f3e95 2047 tcg_gen_andi_tl(cpu_xer, cpu_xer, ~(1 << XER_CA));
d9bce9d9 2048 }
d9bce9d9 2049 if (unlikely(Rc(ctx->opcode) != 0))
26d67362 2050 gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
d9bce9d9 2051}
c7697e1f 2052GEN_HANDLER2(sradi0, "sradi", 0x1F, 0x1A, 0x19, 0x00000000, PPC_64B)
d9bce9d9
JM
2053{
2054 gen_sradi(ctx, 0);
2055}
c7697e1f 2056GEN_HANDLER2(sradi1, "sradi", 0x1F, 0x1B, 0x19, 0x00000000, PPC_64B)
d9bce9d9
JM
2057{
2058 gen_sradi(ctx, 1);
2059}
2060/* srd & srd. */
26d67362
AJ
2061GEN_HANDLER(srd, 0x1F, 0x1B, 0x10, 0x00000000, PPC_64B)
2062{
fea0c503 2063 TCGv t0;
26d67362
AJ
2064 int l1, l2;
2065 l1 = gen_new_label();
2066 l2 = gen_new_label();
2067
a7812ae4 2068 t0 = tcg_temp_local_new();
0cfe58cd
AJ
2069 tcg_gen_andi_tl(t0, cpu_gpr[rB(ctx->opcode)], 0x7f);
2070 tcg_gen_brcondi_tl(TCG_COND_LT, t0, 0x40, l1);
26d67362
AJ
2071 tcg_gen_movi_tl(cpu_gpr[rA(ctx->opcode)], 0);
2072 tcg_gen_br(l2);
2073 gen_set_label(l1);
fea0c503 2074 tcg_gen_shr_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)], t0);
26d67362 2075 gen_set_label(l2);
fea0c503 2076 tcg_temp_free(t0);
26d67362
AJ
2077 if (unlikely(Rc(ctx->opcode) != 0))
2078 gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
2079}
d9bce9d9 2080#endif
79aceca5
FB
2081
2082/*** Floating-Point arithmetic ***/
7c58044c 2083#define _GEN_FLOAT_ACB(name, op, op1, op2, isfloat, set_fprf, type) \
a750fc0b 2084GEN_HANDLER(f##name, op1, op2, 0xFF, 0x00000000, type) \
9a64fbe4 2085{ \
76a66253 2086 if (unlikely(!ctx->fpu_enabled)) { \
e1833e1f 2087 GEN_EXCP_NO_FP(ctx); \
3cc62370
FB
2088 return; \
2089 } \
7c58044c 2090 gen_reset_fpstatus(); \
af12906f
AJ
2091 gen_helper_f##op(cpu_fpr[rD(ctx->opcode)], cpu_fpr[rA(ctx->opcode)], \
2092 cpu_fpr[rC(ctx->opcode)], cpu_fpr[rB(ctx->opcode)]); \
4ecc3190 2093 if (isfloat) { \
af12906f 2094 gen_helper_frsp(cpu_fpr[rD(ctx->opcode)], cpu_fpr[rD(ctx->opcode)]); \
4ecc3190 2095 } \
af12906f
AJ
2096 gen_compute_fprf(cpu_fpr[rD(ctx->opcode)], set_fprf, \
2097 Rc(ctx->opcode) != 0); \
9a64fbe4
FB
2098}
2099
7c58044c
JM
2100#define GEN_FLOAT_ACB(name, op2, set_fprf, type) \
2101_GEN_FLOAT_ACB(name, name, 0x3F, op2, 0, set_fprf, type); \
2102_GEN_FLOAT_ACB(name##s, name, 0x3B, op2, 1, set_fprf, type);
9a64fbe4 2103
7c58044c
JM
2104#define _GEN_FLOAT_AB(name, op, op1, op2, inval, isfloat, set_fprf, type) \
2105GEN_HANDLER(f##name, op1, op2, 0xFF, inval, type) \
9a64fbe4 2106{ \
76a66253 2107 if (unlikely(!ctx->fpu_enabled)) { \
e1833e1f 2108 GEN_EXCP_NO_FP(ctx); \
3cc62370
FB
2109 return; \
2110 } \
7c58044c 2111 gen_reset_fpstatus(); \
af12906f
AJ
2112 gen_helper_f##op(cpu_fpr[rD(ctx->opcode)], cpu_fpr[rA(ctx->opcode)], \
2113 cpu_fpr[rB(ctx->opcode)]); \
4ecc3190 2114 if (isfloat) { \
af12906f 2115 gen_helper_frsp(cpu_fpr[rD(ctx->opcode)], cpu_fpr[rD(ctx->opcode)]); \
4ecc3190 2116 } \
af12906f
AJ
2117 gen_compute_fprf(cpu_fpr[rD(ctx->opcode)], \
2118 set_fprf, Rc(ctx->opcode) != 0); \
9a64fbe4 2119}
7c58044c
JM
2120#define GEN_FLOAT_AB(name, op2, inval, set_fprf, type) \
2121_GEN_FLOAT_AB(name, name, 0x3F, op2, inval, 0, set_fprf, type); \
2122_GEN_FLOAT_AB(name##s, name, 0x3B, op2, inval, 1, set_fprf, type);
9a64fbe4 2123
7c58044c
JM
2124#define _GEN_FLOAT_AC(name, op, op1, op2, inval, isfloat, set_fprf, type) \
2125GEN_HANDLER(f##name, op1, op2, 0xFF, inval, type) \
9a64fbe4 2126{ \
76a66253 2127 if (unlikely(!ctx->fpu_enabled)) { \
e1833e1f 2128 GEN_EXCP_NO_FP(ctx); \
3cc62370
FB
2129 return; \
2130 } \
7c58044c 2131 gen_reset_fpstatus(); \
af12906f
AJ
2132 gen_helper_f##op(cpu_fpr[rD(ctx->opcode)], cpu_fpr[rA(ctx->opcode)], \
2133 cpu_fpr[rC(ctx->opcode)]); \
4ecc3190 2134 if (isfloat) { \
af12906f 2135 gen_helper_frsp(cpu_fpr[rD(ctx->opcode)], cpu_fpr[rD(ctx->opcode)]); \
4ecc3190 2136 } \
af12906f
AJ
2137 gen_compute_fprf(cpu_fpr[rD(ctx->opcode)], \
2138 set_fprf, Rc(ctx->opcode) != 0); \
9a64fbe4 2139}
7c58044c
JM
2140#define GEN_FLOAT_AC(name, op2, inval, set_fprf, type) \
2141_GEN_FLOAT_AC(name, name, 0x3F, op2, inval, 0, set_fprf, type); \
2142_GEN_FLOAT_AC(name##s, name, 0x3B, op2, inval, 1, set_fprf, type);
9a64fbe4 2143
7c58044c 2144#define GEN_FLOAT_B(name, op2, op3, set_fprf, type) \
a750fc0b 2145GEN_HANDLER(f##name, 0x3F, op2, op3, 0x001F0000, type) \
9a64fbe4 2146{ \
76a66253 2147 if (unlikely(!ctx->fpu_enabled)) { \
e1833e1f 2148 GEN_EXCP_NO_FP(ctx); \
3cc62370
FB
2149 return; \
2150 } \
7c58044c 2151 gen_reset_fpstatus(); \
af12906f
AJ
2152 gen_helper_f##name(cpu_fpr[rD(ctx->opcode)], cpu_fpr[rB(ctx->opcode)]); \
2153 gen_compute_fprf(cpu_fpr[rD(ctx->opcode)], \
2154 set_fprf, Rc(ctx->opcode) != 0); \
79aceca5
FB
2155}
2156
7c58044c 2157#define GEN_FLOAT_BS(name, op1, op2, set_fprf, type) \
a750fc0b 2158GEN_HANDLER(f##name, op1, op2, 0xFF, 0x001F07C0, type) \
9a64fbe4 2159{ \
76a66253 2160 if (unlikely(!ctx->fpu_enabled)) { \
e1833e1f 2161 GEN_EXCP_NO_FP(ctx); \
3cc62370
FB
2162 return; \
2163 } \
7c58044c 2164 gen_reset_fpstatus(); \
af12906f
AJ
2165 gen_helper_f##name(cpu_fpr[rD(ctx->opcode)], cpu_fpr[rB(ctx->opcode)]); \
2166 gen_compute_fprf(cpu_fpr[rD(ctx->opcode)], \
2167 set_fprf, Rc(ctx->opcode) != 0); \
79aceca5
FB
2168}
2169
9a64fbe4 2170/* fadd - fadds */
7c58044c 2171GEN_FLOAT_AB(add, 0x15, 0x000007C0, 1, PPC_FLOAT);
4ecc3190 2172/* fdiv - fdivs */
7c58044c 2173GEN_FLOAT_AB(div, 0x12, 0x000007C0, 1, PPC_FLOAT);
4ecc3190 2174/* fmul - fmuls */
7c58044c 2175GEN_FLOAT_AC(mul, 0x19, 0x0000F800, 1, PPC_FLOAT);
79aceca5 2176
d7e4b87e 2177/* fre */
7c58044c 2178GEN_FLOAT_BS(re, 0x3F, 0x18, 1, PPC_FLOAT_EXT);
d7e4b87e 2179
a750fc0b 2180/* fres */
7c58044c 2181GEN_FLOAT_BS(res, 0x3B, 0x18, 1, PPC_FLOAT_FRES);
79aceca5 2182
a750fc0b 2183/* frsqrte */
7c58044c
JM
2184GEN_FLOAT_BS(rsqrte, 0x3F, 0x1A, 1, PPC_FLOAT_FRSQRTE);
2185
2186/* frsqrtes */
af12906f 2187GEN_HANDLER(frsqrtes, 0x3B, 0x1A, 0xFF, 0x001F07C0, PPC_FLOAT_FRSQRTES)
7c58044c 2188{
af12906f
AJ
2189 if (unlikely(!ctx->fpu_enabled)) {
2190 GEN_EXCP_NO_FP(ctx);
2191 return;
2192 }
2193 gen_reset_fpstatus();
2194 gen_helper_frsqrte(cpu_fpr[rD(ctx->opcode)], cpu_fpr[rB(ctx->opcode)]);
2195 gen_helper_frsp(cpu_fpr[rD(ctx->opcode)], cpu_fpr[rD(ctx->opcode)]);
2196 gen_compute_fprf(cpu_fpr[rD(ctx->opcode)], 1, Rc(ctx->opcode) != 0);
7c58044c 2197}
79aceca5 2198
a750fc0b 2199/* fsel */
7c58044c 2200_GEN_FLOAT_ACB(sel, sel, 0x3F, 0x17, 0, 0, PPC_FLOAT_FSEL);
4ecc3190 2201/* fsub - fsubs */
7c58044c 2202GEN_FLOAT_AB(sub, 0x14, 0x000007C0, 1, PPC_FLOAT);
79aceca5
FB
2203/* Optional: */
2204/* fsqrt */
a750fc0b 2205GEN_HANDLER(fsqrt, 0x3F, 0x16, 0xFF, 0x001F07C0, PPC_FLOAT_FSQRT)
c7d344af 2206{
76a66253 2207 if (unlikely(!ctx->fpu_enabled)) {
e1833e1f 2208 GEN_EXCP_NO_FP(ctx);
c7d344af
FB
2209 return;
2210 }
7c58044c 2211 gen_reset_fpstatus();
af12906f
AJ
2212 gen_helper_fsqrt(cpu_fpr[rD(ctx->opcode)], cpu_fpr[rB(ctx->opcode)]);
2213 gen_compute_fprf(cpu_fpr[rD(ctx->opcode)], 1, Rc(ctx->opcode) != 0);
c7d344af 2214}
79aceca5 2215
a750fc0b 2216GEN_HANDLER(fsqrts, 0x3B, 0x16, 0xFF, 0x001F07C0, PPC_FLOAT_FSQRT)
79aceca5 2217{
76a66253 2218 if (unlikely(!ctx->fpu_enabled)) {
e1833e1f 2219 GEN_EXCP_NO_FP(ctx);
3cc62370
FB
2220 return;
2221 }
7c58044c 2222 gen_reset_fpstatus();
af12906f
AJ
2223 gen_helper_fsqrt(cpu_fpr[rD(ctx->opcode)], cpu_fpr[rB(ctx->opcode)]);
2224 gen_helper_frsp(cpu_fpr[rD(ctx->opcode)], cpu_fpr[rD(ctx->opcode)]);
2225 gen_compute_fprf(cpu_fpr[rD(ctx->opcode)], 1, Rc(ctx->opcode) != 0);
79aceca5
FB
2226}
2227
2228/*** Floating-Point multiply-and-add ***/
4ecc3190 2229/* fmadd - fmadds */
7c58044c 2230GEN_FLOAT_ACB(madd, 0x1D, 1, PPC_FLOAT);
4ecc3190 2231/* fmsub - fmsubs */
7c58044c 2232GEN_FLOAT_ACB(msub, 0x1C, 1, PPC_FLOAT);
4ecc3190 2233/* fnmadd - fnmadds */
7c58044c 2234GEN_FLOAT_ACB(nmadd, 0x1F, 1, PPC_FLOAT);
4ecc3190 2235/* fnmsub - fnmsubs */
7c58044c 2236GEN_FLOAT_ACB(nmsub, 0x1E, 1, PPC_FLOAT);
79aceca5
FB
2237
2238/*** Floating-Point round & convert ***/
2239/* fctiw */
7c58044c 2240GEN_FLOAT_B(ctiw, 0x0E, 0x00, 0, PPC_FLOAT);
79aceca5 2241/* fctiwz */
7c58044c 2242GEN_FLOAT_B(ctiwz, 0x0F, 0x00, 0, PPC_FLOAT);
79aceca5 2243/* frsp */
7c58044c 2244GEN_FLOAT_B(rsp, 0x0C, 0x00, 1, PPC_FLOAT);
426613db
JM
2245#if defined(TARGET_PPC64)
2246/* fcfid */
7c58044c 2247GEN_FLOAT_B(cfid, 0x0E, 0x1A, 1, PPC_64B);
426613db 2248/* fctid */
7c58044c 2249GEN_FLOAT_B(ctid, 0x0E, 0x19, 0, PPC_64B);
426613db 2250/* fctidz */
7c58044c 2251GEN_FLOAT_B(ctidz, 0x0F, 0x19, 0, PPC_64B);
426613db 2252#endif
79aceca5 2253
d7e4b87e 2254/* frin */
7c58044c 2255GEN_FLOAT_B(rin, 0x08, 0x0C, 1, PPC_FLOAT_EXT);
d7e4b87e 2256/* friz */
7c58044c 2257GEN_FLOAT_B(riz, 0x08, 0x0D, 1, PPC_FLOAT_EXT);
d7e4b87e 2258/* frip */
7c58044c 2259GEN_FLOAT_B(rip, 0x08, 0x0E, 1, PPC_FLOAT_EXT);
d7e4b87e 2260/* frim */
7c58044c 2261GEN_FLOAT_B(rim, 0x08, 0x0F, 1, PPC_FLOAT_EXT);
d7e4b87e 2262
79aceca5
FB
2263/*** Floating-Point compare ***/
2264/* fcmpo */
76a66253 2265GEN_HANDLER(fcmpo, 0x3F, 0x00, 0x01, 0x00600001, PPC_FLOAT)
79aceca5 2266{
76a66253 2267 if (unlikely(!ctx->fpu_enabled)) {
e1833e1f 2268 GEN_EXCP_NO_FP(ctx);
3cc62370
FB
2269 return;
2270 }
7c58044c 2271 gen_reset_fpstatus();
af12906f
AJ
2272 gen_helper_fcmpo(cpu_crf[crfD(ctx->opcode)],
2273 cpu_fpr[rA(ctx->opcode)], cpu_fpr[rB(ctx->opcode)]);
2274 gen_helper_float_check_status();
79aceca5
FB
2275}
2276
2277/* fcmpu */
76a66253 2278GEN_HANDLER(fcmpu, 0x3F, 0x00, 0x00, 0x00600001, PPC_FLOAT)
79aceca5 2279{
76a66253 2280 if (unlikely(!ctx->fpu_enabled)) {
e1833e1f 2281 GEN_EXCP_NO_FP(ctx);
3cc62370
FB
2282 return;
2283 }
7c58044c 2284 gen_reset_fpstatus();
af12906f
AJ
2285 gen_helper_fcmpu(cpu_crf[crfD(ctx->opcode)],
2286 cpu_fpr[rA(ctx->opcode)], cpu_fpr[rB(ctx->opcode)]);
2287 gen_helper_float_check_status();
79aceca5
FB
2288}
2289
9a64fbe4
FB
2290/*** Floating-point move ***/
2291/* fabs */
7c58044c
JM
2292/* XXX: beware that fabs never checks for NaNs nor update FPSCR */
2293GEN_FLOAT_B(abs, 0x08, 0x08, 0, PPC_FLOAT);
9a64fbe4
FB
2294
2295/* fmr - fmr. */
7c58044c 2296/* XXX: beware that fmr never checks for NaNs nor update FPSCR */
9a64fbe4
FB
2297GEN_HANDLER(fmr, 0x3F, 0x08, 0x02, 0x001F0000, PPC_FLOAT)
2298{
76a66253 2299 if (unlikely(!ctx->fpu_enabled)) {
e1833e1f 2300 GEN_EXCP_NO_FP(ctx);
3cc62370
FB
2301 return;
2302 }
af12906f
AJ
2303 tcg_gen_mov_i64(cpu_fpr[rD(ctx->opcode)], cpu_fpr[rB(ctx->opcode)]);
2304 gen_compute_fprf(cpu_fpr[rD(ctx->opcode)], 0, Rc(ctx->opcode) != 0);
9a64fbe4
FB
2305}
2306
2307/* fnabs */
7c58044c
JM
2308/* XXX: beware that fnabs never checks for NaNs nor update FPSCR */
2309GEN_FLOAT_B(nabs, 0x08, 0x04, 0, PPC_FLOAT);
9a64fbe4 2310/* fneg */
7c58044c
JM
2311/* XXX: beware that fneg never checks for NaNs nor update FPSCR */
2312GEN_FLOAT_B(neg, 0x08, 0x01, 0, PPC_FLOAT);
9a64fbe4 2313
79aceca5
FB
2314/*** Floating-Point status & ctrl register ***/
2315/* mcrfs */
2316GEN_HANDLER(mcrfs, 0x3F, 0x00, 0x02, 0x0063F801, PPC_FLOAT)
2317{
7c58044c
JM
2318 int bfa;
2319
76a66253 2320 if (unlikely(!ctx->fpu_enabled)) {
e1833e1f 2321 GEN_EXCP_NO_FP(ctx);
3cc62370
FB
2322 return;
2323 }
7c58044c
JM
2324 gen_optimize_fprf();
2325 bfa = 4 * (7 - crfS(ctx->opcode));
e1571908
AJ
2326 tcg_gen_shri_i32(cpu_crf[crfD(ctx->opcode)], cpu_fpscr, bfa);
2327 tcg_gen_andi_i32(cpu_crf[crfD(ctx->opcode)], cpu_crf[crfD(ctx->opcode)], 0xf);
af12906f 2328 tcg_gen_andi_i32(cpu_fpscr, cpu_fpscr, ~(0xF << bfa));
79aceca5
FB
2329}
2330
2331/* mffs */
2332GEN_HANDLER(mffs, 0x3F, 0x07, 0x12, 0x001FF800, PPC_FLOAT)
2333{
76a66253 2334 if (unlikely(!ctx->fpu_enabled)) {
e1833e1f 2335 GEN_EXCP_NO_FP(ctx);
3cc62370
FB
2336 return;
2337 }
7c58044c
JM
2338 gen_optimize_fprf();
2339 gen_reset_fpstatus();
af12906f
AJ
2340 tcg_gen_extu_i32_i64(cpu_fpr[rD(ctx->opcode)], cpu_fpscr);
2341 gen_compute_fprf(cpu_fpr[rD(ctx->opcode)], 0, Rc(ctx->opcode) != 0);
79aceca5
FB
2342}
2343
2344/* mtfsb0 */
2345GEN_HANDLER(mtfsb0, 0x3F, 0x06, 0x02, 0x001FF800, PPC_FLOAT)
2346{
fb0eaffc 2347 uint8_t crb;
3b46e624 2348
76a66253 2349 if (unlikely(!ctx->fpu_enabled)) {
e1833e1f 2350 GEN_EXCP_NO_FP(ctx);
3cc62370
FB
2351 return;
2352 }
7c58044c
JM
2353 crb = 32 - (crbD(ctx->opcode) >> 2);
2354 gen_optimize_fprf();
2355 gen_reset_fpstatus();
2356 if (likely(crb != 30 && crb != 29))
af12906f 2357 tcg_gen_andi_i32(cpu_fpscr, cpu_fpscr, ~(1 << crb));
7c58044c 2358 if (unlikely(Rc(ctx->opcode) != 0)) {
e1571908 2359 tcg_gen_shri_i32(cpu_crf[1], cpu_fpscr, FPSCR_OX);
7c58044c 2360 }
79aceca5
FB
2361}
2362
2363/* mtfsb1 */
2364GEN_HANDLER(mtfsb1, 0x3F, 0x06, 0x01, 0x001FF800, PPC_FLOAT)
2365{
fb0eaffc 2366 uint8_t crb;
3b46e624 2367
76a66253 2368 if (unlikely(!ctx->fpu_enabled)) {
e1833e1f 2369 GEN_EXCP_NO_FP(ctx);
3cc62370
FB
2370 return;
2371 }
7c58044c
JM
2372 crb = 32 - (crbD(ctx->opcode) >> 2);
2373 gen_optimize_fprf();
2374 gen_reset_fpstatus();
2375 /* XXX: we pretend we can only do IEEE floating-point computations */
af12906f 2376 if (likely(crb != FPSCR_FEX && crb != FPSCR_VX && crb != FPSCR_NI)) {
0f2f39c2 2377 TCGv_i32 t0 = tcg_const_i32(crb);
af12906f 2378 gen_helper_fpscr_setbit(t0);
0f2f39c2 2379 tcg_temp_free_i32(t0);
af12906f 2380 }
7c58044c 2381 if (unlikely(Rc(ctx->opcode) != 0)) {
e1571908 2382 tcg_gen_shri_i32(cpu_crf[1], cpu_fpscr, FPSCR_OX);
7c58044c
JM
2383 }
2384 /* We can raise a differed exception */
af12906f 2385 gen_helper_float_check_status();
79aceca5
FB
2386}
2387
2388/* mtfsf */
2389GEN_HANDLER(mtfsf, 0x3F, 0x07, 0x16, 0x02010000, PPC_FLOAT)
2390{
0f2f39c2 2391 TCGv_i32 t0;
af12906f 2392
76a66253 2393 if (unlikely(!ctx->fpu_enabled)) {
e1833e1f 2394 GEN_EXCP_NO_FP(ctx);
3cc62370
FB
2395 return;
2396 }
7c58044c 2397 gen_optimize_fprf();
7c58044c 2398 gen_reset_fpstatus();
af12906f
AJ
2399 t0 = tcg_const_i32(FM(ctx->opcode));
2400 gen_helper_store_fpscr(cpu_fpr[rB(ctx->opcode)], t0);
0f2f39c2 2401 tcg_temp_free_i32(t0);
7c58044c 2402 if (unlikely(Rc(ctx->opcode) != 0)) {
e1571908 2403 tcg_gen_shri_i32(cpu_crf[1], cpu_fpscr, FPSCR_OX);
7c58044c
JM
2404 }
2405 /* We can raise a differed exception */
af12906f 2406 gen_helper_float_check_status();
79aceca5
FB
2407}
2408
2409/* mtfsfi */
2410GEN_HANDLER(mtfsfi, 0x3F, 0x06, 0x04, 0x006f0800, PPC_FLOAT)
2411{
7c58044c 2412 int bf, sh;
0f2f39c2
AJ
2413 TCGv_i64 t0;
2414 TCGv_i32 t1;
7c58044c 2415
76a66253 2416 if (unlikely(!ctx->fpu_enabled)) {
e1833e1f 2417 GEN_EXCP_NO_FP(ctx);
3cc62370
FB
2418 return;
2419 }
7c58044c
JM
2420 bf = crbD(ctx->opcode) >> 2;
2421 sh = 7 - bf;
2422 gen_optimize_fprf();
7c58044c 2423 gen_reset_fpstatus();
0f2f39c2 2424 t0 = tcg_const_i64(FPIMM(ctx->opcode) << (4 * sh));
af12906f
AJ
2425 t1 = tcg_const_i32(1 << sh);
2426 gen_helper_store_fpscr(t0, t1);
0f2f39c2
AJ
2427 tcg_temp_free_i64(t0);
2428 tcg_temp_free_i32(t1);
7c58044c 2429 if (unlikely(Rc(ctx->opcode) != 0)) {
e1571908 2430 tcg_gen_shri_i32(cpu_crf[1], cpu_fpscr, FPSCR_OX);
7c58044c
JM
2431 }
2432 /* We can raise a differed exception */
af12906f 2433 gen_helper_float_check_status();
79aceca5
FB
2434}
2435
76a66253
JM
2436/*** Addressing modes ***/
2437/* Register indirect with immediate index : EA = (rA|0) + SIMM */
e2be8d8d
AJ
2438static always_inline void gen_addr_imm_index (TCGv EA,
2439 DisasContext *ctx,
b068d6a7 2440 target_long maskl)
76a66253
JM
2441{
2442 target_long simm = SIMM(ctx->opcode);
2443
be147d08 2444 simm &= ~maskl;
e2be8d8d
AJ
2445 if (rA(ctx->opcode) == 0)
2446 tcg_gen_movi_tl(EA, simm);
2447 else if (likely(simm != 0))
2448 tcg_gen_addi_tl(EA, cpu_gpr[rA(ctx->opcode)], simm);
2449 else
2450 tcg_gen_mov_tl(EA, cpu_gpr[rA(ctx->opcode)]);
76a66253
JM
2451}
2452
e2be8d8d
AJ
2453static always_inline void gen_addr_reg_index (TCGv EA,
2454 DisasContext *ctx)
76a66253 2455{
e2be8d8d
AJ
2456 if (rA(ctx->opcode) == 0)
2457 tcg_gen_mov_tl(EA, cpu_gpr[rB(ctx->opcode)]);
2458 else
2459 tcg_gen_add_tl(EA, cpu_gpr[rA(ctx->opcode)], cpu_gpr[rB(ctx->opcode)]);
76a66253
JM
2460}
2461
e2be8d8d
AJ
2462static always_inline void gen_addr_register (TCGv EA,
2463 DisasContext *ctx)
76a66253 2464{
e2be8d8d
AJ
2465 if (rA(ctx->opcode) == 0)
2466 tcg_gen_movi_tl(EA, 0);
2467 else
2468 tcg_gen_mov_tl(EA, cpu_gpr[rA(ctx->opcode)]);
76a66253
JM
2469}
2470
7863667f
JM
2471#if defined(TARGET_PPC64)
2472#define _GEN_MEM_FUNCS(name, mode) \
2473 &gen_op_##name##_##mode, \
2474 &gen_op_##name##_le_##mode, \
2475 &gen_op_##name##_64_##mode, \
2476 &gen_op_##name##_le_64_##mode
2477#else
2478#define _GEN_MEM_FUNCS(name, mode) \
2479 &gen_op_##name##_##mode, \
2480 &gen_op_##name##_le_##mode
2481#endif
9a64fbe4 2482#if defined(CONFIG_USER_ONLY)
d9bce9d9 2483#if defined(TARGET_PPC64)
7863667f 2484#define NB_MEM_FUNCS 4
d9bce9d9 2485#else
7863667f 2486#define NB_MEM_FUNCS 2
d9bce9d9 2487#endif
7863667f
JM
2488#define GEN_MEM_FUNCS(name) \
2489 _GEN_MEM_FUNCS(name, raw)
9a64fbe4 2490#else
d9bce9d9 2491#if defined(TARGET_PPC64)
7863667f 2492#define NB_MEM_FUNCS 12
2857068e 2493#else
7863667f 2494#define NB_MEM_FUNCS 6
2857068e 2495#endif
7863667f
JM
2496#define GEN_MEM_FUNCS(name) \
2497 _GEN_MEM_FUNCS(name, user), \
2498 _GEN_MEM_FUNCS(name, kernel), \
2499 _GEN_MEM_FUNCS(name, hypv)
2500#endif
2501
2502/*** Integer load ***/
b61f2753
AJ
2503#if defined(TARGET_PPC64)
2504#define GEN_QEMU_LD_PPC64(width) \
2505static always_inline void gen_qemu_ld##width##_ppc64(TCGv t0, TCGv t1, int flags)\
2506{ \
2507 if (likely(flags & 2)) \
2508 tcg_gen_qemu_ld##width(t0, t1, flags >> 2); \
2509 else { \
a7812ae4 2510 TCGv addr = tcg_temp_new(); \
b61f2753
AJ
2511 tcg_gen_ext32u_tl(addr, t1); \
2512 tcg_gen_qemu_ld##width(t0, addr, flags >> 2); \
2513 tcg_temp_free(addr); \
2514 } \
2515}
2516GEN_QEMU_LD_PPC64(8u)
2517GEN_QEMU_LD_PPC64(8s)
2518GEN_QEMU_LD_PPC64(16u)
2519GEN_QEMU_LD_PPC64(16s)
2520GEN_QEMU_LD_PPC64(32u)
2521GEN_QEMU_LD_PPC64(32s)
2522GEN_QEMU_LD_PPC64(64)
2523
2524#define GEN_QEMU_ST_PPC64(width) \
2525static always_inline void gen_qemu_st##width##_ppc64(TCGv t0, TCGv t1, int flags)\
2526{ \
2527 if (likely(flags & 2)) \
2528 tcg_gen_qemu_st##width(t0, t1, flags >> 2); \
2529 else { \
a7812ae4 2530 TCGv addr = tcg_temp_new(); \
b61f2753
AJ
2531 tcg_gen_ext32u_tl(addr, t1); \
2532 tcg_gen_qemu_st##width(t0, addr, flags >> 2); \
2533 tcg_temp_free(addr); \
2534 } \
2535}
2536GEN_QEMU_ST_PPC64(8)
2537GEN_QEMU_ST_PPC64(16)
2538GEN_QEMU_ST_PPC64(32)
2539GEN_QEMU_ST_PPC64(64)
2540
ea363694 2541static always_inline void gen_qemu_ld8u(TCGv arg0, TCGv arg1, int flags)
b61f2753 2542{
ea363694 2543 gen_qemu_ld8u_ppc64(arg0, arg1, flags);
b61f2753
AJ
2544}
2545
ea363694 2546static always_inline void gen_qemu_ld8s(TCGv arg0, TCGv arg1, int flags)
b61f2753 2547{
ea363694 2548 gen_qemu_ld8s_ppc64(arg0, arg1, flags);
b61f2753
AJ
2549}
2550
ea363694 2551static always_inline void gen_qemu_ld16u(TCGv arg0, TCGv arg1, int flags)
b61f2753
AJ
2552{
2553 if (unlikely(flags & 1)) {
a7812ae4 2554 TCGv_i32 t0;
ea363694 2555 gen_qemu_ld16u_ppc64(arg0, arg1, flags);
a7812ae4 2556 t0 = tcg_temp_new_i32();
ea363694
AJ
2557 tcg_gen_trunc_tl_i32(t0, arg0);
2558 tcg_gen_bswap16_i32(t0, t0);
2559 tcg_gen_extu_i32_tl(arg0, t0);
a7812ae4 2560 tcg_temp_free_i32(t0);
b61f2753 2561 } else
ea363694 2562 gen_qemu_ld16u_ppc64(arg0, arg1, flags);
b61f2753
AJ
2563}
2564
ea363694 2565static always_inline void gen_qemu_ld16s(TCGv arg0, TCGv arg1, int flags)
b61f2753
AJ
2566{
2567 if (unlikely(flags & 1)) {
a7812ae4 2568 TCGv_i32 t0;
ea363694 2569 gen_qemu_ld16u_ppc64(arg0, arg1, flags);
a7812ae4 2570 t0 = tcg_temp_new_i32();
ea363694
AJ
2571 tcg_gen_trunc_tl_i32(t0, arg0);
2572 tcg_gen_bswap16_i32(t0, t0);
2573 tcg_gen_extu_i32_tl(arg0, t0);
2574 tcg_gen_ext16s_tl(arg0, arg0);
a7812ae4 2575 tcg_temp_free_i32(t0);
b61f2753 2576 } else
ea363694 2577 gen_qemu_ld16s_ppc64(arg0, arg1, flags);
b61f2753
AJ
2578}
2579
ea363694 2580static always_inline void gen_qemu_ld32u(TCGv arg0, TCGv arg1, int flags)
b61f2753
AJ
2581{
2582 if (unlikely(flags & 1)) {
a7812ae4 2583 TCGv_i32 t0;
ea363694 2584 gen_qemu_ld32u_ppc64(arg0, arg1, flags);
a7812ae4 2585 t0 = tcg_temp_new_i32();
ea363694
AJ
2586 tcg_gen_trunc_tl_i32(t0, arg0);
2587 tcg_gen_bswap_i32(t0, t0);
2588 tcg_gen_extu_i32_tl(arg0, t0);
a7812ae4 2589 tcg_temp_free_i32(t0);
b61f2753 2590 } else
ea363694 2591 gen_qemu_ld32u_ppc64(arg0, arg1, flags);
b61f2753
AJ
2592}
2593
ea363694 2594static always_inline void gen_qemu_ld32s(TCGv arg0, TCGv arg1, int flags)
b61f2753
AJ
2595{
2596 if (unlikely(flags & 1)) {
a7812ae4 2597 TCGv_i32 t0;
ea363694 2598 gen_qemu_ld32u_ppc64(arg0, arg1, flags);
a7812ae4 2599 t0 = tcg_temp_new_i32();
ea363694
AJ
2600 tcg_gen_trunc_tl_i32(t0, arg0);
2601 tcg_gen_bswap_i32(t0, t0);
2602 tcg_gen_ext_i32_tl(arg0, t0);
a7812ae4 2603 tcg_temp_free_i32(t0);
b61f2753 2604 } else
ea363694 2605 gen_qemu_ld32s_ppc64(arg0, arg1, flags);
b61f2753
AJ
2606}
2607
ea363694 2608static always_inline void gen_qemu_ld64(TCGv arg0, TCGv arg1, int flags)
b61f2753 2609{
ea363694 2610 gen_qemu_ld64_ppc64(arg0, arg1, flags);
b61f2753 2611 if (unlikely(flags & 1))
ea363694 2612 tcg_gen_bswap_i64(arg0, arg0);
b61f2753
AJ
2613}
2614
ea363694 2615static always_inline void gen_qemu_st8(TCGv arg0, TCGv arg1, int flags)
b61f2753 2616{
ea363694 2617 gen_qemu_st8_ppc64(arg0, arg1, flags);
b61f2753
AJ
2618}
2619
ea363694 2620static always_inline void gen_qemu_st16(TCGv arg0, TCGv arg1, int flags)
b61f2753
AJ
2621{
2622 if (unlikely(flags & 1)) {
a7812ae4
PB
2623 TCGv_i32 t0;
2624 TCGv_i64 t1;
2625 t0 = tcg_temp_new_i32();
ea363694
AJ
2626 tcg_gen_trunc_tl_i32(t0, arg0);
2627 tcg_gen_ext16u_i32(t0, t0);
2628 tcg_gen_bswap16_i32(t0, t0);
a7812ae4 2629 t1 = tcg_temp_new_i64();
ea363694 2630 tcg_gen_extu_i32_tl(t1, t0);
a7812ae4 2631 tcg_temp_free_i32(t0);
ea363694 2632 gen_qemu_st16_ppc64(t1, arg1, flags);
a7812ae4 2633 tcg_temp_free_i64(t1);
b61f2753 2634 } else
ea363694 2635 gen_qemu_st16_ppc64(arg0, arg1, flags);
b61f2753
AJ
2636}
2637
ea363694 2638static always_inline void gen_qemu_st32(TCGv arg0, TCGv arg1, int flags)
b61f2753
AJ
2639{
2640 if (unlikely(flags & 1)) {
a7812ae4
PB
2641 TCGv_i32 t0;
2642 TCGv_i64 t1;
2643 t0 = tcg_temp_new_i32();
ea363694
AJ
2644 tcg_gen_trunc_tl_i32(t0, arg0);
2645 tcg_gen_bswap_i32(t0, t0);
a7812ae4 2646 t1 = tcg_temp_new_i64();
ea363694 2647 tcg_gen_extu_i32_tl(t1, t0);
a7812ae4 2648 tcg_temp_free_i32(t0);
ea363694 2649 gen_qemu_st32_ppc64(t1, arg1, flags);
a7812ae4 2650 tcg_temp_free_i64(t1);
b61f2753 2651 } else
ea363694 2652 gen_qemu_st32_ppc64(arg0, arg1, flags);
b61f2753
AJ
2653}
2654
ea363694 2655static always_inline void gen_qemu_st64(TCGv arg0, TCGv arg1, int flags)
b61f2753
AJ
2656{
2657 if (unlikely(flags & 1)) {
a7812ae4 2658 TCGv_i64 t0 = tcg_temp_new_i64();
ea363694
AJ
2659 tcg_gen_bswap_i64(t0, arg0);
2660 gen_qemu_st64_ppc64(t0, arg1, flags);
a7812ae4 2661 tcg_temp_free_i64(t0);
b61f2753 2662 } else
ea363694 2663 gen_qemu_st64_ppc64(arg0, arg1, flags);
b61f2753
AJ
2664}
2665
2666
2667#else /* defined(TARGET_PPC64) */
a0d7d5a7
AJ
2668#define GEN_QEMU_LD_PPC32(width) \
2669static always_inline void gen_qemu_ld##width##_ppc32(TCGv arg0, TCGv arg1, int flags) \
2670{ \
2671 tcg_gen_qemu_ld##width(arg0, arg1, flags >> 1); \
b61f2753
AJ
2672}
2673GEN_QEMU_LD_PPC32(8u)
2674GEN_QEMU_LD_PPC32(8s)
2675GEN_QEMU_LD_PPC32(16u)
2676GEN_QEMU_LD_PPC32(16s)
2677GEN_QEMU_LD_PPC32(32u)
2678GEN_QEMU_LD_PPC32(32s)
a0d7d5a7
AJ
2679static always_inline void gen_qemu_ld64_ppc32(TCGv_i64 arg0, TCGv arg1, int flags)
2680{
2681 tcg_gen_qemu_ld64(arg0, arg1, flags >> 1);
2682}
b61f2753 2683
a0d7d5a7
AJ
2684#define GEN_QEMU_ST_PPC32(width) \
2685static always_inline void gen_qemu_st##width##_ppc32(TCGv arg0, TCGv arg1, int flags) \
2686{ \
2687 tcg_gen_qemu_st##width(arg0, arg1, flags >> 1); \
b61f2753
AJ
2688}
2689GEN_QEMU_ST_PPC32(8)
2690GEN_QEMU_ST_PPC32(16)
2691GEN_QEMU_ST_PPC32(32)
a0d7d5a7
AJ
2692static always_inline void gen_qemu_st64_ppc32(TCGv_i64 arg0, TCGv arg1, int flags)
2693{
2694 tcg_gen_qemu_st64(arg0, arg1, flags >> 1);
2695}
b61f2753 2696
ea363694 2697static always_inline void gen_qemu_ld8u(TCGv arg0, TCGv arg1, int flags)
b61f2753 2698{
ea363694 2699 gen_qemu_ld8u_ppc32(arg0, arg1, flags >> 1);
b61f2753
AJ
2700}
2701
ea363694 2702static always_inline void gen_qemu_ld8s(TCGv arg0, TCGv arg1, int flags)
b61f2753 2703{
ea363694 2704 gen_qemu_ld8s_ppc32(arg0, arg1, flags >> 1);
b61f2753
AJ
2705}
2706
ea363694 2707static always_inline void gen_qemu_ld16u(TCGv arg0, TCGv arg1, int flags)
b61f2753 2708{
ea363694 2709 gen_qemu_ld16u_ppc32(arg0, arg1, flags >> 1);
b61f2753 2710 if (unlikely(flags & 1))
ea363694 2711 tcg_gen_bswap16_i32(arg0, arg0);
b61f2753
AJ
2712}
2713
ea363694 2714static always_inline void gen_qemu_ld16s(TCGv arg0, TCGv arg1, int flags)
b61f2753
AJ
2715{
2716 if (unlikely(flags & 1)) {
ea363694
AJ
2717 gen_qemu_ld16u_ppc32(arg0, arg1, flags);
2718 tcg_gen_bswap16_i32(arg0, arg0);
2719 tcg_gen_ext16s_i32(arg0, arg0);
b61f2753 2720 } else
ea363694 2721 gen_qemu_ld16s_ppc32(arg0, arg1, flags);
b61f2753
AJ
2722}
2723
ea363694 2724static always_inline void gen_qemu_ld32u(TCGv arg0, TCGv arg1, int flags)
b61f2753 2725{
ea363694 2726 gen_qemu_ld32u_ppc32(arg0, arg1, flags);
b61f2753 2727 if (unlikely(flags & 1))
ea363694 2728 tcg_gen_bswap_i32(arg0, arg0);
b61f2753
AJ
2729}
2730
a0d7d5a7
AJ
2731static always_inline void gen_qemu_ld64(TCGv_i64 arg0, TCGv arg1, int flags)
2732{
2733 gen_qemu_ld64_ppc32(arg0, arg1, flags);
2734 if (unlikely(flags & 1))
2735 tcg_gen_bswap_i64(arg0, arg0);
2736}
2737
ea363694 2738static always_inline void gen_qemu_st8(TCGv arg0, TCGv arg1, int flags)
b61f2753 2739{
e32ad5c2 2740 gen_qemu_st8_ppc32(arg0, arg1, flags);
b61f2753
AJ
2741}
2742
ea363694 2743static always_inline void gen_qemu_st16(TCGv arg0, TCGv arg1, int flags)
b61f2753
AJ
2744{
2745 if (unlikely(flags & 1)) {
a7812ae4 2746 TCGv_i32 temp = tcg_temp_new_i32();
ea363694 2747 tcg_gen_ext16u_i32(temp, arg0);
b61f2753 2748 tcg_gen_bswap16_i32(temp, temp);
e32ad5c2 2749 gen_qemu_st16_ppc32(temp, arg1, flags);
a7812ae4 2750 tcg_temp_free_i32(temp);
b61f2753 2751 } else
e32ad5c2 2752 gen_qemu_st16_ppc32(arg0, arg1, flags);
b61f2753
AJ
2753}
2754
ea363694 2755static always_inline void gen_qemu_st32(TCGv arg0, TCGv arg1, int flags)
b61f2753
AJ
2756{
2757 if (unlikely(flags & 1)) {
a7812ae4 2758 TCGv_i32 temp = tcg_temp_new_i32();
ea363694 2759 tcg_gen_bswap_i32(temp, arg0);
e32ad5c2 2760 gen_qemu_st32_ppc32(temp, arg1, flags);
a7812ae4 2761 tcg_temp_free_i32(temp);
b61f2753 2762 } else
e32ad5c2 2763 gen_qemu_st32_ppc32(arg0, arg1, flags);
b61f2753
AJ
2764}
2765
a0d7d5a7
AJ
2766static always_inline void gen_qemu_st64(TCGv_i64 arg0, TCGv arg1, int flags)
2767{
2768 if (unlikely(flags & 1)) {
2769 TCGv_i64 temp = tcg_temp_new_i64();
2770 tcg_gen_bswap_i64(temp, arg0);
2771 gen_qemu_st64_ppc32(temp, arg1, flags);
2772 tcg_temp_free_i64(temp);
2773 } else
2774 gen_qemu_st64_ppc32(arg0, arg1, flags);
2775}
b61f2753
AJ
2776#endif
2777
0c8aacd4
AJ
2778#define GEN_LD(name, ldop, opc, type) \
2779GEN_HANDLER(name, opc, 0xFF, 0xFF, 0x00000000, type) \
79aceca5 2780{ \
0c8aacd4 2781 TCGv EA = tcg_temp_new(); \
a7859e89 2782 gen_set_access_type(ACCESS_INT); \
b61f2753 2783 gen_addr_imm_index(EA, ctx, 0); \
0c8aacd4 2784 gen_qemu_##ldop(cpu_gpr[rD(ctx->opcode)], EA, ctx->mem_idx); \
b61f2753 2785 tcg_temp_free(EA); \
79aceca5
FB
2786}
2787
0c8aacd4
AJ
2788#define GEN_LDU(name, ldop, opc, type) \
2789GEN_HANDLER(name##u, opc, 0xFF, 0xFF, 0x00000000, type) \
79aceca5 2790{ \
b61f2753 2791 TCGv EA; \
76a66253
JM
2792 if (unlikely(rA(ctx->opcode) == 0 || \
2793 rA(ctx->opcode) == rD(ctx->opcode))) { \
e1833e1f 2794 GEN_EXCP_INVAL(ctx); \
9fddaa0c 2795 return; \
9a64fbe4 2796 } \
0c8aacd4 2797 EA = tcg_temp_new(); \
a7859e89 2798 gen_set_access_type(ACCESS_INT); \
9d53c753 2799 if (type == PPC_64B) \
b61f2753 2800 gen_addr_imm_index(EA, ctx, 0x03); \
9d53c753 2801 else \
b61f2753 2802 gen_addr_imm_index(EA, ctx, 0); \
0c8aacd4 2803 gen_qemu_##ldop(cpu_gpr[rD(ctx->opcode)], EA, ctx->mem_idx); \
b61f2753
AJ
2804 tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], EA); \
2805 tcg_temp_free(EA); \
79aceca5
FB
2806}
2807
0c8aacd4
AJ
2808#define GEN_LDUX(name, ldop, opc2, opc3, type) \
2809GEN_HANDLER(name##ux, 0x1F, opc2, opc3, 0x00000001, type) \
79aceca5 2810{ \
b61f2753 2811 TCGv EA; \
76a66253
JM
2812 if (unlikely(rA(ctx->opcode) == 0 || \
2813 rA(ctx->opcode) == rD(ctx->opcode))) { \
e1833e1f 2814 GEN_EXCP_INVAL(ctx); \
9fddaa0c 2815 return; \
9a64fbe4 2816 } \
0c8aacd4 2817 EA = tcg_temp_new(); \
a7859e89 2818 gen_set_access_type(ACCESS_INT); \
b61f2753 2819 gen_addr_reg_index(EA, ctx); \
0c8aacd4 2820 gen_qemu_##ldop(cpu_gpr[rD(ctx->opcode)], EA, ctx->mem_idx); \
b61f2753
AJ
2821 tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], EA); \
2822 tcg_temp_free(EA); \
79aceca5
FB
2823}
2824
0c8aacd4
AJ
2825#define GEN_LDX(name, ldop, opc2, opc3, type) \
2826GEN_HANDLER(name##x, 0x1F, opc2, opc3, 0x00000001, type) \
79aceca5 2827{ \
0c8aacd4 2828 TCGv EA = tcg_temp_new(); \
a7859e89 2829 gen_set_access_type(ACCESS_INT); \
b61f2753 2830 gen_addr_reg_index(EA, ctx); \
0c8aacd4 2831 gen_qemu_##ldop(cpu_gpr[rD(ctx->opcode)], EA, ctx->mem_idx); \
b61f2753 2832 tcg_temp_free(EA); \
79aceca5
FB
2833}
2834
0c8aacd4
AJ
2835#define GEN_LDS(name, ldop, op, type) \
2836GEN_LD(name, ldop, op | 0x20, type); \
2837GEN_LDU(name, ldop, op | 0x21, type); \
2838GEN_LDUX(name, ldop, 0x17, op | 0x01, type); \
2839GEN_LDX(name, ldop, 0x17, op | 0x00, type)
79aceca5
FB
2840
2841/* lbz lbzu lbzux lbzx */
0c8aacd4 2842GEN_LDS(lbz, ld8u, 0x02, PPC_INTEGER);
79aceca5 2843/* lha lhau lhaux lhax */
0c8aacd4 2844GEN_LDS(lha, ld16s, 0x0A, PPC_INTEGER);
79aceca5 2845/* lhz lhzu lhzux lhzx */
0c8aacd4 2846GEN_LDS(lhz, ld16u, 0x08, PPC_INTEGER);
79aceca5 2847/* lwz lwzu lwzux lwzx */
0c8aacd4 2848GEN_LDS(lwz, ld32u, 0x00, PPC_INTEGER);
d9bce9d9 2849#if defined(TARGET_PPC64)
d9bce9d9 2850/* lwaux */
0c8aacd4 2851GEN_LDUX(lwa, ld32s, 0x15, 0x0B, PPC_64B);
d9bce9d9 2852/* lwax */
0c8aacd4 2853GEN_LDX(lwa, ld32s, 0x15, 0x0A, PPC_64B);
d9bce9d9 2854/* ldux */
0c8aacd4 2855GEN_LDUX(ld, ld64, 0x15, 0x01, PPC_64B);
d9bce9d9 2856/* ldx */
0c8aacd4 2857GEN_LDX(ld, ld64, 0x15, 0x00, PPC_64B);
d9bce9d9
JM
2858GEN_HANDLER(ld, 0x3A, 0xFF, 0xFF, 0x00000000, PPC_64B)
2859{
b61f2753 2860 TCGv EA;
d9bce9d9
JM
2861 if (Rc(ctx->opcode)) {
2862 if (unlikely(rA(ctx->opcode) == 0 ||
2863 rA(ctx->opcode) == rD(ctx->opcode))) {
e1833e1f 2864 GEN_EXCP_INVAL(ctx);
d9bce9d9
JM
2865 return;
2866 }
2867 }
a7812ae4 2868 EA = tcg_temp_new();
a7859e89 2869 gen_set_access_type(ACCESS_INT);
b61f2753 2870 gen_addr_imm_index(EA, ctx, 0x03);
d9bce9d9
JM
2871 if (ctx->opcode & 0x02) {
2872 /* lwa (lwau is undefined) */
b61f2753 2873 gen_qemu_ld32s(cpu_gpr[rD(ctx->opcode)], EA, ctx->mem_idx);
d9bce9d9
JM
2874 } else {
2875 /* ld - ldu */
b61f2753 2876 gen_qemu_ld64(cpu_gpr[rD(ctx->opcode)], EA, ctx->mem_idx);
d9bce9d9 2877 }
d9bce9d9 2878 if (Rc(ctx->opcode))
b61f2753
AJ
2879 tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], EA);
2880 tcg_temp_free(EA);
d9bce9d9 2881}
be147d08
JM
2882/* lq */
2883GEN_HANDLER(lq, 0x38, 0xFF, 0xFF, 0x00000000, PPC_64BX)
2884{
2885#if defined(CONFIG_USER_ONLY)
2886 GEN_EXCP_PRIVOPC(ctx);
2887#else
2888 int ra, rd;
b61f2753 2889 TCGv EA;
be147d08
JM
2890
2891 /* Restore CPU state */
2892 if (unlikely(ctx->supervisor == 0)) {
2893 GEN_EXCP_PRIVOPC(ctx);
2894 return;
2895 }
2896 ra = rA(ctx->opcode);
2897 rd = rD(ctx->opcode);
2898 if (unlikely((rd & 1) || rd == ra)) {
2899 GEN_EXCP_INVAL(ctx);
2900 return;
2901 }
2902 if (unlikely(ctx->mem_idx & 1)) {
2903 /* Little-endian mode is not handled */
2904 GEN_EXCP(ctx, POWERPC_EXCP_ALIGN, POWERPC_EXCP_ALIGN_LE);
2905 return;
2906 }
a7812ae4 2907 EA = tcg_temp_new();
a7859e89 2908 gen_set_access_type(ACCESS_INT);
b61f2753
AJ
2909 gen_addr_imm_index(EA, ctx, 0x0F);
2910 gen_qemu_ld64(cpu_gpr[rd], EA, ctx->mem_idx);
2911 tcg_gen_addi_tl(EA, EA, 8);
2912 gen_qemu_ld64(cpu_gpr[rd+1], EA, ctx->mem_idx);
2913 tcg_temp_free(EA);
be147d08
JM
2914#endif
2915}
d9bce9d9 2916#endif
79aceca5
FB
2917
2918/*** Integer store ***/
0c8aacd4
AJ
2919#define GEN_ST(name, stop, opc, type) \
2920GEN_HANDLER(name, opc, 0xFF, 0xFF, 0x00000000, type) \
79aceca5 2921{ \
0c8aacd4 2922 TCGv EA = tcg_temp_new(); \
a7859e89 2923 gen_set_access_type(ACCESS_INT); \
b61f2753 2924 gen_addr_imm_index(EA, ctx, 0); \
0c8aacd4 2925 gen_qemu_##stop(cpu_gpr[rS(ctx->opcode)], EA, ctx->mem_idx); \
b61f2753 2926 tcg_temp_free(EA); \
79aceca5
FB
2927}
2928
0c8aacd4
AJ
2929#define GEN_STU(name, stop, opc, type) \
2930GEN_HANDLER(stop##u, opc, 0xFF, 0xFF, 0x00000000, type) \
79aceca5 2931{ \
b61f2753 2932 TCGv EA; \
76a66253 2933 if (unlikely(rA(ctx->opcode) == 0)) { \
e1833e1f 2934 GEN_EXCP_INVAL(ctx); \
9fddaa0c 2935 return; \
9a64fbe4 2936 } \
0c8aacd4 2937 EA = tcg_temp_new(); \
a7859e89 2938 gen_set_access_type(ACCESS_INT); \
9d53c753 2939 if (type == PPC_64B) \
b61f2753 2940 gen_addr_imm_index(EA, ctx, 0x03); \
9d53c753 2941 else \
b61f2753 2942 gen_addr_imm_index(EA, ctx, 0); \
0c8aacd4 2943 gen_qemu_##stop(cpu_gpr[rS(ctx->opcode)], EA, ctx->mem_idx); \
b61f2753
AJ
2944 tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], EA); \
2945 tcg_temp_free(EA); \
79aceca5
FB
2946}
2947
0c8aacd4
AJ
2948#define GEN_STUX(name, stop, opc2, opc3, type) \
2949GEN_HANDLER(name##ux, 0x1F, opc2, opc3, 0x00000001, type) \
79aceca5 2950{ \
b61f2753 2951 TCGv EA; \
76a66253 2952 if (unlikely(rA(ctx->opcode) == 0)) { \
e1833e1f 2953 GEN_EXCP_INVAL(ctx); \
9fddaa0c 2954 return; \
9a64fbe4 2955 } \
0c8aacd4 2956 EA = tcg_temp_new(); \
a7859e89 2957 gen_set_access_type(ACCESS_INT); \
b61f2753 2958 gen_addr_reg_index(EA, ctx); \
0c8aacd4 2959 gen_qemu_##stop(cpu_gpr[rS(ctx->opcode)], EA, ctx->mem_idx); \
b61f2753
AJ
2960 tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], EA); \
2961 tcg_temp_free(EA); \
79aceca5
FB
2962}
2963
0c8aacd4
AJ
2964#define GEN_STX(name, stop, opc2, opc3, type) \
2965GEN_HANDLER(name##x, 0x1F, opc2, opc3, 0x00000001, type) \
79aceca5 2966{ \
0c8aacd4 2967 TCGv EA = tcg_temp_new(); \
a7859e89 2968 gen_set_access_type(ACCESS_INT); \
b61f2753 2969 gen_addr_reg_index(EA, ctx); \
0c8aacd4 2970 gen_qemu_##stop(cpu_gpr[rS(ctx->opcode)], EA, ctx->mem_idx); \
b61f2753 2971 tcg_temp_free(EA); \
79aceca5
FB
2972}
2973
0c8aacd4
AJ
2974#define GEN_STS(name, stop, op, type) \
2975GEN_ST(name, stop, op | 0x20, type); \
2976GEN_STU(name, stop, op | 0x21, type); \
2977GEN_STUX(name, stop, 0x17, op | 0x01, type); \
2978GEN_STX(name, stop, 0x17, op | 0x00, type)
79aceca5
FB
2979
2980/* stb stbu stbux stbx */
0c8aacd4 2981GEN_STS(stb, st8, 0x06, PPC_INTEGER);
79aceca5 2982/* sth sthu sthux sthx */
0c8aacd4 2983GEN_STS(sth, st16, 0x0C, PPC_INTEGER);
79aceca5 2984/* stw stwu stwux stwx */
0c8aacd4 2985GEN_STS(stw, st32, 0x04, PPC_INTEGER);
d9bce9d9 2986#if defined(TARGET_PPC64)
0c8aacd4
AJ
2987GEN_STUX(std, st64, 0x15, 0x05, PPC_64B);
2988GEN_STX(std, st64, 0x15, 0x04, PPC_64B);
be147d08 2989GEN_HANDLER(std, 0x3E, 0xFF, 0xFF, 0x00000000, PPC_64B)
d9bce9d9 2990{
be147d08 2991 int rs;
b61f2753 2992 TCGv EA;
be147d08
JM
2993
2994 rs = rS(ctx->opcode);
2995 if ((ctx->opcode & 0x3) == 0x2) {
2996#if defined(CONFIG_USER_ONLY)
2997 GEN_EXCP_PRIVOPC(ctx);
2998#else
2999 /* stq */
3000 if (unlikely(ctx->supervisor == 0)) {
3001 GEN_EXCP_PRIVOPC(ctx);
3002 return;
3003 }
3004 if (unlikely(rs & 1)) {
e1833e1f 3005 GEN_EXCP_INVAL(ctx);
d9bce9d9
JM
3006 return;
3007 }
be147d08
JM
3008 if (unlikely(ctx->mem_idx & 1)) {
3009 /* Little-endian mode is not handled */
3010 GEN_EXCP(ctx, POWERPC_EXCP_ALIGN, POWERPC_EXCP_ALIGN_LE);
3011 return;
3012 }
a7812ae4 3013 EA = tcg_temp_new();
a7859e89 3014 gen_set_access_type(ACCESS_INT);
b61f2753
AJ
3015 gen_addr_imm_index(EA, ctx, 0x03);
3016 gen_qemu_st64(cpu_gpr[rs], EA, ctx->mem_idx);
3017 tcg_gen_addi_tl(EA, EA, 8);
3018 gen_qemu_st64(cpu_gpr[rs+1], EA, ctx->mem_idx);
3019 tcg_temp_free(EA);
be147d08
JM
3020#endif
3021 } else {
3022 /* std / stdu */
3023 if (Rc(ctx->opcode)) {
3024 if (unlikely(rA(ctx->opcode) == 0)) {
3025 GEN_EXCP_INVAL(ctx);
3026 return;
3027 }
3028 }
a7812ae4 3029 EA = tcg_temp_new();
a7859e89 3030 gen_set_access_type(ACCESS_INT);
b61f2753
AJ
3031 gen_addr_imm_index(EA, ctx, 0x03);
3032 gen_qemu_st64(cpu_gpr[rs], EA, ctx->mem_idx);
be147d08 3033 if (Rc(ctx->opcode))
b61f2753
AJ
3034 tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], EA);
3035 tcg_temp_free(EA);
d9bce9d9 3036 }
d9bce9d9
JM
3037}
3038#endif
79aceca5
FB
3039/*** Integer load and store with byte reverse ***/
3040/* lhbrx */
b61f2753
AJ
3041void always_inline gen_qemu_ld16ur(TCGv t0, TCGv t1, int flags)
3042{
a7812ae4
PB
3043 TCGv_i32 temp = tcg_temp_new_i32();
3044 gen_qemu_ld16u(t0, t1, flags);
3045 tcg_gen_trunc_tl_i32(temp, t0);
b61f2753
AJ
3046 tcg_gen_bswap16_i32(temp, temp);
3047 tcg_gen_extu_i32_tl(t0, temp);
a7812ae4 3048 tcg_temp_free_i32(temp);
b61f2753 3049}
0c8aacd4 3050GEN_LDX(lhbr, ld16ur, 0x16, 0x18, PPC_INTEGER);
b61f2753 3051
79aceca5 3052/* lwbrx */
b61f2753
AJ
3053void always_inline gen_qemu_ld32ur(TCGv t0, TCGv t1, int flags)
3054{
a7812ae4
PB
3055 TCGv_i32 temp = tcg_temp_new_i32();
3056 gen_qemu_ld32u(t0, t1, flags);
3057 tcg_gen_trunc_tl_i32(temp, t0);
b61f2753
AJ
3058 tcg_gen_bswap_i32(temp, temp);
3059 tcg_gen_extu_i32_tl(t0, temp);
a7812ae4 3060 tcg_temp_free_i32(temp);
b61f2753 3061}
0c8aacd4 3062GEN_LDX(lwbr, ld32ur, 0x16, 0x10, PPC_INTEGER);
b61f2753 3063
79aceca5 3064/* sthbrx */
b61f2753
AJ
3065void always_inline gen_qemu_st16r(TCGv t0, TCGv t1, int flags)
3066{
a7812ae4
PB
3067 TCGv_i32 temp = tcg_temp_new_i32();
3068 TCGv t2 = tcg_temp_new();
b61f2753
AJ
3069 tcg_gen_trunc_tl_i32(temp, t0);
3070 tcg_gen_ext16u_i32(temp, temp);
3071 tcg_gen_bswap16_i32(temp, temp);
a7812ae4
PB
3072 tcg_gen_extu_i32_tl(t2, temp);
3073 tcg_temp_free_i32(temp);
3074 gen_qemu_st16(t2, t1, flags);
3075 tcg_temp_free(t2);
b61f2753 3076}
0c8aacd4 3077GEN_STX(sthbr, st16r, 0x16, 0x1C, PPC_INTEGER);
b61f2753 3078
79aceca5 3079/* stwbrx */
b61f2753
AJ
3080void always_inline gen_qemu_st32r(TCGv t0, TCGv t1, int flags)
3081{
a7812ae4
PB
3082 TCGv_i32 temp = tcg_temp_new_i32();
3083 TCGv t2 = tcg_temp_new();
b61f2753
AJ
3084 tcg_gen_trunc_tl_i32(temp, t0);
3085 tcg_gen_bswap_i32(temp, temp);
a7812ae4
PB
3086 tcg_gen_extu_i32_tl(t2, temp);
3087 tcg_temp_free_i32(temp);
87006d13 3088 gen_qemu_st32(t2, t1, flags);
a7812ae4 3089 tcg_temp_free(t2);
b61f2753 3090}
0c8aacd4 3091GEN_STX(stwbr, st32r, 0x16, 0x14, PPC_INTEGER);
79aceca5
FB
3092
3093/*** Integer load and store multiple ***/
3094/* lmw */
3095GEN_HANDLER(lmw, 0x2E, 0xFF, 0xFF, 0x00000000, PPC_INTEGER)
3096{
ff4a62cd
AJ
3097 TCGv t0 = tcg_temp_new();
3098 TCGv_i32 t1 = tcg_const_i32(rD(ctx->opcode));
76a66253 3099 /* NIP cannot be restored if the memory exception comes from an helper */
d9bce9d9 3100 gen_update_nip(ctx, ctx->nip - 4);
ff4a62cd
AJ
3101 gen_addr_imm_index(t0, ctx, 0);
3102 gen_helper_lmw(t0, t1);
3103 tcg_temp_free(t0);
3104 tcg_temp_free_i32(t1);
79aceca5
FB
3105}
3106
3107/* stmw */
3108GEN_HANDLER(stmw, 0x2F, 0xFF, 0xFF, 0x00000000, PPC_INTEGER)
3109{
ff4a62cd
AJ
3110 TCGv t0 = tcg_temp_new();
3111 TCGv_i32 t1 = tcg_const_i32(rS(ctx->opcode));
76a66253 3112 /* NIP cannot be restored if the memory exception comes from an helper */
d9bce9d9 3113 gen_update_nip(ctx, ctx->nip - 4);
ff4a62cd
AJ
3114 gen_addr_imm_index(t0, ctx, 0);
3115 gen_helper_stmw(t0, t1);
3116 tcg_temp_free(t0);
3117 tcg_temp_free_i32(t1);
79aceca5
FB
3118}
3119
3120/*** Integer load and store strings ***/
3121/* lswi */
3fc6c082 3122/* PowerPC32 specification says we must generate an exception if
9a64fbe4
FB
3123 * rA is in the range of registers to be loaded.
3124 * In an other hand, IBM says this is valid, but rA won't be loaded.
3125 * For now, I'll follow the spec...
3126 */
05332d70 3127GEN_HANDLER(lswi, 0x1F, 0x15, 0x12, 0x00000001, PPC_STRING)
79aceca5 3128{
dfbc799d
AJ
3129 TCGv t0;
3130 TCGv_i32 t1, t2;
79aceca5
FB
3131 int nb = NB(ctx->opcode);
3132 int start = rD(ctx->opcode);
9a64fbe4 3133 int ra = rA(ctx->opcode);
79aceca5
FB
3134 int nr;
3135
3136 if (nb == 0)
3137 nb = 32;
3138 nr = nb / 4;
76a66253
JM
3139 if (unlikely(((start + nr) > 32 &&
3140 start <= ra && (start + nr - 32) > ra) ||
3141 ((start + nr) <= 32 && start <= ra && (start + nr) > ra))) {
e1833e1f
JM
3142 GEN_EXCP(ctx, POWERPC_EXCP_PROGRAM,
3143 POWERPC_EXCP_INVAL | POWERPC_EXCP_INVAL_LSWX);
9fddaa0c 3144 return;
297d8e62 3145 }
8dd4983c 3146 /* NIP cannot be restored if the memory exception comes from an helper */
d9bce9d9 3147 gen_update_nip(ctx, ctx->nip - 4);
dfbc799d
AJ
3148 t0 = tcg_temp_new();
3149 gen_addr_register(t0, ctx);
3150 t1 = tcg_const_i32(nb);
3151 t2 = tcg_const_i32(start);
3152 gen_helper_lsw(t0, t1, t2);
3153 tcg_temp_free(t0);
3154 tcg_temp_free_i32(t1);
3155 tcg_temp_free_i32(t2);
79aceca5
FB
3156}
3157
3158/* lswx */
05332d70 3159GEN_HANDLER(lswx, 0x1F, 0x15, 0x10, 0x00000001, PPC_STRING)
79aceca5 3160{
dfbc799d
AJ
3161 TCGv t0 = tcg_temp_new();
3162 TCGv_i32 t1 = tcg_const_i32(rD(ctx->opcode));
3163 TCGv_i32 t2 = tcg_const_i32(rA(ctx->opcode));
3164 TCGv_i32 t3 = tcg_const_i32(rB(ctx->opcode));
76a66253 3165 /* NIP cannot be restored if the memory exception comes from an helper */
d9bce9d9 3166 gen_update_nip(ctx, ctx->nip - 4);
dfbc799d
AJ
3167 gen_addr_reg_index(t0, ctx);
3168 gen_helper_lswx(t0, t1, t2, t3);
3169 tcg_temp_free(t0);
3170 tcg_temp_free_i32(t1);
3171 tcg_temp_free_i32(t2);
3172 tcg_temp_free_i32(t3);
79aceca5
FB
3173}
3174
3175/* stswi */
05332d70 3176GEN_HANDLER(stswi, 0x1F, 0x15, 0x16, 0x00000001, PPC_STRING)
79aceca5 3177{
4b3686fa 3178 int nb = NB(ctx->opcode);
dfbc799d
AJ
3179 TCGv t0 = tcg_temp_new();
3180 TCGv_i32 t1;
3181 TCGv_i32 t2 = tcg_const_i32(rS(ctx->opcode));
76a66253 3182 /* NIP cannot be restored if the memory exception comes from an helper */
d9bce9d9 3183 gen_update_nip(ctx, ctx->nip - 4);
dfbc799d 3184 gen_addr_register(t0, ctx);
4b3686fa
FB
3185 if (nb == 0)
3186 nb = 32;
dfbc799d
AJ
3187 t1 = tcg_const_i32(nb);
3188 gen_helper_stsw(t0, t1, t2);
3189 tcg_temp_free(t0);
3190 tcg_temp_free_i32(t1);
3191 tcg_temp_free_i32(t2);
79aceca5
FB
3192}
3193
3194/* stswx */
05332d70 3195GEN_HANDLER(stswx, 0x1F, 0x15, 0x14, 0x00000001, PPC_STRING)
79aceca5 3196{
dfbc799d
AJ
3197 TCGv t0 = tcg_temp_new();
3198 TCGv_i32 t1 = tcg_temp_new_i32();
3199 TCGv_i32 t2 = tcg_const_i32(rS(ctx->opcode));
8dd4983c 3200 /* NIP cannot be restored if the memory exception comes from an helper */
5fafdf24 3201 gen_update_nip(ctx, ctx->nip - 4);
dfbc799d
AJ
3202 gen_addr_reg_index(t0, ctx);
3203 tcg_gen_trunc_tl_i32(t1, cpu_xer);
3204 tcg_gen_andi_i32(t1, t1, 0x7F);
3205 gen_helper_stsw(t0, t1, t2);
3206 tcg_temp_free(t0);
3207 tcg_temp_free_i32(t1);
3208 tcg_temp_free_i32(t2);
79aceca5
FB
3209}
3210
3211/*** Memory synchronisation ***/
3212/* eieio */
0db1b20e 3213GEN_HANDLER(eieio, 0x1F, 0x16, 0x1A, 0x03FFF801, PPC_MEM_EIEIO)
79aceca5 3214{
79aceca5
FB
3215}
3216
3217/* isync */
0db1b20e 3218GEN_HANDLER(isync, 0x13, 0x16, 0x04, 0x03FFF801, PPC_MEM)
79aceca5 3219{
e1833e1f 3220 GEN_STOP(ctx);
79aceca5
FB
3221}
3222
111bfab3
FB
3223#define op_lwarx() (*gen_op_lwarx[ctx->mem_idx])()
3224#define op_stwcx() (*gen_op_stwcx[ctx->mem_idx])()
7863667f
JM
3225static GenOpFunc *gen_op_lwarx[NB_MEM_FUNCS] = {
3226 GEN_MEM_FUNCS(lwarx),
111bfab3 3227};
7863667f
JM
3228static GenOpFunc *gen_op_stwcx[NB_MEM_FUNCS] = {
3229 GEN_MEM_FUNCS(stwcx),
985a19d6 3230};
9a64fbe4 3231
111bfab3 3232/* lwarx */
76a66253 3233GEN_HANDLER(lwarx, 0x1F, 0x14, 0x00, 0x00000001, PPC_RES)
79aceca5 3234{
30032c94
JM
3235 /* NIP cannot be restored if the memory exception comes from an helper */
3236 gen_update_nip(ctx, ctx->nip - 4);
a7859e89 3237 gen_set_access_type(ACCESS_RES);
e2be8d8d 3238 gen_addr_reg_index(cpu_T[0], ctx);
985a19d6 3239 op_lwarx();
f78fb44e 3240 tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_T[1]);
79aceca5
FB
3241}
3242
3243/* stwcx. */
c7697e1f 3244GEN_HANDLER2(stwcx_, "stwcx.", 0x1F, 0x16, 0x04, 0x00000000, PPC_RES)
79aceca5 3245{
30032c94
JM
3246 /* NIP cannot be restored if the memory exception comes from an helper */
3247 gen_update_nip(ctx, ctx->nip - 4);
a7859e89 3248 gen_set_access_type(ACCESS_RES);
e2be8d8d 3249 gen_addr_reg_index(cpu_T[0], ctx);
f78fb44e 3250 tcg_gen_mov_tl(cpu_T[1], cpu_gpr[rS(ctx->opcode)]);
9a64fbe4 3251 op_stwcx();
79aceca5
FB
3252}
3253
426613db
JM
3254#if defined(TARGET_PPC64)
3255#define op_ldarx() (*gen_op_ldarx[ctx->mem_idx])()
3256#define op_stdcx() (*gen_op_stdcx[ctx->mem_idx])()
7863667f
JM
3257static GenOpFunc *gen_op_ldarx[NB_MEM_FUNCS] = {
3258 GEN_MEM_FUNCS(ldarx),
426613db 3259};
7863667f
JM
3260static GenOpFunc *gen_op_stdcx[NB_MEM_FUNCS] = {
3261 GEN_MEM_FUNCS(stdcx),
426613db 3262};
426613db
JM
3263
3264/* ldarx */
a750fc0b 3265GEN_HANDLER(ldarx, 0x1F, 0x14, 0x02, 0x00000001, PPC_64B)
426613db 3266{
30032c94
JM
3267 /* NIP cannot be restored if the memory exception comes from an helper */
3268 gen_update_nip(ctx, ctx->nip - 4);
a7859e89 3269 gen_set_access_type(ACCESS_RES);
e2be8d8d 3270 gen_addr_reg_index(cpu_T[0], ctx);
426613db 3271 op_ldarx();
f78fb44e 3272 tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_T[1]);
426613db
JM
3273}
3274
3275/* stdcx. */
c7697e1f 3276GEN_HANDLER2(stdcx_, "stdcx.", 0x1F, 0x16, 0x06, 0x00000000, PPC_64B)
426613db 3277{
30032c94
JM
3278 /* NIP cannot be restored if the memory exception comes from an helper */
3279 gen_update_nip(ctx, ctx->nip - 4);
a7859e89 3280 gen_set_access_type(ACCESS_RES);
e2be8d8d 3281 gen_addr_reg_index(cpu_T[0], ctx);
f78fb44e 3282 tcg_gen_mov_tl(cpu_T[1], cpu_gpr[rS(ctx->opcode)]);
426613db
JM
3283 op_stdcx();
3284}
3285#endif /* defined(TARGET_PPC64) */
3286
79aceca5 3287/* sync */
a902d886 3288GEN_HANDLER(sync, 0x1F, 0x16, 0x12, 0x039FF801, PPC_MEM_SYNC)
79aceca5 3289{
79aceca5
FB
3290}
3291
0db1b20e
JM
3292/* wait */
3293GEN_HANDLER(wait, 0x1F, 0x1E, 0x01, 0x03FFF801, PPC_WAIT)
3294{
931ff272
AJ
3295 TCGv_i32 t0 = tcg_temp_new_i32();
3296 tcg_gen_st_i32(t0, cpu_env, offsetof(CPUState, halted));
3297 tcg_temp_free_i32(t0);
0db1b20e 3298 /* Stop translation, as the CPU is supposed to sleep from now */
be147d08 3299 GEN_EXCP(ctx, EXCP_HLT, 1);
0db1b20e
JM
3300}
3301
79aceca5 3302/*** Floating-point load ***/
a0d7d5a7
AJ
3303#define GEN_LDF(name, ldop, opc, type) \
3304GEN_HANDLER(name, opc, 0xFF, 0xFF, 0x00000000, type) \
79aceca5 3305{ \
a0d7d5a7 3306 TCGv EA; \
76a66253 3307 if (unlikely(!ctx->fpu_enabled)) { \
e1833e1f 3308 GEN_EXCP_NO_FP(ctx); \
4ecc3190
FB
3309 return; \
3310 } \
a7859e89 3311 gen_set_access_type(ACCESS_FLOAT); \
a0d7d5a7
AJ
3312 EA = tcg_temp_new(); \
3313 gen_addr_imm_index(EA, ctx, 0); \
3314 gen_qemu_##ldop(cpu_fpr[rD(ctx->opcode)], EA, ctx->mem_idx); \
3315 tcg_temp_free(EA); \
79aceca5
FB
3316}
3317
a0d7d5a7
AJ
3318#define GEN_LDUF(name, ldop, opc, type) \
3319GEN_HANDLER(name##u, opc, 0xFF, 0xFF, 0x00000000, type) \
79aceca5 3320{ \
a0d7d5a7 3321 TCGv EA; \
76a66253 3322 if (unlikely(!ctx->fpu_enabled)) { \
e1833e1f 3323 GEN_EXCP_NO_FP(ctx); \
4ecc3190
FB
3324 return; \
3325 } \
76a66253 3326 if (unlikely(rA(ctx->opcode) == 0)) { \
e1833e1f 3327 GEN_EXCP_INVAL(ctx); \
9fddaa0c 3328 return; \
9a64fbe4 3329 } \
a7859e89 3330 gen_set_access_type(ACCESS_FLOAT); \
a0d7d5a7
AJ
3331 EA = tcg_temp_new(); \
3332 gen_addr_imm_index(EA, ctx, 0); \
3333 gen_qemu_##ldop(cpu_fpr[rD(ctx->opcode)], EA, ctx->mem_idx); \
3334 tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], EA); \
3335 tcg_temp_free(EA); \
79aceca5
FB
3336}
3337
a0d7d5a7
AJ
3338#define GEN_LDUXF(name, ldop, opc, type) \
3339GEN_HANDLER(name##ux, 0x1F, 0x17, opc, 0x00000001, type) \
79aceca5 3340{ \
a0d7d5a7 3341 TCGv EA; \
76a66253 3342 if (unlikely(!ctx->fpu_enabled)) { \
e1833e1f 3343 GEN_EXCP_NO_FP(ctx); \
4ecc3190
FB
3344 return; \
3345 } \
76a66253 3346 if (unlikely(rA(ctx->opcode) == 0)) { \
e1833e1f 3347 GEN_EXCP_INVAL(ctx); \
9fddaa0c 3348 return; \
9a64fbe4 3349 } \
a7859e89 3350 gen_set_access_type(ACCESS_FLOAT); \
a0d7d5a7
AJ
3351 EA = tcg_temp_new(); \
3352 gen_addr_reg_index(EA, ctx); \
3353 gen_qemu_##ldop(cpu_fpr[rD(ctx->opcode)], EA, ctx->mem_idx); \
3354 tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], EA); \
3355 tcg_temp_free(EA); \
79aceca5
FB
3356}
3357
a0d7d5a7
AJ
3358#define GEN_LDXF(name, ldop, opc2, opc3, type) \
3359GEN_HANDLER(name##x, 0x1F, opc2, opc3, 0x00000001, type) \
79aceca5 3360{ \
a0d7d5a7 3361 TCGv EA; \
76a66253 3362 if (unlikely(!ctx->fpu_enabled)) { \
e1833e1f 3363 GEN_EXCP_NO_FP(ctx); \
4ecc3190
FB
3364 return; \
3365 } \
a7859e89 3366 gen_set_access_type(ACCESS_FLOAT); \
a0d7d5a7
AJ
3367 EA = tcg_temp_new(); \
3368 gen_addr_reg_index(EA, ctx); \
3369 gen_qemu_##ldop(cpu_fpr[rD(ctx->opcode)], EA, ctx->mem_idx); \
3370 tcg_temp_free(EA); \
79aceca5
FB
3371}
3372
a0d7d5a7
AJ
3373#define GEN_LDFS(name, ldop, op, type) \
3374GEN_LDF(name, ldop, op | 0x20, type); \
3375GEN_LDUF(name, ldop, op | 0x21, type); \
3376GEN_LDUXF(name, ldop, op | 0x01, type); \
3377GEN_LDXF(name, ldop, 0x17, op | 0x00, type)
3378
3379static always_inline void gen_qemu_ld32fs(TCGv_i64 arg1, TCGv arg2, int flags)
3380{
3381 TCGv t0 = tcg_temp_new();
3382 TCGv_i32 t1 = tcg_temp_new_i32();
3383 gen_qemu_ld32u(t0, arg2, flags);
3384 tcg_gen_trunc_tl_i32(t1, t0);
3385 tcg_temp_free(t0);
3386 gen_helper_float32_to_float64(arg1, t1);
3387 tcg_temp_free_i32(t1);
3388}
79aceca5 3389
a0d7d5a7
AJ
3390 /* lfd lfdu lfdux lfdx */
3391GEN_LDFS(lfd, ld64, 0x12, PPC_FLOAT);
3392 /* lfs lfsu lfsux lfsx */
3393GEN_LDFS(lfs, ld32fs, 0x10, PPC_FLOAT);
79aceca5
FB
3394
3395/*** Floating-point store ***/
a0d7d5a7
AJ
3396#define GEN_STF(name, stop, opc, type) \
3397GEN_HANDLER(name, opc, 0xFF, 0xFF, 0x00000000, type) \
79aceca5 3398{ \
a0d7d5a7 3399 TCGv EA; \
76a66253 3400 if (unlikely(!ctx->fpu_enabled)) { \
e1833e1f 3401 GEN_EXCP_NO_FP(ctx); \
4ecc3190
FB
3402 return; \
3403 } \
a7859e89 3404 gen_set_access_type(ACCESS_FLOAT); \
a0d7d5a7
AJ
3405 EA = tcg_temp_new(); \
3406 gen_addr_imm_index(EA, ctx, 0); \
3407 gen_qemu_##stop(cpu_fpr[rS(ctx->opcode)], EA, ctx->mem_idx); \
3408 tcg_temp_free(EA); \
79aceca5
FB
3409}
3410
a0d7d5a7
AJ
3411#define GEN_STUF(name, stop, opc, type) \
3412GEN_HANDLER(name##u, opc, 0xFF, 0xFF, 0x00000000, type) \
79aceca5 3413{ \
a0d7d5a7 3414 TCGv EA; \
76a66253 3415 if (unlikely(!ctx->fpu_enabled)) { \
e1833e1f 3416 GEN_EXCP_NO_FP(ctx); \
4ecc3190
FB
3417 return; \
3418 } \
76a66253 3419 if (unlikely(rA(ctx->opcode) == 0)) { \
e1833e1f 3420 GEN_EXCP_INVAL(ctx); \
9fddaa0c 3421 return; \
9a64fbe4 3422 } \
a7859e89 3423 gen_set_access_type(ACCESS_FLOAT); \
a0d7d5a7
AJ
3424 EA = tcg_temp_new(); \
3425 gen_addr_imm_index(EA, ctx, 0); \
3426 gen_qemu_##stop(cpu_fpr[rS(ctx->opcode)], EA, ctx->mem_idx); \
3427 tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], EA); \
3428 tcg_temp_free(EA); \
79aceca5
FB
3429}
3430
a0d7d5a7
AJ
3431#define GEN_STUXF(name, stop, opc, type) \
3432GEN_HANDLER(name##ux, 0x1F, 0x17, opc, 0x00000001, type) \
79aceca5 3433{ \
a0d7d5a7 3434 TCGv EA; \
76a66253 3435 if (unlikely(!ctx->fpu_enabled)) { \
e1833e1f 3436 GEN_EXCP_NO_FP(ctx); \
4ecc3190
FB
3437 return; \
3438 } \
76a66253 3439 if (unlikely(rA(ctx->opcode) == 0)) { \
e1833e1f 3440 GEN_EXCP_INVAL(ctx); \
9fddaa0c 3441 return; \
9a64fbe4 3442 } \
a7859e89 3443 gen_set_access_type(ACCESS_FLOAT); \
a0d7d5a7
AJ
3444 EA = tcg_temp_new(); \
3445 gen_addr_reg_index(EA, ctx); \
3446 gen_qemu_##stop(cpu_fpr[rS(ctx->opcode)], EA, ctx->mem_idx); \
3447 tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], EA); \
3448 tcg_temp_free(EA); \
79aceca5
FB
3449}
3450
a0d7d5a7
AJ
3451#define GEN_STXF(name, stop, opc2, opc3, type) \
3452GEN_HANDLER(name##x, 0x1F, opc2, opc3, 0x00000001, type) \
79aceca5 3453{ \
a0d7d5a7 3454 TCGv EA; \
76a66253 3455 if (unlikely(!ctx->fpu_enabled)) { \
e1833e1f 3456 GEN_EXCP_NO_FP(ctx); \
4ecc3190
FB
3457 return; \
3458 } \
a7859e89 3459 gen_set_access_type(ACCESS_FLOAT); \
a0d7d5a7
AJ
3460 EA = tcg_temp_new(); \
3461 gen_addr_reg_index(EA, ctx); \
3462 gen_qemu_##stop(cpu_fpr[rS(ctx->opcode)], EA, ctx->mem_idx); \
3463 tcg_temp_free(EA); \
79aceca5
FB
3464}
3465
a0d7d5a7
AJ
3466#define GEN_STFS(name, stop, op, type) \
3467GEN_STF(name, stop, op | 0x20, type); \
3468GEN_STUF(name, stop, op | 0x21, type); \
3469GEN_STUXF(name, stop, op | 0x01, type); \
3470GEN_STXF(name, stop, 0x17, op | 0x00, type)
3471
3472static always_inline void gen_qemu_st32fs(TCGv_i64 arg1, TCGv arg2, int flags)
3473{
3474 TCGv_i32 t0 = tcg_temp_new_i32();
3475 TCGv t1 = tcg_temp_new();
3476 gen_helper_float64_to_float32(t0, arg1);
3477 tcg_gen_extu_i32_tl(t1, t0);
3478 tcg_temp_free_i32(t0);
3479 gen_qemu_st32(t1, arg2, flags);
3480 tcg_temp_free(t1);
3481}
79aceca5
FB
3482
3483/* stfd stfdu stfdux stfdx */
a0d7d5a7 3484GEN_STFS(stfd, st64, 0x16, PPC_FLOAT);
79aceca5 3485/* stfs stfsu stfsux stfsx */
a0d7d5a7 3486GEN_STFS(stfs, st32fs, 0x14, PPC_FLOAT);
79aceca5
FB
3487
3488/* Optional: */
a0d7d5a7
AJ
3489static always_inline void gen_qemu_st32fiw(TCGv_i64 arg1, TCGv arg2, int flags)
3490{
3491 TCGv t0 = tcg_temp_new();
3492 tcg_gen_trunc_i64_tl(t0, arg1),
3493 gen_qemu_st32(t0, arg2, flags);
3494 tcg_temp_free(t0);
3495}
79aceca5 3496/* stfiwx */
a0d7d5a7 3497GEN_STXF(stfiw, st32fiw, 0x17, 0x1E, PPC_FLOAT_STFIWX);
79aceca5
FB
3498
3499/*** Branch ***/
b068d6a7
JM
3500static always_inline void gen_goto_tb (DisasContext *ctx, int n,
3501 target_ulong dest)
c1942362
FB
3502{
3503 TranslationBlock *tb;
3504 tb = ctx->tb;
a2ffb812
AJ
3505#if defined(TARGET_PPC64)
3506 if (!ctx->sf_mode)
3507 dest = (uint32_t) dest;
3508#endif
57fec1fe 3509 if ((tb->pc & TARGET_PAGE_MASK) == (dest & TARGET_PAGE_MASK) &&
8cbcb4fa 3510 likely(!ctx->singlestep_enabled)) {
57fec1fe 3511 tcg_gen_goto_tb(n);
a2ffb812 3512 tcg_gen_movi_tl(cpu_nip, dest & ~3);
57fec1fe 3513 tcg_gen_exit_tb((long)tb + n);
c1942362 3514 } else {
a2ffb812 3515 tcg_gen_movi_tl(cpu_nip, dest & ~3);
8cbcb4fa
AJ
3516 if (unlikely(ctx->singlestep_enabled)) {
3517 if ((ctx->singlestep_enabled &
bdc4e053 3518 (CPU_BRANCH_STEP | CPU_SINGLE_STEP)) &&
8cbcb4fa
AJ
3519 ctx->exception == POWERPC_EXCP_BRANCH) {
3520 target_ulong tmp = ctx->nip;
3521 ctx->nip = dest;
3522 GEN_EXCP(ctx, POWERPC_EXCP_TRACE, 0);
3523 ctx->nip = tmp;
3524 }
3525 if (ctx->singlestep_enabled & GDBSTUB_SINGLE_STEP) {
3526 gen_update_nip(ctx, dest);
64adab3f 3527 gen_helper_raise_debug();
8cbcb4fa
AJ
3528 }
3529 }
57fec1fe 3530 tcg_gen_exit_tb(0);
c1942362 3531 }
c53be334
FB
3532}
3533
b068d6a7 3534static always_inline void gen_setlr (DisasContext *ctx, target_ulong nip)
e1833e1f
JM
3535{
3536#if defined(TARGET_PPC64)
a2ffb812
AJ
3537 if (ctx->sf_mode == 0)
3538 tcg_gen_movi_tl(cpu_lr, (uint32_t)nip);
e1833e1f
JM
3539 else
3540#endif
a2ffb812 3541 tcg_gen_movi_tl(cpu_lr, nip);
e1833e1f
JM
3542}
3543
79aceca5
FB
3544/* b ba bl bla */
3545GEN_HANDLER(b, 0x12, 0xFF, 0xFF, 0x00000000, PPC_FLOW)
3546{
76a66253 3547 target_ulong li, target;
38a64f9d 3548
8cbcb4fa 3549 ctx->exception = POWERPC_EXCP_BRANCH;
38a64f9d 3550 /* sign extend LI */
76a66253 3551#if defined(TARGET_PPC64)
d9bce9d9
JM
3552 if (ctx->sf_mode)
3553 li = ((int64_t)LI(ctx->opcode) << 38) >> 38;
3554 else
76a66253 3555#endif
d9bce9d9 3556 li = ((int32_t)LI(ctx->opcode) << 6) >> 6;
76a66253 3557 if (likely(AA(ctx->opcode) == 0))
046d6672 3558 target = ctx->nip + li - 4;
79aceca5 3559 else
9a64fbe4 3560 target = li;
e1833e1f
JM
3561 if (LK(ctx->opcode))
3562 gen_setlr(ctx, ctx->nip);
c1942362 3563 gen_goto_tb(ctx, 0, target);
79aceca5
FB
3564}
3565
e98a6e40
FB
3566#define BCOND_IM 0
3567#define BCOND_LR 1
3568#define BCOND_CTR 2
3569
b068d6a7 3570static always_inline void gen_bcond (DisasContext *ctx, int type)
d9bce9d9 3571{
d9bce9d9 3572 uint32_t bo = BO(ctx->opcode);
a2ffb812
AJ
3573 int l1 = gen_new_label();
3574 TCGv target;
e98a6e40 3575
8cbcb4fa 3576 ctx->exception = POWERPC_EXCP_BRANCH;
a2ffb812 3577 if (type == BCOND_LR || type == BCOND_CTR) {
a7812ae4 3578 target = tcg_temp_local_new();
a2ffb812
AJ
3579 if (type == BCOND_CTR)
3580 tcg_gen_mov_tl(target, cpu_ctr);
3581 else
3582 tcg_gen_mov_tl(target, cpu_lr);
e98a6e40 3583 }
e1833e1f
JM
3584 if (LK(ctx->opcode))
3585 gen_setlr(ctx, ctx->nip);
a2ffb812
AJ
3586 l1 = gen_new_label();
3587 if ((bo & 0x4) == 0) {
3588 /* Decrement and test CTR */
a7812ae4 3589 TCGv temp = tcg_temp_new();
a2ffb812
AJ
3590 if (unlikely(type == BCOND_CTR)) {
3591 GEN_EXCP_INVAL(ctx);
3592 return;
3593 }
3594 tcg_gen_subi_tl(cpu_ctr, cpu_ctr, 1);
d9bce9d9 3595#if defined(TARGET_PPC64)
a2ffb812
AJ
3596 if (!ctx->sf_mode)
3597 tcg_gen_ext32u_tl(temp, cpu_ctr);
3598 else
d9bce9d9 3599#endif
a2ffb812
AJ
3600 tcg_gen_mov_tl(temp, cpu_ctr);
3601 if (bo & 0x2) {
3602 tcg_gen_brcondi_tl(TCG_COND_NE, temp, 0, l1);
3603 } else {
3604 tcg_gen_brcondi_tl(TCG_COND_EQ, temp, 0, l1);
e98a6e40 3605 }
a7812ae4 3606 tcg_temp_free(temp);
a2ffb812
AJ
3607 }
3608 if ((bo & 0x10) == 0) {
3609 /* Test CR */
3610 uint32_t bi = BI(ctx->opcode);
3611 uint32_t mask = 1 << (3 - (bi & 0x03));
a7812ae4 3612 TCGv_i32 temp = tcg_temp_new_i32();
a2ffb812 3613
d9bce9d9 3614 if (bo & 0x8) {
a2ffb812
AJ
3615 tcg_gen_andi_i32(temp, cpu_crf[bi >> 2], mask);
3616 tcg_gen_brcondi_i32(TCG_COND_EQ, temp, 0, l1);
d9bce9d9 3617 } else {
a2ffb812
AJ
3618 tcg_gen_andi_i32(temp, cpu_crf[bi >> 2], mask);
3619 tcg_gen_brcondi_i32(TCG_COND_NE, temp, 0, l1);
d9bce9d9 3620 }
a7812ae4 3621 tcg_temp_free_i32(temp);
d9bce9d9 3622 }
e98a6e40 3623 if (type == BCOND_IM) {
a2ffb812
AJ
3624 target_ulong li = (target_long)((int16_t)(BD(ctx->opcode)));
3625 if (likely(AA(ctx->opcode) == 0)) {
3626 gen_goto_tb(ctx, 0, ctx->nip + li - 4);
3627 } else {
3628 gen_goto_tb(ctx, 0, li);
3629 }
c53be334 3630 gen_set_label(l1);
c1942362 3631 gen_goto_tb(ctx, 1, ctx->nip);
e98a6e40 3632 } else {
d9bce9d9 3633#if defined(TARGET_PPC64)
a2ffb812
AJ
3634 if (!(ctx->sf_mode))
3635 tcg_gen_andi_tl(cpu_nip, target, (uint32_t)~3);
3636 else
3637#endif
3638 tcg_gen_andi_tl(cpu_nip, target, ~3);
3639 tcg_gen_exit_tb(0);
3640 gen_set_label(l1);
3641#if defined(TARGET_PPC64)
3642 if (!(ctx->sf_mode))
3643 tcg_gen_movi_tl(cpu_nip, (uint32_t)ctx->nip);
d9bce9d9
JM
3644 else
3645#endif
a2ffb812 3646 tcg_gen_movi_tl(cpu_nip, ctx->nip);
57fec1fe 3647 tcg_gen_exit_tb(0);
08e46e54 3648 }
e98a6e40
FB
3649}
3650
3651GEN_HANDLER(bc, 0x10, 0xFF, 0xFF, 0x00000000, PPC_FLOW)
3b46e624 3652{
e98a6e40
FB
3653 gen_bcond(ctx, BCOND_IM);
3654}
3655
3656GEN_HANDLER(bcctr, 0x13, 0x10, 0x10, 0x00000000, PPC_FLOW)
3b46e624 3657{
e98a6e40
FB
3658 gen_bcond(ctx, BCOND_CTR);
3659}
3660
3661GEN_HANDLER(bclr, 0x13, 0x10, 0x00, 0x00000000, PPC_FLOW)
3b46e624 3662{
e98a6e40
FB
3663 gen_bcond(ctx, BCOND_LR);
3664}
79aceca5
FB
3665
3666/*** Condition register logical ***/
e1571908
AJ
3667#define GEN_CRLOGIC(name, tcg_op, opc) \
3668GEN_HANDLER(name, 0x13, 0x01, opc, 0x00000001, PPC_INTEGER) \
79aceca5 3669{ \
fc0d441e
JM
3670 uint8_t bitmask; \
3671 int sh; \
a7812ae4 3672 TCGv_i32 t0, t1; \
fc0d441e 3673 sh = (crbD(ctx->opcode) & 0x03) - (crbA(ctx->opcode) & 0x03); \
a7812ae4 3674 t0 = tcg_temp_new_i32(); \
fc0d441e 3675 if (sh > 0) \
fea0c503 3676 tcg_gen_shri_i32(t0, cpu_crf[crbA(ctx->opcode) >> 2], sh); \
fc0d441e 3677 else if (sh < 0) \
fea0c503 3678 tcg_gen_shli_i32(t0, cpu_crf[crbA(ctx->opcode) >> 2], -sh); \
e1571908 3679 else \
fea0c503 3680 tcg_gen_mov_i32(t0, cpu_crf[crbA(ctx->opcode) >> 2]); \
a7812ae4 3681 t1 = tcg_temp_new_i32(); \
fc0d441e
JM
3682 sh = (crbD(ctx->opcode) & 0x03) - (crbB(ctx->opcode) & 0x03); \
3683 if (sh > 0) \
fea0c503 3684 tcg_gen_shri_i32(t1, cpu_crf[crbB(ctx->opcode) >> 2], sh); \
fc0d441e 3685 else if (sh < 0) \
fea0c503 3686 tcg_gen_shli_i32(t1, cpu_crf[crbB(ctx->opcode) >> 2], -sh); \
e1571908 3687 else \
fea0c503
AJ
3688 tcg_gen_mov_i32(t1, cpu_crf[crbB(ctx->opcode) >> 2]); \
3689 tcg_op(t0, t0, t1); \
fc0d441e 3690 bitmask = 1 << (3 - (crbD(ctx->opcode) & 0x03)); \
fea0c503
AJ
3691 tcg_gen_andi_i32(t0, t0, bitmask); \
3692 tcg_gen_andi_i32(t1, cpu_crf[crbD(ctx->opcode) >> 2], ~bitmask); \
3693 tcg_gen_or_i32(cpu_crf[crbD(ctx->opcode) >> 2], t0, t1); \
a7812ae4
PB
3694 tcg_temp_free_i32(t0); \
3695 tcg_temp_free_i32(t1); \
79aceca5
FB
3696}
3697
3698/* crand */
e1571908 3699GEN_CRLOGIC(crand, tcg_gen_and_i32, 0x08);
79aceca5 3700/* crandc */
e1571908 3701GEN_CRLOGIC(crandc, tcg_gen_andc_i32, 0x04);
79aceca5 3702/* creqv */
e1571908 3703GEN_CRLOGIC(creqv, tcg_gen_eqv_i32, 0x09);
79aceca5 3704/* crnand */
e1571908 3705GEN_CRLOGIC(crnand, tcg_gen_nand_i32, 0x07);
79aceca5 3706/* crnor */
e1571908 3707GEN_CRLOGIC(crnor, tcg_gen_nor_i32, 0x01);
79aceca5 3708/* cror */
e1571908 3709GEN_CRLOGIC(cror, tcg_gen_or_i32, 0x0E);
79aceca5 3710/* crorc */
e1571908 3711GEN_CRLOGIC(crorc, tcg_gen_orc_i32, 0x0D);
79aceca5 3712/* crxor */
e1571908 3713GEN_CRLOGIC(crxor, tcg_gen_xor_i32, 0x06);
79aceca5
FB
3714/* mcrf */
3715GEN_HANDLER(mcrf, 0x13, 0x00, 0xFF, 0x00000001, PPC_INTEGER)
3716{
47e4661c 3717 tcg_gen_mov_i32(cpu_crf[crfD(ctx->opcode)], cpu_crf[crfS(ctx->opcode)]);
79aceca5
FB
3718}
3719
3720/*** System linkage ***/
3721/* rfi (supervisor only) */
76a66253 3722GEN_HANDLER(rfi, 0x13, 0x12, 0x01, 0x03FF8001, PPC_FLOW)
79aceca5 3723{
9a64fbe4 3724#if defined(CONFIG_USER_ONLY)
e1833e1f 3725 GEN_EXCP_PRIVOPC(ctx);
9a64fbe4
FB
3726#else
3727 /* Restore CPU state */
76a66253 3728 if (unlikely(!ctx->supervisor)) {
e1833e1f 3729 GEN_EXCP_PRIVOPC(ctx);
9fddaa0c 3730 return;
9a64fbe4 3731 }
a42bd6cc 3732 gen_op_rfi();
e1833e1f 3733 GEN_SYNC(ctx);
9a64fbe4 3734#endif
79aceca5
FB
3735}
3736
426613db 3737#if defined(TARGET_PPC64)
a750fc0b 3738GEN_HANDLER(rfid, 0x13, 0x12, 0x00, 0x03FF8001, PPC_64B)
426613db
JM
3739{
3740#if defined(CONFIG_USER_ONLY)
e1833e1f 3741 GEN_EXCP_PRIVOPC(ctx);
426613db
JM
3742#else
3743 /* Restore CPU state */
3744 if (unlikely(!ctx->supervisor)) {
e1833e1f 3745 GEN_EXCP_PRIVOPC(ctx);
426613db
JM
3746 return;
3747 }
a42bd6cc 3748 gen_op_rfid();
e1833e1f 3749 GEN_SYNC(ctx);
426613db
JM
3750#endif
3751}
426613db 3752
5b8105fa 3753GEN_HANDLER(hrfid, 0x13, 0x12, 0x08, 0x03FF8001, PPC_64H)
be147d08
JM
3754{
3755#if defined(CONFIG_USER_ONLY)
3756 GEN_EXCP_PRIVOPC(ctx);
3757#else
3758 /* Restore CPU state */
3759 if (unlikely(ctx->supervisor <= 1)) {
3760 GEN_EXCP_PRIVOPC(ctx);
3761 return;
3762 }
3763 gen_op_hrfid();
3764 GEN_SYNC(ctx);
3765#endif
3766}
3767#endif
3768
79aceca5 3769/* sc */
417bf010
JM
3770#if defined(CONFIG_USER_ONLY)
3771#define POWERPC_SYSCALL POWERPC_EXCP_SYSCALL_USER
3772#else
3773#define POWERPC_SYSCALL POWERPC_EXCP_SYSCALL
3774#endif
e1833e1f 3775GEN_HANDLER(sc, 0x11, 0xFF, 0xFF, 0x03FFF01D, PPC_FLOW)
79aceca5 3776{
e1833e1f
JM
3777 uint32_t lev;
3778
3779 lev = (ctx->opcode >> 5) & 0x7F;
417bf010 3780 GEN_EXCP(ctx, POWERPC_SYSCALL, lev);
79aceca5
FB
3781}
3782
3783/*** Trap ***/
3784/* tw */
76a66253 3785GEN_HANDLER(tw, 0x1F, 0x04, 0x00, 0x00000001, PPC_FLOW)
79aceca5 3786{
cab3bee2 3787 TCGv_i32 t0 = tcg_const_i32(TO(ctx->opcode));
a0ae05aa 3788 /* Update the nip since this might generate a trap exception */
d9bce9d9 3789 gen_update_nip(ctx, ctx->nip);
cab3bee2
AJ
3790 gen_helper_tw(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rB(ctx->opcode)], t0);
3791 tcg_temp_free_i32(t0);
79aceca5
FB
3792}
3793
3794/* twi */
3795GEN_HANDLER(twi, 0x03, 0xFF, 0xFF, 0x00000000, PPC_FLOW)
3796{
cab3bee2
AJ
3797 TCGv t0 = tcg_const_tl(SIMM(ctx->opcode));
3798 TCGv_i32 t1 = tcg_const_i32(TO(ctx->opcode));
d9bce9d9
JM
3799 /* Update the nip since this might generate a trap exception */
3800 gen_update_nip(ctx, ctx->nip);
cab3bee2
AJ
3801 gen_helper_tw(cpu_gpr[rA(ctx->opcode)], t0, t1);
3802 tcg_temp_free(t0);
3803 tcg_temp_free_i32(t1);
79aceca5
FB
3804}
3805
d9bce9d9
JM
3806#if defined(TARGET_PPC64)
3807/* td */
3808GEN_HANDLER(td, 0x1F, 0x04, 0x02, 0x00000001, PPC_64B)
3809{
cab3bee2 3810 TCGv_i32 t0 = tcg_const_i32(TO(ctx->opcode));
d9bce9d9
JM
3811 /* Update the nip since this might generate a trap exception */
3812 gen_update_nip(ctx, ctx->nip);
cab3bee2
AJ
3813 gen_helper_td(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rB(ctx->opcode)], t0);
3814 tcg_temp_free_i32(t0);
d9bce9d9
JM
3815}
3816
3817/* tdi */
3818GEN_HANDLER(tdi, 0x02, 0xFF, 0xFF, 0x00000000, PPC_64B)
3819{
cab3bee2
AJ
3820 TCGv t0 = tcg_const_tl(SIMM(ctx->opcode));
3821 TCGv_i32 t1 = tcg_const_i32(TO(ctx->opcode));
d9bce9d9
JM
3822 /* Update the nip since this might generate a trap exception */
3823 gen_update_nip(ctx, ctx->nip);
cab3bee2
AJ
3824 gen_helper_td(cpu_gpr[rA(ctx->opcode)], t0, t1);
3825 tcg_temp_free(t0);
3826 tcg_temp_free_i32(t1);
d9bce9d9
JM
3827}
3828#endif
3829
79aceca5 3830/*** Processor control ***/
79aceca5
FB
3831/* mcrxr */
3832GEN_HANDLER(mcrxr, 0x1F, 0x00, 0x10, 0x007FF801, PPC_MISC)
3833{
3d7b417e
AJ
3834 tcg_gen_trunc_tl_i32(cpu_crf[crfD(ctx->opcode)], cpu_xer);
3835 tcg_gen_shri_i32(cpu_crf[crfD(ctx->opcode)], cpu_crf[crfD(ctx->opcode)], XER_CA);
269f3e95 3836 tcg_gen_andi_tl(cpu_xer, cpu_xer, ~(1 << XER_SO | 1 << XER_OV | 1 << XER_CA));
79aceca5
FB
3837}
3838
3839/* mfcr */
76a66253 3840GEN_HANDLER(mfcr, 0x1F, 0x13, 0x00, 0x00000801, PPC_MISC)
79aceca5 3841{
76a66253 3842 uint32_t crm, crn;
3b46e624 3843
76a66253
JM
3844 if (likely(ctx->opcode & 0x00100000)) {
3845 crm = CRM(ctx->opcode);
3846 if (likely((crm ^ (crm - 1)) == 0)) {
3847 crn = ffs(crm);
e1571908 3848 tcg_gen_extu_i32_tl(cpu_gpr[rD(ctx->opcode)], cpu_crf[7 - crn]);
76a66253 3849 }
d9bce9d9 3850 } else {
a7812ae4 3851 gen_helper_load_cr(cpu_gpr[rD(ctx->opcode)]);
d9bce9d9 3852 }
79aceca5
FB
3853}
3854
3855/* mfmsr */
3856GEN_HANDLER(mfmsr, 0x1F, 0x13, 0x02, 0x001FF801, PPC_MISC)
3857{
9a64fbe4 3858#if defined(CONFIG_USER_ONLY)
e1833e1f 3859 GEN_EXCP_PRIVREG(ctx);
9a64fbe4 3860#else
76a66253 3861 if (unlikely(!ctx->supervisor)) {
e1833e1f 3862 GEN_EXCP_PRIVREG(ctx);
9fddaa0c 3863 return;
9a64fbe4 3864 }
6676f424 3865 gen_op_load_msr();
f78fb44e 3866 tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_T[0]);
9a64fbe4 3867#endif
79aceca5
FB
3868}
3869
a11b8151 3870#if 1
6f2d8978 3871#define SPR_NOACCESS ((void *)(-1UL))
3fc6c082
FB
3872#else
3873static void spr_noaccess (void *opaque, int sprn)
3874{
3875 sprn = ((sprn >> 5) & 0x1F) | ((sprn & 0x1F) << 5);
3876 printf("ERROR: try to access SPR %d !\n", sprn);
3877}
3878#define SPR_NOACCESS (&spr_noaccess)
3879#endif
3880
79aceca5 3881/* mfspr */
b068d6a7 3882static always_inline void gen_op_mfspr (DisasContext *ctx)
79aceca5 3883{
3fc6c082 3884 void (*read_cb)(void *opaque, int sprn);
79aceca5
FB
3885 uint32_t sprn = SPR(ctx->opcode);
3886
3fc6c082 3887#if !defined(CONFIG_USER_ONLY)
be147d08
JM
3888 if (ctx->supervisor == 2)
3889 read_cb = ctx->spr_cb[sprn].hea_read;
7863667f 3890 else if (ctx->supervisor)
3fc6c082
FB
3891 read_cb = ctx->spr_cb[sprn].oea_read;
3892 else
9a64fbe4 3893#endif
3fc6c082 3894 read_cb = ctx->spr_cb[sprn].uea_read;
76a66253
JM
3895 if (likely(read_cb != NULL)) {
3896 if (likely(read_cb != SPR_NOACCESS)) {
3fc6c082 3897 (*read_cb)(ctx, sprn);
f78fb44e 3898 tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_T[0]);
3fc6c082
FB
3899 } else {
3900 /* Privilege exception */
9fceefa7
JM
3901 /* This is a hack to avoid warnings when running Linux:
3902 * this OS breaks the PowerPC virtualisation model,
3903 * allowing userland application to read the PVR
3904 */
3905 if (sprn != SPR_PVR) {
3906 if (loglevel != 0) {
6b542af7 3907 fprintf(logfile, "Trying to read privileged spr %d %03x at "
077fc206 3908 ADDRX "\n", sprn, sprn, ctx->nip);
9fceefa7 3909 }
077fc206
JM
3910 printf("Trying to read privileged spr %d %03x at " ADDRX "\n",
3911 sprn, sprn, ctx->nip);
f24e5695 3912 }
e1833e1f 3913 GEN_EXCP_PRIVREG(ctx);
79aceca5 3914 }
3fc6c082
FB
3915 } else {
3916 /* Not defined */
4a057712 3917 if (loglevel != 0) {
077fc206
JM
3918 fprintf(logfile, "Trying to read invalid spr %d %03x at "
3919 ADDRX "\n", sprn, sprn, ctx->nip);
f24e5695 3920 }
077fc206
JM
3921 printf("Trying to read invalid spr %d %03x at " ADDRX "\n",
3922 sprn, sprn, ctx->nip);
e1833e1f
JM
3923 GEN_EXCP(ctx, POWERPC_EXCP_PROGRAM,
3924 POWERPC_EXCP_INVAL | POWERPC_EXCP_INVAL_SPR);
79aceca5 3925 }
79aceca5
FB
3926}
3927
3fc6c082 3928GEN_HANDLER(mfspr, 0x1F, 0x13, 0x0A, 0x00000001, PPC_MISC)
79aceca5 3929{
3fc6c082 3930 gen_op_mfspr(ctx);
76a66253 3931}
3fc6c082
FB
3932
3933/* mftb */
a750fc0b 3934GEN_HANDLER(mftb, 0x1F, 0x13, 0x0B, 0x00000001, PPC_MFTB)
3fc6c082
FB
3935{
3936 gen_op_mfspr(ctx);
79aceca5
FB
3937}
3938
3939/* mtcrf */
8dd4983c 3940GEN_HANDLER(mtcrf, 0x1F, 0x10, 0x04, 0x00000801, PPC_MISC)
79aceca5 3941{
76a66253 3942 uint32_t crm, crn;
3b46e624 3943
76a66253
JM
3944 crm = CRM(ctx->opcode);
3945 if (likely((ctx->opcode & 0x00100000) || (crm ^ (crm - 1)) == 0)) {
a7812ae4 3946 TCGv_i32 temp = tcg_temp_new_i32();
76a66253 3947 crn = ffs(crm);
a7812ae4
PB
3948 tcg_gen_trunc_tl_i32(temp, cpu_gpr[rS(ctx->opcode)]);
3949 tcg_gen_shri_i32(cpu_crf[7 - crn], temp, crn * 4);
e1571908 3950 tcg_gen_andi_i32(cpu_crf[7 - crn], cpu_crf[7 - crn], 0xf);
a7812ae4 3951 tcg_temp_free_i32(temp);
76a66253 3952 } else {
a7812ae4
PB
3953 TCGv_i32 temp = tcg_const_i32(crm);
3954 gen_helper_store_cr(cpu_gpr[rS(ctx->opcode)], temp);
3955 tcg_temp_free_i32(temp);
76a66253 3956 }
79aceca5
FB
3957}
3958
3959/* mtmsr */
426613db 3960#if defined(TARGET_PPC64)
be147d08 3961GEN_HANDLER(mtmsrd, 0x1F, 0x12, 0x05, 0x001EF801, PPC_64B)
426613db
JM
3962{
3963#if defined(CONFIG_USER_ONLY)
e1833e1f 3964 GEN_EXCP_PRIVREG(ctx);
426613db
JM
3965#else
3966 if (unlikely(!ctx->supervisor)) {
e1833e1f 3967 GEN_EXCP_PRIVREG(ctx);
426613db
JM
3968 return;
3969 }
f78fb44e 3970 tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rS(ctx->opcode)]);
be147d08
JM
3971 if (ctx->opcode & 0x00010000) {
3972 /* Special form that does not need any synchronisation */
3973 gen_op_update_riee();
3974 } else {
056b05f8
JM
3975 /* XXX: we need to update nip before the store
3976 * if we enter power saving mode, we will exit the loop
3977 * directly from ppc_store_msr
3978 */
be147d08 3979 gen_update_nip(ctx, ctx->nip);
6676f424 3980 gen_op_store_msr();
be147d08
JM
3981 /* Must stop the translation as machine state (may have) changed */
3982 /* Note that mtmsr is not always defined as context-synchronizing */
056b05f8 3983 ctx->exception = POWERPC_EXCP_STOP;
be147d08 3984 }
426613db
JM
3985#endif
3986}
3987#endif
3988
79aceca5
FB
3989GEN_HANDLER(mtmsr, 0x1F, 0x12, 0x04, 0x001FF801, PPC_MISC)
3990{
9a64fbe4 3991#if defined(CONFIG_USER_ONLY)
e1833e1f 3992 GEN_EXCP_PRIVREG(ctx);
9a64fbe4 3993#else
76a66253 3994 if (unlikely(!ctx->supervisor)) {
e1833e1f 3995 GEN_EXCP_PRIVREG(ctx);
9fddaa0c 3996 return;
9a64fbe4 3997 }
f78fb44e 3998 tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rS(ctx->opcode)]);
be147d08
JM
3999 if (ctx->opcode & 0x00010000) {
4000 /* Special form that does not need any synchronisation */
4001 gen_op_update_riee();
4002 } else {
056b05f8
JM
4003 /* XXX: we need to update nip before the store
4004 * if we enter power saving mode, we will exit the loop
4005 * directly from ppc_store_msr
4006 */
be147d08 4007 gen_update_nip(ctx, ctx->nip);
d9bce9d9 4008#if defined(TARGET_PPC64)
be147d08 4009 if (!ctx->sf_mode)
6676f424 4010 gen_op_store_msr_32();
be147d08 4011 else
d9bce9d9 4012#endif
6676f424 4013 gen_op_store_msr();
be147d08
JM
4014 /* Must stop the translation as machine state (may have) changed */
4015 /* Note that mtmsrd is not always defined as context-synchronizing */
056b05f8 4016 ctx->exception = POWERPC_EXCP_STOP;
be147d08 4017 }
9a64fbe4 4018#endif
79aceca5
FB
4019}
4020
4021/* mtspr */
4022GEN_HANDLER(mtspr, 0x1F, 0x13, 0x0E, 0x00000001, PPC_MISC)
4023{
3fc6c082 4024 void (*write_cb)(void *opaque, int sprn);
79aceca5
FB
4025 uint32_t sprn = SPR(ctx->opcode);
4026
3fc6c082 4027#if !defined(CONFIG_USER_ONLY)
be147d08
JM
4028 if (ctx->supervisor == 2)
4029 write_cb = ctx->spr_cb[sprn].hea_write;
7863667f 4030 else if (ctx->supervisor)
3fc6c082
FB
4031 write_cb = ctx->spr_cb[sprn].oea_write;
4032 else
9a64fbe4 4033#endif
3fc6c082 4034 write_cb = ctx->spr_cb[sprn].uea_write;
76a66253
JM
4035 if (likely(write_cb != NULL)) {
4036 if (likely(write_cb != SPR_NOACCESS)) {
f78fb44e 4037 tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rS(ctx->opcode)]);
3fc6c082
FB
4038 (*write_cb)(ctx, sprn);
4039 } else {
4040 /* Privilege exception */
4a057712 4041 if (loglevel != 0) {
077fc206
JM
4042 fprintf(logfile, "Trying to write privileged spr %d %03x at "
4043 ADDRX "\n", sprn, sprn, ctx->nip);
f24e5695 4044 }
077fc206
JM
4045 printf("Trying to write privileged spr %d %03x at " ADDRX "\n",
4046 sprn, sprn, ctx->nip);
e1833e1f 4047 GEN_EXCP_PRIVREG(ctx);
76a66253 4048 }
3fc6c082
FB
4049 } else {
4050 /* Not defined */
4a057712 4051 if (loglevel != 0) {
077fc206
JM
4052 fprintf(logfile, "Trying to write invalid spr %d %03x at "
4053 ADDRX "\n", sprn, sprn, ctx->nip);
f24e5695 4054 }
077fc206
JM
4055 printf("Trying to write invalid spr %d %03x at " ADDRX "\n",
4056 sprn, sprn, ctx->nip);
e1833e1f
JM
4057 GEN_EXCP(ctx, POWERPC_EXCP_PROGRAM,
4058 POWERPC_EXCP_INVAL | POWERPC_EXCP_INVAL_SPR);
79aceca5 4059 }
79aceca5
FB
4060}
4061
4062/*** Cache management ***/
79aceca5 4063/* dcbf */
0db1b20e 4064GEN_HANDLER(dcbf, 0x1F, 0x16, 0x02, 0x03C00001, PPC_CACHE)
79aceca5 4065{
dac454af 4066 /* XXX: specification says this is treated as a load by the MMU */
a7812ae4 4067 TCGv t0 = tcg_temp_new();
a7859e89 4068 gen_set_access_type(ACCESS_CACHE);
fea0c503
AJ
4069 gen_addr_reg_index(t0, ctx);
4070 gen_qemu_ld8u(t0, t0, ctx->mem_idx);
4071 tcg_temp_free(t0);
79aceca5
FB
4072}
4073
4074/* dcbi (Supervisor only) */
9a64fbe4 4075GEN_HANDLER(dcbi, 0x1F, 0x16, 0x0E, 0x03E00001, PPC_CACHE)
79aceca5 4076{
a541f297 4077#if defined(CONFIG_USER_ONLY)
e1833e1f 4078 GEN_EXCP_PRIVOPC(ctx);
a541f297 4079#else
b61f2753 4080 TCGv EA, val;
76a66253 4081 if (unlikely(!ctx->supervisor)) {
e1833e1f 4082 GEN_EXCP_PRIVOPC(ctx);
9fddaa0c 4083 return;
9a64fbe4 4084 }
a7812ae4 4085 EA = tcg_temp_new();
a7859e89 4086 gen_set_access_type(ACCESS_CACHE);
b61f2753 4087 gen_addr_reg_index(EA, ctx);
a7812ae4 4088 val = tcg_temp_new();
76a66253 4089 /* XXX: specification says this should be treated as a store by the MMU */
b61f2753
AJ
4090 gen_qemu_ld8u(val, EA, ctx->mem_idx);
4091 gen_qemu_st8(val, EA, ctx->mem_idx);
4092 tcg_temp_free(val);
4093 tcg_temp_free(EA);
a541f297 4094#endif
79aceca5
FB
4095}
4096
4097/* dcdst */
9a64fbe4 4098GEN_HANDLER(dcbst, 0x1F, 0x16, 0x01, 0x03E00001, PPC_CACHE)
79aceca5 4099{
76a66253 4100 /* XXX: specification say this is treated as a load by the MMU */
a7812ae4 4101 TCGv t0 = tcg_temp_new();
a7859e89 4102 gen_set_access_type(ACCESS_CACHE);
fea0c503
AJ
4103 gen_addr_reg_index(t0, ctx);
4104 gen_qemu_ld8u(t0, t0, ctx->mem_idx);
4105 tcg_temp_free(t0);
79aceca5
FB
4106}
4107
4108/* dcbt */
0db1b20e 4109GEN_HANDLER(dcbt, 0x1F, 0x16, 0x08, 0x02000001, PPC_CACHE)
79aceca5 4110{
0db1b20e 4111 /* interpreted as no-op */
76a66253
JM
4112 /* XXX: specification say this is treated as a load by the MMU
4113 * but does not generate any exception
4114 */
79aceca5
FB
4115}
4116
4117/* dcbtst */
0db1b20e 4118GEN_HANDLER(dcbtst, 0x1F, 0x16, 0x07, 0x02000001, PPC_CACHE)
79aceca5 4119{
0db1b20e 4120 /* interpreted as no-op */
76a66253
JM
4121 /* XXX: specification say this is treated as a load by the MMU
4122 * but does not generate any exception
4123 */
79aceca5
FB
4124}
4125
4126/* dcbz */
d63001d1 4127GEN_HANDLER(dcbz, 0x1F, 0x16, 0x1F, 0x03E00001, PPC_CACHE_DCBZ)
79aceca5 4128{
799a8c8d
AJ
4129 TCGv t0 = tcg_temp_new();
4130 gen_addr_reg_index(t0, ctx);
4131 /* NIP cannot be restored if the memory exception comes from an helper */
4132 gen_update_nip(ctx, ctx->nip - 4);
4133 gen_helper_dcbz(t0);
4134 tcg_temp_free(t0);
d63001d1
JM
4135}
4136
c7697e1f 4137GEN_HANDLER2(dcbz_970, "dcbz", 0x1F, 0x16, 0x1F, 0x03C00001, PPC_CACHE_DCBZT)
d63001d1 4138{
799a8c8d
AJ
4139 TCGv t0 = tcg_temp_new();
4140 gen_addr_reg_index(t0, ctx);
4141 /* NIP cannot be restored if the memory exception comes from an helper */
4142 gen_update_nip(ctx, ctx->nip - 4);
d63001d1 4143 if (ctx->opcode & 0x00200000)
799a8c8d 4144 gen_helper_dcbz(t0);
d63001d1 4145 else
799a8c8d
AJ
4146 gen_helper_dcbz_970(t0);
4147 tcg_temp_free(t0);
79aceca5
FB
4148}
4149
4150/* icbi */
1b413d55 4151GEN_HANDLER(icbi, 0x1F, 0x16, 0x1E, 0x03E00001, PPC_CACHE_ICBI)
79aceca5 4152{
37d269df 4153 TCGv t0 = tcg_temp_new();
30032c94
JM
4154 /* NIP cannot be restored if the memory exception comes from an helper */
4155 gen_update_nip(ctx, ctx->nip - 4);
37d269df
AJ
4156 gen_addr_reg_index(t0, ctx);
4157 gen_helper_icbi(t0);
4158 tcg_temp_free(t0);
79aceca5
FB
4159}
4160
4161/* Optional: */
4162/* dcba */
a750fc0b 4163GEN_HANDLER(dcba, 0x1F, 0x16, 0x17, 0x03E00001, PPC_CACHE_DCBA)
79aceca5 4164{
0db1b20e
JM
4165 /* interpreted as no-op */
4166 /* XXX: specification say this is treated as a store by the MMU
4167 * but does not generate any exception
4168 */
79aceca5
FB
4169}
4170
4171/*** Segment register manipulation ***/
4172/* Supervisor only: */
4173/* mfsr */
4174GEN_HANDLER(mfsr, 0x1F, 0x13, 0x12, 0x0010F801, PPC_SEGMENT)
4175{
9a64fbe4 4176#if defined(CONFIG_USER_ONLY)
e1833e1f 4177 GEN_EXCP_PRIVREG(ctx);
9a64fbe4 4178#else
76a66253 4179 if (unlikely(!ctx->supervisor)) {
e1833e1f 4180 GEN_EXCP_PRIVREG(ctx);
9fddaa0c 4181 return;
9a64fbe4 4182 }
86c581dc 4183 tcg_gen_movi_tl(cpu_T[1], SR(ctx->opcode));
76a66253 4184 gen_op_load_sr();
f78fb44e 4185 tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_T[0]);
9a64fbe4 4186#endif
79aceca5
FB
4187}
4188
4189/* mfsrin */
9a64fbe4 4190GEN_HANDLER(mfsrin, 0x1F, 0x13, 0x14, 0x001F0001, PPC_SEGMENT)
79aceca5 4191{
9a64fbe4 4192#if defined(CONFIG_USER_ONLY)
e1833e1f 4193 GEN_EXCP_PRIVREG(ctx);
9a64fbe4 4194#else
76a66253 4195 if (unlikely(!ctx->supervisor)) {
e1833e1f 4196 GEN_EXCP_PRIVREG(ctx);
9fddaa0c 4197 return;
9a64fbe4 4198 }
f78fb44e 4199 tcg_gen_mov_tl(cpu_T[1], cpu_gpr[rB(ctx->opcode)]);
76a66253
JM
4200 gen_op_srli_T1(28);
4201 gen_op_load_sr();
f78fb44e 4202 tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_T[0]);
9a64fbe4 4203#endif
79aceca5
FB
4204}
4205
4206/* mtsr */
e63c59cb 4207GEN_HANDLER(mtsr, 0x1F, 0x12, 0x06, 0x0010F801, PPC_SEGMENT)
79aceca5 4208{
9a64fbe4 4209#if defined(CONFIG_USER_ONLY)
e1833e1f 4210 GEN_EXCP_PRIVREG(ctx);
9a64fbe4 4211#else
76a66253 4212 if (unlikely(!ctx->supervisor)) {
e1833e1f 4213 GEN_EXCP_PRIVREG(ctx);
9fddaa0c 4214 return;
9a64fbe4 4215 }
f78fb44e 4216 tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rS(ctx->opcode)]);
86c581dc 4217 tcg_gen_movi_tl(cpu_T[1], SR(ctx->opcode));
76a66253 4218 gen_op_store_sr();
9a64fbe4 4219#endif
79aceca5
FB
4220}
4221
4222/* mtsrin */
9a64fbe4 4223GEN_HANDLER(mtsrin, 0x1F, 0x12, 0x07, 0x001F0001, PPC_SEGMENT)
79aceca5 4224{
9a64fbe4 4225#if defined(CONFIG_USER_ONLY)
e1833e1f 4226 GEN_EXCP_PRIVREG(ctx);
9a64fbe4 4227#else
76a66253 4228 if (unlikely(!ctx->supervisor)) {
e1833e1f 4229 GEN_EXCP_PRIVREG(ctx);
9fddaa0c 4230 return;
9a64fbe4 4231 }
f78fb44e
AJ
4232 tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rS(ctx->opcode)]);
4233 tcg_gen_mov_tl(cpu_T[1], cpu_gpr[rB(ctx->opcode)]);
76a66253
JM
4234 gen_op_srli_T1(28);
4235 gen_op_store_sr();
9a64fbe4 4236#endif
79aceca5
FB
4237}
4238
12de9a39
JM
4239#if defined(TARGET_PPC64)
4240/* Specific implementation for PowerPC 64 "bridge" emulation using SLB */
4241/* mfsr */
c7697e1f 4242GEN_HANDLER2(mfsr_64b, "mfsr", 0x1F, 0x13, 0x12, 0x0010F801, PPC_SEGMENT_64B)
12de9a39
JM
4243{
4244#if defined(CONFIG_USER_ONLY)
4245 GEN_EXCP_PRIVREG(ctx);
4246#else
4247 if (unlikely(!ctx->supervisor)) {
4248 GEN_EXCP_PRIVREG(ctx);
4249 return;
4250 }
86c581dc 4251 tcg_gen_movi_tl(cpu_T[1], SR(ctx->opcode));
12de9a39 4252 gen_op_load_slb();
f78fb44e 4253 tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_T[0]);
12de9a39
JM
4254#endif
4255}
4256
4257/* mfsrin */
c7697e1f
JM
4258GEN_HANDLER2(mfsrin_64b, "mfsrin", 0x1F, 0x13, 0x14, 0x001F0001,
4259 PPC_SEGMENT_64B)
12de9a39
JM
4260{
4261#if defined(CONFIG_USER_ONLY)
4262 GEN_EXCP_PRIVREG(ctx);
4263#else
4264 if (unlikely(!ctx->supervisor)) {
4265 GEN_EXCP_PRIVREG(ctx);
4266 return;
4267 }
f78fb44e 4268 tcg_gen_mov_tl(cpu_T[1], cpu_gpr[rB(ctx->opcode)]);
12de9a39
JM
4269 gen_op_srli_T1(28);
4270 gen_op_load_slb();
f78fb44e 4271 tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_T[0]);
12de9a39
JM
4272#endif
4273}
4274
4275/* mtsr */
c7697e1f 4276GEN_HANDLER2(mtsr_64b, "mtsr", 0x1F, 0x12, 0x06, 0x0010F801, PPC_SEGMENT_64B)
12de9a39
JM
4277{
4278#if defined(CONFIG_USER_ONLY)
4279 GEN_EXCP_PRIVREG(ctx);
4280#else
4281 if (unlikely(!ctx->supervisor)) {
4282 GEN_EXCP_PRIVREG(ctx);
4283 return;
4284 }
f78fb44e 4285 tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rS(ctx->opcode)]);
86c581dc 4286 tcg_gen_movi_tl(cpu_T[1], SR(ctx->opcode));
12de9a39
JM
4287 gen_op_store_slb();
4288#endif
4289}
4290
4291/* mtsrin */
c7697e1f
JM
4292GEN_HANDLER2(mtsrin_64b, "mtsrin", 0x1F, 0x12, 0x07, 0x001F0001,
4293 PPC_SEGMENT_64B)
12de9a39
JM
4294{
4295#if defined(CONFIG_USER_ONLY)
4296 GEN_EXCP_PRIVREG(ctx);
4297#else
4298 if (unlikely(!ctx->supervisor)) {
4299 GEN_EXCP_PRIVREG(ctx);
4300 return;
4301 }
f78fb44e
AJ
4302 tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rS(ctx->opcode)]);
4303 tcg_gen_mov_tl(cpu_T[1], cpu_gpr[rB(ctx->opcode)]);
12de9a39
JM
4304 gen_op_srli_T1(28);
4305 gen_op_store_slb();
4306#endif
4307}
4308#endif /* defined(TARGET_PPC64) */
4309
79aceca5
FB
4310/*** Lookaside buffer management ***/
4311/* Optional & supervisor only: */
4312/* tlbia */
3fc6c082 4313GEN_HANDLER(tlbia, 0x1F, 0x12, 0x0B, 0x03FFFC01, PPC_MEM_TLBIA)
79aceca5 4314{
9a64fbe4 4315#if defined(CONFIG_USER_ONLY)
e1833e1f 4316 GEN_EXCP_PRIVOPC(ctx);
9a64fbe4 4317#else
76a66253 4318 if (unlikely(!ctx->supervisor)) {
e1833e1f 4319 GEN_EXCP_PRIVOPC(ctx);
9fddaa0c 4320 return;
9a64fbe4
FB
4321 }
4322 gen_op_tlbia();
4323#endif
79aceca5
FB
4324}
4325
4326/* tlbie */
76a66253 4327GEN_HANDLER(tlbie, 0x1F, 0x12, 0x09, 0x03FF0001, PPC_MEM_TLBIE)
79aceca5 4328{
9a64fbe4 4329#if defined(CONFIG_USER_ONLY)
e1833e1f 4330 GEN_EXCP_PRIVOPC(ctx);
9a64fbe4 4331#else
76a66253 4332 if (unlikely(!ctx->supervisor)) {
e1833e1f 4333 GEN_EXCP_PRIVOPC(ctx);
9fddaa0c 4334 return;
9a64fbe4 4335 }
f78fb44e 4336 tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rB(ctx->opcode)]);
d9bce9d9
JM
4337#if defined(TARGET_PPC64)
4338 if (ctx->sf_mode)
4339 gen_op_tlbie_64();
4340 else
4341#endif
4342 gen_op_tlbie();
9a64fbe4 4343#endif
79aceca5
FB
4344}
4345
4346/* tlbsync */
76a66253 4347GEN_HANDLER(tlbsync, 0x1F, 0x16, 0x11, 0x03FFF801, PPC_MEM_TLBSYNC)
79aceca5 4348{
9a64fbe4 4349#if defined(CONFIG_USER_ONLY)
e1833e1f 4350 GEN_EXCP_PRIVOPC(ctx);
9a64fbe4 4351#else
76a66253 4352 if (unlikely(!ctx->supervisor)) {
e1833e1f 4353 GEN_EXCP_PRIVOPC(ctx);
9fddaa0c 4354 return;
9a64fbe4
FB
4355 }
4356 /* This has no effect: it should ensure that all previous
4357 * tlbie have completed
4358 */
e1833e1f 4359 GEN_STOP(ctx);
9a64fbe4 4360#endif
79aceca5
FB
4361}
4362
426613db
JM
4363#if defined(TARGET_PPC64)
4364/* slbia */
4365GEN_HANDLER(slbia, 0x1F, 0x12, 0x0F, 0x03FFFC01, PPC_SLBI)
4366{
4367#if defined(CONFIG_USER_ONLY)
e1833e1f 4368 GEN_EXCP_PRIVOPC(ctx);
426613db
JM
4369#else
4370 if (unlikely(!ctx->supervisor)) {
e1833e1f 4371 GEN_EXCP_PRIVOPC(ctx);
426613db
JM
4372 return;
4373 }
4374 gen_op_slbia();
426613db
JM
4375#endif
4376}
4377
4378/* slbie */
4379GEN_HANDLER(slbie, 0x1F, 0x12, 0x0D, 0x03FF0001, PPC_SLBI)
4380{
4381#if defined(CONFIG_USER_ONLY)
e1833e1f 4382 GEN_EXCP_PRIVOPC(ctx);
426613db
JM
4383#else
4384 if (unlikely(!ctx->supervisor)) {
e1833e1f 4385 GEN_EXCP_PRIVOPC(ctx);
426613db
JM
4386 return;
4387 }
f78fb44e 4388 tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rB(ctx->opcode)]);
426613db 4389 gen_op_slbie();
426613db
JM
4390#endif
4391}
4392#endif
4393
79aceca5
FB
4394/*** External control ***/
4395/* Optional: */
9a64fbe4
FB
4396#define op_eciwx() (*gen_op_eciwx[ctx->mem_idx])()
4397#define op_ecowx() (*gen_op_ecowx[ctx->mem_idx])()
7863667f
JM
4398static GenOpFunc *gen_op_eciwx[NB_MEM_FUNCS] = {
4399 GEN_MEM_FUNCS(eciwx),
111bfab3 4400};
7863667f
JM
4401static GenOpFunc *gen_op_ecowx[NB_MEM_FUNCS] = {
4402 GEN_MEM_FUNCS(ecowx),
111bfab3 4403};
9a64fbe4 4404
111bfab3 4405/* eciwx */
79aceca5
FB
4406GEN_HANDLER(eciwx, 0x1F, 0x16, 0x0D, 0x00000001, PPC_EXTERN)
4407{
9a64fbe4 4408 /* Should check EAR[E] & alignment ! */
a7859e89 4409 gen_set_access_type(ACCESS_RES);
e2be8d8d 4410 gen_addr_reg_index(cpu_T[0], ctx);
76a66253 4411 op_eciwx();
f78fb44e 4412 tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_T[0]);
76a66253
JM
4413}
4414
4415/* ecowx */
4416GEN_HANDLER(ecowx, 0x1F, 0x16, 0x09, 0x00000001, PPC_EXTERN)
4417{
4418 /* Should check EAR[E] & alignment ! */
e2be8d8d 4419 gen_addr_reg_index(cpu_T[0], ctx);
f78fb44e 4420 tcg_gen_mov_tl(cpu_T[1], cpu_gpr[rS(ctx->opcode)]);
76a66253
JM
4421 op_ecowx();
4422}
4423
4424/* PowerPC 601 specific instructions */
4425/* abs - abs. */
4426GEN_HANDLER(abs, 0x1F, 0x08, 0x0B, 0x0000F800, PPC_POWER_BR)
4427{
f78fb44e 4428 tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rA(ctx->opcode)]);
76a66253 4429 gen_op_POWER_abs();
f78fb44e 4430 tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_T[0]);
76a66253 4431 if (unlikely(Rc(ctx->opcode) != 0))
e1571908 4432 gen_set_Rc0(ctx, cpu_T[0]);
76a66253
JM
4433}
4434
4435/* abso - abso. */
4436GEN_HANDLER(abso, 0x1F, 0x08, 0x1B, 0x0000F800, PPC_POWER_BR)
4437{
f78fb44e 4438 tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rA(ctx->opcode)]);
76a66253 4439 gen_op_POWER_abso();
f78fb44e 4440 tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_T[0]);
76a66253 4441 if (unlikely(Rc(ctx->opcode) != 0))
e1571908 4442 gen_set_Rc0(ctx, cpu_T[0]);
76a66253
JM
4443}
4444
4445/* clcs */
a750fc0b 4446GEN_HANDLER(clcs, 0x1F, 0x10, 0x13, 0x0000F800, PPC_POWER_BR)
76a66253 4447{
f78fb44e 4448 tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rA(ctx->opcode)]);
76a66253 4449 gen_op_POWER_clcs();
c7697e1f 4450 /* Rc=1 sets CR0 to an undefined state */
f78fb44e 4451 tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_T[0]);
76a66253
JM
4452}
4453
4454/* div - div. */
4455GEN_HANDLER(div, 0x1F, 0x0B, 0x0A, 0x00000000, PPC_POWER_BR)
4456{
f78fb44e
AJ
4457 tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rA(ctx->opcode)]);
4458 tcg_gen_mov_tl(cpu_T[1], cpu_gpr[rB(ctx->opcode)]);
76a66253 4459 gen_op_POWER_div();
f78fb44e 4460 tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_T[0]);
76a66253 4461 if (unlikely(Rc(ctx->opcode) != 0))
e1571908 4462 gen_set_Rc0(ctx, cpu_T[0]);
76a66253
JM
4463}
4464
4465/* divo - divo. */
4466GEN_HANDLER(divo, 0x1F, 0x0B, 0x1A, 0x00000000, PPC_POWER_BR)
4467{
f78fb44e
AJ
4468 tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rA(ctx->opcode)]);
4469 tcg_gen_mov_tl(cpu_T[1], cpu_gpr[rB(ctx->opcode)]);
76a66253 4470 gen_op_POWER_divo();
f78fb44e 4471 tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_T[0]);
76a66253 4472 if (unlikely(Rc(ctx->opcode) != 0))
e1571908 4473 gen_set_Rc0(ctx, cpu_T[0]);
76a66253
JM
4474}
4475
4476/* divs - divs. */
4477GEN_HANDLER(divs, 0x1F, 0x0B, 0x0B, 0x00000000, PPC_POWER_BR)
4478{
f78fb44e
AJ
4479 tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rA(ctx->opcode)]);
4480 tcg_gen_mov_tl(cpu_T[1], cpu_gpr[rB(ctx->opcode)]);
76a66253 4481 gen_op_POWER_divs();
f78fb44e 4482 tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_T[0]);
76a66253 4483 if (unlikely(Rc(ctx->opcode) != 0))
e1571908 4484 gen_set_Rc0(ctx, cpu_T[0]);
76a66253
JM
4485}
4486
4487/* divso - divso. */
4488GEN_HANDLER(divso, 0x1F, 0x0B, 0x1B, 0x00000000, PPC_POWER_BR)
4489{
f78fb44e
AJ
4490 tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rA(ctx->opcode)]);
4491 tcg_gen_mov_tl(cpu_T[1], cpu_gpr[rB(ctx->opcode)]);
76a66253 4492 gen_op_POWER_divso();
f78fb44e 4493 tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_T[0]);
76a66253 4494 if (unlikely(Rc(ctx->opcode) != 0))
e1571908 4495 gen_set_Rc0(ctx, cpu_T[0]);
76a66253
JM
4496}
4497
4498/* doz - doz. */
4499GEN_HANDLER(doz, 0x1F, 0x08, 0x08, 0x00000000, PPC_POWER_BR)
4500{
f78fb44e
AJ
4501 tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rA(ctx->opcode)]);
4502 tcg_gen_mov_tl(cpu_T[1], cpu_gpr[rB(ctx->opcode)]);
76a66253 4503 gen_op_POWER_doz();
f78fb44e 4504 tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_T[0]);
76a66253 4505 if (unlikely(Rc(ctx->opcode) != 0))
e1571908 4506 gen_set_Rc0(ctx, cpu_T[0]);
76a66253
JM
4507}
4508
4509/* dozo - dozo. */
4510GEN_HANDLER(dozo, 0x1F, 0x08, 0x18, 0x00000000, PPC_POWER_BR)
4511{
f78fb44e
AJ
4512 tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rA(ctx->opcode)]);
4513 tcg_gen_mov_tl(cpu_T[1], cpu_gpr[rB(ctx->opcode)]);
76a66253 4514 gen_op_POWER_dozo();
f78fb44e 4515 tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_T[0]);
76a66253 4516 if (unlikely(Rc(ctx->opcode) != 0))
e1571908 4517 gen_set_Rc0(ctx, cpu_T[0]);
76a66253
JM
4518}
4519
4520/* dozi */
4521GEN_HANDLER(dozi, 0x09, 0xFF, 0xFF, 0x00000000, PPC_POWER_BR)
4522{
f78fb44e 4523 tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rA(ctx->opcode)]);
86c581dc 4524 tcg_gen_movi_tl(cpu_T[1], SIMM(ctx->opcode));
76a66253 4525 gen_op_POWER_doz();
f78fb44e 4526 tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_T[0]);
76a66253
JM
4527}
4528
76a66253
JM
4529/* lscbx - lscbx. */
4530GEN_HANDLER(lscbx, 0x1F, 0x15, 0x08, 0x00000000, PPC_POWER_BR)
4531{
bdb4b689
AJ
4532 TCGv t0 = tcg_temp_new();
4533 TCGv_i32 t1 = tcg_const_i32(rD(ctx->opcode));
4534 TCGv_i32 t2 = tcg_const_i32(rA(ctx->opcode));
4535 TCGv_i32 t3 = tcg_const_i32(rB(ctx->opcode));
76a66253 4536
bdb4b689 4537 gen_addr_reg_index(t0, ctx);
76a66253 4538 /* NIP cannot be restored if the memory exception comes from an helper */
d9bce9d9 4539 gen_update_nip(ctx, ctx->nip - 4);
bdb4b689
AJ
4540 gen_helper_lscbx(t0, t0, t1, t2, t3);
4541 tcg_temp_free_i32(t1);
4542 tcg_temp_free_i32(t2);
4543 tcg_temp_free_i32(t3);
3d7b417e 4544 tcg_gen_andi_tl(cpu_xer, cpu_xer, ~0x7F);
bdb4b689 4545 tcg_gen_or_tl(cpu_xer, cpu_xer, t0);
76a66253 4546 if (unlikely(Rc(ctx->opcode) != 0))
bdb4b689
AJ
4547 gen_set_Rc0(ctx, t0);
4548 tcg_temp_free(t0);
76a66253
JM
4549}
4550
4551/* maskg - maskg. */
4552GEN_HANDLER(maskg, 0x1F, 0x1D, 0x00, 0x00000000, PPC_POWER_BR)
4553{
f78fb44e
AJ
4554 tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rS(ctx->opcode)]);
4555 tcg_gen_mov_tl(cpu_T[1], cpu_gpr[rB(ctx->opcode)]);
76a66253 4556 gen_op_POWER_maskg();
f78fb44e 4557 tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], cpu_T[0]);
76a66253 4558 if (unlikely(Rc(ctx->opcode) != 0))
e1571908 4559 gen_set_Rc0(ctx, cpu_T[0]);
76a66253
JM
4560}
4561
4562/* maskir - maskir. */
4563GEN_HANDLER(maskir, 0x1F, 0x1D, 0x10, 0x00000000, PPC_POWER_BR)
4564{
f78fb44e
AJ
4565 tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rA(ctx->opcode)]);
4566 tcg_gen_mov_tl(cpu_T[1], cpu_gpr[rS(ctx->opcode)]);
4567 tcg_gen_mov_tl(cpu_T[2], cpu_gpr[rB(ctx->opcode)]);
76a66253 4568 gen_op_POWER_maskir();
f78fb44e 4569 tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], cpu_T[0]);
76a66253 4570 if (unlikely(Rc(ctx->opcode) != 0))
e1571908 4571 gen_set_Rc0(ctx, cpu_T[0]);
76a66253
JM
4572}
4573
4574/* mul - mul. */
4575GEN_HANDLER(mul, 0x1F, 0x0B, 0x03, 0x00000000, PPC_POWER_BR)
4576{
f78fb44e
AJ
4577 tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rA(ctx->opcode)]);
4578 tcg_gen_mov_tl(cpu_T[1], cpu_gpr[rB(ctx->opcode)]);
76a66253 4579 gen_op_POWER_mul();
f78fb44e 4580 tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_T[0]);
76a66253 4581 if (unlikely(Rc(ctx->opcode) != 0))
e1571908 4582 gen_set_Rc0(ctx, cpu_T[0]);
76a66253
JM
4583}
4584
4585/* mulo - mulo. */
4586GEN_HANDLER(mulo, 0x1F, 0x0B, 0x13, 0x00000000, PPC_POWER_BR)
4587{
f78fb44e
AJ
4588 tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rA(ctx->opcode)]);
4589 tcg_gen_mov_tl(cpu_T[1], cpu_gpr[rB(ctx->opcode)]);
76a66253 4590 gen_op_POWER_mulo();
f78fb44e 4591 tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_T[0]);
76a66253 4592 if (unlikely(Rc(ctx->opcode) != 0))
e1571908 4593 gen_set_Rc0(ctx, cpu_T[0]);
76a66253
JM
4594}
4595
4596/* nabs - nabs. */
4597GEN_HANDLER(nabs, 0x1F, 0x08, 0x0F, 0x00000000, PPC_POWER_BR)
4598{
f78fb44e 4599 tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rA(ctx->opcode)]);
76a66253 4600 gen_op_POWER_nabs();
f78fb44e 4601 tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_T[0]);
76a66253 4602 if (unlikely(Rc(ctx->opcode) != 0))
e1571908 4603 gen_set_Rc0(ctx, cpu_T[0]);
76a66253
JM
4604}
4605
4606/* nabso - nabso. */
4607GEN_HANDLER(nabso, 0x1F, 0x08, 0x1F, 0x00000000, PPC_POWER_BR)
4608{
f78fb44e 4609 tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rA(ctx->opcode)]);
76a66253 4610 gen_op_POWER_nabso();
f78fb44e 4611 tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_T[0]);
76a66253 4612 if (unlikely(Rc(ctx->opcode) != 0))
e1571908 4613 gen_set_Rc0(ctx, cpu_T[0]);
76a66253
JM
4614}
4615
4616/* rlmi - rlmi. */
4617GEN_HANDLER(rlmi, 0x16, 0xFF, 0xFF, 0x00000000, PPC_POWER_BR)
4618{
4619 uint32_t mb, me;
4620
4621 mb = MB(ctx->opcode);
4622 me = ME(ctx->opcode);
f78fb44e
AJ
4623 tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rS(ctx->opcode)]);
4624 tcg_gen_mov_tl(cpu_T[1], cpu_gpr[rA(ctx->opcode)]);
4625 tcg_gen_mov_tl(cpu_T[2], cpu_gpr[rB(ctx->opcode)]);
76a66253 4626 gen_op_POWER_rlmi(MASK(mb, me), ~MASK(mb, me));
f78fb44e 4627 tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], cpu_T[0]);
76a66253 4628 if (unlikely(Rc(ctx->opcode) != 0))
e1571908 4629 gen_set_Rc0(ctx, cpu_T[0]);
76a66253
JM
4630}
4631
4632/* rrib - rrib. */
4633GEN_HANDLER(rrib, 0x1F, 0x19, 0x10, 0x00000000, PPC_POWER_BR)
4634{
f78fb44e
AJ
4635 tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rS(ctx->opcode)]);
4636 tcg_gen_mov_tl(cpu_T[1], cpu_gpr[rA(ctx->opcode)]);
4637 tcg_gen_mov_tl(cpu_T[2], cpu_gpr[rB(ctx->opcode)]);
76a66253 4638 gen_op_POWER_rrib();
f78fb44e 4639 tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], cpu_T[0]);
76a66253 4640 if (unlikely(Rc(ctx->opcode) != 0))
e1571908 4641 gen_set_Rc0(ctx, cpu_T[0]);
76a66253
JM
4642}
4643
4644/* sle - sle. */
4645GEN_HANDLER(sle, 0x1F, 0x19, 0x04, 0x00000000, PPC_POWER_BR)
4646{
f78fb44e
AJ
4647 tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rS(ctx->opcode)]);
4648 tcg_gen_mov_tl(cpu_T[1], cpu_gpr[rB(ctx->opcode)]);
76a66253 4649 gen_op_POWER_sle();
f78fb44e 4650 tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], cpu_T[0]);
76a66253 4651 if (unlikely(Rc(ctx->opcode) != 0))
e1571908 4652 gen_set_Rc0(ctx, cpu_T[0]);
76a66253
JM
4653}
4654
4655/* sleq - sleq. */
4656GEN_HANDLER(sleq, 0x1F, 0x19, 0x06, 0x00000000, PPC_POWER_BR)
4657{
f78fb44e
AJ
4658 tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rS(ctx->opcode)]);
4659 tcg_gen_mov_tl(cpu_T[1], cpu_gpr[rB(ctx->opcode)]);
76a66253 4660 gen_op_POWER_sleq();
f78fb44e 4661 tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], cpu_T[0]);
76a66253 4662 if (unlikely(Rc(ctx->opcode) != 0))
e1571908 4663 gen_set_Rc0(ctx, cpu_T[0]);
76a66253
JM
4664}
4665
4666/* sliq - sliq. */
4667GEN_HANDLER(sliq, 0x1F, 0x18, 0x05, 0x00000000, PPC_POWER_BR)
4668{
f78fb44e 4669 tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rS(ctx->opcode)]);
86c581dc 4670 tcg_gen_movi_tl(cpu_T[1], SH(ctx->opcode));
76a66253 4671 gen_op_POWER_sle();
f78fb44e 4672 tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], cpu_T[0]);
76a66253 4673 if (unlikely(Rc(ctx->opcode) != 0))
e1571908 4674 gen_set_Rc0(ctx, cpu_T[0]);
76a66253
JM
4675}
4676
4677/* slliq - slliq. */
4678GEN_HANDLER(slliq, 0x1F, 0x18, 0x07, 0x00000000, PPC_POWER_BR)
4679{
f78fb44e 4680 tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rS(ctx->opcode)]);
86c581dc 4681 tcg_gen_movi_tl(cpu_T[1], SH(ctx->opcode));
76a66253 4682 gen_op_POWER_sleq();
f78fb44e 4683 tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], cpu_T[0]);
76a66253 4684 if (unlikely(Rc(ctx->opcode) != 0))
e1571908 4685 gen_set_Rc0(ctx, cpu_T[0]);
76a66253
JM
4686}
4687
4688/* sllq - sllq. */
4689GEN_HANDLER(sllq, 0x1F, 0x18, 0x06, 0x00000000, PPC_POWER_BR)
4690{
f78fb44e
AJ
4691 tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rS(ctx->opcode)]);
4692 tcg_gen_mov_tl(cpu_T[1], cpu_gpr[rB(ctx->opcode)]);
76a66253 4693 gen_op_POWER_sllq();
f78fb44e 4694 tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], cpu_T[0]);
76a66253 4695 if (unlikely(Rc(ctx->opcode) != 0))
e1571908 4696 gen_set_Rc0(ctx, cpu_T[0]);
76a66253
JM
4697}
4698
4699/* slq - slq. */
4700GEN_HANDLER(slq, 0x1F, 0x18, 0x04, 0x00000000, PPC_POWER_BR)
4701{
f78fb44e
AJ
4702 tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rS(ctx->opcode)]);
4703 tcg_gen_mov_tl(cpu_T[1], cpu_gpr[rB(ctx->opcode)]);
76a66253 4704 gen_op_POWER_slq();
f78fb44e 4705 tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], cpu_T[0]);
76a66253 4706 if (unlikely(Rc(ctx->opcode) != 0))
e1571908 4707 gen_set_Rc0(ctx, cpu_T[0]);
76a66253
JM
4708}
4709
d9bce9d9 4710/* sraiq - sraiq. */
76a66253
JM
4711GEN_HANDLER(sraiq, 0x1F, 0x18, 0x1D, 0x00000000, PPC_POWER_BR)
4712{
f78fb44e 4713 tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rS(ctx->opcode)]);
86c581dc 4714 tcg_gen_movi_tl(cpu_T[1], SH(ctx->opcode));
76a66253 4715 gen_op_POWER_sraq();
f78fb44e 4716 tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], cpu_T[0]);
76a66253 4717 if (unlikely(Rc(ctx->opcode) != 0))
e1571908 4718 gen_set_Rc0(ctx, cpu_T[0]);
76a66253
JM
4719}
4720
4721/* sraq - sraq. */
4722GEN_HANDLER(sraq, 0x1F, 0x18, 0x1C, 0x00000000, PPC_POWER_BR)
4723{
f78fb44e
AJ
4724 tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rS(ctx->opcode)]);
4725 tcg_gen_mov_tl(cpu_T[1], cpu_gpr[rB(ctx->opcode)]);
76a66253 4726 gen_op_POWER_sraq();
f78fb44e 4727 tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], cpu_T[0]);
76a66253 4728 if (unlikely(Rc(ctx->opcode) != 0))
e1571908 4729 gen_set_Rc0(ctx, cpu_T[0]);
76a66253
JM
4730}
4731
4732/* sre - sre. */
4733GEN_HANDLER(sre, 0x1F, 0x19, 0x14, 0x00000000, PPC_POWER_BR)
4734{
f78fb44e
AJ
4735 tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rS(ctx->opcode)]);
4736 tcg_gen_mov_tl(cpu_T[1], cpu_gpr[rB(ctx->opcode)]);
76a66253 4737 gen_op_POWER_sre();
f78fb44e 4738 tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], cpu_T[0]);
76a66253 4739 if (unlikely(Rc(ctx->opcode) != 0))
e1571908 4740 gen_set_Rc0(ctx, cpu_T[0]);
76a66253
JM
4741}
4742
4743/* srea - srea. */
4744GEN_HANDLER(srea, 0x1F, 0x19, 0x1C, 0x00000000, PPC_POWER_BR)
4745{
f78fb44e
AJ
4746 tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rS(ctx->opcode)]);
4747 tcg_gen_mov_tl(cpu_T[1], cpu_gpr[rB(ctx->opcode)]);
76a66253 4748 gen_op_POWER_srea();
f78fb44e 4749 tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], cpu_T[0]);
76a66253 4750 if (unlikely(Rc(ctx->opcode) != 0))
e1571908 4751 gen_set_Rc0(ctx, cpu_T[0]);
76a66253
JM
4752}
4753
4754/* sreq */
4755GEN_HANDLER(sreq, 0x1F, 0x19, 0x16, 0x00000000, PPC_POWER_BR)
4756{
f78fb44e
AJ
4757 tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rS(ctx->opcode)]);
4758 tcg_gen_mov_tl(cpu_T[1], cpu_gpr[rB(ctx->opcode)]);
76a66253 4759 gen_op_POWER_sreq();
f78fb44e 4760 tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], cpu_T[0]);
76a66253 4761 if (unlikely(Rc(ctx->opcode) != 0))
e1571908 4762 gen_set_Rc0(ctx, cpu_T[0]);
76a66253
JM
4763}
4764
4765/* sriq */
4766GEN_HANDLER(sriq, 0x1F, 0x18, 0x15, 0x00000000, PPC_POWER_BR)
4767{
f78fb44e 4768 tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rS(ctx->opcode)]);
86c581dc 4769 tcg_gen_movi_tl(cpu_T[1], SH(ctx->opcode));
76a66253 4770 gen_op_POWER_srq();
f78fb44e 4771 tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], cpu_T[0]);
76a66253 4772 if (unlikely(Rc(ctx->opcode) != 0))
e1571908 4773 gen_set_Rc0(ctx, cpu_T[0]);
76a66253
JM
4774}
4775
4776/* srliq */
4777GEN_HANDLER(srliq, 0x1F, 0x18, 0x17, 0x00000000, PPC_POWER_BR)
4778{
f78fb44e
AJ
4779 tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rS(ctx->opcode)]);
4780 tcg_gen_mov_tl(cpu_T[1], cpu_gpr[rB(ctx->opcode)]);
86c581dc 4781 tcg_gen_movi_tl(cpu_T[1], SH(ctx->opcode));
76a66253 4782 gen_op_POWER_srlq();
f78fb44e 4783 tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], cpu_T[0]);
76a66253 4784 if (unlikely(Rc(ctx->opcode) != 0))
e1571908 4785 gen_set_Rc0(ctx, cpu_T[0]);
76a66253
JM
4786}
4787
4788/* srlq */
4789GEN_HANDLER(srlq, 0x1F, 0x18, 0x16, 0x00000000, PPC_POWER_BR)
4790{
f78fb44e
AJ
4791 tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rS(ctx->opcode)]);
4792 tcg_gen_mov_tl(cpu_T[1], cpu_gpr[rB(ctx->opcode)]);
76a66253 4793 gen_op_POWER_srlq();
f78fb44e 4794 tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], cpu_T[0]);
76a66253 4795 if (unlikely(Rc(ctx->opcode) != 0))
e1571908 4796 gen_set_Rc0(ctx, cpu_T[0]);
76a66253
JM
4797}
4798
4799/* srq */
4800GEN_HANDLER(srq, 0x1F, 0x18, 0x14, 0x00000000, PPC_POWER_BR)
4801{
f78fb44e
AJ
4802 tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rS(ctx->opcode)]);
4803 tcg_gen_mov_tl(cpu_T[1], cpu_gpr[rB(ctx->opcode)]);
76a66253 4804 gen_op_POWER_srq();
f78fb44e 4805 tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], cpu_T[0]);
76a66253 4806 if (unlikely(Rc(ctx->opcode) != 0))
e1571908 4807 gen_set_Rc0(ctx, cpu_T[0]);
76a66253
JM
4808}
4809
4810/* PowerPC 602 specific instructions */
4811/* dsa */
4812GEN_HANDLER(dsa, 0x1F, 0x14, 0x13, 0x03FFF801, PPC_602_SPEC)
4813{
4814 /* XXX: TODO */
e1833e1f 4815 GEN_EXCP_INVAL(ctx);
76a66253
JM
4816}
4817
4818/* esa */
4819GEN_HANDLER(esa, 0x1F, 0x14, 0x12, 0x03FFF801, PPC_602_SPEC)
4820{
4821 /* XXX: TODO */
e1833e1f 4822 GEN_EXCP_INVAL(ctx);
76a66253
JM
4823}
4824
4825/* mfrom */
4826GEN_HANDLER(mfrom, 0x1F, 0x09, 0x08, 0x03E0F801, PPC_602_SPEC)
4827{
4828#if defined(CONFIG_USER_ONLY)
e1833e1f 4829 GEN_EXCP_PRIVOPC(ctx);
76a66253
JM
4830#else
4831 if (unlikely(!ctx->supervisor)) {
e1833e1f 4832 GEN_EXCP_PRIVOPC(ctx);
76a66253
JM
4833 return;
4834 }
cf02a65c 4835 gen_helper_602_mfrom(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)]);
76a66253
JM
4836#endif
4837}
4838
4839/* 602 - 603 - G2 TLB management */
4840/* tlbld */
c7697e1f 4841GEN_HANDLER2(tlbld_6xx, "tlbld", 0x1F, 0x12, 0x1E, 0x03FF0001, PPC_6xx_TLB)
76a66253
JM
4842{
4843#if defined(CONFIG_USER_ONLY)
e1833e1f 4844 GEN_EXCP_PRIVOPC(ctx);
76a66253
JM
4845#else
4846 if (unlikely(!ctx->supervisor)) {
e1833e1f 4847 GEN_EXCP_PRIVOPC(ctx);
76a66253
JM
4848 return;
4849 }
0f3955e2 4850 gen_helper_load_6xx_tlbd(cpu_gpr[rB(ctx->opcode)]);
76a66253
JM
4851#endif
4852}
4853
4854/* tlbli */
c7697e1f 4855GEN_HANDLER2(tlbli_6xx, "tlbli", 0x1F, 0x12, 0x1F, 0x03FF0001, PPC_6xx_TLB)
76a66253
JM
4856{
4857#if defined(CONFIG_USER_ONLY)
e1833e1f 4858 GEN_EXCP_PRIVOPC(ctx);
76a66253
JM
4859#else
4860 if (unlikely(!ctx->supervisor)) {
e1833e1f 4861 GEN_EXCP_PRIVOPC(ctx);
76a66253
JM
4862 return;
4863 }
0f3955e2 4864 gen_helper_load_6xx_tlbi(cpu_gpr[rB(ctx->opcode)]);
76a66253
JM
4865#endif
4866}
4867
7dbe11ac
JM
4868/* 74xx TLB management */
4869/* tlbld */
c7697e1f 4870GEN_HANDLER2(tlbld_74xx, "tlbld", 0x1F, 0x12, 0x1E, 0x03FF0001, PPC_74xx_TLB)
7dbe11ac
JM
4871{
4872#if defined(CONFIG_USER_ONLY)
4873 GEN_EXCP_PRIVOPC(ctx);
4874#else
4875 if (unlikely(!ctx->supervisor)) {
4876 GEN_EXCP_PRIVOPC(ctx);
4877 return;
4878 }
0f3955e2 4879 gen_helper_load_74xx_tlbd(cpu_gpr[rB(ctx->opcode)]);
7dbe11ac
JM
4880#endif
4881}
4882
4883/* tlbli */
c7697e1f 4884GEN_HANDLER2(tlbli_74xx, "tlbli", 0x1F, 0x12, 0x1F, 0x03FF0001, PPC_74xx_TLB)
7dbe11ac
JM
4885{
4886#if defined(CONFIG_USER_ONLY)
4887 GEN_EXCP_PRIVOPC(ctx);
4888#else
4889 if (unlikely(!ctx->supervisor)) {
4890 GEN_EXCP_PRIVOPC(ctx);
4891 return;
4892 }
0f3955e2 4893 gen_helper_load_74xx_tlbi(cpu_gpr[rB(ctx->opcode)]);
7dbe11ac
JM
4894#endif
4895}
4896
76a66253
JM
4897/* POWER instructions not in PowerPC 601 */
4898/* clf */
4899GEN_HANDLER(clf, 0x1F, 0x16, 0x03, 0x03E00000, PPC_POWER)
4900{
4901 /* Cache line flush: implemented as no-op */
4902}
4903
4904/* cli */
4905GEN_HANDLER(cli, 0x1F, 0x16, 0x0F, 0x03E00000, PPC_POWER)
4906{
7f75ffd3 4907 /* Cache line invalidate: privileged and treated as no-op */
76a66253 4908#if defined(CONFIG_USER_ONLY)
e1833e1f 4909 GEN_EXCP_PRIVOPC(ctx);
76a66253
JM
4910#else
4911 if (unlikely(!ctx->supervisor)) {
e1833e1f 4912 GEN_EXCP_PRIVOPC(ctx);
76a66253
JM
4913 return;
4914 }
4915#endif
4916}
4917
4918/* dclst */
4919GEN_HANDLER(dclst, 0x1F, 0x16, 0x13, 0x03E00000, PPC_POWER)
4920{
4921 /* Data cache line store: treated as no-op */
4922}
4923
4924GEN_HANDLER(mfsri, 0x1F, 0x13, 0x13, 0x00000001, PPC_POWER)
4925{
4926#if defined(CONFIG_USER_ONLY)
e1833e1f 4927 GEN_EXCP_PRIVOPC(ctx);
76a66253
JM
4928#else
4929 if (unlikely(!ctx->supervisor)) {
e1833e1f 4930 GEN_EXCP_PRIVOPC(ctx);
76a66253
JM
4931 return;
4932 }
4933 int ra = rA(ctx->opcode);
4934 int rd = rD(ctx->opcode);
4935
e2be8d8d 4936 gen_addr_reg_index(cpu_T[0], ctx);
76a66253 4937 gen_op_POWER_mfsri();
f78fb44e 4938 tcg_gen_mov_tl(cpu_gpr[rd], cpu_T[0]);
76a66253 4939 if (ra != 0 && ra != rd)
f78fb44e 4940 tcg_gen_mov_tl(cpu_gpr[ra], cpu_T[1]);
76a66253
JM
4941#endif
4942}
4943
4944GEN_HANDLER(rac, 0x1F, 0x12, 0x19, 0x00000001, PPC_POWER)
4945{
4946#if defined(CONFIG_USER_ONLY)
e1833e1f 4947 GEN_EXCP_PRIVOPC(ctx);
76a66253
JM
4948#else
4949 if (unlikely(!ctx->supervisor)) {
e1833e1f 4950 GEN_EXCP_PRIVOPC(ctx);
76a66253
JM
4951 return;
4952 }
e2be8d8d 4953 gen_addr_reg_index(cpu_T[0], ctx);
76a66253 4954 gen_op_POWER_rac();
f78fb44e 4955 tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_T[0]);
76a66253
JM
4956#endif
4957}
4958
4959GEN_HANDLER(rfsvc, 0x13, 0x12, 0x02, 0x03FFF0001, PPC_POWER)
4960{
4961#if defined(CONFIG_USER_ONLY)
e1833e1f 4962 GEN_EXCP_PRIVOPC(ctx);
76a66253
JM
4963#else
4964 if (unlikely(!ctx->supervisor)) {
e1833e1f 4965 GEN_EXCP_PRIVOPC(ctx);
76a66253
JM
4966 return;
4967 }
4968 gen_op_POWER_rfsvc();
e1833e1f 4969 GEN_SYNC(ctx);
76a66253
JM
4970#endif
4971}
4972
4973/* svc is not implemented for now */
4974
4975/* POWER2 specific instructions */
4976/* Quad manipulation (load/store two floats at a time) */
76a66253
JM
4977
4978/* lfq */
4979GEN_HANDLER(lfq, 0x38, 0xFF, 0xFF, 0x00000003, PPC_POWER2)
4980{
01a4afeb
AJ
4981 int rd = rD(ctx->opcode);
4982 TCGv t0 = tcg_temp_new();
4983 gen_addr_imm_index(t0, ctx, 0);
4984 gen_qemu_ld64(cpu_fpr[rd], t0, ctx->mem_idx);
4985 tcg_gen_addi_tl(t0, t0, 8);
4986 gen_qemu_ld64(cpu_fpr[(rd + 1) % 32], t0, ctx->mem_idx);
4987 tcg_temp_free(t0);
76a66253
JM
4988}
4989
4990/* lfqu */
4991GEN_HANDLER(lfqu, 0x39, 0xFF, 0xFF, 0x00000003, PPC_POWER2)
4992{
4993 int ra = rA(ctx->opcode);
01a4afeb
AJ
4994 int rd = rD(ctx->opcode);
4995 TCGv t0 = tcg_temp_new();
4996 TCGv t1 = tcg_temp_new();
4997 gen_addr_imm_index(t0, ctx, 0);
4998 gen_qemu_ld64(cpu_fpr[rd], t0, ctx->mem_idx);
4999 tcg_gen_addi_tl(t1, t0, 8);
5000 gen_qemu_ld64(cpu_fpr[(rd + 1) % 32], t1, ctx->mem_idx);
76a66253 5001 if (ra != 0)
01a4afeb
AJ
5002 tcg_gen_mov_tl(cpu_gpr[ra], t0);
5003 tcg_temp_free(t0);
5004 tcg_temp_free(t1);
76a66253
JM
5005}
5006
5007/* lfqux */
5008GEN_HANDLER(lfqux, 0x1F, 0x17, 0x19, 0x00000001, PPC_POWER2)
5009{
5010 int ra = rA(ctx->opcode);
01a4afeb
AJ
5011 int rd = rD(ctx->opcode);
5012 TCGv t0 = tcg_temp_new();
5013 TCGv t1 = tcg_temp_new();
5014 gen_addr_reg_index(t0, ctx);
5015 gen_qemu_ld64(cpu_fpr[rd], t0, ctx->mem_idx);
5016 tcg_gen_addi_tl(t1, t0, 8);
5017 gen_qemu_ld64(cpu_fpr[(rd + 1) % 32], t1, ctx->mem_idx);
76a66253 5018 if (ra != 0)
01a4afeb
AJ
5019 tcg_gen_mov_tl(cpu_gpr[ra], t0);
5020 tcg_temp_free(t0);
5021 tcg_temp_free(t1);
76a66253
JM
5022}
5023
5024/* lfqx */
5025GEN_HANDLER(lfqx, 0x1F, 0x17, 0x18, 0x00000001, PPC_POWER2)
5026{
01a4afeb
AJ
5027 int rd = rD(ctx->opcode);
5028 TCGv t0 = tcg_temp_new();
5029 gen_addr_reg_index(t0, ctx);
5030 gen_qemu_ld64(cpu_fpr[rd], t0, ctx->mem_idx);
5031 tcg_gen_addi_tl(t0, t0, 8);
5032 gen_qemu_ld64(cpu_fpr[(rd + 1) % 32], t0, ctx->mem_idx);
5033 tcg_temp_free(t0);
76a66253
JM
5034}
5035
5036/* stfq */
5037GEN_HANDLER(stfq, 0x3C, 0xFF, 0xFF, 0x00000003, PPC_POWER2)
5038{
01a4afeb
AJ
5039 int rd = rD(ctx->opcode);
5040 TCGv t0 = tcg_temp_new();
5041 gen_addr_imm_index(t0, ctx, 0);
5042 gen_qemu_st64(cpu_fpr[rd], t0, ctx->mem_idx);
5043 tcg_gen_addi_tl(t0, t0, 8);
5044 gen_qemu_st64(cpu_fpr[(rd + 1) % 32], t0, ctx->mem_idx);
5045 tcg_temp_free(t0);
76a66253
JM
5046}
5047
5048/* stfqu */
5049GEN_HANDLER(stfqu, 0x3D, 0xFF, 0xFF, 0x00000003, PPC_POWER2)
5050{
5051 int ra = rA(ctx->opcode);
01a4afeb
AJ
5052 int rd = rD(ctx->opcode);
5053 TCGv t0 = tcg_temp_new();
5054 TCGv t1 = tcg_temp_new();
5055 gen_addr_imm_index(t0, ctx, 0);
5056 gen_qemu_st64(cpu_fpr[rd], t0, ctx->mem_idx);
5057 tcg_gen_addi_tl(t1, t0, 8);
5058 gen_qemu_st64(cpu_fpr[(rd + 1) % 32], t1, ctx->mem_idx);
76a66253 5059 if (ra != 0)
01a4afeb
AJ
5060 tcg_gen_mov_tl(cpu_gpr[ra], t0);
5061 tcg_temp_free(t0);
5062 tcg_temp_free(t1);
76a66253
JM
5063}
5064
5065/* stfqux */
5066GEN_HANDLER(stfqux, 0x1F, 0x17, 0x1D, 0x00000001, PPC_POWER2)
5067{
5068 int ra = rA(ctx->opcode);
01a4afeb
AJ
5069 int rd = rD(ctx->opcode);
5070 TCGv t0 = tcg_temp_new();
5071 TCGv t1 = tcg_temp_new();
5072 gen_addr_reg_index(t0, ctx);
5073 gen_qemu_st64(cpu_fpr[rd], t0, ctx->mem_idx);
5074 tcg_gen_addi_tl(t1, t0, 8);
5075 gen_qemu_st64(cpu_fpr[(rd + 1) % 32], t1, ctx->mem_idx);
76a66253 5076 if (ra != 0)
01a4afeb
AJ
5077 tcg_gen_mov_tl(cpu_gpr[ra], t0);
5078 tcg_temp_free(t0);
5079 tcg_temp_free(t1);
76a66253
JM
5080}
5081
5082/* stfqx */
5083GEN_HANDLER(stfqx, 0x1F, 0x17, 0x1C, 0x00000001, PPC_POWER2)
5084{
01a4afeb
AJ
5085 int rd = rD(ctx->opcode);
5086 TCGv t0 = tcg_temp_new();
5087 gen_addr_reg_index(t0, ctx);
5088 gen_qemu_st64(cpu_fpr[rd], t0, ctx->mem_idx);
5089 tcg_gen_addi_tl(t0, t0, 8);
5090 gen_qemu_st64(cpu_fpr[(rd + 1) % 32], t0, ctx->mem_idx);
5091 tcg_temp_free(t0);
76a66253
JM
5092}
5093
5094/* BookE specific instructions */
2662a059 5095/* XXX: not implemented on 440 ? */
05332d70 5096GEN_HANDLER(mfapidi, 0x1F, 0x13, 0x08, 0x0000F801, PPC_MFAPIDI)
76a66253
JM
5097{
5098 /* XXX: TODO */
e1833e1f 5099 GEN_EXCP_INVAL(ctx);
76a66253
JM
5100}
5101
2662a059 5102/* XXX: not implemented on 440 ? */
05332d70 5103GEN_HANDLER(tlbiva, 0x1F, 0x12, 0x18, 0x03FFF801, PPC_TLBIVA)
76a66253
JM
5104{
5105#if defined(CONFIG_USER_ONLY)
e1833e1f 5106 GEN_EXCP_PRIVOPC(ctx);
76a66253
JM
5107#else
5108 if (unlikely(!ctx->supervisor)) {
e1833e1f 5109 GEN_EXCP_PRIVOPC(ctx);
76a66253
JM
5110 return;
5111 }
e2be8d8d 5112 gen_addr_reg_index(cpu_T[0], ctx);
76a66253 5113 /* Use the same micro-ops as for tlbie */
d9bce9d9
JM
5114#if defined(TARGET_PPC64)
5115 if (ctx->sf_mode)
5116 gen_op_tlbie_64();
5117 else
5118#endif
5119 gen_op_tlbie();
76a66253
JM
5120#endif
5121}
5122
5123/* All 405 MAC instructions are translated here */
b068d6a7
JM
5124static always_inline void gen_405_mulladd_insn (DisasContext *ctx,
5125 int opc2, int opc3,
5126 int ra, int rb, int rt, int Rc)
76a66253 5127{
182608d4
AJ
5128 TCGv t0, t1;
5129
a7812ae4
PB
5130 t0 = tcg_temp_local_new();
5131 t1 = tcg_temp_local_new();
182608d4 5132
76a66253
JM
5133 switch (opc3 & 0x0D) {
5134 case 0x05:
5135 /* macchw - macchw. - macchwo - macchwo. */
5136 /* macchws - macchws. - macchwso - macchwso. */
5137 /* nmacchw - nmacchw. - nmacchwo - nmacchwo. */
5138 /* nmacchws - nmacchws. - nmacchwso - nmacchwso. */
5139 /* mulchw - mulchw. */
182608d4
AJ
5140 tcg_gen_ext16s_tl(t0, cpu_gpr[ra]);
5141 tcg_gen_sari_tl(t1, cpu_gpr[rb], 16);
5142 tcg_gen_ext16s_tl(t1, t1);
76a66253
JM
5143 break;
5144 case 0x04:
5145 /* macchwu - macchwu. - macchwuo - macchwuo. */
5146 /* macchwsu - macchwsu. - macchwsuo - macchwsuo. */
5147 /* mulchwu - mulchwu. */
182608d4
AJ
5148 tcg_gen_ext16u_tl(t0, cpu_gpr[ra]);
5149 tcg_gen_shri_tl(t1, cpu_gpr[rb], 16);
5150 tcg_gen_ext16u_tl(t1, t1);
76a66253
JM
5151 break;
5152 case 0x01:
5153 /* machhw - machhw. - machhwo - machhwo. */
5154 /* machhws - machhws. - machhwso - machhwso. */
5155 /* nmachhw - nmachhw. - nmachhwo - nmachhwo. */
5156 /* nmachhws - nmachhws. - nmachhwso - nmachhwso. */
5157 /* mulhhw - mulhhw. */
182608d4
AJ
5158 tcg_gen_sari_tl(t0, cpu_gpr[ra], 16);
5159 tcg_gen_ext16s_tl(t0, t0);
5160 tcg_gen_sari_tl(t1, cpu_gpr[rb], 16);
5161 tcg_gen_ext16s_tl(t1, t1);
76a66253
JM
5162 break;
5163 case 0x00:
5164 /* machhwu - machhwu. - machhwuo - machhwuo. */
5165 /* machhwsu - machhwsu. - machhwsuo - machhwsuo. */
5166 /* mulhhwu - mulhhwu. */
182608d4
AJ
5167 tcg_gen_shri_tl(t0, cpu_gpr[ra], 16);
5168 tcg_gen_ext16u_tl(t0, t0);
5169 tcg_gen_shri_tl(t1, cpu_gpr[rb], 16);
5170 tcg_gen_ext16u_tl(t1, t1);
76a66253
JM
5171 break;
5172 case 0x0D:
5173 /* maclhw - maclhw. - maclhwo - maclhwo. */
5174 /* maclhws - maclhws. - maclhwso - maclhwso. */
5175 /* nmaclhw - nmaclhw. - nmaclhwo - nmaclhwo. */
5176 /* nmaclhws - nmaclhws. - nmaclhwso - nmaclhwso. */
5177 /* mullhw - mullhw. */
182608d4
AJ
5178 tcg_gen_ext16s_tl(t0, cpu_gpr[ra]);
5179 tcg_gen_ext16s_tl(t1, cpu_gpr[rb]);
76a66253
JM
5180 break;
5181 case 0x0C:
5182 /* maclhwu - maclhwu. - maclhwuo - maclhwuo. */
5183 /* maclhwsu - maclhwsu. - maclhwsuo - maclhwsuo. */
5184 /* mullhwu - mullhwu. */
182608d4
AJ
5185 tcg_gen_ext16u_tl(t0, cpu_gpr[ra]);
5186 tcg_gen_ext16u_tl(t1, cpu_gpr[rb]);
76a66253
JM
5187 break;
5188 }
76a66253 5189 if (opc2 & 0x04) {
182608d4
AJ
5190 /* (n)multiply-and-accumulate (0x0C / 0x0E) */
5191 tcg_gen_mul_tl(t1, t0, t1);
5192 if (opc2 & 0x02) {
5193 /* nmultiply-and-accumulate (0x0E) */
5194 tcg_gen_sub_tl(t0, cpu_gpr[rt], t1);
5195 } else {
5196 /* multiply-and-accumulate (0x0C) */
5197 tcg_gen_add_tl(t0, cpu_gpr[rt], t1);
5198 }
5199
5200 if (opc3 & 0x12) {
5201 /* Check overflow and/or saturate */
5202 int l1 = gen_new_label();
5203
5204 if (opc3 & 0x10) {
5205 /* Start with XER OV disabled, the most likely case */
5206 tcg_gen_andi_tl(cpu_xer, cpu_xer, ~(1 << XER_OV));
5207 }
5208 if (opc3 & 0x01) {
5209 /* Signed */
5210 tcg_gen_xor_tl(t1, cpu_gpr[rt], t1);
5211 tcg_gen_brcondi_tl(TCG_COND_GE, t1, 0, l1);
5212 tcg_gen_xor_tl(t1, cpu_gpr[rt], t0);
5213 tcg_gen_brcondi_tl(TCG_COND_LT, t1, 0, l1);
bdc4e053 5214 if (opc3 & 0x02) {
182608d4
AJ
5215 /* Saturate */
5216 tcg_gen_sari_tl(t0, cpu_gpr[rt], 31);
5217 tcg_gen_xori_tl(t0, t0, 0x7fffffff);
5218 }
5219 } else {
5220 /* Unsigned */
5221 tcg_gen_brcond_tl(TCG_COND_GEU, t0, t1, l1);
bdc4e053 5222 if (opc3 & 0x02) {
182608d4
AJ
5223 /* Saturate */
5224 tcg_gen_movi_tl(t0, UINT32_MAX);
5225 }
5226 }
5227 if (opc3 & 0x10) {
5228 /* Check overflow */
5229 tcg_gen_ori_tl(cpu_xer, cpu_xer, (1 << XER_OV) | (1 << XER_SO));
5230 }
5231 gen_set_label(l1);
5232 tcg_gen_mov_tl(cpu_gpr[rt], t0);
5233 }
5234 } else {
5235 tcg_gen_mul_tl(cpu_gpr[rt], t0, t1);
76a66253 5236 }
182608d4
AJ
5237 tcg_temp_free(t0);
5238 tcg_temp_free(t1);
76a66253
JM
5239 if (unlikely(Rc) != 0) {
5240 /* Update Rc0 */
182608d4 5241 gen_set_Rc0(ctx, cpu_gpr[rt]);
76a66253
JM
5242 }
5243}
5244
a750fc0b
JM
5245#define GEN_MAC_HANDLER(name, opc2, opc3) \
5246GEN_HANDLER(name, 0x04, opc2, opc3, 0x00000000, PPC_405_MAC) \
76a66253
JM
5247{ \
5248 gen_405_mulladd_insn(ctx, opc2, opc3, rA(ctx->opcode), rB(ctx->opcode), \
5249 rD(ctx->opcode), Rc(ctx->opcode)); \
5250}
5251
5252/* macchw - macchw. */
a750fc0b 5253GEN_MAC_HANDLER(macchw, 0x0C, 0x05);
76a66253 5254/* macchwo - macchwo. */
a750fc0b 5255GEN_MAC_HANDLER(macchwo, 0x0C, 0x15);
76a66253 5256/* macchws - macchws. */
a750fc0b 5257GEN_MAC_HANDLER(macchws, 0x0C, 0x07);
76a66253 5258/* macchwso - macchwso. */
a750fc0b 5259GEN_MAC_HANDLER(macchwso, 0x0C, 0x17);
76a66253 5260/* macchwsu - macchwsu. */
a750fc0b 5261GEN_MAC_HANDLER(macchwsu, 0x0C, 0x06);
76a66253 5262/* macchwsuo - macchwsuo. */
a750fc0b 5263GEN_MAC_HANDLER(macchwsuo, 0x0C, 0x16);
76a66253 5264/* macchwu - macchwu. */
a750fc0b 5265GEN_MAC_HANDLER(macchwu, 0x0C, 0x04);
76a66253 5266/* macchwuo - macchwuo. */
a750fc0b 5267GEN_MAC_HANDLER(macchwuo, 0x0C, 0x14);
76a66253 5268/* machhw - machhw. */
a750fc0b 5269GEN_MAC_HANDLER(machhw, 0x0C, 0x01);
76a66253 5270/* machhwo - machhwo. */
a750fc0b 5271GEN_MAC_HANDLER(machhwo, 0x0C, 0x11);
76a66253 5272/* machhws - machhws. */
a750fc0b 5273GEN_MAC_HANDLER(machhws, 0x0C, 0x03);
76a66253 5274/* machhwso - machhwso. */
a750fc0b 5275GEN_MAC_HANDLER(machhwso, 0x0C, 0x13);
76a66253 5276/* machhwsu - machhwsu. */
a750fc0b 5277GEN_MAC_HANDLER(machhwsu, 0x0C, 0x02);
76a66253 5278/* machhwsuo - machhwsuo. */
a750fc0b 5279GEN_MAC_HANDLER(machhwsuo, 0x0C, 0x12);
76a66253 5280/* machhwu - machhwu. */
a750fc0b 5281GEN_MAC_HANDLER(machhwu, 0x0C, 0x00);
76a66253 5282/* machhwuo - machhwuo. */
a750fc0b 5283GEN_MAC_HANDLER(machhwuo, 0x0C, 0x10);
76a66253 5284/* maclhw - maclhw. */
a750fc0b 5285GEN_MAC_HANDLER(maclhw, 0x0C, 0x0D);
76a66253 5286/* maclhwo - maclhwo. */
a750fc0b 5287GEN_MAC_HANDLER(maclhwo, 0x0C, 0x1D);
76a66253 5288/* maclhws - maclhws. */
a750fc0b 5289GEN_MAC_HANDLER(maclhws, 0x0C, 0x0F);
76a66253 5290/* maclhwso - maclhwso. */
a750fc0b 5291GEN_MAC_HANDLER(maclhwso, 0x0C, 0x1F);
76a66253 5292/* maclhwu - maclhwu. */
a750fc0b 5293GEN_MAC_HANDLER(maclhwu, 0x0C, 0x0C);
76a66253 5294/* maclhwuo - maclhwuo. */
a750fc0b 5295GEN_MAC_HANDLER(maclhwuo, 0x0C, 0x1C);
76a66253 5296/* maclhwsu - maclhwsu. */
a750fc0b 5297GEN_MAC_HANDLER(maclhwsu, 0x0C, 0x0E);
76a66253 5298/* maclhwsuo - maclhwsuo. */
a750fc0b 5299GEN_MAC_HANDLER(maclhwsuo, 0x0C, 0x1E);
76a66253 5300/* nmacchw - nmacchw. */
a750fc0b 5301GEN_MAC_HANDLER(nmacchw, 0x0E, 0x05);
76a66253 5302/* nmacchwo - nmacchwo. */
a750fc0b 5303GEN_MAC_HANDLER(nmacchwo, 0x0E, 0x15);
76a66253 5304/* nmacchws - nmacchws. */
a750fc0b 5305GEN_MAC_HANDLER(nmacchws, 0x0E, 0x07);
76a66253 5306/* nmacchwso - nmacchwso. */
a750fc0b 5307GEN_MAC_HANDLER(nmacchwso, 0x0E, 0x17);
76a66253 5308/* nmachhw - nmachhw. */
a750fc0b 5309GEN_MAC_HANDLER(nmachhw, 0x0E, 0x01);
76a66253 5310/* nmachhwo - nmachhwo. */
a750fc0b 5311GEN_MAC_HANDLER(nmachhwo, 0x0E, 0x11);
76a66253 5312/* nmachhws - nmachhws. */
a750fc0b 5313GEN_MAC_HANDLER(nmachhws, 0x0E, 0x03);
76a66253 5314/* nmachhwso - nmachhwso. */
a750fc0b 5315GEN_MAC_HANDLER(nmachhwso, 0x0E, 0x13);
76a66253 5316/* nmaclhw - nmaclhw. */
a750fc0b 5317GEN_MAC_HANDLER(nmaclhw, 0x0E, 0x0D);
76a66253 5318/* nmaclhwo - nmaclhwo. */
a750fc0b 5319GEN_MAC_HANDLER(nmaclhwo, 0x0E, 0x1D);
76a66253 5320/* nmaclhws - nmaclhws. */
a750fc0b 5321GEN_MAC_HANDLER(nmaclhws, 0x0E, 0x0F);
76a66253 5322/* nmaclhwso - nmaclhwso. */
a750fc0b 5323GEN_MAC_HANDLER(nmaclhwso, 0x0E, 0x1F);
76a66253
JM
5324
5325/* mulchw - mulchw. */
a750fc0b 5326GEN_MAC_HANDLER(mulchw, 0x08, 0x05);
76a66253 5327/* mulchwu - mulchwu. */
a750fc0b 5328GEN_MAC_HANDLER(mulchwu, 0x08, 0x04);
76a66253 5329/* mulhhw - mulhhw. */
a750fc0b 5330GEN_MAC_HANDLER(mulhhw, 0x08, 0x01);
76a66253 5331/* mulhhwu - mulhhwu. */
a750fc0b 5332GEN_MAC_HANDLER(mulhhwu, 0x08, 0x00);
76a66253 5333/* mullhw - mullhw. */
a750fc0b 5334GEN_MAC_HANDLER(mullhw, 0x08, 0x0D);
76a66253 5335/* mullhwu - mullhwu. */
a750fc0b 5336GEN_MAC_HANDLER(mullhwu, 0x08, 0x0C);
76a66253
JM
5337
5338/* mfdcr */
05332d70 5339GEN_HANDLER(mfdcr, 0x1F, 0x03, 0x0A, 0x00000001, PPC_DCR)
76a66253
JM
5340{
5341#if defined(CONFIG_USER_ONLY)
e1833e1f 5342 GEN_EXCP_PRIVREG(ctx);
76a66253
JM
5343#else
5344 uint32_t dcrn = SPR(ctx->opcode);
5345
5346 if (unlikely(!ctx->supervisor)) {
e1833e1f 5347 GEN_EXCP_PRIVREG(ctx);
76a66253
JM
5348 return;
5349 }
86c581dc 5350 tcg_gen_movi_tl(cpu_T[0], dcrn);
a42bd6cc 5351 gen_op_load_dcr();
f78fb44e 5352 tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_T[0]);
76a66253
JM
5353#endif
5354}
5355
5356/* mtdcr */
05332d70 5357GEN_HANDLER(mtdcr, 0x1F, 0x03, 0x0E, 0x00000001, PPC_DCR)
76a66253
JM
5358{
5359#if defined(CONFIG_USER_ONLY)
e1833e1f 5360 GEN_EXCP_PRIVREG(ctx);
76a66253
JM
5361#else
5362 uint32_t dcrn = SPR(ctx->opcode);
5363
5364 if (unlikely(!ctx->supervisor)) {
e1833e1f 5365 GEN_EXCP_PRIVREG(ctx);
76a66253
JM
5366 return;
5367 }
86c581dc 5368 tcg_gen_movi_tl(cpu_T[0], dcrn);
f78fb44e 5369 tcg_gen_mov_tl(cpu_T[1], cpu_gpr[rS(ctx->opcode)]);
a42bd6cc
JM
5370 gen_op_store_dcr();
5371#endif
5372}
5373
5374/* mfdcrx */
2662a059 5375/* XXX: not implemented on 440 ? */
05332d70 5376GEN_HANDLER(mfdcrx, 0x1F, 0x03, 0x08, 0x00000000, PPC_DCRX)
a42bd6cc
JM
5377{
5378#if defined(CONFIG_USER_ONLY)
e1833e1f 5379 GEN_EXCP_PRIVREG(ctx);
a42bd6cc
JM
5380#else
5381 if (unlikely(!ctx->supervisor)) {
e1833e1f 5382 GEN_EXCP_PRIVREG(ctx);
a42bd6cc
JM
5383 return;
5384 }
f78fb44e 5385 tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rA(ctx->opcode)]);
a42bd6cc 5386 gen_op_load_dcr();
f78fb44e 5387 tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_T[0]);
a750fc0b 5388 /* Note: Rc update flag set leads to undefined state of Rc0 */
a42bd6cc
JM
5389#endif
5390}
5391
5392/* mtdcrx */
2662a059 5393/* XXX: not implemented on 440 ? */
05332d70 5394GEN_HANDLER(mtdcrx, 0x1F, 0x03, 0x0C, 0x00000000, PPC_DCRX)
a42bd6cc
JM
5395{
5396#if defined(CONFIG_USER_ONLY)
e1833e1f 5397 GEN_EXCP_PRIVREG(ctx);
a42bd6cc
JM
5398#else
5399 if (unlikely(!ctx->supervisor)) {
e1833e1f 5400 GEN_EXCP_PRIVREG(ctx);
a42bd6cc
JM
5401 return;
5402 }
f78fb44e
AJ
5403 tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rA(ctx->opcode)]);
5404 tcg_gen_mov_tl(cpu_T[1], cpu_gpr[rS(ctx->opcode)]);
a42bd6cc 5405 gen_op_store_dcr();
a750fc0b 5406 /* Note: Rc update flag set leads to undefined state of Rc0 */
76a66253
JM
5407#endif
5408}
5409
a750fc0b
JM
5410/* mfdcrux (PPC 460) : user-mode access to DCR */
5411GEN_HANDLER(mfdcrux, 0x1F, 0x03, 0x09, 0x00000000, PPC_DCRUX)
5412{
f78fb44e 5413 tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rA(ctx->opcode)]);
a750fc0b 5414 gen_op_load_dcr();
f78fb44e 5415 tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_T[0]);
a750fc0b
JM
5416 /* Note: Rc update flag set leads to undefined state of Rc0 */
5417}
5418
5419/* mtdcrux (PPC 460) : user-mode access to DCR */
5420GEN_HANDLER(mtdcrux, 0x1F, 0x03, 0x0D, 0x00000000, PPC_DCRUX)
5421{
f78fb44e
AJ
5422 tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rA(ctx->opcode)]);
5423 tcg_gen_mov_tl(cpu_T[1], cpu_gpr[rS(ctx->opcode)]);
a750fc0b
JM
5424 gen_op_store_dcr();
5425 /* Note: Rc update flag set leads to undefined state of Rc0 */
5426}
5427
76a66253
JM
5428/* dccci */
5429GEN_HANDLER(dccci, 0x1F, 0x06, 0x0E, 0x03E00001, PPC_4xx_COMMON)
5430{
5431#if defined(CONFIG_USER_ONLY)
e1833e1f 5432 GEN_EXCP_PRIVOPC(ctx);
76a66253
JM
5433#else
5434 if (unlikely(!ctx->supervisor)) {
e1833e1f 5435 GEN_EXCP_PRIVOPC(ctx);
76a66253
JM
5436 return;
5437 }
5438 /* interpreted as no-op */
5439#endif
5440}
5441
5442/* dcread */
5443GEN_HANDLER(dcread, 0x1F, 0x06, 0x0F, 0x00000001, PPC_4xx_COMMON)
5444{
5445#if defined(CONFIG_USER_ONLY)
e1833e1f 5446 GEN_EXCP_PRIVOPC(ctx);
76a66253 5447#else
b61f2753 5448 TCGv EA, val;
76a66253 5449 if (unlikely(!ctx->supervisor)) {
e1833e1f 5450 GEN_EXCP_PRIVOPC(ctx);
76a66253
JM
5451 return;
5452 }
a7812ae4 5453 EA = tcg_temp_new();
a7859e89 5454 gen_set_access_type(ACCESS_CACHE);
b61f2753 5455 gen_addr_reg_index(EA, ctx);
a7812ae4 5456 val = tcg_temp_new();
b61f2753
AJ
5457 gen_qemu_ld32u(val, EA, ctx->mem_idx);
5458 tcg_temp_free(val);
5459 tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], EA);
5460 tcg_temp_free(EA);
76a66253
JM
5461#endif
5462}
5463
5464/* icbt */
c7697e1f 5465GEN_HANDLER2(icbt_40x, "icbt", 0x1F, 0x06, 0x08, 0x03E00001, PPC_40x_ICBT)
76a66253
JM
5466{
5467 /* interpreted as no-op */
5468 /* XXX: specification say this is treated as a load by the MMU
5469 * but does not generate any exception
5470 */
5471}
5472
5473/* iccci */
5474GEN_HANDLER(iccci, 0x1F, 0x06, 0x1E, 0x00000001, PPC_4xx_COMMON)
5475{
5476#if defined(CONFIG_USER_ONLY)
e1833e1f 5477 GEN_EXCP_PRIVOPC(ctx);
76a66253
JM
5478#else
5479 if (unlikely(!ctx->supervisor)) {
e1833e1f 5480 GEN_EXCP_PRIVOPC(ctx);
76a66253
JM
5481 return;
5482 }
5483 /* interpreted as no-op */
5484#endif
5485}
5486
5487/* icread */
5488GEN_HANDLER(icread, 0x1F, 0x06, 0x1F, 0x03E00001, PPC_4xx_COMMON)
5489{
5490#if defined(CONFIG_USER_ONLY)
e1833e1f 5491 GEN_EXCP_PRIVOPC(ctx);
76a66253
JM
5492#else
5493 if (unlikely(!ctx->supervisor)) {
e1833e1f 5494 GEN_EXCP_PRIVOPC(ctx);
76a66253
JM
5495 return;
5496 }
5497 /* interpreted as no-op */
5498#endif
5499}
5500
5501/* rfci (supervisor only) */
c7697e1f 5502GEN_HANDLER2(rfci_40x, "rfci", 0x13, 0x13, 0x01, 0x03FF8001, PPC_40x_EXCP)
a42bd6cc
JM
5503{
5504#if defined(CONFIG_USER_ONLY)
e1833e1f 5505 GEN_EXCP_PRIVOPC(ctx);
a42bd6cc
JM
5506#else
5507 if (unlikely(!ctx->supervisor)) {
e1833e1f 5508 GEN_EXCP_PRIVOPC(ctx);
a42bd6cc
JM
5509 return;
5510 }
5511 /* Restore CPU state */
5512 gen_op_40x_rfci();
e1833e1f 5513 GEN_SYNC(ctx);
a42bd6cc
JM
5514#endif
5515}
5516
5517GEN_HANDLER(rfci, 0x13, 0x13, 0x01, 0x03FF8001, PPC_BOOKE)
5518{
5519#if defined(CONFIG_USER_ONLY)
e1833e1f 5520 GEN_EXCP_PRIVOPC(ctx);
a42bd6cc
JM
5521#else
5522 if (unlikely(!ctx->supervisor)) {
e1833e1f 5523 GEN_EXCP_PRIVOPC(ctx);
a42bd6cc
JM
5524 return;
5525 }
5526 /* Restore CPU state */
5527 gen_op_rfci();
e1833e1f 5528 GEN_SYNC(ctx);
a42bd6cc
JM
5529#endif
5530}
5531
5532/* BookE specific */
2662a059 5533/* XXX: not implemented on 440 ? */
05332d70 5534GEN_HANDLER(rfdi, 0x13, 0x07, 0x01, 0x03FF8001, PPC_RFDI)
76a66253
JM
5535{
5536#if defined(CONFIG_USER_ONLY)
e1833e1f 5537 GEN_EXCP_PRIVOPC(ctx);
76a66253
JM
5538#else
5539 if (unlikely(!ctx->supervisor)) {
e1833e1f 5540 GEN_EXCP_PRIVOPC(ctx);
76a66253
JM
5541 return;
5542 }
5543 /* Restore CPU state */
a42bd6cc 5544 gen_op_rfdi();
e1833e1f 5545 GEN_SYNC(ctx);
76a66253
JM
5546#endif
5547}
5548
2662a059 5549/* XXX: not implemented on 440 ? */
a750fc0b 5550GEN_HANDLER(rfmci, 0x13, 0x06, 0x01, 0x03FF8001, PPC_RFMCI)
a42bd6cc
JM
5551{
5552#if defined(CONFIG_USER_ONLY)
e1833e1f 5553 GEN_EXCP_PRIVOPC(ctx);
a42bd6cc
JM
5554#else
5555 if (unlikely(!ctx->supervisor)) {
e1833e1f 5556 GEN_EXCP_PRIVOPC(ctx);
a42bd6cc
JM
5557 return;
5558 }
5559 /* Restore CPU state */
5560 gen_op_rfmci();
e1833e1f 5561 GEN_SYNC(ctx);
a42bd6cc
JM
5562#endif
5563}
5eb7995e 5564
d9bce9d9 5565/* TLB management - PowerPC 405 implementation */
76a66253 5566/* tlbre */
c7697e1f 5567GEN_HANDLER2(tlbre_40x, "tlbre", 0x1F, 0x12, 0x1D, 0x00000001, PPC_40x_TLB)
76a66253
JM
5568{
5569#if defined(CONFIG_USER_ONLY)
e1833e1f 5570 GEN_EXCP_PRIVOPC(ctx);
76a66253
JM
5571#else
5572 if (unlikely(!ctx->supervisor)) {
e1833e1f 5573 GEN_EXCP_PRIVOPC(ctx);
76a66253
JM
5574 return;
5575 }
5576 switch (rB(ctx->opcode)) {
5577 case 0:
f78fb44e 5578 tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rA(ctx->opcode)]);
76a66253 5579 gen_op_4xx_tlbre_hi();
f78fb44e 5580 tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_T[0]);
76a66253
JM
5581 break;
5582 case 1:
f78fb44e 5583 tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rA(ctx->opcode)]);
76a66253 5584 gen_op_4xx_tlbre_lo();
f78fb44e 5585 tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_T[0]);
76a66253
JM
5586 break;
5587 default:
e1833e1f 5588 GEN_EXCP_INVAL(ctx);
76a66253 5589 break;
9a64fbe4 5590 }
76a66253
JM
5591#endif
5592}
5593
d9bce9d9 5594/* tlbsx - tlbsx. */
c7697e1f 5595GEN_HANDLER2(tlbsx_40x, "tlbsx", 0x1F, 0x12, 0x1C, 0x00000000, PPC_40x_TLB)
76a66253
JM
5596{
5597#if defined(CONFIG_USER_ONLY)
e1833e1f 5598 GEN_EXCP_PRIVOPC(ctx);
76a66253
JM
5599#else
5600 if (unlikely(!ctx->supervisor)) {
e1833e1f 5601 GEN_EXCP_PRIVOPC(ctx);
76a66253
JM
5602 return;
5603 }
e2be8d8d 5604 gen_addr_reg_index(cpu_T[0], ctx);
daf4f96e 5605 gen_op_4xx_tlbsx();
76a66253 5606 if (Rc(ctx->opcode))
daf4f96e 5607 gen_op_4xx_tlbsx_check();
f78fb44e 5608 tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_T[0]);
76a66253 5609#endif
79aceca5
FB
5610}
5611
76a66253 5612/* tlbwe */
c7697e1f 5613GEN_HANDLER2(tlbwe_40x, "tlbwe", 0x1F, 0x12, 0x1E, 0x00000001, PPC_40x_TLB)
79aceca5 5614{
76a66253 5615#if defined(CONFIG_USER_ONLY)
e1833e1f 5616 GEN_EXCP_PRIVOPC(ctx);
76a66253
JM
5617#else
5618 if (unlikely(!ctx->supervisor)) {
e1833e1f 5619 GEN_EXCP_PRIVOPC(ctx);
76a66253
JM
5620 return;
5621 }
5622 switch (rB(ctx->opcode)) {
5623 case 0:
f78fb44e
AJ
5624 tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rA(ctx->opcode)]);
5625 tcg_gen_mov_tl(cpu_T[1], cpu_gpr[rS(ctx->opcode)]);
76a66253
JM
5626 gen_op_4xx_tlbwe_hi();
5627 break;
5628 case 1:
f78fb44e
AJ
5629 tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rA(ctx->opcode)]);
5630 tcg_gen_mov_tl(cpu_T[1], cpu_gpr[rS(ctx->opcode)]);
76a66253
JM
5631 gen_op_4xx_tlbwe_lo();
5632 break;
5633 default:
e1833e1f 5634 GEN_EXCP_INVAL(ctx);
76a66253 5635 break;
9a64fbe4 5636 }
76a66253
JM
5637#endif
5638}
5639
a4bb6c3e 5640/* TLB management - PowerPC 440 implementation */
5eb7995e 5641/* tlbre */
c7697e1f 5642GEN_HANDLER2(tlbre_440, "tlbre", 0x1F, 0x12, 0x1D, 0x00000001, PPC_BOOKE)
5eb7995e
JM
5643{
5644#if defined(CONFIG_USER_ONLY)
e1833e1f 5645 GEN_EXCP_PRIVOPC(ctx);
5eb7995e
JM
5646#else
5647 if (unlikely(!ctx->supervisor)) {
e1833e1f 5648 GEN_EXCP_PRIVOPC(ctx);
5eb7995e
JM
5649 return;
5650 }
5651 switch (rB(ctx->opcode)) {
5652 case 0:
5eb7995e 5653 case 1:
5eb7995e 5654 case 2:
f78fb44e 5655 tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rA(ctx->opcode)]);
a4bb6c3e 5656 gen_op_440_tlbre(rB(ctx->opcode));
f78fb44e 5657 tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_T[0]);
5eb7995e
JM
5658 break;
5659 default:
e1833e1f 5660 GEN_EXCP_INVAL(ctx);
5eb7995e
JM
5661 break;
5662 }
5663#endif
5664}
5665
5666/* tlbsx - tlbsx. */
c7697e1f 5667GEN_HANDLER2(tlbsx_440, "tlbsx", 0x1F, 0x12, 0x1C, 0x00000000, PPC_BOOKE)
5eb7995e
JM
5668{
5669#if defined(CONFIG_USER_ONLY)
e1833e1f 5670 GEN_EXCP_PRIVOPC(ctx);
5eb7995e
JM
5671#else
5672 if (unlikely(!ctx->supervisor)) {
e1833e1f 5673 GEN_EXCP_PRIVOPC(ctx);
5eb7995e
JM
5674 return;
5675 }
e2be8d8d 5676 gen_addr_reg_index(cpu_T[0], ctx);
daf4f96e 5677 gen_op_440_tlbsx();
5eb7995e 5678 if (Rc(ctx->opcode))
daf4f96e 5679 gen_op_4xx_tlbsx_check();
f78fb44e 5680 tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_T[0]);
5eb7995e
JM
5681#endif
5682}
5683
5684/* tlbwe */
c7697e1f 5685GEN_HANDLER2(tlbwe_440, "tlbwe", 0x1F, 0x12, 0x1E, 0x00000001, PPC_BOOKE)
5eb7995e
JM
5686{
5687#if defined(CONFIG_USER_ONLY)
e1833e1f 5688 GEN_EXCP_PRIVOPC(ctx);
5eb7995e
JM
5689#else
5690 if (unlikely(!ctx->supervisor)) {
e1833e1f 5691 GEN_EXCP_PRIVOPC(ctx);
5eb7995e
JM
5692 return;
5693 }
5694 switch (rB(ctx->opcode)) {
5695 case 0:
5eb7995e 5696 case 1:
5eb7995e 5697 case 2:
f78fb44e
AJ
5698 tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rA(ctx->opcode)]);
5699 tcg_gen_mov_tl(cpu_T[1], cpu_gpr[rS(ctx->opcode)]);
a4bb6c3e 5700 gen_op_440_tlbwe(rB(ctx->opcode));
5eb7995e
JM
5701 break;
5702 default:
e1833e1f 5703 GEN_EXCP_INVAL(ctx);
5eb7995e
JM
5704 break;
5705 }
5706#endif
5707}
5708
76a66253 5709/* wrtee */
05332d70 5710GEN_HANDLER(wrtee, 0x1F, 0x03, 0x04, 0x000FFC01, PPC_WRTEE)
76a66253
JM
5711{
5712#if defined(CONFIG_USER_ONLY)
e1833e1f 5713 GEN_EXCP_PRIVOPC(ctx);
76a66253
JM
5714#else
5715 if (unlikely(!ctx->supervisor)) {
e1833e1f 5716 GEN_EXCP_PRIVOPC(ctx);
76a66253
JM
5717 return;
5718 }
f78fb44e 5719 tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rD(ctx->opcode)]);
a42bd6cc 5720 gen_op_wrte();
dee96f6c
JM
5721 /* Stop translation to have a chance to raise an exception
5722 * if we just set msr_ee to 1
5723 */
e1833e1f 5724 GEN_STOP(ctx);
76a66253
JM
5725#endif
5726}
5727
5728/* wrteei */
05332d70 5729GEN_HANDLER(wrteei, 0x1F, 0x03, 0x05, 0x000EFC01, PPC_WRTEE)
76a66253
JM
5730{
5731#if defined(CONFIG_USER_ONLY)
e1833e1f 5732 GEN_EXCP_PRIVOPC(ctx);
76a66253
JM
5733#else
5734 if (unlikely(!ctx->supervisor)) {
e1833e1f 5735 GEN_EXCP_PRIVOPC(ctx);
76a66253
JM
5736 return;
5737 }
86c581dc 5738 tcg_gen_movi_tl(cpu_T[0], ctx->opcode & 0x00010000);
a42bd6cc 5739 gen_op_wrte();
dee96f6c
JM
5740 /* Stop translation to have a chance to raise an exception
5741 * if we just set msr_ee to 1
5742 */
e1833e1f 5743 GEN_STOP(ctx);
76a66253
JM
5744#endif
5745}
5746
08e46e54 5747/* PowerPC 440 specific instructions */
76a66253
JM
5748/* dlmzb */
5749GEN_HANDLER(dlmzb, 0x1F, 0x0E, 0x02, 0x00000000, PPC_440_SPEC)
5750{
f78fb44e
AJ
5751 tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rS(ctx->opcode)]);
5752 tcg_gen_mov_tl(cpu_T[1], cpu_gpr[rB(ctx->opcode)]);
76a66253 5753 gen_op_440_dlmzb();
f78fb44e 5754 tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], cpu_T[0]);
3d7b417e
AJ
5755 tcg_gen_andi_tl(cpu_xer, cpu_xer, ~0x7F);
5756 tcg_gen_or_tl(cpu_xer, cpu_xer, cpu_T[0]);
76a66253
JM
5757 if (Rc(ctx->opcode)) {
5758 gen_op_440_dlmzb_update_Rc();
a7812ae4
PB
5759 tcg_gen_trunc_tl_i32(cpu_crf[0], cpu_T[0]);
5760 tcg_gen_andi_i32(cpu_crf[0], cpu_crf[0], 0xf);
76a66253
JM
5761 }
5762}
5763
5764/* mbar replaces eieio on 440 */
5765GEN_HANDLER(mbar, 0x1F, 0x16, 0x13, 0x001FF801, PPC_BOOKE)
5766{
5767 /* interpreted as no-op */
5768}
5769
5770/* msync replaces sync on 440 */
0db1b20e 5771GEN_HANDLER(msync, 0x1F, 0x16, 0x12, 0x03FFF801, PPC_BOOKE)
76a66253
JM
5772{
5773 /* interpreted as no-op */
5774}
5775
5776/* icbt */
c7697e1f 5777GEN_HANDLER2(icbt_440, "icbt", 0x1F, 0x16, 0x00, 0x03E00001, PPC_BOOKE)
76a66253
JM
5778{
5779 /* interpreted as no-op */
5780 /* XXX: specification say this is treated as a load by the MMU
5781 * but does not generate any exception
5782 */
79aceca5
FB
5783}
5784
a9d9eb8f
JM
5785/*** Altivec vector extension ***/
5786/* Altivec registers moves */
a9d9eb8f 5787
a9d9eb8f 5788#define GEN_VR_LDX(name, opc2, opc3) \
fe1e5c53 5789GEN_HANDLER(name, 0x1F, opc2, opc3, 0x00000001, PPC_ALTIVEC) \
a9d9eb8f 5790{ \
fe1e5c53 5791 TCGv EA; \
a9d9eb8f
JM
5792 if (unlikely(!ctx->altivec_enabled)) { \
5793 GEN_EXCP_NO_VR(ctx); \
5794 return; \
5795 } \
fe1e5c53
AJ
5796 EA = tcg_temp_new(); \
5797 gen_addr_reg_index(EA, ctx); \
5798 tcg_gen_andi_tl(EA, EA, ~0xf); \
5799 if (ctx->mem_idx & 1) { \
5800 gen_qemu_ld64(cpu_avrl[rD(ctx->opcode)], EA, ctx->mem_idx); \
5801 tcg_gen_addi_tl(EA, EA, 8); \
5802 gen_qemu_ld64(cpu_avrh[rD(ctx->opcode)], EA, ctx->mem_idx); \
5803 } else { \
5804 gen_qemu_ld64(cpu_avrh[rD(ctx->opcode)], EA, ctx->mem_idx); \
5805 tcg_gen_addi_tl(EA, EA, 8); \
5806 gen_qemu_ld64(cpu_avrl[rD(ctx->opcode)], EA, ctx->mem_idx); \
5807 } \
5808 tcg_temp_free(EA); \
a9d9eb8f
JM
5809}
5810
5811#define GEN_VR_STX(name, opc2, opc3) \
5812GEN_HANDLER(st##name, 0x1F, opc2, opc3, 0x00000001, PPC_ALTIVEC) \
5813{ \
fe1e5c53 5814 TCGv EA; \
a9d9eb8f
JM
5815 if (unlikely(!ctx->altivec_enabled)) { \
5816 GEN_EXCP_NO_VR(ctx); \
5817 return; \
5818 } \
fe1e5c53
AJ
5819 EA = tcg_temp_new(); \
5820 gen_addr_reg_index(EA, ctx); \
5821 tcg_gen_andi_tl(EA, EA, ~0xf); \
5822 if (ctx->mem_idx & 1) { \
5823 gen_qemu_st64(cpu_avrl[rD(ctx->opcode)], EA, ctx->mem_idx); \
5824 tcg_gen_addi_tl(EA, EA, 8); \
5825 gen_qemu_st64(cpu_avrh[rD(ctx->opcode)], EA, ctx->mem_idx); \
5826 } else { \
5827 gen_qemu_st64(cpu_avrh[rD(ctx->opcode)], EA, ctx->mem_idx); \
5828 tcg_gen_addi_tl(EA, EA, 8); \
5829 gen_qemu_st64(cpu_avrl[rD(ctx->opcode)], EA, ctx->mem_idx); \
5830 } \
5831 tcg_temp_free(EA); \
a9d9eb8f
JM
5832}
5833
fe1e5c53 5834GEN_VR_LDX(lvx, 0x07, 0x03);
a9d9eb8f 5835/* As we don't emulate the cache, lvxl is stricly equivalent to lvx */
fe1e5c53 5836GEN_VR_LDX(lvxl, 0x07, 0x0B);
a9d9eb8f 5837
fe1e5c53 5838GEN_VR_STX(svx, 0x07, 0x07);
a9d9eb8f 5839/* As we don't emulate the cache, stvxl is stricly equivalent to stvx */
fe1e5c53 5840GEN_VR_STX(svxl, 0x07, 0x0F);
a9d9eb8f 5841
0487d6a8 5842/*** SPE extension ***/
0487d6a8 5843/* Register moves */
3cd7d1dd 5844
a7812ae4 5845static always_inline void gen_load_gpr64(TCGv_i64 t, int reg) {
f78fb44e
AJ
5846#if defined(TARGET_PPC64)
5847 tcg_gen_mov_i64(t, cpu_gpr[reg]);
5848#else
36aa55dc 5849 tcg_gen_concat_i32_i64(t, cpu_gpr[reg], cpu_gprh[reg]);
3cd7d1dd 5850#endif
f78fb44e 5851}
3cd7d1dd 5852
a7812ae4 5853static always_inline void gen_store_gpr64(int reg, TCGv_i64 t) {
f78fb44e
AJ
5854#if defined(TARGET_PPC64)
5855 tcg_gen_mov_i64(cpu_gpr[reg], t);
5856#else
a7812ae4 5857 TCGv_i64 tmp = tcg_temp_new_i64();
f78fb44e 5858 tcg_gen_trunc_i64_i32(cpu_gpr[reg], t);
f78fb44e
AJ
5859 tcg_gen_shri_i64(tmp, t, 32);
5860 tcg_gen_trunc_i64_i32(cpu_gprh[reg], tmp);
a7812ae4 5861 tcg_temp_free_i64(tmp);
3cd7d1dd 5862#endif
f78fb44e 5863}
3cd7d1dd 5864
0487d6a8
JM
5865#define GEN_SPE(name0, name1, opc2, opc3, inval, type) \
5866GEN_HANDLER(name0##_##name1, 0x04, opc2, opc3, inval, type) \
5867{ \
5868 if (Rc(ctx->opcode)) \
5869 gen_##name1(ctx); \
5870 else \
5871 gen_##name0(ctx); \
5872}
5873
5874/* Handler for undefined SPE opcodes */
b068d6a7 5875static always_inline void gen_speundef (DisasContext *ctx)
0487d6a8 5876{
e1833e1f 5877 GEN_EXCP_INVAL(ctx);
0487d6a8
JM
5878}
5879
57951c27
AJ
5880/* SPE logic */
5881#if defined(TARGET_PPC64)
5882#define GEN_SPEOP_LOGIC2(name, tcg_op) \
b068d6a7 5883static always_inline void gen_##name (DisasContext *ctx) \
0487d6a8
JM
5884{ \
5885 if (unlikely(!ctx->spe_enabled)) { \
e1833e1f 5886 GEN_EXCP_NO_AP(ctx); \
0487d6a8
JM
5887 return; \
5888 } \
57951c27
AJ
5889 tcg_op(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)], \
5890 cpu_gpr[rB(ctx->opcode)]); \
5891}
5892#else
5893#define GEN_SPEOP_LOGIC2(name, tcg_op) \
5894static always_inline void gen_##name (DisasContext *ctx) \
5895{ \
5896 if (unlikely(!ctx->spe_enabled)) { \
5897 GEN_EXCP_NO_AP(ctx); \
5898 return; \
5899 } \
5900 tcg_op(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)], \
5901 cpu_gpr[rB(ctx->opcode)]); \
5902 tcg_op(cpu_gprh[rD(ctx->opcode)], cpu_gprh[rA(ctx->opcode)], \
5903 cpu_gprh[rB(ctx->opcode)]); \
0487d6a8 5904}
57951c27
AJ
5905#endif
5906
5907GEN_SPEOP_LOGIC2(evand, tcg_gen_and_tl);
5908GEN_SPEOP_LOGIC2(evandc, tcg_gen_andc_tl);
5909GEN_SPEOP_LOGIC2(evxor, tcg_gen_xor_tl);
5910GEN_SPEOP_LOGIC2(evor, tcg_gen_or_tl);
5911GEN_SPEOP_LOGIC2(evnor, tcg_gen_nor_tl);
5912GEN_SPEOP_LOGIC2(eveqv, tcg_gen_eqv_tl);
5913GEN_SPEOP_LOGIC2(evorc, tcg_gen_orc_tl);
5914GEN_SPEOP_LOGIC2(evnand, tcg_gen_nand_tl);
0487d6a8 5915
57951c27
AJ
5916/* SPE logic immediate */
5917#if defined(TARGET_PPC64)
5918#define GEN_SPEOP_TCG_LOGIC_IMM2(name, tcg_opi) \
3d3a6a0a
AJ
5919static always_inline void gen_##name (DisasContext *ctx) \
5920{ \
5921 if (unlikely(!ctx->spe_enabled)) { \
5922 GEN_EXCP_NO_AP(ctx); \
5923 return; \
5924 } \
a7812ae4
PB
5925 TCGv_i32 t0 = tcg_temp_local_new_i32(); \
5926 TCGv_i32 t1 = tcg_temp_local_new_i32(); \
5927 TCGv_i64 t2 = tcg_temp_local_new_i64(); \
57951c27
AJ
5928 tcg_gen_trunc_i64_i32(t0, cpu_gpr[rA(ctx->opcode)]); \
5929 tcg_opi(t0, t0, rB(ctx->opcode)); \
5930 tcg_gen_shri_i64(t2, cpu_gpr[rA(ctx->opcode)], 32); \
5931 tcg_gen_trunc_i64_i32(t1, t2); \
a7812ae4 5932 tcg_temp_free_i64(t2); \
57951c27
AJ
5933 tcg_opi(t1, t1, rB(ctx->opcode)); \
5934 tcg_gen_concat_i32_i64(cpu_gpr[rD(ctx->opcode)], t0, t1); \
a7812ae4
PB
5935 tcg_temp_free_i32(t0); \
5936 tcg_temp_free_i32(t1); \
3d3a6a0a 5937}
57951c27
AJ
5938#else
5939#define GEN_SPEOP_TCG_LOGIC_IMM2(name, tcg_opi) \
b068d6a7 5940static always_inline void gen_##name (DisasContext *ctx) \
0487d6a8
JM
5941{ \
5942 if (unlikely(!ctx->spe_enabled)) { \
e1833e1f 5943 GEN_EXCP_NO_AP(ctx); \
0487d6a8
JM
5944 return; \
5945 } \
57951c27
AJ
5946 tcg_opi(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)], \
5947 rB(ctx->opcode)); \
5948 tcg_opi(cpu_gprh[rD(ctx->opcode)], cpu_gprh[rA(ctx->opcode)], \
5949 rB(ctx->opcode)); \
0487d6a8 5950}
57951c27
AJ
5951#endif
5952GEN_SPEOP_TCG_LOGIC_IMM2(evslwi, tcg_gen_shli_i32);
5953GEN_SPEOP_TCG_LOGIC_IMM2(evsrwiu, tcg_gen_shri_i32);
5954GEN_SPEOP_TCG_LOGIC_IMM2(evsrwis, tcg_gen_sari_i32);
5955GEN_SPEOP_TCG_LOGIC_IMM2(evrlwi, tcg_gen_rotli_i32);
0487d6a8 5956
57951c27
AJ
5957/* SPE arithmetic */
5958#if defined(TARGET_PPC64)
5959#define GEN_SPEOP_ARITH1(name, tcg_op) \
b068d6a7 5960static always_inline void gen_##name (DisasContext *ctx) \
0487d6a8
JM
5961{ \
5962 if (unlikely(!ctx->spe_enabled)) { \
e1833e1f 5963 GEN_EXCP_NO_AP(ctx); \
0487d6a8
JM
5964 return; \
5965 } \
a7812ae4
PB
5966 TCGv_i32 t0 = tcg_temp_local_new_i32(); \
5967 TCGv_i32 t1 = tcg_temp_local_new_i32(); \
5968 TCGv_i64 t2 = tcg_temp_local_new_i64(); \
57951c27
AJ
5969 tcg_gen_trunc_i64_i32(t0, cpu_gpr[rA(ctx->opcode)]); \
5970 tcg_op(t0, t0); \
5971 tcg_gen_shri_i64(t2, cpu_gpr[rA(ctx->opcode)], 32); \
5972 tcg_gen_trunc_i64_i32(t1, t2); \
a7812ae4 5973 tcg_temp_free_i64(t2); \
57951c27
AJ
5974 tcg_op(t1, t1); \
5975 tcg_gen_concat_i32_i64(cpu_gpr[rD(ctx->opcode)], t0, t1); \
a7812ae4
PB
5976 tcg_temp_free_i32(t0); \
5977 tcg_temp_free_i32(t1); \
0487d6a8 5978}
57951c27 5979#else
a7812ae4 5980#define GEN_SPEOP_ARITH1(name, tcg_op) \
57951c27
AJ
5981static always_inline void gen_##name (DisasContext *ctx) \
5982{ \
5983 if (unlikely(!ctx->spe_enabled)) { \
5984 GEN_EXCP_NO_AP(ctx); \
5985 return; \
5986 } \
5987 tcg_op(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)]); \
5988 tcg_op(cpu_gprh[rD(ctx->opcode)], cpu_gprh[rA(ctx->opcode)]); \
5989}
5990#endif
0487d6a8 5991
a7812ae4 5992static always_inline void gen_op_evabs (TCGv_i32 ret, TCGv_i32 arg1)
57951c27
AJ
5993{
5994 int l1 = gen_new_label();
5995 int l2 = gen_new_label();
0487d6a8 5996
57951c27
AJ
5997 tcg_gen_brcondi_i32(TCG_COND_GE, arg1, 0, l1);
5998 tcg_gen_neg_i32(ret, arg1);
5999 tcg_gen_br(l2);
6000 gen_set_label(l1);
a7812ae4 6001 tcg_gen_mov_i32(ret, arg1);
57951c27
AJ
6002 gen_set_label(l2);
6003}
6004GEN_SPEOP_ARITH1(evabs, gen_op_evabs);
6005GEN_SPEOP_ARITH1(evneg, tcg_gen_neg_i32);
6006GEN_SPEOP_ARITH1(evextsb, tcg_gen_ext8s_i32);
6007GEN_SPEOP_ARITH1(evextsh, tcg_gen_ext16s_i32);
a7812ae4 6008static always_inline void gen_op_evrndw (TCGv_i32 ret, TCGv_i32 arg1)
0487d6a8 6009{
57951c27
AJ
6010 tcg_gen_addi_i32(ret, arg1, 0x8000);
6011 tcg_gen_ext16u_i32(ret, ret);
6012}
6013GEN_SPEOP_ARITH1(evrndw, gen_op_evrndw);
a7812ae4
PB
6014GEN_SPEOP_ARITH1(evcntlsw, gen_helper_cntlsw32);
6015GEN_SPEOP_ARITH1(evcntlzw, gen_helper_cntlzw32);
0487d6a8 6016
57951c27
AJ
6017#if defined(TARGET_PPC64)
6018#define GEN_SPEOP_ARITH2(name, tcg_op) \
6019static always_inline void gen_##name (DisasContext *ctx) \
0487d6a8
JM
6020{ \
6021 if (unlikely(!ctx->spe_enabled)) { \
e1833e1f 6022 GEN_EXCP_NO_AP(ctx); \
0487d6a8
JM
6023 return; \
6024 } \
a7812ae4
PB
6025 TCGv_i32 t0 = tcg_temp_local_new_i32(); \
6026 TCGv_i32 t1 = tcg_temp_local_new_i32(); \
6027 TCGv_i32 t2 = tcg_temp_local_new_i32(); \
6028 TCGv_i64 t3 = tcg_temp_local_new(TCG_TYPE_I64); \
57951c27
AJ
6029 tcg_gen_trunc_i64_i32(t0, cpu_gpr[rA(ctx->opcode)]); \
6030 tcg_gen_trunc_i64_i32(t2, cpu_gpr[rB(ctx->opcode)]); \
6031 tcg_op(t0, t0, t2); \
6032 tcg_gen_shri_i64(t3, cpu_gpr[rA(ctx->opcode)], 32); \
6033 tcg_gen_trunc_i64_i32(t1, t3); \
6034 tcg_gen_shri_i64(t3, cpu_gpr[rB(ctx->opcode)], 32); \
6035 tcg_gen_trunc_i64_i32(t2, t3); \
a7812ae4 6036 tcg_temp_free_i64(t3); \
57951c27 6037 tcg_op(t1, t1, t2); \
a7812ae4 6038 tcg_temp_free_i32(t2); \
57951c27 6039 tcg_gen_concat_i32_i64(cpu_gpr[rD(ctx->opcode)], t0, t1); \
a7812ae4
PB
6040 tcg_temp_free_i32(t0); \
6041 tcg_temp_free_i32(t1); \
0487d6a8 6042}
57951c27
AJ
6043#else
6044#define GEN_SPEOP_ARITH2(name, tcg_op) \
6045static always_inline void gen_##name (DisasContext *ctx) \
0487d6a8
JM
6046{ \
6047 if (unlikely(!ctx->spe_enabled)) { \
e1833e1f 6048 GEN_EXCP_NO_AP(ctx); \
0487d6a8
JM
6049 return; \
6050 } \
57951c27
AJ
6051 tcg_op(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)], \
6052 cpu_gpr[rB(ctx->opcode)]); \
6053 tcg_op(cpu_gprh[rD(ctx->opcode)], cpu_gprh[rA(ctx->opcode)], \
6054 cpu_gprh[rB(ctx->opcode)]); \
0487d6a8 6055}
57951c27 6056#endif
0487d6a8 6057
a7812ae4 6058static always_inline void gen_op_evsrwu (TCGv_i32 ret, TCGv_i32 arg1, TCGv_i32 arg2)
57951c27 6059{
a7812ae4 6060 TCGv_i32 t0;
57951c27 6061 int l1, l2;
0487d6a8 6062
57951c27
AJ
6063 l1 = gen_new_label();
6064 l2 = gen_new_label();
a7812ae4 6065 t0 = tcg_temp_local_new_i32();
57951c27
AJ
6066 /* No error here: 6 bits are used */
6067 tcg_gen_andi_i32(t0, arg2, 0x3F);
6068 tcg_gen_brcondi_i32(TCG_COND_GE, t0, 32, l1);
6069 tcg_gen_shr_i32(ret, arg1, t0);
6070 tcg_gen_br(l2);
6071 gen_set_label(l1);
6072 tcg_gen_movi_i32(ret, 0);
6073 tcg_gen_br(l2);
a7812ae4 6074 tcg_temp_free_i32(t0);
57951c27
AJ
6075}
6076GEN_SPEOP_ARITH2(evsrwu, gen_op_evsrwu);
a7812ae4 6077static always_inline void gen_op_evsrws (TCGv_i32 ret, TCGv_i32 arg1, TCGv_i32 arg2)
57951c27 6078{
a7812ae4 6079 TCGv_i32 t0;
57951c27
AJ
6080 int l1, l2;
6081
6082 l1 = gen_new_label();
6083 l2 = gen_new_label();
a7812ae4 6084 t0 = tcg_temp_local_new_i32();
57951c27
AJ
6085 /* No error here: 6 bits are used */
6086 tcg_gen_andi_i32(t0, arg2, 0x3F);
6087 tcg_gen_brcondi_i32(TCG_COND_GE, t0, 32, l1);
6088 tcg_gen_sar_i32(ret, arg1, t0);
6089 tcg_gen_br(l2);
6090 gen_set_label(l1);
6091 tcg_gen_movi_i32(ret, 0);
6092 tcg_gen_br(l2);
a7812ae4 6093 tcg_temp_free_i32(t0);
57951c27
AJ
6094}
6095GEN_SPEOP_ARITH2(evsrws, gen_op_evsrws);
a7812ae4 6096static always_inline void gen_op_evslw (TCGv_i32 ret, TCGv_i32 arg1, TCGv_i32 arg2)
57951c27 6097{
a7812ae4 6098 TCGv_i32 t0;
57951c27
AJ
6099 int l1, l2;
6100
6101 l1 = gen_new_label();
6102 l2 = gen_new_label();
a7812ae4 6103 t0 = tcg_temp_local_new_i32();
57951c27
AJ
6104 /* No error here: 6 bits are used */
6105 tcg_gen_andi_i32(t0, arg2, 0x3F);
6106 tcg_gen_brcondi_i32(TCG_COND_GE, t0, 32, l1);
6107 tcg_gen_shl_i32(ret, arg1, t0);
6108 tcg_gen_br(l2);
6109 gen_set_label(l1);
6110 tcg_gen_movi_i32(ret, 0);
6111 tcg_gen_br(l2);
a7812ae4 6112 tcg_temp_free_i32(t0);
57951c27
AJ
6113}
6114GEN_SPEOP_ARITH2(evslw, gen_op_evslw);
a7812ae4 6115static always_inline void gen_op_evrlw (TCGv_i32 ret, TCGv_i32 arg1, TCGv_i32 arg2)
57951c27 6116{
a7812ae4 6117 TCGv_i32 t0 = tcg_temp_new_i32();
57951c27
AJ
6118 tcg_gen_andi_i32(t0, arg2, 0x1F);
6119 tcg_gen_rotl_i32(ret, arg1, t0);
a7812ae4 6120 tcg_temp_free_i32(t0);
57951c27
AJ
6121}
6122GEN_SPEOP_ARITH2(evrlw, gen_op_evrlw);
6123static always_inline void gen_evmergehi (DisasContext *ctx)
6124{
6125 if (unlikely(!ctx->spe_enabled)) {
6126 GEN_EXCP_NO_AP(ctx);
6127 return;
6128 }
6129#if defined(TARGET_PPC64)
a7812ae4
PB
6130 TCGv t0 = tcg_temp_new();
6131 TCGv t1 = tcg_temp_new();
57951c27
AJ
6132 tcg_gen_shri_tl(t0, cpu_gpr[rB(ctx->opcode)], 32);
6133 tcg_gen_andi_tl(t1, cpu_gpr[rA(ctx->opcode)], 0xFFFFFFFF0000000ULL);
6134 tcg_gen_or_tl(cpu_gpr[rD(ctx->opcode)], t0, t1);
6135 tcg_temp_free(t0);
6136 tcg_temp_free(t1);
6137#else
6138 tcg_gen_mov_i32(cpu_gpr[rD(ctx->opcode)], cpu_gprh[rB(ctx->opcode)]);
6139 tcg_gen_mov_i32(cpu_gprh[rD(ctx->opcode)], cpu_gprh[rA(ctx->opcode)]);
6140#endif
6141}
6142GEN_SPEOP_ARITH2(evaddw, tcg_gen_add_i32);
a7812ae4 6143static always_inline void gen_op_evsubf (TCGv_i32 ret, TCGv_i32 arg1, TCGv_i32 arg2)
0487d6a8 6144{
57951c27
AJ
6145 tcg_gen_sub_i32(ret, arg2, arg1);
6146}
6147GEN_SPEOP_ARITH2(evsubfw, gen_op_evsubf);
0487d6a8 6148
57951c27
AJ
6149/* SPE arithmetic immediate */
6150#if defined(TARGET_PPC64)
6151#define GEN_SPEOP_ARITH_IMM2(name, tcg_op) \
6152static always_inline void gen_##name (DisasContext *ctx) \
6153{ \
6154 if (unlikely(!ctx->spe_enabled)) { \
6155 GEN_EXCP_NO_AP(ctx); \
6156 return; \
6157 } \
a7812ae4
PB
6158 TCGv_i32 t0 = tcg_temp_local_new_i32(); \
6159 TCGv_i32 t1 = tcg_temp_local_new_i32(); \
6160 TCGv_i64 t2 = tcg_temp_local_new_i64(); \
57951c27
AJ
6161 tcg_gen_trunc_i64_i32(t0, cpu_gpr[rB(ctx->opcode)]); \
6162 tcg_op(t0, t0, rA(ctx->opcode)); \
6163 tcg_gen_shri_i64(t2, cpu_gpr[rB(ctx->opcode)], 32); \
6164 tcg_gen_trunc_i64_i32(t1, t2); \
a7812ae4 6165 tcg_temp_free_i64(t2); \
57951c27
AJ
6166 tcg_op(t1, t1, rA(ctx->opcode)); \
6167 tcg_gen_concat_i32_i64(cpu_gpr[rD(ctx->opcode)], t0, t1); \
a7812ae4
PB
6168 tcg_temp_free_i32(t0); \
6169 tcg_temp_free_i32(t1); \
57951c27
AJ
6170}
6171#else
6172#define GEN_SPEOP_ARITH_IMM2(name, tcg_op) \
6173static always_inline void gen_##name (DisasContext *ctx) \
6174{ \
6175 if (unlikely(!ctx->spe_enabled)) { \
6176 GEN_EXCP_NO_AP(ctx); \
6177 return; \
6178 } \
6179 tcg_op(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rB(ctx->opcode)], \
6180 rA(ctx->opcode)); \
6181 tcg_op(cpu_gprh[rD(ctx->opcode)], cpu_gprh[rB(ctx->opcode)], \
6182 rA(ctx->opcode)); \
6183}
6184#endif
6185GEN_SPEOP_ARITH_IMM2(evaddiw, tcg_gen_addi_i32);
6186GEN_SPEOP_ARITH_IMM2(evsubifw, tcg_gen_subi_i32);
6187
6188/* SPE comparison */
6189#if defined(TARGET_PPC64)
6190#define GEN_SPEOP_COMP(name, tcg_cond) \
6191static always_inline void gen_##name (DisasContext *ctx) \
6192{ \
6193 if (unlikely(!ctx->spe_enabled)) { \
6194 GEN_EXCP_NO_AP(ctx); \
6195 return; \
6196 } \
6197 int l1 = gen_new_label(); \
6198 int l2 = gen_new_label(); \
6199 int l3 = gen_new_label(); \
6200 int l4 = gen_new_label(); \
a7812ae4
PB
6201 TCGv_i32 t0 = tcg_temp_local_new_i32(); \
6202 TCGv_i32 t1 = tcg_temp_local_new_i32(); \
6203 TCGv_i64 t2 = tcg_temp_local_new_i64(); \
57951c27
AJ
6204 tcg_gen_trunc_i64_i32(t0, cpu_gpr[rA(ctx->opcode)]); \
6205 tcg_gen_trunc_i64_i32(t1, cpu_gpr[rB(ctx->opcode)]); \
6206 tcg_gen_brcond_i32(tcg_cond, t0, t1, l1); \
a7812ae4 6207 tcg_gen_movi_i32(cpu_crf[crfD(ctx->opcode)], 0); \
57951c27
AJ
6208 tcg_gen_br(l2); \
6209 gen_set_label(l1); \
6210 tcg_gen_movi_i32(cpu_crf[crfD(ctx->opcode)], \
6211 CRF_CL | CRF_CH_OR_CL | CRF_CH_AND_CL); \
6212 gen_set_label(l2); \
6213 tcg_gen_shri_i64(t2, cpu_gpr[rA(ctx->opcode)], 32); \
6214 tcg_gen_trunc_i64_i32(t0, t2); \
6215 tcg_gen_shri_i64(t2, cpu_gpr[rB(ctx->opcode)], 32); \
6216 tcg_gen_trunc_i64_i32(t1, t2); \
a7812ae4 6217 tcg_temp_free_i64(t2); \
57951c27
AJ
6218 tcg_gen_brcond_i32(tcg_cond, t0, t1, l3); \
6219 tcg_gen_andi_i32(cpu_crf[crfD(ctx->opcode)], cpu_crf[crfD(ctx->opcode)], \
6220 ~(CRF_CH | CRF_CH_AND_CL)); \
6221 tcg_gen_br(l4); \
6222 gen_set_label(l3); \
6223 tcg_gen_ori_i32(cpu_crf[crfD(ctx->opcode)], cpu_crf[crfD(ctx->opcode)], \
6224 CRF_CH | CRF_CH_OR_CL); \
6225 gen_set_label(l4); \
a7812ae4
PB
6226 tcg_temp_free_i32(t0); \
6227 tcg_temp_free_i32(t1); \
57951c27
AJ
6228}
6229#else
6230#define GEN_SPEOP_COMP(name, tcg_cond) \
6231static always_inline void gen_##name (DisasContext *ctx) \
6232{ \
6233 if (unlikely(!ctx->spe_enabled)) { \
6234 GEN_EXCP_NO_AP(ctx); \
6235 return; \
6236 } \
6237 int l1 = gen_new_label(); \
6238 int l2 = gen_new_label(); \
6239 int l3 = gen_new_label(); \
6240 int l4 = gen_new_label(); \
6241 \
6242 tcg_gen_brcond_i32(tcg_cond, cpu_gpr[rA(ctx->opcode)], \
6243 cpu_gpr[rB(ctx->opcode)], l1); \
6244 tcg_gen_movi_tl(cpu_crf[crfD(ctx->opcode)], 0); \
6245 tcg_gen_br(l2); \
6246 gen_set_label(l1); \
6247 tcg_gen_movi_i32(cpu_crf[crfD(ctx->opcode)], \
6248 CRF_CL | CRF_CH_OR_CL | CRF_CH_AND_CL); \
6249 gen_set_label(l2); \
6250 tcg_gen_brcond_i32(tcg_cond, cpu_gprh[rA(ctx->opcode)], \
6251 cpu_gprh[rB(ctx->opcode)], l3); \
6252 tcg_gen_andi_i32(cpu_crf[crfD(ctx->opcode)], cpu_crf[crfD(ctx->opcode)], \
6253 ~(CRF_CH | CRF_CH_AND_CL)); \
6254 tcg_gen_br(l4); \
6255 gen_set_label(l3); \
6256 tcg_gen_ori_i32(cpu_crf[crfD(ctx->opcode)], cpu_crf[crfD(ctx->opcode)], \
6257 CRF_CH | CRF_CH_OR_CL); \
6258 gen_set_label(l4); \
6259}
6260#endif
6261GEN_SPEOP_COMP(evcmpgtu, TCG_COND_GTU);
6262GEN_SPEOP_COMP(evcmpgts, TCG_COND_GT);
6263GEN_SPEOP_COMP(evcmpltu, TCG_COND_LTU);
6264GEN_SPEOP_COMP(evcmplts, TCG_COND_LT);
6265GEN_SPEOP_COMP(evcmpeq, TCG_COND_EQ);
6266
6267/* SPE misc */
6268static always_inline void gen_brinc (DisasContext *ctx)
6269{
6270 /* Note: brinc is usable even if SPE is disabled */
a7812ae4
PB
6271 gen_helper_brinc(cpu_gpr[rD(ctx->opcode)],
6272 cpu_gpr[rA(ctx->opcode)], cpu_gpr[rB(ctx->opcode)]);
0487d6a8 6273}
57951c27
AJ
6274static always_inline void gen_evmergelo (DisasContext *ctx)
6275{
6276 if (unlikely(!ctx->spe_enabled)) {
6277 GEN_EXCP_NO_AP(ctx);
6278 return;
6279 }
6280#if defined(TARGET_PPC64)
a7812ae4
PB
6281 TCGv t0 = tcg_temp_new();
6282 TCGv t1 = tcg_temp_new();
57951c27
AJ
6283 tcg_gen_andi_tl(t0, cpu_gpr[rB(ctx->opcode)], 0x00000000FFFFFFFFLL);
6284 tcg_gen_shli_tl(t1, cpu_gpr[rA(ctx->opcode)], 32);
6285 tcg_gen_or_tl(cpu_gpr[rD(ctx->opcode)], t0, t1);
6286 tcg_temp_free(t0);
6287 tcg_temp_free(t1);
6288#else
6289 tcg_gen_mov_i32(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rB(ctx->opcode)]);
6290 tcg_gen_mov_i32(cpu_gprh[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)]);
6291#endif
6292}
6293static always_inline void gen_evmergehilo (DisasContext *ctx)
6294{
6295 if (unlikely(!ctx->spe_enabled)) {
6296 GEN_EXCP_NO_AP(ctx);
6297 return;
6298 }
6299#if defined(TARGET_PPC64)
a7812ae4
PB
6300 TCGv t0 = tcg_temp_new();
6301 TCGv t1 = tcg_temp_new();
57951c27
AJ
6302 tcg_gen_andi_tl(t0, cpu_gpr[rB(ctx->opcode)], 0x00000000FFFFFFFFLL);
6303 tcg_gen_andi_tl(t1, cpu_gpr[rA(ctx->opcode)], 0xFFFFFFFF0000000ULL);
6304 tcg_gen_or_tl(cpu_gpr[rD(ctx->opcode)], t0, t1);
6305 tcg_temp_free(t0);
6306 tcg_temp_free(t1);
6307#else
6308 tcg_gen_mov_i32(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rB(ctx->opcode)]);
6309 tcg_gen_mov_i32(cpu_gprh[rD(ctx->opcode)], cpu_gprh[rA(ctx->opcode)]);
6310#endif
6311}
6312static always_inline void gen_evmergelohi (DisasContext *ctx)
6313{
6314 if (unlikely(!ctx->spe_enabled)) {
6315 GEN_EXCP_NO_AP(ctx);
6316 return;
6317 }
6318#if defined(TARGET_PPC64)
a7812ae4
PB
6319 TCGv t0 = tcg_temp_new();
6320 TCGv t1 = tcg_temp_new();
57951c27
AJ
6321 tcg_gen_shri_tl(t0, cpu_gpr[rB(ctx->opcode)], 32);
6322 tcg_gen_shli_tl(t1, cpu_gpr[rA(ctx->opcode)], 32);
6323 tcg_gen_or_tl(cpu_gpr[rD(ctx->opcode)], t0, t1);
6324 tcg_temp_free(t0);
6325 tcg_temp_free(t1);
6326#else
6327 tcg_gen_mov_i32(cpu_gpr[rD(ctx->opcode)], cpu_gprh[rB(ctx->opcode)]);
6328 tcg_gen_mov_i32(cpu_gprh[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)]);
6329#endif
6330}
6331static always_inline void gen_evsplati (DisasContext *ctx)
6332{
38d14952 6333 uint64_t imm = ((int32_t)(rA(ctx->opcode) << 11)) >> 27;
0487d6a8 6334
57951c27 6335#if defined(TARGET_PPC64)
38d14952 6336 tcg_gen_movi_tl(cpu_gpr[rD(ctx->opcode)], (imm << 32) | imm);
57951c27
AJ
6337#else
6338 tcg_gen_movi_i32(cpu_gpr[rD(ctx->opcode)], imm);
6339 tcg_gen_movi_i32(cpu_gprh[rD(ctx->opcode)], imm);
6340#endif
6341}
b068d6a7 6342static always_inline void gen_evsplatfi (DisasContext *ctx)
0487d6a8 6343{
38d14952 6344 uint64_t imm = rA(ctx->opcode) << 11;
0487d6a8 6345
57951c27 6346#if defined(TARGET_PPC64)
38d14952 6347 tcg_gen_movi_tl(cpu_gpr[rD(ctx->opcode)], (imm << 32) | imm);
57951c27
AJ
6348#else
6349 tcg_gen_movi_i32(cpu_gpr[rD(ctx->opcode)], imm);
6350 tcg_gen_movi_i32(cpu_gprh[rD(ctx->opcode)], imm);
6351#endif
0487d6a8
JM
6352}
6353
57951c27
AJ
6354static always_inline void gen_evsel (DisasContext *ctx)
6355{
6356 int l1 = gen_new_label();
6357 int l2 = gen_new_label();
6358 int l3 = gen_new_label();
6359 int l4 = gen_new_label();
a7812ae4 6360 TCGv_i32 t0 = tcg_temp_local_new_i32();
57951c27 6361#if defined(TARGET_PPC64)
a7812ae4
PB
6362 TCGv t1 = tcg_temp_local_new();
6363 TCGv t2 = tcg_temp_local_new();
57951c27
AJ
6364#endif
6365 tcg_gen_andi_i32(t0, cpu_crf[ctx->opcode & 0x07], 1 << 3);
6366 tcg_gen_brcondi_i32(TCG_COND_EQ, t0, 0, l1);
6367#if defined(TARGET_PPC64)
6368 tcg_gen_andi_tl(t1, cpu_gpr[rA(ctx->opcode)], 0xFFFFFFFF00000000ULL);
6369#else
6370 tcg_gen_mov_tl(cpu_gprh[rD(ctx->opcode)], cpu_gprh[rA(ctx->opcode)]);
6371#endif
6372 tcg_gen_br(l2);
6373 gen_set_label(l1);
6374#if defined(TARGET_PPC64)
6375 tcg_gen_andi_tl(t1, cpu_gpr[rB(ctx->opcode)], 0xFFFFFFFF00000000ULL);
6376#else
6377 tcg_gen_mov_tl(cpu_gprh[rD(ctx->opcode)], cpu_gprh[rB(ctx->opcode)]);
6378#endif
6379 gen_set_label(l2);
6380 tcg_gen_andi_i32(t0, cpu_crf[ctx->opcode & 0x07], 1 << 2);
6381 tcg_gen_brcondi_i32(TCG_COND_EQ, t0, 0, l3);
6382#if defined(TARGET_PPC64)
6383 tcg_gen_andi_tl(t2, cpu_gpr[rA(ctx->opcode)], 0x00000000FFFFFFFFULL);
6384#else
6385 tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)]);
6386#endif
6387 tcg_gen_br(l4);
6388 gen_set_label(l3);
6389#if defined(TARGET_PPC64)
6390 tcg_gen_andi_tl(t2, cpu_gpr[rB(ctx->opcode)], 0x00000000FFFFFFFFULL);
6391#else
6392 tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rB(ctx->opcode)]);
6393#endif
6394 gen_set_label(l4);
a7812ae4 6395 tcg_temp_free_i32(t0);
57951c27
AJ
6396#if defined(TARGET_PPC64)
6397 tcg_gen_or_tl(cpu_gpr[rD(ctx->opcode)], t1, t2);
6398 tcg_temp_free(t1);
6399 tcg_temp_free(t2);
6400#endif
6401}
6402GEN_HANDLER2(evsel0, "evsel", 0x04, 0x1c, 0x09, 0x00000000, PPC_SPE)
6403{
6404 gen_evsel(ctx);
6405}
6406GEN_HANDLER2(evsel1, "evsel", 0x04, 0x1d, 0x09, 0x00000000, PPC_SPE)
6407{
6408 gen_evsel(ctx);
6409}
6410GEN_HANDLER2(evsel2, "evsel", 0x04, 0x1e, 0x09, 0x00000000, PPC_SPE)
6411{
6412 gen_evsel(ctx);
6413}
6414GEN_HANDLER2(evsel3, "evsel", 0x04, 0x1f, 0x09, 0x00000000, PPC_SPE)
6415{
6416 gen_evsel(ctx);
6417}
0487d6a8
JM
6418
6419GEN_SPE(evaddw, speundef, 0x00, 0x08, 0x00000000, PPC_SPE); ////
6420GEN_SPE(evaddiw, speundef, 0x01, 0x08, 0x00000000, PPC_SPE);
6421GEN_SPE(evsubfw, speundef, 0x02, 0x08, 0x00000000, PPC_SPE); ////
6422GEN_SPE(evsubifw, speundef, 0x03, 0x08, 0x00000000, PPC_SPE);
6423GEN_SPE(evabs, evneg, 0x04, 0x08, 0x0000F800, PPC_SPE); ////
6424GEN_SPE(evextsb, evextsh, 0x05, 0x08, 0x0000F800, PPC_SPE); ////
6425GEN_SPE(evrndw, evcntlzw, 0x06, 0x08, 0x0000F800, PPC_SPE); ////
6426GEN_SPE(evcntlsw, brinc, 0x07, 0x08, 0x00000000, PPC_SPE); //
6427GEN_SPE(speundef, evand, 0x08, 0x08, 0x00000000, PPC_SPE); ////
6428GEN_SPE(evandc, speundef, 0x09, 0x08, 0x00000000, PPC_SPE); ////
6429GEN_SPE(evxor, evor, 0x0B, 0x08, 0x00000000, PPC_SPE); ////
6430GEN_SPE(evnor, eveqv, 0x0C, 0x08, 0x00000000, PPC_SPE); ////
6431GEN_SPE(speundef, evorc, 0x0D, 0x08, 0x00000000, PPC_SPE); ////
6432GEN_SPE(evnand, speundef, 0x0F, 0x08, 0x00000000, PPC_SPE); ////
6433GEN_SPE(evsrwu, evsrws, 0x10, 0x08, 0x00000000, PPC_SPE); ////
6434GEN_SPE(evsrwiu, evsrwis, 0x11, 0x08, 0x00000000, PPC_SPE);
6435GEN_SPE(evslw, speundef, 0x12, 0x08, 0x00000000, PPC_SPE); ////
6436GEN_SPE(evslwi, speundef, 0x13, 0x08, 0x00000000, PPC_SPE);
6437GEN_SPE(evrlw, evsplati, 0x14, 0x08, 0x00000000, PPC_SPE); //
6438GEN_SPE(evrlwi, evsplatfi, 0x15, 0x08, 0x00000000, PPC_SPE);
6439GEN_SPE(evmergehi, evmergelo, 0x16, 0x08, 0x00000000, PPC_SPE); ////
6440GEN_SPE(evmergehilo, evmergelohi, 0x17, 0x08, 0x00000000, PPC_SPE); ////
6441GEN_SPE(evcmpgtu, evcmpgts, 0x18, 0x08, 0x00600000, PPC_SPE); ////
6442GEN_SPE(evcmpltu, evcmplts, 0x19, 0x08, 0x00600000, PPC_SPE); ////
6443GEN_SPE(evcmpeq, speundef, 0x1A, 0x08, 0x00600000, PPC_SPE); ////
6444
6a6ae23f
AJ
6445/* SPE load and stores */
6446static always_inline void gen_addr_spe_imm_index (TCGv EA, DisasContext *ctx, int sh)
6447{
6448 target_ulong uimm = rB(ctx->opcode);
6449
6450 if (rA(ctx->opcode) == 0)
6451 tcg_gen_movi_tl(EA, uimm << sh);
6452 else
6453 tcg_gen_addi_tl(EA, cpu_gpr[rA(ctx->opcode)], uimm << sh);
0487d6a8 6454}
6a6ae23f
AJ
6455
6456static always_inline void gen_op_evldd(DisasContext *ctx, TCGv addr)
6457{
6458#if defined(TARGET_PPC64)
6459 gen_qemu_ld64(cpu_gpr[rD(ctx->opcode)], addr, ctx->mem_idx);
6460#else
6461 TCGv_i64 t0 = tcg_temp_new_i64();
6462 gen_qemu_ld64(t0, addr, ctx->mem_idx);
6463 tcg_gen_trunc_i64_i32(cpu_gpr[rD(ctx->opcode)], t0);
6464 tcg_gen_shri_i64(t0, t0, 32);
6465 tcg_gen_trunc_i64_i32(cpu_gprh[rD(ctx->opcode)], t0);
6466 tcg_temp_free_i64(t0);
6467#endif
0487d6a8 6468}
6a6ae23f
AJ
6469
6470static always_inline void gen_op_evldw(DisasContext *ctx, TCGv addr)
6471{
0487d6a8 6472#if defined(TARGET_PPC64)
6a6ae23f
AJ
6473 TCGv t0 = tcg_temp_new();
6474 gen_qemu_ld32u(t0, addr, ctx->mem_idx);
6475 tcg_gen_shli_tl(cpu_gpr[rD(ctx->opcode)], t0, 32);
6476 tcg_gen_addi_tl(addr, addr, 4);
6477 gen_qemu_ld32u(t0, addr, ctx->mem_idx);
6478 tcg_gen_or_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rD(ctx->opcode)], t0);
6479 tcg_temp_free(t0);
6480#else
6481 gen_qemu_ld32u(cpu_gprh[rD(ctx->opcode)], addr, ctx->mem_idx);
6482 tcg_gen_addi_tl(addr, addr, 4);
6483 gen_qemu_ld32u(cpu_gpr[rD(ctx->opcode)], addr, ctx->mem_idx);
6484#endif
0487d6a8 6485}
6a6ae23f
AJ
6486
6487static always_inline void gen_op_evldh(DisasContext *ctx, TCGv addr)
6488{
6489 TCGv t0 = tcg_temp_new();
6490#if defined(TARGET_PPC64)
6491 gen_qemu_ld16u(t0, addr, ctx->mem_idx);
6492 tcg_gen_shli_tl(cpu_gpr[rD(ctx->opcode)], t0, 48);
6493 tcg_gen_addi_tl(addr, addr, 2);
6494 gen_qemu_ld16u(t0, addr, ctx->mem_idx);
6495 tcg_gen_shli_tl(t0, t0, 32);
6496 tcg_gen_or_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rD(ctx->opcode)], t0);
6497 tcg_gen_addi_tl(addr, addr, 2);
6498 gen_qemu_ld16u(t0, addr, ctx->mem_idx);
6499 tcg_gen_shli_tl(t0, t0, 16);
6500 tcg_gen_or_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rD(ctx->opcode)], t0);
6501 tcg_gen_addi_tl(addr, addr, 2);
6502 gen_qemu_ld16u(t0, addr, ctx->mem_idx);
6503 tcg_gen_or_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rD(ctx->opcode)], t0);
0487d6a8 6504#else
6a6ae23f
AJ
6505 gen_qemu_ld16u(t0, addr, ctx->mem_idx);
6506 tcg_gen_shli_tl(cpu_gprh[rD(ctx->opcode)], t0, 16);
6507 tcg_gen_addi_tl(addr, addr, 2);
6508 gen_qemu_ld16u(t0, addr, ctx->mem_idx);
6509 tcg_gen_or_tl(cpu_gprh[rD(ctx->opcode)], cpu_gprh[rD(ctx->opcode)], t0);
6510 tcg_gen_addi_tl(addr, addr, 2);
6511 gen_qemu_ld16u(t0, addr, ctx->mem_idx);
6512 tcg_gen_shli_tl(cpu_gprh[rD(ctx->opcode)], t0, 16);
6513 tcg_gen_addi_tl(addr, addr, 2);
6514 gen_qemu_ld16u(t0, addr, ctx->mem_idx);
6515 tcg_gen_or_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rD(ctx->opcode)], t0);
0487d6a8 6516#endif
6a6ae23f 6517 tcg_temp_free(t0);
0487d6a8
JM
6518}
6519
6a6ae23f
AJ
6520static always_inline void gen_op_evlhhesplat(DisasContext *ctx, TCGv addr)
6521{
6522 TCGv t0 = tcg_temp_new();
6523 gen_qemu_ld16u(t0, addr, ctx->mem_idx);
6524#if defined(TARGET_PPC64)
6525 tcg_gen_shli_tl(cpu_gpr[rD(ctx->opcode)], t0, 48);
6526 tcg_gen_shli_tl(t0, t0, 16);
6527 tcg_gen_or_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rD(ctx->opcode)], t0);
6528#else
6529 tcg_gen_shli_tl(t0, t0, 16);
6530 tcg_gen_mov_tl(cpu_gprh[rD(ctx->opcode)], t0);
6531 tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], t0);
6532#endif
6533 tcg_temp_free(t0);
0487d6a8
JM
6534}
6535
6a6ae23f
AJ
6536static always_inline void gen_op_evlhhousplat(DisasContext *ctx, TCGv addr)
6537{
6538 TCGv t0 = tcg_temp_new();
6539 gen_qemu_ld16u(t0, addr, ctx->mem_idx);
6540#if defined(TARGET_PPC64)
6541 tcg_gen_shli_tl(cpu_gpr[rD(ctx->opcode)], t0, 32);
6542 tcg_gen_or_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rD(ctx->opcode)], t0);
6543#else
6544 tcg_gen_mov_tl(cpu_gprh[rD(ctx->opcode)], t0);
6545 tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], t0);
6546#endif
6547 tcg_temp_free(t0);
0487d6a8
JM
6548}
6549
6a6ae23f
AJ
6550static always_inline void gen_op_evlhhossplat(DisasContext *ctx, TCGv addr)
6551{
6552 TCGv t0 = tcg_temp_new();
6553 gen_qemu_ld16s(t0, addr, ctx->mem_idx);
6554#if defined(TARGET_PPC64)
6555 tcg_gen_shli_tl(cpu_gpr[rD(ctx->opcode)], t0, 32);
6556 tcg_gen_ext32u_tl(t0, t0);
6557 tcg_gen_or_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rD(ctx->opcode)], t0);
6558#else
6559 tcg_gen_mov_tl(cpu_gprh[rD(ctx->opcode)], t0);
6560 tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], t0);
6561#endif
6562 tcg_temp_free(t0);
6563}
6564
6565static always_inline void gen_op_evlwhe(DisasContext *ctx, TCGv addr)
6566{
6567 TCGv t0 = tcg_temp_new();
6568#if defined(TARGET_PPC64)
6569 gen_qemu_ld16u(t0, addr, ctx->mem_idx);
6570 tcg_gen_shli_tl(cpu_gpr[rD(ctx->opcode)], t0, 48);
6571 gen_qemu_ld16u(t0, addr, ctx->mem_idx);
6572 tcg_gen_shli_tl(t0, t0, 16);
6573 tcg_gen_or_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rD(ctx->opcode)], t0);
6574#else
6575 gen_qemu_ld16u(t0, addr, ctx->mem_idx);
6576 tcg_gen_shli_tl(cpu_gprh[rD(ctx->opcode)], t0, 16);
6577 tcg_gen_addi_tl(addr, addr, 2);
6578 gen_qemu_ld16u(t0, addr, ctx->mem_idx);
6579 tcg_gen_shli_tl(cpu_gpr[rD(ctx->opcode)], t0, 16);
6580#endif
6581 tcg_temp_free(t0);
6582}
6583
6584static always_inline void gen_op_evlwhou(DisasContext *ctx, TCGv addr)
6585{
6586#if defined(TARGET_PPC64)
6587 TCGv t0 = tcg_temp_new();
6588 gen_qemu_ld16u(cpu_gpr[rD(ctx->opcode)], addr, ctx->mem_idx);
6589 tcg_gen_addi_tl(addr, addr, 2);
6590 gen_qemu_ld16u(t0, addr, ctx->mem_idx);
6591 tcg_gen_shli_tl(t0, t0, 32);
6592 tcg_gen_or_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rD(ctx->opcode)], t0);
6593 tcg_temp_free(t0);
6594#else
6595 gen_qemu_ld16u(cpu_gprh[rD(ctx->opcode)], addr, ctx->mem_idx);
6596 tcg_gen_addi_tl(addr, addr, 2);
6597 gen_qemu_ld16u(cpu_gpr[rD(ctx->opcode)], addr, ctx->mem_idx);
6598#endif
6599}
6600
6601static always_inline void gen_op_evlwhos(DisasContext *ctx, TCGv addr)
6602{
6603#if defined(TARGET_PPC64)
6604 TCGv t0 = tcg_temp_new();
6605 gen_qemu_ld16s(t0, addr, ctx->mem_idx);
6606 tcg_gen_ext32u_tl(cpu_gpr[rD(ctx->opcode)], t0);
6607 tcg_gen_addi_tl(addr, addr, 2);
6608 gen_qemu_ld16s(t0, addr, ctx->mem_idx);
6609 tcg_gen_shli_tl(t0, t0, 32);
6610 tcg_gen_or_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rD(ctx->opcode)], t0);
6611 tcg_temp_free(t0);
6612#else
6613 gen_qemu_ld16s(cpu_gprh[rD(ctx->opcode)], addr, ctx->mem_idx);
6614 tcg_gen_addi_tl(addr, addr, 2);
6615 gen_qemu_ld16s(cpu_gpr[rD(ctx->opcode)], addr, ctx->mem_idx);
6616#endif
6617}
6618
6619static always_inline void gen_op_evlwwsplat(DisasContext *ctx, TCGv addr)
6620{
6621 TCGv t0 = tcg_temp_new();
6622 gen_qemu_ld32u(t0, addr, ctx->mem_idx);
0487d6a8 6623#if defined(TARGET_PPC64)
6a6ae23f
AJ
6624 tcg_gen_shli_tl(cpu_gpr[rD(ctx->opcode)], t0, 32);
6625 tcg_gen_or_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rD(ctx->opcode)], t0);
6626#else
6627 tcg_gen_mov_tl(cpu_gprh[rD(ctx->opcode)], t0);
6628 tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], t0);
6629#endif
6630 tcg_temp_free(t0);
6631}
6632
6633static always_inline void gen_op_evlwhsplat(DisasContext *ctx, TCGv addr)
6634{
6635 TCGv t0 = tcg_temp_new();
6636#if defined(TARGET_PPC64)
6637 gen_qemu_ld16u(t0, addr, ctx->mem_idx);
6638 tcg_gen_shli_tl(cpu_gpr[rD(ctx->opcode)], t0, 48);
6639 tcg_gen_shli_tl(t0, t0, 32);
6640 tcg_gen_or_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rD(ctx->opcode)], t0);
6641 tcg_gen_addi_tl(addr, addr, 2);
6642 gen_qemu_ld16u(t0, addr, ctx->mem_idx);
6643 tcg_gen_or_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rD(ctx->opcode)], t0);
6644 tcg_gen_shli_tl(t0, t0, 16);
6645 tcg_gen_or_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rD(ctx->opcode)], t0);
6646#else
6647 gen_qemu_ld16u(t0, addr, ctx->mem_idx);
6648 tcg_gen_shli_tl(cpu_gprh[rD(ctx->opcode)], t0, 16);
6649 tcg_gen_or_tl(cpu_gprh[rD(ctx->opcode)], cpu_gprh[rD(ctx->opcode)], t0);
6650 tcg_gen_addi_tl(addr, addr, 2);
6651 gen_qemu_ld16u(t0, addr, ctx->mem_idx);
6652 tcg_gen_shli_tl(cpu_gpr[rD(ctx->opcode)], t0, 16);
6653 tcg_gen_or_tl(cpu_gpr[rD(ctx->opcode)], cpu_gprh[rD(ctx->opcode)], t0);
0487d6a8 6654#endif
6a6ae23f
AJ
6655 tcg_temp_free(t0);
6656}
6657
6658static always_inline void gen_op_evstdd(DisasContext *ctx, TCGv addr)
6659{
6660#if defined(TARGET_PPC64)
6661 gen_qemu_st64(cpu_gpr[rS(ctx->opcode)], addr, ctx->mem_idx);
0487d6a8 6662#else
6a6ae23f
AJ
6663 TCGv_i64 t0 = tcg_temp_new_i64();
6664 tcg_gen_concat_i32_i64(t0, cpu_gpr[rS(ctx->opcode)], cpu_gprh[rS(ctx->opcode)]);
6665 gen_qemu_st64(t0, addr, ctx->mem_idx);
6666 tcg_temp_free_i64(t0);
6667#endif
6668}
6669
6670static always_inline void gen_op_evstdw(DisasContext *ctx, TCGv addr)
6671{
0487d6a8 6672#if defined(TARGET_PPC64)
6a6ae23f
AJ
6673 TCGv t0 = tcg_temp_new();
6674 tcg_gen_shri_tl(t0, cpu_gpr[rS(ctx->opcode)], 32);
6675 gen_qemu_st32(t0, addr, ctx->mem_idx);
6676 tcg_temp_free(t0);
6677#else
6678 gen_qemu_st32(cpu_gprh[rS(ctx->opcode)], addr, ctx->mem_idx);
6679#endif
6680 tcg_gen_addi_tl(addr, addr, 4);
6681 gen_qemu_st32(cpu_gpr[rS(ctx->opcode)], addr, ctx->mem_idx);
6682}
6683
6684static always_inline void gen_op_evstdh(DisasContext *ctx, TCGv addr)
6685{
6686 TCGv t0 = tcg_temp_new();
6687#if defined(TARGET_PPC64)
6688 tcg_gen_shri_tl(t0, cpu_gpr[rS(ctx->opcode)], 48);
6689#else
6690 tcg_gen_shri_tl(t0, cpu_gprh[rS(ctx->opcode)], 16);
6691#endif
6692 gen_qemu_st16(t0, addr, ctx->mem_idx);
6693 tcg_gen_addi_tl(addr, addr, 2);
6694#if defined(TARGET_PPC64)
6695 tcg_gen_shri_tl(t0, cpu_gpr[rS(ctx->opcode)], 32);
6696 gen_qemu_st16(t0, addr, ctx->mem_idx);
6697#else
6698 gen_qemu_st16(cpu_gprh[rS(ctx->opcode)], addr, ctx->mem_idx);
6699#endif
6700 tcg_gen_addi_tl(addr, addr, 2);
6701 tcg_gen_shri_tl(t0, cpu_gpr[rS(ctx->opcode)], 16);
6702 gen_qemu_st16(t0, addr, ctx->mem_idx);
6703 tcg_temp_free(t0);
6704 tcg_gen_addi_tl(addr, addr, 2);
6705 gen_qemu_st16(cpu_gpr[rS(ctx->opcode)], addr, ctx->mem_idx);
6706}
6707
6708static always_inline void gen_op_evstwhe(DisasContext *ctx, TCGv addr)
6709{
6710 TCGv t0 = tcg_temp_new();
6711#if defined(TARGET_PPC64)
6712 tcg_gen_shri_tl(t0, cpu_gpr[rS(ctx->opcode)], 48);
6713#else
6714 tcg_gen_shri_tl(t0, cpu_gprh[rS(ctx->opcode)], 16);
6715#endif
6716 gen_qemu_st16(t0, addr, ctx->mem_idx);
6717 tcg_gen_addi_tl(addr, addr, 2);
6718 tcg_gen_shri_tl(t0, cpu_gpr[rS(ctx->opcode)], 16);
6719 gen_qemu_st16(t0, addr, ctx->mem_idx);
6720 tcg_temp_free(t0);
6721}
6722
6723static always_inline void gen_op_evstwho(DisasContext *ctx, TCGv addr)
6724{
6725#if defined(TARGET_PPC64)
6726 TCGv t0 = tcg_temp_new();
6727 tcg_gen_shri_tl(t0, cpu_gpr[rS(ctx->opcode)], 32);
6728 gen_qemu_st16(t0, addr, ctx->mem_idx);
6729 tcg_temp_free(t0);
6730#else
6731 gen_qemu_st16(cpu_gprh[rS(ctx->opcode)], addr, ctx->mem_idx);
6732#endif
6733 tcg_gen_addi_tl(addr, addr, 2);
6734 gen_qemu_st16(cpu_gpr[rS(ctx->opcode)], addr, ctx->mem_idx);
6735}
6736
6737static always_inline void gen_op_evstwwe(DisasContext *ctx, TCGv addr)
6738{
6739#if defined(TARGET_PPC64)
6740 TCGv t0 = tcg_temp_new();
6741 tcg_gen_shri_tl(t0, cpu_gpr[rS(ctx->opcode)], 32);
6742 gen_qemu_st32(t0, addr, ctx->mem_idx);
6743 tcg_temp_free(t0);
6744#else
6745 gen_qemu_st32(cpu_gprh[rS(ctx->opcode)], addr, ctx->mem_idx);
6746#endif
6747}
6748
6749static always_inline void gen_op_evstwwo(DisasContext *ctx, TCGv addr)
6750{
6751 gen_qemu_st32(cpu_gpr[rS(ctx->opcode)], addr, ctx->mem_idx);
6752}
6753
6754#define GEN_SPEOP_LDST(name, opc2, sh) \
6755GEN_HANDLER(gen_##name, 0x04, opc2, 0x0C, 0x00000000, PPC_SPE) \
6756{ \
6757 TCGv t0; \
6758 if (unlikely(!ctx->spe_enabled)) { \
6759 GEN_EXCP_NO_AP(ctx); \
6760 return; \
6761 } \
6762 t0 = tcg_temp_new(); \
6763 if (Rc(ctx->opcode)) { \
6764 gen_addr_spe_imm_index(t0, ctx, sh); \
6765 } else { \
6766 gen_addr_reg_index(t0, ctx); \
6767 } \
6768 gen_op_##name(ctx, t0); \
6769 tcg_temp_free(t0); \
6770}
6771
6772GEN_SPEOP_LDST(evldd, 0x00, 3);
6773GEN_SPEOP_LDST(evldw, 0x01, 3);
6774GEN_SPEOP_LDST(evldh, 0x02, 3);
6775GEN_SPEOP_LDST(evlhhesplat, 0x04, 1);
6776GEN_SPEOP_LDST(evlhhousplat, 0x06, 1);
6777GEN_SPEOP_LDST(evlhhossplat, 0x07, 1);
6778GEN_SPEOP_LDST(evlwhe, 0x08, 2);
6779GEN_SPEOP_LDST(evlwhou, 0x0A, 2);
6780GEN_SPEOP_LDST(evlwhos, 0x0B, 2);
6781GEN_SPEOP_LDST(evlwwsplat, 0x0C, 2);
6782GEN_SPEOP_LDST(evlwhsplat, 0x0E, 2);
6783
6784GEN_SPEOP_LDST(evstdd, 0x10, 3);
6785GEN_SPEOP_LDST(evstdw, 0x11, 3);
6786GEN_SPEOP_LDST(evstdh, 0x12, 3);
6787GEN_SPEOP_LDST(evstwhe, 0x18, 2);
6788GEN_SPEOP_LDST(evstwho, 0x1A, 2);
6789GEN_SPEOP_LDST(evstwwe, 0x1C, 2);
6790GEN_SPEOP_LDST(evstwwo, 0x1E, 2);
0487d6a8
JM
6791
6792/* Multiply and add - TODO */
6793#if 0
6794GEN_SPE(speundef, evmhessf, 0x01, 0x10, 0x00000000, PPC_SPE);
6795GEN_SPE(speundef, evmhossf, 0x03, 0x10, 0x00000000, PPC_SPE);
6796GEN_SPE(evmheumi, evmhesmi, 0x04, 0x10, 0x00000000, PPC_SPE);
6797GEN_SPE(speundef, evmhesmf, 0x05, 0x10, 0x00000000, PPC_SPE);
6798GEN_SPE(evmhoumi, evmhosmi, 0x06, 0x10, 0x00000000, PPC_SPE);
6799GEN_SPE(speundef, evmhosmf, 0x07, 0x10, 0x00000000, PPC_SPE);
6800GEN_SPE(speundef, evmhessfa, 0x11, 0x10, 0x00000000, PPC_SPE);
6801GEN_SPE(speundef, evmhossfa, 0x13, 0x10, 0x00000000, PPC_SPE);
6802GEN_SPE(evmheumia, evmhesmia, 0x14, 0x10, 0x00000000, PPC_SPE);
6803GEN_SPE(speundef, evmhesmfa, 0x15, 0x10, 0x00000000, PPC_SPE);
6804GEN_SPE(evmhoumia, evmhosmia, 0x16, 0x10, 0x00000000, PPC_SPE);
6805GEN_SPE(speundef, evmhosmfa, 0x17, 0x10, 0x00000000, PPC_SPE);
6806
6807GEN_SPE(speundef, evmwhssf, 0x03, 0x11, 0x00000000, PPC_SPE);
6808GEN_SPE(evmwlumi, speundef, 0x04, 0x11, 0x00000000, PPC_SPE);
6809GEN_SPE(evmwhumi, evmwhsmi, 0x06, 0x11, 0x00000000, PPC_SPE);
6810GEN_SPE(speundef, evmwhsmf, 0x07, 0x11, 0x00000000, PPC_SPE);
6811GEN_SPE(speundef, evmwssf, 0x09, 0x11, 0x00000000, PPC_SPE);
6812GEN_SPE(evmwumi, evmwsmi, 0x0C, 0x11, 0x00000000, PPC_SPE);
6813GEN_SPE(speundef, evmwsmf, 0x0D, 0x11, 0x00000000, PPC_SPE);
6814GEN_SPE(speundef, evmwhssfa, 0x13, 0x11, 0x00000000, PPC_SPE);
6815GEN_SPE(evmwlumia, speundef, 0x14, 0x11, 0x00000000, PPC_SPE);
6816GEN_SPE(evmwhumia, evmwhsmia, 0x16, 0x11, 0x00000000, PPC_SPE);
6817GEN_SPE(speundef, evmwhsmfa, 0x17, 0x11, 0x00000000, PPC_SPE);
6818GEN_SPE(speundef, evmwssfa, 0x19, 0x11, 0x00000000, PPC_SPE);
6819GEN_SPE(evmwumia, evmwsmia, 0x1C, 0x11, 0x00000000, PPC_SPE);
6820GEN_SPE(speundef, evmwsmfa, 0x1D, 0x11, 0x00000000, PPC_SPE);
6821
6822GEN_SPE(evadduiaaw, evaddsiaaw, 0x00, 0x13, 0x0000F800, PPC_SPE);
6823GEN_SPE(evsubfusiaaw, evsubfssiaaw, 0x01, 0x13, 0x0000F800, PPC_SPE);
6824GEN_SPE(evaddumiaaw, evaddsmiaaw, 0x04, 0x13, 0x0000F800, PPC_SPE);
6825GEN_SPE(evsubfumiaaw, evsubfsmiaaw, 0x05, 0x13, 0x0000F800, PPC_SPE);
6826GEN_SPE(evdivws, evdivwu, 0x06, 0x13, 0x00000000, PPC_SPE);
6827GEN_SPE(evmra, speundef, 0x07, 0x13, 0x0000F800, PPC_SPE);
6828
6829GEN_SPE(evmheusiaaw, evmhessiaaw, 0x00, 0x14, 0x00000000, PPC_SPE);
6830GEN_SPE(speundef, evmhessfaaw, 0x01, 0x14, 0x00000000, PPC_SPE);
6831GEN_SPE(evmhousiaaw, evmhossiaaw, 0x02, 0x14, 0x00000000, PPC_SPE);
6832GEN_SPE(speundef, evmhossfaaw, 0x03, 0x14, 0x00000000, PPC_SPE);
6833GEN_SPE(evmheumiaaw, evmhesmiaaw, 0x04, 0x14, 0x00000000, PPC_SPE);
6834GEN_SPE(speundef, evmhesmfaaw, 0x05, 0x14, 0x00000000, PPC_SPE);
6835GEN_SPE(evmhoumiaaw, evmhosmiaaw, 0x06, 0x14, 0x00000000, PPC_SPE);
6836GEN_SPE(speundef, evmhosmfaaw, 0x07, 0x14, 0x00000000, PPC_SPE);
6837GEN_SPE(evmhegumiaa, evmhegsmiaa, 0x14, 0x14, 0x00000000, PPC_SPE);
6838GEN_SPE(speundef, evmhegsmfaa, 0x15, 0x14, 0x00000000, PPC_SPE);
6839GEN_SPE(evmhogumiaa, evmhogsmiaa, 0x16, 0x14, 0x00000000, PPC_SPE);
6840GEN_SPE(speundef, evmhogsmfaa, 0x17, 0x14, 0x00000000, PPC_SPE);
6841
6842GEN_SPE(evmwlusiaaw, evmwlssiaaw, 0x00, 0x15, 0x00000000, PPC_SPE);
6843GEN_SPE(evmwlumiaaw, evmwlsmiaaw, 0x04, 0x15, 0x00000000, PPC_SPE);
6844GEN_SPE(speundef, evmwssfaa, 0x09, 0x15, 0x00000000, PPC_SPE);
6845GEN_SPE(evmwumiaa, evmwsmiaa, 0x0C, 0x15, 0x00000000, PPC_SPE);
6846GEN_SPE(speundef, evmwsmfaa, 0x0D, 0x15, 0x00000000, PPC_SPE);
6847
6848GEN_SPE(evmheusianw, evmhessianw, 0x00, 0x16, 0x00000000, PPC_SPE);
6849GEN_SPE(speundef, evmhessfanw, 0x01, 0x16, 0x00000000, PPC_SPE);
6850GEN_SPE(evmhousianw, evmhossianw, 0x02, 0x16, 0x00000000, PPC_SPE);
6851GEN_SPE(speundef, evmhossfanw, 0x03, 0x16, 0x00000000, PPC_SPE);
6852GEN_SPE(evmheumianw, evmhesmianw, 0x04, 0x16, 0x00000000, PPC_SPE);
6853GEN_SPE(speundef, evmhesmfanw, 0x05, 0x16, 0x00000000, PPC_SPE);
6854GEN_SPE(evmhoumianw, evmhosmianw, 0x06, 0x16, 0x00000000, PPC_SPE);
6855GEN_SPE(speundef, evmhosmfanw, 0x07, 0x16, 0x00000000, PPC_SPE);
6856GEN_SPE(evmhegumian, evmhegsmian, 0x14, 0x16, 0x00000000, PPC_SPE);
6857GEN_SPE(speundef, evmhegsmfan, 0x15, 0x16, 0x00000000, PPC_SPE);
6858GEN_SPE(evmhigumian, evmhigsmian, 0x16, 0x16, 0x00000000, PPC_SPE);
6859GEN_SPE(speundef, evmhogsmfan, 0x17, 0x16, 0x00000000, PPC_SPE);
6860
6861GEN_SPE(evmwlusianw, evmwlssianw, 0x00, 0x17, 0x00000000, PPC_SPE);
6862GEN_SPE(evmwlumianw, evmwlsmianw, 0x04, 0x17, 0x00000000, PPC_SPE);
6863GEN_SPE(speundef, evmwssfan, 0x09, 0x17, 0x00000000, PPC_SPE);
6864GEN_SPE(evmwumian, evmwsmian, 0x0C, 0x17, 0x00000000, PPC_SPE);
6865GEN_SPE(speundef, evmwsmfan, 0x0D, 0x17, 0x00000000, PPC_SPE);
6866#endif
6867
6868/*** SPE floating-point extension ***/
1c97856d
AJ
6869#if defined(TARGET_PPC64)
6870#define GEN_SPEFPUOP_CONV_32_32(name) \
b068d6a7 6871static always_inline void gen_##name (DisasContext *ctx) \
0487d6a8 6872{ \
1c97856d
AJ
6873 TCGv_i32 t0; \
6874 TCGv t1; \
6875 t0 = tcg_temp_new_i32(); \
6876 tcg_gen_trunc_tl_i32(t0, cpu_gpr[rB(ctx->opcode)]); \
6877 gen_helper_##name(t0, t0); \
6878 t1 = tcg_temp_new(); \
6879 tcg_gen_extu_i32_tl(t1, t0); \
6880 tcg_temp_free_i32(t0); \
6881 tcg_gen_andi_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rD(ctx->opcode)], \
6882 0xFFFFFFFF00000000ULL); \
6883 tcg_gen_or_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rD(ctx->opcode)], t1); \
6884 tcg_temp_free(t1); \
0487d6a8 6885}
1c97856d
AJ
6886#define GEN_SPEFPUOP_CONV_32_64(name) \
6887static always_inline void gen_##name (DisasContext *ctx) \
6888{ \
6889 TCGv_i32 t0; \
6890 TCGv t1; \
6891 t0 = tcg_temp_new_i32(); \
6892 gen_helper_##name(t0, cpu_gpr[rB(ctx->opcode)]); \
6893 t1 = tcg_temp_new(); \
6894 tcg_gen_extu_i32_tl(t1, t0); \
6895 tcg_temp_free_i32(t0); \
6896 tcg_gen_andi_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rD(ctx->opcode)], \
6897 0xFFFFFFFF00000000ULL); \
6898 tcg_gen_or_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rD(ctx->opcode)], t1); \
6899 tcg_temp_free(t1); \
6900}
6901#define GEN_SPEFPUOP_CONV_64_32(name) \
6902static always_inline void gen_##name (DisasContext *ctx) \
6903{ \
6904 TCGv_i32 t0 = tcg_temp_new_i32(); \
6905 tcg_gen_trunc_tl_i32(t0, cpu_gpr[rB(ctx->opcode)]); \
6906 gen_helper_##name(cpu_gpr[rD(ctx->opcode)], t0); \
6907 tcg_temp_free_i32(t0); \
6908}
6909#define GEN_SPEFPUOP_CONV_64_64(name) \
6910static always_inline void gen_##name (DisasContext *ctx) \
6911{ \
6912 gen_helper_##name(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rB(ctx->opcode)]); \
6913}
6914#define GEN_SPEFPUOP_ARITH2_32_32(name) \
57951c27
AJ
6915static always_inline void gen_##name (DisasContext *ctx) \
6916{ \
1c97856d
AJ
6917 TCGv_i32 t0, t1; \
6918 TCGv_i64 t2; \
57951c27
AJ
6919 if (unlikely(!ctx->spe_enabled)) { \
6920 GEN_EXCP_NO_AP(ctx); \
6921 return; \
6922 } \
1c97856d
AJ
6923 t0 = tcg_temp_new_i32(); \
6924 t1 = tcg_temp_new_i32(); \
6925 tcg_gen_trunc_tl_i32(t0, cpu_gpr[rA(ctx->opcode)]); \
6926 tcg_gen_trunc_tl_i32(t1, cpu_gpr[rB(ctx->opcode)]); \
6927 gen_helper_##name(t0, t0, t1); \
6928 tcg_temp_free_i32(t1); \
6929 t2 = tcg_temp_new(); \
6930 tcg_gen_extu_i32_tl(t2, t0); \
6931 tcg_temp_free_i32(t0); \
6932 tcg_gen_andi_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rD(ctx->opcode)], \
6933 0xFFFFFFFF00000000ULL); \
6934 tcg_gen_or_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rD(ctx->opcode)], t2); \
6935 tcg_temp_free(t2); \
57951c27 6936}
1c97856d 6937#define GEN_SPEFPUOP_ARITH2_64_64(name) \
57951c27
AJ
6938static always_inline void gen_##name (DisasContext *ctx) \
6939{ \
6940 if (unlikely(!ctx->spe_enabled)) { \
6941 GEN_EXCP_NO_AP(ctx); \
6942 return; \
6943 } \
1c97856d
AJ
6944 gen_helper_##name(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)], \
6945 cpu_gpr[rB(ctx->opcode)]); \
57951c27 6946}
1c97856d 6947#define GEN_SPEFPUOP_COMP_32(name) \
57951c27
AJ
6948static always_inline void gen_##name (DisasContext *ctx) \
6949{ \
1c97856d 6950 TCGv_i32 t0, t1; \
57951c27
AJ
6951 if (unlikely(!ctx->spe_enabled)) { \
6952 GEN_EXCP_NO_AP(ctx); \
6953 return; \
6954 } \
1c97856d
AJ
6955 t0 = tcg_temp_new_i32(); \
6956 t1 = tcg_temp_new_i32(); \
6957 tcg_gen_trunc_tl_i32(t0, cpu_gpr[rA(ctx->opcode)]); \
6958 tcg_gen_trunc_tl_i32(t1, cpu_gpr[rB(ctx->opcode)]); \
6959 gen_helper_##name(cpu_crf[crfD(ctx->opcode)], t0, t1); \
6960 tcg_temp_free_i32(t0); \
6961 tcg_temp_free_i32(t1); \
6962}
6963#define GEN_SPEFPUOP_COMP_64(name) \
6964static always_inline void gen_##name (DisasContext *ctx) \
6965{ \
6966 if (unlikely(!ctx->spe_enabled)) { \
6967 GEN_EXCP_NO_AP(ctx); \
6968 return; \
6969 } \
6970 gen_helper_##name(cpu_crf[crfD(ctx->opcode)], \
6971 cpu_gpr[rA(ctx->opcode)], cpu_gpr[rB(ctx->opcode)]); \
6972}
6973#else
6974#define GEN_SPEFPUOP_CONV_32_32(name) \
6975static always_inline void gen_##name (DisasContext *ctx) \
6976{ \
6977 gen_helper_##name(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rB(ctx->opcode)]); \
57951c27 6978}
1c97856d
AJ
6979#define GEN_SPEFPUOP_CONV_32_64(name) \
6980static always_inline void gen_##name (DisasContext *ctx) \
6981{ \
6982 TCGv_i64 t0 = tcg_temp_new_i64(); \
6983 gen_load_gpr64(t0, rB(ctx->opcode)); \
6984 gen_helper_##name(cpu_gpr[rD(ctx->opcode)], t0); \
6985 tcg_temp_free_i64(t0); \
6986}
6987#define GEN_SPEFPUOP_CONV_64_32(name) \
6988static always_inline void gen_##name (DisasContext *ctx) \
6989{ \
6990 TCGv_i64 t0 = tcg_temp_new_i64(); \
6991 gen_helper_##name(t0, cpu_gpr[rB(ctx->opcode)]); \
6992 gen_store_gpr64(rD(ctx->opcode), t0); \
6993 tcg_temp_free_i64(t0); \
6994}
6995#define GEN_SPEFPUOP_CONV_64_64(name) \
6996static always_inline void gen_##name (DisasContext *ctx) \
6997{ \
6998 TCGv_i64 t0 = tcg_temp_new_i64(); \
6999 gen_load_gpr64(t0, rB(ctx->opcode)); \
7000 gen_helper_##name(t0, t0); \
7001 gen_store_gpr64(rD(ctx->opcode), t0); \
7002 tcg_temp_free_i64(t0); \
7003}
7004#define GEN_SPEFPUOP_ARITH2_32_32(name) \
7005static always_inline void gen_##name (DisasContext *ctx) \
7006{ \
7007 if (unlikely(!ctx->spe_enabled)) { \
7008 GEN_EXCP_NO_AP(ctx); \
7009 return; \
7010 } \
7011 gen_helper_##name(cpu_gpr[rD(ctx->opcode)], \
7012 cpu_gpr[rA(ctx->opcode)], cpu_gpr[rB(ctx->opcode)]); \
7013}
7014#define GEN_SPEFPUOP_ARITH2_64_64(name) \
7015static always_inline void gen_##name (DisasContext *ctx) \
7016{ \
7017 TCGv_i64 t0, t1; \
7018 if (unlikely(!ctx->spe_enabled)) { \
7019 GEN_EXCP_NO_AP(ctx); \
7020 return; \
7021 } \
7022 t0 = tcg_temp_new_i64(); \
7023 t1 = tcg_temp_new_i64(); \
7024 gen_load_gpr64(t0, rA(ctx->opcode)); \
7025 gen_load_gpr64(t1, rB(ctx->opcode)); \
7026 gen_helper_##name(t0, t0, t1); \
7027 gen_store_gpr64(rD(ctx->opcode), t0); \
7028 tcg_temp_free_i64(t0); \
7029 tcg_temp_free_i64(t1); \
7030}
7031#define GEN_SPEFPUOP_COMP_32(name) \
7032static always_inline void gen_##name (DisasContext *ctx) \
7033{ \
7034 if (unlikely(!ctx->spe_enabled)) { \
7035 GEN_EXCP_NO_AP(ctx); \
7036 return; \
7037 } \
7038 gen_helper_##name(cpu_crf[crfD(ctx->opcode)], \
7039 cpu_gpr[rA(ctx->opcode)], cpu_gpr[rB(ctx->opcode)]); \
7040}
7041#define GEN_SPEFPUOP_COMP_64(name) \
7042static always_inline void gen_##name (DisasContext *ctx) \
7043{ \
7044 TCGv_i64 t0, t1; \
7045 if (unlikely(!ctx->spe_enabled)) { \
7046 GEN_EXCP_NO_AP(ctx); \
7047 return; \
7048 } \
7049 t0 = tcg_temp_new_i64(); \
7050 t1 = tcg_temp_new_i64(); \
7051 gen_load_gpr64(t0, rA(ctx->opcode)); \
7052 gen_load_gpr64(t1, rB(ctx->opcode)); \
7053 gen_helper_##name(cpu_crf[crfD(ctx->opcode)], t0, t1); \
7054 tcg_temp_free_i64(t0); \
7055 tcg_temp_free_i64(t1); \
7056}
7057#endif
57951c27 7058
0487d6a8
JM
7059/* Single precision floating-point vectors operations */
7060/* Arithmetic */
1c97856d
AJ
7061GEN_SPEFPUOP_ARITH2_64_64(evfsadd);
7062GEN_SPEFPUOP_ARITH2_64_64(evfssub);
7063GEN_SPEFPUOP_ARITH2_64_64(evfsmul);
7064GEN_SPEFPUOP_ARITH2_64_64(evfsdiv);
7065static always_inline void gen_evfsabs (DisasContext *ctx)
7066{
7067 if (unlikely(!ctx->spe_enabled)) {
7068 GEN_EXCP_NO_AP(ctx);
7069 return;
7070 }
7071#if defined(TARGET_PPC64)
7072 tcg_gen_andi_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rA(ctx->opcode)], ~0x8000000080000000LL);
7073#else
7074 tcg_gen_andi_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rA(ctx->opcode)], ~0x80000000);
7075 tcg_gen_andi_tl(cpu_gprh[rA(ctx->opcode)], cpu_gprh[rA(ctx->opcode)], ~0x80000000);
7076#endif
7077}
7078static always_inline void gen_evfsnabs (DisasContext *ctx)
7079{
7080 if (unlikely(!ctx->spe_enabled)) {
7081 GEN_EXCP_NO_AP(ctx);
7082 return;
7083 }
7084#if defined(TARGET_PPC64)
7085 tcg_gen_ori_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rA(ctx->opcode)], 0x8000000080000000LL);
7086#else
7087 tcg_gen_ori_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rA(ctx->opcode)], 0x80000000);
7088 tcg_gen_ori_tl(cpu_gprh[rA(ctx->opcode)], cpu_gprh[rA(ctx->opcode)], 0x80000000);
7089#endif
7090}
7091static always_inline void gen_evfsneg (DisasContext *ctx)
7092{
7093 if (unlikely(!ctx->spe_enabled)) {
7094 GEN_EXCP_NO_AP(ctx);
7095 return;
7096 }
7097#if defined(TARGET_PPC64)
7098 tcg_gen_xori_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rA(ctx->opcode)], 0x8000000080000000LL);
7099#else
7100 tcg_gen_xori_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rA(ctx->opcode)], 0x80000000);
7101 tcg_gen_xori_tl(cpu_gprh[rA(ctx->opcode)], cpu_gprh[rA(ctx->opcode)], 0x80000000);
7102#endif
7103}
7104
0487d6a8 7105/* Conversion */
1c97856d
AJ
7106GEN_SPEFPUOP_CONV_64_64(evfscfui);
7107GEN_SPEFPUOP_CONV_64_64(evfscfsi);
7108GEN_SPEFPUOP_CONV_64_64(evfscfuf);
7109GEN_SPEFPUOP_CONV_64_64(evfscfsf);
7110GEN_SPEFPUOP_CONV_64_64(evfsctui);
7111GEN_SPEFPUOP_CONV_64_64(evfsctsi);
7112GEN_SPEFPUOP_CONV_64_64(evfsctuf);
7113GEN_SPEFPUOP_CONV_64_64(evfsctsf);
7114GEN_SPEFPUOP_CONV_64_64(evfsctuiz);
7115GEN_SPEFPUOP_CONV_64_64(evfsctsiz);
7116
0487d6a8 7117/* Comparison */
1c97856d
AJ
7118GEN_SPEFPUOP_COMP_64(evfscmpgt);
7119GEN_SPEFPUOP_COMP_64(evfscmplt);
7120GEN_SPEFPUOP_COMP_64(evfscmpeq);
7121GEN_SPEFPUOP_COMP_64(evfststgt);
7122GEN_SPEFPUOP_COMP_64(evfststlt);
7123GEN_SPEFPUOP_COMP_64(evfststeq);
0487d6a8
JM
7124
7125/* Opcodes definitions */
7126GEN_SPE(evfsadd, evfssub, 0x00, 0x0A, 0x00000000, PPC_SPEFPU); //
7127GEN_SPE(evfsabs, evfsnabs, 0x02, 0x0A, 0x0000F800, PPC_SPEFPU); //
7128GEN_SPE(evfsneg, speundef, 0x03, 0x0A, 0x0000F800, PPC_SPEFPU); //
7129GEN_SPE(evfsmul, evfsdiv, 0x04, 0x0A, 0x00000000, PPC_SPEFPU); //
7130GEN_SPE(evfscmpgt, evfscmplt, 0x06, 0x0A, 0x00600000, PPC_SPEFPU); //
7131GEN_SPE(evfscmpeq, speundef, 0x07, 0x0A, 0x00600000, PPC_SPEFPU); //
7132GEN_SPE(evfscfui, evfscfsi, 0x08, 0x0A, 0x00180000, PPC_SPEFPU); //
7133GEN_SPE(evfscfuf, evfscfsf, 0x09, 0x0A, 0x00180000, PPC_SPEFPU); //
7134GEN_SPE(evfsctui, evfsctsi, 0x0A, 0x0A, 0x00180000, PPC_SPEFPU); //
7135GEN_SPE(evfsctuf, evfsctsf, 0x0B, 0x0A, 0x00180000, PPC_SPEFPU); //
7136GEN_SPE(evfsctuiz, speundef, 0x0C, 0x0A, 0x00180000, PPC_SPEFPU); //
7137GEN_SPE(evfsctsiz, speundef, 0x0D, 0x0A, 0x00180000, PPC_SPEFPU); //
7138GEN_SPE(evfststgt, evfststlt, 0x0E, 0x0A, 0x00600000, PPC_SPEFPU); //
7139GEN_SPE(evfststeq, speundef, 0x0F, 0x0A, 0x00600000, PPC_SPEFPU); //
7140
7141/* Single precision floating-point operations */
7142/* Arithmetic */
1c97856d
AJ
7143GEN_SPEFPUOP_ARITH2_32_32(efsadd);
7144GEN_SPEFPUOP_ARITH2_32_32(efssub);
7145GEN_SPEFPUOP_ARITH2_32_32(efsmul);
7146GEN_SPEFPUOP_ARITH2_32_32(efsdiv);
7147static always_inline void gen_efsabs (DisasContext *ctx)
7148{
7149 if (unlikely(!ctx->spe_enabled)) {
7150 GEN_EXCP_NO_AP(ctx);
7151 return;
7152 }
7153 tcg_gen_andi_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rA(ctx->opcode)], (target_long)~0x80000000LL);
7154}
7155static always_inline void gen_efsnabs (DisasContext *ctx)
7156{
7157 if (unlikely(!ctx->spe_enabled)) {
7158 GEN_EXCP_NO_AP(ctx);
7159 return;
7160 }
7161 tcg_gen_ori_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rA(ctx->opcode)], 0x80000000);
7162}
7163static always_inline void gen_efsneg (DisasContext *ctx)
7164{
7165 if (unlikely(!ctx->spe_enabled)) {
7166 GEN_EXCP_NO_AP(ctx);
7167 return;
7168 }
7169 tcg_gen_xori_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rA(ctx->opcode)], 0x80000000);
7170}
7171
0487d6a8 7172/* Conversion */
1c97856d
AJ
7173GEN_SPEFPUOP_CONV_32_32(efscfui);
7174GEN_SPEFPUOP_CONV_32_32(efscfsi);
7175GEN_SPEFPUOP_CONV_32_32(efscfuf);
7176GEN_SPEFPUOP_CONV_32_32(efscfsf);
7177GEN_SPEFPUOP_CONV_32_32(efsctui);
7178GEN_SPEFPUOP_CONV_32_32(efsctsi);
7179GEN_SPEFPUOP_CONV_32_32(efsctuf);
7180GEN_SPEFPUOP_CONV_32_32(efsctsf);
7181GEN_SPEFPUOP_CONV_32_32(efsctuiz);
7182GEN_SPEFPUOP_CONV_32_32(efsctsiz);
7183GEN_SPEFPUOP_CONV_32_64(efscfd);
7184
0487d6a8 7185/* Comparison */
1c97856d
AJ
7186GEN_SPEFPUOP_COMP_32(efscmpgt);
7187GEN_SPEFPUOP_COMP_32(efscmplt);
7188GEN_SPEFPUOP_COMP_32(efscmpeq);
7189GEN_SPEFPUOP_COMP_32(efststgt);
7190GEN_SPEFPUOP_COMP_32(efststlt);
7191GEN_SPEFPUOP_COMP_32(efststeq);
0487d6a8
JM
7192
7193/* Opcodes definitions */
05332d70 7194GEN_SPE(efsadd, efssub, 0x00, 0x0B, 0x00000000, PPC_SPEFPU); //
0487d6a8
JM
7195GEN_SPE(efsabs, efsnabs, 0x02, 0x0B, 0x0000F800, PPC_SPEFPU); //
7196GEN_SPE(efsneg, speundef, 0x03, 0x0B, 0x0000F800, PPC_SPEFPU); //
7197GEN_SPE(efsmul, efsdiv, 0x04, 0x0B, 0x00000000, PPC_SPEFPU); //
7198GEN_SPE(efscmpgt, efscmplt, 0x06, 0x0B, 0x00600000, PPC_SPEFPU); //
7199GEN_SPE(efscmpeq, efscfd, 0x07, 0x0B, 0x00600000, PPC_SPEFPU); //
7200GEN_SPE(efscfui, efscfsi, 0x08, 0x0B, 0x00180000, PPC_SPEFPU); //
7201GEN_SPE(efscfuf, efscfsf, 0x09, 0x0B, 0x00180000, PPC_SPEFPU); //
7202GEN_SPE(efsctui, efsctsi, 0x0A, 0x0B, 0x00180000, PPC_SPEFPU); //
7203GEN_SPE(efsctuf, efsctsf, 0x0B, 0x0B, 0x00180000, PPC_SPEFPU); //
9ceb2a77
TS
7204GEN_SPE(efsctuiz, speundef, 0x0C, 0x0B, 0x00180000, PPC_SPEFPU); //
7205GEN_SPE(efsctsiz, speundef, 0x0D, 0x0B, 0x00180000, PPC_SPEFPU); //
0487d6a8
JM
7206GEN_SPE(efststgt, efststlt, 0x0E, 0x0B, 0x00600000, PPC_SPEFPU); //
7207GEN_SPE(efststeq, speundef, 0x0F, 0x0B, 0x00600000, PPC_SPEFPU); //
7208
7209/* Double precision floating-point operations */
7210/* Arithmetic */
1c97856d
AJ
7211GEN_SPEFPUOP_ARITH2_64_64(efdadd);
7212GEN_SPEFPUOP_ARITH2_64_64(efdsub);
7213GEN_SPEFPUOP_ARITH2_64_64(efdmul);
7214GEN_SPEFPUOP_ARITH2_64_64(efddiv);
7215static always_inline void gen_efdabs (DisasContext *ctx)
7216{
7217 if (unlikely(!ctx->spe_enabled)) {
7218 GEN_EXCP_NO_AP(ctx);
7219 return;
7220 }
7221#if defined(TARGET_PPC64)
7222 tcg_gen_andi_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rA(ctx->opcode)], ~0x8000000000000000LL);
7223#else
7224 tcg_gen_andi_tl(cpu_gprh[rA(ctx->opcode)], cpu_gprh[rA(ctx->opcode)], ~0x80000000);
7225#endif
7226}
7227static always_inline void gen_efdnabs (DisasContext *ctx)
7228{
7229 if (unlikely(!ctx->spe_enabled)) {
7230 GEN_EXCP_NO_AP(ctx);
7231 return;
7232 }
7233#if defined(TARGET_PPC64)
7234 tcg_gen_ori_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rA(ctx->opcode)], 0x8000000000000000LL);
7235#else
7236 tcg_gen_ori_tl(cpu_gprh[rA(ctx->opcode)], cpu_gprh[rA(ctx->opcode)], 0x80000000);
7237#endif
7238}
7239static always_inline void gen_efdneg (DisasContext *ctx)
7240{
7241 if (unlikely(!ctx->spe_enabled)) {
7242 GEN_EXCP_NO_AP(ctx);
7243 return;
7244 }
7245#if defined(TARGET_PPC64)
7246 tcg_gen_xori_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rA(ctx->opcode)], 0x8000000000000000LL);
7247#else
7248 tcg_gen_xori_tl(cpu_gprh[rA(ctx->opcode)], cpu_gprh[rA(ctx->opcode)], 0x80000000);
7249#endif
7250}
7251
0487d6a8 7252/* Conversion */
1c97856d
AJ
7253GEN_SPEFPUOP_CONV_64_32(efdcfui);
7254GEN_SPEFPUOP_CONV_64_32(efdcfsi);
7255GEN_SPEFPUOP_CONV_64_32(efdcfuf);
7256GEN_SPEFPUOP_CONV_64_32(efdcfsf);
7257GEN_SPEFPUOP_CONV_32_64(efdctui);
7258GEN_SPEFPUOP_CONV_32_64(efdctsi);
7259GEN_SPEFPUOP_CONV_32_64(efdctuf);
7260GEN_SPEFPUOP_CONV_32_64(efdctsf);
7261GEN_SPEFPUOP_CONV_32_64(efdctuiz);
7262GEN_SPEFPUOP_CONV_32_64(efdctsiz);
7263GEN_SPEFPUOP_CONV_64_32(efdcfs);
7264GEN_SPEFPUOP_CONV_64_64(efdcfuid);
7265GEN_SPEFPUOP_CONV_64_64(efdcfsid);
7266GEN_SPEFPUOP_CONV_64_64(efdctuidz);
7267GEN_SPEFPUOP_CONV_64_64(efdctsidz);
0487d6a8 7268
0487d6a8 7269/* Comparison */
1c97856d
AJ
7270GEN_SPEFPUOP_COMP_64(efdcmpgt);
7271GEN_SPEFPUOP_COMP_64(efdcmplt);
7272GEN_SPEFPUOP_COMP_64(efdcmpeq);
7273GEN_SPEFPUOP_COMP_64(efdtstgt);
7274GEN_SPEFPUOP_COMP_64(efdtstlt);
7275GEN_SPEFPUOP_COMP_64(efdtsteq);
0487d6a8
JM
7276
7277/* Opcodes definitions */
7278GEN_SPE(efdadd, efdsub, 0x10, 0x0B, 0x00000000, PPC_SPEFPU); //
7279GEN_SPE(efdcfuid, efdcfsid, 0x11, 0x0B, 0x00180000, PPC_SPEFPU); //
7280GEN_SPE(efdabs, efdnabs, 0x12, 0x0B, 0x0000F800, PPC_SPEFPU); //
7281GEN_SPE(efdneg, speundef, 0x13, 0x0B, 0x0000F800, PPC_SPEFPU); //
7282GEN_SPE(efdmul, efddiv, 0x14, 0x0B, 0x00000000, PPC_SPEFPU); //
7283GEN_SPE(efdctuidz, efdctsidz, 0x15, 0x0B, 0x00180000, PPC_SPEFPU); //
7284GEN_SPE(efdcmpgt, efdcmplt, 0x16, 0x0B, 0x00600000, PPC_SPEFPU); //
7285GEN_SPE(efdcmpeq, efdcfs, 0x17, 0x0B, 0x00600000, PPC_SPEFPU); //
7286GEN_SPE(efdcfui, efdcfsi, 0x18, 0x0B, 0x00180000, PPC_SPEFPU); //
7287GEN_SPE(efdcfuf, efdcfsf, 0x19, 0x0B, 0x00180000, PPC_SPEFPU); //
7288GEN_SPE(efdctui, efdctsi, 0x1A, 0x0B, 0x00180000, PPC_SPEFPU); //
7289GEN_SPE(efdctuf, efdctsf, 0x1B, 0x0B, 0x00180000, PPC_SPEFPU); //
7290GEN_SPE(efdctuiz, speundef, 0x1C, 0x0B, 0x00180000, PPC_SPEFPU); //
7291GEN_SPE(efdctsiz, speundef, 0x1D, 0x0B, 0x00180000, PPC_SPEFPU); //
7292GEN_SPE(efdtstgt, efdtstlt, 0x1E, 0x0B, 0x00600000, PPC_SPEFPU); //
7293GEN_SPE(efdtsteq, speundef, 0x1F, 0x0B, 0x00600000, PPC_SPEFPU); //
0487d6a8 7294
79aceca5
FB
7295/* End opcode list */
7296GEN_OPCODE_MARK(end);
7297
3fc6c082 7298#include "translate_init.c"
0411a972 7299#include "helper_regs.h"
79aceca5 7300
9a64fbe4 7301/*****************************************************************************/
3fc6c082 7302/* Misc PowerPC helpers */
36081602
JM
7303void cpu_dump_state (CPUState *env, FILE *f,
7304 int (*cpu_fprintf)(FILE *f, const char *fmt, ...),
7305 int flags)
79aceca5 7306{
3fc6c082
FB
7307#define RGPL 4
7308#define RFPL 4
3fc6c082 7309
79aceca5
FB
7310 int i;
7311
077fc206 7312 cpu_fprintf(f, "NIP " ADDRX " LR " ADDRX " CTR " ADDRX " XER %08x\n",
3d7b417e 7313 env->nip, env->lr, env->ctr, env->xer);
6b542af7
JM
7314 cpu_fprintf(f, "MSR " ADDRX " HID0 " ADDRX " HF " ADDRX " idx %d\n",
7315 env->msr, env->spr[SPR_HID0], env->hflags, env->mmu_idx);
d9bce9d9 7316#if !defined(NO_TIMER_DUMP)
077fc206 7317 cpu_fprintf(f, "TB %08x %08x "
76a66253
JM
7318#if !defined(CONFIG_USER_ONLY)
7319 "DECR %08x"
7320#endif
7321 "\n",
077fc206 7322 cpu_ppc_load_tbu(env), cpu_ppc_load_tbl(env)
76a66253
JM
7323#if !defined(CONFIG_USER_ONLY)
7324 , cpu_ppc_load_decr(env)
7325#endif
7326 );
077fc206 7327#endif
76a66253 7328 for (i = 0; i < 32; i++) {
3fc6c082
FB
7329 if ((i & (RGPL - 1)) == 0)
7330 cpu_fprintf(f, "GPR%02d", i);
6b542af7 7331 cpu_fprintf(f, " " REGX, ppc_dump_gpr(env, i));
3fc6c082 7332 if ((i & (RGPL - 1)) == (RGPL - 1))
7fe48483 7333 cpu_fprintf(f, "\n");
76a66253 7334 }
3fc6c082 7335 cpu_fprintf(f, "CR ");
76a66253 7336 for (i = 0; i < 8; i++)
7fe48483
FB
7337 cpu_fprintf(f, "%01x", env->crf[i]);
7338 cpu_fprintf(f, " [");
76a66253
JM
7339 for (i = 0; i < 8; i++) {
7340 char a = '-';
7341 if (env->crf[i] & 0x08)
7342 a = 'L';
7343 else if (env->crf[i] & 0x04)
7344 a = 'G';
7345 else if (env->crf[i] & 0x02)
7346 a = 'E';
7fe48483 7347 cpu_fprintf(f, " %c%c", a, env->crf[i] & 0x01 ? 'O' : ' ');
76a66253 7348 }
6b542af7 7349 cpu_fprintf(f, " ] RES " ADDRX "\n", env->reserve);
3fc6c082
FB
7350 for (i = 0; i < 32; i++) {
7351 if ((i & (RFPL - 1)) == 0)
7352 cpu_fprintf(f, "FPR%02d", i);
26a76461 7353 cpu_fprintf(f, " %016" PRIx64, *((uint64_t *)&env->fpr[i]));
3fc6c082 7354 if ((i & (RFPL - 1)) == (RFPL - 1))
7fe48483 7355 cpu_fprintf(f, "\n");
79aceca5 7356 }
f2e63a42 7357#if !defined(CONFIG_USER_ONLY)
6b542af7 7358 cpu_fprintf(f, "SRR0 " ADDRX " SRR1 " ADDRX " SDR1 " ADDRX "\n",
3fc6c082 7359 env->spr[SPR_SRR0], env->spr[SPR_SRR1], env->sdr1);
f2e63a42 7360#endif
79aceca5 7361
3fc6c082
FB
7362#undef RGPL
7363#undef RFPL
79aceca5
FB
7364}
7365
76a66253
JM
7366void cpu_dump_statistics (CPUState *env, FILE*f,
7367 int (*cpu_fprintf)(FILE *f, const char *fmt, ...),
7368 int flags)
7369{
7370#if defined(DO_PPC_STATISTICS)
7371 opc_handler_t **t1, **t2, **t3, *handler;
7372 int op1, op2, op3;
7373
7374 t1 = env->opcodes;
7375 for (op1 = 0; op1 < 64; op1++) {
7376 handler = t1[op1];
7377 if (is_indirect_opcode(handler)) {
7378 t2 = ind_table(handler);
7379 for (op2 = 0; op2 < 32; op2++) {
7380 handler = t2[op2];
7381 if (is_indirect_opcode(handler)) {
7382 t3 = ind_table(handler);
7383 for (op3 = 0; op3 < 32; op3++) {
7384 handler = t3[op3];
7385 if (handler->count == 0)
7386 continue;
7387 cpu_fprintf(f, "%02x %02x %02x (%02x %04d) %16s: "
7388 "%016llx %lld\n",
7389 op1, op2, op3, op1, (op3 << 5) | op2,
7390 handler->oname,
7391 handler->count, handler->count);
7392 }
7393 } else {
7394 if (handler->count == 0)
7395 continue;
7396 cpu_fprintf(f, "%02x %02x (%02x %04d) %16s: "
7397 "%016llx %lld\n",
7398 op1, op2, op1, op2, handler->oname,
7399 handler->count, handler->count);
7400 }
7401 }
7402 } else {
7403 if (handler->count == 0)
7404 continue;
7405 cpu_fprintf(f, "%02x (%02x ) %16s: %016llx %lld\n",
7406 op1, op1, handler->oname,
7407 handler->count, handler->count);
7408 }
7409 }
7410#endif
7411}
7412
9a64fbe4 7413/*****************************************************************************/
2cfc5f17
TS
7414static always_inline void gen_intermediate_code_internal (CPUState *env,
7415 TranslationBlock *tb,
7416 int search_pc)
79aceca5 7417{
9fddaa0c 7418 DisasContext ctx, *ctxp = &ctx;
79aceca5 7419 opc_handler_t **table, *handler;
0fa85d43 7420 target_ulong pc_start;
79aceca5 7421 uint16_t *gen_opc_end;
056401ea 7422 int supervisor, little_endian;
a1d1bb31 7423 CPUBreakpoint *bp;
79aceca5 7424 int j, lj = -1;
2e70f6ef
PB
7425 int num_insns;
7426 int max_insns;
79aceca5
FB
7427
7428 pc_start = tb->pc;
79aceca5 7429 gen_opc_end = gen_opc_buf + OPC_MAX_SIZE;
7c58044c
JM
7430#if defined(OPTIMIZE_FPRF_UPDATE)
7431 gen_fprf_ptr = gen_fprf_buf;
7432#endif
046d6672 7433 ctx.nip = pc_start;
79aceca5 7434 ctx.tb = tb;
e1833e1f 7435 ctx.exception = POWERPC_EXCP_NONE;
3fc6c082 7436 ctx.spr_cb = env->spr_cb;
6ebbf390
JM
7437 supervisor = env->mmu_idx;
7438#if !defined(CONFIG_USER_ONLY)
2857068e 7439 ctx.supervisor = supervisor;
d9bce9d9 7440#endif
056401ea 7441 little_endian = env->hflags & (1 << MSR_LE) ? 1 : 0;
d9bce9d9
JM
7442#if defined(TARGET_PPC64)
7443 ctx.sf_mode = msr_sf;
056401ea 7444 ctx.mem_idx = (supervisor << 2) | (msr_sf << 1) | little_endian;
2857068e 7445#else
056401ea 7446 ctx.mem_idx = (supervisor << 1) | little_endian;
9a64fbe4 7447#endif
3cc62370 7448 ctx.fpu_enabled = msr_fp;
a9d9eb8f 7449 if ((env->flags & POWERPC_FLAG_SPE) && msr_spe)
d26bfc9a
JM
7450 ctx.spe_enabled = msr_spe;
7451 else
7452 ctx.spe_enabled = 0;
a9d9eb8f
JM
7453 if ((env->flags & POWERPC_FLAG_VRE) && msr_vr)
7454 ctx.altivec_enabled = msr_vr;
7455 else
7456 ctx.altivec_enabled = 0;
d26bfc9a 7457 if ((env->flags & POWERPC_FLAG_SE) && msr_se)
8cbcb4fa 7458 ctx.singlestep_enabled = CPU_SINGLE_STEP;
d26bfc9a 7459 else
8cbcb4fa 7460 ctx.singlestep_enabled = 0;
d26bfc9a 7461 if ((env->flags & POWERPC_FLAG_BE) && msr_be)
8cbcb4fa
AJ
7462 ctx.singlestep_enabled |= CPU_BRANCH_STEP;
7463 if (unlikely(env->singlestep_enabled))
7464 ctx.singlestep_enabled |= GDBSTUB_SINGLE_STEP;
3fc6c082 7465#if defined (DO_SINGLE_STEP) && 0
9a64fbe4
FB
7466 /* Single step trace mode */
7467 msr_se = 1;
7468#endif
2e70f6ef
PB
7469 num_insns = 0;
7470 max_insns = tb->cflags & CF_COUNT_MASK;
7471 if (max_insns == 0)
7472 max_insns = CF_COUNT_MASK;
7473
7474 gen_icount_start();
9a64fbe4 7475 /* Set env in case of segfault during code fetch */
e1833e1f 7476 while (ctx.exception == POWERPC_EXCP_NONE && gen_opc_ptr < gen_opc_end) {
c0ce998e
AL
7477 if (unlikely(!TAILQ_EMPTY(&env->breakpoints))) {
7478 TAILQ_FOREACH(bp, &env->breakpoints, entry) {
a1d1bb31 7479 if (bp->pc == ctx.nip) {
5fafdf24 7480 gen_update_nip(&ctx, ctx.nip);
64adab3f 7481 gen_helper_raise_debug();
ea4e754f
FB
7482 break;
7483 }
7484 }
7485 }
76a66253 7486 if (unlikely(search_pc)) {
79aceca5
FB
7487 j = gen_opc_ptr - gen_opc_buf;
7488 if (lj < j) {
7489 lj++;
7490 while (lj < j)
7491 gen_opc_instr_start[lj++] = 0;
046d6672 7492 gen_opc_pc[lj] = ctx.nip;
79aceca5 7493 gen_opc_instr_start[lj] = 1;
2e70f6ef 7494 gen_opc_icount[lj] = num_insns;
79aceca5
FB
7495 }
7496 }
9fddaa0c
FB
7497#if defined PPC_DEBUG_DISAS
7498 if (loglevel & CPU_LOG_TB_IN_ASM) {
79aceca5 7499 fprintf(logfile, "----------------\n");
1b9eb036 7500 fprintf(logfile, "nip=" ADDRX " super=%d ir=%d\n",
0411a972 7501 ctx.nip, supervisor, (int)msr_ir);
9a64fbe4
FB
7502 }
7503#endif
2e70f6ef
PB
7504 if (num_insns + 1 == max_insns && (tb->cflags & CF_LAST_IO))
7505 gen_io_start();
056401ea
JM
7506 if (unlikely(little_endian)) {
7507 ctx.opcode = bswap32(ldl_code(ctx.nip));
7508 } else {
7509 ctx.opcode = ldl_code(ctx.nip);
111bfab3 7510 }
9fddaa0c
FB
7511#if defined PPC_DEBUG_DISAS
7512 if (loglevel & CPU_LOG_TB_IN_ASM) {
111bfab3 7513 fprintf(logfile, "translate opcode %08x (%02x %02x %02x) (%s)\n",
9a64fbe4 7514 ctx.opcode, opc1(ctx.opcode), opc2(ctx.opcode),
056401ea 7515 opc3(ctx.opcode), little_endian ? "little" : "big");
79aceca5
FB
7516 }
7517#endif
046d6672 7518 ctx.nip += 4;
3fc6c082 7519 table = env->opcodes;
2e70f6ef 7520 num_insns++;
79aceca5
FB
7521 handler = table[opc1(ctx.opcode)];
7522 if (is_indirect_opcode(handler)) {
7523 table = ind_table(handler);
7524 handler = table[opc2(ctx.opcode)];
7525 if (is_indirect_opcode(handler)) {
7526 table = ind_table(handler);
7527 handler = table[opc3(ctx.opcode)];
7528 }
7529 }
7530 /* Is opcode *REALLY* valid ? */
76a66253 7531 if (unlikely(handler->handler == &gen_invalid)) {
4a057712 7532 if (loglevel != 0) {
76a66253 7533 fprintf(logfile, "invalid/unsupported opcode: "
6b542af7 7534 "%02x - %02x - %02x (%08x) " ADDRX " %d\n",
76a66253 7535 opc1(ctx.opcode), opc2(ctx.opcode),
0411a972 7536 opc3(ctx.opcode), ctx.opcode, ctx.nip - 4, (int)msr_ir);
4b3686fa
FB
7537 } else {
7538 printf("invalid/unsupported opcode: "
6b542af7 7539 "%02x - %02x - %02x (%08x) " ADDRX " %d\n",
4b3686fa 7540 opc1(ctx.opcode), opc2(ctx.opcode),
0411a972 7541 opc3(ctx.opcode), ctx.opcode, ctx.nip - 4, (int)msr_ir);
4b3686fa 7542 }
76a66253
JM
7543 } else {
7544 if (unlikely((ctx.opcode & handler->inval) != 0)) {
4a057712 7545 if (loglevel != 0) {
79aceca5 7546 fprintf(logfile, "invalid bits: %08x for opcode: "
6b542af7 7547 "%02x - %02x - %02x (%08x) " ADDRX "\n",
79aceca5
FB
7548 ctx.opcode & handler->inval, opc1(ctx.opcode),
7549 opc2(ctx.opcode), opc3(ctx.opcode),
046d6672 7550 ctx.opcode, ctx.nip - 4);
9a64fbe4
FB
7551 } else {
7552 printf("invalid bits: %08x for opcode: "
6b542af7 7553 "%02x - %02x - %02x (%08x) " ADDRX "\n",
76a66253
JM
7554 ctx.opcode & handler->inval, opc1(ctx.opcode),
7555 opc2(ctx.opcode), opc3(ctx.opcode),
046d6672 7556 ctx.opcode, ctx.nip - 4);
76a66253 7557 }
e1833e1f 7558 GEN_EXCP_INVAL(ctxp);
4b3686fa 7559 break;
79aceca5 7560 }
79aceca5 7561 }
4b3686fa 7562 (*(handler->handler))(&ctx);
76a66253
JM
7563#if defined(DO_PPC_STATISTICS)
7564 handler->count++;
7565#endif
9a64fbe4 7566 /* Check trace mode exceptions */
8cbcb4fa
AJ
7567 if (unlikely(ctx.singlestep_enabled & CPU_SINGLE_STEP &&
7568 (ctx.nip <= 0x100 || ctx.nip > 0xF00) &&
7569 ctx.exception != POWERPC_SYSCALL &&
7570 ctx.exception != POWERPC_EXCP_TRAP &&
7571 ctx.exception != POWERPC_EXCP_BRANCH)) {
e1833e1f 7572 GEN_EXCP(ctxp, POWERPC_EXCP_TRACE, 0);
d26bfc9a 7573 } else if (unlikely(((ctx.nip & (TARGET_PAGE_SIZE - 1)) == 0) ||
2e70f6ef
PB
7574 (env->singlestep_enabled) ||
7575 num_insns >= max_insns)) {
d26bfc9a
JM
7576 /* if we reach a page boundary or are single stepping, stop
7577 * generation
7578 */
8dd4983c 7579 break;
76a66253 7580 }
3fc6c082
FB
7581#if defined (DO_SINGLE_STEP)
7582 break;
7583#endif
7584 }
2e70f6ef
PB
7585 if (tb->cflags & CF_LAST_IO)
7586 gen_io_end();
e1833e1f 7587 if (ctx.exception == POWERPC_EXCP_NONE) {
c1942362 7588 gen_goto_tb(&ctx, 0, ctx.nip);
e1833e1f 7589 } else if (ctx.exception != POWERPC_EXCP_BRANCH) {
8cbcb4fa
AJ
7590 if (unlikely(env->singlestep_enabled)) {
7591 gen_update_nip(&ctx, ctx.nip);
64adab3f 7592 gen_helper_raise_debug();
8cbcb4fa 7593 }
76a66253 7594 /* Generate the return instruction */
57fec1fe 7595 tcg_gen_exit_tb(0);
9a64fbe4 7596 }
2e70f6ef 7597 gen_icount_end(tb, num_insns);
79aceca5 7598 *gen_opc_ptr = INDEX_op_end;
76a66253 7599 if (unlikely(search_pc)) {
9a64fbe4
FB
7600 j = gen_opc_ptr - gen_opc_buf;
7601 lj++;
7602 while (lj <= j)
7603 gen_opc_instr_start[lj++] = 0;
9a64fbe4 7604 } else {
046d6672 7605 tb->size = ctx.nip - pc_start;
2e70f6ef 7606 tb->icount = num_insns;
9a64fbe4 7607 }
d9bce9d9 7608#if defined(DEBUG_DISAS)
9fddaa0c 7609 if (loglevel & CPU_LOG_TB_CPU) {
9a64fbe4 7610 fprintf(logfile, "---------------- excp: %04x\n", ctx.exception);
7fe48483 7611 cpu_dump_state(env, logfile, fprintf, 0);
9fddaa0c
FB
7612 }
7613 if (loglevel & CPU_LOG_TB_IN_ASM) {
76a66253 7614 int flags;
237c0af0 7615 flags = env->bfd_mach;
056401ea 7616 flags |= little_endian << 16;
0fa85d43 7617 fprintf(logfile, "IN: %s\n", lookup_symbol(pc_start));
76a66253 7618 target_disas(logfile, pc_start, ctx.nip - pc_start, flags);
79aceca5 7619 fprintf(logfile, "\n");
9fddaa0c 7620 }
79aceca5 7621#endif
79aceca5
FB
7622}
7623
2cfc5f17 7624void gen_intermediate_code (CPUState *env, struct TranslationBlock *tb)
79aceca5 7625{
2cfc5f17 7626 gen_intermediate_code_internal(env, tb, 0);
79aceca5
FB
7627}
7628
2cfc5f17 7629void gen_intermediate_code_pc (CPUState *env, struct TranslationBlock *tb)
79aceca5 7630{
2cfc5f17 7631 gen_intermediate_code_internal(env, tb, 1);
79aceca5 7632}
d2856f1a
AJ
7633
7634void gen_pc_load(CPUState *env, TranslationBlock *tb,
7635 unsigned long searched_pc, int pc_pos, void *puc)
7636{
d2856f1a 7637 env->nip = gen_opc_pc[pc_pos];
d2856f1a 7638}