]> Git Repo - qemu.git/blame - target-ppc/translate.c
ppc: Make alignment exceptions suck less
[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
90dc8812 5 * Copyright (C) 2011 Freescale Semiconductor, Inc.
79aceca5
FB
6 *
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2 of the License, or (at your option) any later version.
11 *
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
8167ee88 18 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
79aceca5 19 */
c6a1c22b 20
0d75590d 21#include "qemu/osdep.h"
79aceca5 22#include "cpu.h"
76cad711 23#include "disas/disas.h"
63c91552 24#include "exec/exec-all.h"
57fec1fe 25#include "tcg-op.h"
1de7afc9 26#include "qemu/host-utils.h"
f08b6170 27#include "exec/cpu_ldst.h"
79aceca5 28
2ef6175a
RH
29#include "exec/helper-proto.h"
30#include "exec/helper-gen.h"
a7812ae4 31
a7e30d84 32#include "trace-tcg.h"
508127e2 33#include "exec/log.h"
a7e30d84
LV
34
35
8cbcb4fa
AJ
36#define CPU_SINGLE_STEP 0x1
37#define CPU_BRANCH_STEP 0x2
38#define GDBSTUB_SINGLE_STEP 0x4
39
a750fc0b 40/* Include definitions for instructions classes and implementations flags */
9fddaa0c 41//#define PPC_DEBUG_DISAS
76a66253 42//#define DO_PPC_STATISTICS
79aceca5 43
d12d51d5 44#ifdef PPC_DEBUG_DISAS
93fcfe39 45# define LOG_DISAS(...) qemu_log_mask(CPU_LOG_TB_IN_ASM, ## __VA_ARGS__)
d12d51d5
AL
46#else
47# define LOG_DISAS(...) do { } while (0)
48#endif
a750fc0b
JM
49/*****************************************************************************/
50/* Code translation helpers */
c53be334 51
f78fb44e 52/* global register indexes */
1bcea73e 53static TCGv_env cpu_env;
1d542695 54static char cpu_reg_names[10*3 + 22*4 /* GPR */
1d542695 55 + 10*4 + 22*5 /* SPE GPRh */
a5e26afa 56 + 10*4 + 22*5 /* FPR */
47e4661c 57 + 2*(10*6 + 22*7) /* AVRh, AVRl */
472b24ce 58 + 10*5 + 22*6 /* VSR */
47e4661c 59 + 8*5 /* CRF */];
f78fb44e 60static TCGv cpu_gpr[32];
f78fb44e 61static TCGv cpu_gprh[32];
a7812ae4
PB
62static TCGv_i64 cpu_fpr[32];
63static TCGv_i64 cpu_avrh[32], cpu_avrl[32];
472b24ce 64static TCGv_i64 cpu_vsr[32];
a7812ae4 65static TCGv_i32 cpu_crf[8];
bd568f18 66static TCGv cpu_nip;
6527f6ea 67static TCGv cpu_msr;
cfdcd37a
AJ
68static TCGv cpu_ctr;
69static TCGv cpu_lr;
697ab892
DG
70#if defined(TARGET_PPC64)
71static TCGv cpu_cfar;
72#endif
da91a00f 73static TCGv cpu_xer, cpu_so, cpu_ov, cpu_ca;
cf360a32 74static TCGv cpu_reserve;
30304420 75static TCGv cpu_fpscr;
a7859e89 76static TCGv_i32 cpu_access_type;
f78fb44e 77
022c62cb 78#include "exec/gen-icount.h"
2e70f6ef
PB
79
80void ppc_translate_init(void)
81{
f78fb44e
AJ
82 int i;
83 char* p;
2dc766da 84 size_t cpu_reg_names_size;
b2437bf2 85 static int done_init = 0;
f78fb44e 86
2e70f6ef
PB
87 if (done_init)
88 return;
f78fb44e 89
a7812ae4 90 cpu_env = tcg_global_reg_new_ptr(TCG_AREG0, "env");
7c255043 91 tcg_ctx.tcg_env = cpu_env;
a7812ae4 92
f78fb44e 93 p = cpu_reg_names;
2dc766da 94 cpu_reg_names_size = sizeof(cpu_reg_names);
47e4661c
AJ
95
96 for (i = 0; i < 8; i++) {
2dc766da 97 snprintf(p, cpu_reg_names_size, "crf%d", i);
e1ccc054 98 cpu_crf[i] = tcg_global_mem_new_i32(cpu_env,
1328c2bf 99 offsetof(CPUPPCState, crf[i]), p);
47e4661c 100 p += 5;
2dc766da 101 cpu_reg_names_size -= 5;
47e4661c
AJ
102 }
103
f78fb44e 104 for (i = 0; i < 32; i++) {
2dc766da 105 snprintf(p, cpu_reg_names_size, "r%d", i);
e1ccc054 106 cpu_gpr[i] = tcg_global_mem_new(cpu_env,
1328c2bf 107 offsetof(CPUPPCState, gpr[i]), p);
f78fb44e 108 p += (i < 10) ? 3 : 4;
2dc766da 109 cpu_reg_names_size -= (i < 10) ? 3 : 4;
2dc766da 110 snprintf(p, cpu_reg_names_size, "r%dH", i);
e1ccc054 111 cpu_gprh[i] = tcg_global_mem_new(cpu_env,
13b6a455 112 offsetof(CPUPPCState, gprh[i]), p);
f78fb44e 113 p += (i < 10) ? 4 : 5;
2dc766da 114 cpu_reg_names_size -= (i < 10) ? 4 : 5;
1d542695 115
2dc766da 116 snprintf(p, cpu_reg_names_size, "fp%d", i);
e1ccc054 117 cpu_fpr[i] = tcg_global_mem_new_i64(cpu_env,
1328c2bf 118 offsetof(CPUPPCState, fpr[i]), p);
ec1ac72d 119 p += (i < 10) ? 4 : 5;
2dc766da 120 cpu_reg_names_size -= (i < 10) ? 4 : 5;
a5e26afa 121
2dc766da 122 snprintf(p, cpu_reg_names_size, "avr%dH", i);
e2542fe2 123#ifdef HOST_WORDS_BIGENDIAN
e1ccc054 124 cpu_avrh[i] = tcg_global_mem_new_i64(cpu_env,
1328c2bf 125 offsetof(CPUPPCState, avr[i].u64[0]), p);
fe1e5c53 126#else
e1ccc054 127 cpu_avrh[i] = tcg_global_mem_new_i64(cpu_env,
1328c2bf 128 offsetof(CPUPPCState, avr[i].u64[1]), p);
fe1e5c53 129#endif
1d542695 130 p += (i < 10) ? 6 : 7;
2dc766da 131 cpu_reg_names_size -= (i < 10) ? 6 : 7;
ec1ac72d 132
2dc766da 133 snprintf(p, cpu_reg_names_size, "avr%dL", i);
e2542fe2 134#ifdef HOST_WORDS_BIGENDIAN
e1ccc054 135 cpu_avrl[i] = tcg_global_mem_new_i64(cpu_env,
1328c2bf 136 offsetof(CPUPPCState, avr[i].u64[1]), p);
fe1e5c53 137#else
e1ccc054 138 cpu_avrl[i] = tcg_global_mem_new_i64(cpu_env,
1328c2bf 139 offsetof(CPUPPCState, avr[i].u64[0]), p);
fe1e5c53 140#endif
1d542695 141 p += (i < 10) ? 6 : 7;
2dc766da 142 cpu_reg_names_size -= (i < 10) ? 6 : 7;
472b24ce 143 snprintf(p, cpu_reg_names_size, "vsr%d", i);
e1ccc054
RH
144 cpu_vsr[i] = tcg_global_mem_new_i64(cpu_env,
145 offsetof(CPUPPCState, vsr[i]), p);
472b24ce
TM
146 p += (i < 10) ? 5 : 6;
147 cpu_reg_names_size -= (i < 10) ? 5 : 6;
f78fb44e 148 }
f10dc08e 149
e1ccc054 150 cpu_nip = tcg_global_mem_new(cpu_env,
1328c2bf 151 offsetof(CPUPPCState, nip), "nip");
bd568f18 152
e1ccc054 153 cpu_msr = tcg_global_mem_new(cpu_env,
1328c2bf 154 offsetof(CPUPPCState, msr), "msr");
6527f6ea 155
e1ccc054 156 cpu_ctr = tcg_global_mem_new(cpu_env,
1328c2bf 157 offsetof(CPUPPCState, ctr), "ctr");
cfdcd37a 158
e1ccc054 159 cpu_lr = tcg_global_mem_new(cpu_env,
1328c2bf 160 offsetof(CPUPPCState, lr), "lr");
cfdcd37a 161
697ab892 162#if defined(TARGET_PPC64)
e1ccc054 163 cpu_cfar = tcg_global_mem_new(cpu_env,
1328c2bf 164 offsetof(CPUPPCState, cfar), "cfar");
697ab892
DG
165#endif
166
e1ccc054 167 cpu_xer = tcg_global_mem_new(cpu_env,
1328c2bf 168 offsetof(CPUPPCState, xer), "xer");
e1ccc054 169 cpu_so = tcg_global_mem_new(cpu_env,
da91a00f 170 offsetof(CPUPPCState, so), "SO");
e1ccc054 171 cpu_ov = tcg_global_mem_new(cpu_env,
da91a00f 172 offsetof(CPUPPCState, ov), "OV");
e1ccc054 173 cpu_ca = tcg_global_mem_new(cpu_env,
da91a00f 174 offsetof(CPUPPCState, ca), "CA");
3d7b417e 175
e1ccc054 176 cpu_reserve = tcg_global_mem_new(cpu_env,
1328c2bf 177 offsetof(CPUPPCState, reserve_addr),
18b21a2f 178 "reserve_addr");
cf360a32 179
e1ccc054 180 cpu_fpscr = tcg_global_mem_new(cpu_env,
30304420 181 offsetof(CPUPPCState, fpscr), "fpscr");
e1571908 182
e1ccc054 183 cpu_access_type = tcg_global_mem_new_i32(cpu_env,
1328c2bf 184 offsetof(CPUPPCState, access_type), "access_type");
a7859e89 185
2e70f6ef
PB
186 done_init = 1;
187}
188
79aceca5 189/* internal defines */
69b058c8 190struct DisasContext {
79aceca5 191 struct TranslationBlock *tb;
0fa85d43 192 target_ulong nip;
79aceca5 193 uint32_t opcode;
9a64fbe4 194 uint32_t exception;
3cc62370 195 /* Routine used to access memory */
5c3ae929 196 bool pr, hv, dr, le_mode;
c5a8d8f3 197 bool lazy_tlb_flush;
3cc62370 198 int mem_idx;
76db3ba4 199 int access_type;
3cc62370 200 /* Translation flags */
e22c357b 201 TCGMemOp default_tcg_memop_mask;
d9bce9d9 202#if defined(TARGET_PPC64)
5c3ae929
BH
203 bool sf_mode;
204 bool has_cfar;
9a64fbe4 205#endif
5c3ae929
BH
206 bool fpu_enabled;
207 bool altivec_enabled;
208 bool vsx_enabled;
209 bool spe_enabled;
210 bool tm_enabled;
c227f099 211 ppc_spr_t *spr_cb; /* Needed to check rights for mfspr/mtspr */
ea4e754f 212 int singlestep_enabled;
7d08d856
AJ
213 uint64_t insns_flags;
214 uint64_t insns_flags2;
69b058c8 215};
79aceca5 216
e22c357b
DK
217/* Return true iff byteswap is needed in a scalar memop */
218static inline bool need_byteswap(const DisasContext *ctx)
219{
220#if defined(TARGET_WORDS_BIGENDIAN)
221 return ctx->le_mode;
222#else
223 return !ctx->le_mode;
224#endif
225}
226
79482e5a
RH
227/* True when active word size < size of target_long. */
228#ifdef TARGET_PPC64
229# define NARROW_MODE(C) (!(C)->sf_mode)
230#else
231# define NARROW_MODE(C) 0
232#endif
233
c227f099 234struct opc_handler_t {
70560da7
FC
235 /* invalid bits for instruction 1 (Rc(opcode) == 0) */
236 uint32_t inval1;
237 /* invalid bits for instruction 2 (Rc(opcode) == 1) */
238 uint32_t inval2;
9a64fbe4 239 /* instruction type */
0487d6a8 240 uint64_t type;
a5858d7a
AG
241 /* extended instruction type */
242 uint64_t type2;
79aceca5
FB
243 /* handler */
244 void (*handler)(DisasContext *ctx);
a750fc0b 245#if defined(DO_PPC_STATISTICS) || defined(PPC_DUMP_CPU)
b55266b5 246 const char *oname;
a750fc0b
JM
247#endif
248#if defined(DO_PPC_STATISTICS)
76a66253
JM
249 uint64_t count;
250#endif
3fc6c082 251};
79aceca5 252
636aa200 253static inline void gen_set_access_type(DisasContext *ctx, int access_type)
a7859e89 254{
76db3ba4
AJ
255 if (ctx->access_type != access_type) {
256 tcg_gen_movi_i32(cpu_access_type, access_type);
257 ctx->access_type = access_type;
258 }
a7859e89
AJ
259}
260
636aa200 261static inline void gen_update_nip(DisasContext *ctx, target_ulong nip)
d9bce9d9 262{
e0c8f9ce
RH
263 if (NARROW_MODE(ctx)) {
264 nip = (uint32_t)nip;
265 }
266 tcg_gen_movi_tl(cpu_nip, nip);
d9bce9d9
JM
267}
268
b9971cc5 269static void gen_exception_err(DisasContext *ctx, uint32_t excp, uint32_t error)
e06fcd75
AJ
270{
271 TCGv_i32 t0, t1;
bd6fefe7
BH
272
273 /* These are all synchronous exceptions, we set the PC back to
274 * the faulting instruction
275 */
e06fcd75 276 if (ctx->exception == POWERPC_EXCP_NONE) {
bd6fefe7 277 gen_update_nip(ctx, ctx->nip - 4);
e06fcd75
AJ
278 }
279 t0 = tcg_const_i32(excp);
280 t1 = tcg_const_i32(error);
e5f17ac6 281 gen_helper_raise_exception_err(cpu_env, t0, t1);
e06fcd75
AJ
282 tcg_temp_free_i32(t0);
283 tcg_temp_free_i32(t1);
284 ctx->exception = (excp);
285}
e1833e1f 286
b9971cc5 287static void gen_exception(DisasContext *ctx, uint32_t excp)
e06fcd75
AJ
288{
289 TCGv_i32 t0;
bd6fefe7
BH
290
291 /* These are all synchronous exceptions, we set the PC back to
292 * the faulting instruction
293 */
e06fcd75 294 if (ctx->exception == POWERPC_EXCP_NONE) {
bd6fefe7 295 gen_update_nip(ctx, ctx->nip - 4);
e06fcd75
AJ
296 }
297 t0 = tcg_const_i32(excp);
e5f17ac6 298 gen_helper_raise_exception(cpu_env, t0);
e06fcd75
AJ
299 tcg_temp_free_i32(t0);
300 ctx->exception = (excp);
301}
e1833e1f 302
bd6fefe7
BH
303static void gen_exception_nip(DisasContext *ctx, uint32_t excp,
304 target_ulong nip)
305{
306 TCGv_i32 t0;
307
308 gen_update_nip(ctx, nip);
309 t0 = tcg_const_i32(excp);
310 gen_helper_raise_exception(cpu_env, t0);
311 tcg_temp_free_i32(t0);
312 ctx->exception = (excp);
313}
314
b9971cc5 315static void gen_debug_exception(DisasContext *ctx)
e06fcd75
AJ
316{
317 TCGv_i32 t0;
5518f3a6 318
bd6fefe7
BH
319 /* These are all synchronous exceptions, we set the PC back to
320 * the faulting instruction
321 */
ee2b3994
SB
322 if ((ctx->exception != POWERPC_EXCP_BRANCH) &&
323 (ctx->exception != POWERPC_EXCP_SYNC)) {
bd6fefe7 324 gen_update_nip(ctx, ctx->nip - 4);
ee2b3994 325 }
e06fcd75 326 t0 = tcg_const_i32(EXCP_DEBUG);
e5f17ac6 327 gen_helper_raise_exception(cpu_env, t0);
e06fcd75
AJ
328 tcg_temp_free_i32(t0);
329}
9a64fbe4 330
636aa200 331static inline void gen_inval_exception(DisasContext *ctx, uint32_t error)
e06fcd75 332{
9b2fadda
BH
333 /* Will be converted to program check if needed */
334 gen_exception_err(ctx, POWERPC_EXCP_HV_EMU, POWERPC_EXCP_INVAL | error);
335}
336
337static inline void gen_priv_exception(DisasContext *ctx, uint32_t error)
338{
339 gen_exception_err(ctx, POWERPC_EXCP_PROGRAM, POWERPC_EXCP_PRIV | error);
340}
341
342static inline void gen_hvpriv_exception(DisasContext *ctx, uint32_t error)
343{
344 /* Will be converted to program check if needed */
345 gen_exception_err(ctx, POWERPC_EXCP_HV_EMU, POWERPC_EXCP_PRIV | error);
e06fcd75 346}
a9d9eb8f 347
f24e5695 348/* Stop translation */
636aa200 349static inline void gen_stop_exception(DisasContext *ctx)
3fc6c082 350{
d9bce9d9 351 gen_update_nip(ctx, ctx->nip);
e1833e1f 352 ctx->exception = POWERPC_EXCP_STOP;
3fc6c082
FB
353}
354
466976d9 355#ifndef CONFIG_USER_ONLY
f24e5695 356/* No need to update nip here, as execution flow will change */
636aa200 357static inline void gen_sync_exception(DisasContext *ctx)
2be0071f 358{
e1833e1f 359 ctx->exception = POWERPC_EXCP_SYNC;
2be0071f 360}
466976d9 361#endif
2be0071f 362
79aceca5 363#define GEN_HANDLER(name, opc1, opc2, opc3, inval, type) \
a5858d7a
AG
364GEN_OPCODE(name, opc1, opc2, opc3, inval, type, PPC_NONE)
365
366#define GEN_HANDLER_E(name, opc1, opc2, opc3, inval, type, type2) \
367GEN_OPCODE(name, opc1, opc2, opc3, inval, type, type2)
79aceca5 368
c7697e1f 369#define GEN_HANDLER2(name, onam, opc1, opc2, opc3, inval, type) \
a5858d7a
AG
370GEN_OPCODE2(name, onam, opc1, opc2, opc3, inval, type, PPC_NONE)
371
372#define GEN_HANDLER2_E(name, onam, opc1, opc2, opc3, inval, type, type2) \
373GEN_OPCODE2(name, onam, opc1, opc2, opc3, inval, type, type2)
c7697e1f 374
323ad19b
ND
375#define GEN_HANDLER_E_2(name, opc1, opc2, opc3, opc4, inval, type, type2) \
376GEN_OPCODE3(name, opc1, opc2, opc3, opc4, inval, type, type2)
377
c227f099 378typedef struct opcode_t {
323ad19b 379 unsigned char opc1, opc2, opc3, opc4;
1235fc06 380#if HOST_LONG_BITS == 64 /* Explicitly align to 64 bits */
323ad19b 381 unsigned char pad[4];
18fba28c 382#endif
c227f099 383 opc_handler_t handler;
b55266b5 384 const char *oname;
c227f099 385} opcode_t;
79aceca5 386
9b2fadda
BH
387/* Helpers for priv. check */
388#define GEN_PRIV \
389 do { \
390 gen_priv_exception(ctx, POWERPC_EXCP_PRIV_OPC); return; \
391 } while (0)
392
393#if defined(CONFIG_USER_ONLY)
394#define CHK_HV GEN_PRIV
395#define CHK_SV GEN_PRIV
b7815375 396#define CHK_HVRM GEN_PRIV
9b2fadda
BH
397#else
398#define CHK_HV \
399 do { \
400 if (unlikely(ctx->pr || !ctx->hv)) { \
401 GEN_PRIV; \
402 } \
403 } while (0)
404#define CHK_SV \
405 do { \
406 if (unlikely(ctx->pr)) { \
407 GEN_PRIV; \
408 } \
409 } while (0)
b7815375
BH
410#define CHK_HVRM \
411 do { \
412 if (unlikely(ctx->pr || !ctx->hv || ctx->dr)) { \
413 GEN_PRIV; \
414 } \
415 } while (0)
9b2fadda
BH
416#endif
417
418#define CHK_NONE
419
420
a750fc0b 421/*****************************************************************************/
79aceca5
FB
422/*** Instruction decoding ***/
423#define EXTRACT_HELPER(name, shift, nb) \
636aa200 424static inline uint32_t name(uint32_t opcode) \
79aceca5
FB
425{ \
426 return (opcode >> (shift)) & ((1 << (nb)) - 1); \
427}
428
429#define EXTRACT_SHELPER(name, shift, nb) \
636aa200 430static inline int32_t name(uint32_t opcode) \
79aceca5 431{ \
18fba28c 432 return (int16_t)((opcode >> (shift)) & ((1 << (nb)) - 1)); \
79aceca5
FB
433}
434
f9fc6d81
TM
435#define EXTRACT_HELPER_SPLIT(name, shift1, nb1, shift2, nb2) \
436static inline uint32_t name(uint32_t opcode) \
437{ \
438 return (((opcode >> (shift1)) & ((1 << (nb1)) - 1)) << nb2) | \
439 ((opcode >> (shift2)) & ((1 << (nb2)) - 1)); \
440}
c5b2b9ce
ND
441
442#define EXTRACT_HELPER_DXFORM(name, \
443 d0_bits, shift_op_d0, shift_d0, \
444 d1_bits, shift_op_d1, shift_d1, \
445 d2_bits, shift_op_d2, shift_d2) \
446static inline int16_t name(uint32_t opcode) \
447{ \
448 return \
449 (((opcode >> (shift_op_d0)) & ((1 << (d0_bits)) - 1)) << (shift_d0)) | \
450 (((opcode >> (shift_op_d1)) & ((1 << (d1_bits)) - 1)) << (shift_d1)) | \
451 (((opcode >> (shift_op_d2)) & ((1 << (d2_bits)) - 1)) << (shift_d2)); \
452}
453
454
79aceca5
FB
455/* Opcode part 1 */
456EXTRACT_HELPER(opc1, 26, 6);
457/* Opcode part 2 */
458EXTRACT_HELPER(opc2, 1, 5);
459/* Opcode part 3 */
460EXTRACT_HELPER(opc3, 6, 5);
323ad19b
ND
461/* Opcode part 4 */
462EXTRACT_HELPER(opc4, 16, 5);
79aceca5
FB
463/* Update Cr0 flags */
464EXTRACT_HELPER(Rc, 0, 1);
a737d3eb
TM
465/* Update Cr6 flags (Altivec) */
466EXTRACT_HELPER(Rc21, 10, 1);
79aceca5
FB
467/* Destination */
468EXTRACT_HELPER(rD, 21, 5);
469/* Source */
470EXTRACT_HELPER(rS, 21, 5);
471/* First operand */
472EXTRACT_HELPER(rA, 16, 5);
473/* Second operand */
474EXTRACT_HELPER(rB, 11, 5);
475/* Third operand */
476EXTRACT_HELPER(rC, 6, 5);
477/*** Get CRn ***/
478EXTRACT_HELPER(crfD, 23, 3);
479EXTRACT_HELPER(crfS, 18, 3);
480EXTRACT_HELPER(crbD, 21, 5);
481EXTRACT_HELPER(crbA, 16, 5);
482EXTRACT_HELPER(crbB, 11, 5);
483/* SPR / TBL */
3fc6c082 484EXTRACT_HELPER(_SPR, 11, 10);
636aa200 485static inline uint32_t SPR(uint32_t opcode)
3fc6c082
FB
486{
487 uint32_t sprn = _SPR(opcode);
488
489 return ((sprn >> 5) & 0x1F) | ((sprn & 0x1F) << 5);
490}
79aceca5 491/*** Get constants ***/
79aceca5
FB
492/* 16 bits signed immediate value */
493EXTRACT_SHELPER(SIMM, 0, 16);
494/* 16 bits unsigned immediate value */
495EXTRACT_HELPER(UIMM, 0, 16);
21d21583
AJ
496/* 5 bits signed immediate value */
497EXTRACT_HELPER(SIMM5, 16, 5);
27a4edb3
AJ
498/* 5 bits signed immediate value */
499EXTRACT_HELPER(UIMM5, 16, 5);
79aceca5
FB
500/* Bit count */
501EXTRACT_HELPER(NB, 11, 5);
502/* Shift count */
503EXTRACT_HELPER(SH, 11, 5);
cd633b10
AJ
504/* Vector shift count */
505EXTRACT_HELPER(VSH, 6, 4);
79aceca5
FB
506/* Mask start */
507EXTRACT_HELPER(MB, 6, 5);
508/* Mask end */
509EXTRACT_HELPER(ME, 1, 5);
fb0eaffc
FB
510/* Trap operand */
511EXTRACT_HELPER(TO, 21, 5);
79aceca5
FB
512
513EXTRACT_HELPER(CRM, 12, 8);
466976d9
PM
514
515#ifndef CONFIG_USER_ONLY
79aceca5 516EXTRACT_HELPER(SR, 16, 4);
466976d9 517#endif
7d08d856
AJ
518
519/* mtfsf/mtfsfi */
779f6590 520EXTRACT_HELPER(FPBF, 23, 3);
e4bb997e 521EXTRACT_HELPER(FPIMM, 12, 4);
779f6590 522EXTRACT_HELPER(FPL, 25, 1);
7d08d856
AJ
523EXTRACT_HELPER(FPFLM, 17, 8);
524EXTRACT_HELPER(FPW, 16, 1);
fb0eaffc 525
c5b2b9ce
ND
526/* addpcis */
527EXTRACT_HELPER_DXFORM(DX, 10, 6, 6, 5, 16, 1, 1, 0, 0)
528
79aceca5 529/*** Jump target decoding ***/
79aceca5 530/* Immediate address */
636aa200 531static inline target_ulong LI(uint32_t opcode)
79aceca5
FB
532{
533 return (opcode >> 0) & 0x03FFFFFC;
534}
535
636aa200 536static inline uint32_t BD(uint32_t opcode)
79aceca5
FB
537{
538 return (opcode >> 0) & 0xFFFC;
539}
540
541EXTRACT_HELPER(BO, 21, 5);
542EXTRACT_HELPER(BI, 16, 5);
543/* Absolute/relative address */
544EXTRACT_HELPER(AA, 1, 1);
545/* Link */
546EXTRACT_HELPER(LK, 0, 1);
547
f0b01f02
TM
548/* DFP Z22-form */
549EXTRACT_HELPER(DCM, 10, 6)
550
551/* DFP Z23-form */
552EXTRACT_HELPER(RMC, 9, 2)
553
79aceca5 554/* Create a mask between <start> and <end> bits */
636aa200 555static inline target_ulong MASK(uint32_t start, uint32_t end)
79aceca5 556{
76a66253 557 target_ulong ret;
79aceca5 558
76a66253
JM
559#if defined(TARGET_PPC64)
560 if (likely(start == 0)) {
6f2d8978 561 ret = UINT64_MAX << (63 - end);
76a66253 562 } else if (likely(end == 63)) {
6f2d8978 563 ret = UINT64_MAX >> start;
76a66253
JM
564 }
565#else
566 if (likely(start == 0)) {
6f2d8978 567 ret = UINT32_MAX << (31 - end);
76a66253 568 } else if (likely(end == 31)) {
6f2d8978 569 ret = UINT32_MAX >> start;
76a66253
JM
570 }
571#endif
572 else {
573 ret = (((target_ulong)(-1ULL)) >> (start)) ^
574 (((target_ulong)(-1ULL) >> (end)) >> 1);
575 if (unlikely(start > end))
576 return ~ret;
577 }
79aceca5
FB
578
579 return ret;
580}
581
f9fc6d81
TM
582EXTRACT_HELPER_SPLIT(xT, 0, 1, 21, 5);
583EXTRACT_HELPER_SPLIT(xS, 0, 1, 21, 5);
584EXTRACT_HELPER_SPLIT(xA, 2, 1, 16, 5);
585EXTRACT_HELPER_SPLIT(xB, 1, 1, 11, 5);
551e3ef7 586EXTRACT_HELPER_SPLIT(xC, 3, 1, 6, 5);
f9fc6d81 587EXTRACT_HELPER(DM, 8, 2);
76c15fe0 588EXTRACT_HELPER(UIM, 16, 2);
acc42968 589EXTRACT_HELPER(SHW, 8, 2);
f0b01f02 590EXTRACT_HELPER(SP, 19, 2);
a750fc0b 591/*****************************************************************************/
a750fc0b 592/* PowerPC instructions table */
933dc6eb 593
76a66253 594#if defined(DO_PPC_STATISTICS)
a5858d7a 595#define GEN_OPCODE(name, op1, op2, op3, invl, _typ, _typ2) \
5c55ff99 596{ \
79aceca5
FB
597 .opc1 = op1, \
598 .opc2 = op2, \
599 .opc3 = op3, \
323ad19b 600 .opc4 = 0xff, \
79aceca5 601 .handler = { \
70560da7
FC
602 .inval1 = invl, \
603 .type = _typ, \
604 .type2 = _typ2, \
605 .handler = &gen_##name, \
606 .oname = stringify(name), \
607 }, \
608 .oname = stringify(name), \
609}
610#define GEN_OPCODE_DUAL(name, op1, op2, op3, invl1, invl2, _typ, _typ2) \
611{ \
612 .opc1 = op1, \
613 .opc2 = op2, \
614 .opc3 = op3, \
323ad19b 615 .opc4 = 0xff, \
70560da7
FC
616 .handler = { \
617 .inval1 = invl1, \
618 .inval2 = invl2, \
9a64fbe4 619 .type = _typ, \
a5858d7a 620 .type2 = _typ2, \
79aceca5 621 .handler = &gen_##name, \
76a66253 622 .oname = stringify(name), \
79aceca5 623 }, \
3fc6c082 624 .oname = stringify(name), \
79aceca5 625}
a5858d7a 626#define GEN_OPCODE2(name, onam, op1, op2, op3, invl, _typ, _typ2) \
5c55ff99 627{ \
c7697e1f
JM
628 .opc1 = op1, \
629 .opc2 = op2, \
630 .opc3 = op3, \
323ad19b 631 .opc4 = 0xff, \
c7697e1f 632 .handler = { \
70560da7 633 .inval1 = invl, \
c7697e1f 634 .type = _typ, \
a5858d7a 635 .type2 = _typ2, \
c7697e1f
JM
636 .handler = &gen_##name, \
637 .oname = onam, \
638 }, \
639 .oname = onam, \
640}
323ad19b
ND
641#define GEN_OPCODE3(name, op1, op2, op3, op4, invl, _typ, _typ2) \
642{ \
643 .opc1 = op1, \
644 .opc2 = op2, \
645 .opc3 = op3, \
646 .opc4 = op4, \
647 .handler = { \
648 .inval1 = invl, \
649 .type = _typ, \
650 .type2 = _typ2, \
651 .handler = &gen_##name, \
652 .oname = stringify(name), \
653 }, \
654 .oname = stringify(name), \
655}
76a66253 656#else
a5858d7a 657#define GEN_OPCODE(name, op1, op2, op3, invl, _typ, _typ2) \
5c55ff99 658{ \
c7697e1f
JM
659 .opc1 = op1, \
660 .opc2 = op2, \
661 .opc3 = op3, \
323ad19b 662 .opc4 = 0xff, \
c7697e1f 663 .handler = { \
70560da7
FC
664 .inval1 = invl, \
665 .type = _typ, \
666 .type2 = _typ2, \
667 .handler = &gen_##name, \
668 }, \
669 .oname = stringify(name), \
670}
671#define GEN_OPCODE_DUAL(name, op1, op2, op3, invl1, invl2, _typ, _typ2) \
672{ \
673 .opc1 = op1, \
674 .opc2 = op2, \
675 .opc3 = op3, \
323ad19b 676 .opc4 = 0xff, \
70560da7
FC
677 .handler = { \
678 .inval1 = invl1, \
679 .inval2 = invl2, \
c7697e1f 680 .type = _typ, \
a5858d7a 681 .type2 = _typ2, \
c7697e1f 682 .handler = &gen_##name, \
5c55ff99
BS
683 }, \
684 .oname = stringify(name), \
685}
a5858d7a 686#define GEN_OPCODE2(name, onam, op1, op2, op3, invl, _typ, _typ2) \
5c55ff99
BS
687{ \
688 .opc1 = op1, \
689 .opc2 = op2, \
690 .opc3 = op3, \
323ad19b 691 .opc4 = 0xff, \
5c55ff99 692 .handler = { \
70560da7 693 .inval1 = invl, \
5c55ff99 694 .type = _typ, \
a5858d7a 695 .type2 = _typ2, \
5c55ff99
BS
696 .handler = &gen_##name, \
697 }, \
698 .oname = onam, \
699}
323ad19b
ND
700#define GEN_OPCODE3(name, op1, op2, op3, op4, invl, _typ, _typ2) \
701{ \
702 .opc1 = op1, \
703 .opc2 = op2, \
704 .opc3 = op3, \
705 .opc4 = op4, \
706 .handler = { \
707 .inval1 = invl, \
708 .type = _typ, \
709 .type2 = _typ2, \
710 .handler = &gen_##name, \
711 }, \
712 .oname = stringify(name), \
713}
5c55ff99 714#endif
2e610050 715
5c55ff99 716/* SPR load/store helpers */
636aa200 717static inline void gen_load_spr(TCGv t, int reg)
5c55ff99 718{
1328c2bf 719 tcg_gen_ld_tl(t, cpu_env, offsetof(CPUPPCState, spr[reg]));
5c55ff99 720}
2e610050 721
636aa200 722static inline void gen_store_spr(int reg, TCGv t)
5c55ff99 723{
1328c2bf 724 tcg_gen_st_tl(t, cpu_env, offsetof(CPUPPCState, spr[reg]));
5c55ff99 725}
2e610050 726
54623277 727/* Invalid instruction */
99e300ef 728static void gen_invalid(DisasContext *ctx)
9a64fbe4 729{
e06fcd75 730 gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL);
9a64fbe4
FB
731}
732
c227f099 733static opc_handler_t invalid_handler = {
70560da7
FC
734 .inval1 = 0xFFFFFFFF,
735 .inval2 = 0xFFFFFFFF,
9a64fbe4 736 .type = PPC_NONE,
a5858d7a 737 .type2 = PPC_NONE,
79aceca5
FB
738 .handler = gen_invalid,
739};
740
e1571908
AJ
741/*** Integer comparison ***/
742
636aa200 743static inline void gen_op_cmp(TCGv arg0, TCGv arg1, int s, int crf)
e1571908 744{
2fdcb629
RH
745 TCGv t0 = tcg_temp_new();
746 TCGv_i32 t1 = tcg_temp_new_i32();
e1571908 747
da91a00f 748 tcg_gen_trunc_tl_i32(cpu_crf[crf], cpu_so);
e1571908 749
2fdcb629
RH
750 tcg_gen_setcond_tl((s ? TCG_COND_LT: TCG_COND_LTU), t0, arg0, arg1);
751 tcg_gen_trunc_tl_i32(t1, t0);
752 tcg_gen_shli_i32(t1, t1, CRF_LT);
753 tcg_gen_or_i32(cpu_crf[crf], cpu_crf[crf], t1);
754
755 tcg_gen_setcond_tl((s ? TCG_COND_GT: TCG_COND_GTU), t0, arg0, arg1);
756 tcg_gen_trunc_tl_i32(t1, t0);
757 tcg_gen_shli_i32(t1, t1, CRF_GT);
758 tcg_gen_or_i32(cpu_crf[crf], cpu_crf[crf], t1);
759
760 tcg_gen_setcond_tl(TCG_COND_EQ, t0, arg0, arg1);
761 tcg_gen_trunc_tl_i32(t1, t0);
762 tcg_gen_shli_i32(t1, t1, CRF_EQ);
763 tcg_gen_or_i32(cpu_crf[crf], cpu_crf[crf], t1);
764
765 tcg_temp_free(t0);
766 tcg_temp_free_i32(t1);
e1571908
AJ
767}
768
636aa200 769static inline void gen_op_cmpi(TCGv arg0, target_ulong arg1, int s, int crf)
e1571908 770{
2fdcb629 771 TCGv t0 = tcg_const_tl(arg1);
ea363694
AJ
772 gen_op_cmp(arg0, t0, s, crf);
773 tcg_temp_free(t0);
e1571908
AJ
774}
775
636aa200 776static inline void gen_op_cmp32(TCGv arg0, TCGv arg1, int s, int crf)
e1571908 777{
ea363694 778 TCGv t0, t1;
2fdcb629
RH
779 t0 = tcg_temp_new();
780 t1 = tcg_temp_new();
e1571908 781 if (s) {
ea363694
AJ
782 tcg_gen_ext32s_tl(t0, arg0);
783 tcg_gen_ext32s_tl(t1, arg1);
e1571908 784 } else {
ea363694
AJ
785 tcg_gen_ext32u_tl(t0, arg0);
786 tcg_gen_ext32u_tl(t1, arg1);
e1571908 787 }
ea363694
AJ
788 gen_op_cmp(t0, t1, s, crf);
789 tcg_temp_free(t1);
790 tcg_temp_free(t0);
e1571908
AJ
791}
792
636aa200 793static inline void gen_op_cmpi32(TCGv arg0, target_ulong arg1, int s, int crf)
e1571908 794{
2fdcb629 795 TCGv t0 = tcg_const_tl(arg1);
ea363694
AJ
796 gen_op_cmp32(arg0, t0, s, crf);
797 tcg_temp_free(t0);
e1571908 798}
e1571908 799
636aa200 800static inline void gen_set_Rc0(DisasContext *ctx, TCGv reg)
e1571908 801{
02765534 802 if (NARROW_MODE(ctx)) {
e1571908 803 gen_op_cmpi32(reg, 0, 1, 0);
02765534 804 } else {
e1571908 805 gen_op_cmpi(reg, 0, 1, 0);
02765534 806 }
e1571908
AJ
807}
808
809/* cmp */
99e300ef 810static void gen_cmp(DisasContext *ctx)
e1571908 811{
36f48d9c 812 if ((ctx->opcode & 0x00200000) && (ctx->insns_flags & PPC_64B)) {
e1571908
AJ
813 gen_op_cmp(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rB(ctx->opcode)],
814 1, crfD(ctx->opcode));
36f48d9c
AG
815 } else {
816 gen_op_cmp32(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rB(ctx->opcode)],
817 1, crfD(ctx->opcode));
02765534 818 }
e1571908
AJ
819}
820
821/* cmpi */
99e300ef 822static void gen_cmpi(DisasContext *ctx)
e1571908 823{
36f48d9c 824 if ((ctx->opcode & 0x00200000) && (ctx->insns_flags & PPC_64B)) {
e1571908
AJ
825 gen_op_cmpi(cpu_gpr[rA(ctx->opcode)], SIMM(ctx->opcode),
826 1, crfD(ctx->opcode));
36f48d9c
AG
827 } else {
828 gen_op_cmpi32(cpu_gpr[rA(ctx->opcode)], SIMM(ctx->opcode),
829 1, crfD(ctx->opcode));
02765534 830 }
e1571908
AJ
831}
832
833/* cmpl */
99e300ef 834static void gen_cmpl(DisasContext *ctx)
e1571908 835{
36f48d9c 836 if ((ctx->opcode & 0x00200000) && (ctx->insns_flags & PPC_64B)) {
e1571908
AJ
837 gen_op_cmp(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rB(ctx->opcode)],
838 0, crfD(ctx->opcode));
36f48d9c
AG
839 } else {
840 gen_op_cmp32(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rB(ctx->opcode)],
841 0, crfD(ctx->opcode));
02765534 842 }
e1571908
AJ
843}
844
845/* cmpli */
99e300ef 846static void gen_cmpli(DisasContext *ctx)
e1571908 847{
36f48d9c 848 if ((ctx->opcode & 0x00200000) && (ctx->insns_flags & PPC_64B)) {
e1571908
AJ
849 gen_op_cmpi(cpu_gpr[rA(ctx->opcode)], UIMM(ctx->opcode),
850 0, crfD(ctx->opcode));
36f48d9c
AG
851 } else {
852 gen_op_cmpi32(cpu_gpr[rA(ctx->opcode)], UIMM(ctx->opcode),
853 0, crfD(ctx->opcode));
02765534 854 }
e1571908
AJ
855}
856
f2442ef9
ND
857/* cmprb - range comparison: isupper, isaplha, islower*/
858static void gen_cmprb(DisasContext *ctx)
859{
860 TCGv_i32 src1 = tcg_temp_new_i32();
861 TCGv_i32 src2 = tcg_temp_new_i32();
862 TCGv_i32 src2lo = tcg_temp_new_i32();
863 TCGv_i32 src2hi = tcg_temp_new_i32();
864 TCGv_i32 crf = cpu_crf[crfD(ctx->opcode)];
865
866 tcg_gen_trunc_tl_i32(src1, cpu_gpr[rA(ctx->opcode)]);
867 tcg_gen_trunc_tl_i32(src2, cpu_gpr[rB(ctx->opcode)]);
868
869 tcg_gen_andi_i32(src1, src1, 0xFF);
870 tcg_gen_ext8u_i32(src2lo, src2);
871 tcg_gen_shri_i32(src2, src2, 8);
872 tcg_gen_ext8u_i32(src2hi, src2);
873
874 tcg_gen_setcond_i32(TCG_COND_LEU, src2lo, src2lo, src1);
875 tcg_gen_setcond_i32(TCG_COND_LEU, src2hi, src1, src2hi);
876 tcg_gen_and_i32(crf, src2lo, src2hi);
877
878 if (ctx->opcode & 0x00200000) {
879 tcg_gen_shri_i32(src2, src2, 8);
880 tcg_gen_ext8u_i32(src2lo, src2);
881 tcg_gen_shri_i32(src2, src2, 8);
882 tcg_gen_ext8u_i32(src2hi, src2);
883 tcg_gen_setcond_i32(TCG_COND_LEU, src2lo, src2lo, src1);
884 tcg_gen_setcond_i32(TCG_COND_LEU, src2hi, src1, src2hi);
885 tcg_gen_and_i32(src2lo, src2lo, src2hi);
886 tcg_gen_or_i32(crf, crf, src2lo);
887 }
888 tcg_gen_shli_i32(crf, crf, CRF_GT);
889 tcg_temp_free_i32(src1);
890 tcg_temp_free_i32(src2);
891 tcg_temp_free_i32(src2lo);
892 tcg_temp_free_i32(src2hi);
893}
894
082ce330
ND
895#if defined(TARGET_PPC64)
896/* cmpeqb */
897static void gen_cmpeqb(DisasContext *ctx)
898{
899 gen_helper_cmpeqb(cpu_crf[crfD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)],
900 cpu_gpr[rB(ctx->opcode)]);
901}
902#endif
903
e1571908 904/* isel (PowerPC 2.03 specification) */
99e300ef 905static void gen_isel(DisasContext *ctx)
e1571908 906{
e1571908 907 uint32_t bi = rC(ctx->opcode);
24f9cd95
RH
908 uint32_t mask = 0x08 >> (bi & 0x03);
909 TCGv t0 = tcg_temp_new();
910 TCGv zr;
e1571908 911
24f9cd95
RH
912 tcg_gen_extu_i32_tl(t0, cpu_crf[bi >> 2]);
913 tcg_gen_andi_tl(t0, t0, mask);
914
915 zr = tcg_const_tl(0);
916 tcg_gen_movcond_tl(TCG_COND_NE, cpu_gpr[rD(ctx->opcode)], t0, zr,
917 rA(ctx->opcode) ? cpu_gpr[rA(ctx->opcode)] : zr,
918 cpu_gpr[rB(ctx->opcode)]);
919 tcg_temp_free(zr);
920 tcg_temp_free(t0);
e1571908
AJ
921}
922
fcfda20f
AJ
923/* cmpb: PowerPC 2.05 specification */
924static void gen_cmpb(DisasContext *ctx)
925{
926 gen_helper_cmpb(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)],
927 cpu_gpr[rB(ctx->opcode)]);
928}
929
79aceca5 930/*** Integer arithmetic ***/
79aceca5 931
636aa200
BS
932static inline void gen_op_arith_compute_ov(DisasContext *ctx, TCGv arg0,
933 TCGv arg1, TCGv arg2, int sub)
74637406 934{
ffe30937 935 TCGv t0 = tcg_temp_new();
79aceca5 936
8e7a6db9 937 tcg_gen_xor_tl(cpu_ov, arg0, arg2);
74637406 938 tcg_gen_xor_tl(t0, arg1, arg2);
ffe30937
RH
939 if (sub) {
940 tcg_gen_and_tl(cpu_ov, cpu_ov, t0);
941 } else {
942 tcg_gen_andc_tl(cpu_ov, cpu_ov, t0);
943 }
944 tcg_temp_free(t0);
02765534 945 if (NARROW_MODE(ctx)) {
ffe30937
RH
946 tcg_gen_ext32s_tl(cpu_ov, cpu_ov);
947 }
ffe30937
RH
948 tcg_gen_shri_tl(cpu_ov, cpu_ov, TARGET_LONG_BITS - 1);
949 tcg_gen_or_tl(cpu_so, cpu_so, cpu_ov);
79aceca5
FB
950}
951
74637406 952/* Common add function */
636aa200 953static inline void gen_op_arith_add(DisasContext *ctx, TCGv ret, TCGv arg1,
b5a73f8d
RH
954 TCGv arg2, bool add_ca, bool compute_ca,
955 bool compute_ov, bool compute_rc0)
74637406 956{
b5a73f8d 957 TCGv t0 = ret;
d9bce9d9 958
752d634e 959 if (compute_ca || compute_ov) {
146de60d 960 t0 = tcg_temp_new();
74637406 961 }
79aceca5 962
da91a00f 963 if (compute_ca) {
79482e5a 964 if (NARROW_MODE(ctx)) {
752d634e
RH
965 /* Caution: a non-obvious corner case of the spec is that we
966 must produce the *entire* 64-bit addition, but produce the
967 carry into bit 32. */
79482e5a 968 TCGv t1 = tcg_temp_new();
752d634e
RH
969 tcg_gen_xor_tl(t1, arg1, arg2); /* add without carry */
970 tcg_gen_add_tl(t0, arg1, arg2);
79482e5a
RH
971 if (add_ca) {
972 tcg_gen_add_tl(t0, t0, cpu_ca);
973 }
752d634e
RH
974 tcg_gen_xor_tl(cpu_ca, t0, t1); /* bits changed w/ carry */
975 tcg_temp_free(t1);
976 tcg_gen_shri_tl(cpu_ca, cpu_ca, 32); /* extract bit 32 */
977 tcg_gen_andi_tl(cpu_ca, cpu_ca, 1);
b5a73f8d 978 } else {
79482e5a
RH
979 TCGv zero = tcg_const_tl(0);
980 if (add_ca) {
981 tcg_gen_add2_tl(t0, cpu_ca, arg1, zero, cpu_ca, zero);
982 tcg_gen_add2_tl(t0, cpu_ca, t0, cpu_ca, arg2, zero);
983 } else {
984 tcg_gen_add2_tl(t0, cpu_ca, arg1, zero, arg2, zero);
985 }
986 tcg_temp_free(zero);
b5a73f8d 987 }
b5a73f8d
RH
988 } else {
989 tcg_gen_add_tl(t0, arg1, arg2);
990 if (add_ca) {
991 tcg_gen_add_tl(t0, t0, cpu_ca);
992 }
da91a00f 993 }
79aceca5 994
74637406
AJ
995 if (compute_ov) {
996 gen_op_arith_compute_ov(ctx, t0, arg1, arg2, 0);
997 }
b5a73f8d 998 if (unlikely(compute_rc0)) {
74637406 999 gen_set_Rc0(ctx, t0);
b5a73f8d 1000 }
74637406 1001
a7812ae4 1002 if (!TCGV_EQUAL(t0, ret)) {
74637406
AJ
1003 tcg_gen_mov_tl(ret, t0);
1004 tcg_temp_free(t0);
1005 }
39dd32ee 1006}
74637406
AJ
1007/* Add functions with two operands */
1008#define GEN_INT_ARITH_ADD(name, opc3, add_ca, compute_ca, compute_ov) \
b5a73f8d 1009static void glue(gen_, name)(DisasContext *ctx) \
74637406
AJ
1010{ \
1011 gen_op_arith_add(ctx, cpu_gpr[rD(ctx->opcode)], \
1012 cpu_gpr[rA(ctx->opcode)], cpu_gpr[rB(ctx->opcode)], \
b5a73f8d 1013 add_ca, compute_ca, compute_ov, Rc(ctx->opcode)); \
74637406
AJ
1014}
1015/* Add functions with one operand and one immediate */
1016#define GEN_INT_ARITH_ADD_CONST(name, opc3, const_val, \
1017 add_ca, compute_ca, compute_ov) \
b5a73f8d 1018static void glue(gen_, name)(DisasContext *ctx) \
74637406 1019{ \
b5a73f8d 1020 TCGv t0 = tcg_const_tl(const_val); \
74637406
AJ
1021 gen_op_arith_add(ctx, cpu_gpr[rD(ctx->opcode)], \
1022 cpu_gpr[rA(ctx->opcode)], t0, \
b5a73f8d 1023 add_ca, compute_ca, compute_ov, Rc(ctx->opcode)); \
74637406
AJ
1024 tcg_temp_free(t0); \
1025}
1026
1027/* add add. addo addo. */
1028GEN_INT_ARITH_ADD(add, 0x08, 0, 0, 0)
1029GEN_INT_ARITH_ADD(addo, 0x18, 0, 0, 1)
1030/* addc addc. addco addco. */
1031GEN_INT_ARITH_ADD(addc, 0x00, 0, 1, 0)
1032GEN_INT_ARITH_ADD(addco, 0x10, 0, 1, 1)
1033/* adde adde. addeo addeo. */
1034GEN_INT_ARITH_ADD(adde, 0x04, 1, 1, 0)
1035GEN_INT_ARITH_ADD(addeo, 0x14, 1, 1, 1)
1036/* addme addme. addmeo addmeo. */
1037GEN_INT_ARITH_ADD_CONST(addme, 0x07, -1LL, 1, 1, 0)
1038GEN_INT_ARITH_ADD_CONST(addmeo, 0x17, -1LL, 1, 1, 1)
1039/* addze addze. addzeo addzeo.*/
1040GEN_INT_ARITH_ADD_CONST(addze, 0x06, 0, 1, 1, 0)
1041GEN_INT_ARITH_ADD_CONST(addzeo, 0x16, 0, 1, 1, 1)
1042/* addi */
99e300ef 1043static void gen_addi(DisasContext *ctx)
d9bce9d9 1044{
74637406
AJ
1045 target_long simm = SIMM(ctx->opcode);
1046
1047 if (rA(ctx->opcode) == 0) {
1048 /* li case */
1049 tcg_gen_movi_tl(cpu_gpr[rD(ctx->opcode)], simm);
1050 } else {
b5a73f8d
RH
1051 tcg_gen_addi_tl(cpu_gpr[rD(ctx->opcode)],
1052 cpu_gpr[rA(ctx->opcode)], simm);
74637406 1053 }
d9bce9d9 1054}
74637406 1055/* addic addic.*/
b5a73f8d 1056static inline void gen_op_addic(DisasContext *ctx, bool compute_rc0)
d9bce9d9 1057{
b5a73f8d
RH
1058 TCGv c = tcg_const_tl(SIMM(ctx->opcode));
1059 gen_op_arith_add(ctx, cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)],
1060 c, 0, 1, 0, compute_rc0);
1061 tcg_temp_free(c);
d9bce9d9 1062}
99e300ef
BS
1063
1064static void gen_addic(DisasContext *ctx)
d9bce9d9 1065{
b5a73f8d 1066 gen_op_addic(ctx, 0);
d9bce9d9 1067}
e8eaa2c0
BS
1068
1069static void gen_addic_(DisasContext *ctx)
d9bce9d9 1070{
b5a73f8d 1071 gen_op_addic(ctx, 1);
d9bce9d9 1072}
99e300ef 1073
54623277 1074/* addis */
99e300ef 1075static void gen_addis(DisasContext *ctx)
d9bce9d9 1076{
74637406
AJ
1077 target_long simm = SIMM(ctx->opcode);
1078
1079 if (rA(ctx->opcode) == 0) {
1080 /* lis case */
1081 tcg_gen_movi_tl(cpu_gpr[rD(ctx->opcode)], simm << 16);
1082 } else {
b5a73f8d
RH
1083 tcg_gen_addi_tl(cpu_gpr[rD(ctx->opcode)],
1084 cpu_gpr[rA(ctx->opcode)], simm << 16);
74637406 1085 }
d9bce9d9 1086}
74637406 1087
c5b2b9ce
ND
1088/* addpcis */
1089static void gen_addpcis(DisasContext *ctx)
1090{
1091 target_long d = DX(ctx->opcode);
1092
1093 tcg_gen_movi_tl(cpu_gpr[rD(ctx->opcode)], ctx->nip + (d << 16));
1094}
1095
636aa200
BS
1096static inline void gen_op_arith_divw(DisasContext *ctx, TCGv ret, TCGv arg1,
1097 TCGv arg2, int sign, int compute_ov)
d9bce9d9 1098{
42a268c2
RH
1099 TCGLabel *l1 = gen_new_label();
1100 TCGLabel *l2 = gen_new_label();
a7812ae4
PB
1101 TCGv_i32 t0 = tcg_temp_local_new_i32();
1102 TCGv_i32 t1 = tcg_temp_local_new_i32();
74637406 1103
2ef1b120
AJ
1104 tcg_gen_trunc_tl_i32(t0, arg1);
1105 tcg_gen_trunc_tl_i32(t1, arg2);
1106 tcg_gen_brcondi_i32(TCG_COND_EQ, t1, 0, l1);
74637406 1107 if (sign) {
42a268c2 1108 TCGLabel *l3 = gen_new_label();
2ef1b120
AJ
1109 tcg_gen_brcondi_i32(TCG_COND_NE, t1, -1, l3);
1110 tcg_gen_brcondi_i32(TCG_COND_EQ, t0, INT32_MIN, l1);
74637406 1111 gen_set_label(l3);
2ef1b120 1112 tcg_gen_div_i32(t0, t0, t1);
74637406 1113 } else {
2ef1b120 1114 tcg_gen_divu_i32(t0, t0, t1);
74637406
AJ
1115 }
1116 if (compute_ov) {
da91a00f 1117 tcg_gen_movi_tl(cpu_ov, 0);
74637406
AJ
1118 }
1119 tcg_gen_br(l2);
1120 gen_set_label(l1);
1121 if (sign) {
2ef1b120 1122 tcg_gen_sari_i32(t0, t0, 31);
74637406
AJ
1123 } else {
1124 tcg_gen_movi_i32(t0, 0);
1125 }
1126 if (compute_ov) {
da91a00f
RH
1127 tcg_gen_movi_tl(cpu_ov, 1);
1128 tcg_gen_movi_tl(cpu_so, 1);
74637406
AJ
1129 }
1130 gen_set_label(l2);
2ef1b120 1131 tcg_gen_extu_i32_tl(ret, t0);
a7812ae4
PB
1132 tcg_temp_free_i32(t0);
1133 tcg_temp_free_i32(t1);
74637406
AJ
1134 if (unlikely(Rc(ctx->opcode) != 0))
1135 gen_set_Rc0(ctx, ret);
d9bce9d9 1136}
74637406
AJ
1137/* Div functions */
1138#define GEN_INT_ARITH_DIVW(name, opc3, sign, compute_ov) \
99e300ef 1139static void glue(gen_, name)(DisasContext *ctx) \
74637406
AJ
1140{ \
1141 gen_op_arith_divw(ctx, cpu_gpr[rD(ctx->opcode)], \
1142 cpu_gpr[rA(ctx->opcode)], cpu_gpr[rB(ctx->opcode)], \
1143 sign, compute_ov); \
1144}
1145/* divwu divwu. divwuo divwuo. */
1146GEN_INT_ARITH_DIVW(divwu, 0x0E, 0, 0);
1147GEN_INT_ARITH_DIVW(divwuo, 0x1E, 0, 1);
1148/* divw divw. divwo divwo. */
1149GEN_INT_ARITH_DIVW(divw, 0x0F, 1, 0);
1150GEN_INT_ARITH_DIVW(divwo, 0x1F, 1, 1);
98d1eb27
TM
1151
1152/* div[wd]eu[o][.] */
1153#define GEN_DIVE(name, hlpr, compute_ov) \
1154static void gen_##name(DisasContext *ctx) \
1155{ \
1156 TCGv_i32 t0 = tcg_const_i32(compute_ov); \
1157 gen_helper_##hlpr(cpu_gpr[rD(ctx->opcode)], cpu_env, \
1158 cpu_gpr[rA(ctx->opcode)], cpu_gpr[rB(ctx->opcode)], t0); \
1159 tcg_temp_free_i32(t0); \
1160 if (unlikely(Rc(ctx->opcode) != 0)) { \
1161 gen_set_Rc0(ctx, cpu_gpr[rD(ctx->opcode)]); \
1162 } \
1163}
1164
6a4fda33
TM
1165GEN_DIVE(divweu, divweu, 0);
1166GEN_DIVE(divweuo, divweu, 1);
a98eb9e9
TM
1167GEN_DIVE(divwe, divwe, 0);
1168GEN_DIVE(divweo, divwe, 1);
6a4fda33 1169
d9bce9d9 1170#if defined(TARGET_PPC64)
636aa200
BS
1171static inline void gen_op_arith_divd(DisasContext *ctx, TCGv ret, TCGv arg1,
1172 TCGv arg2, int sign, int compute_ov)
d9bce9d9 1173{
42a268c2
RH
1174 TCGLabel *l1 = gen_new_label();
1175 TCGLabel *l2 = gen_new_label();
74637406
AJ
1176
1177 tcg_gen_brcondi_i64(TCG_COND_EQ, arg2, 0, l1);
1178 if (sign) {
42a268c2 1179 TCGLabel *l3 = gen_new_label();
74637406
AJ
1180 tcg_gen_brcondi_i64(TCG_COND_NE, arg2, -1, l3);
1181 tcg_gen_brcondi_i64(TCG_COND_EQ, arg1, INT64_MIN, l1);
1182 gen_set_label(l3);
74637406
AJ
1183 tcg_gen_div_i64(ret, arg1, arg2);
1184 } else {
1185 tcg_gen_divu_i64(ret, arg1, arg2);
1186 }
1187 if (compute_ov) {
da91a00f 1188 tcg_gen_movi_tl(cpu_ov, 0);
74637406
AJ
1189 }
1190 tcg_gen_br(l2);
1191 gen_set_label(l1);
1192 if (sign) {
1193 tcg_gen_sari_i64(ret, arg1, 63);
1194 } else {
1195 tcg_gen_movi_i64(ret, 0);
1196 }
1197 if (compute_ov) {
da91a00f
RH
1198 tcg_gen_movi_tl(cpu_ov, 1);
1199 tcg_gen_movi_tl(cpu_so, 1);
74637406
AJ
1200 }
1201 gen_set_label(l2);
1202 if (unlikely(Rc(ctx->opcode) != 0))
1203 gen_set_Rc0(ctx, ret);
d9bce9d9 1204}
74637406 1205#define GEN_INT_ARITH_DIVD(name, opc3, sign, compute_ov) \
99e300ef 1206static void glue(gen_, name)(DisasContext *ctx) \
74637406 1207{ \
2ef1b120
AJ
1208 gen_op_arith_divd(ctx, cpu_gpr[rD(ctx->opcode)], \
1209 cpu_gpr[rA(ctx->opcode)], cpu_gpr[rB(ctx->opcode)], \
1210 sign, compute_ov); \
74637406
AJ
1211}
1212/* divwu divwu. divwuo divwuo. */
1213GEN_INT_ARITH_DIVD(divdu, 0x0E, 0, 0);
1214GEN_INT_ARITH_DIVD(divduo, 0x1E, 0, 1);
1215/* divw divw. divwo divwo. */
1216GEN_INT_ARITH_DIVD(divd, 0x0F, 1, 0);
1217GEN_INT_ARITH_DIVD(divdo, 0x1F, 1, 1);
98d1eb27
TM
1218
1219GEN_DIVE(divdeu, divdeu, 0);
1220GEN_DIVE(divdeuo, divdeu, 1);
e44259b6
TM
1221GEN_DIVE(divde, divde, 0);
1222GEN_DIVE(divdeo, divde, 1);
d9bce9d9 1223#endif
74637406 1224
af2c6620
ND
1225static inline void gen_op_arith_modw(DisasContext *ctx, TCGv ret, TCGv arg1,
1226 TCGv arg2, int sign)
1227{
1228 TCGv_i32 t0 = tcg_temp_new_i32();
1229 TCGv_i32 t1 = tcg_temp_new_i32();
1230
1231 tcg_gen_trunc_tl_i32(t0, arg1);
1232 tcg_gen_trunc_tl_i32(t1, arg2);
1233 if (sign) {
1234 TCGv_i32 t2 = tcg_temp_new_i32();
1235 TCGv_i32 t3 = tcg_temp_new_i32();
1236 tcg_gen_setcondi_i32(TCG_COND_EQ, t2, t0, INT_MIN);
1237 tcg_gen_setcondi_i32(TCG_COND_EQ, t3, t1, -1);
1238 tcg_gen_and_i32(t2, t2, t3);
1239 tcg_gen_setcondi_i32(TCG_COND_EQ, t3, t1, 0);
1240 tcg_gen_or_i32(t2, t2, t3);
1241 tcg_gen_movi_i32(t3, 0);
1242 tcg_gen_movcond_i32(TCG_COND_NE, t1, t2, t3, t2, t1);
1243 tcg_gen_rem_i32(t3, t0, t1);
1244 tcg_gen_ext_i32_tl(ret, t3);
1245 tcg_temp_free_i32(t2);
1246 tcg_temp_free_i32(t3);
1247 } else {
1248 TCGv_i32 t2 = tcg_const_i32(1);
1249 TCGv_i32 t3 = tcg_const_i32(0);
1250 tcg_gen_movcond_i32(TCG_COND_EQ, t1, t1, t3, t2, t1);
1251 tcg_gen_remu_i32(t3, t0, t1);
1252 tcg_gen_extu_i32_tl(ret, t3);
1253 tcg_temp_free_i32(t2);
1254 tcg_temp_free_i32(t3);
1255 }
1256 tcg_temp_free_i32(t0);
1257 tcg_temp_free_i32(t1);
1258}
1259
1260#define GEN_INT_ARITH_MODW(name, opc3, sign) \
1261static void glue(gen_, name)(DisasContext *ctx) \
1262{ \
1263 gen_op_arith_modw(ctx, cpu_gpr[rD(ctx->opcode)], \
1264 cpu_gpr[rA(ctx->opcode)], cpu_gpr[rB(ctx->opcode)], \
1265 sign); \
1266}
1267
1268GEN_INT_ARITH_MODW(moduw, 0x08, 0);
1269GEN_INT_ARITH_MODW(modsw, 0x18, 1);
1270
063cf14f
ND
1271#if defined(TARGET_PPC64)
1272static inline void gen_op_arith_modd(DisasContext *ctx, TCGv ret, TCGv arg1,
1273 TCGv arg2, int sign)
1274{
1275 TCGv_i64 t0 = tcg_temp_new_i64();
1276 TCGv_i64 t1 = tcg_temp_new_i64();
1277
1278 tcg_gen_mov_i64(t0, arg1);
1279 tcg_gen_mov_i64(t1, arg2);
1280 if (sign) {
1281 TCGv_i64 t2 = tcg_temp_new_i64();
1282 TCGv_i64 t3 = tcg_temp_new_i64();
1283 tcg_gen_setcondi_i64(TCG_COND_EQ, t2, t0, INT64_MIN);
1284 tcg_gen_setcondi_i64(TCG_COND_EQ, t3, t1, -1);
1285 tcg_gen_and_i64(t2, t2, t3);
1286 tcg_gen_setcondi_i64(TCG_COND_EQ, t3, t1, 0);
1287 tcg_gen_or_i64(t2, t2, t3);
1288 tcg_gen_movi_i64(t3, 0);
1289 tcg_gen_movcond_i64(TCG_COND_NE, t1, t2, t3, t2, t1);
1290 tcg_gen_rem_i64(ret, t0, t1);
1291 tcg_temp_free_i64(t2);
1292 tcg_temp_free_i64(t3);
1293 } else {
1294 TCGv_i64 t2 = tcg_const_i64(1);
1295 TCGv_i64 t3 = tcg_const_i64(0);
1296 tcg_gen_movcond_i64(TCG_COND_EQ, t1, t1, t3, t2, t1);
1297 tcg_gen_remu_i64(ret, t0, t1);
1298 tcg_temp_free_i64(t2);
1299 tcg_temp_free_i64(t3);
1300 }
1301 tcg_temp_free_i64(t0);
1302 tcg_temp_free_i64(t1);
1303}
1304
1305#define GEN_INT_ARITH_MODD(name, opc3, sign) \
1306static void glue(gen_, name)(DisasContext *ctx) \
1307{ \
1308 gen_op_arith_modd(ctx, cpu_gpr[rD(ctx->opcode)], \
1309 cpu_gpr[rA(ctx->opcode)], cpu_gpr[rB(ctx->opcode)], \
1310 sign); \
1311}
1312
1313GEN_INT_ARITH_MODD(modud, 0x08, 0);
1314GEN_INT_ARITH_MODD(modsd, 0x18, 1);
1315#endif
1316
74637406 1317/* mulhw mulhw. */
99e300ef 1318static void gen_mulhw(DisasContext *ctx)
d9bce9d9 1319{
23ad1d5d
RH
1320 TCGv_i32 t0 = tcg_temp_new_i32();
1321 TCGv_i32 t1 = tcg_temp_new_i32();
74637406 1322
23ad1d5d
RH
1323 tcg_gen_trunc_tl_i32(t0, cpu_gpr[rA(ctx->opcode)]);
1324 tcg_gen_trunc_tl_i32(t1, cpu_gpr[rB(ctx->opcode)]);
1325 tcg_gen_muls2_i32(t0, t1, t0, t1);
1326 tcg_gen_extu_i32_tl(cpu_gpr[rD(ctx->opcode)], t1);
1327 tcg_temp_free_i32(t0);
1328 tcg_temp_free_i32(t1);
74637406
AJ
1329 if (unlikely(Rc(ctx->opcode) != 0))
1330 gen_set_Rc0(ctx, cpu_gpr[rD(ctx->opcode)]);
d9bce9d9 1331}
99e300ef 1332
54623277 1333/* mulhwu mulhwu. */
99e300ef 1334static void gen_mulhwu(DisasContext *ctx)
d9bce9d9 1335{
23ad1d5d
RH
1336 TCGv_i32 t0 = tcg_temp_new_i32();
1337 TCGv_i32 t1 = tcg_temp_new_i32();
74637406 1338
23ad1d5d
RH
1339 tcg_gen_trunc_tl_i32(t0, cpu_gpr[rA(ctx->opcode)]);
1340 tcg_gen_trunc_tl_i32(t1, cpu_gpr[rB(ctx->opcode)]);
1341 tcg_gen_mulu2_i32(t0, t1, t0, t1);
1342 tcg_gen_extu_i32_tl(cpu_gpr[rD(ctx->opcode)], t1);
1343 tcg_temp_free_i32(t0);
1344 tcg_temp_free_i32(t1);
74637406
AJ
1345 if (unlikely(Rc(ctx->opcode) != 0))
1346 gen_set_Rc0(ctx, cpu_gpr[rD(ctx->opcode)]);
d9bce9d9 1347}
99e300ef 1348
54623277 1349/* mullw mullw. */
99e300ef 1350static void gen_mullw(DisasContext *ctx)
d9bce9d9 1351{
1fa74845
TM
1352#if defined(TARGET_PPC64)
1353 TCGv_i64 t0, t1;
1354 t0 = tcg_temp_new_i64();
1355 t1 = tcg_temp_new_i64();
1356 tcg_gen_ext32s_tl(t0, cpu_gpr[rA(ctx->opcode)]);
1357 tcg_gen_ext32s_tl(t1, cpu_gpr[rB(ctx->opcode)]);
1358 tcg_gen_mul_i64(cpu_gpr[rD(ctx->opcode)], t0, t1);
1359 tcg_temp_free(t0);
1360 tcg_temp_free(t1);
1361#else
03039e5e
TM
1362 tcg_gen_mul_i32(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)],
1363 cpu_gpr[rB(ctx->opcode)]);
1fa74845 1364#endif
74637406
AJ
1365 if (unlikely(Rc(ctx->opcode) != 0))
1366 gen_set_Rc0(ctx, cpu_gpr[rD(ctx->opcode)]);
d9bce9d9 1367}
99e300ef 1368
54623277 1369/* mullwo mullwo. */
99e300ef 1370static void gen_mullwo(DisasContext *ctx)
d9bce9d9 1371{
e4a2c846
RH
1372 TCGv_i32 t0 = tcg_temp_new_i32();
1373 TCGv_i32 t1 = tcg_temp_new_i32();
74637406 1374
e4a2c846
RH
1375 tcg_gen_trunc_tl_i32(t0, cpu_gpr[rA(ctx->opcode)]);
1376 tcg_gen_trunc_tl_i32(t1, cpu_gpr[rB(ctx->opcode)]);
1377 tcg_gen_muls2_i32(t0, t1, t0, t1);
f11ebbf8 1378#if defined(TARGET_PPC64)
26977876
TM
1379 tcg_gen_concat_i32_i64(cpu_gpr[rD(ctx->opcode)], t0, t1);
1380#else
1381 tcg_gen_mov_i32(cpu_gpr[rD(ctx->opcode)], t0);
f11ebbf8 1382#endif
e4a2c846
RH
1383
1384 tcg_gen_sari_i32(t0, t0, 31);
1385 tcg_gen_setcond_i32(TCG_COND_NE, t0, t0, t1);
1386 tcg_gen_extu_i32_tl(cpu_ov, t0);
1387 tcg_gen_or_tl(cpu_so, cpu_so, cpu_ov);
1388
1389 tcg_temp_free_i32(t0);
1390 tcg_temp_free_i32(t1);
74637406
AJ
1391 if (unlikely(Rc(ctx->opcode) != 0))
1392 gen_set_Rc0(ctx, cpu_gpr[rD(ctx->opcode)]);
d9bce9d9 1393}
99e300ef 1394
54623277 1395/* mulli */
99e300ef 1396static void gen_mulli(DisasContext *ctx)
d9bce9d9 1397{
74637406
AJ
1398 tcg_gen_muli_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)],
1399 SIMM(ctx->opcode));
d9bce9d9 1400}
23ad1d5d 1401
d9bce9d9 1402#if defined(TARGET_PPC64)
74637406 1403/* mulhd mulhd. */
23ad1d5d
RH
1404static void gen_mulhd(DisasContext *ctx)
1405{
1406 TCGv lo = tcg_temp_new();
1407 tcg_gen_muls2_tl(lo, cpu_gpr[rD(ctx->opcode)],
1408 cpu_gpr[rA(ctx->opcode)], cpu_gpr[rB(ctx->opcode)]);
1409 tcg_temp_free(lo);
1410 if (unlikely(Rc(ctx->opcode) != 0)) {
1411 gen_set_Rc0(ctx, cpu_gpr[rD(ctx->opcode)]);
1412 }
1413}
1414
74637406 1415/* mulhdu mulhdu. */
23ad1d5d
RH
1416static void gen_mulhdu(DisasContext *ctx)
1417{
1418 TCGv lo = tcg_temp_new();
1419 tcg_gen_mulu2_tl(lo, cpu_gpr[rD(ctx->opcode)],
1420 cpu_gpr[rA(ctx->opcode)], cpu_gpr[rB(ctx->opcode)]);
1421 tcg_temp_free(lo);
1422 if (unlikely(Rc(ctx->opcode) != 0)) {
1423 gen_set_Rc0(ctx, cpu_gpr[rD(ctx->opcode)]);
1424 }
1425}
99e300ef 1426
54623277 1427/* mulld mulld. */
99e300ef 1428static void gen_mulld(DisasContext *ctx)
d9bce9d9 1429{
74637406
AJ
1430 tcg_gen_mul_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)],
1431 cpu_gpr[rB(ctx->opcode)]);
1432 if (unlikely(Rc(ctx->opcode) != 0))
1433 gen_set_Rc0(ctx, cpu_gpr[rD(ctx->opcode)]);
d9bce9d9 1434}
d15f74fb 1435
74637406 1436/* mulldo mulldo. */
d15f74fb
BS
1437static void gen_mulldo(DisasContext *ctx)
1438{
22ffad31
TM
1439 TCGv_i64 t0 = tcg_temp_new_i64();
1440 TCGv_i64 t1 = tcg_temp_new_i64();
1441
1442 tcg_gen_muls2_i64(t0, t1, cpu_gpr[rA(ctx->opcode)],
1443 cpu_gpr[rB(ctx->opcode)]);
1444 tcg_gen_mov_i64(cpu_gpr[rD(ctx->opcode)], t0);
1445
1446 tcg_gen_sari_i64(t0, t0, 63);
1447 tcg_gen_setcond_i64(TCG_COND_NE, cpu_ov, t0, t1);
1448 tcg_gen_or_tl(cpu_so, cpu_so, cpu_ov);
1449
1450 tcg_temp_free_i64(t0);
1451 tcg_temp_free_i64(t1);
1452
d15f74fb
BS
1453 if (unlikely(Rc(ctx->opcode) != 0)) {
1454 gen_set_Rc0(ctx, cpu_gpr[rD(ctx->opcode)]);
1455 }
1456}
d9bce9d9 1457#endif
74637406 1458
74637406 1459/* Common subf function */
636aa200 1460static inline void gen_op_arith_subf(DisasContext *ctx, TCGv ret, TCGv arg1,
b5a73f8d
RH
1461 TCGv arg2, bool add_ca, bool compute_ca,
1462 bool compute_ov, bool compute_rc0)
79aceca5 1463{
b5a73f8d 1464 TCGv t0 = ret;
79aceca5 1465
752d634e 1466 if (compute_ca || compute_ov) {
b5a73f8d 1467 t0 = tcg_temp_new();
da91a00f 1468 }
74637406 1469
79482e5a
RH
1470 if (compute_ca) {
1471 /* dest = ~arg1 + arg2 [+ ca]. */
1472 if (NARROW_MODE(ctx)) {
752d634e
RH
1473 /* Caution: a non-obvious corner case of the spec is that we
1474 must produce the *entire* 64-bit addition, but produce the
1475 carry into bit 32. */
79482e5a 1476 TCGv inv1 = tcg_temp_new();
752d634e 1477 TCGv t1 = tcg_temp_new();
79482e5a 1478 tcg_gen_not_tl(inv1, arg1);
79482e5a 1479 if (add_ca) {
752d634e 1480 tcg_gen_add_tl(t0, arg2, cpu_ca);
79482e5a 1481 } else {
752d634e 1482 tcg_gen_addi_tl(t0, arg2, 1);
79482e5a 1483 }
752d634e 1484 tcg_gen_xor_tl(t1, arg2, inv1); /* add without carry */
79482e5a 1485 tcg_gen_add_tl(t0, t0, inv1);
c80d1df5 1486 tcg_temp_free(inv1);
752d634e
RH
1487 tcg_gen_xor_tl(cpu_ca, t0, t1); /* bits changes w/ carry */
1488 tcg_temp_free(t1);
1489 tcg_gen_shri_tl(cpu_ca, cpu_ca, 32); /* extract bit 32 */
1490 tcg_gen_andi_tl(cpu_ca, cpu_ca, 1);
79482e5a 1491 } else if (add_ca) {
08f4a0f7
RH
1492 TCGv zero, inv1 = tcg_temp_new();
1493 tcg_gen_not_tl(inv1, arg1);
b5a73f8d
RH
1494 zero = tcg_const_tl(0);
1495 tcg_gen_add2_tl(t0, cpu_ca, arg2, zero, cpu_ca, zero);
08f4a0f7 1496 tcg_gen_add2_tl(t0, cpu_ca, t0, cpu_ca, inv1, zero);
b5a73f8d 1497 tcg_temp_free(zero);
08f4a0f7 1498 tcg_temp_free(inv1);
b5a73f8d 1499 } else {
79482e5a 1500 tcg_gen_setcond_tl(TCG_COND_GEU, cpu_ca, arg2, arg1);
b5a73f8d 1501 tcg_gen_sub_tl(t0, arg2, arg1);
b5a73f8d 1502 }
79482e5a
RH
1503 } else if (add_ca) {
1504 /* Since we're ignoring carry-out, we can simplify the
1505 standard ~arg1 + arg2 + ca to arg2 - arg1 + ca - 1. */
1506 tcg_gen_sub_tl(t0, arg2, arg1);
1507 tcg_gen_add_tl(t0, t0, cpu_ca);
1508 tcg_gen_subi_tl(t0, t0, 1);
79aceca5 1509 } else {
b5a73f8d 1510 tcg_gen_sub_tl(t0, arg2, arg1);
74637406 1511 }
b5a73f8d 1512
74637406
AJ
1513 if (compute_ov) {
1514 gen_op_arith_compute_ov(ctx, t0, arg1, arg2, 1);
1515 }
b5a73f8d 1516 if (unlikely(compute_rc0)) {
74637406 1517 gen_set_Rc0(ctx, t0);
b5a73f8d 1518 }
74637406 1519
a7812ae4 1520 if (!TCGV_EQUAL(t0, ret)) {
74637406
AJ
1521 tcg_gen_mov_tl(ret, t0);
1522 tcg_temp_free(t0);
79aceca5 1523 }
79aceca5 1524}
74637406
AJ
1525/* Sub functions with Two operands functions */
1526#define GEN_INT_ARITH_SUBF(name, opc3, add_ca, compute_ca, compute_ov) \
b5a73f8d 1527static void glue(gen_, name)(DisasContext *ctx) \
74637406
AJ
1528{ \
1529 gen_op_arith_subf(ctx, cpu_gpr[rD(ctx->opcode)], \
1530 cpu_gpr[rA(ctx->opcode)], cpu_gpr[rB(ctx->opcode)], \
b5a73f8d 1531 add_ca, compute_ca, compute_ov, Rc(ctx->opcode)); \
74637406
AJ
1532}
1533/* Sub functions with one operand and one immediate */
1534#define GEN_INT_ARITH_SUBF_CONST(name, opc3, const_val, \
1535 add_ca, compute_ca, compute_ov) \
b5a73f8d 1536static void glue(gen_, name)(DisasContext *ctx) \
74637406 1537{ \
b5a73f8d 1538 TCGv t0 = tcg_const_tl(const_val); \
74637406
AJ
1539 gen_op_arith_subf(ctx, cpu_gpr[rD(ctx->opcode)], \
1540 cpu_gpr[rA(ctx->opcode)], t0, \
b5a73f8d 1541 add_ca, compute_ca, compute_ov, Rc(ctx->opcode)); \
74637406
AJ
1542 tcg_temp_free(t0); \
1543}
1544/* subf subf. subfo subfo. */
1545GEN_INT_ARITH_SUBF(subf, 0x01, 0, 0, 0)
1546GEN_INT_ARITH_SUBF(subfo, 0x11, 0, 0, 1)
1547/* subfc subfc. subfco subfco. */
1548GEN_INT_ARITH_SUBF(subfc, 0x00, 0, 1, 0)
1549GEN_INT_ARITH_SUBF(subfco, 0x10, 0, 1, 1)
1550/* subfe subfe. subfeo subfo. */
1551GEN_INT_ARITH_SUBF(subfe, 0x04, 1, 1, 0)
1552GEN_INT_ARITH_SUBF(subfeo, 0x14, 1, 1, 1)
1553/* subfme subfme. subfmeo subfmeo. */
1554GEN_INT_ARITH_SUBF_CONST(subfme, 0x07, -1LL, 1, 1, 0)
1555GEN_INT_ARITH_SUBF_CONST(subfmeo, 0x17, -1LL, 1, 1, 1)
1556/* subfze subfze. subfzeo subfzeo.*/
1557GEN_INT_ARITH_SUBF_CONST(subfze, 0x06, 0, 1, 1, 0)
1558GEN_INT_ARITH_SUBF_CONST(subfzeo, 0x16, 0, 1, 1, 1)
99e300ef 1559
54623277 1560/* subfic */
99e300ef 1561static void gen_subfic(DisasContext *ctx)
79aceca5 1562{
b5a73f8d
RH
1563 TCGv c = tcg_const_tl(SIMM(ctx->opcode));
1564 gen_op_arith_subf(ctx, cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)],
1565 c, 0, 1, 0, 0);
1566 tcg_temp_free(c);
79aceca5
FB
1567}
1568
fd3f0081
RH
1569/* neg neg. nego nego. */
1570static inline void gen_op_arith_neg(DisasContext *ctx, bool compute_ov)
1571{
1572 TCGv zero = tcg_const_tl(0);
1573 gen_op_arith_subf(ctx, cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)],
1574 zero, 0, 0, compute_ov, Rc(ctx->opcode));
1575 tcg_temp_free(zero);
1576}
1577
1578static void gen_neg(DisasContext *ctx)
1579{
1580 gen_op_arith_neg(ctx, 0);
1581}
1582
1583static void gen_nego(DisasContext *ctx)
1584{
1585 gen_op_arith_neg(ctx, 1);
1586}
1587
79aceca5 1588/*** Integer logical ***/
26d67362 1589#define GEN_LOGICAL2(name, tcg_op, opc, type) \
99e300ef 1590static void glue(gen_, name)(DisasContext *ctx) \
79aceca5 1591{ \
26d67362
AJ
1592 tcg_op(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)], \
1593 cpu_gpr[rB(ctx->opcode)]); \
76a66253 1594 if (unlikely(Rc(ctx->opcode) != 0)) \
26d67362 1595 gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]); \
79aceca5 1596}
79aceca5 1597
26d67362 1598#define GEN_LOGICAL1(name, tcg_op, opc, type) \
99e300ef 1599static void glue(gen_, name)(DisasContext *ctx) \
79aceca5 1600{ \
26d67362 1601 tcg_op(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)]); \
76a66253 1602 if (unlikely(Rc(ctx->opcode) != 0)) \
26d67362 1603 gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]); \
79aceca5
FB
1604}
1605
1606/* and & and. */
26d67362 1607GEN_LOGICAL2(and, tcg_gen_and_tl, 0x00, PPC_INTEGER);
79aceca5 1608/* andc & andc. */
26d67362 1609GEN_LOGICAL2(andc, tcg_gen_andc_tl, 0x01, PPC_INTEGER);
e8eaa2c0 1610
54623277 1611/* andi. */
e8eaa2c0 1612static void gen_andi_(DisasContext *ctx)
79aceca5 1613{
26d67362
AJ
1614 tcg_gen_andi_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)], UIMM(ctx->opcode));
1615 gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
79aceca5 1616}
e8eaa2c0 1617
54623277 1618/* andis. */
e8eaa2c0 1619static void gen_andis_(DisasContext *ctx)
79aceca5 1620{
26d67362
AJ
1621 tcg_gen_andi_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)], UIMM(ctx->opcode) << 16);
1622 gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
79aceca5 1623}
99e300ef 1624
54623277 1625/* cntlzw */
99e300ef 1626static void gen_cntlzw(DisasContext *ctx)
26d67362 1627{
a7812ae4 1628 gen_helper_cntlzw(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)]);
26d67362 1629 if (unlikely(Rc(ctx->opcode) != 0))
2e31f5d3 1630 gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
26d67362 1631}
b35344e4
ND
1632
1633/* cnttzw */
1634static void gen_cnttzw(DisasContext *ctx)
1635{
1636 gen_helper_cnttzw(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)]);
1637 if (unlikely(Rc(ctx->opcode) != 0)) {
1638 gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
1639 }
1640}
1641
79aceca5 1642/* eqv & eqv. */
26d67362 1643GEN_LOGICAL2(eqv, tcg_gen_eqv_tl, 0x08, PPC_INTEGER);
79aceca5 1644/* extsb & extsb. */
26d67362 1645GEN_LOGICAL1(extsb, tcg_gen_ext8s_tl, 0x1D, PPC_INTEGER);
79aceca5 1646/* extsh & extsh. */
26d67362 1647GEN_LOGICAL1(extsh, tcg_gen_ext16s_tl, 0x1C, PPC_INTEGER);
79aceca5 1648/* nand & nand. */
26d67362 1649GEN_LOGICAL2(nand, tcg_gen_nand_tl, 0x0E, PPC_INTEGER);
79aceca5 1650/* nor & nor. */
26d67362 1651GEN_LOGICAL2(nor, tcg_gen_nor_tl, 0x03, PPC_INTEGER);
99e300ef 1652
7f2b1744 1653#if defined(TARGET_PPC64) && !defined(CONFIG_USER_ONLY)
b68e60e6
BH
1654static void gen_pause(DisasContext *ctx)
1655{
1656 TCGv_i32 t0 = tcg_const_i32(0);
1657 tcg_gen_st_i32(t0, cpu_env,
1658 -offsetof(PowerPCCPU, env) + offsetof(CPUState, halted));
1659 tcg_temp_free_i32(t0);
1660
1661 /* Stop translation, this gives other CPUs a chance to run */
bd6fefe7 1662 gen_exception_nip(ctx, EXCP_HLT, ctx->nip);
b68e60e6
BH
1663}
1664#endif /* defined(TARGET_PPC64) */
1665
54623277 1666/* or & or. */
99e300ef 1667static void gen_or(DisasContext *ctx)
9a64fbe4 1668{
76a66253
JM
1669 int rs, ra, rb;
1670
1671 rs = rS(ctx->opcode);
1672 ra = rA(ctx->opcode);
1673 rb = rB(ctx->opcode);
1674 /* Optimisation for mr. ri case */
1675 if (rs != ra || rs != rb) {
26d67362
AJ
1676 if (rs != rb)
1677 tcg_gen_or_tl(cpu_gpr[ra], cpu_gpr[rs], cpu_gpr[rb]);
1678 else
1679 tcg_gen_mov_tl(cpu_gpr[ra], cpu_gpr[rs]);
76a66253 1680 if (unlikely(Rc(ctx->opcode) != 0))
26d67362 1681 gen_set_Rc0(ctx, cpu_gpr[ra]);
76a66253 1682 } else if (unlikely(Rc(ctx->opcode) != 0)) {
26d67362 1683 gen_set_Rc0(ctx, cpu_gpr[rs]);
c80f84e3 1684#if defined(TARGET_PPC64)
9e196938 1685 } else if (rs != 0) { /* 0 is nop */
26d67362
AJ
1686 int prio = 0;
1687
c80f84e3
JM
1688 switch (rs) {
1689 case 1:
1690 /* Set process priority to low */
26d67362 1691 prio = 2;
c80f84e3
JM
1692 break;
1693 case 6:
1694 /* Set process priority to medium-low */
26d67362 1695 prio = 3;
c80f84e3
JM
1696 break;
1697 case 2:
1698 /* Set process priority to normal */
26d67362 1699 prio = 4;
c80f84e3 1700 break;
be147d08
JM
1701#if !defined(CONFIG_USER_ONLY)
1702 case 31:
c47493f2 1703 if (!ctx->pr) {
be147d08 1704 /* Set process priority to very low */
26d67362 1705 prio = 1;
be147d08
JM
1706 }
1707 break;
1708 case 5:
c47493f2 1709 if (!ctx->pr) {
be147d08 1710 /* Set process priority to medium-hight */
26d67362 1711 prio = 5;
be147d08
JM
1712 }
1713 break;
1714 case 3:
c47493f2 1715 if (!ctx->pr) {
be147d08 1716 /* Set process priority to high */
26d67362 1717 prio = 6;
be147d08
JM
1718 }
1719 break;
be147d08 1720 case 7:
b68e60e6 1721 if (ctx->hv && !ctx->pr) {
be147d08 1722 /* Set process priority to very high */
26d67362 1723 prio = 7;
be147d08
JM
1724 }
1725 break;
be147d08 1726#endif
c80f84e3 1727 default:
c80f84e3
JM
1728 break;
1729 }
26d67362 1730 if (prio) {
a7812ae4 1731 TCGv t0 = tcg_temp_new();
54cdcae6 1732 gen_load_spr(t0, SPR_PPR);
ea363694
AJ
1733 tcg_gen_andi_tl(t0, t0, ~0x001C000000000000ULL);
1734 tcg_gen_ori_tl(t0, t0, ((uint64_t)prio) << 50);
54cdcae6 1735 gen_store_spr(SPR_PPR, t0);
ea363694 1736 tcg_temp_free(t0);
9e196938 1737 }
7f2b1744 1738#if !defined(CONFIG_USER_ONLY)
9e196938
AL
1739 /* Pause out of TCG otherwise spin loops with smt_low eat too much
1740 * CPU and the kernel hangs. This applies to all encodings other
1741 * than no-op, e.g., miso(rs=26), yield(27), mdoio(29), mdoom(30),
1742 * and all currently undefined.
1743 */
1744 gen_pause(ctx);
7f2b1744 1745#endif
c80f84e3 1746#endif
9a64fbe4 1747 }
9a64fbe4 1748}
79aceca5 1749/* orc & orc. */
26d67362 1750GEN_LOGICAL2(orc, tcg_gen_orc_tl, 0x0C, PPC_INTEGER);
99e300ef 1751
54623277 1752/* xor & xor. */
99e300ef 1753static void gen_xor(DisasContext *ctx)
9a64fbe4 1754{
9a64fbe4 1755 /* Optimisation for "set to zero" case */
26d67362 1756 if (rS(ctx->opcode) != rB(ctx->opcode))
312179c4 1757 tcg_gen_xor_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)], cpu_gpr[rB(ctx->opcode)]);
26d67362
AJ
1758 else
1759 tcg_gen_movi_tl(cpu_gpr[rA(ctx->opcode)], 0);
76a66253 1760 if (unlikely(Rc(ctx->opcode) != 0))
26d67362 1761 gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
9a64fbe4 1762}
99e300ef 1763
54623277 1764/* ori */
99e300ef 1765static void gen_ori(DisasContext *ctx)
79aceca5 1766{
76a66253 1767 target_ulong uimm = UIMM(ctx->opcode);
79aceca5 1768
9a64fbe4 1769 if (rS(ctx->opcode) == rA(ctx->opcode) && uimm == 0) {
9a64fbe4 1770 return;
76a66253 1771 }
26d67362 1772 tcg_gen_ori_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)], uimm);
79aceca5 1773}
99e300ef 1774
54623277 1775/* oris */
99e300ef 1776static void gen_oris(DisasContext *ctx)
79aceca5 1777{
76a66253 1778 target_ulong uimm = UIMM(ctx->opcode);
79aceca5 1779
9a64fbe4
FB
1780 if (rS(ctx->opcode) == rA(ctx->opcode) && uimm == 0) {
1781 /* NOP */
1782 return;
76a66253 1783 }
26d67362 1784 tcg_gen_ori_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)], uimm << 16);
79aceca5 1785}
99e300ef 1786
54623277 1787/* xori */
99e300ef 1788static void gen_xori(DisasContext *ctx)
79aceca5 1789{
76a66253 1790 target_ulong uimm = UIMM(ctx->opcode);
9a64fbe4
FB
1791
1792 if (rS(ctx->opcode) == rA(ctx->opcode) && uimm == 0) {
1793 /* NOP */
1794 return;
1795 }
26d67362 1796 tcg_gen_xori_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)], uimm);
79aceca5 1797}
99e300ef 1798
54623277 1799/* xoris */
99e300ef 1800static void gen_xoris(DisasContext *ctx)
79aceca5 1801{
76a66253 1802 target_ulong uimm = UIMM(ctx->opcode);
9a64fbe4
FB
1803
1804 if (rS(ctx->opcode) == rA(ctx->opcode) && uimm == 0) {
1805 /* NOP */
1806 return;
1807 }
26d67362 1808 tcg_gen_xori_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)], uimm << 16);
79aceca5 1809}
99e300ef 1810
54623277 1811/* popcntb : PowerPC 2.03 specification */
99e300ef 1812static void gen_popcntb(DisasContext *ctx)
d9bce9d9 1813{
eaabeef2
DG
1814 gen_helper_popcntb(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)]);
1815}
1816
1817static void gen_popcntw(DisasContext *ctx)
1818{
1819 gen_helper_popcntw(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)]);
1820}
1821
d9bce9d9 1822#if defined(TARGET_PPC64)
eaabeef2
DG
1823/* popcntd: PowerPC 2.06 specification */
1824static void gen_popcntd(DisasContext *ctx)
1825{
1826 gen_helper_popcntd(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)]);
d9bce9d9 1827}
eaabeef2 1828#endif
d9bce9d9 1829
725bcec2
AJ
1830/* prtyw: PowerPC 2.05 specification */
1831static void gen_prtyw(DisasContext *ctx)
1832{
1833 TCGv ra = cpu_gpr[rA(ctx->opcode)];
1834 TCGv rs = cpu_gpr[rS(ctx->opcode)];
1835 TCGv t0 = tcg_temp_new();
1836 tcg_gen_shri_tl(t0, rs, 16);
1837 tcg_gen_xor_tl(ra, rs, t0);
1838 tcg_gen_shri_tl(t0, ra, 8);
1839 tcg_gen_xor_tl(ra, ra, t0);
1840 tcg_gen_andi_tl(ra, ra, (target_ulong)0x100000001ULL);
1841 tcg_temp_free(t0);
1842}
1843
1844#if defined(TARGET_PPC64)
1845/* prtyd: PowerPC 2.05 specification */
1846static void gen_prtyd(DisasContext *ctx)
1847{
1848 TCGv ra = cpu_gpr[rA(ctx->opcode)];
1849 TCGv rs = cpu_gpr[rS(ctx->opcode)];
1850 TCGv t0 = tcg_temp_new();
1851 tcg_gen_shri_tl(t0, rs, 32);
1852 tcg_gen_xor_tl(ra, rs, t0);
1853 tcg_gen_shri_tl(t0, ra, 16);
1854 tcg_gen_xor_tl(ra, ra, t0);
1855 tcg_gen_shri_tl(t0, ra, 8);
1856 tcg_gen_xor_tl(ra, ra, t0);
1857 tcg_gen_andi_tl(ra, ra, 1);
1858 tcg_temp_free(t0);
1859}
1860#endif
1861
86ba37ed
TM
1862#if defined(TARGET_PPC64)
1863/* bpermd */
1864static void gen_bpermd(DisasContext *ctx)
1865{
1866 gen_helper_bpermd(cpu_gpr[rA(ctx->opcode)],
1867 cpu_gpr[rS(ctx->opcode)], cpu_gpr[rB(ctx->opcode)]);
1868}
1869#endif
1870
d9bce9d9
JM
1871#if defined(TARGET_PPC64)
1872/* extsw & extsw. */
26d67362 1873GEN_LOGICAL1(extsw, tcg_gen_ext32s_tl, 0x1E, PPC_64B);
99e300ef 1874
54623277 1875/* cntlzd */
99e300ef 1876static void gen_cntlzd(DisasContext *ctx)
26d67362 1877{
a7812ae4 1878 gen_helper_cntlzd(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)]);
26d67362
AJ
1879 if (unlikely(Rc(ctx->opcode) != 0))
1880 gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
1881}
e91d95b2
SD
1882
1883/* cnttzd */
1884static void gen_cnttzd(DisasContext *ctx)
1885{
1886 gen_helper_cnttzd(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)]);
1887 if (unlikely(Rc(ctx->opcode) != 0)) {
1888 gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
1889 }
1890}
d9bce9d9
JM
1891#endif
1892
79aceca5 1893/*** Integer rotate ***/
99e300ef 1894
54623277 1895/* rlwimi & rlwimi. */
99e300ef 1896static void gen_rlwimi(DisasContext *ctx)
79aceca5 1897{
63ae0915
RH
1898 TCGv t_ra = cpu_gpr[rA(ctx->opcode)];
1899 TCGv t_rs = cpu_gpr[rS(ctx->opcode)];
1900 uint32_t sh = SH(ctx->opcode);
1901 uint32_t mb = MB(ctx->opcode);
1902 uint32_t me = ME(ctx->opcode);
1903
1904 if (sh == (31-me) && mb <= me) {
1905 tcg_gen_deposit_tl(t_ra, t_ra, t_rs, sh, me - mb + 1);
d03ef511 1906 } else {
d03ef511 1907 target_ulong mask;
a7812ae4 1908 TCGv t1;
63ae0915 1909
76a66253 1910#if defined(TARGET_PPC64)
d03ef511
AJ
1911 mb += 32;
1912 me += 32;
76a66253 1913#endif
d03ef511 1914 mask = MASK(mb, me);
63ae0915 1915
a7812ae4 1916 t1 = tcg_temp_new();
2e11b15d
RH
1917 if (mask <= 0xffffffffu) {
1918 TCGv_i32 t0 = tcg_temp_new_i32();
1919 tcg_gen_trunc_tl_i32(t0, t_rs);
1920 tcg_gen_rotli_i32(t0, t0, sh);
1921 tcg_gen_extu_i32_tl(t1, t0);
1922 tcg_temp_free_i32(t0);
1923 } else {
1924#if defined(TARGET_PPC64)
1925 tcg_gen_deposit_i64(t1, t_rs, t_rs, 32, 32);
1926 tcg_gen_rotli_i64(t1, t1, sh);
1927#else
1928 g_assert_not_reached();
1929#endif
1930 }
63ae0915
RH
1931
1932 tcg_gen_andi_tl(t1, t1, mask);
1933 tcg_gen_andi_tl(t_ra, t_ra, ~mask);
1934 tcg_gen_or_tl(t_ra, t_ra, t1);
d03ef511
AJ
1935 tcg_temp_free(t1);
1936 }
63ae0915
RH
1937 if (unlikely(Rc(ctx->opcode) != 0)) {
1938 gen_set_Rc0(ctx, t_ra);
1939 }
79aceca5 1940}
99e300ef 1941
54623277 1942/* rlwinm & rlwinm. */
99e300ef 1943static void gen_rlwinm(DisasContext *ctx)
79aceca5 1944{
63ae0915
RH
1945 TCGv t_ra = cpu_gpr[rA(ctx->opcode)];
1946 TCGv t_rs = cpu_gpr[rS(ctx->opcode)];
1947 uint32_t sh = SH(ctx->opcode);
1948 uint32_t mb = MB(ctx->opcode);
1949 uint32_t me = ME(ctx->opcode);
1950
1951 if (mb == 0 && me == (31 - sh)) {
1952 tcg_gen_shli_tl(t_ra, t_rs, sh);
1953 tcg_gen_ext32u_tl(t_ra, t_ra);
1954 } else if (sh != 0 && me == 31 && sh == (32 - mb)) {
1955 tcg_gen_ext32u_tl(t_ra, t_rs);
1956 tcg_gen_shri_tl(t_ra, t_ra, mb);
d03ef511 1957 } else {
2e11b15d 1958 target_ulong mask;
76a66253 1959#if defined(TARGET_PPC64)
d03ef511
AJ
1960 mb += 32;
1961 me += 32;
76a66253 1962#endif
2e11b15d
RH
1963 mask = MASK(mb, me);
1964
820724d1 1965 if (mask <= 0xffffffffu) {
63ae0915 1966 TCGv_i32 t0 = tcg_temp_new_i32();
63ae0915
RH
1967 tcg_gen_trunc_tl_i32(t0, t_rs);
1968 tcg_gen_rotli_i32(t0, t0, sh);
2e11b15d 1969 tcg_gen_andi_i32(t0, t0, mask);
63ae0915
RH
1970 tcg_gen_extu_i32_tl(t_ra, t0);
1971 tcg_temp_free_i32(t0);
2e11b15d
RH
1972 } else {
1973#if defined(TARGET_PPC64)
1974 tcg_gen_deposit_i64(t_ra, t_rs, t_rs, 32, 32);
1975 tcg_gen_rotli_i64(t_ra, t_ra, sh);
1976 tcg_gen_andi_i64(t_ra, t_ra, mask);
1977#else
1978 g_assert_not_reached();
1979#endif
63ae0915
RH
1980 }
1981 }
1982 if (unlikely(Rc(ctx->opcode) != 0)) {
1983 gen_set_Rc0(ctx, t_ra);
d03ef511 1984 }
79aceca5 1985}
99e300ef 1986
54623277 1987/* rlwnm & rlwnm. */
99e300ef 1988static void gen_rlwnm(DisasContext *ctx)
79aceca5 1989{
63ae0915
RH
1990 TCGv t_ra = cpu_gpr[rA(ctx->opcode)];
1991 TCGv t_rs = cpu_gpr[rS(ctx->opcode)];
1992 TCGv t_rb = cpu_gpr[rB(ctx->opcode)];
1993 uint32_t mb = MB(ctx->opcode);
1994 uint32_t me = ME(ctx->opcode);
2e11b15d 1995 target_ulong mask;
57fca134 1996
54843a58 1997#if defined(TARGET_PPC64)
63ae0915
RH
1998 mb += 32;
1999 me += 32;
54843a58 2000#endif
2e11b15d
RH
2001 mask = MASK(mb, me);
2002
2003 if (mask <= 0xffffffffu) {
2004 TCGv_i32 t0 = tcg_temp_new_i32();
2005 TCGv_i32 t1 = tcg_temp_new_i32();
2006 tcg_gen_trunc_tl_i32(t0, t_rb);
2007 tcg_gen_trunc_tl_i32(t1, t_rs);
2008 tcg_gen_andi_i32(t0, t0, 0x1f);
2009 tcg_gen_rotl_i32(t1, t1, t0);
2010 tcg_gen_extu_i32_tl(t_ra, t1);
2011 tcg_temp_free_i32(t0);
2012 tcg_temp_free_i32(t1);
2013 } else {
2014#if defined(TARGET_PPC64)
2015 TCGv_i64 t0 = tcg_temp_new_i64();
2016 tcg_gen_andi_i64(t0, t_rb, 0x1f);
2017 tcg_gen_deposit_i64(t_ra, t_rs, t_rs, 32, 32);
2018 tcg_gen_rotl_i64(t_ra, t_ra, t0);
2019 tcg_temp_free_i64(t0);
2020#else
2021 g_assert_not_reached();
2022#endif
2023 }
57fca134 2024
2e11b15d 2025 tcg_gen_andi_tl(t_ra, t_ra, mask);
63ae0915
RH
2026
2027 if (unlikely(Rc(ctx->opcode) != 0)) {
2028 gen_set_Rc0(ctx, t_ra);
79aceca5 2029 }
79aceca5
FB
2030}
2031
d9bce9d9
JM
2032#if defined(TARGET_PPC64)
2033#define GEN_PPC64_R2(name, opc1, opc2) \
e8eaa2c0 2034static void glue(gen_, name##0)(DisasContext *ctx) \
d9bce9d9
JM
2035{ \
2036 gen_##name(ctx, 0); \
2037} \
e8eaa2c0
BS
2038 \
2039static void glue(gen_, name##1)(DisasContext *ctx) \
d9bce9d9
JM
2040{ \
2041 gen_##name(ctx, 1); \
2042}
2043#define GEN_PPC64_R4(name, opc1, opc2) \
e8eaa2c0 2044static void glue(gen_, name##0)(DisasContext *ctx) \
d9bce9d9
JM
2045{ \
2046 gen_##name(ctx, 0, 0); \
2047} \
e8eaa2c0
BS
2048 \
2049static void glue(gen_, name##1)(DisasContext *ctx) \
d9bce9d9
JM
2050{ \
2051 gen_##name(ctx, 0, 1); \
2052} \
e8eaa2c0
BS
2053 \
2054static void glue(gen_, name##2)(DisasContext *ctx) \
d9bce9d9
JM
2055{ \
2056 gen_##name(ctx, 1, 0); \
2057} \
e8eaa2c0
BS
2058 \
2059static void glue(gen_, name##3)(DisasContext *ctx) \
d9bce9d9
JM
2060{ \
2061 gen_##name(ctx, 1, 1); \
2062}
51789c41 2063
a7b2c8b9 2064static void gen_rldinm(DisasContext *ctx, int mb, int me, int sh)
51789c41 2065{
a7b2c8b9
RH
2066 TCGv t_ra = cpu_gpr[rA(ctx->opcode)];
2067 TCGv t_rs = cpu_gpr[rS(ctx->opcode)];
2068
2069 if (sh != 0 && mb == 0 && me == (63 - sh)) {
2070 tcg_gen_shli_tl(t_ra, t_rs, sh);
2071 } else if (sh != 0 && me == 63 && sh == (64 - mb)) {
2072 tcg_gen_shri_tl(t_ra, t_rs, mb);
d03ef511 2073 } else {
a7b2c8b9
RH
2074 tcg_gen_rotli_tl(t_ra, t_rs, sh);
2075 tcg_gen_andi_tl(t_ra, t_ra, MASK(mb, me));
2076 }
2077 if (unlikely(Rc(ctx->opcode) != 0)) {
2078 gen_set_Rc0(ctx, t_ra);
51789c41 2079 }
51789c41 2080}
a7b2c8b9 2081
d9bce9d9 2082/* rldicl - rldicl. */
636aa200 2083static inline void gen_rldicl(DisasContext *ctx, int mbn, int shn)
d9bce9d9 2084{
51789c41 2085 uint32_t sh, mb;
d9bce9d9 2086
9d53c753
JM
2087 sh = SH(ctx->opcode) | (shn << 5);
2088 mb = MB(ctx->opcode) | (mbn << 5);
51789c41 2089 gen_rldinm(ctx, mb, 63, sh);
d9bce9d9 2090}
51789c41 2091GEN_PPC64_R4(rldicl, 0x1E, 0x00);
a7b2c8b9 2092
d9bce9d9 2093/* rldicr - rldicr. */
636aa200 2094static inline void gen_rldicr(DisasContext *ctx, int men, int shn)
d9bce9d9 2095{
51789c41 2096 uint32_t sh, me;
d9bce9d9 2097
9d53c753
JM
2098 sh = SH(ctx->opcode) | (shn << 5);
2099 me = MB(ctx->opcode) | (men << 5);
51789c41 2100 gen_rldinm(ctx, 0, me, sh);
d9bce9d9 2101}
51789c41 2102GEN_PPC64_R4(rldicr, 0x1E, 0x02);
a7b2c8b9 2103
d9bce9d9 2104/* rldic - rldic. */
636aa200 2105static inline void gen_rldic(DisasContext *ctx, int mbn, int shn)
d9bce9d9 2106{
51789c41 2107 uint32_t sh, mb;
d9bce9d9 2108
9d53c753
JM
2109 sh = SH(ctx->opcode) | (shn << 5);
2110 mb = MB(ctx->opcode) | (mbn << 5);
51789c41
JM
2111 gen_rldinm(ctx, mb, 63 - sh, sh);
2112}
2113GEN_PPC64_R4(rldic, 0x1E, 0x04);
2114
a7b2c8b9 2115static void gen_rldnm(DisasContext *ctx, int mb, int me)
51789c41 2116{
a7b2c8b9
RH
2117 TCGv t_ra = cpu_gpr[rA(ctx->opcode)];
2118 TCGv t_rs = cpu_gpr[rS(ctx->opcode)];
2119 TCGv t_rb = cpu_gpr[rB(ctx->opcode)];
54843a58 2120 TCGv t0;
d03ef511 2121
a7812ae4 2122 t0 = tcg_temp_new();
a7b2c8b9
RH
2123 tcg_gen_andi_tl(t0, t_rb, 0x3f);
2124 tcg_gen_rotl_tl(t_ra, t_rs, t0);
54843a58 2125 tcg_temp_free(t0);
a7b2c8b9
RH
2126
2127 tcg_gen_andi_tl(t_ra, t_ra, MASK(mb, me));
2128 if (unlikely(Rc(ctx->opcode) != 0)) {
2129 gen_set_Rc0(ctx, t_ra);
2130 }
d9bce9d9 2131}
51789c41 2132
d9bce9d9 2133/* rldcl - rldcl. */
636aa200 2134static inline void gen_rldcl(DisasContext *ctx, int mbn)
d9bce9d9 2135{
51789c41 2136 uint32_t mb;
d9bce9d9 2137
9d53c753 2138 mb = MB(ctx->opcode) | (mbn << 5);
51789c41 2139 gen_rldnm(ctx, mb, 63);
d9bce9d9 2140}
36081602 2141GEN_PPC64_R2(rldcl, 0x1E, 0x08);
a7b2c8b9 2142
d9bce9d9 2143/* rldcr - rldcr. */
636aa200 2144static inline void gen_rldcr(DisasContext *ctx, int men)
d9bce9d9 2145{
51789c41 2146 uint32_t me;
d9bce9d9 2147
9d53c753 2148 me = MB(ctx->opcode) | (men << 5);
51789c41 2149 gen_rldnm(ctx, 0, me);
d9bce9d9 2150}
36081602 2151GEN_PPC64_R2(rldcr, 0x1E, 0x09);
a7b2c8b9 2152
d9bce9d9 2153/* rldimi - rldimi. */
a7b2c8b9 2154static void gen_rldimi(DisasContext *ctx, int mbn, int shn)
d9bce9d9 2155{
a7b2c8b9
RH
2156 TCGv t_ra = cpu_gpr[rA(ctx->opcode)];
2157 TCGv t_rs = cpu_gpr[rS(ctx->opcode)];
2158 uint32_t sh = SH(ctx->opcode) | (shn << 5);
2159 uint32_t mb = MB(ctx->opcode) | (mbn << 5);
2160 uint32_t me = 63 - sh;
d9bce9d9 2161
a7b2c8b9
RH
2162 if (mb <= me) {
2163 tcg_gen_deposit_tl(t_ra, t_ra, t_rs, sh, me - mb + 1);
d03ef511 2164 } else {
a7b2c8b9
RH
2165 target_ulong mask = MASK(mb, me);
2166 TCGv t1 = tcg_temp_new();
d03ef511 2167
a7b2c8b9
RH
2168 tcg_gen_rotli_tl(t1, t_rs, sh);
2169 tcg_gen_andi_tl(t1, t1, mask);
2170 tcg_gen_andi_tl(t_ra, t_ra, ~mask);
2171 tcg_gen_or_tl(t_ra, t_ra, t1);
d03ef511 2172 tcg_temp_free(t1);
51789c41 2173 }
a7b2c8b9
RH
2174 if (unlikely(Rc(ctx->opcode) != 0)) {
2175 gen_set_Rc0(ctx, t_ra);
2176 }
d9bce9d9 2177}
36081602 2178GEN_PPC64_R4(rldimi, 0x1E, 0x06);
d9bce9d9
JM
2179#endif
2180
79aceca5 2181/*** Integer shift ***/
99e300ef 2182
54623277 2183/* slw & slw. */
99e300ef 2184static void gen_slw(DisasContext *ctx)
26d67362 2185{
7fd6bf7d 2186 TCGv t0, t1;
26d67362 2187
7fd6bf7d
AJ
2188 t0 = tcg_temp_new();
2189 /* AND rS with a mask that is 0 when rB >= 0x20 */
2190#if defined(TARGET_PPC64)
2191 tcg_gen_shli_tl(t0, cpu_gpr[rB(ctx->opcode)], 0x3a);
2192 tcg_gen_sari_tl(t0, t0, 0x3f);
2193#else
2194 tcg_gen_shli_tl(t0, cpu_gpr[rB(ctx->opcode)], 0x1a);
2195 tcg_gen_sari_tl(t0, t0, 0x1f);
2196#endif
2197 tcg_gen_andc_tl(t0, cpu_gpr[rS(ctx->opcode)], t0);
2198 t1 = tcg_temp_new();
2199 tcg_gen_andi_tl(t1, cpu_gpr[rB(ctx->opcode)], 0x1f);
2200 tcg_gen_shl_tl(cpu_gpr[rA(ctx->opcode)], t0, t1);
2201 tcg_temp_free(t1);
fea0c503 2202 tcg_temp_free(t0);
7fd6bf7d 2203 tcg_gen_ext32u_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rA(ctx->opcode)]);
26d67362
AJ
2204 if (unlikely(Rc(ctx->opcode) != 0))
2205 gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
2206}
99e300ef 2207
54623277 2208/* sraw & sraw. */
99e300ef 2209static void gen_sraw(DisasContext *ctx)
26d67362 2210{
d15f74fb 2211 gen_helper_sraw(cpu_gpr[rA(ctx->opcode)], cpu_env,
a7812ae4 2212 cpu_gpr[rS(ctx->opcode)], cpu_gpr[rB(ctx->opcode)]);
26d67362
AJ
2213 if (unlikely(Rc(ctx->opcode) != 0))
2214 gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
2215}
99e300ef 2216
54623277 2217/* srawi & srawi. */
99e300ef 2218static void gen_srawi(DisasContext *ctx)
79aceca5 2219{
26d67362 2220 int sh = SH(ctx->opcode);
ba4af3e4
RH
2221 TCGv dst = cpu_gpr[rA(ctx->opcode)];
2222 TCGv src = cpu_gpr[rS(ctx->opcode)];
2223 if (sh == 0) {
34a0fad1 2224 tcg_gen_ext32s_tl(dst, src);
da91a00f 2225 tcg_gen_movi_tl(cpu_ca, 0);
26d67362 2226 } else {
ba4af3e4
RH
2227 TCGv t0;
2228 tcg_gen_ext32s_tl(dst, src);
2229 tcg_gen_andi_tl(cpu_ca, dst, (1ULL << sh) - 1);
2230 t0 = tcg_temp_new();
2231 tcg_gen_sari_tl(t0, dst, TARGET_LONG_BITS - 1);
2232 tcg_gen_and_tl(cpu_ca, cpu_ca, t0);
2233 tcg_temp_free(t0);
2234 tcg_gen_setcondi_tl(TCG_COND_NE, cpu_ca, cpu_ca, 0);
2235 tcg_gen_sari_tl(dst, dst, sh);
2236 }
2237 if (unlikely(Rc(ctx->opcode) != 0)) {
2238 gen_set_Rc0(ctx, dst);
d9bce9d9 2239 }
79aceca5 2240}
99e300ef 2241
54623277 2242/* srw & srw. */
99e300ef 2243static void gen_srw(DisasContext *ctx)
26d67362 2244{
fea0c503 2245 TCGv t0, t1;
d9bce9d9 2246
7fd6bf7d
AJ
2247 t0 = tcg_temp_new();
2248 /* AND rS with a mask that is 0 when rB >= 0x20 */
2249#if defined(TARGET_PPC64)
2250 tcg_gen_shli_tl(t0, cpu_gpr[rB(ctx->opcode)], 0x3a);
2251 tcg_gen_sari_tl(t0, t0, 0x3f);
2252#else
2253 tcg_gen_shli_tl(t0, cpu_gpr[rB(ctx->opcode)], 0x1a);
2254 tcg_gen_sari_tl(t0, t0, 0x1f);
2255#endif
2256 tcg_gen_andc_tl(t0, cpu_gpr[rS(ctx->opcode)], t0);
2257 tcg_gen_ext32u_tl(t0, t0);
a7812ae4 2258 t1 = tcg_temp_new();
7fd6bf7d
AJ
2259 tcg_gen_andi_tl(t1, cpu_gpr[rB(ctx->opcode)], 0x1f);
2260 tcg_gen_shr_tl(cpu_gpr[rA(ctx->opcode)], t0, t1);
fea0c503 2261 tcg_temp_free(t1);
fea0c503 2262 tcg_temp_free(t0);
26d67362
AJ
2263 if (unlikely(Rc(ctx->opcode) != 0))
2264 gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
2265}
54623277 2266
d9bce9d9
JM
2267#if defined(TARGET_PPC64)
2268/* sld & sld. */
99e300ef 2269static void gen_sld(DisasContext *ctx)
26d67362 2270{
7fd6bf7d 2271 TCGv t0, t1;
26d67362 2272
7fd6bf7d
AJ
2273 t0 = tcg_temp_new();
2274 /* AND rS with a mask that is 0 when rB >= 0x40 */
2275 tcg_gen_shli_tl(t0, cpu_gpr[rB(ctx->opcode)], 0x39);
2276 tcg_gen_sari_tl(t0, t0, 0x3f);
2277 tcg_gen_andc_tl(t0, cpu_gpr[rS(ctx->opcode)], t0);
2278 t1 = tcg_temp_new();
2279 tcg_gen_andi_tl(t1, cpu_gpr[rB(ctx->opcode)], 0x3f);
2280 tcg_gen_shl_tl(cpu_gpr[rA(ctx->opcode)], t0, t1);
2281 tcg_temp_free(t1);
fea0c503 2282 tcg_temp_free(t0);
26d67362
AJ
2283 if (unlikely(Rc(ctx->opcode) != 0))
2284 gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
2285}
99e300ef 2286
54623277 2287/* srad & srad. */
99e300ef 2288static void gen_srad(DisasContext *ctx)
26d67362 2289{
d15f74fb 2290 gen_helper_srad(cpu_gpr[rA(ctx->opcode)], cpu_env,
a7812ae4 2291 cpu_gpr[rS(ctx->opcode)], cpu_gpr[rB(ctx->opcode)]);
26d67362
AJ
2292 if (unlikely(Rc(ctx->opcode) != 0))
2293 gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
2294}
d9bce9d9 2295/* sradi & sradi. */
636aa200 2296static inline void gen_sradi(DisasContext *ctx, int n)
d9bce9d9 2297{
26d67362 2298 int sh = SH(ctx->opcode) + (n << 5);
ba4af3e4
RH
2299 TCGv dst = cpu_gpr[rA(ctx->opcode)];
2300 TCGv src = cpu_gpr[rS(ctx->opcode)];
2301 if (sh == 0) {
2302 tcg_gen_mov_tl(dst, src);
da91a00f 2303 tcg_gen_movi_tl(cpu_ca, 0);
26d67362 2304 } else {
ba4af3e4
RH
2305 TCGv t0;
2306 tcg_gen_andi_tl(cpu_ca, src, (1ULL << sh) - 1);
2307 t0 = tcg_temp_new();
2308 tcg_gen_sari_tl(t0, src, TARGET_LONG_BITS - 1);
2309 tcg_gen_and_tl(cpu_ca, cpu_ca, t0);
2310 tcg_temp_free(t0);
2311 tcg_gen_setcondi_tl(TCG_COND_NE, cpu_ca, cpu_ca, 0);
2312 tcg_gen_sari_tl(dst, src, sh);
2313 }
2314 if (unlikely(Rc(ctx->opcode) != 0)) {
2315 gen_set_Rc0(ctx, dst);
d9bce9d9 2316 }
d9bce9d9 2317}
e8eaa2c0
BS
2318
2319static void gen_sradi0(DisasContext *ctx)
d9bce9d9
JM
2320{
2321 gen_sradi(ctx, 0);
2322}
e8eaa2c0
BS
2323
2324static void gen_sradi1(DisasContext *ctx)
d9bce9d9
JM
2325{
2326 gen_sradi(ctx, 1);
2327}
99e300ef 2328
54623277 2329/* srd & srd. */
99e300ef 2330static void gen_srd(DisasContext *ctx)
26d67362 2331{
7fd6bf7d 2332 TCGv t0, t1;
26d67362 2333
7fd6bf7d
AJ
2334 t0 = tcg_temp_new();
2335 /* AND rS with a mask that is 0 when rB >= 0x40 */
2336 tcg_gen_shli_tl(t0, cpu_gpr[rB(ctx->opcode)], 0x39);
2337 tcg_gen_sari_tl(t0, t0, 0x3f);
2338 tcg_gen_andc_tl(t0, cpu_gpr[rS(ctx->opcode)], t0);
2339 t1 = tcg_temp_new();
2340 tcg_gen_andi_tl(t1, cpu_gpr[rB(ctx->opcode)], 0x3f);
2341 tcg_gen_shr_tl(cpu_gpr[rA(ctx->opcode)], t0, t1);
2342 tcg_temp_free(t1);
fea0c503 2343 tcg_temp_free(t0);
26d67362
AJ
2344 if (unlikely(Rc(ctx->opcode) != 0))
2345 gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
2346}
d9bce9d9 2347#endif
79aceca5 2348
76a66253
JM
2349/*** Addressing modes ***/
2350/* Register indirect with immediate index : EA = (rA|0) + SIMM */
636aa200
BS
2351static inline void gen_addr_imm_index(DisasContext *ctx, TCGv EA,
2352 target_long maskl)
76a66253
JM
2353{
2354 target_long simm = SIMM(ctx->opcode);
2355
be147d08 2356 simm &= ~maskl;
76db3ba4 2357 if (rA(ctx->opcode) == 0) {
c791fe84
RH
2358 if (NARROW_MODE(ctx)) {
2359 simm = (uint32_t)simm;
2360 }
e2be8d8d 2361 tcg_gen_movi_tl(EA, simm);
76db3ba4 2362 } else if (likely(simm != 0)) {
e2be8d8d 2363 tcg_gen_addi_tl(EA, cpu_gpr[rA(ctx->opcode)], simm);
c791fe84 2364 if (NARROW_MODE(ctx)) {
76db3ba4
AJ
2365 tcg_gen_ext32u_tl(EA, EA);
2366 }
76db3ba4 2367 } else {
c791fe84 2368 if (NARROW_MODE(ctx)) {
76db3ba4 2369 tcg_gen_ext32u_tl(EA, cpu_gpr[rA(ctx->opcode)]);
c791fe84
RH
2370 } else {
2371 tcg_gen_mov_tl(EA, cpu_gpr[rA(ctx->opcode)]);
2372 }
76db3ba4 2373 }
76a66253
JM
2374}
2375
636aa200 2376static inline void gen_addr_reg_index(DisasContext *ctx, TCGv EA)
76a66253 2377{
76db3ba4 2378 if (rA(ctx->opcode) == 0) {
c791fe84 2379 if (NARROW_MODE(ctx)) {
76db3ba4 2380 tcg_gen_ext32u_tl(EA, cpu_gpr[rB(ctx->opcode)]);
c791fe84
RH
2381 } else {
2382 tcg_gen_mov_tl(EA, cpu_gpr[rB(ctx->opcode)]);
2383 }
76db3ba4 2384 } else {
e2be8d8d 2385 tcg_gen_add_tl(EA, cpu_gpr[rA(ctx->opcode)], cpu_gpr[rB(ctx->opcode)]);
c791fe84 2386 if (NARROW_MODE(ctx)) {
76db3ba4
AJ
2387 tcg_gen_ext32u_tl(EA, EA);
2388 }
76db3ba4 2389 }
76a66253
JM
2390}
2391
636aa200 2392static inline void gen_addr_register(DisasContext *ctx, TCGv EA)
76a66253 2393{
76db3ba4 2394 if (rA(ctx->opcode) == 0) {
e2be8d8d 2395 tcg_gen_movi_tl(EA, 0);
c791fe84
RH
2396 } else if (NARROW_MODE(ctx)) {
2397 tcg_gen_ext32u_tl(EA, cpu_gpr[rA(ctx->opcode)]);
76db3ba4 2398 } else {
c791fe84 2399 tcg_gen_mov_tl(EA, cpu_gpr[rA(ctx->opcode)]);
76db3ba4
AJ
2400 }
2401}
2402
636aa200
BS
2403static inline void gen_addr_add(DisasContext *ctx, TCGv ret, TCGv arg1,
2404 target_long val)
76db3ba4
AJ
2405{
2406 tcg_gen_addi_tl(ret, arg1, val);
c791fe84 2407 if (NARROW_MODE(ctx)) {
76db3ba4
AJ
2408 tcg_gen_ext32u_tl(ret, ret);
2409 }
76a66253
JM
2410}
2411
636aa200 2412static inline void gen_check_align(DisasContext *ctx, TCGv EA, int mask)
cf360a32 2413{
42a268c2 2414 TCGLabel *l1 = gen_new_label();
cf360a32
AJ
2415 TCGv t0 = tcg_temp_new();
2416 TCGv_i32 t1, t2;
cf360a32
AJ
2417 tcg_gen_andi_tl(t0, EA, mask);
2418 tcg_gen_brcondi_tl(TCG_COND_EQ, t0, 0, l1);
2419 t1 = tcg_const_i32(POWERPC_EXCP_ALIGN);
3433b732 2420 t2 = tcg_const_i32(ctx->opcode & 0x03FF0000);
573708e3 2421 gen_update_nip(ctx, ctx->nip - 4);
e5f17ac6 2422 gen_helper_raise_exception_err(cpu_env, t1, t2);
cf360a32
AJ
2423 tcg_temp_free_i32(t1);
2424 tcg_temp_free_i32(t2);
2425 gen_set_label(l1);
2426 tcg_temp_free(t0);
2427}
2428
7863667f 2429/*** Integer load ***/
636aa200 2430static inline void gen_qemu_ld8u(DisasContext *ctx, TCGv arg1, TCGv arg2)
76db3ba4
AJ
2431{
2432 tcg_gen_qemu_ld8u(arg1, arg2, ctx->mem_idx);
2433}
2434
636aa200 2435static inline void gen_qemu_ld16u(DisasContext *ctx, TCGv arg1, TCGv arg2)
76db3ba4 2436{
e22c357b
DK
2437 TCGMemOp op = MO_UW | ctx->default_tcg_memop_mask;
2438 tcg_gen_qemu_ld_tl(arg1, arg2, ctx->mem_idx, op);
b61f2753
AJ
2439}
2440
636aa200 2441static inline void gen_qemu_ld16s(DisasContext *ctx, TCGv arg1, TCGv arg2)
b61f2753 2442{
e22c357b
DK
2443 TCGMemOp op = MO_SW | ctx->default_tcg_memop_mask;
2444 tcg_gen_qemu_ld_tl(arg1, arg2, ctx->mem_idx, op);
b61f2753
AJ
2445}
2446
636aa200 2447static inline void gen_qemu_ld32u(DisasContext *ctx, TCGv arg1, TCGv arg2)
b61f2753 2448{
e22c357b
DK
2449 TCGMemOp op = MO_UL | ctx->default_tcg_memop_mask;
2450 tcg_gen_qemu_ld_tl(arg1, arg2, ctx->mem_idx, op);
b61f2753
AJ
2451}
2452
f976b09e
AG
2453static void gen_qemu_ld32u_i64(DisasContext *ctx, TCGv_i64 val, TCGv addr)
2454{
2455 TCGv tmp = tcg_temp_new();
2456 gen_qemu_ld32u(ctx, tmp, addr);
2457 tcg_gen_extu_tl_i64(val, tmp);
2458 tcg_temp_free(tmp);
2459}
2460
636aa200 2461static inline void gen_qemu_ld32s(DisasContext *ctx, TCGv arg1, TCGv arg2)
b61f2753 2462{
e22c357b
DK
2463 TCGMemOp op = MO_SL | ctx->default_tcg_memop_mask;
2464 tcg_gen_qemu_ld_tl(arg1, arg2, ctx->mem_idx, op);
b61f2753
AJ
2465}
2466
cac7f0ba
TM
2467static void gen_qemu_ld32s_i64(DisasContext *ctx, TCGv_i64 val, TCGv addr)
2468{
2469 TCGv tmp = tcg_temp_new();
2470 gen_qemu_ld32s(ctx, tmp, addr);
2471 tcg_gen_ext_tl_i64(val, tmp);
2472 tcg_temp_free(tmp);
2473}
2474
636aa200 2475static inline void gen_qemu_ld64(DisasContext *ctx, TCGv_i64 arg1, TCGv arg2)
b61f2753 2476{
e22c357b
DK
2477 TCGMemOp op = MO_Q | ctx->default_tcg_memop_mask;
2478 tcg_gen_qemu_ld_i64(arg1, arg2, ctx->mem_idx, op);
b61f2753
AJ
2479}
2480
636aa200 2481static inline void gen_qemu_st8(DisasContext *ctx, TCGv arg1, TCGv arg2)
b61f2753 2482{
76db3ba4 2483 tcg_gen_qemu_st8(arg1, arg2, ctx->mem_idx);
b61f2753
AJ
2484}
2485
636aa200 2486static inline void gen_qemu_st16(DisasContext *ctx, TCGv arg1, TCGv arg2)
b61f2753 2487{
e22c357b
DK
2488 TCGMemOp op = MO_UW | ctx->default_tcg_memop_mask;
2489 tcg_gen_qemu_st_tl(arg1, arg2, ctx->mem_idx, op);
b61f2753
AJ
2490}
2491
636aa200 2492static inline void gen_qemu_st32(DisasContext *ctx, TCGv arg1, TCGv arg2)
b61f2753 2493{
e22c357b
DK
2494 TCGMemOp op = MO_UL | ctx->default_tcg_memop_mask;
2495 tcg_gen_qemu_st_tl(arg1, arg2, ctx->mem_idx, op);
b61f2753
AJ
2496}
2497
f976b09e
AG
2498static void gen_qemu_st32_i64(DisasContext *ctx, TCGv_i64 val, TCGv addr)
2499{
2500 TCGv tmp = tcg_temp_new();
2501 tcg_gen_trunc_i64_tl(tmp, val);
2502 gen_qemu_st32(ctx, tmp, addr);
2503 tcg_temp_free(tmp);
2504}
2505
636aa200 2506static inline void gen_qemu_st64(DisasContext *ctx, TCGv_i64 arg1, TCGv arg2)
b61f2753 2507{
e22c357b
DK
2508 TCGMemOp op = MO_Q | ctx->default_tcg_memop_mask;
2509 tcg_gen_qemu_st_i64(arg1, arg2, ctx->mem_idx, op);
b61f2753
AJ
2510}
2511
0c8aacd4 2512#define GEN_LD(name, ldop, opc, type) \
99e300ef 2513static void glue(gen_, name)(DisasContext *ctx) \
79aceca5 2514{ \
76db3ba4
AJ
2515 TCGv EA; \
2516 gen_set_access_type(ctx, ACCESS_INT); \
2517 EA = tcg_temp_new(); \
2518 gen_addr_imm_index(ctx, EA, 0); \
2519 gen_qemu_##ldop(ctx, cpu_gpr[rD(ctx->opcode)], EA); \
b61f2753 2520 tcg_temp_free(EA); \
79aceca5
FB
2521}
2522
0c8aacd4 2523#define GEN_LDU(name, ldop, opc, type) \
99e300ef 2524static void glue(gen_, name##u)(DisasContext *ctx) \
79aceca5 2525{ \
b61f2753 2526 TCGv EA; \
76a66253
JM
2527 if (unlikely(rA(ctx->opcode) == 0 || \
2528 rA(ctx->opcode) == rD(ctx->opcode))) { \
e06fcd75 2529 gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL); \
9fddaa0c 2530 return; \
9a64fbe4 2531 } \
76db3ba4 2532 gen_set_access_type(ctx, ACCESS_INT); \
0c8aacd4 2533 EA = tcg_temp_new(); \
9d53c753 2534 if (type == PPC_64B) \
76db3ba4 2535 gen_addr_imm_index(ctx, EA, 0x03); \
9d53c753 2536 else \
76db3ba4
AJ
2537 gen_addr_imm_index(ctx, EA, 0); \
2538 gen_qemu_##ldop(ctx, cpu_gpr[rD(ctx->opcode)], EA); \
b61f2753
AJ
2539 tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], EA); \
2540 tcg_temp_free(EA); \
79aceca5
FB
2541}
2542
0c8aacd4 2543#define GEN_LDUX(name, ldop, opc2, opc3, type) \
99e300ef 2544static void glue(gen_, name##ux)(DisasContext *ctx) \
79aceca5 2545{ \
b61f2753 2546 TCGv EA; \
76a66253
JM
2547 if (unlikely(rA(ctx->opcode) == 0 || \
2548 rA(ctx->opcode) == rD(ctx->opcode))) { \
e06fcd75 2549 gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL); \
9fddaa0c 2550 return; \
9a64fbe4 2551 } \
76db3ba4 2552 gen_set_access_type(ctx, ACCESS_INT); \
0c8aacd4 2553 EA = tcg_temp_new(); \
76db3ba4
AJ
2554 gen_addr_reg_index(ctx, EA); \
2555 gen_qemu_##ldop(ctx, cpu_gpr[rD(ctx->opcode)], EA); \
b61f2753
AJ
2556 tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], EA); \
2557 tcg_temp_free(EA); \
79aceca5
FB
2558}
2559
b7815375 2560#define GEN_LDX_E(name, ldop, opc2, opc3, type, type2, chk) \
99e300ef 2561static void glue(gen_, name##x)(DisasContext *ctx) \
79aceca5 2562{ \
76db3ba4 2563 TCGv EA; \
b7815375 2564 chk; \
76db3ba4
AJ
2565 gen_set_access_type(ctx, ACCESS_INT); \
2566 EA = tcg_temp_new(); \
2567 gen_addr_reg_index(ctx, EA); \
2568 gen_qemu_##ldop(ctx, cpu_gpr[rD(ctx->opcode)], EA); \
b61f2753 2569 tcg_temp_free(EA); \
79aceca5 2570}
b7815375 2571
cd6e9320 2572#define GEN_LDX(name, ldop, opc2, opc3, type) \
b7815375
BH
2573 GEN_LDX_E(name, ldop, opc2, opc3, type, PPC_NONE, CHK_NONE)
2574
2575#define GEN_LDX_HVRM(name, ldop, opc2, opc3, type) \
2576 GEN_LDX_E(name, ldop, opc2, opc3, type, PPC_NONE, CHK_HVRM)
79aceca5 2577
0c8aacd4
AJ
2578#define GEN_LDS(name, ldop, op, type) \
2579GEN_LD(name, ldop, op | 0x20, type); \
2580GEN_LDU(name, ldop, op | 0x21, type); \
2581GEN_LDUX(name, ldop, 0x17, op | 0x01, type); \
2582GEN_LDX(name, ldop, 0x17, op | 0x00, type)
79aceca5
FB
2583
2584/* lbz lbzu lbzux lbzx */
0c8aacd4 2585GEN_LDS(lbz, ld8u, 0x02, PPC_INTEGER);
79aceca5 2586/* lha lhau lhaux lhax */
0c8aacd4 2587GEN_LDS(lha, ld16s, 0x0A, PPC_INTEGER);
79aceca5 2588/* lhz lhzu lhzux lhzx */
0c8aacd4 2589GEN_LDS(lhz, ld16u, 0x08, PPC_INTEGER);
79aceca5 2590/* lwz lwzu lwzux lwzx */
0c8aacd4 2591GEN_LDS(lwz, ld32u, 0x00, PPC_INTEGER);
d9bce9d9 2592#if defined(TARGET_PPC64)
d9bce9d9 2593/* lwaux */
0c8aacd4 2594GEN_LDUX(lwa, ld32s, 0x15, 0x0B, PPC_64B);
d9bce9d9 2595/* lwax */
0c8aacd4 2596GEN_LDX(lwa, ld32s, 0x15, 0x0A, PPC_64B);
d9bce9d9 2597/* ldux */
0c8aacd4 2598GEN_LDUX(ld, ld64, 0x15, 0x01, PPC_64B);
d9bce9d9 2599/* ldx */
0c8aacd4 2600GEN_LDX(ld, ld64, 0x15, 0x00, PPC_64B);
99e300ef 2601
b7815375
BH
2602/* CI load/store variants */
2603GEN_LDX_HVRM(ldcix, ld64, 0x15, 0x1b, PPC_CILDST)
2604GEN_LDX_HVRM(lwzcix, ld32u, 0x15, 0x15, PPC_CILDST)
2605GEN_LDX_HVRM(lhzcix, ld16u, 0x15, 0x19, PPC_CILDST)
2606GEN_LDX_HVRM(lbzcix, ld8u, 0x15, 0x1a, PPC_CILDST)
2607
99e300ef 2608static void gen_ld(DisasContext *ctx)
d9bce9d9 2609{
b61f2753 2610 TCGv EA;
d9bce9d9
JM
2611 if (Rc(ctx->opcode)) {
2612 if (unlikely(rA(ctx->opcode) == 0 ||
2613 rA(ctx->opcode) == rD(ctx->opcode))) {
e06fcd75 2614 gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL);
d9bce9d9
JM
2615 return;
2616 }
2617 }
76db3ba4 2618 gen_set_access_type(ctx, ACCESS_INT);
a7812ae4 2619 EA = tcg_temp_new();
76db3ba4 2620 gen_addr_imm_index(ctx, EA, 0x03);
d9bce9d9
JM
2621 if (ctx->opcode & 0x02) {
2622 /* lwa (lwau is undefined) */
76db3ba4 2623 gen_qemu_ld32s(ctx, cpu_gpr[rD(ctx->opcode)], EA);
d9bce9d9
JM
2624 } else {
2625 /* ld - ldu */
76db3ba4 2626 gen_qemu_ld64(ctx, cpu_gpr[rD(ctx->opcode)], EA);
d9bce9d9 2627 }
d9bce9d9 2628 if (Rc(ctx->opcode))
b61f2753
AJ
2629 tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], EA);
2630 tcg_temp_free(EA);
d9bce9d9 2631}
99e300ef 2632
54623277 2633/* lq */
99e300ef 2634static void gen_lq(DisasContext *ctx)
be147d08 2635{
be147d08 2636 int ra, rd;
b61f2753 2637 TCGv EA;
be147d08 2638
e0498daa
TM
2639 /* lq is a legal user mode instruction starting in ISA 2.07 */
2640 bool legal_in_user_mode = (ctx->insns_flags2 & PPC2_LSQ_ISA207) != 0;
2641 bool le_is_supported = (ctx->insns_flags2 & PPC2_LSQ_ISA207) != 0;
2642
c47493f2 2643 if (!legal_in_user_mode && ctx->pr) {
9b2fadda 2644 gen_priv_exception(ctx, POWERPC_EXCP_PRIV_OPC);
be147d08
JM
2645 return;
2646 }
e0498daa
TM
2647
2648 if (!le_is_supported && ctx->le_mode) {
2649 gen_exception_err(ctx, POWERPC_EXCP_ALIGN, POWERPC_EXCP_ALIGN_LE);
2650 return;
2651 }
2652
be147d08
JM
2653 ra = rA(ctx->opcode);
2654 rd = rD(ctx->opcode);
2655 if (unlikely((rd & 1) || rd == ra)) {
e06fcd75 2656 gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL);
be147d08
JM
2657 return;
2658 }
e0498daa 2659
76db3ba4 2660 gen_set_access_type(ctx, ACCESS_INT);
a7812ae4 2661 EA = tcg_temp_new();
76db3ba4 2662 gen_addr_imm_index(ctx, EA, 0x0F);
e0498daa 2663
e22c357b
DK
2664 /* We only need to swap high and low halves. gen_qemu_ld64 does necessary
2665 64-bit byteswap already. */
e0498daa
TM
2666 if (unlikely(ctx->le_mode)) {
2667 gen_qemu_ld64(ctx, cpu_gpr[rd+1], EA);
2668 gen_addr_add(ctx, EA, EA, 8);
2669 gen_qemu_ld64(ctx, cpu_gpr[rd], EA);
2670 } else {
2671 gen_qemu_ld64(ctx, cpu_gpr[rd], EA);
2672 gen_addr_add(ctx, EA, EA, 8);
2673 gen_qemu_ld64(ctx, cpu_gpr[rd+1], EA);
2674 }
b61f2753 2675 tcg_temp_free(EA);
be147d08 2676}
d9bce9d9 2677#endif
79aceca5
FB
2678
2679/*** Integer store ***/
0c8aacd4 2680#define GEN_ST(name, stop, opc, type) \
99e300ef 2681static void glue(gen_, name)(DisasContext *ctx) \
79aceca5 2682{ \
76db3ba4
AJ
2683 TCGv EA; \
2684 gen_set_access_type(ctx, ACCESS_INT); \
2685 EA = tcg_temp_new(); \
2686 gen_addr_imm_index(ctx, EA, 0); \
2687 gen_qemu_##stop(ctx, cpu_gpr[rS(ctx->opcode)], EA); \
b61f2753 2688 tcg_temp_free(EA); \
79aceca5
FB
2689}
2690
0c8aacd4 2691#define GEN_STU(name, stop, opc, type) \
99e300ef 2692static void glue(gen_, stop##u)(DisasContext *ctx) \
79aceca5 2693{ \
b61f2753 2694 TCGv EA; \
76a66253 2695 if (unlikely(rA(ctx->opcode) == 0)) { \
e06fcd75 2696 gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL); \
9fddaa0c 2697 return; \
9a64fbe4 2698 } \
76db3ba4 2699 gen_set_access_type(ctx, ACCESS_INT); \
0c8aacd4 2700 EA = tcg_temp_new(); \
9d53c753 2701 if (type == PPC_64B) \
76db3ba4 2702 gen_addr_imm_index(ctx, EA, 0x03); \
9d53c753 2703 else \
76db3ba4
AJ
2704 gen_addr_imm_index(ctx, EA, 0); \
2705 gen_qemu_##stop(ctx, cpu_gpr[rS(ctx->opcode)], EA); \
b61f2753
AJ
2706 tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], EA); \
2707 tcg_temp_free(EA); \
79aceca5
FB
2708}
2709
0c8aacd4 2710#define GEN_STUX(name, stop, opc2, opc3, type) \
99e300ef 2711static void glue(gen_, name##ux)(DisasContext *ctx) \
79aceca5 2712{ \
b61f2753 2713 TCGv EA; \
76a66253 2714 if (unlikely(rA(ctx->opcode) == 0)) { \
e06fcd75 2715 gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL); \
9fddaa0c 2716 return; \
9a64fbe4 2717 } \
76db3ba4 2718 gen_set_access_type(ctx, ACCESS_INT); \
0c8aacd4 2719 EA = tcg_temp_new(); \
76db3ba4
AJ
2720 gen_addr_reg_index(ctx, EA); \
2721 gen_qemu_##stop(ctx, cpu_gpr[rS(ctx->opcode)], EA); \
b61f2753
AJ
2722 tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], EA); \
2723 tcg_temp_free(EA); \
79aceca5
FB
2724}
2725
b7815375 2726#define GEN_STX_E(name, stop, opc2, opc3, type, type2, chk) \
cd6e9320 2727static void glue(gen_, name##x)(DisasContext *ctx) \
79aceca5 2728{ \
76db3ba4 2729 TCGv EA; \
b7815375 2730 chk; \
76db3ba4
AJ
2731 gen_set_access_type(ctx, ACCESS_INT); \
2732 EA = tcg_temp_new(); \
2733 gen_addr_reg_index(ctx, EA); \
2734 gen_qemu_##stop(ctx, cpu_gpr[rS(ctx->opcode)], EA); \
b61f2753 2735 tcg_temp_free(EA); \
79aceca5 2736}
cd6e9320 2737#define GEN_STX(name, stop, opc2, opc3, type) \
b7815375
BH
2738 GEN_STX_E(name, stop, opc2, opc3, type, PPC_NONE, CHK_NONE)
2739
2740#define GEN_STX_HVRM(name, stop, opc2, opc3, type) \
2741 GEN_STX_E(name, stop, opc2, opc3, type, PPC_NONE, CHK_HVRM)
79aceca5 2742
0c8aacd4
AJ
2743#define GEN_STS(name, stop, op, type) \
2744GEN_ST(name, stop, op | 0x20, type); \
2745GEN_STU(name, stop, op | 0x21, type); \
2746GEN_STUX(name, stop, 0x17, op | 0x01, type); \
2747GEN_STX(name, stop, 0x17, op | 0x00, type)
79aceca5
FB
2748
2749/* stb stbu stbux stbx */
0c8aacd4 2750GEN_STS(stb, st8, 0x06, PPC_INTEGER);
79aceca5 2751/* sth sthu sthux sthx */
0c8aacd4 2752GEN_STS(sth, st16, 0x0C, PPC_INTEGER);
79aceca5 2753/* stw stwu stwux stwx */
0c8aacd4 2754GEN_STS(stw, st32, 0x04, PPC_INTEGER);
d9bce9d9 2755#if defined(TARGET_PPC64)
0c8aacd4
AJ
2756GEN_STUX(std, st64, 0x15, 0x05, PPC_64B);
2757GEN_STX(std, st64, 0x15, 0x04, PPC_64B);
b7815375
BH
2758GEN_STX_HVRM(stdcix, st64, 0x15, 0x1f, PPC_CILDST)
2759GEN_STX_HVRM(stwcix, st32, 0x15, 0x1c, PPC_CILDST)
2760GEN_STX_HVRM(sthcix, st16, 0x15, 0x1d, PPC_CILDST)
2761GEN_STX_HVRM(stbcix, st8, 0x15, 0x1e, PPC_CILDST)
99e300ef
BS
2762
2763static void gen_std(DisasContext *ctx)
d9bce9d9 2764{
be147d08 2765 int rs;
b61f2753 2766 TCGv EA;
be147d08
JM
2767
2768 rs = rS(ctx->opcode);
84cab1e2 2769 if ((ctx->opcode & 0x3) == 0x2) { /* stq */
84cab1e2
TM
2770 bool legal_in_user_mode = (ctx->insns_flags2 & PPC2_LSQ_ISA207) != 0;
2771 bool le_is_supported = (ctx->insns_flags2 & PPC2_LSQ_ISA207) != 0;
2772
dfdd3e43
BH
2773 if (!(ctx->insns_flags & PPC_64BX)) {
2774 gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL);
2775 }
2776
c47493f2 2777 if (!legal_in_user_mode && ctx->pr) {
9b2fadda 2778 gen_priv_exception(ctx, POWERPC_EXCP_PRIV_OPC);
be147d08
JM
2779 return;
2780 }
84cab1e2
TM
2781
2782 if (!le_is_supported && ctx->le_mode) {
2783 gen_exception_err(ctx, POWERPC_EXCP_ALIGN, POWERPC_EXCP_ALIGN_LE);
d9bce9d9
JM
2784 return;
2785 }
84cab1e2
TM
2786
2787 if (unlikely(rs & 1)) {
2788 gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL);
be147d08
JM
2789 return;
2790 }
76db3ba4 2791 gen_set_access_type(ctx, ACCESS_INT);
a7812ae4 2792 EA = tcg_temp_new();
76db3ba4 2793 gen_addr_imm_index(ctx, EA, 0x03);
84cab1e2 2794
e22c357b
DK
2795 /* We only need to swap high and low halves. gen_qemu_st64 does
2796 necessary 64-bit byteswap already. */
84cab1e2
TM
2797 if (unlikely(ctx->le_mode)) {
2798 gen_qemu_st64(ctx, cpu_gpr[rs+1], EA);
2799 gen_addr_add(ctx, EA, EA, 8);
2800 gen_qemu_st64(ctx, cpu_gpr[rs], EA);
2801 } else {
2802 gen_qemu_st64(ctx, cpu_gpr[rs], EA);
2803 gen_addr_add(ctx, EA, EA, 8);
2804 gen_qemu_st64(ctx, cpu_gpr[rs+1], EA);
2805 }
b61f2753 2806 tcg_temp_free(EA);
be147d08 2807 } else {
84cab1e2 2808 /* std / stdu*/
be147d08
JM
2809 if (Rc(ctx->opcode)) {
2810 if (unlikely(rA(ctx->opcode) == 0)) {
e06fcd75 2811 gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL);
be147d08
JM
2812 return;
2813 }
2814 }
76db3ba4 2815 gen_set_access_type(ctx, ACCESS_INT);
a7812ae4 2816 EA = tcg_temp_new();
76db3ba4
AJ
2817 gen_addr_imm_index(ctx, EA, 0x03);
2818 gen_qemu_st64(ctx, cpu_gpr[rs], EA);
be147d08 2819 if (Rc(ctx->opcode))
b61f2753
AJ
2820 tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], EA);
2821 tcg_temp_free(EA);
d9bce9d9 2822 }
d9bce9d9
JM
2823}
2824#endif
79aceca5 2825/*** Integer load and store with byte reverse ***/
e22c357b 2826
79aceca5 2827/* lhbrx */
86178a57 2828static inline void gen_qemu_ld16ur(DisasContext *ctx, TCGv arg1, TCGv arg2)
b61f2753 2829{
e22c357b
DK
2830 TCGMemOp op = MO_UW | (ctx->default_tcg_memop_mask ^ MO_BSWAP);
2831 tcg_gen_qemu_ld_tl(arg1, arg2, ctx->mem_idx, op);
b61f2753 2832}
0c8aacd4 2833GEN_LDX(lhbr, ld16ur, 0x16, 0x18, PPC_INTEGER);
b61f2753 2834
79aceca5 2835/* lwbrx */
86178a57 2836static inline void gen_qemu_ld32ur(DisasContext *ctx, TCGv arg1, TCGv arg2)
b61f2753 2837{
e22c357b
DK
2838 TCGMemOp op = MO_UL | (ctx->default_tcg_memop_mask ^ MO_BSWAP);
2839 tcg_gen_qemu_ld_tl(arg1, arg2, ctx->mem_idx, op);
b61f2753 2840}
0c8aacd4 2841GEN_LDX(lwbr, ld32ur, 0x16, 0x10, PPC_INTEGER);
b61f2753 2842
cd6e9320
TH
2843#if defined(TARGET_PPC64)
2844/* ldbrx */
2845static inline void gen_qemu_ld64ur(DisasContext *ctx, TCGv arg1, TCGv arg2)
2846{
e22c357b
DK
2847 TCGMemOp op = MO_Q | (ctx->default_tcg_memop_mask ^ MO_BSWAP);
2848 tcg_gen_qemu_ld_i64(arg1, arg2, ctx->mem_idx, op);
cd6e9320 2849}
b7815375 2850GEN_LDX_E(ldbr, ld64ur, 0x14, 0x10, PPC_NONE, PPC2_DBRX, CHK_NONE);
cd6e9320
TH
2851#endif /* TARGET_PPC64 */
2852
79aceca5 2853/* sthbrx */
86178a57 2854static inline void gen_qemu_st16r(DisasContext *ctx, TCGv arg1, TCGv arg2)
b61f2753 2855{
e22c357b
DK
2856 TCGMemOp op = MO_UW | (ctx->default_tcg_memop_mask ^ MO_BSWAP);
2857 tcg_gen_qemu_st_tl(arg1, arg2, ctx->mem_idx, op);
b61f2753 2858}
0c8aacd4 2859GEN_STX(sthbr, st16r, 0x16, 0x1C, PPC_INTEGER);
b61f2753 2860
79aceca5 2861/* stwbrx */
86178a57 2862static inline void gen_qemu_st32r(DisasContext *ctx, TCGv arg1, TCGv arg2)
b61f2753 2863{
e22c357b
DK
2864 TCGMemOp op = MO_UL | (ctx->default_tcg_memop_mask ^ MO_BSWAP);
2865 tcg_gen_qemu_st_tl(arg1, arg2, ctx->mem_idx, op);
b61f2753 2866}
0c8aacd4 2867GEN_STX(stwbr, st32r, 0x16, 0x14, PPC_INTEGER);
79aceca5 2868
cd6e9320
TH
2869#if defined(TARGET_PPC64)
2870/* stdbrx */
2871static inline void gen_qemu_st64r(DisasContext *ctx, TCGv arg1, TCGv arg2)
2872{
e22c357b
DK
2873 TCGMemOp op = MO_Q | (ctx->default_tcg_memop_mask ^ MO_BSWAP);
2874 tcg_gen_qemu_st_i64(arg1, arg2, ctx->mem_idx, op);
cd6e9320 2875}
b7815375 2876GEN_STX_E(stdbr, st64r, 0x14, 0x14, PPC_NONE, PPC2_DBRX, CHK_NONE);
cd6e9320
TH
2877#endif /* TARGET_PPC64 */
2878
79aceca5 2879/*** Integer load and store multiple ***/
99e300ef 2880
54623277 2881/* lmw */
99e300ef 2882static void gen_lmw(DisasContext *ctx)
79aceca5 2883{
76db3ba4
AJ
2884 TCGv t0;
2885 TCGv_i32 t1;
2886 gen_set_access_type(ctx, ACCESS_INT);
76db3ba4
AJ
2887 t0 = tcg_temp_new();
2888 t1 = tcg_const_i32(rD(ctx->opcode));
2889 gen_addr_imm_index(ctx, t0, 0);
2f5a189c 2890 gen_helper_lmw(cpu_env, t0, t1);
ff4a62cd
AJ
2891 tcg_temp_free(t0);
2892 tcg_temp_free_i32(t1);
79aceca5
FB
2893}
2894
2895/* stmw */
99e300ef 2896static void gen_stmw(DisasContext *ctx)
79aceca5 2897{
76db3ba4
AJ
2898 TCGv t0;
2899 TCGv_i32 t1;
2900 gen_set_access_type(ctx, ACCESS_INT);
76db3ba4
AJ
2901 t0 = tcg_temp_new();
2902 t1 = tcg_const_i32(rS(ctx->opcode));
2903 gen_addr_imm_index(ctx, t0, 0);
2f5a189c 2904 gen_helper_stmw(cpu_env, t0, t1);
ff4a62cd
AJ
2905 tcg_temp_free(t0);
2906 tcg_temp_free_i32(t1);
79aceca5
FB
2907}
2908
2909/*** Integer load and store strings ***/
54623277 2910
79aceca5 2911/* lswi */
3fc6c082 2912/* PowerPC32 specification says we must generate an exception if
9a64fbe4
FB
2913 * rA is in the range of registers to be loaded.
2914 * In an other hand, IBM says this is valid, but rA won't be loaded.
2915 * For now, I'll follow the spec...
2916 */
99e300ef 2917static void gen_lswi(DisasContext *ctx)
79aceca5 2918{
dfbc799d
AJ
2919 TCGv t0;
2920 TCGv_i32 t1, t2;
79aceca5
FB
2921 int nb = NB(ctx->opcode);
2922 int start = rD(ctx->opcode);
9a64fbe4 2923 int ra = rA(ctx->opcode);
79aceca5
FB
2924 int nr;
2925
2926 if (nb == 0)
2927 nb = 32;
afbee712
TH
2928 nr = (nb + 3) / 4;
2929 if (unlikely(lsw_reg_in_range(start, nr, ra))) {
e06fcd75 2930 gen_inval_exception(ctx, POWERPC_EXCP_INVAL_LSWX);
9fddaa0c 2931 return;
297d8e62 2932 }
76db3ba4 2933 gen_set_access_type(ctx, ACCESS_INT);
dfbc799d 2934 t0 = tcg_temp_new();
76db3ba4 2935 gen_addr_register(ctx, t0);
dfbc799d
AJ
2936 t1 = tcg_const_i32(nb);
2937 t2 = tcg_const_i32(start);
2f5a189c 2938 gen_helper_lsw(cpu_env, t0, t1, t2);
dfbc799d
AJ
2939 tcg_temp_free(t0);
2940 tcg_temp_free_i32(t1);
2941 tcg_temp_free_i32(t2);
79aceca5
FB
2942}
2943
2944/* lswx */
99e300ef 2945static void gen_lswx(DisasContext *ctx)
79aceca5 2946{
76db3ba4
AJ
2947 TCGv t0;
2948 TCGv_i32 t1, t2, t3;
2949 gen_set_access_type(ctx, ACCESS_INT);
76db3ba4
AJ
2950 t0 = tcg_temp_new();
2951 gen_addr_reg_index(ctx, t0);
2952 t1 = tcg_const_i32(rD(ctx->opcode));
2953 t2 = tcg_const_i32(rA(ctx->opcode));
2954 t3 = tcg_const_i32(rB(ctx->opcode));
2f5a189c 2955 gen_helper_lswx(cpu_env, t0, t1, t2, t3);
dfbc799d
AJ
2956 tcg_temp_free(t0);
2957 tcg_temp_free_i32(t1);
2958 tcg_temp_free_i32(t2);
2959 tcg_temp_free_i32(t3);
79aceca5
FB
2960}
2961
2962/* stswi */
99e300ef 2963static void gen_stswi(DisasContext *ctx)
79aceca5 2964{
76db3ba4
AJ
2965 TCGv t0;
2966 TCGv_i32 t1, t2;
4b3686fa 2967 int nb = NB(ctx->opcode);
76db3ba4 2968 gen_set_access_type(ctx, ACCESS_INT);
76db3ba4
AJ
2969 t0 = tcg_temp_new();
2970 gen_addr_register(ctx, t0);
4b3686fa
FB
2971 if (nb == 0)
2972 nb = 32;
dfbc799d 2973 t1 = tcg_const_i32(nb);
76db3ba4 2974 t2 = tcg_const_i32(rS(ctx->opcode));
2f5a189c 2975 gen_helper_stsw(cpu_env, t0, t1, t2);
dfbc799d
AJ
2976 tcg_temp_free(t0);
2977 tcg_temp_free_i32(t1);
2978 tcg_temp_free_i32(t2);
79aceca5
FB
2979}
2980
2981/* stswx */
99e300ef 2982static void gen_stswx(DisasContext *ctx)
79aceca5 2983{
76db3ba4
AJ
2984 TCGv t0;
2985 TCGv_i32 t1, t2;
2986 gen_set_access_type(ctx, ACCESS_INT);
76db3ba4
AJ
2987 t0 = tcg_temp_new();
2988 gen_addr_reg_index(ctx, t0);
2989 t1 = tcg_temp_new_i32();
dfbc799d
AJ
2990 tcg_gen_trunc_tl_i32(t1, cpu_xer);
2991 tcg_gen_andi_i32(t1, t1, 0x7F);
76db3ba4 2992 t2 = tcg_const_i32(rS(ctx->opcode));
2f5a189c 2993 gen_helper_stsw(cpu_env, t0, t1, t2);
dfbc799d
AJ
2994 tcg_temp_free(t0);
2995 tcg_temp_free_i32(t1);
2996 tcg_temp_free_i32(t2);
79aceca5
FB
2997}
2998
2999/*** Memory synchronisation ***/
3000/* eieio */
99e300ef 3001static void gen_eieio(DisasContext *ctx)
79aceca5 3002{
79aceca5
FB
3003}
3004
c5a8d8f3 3005#if !defined(CONFIG_USER_ONLY)
cd0c6f47
BH
3006static inline void gen_check_tlb_flush(DisasContext *ctx)
3007{
c5a8d8f3
BH
3008 TCGv_i32 t;
3009 TCGLabel *l;
cd0c6f47 3010
c5a8d8f3
BH
3011 if (!ctx->lazy_tlb_flush) {
3012 return;
3013 }
3014 l = gen_new_label();
3015 t = tcg_temp_new_i32();
cd0c6f47
BH
3016 tcg_gen_ld_i32(t, cpu_env, offsetof(CPUPPCState, tlb_need_flush));
3017 tcg_gen_brcondi_i32(TCG_COND_EQ, t, 0, l);
3018 gen_helper_check_tlb_flush(cpu_env);
3019 gen_set_label(l);
3020 tcg_temp_free_i32(t);
3021}
3022#else
3023static inline void gen_check_tlb_flush(DisasContext *ctx) { }
3024#endif
3025
79aceca5 3026/* isync */
99e300ef 3027static void gen_isync(DisasContext *ctx)
79aceca5 3028{
cd0c6f47
BH
3029 /*
3030 * We need to check for a pending TLB flush. This can only happen in
3031 * kernel mode however so check MSR_PR
3032 */
3033 if (!ctx->pr) {
3034 gen_check_tlb_flush(ctx);
3035 }
e06fcd75 3036 gen_stop_exception(ctx);
79aceca5
FB
3037}
3038
5c77a786
TM
3039#define LARX(name, len, loadop) \
3040static void gen_##name(DisasContext *ctx) \
3041{ \
3042 TCGv t0; \
3043 TCGv gpr = cpu_gpr[rD(ctx->opcode)]; \
3044 gen_set_access_type(ctx, ACCESS_RES); \
3045 t0 = tcg_temp_local_new(); \
3046 gen_addr_reg_index(ctx, t0); \
3047 if ((len) > 1) { \
3048 gen_check_align(ctx, t0, (len)-1); \
3049 } \
3050 gen_qemu_##loadop(ctx, gpr, t0); \
3051 tcg_gen_mov_tl(cpu_reserve, t0); \
3052 tcg_gen_st_tl(gpr, cpu_env, offsetof(CPUPPCState, reserve_val)); \
3053 tcg_temp_free(t0); \
79aceca5
FB
3054}
3055
5c77a786
TM
3056/* lwarx */
3057LARX(lbarx, 1, ld8u);
3058LARX(lharx, 2, ld16u);
3059LARX(lwarx, 4, ld32u);
3060
3061
4425265b 3062#if defined(CONFIG_USER_ONLY)
587c51f7
TM
3063static void gen_conditional_store(DisasContext *ctx, TCGv EA,
3064 int reg, int size)
4425265b
NF
3065{
3066 TCGv t0 = tcg_temp_new();
4425265b 3067
1328c2bf 3068 tcg_gen_st_tl(EA, cpu_env, offsetof(CPUPPCState, reserve_ea));
4425265b 3069 tcg_gen_movi_tl(t0, (size << 5) | reg);
1328c2bf 3070 tcg_gen_st_tl(t0, cpu_env, offsetof(CPUPPCState, reserve_info));
4425265b 3071 tcg_temp_free(t0);
bd6fefe7 3072 gen_exception_err(ctx, POWERPC_EXCP_STCX, 0);
4425265b 3073}
4425265b 3074#else
587c51f7
TM
3075static void gen_conditional_store(DisasContext *ctx, TCGv EA,
3076 int reg, int size)
3077{
42a268c2 3078 TCGLabel *l1;
4425265b 3079
587c51f7
TM
3080 tcg_gen_trunc_tl_i32(cpu_crf[0], cpu_so);
3081 l1 = gen_new_label();
3082 tcg_gen_brcond_tl(TCG_COND_NE, EA, cpu_reserve, l1);
3083 tcg_gen_ori_i32(cpu_crf[0], cpu_crf[0], 1 << CRF_EQ);
3084#if defined(TARGET_PPC64)
3085 if (size == 8) {
3086 gen_qemu_st64(ctx, cpu_gpr[reg], EA);
3087 } else
3088#endif
3089 if (size == 4) {
3090 gen_qemu_st32(ctx, cpu_gpr[reg], EA);
3091 } else if (size == 2) {
3092 gen_qemu_st16(ctx, cpu_gpr[reg], EA);
27b95bfe
TM
3093#if defined(TARGET_PPC64)
3094 } else if (size == 16) {
3707cd62 3095 TCGv gpr1, gpr2 , EA8;
27b95bfe
TM
3096 if (unlikely(ctx->le_mode)) {
3097 gpr1 = cpu_gpr[reg+1];
3098 gpr2 = cpu_gpr[reg];
3099 } else {
3100 gpr1 = cpu_gpr[reg];
3101 gpr2 = cpu_gpr[reg+1];
3102 }
3103 gen_qemu_st64(ctx, gpr1, EA);
3707cd62
TM
3104 EA8 = tcg_temp_local_new();
3105 gen_addr_add(ctx, EA8, EA, 8);
3106 gen_qemu_st64(ctx, gpr2, EA8);
3107 tcg_temp_free(EA8);
27b95bfe 3108#endif
587c51f7
TM
3109 } else {
3110 gen_qemu_st8(ctx, cpu_gpr[reg], EA);
4425265b 3111 }
587c51f7
TM
3112 gen_set_label(l1);
3113 tcg_gen_movi_tl(cpu_reserve, -1);
3114}
4425265b 3115#endif
587c51f7
TM
3116
3117#define STCX(name, len) \
3118static void gen_##name(DisasContext *ctx) \
3119{ \
3120 TCGv t0; \
27b95bfe
TM
3121 if (unlikely((len == 16) && (rD(ctx->opcode) & 1))) { \
3122 gen_inval_exception(ctx, \
3123 POWERPC_EXCP_INVAL_INVAL); \
3124 return; \
3125 } \
587c51f7
TM
3126 gen_set_access_type(ctx, ACCESS_RES); \
3127 t0 = tcg_temp_local_new(); \
3128 gen_addr_reg_index(ctx, t0); \
3129 if (len > 1) { \
3130 gen_check_align(ctx, t0, (len)-1); \
3131 } \
3132 gen_conditional_store(ctx, t0, rS(ctx->opcode), len); \
3133 tcg_temp_free(t0); \
79aceca5
FB
3134}
3135
587c51f7
TM
3136STCX(stbcx_, 1);
3137STCX(sthcx_, 2);
3138STCX(stwcx_, 4);
3139
426613db 3140#if defined(TARGET_PPC64)
426613db 3141/* ldarx */
5c77a786 3142LARX(ldarx, 8, ld64);
426613db 3143
9c294d5a
TM
3144/* lqarx */
3145static void gen_lqarx(DisasContext *ctx)
3146{
3147 TCGv EA;
3148 int rd = rD(ctx->opcode);
3149 TCGv gpr1, gpr2;
3150
3151 if (unlikely((rd & 1) || (rd == rA(ctx->opcode)) ||
3152 (rd == rB(ctx->opcode)))) {
3153 gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL);
3154 return;
3155 }
3156
3157 gen_set_access_type(ctx, ACCESS_RES);
3158 EA = tcg_temp_local_new();
3159 gen_addr_reg_index(ctx, EA);
3160 gen_check_align(ctx, EA, 15);
3161 if (unlikely(ctx->le_mode)) {
3162 gpr1 = cpu_gpr[rd+1];
3163 gpr2 = cpu_gpr[rd];
3164 } else {
3165 gpr1 = cpu_gpr[rd];
3166 gpr2 = cpu_gpr[rd+1];
3167 }
3168 gen_qemu_ld64(ctx, gpr1, EA);
3169 tcg_gen_mov_tl(cpu_reserve, EA);
3170
3171 gen_addr_add(ctx, EA, EA, 8);
3172 gen_qemu_ld64(ctx, gpr2, EA);
3173
3174 tcg_gen_st_tl(gpr1, cpu_env, offsetof(CPUPPCState, reserve_val));
3175 tcg_gen_st_tl(gpr2, cpu_env, offsetof(CPUPPCState, reserve_val2));
3176
3177 tcg_temp_free(EA);
3178}
3179
426613db 3180/* stdcx. */
587c51f7 3181STCX(stdcx_, 8);
27b95bfe 3182STCX(stqcx_, 16);
426613db
JM
3183#endif /* defined(TARGET_PPC64) */
3184
79aceca5 3185/* sync */
99e300ef 3186static void gen_sync(DisasContext *ctx)
79aceca5 3187{
cd0c6f47
BH
3188 uint32_t l = (ctx->opcode >> 21) & 3;
3189
3190 /*
c5a8d8f3
BH
3191 * We may need to check for a pending TLB flush.
3192 *
3193 * We do this on ptesync (l == 2) on ppc64 and any sync pn ppc32.
3194 *
3195 * Additionally, this can only happen in kernel mode however so
3196 * check MSR_PR as well.
cd0c6f47 3197 */
c5a8d8f3 3198 if (((l == 2) || !(ctx->insns_flags & PPC_64B)) && !ctx->pr) {
cd0c6f47
BH
3199 gen_check_tlb_flush(ctx);
3200 }
79aceca5
FB
3201}
3202
0db1b20e 3203/* wait */
99e300ef 3204static void gen_wait(DisasContext *ctx)
0db1b20e 3205{
35b5066e 3206 TCGv_i32 t0 = tcg_const_i32(1);
259186a7
AF
3207 tcg_gen_st_i32(t0, cpu_env,
3208 -offsetof(PowerPCCPU, env) + offsetof(CPUState, halted));
931ff272 3209 tcg_temp_free_i32(t0);
0db1b20e 3210 /* Stop translation, as the CPU is supposed to sleep from now */
bd6fefe7 3211 gen_exception_nip(ctx, EXCP_HLT, ctx->nip);
0db1b20e
JM
3212}
3213
7778a575
BH
3214#if defined(TARGET_PPC64)
3215static void gen_doze(DisasContext *ctx)
3216{
3217#if defined(CONFIG_USER_ONLY)
3218 GEN_PRIV;
3219#else
3220 TCGv_i32 t;
3221
3222 CHK_HV;
3223 t = tcg_const_i32(PPC_PM_DOZE);
3224 gen_helper_pminsn(cpu_env, t);
3225 tcg_temp_free_i32(t);
3226 gen_stop_exception(ctx);
3227#endif /* defined(CONFIG_USER_ONLY) */
3228}
3229
3230static void gen_nap(DisasContext *ctx)
3231{
3232#if defined(CONFIG_USER_ONLY)
3233 GEN_PRIV;
3234#else
3235 TCGv_i32 t;
3236
3237 CHK_HV;
3238 t = tcg_const_i32(PPC_PM_NAP);
3239 gen_helper_pminsn(cpu_env, t);
3240 tcg_temp_free_i32(t);
3241 gen_stop_exception(ctx);
3242#endif /* defined(CONFIG_USER_ONLY) */
3243}
3244
3245static void gen_sleep(DisasContext *ctx)
3246{
3247#if defined(CONFIG_USER_ONLY)
3248 GEN_PRIV;
3249#else
3250 TCGv_i32 t;
3251
3252 CHK_HV;
3253 t = tcg_const_i32(PPC_PM_SLEEP);
3254 gen_helper_pminsn(cpu_env, t);
3255 tcg_temp_free_i32(t);
3256 gen_stop_exception(ctx);
3257#endif /* defined(CONFIG_USER_ONLY) */
3258}
3259
3260static void gen_rvwinkle(DisasContext *ctx)
3261{
3262#if defined(CONFIG_USER_ONLY)
3263 GEN_PRIV;
3264#else
3265 TCGv_i32 t;
3266
3267 CHK_HV;
3268 t = tcg_const_i32(PPC_PM_RVWINKLE);
3269 gen_helper_pminsn(cpu_env, t);
3270 tcg_temp_free_i32(t);
3271 gen_stop_exception(ctx);
3272#endif /* defined(CONFIG_USER_ONLY) */
3273}
3274#endif /* #if defined(TARGET_PPC64) */
3275
697ab892
DG
3276static inline void gen_update_cfar(DisasContext *ctx, target_ulong nip)
3277{
3278#if defined(TARGET_PPC64)
3279 if (ctx->has_cfar)
3280 tcg_gen_movi_tl(cpu_cfar, nip);
3281#endif
3282}
3283
90aa39a1
SF
3284static inline bool use_goto_tb(DisasContext *ctx, target_ulong dest)
3285{
3286 if (unlikely(ctx->singlestep_enabled)) {
3287 return false;
3288 }
3289
3290#ifndef CONFIG_USER_ONLY
3291 return (ctx->tb->pc & TARGET_PAGE_MASK) == (dest & TARGET_PAGE_MASK);
3292#else
3293 return true;
3294#endif
3295}
3296
79aceca5 3297/*** Branch ***/
636aa200 3298static inline void gen_goto_tb(DisasContext *ctx, int n, target_ulong dest)
c1942362 3299{
e0c8f9ce 3300 if (NARROW_MODE(ctx)) {
a2ffb812 3301 dest = (uint32_t) dest;
e0c8f9ce 3302 }
90aa39a1 3303 if (use_goto_tb(ctx, dest)) {
57fec1fe 3304 tcg_gen_goto_tb(n);
a2ffb812 3305 tcg_gen_movi_tl(cpu_nip, dest & ~3);
90aa39a1 3306 tcg_gen_exit_tb((uintptr_t)ctx->tb + n);
c1942362 3307 } else {
a2ffb812 3308 tcg_gen_movi_tl(cpu_nip, dest & ~3);
8cbcb4fa
AJ
3309 if (unlikely(ctx->singlestep_enabled)) {
3310 if ((ctx->singlestep_enabled &
bdc4e053 3311 (CPU_BRANCH_STEP | CPU_SINGLE_STEP)) &&
f0cc4aa8
JG
3312 (ctx->exception == POWERPC_EXCP_BRANCH ||
3313 ctx->exception == POWERPC_EXCP_TRACE)) {
bd6fefe7 3314 gen_exception_nip(ctx, POWERPC_EXCP_TRACE, dest);
8cbcb4fa
AJ
3315 }
3316 if (ctx->singlestep_enabled & GDBSTUB_SINGLE_STEP) {
e06fcd75 3317 gen_debug_exception(ctx);
8cbcb4fa
AJ
3318 }
3319 }
57fec1fe 3320 tcg_gen_exit_tb(0);
c1942362 3321 }
c53be334
FB
3322}
3323
636aa200 3324static inline void gen_setlr(DisasContext *ctx, target_ulong nip)
e1833e1f 3325{
e0c8f9ce
RH
3326 if (NARROW_MODE(ctx)) {
3327 nip = (uint32_t)nip;
3328 }
3329 tcg_gen_movi_tl(cpu_lr, nip);
e1833e1f
JM
3330}
3331
79aceca5 3332/* b ba bl bla */
99e300ef 3333static void gen_b(DisasContext *ctx)
79aceca5 3334{
76a66253 3335 target_ulong li, target;
38a64f9d 3336
8cbcb4fa 3337 ctx->exception = POWERPC_EXCP_BRANCH;
38a64f9d 3338 /* sign extend LI */
e0c8f9ce
RH
3339 li = LI(ctx->opcode);
3340 li = (li ^ 0x02000000) - 0x02000000;
3341 if (likely(AA(ctx->opcode) == 0)) {
046d6672 3342 target = ctx->nip + li - 4;
e0c8f9ce 3343 } else {
9a64fbe4 3344 target = li;
e0c8f9ce
RH
3345 }
3346 if (LK(ctx->opcode)) {
e1833e1f 3347 gen_setlr(ctx, ctx->nip);
e0c8f9ce 3348 }
697ab892 3349 gen_update_cfar(ctx, ctx->nip);
c1942362 3350 gen_goto_tb(ctx, 0, target);
79aceca5
FB
3351}
3352
e98a6e40
FB
3353#define BCOND_IM 0
3354#define BCOND_LR 1
3355#define BCOND_CTR 2
52a4984d 3356#define BCOND_TAR 3
e98a6e40 3357
636aa200 3358static inline void gen_bcond(DisasContext *ctx, int type)
d9bce9d9 3359{
d9bce9d9 3360 uint32_t bo = BO(ctx->opcode);
42a268c2 3361 TCGLabel *l1;
a2ffb812 3362 TCGv target;
e98a6e40 3363
8cbcb4fa 3364 ctx->exception = POWERPC_EXCP_BRANCH;
52a4984d 3365 if (type == BCOND_LR || type == BCOND_CTR || type == BCOND_TAR) {
a7812ae4 3366 target = tcg_temp_local_new();
a2ffb812
AJ
3367 if (type == BCOND_CTR)
3368 tcg_gen_mov_tl(target, cpu_ctr);
52a4984d
TM
3369 else if (type == BCOND_TAR)
3370 gen_load_spr(target, SPR_TAR);
a2ffb812
AJ
3371 else
3372 tcg_gen_mov_tl(target, cpu_lr);
d2e9fd8f 3373 } else {
3374 TCGV_UNUSED(target);
e98a6e40 3375 }
e1833e1f
JM
3376 if (LK(ctx->opcode))
3377 gen_setlr(ctx, ctx->nip);
a2ffb812
AJ
3378 l1 = gen_new_label();
3379 if ((bo & 0x4) == 0) {
3380 /* Decrement and test CTR */
a7812ae4 3381 TCGv temp = tcg_temp_new();
a2ffb812 3382 if (unlikely(type == BCOND_CTR)) {
e06fcd75 3383 gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL);
a2ffb812
AJ
3384 return;
3385 }
3386 tcg_gen_subi_tl(cpu_ctr, cpu_ctr, 1);
e0c8f9ce 3387 if (NARROW_MODE(ctx)) {
a2ffb812 3388 tcg_gen_ext32u_tl(temp, cpu_ctr);
e0c8f9ce 3389 } else {
a2ffb812 3390 tcg_gen_mov_tl(temp, cpu_ctr);
e0c8f9ce 3391 }
a2ffb812
AJ
3392 if (bo & 0x2) {
3393 tcg_gen_brcondi_tl(TCG_COND_NE, temp, 0, l1);
3394 } else {
3395 tcg_gen_brcondi_tl(TCG_COND_EQ, temp, 0, l1);
e98a6e40 3396 }
a7812ae4 3397 tcg_temp_free(temp);
a2ffb812
AJ
3398 }
3399 if ((bo & 0x10) == 0) {
3400 /* Test CR */
3401 uint32_t bi = BI(ctx->opcode);
8f9fb7ac 3402 uint32_t mask = 0x08 >> (bi & 0x03);
a7812ae4 3403 TCGv_i32 temp = tcg_temp_new_i32();
a2ffb812 3404
d9bce9d9 3405 if (bo & 0x8) {
a2ffb812
AJ
3406 tcg_gen_andi_i32(temp, cpu_crf[bi >> 2], mask);
3407 tcg_gen_brcondi_i32(TCG_COND_EQ, temp, 0, l1);
d9bce9d9 3408 } else {
a2ffb812
AJ
3409 tcg_gen_andi_i32(temp, cpu_crf[bi >> 2], mask);
3410 tcg_gen_brcondi_i32(TCG_COND_NE, temp, 0, l1);
d9bce9d9 3411 }
a7812ae4 3412 tcg_temp_free_i32(temp);
d9bce9d9 3413 }
697ab892 3414 gen_update_cfar(ctx, ctx->nip);
e98a6e40 3415 if (type == BCOND_IM) {
a2ffb812
AJ
3416 target_ulong li = (target_long)((int16_t)(BD(ctx->opcode)));
3417 if (likely(AA(ctx->opcode) == 0)) {
3418 gen_goto_tb(ctx, 0, ctx->nip + li - 4);
3419 } else {
3420 gen_goto_tb(ctx, 0, li);
3421 }
c53be334 3422 gen_set_label(l1);
c1942362 3423 gen_goto_tb(ctx, 1, ctx->nip);
e98a6e40 3424 } else {
e0c8f9ce 3425 if (NARROW_MODE(ctx)) {
a2ffb812 3426 tcg_gen_andi_tl(cpu_nip, target, (uint32_t)~3);
e0c8f9ce 3427 } else {
a2ffb812 3428 tcg_gen_andi_tl(cpu_nip, target, ~3);
e0c8f9ce 3429 }
a2ffb812
AJ
3430 tcg_gen_exit_tb(0);
3431 gen_set_label(l1);
e0c8f9ce 3432 gen_update_nip(ctx, ctx->nip);
57fec1fe 3433 tcg_gen_exit_tb(0);
08e46e54 3434 }
a9e8f4e7 3435 if (type == BCOND_LR || type == BCOND_CTR || type == BCOND_TAR) {
c80d1df5
AG
3436 tcg_temp_free(target);
3437 }
e98a6e40
FB
3438}
3439
99e300ef 3440static void gen_bc(DisasContext *ctx)
3b46e624 3441{
e98a6e40
FB
3442 gen_bcond(ctx, BCOND_IM);
3443}
3444
99e300ef 3445static void gen_bcctr(DisasContext *ctx)
3b46e624 3446{
e98a6e40
FB
3447 gen_bcond(ctx, BCOND_CTR);
3448}
3449
99e300ef 3450static void gen_bclr(DisasContext *ctx)
3b46e624 3451{
e98a6e40
FB
3452 gen_bcond(ctx, BCOND_LR);
3453}
79aceca5 3454
52a4984d
TM
3455static void gen_bctar(DisasContext *ctx)
3456{
3457 gen_bcond(ctx, BCOND_TAR);
3458}
3459
79aceca5 3460/*** Condition register logical ***/
e1571908 3461#define GEN_CRLOGIC(name, tcg_op, opc) \
99e300ef 3462static void glue(gen_, name)(DisasContext *ctx) \
79aceca5 3463{ \
fc0d441e
JM
3464 uint8_t bitmask; \
3465 int sh; \
a7812ae4 3466 TCGv_i32 t0, t1; \
fc0d441e 3467 sh = (crbD(ctx->opcode) & 0x03) - (crbA(ctx->opcode) & 0x03); \
a7812ae4 3468 t0 = tcg_temp_new_i32(); \
fc0d441e 3469 if (sh > 0) \
fea0c503 3470 tcg_gen_shri_i32(t0, cpu_crf[crbA(ctx->opcode) >> 2], sh); \
fc0d441e 3471 else if (sh < 0) \
fea0c503 3472 tcg_gen_shli_i32(t0, cpu_crf[crbA(ctx->opcode) >> 2], -sh); \
e1571908 3473 else \
fea0c503 3474 tcg_gen_mov_i32(t0, cpu_crf[crbA(ctx->opcode) >> 2]); \
a7812ae4 3475 t1 = tcg_temp_new_i32(); \
fc0d441e
JM
3476 sh = (crbD(ctx->opcode) & 0x03) - (crbB(ctx->opcode) & 0x03); \
3477 if (sh > 0) \
fea0c503 3478 tcg_gen_shri_i32(t1, cpu_crf[crbB(ctx->opcode) >> 2], sh); \
fc0d441e 3479 else if (sh < 0) \
fea0c503 3480 tcg_gen_shli_i32(t1, cpu_crf[crbB(ctx->opcode) >> 2], -sh); \
e1571908 3481 else \
fea0c503
AJ
3482 tcg_gen_mov_i32(t1, cpu_crf[crbB(ctx->opcode) >> 2]); \
3483 tcg_op(t0, t0, t1); \
8f9fb7ac 3484 bitmask = 0x08 >> (crbD(ctx->opcode) & 0x03); \
fea0c503
AJ
3485 tcg_gen_andi_i32(t0, t0, bitmask); \
3486 tcg_gen_andi_i32(t1, cpu_crf[crbD(ctx->opcode) >> 2], ~bitmask); \
3487 tcg_gen_or_i32(cpu_crf[crbD(ctx->opcode) >> 2], t0, t1); \
a7812ae4
PB
3488 tcg_temp_free_i32(t0); \
3489 tcg_temp_free_i32(t1); \
79aceca5
FB
3490}
3491
3492/* crand */
e1571908 3493GEN_CRLOGIC(crand, tcg_gen_and_i32, 0x08);
79aceca5 3494/* crandc */
e1571908 3495GEN_CRLOGIC(crandc, tcg_gen_andc_i32, 0x04);
79aceca5 3496/* creqv */
e1571908 3497GEN_CRLOGIC(creqv, tcg_gen_eqv_i32, 0x09);
79aceca5 3498/* crnand */
e1571908 3499GEN_CRLOGIC(crnand, tcg_gen_nand_i32, 0x07);
79aceca5 3500/* crnor */
e1571908 3501GEN_CRLOGIC(crnor, tcg_gen_nor_i32, 0x01);
79aceca5 3502/* cror */
e1571908 3503GEN_CRLOGIC(cror, tcg_gen_or_i32, 0x0E);
79aceca5 3504/* crorc */
e1571908 3505GEN_CRLOGIC(crorc, tcg_gen_orc_i32, 0x0D);
79aceca5 3506/* crxor */
e1571908 3507GEN_CRLOGIC(crxor, tcg_gen_xor_i32, 0x06);
99e300ef 3508
54623277 3509/* mcrf */
99e300ef 3510static void gen_mcrf(DisasContext *ctx)
79aceca5 3511{
47e4661c 3512 tcg_gen_mov_i32(cpu_crf[crfD(ctx->opcode)], cpu_crf[crfS(ctx->opcode)]);
79aceca5
FB
3513}
3514
3515/*** System linkage ***/
99e300ef 3516
c47493f2 3517/* rfi (supervisor only) */
99e300ef 3518static void gen_rfi(DisasContext *ctx)
79aceca5 3519{
9a64fbe4 3520#if defined(CONFIG_USER_ONLY)
9b2fadda 3521 GEN_PRIV;
9a64fbe4 3522#else
a2e71b28
BH
3523 /* FIXME: This instruction doesn't exist anymore on 64-bit server
3524 * processors compliant with arch 2.x, we should remove it there,
3525 * but we need to fix OpenBIOS not to use it on 970 first
3526 */
9a64fbe4 3527 /* Restore CPU state */
9b2fadda 3528 CHK_SV;
697ab892 3529 gen_update_cfar(ctx, ctx->nip);
e5f17ac6 3530 gen_helper_rfi(cpu_env);
e06fcd75 3531 gen_sync_exception(ctx);
9a64fbe4 3532#endif
79aceca5
FB
3533}
3534
426613db 3535#if defined(TARGET_PPC64)
99e300ef 3536static void gen_rfid(DisasContext *ctx)
426613db
JM
3537{
3538#if defined(CONFIG_USER_ONLY)
9b2fadda 3539 GEN_PRIV;
426613db
JM
3540#else
3541 /* Restore CPU state */
9b2fadda 3542 CHK_SV;
697ab892 3543 gen_update_cfar(ctx, ctx->nip);
e5f17ac6 3544 gen_helper_rfid(cpu_env);
e06fcd75 3545 gen_sync_exception(ctx);
426613db
JM
3546#endif
3547}
426613db 3548
99e300ef 3549static void gen_hrfid(DisasContext *ctx)
be147d08
JM
3550{
3551#if defined(CONFIG_USER_ONLY)
9b2fadda 3552 GEN_PRIV;
be147d08
JM
3553#else
3554 /* Restore CPU state */
9b2fadda 3555 CHK_HV;
e5f17ac6 3556 gen_helper_hrfid(cpu_env);
e06fcd75 3557 gen_sync_exception(ctx);
be147d08
JM
3558#endif
3559}
3560#endif
3561
79aceca5 3562/* sc */
417bf010
JM
3563#if defined(CONFIG_USER_ONLY)
3564#define POWERPC_SYSCALL POWERPC_EXCP_SYSCALL_USER
3565#else
3566#define POWERPC_SYSCALL POWERPC_EXCP_SYSCALL
3567#endif
99e300ef 3568static void gen_sc(DisasContext *ctx)
79aceca5 3569{
e1833e1f
JM
3570 uint32_t lev;
3571
3572 lev = (ctx->opcode >> 5) & 0x7F;
e06fcd75 3573 gen_exception_err(ctx, POWERPC_SYSCALL, lev);
79aceca5
FB
3574}
3575
3576/*** Trap ***/
99e300ef 3577
54623277 3578/* tw */
99e300ef 3579static void gen_tw(DisasContext *ctx)
79aceca5 3580{
cab3bee2 3581 TCGv_i32 t0 = tcg_const_i32(TO(ctx->opcode));
e5f17ac6
BS
3582 gen_helper_tw(cpu_env, cpu_gpr[rA(ctx->opcode)], cpu_gpr[rB(ctx->opcode)],
3583 t0);
cab3bee2 3584 tcg_temp_free_i32(t0);
79aceca5
FB
3585}
3586
3587/* twi */
99e300ef 3588static void gen_twi(DisasContext *ctx)
79aceca5 3589{
cab3bee2
AJ
3590 TCGv t0 = tcg_const_tl(SIMM(ctx->opcode));
3591 TCGv_i32 t1 = tcg_const_i32(TO(ctx->opcode));
e5f17ac6 3592 gen_helper_tw(cpu_env, cpu_gpr[rA(ctx->opcode)], t0, t1);
cab3bee2
AJ
3593 tcg_temp_free(t0);
3594 tcg_temp_free_i32(t1);
79aceca5
FB
3595}
3596
d9bce9d9
JM
3597#if defined(TARGET_PPC64)
3598/* td */
99e300ef 3599static void gen_td(DisasContext *ctx)
d9bce9d9 3600{
cab3bee2 3601 TCGv_i32 t0 = tcg_const_i32(TO(ctx->opcode));
e5f17ac6
BS
3602 gen_helper_td(cpu_env, cpu_gpr[rA(ctx->opcode)], cpu_gpr[rB(ctx->opcode)],
3603 t0);
cab3bee2 3604 tcg_temp_free_i32(t0);
d9bce9d9
JM
3605}
3606
3607/* tdi */
99e300ef 3608static void gen_tdi(DisasContext *ctx)
d9bce9d9 3609{
cab3bee2
AJ
3610 TCGv t0 = tcg_const_tl(SIMM(ctx->opcode));
3611 TCGv_i32 t1 = tcg_const_i32(TO(ctx->opcode));
e5f17ac6 3612 gen_helper_td(cpu_env, cpu_gpr[rA(ctx->opcode)], t0, t1);
cab3bee2
AJ
3613 tcg_temp_free(t0);
3614 tcg_temp_free_i32(t1);
d9bce9d9
JM
3615}
3616#endif
3617
79aceca5 3618/*** Processor control ***/
99e300ef 3619
da91a00f
RH
3620static void gen_read_xer(TCGv dst)
3621{
3622 TCGv t0 = tcg_temp_new();
3623 TCGv t1 = tcg_temp_new();
3624 TCGv t2 = tcg_temp_new();
3625 tcg_gen_mov_tl(dst, cpu_xer);
3626 tcg_gen_shli_tl(t0, cpu_so, XER_SO);
3627 tcg_gen_shli_tl(t1, cpu_ov, XER_OV);
3628 tcg_gen_shli_tl(t2, cpu_ca, XER_CA);
3629 tcg_gen_or_tl(t0, t0, t1);
3630 tcg_gen_or_tl(dst, dst, t2);
3631 tcg_gen_or_tl(dst, dst, t0);
3632 tcg_temp_free(t0);
3633 tcg_temp_free(t1);
3634 tcg_temp_free(t2);
3635}
3636
3637static void gen_write_xer(TCGv src)
3638{
3639 tcg_gen_andi_tl(cpu_xer, src,
3640 ~((1u << XER_SO) | (1u << XER_OV) | (1u << XER_CA)));
3641 tcg_gen_shri_tl(cpu_so, src, XER_SO);
3642 tcg_gen_shri_tl(cpu_ov, src, XER_OV);
3643 tcg_gen_shri_tl(cpu_ca, src, XER_CA);
3644 tcg_gen_andi_tl(cpu_so, cpu_so, 1);
3645 tcg_gen_andi_tl(cpu_ov, cpu_ov, 1);
3646 tcg_gen_andi_tl(cpu_ca, cpu_ca, 1);
3647}
3648
54623277 3649/* mcrxr */
99e300ef 3650static void gen_mcrxr(DisasContext *ctx)
79aceca5 3651{
da91a00f
RH
3652 TCGv_i32 t0 = tcg_temp_new_i32();
3653 TCGv_i32 t1 = tcg_temp_new_i32();
3654 TCGv_i32 dst = cpu_crf[crfD(ctx->opcode)];
3655
3656 tcg_gen_trunc_tl_i32(t0, cpu_so);
3657 tcg_gen_trunc_tl_i32(t1, cpu_ov);
3658 tcg_gen_trunc_tl_i32(dst, cpu_ca);
294d1292
SB
3659 tcg_gen_shli_i32(t0, t0, 3);
3660 tcg_gen_shli_i32(t1, t1, 2);
3661 tcg_gen_shli_i32(dst, dst, 1);
da91a00f
RH
3662 tcg_gen_or_i32(dst, dst, t0);
3663 tcg_gen_or_i32(dst, dst, t1);
3664 tcg_temp_free_i32(t0);
3665 tcg_temp_free_i32(t1);
3666
3667 tcg_gen_movi_tl(cpu_so, 0);
3668 tcg_gen_movi_tl(cpu_ov, 0);
3669 tcg_gen_movi_tl(cpu_ca, 0);
79aceca5
FB
3670}
3671
0cfe11ea 3672/* mfcr mfocrf */
99e300ef 3673static void gen_mfcr(DisasContext *ctx)
79aceca5 3674{
76a66253 3675 uint32_t crm, crn;
3b46e624 3676
76a66253
JM
3677 if (likely(ctx->opcode & 0x00100000)) {
3678 crm = CRM(ctx->opcode);
8dd640e4 3679 if (likely(crm && ((crm & (crm - 1)) == 0))) {
0cfe11ea 3680 crn = ctz32 (crm);
e1571908 3681 tcg_gen_extu_i32_tl(cpu_gpr[rD(ctx->opcode)], cpu_crf[7 - crn]);
0497d2f4
AJ
3682 tcg_gen_shli_tl(cpu_gpr[rD(ctx->opcode)],
3683 cpu_gpr[rD(ctx->opcode)], crn * 4);
76a66253 3684 }
d9bce9d9 3685 } else {
651721b2
AJ
3686 TCGv_i32 t0 = tcg_temp_new_i32();
3687 tcg_gen_mov_i32(t0, cpu_crf[0]);
3688 tcg_gen_shli_i32(t0, t0, 4);
3689 tcg_gen_or_i32(t0, t0, cpu_crf[1]);
3690 tcg_gen_shli_i32(t0, t0, 4);
3691 tcg_gen_or_i32(t0, t0, cpu_crf[2]);
3692 tcg_gen_shli_i32(t0, t0, 4);
3693 tcg_gen_or_i32(t0, t0, cpu_crf[3]);
3694 tcg_gen_shli_i32(t0, t0, 4);
3695 tcg_gen_or_i32(t0, t0, cpu_crf[4]);
3696 tcg_gen_shli_i32(t0, t0, 4);
3697 tcg_gen_or_i32(t0, t0, cpu_crf[5]);
3698 tcg_gen_shli_i32(t0, t0, 4);
3699 tcg_gen_or_i32(t0, t0, cpu_crf[6]);
3700 tcg_gen_shli_i32(t0, t0, 4);
3701 tcg_gen_or_i32(t0, t0, cpu_crf[7]);
3702 tcg_gen_extu_i32_tl(cpu_gpr[rD(ctx->opcode)], t0);
3703 tcg_temp_free_i32(t0);
d9bce9d9 3704 }
79aceca5
FB
3705}
3706
3707/* mfmsr */
99e300ef 3708static void gen_mfmsr(DisasContext *ctx)
79aceca5 3709{
9b2fadda 3710 CHK_SV;
6527f6ea 3711 tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_msr);
79aceca5
FB
3712}
3713
69b058c8 3714static void spr_noaccess(DisasContext *ctx, int gprn, int sprn)
3fc6c082 3715{
7b13448f 3716#if 0
3fc6c082
FB
3717 sprn = ((sprn >> 5) & 0x1F) | ((sprn & 0x1F) << 5);
3718 printf("ERROR: try to access SPR %d !\n", sprn);
7b13448f 3719#endif
3fc6c082
FB
3720}
3721#define SPR_NOACCESS (&spr_noaccess)
3fc6c082 3722
79aceca5 3723/* mfspr */
636aa200 3724static inline void gen_op_mfspr(DisasContext *ctx)
79aceca5 3725{
69b058c8 3726 void (*read_cb)(DisasContext *ctx, int gprn, int sprn);
79aceca5
FB
3727 uint32_t sprn = SPR(ctx->opcode);
3728
eb94268e
BH
3729#if defined(CONFIG_USER_ONLY)
3730 read_cb = ctx->spr_cb[sprn].uea_read;
3731#else
3732 if (ctx->pr) {
3733 read_cb = ctx->spr_cb[sprn].uea_read;
3734 } else if (ctx->hv) {
be147d08 3735 read_cb = ctx->spr_cb[sprn].hea_read;
eb94268e 3736 } else {
3fc6c082 3737 read_cb = ctx->spr_cb[sprn].oea_read;
eb94268e 3738 }
9a64fbe4 3739#endif
76a66253
JM
3740 if (likely(read_cb != NULL)) {
3741 if (likely(read_cb != SPR_NOACCESS)) {
45d827d2 3742 (*read_cb)(ctx, rD(ctx->opcode), sprn);
3fc6c082
FB
3743 } else {
3744 /* Privilege exception */
9fceefa7
JM
3745 /* This is a hack to avoid warnings when running Linux:
3746 * this OS breaks the PowerPC virtualisation model,
3747 * allowing userland application to read the PVR
3748 */
3749 if (sprn != SPR_PVR) {
013a2942
PB
3750 fprintf(stderr, "Trying to read privileged spr %d (0x%03x) at "
3751 TARGET_FMT_lx "\n", sprn, sprn, ctx->nip - 4);
3752 if (qemu_log_separate()) {
3753 qemu_log("Trying to read privileged spr %d (0x%03x) at "
3754 TARGET_FMT_lx "\n", sprn, sprn, ctx->nip - 4);
3755 }
f24e5695 3756 }
9b2fadda 3757 gen_priv_exception(ctx, POWERPC_EXCP_PRIV_REG);
79aceca5 3758 }
3fc6c082 3759 } else {
9b2fadda
BH
3760 /* ISA 2.07 defines these as no-ops */
3761 if ((ctx->insns_flags2 & PPC2_ISA207S) &&
3762 (sprn >= 808 && sprn <= 811)) {
3763 /* This is a nop */
3764 return;
3765 }
3fc6c082 3766 /* Not defined */
013a2942
PB
3767 fprintf(stderr, "Trying to read invalid spr %d (0x%03x) at "
3768 TARGET_FMT_lx "\n", sprn, sprn, ctx->nip - 4);
3769 if (qemu_log_separate()) {
3770 qemu_log("Trying to read invalid spr %d (0x%03x) at "
3771 TARGET_FMT_lx "\n", sprn, sprn, ctx->nip - 4);
3772 }
9b2fadda
BH
3773
3774 /* The behaviour depends on MSR:PR and SPR# bit 0x10,
3775 * it can generate a priv, a hv emu or a no-op
3776 */
3777 if (sprn & 0x10) {
3778 if (ctx->pr) {
3779 gen_priv_exception(ctx, POWERPC_EXCP_INVAL_SPR);
3780 }
3781 } else {
3782 if (ctx->pr || sprn == 0 || sprn == 4 || sprn == 5 || sprn == 6) {
3783 gen_hvpriv_exception(ctx, POWERPC_EXCP_INVAL_SPR);
3784 }
4d6a0680 3785 }
79aceca5 3786 }
79aceca5
FB
3787}
3788
99e300ef 3789static void gen_mfspr(DisasContext *ctx)
79aceca5 3790{
3fc6c082 3791 gen_op_mfspr(ctx);
76a66253 3792}
3fc6c082
FB
3793
3794/* mftb */
99e300ef 3795static void gen_mftb(DisasContext *ctx)
3fc6c082
FB
3796{
3797 gen_op_mfspr(ctx);
79aceca5
FB
3798}
3799
0cfe11ea 3800/* mtcrf mtocrf*/
99e300ef 3801static void gen_mtcrf(DisasContext *ctx)
79aceca5 3802{
76a66253 3803 uint32_t crm, crn;
3b46e624 3804
76a66253 3805 crm = CRM(ctx->opcode);
8dd640e4 3806 if (likely((ctx->opcode & 0x00100000))) {
3807 if (crm && ((crm & (crm - 1)) == 0)) {
3808 TCGv_i32 temp = tcg_temp_new_i32();
0cfe11ea 3809 crn = ctz32 (crm);
8dd640e4 3810 tcg_gen_trunc_tl_i32(temp, cpu_gpr[rS(ctx->opcode)]);
0cfe11ea
AJ
3811 tcg_gen_shri_i32(temp, temp, crn * 4);
3812 tcg_gen_andi_i32(cpu_crf[7 - crn], temp, 0xf);
8dd640e4 3813 tcg_temp_free_i32(temp);
3814 }
76a66253 3815 } else {
651721b2
AJ
3816 TCGv_i32 temp = tcg_temp_new_i32();
3817 tcg_gen_trunc_tl_i32(temp, cpu_gpr[rS(ctx->opcode)]);
3818 for (crn = 0 ; crn < 8 ; crn++) {
3819 if (crm & (1 << crn)) {
3820 tcg_gen_shri_i32(cpu_crf[7 - crn], temp, crn * 4);
3821 tcg_gen_andi_i32(cpu_crf[7 - crn], cpu_crf[7 - crn], 0xf);
3822 }
3823 }
a7812ae4 3824 tcg_temp_free_i32(temp);
76a66253 3825 }
79aceca5
FB
3826}
3827
3828/* mtmsr */
426613db 3829#if defined(TARGET_PPC64)
99e300ef 3830static void gen_mtmsrd(DisasContext *ctx)
426613db 3831{
9b2fadda
BH
3832 CHK_SV;
3833
3834#if !defined(CONFIG_USER_ONLY)
be147d08
JM
3835 if (ctx->opcode & 0x00010000) {
3836 /* Special form that does not need any synchronisation */
6527f6ea
AJ
3837 TCGv t0 = tcg_temp_new();
3838 tcg_gen_andi_tl(t0, cpu_gpr[rS(ctx->opcode)], (1 << MSR_RI) | (1 << MSR_EE));
c409bc5d 3839 tcg_gen_andi_tl(cpu_msr, cpu_msr, ~(target_ulong)((1 << MSR_RI) | (1 << MSR_EE)));
6527f6ea
AJ
3840 tcg_gen_or_tl(cpu_msr, cpu_msr, t0);
3841 tcg_temp_free(t0);
be147d08 3842 } else {
056b05f8
JM
3843 /* XXX: we need to update nip before the store
3844 * if we enter power saving mode, we will exit the loop
3845 * directly from ppc_store_msr
3846 */
be147d08 3847 gen_update_nip(ctx, ctx->nip);
e5f17ac6 3848 gen_helper_store_msr(cpu_env, cpu_gpr[rS(ctx->opcode)]);
be147d08
JM
3849 /* Must stop the translation as machine state (may have) changed */
3850 /* Note that mtmsr is not always defined as context-synchronizing */
e06fcd75 3851 gen_stop_exception(ctx);
be147d08 3852 }
9b2fadda 3853#endif /* !defined(CONFIG_USER_ONLY) */
426613db 3854}
9b2fadda 3855#endif /* defined(TARGET_PPC64) */
426613db 3856
99e300ef 3857static void gen_mtmsr(DisasContext *ctx)
79aceca5 3858{
9b2fadda
BH
3859 CHK_SV;
3860
3861#if !defined(CONFIG_USER_ONLY)
3862 if (ctx->opcode & 0x00010000) {
be147d08 3863 /* Special form that does not need any synchronisation */
6527f6ea
AJ
3864 TCGv t0 = tcg_temp_new();
3865 tcg_gen_andi_tl(t0, cpu_gpr[rS(ctx->opcode)], (1 << MSR_RI) | (1 << MSR_EE));
c409bc5d 3866 tcg_gen_andi_tl(cpu_msr, cpu_msr, ~(target_ulong)((1 << MSR_RI) | (1 << MSR_EE)));
6527f6ea
AJ
3867 tcg_gen_or_tl(cpu_msr, cpu_msr, t0);
3868 tcg_temp_free(t0);
be147d08 3869 } else {
8018dc63
AG
3870 TCGv msr = tcg_temp_new();
3871
056b05f8
JM
3872 /* XXX: we need to update nip before the store
3873 * if we enter power saving mode, we will exit the loop
3874 * directly from ppc_store_msr
3875 */
be147d08 3876 gen_update_nip(ctx, ctx->nip);
d9bce9d9 3877#if defined(TARGET_PPC64)
8018dc63
AG
3878 tcg_gen_deposit_tl(msr, cpu_msr, cpu_gpr[rS(ctx->opcode)], 0, 32);
3879#else
3880 tcg_gen_mov_tl(msr, cpu_gpr[rS(ctx->opcode)]);
d9bce9d9 3881#endif
e5f17ac6 3882 gen_helper_store_msr(cpu_env, msr);
c80d1df5 3883 tcg_temp_free(msr);
be147d08 3884 /* Must stop the translation as machine state (may have) changed */
6527f6ea 3885 /* Note that mtmsr is not always defined as context-synchronizing */
e06fcd75 3886 gen_stop_exception(ctx);
be147d08 3887 }
9a64fbe4 3888#endif
79aceca5
FB
3889}
3890
3891/* mtspr */
99e300ef 3892static void gen_mtspr(DisasContext *ctx)
79aceca5 3893{
69b058c8 3894 void (*write_cb)(DisasContext *ctx, int sprn, int gprn);
79aceca5
FB
3895 uint32_t sprn = SPR(ctx->opcode);
3896
eb94268e
BH
3897#if defined(CONFIG_USER_ONLY)
3898 write_cb = ctx->spr_cb[sprn].uea_write;
3899#else
3900 if (ctx->pr) {
3901 write_cb = ctx->spr_cb[sprn].uea_write;
3902 } else if (ctx->hv) {
be147d08 3903 write_cb = ctx->spr_cb[sprn].hea_write;
eb94268e 3904 } else {
3fc6c082 3905 write_cb = ctx->spr_cb[sprn].oea_write;
eb94268e 3906 }
9a64fbe4 3907#endif
76a66253
JM
3908 if (likely(write_cb != NULL)) {
3909 if (likely(write_cb != SPR_NOACCESS)) {
45d827d2 3910 (*write_cb)(ctx, sprn, rS(ctx->opcode));
3fc6c082
FB
3911 } else {
3912 /* Privilege exception */
013a2942
PB
3913 fprintf(stderr, "Trying to write privileged spr %d (0x%03x) at "
3914 TARGET_FMT_lx "\n", sprn, sprn, ctx->nip - 4);
3915 if (qemu_log_separate()) {
3916 qemu_log("Trying to write privileged spr %d (0x%03x) at "
3917 TARGET_FMT_lx "\n", sprn, sprn, ctx->nip - 4);
3918 }
9b2fadda 3919 gen_priv_exception(ctx, POWERPC_EXCP_PRIV_REG);
76a66253 3920 }
3fc6c082 3921 } else {
9b2fadda
BH
3922 /* ISA 2.07 defines these as no-ops */
3923 if ((ctx->insns_flags2 & PPC2_ISA207S) &&
3924 (sprn >= 808 && sprn <= 811)) {
3925 /* This is a nop */
3926 return;
3927 }
3928
3fc6c082 3929 /* Not defined */
013a2942
PB
3930 if (qemu_log_separate()) {
3931 qemu_log("Trying to write invalid spr %d (0x%03x) at "
3932 TARGET_FMT_lx "\n", sprn, sprn, ctx->nip - 4);
3933 }
3934 fprintf(stderr, "Trying to write invalid spr %d (0x%03x) at "
3935 TARGET_FMT_lx "\n", sprn, sprn, ctx->nip - 4);
4d6a0680 3936
9b2fadda
BH
3937
3938 /* The behaviour depends on MSR:PR and SPR# bit 0x10,
3939 * it can generate a priv, a hv emu or a no-op
3940 */
3941 if (sprn & 0x10) {
3942 if (ctx->pr) {
3943 gen_priv_exception(ctx, POWERPC_EXCP_INVAL_SPR);
3944 }
3945 } else {
3946 if (ctx->pr || sprn == 0) {
3947 gen_hvpriv_exception(ctx, POWERPC_EXCP_INVAL_SPR);
3948 }
4d6a0680 3949 }
79aceca5 3950 }
79aceca5
FB
3951}
3952
dc2ee038
VAS
3953#if defined(TARGET_PPC64)
3954/* setb */
3955static void gen_setb(DisasContext *ctx)
3956{
3957 TCGv_i32 t0 = tcg_temp_new_i32();
3958 TCGv_i32 t8 = tcg_temp_new_i32();
3959 TCGv_i32 tm1 = tcg_temp_new_i32();
3960 int crf = crfS(ctx->opcode);
3961
3962 tcg_gen_setcondi_i32(TCG_COND_GEU, t0, cpu_crf[crf], 4);
3963 tcg_gen_movi_i32(t8, 8);
3964 tcg_gen_movi_i32(tm1, -1);
3965 tcg_gen_movcond_i32(TCG_COND_GEU, t0, cpu_crf[crf], t8, tm1, t0);
3966 tcg_gen_ext_i32_tl(cpu_gpr[rD(ctx->opcode)], t0);
3967
3968 tcg_temp_free_i32(t0);
3969 tcg_temp_free_i32(t8);
3970 tcg_temp_free_i32(tm1);
3971}
3972#endif
3973
79aceca5 3974/*** Cache management ***/
99e300ef 3975
54623277 3976/* dcbf */
99e300ef 3977static void gen_dcbf(DisasContext *ctx)
79aceca5 3978{
dac454af 3979 /* XXX: specification says this is treated as a load by the MMU */
76db3ba4
AJ
3980 TCGv t0;
3981 gen_set_access_type(ctx, ACCESS_CACHE);
3982 t0 = tcg_temp_new();
3983 gen_addr_reg_index(ctx, t0);
3984 gen_qemu_ld8u(ctx, t0, t0);
fea0c503 3985 tcg_temp_free(t0);
79aceca5
FB
3986}
3987
3988/* dcbi (Supervisor only) */
99e300ef 3989static void gen_dcbi(DisasContext *ctx)
79aceca5 3990{
a541f297 3991#if defined(CONFIG_USER_ONLY)
9b2fadda 3992 GEN_PRIV;
a541f297 3993#else
b61f2753 3994 TCGv EA, val;
9b2fadda
BH
3995
3996 CHK_SV;
a7812ae4 3997 EA = tcg_temp_new();
76db3ba4
AJ
3998 gen_set_access_type(ctx, ACCESS_CACHE);
3999 gen_addr_reg_index(ctx, EA);
a7812ae4 4000 val = tcg_temp_new();
76a66253 4001 /* XXX: specification says this should be treated as a store by the MMU */
76db3ba4
AJ
4002 gen_qemu_ld8u(ctx, val, EA);
4003 gen_qemu_st8(ctx, val, EA);
b61f2753
AJ
4004 tcg_temp_free(val);
4005 tcg_temp_free(EA);
9b2fadda 4006#endif /* defined(CONFIG_USER_ONLY) */
79aceca5
FB
4007}
4008
4009/* dcdst */
99e300ef 4010static void gen_dcbst(DisasContext *ctx)
79aceca5 4011{
76a66253 4012 /* XXX: specification say this is treated as a load by the MMU */
76db3ba4
AJ
4013 TCGv t0;
4014 gen_set_access_type(ctx, ACCESS_CACHE);
4015 t0 = tcg_temp_new();
4016 gen_addr_reg_index(ctx, t0);
4017 gen_qemu_ld8u(ctx, t0, t0);
fea0c503 4018 tcg_temp_free(t0);
79aceca5
FB
4019}
4020
4021/* dcbt */
99e300ef 4022static void gen_dcbt(DisasContext *ctx)
79aceca5 4023{
0db1b20e 4024 /* interpreted as no-op */
76a66253
JM
4025 /* XXX: specification say this is treated as a load by the MMU
4026 * but does not generate any exception
4027 */
79aceca5
FB
4028}
4029
4030/* dcbtst */
99e300ef 4031static void gen_dcbtst(DisasContext *ctx)
79aceca5 4032{
0db1b20e 4033 /* interpreted as no-op */
76a66253
JM
4034 /* XXX: specification say this is treated as a load by the MMU
4035 * but does not generate any exception
4036 */
79aceca5
FB
4037}
4038
4d09d529
AG
4039/* dcbtls */
4040static void gen_dcbtls(DisasContext *ctx)
4041{
4042 /* Always fails locking the cache */
4043 TCGv t0 = tcg_temp_new();
4044 gen_load_spr(t0, SPR_Exxx_L1CSR0);
4045 tcg_gen_ori_tl(t0, t0, L1CSR0_CUL);
4046 gen_store_spr(SPR_Exxx_L1CSR0, t0);
4047 tcg_temp_free(t0);
4048}
4049
79aceca5 4050/* dcbz */
99e300ef 4051static void gen_dcbz(DisasContext *ctx)
79aceca5 4052{
8e33944f
AG
4053 TCGv tcgv_addr;
4054 TCGv_i32 tcgv_is_dcbzl;
4055 int is_dcbzl = ctx->opcode & 0x00200000 ? 1 : 0;
d63001d1 4056
76db3ba4 4057 gen_set_access_type(ctx, ACCESS_CACHE);
8e33944f
AG
4058 tcgv_addr = tcg_temp_new();
4059 tcgv_is_dcbzl = tcg_const_i32(is_dcbzl);
4060
4061 gen_addr_reg_index(ctx, tcgv_addr);
4062 gen_helper_dcbz(cpu_env, tcgv_addr, tcgv_is_dcbzl);
4063
4064 tcg_temp_free(tcgv_addr);
4065 tcg_temp_free_i32(tcgv_is_dcbzl);
79aceca5
FB
4066}
4067
ae1c1a3d 4068/* dst / dstt */
99e300ef 4069static void gen_dst(DisasContext *ctx)
ae1c1a3d
AJ
4070{
4071 if (rA(ctx->opcode) == 0) {
e41029b3 4072 gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL);
ae1c1a3d
AJ
4073 } else {
4074 /* interpreted as no-op */
4075 }
4076}
4077
4078/* dstst /dststt */
99e300ef 4079static void gen_dstst(DisasContext *ctx)
ae1c1a3d
AJ
4080{
4081 if (rA(ctx->opcode) == 0) {
e41029b3 4082 gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL);
ae1c1a3d
AJ
4083 } else {
4084 /* interpreted as no-op */
4085 }
4086
4087}
4088
4089/* dss / dssall */
99e300ef 4090static void gen_dss(DisasContext *ctx)
ae1c1a3d
AJ
4091{
4092 /* interpreted as no-op */
4093}
4094
79aceca5 4095/* icbi */
99e300ef 4096static void gen_icbi(DisasContext *ctx)
79aceca5 4097{
76db3ba4
AJ
4098 TCGv t0;
4099 gen_set_access_type(ctx, ACCESS_CACHE);
76db3ba4
AJ
4100 t0 = tcg_temp_new();
4101 gen_addr_reg_index(ctx, t0);
2f5a189c 4102 gen_helper_icbi(cpu_env, t0);
37d269df 4103 tcg_temp_free(t0);
79aceca5
FB
4104}
4105
4106/* Optional: */
4107/* dcba */
99e300ef 4108static void gen_dcba(DisasContext *ctx)
79aceca5 4109{
0db1b20e
JM
4110 /* interpreted as no-op */
4111 /* XXX: specification say this is treated as a store by the MMU
4112 * but does not generate any exception
4113 */
79aceca5
FB
4114}
4115
4116/*** Segment register manipulation ***/
4117/* Supervisor only: */
99e300ef 4118
54623277 4119/* mfsr */
99e300ef 4120static void gen_mfsr(DisasContext *ctx)
79aceca5 4121{
9a64fbe4 4122#if defined(CONFIG_USER_ONLY)
9b2fadda 4123 GEN_PRIV;
9a64fbe4 4124#else
74d37793 4125 TCGv t0;
9b2fadda
BH
4126
4127 CHK_SV;
74d37793 4128 t0 = tcg_const_tl(SR(ctx->opcode));
c6c7cf05 4129 gen_helper_load_sr(cpu_gpr[rD(ctx->opcode)], cpu_env, t0);
74d37793 4130 tcg_temp_free(t0);
9b2fadda 4131#endif /* defined(CONFIG_USER_ONLY) */
79aceca5
FB
4132}
4133
4134/* mfsrin */
99e300ef 4135static void gen_mfsrin(DisasContext *ctx)
79aceca5 4136{
9a64fbe4 4137#if defined(CONFIG_USER_ONLY)
9b2fadda 4138 GEN_PRIV;
9a64fbe4 4139#else
74d37793 4140 TCGv t0;
9b2fadda
BH
4141
4142 CHK_SV;
74d37793
AJ
4143 t0 = tcg_temp_new();
4144 tcg_gen_shri_tl(t0, cpu_gpr[rB(ctx->opcode)], 28);
4145 tcg_gen_andi_tl(t0, t0, 0xF);
c6c7cf05 4146 gen_helper_load_sr(cpu_gpr[rD(ctx->opcode)], cpu_env, t0);
74d37793 4147 tcg_temp_free(t0);
9b2fadda 4148#endif /* defined(CONFIG_USER_ONLY) */
79aceca5
FB
4149}
4150
4151/* mtsr */
99e300ef 4152static void gen_mtsr(DisasContext *ctx)
79aceca5 4153{
9a64fbe4 4154#if defined(CONFIG_USER_ONLY)
9b2fadda 4155 GEN_PRIV;
9a64fbe4 4156#else
74d37793 4157 TCGv t0;
9b2fadda
BH
4158
4159 CHK_SV;
74d37793 4160 t0 = tcg_const_tl(SR(ctx->opcode));
c6c7cf05 4161 gen_helper_store_sr(cpu_env, t0, cpu_gpr[rS(ctx->opcode)]);
74d37793 4162 tcg_temp_free(t0);
9b2fadda 4163#endif /* defined(CONFIG_USER_ONLY) */
79aceca5
FB
4164}
4165
4166/* mtsrin */
99e300ef 4167static void gen_mtsrin(DisasContext *ctx)
79aceca5 4168{
9a64fbe4 4169#if defined(CONFIG_USER_ONLY)
9b2fadda 4170 GEN_PRIV;
9a64fbe4 4171#else
74d37793 4172 TCGv t0;
9b2fadda
BH
4173 CHK_SV;
4174
74d37793
AJ
4175 t0 = tcg_temp_new();
4176 tcg_gen_shri_tl(t0, cpu_gpr[rB(ctx->opcode)], 28);
4177 tcg_gen_andi_tl(t0, t0, 0xF);
c6c7cf05 4178 gen_helper_store_sr(cpu_env, t0, cpu_gpr[rD(ctx->opcode)]);
74d37793 4179 tcg_temp_free(t0);
9b2fadda 4180#endif /* defined(CONFIG_USER_ONLY) */
79aceca5
FB
4181}
4182
12de9a39
JM
4183#if defined(TARGET_PPC64)
4184/* Specific implementation for PowerPC 64 "bridge" emulation using SLB */
e8eaa2c0 4185
54623277 4186/* mfsr */
e8eaa2c0 4187static void gen_mfsr_64b(DisasContext *ctx)
12de9a39
JM
4188{
4189#if defined(CONFIG_USER_ONLY)
9b2fadda 4190 GEN_PRIV;
12de9a39 4191#else
74d37793 4192 TCGv t0;
9b2fadda
BH
4193
4194 CHK_SV;
74d37793 4195 t0 = tcg_const_tl(SR(ctx->opcode));
c6c7cf05 4196 gen_helper_load_sr(cpu_gpr[rD(ctx->opcode)], cpu_env, t0);
74d37793 4197 tcg_temp_free(t0);
9b2fadda 4198#endif /* defined(CONFIG_USER_ONLY) */
12de9a39
JM
4199}
4200
4201/* mfsrin */
e8eaa2c0 4202static void gen_mfsrin_64b(DisasContext *ctx)
12de9a39
JM
4203{
4204#if defined(CONFIG_USER_ONLY)
9b2fadda 4205 GEN_PRIV;
12de9a39 4206#else
74d37793 4207 TCGv t0;
9b2fadda
BH
4208
4209 CHK_SV;
74d37793
AJ
4210 t0 = tcg_temp_new();
4211 tcg_gen_shri_tl(t0, cpu_gpr[rB(ctx->opcode)], 28);
4212 tcg_gen_andi_tl(t0, t0, 0xF);
c6c7cf05 4213 gen_helper_load_sr(cpu_gpr[rD(ctx->opcode)], cpu_env, t0);
74d37793 4214 tcg_temp_free(t0);
9b2fadda 4215#endif /* defined(CONFIG_USER_ONLY) */
12de9a39
JM
4216}
4217
4218/* mtsr */
e8eaa2c0 4219static void gen_mtsr_64b(DisasContext *ctx)
12de9a39
JM
4220{
4221#if defined(CONFIG_USER_ONLY)
9b2fadda 4222 GEN_PRIV;
12de9a39 4223#else
74d37793 4224 TCGv t0;
9b2fadda
BH
4225
4226 CHK_SV;
74d37793 4227 t0 = tcg_const_tl(SR(ctx->opcode));
c6c7cf05 4228 gen_helper_store_sr(cpu_env, t0, cpu_gpr[rS(ctx->opcode)]);
74d37793 4229 tcg_temp_free(t0);
9b2fadda 4230#endif /* defined(CONFIG_USER_ONLY) */
12de9a39
JM
4231}
4232
4233/* mtsrin */
e8eaa2c0 4234static void gen_mtsrin_64b(DisasContext *ctx)
12de9a39
JM
4235{
4236#if defined(CONFIG_USER_ONLY)
9b2fadda 4237 GEN_PRIV;
12de9a39 4238#else
74d37793 4239 TCGv t0;
9b2fadda
BH
4240
4241 CHK_SV;
74d37793
AJ
4242 t0 = tcg_temp_new();
4243 tcg_gen_shri_tl(t0, cpu_gpr[rB(ctx->opcode)], 28);
4244 tcg_gen_andi_tl(t0, t0, 0xF);
c6c7cf05 4245 gen_helper_store_sr(cpu_env, t0, cpu_gpr[rS(ctx->opcode)]);
74d37793 4246 tcg_temp_free(t0);
9b2fadda 4247#endif /* defined(CONFIG_USER_ONLY) */
12de9a39 4248}
f6b868fc
BS
4249
4250/* slbmte */
e8eaa2c0 4251static void gen_slbmte(DisasContext *ctx)
f6b868fc
BS
4252{
4253#if defined(CONFIG_USER_ONLY)
9b2fadda 4254 GEN_PRIV;
f6b868fc 4255#else
9b2fadda
BH
4256 CHK_SV;
4257
c6c7cf05
BS
4258 gen_helper_store_slb(cpu_env, cpu_gpr[rB(ctx->opcode)],
4259 cpu_gpr[rS(ctx->opcode)]);
9b2fadda 4260#endif /* defined(CONFIG_USER_ONLY) */
f6b868fc
BS
4261}
4262
efdef95f
DG
4263static void gen_slbmfee(DisasContext *ctx)
4264{
4265#if defined(CONFIG_USER_ONLY)
9b2fadda 4266 GEN_PRIV;
efdef95f 4267#else
9b2fadda
BH
4268 CHK_SV;
4269
c6c7cf05 4270 gen_helper_load_slb_esid(cpu_gpr[rS(ctx->opcode)], cpu_env,
efdef95f 4271 cpu_gpr[rB(ctx->opcode)]);
9b2fadda 4272#endif /* defined(CONFIG_USER_ONLY) */
efdef95f
DG
4273}
4274
4275static void gen_slbmfev(DisasContext *ctx)
4276{
4277#if defined(CONFIG_USER_ONLY)
9b2fadda 4278 GEN_PRIV;
efdef95f 4279#else
9b2fadda
BH
4280 CHK_SV;
4281
c6c7cf05 4282 gen_helper_load_slb_vsid(cpu_gpr[rS(ctx->opcode)], cpu_env,
efdef95f 4283 cpu_gpr[rB(ctx->opcode)]);
9b2fadda 4284#endif /* defined(CONFIG_USER_ONLY) */
efdef95f 4285}
c76c22d5
BH
4286
4287static void gen_slbfee_(DisasContext *ctx)
4288{
4289#if defined(CONFIG_USER_ONLY)
4290 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
4291#else
4292 TCGLabel *l1, *l2;
4293
4294 if (unlikely(ctx->pr)) {
4295 gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
4296 return;
4297 }
4298 gen_helper_find_slb_vsid(cpu_gpr[rS(ctx->opcode)], cpu_env,
4299 cpu_gpr[rB(ctx->opcode)]);
4300 l1 = gen_new_label();
4301 l2 = gen_new_label();
4302 tcg_gen_trunc_tl_i32(cpu_crf[0], cpu_so);
4303 tcg_gen_brcondi_tl(TCG_COND_EQ, cpu_gpr[rS(ctx->opcode)], -1, l1);
4304 tcg_gen_ori_i32(cpu_crf[0], cpu_crf[0], 1 << CRF_EQ);
4305 tcg_gen_br(l2);
4306 gen_set_label(l1);
4307 tcg_gen_movi_tl(cpu_gpr[rS(ctx->opcode)], 0);
4308 gen_set_label(l2);
4309#endif
4310}
12de9a39
JM
4311#endif /* defined(TARGET_PPC64) */
4312
79aceca5 4313/*** Lookaside buffer management ***/
c47493f2 4314/* Optional & supervisor only: */
99e300ef 4315
54623277 4316/* tlbia */
99e300ef 4317static void gen_tlbia(DisasContext *ctx)
79aceca5 4318{
9a64fbe4 4319#if defined(CONFIG_USER_ONLY)
9b2fadda 4320 GEN_PRIV;
9a64fbe4 4321#else
9b2fadda
BH
4322 CHK_HV;
4323
c6c7cf05 4324 gen_helper_tlbia(cpu_env);
9b2fadda 4325#endif /* defined(CONFIG_USER_ONLY) */
79aceca5
FB
4326}
4327
bf14b1ce 4328/* tlbiel */
99e300ef 4329static void gen_tlbiel(DisasContext *ctx)
bf14b1ce
BS
4330{
4331#if defined(CONFIG_USER_ONLY)
9b2fadda 4332 GEN_PRIV;
bf14b1ce 4333#else
9b2fadda
BH
4334 CHK_SV;
4335
c6c7cf05 4336 gen_helper_tlbie(cpu_env, cpu_gpr[rB(ctx->opcode)]);
9b2fadda 4337#endif /* defined(CONFIG_USER_ONLY) */
bf14b1ce
BS
4338}
4339
79aceca5 4340/* tlbie */
99e300ef 4341static void gen_tlbie(DisasContext *ctx)
79aceca5 4342{
9a64fbe4 4343#if defined(CONFIG_USER_ONLY)
9b2fadda 4344 GEN_PRIV;
9a64fbe4 4345#else
9b2fadda
BH
4346 CHK_HV;
4347
9ca3f7f3 4348 if (NARROW_MODE(ctx)) {
74d37793
AJ
4349 TCGv t0 = tcg_temp_new();
4350 tcg_gen_ext32u_tl(t0, cpu_gpr[rB(ctx->opcode)]);
c6c7cf05 4351 gen_helper_tlbie(cpu_env, t0);
74d37793 4352 tcg_temp_free(t0);
9ca3f7f3 4353 } else {
c6c7cf05 4354 gen_helper_tlbie(cpu_env, cpu_gpr[rB(ctx->opcode)]);
9ca3f7f3 4355 }
9b2fadda 4356#endif /* defined(CONFIG_USER_ONLY) */
79aceca5
FB
4357}
4358
4359/* tlbsync */
99e300ef 4360static void gen_tlbsync(DisasContext *ctx)
79aceca5 4361{
9a64fbe4 4362#if defined(CONFIG_USER_ONLY)
9b2fadda 4363 GEN_PRIV;
9a64fbe4 4364#else
9b2fadda
BH
4365 CHK_HV;
4366
cd0c6f47
BH
4367 /* tlbsync is a nop for server, ptesync handles delayed tlb flush,
4368 * embedded however needs to deal with tlbsync. We don't try to be
4369 * fancy and swallow the overhead of checking for both.
9a64fbe4 4370 */
cd0c6f47 4371 gen_check_tlb_flush(ctx);
9b2fadda 4372#endif /* defined(CONFIG_USER_ONLY) */
79aceca5
FB
4373}
4374
426613db
JM
4375#if defined(TARGET_PPC64)
4376/* slbia */
99e300ef 4377static void gen_slbia(DisasContext *ctx)
426613db
JM
4378{
4379#if defined(CONFIG_USER_ONLY)
9b2fadda 4380 GEN_PRIV;
426613db 4381#else
9b2fadda
BH
4382 CHK_SV;
4383
c6c7cf05 4384 gen_helper_slbia(cpu_env);
9b2fadda 4385#endif /* defined(CONFIG_USER_ONLY) */
426613db
JM
4386}
4387
4388/* slbie */
99e300ef 4389static void gen_slbie(DisasContext *ctx)
426613db
JM
4390{
4391#if defined(CONFIG_USER_ONLY)
9b2fadda 4392 GEN_PRIV;
426613db 4393#else
9b2fadda
BH
4394 CHK_SV;
4395
c6c7cf05 4396 gen_helper_slbie(cpu_env, cpu_gpr[rB(ctx->opcode)]);
9b2fadda 4397#endif /* defined(CONFIG_USER_ONLY) */
426613db 4398}
9b2fadda 4399#endif /* defined(TARGET_PPC64) */
426613db 4400
79aceca5
FB
4401/*** External control ***/
4402/* Optional: */
99e300ef 4403
54623277 4404/* eciwx */
99e300ef 4405static void gen_eciwx(DisasContext *ctx)
79aceca5 4406{
76db3ba4 4407 TCGv t0;
fa407c03 4408 /* Should check EAR[E] ! */
76db3ba4
AJ
4409 gen_set_access_type(ctx, ACCESS_EXT);
4410 t0 = tcg_temp_new();
4411 gen_addr_reg_index(ctx, t0);
fa407c03 4412 gen_check_align(ctx, t0, 0x03);
76db3ba4 4413 gen_qemu_ld32u(ctx, cpu_gpr[rD(ctx->opcode)], t0);
fa407c03 4414 tcg_temp_free(t0);
76a66253
JM
4415}
4416
4417/* ecowx */
99e300ef 4418static void gen_ecowx(DisasContext *ctx)
76a66253 4419{
76db3ba4 4420 TCGv t0;
fa407c03 4421 /* Should check EAR[E] ! */
76db3ba4
AJ
4422 gen_set_access_type(ctx, ACCESS_EXT);
4423 t0 = tcg_temp_new();
4424 gen_addr_reg_index(ctx, t0);
fa407c03 4425 gen_check_align(ctx, t0, 0x03);
76db3ba4 4426 gen_qemu_st32(ctx, cpu_gpr[rD(ctx->opcode)], t0);
fa407c03 4427 tcg_temp_free(t0);
76a66253
JM
4428}
4429
4430/* PowerPC 601 specific instructions */
99e300ef 4431
54623277 4432/* abs - abs. */
99e300ef 4433static void gen_abs(DisasContext *ctx)
76a66253 4434{
42a268c2
RH
4435 TCGLabel *l1 = gen_new_label();
4436 TCGLabel *l2 = gen_new_label();
22e0e173
AJ
4437 tcg_gen_brcondi_tl(TCG_COND_GE, cpu_gpr[rA(ctx->opcode)], 0, l1);
4438 tcg_gen_neg_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)]);
4439 tcg_gen_br(l2);
4440 gen_set_label(l1);
4441 tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)]);
4442 gen_set_label(l2);
76a66253 4443 if (unlikely(Rc(ctx->opcode) != 0))
22e0e173 4444 gen_set_Rc0(ctx, cpu_gpr[rD(ctx->opcode)]);
76a66253
JM
4445}
4446
4447/* abso - abso. */
99e300ef 4448static void gen_abso(DisasContext *ctx)
76a66253 4449{
42a268c2
RH
4450 TCGLabel *l1 = gen_new_label();
4451 TCGLabel *l2 = gen_new_label();
4452 TCGLabel *l3 = gen_new_label();
22e0e173 4453 /* Start with XER OV disabled, the most likely case */
da91a00f 4454 tcg_gen_movi_tl(cpu_ov, 0);
22e0e173
AJ
4455 tcg_gen_brcondi_tl(TCG_COND_GE, cpu_gpr[rA(ctx->opcode)], 0, l2);
4456 tcg_gen_brcondi_tl(TCG_COND_NE, cpu_gpr[rA(ctx->opcode)], 0x80000000, l1);
da91a00f
RH
4457 tcg_gen_movi_tl(cpu_ov, 1);
4458 tcg_gen_movi_tl(cpu_so, 1);
22e0e173
AJ
4459 tcg_gen_br(l2);
4460 gen_set_label(l1);
4461 tcg_gen_neg_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)]);
4462 tcg_gen_br(l3);
4463 gen_set_label(l2);
4464 tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)]);
4465 gen_set_label(l3);
76a66253 4466 if (unlikely(Rc(ctx->opcode) != 0))
22e0e173 4467 gen_set_Rc0(ctx, cpu_gpr[rD(ctx->opcode)]);
76a66253
JM
4468}
4469
4470/* clcs */
99e300ef 4471static void gen_clcs(DisasContext *ctx)
76a66253 4472{
22e0e173 4473 TCGv_i32 t0 = tcg_const_i32(rA(ctx->opcode));
d523dd00 4474 gen_helper_clcs(cpu_gpr[rD(ctx->opcode)], cpu_env, t0);
22e0e173 4475 tcg_temp_free_i32(t0);
c7697e1f 4476 /* Rc=1 sets CR0 to an undefined state */
76a66253
JM
4477}
4478
4479/* div - div. */
99e300ef 4480static void gen_div(DisasContext *ctx)
76a66253 4481{
d15f74fb
BS
4482 gen_helper_div(cpu_gpr[rD(ctx->opcode)], cpu_env, cpu_gpr[rA(ctx->opcode)],
4483 cpu_gpr[rB(ctx->opcode)]);
76a66253 4484 if (unlikely(Rc(ctx->opcode) != 0))
22e0e173 4485 gen_set_Rc0(ctx, cpu_gpr[rD(ctx->opcode)]);
76a66253
JM
4486}
4487
4488/* divo - divo. */
99e300ef 4489static void gen_divo(DisasContext *ctx)
76a66253 4490{
d15f74fb
BS
4491 gen_helper_divo(cpu_gpr[rD(ctx->opcode)], cpu_env, cpu_gpr[rA(ctx->opcode)],
4492 cpu_gpr[rB(ctx->opcode)]);
76a66253 4493 if (unlikely(Rc(ctx->opcode) != 0))
22e0e173 4494 gen_set_Rc0(ctx, cpu_gpr[rD(ctx->opcode)]);
76a66253
JM
4495}
4496
4497/* divs - divs. */
99e300ef 4498static void gen_divs(DisasContext *ctx)
76a66253 4499{
d15f74fb
BS
4500 gen_helper_divs(cpu_gpr[rD(ctx->opcode)], cpu_env, cpu_gpr[rA(ctx->opcode)],
4501 cpu_gpr[rB(ctx->opcode)]);
76a66253 4502 if (unlikely(Rc(ctx->opcode) != 0))
22e0e173 4503 gen_set_Rc0(ctx, cpu_gpr[rD(ctx->opcode)]);
76a66253
JM
4504}
4505
4506/* divso - divso. */
99e300ef 4507static void gen_divso(DisasContext *ctx)
76a66253 4508{
d15f74fb
BS
4509 gen_helper_divso(cpu_gpr[rD(ctx->opcode)], cpu_env,
4510 cpu_gpr[rA(ctx->opcode)], cpu_gpr[rB(ctx->opcode)]);
76a66253 4511 if (unlikely(Rc(ctx->opcode) != 0))
22e0e173 4512 gen_set_Rc0(ctx, cpu_gpr[rD(ctx->opcode)]);
76a66253
JM
4513}
4514
4515/* doz - doz. */
99e300ef 4516static void gen_doz(DisasContext *ctx)
76a66253 4517{
42a268c2
RH
4518 TCGLabel *l1 = gen_new_label();
4519 TCGLabel *l2 = gen_new_label();
22e0e173
AJ
4520 tcg_gen_brcond_tl(TCG_COND_GE, cpu_gpr[rB(ctx->opcode)], cpu_gpr[rA(ctx->opcode)], l1);
4521 tcg_gen_sub_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rB(ctx->opcode)], cpu_gpr[rA(ctx->opcode)]);
4522 tcg_gen_br(l2);
4523 gen_set_label(l1);
4524 tcg_gen_movi_tl(cpu_gpr[rD(ctx->opcode)], 0);
4525 gen_set_label(l2);
76a66253 4526 if (unlikely(Rc(ctx->opcode) != 0))
22e0e173 4527 gen_set_Rc0(ctx, cpu_gpr[rD(ctx->opcode)]);
76a66253
JM
4528}
4529
4530/* dozo - dozo. */
99e300ef 4531static void gen_dozo(DisasContext *ctx)
76a66253 4532{
42a268c2
RH
4533 TCGLabel *l1 = gen_new_label();
4534 TCGLabel *l2 = gen_new_label();
22e0e173
AJ
4535 TCGv t0 = tcg_temp_new();
4536 TCGv t1 = tcg_temp_new();
4537 TCGv t2 = tcg_temp_new();
4538 /* Start with XER OV disabled, the most likely case */
da91a00f 4539 tcg_gen_movi_tl(cpu_ov, 0);
22e0e173
AJ
4540 tcg_gen_brcond_tl(TCG_COND_GE, cpu_gpr[rB(ctx->opcode)], cpu_gpr[rA(ctx->opcode)], l1);
4541 tcg_gen_sub_tl(t0, cpu_gpr[rB(ctx->opcode)], cpu_gpr[rA(ctx->opcode)]);
4542 tcg_gen_xor_tl(t1, cpu_gpr[rB(ctx->opcode)], cpu_gpr[rA(ctx->opcode)]);
4543 tcg_gen_xor_tl(t2, cpu_gpr[rA(ctx->opcode)], t0);
4544 tcg_gen_andc_tl(t1, t1, t2);
4545 tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], t0);
4546 tcg_gen_brcondi_tl(TCG_COND_GE, t1, 0, l2);
da91a00f
RH
4547 tcg_gen_movi_tl(cpu_ov, 1);
4548 tcg_gen_movi_tl(cpu_so, 1);
22e0e173
AJ
4549 tcg_gen_br(l2);
4550 gen_set_label(l1);
4551 tcg_gen_movi_tl(cpu_gpr[rD(ctx->opcode)], 0);
4552 gen_set_label(l2);
4553 tcg_temp_free(t0);
4554 tcg_temp_free(t1);
4555 tcg_temp_free(t2);
76a66253 4556 if (unlikely(Rc(ctx->opcode) != 0))
22e0e173 4557 gen_set_Rc0(ctx, cpu_gpr[rD(ctx->opcode)]);
76a66253
JM
4558}
4559
4560/* dozi */
99e300ef 4561static void gen_dozi(DisasContext *ctx)
76a66253 4562{
22e0e173 4563 target_long simm = SIMM(ctx->opcode);
42a268c2
RH
4564 TCGLabel *l1 = gen_new_label();
4565 TCGLabel *l2 = gen_new_label();
22e0e173
AJ
4566 tcg_gen_brcondi_tl(TCG_COND_LT, cpu_gpr[rA(ctx->opcode)], simm, l1);
4567 tcg_gen_subfi_tl(cpu_gpr[rD(ctx->opcode)], simm, cpu_gpr[rA(ctx->opcode)]);
4568 tcg_gen_br(l2);
4569 gen_set_label(l1);
4570 tcg_gen_movi_tl(cpu_gpr[rD(ctx->opcode)], 0);
4571 gen_set_label(l2);
4572 if (unlikely(Rc(ctx->opcode) != 0))
4573 gen_set_Rc0(ctx, cpu_gpr[rD(ctx->opcode)]);
76a66253
JM
4574}
4575
76a66253 4576/* lscbx - lscbx. */
99e300ef 4577static void gen_lscbx(DisasContext *ctx)
76a66253 4578{
bdb4b689
AJ
4579 TCGv t0 = tcg_temp_new();
4580 TCGv_i32 t1 = tcg_const_i32(rD(ctx->opcode));
4581 TCGv_i32 t2 = tcg_const_i32(rA(ctx->opcode));
4582 TCGv_i32 t3 = tcg_const_i32(rB(ctx->opcode));
76a66253 4583
76db3ba4 4584 gen_addr_reg_index(ctx, t0);
2f5a189c 4585 gen_helper_lscbx(t0, cpu_env, t0, t1, t2, t3);
bdb4b689
AJ
4586 tcg_temp_free_i32(t1);
4587 tcg_temp_free_i32(t2);
4588 tcg_temp_free_i32(t3);
3d7b417e 4589 tcg_gen_andi_tl(cpu_xer, cpu_xer, ~0x7F);
bdb4b689 4590 tcg_gen_or_tl(cpu_xer, cpu_xer, t0);
76a66253 4591 if (unlikely(Rc(ctx->opcode) != 0))
bdb4b689
AJ
4592 gen_set_Rc0(ctx, t0);
4593 tcg_temp_free(t0);
76a66253
JM
4594}
4595
4596/* maskg - maskg. */
99e300ef 4597static void gen_maskg(DisasContext *ctx)
76a66253 4598{
42a268c2 4599 TCGLabel *l1 = gen_new_label();
22e0e173
AJ
4600 TCGv t0 = tcg_temp_new();
4601 TCGv t1 = tcg_temp_new();
4602 TCGv t2 = tcg_temp_new();
4603 TCGv t3 = tcg_temp_new();
4604 tcg_gen_movi_tl(t3, 0xFFFFFFFF);
4605 tcg_gen_andi_tl(t0, cpu_gpr[rB(ctx->opcode)], 0x1F);
4606 tcg_gen_andi_tl(t1, cpu_gpr[rS(ctx->opcode)], 0x1F);
4607 tcg_gen_addi_tl(t2, t0, 1);
4608 tcg_gen_shr_tl(t2, t3, t2);
4609 tcg_gen_shr_tl(t3, t3, t1);
4610 tcg_gen_xor_tl(cpu_gpr[rA(ctx->opcode)], t2, t3);
4611 tcg_gen_brcond_tl(TCG_COND_GE, t0, t1, l1);
4612 tcg_gen_neg_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rA(ctx->opcode)]);
4613 gen_set_label(l1);
4614 tcg_temp_free(t0);
4615 tcg_temp_free(t1);
4616 tcg_temp_free(t2);
4617 tcg_temp_free(t3);
76a66253 4618 if (unlikely(Rc(ctx->opcode) != 0))
22e0e173 4619 gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
76a66253
JM
4620}
4621
4622/* maskir - maskir. */
99e300ef 4623static void gen_maskir(DisasContext *ctx)
76a66253 4624{
22e0e173
AJ
4625 TCGv t0 = tcg_temp_new();
4626 TCGv t1 = tcg_temp_new();
4627 tcg_gen_and_tl(t0, cpu_gpr[rS(ctx->opcode)], cpu_gpr[rB(ctx->opcode)]);
4628 tcg_gen_andc_tl(t1, cpu_gpr[rA(ctx->opcode)], cpu_gpr[rB(ctx->opcode)]);
4629 tcg_gen_or_tl(cpu_gpr[rA(ctx->opcode)], t0, t1);
4630 tcg_temp_free(t0);
4631 tcg_temp_free(t1);
76a66253 4632 if (unlikely(Rc(ctx->opcode) != 0))
22e0e173 4633 gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
76a66253
JM
4634}
4635
4636/* mul - mul. */
99e300ef 4637static void gen_mul(DisasContext *ctx)
76a66253 4638{
22e0e173
AJ
4639 TCGv_i64 t0 = tcg_temp_new_i64();
4640 TCGv_i64 t1 = tcg_temp_new_i64();
4641 TCGv t2 = tcg_temp_new();
4642 tcg_gen_extu_tl_i64(t0, cpu_gpr[rA(ctx->opcode)]);
4643 tcg_gen_extu_tl_i64(t1, cpu_gpr[rB(ctx->opcode)]);
4644 tcg_gen_mul_i64(t0, t0, t1);
4645 tcg_gen_trunc_i64_tl(t2, t0);
4646 gen_store_spr(SPR_MQ, t2);
4647 tcg_gen_shri_i64(t1, t0, 32);
4648 tcg_gen_trunc_i64_tl(cpu_gpr[rD(ctx->opcode)], t1);
4649 tcg_temp_free_i64(t0);
4650 tcg_temp_free_i64(t1);
4651 tcg_temp_free(t2);
76a66253 4652 if (unlikely(Rc(ctx->opcode) != 0))
22e0e173 4653 gen_set_Rc0(ctx, cpu_gpr[rD(ctx->opcode)]);
76a66253
JM
4654}
4655
4656/* mulo - mulo. */
99e300ef 4657static void gen_mulo(DisasContext *ctx)
76a66253 4658{
42a268c2 4659 TCGLabel *l1 = gen_new_label();
22e0e173
AJ
4660 TCGv_i64 t0 = tcg_temp_new_i64();
4661 TCGv_i64 t1 = tcg_temp_new_i64();
4662 TCGv t2 = tcg_temp_new();
4663 /* Start with XER OV disabled, the most likely case */
da91a00f 4664 tcg_gen_movi_tl(cpu_ov, 0);
22e0e173
AJ
4665 tcg_gen_extu_tl_i64(t0, cpu_gpr[rA(ctx->opcode)]);
4666 tcg_gen_extu_tl_i64(t1, cpu_gpr[rB(ctx->opcode)]);
4667 tcg_gen_mul_i64(t0, t0, t1);
4668 tcg_gen_trunc_i64_tl(t2, t0);
4669 gen_store_spr(SPR_MQ, t2);
4670 tcg_gen_shri_i64(t1, t0, 32);
4671 tcg_gen_trunc_i64_tl(cpu_gpr[rD(ctx->opcode)], t1);
4672 tcg_gen_ext32s_i64(t1, t0);
4673 tcg_gen_brcond_i64(TCG_COND_EQ, t0, t1, l1);
da91a00f
RH
4674 tcg_gen_movi_tl(cpu_ov, 1);
4675 tcg_gen_movi_tl(cpu_so, 1);
22e0e173
AJ
4676 gen_set_label(l1);
4677 tcg_temp_free_i64(t0);
4678 tcg_temp_free_i64(t1);
4679 tcg_temp_free(t2);
76a66253 4680 if (unlikely(Rc(ctx->opcode) != 0))
22e0e173 4681 gen_set_Rc0(ctx, cpu_gpr[rD(ctx->opcode)]);
76a66253
JM
4682}
4683
4684/* nabs - nabs. */
99e300ef 4685static void gen_nabs(DisasContext *ctx)
76a66253 4686{
42a268c2
RH
4687 TCGLabel *l1 = gen_new_label();
4688 TCGLabel *l2 = gen_new_label();
22e0e173
AJ
4689 tcg_gen_brcondi_tl(TCG_COND_GT, cpu_gpr[rA(ctx->opcode)], 0, l1);
4690 tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)]);
4691 tcg_gen_br(l2);
4692 gen_set_label(l1);
4693 tcg_gen_neg_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)]);
4694 gen_set_label(l2);
76a66253 4695 if (unlikely(Rc(ctx->opcode) != 0))
22e0e173 4696 gen_set_Rc0(ctx, cpu_gpr[rD(ctx->opcode)]);
76a66253
JM
4697}
4698
4699/* nabso - nabso. */
99e300ef 4700static void gen_nabso(DisasContext *ctx)
76a66253 4701{
42a268c2
RH
4702 TCGLabel *l1 = gen_new_label();
4703 TCGLabel *l2 = gen_new_label();
22e0e173
AJ
4704 tcg_gen_brcondi_tl(TCG_COND_GT, cpu_gpr[rA(ctx->opcode)], 0, l1);
4705 tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)]);
4706 tcg_gen_br(l2);
4707 gen_set_label(l1);
4708 tcg_gen_neg_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)]);
4709 gen_set_label(l2);
4710 /* nabs never overflows */
da91a00f 4711 tcg_gen_movi_tl(cpu_ov, 0);
76a66253 4712 if (unlikely(Rc(ctx->opcode) != 0))
22e0e173 4713 gen_set_Rc0(ctx, cpu_gpr[rD(ctx->opcode)]);
76a66253
JM
4714}
4715
4716/* rlmi - rlmi. */
99e300ef 4717static void gen_rlmi(DisasContext *ctx)
76a66253 4718{
7487953d
AJ
4719 uint32_t mb = MB(ctx->opcode);
4720 uint32_t me = ME(ctx->opcode);
4721 TCGv t0 = tcg_temp_new();
4722 tcg_gen_andi_tl(t0, cpu_gpr[rB(ctx->opcode)], 0x1F);
4723 tcg_gen_rotl_tl(t0, cpu_gpr[rS(ctx->opcode)], t0);
4724 tcg_gen_andi_tl(t0, t0, MASK(mb, me));
4725 tcg_gen_andi_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rA(ctx->opcode)], ~MASK(mb, me));
4726 tcg_gen_or_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rA(ctx->opcode)], t0);
4727 tcg_temp_free(t0);
76a66253 4728 if (unlikely(Rc(ctx->opcode) != 0))
7487953d 4729 gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
76a66253
JM
4730}
4731
4732/* rrib - rrib. */
99e300ef 4733static void gen_rrib(DisasContext *ctx)
76a66253 4734{
7487953d
AJ
4735 TCGv t0 = tcg_temp_new();
4736 TCGv t1 = tcg_temp_new();
4737 tcg_gen_andi_tl(t0, cpu_gpr[rB(ctx->opcode)], 0x1F);
4738 tcg_gen_movi_tl(t1, 0x80000000);
4739 tcg_gen_shr_tl(t1, t1, t0);
4740 tcg_gen_shr_tl(t0, cpu_gpr[rS(ctx->opcode)], t0);
4741 tcg_gen_and_tl(t0, t0, t1);
4742 tcg_gen_andc_tl(t1, cpu_gpr[rA(ctx->opcode)], t1);
4743 tcg_gen_or_tl(cpu_gpr[rA(ctx->opcode)], t0, t1);
4744 tcg_temp_free(t0);
4745 tcg_temp_free(t1);
76a66253 4746 if (unlikely(Rc(ctx->opcode) != 0))
7487953d 4747 gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
76a66253
JM
4748}
4749
4750/* sle - sle. */
99e300ef 4751static void gen_sle(DisasContext *ctx)
76a66253 4752{
7487953d
AJ
4753 TCGv t0 = tcg_temp_new();
4754 TCGv t1 = tcg_temp_new();
4755 tcg_gen_andi_tl(t1, cpu_gpr[rB(ctx->opcode)], 0x1F);
4756 tcg_gen_shl_tl(t0, cpu_gpr[rS(ctx->opcode)], t1);
4757 tcg_gen_subfi_tl(t1, 32, t1);
4758 tcg_gen_shr_tl(t1, cpu_gpr[rS(ctx->opcode)], t1);
4759 tcg_gen_or_tl(t1, t0, t1);
4760 tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], t0);
4761 gen_store_spr(SPR_MQ, t1);
4762 tcg_temp_free(t0);
4763 tcg_temp_free(t1);
76a66253 4764 if (unlikely(Rc(ctx->opcode) != 0))
7487953d 4765 gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
76a66253
JM
4766}
4767
4768/* sleq - sleq. */
99e300ef 4769static void gen_sleq(DisasContext *ctx)
76a66253 4770{
7487953d
AJ
4771 TCGv t0 = tcg_temp_new();
4772 TCGv t1 = tcg_temp_new();
4773 TCGv t2 = tcg_temp_new();
4774 tcg_gen_andi_tl(t0, cpu_gpr[rB(ctx->opcode)], 0x1F);
4775 tcg_gen_movi_tl(t2, 0xFFFFFFFF);
4776 tcg_gen_shl_tl(t2, t2, t0);
4777 tcg_gen_rotl_tl(t0, cpu_gpr[rS(ctx->opcode)], t0);
4778 gen_load_spr(t1, SPR_MQ);
4779 gen_store_spr(SPR_MQ, t0);
4780 tcg_gen_and_tl(t0, t0, t2);
4781 tcg_gen_andc_tl(t1, t1, t2);
4782 tcg_gen_or_tl(cpu_gpr[rA(ctx->opcode)], t0, t1);
4783 tcg_temp_free(t0);
4784 tcg_temp_free(t1);
4785 tcg_temp_free(t2);
76a66253 4786 if (unlikely(Rc(ctx->opcode) != 0))
7487953d 4787 gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
76a66253
JM
4788}
4789
4790/* sliq - sliq. */
99e300ef 4791static void gen_sliq(DisasContext *ctx)
76a66253 4792{
7487953d
AJ
4793 int sh = SH(ctx->opcode);
4794 TCGv t0 = tcg_temp_new();
4795 TCGv t1 = tcg_temp_new();
4796 tcg_gen_shli_tl(t0, cpu_gpr[rS(ctx->opcode)], sh);
4797 tcg_gen_shri_tl(t1, cpu_gpr[rS(ctx->opcode)], 32 - sh);
4798 tcg_gen_or_tl(t1, t0, t1);
4799 tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], t0);
4800 gen_store_spr(SPR_MQ, t1);
4801 tcg_temp_free(t0);
4802 tcg_temp_free(t1);
76a66253 4803 if (unlikely(Rc(ctx->opcode) != 0))
7487953d 4804 gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
76a66253
JM
4805}
4806
4807/* slliq - slliq. */
99e300ef 4808static void gen_slliq(DisasContext *ctx)
76a66253 4809{
7487953d
AJ
4810 int sh = SH(ctx->opcode);
4811 TCGv t0 = tcg_temp_new();
4812 TCGv t1 = tcg_temp_new();
4813 tcg_gen_rotli_tl(t0, cpu_gpr[rS(ctx->opcode)], sh);
4814 gen_load_spr(t1, SPR_MQ);
4815 gen_store_spr(SPR_MQ, t0);
4816 tcg_gen_andi_tl(t0, t0, (0xFFFFFFFFU << sh));
4817 tcg_gen_andi_tl(t1, t1, ~(0xFFFFFFFFU << sh));
4818 tcg_gen_or_tl(cpu_gpr[rA(ctx->opcode)], t0, t1);
4819 tcg_temp_free(t0);
4820 tcg_temp_free(t1);
76a66253 4821 if (unlikely(Rc(ctx->opcode) != 0))
7487953d 4822 gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
76a66253
JM
4823}
4824
4825/* sllq - sllq. */
99e300ef 4826static void gen_sllq(DisasContext *ctx)
76a66253 4827{
42a268c2
RH
4828 TCGLabel *l1 = gen_new_label();
4829 TCGLabel *l2 = gen_new_label();
7487953d
AJ
4830 TCGv t0 = tcg_temp_local_new();
4831 TCGv t1 = tcg_temp_local_new();
4832 TCGv t2 = tcg_temp_local_new();
4833 tcg_gen_andi_tl(t2, cpu_gpr[rB(ctx->opcode)], 0x1F);
4834 tcg_gen_movi_tl(t1, 0xFFFFFFFF);
4835 tcg_gen_shl_tl(t1, t1, t2);
4836 tcg_gen_andi_tl(t0, cpu_gpr[rB(ctx->opcode)], 0x20);
4837 tcg_gen_brcondi_tl(TCG_COND_EQ, t0, 0, l1);
4838 gen_load_spr(t0, SPR_MQ);
4839 tcg_gen_and_tl(cpu_gpr[rA(ctx->opcode)], t0, t1);
4840 tcg_gen_br(l2);
4841 gen_set_label(l1);
4842 tcg_gen_shl_tl(t0, cpu_gpr[rS(ctx->opcode)], t2);
4843 gen_load_spr(t2, SPR_MQ);
4844 tcg_gen_andc_tl(t1, t2, t1);
4845 tcg_gen_or_tl(cpu_gpr[rA(ctx->opcode)], t0, t1);
4846 gen_set_label(l2);
4847 tcg_temp_free(t0);
4848 tcg_temp_free(t1);
4849 tcg_temp_free(t2);
76a66253 4850 if (unlikely(Rc(ctx->opcode) != 0))
7487953d 4851 gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
76a66253
JM
4852}
4853
4854/* slq - slq. */
99e300ef 4855static void gen_slq(DisasContext *ctx)
76a66253 4856{
42a268c2 4857 TCGLabel *l1 = gen_new_label();
7487953d
AJ
4858 TCGv t0 = tcg_temp_new();
4859 TCGv t1 = tcg_temp_new();
4860 tcg_gen_andi_tl(t1, cpu_gpr[rB(ctx->opcode)], 0x1F);
4861 tcg_gen_shl_tl(t0, cpu_gpr[rS(ctx->opcode)], t1);
4862 tcg_gen_subfi_tl(t1, 32, t1);
4863 tcg_gen_shr_tl(t1, cpu_gpr[rS(ctx->opcode)], t1);
4864 tcg_gen_or_tl(t1, t0, t1);
4865 gen_store_spr(SPR_MQ, t1);
4866 tcg_gen_andi_tl(t1, cpu_gpr[rB(ctx->opcode)], 0x20);
4867 tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], t0);
4868 tcg_gen_brcondi_tl(TCG_COND_EQ, t1, 0, l1);
4869 tcg_gen_movi_tl(cpu_gpr[rA(ctx->opcode)], 0);
4870 gen_set_label(l1);
4871 tcg_temp_free(t0);
4872 tcg_temp_free(t1);
76a66253 4873 if (unlikely(Rc(ctx->opcode) != 0))
7487953d 4874 gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
76a66253
JM
4875}
4876
d9bce9d9 4877/* sraiq - sraiq. */
99e300ef 4878static void gen_sraiq(DisasContext *ctx)
76a66253 4879{
7487953d 4880 int sh = SH(ctx->opcode);
42a268c2 4881 TCGLabel *l1 = gen_new_label();
7487953d
AJ
4882 TCGv t0 = tcg_temp_new();
4883 TCGv t1 = tcg_temp_new();
4884 tcg_gen_shri_tl(t0, cpu_gpr[rS(ctx->opcode)], sh);
4885 tcg_gen_shli_tl(t1, cpu_gpr[rS(ctx->opcode)], 32 - sh);
4886 tcg_gen_or_tl(t0, t0, t1);
4887 gen_store_spr(SPR_MQ, t0);
da91a00f 4888 tcg_gen_movi_tl(cpu_ca, 0);
7487953d
AJ
4889 tcg_gen_brcondi_tl(TCG_COND_EQ, t1, 0, l1);
4890 tcg_gen_brcondi_tl(TCG_COND_GE, cpu_gpr[rS(ctx->opcode)], 0, l1);
da91a00f 4891 tcg_gen_movi_tl(cpu_ca, 1);
7487953d
AJ
4892 gen_set_label(l1);
4893 tcg_gen_sari_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)], sh);
4894 tcg_temp_free(t0);
4895 tcg_temp_free(t1);
76a66253 4896 if (unlikely(Rc(ctx->opcode) != 0))
7487953d 4897 gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
76a66253
JM
4898}
4899
4900/* sraq - sraq. */
99e300ef 4901static void gen_sraq(DisasContext *ctx)
76a66253 4902{
42a268c2
RH
4903 TCGLabel *l1 = gen_new_label();
4904 TCGLabel *l2 = gen_new_label();
7487953d
AJ
4905 TCGv t0 = tcg_temp_new();
4906 TCGv t1 = tcg_temp_local_new();
4907 TCGv t2 = tcg_temp_local_new();
4908 tcg_gen_andi_tl(t2, cpu_gpr[rB(ctx->opcode)], 0x1F);
4909 tcg_gen_shr_tl(t0, cpu_gpr[rS(ctx->opcode)], t2);
4910 tcg_gen_sar_tl(t1, cpu_gpr[rS(ctx->opcode)], t2);
4911 tcg_gen_subfi_tl(t2, 32, t2);
4912 tcg_gen_shl_tl(t2, cpu_gpr[rS(ctx->opcode)], t2);
4913 tcg_gen_or_tl(t0, t0, t2);
4914 gen_store_spr(SPR_MQ, t0);
4915 tcg_gen_andi_tl(t0, cpu_gpr[rB(ctx->opcode)], 0x20);
4916 tcg_gen_brcondi_tl(TCG_COND_EQ, t2, 0, l1);
4917 tcg_gen_mov_tl(t2, cpu_gpr[rS(ctx->opcode)]);
4918 tcg_gen_sari_tl(t1, cpu_gpr[rS(ctx->opcode)], 31);
4919 gen_set_label(l1);
4920 tcg_temp_free(t0);
4921 tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], t1);
da91a00f 4922 tcg_gen_movi_tl(cpu_ca, 0);
7487953d
AJ
4923 tcg_gen_brcondi_tl(TCG_COND_GE, t1, 0, l2);
4924 tcg_gen_brcondi_tl(TCG_COND_EQ, t2, 0, l2);
da91a00f 4925 tcg_gen_movi_tl(cpu_ca, 1);
7487953d
AJ
4926 gen_set_label(l2);
4927 tcg_temp_free(t1);
4928 tcg_temp_free(t2);
76a66253 4929 if (unlikely(Rc(ctx->opcode) != 0))
7487953d 4930 gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
76a66253
JM
4931}
4932
4933/* sre - sre. */
99e300ef 4934static void gen_sre(DisasContext *ctx)
76a66253 4935{
7487953d
AJ
4936 TCGv t0 = tcg_temp_new();
4937 TCGv t1 = tcg_temp_new();
4938 tcg_gen_andi_tl(t1, cpu_gpr[rB(ctx->opcode)], 0x1F);
4939 tcg_gen_shr_tl(t0, cpu_gpr[rS(ctx->opcode)], t1);
4940 tcg_gen_subfi_tl(t1, 32, t1);
4941 tcg_gen_shl_tl(t1, cpu_gpr[rS(ctx->opcode)], t1);
4942 tcg_gen_or_tl(t1, t0, t1);
4943 tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], t0);
4944 gen_store_spr(SPR_MQ, t1);
4945 tcg_temp_free(t0);
4946 tcg_temp_free(t1);
76a66253 4947 if (unlikely(Rc(ctx->opcode) != 0))
7487953d 4948 gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
76a66253
JM
4949}
4950
4951/* srea - srea. */
99e300ef 4952static void gen_srea(DisasContext *ctx)
76a66253 4953{
7487953d
AJ
4954 TCGv t0 = tcg_temp_new();
4955 TCGv t1 = tcg_temp_new();
4956 tcg_gen_andi_tl(t1, cpu_gpr[rB(ctx->opcode)], 0x1F);
4957 tcg_gen_rotr_tl(t0, cpu_gpr[rS(ctx->opcode)], t1);
4958 gen_store_spr(SPR_MQ, t0);
4959 tcg_gen_sar_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)], t1);
4960 tcg_temp_free(t0);
4961 tcg_temp_free(t1);
76a66253 4962 if (unlikely(Rc(ctx->opcode) != 0))
7487953d 4963 gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
76a66253
JM
4964}
4965
4966/* sreq */
99e300ef 4967static void gen_sreq(DisasContext *ctx)
76a66253 4968{
7487953d
AJ
4969 TCGv t0 = tcg_temp_new();
4970 TCGv t1 = tcg_temp_new();
4971 TCGv t2 = tcg_temp_new();
4972 tcg_gen_andi_tl(t0, cpu_gpr[rB(ctx->opcode)], 0x1F);
4973 tcg_gen_movi_tl(t1, 0xFFFFFFFF);
4974 tcg_gen_shr_tl(t1, t1, t0);
4975 tcg_gen_rotr_tl(t0, cpu_gpr[rS(ctx->opcode)], t0);
4976 gen_load_spr(t2, SPR_MQ);
4977 gen_store_spr(SPR_MQ, t0);
4978 tcg_gen_and_tl(t0, t0, t1);
4979 tcg_gen_andc_tl(t2, t2, t1);
4980 tcg_gen_or_tl(cpu_gpr[rA(ctx->opcode)], t0, t2);
4981 tcg_temp_free(t0);
4982 tcg_temp_free(t1);
4983 tcg_temp_free(t2);
76a66253 4984 if (unlikely(Rc(ctx->opcode) != 0))
7487953d 4985 gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
76a66253
JM
4986}
4987
4988/* sriq */
99e300ef 4989static void gen_sriq(DisasContext *ctx)
76a66253 4990{
7487953d
AJ
4991 int sh = SH(ctx->opcode);
4992 TCGv t0 = tcg_temp_new();
4993 TCGv t1 = tcg_temp_new();
4994 tcg_gen_shri_tl(t0, cpu_gpr[rS(ctx->opcode)], sh);
4995 tcg_gen_shli_tl(t1, cpu_gpr[rS(ctx->opcode)], 32 - sh);
4996 tcg_gen_or_tl(t1, t0, t1);
4997 tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], t0);
4998 gen_store_spr(SPR_MQ, t1);
4999 tcg_temp_free(t0);
5000 tcg_temp_free(t1);
76a66253 5001 if (unlikely(Rc(ctx->opcode) != 0))
7487953d 5002 gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
76a66253
JM
5003}
5004
5005/* srliq */
99e300ef 5006static void gen_srliq(DisasContext *ctx)
76a66253 5007{
7487953d
AJ
5008 int sh = SH(ctx->opcode);
5009 TCGv t0 = tcg_temp_new();
5010 TCGv t1 = tcg_temp_new();
5011 tcg_gen_rotri_tl(t0, cpu_gpr[rS(ctx->opcode)], sh);
5012 gen_load_spr(t1, SPR_MQ);
5013 gen_store_spr(SPR_MQ, t0);
5014 tcg_gen_andi_tl(t0, t0, (0xFFFFFFFFU >> sh));
5015 tcg_gen_andi_tl(t1, t1, ~(0xFFFFFFFFU >> sh));
5016 tcg_gen_or_tl(cpu_gpr[rA(ctx->opcode)], t0, t1);
5017 tcg_temp_free(t0);
5018 tcg_temp_free(t1);
76a66253 5019 if (unlikely(Rc(ctx->opcode) != 0))
7487953d 5020 gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
76a66253
JM
5021}
5022
5023/* srlq */
99e300ef 5024static void gen_srlq(DisasContext *ctx)
76a66253 5025{
42a268c2
RH
5026 TCGLabel *l1 = gen_new_label();
5027 TCGLabel *l2 = gen_new_label();
7487953d
AJ
5028 TCGv t0 = tcg_temp_local_new();
5029 TCGv t1 = tcg_temp_local_new();
5030 TCGv t2 = tcg_temp_local_new();
5031 tcg_gen_andi_tl(t2, cpu_gpr[rB(ctx->opcode)], 0x1F);
5032 tcg_gen_movi_tl(t1, 0xFFFFFFFF);
5033 tcg_gen_shr_tl(t2, t1, t2);
5034 tcg_gen_andi_tl(t0, cpu_gpr[rB(ctx->opcode)], 0x20);
5035 tcg_gen_brcondi_tl(TCG_COND_EQ, t0, 0, l1);
5036 gen_load_spr(t0, SPR_MQ);
5037 tcg_gen_and_tl(cpu_gpr[rA(ctx->opcode)], t0, t2);
5038 tcg_gen_br(l2);
5039 gen_set_label(l1);
5040 tcg_gen_shr_tl(t0, cpu_gpr[rS(ctx->opcode)], t2);
5041 tcg_gen_and_tl(t0, t0, t2);
5042 gen_load_spr(t1, SPR_MQ);
5043 tcg_gen_andc_tl(t1, t1, t2);
5044 tcg_gen_or_tl(cpu_gpr[rA(ctx->opcode)], t0, t1);
5045 gen_set_label(l2);
5046 tcg_temp_free(t0);
5047 tcg_temp_free(t1);
5048 tcg_temp_free(t2);
76a66253 5049 if (unlikely(Rc(ctx->opcode) != 0))
7487953d 5050 gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
76a66253
JM
5051}
5052
5053/* srq */
99e300ef 5054static void gen_srq(DisasContext *ctx)
76a66253 5055{
42a268c2 5056 TCGLabel *l1 = gen_new_label();
7487953d
AJ
5057 TCGv t0 = tcg_temp_new();
5058 TCGv t1 = tcg_temp_new();
5059 tcg_gen_andi_tl(t1, cpu_gpr[rB(ctx->opcode)], 0x1F);
5060 tcg_gen_shr_tl(t0, cpu_gpr[rS(ctx->opcode)], t1);
5061 tcg_gen_subfi_tl(t1, 32, t1);
5062 tcg_gen_shl_tl(t1, cpu_gpr[rS(ctx->opcode)], t1);
5063 tcg_gen_or_tl(t1, t0, t1);
5064 gen_store_spr(SPR_MQ, t1);
5065 tcg_gen_andi_tl(t1, cpu_gpr[rB(ctx->opcode)], 0x20);
5066 tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], t0);
5067 tcg_gen_brcondi_tl(TCG_COND_EQ, t0, 0, l1);
5068 tcg_gen_movi_tl(cpu_gpr[rA(ctx->opcode)], 0);
5069 gen_set_label(l1);
5070 tcg_temp_free(t0);
5071 tcg_temp_free(t1);
76a66253 5072 if (unlikely(Rc(ctx->opcode) != 0))
7487953d 5073 gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
76a66253
JM
5074}
5075
5076/* PowerPC 602 specific instructions */
99e300ef 5077
54623277 5078/* dsa */
99e300ef 5079static void gen_dsa(DisasContext *ctx)
76a66253
JM
5080{
5081 /* XXX: TODO */
e06fcd75 5082 gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL);
76a66253
JM
5083}
5084
5085/* esa */
99e300ef 5086static void gen_esa(DisasContext *ctx)
76a66253
JM
5087{
5088 /* XXX: TODO */
e06fcd75 5089 gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL);
76a66253
JM
5090}
5091
5092/* mfrom */
99e300ef 5093static void gen_mfrom(DisasContext *ctx)
76a66253
JM
5094{
5095#if defined(CONFIG_USER_ONLY)
9b2fadda 5096 GEN_PRIV;
76a66253 5097#else
9b2fadda 5098 CHK_SV;
cf02a65c 5099 gen_helper_602_mfrom(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)]);
9b2fadda 5100#endif /* defined(CONFIG_USER_ONLY) */
76a66253
JM
5101}
5102
5103/* 602 - 603 - G2 TLB management */
e8eaa2c0 5104
54623277 5105/* tlbld */
e8eaa2c0 5106static void gen_tlbld_6xx(DisasContext *ctx)
76a66253
JM
5107{
5108#if defined(CONFIG_USER_ONLY)
9b2fadda 5109 GEN_PRIV;
76a66253 5110#else
9b2fadda 5111 CHK_SV;
c6c7cf05 5112 gen_helper_6xx_tlbd(cpu_env, cpu_gpr[rB(ctx->opcode)]);
9b2fadda 5113#endif /* defined(CONFIG_USER_ONLY) */
76a66253
JM
5114}
5115
5116/* tlbli */
e8eaa2c0 5117static void gen_tlbli_6xx(DisasContext *ctx)
76a66253
JM
5118{
5119#if defined(CONFIG_USER_ONLY)
9b2fadda 5120 GEN_PRIV;
76a66253 5121#else
9b2fadda 5122 CHK_SV;
c6c7cf05 5123 gen_helper_6xx_tlbi(cpu_env, cpu_gpr[rB(ctx->opcode)]);
9b2fadda 5124#endif /* defined(CONFIG_USER_ONLY) */
76a66253
JM
5125}
5126
7dbe11ac 5127/* 74xx TLB management */
e8eaa2c0 5128
54623277 5129/* tlbld */
e8eaa2c0 5130static void gen_tlbld_74xx(DisasContext *ctx)
7dbe11ac
JM
5131{
5132#if defined(CONFIG_USER_ONLY)
9b2fadda 5133 GEN_PRIV;
7dbe11ac 5134#else
9b2fadda 5135 CHK_SV;
c6c7cf05 5136 gen_helper_74xx_tlbd(cpu_env, cpu_gpr[rB(ctx->opcode)]);
9b2fadda 5137#endif /* defined(CONFIG_USER_ONLY) */
7dbe11ac
JM
5138}
5139
5140/* tlbli */
e8eaa2c0 5141static void gen_tlbli_74xx(DisasContext *ctx)
7dbe11ac
JM
5142{
5143#if defined(CONFIG_USER_ONLY)
9b2fadda 5144 GEN_PRIV;
7dbe11ac 5145#else
9b2fadda 5146 CHK_SV;
c6c7cf05 5147 gen_helper_74xx_tlbi(cpu_env, cpu_gpr[rB(ctx->opcode)]);
9b2fadda 5148#endif /* defined(CONFIG_USER_ONLY) */
7dbe11ac
JM
5149}
5150
76a66253 5151/* POWER instructions not in PowerPC 601 */
99e300ef 5152
54623277 5153/* clf */
99e300ef 5154static void gen_clf(DisasContext *ctx)
76a66253
JM
5155{
5156 /* Cache line flush: implemented as no-op */
5157}
5158
5159/* cli */
99e300ef 5160static void gen_cli(DisasContext *ctx)
76a66253 5161{
76a66253 5162#if defined(CONFIG_USER_ONLY)
9b2fadda 5163 GEN_PRIV;
76a66253 5164#else
9b2fadda
BH
5165 /* Cache line invalidate: privileged and treated as no-op */
5166 CHK_SV;
5167#endif /* defined(CONFIG_USER_ONLY) */
76a66253
JM
5168}
5169
5170/* dclst */
99e300ef 5171static void gen_dclst(DisasContext *ctx)
76a66253
JM
5172{
5173 /* Data cache line store: treated as no-op */
5174}
5175
99e300ef 5176static void gen_mfsri(DisasContext *ctx)
76a66253
JM
5177{
5178#if defined(CONFIG_USER_ONLY)
9b2fadda 5179 GEN_PRIV;
76a66253 5180#else
74d37793
AJ
5181 int ra = rA(ctx->opcode);
5182 int rd = rD(ctx->opcode);
5183 TCGv t0;
9b2fadda
BH
5184
5185 CHK_SV;
74d37793 5186 t0 = tcg_temp_new();
76db3ba4 5187 gen_addr_reg_index(ctx, t0);
74d37793
AJ
5188 tcg_gen_shri_tl(t0, t0, 28);
5189 tcg_gen_andi_tl(t0, t0, 0xF);
c6c7cf05 5190 gen_helper_load_sr(cpu_gpr[rd], cpu_env, t0);
74d37793 5191 tcg_temp_free(t0);
76a66253 5192 if (ra != 0 && ra != rd)
74d37793 5193 tcg_gen_mov_tl(cpu_gpr[ra], cpu_gpr[rd]);
9b2fadda 5194#endif /* defined(CONFIG_USER_ONLY) */
76a66253
JM
5195}
5196
99e300ef 5197static void gen_rac(DisasContext *ctx)
76a66253
JM
5198{
5199#if defined(CONFIG_USER_ONLY)
9b2fadda 5200 GEN_PRIV;
76a66253 5201#else
22e0e173 5202 TCGv t0;
9b2fadda
BH
5203
5204 CHK_SV;
22e0e173 5205 t0 = tcg_temp_new();
76db3ba4 5206 gen_addr_reg_index(ctx, t0);
c6c7cf05 5207 gen_helper_rac(cpu_gpr[rD(ctx->opcode)], cpu_env, t0);
22e0e173 5208 tcg_temp_free(t0);
9b2fadda 5209#endif /* defined(CONFIG_USER_ONLY) */
76a66253
JM
5210}
5211
99e300ef 5212static void gen_rfsvc(DisasContext *ctx)
76a66253
JM
5213{
5214#if defined(CONFIG_USER_ONLY)
9b2fadda 5215 GEN_PRIV;
76a66253 5216#else
9b2fadda
BH
5217 CHK_SV;
5218
e5f17ac6 5219 gen_helper_rfsvc(cpu_env);
e06fcd75 5220 gen_sync_exception(ctx);
9b2fadda 5221#endif /* defined(CONFIG_USER_ONLY) */
76a66253
JM
5222}
5223
f9651121 5224#include "translate/fp-impl.c"
76a66253 5225
0304af89
BH
5226#include "translate/vmx-impl.c"
5227
3014427a
BH
5228#include "translate/vsx-impl.c"
5229
f9651121 5230/* svc is not implemented for now */
76a66253
JM
5231
5232/* BookE specific instructions */
99e300ef 5233
54623277 5234/* XXX: not implemented on 440 ? */
99e300ef 5235static void gen_mfapidi(DisasContext *ctx)
76a66253
JM
5236{
5237 /* XXX: TODO */
e06fcd75 5238 gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL);
76a66253
JM
5239}
5240
2662a059 5241/* XXX: not implemented on 440 ? */
99e300ef 5242static void gen_tlbiva(DisasContext *ctx)
76a66253
JM
5243{
5244#if defined(CONFIG_USER_ONLY)
9b2fadda 5245 GEN_PRIV;
76a66253 5246#else
74d37793 5247 TCGv t0;
9b2fadda
BH
5248
5249 CHK_SV;
ec72e276 5250 t0 = tcg_temp_new();
76db3ba4 5251 gen_addr_reg_index(ctx, t0);
4693364f 5252 gen_helper_tlbiva(cpu_env, cpu_gpr[rB(ctx->opcode)]);
74d37793 5253 tcg_temp_free(t0);
9b2fadda 5254#endif /* defined(CONFIG_USER_ONLY) */
76a66253
JM
5255}
5256
5257/* All 405 MAC instructions are translated here */
636aa200
BS
5258static inline void gen_405_mulladd_insn(DisasContext *ctx, int opc2, int opc3,
5259 int ra, int rb, int rt, int Rc)
76a66253 5260{
182608d4
AJ
5261 TCGv t0, t1;
5262
a7812ae4
PB
5263 t0 = tcg_temp_local_new();
5264 t1 = tcg_temp_local_new();
182608d4 5265
76a66253
JM
5266 switch (opc3 & 0x0D) {
5267 case 0x05:
5268 /* macchw - macchw. - macchwo - macchwo. */
5269 /* macchws - macchws. - macchwso - macchwso. */
5270 /* nmacchw - nmacchw. - nmacchwo - nmacchwo. */
5271 /* nmacchws - nmacchws. - nmacchwso - nmacchwso. */
5272 /* mulchw - mulchw. */
182608d4
AJ
5273 tcg_gen_ext16s_tl(t0, cpu_gpr[ra]);
5274 tcg_gen_sari_tl(t1, cpu_gpr[rb], 16);
5275 tcg_gen_ext16s_tl(t1, t1);
76a66253
JM
5276 break;
5277 case 0x04:
5278 /* macchwu - macchwu. - macchwuo - macchwuo. */
5279 /* macchwsu - macchwsu. - macchwsuo - macchwsuo. */
5280 /* mulchwu - mulchwu. */
182608d4
AJ
5281 tcg_gen_ext16u_tl(t0, cpu_gpr[ra]);
5282 tcg_gen_shri_tl(t1, cpu_gpr[rb], 16);
5283 tcg_gen_ext16u_tl(t1, t1);
76a66253
JM
5284 break;
5285 case 0x01:
5286 /* machhw - machhw. - machhwo - machhwo. */
5287 /* machhws - machhws. - machhwso - machhwso. */
5288 /* nmachhw - nmachhw. - nmachhwo - nmachhwo. */
5289 /* nmachhws - nmachhws. - nmachhwso - nmachhwso. */
5290 /* mulhhw - mulhhw. */
182608d4
AJ
5291 tcg_gen_sari_tl(t0, cpu_gpr[ra], 16);
5292 tcg_gen_ext16s_tl(t0, t0);
5293 tcg_gen_sari_tl(t1, cpu_gpr[rb], 16);
5294 tcg_gen_ext16s_tl(t1, t1);
76a66253
JM
5295 break;
5296 case 0x00:
5297 /* machhwu - machhwu. - machhwuo - machhwuo. */
5298 /* machhwsu - machhwsu. - machhwsuo - machhwsuo. */
5299 /* mulhhwu - mulhhwu. */
182608d4
AJ
5300 tcg_gen_shri_tl(t0, cpu_gpr[ra], 16);
5301 tcg_gen_ext16u_tl(t0, t0);
5302 tcg_gen_shri_tl(t1, cpu_gpr[rb], 16);
5303 tcg_gen_ext16u_tl(t1, t1);
76a66253
JM
5304 break;
5305 case 0x0D:
5306 /* maclhw - maclhw. - maclhwo - maclhwo. */
5307 /* maclhws - maclhws. - maclhwso - maclhwso. */
5308 /* nmaclhw - nmaclhw. - nmaclhwo - nmaclhwo. */
5309 /* nmaclhws - nmaclhws. - nmaclhwso - nmaclhwso. */
5310 /* mullhw - mullhw. */
182608d4
AJ
5311 tcg_gen_ext16s_tl(t0, cpu_gpr[ra]);
5312 tcg_gen_ext16s_tl(t1, cpu_gpr[rb]);
76a66253
JM
5313 break;
5314 case 0x0C:
5315 /* maclhwu - maclhwu. - maclhwuo - maclhwuo. */
5316 /* maclhwsu - maclhwsu. - maclhwsuo - maclhwsuo. */
5317 /* mullhwu - mullhwu. */
182608d4
AJ
5318 tcg_gen_ext16u_tl(t0, cpu_gpr[ra]);
5319 tcg_gen_ext16u_tl(t1, cpu_gpr[rb]);
76a66253
JM
5320 break;
5321 }
76a66253 5322 if (opc2 & 0x04) {
182608d4
AJ
5323 /* (n)multiply-and-accumulate (0x0C / 0x0E) */
5324 tcg_gen_mul_tl(t1, t0, t1);
5325 if (opc2 & 0x02) {
5326 /* nmultiply-and-accumulate (0x0E) */
5327 tcg_gen_sub_tl(t0, cpu_gpr[rt], t1);
5328 } else {
5329 /* multiply-and-accumulate (0x0C) */
5330 tcg_gen_add_tl(t0, cpu_gpr[rt], t1);
5331 }
5332
5333 if (opc3 & 0x12) {
5334 /* Check overflow and/or saturate */
42a268c2 5335 TCGLabel *l1 = gen_new_label();
182608d4
AJ
5336
5337 if (opc3 & 0x10) {
5338 /* Start with XER OV disabled, the most likely case */
da91a00f 5339 tcg_gen_movi_tl(cpu_ov, 0);
182608d4
AJ
5340 }
5341 if (opc3 & 0x01) {
5342 /* Signed */
5343 tcg_gen_xor_tl(t1, cpu_gpr[rt], t1);
5344 tcg_gen_brcondi_tl(TCG_COND_GE, t1, 0, l1);
5345 tcg_gen_xor_tl(t1, cpu_gpr[rt], t0);
5346 tcg_gen_brcondi_tl(TCG_COND_LT, t1, 0, l1);
bdc4e053 5347 if (opc3 & 0x02) {
182608d4
AJ
5348 /* Saturate */
5349 tcg_gen_sari_tl(t0, cpu_gpr[rt], 31);
5350 tcg_gen_xori_tl(t0, t0, 0x7fffffff);
5351 }
5352 } else {
5353 /* Unsigned */
5354 tcg_gen_brcond_tl(TCG_COND_GEU, t0, t1, l1);
bdc4e053 5355 if (opc3 & 0x02) {
182608d4
AJ
5356 /* Saturate */
5357 tcg_gen_movi_tl(t0, UINT32_MAX);
5358 }
5359 }
5360 if (opc3 & 0x10) {
5361 /* Check overflow */
da91a00f
RH
5362 tcg_gen_movi_tl(cpu_ov, 1);
5363 tcg_gen_movi_tl(cpu_so, 1);
182608d4
AJ
5364 }
5365 gen_set_label(l1);
5366 tcg_gen_mov_tl(cpu_gpr[rt], t0);
5367 }
5368 } else {
5369 tcg_gen_mul_tl(cpu_gpr[rt], t0, t1);
76a66253 5370 }
182608d4
AJ
5371 tcg_temp_free(t0);
5372 tcg_temp_free(t1);
76a66253
JM
5373 if (unlikely(Rc) != 0) {
5374 /* Update Rc0 */
182608d4 5375 gen_set_Rc0(ctx, cpu_gpr[rt]);
76a66253
JM
5376 }
5377}
5378
a750fc0b 5379#define GEN_MAC_HANDLER(name, opc2, opc3) \
99e300ef 5380static void glue(gen_, name)(DisasContext *ctx) \
76a66253
JM
5381{ \
5382 gen_405_mulladd_insn(ctx, opc2, opc3, rA(ctx->opcode), rB(ctx->opcode), \
5383 rD(ctx->opcode), Rc(ctx->opcode)); \
5384}
5385
5386/* macchw - macchw. */
a750fc0b 5387GEN_MAC_HANDLER(macchw, 0x0C, 0x05);
76a66253 5388/* macchwo - macchwo. */
a750fc0b 5389GEN_MAC_HANDLER(macchwo, 0x0C, 0x15);
76a66253 5390/* macchws - macchws. */
a750fc0b 5391GEN_MAC_HANDLER(macchws, 0x0C, 0x07);
76a66253 5392/* macchwso - macchwso. */
a750fc0b 5393GEN_MAC_HANDLER(macchwso, 0x0C, 0x17);
76a66253 5394/* macchwsu - macchwsu. */
a750fc0b 5395GEN_MAC_HANDLER(macchwsu, 0x0C, 0x06);
76a66253 5396/* macchwsuo - macchwsuo. */
a750fc0b 5397GEN_MAC_HANDLER(macchwsuo, 0x0C, 0x16);
76a66253 5398/* macchwu - macchwu. */
a750fc0b 5399GEN_MAC_HANDLER(macchwu, 0x0C, 0x04);
76a66253 5400/* macchwuo - macchwuo. */
a750fc0b 5401GEN_MAC_HANDLER(macchwuo, 0x0C, 0x14);
76a66253 5402/* machhw - machhw. */
a750fc0b 5403GEN_MAC_HANDLER(machhw, 0x0C, 0x01);
76a66253 5404/* machhwo - machhwo. */
a750fc0b 5405GEN_MAC_HANDLER(machhwo, 0x0C, 0x11);
76a66253 5406/* machhws - machhws. */
a750fc0b 5407GEN_MAC_HANDLER(machhws, 0x0C, 0x03);
76a66253 5408/* machhwso - machhwso. */
a750fc0b 5409GEN_MAC_HANDLER(machhwso, 0x0C, 0x13);
76a66253 5410/* machhwsu - machhwsu. */
a750fc0b 5411GEN_MAC_HANDLER(machhwsu, 0x0C, 0x02);
76a66253 5412/* machhwsuo - machhwsuo. */
a750fc0b 5413GEN_MAC_HANDLER(machhwsuo, 0x0C, 0x12);
76a66253 5414/* machhwu - machhwu. */
a750fc0b 5415GEN_MAC_HANDLER(machhwu, 0x0C, 0x00);
76a66253 5416/* machhwuo - machhwuo. */
a750fc0b 5417GEN_MAC_HANDLER(machhwuo, 0x0C, 0x10);
76a66253 5418/* maclhw - maclhw. */
a750fc0b 5419GEN_MAC_HANDLER(maclhw, 0x0C, 0x0D);
76a66253 5420/* maclhwo - maclhwo. */
a750fc0b 5421GEN_MAC_HANDLER(maclhwo, 0x0C, 0x1D);
76a66253 5422/* maclhws - maclhws. */
a750fc0b 5423GEN_MAC_HANDLER(maclhws, 0x0C, 0x0F);
76a66253 5424/* maclhwso - maclhwso. */
a750fc0b 5425GEN_MAC_HANDLER(maclhwso, 0x0C, 0x1F);
76a66253 5426/* maclhwu - maclhwu. */
a750fc0b 5427GEN_MAC_HANDLER(maclhwu, 0x0C, 0x0C);
76a66253 5428/* maclhwuo - maclhwuo. */
a750fc0b 5429GEN_MAC_HANDLER(maclhwuo, 0x0C, 0x1C);
76a66253 5430/* maclhwsu - maclhwsu. */
a750fc0b 5431GEN_MAC_HANDLER(maclhwsu, 0x0C, 0x0E);
76a66253 5432/* maclhwsuo - maclhwsuo. */
a750fc0b 5433GEN_MAC_HANDLER(maclhwsuo, 0x0C, 0x1E);
76a66253 5434/* nmacchw - nmacchw. */
a750fc0b 5435GEN_MAC_HANDLER(nmacchw, 0x0E, 0x05);
76a66253 5436/* nmacchwo - nmacchwo. */
a750fc0b 5437GEN_MAC_HANDLER(nmacchwo, 0x0E, 0x15);
76a66253 5438/* nmacchws - nmacchws. */
a750fc0b 5439GEN_MAC_HANDLER(nmacchws, 0x0E, 0x07);
76a66253 5440/* nmacchwso - nmacchwso. */
a750fc0b 5441GEN_MAC_HANDLER(nmacchwso, 0x0E, 0x17);
76a66253 5442/* nmachhw - nmachhw. */
a750fc0b 5443GEN_MAC_HANDLER(nmachhw, 0x0E, 0x01);
76a66253 5444/* nmachhwo - nmachhwo. */
a750fc0b 5445GEN_MAC_HANDLER(nmachhwo, 0x0E, 0x11);
76a66253 5446/* nmachhws - nmachhws. */
a750fc0b 5447GEN_MAC_HANDLER(nmachhws, 0x0E, 0x03);
76a66253 5448/* nmachhwso - nmachhwso. */
a750fc0b 5449GEN_MAC_HANDLER(nmachhwso, 0x0E, 0x13);
76a66253 5450/* nmaclhw - nmaclhw. */
a750fc0b 5451GEN_MAC_HANDLER(nmaclhw, 0x0E, 0x0D);
76a66253 5452/* nmaclhwo - nmaclhwo. */
a750fc0b 5453GEN_MAC_HANDLER(nmaclhwo, 0x0E, 0x1D);
76a66253 5454/* nmaclhws - nmaclhws. */
a750fc0b 5455GEN_MAC_HANDLER(nmaclhws, 0x0E, 0x0F);
76a66253 5456/* nmaclhwso - nmaclhwso. */
a750fc0b 5457GEN_MAC_HANDLER(nmaclhwso, 0x0E, 0x1F);
76a66253
JM
5458
5459/* mulchw - mulchw. */
a750fc0b 5460GEN_MAC_HANDLER(mulchw, 0x08, 0x05);
76a66253 5461/* mulchwu - mulchwu. */
a750fc0b 5462GEN_MAC_HANDLER(mulchwu, 0x08, 0x04);
76a66253 5463/* mulhhw - mulhhw. */
a750fc0b 5464GEN_MAC_HANDLER(mulhhw, 0x08, 0x01);
76a66253 5465/* mulhhwu - mulhhwu. */
a750fc0b 5466GEN_MAC_HANDLER(mulhhwu, 0x08, 0x00);
76a66253 5467/* mullhw - mullhw. */
a750fc0b 5468GEN_MAC_HANDLER(mullhw, 0x08, 0x0D);
76a66253 5469/* mullhwu - mullhwu. */
a750fc0b 5470GEN_MAC_HANDLER(mullhwu, 0x08, 0x0C);
76a66253
JM
5471
5472/* mfdcr */
99e300ef 5473static void gen_mfdcr(DisasContext *ctx)
76a66253
JM
5474{
5475#if defined(CONFIG_USER_ONLY)
9b2fadda 5476 GEN_PRIV;
76a66253 5477#else
06dca6a7 5478 TCGv dcrn;
9b2fadda
BH
5479
5480 CHK_SV;
06dca6a7 5481 dcrn = tcg_const_tl(SPR(ctx->opcode));
d0f1562d 5482 gen_helper_load_dcr(cpu_gpr[rD(ctx->opcode)], cpu_env, dcrn);
06dca6a7 5483 tcg_temp_free(dcrn);
9b2fadda 5484#endif /* defined(CONFIG_USER_ONLY) */
76a66253
JM
5485}
5486
5487/* mtdcr */
99e300ef 5488static void gen_mtdcr(DisasContext *ctx)
76a66253
JM
5489{
5490#if defined(CONFIG_USER_ONLY)
9b2fadda 5491 GEN_PRIV;
76a66253 5492#else
06dca6a7 5493 TCGv dcrn;
9b2fadda
BH
5494
5495 CHK_SV;
06dca6a7 5496 dcrn = tcg_const_tl(SPR(ctx->opcode));
d0f1562d 5497 gen_helper_store_dcr(cpu_env, dcrn, cpu_gpr[rS(ctx->opcode)]);
06dca6a7 5498 tcg_temp_free(dcrn);
9b2fadda 5499#endif /* defined(CONFIG_USER_ONLY) */
a42bd6cc
JM
5500}
5501
5502/* mfdcrx */
2662a059 5503/* XXX: not implemented on 440 ? */
99e300ef 5504static void gen_mfdcrx(DisasContext *ctx)
a42bd6cc
JM
5505{
5506#if defined(CONFIG_USER_ONLY)
9b2fadda 5507 GEN_PRIV;
a42bd6cc 5508#else
9b2fadda 5509 CHK_SV;
d0f1562d
BS
5510 gen_helper_load_dcr(cpu_gpr[rD(ctx->opcode)], cpu_env,
5511 cpu_gpr[rA(ctx->opcode)]);
a750fc0b 5512 /* Note: Rc update flag set leads to undefined state of Rc0 */
9b2fadda 5513#endif /* defined(CONFIG_USER_ONLY) */
a42bd6cc
JM
5514}
5515
5516/* mtdcrx */
2662a059 5517/* XXX: not implemented on 440 ? */
99e300ef 5518static void gen_mtdcrx(DisasContext *ctx)
a42bd6cc
JM
5519{
5520#if defined(CONFIG_USER_ONLY)
9b2fadda 5521 GEN_PRIV;
a42bd6cc 5522#else
9b2fadda 5523 CHK_SV;
d0f1562d
BS
5524 gen_helper_store_dcr(cpu_env, cpu_gpr[rA(ctx->opcode)],
5525 cpu_gpr[rS(ctx->opcode)]);
a750fc0b 5526 /* Note: Rc update flag set leads to undefined state of Rc0 */
9b2fadda 5527#endif /* defined(CONFIG_USER_ONLY) */
76a66253
JM
5528}
5529
a750fc0b 5530/* mfdcrux (PPC 460) : user-mode access to DCR */
99e300ef 5531static void gen_mfdcrux(DisasContext *ctx)
a750fc0b 5532{
d0f1562d
BS
5533 gen_helper_load_dcr(cpu_gpr[rD(ctx->opcode)], cpu_env,
5534 cpu_gpr[rA(ctx->opcode)]);
a750fc0b
JM
5535 /* Note: Rc update flag set leads to undefined state of Rc0 */
5536}
5537
5538/* mtdcrux (PPC 460) : user-mode access to DCR */
99e300ef 5539static void gen_mtdcrux(DisasContext *ctx)
a750fc0b 5540{
975e5463 5541 gen_helper_store_dcr(cpu_env, cpu_gpr[rA(ctx->opcode)],
d0f1562d 5542 cpu_gpr[rS(ctx->opcode)]);
a750fc0b
JM
5543 /* Note: Rc update flag set leads to undefined state of Rc0 */
5544}
5545
76a66253 5546/* dccci */
99e300ef 5547static void gen_dccci(DisasContext *ctx)
76a66253 5548{
9b2fadda 5549 CHK_SV;
76a66253 5550 /* interpreted as no-op */
76a66253
JM
5551}
5552
5553/* dcread */
99e300ef 5554static void gen_dcread(DisasContext *ctx)
76a66253
JM
5555{
5556#if defined(CONFIG_USER_ONLY)
9b2fadda 5557 GEN_PRIV;
76a66253 5558#else
b61f2753 5559 TCGv EA, val;
9b2fadda
BH
5560
5561 CHK_SV;
76db3ba4 5562 gen_set_access_type(ctx, ACCESS_CACHE);
a7812ae4 5563 EA = tcg_temp_new();
76db3ba4 5564 gen_addr_reg_index(ctx, EA);
a7812ae4 5565 val = tcg_temp_new();
76db3ba4 5566 gen_qemu_ld32u(ctx, val, EA);
b61f2753
AJ
5567 tcg_temp_free(val);
5568 tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], EA);
5569 tcg_temp_free(EA);
9b2fadda 5570#endif /* defined(CONFIG_USER_ONLY) */
76a66253
JM
5571}
5572
5573/* icbt */
e8eaa2c0 5574static void gen_icbt_40x(DisasContext *ctx)
76a66253
JM
5575{
5576 /* interpreted as no-op */
5577 /* XXX: specification say this is treated as a load by the MMU
5578 * but does not generate any exception
5579 */
5580}
5581
5582/* iccci */
99e300ef 5583static void gen_iccci(DisasContext *ctx)
76a66253 5584{
9b2fadda 5585 CHK_SV;
76a66253 5586 /* interpreted as no-op */
76a66253
JM
5587}
5588
5589/* icread */
99e300ef 5590static void gen_icread(DisasContext *ctx)
76a66253 5591{
9b2fadda 5592 CHK_SV;
76a66253 5593 /* interpreted as no-op */
76a66253
JM
5594}
5595
c47493f2 5596/* rfci (supervisor only) */
e8eaa2c0 5597static void gen_rfci_40x(DisasContext *ctx)
a42bd6cc
JM
5598{
5599#if defined(CONFIG_USER_ONLY)
9b2fadda 5600 GEN_PRIV;
a42bd6cc 5601#else
9b2fadda 5602 CHK_SV;
a42bd6cc 5603 /* Restore CPU state */
e5f17ac6 5604 gen_helper_40x_rfci(cpu_env);
e06fcd75 5605 gen_sync_exception(ctx);
9b2fadda 5606#endif /* defined(CONFIG_USER_ONLY) */
a42bd6cc
JM
5607}
5608
99e300ef 5609static void gen_rfci(DisasContext *ctx)
a42bd6cc
JM
5610{
5611#if defined(CONFIG_USER_ONLY)
9b2fadda 5612 GEN_PRIV;
a42bd6cc 5613#else
9b2fadda 5614 CHK_SV;
a42bd6cc 5615 /* Restore CPU state */
e5f17ac6 5616 gen_helper_rfci(cpu_env);
e06fcd75 5617 gen_sync_exception(ctx);
9b2fadda 5618#endif /* defined(CONFIG_USER_ONLY) */
a42bd6cc
JM
5619}
5620
5621/* BookE specific */
99e300ef 5622
54623277 5623/* XXX: not implemented on 440 ? */
99e300ef 5624static void gen_rfdi(DisasContext *ctx)
76a66253
JM
5625{
5626#if defined(CONFIG_USER_ONLY)
9b2fadda 5627 GEN_PRIV;
76a66253 5628#else
9b2fadda 5629 CHK_SV;
76a66253 5630 /* Restore CPU state */
e5f17ac6 5631 gen_helper_rfdi(cpu_env);
e06fcd75 5632 gen_sync_exception(ctx);
9b2fadda 5633#endif /* defined(CONFIG_USER_ONLY) */
76a66253
JM
5634}
5635
2662a059 5636/* XXX: not implemented on 440 ? */
99e300ef 5637static void gen_rfmci(DisasContext *ctx)
a42bd6cc
JM
5638{
5639#if defined(CONFIG_USER_ONLY)
9b2fadda 5640 GEN_PRIV;
a42bd6cc 5641#else
9b2fadda 5642 CHK_SV;
a42bd6cc 5643 /* Restore CPU state */
e5f17ac6 5644 gen_helper_rfmci(cpu_env);
e06fcd75 5645 gen_sync_exception(ctx);
9b2fadda 5646#endif /* defined(CONFIG_USER_ONLY) */
a42bd6cc 5647}
5eb7995e 5648
d9bce9d9 5649/* TLB management - PowerPC 405 implementation */
e8eaa2c0 5650
54623277 5651/* tlbre */
e8eaa2c0 5652static void gen_tlbre_40x(DisasContext *ctx)
76a66253
JM
5653{
5654#if defined(CONFIG_USER_ONLY)
9b2fadda 5655 GEN_PRIV;
76a66253 5656#else
9b2fadda 5657 CHK_SV;
76a66253
JM
5658 switch (rB(ctx->opcode)) {
5659 case 0:
c6c7cf05
BS
5660 gen_helper_4xx_tlbre_hi(cpu_gpr[rD(ctx->opcode)], cpu_env,
5661 cpu_gpr[rA(ctx->opcode)]);
76a66253
JM
5662 break;
5663 case 1:
c6c7cf05
BS
5664 gen_helper_4xx_tlbre_lo(cpu_gpr[rD(ctx->opcode)], cpu_env,
5665 cpu_gpr[rA(ctx->opcode)]);
76a66253
JM
5666 break;
5667 default:
e06fcd75 5668 gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL);
76a66253 5669 break;
9a64fbe4 5670 }
9b2fadda 5671#endif /* defined(CONFIG_USER_ONLY) */
76a66253
JM
5672}
5673
d9bce9d9 5674/* tlbsx - tlbsx. */
e8eaa2c0 5675static void gen_tlbsx_40x(DisasContext *ctx)
76a66253
JM
5676{
5677#if defined(CONFIG_USER_ONLY)
9b2fadda 5678 GEN_PRIV;
76a66253 5679#else
74d37793 5680 TCGv t0;
9b2fadda
BH
5681
5682 CHK_SV;
74d37793 5683 t0 = tcg_temp_new();
76db3ba4 5684 gen_addr_reg_index(ctx, t0);
c6c7cf05 5685 gen_helper_4xx_tlbsx(cpu_gpr[rD(ctx->opcode)], cpu_env, t0);
74d37793
AJ
5686 tcg_temp_free(t0);
5687 if (Rc(ctx->opcode)) {
42a268c2 5688 TCGLabel *l1 = gen_new_label();
da91a00f 5689 tcg_gen_trunc_tl_i32(cpu_crf[0], cpu_so);
74d37793
AJ
5690 tcg_gen_brcondi_tl(TCG_COND_EQ, cpu_gpr[rD(ctx->opcode)], -1, l1);
5691 tcg_gen_ori_i32(cpu_crf[0], cpu_crf[0], 0x02);
5692 gen_set_label(l1);
5693 }
9b2fadda 5694#endif /* defined(CONFIG_USER_ONLY) */
79aceca5
FB
5695}
5696
76a66253 5697/* tlbwe */
e8eaa2c0 5698static void gen_tlbwe_40x(DisasContext *ctx)
79aceca5 5699{
76a66253 5700#if defined(CONFIG_USER_ONLY)
9b2fadda 5701 GEN_PRIV;
76a66253 5702#else
9b2fadda
BH
5703 CHK_SV;
5704
76a66253
JM
5705 switch (rB(ctx->opcode)) {
5706 case 0:
c6c7cf05
BS
5707 gen_helper_4xx_tlbwe_hi(cpu_env, cpu_gpr[rA(ctx->opcode)],
5708 cpu_gpr[rS(ctx->opcode)]);
76a66253
JM
5709 break;
5710 case 1:
c6c7cf05
BS
5711 gen_helper_4xx_tlbwe_lo(cpu_env, cpu_gpr[rA(ctx->opcode)],
5712 cpu_gpr[rS(ctx->opcode)]);
76a66253
JM
5713 break;
5714 default:
e06fcd75 5715 gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL);
76a66253 5716 break;
9a64fbe4 5717 }
9b2fadda 5718#endif /* defined(CONFIG_USER_ONLY) */
76a66253
JM
5719}
5720
a4bb6c3e 5721/* TLB management - PowerPC 440 implementation */
e8eaa2c0 5722
54623277 5723/* tlbre */
e8eaa2c0 5724static void gen_tlbre_440(DisasContext *ctx)
5eb7995e
JM
5725{
5726#if defined(CONFIG_USER_ONLY)
9b2fadda 5727 GEN_PRIV;
5eb7995e 5728#else
9b2fadda
BH
5729 CHK_SV;
5730
5eb7995e
JM
5731 switch (rB(ctx->opcode)) {
5732 case 0:
5eb7995e 5733 case 1:
5eb7995e 5734 case 2:
74d37793
AJ
5735 {
5736 TCGv_i32 t0 = tcg_const_i32(rB(ctx->opcode));
c6c7cf05
BS
5737 gen_helper_440_tlbre(cpu_gpr[rD(ctx->opcode)], cpu_env,
5738 t0, cpu_gpr[rA(ctx->opcode)]);
74d37793
AJ
5739 tcg_temp_free_i32(t0);
5740 }
5eb7995e
JM
5741 break;
5742 default:
e06fcd75 5743 gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL);
5eb7995e
JM
5744 break;
5745 }
9b2fadda 5746#endif /* defined(CONFIG_USER_ONLY) */
5eb7995e
JM
5747}
5748
5749/* tlbsx - tlbsx. */
e8eaa2c0 5750static void gen_tlbsx_440(DisasContext *ctx)
5eb7995e
JM
5751{
5752#if defined(CONFIG_USER_ONLY)
9b2fadda 5753 GEN_PRIV;
5eb7995e 5754#else
74d37793 5755 TCGv t0;
9b2fadda
BH
5756
5757 CHK_SV;
74d37793 5758 t0 = tcg_temp_new();
76db3ba4 5759 gen_addr_reg_index(ctx, t0);
c6c7cf05 5760 gen_helper_440_tlbsx(cpu_gpr[rD(ctx->opcode)], cpu_env, t0);
74d37793
AJ
5761 tcg_temp_free(t0);
5762 if (Rc(ctx->opcode)) {
42a268c2 5763 TCGLabel *l1 = gen_new_label();
da91a00f 5764 tcg_gen_trunc_tl_i32(cpu_crf[0], cpu_so);
74d37793
AJ
5765 tcg_gen_brcondi_tl(TCG_COND_EQ, cpu_gpr[rD(ctx->opcode)], -1, l1);
5766 tcg_gen_ori_i32(cpu_crf[0], cpu_crf[0], 0x02);
5767 gen_set_label(l1);
5768 }
9b2fadda 5769#endif /* defined(CONFIG_USER_ONLY) */
5eb7995e
JM
5770}
5771
5772/* tlbwe */
e8eaa2c0 5773static void gen_tlbwe_440(DisasContext *ctx)
5eb7995e
JM
5774{
5775#if defined(CONFIG_USER_ONLY)
9b2fadda 5776 GEN_PRIV;
5eb7995e 5777#else
9b2fadda 5778 CHK_SV;
5eb7995e
JM
5779 switch (rB(ctx->opcode)) {
5780 case 0:
5eb7995e 5781 case 1:
5eb7995e 5782 case 2:
74d37793
AJ
5783 {
5784 TCGv_i32 t0 = tcg_const_i32(rB(ctx->opcode));
c6c7cf05
BS
5785 gen_helper_440_tlbwe(cpu_env, t0, cpu_gpr[rA(ctx->opcode)],
5786 cpu_gpr[rS(ctx->opcode)]);
74d37793
AJ
5787 tcg_temp_free_i32(t0);
5788 }
5eb7995e
JM
5789 break;
5790 default:
e06fcd75 5791 gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL);
5eb7995e
JM
5792 break;
5793 }
9b2fadda 5794#endif /* defined(CONFIG_USER_ONLY) */
5eb7995e
JM
5795}
5796
01662f3e
AG
5797/* TLB management - PowerPC BookE 2.06 implementation */
5798
5799/* tlbre */
5800static void gen_tlbre_booke206(DisasContext *ctx)
5801{
9b2fadda
BH
5802 #if defined(CONFIG_USER_ONLY)
5803 GEN_PRIV;
01662f3e 5804#else
9b2fadda 5805 CHK_SV;
c6c7cf05 5806 gen_helper_booke206_tlbre(cpu_env);
9b2fadda 5807#endif /* defined(CONFIG_USER_ONLY) */
01662f3e
AG
5808}
5809
5810/* tlbsx - tlbsx. */
5811static void gen_tlbsx_booke206(DisasContext *ctx)
5812{
5813#if defined(CONFIG_USER_ONLY)
9b2fadda 5814 GEN_PRIV;
01662f3e
AG
5815#else
5816 TCGv t0;
01662f3e 5817
9b2fadda 5818 CHK_SV;
01662f3e
AG
5819 if (rA(ctx->opcode)) {
5820 t0 = tcg_temp_new();
5821 tcg_gen_mov_tl(t0, cpu_gpr[rD(ctx->opcode)]);
5822 } else {
5823 t0 = tcg_const_tl(0);
5824 }
5825
5826 tcg_gen_add_tl(t0, t0, cpu_gpr[rB(ctx->opcode)]);
c6c7cf05 5827 gen_helper_booke206_tlbsx(cpu_env, t0);
c80d1df5 5828 tcg_temp_free(t0);
9b2fadda 5829#endif /* defined(CONFIG_USER_ONLY) */
01662f3e
AG
5830}
5831
5832/* tlbwe */
5833static void gen_tlbwe_booke206(DisasContext *ctx)
5834{
5835#if defined(CONFIG_USER_ONLY)
9b2fadda 5836 GEN_PRIV;
01662f3e 5837#else
9b2fadda 5838 CHK_SV;
c6c7cf05 5839 gen_helper_booke206_tlbwe(cpu_env);
9b2fadda 5840#endif /* defined(CONFIG_USER_ONLY) */
01662f3e
AG
5841}
5842
5843static void gen_tlbivax_booke206(DisasContext *ctx)
5844{
5845#if defined(CONFIG_USER_ONLY)
9b2fadda 5846 GEN_PRIV;
01662f3e
AG
5847#else
5848 TCGv t0;
01662f3e 5849
9b2fadda 5850 CHK_SV;
01662f3e
AG
5851 t0 = tcg_temp_new();
5852 gen_addr_reg_index(ctx, t0);
c6c7cf05 5853 gen_helper_booke206_tlbivax(cpu_env, t0);
c80d1df5 5854 tcg_temp_free(t0);
9b2fadda 5855#endif /* defined(CONFIG_USER_ONLY) */
01662f3e
AG
5856}
5857
6d3db821
AG
5858static void gen_tlbilx_booke206(DisasContext *ctx)
5859{
5860#if defined(CONFIG_USER_ONLY)
9b2fadda 5861 GEN_PRIV;
6d3db821
AG
5862#else
5863 TCGv t0;
6d3db821 5864
9b2fadda 5865 CHK_SV;
6d3db821
AG
5866 t0 = tcg_temp_new();
5867 gen_addr_reg_index(ctx, t0);
5868
5869 switch((ctx->opcode >> 21) & 0x3) {
5870 case 0:
c6c7cf05 5871 gen_helper_booke206_tlbilx0(cpu_env, t0);
6d3db821
AG
5872 break;
5873 case 1:
c6c7cf05 5874 gen_helper_booke206_tlbilx1(cpu_env, t0);
6d3db821
AG
5875 break;
5876 case 3:
c6c7cf05 5877 gen_helper_booke206_tlbilx3(cpu_env, t0);
6d3db821
AG
5878 break;
5879 default:
5880 gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL);
5881 break;
5882 }
5883
5884 tcg_temp_free(t0);
9b2fadda 5885#endif /* defined(CONFIG_USER_ONLY) */
6d3db821
AG
5886}
5887
01662f3e 5888
76a66253 5889/* wrtee */
99e300ef 5890static void gen_wrtee(DisasContext *ctx)
76a66253
JM
5891{
5892#if defined(CONFIG_USER_ONLY)
9b2fadda 5893 GEN_PRIV;
76a66253 5894#else
6527f6ea 5895 TCGv t0;
9b2fadda
BH
5896
5897 CHK_SV;
6527f6ea
AJ
5898 t0 = tcg_temp_new();
5899 tcg_gen_andi_tl(t0, cpu_gpr[rD(ctx->opcode)], (1 << MSR_EE));
5900 tcg_gen_andi_tl(cpu_msr, cpu_msr, ~(1 << MSR_EE));
5901 tcg_gen_or_tl(cpu_msr, cpu_msr, t0);
5902 tcg_temp_free(t0);
dee96f6c
JM
5903 /* Stop translation to have a chance to raise an exception
5904 * if we just set msr_ee to 1
5905 */
e06fcd75 5906 gen_stop_exception(ctx);
9b2fadda 5907#endif /* defined(CONFIG_USER_ONLY) */
76a66253
JM
5908}
5909
5910/* wrteei */
99e300ef 5911static void gen_wrteei(DisasContext *ctx)
76a66253
JM
5912{
5913#if defined(CONFIG_USER_ONLY)
9b2fadda 5914 GEN_PRIV;
76a66253 5915#else
9b2fadda 5916 CHK_SV;
fbe73008 5917 if (ctx->opcode & 0x00008000) {
6527f6ea
AJ
5918 tcg_gen_ori_tl(cpu_msr, cpu_msr, (1 << MSR_EE));
5919 /* Stop translation to have a chance to raise an exception */
e06fcd75 5920 gen_stop_exception(ctx);
6527f6ea 5921 } else {
1b6e5f99 5922 tcg_gen_andi_tl(cpu_msr, cpu_msr, ~(1 << MSR_EE));
6527f6ea 5923 }
9b2fadda 5924#endif /* defined(CONFIG_USER_ONLY) */
76a66253
JM
5925}
5926
08e46e54 5927/* PowerPC 440 specific instructions */
99e300ef 5928
54623277 5929/* dlmzb */
99e300ef 5930static void gen_dlmzb(DisasContext *ctx)
76a66253 5931{
ef0d51af 5932 TCGv_i32 t0 = tcg_const_i32(Rc(ctx->opcode));
d15f74fb
BS
5933 gen_helper_dlmzb(cpu_gpr[rA(ctx->opcode)], cpu_env,
5934 cpu_gpr[rS(ctx->opcode)], cpu_gpr[rB(ctx->opcode)], t0);
ef0d51af 5935 tcg_temp_free_i32(t0);
76a66253
JM
5936}
5937
5938/* mbar replaces eieio on 440 */
99e300ef 5939static void gen_mbar(DisasContext *ctx)
76a66253
JM
5940{
5941 /* interpreted as no-op */
5942}
5943
5944/* msync replaces sync on 440 */
dcb2b9e1 5945static void gen_msync_4xx(DisasContext *ctx)
76a66253
JM
5946{
5947 /* interpreted as no-op */
5948}
5949
5950/* icbt */
e8eaa2c0 5951static void gen_icbt_440(DisasContext *ctx)
76a66253
JM
5952{
5953 /* interpreted as no-op */
5954 /* XXX: specification say this is treated as a load by the MMU
5955 * but does not generate any exception
5956 */
79aceca5
FB
5957}
5958
9e0b5cb1
AG
5959/* Embedded.Processor Control */
5960
5961static void gen_msgclr(DisasContext *ctx)
5962{
5963#if defined(CONFIG_USER_ONLY)
9b2fadda 5964 GEN_PRIV;
9e0b5cb1 5965#else
9b2fadda 5966 CHK_SV;
e5f17ac6 5967 gen_helper_msgclr(cpu_env, cpu_gpr[rB(ctx->opcode)]);
9b2fadda 5968#endif /* defined(CONFIG_USER_ONLY) */
9e0b5cb1
AG
5969}
5970
d5d11a39
AG
5971static void gen_msgsnd(DisasContext *ctx)
5972{
5973#if defined(CONFIG_USER_ONLY)
9b2fadda 5974 GEN_PRIV;
d5d11a39 5975#else
9b2fadda 5976 CHK_SV;
d5d11a39 5977 gen_helper_msgsnd(cpu_gpr[rB(ctx->opcode)]);
9b2fadda 5978#endif /* defined(CONFIG_USER_ONLY) */
d5d11a39
AG
5979}
5980
b04ae981 5981
aeeb044c
ND
5982#if defined(TARGET_PPC64)
5983static void gen_maddld(DisasContext *ctx)
5984{
5985 TCGv_i64 t1 = tcg_temp_new_i64();
5986
5987 tcg_gen_mul_i64(t1, cpu_gpr[rA(ctx->opcode)], cpu_gpr[rB(ctx->opcode)]);
5988 tcg_gen_add_i64(cpu_gpr[rD(ctx->opcode)], t1, cpu_gpr[rC(ctx->opcode)]);
5989 tcg_temp_free_i64(t1);
5990}
5f29cc82
ND
5991
5992/* maddhd maddhdu */
5993static void gen_maddhd_maddhdu(DisasContext *ctx)
5994{
5995 TCGv_i64 lo = tcg_temp_new_i64();
5996 TCGv_i64 hi = tcg_temp_new_i64();
5997 TCGv_i64 t1 = tcg_temp_new_i64();
5998
5999 if (Rc(ctx->opcode)) {
6000 tcg_gen_mulu2_i64(lo, hi, cpu_gpr[rA(ctx->opcode)],
6001 cpu_gpr[rB(ctx->opcode)]);
6002 tcg_gen_movi_i64(t1, 0);
6003 } else {
6004 tcg_gen_muls2_i64(lo, hi, cpu_gpr[rA(ctx->opcode)],
6005 cpu_gpr[rB(ctx->opcode)]);
6006 tcg_gen_sari_i64(t1, cpu_gpr[rC(ctx->opcode)], 63);
6007 }
6008 tcg_gen_add2_i64(t1, cpu_gpr[rD(ctx->opcode)], lo, hi,
6009 cpu_gpr[rC(ctx->opcode)], t1);
6010 tcg_temp_free_i64(lo);
6011 tcg_temp_free_i64(hi);
6012 tcg_temp_free_i64(t1);
6013}
aeeb044c
ND
6014#endif /* defined(TARGET_PPC64) */
6015
8b25cdd3 6016#include "translate/dfp-impl.c"
804e654a 6017
4083de6b 6018#include "translate/spe-impl.c"
0487d6a8 6019
0ff93d11
TM
6020static void gen_tbegin(DisasContext *ctx)
6021{
6022 if (unlikely(!ctx->tm_enabled)) {
6023 gen_exception_err(ctx, POWERPC_EXCP_FU, FSCR_IC_TM);
6024 return;
6025 }
6026 gen_helper_tbegin(cpu_env);
6027}
6028
56a84615
TM
6029#define GEN_TM_NOOP(name) \
6030static inline void gen_##name(DisasContext *ctx) \
6031{ \
6032 if (unlikely(!ctx->tm_enabled)) { \
6033 gen_exception_err(ctx, POWERPC_EXCP_FU, FSCR_IC_TM); \
6034 return; \
6035 } \
6036 /* Because tbegin always fails in QEMU, these user \
6037 * space instructions all have a simple implementation: \
6038 * \
6039 * CR[0] = 0b0 || MSR[TS] || 0b0 \
6040 * = 0b0 || 0b00 || 0b0 \
6041 */ \
6042 tcg_gen_movi_i32(cpu_crf[0], 0); \
6043}
6044
6045GEN_TM_NOOP(tend);
6046GEN_TM_NOOP(tabort);
6047GEN_TM_NOOP(tabortwc);
6048GEN_TM_NOOP(tabortwci);
6049GEN_TM_NOOP(tabortdc);
6050GEN_TM_NOOP(tabortdci);
6051GEN_TM_NOOP(tsr);
6052
aeedd582
TM
6053static void gen_tcheck(DisasContext *ctx)
6054{
6055 if (unlikely(!ctx->tm_enabled)) {
6056 gen_exception_err(ctx, POWERPC_EXCP_FU, FSCR_IC_TM);
6057 return;
6058 }
6059 /* Because tbegin always fails, the tcheck implementation
6060 * is simple:
6061 *
6062 * CR[CRF] = TDOOMED || MSR[TS] || 0b0
6063 * = 0b1 || 0b00 || 0b0
6064 */
6065 tcg_gen_movi_i32(cpu_crf[crfD(ctx->opcode)], 0x8);
6066}
6067
f83c2378
TM
6068#if defined(CONFIG_USER_ONLY)
6069#define GEN_TM_PRIV_NOOP(name) \
6070static inline void gen_##name(DisasContext *ctx) \
6071{ \
9b2fadda 6072 gen_priv_exception(ctx, POWERPC_EXCP_PRIV_OPC); \
f83c2378
TM
6073}
6074
6075#else
6076
6077#define GEN_TM_PRIV_NOOP(name) \
6078static inline void gen_##name(DisasContext *ctx) \
6079{ \
9b2fadda 6080 CHK_SV; \
f83c2378
TM
6081 if (unlikely(!ctx->tm_enabled)) { \
6082 gen_exception_err(ctx, POWERPC_EXCP_FU, FSCR_IC_TM); \
6083 return; \
6084 } \
6085 /* Because tbegin always fails, the implementation is \
6086 * simple: \
6087 * \
6088 * CR[0] = 0b0 || MSR[TS] || 0b0 \
6089 * = 0b0 || 0b00 | 0b0 \
6090 */ \
6091 tcg_gen_movi_i32(cpu_crf[0], 0); \
6092}
6093
6094#endif
6095
6096GEN_TM_PRIV_NOOP(treclaim);
6097GEN_TM_PRIV_NOOP(trechkpt);
6098
c227f099 6099static opcode_t opcodes[] = {
5c55ff99
BS
6100GEN_HANDLER(invalid, 0x00, 0x00, 0x00, 0xFFFFFFFF, PPC_NONE),
6101GEN_HANDLER(cmp, 0x1F, 0x00, 0x00, 0x00400000, PPC_INTEGER),
6102GEN_HANDLER(cmpi, 0x0B, 0xFF, 0xFF, 0x00400000, PPC_INTEGER),
6103GEN_HANDLER(cmpl, 0x1F, 0x00, 0x01, 0x00400000, PPC_INTEGER),
6104GEN_HANDLER(cmpli, 0x0A, 0xFF, 0xFF, 0x00400000, PPC_INTEGER),
082ce330
ND
6105#if defined(TARGET_PPC64)
6106GEN_HANDLER_E(cmpeqb, 0x1F, 0x00, 0x07, 0x00600000, PPC_NONE, PPC2_ISA300),
6107#endif
fcfda20f 6108GEN_HANDLER_E(cmpb, 0x1F, 0x1C, 0x0F, 0x00000001, PPC_NONE, PPC2_ISA205),
f2442ef9 6109GEN_HANDLER_E(cmprb, 0x1F, 0x00, 0x06, 0x00400001, PPC_NONE, PPC2_ISA300),
5c55ff99
BS
6110GEN_HANDLER(isel, 0x1F, 0x0F, 0xFF, 0x00000001, PPC_ISEL),
6111GEN_HANDLER(addi, 0x0E, 0xFF, 0xFF, 0x00000000, PPC_INTEGER),
6112GEN_HANDLER(addic, 0x0C, 0xFF, 0xFF, 0x00000000, PPC_INTEGER),
6113GEN_HANDLER2(addic_, "addic.", 0x0D, 0xFF, 0xFF, 0x00000000, PPC_INTEGER),
6114GEN_HANDLER(addis, 0x0F, 0xFF, 0xFF, 0x00000000, PPC_INTEGER),
c5b2b9ce 6115GEN_HANDLER_E(addpcis, 0x13, 0x2, 0xFF, 0x00000000, PPC_NONE, PPC2_ISA300),
5c55ff99
BS
6116GEN_HANDLER(mulhw, 0x1F, 0x0B, 0x02, 0x00000400, PPC_INTEGER),
6117GEN_HANDLER(mulhwu, 0x1F, 0x0B, 0x00, 0x00000400, PPC_INTEGER),
6118GEN_HANDLER(mullw, 0x1F, 0x0B, 0x07, 0x00000000, PPC_INTEGER),
6119GEN_HANDLER(mullwo, 0x1F, 0x0B, 0x17, 0x00000000, PPC_INTEGER),
6120GEN_HANDLER(mulli, 0x07, 0xFF, 0xFF, 0x00000000, PPC_INTEGER),
6121#if defined(TARGET_PPC64)
6122GEN_HANDLER(mulld, 0x1F, 0x09, 0x07, 0x00000000, PPC_64B),
6123#endif
6124GEN_HANDLER(neg, 0x1F, 0x08, 0x03, 0x0000F800, PPC_INTEGER),
6125GEN_HANDLER(nego, 0x1F, 0x08, 0x13, 0x0000F800, PPC_INTEGER),
6126GEN_HANDLER(subfic, 0x08, 0xFF, 0xFF, 0x00000000, PPC_INTEGER),
6127GEN_HANDLER2(andi_, "andi.", 0x1C, 0xFF, 0xFF, 0x00000000, PPC_INTEGER),
6128GEN_HANDLER2(andis_, "andis.", 0x1D, 0xFF, 0xFF, 0x00000000, PPC_INTEGER),
6129GEN_HANDLER(cntlzw, 0x1F, 0x1A, 0x00, 0x00000000, PPC_INTEGER),
b35344e4 6130GEN_HANDLER_E(cnttzw, 0x1F, 0x1A, 0x10, 0x00000000, PPC_NONE, PPC2_ISA300),
5c55ff99
BS
6131GEN_HANDLER(or, 0x1F, 0x1C, 0x0D, 0x00000000, PPC_INTEGER),
6132GEN_HANDLER(xor, 0x1F, 0x1C, 0x09, 0x00000000, PPC_INTEGER),
6133GEN_HANDLER(ori, 0x18, 0xFF, 0xFF, 0x00000000, PPC_INTEGER),
6134GEN_HANDLER(oris, 0x19, 0xFF, 0xFF, 0x00000000, PPC_INTEGER),
6135GEN_HANDLER(xori, 0x1A, 0xFF, 0xFF, 0x00000000, PPC_INTEGER),
6136GEN_HANDLER(xoris, 0x1B, 0xFF, 0xFF, 0x00000000, PPC_INTEGER),
6ab39b1b 6137GEN_HANDLER(popcntb, 0x1F, 0x1A, 0x03, 0x0000F801, PPC_POPCNTB),
eaabeef2 6138GEN_HANDLER(popcntw, 0x1F, 0x1A, 0x0b, 0x0000F801, PPC_POPCNTWD),
725bcec2 6139GEN_HANDLER_E(prtyw, 0x1F, 0x1A, 0x04, 0x0000F801, PPC_NONE, PPC2_ISA205),
5c55ff99 6140#if defined(TARGET_PPC64)
eaabeef2 6141GEN_HANDLER(popcntd, 0x1F, 0x1A, 0x0F, 0x0000F801, PPC_POPCNTWD),
5c55ff99 6142GEN_HANDLER(cntlzd, 0x1F, 0x1A, 0x01, 0x00000000, PPC_64B),
e91d95b2 6143GEN_HANDLER_E(cnttzd, 0x1F, 0x1A, 0x11, 0x00000000, PPC_NONE, PPC2_ISA300),
725bcec2 6144GEN_HANDLER_E(prtyd, 0x1F, 0x1A, 0x05, 0x0000F801, PPC_NONE, PPC2_ISA205),
86ba37ed 6145GEN_HANDLER_E(bpermd, 0x1F, 0x1C, 0x07, 0x00000001, PPC_NONE, PPC2_PERM_ISA206),
5c55ff99
BS
6146#endif
6147GEN_HANDLER(rlwimi, 0x14, 0xFF, 0xFF, 0x00000000, PPC_INTEGER),
6148GEN_HANDLER(rlwinm, 0x15, 0xFF, 0xFF, 0x00000000, PPC_INTEGER),
6149GEN_HANDLER(rlwnm, 0x17, 0xFF, 0xFF, 0x00000000, PPC_INTEGER),
6150GEN_HANDLER(slw, 0x1F, 0x18, 0x00, 0x00000000, PPC_INTEGER),
6151GEN_HANDLER(sraw, 0x1F, 0x18, 0x18, 0x00000000, PPC_INTEGER),
6152GEN_HANDLER(srawi, 0x1F, 0x18, 0x19, 0x00000000, PPC_INTEGER),
6153GEN_HANDLER(srw, 0x1F, 0x18, 0x10, 0x00000000, PPC_INTEGER),
6154#if defined(TARGET_PPC64)
6155GEN_HANDLER(sld, 0x1F, 0x1B, 0x00, 0x00000000, PPC_64B),
6156GEN_HANDLER(srad, 0x1F, 0x1A, 0x18, 0x00000000, PPC_64B),
6157GEN_HANDLER2(sradi0, "sradi", 0x1F, 0x1A, 0x19, 0x00000000, PPC_64B),
6158GEN_HANDLER2(sradi1, "sradi", 0x1F, 0x1B, 0x19, 0x00000000, PPC_64B),
6159GEN_HANDLER(srd, 0x1F, 0x1B, 0x10, 0x00000000, PPC_64B),
6160#endif
5c55ff99
BS
6161#if defined(TARGET_PPC64)
6162GEN_HANDLER(ld, 0x3A, 0xFF, 0xFF, 0x00000000, PPC_64B),
6163GEN_HANDLER(lq, 0x38, 0xFF, 0xFF, 0x00000000, PPC_64BX),
6164GEN_HANDLER(std, 0x3E, 0xFF, 0xFF, 0x00000000, PPC_64B),
6165#endif
6166GEN_HANDLER(lmw, 0x2E, 0xFF, 0xFF, 0x00000000, PPC_INTEGER),
6167GEN_HANDLER(stmw, 0x2F, 0xFF, 0xFF, 0x00000000, PPC_INTEGER),
6168GEN_HANDLER(lswi, 0x1F, 0x15, 0x12, 0x00000001, PPC_STRING),
6169GEN_HANDLER(lswx, 0x1F, 0x15, 0x10, 0x00000001, PPC_STRING),
6170GEN_HANDLER(stswi, 0x1F, 0x15, 0x16, 0x00000001, PPC_STRING),
6171GEN_HANDLER(stswx, 0x1F, 0x15, 0x14, 0x00000001, PPC_STRING),
6172GEN_HANDLER(eieio, 0x1F, 0x16, 0x1A, 0x03FFF801, PPC_MEM_EIEIO),
6173GEN_HANDLER(isync, 0x13, 0x16, 0x04, 0x03FFF801, PPC_MEM),
5c77a786
TM
6174GEN_HANDLER_E(lbarx, 0x1F, 0x14, 0x01, 0, PPC_NONE, PPC2_ATOMIC_ISA206),
6175GEN_HANDLER_E(lharx, 0x1F, 0x14, 0x03, 0, PPC_NONE, PPC2_ATOMIC_ISA206),
f844c817 6176GEN_HANDLER(lwarx, 0x1F, 0x14, 0x00, 0x00000000, PPC_RES),
587c51f7
TM
6177GEN_HANDLER_E(stbcx_, 0x1F, 0x16, 0x15, 0, PPC_NONE, PPC2_ATOMIC_ISA206),
6178GEN_HANDLER_E(sthcx_, 0x1F, 0x16, 0x16, 0, PPC_NONE, PPC2_ATOMIC_ISA206),
5c55ff99
BS
6179GEN_HANDLER2(stwcx_, "stwcx.", 0x1F, 0x16, 0x04, 0x00000000, PPC_RES),
6180#if defined(TARGET_PPC64)
f844c817 6181GEN_HANDLER(ldarx, 0x1F, 0x14, 0x02, 0x00000000, PPC_64B),
9c294d5a 6182GEN_HANDLER_E(lqarx, 0x1F, 0x14, 0x08, 0, PPC_NONE, PPC2_LSQ_ISA207),
5c55ff99 6183GEN_HANDLER2(stdcx_, "stdcx.", 0x1F, 0x16, 0x06, 0x00000000, PPC_64B),
27b95bfe 6184GEN_HANDLER_E(stqcx_, 0x1F, 0x16, 0x05, 0, PPC_NONE, PPC2_LSQ_ISA207),
5c55ff99
BS
6185#endif
6186GEN_HANDLER(sync, 0x1F, 0x16, 0x12, 0x039FF801, PPC_MEM_SYNC),
6187GEN_HANDLER(wait, 0x1F, 0x1E, 0x01, 0x03FFF801, PPC_WAIT),
6188GEN_HANDLER(b, 0x12, 0xFF, 0xFF, 0x00000000, PPC_FLOW),
6189GEN_HANDLER(bc, 0x10, 0xFF, 0xFF, 0x00000000, PPC_FLOW),
6190GEN_HANDLER(bcctr, 0x13, 0x10, 0x10, 0x00000000, PPC_FLOW),
6191GEN_HANDLER(bclr, 0x13, 0x10, 0x00, 0x00000000, PPC_FLOW),
52a4984d 6192GEN_HANDLER_E(bctar, 0x13, 0x10, 0x11, 0, PPC_NONE, PPC2_BCTAR_ISA207),
5c55ff99
BS
6193GEN_HANDLER(mcrf, 0x13, 0x00, 0xFF, 0x00000001, PPC_INTEGER),
6194GEN_HANDLER(rfi, 0x13, 0x12, 0x01, 0x03FF8001, PPC_FLOW),
6195#if defined(TARGET_PPC64)
6196GEN_HANDLER(rfid, 0x13, 0x12, 0x00, 0x03FF8001, PPC_64B),
7778a575
BH
6197GEN_HANDLER_E(doze, 0x13, 0x12, 0x0c, 0x03FFF801, PPC_NONE, PPC2_PM_ISA206),
6198GEN_HANDLER_E(nap, 0x13, 0x12, 0x0d, 0x03FFF801, PPC_NONE, PPC2_PM_ISA206),
6199GEN_HANDLER_E(sleep, 0x13, 0x12, 0x0e, 0x03FFF801, PPC_NONE, PPC2_PM_ISA206),
6200GEN_HANDLER_E(rvwinkle, 0x13, 0x12, 0x0f, 0x03FFF801, PPC_NONE, PPC2_PM_ISA206),
5c55ff99
BS
6201GEN_HANDLER(hrfid, 0x13, 0x12, 0x08, 0x03FF8001, PPC_64H),
6202#endif
6203GEN_HANDLER(sc, 0x11, 0xFF, 0xFF, 0x03FFF01D, PPC_FLOW),
6204GEN_HANDLER(tw, 0x1F, 0x04, 0x00, 0x00000001, PPC_FLOW),
6205GEN_HANDLER(twi, 0x03, 0xFF, 0xFF, 0x00000000, PPC_FLOW),
6206#if defined(TARGET_PPC64)
6207GEN_HANDLER(td, 0x1F, 0x04, 0x02, 0x00000001, PPC_64B),
6208GEN_HANDLER(tdi, 0x02, 0xFF, 0xFF, 0x00000000, PPC_64B),
6209#endif
6210GEN_HANDLER(mcrxr, 0x1F, 0x00, 0x10, 0x007FF801, PPC_MISC),
6211GEN_HANDLER(mfcr, 0x1F, 0x13, 0x00, 0x00000801, PPC_MISC),
6212GEN_HANDLER(mfmsr, 0x1F, 0x13, 0x02, 0x001FF801, PPC_MISC),
6213GEN_HANDLER(mfspr, 0x1F, 0x13, 0x0A, 0x00000001, PPC_MISC),
6214GEN_HANDLER(mftb, 0x1F, 0x13, 0x0B, 0x00000001, PPC_MFTB),
6215GEN_HANDLER(mtcrf, 0x1F, 0x10, 0x04, 0x00000801, PPC_MISC),
6216#if defined(TARGET_PPC64)
6217GEN_HANDLER(mtmsrd, 0x1F, 0x12, 0x05, 0x001EF801, PPC_64B),
dc2ee038 6218GEN_HANDLER_E(setb, 0x1F, 0x00, 0x04, 0x0003F801, PPC_NONE, PPC2_ISA300),
5c55ff99 6219#endif
5e31867f 6220GEN_HANDLER(mtmsr, 0x1F, 0x12, 0x04, 0x001EF801, PPC_MISC),
4248b336 6221GEN_HANDLER(mtspr, 0x1F, 0x13, 0x0E, 0x00000000, PPC_MISC),
5c55ff99
BS
6222GEN_HANDLER(dcbf, 0x1F, 0x16, 0x02, 0x03C00001, PPC_CACHE),
6223GEN_HANDLER(dcbi, 0x1F, 0x16, 0x0E, 0x03E00001, PPC_CACHE),
6224GEN_HANDLER(dcbst, 0x1F, 0x16, 0x01, 0x03E00001, PPC_CACHE),
3f34cf91
CLG
6225GEN_HANDLER(dcbt, 0x1F, 0x16, 0x08, 0x00000001, PPC_CACHE),
6226GEN_HANDLER(dcbtst, 0x1F, 0x16, 0x07, 0x00000001, PPC_CACHE),
4d09d529 6227GEN_HANDLER_E(dcbtls, 0x1F, 0x06, 0x05, 0x02000001, PPC_BOOKE, PPC2_BOOKE206),
8e33944f 6228GEN_HANDLER(dcbz, 0x1F, 0x16, 0x1F, 0x03C00001, PPC_CACHE_DCBZ),
5c55ff99
BS
6229GEN_HANDLER(dst, 0x1F, 0x16, 0x0A, 0x01800001, PPC_ALTIVEC),
6230GEN_HANDLER(dstst, 0x1F, 0x16, 0x0B, 0x02000001, PPC_ALTIVEC),
6231GEN_HANDLER(dss, 0x1F, 0x16, 0x19, 0x019FF801, PPC_ALTIVEC),
6232GEN_HANDLER(icbi, 0x1F, 0x16, 0x1E, 0x03E00001, PPC_CACHE_ICBI),
6233GEN_HANDLER(dcba, 0x1F, 0x16, 0x17, 0x03E00001, PPC_CACHE_DCBA),
6234GEN_HANDLER(mfsr, 0x1F, 0x13, 0x12, 0x0010F801, PPC_SEGMENT),
6235GEN_HANDLER(mfsrin, 0x1F, 0x13, 0x14, 0x001F0001, PPC_SEGMENT),
6236GEN_HANDLER(mtsr, 0x1F, 0x12, 0x06, 0x0010F801, PPC_SEGMENT),
6237GEN_HANDLER(mtsrin, 0x1F, 0x12, 0x07, 0x001F0001, PPC_SEGMENT),
6238#if defined(TARGET_PPC64)
6239GEN_HANDLER2(mfsr_64b, "mfsr", 0x1F, 0x13, 0x12, 0x0010F801, PPC_SEGMENT_64B),
6240GEN_HANDLER2(mfsrin_64b, "mfsrin", 0x1F, 0x13, 0x14, 0x001F0001,
6241 PPC_SEGMENT_64B),
6242GEN_HANDLER2(mtsr_64b, "mtsr", 0x1F, 0x12, 0x06, 0x0010F801, PPC_SEGMENT_64B),
6243GEN_HANDLER2(mtsrin_64b, "mtsrin", 0x1F, 0x12, 0x07, 0x001F0001,
6244 PPC_SEGMENT_64B),
efdef95f
DG
6245GEN_HANDLER2(slbmte, "slbmte", 0x1F, 0x12, 0x0C, 0x001F0001, PPC_SEGMENT_64B),
6246GEN_HANDLER2(slbmfee, "slbmfee", 0x1F, 0x13, 0x1C, 0x001F0001, PPC_SEGMENT_64B),
6247GEN_HANDLER2(slbmfev, "slbmfev", 0x1F, 0x13, 0x1A, 0x001F0001, PPC_SEGMENT_64B),
c76c22d5 6248GEN_HANDLER2(slbfee_, "slbfee.", 0x1F, 0x13, 0x1E, 0x001F0000, PPC_SEGMENT_64B),
5c55ff99
BS
6249#endif
6250GEN_HANDLER(tlbia, 0x1F, 0x12, 0x0B, 0x03FFFC01, PPC_MEM_TLBIA),
f9ef0527
BH
6251/* XXX Those instructions will need to be handled differently for
6252 * different ISA versions */
6253GEN_HANDLER(tlbiel, 0x1F, 0x12, 0x08, 0x001F0001, PPC_MEM_TLBIE),
6254GEN_HANDLER(tlbie, 0x1F, 0x12, 0x09, 0x001F0001, PPC_MEM_TLBIE),
5c55ff99
BS
6255GEN_HANDLER(tlbsync, 0x1F, 0x16, 0x11, 0x03FFF801, PPC_MEM_TLBSYNC),
6256#if defined(TARGET_PPC64)
2f9254d9 6257GEN_HANDLER(slbia, 0x1F, 0x12, 0x0F, 0x031FFC01, PPC_SLBI),
5c55ff99
BS
6258GEN_HANDLER(slbie, 0x1F, 0x12, 0x0D, 0x03FF0001, PPC_SLBI),
6259#endif
6260GEN_HANDLER(eciwx, 0x1F, 0x16, 0x0D, 0x00000001, PPC_EXTERN),
6261GEN_HANDLER(ecowx, 0x1F, 0x16, 0x09, 0x00000001, PPC_EXTERN),
6262GEN_HANDLER(abs, 0x1F, 0x08, 0x0B, 0x0000F800, PPC_POWER_BR),
6263GEN_HANDLER(abso, 0x1F, 0x08, 0x1B, 0x0000F800, PPC_POWER_BR),
6264GEN_HANDLER(clcs, 0x1F, 0x10, 0x13, 0x0000F800, PPC_POWER_BR),
6265GEN_HANDLER(div, 0x1F, 0x0B, 0x0A, 0x00000000, PPC_POWER_BR),
6266GEN_HANDLER(divo, 0x1F, 0x0B, 0x1A, 0x00000000, PPC_POWER_BR),
6267GEN_HANDLER(divs, 0x1F, 0x0B, 0x0B, 0x00000000, PPC_POWER_BR),
6268GEN_HANDLER(divso, 0x1F, 0x0B, 0x1B, 0x00000000, PPC_POWER_BR),
6269GEN_HANDLER(doz, 0x1F, 0x08, 0x08, 0x00000000, PPC_POWER_BR),
6270GEN_HANDLER(dozo, 0x1F, 0x08, 0x18, 0x00000000, PPC_POWER_BR),
6271GEN_HANDLER(dozi, 0x09, 0xFF, 0xFF, 0x00000000, PPC_POWER_BR),
6272GEN_HANDLER(lscbx, 0x1F, 0x15, 0x08, 0x00000000, PPC_POWER_BR),
6273GEN_HANDLER(maskg, 0x1F, 0x1D, 0x00, 0x00000000, PPC_POWER_BR),
6274GEN_HANDLER(maskir, 0x1F, 0x1D, 0x10, 0x00000000, PPC_POWER_BR),
6275GEN_HANDLER(mul, 0x1F, 0x0B, 0x03, 0x00000000, PPC_POWER_BR),
6276GEN_HANDLER(mulo, 0x1F, 0x0B, 0x13, 0x00000000, PPC_POWER_BR),
6277GEN_HANDLER(nabs, 0x1F, 0x08, 0x0F, 0x00000000, PPC_POWER_BR),
6278GEN_HANDLER(nabso, 0x1F, 0x08, 0x1F, 0x00000000, PPC_POWER_BR),
6279GEN_HANDLER(rlmi, 0x16, 0xFF, 0xFF, 0x00000000, PPC_POWER_BR),
6280GEN_HANDLER(rrib, 0x1F, 0x19, 0x10, 0x00000000, PPC_POWER_BR),
6281GEN_HANDLER(sle, 0x1F, 0x19, 0x04, 0x00000000, PPC_POWER_BR),
6282GEN_HANDLER(sleq, 0x1F, 0x19, 0x06, 0x00000000, PPC_POWER_BR),
6283GEN_HANDLER(sliq, 0x1F, 0x18, 0x05, 0x00000000, PPC_POWER_BR),
6284GEN_HANDLER(slliq, 0x1F, 0x18, 0x07, 0x00000000, PPC_POWER_BR),
6285GEN_HANDLER(sllq, 0x1F, 0x18, 0x06, 0x00000000, PPC_POWER_BR),
6286GEN_HANDLER(slq, 0x1F, 0x18, 0x04, 0x00000000, PPC_POWER_BR),
6287GEN_HANDLER(sraiq, 0x1F, 0x18, 0x1D, 0x00000000, PPC_POWER_BR),
6288GEN_HANDLER(sraq, 0x1F, 0x18, 0x1C, 0x00000000, PPC_POWER_BR),
6289GEN_HANDLER(sre, 0x1F, 0x19, 0x14, 0x00000000, PPC_POWER_BR),
6290GEN_HANDLER(srea, 0x1F, 0x19, 0x1C, 0x00000000, PPC_POWER_BR),
6291GEN_HANDLER(sreq, 0x1F, 0x19, 0x16, 0x00000000, PPC_POWER_BR),
6292GEN_HANDLER(sriq, 0x1F, 0x18, 0x15, 0x00000000, PPC_POWER_BR),
6293GEN_HANDLER(srliq, 0x1F, 0x18, 0x17, 0x00000000, PPC_POWER_BR),
6294GEN_HANDLER(srlq, 0x1F, 0x18, 0x16, 0x00000000, PPC_POWER_BR),
6295GEN_HANDLER(srq, 0x1F, 0x18, 0x14, 0x00000000, PPC_POWER_BR),
6296GEN_HANDLER(dsa, 0x1F, 0x14, 0x13, 0x03FFF801, PPC_602_SPEC),
6297GEN_HANDLER(esa, 0x1F, 0x14, 0x12, 0x03FFF801, PPC_602_SPEC),
6298GEN_HANDLER(mfrom, 0x1F, 0x09, 0x08, 0x03E0F801, PPC_602_SPEC),
6299GEN_HANDLER2(tlbld_6xx, "tlbld", 0x1F, 0x12, 0x1E, 0x03FF0001, PPC_6xx_TLB),
6300GEN_HANDLER2(tlbli_6xx, "tlbli", 0x1F, 0x12, 0x1F, 0x03FF0001, PPC_6xx_TLB),
6301GEN_HANDLER2(tlbld_74xx, "tlbld", 0x1F, 0x12, 0x1E, 0x03FF0001, PPC_74xx_TLB),
6302GEN_HANDLER2(tlbli_74xx, "tlbli", 0x1F, 0x12, 0x1F, 0x03FF0001, PPC_74xx_TLB),
6303GEN_HANDLER(clf, 0x1F, 0x16, 0x03, 0x03E00000, PPC_POWER),
6304GEN_HANDLER(cli, 0x1F, 0x16, 0x0F, 0x03E00000, PPC_POWER),
6305GEN_HANDLER(dclst, 0x1F, 0x16, 0x13, 0x03E00000, PPC_POWER),
6306GEN_HANDLER(mfsri, 0x1F, 0x13, 0x13, 0x00000001, PPC_POWER),
6307GEN_HANDLER(rac, 0x1F, 0x12, 0x19, 0x00000001, PPC_POWER),
6308GEN_HANDLER(rfsvc, 0x13, 0x12, 0x02, 0x03FFF0001, PPC_POWER),
6309GEN_HANDLER(lfq, 0x38, 0xFF, 0xFF, 0x00000003, PPC_POWER2),
6310GEN_HANDLER(lfqu, 0x39, 0xFF, 0xFF, 0x00000003, PPC_POWER2),
6311GEN_HANDLER(lfqux, 0x1F, 0x17, 0x19, 0x00000001, PPC_POWER2),
6312GEN_HANDLER(lfqx, 0x1F, 0x17, 0x18, 0x00000001, PPC_POWER2),
6313GEN_HANDLER(stfq, 0x3C, 0xFF, 0xFF, 0x00000003, PPC_POWER2),
6314GEN_HANDLER(stfqu, 0x3D, 0xFF, 0xFF, 0x00000003, PPC_POWER2),
6315GEN_HANDLER(stfqux, 0x1F, 0x17, 0x1D, 0x00000001, PPC_POWER2),
6316GEN_HANDLER(stfqx, 0x1F, 0x17, 0x1C, 0x00000001, PPC_POWER2),
6317GEN_HANDLER(mfapidi, 0x1F, 0x13, 0x08, 0x0000F801, PPC_MFAPIDI),
6318GEN_HANDLER(tlbiva, 0x1F, 0x12, 0x18, 0x03FFF801, PPC_TLBIVA),
6319GEN_HANDLER(mfdcr, 0x1F, 0x03, 0x0A, 0x00000001, PPC_DCR),
6320GEN_HANDLER(mtdcr, 0x1F, 0x03, 0x0E, 0x00000001, PPC_DCR),
6321GEN_HANDLER(mfdcrx, 0x1F, 0x03, 0x08, 0x00000000, PPC_DCRX),
6322GEN_HANDLER(mtdcrx, 0x1F, 0x03, 0x0C, 0x00000000, PPC_DCRX),
6323GEN_HANDLER(mfdcrux, 0x1F, 0x03, 0x09, 0x00000000, PPC_DCRUX),
6324GEN_HANDLER(mtdcrux, 0x1F, 0x03, 0x0D, 0x00000000, PPC_DCRUX),
6325GEN_HANDLER(dccci, 0x1F, 0x06, 0x0E, 0x03E00001, PPC_4xx_COMMON),
6326GEN_HANDLER(dcread, 0x1F, 0x06, 0x0F, 0x00000001, PPC_4xx_COMMON),
6327GEN_HANDLER2(icbt_40x, "icbt", 0x1F, 0x06, 0x08, 0x03E00001, PPC_40x_ICBT),
6328GEN_HANDLER(iccci, 0x1F, 0x06, 0x1E, 0x00000001, PPC_4xx_COMMON),
6329GEN_HANDLER(icread, 0x1F, 0x06, 0x1F, 0x03E00001, PPC_4xx_COMMON),
6330GEN_HANDLER2(rfci_40x, "rfci", 0x13, 0x13, 0x01, 0x03FF8001, PPC_40x_EXCP),
01662f3e 6331GEN_HANDLER_E(rfci, 0x13, 0x13, 0x01, 0x03FF8001, PPC_BOOKE, PPC2_BOOKE206),
5c55ff99
BS
6332GEN_HANDLER(rfdi, 0x13, 0x07, 0x01, 0x03FF8001, PPC_RFDI),
6333GEN_HANDLER(rfmci, 0x13, 0x06, 0x01, 0x03FF8001, PPC_RFMCI),
6334GEN_HANDLER2(tlbre_40x, "tlbre", 0x1F, 0x12, 0x1D, 0x00000001, PPC_40x_TLB),
6335GEN_HANDLER2(tlbsx_40x, "tlbsx", 0x1F, 0x12, 0x1C, 0x00000000, PPC_40x_TLB),
6336GEN_HANDLER2(tlbwe_40x, "tlbwe", 0x1F, 0x12, 0x1E, 0x00000001, PPC_40x_TLB),
6337GEN_HANDLER2(tlbre_440, "tlbre", 0x1F, 0x12, 0x1D, 0x00000001, PPC_BOOKE),
6338GEN_HANDLER2(tlbsx_440, "tlbsx", 0x1F, 0x12, 0x1C, 0x00000000, PPC_BOOKE),
6339GEN_HANDLER2(tlbwe_440, "tlbwe", 0x1F, 0x12, 0x1E, 0x00000001, PPC_BOOKE),
01662f3e
AG
6340GEN_HANDLER2_E(tlbre_booke206, "tlbre", 0x1F, 0x12, 0x1D, 0x00000001,
6341 PPC_NONE, PPC2_BOOKE206),
6342GEN_HANDLER2_E(tlbsx_booke206, "tlbsx", 0x1F, 0x12, 0x1C, 0x00000000,
6343 PPC_NONE, PPC2_BOOKE206),
6344GEN_HANDLER2_E(tlbwe_booke206, "tlbwe", 0x1F, 0x12, 0x1E, 0x00000001,
6345 PPC_NONE, PPC2_BOOKE206),
6346GEN_HANDLER2_E(tlbivax_booke206, "tlbivax", 0x1F, 0x12, 0x18, 0x00000001,
6347 PPC_NONE, PPC2_BOOKE206),
6d3db821
AG
6348GEN_HANDLER2_E(tlbilx_booke206, "tlbilx", 0x1F, 0x12, 0x00, 0x03800001,
6349 PPC_NONE, PPC2_BOOKE206),
d5d11a39
AG
6350GEN_HANDLER2_E(msgsnd, "msgsnd", 0x1F, 0x0E, 0x06, 0x03ff0001,
6351 PPC_NONE, PPC2_PRCNTL),
9e0b5cb1
AG
6352GEN_HANDLER2_E(msgclr, "msgclr", 0x1F, 0x0E, 0x07, 0x03ff0001,
6353 PPC_NONE, PPC2_PRCNTL),
5c55ff99 6354GEN_HANDLER(wrtee, 0x1F, 0x03, 0x04, 0x000FFC01, PPC_WRTEE),
fbe73008 6355GEN_HANDLER(wrteei, 0x1F, 0x03, 0x05, 0x000E7C01, PPC_WRTEE),
5c55ff99 6356GEN_HANDLER(dlmzb, 0x1F, 0x0E, 0x02, 0x00000000, PPC_440_SPEC),
01662f3e
AG
6357GEN_HANDLER_E(mbar, 0x1F, 0x16, 0x1a, 0x001FF801,
6358 PPC_BOOKE, PPC2_BOOKE206),
dcb2b9e1 6359GEN_HANDLER(msync_4xx, 0x1F, 0x16, 0x12, 0x03FFF801, PPC_BOOKE),
01662f3e
AG
6360GEN_HANDLER2_E(icbt_440, "icbt", 0x1F, 0x16, 0x00, 0x03E00001,
6361 PPC_BOOKE, PPC2_BOOKE206),
5c55ff99
BS
6362GEN_HANDLER(lvsl, 0x1f, 0x06, 0x00, 0x00000001, PPC_ALTIVEC),
6363GEN_HANDLER(lvsr, 0x1f, 0x06, 0x01, 0x00000001, PPC_ALTIVEC),
6364GEN_HANDLER(mfvscr, 0x04, 0x2, 0x18, 0x001ff800, PPC_ALTIVEC),
6365GEN_HANDLER(mtvscr, 0x04, 0x2, 0x19, 0x03ff0000, PPC_ALTIVEC),
5c55ff99 6366GEN_HANDLER(vmladduhm, 0x04, 0x11, 0xFF, 0x00000000, PPC_ALTIVEC),
aeeb044c 6367#if defined(TARGET_PPC64)
5f29cc82
ND
6368GEN_HANDLER_E(maddhd_maddhdu, 0x04, 0x18, 0xFF, 0x00000000, PPC_NONE,
6369 PPC2_ISA300),
aeeb044c
ND
6370GEN_HANDLER_E(maddld, 0x04, 0x19, 0xFF, 0x00000000, PPC_NONE, PPC2_ISA300),
6371#endif
5c55ff99
BS
6372
6373#undef GEN_INT_ARITH_ADD
6374#undef GEN_INT_ARITH_ADD_CONST
6375#define GEN_INT_ARITH_ADD(name, opc3, add_ca, compute_ca, compute_ov) \
6376GEN_HANDLER(name, 0x1F, 0x0A, opc3, 0x00000000, PPC_INTEGER),
6377#define GEN_INT_ARITH_ADD_CONST(name, opc3, const_val, \
6378 add_ca, compute_ca, compute_ov) \
6379GEN_HANDLER(name, 0x1F, 0x0A, opc3, 0x0000F800, PPC_INTEGER),
6380GEN_INT_ARITH_ADD(add, 0x08, 0, 0, 0)
6381GEN_INT_ARITH_ADD(addo, 0x18, 0, 0, 1)
6382GEN_INT_ARITH_ADD(addc, 0x00, 0, 1, 0)
6383GEN_INT_ARITH_ADD(addco, 0x10, 0, 1, 1)
6384GEN_INT_ARITH_ADD(adde, 0x04, 1, 1, 0)
6385GEN_INT_ARITH_ADD(addeo, 0x14, 1, 1, 1)
6386GEN_INT_ARITH_ADD_CONST(addme, 0x07, -1LL, 1, 1, 0)
6387GEN_INT_ARITH_ADD_CONST(addmeo, 0x17, -1LL, 1, 1, 1)
6388GEN_INT_ARITH_ADD_CONST(addze, 0x06, 0, 1, 1, 0)
6389GEN_INT_ARITH_ADD_CONST(addzeo, 0x16, 0, 1, 1, 1)
6390
6391#undef GEN_INT_ARITH_DIVW
6392#define GEN_INT_ARITH_DIVW(name, opc3, sign, compute_ov) \
6393GEN_HANDLER(name, 0x1F, 0x0B, opc3, 0x00000000, PPC_INTEGER)
6394GEN_INT_ARITH_DIVW(divwu, 0x0E, 0, 0),
6395GEN_INT_ARITH_DIVW(divwuo, 0x1E, 0, 1),
6396GEN_INT_ARITH_DIVW(divw, 0x0F, 1, 0),
6397GEN_INT_ARITH_DIVW(divwo, 0x1F, 1, 1),
a98eb9e9
TM
6398GEN_HANDLER_E(divwe, 0x1F, 0x0B, 0x0D, 0, PPC_NONE, PPC2_DIVE_ISA206),
6399GEN_HANDLER_E(divweo, 0x1F, 0x0B, 0x1D, 0, PPC_NONE, PPC2_DIVE_ISA206),
6a4fda33
TM
6400GEN_HANDLER_E(divweu, 0x1F, 0x0B, 0x0C, 0, PPC_NONE, PPC2_DIVE_ISA206),
6401GEN_HANDLER_E(divweuo, 0x1F, 0x0B, 0x1C, 0, PPC_NONE, PPC2_DIVE_ISA206),
af2c6620
ND
6402GEN_HANDLER_E(modsw, 0x1F, 0x0B, 0x18, 0x00000001, PPC_NONE, PPC2_ISA300),
6403GEN_HANDLER_E(moduw, 0x1F, 0x0B, 0x08, 0x00000001, PPC_NONE, PPC2_ISA300),
5c55ff99
BS
6404
6405#if defined(TARGET_PPC64)
6406#undef GEN_INT_ARITH_DIVD
6407#define GEN_INT_ARITH_DIVD(name, opc3, sign, compute_ov) \
6408GEN_HANDLER(name, 0x1F, 0x09, opc3, 0x00000000, PPC_64B)
6409GEN_INT_ARITH_DIVD(divdu, 0x0E, 0, 0),
6410GEN_INT_ARITH_DIVD(divduo, 0x1E, 0, 1),
6411GEN_INT_ARITH_DIVD(divd, 0x0F, 1, 0),
6412GEN_INT_ARITH_DIVD(divdo, 0x1F, 1, 1),
6413
98d1eb27
TM
6414GEN_HANDLER_E(divdeu, 0x1F, 0x09, 0x0C, 0, PPC_NONE, PPC2_DIVE_ISA206),
6415GEN_HANDLER_E(divdeuo, 0x1F, 0x09, 0x1C, 0, PPC_NONE, PPC2_DIVE_ISA206),
e44259b6
TM
6416GEN_HANDLER_E(divde, 0x1F, 0x09, 0x0D, 0, PPC_NONE, PPC2_DIVE_ISA206),
6417GEN_HANDLER_E(divdeo, 0x1F, 0x09, 0x1D, 0, PPC_NONE, PPC2_DIVE_ISA206),
063cf14f
ND
6418GEN_HANDLER_E(modsd, 0x1F, 0x09, 0x18, 0x00000001, PPC_NONE, PPC2_ISA300),
6419GEN_HANDLER_E(modud, 0x1F, 0x09, 0x08, 0x00000001, PPC_NONE, PPC2_ISA300),
98d1eb27 6420
5c55ff99
BS
6421#undef GEN_INT_ARITH_MUL_HELPER
6422#define GEN_INT_ARITH_MUL_HELPER(name, opc3) \
6423GEN_HANDLER(name, 0x1F, 0x09, opc3, 0x00000000, PPC_64B)
6424GEN_INT_ARITH_MUL_HELPER(mulhdu, 0x00),
6425GEN_INT_ARITH_MUL_HELPER(mulhd, 0x02),
6426GEN_INT_ARITH_MUL_HELPER(mulldo, 0x17),
6427#endif
6428
6429#undef GEN_INT_ARITH_SUBF
6430#undef GEN_INT_ARITH_SUBF_CONST
6431#define GEN_INT_ARITH_SUBF(name, opc3, add_ca, compute_ca, compute_ov) \
6432GEN_HANDLER(name, 0x1F, 0x08, opc3, 0x00000000, PPC_INTEGER),
6433#define GEN_INT_ARITH_SUBF_CONST(name, opc3, const_val, \
6434 add_ca, compute_ca, compute_ov) \
6435GEN_HANDLER(name, 0x1F, 0x08, opc3, 0x0000F800, PPC_INTEGER),
6436GEN_INT_ARITH_SUBF(subf, 0x01, 0, 0, 0)
6437GEN_INT_ARITH_SUBF(subfo, 0x11, 0, 0, 1)
6438GEN_INT_ARITH_SUBF(subfc, 0x00, 0, 1, 0)
6439GEN_INT_ARITH_SUBF(subfco, 0x10, 0, 1, 1)
6440GEN_INT_ARITH_SUBF(subfe, 0x04, 1, 1, 0)
6441GEN_INT_ARITH_SUBF(subfeo, 0x14, 1, 1, 1)
6442GEN_INT_ARITH_SUBF_CONST(subfme, 0x07, -1LL, 1, 1, 0)
6443GEN_INT_ARITH_SUBF_CONST(subfmeo, 0x17, -1LL, 1, 1, 1)
6444GEN_INT_ARITH_SUBF_CONST(subfze, 0x06, 0, 1, 1, 0)
6445GEN_INT_ARITH_SUBF_CONST(subfzeo, 0x16, 0, 1, 1, 1)
6446
6447#undef GEN_LOGICAL1
6448#undef GEN_LOGICAL2
6449#define GEN_LOGICAL2(name, tcg_op, opc, type) \
6450GEN_HANDLER(name, 0x1F, 0x1C, opc, 0x00000000, type)
6451#define GEN_LOGICAL1(name, tcg_op, opc, type) \
6452GEN_HANDLER(name, 0x1F, 0x1A, opc, 0x00000000, type)
6453GEN_LOGICAL2(and, tcg_gen_and_tl, 0x00, PPC_INTEGER),
6454GEN_LOGICAL2(andc, tcg_gen_andc_tl, 0x01, PPC_INTEGER),
6455GEN_LOGICAL2(eqv, tcg_gen_eqv_tl, 0x08, PPC_INTEGER),
6456GEN_LOGICAL1(extsb, tcg_gen_ext8s_tl, 0x1D, PPC_INTEGER),
6457GEN_LOGICAL1(extsh, tcg_gen_ext16s_tl, 0x1C, PPC_INTEGER),
6458GEN_LOGICAL2(nand, tcg_gen_nand_tl, 0x0E, PPC_INTEGER),
6459GEN_LOGICAL2(nor, tcg_gen_nor_tl, 0x03, PPC_INTEGER),
6460GEN_LOGICAL2(orc, tcg_gen_orc_tl, 0x0C, PPC_INTEGER),
6461#if defined(TARGET_PPC64)
6462GEN_LOGICAL1(extsw, tcg_gen_ext32s_tl, 0x1E, PPC_64B),
6463#endif
6464
6465#if defined(TARGET_PPC64)
6466#undef GEN_PPC64_R2
6467#undef GEN_PPC64_R4
6468#define GEN_PPC64_R2(name, opc1, opc2) \
6469GEN_HANDLER2(name##0, stringify(name), opc1, opc2, 0xFF, 0x00000000, PPC_64B),\
6470GEN_HANDLER2(name##1, stringify(name), opc1, opc2 | 0x10, 0xFF, 0x00000000, \
6471 PPC_64B)
6472#define GEN_PPC64_R4(name, opc1, opc2) \
6473GEN_HANDLER2(name##0, stringify(name), opc1, opc2, 0xFF, 0x00000000, PPC_64B),\
6474GEN_HANDLER2(name##1, stringify(name), opc1, opc2 | 0x01, 0xFF, 0x00000000, \
6475 PPC_64B), \
6476GEN_HANDLER2(name##2, stringify(name), opc1, opc2 | 0x10, 0xFF, 0x00000000, \
6477 PPC_64B), \
6478GEN_HANDLER2(name##3, stringify(name), opc1, opc2 | 0x11, 0xFF, 0x00000000, \
6479 PPC_64B)
6480GEN_PPC64_R4(rldicl, 0x1E, 0x00),
6481GEN_PPC64_R4(rldicr, 0x1E, 0x02),
6482GEN_PPC64_R4(rldic, 0x1E, 0x04),
6483GEN_PPC64_R2(rldcl, 0x1E, 0x08),
6484GEN_PPC64_R2(rldcr, 0x1E, 0x09),
6485GEN_PPC64_R4(rldimi, 0x1E, 0x06),
6486#endif
6487
5c55ff99
BS
6488#undef GEN_LD
6489#undef GEN_LDU
6490#undef GEN_LDUX
cd6e9320 6491#undef GEN_LDX_E
5c55ff99
BS
6492#undef GEN_LDS
6493#define GEN_LD(name, ldop, opc, type) \
6494GEN_HANDLER(name, opc, 0xFF, 0xFF, 0x00000000, type),
6495#define GEN_LDU(name, ldop, opc, type) \
6496GEN_HANDLER(name##u, opc, 0xFF, 0xFF, 0x00000000, type),
6497#define GEN_LDUX(name, ldop, opc2, opc3, type) \
6498GEN_HANDLER(name##ux, 0x1F, opc2, opc3, 0x00000001, type),
b7815375 6499#define GEN_LDX_E(name, ldop, opc2, opc3, type, type2, chk) \
cd6e9320 6500GEN_HANDLER_E(name##x, 0x1F, opc2, opc3, 0x00000001, type, type2),
5c55ff99
BS
6501#define GEN_LDS(name, ldop, op, type) \
6502GEN_LD(name, ldop, op | 0x20, type) \
6503GEN_LDU(name, ldop, op | 0x21, type) \
6504GEN_LDUX(name, ldop, 0x17, op | 0x01, type) \
6505GEN_LDX(name, ldop, 0x17, op | 0x00, type)
6506
6507GEN_LDS(lbz, ld8u, 0x02, PPC_INTEGER)
6508GEN_LDS(lha, ld16s, 0x0A, PPC_INTEGER)
6509GEN_LDS(lhz, ld16u, 0x08, PPC_INTEGER)
6510GEN_LDS(lwz, ld32u, 0x00, PPC_INTEGER)
6511#if defined(TARGET_PPC64)
6512GEN_LDUX(lwa, ld32s, 0x15, 0x0B, PPC_64B)
6513GEN_LDX(lwa, ld32s, 0x15, 0x0A, PPC_64B)
6514GEN_LDUX(ld, ld64, 0x15, 0x01, PPC_64B)
6515GEN_LDX(ld, ld64, 0x15, 0x00, PPC_64B)
b7815375
BH
6516GEN_LDX_E(ldbr, ld64ur, 0x14, 0x10, PPC_NONE, PPC2_DBRX, CHK_NONE)
6517
6518/* HV/P7 and later only */
6519GEN_LDX_HVRM(ldcix, ld64, 0x15, 0x1b, PPC_CILDST)
6520GEN_LDX_HVRM(lwzcix, ld32u, 0x15, 0x18, PPC_CILDST)
6521GEN_LDX_HVRM(lhzcix, ld16u, 0x15, 0x19, PPC_CILDST)
6522GEN_LDX_HVRM(lbzcix, ld8u, 0x15, 0x1a, PPC_CILDST)
5c55ff99
BS
6523#endif
6524GEN_LDX(lhbr, ld16ur, 0x16, 0x18, PPC_INTEGER)
6525GEN_LDX(lwbr, ld32ur, 0x16, 0x10, PPC_INTEGER)
6526
6527#undef GEN_ST
6528#undef GEN_STU
6529#undef GEN_STUX
cd6e9320 6530#undef GEN_STX_E
5c55ff99
BS
6531#undef GEN_STS
6532#define GEN_ST(name, stop, opc, type) \
6533GEN_HANDLER(name, opc, 0xFF, 0xFF, 0x00000000, type),
6534#define GEN_STU(name, stop, opc, type) \
6535GEN_HANDLER(stop##u, opc, 0xFF, 0xFF, 0x00000000, type),
6536#define GEN_STUX(name, stop, opc2, opc3, type) \
6537GEN_HANDLER(name##ux, 0x1F, opc2, opc3, 0x00000001, type),
b7815375 6538#define GEN_STX_E(name, stop, opc2, opc3, type, type2, chk) \
cd6e9320 6539GEN_HANDLER_E(name##x, 0x1F, opc2, opc3, 0x00000001, type, type2),
5c55ff99
BS
6540#define GEN_STS(name, stop, op, type) \
6541GEN_ST(name, stop, op | 0x20, type) \
6542GEN_STU(name, stop, op | 0x21, type) \
6543GEN_STUX(name, stop, 0x17, op | 0x01, type) \
6544GEN_STX(name, stop, 0x17, op | 0x00, type)
6545
6546GEN_STS(stb, st8, 0x06, PPC_INTEGER)
6547GEN_STS(sth, st16, 0x0C, PPC_INTEGER)
6548GEN_STS(stw, st32, 0x04, PPC_INTEGER)
6549#if defined(TARGET_PPC64)
6550GEN_STUX(std, st64, 0x15, 0x05, PPC_64B)
6551GEN_STX(std, st64, 0x15, 0x04, PPC_64B)
b7815375
BH
6552GEN_STX_E(stdbr, st64r, 0x14, 0x14, PPC_NONE, PPC2_DBRX, CHK_NONE)
6553GEN_STX_HVRM(stdcix, st64, 0x15, 0x1f, PPC_CILDST)
6554GEN_STX_HVRM(stwcix, st32, 0x15, 0x1c, PPC_CILDST)
6555GEN_STX_HVRM(sthcix, st16, 0x15, 0x1d, PPC_CILDST)
6556GEN_STX_HVRM(stbcix, st8, 0x15, 0x1e, PPC_CILDST)
5c55ff99
BS
6557#endif
6558GEN_STX(sthbr, st16r, 0x16, 0x1C, PPC_INTEGER)
6559GEN_STX(stwbr, st32r, 0x16, 0x14, PPC_INTEGER)
6560
5c55ff99
BS
6561#undef GEN_CRLOGIC
6562#define GEN_CRLOGIC(name, tcg_op, opc) \
6563GEN_HANDLER(name, 0x13, 0x01, opc, 0x00000001, PPC_INTEGER)
6564GEN_CRLOGIC(crand, tcg_gen_and_i32, 0x08),
6565GEN_CRLOGIC(crandc, tcg_gen_andc_i32, 0x04),
6566GEN_CRLOGIC(creqv, tcg_gen_eqv_i32, 0x09),
6567GEN_CRLOGIC(crnand, tcg_gen_nand_i32, 0x07),
6568GEN_CRLOGIC(crnor, tcg_gen_nor_i32, 0x01),
6569GEN_CRLOGIC(cror, tcg_gen_or_i32, 0x0E),
6570GEN_CRLOGIC(crorc, tcg_gen_orc_i32, 0x0D),
6571GEN_CRLOGIC(crxor, tcg_gen_xor_i32, 0x06),
6572
6573#undef GEN_MAC_HANDLER
6574#define GEN_MAC_HANDLER(name, opc2, opc3) \
6575GEN_HANDLER(name, 0x04, opc2, opc3, 0x00000000, PPC_405_MAC)
6576GEN_MAC_HANDLER(macchw, 0x0C, 0x05),
6577GEN_MAC_HANDLER(macchwo, 0x0C, 0x15),
6578GEN_MAC_HANDLER(macchws, 0x0C, 0x07),
6579GEN_MAC_HANDLER(macchwso, 0x0C, 0x17),
6580GEN_MAC_HANDLER(macchwsu, 0x0C, 0x06),
6581GEN_MAC_HANDLER(macchwsuo, 0x0C, 0x16),
6582GEN_MAC_HANDLER(macchwu, 0x0C, 0x04),
6583GEN_MAC_HANDLER(macchwuo, 0x0C, 0x14),
6584GEN_MAC_HANDLER(machhw, 0x0C, 0x01),
6585GEN_MAC_HANDLER(machhwo, 0x0C, 0x11),
6586GEN_MAC_HANDLER(machhws, 0x0C, 0x03),
6587GEN_MAC_HANDLER(machhwso, 0x0C, 0x13),
6588GEN_MAC_HANDLER(machhwsu, 0x0C, 0x02),
6589GEN_MAC_HANDLER(machhwsuo, 0x0C, 0x12),
6590GEN_MAC_HANDLER(machhwu, 0x0C, 0x00),
6591GEN_MAC_HANDLER(machhwuo, 0x0C, 0x10),
6592GEN_MAC_HANDLER(maclhw, 0x0C, 0x0D),
6593GEN_MAC_HANDLER(maclhwo, 0x0C, 0x1D),
6594GEN_MAC_HANDLER(maclhws, 0x0C, 0x0F),
6595GEN_MAC_HANDLER(maclhwso, 0x0C, 0x1F),
6596GEN_MAC_HANDLER(maclhwu, 0x0C, 0x0C),
6597GEN_MAC_HANDLER(maclhwuo, 0x0C, 0x1C),
6598GEN_MAC_HANDLER(maclhwsu, 0x0C, 0x0E),
6599GEN_MAC_HANDLER(maclhwsuo, 0x0C, 0x1E),
6600GEN_MAC_HANDLER(nmacchw, 0x0E, 0x05),
6601GEN_MAC_HANDLER(nmacchwo, 0x0E, 0x15),
6602GEN_MAC_HANDLER(nmacchws, 0x0E, 0x07),
6603GEN_MAC_HANDLER(nmacchwso, 0x0E, 0x17),
6604GEN_MAC_HANDLER(nmachhw, 0x0E, 0x01),
6605GEN_MAC_HANDLER(nmachhwo, 0x0E, 0x11),
6606GEN_MAC_HANDLER(nmachhws, 0x0E, 0x03),
6607GEN_MAC_HANDLER(nmachhwso, 0x0E, 0x13),
6608GEN_MAC_HANDLER(nmaclhw, 0x0E, 0x0D),
6609GEN_MAC_HANDLER(nmaclhwo, 0x0E, 0x1D),
6610GEN_MAC_HANDLER(nmaclhws, 0x0E, 0x0F),
6611GEN_MAC_HANDLER(nmaclhwso, 0x0E, 0x1F),
6612GEN_MAC_HANDLER(mulchw, 0x08, 0x05),
6613GEN_MAC_HANDLER(mulchwu, 0x08, 0x04),
6614GEN_MAC_HANDLER(mulhhw, 0x08, 0x01),
6615GEN_MAC_HANDLER(mulhhwu, 0x08, 0x00),
6616GEN_MAC_HANDLER(mullhw, 0x08, 0x0D),
6617GEN_MAC_HANDLER(mullhwu, 0x08, 0x0C),
6618
f9651121
BH
6619#include "translate/fp-ops.c"
6620
0304af89 6621#include "translate/vmx-ops.c"
ac174549 6622
3014427a 6623#include "translate/vsx-ops.c"
cd73f2c9 6624
8b25cdd3 6625#include "translate/dfp-ops.c"
804e654a 6626
4083de6b 6627#include "translate/spe-ops.c"
0ff93d11
TM
6628
6629GEN_HANDLER2_E(tbegin, "tbegin", 0x1F, 0x0E, 0x14, 0x01DFF800, \
6630 PPC_NONE, PPC2_TM),
56a84615
TM
6631GEN_HANDLER2_E(tend, "tend", 0x1F, 0x0E, 0x15, 0x01FFF800, \
6632 PPC_NONE, PPC2_TM),
6633GEN_HANDLER2_E(tabort, "tabort", 0x1F, 0x0E, 0x1C, 0x03E0F800, \
6634 PPC_NONE, PPC2_TM),
6635GEN_HANDLER2_E(tabortwc, "tabortwc", 0x1F, 0x0E, 0x18, 0x00000000, \
6636 PPC_NONE, PPC2_TM),
6637GEN_HANDLER2_E(tabortwci, "tabortwci", 0x1F, 0x0E, 0x1A, 0x00000000, \
6638 PPC_NONE, PPC2_TM),
6639GEN_HANDLER2_E(tabortdc, "tabortdc", 0x1F, 0x0E, 0x19, 0x00000000, \
6640 PPC_NONE, PPC2_TM),
6641GEN_HANDLER2_E(tabortdci, "tabortdci", 0x1F, 0x0E, 0x1B, 0x00000000, \
6642 PPC_NONE, PPC2_TM),
6643GEN_HANDLER2_E(tsr, "tsr", 0x1F, 0x0E, 0x17, 0x03DFF800, \
6644 PPC_NONE, PPC2_TM),
aeedd582
TM
6645GEN_HANDLER2_E(tcheck, "tcheck", 0x1F, 0x0E, 0x16, 0x007FF800, \
6646 PPC_NONE, PPC2_TM),
f83c2378
TM
6647GEN_HANDLER2_E(treclaim, "treclaim", 0x1F, 0x0E, 0x1D, 0x03E0F800, \
6648 PPC_NONE, PPC2_TM),
6649GEN_HANDLER2_E(trechkpt, "trechkpt", 0x1F, 0x0E, 0x1F, 0x03FFF800, \
6650 PPC_NONE, PPC2_TM),
5c55ff99
BS
6651};
6652
0411a972 6653#include "helper_regs.h"
a1389542 6654#include "translate_init.c"
79aceca5 6655
9a64fbe4 6656/*****************************************************************************/
3fc6c082 6657/* Misc PowerPC helpers */
878096ee
AF
6658void ppc_cpu_dump_state(CPUState *cs, FILE *f, fprintf_function cpu_fprintf,
6659 int flags)
79aceca5 6660{
3fc6c082
FB
6661#define RGPL 4
6662#define RFPL 4
3fc6c082 6663
878096ee
AF
6664 PowerPCCPU *cpu = POWERPC_CPU(cs);
6665 CPUPPCState *env = &cpu->env;
79aceca5
FB
6666 int i;
6667
90e189ec 6668 cpu_fprintf(f, "NIP " TARGET_FMT_lx " LR " TARGET_FMT_lx " CTR "
21e5d28a
TG
6669 TARGET_FMT_lx " XER " TARGET_FMT_lx " CPU#%d\n",
6670 env->nip, env->lr, env->ctr, cpu_read_xer(env),
6671 cs->cpu_index);
90e189ec 6672 cpu_fprintf(f, "MSR " TARGET_FMT_lx " HID0 " TARGET_FMT_lx " HF "
9fb04491
BH
6673 TARGET_FMT_lx " iidx %d didx %d\n",
6674 env->msr, env->spr[SPR_HID0],
6675 env->hflags, env->immu_idx, env->dmmu_idx);
d9bce9d9 6676#if !defined(NO_TIMER_DUMP)
9a78eead 6677 cpu_fprintf(f, "TB %08" PRIu32 " %08" PRIu64
76a66253 6678#if !defined(CONFIG_USER_ONLY)
9a78eead 6679 " DECR %08" PRIu32
76a66253
JM
6680#endif
6681 "\n",
077fc206 6682 cpu_ppc_load_tbu(env), cpu_ppc_load_tbl(env)
76a66253
JM
6683#if !defined(CONFIG_USER_ONLY)
6684 , cpu_ppc_load_decr(env)
6685#endif
6686 );
077fc206 6687#endif
76a66253 6688 for (i = 0; i < 32; i++) {
3fc6c082
FB
6689 if ((i & (RGPL - 1)) == 0)
6690 cpu_fprintf(f, "GPR%02d", i);
b11ebf64 6691 cpu_fprintf(f, " %016" PRIx64, ppc_dump_gpr(env, i));
3fc6c082 6692 if ((i & (RGPL - 1)) == (RGPL - 1))
7fe48483 6693 cpu_fprintf(f, "\n");
76a66253 6694 }
3fc6c082 6695 cpu_fprintf(f, "CR ");
76a66253 6696 for (i = 0; i < 8; i++)
7fe48483
FB
6697 cpu_fprintf(f, "%01x", env->crf[i]);
6698 cpu_fprintf(f, " [");
76a66253
JM
6699 for (i = 0; i < 8; i++) {
6700 char a = '-';
6701 if (env->crf[i] & 0x08)
6702 a = 'L';
6703 else if (env->crf[i] & 0x04)
6704 a = 'G';
6705 else if (env->crf[i] & 0x02)
6706 a = 'E';
7fe48483 6707 cpu_fprintf(f, " %c%c", a, env->crf[i] & 0x01 ? 'O' : ' ');
76a66253 6708 }
90e189ec
BS
6709 cpu_fprintf(f, " ] RES " TARGET_FMT_lx "\n",
6710 env->reserve_addr);
3fc6c082
FB
6711 for (i = 0; i < 32; i++) {
6712 if ((i & (RFPL - 1)) == 0)
6713 cpu_fprintf(f, "FPR%02d", i);
26a76461 6714 cpu_fprintf(f, " %016" PRIx64, *((uint64_t *)&env->fpr[i]));
3fc6c082 6715 if ((i & (RFPL - 1)) == (RFPL - 1))
7fe48483 6716 cpu_fprintf(f, "\n");
79aceca5 6717 }
30304420 6718 cpu_fprintf(f, "FPSCR " TARGET_FMT_lx "\n", env->fpscr);
f2e63a42 6719#if !defined(CONFIG_USER_ONLY)
90dc8812
SW
6720 cpu_fprintf(f, " SRR0 " TARGET_FMT_lx " SRR1 " TARGET_FMT_lx
6721 " PVR " TARGET_FMT_lx " VRSAVE " TARGET_FMT_lx "\n",
6722 env->spr[SPR_SRR0], env->spr[SPR_SRR1],
6723 env->spr[SPR_PVR], env->spr[SPR_VRSAVE]);
6724
6725 cpu_fprintf(f, "SPRG0 " TARGET_FMT_lx " SPRG1 " TARGET_FMT_lx
6726 " SPRG2 " TARGET_FMT_lx " SPRG3 " TARGET_FMT_lx "\n",
6727 env->spr[SPR_SPRG0], env->spr[SPR_SPRG1],
6728 env->spr[SPR_SPRG2], env->spr[SPR_SPRG3]);
6729
6730 cpu_fprintf(f, "SPRG4 " TARGET_FMT_lx " SPRG5 " TARGET_FMT_lx
6731 " SPRG6 " TARGET_FMT_lx " SPRG7 " TARGET_FMT_lx "\n",
6732 env->spr[SPR_SPRG4], env->spr[SPR_SPRG5],
6733 env->spr[SPR_SPRG6], env->spr[SPR_SPRG7]);
6734
f2b70fde
BH
6735#if defined(TARGET_PPC64)
6736 if (env->excp_model == POWERPC_EXCP_POWER7 ||
6737 env->excp_model == POWERPC_EXCP_POWER8) {
6738 cpu_fprintf(f, "HSRR0 " TARGET_FMT_lx " HSRR1 " TARGET_FMT_lx "\n",
6739 env->spr[SPR_HSRR0], env->spr[SPR_HSRR1]);
6740 }
6741#endif
90dc8812
SW
6742 if (env->excp_model == POWERPC_EXCP_BOOKE) {
6743 cpu_fprintf(f, "CSRR0 " TARGET_FMT_lx " CSRR1 " TARGET_FMT_lx
6744 " MCSRR0 " TARGET_FMT_lx " MCSRR1 " TARGET_FMT_lx "\n",
6745 env->spr[SPR_BOOKE_CSRR0], env->spr[SPR_BOOKE_CSRR1],
6746 env->spr[SPR_BOOKE_MCSRR0], env->spr[SPR_BOOKE_MCSRR1]);
6747
6748 cpu_fprintf(f, " TCR " TARGET_FMT_lx " TSR " TARGET_FMT_lx
6749 " ESR " TARGET_FMT_lx " DEAR " TARGET_FMT_lx "\n",
6750 env->spr[SPR_BOOKE_TCR], env->spr[SPR_BOOKE_TSR],
6751 env->spr[SPR_BOOKE_ESR], env->spr[SPR_BOOKE_DEAR]);
6752
6753 cpu_fprintf(f, " PIR " TARGET_FMT_lx " DECAR " TARGET_FMT_lx
6754 " IVPR " TARGET_FMT_lx " EPCR " TARGET_FMT_lx "\n",
6755 env->spr[SPR_BOOKE_PIR], env->spr[SPR_BOOKE_DECAR],
6756 env->spr[SPR_BOOKE_IVPR], env->spr[SPR_BOOKE_EPCR]);
6757
6758 cpu_fprintf(f, " MCSR " TARGET_FMT_lx " SPRG8 " TARGET_FMT_lx
6759 " EPR " TARGET_FMT_lx "\n",
6760 env->spr[SPR_BOOKE_MCSR], env->spr[SPR_BOOKE_SPRG8],
6761 env->spr[SPR_BOOKE_EPR]);
6762
6763 /* FSL-specific */
6764 cpu_fprintf(f, " MCAR " TARGET_FMT_lx " PID1 " TARGET_FMT_lx
6765 " PID2 " TARGET_FMT_lx " SVR " TARGET_FMT_lx "\n",
6766 env->spr[SPR_Exxx_MCAR], env->spr[SPR_BOOKE_PID1],
6767 env->spr[SPR_BOOKE_PID2], env->spr[SPR_E500_SVR]);
6768
6769 /*
6770 * IVORs are left out as they are large and do not change often --
6771 * they can be read with "p $ivor0", "p $ivor1", etc.
6772 */
6773 }
6774
697ab892
DG
6775#if defined(TARGET_PPC64)
6776 if (env->flags & POWERPC_FLAG_CFAR) {
6777 cpu_fprintf(f, " CFAR " TARGET_FMT_lx"\n", env->cfar);
6778 }
6779#endif
6780
90dc8812
SW
6781 switch (env->mmu_model) {
6782 case POWERPC_MMU_32B:
6783 case POWERPC_MMU_601:
6784 case POWERPC_MMU_SOFT_6xx:
6785 case POWERPC_MMU_SOFT_74xx:
6786#if defined(TARGET_PPC64)
90dc8812 6787 case POWERPC_MMU_64B:
aa4bb587 6788 case POWERPC_MMU_2_03:
ca480de6 6789 case POWERPC_MMU_2_06:
808bc3b0 6790 case POWERPC_MMU_2_06a:
aa4bb587 6791 case POWERPC_MMU_2_07:
808bc3b0 6792 case POWERPC_MMU_2_07a:
90dc8812 6793#endif
ca480de6
AB
6794 cpu_fprintf(f, " SDR1 " TARGET_FMT_lx " DAR " TARGET_FMT_lx
6795 " DSISR " TARGET_FMT_lx "\n", env->spr[SPR_SDR1],
6796 env->spr[SPR_DAR], env->spr[SPR_DSISR]);
90dc8812 6797 break;
01662f3e 6798 case POWERPC_MMU_BOOKE206:
90dc8812
SW
6799 cpu_fprintf(f, " MAS0 " TARGET_FMT_lx " MAS1 " TARGET_FMT_lx
6800 " MAS2 " TARGET_FMT_lx " MAS3 " TARGET_FMT_lx "\n",
6801 env->spr[SPR_BOOKE_MAS0], env->spr[SPR_BOOKE_MAS1],
6802 env->spr[SPR_BOOKE_MAS2], env->spr[SPR_BOOKE_MAS3]);
6803
6804 cpu_fprintf(f, " MAS4 " TARGET_FMT_lx " MAS6 " TARGET_FMT_lx
6805 " MAS7 " TARGET_FMT_lx " PID " TARGET_FMT_lx "\n",
6806 env->spr[SPR_BOOKE_MAS4], env->spr[SPR_BOOKE_MAS6],
6807 env->spr[SPR_BOOKE_MAS7], env->spr[SPR_BOOKE_PID]);
6808
6809 cpu_fprintf(f, "MMUCFG " TARGET_FMT_lx " TLB0CFG " TARGET_FMT_lx
6810 " TLB1CFG " TARGET_FMT_lx "\n",
6811 env->spr[SPR_MMUCFG], env->spr[SPR_BOOKE_TLB0CFG],
6812 env->spr[SPR_BOOKE_TLB1CFG]);
6813 break;
6814 default:
6815 break;
6816 }
f2e63a42 6817#endif
79aceca5 6818
3fc6c082
FB
6819#undef RGPL
6820#undef RFPL
79aceca5
FB
6821}
6822
878096ee
AF
6823void ppc_cpu_dump_statistics(CPUState *cs, FILE*f,
6824 fprintf_function cpu_fprintf, int flags)
76a66253
JM
6825{
6826#if defined(DO_PPC_STATISTICS)
878096ee 6827 PowerPCCPU *cpu = POWERPC_CPU(cs);
c227f099 6828 opc_handler_t **t1, **t2, **t3, *handler;
76a66253
JM
6829 int op1, op2, op3;
6830
878096ee 6831 t1 = cpu->env.opcodes;
76a66253
JM
6832 for (op1 = 0; op1 < 64; op1++) {
6833 handler = t1[op1];
6834 if (is_indirect_opcode(handler)) {
6835 t2 = ind_table(handler);
6836 for (op2 = 0; op2 < 32; op2++) {
6837 handler = t2[op2];
6838 if (is_indirect_opcode(handler)) {
6839 t3 = ind_table(handler);
6840 for (op3 = 0; op3 < 32; op3++) {
6841 handler = t3[op3];
6842 if (handler->count == 0)
6843 continue;
6844 cpu_fprintf(f, "%02x %02x %02x (%02x %04d) %16s: "
0bfcd599 6845 "%016" PRIx64 " %" PRId64 "\n",
76a66253
JM
6846 op1, op2, op3, op1, (op3 << 5) | op2,
6847 handler->oname,
6848 handler->count, handler->count);
6849 }
6850 } else {
6851 if (handler->count == 0)
6852 continue;
6853 cpu_fprintf(f, "%02x %02x (%02x %04d) %16s: "
0bfcd599 6854 "%016" PRIx64 " %" PRId64 "\n",
76a66253
JM
6855 op1, op2, op1, op2, handler->oname,
6856 handler->count, handler->count);
6857 }
6858 }
6859 } else {
6860 if (handler->count == 0)
6861 continue;
0bfcd599
BS
6862 cpu_fprintf(f, "%02x (%02x ) %16s: %016" PRIx64
6863 " %" PRId64 "\n",
76a66253
JM
6864 op1, op1, handler->oname,
6865 handler->count, handler->count);
6866 }
6867 }
6868#endif
6869}
6870
9a64fbe4 6871/*****************************************************************************/
4e5e1215 6872void gen_intermediate_code(CPUPPCState *env, struct TranslationBlock *tb)
79aceca5 6873{
4e5e1215 6874 PowerPCCPU *cpu = ppc_env_get_cpu(env);
ed2803da 6875 CPUState *cs = CPU(cpu);
9fddaa0c 6876 DisasContext ctx, *ctxp = &ctx;
c227f099 6877 opc_handler_t **table, *handler;
0fa85d43 6878 target_ulong pc_start;
2e70f6ef
PB
6879 int num_insns;
6880 int max_insns;
79aceca5
FB
6881
6882 pc_start = tb->pc;
046d6672 6883 ctx.nip = pc_start;
79aceca5 6884 ctx.tb = tb;
e1833e1f 6885 ctx.exception = POWERPC_EXCP_NONE;
3fc6c082 6886 ctx.spr_cb = env->spr_cb;
c47493f2 6887 ctx.pr = msr_pr;
9fb04491 6888 ctx.mem_idx = env->dmmu_idx;
b7815375 6889 ctx.dr = msr_dr;
932ccbdd
BH
6890#if !defined(CONFIG_USER_ONLY)
6891 ctx.hv = msr_hv || !env->has_hv_mode;
6892#endif
7d08d856
AJ
6893 ctx.insns_flags = env->insns_flags;
6894 ctx.insns_flags2 = env->insns_flags2;
76db3ba4 6895 ctx.access_type = -1;
5c3ae929 6896 ctx.le_mode = !!(env->hflags & (1 << MSR_LE));
e22c357b 6897 ctx.default_tcg_memop_mask = ctx.le_mode ? MO_LE : MO_BE;
d9bce9d9 6898#if defined(TARGET_PPC64)
e42a61f1 6899 ctx.sf_mode = msr_is_64bit(env, env->msr);
697ab892 6900 ctx.has_cfar = !!(env->flags & POWERPC_FLAG_CFAR);
9a64fbe4 6901#endif
c5a8d8f3
BH
6902 if (env->mmu_model == POWERPC_MMU_32B ||
6903 env->mmu_model == POWERPC_MMU_601 ||
6904 (env->mmu_model & POWERPC_MMU_64B))
6905 ctx.lazy_tlb_flush = true;
6906
5c3ae929 6907 ctx.fpu_enabled = !!msr_fp;
a9d9eb8f 6908 if ((env->flags & POWERPC_FLAG_SPE) && msr_spe)
5c3ae929 6909 ctx.spe_enabled = !!msr_spe;
d26bfc9a 6910 else
5c3ae929 6911 ctx.spe_enabled = false;
a9d9eb8f 6912 if ((env->flags & POWERPC_FLAG_VRE) && msr_vr)
5c3ae929 6913 ctx.altivec_enabled = !!msr_vr;
a9d9eb8f 6914 else
5c3ae929 6915 ctx.altivec_enabled = false;
1f29871c 6916 if ((env->flags & POWERPC_FLAG_VSX) && msr_vsx) {
5c3ae929 6917 ctx.vsx_enabled = !!msr_vsx;
1f29871c 6918 } else {
5c3ae929 6919 ctx.vsx_enabled = false;
1f29871c 6920 }
69d1a937
TM
6921#if defined(TARGET_PPC64)
6922 if ((env->flags & POWERPC_FLAG_TM) && msr_tm) {
5c3ae929 6923 ctx.tm_enabled = !!msr_tm;
69d1a937 6924 } else {
5c3ae929 6925 ctx.tm_enabled = false;
69d1a937
TM
6926 }
6927#endif
d26bfc9a 6928 if ((env->flags & POWERPC_FLAG_SE) && msr_se)
8cbcb4fa 6929 ctx.singlestep_enabled = CPU_SINGLE_STEP;
d26bfc9a 6930 else
8cbcb4fa 6931 ctx.singlestep_enabled = 0;
d26bfc9a 6932 if ((env->flags & POWERPC_FLAG_BE) && msr_be)
8cbcb4fa 6933 ctx.singlestep_enabled |= CPU_BRANCH_STEP;
ed2803da 6934 if (unlikely(cs->singlestep_enabled)) {
8cbcb4fa 6935 ctx.singlestep_enabled |= GDBSTUB_SINGLE_STEP;
ed2803da 6936 }
3fc6c082 6937#if defined (DO_SINGLE_STEP) && 0
9a64fbe4
FB
6938 /* Single step trace mode */
6939 msr_se = 1;
6940#endif
2e70f6ef
PB
6941 num_insns = 0;
6942 max_insns = tb->cflags & CF_COUNT_MASK;
190ce7fb 6943 if (max_insns == 0) {
2e70f6ef 6944 max_insns = CF_COUNT_MASK;
190ce7fb
RH
6945 }
6946 if (max_insns > TCG_MAX_INSNS) {
6947 max_insns = TCG_MAX_INSNS;
6948 }
2e70f6ef 6949
cd42d5b2 6950 gen_tb_start(tb);
3de31797 6951 tcg_clear_temp_count();
9a64fbe4 6952 /* Set env in case of segfault during code fetch */
fe700adb 6953 while (ctx.exception == POWERPC_EXCP_NONE && !tcg_op_buf_full()) {
667b8e29 6954 tcg_gen_insn_start(ctx.nip);
959082fc 6955 num_insns++;
667b8e29 6956
b933066a
RH
6957 if (unlikely(cpu_breakpoint_test(cs, ctx.nip, BP_ANY))) {
6958 gen_debug_exception(ctxp);
522a0d4e
RH
6959 /* The address covered by the breakpoint must be included in
6960 [tb->pc, tb->pc + tb->size) in order to for it to be
6961 properly cleared -- thus we increment the PC here so that
6962 the logic setting tb->size below does the right thing. */
6963 ctx.nip += 4;
b933066a
RH
6964 break;
6965 }
6966
d12d51d5 6967 LOG_DISAS("----------------\n");
90e189ec 6968 LOG_DISAS("nip=" TARGET_FMT_lx " super=%d ir=%d\n",
d12d51d5 6969 ctx.nip, ctx.mem_idx, (int)msr_ir);
959082fc 6970 if (num_insns == max_insns && (tb->cflags & CF_LAST_IO))
2e70f6ef 6971 gen_io_start();
e22c357b 6972 if (unlikely(need_byteswap(&ctx))) {
2f5a189c 6973 ctx.opcode = bswap32(cpu_ldl_code(env, ctx.nip));
056401ea 6974 } else {
2f5a189c 6975 ctx.opcode = cpu_ldl_code(env, ctx.nip);
111bfab3 6976 }
323ad19b
ND
6977 LOG_DISAS("translate opcode %08x (%02x %02x %02x %02x) (%s)\n",
6978 ctx.opcode, opc1(ctx.opcode), opc2(ctx.opcode),
6979 opc3(ctx.opcode), opc4(ctx.opcode),
6980 ctx.le_mode ? "little" : "big");
046d6672 6981 ctx.nip += 4;
3fc6c082 6982 table = env->opcodes;
79aceca5
FB
6983 handler = table[opc1(ctx.opcode)];
6984 if (is_indirect_opcode(handler)) {
6985 table = ind_table(handler);
6986 handler = table[opc2(ctx.opcode)];
6987 if (is_indirect_opcode(handler)) {
6988 table = ind_table(handler);
6989 handler = table[opc3(ctx.opcode)];
323ad19b
ND
6990 if (is_indirect_opcode(handler)) {
6991 table = ind_table(handler);
6992 handler = table[opc4(ctx.opcode)];
6993 }
79aceca5
FB
6994 }
6995 }
6996 /* Is opcode *REALLY* valid ? */
76a66253 6997 if (unlikely(handler->handler == &gen_invalid)) {
48880da6 6998 qemu_log_mask(LOG_GUEST_ERROR, "invalid/unsupported opcode: "
323ad19b
ND
6999 "%02x - %02x - %02x - %02x (%08x) "
7000 TARGET_FMT_lx " %d\n",
48880da6 7001 opc1(ctx.opcode), opc2(ctx.opcode),
323ad19b
ND
7002 opc3(ctx.opcode), opc4(ctx.opcode),
7003 ctx.opcode, ctx.nip - 4, (int)msr_ir);
76a66253 7004 } else {
70560da7
FC
7005 uint32_t inval;
7006
7007 if (unlikely(handler->type & (PPC_SPE | PPC_SPE_SINGLE | PPC_SPE_DOUBLE) && Rc(ctx.opcode))) {
7008 inval = handler->inval2;
7009 } else {
7010 inval = handler->inval1;
7011 }
7012
7013 if (unlikely((ctx.opcode & inval) != 0)) {
48880da6 7014 qemu_log_mask(LOG_GUEST_ERROR, "invalid bits: %08x for opcode: "
323ad19b
ND
7015 "%02x - %02x - %02x - %02x (%08x) "
7016 TARGET_FMT_lx "\n", ctx.opcode & inval,
7017 opc1(ctx.opcode), opc2(ctx.opcode),
7018 opc3(ctx.opcode), opc4(ctx.opcode),
48880da6 7019 ctx.opcode, ctx.nip - 4);
e06fcd75 7020 gen_inval_exception(ctxp, POWERPC_EXCP_INVAL_INVAL);
4b3686fa 7021 break;
79aceca5 7022 }
79aceca5 7023 }
4b3686fa 7024 (*(handler->handler))(&ctx);
76a66253
JM
7025#if defined(DO_PPC_STATISTICS)
7026 handler->count++;
7027#endif
9a64fbe4 7028 /* Check trace mode exceptions */
8cbcb4fa
AJ
7029 if (unlikely(ctx.singlestep_enabled & CPU_SINGLE_STEP &&
7030 (ctx.nip <= 0x100 || ctx.nip > 0xF00) &&
7031 ctx.exception != POWERPC_SYSCALL &&
7032 ctx.exception != POWERPC_EXCP_TRAP &&
7033 ctx.exception != POWERPC_EXCP_BRANCH)) {
bd6fefe7 7034 gen_exception_nip(ctxp, POWERPC_EXCP_TRACE, ctx.nip);
d26bfc9a 7035 } else if (unlikely(((ctx.nip & (TARGET_PAGE_SIZE - 1)) == 0) ||
ed2803da 7036 (cs->singlestep_enabled) ||
1b530a6d 7037 singlestep ||
2e70f6ef 7038 num_insns >= max_insns)) {
d26bfc9a
JM
7039 /* if we reach a page boundary or are single stepping, stop
7040 * generation
7041 */
8dd4983c 7042 break;
76a66253 7043 }
3de31797 7044 if (tcg_check_temp_count()) {
323ad19b
ND
7045 fprintf(stderr, "Opcode %02x %02x %02x %02x (%08x) leaked "
7046 "temporaries\n", opc1(ctx.opcode), opc2(ctx.opcode),
7047 opc3(ctx.opcode), opc4(ctx.opcode), ctx.opcode);
3de31797
AG
7048 exit(1);
7049 }
3fc6c082 7050 }
2e70f6ef
PB
7051 if (tb->cflags & CF_LAST_IO)
7052 gen_io_end();
e1833e1f 7053 if (ctx.exception == POWERPC_EXCP_NONE) {
c1942362 7054 gen_goto_tb(&ctx, 0, ctx.nip);
e1833e1f 7055 } else if (ctx.exception != POWERPC_EXCP_BRANCH) {
ed2803da 7056 if (unlikely(cs->singlestep_enabled)) {
e06fcd75 7057 gen_debug_exception(ctxp);
8cbcb4fa 7058 }
76a66253 7059 /* Generate the return instruction */
57fec1fe 7060 tcg_gen_exit_tb(0);
9a64fbe4 7061 }
806f352d 7062 gen_tb_end(tb, num_insns);
0a7df5da 7063
4e5e1215
RH
7064 tb->size = ctx.nip - pc_start;
7065 tb->icount = num_insns;
7066
d9bce9d9 7067#if defined(DEBUG_DISAS)
4910e6e4
RH
7068 if (qemu_loglevel_mask(CPU_LOG_TB_IN_ASM)
7069 && qemu_log_in_addr_range(pc_start)) {
76a66253 7070 int flags;
237c0af0 7071 flags = env->bfd_mach;
76db3ba4 7072 flags |= ctx.le_mode << 16;
93fcfe39 7073 qemu_log("IN: %s\n", lookup_symbol(pc_start));
d49190c4 7074 log_target_disas(cs, pc_start, ctx.nip - pc_start, flags);
93fcfe39 7075 qemu_log("\n");
9fddaa0c 7076 }
79aceca5 7077#endif
79aceca5
FB
7078}
7079
bad729e2
RH
7080void restore_state_to_opc(CPUPPCState *env, TranslationBlock *tb,
7081 target_ulong *data)
d2856f1a 7082{
bad729e2 7083 env->nip = data[0];
d2856f1a 7084}
This page took 2.907153 seconds and 4 git commands to generate.