]> Git Repo - qemu.git/blob - target-ppc/translate.c
target-ppc: add modulo word operations
[qemu.git] / target-ppc / translate.c
1 /*
2  *  PowerPC emulation for qemu: main translation routines.
3  *
4  *  Copyright (c) 2003-2007 Jocelyn Mayer
5  *  Copyright (C) 2011 Freescale Semiconductor, Inc.
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
18  * License along with this library; if not, see <http://www.gnu.org/licenses/>.
19  */
20
21 #include "qemu/osdep.h"
22 #include "cpu.h"
23 #include "disas/disas.h"
24 #include "exec/exec-all.h"
25 #include "tcg-op.h"
26 #include "qemu/host-utils.h"
27 #include "exec/cpu_ldst.h"
28
29 #include "exec/helper-proto.h"
30 #include "exec/helper-gen.h"
31
32 #include "trace-tcg.h"
33 #include "exec/log.h"
34
35
36 #define CPU_SINGLE_STEP 0x1
37 #define CPU_BRANCH_STEP 0x2
38 #define GDBSTUB_SINGLE_STEP 0x4
39
40 /* Include definitions for instructions classes and implementations flags */
41 //#define PPC_DEBUG_DISAS
42 //#define DO_PPC_STATISTICS
43
44 #ifdef PPC_DEBUG_DISAS
45 #  define LOG_DISAS(...) qemu_log_mask(CPU_LOG_TB_IN_ASM, ## __VA_ARGS__)
46 #else
47 #  define LOG_DISAS(...) do { } while (0)
48 #endif
49 /*****************************************************************************/
50 /* Code translation helpers                                                  */
51
52 /* global register indexes */
53 static TCGv_env cpu_env;
54 static char cpu_reg_names[10*3 + 22*4 /* GPR */
55     + 10*4 + 22*5 /* SPE GPRh */
56     + 10*4 + 22*5 /* FPR */
57     + 2*(10*6 + 22*7) /* AVRh, AVRl */
58     + 10*5 + 22*6 /* VSR */
59     + 8*5 /* CRF */];
60 static TCGv cpu_gpr[32];
61 static TCGv cpu_gprh[32];
62 static TCGv_i64 cpu_fpr[32];
63 static TCGv_i64 cpu_avrh[32], cpu_avrl[32];
64 static TCGv_i64 cpu_vsr[32];
65 static TCGv_i32 cpu_crf[8];
66 static TCGv cpu_nip;
67 static TCGv cpu_msr;
68 static TCGv cpu_ctr;
69 static TCGv cpu_lr;
70 #if defined(TARGET_PPC64)
71 static TCGv cpu_cfar;
72 #endif
73 static TCGv cpu_xer, cpu_so, cpu_ov, cpu_ca;
74 static TCGv cpu_reserve;
75 static TCGv cpu_fpscr;
76 static TCGv_i32 cpu_access_type;
77
78 #include "exec/gen-icount.h"
79
80 void ppc_translate_init(void)
81 {
82     int i;
83     char* p;
84     size_t cpu_reg_names_size;
85     static int done_init = 0;
86
87     if (done_init)
88         return;
89
90     cpu_env = tcg_global_reg_new_ptr(TCG_AREG0, "env");
91     tcg_ctx.tcg_env = cpu_env;
92
93     p = cpu_reg_names;
94     cpu_reg_names_size = sizeof(cpu_reg_names);
95
96     for (i = 0; i < 8; i++) {
97         snprintf(p, cpu_reg_names_size, "crf%d", i);
98         cpu_crf[i] = tcg_global_mem_new_i32(cpu_env,
99                                             offsetof(CPUPPCState, crf[i]), p);
100         p += 5;
101         cpu_reg_names_size -= 5;
102     }
103
104     for (i = 0; i < 32; i++) {
105         snprintf(p, cpu_reg_names_size, "r%d", i);
106         cpu_gpr[i] = tcg_global_mem_new(cpu_env,
107                                         offsetof(CPUPPCState, gpr[i]), p);
108         p += (i < 10) ? 3 : 4;
109         cpu_reg_names_size -= (i < 10) ? 3 : 4;
110         snprintf(p, cpu_reg_names_size, "r%dH", i);
111         cpu_gprh[i] = tcg_global_mem_new(cpu_env,
112                                          offsetof(CPUPPCState, gprh[i]), p);
113         p += (i < 10) ? 4 : 5;
114         cpu_reg_names_size -= (i < 10) ? 4 : 5;
115
116         snprintf(p, cpu_reg_names_size, "fp%d", i);
117         cpu_fpr[i] = tcg_global_mem_new_i64(cpu_env,
118                                             offsetof(CPUPPCState, fpr[i]), p);
119         p += (i < 10) ? 4 : 5;
120         cpu_reg_names_size -= (i < 10) ? 4 : 5;
121
122         snprintf(p, cpu_reg_names_size, "avr%dH", i);
123 #ifdef HOST_WORDS_BIGENDIAN
124         cpu_avrh[i] = tcg_global_mem_new_i64(cpu_env,
125                                              offsetof(CPUPPCState, avr[i].u64[0]), p);
126 #else
127         cpu_avrh[i] = tcg_global_mem_new_i64(cpu_env,
128                                              offsetof(CPUPPCState, avr[i].u64[1]), p);
129 #endif
130         p += (i < 10) ? 6 : 7;
131         cpu_reg_names_size -= (i < 10) ? 6 : 7;
132
133         snprintf(p, cpu_reg_names_size, "avr%dL", i);
134 #ifdef HOST_WORDS_BIGENDIAN
135         cpu_avrl[i] = tcg_global_mem_new_i64(cpu_env,
136                                              offsetof(CPUPPCState, avr[i].u64[1]), p);
137 #else
138         cpu_avrl[i] = tcg_global_mem_new_i64(cpu_env,
139                                              offsetof(CPUPPCState, avr[i].u64[0]), p);
140 #endif
141         p += (i < 10) ? 6 : 7;
142         cpu_reg_names_size -= (i < 10) ? 6 : 7;
143         snprintf(p, cpu_reg_names_size, "vsr%d", i);
144         cpu_vsr[i] = tcg_global_mem_new_i64(cpu_env,
145                                             offsetof(CPUPPCState, vsr[i]), p);
146         p += (i < 10) ? 5 : 6;
147         cpu_reg_names_size -= (i < 10) ? 5 : 6;
148     }
149
150     cpu_nip = tcg_global_mem_new(cpu_env,
151                                  offsetof(CPUPPCState, nip), "nip");
152
153     cpu_msr = tcg_global_mem_new(cpu_env,
154                                  offsetof(CPUPPCState, msr), "msr");
155
156     cpu_ctr = tcg_global_mem_new(cpu_env,
157                                  offsetof(CPUPPCState, ctr), "ctr");
158
159     cpu_lr = tcg_global_mem_new(cpu_env,
160                                 offsetof(CPUPPCState, lr), "lr");
161
162 #if defined(TARGET_PPC64)
163     cpu_cfar = tcg_global_mem_new(cpu_env,
164                                   offsetof(CPUPPCState, cfar), "cfar");
165 #endif
166
167     cpu_xer = tcg_global_mem_new(cpu_env,
168                                  offsetof(CPUPPCState, xer), "xer");
169     cpu_so = tcg_global_mem_new(cpu_env,
170                                 offsetof(CPUPPCState, so), "SO");
171     cpu_ov = tcg_global_mem_new(cpu_env,
172                                 offsetof(CPUPPCState, ov), "OV");
173     cpu_ca = tcg_global_mem_new(cpu_env,
174                                 offsetof(CPUPPCState, ca), "CA");
175
176     cpu_reserve = tcg_global_mem_new(cpu_env,
177                                      offsetof(CPUPPCState, reserve_addr),
178                                      "reserve_addr");
179
180     cpu_fpscr = tcg_global_mem_new(cpu_env,
181                                    offsetof(CPUPPCState, fpscr), "fpscr");
182
183     cpu_access_type = tcg_global_mem_new_i32(cpu_env,
184                                              offsetof(CPUPPCState, access_type), "access_type");
185
186     done_init = 1;
187 }
188
189 /* internal defines */
190 struct DisasContext {
191     struct TranslationBlock *tb;
192     target_ulong nip;
193     uint32_t opcode;
194     uint32_t exception;
195     /* Routine used to access memory */
196     bool pr, hv, dr, le_mode;
197     bool lazy_tlb_flush;
198     int mem_idx;
199     int access_type;
200     /* Translation flags */
201     TCGMemOp default_tcg_memop_mask;
202 #if defined(TARGET_PPC64)
203     bool sf_mode;
204     bool has_cfar;
205 #endif
206     bool fpu_enabled;
207     bool altivec_enabled;
208     bool vsx_enabled;
209     bool spe_enabled;
210     bool tm_enabled;
211     ppc_spr_t *spr_cb; /* Needed to check rights for mfspr/mtspr */
212     int singlestep_enabled;
213     uint64_t insns_flags;
214     uint64_t insns_flags2;
215 };
216
217 /* Return true iff byteswap is needed in a scalar memop */
218 static 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
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
234 struct opc_handler_t {
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;
239     /* instruction type */
240     uint64_t type;
241     /* extended instruction type */
242     uint64_t type2;
243     /* handler */
244     void (*handler)(DisasContext *ctx);
245 #if defined(DO_PPC_STATISTICS) || defined(PPC_DUMP_CPU)
246     const char *oname;
247 #endif
248 #if defined(DO_PPC_STATISTICS)
249     uint64_t count;
250 #endif
251 };
252
253 static inline void gen_reset_fpstatus(void)
254 {
255     gen_helper_reset_fpstatus(cpu_env);
256 }
257
258 static inline void gen_compute_fprf(TCGv_i64 arg)
259 {
260     gen_helper_compute_fprf(cpu_env, arg);
261     gen_helper_float_check_status(cpu_env);
262 }
263
264 static inline void gen_set_access_type(DisasContext *ctx, int access_type)
265 {
266     if (ctx->access_type != access_type) {
267         tcg_gen_movi_i32(cpu_access_type, access_type);
268         ctx->access_type = access_type;
269     }
270 }
271
272 static inline void gen_update_nip(DisasContext *ctx, target_ulong nip)
273 {
274     if (NARROW_MODE(ctx)) {
275         nip = (uint32_t)nip;
276     }
277     tcg_gen_movi_tl(cpu_nip, nip);
278 }
279
280 void gen_update_current_nip(void *opaque)
281 {
282     DisasContext *ctx = opaque;
283
284     tcg_gen_movi_tl(cpu_nip, ctx->nip);
285 }
286
287 static void gen_exception_err(DisasContext *ctx, uint32_t excp, uint32_t error)
288 {
289     TCGv_i32 t0, t1;
290     if (ctx->exception == POWERPC_EXCP_NONE) {
291         gen_update_nip(ctx, ctx->nip);
292     }
293     t0 = tcg_const_i32(excp);
294     t1 = tcg_const_i32(error);
295     gen_helper_raise_exception_err(cpu_env, t0, t1);
296     tcg_temp_free_i32(t0);
297     tcg_temp_free_i32(t1);
298     ctx->exception = (excp);
299 }
300
301 static void gen_exception(DisasContext *ctx, uint32_t excp)
302 {
303     TCGv_i32 t0;
304     if (ctx->exception == POWERPC_EXCP_NONE) {
305         gen_update_nip(ctx, ctx->nip);
306     }
307     t0 = tcg_const_i32(excp);
308     gen_helper_raise_exception(cpu_env, t0);
309     tcg_temp_free_i32(t0);
310     ctx->exception = (excp);
311 }
312
313 static void gen_debug_exception(DisasContext *ctx)
314 {
315     TCGv_i32 t0;
316
317     if ((ctx->exception != POWERPC_EXCP_BRANCH) &&
318         (ctx->exception != POWERPC_EXCP_SYNC)) {
319         gen_update_nip(ctx, ctx->nip);
320     }
321     t0 = tcg_const_i32(EXCP_DEBUG);
322     gen_helper_raise_exception(cpu_env, t0);
323     tcg_temp_free_i32(t0);
324 }
325
326 static inline void gen_inval_exception(DisasContext *ctx, uint32_t error)
327 {
328     /* Will be converted to program check if needed */
329     gen_exception_err(ctx, POWERPC_EXCP_HV_EMU, POWERPC_EXCP_INVAL | error);
330 }
331
332 static inline void gen_priv_exception(DisasContext *ctx, uint32_t error)
333 {
334     gen_exception_err(ctx, POWERPC_EXCP_PROGRAM, POWERPC_EXCP_PRIV | error);
335 }
336
337 static inline void gen_hvpriv_exception(DisasContext *ctx, uint32_t error)
338 {
339     /* Will be converted to program check if needed */
340     gen_exception_err(ctx, POWERPC_EXCP_HV_EMU, POWERPC_EXCP_PRIV | error);
341 }
342
343 /* Stop translation */
344 static inline void gen_stop_exception(DisasContext *ctx)
345 {
346     gen_update_nip(ctx, ctx->nip);
347     ctx->exception = POWERPC_EXCP_STOP;
348 }
349
350 #ifndef CONFIG_USER_ONLY
351 /* No need to update nip here, as execution flow will change */
352 static inline void gen_sync_exception(DisasContext *ctx)
353 {
354     ctx->exception = POWERPC_EXCP_SYNC;
355 }
356 #endif
357
358 #define GEN_HANDLER(name, opc1, opc2, opc3, inval, type)                      \
359 GEN_OPCODE(name, opc1, opc2, opc3, inval, type, PPC_NONE)
360
361 #define GEN_HANDLER_E(name, opc1, opc2, opc3, inval, type, type2)             \
362 GEN_OPCODE(name, opc1, opc2, opc3, inval, type, type2)
363
364 #define GEN_HANDLER2(name, onam, opc1, opc2, opc3, inval, type)               \
365 GEN_OPCODE2(name, onam, opc1, opc2, opc3, inval, type, PPC_NONE)
366
367 #define GEN_HANDLER2_E(name, onam, opc1, opc2, opc3, inval, type, type2)      \
368 GEN_OPCODE2(name, onam, opc1, opc2, opc3, inval, type, type2)
369
370 typedef struct opcode_t {
371     unsigned char opc1, opc2, opc3;
372 #if HOST_LONG_BITS == 64 /* Explicitly align to 64 bits */
373     unsigned char pad[5];
374 #else
375     unsigned char pad[1];
376 #endif
377     opc_handler_t handler;
378     const char *oname;
379 } opcode_t;
380
381 /* Helpers for priv. check */
382 #define GEN_PRIV                                                \
383     do {                                                        \
384         gen_priv_exception(ctx, POWERPC_EXCP_PRIV_OPC); return; \
385     } while (0)
386
387 #if defined(CONFIG_USER_ONLY)
388 #define CHK_HV GEN_PRIV
389 #define CHK_SV GEN_PRIV
390 #define CHK_HVRM GEN_PRIV
391 #else
392 #define CHK_HV                                                          \
393     do {                                                                \
394         if (unlikely(ctx->pr || !ctx->hv)) {                            \
395             GEN_PRIV;                                                   \
396         }                                                               \
397     } while (0)
398 #define CHK_SV                   \
399     do {                         \
400         if (unlikely(ctx->pr)) { \
401             GEN_PRIV;            \
402         }                        \
403     } while (0)
404 #define CHK_HVRM                                            \
405     do {                                                    \
406         if (unlikely(ctx->pr || !ctx->hv || ctx->dr)) {     \
407             GEN_PRIV;                                       \
408         }                                                   \
409     } while (0)
410 #endif
411
412 #define CHK_NONE
413
414
415 /*****************************************************************************/
416 /***                           Instruction decoding                        ***/
417 #define EXTRACT_HELPER(name, shift, nb)                                       \
418 static inline uint32_t name(uint32_t opcode)                                  \
419 {                                                                             \
420     return (opcode >> (shift)) & ((1 << (nb)) - 1);                           \
421 }
422
423 #define EXTRACT_SHELPER(name, shift, nb)                                      \
424 static inline int32_t name(uint32_t opcode)                                   \
425 {                                                                             \
426     return (int16_t)((opcode >> (shift)) & ((1 << (nb)) - 1));                \
427 }
428
429 #define EXTRACT_HELPER_SPLIT(name, shift1, nb1, shift2, nb2)                  \
430 static inline uint32_t name(uint32_t opcode)                                  \
431 {                                                                             \
432     return (((opcode >> (shift1)) & ((1 << (nb1)) - 1)) << nb2) |             \
433             ((opcode >> (shift2)) & ((1 << (nb2)) - 1));                      \
434 }
435
436 #define EXTRACT_HELPER_DXFORM(name,                                           \
437                               d0_bits, shift_op_d0, shift_d0,                 \
438                               d1_bits, shift_op_d1, shift_d1,                 \
439                               d2_bits, shift_op_d2, shift_d2)                 \
440 static inline int16_t name(uint32_t opcode)                                   \
441 {                                                                             \
442     return                                                                    \
443         (((opcode >> (shift_op_d0)) & ((1 << (d0_bits)) - 1)) << (shift_d0)) | \
444         (((opcode >> (shift_op_d1)) & ((1 << (d1_bits)) - 1)) << (shift_d1)) | \
445         (((opcode >> (shift_op_d2)) & ((1 << (d2_bits)) - 1)) << (shift_d2));  \
446 }
447
448
449 /* Opcode part 1 */
450 EXTRACT_HELPER(opc1, 26, 6);
451 /* Opcode part 2 */
452 EXTRACT_HELPER(opc2, 1, 5);
453 /* Opcode part 3 */
454 EXTRACT_HELPER(opc3, 6, 5);
455 /* Update Cr0 flags */
456 EXTRACT_HELPER(Rc, 0, 1);
457 /* Update Cr6 flags (Altivec) */
458 EXTRACT_HELPER(Rc21, 10, 1);
459 /* Destination */
460 EXTRACT_HELPER(rD, 21, 5);
461 /* Source */
462 EXTRACT_HELPER(rS, 21, 5);
463 /* First operand */
464 EXTRACT_HELPER(rA, 16, 5);
465 /* Second operand */
466 EXTRACT_HELPER(rB, 11, 5);
467 /* Third operand */
468 EXTRACT_HELPER(rC, 6, 5);
469 /***                               Get CRn                                 ***/
470 EXTRACT_HELPER(crfD, 23, 3);
471 EXTRACT_HELPER(crfS, 18, 3);
472 EXTRACT_HELPER(crbD, 21, 5);
473 EXTRACT_HELPER(crbA, 16, 5);
474 EXTRACT_HELPER(crbB, 11, 5);
475 /* SPR / TBL */
476 EXTRACT_HELPER(_SPR, 11, 10);
477 static inline uint32_t SPR(uint32_t opcode)
478 {
479     uint32_t sprn = _SPR(opcode);
480
481     return ((sprn >> 5) & 0x1F) | ((sprn & 0x1F) << 5);
482 }
483 /***                              Get constants                            ***/
484 /* 16 bits signed immediate value */
485 EXTRACT_SHELPER(SIMM, 0, 16);
486 /* 16 bits unsigned immediate value */
487 EXTRACT_HELPER(UIMM, 0, 16);
488 /* 5 bits signed immediate value */
489 EXTRACT_HELPER(SIMM5, 16, 5);
490 /* 5 bits signed immediate value */
491 EXTRACT_HELPER(UIMM5, 16, 5);
492 /* Bit count */
493 EXTRACT_HELPER(NB, 11, 5);
494 /* Shift count */
495 EXTRACT_HELPER(SH, 11, 5);
496 /* Vector shift count */
497 EXTRACT_HELPER(VSH, 6, 4);
498 /* Mask start */
499 EXTRACT_HELPER(MB, 6, 5);
500 /* Mask end */
501 EXTRACT_HELPER(ME, 1, 5);
502 /* Trap operand */
503 EXTRACT_HELPER(TO, 21, 5);
504
505 EXTRACT_HELPER(CRM, 12, 8);
506
507 #ifndef CONFIG_USER_ONLY
508 EXTRACT_HELPER(SR, 16, 4);
509 #endif
510
511 /* mtfsf/mtfsfi */
512 EXTRACT_HELPER(FPBF, 23, 3);
513 EXTRACT_HELPER(FPIMM, 12, 4);
514 EXTRACT_HELPER(FPL, 25, 1);
515 EXTRACT_HELPER(FPFLM, 17, 8);
516 EXTRACT_HELPER(FPW, 16, 1);
517
518 /* addpcis */
519 EXTRACT_HELPER_DXFORM(DX, 10, 6, 6, 5, 16, 1, 1, 0, 0)
520
521 /***                            Jump target decoding                       ***/
522 /* Immediate address */
523 static inline target_ulong LI(uint32_t opcode)
524 {
525     return (opcode >> 0) & 0x03FFFFFC;
526 }
527
528 static inline uint32_t BD(uint32_t opcode)
529 {
530     return (opcode >> 0) & 0xFFFC;
531 }
532
533 EXTRACT_HELPER(BO, 21, 5);
534 EXTRACT_HELPER(BI, 16, 5);
535 /* Absolute/relative address */
536 EXTRACT_HELPER(AA, 1, 1);
537 /* Link */
538 EXTRACT_HELPER(LK, 0, 1);
539
540 /* DFP Z22-form */
541 EXTRACT_HELPER(DCM, 10, 6)
542
543 /* DFP Z23-form */
544 EXTRACT_HELPER(RMC, 9, 2)
545
546 /* Create a mask between <start> and <end> bits */
547 static inline target_ulong MASK(uint32_t start, uint32_t end)
548 {
549     target_ulong ret;
550
551 #if defined(TARGET_PPC64)
552     if (likely(start == 0)) {
553         ret = UINT64_MAX << (63 - end);
554     } else if (likely(end == 63)) {
555         ret = UINT64_MAX >> start;
556     }
557 #else
558     if (likely(start == 0)) {
559         ret = UINT32_MAX << (31  - end);
560     } else if (likely(end == 31)) {
561         ret = UINT32_MAX >> start;
562     }
563 #endif
564     else {
565         ret = (((target_ulong)(-1ULL)) >> (start)) ^
566             (((target_ulong)(-1ULL) >> (end)) >> 1);
567         if (unlikely(start > end))
568             return ~ret;
569     }
570
571     return ret;
572 }
573
574 EXTRACT_HELPER_SPLIT(xT, 0, 1, 21, 5);
575 EXTRACT_HELPER_SPLIT(xS, 0, 1, 21, 5);
576 EXTRACT_HELPER_SPLIT(xA, 2, 1, 16, 5);
577 EXTRACT_HELPER_SPLIT(xB, 1, 1, 11, 5);
578 EXTRACT_HELPER_SPLIT(xC, 3, 1,  6, 5);
579 EXTRACT_HELPER(DM, 8, 2);
580 EXTRACT_HELPER(UIM, 16, 2);
581 EXTRACT_HELPER(SHW, 8, 2);
582 EXTRACT_HELPER(SP, 19, 2);
583 /*****************************************************************************/
584 /* PowerPC instructions table                                                */
585
586 #if defined(DO_PPC_STATISTICS)
587 #define GEN_OPCODE(name, op1, op2, op3, invl, _typ, _typ2)                    \
588 {                                                                             \
589     .opc1 = op1,                                                              \
590     .opc2 = op2,                                                              \
591     .opc3 = op3,                                                              \
592     .pad  = { 0, },                                                           \
593     .handler = {                                                              \
594         .inval1  = invl,                                                      \
595         .type = _typ,                                                         \
596         .type2 = _typ2,                                                       \
597         .handler = &gen_##name,                                               \
598         .oname = stringify(name),                                             \
599     },                                                                        \
600     .oname = stringify(name),                                                 \
601 }
602 #define GEN_OPCODE_DUAL(name, op1, op2, op3, invl1, invl2, _typ, _typ2)       \
603 {                                                                             \
604     .opc1 = op1,                                                              \
605     .opc2 = op2,                                                              \
606     .opc3 = op3,                                                              \
607     .pad  = { 0, },                                                           \
608     .handler = {                                                              \
609         .inval1  = invl1,                                                     \
610         .inval2  = invl2,                                                     \
611         .type = _typ,                                                         \
612         .type2 = _typ2,                                                       \
613         .handler = &gen_##name,                                               \
614         .oname = stringify(name),                                             \
615     },                                                                        \
616     .oname = stringify(name),                                                 \
617 }
618 #define GEN_OPCODE2(name, onam, op1, op2, op3, invl, _typ, _typ2)             \
619 {                                                                             \
620     .opc1 = op1,                                                              \
621     .opc2 = op2,                                                              \
622     .opc3 = op3,                                                              \
623     .pad  = { 0, },                                                           \
624     .handler = {                                                              \
625         .inval1  = invl,                                                      \
626         .type = _typ,                                                         \
627         .type2 = _typ2,                                                       \
628         .handler = &gen_##name,                                               \
629         .oname = onam,                                                        \
630     },                                                                        \
631     .oname = onam,                                                            \
632 }
633 #else
634 #define GEN_OPCODE(name, op1, op2, op3, invl, _typ, _typ2)                    \
635 {                                                                             \
636     .opc1 = op1,                                                              \
637     .opc2 = op2,                                                              \
638     .opc3 = op3,                                                              \
639     .pad  = { 0, },                                                           \
640     .handler = {                                                              \
641         .inval1  = invl,                                                      \
642         .type = _typ,                                                         \
643         .type2 = _typ2,                                                       \
644         .handler = &gen_##name,                                               \
645     },                                                                        \
646     .oname = stringify(name),                                                 \
647 }
648 #define GEN_OPCODE_DUAL(name, op1, op2, op3, invl1, invl2, _typ, _typ2)       \
649 {                                                                             \
650     .opc1 = op1,                                                              \
651     .opc2 = op2,                                                              \
652     .opc3 = op3,                                                              \
653     .pad  = { 0, },                                                           \
654     .handler = {                                                              \
655         .inval1  = invl1,                                                     \
656         .inval2  = invl2,                                                     \
657         .type = _typ,                                                         \
658         .type2 = _typ2,                                                       \
659         .handler = &gen_##name,                                               \
660     },                                                                        \
661     .oname = stringify(name),                                                 \
662 }
663 #define GEN_OPCODE2(name, onam, op1, op2, op3, invl, _typ, _typ2)             \
664 {                                                                             \
665     .opc1 = op1,                                                              \
666     .opc2 = op2,                                                              \
667     .opc3 = op3,                                                              \
668     .pad  = { 0, },                                                           \
669     .handler = {                                                              \
670         .inval1  = invl,                                                      \
671         .type = _typ,                                                         \
672         .type2 = _typ2,                                                       \
673         .handler = &gen_##name,                                               \
674     },                                                                        \
675     .oname = onam,                                                            \
676 }
677 #endif
678
679 /* SPR load/store helpers */
680 static inline void gen_load_spr(TCGv t, int reg)
681 {
682     tcg_gen_ld_tl(t, cpu_env, offsetof(CPUPPCState, spr[reg]));
683 }
684
685 static inline void gen_store_spr(int reg, TCGv t)
686 {
687     tcg_gen_st_tl(t, cpu_env, offsetof(CPUPPCState, spr[reg]));
688 }
689
690 /* Invalid instruction */
691 static void gen_invalid(DisasContext *ctx)
692 {
693     gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL);
694 }
695
696 static opc_handler_t invalid_handler = {
697     .inval1  = 0xFFFFFFFF,
698     .inval2  = 0xFFFFFFFF,
699     .type    = PPC_NONE,
700     .type2   = PPC_NONE,
701     .handler = gen_invalid,
702 };
703
704 /***                           Integer comparison                          ***/
705
706 static inline void gen_op_cmp(TCGv arg0, TCGv arg1, int s, int crf)
707 {
708     TCGv t0 = tcg_temp_new();
709     TCGv_i32 t1 = tcg_temp_new_i32();
710
711     tcg_gen_trunc_tl_i32(cpu_crf[crf], cpu_so);
712
713     tcg_gen_setcond_tl((s ? TCG_COND_LT: TCG_COND_LTU), t0, arg0, arg1);
714     tcg_gen_trunc_tl_i32(t1, t0);
715     tcg_gen_shli_i32(t1, t1, CRF_LT);
716     tcg_gen_or_i32(cpu_crf[crf], cpu_crf[crf], t1);
717
718     tcg_gen_setcond_tl((s ? TCG_COND_GT: TCG_COND_GTU), t0, arg0, arg1);
719     tcg_gen_trunc_tl_i32(t1, t0);
720     tcg_gen_shli_i32(t1, t1, CRF_GT);
721     tcg_gen_or_i32(cpu_crf[crf], cpu_crf[crf], t1);
722
723     tcg_gen_setcond_tl(TCG_COND_EQ, t0, arg0, arg1);
724     tcg_gen_trunc_tl_i32(t1, t0);
725     tcg_gen_shli_i32(t1, t1, CRF_EQ);
726     tcg_gen_or_i32(cpu_crf[crf], cpu_crf[crf], t1);
727
728     tcg_temp_free(t0);
729     tcg_temp_free_i32(t1);
730 }
731
732 static inline void gen_op_cmpi(TCGv arg0, target_ulong arg1, int s, int crf)
733 {
734     TCGv t0 = tcg_const_tl(arg1);
735     gen_op_cmp(arg0, t0, s, crf);
736     tcg_temp_free(t0);
737 }
738
739 static inline void gen_op_cmp32(TCGv arg0, TCGv arg1, int s, int crf)
740 {
741     TCGv t0, t1;
742     t0 = tcg_temp_new();
743     t1 = tcg_temp_new();
744     if (s) {
745         tcg_gen_ext32s_tl(t0, arg0);
746         tcg_gen_ext32s_tl(t1, arg1);
747     } else {
748         tcg_gen_ext32u_tl(t0, arg0);
749         tcg_gen_ext32u_tl(t1, arg1);
750     }
751     gen_op_cmp(t0, t1, s, crf);
752     tcg_temp_free(t1);
753     tcg_temp_free(t0);
754 }
755
756 static inline void gen_op_cmpi32(TCGv arg0, target_ulong arg1, int s, int crf)
757 {
758     TCGv t0 = tcg_const_tl(arg1);
759     gen_op_cmp32(arg0, t0, s, crf);
760     tcg_temp_free(t0);
761 }
762
763 static inline void gen_set_Rc0(DisasContext *ctx, TCGv reg)
764 {
765     if (NARROW_MODE(ctx)) {
766         gen_op_cmpi32(reg, 0, 1, 0);
767     } else {
768         gen_op_cmpi(reg, 0, 1, 0);
769     }
770 }
771
772 /* cmp */
773 static void gen_cmp(DisasContext *ctx)
774 {
775     if ((ctx->opcode & 0x00200000) && (ctx->insns_flags & PPC_64B)) {
776         gen_op_cmp(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rB(ctx->opcode)],
777                    1, crfD(ctx->opcode));
778     } else {
779         gen_op_cmp32(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rB(ctx->opcode)],
780                      1, crfD(ctx->opcode));
781     }
782 }
783
784 /* cmpi */
785 static void gen_cmpi(DisasContext *ctx)
786 {
787     if ((ctx->opcode & 0x00200000) && (ctx->insns_flags & PPC_64B)) {
788         gen_op_cmpi(cpu_gpr[rA(ctx->opcode)], SIMM(ctx->opcode),
789                     1, crfD(ctx->opcode));
790     } else {
791         gen_op_cmpi32(cpu_gpr[rA(ctx->opcode)], SIMM(ctx->opcode),
792                       1, crfD(ctx->opcode));
793     }
794 }
795
796 /* cmpl */
797 static void gen_cmpl(DisasContext *ctx)
798 {
799     if ((ctx->opcode & 0x00200000) && (ctx->insns_flags & PPC_64B)) {
800         gen_op_cmp(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rB(ctx->opcode)],
801                    0, crfD(ctx->opcode));
802     } else {
803         gen_op_cmp32(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rB(ctx->opcode)],
804                      0, crfD(ctx->opcode));
805     }
806 }
807
808 /* cmpli */
809 static void gen_cmpli(DisasContext *ctx)
810 {
811     if ((ctx->opcode & 0x00200000) && (ctx->insns_flags & PPC_64B)) {
812         gen_op_cmpi(cpu_gpr[rA(ctx->opcode)], UIMM(ctx->opcode),
813                     0, crfD(ctx->opcode));
814     } else {
815         gen_op_cmpi32(cpu_gpr[rA(ctx->opcode)], UIMM(ctx->opcode),
816                       0, crfD(ctx->opcode));
817     }
818 }
819
820 /* cmprb - range comparison: isupper, isaplha, islower*/
821 static void gen_cmprb(DisasContext *ctx)
822 {
823     TCGv_i32 src1 = tcg_temp_new_i32();
824     TCGv_i32 src2 = tcg_temp_new_i32();
825     TCGv_i32 src2lo = tcg_temp_new_i32();
826     TCGv_i32 src2hi = tcg_temp_new_i32();
827     TCGv_i32 crf = cpu_crf[crfD(ctx->opcode)];
828
829     tcg_gen_trunc_tl_i32(src1, cpu_gpr[rA(ctx->opcode)]);
830     tcg_gen_trunc_tl_i32(src2, cpu_gpr[rB(ctx->opcode)]);
831
832     tcg_gen_andi_i32(src1, src1, 0xFF);
833     tcg_gen_ext8u_i32(src2lo, src2);
834     tcg_gen_shri_i32(src2, src2, 8);
835     tcg_gen_ext8u_i32(src2hi, src2);
836
837     tcg_gen_setcond_i32(TCG_COND_LEU, src2lo, src2lo, src1);
838     tcg_gen_setcond_i32(TCG_COND_LEU, src2hi, src1, src2hi);
839     tcg_gen_and_i32(crf, src2lo, src2hi);
840
841     if (ctx->opcode & 0x00200000) {
842         tcg_gen_shri_i32(src2, src2, 8);
843         tcg_gen_ext8u_i32(src2lo, src2);
844         tcg_gen_shri_i32(src2, src2, 8);
845         tcg_gen_ext8u_i32(src2hi, src2);
846         tcg_gen_setcond_i32(TCG_COND_LEU, src2lo, src2lo, src1);
847         tcg_gen_setcond_i32(TCG_COND_LEU, src2hi, src1, src2hi);
848         tcg_gen_and_i32(src2lo, src2lo, src2hi);
849         tcg_gen_or_i32(crf, crf, src2lo);
850     }
851     tcg_gen_shli_i32(crf, crf, CRF_GT);
852     tcg_temp_free_i32(src1);
853     tcg_temp_free_i32(src2);
854     tcg_temp_free_i32(src2lo);
855     tcg_temp_free_i32(src2hi);
856 }
857
858 /* isel (PowerPC 2.03 specification) */
859 static void gen_isel(DisasContext *ctx)
860 {
861     uint32_t bi = rC(ctx->opcode);
862     uint32_t mask = 0x08 >> (bi & 0x03);
863     TCGv t0 = tcg_temp_new();
864     TCGv zr;
865
866     tcg_gen_extu_i32_tl(t0, cpu_crf[bi >> 2]);
867     tcg_gen_andi_tl(t0, t0, mask);
868
869     zr = tcg_const_tl(0);
870     tcg_gen_movcond_tl(TCG_COND_NE, cpu_gpr[rD(ctx->opcode)], t0, zr,
871                        rA(ctx->opcode) ? cpu_gpr[rA(ctx->opcode)] : zr,
872                        cpu_gpr[rB(ctx->opcode)]);
873     tcg_temp_free(zr);
874     tcg_temp_free(t0);
875 }
876
877 /* cmpb: PowerPC 2.05 specification */
878 static void gen_cmpb(DisasContext *ctx)
879 {
880     gen_helper_cmpb(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)],
881                     cpu_gpr[rB(ctx->opcode)]);
882 }
883
884 /***                           Integer arithmetic                          ***/
885
886 static inline void gen_op_arith_compute_ov(DisasContext *ctx, TCGv arg0,
887                                            TCGv arg1, TCGv arg2, int sub)
888 {
889     TCGv t0 = tcg_temp_new();
890
891     tcg_gen_xor_tl(cpu_ov, arg0, arg2);
892     tcg_gen_xor_tl(t0, arg1, arg2);
893     if (sub) {
894         tcg_gen_and_tl(cpu_ov, cpu_ov, t0);
895     } else {
896         tcg_gen_andc_tl(cpu_ov, cpu_ov, t0);
897     }
898     tcg_temp_free(t0);
899     if (NARROW_MODE(ctx)) {
900         tcg_gen_ext32s_tl(cpu_ov, cpu_ov);
901     }
902     tcg_gen_shri_tl(cpu_ov, cpu_ov, TARGET_LONG_BITS - 1);
903     tcg_gen_or_tl(cpu_so, cpu_so, cpu_ov);
904 }
905
906 /* Common add function */
907 static inline void gen_op_arith_add(DisasContext *ctx, TCGv ret, TCGv arg1,
908                                     TCGv arg2, bool add_ca, bool compute_ca,
909                                     bool compute_ov, bool compute_rc0)
910 {
911     TCGv t0 = ret;
912
913     if (compute_ca || compute_ov) {
914         t0 = tcg_temp_new();
915     }
916
917     if (compute_ca) {
918         if (NARROW_MODE(ctx)) {
919             /* Caution: a non-obvious corner case of the spec is that we
920                must produce the *entire* 64-bit addition, but produce the
921                carry into bit 32.  */
922             TCGv t1 = tcg_temp_new();
923             tcg_gen_xor_tl(t1, arg1, arg2);        /* add without carry */
924             tcg_gen_add_tl(t0, arg1, arg2);
925             if (add_ca) {
926                 tcg_gen_add_tl(t0, t0, cpu_ca);
927             }
928             tcg_gen_xor_tl(cpu_ca, t0, t1);        /* bits changed w/ carry */
929             tcg_temp_free(t1);
930             tcg_gen_shri_tl(cpu_ca, cpu_ca, 32);   /* extract bit 32 */
931             tcg_gen_andi_tl(cpu_ca, cpu_ca, 1);
932         } else {
933             TCGv zero = tcg_const_tl(0);
934             if (add_ca) {
935                 tcg_gen_add2_tl(t0, cpu_ca, arg1, zero, cpu_ca, zero);
936                 tcg_gen_add2_tl(t0, cpu_ca, t0, cpu_ca, arg2, zero);
937             } else {
938                 tcg_gen_add2_tl(t0, cpu_ca, arg1, zero, arg2, zero);
939             }
940             tcg_temp_free(zero);
941         }
942     } else {
943         tcg_gen_add_tl(t0, arg1, arg2);
944         if (add_ca) {
945             tcg_gen_add_tl(t0, t0, cpu_ca);
946         }
947     }
948
949     if (compute_ov) {
950         gen_op_arith_compute_ov(ctx, t0, arg1, arg2, 0);
951     }
952     if (unlikely(compute_rc0)) {
953         gen_set_Rc0(ctx, t0);
954     }
955
956     if (!TCGV_EQUAL(t0, ret)) {
957         tcg_gen_mov_tl(ret, t0);
958         tcg_temp_free(t0);
959     }
960 }
961 /* Add functions with two operands */
962 #define GEN_INT_ARITH_ADD(name, opc3, add_ca, compute_ca, compute_ov)         \
963 static void glue(gen_, name)(DisasContext *ctx)                               \
964 {                                                                             \
965     gen_op_arith_add(ctx, cpu_gpr[rD(ctx->opcode)],                           \
966                      cpu_gpr[rA(ctx->opcode)], cpu_gpr[rB(ctx->opcode)],      \
967                      add_ca, compute_ca, compute_ov, Rc(ctx->opcode));        \
968 }
969 /* Add functions with one operand and one immediate */
970 #define GEN_INT_ARITH_ADD_CONST(name, opc3, const_val,                        \
971                                 add_ca, compute_ca, compute_ov)               \
972 static void glue(gen_, name)(DisasContext *ctx)                               \
973 {                                                                             \
974     TCGv t0 = tcg_const_tl(const_val);                                        \
975     gen_op_arith_add(ctx, cpu_gpr[rD(ctx->opcode)],                           \
976                      cpu_gpr[rA(ctx->opcode)], t0,                            \
977                      add_ca, compute_ca, compute_ov, Rc(ctx->opcode));        \
978     tcg_temp_free(t0);                                                        \
979 }
980
981 /* add  add.  addo  addo. */
982 GEN_INT_ARITH_ADD(add, 0x08, 0, 0, 0)
983 GEN_INT_ARITH_ADD(addo, 0x18, 0, 0, 1)
984 /* addc  addc.  addco  addco. */
985 GEN_INT_ARITH_ADD(addc, 0x00, 0, 1, 0)
986 GEN_INT_ARITH_ADD(addco, 0x10, 0, 1, 1)
987 /* adde  adde.  addeo  addeo. */
988 GEN_INT_ARITH_ADD(adde, 0x04, 1, 1, 0)
989 GEN_INT_ARITH_ADD(addeo, 0x14, 1, 1, 1)
990 /* addme  addme.  addmeo  addmeo.  */
991 GEN_INT_ARITH_ADD_CONST(addme, 0x07, -1LL, 1, 1, 0)
992 GEN_INT_ARITH_ADD_CONST(addmeo, 0x17, -1LL, 1, 1, 1)
993 /* addze  addze.  addzeo  addzeo.*/
994 GEN_INT_ARITH_ADD_CONST(addze, 0x06, 0, 1, 1, 0)
995 GEN_INT_ARITH_ADD_CONST(addzeo, 0x16, 0, 1, 1, 1)
996 /* addi */
997 static void gen_addi(DisasContext *ctx)
998 {
999     target_long simm = SIMM(ctx->opcode);
1000
1001     if (rA(ctx->opcode) == 0) {
1002         /* li case */
1003         tcg_gen_movi_tl(cpu_gpr[rD(ctx->opcode)], simm);
1004     } else {
1005         tcg_gen_addi_tl(cpu_gpr[rD(ctx->opcode)],
1006                         cpu_gpr[rA(ctx->opcode)], simm);
1007     }
1008 }
1009 /* addic  addic.*/
1010 static inline void gen_op_addic(DisasContext *ctx, bool compute_rc0)
1011 {
1012     TCGv c = tcg_const_tl(SIMM(ctx->opcode));
1013     gen_op_arith_add(ctx, cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)],
1014                      c, 0, 1, 0, compute_rc0);
1015     tcg_temp_free(c);
1016 }
1017
1018 static void gen_addic(DisasContext *ctx)
1019 {
1020     gen_op_addic(ctx, 0);
1021 }
1022
1023 static void gen_addic_(DisasContext *ctx)
1024 {
1025     gen_op_addic(ctx, 1);
1026 }
1027
1028 /* addis */
1029 static void gen_addis(DisasContext *ctx)
1030 {
1031     target_long simm = SIMM(ctx->opcode);
1032
1033     if (rA(ctx->opcode) == 0) {
1034         /* lis case */
1035         tcg_gen_movi_tl(cpu_gpr[rD(ctx->opcode)], simm << 16);
1036     } else {
1037         tcg_gen_addi_tl(cpu_gpr[rD(ctx->opcode)],
1038                         cpu_gpr[rA(ctx->opcode)], simm << 16);
1039     }
1040 }
1041
1042 /* addpcis */
1043 static void gen_addpcis(DisasContext *ctx)
1044 {
1045     target_long d = DX(ctx->opcode);
1046
1047     tcg_gen_movi_tl(cpu_gpr[rD(ctx->opcode)], ctx->nip + (d << 16));
1048 }
1049
1050 static inline void gen_op_arith_divw(DisasContext *ctx, TCGv ret, TCGv arg1,
1051                                      TCGv arg2, int sign, int compute_ov)
1052 {
1053     TCGLabel *l1 = gen_new_label();
1054     TCGLabel *l2 = gen_new_label();
1055     TCGv_i32 t0 = tcg_temp_local_new_i32();
1056     TCGv_i32 t1 = tcg_temp_local_new_i32();
1057
1058     tcg_gen_trunc_tl_i32(t0, arg1);
1059     tcg_gen_trunc_tl_i32(t1, arg2);
1060     tcg_gen_brcondi_i32(TCG_COND_EQ, t1, 0, l1);
1061     if (sign) {
1062         TCGLabel *l3 = gen_new_label();
1063         tcg_gen_brcondi_i32(TCG_COND_NE, t1, -1, l3);
1064         tcg_gen_brcondi_i32(TCG_COND_EQ, t0, INT32_MIN, l1);
1065         gen_set_label(l3);
1066         tcg_gen_div_i32(t0, t0, t1);
1067     } else {
1068         tcg_gen_divu_i32(t0, t0, t1);
1069     }
1070     if (compute_ov) {
1071         tcg_gen_movi_tl(cpu_ov, 0);
1072     }
1073     tcg_gen_br(l2);
1074     gen_set_label(l1);
1075     if (sign) {
1076         tcg_gen_sari_i32(t0, t0, 31);
1077     } else {
1078         tcg_gen_movi_i32(t0, 0);
1079     }
1080     if (compute_ov) {
1081         tcg_gen_movi_tl(cpu_ov, 1);
1082         tcg_gen_movi_tl(cpu_so, 1);
1083     }
1084     gen_set_label(l2);
1085     tcg_gen_extu_i32_tl(ret, t0);
1086     tcg_temp_free_i32(t0);
1087     tcg_temp_free_i32(t1);
1088     if (unlikely(Rc(ctx->opcode) != 0))
1089         gen_set_Rc0(ctx, ret);
1090 }
1091 /* Div functions */
1092 #define GEN_INT_ARITH_DIVW(name, opc3, sign, compute_ov)                      \
1093 static void glue(gen_, name)(DisasContext *ctx)                                       \
1094 {                                                                             \
1095     gen_op_arith_divw(ctx, cpu_gpr[rD(ctx->opcode)],                          \
1096                      cpu_gpr[rA(ctx->opcode)], cpu_gpr[rB(ctx->opcode)],      \
1097                      sign, compute_ov);                                       \
1098 }
1099 /* divwu  divwu.  divwuo  divwuo.   */
1100 GEN_INT_ARITH_DIVW(divwu, 0x0E, 0, 0);
1101 GEN_INT_ARITH_DIVW(divwuo, 0x1E, 0, 1);
1102 /* divw  divw.  divwo  divwo.   */
1103 GEN_INT_ARITH_DIVW(divw, 0x0F, 1, 0);
1104 GEN_INT_ARITH_DIVW(divwo, 0x1F, 1, 1);
1105
1106 /* div[wd]eu[o][.] */
1107 #define GEN_DIVE(name, hlpr, compute_ov)                                      \
1108 static void gen_##name(DisasContext *ctx)                                     \
1109 {                                                                             \
1110     TCGv_i32 t0 = tcg_const_i32(compute_ov);                                  \
1111     gen_helper_##hlpr(cpu_gpr[rD(ctx->opcode)], cpu_env,                      \
1112                      cpu_gpr[rA(ctx->opcode)], cpu_gpr[rB(ctx->opcode)], t0); \
1113     tcg_temp_free_i32(t0);                                                    \
1114     if (unlikely(Rc(ctx->opcode) != 0)) {                                     \
1115         gen_set_Rc0(ctx, cpu_gpr[rD(ctx->opcode)]);                           \
1116     }                                                                         \
1117 }
1118
1119 GEN_DIVE(divweu, divweu, 0);
1120 GEN_DIVE(divweuo, divweu, 1);
1121 GEN_DIVE(divwe, divwe, 0);
1122 GEN_DIVE(divweo, divwe, 1);
1123
1124 #if defined(TARGET_PPC64)
1125 static inline void gen_op_arith_divd(DisasContext *ctx, TCGv ret, TCGv arg1,
1126                                      TCGv arg2, int sign, int compute_ov)
1127 {
1128     TCGLabel *l1 = gen_new_label();
1129     TCGLabel *l2 = gen_new_label();
1130
1131     tcg_gen_brcondi_i64(TCG_COND_EQ, arg2, 0, l1);
1132     if (sign) {
1133         TCGLabel *l3 = gen_new_label();
1134         tcg_gen_brcondi_i64(TCG_COND_NE, arg2, -1, l3);
1135         tcg_gen_brcondi_i64(TCG_COND_EQ, arg1, INT64_MIN, l1);
1136         gen_set_label(l3);
1137         tcg_gen_div_i64(ret, arg1, arg2);
1138     } else {
1139         tcg_gen_divu_i64(ret, arg1, arg2);
1140     }
1141     if (compute_ov) {
1142         tcg_gen_movi_tl(cpu_ov, 0);
1143     }
1144     tcg_gen_br(l2);
1145     gen_set_label(l1);
1146     if (sign) {
1147         tcg_gen_sari_i64(ret, arg1, 63);
1148     } else {
1149         tcg_gen_movi_i64(ret, 0);
1150     }
1151     if (compute_ov) {
1152         tcg_gen_movi_tl(cpu_ov, 1);
1153         tcg_gen_movi_tl(cpu_so, 1);
1154     }
1155     gen_set_label(l2);
1156     if (unlikely(Rc(ctx->opcode) != 0))
1157         gen_set_Rc0(ctx, ret);
1158 }
1159 #define GEN_INT_ARITH_DIVD(name, opc3, sign, compute_ov)                      \
1160 static void glue(gen_, name)(DisasContext *ctx)                                       \
1161 {                                                                             \
1162     gen_op_arith_divd(ctx, cpu_gpr[rD(ctx->opcode)],                          \
1163                       cpu_gpr[rA(ctx->opcode)], cpu_gpr[rB(ctx->opcode)],     \
1164                       sign, compute_ov);                                      \
1165 }
1166 /* divwu  divwu.  divwuo  divwuo.   */
1167 GEN_INT_ARITH_DIVD(divdu, 0x0E, 0, 0);
1168 GEN_INT_ARITH_DIVD(divduo, 0x1E, 0, 1);
1169 /* divw  divw.  divwo  divwo.   */
1170 GEN_INT_ARITH_DIVD(divd, 0x0F, 1, 0);
1171 GEN_INT_ARITH_DIVD(divdo, 0x1F, 1, 1);
1172
1173 GEN_DIVE(divdeu, divdeu, 0);
1174 GEN_DIVE(divdeuo, divdeu, 1);
1175 GEN_DIVE(divde, divde, 0);
1176 GEN_DIVE(divdeo, divde, 1);
1177 #endif
1178
1179 static inline void gen_op_arith_modw(DisasContext *ctx, TCGv ret, TCGv arg1,
1180                                      TCGv arg2, int sign)
1181 {
1182     TCGv_i32 t0 = tcg_temp_new_i32();
1183     TCGv_i32 t1 = tcg_temp_new_i32();
1184
1185     tcg_gen_trunc_tl_i32(t0, arg1);
1186     tcg_gen_trunc_tl_i32(t1, arg2);
1187     if (sign) {
1188         TCGv_i32 t2 = tcg_temp_new_i32();
1189         TCGv_i32 t3 = tcg_temp_new_i32();
1190         tcg_gen_setcondi_i32(TCG_COND_EQ, t2, t0, INT_MIN);
1191         tcg_gen_setcondi_i32(TCG_COND_EQ, t3, t1, -1);
1192         tcg_gen_and_i32(t2, t2, t3);
1193         tcg_gen_setcondi_i32(TCG_COND_EQ, t3, t1, 0);
1194         tcg_gen_or_i32(t2, t2, t3);
1195         tcg_gen_movi_i32(t3, 0);
1196         tcg_gen_movcond_i32(TCG_COND_NE, t1, t2, t3, t2, t1);
1197         tcg_gen_rem_i32(t3, t0, t1);
1198         tcg_gen_ext_i32_tl(ret, t3);
1199         tcg_temp_free_i32(t2);
1200         tcg_temp_free_i32(t3);
1201     } else {
1202         TCGv_i32 t2 = tcg_const_i32(1);
1203         TCGv_i32 t3 = tcg_const_i32(0);
1204         tcg_gen_movcond_i32(TCG_COND_EQ, t1, t1, t3, t2, t1);
1205         tcg_gen_remu_i32(t3, t0, t1);
1206         tcg_gen_extu_i32_tl(ret, t3);
1207         tcg_temp_free_i32(t2);
1208         tcg_temp_free_i32(t3);
1209     }
1210     tcg_temp_free_i32(t0);
1211     tcg_temp_free_i32(t1);
1212 }
1213
1214 #define GEN_INT_ARITH_MODW(name, opc3, sign)                                \
1215 static void glue(gen_, name)(DisasContext *ctx)                             \
1216 {                                                                           \
1217     gen_op_arith_modw(ctx, cpu_gpr[rD(ctx->opcode)],                        \
1218                       cpu_gpr[rA(ctx->opcode)], cpu_gpr[rB(ctx->opcode)],   \
1219                       sign);                                                \
1220 }
1221
1222 GEN_INT_ARITH_MODW(moduw, 0x08, 0);
1223 GEN_INT_ARITH_MODW(modsw, 0x18, 1);
1224
1225 /* mulhw  mulhw. */
1226 static void gen_mulhw(DisasContext *ctx)
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, cpu_gpr[rA(ctx->opcode)]);
1232     tcg_gen_trunc_tl_i32(t1, cpu_gpr[rB(ctx->opcode)]);
1233     tcg_gen_muls2_i32(t0, t1, t0, t1);
1234     tcg_gen_extu_i32_tl(cpu_gpr[rD(ctx->opcode)], t1);
1235     tcg_temp_free_i32(t0);
1236     tcg_temp_free_i32(t1);
1237     if (unlikely(Rc(ctx->opcode) != 0))
1238         gen_set_Rc0(ctx, cpu_gpr[rD(ctx->opcode)]);
1239 }
1240
1241 /* mulhwu  mulhwu.  */
1242 static void gen_mulhwu(DisasContext *ctx)
1243 {
1244     TCGv_i32 t0 = tcg_temp_new_i32();
1245     TCGv_i32 t1 = tcg_temp_new_i32();
1246
1247     tcg_gen_trunc_tl_i32(t0, cpu_gpr[rA(ctx->opcode)]);
1248     tcg_gen_trunc_tl_i32(t1, cpu_gpr[rB(ctx->opcode)]);
1249     tcg_gen_mulu2_i32(t0, t1, t0, t1);
1250     tcg_gen_extu_i32_tl(cpu_gpr[rD(ctx->opcode)], t1);
1251     tcg_temp_free_i32(t0);
1252     tcg_temp_free_i32(t1);
1253     if (unlikely(Rc(ctx->opcode) != 0))
1254         gen_set_Rc0(ctx, cpu_gpr[rD(ctx->opcode)]);
1255 }
1256
1257 /* mullw  mullw. */
1258 static void gen_mullw(DisasContext *ctx)
1259 {
1260 #if defined(TARGET_PPC64)
1261     TCGv_i64 t0, t1;
1262     t0 = tcg_temp_new_i64();
1263     t1 = tcg_temp_new_i64();
1264     tcg_gen_ext32s_tl(t0, cpu_gpr[rA(ctx->opcode)]);
1265     tcg_gen_ext32s_tl(t1, cpu_gpr[rB(ctx->opcode)]);
1266     tcg_gen_mul_i64(cpu_gpr[rD(ctx->opcode)], t0, t1);
1267     tcg_temp_free(t0);
1268     tcg_temp_free(t1);
1269 #else
1270     tcg_gen_mul_i32(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)],
1271                     cpu_gpr[rB(ctx->opcode)]);
1272 #endif
1273     if (unlikely(Rc(ctx->opcode) != 0))
1274         gen_set_Rc0(ctx, cpu_gpr[rD(ctx->opcode)]);
1275 }
1276
1277 /* mullwo  mullwo. */
1278 static void gen_mullwo(DisasContext *ctx)
1279 {
1280     TCGv_i32 t0 = tcg_temp_new_i32();
1281     TCGv_i32 t1 = tcg_temp_new_i32();
1282
1283     tcg_gen_trunc_tl_i32(t0, cpu_gpr[rA(ctx->opcode)]);
1284     tcg_gen_trunc_tl_i32(t1, cpu_gpr[rB(ctx->opcode)]);
1285     tcg_gen_muls2_i32(t0, t1, t0, t1);
1286 #if defined(TARGET_PPC64)
1287     tcg_gen_concat_i32_i64(cpu_gpr[rD(ctx->opcode)], t0, t1);
1288 #else
1289     tcg_gen_mov_i32(cpu_gpr[rD(ctx->opcode)], t0);
1290 #endif
1291
1292     tcg_gen_sari_i32(t0, t0, 31);
1293     tcg_gen_setcond_i32(TCG_COND_NE, t0, t0, t1);
1294     tcg_gen_extu_i32_tl(cpu_ov, t0);
1295     tcg_gen_or_tl(cpu_so, cpu_so, cpu_ov);
1296
1297     tcg_temp_free_i32(t0);
1298     tcg_temp_free_i32(t1);
1299     if (unlikely(Rc(ctx->opcode) != 0))
1300         gen_set_Rc0(ctx, cpu_gpr[rD(ctx->opcode)]);
1301 }
1302
1303 /* mulli */
1304 static void gen_mulli(DisasContext *ctx)
1305 {
1306     tcg_gen_muli_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)],
1307                     SIMM(ctx->opcode));
1308 }
1309
1310 #if defined(TARGET_PPC64)
1311 /* mulhd  mulhd. */
1312 static void gen_mulhd(DisasContext *ctx)
1313 {
1314     TCGv lo = tcg_temp_new();
1315     tcg_gen_muls2_tl(lo, cpu_gpr[rD(ctx->opcode)],
1316                      cpu_gpr[rA(ctx->opcode)], cpu_gpr[rB(ctx->opcode)]);
1317     tcg_temp_free(lo);
1318     if (unlikely(Rc(ctx->opcode) != 0)) {
1319         gen_set_Rc0(ctx, cpu_gpr[rD(ctx->opcode)]);
1320     }
1321 }
1322
1323 /* mulhdu  mulhdu. */
1324 static void gen_mulhdu(DisasContext *ctx)
1325 {
1326     TCGv lo = tcg_temp_new();
1327     tcg_gen_mulu2_tl(lo, cpu_gpr[rD(ctx->opcode)],
1328                      cpu_gpr[rA(ctx->opcode)], cpu_gpr[rB(ctx->opcode)]);
1329     tcg_temp_free(lo);
1330     if (unlikely(Rc(ctx->opcode) != 0)) {
1331         gen_set_Rc0(ctx, cpu_gpr[rD(ctx->opcode)]);
1332     }
1333 }
1334
1335 /* mulld  mulld. */
1336 static void gen_mulld(DisasContext *ctx)
1337 {
1338     tcg_gen_mul_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)],
1339                    cpu_gpr[rB(ctx->opcode)]);
1340     if (unlikely(Rc(ctx->opcode) != 0))
1341         gen_set_Rc0(ctx, cpu_gpr[rD(ctx->opcode)]);
1342 }
1343
1344 /* mulldo  mulldo. */
1345 static void gen_mulldo(DisasContext *ctx)
1346 {
1347     TCGv_i64 t0 = tcg_temp_new_i64();
1348     TCGv_i64 t1 = tcg_temp_new_i64();
1349
1350     tcg_gen_muls2_i64(t0, t1, cpu_gpr[rA(ctx->opcode)],
1351                       cpu_gpr[rB(ctx->opcode)]);
1352     tcg_gen_mov_i64(cpu_gpr[rD(ctx->opcode)], t0);
1353
1354     tcg_gen_sari_i64(t0, t0, 63);
1355     tcg_gen_setcond_i64(TCG_COND_NE, cpu_ov, t0, t1);
1356     tcg_gen_or_tl(cpu_so, cpu_so, cpu_ov);
1357
1358     tcg_temp_free_i64(t0);
1359     tcg_temp_free_i64(t1);
1360
1361     if (unlikely(Rc(ctx->opcode) != 0)) {
1362         gen_set_Rc0(ctx, cpu_gpr[rD(ctx->opcode)]);
1363     }
1364 }
1365 #endif
1366
1367 /* Common subf function */
1368 static inline void gen_op_arith_subf(DisasContext *ctx, TCGv ret, TCGv arg1,
1369                                      TCGv arg2, bool add_ca, bool compute_ca,
1370                                      bool compute_ov, bool compute_rc0)
1371 {
1372     TCGv t0 = ret;
1373
1374     if (compute_ca || compute_ov) {
1375         t0 = tcg_temp_new();
1376     }
1377
1378     if (compute_ca) {
1379         /* dest = ~arg1 + arg2 [+ ca].  */
1380         if (NARROW_MODE(ctx)) {
1381             /* Caution: a non-obvious corner case of the spec is that we
1382                must produce the *entire* 64-bit addition, but produce the
1383                carry into bit 32.  */
1384             TCGv inv1 = tcg_temp_new();
1385             TCGv t1 = tcg_temp_new();
1386             tcg_gen_not_tl(inv1, arg1);
1387             if (add_ca) {
1388                 tcg_gen_add_tl(t0, arg2, cpu_ca);
1389             } else {
1390                 tcg_gen_addi_tl(t0, arg2, 1);
1391             }
1392             tcg_gen_xor_tl(t1, arg2, inv1);         /* add without carry */
1393             tcg_gen_add_tl(t0, t0, inv1);
1394             tcg_temp_free(inv1);
1395             tcg_gen_xor_tl(cpu_ca, t0, t1);         /* bits changes w/ carry */
1396             tcg_temp_free(t1);
1397             tcg_gen_shri_tl(cpu_ca, cpu_ca, 32);    /* extract bit 32 */
1398             tcg_gen_andi_tl(cpu_ca, cpu_ca, 1);
1399         } else if (add_ca) {
1400             TCGv zero, inv1 = tcg_temp_new();
1401             tcg_gen_not_tl(inv1, arg1);
1402             zero = tcg_const_tl(0);
1403             tcg_gen_add2_tl(t0, cpu_ca, arg2, zero, cpu_ca, zero);
1404             tcg_gen_add2_tl(t0, cpu_ca, t0, cpu_ca, inv1, zero);
1405             tcg_temp_free(zero);
1406             tcg_temp_free(inv1);
1407         } else {
1408             tcg_gen_setcond_tl(TCG_COND_GEU, cpu_ca, arg2, arg1);
1409             tcg_gen_sub_tl(t0, arg2, arg1);
1410         }
1411     } else if (add_ca) {
1412         /* Since we're ignoring carry-out, we can simplify the
1413            standard ~arg1 + arg2 + ca to arg2 - arg1 + ca - 1.  */
1414         tcg_gen_sub_tl(t0, arg2, arg1);
1415         tcg_gen_add_tl(t0, t0, cpu_ca);
1416         tcg_gen_subi_tl(t0, t0, 1);
1417     } else {
1418         tcg_gen_sub_tl(t0, arg2, arg1);
1419     }
1420
1421     if (compute_ov) {
1422         gen_op_arith_compute_ov(ctx, t0, arg1, arg2, 1);
1423     }
1424     if (unlikely(compute_rc0)) {
1425         gen_set_Rc0(ctx, t0);
1426     }
1427
1428     if (!TCGV_EQUAL(t0, ret)) {
1429         tcg_gen_mov_tl(ret, t0);
1430         tcg_temp_free(t0);
1431     }
1432 }
1433 /* Sub functions with Two operands functions */
1434 #define GEN_INT_ARITH_SUBF(name, opc3, add_ca, compute_ca, compute_ov)        \
1435 static void glue(gen_, name)(DisasContext *ctx)                               \
1436 {                                                                             \
1437     gen_op_arith_subf(ctx, cpu_gpr[rD(ctx->opcode)],                          \
1438                       cpu_gpr[rA(ctx->opcode)], cpu_gpr[rB(ctx->opcode)],     \
1439                       add_ca, compute_ca, compute_ov, Rc(ctx->opcode));       \
1440 }
1441 /* Sub functions with one operand and one immediate */
1442 #define GEN_INT_ARITH_SUBF_CONST(name, opc3, const_val,                       \
1443                                 add_ca, compute_ca, compute_ov)               \
1444 static void glue(gen_, name)(DisasContext *ctx)                               \
1445 {                                                                             \
1446     TCGv t0 = tcg_const_tl(const_val);                                        \
1447     gen_op_arith_subf(ctx, cpu_gpr[rD(ctx->opcode)],                          \
1448                       cpu_gpr[rA(ctx->opcode)], t0,                           \
1449                       add_ca, compute_ca, compute_ov, Rc(ctx->opcode));       \
1450     tcg_temp_free(t0);                                                        \
1451 }
1452 /* subf  subf.  subfo  subfo. */
1453 GEN_INT_ARITH_SUBF(subf, 0x01, 0, 0, 0)
1454 GEN_INT_ARITH_SUBF(subfo, 0x11, 0, 0, 1)
1455 /* subfc  subfc.  subfco  subfco. */
1456 GEN_INT_ARITH_SUBF(subfc, 0x00, 0, 1, 0)
1457 GEN_INT_ARITH_SUBF(subfco, 0x10, 0, 1, 1)
1458 /* subfe  subfe.  subfeo  subfo. */
1459 GEN_INT_ARITH_SUBF(subfe, 0x04, 1, 1, 0)
1460 GEN_INT_ARITH_SUBF(subfeo, 0x14, 1, 1, 1)
1461 /* subfme  subfme.  subfmeo  subfmeo.  */
1462 GEN_INT_ARITH_SUBF_CONST(subfme, 0x07, -1LL, 1, 1, 0)
1463 GEN_INT_ARITH_SUBF_CONST(subfmeo, 0x17, -1LL, 1, 1, 1)
1464 /* subfze  subfze.  subfzeo  subfzeo.*/
1465 GEN_INT_ARITH_SUBF_CONST(subfze, 0x06, 0, 1, 1, 0)
1466 GEN_INT_ARITH_SUBF_CONST(subfzeo, 0x16, 0, 1, 1, 1)
1467
1468 /* subfic */
1469 static void gen_subfic(DisasContext *ctx)
1470 {
1471     TCGv c = tcg_const_tl(SIMM(ctx->opcode));
1472     gen_op_arith_subf(ctx, cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)],
1473                       c, 0, 1, 0, 0);
1474     tcg_temp_free(c);
1475 }
1476
1477 /* neg neg. nego nego. */
1478 static inline void gen_op_arith_neg(DisasContext *ctx, bool compute_ov)
1479 {
1480     TCGv zero = tcg_const_tl(0);
1481     gen_op_arith_subf(ctx, cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)],
1482                       zero, 0, 0, compute_ov, Rc(ctx->opcode));
1483     tcg_temp_free(zero);
1484 }
1485
1486 static void gen_neg(DisasContext *ctx)
1487 {
1488     gen_op_arith_neg(ctx, 0);
1489 }
1490
1491 static void gen_nego(DisasContext *ctx)
1492 {
1493     gen_op_arith_neg(ctx, 1);
1494 }
1495
1496 /***                            Integer logical                            ***/
1497 #define GEN_LOGICAL2(name, tcg_op, opc, type)                                 \
1498 static void glue(gen_, name)(DisasContext *ctx)                                       \
1499 {                                                                             \
1500     tcg_op(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)],                \
1501        cpu_gpr[rB(ctx->opcode)]);                                             \
1502     if (unlikely(Rc(ctx->opcode) != 0))                                       \
1503         gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);                           \
1504 }
1505
1506 #define GEN_LOGICAL1(name, tcg_op, opc, type)                                 \
1507 static void glue(gen_, name)(DisasContext *ctx)                                       \
1508 {                                                                             \
1509     tcg_op(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)]);               \
1510     if (unlikely(Rc(ctx->opcode) != 0))                                       \
1511         gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);                           \
1512 }
1513
1514 /* and & and. */
1515 GEN_LOGICAL2(and, tcg_gen_and_tl, 0x00, PPC_INTEGER);
1516 /* andc & andc. */
1517 GEN_LOGICAL2(andc, tcg_gen_andc_tl, 0x01, PPC_INTEGER);
1518
1519 /* andi. */
1520 static void gen_andi_(DisasContext *ctx)
1521 {
1522     tcg_gen_andi_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)], UIMM(ctx->opcode));
1523     gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
1524 }
1525
1526 /* andis. */
1527 static void gen_andis_(DisasContext *ctx)
1528 {
1529     tcg_gen_andi_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)], UIMM(ctx->opcode) << 16);
1530     gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
1531 }
1532
1533 /* cntlzw */
1534 static void gen_cntlzw(DisasContext *ctx)
1535 {
1536     gen_helper_cntlzw(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)]);
1537     if (unlikely(Rc(ctx->opcode) != 0))
1538         gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
1539 }
1540 /* eqv & eqv. */
1541 GEN_LOGICAL2(eqv, tcg_gen_eqv_tl, 0x08, PPC_INTEGER);
1542 /* extsb & extsb. */
1543 GEN_LOGICAL1(extsb, tcg_gen_ext8s_tl, 0x1D, PPC_INTEGER);
1544 /* extsh & extsh. */
1545 GEN_LOGICAL1(extsh, tcg_gen_ext16s_tl, 0x1C, PPC_INTEGER);
1546 /* nand & nand. */
1547 GEN_LOGICAL2(nand, tcg_gen_nand_tl, 0x0E, PPC_INTEGER);
1548 /* nor & nor. */
1549 GEN_LOGICAL2(nor, tcg_gen_nor_tl, 0x03, PPC_INTEGER);
1550
1551 #if defined(TARGET_PPC64) && !defined(CONFIG_USER_ONLY)
1552 static void gen_pause(DisasContext *ctx)
1553 {
1554     TCGv_i32 t0 = tcg_const_i32(0);
1555     tcg_gen_st_i32(t0, cpu_env,
1556                    -offsetof(PowerPCCPU, env) + offsetof(CPUState, halted));
1557     tcg_temp_free_i32(t0);
1558
1559     /* Stop translation, this gives other CPUs a chance to run */
1560     gen_exception_err(ctx, EXCP_HLT, 1);
1561 }
1562 #endif /* defined(TARGET_PPC64) */
1563
1564 /* or & or. */
1565 static void gen_or(DisasContext *ctx)
1566 {
1567     int rs, ra, rb;
1568
1569     rs = rS(ctx->opcode);
1570     ra = rA(ctx->opcode);
1571     rb = rB(ctx->opcode);
1572     /* Optimisation for mr. ri case */
1573     if (rs != ra || rs != rb) {
1574         if (rs != rb)
1575             tcg_gen_or_tl(cpu_gpr[ra], cpu_gpr[rs], cpu_gpr[rb]);
1576         else
1577             tcg_gen_mov_tl(cpu_gpr[ra], cpu_gpr[rs]);
1578         if (unlikely(Rc(ctx->opcode) != 0))
1579             gen_set_Rc0(ctx, cpu_gpr[ra]);
1580     } else if (unlikely(Rc(ctx->opcode) != 0)) {
1581         gen_set_Rc0(ctx, cpu_gpr[rs]);
1582 #if defined(TARGET_PPC64)
1583     } else if (rs != 0) { /* 0 is nop */
1584         int prio = 0;
1585
1586         switch (rs) {
1587         case 1:
1588             /* Set process priority to low */
1589             prio = 2;
1590             break;
1591         case 6:
1592             /* Set process priority to medium-low */
1593             prio = 3;
1594             break;
1595         case 2:
1596             /* Set process priority to normal */
1597             prio = 4;
1598             break;
1599 #if !defined(CONFIG_USER_ONLY)
1600         case 31:
1601             if (!ctx->pr) {
1602                 /* Set process priority to very low */
1603                 prio = 1;
1604             }
1605             break;
1606         case 5:
1607             if (!ctx->pr) {
1608                 /* Set process priority to medium-hight */
1609                 prio = 5;
1610             }
1611             break;
1612         case 3:
1613             if (!ctx->pr) {
1614                 /* Set process priority to high */
1615                 prio = 6;
1616             }
1617             break;
1618         case 7:
1619             if (ctx->hv && !ctx->pr) {
1620                 /* Set process priority to very high */
1621                 prio = 7;
1622             }
1623             break;
1624 #endif
1625         default:
1626             break;
1627         }
1628         if (prio) {
1629             TCGv t0 = tcg_temp_new();
1630             gen_load_spr(t0, SPR_PPR);
1631             tcg_gen_andi_tl(t0, t0, ~0x001C000000000000ULL);
1632             tcg_gen_ori_tl(t0, t0, ((uint64_t)prio) << 50);
1633             gen_store_spr(SPR_PPR, t0);
1634             tcg_temp_free(t0);
1635         }
1636 #if !defined(CONFIG_USER_ONLY)
1637         /* Pause out of TCG otherwise spin loops with smt_low eat too much
1638          * CPU and the kernel hangs.  This applies to all encodings other
1639          * than no-op, e.g., miso(rs=26), yield(27), mdoio(29), mdoom(30),
1640          * and all currently undefined.
1641          */
1642         gen_pause(ctx);
1643 #endif
1644 #endif
1645     }
1646 }
1647 /* orc & orc. */
1648 GEN_LOGICAL2(orc, tcg_gen_orc_tl, 0x0C, PPC_INTEGER);
1649
1650 /* xor & xor. */
1651 static void gen_xor(DisasContext *ctx)
1652 {
1653     /* Optimisation for "set to zero" case */
1654     if (rS(ctx->opcode) != rB(ctx->opcode))
1655         tcg_gen_xor_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)], cpu_gpr[rB(ctx->opcode)]);
1656     else
1657         tcg_gen_movi_tl(cpu_gpr[rA(ctx->opcode)], 0);
1658     if (unlikely(Rc(ctx->opcode) != 0))
1659         gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
1660 }
1661
1662 /* ori */
1663 static void gen_ori(DisasContext *ctx)
1664 {
1665     target_ulong uimm = UIMM(ctx->opcode);
1666
1667     if (rS(ctx->opcode) == rA(ctx->opcode) && uimm == 0) {
1668         return;
1669     }
1670     tcg_gen_ori_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)], uimm);
1671 }
1672
1673 /* oris */
1674 static void gen_oris(DisasContext *ctx)
1675 {
1676     target_ulong uimm = UIMM(ctx->opcode);
1677
1678     if (rS(ctx->opcode) == rA(ctx->opcode) && uimm == 0) {
1679         /* NOP */
1680         return;
1681     }
1682     tcg_gen_ori_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)], uimm << 16);
1683 }
1684
1685 /* xori */
1686 static void gen_xori(DisasContext *ctx)
1687 {
1688     target_ulong uimm = UIMM(ctx->opcode);
1689
1690     if (rS(ctx->opcode) == rA(ctx->opcode) && uimm == 0) {
1691         /* NOP */
1692         return;
1693     }
1694     tcg_gen_xori_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)], uimm);
1695 }
1696
1697 /* xoris */
1698 static void gen_xoris(DisasContext *ctx)
1699 {
1700     target_ulong uimm = UIMM(ctx->opcode);
1701
1702     if (rS(ctx->opcode) == rA(ctx->opcode) && uimm == 0) {
1703         /* NOP */
1704         return;
1705     }
1706     tcg_gen_xori_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)], uimm << 16);
1707 }
1708
1709 /* popcntb : PowerPC 2.03 specification */
1710 static void gen_popcntb(DisasContext *ctx)
1711 {
1712     gen_helper_popcntb(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)]);
1713 }
1714
1715 static void gen_popcntw(DisasContext *ctx)
1716 {
1717     gen_helper_popcntw(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)]);
1718 }
1719
1720 #if defined(TARGET_PPC64)
1721 /* popcntd: PowerPC 2.06 specification */
1722 static void gen_popcntd(DisasContext *ctx)
1723 {
1724     gen_helper_popcntd(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)]);
1725 }
1726 #endif
1727
1728 /* prtyw: PowerPC 2.05 specification */
1729 static void gen_prtyw(DisasContext *ctx)
1730 {
1731     TCGv ra = cpu_gpr[rA(ctx->opcode)];
1732     TCGv rs = cpu_gpr[rS(ctx->opcode)];
1733     TCGv t0 = tcg_temp_new();
1734     tcg_gen_shri_tl(t0, rs, 16);
1735     tcg_gen_xor_tl(ra, rs, t0);
1736     tcg_gen_shri_tl(t0, ra, 8);
1737     tcg_gen_xor_tl(ra, ra, t0);
1738     tcg_gen_andi_tl(ra, ra, (target_ulong)0x100000001ULL);
1739     tcg_temp_free(t0);
1740 }
1741
1742 #if defined(TARGET_PPC64)
1743 /* prtyd: PowerPC 2.05 specification */
1744 static void gen_prtyd(DisasContext *ctx)
1745 {
1746     TCGv ra = cpu_gpr[rA(ctx->opcode)];
1747     TCGv rs = cpu_gpr[rS(ctx->opcode)];
1748     TCGv t0 = tcg_temp_new();
1749     tcg_gen_shri_tl(t0, rs, 32);
1750     tcg_gen_xor_tl(ra, rs, t0);
1751     tcg_gen_shri_tl(t0, ra, 16);
1752     tcg_gen_xor_tl(ra, ra, t0);
1753     tcg_gen_shri_tl(t0, ra, 8);
1754     tcg_gen_xor_tl(ra, ra, t0);
1755     tcg_gen_andi_tl(ra, ra, 1);
1756     tcg_temp_free(t0);
1757 }
1758 #endif
1759
1760 #if defined(TARGET_PPC64)
1761 /* bpermd */
1762 static void gen_bpermd(DisasContext *ctx)
1763 {
1764     gen_helper_bpermd(cpu_gpr[rA(ctx->opcode)],
1765                       cpu_gpr[rS(ctx->opcode)], cpu_gpr[rB(ctx->opcode)]);
1766 }
1767 #endif
1768
1769 #if defined(TARGET_PPC64)
1770 /* extsw & extsw. */
1771 GEN_LOGICAL1(extsw, tcg_gen_ext32s_tl, 0x1E, PPC_64B);
1772
1773 /* cntlzd */
1774 static void gen_cntlzd(DisasContext *ctx)
1775 {
1776     gen_helper_cntlzd(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)]);
1777     if (unlikely(Rc(ctx->opcode) != 0))
1778         gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
1779 }
1780 #endif
1781
1782 /***                             Integer rotate                            ***/
1783
1784 /* rlwimi & rlwimi. */
1785 static void gen_rlwimi(DisasContext *ctx)
1786 {
1787     TCGv t_ra = cpu_gpr[rA(ctx->opcode)];
1788     TCGv t_rs = cpu_gpr[rS(ctx->opcode)];
1789     uint32_t sh = SH(ctx->opcode);
1790     uint32_t mb = MB(ctx->opcode);
1791     uint32_t me = ME(ctx->opcode);
1792
1793     if (sh == (31-me) && mb <= me) {
1794         tcg_gen_deposit_tl(t_ra, t_ra, t_rs, sh, me - mb + 1);
1795     } else {
1796         target_ulong mask;
1797         TCGv t1;
1798
1799 #if defined(TARGET_PPC64)
1800         mb += 32;
1801         me += 32;
1802 #endif
1803         mask = MASK(mb, me);
1804
1805         t1 = tcg_temp_new();
1806         if (mask <= 0xffffffffu) {
1807             TCGv_i32 t0 = tcg_temp_new_i32();
1808             tcg_gen_trunc_tl_i32(t0, t_rs);
1809             tcg_gen_rotli_i32(t0, t0, sh);
1810             tcg_gen_extu_i32_tl(t1, t0);
1811             tcg_temp_free_i32(t0);
1812         } else {
1813 #if defined(TARGET_PPC64)
1814             tcg_gen_deposit_i64(t1, t_rs, t_rs, 32, 32);
1815             tcg_gen_rotli_i64(t1, t1, sh);
1816 #else
1817             g_assert_not_reached();
1818 #endif
1819         }
1820
1821         tcg_gen_andi_tl(t1, t1, mask);
1822         tcg_gen_andi_tl(t_ra, t_ra, ~mask);
1823         tcg_gen_or_tl(t_ra, t_ra, t1);
1824         tcg_temp_free(t1);
1825     }
1826     if (unlikely(Rc(ctx->opcode) != 0)) {
1827         gen_set_Rc0(ctx, t_ra);
1828     }
1829 }
1830
1831 /* rlwinm & rlwinm. */
1832 static void gen_rlwinm(DisasContext *ctx)
1833 {
1834     TCGv t_ra = cpu_gpr[rA(ctx->opcode)];
1835     TCGv t_rs = cpu_gpr[rS(ctx->opcode)];
1836     uint32_t sh = SH(ctx->opcode);
1837     uint32_t mb = MB(ctx->opcode);
1838     uint32_t me = ME(ctx->opcode);
1839
1840     if (mb == 0 && me == (31 - sh)) {
1841         tcg_gen_shli_tl(t_ra, t_rs, sh);
1842         tcg_gen_ext32u_tl(t_ra, t_ra);
1843     } else if (sh != 0 && me == 31 && sh == (32 - mb)) {
1844         tcg_gen_ext32u_tl(t_ra, t_rs);
1845         tcg_gen_shri_tl(t_ra, t_ra, mb);
1846     } else {
1847         target_ulong mask;
1848 #if defined(TARGET_PPC64)
1849         mb += 32;
1850         me += 32;
1851 #endif
1852         mask = MASK(mb, me);
1853
1854         if (mask <= 0xffffffffu) {
1855             TCGv_i32 t0 = tcg_temp_new_i32();
1856             tcg_gen_trunc_tl_i32(t0, t_rs);
1857             tcg_gen_rotli_i32(t0, t0, sh);
1858             tcg_gen_andi_i32(t0, t0, mask);
1859             tcg_gen_extu_i32_tl(t_ra, t0);
1860             tcg_temp_free_i32(t0);
1861         } else {
1862 #if defined(TARGET_PPC64)
1863             tcg_gen_deposit_i64(t_ra, t_rs, t_rs, 32, 32);
1864             tcg_gen_rotli_i64(t_ra, t_ra, sh);
1865             tcg_gen_andi_i64(t_ra, t_ra, mask);
1866 #else
1867             g_assert_not_reached();
1868 #endif
1869         }
1870     }
1871     if (unlikely(Rc(ctx->opcode) != 0)) {
1872         gen_set_Rc0(ctx, t_ra);
1873     }
1874 }
1875
1876 /* rlwnm & rlwnm. */
1877 static void gen_rlwnm(DisasContext *ctx)
1878 {
1879     TCGv t_ra = cpu_gpr[rA(ctx->opcode)];
1880     TCGv t_rs = cpu_gpr[rS(ctx->opcode)];
1881     TCGv t_rb = cpu_gpr[rB(ctx->opcode)];
1882     uint32_t mb = MB(ctx->opcode);
1883     uint32_t me = ME(ctx->opcode);
1884     target_ulong mask;
1885
1886 #if defined(TARGET_PPC64)
1887     mb += 32;
1888     me += 32;
1889 #endif
1890     mask = MASK(mb, me);
1891
1892     if (mask <= 0xffffffffu) {
1893         TCGv_i32 t0 = tcg_temp_new_i32();
1894         TCGv_i32 t1 = tcg_temp_new_i32();
1895         tcg_gen_trunc_tl_i32(t0, t_rb);
1896         tcg_gen_trunc_tl_i32(t1, t_rs);
1897         tcg_gen_andi_i32(t0, t0, 0x1f);
1898         tcg_gen_rotl_i32(t1, t1, t0);
1899         tcg_gen_extu_i32_tl(t_ra, t1);
1900         tcg_temp_free_i32(t0);
1901         tcg_temp_free_i32(t1);
1902     } else {
1903 #if defined(TARGET_PPC64)
1904         TCGv_i64 t0 = tcg_temp_new_i64();
1905         tcg_gen_andi_i64(t0, t_rb, 0x1f);
1906         tcg_gen_deposit_i64(t_ra, t_rs, t_rs, 32, 32);
1907         tcg_gen_rotl_i64(t_ra, t_ra, t0);
1908         tcg_temp_free_i64(t0);
1909 #else
1910         g_assert_not_reached();
1911 #endif
1912     }
1913
1914     tcg_gen_andi_tl(t_ra, t_ra, mask);
1915
1916     if (unlikely(Rc(ctx->opcode) != 0)) {
1917         gen_set_Rc0(ctx, t_ra);
1918     }
1919 }
1920
1921 #if defined(TARGET_PPC64)
1922 #define GEN_PPC64_R2(name, opc1, opc2)                                        \
1923 static void glue(gen_, name##0)(DisasContext *ctx)                            \
1924 {                                                                             \
1925     gen_##name(ctx, 0);                                                       \
1926 }                                                                             \
1927                                                                               \
1928 static void glue(gen_, name##1)(DisasContext *ctx)                            \
1929 {                                                                             \
1930     gen_##name(ctx, 1);                                                       \
1931 }
1932 #define GEN_PPC64_R4(name, opc1, opc2)                                        \
1933 static void glue(gen_, name##0)(DisasContext *ctx)                            \
1934 {                                                                             \
1935     gen_##name(ctx, 0, 0);                                                    \
1936 }                                                                             \
1937                                                                               \
1938 static void glue(gen_, name##1)(DisasContext *ctx)                            \
1939 {                                                                             \
1940     gen_##name(ctx, 0, 1);                                                    \
1941 }                                                                             \
1942                                                                               \
1943 static void glue(gen_, name##2)(DisasContext *ctx)                            \
1944 {                                                                             \
1945     gen_##name(ctx, 1, 0);                                                    \
1946 }                                                                             \
1947                                                                               \
1948 static void glue(gen_, name##3)(DisasContext *ctx)                            \
1949 {                                                                             \
1950     gen_##name(ctx, 1, 1);                                                    \
1951 }
1952
1953 static void gen_rldinm(DisasContext *ctx, int mb, int me, int sh)
1954 {
1955     TCGv t_ra = cpu_gpr[rA(ctx->opcode)];
1956     TCGv t_rs = cpu_gpr[rS(ctx->opcode)];
1957
1958     if (sh != 0 && mb == 0 && me == (63 - sh)) {
1959         tcg_gen_shli_tl(t_ra, t_rs, sh);
1960     } else if (sh != 0 && me == 63 && sh == (64 - mb)) {
1961         tcg_gen_shri_tl(t_ra, t_rs, mb);
1962     } else {
1963         tcg_gen_rotli_tl(t_ra, t_rs, sh);
1964         tcg_gen_andi_tl(t_ra, t_ra, MASK(mb, me));
1965     }
1966     if (unlikely(Rc(ctx->opcode) != 0)) {
1967         gen_set_Rc0(ctx, t_ra);
1968     }
1969 }
1970
1971 /* rldicl - rldicl. */
1972 static inline void gen_rldicl(DisasContext *ctx, int mbn, int shn)
1973 {
1974     uint32_t sh, mb;
1975
1976     sh = SH(ctx->opcode) | (shn << 5);
1977     mb = MB(ctx->opcode) | (mbn << 5);
1978     gen_rldinm(ctx, mb, 63, sh);
1979 }
1980 GEN_PPC64_R4(rldicl, 0x1E, 0x00);
1981
1982 /* rldicr - rldicr. */
1983 static inline void gen_rldicr(DisasContext *ctx, int men, int shn)
1984 {
1985     uint32_t sh, me;
1986
1987     sh = SH(ctx->opcode) | (shn << 5);
1988     me = MB(ctx->opcode) | (men << 5);
1989     gen_rldinm(ctx, 0, me, sh);
1990 }
1991 GEN_PPC64_R4(rldicr, 0x1E, 0x02);
1992
1993 /* rldic - rldic. */
1994 static inline void gen_rldic(DisasContext *ctx, int mbn, int shn)
1995 {
1996     uint32_t sh, mb;
1997
1998     sh = SH(ctx->opcode) | (shn << 5);
1999     mb = MB(ctx->opcode) | (mbn << 5);
2000     gen_rldinm(ctx, mb, 63 - sh, sh);
2001 }
2002 GEN_PPC64_R4(rldic, 0x1E, 0x04);
2003
2004 static void gen_rldnm(DisasContext *ctx, int mb, int me)
2005 {
2006     TCGv t_ra = cpu_gpr[rA(ctx->opcode)];
2007     TCGv t_rs = cpu_gpr[rS(ctx->opcode)];
2008     TCGv t_rb = cpu_gpr[rB(ctx->opcode)];
2009     TCGv t0;
2010
2011     t0 = tcg_temp_new();
2012     tcg_gen_andi_tl(t0, t_rb, 0x3f);
2013     tcg_gen_rotl_tl(t_ra, t_rs, t0);
2014     tcg_temp_free(t0);
2015
2016     tcg_gen_andi_tl(t_ra, t_ra, MASK(mb, me));
2017     if (unlikely(Rc(ctx->opcode) != 0)) {
2018         gen_set_Rc0(ctx, t_ra);
2019     }
2020 }
2021
2022 /* rldcl - rldcl. */
2023 static inline void gen_rldcl(DisasContext *ctx, int mbn)
2024 {
2025     uint32_t mb;
2026
2027     mb = MB(ctx->opcode) | (mbn << 5);
2028     gen_rldnm(ctx, mb, 63);
2029 }
2030 GEN_PPC64_R2(rldcl, 0x1E, 0x08);
2031
2032 /* rldcr - rldcr. */
2033 static inline void gen_rldcr(DisasContext *ctx, int men)
2034 {
2035     uint32_t me;
2036
2037     me = MB(ctx->opcode) | (men << 5);
2038     gen_rldnm(ctx, 0, me);
2039 }
2040 GEN_PPC64_R2(rldcr, 0x1E, 0x09);
2041
2042 /* rldimi - rldimi. */
2043 static void gen_rldimi(DisasContext *ctx, int mbn, int shn)
2044 {
2045     TCGv t_ra = cpu_gpr[rA(ctx->opcode)];
2046     TCGv t_rs = cpu_gpr[rS(ctx->opcode)];
2047     uint32_t sh = SH(ctx->opcode) | (shn << 5);
2048     uint32_t mb = MB(ctx->opcode) | (mbn << 5);
2049     uint32_t me = 63 - sh;
2050
2051     if (mb <= me) {
2052         tcg_gen_deposit_tl(t_ra, t_ra, t_rs, sh, me - mb + 1);
2053     } else {
2054         target_ulong mask = MASK(mb, me);
2055         TCGv t1 = tcg_temp_new();
2056
2057         tcg_gen_rotli_tl(t1, t_rs, sh);
2058         tcg_gen_andi_tl(t1, t1, mask);
2059         tcg_gen_andi_tl(t_ra, t_ra, ~mask);
2060         tcg_gen_or_tl(t_ra, t_ra, t1);
2061         tcg_temp_free(t1);
2062     }
2063     if (unlikely(Rc(ctx->opcode) != 0)) {
2064         gen_set_Rc0(ctx, t_ra);
2065     }
2066 }
2067 GEN_PPC64_R4(rldimi, 0x1E, 0x06);
2068 #endif
2069
2070 /***                             Integer shift                             ***/
2071
2072 /* slw & slw. */
2073 static void gen_slw(DisasContext *ctx)
2074 {
2075     TCGv t0, t1;
2076
2077     t0 = tcg_temp_new();
2078     /* AND rS with a mask that is 0 when rB >= 0x20 */
2079 #if defined(TARGET_PPC64)
2080     tcg_gen_shli_tl(t0, cpu_gpr[rB(ctx->opcode)], 0x3a);
2081     tcg_gen_sari_tl(t0, t0, 0x3f);
2082 #else
2083     tcg_gen_shli_tl(t0, cpu_gpr[rB(ctx->opcode)], 0x1a);
2084     tcg_gen_sari_tl(t0, t0, 0x1f);
2085 #endif
2086     tcg_gen_andc_tl(t0, cpu_gpr[rS(ctx->opcode)], t0);
2087     t1 = tcg_temp_new();
2088     tcg_gen_andi_tl(t1, cpu_gpr[rB(ctx->opcode)], 0x1f);
2089     tcg_gen_shl_tl(cpu_gpr[rA(ctx->opcode)], t0, t1);
2090     tcg_temp_free(t1);
2091     tcg_temp_free(t0);
2092     tcg_gen_ext32u_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rA(ctx->opcode)]);
2093     if (unlikely(Rc(ctx->opcode) != 0))
2094         gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
2095 }
2096
2097 /* sraw & sraw. */
2098 static void gen_sraw(DisasContext *ctx)
2099 {
2100     gen_helper_sraw(cpu_gpr[rA(ctx->opcode)], cpu_env,
2101                     cpu_gpr[rS(ctx->opcode)], cpu_gpr[rB(ctx->opcode)]);
2102     if (unlikely(Rc(ctx->opcode) != 0))
2103         gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
2104 }
2105
2106 /* srawi & srawi. */
2107 static void gen_srawi(DisasContext *ctx)
2108 {
2109     int sh = SH(ctx->opcode);
2110     TCGv dst = cpu_gpr[rA(ctx->opcode)];
2111     TCGv src = cpu_gpr[rS(ctx->opcode)];
2112     if (sh == 0) {
2113         tcg_gen_ext32s_tl(dst, src);
2114         tcg_gen_movi_tl(cpu_ca, 0);
2115     } else {
2116         TCGv t0;
2117         tcg_gen_ext32s_tl(dst, src);
2118         tcg_gen_andi_tl(cpu_ca, dst, (1ULL << sh) - 1);
2119         t0 = tcg_temp_new();
2120         tcg_gen_sari_tl(t0, dst, TARGET_LONG_BITS - 1);
2121         tcg_gen_and_tl(cpu_ca, cpu_ca, t0);
2122         tcg_temp_free(t0);
2123         tcg_gen_setcondi_tl(TCG_COND_NE, cpu_ca, cpu_ca, 0);
2124         tcg_gen_sari_tl(dst, dst, sh);
2125     }
2126     if (unlikely(Rc(ctx->opcode) != 0)) {
2127         gen_set_Rc0(ctx, dst);
2128     }
2129 }
2130
2131 /* srw & srw. */
2132 static void gen_srw(DisasContext *ctx)
2133 {
2134     TCGv t0, t1;
2135
2136     t0 = tcg_temp_new();
2137     /* AND rS with a mask that is 0 when rB >= 0x20 */
2138 #if defined(TARGET_PPC64)
2139     tcg_gen_shli_tl(t0, cpu_gpr[rB(ctx->opcode)], 0x3a);
2140     tcg_gen_sari_tl(t0, t0, 0x3f);
2141 #else
2142     tcg_gen_shli_tl(t0, cpu_gpr[rB(ctx->opcode)], 0x1a);
2143     tcg_gen_sari_tl(t0, t0, 0x1f);
2144 #endif
2145     tcg_gen_andc_tl(t0, cpu_gpr[rS(ctx->opcode)], t0);
2146     tcg_gen_ext32u_tl(t0, t0);
2147     t1 = tcg_temp_new();
2148     tcg_gen_andi_tl(t1, cpu_gpr[rB(ctx->opcode)], 0x1f);
2149     tcg_gen_shr_tl(cpu_gpr[rA(ctx->opcode)], t0, t1);
2150     tcg_temp_free(t1);
2151     tcg_temp_free(t0);
2152     if (unlikely(Rc(ctx->opcode) != 0))
2153         gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
2154 }
2155
2156 #if defined(TARGET_PPC64)
2157 /* sld & sld. */
2158 static void gen_sld(DisasContext *ctx)
2159 {
2160     TCGv t0, t1;
2161
2162     t0 = tcg_temp_new();
2163     /* AND rS with a mask that is 0 when rB >= 0x40 */
2164     tcg_gen_shli_tl(t0, cpu_gpr[rB(ctx->opcode)], 0x39);
2165     tcg_gen_sari_tl(t0, t0, 0x3f);
2166     tcg_gen_andc_tl(t0, cpu_gpr[rS(ctx->opcode)], t0);
2167     t1 = tcg_temp_new();
2168     tcg_gen_andi_tl(t1, cpu_gpr[rB(ctx->opcode)], 0x3f);
2169     tcg_gen_shl_tl(cpu_gpr[rA(ctx->opcode)], t0, t1);
2170     tcg_temp_free(t1);
2171     tcg_temp_free(t0);
2172     if (unlikely(Rc(ctx->opcode) != 0))
2173         gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
2174 }
2175
2176 /* srad & srad. */
2177 static void gen_srad(DisasContext *ctx)
2178 {
2179     gen_helper_srad(cpu_gpr[rA(ctx->opcode)], cpu_env,
2180                     cpu_gpr[rS(ctx->opcode)], cpu_gpr[rB(ctx->opcode)]);
2181     if (unlikely(Rc(ctx->opcode) != 0))
2182         gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
2183 }
2184 /* sradi & sradi. */
2185 static inline void gen_sradi(DisasContext *ctx, int n)
2186 {
2187     int sh = SH(ctx->opcode) + (n << 5);
2188     TCGv dst = cpu_gpr[rA(ctx->opcode)];
2189     TCGv src = cpu_gpr[rS(ctx->opcode)];
2190     if (sh == 0) {
2191         tcg_gen_mov_tl(dst, src);
2192         tcg_gen_movi_tl(cpu_ca, 0);
2193     } else {
2194         TCGv t0;
2195         tcg_gen_andi_tl(cpu_ca, src, (1ULL << sh) - 1);
2196         t0 = tcg_temp_new();
2197         tcg_gen_sari_tl(t0, src, TARGET_LONG_BITS - 1);
2198         tcg_gen_and_tl(cpu_ca, cpu_ca, t0);
2199         tcg_temp_free(t0);
2200         tcg_gen_setcondi_tl(TCG_COND_NE, cpu_ca, cpu_ca, 0);
2201         tcg_gen_sari_tl(dst, src, sh);
2202     }
2203     if (unlikely(Rc(ctx->opcode) != 0)) {
2204         gen_set_Rc0(ctx, dst);
2205     }
2206 }
2207
2208 static void gen_sradi0(DisasContext *ctx)
2209 {
2210     gen_sradi(ctx, 0);
2211 }
2212
2213 static void gen_sradi1(DisasContext *ctx)
2214 {
2215     gen_sradi(ctx, 1);
2216 }
2217
2218 /* srd & srd. */
2219 static void gen_srd(DisasContext *ctx)
2220 {
2221     TCGv t0, t1;
2222
2223     t0 = tcg_temp_new();
2224     /* AND rS with a mask that is 0 when rB >= 0x40 */
2225     tcg_gen_shli_tl(t0, cpu_gpr[rB(ctx->opcode)], 0x39);
2226     tcg_gen_sari_tl(t0, t0, 0x3f);
2227     tcg_gen_andc_tl(t0, cpu_gpr[rS(ctx->opcode)], t0);
2228     t1 = tcg_temp_new();
2229     tcg_gen_andi_tl(t1, cpu_gpr[rB(ctx->opcode)], 0x3f);
2230     tcg_gen_shr_tl(cpu_gpr[rA(ctx->opcode)], t0, t1);
2231     tcg_temp_free(t1);
2232     tcg_temp_free(t0);
2233     if (unlikely(Rc(ctx->opcode) != 0))
2234         gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
2235 }
2236 #endif
2237
2238 #if defined(TARGET_PPC64)
2239 static void gen_set_cr1_from_fpscr(DisasContext *ctx)
2240 {
2241     TCGv_i32 tmp = tcg_temp_new_i32();
2242     tcg_gen_trunc_tl_i32(tmp, cpu_fpscr);
2243     tcg_gen_shri_i32(cpu_crf[1], tmp, 28);
2244     tcg_temp_free_i32(tmp);
2245 }
2246 #else
2247 static void gen_set_cr1_from_fpscr(DisasContext *ctx)
2248 {
2249     tcg_gen_shri_tl(cpu_crf[1], cpu_fpscr, 28);
2250 }
2251 #endif
2252
2253 /***                       Floating-Point arithmetic                       ***/
2254 #define _GEN_FLOAT_ACB(name, op, op1, op2, isfloat, set_fprf, type)           \
2255 static void gen_f##name(DisasContext *ctx)                                    \
2256 {                                                                             \
2257     if (unlikely(!ctx->fpu_enabled)) {                                        \
2258         gen_exception(ctx, POWERPC_EXCP_FPU);                                 \
2259         return;                                                               \
2260     }                                                                         \
2261     /* NIP cannot be restored if the memory exception comes from an helper */ \
2262     gen_update_nip(ctx, ctx->nip - 4);                                        \
2263     gen_reset_fpstatus();                                                     \
2264     gen_helper_f##op(cpu_fpr[rD(ctx->opcode)], cpu_env,                       \
2265                      cpu_fpr[rA(ctx->opcode)],                                \
2266                      cpu_fpr[rC(ctx->opcode)], cpu_fpr[rB(ctx->opcode)]);     \
2267     if (isfloat) {                                                            \
2268         gen_helper_frsp(cpu_fpr[rD(ctx->opcode)], cpu_env,                    \
2269                         cpu_fpr[rD(ctx->opcode)]);                            \
2270     }                                                                         \
2271     if (set_fprf) {                                                           \
2272         gen_compute_fprf(cpu_fpr[rD(ctx->opcode)]);                           \
2273     }                                                                         \
2274     if (unlikely(Rc(ctx->opcode) != 0)) {                                     \
2275         gen_set_cr1_from_fpscr(ctx);                                          \
2276     }                                                                         \
2277 }
2278
2279 #define GEN_FLOAT_ACB(name, op2, set_fprf, type)                              \
2280 _GEN_FLOAT_ACB(name, name, 0x3F, op2, 0, set_fprf, type);                     \
2281 _GEN_FLOAT_ACB(name##s, name, 0x3B, op2, 1, set_fprf, type);
2282
2283 #define _GEN_FLOAT_AB(name, op, op1, op2, inval, isfloat, set_fprf, type)     \
2284 static void gen_f##name(DisasContext *ctx)                                    \
2285 {                                                                             \
2286     if (unlikely(!ctx->fpu_enabled)) {                                        \
2287         gen_exception(ctx, POWERPC_EXCP_FPU);                                 \
2288         return;                                                               \
2289     }                                                                         \
2290     /* NIP cannot be restored if the memory exception comes from an helper */ \
2291     gen_update_nip(ctx, ctx->nip - 4);                                        \
2292     gen_reset_fpstatus();                                                     \
2293     gen_helper_f##op(cpu_fpr[rD(ctx->opcode)], cpu_env,                       \
2294                      cpu_fpr[rA(ctx->opcode)],                                \
2295                      cpu_fpr[rB(ctx->opcode)]);                               \
2296     if (isfloat) {                                                            \
2297         gen_helper_frsp(cpu_fpr[rD(ctx->opcode)], cpu_env,                    \
2298                         cpu_fpr[rD(ctx->opcode)]);                            \
2299     }                                                                         \
2300     if (set_fprf) {                                                           \
2301         gen_compute_fprf(cpu_fpr[rD(ctx->opcode)]);                           \
2302     }                                                                         \
2303     if (unlikely(Rc(ctx->opcode) != 0)) {                                     \
2304         gen_set_cr1_from_fpscr(ctx);                                          \
2305     }                                                                         \
2306 }
2307 #define GEN_FLOAT_AB(name, op2, inval, set_fprf, type)                        \
2308 _GEN_FLOAT_AB(name, name, 0x3F, op2, inval, 0, set_fprf, type);               \
2309 _GEN_FLOAT_AB(name##s, name, 0x3B, op2, inval, 1, set_fprf, type);
2310
2311 #define _GEN_FLOAT_AC(name, op, op1, op2, inval, isfloat, set_fprf, type)     \
2312 static void gen_f##name(DisasContext *ctx)                                    \
2313 {                                                                             \
2314     if (unlikely(!ctx->fpu_enabled)) {                                        \
2315         gen_exception(ctx, POWERPC_EXCP_FPU);                                 \
2316         return;                                                               \
2317     }                                                                         \
2318     /* NIP cannot be restored if the memory exception comes from an helper */ \
2319     gen_update_nip(ctx, ctx->nip - 4);                                        \
2320     gen_reset_fpstatus();                                                     \
2321     gen_helper_f##op(cpu_fpr[rD(ctx->opcode)], cpu_env,                       \
2322                      cpu_fpr[rA(ctx->opcode)],                                \
2323                      cpu_fpr[rC(ctx->opcode)]);                               \
2324     if (isfloat) {                                                            \
2325         gen_helper_frsp(cpu_fpr[rD(ctx->opcode)], cpu_env,                    \
2326                         cpu_fpr[rD(ctx->opcode)]);                            \
2327     }                                                                         \
2328     if (set_fprf) {                                                           \
2329         gen_compute_fprf(cpu_fpr[rD(ctx->opcode)]);                           \
2330     }                                                                         \
2331     if (unlikely(Rc(ctx->opcode) != 0)) {                                     \
2332         gen_set_cr1_from_fpscr(ctx);                                          \
2333     }                                                                         \
2334 }
2335 #define GEN_FLOAT_AC(name, op2, inval, set_fprf, type)                        \
2336 _GEN_FLOAT_AC(name, name, 0x3F, op2, inval, 0, set_fprf, type);               \
2337 _GEN_FLOAT_AC(name##s, name, 0x3B, op2, inval, 1, set_fprf, type);
2338
2339 #define GEN_FLOAT_B(name, op2, op3, set_fprf, type)                           \
2340 static void gen_f##name(DisasContext *ctx)                                    \
2341 {                                                                             \
2342     if (unlikely(!ctx->fpu_enabled)) {                                        \
2343         gen_exception(ctx, POWERPC_EXCP_FPU);                                 \
2344         return;                                                               \
2345     }                                                                         \
2346     /* NIP cannot be restored if the memory exception comes from an helper */ \
2347     gen_update_nip(ctx, ctx->nip - 4);                                        \
2348     gen_reset_fpstatus();                                                     \
2349     gen_helper_f##name(cpu_fpr[rD(ctx->opcode)], cpu_env,                     \
2350                        cpu_fpr[rB(ctx->opcode)]);                             \
2351     if (set_fprf) {                                                           \
2352         gen_compute_fprf(cpu_fpr[rD(ctx->opcode)]);                           \
2353     }                                                                         \
2354     if (unlikely(Rc(ctx->opcode) != 0)) {                                     \
2355         gen_set_cr1_from_fpscr(ctx);                                          \
2356     }                                                                         \
2357 }
2358
2359 #define GEN_FLOAT_BS(name, op1, op2, set_fprf, type)                          \
2360 static void gen_f##name(DisasContext *ctx)                                    \
2361 {                                                                             \
2362     if (unlikely(!ctx->fpu_enabled)) {                                        \
2363         gen_exception(ctx, POWERPC_EXCP_FPU);                                 \
2364         return;                                                               \
2365     }                                                                         \
2366     /* NIP cannot be restored if the memory exception comes from an helper */ \
2367     gen_update_nip(ctx, ctx->nip - 4);                                        \
2368     gen_reset_fpstatus();                                                     \
2369     gen_helper_f##name(cpu_fpr[rD(ctx->opcode)], cpu_env,                     \
2370                        cpu_fpr[rB(ctx->opcode)]);                             \
2371     if (set_fprf) {                                                           \
2372         gen_compute_fprf(cpu_fpr[rD(ctx->opcode)]);                           \
2373     }                                                                         \
2374     if (unlikely(Rc(ctx->opcode) != 0)) {                                     \
2375         gen_set_cr1_from_fpscr(ctx);                                          \
2376     }                                                                         \
2377 }
2378
2379 /* fadd - fadds */
2380 GEN_FLOAT_AB(add, 0x15, 0x000007C0, 1, PPC_FLOAT);
2381 /* fdiv - fdivs */
2382 GEN_FLOAT_AB(div, 0x12, 0x000007C0, 1, PPC_FLOAT);
2383 /* fmul - fmuls */
2384 GEN_FLOAT_AC(mul, 0x19, 0x0000F800, 1, PPC_FLOAT);
2385
2386 /* fre */
2387 GEN_FLOAT_BS(re, 0x3F, 0x18, 1, PPC_FLOAT_EXT);
2388
2389 /* fres */
2390 GEN_FLOAT_BS(res, 0x3B, 0x18, 1, PPC_FLOAT_FRES);
2391
2392 /* frsqrte */
2393 GEN_FLOAT_BS(rsqrte, 0x3F, 0x1A, 1, PPC_FLOAT_FRSQRTE);
2394
2395 /* frsqrtes */
2396 static void gen_frsqrtes(DisasContext *ctx)
2397 {
2398     if (unlikely(!ctx->fpu_enabled)) {
2399         gen_exception(ctx, POWERPC_EXCP_FPU);
2400         return;
2401     }
2402     /* NIP cannot be restored if the memory exception comes from an helper */
2403     gen_update_nip(ctx, ctx->nip - 4);
2404     gen_reset_fpstatus();
2405     gen_helper_frsqrte(cpu_fpr[rD(ctx->opcode)], cpu_env,
2406                        cpu_fpr[rB(ctx->opcode)]);
2407     gen_helper_frsp(cpu_fpr[rD(ctx->opcode)], cpu_env,
2408                     cpu_fpr[rD(ctx->opcode)]);
2409     gen_compute_fprf(cpu_fpr[rD(ctx->opcode)]);
2410     if (unlikely(Rc(ctx->opcode) != 0)) {
2411         gen_set_cr1_from_fpscr(ctx);
2412     }
2413 }
2414
2415 /* fsel */
2416 _GEN_FLOAT_ACB(sel, sel, 0x3F, 0x17, 0, 0, PPC_FLOAT_FSEL);
2417 /* fsub - fsubs */
2418 GEN_FLOAT_AB(sub, 0x14, 0x000007C0, 1, PPC_FLOAT);
2419 /* Optional: */
2420
2421 /* fsqrt */
2422 static void gen_fsqrt(DisasContext *ctx)
2423 {
2424     if (unlikely(!ctx->fpu_enabled)) {
2425         gen_exception(ctx, POWERPC_EXCP_FPU);
2426         return;
2427     }
2428     /* NIP cannot be restored if the memory exception comes from an helper */
2429     gen_update_nip(ctx, ctx->nip - 4);
2430     gen_reset_fpstatus();
2431     gen_helper_fsqrt(cpu_fpr[rD(ctx->opcode)], cpu_env,
2432                      cpu_fpr[rB(ctx->opcode)]);
2433     gen_compute_fprf(cpu_fpr[rD(ctx->opcode)]);
2434     if (unlikely(Rc(ctx->opcode) != 0)) {
2435         gen_set_cr1_from_fpscr(ctx);
2436     }
2437 }
2438
2439 static void gen_fsqrts(DisasContext *ctx)
2440 {
2441     if (unlikely(!ctx->fpu_enabled)) {
2442         gen_exception(ctx, POWERPC_EXCP_FPU);
2443         return;
2444     }
2445     /* NIP cannot be restored if the memory exception comes from an helper */
2446     gen_update_nip(ctx, ctx->nip - 4);
2447     gen_reset_fpstatus();
2448     gen_helper_fsqrt(cpu_fpr[rD(ctx->opcode)], cpu_env,
2449                      cpu_fpr[rB(ctx->opcode)]);
2450     gen_helper_frsp(cpu_fpr[rD(ctx->opcode)], cpu_env,
2451                     cpu_fpr[rD(ctx->opcode)]);
2452     gen_compute_fprf(cpu_fpr[rD(ctx->opcode)]);
2453     if (unlikely(Rc(ctx->opcode) != 0)) {
2454         gen_set_cr1_from_fpscr(ctx);
2455     }
2456 }
2457
2458 /***                     Floating-Point multiply-and-add                   ***/
2459 /* fmadd - fmadds */
2460 GEN_FLOAT_ACB(madd, 0x1D, 1, PPC_FLOAT);
2461 /* fmsub - fmsubs */
2462 GEN_FLOAT_ACB(msub, 0x1C, 1, PPC_FLOAT);
2463 /* fnmadd - fnmadds */
2464 GEN_FLOAT_ACB(nmadd, 0x1F, 1, PPC_FLOAT);
2465 /* fnmsub - fnmsubs */
2466 GEN_FLOAT_ACB(nmsub, 0x1E, 1, PPC_FLOAT);
2467
2468 /***                     Floating-Point round & convert                    ***/
2469 /* fctiw */
2470 GEN_FLOAT_B(ctiw, 0x0E, 0x00, 0, PPC_FLOAT);
2471 /* fctiwu */
2472 GEN_FLOAT_B(ctiwu, 0x0E, 0x04, 0, PPC2_FP_CVT_ISA206);
2473 /* fctiwz */
2474 GEN_FLOAT_B(ctiwz, 0x0F, 0x00, 0, PPC_FLOAT);
2475 /* fctiwuz */
2476 GEN_FLOAT_B(ctiwuz, 0x0F, 0x04, 0, PPC2_FP_CVT_ISA206);
2477 /* frsp */
2478 GEN_FLOAT_B(rsp, 0x0C, 0x00, 1, PPC_FLOAT);
2479 /* fcfid */
2480 GEN_FLOAT_B(cfid, 0x0E, 0x1A, 1, PPC2_FP_CVT_S64);
2481 /* fcfids */
2482 GEN_FLOAT_B(cfids, 0x0E, 0x1A, 0, PPC2_FP_CVT_ISA206);
2483 /* fcfidu */
2484 GEN_FLOAT_B(cfidu, 0x0E, 0x1E, 0, PPC2_FP_CVT_ISA206);
2485 /* fcfidus */
2486 GEN_FLOAT_B(cfidus, 0x0E, 0x1E, 0, PPC2_FP_CVT_ISA206);
2487 /* fctid */
2488 GEN_FLOAT_B(ctid, 0x0E, 0x19, 0, PPC2_FP_CVT_S64);
2489 /* fctidu */
2490 GEN_FLOAT_B(ctidu, 0x0E, 0x1D, 0, PPC2_FP_CVT_ISA206);
2491 /* fctidz */
2492 GEN_FLOAT_B(ctidz, 0x0F, 0x19, 0, PPC2_FP_CVT_S64);
2493 /* fctidu */
2494 GEN_FLOAT_B(ctiduz, 0x0F, 0x1D, 0, PPC2_FP_CVT_ISA206);
2495
2496 /* frin */
2497 GEN_FLOAT_B(rin, 0x08, 0x0C, 1, PPC_FLOAT_EXT);
2498 /* friz */
2499 GEN_FLOAT_B(riz, 0x08, 0x0D, 1, PPC_FLOAT_EXT);
2500 /* frip */
2501 GEN_FLOAT_B(rip, 0x08, 0x0E, 1, PPC_FLOAT_EXT);
2502 /* frim */
2503 GEN_FLOAT_B(rim, 0x08, 0x0F, 1, PPC_FLOAT_EXT);
2504
2505 static void gen_ftdiv(DisasContext *ctx)
2506 {
2507     if (unlikely(!ctx->fpu_enabled)) {
2508         gen_exception(ctx, POWERPC_EXCP_FPU);
2509         return;
2510     }
2511     gen_helper_ftdiv(cpu_crf[crfD(ctx->opcode)], cpu_fpr[rA(ctx->opcode)],
2512                      cpu_fpr[rB(ctx->opcode)]);
2513 }
2514
2515 static void gen_ftsqrt(DisasContext *ctx)
2516 {
2517     if (unlikely(!ctx->fpu_enabled)) {
2518         gen_exception(ctx, POWERPC_EXCP_FPU);
2519         return;
2520     }
2521     gen_helper_ftsqrt(cpu_crf[crfD(ctx->opcode)], cpu_fpr[rB(ctx->opcode)]);
2522 }
2523
2524
2525
2526 /***                         Floating-Point compare                        ***/
2527
2528 /* fcmpo */
2529 static void gen_fcmpo(DisasContext *ctx)
2530 {
2531     TCGv_i32 crf;
2532     if (unlikely(!ctx->fpu_enabled)) {
2533         gen_exception(ctx, POWERPC_EXCP_FPU);
2534         return;
2535     }
2536     /* NIP cannot be restored if the memory exception comes from an helper */
2537     gen_update_nip(ctx, ctx->nip - 4);
2538     gen_reset_fpstatus();
2539     crf = tcg_const_i32(crfD(ctx->opcode));
2540     gen_helper_fcmpo(cpu_env, cpu_fpr[rA(ctx->opcode)],
2541                      cpu_fpr[rB(ctx->opcode)], crf);
2542     tcg_temp_free_i32(crf);
2543     gen_helper_float_check_status(cpu_env);
2544 }
2545
2546 /* fcmpu */
2547 static void gen_fcmpu(DisasContext *ctx)
2548 {
2549     TCGv_i32 crf;
2550     if (unlikely(!ctx->fpu_enabled)) {
2551         gen_exception(ctx, POWERPC_EXCP_FPU);
2552         return;
2553     }
2554     /* NIP cannot be restored if the memory exception comes from an helper */
2555     gen_update_nip(ctx, ctx->nip - 4);
2556     gen_reset_fpstatus();
2557     crf = tcg_const_i32(crfD(ctx->opcode));
2558     gen_helper_fcmpu(cpu_env, cpu_fpr[rA(ctx->opcode)],
2559                      cpu_fpr[rB(ctx->opcode)], crf);
2560     tcg_temp_free_i32(crf);
2561     gen_helper_float_check_status(cpu_env);
2562 }
2563
2564 /***                         Floating-point move                           ***/
2565 /* fabs */
2566 /* XXX: beware that fabs never checks for NaNs nor update FPSCR */
2567 static void gen_fabs(DisasContext *ctx)
2568 {
2569     if (unlikely(!ctx->fpu_enabled)) {
2570         gen_exception(ctx, POWERPC_EXCP_FPU);
2571         return;
2572     }
2573     tcg_gen_andi_i64(cpu_fpr[rD(ctx->opcode)], cpu_fpr[rB(ctx->opcode)],
2574                      ~(1ULL << 63));
2575     if (unlikely(Rc(ctx->opcode))) {
2576         gen_set_cr1_from_fpscr(ctx);
2577     }
2578 }
2579
2580 /* fmr  - fmr. */
2581 /* XXX: beware that fmr never checks for NaNs nor update FPSCR */
2582 static void gen_fmr(DisasContext *ctx)
2583 {
2584     if (unlikely(!ctx->fpu_enabled)) {
2585         gen_exception(ctx, POWERPC_EXCP_FPU);
2586         return;
2587     }
2588     tcg_gen_mov_i64(cpu_fpr[rD(ctx->opcode)], cpu_fpr[rB(ctx->opcode)]);
2589     if (unlikely(Rc(ctx->opcode))) {
2590         gen_set_cr1_from_fpscr(ctx);
2591     }
2592 }
2593
2594 /* fnabs */
2595 /* XXX: beware that fnabs never checks for NaNs nor update FPSCR */
2596 static void gen_fnabs(DisasContext *ctx)
2597 {
2598     if (unlikely(!ctx->fpu_enabled)) {
2599         gen_exception(ctx, POWERPC_EXCP_FPU);
2600         return;
2601     }
2602     tcg_gen_ori_i64(cpu_fpr[rD(ctx->opcode)], cpu_fpr[rB(ctx->opcode)],
2603                     1ULL << 63);
2604     if (unlikely(Rc(ctx->opcode))) {
2605         gen_set_cr1_from_fpscr(ctx);
2606     }
2607 }
2608
2609 /* fneg */
2610 /* XXX: beware that fneg never checks for NaNs nor update FPSCR */
2611 static void gen_fneg(DisasContext *ctx)
2612 {
2613     if (unlikely(!ctx->fpu_enabled)) {
2614         gen_exception(ctx, POWERPC_EXCP_FPU);
2615         return;
2616     }
2617     tcg_gen_xori_i64(cpu_fpr[rD(ctx->opcode)], cpu_fpr[rB(ctx->opcode)],
2618                      1ULL << 63);
2619     if (unlikely(Rc(ctx->opcode))) {
2620         gen_set_cr1_from_fpscr(ctx);
2621     }
2622 }
2623
2624 /* fcpsgn: PowerPC 2.05 specification */
2625 /* XXX: beware that fcpsgn never checks for NaNs nor update FPSCR */
2626 static void gen_fcpsgn(DisasContext *ctx)
2627 {
2628     if (unlikely(!ctx->fpu_enabled)) {
2629         gen_exception(ctx, POWERPC_EXCP_FPU);
2630         return;
2631     }
2632     tcg_gen_deposit_i64(cpu_fpr[rD(ctx->opcode)], cpu_fpr[rA(ctx->opcode)],
2633                         cpu_fpr[rB(ctx->opcode)], 0, 63);
2634     if (unlikely(Rc(ctx->opcode))) {
2635         gen_set_cr1_from_fpscr(ctx);
2636     }
2637 }
2638
2639 static void gen_fmrgew(DisasContext *ctx)
2640 {
2641     TCGv_i64 b0;
2642     if (unlikely(!ctx->fpu_enabled)) {
2643         gen_exception(ctx, POWERPC_EXCP_FPU);
2644         return;
2645     }
2646     b0 = tcg_temp_new_i64();
2647     tcg_gen_shri_i64(b0, cpu_fpr[rB(ctx->opcode)], 32);
2648     tcg_gen_deposit_i64(cpu_fpr[rD(ctx->opcode)], cpu_fpr[rA(ctx->opcode)],
2649                         b0, 0, 32);
2650     tcg_temp_free_i64(b0);
2651 }
2652
2653 static void gen_fmrgow(DisasContext *ctx)
2654 {
2655     if (unlikely(!ctx->fpu_enabled)) {
2656         gen_exception(ctx, POWERPC_EXCP_FPU);
2657         return;
2658     }
2659     tcg_gen_deposit_i64(cpu_fpr[rD(ctx->opcode)],
2660                         cpu_fpr[rB(ctx->opcode)],
2661                         cpu_fpr[rA(ctx->opcode)],
2662                         32, 32);
2663 }
2664
2665 /***                  Floating-Point status & ctrl register                ***/
2666
2667 /* mcrfs */
2668 static void gen_mcrfs(DisasContext *ctx)
2669 {
2670     TCGv tmp = tcg_temp_new();
2671     TCGv_i32 tmask;
2672     TCGv_i64 tnew_fpscr = tcg_temp_new_i64();
2673     int bfa;
2674     int nibble;
2675     int shift;
2676
2677     if (unlikely(!ctx->fpu_enabled)) {
2678         gen_exception(ctx, POWERPC_EXCP_FPU);
2679         return;
2680     }
2681     bfa = crfS(ctx->opcode);
2682     nibble = 7 - bfa;
2683     shift = 4 * nibble;
2684     tcg_gen_shri_tl(tmp, cpu_fpscr, shift);
2685     tcg_gen_trunc_tl_i32(cpu_crf[crfD(ctx->opcode)], tmp);
2686     tcg_gen_andi_i32(cpu_crf[crfD(ctx->opcode)], cpu_crf[crfD(ctx->opcode)], 0xf);
2687     tcg_temp_free(tmp);
2688     tcg_gen_extu_tl_i64(tnew_fpscr, cpu_fpscr);
2689     /* Only the exception bits (including FX) should be cleared if read */
2690     tcg_gen_andi_i64(tnew_fpscr, tnew_fpscr, ~((0xF << shift) & FP_EX_CLEAR_BITS));
2691     /* FEX and VX need to be updated, so don't set fpscr directly */
2692     tmask = tcg_const_i32(1 << nibble);
2693     gen_helper_store_fpscr(cpu_env, tnew_fpscr, tmask);
2694     tcg_temp_free_i32(tmask);
2695     tcg_temp_free_i64(tnew_fpscr);
2696 }
2697
2698 /* mffs */
2699 static void gen_mffs(DisasContext *ctx)
2700 {
2701     if (unlikely(!ctx->fpu_enabled)) {
2702         gen_exception(ctx, POWERPC_EXCP_FPU);
2703         return;
2704     }
2705     gen_reset_fpstatus();
2706     tcg_gen_extu_tl_i64(cpu_fpr[rD(ctx->opcode)], cpu_fpscr);
2707     if (unlikely(Rc(ctx->opcode))) {
2708         gen_set_cr1_from_fpscr(ctx);
2709     }
2710 }
2711
2712 /* mtfsb0 */
2713 static void gen_mtfsb0(DisasContext *ctx)
2714 {
2715     uint8_t crb;
2716
2717     if (unlikely(!ctx->fpu_enabled)) {
2718         gen_exception(ctx, POWERPC_EXCP_FPU);
2719         return;
2720     }
2721     crb = 31 - crbD(ctx->opcode);
2722     gen_reset_fpstatus();
2723     if (likely(crb != FPSCR_FEX && crb != FPSCR_VX)) {
2724         TCGv_i32 t0;
2725         /* NIP cannot be restored if the memory exception comes from an helper */
2726         gen_update_nip(ctx, ctx->nip - 4);
2727         t0 = tcg_const_i32(crb);
2728         gen_helper_fpscr_clrbit(cpu_env, t0);
2729         tcg_temp_free_i32(t0);
2730     }
2731     if (unlikely(Rc(ctx->opcode) != 0)) {
2732         tcg_gen_trunc_tl_i32(cpu_crf[1], cpu_fpscr);
2733         tcg_gen_shri_i32(cpu_crf[1], cpu_crf[1], FPSCR_OX);
2734     }
2735 }
2736
2737 /* mtfsb1 */
2738 static void gen_mtfsb1(DisasContext *ctx)
2739 {
2740     uint8_t crb;
2741
2742     if (unlikely(!ctx->fpu_enabled)) {
2743         gen_exception(ctx, POWERPC_EXCP_FPU);
2744         return;
2745     }
2746     crb = 31 - crbD(ctx->opcode);
2747     gen_reset_fpstatus();
2748     /* XXX: we pretend we can only do IEEE floating-point computations */
2749     if (likely(crb != FPSCR_FEX && crb != FPSCR_VX && crb != FPSCR_NI)) {
2750         TCGv_i32 t0;
2751         /* NIP cannot be restored if the memory exception comes from an helper */
2752         gen_update_nip(ctx, ctx->nip - 4);
2753         t0 = tcg_const_i32(crb);
2754         gen_helper_fpscr_setbit(cpu_env, t0);
2755         tcg_temp_free_i32(t0);
2756     }
2757     if (unlikely(Rc(ctx->opcode) != 0)) {
2758         tcg_gen_trunc_tl_i32(cpu_crf[1], cpu_fpscr);
2759         tcg_gen_shri_i32(cpu_crf[1], cpu_crf[1], FPSCR_OX);
2760     }
2761     /* We can raise a differed exception */
2762     gen_helper_float_check_status(cpu_env);
2763 }
2764
2765 /* mtfsf */
2766 static void gen_mtfsf(DisasContext *ctx)
2767 {
2768     TCGv_i32 t0;
2769     int flm, l, w;
2770
2771     if (unlikely(!ctx->fpu_enabled)) {
2772         gen_exception(ctx, POWERPC_EXCP_FPU);
2773         return;
2774     }
2775     flm = FPFLM(ctx->opcode);
2776     l = FPL(ctx->opcode);
2777     w = FPW(ctx->opcode);
2778     if (unlikely(w & !(ctx->insns_flags2 & PPC2_ISA205))) {
2779         gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL);
2780         return;
2781     }
2782     /* NIP cannot be restored if the memory exception comes from an helper */
2783     gen_update_nip(ctx, ctx->nip - 4);
2784     gen_reset_fpstatus();
2785     if (l) {
2786         t0 = tcg_const_i32((ctx->insns_flags2 & PPC2_ISA205) ? 0xffff : 0xff);
2787     } else {
2788         t0 = tcg_const_i32(flm << (w * 8));
2789     }
2790     gen_helper_store_fpscr(cpu_env, cpu_fpr[rB(ctx->opcode)], t0);
2791     tcg_temp_free_i32(t0);
2792     if (unlikely(Rc(ctx->opcode) != 0)) {
2793         tcg_gen_trunc_tl_i32(cpu_crf[1], cpu_fpscr);
2794         tcg_gen_shri_i32(cpu_crf[1], cpu_crf[1], FPSCR_OX);
2795     }
2796     /* We can raise a differed exception */
2797     gen_helper_float_check_status(cpu_env);
2798 }
2799
2800 /* mtfsfi */
2801 static void gen_mtfsfi(DisasContext *ctx)
2802 {
2803     int bf, sh, w;
2804     TCGv_i64 t0;
2805     TCGv_i32 t1;
2806
2807     if (unlikely(!ctx->fpu_enabled)) {
2808         gen_exception(ctx, POWERPC_EXCP_FPU);
2809         return;
2810     }
2811     w = FPW(ctx->opcode);
2812     bf = FPBF(ctx->opcode);
2813     if (unlikely(w & !(ctx->insns_flags2 & PPC2_ISA205))) {
2814         gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL);
2815         return;
2816     }
2817     sh = (8 * w) + 7 - bf;
2818     /* NIP cannot be restored if the memory exception comes from an helper */
2819     gen_update_nip(ctx, ctx->nip - 4);
2820     gen_reset_fpstatus();
2821     t0 = tcg_const_i64(((uint64_t)FPIMM(ctx->opcode)) << (4 * sh));
2822     t1 = tcg_const_i32(1 << sh);
2823     gen_helper_store_fpscr(cpu_env, t0, t1);
2824     tcg_temp_free_i64(t0);
2825     tcg_temp_free_i32(t1);
2826     if (unlikely(Rc(ctx->opcode) != 0)) {
2827         tcg_gen_trunc_tl_i32(cpu_crf[1], cpu_fpscr);
2828         tcg_gen_shri_i32(cpu_crf[1], cpu_crf[1], FPSCR_OX);
2829     }
2830     /* We can raise a differed exception */
2831     gen_helper_float_check_status(cpu_env);
2832 }
2833
2834 /***                           Addressing modes                            ***/
2835 /* Register indirect with immediate index : EA = (rA|0) + SIMM */
2836 static inline void gen_addr_imm_index(DisasContext *ctx, TCGv EA,
2837                                       target_long maskl)
2838 {
2839     target_long simm = SIMM(ctx->opcode);
2840
2841     simm &= ~maskl;
2842     if (rA(ctx->opcode) == 0) {
2843         if (NARROW_MODE(ctx)) {
2844             simm = (uint32_t)simm;
2845         }
2846         tcg_gen_movi_tl(EA, simm);
2847     } else if (likely(simm != 0)) {
2848         tcg_gen_addi_tl(EA, cpu_gpr[rA(ctx->opcode)], simm);
2849         if (NARROW_MODE(ctx)) {
2850             tcg_gen_ext32u_tl(EA, EA);
2851         }
2852     } else {
2853         if (NARROW_MODE(ctx)) {
2854             tcg_gen_ext32u_tl(EA, cpu_gpr[rA(ctx->opcode)]);
2855         } else {
2856             tcg_gen_mov_tl(EA, cpu_gpr[rA(ctx->opcode)]);
2857         }
2858     }
2859 }
2860
2861 static inline void gen_addr_reg_index(DisasContext *ctx, TCGv EA)
2862 {
2863     if (rA(ctx->opcode) == 0) {
2864         if (NARROW_MODE(ctx)) {
2865             tcg_gen_ext32u_tl(EA, cpu_gpr[rB(ctx->opcode)]);
2866         } else {
2867             tcg_gen_mov_tl(EA, cpu_gpr[rB(ctx->opcode)]);
2868         }
2869     } else {
2870         tcg_gen_add_tl(EA, cpu_gpr[rA(ctx->opcode)], cpu_gpr[rB(ctx->opcode)]);
2871         if (NARROW_MODE(ctx)) {
2872             tcg_gen_ext32u_tl(EA, EA);
2873         }
2874     }
2875 }
2876
2877 static inline void gen_addr_register(DisasContext *ctx, TCGv EA)
2878 {
2879     if (rA(ctx->opcode) == 0) {
2880         tcg_gen_movi_tl(EA, 0);
2881     } else if (NARROW_MODE(ctx)) {
2882         tcg_gen_ext32u_tl(EA, cpu_gpr[rA(ctx->opcode)]);
2883     } else {
2884         tcg_gen_mov_tl(EA, cpu_gpr[rA(ctx->opcode)]);
2885     }
2886 }
2887
2888 static inline void gen_addr_add(DisasContext *ctx, TCGv ret, TCGv arg1,
2889                                 target_long val)
2890 {
2891     tcg_gen_addi_tl(ret, arg1, val);
2892     if (NARROW_MODE(ctx)) {
2893         tcg_gen_ext32u_tl(ret, ret);
2894     }
2895 }
2896
2897 static inline void gen_check_align(DisasContext *ctx, TCGv EA, int mask)
2898 {
2899     TCGLabel *l1 = gen_new_label();
2900     TCGv t0 = tcg_temp_new();
2901     TCGv_i32 t1, t2;
2902     /* NIP cannot be restored if the memory exception comes from an helper */
2903     gen_update_nip(ctx, ctx->nip - 4);
2904     tcg_gen_andi_tl(t0, EA, mask);
2905     tcg_gen_brcondi_tl(TCG_COND_EQ, t0, 0, l1);
2906     t1 = tcg_const_i32(POWERPC_EXCP_ALIGN);
2907     t2 = tcg_const_i32(0);
2908     gen_helper_raise_exception_err(cpu_env, t1, t2);
2909     tcg_temp_free_i32(t1);
2910     tcg_temp_free_i32(t2);
2911     gen_set_label(l1);
2912     tcg_temp_free(t0);
2913 }
2914
2915 /***                             Integer load                              ***/
2916 static inline void gen_qemu_ld8u(DisasContext *ctx, TCGv arg1, TCGv arg2)
2917 {
2918     tcg_gen_qemu_ld8u(arg1, arg2, ctx->mem_idx);
2919 }
2920
2921 static inline void gen_qemu_ld16u(DisasContext *ctx, TCGv arg1, TCGv arg2)
2922 {
2923     TCGMemOp op = MO_UW | ctx->default_tcg_memop_mask;
2924     tcg_gen_qemu_ld_tl(arg1, arg2, ctx->mem_idx, op);
2925 }
2926
2927 static inline void gen_qemu_ld16s(DisasContext *ctx, TCGv arg1, TCGv arg2)
2928 {
2929     TCGMemOp op = MO_SW | ctx->default_tcg_memop_mask;
2930     tcg_gen_qemu_ld_tl(arg1, arg2, ctx->mem_idx, op);
2931 }
2932
2933 static inline void gen_qemu_ld32u(DisasContext *ctx, TCGv arg1, TCGv arg2)
2934 {
2935     TCGMemOp op = MO_UL | ctx->default_tcg_memop_mask;
2936     tcg_gen_qemu_ld_tl(arg1, arg2, ctx->mem_idx, op);
2937 }
2938
2939 static void gen_qemu_ld32u_i64(DisasContext *ctx, TCGv_i64 val, TCGv addr)
2940 {
2941     TCGv tmp = tcg_temp_new();
2942     gen_qemu_ld32u(ctx, tmp, addr);
2943     tcg_gen_extu_tl_i64(val, tmp);
2944     tcg_temp_free(tmp);
2945 }
2946
2947 static inline void gen_qemu_ld32s(DisasContext *ctx, TCGv arg1, TCGv arg2)
2948 {
2949     TCGMemOp op = MO_SL | ctx->default_tcg_memop_mask;
2950     tcg_gen_qemu_ld_tl(arg1, arg2, ctx->mem_idx, op);
2951 }
2952
2953 static void gen_qemu_ld32s_i64(DisasContext *ctx, TCGv_i64 val, TCGv addr)
2954 {
2955     TCGv tmp = tcg_temp_new();
2956     gen_qemu_ld32s(ctx, tmp, addr);
2957     tcg_gen_ext_tl_i64(val, tmp);
2958     tcg_temp_free(tmp);
2959 }
2960
2961 static inline void gen_qemu_ld64(DisasContext *ctx, TCGv_i64 arg1, TCGv arg2)
2962 {
2963     TCGMemOp op = MO_Q | ctx->default_tcg_memop_mask;
2964     tcg_gen_qemu_ld_i64(arg1, arg2, ctx->mem_idx, op);
2965 }
2966
2967 static inline void gen_qemu_st8(DisasContext *ctx, TCGv arg1, TCGv arg2)
2968 {
2969     tcg_gen_qemu_st8(arg1, arg2, ctx->mem_idx);
2970 }
2971
2972 static inline void gen_qemu_st16(DisasContext *ctx, TCGv arg1, TCGv arg2)
2973 {
2974     TCGMemOp op = MO_UW | ctx->default_tcg_memop_mask;
2975     tcg_gen_qemu_st_tl(arg1, arg2, ctx->mem_idx, op);
2976 }
2977
2978 static inline void gen_qemu_st32(DisasContext *ctx, TCGv arg1, TCGv arg2)
2979 {
2980     TCGMemOp op = MO_UL | ctx->default_tcg_memop_mask;
2981     tcg_gen_qemu_st_tl(arg1, arg2, ctx->mem_idx, op);
2982 }
2983
2984 static void gen_qemu_st32_i64(DisasContext *ctx, TCGv_i64 val, TCGv addr)
2985 {
2986     TCGv tmp = tcg_temp_new();
2987     tcg_gen_trunc_i64_tl(tmp, val);
2988     gen_qemu_st32(ctx, tmp, addr);
2989     tcg_temp_free(tmp);
2990 }
2991
2992 static inline void gen_qemu_st64(DisasContext *ctx, TCGv_i64 arg1, TCGv arg2)
2993 {
2994     TCGMemOp op = MO_Q | ctx->default_tcg_memop_mask;
2995     tcg_gen_qemu_st_i64(arg1, arg2, ctx->mem_idx, op);
2996 }
2997
2998 #define GEN_LD(name, ldop, opc, type)                                         \
2999 static void glue(gen_, name)(DisasContext *ctx)                                       \
3000 {                                                                             \
3001     TCGv EA;                                                                  \
3002     gen_set_access_type(ctx, ACCESS_INT);                                     \
3003     EA = tcg_temp_new();                                                      \
3004     gen_addr_imm_index(ctx, EA, 0);                                           \
3005     gen_qemu_##ldop(ctx, cpu_gpr[rD(ctx->opcode)], EA);                       \
3006     tcg_temp_free(EA);                                                        \
3007 }
3008
3009 #define GEN_LDU(name, ldop, opc, type)                                        \
3010 static void glue(gen_, name##u)(DisasContext *ctx)                                    \
3011 {                                                                             \
3012     TCGv EA;                                                                  \
3013     if (unlikely(rA(ctx->opcode) == 0 ||                                      \
3014                  rA(ctx->opcode) == rD(ctx->opcode))) {                       \
3015         gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL);                   \
3016         return;                                                               \
3017     }                                                                         \
3018     gen_set_access_type(ctx, ACCESS_INT);                                     \
3019     EA = tcg_temp_new();                                                      \
3020     if (type == PPC_64B)                                                      \
3021         gen_addr_imm_index(ctx, EA, 0x03);                                    \
3022     else                                                                      \
3023         gen_addr_imm_index(ctx, EA, 0);                                       \
3024     gen_qemu_##ldop(ctx, cpu_gpr[rD(ctx->opcode)], EA);                       \
3025     tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], EA);                             \
3026     tcg_temp_free(EA);                                                        \
3027 }
3028
3029 #define GEN_LDUX(name, ldop, opc2, opc3, type)                                \
3030 static void glue(gen_, name##ux)(DisasContext *ctx)                                   \
3031 {                                                                             \
3032     TCGv EA;                                                                  \
3033     if (unlikely(rA(ctx->opcode) == 0 ||                                      \
3034                  rA(ctx->opcode) == rD(ctx->opcode))) {                       \
3035         gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL);                   \
3036         return;                                                               \
3037     }                                                                         \
3038     gen_set_access_type(ctx, ACCESS_INT);                                     \
3039     EA = tcg_temp_new();                                                      \
3040     gen_addr_reg_index(ctx, EA);                                              \
3041     gen_qemu_##ldop(ctx, cpu_gpr[rD(ctx->opcode)], EA);                       \
3042     tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], EA);                             \
3043     tcg_temp_free(EA);                                                        \
3044 }
3045
3046 #define GEN_LDX_E(name, ldop, opc2, opc3, type, type2, chk)                   \
3047 static void glue(gen_, name##x)(DisasContext *ctx)                            \
3048 {                                                                             \
3049     TCGv EA;                                                                  \
3050     chk;                                                                      \
3051     gen_set_access_type(ctx, ACCESS_INT);                                     \
3052     EA = tcg_temp_new();                                                      \
3053     gen_addr_reg_index(ctx, EA);                                              \
3054     gen_qemu_##ldop(ctx, cpu_gpr[rD(ctx->opcode)], EA);                       \
3055     tcg_temp_free(EA);                                                        \
3056 }
3057
3058 #define GEN_LDX(name, ldop, opc2, opc3, type)                                 \
3059     GEN_LDX_E(name, ldop, opc2, opc3, type, PPC_NONE, CHK_NONE)
3060
3061 #define GEN_LDX_HVRM(name, ldop, opc2, opc3, type)                            \
3062     GEN_LDX_E(name, ldop, opc2, opc3, type, PPC_NONE, CHK_HVRM)
3063
3064 #define GEN_LDS(name, ldop, op, type)                                         \
3065 GEN_LD(name, ldop, op | 0x20, type);                                          \
3066 GEN_LDU(name, ldop, op | 0x21, type);                                         \
3067 GEN_LDUX(name, ldop, 0x17, op | 0x01, type);                                  \
3068 GEN_LDX(name, ldop, 0x17, op | 0x00, type)
3069
3070 /* lbz lbzu lbzux lbzx */
3071 GEN_LDS(lbz, ld8u, 0x02, PPC_INTEGER);
3072 /* lha lhau lhaux lhax */
3073 GEN_LDS(lha, ld16s, 0x0A, PPC_INTEGER);
3074 /* lhz lhzu lhzux lhzx */
3075 GEN_LDS(lhz, ld16u, 0x08, PPC_INTEGER);
3076 /* lwz lwzu lwzux lwzx */
3077 GEN_LDS(lwz, ld32u, 0x00, PPC_INTEGER);
3078 #if defined(TARGET_PPC64)
3079 /* lwaux */
3080 GEN_LDUX(lwa, ld32s, 0x15, 0x0B, PPC_64B);
3081 /* lwax */
3082 GEN_LDX(lwa, ld32s, 0x15, 0x0A, PPC_64B);
3083 /* ldux */
3084 GEN_LDUX(ld, ld64, 0x15, 0x01, PPC_64B);
3085 /* ldx */
3086 GEN_LDX(ld, ld64, 0x15, 0x00, PPC_64B);
3087
3088 /* CI load/store variants */
3089 GEN_LDX_HVRM(ldcix, ld64, 0x15, 0x1b, PPC_CILDST)
3090 GEN_LDX_HVRM(lwzcix, ld32u, 0x15, 0x15, PPC_CILDST)
3091 GEN_LDX_HVRM(lhzcix, ld16u, 0x15, 0x19, PPC_CILDST)
3092 GEN_LDX_HVRM(lbzcix, ld8u, 0x15, 0x1a, PPC_CILDST)
3093
3094 static void gen_ld(DisasContext *ctx)
3095 {
3096     TCGv EA;
3097     if (Rc(ctx->opcode)) {
3098         if (unlikely(rA(ctx->opcode) == 0 ||
3099                      rA(ctx->opcode) == rD(ctx->opcode))) {
3100             gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL);
3101             return;
3102         }
3103     }
3104     gen_set_access_type(ctx, ACCESS_INT);
3105     EA = tcg_temp_new();
3106     gen_addr_imm_index(ctx, EA, 0x03);
3107     if (ctx->opcode & 0x02) {
3108         /* lwa (lwau is undefined) */
3109         gen_qemu_ld32s(ctx, cpu_gpr[rD(ctx->opcode)], EA);
3110     } else {
3111         /* ld - ldu */
3112         gen_qemu_ld64(ctx, cpu_gpr[rD(ctx->opcode)], EA);
3113     }
3114     if (Rc(ctx->opcode))
3115         tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], EA);
3116     tcg_temp_free(EA);
3117 }
3118
3119 /* lq */
3120 static void gen_lq(DisasContext *ctx)
3121 {
3122     int ra, rd;
3123     TCGv EA;
3124
3125     /* lq is a legal user mode instruction starting in ISA 2.07 */
3126     bool legal_in_user_mode = (ctx->insns_flags2 & PPC2_LSQ_ISA207) != 0;
3127     bool le_is_supported = (ctx->insns_flags2 & PPC2_LSQ_ISA207) != 0;
3128
3129     if (!legal_in_user_mode && ctx->pr) {
3130         gen_priv_exception(ctx, POWERPC_EXCP_PRIV_OPC);
3131         return;
3132     }
3133
3134     if (!le_is_supported && ctx->le_mode) {
3135         gen_exception_err(ctx, POWERPC_EXCP_ALIGN, POWERPC_EXCP_ALIGN_LE);
3136         return;
3137     }
3138
3139     ra = rA(ctx->opcode);
3140     rd = rD(ctx->opcode);
3141     if (unlikely((rd & 1) || rd == ra)) {
3142         gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL);
3143         return;
3144     }
3145
3146     gen_set_access_type(ctx, ACCESS_INT);
3147     EA = tcg_temp_new();
3148     gen_addr_imm_index(ctx, EA, 0x0F);
3149
3150     /* We only need to swap high and low halves. gen_qemu_ld64 does necessary
3151        64-bit byteswap already. */
3152     if (unlikely(ctx->le_mode)) {
3153         gen_qemu_ld64(ctx, cpu_gpr[rd+1], EA);
3154         gen_addr_add(ctx, EA, EA, 8);
3155         gen_qemu_ld64(ctx, cpu_gpr[rd], EA);
3156     } else {
3157         gen_qemu_ld64(ctx, cpu_gpr[rd], EA);
3158         gen_addr_add(ctx, EA, EA, 8);
3159         gen_qemu_ld64(ctx, cpu_gpr[rd+1], EA);
3160     }
3161     tcg_temp_free(EA);
3162 }
3163 #endif
3164
3165 /***                              Integer store                            ***/
3166 #define GEN_ST(name, stop, opc, type)                                         \
3167 static void glue(gen_, name)(DisasContext *ctx)                                       \
3168 {                                                                             \
3169     TCGv EA;                                                                  \
3170     gen_set_access_type(ctx, ACCESS_INT);                                     \
3171     EA = tcg_temp_new();                                                      \
3172     gen_addr_imm_index(ctx, EA, 0);                                           \
3173     gen_qemu_##stop(ctx, cpu_gpr[rS(ctx->opcode)], EA);                       \
3174     tcg_temp_free(EA);                                                        \
3175 }
3176
3177 #define GEN_STU(name, stop, opc, type)                                        \
3178 static void glue(gen_, stop##u)(DisasContext *ctx)                                    \
3179 {                                                                             \
3180     TCGv EA;                                                                  \
3181     if (unlikely(rA(ctx->opcode) == 0)) {                                     \
3182         gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL);                   \
3183         return;                                                               \
3184     }                                                                         \
3185     gen_set_access_type(ctx, ACCESS_INT);                                     \
3186     EA = tcg_temp_new();                                                      \
3187     if (type == PPC_64B)                                                      \
3188         gen_addr_imm_index(ctx, EA, 0x03);                                    \
3189     else                                                                      \
3190         gen_addr_imm_index(ctx, EA, 0);                                       \
3191     gen_qemu_##stop(ctx, cpu_gpr[rS(ctx->opcode)], EA);                       \
3192     tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], EA);                             \
3193     tcg_temp_free(EA);                                                        \
3194 }
3195
3196 #define GEN_STUX(name, stop, opc2, opc3, type)                                \
3197 static void glue(gen_, name##ux)(DisasContext *ctx)                                   \
3198 {                                                                             \
3199     TCGv EA;                                                                  \
3200     if (unlikely(rA(ctx->opcode) == 0)) {                                     \
3201         gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL);                   \
3202         return;                                                               \
3203     }                                                                         \
3204     gen_set_access_type(ctx, ACCESS_INT);                                     \
3205     EA = tcg_temp_new();                                                      \
3206     gen_addr_reg_index(ctx, EA);                                              \
3207     gen_qemu_##stop(ctx, cpu_gpr[rS(ctx->opcode)], EA);                       \
3208     tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], EA);                             \
3209     tcg_temp_free(EA);                                                        \
3210 }
3211
3212 #define GEN_STX_E(name, stop, opc2, opc3, type, type2, chk)                   \
3213 static void glue(gen_, name##x)(DisasContext *ctx)                            \
3214 {                                                                             \
3215     TCGv EA;                                                                  \
3216     chk;                                                                      \
3217     gen_set_access_type(ctx, ACCESS_INT);                                     \
3218     EA = tcg_temp_new();                                                      \
3219     gen_addr_reg_index(ctx, EA);                                              \
3220     gen_qemu_##stop(ctx, cpu_gpr[rS(ctx->opcode)], EA);                       \
3221     tcg_temp_free(EA);                                                        \
3222 }
3223 #define GEN_STX(name, stop, opc2, opc3, type)                                 \
3224     GEN_STX_E(name, stop, opc2, opc3, type, PPC_NONE, CHK_NONE)
3225
3226 #define GEN_STX_HVRM(name, stop, opc2, opc3, type)                            \
3227     GEN_STX_E(name, stop, opc2, opc3, type, PPC_NONE, CHK_HVRM)
3228
3229 #define GEN_STS(name, stop, op, type)                                         \
3230 GEN_ST(name, stop, op | 0x20, type);                                          \
3231 GEN_STU(name, stop, op | 0x21, type);                                         \
3232 GEN_STUX(name, stop, 0x17, op | 0x01, type);                                  \
3233 GEN_STX(name, stop, 0x17, op | 0x00, type)
3234
3235 /* stb stbu stbux stbx */
3236 GEN_STS(stb, st8, 0x06, PPC_INTEGER);
3237 /* sth sthu sthux sthx */
3238 GEN_STS(sth, st16, 0x0C, PPC_INTEGER);
3239 /* stw stwu stwux stwx */
3240 GEN_STS(stw, st32, 0x04, PPC_INTEGER);
3241 #if defined(TARGET_PPC64)
3242 GEN_STUX(std, st64, 0x15, 0x05, PPC_64B);
3243 GEN_STX(std, st64, 0x15, 0x04, PPC_64B);
3244 GEN_STX_HVRM(stdcix, st64, 0x15, 0x1f, PPC_CILDST)
3245 GEN_STX_HVRM(stwcix, st32, 0x15, 0x1c, PPC_CILDST)
3246 GEN_STX_HVRM(sthcix, st16, 0x15, 0x1d, PPC_CILDST)
3247 GEN_STX_HVRM(stbcix, st8, 0x15, 0x1e, PPC_CILDST)
3248
3249 static void gen_std(DisasContext *ctx)
3250 {
3251     int rs;
3252     TCGv EA;
3253
3254     rs = rS(ctx->opcode);
3255     if ((ctx->opcode & 0x3) == 0x2) { /* stq */
3256         bool legal_in_user_mode = (ctx->insns_flags2 & PPC2_LSQ_ISA207) != 0;
3257         bool le_is_supported = (ctx->insns_flags2 & PPC2_LSQ_ISA207) != 0;
3258
3259         if (!(ctx->insns_flags & PPC_64BX)) {
3260             gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL);
3261         }
3262
3263         if (!legal_in_user_mode && ctx->pr) {
3264             gen_priv_exception(ctx, POWERPC_EXCP_PRIV_OPC);
3265             return;
3266         }
3267
3268         if (!le_is_supported && ctx->le_mode) {
3269             gen_exception_err(ctx, POWERPC_EXCP_ALIGN, POWERPC_EXCP_ALIGN_LE);
3270             return;
3271         }
3272
3273         if (unlikely(rs & 1)) {
3274             gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL);
3275             return;
3276         }
3277         gen_set_access_type(ctx, ACCESS_INT);
3278         EA = tcg_temp_new();
3279         gen_addr_imm_index(ctx, EA, 0x03);
3280
3281         /* We only need to swap high and low halves. gen_qemu_st64 does
3282            necessary 64-bit byteswap already. */
3283         if (unlikely(ctx->le_mode)) {
3284             gen_qemu_st64(ctx, cpu_gpr[rs+1], EA);
3285             gen_addr_add(ctx, EA, EA, 8);
3286             gen_qemu_st64(ctx, cpu_gpr[rs], EA);
3287         } else {
3288             gen_qemu_st64(ctx, cpu_gpr[rs], EA);
3289             gen_addr_add(ctx, EA, EA, 8);
3290             gen_qemu_st64(ctx, cpu_gpr[rs+1], EA);
3291         }
3292         tcg_temp_free(EA);
3293     } else {
3294         /* std / stdu*/
3295         if (Rc(ctx->opcode)) {
3296             if (unlikely(rA(ctx->opcode) == 0)) {
3297                 gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL);
3298                 return;
3299             }
3300         }
3301         gen_set_access_type(ctx, ACCESS_INT);
3302         EA = tcg_temp_new();
3303         gen_addr_imm_index(ctx, EA, 0x03);
3304         gen_qemu_st64(ctx, cpu_gpr[rs], EA);
3305         if (Rc(ctx->opcode))
3306             tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], EA);
3307         tcg_temp_free(EA);
3308     }
3309 }
3310 #endif
3311 /***                Integer load and store with byte reverse               ***/
3312
3313 /* lhbrx */
3314 static inline void gen_qemu_ld16ur(DisasContext *ctx, TCGv arg1, TCGv arg2)
3315 {
3316     TCGMemOp op = MO_UW | (ctx->default_tcg_memop_mask ^ MO_BSWAP);
3317     tcg_gen_qemu_ld_tl(arg1, arg2, ctx->mem_idx, op);
3318 }
3319 GEN_LDX(lhbr, ld16ur, 0x16, 0x18, PPC_INTEGER);
3320
3321 /* lwbrx */
3322 static inline void gen_qemu_ld32ur(DisasContext *ctx, TCGv arg1, TCGv arg2)
3323 {
3324     TCGMemOp op = MO_UL | (ctx->default_tcg_memop_mask ^ MO_BSWAP);
3325     tcg_gen_qemu_ld_tl(arg1, arg2, ctx->mem_idx, op);
3326 }
3327 GEN_LDX(lwbr, ld32ur, 0x16, 0x10, PPC_INTEGER);
3328
3329 #if defined(TARGET_PPC64)
3330 /* ldbrx */
3331 static inline void gen_qemu_ld64ur(DisasContext *ctx, TCGv arg1, TCGv arg2)
3332 {
3333     TCGMemOp op = MO_Q | (ctx->default_tcg_memop_mask ^ MO_BSWAP);
3334     tcg_gen_qemu_ld_i64(arg1, arg2, ctx->mem_idx, op);
3335 }
3336 GEN_LDX_E(ldbr, ld64ur, 0x14, 0x10, PPC_NONE, PPC2_DBRX, CHK_NONE);
3337 #endif  /* TARGET_PPC64 */
3338
3339 /* sthbrx */
3340 static inline void gen_qemu_st16r(DisasContext *ctx, TCGv arg1, TCGv arg2)
3341 {
3342     TCGMemOp op = MO_UW | (ctx->default_tcg_memop_mask ^ MO_BSWAP);
3343     tcg_gen_qemu_st_tl(arg1, arg2, ctx->mem_idx, op);
3344 }
3345 GEN_STX(sthbr, st16r, 0x16, 0x1C, PPC_INTEGER);
3346
3347 /* stwbrx */
3348 static inline void gen_qemu_st32r(DisasContext *ctx, TCGv arg1, TCGv arg2)
3349 {
3350     TCGMemOp op = MO_UL | (ctx->default_tcg_memop_mask ^ MO_BSWAP);
3351     tcg_gen_qemu_st_tl(arg1, arg2, ctx->mem_idx, op);
3352 }
3353 GEN_STX(stwbr, st32r, 0x16, 0x14, PPC_INTEGER);
3354
3355 #if defined(TARGET_PPC64)
3356 /* stdbrx */
3357 static inline void gen_qemu_st64r(DisasContext *ctx, TCGv arg1, TCGv arg2)
3358 {
3359     TCGMemOp op = MO_Q | (ctx->default_tcg_memop_mask ^ MO_BSWAP);
3360     tcg_gen_qemu_st_i64(arg1, arg2, ctx->mem_idx, op);
3361 }
3362 GEN_STX_E(stdbr, st64r, 0x14, 0x14, PPC_NONE, PPC2_DBRX, CHK_NONE);
3363 #endif  /* TARGET_PPC64 */
3364
3365 /***                    Integer load and store multiple                    ***/
3366
3367 /* lmw */
3368 static void gen_lmw(DisasContext *ctx)
3369 {
3370     TCGv t0;
3371     TCGv_i32 t1;
3372     gen_set_access_type(ctx, ACCESS_INT);
3373     /* NIP cannot be restored if the memory exception comes from an helper */
3374     gen_update_nip(ctx, ctx->nip - 4);
3375     t0 = tcg_temp_new();
3376     t1 = tcg_const_i32(rD(ctx->opcode));
3377     gen_addr_imm_index(ctx, t0, 0);
3378     gen_helper_lmw(cpu_env, t0, t1);
3379     tcg_temp_free(t0);
3380     tcg_temp_free_i32(t1);
3381 }
3382
3383 /* stmw */
3384 static void gen_stmw(DisasContext *ctx)
3385 {
3386     TCGv t0;
3387     TCGv_i32 t1;
3388     gen_set_access_type(ctx, ACCESS_INT);
3389     /* NIP cannot be restored if the memory exception comes from an helper */
3390     gen_update_nip(ctx, ctx->nip - 4);
3391     t0 = tcg_temp_new();
3392     t1 = tcg_const_i32(rS(ctx->opcode));
3393     gen_addr_imm_index(ctx, t0, 0);
3394     gen_helper_stmw(cpu_env, t0, t1);
3395     tcg_temp_free(t0);
3396     tcg_temp_free_i32(t1);
3397 }
3398
3399 /***                    Integer load and store strings                     ***/
3400
3401 /* lswi */
3402 /* PowerPC32 specification says we must generate an exception if
3403  * rA is in the range of registers to be loaded.
3404  * In an other hand, IBM says this is valid, but rA won't be loaded.
3405  * For now, I'll follow the spec...
3406  */
3407 static void gen_lswi(DisasContext *ctx)
3408 {
3409     TCGv t0;
3410     TCGv_i32 t1, t2;
3411     int nb = NB(ctx->opcode);
3412     int start = rD(ctx->opcode);
3413     int ra = rA(ctx->opcode);
3414     int nr;
3415
3416     if (nb == 0)
3417         nb = 32;
3418     nr = (nb + 3) / 4;
3419     if (unlikely(lsw_reg_in_range(start, nr, ra))) {
3420         gen_inval_exception(ctx, POWERPC_EXCP_INVAL_LSWX);
3421         return;
3422     }
3423     gen_set_access_type(ctx, ACCESS_INT);
3424     /* NIP cannot be restored if the memory exception comes from an helper */
3425     gen_update_nip(ctx, ctx->nip - 4);
3426     t0 = tcg_temp_new();
3427     gen_addr_register(ctx, t0);
3428     t1 = tcg_const_i32(nb);
3429     t2 = tcg_const_i32(start);
3430     gen_helper_lsw(cpu_env, t0, t1, t2);
3431     tcg_temp_free(t0);
3432     tcg_temp_free_i32(t1);
3433     tcg_temp_free_i32(t2);
3434 }
3435
3436 /* lswx */
3437 static void gen_lswx(DisasContext *ctx)
3438 {
3439     TCGv t0;
3440     TCGv_i32 t1, t2, t3;
3441     gen_set_access_type(ctx, ACCESS_INT);
3442     /* NIP cannot be restored if the memory exception comes from an helper */
3443     gen_update_nip(ctx, ctx->nip - 4);
3444     t0 = tcg_temp_new();
3445     gen_addr_reg_index(ctx, t0);
3446     t1 = tcg_const_i32(rD(ctx->opcode));
3447     t2 = tcg_const_i32(rA(ctx->opcode));
3448     t3 = tcg_const_i32(rB(ctx->opcode));
3449     gen_helper_lswx(cpu_env, t0, t1, t2, t3);
3450     tcg_temp_free(t0);
3451     tcg_temp_free_i32(t1);
3452     tcg_temp_free_i32(t2);
3453     tcg_temp_free_i32(t3);
3454 }
3455
3456 /* stswi */
3457 static void gen_stswi(DisasContext *ctx)
3458 {
3459     TCGv t0;
3460     TCGv_i32 t1, t2;
3461     int nb = NB(ctx->opcode);
3462     gen_set_access_type(ctx, ACCESS_INT);
3463     /* NIP cannot be restored if the memory exception comes from an helper */
3464     gen_update_nip(ctx, ctx->nip - 4);
3465     t0 = tcg_temp_new();
3466     gen_addr_register(ctx, t0);
3467     if (nb == 0)
3468         nb = 32;
3469     t1 = tcg_const_i32(nb);
3470     t2 = tcg_const_i32(rS(ctx->opcode));
3471     gen_helper_stsw(cpu_env, t0, t1, t2);
3472     tcg_temp_free(t0);
3473     tcg_temp_free_i32(t1);
3474     tcg_temp_free_i32(t2);
3475 }
3476
3477 /* stswx */
3478 static void gen_stswx(DisasContext *ctx)
3479 {
3480     TCGv t0;
3481     TCGv_i32 t1, t2;
3482     gen_set_access_type(ctx, ACCESS_INT);
3483     /* NIP cannot be restored if the memory exception comes from an helper */
3484     gen_update_nip(ctx, ctx->nip - 4);
3485     t0 = tcg_temp_new();
3486     gen_addr_reg_index(ctx, t0);
3487     t1 = tcg_temp_new_i32();
3488     tcg_gen_trunc_tl_i32(t1, cpu_xer);
3489     tcg_gen_andi_i32(t1, t1, 0x7F);
3490     t2 = tcg_const_i32(rS(ctx->opcode));
3491     gen_helper_stsw(cpu_env, t0, t1, t2);
3492     tcg_temp_free(t0);
3493     tcg_temp_free_i32(t1);
3494     tcg_temp_free_i32(t2);
3495 }
3496
3497 /***                        Memory synchronisation                         ***/
3498 /* eieio */
3499 static void gen_eieio(DisasContext *ctx)
3500 {
3501 }
3502
3503 #if !defined(CONFIG_USER_ONLY)
3504 static inline void gen_check_tlb_flush(DisasContext *ctx)
3505 {
3506     TCGv_i32 t;
3507     TCGLabel *l;
3508
3509     if (!ctx->lazy_tlb_flush) {
3510         return;
3511     }
3512     l = gen_new_label();
3513     t = tcg_temp_new_i32();
3514     tcg_gen_ld_i32(t, cpu_env, offsetof(CPUPPCState, tlb_need_flush));
3515     tcg_gen_brcondi_i32(TCG_COND_EQ, t, 0, l);
3516     gen_helper_check_tlb_flush(cpu_env);
3517     gen_set_label(l);
3518     tcg_temp_free_i32(t);
3519 }
3520 #else
3521 static inline void gen_check_tlb_flush(DisasContext *ctx) { }
3522 #endif
3523
3524 /* isync */
3525 static void gen_isync(DisasContext *ctx)
3526 {
3527     /*
3528      * We need to check for a pending TLB flush. This can only happen in
3529      * kernel mode however so check MSR_PR
3530      */
3531     if (!ctx->pr) {
3532         gen_check_tlb_flush(ctx);
3533     }
3534     gen_stop_exception(ctx);
3535 }
3536
3537 #define LARX(name, len, loadop)                                      \
3538 static void gen_##name(DisasContext *ctx)                            \
3539 {                                                                    \
3540     TCGv t0;                                                         \
3541     TCGv gpr = cpu_gpr[rD(ctx->opcode)];                             \
3542     gen_set_access_type(ctx, ACCESS_RES);                            \
3543     t0 = tcg_temp_local_new();                                       \
3544     gen_addr_reg_index(ctx, t0);                                     \
3545     if ((len) > 1) {                                                 \
3546         gen_check_align(ctx, t0, (len)-1);                           \
3547     }                                                                \
3548     gen_qemu_##loadop(ctx, gpr, t0);                                 \
3549     tcg_gen_mov_tl(cpu_reserve, t0);                                 \
3550     tcg_gen_st_tl(gpr, cpu_env, offsetof(CPUPPCState, reserve_val)); \
3551     tcg_temp_free(t0);                                               \
3552 }
3553
3554 /* lwarx */
3555 LARX(lbarx, 1, ld8u);
3556 LARX(lharx, 2, ld16u);
3557 LARX(lwarx, 4, ld32u);
3558
3559
3560 #if defined(CONFIG_USER_ONLY)
3561 static void gen_conditional_store(DisasContext *ctx, TCGv EA,
3562                                   int reg, int size)
3563 {
3564     TCGv t0 = tcg_temp_new();
3565     uint32_t save_exception = ctx->exception;
3566
3567     tcg_gen_st_tl(EA, cpu_env, offsetof(CPUPPCState, reserve_ea));
3568     tcg_gen_movi_tl(t0, (size << 5) | reg);
3569     tcg_gen_st_tl(t0, cpu_env, offsetof(CPUPPCState, reserve_info));
3570     tcg_temp_free(t0);
3571     gen_update_nip(ctx, ctx->nip-4);
3572     ctx->exception = POWERPC_EXCP_BRANCH;
3573     gen_exception(ctx, POWERPC_EXCP_STCX);
3574     ctx->exception = save_exception;
3575 }
3576 #else
3577 static void gen_conditional_store(DisasContext *ctx, TCGv EA,
3578                                   int reg, int size)
3579 {
3580     TCGLabel *l1;
3581
3582     tcg_gen_trunc_tl_i32(cpu_crf[0], cpu_so);
3583     l1 = gen_new_label();
3584     tcg_gen_brcond_tl(TCG_COND_NE, EA, cpu_reserve, l1);
3585     tcg_gen_ori_i32(cpu_crf[0], cpu_crf[0], 1 << CRF_EQ);
3586 #if defined(TARGET_PPC64)
3587     if (size == 8) {
3588         gen_qemu_st64(ctx, cpu_gpr[reg], EA);
3589     } else
3590 #endif
3591     if (size == 4) {
3592         gen_qemu_st32(ctx, cpu_gpr[reg], EA);
3593     } else if (size == 2) {
3594         gen_qemu_st16(ctx, cpu_gpr[reg], EA);
3595 #if defined(TARGET_PPC64)
3596     } else if (size == 16) {
3597         TCGv gpr1, gpr2 , EA8;
3598         if (unlikely(ctx->le_mode)) {
3599             gpr1 = cpu_gpr[reg+1];
3600             gpr2 = cpu_gpr[reg];
3601         } else {
3602             gpr1 = cpu_gpr[reg];
3603             gpr2 = cpu_gpr[reg+1];
3604         }
3605         gen_qemu_st64(ctx, gpr1, EA);
3606         EA8 = tcg_temp_local_new();
3607         gen_addr_add(ctx, EA8, EA, 8);
3608         gen_qemu_st64(ctx, gpr2, EA8);
3609         tcg_temp_free(EA8);
3610 #endif
3611     } else {
3612         gen_qemu_st8(ctx, cpu_gpr[reg], EA);
3613     }
3614     gen_set_label(l1);
3615     tcg_gen_movi_tl(cpu_reserve, -1);
3616 }
3617 #endif
3618
3619 #define STCX(name, len)                                   \
3620 static void gen_##name(DisasContext *ctx)                 \
3621 {                                                         \
3622     TCGv t0;                                              \
3623     if (unlikely((len == 16) && (rD(ctx->opcode) & 1))) { \
3624         gen_inval_exception(ctx,                          \
3625                             POWERPC_EXCP_INVAL_INVAL);    \
3626         return;                                           \
3627     }                                                     \
3628     gen_set_access_type(ctx, ACCESS_RES);                 \
3629     t0 = tcg_temp_local_new();                            \
3630     gen_addr_reg_index(ctx, t0);                          \
3631     if (len > 1) {                                        \
3632         gen_check_align(ctx, t0, (len)-1);                \
3633     }                                                     \
3634     gen_conditional_store(ctx, t0, rS(ctx->opcode), len); \
3635     tcg_temp_free(t0);                                    \
3636 }
3637
3638 STCX(stbcx_, 1);
3639 STCX(sthcx_, 2);
3640 STCX(stwcx_, 4);
3641
3642 #if defined(TARGET_PPC64)
3643 /* ldarx */
3644 LARX(ldarx, 8, ld64);
3645
3646 /* lqarx */
3647 static void gen_lqarx(DisasContext *ctx)
3648 {
3649     TCGv EA;
3650     int rd = rD(ctx->opcode);
3651     TCGv gpr1, gpr2;
3652
3653     if (unlikely((rd & 1) || (rd == rA(ctx->opcode)) ||
3654                  (rd == rB(ctx->opcode)))) {
3655         gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL);
3656         return;
3657     }
3658
3659     gen_set_access_type(ctx, ACCESS_RES);
3660     EA = tcg_temp_local_new();
3661     gen_addr_reg_index(ctx, EA);
3662     gen_check_align(ctx, EA, 15);
3663     if (unlikely(ctx->le_mode)) {
3664         gpr1 = cpu_gpr[rd+1];
3665         gpr2 = cpu_gpr[rd];
3666     } else {
3667         gpr1 = cpu_gpr[rd];
3668         gpr2 = cpu_gpr[rd+1];
3669     }
3670     gen_qemu_ld64(ctx, gpr1, EA);
3671     tcg_gen_mov_tl(cpu_reserve, EA);
3672
3673     gen_addr_add(ctx, EA, EA, 8);
3674     gen_qemu_ld64(ctx, gpr2, EA);
3675
3676     tcg_gen_st_tl(gpr1, cpu_env, offsetof(CPUPPCState, reserve_val));
3677     tcg_gen_st_tl(gpr2, cpu_env, offsetof(CPUPPCState, reserve_val2));
3678
3679     tcg_temp_free(EA);
3680 }
3681
3682 /* stdcx. */
3683 STCX(stdcx_, 8);
3684 STCX(stqcx_, 16);
3685 #endif /* defined(TARGET_PPC64) */
3686
3687 /* sync */
3688 static void gen_sync(DisasContext *ctx)
3689 {
3690     uint32_t l = (ctx->opcode >> 21) & 3;
3691
3692     /*
3693      * We may need to check for a pending TLB flush.
3694      *
3695      * We do this on ptesync (l == 2) on ppc64 and any sync pn ppc32.
3696      *
3697      * Additionally, this can only happen in kernel mode however so
3698      * check MSR_PR as well.
3699      */
3700     if (((l == 2) || !(ctx->insns_flags & PPC_64B)) && !ctx->pr) {
3701         gen_check_tlb_flush(ctx);
3702     }
3703 }
3704
3705 /* wait */
3706 static void gen_wait(DisasContext *ctx)
3707 {
3708     TCGv_i32 t0 = tcg_const_i32(1);
3709     tcg_gen_st_i32(t0, cpu_env,
3710                    -offsetof(PowerPCCPU, env) + offsetof(CPUState, halted));
3711     tcg_temp_free_i32(t0);
3712     /* Stop translation, as the CPU is supposed to sleep from now */
3713     gen_exception_err(ctx, EXCP_HLT, 1);
3714 }
3715
3716 #if defined(TARGET_PPC64)
3717 static void gen_doze(DisasContext *ctx)
3718 {
3719 #if defined(CONFIG_USER_ONLY)
3720     GEN_PRIV;
3721 #else
3722     TCGv_i32 t;
3723
3724     CHK_HV;
3725     t = tcg_const_i32(PPC_PM_DOZE);
3726     gen_helper_pminsn(cpu_env, t);
3727     tcg_temp_free_i32(t);
3728     gen_stop_exception(ctx);
3729 #endif /* defined(CONFIG_USER_ONLY) */
3730 }
3731
3732 static void gen_nap(DisasContext *ctx)
3733 {
3734 #if defined(CONFIG_USER_ONLY)
3735     GEN_PRIV;
3736 #else
3737     TCGv_i32 t;
3738
3739     CHK_HV;
3740     t = tcg_const_i32(PPC_PM_NAP);
3741     gen_helper_pminsn(cpu_env, t);
3742     tcg_temp_free_i32(t);
3743     gen_stop_exception(ctx);
3744 #endif /* defined(CONFIG_USER_ONLY) */
3745 }
3746
3747 static void gen_sleep(DisasContext *ctx)
3748 {
3749 #if defined(CONFIG_USER_ONLY)
3750     GEN_PRIV;
3751 #else
3752     TCGv_i32 t;
3753
3754     CHK_HV;
3755     t = tcg_const_i32(PPC_PM_SLEEP);
3756     gen_helper_pminsn(cpu_env, t);
3757     tcg_temp_free_i32(t);
3758     gen_stop_exception(ctx);
3759 #endif /* defined(CONFIG_USER_ONLY) */
3760 }
3761
3762 static void gen_rvwinkle(DisasContext *ctx)
3763 {
3764 #if defined(CONFIG_USER_ONLY)
3765     GEN_PRIV;
3766 #else
3767     TCGv_i32 t;
3768
3769     CHK_HV;
3770     t = tcg_const_i32(PPC_PM_RVWINKLE);
3771     gen_helper_pminsn(cpu_env, t);
3772     tcg_temp_free_i32(t);
3773     gen_stop_exception(ctx);
3774 #endif /* defined(CONFIG_USER_ONLY) */
3775 }
3776 #endif /* #if defined(TARGET_PPC64) */
3777
3778 /***                         Floating-point load                           ***/
3779 #define GEN_LDF(name, ldop, opc, type)                                        \
3780 static void glue(gen_, name)(DisasContext *ctx)                                       \
3781 {                                                                             \
3782     TCGv EA;                                                                  \
3783     if (unlikely(!ctx->fpu_enabled)) {                                        \
3784         gen_exception(ctx, POWERPC_EXCP_FPU);                                 \
3785         return;                                                               \
3786     }                                                                         \
3787     gen_set_access_type(ctx, ACCESS_FLOAT);                                   \
3788     EA = tcg_temp_new();                                                      \
3789     gen_addr_imm_index(ctx, EA, 0);                                           \
3790     gen_qemu_##ldop(ctx, cpu_fpr[rD(ctx->opcode)], EA);                       \
3791     tcg_temp_free(EA);                                                        \
3792 }
3793
3794 #define GEN_LDUF(name, ldop, opc, type)                                       \
3795 static void glue(gen_, name##u)(DisasContext *ctx)                                    \
3796 {                                                                             \
3797     TCGv EA;                                                                  \
3798     if (unlikely(!ctx->fpu_enabled)) {                                        \
3799         gen_exception(ctx, POWERPC_EXCP_FPU);                                 \
3800         return;                                                               \
3801     }                                                                         \
3802     if (unlikely(rA(ctx->opcode) == 0)) {                                     \
3803         gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL);                   \
3804         return;                                                               \
3805     }                                                                         \
3806     gen_set_access_type(ctx, ACCESS_FLOAT);                                   \
3807     EA = tcg_temp_new();                                                      \
3808     gen_addr_imm_index(ctx, EA, 0);                                           \
3809     gen_qemu_##ldop(ctx, cpu_fpr[rD(ctx->opcode)], EA);                       \
3810     tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], EA);                             \
3811     tcg_temp_free(EA);                                                        \
3812 }
3813
3814 #define GEN_LDUXF(name, ldop, opc, type)                                      \
3815 static void glue(gen_, name##ux)(DisasContext *ctx)                                   \
3816 {                                                                             \
3817     TCGv EA;                                                                  \
3818     if (unlikely(!ctx->fpu_enabled)) {                                        \
3819         gen_exception(ctx, POWERPC_EXCP_FPU);                                 \
3820         return;                                                               \
3821     }                                                                         \
3822     if (unlikely(rA(ctx->opcode) == 0)) {                                     \
3823         gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL);                   \
3824         return;                                                               \
3825     }                                                                         \
3826     gen_set_access_type(ctx, ACCESS_FLOAT);                                   \
3827     EA = tcg_temp_new();                                                      \
3828     gen_addr_reg_index(ctx, EA);                                              \
3829     gen_qemu_##ldop(ctx, cpu_fpr[rD(ctx->opcode)], EA);                       \
3830     tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], EA);                             \
3831     tcg_temp_free(EA);                                                        \
3832 }
3833
3834 #define GEN_LDXF(name, ldop, opc2, opc3, type)                                \
3835 static void glue(gen_, name##x)(DisasContext *ctx)                                    \
3836 {                                                                             \
3837     TCGv EA;                                                                  \
3838     if (unlikely(!ctx->fpu_enabled)) {                                        \
3839         gen_exception(ctx, POWERPC_EXCP_FPU);                                 \
3840         return;                                                               \
3841     }                                                                         \
3842     gen_set_access_type(ctx, ACCESS_FLOAT);                                   \
3843     EA = tcg_temp_new();                                                      \
3844     gen_addr_reg_index(ctx, EA);                                              \
3845     gen_qemu_##ldop(ctx, cpu_fpr[rD(ctx->opcode)], EA);                       \
3846     tcg_temp_free(EA);                                                        \
3847 }
3848
3849 #define GEN_LDFS(name, ldop, op, type)                                        \
3850 GEN_LDF(name, ldop, op | 0x20, type);                                         \
3851 GEN_LDUF(name, ldop, op | 0x21, type);                                        \
3852 GEN_LDUXF(name, ldop, op | 0x01, type);                                       \
3853 GEN_LDXF(name, ldop, 0x17, op | 0x00, type)
3854
3855 static inline void gen_qemu_ld32fs(DisasContext *ctx, TCGv_i64 arg1, TCGv arg2)
3856 {
3857     TCGv t0 = tcg_temp_new();
3858     TCGv_i32 t1 = tcg_temp_new_i32();
3859     gen_qemu_ld32u(ctx, t0, arg2);
3860     tcg_gen_trunc_tl_i32(t1, t0);
3861     tcg_temp_free(t0);
3862     gen_helper_float32_to_float64(arg1, cpu_env, t1);
3863     tcg_temp_free_i32(t1);
3864 }
3865
3866  /* lfd lfdu lfdux lfdx */
3867 GEN_LDFS(lfd, ld64, 0x12, PPC_FLOAT);
3868  /* lfs lfsu lfsux lfsx */
3869 GEN_LDFS(lfs, ld32fs, 0x10, PPC_FLOAT);
3870
3871 /* lfdp */
3872 static void gen_lfdp(DisasContext *ctx)
3873 {
3874     TCGv EA;
3875     if (unlikely(!ctx->fpu_enabled)) {
3876         gen_exception(ctx, POWERPC_EXCP_FPU);
3877         return;
3878     }
3879     gen_set_access_type(ctx, ACCESS_FLOAT);
3880     EA = tcg_temp_new();
3881     gen_addr_imm_index(ctx, EA, 0);
3882     /* We only need to swap high and low halves. gen_qemu_ld64 does necessary
3883        64-bit byteswap already. */
3884     if (unlikely(ctx->le_mode)) {
3885         gen_qemu_ld64(ctx, cpu_fpr[rD(ctx->opcode) + 1], EA);
3886         tcg_gen_addi_tl(EA, EA, 8);
3887         gen_qemu_ld64(ctx, cpu_fpr[rD(ctx->opcode)], EA);
3888     } else {
3889         gen_qemu_ld64(ctx, cpu_fpr[rD(ctx->opcode)], EA);
3890         tcg_gen_addi_tl(EA, EA, 8);
3891         gen_qemu_ld64(ctx, cpu_fpr[rD(ctx->opcode) + 1], EA);
3892     }
3893     tcg_temp_free(EA);
3894 }
3895
3896 /* lfdpx */
3897 static void gen_lfdpx(DisasContext *ctx)
3898 {
3899     TCGv EA;
3900     if (unlikely(!ctx->fpu_enabled)) {
3901         gen_exception(ctx, POWERPC_EXCP_FPU);
3902         return;
3903     }
3904     gen_set_access_type(ctx, ACCESS_FLOAT);
3905     EA = tcg_temp_new();
3906     gen_addr_reg_index(ctx, EA);
3907     /* We only need to swap high and low halves. gen_qemu_ld64 does necessary
3908        64-bit byteswap already. */
3909     if (unlikely(ctx->le_mode)) {
3910         gen_qemu_ld64(ctx, cpu_fpr[rD(ctx->opcode) + 1], EA);
3911         tcg_gen_addi_tl(EA, EA, 8);
3912         gen_qemu_ld64(ctx, cpu_fpr[rD(ctx->opcode)], EA);
3913     } else {
3914         gen_qemu_ld64(ctx, cpu_fpr[rD(ctx->opcode)], EA);
3915         tcg_gen_addi_tl(EA, EA, 8);
3916         gen_qemu_ld64(ctx, cpu_fpr[rD(ctx->opcode) + 1], EA);
3917     }
3918     tcg_temp_free(EA);
3919 }
3920
3921 /* lfiwax */
3922 static void gen_lfiwax(DisasContext *ctx)
3923 {
3924     TCGv EA;
3925     TCGv t0;
3926     if (unlikely(!ctx->fpu_enabled)) {
3927         gen_exception(ctx, POWERPC_EXCP_FPU);
3928         return;
3929     }
3930     gen_set_access_type(ctx, ACCESS_FLOAT);
3931     EA = tcg_temp_new();
3932     t0 = tcg_temp_new();
3933     gen_addr_reg_index(ctx, EA);
3934     gen_qemu_ld32s(ctx, t0, EA);
3935     tcg_gen_ext_tl_i64(cpu_fpr[rD(ctx->opcode)], t0);
3936     tcg_temp_free(EA);
3937     tcg_temp_free(t0);
3938 }
3939
3940 /* lfiwzx */
3941 static void gen_lfiwzx(DisasContext *ctx)
3942 {
3943     TCGv EA;
3944     if (unlikely(!ctx->fpu_enabled)) {
3945         gen_exception(ctx, POWERPC_EXCP_FPU);
3946         return;
3947     }
3948     gen_set_access_type(ctx, ACCESS_FLOAT);
3949     EA = tcg_temp_new();
3950     gen_addr_reg_index(ctx, EA);
3951     gen_qemu_ld32u_i64(ctx, cpu_fpr[rD(ctx->opcode)], EA);
3952     tcg_temp_free(EA);
3953 }
3954 /***                         Floating-point store                          ***/
3955 #define GEN_STF(name, stop, opc, type)                                        \
3956 static void glue(gen_, name)(DisasContext *ctx)                                       \
3957 {                                                                             \
3958     TCGv EA;                                                                  \
3959     if (unlikely(!ctx->fpu_enabled)) {                                        \
3960         gen_exception(ctx, POWERPC_EXCP_FPU);                                 \
3961         return;                                                               \
3962     }                                                                         \
3963     gen_set_access_type(ctx, ACCESS_FLOAT);                                   \
3964     EA = tcg_temp_new();                                                      \
3965     gen_addr_imm_index(ctx, EA, 0);                                           \
3966     gen_qemu_##stop(ctx, cpu_fpr[rS(ctx->opcode)], EA);                       \
3967     tcg_temp_free(EA);                                                        \
3968 }
3969
3970 #define GEN_STUF(name, stop, opc, type)                                       \
3971 static void glue(gen_, name##u)(DisasContext *ctx)                                    \
3972 {                                                                             \
3973     TCGv EA;                                                                  \
3974     if (unlikely(!ctx->fpu_enabled)) {                                        \
3975         gen_exception(ctx, POWERPC_EXCP_FPU);                                 \
3976         return;                                                               \
3977     }                                                                         \
3978     if (unlikely(rA(ctx->opcode) == 0)) {                                     \
3979         gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL);                   \
3980         return;                                                               \
3981     }                                                                         \
3982     gen_set_access_type(ctx, ACCESS_FLOAT);                                   \
3983     EA = tcg_temp_new();                                                      \
3984     gen_addr_imm_index(ctx, EA, 0);                                           \
3985     gen_qemu_##stop(ctx, cpu_fpr[rS(ctx->opcode)], EA);                       \
3986     tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], EA);                             \
3987     tcg_temp_free(EA);                                                        \
3988 }
3989
3990 #define GEN_STUXF(name, stop, opc, type)                                      \
3991 static void glue(gen_, name##ux)(DisasContext *ctx)                                   \
3992 {                                                                             \
3993     TCGv EA;                                                                  \
3994     if (unlikely(!ctx->fpu_enabled)) {                                        \
3995         gen_exception(ctx, POWERPC_EXCP_FPU);                                 \
3996         return;                                                               \
3997     }                                                                         \
3998     if (unlikely(rA(ctx->opcode) == 0)) {                                     \
3999         gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL);                   \
4000         return;                                                               \
4001     }                                                                         \
4002     gen_set_access_type(ctx, ACCESS_FLOAT);                                   \
4003     EA = tcg_temp_new();                                                      \
4004     gen_addr_reg_index(ctx, EA);                                              \
4005     gen_qemu_##stop(ctx, cpu_fpr[rS(ctx->opcode)], EA);                       \
4006     tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], EA);                             \
4007     tcg_temp_free(EA);                                                        \
4008 }
4009
4010 #define GEN_STXF(name, stop, opc2, opc3, type)                                \
4011 static void glue(gen_, name##x)(DisasContext *ctx)                                    \
4012 {                                                                             \
4013     TCGv EA;                                                                  \
4014     if (unlikely(!ctx->fpu_enabled)) {                                        \
4015         gen_exception(ctx, POWERPC_EXCP_FPU);                                 \
4016         return;                                                               \
4017     }                                                                         \
4018     gen_set_access_type(ctx, ACCESS_FLOAT);                                   \
4019     EA = tcg_temp_new();                                                      \
4020     gen_addr_reg_index(ctx, EA);                                              \
4021     gen_qemu_##stop(ctx, cpu_fpr[rS(ctx->opcode)], EA);                       \
4022     tcg_temp_free(EA);                                                        \
4023 }
4024
4025 #define GEN_STFS(name, stop, op, type)                                        \
4026 GEN_STF(name, stop, op | 0x20, type);                                         \
4027 GEN_STUF(name, stop, op | 0x21, type);                                        \
4028 GEN_STUXF(name, stop, op | 0x01, type);                                       \
4029 GEN_STXF(name, stop, 0x17, op | 0x00, type)
4030
4031 static inline void gen_qemu_st32fs(DisasContext *ctx, TCGv_i64 arg1, TCGv arg2)
4032 {
4033     TCGv_i32 t0 = tcg_temp_new_i32();
4034     TCGv t1 = tcg_temp_new();
4035     gen_helper_float64_to_float32(t0, cpu_env, arg1);
4036     tcg_gen_extu_i32_tl(t1, t0);
4037     tcg_temp_free_i32(t0);
4038     gen_qemu_st32(ctx, t1, arg2);
4039     tcg_temp_free(t1);
4040 }
4041
4042 /* stfd stfdu stfdux stfdx */
4043 GEN_STFS(stfd, st64, 0x16, PPC_FLOAT);
4044 /* stfs stfsu stfsux stfsx */
4045 GEN_STFS(stfs, st32fs, 0x14, PPC_FLOAT);
4046
4047 /* stfdp */
4048 static void gen_stfdp(DisasContext *ctx)
4049 {
4050     TCGv EA;
4051     if (unlikely(!ctx->fpu_enabled)) {
4052         gen_exception(ctx, POWERPC_EXCP_FPU);
4053         return;
4054     }
4055     gen_set_access_type(ctx, ACCESS_FLOAT);
4056     EA = tcg_temp_new();
4057     gen_addr_imm_index(ctx, EA, 0);
4058     /* We only need to swap high and low halves. gen_qemu_st64 does necessary
4059        64-bit byteswap already. */
4060     if (unlikely(ctx->le_mode)) {
4061         gen_qemu_st64(ctx, cpu_fpr[rD(ctx->opcode) + 1], EA);
4062         tcg_gen_addi_tl(EA, EA, 8);
4063         gen_qemu_st64(ctx, cpu_fpr[rD(ctx->opcode)], EA);
4064     } else {
4065         gen_qemu_st64(ctx, cpu_fpr[rD(ctx->opcode)], EA);
4066         tcg_gen_addi_tl(EA, EA, 8);
4067         gen_qemu_st64(ctx, cpu_fpr[rD(ctx->opcode) + 1], EA);
4068     }
4069     tcg_temp_free(EA);
4070 }
4071
4072 /* stfdpx */
4073 static void gen_stfdpx(DisasContext *ctx)
4074 {
4075     TCGv EA;
4076     if (unlikely(!ctx->fpu_enabled)) {
4077         gen_exception(ctx, POWERPC_EXCP_FPU);
4078         return;
4079     }
4080     gen_set_access_type(ctx, ACCESS_FLOAT);
4081     EA = tcg_temp_new();
4082     gen_addr_reg_index(ctx, EA);
4083     /* We only need to swap high and low halves. gen_qemu_st64 does necessary
4084        64-bit byteswap already. */
4085     if (unlikely(ctx->le_mode)) {
4086         gen_qemu_st64(ctx, cpu_fpr[rD(ctx->opcode) + 1], EA);
4087         tcg_gen_addi_tl(EA, EA, 8);
4088         gen_qemu_st64(ctx, cpu_fpr[rD(ctx->opcode)], EA);
4089     } else {
4090         gen_qemu_st64(ctx, cpu_fpr[rD(ctx->opcode)], EA);
4091         tcg_gen_addi_tl(EA, EA, 8);
4092         gen_qemu_st64(ctx, cpu_fpr[rD(ctx->opcode) + 1], EA);
4093     }
4094     tcg_temp_free(EA);
4095 }
4096
4097 /* Optional: */
4098 static inline void gen_qemu_st32fiw(DisasContext *ctx, TCGv_i64 arg1, TCGv arg2)
4099 {
4100     TCGv t0 = tcg_temp_new();
4101     tcg_gen_trunc_i64_tl(t0, arg1),
4102     gen_qemu_st32(ctx, t0, arg2);
4103     tcg_temp_free(t0);
4104 }
4105 /* stfiwx */
4106 GEN_STXF(stfiw, st32fiw, 0x17, 0x1E, PPC_FLOAT_STFIWX);
4107
4108 static inline void gen_update_cfar(DisasContext *ctx, target_ulong nip)
4109 {
4110 #if defined(TARGET_PPC64)
4111     if (ctx->has_cfar)
4112         tcg_gen_movi_tl(cpu_cfar, nip);
4113 #endif
4114 }
4115
4116 static inline bool use_goto_tb(DisasContext *ctx, target_ulong dest)
4117 {
4118     if (unlikely(ctx->singlestep_enabled)) {
4119         return false;
4120     }
4121
4122 #ifndef CONFIG_USER_ONLY
4123     return (ctx->tb->pc & TARGET_PAGE_MASK) == (dest & TARGET_PAGE_MASK);
4124 #else
4125     return true;
4126 #endif
4127 }
4128
4129 /***                                Branch                                 ***/
4130 static inline void gen_goto_tb(DisasContext *ctx, int n, target_ulong dest)
4131 {
4132     if (NARROW_MODE(ctx)) {
4133         dest = (uint32_t) dest;
4134     }
4135     if (use_goto_tb(ctx, dest)) {
4136         tcg_gen_goto_tb(n);
4137         tcg_gen_movi_tl(cpu_nip, dest & ~3);
4138         tcg_gen_exit_tb((uintptr_t)ctx->tb + n);
4139     } else {
4140         tcg_gen_movi_tl(cpu_nip, dest & ~3);
4141         if (unlikely(ctx->singlestep_enabled)) {
4142             if ((ctx->singlestep_enabled &
4143                 (CPU_BRANCH_STEP | CPU_SINGLE_STEP)) &&
4144                 (ctx->exception == POWERPC_EXCP_BRANCH ||
4145                  ctx->exception == POWERPC_EXCP_TRACE)) {
4146                 target_ulong tmp = ctx->nip;
4147                 ctx->nip = dest;
4148                 gen_exception(ctx, POWERPC_EXCP_TRACE);
4149                 ctx->nip = tmp;
4150             }
4151             if (ctx->singlestep_enabled & GDBSTUB_SINGLE_STEP) {
4152                 gen_debug_exception(ctx);
4153             }
4154         }
4155         tcg_gen_exit_tb(0);
4156     }
4157 }
4158
4159 static inline void gen_setlr(DisasContext *ctx, target_ulong nip)
4160 {
4161     if (NARROW_MODE(ctx)) {
4162         nip = (uint32_t)nip;
4163     }
4164     tcg_gen_movi_tl(cpu_lr, nip);
4165 }
4166
4167 /* b ba bl bla */
4168 static void gen_b(DisasContext *ctx)
4169 {
4170     target_ulong li, target;
4171
4172     ctx->exception = POWERPC_EXCP_BRANCH;
4173     /* sign extend LI */
4174     li = LI(ctx->opcode);
4175     li = (li ^ 0x02000000) - 0x02000000;
4176     if (likely(AA(ctx->opcode) == 0)) {
4177         target = ctx->nip + li - 4;
4178     } else {
4179         target = li;
4180     }
4181     if (LK(ctx->opcode)) {
4182         gen_setlr(ctx, ctx->nip);
4183     }
4184     gen_update_cfar(ctx, ctx->nip);
4185     gen_goto_tb(ctx, 0, target);
4186 }
4187
4188 #define BCOND_IM  0
4189 #define BCOND_LR  1
4190 #define BCOND_CTR 2
4191 #define BCOND_TAR 3
4192
4193 static inline void gen_bcond(DisasContext *ctx, int type)
4194 {
4195     uint32_t bo = BO(ctx->opcode);
4196     TCGLabel *l1;
4197     TCGv target;
4198
4199     ctx->exception = POWERPC_EXCP_BRANCH;
4200     if (type == BCOND_LR || type == BCOND_CTR || type == BCOND_TAR) {
4201         target = tcg_temp_local_new();
4202         if (type == BCOND_CTR)
4203             tcg_gen_mov_tl(target, cpu_ctr);
4204         else if (type == BCOND_TAR)
4205             gen_load_spr(target, SPR_TAR);
4206         else
4207             tcg_gen_mov_tl(target, cpu_lr);
4208     } else {
4209         TCGV_UNUSED(target);
4210     }
4211     if (LK(ctx->opcode))
4212         gen_setlr(ctx, ctx->nip);
4213     l1 = gen_new_label();
4214     if ((bo & 0x4) == 0) {
4215         /* Decrement and test CTR */
4216         TCGv temp = tcg_temp_new();
4217         if (unlikely(type == BCOND_CTR)) {
4218             gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL);
4219             return;
4220         }
4221         tcg_gen_subi_tl(cpu_ctr, cpu_ctr, 1);
4222         if (NARROW_MODE(ctx)) {
4223             tcg_gen_ext32u_tl(temp, cpu_ctr);
4224         } else {
4225             tcg_gen_mov_tl(temp, cpu_ctr);
4226         }
4227         if (bo & 0x2) {
4228             tcg_gen_brcondi_tl(TCG_COND_NE, temp, 0, l1);
4229         } else {
4230             tcg_gen_brcondi_tl(TCG_COND_EQ, temp, 0, l1);
4231         }
4232         tcg_temp_free(temp);
4233     }
4234     if ((bo & 0x10) == 0) {
4235         /* Test CR */
4236         uint32_t bi = BI(ctx->opcode);
4237         uint32_t mask = 0x08 >> (bi & 0x03);
4238         TCGv_i32 temp = tcg_temp_new_i32();
4239
4240         if (bo & 0x8) {
4241             tcg_gen_andi_i32(temp, cpu_crf[bi >> 2], mask);
4242             tcg_gen_brcondi_i32(TCG_COND_EQ, temp, 0, l1);
4243         } else {
4244             tcg_gen_andi_i32(temp, cpu_crf[bi >> 2], mask);
4245             tcg_gen_brcondi_i32(TCG_COND_NE, temp, 0, l1);
4246         }
4247         tcg_temp_free_i32(temp);
4248     }
4249     gen_update_cfar(ctx, ctx->nip);
4250     if (type == BCOND_IM) {
4251         target_ulong li = (target_long)((int16_t)(BD(ctx->opcode)));
4252         if (likely(AA(ctx->opcode) == 0)) {
4253             gen_goto_tb(ctx, 0, ctx->nip + li - 4);
4254         } else {
4255             gen_goto_tb(ctx, 0, li);
4256         }
4257         gen_set_label(l1);
4258         gen_goto_tb(ctx, 1, ctx->nip);
4259     } else {
4260         if (NARROW_MODE(ctx)) {
4261             tcg_gen_andi_tl(cpu_nip, target, (uint32_t)~3);
4262         } else {
4263             tcg_gen_andi_tl(cpu_nip, target, ~3);
4264         }
4265         tcg_gen_exit_tb(0);
4266         gen_set_label(l1);
4267         gen_update_nip(ctx, ctx->nip);
4268         tcg_gen_exit_tb(0);
4269     }
4270     if (type == BCOND_LR || type == BCOND_CTR || type == BCOND_TAR) {
4271         tcg_temp_free(target);
4272     }
4273 }
4274
4275 static void gen_bc(DisasContext *ctx)
4276 {
4277     gen_bcond(ctx, BCOND_IM);
4278 }
4279
4280 static void gen_bcctr(DisasContext *ctx)
4281 {
4282     gen_bcond(ctx, BCOND_CTR);
4283 }
4284
4285 static void gen_bclr(DisasContext *ctx)
4286 {
4287     gen_bcond(ctx, BCOND_LR);
4288 }
4289
4290 static void gen_bctar(DisasContext *ctx)
4291 {
4292     gen_bcond(ctx, BCOND_TAR);
4293 }
4294
4295 /***                      Condition register logical                       ***/
4296 #define GEN_CRLOGIC(name, tcg_op, opc)                                        \
4297 static void glue(gen_, name)(DisasContext *ctx)                                       \
4298 {                                                                             \
4299     uint8_t bitmask;                                                          \
4300     int sh;                                                                   \
4301     TCGv_i32 t0, t1;                                                          \
4302     sh = (crbD(ctx->opcode) & 0x03) - (crbA(ctx->opcode) & 0x03);             \
4303     t0 = tcg_temp_new_i32();                                                  \
4304     if (sh > 0)                                                               \
4305         tcg_gen_shri_i32(t0, cpu_crf[crbA(ctx->opcode) >> 2], sh);            \
4306     else if (sh < 0)                                                          \
4307         tcg_gen_shli_i32(t0, cpu_crf[crbA(ctx->opcode) >> 2], -sh);           \
4308     else                                                                      \
4309         tcg_gen_mov_i32(t0, cpu_crf[crbA(ctx->opcode) >> 2]);                 \
4310     t1 = tcg_temp_new_i32();                                                  \
4311     sh = (crbD(ctx->opcode) & 0x03) - (crbB(ctx->opcode) & 0x03);             \
4312     if (sh > 0)                                                               \
4313         tcg_gen_shri_i32(t1, cpu_crf[crbB(ctx->opcode) >> 2], sh);            \
4314     else if (sh < 0)                                                          \
4315         tcg_gen_shli_i32(t1, cpu_crf[crbB(ctx->opcode) >> 2], -sh);           \
4316     else                                                                      \
4317         tcg_gen_mov_i32(t1, cpu_crf[crbB(ctx->opcode) >> 2]);                 \
4318     tcg_op(t0, t0, t1);                                                       \
4319     bitmask = 0x08 >> (crbD(ctx->opcode) & 0x03);                             \
4320     tcg_gen_andi_i32(t0, t0, bitmask);                                        \
4321     tcg_gen_andi_i32(t1, cpu_crf[crbD(ctx->opcode) >> 2], ~bitmask);          \
4322     tcg_gen_or_i32(cpu_crf[crbD(ctx->opcode) >> 2], t0, t1);                  \
4323     tcg_temp_free_i32(t0);                                                    \
4324     tcg_temp_free_i32(t1);                                                    \
4325 }
4326
4327 /* crand */
4328 GEN_CRLOGIC(crand, tcg_gen_and_i32, 0x08);
4329 /* crandc */
4330 GEN_CRLOGIC(crandc, tcg_gen_andc_i32, 0x04);
4331 /* creqv */
4332 GEN_CRLOGIC(creqv, tcg_gen_eqv_i32, 0x09);
4333 /* crnand */
4334 GEN_CRLOGIC(crnand, tcg_gen_nand_i32, 0x07);
4335 /* crnor */
4336 GEN_CRLOGIC(crnor, tcg_gen_nor_i32, 0x01);
4337 /* cror */
4338 GEN_CRLOGIC(cror, tcg_gen_or_i32, 0x0E);
4339 /* crorc */
4340 GEN_CRLOGIC(crorc, tcg_gen_orc_i32, 0x0D);
4341 /* crxor */
4342 GEN_CRLOGIC(crxor, tcg_gen_xor_i32, 0x06);
4343
4344 /* mcrf */
4345 static void gen_mcrf(DisasContext *ctx)
4346 {
4347     tcg_gen_mov_i32(cpu_crf[crfD(ctx->opcode)], cpu_crf[crfS(ctx->opcode)]);
4348 }
4349
4350 /***                           System linkage                              ***/
4351
4352 /* rfi (supervisor only) */
4353 static void gen_rfi(DisasContext *ctx)
4354 {
4355 #if defined(CONFIG_USER_ONLY)
4356     GEN_PRIV;
4357 #else
4358     /* FIXME: This instruction doesn't exist anymore on 64-bit server
4359      * processors compliant with arch 2.x, we should remove it there,
4360      * but we need to fix OpenBIOS not to use it on 970 first
4361      */
4362     /* Restore CPU state */
4363     CHK_SV;
4364     gen_update_cfar(ctx, ctx->nip);
4365     gen_helper_rfi(cpu_env);
4366     gen_sync_exception(ctx);
4367 #endif
4368 }
4369
4370 #if defined(TARGET_PPC64)
4371 static void gen_rfid(DisasContext *ctx)
4372 {
4373 #if defined(CONFIG_USER_ONLY)
4374     GEN_PRIV;
4375 #else
4376     /* Restore CPU state */
4377     CHK_SV;
4378     gen_update_cfar(ctx, ctx->nip);
4379     gen_helper_rfid(cpu_env);
4380     gen_sync_exception(ctx);
4381 #endif
4382 }
4383
4384 static void gen_hrfid(DisasContext *ctx)
4385 {
4386 #if defined(CONFIG_USER_ONLY)
4387     GEN_PRIV;
4388 #else
4389     /* Restore CPU state */
4390     CHK_HV;
4391     gen_helper_hrfid(cpu_env);
4392     gen_sync_exception(ctx);
4393 #endif
4394 }
4395 #endif
4396
4397 /* sc */
4398 #if defined(CONFIG_USER_ONLY)
4399 #define POWERPC_SYSCALL POWERPC_EXCP_SYSCALL_USER
4400 #else
4401 #define POWERPC_SYSCALL POWERPC_EXCP_SYSCALL
4402 #endif
4403 static void gen_sc(DisasContext *ctx)
4404 {
4405     uint32_t lev;
4406
4407     lev = (ctx->opcode >> 5) & 0x7F;
4408     gen_exception_err(ctx, POWERPC_SYSCALL, lev);
4409 }
4410
4411 /***                                Trap                                   ***/
4412
4413 /* tw */
4414 static void gen_tw(DisasContext *ctx)
4415 {
4416     TCGv_i32 t0 = tcg_const_i32(TO(ctx->opcode));
4417     /* Update the nip since this might generate a trap exception */
4418     gen_update_nip(ctx, ctx->nip);
4419     gen_helper_tw(cpu_env, cpu_gpr[rA(ctx->opcode)], cpu_gpr[rB(ctx->opcode)],
4420                   t0);
4421     tcg_temp_free_i32(t0);
4422 }
4423
4424 /* twi */
4425 static void gen_twi(DisasContext *ctx)
4426 {
4427     TCGv t0 = tcg_const_tl(SIMM(ctx->opcode));
4428     TCGv_i32 t1 = tcg_const_i32(TO(ctx->opcode));
4429     /* Update the nip since this might generate a trap exception */
4430     gen_update_nip(ctx, ctx->nip);
4431     gen_helper_tw(cpu_env, cpu_gpr[rA(ctx->opcode)], t0, t1);
4432     tcg_temp_free(t0);
4433     tcg_temp_free_i32(t1);
4434 }
4435
4436 #if defined(TARGET_PPC64)
4437 /* td */
4438 static void gen_td(DisasContext *ctx)
4439 {
4440     TCGv_i32 t0 = tcg_const_i32(TO(ctx->opcode));
4441     /* Update the nip since this might generate a trap exception */
4442     gen_update_nip(ctx, ctx->nip);
4443     gen_helper_td(cpu_env, cpu_gpr[rA(ctx->opcode)], cpu_gpr[rB(ctx->opcode)],
4444                   t0);
4445     tcg_temp_free_i32(t0);
4446 }
4447
4448 /* tdi */
4449 static void gen_tdi(DisasContext *ctx)
4450 {
4451     TCGv t0 = tcg_const_tl(SIMM(ctx->opcode));
4452     TCGv_i32 t1 = tcg_const_i32(TO(ctx->opcode));
4453     /* Update the nip since this might generate a trap exception */
4454     gen_update_nip(ctx, ctx->nip);
4455     gen_helper_td(cpu_env, cpu_gpr[rA(ctx->opcode)], t0, t1);
4456     tcg_temp_free(t0);
4457     tcg_temp_free_i32(t1);
4458 }
4459 #endif
4460
4461 /***                          Processor control                            ***/
4462
4463 static void gen_read_xer(TCGv dst)
4464 {
4465     TCGv t0 = tcg_temp_new();
4466     TCGv t1 = tcg_temp_new();
4467     TCGv t2 = tcg_temp_new();
4468     tcg_gen_mov_tl(dst, cpu_xer);
4469     tcg_gen_shli_tl(t0, cpu_so, XER_SO);
4470     tcg_gen_shli_tl(t1, cpu_ov, XER_OV);
4471     tcg_gen_shli_tl(t2, cpu_ca, XER_CA);
4472     tcg_gen_or_tl(t0, t0, t1);
4473     tcg_gen_or_tl(dst, dst, t2);
4474     tcg_gen_or_tl(dst, dst, t0);
4475     tcg_temp_free(t0);
4476     tcg_temp_free(t1);
4477     tcg_temp_free(t2);
4478 }
4479
4480 static void gen_write_xer(TCGv src)
4481 {
4482     tcg_gen_andi_tl(cpu_xer, src,
4483                     ~((1u << XER_SO) | (1u << XER_OV) | (1u << XER_CA)));
4484     tcg_gen_shri_tl(cpu_so, src, XER_SO);
4485     tcg_gen_shri_tl(cpu_ov, src, XER_OV);
4486     tcg_gen_shri_tl(cpu_ca, src, XER_CA);
4487     tcg_gen_andi_tl(cpu_so, cpu_so, 1);
4488     tcg_gen_andi_tl(cpu_ov, cpu_ov, 1);
4489     tcg_gen_andi_tl(cpu_ca, cpu_ca, 1);
4490 }
4491
4492 /* mcrxr */
4493 static void gen_mcrxr(DisasContext *ctx)
4494 {
4495     TCGv_i32 t0 = tcg_temp_new_i32();
4496     TCGv_i32 t1 = tcg_temp_new_i32();
4497     TCGv_i32 dst = cpu_crf[crfD(ctx->opcode)];
4498
4499     tcg_gen_trunc_tl_i32(t0, cpu_so);
4500     tcg_gen_trunc_tl_i32(t1, cpu_ov);
4501     tcg_gen_trunc_tl_i32(dst, cpu_ca);
4502     tcg_gen_shli_i32(t0, t0, 3);
4503     tcg_gen_shli_i32(t1, t1, 2);
4504     tcg_gen_shli_i32(dst, dst, 1);
4505     tcg_gen_or_i32(dst, dst, t0);
4506     tcg_gen_or_i32(dst, dst, t1);
4507     tcg_temp_free_i32(t0);
4508     tcg_temp_free_i32(t1);
4509
4510     tcg_gen_movi_tl(cpu_so, 0);
4511     tcg_gen_movi_tl(cpu_ov, 0);
4512     tcg_gen_movi_tl(cpu_ca, 0);
4513 }
4514
4515 /* mfcr mfocrf */
4516 static void gen_mfcr(DisasContext *ctx)
4517 {
4518     uint32_t crm, crn;
4519
4520     if (likely(ctx->opcode & 0x00100000)) {
4521         crm = CRM(ctx->opcode);
4522         if (likely(crm && ((crm & (crm - 1)) == 0))) {
4523             crn = ctz32 (crm);
4524             tcg_gen_extu_i32_tl(cpu_gpr[rD(ctx->opcode)], cpu_crf[7 - crn]);
4525             tcg_gen_shli_tl(cpu_gpr[rD(ctx->opcode)],
4526                             cpu_gpr[rD(ctx->opcode)], crn * 4);
4527         }
4528     } else {
4529         TCGv_i32 t0 = tcg_temp_new_i32();
4530         tcg_gen_mov_i32(t0, cpu_crf[0]);
4531         tcg_gen_shli_i32(t0, t0, 4);
4532         tcg_gen_or_i32(t0, t0, cpu_crf[1]);
4533         tcg_gen_shli_i32(t0, t0, 4);
4534         tcg_gen_or_i32(t0, t0, cpu_crf[2]);
4535         tcg_gen_shli_i32(t0, t0, 4);
4536         tcg_gen_or_i32(t0, t0, cpu_crf[3]);
4537         tcg_gen_shli_i32(t0, t0, 4);
4538         tcg_gen_or_i32(t0, t0, cpu_crf[4]);
4539         tcg_gen_shli_i32(t0, t0, 4);
4540         tcg_gen_or_i32(t0, t0, cpu_crf[5]);
4541         tcg_gen_shli_i32(t0, t0, 4);
4542         tcg_gen_or_i32(t0, t0, cpu_crf[6]);
4543         tcg_gen_shli_i32(t0, t0, 4);
4544         tcg_gen_or_i32(t0, t0, cpu_crf[7]);
4545         tcg_gen_extu_i32_tl(cpu_gpr[rD(ctx->opcode)], t0);
4546         tcg_temp_free_i32(t0);
4547     }
4548 }
4549
4550 /* mfmsr */
4551 static void gen_mfmsr(DisasContext *ctx)
4552 {
4553     CHK_SV;
4554     tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_msr);
4555 }
4556
4557 static void spr_noaccess(DisasContext *ctx, int gprn, int sprn)
4558 {
4559 #if 0
4560     sprn = ((sprn >> 5) & 0x1F) | ((sprn & 0x1F) << 5);
4561     printf("ERROR: try to access SPR %d !\n", sprn);
4562 #endif
4563 }
4564 #define SPR_NOACCESS (&spr_noaccess)
4565
4566 /* mfspr */
4567 static inline void gen_op_mfspr(DisasContext *ctx)
4568 {
4569     void (*read_cb)(DisasContext *ctx, int gprn, int sprn);
4570     uint32_t sprn = SPR(ctx->opcode);
4571
4572 #if defined(CONFIG_USER_ONLY)
4573     read_cb = ctx->spr_cb[sprn].uea_read;
4574 #else
4575     if (ctx->pr) {
4576         read_cb = ctx->spr_cb[sprn].uea_read;
4577     } else if (ctx->hv) {
4578         read_cb = ctx->spr_cb[sprn].hea_read;
4579     } else {
4580         read_cb = ctx->spr_cb[sprn].oea_read;
4581     }
4582 #endif
4583     if (likely(read_cb != NULL)) {
4584         if (likely(read_cb != SPR_NOACCESS)) {
4585             (*read_cb)(ctx, rD(ctx->opcode), sprn);
4586         } else {
4587             /* Privilege exception */
4588             /* This is a hack to avoid warnings when running Linux:
4589              * this OS breaks the PowerPC virtualisation model,
4590              * allowing userland application to read the PVR
4591              */
4592             if (sprn != SPR_PVR) {
4593                 fprintf(stderr, "Trying to read privileged spr %d (0x%03x) at "
4594                         TARGET_FMT_lx "\n", sprn, sprn, ctx->nip - 4);
4595                 if (qemu_log_separate()) {
4596                     qemu_log("Trying to read privileged spr %d (0x%03x) at "
4597                              TARGET_FMT_lx "\n", sprn, sprn, ctx->nip - 4);
4598                 }
4599             }
4600             gen_priv_exception(ctx, POWERPC_EXCP_PRIV_REG);
4601         }
4602     } else {
4603         /* ISA 2.07 defines these as no-ops */
4604         if ((ctx->insns_flags2 & PPC2_ISA207S) &&
4605             (sprn >= 808 && sprn <= 811)) {
4606             /* This is a nop */
4607             return;
4608         }
4609         /* Not defined */
4610         fprintf(stderr, "Trying to read invalid spr %d (0x%03x) at "
4611                 TARGET_FMT_lx "\n", sprn, sprn, ctx->nip - 4);
4612         if (qemu_log_separate()) {
4613             qemu_log("Trying to read invalid spr %d (0x%03x) at "
4614                      TARGET_FMT_lx "\n", sprn, sprn, ctx->nip - 4);
4615         }
4616
4617         /* The behaviour depends on MSR:PR and SPR# bit 0x10,
4618          * it can generate a priv, a hv emu or a no-op
4619          */
4620         if (sprn & 0x10) {
4621             if (ctx->pr) {
4622                 gen_priv_exception(ctx, POWERPC_EXCP_INVAL_SPR);
4623             }
4624         } else {
4625             if (ctx->pr || sprn == 0 || sprn == 4 || sprn == 5 || sprn == 6) {
4626                 gen_hvpriv_exception(ctx, POWERPC_EXCP_INVAL_SPR);
4627             }
4628         }
4629     }
4630 }
4631
4632 static void gen_mfspr(DisasContext *ctx)
4633 {
4634     gen_op_mfspr(ctx);
4635 }
4636
4637 /* mftb */
4638 static void gen_mftb(DisasContext *ctx)
4639 {
4640     gen_op_mfspr(ctx);
4641 }
4642
4643 /* mtcrf mtocrf*/
4644 static void gen_mtcrf(DisasContext *ctx)
4645 {
4646     uint32_t crm, crn;
4647
4648     crm = CRM(ctx->opcode);
4649     if (likely((ctx->opcode & 0x00100000))) {
4650         if (crm && ((crm & (crm - 1)) == 0)) {
4651             TCGv_i32 temp = tcg_temp_new_i32();
4652             crn = ctz32 (crm);
4653             tcg_gen_trunc_tl_i32(temp, cpu_gpr[rS(ctx->opcode)]);
4654             tcg_gen_shri_i32(temp, temp, crn * 4);
4655             tcg_gen_andi_i32(cpu_crf[7 - crn], temp, 0xf);
4656             tcg_temp_free_i32(temp);
4657         }
4658     } else {
4659         TCGv_i32 temp = tcg_temp_new_i32();
4660         tcg_gen_trunc_tl_i32(temp, cpu_gpr[rS(ctx->opcode)]);
4661         for (crn = 0 ; crn < 8 ; crn++) {
4662             if (crm & (1 << crn)) {
4663                     tcg_gen_shri_i32(cpu_crf[7 - crn], temp, crn * 4);
4664                     tcg_gen_andi_i32(cpu_crf[7 - crn], cpu_crf[7 - crn], 0xf);
4665             }
4666         }
4667         tcg_temp_free_i32(temp);
4668     }
4669 }
4670
4671 /* mtmsr */
4672 #if defined(TARGET_PPC64)
4673 static void gen_mtmsrd(DisasContext *ctx)
4674 {
4675     CHK_SV;
4676
4677 #if !defined(CONFIG_USER_ONLY)
4678     if (ctx->opcode & 0x00010000) {
4679         /* Special form that does not need any synchronisation */
4680         TCGv t0 = tcg_temp_new();
4681         tcg_gen_andi_tl(t0, cpu_gpr[rS(ctx->opcode)], (1 << MSR_RI) | (1 << MSR_EE));
4682         tcg_gen_andi_tl(cpu_msr, cpu_msr, ~(target_ulong)((1 << MSR_RI) | (1 << MSR_EE)));
4683         tcg_gen_or_tl(cpu_msr, cpu_msr, t0);
4684         tcg_temp_free(t0);
4685     } else {
4686         /* XXX: we need to update nip before the store
4687          *      if we enter power saving mode, we will exit the loop
4688          *      directly from ppc_store_msr
4689          */
4690         gen_update_nip(ctx, ctx->nip);
4691         gen_helper_store_msr(cpu_env, cpu_gpr[rS(ctx->opcode)]);
4692         /* Must stop the translation as machine state (may have) changed */
4693         /* Note that mtmsr is not always defined as context-synchronizing */
4694         gen_stop_exception(ctx);
4695     }
4696 #endif /* !defined(CONFIG_USER_ONLY) */
4697 }
4698 #endif /* defined(TARGET_PPC64) */
4699
4700 static void gen_mtmsr(DisasContext *ctx)
4701 {
4702     CHK_SV;
4703
4704 #if !defined(CONFIG_USER_ONLY)
4705    if (ctx->opcode & 0x00010000) {
4706         /* Special form that does not need any synchronisation */
4707         TCGv t0 = tcg_temp_new();
4708         tcg_gen_andi_tl(t0, cpu_gpr[rS(ctx->opcode)], (1 << MSR_RI) | (1 << MSR_EE));
4709         tcg_gen_andi_tl(cpu_msr, cpu_msr, ~(target_ulong)((1 << MSR_RI) | (1 << MSR_EE)));
4710         tcg_gen_or_tl(cpu_msr, cpu_msr, t0);
4711         tcg_temp_free(t0);
4712     } else {
4713         TCGv msr = tcg_temp_new();
4714
4715         /* XXX: we need to update nip before the store
4716          *      if we enter power saving mode, we will exit the loop
4717          *      directly from ppc_store_msr
4718          */
4719         gen_update_nip(ctx, ctx->nip);
4720 #if defined(TARGET_PPC64)
4721         tcg_gen_deposit_tl(msr, cpu_msr, cpu_gpr[rS(ctx->opcode)], 0, 32);
4722 #else
4723         tcg_gen_mov_tl(msr, cpu_gpr[rS(ctx->opcode)]);
4724 #endif
4725         gen_helper_store_msr(cpu_env, msr);
4726         tcg_temp_free(msr);
4727         /* Must stop the translation as machine state (may have) changed */
4728         /* Note that mtmsr is not always defined as context-synchronizing */
4729         gen_stop_exception(ctx);
4730     }
4731 #endif
4732 }
4733
4734 /* mtspr */
4735 static void gen_mtspr(DisasContext *ctx)
4736 {
4737     void (*write_cb)(DisasContext *ctx, int sprn, int gprn);
4738     uint32_t sprn = SPR(ctx->opcode);
4739
4740 #if defined(CONFIG_USER_ONLY)
4741     write_cb = ctx->spr_cb[sprn].uea_write;
4742 #else
4743     if (ctx->pr) {
4744         write_cb = ctx->spr_cb[sprn].uea_write;
4745     } else if (ctx->hv) {
4746         write_cb = ctx->spr_cb[sprn].hea_write;
4747     } else {
4748         write_cb = ctx->spr_cb[sprn].oea_write;
4749     }
4750 #endif
4751     if (likely(write_cb != NULL)) {
4752         if (likely(write_cb != SPR_NOACCESS)) {
4753             (*write_cb)(ctx, sprn, rS(ctx->opcode));
4754         } else {
4755             /* Privilege exception */
4756             fprintf(stderr, "Trying to write privileged spr %d (0x%03x) at "
4757                     TARGET_FMT_lx "\n", sprn, sprn, ctx->nip - 4);
4758             if (qemu_log_separate()) {
4759                 qemu_log("Trying to write privileged spr %d (0x%03x) at "
4760                          TARGET_FMT_lx "\n", sprn, sprn, ctx->nip - 4);
4761             }
4762             gen_priv_exception(ctx, POWERPC_EXCP_PRIV_REG);
4763         }
4764     } else {
4765         /* ISA 2.07 defines these as no-ops */
4766         if ((ctx->insns_flags2 & PPC2_ISA207S) &&
4767             (sprn >= 808 && sprn <= 811)) {
4768             /* This is a nop */
4769             return;
4770         }
4771
4772         /* Not defined */
4773         if (qemu_log_separate()) {
4774             qemu_log("Trying to write invalid spr %d (0x%03x) at "
4775                      TARGET_FMT_lx "\n", sprn, sprn, ctx->nip - 4);
4776         }
4777         fprintf(stderr, "Trying to write invalid spr %d (0x%03x) at "
4778                 TARGET_FMT_lx "\n", sprn, sprn, ctx->nip - 4);
4779
4780
4781         /* The behaviour depends on MSR:PR and SPR# bit 0x10,
4782          * it can generate a priv, a hv emu or a no-op
4783          */
4784         if (sprn & 0x10) {
4785             if (ctx->pr) {
4786                 gen_priv_exception(ctx, POWERPC_EXCP_INVAL_SPR);
4787             }
4788         } else {
4789             if (ctx->pr || sprn == 0) {
4790                 gen_hvpriv_exception(ctx, POWERPC_EXCP_INVAL_SPR);
4791             }
4792         }
4793     }
4794 }
4795
4796 /***                         Cache management                              ***/
4797
4798 /* dcbf */
4799 static void gen_dcbf(DisasContext *ctx)
4800 {
4801     /* XXX: specification says this is treated as a load by the MMU */
4802     TCGv t0;
4803     gen_set_access_type(ctx, ACCESS_CACHE);
4804     t0 = tcg_temp_new();
4805     gen_addr_reg_index(ctx, t0);
4806     gen_qemu_ld8u(ctx, t0, t0);
4807     tcg_temp_free(t0);
4808 }
4809
4810 /* dcbi (Supervisor only) */
4811 static void gen_dcbi(DisasContext *ctx)
4812 {
4813 #if defined(CONFIG_USER_ONLY)
4814     GEN_PRIV;
4815 #else
4816     TCGv EA, val;
4817
4818     CHK_SV;
4819     EA = tcg_temp_new();
4820     gen_set_access_type(ctx, ACCESS_CACHE);
4821     gen_addr_reg_index(ctx, EA);
4822     val = tcg_temp_new();
4823     /* XXX: specification says this should be treated as a store by the MMU */
4824     gen_qemu_ld8u(ctx, val, EA);
4825     gen_qemu_st8(ctx, val, EA);
4826     tcg_temp_free(val);
4827     tcg_temp_free(EA);
4828 #endif /* defined(CONFIG_USER_ONLY) */
4829 }
4830
4831 /* dcdst */
4832 static void gen_dcbst(DisasContext *ctx)
4833 {
4834     /* XXX: specification say this is treated as a load by the MMU */
4835     TCGv t0;
4836     gen_set_access_type(ctx, ACCESS_CACHE);
4837     t0 = tcg_temp_new();
4838     gen_addr_reg_index(ctx, t0);
4839     gen_qemu_ld8u(ctx, t0, t0);
4840     tcg_temp_free(t0);
4841 }
4842
4843 /* dcbt */
4844 static void gen_dcbt(DisasContext *ctx)
4845 {
4846     /* interpreted as no-op */
4847     /* XXX: specification say this is treated as a load by the MMU
4848      *      but does not generate any exception
4849      */
4850 }
4851
4852 /* dcbtst */
4853 static void gen_dcbtst(DisasContext *ctx)
4854 {
4855     /* interpreted as no-op */
4856     /* XXX: specification say this is treated as a load by the MMU
4857      *      but does not generate any exception
4858      */
4859 }
4860
4861 /* dcbtls */
4862 static void gen_dcbtls(DisasContext *ctx)
4863 {
4864     /* Always fails locking the cache */
4865     TCGv t0 = tcg_temp_new();
4866     gen_load_spr(t0, SPR_Exxx_L1CSR0);
4867     tcg_gen_ori_tl(t0, t0, L1CSR0_CUL);
4868     gen_store_spr(SPR_Exxx_L1CSR0, t0);
4869     tcg_temp_free(t0);
4870 }
4871
4872 /* dcbz */
4873 static void gen_dcbz(DisasContext *ctx)
4874 {
4875     TCGv tcgv_addr;
4876     TCGv_i32 tcgv_is_dcbzl;
4877     int is_dcbzl = ctx->opcode & 0x00200000 ? 1 : 0;
4878
4879     gen_set_access_type(ctx, ACCESS_CACHE);
4880     /* NIP cannot be restored if the memory exception comes from an helper */
4881     gen_update_nip(ctx, ctx->nip - 4);
4882     tcgv_addr = tcg_temp_new();
4883     tcgv_is_dcbzl = tcg_const_i32(is_dcbzl);
4884
4885     gen_addr_reg_index(ctx, tcgv_addr);
4886     gen_helper_dcbz(cpu_env, tcgv_addr, tcgv_is_dcbzl);
4887
4888     tcg_temp_free(tcgv_addr);
4889     tcg_temp_free_i32(tcgv_is_dcbzl);
4890 }
4891
4892 /* dst / dstt */
4893 static void gen_dst(DisasContext *ctx)
4894 {
4895     if (rA(ctx->opcode) == 0) {
4896         gen_inval_exception(ctx, POWERPC_EXCP_INVAL_LSWX);
4897     } else {
4898         /* interpreted as no-op */
4899     }
4900 }
4901
4902 /* dstst /dststt */
4903 static void gen_dstst(DisasContext *ctx)
4904 {
4905     if (rA(ctx->opcode) == 0) {
4906         gen_inval_exception(ctx, POWERPC_EXCP_INVAL_LSWX);
4907     } else {
4908         /* interpreted as no-op */
4909     }
4910
4911 }
4912
4913 /* dss / dssall */
4914 static void gen_dss(DisasContext *ctx)
4915 {
4916     /* interpreted as no-op */
4917 }
4918
4919 /* icbi */
4920 static void gen_icbi(DisasContext *ctx)
4921 {
4922     TCGv t0;
4923     gen_set_access_type(ctx, ACCESS_CACHE);
4924     /* NIP cannot be restored if the memory exception comes from an helper */
4925     gen_update_nip(ctx, ctx->nip - 4);
4926     t0 = tcg_temp_new();
4927     gen_addr_reg_index(ctx, t0);
4928     gen_helper_icbi(cpu_env, t0);
4929     tcg_temp_free(t0);
4930 }
4931
4932 /* Optional: */
4933 /* dcba */
4934 static void gen_dcba(DisasContext *ctx)
4935 {
4936     /* interpreted as no-op */
4937     /* XXX: specification say this is treated as a store by the MMU
4938      *      but does not generate any exception
4939      */
4940 }
4941
4942 /***                    Segment register manipulation                      ***/
4943 /* Supervisor only: */
4944
4945 /* mfsr */
4946 static void gen_mfsr(DisasContext *ctx)
4947 {
4948 #if defined(CONFIG_USER_ONLY)
4949     GEN_PRIV;
4950 #else
4951     TCGv t0;
4952
4953     CHK_SV;
4954     t0 = tcg_const_tl(SR(ctx->opcode));
4955     gen_helper_load_sr(cpu_gpr[rD(ctx->opcode)], cpu_env, t0);
4956     tcg_temp_free(t0);
4957 #endif /* defined(CONFIG_USER_ONLY) */
4958 }
4959
4960 /* mfsrin */
4961 static void gen_mfsrin(DisasContext *ctx)
4962 {
4963 #if defined(CONFIG_USER_ONLY)
4964     GEN_PRIV;
4965 #else
4966     TCGv t0;
4967
4968     CHK_SV;
4969     t0 = tcg_temp_new();
4970     tcg_gen_shri_tl(t0, cpu_gpr[rB(ctx->opcode)], 28);
4971     tcg_gen_andi_tl(t0, t0, 0xF);
4972     gen_helper_load_sr(cpu_gpr[rD(ctx->opcode)], cpu_env, t0);
4973     tcg_temp_free(t0);
4974 #endif /* defined(CONFIG_USER_ONLY) */
4975 }
4976
4977 /* mtsr */
4978 static void gen_mtsr(DisasContext *ctx)
4979 {
4980 #if defined(CONFIG_USER_ONLY)
4981     GEN_PRIV;
4982 #else
4983     TCGv t0;
4984
4985     CHK_SV;
4986     t0 = tcg_const_tl(SR(ctx->opcode));
4987     gen_helper_store_sr(cpu_env, t0, cpu_gpr[rS(ctx->opcode)]);
4988     tcg_temp_free(t0);
4989 #endif /* defined(CONFIG_USER_ONLY) */
4990 }
4991
4992 /* mtsrin */
4993 static void gen_mtsrin(DisasContext *ctx)
4994 {
4995 #if defined(CONFIG_USER_ONLY)
4996     GEN_PRIV;
4997 #else
4998     TCGv t0;
4999     CHK_SV;
5000
5001     t0 = tcg_temp_new();
5002     tcg_gen_shri_tl(t0, cpu_gpr[rB(ctx->opcode)], 28);
5003     tcg_gen_andi_tl(t0, t0, 0xF);
5004     gen_helper_store_sr(cpu_env, t0, cpu_gpr[rD(ctx->opcode)]);
5005     tcg_temp_free(t0);
5006 #endif /* defined(CONFIG_USER_ONLY) */
5007 }
5008
5009 #if defined(TARGET_PPC64)
5010 /* Specific implementation for PowerPC 64 "bridge" emulation using SLB */
5011
5012 /* mfsr */
5013 static void gen_mfsr_64b(DisasContext *ctx)
5014 {
5015 #if defined(CONFIG_USER_ONLY)
5016     GEN_PRIV;
5017 #else
5018     TCGv t0;
5019
5020     CHK_SV;
5021     t0 = tcg_const_tl(SR(ctx->opcode));
5022     gen_helper_load_sr(cpu_gpr[rD(ctx->opcode)], cpu_env, t0);
5023     tcg_temp_free(t0);
5024 #endif /* defined(CONFIG_USER_ONLY) */
5025 }
5026
5027 /* mfsrin */
5028 static void gen_mfsrin_64b(DisasContext *ctx)
5029 {
5030 #if defined(CONFIG_USER_ONLY)
5031     GEN_PRIV;
5032 #else
5033     TCGv t0;
5034
5035     CHK_SV;
5036     t0 = tcg_temp_new();
5037     tcg_gen_shri_tl(t0, cpu_gpr[rB(ctx->opcode)], 28);
5038     tcg_gen_andi_tl(t0, t0, 0xF);
5039     gen_helper_load_sr(cpu_gpr[rD(ctx->opcode)], cpu_env, t0);
5040     tcg_temp_free(t0);
5041 #endif /* defined(CONFIG_USER_ONLY) */
5042 }
5043
5044 /* mtsr */
5045 static void gen_mtsr_64b(DisasContext *ctx)
5046 {
5047 #if defined(CONFIG_USER_ONLY)
5048     GEN_PRIV;
5049 #else
5050     TCGv t0;
5051
5052     CHK_SV;
5053     t0 = tcg_const_tl(SR(ctx->opcode));
5054     gen_helper_store_sr(cpu_env, t0, cpu_gpr[rS(ctx->opcode)]);
5055     tcg_temp_free(t0);
5056 #endif /* defined(CONFIG_USER_ONLY) */
5057 }
5058
5059 /* mtsrin */
5060 static void gen_mtsrin_64b(DisasContext *ctx)
5061 {
5062 #if defined(CONFIG_USER_ONLY)
5063     GEN_PRIV;
5064 #else
5065     TCGv t0;
5066
5067     CHK_SV;
5068     t0 = tcg_temp_new();
5069     tcg_gen_shri_tl(t0, cpu_gpr[rB(ctx->opcode)], 28);
5070     tcg_gen_andi_tl(t0, t0, 0xF);
5071     gen_helper_store_sr(cpu_env, t0, cpu_gpr[rS(ctx->opcode)]);
5072     tcg_temp_free(t0);
5073 #endif /* defined(CONFIG_USER_ONLY) */
5074 }
5075
5076 /* slbmte */
5077 static void gen_slbmte(DisasContext *ctx)
5078 {
5079 #if defined(CONFIG_USER_ONLY)
5080     GEN_PRIV;
5081 #else
5082     CHK_SV;
5083
5084     gen_helper_store_slb(cpu_env, cpu_gpr[rB(ctx->opcode)],
5085                          cpu_gpr[rS(ctx->opcode)]);
5086 #endif /* defined(CONFIG_USER_ONLY) */
5087 }
5088
5089 static void gen_slbmfee(DisasContext *ctx)
5090 {
5091 #if defined(CONFIG_USER_ONLY)
5092     GEN_PRIV;
5093 #else
5094     CHK_SV;
5095
5096     gen_helper_load_slb_esid(cpu_gpr[rS(ctx->opcode)], cpu_env,
5097                              cpu_gpr[rB(ctx->opcode)]);
5098 #endif /* defined(CONFIG_USER_ONLY) */
5099 }
5100
5101 static void gen_slbmfev(DisasContext *ctx)
5102 {
5103 #if defined(CONFIG_USER_ONLY)
5104     GEN_PRIV;
5105 #else
5106     CHK_SV;
5107
5108     gen_helper_load_slb_vsid(cpu_gpr[rS(ctx->opcode)], cpu_env,
5109                              cpu_gpr[rB(ctx->opcode)]);
5110 #endif /* defined(CONFIG_USER_ONLY) */
5111 }
5112
5113 static void gen_slbfee_(DisasContext *ctx)
5114 {
5115 #if defined(CONFIG_USER_ONLY)
5116     gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
5117 #else
5118     TCGLabel *l1, *l2;
5119
5120     if (unlikely(ctx->pr)) {
5121         gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
5122         return;
5123     }
5124     gen_helper_find_slb_vsid(cpu_gpr[rS(ctx->opcode)], cpu_env,
5125                              cpu_gpr[rB(ctx->opcode)]);
5126     l1 = gen_new_label();
5127     l2 = gen_new_label();
5128     tcg_gen_trunc_tl_i32(cpu_crf[0], cpu_so);
5129     tcg_gen_brcondi_tl(TCG_COND_EQ, cpu_gpr[rS(ctx->opcode)], -1, l1);
5130     tcg_gen_ori_i32(cpu_crf[0], cpu_crf[0], 1 << CRF_EQ);
5131     tcg_gen_br(l2);
5132     gen_set_label(l1);
5133     tcg_gen_movi_tl(cpu_gpr[rS(ctx->opcode)], 0);
5134     gen_set_label(l2);
5135 #endif
5136 }
5137 #endif /* defined(TARGET_PPC64) */
5138
5139 /***                      Lookaside buffer management                      ***/
5140 /* Optional & supervisor only: */
5141
5142 /* tlbia */
5143 static void gen_tlbia(DisasContext *ctx)
5144 {
5145 #if defined(CONFIG_USER_ONLY)
5146     GEN_PRIV;
5147 #else
5148     CHK_HV;
5149
5150     gen_helper_tlbia(cpu_env);
5151 #endif  /* defined(CONFIG_USER_ONLY) */
5152 }
5153
5154 /* tlbiel */
5155 static void gen_tlbiel(DisasContext *ctx)
5156 {
5157 #if defined(CONFIG_USER_ONLY)
5158     GEN_PRIV;
5159 #else
5160     CHK_SV;
5161
5162     gen_helper_tlbie(cpu_env, cpu_gpr[rB(ctx->opcode)]);
5163 #endif /* defined(CONFIG_USER_ONLY) */
5164 }
5165
5166 /* tlbie */
5167 static void gen_tlbie(DisasContext *ctx)
5168 {
5169 #if defined(CONFIG_USER_ONLY)
5170     GEN_PRIV;
5171 #else
5172     CHK_HV;
5173
5174     if (NARROW_MODE(ctx)) {
5175         TCGv t0 = tcg_temp_new();
5176         tcg_gen_ext32u_tl(t0, cpu_gpr[rB(ctx->opcode)]);
5177         gen_helper_tlbie(cpu_env, t0);
5178         tcg_temp_free(t0);
5179     } else {
5180         gen_helper_tlbie(cpu_env, cpu_gpr[rB(ctx->opcode)]);
5181     }
5182 #endif /* defined(CONFIG_USER_ONLY) */
5183 }
5184
5185 /* tlbsync */
5186 static void gen_tlbsync(DisasContext *ctx)
5187 {
5188 #if defined(CONFIG_USER_ONLY)
5189     GEN_PRIV;
5190 #else
5191     CHK_HV;
5192
5193     /* tlbsync is a nop for server, ptesync handles delayed tlb flush,
5194      * embedded however needs to deal with tlbsync. We don't try to be
5195      * fancy and swallow the overhead of checking for both.
5196      */
5197     gen_check_tlb_flush(ctx);
5198 #endif /* defined(CONFIG_USER_ONLY) */
5199 }
5200
5201 #if defined(TARGET_PPC64)
5202 /* slbia */
5203 static void gen_slbia(DisasContext *ctx)
5204 {
5205 #if defined(CONFIG_USER_ONLY)
5206     GEN_PRIV;
5207 #else
5208     CHK_SV;
5209
5210     gen_helper_slbia(cpu_env);
5211 #endif /* defined(CONFIG_USER_ONLY) */
5212 }
5213
5214 /* slbie */
5215 static void gen_slbie(DisasContext *ctx)
5216 {
5217 #if defined(CONFIG_USER_ONLY)
5218     GEN_PRIV;
5219 #else
5220     CHK_SV;
5221
5222     gen_helper_slbie(cpu_env, cpu_gpr[rB(ctx->opcode)]);
5223 #endif /* defined(CONFIG_USER_ONLY) */
5224 }
5225 #endif  /* defined(TARGET_PPC64) */
5226
5227 /***                              External control                         ***/
5228 /* Optional: */
5229
5230 /* eciwx */
5231 static void gen_eciwx(DisasContext *ctx)
5232 {
5233     TCGv t0;
5234     /* Should check EAR[E] ! */
5235     gen_set_access_type(ctx, ACCESS_EXT);
5236     t0 = tcg_temp_new();
5237     gen_addr_reg_index(ctx, t0);
5238     gen_check_align(ctx, t0, 0x03);
5239     gen_qemu_ld32u(ctx, cpu_gpr[rD(ctx->opcode)], t0);
5240     tcg_temp_free(t0);
5241 }
5242
5243 /* ecowx */
5244 static void gen_ecowx(DisasContext *ctx)
5245 {
5246     TCGv t0;
5247     /* Should check EAR[E] ! */
5248     gen_set_access_type(ctx, ACCESS_EXT);
5249     t0 = tcg_temp_new();
5250     gen_addr_reg_index(ctx, t0);
5251     gen_check_align(ctx, t0, 0x03);
5252     gen_qemu_st32(ctx, cpu_gpr[rD(ctx->opcode)], t0);
5253     tcg_temp_free(t0);
5254 }
5255
5256 /* PowerPC 601 specific instructions */
5257
5258 /* abs - abs. */
5259 static void gen_abs(DisasContext *ctx)
5260 {
5261     TCGLabel *l1 = gen_new_label();
5262     TCGLabel *l2 = gen_new_label();
5263     tcg_gen_brcondi_tl(TCG_COND_GE, cpu_gpr[rA(ctx->opcode)], 0, l1);
5264     tcg_gen_neg_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)]);
5265     tcg_gen_br(l2);
5266     gen_set_label(l1);
5267     tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)]);
5268     gen_set_label(l2);
5269     if (unlikely(Rc(ctx->opcode) != 0))
5270         gen_set_Rc0(ctx, cpu_gpr[rD(ctx->opcode)]);
5271 }
5272
5273 /* abso - abso. */
5274 static void gen_abso(DisasContext *ctx)
5275 {
5276     TCGLabel *l1 = gen_new_label();
5277     TCGLabel *l2 = gen_new_label();
5278     TCGLabel *l3 = gen_new_label();
5279     /* Start with XER OV disabled, the most likely case */
5280     tcg_gen_movi_tl(cpu_ov, 0);
5281     tcg_gen_brcondi_tl(TCG_COND_GE, cpu_gpr[rA(ctx->opcode)], 0, l2);
5282     tcg_gen_brcondi_tl(TCG_COND_NE, cpu_gpr[rA(ctx->opcode)], 0x80000000, l1);
5283     tcg_gen_movi_tl(cpu_ov, 1);
5284     tcg_gen_movi_tl(cpu_so, 1);
5285     tcg_gen_br(l2);
5286     gen_set_label(l1);
5287     tcg_gen_neg_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)]);
5288     tcg_gen_br(l3);
5289     gen_set_label(l2);
5290     tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)]);
5291     gen_set_label(l3);
5292     if (unlikely(Rc(ctx->opcode) != 0))
5293         gen_set_Rc0(ctx, cpu_gpr[rD(ctx->opcode)]);
5294 }
5295
5296 /* clcs */
5297 static void gen_clcs(DisasContext *ctx)
5298 {
5299     TCGv_i32 t0 = tcg_const_i32(rA(ctx->opcode));
5300     gen_helper_clcs(cpu_gpr[rD(ctx->opcode)], cpu_env, t0);
5301     tcg_temp_free_i32(t0);
5302     /* Rc=1 sets CR0 to an undefined state */
5303 }
5304
5305 /* div - div. */
5306 static void gen_div(DisasContext *ctx)
5307 {
5308     gen_helper_div(cpu_gpr[rD(ctx->opcode)], cpu_env, cpu_gpr[rA(ctx->opcode)],
5309                    cpu_gpr[rB(ctx->opcode)]);
5310     if (unlikely(Rc(ctx->opcode) != 0))
5311         gen_set_Rc0(ctx, cpu_gpr[rD(ctx->opcode)]);
5312 }
5313
5314 /* divo - divo. */
5315 static void gen_divo(DisasContext *ctx)
5316 {
5317     gen_helper_divo(cpu_gpr[rD(ctx->opcode)], cpu_env, cpu_gpr[rA(ctx->opcode)],
5318                     cpu_gpr[rB(ctx->opcode)]);
5319     if (unlikely(Rc(ctx->opcode) != 0))
5320         gen_set_Rc0(ctx, cpu_gpr[rD(ctx->opcode)]);
5321 }
5322
5323 /* divs - divs. */
5324 static void gen_divs(DisasContext *ctx)
5325 {
5326     gen_helper_divs(cpu_gpr[rD(ctx->opcode)], cpu_env, cpu_gpr[rA(ctx->opcode)],
5327                     cpu_gpr[rB(ctx->opcode)]);
5328     if (unlikely(Rc(ctx->opcode) != 0))
5329         gen_set_Rc0(ctx, cpu_gpr[rD(ctx->opcode)]);
5330 }
5331
5332 /* divso - divso. */
5333 static void gen_divso(DisasContext *ctx)
5334 {
5335     gen_helper_divso(cpu_gpr[rD(ctx->opcode)], cpu_env,
5336                      cpu_gpr[rA(ctx->opcode)], cpu_gpr[rB(ctx->opcode)]);
5337     if (unlikely(Rc(ctx->opcode) != 0))
5338         gen_set_Rc0(ctx, cpu_gpr[rD(ctx->opcode)]);
5339 }
5340
5341 /* doz - doz. */
5342 static void gen_doz(DisasContext *ctx)
5343 {
5344     TCGLabel *l1 = gen_new_label();
5345     TCGLabel *l2 = gen_new_label();
5346     tcg_gen_brcond_tl(TCG_COND_GE, cpu_gpr[rB(ctx->opcode)], cpu_gpr[rA(ctx->opcode)], l1);
5347     tcg_gen_sub_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rB(ctx->opcode)], cpu_gpr[rA(ctx->opcode)]);
5348     tcg_gen_br(l2);
5349     gen_set_label(l1);
5350     tcg_gen_movi_tl(cpu_gpr[rD(ctx->opcode)], 0);
5351     gen_set_label(l2);
5352     if (unlikely(Rc(ctx->opcode) != 0))
5353         gen_set_Rc0(ctx, cpu_gpr[rD(ctx->opcode)]);
5354 }
5355
5356 /* dozo - dozo. */
5357 static void gen_dozo(DisasContext *ctx)
5358 {
5359     TCGLabel *l1 = gen_new_label();
5360     TCGLabel *l2 = gen_new_label();
5361     TCGv t0 = tcg_temp_new();
5362     TCGv t1 = tcg_temp_new();
5363     TCGv t2 = tcg_temp_new();
5364     /* Start with XER OV disabled, the most likely case */
5365     tcg_gen_movi_tl(cpu_ov, 0);
5366     tcg_gen_brcond_tl(TCG_COND_GE, cpu_gpr[rB(ctx->opcode)], cpu_gpr[rA(ctx->opcode)], l1);
5367     tcg_gen_sub_tl(t0, cpu_gpr[rB(ctx->opcode)], cpu_gpr[rA(ctx->opcode)]);
5368     tcg_gen_xor_tl(t1, cpu_gpr[rB(ctx->opcode)], cpu_gpr[rA(ctx->opcode)]);
5369     tcg_gen_xor_tl(t2, cpu_gpr[rA(ctx->opcode)], t0);
5370     tcg_gen_andc_tl(t1, t1, t2);
5371     tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], t0);
5372     tcg_gen_brcondi_tl(TCG_COND_GE, t1, 0, l2);
5373     tcg_gen_movi_tl(cpu_ov, 1);
5374     tcg_gen_movi_tl(cpu_so, 1);
5375     tcg_gen_br(l2);
5376     gen_set_label(l1);
5377     tcg_gen_movi_tl(cpu_gpr[rD(ctx->opcode)], 0);
5378     gen_set_label(l2);
5379     tcg_temp_free(t0);
5380     tcg_temp_free(t1);
5381     tcg_temp_free(t2);
5382     if (unlikely(Rc(ctx->opcode) != 0))
5383         gen_set_Rc0(ctx, cpu_gpr[rD(ctx->opcode)]);
5384 }
5385
5386 /* dozi */
5387 static void gen_dozi(DisasContext *ctx)
5388 {
5389     target_long simm = SIMM(ctx->opcode);
5390     TCGLabel *l1 = gen_new_label();
5391     TCGLabel *l2 = gen_new_label();
5392     tcg_gen_brcondi_tl(TCG_COND_LT, cpu_gpr[rA(ctx->opcode)], simm, l1);
5393     tcg_gen_subfi_tl(cpu_gpr[rD(ctx->opcode)], simm, cpu_gpr[rA(ctx->opcode)]);
5394     tcg_gen_br(l2);
5395     gen_set_label(l1);
5396     tcg_gen_movi_tl(cpu_gpr[rD(ctx->opcode)], 0);
5397     gen_set_label(l2);
5398     if (unlikely(Rc(ctx->opcode) != 0))
5399         gen_set_Rc0(ctx, cpu_gpr[rD(ctx->opcode)]);
5400 }
5401
5402 /* lscbx - lscbx. */
5403 static void gen_lscbx(DisasContext *ctx)
5404 {
5405     TCGv t0 = tcg_temp_new();
5406     TCGv_i32 t1 = tcg_const_i32(rD(ctx->opcode));
5407     TCGv_i32 t2 = tcg_const_i32(rA(ctx->opcode));
5408     TCGv_i32 t3 = tcg_const_i32(rB(ctx->opcode));
5409
5410     gen_addr_reg_index(ctx, t0);
5411     /* NIP cannot be restored if the memory exception comes from an helper */
5412     gen_update_nip(ctx, ctx->nip - 4);
5413     gen_helper_lscbx(t0, cpu_env, t0, t1, t2, t3);
5414     tcg_temp_free_i32(t1);
5415     tcg_temp_free_i32(t2);
5416     tcg_temp_free_i32(t3);
5417     tcg_gen_andi_tl(cpu_xer, cpu_xer, ~0x7F);
5418     tcg_gen_or_tl(cpu_xer, cpu_xer, t0);
5419     if (unlikely(Rc(ctx->opcode) != 0))
5420         gen_set_Rc0(ctx, t0);
5421     tcg_temp_free(t0);
5422 }
5423
5424 /* maskg - maskg. */
5425 static void gen_maskg(DisasContext *ctx)
5426 {
5427     TCGLabel *l1 = gen_new_label();
5428     TCGv t0 = tcg_temp_new();
5429     TCGv t1 = tcg_temp_new();
5430     TCGv t2 = tcg_temp_new();
5431     TCGv t3 = tcg_temp_new();
5432     tcg_gen_movi_tl(t3, 0xFFFFFFFF);
5433     tcg_gen_andi_tl(t0, cpu_gpr[rB(ctx->opcode)], 0x1F);
5434     tcg_gen_andi_tl(t1, cpu_gpr[rS(ctx->opcode)], 0x1F);
5435     tcg_gen_addi_tl(t2, t0, 1);
5436     tcg_gen_shr_tl(t2, t3, t2);
5437     tcg_gen_shr_tl(t3, t3, t1);
5438     tcg_gen_xor_tl(cpu_gpr[rA(ctx->opcode)], t2, t3);
5439     tcg_gen_brcond_tl(TCG_COND_GE, t0, t1, l1);
5440     tcg_gen_neg_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rA(ctx->opcode)]);
5441     gen_set_label(l1);
5442     tcg_temp_free(t0);
5443     tcg_temp_free(t1);
5444     tcg_temp_free(t2);
5445     tcg_temp_free(t3);
5446     if (unlikely(Rc(ctx->opcode) != 0))
5447         gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
5448 }
5449
5450 /* maskir - maskir. */
5451 static void gen_maskir(DisasContext *ctx)
5452 {
5453     TCGv t0 = tcg_temp_new();
5454     TCGv t1 = tcg_temp_new();
5455     tcg_gen_and_tl(t0, cpu_gpr[rS(ctx->opcode)], cpu_gpr[rB(ctx->opcode)]);
5456     tcg_gen_andc_tl(t1, cpu_gpr[rA(ctx->opcode)], cpu_gpr[rB(ctx->opcode)]);
5457     tcg_gen_or_tl(cpu_gpr[rA(ctx->opcode)], t0, t1);
5458     tcg_temp_free(t0);
5459     tcg_temp_free(t1);
5460     if (unlikely(Rc(ctx->opcode) != 0))
5461         gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
5462 }
5463
5464 /* mul - mul. */
5465 static void gen_mul(DisasContext *ctx)
5466 {
5467     TCGv_i64 t0 = tcg_temp_new_i64();
5468     TCGv_i64 t1 = tcg_temp_new_i64();
5469     TCGv t2 = tcg_temp_new();
5470     tcg_gen_extu_tl_i64(t0, cpu_gpr[rA(ctx->opcode)]);
5471     tcg_gen_extu_tl_i64(t1, cpu_gpr[rB(ctx->opcode)]);
5472     tcg_gen_mul_i64(t0, t0, t1);
5473     tcg_gen_trunc_i64_tl(t2, t0);
5474     gen_store_spr(SPR_MQ, t2);
5475     tcg_gen_shri_i64(t1, t0, 32);
5476     tcg_gen_trunc_i64_tl(cpu_gpr[rD(ctx->opcode)], t1);
5477     tcg_temp_free_i64(t0);
5478     tcg_temp_free_i64(t1);
5479     tcg_temp_free(t2);
5480     if (unlikely(Rc(ctx->opcode) != 0))
5481         gen_set_Rc0(ctx, cpu_gpr[rD(ctx->opcode)]);
5482 }
5483
5484 /* mulo - mulo. */
5485 static void gen_mulo(DisasContext *ctx)
5486 {
5487     TCGLabel *l1 = gen_new_label();
5488     TCGv_i64 t0 = tcg_temp_new_i64();
5489     TCGv_i64 t1 = tcg_temp_new_i64();
5490     TCGv t2 = tcg_temp_new();
5491     /* Start with XER OV disabled, the most likely case */
5492     tcg_gen_movi_tl(cpu_ov, 0);
5493     tcg_gen_extu_tl_i64(t0, cpu_gpr[rA(ctx->opcode)]);
5494     tcg_gen_extu_tl_i64(t1, cpu_gpr[rB(ctx->opcode)]);
5495     tcg_gen_mul_i64(t0, t0, t1);
5496     tcg_gen_trunc_i64_tl(t2, t0);
5497     gen_store_spr(SPR_MQ, t2);
5498     tcg_gen_shri_i64(t1, t0, 32);
5499     tcg_gen_trunc_i64_tl(cpu_gpr[rD(ctx->opcode)], t1);
5500     tcg_gen_ext32s_i64(t1, t0);
5501     tcg_gen_brcond_i64(TCG_COND_EQ, t0, t1, l1);
5502     tcg_gen_movi_tl(cpu_ov, 1);
5503     tcg_gen_movi_tl(cpu_so, 1);
5504     gen_set_label(l1);
5505     tcg_temp_free_i64(t0);
5506     tcg_temp_free_i64(t1);
5507     tcg_temp_free(t2);
5508     if (unlikely(Rc(ctx->opcode) != 0))
5509         gen_set_Rc0(ctx, cpu_gpr[rD(ctx->opcode)]);
5510 }
5511
5512 /* nabs - nabs. */
5513 static void gen_nabs(DisasContext *ctx)
5514 {
5515     TCGLabel *l1 = gen_new_label();
5516     TCGLabel *l2 = gen_new_label();
5517     tcg_gen_brcondi_tl(TCG_COND_GT, cpu_gpr[rA(ctx->opcode)], 0, l1);
5518     tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)]);
5519     tcg_gen_br(l2);
5520     gen_set_label(l1);
5521     tcg_gen_neg_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)]);
5522     gen_set_label(l2);
5523     if (unlikely(Rc(ctx->opcode) != 0))
5524         gen_set_Rc0(ctx, cpu_gpr[rD(ctx->opcode)]);
5525 }
5526
5527 /* nabso - nabso. */
5528 static void gen_nabso(DisasContext *ctx)
5529 {
5530     TCGLabel *l1 = gen_new_label();
5531     TCGLabel *l2 = gen_new_label();
5532     tcg_gen_brcondi_tl(TCG_COND_GT, cpu_gpr[rA(ctx->opcode)], 0, l1);
5533     tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)]);
5534     tcg_gen_br(l2);
5535     gen_set_label(l1);
5536     tcg_gen_neg_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)]);
5537     gen_set_label(l2);
5538     /* nabs never overflows */
5539     tcg_gen_movi_tl(cpu_ov, 0);
5540     if (unlikely(Rc(ctx->opcode) != 0))
5541         gen_set_Rc0(ctx, cpu_gpr[rD(ctx->opcode)]);
5542 }
5543
5544 /* rlmi - rlmi. */
5545 static void gen_rlmi(DisasContext *ctx)
5546 {
5547     uint32_t mb = MB(ctx->opcode);
5548     uint32_t me = ME(ctx->opcode);
5549     TCGv t0 = tcg_temp_new();
5550     tcg_gen_andi_tl(t0, cpu_gpr[rB(ctx->opcode)], 0x1F);
5551     tcg_gen_rotl_tl(t0, cpu_gpr[rS(ctx->opcode)], t0);
5552     tcg_gen_andi_tl(t0, t0, MASK(mb, me));
5553     tcg_gen_andi_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rA(ctx->opcode)], ~MASK(mb, me));
5554     tcg_gen_or_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rA(ctx->opcode)], t0);
5555     tcg_temp_free(t0);
5556     if (unlikely(Rc(ctx->opcode) != 0))
5557         gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
5558 }
5559
5560 /* rrib - rrib. */
5561 static void gen_rrib(DisasContext *ctx)
5562 {
5563     TCGv t0 = tcg_temp_new();
5564     TCGv t1 = tcg_temp_new();
5565     tcg_gen_andi_tl(t0, cpu_gpr[rB(ctx->opcode)], 0x1F);
5566     tcg_gen_movi_tl(t1, 0x80000000);
5567     tcg_gen_shr_tl(t1, t1, t0);
5568     tcg_gen_shr_tl(t0, cpu_gpr[rS(ctx->opcode)], t0);
5569     tcg_gen_and_tl(t0, t0, t1);
5570     tcg_gen_andc_tl(t1, cpu_gpr[rA(ctx->opcode)], t1);
5571     tcg_gen_or_tl(cpu_gpr[rA(ctx->opcode)], t0, t1);
5572     tcg_temp_free(t0);
5573     tcg_temp_free(t1);
5574     if (unlikely(Rc(ctx->opcode) != 0))
5575         gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
5576 }
5577
5578 /* sle - sle. */
5579 static void gen_sle(DisasContext *ctx)
5580 {
5581     TCGv t0 = tcg_temp_new();
5582     TCGv t1 = tcg_temp_new();
5583     tcg_gen_andi_tl(t1, cpu_gpr[rB(ctx->opcode)], 0x1F);
5584     tcg_gen_shl_tl(t0, cpu_gpr[rS(ctx->opcode)], t1);
5585     tcg_gen_subfi_tl(t1, 32, t1);
5586     tcg_gen_shr_tl(t1, cpu_gpr[rS(ctx->opcode)], t1);
5587     tcg_gen_or_tl(t1, t0, t1);
5588     tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], t0);
5589     gen_store_spr(SPR_MQ, t1);
5590     tcg_temp_free(t0);
5591     tcg_temp_free(t1);
5592     if (unlikely(Rc(ctx->opcode) != 0))
5593         gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
5594 }
5595
5596 /* sleq - sleq. */
5597 static void gen_sleq(DisasContext *ctx)
5598 {
5599     TCGv t0 = tcg_temp_new();
5600     TCGv t1 = tcg_temp_new();
5601     TCGv t2 = tcg_temp_new();
5602     tcg_gen_andi_tl(t0, cpu_gpr[rB(ctx->opcode)], 0x1F);
5603     tcg_gen_movi_tl(t2, 0xFFFFFFFF);
5604     tcg_gen_shl_tl(t2, t2, t0);
5605     tcg_gen_rotl_tl(t0, cpu_gpr[rS(ctx->opcode)], t0);
5606     gen_load_spr(t1, SPR_MQ);
5607     gen_store_spr(SPR_MQ, t0);
5608     tcg_gen_and_tl(t0, t0, t2);
5609     tcg_gen_andc_tl(t1, t1, t2);
5610     tcg_gen_or_tl(cpu_gpr[rA(ctx->opcode)], t0, t1);
5611     tcg_temp_free(t0);
5612     tcg_temp_free(t1);
5613     tcg_temp_free(t2);
5614     if (unlikely(Rc(ctx->opcode) != 0))
5615         gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
5616 }
5617
5618 /* sliq - sliq. */
5619 static void gen_sliq(DisasContext *ctx)
5620 {
5621     int sh = SH(ctx->opcode);
5622     TCGv t0 = tcg_temp_new();
5623     TCGv t1 = tcg_temp_new();
5624     tcg_gen_shli_tl(t0, cpu_gpr[rS(ctx->opcode)], sh);
5625     tcg_gen_shri_tl(t1, cpu_gpr[rS(ctx->opcode)], 32 - sh);
5626     tcg_gen_or_tl(t1, t0, t1);
5627     tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], t0);
5628     gen_store_spr(SPR_MQ, t1);
5629     tcg_temp_free(t0);
5630     tcg_temp_free(t1);
5631     if (unlikely(Rc(ctx->opcode) != 0))
5632         gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
5633 }
5634
5635 /* slliq - slliq. */
5636 static void gen_slliq(DisasContext *ctx)
5637 {
5638     int sh = SH(ctx->opcode);
5639     TCGv t0 = tcg_temp_new();
5640     TCGv t1 = tcg_temp_new();
5641     tcg_gen_rotli_tl(t0, cpu_gpr[rS(ctx->opcode)], sh);
5642     gen_load_spr(t1, SPR_MQ);
5643     gen_store_spr(SPR_MQ, t0);
5644     tcg_gen_andi_tl(t0, t0,  (0xFFFFFFFFU << sh));
5645     tcg_gen_andi_tl(t1, t1, ~(0xFFFFFFFFU << sh));
5646     tcg_gen_or_tl(cpu_gpr[rA(ctx->opcode)], t0, t1);
5647     tcg_temp_free(t0);
5648     tcg_temp_free(t1);
5649     if (unlikely(Rc(ctx->opcode) != 0))
5650         gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
5651 }
5652
5653 /* sllq - sllq. */
5654 static void gen_sllq(DisasContext *ctx)
5655 {
5656     TCGLabel *l1 = gen_new_label();
5657     TCGLabel *l2 = gen_new_label();
5658     TCGv t0 = tcg_temp_local_new();
5659     TCGv t1 = tcg_temp_local_new();
5660     TCGv t2 = tcg_temp_local_new();
5661     tcg_gen_andi_tl(t2, cpu_gpr[rB(ctx->opcode)], 0x1F);
5662     tcg_gen_movi_tl(t1, 0xFFFFFFFF);
5663     tcg_gen_shl_tl(t1, t1, t2);
5664     tcg_gen_andi_tl(t0, cpu_gpr[rB(ctx->opcode)], 0x20);
5665     tcg_gen_brcondi_tl(TCG_COND_EQ, t0, 0, l1);
5666     gen_load_spr(t0, SPR_MQ);
5667     tcg_gen_and_tl(cpu_gpr[rA(ctx->opcode)], t0, t1);
5668     tcg_gen_br(l2);
5669     gen_set_label(l1);
5670     tcg_gen_shl_tl(t0, cpu_gpr[rS(ctx->opcode)], t2);
5671     gen_load_spr(t2, SPR_MQ);
5672     tcg_gen_andc_tl(t1, t2, t1);
5673     tcg_gen_or_tl(cpu_gpr[rA(ctx->opcode)], t0, t1);
5674     gen_set_label(l2);
5675     tcg_temp_free(t0);
5676     tcg_temp_free(t1);
5677     tcg_temp_free(t2);
5678     if (unlikely(Rc(ctx->opcode) != 0))
5679         gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
5680 }
5681
5682 /* slq - slq. */
5683 static void gen_slq(DisasContext *ctx)
5684 {
5685     TCGLabel *l1 = gen_new_label();
5686     TCGv t0 = tcg_temp_new();
5687     TCGv t1 = tcg_temp_new();
5688     tcg_gen_andi_tl(t1, cpu_gpr[rB(ctx->opcode)], 0x1F);
5689     tcg_gen_shl_tl(t0, cpu_gpr[rS(ctx->opcode)], t1);
5690     tcg_gen_subfi_tl(t1, 32, t1);
5691     tcg_gen_shr_tl(t1, cpu_gpr[rS(ctx->opcode)], t1);
5692     tcg_gen_or_tl(t1, t0, t1);
5693     gen_store_spr(SPR_MQ, t1);
5694     tcg_gen_andi_tl(t1, cpu_gpr[rB(ctx->opcode)], 0x20);
5695     tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], t0);
5696     tcg_gen_brcondi_tl(TCG_COND_EQ, t1, 0, l1);
5697     tcg_gen_movi_tl(cpu_gpr[rA(ctx->opcode)], 0);
5698     gen_set_label(l1);
5699     tcg_temp_free(t0);
5700     tcg_temp_free(t1);
5701     if (unlikely(Rc(ctx->opcode) != 0))
5702         gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
5703 }
5704
5705 /* sraiq - sraiq. */
5706 static void gen_sraiq(DisasContext *ctx)
5707 {
5708     int sh = SH(ctx->opcode);
5709     TCGLabel *l1 = gen_new_label();
5710     TCGv t0 = tcg_temp_new();
5711     TCGv t1 = tcg_temp_new();
5712     tcg_gen_shri_tl(t0, cpu_gpr[rS(ctx->opcode)], sh);
5713     tcg_gen_shli_tl(t1, cpu_gpr[rS(ctx->opcode)], 32 - sh);
5714     tcg_gen_or_tl(t0, t0, t1);
5715     gen_store_spr(SPR_MQ, t0);
5716     tcg_gen_movi_tl(cpu_ca, 0);
5717     tcg_gen_brcondi_tl(TCG_COND_EQ, t1, 0, l1);
5718     tcg_gen_brcondi_tl(TCG_COND_GE, cpu_gpr[rS(ctx->opcode)], 0, l1);
5719     tcg_gen_movi_tl(cpu_ca, 1);
5720     gen_set_label(l1);
5721     tcg_gen_sari_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)], sh);
5722     tcg_temp_free(t0);
5723     tcg_temp_free(t1);
5724     if (unlikely(Rc(ctx->opcode) != 0))
5725         gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
5726 }
5727
5728 /* sraq - sraq. */
5729 static void gen_sraq(DisasContext *ctx)
5730 {
5731     TCGLabel *l1 = gen_new_label();
5732     TCGLabel *l2 = gen_new_label();
5733     TCGv t0 = tcg_temp_new();
5734     TCGv t1 = tcg_temp_local_new();
5735     TCGv t2 = tcg_temp_local_new();
5736     tcg_gen_andi_tl(t2, cpu_gpr[rB(ctx->opcode)], 0x1F);
5737     tcg_gen_shr_tl(t0, cpu_gpr[rS(ctx->opcode)], t2);
5738     tcg_gen_sar_tl(t1, cpu_gpr[rS(ctx->opcode)], t2);
5739     tcg_gen_subfi_tl(t2, 32, t2);
5740     tcg_gen_shl_tl(t2, cpu_gpr[rS(ctx->opcode)], t2);
5741     tcg_gen_or_tl(t0, t0, t2);
5742     gen_store_spr(SPR_MQ, t0);
5743     tcg_gen_andi_tl(t0, cpu_gpr[rB(ctx->opcode)], 0x20);
5744     tcg_gen_brcondi_tl(TCG_COND_EQ, t2, 0, l1);
5745     tcg_gen_mov_tl(t2, cpu_gpr[rS(ctx->opcode)]);
5746     tcg_gen_sari_tl(t1, cpu_gpr[rS(ctx->opcode)], 31);
5747     gen_set_label(l1);
5748     tcg_temp_free(t0);
5749     tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], t1);
5750     tcg_gen_movi_tl(cpu_ca, 0);
5751     tcg_gen_brcondi_tl(TCG_COND_GE, t1, 0, l2);
5752     tcg_gen_brcondi_tl(TCG_COND_EQ, t2, 0, l2);
5753     tcg_gen_movi_tl(cpu_ca, 1);
5754     gen_set_label(l2);
5755     tcg_temp_free(t1);
5756     tcg_temp_free(t2);
5757     if (unlikely(Rc(ctx->opcode) != 0))
5758         gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
5759 }
5760
5761 /* sre - sre. */
5762 static void gen_sre(DisasContext *ctx)
5763 {
5764     TCGv t0 = tcg_temp_new();
5765     TCGv t1 = tcg_temp_new();
5766     tcg_gen_andi_tl(t1, cpu_gpr[rB(ctx->opcode)], 0x1F);
5767     tcg_gen_shr_tl(t0, cpu_gpr[rS(ctx->opcode)], t1);
5768     tcg_gen_subfi_tl(t1, 32, t1);
5769     tcg_gen_shl_tl(t1, cpu_gpr[rS(ctx->opcode)], t1);
5770     tcg_gen_or_tl(t1, t0, t1);
5771     tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], t0);
5772     gen_store_spr(SPR_MQ, t1);
5773     tcg_temp_free(t0);
5774     tcg_temp_free(t1);
5775     if (unlikely(Rc(ctx->opcode) != 0))
5776         gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
5777 }
5778
5779 /* srea - srea. */
5780 static void gen_srea(DisasContext *ctx)
5781 {
5782     TCGv t0 = tcg_temp_new();
5783     TCGv t1 = tcg_temp_new();
5784     tcg_gen_andi_tl(t1, cpu_gpr[rB(ctx->opcode)], 0x1F);
5785     tcg_gen_rotr_tl(t0, cpu_gpr[rS(ctx->opcode)], t1);
5786     gen_store_spr(SPR_MQ, t0);
5787     tcg_gen_sar_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)], t1);
5788     tcg_temp_free(t0);
5789     tcg_temp_free(t1);
5790     if (unlikely(Rc(ctx->opcode) != 0))
5791         gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
5792 }
5793
5794 /* sreq */
5795 static void gen_sreq(DisasContext *ctx)
5796 {
5797     TCGv t0 = tcg_temp_new();
5798     TCGv t1 = tcg_temp_new();
5799     TCGv t2 = tcg_temp_new();
5800     tcg_gen_andi_tl(t0, cpu_gpr[rB(ctx->opcode)], 0x1F);
5801     tcg_gen_movi_tl(t1, 0xFFFFFFFF);
5802     tcg_gen_shr_tl(t1, t1, t0);
5803     tcg_gen_rotr_tl(t0, cpu_gpr[rS(ctx->opcode)], t0);
5804     gen_load_spr(t2, SPR_MQ);
5805     gen_store_spr(SPR_MQ, t0);
5806     tcg_gen_and_tl(t0, t0, t1);
5807     tcg_gen_andc_tl(t2, t2, t1);
5808     tcg_gen_or_tl(cpu_gpr[rA(ctx->opcode)], t0, t2);
5809     tcg_temp_free(t0);
5810     tcg_temp_free(t1);
5811     tcg_temp_free(t2);
5812     if (unlikely(Rc(ctx->opcode) != 0))
5813         gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
5814 }
5815
5816 /* sriq */
5817 static void gen_sriq(DisasContext *ctx)
5818 {
5819     int sh = SH(ctx->opcode);
5820     TCGv t0 = tcg_temp_new();
5821     TCGv t1 = tcg_temp_new();
5822     tcg_gen_shri_tl(t0, cpu_gpr[rS(ctx->opcode)], sh);
5823     tcg_gen_shli_tl(t1, cpu_gpr[rS(ctx->opcode)], 32 - sh);
5824     tcg_gen_or_tl(t1, t0, t1);
5825     tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], t0);
5826     gen_store_spr(SPR_MQ, t1);
5827     tcg_temp_free(t0);
5828     tcg_temp_free(t1);
5829     if (unlikely(Rc(ctx->opcode) != 0))
5830         gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
5831 }
5832
5833 /* srliq */
5834 static void gen_srliq(DisasContext *ctx)
5835 {
5836     int sh = SH(ctx->opcode);
5837     TCGv t0 = tcg_temp_new();
5838     TCGv t1 = tcg_temp_new();
5839     tcg_gen_rotri_tl(t0, cpu_gpr[rS(ctx->opcode)], sh);
5840     gen_load_spr(t1, SPR_MQ);
5841     gen_store_spr(SPR_MQ, t0);
5842     tcg_gen_andi_tl(t0, t0,  (0xFFFFFFFFU >> sh));
5843     tcg_gen_andi_tl(t1, t1, ~(0xFFFFFFFFU >> sh));
5844     tcg_gen_or_tl(cpu_gpr[rA(ctx->opcode)], t0, t1);
5845     tcg_temp_free(t0);
5846     tcg_temp_free(t1);
5847     if (unlikely(Rc(ctx->opcode) != 0))
5848         gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
5849 }
5850
5851 /* srlq */
5852 static void gen_srlq(DisasContext *ctx)
5853 {
5854     TCGLabel *l1 = gen_new_label();
5855     TCGLabel *l2 = gen_new_label();
5856     TCGv t0 = tcg_temp_local_new();
5857     TCGv t1 = tcg_temp_local_new();
5858     TCGv t2 = tcg_temp_local_new();
5859     tcg_gen_andi_tl(t2, cpu_gpr[rB(ctx->opcode)], 0x1F);
5860     tcg_gen_movi_tl(t1, 0xFFFFFFFF);
5861     tcg_gen_shr_tl(t2, t1, t2);
5862     tcg_gen_andi_tl(t0, cpu_gpr[rB(ctx->opcode)], 0x20);
5863     tcg_gen_brcondi_tl(TCG_COND_EQ, t0, 0, l1);
5864     gen_load_spr(t0, SPR_MQ);
5865     tcg_gen_and_tl(cpu_gpr[rA(ctx->opcode)], t0, t2);
5866     tcg_gen_br(l2);
5867     gen_set_label(l1);
5868     tcg_gen_shr_tl(t0, cpu_gpr[rS(ctx->opcode)], t2);
5869     tcg_gen_and_tl(t0, t0, t2);
5870     gen_load_spr(t1, SPR_MQ);
5871     tcg_gen_andc_tl(t1, t1, t2);
5872     tcg_gen_or_tl(cpu_gpr[rA(ctx->opcode)], t0, t1);
5873     gen_set_label(l2);
5874     tcg_temp_free(t0);
5875     tcg_temp_free(t1);
5876     tcg_temp_free(t2);
5877     if (unlikely(Rc(ctx->opcode) != 0))
5878         gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
5879 }
5880
5881 /* srq */
5882 static void gen_srq(DisasContext *ctx)
5883 {
5884     TCGLabel *l1 = gen_new_label();
5885     TCGv t0 = tcg_temp_new();
5886     TCGv t1 = tcg_temp_new();
5887     tcg_gen_andi_tl(t1, cpu_gpr[rB(ctx->opcode)], 0x1F);
5888     tcg_gen_shr_tl(t0, cpu_gpr[rS(ctx->opcode)], t1);
5889     tcg_gen_subfi_tl(t1, 32, t1);
5890     tcg_gen_shl_tl(t1, cpu_gpr[rS(ctx->opcode)], t1);
5891     tcg_gen_or_tl(t1, t0, t1);
5892     gen_store_spr(SPR_MQ, t1);
5893     tcg_gen_andi_tl(t1, cpu_gpr[rB(ctx->opcode)], 0x20);
5894     tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], t0);
5895     tcg_gen_brcondi_tl(TCG_COND_EQ, t0, 0, l1);
5896     tcg_gen_movi_tl(cpu_gpr[rA(ctx->opcode)], 0);
5897     gen_set_label(l1);
5898     tcg_temp_free(t0);
5899     tcg_temp_free(t1);
5900     if (unlikely(Rc(ctx->opcode) != 0))
5901         gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
5902 }
5903
5904 /* PowerPC 602 specific instructions */
5905
5906 /* dsa  */
5907 static void gen_dsa(DisasContext *ctx)
5908 {
5909     /* XXX: TODO */
5910     gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL);
5911 }
5912
5913 /* esa */
5914 static void gen_esa(DisasContext *ctx)
5915 {
5916     /* XXX: TODO */
5917     gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL);
5918 }
5919
5920 /* mfrom */
5921 static void gen_mfrom(DisasContext *ctx)
5922 {
5923 #if defined(CONFIG_USER_ONLY)
5924     GEN_PRIV;
5925 #else
5926     CHK_SV;
5927     gen_helper_602_mfrom(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)]);
5928 #endif /* defined(CONFIG_USER_ONLY) */
5929 }
5930
5931 /* 602 - 603 - G2 TLB management */
5932
5933 /* tlbld */
5934 static void gen_tlbld_6xx(DisasContext *ctx)
5935 {
5936 #if defined(CONFIG_USER_ONLY)
5937     GEN_PRIV;
5938 #else
5939     CHK_SV;
5940     gen_helper_6xx_tlbd(cpu_env, cpu_gpr[rB(ctx->opcode)]);
5941 #endif /* defined(CONFIG_USER_ONLY) */
5942 }
5943
5944 /* tlbli */
5945 static void gen_tlbli_6xx(DisasContext *ctx)
5946 {
5947 #if defined(CONFIG_USER_ONLY)
5948     GEN_PRIV;
5949 #else
5950     CHK_SV;
5951     gen_helper_6xx_tlbi(cpu_env, cpu_gpr[rB(ctx->opcode)]);
5952 #endif /* defined(CONFIG_USER_ONLY) */
5953 }
5954
5955 /* 74xx TLB management */
5956
5957 /* tlbld */
5958 static void gen_tlbld_74xx(DisasContext *ctx)
5959 {
5960 #if defined(CONFIG_USER_ONLY)
5961     GEN_PRIV;
5962 #else
5963     CHK_SV;
5964     gen_helper_74xx_tlbd(cpu_env, cpu_gpr[rB(ctx->opcode)]);
5965 #endif /* defined(CONFIG_USER_ONLY) */
5966 }
5967
5968 /* tlbli */
5969 static void gen_tlbli_74xx(DisasContext *ctx)
5970 {
5971 #if defined(CONFIG_USER_ONLY)
5972     GEN_PRIV;
5973 #else
5974     CHK_SV;
5975     gen_helper_74xx_tlbi(cpu_env, cpu_gpr[rB(ctx->opcode)]);
5976 #endif /* defined(CONFIG_USER_ONLY) */
5977 }
5978
5979 /* POWER instructions not in PowerPC 601 */
5980
5981 /* clf */
5982 static void gen_clf(DisasContext *ctx)
5983 {
5984     /* Cache line flush: implemented as no-op */
5985 }
5986
5987 /* cli */
5988 static void gen_cli(DisasContext *ctx)
5989 {
5990 #if defined(CONFIG_USER_ONLY)
5991     GEN_PRIV;
5992 #else
5993     /* Cache line invalidate: privileged and treated as no-op */
5994     CHK_SV;
5995 #endif /* defined(CONFIG_USER_ONLY) */
5996 }
5997
5998 /* dclst */
5999 static void gen_dclst(DisasContext *ctx)
6000 {
6001     /* Data cache line store: treated as no-op */
6002 }
6003
6004 static void gen_mfsri(DisasContext *ctx)
6005 {
6006 #if defined(CONFIG_USER_ONLY)
6007     GEN_PRIV;
6008 #else
6009     int ra = rA(ctx->opcode);
6010     int rd = rD(ctx->opcode);
6011     TCGv t0;
6012
6013     CHK_SV;
6014     t0 = tcg_temp_new();
6015     gen_addr_reg_index(ctx, t0);
6016     tcg_gen_shri_tl(t0, t0, 28);
6017     tcg_gen_andi_tl(t0, t0, 0xF);
6018     gen_helper_load_sr(cpu_gpr[rd], cpu_env, t0);
6019     tcg_temp_free(t0);
6020     if (ra != 0 && ra != rd)
6021         tcg_gen_mov_tl(cpu_gpr[ra], cpu_gpr[rd]);
6022 #endif /* defined(CONFIG_USER_ONLY) */
6023 }
6024
6025 static void gen_rac(DisasContext *ctx)
6026 {
6027 #if defined(CONFIG_USER_ONLY)
6028     GEN_PRIV;
6029 #else
6030     TCGv t0;
6031
6032     CHK_SV;
6033     t0 = tcg_temp_new();
6034     gen_addr_reg_index(ctx, t0);
6035     gen_helper_rac(cpu_gpr[rD(ctx->opcode)], cpu_env, t0);
6036     tcg_temp_free(t0);
6037 #endif /* defined(CONFIG_USER_ONLY) */
6038 }
6039
6040 static void gen_rfsvc(DisasContext *ctx)
6041 {
6042 #if defined(CONFIG_USER_ONLY)
6043     GEN_PRIV;
6044 #else
6045     CHK_SV;
6046
6047     gen_helper_rfsvc(cpu_env);
6048     gen_sync_exception(ctx);
6049 #endif /* defined(CONFIG_USER_ONLY) */
6050 }
6051
6052 /* svc is not implemented for now */
6053
6054 /* POWER2 specific instructions */
6055 /* Quad manipulation (load/store two floats at a time) */
6056
6057 /* lfq */
6058 static void gen_lfq(DisasContext *ctx)
6059 {
6060     int rd = rD(ctx->opcode);
6061     TCGv t0;
6062     gen_set_access_type(ctx, ACCESS_FLOAT);
6063     t0 = tcg_temp_new();
6064     gen_addr_imm_index(ctx, t0, 0);
6065     gen_qemu_ld64(ctx, cpu_fpr[rd], t0);
6066     gen_addr_add(ctx, t0, t0, 8);
6067     gen_qemu_ld64(ctx, cpu_fpr[(rd + 1) % 32], t0);
6068     tcg_temp_free(t0);
6069 }
6070
6071 /* lfqu */
6072 static void gen_lfqu(DisasContext *ctx)
6073 {
6074     int ra = rA(ctx->opcode);
6075     int rd = rD(ctx->opcode);
6076     TCGv t0, t1;
6077     gen_set_access_type(ctx, ACCESS_FLOAT);
6078     t0 = tcg_temp_new();
6079     t1 = tcg_temp_new();
6080     gen_addr_imm_index(ctx, t0, 0);
6081     gen_qemu_ld64(ctx, cpu_fpr[rd], t0);
6082     gen_addr_add(ctx, t1, t0, 8);
6083     gen_qemu_ld64(ctx, cpu_fpr[(rd + 1) % 32], t1);
6084     if (ra != 0)
6085         tcg_gen_mov_tl(cpu_gpr[ra], t0);
6086     tcg_temp_free(t0);
6087     tcg_temp_free(t1);
6088 }
6089
6090 /* lfqux */
6091 static void gen_lfqux(DisasContext *ctx)
6092 {
6093     int ra = rA(ctx->opcode);
6094     int rd = rD(ctx->opcode);
6095     gen_set_access_type(ctx, ACCESS_FLOAT);
6096     TCGv t0, t1;
6097     t0 = tcg_temp_new();
6098     gen_addr_reg_index(ctx, t0);
6099     gen_qemu_ld64(ctx, cpu_fpr[rd], t0);
6100     t1 = tcg_temp_new();
6101     gen_addr_add(ctx, t1, t0, 8);
6102     gen_qemu_ld64(ctx, cpu_fpr[(rd + 1) % 32], t1);
6103     tcg_temp_free(t1);
6104     if (ra != 0)
6105         tcg_gen_mov_tl(cpu_gpr[ra], t0);
6106     tcg_temp_free(t0);
6107 }
6108
6109 /* lfqx */
6110 static void gen_lfqx(DisasContext *ctx)
6111 {
6112     int rd = rD(ctx->opcode);
6113     TCGv t0;
6114     gen_set_access_type(ctx, ACCESS_FLOAT);
6115     t0 = tcg_temp_new();
6116     gen_addr_reg_index(ctx, t0);
6117     gen_qemu_ld64(ctx, cpu_fpr[rd], t0);
6118     gen_addr_add(ctx, t0, t0, 8);
6119     gen_qemu_ld64(ctx, cpu_fpr[(rd + 1) % 32], t0);
6120     tcg_temp_free(t0);
6121 }
6122
6123 /* stfq */
6124 static void gen_stfq(DisasContext *ctx)
6125 {
6126     int rd = rD(ctx->opcode);
6127     TCGv t0;
6128     gen_set_access_type(ctx, ACCESS_FLOAT);
6129     t0 = tcg_temp_new();
6130     gen_addr_imm_index(ctx, t0, 0);
6131     gen_qemu_st64(ctx, cpu_fpr[rd], t0);
6132     gen_addr_add(ctx, t0, t0, 8);
6133     gen_qemu_st64(ctx, cpu_fpr[(rd + 1) % 32], t0);
6134     tcg_temp_free(t0);
6135 }
6136
6137 /* stfqu */
6138 static void gen_stfqu(DisasContext *ctx)
6139 {
6140     int ra = rA(ctx->opcode);
6141     int rd = rD(ctx->opcode);
6142     TCGv t0, t1;
6143     gen_set_access_type(ctx, ACCESS_FLOAT);
6144     t0 = tcg_temp_new();
6145     gen_addr_imm_index(ctx, t0, 0);
6146     gen_qemu_st64(ctx, cpu_fpr[rd], t0);
6147     t1 = tcg_temp_new();
6148     gen_addr_add(ctx, t1, t0, 8);
6149     gen_qemu_st64(ctx, cpu_fpr[(rd + 1) % 32], t1);
6150     tcg_temp_free(t1);
6151     if (ra != 0)
6152         tcg_gen_mov_tl(cpu_gpr[ra], t0);
6153     tcg_temp_free(t0);
6154 }
6155
6156 /* stfqux */
6157 static void gen_stfqux(DisasContext *ctx)
6158 {
6159     int ra = rA(ctx->opcode);
6160     int rd = rD(ctx->opcode);
6161     TCGv t0, t1;
6162     gen_set_access_type(ctx, ACCESS_FLOAT);
6163     t0 = tcg_temp_new();
6164     gen_addr_reg_index(ctx, t0);
6165     gen_qemu_st64(ctx, cpu_fpr[rd], t0);
6166     t1 = tcg_temp_new();
6167     gen_addr_add(ctx, t1, t0, 8);
6168     gen_qemu_st64(ctx, cpu_fpr[(rd + 1) % 32], t1);
6169     tcg_temp_free(t1);
6170     if (ra != 0)
6171         tcg_gen_mov_tl(cpu_gpr[ra], t0);
6172     tcg_temp_free(t0);
6173 }
6174
6175 /* stfqx */
6176 static void gen_stfqx(DisasContext *ctx)
6177 {
6178     int rd = rD(ctx->opcode);
6179     TCGv t0;
6180     gen_set_access_type(ctx, ACCESS_FLOAT);
6181     t0 = tcg_temp_new();
6182     gen_addr_reg_index(ctx, t0);
6183     gen_qemu_st64(ctx, cpu_fpr[rd], t0);
6184     gen_addr_add(ctx, t0, t0, 8);
6185     gen_qemu_st64(ctx, cpu_fpr[(rd + 1) % 32], t0);
6186     tcg_temp_free(t0);
6187 }
6188
6189 /* BookE specific instructions */
6190
6191 /* XXX: not implemented on 440 ? */
6192 static void gen_mfapidi(DisasContext *ctx)
6193 {
6194     /* XXX: TODO */
6195     gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL);
6196 }
6197
6198 /* XXX: not implemented on 440 ? */
6199 static void gen_tlbiva(DisasContext *ctx)
6200 {
6201 #if defined(CONFIG_USER_ONLY)
6202     GEN_PRIV;
6203 #else
6204     TCGv t0;
6205
6206     CHK_SV;
6207     t0 = tcg_temp_new();
6208     gen_addr_reg_index(ctx, t0);
6209     gen_helper_tlbiva(cpu_env, cpu_gpr[rB(ctx->opcode)]);
6210     tcg_temp_free(t0);
6211 #endif /* defined(CONFIG_USER_ONLY) */
6212 }
6213
6214 /* All 405 MAC instructions are translated here */
6215 static inline void gen_405_mulladd_insn(DisasContext *ctx, int opc2, int opc3,
6216                                         int ra, int rb, int rt, int Rc)
6217 {
6218     TCGv t0, t1;
6219
6220     t0 = tcg_temp_local_new();
6221     t1 = tcg_temp_local_new();
6222
6223     switch (opc3 & 0x0D) {
6224     case 0x05:
6225         /* macchw    - macchw.    - macchwo   - macchwo.   */
6226         /* macchws   - macchws.   - macchwso  - macchwso.  */
6227         /* nmacchw   - nmacchw.   - nmacchwo  - nmacchwo.  */
6228         /* nmacchws  - nmacchws.  - nmacchwso - nmacchwso. */
6229         /* mulchw - mulchw. */
6230         tcg_gen_ext16s_tl(t0, cpu_gpr[ra]);
6231         tcg_gen_sari_tl(t1, cpu_gpr[rb], 16);
6232         tcg_gen_ext16s_tl(t1, t1);
6233         break;
6234     case 0x04:
6235         /* macchwu   - macchwu.   - macchwuo  - macchwuo.  */
6236         /* macchwsu  - macchwsu.  - macchwsuo - macchwsuo. */
6237         /* mulchwu - mulchwu. */
6238         tcg_gen_ext16u_tl(t0, cpu_gpr[ra]);
6239         tcg_gen_shri_tl(t1, cpu_gpr[rb], 16);
6240         tcg_gen_ext16u_tl(t1, t1);
6241         break;
6242     case 0x01:
6243         /* machhw    - machhw.    - machhwo   - machhwo.   */
6244         /* machhws   - machhws.   - machhwso  - machhwso.  */
6245         /* nmachhw   - nmachhw.   - nmachhwo  - nmachhwo.  */
6246         /* nmachhws  - nmachhws.  - nmachhwso - nmachhwso. */
6247         /* mulhhw - mulhhw. */
6248         tcg_gen_sari_tl(t0, cpu_gpr[ra], 16);
6249         tcg_gen_ext16s_tl(t0, t0);
6250         tcg_gen_sari_tl(t1, cpu_gpr[rb], 16);
6251         tcg_gen_ext16s_tl(t1, t1);
6252         break;
6253     case 0x00:
6254         /* machhwu   - machhwu.   - machhwuo  - machhwuo.  */
6255         /* machhwsu  - machhwsu.  - machhwsuo - machhwsuo. */
6256         /* mulhhwu - mulhhwu. */
6257         tcg_gen_shri_tl(t0, cpu_gpr[ra], 16);
6258         tcg_gen_ext16u_tl(t0, t0);
6259         tcg_gen_shri_tl(t1, cpu_gpr[rb], 16);
6260         tcg_gen_ext16u_tl(t1, t1);
6261         break;
6262     case 0x0D:
6263         /* maclhw    - maclhw.    - maclhwo   - maclhwo.   */
6264         /* maclhws   - maclhws.   - maclhwso  - maclhwso.  */
6265         /* nmaclhw   - nmaclhw.   - nmaclhwo  - nmaclhwo.  */
6266         /* nmaclhws  - nmaclhws.  - nmaclhwso - nmaclhwso. */
6267         /* mullhw - mullhw. */
6268         tcg_gen_ext16s_tl(t0, cpu_gpr[ra]);
6269         tcg_gen_ext16s_tl(t1, cpu_gpr[rb]);
6270         break;
6271     case 0x0C:
6272         /* maclhwu   - maclhwu.   - maclhwuo  - maclhwuo.  */
6273         /* maclhwsu  - maclhwsu.  - maclhwsuo - maclhwsuo. */
6274         /* mullhwu - mullhwu. */
6275         tcg_gen_ext16u_tl(t0, cpu_gpr[ra]);
6276         tcg_gen_ext16u_tl(t1, cpu_gpr[rb]);
6277         break;
6278     }
6279     if (opc2 & 0x04) {
6280         /* (n)multiply-and-accumulate (0x0C / 0x0E) */
6281         tcg_gen_mul_tl(t1, t0, t1);
6282         if (opc2 & 0x02) {
6283             /* nmultiply-and-accumulate (0x0E) */
6284             tcg_gen_sub_tl(t0, cpu_gpr[rt], t1);
6285         } else {
6286             /* multiply-and-accumulate (0x0C) */
6287             tcg_gen_add_tl(t0, cpu_gpr[rt], t1);
6288         }
6289
6290         if (opc3 & 0x12) {
6291             /* Check overflow and/or saturate */
6292             TCGLabel *l1 = gen_new_label();
6293
6294             if (opc3 & 0x10) {
6295                 /* Start with XER OV disabled, the most likely case */
6296                 tcg_gen_movi_tl(cpu_ov, 0);
6297             }
6298             if (opc3 & 0x01) {
6299                 /* Signed */
6300                 tcg_gen_xor_tl(t1, cpu_gpr[rt], t1);
6301                 tcg_gen_brcondi_tl(TCG_COND_GE, t1, 0, l1);
6302                 tcg_gen_xor_tl(t1, cpu_gpr[rt], t0);
6303                 tcg_gen_brcondi_tl(TCG_COND_LT, t1, 0, l1);
6304                 if (opc3 & 0x02) {
6305                     /* Saturate */
6306                     tcg_gen_sari_tl(t0, cpu_gpr[rt], 31);
6307                     tcg_gen_xori_tl(t0, t0, 0x7fffffff);
6308                 }
6309             } else {
6310                 /* Unsigned */
6311                 tcg_gen_brcond_tl(TCG_COND_GEU, t0, t1, l1);
6312                 if (opc3 & 0x02) {
6313                     /* Saturate */
6314                     tcg_gen_movi_tl(t0, UINT32_MAX);
6315                 }
6316             }
6317             if (opc3 & 0x10) {
6318                 /* Check overflow */
6319                 tcg_gen_movi_tl(cpu_ov, 1);
6320                 tcg_gen_movi_tl(cpu_so, 1);
6321             }
6322             gen_set_label(l1);
6323             tcg_gen_mov_tl(cpu_gpr[rt], t0);
6324         }
6325     } else {
6326         tcg_gen_mul_tl(cpu_gpr[rt], t0, t1);
6327     }
6328     tcg_temp_free(t0);
6329     tcg_temp_free(t1);
6330     if (unlikely(Rc) != 0) {
6331         /* Update Rc0 */
6332         gen_set_Rc0(ctx, cpu_gpr[rt]);
6333     }
6334 }
6335
6336 #define GEN_MAC_HANDLER(name, opc2, opc3)                                     \
6337 static void glue(gen_, name)(DisasContext *ctx)                               \
6338 {                                                                             \
6339     gen_405_mulladd_insn(ctx, opc2, opc3, rA(ctx->opcode), rB(ctx->opcode),   \
6340                          rD(ctx->opcode), Rc(ctx->opcode));                   \
6341 }
6342
6343 /* macchw    - macchw.    */
6344 GEN_MAC_HANDLER(macchw, 0x0C, 0x05);
6345 /* macchwo   - macchwo.   */
6346 GEN_MAC_HANDLER(macchwo, 0x0C, 0x15);
6347 /* macchws   - macchws.   */
6348 GEN_MAC_HANDLER(macchws, 0x0C, 0x07);
6349 /* macchwso  - macchwso.  */
6350 GEN_MAC_HANDLER(macchwso, 0x0C, 0x17);
6351 /* macchwsu  - macchwsu.  */
6352 GEN_MAC_HANDLER(macchwsu, 0x0C, 0x06);
6353 /* macchwsuo - macchwsuo. */
6354 GEN_MAC_HANDLER(macchwsuo, 0x0C, 0x16);
6355 /* macchwu   - macchwu.   */
6356 GEN_MAC_HANDLER(macchwu, 0x0C, 0x04);
6357 /* macchwuo  - macchwuo.  */
6358 GEN_MAC_HANDLER(macchwuo, 0x0C, 0x14);
6359 /* machhw    - machhw.    */
6360 GEN_MAC_HANDLER(machhw, 0x0C, 0x01);
6361 /* machhwo   - machhwo.   */
6362 GEN_MAC_HANDLER(machhwo, 0x0C, 0x11);
6363 /* machhws   - machhws.   */
6364 GEN_MAC_HANDLER(machhws, 0x0C, 0x03);
6365 /* machhwso  - machhwso.  */
6366 GEN_MAC_HANDLER(machhwso, 0x0C, 0x13);
6367 /* machhwsu  - machhwsu.  */
6368 GEN_MAC_HANDLER(machhwsu, 0x0C, 0x02);
6369 /* machhwsuo - machhwsuo. */
6370 GEN_MAC_HANDLER(machhwsuo, 0x0C, 0x12);
6371 /* machhwu   - machhwu.   */
6372 GEN_MAC_HANDLER(machhwu, 0x0C, 0x00);
6373 /* machhwuo  - machhwuo.  */
6374 GEN_MAC_HANDLER(machhwuo, 0x0C, 0x10);
6375 /* maclhw    - maclhw.    */
6376 GEN_MAC_HANDLER(maclhw, 0x0C, 0x0D);
6377 /* maclhwo   - maclhwo.   */
6378 GEN_MAC_HANDLER(maclhwo, 0x0C, 0x1D);
6379 /* maclhws   - maclhws.   */
6380 GEN_MAC_HANDLER(maclhws, 0x0C, 0x0F);
6381 /* maclhwso  - maclhwso.  */
6382 GEN_MAC_HANDLER(maclhwso, 0x0C, 0x1F);
6383 /* maclhwu   - maclhwu.   */
6384 GEN_MAC_HANDLER(maclhwu, 0x0C, 0x0C);
6385 /* maclhwuo  - maclhwuo.  */
6386 GEN_MAC_HANDLER(maclhwuo, 0x0C, 0x1C);
6387 /* maclhwsu  - maclhwsu.  */
6388 GEN_MAC_HANDLER(maclhwsu, 0x0C, 0x0E);
6389 /* maclhwsuo - maclhwsuo. */
6390 GEN_MAC_HANDLER(maclhwsuo, 0x0C, 0x1E);
6391 /* nmacchw   - nmacchw.   */
6392 GEN_MAC_HANDLER(nmacchw, 0x0E, 0x05);
6393 /* nmacchwo  - nmacchwo.  */
6394 GEN_MAC_HANDLER(nmacchwo, 0x0E, 0x15);
6395 /* nmacchws  - nmacchws.  */
6396 GEN_MAC_HANDLER(nmacchws, 0x0E, 0x07);
6397 /* nmacchwso - nmacchwso. */
6398 GEN_MAC_HANDLER(nmacchwso, 0x0E, 0x17);
6399 /* nmachhw   - nmachhw.   */
6400 GEN_MAC_HANDLER(nmachhw, 0x0E, 0x01);
6401 /* nmachhwo  - nmachhwo.  */
6402 GEN_MAC_HANDLER(nmachhwo, 0x0E, 0x11);
6403 /* nmachhws  - nmachhws.  */
6404 GEN_MAC_HANDLER(nmachhws, 0x0E, 0x03);
6405 /* nmachhwso - nmachhwso. */
6406 GEN_MAC_HANDLER(nmachhwso, 0x0E, 0x13);
6407 /* nmaclhw   - nmaclhw.   */
6408 GEN_MAC_HANDLER(nmaclhw, 0x0E, 0x0D);
6409 /* nmaclhwo  - nmaclhwo.  */
6410 GEN_MAC_HANDLER(nmaclhwo, 0x0E, 0x1D);
6411 /* nmaclhws  - nmaclhws.  */
6412 GEN_MAC_HANDLER(nmaclhws, 0x0E, 0x0F);
6413 /* nmaclhwso - nmaclhwso. */
6414 GEN_MAC_HANDLER(nmaclhwso, 0x0E, 0x1F);
6415
6416 /* mulchw  - mulchw.  */
6417 GEN_MAC_HANDLER(mulchw, 0x08, 0x05);
6418 /* mulchwu - mulchwu. */
6419 GEN_MAC_HANDLER(mulchwu, 0x08, 0x04);
6420 /* mulhhw  - mulhhw.  */
6421 GEN_MAC_HANDLER(mulhhw, 0x08, 0x01);
6422 /* mulhhwu - mulhhwu. */
6423 GEN_MAC_HANDLER(mulhhwu, 0x08, 0x00);
6424 /* mullhw  - mullhw.  */
6425 GEN_MAC_HANDLER(mullhw, 0x08, 0x0D);
6426 /* mullhwu - mullhwu. */
6427 GEN_MAC_HANDLER(mullhwu, 0x08, 0x0C);
6428
6429 /* mfdcr */
6430 static void gen_mfdcr(DisasContext *ctx)
6431 {
6432 #if defined(CONFIG_USER_ONLY)
6433     GEN_PRIV;
6434 #else
6435     TCGv dcrn;
6436
6437     CHK_SV;
6438     /* NIP cannot be restored if the memory exception comes from an helper */
6439     gen_update_nip(ctx, ctx->nip - 4);
6440     dcrn = tcg_const_tl(SPR(ctx->opcode));
6441     gen_helper_load_dcr(cpu_gpr[rD(ctx->opcode)], cpu_env, dcrn);
6442     tcg_temp_free(dcrn);
6443 #endif /* defined(CONFIG_USER_ONLY) */
6444 }
6445
6446 /* mtdcr */
6447 static void gen_mtdcr(DisasContext *ctx)
6448 {
6449 #if defined(CONFIG_USER_ONLY)
6450     GEN_PRIV;
6451 #else
6452     TCGv dcrn;
6453
6454     CHK_SV;
6455     /* NIP cannot be restored if the memory exception comes from an helper */
6456     gen_update_nip(ctx, ctx->nip - 4);
6457     dcrn = tcg_const_tl(SPR(ctx->opcode));
6458     gen_helper_store_dcr(cpu_env, dcrn, cpu_gpr[rS(ctx->opcode)]);
6459     tcg_temp_free(dcrn);
6460 #endif /* defined(CONFIG_USER_ONLY) */
6461 }
6462
6463 /* mfdcrx */
6464 /* XXX: not implemented on 440 ? */
6465 static void gen_mfdcrx(DisasContext *ctx)
6466 {
6467 #if defined(CONFIG_USER_ONLY)
6468     GEN_PRIV;
6469 #else
6470     CHK_SV;
6471     /* NIP cannot be restored if the memory exception comes from an helper */
6472     gen_update_nip(ctx, ctx->nip - 4);
6473     gen_helper_load_dcr(cpu_gpr[rD(ctx->opcode)], cpu_env,
6474                         cpu_gpr[rA(ctx->opcode)]);
6475     /* Note: Rc update flag set leads to undefined state of Rc0 */
6476 #endif /* defined(CONFIG_USER_ONLY) */
6477 }
6478
6479 /* mtdcrx */
6480 /* XXX: not implemented on 440 ? */
6481 static void gen_mtdcrx(DisasContext *ctx)
6482 {
6483 #if defined(CONFIG_USER_ONLY)
6484     GEN_PRIV;
6485 #else
6486     CHK_SV;
6487     /* NIP cannot be restored if the memory exception comes from an helper */
6488     gen_update_nip(ctx, ctx->nip - 4);
6489     gen_helper_store_dcr(cpu_env, cpu_gpr[rA(ctx->opcode)],
6490                          cpu_gpr[rS(ctx->opcode)]);
6491     /* Note: Rc update flag set leads to undefined state of Rc0 */
6492 #endif /* defined(CONFIG_USER_ONLY) */
6493 }
6494
6495 /* mfdcrux (PPC 460) : user-mode access to DCR */
6496 static void gen_mfdcrux(DisasContext *ctx)
6497 {
6498     /* NIP cannot be restored if the memory exception comes from an helper */
6499     gen_update_nip(ctx, ctx->nip - 4);
6500     gen_helper_load_dcr(cpu_gpr[rD(ctx->opcode)], cpu_env,
6501                         cpu_gpr[rA(ctx->opcode)]);
6502     /* Note: Rc update flag set leads to undefined state of Rc0 */
6503 }
6504
6505 /* mtdcrux (PPC 460) : user-mode access to DCR */
6506 static void gen_mtdcrux(DisasContext *ctx)
6507 {
6508     /* NIP cannot be restored if the memory exception comes from an helper */
6509     gen_update_nip(ctx, ctx->nip - 4);
6510     gen_helper_store_dcr(cpu_env, cpu_gpr[rA(ctx->opcode)],
6511                          cpu_gpr[rS(ctx->opcode)]);
6512     /* Note: Rc update flag set leads to undefined state of Rc0 */
6513 }
6514
6515 /* dccci */
6516 static void gen_dccci(DisasContext *ctx)
6517 {
6518     CHK_SV;
6519     /* interpreted as no-op */
6520 }
6521
6522 /* dcread */
6523 static void gen_dcread(DisasContext *ctx)
6524 {
6525 #if defined(CONFIG_USER_ONLY)
6526     GEN_PRIV;
6527 #else
6528     TCGv EA, val;
6529
6530     CHK_SV;
6531     gen_set_access_type(ctx, ACCESS_CACHE);
6532     EA = tcg_temp_new();
6533     gen_addr_reg_index(ctx, EA);
6534     val = tcg_temp_new();
6535     gen_qemu_ld32u(ctx, val, EA);
6536     tcg_temp_free(val);
6537     tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], EA);
6538     tcg_temp_free(EA);
6539 #endif /* defined(CONFIG_USER_ONLY) */
6540 }
6541
6542 /* icbt */
6543 static void gen_icbt_40x(DisasContext *ctx)
6544 {
6545     /* interpreted as no-op */
6546     /* XXX: specification say this is treated as a load by the MMU
6547      *      but does not generate any exception
6548      */
6549 }
6550
6551 /* iccci */
6552 static void gen_iccci(DisasContext *ctx)
6553 {
6554     CHK_SV;
6555     /* interpreted as no-op */
6556 }
6557
6558 /* icread */
6559 static void gen_icread(DisasContext *ctx)
6560 {
6561     CHK_SV;
6562     /* interpreted as no-op */
6563 }
6564
6565 /* rfci (supervisor only) */
6566 static void gen_rfci_40x(DisasContext *ctx)
6567 {
6568 #if defined(CONFIG_USER_ONLY)
6569     GEN_PRIV;
6570 #else
6571     CHK_SV;
6572     /* Restore CPU state */
6573     gen_helper_40x_rfci(cpu_env);
6574     gen_sync_exception(ctx);
6575 #endif /* defined(CONFIG_USER_ONLY) */
6576 }
6577
6578 static void gen_rfci(DisasContext *ctx)
6579 {
6580 #if defined(CONFIG_USER_ONLY)
6581     GEN_PRIV;
6582 #else
6583     CHK_SV;
6584     /* Restore CPU state */
6585     gen_helper_rfci(cpu_env);
6586     gen_sync_exception(ctx);
6587 #endif /* defined(CONFIG_USER_ONLY) */
6588 }
6589
6590 /* BookE specific */
6591
6592 /* XXX: not implemented on 440 ? */
6593 static void gen_rfdi(DisasContext *ctx)
6594 {
6595 #if defined(CONFIG_USER_ONLY)
6596     GEN_PRIV;
6597 #else
6598     CHK_SV;
6599     /* Restore CPU state */
6600     gen_helper_rfdi(cpu_env);
6601     gen_sync_exception(ctx);
6602 #endif /* defined(CONFIG_USER_ONLY) */
6603 }
6604
6605 /* XXX: not implemented on 440 ? */
6606 static void gen_rfmci(DisasContext *ctx)
6607 {
6608 #if defined(CONFIG_USER_ONLY)
6609     GEN_PRIV;
6610 #else
6611     CHK_SV;
6612     /* Restore CPU state */
6613     gen_helper_rfmci(cpu_env);
6614     gen_sync_exception(ctx);
6615 #endif /* defined(CONFIG_USER_ONLY) */
6616 }
6617
6618 /* TLB management - PowerPC 405 implementation */
6619
6620 /* tlbre */
6621 static void gen_tlbre_40x(DisasContext *ctx)
6622 {
6623 #if defined(CONFIG_USER_ONLY)
6624     GEN_PRIV;
6625 #else
6626     CHK_SV;
6627     switch (rB(ctx->opcode)) {
6628     case 0:
6629         gen_helper_4xx_tlbre_hi(cpu_gpr[rD(ctx->opcode)], cpu_env,
6630                                 cpu_gpr[rA(ctx->opcode)]);
6631         break;
6632     case 1:
6633         gen_helper_4xx_tlbre_lo(cpu_gpr[rD(ctx->opcode)], cpu_env,
6634                                 cpu_gpr[rA(ctx->opcode)]);
6635         break;
6636     default:
6637         gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL);
6638         break;
6639     }
6640 #endif /* defined(CONFIG_USER_ONLY) */
6641 }
6642
6643 /* tlbsx - tlbsx. */
6644 static void gen_tlbsx_40x(DisasContext *ctx)
6645 {
6646 #if defined(CONFIG_USER_ONLY)
6647     GEN_PRIV;
6648 #else
6649     TCGv t0;
6650
6651     CHK_SV;
6652     t0 = tcg_temp_new();
6653     gen_addr_reg_index(ctx, t0);
6654     gen_helper_4xx_tlbsx(cpu_gpr[rD(ctx->opcode)], cpu_env, t0);
6655     tcg_temp_free(t0);
6656     if (Rc(ctx->opcode)) {
6657         TCGLabel *l1 = gen_new_label();
6658         tcg_gen_trunc_tl_i32(cpu_crf[0], cpu_so);
6659         tcg_gen_brcondi_tl(TCG_COND_EQ, cpu_gpr[rD(ctx->opcode)], -1, l1);
6660         tcg_gen_ori_i32(cpu_crf[0], cpu_crf[0], 0x02);
6661         gen_set_label(l1);
6662     }
6663 #endif /* defined(CONFIG_USER_ONLY) */
6664 }
6665
6666 /* tlbwe */
6667 static void gen_tlbwe_40x(DisasContext *ctx)
6668 {
6669 #if defined(CONFIG_USER_ONLY)
6670     GEN_PRIV;
6671 #else
6672     CHK_SV;
6673
6674     switch (rB(ctx->opcode)) {
6675     case 0:
6676         gen_helper_4xx_tlbwe_hi(cpu_env, cpu_gpr[rA(ctx->opcode)],
6677                                 cpu_gpr[rS(ctx->opcode)]);
6678         break;
6679     case 1:
6680         gen_helper_4xx_tlbwe_lo(cpu_env, cpu_gpr[rA(ctx->opcode)],
6681                                 cpu_gpr[rS(ctx->opcode)]);
6682         break;
6683     default:
6684         gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL);
6685         break;
6686     }
6687 #endif /* defined(CONFIG_USER_ONLY) */
6688 }
6689
6690 /* TLB management - PowerPC 440 implementation */
6691
6692 /* tlbre */
6693 static void gen_tlbre_440(DisasContext *ctx)
6694 {
6695 #if defined(CONFIG_USER_ONLY)
6696     GEN_PRIV;
6697 #else
6698     CHK_SV;
6699
6700     switch (rB(ctx->opcode)) {
6701     case 0:
6702     case 1:
6703     case 2:
6704         {
6705             TCGv_i32 t0 = tcg_const_i32(rB(ctx->opcode));
6706             gen_helper_440_tlbre(cpu_gpr[rD(ctx->opcode)], cpu_env,
6707                                  t0, cpu_gpr[rA(ctx->opcode)]);
6708             tcg_temp_free_i32(t0);
6709         }
6710         break;
6711     default:
6712         gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL);
6713         break;
6714     }
6715 #endif /* defined(CONFIG_USER_ONLY) */
6716 }
6717
6718 /* tlbsx - tlbsx. */
6719 static void gen_tlbsx_440(DisasContext *ctx)
6720 {
6721 #if defined(CONFIG_USER_ONLY)
6722     GEN_PRIV;
6723 #else
6724     TCGv t0;
6725
6726     CHK_SV;
6727     t0 = tcg_temp_new();
6728     gen_addr_reg_index(ctx, t0);
6729     gen_helper_440_tlbsx(cpu_gpr[rD(ctx->opcode)], cpu_env, t0);
6730     tcg_temp_free(t0);
6731     if (Rc(ctx->opcode)) {
6732         TCGLabel *l1 = gen_new_label();
6733         tcg_gen_trunc_tl_i32(cpu_crf[0], cpu_so);
6734         tcg_gen_brcondi_tl(TCG_COND_EQ, cpu_gpr[rD(ctx->opcode)], -1, l1);
6735         tcg_gen_ori_i32(cpu_crf[0], cpu_crf[0], 0x02);
6736         gen_set_label(l1);
6737     }
6738 #endif /* defined(CONFIG_USER_ONLY) */
6739 }
6740
6741 /* tlbwe */
6742 static void gen_tlbwe_440(DisasContext *ctx)
6743 {
6744 #if defined(CONFIG_USER_ONLY)
6745     GEN_PRIV;
6746 #else
6747     CHK_SV;
6748     switch (rB(ctx->opcode)) {
6749     case 0:
6750     case 1:
6751     case 2:
6752         {
6753             TCGv_i32 t0 = tcg_const_i32(rB(ctx->opcode));
6754             gen_helper_440_tlbwe(cpu_env, t0, cpu_gpr[rA(ctx->opcode)],
6755                                  cpu_gpr[rS(ctx->opcode)]);
6756             tcg_temp_free_i32(t0);
6757         }
6758         break;
6759     default:
6760         gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL);
6761         break;
6762     }
6763 #endif /* defined(CONFIG_USER_ONLY) */
6764 }
6765
6766 /* TLB management - PowerPC BookE 2.06 implementation */
6767
6768 /* tlbre */
6769 static void gen_tlbre_booke206(DisasContext *ctx)
6770 {
6771  #if defined(CONFIG_USER_ONLY)
6772     GEN_PRIV;
6773 #else
6774    CHK_SV;
6775     gen_helper_booke206_tlbre(cpu_env);
6776 #endif /* defined(CONFIG_USER_ONLY) */
6777 }
6778
6779 /* tlbsx - tlbsx. */
6780 static void gen_tlbsx_booke206(DisasContext *ctx)
6781 {
6782 #if defined(CONFIG_USER_ONLY)
6783     GEN_PRIV;
6784 #else
6785     TCGv t0;
6786
6787     CHK_SV;
6788     if (rA(ctx->opcode)) {
6789         t0 = tcg_temp_new();
6790         tcg_gen_mov_tl(t0, cpu_gpr[rD(ctx->opcode)]);
6791     } else {
6792         t0 = tcg_const_tl(0);
6793     }
6794
6795     tcg_gen_add_tl(t0, t0, cpu_gpr[rB(ctx->opcode)]);
6796     gen_helper_booke206_tlbsx(cpu_env, t0);
6797     tcg_temp_free(t0);
6798 #endif /* defined(CONFIG_USER_ONLY) */
6799 }
6800
6801 /* tlbwe */
6802 static void gen_tlbwe_booke206(DisasContext *ctx)
6803 {
6804 #if defined(CONFIG_USER_ONLY)
6805     GEN_PRIV;
6806 #else
6807     CHK_SV;
6808     gen_update_nip(ctx, ctx->nip - 4);
6809     gen_helper_booke206_tlbwe(cpu_env);
6810 #endif /* defined(CONFIG_USER_ONLY) */
6811 }
6812
6813 static void gen_tlbivax_booke206(DisasContext *ctx)
6814 {
6815 #if defined(CONFIG_USER_ONLY)
6816     GEN_PRIV;
6817 #else
6818     TCGv t0;
6819
6820     CHK_SV;
6821     t0 = tcg_temp_new();
6822     gen_addr_reg_index(ctx, t0);
6823     gen_helper_booke206_tlbivax(cpu_env, t0);
6824     tcg_temp_free(t0);
6825 #endif /* defined(CONFIG_USER_ONLY) */
6826 }
6827
6828 static void gen_tlbilx_booke206(DisasContext *ctx)
6829 {
6830 #if defined(CONFIG_USER_ONLY)
6831     GEN_PRIV;
6832 #else
6833     TCGv t0;
6834
6835     CHK_SV;
6836     t0 = tcg_temp_new();
6837     gen_addr_reg_index(ctx, t0);
6838
6839     switch((ctx->opcode >> 21) & 0x3) {
6840     case 0:
6841         gen_helper_booke206_tlbilx0(cpu_env, t0);
6842         break;
6843     case 1:
6844         gen_helper_booke206_tlbilx1(cpu_env, t0);
6845         break;
6846     case 3:
6847         gen_helper_booke206_tlbilx3(cpu_env, t0);
6848         break;
6849     default:
6850         gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL);
6851         break;
6852     }
6853
6854     tcg_temp_free(t0);
6855 #endif /* defined(CONFIG_USER_ONLY) */
6856 }
6857
6858
6859 /* wrtee */
6860 static void gen_wrtee(DisasContext *ctx)
6861 {
6862 #if defined(CONFIG_USER_ONLY)
6863     GEN_PRIV;
6864 #else
6865     TCGv t0;
6866
6867     CHK_SV;
6868     t0 = tcg_temp_new();
6869     tcg_gen_andi_tl(t0, cpu_gpr[rD(ctx->opcode)], (1 << MSR_EE));
6870     tcg_gen_andi_tl(cpu_msr, cpu_msr, ~(1 << MSR_EE));
6871     tcg_gen_or_tl(cpu_msr, cpu_msr, t0);
6872     tcg_temp_free(t0);
6873     /* Stop translation to have a chance to raise an exception
6874      * if we just set msr_ee to 1
6875      */
6876     gen_stop_exception(ctx);
6877 #endif /* defined(CONFIG_USER_ONLY) */
6878 }
6879
6880 /* wrteei */
6881 static void gen_wrteei(DisasContext *ctx)
6882 {
6883 #if defined(CONFIG_USER_ONLY)
6884     GEN_PRIV;
6885 #else
6886     CHK_SV;
6887     if (ctx->opcode & 0x00008000) {
6888         tcg_gen_ori_tl(cpu_msr, cpu_msr, (1 << MSR_EE));
6889         /* Stop translation to have a chance to raise an exception */
6890         gen_stop_exception(ctx);
6891     } else {
6892         tcg_gen_andi_tl(cpu_msr, cpu_msr, ~(1 << MSR_EE));
6893     }
6894 #endif /* defined(CONFIG_USER_ONLY) */
6895 }
6896
6897 /* PowerPC 440 specific instructions */
6898
6899 /* dlmzb */
6900 static void gen_dlmzb(DisasContext *ctx)
6901 {
6902     TCGv_i32 t0 = tcg_const_i32(Rc(ctx->opcode));
6903     gen_helper_dlmzb(cpu_gpr[rA(ctx->opcode)], cpu_env,
6904                      cpu_gpr[rS(ctx->opcode)], cpu_gpr[rB(ctx->opcode)], t0);
6905     tcg_temp_free_i32(t0);
6906 }
6907
6908 /* mbar replaces eieio on 440 */
6909 static void gen_mbar(DisasContext *ctx)
6910 {
6911     /* interpreted as no-op */
6912 }
6913
6914 /* msync replaces sync on 440 */
6915 static void gen_msync_4xx(DisasContext *ctx)
6916 {
6917     /* interpreted as no-op */
6918 }
6919
6920 /* icbt */
6921 static void gen_icbt_440(DisasContext *ctx)
6922 {
6923     /* interpreted as no-op */
6924     /* XXX: specification say this is treated as a load by the MMU
6925      *      but does not generate any exception
6926      */
6927 }
6928
6929 /* Embedded.Processor Control */
6930
6931 static void gen_msgclr(DisasContext *ctx)
6932 {
6933 #if defined(CONFIG_USER_ONLY)
6934     GEN_PRIV;
6935 #else
6936     CHK_SV;
6937     gen_helper_msgclr(cpu_env, cpu_gpr[rB(ctx->opcode)]);
6938 #endif /* defined(CONFIG_USER_ONLY) */
6939 }
6940
6941 static void gen_msgsnd(DisasContext *ctx)
6942 {
6943 #if defined(CONFIG_USER_ONLY)
6944     GEN_PRIV;
6945 #else
6946     CHK_SV;
6947     gen_helper_msgsnd(cpu_gpr[rB(ctx->opcode)]);
6948 #endif /* defined(CONFIG_USER_ONLY) */
6949 }
6950
6951 /***                      Altivec vector extension                         ***/
6952 /* Altivec registers moves */
6953
6954 static inline TCGv_ptr gen_avr_ptr(int reg)
6955 {
6956     TCGv_ptr r = tcg_temp_new_ptr();
6957     tcg_gen_addi_ptr(r, cpu_env, offsetof(CPUPPCState, avr[reg]));
6958     return r;
6959 }
6960
6961 #define GEN_VR_LDX(name, opc2, opc3)                                          \
6962 static void glue(gen_, name)(DisasContext *ctx)                                       \
6963 {                                                                             \
6964     TCGv EA;                                                                  \
6965     if (unlikely(!ctx->altivec_enabled)) {                                    \
6966         gen_exception(ctx, POWERPC_EXCP_VPU);                                 \
6967         return;                                                               \
6968     }                                                                         \
6969     gen_set_access_type(ctx, ACCESS_INT);                                     \
6970     EA = tcg_temp_new();                                                      \
6971     gen_addr_reg_index(ctx, EA);                                              \
6972     tcg_gen_andi_tl(EA, EA, ~0xf);                                            \
6973     /* We only need to swap high and low halves. gen_qemu_ld64 does necessary \
6974        64-bit byteswap already. */                                            \
6975     if (ctx->le_mode) {                                                       \
6976         gen_qemu_ld64(ctx, cpu_avrl[rD(ctx->opcode)], EA);                    \
6977         tcg_gen_addi_tl(EA, EA, 8);                                           \
6978         gen_qemu_ld64(ctx, cpu_avrh[rD(ctx->opcode)], EA);                    \
6979     } else {                                                                  \
6980         gen_qemu_ld64(ctx, cpu_avrh[rD(ctx->opcode)], EA);                    \
6981         tcg_gen_addi_tl(EA, EA, 8);                                           \
6982         gen_qemu_ld64(ctx, cpu_avrl[rD(ctx->opcode)], EA);                    \
6983     }                                                                         \
6984     tcg_temp_free(EA);                                                        \
6985 }
6986
6987 #define GEN_VR_STX(name, opc2, opc3)                                          \
6988 static void gen_st##name(DisasContext *ctx)                                   \
6989 {                                                                             \
6990     TCGv EA;                                                                  \
6991     if (unlikely(!ctx->altivec_enabled)) {                                    \
6992         gen_exception(ctx, POWERPC_EXCP_VPU);                                 \
6993         return;                                                               \
6994     }                                                                         \
6995     gen_set_access_type(ctx, ACCESS_INT);                                     \
6996     EA = tcg_temp_new();                                                      \
6997     gen_addr_reg_index(ctx, EA);                                              \
6998     tcg_gen_andi_tl(EA, EA, ~0xf);                                            \
6999     /* We only need to swap high and low halves. gen_qemu_st64 does necessary \
7000        64-bit byteswap already. */                                            \
7001     if (ctx->le_mode) {                                                       \
7002         gen_qemu_st64(ctx, cpu_avrl[rD(ctx->opcode)], EA);                    \
7003         tcg_gen_addi_tl(EA, EA, 8);                                           \
7004         gen_qemu_st64(ctx, cpu_avrh[rD(ctx->opcode)], EA);                    \
7005     } else {                                                                  \
7006         gen_qemu_st64(ctx, cpu_avrh[rD(ctx->opcode)], EA);                    \
7007         tcg_gen_addi_tl(EA, EA, 8);                                           \
7008         gen_qemu_st64(ctx, cpu_avrl[rD(ctx->opcode)], EA);                    \
7009     }                                                                         \
7010     tcg_temp_free(EA);                                                        \
7011 }
7012
7013 #define GEN_VR_LVE(name, opc2, opc3, size)                              \
7014 static void gen_lve##name(DisasContext *ctx)                            \
7015     {                                                                   \
7016         TCGv EA;                                                        \
7017         TCGv_ptr rs;                                                    \
7018         if (unlikely(!ctx->altivec_enabled)) {                          \
7019             gen_exception(ctx, POWERPC_EXCP_VPU);                       \
7020             return;                                                     \
7021         }                                                               \
7022         gen_set_access_type(ctx, ACCESS_INT);                           \
7023         EA = tcg_temp_new();                                            \
7024         gen_addr_reg_index(ctx, EA);                                    \
7025         if (size > 1) {                                                 \
7026             tcg_gen_andi_tl(EA, EA, ~(size - 1));                       \
7027         }                                                               \
7028         rs = gen_avr_ptr(rS(ctx->opcode));                              \
7029         gen_helper_lve##name(cpu_env, rs, EA);                          \
7030         tcg_temp_free(EA);                                              \
7031         tcg_temp_free_ptr(rs);                                          \
7032     }
7033
7034 #define GEN_VR_STVE(name, opc2, opc3, size)                             \
7035 static void gen_stve##name(DisasContext *ctx)                           \
7036     {                                                                   \
7037         TCGv EA;                                                        \
7038         TCGv_ptr rs;                                                    \
7039         if (unlikely(!ctx->altivec_enabled)) {                          \
7040             gen_exception(ctx, POWERPC_EXCP_VPU);                       \
7041             return;                                                     \
7042         }                                                               \
7043         gen_set_access_type(ctx, ACCESS_INT);                           \
7044         EA = tcg_temp_new();                                            \
7045         gen_addr_reg_index(ctx, EA);                                    \
7046         if (size > 1) {                                                 \
7047             tcg_gen_andi_tl(EA, EA, ~(size - 1));                       \
7048         }                                                               \
7049         rs = gen_avr_ptr(rS(ctx->opcode));                              \
7050         gen_helper_stve##name(cpu_env, rs, EA);                         \
7051         tcg_temp_free(EA);                                              \
7052         tcg_temp_free_ptr(rs);                                          \
7053     }
7054
7055 GEN_VR_LDX(lvx, 0x07, 0x03);
7056 /* As we don't emulate the cache, lvxl is stricly equivalent to lvx */
7057 GEN_VR_LDX(lvxl, 0x07, 0x0B);
7058
7059 GEN_VR_LVE(bx, 0x07, 0x00, 1);
7060 GEN_VR_LVE(hx, 0x07, 0x01, 2);
7061 GEN_VR_LVE(wx, 0x07, 0x02, 4);
7062
7063 GEN_VR_STX(svx, 0x07, 0x07);
7064 /* As we don't emulate the cache, stvxl is stricly equivalent to stvx */
7065 GEN_VR_STX(svxl, 0x07, 0x0F);
7066
7067 GEN_VR_STVE(bx, 0x07, 0x04, 1);
7068 GEN_VR_STVE(hx, 0x07, 0x05, 2);
7069 GEN_VR_STVE(wx, 0x07, 0x06, 4);
7070
7071 static void gen_lvsl(DisasContext *ctx)
7072 {
7073     TCGv_ptr rd;
7074     TCGv EA;
7075     if (unlikely(!ctx->altivec_enabled)) {
7076         gen_exception(ctx, POWERPC_EXCP_VPU);
7077         return;
7078     }
7079     EA = tcg_temp_new();
7080     gen_addr_reg_index(ctx, EA);
7081     rd = gen_avr_ptr(rD(ctx->opcode));
7082     gen_helper_lvsl(rd, EA);
7083     tcg_temp_free(EA);
7084     tcg_temp_free_ptr(rd);
7085 }
7086
7087 static void gen_lvsr(DisasContext *ctx)
7088 {
7089     TCGv_ptr rd;
7090     TCGv EA;
7091     if (unlikely(!ctx->altivec_enabled)) {
7092         gen_exception(ctx, POWERPC_EXCP_VPU);
7093         return;
7094     }
7095     EA = tcg_temp_new();
7096     gen_addr_reg_index(ctx, EA);
7097     rd = gen_avr_ptr(rD(ctx->opcode));
7098     gen_helper_lvsr(rd, EA);
7099     tcg_temp_free(EA);
7100     tcg_temp_free_ptr(rd);
7101 }
7102
7103 static void gen_mfvscr(DisasContext *ctx)
7104 {
7105     TCGv_i32 t;
7106     if (unlikely(!ctx->altivec_enabled)) {
7107         gen_exception(ctx, POWERPC_EXCP_VPU);
7108         return;
7109     }
7110     tcg_gen_movi_i64(cpu_avrh[rD(ctx->opcode)], 0);
7111     t = tcg_temp_new_i32();
7112     tcg_gen_ld_i32(t, cpu_env, offsetof(CPUPPCState, vscr));
7113     tcg_gen_extu_i32_i64(cpu_avrl[rD(ctx->opcode)], t);
7114     tcg_temp_free_i32(t);
7115 }
7116
7117 static void gen_mtvscr(DisasContext *ctx)
7118 {
7119     TCGv_ptr p;
7120     if (unlikely(!ctx->altivec_enabled)) {
7121         gen_exception(ctx, POWERPC_EXCP_VPU);
7122         return;
7123     }
7124     p = gen_avr_ptr(rB(ctx->opcode));
7125     gen_helper_mtvscr(cpu_env, p);
7126     tcg_temp_free_ptr(p);
7127 }
7128
7129 /* Logical operations */
7130 #define GEN_VX_LOGICAL(name, tcg_op, opc2, opc3)                        \
7131 static void glue(gen_, name)(DisasContext *ctx)                                 \
7132 {                                                                       \
7133     if (unlikely(!ctx->altivec_enabled)) {                              \
7134         gen_exception(ctx, POWERPC_EXCP_VPU);                           \
7135         return;                                                         \
7136     }                                                                   \
7137     tcg_op(cpu_avrh[rD(ctx->opcode)], cpu_avrh[rA(ctx->opcode)], cpu_avrh[rB(ctx->opcode)]); \
7138     tcg_op(cpu_avrl[rD(ctx->opcode)], cpu_avrl[rA(ctx->opcode)], cpu_avrl[rB(ctx->opcode)]); \
7139 }
7140
7141 GEN_VX_LOGICAL(vand, tcg_gen_and_i64, 2, 16);
7142 GEN_VX_LOGICAL(vandc, tcg_gen_andc_i64, 2, 17);
7143 GEN_VX_LOGICAL(vor, tcg_gen_or_i64, 2, 18);
7144 GEN_VX_LOGICAL(vxor, tcg_gen_xor_i64, 2, 19);
7145 GEN_VX_LOGICAL(vnor, tcg_gen_nor_i64, 2, 20);
7146 GEN_VX_LOGICAL(veqv, tcg_gen_eqv_i64, 2, 26);
7147 GEN_VX_LOGICAL(vnand, tcg_gen_nand_i64, 2, 22);
7148 GEN_VX_LOGICAL(vorc, tcg_gen_orc_i64, 2, 21);
7149
7150 #define GEN_VXFORM(name, opc2, opc3)                                    \
7151 static void glue(gen_, name)(DisasContext *ctx)                                 \
7152 {                                                                       \
7153     TCGv_ptr ra, rb, rd;                                                \
7154     if (unlikely(!ctx->altivec_enabled)) {                              \
7155         gen_exception(ctx, POWERPC_EXCP_VPU);                           \
7156         return;                                                         \
7157     }                                                                   \
7158     ra = gen_avr_ptr(rA(ctx->opcode));                                  \
7159     rb = gen_avr_ptr(rB(ctx->opcode));                                  \
7160     rd = gen_avr_ptr(rD(ctx->opcode));                                  \
7161     gen_helper_##name (rd, ra, rb);                                     \
7162     tcg_temp_free_ptr(ra);                                              \
7163     tcg_temp_free_ptr(rb);                                              \
7164     tcg_temp_free_ptr(rd);                                              \
7165 }
7166
7167 #define GEN_VXFORM_ENV(name, opc2, opc3)                                \
7168 static void glue(gen_, name)(DisasContext *ctx)                         \
7169 {                                                                       \
7170     TCGv_ptr ra, rb, rd;                                                \
7171     if (unlikely(!ctx->altivec_enabled)) {                              \
7172         gen_exception(ctx, POWERPC_EXCP_VPU);                           \
7173         return;                                                         \
7174     }                                                                   \
7175     ra = gen_avr_ptr(rA(ctx->opcode));                                  \
7176     rb = gen_avr_ptr(rB(ctx->opcode));                                  \
7177     rd = gen_avr_ptr(rD(ctx->opcode));                                  \
7178     gen_helper_##name(cpu_env, rd, ra, rb);                             \
7179     tcg_temp_free_ptr(ra);                                              \
7180     tcg_temp_free_ptr(rb);                                              \
7181     tcg_temp_free_ptr(rd);                                              \
7182 }
7183
7184 #define GEN_VXFORM3(name, opc2, opc3)                                   \
7185 static void glue(gen_, name)(DisasContext *ctx)                         \
7186 {                                                                       \
7187     TCGv_ptr ra, rb, rc, rd;                                            \
7188     if (unlikely(!ctx->altivec_enabled)) {                              \
7189         gen_exception(ctx, POWERPC_EXCP_VPU);                           \
7190         return;                                                         \
7191     }                                                                   \
7192     ra = gen_avr_ptr(rA(ctx->opcode));                                  \
7193     rb = gen_avr_ptr(rB(ctx->opcode));                                  \
7194     rc = gen_avr_ptr(rC(ctx->opcode));                                  \
7195     rd = gen_avr_ptr(rD(ctx->opcode));                                  \
7196     gen_helper_##name(rd, ra, rb, rc);                                  \
7197     tcg_temp_free_ptr(ra);                                              \
7198     tcg_temp_free_ptr(rb);                                              \
7199     tcg_temp_free_ptr(rc);                                              \
7200     tcg_temp_free_ptr(rd);                                              \
7201 }
7202
7203 /*
7204  * Support for Altivec instruction pairs that use bit 31 (Rc) as
7205  * an opcode bit.  In general, these pairs come from different
7206  * versions of the ISA, so we must also support a pair of flags for
7207  * each instruction.
7208  */
7209 #define GEN_VXFORM_DUAL(name0, flg0, flg2_0, name1, flg1, flg2_1)          \
7210 static void glue(gen_, name0##_##name1)(DisasContext *ctx)             \
7211 {                                                                      \
7212     if ((Rc(ctx->opcode) == 0) &&                                      \
7213         ((ctx->insns_flags & flg0) || (ctx->insns_flags2 & flg2_0))) { \
7214         gen_##name0(ctx);                                              \
7215     } else if ((Rc(ctx->opcode) == 1) &&                               \
7216         ((ctx->insns_flags & flg1) || (ctx->insns_flags2 & flg2_1))) { \
7217         gen_##name1(ctx);                                              \
7218     } else {                                                           \
7219         gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL);            \
7220     }                                                                  \
7221 }
7222
7223 GEN_VXFORM(vaddubm, 0, 0);
7224 GEN_VXFORM(vadduhm, 0, 1);
7225 GEN_VXFORM(vadduwm, 0, 2);
7226 GEN_VXFORM(vaddudm, 0, 3);
7227 GEN_VXFORM(vsububm, 0, 16);
7228 GEN_VXFORM(vsubuhm, 0, 17);
7229 GEN_VXFORM(vsubuwm, 0, 18);
7230 GEN_VXFORM(vsubudm, 0, 19);
7231 GEN_VXFORM(vmaxub, 1, 0);
7232 GEN_VXFORM(vmaxuh, 1, 1);
7233 GEN_VXFORM(vmaxuw, 1, 2);
7234 GEN_VXFORM(vmaxud, 1, 3);
7235 GEN_VXFORM(vmaxsb, 1, 4);
7236 GEN_VXFORM(vmaxsh, 1, 5);
7237 GEN_VXFORM(vmaxsw, 1, 6);
7238 GEN_VXFORM(vmaxsd, 1, 7);
7239 GEN_VXFORM(vminub, 1, 8);
7240 GEN_VXFORM(vminuh, 1, 9);
7241 GEN_VXFORM(vminuw, 1, 10);
7242 GEN_VXFORM(vminud, 1, 11);
7243 GEN_VXFORM(vminsb, 1, 12);
7244 GEN_VXFORM(vminsh, 1, 13);
7245 GEN_VXFORM(vminsw, 1, 14);
7246 GEN_VXFORM(vminsd, 1, 15);
7247 GEN_VXFORM(vavgub, 1, 16);
7248 GEN_VXFORM(vavguh, 1, 17);
7249 GEN_VXFORM(vavguw, 1, 18);
7250 GEN_VXFORM(vavgsb, 1, 20);
7251 GEN_VXFORM(vavgsh, 1, 21);
7252 GEN_VXFORM(vavgsw, 1, 22);
7253 GEN_VXFORM(vmrghb, 6, 0);
7254 GEN_VXFORM(vmrghh, 6, 1);
7255 GEN_VXFORM(vmrghw, 6, 2);
7256 GEN_VXFORM(vmrglb, 6, 4);
7257 GEN_VXFORM(vmrglh, 6, 5);
7258 GEN_VXFORM(vmrglw, 6, 6);
7259
7260 static void gen_vmrgew(DisasContext *ctx)
7261 {
7262     TCGv_i64 tmp;
7263     int VT, VA, VB;
7264     if (unlikely(!ctx->altivec_enabled)) {
7265         gen_exception(ctx, POWERPC_EXCP_VPU);
7266         return;
7267     }
7268     VT = rD(ctx->opcode);
7269     VA = rA(ctx->opcode);
7270     VB = rB(ctx->opcode);
7271     tmp = tcg_temp_new_i64();
7272     tcg_gen_shri_i64(tmp, cpu_avrh[VB], 32);
7273     tcg_gen_deposit_i64(cpu_avrh[VT], cpu_avrh[VA], tmp, 0, 32);
7274     tcg_gen_shri_i64(tmp, cpu_avrl[VB], 32);
7275     tcg_gen_deposit_i64(cpu_avrl[VT], cpu_avrl[VA], tmp, 0, 32);
7276     tcg_temp_free_i64(tmp);
7277 }
7278
7279 static void gen_vmrgow(DisasContext *ctx)
7280 {
7281     int VT, VA, VB;
7282     if (unlikely(!ctx->altivec_enabled)) {
7283         gen_exception(ctx, POWERPC_EXCP_VPU);
7284         return;
7285     }
7286     VT = rD(ctx->opcode);
7287     VA = rA(ctx->opcode);
7288     VB = rB(ctx->opcode);
7289
7290     tcg_gen_deposit_i64(cpu_avrh[VT], cpu_avrh[VB], cpu_avrh[VA], 32, 32);
7291     tcg_gen_deposit_i64(cpu_avrl[VT], cpu_avrl[VB], cpu_avrl[VA], 32, 32);
7292 }
7293
7294 GEN_VXFORM(vmuloub, 4, 0);
7295 GEN_VXFORM(vmulouh, 4, 1);
7296 GEN_VXFORM(vmulouw, 4, 2);
7297 GEN_VXFORM(vmuluwm, 4, 2);
7298 GEN_VXFORM_DUAL(vmulouw, PPC_ALTIVEC, PPC_NONE,
7299                 vmuluwm, PPC_NONE, PPC2_ALTIVEC_207)
7300 GEN_VXFORM(vmulosb, 4, 4);
7301 GEN_VXFORM(vmulosh, 4, 5);
7302 GEN_VXFORM(vmulosw, 4, 6);
7303 GEN_VXFORM(vmuleub, 4, 8);
7304 GEN_VXFORM(vmuleuh, 4, 9);
7305 GEN_VXFORM(vmuleuw, 4, 10);
7306 GEN_VXFORM(vmulesb, 4, 12);
7307 GEN_VXFORM(vmulesh, 4, 13);
7308 GEN_VXFORM(vmulesw, 4, 14);
7309 GEN_VXFORM(vslb, 2, 4);
7310 GEN_VXFORM(vslh, 2, 5);
7311 GEN_VXFORM(vslw, 2, 6);
7312 GEN_VXFORM(vsld, 2, 23);
7313 GEN_VXFORM(vsrb, 2, 8);
7314 GEN_VXFORM(vsrh, 2, 9);
7315 GEN_VXFORM(vsrw, 2, 10);
7316 GEN_VXFORM(vsrd, 2, 27);
7317 GEN_VXFORM(vsrab, 2, 12);
7318 GEN_VXFORM(vsrah, 2, 13);
7319 GEN_VXFORM(vsraw, 2, 14);
7320 GEN_VXFORM(vsrad, 2, 15);
7321 GEN_VXFORM(vslo, 6, 16);
7322 GEN_VXFORM(vsro, 6, 17);
7323 GEN_VXFORM(vaddcuw, 0, 6);
7324 GEN_VXFORM(vsubcuw, 0, 22);
7325 GEN_VXFORM_ENV(vaddubs, 0, 8);
7326 GEN_VXFORM_ENV(vadduhs, 0, 9);
7327 GEN_VXFORM_ENV(vadduws, 0, 10);
7328 GEN_VXFORM_ENV(vaddsbs, 0, 12);
7329 GEN_VXFORM_ENV(vaddshs, 0, 13);
7330 GEN_VXFORM_ENV(vaddsws, 0, 14);
7331 GEN_VXFORM_ENV(vsububs, 0, 24);
7332 GEN_VXFORM_ENV(vsubuhs, 0, 25);
7333 GEN_VXFORM_ENV(vsubuws, 0, 26);
7334 GEN_VXFORM_ENV(vsubsbs, 0, 28);
7335 GEN_VXFORM_ENV(vsubshs, 0, 29);
7336 GEN_VXFORM_ENV(vsubsws, 0, 30);
7337 GEN_VXFORM(vadduqm, 0, 4);
7338 GEN_VXFORM(vaddcuq, 0, 5);
7339 GEN_VXFORM3(vaddeuqm, 30, 0);
7340 GEN_VXFORM3(vaddecuq, 30, 0);
7341 GEN_VXFORM_DUAL(vaddeuqm, PPC_NONE, PPC2_ALTIVEC_207, \
7342             vaddecuq, PPC_NONE, PPC2_ALTIVEC_207)
7343 GEN_VXFORM(vsubuqm, 0, 20);
7344 GEN_VXFORM(vsubcuq, 0, 21);
7345 GEN_VXFORM3(vsubeuqm, 31, 0);
7346 GEN_VXFORM3(vsubecuq, 31, 0);
7347 GEN_VXFORM_DUAL(vsubeuqm, PPC_NONE, PPC2_ALTIVEC_207, \
7348             vsubecuq, PPC_NONE, PPC2_ALTIVEC_207)
7349 GEN_VXFORM(vrlb, 2, 0);
7350 GEN_VXFORM(vrlh, 2, 1);
7351 GEN_VXFORM(vrlw, 2, 2);
7352 GEN_VXFORM(vrld, 2, 3);
7353 GEN_VXFORM(vsl, 2, 7);
7354 GEN_VXFORM(vsr, 2, 11);
7355 GEN_VXFORM_ENV(vpkuhum, 7, 0);
7356 GEN_VXFORM_ENV(vpkuwum, 7, 1);
7357 GEN_VXFORM_ENV(vpkudum, 7, 17);
7358 GEN_VXFORM_ENV(vpkuhus, 7, 2);
7359 GEN_VXFORM_ENV(vpkuwus, 7, 3);
7360 GEN_VXFORM_ENV(vpkudus, 7, 19);
7361 GEN_VXFORM_ENV(vpkshus, 7, 4);
7362 GEN_VXFORM_ENV(vpkswus, 7, 5);
7363 GEN_VXFORM_ENV(vpksdus, 7, 21);
7364 GEN_VXFORM_ENV(vpkshss, 7, 6);
7365 GEN_VXFORM_ENV(vpkswss, 7, 7);
7366 GEN_VXFORM_ENV(vpksdss, 7, 23);
7367 GEN_VXFORM(vpkpx, 7, 12);
7368 GEN_VXFORM_ENV(vsum4ubs, 4, 24);
7369 GEN_VXFORM_ENV(vsum4sbs, 4, 28);
7370 GEN_VXFORM_ENV(vsum4shs, 4, 25);
7371 GEN_VXFORM_ENV(vsum2sws, 4, 26);
7372 GEN_VXFORM_ENV(vsumsws, 4, 30);
7373 GEN_VXFORM_ENV(vaddfp, 5, 0);
7374 GEN_VXFORM_ENV(vsubfp, 5, 1);
7375 GEN_VXFORM_ENV(vmaxfp, 5, 16);
7376 GEN_VXFORM_ENV(vminfp, 5, 17);
7377
7378 #define GEN_VXRFORM1(opname, name, str, opc2, opc3)                     \
7379 static void glue(gen_, name)(DisasContext *ctx)                         \
7380     {                                                                   \
7381         TCGv_ptr ra, rb, rd;                                            \
7382         if (unlikely(!ctx->altivec_enabled)) {                          \
7383             gen_exception(ctx, POWERPC_EXCP_VPU);                       \
7384             return;                                                     \
7385         }                                                               \
7386         ra = gen_avr_ptr(rA(ctx->opcode));                              \
7387         rb = gen_avr_ptr(rB(ctx->opcode));                              \
7388         rd = gen_avr_ptr(rD(ctx->opcode));                              \
7389         gen_helper_##opname(cpu_env, rd, ra, rb);                       \
7390         tcg_temp_free_ptr(ra);                                          \
7391         tcg_temp_free_ptr(rb);                                          \
7392         tcg_temp_free_ptr(rd);                                          \
7393     }
7394
7395 #define GEN_VXRFORM(name, opc2, opc3)                                \
7396     GEN_VXRFORM1(name, name, #name, opc2, opc3)                      \
7397     GEN_VXRFORM1(name##_dot, name##_, #name ".", opc2, (opc3 | (0x1 << 4)))
7398
7399 /*
7400  * Support for Altivec instructions that use bit 31 (Rc) as an opcode
7401  * bit but also use bit 21 as an actual Rc bit.  In general, thse pairs
7402  * come from different versions of the ISA, so we must also support a
7403  * pair of flags for each instruction.
7404  */
7405 #define GEN_VXRFORM_DUAL(name0, flg0, flg2_0, name1, flg1, flg2_1)     \
7406 static void glue(gen_, name0##_##name1)(DisasContext *ctx)             \
7407 {                                                                      \
7408     if ((Rc(ctx->opcode) == 0) &&                                      \
7409         ((ctx->insns_flags & flg0) || (ctx->insns_flags2 & flg2_0))) { \
7410         if (Rc21(ctx->opcode) == 0) {                                  \
7411             gen_##name0(ctx);                                          \
7412         } else {                                                       \
7413             gen_##name0##_(ctx);                                       \
7414         }                                                              \
7415     } else if ((Rc(ctx->opcode) == 1) &&                               \
7416         ((ctx->insns_flags & flg1) || (ctx->insns_flags2 & flg2_1))) { \
7417         if (Rc21(ctx->opcode) == 0) {                                  \
7418             gen_##name1(ctx);                                          \
7419         } else {                                                       \
7420             gen_##name1##_(ctx);                                       \
7421         }                                                              \
7422     } else {                                                           \
7423         gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL);            \
7424     }                                                                  \
7425 }
7426
7427 GEN_VXRFORM(vcmpequb, 3, 0)
7428 GEN_VXRFORM(vcmpequh, 3, 1)
7429 GEN_VXRFORM(vcmpequw, 3, 2)
7430 GEN_VXRFORM(vcmpequd, 3, 3)
7431 GEN_VXRFORM(vcmpgtsb, 3, 12)
7432 GEN_VXRFORM(vcmpgtsh, 3, 13)
7433 GEN_VXRFORM(vcmpgtsw, 3, 14)
7434 GEN_VXRFORM(vcmpgtsd, 3, 15)
7435 GEN_VXRFORM(vcmpgtub, 3, 8)
7436 GEN_VXRFORM(vcmpgtuh, 3, 9)
7437 GEN_VXRFORM(vcmpgtuw, 3, 10)
7438 GEN_VXRFORM(vcmpgtud, 3, 11)
7439 GEN_VXRFORM(vcmpeqfp, 3, 3)
7440 GEN_VXRFORM(vcmpgefp, 3, 7)
7441 GEN_VXRFORM(vcmpgtfp, 3, 11)
7442 GEN_VXRFORM(vcmpbfp, 3, 15)
7443
7444 GEN_VXRFORM_DUAL(vcmpeqfp, PPC_ALTIVEC, PPC_NONE, \
7445                  vcmpequd, PPC_NONE, PPC2_ALTIVEC_207)
7446 GEN_VXRFORM_DUAL(vcmpbfp, PPC_ALTIVEC, PPC_NONE, \
7447                  vcmpgtsd, PPC_NONE, PPC2_ALTIVEC_207)
7448 GEN_VXRFORM_DUAL(vcmpgtfp, PPC_ALTIVEC, PPC_NONE, \
7449                  vcmpgtud, PPC_NONE, PPC2_ALTIVEC_207)
7450
7451 #define GEN_VXFORM_SIMM(name, opc2, opc3)                               \
7452 static void glue(gen_, name)(DisasContext *ctx)                         \
7453     {                                                                   \
7454         TCGv_ptr rd;                                                    \
7455         TCGv_i32 simm;                                                  \
7456         if (unlikely(!ctx->altivec_enabled)) {                          \
7457             gen_exception(ctx, POWERPC_EXCP_VPU);                       \
7458             return;                                                     \
7459         }                                                               \
7460         simm = tcg_const_i32(SIMM5(ctx->opcode));                       \
7461         rd = gen_avr_ptr(rD(ctx->opcode));                              \
7462         gen_helper_##name (rd, simm);                                   \
7463         tcg_temp_free_i32(simm);                                        \
7464         tcg_temp_free_ptr(rd);                                          \
7465     }
7466
7467 GEN_VXFORM_SIMM(vspltisb, 6, 12);
7468 GEN_VXFORM_SIMM(vspltish, 6, 13);
7469 GEN_VXFORM_SIMM(vspltisw, 6, 14);
7470
7471 #define GEN_VXFORM_NOA(name, opc2, opc3)                                \
7472 static void glue(gen_, name)(DisasContext *ctx)                                 \
7473     {                                                                   \
7474         TCGv_ptr rb, rd;                                                \
7475         if (unlikely(!ctx->altivec_enabled)) {                          \
7476             gen_exception(ctx, POWERPC_EXCP_VPU);                       \
7477             return;                                                     \
7478         }                                                               \
7479         rb = gen_avr_ptr(rB(ctx->opcode));                              \
7480         rd = gen_avr_ptr(rD(ctx->opcode));                              \
7481         gen_helper_##name (rd, rb);                                     \
7482         tcg_temp_free_ptr(rb);                                          \
7483         tcg_temp_free_ptr(rd);                                         \
7484     }
7485
7486 #define GEN_VXFORM_NOA_ENV(name, opc2, opc3)                            \
7487 static void glue(gen_, name)(DisasContext *ctx)                         \
7488     {                                                                   \
7489         TCGv_ptr rb, rd;                                                \
7490                                                                         \
7491         if (unlikely(!ctx->altivec_enabled)) {                          \
7492             gen_exception(ctx, POWERPC_EXCP_VPU);                       \
7493             return;                                                     \
7494         }                                                               \
7495         rb = gen_avr_ptr(rB(ctx->opcode));                              \
7496         rd = gen_avr_ptr(rD(ctx->opcode));                              \
7497         gen_helper_##name(cpu_env, rd, rb);                             \
7498         tcg_temp_free_ptr(rb);                                          \
7499         tcg_temp_free_ptr(rd);                                          \
7500     }
7501
7502 GEN_VXFORM_NOA(vupkhsb, 7, 8);
7503 GEN_VXFORM_NOA(vupkhsh, 7, 9);
7504 GEN_VXFORM_NOA(vupkhsw, 7, 25);
7505 GEN_VXFORM_NOA(vupklsb, 7, 10);
7506 GEN_VXFORM_NOA(vupklsh, 7, 11);
7507 GEN_VXFORM_NOA(vupklsw, 7, 27);
7508 GEN_VXFORM_NOA(vupkhpx, 7, 13);
7509 GEN_VXFORM_NOA(vupklpx, 7, 15);
7510 GEN_VXFORM_NOA_ENV(vrefp, 5, 4);
7511 GEN_VXFORM_NOA_ENV(vrsqrtefp, 5, 5);
7512 GEN_VXFORM_NOA_ENV(vexptefp, 5, 6);
7513 GEN_VXFORM_NOA_ENV(vlogefp, 5, 7);
7514 GEN_VXFORM_NOA_ENV(vrfim, 5, 11);
7515 GEN_VXFORM_NOA_ENV(vrfin, 5, 8);
7516 GEN_VXFORM_NOA_ENV(vrfip, 5, 10);
7517 GEN_VXFORM_NOA_ENV(vrfiz, 5, 9);
7518
7519 #define GEN_VXFORM_SIMM(name, opc2, opc3)                               \
7520 static void glue(gen_, name)(DisasContext *ctx)                                 \
7521     {                                                                   \
7522         TCGv_ptr rd;                                                    \
7523         TCGv_i32 simm;                                                  \
7524         if (unlikely(!ctx->altivec_enabled)) {                          \
7525             gen_exception(ctx, POWERPC_EXCP_VPU);                       \
7526             return;                                                     \
7527         }                                                               \
7528         simm = tcg_const_i32(SIMM5(ctx->opcode));                       \
7529         rd = gen_avr_ptr(rD(ctx->opcode));                              \
7530         gen_helper_##name (rd, simm);                                   \
7531         tcg_temp_free_i32(simm);                                        \
7532         tcg_temp_free_ptr(rd);                                          \
7533     }
7534
7535 #define GEN_VXFORM_UIMM(name, opc2, opc3)                               \
7536 static void glue(gen_, name)(DisasContext *ctx)                                 \
7537     {                                                                   \
7538         TCGv_ptr rb, rd;                                                \
7539         TCGv_i32 uimm;                                                  \
7540         if (unlikely(!ctx->altivec_enabled)) {                          \
7541             gen_exception(ctx, POWERPC_EXCP_VPU);                       \
7542             return;                                                     \
7543         }                                                               \
7544         uimm = tcg_const_i32(UIMM5(ctx->opcode));                       \
7545         rb = gen_avr_ptr(rB(ctx->opcode));                              \
7546         rd = gen_avr_ptr(rD(ctx->opcode));                              \
7547         gen_helper_##name (rd, rb, uimm);                               \
7548         tcg_temp_free_i32(uimm);                                        \
7549         tcg_temp_free_ptr(rb);                                          \
7550         tcg_temp_free_ptr(rd);                                          \
7551     }
7552
7553 #define GEN_VXFORM_UIMM_ENV(name, opc2, opc3)                           \
7554 static void glue(gen_, name)(DisasContext *ctx)                         \
7555     {                                                                   \
7556         TCGv_ptr rb, rd;                                                \
7557         TCGv_i32 uimm;                                                  \
7558                                                                         \
7559         if (unlikely(!ctx->altivec_enabled)) {                          \
7560             gen_exception(ctx, POWERPC_EXCP_VPU);                       \
7561             return;                                                     \
7562         }                                                               \
7563         uimm = tcg_const_i32(UIMM5(ctx->opcode));                       \
7564         rb = gen_avr_ptr(rB(ctx->opcode));                              \
7565         rd = gen_avr_ptr(rD(ctx->opcode));                              \
7566         gen_helper_##name(cpu_env, rd, rb, uimm);                       \
7567         tcg_temp_free_i32(uimm);                                        \
7568         tcg_temp_free_ptr(rb);                                          \
7569         tcg_temp_free_ptr(rd);                                          \
7570     }
7571
7572 GEN_VXFORM_UIMM(vspltb, 6, 8);
7573 GEN_VXFORM_UIMM(vsplth, 6, 9);
7574 GEN_VXFORM_UIMM(vspltw, 6, 10);
7575 GEN_VXFORM_UIMM_ENV(vcfux, 5, 12);
7576 GEN_VXFORM_UIMM_ENV(vcfsx, 5, 13);
7577 GEN_VXFORM_UIMM_ENV(vctuxs, 5, 14);
7578 GEN_VXFORM_UIMM_ENV(vctsxs, 5, 15);
7579
7580 static void gen_vsldoi(DisasContext *ctx)
7581 {
7582     TCGv_ptr ra, rb, rd;
7583     TCGv_i32 sh;
7584     if (unlikely(!ctx->altivec_enabled)) {
7585         gen_exception(ctx, POWERPC_EXCP_VPU);
7586         return;
7587     }
7588     ra = gen_avr_ptr(rA(ctx->opcode));
7589     rb = gen_avr_ptr(rB(ctx->opcode));
7590     rd = gen_avr_ptr(rD(ctx->opcode));
7591     sh = tcg_const_i32(VSH(ctx->opcode));
7592     gen_helper_vsldoi (rd, ra, rb, sh);
7593     tcg_temp_free_ptr(ra);
7594     tcg_temp_free_ptr(rb);
7595     tcg_temp_free_ptr(rd);
7596     tcg_temp_free_i32(sh);
7597 }
7598
7599 #define GEN_VAFORM_PAIRED(name0, name1, opc2)                           \
7600 static void glue(gen_, name0##_##name1)(DisasContext *ctx)              \
7601     {                                                                   \
7602         TCGv_ptr ra, rb, rc, rd;                                        \
7603         if (unlikely(!ctx->altivec_enabled)) {                          \
7604             gen_exception(ctx, POWERPC_EXCP_VPU);                       \
7605             return;                                                     \
7606         }                                                               \
7607         ra = gen_avr_ptr(rA(ctx->opcode));                              \
7608         rb = gen_avr_ptr(rB(ctx->opcode));                              \
7609         rc = gen_avr_ptr(rC(ctx->opcode));                              \
7610         rd = gen_avr_ptr(rD(ctx->opcode));                              \
7611         if (Rc(ctx->opcode)) {                                          \
7612             gen_helper_##name1(cpu_env, rd, ra, rb, rc);                \
7613         } else {                                                        \
7614             gen_helper_##name0(cpu_env, rd, ra, rb, rc);                \
7615         }                                                               \
7616         tcg_temp_free_ptr(ra);                                          \
7617         tcg_temp_free_ptr(rb);                                          \
7618         tcg_temp_free_ptr(rc);                                          \
7619         tcg_temp_free_ptr(rd);                                          \
7620     }
7621
7622 GEN_VAFORM_PAIRED(vmhaddshs, vmhraddshs, 16)
7623
7624 static void gen_vmladduhm(DisasContext *ctx)
7625 {
7626     TCGv_ptr ra, rb, rc, rd;
7627     if (unlikely(!ctx->altivec_enabled)) {
7628         gen_exception(ctx, POWERPC_EXCP_VPU);
7629         return;
7630     }
7631     ra = gen_avr_ptr(rA(ctx->opcode));
7632     rb = gen_avr_ptr(rB(ctx->opcode));
7633     rc = gen_avr_ptr(rC(ctx->opcode));
7634     rd = gen_avr_ptr(rD(ctx->opcode));
7635     gen_helper_vmladduhm(rd, ra, rb, rc);
7636     tcg_temp_free_ptr(ra);
7637     tcg_temp_free_ptr(rb);
7638     tcg_temp_free_ptr(rc);
7639     tcg_temp_free_ptr(rd);
7640 }
7641
7642 GEN_VAFORM_PAIRED(vmsumubm, vmsummbm, 18)
7643 GEN_VAFORM_PAIRED(vmsumuhm, vmsumuhs, 19)
7644 GEN_VAFORM_PAIRED(vmsumshm, vmsumshs, 20)
7645 GEN_VAFORM_PAIRED(vsel, vperm, 21)
7646 GEN_VAFORM_PAIRED(vmaddfp, vnmsubfp, 23)
7647
7648 GEN_VXFORM_NOA(vclzb, 1, 28)
7649 GEN_VXFORM_NOA(vclzh, 1, 29)
7650 GEN_VXFORM_NOA(vclzw, 1, 30)
7651 GEN_VXFORM_NOA(vclzd, 1, 31)
7652 GEN_VXFORM_NOA(vpopcntb, 1, 28)
7653 GEN_VXFORM_NOA(vpopcnth, 1, 29)
7654 GEN_VXFORM_NOA(vpopcntw, 1, 30)
7655 GEN_VXFORM_NOA(vpopcntd, 1, 31)
7656 GEN_VXFORM_DUAL(vclzb, PPC_NONE, PPC2_ALTIVEC_207, \
7657                 vpopcntb, PPC_NONE, PPC2_ALTIVEC_207)
7658 GEN_VXFORM_DUAL(vclzh, PPC_NONE, PPC2_ALTIVEC_207, \
7659                 vpopcnth, PPC_NONE, PPC2_ALTIVEC_207)
7660 GEN_VXFORM_DUAL(vclzw, PPC_NONE, PPC2_ALTIVEC_207, \
7661                 vpopcntw, PPC_NONE, PPC2_ALTIVEC_207)
7662 GEN_VXFORM_DUAL(vclzd, PPC_NONE, PPC2_ALTIVEC_207, \
7663                 vpopcntd, PPC_NONE, PPC2_ALTIVEC_207)
7664 GEN_VXFORM(vbpermq, 6, 21);
7665 GEN_VXFORM_NOA(vgbbd, 6, 20);
7666 GEN_VXFORM(vpmsumb, 4, 16)
7667 GEN_VXFORM(vpmsumh, 4, 17)
7668 GEN_VXFORM(vpmsumw, 4, 18)
7669 GEN_VXFORM(vpmsumd, 4, 19)
7670
7671 #define GEN_BCD(op)                                 \
7672 static void gen_##op(DisasContext *ctx)             \
7673 {                                                   \
7674     TCGv_ptr ra, rb, rd;                            \
7675     TCGv_i32 ps;                                    \
7676                                                     \
7677     if (unlikely(!ctx->altivec_enabled)) {          \
7678         gen_exception(ctx, POWERPC_EXCP_VPU);       \
7679         return;                                     \
7680     }                                               \
7681                                                     \
7682     ra = gen_avr_ptr(rA(ctx->opcode));              \
7683     rb = gen_avr_ptr(rB(ctx->opcode));              \
7684     rd = gen_avr_ptr(rD(ctx->opcode));              \
7685                                                     \
7686     ps = tcg_const_i32((ctx->opcode & 0x200) != 0); \
7687                                                     \
7688     gen_helper_##op(cpu_crf[6], rd, ra, rb, ps);    \
7689                                                     \
7690     tcg_temp_free_ptr(ra);                          \
7691     tcg_temp_free_ptr(rb);                          \
7692     tcg_temp_free_ptr(rd);                          \
7693     tcg_temp_free_i32(ps);                          \
7694 }
7695
7696 GEN_BCD(bcdadd)
7697 GEN_BCD(bcdsub)
7698
7699 GEN_VXFORM_DUAL(vsububm, PPC_ALTIVEC, PPC_NONE, \
7700                 bcdadd, PPC_NONE, PPC2_ALTIVEC_207)
7701 GEN_VXFORM_DUAL(vsububs, PPC_ALTIVEC, PPC_NONE, \
7702                 bcdadd, PPC_NONE, PPC2_ALTIVEC_207)
7703 GEN_VXFORM_DUAL(vsubuhm, PPC_ALTIVEC, PPC_NONE, \
7704                 bcdsub, PPC_NONE, PPC2_ALTIVEC_207)
7705 GEN_VXFORM_DUAL(vsubuhs, PPC_ALTIVEC, PPC_NONE, \
7706                 bcdsub, PPC_NONE, PPC2_ALTIVEC_207)
7707
7708 static void gen_vsbox(DisasContext *ctx)
7709 {
7710     TCGv_ptr ra, rd;
7711     if (unlikely(!ctx->altivec_enabled)) {
7712         gen_exception(ctx, POWERPC_EXCP_VPU);
7713         return;
7714     }
7715     ra = gen_avr_ptr(rA(ctx->opcode));
7716     rd = gen_avr_ptr(rD(ctx->opcode));
7717     gen_helper_vsbox(rd, ra);
7718     tcg_temp_free_ptr(ra);
7719     tcg_temp_free_ptr(rd);
7720 }
7721
7722 GEN_VXFORM(vcipher, 4, 20)
7723 GEN_VXFORM(vcipherlast, 4, 20)
7724 GEN_VXFORM(vncipher, 4, 21)
7725 GEN_VXFORM(vncipherlast, 4, 21)
7726
7727 GEN_VXFORM_DUAL(vcipher, PPC_NONE, PPC2_ALTIVEC_207,
7728                 vcipherlast, PPC_NONE, PPC2_ALTIVEC_207)
7729 GEN_VXFORM_DUAL(vncipher, PPC_NONE, PPC2_ALTIVEC_207,
7730                 vncipherlast, PPC_NONE, PPC2_ALTIVEC_207)
7731
7732 #define VSHASIGMA(op)                         \
7733 static void gen_##op(DisasContext *ctx)       \
7734 {                                             \
7735     TCGv_ptr ra, rd;                          \
7736     TCGv_i32 st_six;                          \
7737     if (unlikely(!ctx->altivec_enabled)) {    \
7738         gen_exception(ctx, POWERPC_EXCP_VPU); \
7739         return;                               \
7740     }                                         \
7741     ra = gen_avr_ptr(rA(ctx->opcode));        \
7742     rd = gen_avr_ptr(rD(ctx->opcode));        \
7743     st_six = tcg_const_i32(rB(ctx->opcode));  \
7744     gen_helper_##op(rd, ra, st_six);          \
7745     tcg_temp_free_ptr(ra);                    \
7746     tcg_temp_free_ptr(rd);                    \
7747     tcg_temp_free_i32(st_six);                \
7748 }
7749
7750 VSHASIGMA(vshasigmaw)
7751 VSHASIGMA(vshasigmad)
7752
7753 GEN_VXFORM3(vpermxor, 22, 0xFF)
7754 GEN_VXFORM_DUAL(vsldoi, PPC_ALTIVEC, PPC_NONE,
7755                 vpermxor, PPC_NONE, PPC2_ALTIVEC_207)
7756
7757 /***                           VSX extension                               ***/
7758
7759 static inline TCGv_i64 cpu_vsrh(int n)
7760 {
7761     if (n < 32) {
7762         return cpu_fpr[n];
7763     } else {
7764         return cpu_avrh[n-32];
7765     }
7766 }
7767
7768 static inline TCGv_i64 cpu_vsrl(int n)
7769 {
7770     if (n < 32) {
7771         return cpu_vsr[n];
7772     } else {
7773         return cpu_avrl[n-32];
7774     }
7775 }
7776
7777 #define VSX_LOAD_SCALAR(name, operation)                      \
7778 static void gen_##name(DisasContext *ctx)                     \
7779 {                                                             \
7780     TCGv EA;                                                  \
7781     if (unlikely(!ctx->vsx_enabled)) {                        \
7782         gen_exception(ctx, POWERPC_EXCP_VSXU);                \
7783         return;                                               \
7784     }                                                         \
7785     gen_set_access_type(ctx, ACCESS_INT);                     \
7786     EA = tcg_temp_new();                                      \
7787     gen_addr_reg_index(ctx, EA);                              \
7788     gen_qemu_##operation(ctx, cpu_vsrh(xT(ctx->opcode)), EA); \
7789     /* NOTE: cpu_vsrl is undefined */                         \
7790     tcg_temp_free(EA);                                        \
7791 }
7792
7793 VSX_LOAD_SCALAR(lxsdx, ld64)
7794 VSX_LOAD_SCALAR(lxsiwax, ld32s_i64)
7795 VSX_LOAD_SCALAR(lxsiwzx, ld32u_i64)
7796 VSX_LOAD_SCALAR(lxsspx, ld32fs)
7797
7798 static void gen_lxvd2x(DisasContext *ctx)
7799 {
7800     TCGv EA;
7801     if (unlikely(!ctx->vsx_enabled)) {
7802         gen_exception(ctx, POWERPC_EXCP_VSXU);
7803         return;
7804     }
7805     gen_set_access_type(ctx, ACCESS_INT);
7806     EA = tcg_temp_new();
7807     gen_addr_reg_index(ctx, EA);
7808     gen_qemu_ld64(ctx, cpu_vsrh(xT(ctx->opcode)), EA);
7809     tcg_gen_addi_tl(EA, EA, 8);
7810     gen_qemu_ld64(ctx, cpu_vsrl(xT(ctx->opcode)), EA);
7811     tcg_temp_free(EA);
7812 }
7813
7814 static void gen_lxvdsx(DisasContext *ctx)
7815 {
7816     TCGv EA;
7817     if (unlikely(!ctx->vsx_enabled)) {
7818         gen_exception(ctx, POWERPC_EXCP_VSXU);
7819         return;
7820     }
7821     gen_set_access_type(ctx, ACCESS_INT);
7822     EA = tcg_temp_new();
7823     gen_addr_reg_index(ctx, EA);
7824     gen_qemu_ld64(ctx, cpu_vsrh(xT(ctx->opcode)), EA);
7825     tcg_gen_mov_i64(cpu_vsrl(xT(ctx->opcode)), cpu_vsrh(xT(ctx->opcode)));
7826     tcg_temp_free(EA);
7827 }
7828
7829 static void gen_lxvw4x(DisasContext *ctx)
7830 {
7831     TCGv EA;
7832     TCGv_i64 tmp;
7833     TCGv_i64 xth = cpu_vsrh(xT(ctx->opcode));
7834     TCGv_i64 xtl = cpu_vsrl(xT(ctx->opcode));
7835     if (unlikely(!ctx->vsx_enabled)) {
7836         gen_exception(ctx, POWERPC_EXCP_VSXU);
7837         return;
7838     }
7839     gen_set_access_type(ctx, ACCESS_INT);
7840     EA = tcg_temp_new();
7841     tmp = tcg_temp_new_i64();
7842
7843     gen_addr_reg_index(ctx, EA);
7844     gen_qemu_ld32u_i64(ctx, tmp, EA);
7845     tcg_gen_addi_tl(EA, EA, 4);
7846     gen_qemu_ld32u_i64(ctx, xth, EA);
7847     tcg_gen_deposit_i64(xth, xth, tmp, 32, 32);
7848
7849     tcg_gen_addi_tl(EA, EA, 4);
7850     gen_qemu_ld32u_i64(ctx, tmp, EA);
7851     tcg_gen_addi_tl(EA, EA, 4);
7852     gen_qemu_ld32u_i64(ctx, xtl, EA);
7853     tcg_gen_deposit_i64(xtl, xtl, tmp, 32, 32);
7854
7855     tcg_temp_free(EA);
7856     tcg_temp_free_i64(tmp);
7857 }
7858
7859 #define VSX_STORE_SCALAR(name, operation)                     \
7860 static void gen_##name(DisasContext *ctx)                     \
7861 {                                                             \
7862     TCGv EA;                                                  \
7863     if (unlikely(!ctx->vsx_enabled)) {                        \
7864         gen_exception(ctx, POWERPC_EXCP_VSXU);                \
7865         return;                                               \
7866     }                                                         \
7867     gen_set_access_type(ctx, ACCESS_INT);                     \
7868     EA = tcg_temp_new();                                      \
7869     gen_addr_reg_index(ctx, EA);                              \
7870     gen_qemu_##operation(ctx, cpu_vsrh(xS(ctx->opcode)), EA); \
7871     tcg_temp_free(EA);                                        \
7872 }
7873
7874 VSX_STORE_SCALAR(stxsdx, st64)
7875 VSX_STORE_SCALAR(stxsiwx, st32_i64)
7876 VSX_STORE_SCALAR(stxsspx, st32fs)
7877
7878 static void gen_stxvd2x(DisasContext *ctx)
7879 {
7880     TCGv EA;
7881     if (unlikely(!ctx->vsx_enabled)) {
7882         gen_exception(ctx, POWERPC_EXCP_VSXU);
7883         return;
7884     }
7885     gen_set_access_type(ctx, ACCESS_INT);
7886     EA = tcg_temp_new();
7887     gen_addr_reg_index(ctx, EA);
7888     gen_qemu_st64(ctx, cpu_vsrh(xS(ctx->opcode)), EA);
7889     tcg_gen_addi_tl(EA, EA, 8);
7890     gen_qemu_st64(ctx, cpu_vsrl(xS(ctx->opcode)), EA);
7891     tcg_temp_free(EA);
7892 }
7893
7894 static void gen_stxvw4x(DisasContext *ctx)
7895 {
7896     TCGv_i64 tmp;
7897     TCGv EA;
7898     if (unlikely(!ctx->vsx_enabled)) {
7899         gen_exception(ctx, POWERPC_EXCP_VSXU);
7900         return;
7901     }
7902     gen_set_access_type(ctx, ACCESS_INT);
7903     EA = tcg_temp_new();
7904     gen_addr_reg_index(ctx, EA);
7905     tmp = tcg_temp_new_i64();
7906
7907     tcg_gen_shri_i64(tmp, cpu_vsrh(xS(ctx->opcode)), 32);
7908     gen_qemu_st32_i64(ctx, tmp, EA);
7909     tcg_gen_addi_tl(EA, EA, 4);
7910     gen_qemu_st32_i64(ctx, cpu_vsrh(xS(ctx->opcode)), EA);
7911
7912     tcg_gen_shri_i64(tmp, cpu_vsrl(xS(ctx->opcode)), 32);
7913     tcg_gen_addi_tl(EA, EA, 4);
7914     gen_qemu_st32_i64(ctx, tmp, EA);
7915     tcg_gen_addi_tl(EA, EA, 4);
7916     gen_qemu_st32_i64(ctx, cpu_vsrl(xS(ctx->opcode)), EA);
7917
7918     tcg_temp_free(EA);
7919     tcg_temp_free_i64(tmp);
7920 }
7921
7922 #define MV_VSRW(name, tcgop1, tcgop2, target, source)           \
7923 static void gen_##name(DisasContext *ctx)                       \
7924 {                                                               \
7925     if (xS(ctx->opcode) < 32) {                                 \
7926         if (unlikely(!ctx->fpu_enabled)) {                      \
7927             gen_exception(ctx, POWERPC_EXCP_FPU);               \
7928             return;                                             \
7929         }                                                       \
7930     } else {                                                    \
7931         if (unlikely(!ctx->altivec_enabled)) {                  \
7932             gen_exception(ctx, POWERPC_EXCP_VPU);               \
7933             return;                                             \
7934         }                                                       \
7935     }                                                           \
7936     TCGv_i64 tmp = tcg_temp_new_i64();                          \
7937     tcg_gen_##tcgop1(tmp, source);                              \
7938     tcg_gen_##tcgop2(target, tmp);                              \
7939     tcg_temp_free_i64(tmp);                                     \
7940 }
7941
7942
7943 MV_VSRW(mfvsrwz, ext32u_i64, trunc_i64_tl, cpu_gpr[rA(ctx->opcode)], \
7944         cpu_vsrh(xS(ctx->opcode)))
7945 MV_VSRW(mtvsrwa, extu_tl_i64, ext32s_i64, cpu_vsrh(xT(ctx->opcode)), \
7946         cpu_gpr[rA(ctx->opcode)])
7947 MV_VSRW(mtvsrwz, extu_tl_i64, ext32u_i64, cpu_vsrh(xT(ctx->opcode)), \
7948         cpu_gpr[rA(ctx->opcode)])
7949
7950 #if defined(TARGET_PPC64)
7951 #define MV_VSRD(name, target, source)                           \
7952 static void gen_##name(DisasContext *ctx)                       \
7953 {                                                               \
7954     if (xS(ctx->opcode) < 32) {                                 \
7955         if (unlikely(!ctx->fpu_enabled)) {                      \
7956             gen_exception(ctx, POWERPC_EXCP_FPU);               \
7957             return;                                             \
7958         }                                                       \
7959     } else {                                                    \
7960         if (unlikely(!ctx->altivec_enabled)) {                  \
7961             gen_exception(ctx, POWERPC_EXCP_VPU);               \
7962             return;                                             \
7963         }                                                       \
7964     }                                                           \
7965     tcg_gen_mov_i64(target, source);                            \
7966 }
7967
7968 MV_VSRD(mfvsrd, cpu_gpr[rA(ctx->opcode)], cpu_vsrh(xS(ctx->opcode)))
7969 MV_VSRD(mtvsrd, cpu_vsrh(xT(ctx->opcode)), cpu_gpr[rA(ctx->opcode)])
7970
7971 #endif
7972
7973 static void gen_xxpermdi(DisasContext *ctx)
7974 {
7975     if (unlikely(!ctx->vsx_enabled)) {
7976         gen_exception(ctx, POWERPC_EXCP_VSXU);
7977         return;
7978     }
7979
7980     if (unlikely((xT(ctx->opcode) == xA(ctx->opcode)) ||
7981                  (xT(ctx->opcode) == xB(ctx->opcode)))) {
7982         TCGv_i64 xh, xl;
7983
7984         xh = tcg_temp_new_i64();
7985         xl = tcg_temp_new_i64();
7986
7987         if ((DM(ctx->opcode) & 2) == 0) {
7988             tcg_gen_mov_i64(xh, cpu_vsrh(xA(ctx->opcode)));
7989         } else {
7990             tcg_gen_mov_i64(xh, cpu_vsrl(xA(ctx->opcode)));
7991         }
7992         if ((DM(ctx->opcode) & 1) == 0) {
7993             tcg_gen_mov_i64(xl, cpu_vsrh(xB(ctx->opcode)));
7994         } else {
7995             tcg_gen_mov_i64(xl, cpu_vsrl(xB(ctx->opcode)));
7996         }
7997
7998         tcg_gen_mov_i64(cpu_vsrh(xT(ctx->opcode)), xh);
7999         tcg_gen_mov_i64(cpu_vsrl(xT(ctx->opcode)), xl);
8000
8001         tcg_temp_free_i64(xh);
8002         tcg_temp_free_i64(xl);
8003     } else {
8004         if ((DM(ctx->opcode) & 2) == 0) {
8005             tcg_gen_mov_i64(cpu_vsrh(xT(ctx->opcode)), cpu_vsrh(xA(ctx->opcode)));
8006         } else {
8007             tcg_gen_mov_i64(cpu_vsrh(xT(ctx->opcode)), cpu_vsrl(xA(ctx->opcode)));
8008         }
8009         if ((DM(ctx->opcode) & 1) == 0) {
8010             tcg_gen_mov_i64(cpu_vsrl(xT(ctx->opcode)), cpu_vsrh(xB(ctx->opcode)));
8011         } else {
8012             tcg_gen_mov_i64(cpu_vsrl(xT(ctx->opcode)), cpu_vsrl(xB(ctx->opcode)));
8013         }
8014     }
8015 }
8016
8017 #define OP_ABS 1
8018 #define OP_NABS 2
8019 #define OP_NEG 3
8020 #define OP_CPSGN 4
8021 #define SGN_MASK_DP  0x8000000000000000ull
8022 #define SGN_MASK_SP 0x8000000080000000ull
8023
8024 #define VSX_SCALAR_MOVE(name, op, sgn_mask)                       \
8025 static void glue(gen_, name)(DisasContext * ctx)                  \
8026     {                                                             \
8027         TCGv_i64 xb, sgm;                                         \
8028         if (unlikely(!ctx->vsx_enabled)) {                        \
8029             gen_exception(ctx, POWERPC_EXCP_VSXU);                \
8030             return;                                               \
8031         }                                                         \
8032         xb = tcg_temp_new_i64();                                  \
8033         sgm = tcg_temp_new_i64();                                 \
8034         tcg_gen_mov_i64(xb, cpu_vsrh(xB(ctx->opcode)));           \
8035         tcg_gen_movi_i64(sgm, sgn_mask);                          \
8036         switch (op) {                                             \
8037             case OP_ABS: {                                        \
8038                 tcg_gen_andc_i64(xb, xb, sgm);                    \
8039                 break;                                            \
8040             }                                                     \
8041             case OP_NABS: {                                       \
8042                 tcg_gen_or_i64(xb, xb, sgm);                      \
8043                 break;                                            \
8044             }                                                     \
8045             case OP_NEG: {                                        \
8046                 tcg_gen_xor_i64(xb, xb, sgm);                     \
8047                 break;                                            \
8048             }                                                     \
8049             case OP_CPSGN: {                                      \
8050                 TCGv_i64 xa = tcg_temp_new_i64();                 \
8051                 tcg_gen_mov_i64(xa, cpu_vsrh(xA(ctx->opcode)));   \
8052                 tcg_gen_and_i64(xa, xa, sgm);                     \
8053                 tcg_gen_andc_i64(xb, xb, sgm);                    \
8054                 tcg_gen_or_i64(xb, xb, xa);                       \
8055                 tcg_temp_free_i64(xa);                            \
8056                 break;                                            \
8057             }                                                     \
8058         }                                                         \
8059         tcg_gen_mov_i64(cpu_vsrh(xT(ctx->opcode)), xb);           \
8060         tcg_temp_free_i64(xb);                                    \
8061         tcg_temp_free_i64(sgm);                                   \
8062     }
8063
8064 VSX_SCALAR_MOVE(xsabsdp, OP_ABS, SGN_MASK_DP)
8065 VSX_SCALAR_MOVE(xsnabsdp, OP_NABS, SGN_MASK_DP)
8066 VSX_SCALAR_MOVE(xsnegdp, OP_NEG, SGN_MASK_DP)
8067 VSX_SCALAR_MOVE(xscpsgndp, OP_CPSGN, SGN_MASK_DP)
8068
8069 #define VSX_VECTOR_MOVE(name, op, sgn_mask)                      \
8070 static void glue(gen_, name)(DisasContext * ctx)                 \
8071     {                                                            \
8072         TCGv_i64 xbh, xbl, sgm;                                  \
8073         if (unlikely(!ctx->vsx_enabled)) {                       \
8074             gen_exception(ctx, POWERPC_EXCP_VSXU);               \
8075             return;                                              \
8076         }                                                        \
8077         xbh = tcg_temp_new_i64();                                \
8078         xbl = tcg_temp_new_i64();                                \
8079         sgm = tcg_temp_new_i64();                                \
8080         tcg_gen_mov_i64(xbh, cpu_vsrh(xB(ctx->opcode)));         \
8081         tcg_gen_mov_i64(xbl, cpu_vsrl(xB(ctx->opcode)));         \
8082         tcg_gen_movi_i64(sgm, sgn_mask);                         \
8083         switch (op) {                                            \
8084             case OP_ABS: {                                       \
8085                 tcg_gen_andc_i64(xbh, xbh, sgm);                 \
8086                 tcg_gen_andc_i64(xbl, xbl, sgm);                 \
8087                 break;                                           \
8088             }                                                    \
8089             case OP_NABS: {                                      \
8090                 tcg_gen_or_i64(xbh, xbh, sgm);                   \
8091                 tcg_gen_or_i64(xbl, xbl, sgm);                   \
8092                 break;                                           \
8093             }                                                    \
8094             case OP_NEG: {                                       \
8095                 tcg_gen_xor_i64(xbh, xbh, sgm);                  \
8096                 tcg_gen_xor_i64(xbl, xbl, sgm);                  \
8097                 break;                                           \
8098             }                                                    \
8099             case OP_CPSGN: {                                     \
8100                 TCGv_i64 xah = tcg_temp_new_i64();               \
8101                 TCGv_i64 xal = tcg_temp_new_i64();               \
8102                 tcg_gen_mov_i64(xah, cpu_vsrh(xA(ctx->opcode))); \
8103                 tcg_gen_mov_i64(xal, cpu_vsrl(xA(ctx->opcode))); \
8104                 tcg_gen_and_i64(xah, xah, sgm);                  \
8105                 tcg_gen_and_i64(xal, xal, sgm);                  \
8106                 tcg_gen_andc_i64(xbh, xbh, sgm);                 \
8107                 tcg_gen_andc_i64(xbl, xbl, sgm);                 \
8108                 tcg_gen_or_i64(xbh, xbh, xah);                   \
8109                 tcg_gen_or_i64(xbl, xbl, xal);                   \
8110                 tcg_temp_free_i64(xah);                          \
8111                 tcg_temp_free_i64(xal);                          \
8112                 break;                                           \
8113             }                                                    \
8114         }                                                        \
8115         tcg_gen_mov_i64(cpu_vsrh(xT(ctx->opcode)), xbh);         \
8116         tcg_gen_mov_i64(cpu_vsrl(xT(ctx->opcode)), xbl);         \
8117         tcg_temp_free_i64(xbh);                                  \
8118         tcg_temp_free_i64(xbl);                                  \
8119         tcg_temp_free_i64(sgm);                                  \
8120     }
8121
8122 VSX_VECTOR_MOVE(xvabsdp, OP_ABS, SGN_MASK_DP)
8123 VSX_VECTOR_MOVE(xvnabsdp, OP_NABS, SGN_MASK_DP)
8124 VSX_VECTOR_MOVE(xvnegdp, OP_NEG, SGN_MASK_DP)
8125 VSX_VECTOR_MOVE(xvcpsgndp, OP_CPSGN, SGN_MASK_DP)
8126 VSX_VECTOR_MOVE(xvabssp, OP_ABS, SGN_MASK_SP)
8127 VSX_VECTOR_MOVE(xvnabssp, OP_NABS, SGN_MASK_SP)
8128 VSX_VECTOR_MOVE(xvnegsp, OP_NEG, SGN_MASK_SP)
8129 VSX_VECTOR_MOVE(xvcpsgnsp, OP_CPSGN, SGN_MASK_SP)
8130
8131 #define GEN_VSX_HELPER_2(name, op1, op2, inval, type)                         \
8132 static void gen_##name(DisasContext * ctx)                                    \
8133 {                                                                             \
8134     TCGv_i32 opc;                                                             \
8135     if (unlikely(!ctx->vsx_enabled)) {                                        \
8136         gen_exception(ctx, POWERPC_EXCP_VSXU);                                \
8137         return;                                                               \
8138     }                                                                         \
8139     /* NIP cannot be restored if the memory exception comes from an helper */ \
8140     gen_update_nip(ctx, ctx->nip - 4);                                        \
8141     opc = tcg_const_i32(ctx->opcode);                                         \
8142     gen_helper_##name(cpu_env, opc);                                          \
8143     tcg_temp_free_i32(opc);                                                   \
8144 }
8145
8146 #define GEN_VSX_HELPER_XT_XB_ENV(name, op1, op2, inval, type) \
8147 static void gen_##name(DisasContext * ctx)                    \
8148 {                                                             \
8149     if (unlikely(!ctx->vsx_enabled)) {                        \
8150         gen_exception(ctx, POWERPC_EXCP_VSXU);                \
8151         return;                                               \
8152     }                                                         \
8153     /* NIP cannot be restored if the exception comes */       \
8154     /* from a helper. */                                      \
8155     gen_update_nip(ctx, ctx->nip - 4);                        \
8156                                                               \
8157     gen_helper_##name(cpu_vsrh(xT(ctx->opcode)), cpu_env,     \
8158                       cpu_vsrh(xB(ctx->opcode)));             \
8159 }
8160
8161 GEN_VSX_HELPER_2(xsadddp, 0x00, 0x04, 0, PPC2_VSX)
8162 GEN_VSX_HELPER_2(xssubdp, 0x00, 0x05, 0, PPC2_VSX)
8163 GEN_VSX_HELPER_2(xsmuldp, 0x00, 0x06, 0, PPC2_VSX)
8164 GEN_VSX_HELPER_2(xsdivdp, 0x00, 0x07, 0, PPC2_VSX)
8165 GEN_VSX_HELPER_2(xsredp, 0x14, 0x05, 0, PPC2_VSX)
8166 GEN_VSX_HELPER_2(xssqrtdp, 0x16, 0x04, 0, PPC2_VSX)
8167 GEN_VSX_HELPER_2(xsrsqrtedp, 0x14, 0x04, 0, PPC2_VSX)
8168 GEN_VSX_HELPER_2(xstdivdp, 0x14, 0x07, 0, PPC2_VSX)
8169 GEN_VSX_HELPER_2(xstsqrtdp, 0x14, 0x06, 0, PPC2_VSX)
8170 GEN_VSX_HELPER_2(xsmaddadp, 0x04, 0x04, 0, PPC2_VSX)
8171 GEN_VSX_HELPER_2(xsmaddmdp, 0x04, 0x05, 0, PPC2_VSX)
8172 GEN_VSX_HELPER_2(xsmsubadp, 0x04, 0x06, 0, PPC2_VSX)
8173 GEN_VSX_HELPER_2(xsmsubmdp, 0x04, 0x07, 0, PPC2_VSX)
8174 GEN_VSX_HELPER_2(xsnmaddadp, 0x04, 0x14, 0, PPC2_VSX)
8175 GEN_VSX_HELPER_2(xsnmaddmdp, 0x04, 0x15, 0, PPC2_VSX)
8176 GEN_VSX_HELPER_2(xsnmsubadp, 0x04, 0x16, 0, PPC2_VSX)
8177 GEN_VSX_HELPER_2(xsnmsubmdp, 0x04, 0x17, 0, PPC2_VSX)
8178 GEN_VSX_HELPER_2(xscmpodp, 0x0C, 0x05, 0, PPC2_VSX)
8179 GEN_VSX_HELPER_2(xscmpudp, 0x0C, 0x04, 0, PPC2_VSX)
8180 GEN_VSX_HELPER_2(xsmaxdp, 0x00, 0x14, 0, PPC2_VSX)
8181 GEN_VSX_HELPER_2(xsmindp, 0x00, 0x15, 0, PPC2_VSX)
8182 GEN_VSX_HELPER_2(xscvdpsp, 0x12, 0x10, 0, PPC2_VSX)
8183 GEN_VSX_HELPER_XT_XB_ENV(xscvdpspn, 0x16, 0x10, 0, PPC2_VSX207)
8184 GEN_VSX_HELPER_2(xscvspdp, 0x12, 0x14, 0, PPC2_VSX)
8185 GEN_VSX_HELPER_XT_XB_ENV(xscvspdpn, 0x16, 0x14, 0, PPC2_VSX207)
8186 GEN_VSX_HELPER_2(xscvdpsxds, 0x10, 0x15, 0, PPC2_VSX)
8187 GEN_VSX_HELPER_2(xscvdpsxws, 0x10, 0x05, 0, PPC2_VSX)
8188 GEN_VSX_HELPER_2(xscvdpuxds, 0x10, 0x14, 0, PPC2_VSX)
8189 GEN_VSX_HELPER_2(xscvdpuxws, 0x10, 0x04, 0, PPC2_VSX)
8190 GEN_VSX_HELPER_2(xscvsxddp, 0x10, 0x17, 0, PPC2_VSX)
8191 GEN_VSX_HELPER_2(xscvuxddp, 0x10, 0x16, 0, PPC2_VSX)
8192 GEN_VSX_HELPER_2(xsrdpi, 0x12, 0x04, 0, PPC2_VSX)
8193 GEN_VSX_HELPER_2(xsrdpic, 0x16, 0x06, 0, PPC2_VSX)
8194 GEN_VSX_HELPER_2(xsrdpim, 0x12, 0x07, 0, PPC2_VSX)
8195 GEN_VSX_HELPER_2(xsrdpip, 0x12, 0x06, 0, PPC2_VSX)
8196 GEN_VSX_HELPER_2(xsrdpiz, 0x12, 0x05, 0, PPC2_VSX)
8197 GEN_VSX_HELPER_XT_XB_ENV(xsrsp, 0x12, 0x11, 0, PPC2_VSX207)
8198
8199 GEN_VSX_HELPER_2(xsaddsp, 0x00, 0x00, 0, PPC2_VSX207)
8200 GEN_VSX_HELPER_2(xssubsp, 0x00, 0x01, 0, PPC2_VSX207)
8201 GEN_VSX_HELPER_2(xsmulsp, 0x00, 0x02, 0, PPC2_VSX207)
8202 GEN_VSX_HELPER_2(xsdivsp, 0x00, 0x03, 0, PPC2_VSX207)
8203 GEN_VSX_HELPER_2(xsresp, 0x14, 0x01, 0, PPC2_VSX207)
8204 GEN_VSX_HELPER_2(xssqrtsp, 0x16, 0x00, 0, PPC2_VSX207)
8205 GEN_VSX_HELPER_2(xsrsqrtesp, 0x14, 0x00, 0, PPC2_VSX207)
8206 GEN_VSX_HELPER_2(xsmaddasp, 0x04, 0x00, 0, PPC2_VSX207)
8207 GEN_VSX_HELPER_2(xsmaddmsp, 0x04, 0x01, 0, PPC2_VSX207)
8208 GEN_VSX_HELPER_2(xsmsubasp, 0x04, 0x02, 0, PPC2_VSX207)
8209 GEN_VSX_HELPER_2(xsmsubmsp, 0x04, 0x03, 0, PPC2_VSX207)
8210 GEN_VSX_HELPER_2(xsnmaddasp, 0x04, 0x10, 0, PPC2_VSX207)
8211 GEN_VSX_HELPER_2(xsnmaddmsp, 0x04, 0x11, 0, PPC2_VSX207)
8212 GEN_VSX_HELPER_2(xsnmsubasp, 0x04, 0x12, 0, PPC2_VSX207)
8213 GEN_VSX_HELPER_2(xsnmsubmsp, 0x04, 0x13, 0, PPC2_VSX207)
8214 GEN_VSX_HELPER_2(xscvsxdsp, 0x10, 0x13, 0, PPC2_VSX207)
8215 GEN_VSX_HELPER_2(xscvuxdsp, 0x10, 0x12, 0, PPC2_VSX207)
8216
8217 GEN_VSX_HELPER_2(xvadddp, 0x00, 0x0C, 0, PPC2_VSX)
8218 GEN_VSX_HELPER_2(xvsubdp, 0x00, 0x0D, 0, PPC2_VSX)
8219 GEN_VSX_HELPER_2(xvmuldp, 0x00, 0x0E, 0, PPC2_VSX)
8220 GEN_VSX_HELPER_2(xvdivdp, 0x00, 0x0F, 0, PPC2_VSX)
8221 GEN_VSX_HELPER_2(xvredp, 0x14, 0x0D, 0, PPC2_VSX)
8222 GEN_VSX_HELPER_2(xvsqrtdp, 0x16, 0x0C, 0, PPC2_VSX)
8223 GEN_VSX_HELPER_2(xvrsqrtedp, 0x14, 0x0C, 0, PPC2_VSX)
8224 GEN_VSX_HELPER_2(xvtdivdp, 0x14, 0x0F, 0, PPC2_VSX)
8225 GEN_VSX_HELPER_2(xvtsqrtdp, 0x14, 0x0E, 0, PPC2_VSX)
8226 GEN_VSX_HELPER_2(xvmaddadp, 0x04, 0x0C, 0, PPC2_VSX)
8227 GEN_VSX_HELPER_2(xvmaddmdp, 0x04, 0x0D, 0, PPC2_VSX)
8228 GEN_VSX_HELPER_2(xvmsubadp, 0x04, 0x0E, 0, PPC2_VSX)
8229 GEN_VSX_HELPER_2(xvmsubmdp, 0x04, 0x0F, 0, PPC2_VSX)
8230 GEN_VSX_HELPER_2(xvnmaddadp, 0x04, 0x1C, 0, PPC2_VSX)
8231 GEN_VSX_HELPER_2(xvnmaddmdp, 0x04, 0x1D, 0, PPC2_VSX)
8232 GEN_VSX_HELPER_2(xvnmsubadp, 0x04, 0x1E, 0, PPC2_VSX)
8233 GEN_VSX_HELPER_2(xvnmsubmdp, 0x04, 0x1F, 0, PPC2_VSX)
8234 GEN_VSX_HELPER_2(xvmaxdp, 0x00, 0x1C, 0, PPC2_VSX)
8235 GEN_VSX_HELPER_2(xvmindp, 0x00, 0x1D, 0, PPC2_VSX)
8236 GEN_VSX_HELPER_2(xvcmpeqdp, 0x0C, 0x0C, 0, PPC2_VSX)
8237 GEN_VSX_HELPER_2(xvcmpgtdp, 0x0C, 0x0D, 0, PPC2_VSX)
8238 GEN_VSX_HELPER_2(xvcmpgedp, 0x0C, 0x0E, 0, PPC2_VSX)
8239 GEN_VSX_HELPER_2(xvcvdpsp, 0x12, 0x18, 0, PPC2_VSX)
8240 GEN_VSX_HELPER_2(xvcvdpsxds, 0x10, 0x1D, 0, PPC2_VSX)
8241 GEN_VSX_HELPER_2(xvcvdpsxws, 0x10, 0x0D, 0, PPC2_VSX)
8242 GEN_VSX_HELPER_2(xvcvdpuxds, 0x10, 0x1C, 0, PPC2_VSX)
8243 GEN_VSX_HELPER_2(xvcvdpuxws, 0x10, 0x0C, 0, PPC2_VSX)
8244 GEN_VSX_HELPER_2(xvcvsxddp, 0x10, 0x1F, 0, PPC2_VSX)
8245 GEN_VSX_HELPER_2(xvcvuxddp, 0x10, 0x1E, 0, PPC2_VSX)
8246 GEN_VSX_HELPER_2(xvcvsxwdp, 0x10, 0x0F, 0, PPC2_VSX)
8247 GEN_VSX_HELPER_2(xvcvuxwdp, 0x10, 0x0E, 0, PPC2_VSX)
8248 GEN_VSX_HELPER_2(xvrdpi, 0x12, 0x0C, 0, PPC2_VSX)
8249 GEN_VSX_HELPER_2(xvrdpic, 0x16, 0x0E, 0, PPC2_VSX)
8250 GEN_VSX_HELPER_2(xvrdpim, 0x12, 0x0F, 0, PPC2_VSX)
8251 GEN_VSX_HELPER_2(xvrdpip, 0x12, 0x0E, 0, PPC2_VSX)
8252 GEN_VSX_HELPER_2(xvrdpiz, 0x12, 0x0D, 0, PPC2_VSX)
8253
8254 GEN_VSX_HELPER_2(xvaddsp, 0x00, 0x08, 0, PPC2_VSX)
8255 GEN_VSX_HELPER_2(xvsubsp, 0x00, 0x09, 0, PPC2_VSX)
8256 GEN_VSX_HELPER_2(xvmulsp, 0x00, 0x0A, 0, PPC2_VSX)
8257 GEN_VSX_HELPER_2(xvdivsp, 0x00, 0x0B, 0, PPC2_VSX)
8258 GEN_VSX_HELPER_2(xvresp, 0x14, 0x09, 0, PPC2_VSX)
8259 GEN_VSX_HELPER_2(xvsqrtsp, 0x16, 0x08, 0, PPC2_VSX)
8260 GEN_VSX_HELPER_2(xvrsqrtesp, 0x14, 0x08, 0, PPC2_VSX)
8261 GEN_VSX_HELPER_2(xvtdivsp, 0x14, 0x0B, 0, PPC2_VSX)
8262 GEN_VSX_HELPER_2(xvtsqrtsp, 0x14, 0x0A, 0, PPC2_VSX)
8263 GEN_VSX_HELPER_2(xvmaddasp, 0x04, 0x08, 0, PPC2_VSX)
8264 GEN_VSX_HELPER_2(xvmaddmsp, 0x04, 0x09, 0, PPC2_VSX)
8265 GEN_VSX_HELPER_2(xvmsubasp, 0x04, 0x0A, 0, PPC2_VSX)
8266 GEN_VSX_HELPER_2(xvmsubmsp, 0x04, 0x0B, 0, PPC2_VSX)
8267 GEN_VSX_HELPER_2(xvnmaddasp, 0x04, 0x18, 0, PPC2_VSX)
8268 GEN_VSX_HELPER_2(xvnmaddmsp, 0x04, 0x19, 0, PPC2_VSX)
8269 GEN_VSX_HELPER_2(xvnmsubasp, 0x04, 0x1A, 0, PPC2_VSX)
8270 GEN_VSX_HELPER_2(xvnmsubmsp, 0x04, 0x1B, 0, PPC2_VSX)
8271 GEN_VSX_HELPER_2(xvmaxsp, 0x00, 0x18, 0, PPC2_VSX)
8272 GEN_VSX_HELPER_2(xvminsp, 0x00, 0x19, 0, PPC2_VSX)
8273 GEN_VSX_HELPER_2(xvcmpeqsp, 0x0C, 0x08, 0, PPC2_VSX)
8274 GEN_VSX_HELPER_2(xvcmpgtsp, 0x0C, 0x09, 0, PPC2_VSX)
8275 GEN_VSX_HELPER_2(xvcmpgesp, 0x0C, 0x0A, 0, PPC2_VSX)
8276 GEN_VSX_HELPER_2(xvcvspdp, 0x12, 0x1C, 0, PPC2_VSX)
8277 GEN_VSX_HELPER_2(xvcvspsxds, 0x10, 0x19, 0, PPC2_VSX)
8278 GEN_VSX_HELPER_2(xvcvspsxws, 0x10, 0x09, 0, PPC2_VSX)
8279 GEN_VSX_HELPER_2(xvcvspuxds, 0x10, 0x18, 0, PPC2_VSX)
8280 GEN_VSX_HELPER_2(xvcvspuxws, 0x10, 0x08, 0, PPC2_VSX)
8281 GEN_VSX_HELPER_2(xvcvsxdsp, 0x10, 0x1B, 0, PPC2_VSX)
8282 GEN_VSX_HELPER_2(xvcvuxdsp, 0x10, 0x1A, 0, PPC2_VSX)
8283 GEN_VSX_HELPER_2(xvcvsxwsp, 0x10, 0x0B, 0, PPC2_VSX)
8284 GEN_VSX_HELPER_2(xvcvuxwsp, 0x10, 0x0A, 0, PPC2_VSX)
8285 GEN_VSX_HELPER_2(xvrspi, 0x12, 0x08, 0, PPC2_VSX)
8286 GEN_VSX_HELPER_2(xvrspic, 0x16, 0x0A, 0, PPC2_VSX)
8287 GEN_VSX_HELPER_2(xvrspim, 0x12, 0x0B, 0, PPC2_VSX)
8288 GEN_VSX_HELPER_2(xvrspip, 0x12, 0x0A, 0, PPC2_VSX)
8289 GEN_VSX_HELPER_2(xvrspiz, 0x12, 0x09, 0, PPC2_VSX)
8290
8291 #define VSX_LOGICAL(name, tcg_op)                                    \
8292 static void glue(gen_, name)(DisasContext * ctx)                     \
8293     {                                                                \
8294         if (unlikely(!ctx->vsx_enabled)) {                           \
8295             gen_exception(ctx, POWERPC_EXCP_VSXU);                   \
8296             return;                                                  \
8297         }                                                            \
8298         tcg_op(cpu_vsrh(xT(ctx->opcode)), cpu_vsrh(xA(ctx->opcode)), \
8299             cpu_vsrh(xB(ctx->opcode)));                              \
8300         tcg_op(cpu_vsrl(xT(ctx->opcode)), cpu_vsrl(xA(ctx->opcode)), \
8301             cpu_vsrl(xB(ctx->opcode)));                              \
8302     }
8303
8304 VSX_LOGICAL(xxland, tcg_gen_and_i64)
8305 VSX_LOGICAL(xxlandc, tcg_gen_andc_i64)
8306 VSX_LOGICAL(xxlor, tcg_gen_or_i64)
8307 VSX_LOGICAL(xxlxor, tcg_gen_xor_i64)
8308 VSX_LOGICAL(xxlnor, tcg_gen_nor_i64)
8309 VSX_LOGICAL(xxleqv, tcg_gen_eqv_i64)
8310 VSX_LOGICAL(xxlnand, tcg_gen_nand_i64)
8311 VSX_LOGICAL(xxlorc, tcg_gen_orc_i64)
8312
8313 #define VSX_XXMRG(name, high)                               \
8314 static void glue(gen_, name)(DisasContext * ctx)            \
8315     {                                                       \
8316         TCGv_i64 a0, a1, b0, b1;                            \
8317         if (unlikely(!ctx->vsx_enabled)) {                  \
8318             gen_exception(ctx, POWERPC_EXCP_VSXU);          \
8319             return;                                         \
8320         }                                                   \
8321         a0 = tcg_temp_new_i64();                            \
8322         a1 = tcg_temp_new_i64();                            \
8323         b0 = tcg_temp_new_i64();                            \
8324         b1 = tcg_temp_new_i64();                            \
8325         if (high) {                                         \
8326             tcg_gen_mov_i64(a0, cpu_vsrh(xA(ctx->opcode))); \
8327             tcg_gen_mov_i64(a1, cpu_vsrh(xA(ctx->opcode))); \
8328             tcg_gen_mov_i64(b0, cpu_vsrh(xB(ctx->opcode))); \
8329             tcg_gen_mov_i64(b1, cpu_vsrh(xB(ctx->opcode))); \
8330         } else {                                            \
8331             tcg_gen_mov_i64(a0, cpu_vsrl(xA(ctx->opcode))); \
8332             tcg_gen_mov_i64(a1, cpu_vsrl(xA(ctx->opcode))); \
8333             tcg_gen_mov_i64(b0, cpu_vsrl(xB(ctx->opcode))); \
8334             tcg_gen_mov_i64(b1, cpu_vsrl(xB(ctx->opcode))); \
8335         }                                                   \
8336         tcg_gen_shri_i64(a0, a0, 32);                       \
8337         tcg_gen_shri_i64(b0, b0, 32);                       \
8338         tcg_gen_deposit_i64(cpu_vsrh(xT(ctx->opcode)),      \
8339                             b0, a0, 32, 32);                \
8340         tcg_gen_deposit_i64(cpu_vsrl(xT(ctx->opcode)),      \
8341                             b1, a1, 32, 32);                \
8342         tcg_temp_free_i64(a0);                              \
8343         tcg_temp_free_i64(a1);                              \
8344         tcg_temp_free_i64(b0);                              \
8345         tcg_temp_free_i64(b1);                              \
8346     }
8347
8348 VSX_XXMRG(xxmrghw, 1)
8349 VSX_XXMRG(xxmrglw, 0)
8350
8351 static void gen_xxsel(DisasContext * ctx)
8352 {
8353     TCGv_i64 a, b, c;
8354     if (unlikely(!ctx->vsx_enabled)) {
8355         gen_exception(ctx, POWERPC_EXCP_VSXU);
8356         return;
8357     }
8358     a = tcg_temp_new_i64();
8359     b = tcg_temp_new_i64();
8360     c = tcg_temp_new_i64();
8361
8362     tcg_gen_mov_i64(a, cpu_vsrh(xA(ctx->opcode)));
8363     tcg_gen_mov_i64(b, cpu_vsrh(xB(ctx->opcode)));
8364     tcg_gen_mov_i64(c, cpu_vsrh(xC(ctx->opcode)));
8365
8366     tcg_gen_and_i64(b, b, c);
8367     tcg_gen_andc_i64(a, a, c);
8368     tcg_gen_or_i64(cpu_vsrh(xT(ctx->opcode)), a, b);
8369
8370     tcg_gen_mov_i64(a, cpu_vsrl(xA(ctx->opcode)));
8371     tcg_gen_mov_i64(b, cpu_vsrl(xB(ctx->opcode)));
8372     tcg_gen_mov_i64(c, cpu_vsrl(xC(ctx->opcode)));
8373
8374     tcg_gen_and_i64(b, b, c);
8375     tcg_gen_andc_i64(a, a, c);
8376     tcg_gen_or_i64(cpu_vsrl(xT(ctx->opcode)), a, b);
8377
8378     tcg_temp_free_i64(a);
8379     tcg_temp_free_i64(b);
8380     tcg_temp_free_i64(c);
8381 }
8382
8383 static void gen_xxspltw(DisasContext *ctx)
8384 {
8385     TCGv_i64 b, b2;
8386     TCGv_i64 vsr = (UIM(ctx->opcode) & 2) ?
8387                    cpu_vsrl(xB(ctx->opcode)) :
8388                    cpu_vsrh(xB(ctx->opcode));
8389
8390     if (unlikely(!ctx->vsx_enabled)) {
8391         gen_exception(ctx, POWERPC_EXCP_VSXU);
8392         return;
8393     }
8394
8395     b = tcg_temp_new_i64();
8396     b2 = tcg_temp_new_i64();
8397
8398     if (UIM(ctx->opcode) & 1) {
8399         tcg_gen_ext32u_i64(b, vsr);
8400     } else {
8401         tcg_gen_shri_i64(b, vsr, 32);
8402     }
8403
8404     tcg_gen_shli_i64(b2, b, 32);
8405     tcg_gen_or_i64(cpu_vsrh(xT(ctx->opcode)), b, b2);
8406     tcg_gen_mov_i64(cpu_vsrl(xT(ctx->opcode)), cpu_vsrh(xT(ctx->opcode)));
8407
8408     tcg_temp_free_i64(b);
8409     tcg_temp_free_i64(b2);
8410 }
8411
8412 static void gen_xxsldwi(DisasContext *ctx)
8413 {
8414     TCGv_i64 xth, xtl;
8415     if (unlikely(!ctx->vsx_enabled)) {
8416         gen_exception(ctx, POWERPC_EXCP_VSXU);
8417         return;
8418     }
8419     xth = tcg_temp_new_i64();
8420     xtl = tcg_temp_new_i64();
8421
8422     switch (SHW(ctx->opcode)) {
8423         case 0: {
8424             tcg_gen_mov_i64(xth, cpu_vsrh(xA(ctx->opcode)));
8425             tcg_gen_mov_i64(xtl, cpu_vsrl(xA(ctx->opcode)));
8426             break;
8427         }
8428         case 1: {
8429             TCGv_i64 t0 = tcg_temp_new_i64();
8430             tcg_gen_mov_i64(xth, cpu_vsrh(xA(ctx->opcode)));
8431             tcg_gen_shli_i64(xth, xth, 32);
8432             tcg_gen_mov_i64(t0, cpu_vsrl(xA(ctx->opcode)));
8433             tcg_gen_shri_i64(t0, t0, 32);
8434             tcg_gen_or_i64(xth, xth, t0);
8435             tcg_gen_mov_i64(xtl, cpu_vsrl(xA(ctx->opcode)));
8436             tcg_gen_shli_i64(xtl, xtl, 32);
8437             tcg_gen_mov_i64(t0, cpu_vsrh(xB(ctx->opcode)));
8438             tcg_gen_shri_i64(t0, t0, 32);
8439             tcg_gen_or_i64(xtl, xtl, t0);
8440             tcg_temp_free_i64(t0);
8441             break;
8442         }
8443         case 2: {
8444             tcg_gen_mov_i64(xth, cpu_vsrl(xA(ctx->opcode)));
8445             tcg_gen_mov_i64(xtl, cpu_vsrh(xB(ctx->opcode)));
8446             break;
8447         }
8448         case 3: {
8449             TCGv_i64 t0 = tcg_temp_new_i64();
8450             tcg_gen_mov_i64(xth, cpu_vsrl(xA(ctx->opcode)));
8451             tcg_gen_shli_i64(xth, xth, 32);
8452             tcg_gen_mov_i64(t0, cpu_vsrh(xB(ctx->opcode)));
8453             tcg_gen_shri_i64(t0, t0, 32);
8454             tcg_gen_or_i64(xth, xth, t0);
8455             tcg_gen_mov_i64(xtl, cpu_vsrh(xB(ctx->opcode)));
8456             tcg_gen_shli_i64(xtl, xtl, 32);
8457             tcg_gen_mov_i64(t0, cpu_vsrl(xB(ctx->opcode)));
8458             tcg_gen_shri_i64(t0, t0, 32);
8459             tcg_gen_or_i64(xtl, xtl, t0);
8460             tcg_temp_free_i64(t0);
8461             break;
8462         }
8463     }
8464
8465     tcg_gen_mov_i64(cpu_vsrh(xT(ctx->opcode)), xth);
8466     tcg_gen_mov_i64(cpu_vsrl(xT(ctx->opcode)), xtl);
8467
8468     tcg_temp_free_i64(xth);
8469     tcg_temp_free_i64(xtl);
8470 }
8471
8472 /*** Decimal Floating Point ***/
8473
8474 static inline TCGv_ptr gen_fprp_ptr(int reg)
8475 {
8476     TCGv_ptr r = tcg_temp_new_ptr();
8477     tcg_gen_addi_ptr(r, cpu_env, offsetof(CPUPPCState, fpr[reg]));
8478     return r;
8479 }
8480
8481 #define GEN_DFP_T_A_B_Rc(name)                   \
8482 static void gen_##name(DisasContext *ctx)        \
8483 {                                                \
8484     TCGv_ptr rd, ra, rb;                         \
8485     if (unlikely(!ctx->fpu_enabled)) {           \
8486         gen_exception(ctx, POWERPC_EXCP_FPU);    \
8487         return;                                  \
8488     }                                            \
8489     gen_update_nip(ctx, ctx->nip - 4);           \
8490     rd = gen_fprp_ptr(rD(ctx->opcode));          \
8491     ra = gen_fprp_ptr(rA(ctx->opcode));          \
8492     rb = gen_fprp_ptr(rB(ctx->opcode));          \
8493     gen_helper_##name(cpu_env, rd, ra, rb);      \
8494     if (unlikely(Rc(ctx->opcode) != 0)) {        \
8495         gen_set_cr1_from_fpscr(ctx);             \
8496     }                                            \
8497     tcg_temp_free_ptr(rd);                       \
8498     tcg_temp_free_ptr(ra);                       \
8499     tcg_temp_free_ptr(rb);                       \
8500 }
8501
8502 #define GEN_DFP_BF_A_B(name)                      \
8503 static void gen_##name(DisasContext *ctx)         \
8504 {                                                 \
8505     TCGv_ptr ra, rb;                              \
8506     if (unlikely(!ctx->fpu_enabled)) {            \
8507         gen_exception(ctx, POWERPC_EXCP_FPU);     \
8508         return;                                   \
8509     }                                             \
8510     gen_update_nip(ctx, ctx->nip - 4);            \
8511     ra = gen_fprp_ptr(rA(ctx->opcode));           \
8512     rb = gen_fprp_ptr(rB(ctx->opcode));           \
8513     gen_helper_##name(cpu_crf[crfD(ctx->opcode)], \
8514                       cpu_env, ra, rb);           \
8515     tcg_temp_free_ptr(ra);                        \
8516     tcg_temp_free_ptr(rb);                        \
8517 }
8518
8519 #define GEN_DFP_BF_A_DCM(name)                    \
8520 static void gen_##name(DisasContext *ctx)         \
8521 {                                                 \
8522     TCGv_ptr ra;                                  \
8523     TCGv_i32 dcm;                                 \
8524     if (unlikely(!ctx->fpu_enabled)) {            \
8525         gen_exception(ctx, POWERPC_EXCP_FPU);     \
8526         return;                                   \
8527     }                                             \
8528     gen_update_nip(ctx, ctx->nip - 4);            \
8529     ra = gen_fprp_ptr(rA(ctx->opcode));           \
8530     dcm = tcg_const_i32(DCM(ctx->opcode));        \
8531     gen_helper_##name(cpu_crf[crfD(ctx->opcode)], \
8532                       cpu_env, ra, dcm);          \
8533     tcg_temp_free_ptr(ra);                        \
8534     tcg_temp_free_i32(dcm);                       \
8535 }
8536
8537 #define GEN_DFP_T_B_U32_U32_Rc(name, u32f1, u32f2)    \
8538 static void gen_##name(DisasContext *ctx)             \
8539 {                                                     \
8540     TCGv_ptr rt, rb;                                  \
8541     TCGv_i32 u32_1, u32_2;                            \
8542     if (unlikely(!ctx->fpu_enabled)) {                \
8543         gen_exception(ctx, POWERPC_EXCP_FPU);         \
8544         return;                                       \
8545     }                                                 \
8546     gen_update_nip(ctx, ctx->nip - 4);                \
8547     rt = gen_fprp_ptr(rD(ctx->opcode));               \
8548     rb = gen_fprp_ptr(rB(ctx->opcode));               \
8549     u32_1 = tcg_const_i32(u32f1(ctx->opcode));        \
8550     u32_2 = tcg_const_i32(u32f2(ctx->opcode));        \
8551     gen_helper_##name(cpu_env, rt, rb, u32_1, u32_2); \
8552     if (unlikely(Rc(ctx->opcode) != 0)) {             \
8553         gen_set_cr1_from_fpscr(ctx);                  \
8554     }                                                 \
8555     tcg_temp_free_ptr(rt);                            \
8556     tcg_temp_free_ptr(rb);                            \
8557     tcg_temp_free_i32(u32_1);                         \
8558     tcg_temp_free_i32(u32_2);                         \
8559 }
8560
8561 #define GEN_DFP_T_A_B_I32_Rc(name, i32fld)       \
8562 static void gen_##name(DisasContext *ctx)        \
8563 {                                                \
8564     TCGv_ptr rt, ra, rb;                         \
8565     TCGv_i32 i32;                                \
8566     if (unlikely(!ctx->fpu_enabled)) {           \
8567         gen_exception(ctx, POWERPC_EXCP_FPU);    \
8568         return;                                  \
8569     }                                            \
8570     gen_update_nip(ctx, ctx->nip - 4);           \
8571     rt = gen_fprp_ptr(rD(ctx->opcode));          \
8572     ra = gen_fprp_ptr(rA(ctx->opcode));          \
8573     rb = gen_fprp_ptr(rB(ctx->opcode));          \
8574     i32 = tcg_const_i32(i32fld(ctx->opcode));    \
8575     gen_helper_##name(cpu_env, rt, ra, rb, i32); \
8576     if (unlikely(Rc(ctx->opcode) != 0)) {        \
8577         gen_set_cr1_from_fpscr(ctx);             \
8578     }                                            \
8579     tcg_temp_free_ptr(rt);                       \
8580     tcg_temp_free_ptr(rb);                       \
8581     tcg_temp_free_ptr(ra);                       \
8582     tcg_temp_free_i32(i32);                      \
8583     }
8584
8585 #define GEN_DFP_T_B_Rc(name)                     \
8586 static void gen_##name(DisasContext *ctx)        \
8587 {                                                \
8588     TCGv_ptr rt, rb;                             \
8589     if (unlikely(!ctx->fpu_enabled)) {           \
8590         gen_exception(ctx, POWERPC_EXCP_FPU);    \
8591         return;                                  \
8592     }                                            \
8593     gen_update_nip(ctx, ctx->nip - 4);           \
8594     rt = gen_fprp_ptr(rD(ctx->opcode));          \
8595     rb = gen_fprp_ptr(rB(ctx->opcode));          \
8596     gen_helper_##name(cpu_env, rt, rb);          \
8597     if (unlikely(Rc(ctx->opcode) != 0)) {        \
8598         gen_set_cr1_from_fpscr(ctx);             \
8599     }                                            \
8600     tcg_temp_free_ptr(rt);                       \
8601     tcg_temp_free_ptr(rb);                       \
8602     }
8603
8604 #define GEN_DFP_T_FPR_I32_Rc(name, fprfld, i32fld) \
8605 static void gen_##name(DisasContext *ctx)          \
8606 {                                                  \
8607     TCGv_ptr rt, rs;                               \
8608     TCGv_i32 i32;                                  \
8609     if (unlikely(!ctx->fpu_enabled)) {             \
8610         gen_exception(ctx, POWERPC_EXCP_FPU);      \
8611         return;                                    \
8612     }                                              \
8613     gen_update_nip(ctx, ctx->nip - 4);             \
8614     rt = gen_fprp_ptr(rD(ctx->opcode));            \
8615     rs = gen_fprp_ptr(fprfld(ctx->opcode));        \
8616     i32 = tcg_const_i32(i32fld(ctx->opcode));      \
8617     gen_helper_##name(cpu_env, rt, rs, i32);       \
8618     if (unlikely(Rc(ctx->opcode) != 0)) {          \
8619         gen_set_cr1_from_fpscr(ctx);               \
8620     }                                              \
8621     tcg_temp_free_ptr(rt);                         \
8622     tcg_temp_free_ptr(rs);                         \
8623     tcg_temp_free_i32(i32);                        \
8624 }
8625
8626 GEN_DFP_T_A_B_Rc(dadd)
8627 GEN_DFP_T_A_B_Rc(daddq)
8628 GEN_DFP_T_A_B_Rc(dsub)
8629 GEN_DFP_T_A_B_Rc(dsubq)
8630 GEN_DFP_T_A_B_Rc(dmul)
8631 GEN_DFP_T_A_B_Rc(dmulq)
8632 GEN_DFP_T_A_B_Rc(ddiv)
8633 GEN_DFP_T_A_B_Rc(ddivq)
8634 GEN_DFP_BF_A_B(dcmpu)
8635 GEN_DFP_BF_A_B(dcmpuq)
8636 GEN_DFP_BF_A_B(dcmpo)
8637 GEN_DFP_BF_A_B(dcmpoq)
8638 GEN_DFP_BF_A_DCM(dtstdc)
8639 GEN_DFP_BF_A_DCM(dtstdcq)
8640 GEN_DFP_BF_A_DCM(dtstdg)
8641 GEN_DFP_BF_A_DCM(dtstdgq)
8642 GEN_DFP_BF_A_B(dtstex)
8643 GEN_DFP_BF_A_B(dtstexq)
8644 GEN_DFP_BF_A_B(dtstsf)
8645 GEN_DFP_BF_A_B(dtstsfq)
8646 GEN_DFP_T_B_U32_U32_Rc(dquai, SIMM5, RMC)
8647 GEN_DFP_T_B_U32_U32_Rc(dquaiq, SIMM5, RMC)
8648 GEN_DFP_T_A_B_I32_Rc(dqua, RMC)
8649 GEN_DFP_T_A_B_I32_Rc(dquaq, RMC)
8650 GEN_DFP_T_A_B_I32_Rc(drrnd, RMC)
8651 GEN_DFP_T_A_B_I32_Rc(drrndq, RMC)
8652 GEN_DFP_T_B_U32_U32_Rc(drintx, FPW, RMC)
8653 GEN_DFP_T_B_U32_U32_Rc(drintxq, FPW, RMC)
8654 GEN_DFP_T_B_U32_U32_Rc(drintn, FPW, RMC)
8655 GEN_DFP_T_B_U32_U32_Rc(drintnq, FPW, RMC)
8656 GEN_DFP_T_B_Rc(dctdp)
8657 GEN_DFP_T_B_Rc(dctqpq)
8658 GEN_DFP_T_B_Rc(drsp)
8659 GEN_DFP_T_B_Rc(drdpq)
8660 GEN_DFP_T_B_Rc(dcffix)
8661 GEN_DFP_T_B_Rc(dcffixq)
8662 GEN_DFP_T_B_Rc(dctfix)
8663 GEN_DFP_T_B_Rc(dctfixq)
8664 GEN_DFP_T_FPR_I32_Rc(ddedpd, rB, SP)
8665 GEN_DFP_T_FPR_I32_Rc(ddedpdq, rB, SP)
8666 GEN_DFP_T_FPR_I32_Rc(denbcd, rB, SP)
8667 GEN_DFP_T_FPR_I32_Rc(denbcdq, rB, SP)
8668 GEN_DFP_T_B_Rc(dxex)
8669 GEN_DFP_T_B_Rc(dxexq)
8670 GEN_DFP_T_A_B_Rc(diex)
8671 GEN_DFP_T_A_B_Rc(diexq)
8672 GEN_DFP_T_FPR_I32_Rc(dscli, rA, DCM)
8673 GEN_DFP_T_FPR_I32_Rc(dscliq, rA, DCM)
8674 GEN_DFP_T_FPR_I32_Rc(dscri, rA, DCM)
8675 GEN_DFP_T_FPR_I32_Rc(dscriq, rA, DCM)
8676
8677 /***                           SPE extension                               ***/
8678 /* Register moves */
8679
8680 static inline void gen_evmra(DisasContext *ctx)
8681 {
8682
8683     if (unlikely(!ctx->spe_enabled)) {
8684         gen_exception(ctx, POWERPC_EXCP_SPEU);
8685         return;
8686     }
8687
8688     TCGv_i64 tmp = tcg_temp_new_i64();
8689
8690     /* tmp := rA_lo + rA_hi << 32 */
8691     tcg_gen_concat_tl_i64(tmp, cpu_gpr[rA(ctx->opcode)], cpu_gprh[rA(ctx->opcode)]);
8692
8693     /* spe_acc := tmp */
8694     tcg_gen_st_i64(tmp, cpu_env, offsetof(CPUPPCState, spe_acc));
8695     tcg_temp_free_i64(tmp);
8696
8697     /* rD := rA */
8698     tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)]);
8699     tcg_gen_mov_tl(cpu_gprh[rD(ctx->opcode)], cpu_gprh[rA(ctx->opcode)]);
8700 }
8701
8702 static inline void gen_load_gpr64(TCGv_i64 t, int reg)
8703 {
8704     tcg_gen_concat_tl_i64(t, cpu_gpr[reg], cpu_gprh[reg]);
8705 }
8706
8707 static inline void gen_store_gpr64(int reg, TCGv_i64 t)
8708 {
8709     tcg_gen_extr_i64_tl(cpu_gpr[reg], cpu_gprh[reg], t);
8710 }
8711
8712 #define GEN_SPE(name0, name1, opc2, opc3, inval0, inval1, type)         \
8713 static void glue(gen_, name0##_##name1)(DisasContext *ctx)                    \
8714 {                                                                             \
8715     if (Rc(ctx->opcode))                                                      \
8716         gen_##name1(ctx);                                                     \
8717     else                                                                      \
8718         gen_##name0(ctx);                                                     \
8719 }
8720
8721 /* Handler for undefined SPE opcodes */
8722 static inline void gen_speundef(DisasContext *ctx)
8723 {
8724     gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL);
8725 }
8726
8727 /* SPE logic */
8728 #define GEN_SPEOP_LOGIC2(name, tcg_op)                                        \
8729 static inline void gen_##name(DisasContext *ctx)                              \
8730 {                                                                             \
8731     if (unlikely(!ctx->spe_enabled)) {                                        \
8732         gen_exception(ctx, POWERPC_EXCP_SPEU);                                \
8733         return;                                                               \
8734     }                                                                         \
8735     tcg_op(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)],                \
8736            cpu_gpr[rB(ctx->opcode)]);                                         \
8737     tcg_op(cpu_gprh[rD(ctx->opcode)], cpu_gprh[rA(ctx->opcode)],              \
8738            cpu_gprh[rB(ctx->opcode)]);                                        \
8739 }
8740
8741 GEN_SPEOP_LOGIC2(evand, tcg_gen_and_tl);
8742 GEN_SPEOP_LOGIC2(evandc, tcg_gen_andc_tl);
8743 GEN_SPEOP_LOGIC2(evxor, tcg_gen_xor_tl);
8744 GEN_SPEOP_LOGIC2(evor, tcg_gen_or_tl);
8745 GEN_SPEOP_LOGIC2(evnor, tcg_gen_nor_tl);
8746 GEN_SPEOP_LOGIC2(eveqv, tcg_gen_eqv_tl);
8747 GEN_SPEOP_LOGIC2(evorc, tcg_gen_orc_tl);
8748 GEN_SPEOP_LOGIC2(evnand, tcg_gen_nand_tl);
8749
8750 /* SPE logic immediate */
8751 #define GEN_SPEOP_TCG_LOGIC_IMM2(name, tcg_opi)                               \
8752 static inline void gen_##name(DisasContext *ctx)                              \
8753 {                                                                             \
8754     TCGv_i32 t0;                                                              \
8755     if (unlikely(!ctx->spe_enabled)) {                                        \
8756         gen_exception(ctx, POWERPC_EXCP_SPEU);                                \
8757         return;                                                               \
8758     }                                                                         \
8759     t0 = tcg_temp_new_i32();                                                  \
8760                                                                               \
8761     tcg_gen_trunc_tl_i32(t0, cpu_gpr[rA(ctx->opcode)]);                       \
8762     tcg_opi(t0, t0, rB(ctx->opcode));                                         \
8763     tcg_gen_extu_i32_tl(cpu_gpr[rD(ctx->opcode)], t0);                        \
8764                                                                               \
8765     tcg_gen_trunc_tl_i32(t0, cpu_gprh[rA(ctx->opcode)]);                      \
8766     tcg_opi(t0, t0, rB(ctx->opcode));                                         \
8767     tcg_gen_extu_i32_tl(cpu_gprh[rD(ctx->opcode)], t0);                       \
8768                                                                               \
8769     tcg_temp_free_i32(t0);                                                    \
8770 }
8771 GEN_SPEOP_TCG_LOGIC_IMM2(evslwi, tcg_gen_shli_i32);
8772 GEN_SPEOP_TCG_LOGIC_IMM2(evsrwiu, tcg_gen_shri_i32);
8773 GEN_SPEOP_TCG_LOGIC_IMM2(evsrwis, tcg_gen_sari_i32);
8774 GEN_SPEOP_TCG_LOGIC_IMM2(evrlwi, tcg_gen_rotli_i32);
8775
8776 /* SPE arithmetic */
8777 #define GEN_SPEOP_ARITH1(name, tcg_op)                                        \
8778 static inline void gen_##name(DisasContext *ctx)                              \
8779 {                                                                             \
8780     TCGv_i32 t0;                                                              \
8781     if (unlikely(!ctx->spe_enabled)) {                                        \
8782         gen_exception(ctx, POWERPC_EXCP_SPEU);                                \
8783         return;                                                               \
8784     }                                                                         \
8785     t0 = tcg_temp_new_i32();                                                  \
8786                                                                               \
8787     tcg_gen_trunc_tl_i32(t0, cpu_gpr[rA(ctx->opcode)]);                       \
8788     tcg_op(t0, t0);                                                           \
8789     tcg_gen_extu_i32_tl(cpu_gpr[rD(ctx->opcode)], t0);                        \
8790                                                                               \
8791     tcg_gen_trunc_tl_i32(t0, cpu_gprh[rA(ctx->opcode)]);                      \
8792     tcg_op(t0, t0);                                                           \
8793     tcg_gen_extu_i32_tl(cpu_gprh[rD(ctx->opcode)], t0);                       \
8794                                                                               \
8795     tcg_temp_free_i32(t0);                                                    \
8796 }
8797
8798 static inline void gen_op_evabs(TCGv_i32 ret, TCGv_i32 arg1)
8799 {
8800     TCGLabel *l1 = gen_new_label();
8801     TCGLabel *l2 = gen_new_label();
8802
8803     tcg_gen_brcondi_i32(TCG_COND_GE, arg1, 0, l1);
8804     tcg_gen_neg_i32(ret, arg1);
8805     tcg_gen_br(l2);
8806     gen_set_label(l1);
8807     tcg_gen_mov_i32(ret, arg1);
8808     gen_set_label(l2);
8809 }
8810 GEN_SPEOP_ARITH1(evabs, gen_op_evabs);
8811 GEN_SPEOP_ARITH1(evneg, tcg_gen_neg_i32);
8812 GEN_SPEOP_ARITH1(evextsb, tcg_gen_ext8s_i32);
8813 GEN_SPEOP_ARITH1(evextsh, tcg_gen_ext16s_i32);
8814 static inline void gen_op_evrndw(TCGv_i32 ret, TCGv_i32 arg1)
8815 {
8816     tcg_gen_addi_i32(ret, arg1, 0x8000);
8817     tcg_gen_ext16u_i32(ret, ret);
8818 }
8819 GEN_SPEOP_ARITH1(evrndw, gen_op_evrndw);
8820 GEN_SPEOP_ARITH1(evcntlsw, gen_helper_cntlsw32);
8821 GEN_SPEOP_ARITH1(evcntlzw, gen_helper_cntlzw32);
8822
8823 #define GEN_SPEOP_ARITH2(name, tcg_op)                                        \
8824 static inline void gen_##name(DisasContext *ctx)                              \
8825 {                                                                             \
8826     TCGv_i32 t0, t1;                                                          \
8827     if (unlikely(!ctx->spe_enabled)) {                                        \
8828         gen_exception(ctx, POWERPC_EXCP_SPEU);                                \
8829         return;                                                               \
8830     }                                                                         \
8831     t0 = tcg_temp_new_i32();                                                  \
8832     t1 = tcg_temp_new_i32();                                                  \
8833                                                                               \
8834     tcg_gen_trunc_tl_i32(t0, cpu_gpr[rA(ctx->opcode)]);                       \
8835     tcg_gen_trunc_tl_i32(t1, cpu_gpr[rB(ctx->opcode)]);                       \
8836     tcg_op(t0, t0, t1);                                                       \
8837     tcg_gen_extu_i32_tl(cpu_gpr[rD(ctx->opcode)], t0);                        \
8838                                                                               \
8839     tcg_gen_trunc_tl_i32(t0, cpu_gprh[rA(ctx->opcode)]);                      \
8840     tcg_gen_trunc_tl_i32(t1, cpu_gprh[rB(ctx->opcode)]);                      \
8841     tcg_op(t0, t0, t1);                                                       \
8842     tcg_gen_extu_i32_tl(cpu_gprh[rD(ctx->opcode)], t0);                       \
8843                                                                               \
8844     tcg_temp_free_i32(t0);                                                    \
8845     tcg_temp_free_i32(t1);                                                    \
8846 }
8847
8848 static inline void gen_op_evsrwu(TCGv_i32 ret, TCGv_i32 arg1, TCGv_i32 arg2)
8849 {
8850     TCGLabel *l1 = gen_new_label();
8851     TCGLabel *l2 = gen_new_label();
8852     TCGv_i32 t0 = tcg_temp_local_new_i32();
8853
8854     /* No error here: 6 bits are used */
8855     tcg_gen_andi_i32(t0, arg2, 0x3F);
8856     tcg_gen_brcondi_i32(TCG_COND_GE, t0, 32, l1);
8857     tcg_gen_shr_i32(ret, arg1, t0);
8858     tcg_gen_br(l2);
8859     gen_set_label(l1);
8860     tcg_gen_movi_i32(ret, 0);
8861     gen_set_label(l2);
8862     tcg_temp_free_i32(t0);
8863 }
8864 GEN_SPEOP_ARITH2(evsrwu, gen_op_evsrwu);
8865 static inline void gen_op_evsrws(TCGv_i32 ret, TCGv_i32 arg1, TCGv_i32 arg2)
8866 {
8867     TCGLabel *l1 = gen_new_label();
8868     TCGLabel *l2 = gen_new_label();
8869     TCGv_i32 t0 = tcg_temp_local_new_i32();
8870
8871     /* No error here: 6 bits are used */
8872     tcg_gen_andi_i32(t0, arg2, 0x3F);
8873     tcg_gen_brcondi_i32(TCG_COND_GE, t0, 32, l1);
8874     tcg_gen_sar_i32(ret, arg1, t0);
8875     tcg_gen_br(l2);
8876     gen_set_label(l1);
8877     tcg_gen_movi_i32(ret, 0);
8878     gen_set_label(l2);
8879     tcg_temp_free_i32(t0);
8880 }
8881 GEN_SPEOP_ARITH2(evsrws, gen_op_evsrws);
8882 static inline void gen_op_evslw(TCGv_i32 ret, TCGv_i32 arg1, TCGv_i32 arg2)
8883 {
8884     TCGLabel *l1 = gen_new_label();
8885     TCGLabel *l2 = gen_new_label();
8886     TCGv_i32 t0 = tcg_temp_local_new_i32();
8887
8888     /* No error here: 6 bits are used */
8889     tcg_gen_andi_i32(t0, arg2, 0x3F);
8890     tcg_gen_brcondi_i32(TCG_COND_GE, t0, 32, l1);
8891     tcg_gen_shl_i32(ret, arg1, t0);
8892     tcg_gen_br(l2);
8893     gen_set_label(l1);
8894     tcg_gen_movi_i32(ret, 0);
8895     gen_set_label(l2);
8896     tcg_temp_free_i32(t0);
8897 }
8898 GEN_SPEOP_ARITH2(evslw, gen_op_evslw);
8899 static inline void gen_op_evrlw(TCGv_i32 ret, TCGv_i32 arg1, TCGv_i32 arg2)
8900 {
8901     TCGv_i32 t0 = tcg_temp_new_i32();
8902     tcg_gen_andi_i32(t0, arg2, 0x1F);
8903     tcg_gen_rotl_i32(ret, arg1, t0);
8904     tcg_temp_free_i32(t0);
8905 }
8906 GEN_SPEOP_ARITH2(evrlw, gen_op_evrlw);
8907 static inline void gen_evmergehi(DisasContext *ctx)
8908 {
8909     if (unlikely(!ctx->spe_enabled)) {
8910         gen_exception(ctx, POWERPC_EXCP_SPEU);
8911         return;
8912     }
8913     tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_gprh[rB(ctx->opcode)]);
8914     tcg_gen_mov_tl(cpu_gprh[rD(ctx->opcode)], cpu_gprh[rA(ctx->opcode)]);
8915 }
8916 GEN_SPEOP_ARITH2(evaddw, tcg_gen_add_i32);
8917 static inline void gen_op_evsubf(TCGv_i32 ret, TCGv_i32 arg1, TCGv_i32 arg2)
8918 {
8919     tcg_gen_sub_i32(ret, arg2, arg1);
8920 }
8921 GEN_SPEOP_ARITH2(evsubfw, gen_op_evsubf);
8922
8923 /* SPE arithmetic immediate */
8924 #define GEN_SPEOP_ARITH_IMM2(name, tcg_op)                                    \
8925 static inline void gen_##name(DisasContext *ctx)                              \
8926 {                                                                             \
8927     TCGv_i32 t0;                                                              \
8928     if (unlikely(!ctx->spe_enabled)) {                                        \
8929         gen_exception(ctx, POWERPC_EXCP_SPEU);                                \
8930         return;                                                               \
8931     }                                                                         \
8932     t0 = tcg_temp_new_i32();                                                  \
8933                                                                               \
8934     tcg_gen_trunc_tl_i32(t0, cpu_gpr[rB(ctx->opcode)]);                       \
8935     tcg_op(t0, t0, rA(ctx->opcode));                                          \
8936     tcg_gen_extu_i32_tl(cpu_gpr[rD(ctx->opcode)], t0);                        \
8937                                                                               \
8938     tcg_gen_trunc_tl_i32(t0, cpu_gprh[rB(ctx->opcode)]);                      \
8939     tcg_op(t0, t0, rA(ctx->opcode));                                          \
8940     tcg_gen_extu_i32_tl(cpu_gprh[rD(ctx->opcode)], t0);                       \
8941                                                                               \
8942     tcg_temp_free_i32(t0);                                                    \
8943 }
8944 GEN_SPEOP_ARITH_IMM2(evaddiw, tcg_gen_addi_i32);
8945 GEN_SPEOP_ARITH_IMM2(evsubifw, tcg_gen_subi_i32);
8946
8947 /* SPE comparison */
8948 #define GEN_SPEOP_COMP(name, tcg_cond)                                        \
8949 static inline void gen_##name(DisasContext *ctx)                              \
8950 {                                                                             \
8951     if (unlikely(!ctx->spe_enabled)) {                                        \
8952         gen_exception(ctx, POWERPC_EXCP_SPEU);                                \
8953         return;                                                               \
8954     }                                                                         \
8955     TCGLabel *l1 = gen_new_label();                                           \
8956     TCGLabel *l2 = gen_new_label();                                           \
8957     TCGLabel *l3 = gen_new_label();                                           \
8958     TCGLabel *l4 = gen_new_label();                                           \
8959                                                                               \
8960     tcg_gen_ext32s_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rA(ctx->opcode)]);    \
8961     tcg_gen_ext32s_tl(cpu_gpr[rB(ctx->opcode)], cpu_gpr[rB(ctx->opcode)]);    \
8962     tcg_gen_ext32s_tl(cpu_gprh[rA(ctx->opcode)], cpu_gprh[rA(ctx->opcode)]);  \
8963     tcg_gen_ext32s_tl(cpu_gprh[rB(ctx->opcode)], cpu_gprh[rB(ctx->opcode)]);  \
8964                                                                               \
8965     tcg_gen_brcond_tl(tcg_cond, cpu_gpr[rA(ctx->opcode)],                     \
8966                        cpu_gpr[rB(ctx->opcode)], l1);                         \
8967     tcg_gen_movi_i32(cpu_crf[crfD(ctx->opcode)], 0);                          \
8968     tcg_gen_br(l2);                                                           \
8969     gen_set_label(l1);                                                        \
8970     tcg_gen_movi_i32(cpu_crf[crfD(ctx->opcode)],                              \
8971                      CRF_CL | CRF_CH_OR_CL | CRF_CH_AND_CL);                  \
8972     gen_set_label(l2);                                                        \
8973     tcg_gen_brcond_tl(tcg_cond, cpu_gprh[rA(ctx->opcode)],                    \
8974                        cpu_gprh[rB(ctx->opcode)], l3);                        \
8975     tcg_gen_andi_i32(cpu_crf[crfD(ctx->opcode)], cpu_crf[crfD(ctx->opcode)],  \
8976                      ~(CRF_CH | CRF_CH_AND_CL));                              \
8977     tcg_gen_br(l4);                                                           \
8978     gen_set_label(l3);                                                        \
8979     tcg_gen_ori_i32(cpu_crf[crfD(ctx->opcode)], cpu_crf[crfD(ctx->opcode)],   \
8980                     CRF_CH | CRF_CH_OR_CL);                                   \
8981     gen_set_label(l4);                                                        \
8982 }
8983 GEN_SPEOP_COMP(evcmpgtu, TCG_COND_GTU);
8984 GEN_SPEOP_COMP(evcmpgts, TCG_COND_GT);
8985 GEN_SPEOP_COMP(evcmpltu, TCG_COND_LTU);
8986 GEN_SPEOP_COMP(evcmplts, TCG_COND_LT);
8987 GEN_SPEOP_COMP(evcmpeq, TCG_COND_EQ);
8988
8989 /* SPE misc */
8990 static inline void gen_brinc(DisasContext *ctx)
8991 {
8992     /* Note: brinc is usable even if SPE is disabled */
8993     gen_helper_brinc(cpu_gpr[rD(ctx->opcode)],
8994                      cpu_gpr[rA(ctx->opcode)], cpu_gpr[rB(ctx->opcode)]);
8995 }
8996 static inline void gen_evmergelo(DisasContext *ctx)
8997 {
8998     if (unlikely(!ctx->spe_enabled)) {
8999         gen_exception(ctx, POWERPC_EXCP_SPEU);
9000         return;
9001     }
9002     tcg_gen_mov_tl(cpu_gprh[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)]);
9003     tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rB(ctx->opcode)]);
9004 }
9005 static inline void gen_evmergehilo(DisasContext *ctx)
9006 {
9007     if (unlikely(!ctx->spe_enabled)) {
9008         gen_exception(ctx, POWERPC_EXCP_SPEU);
9009         return;
9010     }
9011     tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rB(ctx->opcode)]);
9012     tcg_gen_mov_tl(cpu_gprh[rD(ctx->opcode)], cpu_gprh[rA(ctx->opcode)]);
9013 }
9014 static inline void gen_evmergelohi(DisasContext *ctx)
9015 {
9016     if (unlikely(!ctx->spe_enabled)) {
9017         gen_exception(ctx, POWERPC_EXCP_SPEU);
9018         return;
9019     }
9020     if (rD(ctx->opcode) == rA(ctx->opcode)) {
9021         TCGv tmp = tcg_temp_new();
9022         tcg_gen_mov_tl(tmp, cpu_gpr[rA(ctx->opcode)]);
9023         tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_gprh[rB(ctx->opcode)]);
9024         tcg_gen_mov_tl(cpu_gprh[rD(ctx->opcode)], tmp);
9025         tcg_temp_free(tmp);
9026     } else {
9027         tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_gprh[rB(ctx->opcode)]);
9028         tcg_gen_mov_tl(cpu_gprh[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)]);
9029     }
9030 }
9031 static inline void gen_evsplati(DisasContext *ctx)
9032 {
9033     uint64_t imm = ((int32_t)(rA(ctx->opcode) << 27)) >> 27;
9034
9035     tcg_gen_movi_tl(cpu_gpr[rD(ctx->opcode)], imm);
9036     tcg_gen_movi_tl(cpu_gprh[rD(ctx->opcode)], imm);
9037 }
9038 static inline void gen_evsplatfi(DisasContext *ctx)
9039 {
9040     uint64_t imm = rA(ctx->opcode) << 27;
9041
9042     tcg_gen_movi_tl(cpu_gpr[rD(ctx->opcode)], imm);
9043     tcg_gen_movi_tl(cpu_gprh[rD(ctx->opcode)], imm);
9044 }
9045
9046 static inline void gen_evsel(DisasContext *ctx)
9047 {
9048     TCGLabel *l1 = gen_new_label();
9049     TCGLabel *l2 = gen_new_label();
9050     TCGLabel *l3 = gen_new_label();
9051     TCGLabel *l4 = gen_new_label();
9052     TCGv_i32 t0 = tcg_temp_local_new_i32();
9053
9054     tcg_gen_andi_i32(t0, cpu_crf[ctx->opcode & 0x07], 1 << 3);
9055     tcg_gen_brcondi_i32(TCG_COND_EQ, t0, 0, l1);
9056     tcg_gen_mov_tl(cpu_gprh[rD(ctx->opcode)], cpu_gprh[rA(ctx->opcode)]);
9057     tcg_gen_br(l2);
9058     gen_set_label(l1);
9059     tcg_gen_mov_tl(cpu_gprh[rD(ctx->opcode)], cpu_gprh[rB(ctx->opcode)]);
9060     gen_set_label(l2);
9061     tcg_gen_andi_i32(t0, cpu_crf[ctx->opcode & 0x07], 1 << 2);
9062     tcg_gen_brcondi_i32(TCG_COND_EQ, t0, 0, l3);
9063     tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)]);
9064     tcg_gen_br(l4);
9065     gen_set_label(l3);
9066     tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rB(ctx->opcode)]);
9067     gen_set_label(l4);
9068     tcg_temp_free_i32(t0);
9069 }
9070
9071 static void gen_evsel0(DisasContext *ctx)
9072 {
9073     gen_evsel(ctx);
9074 }
9075
9076 static void gen_evsel1(DisasContext *ctx)
9077 {
9078     gen_evsel(ctx);
9079 }
9080
9081 static void gen_evsel2(DisasContext *ctx)
9082 {
9083     gen_evsel(ctx);
9084 }
9085
9086 static void gen_evsel3(DisasContext *ctx)
9087 {
9088     gen_evsel(ctx);
9089 }
9090
9091 /* Multiply */
9092
9093 static inline void gen_evmwumi(DisasContext *ctx)
9094 {
9095     TCGv_i64 t0, t1;
9096
9097     if (unlikely(!ctx->spe_enabled)) {
9098         gen_exception(ctx, POWERPC_EXCP_SPEU);
9099         return;
9100     }
9101
9102     t0 = tcg_temp_new_i64();
9103     t1 = tcg_temp_new_i64();
9104
9105     /* t0 := rA; t1 := rB */
9106     tcg_gen_extu_tl_i64(t0, cpu_gpr[rA(ctx->opcode)]);
9107     tcg_gen_ext32u_i64(t0, t0);
9108     tcg_gen_extu_tl_i64(t1, cpu_gpr[rB(ctx->opcode)]);
9109     tcg_gen_ext32u_i64(t1, t1);
9110
9111     tcg_gen_mul_i64(t0, t0, t1);  /* t0 := rA * rB */
9112
9113     gen_store_gpr64(rD(ctx->opcode), t0); /* rD := t0 */
9114
9115     tcg_temp_free_i64(t0);
9116     tcg_temp_free_i64(t1);
9117 }
9118
9119 static inline void gen_evmwumia(DisasContext *ctx)
9120 {
9121     TCGv_i64 tmp;
9122
9123     if (unlikely(!ctx->spe_enabled)) {
9124         gen_exception(ctx, POWERPC_EXCP_SPEU);
9125         return;
9126     }
9127
9128     gen_evmwumi(ctx);            /* rD := rA * rB */
9129
9130     tmp = tcg_temp_new_i64();
9131
9132     /* acc := rD */
9133     gen_load_gpr64(tmp, rD(ctx->opcode));
9134     tcg_gen_st_i64(tmp, cpu_env, offsetof(CPUPPCState, spe_acc));
9135     tcg_temp_free_i64(tmp);
9136 }
9137
9138 static inline void gen_evmwumiaa(DisasContext *ctx)
9139 {
9140     TCGv_i64 acc;
9141     TCGv_i64 tmp;
9142
9143     if (unlikely(!ctx->spe_enabled)) {
9144         gen_exception(ctx, POWERPC_EXCP_SPEU);
9145         return;
9146     }
9147
9148     gen_evmwumi(ctx);           /* rD := rA * rB */
9149
9150     acc = tcg_temp_new_i64();
9151     tmp = tcg_temp_new_i64();
9152
9153     /* tmp := rD */
9154     gen_load_gpr64(tmp, rD(ctx->opcode));
9155
9156     /* Load acc */
9157     tcg_gen_ld_i64(acc, cpu_env, offsetof(CPUPPCState, spe_acc));
9158
9159     /* acc := tmp + acc */
9160     tcg_gen_add_i64(acc, acc, tmp);
9161
9162     /* Store acc */
9163     tcg_gen_st_i64(acc, cpu_env, offsetof(CPUPPCState, spe_acc));
9164
9165     /* rD := acc */
9166     gen_store_gpr64(rD(ctx->opcode), acc);
9167
9168     tcg_temp_free_i64(acc);
9169     tcg_temp_free_i64(tmp);
9170 }
9171
9172 static inline void gen_evmwsmi(DisasContext *ctx)
9173 {
9174     TCGv_i64 t0, t1;
9175
9176     if (unlikely(!ctx->spe_enabled)) {
9177         gen_exception(ctx, POWERPC_EXCP_SPEU);
9178         return;
9179     }
9180
9181     t0 = tcg_temp_new_i64();
9182     t1 = tcg_temp_new_i64();
9183
9184     /* t0 := rA; t1 := rB */
9185     tcg_gen_extu_tl_i64(t0, cpu_gpr[rA(ctx->opcode)]);
9186     tcg_gen_ext32s_i64(t0, t0);
9187     tcg_gen_extu_tl_i64(t1, cpu_gpr[rB(ctx->opcode)]);
9188     tcg_gen_ext32s_i64(t1, t1);
9189
9190     tcg_gen_mul_i64(t0, t0, t1);  /* t0 := rA * rB */
9191
9192     gen_store_gpr64(rD(ctx->opcode), t0); /* rD := t0 */
9193
9194     tcg_temp_free_i64(t0);
9195     tcg_temp_free_i64(t1);
9196 }
9197
9198 static inline void gen_evmwsmia(DisasContext *ctx)
9199 {
9200     TCGv_i64 tmp;
9201
9202     gen_evmwsmi(ctx);            /* rD := rA * rB */
9203
9204     tmp = tcg_temp_new_i64();
9205
9206     /* acc := rD */
9207     gen_load_gpr64(tmp, rD(ctx->opcode));
9208     tcg_gen_st_i64(tmp, cpu_env, offsetof(CPUPPCState, spe_acc));
9209
9210     tcg_temp_free_i64(tmp);
9211 }
9212
9213 static inline void gen_evmwsmiaa(DisasContext *ctx)
9214 {
9215     TCGv_i64 acc = tcg_temp_new_i64();
9216     TCGv_i64 tmp = tcg_temp_new_i64();
9217
9218     gen_evmwsmi(ctx);           /* rD := rA * rB */
9219
9220     acc = tcg_temp_new_i64();
9221     tmp = tcg_temp_new_i64();
9222
9223     /* tmp := rD */
9224     gen_load_gpr64(tmp, rD(ctx->opcode));
9225
9226     /* Load acc */
9227     tcg_gen_ld_i64(acc, cpu_env, offsetof(CPUPPCState, spe_acc));
9228
9229     /* acc := tmp + acc */
9230     tcg_gen_add_i64(acc, acc, tmp);
9231
9232     /* Store acc */
9233     tcg_gen_st_i64(acc, cpu_env, offsetof(CPUPPCState, spe_acc));
9234
9235     /* rD := acc */
9236     gen_store_gpr64(rD(ctx->opcode), acc);
9237
9238     tcg_temp_free_i64(acc);
9239     tcg_temp_free_i64(tmp);
9240 }
9241
9242 GEN_SPE(evaddw,      speundef,    0x00, 0x08, 0x00000000, 0xFFFFFFFF, PPC_SPE); ////
9243 GEN_SPE(evaddiw,     speundef,    0x01, 0x08, 0x00000000, 0xFFFFFFFF, PPC_SPE);
9244 GEN_SPE(evsubfw,     speundef,    0x02, 0x08, 0x00000000, 0xFFFFFFFF, PPC_SPE); ////
9245 GEN_SPE(evsubifw,    speundef,    0x03, 0x08, 0x00000000, 0xFFFFFFFF, PPC_SPE);
9246 GEN_SPE(evabs,       evneg,       0x04, 0x08, 0x0000F800, 0x0000F800, PPC_SPE); ////
9247 GEN_SPE(evextsb,     evextsh,     0x05, 0x08, 0x0000F800, 0x0000F800, PPC_SPE); ////
9248 GEN_SPE(evrndw,      evcntlzw,    0x06, 0x08, 0x0000F800, 0x0000F800, PPC_SPE); ////
9249 GEN_SPE(evcntlsw,    brinc,       0x07, 0x08, 0x0000F800, 0x00000000, PPC_SPE); //
9250 GEN_SPE(evmra,       speundef,    0x02, 0x13, 0x0000F800, 0xFFFFFFFF, PPC_SPE);
9251 GEN_SPE(speundef,    evand,       0x08, 0x08, 0xFFFFFFFF, 0x00000000, PPC_SPE); ////
9252 GEN_SPE(evandc,      speundef,    0x09, 0x08, 0x00000000, 0xFFFFFFFF, PPC_SPE); ////
9253 GEN_SPE(evxor,       evor,        0x0B, 0x08, 0x00000000, 0x00000000, PPC_SPE); ////
9254 GEN_SPE(evnor,       eveqv,       0x0C, 0x08, 0x00000000, 0x00000000, PPC_SPE); ////
9255 GEN_SPE(evmwumi,     evmwsmi,     0x0C, 0x11, 0x00000000, 0x00000000, PPC_SPE);
9256 GEN_SPE(evmwumia,    evmwsmia,    0x1C, 0x11, 0x00000000, 0x00000000, PPC_SPE);
9257 GEN_SPE(evmwumiaa,   evmwsmiaa,   0x0C, 0x15, 0x00000000, 0x00000000, PPC_SPE);
9258 GEN_SPE(speundef,    evorc,       0x0D, 0x08, 0xFFFFFFFF, 0x00000000, PPC_SPE); ////
9259 GEN_SPE(evnand,      speundef,    0x0F, 0x08, 0x00000000, 0xFFFFFFFF, PPC_SPE); ////
9260 GEN_SPE(evsrwu,      evsrws,      0x10, 0x08, 0x00000000, 0x00000000, PPC_SPE); ////
9261 GEN_SPE(evsrwiu,     evsrwis,     0x11, 0x08, 0x00000000, 0x00000000, PPC_SPE);
9262 GEN_SPE(evslw,       speundef,    0x12, 0x08, 0x00000000, 0xFFFFFFFF, PPC_SPE); ////
9263 GEN_SPE(evslwi,      speundef,    0x13, 0x08, 0x00000000, 0xFFFFFFFF, PPC_SPE);
9264 GEN_SPE(evrlw,       evsplati,    0x14, 0x08, 0x00000000, 0x0000F800, PPC_SPE); //
9265 GEN_SPE(evrlwi,      evsplatfi,   0x15, 0x08, 0x00000000, 0x0000F800, PPC_SPE);
9266 GEN_SPE(evmergehi,   evmergelo,   0x16, 0x08, 0x00000000, 0x00000000, PPC_SPE); ////
9267 GEN_SPE(evmergehilo, evmergelohi, 0x17, 0x08, 0x00000000, 0x00000000, PPC_SPE); ////
9268 GEN_SPE(evcmpgtu,    evcmpgts,    0x18, 0x08, 0x00600000, 0x00600000, PPC_SPE); ////
9269 GEN_SPE(evcmpltu,    evcmplts,    0x19, 0x08, 0x00600000, 0x00600000, PPC_SPE); ////
9270 GEN_SPE(evcmpeq,     speundef,    0x1A, 0x08, 0x00600000, 0xFFFFFFFF, PPC_SPE); ////
9271
9272 /* SPE load and stores */
9273 static inline void gen_addr_spe_imm_index(DisasContext *ctx, TCGv EA, int sh)
9274 {
9275     target_ulong uimm = rB(ctx->opcode);
9276
9277     if (rA(ctx->opcode) == 0) {
9278         tcg_gen_movi_tl(EA, uimm << sh);
9279     } else {
9280         tcg_gen_addi_tl(EA, cpu_gpr[rA(ctx->opcode)], uimm << sh);
9281         if (NARROW_MODE(ctx)) {
9282             tcg_gen_ext32u_tl(EA, EA);
9283         }
9284     }
9285 }
9286
9287 static inline void gen_op_evldd(DisasContext *ctx, TCGv addr)
9288 {
9289     TCGv_i64 t0 = tcg_temp_new_i64();
9290     gen_qemu_ld64(ctx, t0, addr);
9291     gen_store_gpr64(rD(ctx->opcode), t0);
9292     tcg_temp_free_i64(t0);
9293 }
9294
9295 static inline void gen_op_evldw(DisasContext *ctx, TCGv addr)
9296 {
9297     gen_qemu_ld32u(ctx, cpu_gprh[rD(ctx->opcode)], addr);
9298     gen_addr_add(ctx, addr, addr, 4);
9299     gen_qemu_ld32u(ctx, cpu_gpr[rD(ctx->opcode)], addr);
9300 }
9301
9302 static inline void gen_op_evldh(DisasContext *ctx, TCGv addr)
9303 {
9304     TCGv t0 = tcg_temp_new();
9305     gen_qemu_ld16u(ctx, t0, addr);
9306     tcg_gen_shli_tl(cpu_gprh[rD(ctx->opcode)], t0, 16);
9307     gen_addr_add(ctx, addr, addr, 2);
9308     gen_qemu_ld16u(ctx, t0, addr);
9309     tcg_gen_or_tl(cpu_gprh[rD(ctx->opcode)], cpu_gprh[rD(ctx->opcode)], t0);
9310     gen_addr_add(ctx, addr, addr, 2);
9311     gen_qemu_ld16u(ctx, t0, addr);
9312     tcg_gen_shli_tl(cpu_gprh[rD(ctx->opcode)], t0, 16);
9313     gen_addr_add(ctx, addr, addr, 2);
9314     gen_qemu_ld16u(ctx, t0, addr);
9315     tcg_gen_or_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rD(ctx->opcode)], t0);
9316     tcg_temp_free(t0);
9317 }
9318
9319 static inline void gen_op_evlhhesplat(DisasContext *ctx, TCGv addr)
9320 {
9321     TCGv t0 = tcg_temp_new();
9322     gen_qemu_ld16u(ctx, t0, addr);
9323     tcg_gen_shli_tl(t0, t0, 16);
9324     tcg_gen_mov_tl(cpu_gprh[rD(ctx->opcode)], t0);
9325     tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], t0);
9326     tcg_temp_free(t0);
9327 }
9328
9329 static inline void gen_op_evlhhousplat(DisasContext *ctx, TCGv addr)
9330 {
9331     TCGv t0 = tcg_temp_new();
9332     gen_qemu_ld16u(ctx, t0, addr);
9333     tcg_gen_mov_tl(cpu_gprh[rD(ctx->opcode)], t0);
9334     tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], t0);
9335     tcg_temp_free(t0);
9336 }
9337
9338 static inline void gen_op_evlhhossplat(DisasContext *ctx, TCGv addr)
9339 {
9340     TCGv t0 = tcg_temp_new();
9341     gen_qemu_ld16s(ctx, t0, addr);
9342     tcg_gen_mov_tl(cpu_gprh[rD(ctx->opcode)], t0);
9343     tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], t0);
9344     tcg_temp_free(t0);
9345 }
9346
9347 static inline void gen_op_evlwhe(DisasContext *ctx, TCGv addr)
9348 {
9349     TCGv t0 = tcg_temp_new();
9350     gen_qemu_ld16u(ctx, t0, addr);
9351     tcg_gen_shli_tl(cpu_gprh[rD(ctx->opcode)], t0, 16);
9352     gen_addr_add(ctx, addr, addr, 2);
9353     gen_qemu_ld16u(ctx, t0, addr);
9354     tcg_gen_shli_tl(cpu_gpr[rD(ctx->opcode)], t0, 16);
9355     tcg_temp_free(t0);
9356 }
9357
9358 static inline void gen_op_evlwhou(DisasContext *ctx, TCGv addr)
9359 {
9360     gen_qemu_ld16u(ctx, cpu_gprh[rD(ctx->opcode)], addr);
9361     gen_addr_add(ctx, addr, addr, 2);
9362     gen_qemu_ld16u(ctx, cpu_gpr[rD(ctx->opcode)], addr);
9363 }
9364
9365 static inline void gen_op_evlwhos(DisasContext *ctx, TCGv addr)
9366 {
9367     gen_qemu_ld16s(ctx, cpu_gprh[rD(ctx->opcode)], addr);
9368     gen_addr_add(ctx, addr, addr, 2);
9369     gen_qemu_ld16s(ctx, cpu_gpr[rD(ctx->opcode)], addr);
9370 }
9371
9372 static inline void gen_op_evlwwsplat(DisasContext *ctx, TCGv addr)
9373 {
9374     TCGv t0 = tcg_temp_new();
9375     gen_qemu_ld32u(ctx, t0, addr);
9376     tcg_gen_mov_tl(cpu_gprh[rD(ctx->opcode)], t0);
9377     tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], t0);
9378     tcg_temp_free(t0);
9379 }
9380
9381 static inline void gen_op_evlwhsplat(DisasContext *ctx, TCGv addr)
9382 {
9383     TCGv t0 = tcg_temp_new();
9384     gen_qemu_ld16u(ctx, t0, addr);
9385     tcg_gen_shli_tl(cpu_gprh[rD(ctx->opcode)], t0, 16);
9386     tcg_gen_or_tl(cpu_gprh[rD(ctx->opcode)], cpu_gprh[rD(ctx->opcode)], t0);
9387     gen_addr_add(ctx, addr, addr, 2);
9388     gen_qemu_ld16u(ctx, t0, addr);
9389     tcg_gen_shli_tl(cpu_gpr[rD(ctx->opcode)], t0, 16);
9390     tcg_gen_or_tl(cpu_gpr[rD(ctx->opcode)], cpu_gprh[rD(ctx->opcode)], t0);
9391     tcg_temp_free(t0);
9392 }
9393
9394 static inline void gen_op_evstdd(DisasContext *ctx, TCGv addr)
9395 {
9396     TCGv_i64 t0 = tcg_temp_new_i64();
9397     gen_load_gpr64(t0, rS(ctx->opcode));
9398     gen_qemu_st64(ctx, t0, addr);
9399     tcg_temp_free_i64(t0);
9400 }
9401
9402 static inline void gen_op_evstdw(DisasContext *ctx, TCGv addr)
9403 {
9404     gen_qemu_st32(ctx, cpu_gprh[rS(ctx->opcode)], addr);
9405     gen_addr_add(ctx, addr, addr, 4);
9406     gen_qemu_st32(ctx, cpu_gpr[rS(ctx->opcode)], addr);
9407 }
9408
9409 static inline void gen_op_evstdh(DisasContext *ctx, TCGv addr)
9410 {
9411     TCGv t0 = tcg_temp_new();
9412     tcg_gen_shri_tl(t0, cpu_gprh[rS(ctx->opcode)], 16);
9413     gen_qemu_st16(ctx, t0, addr);
9414     gen_addr_add(ctx, addr, addr, 2);
9415     gen_qemu_st16(ctx, cpu_gprh[rS(ctx->opcode)], addr);
9416     gen_addr_add(ctx, addr, addr, 2);
9417     tcg_gen_shri_tl(t0, cpu_gpr[rS(ctx->opcode)], 16);
9418     gen_qemu_st16(ctx, t0, addr);
9419     tcg_temp_free(t0);
9420     gen_addr_add(ctx, addr, addr, 2);
9421     gen_qemu_st16(ctx, cpu_gpr[rS(ctx->opcode)], addr);
9422 }
9423
9424 static inline void gen_op_evstwhe(DisasContext *ctx, TCGv addr)
9425 {
9426     TCGv t0 = tcg_temp_new();
9427     tcg_gen_shri_tl(t0, cpu_gprh[rS(ctx->opcode)], 16);
9428     gen_qemu_st16(ctx, t0, addr);
9429     gen_addr_add(ctx, addr, addr, 2);
9430     tcg_gen_shri_tl(t0, cpu_gpr[rS(ctx->opcode)], 16);
9431     gen_qemu_st16(ctx, t0, addr);
9432     tcg_temp_free(t0);
9433 }
9434
9435 static inline void gen_op_evstwho(DisasContext *ctx, TCGv addr)
9436 {
9437     gen_qemu_st16(ctx, cpu_gprh[rS(ctx->opcode)], addr);
9438     gen_addr_add(ctx, addr, addr, 2);
9439     gen_qemu_st16(ctx, cpu_gpr[rS(ctx->opcode)], addr);
9440 }
9441
9442 static inline void gen_op_evstwwe(DisasContext *ctx, TCGv addr)
9443 {
9444     gen_qemu_st32(ctx, cpu_gprh[rS(ctx->opcode)], addr);
9445 }
9446
9447 static inline void gen_op_evstwwo(DisasContext *ctx, TCGv addr)
9448 {
9449     gen_qemu_st32(ctx, cpu_gpr[rS(ctx->opcode)], addr);
9450 }
9451
9452 #define GEN_SPEOP_LDST(name, opc2, sh)                                        \
9453 static void glue(gen_, name)(DisasContext *ctx)                                       \
9454 {                                                                             \
9455     TCGv t0;                                                                  \
9456     if (unlikely(!ctx->spe_enabled)) {                                        \
9457         gen_exception(ctx, POWERPC_EXCP_SPEU);                                \
9458         return;                                                               \
9459     }                                                                         \
9460     gen_set_access_type(ctx, ACCESS_INT);                                     \
9461     t0 = tcg_temp_new();                                                      \
9462     if (Rc(ctx->opcode)) {                                                    \
9463         gen_addr_spe_imm_index(ctx, t0, sh);                                  \
9464     } else {                                                                  \
9465         gen_addr_reg_index(ctx, t0);                                          \
9466     }                                                                         \
9467     gen_op_##name(ctx, t0);                                                   \
9468     tcg_temp_free(t0);                                                        \
9469 }
9470
9471 GEN_SPEOP_LDST(evldd, 0x00, 3);
9472 GEN_SPEOP_LDST(evldw, 0x01, 3);
9473 GEN_SPEOP_LDST(evldh, 0x02, 3);
9474 GEN_SPEOP_LDST(evlhhesplat, 0x04, 1);
9475 GEN_SPEOP_LDST(evlhhousplat, 0x06, 1);
9476 GEN_SPEOP_LDST(evlhhossplat, 0x07, 1);
9477 GEN_SPEOP_LDST(evlwhe, 0x08, 2);
9478 GEN_SPEOP_LDST(evlwhou, 0x0A, 2);
9479 GEN_SPEOP_LDST(evlwhos, 0x0B, 2);
9480 GEN_SPEOP_LDST(evlwwsplat, 0x0C, 2);
9481 GEN_SPEOP_LDST(evlwhsplat, 0x0E, 2);
9482
9483 GEN_SPEOP_LDST(evstdd, 0x10, 3);
9484 GEN_SPEOP_LDST(evstdw, 0x11, 3);
9485 GEN_SPEOP_LDST(evstdh, 0x12, 3);
9486 GEN_SPEOP_LDST(evstwhe, 0x18, 2);
9487 GEN_SPEOP_LDST(evstwho, 0x1A, 2);
9488 GEN_SPEOP_LDST(evstwwe, 0x1C, 2);
9489 GEN_SPEOP_LDST(evstwwo, 0x1E, 2);
9490
9491 /* Multiply and add - TODO */
9492 #if 0
9493 GEN_SPE(speundef,       evmhessf,      0x01, 0x10, 0xFFFFFFFF, 0x00000000, PPC_SPE);//
9494 GEN_SPE(speundef,       evmhossf,      0x03, 0x10, 0xFFFFFFFF, 0x00000000, PPC_SPE);
9495 GEN_SPE(evmheumi,       evmhesmi,      0x04, 0x10, 0x00000000, 0x00000000, PPC_SPE);
9496 GEN_SPE(speundef,       evmhesmf,      0x05, 0x10, 0xFFFFFFFF, 0x00000000, PPC_SPE);
9497 GEN_SPE(evmhoumi,       evmhosmi,      0x06, 0x10, 0x00000000, 0x00000000, PPC_SPE);
9498 GEN_SPE(speundef,       evmhosmf,      0x07, 0x10, 0xFFFFFFFF, 0x00000000, PPC_SPE);
9499 GEN_SPE(speundef,       evmhessfa,     0x11, 0x10, 0xFFFFFFFF, 0x00000000, PPC_SPE);
9500 GEN_SPE(speundef,       evmhossfa,     0x13, 0x10, 0xFFFFFFFF, 0x00000000, PPC_SPE);
9501 GEN_SPE(evmheumia,      evmhesmia,     0x14, 0x10, 0x00000000, 0x00000000, PPC_SPE);
9502 GEN_SPE(speundef,       evmhesmfa,     0x15, 0x10, 0xFFFFFFFF, 0x00000000, PPC_SPE);
9503 GEN_SPE(evmhoumia,      evmhosmia,     0x16, 0x10, 0x00000000, 0x00000000, PPC_SPE);
9504 GEN_SPE(speundef,       evmhosmfa,     0x17, 0x10, 0xFFFFFFFF, 0x00000000, PPC_SPE);
9505
9506 GEN_SPE(speundef,       evmwhssf,      0x03, 0x11, 0xFFFFFFFF, 0x00000000, PPC_SPE);
9507 GEN_SPE(evmwlumi,       speundef,      0x04, 0x11, 0x00000000, 0xFFFFFFFF, PPC_SPE);
9508 GEN_SPE(evmwhumi,       evmwhsmi,      0x06, 0x11, 0x00000000, 0x00000000, PPC_SPE);
9509 GEN_SPE(speundef,       evmwhsmf,      0x07, 0x11, 0xFFFFFFFF, 0x00000000, PPC_SPE);
9510 GEN_SPE(speundef,       evmwssf,       0x09, 0x11, 0xFFFFFFFF, 0x00000000, PPC_SPE);
9511 GEN_SPE(speundef,       evmwsmf,       0x0D, 0x11, 0xFFFFFFFF, 0x00000000, PPC_SPE);
9512 GEN_SPE(speundef,       evmwhssfa,     0x13, 0x11, 0xFFFFFFFF, 0x00000000, PPC_SPE);
9513 GEN_SPE(evmwlumia,      speundef,      0x14, 0x11, 0x00000000, 0xFFFFFFFF, PPC_SPE);
9514 GEN_SPE(evmwhumia,      evmwhsmia,     0x16, 0x11, 0x00000000, 0x00000000, PPC_SPE);
9515 GEN_SPE(speundef,       evmwhsmfa,     0x17, 0x11, 0xFFFFFFFF, 0x00000000, PPC_SPE);
9516 GEN_SPE(speundef,       evmwssfa,      0x19, 0x11, 0xFFFFFFFF, 0x00000000, PPC_SPE);
9517 GEN_SPE(speundef,       evmwsmfa,      0x1D, 0x11, 0xFFFFFFFF, 0x00000000, PPC_SPE);
9518
9519 GEN_SPE(evadduiaaw,     evaddsiaaw,    0x00, 0x13, 0x0000F800, 0x0000F800, PPC_SPE);
9520 GEN_SPE(evsubfusiaaw,   evsubfssiaaw,  0x01, 0x13, 0x0000F800, 0x0000F800, PPC_SPE);
9521 GEN_SPE(evaddumiaaw,    evaddsmiaaw,   0x04, 0x13, 0x0000F800, 0x0000F800, PPC_SPE);
9522 GEN_SPE(evsubfumiaaw,   evsubfsmiaaw,  0x05, 0x13, 0x0000F800, 0x0000F800, PPC_SPE);
9523 GEN_SPE(evdivws,        evdivwu,       0x06, 0x13, 0x00000000, 0x00000000, PPC_SPE);
9524
9525 GEN_SPE(evmheusiaaw,    evmhessiaaw,   0x00, 0x14, 0x00000000, 0x00000000, PPC_SPE);
9526 GEN_SPE(speundef,       evmhessfaaw,   0x01, 0x14, 0xFFFFFFFF, 0x00000000, PPC_SPE);
9527 GEN_SPE(evmhousiaaw,    evmhossiaaw,   0x02, 0x14, 0x00000000, 0x00000000, PPC_SPE);
9528 GEN_SPE(speundef,       evmhossfaaw,   0x03, 0x14, 0xFFFFFFFF, 0x00000000, PPC_SPE);
9529 GEN_SPE(evmheumiaaw,    evmhesmiaaw,   0x04, 0x14, 0x00000000, 0x00000000, PPC_SPE);
9530 GEN_SPE(speundef,       evmhesmfaaw,   0x05, 0x14, 0xFFFFFFFF, 0x00000000, PPC_SPE);
9531 GEN_SPE(evmhoumiaaw,    evmhosmiaaw,   0x06, 0x14, 0x00000000, 0x00000000, PPC_SPE);
9532 GEN_SPE(speundef,       evmhosmfaaw,   0x07, 0x14, 0xFFFFFFFF, 0x00000000, PPC_SPE);
9533 GEN_SPE(evmhegumiaa,    evmhegsmiaa,   0x14, 0x14, 0x00000000, 0x00000000, PPC_SPE);
9534 GEN_SPE(speundef,       evmhegsmfaa,   0x15, 0x14, 0xFFFFFFFF, 0x00000000, PPC_SPE);
9535 GEN_SPE(evmhogumiaa,    evmhogsmiaa,   0x16, 0x14, 0x00000000, 0x00000000, PPC_SPE);
9536 GEN_SPE(speundef,       evmhogsmfaa,   0x17, 0x14, 0xFFFFFFFF, 0x00000000, PPC_SPE);
9537
9538 GEN_SPE(evmwlusiaaw,    evmwlssiaaw,   0x00, 0x15, 0x00000000, 0x00000000, PPC_SPE);
9539 GEN_SPE(evmwlumiaaw,    evmwlsmiaaw,   0x04, 0x15, 0x00000000, 0x00000000, PPC_SPE);
9540 GEN_SPE(speundef,       evmwssfaa,     0x09, 0x15, 0xFFFFFFFF, 0x00000000, PPC_SPE);
9541 GEN_SPE(speundef,       evmwsmfaa,     0x0D, 0x15, 0xFFFFFFFF, 0x00000000, PPC_SPE);
9542
9543 GEN_SPE(evmheusianw,    evmhessianw,   0x00, 0x16, 0x00000000, 0x00000000, PPC_SPE);
9544 GEN_SPE(speundef,       evmhessfanw,   0x01, 0x16, 0xFFFFFFFF, 0x00000000, PPC_SPE);
9545 GEN_SPE(evmhousianw,    evmhossianw,   0x02, 0x16, 0x00000000, 0x00000000, PPC_SPE);
9546 GEN_SPE(speundef,       evmhossfanw,   0x03, 0x16, 0xFFFFFFFF, 0x00000000, PPC_SPE);
9547 GEN_SPE(evmheumianw,    evmhesmianw,   0x04, 0x16, 0x00000000, 0x00000000, PPC_SPE);
9548 GEN_SPE(speundef,       evmhesmfanw,   0x05, 0x16, 0xFFFFFFFF, 0x00000000, PPC_SPE);
9549 GEN_SPE(evmhoumianw,    evmhosmianw,   0x06, 0x16, 0x00000000, 0x00000000, PPC_SPE);
9550 GEN_SPE(speundef,       evmhosmfanw,   0x07, 0x16, 0xFFFFFFFF, 0x00000000, PPC_SPE);
9551 GEN_SPE(evmhegumian,    evmhegsmian,   0x14, 0x16, 0x00000000, 0x00000000, PPC_SPE);
9552 GEN_SPE(speundef,       evmhegsmfan,   0x15, 0x16, 0xFFFFFFFF, 0x00000000, PPC_SPE);
9553 GEN_SPE(evmhigumian,    evmhigsmian,   0x16, 0x16, 0x00000000, 0x00000000, PPC_SPE);
9554 GEN_SPE(speundef,       evmhogsmfan,   0x17, 0x16, 0xFFFFFFFF, 0x00000000, PPC_SPE);
9555
9556 GEN_SPE(evmwlusianw,    evmwlssianw,   0x00, 0x17, 0x00000000, 0x00000000, PPC_SPE);
9557 GEN_SPE(evmwlumianw,    evmwlsmianw,   0x04, 0x17, 0x00000000, 0x00000000, PPC_SPE);
9558 GEN_SPE(speundef,       evmwssfan,     0x09, 0x17, 0xFFFFFFFF, 0x00000000, PPC_SPE);
9559 GEN_SPE(evmwumian,      evmwsmian,     0x0C, 0x17, 0x00000000, 0x00000000, PPC_SPE);
9560 GEN_SPE(speundef,       evmwsmfan,     0x0D, 0x17, 0xFFFFFFFF, 0x00000000, PPC_SPE);
9561 #endif
9562
9563 /***                      SPE floating-point extension                     ***/
9564 #define GEN_SPEFPUOP_CONV_32_32(name)                                         \
9565 static inline void gen_##name(DisasContext *ctx)                              \
9566 {                                                                             \
9567     TCGv_i32 t0 = tcg_temp_new_i32();                                         \
9568     tcg_gen_trunc_tl_i32(t0, cpu_gpr[rB(ctx->opcode)]);                       \
9569     gen_helper_##name(t0, cpu_env, t0);                                       \
9570     tcg_gen_extu_i32_tl(cpu_gpr[rD(ctx->opcode)], t0);                        \
9571     tcg_temp_free_i32(t0);                                                    \
9572 }
9573 #define GEN_SPEFPUOP_CONV_32_64(name)                                         \
9574 static inline void gen_##name(DisasContext *ctx)                              \
9575 {                                                                             \
9576     TCGv_i64 t0 = tcg_temp_new_i64();                                         \
9577     TCGv_i32 t1 = tcg_temp_new_i32();                                         \
9578     gen_load_gpr64(t0, rB(ctx->opcode));                                      \
9579     gen_helper_##name(t1, cpu_env, t0);                                       \
9580     tcg_gen_extu_i32_tl(cpu_gpr[rD(ctx->opcode)], t1);                        \
9581     tcg_temp_free_i64(t0);                                                    \
9582     tcg_temp_free_i32(t1);                                                    \
9583 }
9584 #define GEN_SPEFPUOP_CONV_64_32(name)                                         \
9585 static inline void gen_##name(DisasContext *ctx)                              \
9586 {                                                                             \
9587     TCGv_i64 t0 = tcg_temp_new_i64();                                         \
9588     TCGv_i32 t1 = tcg_temp_new_i32();                                         \
9589     tcg_gen_trunc_tl_i32(t1, cpu_gpr[rB(ctx->opcode)]);                       \
9590     gen_helper_##name(t0, cpu_env, t1);                                       \
9591     gen_store_gpr64(rD(ctx->opcode), t0);                                     \
9592     tcg_temp_free_i64(t0);                                                    \
9593     tcg_temp_free_i32(t1);                                                    \
9594 }
9595 #define GEN_SPEFPUOP_CONV_64_64(name)                                         \
9596 static inline void gen_##name(DisasContext *ctx)                              \
9597 {                                                                             \
9598     TCGv_i64 t0 = tcg_temp_new_i64();                                         \
9599     gen_load_gpr64(t0, rB(ctx->opcode));                                      \
9600     gen_helper_##name(t0, cpu_env, t0);                                       \
9601     gen_store_gpr64(rD(ctx->opcode), t0);                                     \
9602     tcg_temp_free_i64(t0);                                                    \
9603 }
9604 #define GEN_SPEFPUOP_ARITH2_32_32(name)                                       \
9605 static inline void gen_##name(DisasContext *ctx)                              \
9606 {                                                                             \
9607     TCGv_i32 t0, t1;                                                          \
9608     if (unlikely(!ctx->spe_enabled)) {                                        \
9609         gen_exception(ctx, POWERPC_EXCP_SPEU);                                \
9610         return;                                                               \
9611     }                                                                         \
9612     t0 = tcg_temp_new_i32();                                                  \
9613     t1 = tcg_temp_new_i32();                                                  \
9614     tcg_gen_trunc_tl_i32(t0, cpu_gpr[rA(ctx->opcode)]);                       \
9615     tcg_gen_trunc_tl_i32(t1, cpu_gpr[rB(ctx->opcode)]);                       \
9616     gen_helper_##name(t0, cpu_env, t0, t1);                                   \
9617     tcg_gen_extu_i32_tl(cpu_gpr[rD(ctx->opcode)], t0);                        \
9618                                                                               \
9619     tcg_temp_free_i32(t0);                                                    \
9620     tcg_temp_free_i32(t1);                                                    \
9621 }
9622 #define GEN_SPEFPUOP_ARITH2_64_64(name)                                       \
9623 static inline void gen_##name(DisasContext *ctx)                              \
9624 {                                                                             \
9625     TCGv_i64 t0, t1;                                                          \
9626     if (unlikely(!ctx->spe_enabled)) {                                        \
9627         gen_exception(ctx, POWERPC_EXCP_SPEU);                                \
9628         return;                                                               \
9629     }                                                                         \
9630     t0 = tcg_temp_new_i64();                                                  \
9631     t1 = tcg_temp_new_i64();                                                  \
9632     gen_load_gpr64(t0, rA(ctx->opcode));                                      \
9633     gen_load_gpr64(t1, rB(ctx->opcode));                                      \
9634     gen_helper_##name(t0, cpu_env, t0, t1);                                   \
9635     gen_store_gpr64(rD(ctx->opcode), t0);                                     \
9636     tcg_temp_free_i64(t0);                                                    \
9637     tcg_temp_free_i64(t1);                                                    \
9638 }
9639 #define GEN_SPEFPUOP_COMP_32(name)                                            \
9640 static inline void gen_##name(DisasContext *ctx)                              \
9641 {                                                                             \
9642     TCGv_i32 t0, t1;                                                          \
9643     if (unlikely(!ctx->spe_enabled)) {                                        \
9644         gen_exception(ctx, POWERPC_EXCP_SPEU);                                \
9645         return;                                                               \
9646     }                                                                         \
9647     t0 = tcg_temp_new_i32();                                                  \
9648     t1 = tcg_temp_new_i32();                                                  \
9649                                                                               \
9650     tcg_gen_trunc_tl_i32(t0, cpu_gpr[rA(ctx->opcode)]);                       \
9651     tcg_gen_trunc_tl_i32(t1, cpu_gpr[rB(ctx->opcode)]);                       \
9652     gen_helper_##name(cpu_crf[crfD(ctx->opcode)], cpu_env, t0, t1);           \
9653                                                                               \
9654     tcg_temp_free_i32(t0);                                                    \
9655     tcg_temp_free_i32(t1);                                                    \
9656 }
9657 #define GEN_SPEFPUOP_COMP_64(name)                                            \
9658 static inline void gen_##name(DisasContext *ctx)                              \
9659 {                                                                             \
9660     TCGv_i64 t0, t1;                                                          \
9661     if (unlikely(!ctx->spe_enabled)) {                                        \
9662         gen_exception(ctx, POWERPC_EXCP_SPEU);                                \
9663         return;                                                               \
9664     }                                                                         \
9665     t0 = tcg_temp_new_i64();                                                  \
9666     t1 = tcg_temp_new_i64();                                                  \
9667     gen_load_gpr64(t0, rA(ctx->opcode));                                      \
9668     gen_load_gpr64(t1, rB(ctx->opcode));                                      \
9669     gen_helper_##name(cpu_crf[crfD(ctx->opcode)], cpu_env, t0, t1);           \
9670     tcg_temp_free_i64(t0);                                                    \
9671     tcg_temp_free_i64(t1);                                                    \
9672 }
9673
9674 /* Single precision floating-point vectors operations */
9675 /* Arithmetic */
9676 GEN_SPEFPUOP_ARITH2_64_64(evfsadd);
9677 GEN_SPEFPUOP_ARITH2_64_64(evfssub);
9678 GEN_SPEFPUOP_ARITH2_64_64(evfsmul);
9679 GEN_SPEFPUOP_ARITH2_64_64(evfsdiv);
9680 static inline void gen_evfsabs(DisasContext *ctx)
9681 {
9682     if (unlikely(!ctx->spe_enabled)) {
9683         gen_exception(ctx, POWERPC_EXCP_SPEU);
9684         return;
9685     }
9686     tcg_gen_andi_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)],
9687                     ~0x80000000);
9688     tcg_gen_andi_tl(cpu_gprh[rD(ctx->opcode)], cpu_gprh[rA(ctx->opcode)],
9689                     ~0x80000000);
9690 }
9691 static inline void gen_evfsnabs(DisasContext *ctx)
9692 {
9693     if (unlikely(!ctx->spe_enabled)) {
9694         gen_exception(ctx, POWERPC_EXCP_SPEU);
9695         return;
9696     }
9697     tcg_gen_ori_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)],
9698                    0x80000000);
9699     tcg_gen_ori_tl(cpu_gprh[rD(ctx->opcode)], cpu_gprh[rA(ctx->opcode)],
9700                    0x80000000);
9701 }
9702 static inline void gen_evfsneg(DisasContext *ctx)
9703 {
9704     if (unlikely(!ctx->spe_enabled)) {
9705         gen_exception(ctx, POWERPC_EXCP_SPEU);
9706         return;
9707     }
9708     tcg_gen_xori_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)],
9709                     0x80000000);
9710     tcg_gen_xori_tl(cpu_gprh[rD(ctx->opcode)], cpu_gprh[rA(ctx->opcode)],
9711                     0x80000000);
9712 }
9713
9714 /* Conversion */
9715 GEN_SPEFPUOP_CONV_64_64(evfscfui);
9716 GEN_SPEFPUOP_CONV_64_64(evfscfsi);
9717 GEN_SPEFPUOP_CONV_64_64(evfscfuf);
9718 GEN_SPEFPUOP_CONV_64_64(evfscfsf);
9719 GEN_SPEFPUOP_CONV_64_64(evfsctui);
9720 GEN_SPEFPUOP_CONV_64_64(evfsctsi);
9721 GEN_SPEFPUOP_CONV_64_64(evfsctuf);
9722 GEN_SPEFPUOP_CONV_64_64(evfsctsf);
9723 GEN_SPEFPUOP_CONV_64_64(evfsctuiz);
9724 GEN_SPEFPUOP_CONV_64_64(evfsctsiz);
9725
9726 /* Comparison */
9727 GEN_SPEFPUOP_COMP_64(evfscmpgt);
9728 GEN_SPEFPUOP_COMP_64(evfscmplt);
9729 GEN_SPEFPUOP_COMP_64(evfscmpeq);
9730 GEN_SPEFPUOP_COMP_64(evfststgt);
9731 GEN_SPEFPUOP_COMP_64(evfststlt);
9732 GEN_SPEFPUOP_COMP_64(evfststeq);
9733
9734 /* Opcodes definitions */
9735 GEN_SPE(evfsadd,   evfssub,   0x00, 0x0A, 0x00000000, 0x00000000, PPC_SPE_SINGLE); //
9736 GEN_SPE(evfsabs,   evfsnabs,  0x02, 0x0A, 0x0000F800, 0x0000F800, PPC_SPE_SINGLE); //
9737 GEN_SPE(evfsneg,   speundef,  0x03, 0x0A, 0x0000F800, 0xFFFFFFFF, PPC_SPE_SINGLE); //
9738 GEN_SPE(evfsmul,   evfsdiv,   0x04, 0x0A, 0x00000000, 0x00000000, PPC_SPE_SINGLE); //
9739 GEN_SPE(evfscmpgt, evfscmplt, 0x06, 0x0A, 0x00600000, 0x00600000, PPC_SPE_SINGLE); //
9740 GEN_SPE(evfscmpeq, speundef,  0x07, 0x0A, 0x00600000, 0xFFFFFFFF, PPC_SPE_SINGLE); //
9741 GEN_SPE(evfscfui,  evfscfsi,  0x08, 0x0A, 0x00180000, 0x00180000, PPC_SPE_SINGLE); //
9742 GEN_SPE(evfscfuf,  evfscfsf,  0x09, 0x0A, 0x00180000, 0x00180000, PPC_SPE_SINGLE); //
9743 GEN_SPE(evfsctui,  evfsctsi,  0x0A, 0x0A, 0x00180000, 0x00180000, PPC_SPE_SINGLE); //
9744 GEN_SPE(evfsctuf,  evfsctsf,  0x0B, 0x0A, 0x00180000, 0x00180000, PPC_SPE_SINGLE); //
9745 GEN_SPE(evfsctuiz, speundef,  0x0C, 0x0A, 0x00180000, 0xFFFFFFFF, PPC_SPE_SINGLE); //
9746 GEN_SPE(evfsctsiz, speundef,  0x0D, 0x0A, 0x00180000, 0xFFFFFFFF, PPC_SPE_SINGLE); //
9747 GEN_SPE(evfststgt, evfststlt, 0x0E, 0x0A, 0x00600000, 0x00600000, PPC_SPE_SINGLE); //
9748 GEN_SPE(evfststeq, speundef,  0x0F, 0x0A, 0x00600000, 0xFFFFFFFF, PPC_SPE_SINGLE); //
9749
9750 /* Single precision floating-point operations */
9751 /* Arithmetic */
9752 GEN_SPEFPUOP_ARITH2_32_32(efsadd);
9753 GEN_SPEFPUOP_ARITH2_32_32(efssub);
9754 GEN_SPEFPUOP_ARITH2_32_32(efsmul);
9755 GEN_SPEFPUOP_ARITH2_32_32(efsdiv);
9756 static inline void gen_efsabs(DisasContext *ctx)
9757 {
9758     if (unlikely(!ctx->spe_enabled)) {
9759         gen_exception(ctx, POWERPC_EXCP_SPEU);
9760         return;
9761     }
9762     tcg_gen_andi_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)], (target_long)~0x80000000LL);
9763 }
9764 static inline void gen_efsnabs(DisasContext *ctx)
9765 {
9766     if (unlikely(!ctx->spe_enabled)) {
9767         gen_exception(ctx, POWERPC_EXCP_SPEU);
9768         return;
9769     }
9770     tcg_gen_ori_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)], 0x80000000);
9771 }
9772 static inline void gen_efsneg(DisasContext *ctx)
9773 {
9774     if (unlikely(!ctx->spe_enabled)) {
9775         gen_exception(ctx, POWERPC_EXCP_SPEU);
9776         return;
9777     }
9778     tcg_gen_xori_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)], 0x80000000);
9779 }
9780
9781 /* Conversion */
9782 GEN_SPEFPUOP_CONV_32_32(efscfui);
9783 GEN_SPEFPUOP_CONV_32_32(efscfsi);
9784 GEN_SPEFPUOP_CONV_32_32(efscfuf);
9785 GEN_SPEFPUOP_CONV_32_32(efscfsf);
9786 GEN_SPEFPUOP_CONV_32_32(efsctui);
9787 GEN_SPEFPUOP_CONV_32_32(efsctsi);
9788 GEN_SPEFPUOP_CONV_32_32(efsctuf);
9789 GEN_SPEFPUOP_CONV_32_32(efsctsf);
9790 GEN_SPEFPUOP_CONV_32_32(efsctuiz);
9791 GEN_SPEFPUOP_CONV_32_32(efsctsiz);
9792 GEN_SPEFPUOP_CONV_32_64(efscfd);
9793
9794 /* Comparison */
9795 GEN_SPEFPUOP_COMP_32(efscmpgt);
9796 GEN_SPEFPUOP_COMP_32(efscmplt);
9797 GEN_SPEFPUOP_COMP_32(efscmpeq);
9798 GEN_SPEFPUOP_COMP_32(efststgt);
9799 GEN_SPEFPUOP_COMP_32(efststlt);
9800 GEN_SPEFPUOP_COMP_32(efststeq);
9801
9802 /* Opcodes definitions */
9803 GEN_SPE(efsadd,   efssub,   0x00, 0x0B, 0x00000000, 0x00000000, PPC_SPE_SINGLE); //
9804 GEN_SPE(efsabs,   efsnabs,  0x02, 0x0B, 0x0000F800, 0x0000F800, PPC_SPE_SINGLE); //
9805 GEN_SPE(efsneg,   speundef, 0x03, 0x0B, 0x0000F800, 0xFFFFFFFF, PPC_SPE_SINGLE); //
9806 GEN_SPE(efsmul,   efsdiv,   0x04, 0x0B, 0x00000000, 0x00000000, PPC_SPE_SINGLE); //
9807 GEN_SPE(efscmpgt, efscmplt, 0x06, 0x0B, 0x00600000, 0x00600000, PPC_SPE_SINGLE); //
9808 GEN_SPE(efscmpeq, efscfd,   0x07, 0x0B, 0x00600000, 0x00180000, PPC_SPE_SINGLE); //
9809 GEN_SPE(efscfui,  efscfsi,  0x08, 0x0B, 0x00180000, 0x00180000, PPC_SPE_SINGLE); //
9810 GEN_SPE(efscfuf,  efscfsf,  0x09, 0x0B, 0x00180000, 0x00180000, PPC_SPE_SINGLE); //
9811 GEN_SPE(efsctui,  efsctsi,  0x0A, 0x0B, 0x00180000, 0x00180000, PPC_SPE_SINGLE); //
9812 GEN_SPE(efsctuf,  efsctsf,  0x0B, 0x0B, 0x00180000, 0x00180000, PPC_SPE_SINGLE); //
9813 GEN_SPE(efsctuiz, speundef, 0x0C, 0x0B, 0x00180000, 0xFFFFFFFF, PPC_SPE_SINGLE); //
9814 GEN_SPE(efsctsiz, speundef, 0x0D, 0x0B, 0x00180000, 0xFFFFFFFF, PPC_SPE_SINGLE); //
9815 GEN_SPE(efststgt, efststlt, 0x0E, 0x0B, 0x00600000, 0x00600000, PPC_SPE_SINGLE); //
9816 GEN_SPE(efststeq, speundef, 0x0F, 0x0B, 0x00600000, 0xFFFFFFFF, PPC_SPE_SINGLE); //
9817
9818 /* Double precision floating-point operations */
9819 /* Arithmetic */
9820 GEN_SPEFPUOP_ARITH2_64_64(efdadd);
9821 GEN_SPEFPUOP_ARITH2_64_64(efdsub);
9822 GEN_SPEFPUOP_ARITH2_64_64(efdmul);
9823 GEN_SPEFPUOP_ARITH2_64_64(efddiv);
9824 static inline void gen_efdabs(DisasContext *ctx)
9825 {
9826     if (unlikely(!ctx->spe_enabled)) {
9827         gen_exception(ctx, POWERPC_EXCP_SPEU);
9828         return;
9829     }
9830     tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)]);
9831     tcg_gen_andi_tl(cpu_gprh[rD(ctx->opcode)], cpu_gprh[rA(ctx->opcode)],
9832                     ~0x80000000);
9833 }
9834 static inline void gen_efdnabs(DisasContext *ctx)
9835 {
9836     if (unlikely(!ctx->spe_enabled)) {
9837         gen_exception(ctx, POWERPC_EXCP_SPEU);
9838         return;
9839     }
9840     tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)]);
9841     tcg_gen_ori_tl(cpu_gprh[rD(ctx->opcode)], cpu_gprh[rA(ctx->opcode)],
9842                    0x80000000);
9843 }
9844 static inline void gen_efdneg(DisasContext *ctx)
9845 {
9846     if (unlikely(!ctx->spe_enabled)) {
9847         gen_exception(ctx, POWERPC_EXCP_SPEU);
9848         return;
9849     }
9850     tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)]);
9851     tcg_gen_xori_tl(cpu_gprh[rD(ctx->opcode)], cpu_gprh[rA(ctx->opcode)],
9852                     0x80000000);
9853 }
9854
9855 /* Conversion */
9856 GEN_SPEFPUOP_CONV_64_32(efdcfui);
9857 GEN_SPEFPUOP_CONV_64_32(efdcfsi);
9858 GEN_SPEFPUOP_CONV_64_32(efdcfuf);
9859 GEN_SPEFPUOP_CONV_64_32(efdcfsf);
9860 GEN_SPEFPUOP_CONV_32_64(efdctui);
9861 GEN_SPEFPUOP_CONV_32_64(efdctsi);
9862 GEN_SPEFPUOP_CONV_32_64(efdctuf);
9863 GEN_SPEFPUOP_CONV_32_64(efdctsf);
9864 GEN_SPEFPUOP_CONV_32_64(efdctuiz);
9865 GEN_SPEFPUOP_CONV_32_64(efdctsiz);
9866 GEN_SPEFPUOP_CONV_64_32(efdcfs);
9867 GEN_SPEFPUOP_CONV_64_64(efdcfuid);
9868 GEN_SPEFPUOP_CONV_64_64(efdcfsid);
9869 GEN_SPEFPUOP_CONV_64_64(efdctuidz);
9870 GEN_SPEFPUOP_CONV_64_64(efdctsidz);
9871
9872 /* Comparison */
9873 GEN_SPEFPUOP_COMP_64(efdcmpgt);
9874 GEN_SPEFPUOP_COMP_64(efdcmplt);
9875 GEN_SPEFPUOP_COMP_64(efdcmpeq);
9876 GEN_SPEFPUOP_COMP_64(efdtstgt);
9877 GEN_SPEFPUOP_COMP_64(efdtstlt);
9878 GEN_SPEFPUOP_COMP_64(efdtsteq);
9879
9880 /* Opcodes definitions */
9881 GEN_SPE(efdadd,    efdsub,    0x10, 0x0B, 0x00000000, 0x00000000, PPC_SPE_DOUBLE); //
9882 GEN_SPE(efdcfuid,  efdcfsid,  0x11, 0x0B, 0x00180000, 0x00180000, PPC_SPE_DOUBLE); //
9883 GEN_SPE(efdabs,    efdnabs,   0x12, 0x0B, 0x0000F800, 0x0000F800, PPC_SPE_DOUBLE); //
9884 GEN_SPE(efdneg,    speundef,  0x13, 0x0B, 0x0000F800, 0xFFFFFFFF, PPC_SPE_DOUBLE); //
9885 GEN_SPE(efdmul,    efddiv,    0x14, 0x0B, 0x00000000, 0x00000000, PPC_SPE_DOUBLE); //
9886 GEN_SPE(efdctuidz, efdctsidz, 0x15, 0x0B, 0x00180000, 0x00180000, PPC_SPE_DOUBLE); //
9887 GEN_SPE(efdcmpgt,  efdcmplt,  0x16, 0x0B, 0x00600000, 0x00600000, PPC_SPE_DOUBLE); //
9888 GEN_SPE(efdcmpeq,  efdcfs,    0x17, 0x0B, 0x00600000, 0x00180000, PPC_SPE_DOUBLE); //
9889 GEN_SPE(efdcfui,   efdcfsi,   0x18, 0x0B, 0x00180000, 0x00180000, PPC_SPE_DOUBLE); //
9890 GEN_SPE(efdcfuf,   efdcfsf,   0x19, 0x0B, 0x00180000, 0x00180000, PPC_SPE_DOUBLE); //
9891 GEN_SPE(efdctui,   efdctsi,   0x1A, 0x0B, 0x00180000, 0x00180000, PPC_SPE_DOUBLE); //
9892 GEN_SPE(efdctuf,   efdctsf,   0x1B, 0x0B, 0x00180000, 0x00180000, PPC_SPE_DOUBLE); //
9893 GEN_SPE(efdctuiz,  speundef,  0x1C, 0x0B, 0x00180000, 0xFFFFFFFF, PPC_SPE_DOUBLE); //
9894 GEN_SPE(efdctsiz,  speundef,  0x1D, 0x0B, 0x00180000, 0xFFFFFFFF, PPC_SPE_DOUBLE); //
9895 GEN_SPE(efdtstgt,  efdtstlt,  0x1E, 0x0B, 0x00600000, 0x00600000, PPC_SPE_DOUBLE); //
9896 GEN_SPE(efdtsteq,  speundef,  0x1F, 0x0B, 0x00600000, 0xFFFFFFFF, PPC_SPE_DOUBLE); //
9897
9898 static void gen_tbegin(DisasContext *ctx)
9899 {
9900     if (unlikely(!ctx->tm_enabled)) {
9901         gen_exception_err(ctx, POWERPC_EXCP_FU, FSCR_IC_TM);
9902         return;
9903     }
9904     gen_helper_tbegin(cpu_env);
9905 }
9906
9907 #define GEN_TM_NOOP(name)                                      \
9908 static inline void gen_##name(DisasContext *ctx)               \
9909 {                                                              \
9910     if (unlikely(!ctx->tm_enabled)) {                          \
9911         gen_exception_err(ctx, POWERPC_EXCP_FU, FSCR_IC_TM);   \
9912         return;                                                \
9913     }                                                          \
9914     /* Because tbegin always fails in QEMU, these user         \
9915      * space instructions all have a simple implementation:    \
9916      *                                                         \
9917      *     CR[0] = 0b0 || MSR[TS] || 0b0                       \
9918      *           = 0b0 || 0b00    || 0b0                       \
9919      */                                                        \
9920     tcg_gen_movi_i32(cpu_crf[0], 0);                           \
9921 }
9922
9923 GEN_TM_NOOP(tend);
9924 GEN_TM_NOOP(tabort);
9925 GEN_TM_NOOP(tabortwc);
9926 GEN_TM_NOOP(tabortwci);
9927 GEN_TM_NOOP(tabortdc);
9928 GEN_TM_NOOP(tabortdci);
9929 GEN_TM_NOOP(tsr);
9930
9931 static void gen_tcheck(DisasContext *ctx)
9932 {
9933     if (unlikely(!ctx->tm_enabled)) {
9934         gen_exception_err(ctx, POWERPC_EXCP_FU, FSCR_IC_TM);
9935         return;
9936     }
9937     /* Because tbegin always fails, the tcheck implementation
9938      * is simple:
9939      *
9940      * CR[CRF] = TDOOMED || MSR[TS] || 0b0
9941      *         = 0b1 || 0b00 || 0b0
9942      */
9943     tcg_gen_movi_i32(cpu_crf[crfD(ctx->opcode)], 0x8);
9944 }
9945
9946 #if defined(CONFIG_USER_ONLY)
9947 #define GEN_TM_PRIV_NOOP(name)                                 \
9948 static inline void gen_##name(DisasContext *ctx)               \
9949 {                                                              \
9950     gen_priv_exception(ctx, POWERPC_EXCP_PRIV_OPC);           \
9951 }
9952
9953 #else
9954
9955 #define GEN_TM_PRIV_NOOP(name)                                 \
9956 static inline void gen_##name(DisasContext *ctx)               \
9957 {                                                              \
9958     CHK_SV;                                                    \
9959     if (unlikely(!ctx->tm_enabled)) {                          \
9960         gen_exception_err(ctx, POWERPC_EXCP_FU, FSCR_IC_TM);   \
9961         return;                                                \
9962     }                                                          \
9963     /* Because tbegin always fails, the implementation is      \
9964      * simple:                                                 \
9965      *                                                         \
9966      *   CR[0] = 0b0 || MSR[TS] || 0b0                         \
9967      *         = 0b0 || 0b00 | 0b0                             \
9968      */                                                        \
9969     tcg_gen_movi_i32(cpu_crf[0], 0);                           \
9970 }
9971
9972 #endif
9973
9974 GEN_TM_PRIV_NOOP(treclaim);
9975 GEN_TM_PRIV_NOOP(trechkpt);
9976
9977 static opcode_t opcodes[] = {
9978 GEN_HANDLER(invalid, 0x00, 0x00, 0x00, 0xFFFFFFFF, PPC_NONE),
9979 GEN_HANDLER(cmp, 0x1F, 0x00, 0x00, 0x00400000, PPC_INTEGER),
9980 GEN_HANDLER(cmpi, 0x0B, 0xFF, 0xFF, 0x00400000, PPC_INTEGER),
9981 GEN_HANDLER(cmpl, 0x1F, 0x00, 0x01, 0x00400000, PPC_INTEGER),
9982 GEN_HANDLER(cmpli, 0x0A, 0xFF, 0xFF, 0x00400000, PPC_INTEGER),
9983 GEN_HANDLER_E(cmpb, 0x1F, 0x1C, 0x0F, 0x00000001, PPC_NONE, PPC2_ISA205),
9984 GEN_HANDLER_E(cmprb, 0x1F, 0x00, 0x06, 0x00400001, PPC_NONE, PPC2_ISA300),
9985 GEN_HANDLER(isel, 0x1F, 0x0F, 0xFF, 0x00000001, PPC_ISEL),
9986 GEN_HANDLER(addi, 0x0E, 0xFF, 0xFF, 0x00000000, PPC_INTEGER),
9987 GEN_HANDLER(addic, 0x0C, 0xFF, 0xFF, 0x00000000, PPC_INTEGER),
9988 GEN_HANDLER2(addic_, "addic.", 0x0D, 0xFF, 0xFF, 0x00000000, PPC_INTEGER),
9989 GEN_HANDLER(addis, 0x0F, 0xFF, 0xFF, 0x00000000, PPC_INTEGER),
9990 GEN_HANDLER_E(addpcis, 0x13, 0x2, 0xFF, 0x00000000, PPC_NONE, PPC2_ISA300),
9991 GEN_HANDLER(mulhw, 0x1F, 0x0B, 0x02, 0x00000400, PPC_INTEGER),
9992 GEN_HANDLER(mulhwu, 0x1F, 0x0B, 0x00, 0x00000400, PPC_INTEGER),
9993 GEN_HANDLER(mullw, 0x1F, 0x0B, 0x07, 0x00000000, PPC_INTEGER),
9994 GEN_HANDLER(mullwo, 0x1F, 0x0B, 0x17, 0x00000000, PPC_INTEGER),
9995 GEN_HANDLER(mulli, 0x07, 0xFF, 0xFF, 0x00000000, PPC_INTEGER),
9996 #if defined(TARGET_PPC64)
9997 GEN_HANDLER(mulld, 0x1F, 0x09, 0x07, 0x00000000, PPC_64B),
9998 #endif
9999 GEN_HANDLER(neg, 0x1F, 0x08, 0x03, 0x0000F800, PPC_INTEGER),
10000 GEN_HANDLER(nego, 0x1F, 0x08, 0x13, 0x0000F800, PPC_INTEGER),
10001 GEN_HANDLER(subfic, 0x08, 0xFF, 0xFF, 0x00000000, PPC_INTEGER),
10002 GEN_HANDLER2(andi_, "andi.", 0x1C, 0xFF, 0xFF, 0x00000000, PPC_INTEGER),
10003 GEN_HANDLER2(andis_, "andis.", 0x1D, 0xFF, 0xFF, 0x00000000, PPC_INTEGER),
10004 GEN_HANDLER(cntlzw, 0x1F, 0x1A, 0x00, 0x00000000, PPC_INTEGER),
10005 GEN_HANDLER(or, 0x1F, 0x1C, 0x0D, 0x00000000, PPC_INTEGER),
10006 GEN_HANDLER(xor, 0x1F, 0x1C, 0x09, 0x00000000, PPC_INTEGER),
10007 GEN_HANDLER(ori, 0x18, 0xFF, 0xFF, 0x00000000, PPC_INTEGER),
10008 GEN_HANDLER(oris, 0x19, 0xFF, 0xFF, 0x00000000, PPC_INTEGER),
10009 GEN_HANDLER(xori, 0x1A, 0xFF, 0xFF, 0x00000000, PPC_INTEGER),
10010 GEN_HANDLER(xoris, 0x1B, 0xFF, 0xFF, 0x00000000, PPC_INTEGER),
10011 GEN_HANDLER(popcntb, 0x1F, 0x1A, 0x03, 0x0000F801, PPC_POPCNTB),
10012 GEN_HANDLER(popcntw, 0x1F, 0x1A, 0x0b, 0x0000F801, PPC_POPCNTWD),
10013 GEN_HANDLER_E(prtyw, 0x1F, 0x1A, 0x04, 0x0000F801, PPC_NONE, PPC2_ISA205),
10014 #if defined(TARGET_PPC64)
10015 GEN_HANDLER(popcntd, 0x1F, 0x1A, 0x0F, 0x0000F801, PPC_POPCNTWD),
10016 GEN_HANDLER(cntlzd, 0x1F, 0x1A, 0x01, 0x00000000, PPC_64B),
10017 GEN_HANDLER_E(prtyd, 0x1F, 0x1A, 0x05, 0x0000F801, PPC_NONE, PPC2_ISA205),
10018 GEN_HANDLER_E(bpermd, 0x1F, 0x1C, 0x07, 0x00000001, PPC_NONE, PPC2_PERM_ISA206),
10019 #endif
10020 GEN_HANDLER(rlwimi, 0x14, 0xFF, 0xFF, 0x00000000, PPC_INTEGER),
10021 GEN_HANDLER(rlwinm, 0x15, 0xFF, 0xFF, 0x00000000, PPC_INTEGER),
10022 GEN_HANDLER(rlwnm, 0x17, 0xFF, 0xFF, 0x00000000, PPC_INTEGER),
10023 GEN_HANDLER(slw, 0x1F, 0x18, 0x00, 0x00000000, PPC_INTEGER),
10024 GEN_HANDLER(sraw, 0x1F, 0x18, 0x18, 0x00000000, PPC_INTEGER),
10025 GEN_HANDLER(srawi, 0x1F, 0x18, 0x19, 0x00000000, PPC_INTEGER),
10026 GEN_HANDLER(srw, 0x1F, 0x18, 0x10, 0x00000000, PPC_INTEGER),
10027 #if defined(TARGET_PPC64)
10028 GEN_HANDLER(sld, 0x1F, 0x1B, 0x00, 0x00000000, PPC_64B),
10029 GEN_HANDLER(srad, 0x1F, 0x1A, 0x18, 0x00000000, PPC_64B),
10030 GEN_HANDLER2(sradi0, "sradi", 0x1F, 0x1A, 0x19, 0x00000000, PPC_64B),
10031 GEN_HANDLER2(sradi1, "sradi", 0x1F, 0x1B, 0x19, 0x00000000, PPC_64B),
10032 GEN_HANDLER(srd, 0x1F, 0x1B, 0x10, 0x00000000, PPC_64B),
10033 #endif
10034 GEN_HANDLER(frsqrtes, 0x3B, 0x1A, 0xFF, 0x001F07C0, PPC_FLOAT_FRSQRTES),
10035 GEN_HANDLER(fsqrt, 0x3F, 0x16, 0xFF, 0x001F07C0, PPC_FLOAT_FSQRT),
10036 GEN_HANDLER(fsqrts, 0x3B, 0x16, 0xFF, 0x001F07C0, PPC_FLOAT_FSQRT),
10037 GEN_HANDLER(fcmpo, 0x3F, 0x00, 0x01, 0x00600001, PPC_FLOAT),
10038 GEN_HANDLER(fcmpu, 0x3F, 0x00, 0x00, 0x00600001, PPC_FLOAT),
10039 GEN_HANDLER(fabs, 0x3F, 0x08, 0x08, 0x001F0000, PPC_FLOAT),
10040 GEN_HANDLER(fmr, 0x3F, 0x08, 0x02, 0x001F0000, PPC_FLOAT),
10041 GEN_HANDLER(fnabs, 0x3F, 0x08, 0x04, 0x001F0000, PPC_FLOAT),
10042 GEN_HANDLER(fneg, 0x3F, 0x08, 0x01, 0x001F0000, PPC_FLOAT),
10043 GEN_HANDLER_E(fcpsgn, 0x3F, 0x08, 0x00, 0x00000000, PPC_NONE, PPC2_ISA205),
10044 GEN_HANDLER_E(fmrgew, 0x3F, 0x06, 0x1E, 0x00000001, PPC_NONE, PPC2_VSX207),
10045 GEN_HANDLER_E(fmrgow, 0x3F, 0x06, 0x1A, 0x00000001, PPC_NONE, PPC2_VSX207),
10046 GEN_HANDLER(mcrfs, 0x3F, 0x00, 0x02, 0x0063F801, PPC_FLOAT),
10047 GEN_HANDLER(mffs, 0x3F, 0x07, 0x12, 0x001FF800, PPC_FLOAT),
10048 GEN_HANDLER(mtfsb0, 0x3F, 0x06, 0x02, 0x001FF800, PPC_FLOAT),
10049 GEN_HANDLER(mtfsb1, 0x3F, 0x06, 0x01, 0x001FF800, PPC_FLOAT),
10050 GEN_HANDLER(mtfsf, 0x3F, 0x07, 0x16, 0x00000000, PPC_FLOAT),
10051 GEN_HANDLER(mtfsfi, 0x3F, 0x06, 0x04, 0x006e0800, PPC_FLOAT),
10052 #if defined(TARGET_PPC64)
10053 GEN_HANDLER(ld, 0x3A, 0xFF, 0xFF, 0x00000000, PPC_64B),
10054 GEN_HANDLER(lq, 0x38, 0xFF, 0xFF, 0x00000000, PPC_64BX),
10055 GEN_HANDLER(std, 0x3E, 0xFF, 0xFF, 0x00000000, PPC_64B),
10056 #endif
10057 GEN_HANDLER(lmw, 0x2E, 0xFF, 0xFF, 0x00000000, PPC_INTEGER),
10058 GEN_HANDLER(stmw, 0x2F, 0xFF, 0xFF, 0x00000000, PPC_INTEGER),
10059 GEN_HANDLER(lswi, 0x1F, 0x15, 0x12, 0x00000001, PPC_STRING),
10060 GEN_HANDLER(lswx, 0x1F, 0x15, 0x10, 0x00000001, PPC_STRING),
10061 GEN_HANDLER(stswi, 0x1F, 0x15, 0x16, 0x00000001, PPC_STRING),
10062 GEN_HANDLER(stswx, 0x1F, 0x15, 0x14, 0x00000001, PPC_STRING),
10063 GEN_HANDLER(eieio, 0x1F, 0x16, 0x1A, 0x03FFF801, PPC_MEM_EIEIO),
10064 GEN_HANDLER(isync, 0x13, 0x16, 0x04, 0x03FFF801, PPC_MEM),
10065 GEN_HANDLER_E(lbarx, 0x1F, 0x14, 0x01, 0, PPC_NONE, PPC2_ATOMIC_ISA206),
10066 GEN_HANDLER_E(lharx, 0x1F, 0x14, 0x03, 0, PPC_NONE, PPC2_ATOMIC_ISA206),
10067 GEN_HANDLER(lwarx, 0x1F, 0x14, 0x00, 0x00000000, PPC_RES),
10068 GEN_HANDLER_E(stbcx_, 0x1F, 0x16, 0x15, 0, PPC_NONE, PPC2_ATOMIC_ISA206),
10069 GEN_HANDLER_E(sthcx_, 0x1F, 0x16, 0x16, 0, PPC_NONE, PPC2_ATOMIC_ISA206),
10070 GEN_HANDLER2(stwcx_, "stwcx.", 0x1F, 0x16, 0x04, 0x00000000, PPC_RES),
10071 #if defined(TARGET_PPC64)
10072 GEN_HANDLER(ldarx, 0x1F, 0x14, 0x02, 0x00000000, PPC_64B),
10073 GEN_HANDLER_E(lqarx, 0x1F, 0x14, 0x08, 0, PPC_NONE, PPC2_LSQ_ISA207),
10074 GEN_HANDLER2(stdcx_, "stdcx.", 0x1F, 0x16, 0x06, 0x00000000, PPC_64B),
10075 GEN_HANDLER_E(stqcx_, 0x1F, 0x16, 0x05, 0, PPC_NONE, PPC2_LSQ_ISA207),
10076 #endif
10077 GEN_HANDLER(sync, 0x1F, 0x16, 0x12, 0x039FF801, PPC_MEM_SYNC),
10078 GEN_HANDLER(wait, 0x1F, 0x1E, 0x01, 0x03FFF801, PPC_WAIT),
10079 GEN_HANDLER(b, 0x12, 0xFF, 0xFF, 0x00000000, PPC_FLOW),
10080 GEN_HANDLER(bc, 0x10, 0xFF, 0xFF, 0x00000000, PPC_FLOW),
10081 GEN_HANDLER(bcctr, 0x13, 0x10, 0x10, 0x00000000, PPC_FLOW),
10082 GEN_HANDLER(bclr, 0x13, 0x10, 0x00, 0x00000000, PPC_FLOW),
10083 GEN_HANDLER_E(bctar, 0x13, 0x10, 0x11, 0, PPC_NONE, PPC2_BCTAR_ISA207),
10084 GEN_HANDLER(mcrf, 0x13, 0x00, 0xFF, 0x00000001, PPC_INTEGER),
10085 GEN_HANDLER(rfi, 0x13, 0x12, 0x01, 0x03FF8001, PPC_FLOW),
10086 #if defined(TARGET_PPC64)
10087 GEN_HANDLER(rfid, 0x13, 0x12, 0x00, 0x03FF8001, PPC_64B),
10088 GEN_HANDLER_E(doze, 0x13, 0x12, 0x0c, 0x03FFF801, PPC_NONE, PPC2_PM_ISA206),
10089 GEN_HANDLER_E(nap, 0x13, 0x12, 0x0d, 0x03FFF801, PPC_NONE, PPC2_PM_ISA206),
10090 GEN_HANDLER_E(sleep, 0x13, 0x12, 0x0e, 0x03FFF801, PPC_NONE, PPC2_PM_ISA206),
10091 GEN_HANDLER_E(rvwinkle, 0x13, 0x12, 0x0f, 0x03FFF801, PPC_NONE, PPC2_PM_ISA206),
10092 GEN_HANDLER(hrfid, 0x13, 0x12, 0x08, 0x03FF8001, PPC_64H),
10093 #endif
10094 GEN_HANDLER(sc, 0x11, 0xFF, 0xFF, 0x03FFF01D, PPC_FLOW),
10095 GEN_HANDLER(tw, 0x1F, 0x04, 0x00, 0x00000001, PPC_FLOW),
10096 GEN_HANDLER(twi, 0x03, 0xFF, 0xFF, 0x00000000, PPC_FLOW),
10097 #if defined(TARGET_PPC64)
10098 GEN_HANDLER(td, 0x1F, 0x04, 0x02, 0x00000001, PPC_64B),
10099 GEN_HANDLER(tdi, 0x02, 0xFF, 0xFF, 0x00000000, PPC_64B),
10100 #endif
10101 GEN_HANDLER(mcrxr, 0x1F, 0x00, 0x10, 0x007FF801, PPC_MISC),
10102 GEN_HANDLER(mfcr, 0x1F, 0x13, 0x00, 0x00000801, PPC_MISC),
10103 GEN_HANDLER(mfmsr, 0x1F, 0x13, 0x02, 0x001FF801, PPC_MISC),
10104 GEN_HANDLER(mfspr, 0x1F, 0x13, 0x0A, 0x00000001, PPC_MISC),
10105 GEN_HANDLER(mftb, 0x1F, 0x13, 0x0B, 0x00000001, PPC_MFTB),
10106 GEN_HANDLER(mtcrf, 0x1F, 0x10, 0x04, 0x00000801, PPC_MISC),
10107 #if defined(TARGET_PPC64)
10108 GEN_HANDLER(mtmsrd, 0x1F, 0x12, 0x05, 0x001EF801, PPC_64B),
10109 #endif
10110 GEN_HANDLER(mtmsr, 0x1F, 0x12, 0x04, 0x001EF801, PPC_MISC),
10111 GEN_HANDLER(mtspr, 0x1F, 0x13, 0x0E, 0x00000000, PPC_MISC),
10112 GEN_HANDLER(dcbf, 0x1F, 0x16, 0x02, 0x03C00001, PPC_CACHE),
10113 GEN_HANDLER(dcbi, 0x1F, 0x16, 0x0E, 0x03E00001, PPC_CACHE),
10114 GEN_HANDLER(dcbst, 0x1F, 0x16, 0x01, 0x03E00001, PPC_CACHE),
10115 GEN_HANDLER(dcbt, 0x1F, 0x16, 0x08, 0x00000001, PPC_CACHE),
10116 GEN_HANDLER(dcbtst, 0x1F, 0x16, 0x07, 0x00000001, PPC_CACHE),
10117 GEN_HANDLER_E(dcbtls, 0x1F, 0x06, 0x05, 0x02000001, PPC_BOOKE, PPC2_BOOKE206),
10118 GEN_HANDLER(dcbz, 0x1F, 0x16, 0x1F, 0x03C00001, PPC_CACHE_DCBZ),
10119 GEN_HANDLER(dst, 0x1F, 0x16, 0x0A, 0x01800001, PPC_ALTIVEC),
10120 GEN_HANDLER(dstst, 0x1F, 0x16, 0x0B, 0x02000001, PPC_ALTIVEC),
10121 GEN_HANDLER(dss, 0x1F, 0x16, 0x19, 0x019FF801, PPC_ALTIVEC),
10122 GEN_HANDLER(icbi, 0x1F, 0x16, 0x1E, 0x03E00001, PPC_CACHE_ICBI),
10123 GEN_HANDLER(dcba, 0x1F, 0x16, 0x17, 0x03E00001, PPC_CACHE_DCBA),
10124 GEN_HANDLER(mfsr, 0x1F, 0x13, 0x12, 0x0010F801, PPC_SEGMENT),
10125 GEN_HANDLER(mfsrin, 0x1F, 0x13, 0x14, 0x001F0001, PPC_SEGMENT),
10126 GEN_HANDLER(mtsr, 0x1F, 0x12, 0x06, 0x0010F801, PPC_SEGMENT),
10127 GEN_HANDLER(mtsrin, 0x1F, 0x12, 0x07, 0x001F0001, PPC_SEGMENT),
10128 #if defined(TARGET_PPC64)
10129 GEN_HANDLER2(mfsr_64b, "mfsr", 0x1F, 0x13, 0x12, 0x0010F801, PPC_SEGMENT_64B),
10130 GEN_HANDLER2(mfsrin_64b, "mfsrin", 0x1F, 0x13, 0x14, 0x001F0001,
10131              PPC_SEGMENT_64B),
10132 GEN_HANDLER2(mtsr_64b, "mtsr", 0x1F, 0x12, 0x06, 0x0010F801, PPC_SEGMENT_64B),
10133 GEN_HANDLER2(mtsrin_64b, "mtsrin", 0x1F, 0x12, 0x07, 0x001F0001,
10134              PPC_SEGMENT_64B),
10135 GEN_HANDLER2(slbmte, "slbmte", 0x1F, 0x12, 0x0C, 0x001F0001, PPC_SEGMENT_64B),
10136 GEN_HANDLER2(slbmfee, "slbmfee", 0x1F, 0x13, 0x1C, 0x001F0001, PPC_SEGMENT_64B),
10137 GEN_HANDLER2(slbmfev, "slbmfev", 0x1F, 0x13, 0x1A, 0x001F0001, PPC_SEGMENT_64B),
10138 GEN_HANDLER2(slbfee_, "slbfee.", 0x1F, 0x13, 0x1E, 0x001F0000, PPC_SEGMENT_64B),
10139 #endif
10140 GEN_HANDLER(tlbia, 0x1F, 0x12, 0x0B, 0x03FFFC01, PPC_MEM_TLBIA),
10141 /* XXX Those instructions will need to be handled differently for
10142  * different ISA versions */
10143 GEN_HANDLER(tlbiel, 0x1F, 0x12, 0x08, 0x001F0001, PPC_MEM_TLBIE),
10144 GEN_HANDLER(tlbie, 0x1F, 0x12, 0x09, 0x001F0001, PPC_MEM_TLBIE),
10145 GEN_HANDLER(tlbsync, 0x1F, 0x16, 0x11, 0x03FFF801, PPC_MEM_TLBSYNC),
10146 #if defined(TARGET_PPC64)
10147 GEN_HANDLER(slbia, 0x1F, 0x12, 0x0F, 0x031FFC01, PPC_SLBI),
10148 GEN_HANDLER(slbie, 0x1F, 0x12, 0x0D, 0x03FF0001, PPC_SLBI),
10149 #endif
10150 GEN_HANDLER(eciwx, 0x1F, 0x16, 0x0D, 0x00000001, PPC_EXTERN),
10151 GEN_HANDLER(ecowx, 0x1F, 0x16, 0x09, 0x00000001, PPC_EXTERN),
10152 GEN_HANDLER(abs, 0x1F, 0x08, 0x0B, 0x0000F800, PPC_POWER_BR),
10153 GEN_HANDLER(abso, 0x1F, 0x08, 0x1B, 0x0000F800, PPC_POWER_BR),
10154 GEN_HANDLER(clcs, 0x1F, 0x10, 0x13, 0x0000F800, PPC_POWER_BR),
10155 GEN_HANDLER(div, 0x1F, 0x0B, 0x0A, 0x00000000, PPC_POWER_BR),
10156 GEN_HANDLER(divo, 0x1F, 0x0B, 0x1A, 0x00000000, PPC_POWER_BR),
10157 GEN_HANDLER(divs, 0x1F, 0x0B, 0x0B, 0x00000000, PPC_POWER_BR),
10158 GEN_HANDLER(divso, 0x1F, 0x0B, 0x1B, 0x00000000, PPC_POWER_BR),
10159 GEN_HANDLER(doz, 0x1F, 0x08, 0x08, 0x00000000, PPC_POWER_BR),
10160 GEN_HANDLER(dozo, 0x1F, 0x08, 0x18, 0x00000000, PPC_POWER_BR),
10161 GEN_HANDLER(dozi, 0x09, 0xFF, 0xFF, 0x00000000, PPC_POWER_BR),
10162 GEN_HANDLER(lscbx, 0x1F, 0x15, 0x08, 0x00000000, PPC_POWER_BR),
10163 GEN_HANDLER(maskg, 0x1F, 0x1D, 0x00, 0x00000000, PPC_POWER_BR),
10164 GEN_HANDLER(maskir, 0x1F, 0x1D, 0x10, 0x00000000, PPC_POWER_BR),
10165 GEN_HANDLER(mul, 0x1F, 0x0B, 0x03, 0x00000000, PPC_POWER_BR),
10166 GEN_HANDLER(mulo, 0x1F, 0x0B, 0x13, 0x00000000, PPC_POWER_BR),
10167 GEN_HANDLER(nabs, 0x1F, 0x08, 0x0F, 0x00000000, PPC_POWER_BR),
10168 GEN_HANDLER(nabso, 0x1F, 0x08, 0x1F, 0x00000000, PPC_POWER_BR),
10169 GEN_HANDLER(rlmi, 0x16, 0xFF, 0xFF, 0x00000000, PPC_POWER_BR),
10170 GEN_HANDLER(rrib, 0x1F, 0x19, 0x10, 0x00000000, PPC_POWER_BR),
10171 GEN_HANDLER(sle, 0x1F, 0x19, 0x04, 0x00000000, PPC_POWER_BR),
10172 GEN_HANDLER(sleq, 0x1F, 0x19, 0x06, 0x00000000, PPC_POWER_BR),
10173 GEN_HANDLER(sliq, 0x1F, 0x18, 0x05, 0x00000000, PPC_POWER_BR),
10174 GEN_HANDLER(slliq, 0x1F, 0x18, 0x07, 0x00000000, PPC_POWER_BR),
10175 GEN_HANDLER(sllq, 0x1F, 0x18, 0x06, 0x00000000, PPC_POWER_BR),
10176 GEN_HANDLER(slq, 0x1F, 0x18, 0x04, 0x00000000, PPC_POWER_BR),
10177 GEN_HANDLER(sraiq, 0x1F, 0x18, 0x1D, 0x00000000, PPC_POWER_BR),
10178 GEN_HANDLER(sraq, 0x1F, 0x18, 0x1C, 0x00000000, PPC_POWER_BR),
10179 GEN_HANDLER(sre, 0x1F, 0x19, 0x14, 0x00000000, PPC_POWER_BR),
10180 GEN_HANDLER(srea, 0x1F, 0x19, 0x1C, 0x00000000, PPC_POWER_BR),
10181 GEN_HANDLER(sreq, 0x1F, 0x19, 0x16, 0x00000000, PPC_POWER_BR),
10182 GEN_HANDLER(sriq, 0x1F, 0x18, 0x15, 0x00000000, PPC_POWER_BR),
10183 GEN_HANDLER(srliq, 0x1F, 0x18, 0x17, 0x00000000, PPC_POWER_BR),
10184 GEN_HANDLER(srlq, 0x1F, 0x18, 0x16, 0x00000000, PPC_POWER_BR),
10185 GEN_HANDLER(srq, 0x1F, 0x18, 0x14, 0x00000000, PPC_POWER_BR),
10186 GEN_HANDLER(dsa, 0x1F, 0x14, 0x13, 0x03FFF801, PPC_602_SPEC),
10187 GEN_HANDLER(esa, 0x1F, 0x14, 0x12, 0x03FFF801, PPC_602_SPEC),
10188 GEN_HANDLER(mfrom, 0x1F, 0x09, 0x08, 0x03E0F801, PPC_602_SPEC),
10189 GEN_HANDLER2(tlbld_6xx, "tlbld", 0x1F, 0x12, 0x1E, 0x03FF0001, PPC_6xx_TLB),
10190 GEN_HANDLER2(tlbli_6xx, "tlbli", 0x1F, 0x12, 0x1F, 0x03FF0001, PPC_6xx_TLB),
10191 GEN_HANDLER2(tlbld_74xx, "tlbld", 0x1F, 0x12, 0x1E, 0x03FF0001, PPC_74xx_TLB),
10192 GEN_HANDLER2(tlbli_74xx, "tlbli", 0x1F, 0x12, 0x1F, 0x03FF0001, PPC_74xx_TLB),
10193 GEN_HANDLER(clf, 0x1F, 0x16, 0x03, 0x03E00000, PPC_POWER),
10194 GEN_HANDLER(cli, 0x1F, 0x16, 0x0F, 0x03E00000, PPC_POWER),
10195 GEN_HANDLER(dclst, 0x1F, 0x16, 0x13, 0x03E00000, PPC_POWER),
10196 GEN_HANDLER(mfsri, 0x1F, 0x13, 0x13, 0x00000001, PPC_POWER),
10197 GEN_HANDLER(rac, 0x1F, 0x12, 0x19, 0x00000001, PPC_POWER),
10198 GEN_HANDLER(rfsvc, 0x13, 0x12, 0x02, 0x03FFF0001, PPC_POWER),
10199 GEN_HANDLER(lfq, 0x38, 0xFF, 0xFF, 0x00000003, PPC_POWER2),
10200 GEN_HANDLER(lfqu, 0x39, 0xFF, 0xFF, 0x00000003, PPC_POWER2),
10201 GEN_HANDLER(lfqux, 0x1F, 0x17, 0x19, 0x00000001, PPC_POWER2),
10202 GEN_HANDLER(lfqx, 0x1F, 0x17, 0x18, 0x00000001, PPC_POWER2),
10203 GEN_HANDLER(stfq, 0x3C, 0xFF, 0xFF, 0x00000003, PPC_POWER2),
10204 GEN_HANDLER(stfqu, 0x3D, 0xFF, 0xFF, 0x00000003, PPC_POWER2),
10205 GEN_HANDLER(stfqux, 0x1F, 0x17, 0x1D, 0x00000001, PPC_POWER2),
10206 GEN_HANDLER(stfqx, 0x1F, 0x17, 0x1C, 0x00000001, PPC_POWER2),
10207 GEN_HANDLER(mfapidi, 0x1F, 0x13, 0x08, 0x0000F801, PPC_MFAPIDI),
10208 GEN_HANDLER(tlbiva, 0x1F, 0x12, 0x18, 0x03FFF801, PPC_TLBIVA),
10209 GEN_HANDLER(mfdcr, 0x1F, 0x03, 0x0A, 0x00000001, PPC_DCR),
10210 GEN_HANDLER(mtdcr, 0x1F, 0x03, 0x0E, 0x00000001, PPC_DCR),
10211 GEN_HANDLER(mfdcrx, 0x1F, 0x03, 0x08, 0x00000000, PPC_DCRX),
10212 GEN_HANDLER(mtdcrx, 0x1F, 0x03, 0x0C, 0x00000000, PPC_DCRX),
10213 GEN_HANDLER(mfdcrux, 0x1F, 0x03, 0x09, 0x00000000, PPC_DCRUX),
10214 GEN_HANDLER(mtdcrux, 0x1F, 0x03, 0x0D, 0x00000000, PPC_DCRUX),
10215 GEN_HANDLER(dccci, 0x1F, 0x06, 0x0E, 0x03E00001, PPC_4xx_COMMON),
10216 GEN_HANDLER(dcread, 0x1F, 0x06, 0x0F, 0x00000001, PPC_4xx_COMMON),
10217 GEN_HANDLER2(icbt_40x, "icbt", 0x1F, 0x06, 0x08, 0x03E00001, PPC_40x_ICBT),
10218 GEN_HANDLER(iccci, 0x1F, 0x06, 0x1E, 0x00000001, PPC_4xx_COMMON),
10219 GEN_HANDLER(icread, 0x1F, 0x06, 0x1F, 0x03E00001, PPC_4xx_COMMON),
10220 GEN_HANDLER2(rfci_40x, "rfci", 0x13, 0x13, 0x01, 0x03FF8001, PPC_40x_EXCP),
10221 GEN_HANDLER_E(rfci, 0x13, 0x13, 0x01, 0x03FF8001, PPC_BOOKE, PPC2_BOOKE206),
10222 GEN_HANDLER(rfdi, 0x13, 0x07, 0x01, 0x03FF8001, PPC_RFDI),
10223 GEN_HANDLER(rfmci, 0x13, 0x06, 0x01, 0x03FF8001, PPC_RFMCI),
10224 GEN_HANDLER2(tlbre_40x, "tlbre", 0x1F, 0x12, 0x1D, 0x00000001, PPC_40x_TLB),
10225 GEN_HANDLER2(tlbsx_40x, "tlbsx", 0x1F, 0x12, 0x1C, 0x00000000, PPC_40x_TLB),
10226 GEN_HANDLER2(tlbwe_40x, "tlbwe", 0x1F, 0x12, 0x1E, 0x00000001, PPC_40x_TLB),
10227 GEN_HANDLER2(tlbre_440, "tlbre", 0x1F, 0x12, 0x1D, 0x00000001, PPC_BOOKE),
10228 GEN_HANDLER2(tlbsx_440, "tlbsx", 0x1F, 0x12, 0x1C, 0x00000000, PPC_BOOKE),
10229 GEN_HANDLER2(tlbwe_440, "tlbwe", 0x1F, 0x12, 0x1E, 0x00000001, PPC_BOOKE),
10230 GEN_HANDLER2_E(tlbre_booke206, "tlbre", 0x1F, 0x12, 0x1D, 0x00000001,
10231                PPC_NONE, PPC2_BOOKE206),
10232 GEN_HANDLER2_E(tlbsx_booke206, "tlbsx", 0x1F, 0x12, 0x1C, 0x00000000,
10233                PPC_NONE, PPC2_BOOKE206),
10234 GEN_HANDLER2_E(tlbwe_booke206, "tlbwe", 0x1F, 0x12, 0x1E, 0x00000001,
10235                PPC_NONE, PPC2_BOOKE206),
10236 GEN_HANDLER2_E(tlbivax_booke206, "tlbivax", 0x1F, 0x12, 0x18, 0x00000001,
10237                PPC_NONE, PPC2_BOOKE206),
10238 GEN_HANDLER2_E(tlbilx_booke206, "tlbilx", 0x1F, 0x12, 0x00, 0x03800001,
10239                PPC_NONE, PPC2_BOOKE206),
10240 GEN_HANDLER2_E(msgsnd, "msgsnd", 0x1F, 0x0E, 0x06, 0x03ff0001,
10241                PPC_NONE, PPC2_PRCNTL),
10242 GEN_HANDLER2_E(msgclr, "msgclr", 0x1F, 0x0E, 0x07, 0x03ff0001,
10243                PPC_NONE, PPC2_PRCNTL),
10244 GEN_HANDLER(wrtee, 0x1F, 0x03, 0x04, 0x000FFC01, PPC_WRTEE),
10245 GEN_HANDLER(wrteei, 0x1F, 0x03, 0x05, 0x000E7C01, PPC_WRTEE),
10246 GEN_HANDLER(dlmzb, 0x1F, 0x0E, 0x02, 0x00000000, PPC_440_SPEC),
10247 GEN_HANDLER_E(mbar, 0x1F, 0x16, 0x1a, 0x001FF801,
10248               PPC_BOOKE, PPC2_BOOKE206),
10249 GEN_HANDLER(msync_4xx, 0x1F, 0x16, 0x12, 0x03FFF801, PPC_BOOKE),
10250 GEN_HANDLER2_E(icbt_440, "icbt", 0x1F, 0x16, 0x00, 0x03E00001,
10251                PPC_BOOKE, PPC2_BOOKE206),
10252 GEN_HANDLER(lvsl, 0x1f, 0x06, 0x00, 0x00000001, PPC_ALTIVEC),
10253 GEN_HANDLER(lvsr, 0x1f, 0x06, 0x01, 0x00000001, PPC_ALTIVEC),
10254 GEN_HANDLER(mfvscr, 0x04, 0x2, 0x18, 0x001ff800, PPC_ALTIVEC),
10255 GEN_HANDLER(mtvscr, 0x04, 0x2, 0x19, 0x03ff0000, PPC_ALTIVEC),
10256 GEN_HANDLER(vmladduhm, 0x04, 0x11, 0xFF, 0x00000000, PPC_ALTIVEC),
10257 GEN_HANDLER2(evsel0, "evsel", 0x04, 0x1c, 0x09, 0x00000000, PPC_SPE),
10258 GEN_HANDLER2(evsel1, "evsel", 0x04, 0x1d, 0x09, 0x00000000, PPC_SPE),
10259 GEN_HANDLER2(evsel2, "evsel", 0x04, 0x1e, 0x09, 0x00000000, PPC_SPE),
10260 GEN_HANDLER2(evsel3, "evsel", 0x04, 0x1f, 0x09, 0x00000000, PPC_SPE),
10261
10262 #undef GEN_INT_ARITH_ADD
10263 #undef GEN_INT_ARITH_ADD_CONST
10264 #define GEN_INT_ARITH_ADD(name, opc3, add_ca, compute_ca, compute_ov)         \
10265 GEN_HANDLER(name, 0x1F, 0x0A, opc3, 0x00000000, PPC_INTEGER),
10266 #define GEN_INT_ARITH_ADD_CONST(name, opc3, const_val,                        \
10267                                 add_ca, compute_ca, compute_ov)               \
10268 GEN_HANDLER(name, 0x1F, 0x0A, opc3, 0x0000F800, PPC_INTEGER),
10269 GEN_INT_ARITH_ADD(add, 0x08, 0, 0, 0)
10270 GEN_INT_ARITH_ADD(addo, 0x18, 0, 0, 1)
10271 GEN_INT_ARITH_ADD(addc, 0x00, 0, 1, 0)
10272 GEN_INT_ARITH_ADD(addco, 0x10, 0, 1, 1)
10273 GEN_INT_ARITH_ADD(adde, 0x04, 1, 1, 0)
10274 GEN_INT_ARITH_ADD(addeo, 0x14, 1, 1, 1)
10275 GEN_INT_ARITH_ADD_CONST(addme, 0x07, -1LL, 1, 1, 0)
10276 GEN_INT_ARITH_ADD_CONST(addmeo, 0x17, -1LL, 1, 1, 1)
10277 GEN_INT_ARITH_ADD_CONST(addze, 0x06, 0, 1, 1, 0)
10278 GEN_INT_ARITH_ADD_CONST(addzeo, 0x16, 0, 1, 1, 1)
10279
10280 #undef GEN_INT_ARITH_DIVW
10281 #define GEN_INT_ARITH_DIVW(name, opc3, sign, compute_ov)                      \
10282 GEN_HANDLER(name, 0x1F, 0x0B, opc3, 0x00000000, PPC_INTEGER)
10283 GEN_INT_ARITH_DIVW(divwu, 0x0E, 0, 0),
10284 GEN_INT_ARITH_DIVW(divwuo, 0x1E, 0, 1),
10285 GEN_INT_ARITH_DIVW(divw, 0x0F, 1, 0),
10286 GEN_INT_ARITH_DIVW(divwo, 0x1F, 1, 1),
10287 GEN_HANDLER_E(divwe, 0x1F, 0x0B, 0x0D, 0, PPC_NONE, PPC2_DIVE_ISA206),
10288 GEN_HANDLER_E(divweo, 0x1F, 0x0B, 0x1D, 0, PPC_NONE, PPC2_DIVE_ISA206),
10289 GEN_HANDLER_E(divweu, 0x1F, 0x0B, 0x0C, 0, PPC_NONE, PPC2_DIVE_ISA206),
10290 GEN_HANDLER_E(divweuo, 0x1F, 0x0B, 0x1C, 0, PPC_NONE, PPC2_DIVE_ISA206),
10291 GEN_HANDLER_E(modsw, 0x1F, 0x0B, 0x18, 0x00000001, PPC_NONE, PPC2_ISA300),
10292 GEN_HANDLER_E(moduw, 0x1F, 0x0B, 0x08, 0x00000001, PPC_NONE, PPC2_ISA300),
10293
10294 #if defined(TARGET_PPC64)
10295 #undef GEN_INT_ARITH_DIVD
10296 #define GEN_INT_ARITH_DIVD(name, opc3, sign, compute_ov)                      \
10297 GEN_HANDLER(name, 0x1F, 0x09, opc3, 0x00000000, PPC_64B)
10298 GEN_INT_ARITH_DIVD(divdu, 0x0E, 0, 0),
10299 GEN_INT_ARITH_DIVD(divduo, 0x1E, 0, 1),
10300 GEN_INT_ARITH_DIVD(divd, 0x0F, 1, 0),
10301 GEN_INT_ARITH_DIVD(divdo, 0x1F, 1, 1),
10302
10303 GEN_HANDLER_E(divdeu, 0x1F, 0x09, 0x0C, 0, PPC_NONE, PPC2_DIVE_ISA206),
10304 GEN_HANDLER_E(divdeuo, 0x1F, 0x09, 0x1C, 0, PPC_NONE, PPC2_DIVE_ISA206),
10305 GEN_HANDLER_E(divde, 0x1F, 0x09, 0x0D, 0, PPC_NONE, PPC2_DIVE_ISA206),
10306 GEN_HANDLER_E(divdeo, 0x1F, 0x09, 0x1D, 0, PPC_NONE, PPC2_DIVE_ISA206),
10307
10308 #undef GEN_INT_ARITH_MUL_HELPER
10309 #define GEN_INT_ARITH_MUL_HELPER(name, opc3)                                  \
10310 GEN_HANDLER(name, 0x1F, 0x09, opc3, 0x00000000, PPC_64B)
10311 GEN_INT_ARITH_MUL_HELPER(mulhdu, 0x00),
10312 GEN_INT_ARITH_MUL_HELPER(mulhd, 0x02),
10313 GEN_INT_ARITH_MUL_HELPER(mulldo, 0x17),
10314 #endif
10315
10316 #undef GEN_INT_ARITH_SUBF
10317 #undef GEN_INT_ARITH_SUBF_CONST
10318 #define GEN_INT_ARITH_SUBF(name, opc3, add_ca, compute_ca, compute_ov)        \
10319 GEN_HANDLER(name, 0x1F, 0x08, opc3, 0x00000000, PPC_INTEGER),
10320 #define GEN_INT_ARITH_SUBF_CONST(name, opc3, const_val,                       \
10321                                 add_ca, compute_ca, compute_ov)               \
10322 GEN_HANDLER(name, 0x1F, 0x08, opc3, 0x0000F800, PPC_INTEGER),
10323 GEN_INT_ARITH_SUBF(subf, 0x01, 0, 0, 0)
10324 GEN_INT_ARITH_SUBF(subfo, 0x11, 0, 0, 1)
10325 GEN_INT_ARITH_SUBF(subfc, 0x00, 0, 1, 0)
10326 GEN_INT_ARITH_SUBF(subfco, 0x10, 0, 1, 1)
10327 GEN_INT_ARITH_SUBF(subfe, 0x04, 1, 1, 0)
10328 GEN_INT_ARITH_SUBF(subfeo, 0x14, 1, 1, 1)
10329 GEN_INT_ARITH_SUBF_CONST(subfme, 0x07, -1LL, 1, 1, 0)
10330 GEN_INT_ARITH_SUBF_CONST(subfmeo, 0x17, -1LL, 1, 1, 1)
10331 GEN_INT_ARITH_SUBF_CONST(subfze, 0x06, 0, 1, 1, 0)
10332 GEN_INT_ARITH_SUBF_CONST(subfzeo, 0x16, 0, 1, 1, 1)
10333
10334 #undef GEN_LOGICAL1
10335 #undef GEN_LOGICAL2
10336 #define GEN_LOGICAL2(name, tcg_op, opc, type)                                 \
10337 GEN_HANDLER(name, 0x1F, 0x1C, opc, 0x00000000, type)
10338 #define GEN_LOGICAL1(name, tcg_op, opc, type)                                 \
10339 GEN_HANDLER(name, 0x1F, 0x1A, opc, 0x00000000, type)
10340 GEN_LOGICAL2(and, tcg_gen_and_tl, 0x00, PPC_INTEGER),
10341 GEN_LOGICAL2(andc, tcg_gen_andc_tl, 0x01, PPC_INTEGER),
10342 GEN_LOGICAL2(eqv, tcg_gen_eqv_tl, 0x08, PPC_INTEGER),
10343 GEN_LOGICAL1(extsb, tcg_gen_ext8s_tl, 0x1D, PPC_INTEGER),
10344 GEN_LOGICAL1(extsh, tcg_gen_ext16s_tl, 0x1C, PPC_INTEGER),
10345 GEN_LOGICAL2(nand, tcg_gen_nand_tl, 0x0E, PPC_INTEGER),
10346 GEN_LOGICAL2(nor, tcg_gen_nor_tl, 0x03, PPC_INTEGER),
10347 GEN_LOGICAL2(orc, tcg_gen_orc_tl, 0x0C, PPC_INTEGER),
10348 #if defined(TARGET_PPC64)
10349 GEN_LOGICAL1(extsw, tcg_gen_ext32s_tl, 0x1E, PPC_64B),
10350 #endif
10351
10352 #if defined(TARGET_PPC64)
10353 #undef GEN_PPC64_R2
10354 #undef GEN_PPC64_R4
10355 #define GEN_PPC64_R2(name, opc1, opc2)                                        \
10356 GEN_HANDLER2(name##0, stringify(name), opc1, opc2, 0xFF, 0x00000000, PPC_64B),\
10357 GEN_HANDLER2(name##1, stringify(name), opc1, opc2 | 0x10, 0xFF, 0x00000000,   \
10358              PPC_64B)
10359 #define GEN_PPC64_R4(name, opc1, opc2)                                        \
10360 GEN_HANDLER2(name##0, stringify(name), opc1, opc2, 0xFF, 0x00000000, PPC_64B),\
10361 GEN_HANDLER2(name##1, stringify(name), opc1, opc2 | 0x01, 0xFF, 0x00000000,   \
10362              PPC_64B),                                                        \
10363 GEN_HANDLER2(name##2, stringify(name), opc1, opc2 | 0x10, 0xFF, 0x00000000,   \
10364              PPC_64B),                                                        \
10365 GEN_HANDLER2(name##3, stringify(name), opc1, opc2 | 0x11, 0xFF, 0x00000000,   \
10366              PPC_64B)
10367 GEN_PPC64_R4(rldicl, 0x1E, 0x00),
10368 GEN_PPC64_R4(rldicr, 0x1E, 0x02),
10369 GEN_PPC64_R4(rldic, 0x1E, 0x04),
10370 GEN_PPC64_R2(rldcl, 0x1E, 0x08),
10371 GEN_PPC64_R2(rldcr, 0x1E, 0x09),
10372 GEN_PPC64_R4(rldimi, 0x1E, 0x06),
10373 #endif
10374
10375 #undef _GEN_FLOAT_ACB
10376 #undef GEN_FLOAT_ACB
10377 #undef _GEN_FLOAT_AB
10378 #undef GEN_FLOAT_AB
10379 #undef _GEN_FLOAT_AC
10380 #undef GEN_FLOAT_AC
10381 #undef GEN_FLOAT_B
10382 #undef GEN_FLOAT_BS
10383 #define _GEN_FLOAT_ACB(name, op, op1, op2, isfloat, set_fprf, type)           \
10384 GEN_HANDLER(f##name, op1, op2, 0xFF, 0x00000000, type)
10385 #define GEN_FLOAT_ACB(name, op2, set_fprf, type)                              \
10386 _GEN_FLOAT_ACB(name, name, 0x3F, op2, 0, set_fprf, type),                     \
10387 _GEN_FLOAT_ACB(name##s, name, 0x3B, op2, 1, set_fprf, type)
10388 #define _GEN_FLOAT_AB(name, op, op1, op2, inval, isfloat, set_fprf, type)     \
10389 GEN_HANDLER(f##name, op1, op2, 0xFF, inval, type)
10390 #define GEN_FLOAT_AB(name, op2, inval, set_fprf, type)                        \
10391 _GEN_FLOAT_AB(name, name, 0x3F, op2, inval, 0, set_fprf, type),               \
10392 _GEN_FLOAT_AB(name##s, name, 0x3B, op2, inval, 1, set_fprf, type)
10393 #define _GEN_FLOAT_AC(name, op, op1, op2, inval, isfloat, set_fprf, type)     \
10394 GEN_HANDLER(f##name, op1, op2, 0xFF, inval, type)
10395 #define GEN_FLOAT_AC(name, op2, inval, set_fprf, type)                        \
10396 _GEN_FLOAT_AC(name, name, 0x3F, op2, inval, 0, set_fprf, type),               \
10397 _GEN_FLOAT_AC(name##s, name, 0x3B, op2, inval, 1, set_fprf, type)
10398 #define GEN_FLOAT_B(name, op2, op3, set_fprf, type)                           \
10399 GEN_HANDLER(f##name, 0x3F, op2, op3, 0x001F0000, type)
10400 #define GEN_FLOAT_BS(name, op1, op2, set_fprf, type)                          \
10401 GEN_HANDLER(f##name, op1, op2, 0xFF, 0x001F07C0, type)
10402
10403 GEN_FLOAT_AB(add, 0x15, 0x000007C0, 1, PPC_FLOAT),
10404 GEN_FLOAT_AB(div, 0x12, 0x000007C0, 1, PPC_FLOAT),
10405 GEN_FLOAT_AC(mul, 0x19, 0x0000F800, 1, PPC_FLOAT),
10406 GEN_FLOAT_BS(re, 0x3F, 0x18, 1, PPC_FLOAT_EXT),
10407 GEN_FLOAT_BS(res, 0x3B, 0x18, 1, PPC_FLOAT_FRES),
10408 GEN_FLOAT_BS(rsqrte, 0x3F, 0x1A, 1, PPC_FLOAT_FRSQRTE),
10409 _GEN_FLOAT_ACB(sel, sel, 0x3F, 0x17, 0, 0, PPC_FLOAT_FSEL),
10410 GEN_FLOAT_AB(sub, 0x14, 0x000007C0, 1, PPC_FLOAT),
10411 GEN_FLOAT_ACB(madd, 0x1D, 1, PPC_FLOAT),
10412 GEN_FLOAT_ACB(msub, 0x1C, 1, PPC_FLOAT),
10413 GEN_FLOAT_ACB(nmadd, 0x1F, 1, PPC_FLOAT),
10414 GEN_FLOAT_ACB(nmsub, 0x1E, 1, PPC_FLOAT),
10415 GEN_HANDLER_E(ftdiv, 0x3F, 0x00, 0x04, 1, PPC_NONE, PPC2_FP_TST_ISA206),
10416 GEN_HANDLER_E(ftsqrt, 0x3F, 0x00, 0x05, 1, PPC_NONE, PPC2_FP_TST_ISA206),
10417 GEN_FLOAT_B(ctiw, 0x0E, 0x00, 0, PPC_FLOAT),
10418 GEN_HANDLER_E(fctiwu, 0x3F, 0x0E, 0x04, 0, PPC_NONE, PPC2_FP_CVT_ISA206),
10419 GEN_FLOAT_B(ctiwz, 0x0F, 0x00, 0, PPC_FLOAT),
10420 GEN_HANDLER_E(fctiwuz, 0x3F, 0x0F, 0x04, 0, PPC_NONE, PPC2_FP_CVT_ISA206),
10421 GEN_FLOAT_B(rsp, 0x0C, 0x00, 1, PPC_FLOAT),
10422 GEN_HANDLER_E(fcfid, 0x3F, 0x0E, 0x1A, 0x001F0000, PPC_NONE, PPC2_FP_CVT_S64),
10423 GEN_HANDLER_E(fcfids, 0x3B, 0x0E, 0x1A, 0, PPC_NONE, PPC2_FP_CVT_ISA206),
10424 GEN_HANDLER_E(fcfidu, 0x3F, 0x0E, 0x1E, 0, PPC_NONE, PPC2_FP_CVT_ISA206),
10425 GEN_HANDLER_E(fcfidus, 0x3B, 0x0E, 0x1E, 0, PPC_NONE, PPC2_FP_CVT_ISA206),
10426 GEN_HANDLER_E(fctid, 0x3F, 0x0E, 0x19, 0x001F0000, PPC_NONE, PPC2_FP_CVT_S64),
10427 GEN_HANDLER_E(fctidu, 0x3F, 0x0E, 0x1D, 0, PPC_NONE, PPC2_FP_CVT_ISA206),
10428 GEN_HANDLER_E(fctidz, 0x3F, 0x0F, 0x19, 0x001F0000, PPC_NONE, PPC2_FP_CVT_S64),
10429 GEN_HANDLER_E(fctiduz, 0x3F, 0x0F, 0x1D, 0, PPC_NONE, PPC2_FP_CVT_ISA206),
10430 GEN_FLOAT_B(rin, 0x08, 0x0C, 1, PPC_FLOAT_EXT),
10431 GEN_FLOAT_B(riz, 0x08, 0x0D, 1, PPC_FLOAT_EXT),
10432 GEN_FLOAT_B(rip, 0x08, 0x0E, 1, PPC_FLOAT_EXT),
10433 GEN_FLOAT_B(rim, 0x08, 0x0F, 1, PPC_FLOAT_EXT),
10434
10435 #undef GEN_LD
10436 #undef GEN_LDU
10437 #undef GEN_LDUX
10438 #undef GEN_LDX_E
10439 #undef GEN_LDS
10440 #define GEN_LD(name, ldop, opc, type)                                         \
10441 GEN_HANDLER(name, opc, 0xFF, 0xFF, 0x00000000, type),
10442 #define GEN_LDU(name, ldop, opc, type)                                        \
10443 GEN_HANDLER(name##u, opc, 0xFF, 0xFF, 0x00000000, type),
10444 #define GEN_LDUX(name, ldop, opc2, opc3, type)                                \
10445 GEN_HANDLER(name##ux, 0x1F, opc2, opc3, 0x00000001, type),
10446 #define GEN_LDX_E(name, ldop, opc2, opc3, type, type2, chk)                   \
10447 GEN_HANDLER_E(name##x, 0x1F, opc2, opc3, 0x00000001, type, type2),
10448 #define GEN_LDS(name, ldop, op, type)                                         \
10449 GEN_LD(name, ldop, op | 0x20, type)                                           \
10450 GEN_LDU(name, ldop, op | 0x21, type)                                          \
10451 GEN_LDUX(name, ldop, 0x17, op | 0x01, type)                                   \
10452 GEN_LDX(name, ldop, 0x17, op | 0x00, type)
10453
10454 GEN_LDS(lbz, ld8u, 0x02, PPC_INTEGER)
10455 GEN_LDS(lha, ld16s, 0x0A, PPC_INTEGER)
10456 GEN_LDS(lhz, ld16u, 0x08, PPC_INTEGER)
10457 GEN_LDS(lwz, ld32u, 0x00, PPC_INTEGER)
10458 #if defined(TARGET_PPC64)
10459 GEN_LDUX(lwa, ld32s, 0x15, 0x0B, PPC_64B)
10460 GEN_LDX(lwa, ld32s, 0x15, 0x0A, PPC_64B)
10461 GEN_LDUX(ld, ld64, 0x15, 0x01, PPC_64B)
10462 GEN_LDX(ld, ld64, 0x15, 0x00, PPC_64B)
10463 GEN_LDX_E(ldbr, ld64ur, 0x14, 0x10, PPC_NONE, PPC2_DBRX, CHK_NONE)
10464
10465 /* HV/P7 and later only */
10466 GEN_LDX_HVRM(ldcix, ld64, 0x15, 0x1b, PPC_CILDST)
10467 GEN_LDX_HVRM(lwzcix, ld32u, 0x15, 0x18, PPC_CILDST)
10468 GEN_LDX_HVRM(lhzcix, ld16u, 0x15, 0x19, PPC_CILDST)
10469 GEN_LDX_HVRM(lbzcix, ld8u, 0x15, 0x1a, PPC_CILDST)
10470 #endif
10471 GEN_LDX(lhbr, ld16ur, 0x16, 0x18, PPC_INTEGER)
10472 GEN_LDX(lwbr, ld32ur, 0x16, 0x10, PPC_INTEGER)
10473
10474 #undef GEN_ST
10475 #undef GEN_STU
10476 #undef GEN_STUX
10477 #undef GEN_STX_E
10478 #undef GEN_STS
10479 #define GEN_ST(name, stop, opc, type)                                         \
10480 GEN_HANDLER(name, opc, 0xFF, 0xFF, 0x00000000, type),
10481 #define GEN_STU(name, stop, opc, type)                                        \
10482 GEN_HANDLER(stop##u, opc, 0xFF, 0xFF, 0x00000000, type),
10483 #define GEN_STUX(name, stop, opc2, opc3, type)                                \
10484 GEN_HANDLER(name##ux, 0x1F, opc2, opc3, 0x00000001, type),
10485 #define GEN_STX_E(name, stop, opc2, opc3, type, type2, chk)                   \
10486 GEN_HANDLER_E(name##x, 0x1F, opc2, opc3, 0x00000001, type, type2),
10487 #define GEN_STS(name, stop, op, type)                                         \
10488 GEN_ST(name, stop, op | 0x20, type)                                           \
10489 GEN_STU(name, stop, op | 0x21, type)                                          \
10490 GEN_STUX(name, stop, 0x17, op | 0x01, type)                                   \
10491 GEN_STX(name, stop, 0x17, op | 0x00, type)
10492
10493 GEN_STS(stb, st8, 0x06, PPC_INTEGER)
10494 GEN_STS(sth, st16, 0x0C, PPC_INTEGER)
10495 GEN_STS(stw, st32, 0x04, PPC_INTEGER)
10496 #if defined(TARGET_PPC64)
10497 GEN_STUX(std, st64, 0x15, 0x05, PPC_64B)
10498 GEN_STX(std, st64, 0x15, 0x04, PPC_64B)
10499 GEN_STX_E(stdbr, st64r, 0x14, 0x14, PPC_NONE, PPC2_DBRX, CHK_NONE)
10500 GEN_STX_HVRM(stdcix, st64, 0x15, 0x1f, PPC_CILDST)
10501 GEN_STX_HVRM(stwcix, st32, 0x15, 0x1c, PPC_CILDST)
10502 GEN_STX_HVRM(sthcix, st16, 0x15, 0x1d, PPC_CILDST)
10503 GEN_STX_HVRM(stbcix, st8, 0x15, 0x1e, PPC_CILDST)
10504 #endif
10505 GEN_STX(sthbr, st16r, 0x16, 0x1C, PPC_INTEGER)
10506 GEN_STX(stwbr, st32r, 0x16, 0x14, PPC_INTEGER)
10507
10508 #undef GEN_LDF
10509 #undef GEN_LDUF
10510 #undef GEN_LDUXF
10511 #undef GEN_LDXF
10512 #undef GEN_LDFS
10513 #define GEN_LDF(name, ldop, opc, type)                                        \
10514 GEN_HANDLER(name, opc, 0xFF, 0xFF, 0x00000000, type),
10515 #define GEN_LDUF(name, ldop, opc, type)                                       \
10516 GEN_HANDLER(name##u, opc, 0xFF, 0xFF, 0x00000000, type),
10517 #define GEN_LDUXF(name, ldop, opc, type)                                      \
10518 GEN_HANDLER(name##ux, 0x1F, 0x17, opc, 0x00000001, type),
10519 #define GEN_LDXF(name, ldop, opc2, opc3, type)                                \
10520 GEN_HANDLER(name##x, 0x1F, opc2, opc3, 0x00000001, type),
10521 #define GEN_LDFS(name, ldop, op, type)                                        \
10522 GEN_LDF(name, ldop, op | 0x20, type)                                          \
10523 GEN_LDUF(name, ldop, op | 0x21, type)                                         \
10524 GEN_LDUXF(name, ldop, op | 0x01, type)                                        \
10525 GEN_LDXF(name, ldop, 0x17, op | 0x00, type)
10526
10527 GEN_LDFS(lfd, ld64, 0x12, PPC_FLOAT)
10528 GEN_LDFS(lfs, ld32fs, 0x10, PPC_FLOAT)
10529 GEN_HANDLER_E(lfiwax, 0x1f, 0x17, 0x1a, 0x00000001, PPC_NONE, PPC2_ISA205),
10530 GEN_HANDLER_E(lfiwzx, 0x1f, 0x17, 0x1b, 0x1, PPC_NONE, PPC2_FP_CVT_ISA206),
10531 GEN_HANDLER_E(lfdp, 0x39, 0xFF, 0xFF, 0x00200003, PPC_NONE, PPC2_ISA205),
10532 GEN_HANDLER_E(lfdpx, 0x1F, 0x17, 0x18, 0x00200001, PPC_NONE, PPC2_ISA205),
10533
10534 #undef GEN_STF
10535 #undef GEN_STUF
10536 #undef GEN_STUXF
10537 #undef GEN_STXF
10538 #undef GEN_STFS
10539 #define GEN_STF(name, stop, opc, type)                                        \
10540 GEN_HANDLER(name, opc, 0xFF, 0xFF, 0x00000000, type),
10541 #define GEN_STUF(name, stop, opc, type)                                       \
10542 GEN_HANDLER(name##u, opc, 0xFF, 0xFF, 0x00000000, type),
10543 #define GEN_STUXF(name, stop, opc, type)                                      \
10544 GEN_HANDLER(name##ux, 0x1F, 0x17, opc, 0x00000001, type),
10545 #define GEN_STXF(name, stop, opc2, opc3, type)                                \
10546 GEN_HANDLER(name##x, 0x1F, opc2, opc3, 0x00000001, type),
10547 #define GEN_STFS(name, stop, op, type)                                        \
10548 GEN_STF(name, stop, op | 0x20, type)                                          \
10549 GEN_STUF(name, stop, op | 0x21, type)                                         \
10550 GEN_STUXF(name, stop, op | 0x01, type)                                        \
10551 GEN_STXF(name, stop, 0x17, op | 0x00, type)
10552
10553 GEN_STFS(stfd, st64, 0x16, PPC_FLOAT)
10554 GEN_STFS(stfs, st32fs, 0x14, PPC_FLOAT)
10555 GEN_STXF(stfiw, st32fiw, 0x17, 0x1E, PPC_FLOAT_STFIWX)
10556 GEN_HANDLER_E(stfdp, 0x3D, 0xFF, 0xFF, 0x00200003, PPC_NONE, PPC2_ISA205),
10557 GEN_HANDLER_E(stfdpx, 0x1F, 0x17, 0x1C, 0x00200001, PPC_NONE, PPC2_ISA205),
10558
10559 #undef GEN_CRLOGIC
10560 #define GEN_CRLOGIC(name, tcg_op, opc)                                        \
10561 GEN_HANDLER(name, 0x13, 0x01, opc, 0x00000001, PPC_INTEGER)
10562 GEN_CRLOGIC(crand, tcg_gen_and_i32, 0x08),
10563 GEN_CRLOGIC(crandc, tcg_gen_andc_i32, 0x04),
10564 GEN_CRLOGIC(creqv, tcg_gen_eqv_i32, 0x09),
10565 GEN_CRLOGIC(crnand, tcg_gen_nand_i32, 0x07),
10566 GEN_CRLOGIC(crnor, tcg_gen_nor_i32, 0x01),
10567 GEN_CRLOGIC(cror, tcg_gen_or_i32, 0x0E),
10568 GEN_CRLOGIC(crorc, tcg_gen_orc_i32, 0x0D),
10569 GEN_CRLOGIC(crxor, tcg_gen_xor_i32, 0x06),
10570
10571 #undef GEN_MAC_HANDLER
10572 #define GEN_MAC_HANDLER(name, opc2, opc3)                                     \
10573 GEN_HANDLER(name, 0x04, opc2, opc3, 0x00000000, PPC_405_MAC)
10574 GEN_MAC_HANDLER(macchw, 0x0C, 0x05),
10575 GEN_MAC_HANDLER(macchwo, 0x0C, 0x15),
10576 GEN_MAC_HANDLER(macchws, 0x0C, 0x07),
10577 GEN_MAC_HANDLER(macchwso, 0x0C, 0x17),
10578 GEN_MAC_HANDLER(macchwsu, 0x0C, 0x06),
10579 GEN_MAC_HANDLER(macchwsuo, 0x0C, 0x16),
10580 GEN_MAC_HANDLER(macchwu, 0x0C, 0x04),
10581 GEN_MAC_HANDLER(macchwuo, 0x0C, 0x14),
10582 GEN_MAC_HANDLER(machhw, 0x0C, 0x01),
10583 GEN_MAC_HANDLER(machhwo, 0x0C, 0x11),
10584 GEN_MAC_HANDLER(machhws, 0x0C, 0x03),
10585 GEN_MAC_HANDLER(machhwso, 0x0C, 0x13),
10586 GEN_MAC_HANDLER(machhwsu, 0x0C, 0x02),
10587 GEN_MAC_HANDLER(machhwsuo, 0x0C, 0x12),
10588 GEN_MAC_HANDLER(machhwu, 0x0C, 0x00),
10589 GEN_MAC_HANDLER(machhwuo, 0x0C, 0x10),
10590 GEN_MAC_HANDLER(maclhw, 0x0C, 0x0D),
10591 GEN_MAC_HANDLER(maclhwo, 0x0C, 0x1D),
10592 GEN_MAC_HANDLER(maclhws, 0x0C, 0x0F),
10593 GEN_MAC_HANDLER(maclhwso, 0x0C, 0x1F),
10594 GEN_MAC_HANDLER(maclhwu, 0x0C, 0x0C),
10595 GEN_MAC_HANDLER(maclhwuo, 0x0C, 0x1C),
10596 GEN_MAC_HANDLER(maclhwsu, 0x0C, 0x0E),
10597 GEN_MAC_HANDLER(maclhwsuo, 0x0C, 0x1E),
10598 GEN_MAC_HANDLER(nmacchw, 0x0E, 0x05),
10599 GEN_MAC_HANDLER(nmacchwo, 0x0E, 0x15),
10600 GEN_MAC_HANDLER(nmacchws, 0x0E, 0x07),
10601 GEN_MAC_HANDLER(nmacchwso, 0x0E, 0x17),
10602 GEN_MAC_HANDLER(nmachhw, 0x0E, 0x01),
10603 GEN_MAC_HANDLER(nmachhwo, 0x0E, 0x11),
10604 GEN_MAC_HANDLER(nmachhws, 0x0E, 0x03),
10605 GEN_MAC_HANDLER(nmachhwso, 0x0E, 0x13),
10606 GEN_MAC_HANDLER(nmaclhw, 0x0E, 0x0D),
10607 GEN_MAC_HANDLER(nmaclhwo, 0x0E, 0x1D),
10608 GEN_MAC_HANDLER(nmaclhws, 0x0E, 0x0F),
10609 GEN_MAC_HANDLER(nmaclhwso, 0x0E, 0x1F),
10610 GEN_MAC_HANDLER(mulchw, 0x08, 0x05),
10611 GEN_MAC_HANDLER(mulchwu, 0x08, 0x04),
10612 GEN_MAC_HANDLER(mulhhw, 0x08, 0x01),
10613 GEN_MAC_HANDLER(mulhhwu, 0x08, 0x00),
10614 GEN_MAC_HANDLER(mullhw, 0x08, 0x0D),
10615 GEN_MAC_HANDLER(mullhwu, 0x08, 0x0C),
10616
10617 #undef GEN_VR_LDX
10618 #undef GEN_VR_STX
10619 #undef GEN_VR_LVE
10620 #undef GEN_VR_STVE
10621 #define GEN_VR_LDX(name, opc2, opc3)                                          \
10622 GEN_HANDLER(name, 0x1F, opc2, opc3, 0x00000001, PPC_ALTIVEC)
10623 #define GEN_VR_STX(name, opc2, opc3)                                          \
10624 GEN_HANDLER(st##name, 0x1F, opc2, opc3, 0x00000001, PPC_ALTIVEC)
10625 #define GEN_VR_LVE(name, opc2, opc3)                                    \
10626     GEN_HANDLER(lve##name, 0x1F, opc2, opc3, 0x00000001, PPC_ALTIVEC)
10627 #define GEN_VR_STVE(name, opc2, opc3)                                   \
10628     GEN_HANDLER(stve##name, 0x1F, opc2, opc3, 0x00000001, PPC_ALTIVEC)
10629 GEN_VR_LDX(lvx, 0x07, 0x03),
10630 GEN_VR_LDX(lvxl, 0x07, 0x0B),
10631 GEN_VR_LVE(bx, 0x07, 0x00),
10632 GEN_VR_LVE(hx, 0x07, 0x01),
10633 GEN_VR_LVE(wx, 0x07, 0x02),
10634 GEN_VR_STX(svx, 0x07, 0x07),
10635 GEN_VR_STX(svxl, 0x07, 0x0F),
10636 GEN_VR_STVE(bx, 0x07, 0x04),
10637 GEN_VR_STVE(hx, 0x07, 0x05),
10638 GEN_VR_STVE(wx, 0x07, 0x06),
10639
10640 #undef GEN_VX_LOGICAL
10641 #define GEN_VX_LOGICAL(name, tcg_op, opc2, opc3)                        \
10642 GEN_HANDLER(name, 0x04, opc2, opc3, 0x00000000, PPC_ALTIVEC)
10643
10644 #undef GEN_VX_LOGICAL_207
10645 #define GEN_VX_LOGICAL_207(name, tcg_op, opc2, opc3) \
10646 GEN_HANDLER_E(name, 0x04, opc2, opc3, 0x00000000, PPC_NONE, PPC2_ALTIVEC_207)
10647
10648 GEN_VX_LOGICAL(vand, tcg_gen_and_i64, 2, 16),
10649 GEN_VX_LOGICAL(vandc, tcg_gen_andc_i64, 2, 17),
10650 GEN_VX_LOGICAL(vor, tcg_gen_or_i64, 2, 18),
10651 GEN_VX_LOGICAL(vxor, tcg_gen_xor_i64, 2, 19),
10652 GEN_VX_LOGICAL(vnor, tcg_gen_nor_i64, 2, 20),
10653 GEN_VX_LOGICAL_207(veqv, tcg_gen_eqv_i64, 2, 26),
10654 GEN_VX_LOGICAL_207(vnand, tcg_gen_nand_i64, 2, 22),
10655 GEN_VX_LOGICAL_207(vorc, tcg_gen_orc_i64, 2, 21),
10656
10657 #undef GEN_VXFORM
10658 #define GEN_VXFORM(name, opc2, opc3)                                    \
10659 GEN_HANDLER(name, 0x04, opc2, opc3, 0x00000000, PPC_ALTIVEC)
10660
10661 #undef GEN_VXFORM_207
10662 #define GEN_VXFORM_207(name, opc2, opc3) \
10663 GEN_HANDLER_E(name, 0x04, opc2, opc3, 0x00000000, PPC_NONE, PPC2_ALTIVEC_207)
10664
10665 #undef GEN_VXFORM_DUAL
10666 #define GEN_VXFORM_DUAL(name0, name1, opc2, opc3, type0, type1) \
10667 GEN_HANDLER_E(name0##_##name1, 0x4, opc2, opc3, 0x00000000, type0, type1)
10668
10669 #undef GEN_VXRFORM_DUAL
10670 #define GEN_VXRFORM_DUAL(name0, name1, opc2, opc3, tp0, tp1) \
10671 GEN_HANDLER_E(name0##_##name1, 0x4, opc2, opc3, 0x00000000, tp0, tp1), \
10672 GEN_HANDLER_E(name0##_##name1, 0x4, opc2, (opc3 | 0x10), 0x00000000, tp0, tp1),
10673
10674 GEN_VXFORM(vaddubm, 0, 0),
10675 GEN_VXFORM(vadduhm, 0, 1),
10676 GEN_VXFORM(vadduwm, 0, 2),
10677 GEN_VXFORM_207(vaddudm, 0, 3),
10678 GEN_VXFORM_DUAL(vsububm, bcdadd, 0, 16, PPC_ALTIVEC, PPC_NONE),
10679 GEN_VXFORM_DUAL(vsubuhm, bcdsub, 0, 17, PPC_ALTIVEC, PPC_NONE),
10680 GEN_VXFORM(vsubuwm, 0, 18),
10681 GEN_VXFORM_207(vsubudm, 0, 19),
10682 GEN_VXFORM(vmaxub, 1, 0),
10683 GEN_VXFORM(vmaxuh, 1, 1),
10684 GEN_VXFORM(vmaxuw, 1, 2),
10685 GEN_VXFORM_207(vmaxud, 1, 3),
10686 GEN_VXFORM(vmaxsb, 1, 4),
10687 GEN_VXFORM(vmaxsh, 1, 5),
10688 GEN_VXFORM(vmaxsw, 1, 6),
10689 GEN_VXFORM_207(vmaxsd, 1, 7),
10690 GEN_VXFORM(vminub, 1, 8),
10691 GEN_VXFORM(vminuh, 1, 9),
10692 GEN_VXFORM(vminuw, 1, 10),
10693 GEN_VXFORM_207(vminud, 1, 11),
10694 GEN_VXFORM(vminsb, 1, 12),
10695 GEN_VXFORM(vminsh, 1, 13),
10696 GEN_VXFORM(vminsw, 1, 14),
10697 GEN_VXFORM_207(vminsd, 1, 15),
10698 GEN_VXFORM(vavgub, 1, 16),
10699 GEN_VXFORM(vavguh, 1, 17),
10700 GEN_VXFORM(vavguw, 1, 18),
10701 GEN_VXFORM(vavgsb, 1, 20),
10702 GEN_VXFORM(vavgsh, 1, 21),
10703 GEN_VXFORM(vavgsw, 1, 22),
10704 GEN_VXFORM(vmrghb, 6, 0),
10705 GEN_VXFORM(vmrghh, 6, 1),
10706 GEN_VXFORM(vmrghw, 6, 2),
10707 GEN_VXFORM(vmrglb, 6, 4),
10708 GEN_VXFORM(vmrglh, 6, 5),
10709 GEN_VXFORM(vmrglw, 6, 6),
10710 GEN_VXFORM_207(vmrgew, 6, 30),
10711 GEN_VXFORM_207(vmrgow, 6, 26),
10712 GEN_VXFORM(vmuloub, 4, 0),
10713 GEN_VXFORM(vmulouh, 4, 1),
10714 GEN_VXFORM_DUAL(vmulouw, vmuluwm, 4, 2, PPC_ALTIVEC, PPC_NONE),
10715 GEN_VXFORM(vmulosb, 4, 4),
10716 GEN_VXFORM(vmulosh, 4, 5),
10717 GEN_VXFORM_207(vmulosw, 4, 6),
10718 GEN_VXFORM(vmuleub, 4, 8),
10719 GEN_VXFORM(vmuleuh, 4, 9),
10720 GEN_VXFORM_207(vmuleuw, 4, 10),
10721 GEN_VXFORM(vmulesb, 4, 12),
10722 GEN_VXFORM(vmulesh, 4, 13),
10723 GEN_VXFORM_207(vmulesw, 4, 14),
10724 GEN_VXFORM(vslb, 2, 4),
10725 GEN_VXFORM(vslh, 2, 5),
10726 GEN_VXFORM(vslw, 2, 6),
10727 GEN_VXFORM_207(vsld, 2, 23),
10728 GEN_VXFORM(vsrb, 2, 8),
10729 GEN_VXFORM(vsrh, 2, 9),
10730 GEN_VXFORM(vsrw, 2, 10),
10731 GEN_VXFORM_207(vsrd, 2, 27),
10732 GEN_VXFORM(vsrab, 2, 12),
10733 GEN_VXFORM(vsrah, 2, 13),
10734 GEN_VXFORM(vsraw, 2, 14),
10735 GEN_VXFORM_207(vsrad, 2, 15),
10736 GEN_VXFORM(vslo, 6, 16),
10737 GEN_VXFORM(vsro, 6, 17),
10738 GEN_VXFORM(vaddcuw, 0, 6),
10739 GEN_VXFORM(vsubcuw, 0, 22),
10740 GEN_VXFORM(vaddubs, 0, 8),
10741 GEN_VXFORM(vadduhs, 0, 9),
10742 GEN_VXFORM(vadduws, 0, 10),
10743 GEN_VXFORM(vaddsbs, 0, 12),
10744 GEN_VXFORM(vaddshs, 0, 13),
10745 GEN_VXFORM(vaddsws, 0, 14),
10746 GEN_VXFORM_DUAL(vsububs, bcdadd, 0, 24, PPC_ALTIVEC, PPC_NONE),
10747 GEN_VXFORM_DUAL(vsubuhs, bcdsub, 0, 25, PPC_ALTIVEC, PPC_NONE),
10748 GEN_VXFORM(vsubuws, 0, 26),
10749 GEN_VXFORM(vsubsbs, 0, 28),
10750 GEN_VXFORM(vsubshs, 0, 29),
10751 GEN_VXFORM(vsubsws, 0, 30),
10752 GEN_VXFORM_207(vadduqm, 0, 4),
10753 GEN_VXFORM_207(vaddcuq, 0, 5),
10754 GEN_VXFORM_DUAL(vaddeuqm, vaddecuq, 30, 0xFF, PPC_NONE, PPC2_ALTIVEC_207),
10755 GEN_VXFORM_207(vsubuqm, 0, 20),
10756 GEN_VXFORM_207(vsubcuq, 0, 21),
10757 GEN_VXFORM_DUAL(vsubeuqm, vsubecuq, 31, 0xFF, PPC_NONE, PPC2_ALTIVEC_207),
10758 GEN_VXFORM(vrlb, 2, 0),
10759 GEN_VXFORM(vrlh, 2, 1),
10760 GEN_VXFORM(vrlw, 2, 2),
10761 GEN_VXFORM_207(vrld, 2, 3),
10762 GEN_VXFORM(vsl, 2, 7),
10763 GEN_VXFORM(vsr, 2, 11),
10764 GEN_VXFORM(vpkuhum, 7, 0),
10765 GEN_VXFORM(vpkuwum, 7, 1),
10766 GEN_VXFORM_207(vpkudum, 7, 17),
10767 GEN_VXFORM(vpkuhus, 7, 2),
10768 GEN_VXFORM(vpkuwus, 7, 3),
10769 GEN_VXFORM_207(vpkudus, 7, 19),
10770 GEN_VXFORM(vpkshus, 7, 4),
10771 GEN_VXFORM(vpkswus, 7, 5),
10772 GEN_VXFORM_207(vpksdus, 7, 21),
10773 GEN_VXFORM(vpkshss, 7, 6),
10774 GEN_VXFORM(vpkswss, 7, 7),
10775 GEN_VXFORM_207(vpksdss, 7, 23),
10776 GEN_VXFORM(vpkpx, 7, 12),
10777 GEN_VXFORM(vsum4ubs, 4, 24),
10778 GEN_VXFORM(vsum4sbs, 4, 28),
10779 GEN_VXFORM(vsum4shs, 4, 25),
10780 GEN_VXFORM(vsum2sws, 4, 26),
10781 GEN_VXFORM(vsumsws, 4, 30),
10782 GEN_VXFORM(vaddfp, 5, 0),
10783 GEN_VXFORM(vsubfp, 5, 1),
10784 GEN_VXFORM(vmaxfp, 5, 16),
10785 GEN_VXFORM(vminfp, 5, 17),
10786
10787 #undef GEN_VXRFORM1
10788 #undef GEN_VXRFORM
10789 #define GEN_VXRFORM1(opname, name, str, opc2, opc3)                     \
10790     GEN_HANDLER2(name, str, 0x4, opc2, opc3, 0x00000000, PPC_ALTIVEC),
10791 #define GEN_VXRFORM(name, opc2, opc3)                                \
10792     GEN_VXRFORM1(name, name, #name, opc2, opc3)                      \
10793     GEN_VXRFORM1(name##_dot, name##_, #name ".", opc2, (opc3 | (0x1 << 4)))
10794 GEN_VXRFORM(vcmpequb, 3, 0)
10795 GEN_VXRFORM(vcmpequh, 3, 1)
10796 GEN_VXRFORM(vcmpequw, 3, 2)
10797 GEN_VXRFORM(vcmpgtsb, 3, 12)
10798 GEN_VXRFORM(vcmpgtsh, 3, 13)
10799 GEN_VXRFORM(vcmpgtsw, 3, 14)
10800 GEN_VXRFORM(vcmpgtub, 3, 8)
10801 GEN_VXRFORM(vcmpgtuh, 3, 9)
10802 GEN_VXRFORM(vcmpgtuw, 3, 10)
10803 GEN_VXRFORM_DUAL(vcmpeqfp, vcmpequd, 3, 3, PPC_ALTIVEC, PPC_NONE)
10804 GEN_VXRFORM(vcmpgefp, 3, 7)
10805 GEN_VXRFORM_DUAL(vcmpgtfp, vcmpgtud, 3, 11, PPC_ALTIVEC, PPC_NONE)
10806 GEN_VXRFORM_DUAL(vcmpbfp, vcmpgtsd, 3, 15, PPC_ALTIVEC, PPC_NONE)
10807
10808 #undef GEN_VXFORM_SIMM
10809 #define GEN_VXFORM_SIMM(name, opc2, opc3)                               \
10810     GEN_HANDLER(name, 0x04, opc2, opc3, 0x00000000, PPC_ALTIVEC)
10811 GEN_VXFORM_SIMM(vspltisb, 6, 12),
10812 GEN_VXFORM_SIMM(vspltish, 6, 13),
10813 GEN_VXFORM_SIMM(vspltisw, 6, 14),
10814
10815 #undef GEN_VXFORM_NOA
10816 #define GEN_VXFORM_NOA(name, opc2, opc3)                                \
10817     GEN_HANDLER(name, 0x04, opc2, opc3, 0x001f0000, PPC_ALTIVEC)
10818 GEN_VXFORM_NOA(vupkhsb, 7, 8),
10819 GEN_VXFORM_NOA(vupkhsh, 7, 9),
10820 GEN_VXFORM_207(vupkhsw, 7, 25),
10821 GEN_VXFORM_NOA(vupklsb, 7, 10),
10822 GEN_VXFORM_NOA(vupklsh, 7, 11),
10823 GEN_VXFORM_207(vupklsw, 7, 27),
10824 GEN_VXFORM_NOA(vupkhpx, 7, 13),
10825 GEN_VXFORM_NOA(vupklpx, 7, 15),
10826 GEN_VXFORM_NOA(vrefp, 5, 4),
10827 GEN_VXFORM_NOA(vrsqrtefp, 5, 5),
10828 GEN_VXFORM_NOA(vexptefp, 5, 6),
10829 GEN_VXFORM_NOA(vlogefp, 5, 7),
10830 GEN_VXFORM_NOA(vrfim, 5, 11),
10831 GEN_VXFORM_NOA(vrfin, 5, 8),
10832 GEN_VXFORM_NOA(vrfip, 5, 10),
10833 GEN_VXFORM_NOA(vrfiz, 5, 9),
10834
10835 #undef GEN_VXFORM_UIMM
10836 #define GEN_VXFORM_UIMM(name, opc2, opc3)                               \
10837     GEN_HANDLER(name, 0x04, opc2, opc3, 0x00000000, PPC_ALTIVEC)
10838 GEN_VXFORM_UIMM(vspltb, 6, 8),
10839 GEN_VXFORM_UIMM(vsplth, 6, 9),
10840 GEN_VXFORM_UIMM(vspltw, 6, 10),
10841 GEN_VXFORM_UIMM(vcfux, 5, 12),
10842 GEN_VXFORM_UIMM(vcfsx, 5, 13),
10843 GEN_VXFORM_UIMM(vctuxs, 5, 14),
10844 GEN_VXFORM_UIMM(vctsxs, 5, 15),
10845
10846 #undef GEN_VAFORM_PAIRED
10847 #define GEN_VAFORM_PAIRED(name0, name1, opc2)                           \
10848     GEN_HANDLER(name0##_##name1, 0x04, opc2, 0xFF, 0x00000000, PPC_ALTIVEC)
10849 GEN_VAFORM_PAIRED(vmhaddshs, vmhraddshs, 16),
10850 GEN_VAFORM_PAIRED(vmsumubm, vmsummbm, 18),
10851 GEN_VAFORM_PAIRED(vmsumuhm, vmsumuhs, 19),
10852 GEN_VAFORM_PAIRED(vmsumshm, vmsumshs, 20),
10853 GEN_VAFORM_PAIRED(vsel, vperm, 21),
10854 GEN_VAFORM_PAIRED(vmaddfp, vnmsubfp, 23),
10855
10856 GEN_VXFORM_DUAL(vclzb, vpopcntb, 1, 28, PPC_NONE, PPC2_ALTIVEC_207),
10857 GEN_VXFORM_DUAL(vclzh, vpopcnth, 1, 29, PPC_NONE, PPC2_ALTIVEC_207),
10858 GEN_VXFORM_DUAL(vclzw, vpopcntw, 1, 30, PPC_NONE, PPC2_ALTIVEC_207),
10859 GEN_VXFORM_DUAL(vclzd, vpopcntd, 1, 31, PPC_NONE, PPC2_ALTIVEC_207),
10860
10861 GEN_VXFORM_207(vbpermq, 6, 21),
10862 GEN_VXFORM_207(vgbbd, 6, 20),
10863 GEN_VXFORM_207(vpmsumb, 4, 16),
10864 GEN_VXFORM_207(vpmsumh, 4, 17),
10865 GEN_VXFORM_207(vpmsumw, 4, 18),
10866 GEN_VXFORM_207(vpmsumd, 4, 19),
10867
10868 GEN_VXFORM_207(vsbox, 4, 23),
10869
10870 GEN_VXFORM_DUAL(vcipher, vcipherlast, 4, 20, PPC_NONE, PPC2_ALTIVEC_207),
10871 GEN_VXFORM_DUAL(vncipher, vncipherlast, 4, 21, PPC_NONE, PPC2_ALTIVEC_207),
10872
10873 GEN_VXFORM_207(vshasigmaw, 1, 26),
10874 GEN_VXFORM_207(vshasigmad, 1, 27),
10875
10876 GEN_VXFORM_DUAL(vsldoi, vpermxor, 22, 0xFF, PPC_ALTIVEC, PPC_NONE),
10877
10878 GEN_HANDLER_E(lxsdx, 0x1F, 0x0C, 0x12, 0, PPC_NONE, PPC2_VSX),
10879 GEN_HANDLER_E(lxsiwax, 0x1F, 0x0C, 0x02, 0, PPC_NONE, PPC2_VSX207),
10880 GEN_HANDLER_E(lxsiwzx, 0x1F, 0x0C, 0x00, 0, PPC_NONE, PPC2_VSX207),
10881 GEN_HANDLER_E(lxsspx, 0x1F, 0x0C, 0x10, 0, PPC_NONE, PPC2_VSX207),
10882 GEN_HANDLER_E(lxvd2x, 0x1F, 0x0C, 0x1A, 0, PPC_NONE, PPC2_VSX),
10883 GEN_HANDLER_E(lxvdsx, 0x1F, 0x0C, 0x0A, 0, PPC_NONE, PPC2_VSX),
10884 GEN_HANDLER_E(lxvw4x, 0x1F, 0x0C, 0x18, 0, PPC_NONE, PPC2_VSX),
10885
10886 GEN_HANDLER_E(stxsdx, 0x1F, 0xC, 0x16, 0, PPC_NONE, PPC2_VSX),
10887 GEN_HANDLER_E(stxsiwx, 0x1F, 0xC, 0x04, 0, PPC_NONE, PPC2_VSX207),
10888 GEN_HANDLER_E(stxsspx, 0x1F, 0xC, 0x14, 0, PPC_NONE, PPC2_VSX207),
10889 GEN_HANDLER_E(stxvd2x, 0x1F, 0xC, 0x1E, 0, PPC_NONE, PPC2_VSX),
10890 GEN_HANDLER_E(stxvw4x, 0x1F, 0xC, 0x1C, 0, PPC_NONE, PPC2_VSX),
10891
10892 GEN_HANDLER_E(mfvsrwz, 0x1F, 0x13, 0x03, 0x0000F800, PPC_NONE, PPC2_VSX207),
10893 GEN_HANDLER_E(mtvsrwa, 0x1F, 0x13, 0x06, 0x0000F800, PPC_NONE, PPC2_VSX207),
10894 GEN_HANDLER_E(mtvsrwz, 0x1F, 0x13, 0x07, 0x0000F800, PPC_NONE, PPC2_VSX207),
10895 #if defined(TARGET_PPC64)
10896 GEN_HANDLER_E(mfvsrd, 0x1F, 0x13, 0x01, 0x0000F800, PPC_NONE, PPC2_VSX207),
10897 GEN_HANDLER_E(mtvsrd, 0x1F, 0x13, 0x05, 0x0000F800, PPC_NONE, PPC2_VSX207),
10898 #endif
10899
10900 #undef GEN_XX2FORM
10901 #define GEN_XX2FORM(name, opc2, opc3, fl2)                           \
10902 GEN_HANDLER2_E(name, #name, 0x3C, opc2 | 0, opc3, 0, PPC_NONE, fl2), \
10903 GEN_HANDLER2_E(name, #name, 0x3C, opc2 | 1, opc3, 0, PPC_NONE, fl2)
10904
10905 #undef GEN_XX3FORM
10906 #define GEN_XX3FORM(name, opc2, opc3, fl2)                           \
10907 GEN_HANDLER2_E(name, #name, 0x3C, opc2 | 0, opc3, 0, PPC_NONE, fl2), \
10908 GEN_HANDLER2_E(name, #name, 0x3C, opc2 | 1, opc3, 0, PPC_NONE, fl2), \
10909 GEN_HANDLER2_E(name, #name, 0x3C, opc2 | 2, opc3, 0, PPC_NONE, fl2), \
10910 GEN_HANDLER2_E(name, #name, 0x3C, opc2 | 3, opc3, 0, PPC_NONE, fl2)
10911
10912 #undef GEN_XX2IFORM
10913 #define GEN_XX2IFORM(name, opc2, opc3, fl2)                           \
10914 GEN_HANDLER2_E(name, #name, 0x3C, opc2 | 0, opc3, 1, PPC_NONE, fl2), \
10915 GEN_HANDLER2_E(name, #name, 0x3C, opc2 | 1, opc3, 1, PPC_NONE, fl2), \
10916 GEN_HANDLER2_E(name, #name, 0x3C, opc2 | 2, opc3, 1, PPC_NONE, fl2), \
10917 GEN_HANDLER2_E(name, #name, 0x3C, opc2 | 3, opc3, 1, PPC_NONE, fl2)
10918
10919 #undef GEN_XX3_RC_FORM
10920 #define GEN_XX3_RC_FORM(name, opc2, opc3, fl2)                          \
10921 GEN_HANDLER2_E(name, #name, 0x3C, opc2 | 0x00, opc3 | 0x00, 0, PPC_NONE, fl2), \
10922 GEN_HANDLER2_E(name, #name, 0x3C, opc2 | 0x01, opc3 | 0x00, 0, PPC_NONE, fl2), \
10923 GEN_HANDLER2_E(name, #name, 0x3C, opc2 | 0x02, opc3 | 0x00, 0, PPC_NONE, fl2), \
10924 GEN_HANDLER2_E(name, #name, 0x3C, opc2 | 0x03, opc3 | 0x00, 0, PPC_NONE, fl2), \
10925 GEN_HANDLER2_E(name, #name, 0x3C, opc2 | 0x00, opc3 | 0x10, 0, PPC_NONE, fl2), \
10926 GEN_HANDLER2_E(name, #name, 0x3C, opc2 | 0x01, opc3 | 0x10, 0, PPC_NONE, fl2), \
10927 GEN_HANDLER2_E(name, #name, 0x3C, opc2 | 0x02, opc3 | 0x10, 0, PPC_NONE, fl2), \
10928 GEN_HANDLER2_E(name, #name, 0x3C, opc2 | 0x03, opc3 | 0x10, 0, PPC_NONE, fl2)
10929
10930 #undef GEN_XX3FORM_DM
10931 #define GEN_XX3FORM_DM(name, opc2, opc3) \
10932 GEN_HANDLER2_E(name, #name, 0x3C, opc2|0x00, opc3|0x00, 0, PPC_NONE, PPC2_VSX),\
10933 GEN_HANDLER2_E(name, #name, 0x3C, opc2|0x01, opc3|0x00, 0, PPC_NONE, PPC2_VSX),\
10934 GEN_HANDLER2_E(name, #name, 0x3C, opc2|0x02, opc3|0x00, 0, PPC_NONE, PPC2_VSX),\
10935 GEN_HANDLER2_E(name, #name, 0x3C, opc2|0x03, opc3|0x00, 0, PPC_NONE, PPC2_VSX),\
10936 GEN_HANDLER2_E(name, #name, 0x3C, opc2|0x00, opc3|0x04, 0, PPC_NONE, PPC2_VSX),\
10937 GEN_HANDLER2_E(name, #name, 0x3C, opc2|0x01, opc3|0x04, 0, PPC_NONE, PPC2_VSX),\
10938 GEN_HANDLER2_E(name, #name, 0x3C, opc2|0x02, opc3|0x04, 0, PPC_NONE, PPC2_VSX),\
10939 GEN_HANDLER2_E(name, #name, 0x3C, opc2|0x03, opc3|0x04, 0, PPC_NONE, PPC2_VSX),\
10940 GEN_HANDLER2_E(name, #name, 0x3C, opc2|0x00, opc3|0x08, 0, PPC_NONE, PPC2_VSX),\
10941 GEN_HANDLER2_E(name, #name, 0x3C, opc2|0x01, opc3|0x08, 0, PPC_NONE, PPC2_VSX),\
10942 GEN_HANDLER2_E(name, #name, 0x3C, opc2|0x02, opc3|0x08, 0, PPC_NONE, PPC2_VSX),\
10943 GEN_HANDLER2_E(name, #name, 0x3C, opc2|0x03, opc3|0x08, 0, PPC_NONE, PPC2_VSX),\
10944 GEN_HANDLER2_E(name, #name, 0x3C, opc2|0x00, opc3|0x0C, 0, PPC_NONE, PPC2_VSX),\
10945 GEN_HANDLER2_E(name, #name, 0x3C, opc2|0x01, opc3|0x0C, 0, PPC_NONE, PPC2_VSX),\
10946 GEN_HANDLER2_E(name, #name, 0x3C, opc2|0x02, opc3|0x0C, 0, PPC_NONE, PPC2_VSX),\
10947 GEN_HANDLER2_E(name, #name, 0x3C, opc2|0x03, opc3|0x0C, 0, PPC_NONE, PPC2_VSX)
10948
10949 GEN_XX2FORM(xsabsdp, 0x12, 0x15, PPC2_VSX),
10950 GEN_XX2FORM(xsnabsdp, 0x12, 0x16, PPC2_VSX),
10951 GEN_XX2FORM(xsnegdp, 0x12, 0x17, PPC2_VSX),
10952 GEN_XX3FORM(xscpsgndp, 0x00, 0x16, PPC2_VSX),
10953
10954 GEN_XX2FORM(xvabsdp, 0x12, 0x1D, PPC2_VSX),
10955 GEN_XX2FORM(xvnabsdp, 0x12, 0x1E, PPC2_VSX),
10956 GEN_XX2FORM(xvnegdp, 0x12, 0x1F, PPC2_VSX),
10957 GEN_XX3FORM(xvcpsgndp, 0x00, 0x1E, PPC2_VSX),
10958 GEN_XX2FORM(xvabssp, 0x12, 0x19, PPC2_VSX),
10959 GEN_XX2FORM(xvnabssp, 0x12, 0x1A, PPC2_VSX),
10960 GEN_XX2FORM(xvnegsp, 0x12, 0x1B, PPC2_VSX),
10961 GEN_XX3FORM(xvcpsgnsp, 0x00, 0x1A, PPC2_VSX),
10962
10963 GEN_XX3FORM(xsadddp, 0x00, 0x04, PPC2_VSX),
10964 GEN_XX3FORM(xssubdp, 0x00, 0x05, PPC2_VSX),
10965 GEN_XX3FORM(xsmuldp, 0x00, 0x06, PPC2_VSX),
10966 GEN_XX3FORM(xsdivdp, 0x00, 0x07, PPC2_VSX),
10967 GEN_XX2FORM(xsredp,  0x14, 0x05, PPC2_VSX),
10968 GEN_XX2FORM(xssqrtdp,  0x16, 0x04, PPC2_VSX),
10969 GEN_XX2FORM(xsrsqrtedp,  0x14, 0x04, PPC2_VSX),
10970 GEN_XX3FORM(xstdivdp,  0x14, 0x07, PPC2_VSX),
10971 GEN_XX2FORM(xstsqrtdp,  0x14, 0x06, PPC2_VSX),
10972 GEN_XX3FORM(xsmaddadp, 0x04, 0x04, PPC2_VSX),
10973 GEN_XX3FORM(xsmaddmdp, 0x04, 0x05, PPC2_VSX),
10974 GEN_XX3FORM(xsmsubadp, 0x04, 0x06, PPC2_VSX),
10975 GEN_XX3FORM(xsmsubmdp, 0x04, 0x07, PPC2_VSX),
10976 GEN_XX3FORM(xsnmaddadp, 0x04, 0x14, PPC2_VSX),
10977 GEN_XX3FORM(xsnmaddmdp, 0x04, 0x15, PPC2_VSX),
10978 GEN_XX3FORM(xsnmsubadp, 0x04, 0x16, PPC2_VSX),
10979 GEN_XX3FORM(xsnmsubmdp, 0x04, 0x17, PPC2_VSX),
10980 GEN_XX2IFORM(xscmpodp,  0x0C, 0x05, PPC2_VSX),
10981 GEN_XX2IFORM(xscmpudp,  0x0C, 0x04, PPC2_VSX),
10982 GEN_XX3FORM(xsmaxdp, 0x00, 0x14, PPC2_VSX),
10983 GEN_XX3FORM(xsmindp, 0x00, 0x15, PPC2_VSX),
10984 GEN_XX2FORM(xscvdpsp, 0x12, 0x10, PPC2_VSX),
10985 GEN_XX2FORM(xscvdpspn, 0x16, 0x10, PPC2_VSX207),
10986 GEN_XX2FORM(xscvspdp, 0x12, 0x14, PPC2_VSX),
10987 GEN_XX2FORM(xscvspdpn, 0x16, 0x14, PPC2_VSX207),
10988 GEN_XX2FORM(xscvdpsxds, 0x10, 0x15, PPC2_VSX),
10989 GEN_XX2FORM(xscvdpsxws, 0x10, 0x05, PPC2_VSX),
10990 GEN_XX2FORM(xscvdpuxds, 0x10, 0x14, PPC2_VSX),
10991 GEN_XX2FORM(xscvdpuxws, 0x10, 0x04, PPC2_VSX),
10992 GEN_XX2FORM(xscvsxddp, 0x10, 0x17, PPC2_VSX),
10993 GEN_XX2FORM(xscvuxddp, 0x10, 0x16, PPC2_VSX),
10994 GEN_XX2FORM(xsrdpi, 0x12, 0x04, PPC2_VSX),
10995 GEN_XX2FORM(xsrdpic, 0x16, 0x06, PPC2_VSX),
10996 GEN_XX2FORM(xsrdpim, 0x12, 0x07, PPC2_VSX),
10997 GEN_XX2FORM(xsrdpip, 0x12, 0x06, PPC2_VSX),
10998 GEN_XX2FORM(xsrdpiz, 0x12, 0x05, PPC2_VSX),
10999
11000 GEN_XX3FORM(xsaddsp, 0x00, 0x00, PPC2_VSX207),
11001 GEN_XX3FORM(xssubsp, 0x00, 0x01, PPC2_VSX207),
11002 GEN_XX3FORM(xsmulsp, 0x00, 0x02, PPC2_VSX207),
11003 GEN_XX3FORM(xsdivsp, 0x00, 0x03, PPC2_VSX207),
11004 GEN_XX2FORM(xsresp,  0x14, 0x01, PPC2_VSX207),
11005 GEN_XX2FORM(xsrsp, 0x12, 0x11, PPC2_VSX207),
11006 GEN_XX2FORM(xssqrtsp,  0x16, 0x00, PPC2_VSX207),
11007 GEN_XX2FORM(xsrsqrtesp,  0x14, 0x00, PPC2_VSX207),
11008 GEN_XX3FORM(xsmaddasp, 0x04, 0x00, PPC2_VSX207),
11009 GEN_XX3FORM(xsmaddmsp, 0x04, 0x01, PPC2_VSX207),
11010 GEN_XX3FORM(xsmsubasp, 0x04, 0x02, PPC2_VSX207),
11011 GEN_XX3FORM(xsmsubmsp, 0x04, 0x03, PPC2_VSX207),
11012 GEN_XX3FORM(xsnmaddasp, 0x04, 0x10, PPC2_VSX207),
11013 GEN_XX3FORM(xsnmaddmsp, 0x04, 0x11, PPC2_VSX207),
11014 GEN_XX3FORM(xsnmsubasp, 0x04, 0x12, PPC2_VSX207),
11015 GEN_XX3FORM(xsnmsubmsp, 0x04, 0x13, PPC2_VSX207),
11016 GEN_XX2FORM(xscvsxdsp, 0x10, 0x13, PPC2_VSX207),
11017 GEN_XX2FORM(xscvuxdsp, 0x10, 0x12, PPC2_VSX207),
11018
11019 GEN_XX3FORM(xvadddp, 0x00, 0x0C, PPC2_VSX),
11020 GEN_XX3FORM(xvsubdp, 0x00, 0x0D, PPC2_VSX),
11021 GEN_XX3FORM(xvmuldp, 0x00, 0x0E, PPC2_VSX),
11022 GEN_XX3FORM(xvdivdp, 0x00, 0x0F, PPC2_VSX),
11023 GEN_XX2FORM(xvredp,  0x14, 0x0D, PPC2_VSX),
11024 GEN_XX2FORM(xvsqrtdp,  0x16, 0x0C, PPC2_VSX),
11025 GEN_XX2FORM(xvrsqrtedp,  0x14, 0x0C, PPC2_VSX),
11026 GEN_XX3FORM(xvtdivdp, 0x14, 0x0F, PPC2_VSX),
11027 GEN_XX2FORM(xvtsqrtdp, 0x14, 0x0E, PPC2_VSX),
11028 GEN_XX3FORM(xvmaddadp, 0x04, 0x0C, PPC2_VSX),
11029 GEN_XX3FORM(xvmaddmdp, 0x04, 0x0D, PPC2_VSX),
11030 GEN_XX3FORM(xvmsubadp, 0x04, 0x0E, PPC2_VSX),
11031 GEN_XX3FORM(xvmsubmdp, 0x04, 0x0F, PPC2_VSX),
11032 GEN_XX3FORM(xvnmaddadp, 0x04, 0x1C, PPC2_VSX),
11033 GEN_XX3FORM(xvnmaddmdp, 0x04, 0x1D, PPC2_VSX),
11034 GEN_XX3FORM(xvnmsubadp, 0x04, 0x1E, PPC2_VSX),
11035 GEN_XX3FORM(xvnmsubmdp, 0x04, 0x1F, PPC2_VSX),
11036 GEN_XX3FORM(xvmaxdp, 0x00, 0x1C, PPC2_VSX),
11037 GEN_XX3FORM(xvmindp, 0x00, 0x1D, PPC2_VSX),
11038 GEN_XX3_RC_FORM(xvcmpeqdp, 0x0C, 0x0C, PPC2_VSX),
11039 GEN_XX3_RC_FORM(xvcmpgtdp, 0x0C, 0x0D, PPC2_VSX),
11040 GEN_XX3_RC_FORM(xvcmpgedp, 0x0C, 0x0E, PPC2_VSX),
11041 GEN_XX2FORM(xvcvdpsp, 0x12, 0x18, PPC2_VSX),
11042 GEN_XX2FORM(xvcvdpsxds, 0x10, 0x1D, PPC2_VSX),
11043 GEN_XX2FORM(xvcvdpsxws, 0x10, 0x0D, PPC2_VSX),
11044 GEN_XX2FORM(xvcvdpuxds, 0x10, 0x1C, PPC2_VSX),
11045 GEN_XX2FORM(xvcvdpuxws, 0x10, 0x0C, PPC2_VSX),
11046 GEN_XX2FORM(xvcvsxddp, 0x10, 0x1F, PPC2_VSX),
11047 GEN_XX2FORM(xvcvuxddp, 0x10, 0x1E, PPC2_VSX),
11048 GEN_XX2FORM(xvcvsxwdp, 0x10, 0x0F, PPC2_VSX),
11049 GEN_XX2FORM(xvcvuxwdp, 0x10, 0x0E, PPC2_VSX),
11050 GEN_XX2FORM(xvrdpi, 0x12, 0x0C, PPC2_VSX),
11051 GEN_XX2FORM(xvrdpic, 0x16, 0x0E, PPC2_VSX),
11052 GEN_XX2FORM(xvrdpim, 0x12, 0x0F, PPC2_VSX),
11053 GEN_XX2FORM(xvrdpip, 0x12, 0x0E, PPC2_VSX),
11054 GEN_XX2FORM(xvrdpiz, 0x12, 0x0D, PPC2_VSX),
11055
11056 GEN_XX3FORM(xvaddsp, 0x00, 0x08, PPC2_VSX),
11057 GEN_XX3FORM(xvsubsp, 0x00, 0x09, PPC2_VSX),
11058 GEN_XX3FORM(xvmulsp, 0x00, 0x0A, PPC2_VSX),
11059 GEN_XX3FORM(xvdivsp, 0x00, 0x0B, PPC2_VSX),
11060 GEN_XX2FORM(xvresp, 0x14, 0x09, PPC2_VSX),
11061 GEN_XX2FORM(xvsqrtsp, 0x16, 0x08, PPC2_VSX),
11062 GEN_XX2FORM(xvrsqrtesp, 0x14, 0x08, PPC2_VSX),
11063 GEN_XX3FORM(xvtdivsp, 0x14, 0x0B, PPC2_VSX),
11064 GEN_XX2FORM(xvtsqrtsp, 0x14, 0x0A, PPC2_VSX),
11065 GEN_XX3FORM(xvmaddasp, 0x04, 0x08, PPC2_VSX),
11066 GEN_XX3FORM(xvmaddmsp, 0x04, 0x09, PPC2_VSX),
11067 GEN_XX3FORM(xvmsubasp, 0x04, 0x0A, PPC2_VSX),
11068 GEN_XX3FORM(xvmsubmsp, 0x04, 0x0B, PPC2_VSX),
11069 GEN_XX3FORM(xvnmaddasp, 0x04, 0x18, PPC2_VSX),
11070 GEN_XX3FORM(xvnmaddmsp, 0x04, 0x19, PPC2_VSX),
11071 GEN_XX3FORM(xvnmsubasp, 0x04, 0x1A, PPC2_VSX),
11072 GEN_XX3FORM(xvnmsubmsp, 0x04, 0x1B, PPC2_VSX),
11073 GEN_XX3FORM(xvmaxsp, 0x00, 0x18, PPC2_VSX),
11074 GEN_XX3FORM(xvminsp, 0x00, 0x19, PPC2_VSX),
11075 GEN_XX3_RC_FORM(xvcmpeqsp, 0x0C, 0x08, PPC2_VSX),
11076 GEN_XX3_RC_FORM(xvcmpgtsp, 0x0C, 0x09, PPC2_VSX),
11077 GEN_XX3_RC_FORM(xvcmpgesp, 0x0C, 0x0A, PPC2_VSX),
11078 GEN_XX2FORM(xvcvspdp, 0x12, 0x1C, PPC2_VSX),
11079 GEN_XX2FORM(xvcvspsxds, 0x10, 0x19, PPC2_VSX),
11080 GEN_XX2FORM(xvcvspsxws, 0x10, 0x09, PPC2_VSX),
11081 GEN_XX2FORM(xvcvspuxds, 0x10, 0x18, PPC2_VSX),
11082 GEN_XX2FORM(xvcvspuxws, 0x10, 0x08, PPC2_VSX),
11083 GEN_XX2FORM(xvcvsxdsp, 0x10, 0x1B, PPC2_VSX),
11084 GEN_XX2FORM(xvcvuxdsp, 0x10, 0x1A, PPC2_VSX),
11085 GEN_XX2FORM(xvcvsxwsp, 0x10, 0x0B, PPC2_VSX),
11086 GEN_XX2FORM(xvcvuxwsp, 0x10, 0x0A, PPC2_VSX),
11087 GEN_XX2FORM(xvrspi, 0x12, 0x08, PPC2_VSX),
11088 GEN_XX2FORM(xvrspic, 0x16, 0x0A, PPC2_VSX),
11089 GEN_XX2FORM(xvrspim, 0x12, 0x0B, PPC2_VSX),
11090 GEN_XX2FORM(xvrspip, 0x12, 0x0A, PPC2_VSX),
11091 GEN_XX2FORM(xvrspiz, 0x12, 0x09, PPC2_VSX),
11092
11093 #undef VSX_LOGICAL
11094 #define VSX_LOGICAL(name, opc2, opc3, fl2) \
11095 GEN_XX3FORM(name, opc2, opc3, fl2)
11096
11097 VSX_LOGICAL(xxland, 0x8, 0x10, PPC2_VSX),
11098 VSX_LOGICAL(xxlandc, 0x8, 0x11, PPC2_VSX),
11099 VSX_LOGICAL(xxlor, 0x8, 0x12, PPC2_VSX),
11100 VSX_LOGICAL(xxlxor, 0x8, 0x13, PPC2_VSX),
11101 VSX_LOGICAL(xxlnor, 0x8, 0x14, PPC2_VSX),
11102 VSX_LOGICAL(xxleqv, 0x8, 0x17, PPC2_VSX207),
11103 VSX_LOGICAL(xxlnand, 0x8, 0x16, PPC2_VSX207),
11104 VSX_LOGICAL(xxlorc, 0x8, 0x15, PPC2_VSX207),
11105 GEN_XX3FORM(xxmrghw, 0x08, 0x02, PPC2_VSX),
11106 GEN_XX3FORM(xxmrglw, 0x08, 0x06, PPC2_VSX),
11107 GEN_XX2FORM(xxspltw, 0x08, 0x0A, PPC2_VSX),
11108 GEN_XX3FORM_DM(xxsldwi, 0x08, 0x00),
11109
11110 #define GEN_XXSEL_ROW(opc3) \
11111 GEN_HANDLER2_E(xxsel, "xxsel", 0x3C, 0x18, opc3, 0, PPC_NONE, PPC2_VSX), \
11112 GEN_HANDLER2_E(xxsel, "xxsel", 0x3C, 0x19, opc3, 0, PPC_NONE, PPC2_VSX), \
11113 GEN_HANDLER2_E(xxsel, "xxsel", 0x3C, 0x1A, opc3, 0, PPC_NONE, PPC2_VSX), \
11114 GEN_HANDLER2_E(xxsel, "xxsel", 0x3C, 0x1B, opc3, 0, PPC_NONE, PPC2_VSX), \
11115 GEN_HANDLER2_E(xxsel, "xxsel", 0x3C, 0x1C, opc3, 0, PPC_NONE, PPC2_VSX), \
11116 GEN_HANDLER2_E(xxsel, "xxsel", 0x3C, 0x1D, opc3, 0, PPC_NONE, PPC2_VSX), \
11117 GEN_HANDLER2_E(xxsel, "xxsel", 0x3C, 0x1E, opc3, 0, PPC_NONE, PPC2_VSX), \
11118 GEN_HANDLER2_E(xxsel, "xxsel", 0x3C, 0x1F, opc3, 0, PPC_NONE, PPC2_VSX), \
11119
11120 GEN_XXSEL_ROW(0x00)
11121 GEN_XXSEL_ROW(0x01)
11122 GEN_XXSEL_ROW(0x02)
11123 GEN_XXSEL_ROW(0x03)
11124 GEN_XXSEL_ROW(0x04)
11125 GEN_XXSEL_ROW(0x05)
11126 GEN_XXSEL_ROW(0x06)
11127 GEN_XXSEL_ROW(0x07)
11128 GEN_XXSEL_ROW(0x08)
11129 GEN_XXSEL_ROW(0x09)
11130 GEN_XXSEL_ROW(0x0A)
11131 GEN_XXSEL_ROW(0x0B)
11132 GEN_XXSEL_ROW(0x0C)
11133 GEN_XXSEL_ROW(0x0D)
11134 GEN_XXSEL_ROW(0x0E)
11135 GEN_XXSEL_ROW(0x0F)
11136 GEN_XXSEL_ROW(0x10)
11137 GEN_XXSEL_ROW(0x11)
11138 GEN_XXSEL_ROW(0x12)
11139 GEN_XXSEL_ROW(0x13)
11140 GEN_XXSEL_ROW(0x14)
11141 GEN_XXSEL_ROW(0x15)
11142 GEN_XXSEL_ROW(0x16)
11143 GEN_XXSEL_ROW(0x17)
11144 GEN_XXSEL_ROW(0x18)
11145 GEN_XXSEL_ROW(0x19)
11146 GEN_XXSEL_ROW(0x1A)
11147 GEN_XXSEL_ROW(0x1B)
11148 GEN_XXSEL_ROW(0x1C)
11149 GEN_XXSEL_ROW(0x1D)
11150 GEN_XXSEL_ROW(0x1E)
11151 GEN_XXSEL_ROW(0x1F)
11152
11153 GEN_XX3FORM_DM(xxpermdi, 0x08, 0x01),
11154
11155 #undef GEN_DFP_T_A_B_Rc
11156 #undef GEN_DFP_BF_A_B
11157 #undef GEN_DFP_BF_A_DCM
11158 #undef GEN_DFP_T_B_U32_U32_Rc
11159 #undef GEN_DFP_T_A_B_I32_Rc
11160 #undef GEN_DFP_T_B_Rc
11161 #undef GEN_DFP_T_FPR_I32_Rc
11162
11163 #define _GEN_DFP_LONG(name, op1, op2, mask) \
11164 GEN_HANDLER_E(name, 0x3B, op1, op2, mask, PPC_NONE, PPC2_DFP)
11165
11166 #define _GEN_DFP_LONGx2(name, op1, op2, mask) \
11167 GEN_HANDLER_E(name, 0x3B, op1, 0x00 | op2, mask, PPC_NONE, PPC2_DFP), \
11168 GEN_HANDLER_E(name, 0x3B, op1, 0x10 | op2, mask, PPC_NONE, PPC2_DFP)
11169
11170 #define _GEN_DFP_LONGx4(name, op1, op2, mask) \
11171 GEN_HANDLER_E(name, 0x3B, op1, 0x00 | op2, mask, PPC_NONE, PPC2_DFP), \
11172 GEN_HANDLER_E(name, 0x3B, op1, 0x08 | op2, mask, PPC_NONE, PPC2_DFP), \
11173 GEN_HANDLER_E(name, 0x3B, op1, 0x10 | op2, mask, PPC_NONE, PPC2_DFP), \
11174 GEN_HANDLER_E(name, 0x3B, op1, 0x18 | op2, mask, PPC_NONE, PPC2_DFP)
11175
11176 #define _GEN_DFP_QUAD(name, op1, op2, mask) \
11177 GEN_HANDLER_E(name, 0x3F, op1, op2, mask, PPC_NONE, PPC2_DFP)
11178
11179 #define _GEN_DFP_QUADx2(name, op1, op2, mask) \
11180 GEN_HANDLER_E(name, 0x3F, op1, 0x00 | op2, mask, PPC_NONE, PPC2_DFP), \
11181 GEN_HANDLER_E(name, 0x3F, op1, 0x10 | op2, mask, PPC_NONE, PPC2_DFP)
11182
11183 #define _GEN_DFP_QUADx4(name, op1, op2, mask)                         \
11184 GEN_HANDLER_E(name, 0x3F, op1, 0x00 | op2, mask, PPC_NONE, PPC2_DFP), \
11185 GEN_HANDLER_E(name, 0x3F, op1, 0x08 | op2, mask, PPC_NONE, PPC2_DFP), \
11186 GEN_HANDLER_E(name, 0x3F, op1, 0x10 | op2, mask, PPC_NONE, PPC2_DFP), \
11187 GEN_HANDLER_E(name, 0x3F, op1, 0x18 | op2, mask, PPC_NONE, PPC2_DFP)
11188
11189 #define GEN_DFP_T_A_B_Rc(name, op1, op2) \
11190 _GEN_DFP_LONG(name, op1, op2, 0x00000000)
11191
11192 #define GEN_DFP_Tp_Ap_Bp_Rc(name, op1, op2) \
11193 _GEN_DFP_QUAD(name, op1, op2, 0x00210800)
11194
11195 #define GEN_DFP_Tp_A_Bp_Rc(name, op1, op2) \
11196 _GEN_DFP_QUAD(name, op1, op2, 0x00200800)
11197
11198 #define GEN_DFP_T_B_Rc(name, op1, op2) \
11199 _GEN_DFP_LONG(name, op1, op2, 0x001F0000)
11200
11201 #define GEN_DFP_Tp_Bp_Rc(name, op1, op2) \
11202 _GEN_DFP_QUAD(name, op1, op2, 0x003F0800)
11203
11204 #define GEN_DFP_Tp_B_Rc(name, op1, op2) \
11205 _GEN_DFP_QUAD(name, op1, op2, 0x003F0000)
11206
11207 #define GEN_DFP_T_Bp_Rc(name, op1, op2) \
11208 _GEN_DFP_QUAD(name, op1, op2, 0x001F0800)
11209
11210 #define GEN_DFP_BF_A_B(name, op1, op2) \
11211 _GEN_DFP_LONG(name, op1, op2, 0x00000001)
11212
11213 #define GEN_DFP_BF_Ap_Bp(name, op1, op2) \
11214 _GEN_DFP_QUAD(name, op1, op2, 0x00610801)
11215
11216 #define GEN_DFP_BF_A_Bp(name, op1, op2) \
11217 _GEN_DFP_QUAD(name, op1, op2, 0x00600801)
11218
11219 #define GEN_DFP_BF_A_DCM(name, op1, op2) \
11220 _GEN_DFP_LONGx2(name, op1, op2, 0x00600001)
11221
11222 #define GEN_DFP_BF_Ap_DCM(name, op1, op2) \
11223 _GEN_DFP_QUADx2(name, op1, op2, 0x00610001)
11224
11225 #define GEN_DFP_T_A_B_RMC_Rc(name, op1, op2) \
11226 _GEN_DFP_LONGx4(name, op1, op2, 0x00000000)
11227
11228 #define GEN_DFP_Tp_Ap_Bp_RMC_Rc(name, op1, op2) \
11229 _GEN_DFP_QUADx4(name, op1, op2, 0x02010800)
11230
11231 #define GEN_DFP_Tp_A_Bp_RMC_Rc(name, op1, op2) \
11232 _GEN_DFP_QUADx4(name, op1, op2, 0x02000800)
11233
11234 #define GEN_DFP_TE_T_B_RMC_Rc(name, op1, op2) \
11235 _GEN_DFP_LONGx4(name, op1, op2, 0x00000000)
11236
11237 #define GEN_DFP_TE_Tp_Bp_RMC_Rc(name, op1, op2) \
11238 _GEN_DFP_QUADx4(name, op1, op2, 0x00200800)
11239
11240 #define GEN_DFP_R_T_B_RMC_Rc(name, op1, op2) \
11241 _GEN_DFP_LONGx4(name, op1, op2, 0x001E0000)
11242
11243 #define GEN_DFP_R_Tp_Bp_RMC_Rc(name, op1, op2) \
11244 _GEN_DFP_QUADx4(name, op1, op2, 0x003E0800)
11245
11246 #define GEN_DFP_SP_T_B_Rc(name, op1, op2) \
11247 _GEN_DFP_LONG(name, op1, op2, 0x00070000)
11248
11249 #define GEN_DFP_SP_Tp_Bp_Rc(name, op1, op2) \
11250 _GEN_DFP_QUAD(name, op1, op2, 0x00270800)
11251
11252 #define GEN_DFP_S_T_B_Rc(name, op1, op2) \
11253 _GEN_DFP_LONG(name, op1, op2, 0x000F0000)
11254
11255 #define GEN_DFP_S_Tp_Bp_Rc(name, op1, op2) \
11256 _GEN_DFP_QUAD(name, op1, op2, 0x002F0800)
11257
11258 #define GEN_DFP_T_A_SH_Rc(name, op1, op2) \
11259 _GEN_DFP_LONGx2(name, op1, op2, 0x00000000)
11260
11261 #define GEN_DFP_Tp_Ap_SH_Rc(name, op1, op2) \
11262 _GEN_DFP_QUADx2(name, op1, op2, 0x00210000)
11263
11264 GEN_DFP_T_A_B_Rc(dadd, 0x02, 0x00),
11265 GEN_DFP_Tp_Ap_Bp_Rc(daddq, 0x02, 0x00),
11266 GEN_DFP_T_A_B_Rc(dsub, 0x02, 0x10),
11267 GEN_DFP_Tp_Ap_Bp_Rc(dsubq, 0x02, 0x10),
11268 GEN_DFP_T_A_B_Rc(dmul, 0x02, 0x01),
11269 GEN_DFP_Tp_Ap_Bp_Rc(dmulq, 0x02, 0x01),
11270 GEN_DFP_T_A_B_Rc(ddiv, 0x02, 0x11),
11271 GEN_DFP_Tp_Ap_Bp_Rc(ddivq, 0x02, 0x11),
11272 GEN_DFP_BF_A_B(dcmpu, 0x02, 0x14),
11273 GEN_DFP_BF_Ap_Bp(dcmpuq, 0x02, 0x14),
11274 GEN_DFP_BF_A_B(dcmpo, 0x02, 0x04),
11275 GEN_DFP_BF_Ap_Bp(dcmpoq, 0x02, 0x04),
11276 GEN_DFP_BF_A_DCM(dtstdc, 0x02, 0x06),
11277 GEN_DFP_BF_Ap_DCM(dtstdcq, 0x02, 0x06),
11278 GEN_DFP_BF_A_DCM(dtstdg, 0x02, 0x07),
11279 GEN_DFP_BF_Ap_DCM(dtstdgq, 0x02, 0x07),
11280 GEN_DFP_BF_A_B(dtstex, 0x02, 0x05),
11281 GEN_DFP_BF_Ap_Bp(dtstexq, 0x02, 0x05),
11282 GEN_DFP_BF_A_B(dtstsf, 0x02, 0x15),
11283 GEN_DFP_BF_A_Bp(dtstsfq, 0x02, 0x15),
11284 GEN_DFP_TE_T_B_RMC_Rc(dquai, 0x03, 0x02),
11285 GEN_DFP_TE_Tp_Bp_RMC_Rc(dquaiq, 0x03, 0x02),
11286 GEN_DFP_T_A_B_RMC_Rc(dqua, 0x03, 0x00),
11287 GEN_DFP_Tp_Ap_Bp_RMC_Rc(dquaq, 0x03, 0x00),
11288 GEN_DFP_T_A_B_RMC_Rc(drrnd, 0x03, 0x01),
11289 GEN_DFP_Tp_A_Bp_RMC_Rc(drrndq, 0x03, 0x01),
11290 GEN_DFP_R_T_B_RMC_Rc(drintx, 0x03, 0x03),
11291 GEN_DFP_R_Tp_Bp_RMC_Rc(drintxq, 0x03, 0x03),
11292 GEN_DFP_R_T_B_RMC_Rc(drintn, 0x03, 0x07),
11293 GEN_DFP_R_Tp_Bp_RMC_Rc(drintnq, 0x03, 0x07),
11294 GEN_DFP_T_B_Rc(dctdp, 0x02, 0x08),
11295 GEN_DFP_Tp_B_Rc(dctqpq, 0x02, 0x08),
11296 GEN_DFP_T_B_Rc(drsp, 0x02, 0x18),
11297 GEN_DFP_Tp_Bp_Rc(drdpq, 0x02, 0x18),
11298 GEN_DFP_T_B_Rc(dcffix, 0x02, 0x19),
11299 GEN_DFP_Tp_B_Rc(dcffixq, 0x02, 0x19),
11300 GEN_DFP_T_B_Rc(dctfix, 0x02, 0x09),
11301 GEN_DFP_T_Bp_Rc(dctfixq, 0x02, 0x09),
11302 GEN_DFP_SP_T_B_Rc(ddedpd, 0x02, 0x0a),
11303 GEN_DFP_SP_Tp_Bp_Rc(ddedpdq, 0x02, 0x0a),
11304 GEN_DFP_S_T_B_Rc(denbcd, 0x02, 0x1a),
11305 GEN_DFP_S_Tp_Bp_Rc(denbcdq, 0x02, 0x1a),
11306 GEN_DFP_T_B_Rc(dxex, 0x02, 0x0b),
11307 GEN_DFP_T_Bp_Rc(dxexq, 0x02, 0x0b),
11308 GEN_DFP_T_A_B_Rc(diex, 0x02, 0x1b),
11309 GEN_DFP_Tp_A_Bp_Rc(diexq, 0x02, 0x1b),
11310 GEN_DFP_T_A_SH_Rc(dscli, 0x02, 0x02),
11311 GEN_DFP_Tp_Ap_SH_Rc(dscliq, 0x02, 0x02),
11312 GEN_DFP_T_A_SH_Rc(dscri, 0x02, 0x03),
11313 GEN_DFP_Tp_Ap_SH_Rc(dscriq, 0x02, 0x03),
11314
11315 #undef GEN_SPE
11316 #define GEN_SPE(name0, name1, opc2, opc3, inval0, inval1, type) \
11317     GEN_OPCODE_DUAL(name0##_##name1, 0x04, opc2, opc3, inval0, inval1, type, PPC_NONE)
11318 GEN_SPE(evaddw,      speundef,    0x00, 0x08, 0x00000000, 0xFFFFFFFF, PPC_SPE),
11319 GEN_SPE(evaddiw,     speundef,    0x01, 0x08, 0x00000000, 0xFFFFFFFF, PPC_SPE),
11320 GEN_SPE(evsubfw,     speundef,    0x02, 0x08, 0x00000000, 0xFFFFFFFF, PPC_SPE),
11321 GEN_SPE(evsubifw,    speundef,    0x03, 0x08, 0x00000000, 0xFFFFFFFF, PPC_SPE),
11322 GEN_SPE(evabs,       evneg,       0x04, 0x08, 0x0000F800, 0x0000F800, PPC_SPE),
11323 GEN_SPE(evextsb,     evextsh,     0x05, 0x08, 0x0000F800, 0x0000F800, PPC_SPE),
11324 GEN_SPE(evrndw,      evcntlzw,    0x06, 0x08, 0x0000F800, 0x0000F800, PPC_SPE),
11325 GEN_SPE(evcntlsw,    brinc,       0x07, 0x08, 0x0000F800, 0x00000000, PPC_SPE),
11326 GEN_SPE(evmra,       speundef,    0x02, 0x13, 0x0000F800, 0xFFFFFFFF, PPC_SPE),
11327 GEN_SPE(speundef,    evand,       0x08, 0x08, 0xFFFFFFFF, 0x00000000, PPC_SPE),
11328 GEN_SPE(evandc,      speundef,    0x09, 0x08, 0x00000000, 0xFFFFFFFF, PPC_SPE),
11329 GEN_SPE(evxor,       evor,        0x0B, 0x08, 0x00000000, 0x00000000, PPC_SPE),
11330 GEN_SPE(evnor,       eveqv,       0x0C, 0x08, 0x00000000, 0x00000000, PPC_SPE),
11331 GEN_SPE(evmwumi,     evmwsmi,     0x0C, 0x11, 0x00000000, 0x00000000, PPC_SPE),
11332 GEN_SPE(evmwumia,    evmwsmia,    0x1C, 0x11, 0x00000000, 0x00000000, PPC_SPE),
11333 GEN_SPE(evmwumiaa,   evmwsmiaa,   0x0C, 0x15, 0x00000000, 0x00000000, PPC_SPE),
11334 GEN_SPE(speundef,    evorc,       0x0D, 0x08, 0xFFFFFFFF, 0x00000000, PPC_SPE),
11335 GEN_SPE(evnand,      speundef,    0x0F, 0x08, 0x00000000, 0xFFFFFFFF, PPC_SPE),
11336 GEN_SPE(evsrwu,      evsrws,      0x10, 0x08, 0x00000000, 0x00000000, PPC_SPE),
11337 GEN_SPE(evsrwiu,     evsrwis,     0x11, 0x08, 0x00000000, 0x00000000, PPC_SPE),
11338 GEN_SPE(evslw,       speundef,    0x12, 0x08, 0x00000000, 0xFFFFFFFF, PPC_SPE),
11339 GEN_SPE(evslwi,      speundef,    0x13, 0x08, 0x00000000, 0xFFFFFFFF, PPC_SPE),
11340 GEN_SPE(evrlw,       evsplati,    0x14, 0x08, 0x00000000, 0x0000F800, PPC_SPE),
11341 GEN_SPE(evrlwi,      evsplatfi,   0x15, 0x08, 0x00000000, 0x0000F800, PPC_SPE),
11342 GEN_SPE(evmergehi,   evmergelo,   0x16, 0x08, 0x00000000, 0x00000000, PPC_SPE),
11343 GEN_SPE(evmergehilo, evmergelohi, 0x17, 0x08, 0x00000000, 0x00000000, PPC_SPE),
11344 GEN_SPE(evcmpgtu,    evcmpgts,    0x18, 0x08, 0x00600000, 0x00600000, PPC_SPE),
11345 GEN_SPE(evcmpltu,    evcmplts,    0x19, 0x08, 0x00600000, 0x00600000, PPC_SPE),
11346 GEN_SPE(evcmpeq,     speundef,    0x1A, 0x08, 0x00600000, 0xFFFFFFFF, PPC_SPE),
11347
11348 GEN_SPE(evfsadd,     evfssub,     0x00, 0x0A, 0x00000000, 0x00000000, PPC_SPE_SINGLE),
11349 GEN_SPE(evfsabs,     evfsnabs,    0x02, 0x0A, 0x0000F800, 0x0000F800, PPC_SPE_SINGLE),
11350 GEN_SPE(evfsneg,     speundef,    0x03, 0x0A, 0x0000F800, 0xFFFFFFFF, PPC_SPE_SINGLE),
11351 GEN_SPE(evfsmul,     evfsdiv,     0x04, 0x0A, 0x00000000, 0x00000000, PPC_SPE_SINGLE),
11352 GEN_SPE(evfscmpgt,   evfscmplt,   0x06, 0x0A, 0x00600000, 0x00600000, PPC_SPE_SINGLE),
11353 GEN_SPE(evfscmpeq,   speundef,    0x07, 0x0A, 0x00600000, 0xFFFFFFFF, PPC_SPE_SINGLE),
11354 GEN_SPE(evfscfui,    evfscfsi,    0x08, 0x0A, 0x00180000, 0x00180000, PPC_SPE_SINGLE),
11355 GEN_SPE(evfscfuf,    evfscfsf,    0x09, 0x0A, 0x00180000, 0x00180000, PPC_SPE_SINGLE),
11356 GEN_SPE(evfsctui,    evfsctsi,    0x0A, 0x0A, 0x00180000, 0x00180000, PPC_SPE_SINGLE),
11357 GEN_SPE(evfsctuf,    evfsctsf,    0x0B, 0x0A, 0x00180000, 0x00180000, PPC_SPE_SINGLE),
11358 GEN_SPE(evfsctuiz,   speundef,    0x0C, 0x0A, 0x00180000, 0xFFFFFFFF, PPC_SPE_SINGLE),
11359 GEN_SPE(evfsctsiz,   speundef,    0x0D, 0x0A, 0x00180000, 0xFFFFFFFF, PPC_SPE_SINGLE),
11360 GEN_SPE(evfststgt,   evfststlt,   0x0E, 0x0A, 0x00600000, 0x00600000, PPC_SPE_SINGLE),
11361 GEN_SPE(evfststeq,   speundef,    0x0F, 0x0A, 0x00600000, 0xFFFFFFFF, PPC_SPE_SINGLE),
11362
11363 GEN_SPE(efsadd,      efssub,      0x00, 0x0B, 0x00000000, 0x00000000, PPC_SPE_SINGLE),
11364 GEN_SPE(efsabs,      efsnabs,     0x02, 0x0B, 0x0000F800, 0x0000F800, PPC_SPE_SINGLE),
11365 GEN_SPE(efsneg,      speundef,    0x03, 0x0B, 0x0000F800, 0xFFFFFFFF, PPC_SPE_SINGLE),
11366 GEN_SPE(efsmul,      efsdiv,      0x04, 0x0B, 0x00000000, 0x00000000, PPC_SPE_SINGLE),
11367 GEN_SPE(efscmpgt,    efscmplt,    0x06, 0x0B, 0x00600000, 0x00600000, PPC_SPE_SINGLE),
11368 GEN_SPE(efscmpeq,    efscfd,      0x07, 0x0B, 0x00600000, 0x00180000, PPC_SPE_SINGLE),
11369 GEN_SPE(efscfui,     efscfsi,     0x08, 0x0B, 0x00180000, 0x00180000, PPC_SPE_SINGLE),
11370 GEN_SPE(efscfuf,     efscfsf,     0x09, 0x0B, 0x00180000, 0x00180000, PPC_SPE_SINGLE),
11371 GEN_SPE(efsctui,     efsctsi,     0x0A, 0x0B, 0x00180000, 0x00180000, PPC_SPE_SINGLE),
11372 GEN_SPE(efsctuf,     efsctsf,     0x0B, 0x0B, 0x00180000, 0x00180000, PPC_SPE_SINGLE),
11373 GEN_SPE(efsctuiz,    speundef,    0x0C, 0x0B, 0x00180000, 0xFFFFFFFF, PPC_SPE_SINGLE),
11374 GEN_SPE(efsctsiz,    speundef,    0x0D, 0x0B, 0x00180000, 0xFFFFFFFF, PPC_SPE_SINGLE),
11375 GEN_SPE(efststgt,    efststlt,    0x0E, 0x0B, 0x00600000, 0x00600000, PPC_SPE_SINGLE),
11376 GEN_SPE(efststeq,    speundef,    0x0F, 0x0B, 0x00600000, 0xFFFFFFFF, PPC_SPE_SINGLE),
11377
11378 GEN_SPE(efdadd,      efdsub,      0x10, 0x0B, 0x00000000, 0x00000000, PPC_SPE_DOUBLE),
11379 GEN_SPE(efdcfuid,    efdcfsid,    0x11, 0x0B, 0x00180000, 0x00180000, PPC_SPE_DOUBLE),
11380 GEN_SPE(efdabs,      efdnabs,     0x12, 0x0B, 0x0000F800, 0x0000F800, PPC_SPE_DOUBLE),
11381 GEN_SPE(efdneg,      speundef,    0x13, 0x0B, 0x0000F800, 0xFFFFFFFF, PPC_SPE_DOUBLE),
11382 GEN_SPE(efdmul,      efddiv,      0x14, 0x0B, 0x00000000, 0x00000000, PPC_SPE_DOUBLE),
11383 GEN_SPE(efdctuidz,   efdctsidz,   0x15, 0x0B, 0x00180000, 0x00180000, PPC_SPE_DOUBLE),
11384 GEN_SPE(efdcmpgt,    efdcmplt,    0x16, 0x0B, 0x00600000, 0x00600000, PPC_SPE_DOUBLE),
11385 GEN_SPE(efdcmpeq,    efdcfs,      0x17, 0x0B, 0x00600000, 0x00180000, PPC_SPE_DOUBLE),
11386 GEN_SPE(efdcfui,     efdcfsi,     0x18, 0x0B, 0x00180000, 0x00180000, PPC_SPE_DOUBLE),
11387 GEN_SPE(efdcfuf,     efdcfsf,     0x19, 0x0B, 0x00180000, 0x00180000, PPC_SPE_DOUBLE),
11388 GEN_SPE(efdctui,     efdctsi,     0x1A, 0x0B, 0x00180000, 0x00180000, PPC_SPE_DOUBLE),
11389 GEN_SPE(efdctuf,     efdctsf,     0x1B, 0x0B, 0x00180000, 0x00180000, PPC_SPE_DOUBLE),
11390 GEN_SPE(efdctuiz,    speundef,    0x1C, 0x0B, 0x00180000, 0xFFFFFFFF, PPC_SPE_DOUBLE),
11391 GEN_SPE(efdctsiz,    speundef,    0x1D, 0x0B, 0x00180000, 0xFFFFFFFF, PPC_SPE_DOUBLE),
11392 GEN_SPE(efdtstgt,    efdtstlt,    0x1E, 0x0B, 0x00600000, 0x00600000, PPC_SPE_DOUBLE),
11393 GEN_SPE(efdtsteq,    speundef,    0x1F, 0x0B, 0x00600000, 0xFFFFFFFF, PPC_SPE_DOUBLE),
11394
11395 #undef GEN_SPEOP_LDST
11396 #define GEN_SPEOP_LDST(name, opc2, sh)                                        \
11397 GEN_HANDLER(name, 0x04, opc2, 0x0C, 0x00000000, PPC_SPE)
11398 GEN_SPEOP_LDST(evldd, 0x00, 3),
11399 GEN_SPEOP_LDST(evldw, 0x01, 3),
11400 GEN_SPEOP_LDST(evldh, 0x02, 3),
11401 GEN_SPEOP_LDST(evlhhesplat, 0x04, 1),
11402 GEN_SPEOP_LDST(evlhhousplat, 0x06, 1),
11403 GEN_SPEOP_LDST(evlhhossplat, 0x07, 1),
11404 GEN_SPEOP_LDST(evlwhe, 0x08, 2),
11405 GEN_SPEOP_LDST(evlwhou, 0x0A, 2),
11406 GEN_SPEOP_LDST(evlwhos, 0x0B, 2),
11407 GEN_SPEOP_LDST(evlwwsplat, 0x0C, 2),
11408 GEN_SPEOP_LDST(evlwhsplat, 0x0E, 2),
11409
11410 GEN_SPEOP_LDST(evstdd, 0x10, 3),
11411 GEN_SPEOP_LDST(evstdw, 0x11, 3),
11412 GEN_SPEOP_LDST(evstdh, 0x12, 3),
11413 GEN_SPEOP_LDST(evstwhe, 0x18, 2),
11414 GEN_SPEOP_LDST(evstwho, 0x1A, 2),
11415 GEN_SPEOP_LDST(evstwwe, 0x1C, 2),
11416 GEN_SPEOP_LDST(evstwwo, 0x1E, 2),
11417
11418 GEN_HANDLER2_E(tbegin, "tbegin", 0x1F, 0x0E, 0x14, 0x01DFF800, \
11419                PPC_NONE, PPC2_TM),
11420 GEN_HANDLER2_E(tend,   "tend",   0x1F, 0x0E, 0x15, 0x01FFF800, \
11421                PPC_NONE, PPC2_TM),
11422 GEN_HANDLER2_E(tabort, "tabort", 0x1F, 0x0E, 0x1C, 0x03E0F800, \
11423                PPC_NONE, PPC2_TM),
11424 GEN_HANDLER2_E(tabortwc, "tabortwc", 0x1F, 0x0E, 0x18, 0x00000000, \
11425                PPC_NONE, PPC2_TM),
11426 GEN_HANDLER2_E(tabortwci, "tabortwci", 0x1F, 0x0E, 0x1A, 0x00000000, \
11427                PPC_NONE, PPC2_TM),
11428 GEN_HANDLER2_E(tabortdc, "tabortdc", 0x1F, 0x0E, 0x19, 0x00000000, \
11429                PPC_NONE, PPC2_TM),
11430 GEN_HANDLER2_E(tabortdci, "tabortdci", 0x1F, 0x0E, 0x1B, 0x00000000, \
11431                PPC_NONE, PPC2_TM),
11432 GEN_HANDLER2_E(tsr, "tsr", 0x1F, 0x0E, 0x17, 0x03DFF800, \
11433                PPC_NONE, PPC2_TM),
11434 GEN_HANDLER2_E(tcheck, "tcheck", 0x1F, 0x0E, 0x16, 0x007FF800, \
11435                PPC_NONE, PPC2_TM),
11436 GEN_HANDLER2_E(treclaim, "treclaim", 0x1F, 0x0E, 0x1D, 0x03E0F800, \
11437                PPC_NONE, PPC2_TM),
11438 GEN_HANDLER2_E(trechkpt, "trechkpt", 0x1F, 0x0E, 0x1F, 0x03FFF800, \
11439                PPC_NONE, PPC2_TM),
11440 };
11441
11442 #include "helper_regs.h"
11443 #include "translate_init.c"
11444
11445 /*****************************************************************************/
11446 /* Misc PowerPC helpers */
11447 void ppc_cpu_dump_state(CPUState *cs, FILE *f, fprintf_function cpu_fprintf,
11448                         int flags)
11449 {
11450 #define RGPL  4
11451 #define RFPL  4
11452
11453     PowerPCCPU *cpu = POWERPC_CPU(cs);
11454     CPUPPCState *env = &cpu->env;
11455     int i;
11456
11457     cpu_fprintf(f, "NIP " TARGET_FMT_lx "   LR " TARGET_FMT_lx " CTR "
11458                 TARGET_FMT_lx " XER " TARGET_FMT_lx " CPU#%d\n",
11459                 env->nip, env->lr, env->ctr, cpu_read_xer(env),
11460                 cs->cpu_index);
11461     cpu_fprintf(f, "MSR " TARGET_FMT_lx " HID0 " TARGET_FMT_lx "  HF "
11462                 TARGET_FMT_lx " iidx %d didx %d\n",
11463                 env->msr, env->spr[SPR_HID0],
11464                 env->hflags, env->immu_idx, env->dmmu_idx);
11465 #if !defined(NO_TIMER_DUMP)
11466     cpu_fprintf(f, "TB %08" PRIu32 " %08" PRIu64
11467 #if !defined(CONFIG_USER_ONLY)
11468                 " DECR %08" PRIu32
11469 #endif
11470                 "\n",
11471                 cpu_ppc_load_tbu(env), cpu_ppc_load_tbl(env)
11472 #if !defined(CONFIG_USER_ONLY)
11473                 , cpu_ppc_load_decr(env)
11474 #endif
11475                 );
11476 #endif
11477     for (i = 0; i < 32; i++) {
11478         if ((i & (RGPL - 1)) == 0)
11479             cpu_fprintf(f, "GPR%02d", i);
11480         cpu_fprintf(f, " %016" PRIx64, ppc_dump_gpr(env, i));
11481         if ((i & (RGPL - 1)) == (RGPL - 1))
11482             cpu_fprintf(f, "\n");
11483     }
11484     cpu_fprintf(f, "CR ");
11485     for (i = 0; i < 8; i++)
11486         cpu_fprintf(f, "%01x", env->crf[i]);
11487     cpu_fprintf(f, "  [");
11488     for (i = 0; i < 8; i++) {
11489         char a = '-';
11490         if (env->crf[i] & 0x08)
11491             a = 'L';
11492         else if (env->crf[i] & 0x04)
11493             a = 'G';
11494         else if (env->crf[i] & 0x02)
11495             a = 'E';
11496         cpu_fprintf(f, " %c%c", a, env->crf[i] & 0x01 ? 'O' : ' ');
11497     }
11498     cpu_fprintf(f, " ]             RES " TARGET_FMT_lx "\n",
11499                 env->reserve_addr);
11500     for (i = 0; i < 32; i++) {
11501         if ((i & (RFPL - 1)) == 0)
11502             cpu_fprintf(f, "FPR%02d", i);
11503         cpu_fprintf(f, " %016" PRIx64, *((uint64_t *)&env->fpr[i]));
11504         if ((i & (RFPL - 1)) == (RFPL - 1))
11505             cpu_fprintf(f, "\n");
11506     }
11507     cpu_fprintf(f, "FPSCR " TARGET_FMT_lx "\n", env->fpscr);
11508 #if !defined(CONFIG_USER_ONLY)
11509     cpu_fprintf(f, " SRR0 " TARGET_FMT_lx "  SRR1 " TARGET_FMT_lx
11510                    "    PVR " TARGET_FMT_lx " VRSAVE " TARGET_FMT_lx "\n",
11511                 env->spr[SPR_SRR0], env->spr[SPR_SRR1],
11512                 env->spr[SPR_PVR], env->spr[SPR_VRSAVE]);
11513
11514     cpu_fprintf(f, "SPRG0 " TARGET_FMT_lx " SPRG1 " TARGET_FMT_lx
11515                    "  SPRG2 " TARGET_FMT_lx "  SPRG3 " TARGET_FMT_lx "\n",
11516                 env->spr[SPR_SPRG0], env->spr[SPR_SPRG1],
11517                 env->spr[SPR_SPRG2], env->spr[SPR_SPRG3]);
11518
11519     cpu_fprintf(f, "SPRG4 " TARGET_FMT_lx " SPRG5 " TARGET_FMT_lx
11520                    "  SPRG6 " TARGET_FMT_lx "  SPRG7 " TARGET_FMT_lx "\n",
11521                 env->spr[SPR_SPRG4], env->spr[SPR_SPRG5],
11522                 env->spr[SPR_SPRG6], env->spr[SPR_SPRG7]);
11523
11524 #if defined(TARGET_PPC64)
11525     if (env->excp_model == POWERPC_EXCP_POWER7 ||
11526         env->excp_model == POWERPC_EXCP_POWER8) {
11527         cpu_fprintf(f, "HSRR0 " TARGET_FMT_lx " HSRR1 " TARGET_FMT_lx "\n",
11528                     env->spr[SPR_HSRR0], env->spr[SPR_HSRR1]);
11529     }
11530 #endif
11531     if (env->excp_model == POWERPC_EXCP_BOOKE) {
11532         cpu_fprintf(f, "CSRR0 " TARGET_FMT_lx " CSRR1 " TARGET_FMT_lx
11533                        " MCSRR0 " TARGET_FMT_lx " MCSRR1 " TARGET_FMT_lx "\n",
11534                     env->spr[SPR_BOOKE_CSRR0], env->spr[SPR_BOOKE_CSRR1],
11535                     env->spr[SPR_BOOKE_MCSRR0], env->spr[SPR_BOOKE_MCSRR1]);
11536
11537         cpu_fprintf(f, "  TCR " TARGET_FMT_lx "   TSR " TARGET_FMT_lx
11538                        "    ESR " TARGET_FMT_lx "   DEAR " TARGET_FMT_lx "\n",
11539                     env->spr[SPR_BOOKE_TCR], env->spr[SPR_BOOKE_TSR],
11540                     env->spr[SPR_BOOKE_ESR], env->spr[SPR_BOOKE_DEAR]);
11541
11542         cpu_fprintf(f, "  PIR " TARGET_FMT_lx " DECAR " TARGET_FMT_lx
11543                        "   IVPR " TARGET_FMT_lx "   EPCR " TARGET_FMT_lx "\n",
11544                     env->spr[SPR_BOOKE_PIR], env->spr[SPR_BOOKE_DECAR],
11545                     env->spr[SPR_BOOKE_IVPR], env->spr[SPR_BOOKE_EPCR]);
11546
11547         cpu_fprintf(f, " MCSR " TARGET_FMT_lx " SPRG8 " TARGET_FMT_lx
11548                        "    EPR " TARGET_FMT_lx "\n",
11549                     env->spr[SPR_BOOKE_MCSR], env->spr[SPR_BOOKE_SPRG8],
11550                     env->spr[SPR_BOOKE_EPR]);
11551
11552         /* FSL-specific */
11553         cpu_fprintf(f, " MCAR " TARGET_FMT_lx "  PID1 " TARGET_FMT_lx
11554                        "   PID2 " TARGET_FMT_lx "    SVR " TARGET_FMT_lx "\n",
11555                     env->spr[SPR_Exxx_MCAR], env->spr[SPR_BOOKE_PID1],
11556                     env->spr[SPR_BOOKE_PID2], env->spr[SPR_E500_SVR]);
11557
11558         /*
11559          * IVORs are left out as they are large and do not change often --
11560          * they can be read with "p $ivor0", "p $ivor1", etc.
11561          */
11562     }
11563
11564 #if defined(TARGET_PPC64)
11565     if (env->flags & POWERPC_FLAG_CFAR) {
11566         cpu_fprintf(f, " CFAR " TARGET_FMT_lx"\n", env->cfar);
11567     }
11568 #endif
11569
11570     switch (env->mmu_model) {
11571     case POWERPC_MMU_32B:
11572     case POWERPC_MMU_601:
11573     case POWERPC_MMU_SOFT_6xx:
11574     case POWERPC_MMU_SOFT_74xx:
11575 #if defined(TARGET_PPC64)
11576     case POWERPC_MMU_64B:
11577     case POWERPC_MMU_2_03:
11578     case POWERPC_MMU_2_06:
11579     case POWERPC_MMU_2_06a:
11580     case POWERPC_MMU_2_07:
11581     case POWERPC_MMU_2_07a:
11582 #endif
11583         cpu_fprintf(f, " SDR1 " TARGET_FMT_lx "   DAR " TARGET_FMT_lx
11584                        "  DSISR " TARGET_FMT_lx "\n", env->spr[SPR_SDR1],
11585                     env->spr[SPR_DAR], env->spr[SPR_DSISR]);
11586         break;
11587     case POWERPC_MMU_BOOKE206:
11588         cpu_fprintf(f, " MAS0 " TARGET_FMT_lx "  MAS1 " TARGET_FMT_lx
11589                        "   MAS2 " TARGET_FMT_lx "   MAS3 " TARGET_FMT_lx "\n",
11590                     env->spr[SPR_BOOKE_MAS0], env->spr[SPR_BOOKE_MAS1],
11591                     env->spr[SPR_BOOKE_MAS2], env->spr[SPR_BOOKE_MAS3]);
11592
11593         cpu_fprintf(f, " MAS4 " TARGET_FMT_lx "  MAS6 " TARGET_FMT_lx
11594                        "   MAS7 " TARGET_FMT_lx "    PID " TARGET_FMT_lx "\n",
11595                     env->spr[SPR_BOOKE_MAS4], env->spr[SPR_BOOKE_MAS6],
11596                     env->spr[SPR_BOOKE_MAS7], env->spr[SPR_BOOKE_PID]);
11597
11598         cpu_fprintf(f, "MMUCFG " TARGET_FMT_lx " TLB0CFG " TARGET_FMT_lx
11599                        " TLB1CFG " TARGET_FMT_lx "\n",
11600                     env->spr[SPR_MMUCFG], env->spr[SPR_BOOKE_TLB0CFG],
11601                     env->spr[SPR_BOOKE_TLB1CFG]);
11602         break;
11603     default:
11604         break;
11605     }
11606 #endif
11607
11608 #undef RGPL
11609 #undef RFPL
11610 }
11611
11612 void ppc_cpu_dump_statistics(CPUState *cs, FILE*f,
11613                              fprintf_function cpu_fprintf, int flags)
11614 {
11615 #if defined(DO_PPC_STATISTICS)
11616     PowerPCCPU *cpu = POWERPC_CPU(cs);
11617     opc_handler_t **t1, **t2, **t3, *handler;
11618     int op1, op2, op3;
11619
11620     t1 = cpu->env.opcodes;
11621     for (op1 = 0; op1 < 64; op1++) {
11622         handler = t1[op1];
11623         if (is_indirect_opcode(handler)) {
11624             t2 = ind_table(handler);
11625             for (op2 = 0; op2 < 32; op2++) {
11626                 handler = t2[op2];
11627                 if (is_indirect_opcode(handler)) {
11628                     t3 = ind_table(handler);
11629                     for (op3 = 0; op3 < 32; op3++) {
11630                         handler = t3[op3];
11631                         if (handler->count == 0)
11632                             continue;
11633                         cpu_fprintf(f, "%02x %02x %02x (%02x %04d) %16s: "
11634                                     "%016" PRIx64 " %" PRId64 "\n",
11635                                     op1, op2, op3, op1, (op3 << 5) | op2,
11636                                     handler->oname,
11637                                     handler->count, handler->count);
11638                     }
11639                 } else {
11640                     if (handler->count == 0)
11641                         continue;
11642                     cpu_fprintf(f, "%02x %02x    (%02x %04d) %16s: "
11643                                 "%016" PRIx64 " %" PRId64 "\n",
11644                                 op1, op2, op1, op2, handler->oname,
11645                                 handler->count, handler->count);
11646                 }
11647             }
11648         } else {
11649             if (handler->count == 0)
11650                 continue;
11651             cpu_fprintf(f, "%02x       (%02x     ) %16s: %016" PRIx64
11652                         " %" PRId64 "\n",
11653                         op1, op1, handler->oname,
11654                         handler->count, handler->count);
11655         }
11656     }
11657 #endif
11658 }
11659
11660 /*****************************************************************************/
11661 void gen_intermediate_code(CPUPPCState *env, struct TranslationBlock *tb)
11662 {
11663     PowerPCCPU *cpu = ppc_env_get_cpu(env);
11664     CPUState *cs = CPU(cpu);
11665     DisasContext ctx, *ctxp = &ctx;
11666     opc_handler_t **table, *handler;
11667     target_ulong pc_start;
11668     int num_insns;
11669     int max_insns;
11670
11671     pc_start = tb->pc;
11672     ctx.nip = pc_start;
11673     ctx.tb = tb;
11674     ctx.exception = POWERPC_EXCP_NONE;
11675     ctx.spr_cb = env->spr_cb;
11676     ctx.pr = msr_pr;
11677     ctx.mem_idx = env->dmmu_idx;
11678     ctx.dr = msr_dr;
11679 #if !defined(CONFIG_USER_ONLY)
11680     ctx.hv = msr_hv || !env->has_hv_mode;
11681 #endif
11682     ctx.insns_flags = env->insns_flags;
11683     ctx.insns_flags2 = env->insns_flags2;
11684     ctx.access_type = -1;
11685     ctx.le_mode = !!(env->hflags & (1 << MSR_LE));
11686     ctx.default_tcg_memop_mask = ctx.le_mode ? MO_LE : MO_BE;
11687 #if defined(TARGET_PPC64)
11688     ctx.sf_mode = msr_is_64bit(env, env->msr);
11689     ctx.has_cfar = !!(env->flags & POWERPC_FLAG_CFAR);
11690 #endif
11691     if (env->mmu_model == POWERPC_MMU_32B ||
11692         env->mmu_model == POWERPC_MMU_601 ||
11693         (env->mmu_model & POWERPC_MMU_64B))
11694             ctx.lazy_tlb_flush = true;
11695
11696     ctx.fpu_enabled = !!msr_fp;
11697     if ((env->flags & POWERPC_FLAG_SPE) && msr_spe)
11698         ctx.spe_enabled = !!msr_spe;
11699     else
11700         ctx.spe_enabled = false;
11701     if ((env->flags & POWERPC_FLAG_VRE) && msr_vr)
11702         ctx.altivec_enabled = !!msr_vr;
11703     else
11704         ctx.altivec_enabled = false;
11705     if ((env->flags & POWERPC_FLAG_VSX) && msr_vsx) {
11706         ctx.vsx_enabled = !!msr_vsx;
11707     } else {
11708         ctx.vsx_enabled = false;
11709     }
11710 #if defined(TARGET_PPC64)
11711     if ((env->flags & POWERPC_FLAG_TM) && msr_tm) {
11712         ctx.tm_enabled = !!msr_tm;
11713     } else {
11714         ctx.tm_enabled = false;
11715     }
11716 #endif
11717     if ((env->flags & POWERPC_FLAG_SE) && msr_se)
11718         ctx.singlestep_enabled = CPU_SINGLE_STEP;
11719     else
11720         ctx.singlestep_enabled = 0;
11721     if ((env->flags & POWERPC_FLAG_BE) && msr_be)
11722         ctx.singlestep_enabled |= CPU_BRANCH_STEP;
11723     if (unlikely(cs->singlestep_enabled)) {
11724         ctx.singlestep_enabled |= GDBSTUB_SINGLE_STEP;
11725     }
11726 #if defined (DO_SINGLE_STEP) && 0
11727     /* Single step trace mode */
11728     msr_se = 1;
11729 #endif
11730     num_insns = 0;
11731     max_insns = tb->cflags & CF_COUNT_MASK;
11732     if (max_insns == 0) {
11733         max_insns = CF_COUNT_MASK;
11734     }
11735     if (max_insns > TCG_MAX_INSNS) {
11736         max_insns = TCG_MAX_INSNS;
11737     }
11738
11739     gen_tb_start(tb);
11740     tcg_clear_temp_count();
11741     /* Set env in case of segfault during code fetch */
11742     while (ctx.exception == POWERPC_EXCP_NONE && !tcg_op_buf_full()) {
11743         tcg_gen_insn_start(ctx.nip);
11744         num_insns++;
11745
11746         if (unlikely(cpu_breakpoint_test(cs, ctx.nip, BP_ANY))) {
11747             gen_debug_exception(ctxp);
11748             /* The address covered by the breakpoint must be included in
11749                [tb->pc, tb->pc + tb->size) in order to for it to be
11750                properly cleared -- thus we increment the PC here so that
11751                the logic setting tb->size below does the right thing.  */
11752             ctx.nip += 4;
11753             break;
11754         }
11755
11756         LOG_DISAS("----------------\n");
11757         LOG_DISAS("nip=" TARGET_FMT_lx " super=%d ir=%d\n",
11758                   ctx.nip, ctx.mem_idx, (int)msr_ir);
11759         if (num_insns == max_insns && (tb->cflags & CF_LAST_IO))
11760             gen_io_start();
11761         if (unlikely(need_byteswap(&ctx))) {
11762             ctx.opcode = bswap32(cpu_ldl_code(env, ctx.nip));
11763         } else {
11764             ctx.opcode = cpu_ldl_code(env, ctx.nip);
11765         }
11766         LOG_DISAS("translate opcode %08x (%02x %02x %02x) (%s)\n",
11767                     ctx.opcode, opc1(ctx.opcode), opc2(ctx.opcode),
11768                     opc3(ctx.opcode), ctx.le_mode ? "little" : "big");
11769         ctx.nip += 4;
11770         table = env->opcodes;
11771         handler = table[opc1(ctx.opcode)];
11772         if (is_indirect_opcode(handler)) {
11773             table = ind_table(handler);
11774             handler = table[opc2(ctx.opcode)];
11775             if (is_indirect_opcode(handler)) {
11776                 table = ind_table(handler);
11777                 handler = table[opc3(ctx.opcode)];
11778             }
11779         }
11780         /* Is opcode *REALLY* valid ? */
11781         if (unlikely(handler->handler == &gen_invalid)) {
11782             qemu_log_mask(LOG_GUEST_ERROR, "invalid/unsupported opcode: "
11783                           "%02x - %02x - %02x (%08x) " TARGET_FMT_lx " %d\n",
11784                           opc1(ctx.opcode), opc2(ctx.opcode),
11785                           opc3(ctx.opcode), ctx.opcode, ctx.nip - 4, (int)msr_ir);
11786         } else {
11787             uint32_t inval;
11788
11789             if (unlikely(handler->type & (PPC_SPE | PPC_SPE_SINGLE | PPC_SPE_DOUBLE) && Rc(ctx.opcode))) {
11790                 inval = handler->inval2;
11791             } else {
11792                 inval = handler->inval1;
11793             }
11794
11795             if (unlikely((ctx.opcode & inval) != 0)) {
11796                 qemu_log_mask(LOG_GUEST_ERROR, "invalid bits: %08x for opcode: "
11797                               "%02x - %02x - %02x (%08x) " TARGET_FMT_lx "\n",
11798                               ctx.opcode & inval, opc1(ctx.opcode),
11799                               opc2(ctx.opcode), opc3(ctx.opcode),
11800                               ctx.opcode, ctx.nip - 4);
11801                 gen_inval_exception(ctxp, POWERPC_EXCP_INVAL_INVAL);
11802                 break;
11803             }
11804         }
11805         (*(handler->handler))(&ctx);
11806 #if defined(DO_PPC_STATISTICS)
11807         handler->count++;
11808 #endif
11809         /* Check trace mode exceptions */
11810         if (unlikely(ctx.singlestep_enabled & CPU_SINGLE_STEP &&
11811                      (ctx.nip <= 0x100 || ctx.nip > 0xF00) &&
11812                      ctx.exception != POWERPC_SYSCALL &&
11813                      ctx.exception != POWERPC_EXCP_TRAP &&
11814                      ctx.exception != POWERPC_EXCP_BRANCH)) {
11815             gen_exception(ctxp, POWERPC_EXCP_TRACE);
11816         } else if (unlikely(((ctx.nip & (TARGET_PAGE_SIZE - 1)) == 0) ||
11817                             (cs->singlestep_enabled) ||
11818                             singlestep ||
11819                             num_insns >= max_insns)) {
11820             /* if we reach a page boundary or are single stepping, stop
11821              * generation
11822              */
11823             break;
11824         }
11825         if (tcg_check_temp_count()) {
11826             fprintf(stderr, "Opcode %02x %02x %02x (%08x) leaked temporaries\n",
11827                     opc1(ctx.opcode), opc2(ctx.opcode), opc3(ctx.opcode),
11828                     ctx.opcode);
11829             exit(1);
11830         }
11831     }
11832     if (tb->cflags & CF_LAST_IO)
11833         gen_io_end();
11834     if (ctx.exception == POWERPC_EXCP_NONE) {
11835         gen_goto_tb(&ctx, 0, ctx.nip);
11836     } else if (ctx.exception != POWERPC_EXCP_BRANCH) {
11837         if (unlikely(cs->singlestep_enabled)) {
11838             gen_debug_exception(ctxp);
11839         }
11840         /* Generate the return instruction */
11841         tcg_gen_exit_tb(0);
11842     }
11843     gen_tb_end(tb, num_insns);
11844
11845     tb->size = ctx.nip - pc_start;
11846     tb->icount = num_insns;
11847
11848 #if defined(DEBUG_DISAS)
11849     if (qemu_loglevel_mask(CPU_LOG_TB_IN_ASM)
11850         && qemu_log_in_addr_range(pc_start)) {
11851         int flags;
11852         flags = env->bfd_mach;
11853         flags |= ctx.le_mode << 16;
11854         qemu_log("IN: %s\n", lookup_symbol(pc_start));
11855         log_target_disas(cs, pc_start, ctx.nip - pc_start, flags);
11856         qemu_log("\n");
11857     }
11858 #endif
11859 }
11860
11861 void restore_state_to_opc(CPUPPCState *env, TranslationBlock *tb,
11862                           target_ulong *data)
11863 {
11864     env->nip = data[0];
11865 }
This page took 0.710106 seconds and 4 git commands to generate.