]> Git Repo - qemu.git/blob - target-ppc/translate.c
target-ppc: Fully Migrate to gen_set_cr1_from_fpscr
[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 "cpu.h"
22 #include "disas/disas.h"
23 #include "tcg-op.h"
24 #include "qemu/host-utils.h"
25 #include "exec/cpu_ldst.h"
26
27 #include "exec/helper-proto.h"
28 #include "exec/helper-gen.h"
29
30 #include "trace-tcg.h"
31
32
33 #define CPU_SINGLE_STEP 0x1
34 #define CPU_BRANCH_STEP 0x2
35 #define GDBSTUB_SINGLE_STEP 0x4
36
37 /* Include definitions for instructions classes and implementations flags */
38 //#define PPC_DEBUG_DISAS
39 //#define DO_PPC_STATISTICS
40
41 #ifdef PPC_DEBUG_DISAS
42 #  define LOG_DISAS(...) qemu_log_mask(CPU_LOG_TB_IN_ASM, ## __VA_ARGS__)
43 #else
44 #  define LOG_DISAS(...) do { } while (0)
45 #endif
46 /*****************************************************************************/
47 /* Code translation helpers                                                  */
48
49 /* global register indexes */
50 static TCGv_ptr cpu_env;
51 static char cpu_reg_names[10*3 + 22*4 /* GPR */
52     + 10*4 + 22*5 /* SPE GPRh */
53     + 10*4 + 22*5 /* FPR */
54     + 2*(10*6 + 22*7) /* AVRh, AVRl */
55     + 10*5 + 22*6 /* VSR */
56     + 8*5 /* CRF */];
57 static TCGv cpu_gpr[32];
58 static TCGv cpu_gprh[32];
59 static TCGv_i64 cpu_fpr[32];
60 static TCGv_i64 cpu_avrh[32], cpu_avrl[32];
61 static TCGv_i64 cpu_vsr[32];
62 static TCGv_i32 cpu_crf[8];
63 static TCGv cpu_nip;
64 static TCGv cpu_msr;
65 static TCGv cpu_ctr;
66 static TCGv cpu_lr;
67 #if defined(TARGET_PPC64)
68 static TCGv cpu_cfar;
69 #endif
70 static TCGv cpu_xer, cpu_so, cpu_ov, cpu_ca;
71 static TCGv cpu_reserve;
72 static TCGv cpu_fpscr;
73 static TCGv_i32 cpu_access_type;
74
75 #include "exec/gen-icount.h"
76
77 void ppc_translate_init(void)
78 {
79     int i;
80     char* p;
81     size_t cpu_reg_names_size;
82     static int done_init = 0;
83
84     if (done_init)
85         return;
86
87     cpu_env = tcg_global_reg_new_ptr(TCG_AREG0, "env");
88
89     p = cpu_reg_names;
90     cpu_reg_names_size = sizeof(cpu_reg_names);
91
92     for (i = 0; i < 8; i++) {
93         snprintf(p, cpu_reg_names_size, "crf%d", i);
94         cpu_crf[i] = tcg_global_mem_new_i32(TCG_AREG0,
95                                             offsetof(CPUPPCState, crf[i]), p);
96         p += 5;
97         cpu_reg_names_size -= 5;
98     }
99
100     for (i = 0; i < 32; i++) {
101         snprintf(p, cpu_reg_names_size, "r%d", i);
102         cpu_gpr[i] = tcg_global_mem_new(TCG_AREG0,
103                                         offsetof(CPUPPCState, gpr[i]), p);
104         p += (i < 10) ? 3 : 4;
105         cpu_reg_names_size -= (i < 10) ? 3 : 4;
106         snprintf(p, cpu_reg_names_size, "r%dH", i);
107         cpu_gprh[i] = tcg_global_mem_new(TCG_AREG0,
108                                          offsetof(CPUPPCState, gprh[i]), p);
109         p += (i < 10) ? 4 : 5;
110         cpu_reg_names_size -= (i < 10) ? 4 : 5;
111
112         snprintf(p, cpu_reg_names_size, "fp%d", i);
113         cpu_fpr[i] = tcg_global_mem_new_i64(TCG_AREG0,
114                                             offsetof(CPUPPCState, fpr[i]), p);
115         p += (i < 10) ? 4 : 5;
116         cpu_reg_names_size -= (i < 10) ? 4 : 5;
117
118         snprintf(p, cpu_reg_names_size, "avr%dH", i);
119 #ifdef HOST_WORDS_BIGENDIAN
120         cpu_avrh[i] = tcg_global_mem_new_i64(TCG_AREG0,
121                                              offsetof(CPUPPCState, avr[i].u64[0]), p);
122 #else
123         cpu_avrh[i] = tcg_global_mem_new_i64(TCG_AREG0,
124                                              offsetof(CPUPPCState, avr[i].u64[1]), p);
125 #endif
126         p += (i < 10) ? 6 : 7;
127         cpu_reg_names_size -= (i < 10) ? 6 : 7;
128
129         snprintf(p, cpu_reg_names_size, "avr%dL", i);
130 #ifdef HOST_WORDS_BIGENDIAN
131         cpu_avrl[i] = tcg_global_mem_new_i64(TCG_AREG0,
132                                              offsetof(CPUPPCState, avr[i].u64[1]), p);
133 #else
134         cpu_avrl[i] = tcg_global_mem_new_i64(TCG_AREG0,
135                                              offsetof(CPUPPCState, avr[i].u64[0]), p);
136 #endif
137         p += (i < 10) ? 6 : 7;
138         cpu_reg_names_size -= (i < 10) ? 6 : 7;
139         snprintf(p, cpu_reg_names_size, "vsr%d", i);
140         cpu_vsr[i] = tcg_global_mem_new_i64(TCG_AREG0,
141                                              offsetof(CPUPPCState, vsr[i]), p);
142         p += (i < 10) ? 5 : 6;
143         cpu_reg_names_size -= (i < 10) ? 5 : 6;
144     }
145
146     cpu_nip = tcg_global_mem_new(TCG_AREG0,
147                                  offsetof(CPUPPCState, nip), "nip");
148
149     cpu_msr = tcg_global_mem_new(TCG_AREG0,
150                                  offsetof(CPUPPCState, msr), "msr");
151
152     cpu_ctr = tcg_global_mem_new(TCG_AREG0,
153                                  offsetof(CPUPPCState, ctr), "ctr");
154
155     cpu_lr = tcg_global_mem_new(TCG_AREG0,
156                                 offsetof(CPUPPCState, lr), "lr");
157
158 #if defined(TARGET_PPC64)
159     cpu_cfar = tcg_global_mem_new(TCG_AREG0,
160                                   offsetof(CPUPPCState, cfar), "cfar");
161 #endif
162
163     cpu_xer = tcg_global_mem_new(TCG_AREG0,
164                                  offsetof(CPUPPCState, xer), "xer");
165     cpu_so = tcg_global_mem_new(TCG_AREG0,
166                                 offsetof(CPUPPCState, so), "SO");
167     cpu_ov = tcg_global_mem_new(TCG_AREG0,
168                                 offsetof(CPUPPCState, ov), "OV");
169     cpu_ca = tcg_global_mem_new(TCG_AREG0,
170                                 offsetof(CPUPPCState, ca), "CA");
171
172     cpu_reserve = tcg_global_mem_new(TCG_AREG0,
173                                      offsetof(CPUPPCState, reserve_addr),
174                                      "reserve_addr");
175
176     cpu_fpscr = tcg_global_mem_new(TCG_AREG0,
177                                    offsetof(CPUPPCState, fpscr), "fpscr");
178
179     cpu_access_type = tcg_global_mem_new_i32(TCG_AREG0,
180                                              offsetof(CPUPPCState, access_type), "access_type");
181
182     done_init = 1;
183 }
184
185 /* internal defines */
186 typedef struct DisasContext {
187     struct TranslationBlock *tb;
188     target_ulong nip;
189     uint32_t opcode;
190     uint32_t exception;
191     /* Routine used to access memory */
192     bool pr, hv;
193     int mem_idx;
194     int access_type;
195     /* Translation flags */
196     int le_mode;
197     TCGMemOp default_tcg_memop_mask;
198 #if defined(TARGET_PPC64)
199     int sf_mode;
200     int has_cfar;
201 #endif
202     int fpu_enabled;
203     int altivec_enabled;
204     int vsx_enabled;
205     int spe_enabled;
206     ppc_spr_t *spr_cb; /* Needed to check rights for mfspr/mtspr */
207     int singlestep_enabled;
208     uint64_t insns_flags;
209     uint64_t insns_flags2;
210 } DisasContext;
211
212 /* Return true iff byteswap is needed in a scalar memop */
213 static inline bool need_byteswap(const DisasContext *ctx)
214 {
215 #if defined(TARGET_WORDS_BIGENDIAN)
216      return ctx->le_mode;
217 #else
218      return !ctx->le_mode;
219 #endif
220 }
221
222 /* True when active word size < size of target_long.  */
223 #ifdef TARGET_PPC64
224 # define NARROW_MODE(C)  (!(C)->sf_mode)
225 #else
226 # define NARROW_MODE(C)  0
227 #endif
228
229 struct opc_handler_t {
230     /* invalid bits for instruction 1 (Rc(opcode) == 0) */
231     uint32_t inval1;
232     /* invalid bits for instruction 2 (Rc(opcode) == 1) */
233     uint32_t inval2;
234     /* instruction type */
235     uint64_t type;
236     /* extended instruction type */
237     uint64_t type2;
238     /* handler */
239     void (*handler)(DisasContext *ctx);
240 #if defined(DO_PPC_STATISTICS) || defined(PPC_DUMP_CPU)
241     const char *oname;
242 #endif
243 #if defined(DO_PPC_STATISTICS)
244     uint64_t count;
245 #endif
246 };
247
248 static inline void gen_reset_fpstatus(void)
249 {
250     gen_helper_reset_fpstatus(cpu_env);
251 }
252
253 static inline void gen_compute_fprf(TCGv_i64 arg, int set_fprf)
254 {
255     TCGv_i32 t0 = tcg_temp_new_i32();
256
257     if (set_fprf != 0) {
258         /* This case might be optimized later */
259         tcg_gen_movi_i32(t0, 1);
260         gen_helper_compute_fprf(t0, cpu_env, arg, t0);
261         gen_helper_float_check_status(cpu_env);
262     }
263
264     tcg_temp_free_i32(t0);
265 }
266
267 static inline void gen_set_access_type(DisasContext *ctx, int access_type)
268 {
269     if (ctx->access_type != access_type) {
270         tcg_gen_movi_i32(cpu_access_type, access_type);
271         ctx->access_type = access_type;
272     }
273 }
274
275 static inline void gen_update_nip(DisasContext *ctx, target_ulong nip)
276 {
277     if (NARROW_MODE(ctx)) {
278         nip = (uint32_t)nip;
279     }
280     tcg_gen_movi_tl(cpu_nip, nip);
281 }
282
283 void gen_update_current_nip(void *opaque)
284 {
285     DisasContext *ctx = opaque;
286
287     tcg_gen_movi_tl(cpu_nip, ctx->nip);
288 }
289
290 static inline void gen_exception_err(DisasContext *ctx, uint32_t excp, uint32_t error)
291 {
292     TCGv_i32 t0, t1;
293     if (ctx->exception == POWERPC_EXCP_NONE) {
294         gen_update_nip(ctx, ctx->nip);
295     }
296     t0 = tcg_const_i32(excp);
297     t1 = tcg_const_i32(error);
298     gen_helper_raise_exception_err(cpu_env, t0, t1);
299     tcg_temp_free_i32(t0);
300     tcg_temp_free_i32(t1);
301     ctx->exception = (excp);
302 }
303
304 static inline void gen_exception(DisasContext *ctx, uint32_t excp)
305 {
306     TCGv_i32 t0;
307     if (ctx->exception == POWERPC_EXCP_NONE) {
308         gen_update_nip(ctx, ctx->nip);
309     }
310     t0 = tcg_const_i32(excp);
311     gen_helper_raise_exception(cpu_env, t0);
312     tcg_temp_free_i32(t0);
313     ctx->exception = (excp);
314 }
315
316 static inline void gen_debug_exception(DisasContext *ctx)
317 {
318     TCGv_i32 t0;
319
320     if ((ctx->exception != POWERPC_EXCP_BRANCH) &&
321         (ctx->exception != POWERPC_EXCP_SYNC)) {
322         gen_update_nip(ctx, ctx->nip);
323     }
324     t0 = tcg_const_i32(EXCP_DEBUG);
325     gen_helper_raise_exception(cpu_env, t0);
326     tcg_temp_free_i32(t0);
327 }
328
329 static inline void gen_inval_exception(DisasContext *ctx, uint32_t error)
330 {
331     gen_exception_err(ctx, POWERPC_EXCP_PROGRAM, POWERPC_EXCP_INVAL | error);
332 }
333
334 /* Stop translation */
335 static inline void gen_stop_exception(DisasContext *ctx)
336 {
337     gen_update_nip(ctx, ctx->nip);
338     ctx->exception = POWERPC_EXCP_STOP;
339 }
340
341 /* No need to update nip here, as execution flow will change */
342 static inline void gen_sync_exception(DisasContext *ctx)
343 {
344     ctx->exception = POWERPC_EXCP_SYNC;
345 }
346
347 #define GEN_HANDLER(name, opc1, opc2, opc3, inval, type)                      \
348 GEN_OPCODE(name, opc1, opc2, opc3, inval, type, PPC_NONE)
349
350 #define GEN_HANDLER_E(name, opc1, opc2, opc3, inval, type, type2)             \
351 GEN_OPCODE(name, opc1, opc2, opc3, inval, type, type2)
352
353 #define GEN_HANDLER2(name, onam, opc1, opc2, opc3, inval, type)               \
354 GEN_OPCODE2(name, onam, opc1, opc2, opc3, inval, type, PPC_NONE)
355
356 #define GEN_HANDLER2_E(name, onam, opc1, opc2, opc3, inval, type, type2)      \
357 GEN_OPCODE2(name, onam, opc1, opc2, opc3, inval, type, type2)
358
359 typedef struct opcode_t {
360     unsigned char opc1, opc2, opc3;
361 #if HOST_LONG_BITS == 64 /* Explicitly align to 64 bits */
362     unsigned char pad[5];
363 #else
364     unsigned char pad[1];
365 #endif
366     opc_handler_t handler;
367     const char *oname;
368 } opcode_t;
369
370 /*****************************************************************************/
371 /***                           Instruction decoding                        ***/
372 #define EXTRACT_HELPER(name, shift, nb)                                       \
373 static inline uint32_t name(uint32_t opcode)                                  \
374 {                                                                             \
375     return (opcode >> (shift)) & ((1 << (nb)) - 1);                           \
376 }
377
378 #define EXTRACT_SHELPER(name, shift, nb)                                      \
379 static inline int32_t name(uint32_t opcode)                                   \
380 {                                                                             \
381     return (int16_t)((opcode >> (shift)) & ((1 << (nb)) - 1));                \
382 }
383
384 #define EXTRACT_HELPER_SPLIT(name, shift1, nb1, shift2, nb2)                  \
385 static inline uint32_t name(uint32_t opcode)                                  \
386 {                                                                             \
387     return (((opcode >> (shift1)) & ((1 << (nb1)) - 1)) << nb2) |             \
388             ((opcode >> (shift2)) & ((1 << (nb2)) - 1));                      \
389 }
390 /* Opcode part 1 */
391 EXTRACT_HELPER(opc1, 26, 6);
392 /* Opcode part 2 */
393 EXTRACT_HELPER(opc2, 1, 5);
394 /* Opcode part 3 */
395 EXTRACT_HELPER(opc3, 6, 5);
396 /* Update Cr0 flags */
397 EXTRACT_HELPER(Rc, 0, 1);
398 /* Update Cr6 flags (Altivec) */
399 EXTRACT_HELPER(Rc21, 10, 1);
400 /* Destination */
401 EXTRACT_HELPER(rD, 21, 5);
402 /* Source */
403 EXTRACT_HELPER(rS, 21, 5);
404 /* First operand */
405 EXTRACT_HELPER(rA, 16, 5);
406 /* Second operand */
407 EXTRACT_HELPER(rB, 11, 5);
408 /* Third operand */
409 EXTRACT_HELPER(rC, 6, 5);
410 /***                               Get CRn                                 ***/
411 EXTRACT_HELPER(crfD, 23, 3);
412 EXTRACT_HELPER(crfS, 18, 3);
413 EXTRACT_HELPER(crbD, 21, 5);
414 EXTRACT_HELPER(crbA, 16, 5);
415 EXTRACT_HELPER(crbB, 11, 5);
416 /* SPR / TBL */
417 EXTRACT_HELPER(_SPR, 11, 10);
418 static inline uint32_t SPR(uint32_t opcode)
419 {
420     uint32_t sprn = _SPR(opcode);
421
422     return ((sprn >> 5) & 0x1F) | ((sprn & 0x1F) << 5);
423 }
424 /***                              Get constants                            ***/
425 /* 16 bits signed immediate value */
426 EXTRACT_SHELPER(SIMM, 0, 16);
427 /* 16 bits unsigned immediate value */
428 EXTRACT_HELPER(UIMM, 0, 16);
429 /* 5 bits signed immediate value */
430 EXTRACT_HELPER(SIMM5, 16, 5);
431 /* 5 bits signed immediate value */
432 EXTRACT_HELPER(UIMM5, 16, 5);
433 /* Bit count */
434 EXTRACT_HELPER(NB, 11, 5);
435 /* Shift count */
436 EXTRACT_HELPER(SH, 11, 5);
437 /* Vector shift count */
438 EXTRACT_HELPER(VSH, 6, 4);
439 /* Mask start */
440 EXTRACT_HELPER(MB, 6, 5);
441 /* Mask end */
442 EXTRACT_HELPER(ME, 1, 5);
443 /* Trap operand */
444 EXTRACT_HELPER(TO, 21, 5);
445
446 EXTRACT_HELPER(CRM, 12, 8);
447 EXTRACT_HELPER(SR, 16, 4);
448
449 /* mtfsf/mtfsfi */
450 EXTRACT_HELPER(FPBF, 23, 3);
451 EXTRACT_HELPER(FPIMM, 12, 4);
452 EXTRACT_HELPER(FPL, 25, 1);
453 EXTRACT_HELPER(FPFLM, 17, 8);
454 EXTRACT_HELPER(FPW, 16, 1);
455
456 /***                            Jump target decoding                       ***/
457 /* Immediate address */
458 static inline target_ulong LI(uint32_t opcode)
459 {
460     return (opcode >> 0) & 0x03FFFFFC;
461 }
462
463 static inline uint32_t BD(uint32_t opcode)
464 {
465     return (opcode >> 0) & 0xFFFC;
466 }
467
468 EXTRACT_HELPER(BO, 21, 5);
469 EXTRACT_HELPER(BI, 16, 5);
470 /* Absolute/relative address */
471 EXTRACT_HELPER(AA, 1, 1);
472 /* Link */
473 EXTRACT_HELPER(LK, 0, 1);
474
475 /* DFP Z22-form */
476 EXTRACT_HELPER(DCM, 10, 6)
477
478 /* DFP Z23-form */
479 EXTRACT_HELPER(RMC, 9, 2)
480
481 /* Create a mask between <start> and <end> bits */
482 static inline target_ulong MASK(uint32_t start, uint32_t end)
483 {
484     target_ulong ret;
485
486 #if defined(TARGET_PPC64)
487     if (likely(start == 0)) {
488         ret = UINT64_MAX << (63 - end);
489     } else if (likely(end == 63)) {
490         ret = UINT64_MAX >> start;
491     }
492 #else
493     if (likely(start == 0)) {
494         ret = UINT32_MAX << (31  - end);
495     } else if (likely(end == 31)) {
496         ret = UINT32_MAX >> start;
497     }
498 #endif
499     else {
500         ret = (((target_ulong)(-1ULL)) >> (start)) ^
501             (((target_ulong)(-1ULL) >> (end)) >> 1);
502         if (unlikely(start > end))
503             return ~ret;
504     }
505
506     return ret;
507 }
508
509 EXTRACT_HELPER_SPLIT(xT, 0, 1, 21, 5);
510 EXTRACT_HELPER_SPLIT(xS, 0, 1, 21, 5);
511 EXTRACT_HELPER_SPLIT(xA, 2, 1, 16, 5);
512 EXTRACT_HELPER_SPLIT(xB, 1, 1, 11, 5);
513 EXTRACT_HELPER_SPLIT(xC, 3, 1,  6, 5);
514 EXTRACT_HELPER(DM, 8, 2);
515 EXTRACT_HELPER(UIM, 16, 2);
516 EXTRACT_HELPER(SHW, 8, 2);
517 EXTRACT_HELPER(SP, 19, 2);
518 /*****************************************************************************/
519 /* PowerPC instructions table                                                */
520
521 #if defined(DO_PPC_STATISTICS)
522 #define GEN_OPCODE(name, op1, op2, op3, invl, _typ, _typ2)                    \
523 {                                                                             \
524     .opc1 = op1,                                                              \
525     .opc2 = op2,                                                              \
526     .opc3 = op3,                                                              \
527     .pad  = { 0, },                                                           \
528     .handler = {                                                              \
529         .inval1  = invl,                                                      \
530         .type = _typ,                                                         \
531         .type2 = _typ2,                                                       \
532         .handler = &gen_##name,                                               \
533         .oname = stringify(name),                                             \
534     },                                                                        \
535     .oname = stringify(name),                                                 \
536 }
537 #define GEN_OPCODE_DUAL(name, op1, op2, op3, invl1, invl2, _typ, _typ2)       \
538 {                                                                             \
539     .opc1 = op1,                                                              \
540     .opc2 = op2,                                                              \
541     .opc3 = op3,                                                              \
542     .pad  = { 0, },                                                           \
543     .handler = {                                                              \
544         .inval1  = invl1,                                                     \
545         .inval2  = invl2,                                                     \
546         .type = _typ,                                                         \
547         .type2 = _typ2,                                                       \
548         .handler = &gen_##name,                                               \
549         .oname = stringify(name),                                             \
550     },                                                                        \
551     .oname = stringify(name),                                                 \
552 }
553 #define GEN_OPCODE2(name, onam, op1, op2, op3, invl, _typ, _typ2)             \
554 {                                                                             \
555     .opc1 = op1,                                                              \
556     .opc2 = op2,                                                              \
557     .opc3 = op3,                                                              \
558     .pad  = { 0, },                                                           \
559     .handler = {                                                              \
560         .inval1  = invl,                                                      \
561         .type = _typ,                                                         \
562         .type2 = _typ2,                                                       \
563         .handler = &gen_##name,                                               \
564         .oname = onam,                                                        \
565     },                                                                        \
566     .oname = onam,                                                            \
567 }
568 #else
569 #define GEN_OPCODE(name, op1, op2, op3, invl, _typ, _typ2)                    \
570 {                                                                             \
571     .opc1 = op1,                                                              \
572     .opc2 = op2,                                                              \
573     .opc3 = op3,                                                              \
574     .pad  = { 0, },                                                           \
575     .handler = {                                                              \
576         .inval1  = invl,                                                      \
577         .type = _typ,                                                         \
578         .type2 = _typ2,                                                       \
579         .handler = &gen_##name,                                               \
580     },                                                                        \
581     .oname = stringify(name),                                                 \
582 }
583 #define GEN_OPCODE_DUAL(name, op1, op2, op3, invl1, invl2, _typ, _typ2)       \
584 {                                                                             \
585     .opc1 = op1,                                                              \
586     .opc2 = op2,                                                              \
587     .opc3 = op3,                                                              \
588     .pad  = { 0, },                                                           \
589     .handler = {                                                              \
590         .inval1  = invl1,                                                     \
591         .inval2  = invl2,                                                     \
592         .type = _typ,                                                         \
593         .type2 = _typ2,                                                       \
594         .handler = &gen_##name,                                               \
595     },                                                                        \
596     .oname = stringify(name),                                                 \
597 }
598 #define GEN_OPCODE2(name, onam, op1, op2, op3, invl, _typ, _typ2)             \
599 {                                                                             \
600     .opc1 = op1,                                                              \
601     .opc2 = op2,                                                              \
602     .opc3 = op3,                                                              \
603     .pad  = { 0, },                                                           \
604     .handler = {                                                              \
605         .inval1  = invl,                                                      \
606         .type = _typ,                                                         \
607         .type2 = _typ2,                                                       \
608         .handler = &gen_##name,                                               \
609     },                                                                        \
610     .oname = onam,                                                            \
611 }
612 #endif
613
614 /* SPR load/store helpers */
615 static inline void gen_load_spr(TCGv t, int reg)
616 {
617     tcg_gen_ld_tl(t, cpu_env, offsetof(CPUPPCState, spr[reg]));
618 }
619
620 static inline void gen_store_spr(int reg, TCGv t)
621 {
622     tcg_gen_st_tl(t, cpu_env, offsetof(CPUPPCState, spr[reg]));
623 }
624
625 /* Invalid instruction */
626 static void gen_invalid(DisasContext *ctx)
627 {
628     gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL);
629 }
630
631 static opc_handler_t invalid_handler = {
632     .inval1  = 0xFFFFFFFF,
633     .inval2  = 0xFFFFFFFF,
634     .type    = PPC_NONE,
635     .type2   = PPC_NONE,
636     .handler = gen_invalid,
637 };
638
639 /***                           Integer comparison                          ***/
640
641 static inline void gen_op_cmp(TCGv arg0, TCGv arg1, int s, int crf)
642 {
643     TCGv t0 = tcg_temp_new();
644     TCGv_i32 t1 = tcg_temp_new_i32();
645
646     tcg_gen_trunc_tl_i32(cpu_crf[crf], cpu_so);
647
648     tcg_gen_setcond_tl((s ? TCG_COND_LT: TCG_COND_LTU), t0, arg0, arg1);
649     tcg_gen_trunc_tl_i32(t1, t0);
650     tcg_gen_shli_i32(t1, t1, CRF_LT);
651     tcg_gen_or_i32(cpu_crf[crf], cpu_crf[crf], t1);
652
653     tcg_gen_setcond_tl((s ? TCG_COND_GT: TCG_COND_GTU), t0, arg0, arg1);
654     tcg_gen_trunc_tl_i32(t1, t0);
655     tcg_gen_shli_i32(t1, t1, CRF_GT);
656     tcg_gen_or_i32(cpu_crf[crf], cpu_crf[crf], t1);
657
658     tcg_gen_setcond_tl(TCG_COND_EQ, t0, arg0, arg1);
659     tcg_gen_trunc_tl_i32(t1, t0);
660     tcg_gen_shli_i32(t1, t1, CRF_EQ);
661     tcg_gen_or_i32(cpu_crf[crf], cpu_crf[crf], t1);
662
663     tcg_temp_free(t0);
664     tcg_temp_free_i32(t1);
665 }
666
667 static inline void gen_op_cmpi(TCGv arg0, target_ulong arg1, int s, int crf)
668 {
669     TCGv t0 = tcg_const_tl(arg1);
670     gen_op_cmp(arg0, t0, s, crf);
671     tcg_temp_free(t0);
672 }
673
674 static inline void gen_op_cmp32(TCGv arg0, TCGv arg1, int s, int crf)
675 {
676     TCGv t0, t1;
677     t0 = tcg_temp_new();
678     t1 = tcg_temp_new();
679     if (s) {
680         tcg_gen_ext32s_tl(t0, arg0);
681         tcg_gen_ext32s_tl(t1, arg1);
682     } else {
683         tcg_gen_ext32u_tl(t0, arg0);
684         tcg_gen_ext32u_tl(t1, arg1);
685     }
686     gen_op_cmp(t0, t1, s, crf);
687     tcg_temp_free(t1);
688     tcg_temp_free(t0);
689 }
690
691 static inline void gen_op_cmpi32(TCGv arg0, target_ulong arg1, int s, int crf)
692 {
693     TCGv t0 = tcg_const_tl(arg1);
694     gen_op_cmp32(arg0, t0, s, crf);
695     tcg_temp_free(t0);
696 }
697
698 static inline void gen_set_Rc0(DisasContext *ctx, TCGv reg)
699 {
700     if (NARROW_MODE(ctx)) {
701         gen_op_cmpi32(reg, 0, 1, 0);
702     } else {
703         gen_op_cmpi(reg, 0, 1, 0);
704     }
705 }
706
707 /* cmp */
708 static void gen_cmp(DisasContext *ctx)
709 {
710     if ((ctx->opcode & 0x00200000) && (ctx->insns_flags & PPC_64B)) {
711         gen_op_cmp(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rB(ctx->opcode)],
712                    1, crfD(ctx->opcode));
713     } else {
714         gen_op_cmp32(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rB(ctx->opcode)],
715                      1, crfD(ctx->opcode));
716     }
717 }
718
719 /* cmpi */
720 static void gen_cmpi(DisasContext *ctx)
721 {
722     if ((ctx->opcode & 0x00200000) && (ctx->insns_flags & PPC_64B)) {
723         gen_op_cmpi(cpu_gpr[rA(ctx->opcode)], SIMM(ctx->opcode),
724                     1, crfD(ctx->opcode));
725     } else {
726         gen_op_cmpi32(cpu_gpr[rA(ctx->opcode)], SIMM(ctx->opcode),
727                       1, crfD(ctx->opcode));
728     }
729 }
730
731 /* cmpl */
732 static void gen_cmpl(DisasContext *ctx)
733 {
734     if ((ctx->opcode & 0x00200000) && (ctx->insns_flags & PPC_64B)) {
735         gen_op_cmp(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rB(ctx->opcode)],
736                    0, crfD(ctx->opcode));
737     } else {
738         gen_op_cmp32(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rB(ctx->opcode)],
739                      0, crfD(ctx->opcode));
740     }
741 }
742
743 /* cmpli */
744 static void gen_cmpli(DisasContext *ctx)
745 {
746     if ((ctx->opcode & 0x00200000) && (ctx->insns_flags & PPC_64B)) {
747         gen_op_cmpi(cpu_gpr[rA(ctx->opcode)], UIMM(ctx->opcode),
748                     0, crfD(ctx->opcode));
749     } else {
750         gen_op_cmpi32(cpu_gpr[rA(ctx->opcode)], UIMM(ctx->opcode),
751                       0, crfD(ctx->opcode));
752     }
753 }
754
755 /* isel (PowerPC 2.03 specification) */
756 static void gen_isel(DisasContext *ctx)
757 {
758     int l1, l2;
759     uint32_t bi = rC(ctx->opcode);
760     uint32_t mask;
761     TCGv_i32 t0;
762
763     l1 = gen_new_label();
764     l2 = gen_new_label();
765
766     mask = 0x08 >> (bi & 0x03);
767     t0 = tcg_temp_new_i32();
768     tcg_gen_andi_i32(t0, cpu_crf[bi >> 2], mask);
769     tcg_gen_brcondi_i32(TCG_COND_EQ, t0, 0, l1);
770     if (rA(ctx->opcode) == 0)
771         tcg_gen_movi_tl(cpu_gpr[rD(ctx->opcode)], 0);
772     else
773         tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)]);
774     tcg_gen_br(l2);
775     gen_set_label(l1);
776     tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rB(ctx->opcode)]);
777     gen_set_label(l2);
778     tcg_temp_free_i32(t0);
779 }
780
781 /* cmpb: PowerPC 2.05 specification */
782 static void gen_cmpb(DisasContext *ctx)
783 {
784     gen_helper_cmpb(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)],
785                     cpu_gpr[rB(ctx->opcode)]);
786 }
787
788 /***                           Integer arithmetic                          ***/
789
790 static inline void gen_op_arith_compute_ov(DisasContext *ctx, TCGv arg0,
791                                            TCGv arg1, TCGv arg2, int sub)
792 {
793     TCGv t0 = tcg_temp_new();
794
795     tcg_gen_xor_tl(cpu_ov, arg0, arg2);
796     tcg_gen_xor_tl(t0, arg1, arg2);
797     if (sub) {
798         tcg_gen_and_tl(cpu_ov, cpu_ov, t0);
799     } else {
800         tcg_gen_andc_tl(cpu_ov, cpu_ov, t0);
801     }
802     tcg_temp_free(t0);
803     if (NARROW_MODE(ctx)) {
804         tcg_gen_ext32s_tl(cpu_ov, cpu_ov);
805     }
806     tcg_gen_shri_tl(cpu_ov, cpu_ov, TARGET_LONG_BITS - 1);
807     tcg_gen_or_tl(cpu_so, cpu_so, cpu_ov);
808 }
809
810 /* Common add function */
811 static inline void gen_op_arith_add(DisasContext *ctx, TCGv ret, TCGv arg1,
812                                     TCGv arg2, bool add_ca, bool compute_ca,
813                                     bool compute_ov, bool compute_rc0)
814 {
815     TCGv t0 = ret;
816
817     if (compute_ca || compute_ov) {
818         t0 = tcg_temp_new();
819     }
820
821     if (compute_ca) {
822         if (NARROW_MODE(ctx)) {
823             /* Caution: a non-obvious corner case of the spec is that we
824                must produce the *entire* 64-bit addition, but produce the
825                carry into bit 32.  */
826             TCGv t1 = tcg_temp_new();
827             tcg_gen_xor_tl(t1, arg1, arg2);        /* add without carry */
828             tcg_gen_add_tl(t0, arg1, arg2);
829             if (add_ca) {
830                 tcg_gen_add_tl(t0, t0, cpu_ca);
831             }
832             tcg_gen_xor_tl(cpu_ca, t0, t1);        /* bits changed w/ carry */
833             tcg_temp_free(t1);
834             tcg_gen_shri_tl(cpu_ca, cpu_ca, 32);   /* extract bit 32 */
835             tcg_gen_andi_tl(cpu_ca, cpu_ca, 1);
836         } else {
837             TCGv zero = tcg_const_tl(0);
838             if (add_ca) {
839                 tcg_gen_add2_tl(t0, cpu_ca, arg1, zero, cpu_ca, zero);
840                 tcg_gen_add2_tl(t0, cpu_ca, t0, cpu_ca, arg2, zero);
841             } else {
842                 tcg_gen_add2_tl(t0, cpu_ca, arg1, zero, arg2, zero);
843             }
844             tcg_temp_free(zero);
845         }
846     } else {
847         tcg_gen_add_tl(t0, arg1, arg2);
848         if (add_ca) {
849             tcg_gen_add_tl(t0, t0, cpu_ca);
850         }
851     }
852
853     if (compute_ov) {
854         gen_op_arith_compute_ov(ctx, t0, arg1, arg2, 0);
855     }
856     if (unlikely(compute_rc0)) {
857         gen_set_Rc0(ctx, t0);
858     }
859
860     if (!TCGV_EQUAL(t0, ret)) {
861         tcg_gen_mov_tl(ret, t0);
862         tcg_temp_free(t0);
863     }
864 }
865 /* Add functions with two operands */
866 #define GEN_INT_ARITH_ADD(name, opc3, add_ca, compute_ca, compute_ov)         \
867 static void glue(gen_, name)(DisasContext *ctx)                               \
868 {                                                                             \
869     gen_op_arith_add(ctx, cpu_gpr[rD(ctx->opcode)],                           \
870                      cpu_gpr[rA(ctx->opcode)], cpu_gpr[rB(ctx->opcode)],      \
871                      add_ca, compute_ca, compute_ov, Rc(ctx->opcode));        \
872 }
873 /* Add functions with one operand and one immediate */
874 #define GEN_INT_ARITH_ADD_CONST(name, opc3, const_val,                        \
875                                 add_ca, compute_ca, compute_ov)               \
876 static void glue(gen_, name)(DisasContext *ctx)                               \
877 {                                                                             \
878     TCGv t0 = tcg_const_tl(const_val);                                        \
879     gen_op_arith_add(ctx, cpu_gpr[rD(ctx->opcode)],                           \
880                      cpu_gpr[rA(ctx->opcode)], t0,                            \
881                      add_ca, compute_ca, compute_ov, Rc(ctx->opcode));        \
882     tcg_temp_free(t0);                                                        \
883 }
884
885 /* add  add.  addo  addo. */
886 GEN_INT_ARITH_ADD(add, 0x08, 0, 0, 0)
887 GEN_INT_ARITH_ADD(addo, 0x18, 0, 0, 1)
888 /* addc  addc.  addco  addco. */
889 GEN_INT_ARITH_ADD(addc, 0x00, 0, 1, 0)
890 GEN_INT_ARITH_ADD(addco, 0x10, 0, 1, 1)
891 /* adde  adde.  addeo  addeo. */
892 GEN_INT_ARITH_ADD(adde, 0x04, 1, 1, 0)
893 GEN_INT_ARITH_ADD(addeo, 0x14, 1, 1, 1)
894 /* addme  addme.  addmeo  addmeo.  */
895 GEN_INT_ARITH_ADD_CONST(addme, 0x07, -1LL, 1, 1, 0)
896 GEN_INT_ARITH_ADD_CONST(addmeo, 0x17, -1LL, 1, 1, 1)
897 /* addze  addze.  addzeo  addzeo.*/
898 GEN_INT_ARITH_ADD_CONST(addze, 0x06, 0, 1, 1, 0)
899 GEN_INT_ARITH_ADD_CONST(addzeo, 0x16, 0, 1, 1, 1)
900 /* addi */
901 static void gen_addi(DisasContext *ctx)
902 {
903     target_long simm = SIMM(ctx->opcode);
904
905     if (rA(ctx->opcode) == 0) {
906         /* li case */
907         tcg_gen_movi_tl(cpu_gpr[rD(ctx->opcode)], simm);
908     } else {
909         tcg_gen_addi_tl(cpu_gpr[rD(ctx->opcode)],
910                         cpu_gpr[rA(ctx->opcode)], simm);
911     }
912 }
913 /* addic  addic.*/
914 static inline void gen_op_addic(DisasContext *ctx, bool compute_rc0)
915 {
916     TCGv c = tcg_const_tl(SIMM(ctx->opcode));
917     gen_op_arith_add(ctx, cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)],
918                      c, 0, 1, 0, compute_rc0);
919     tcg_temp_free(c);
920 }
921
922 static void gen_addic(DisasContext *ctx)
923 {
924     gen_op_addic(ctx, 0);
925 }
926
927 static void gen_addic_(DisasContext *ctx)
928 {
929     gen_op_addic(ctx, 1);
930 }
931
932 /* addis */
933 static void gen_addis(DisasContext *ctx)
934 {
935     target_long simm = SIMM(ctx->opcode);
936
937     if (rA(ctx->opcode) == 0) {
938         /* lis case */
939         tcg_gen_movi_tl(cpu_gpr[rD(ctx->opcode)], simm << 16);
940     } else {
941         tcg_gen_addi_tl(cpu_gpr[rD(ctx->opcode)],
942                         cpu_gpr[rA(ctx->opcode)], simm << 16);
943     }
944 }
945
946 static inline void gen_op_arith_divw(DisasContext *ctx, TCGv ret, TCGv arg1,
947                                      TCGv arg2, int sign, int compute_ov)
948 {
949     int l1 = gen_new_label();
950     int l2 = gen_new_label();
951     TCGv_i32 t0 = tcg_temp_local_new_i32();
952     TCGv_i32 t1 = tcg_temp_local_new_i32();
953
954     tcg_gen_trunc_tl_i32(t0, arg1);
955     tcg_gen_trunc_tl_i32(t1, arg2);
956     tcg_gen_brcondi_i32(TCG_COND_EQ, t1, 0, l1);
957     if (sign) {
958         int l3 = gen_new_label();
959         tcg_gen_brcondi_i32(TCG_COND_NE, t1, -1, l3);
960         tcg_gen_brcondi_i32(TCG_COND_EQ, t0, INT32_MIN, l1);
961         gen_set_label(l3);
962         tcg_gen_div_i32(t0, t0, t1);
963     } else {
964         tcg_gen_divu_i32(t0, t0, t1);
965     }
966     if (compute_ov) {
967         tcg_gen_movi_tl(cpu_ov, 0);
968     }
969     tcg_gen_br(l2);
970     gen_set_label(l1);
971     if (sign) {
972         tcg_gen_sari_i32(t0, t0, 31);
973     } else {
974         tcg_gen_movi_i32(t0, 0);
975     }
976     if (compute_ov) {
977         tcg_gen_movi_tl(cpu_ov, 1);
978         tcg_gen_movi_tl(cpu_so, 1);
979     }
980     gen_set_label(l2);
981     tcg_gen_extu_i32_tl(ret, t0);
982     tcg_temp_free_i32(t0);
983     tcg_temp_free_i32(t1);
984     if (unlikely(Rc(ctx->opcode) != 0))
985         gen_set_Rc0(ctx, ret);
986 }
987 /* Div functions */
988 #define GEN_INT_ARITH_DIVW(name, opc3, sign, compute_ov)                      \
989 static void glue(gen_, name)(DisasContext *ctx)                                       \
990 {                                                                             \
991     gen_op_arith_divw(ctx, cpu_gpr[rD(ctx->opcode)],                          \
992                      cpu_gpr[rA(ctx->opcode)], cpu_gpr[rB(ctx->opcode)],      \
993                      sign, compute_ov);                                       \
994 }
995 /* divwu  divwu.  divwuo  divwuo.   */
996 GEN_INT_ARITH_DIVW(divwu, 0x0E, 0, 0);
997 GEN_INT_ARITH_DIVW(divwuo, 0x1E, 0, 1);
998 /* divw  divw.  divwo  divwo.   */
999 GEN_INT_ARITH_DIVW(divw, 0x0F, 1, 0);
1000 GEN_INT_ARITH_DIVW(divwo, 0x1F, 1, 1);
1001
1002 /* div[wd]eu[o][.] */
1003 #define GEN_DIVE(name, hlpr, compute_ov)                                      \
1004 static void gen_##name(DisasContext *ctx)                                     \
1005 {                                                                             \
1006     TCGv_i32 t0 = tcg_const_i32(compute_ov);                                  \
1007     gen_helper_##hlpr(cpu_gpr[rD(ctx->opcode)], cpu_env,                      \
1008                      cpu_gpr[rA(ctx->opcode)], cpu_gpr[rB(ctx->opcode)], t0); \
1009     tcg_temp_free_i32(t0);                                                    \
1010     if (unlikely(Rc(ctx->opcode) != 0)) {                                     \
1011         gen_set_Rc0(ctx, cpu_gpr[rD(ctx->opcode)]);                           \
1012     }                                                                         \
1013 }
1014
1015 GEN_DIVE(divweu, divweu, 0);
1016 GEN_DIVE(divweuo, divweu, 1);
1017 GEN_DIVE(divwe, divwe, 0);
1018 GEN_DIVE(divweo, divwe, 1);
1019
1020 #if defined(TARGET_PPC64)
1021 static inline void gen_op_arith_divd(DisasContext *ctx, TCGv ret, TCGv arg1,
1022                                      TCGv arg2, int sign, int compute_ov)
1023 {
1024     int l1 = gen_new_label();
1025     int l2 = gen_new_label();
1026
1027     tcg_gen_brcondi_i64(TCG_COND_EQ, arg2, 0, l1);
1028     if (sign) {
1029         int l3 = gen_new_label();
1030         tcg_gen_brcondi_i64(TCG_COND_NE, arg2, -1, l3);
1031         tcg_gen_brcondi_i64(TCG_COND_EQ, arg1, INT64_MIN, l1);
1032         gen_set_label(l3);
1033         tcg_gen_div_i64(ret, arg1, arg2);
1034     } else {
1035         tcg_gen_divu_i64(ret, arg1, arg2);
1036     }
1037     if (compute_ov) {
1038         tcg_gen_movi_tl(cpu_ov, 0);
1039     }
1040     tcg_gen_br(l2);
1041     gen_set_label(l1);
1042     if (sign) {
1043         tcg_gen_sari_i64(ret, arg1, 63);
1044     } else {
1045         tcg_gen_movi_i64(ret, 0);
1046     }
1047     if (compute_ov) {
1048         tcg_gen_movi_tl(cpu_ov, 1);
1049         tcg_gen_movi_tl(cpu_so, 1);
1050     }
1051     gen_set_label(l2);
1052     if (unlikely(Rc(ctx->opcode) != 0))
1053         gen_set_Rc0(ctx, ret);
1054 }
1055 #define GEN_INT_ARITH_DIVD(name, opc3, sign, compute_ov)                      \
1056 static void glue(gen_, name)(DisasContext *ctx)                                       \
1057 {                                                                             \
1058     gen_op_arith_divd(ctx, cpu_gpr[rD(ctx->opcode)],                          \
1059                       cpu_gpr[rA(ctx->opcode)], cpu_gpr[rB(ctx->opcode)],     \
1060                       sign, compute_ov);                                      \
1061 }
1062 /* divwu  divwu.  divwuo  divwuo.   */
1063 GEN_INT_ARITH_DIVD(divdu, 0x0E, 0, 0);
1064 GEN_INT_ARITH_DIVD(divduo, 0x1E, 0, 1);
1065 /* divw  divw.  divwo  divwo.   */
1066 GEN_INT_ARITH_DIVD(divd, 0x0F, 1, 0);
1067 GEN_INT_ARITH_DIVD(divdo, 0x1F, 1, 1);
1068
1069 GEN_DIVE(divdeu, divdeu, 0);
1070 GEN_DIVE(divdeuo, divdeu, 1);
1071 GEN_DIVE(divde, divde, 0);
1072 GEN_DIVE(divdeo, divde, 1);
1073 #endif
1074
1075 /* mulhw  mulhw. */
1076 static void gen_mulhw(DisasContext *ctx)
1077 {
1078     TCGv_i32 t0 = tcg_temp_new_i32();
1079     TCGv_i32 t1 = tcg_temp_new_i32();
1080
1081     tcg_gen_trunc_tl_i32(t0, cpu_gpr[rA(ctx->opcode)]);
1082     tcg_gen_trunc_tl_i32(t1, cpu_gpr[rB(ctx->opcode)]);
1083     tcg_gen_muls2_i32(t0, t1, t0, t1);
1084     tcg_gen_extu_i32_tl(cpu_gpr[rD(ctx->opcode)], t1);
1085     tcg_temp_free_i32(t0);
1086     tcg_temp_free_i32(t1);
1087     if (unlikely(Rc(ctx->opcode) != 0))
1088         gen_set_Rc0(ctx, cpu_gpr[rD(ctx->opcode)]);
1089 }
1090
1091 /* mulhwu  mulhwu.  */
1092 static void gen_mulhwu(DisasContext *ctx)
1093 {
1094     TCGv_i32 t0 = tcg_temp_new_i32();
1095     TCGv_i32 t1 = tcg_temp_new_i32();
1096
1097     tcg_gen_trunc_tl_i32(t0, cpu_gpr[rA(ctx->opcode)]);
1098     tcg_gen_trunc_tl_i32(t1, cpu_gpr[rB(ctx->opcode)]);
1099     tcg_gen_mulu2_i32(t0, t1, t0, t1);
1100     tcg_gen_extu_i32_tl(cpu_gpr[rD(ctx->opcode)], t1);
1101     tcg_temp_free_i32(t0);
1102     tcg_temp_free_i32(t1);
1103     if (unlikely(Rc(ctx->opcode) != 0))
1104         gen_set_Rc0(ctx, cpu_gpr[rD(ctx->opcode)]);
1105 }
1106
1107 /* mullw  mullw. */
1108 static void gen_mullw(DisasContext *ctx)
1109 {
1110 #if defined(TARGET_PPC64)
1111     TCGv_i64 t0, t1;
1112     t0 = tcg_temp_new_i64();
1113     t1 = tcg_temp_new_i64();
1114     tcg_gen_ext32s_tl(t0, cpu_gpr[rA(ctx->opcode)]);
1115     tcg_gen_ext32s_tl(t1, cpu_gpr[rB(ctx->opcode)]);
1116     tcg_gen_mul_i64(cpu_gpr[rD(ctx->opcode)], t0, t1);
1117     tcg_temp_free(t0);
1118     tcg_temp_free(t1);
1119 #else
1120     tcg_gen_mul_i32(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)],
1121                     cpu_gpr[rB(ctx->opcode)]);
1122 #endif
1123     if (unlikely(Rc(ctx->opcode) != 0))
1124         gen_set_Rc0(ctx, cpu_gpr[rD(ctx->opcode)]);
1125 }
1126
1127 /* mullwo  mullwo. */
1128 static void gen_mullwo(DisasContext *ctx)
1129 {
1130     TCGv_i32 t0 = tcg_temp_new_i32();
1131     TCGv_i32 t1 = tcg_temp_new_i32();
1132
1133     tcg_gen_trunc_tl_i32(t0, cpu_gpr[rA(ctx->opcode)]);
1134     tcg_gen_trunc_tl_i32(t1, cpu_gpr[rB(ctx->opcode)]);
1135     tcg_gen_muls2_i32(t0, t1, t0, t1);
1136 #if defined(TARGET_PPC64)
1137     tcg_gen_concat_i32_i64(cpu_gpr[rD(ctx->opcode)], t0, t1);
1138 #else
1139     tcg_gen_mov_i32(cpu_gpr[rD(ctx->opcode)], t0);
1140 #endif
1141
1142     tcg_gen_sari_i32(t0, t0, 31);
1143     tcg_gen_setcond_i32(TCG_COND_NE, t0, t0, t1);
1144     tcg_gen_extu_i32_tl(cpu_ov, t0);
1145     tcg_gen_or_tl(cpu_so, cpu_so, cpu_ov);
1146
1147     tcg_temp_free_i32(t0);
1148     tcg_temp_free_i32(t1);
1149     if (unlikely(Rc(ctx->opcode) != 0))
1150         gen_set_Rc0(ctx, cpu_gpr[rD(ctx->opcode)]);
1151 }
1152
1153 /* mulli */
1154 static void gen_mulli(DisasContext *ctx)
1155 {
1156     tcg_gen_muli_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)],
1157                     SIMM(ctx->opcode));
1158 }
1159
1160 #if defined(TARGET_PPC64)
1161 /* mulhd  mulhd. */
1162 static void gen_mulhd(DisasContext *ctx)
1163 {
1164     TCGv lo = tcg_temp_new();
1165     tcg_gen_muls2_tl(lo, cpu_gpr[rD(ctx->opcode)],
1166                      cpu_gpr[rA(ctx->opcode)], cpu_gpr[rB(ctx->opcode)]);
1167     tcg_temp_free(lo);
1168     if (unlikely(Rc(ctx->opcode) != 0)) {
1169         gen_set_Rc0(ctx, cpu_gpr[rD(ctx->opcode)]);
1170     }
1171 }
1172
1173 /* mulhdu  mulhdu. */
1174 static void gen_mulhdu(DisasContext *ctx)
1175 {
1176     TCGv lo = tcg_temp_new();
1177     tcg_gen_mulu2_tl(lo, cpu_gpr[rD(ctx->opcode)],
1178                      cpu_gpr[rA(ctx->opcode)], cpu_gpr[rB(ctx->opcode)]);
1179     tcg_temp_free(lo);
1180     if (unlikely(Rc(ctx->opcode) != 0)) {
1181         gen_set_Rc0(ctx, cpu_gpr[rD(ctx->opcode)]);
1182     }
1183 }
1184
1185 /* mulld  mulld. */
1186 static void gen_mulld(DisasContext *ctx)
1187 {
1188     tcg_gen_mul_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)],
1189                    cpu_gpr[rB(ctx->opcode)]);
1190     if (unlikely(Rc(ctx->opcode) != 0))
1191         gen_set_Rc0(ctx, cpu_gpr[rD(ctx->opcode)]);
1192 }
1193
1194 /* mulldo  mulldo. */
1195 static void gen_mulldo(DisasContext *ctx)
1196 {
1197     TCGv_i64 t0 = tcg_temp_new_i64();
1198     TCGv_i64 t1 = tcg_temp_new_i64();
1199
1200     tcg_gen_muls2_i64(t0, t1, cpu_gpr[rA(ctx->opcode)],
1201                       cpu_gpr[rB(ctx->opcode)]);
1202     tcg_gen_mov_i64(cpu_gpr[rD(ctx->opcode)], t0);
1203
1204     tcg_gen_sari_i64(t0, t0, 63);
1205     tcg_gen_setcond_i64(TCG_COND_NE, cpu_ov, t0, t1);
1206     tcg_gen_or_tl(cpu_so, cpu_so, cpu_ov);
1207
1208     tcg_temp_free_i64(t0);
1209     tcg_temp_free_i64(t1);
1210
1211     if (unlikely(Rc(ctx->opcode) != 0)) {
1212         gen_set_Rc0(ctx, cpu_gpr[rD(ctx->opcode)]);
1213     }
1214 }
1215 #endif
1216
1217 /* Common subf function */
1218 static inline void gen_op_arith_subf(DisasContext *ctx, TCGv ret, TCGv arg1,
1219                                      TCGv arg2, bool add_ca, bool compute_ca,
1220                                      bool compute_ov, bool compute_rc0)
1221 {
1222     TCGv t0 = ret;
1223
1224     if (compute_ca || compute_ov) {
1225         t0 = tcg_temp_new();
1226     }
1227
1228     if (compute_ca) {
1229         /* dest = ~arg1 + arg2 [+ ca].  */
1230         if (NARROW_MODE(ctx)) {
1231             /* Caution: a non-obvious corner case of the spec is that we
1232                must produce the *entire* 64-bit addition, but produce the
1233                carry into bit 32.  */
1234             TCGv inv1 = tcg_temp_new();
1235             TCGv t1 = tcg_temp_new();
1236             tcg_gen_not_tl(inv1, arg1);
1237             if (add_ca) {
1238                 tcg_gen_add_tl(t0, arg2, cpu_ca);
1239             } else {
1240                 tcg_gen_addi_tl(t0, arg2, 1);
1241             }
1242             tcg_gen_xor_tl(t1, arg2, inv1);         /* add without carry */
1243             tcg_gen_add_tl(t0, t0, inv1);
1244             tcg_temp_free(inv1);
1245             tcg_gen_xor_tl(cpu_ca, t0, t1);         /* bits changes w/ carry */
1246             tcg_temp_free(t1);
1247             tcg_gen_shri_tl(cpu_ca, cpu_ca, 32);    /* extract bit 32 */
1248             tcg_gen_andi_tl(cpu_ca, cpu_ca, 1);
1249         } else if (add_ca) {
1250             TCGv zero, inv1 = tcg_temp_new();
1251             tcg_gen_not_tl(inv1, arg1);
1252             zero = tcg_const_tl(0);
1253             tcg_gen_add2_tl(t0, cpu_ca, arg2, zero, cpu_ca, zero);
1254             tcg_gen_add2_tl(t0, cpu_ca, t0, cpu_ca, inv1, zero);
1255             tcg_temp_free(zero);
1256             tcg_temp_free(inv1);
1257         } else {
1258             tcg_gen_setcond_tl(TCG_COND_GEU, cpu_ca, arg2, arg1);
1259             tcg_gen_sub_tl(t0, arg2, arg1);
1260         }
1261     } else if (add_ca) {
1262         /* Since we're ignoring carry-out, we can simplify the
1263            standard ~arg1 + arg2 + ca to arg2 - arg1 + ca - 1.  */
1264         tcg_gen_sub_tl(t0, arg2, arg1);
1265         tcg_gen_add_tl(t0, t0, cpu_ca);
1266         tcg_gen_subi_tl(t0, t0, 1);
1267     } else {
1268         tcg_gen_sub_tl(t0, arg2, arg1);
1269     }
1270
1271     if (compute_ov) {
1272         gen_op_arith_compute_ov(ctx, t0, arg1, arg2, 1);
1273     }
1274     if (unlikely(compute_rc0)) {
1275         gen_set_Rc0(ctx, t0);
1276     }
1277
1278     if (!TCGV_EQUAL(t0, ret)) {
1279         tcg_gen_mov_tl(ret, t0);
1280         tcg_temp_free(t0);
1281     }
1282 }
1283 /* Sub functions with Two operands functions */
1284 #define GEN_INT_ARITH_SUBF(name, opc3, add_ca, compute_ca, compute_ov)        \
1285 static void glue(gen_, name)(DisasContext *ctx)                               \
1286 {                                                                             \
1287     gen_op_arith_subf(ctx, cpu_gpr[rD(ctx->opcode)],                          \
1288                       cpu_gpr[rA(ctx->opcode)], cpu_gpr[rB(ctx->opcode)],     \
1289                       add_ca, compute_ca, compute_ov, Rc(ctx->opcode));       \
1290 }
1291 /* Sub functions with one operand and one immediate */
1292 #define GEN_INT_ARITH_SUBF_CONST(name, opc3, const_val,                       \
1293                                 add_ca, compute_ca, compute_ov)               \
1294 static void glue(gen_, name)(DisasContext *ctx)                               \
1295 {                                                                             \
1296     TCGv t0 = tcg_const_tl(const_val);                                        \
1297     gen_op_arith_subf(ctx, cpu_gpr[rD(ctx->opcode)],                          \
1298                       cpu_gpr[rA(ctx->opcode)], t0,                           \
1299                       add_ca, compute_ca, compute_ov, Rc(ctx->opcode));       \
1300     tcg_temp_free(t0);                                                        \
1301 }
1302 /* subf  subf.  subfo  subfo. */
1303 GEN_INT_ARITH_SUBF(subf, 0x01, 0, 0, 0)
1304 GEN_INT_ARITH_SUBF(subfo, 0x11, 0, 0, 1)
1305 /* subfc  subfc.  subfco  subfco. */
1306 GEN_INT_ARITH_SUBF(subfc, 0x00, 0, 1, 0)
1307 GEN_INT_ARITH_SUBF(subfco, 0x10, 0, 1, 1)
1308 /* subfe  subfe.  subfeo  subfo. */
1309 GEN_INT_ARITH_SUBF(subfe, 0x04, 1, 1, 0)
1310 GEN_INT_ARITH_SUBF(subfeo, 0x14, 1, 1, 1)
1311 /* subfme  subfme.  subfmeo  subfmeo.  */
1312 GEN_INT_ARITH_SUBF_CONST(subfme, 0x07, -1LL, 1, 1, 0)
1313 GEN_INT_ARITH_SUBF_CONST(subfmeo, 0x17, -1LL, 1, 1, 1)
1314 /* subfze  subfze.  subfzeo  subfzeo.*/
1315 GEN_INT_ARITH_SUBF_CONST(subfze, 0x06, 0, 1, 1, 0)
1316 GEN_INT_ARITH_SUBF_CONST(subfzeo, 0x16, 0, 1, 1, 1)
1317
1318 /* subfic */
1319 static void gen_subfic(DisasContext *ctx)
1320 {
1321     TCGv c = tcg_const_tl(SIMM(ctx->opcode));
1322     gen_op_arith_subf(ctx, cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)],
1323                       c, 0, 1, 0, 0);
1324     tcg_temp_free(c);
1325 }
1326
1327 /* neg neg. nego nego. */
1328 static inline void gen_op_arith_neg(DisasContext *ctx, bool compute_ov)
1329 {
1330     TCGv zero = tcg_const_tl(0);
1331     gen_op_arith_subf(ctx, cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)],
1332                       zero, 0, 0, compute_ov, Rc(ctx->opcode));
1333     tcg_temp_free(zero);
1334 }
1335
1336 static void gen_neg(DisasContext *ctx)
1337 {
1338     gen_op_arith_neg(ctx, 0);
1339 }
1340
1341 static void gen_nego(DisasContext *ctx)
1342 {
1343     gen_op_arith_neg(ctx, 1);
1344 }
1345
1346 /***                            Integer logical                            ***/
1347 #define GEN_LOGICAL2(name, tcg_op, opc, type)                                 \
1348 static void glue(gen_, name)(DisasContext *ctx)                                       \
1349 {                                                                             \
1350     tcg_op(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)],                \
1351        cpu_gpr[rB(ctx->opcode)]);                                             \
1352     if (unlikely(Rc(ctx->opcode) != 0))                                       \
1353         gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);                           \
1354 }
1355
1356 #define GEN_LOGICAL1(name, tcg_op, opc, type)                                 \
1357 static void glue(gen_, name)(DisasContext *ctx)                                       \
1358 {                                                                             \
1359     tcg_op(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)]);               \
1360     if (unlikely(Rc(ctx->opcode) != 0))                                       \
1361         gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);                           \
1362 }
1363
1364 /* and & and. */
1365 GEN_LOGICAL2(and, tcg_gen_and_tl, 0x00, PPC_INTEGER);
1366 /* andc & andc. */
1367 GEN_LOGICAL2(andc, tcg_gen_andc_tl, 0x01, PPC_INTEGER);
1368
1369 /* andi. */
1370 static void gen_andi_(DisasContext *ctx)
1371 {
1372     tcg_gen_andi_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)], UIMM(ctx->opcode));
1373     gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
1374 }
1375
1376 /* andis. */
1377 static void gen_andis_(DisasContext *ctx)
1378 {
1379     tcg_gen_andi_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)], UIMM(ctx->opcode) << 16);
1380     gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
1381 }
1382
1383 /* cntlzw */
1384 static void gen_cntlzw(DisasContext *ctx)
1385 {
1386     gen_helper_cntlzw(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)]);
1387     if (unlikely(Rc(ctx->opcode) != 0))
1388         gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
1389 }
1390 /* eqv & eqv. */
1391 GEN_LOGICAL2(eqv, tcg_gen_eqv_tl, 0x08, PPC_INTEGER);
1392 /* extsb & extsb. */
1393 GEN_LOGICAL1(extsb, tcg_gen_ext8s_tl, 0x1D, PPC_INTEGER);
1394 /* extsh & extsh. */
1395 GEN_LOGICAL1(extsh, tcg_gen_ext16s_tl, 0x1C, PPC_INTEGER);
1396 /* nand & nand. */
1397 GEN_LOGICAL2(nand, tcg_gen_nand_tl, 0x0E, PPC_INTEGER);
1398 /* nor & nor. */
1399 GEN_LOGICAL2(nor, tcg_gen_nor_tl, 0x03, PPC_INTEGER);
1400
1401 /* or & or. */
1402 static void gen_or(DisasContext *ctx)
1403 {
1404     int rs, ra, rb;
1405
1406     rs = rS(ctx->opcode);
1407     ra = rA(ctx->opcode);
1408     rb = rB(ctx->opcode);
1409     /* Optimisation for mr. ri case */
1410     if (rs != ra || rs != rb) {
1411         if (rs != rb)
1412             tcg_gen_or_tl(cpu_gpr[ra], cpu_gpr[rs], cpu_gpr[rb]);
1413         else
1414             tcg_gen_mov_tl(cpu_gpr[ra], cpu_gpr[rs]);
1415         if (unlikely(Rc(ctx->opcode) != 0))
1416             gen_set_Rc0(ctx, cpu_gpr[ra]);
1417     } else if (unlikely(Rc(ctx->opcode) != 0)) {
1418         gen_set_Rc0(ctx, cpu_gpr[rs]);
1419 #if defined(TARGET_PPC64)
1420     } else {
1421         int prio = 0;
1422
1423         switch (rs) {
1424         case 1:
1425             /* Set process priority to low */
1426             prio = 2;
1427             break;
1428         case 6:
1429             /* Set process priority to medium-low */
1430             prio = 3;
1431             break;
1432         case 2:
1433             /* Set process priority to normal */
1434             prio = 4;
1435             break;
1436 #if !defined(CONFIG_USER_ONLY)
1437         case 31:
1438             if (!ctx->pr) {
1439                 /* Set process priority to very low */
1440                 prio = 1;
1441             }
1442             break;
1443         case 5:
1444             if (!ctx->pr) {
1445                 /* Set process priority to medium-hight */
1446                 prio = 5;
1447             }
1448             break;
1449         case 3:
1450             if (!ctx->pr) {
1451                 /* Set process priority to high */
1452                 prio = 6;
1453             }
1454             break;
1455         case 7:
1456             if (ctx->hv) {
1457                 /* Set process priority to very high */
1458                 prio = 7;
1459             }
1460             break;
1461 #endif
1462         default:
1463             /* nop */
1464             break;
1465         }
1466         if (prio) {
1467             TCGv t0 = tcg_temp_new();
1468             gen_load_spr(t0, SPR_PPR);
1469             tcg_gen_andi_tl(t0, t0, ~0x001C000000000000ULL);
1470             tcg_gen_ori_tl(t0, t0, ((uint64_t)prio) << 50);
1471             gen_store_spr(SPR_PPR, t0);
1472             tcg_temp_free(t0);
1473         }
1474 #endif
1475     }
1476 }
1477 /* orc & orc. */
1478 GEN_LOGICAL2(orc, tcg_gen_orc_tl, 0x0C, PPC_INTEGER);
1479
1480 /* xor & xor. */
1481 static void gen_xor(DisasContext *ctx)
1482 {
1483     /* Optimisation for "set to zero" case */
1484     if (rS(ctx->opcode) != rB(ctx->opcode))
1485         tcg_gen_xor_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)], cpu_gpr[rB(ctx->opcode)]);
1486     else
1487         tcg_gen_movi_tl(cpu_gpr[rA(ctx->opcode)], 0);
1488     if (unlikely(Rc(ctx->opcode) != 0))
1489         gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
1490 }
1491
1492 /* ori */
1493 static void gen_ori(DisasContext *ctx)
1494 {
1495     target_ulong uimm = UIMM(ctx->opcode);
1496
1497     if (rS(ctx->opcode) == rA(ctx->opcode) && uimm == 0) {
1498         /* NOP */
1499         /* XXX: should handle special NOPs for POWER series */
1500         return;
1501     }
1502     tcg_gen_ori_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)], uimm);
1503 }
1504
1505 /* oris */
1506 static void gen_oris(DisasContext *ctx)
1507 {
1508     target_ulong uimm = UIMM(ctx->opcode);
1509
1510     if (rS(ctx->opcode) == rA(ctx->opcode) && uimm == 0) {
1511         /* NOP */
1512         return;
1513     }
1514     tcg_gen_ori_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)], uimm << 16);
1515 }
1516
1517 /* xori */
1518 static void gen_xori(DisasContext *ctx)
1519 {
1520     target_ulong uimm = UIMM(ctx->opcode);
1521
1522     if (rS(ctx->opcode) == rA(ctx->opcode) && uimm == 0) {
1523         /* NOP */
1524         return;
1525     }
1526     tcg_gen_xori_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)], uimm);
1527 }
1528
1529 /* xoris */
1530 static void gen_xoris(DisasContext *ctx)
1531 {
1532     target_ulong uimm = UIMM(ctx->opcode);
1533
1534     if (rS(ctx->opcode) == rA(ctx->opcode) && uimm == 0) {
1535         /* NOP */
1536         return;
1537     }
1538     tcg_gen_xori_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)], uimm << 16);
1539 }
1540
1541 /* popcntb : PowerPC 2.03 specification */
1542 static void gen_popcntb(DisasContext *ctx)
1543 {
1544     gen_helper_popcntb(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)]);
1545 }
1546
1547 static void gen_popcntw(DisasContext *ctx)
1548 {
1549     gen_helper_popcntw(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)]);
1550 }
1551
1552 #if defined(TARGET_PPC64)
1553 /* popcntd: PowerPC 2.06 specification */
1554 static void gen_popcntd(DisasContext *ctx)
1555 {
1556     gen_helper_popcntd(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)]);
1557 }
1558 #endif
1559
1560 /* prtyw: PowerPC 2.05 specification */
1561 static void gen_prtyw(DisasContext *ctx)
1562 {
1563     TCGv ra = cpu_gpr[rA(ctx->opcode)];
1564     TCGv rs = cpu_gpr[rS(ctx->opcode)];
1565     TCGv t0 = tcg_temp_new();
1566     tcg_gen_shri_tl(t0, rs, 16);
1567     tcg_gen_xor_tl(ra, rs, t0);
1568     tcg_gen_shri_tl(t0, ra, 8);
1569     tcg_gen_xor_tl(ra, ra, t0);
1570     tcg_gen_andi_tl(ra, ra, (target_ulong)0x100000001ULL);
1571     tcg_temp_free(t0);
1572 }
1573
1574 #if defined(TARGET_PPC64)
1575 /* prtyd: PowerPC 2.05 specification */
1576 static void gen_prtyd(DisasContext *ctx)
1577 {
1578     TCGv ra = cpu_gpr[rA(ctx->opcode)];
1579     TCGv rs = cpu_gpr[rS(ctx->opcode)];
1580     TCGv t0 = tcg_temp_new();
1581     tcg_gen_shri_tl(t0, rs, 32);
1582     tcg_gen_xor_tl(ra, rs, t0);
1583     tcg_gen_shri_tl(t0, ra, 16);
1584     tcg_gen_xor_tl(ra, ra, t0);
1585     tcg_gen_shri_tl(t0, ra, 8);
1586     tcg_gen_xor_tl(ra, ra, t0);
1587     tcg_gen_andi_tl(ra, ra, 1);
1588     tcg_temp_free(t0);
1589 }
1590 #endif
1591
1592 #if defined(TARGET_PPC64)
1593 /* bpermd */
1594 static void gen_bpermd(DisasContext *ctx)
1595 {
1596     gen_helper_bpermd(cpu_gpr[rA(ctx->opcode)],
1597                       cpu_gpr[rS(ctx->opcode)], cpu_gpr[rB(ctx->opcode)]);
1598 }
1599 #endif
1600
1601 #if defined(TARGET_PPC64)
1602 /* extsw & extsw. */
1603 GEN_LOGICAL1(extsw, tcg_gen_ext32s_tl, 0x1E, PPC_64B);
1604
1605 /* cntlzd */
1606 static void gen_cntlzd(DisasContext *ctx)
1607 {
1608     gen_helper_cntlzd(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)]);
1609     if (unlikely(Rc(ctx->opcode) != 0))
1610         gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
1611 }
1612 #endif
1613
1614 /***                             Integer rotate                            ***/
1615
1616 /* rlwimi & rlwimi. */
1617 static void gen_rlwimi(DisasContext *ctx)
1618 {
1619     uint32_t mb, me, sh;
1620
1621     mb = MB(ctx->opcode);
1622     me = ME(ctx->opcode);
1623     sh = SH(ctx->opcode);
1624     if (likely(sh == (31-me) && mb <= me)) {
1625         tcg_gen_deposit_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rA(ctx->opcode)],
1626                            cpu_gpr[rS(ctx->opcode)], sh, me - mb + 1);
1627     } else {
1628         target_ulong mask;
1629         TCGv t1;
1630         TCGv t0 = tcg_temp_new();
1631 #if defined(TARGET_PPC64)
1632         tcg_gen_deposit_i64(t0, cpu_gpr[rS(ctx->opcode)],
1633             cpu_gpr[rS(ctx->opcode)], 32, 32);
1634         tcg_gen_rotli_i64(t0, t0, sh);
1635 #else
1636         tcg_gen_rotli_i32(t0, cpu_gpr[rS(ctx->opcode)], sh);
1637 #endif
1638 #if defined(TARGET_PPC64)
1639         mb += 32;
1640         me += 32;
1641 #endif
1642         mask = MASK(mb, me);
1643         t1 = tcg_temp_new();
1644         tcg_gen_andi_tl(t0, t0, mask);
1645         tcg_gen_andi_tl(t1, cpu_gpr[rA(ctx->opcode)], ~mask);
1646         tcg_gen_or_tl(cpu_gpr[rA(ctx->opcode)], t0, t1);
1647         tcg_temp_free(t0);
1648         tcg_temp_free(t1);
1649     }
1650     if (unlikely(Rc(ctx->opcode) != 0))
1651         gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
1652 }
1653
1654 /* rlwinm & rlwinm. */
1655 static void gen_rlwinm(DisasContext *ctx)
1656 {
1657     uint32_t mb, me, sh;
1658
1659     sh = SH(ctx->opcode);
1660     mb = MB(ctx->opcode);
1661     me = ME(ctx->opcode);
1662
1663     if (likely(mb == 0 && me == (31 - sh))) {
1664         if (likely(sh == 0)) {
1665             tcg_gen_ext32u_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)]);
1666         } else {
1667             TCGv t0 = tcg_temp_new();
1668             tcg_gen_ext32u_tl(t0, cpu_gpr[rS(ctx->opcode)]);
1669             tcg_gen_shli_tl(t0, t0, sh);
1670             tcg_gen_ext32u_tl(cpu_gpr[rA(ctx->opcode)], t0);
1671             tcg_temp_free(t0);
1672         }
1673     } else if (likely(sh != 0 && me == 31 && sh == (32 - mb))) {
1674         TCGv t0 = tcg_temp_new();
1675         tcg_gen_ext32u_tl(t0, cpu_gpr[rS(ctx->opcode)]);
1676         tcg_gen_shri_tl(t0, t0, mb);
1677         tcg_gen_ext32u_tl(cpu_gpr[rA(ctx->opcode)], t0);
1678         tcg_temp_free(t0);
1679     } else if (likely(mb == 0 && me == 31)) {
1680         TCGv_i32 t0 = tcg_temp_new_i32();
1681         tcg_gen_trunc_tl_i32(t0, cpu_gpr[rS(ctx->opcode)]);
1682         tcg_gen_rotli_i32(t0, t0, sh);
1683         tcg_gen_extu_i32_tl(cpu_gpr[rA(ctx->opcode)], t0);
1684         tcg_temp_free_i32(t0);
1685     } else {
1686         TCGv t0 = tcg_temp_new();
1687 #if defined(TARGET_PPC64)
1688         tcg_gen_deposit_i64(t0, cpu_gpr[rS(ctx->opcode)],
1689             cpu_gpr[rS(ctx->opcode)], 32, 32);
1690         tcg_gen_rotli_i64(t0, t0, sh);
1691 #else
1692         tcg_gen_rotli_i32(t0, cpu_gpr[rS(ctx->opcode)], sh);
1693 #endif
1694 #if defined(TARGET_PPC64)
1695         mb += 32;
1696         me += 32;
1697 #endif
1698         tcg_gen_andi_tl(cpu_gpr[rA(ctx->opcode)], t0, MASK(mb, me));
1699         tcg_temp_free(t0);
1700     }
1701     if (unlikely(Rc(ctx->opcode) != 0))
1702         gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
1703 }
1704
1705 /* rlwnm & rlwnm. */
1706 static void gen_rlwnm(DisasContext *ctx)
1707 {
1708     uint32_t mb, me;
1709     mb = MB(ctx->opcode);
1710     me = ME(ctx->opcode);
1711
1712     if (likely(mb == 0 && me == 31)) {
1713         TCGv_i32 t0, t1;
1714         t0 = tcg_temp_new_i32();
1715         t1 = tcg_temp_new_i32();
1716         tcg_gen_trunc_tl_i32(t0, cpu_gpr[rB(ctx->opcode)]);
1717         tcg_gen_trunc_tl_i32(t1, cpu_gpr[rS(ctx->opcode)]);
1718         tcg_gen_andi_i32(t0, t0, 0x1f);
1719         tcg_gen_rotl_i32(t1, t1, t0);
1720         tcg_gen_extu_i32_tl(cpu_gpr[rA(ctx->opcode)], t1);
1721         tcg_temp_free_i32(t0);
1722         tcg_temp_free_i32(t1);
1723     } else {
1724         TCGv t0;
1725 #if defined(TARGET_PPC64)
1726         TCGv t1;
1727 #endif
1728
1729         t0 = tcg_temp_new();
1730         tcg_gen_andi_tl(t0, cpu_gpr[rB(ctx->opcode)], 0x1f);
1731 #if defined(TARGET_PPC64)
1732         t1 = tcg_temp_new_i64();
1733         tcg_gen_deposit_i64(t1, cpu_gpr[rS(ctx->opcode)],
1734                             cpu_gpr[rS(ctx->opcode)], 32, 32);
1735         tcg_gen_rotl_i64(t0, t1, t0);
1736         tcg_temp_free_i64(t1);
1737 #else
1738         tcg_gen_rotl_i32(t0, cpu_gpr[rS(ctx->opcode)], t0);
1739 #endif
1740         if (unlikely(mb != 0 || me != 31)) {
1741 #if defined(TARGET_PPC64)
1742             mb += 32;
1743             me += 32;
1744 #endif
1745             tcg_gen_andi_tl(cpu_gpr[rA(ctx->opcode)], t0, MASK(mb, me));
1746         } else {
1747             tcg_gen_andi_tl(t0, t0, MASK(32, 63));
1748             tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], t0);
1749         }
1750         tcg_temp_free(t0);
1751     }
1752     if (unlikely(Rc(ctx->opcode) != 0))
1753         gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
1754 }
1755
1756 #if defined(TARGET_PPC64)
1757 #define GEN_PPC64_R2(name, opc1, opc2)                                        \
1758 static void glue(gen_, name##0)(DisasContext *ctx)                            \
1759 {                                                                             \
1760     gen_##name(ctx, 0);                                                       \
1761 }                                                                             \
1762                                                                               \
1763 static void glue(gen_, name##1)(DisasContext *ctx)                            \
1764 {                                                                             \
1765     gen_##name(ctx, 1);                                                       \
1766 }
1767 #define GEN_PPC64_R4(name, opc1, opc2)                                        \
1768 static void glue(gen_, name##0)(DisasContext *ctx)                            \
1769 {                                                                             \
1770     gen_##name(ctx, 0, 0);                                                    \
1771 }                                                                             \
1772                                                                               \
1773 static void glue(gen_, name##1)(DisasContext *ctx)                            \
1774 {                                                                             \
1775     gen_##name(ctx, 0, 1);                                                    \
1776 }                                                                             \
1777                                                                               \
1778 static void glue(gen_, name##2)(DisasContext *ctx)                            \
1779 {                                                                             \
1780     gen_##name(ctx, 1, 0);                                                    \
1781 }                                                                             \
1782                                                                               \
1783 static void glue(gen_, name##3)(DisasContext *ctx)                            \
1784 {                                                                             \
1785     gen_##name(ctx, 1, 1);                                                    \
1786 }
1787
1788 static inline void gen_rldinm(DisasContext *ctx, uint32_t mb, uint32_t me,
1789                               uint32_t sh)
1790 {
1791     if (likely(sh != 0 && mb == 0 && me == (63 - sh))) {
1792         tcg_gen_shli_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)], sh);
1793     } else if (likely(sh != 0 && me == 63 && sh == (64 - mb))) {
1794         tcg_gen_shri_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)], mb);
1795     } else {
1796         TCGv t0 = tcg_temp_new();
1797         tcg_gen_rotli_tl(t0, cpu_gpr[rS(ctx->opcode)], sh);
1798         if (likely(mb == 0 && me == 63)) {
1799             tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], t0);
1800         } else {
1801             tcg_gen_andi_tl(cpu_gpr[rA(ctx->opcode)], t0, MASK(mb, me));
1802         }
1803         tcg_temp_free(t0);
1804     }
1805     if (unlikely(Rc(ctx->opcode) != 0))
1806         gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
1807 }
1808 /* rldicl - rldicl. */
1809 static inline void gen_rldicl(DisasContext *ctx, int mbn, int shn)
1810 {
1811     uint32_t sh, mb;
1812
1813     sh = SH(ctx->opcode) | (shn << 5);
1814     mb = MB(ctx->opcode) | (mbn << 5);
1815     gen_rldinm(ctx, mb, 63, sh);
1816 }
1817 GEN_PPC64_R4(rldicl, 0x1E, 0x00);
1818 /* rldicr - rldicr. */
1819 static inline void gen_rldicr(DisasContext *ctx, int men, int shn)
1820 {
1821     uint32_t sh, me;
1822
1823     sh = SH(ctx->opcode) | (shn << 5);
1824     me = MB(ctx->opcode) | (men << 5);
1825     gen_rldinm(ctx, 0, me, sh);
1826 }
1827 GEN_PPC64_R4(rldicr, 0x1E, 0x02);
1828 /* rldic - rldic. */
1829 static inline void gen_rldic(DisasContext *ctx, int mbn, int shn)
1830 {
1831     uint32_t sh, mb;
1832
1833     sh = SH(ctx->opcode) | (shn << 5);
1834     mb = MB(ctx->opcode) | (mbn << 5);
1835     gen_rldinm(ctx, mb, 63 - sh, sh);
1836 }
1837 GEN_PPC64_R4(rldic, 0x1E, 0x04);
1838
1839 static inline void gen_rldnm(DisasContext *ctx, uint32_t mb, uint32_t me)
1840 {
1841     TCGv t0;
1842
1843     t0 = tcg_temp_new();
1844     tcg_gen_andi_tl(t0, cpu_gpr[rB(ctx->opcode)], 0x3f);
1845     tcg_gen_rotl_tl(t0, cpu_gpr[rS(ctx->opcode)], t0);
1846     if (unlikely(mb != 0 || me != 63)) {
1847         tcg_gen_andi_tl(cpu_gpr[rA(ctx->opcode)], t0, MASK(mb, me));
1848     } else {
1849         tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], t0);
1850     }
1851     tcg_temp_free(t0);
1852     if (unlikely(Rc(ctx->opcode) != 0))
1853         gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
1854 }
1855
1856 /* rldcl - rldcl. */
1857 static inline void gen_rldcl(DisasContext *ctx, int mbn)
1858 {
1859     uint32_t mb;
1860
1861     mb = MB(ctx->opcode) | (mbn << 5);
1862     gen_rldnm(ctx, mb, 63);
1863 }
1864 GEN_PPC64_R2(rldcl, 0x1E, 0x08);
1865 /* rldcr - rldcr. */
1866 static inline void gen_rldcr(DisasContext *ctx, int men)
1867 {
1868     uint32_t me;
1869
1870     me = MB(ctx->opcode) | (men << 5);
1871     gen_rldnm(ctx, 0, me);
1872 }
1873 GEN_PPC64_R2(rldcr, 0x1E, 0x09);
1874 /* rldimi - rldimi. */
1875 static inline void gen_rldimi(DisasContext *ctx, int mbn, int shn)
1876 {
1877     uint32_t sh, mb, me;
1878
1879     sh = SH(ctx->opcode) | (shn << 5);
1880     mb = MB(ctx->opcode) | (mbn << 5);
1881     me = 63 - sh;
1882     if (unlikely(sh == 0 && mb == 0)) {
1883         tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)]);
1884     } else {
1885         TCGv t0, t1;
1886         target_ulong mask;
1887
1888         t0 = tcg_temp_new();
1889         tcg_gen_rotli_tl(t0, cpu_gpr[rS(ctx->opcode)], sh);
1890         t1 = tcg_temp_new();
1891         mask = MASK(mb, me);
1892         tcg_gen_andi_tl(t0, t0, mask);
1893         tcg_gen_andi_tl(t1, cpu_gpr[rA(ctx->opcode)], ~mask);
1894         tcg_gen_or_tl(cpu_gpr[rA(ctx->opcode)], t0, t1);
1895         tcg_temp_free(t0);
1896         tcg_temp_free(t1);
1897     }
1898     if (unlikely(Rc(ctx->opcode) != 0))
1899         gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
1900 }
1901 GEN_PPC64_R4(rldimi, 0x1E, 0x06);
1902 #endif
1903
1904 /***                             Integer shift                             ***/
1905
1906 /* slw & slw. */
1907 static void gen_slw(DisasContext *ctx)
1908 {
1909     TCGv t0, t1;
1910
1911     t0 = tcg_temp_new();
1912     /* AND rS with a mask that is 0 when rB >= 0x20 */
1913 #if defined(TARGET_PPC64)
1914     tcg_gen_shli_tl(t0, cpu_gpr[rB(ctx->opcode)], 0x3a);
1915     tcg_gen_sari_tl(t0, t0, 0x3f);
1916 #else
1917     tcg_gen_shli_tl(t0, cpu_gpr[rB(ctx->opcode)], 0x1a);
1918     tcg_gen_sari_tl(t0, t0, 0x1f);
1919 #endif
1920     tcg_gen_andc_tl(t0, cpu_gpr[rS(ctx->opcode)], t0);
1921     t1 = tcg_temp_new();
1922     tcg_gen_andi_tl(t1, cpu_gpr[rB(ctx->opcode)], 0x1f);
1923     tcg_gen_shl_tl(cpu_gpr[rA(ctx->opcode)], t0, t1);
1924     tcg_temp_free(t1);
1925     tcg_temp_free(t0);
1926     tcg_gen_ext32u_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rA(ctx->opcode)]);
1927     if (unlikely(Rc(ctx->opcode) != 0))
1928         gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
1929 }
1930
1931 /* sraw & sraw. */
1932 static void gen_sraw(DisasContext *ctx)
1933 {
1934     gen_helper_sraw(cpu_gpr[rA(ctx->opcode)], cpu_env,
1935                     cpu_gpr[rS(ctx->opcode)], cpu_gpr[rB(ctx->opcode)]);
1936     if (unlikely(Rc(ctx->opcode) != 0))
1937         gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
1938 }
1939
1940 /* srawi & srawi. */
1941 static void gen_srawi(DisasContext *ctx)
1942 {
1943     int sh = SH(ctx->opcode);
1944     TCGv dst = cpu_gpr[rA(ctx->opcode)];
1945     TCGv src = cpu_gpr[rS(ctx->opcode)];
1946     if (sh == 0) {
1947         tcg_gen_ext32s_tl(dst, src);
1948         tcg_gen_movi_tl(cpu_ca, 0);
1949     } else {
1950         TCGv t0;
1951         tcg_gen_ext32s_tl(dst, src);
1952         tcg_gen_andi_tl(cpu_ca, dst, (1ULL << sh) - 1);
1953         t0 = tcg_temp_new();
1954         tcg_gen_sari_tl(t0, dst, TARGET_LONG_BITS - 1);
1955         tcg_gen_and_tl(cpu_ca, cpu_ca, t0);
1956         tcg_temp_free(t0);
1957         tcg_gen_setcondi_tl(TCG_COND_NE, cpu_ca, cpu_ca, 0);
1958         tcg_gen_sari_tl(dst, dst, sh);
1959     }
1960     if (unlikely(Rc(ctx->opcode) != 0)) {
1961         gen_set_Rc0(ctx, dst);
1962     }
1963 }
1964
1965 /* srw & srw. */
1966 static void gen_srw(DisasContext *ctx)
1967 {
1968     TCGv t0, t1;
1969
1970     t0 = tcg_temp_new();
1971     /* AND rS with a mask that is 0 when rB >= 0x20 */
1972 #if defined(TARGET_PPC64)
1973     tcg_gen_shli_tl(t0, cpu_gpr[rB(ctx->opcode)], 0x3a);
1974     tcg_gen_sari_tl(t0, t0, 0x3f);
1975 #else
1976     tcg_gen_shli_tl(t0, cpu_gpr[rB(ctx->opcode)], 0x1a);
1977     tcg_gen_sari_tl(t0, t0, 0x1f);
1978 #endif
1979     tcg_gen_andc_tl(t0, cpu_gpr[rS(ctx->opcode)], t0);
1980     tcg_gen_ext32u_tl(t0, t0);
1981     t1 = tcg_temp_new();
1982     tcg_gen_andi_tl(t1, cpu_gpr[rB(ctx->opcode)], 0x1f);
1983     tcg_gen_shr_tl(cpu_gpr[rA(ctx->opcode)], t0, t1);
1984     tcg_temp_free(t1);
1985     tcg_temp_free(t0);
1986     if (unlikely(Rc(ctx->opcode) != 0))
1987         gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
1988 }
1989
1990 #if defined(TARGET_PPC64)
1991 /* sld & sld. */
1992 static void gen_sld(DisasContext *ctx)
1993 {
1994     TCGv t0, t1;
1995
1996     t0 = tcg_temp_new();
1997     /* AND rS with a mask that is 0 when rB >= 0x40 */
1998     tcg_gen_shli_tl(t0, cpu_gpr[rB(ctx->opcode)], 0x39);
1999     tcg_gen_sari_tl(t0, t0, 0x3f);
2000     tcg_gen_andc_tl(t0, cpu_gpr[rS(ctx->opcode)], t0);
2001     t1 = tcg_temp_new();
2002     tcg_gen_andi_tl(t1, cpu_gpr[rB(ctx->opcode)], 0x3f);
2003     tcg_gen_shl_tl(cpu_gpr[rA(ctx->opcode)], t0, t1);
2004     tcg_temp_free(t1);
2005     tcg_temp_free(t0);
2006     if (unlikely(Rc(ctx->opcode) != 0))
2007         gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
2008 }
2009
2010 /* srad & srad. */
2011 static void gen_srad(DisasContext *ctx)
2012 {
2013     gen_helper_srad(cpu_gpr[rA(ctx->opcode)], cpu_env,
2014                     cpu_gpr[rS(ctx->opcode)], cpu_gpr[rB(ctx->opcode)]);
2015     if (unlikely(Rc(ctx->opcode) != 0))
2016         gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
2017 }
2018 /* sradi & sradi. */
2019 static inline void gen_sradi(DisasContext *ctx, int n)
2020 {
2021     int sh = SH(ctx->opcode) + (n << 5);
2022     TCGv dst = cpu_gpr[rA(ctx->opcode)];
2023     TCGv src = cpu_gpr[rS(ctx->opcode)];
2024     if (sh == 0) {
2025         tcg_gen_mov_tl(dst, src);
2026         tcg_gen_movi_tl(cpu_ca, 0);
2027     } else {
2028         TCGv t0;
2029         tcg_gen_andi_tl(cpu_ca, src, (1ULL << sh) - 1);
2030         t0 = tcg_temp_new();
2031         tcg_gen_sari_tl(t0, src, TARGET_LONG_BITS - 1);
2032         tcg_gen_and_tl(cpu_ca, cpu_ca, t0);
2033         tcg_temp_free(t0);
2034         tcg_gen_setcondi_tl(TCG_COND_NE, cpu_ca, cpu_ca, 0);
2035         tcg_gen_sari_tl(dst, src, sh);
2036     }
2037     if (unlikely(Rc(ctx->opcode) != 0)) {
2038         gen_set_Rc0(ctx, dst);
2039     }
2040 }
2041
2042 static void gen_sradi0(DisasContext *ctx)
2043 {
2044     gen_sradi(ctx, 0);
2045 }
2046
2047 static void gen_sradi1(DisasContext *ctx)
2048 {
2049     gen_sradi(ctx, 1);
2050 }
2051
2052 /* srd & srd. */
2053 static void gen_srd(DisasContext *ctx)
2054 {
2055     TCGv t0, t1;
2056
2057     t0 = tcg_temp_new();
2058     /* AND rS with a mask that is 0 when rB >= 0x40 */
2059     tcg_gen_shli_tl(t0, cpu_gpr[rB(ctx->opcode)], 0x39);
2060     tcg_gen_sari_tl(t0, t0, 0x3f);
2061     tcg_gen_andc_tl(t0, cpu_gpr[rS(ctx->opcode)], t0);
2062     t1 = tcg_temp_new();
2063     tcg_gen_andi_tl(t1, cpu_gpr[rB(ctx->opcode)], 0x3f);
2064     tcg_gen_shr_tl(cpu_gpr[rA(ctx->opcode)], t0, t1);
2065     tcg_temp_free(t1);
2066     tcg_temp_free(t0);
2067     if (unlikely(Rc(ctx->opcode) != 0))
2068         gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
2069 }
2070 #endif
2071
2072 #if defined(TARGET_PPC64)
2073 static void gen_set_cr1_from_fpscr(DisasContext *ctx)
2074 {
2075     TCGv_i32 tmp = tcg_temp_new_i32();
2076     tcg_gen_trunc_tl_i32(tmp, cpu_fpscr);
2077     tcg_gen_shri_i32(cpu_crf[1], tmp, 28);
2078     tcg_temp_free_i32(tmp);
2079 }
2080 #else
2081 static void gen_set_cr1_from_fpscr(DisasContext *ctx)
2082 {
2083     tcg_gen_shri_tl(cpu_crf[1], cpu_fpscr, 28);
2084 }
2085 #endif
2086
2087 /***                       Floating-Point arithmetic                       ***/
2088 #define _GEN_FLOAT_ACB(name, op, op1, op2, isfloat, set_fprf, type)           \
2089 static void gen_f##name(DisasContext *ctx)                                    \
2090 {                                                                             \
2091     if (unlikely(!ctx->fpu_enabled)) {                                        \
2092         gen_exception(ctx, POWERPC_EXCP_FPU);                                 \
2093         return;                                                               \
2094     }                                                                         \
2095     /* NIP cannot be restored if the memory exception comes from an helper */ \
2096     gen_update_nip(ctx, ctx->nip - 4);                                        \
2097     gen_reset_fpstatus();                                                     \
2098     gen_helper_f##op(cpu_fpr[rD(ctx->opcode)], cpu_env,                       \
2099                      cpu_fpr[rA(ctx->opcode)],                                \
2100                      cpu_fpr[rC(ctx->opcode)], cpu_fpr[rB(ctx->opcode)]);     \
2101     if (isfloat) {                                                            \
2102         gen_helper_frsp(cpu_fpr[rD(ctx->opcode)], cpu_env,                    \
2103                         cpu_fpr[rD(ctx->opcode)]);                            \
2104     }                                                                         \
2105     gen_compute_fprf(cpu_fpr[rD(ctx->opcode)], set_fprf);                     \
2106     if (unlikely(Rc(ctx->opcode) != 0)) {                                     \
2107         gen_set_cr1_from_fpscr(ctx);                                          \
2108     }                                                                         \
2109 }
2110
2111 #define GEN_FLOAT_ACB(name, op2, set_fprf, type)                              \
2112 _GEN_FLOAT_ACB(name, name, 0x3F, op2, 0, set_fprf, type);                     \
2113 _GEN_FLOAT_ACB(name##s, name, 0x3B, op2, 1, set_fprf, type);
2114
2115 #define _GEN_FLOAT_AB(name, op, op1, op2, inval, isfloat, set_fprf, type)     \
2116 static void gen_f##name(DisasContext *ctx)                                    \
2117 {                                                                             \
2118     if (unlikely(!ctx->fpu_enabled)) {                                        \
2119         gen_exception(ctx, POWERPC_EXCP_FPU);                                 \
2120         return;                                                               \
2121     }                                                                         \
2122     /* NIP cannot be restored if the memory exception comes from an helper */ \
2123     gen_update_nip(ctx, ctx->nip - 4);                                        \
2124     gen_reset_fpstatus();                                                     \
2125     gen_helper_f##op(cpu_fpr[rD(ctx->opcode)], cpu_env,                       \
2126                      cpu_fpr[rA(ctx->opcode)],                                \
2127                      cpu_fpr[rB(ctx->opcode)]);                               \
2128     if (isfloat) {                                                            \
2129         gen_helper_frsp(cpu_fpr[rD(ctx->opcode)], cpu_env,                    \
2130                         cpu_fpr[rD(ctx->opcode)]);                            \
2131     }                                                                         \
2132     gen_compute_fprf(cpu_fpr[rD(ctx->opcode)], set_fprf);                     \
2133     if (unlikely(Rc(ctx->opcode) != 0)) {                                     \
2134         gen_set_cr1_from_fpscr(ctx);                                          \
2135     }                                                                         \
2136 }
2137 #define GEN_FLOAT_AB(name, op2, inval, set_fprf, type)                        \
2138 _GEN_FLOAT_AB(name, name, 0x3F, op2, inval, 0, set_fprf, type);               \
2139 _GEN_FLOAT_AB(name##s, name, 0x3B, op2, inval, 1, set_fprf, type);
2140
2141 #define _GEN_FLOAT_AC(name, op, op1, op2, inval, isfloat, set_fprf, type)     \
2142 static void gen_f##name(DisasContext *ctx)                                    \
2143 {                                                                             \
2144     if (unlikely(!ctx->fpu_enabled)) {                                        \
2145         gen_exception(ctx, POWERPC_EXCP_FPU);                                 \
2146         return;                                                               \
2147     }                                                                         \
2148     /* NIP cannot be restored if the memory exception comes from an helper */ \
2149     gen_update_nip(ctx, ctx->nip - 4);                                        \
2150     gen_reset_fpstatus();                                                     \
2151     gen_helper_f##op(cpu_fpr[rD(ctx->opcode)], cpu_env,                       \
2152                      cpu_fpr[rA(ctx->opcode)],                                \
2153                      cpu_fpr[rC(ctx->opcode)]);                               \
2154     if (isfloat) {                                                            \
2155         gen_helper_frsp(cpu_fpr[rD(ctx->opcode)], cpu_env,                    \
2156                         cpu_fpr[rD(ctx->opcode)]);                            \
2157     }                                                                         \
2158     gen_compute_fprf(cpu_fpr[rD(ctx->opcode)], set_fprf);                     \
2159     if (unlikely(Rc(ctx->opcode) != 0)) {                                     \
2160         gen_set_cr1_from_fpscr(ctx);                                          \
2161     }                                                                         \
2162 }
2163 #define GEN_FLOAT_AC(name, op2, inval, set_fprf, type)                        \
2164 _GEN_FLOAT_AC(name, name, 0x3F, op2, inval, 0, set_fprf, type);               \
2165 _GEN_FLOAT_AC(name##s, name, 0x3B, op2, inval, 1, set_fprf, type);
2166
2167 #define GEN_FLOAT_B(name, op2, op3, set_fprf, type)                           \
2168 static void gen_f##name(DisasContext *ctx)                                    \
2169 {                                                                             \
2170     if (unlikely(!ctx->fpu_enabled)) {                                        \
2171         gen_exception(ctx, POWERPC_EXCP_FPU);                                 \
2172         return;                                                               \
2173     }                                                                         \
2174     /* NIP cannot be restored if the memory exception comes from an helper */ \
2175     gen_update_nip(ctx, ctx->nip - 4);                                        \
2176     gen_reset_fpstatus();                                                     \
2177     gen_helper_f##name(cpu_fpr[rD(ctx->opcode)], cpu_env,                     \
2178                        cpu_fpr[rB(ctx->opcode)]);                             \
2179     gen_compute_fprf(cpu_fpr[rD(ctx->opcode)], set_fprf);                     \
2180     if (unlikely(Rc(ctx->opcode) != 0)) {                                     \
2181         gen_set_cr1_from_fpscr(ctx);                                          \
2182     }                                                                         \
2183 }
2184
2185 #define GEN_FLOAT_BS(name, op1, op2, set_fprf, type)                          \
2186 static void gen_f##name(DisasContext *ctx)                                    \
2187 {                                                                             \
2188     if (unlikely(!ctx->fpu_enabled)) {                                        \
2189         gen_exception(ctx, POWERPC_EXCP_FPU);                                 \
2190         return;                                                               \
2191     }                                                                         \
2192     /* NIP cannot be restored if the memory exception comes from an helper */ \
2193     gen_update_nip(ctx, ctx->nip - 4);                                        \
2194     gen_reset_fpstatus();                                                     \
2195     gen_helper_f##name(cpu_fpr[rD(ctx->opcode)], cpu_env,                     \
2196                        cpu_fpr[rB(ctx->opcode)]);                             \
2197     gen_compute_fprf(cpu_fpr[rD(ctx->opcode)], set_fprf);                     \
2198     if (unlikely(Rc(ctx->opcode) != 0)) {                                     \
2199         gen_set_cr1_from_fpscr(ctx);                                          \
2200     }                                                                         \
2201 }
2202
2203 /* fadd - fadds */
2204 GEN_FLOAT_AB(add, 0x15, 0x000007C0, 1, PPC_FLOAT);
2205 /* fdiv - fdivs */
2206 GEN_FLOAT_AB(div, 0x12, 0x000007C0, 1, PPC_FLOAT);
2207 /* fmul - fmuls */
2208 GEN_FLOAT_AC(mul, 0x19, 0x0000F800, 1, PPC_FLOAT);
2209
2210 /* fre */
2211 GEN_FLOAT_BS(re, 0x3F, 0x18, 1, PPC_FLOAT_EXT);
2212
2213 /* fres */
2214 GEN_FLOAT_BS(res, 0x3B, 0x18, 1, PPC_FLOAT_FRES);
2215
2216 /* frsqrte */
2217 GEN_FLOAT_BS(rsqrte, 0x3F, 0x1A, 1, PPC_FLOAT_FRSQRTE);
2218
2219 /* frsqrtes */
2220 static void gen_frsqrtes(DisasContext *ctx)
2221 {
2222     if (unlikely(!ctx->fpu_enabled)) {
2223         gen_exception(ctx, POWERPC_EXCP_FPU);
2224         return;
2225     }
2226     /* NIP cannot be restored if the memory exception comes from an helper */
2227     gen_update_nip(ctx, ctx->nip - 4);
2228     gen_reset_fpstatus();
2229     gen_helper_frsqrte(cpu_fpr[rD(ctx->opcode)], cpu_env,
2230                        cpu_fpr[rB(ctx->opcode)]);
2231     gen_helper_frsp(cpu_fpr[rD(ctx->opcode)], cpu_env,
2232                     cpu_fpr[rD(ctx->opcode)]);
2233     gen_compute_fprf(cpu_fpr[rD(ctx->opcode)], 1);
2234     if (unlikely(Rc(ctx->opcode) != 0)) {
2235         gen_set_cr1_from_fpscr(ctx);
2236     }
2237 }
2238
2239 /* fsel */
2240 _GEN_FLOAT_ACB(sel, sel, 0x3F, 0x17, 0, 0, PPC_FLOAT_FSEL);
2241 /* fsub - fsubs */
2242 GEN_FLOAT_AB(sub, 0x14, 0x000007C0, 1, PPC_FLOAT);
2243 /* Optional: */
2244
2245 /* fsqrt */
2246 static void gen_fsqrt(DisasContext *ctx)
2247 {
2248     if (unlikely(!ctx->fpu_enabled)) {
2249         gen_exception(ctx, POWERPC_EXCP_FPU);
2250         return;
2251     }
2252     /* NIP cannot be restored if the memory exception comes from an helper */
2253     gen_update_nip(ctx, ctx->nip - 4);
2254     gen_reset_fpstatus();
2255     gen_helper_fsqrt(cpu_fpr[rD(ctx->opcode)], cpu_env,
2256                      cpu_fpr[rB(ctx->opcode)]);
2257     gen_compute_fprf(cpu_fpr[rD(ctx->opcode)], 1);
2258     if (unlikely(Rc(ctx->opcode) != 0)) {
2259         gen_set_cr1_from_fpscr(ctx);
2260     }
2261 }
2262
2263 static void gen_fsqrts(DisasContext *ctx)
2264 {
2265     if (unlikely(!ctx->fpu_enabled)) {
2266         gen_exception(ctx, POWERPC_EXCP_FPU);
2267         return;
2268     }
2269     /* NIP cannot be restored if the memory exception comes from an helper */
2270     gen_update_nip(ctx, ctx->nip - 4);
2271     gen_reset_fpstatus();
2272     gen_helper_fsqrt(cpu_fpr[rD(ctx->opcode)], cpu_env,
2273                      cpu_fpr[rB(ctx->opcode)]);
2274     gen_helper_frsp(cpu_fpr[rD(ctx->opcode)], cpu_env,
2275                     cpu_fpr[rD(ctx->opcode)]);
2276     gen_compute_fprf(cpu_fpr[rD(ctx->opcode)], 1);
2277     if (unlikely(Rc(ctx->opcode) != 0)) {
2278         gen_set_cr1_from_fpscr(ctx);
2279     }
2280 }
2281
2282 /***                     Floating-Point multiply-and-add                   ***/
2283 /* fmadd - fmadds */
2284 GEN_FLOAT_ACB(madd, 0x1D, 1, PPC_FLOAT);
2285 /* fmsub - fmsubs */
2286 GEN_FLOAT_ACB(msub, 0x1C, 1, PPC_FLOAT);
2287 /* fnmadd - fnmadds */
2288 GEN_FLOAT_ACB(nmadd, 0x1F, 1, PPC_FLOAT);
2289 /* fnmsub - fnmsubs */
2290 GEN_FLOAT_ACB(nmsub, 0x1E, 1, PPC_FLOAT);
2291
2292 /***                     Floating-Point round & convert                    ***/
2293 /* fctiw */
2294 GEN_FLOAT_B(ctiw, 0x0E, 0x00, 0, PPC_FLOAT);
2295 /* fctiwu */
2296 GEN_FLOAT_B(ctiwu, 0x0E, 0x04, 0, PPC2_FP_CVT_ISA206);
2297 /* fctiwz */
2298 GEN_FLOAT_B(ctiwz, 0x0F, 0x00, 0, PPC_FLOAT);
2299 /* fctiwuz */
2300 GEN_FLOAT_B(ctiwuz, 0x0F, 0x04, 0, PPC2_FP_CVT_ISA206);
2301 /* frsp */
2302 GEN_FLOAT_B(rsp, 0x0C, 0x00, 1, PPC_FLOAT);
2303 /* fcfid */
2304 GEN_FLOAT_B(cfid, 0x0E, 0x1A, 1, PPC2_FP_CVT_S64);
2305 /* fcfids */
2306 GEN_FLOAT_B(cfids, 0x0E, 0x1A, 0, PPC2_FP_CVT_ISA206);
2307 /* fcfidu */
2308 GEN_FLOAT_B(cfidu, 0x0E, 0x1E, 0, PPC2_FP_CVT_ISA206);
2309 /* fcfidus */
2310 GEN_FLOAT_B(cfidus, 0x0E, 0x1E, 0, PPC2_FP_CVT_ISA206);
2311 /* fctid */
2312 GEN_FLOAT_B(ctid, 0x0E, 0x19, 0, PPC2_FP_CVT_S64);
2313 /* fctidu */
2314 GEN_FLOAT_B(ctidu, 0x0E, 0x1D, 0, PPC2_FP_CVT_ISA206);
2315 /* fctidz */
2316 GEN_FLOAT_B(ctidz, 0x0F, 0x19, 0, PPC2_FP_CVT_S64);
2317 /* fctidu */
2318 GEN_FLOAT_B(ctiduz, 0x0F, 0x1D, 0, PPC2_FP_CVT_ISA206);
2319
2320 /* frin */
2321 GEN_FLOAT_B(rin, 0x08, 0x0C, 1, PPC_FLOAT_EXT);
2322 /* friz */
2323 GEN_FLOAT_B(riz, 0x08, 0x0D, 1, PPC_FLOAT_EXT);
2324 /* frip */
2325 GEN_FLOAT_B(rip, 0x08, 0x0E, 1, PPC_FLOAT_EXT);
2326 /* frim */
2327 GEN_FLOAT_B(rim, 0x08, 0x0F, 1, PPC_FLOAT_EXT);
2328
2329 static void gen_ftdiv(DisasContext *ctx)
2330 {
2331     if (unlikely(!ctx->fpu_enabled)) {
2332         gen_exception(ctx, POWERPC_EXCP_FPU);
2333         return;
2334     }
2335     gen_helper_ftdiv(cpu_crf[crfD(ctx->opcode)], cpu_fpr[rA(ctx->opcode)],
2336                      cpu_fpr[rB(ctx->opcode)]);
2337 }
2338
2339 static void gen_ftsqrt(DisasContext *ctx)
2340 {
2341     if (unlikely(!ctx->fpu_enabled)) {
2342         gen_exception(ctx, POWERPC_EXCP_FPU);
2343         return;
2344     }
2345     gen_helper_ftsqrt(cpu_crf[crfD(ctx->opcode)], cpu_fpr[rB(ctx->opcode)]);
2346 }
2347
2348
2349
2350 /***                         Floating-Point compare                        ***/
2351
2352 /* fcmpo */
2353 static void gen_fcmpo(DisasContext *ctx)
2354 {
2355     TCGv_i32 crf;
2356     if (unlikely(!ctx->fpu_enabled)) {
2357         gen_exception(ctx, POWERPC_EXCP_FPU);
2358         return;
2359     }
2360     /* NIP cannot be restored if the memory exception comes from an helper */
2361     gen_update_nip(ctx, ctx->nip - 4);
2362     gen_reset_fpstatus();
2363     crf = tcg_const_i32(crfD(ctx->opcode));
2364     gen_helper_fcmpo(cpu_env, cpu_fpr[rA(ctx->opcode)],
2365                      cpu_fpr[rB(ctx->opcode)], crf);
2366     tcg_temp_free_i32(crf);
2367     gen_helper_float_check_status(cpu_env);
2368 }
2369
2370 /* fcmpu */
2371 static void gen_fcmpu(DisasContext *ctx)
2372 {
2373     TCGv_i32 crf;
2374     if (unlikely(!ctx->fpu_enabled)) {
2375         gen_exception(ctx, POWERPC_EXCP_FPU);
2376         return;
2377     }
2378     /* NIP cannot be restored if the memory exception comes from an helper */
2379     gen_update_nip(ctx, ctx->nip - 4);
2380     gen_reset_fpstatus();
2381     crf = tcg_const_i32(crfD(ctx->opcode));
2382     gen_helper_fcmpu(cpu_env, cpu_fpr[rA(ctx->opcode)],
2383                      cpu_fpr[rB(ctx->opcode)], crf);
2384     tcg_temp_free_i32(crf);
2385     gen_helper_float_check_status(cpu_env);
2386 }
2387
2388 /***                         Floating-point move                           ***/
2389 /* fabs */
2390 /* XXX: beware that fabs never checks for NaNs nor update FPSCR */
2391 static void gen_fabs(DisasContext *ctx)
2392 {
2393     if (unlikely(!ctx->fpu_enabled)) {
2394         gen_exception(ctx, POWERPC_EXCP_FPU);
2395         return;
2396     }
2397     tcg_gen_andi_i64(cpu_fpr[rD(ctx->opcode)], cpu_fpr[rB(ctx->opcode)],
2398                      ~(1ULL << 63));
2399     if (unlikely(Rc(ctx->opcode))) {
2400         gen_set_cr1_from_fpscr(ctx);
2401     }
2402 }
2403
2404 /* fmr  - fmr. */
2405 /* XXX: beware that fmr never checks for NaNs nor update FPSCR */
2406 static void gen_fmr(DisasContext *ctx)
2407 {
2408     if (unlikely(!ctx->fpu_enabled)) {
2409         gen_exception(ctx, POWERPC_EXCP_FPU);
2410         return;
2411     }
2412     tcg_gen_mov_i64(cpu_fpr[rD(ctx->opcode)], cpu_fpr[rB(ctx->opcode)]);
2413     if (unlikely(Rc(ctx->opcode))) {
2414         gen_set_cr1_from_fpscr(ctx);
2415     }
2416 }
2417
2418 /* fnabs */
2419 /* XXX: beware that fnabs never checks for NaNs nor update FPSCR */
2420 static void gen_fnabs(DisasContext *ctx)
2421 {
2422     if (unlikely(!ctx->fpu_enabled)) {
2423         gen_exception(ctx, POWERPC_EXCP_FPU);
2424         return;
2425     }
2426     tcg_gen_ori_i64(cpu_fpr[rD(ctx->opcode)], cpu_fpr[rB(ctx->opcode)],
2427                     1ULL << 63);
2428     if (unlikely(Rc(ctx->opcode))) {
2429         gen_set_cr1_from_fpscr(ctx);
2430     }
2431 }
2432
2433 /* fneg */
2434 /* XXX: beware that fneg never checks for NaNs nor update FPSCR */
2435 static void gen_fneg(DisasContext *ctx)
2436 {
2437     if (unlikely(!ctx->fpu_enabled)) {
2438         gen_exception(ctx, POWERPC_EXCP_FPU);
2439         return;
2440     }
2441     tcg_gen_xori_i64(cpu_fpr[rD(ctx->opcode)], cpu_fpr[rB(ctx->opcode)],
2442                      1ULL << 63);
2443     if (unlikely(Rc(ctx->opcode))) {
2444         gen_set_cr1_from_fpscr(ctx);
2445     }
2446 }
2447
2448 /* fcpsgn: PowerPC 2.05 specification */
2449 /* XXX: beware that fcpsgn never checks for NaNs nor update FPSCR */
2450 static void gen_fcpsgn(DisasContext *ctx)
2451 {
2452     if (unlikely(!ctx->fpu_enabled)) {
2453         gen_exception(ctx, POWERPC_EXCP_FPU);
2454         return;
2455     }
2456     tcg_gen_deposit_i64(cpu_fpr[rD(ctx->opcode)], cpu_fpr[rA(ctx->opcode)],
2457                         cpu_fpr[rB(ctx->opcode)], 0, 63);
2458     if (unlikely(Rc(ctx->opcode))) {
2459         gen_set_cr1_from_fpscr(ctx);
2460     }
2461 }
2462
2463 static void gen_fmrgew(DisasContext *ctx)
2464 {
2465     TCGv_i64 b0;
2466     if (unlikely(!ctx->fpu_enabled)) {
2467         gen_exception(ctx, POWERPC_EXCP_FPU);
2468         return;
2469     }
2470     b0 = tcg_temp_new_i64();
2471     tcg_gen_shri_i64(b0, cpu_fpr[rB(ctx->opcode)], 32);
2472     tcg_gen_deposit_i64(cpu_fpr[rD(ctx->opcode)], cpu_fpr[rA(ctx->opcode)],
2473                         b0, 0, 32);
2474     tcg_temp_free_i64(b0);
2475 }
2476
2477 static void gen_fmrgow(DisasContext *ctx)
2478 {
2479     if (unlikely(!ctx->fpu_enabled)) {
2480         gen_exception(ctx, POWERPC_EXCP_FPU);
2481         return;
2482     }
2483     tcg_gen_deposit_i64(cpu_fpr[rD(ctx->opcode)],
2484                         cpu_fpr[rB(ctx->opcode)],
2485                         cpu_fpr[rA(ctx->opcode)],
2486                         32, 32);
2487 }
2488
2489 /***                  Floating-Point status & ctrl register                ***/
2490
2491 /* mcrfs */
2492 static void gen_mcrfs(DisasContext *ctx)
2493 {
2494     TCGv tmp = tcg_temp_new();
2495     int bfa;
2496
2497     if (unlikely(!ctx->fpu_enabled)) {
2498         gen_exception(ctx, POWERPC_EXCP_FPU);
2499         return;
2500     }
2501     bfa = 4 * (7 - crfS(ctx->opcode));
2502     tcg_gen_shri_tl(tmp, cpu_fpscr, bfa);
2503     tcg_gen_trunc_tl_i32(cpu_crf[crfD(ctx->opcode)], tmp);
2504     tcg_temp_free(tmp);
2505     tcg_gen_andi_i32(cpu_crf[crfD(ctx->opcode)], cpu_crf[crfD(ctx->opcode)], 0xf);
2506     tcg_gen_andi_tl(cpu_fpscr, cpu_fpscr, ~(0xF << bfa));
2507 }
2508
2509 /* mffs */
2510 static void gen_mffs(DisasContext *ctx)
2511 {
2512     if (unlikely(!ctx->fpu_enabled)) {
2513         gen_exception(ctx, POWERPC_EXCP_FPU);
2514         return;
2515     }
2516     gen_reset_fpstatus();
2517     tcg_gen_extu_tl_i64(cpu_fpr[rD(ctx->opcode)], cpu_fpscr);
2518     if (unlikely(Rc(ctx->opcode))) {
2519         gen_set_cr1_from_fpscr(ctx);
2520     }
2521 }
2522
2523 /* mtfsb0 */
2524 static void gen_mtfsb0(DisasContext *ctx)
2525 {
2526     uint8_t crb;
2527
2528     if (unlikely(!ctx->fpu_enabled)) {
2529         gen_exception(ctx, POWERPC_EXCP_FPU);
2530         return;
2531     }
2532     crb = 31 - crbD(ctx->opcode);
2533     gen_reset_fpstatus();
2534     if (likely(crb != FPSCR_FEX && crb != FPSCR_VX)) {
2535         TCGv_i32 t0;
2536         /* NIP cannot be restored if the memory exception comes from an helper */
2537         gen_update_nip(ctx, ctx->nip - 4);
2538         t0 = tcg_const_i32(crb);
2539         gen_helper_fpscr_clrbit(cpu_env, t0);
2540         tcg_temp_free_i32(t0);
2541     }
2542     if (unlikely(Rc(ctx->opcode) != 0)) {
2543         tcg_gen_trunc_tl_i32(cpu_crf[1], cpu_fpscr);
2544         tcg_gen_shri_i32(cpu_crf[1], cpu_crf[1], FPSCR_OX);
2545     }
2546 }
2547
2548 /* mtfsb1 */
2549 static void gen_mtfsb1(DisasContext *ctx)
2550 {
2551     uint8_t crb;
2552
2553     if (unlikely(!ctx->fpu_enabled)) {
2554         gen_exception(ctx, POWERPC_EXCP_FPU);
2555         return;
2556     }
2557     crb = 31 - crbD(ctx->opcode);
2558     gen_reset_fpstatus();
2559     /* XXX: we pretend we can only do IEEE floating-point computations */
2560     if (likely(crb != FPSCR_FEX && crb != FPSCR_VX && crb != FPSCR_NI)) {
2561         TCGv_i32 t0;
2562         /* NIP cannot be restored if the memory exception comes from an helper */
2563         gen_update_nip(ctx, ctx->nip - 4);
2564         t0 = tcg_const_i32(crb);
2565         gen_helper_fpscr_setbit(cpu_env, t0);
2566         tcg_temp_free_i32(t0);
2567     }
2568     if (unlikely(Rc(ctx->opcode) != 0)) {
2569         tcg_gen_trunc_tl_i32(cpu_crf[1], cpu_fpscr);
2570         tcg_gen_shri_i32(cpu_crf[1], cpu_crf[1], FPSCR_OX);
2571     }
2572     /* We can raise a differed exception */
2573     gen_helper_float_check_status(cpu_env);
2574 }
2575
2576 /* mtfsf */
2577 static void gen_mtfsf(DisasContext *ctx)
2578 {
2579     TCGv_i32 t0;
2580     int flm, l, w;
2581
2582     if (unlikely(!ctx->fpu_enabled)) {
2583         gen_exception(ctx, POWERPC_EXCP_FPU);
2584         return;
2585     }
2586     flm = FPFLM(ctx->opcode);
2587     l = FPL(ctx->opcode);
2588     w = FPW(ctx->opcode);
2589     if (unlikely(w & !(ctx->insns_flags2 & PPC2_ISA205))) {
2590         gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL);
2591         return;
2592     }
2593     /* NIP cannot be restored if the memory exception comes from an helper */
2594     gen_update_nip(ctx, ctx->nip - 4);
2595     gen_reset_fpstatus();
2596     if (l) {
2597         t0 = tcg_const_i32((ctx->insns_flags2 & PPC2_ISA205) ? 0xffff : 0xff);
2598     } else {
2599         t0 = tcg_const_i32(flm << (w * 8));
2600     }
2601     gen_helper_store_fpscr(cpu_env, cpu_fpr[rB(ctx->opcode)], t0);
2602     tcg_temp_free_i32(t0);
2603     if (unlikely(Rc(ctx->opcode) != 0)) {
2604         tcg_gen_trunc_tl_i32(cpu_crf[1], cpu_fpscr);
2605         tcg_gen_shri_i32(cpu_crf[1], cpu_crf[1], FPSCR_OX);
2606     }
2607     /* We can raise a differed exception */
2608     gen_helper_float_check_status(cpu_env);
2609 }
2610
2611 /* mtfsfi */
2612 static void gen_mtfsfi(DisasContext *ctx)
2613 {
2614     int bf, sh, w;
2615     TCGv_i64 t0;
2616     TCGv_i32 t1;
2617
2618     if (unlikely(!ctx->fpu_enabled)) {
2619         gen_exception(ctx, POWERPC_EXCP_FPU);
2620         return;
2621     }
2622     w = FPW(ctx->opcode);
2623     bf = FPBF(ctx->opcode);
2624     if (unlikely(w & !(ctx->insns_flags2 & PPC2_ISA205))) {
2625         gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL);
2626         return;
2627     }
2628     sh = (8 * w) + 7 - bf;
2629     /* NIP cannot be restored if the memory exception comes from an helper */
2630     gen_update_nip(ctx, ctx->nip - 4);
2631     gen_reset_fpstatus();
2632     t0 = tcg_const_i64(((uint64_t)FPIMM(ctx->opcode)) << (4 * sh));
2633     t1 = tcg_const_i32(1 << sh);
2634     gen_helper_store_fpscr(cpu_env, t0, t1);
2635     tcg_temp_free_i64(t0);
2636     tcg_temp_free_i32(t1);
2637     if (unlikely(Rc(ctx->opcode) != 0)) {
2638         tcg_gen_trunc_tl_i32(cpu_crf[1], cpu_fpscr);
2639         tcg_gen_shri_i32(cpu_crf[1], cpu_crf[1], FPSCR_OX);
2640     }
2641     /* We can raise a differed exception */
2642     gen_helper_float_check_status(cpu_env);
2643 }
2644
2645 /***                           Addressing modes                            ***/
2646 /* Register indirect with immediate index : EA = (rA|0) + SIMM */
2647 static inline void gen_addr_imm_index(DisasContext *ctx, TCGv EA,
2648                                       target_long maskl)
2649 {
2650     target_long simm = SIMM(ctx->opcode);
2651
2652     simm &= ~maskl;
2653     if (rA(ctx->opcode) == 0) {
2654         if (NARROW_MODE(ctx)) {
2655             simm = (uint32_t)simm;
2656         }
2657         tcg_gen_movi_tl(EA, simm);
2658     } else if (likely(simm != 0)) {
2659         tcg_gen_addi_tl(EA, cpu_gpr[rA(ctx->opcode)], simm);
2660         if (NARROW_MODE(ctx)) {
2661             tcg_gen_ext32u_tl(EA, EA);
2662         }
2663     } else {
2664         if (NARROW_MODE(ctx)) {
2665             tcg_gen_ext32u_tl(EA, cpu_gpr[rA(ctx->opcode)]);
2666         } else {
2667             tcg_gen_mov_tl(EA, cpu_gpr[rA(ctx->opcode)]);
2668         }
2669     }
2670 }
2671
2672 static inline void gen_addr_reg_index(DisasContext *ctx, TCGv EA)
2673 {
2674     if (rA(ctx->opcode) == 0) {
2675         if (NARROW_MODE(ctx)) {
2676             tcg_gen_ext32u_tl(EA, cpu_gpr[rB(ctx->opcode)]);
2677         } else {
2678             tcg_gen_mov_tl(EA, cpu_gpr[rB(ctx->opcode)]);
2679         }
2680     } else {
2681         tcg_gen_add_tl(EA, cpu_gpr[rA(ctx->opcode)], cpu_gpr[rB(ctx->opcode)]);
2682         if (NARROW_MODE(ctx)) {
2683             tcg_gen_ext32u_tl(EA, EA);
2684         }
2685     }
2686 }
2687
2688 static inline void gen_addr_register(DisasContext *ctx, TCGv EA)
2689 {
2690     if (rA(ctx->opcode) == 0) {
2691         tcg_gen_movi_tl(EA, 0);
2692     } else if (NARROW_MODE(ctx)) {
2693         tcg_gen_ext32u_tl(EA, cpu_gpr[rA(ctx->opcode)]);
2694     } else {
2695         tcg_gen_mov_tl(EA, cpu_gpr[rA(ctx->opcode)]);
2696     }
2697 }
2698
2699 static inline void gen_addr_add(DisasContext *ctx, TCGv ret, TCGv arg1,
2700                                 target_long val)
2701 {
2702     tcg_gen_addi_tl(ret, arg1, val);
2703     if (NARROW_MODE(ctx)) {
2704         tcg_gen_ext32u_tl(ret, ret);
2705     }
2706 }
2707
2708 static inline void gen_check_align(DisasContext *ctx, TCGv EA, int mask)
2709 {
2710     int l1 = gen_new_label();
2711     TCGv t0 = tcg_temp_new();
2712     TCGv_i32 t1, t2;
2713     /* NIP cannot be restored if the memory exception comes from an helper */
2714     gen_update_nip(ctx, ctx->nip - 4);
2715     tcg_gen_andi_tl(t0, EA, mask);
2716     tcg_gen_brcondi_tl(TCG_COND_EQ, t0, 0, l1);
2717     t1 = tcg_const_i32(POWERPC_EXCP_ALIGN);
2718     t2 = tcg_const_i32(0);
2719     gen_helper_raise_exception_err(cpu_env, t1, t2);
2720     tcg_temp_free_i32(t1);
2721     tcg_temp_free_i32(t2);
2722     gen_set_label(l1);
2723     tcg_temp_free(t0);
2724 }
2725
2726 /***                             Integer load                              ***/
2727 static inline void gen_qemu_ld8u(DisasContext *ctx, TCGv arg1, TCGv arg2)
2728 {
2729     tcg_gen_qemu_ld8u(arg1, arg2, ctx->mem_idx);
2730 }
2731
2732 static inline void gen_qemu_ld16u(DisasContext *ctx, TCGv arg1, TCGv arg2)
2733 {
2734     TCGMemOp op = MO_UW | ctx->default_tcg_memop_mask;
2735     tcg_gen_qemu_ld_tl(arg1, arg2, ctx->mem_idx, op);
2736 }
2737
2738 static inline void gen_qemu_ld16s(DisasContext *ctx, TCGv arg1, TCGv arg2)
2739 {
2740     TCGMemOp op = MO_SW | ctx->default_tcg_memop_mask;
2741     tcg_gen_qemu_ld_tl(arg1, arg2, ctx->mem_idx, op);
2742 }
2743
2744 static inline void gen_qemu_ld32u(DisasContext *ctx, TCGv arg1, TCGv arg2)
2745 {
2746     TCGMemOp op = MO_UL | ctx->default_tcg_memop_mask;
2747     tcg_gen_qemu_ld_tl(arg1, arg2, ctx->mem_idx, op);
2748 }
2749
2750 static void gen_qemu_ld32u_i64(DisasContext *ctx, TCGv_i64 val, TCGv addr)
2751 {
2752     TCGv tmp = tcg_temp_new();
2753     gen_qemu_ld32u(ctx, tmp, addr);
2754     tcg_gen_extu_tl_i64(val, tmp);
2755     tcg_temp_free(tmp);
2756 }
2757
2758 static inline void gen_qemu_ld32s(DisasContext *ctx, TCGv arg1, TCGv arg2)
2759 {
2760     TCGMemOp op = MO_SL | ctx->default_tcg_memop_mask;
2761     tcg_gen_qemu_ld_tl(arg1, arg2, ctx->mem_idx, op);
2762 }
2763
2764 static void gen_qemu_ld32s_i64(DisasContext *ctx, TCGv_i64 val, TCGv addr)
2765 {
2766     TCGv tmp = tcg_temp_new();
2767     gen_qemu_ld32s(ctx, tmp, addr);
2768     tcg_gen_ext_tl_i64(val, tmp);
2769     tcg_temp_free(tmp);
2770 }
2771
2772 static inline void gen_qemu_ld64(DisasContext *ctx, TCGv_i64 arg1, TCGv arg2)
2773 {
2774     TCGMemOp op = MO_Q | ctx->default_tcg_memop_mask;
2775     tcg_gen_qemu_ld_i64(arg1, arg2, ctx->mem_idx, op);
2776 }
2777
2778 static inline void gen_qemu_st8(DisasContext *ctx, TCGv arg1, TCGv arg2)
2779 {
2780     tcg_gen_qemu_st8(arg1, arg2, ctx->mem_idx);
2781 }
2782
2783 static inline void gen_qemu_st16(DisasContext *ctx, TCGv arg1, TCGv arg2)
2784 {
2785     TCGMemOp op = MO_UW | ctx->default_tcg_memop_mask;
2786     tcg_gen_qemu_st_tl(arg1, arg2, ctx->mem_idx, op);
2787 }
2788
2789 static inline void gen_qemu_st32(DisasContext *ctx, TCGv arg1, TCGv arg2)
2790 {
2791     TCGMemOp op = MO_UL | ctx->default_tcg_memop_mask;
2792     tcg_gen_qemu_st_tl(arg1, arg2, ctx->mem_idx, op);
2793 }
2794
2795 static void gen_qemu_st32_i64(DisasContext *ctx, TCGv_i64 val, TCGv addr)
2796 {
2797     TCGv tmp = tcg_temp_new();
2798     tcg_gen_trunc_i64_tl(tmp, val);
2799     gen_qemu_st32(ctx, tmp, addr);
2800     tcg_temp_free(tmp);
2801 }
2802
2803 static inline void gen_qemu_st64(DisasContext *ctx, TCGv_i64 arg1, TCGv arg2)
2804 {
2805     TCGMemOp op = MO_Q | ctx->default_tcg_memop_mask;
2806     tcg_gen_qemu_st_i64(arg1, arg2, ctx->mem_idx, op);
2807 }
2808
2809 #define GEN_LD(name, ldop, opc, type)                                         \
2810 static void glue(gen_, name)(DisasContext *ctx)                                       \
2811 {                                                                             \
2812     TCGv EA;                                                                  \
2813     gen_set_access_type(ctx, ACCESS_INT);                                     \
2814     EA = tcg_temp_new();                                                      \
2815     gen_addr_imm_index(ctx, EA, 0);                                           \
2816     gen_qemu_##ldop(ctx, cpu_gpr[rD(ctx->opcode)], EA);                       \
2817     tcg_temp_free(EA);                                                        \
2818 }
2819
2820 #define GEN_LDU(name, ldop, opc, type)                                        \
2821 static void glue(gen_, name##u)(DisasContext *ctx)                                    \
2822 {                                                                             \
2823     TCGv EA;                                                                  \
2824     if (unlikely(rA(ctx->opcode) == 0 ||                                      \
2825                  rA(ctx->opcode) == rD(ctx->opcode))) {                       \
2826         gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL);                   \
2827         return;                                                               \
2828     }                                                                         \
2829     gen_set_access_type(ctx, ACCESS_INT);                                     \
2830     EA = tcg_temp_new();                                                      \
2831     if (type == PPC_64B)                                                      \
2832         gen_addr_imm_index(ctx, EA, 0x03);                                    \
2833     else                                                                      \
2834         gen_addr_imm_index(ctx, EA, 0);                                       \
2835     gen_qemu_##ldop(ctx, cpu_gpr[rD(ctx->opcode)], EA);                       \
2836     tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], EA);                             \
2837     tcg_temp_free(EA);                                                        \
2838 }
2839
2840 #define GEN_LDUX(name, ldop, opc2, opc3, type)                                \
2841 static void glue(gen_, name##ux)(DisasContext *ctx)                                   \
2842 {                                                                             \
2843     TCGv EA;                                                                  \
2844     if (unlikely(rA(ctx->opcode) == 0 ||                                      \
2845                  rA(ctx->opcode) == rD(ctx->opcode))) {                       \
2846         gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL);                   \
2847         return;                                                               \
2848     }                                                                         \
2849     gen_set_access_type(ctx, ACCESS_INT);                                     \
2850     EA = tcg_temp_new();                                                      \
2851     gen_addr_reg_index(ctx, EA);                                              \
2852     gen_qemu_##ldop(ctx, cpu_gpr[rD(ctx->opcode)], EA);                       \
2853     tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], EA);                             \
2854     tcg_temp_free(EA);                                                        \
2855 }
2856
2857 #define GEN_LDX_E(name, ldop, opc2, opc3, type, type2)                        \
2858 static void glue(gen_, name##x)(DisasContext *ctx)                            \
2859 {                                                                             \
2860     TCGv EA;                                                                  \
2861     gen_set_access_type(ctx, ACCESS_INT);                                     \
2862     EA = tcg_temp_new();                                                      \
2863     gen_addr_reg_index(ctx, EA);                                              \
2864     gen_qemu_##ldop(ctx, cpu_gpr[rD(ctx->opcode)], EA);                       \
2865     tcg_temp_free(EA);                                                        \
2866 }
2867 #define GEN_LDX(name, ldop, opc2, opc3, type)                                 \
2868     GEN_LDX_E(name, ldop, opc2, opc3, type, PPC_NONE)
2869
2870 #define GEN_LDS(name, ldop, op, type)                                         \
2871 GEN_LD(name, ldop, op | 0x20, type);                                          \
2872 GEN_LDU(name, ldop, op | 0x21, type);                                         \
2873 GEN_LDUX(name, ldop, 0x17, op | 0x01, type);                                  \
2874 GEN_LDX(name, ldop, 0x17, op | 0x00, type)
2875
2876 /* lbz lbzu lbzux lbzx */
2877 GEN_LDS(lbz, ld8u, 0x02, PPC_INTEGER);
2878 /* lha lhau lhaux lhax */
2879 GEN_LDS(lha, ld16s, 0x0A, PPC_INTEGER);
2880 /* lhz lhzu lhzux lhzx */
2881 GEN_LDS(lhz, ld16u, 0x08, PPC_INTEGER);
2882 /* lwz lwzu lwzux lwzx */
2883 GEN_LDS(lwz, ld32u, 0x00, PPC_INTEGER);
2884 #if defined(TARGET_PPC64)
2885 /* lwaux */
2886 GEN_LDUX(lwa, ld32s, 0x15, 0x0B, PPC_64B);
2887 /* lwax */
2888 GEN_LDX(lwa, ld32s, 0x15, 0x0A, PPC_64B);
2889 /* ldux */
2890 GEN_LDUX(ld, ld64, 0x15, 0x01, PPC_64B);
2891 /* ldx */
2892 GEN_LDX(ld, ld64, 0x15, 0x00, PPC_64B);
2893
2894 static void gen_ld(DisasContext *ctx)
2895 {
2896     TCGv EA;
2897     if (Rc(ctx->opcode)) {
2898         if (unlikely(rA(ctx->opcode) == 0 ||
2899                      rA(ctx->opcode) == rD(ctx->opcode))) {
2900             gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL);
2901             return;
2902         }
2903     }
2904     gen_set_access_type(ctx, ACCESS_INT);
2905     EA = tcg_temp_new();
2906     gen_addr_imm_index(ctx, EA, 0x03);
2907     if (ctx->opcode & 0x02) {
2908         /* lwa (lwau is undefined) */
2909         gen_qemu_ld32s(ctx, cpu_gpr[rD(ctx->opcode)], EA);
2910     } else {
2911         /* ld - ldu */
2912         gen_qemu_ld64(ctx, cpu_gpr[rD(ctx->opcode)], EA);
2913     }
2914     if (Rc(ctx->opcode))
2915         tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], EA);
2916     tcg_temp_free(EA);
2917 }
2918
2919 /* lq */
2920 static void gen_lq(DisasContext *ctx)
2921 {
2922     int ra, rd;
2923     TCGv EA;
2924
2925     /* lq is a legal user mode instruction starting in ISA 2.07 */
2926     bool legal_in_user_mode = (ctx->insns_flags2 & PPC2_LSQ_ISA207) != 0;
2927     bool le_is_supported = (ctx->insns_flags2 & PPC2_LSQ_ISA207) != 0;
2928
2929     if (!legal_in_user_mode && ctx->pr) {
2930         gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
2931         return;
2932     }
2933
2934     if (!le_is_supported && ctx->le_mode) {
2935         gen_exception_err(ctx, POWERPC_EXCP_ALIGN, POWERPC_EXCP_ALIGN_LE);
2936         return;
2937     }
2938
2939     ra = rA(ctx->opcode);
2940     rd = rD(ctx->opcode);
2941     if (unlikely((rd & 1) || rd == ra)) {
2942         gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL);
2943         return;
2944     }
2945
2946     gen_set_access_type(ctx, ACCESS_INT);
2947     EA = tcg_temp_new();
2948     gen_addr_imm_index(ctx, EA, 0x0F);
2949
2950     /* We only need to swap high and low halves. gen_qemu_ld64 does necessary
2951        64-bit byteswap already. */
2952     if (unlikely(ctx->le_mode)) {
2953         gen_qemu_ld64(ctx, cpu_gpr[rd+1], EA);
2954         gen_addr_add(ctx, EA, EA, 8);
2955         gen_qemu_ld64(ctx, cpu_gpr[rd], EA);
2956     } else {
2957         gen_qemu_ld64(ctx, cpu_gpr[rd], EA);
2958         gen_addr_add(ctx, EA, EA, 8);
2959         gen_qemu_ld64(ctx, cpu_gpr[rd+1], EA);
2960     }
2961     tcg_temp_free(EA);
2962 }
2963 #endif
2964
2965 /***                              Integer store                            ***/
2966 #define GEN_ST(name, stop, opc, type)                                         \
2967 static void glue(gen_, name)(DisasContext *ctx)                                       \
2968 {                                                                             \
2969     TCGv EA;                                                                  \
2970     gen_set_access_type(ctx, ACCESS_INT);                                     \
2971     EA = tcg_temp_new();                                                      \
2972     gen_addr_imm_index(ctx, EA, 0);                                           \
2973     gen_qemu_##stop(ctx, cpu_gpr[rS(ctx->opcode)], EA);                       \
2974     tcg_temp_free(EA);                                                        \
2975 }
2976
2977 #define GEN_STU(name, stop, opc, type)                                        \
2978 static void glue(gen_, stop##u)(DisasContext *ctx)                                    \
2979 {                                                                             \
2980     TCGv EA;                                                                  \
2981     if (unlikely(rA(ctx->opcode) == 0)) {                                     \
2982         gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL);                   \
2983         return;                                                               \
2984     }                                                                         \
2985     gen_set_access_type(ctx, ACCESS_INT);                                     \
2986     EA = tcg_temp_new();                                                      \
2987     if (type == PPC_64B)                                                      \
2988         gen_addr_imm_index(ctx, EA, 0x03);                                    \
2989     else                                                                      \
2990         gen_addr_imm_index(ctx, EA, 0);                                       \
2991     gen_qemu_##stop(ctx, cpu_gpr[rS(ctx->opcode)], EA);                       \
2992     tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], EA);                             \
2993     tcg_temp_free(EA);                                                        \
2994 }
2995
2996 #define GEN_STUX(name, stop, opc2, opc3, type)                                \
2997 static void glue(gen_, name##ux)(DisasContext *ctx)                                   \
2998 {                                                                             \
2999     TCGv EA;                                                                  \
3000     if (unlikely(rA(ctx->opcode) == 0)) {                                     \
3001         gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL);                   \
3002         return;                                                               \
3003     }                                                                         \
3004     gen_set_access_type(ctx, ACCESS_INT);                                     \
3005     EA = tcg_temp_new();                                                      \
3006     gen_addr_reg_index(ctx, EA);                                              \
3007     gen_qemu_##stop(ctx, cpu_gpr[rS(ctx->opcode)], EA);                       \
3008     tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], EA);                             \
3009     tcg_temp_free(EA);                                                        \
3010 }
3011
3012 #define GEN_STX_E(name, stop, opc2, opc3, type, type2)                        \
3013 static void glue(gen_, name##x)(DisasContext *ctx)                            \
3014 {                                                                             \
3015     TCGv EA;                                                                  \
3016     gen_set_access_type(ctx, ACCESS_INT);                                     \
3017     EA = tcg_temp_new();                                                      \
3018     gen_addr_reg_index(ctx, EA);                                              \
3019     gen_qemu_##stop(ctx, cpu_gpr[rS(ctx->opcode)], EA);                       \
3020     tcg_temp_free(EA);                                                        \
3021 }
3022 #define GEN_STX(name, stop, opc2, opc3, type)                                 \
3023     GEN_STX_E(name, stop, opc2, opc3, type, PPC_NONE)
3024
3025 #define GEN_STS(name, stop, op, type)                                         \
3026 GEN_ST(name, stop, op | 0x20, type);                                          \
3027 GEN_STU(name, stop, op | 0x21, type);                                         \
3028 GEN_STUX(name, stop, 0x17, op | 0x01, type);                                  \
3029 GEN_STX(name, stop, 0x17, op | 0x00, type)
3030
3031 /* stb stbu stbux stbx */
3032 GEN_STS(stb, st8, 0x06, PPC_INTEGER);
3033 /* sth sthu sthux sthx */
3034 GEN_STS(sth, st16, 0x0C, PPC_INTEGER);
3035 /* stw stwu stwux stwx */
3036 GEN_STS(stw, st32, 0x04, PPC_INTEGER);
3037 #if defined(TARGET_PPC64)
3038 GEN_STUX(std, st64, 0x15, 0x05, PPC_64B);
3039 GEN_STX(std, st64, 0x15, 0x04, PPC_64B);
3040
3041 static void gen_std(DisasContext *ctx)
3042 {
3043     int rs;
3044     TCGv EA;
3045
3046     rs = rS(ctx->opcode);
3047     if ((ctx->opcode & 0x3) == 0x2) { /* stq */
3048
3049         bool legal_in_user_mode = (ctx->insns_flags2 & PPC2_LSQ_ISA207) != 0;
3050         bool le_is_supported = (ctx->insns_flags2 & PPC2_LSQ_ISA207) != 0;
3051
3052         if (!legal_in_user_mode && ctx->pr) {
3053             gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
3054             return;
3055         }
3056
3057         if (!le_is_supported && ctx->le_mode) {
3058             gen_exception_err(ctx, POWERPC_EXCP_ALIGN, POWERPC_EXCP_ALIGN_LE);
3059             return;
3060         }
3061
3062         if (unlikely(rs & 1)) {
3063             gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL);
3064             return;
3065         }
3066         gen_set_access_type(ctx, ACCESS_INT);
3067         EA = tcg_temp_new();
3068         gen_addr_imm_index(ctx, EA, 0x03);
3069
3070         /* We only need to swap high and low halves. gen_qemu_st64 does
3071            necessary 64-bit byteswap already. */
3072         if (unlikely(ctx->le_mode)) {
3073             gen_qemu_st64(ctx, cpu_gpr[rs+1], EA);
3074             gen_addr_add(ctx, EA, EA, 8);
3075             gen_qemu_st64(ctx, cpu_gpr[rs], EA);
3076         } else {
3077             gen_qemu_st64(ctx, cpu_gpr[rs], EA);
3078             gen_addr_add(ctx, EA, EA, 8);
3079             gen_qemu_st64(ctx, cpu_gpr[rs+1], EA);
3080         }
3081         tcg_temp_free(EA);
3082     } else {
3083         /* std / stdu*/
3084         if (Rc(ctx->opcode)) {
3085             if (unlikely(rA(ctx->opcode) == 0)) {
3086                 gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL);
3087                 return;
3088             }
3089         }
3090         gen_set_access_type(ctx, ACCESS_INT);
3091         EA = tcg_temp_new();
3092         gen_addr_imm_index(ctx, EA, 0x03);
3093         gen_qemu_st64(ctx, cpu_gpr[rs], EA);
3094         if (Rc(ctx->opcode))
3095             tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], EA);
3096         tcg_temp_free(EA);
3097     }
3098 }
3099 #endif
3100 /***                Integer load and store with byte reverse               ***/
3101
3102 /* lhbrx */
3103 static inline void gen_qemu_ld16ur(DisasContext *ctx, TCGv arg1, TCGv arg2)
3104 {
3105     TCGMemOp op = MO_UW | (ctx->default_tcg_memop_mask ^ MO_BSWAP);
3106     tcg_gen_qemu_ld_tl(arg1, arg2, ctx->mem_idx, op);
3107 }
3108 GEN_LDX(lhbr, ld16ur, 0x16, 0x18, PPC_INTEGER);
3109
3110 /* lwbrx */
3111 static inline void gen_qemu_ld32ur(DisasContext *ctx, TCGv arg1, TCGv arg2)
3112 {
3113     TCGMemOp op = MO_UL | (ctx->default_tcg_memop_mask ^ MO_BSWAP);
3114     tcg_gen_qemu_ld_tl(arg1, arg2, ctx->mem_idx, op);
3115 }
3116 GEN_LDX(lwbr, ld32ur, 0x16, 0x10, PPC_INTEGER);
3117
3118 #if defined(TARGET_PPC64)
3119 /* ldbrx */
3120 static inline void gen_qemu_ld64ur(DisasContext *ctx, TCGv arg1, TCGv arg2)
3121 {
3122     TCGMemOp op = MO_Q | (ctx->default_tcg_memop_mask ^ MO_BSWAP);
3123     tcg_gen_qemu_ld_i64(arg1, arg2, ctx->mem_idx, op);
3124 }
3125 GEN_LDX_E(ldbr, ld64ur, 0x14, 0x10, PPC_NONE, PPC2_DBRX);
3126 #endif  /* TARGET_PPC64 */
3127
3128 /* sthbrx */
3129 static inline void gen_qemu_st16r(DisasContext *ctx, TCGv arg1, TCGv arg2)
3130 {
3131     TCGMemOp op = MO_UW | (ctx->default_tcg_memop_mask ^ MO_BSWAP);
3132     tcg_gen_qemu_st_tl(arg1, arg2, ctx->mem_idx, op);
3133 }
3134 GEN_STX(sthbr, st16r, 0x16, 0x1C, PPC_INTEGER);
3135
3136 /* stwbrx */
3137 static inline void gen_qemu_st32r(DisasContext *ctx, TCGv arg1, TCGv arg2)
3138 {
3139     TCGMemOp op = MO_UL | (ctx->default_tcg_memop_mask ^ MO_BSWAP);
3140     tcg_gen_qemu_st_tl(arg1, arg2, ctx->mem_idx, op);
3141 }
3142 GEN_STX(stwbr, st32r, 0x16, 0x14, PPC_INTEGER);
3143
3144 #if defined(TARGET_PPC64)
3145 /* stdbrx */
3146 static inline void gen_qemu_st64r(DisasContext *ctx, TCGv arg1, TCGv arg2)
3147 {
3148     TCGMemOp op = MO_Q | (ctx->default_tcg_memop_mask ^ MO_BSWAP);
3149     tcg_gen_qemu_st_i64(arg1, arg2, ctx->mem_idx, op);
3150 }
3151 GEN_STX_E(stdbr, st64r, 0x14, 0x14, PPC_NONE, PPC2_DBRX);
3152 #endif  /* TARGET_PPC64 */
3153
3154 /***                    Integer load and store multiple                    ***/
3155
3156 /* lmw */
3157 static void gen_lmw(DisasContext *ctx)
3158 {
3159     TCGv t0;
3160     TCGv_i32 t1;
3161     gen_set_access_type(ctx, ACCESS_INT);
3162     /* NIP cannot be restored if the memory exception comes from an helper */
3163     gen_update_nip(ctx, ctx->nip - 4);
3164     t0 = tcg_temp_new();
3165     t1 = tcg_const_i32(rD(ctx->opcode));
3166     gen_addr_imm_index(ctx, t0, 0);
3167     gen_helper_lmw(cpu_env, t0, t1);
3168     tcg_temp_free(t0);
3169     tcg_temp_free_i32(t1);
3170 }
3171
3172 /* stmw */
3173 static void gen_stmw(DisasContext *ctx)
3174 {
3175     TCGv t0;
3176     TCGv_i32 t1;
3177     gen_set_access_type(ctx, ACCESS_INT);
3178     /* NIP cannot be restored if the memory exception comes from an helper */
3179     gen_update_nip(ctx, ctx->nip - 4);
3180     t0 = tcg_temp_new();
3181     t1 = tcg_const_i32(rS(ctx->opcode));
3182     gen_addr_imm_index(ctx, t0, 0);
3183     gen_helper_stmw(cpu_env, t0, t1);
3184     tcg_temp_free(t0);
3185     tcg_temp_free_i32(t1);
3186 }
3187
3188 /***                    Integer load and store strings                     ***/
3189
3190 /* lswi */
3191 /* PowerPC32 specification says we must generate an exception if
3192  * rA is in the range of registers to be loaded.
3193  * In an other hand, IBM says this is valid, but rA won't be loaded.
3194  * For now, I'll follow the spec...
3195  */
3196 static void gen_lswi(DisasContext *ctx)
3197 {
3198     TCGv t0;
3199     TCGv_i32 t1, t2;
3200     int nb = NB(ctx->opcode);
3201     int start = rD(ctx->opcode);
3202     int ra = rA(ctx->opcode);
3203     int nr;
3204
3205     if (nb == 0)
3206         nb = 32;
3207     nr = nb / 4;
3208     if (unlikely(((start + nr) > 32  &&
3209                   start <= ra && (start + nr - 32) > ra) ||
3210                  ((start + nr) <= 32 && start <= ra && (start + nr) > ra))) {
3211         gen_inval_exception(ctx, POWERPC_EXCP_INVAL_LSWX);
3212         return;
3213     }
3214     gen_set_access_type(ctx, ACCESS_INT);
3215     /* NIP cannot be restored if the memory exception comes from an helper */
3216     gen_update_nip(ctx, ctx->nip - 4);
3217     t0 = tcg_temp_new();
3218     gen_addr_register(ctx, t0);
3219     t1 = tcg_const_i32(nb);
3220     t2 = tcg_const_i32(start);
3221     gen_helper_lsw(cpu_env, t0, t1, t2);
3222     tcg_temp_free(t0);
3223     tcg_temp_free_i32(t1);
3224     tcg_temp_free_i32(t2);
3225 }
3226
3227 /* lswx */
3228 static void gen_lswx(DisasContext *ctx)
3229 {
3230     TCGv t0;
3231     TCGv_i32 t1, t2, t3;
3232     gen_set_access_type(ctx, ACCESS_INT);
3233     /* NIP cannot be restored if the memory exception comes from an helper */
3234     gen_update_nip(ctx, ctx->nip - 4);
3235     t0 = tcg_temp_new();
3236     gen_addr_reg_index(ctx, t0);
3237     t1 = tcg_const_i32(rD(ctx->opcode));
3238     t2 = tcg_const_i32(rA(ctx->opcode));
3239     t3 = tcg_const_i32(rB(ctx->opcode));
3240     gen_helper_lswx(cpu_env, t0, t1, t2, t3);
3241     tcg_temp_free(t0);
3242     tcg_temp_free_i32(t1);
3243     tcg_temp_free_i32(t2);
3244     tcg_temp_free_i32(t3);
3245 }
3246
3247 /* stswi */
3248 static void gen_stswi(DisasContext *ctx)
3249 {
3250     TCGv t0;
3251     TCGv_i32 t1, t2;
3252     int nb = NB(ctx->opcode);
3253     gen_set_access_type(ctx, ACCESS_INT);
3254     /* NIP cannot be restored if the memory exception comes from an helper */
3255     gen_update_nip(ctx, ctx->nip - 4);
3256     t0 = tcg_temp_new();
3257     gen_addr_register(ctx, t0);
3258     if (nb == 0)
3259         nb = 32;
3260     t1 = tcg_const_i32(nb);
3261     t2 = tcg_const_i32(rS(ctx->opcode));
3262     gen_helper_stsw(cpu_env, t0, t1, t2);
3263     tcg_temp_free(t0);
3264     tcg_temp_free_i32(t1);
3265     tcg_temp_free_i32(t2);
3266 }
3267
3268 /* stswx */
3269 static void gen_stswx(DisasContext *ctx)
3270 {
3271     TCGv t0;
3272     TCGv_i32 t1, t2;
3273     gen_set_access_type(ctx, ACCESS_INT);
3274     /* NIP cannot be restored if the memory exception comes from an helper */
3275     gen_update_nip(ctx, ctx->nip - 4);
3276     t0 = tcg_temp_new();
3277     gen_addr_reg_index(ctx, t0);
3278     t1 = tcg_temp_new_i32();
3279     tcg_gen_trunc_tl_i32(t1, cpu_xer);
3280     tcg_gen_andi_i32(t1, t1, 0x7F);
3281     t2 = tcg_const_i32(rS(ctx->opcode));
3282     gen_helper_stsw(cpu_env, t0, t1, t2);
3283     tcg_temp_free(t0);
3284     tcg_temp_free_i32(t1);
3285     tcg_temp_free_i32(t2);
3286 }
3287
3288 /***                        Memory synchronisation                         ***/
3289 /* eieio */
3290 static void gen_eieio(DisasContext *ctx)
3291 {
3292 }
3293
3294 /* isync */
3295 static void gen_isync(DisasContext *ctx)
3296 {
3297     gen_stop_exception(ctx);
3298 }
3299
3300 #define LARX(name, len, loadop)                                      \
3301 static void gen_##name(DisasContext *ctx)                            \
3302 {                                                                    \
3303     TCGv t0;                                                         \
3304     TCGv gpr = cpu_gpr[rD(ctx->opcode)];                             \
3305     gen_set_access_type(ctx, ACCESS_RES);                            \
3306     t0 = tcg_temp_local_new();                                       \
3307     gen_addr_reg_index(ctx, t0);                                     \
3308     if ((len) > 1) {                                                 \
3309         gen_check_align(ctx, t0, (len)-1);                           \
3310     }                                                                \
3311     gen_qemu_##loadop(ctx, gpr, t0);                                 \
3312     tcg_gen_mov_tl(cpu_reserve, t0);                                 \
3313     tcg_gen_st_tl(gpr, cpu_env, offsetof(CPUPPCState, reserve_val)); \
3314     tcg_temp_free(t0);                                               \
3315 }
3316
3317 /* lwarx */
3318 LARX(lbarx, 1, ld8u);
3319 LARX(lharx, 2, ld16u);
3320 LARX(lwarx, 4, ld32u);
3321
3322
3323 #if defined(CONFIG_USER_ONLY)
3324 static void gen_conditional_store(DisasContext *ctx, TCGv EA,
3325                                   int reg, int size)
3326 {
3327     TCGv t0 = tcg_temp_new();
3328     uint32_t save_exception = ctx->exception;
3329
3330     tcg_gen_st_tl(EA, cpu_env, offsetof(CPUPPCState, reserve_ea));
3331     tcg_gen_movi_tl(t0, (size << 5) | reg);
3332     tcg_gen_st_tl(t0, cpu_env, offsetof(CPUPPCState, reserve_info));
3333     tcg_temp_free(t0);
3334     gen_update_nip(ctx, ctx->nip-4);
3335     ctx->exception = POWERPC_EXCP_BRANCH;
3336     gen_exception(ctx, POWERPC_EXCP_STCX);
3337     ctx->exception = save_exception;
3338 }
3339 #else
3340 static void gen_conditional_store(DisasContext *ctx, TCGv EA,
3341                                   int reg, int size)
3342 {
3343     int l1;
3344
3345     tcg_gen_trunc_tl_i32(cpu_crf[0], cpu_so);
3346     l1 = gen_new_label();
3347     tcg_gen_brcond_tl(TCG_COND_NE, EA, cpu_reserve, l1);
3348     tcg_gen_ori_i32(cpu_crf[0], cpu_crf[0], 1 << CRF_EQ);
3349 #if defined(TARGET_PPC64)
3350     if (size == 8) {
3351         gen_qemu_st64(ctx, cpu_gpr[reg], EA);
3352     } else
3353 #endif
3354     if (size == 4) {
3355         gen_qemu_st32(ctx, cpu_gpr[reg], EA);
3356     } else if (size == 2) {
3357         gen_qemu_st16(ctx, cpu_gpr[reg], EA);
3358 #if defined(TARGET_PPC64)
3359     } else if (size == 16) {
3360         TCGv gpr1, gpr2 , EA8;
3361         if (unlikely(ctx->le_mode)) {
3362             gpr1 = cpu_gpr[reg+1];
3363             gpr2 = cpu_gpr[reg];
3364         } else {
3365             gpr1 = cpu_gpr[reg];
3366             gpr2 = cpu_gpr[reg+1];
3367         }
3368         gen_qemu_st64(ctx, gpr1, EA);
3369         EA8 = tcg_temp_local_new();
3370         gen_addr_add(ctx, EA8, EA, 8);
3371         gen_qemu_st64(ctx, gpr2, EA8);
3372         tcg_temp_free(EA8);
3373 #endif
3374     } else {
3375         gen_qemu_st8(ctx, cpu_gpr[reg], EA);
3376     }
3377     gen_set_label(l1);
3378     tcg_gen_movi_tl(cpu_reserve, -1);
3379 }
3380 #endif
3381
3382 #define STCX(name, len)                                   \
3383 static void gen_##name(DisasContext *ctx)                 \
3384 {                                                         \
3385     TCGv t0;                                              \
3386     if (unlikely((len == 16) && (rD(ctx->opcode) & 1))) { \
3387         gen_inval_exception(ctx,                          \
3388                             POWERPC_EXCP_INVAL_INVAL);    \
3389         return;                                           \
3390     }                                                     \
3391     gen_set_access_type(ctx, ACCESS_RES);                 \
3392     t0 = tcg_temp_local_new();                            \
3393     gen_addr_reg_index(ctx, t0);                          \
3394     if (len > 1) {                                        \
3395         gen_check_align(ctx, t0, (len)-1);                \
3396     }                                                     \
3397     gen_conditional_store(ctx, t0, rS(ctx->opcode), len); \
3398     tcg_temp_free(t0);                                    \
3399 }
3400
3401 STCX(stbcx_, 1);
3402 STCX(sthcx_, 2);
3403 STCX(stwcx_, 4);
3404
3405 #if defined(TARGET_PPC64)
3406 /* ldarx */
3407 LARX(ldarx, 8, ld64);
3408
3409 /* lqarx */
3410 static void gen_lqarx(DisasContext *ctx)
3411 {
3412     TCGv EA;
3413     int rd = rD(ctx->opcode);
3414     TCGv gpr1, gpr2;
3415
3416     if (unlikely((rd & 1) || (rd == rA(ctx->opcode)) ||
3417                  (rd == rB(ctx->opcode)))) {
3418         gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL);
3419         return;
3420     }
3421
3422     gen_set_access_type(ctx, ACCESS_RES);
3423     EA = tcg_temp_local_new();
3424     gen_addr_reg_index(ctx, EA);
3425     gen_check_align(ctx, EA, 15);
3426     if (unlikely(ctx->le_mode)) {
3427         gpr1 = cpu_gpr[rd+1];
3428         gpr2 = cpu_gpr[rd];
3429     } else {
3430         gpr1 = cpu_gpr[rd];
3431         gpr2 = cpu_gpr[rd+1];
3432     }
3433     gen_qemu_ld64(ctx, gpr1, EA);
3434     tcg_gen_mov_tl(cpu_reserve, EA);
3435
3436     gen_addr_add(ctx, EA, EA, 8);
3437     gen_qemu_ld64(ctx, gpr2, EA);
3438
3439     tcg_gen_st_tl(gpr1, cpu_env, offsetof(CPUPPCState, reserve_val));
3440     tcg_gen_st_tl(gpr2, cpu_env, offsetof(CPUPPCState, reserve_val2));
3441
3442     tcg_temp_free(EA);
3443 }
3444
3445 /* stdcx. */
3446 STCX(stdcx_, 8);
3447 STCX(stqcx_, 16);
3448 #endif /* defined(TARGET_PPC64) */
3449
3450 /* sync */
3451 static void gen_sync(DisasContext *ctx)
3452 {
3453 }
3454
3455 /* wait */
3456 static void gen_wait(DisasContext *ctx)
3457 {
3458     TCGv_i32 t0 = tcg_temp_new_i32();
3459     tcg_gen_st_i32(t0, cpu_env,
3460                    -offsetof(PowerPCCPU, env) + offsetof(CPUState, halted));
3461     tcg_temp_free_i32(t0);
3462     /* Stop translation, as the CPU is supposed to sleep from now */
3463     gen_exception_err(ctx, EXCP_HLT, 1);
3464 }
3465
3466 /***                         Floating-point load                           ***/
3467 #define GEN_LDF(name, ldop, opc, type)                                        \
3468 static void glue(gen_, name)(DisasContext *ctx)                                       \
3469 {                                                                             \
3470     TCGv EA;                                                                  \
3471     if (unlikely(!ctx->fpu_enabled)) {                                        \
3472         gen_exception(ctx, POWERPC_EXCP_FPU);                                 \
3473         return;                                                               \
3474     }                                                                         \
3475     gen_set_access_type(ctx, ACCESS_FLOAT);                                   \
3476     EA = tcg_temp_new();                                                      \
3477     gen_addr_imm_index(ctx, EA, 0);                                           \
3478     gen_qemu_##ldop(ctx, cpu_fpr[rD(ctx->opcode)], EA);                       \
3479     tcg_temp_free(EA);                                                        \
3480 }
3481
3482 #define GEN_LDUF(name, ldop, opc, type)                                       \
3483 static void glue(gen_, name##u)(DisasContext *ctx)                                    \
3484 {                                                                             \
3485     TCGv EA;                                                                  \
3486     if (unlikely(!ctx->fpu_enabled)) {                                        \
3487         gen_exception(ctx, POWERPC_EXCP_FPU);                                 \
3488         return;                                                               \
3489     }                                                                         \
3490     if (unlikely(rA(ctx->opcode) == 0)) {                                     \
3491         gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL);                   \
3492         return;                                                               \
3493     }                                                                         \
3494     gen_set_access_type(ctx, ACCESS_FLOAT);                                   \
3495     EA = tcg_temp_new();                                                      \
3496     gen_addr_imm_index(ctx, EA, 0);                                           \
3497     gen_qemu_##ldop(ctx, cpu_fpr[rD(ctx->opcode)], EA);                       \
3498     tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], EA);                             \
3499     tcg_temp_free(EA);                                                        \
3500 }
3501
3502 #define GEN_LDUXF(name, ldop, opc, type)                                      \
3503 static void glue(gen_, name##ux)(DisasContext *ctx)                                   \
3504 {                                                                             \
3505     TCGv EA;                                                                  \
3506     if (unlikely(!ctx->fpu_enabled)) {                                        \
3507         gen_exception(ctx, POWERPC_EXCP_FPU);                                 \
3508         return;                                                               \
3509     }                                                                         \
3510     if (unlikely(rA(ctx->opcode) == 0)) {                                     \
3511         gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL);                   \
3512         return;                                                               \
3513     }                                                                         \
3514     gen_set_access_type(ctx, ACCESS_FLOAT);                                   \
3515     EA = tcg_temp_new();                                                      \
3516     gen_addr_reg_index(ctx, EA);                                              \
3517     gen_qemu_##ldop(ctx, cpu_fpr[rD(ctx->opcode)], EA);                       \
3518     tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], EA);                             \
3519     tcg_temp_free(EA);                                                        \
3520 }
3521
3522 #define GEN_LDXF(name, ldop, opc2, opc3, type)                                \
3523 static void glue(gen_, name##x)(DisasContext *ctx)                                    \
3524 {                                                                             \
3525     TCGv EA;                                                                  \
3526     if (unlikely(!ctx->fpu_enabled)) {                                        \
3527         gen_exception(ctx, POWERPC_EXCP_FPU);                                 \
3528         return;                                                               \
3529     }                                                                         \
3530     gen_set_access_type(ctx, ACCESS_FLOAT);                                   \
3531     EA = tcg_temp_new();                                                      \
3532     gen_addr_reg_index(ctx, EA);                                              \
3533     gen_qemu_##ldop(ctx, cpu_fpr[rD(ctx->opcode)], EA);                       \
3534     tcg_temp_free(EA);                                                        \
3535 }
3536
3537 #define GEN_LDFS(name, ldop, op, type)                                        \
3538 GEN_LDF(name, ldop, op | 0x20, type);                                         \
3539 GEN_LDUF(name, ldop, op | 0x21, type);                                        \
3540 GEN_LDUXF(name, ldop, op | 0x01, type);                                       \
3541 GEN_LDXF(name, ldop, 0x17, op | 0x00, type)
3542
3543 static inline void gen_qemu_ld32fs(DisasContext *ctx, TCGv_i64 arg1, TCGv arg2)
3544 {
3545     TCGv t0 = tcg_temp_new();
3546     TCGv_i32 t1 = tcg_temp_new_i32();
3547     gen_qemu_ld32u(ctx, t0, arg2);
3548     tcg_gen_trunc_tl_i32(t1, t0);
3549     tcg_temp_free(t0);
3550     gen_helper_float32_to_float64(arg1, cpu_env, t1);
3551     tcg_temp_free_i32(t1);
3552 }
3553
3554  /* lfd lfdu lfdux lfdx */
3555 GEN_LDFS(lfd, ld64, 0x12, PPC_FLOAT);
3556  /* lfs lfsu lfsux lfsx */
3557 GEN_LDFS(lfs, ld32fs, 0x10, PPC_FLOAT);
3558
3559 /* lfdp */
3560 static void gen_lfdp(DisasContext *ctx)
3561 {
3562     TCGv EA;
3563     if (unlikely(!ctx->fpu_enabled)) {
3564         gen_exception(ctx, POWERPC_EXCP_FPU);
3565         return;
3566     }
3567     gen_set_access_type(ctx, ACCESS_FLOAT);
3568     EA = tcg_temp_new();
3569     gen_addr_imm_index(ctx, EA, 0);
3570     /* We only need to swap high and low halves. gen_qemu_ld64 does necessary
3571        64-bit byteswap already. */
3572     if (unlikely(ctx->le_mode)) {
3573         gen_qemu_ld64(ctx, cpu_fpr[rD(ctx->opcode) + 1], EA);
3574         tcg_gen_addi_tl(EA, EA, 8);
3575         gen_qemu_ld64(ctx, cpu_fpr[rD(ctx->opcode)], EA);
3576     } else {
3577         gen_qemu_ld64(ctx, cpu_fpr[rD(ctx->opcode)], EA);
3578         tcg_gen_addi_tl(EA, EA, 8);
3579         gen_qemu_ld64(ctx, cpu_fpr[rD(ctx->opcode) + 1], EA);
3580     }
3581     tcg_temp_free(EA);
3582 }
3583
3584 /* lfdpx */
3585 static void gen_lfdpx(DisasContext *ctx)
3586 {
3587     TCGv EA;
3588     if (unlikely(!ctx->fpu_enabled)) {
3589         gen_exception(ctx, POWERPC_EXCP_FPU);
3590         return;
3591     }
3592     gen_set_access_type(ctx, ACCESS_FLOAT);
3593     EA = tcg_temp_new();
3594     gen_addr_reg_index(ctx, EA);
3595     /* We only need to swap high and low halves. gen_qemu_ld64 does necessary
3596        64-bit byteswap already. */
3597     if (unlikely(ctx->le_mode)) {
3598         gen_qemu_ld64(ctx, cpu_fpr[rD(ctx->opcode) + 1], EA);
3599         tcg_gen_addi_tl(EA, EA, 8);
3600         gen_qemu_ld64(ctx, cpu_fpr[rD(ctx->opcode)], EA);
3601     } else {
3602         gen_qemu_ld64(ctx, cpu_fpr[rD(ctx->opcode)], EA);
3603         tcg_gen_addi_tl(EA, EA, 8);
3604         gen_qemu_ld64(ctx, cpu_fpr[rD(ctx->opcode) + 1], EA);
3605     }
3606     tcg_temp_free(EA);
3607 }
3608
3609 /* lfiwax */
3610 static void gen_lfiwax(DisasContext *ctx)
3611 {
3612     TCGv EA;
3613     TCGv t0;
3614     if (unlikely(!ctx->fpu_enabled)) {
3615         gen_exception(ctx, POWERPC_EXCP_FPU);
3616         return;
3617     }
3618     gen_set_access_type(ctx, ACCESS_FLOAT);
3619     EA = tcg_temp_new();
3620     t0 = tcg_temp_new();
3621     gen_addr_reg_index(ctx, EA);
3622     gen_qemu_ld32s(ctx, t0, EA);
3623     tcg_gen_ext_tl_i64(cpu_fpr[rD(ctx->opcode)], t0);
3624     tcg_temp_free(EA);
3625     tcg_temp_free(t0);
3626 }
3627
3628 /* lfiwzx */
3629 static void gen_lfiwzx(DisasContext *ctx)
3630 {
3631     TCGv EA;
3632     if (unlikely(!ctx->fpu_enabled)) {
3633         gen_exception(ctx, POWERPC_EXCP_FPU);
3634         return;
3635     }
3636     gen_set_access_type(ctx, ACCESS_FLOAT);
3637     EA = tcg_temp_new();
3638     gen_addr_reg_index(ctx, EA);
3639     gen_qemu_ld32u_i64(ctx, cpu_fpr[rD(ctx->opcode)], EA);
3640     tcg_temp_free(EA);
3641 }
3642 /***                         Floating-point store                          ***/
3643 #define GEN_STF(name, stop, opc, type)                                        \
3644 static void glue(gen_, name)(DisasContext *ctx)                                       \
3645 {                                                                             \
3646     TCGv EA;                                                                  \
3647     if (unlikely(!ctx->fpu_enabled)) {                                        \
3648         gen_exception(ctx, POWERPC_EXCP_FPU);                                 \
3649         return;                                                               \
3650     }                                                                         \
3651     gen_set_access_type(ctx, ACCESS_FLOAT);                                   \
3652     EA = tcg_temp_new();                                                      \
3653     gen_addr_imm_index(ctx, EA, 0);                                           \
3654     gen_qemu_##stop(ctx, cpu_fpr[rS(ctx->opcode)], EA);                       \
3655     tcg_temp_free(EA);                                                        \
3656 }
3657
3658 #define GEN_STUF(name, stop, opc, type)                                       \
3659 static void glue(gen_, name##u)(DisasContext *ctx)                                    \
3660 {                                                                             \
3661     TCGv EA;                                                                  \
3662     if (unlikely(!ctx->fpu_enabled)) {                                        \
3663         gen_exception(ctx, POWERPC_EXCP_FPU);                                 \
3664         return;                                                               \
3665     }                                                                         \
3666     if (unlikely(rA(ctx->opcode) == 0)) {                                     \
3667         gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL);                   \
3668         return;                                                               \
3669     }                                                                         \
3670     gen_set_access_type(ctx, ACCESS_FLOAT);                                   \
3671     EA = tcg_temp_new();                                                      \
3672     gen_addr_imm_index(ctx, EA, 0);                                           \
3673     gen_qemu_##stop(ctx, cpu_fpr[rS(ctx->opcode)], EA);                       \
3674     tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], EA);                             \
3675     tcg_temp_free(EA);                                                        \
3676 }
3677
3678 #define GEN_STUXF(name, stop, opc, type)                                      \
3679 static void glue(gen_, name##ux)(DisasContext *ctx)                                   \
3680 {                                                                             \
3681     TCGv EA;                                                                  \
3682     if (unlikely(!ctx->fpu_enabled)) {                                        \
3683         gen_exception(ctx, POWERPC_EXCP_FPU);                                 \
3684         return;                                                               \
3685     }                                                                         \
3686     if (unlikely(rA(ctx->opcode) == 0)) {                                     \
3687         gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL);                   \
3688         return;                                                               \
3689     }                                                                         \
3690     gen_set_access_type(ctx, ACCESS_FLOAT);                                   \
3691     EA = tcg_temp_new();                                                      \
3692     gen_addr_reg_index(ctx, EA);                                              \
3693     gen_qemu_##stop(ctx, cpu_fpr[rS(ctx->opcode)], EA);                       \
3694     tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], EA);                             \
3695     tcg_temp_free(EA);                                                        \
3696 }
3697
3698 #define GEN_STXF(name, stop, opc2, opc3, type)                                \
3699 static void glue(gen_, name##x)(DisasContext *ctx)                                    \
3700 {                                                                             \
3701     TCGv EA;                                                                  \
3702     if (unlikely(!ctx->fpu_enabled)) {                                        \
3703         gen_exception(ctx, POWERPC_EXCP_FPU);                                 \
3704         return;                                                               \
3705     }                                                                         \
3706     gen_set_access_type(ctx, ACCESS_FLOAT);                                   \
3707     EA = tcg_temp_new();                                                      \
3708     gen_addr_reg_index(ctx, EA);                                              \
3709     gen_qemu_##stop(ctx, cpu_fpr[rS(ctx->opcode)], EA);                       \
3710     tcg_temp_free(EA);                                                        \
3711 }
3712
3713 #define GEN_STFS(name, stop, op, type)                                        \
3714 GEN_STF(name, stop, op | 0x20, type);                                         \
3715 GEN_STUF(name, stop, op | 0x21, type);                                        \
3716 GEN_STUXF(name, stop, op | 0x01, type);                                       \
3717 GEN_STXF(name, stop, 0x17, op | 0x00, type)
3718
3719 static inline void gen_qemu_st32fs(DisasContext *ctx, TCGv_i64 arg1, TCGv arg2)
3720 {
3721     TCGv_i32 t0 = tcg_temp_new_i32();
3722     TCGv t1 = tcg_temp_new();
3723     gen_helper_float64_to_float32(t0, cpu_env, arg1);
3724     tcg_gen_extu_i32_tl(t1, t0);
3725     tcg_temp_free_i32(t0);
3726     gen_qemu_st32(ctx, t1, arg2);
3727     tcg_temp_free(t1);
3728 }
3729
3730 /* stfd stfdu stfdux stfdx */
3731 GEN_STFS(stfd, st64, 0x16, PPC_FLOAT);
3732 /* stfs stfsu stfsux stfsx */
3733 GEN_STFS(stfs, st32fs, 0x14, PPC_FLOAT);
3734
3735 /* stfdp */
3736 static void gen_stfdp(DisasContext *ctx)
3737 {
3738     TCGv EA;
3739     if (unlikely(!ctx->fpu_enabled)) {
3740         gen_exception(ctx, POWERPC_EXCP_FPU);
3741         return;
3742     }
3743     gen_set_access_type(ctx, ACCESS_FLOAT);
3744     EA = tcg_temp_new();
3745     gen_addr_imm_index(ctx, EA, 0);
3746     /* We only need to swap high and low halves. gen_qemu_st64 does necessary
3747        64-bit byteswap already. */
3748     if (unlikely(ctx->le_mode)) {
3749         gen_qemu_st64(ctx, cpu_fpr[rD(ctx->opcode) + 1], EA);
3750         tcg_gen_addi_tl(EA, EA, 8);
3751         gen_qemu_st64(ctx, cpu_fpr[rD(ctx->opcode)], EA);
3752     } else {
3753         gen_qemu_st64(ctx, cpu_fpr[rD(ctx->opcode)], EA);
3754         tcg_gen_addi_tl(EA, EA, 8);
3755         gen_qemu_st64(ctx, cpu_fpr[rD(ctx->opcode) + 1], EA);
3756     }
3757     tcg_temp_free(EA);
3758 }
3759
3760 /* stfdpx */
3761 static void gen_stfdpx(DisasContext *ctx)
3762 {
3763     TCGv EA;
3764     if (unlikely(!ctx->fpu_enabled)) {
3765         gen_exception(ctx, POWERPC_EXCP_FPU);
3766         return;
3767     }
3768     gen_set_access_type(ctx, ACCESS_FLOAT);
3769     EA = tcg_temp_new();
3770     gen_addr_reg_index(ctx, EA);
3771     /* We only need to swap high and low halves. gen_qemu_st64 does necessary
3772        64-bit byteswap already. */
3773     if (unlikely(ctx->le_mode)) {
3774         gen_qemu_st64(ctx, cpu_fpr[rD(ctx->opcode) + 1], EA);
3775         tcg_gen_addi_tl(EA, EA, 8);
3776         gen_qemu_st64(ctx, cpu_fpr[rD(ctx->opcode)], EA);
3777     } else {
3778         gen_qemu_st64(ctx, cpu_fpr[rD(ctx->opcode)], EA);
3779         tcg_gen_addi_tl(EA, EA, 8);
3780         gen_qemu_st64(ctx, cpu_fpr[rD(ctx->opcode) + 1], EA);
3781     }
3782     tcg_temp_free(EA);
3783 }
3784
3785 /* Optional: */
3786 static inline void gen_qemu_st32fiw(DisasContext *ctx, TCGv_i64 arg1, TCGv arg2)
3787 {
3788     TCGv t0 = tcg_temp_new();
3789     tcg_gen_trunc_i64_tl(t0, arg1),
3790     gen_qemu_st32(ctx, t0, arg2);
3791     tcg_temp_free(t0);
3792 }
3793 /* stfiwx */
3794 GEN_STXF(stfiw, st32fiw, 0x17, 0x1E, PPC_FLOAT_STFIWX);
3795
3796 static inline void gen_update_cfar(DisasContext *ctx, target_ulong nip)
3797 {
3798 #if defined(TARGET_PPC64)
3799     if (ctx->has_cfar)
3800         tcg_gen_movi_tl(cpu_cfar, nip);
3801 #endif
3802 }
3803
3804 /***                                Branch                                 ***/
3805 static inline void gen_goto_tb(DisasContext *ctx, int n, target_ulong dest)
3806 {
3807     TranslationBlock *tb;
3808     tb = ctx->tb;
3809     if (NARROW_MODE(ctx)) {
3810         dest = (uint32_t) dest;
3811     }
3812     if ((tb->pc & TARGET_PAGE_MASK) == (dest & TARGET_PAGE_MASK) &&
3813         likely(!ctx->singlestep_enabled)) {
3814         tcg_gen_goto_tb(n);
3815         tcg_gen_movi_tl(cpu_nip, dest & ~3);
3816         tcg_gen_exit_tb((uintptr_t)tb + n);
3817     } else {
3818         tcg_gen_movi_tl(cpu_nip, dest & ~3);
3819         if (unlikely(ctx->singlestep_enabled)) {
3820             if ((ctx->singlestep_enabled &
3821                 (CPU_BRANCH_STEP | CPU_SINGLE_STEP)) &&
3822                 (ctx->exception == POWERPC_EXCP_BRANCH ||
3823                  ctx->exception == POWERPC_EXCP_TRACE)) {
3824                 target_ulong tmp = ctx->nip;
3825                 ctx->nip = dest;
3826                 gen_exception(ctx, POWERPC_EXCP_TRACE);
3827                 ctx->nip = tmp;
3828             }
3829             if (ctx->singlestep_enabled & GDBSTUB_SINGLE_STEP) {
3830                 gen_debug_exception(ctx);
3831             }
3832         }
3833         tcg_gen_exit_tb(0);
3834     }
3835 }
3836
3837 static inline void gen_setlr(DisasContext *ctx, target_ulong nip)
3838 {
3839     if (NARROW_MODE(ctx)) {
3840         nip = (uint32_t)nip;
3841     }
3842     tcg_gen_movi_tl(cpu_lr, nip);
3843 }
3844
3845 /* b ba bl bla */
3846 static void gen_b(DisasContext *ctx)
3847 {
3848     target_ulong li, target;
3849
3850     ctx->exception = POWERPC_EXCP_BRANCH;
3851     /* sign extend LI */
3852     li = LI(ctx->opcode);
3853     li = (li ^ 0x02000000) - 0x02000000;
3854     if (likely(AA(ctx->opcode) == 0)) {
3855         target = ctx->nip + li - 4;
3856     } else {
3857         target = li;
3858     }
3859     if (LK(ctx->opcode)) {
3860         gen_setlr(ctx, ctx->nip);
3861     }
3862     gen_update_cfar(ctx, ctx->nip);
3863     gen_goto_tb(ctx, 0, target);
3864 }
3865
3866 #define BCOND_IM  0
3867 #define BCOND_LR  1
3868 #define BCOND_CTR 2
3869 #define BCOND_TAR 3
3870
3871 static inline void gen_bcond(DisasContext *ctx, int type)
3872 {
3873     uint32_t bo = BO(ctx->opcode);
3874     int l1;
3875     TCGv target;
3876
3877     ctx->exception = POWERPC_EXCP_BRANCH;
3878     if (type == BCOND_LR || type == BCOND_CTR || type == BCOND_TAR) {
3879         target = tcg_temp_local_new();
3880         if (type == BCOND_CTR)
3881             tcg_gen_mov_tl(target, cpu_ctr);
3882         else if (type == BCOND_TAR)
3883             gen_load_spr(target, SPR_TAR);
3884         else
3885             tcg_gen_mov_tl(target, cpu_lr);
3886     } else {
3887         TCGV_UNUSED(target);
3888     }
3889     if (LK(ctx->opcode))
3890         gen_setlr(ctx, ctx->nip);
3891     l1 = gen_new_label();
3892     if ((bo & 0x4) == 0) {
3893         /* Decrement and test CTR */
3894         TCGv temp = tcg_temp_new();
3895         if (unlikely(type == BCOND_CTR)) {
3896             gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL);
3897             return;
3898         }
3899         tcg_gen_subi_tl(cpu_ctr, cpu_ctr, 1);
3900         if (NARROW_MODE(ctx)) {
3901             tcg_gen_ext32u_tl(temp, cpu_ctr);
3902         } else {
3903             tcg_gen_mov_tl(temp, cpu_ctr);
3904         }
3905         if (bo & 0x2) {
3906             tcg_gen_brcondi_tl(TCG_COND_NE, temp, 0, l1);
3907         } else {
3908             tcg_gen_brcondi_tl(TCG_COND_EQ, temp, 0, l1);
3909         }
3910         tcg_temp_free(temp);
3911     }
3912     if ((bo & 0x10) == 0) {
3913         /* Test CR */
3914         uint32_t bi = BI(ctx->opcode);
3915         uint32_t mask = 0x08 >> (bi & 0x03);
3916         TCGv_i32 temp = tcg_temp_new_i32();
3917
3918         if (bo & 0x8) {
3919             tcg_gen_andi_i32(temp, cpu_crf[bi >> 2], mask);
3920             tcg_gen_brcondi_i32(TCG_COND_EQ, temp, 0, l1);
3921         } else {
3922             tcg_gen_andi_i32(temp, cpu_crf[bi >> 2], mask);
3923             tcg_gen_brcondi_i32(TCG_COND_NE, temp, 0, l1);
3924         }
3925         tcg_temp_free_i32(temp);
3926     }
3927     gen_update_cfar(ctx, ctx->nip);
3928     if (type == BCOND_IM) {
3929         target_ulong li = (target_long)((int16_t)(BD(ctx->opcode)));
3930         if (likely(AA(ctx->opcode) == 0)) {
3931             gen_goto_tb(ctx, 0, ctx->nip + li - 4);
3932         } else {
3933             gen_goto_tb(ctx, 0, li);
3934         }
3935         gen_set_label(l1);
3936         gen_goto_tb(ctx, 1, ctx->nip);
3937     } else {
3938         if (NARROW_MODE(ctx)) {
3939             tcg_gen_andi_tl(cpu_nip, target, (uint32_t)~3);
3940         } else {
3941             tcg_gen_andi_tl(cpu_nip, target, ~3);
3942         }
3943         tcg_gen_exit_tb(0);
3944         gen_set_label(l1);
3945         gen_update_nip(ctx, ctx->nip);
3946         tcg_gen_exit_tb(0);
3947     }
3948     if (type == BCOND_LR || type == BCOND_CTR || type == BCOND_TAR) {
3949         tcg_temp_free(target);
3950     }
3951 }
3952
3953 static void gen_bc(DisasContext *ctx)
3954 {
3955     gen_bcond(ctx, BCOND_IM);
3956 }
3957
3958 static void gen_bcctr(DisasContext *ctx)
3959 {
3960     gen_bcond(ctx, BCOND_CTR);
3961 }
3962
3963 static void gen_bclr(DisasContext *ctx)
3964 {
3965     gen_bcond(ctx, BCOND_LR);
3966 }
3967
3968 static void gen_bctar(DisasContext *ctx)
3969 {
3970     gen_bcond(ctx, BCOND_TAR);
3971 }
3972
3973 /***                      Condition register logical                       ***/
3974 #define GEN_CRLOGIC(name, tcg_op, opc)                                        \
3975 static void glue(gen_, name)(DisasContext *ctx)                                       \
3976 {                                                                             \
3977     uint8_t bitmask;                                                          \
3978     int sh;                                                                   \
3979     TCGv_i32 t0, t1;                                                          \
3980     sh = (crbD(ctx->opcode) & 0x03) - (crbA(ctx->opcode) & 0x03);             \
3981     t0 = tcg_temp_new_i32();                                                  \
3982     if (sh > 0)                                                               \
3983         tcg_gen_shri_i32(t0, cpu_crf[crbA(ctx->opcode) >> 2], sh);            \
3984     else if (sh < 0)                                                          \
3985         tcg_gen_shli_i32(t0, cpu_crf[crbA(ctx->opcode) >> 2], -sh);           \
3986     else                                                                      \
3987         tcg_gen_mov_i32(t0, cpu_crf[crbA(ctx->opcode) >> 2]);                 \
3988     t1 = tcg_temp_new_i32();                                                  \
3989     sh = (crbD(ctx->opcode) & 0x03) - (crbB(ctx->opcode) & 0x03);             \
3990     if (sh > 0)                                                               \
3991         tcg_gen_shri_i32(t1, cpu_crf[crbB(ctx->opcode) >> 2], sh);            \
3992     else if (sh < 0)                                                          \
3993         tcg_gen_shli_i32(t1, cpu_crf[crbB(ctx->opcode) >> 2], -sh);           \
3994     else                                                                      \
3995         tcg_gen_mov_i32(t1, cpu_crf[crbB(ctx->opcode) >> 2]);                 \
3996     tcg_op(t0, t0, t1);                                                       \
3997     bitmask = 0x08 >> (crbD(ctx->opcode) & 0x03);                             \
3998     tcg_gen_andi_i32(t0, t0, bitmask);                                        \
3999     tcg_gen_andi_i32(t1, cpu_crf[crbD(ctx->opcode) >> 2], ~bitmask);          \
4000     tcg_gen_or_i32(cpu_crf[crbD(ctx->opcode) >> 2], t0, t1);                  \
4001     tcg_temp_free_i32(t0);                                                    \
4002     tcg_temp_free_i32(t1);                                                    \
4003 }
4004
4005 /* crand */
4006 GEN_CRLOGIC(crand, tcg_gen_and_i32, 0x08);
4007 /* crandc */
4008 GEN_CRLOGIC(crandc, tcg_gen_andc_i32, 0x04);
4009 /* creqv */
4010 GEN_CRLOGIC(creqv, tcg_gen_eqv_i32, 0x09);
4011 /* crnand */
4012 GEN_CRLOGIC(crnand, tcg_gen_nand_i32, 0x07);
4013 /* crnor */
4014 GEN_CRLOGIC(crnor, tcg_gen_nor_i32, 0x01);
4015 /* cror */
4016 GEN_CRLOGIC(cror, tcg_gen_or_i32, 0x0E);
4017 /* crorc */
4018 GEN_CRLOGIC(crorc, tcg_gen_orc_i32, 0x0D);
4019 /* crxor */
4020 GEN_CRLOGIC(crxor, tcg_gen_xor_i32, 0x06);
4021
4022 /* mcrf */
4023 static void gen_mcrf(DisasContext *ctx)
4024 {
4025     tcg_gen_mov_i32(cpu_crf[crfD(ctx->opcode)], cpu_crf[crfS(ctx->opcode)]);
4026 }
4027
4028 /***                           System linkage                              ***/
4029
4030 /* rfi (supervisor only) */
4031 static void gen_rfi(DisasContext *ctx)
4032 {
4033 #if defined(CONFIG_USER_ONLY)
4034     gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
4035 #else
4036     /* Restore CPU state */
4037     if (unlikely(ctx->pr)) {
4038         gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
4039         return;
4040     }
4041     gen_update_cfar(ctx, ctx->nip);
4042     gen_helper_rfi(cpu_env);
4043     gen_sync_exception(ctx);
4044 #endif
4045 }
4046
4047 #if defined(TARGET_PPC64)
4048 static void gen_rfid(DisasContext *ctx)
4049 {
4050 #if defined(CONFIG_USER_ONLY)
4051     gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
4052 #else
4053     /* Restore CPU state */
4054     if (unlikely(ctx->pr)) {
4055         gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
4056         return;
4057     }
4058     gen_update_cfar(ctx, ctx->nip);
4059     gen_helper_rfid(cpu_env);
4060     gen_sync_exception(ctx);
4061 #endif
4062 }
4063
4064 static void gen_hrfid(DisasContext *ctx)
4065 {
4066 #if defined(CONFIG_USER_ONLY)
4067     gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
4068 #else
4069     /* Restore CPU state */
4070     if (unlikely(!ctx->hv)) {
4071         gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
4072         return;
4073     }
4074     gen_helper_hrfid(cpu_env);
4075     gen_sync_exception(ctx);
4076 #endif
4077 }
4078 #endif
4079
4080 /* sc */
4081 #if defined(CONFIG_USER_ONLY)
4082 #define POWERPC_SYSCALL POWERPC_EXCP_SYSCALL_USER
4083 #else
4084 #define POWERPC_SYSCALL POWERPC_EXCP_SYSCALL
4085 #endif
4086 static void gen_sc(DisasContext *ctx)
4087 {
4088     uint32_t lev;
4089
4090     lev = (ctx->opcode >> 5) & 0x7F;
4091     gen_exception_err(ctx, POWERPC_SYSCALL, lev);
4092 }
4093
4094 /***                                Trap                                   ***/
4095
4096 /* tw */
4097 static void gen_tw(DisasContext *ctx)
4098 {
4099     TCGv_i32 t0 = tcg_const_i32(TO(ctx->opcode));
4100     /* Update the nip since this might generate a trap exception */
4101     gen_update_nip(ctx, ctx->nip);
4102     gen_helper_tw(cpu_env, cpu_gpr[rA(ctx->opcode)], cpu_gpr[rB(ctx->opcode)],
4103                   t0);
4104     tcg_temp_free_i32(t0);
4105 }
4106
4107 /* twi */
4108 static void gen_twi(DisasContext *ctx)
4109 {
4110     TCGv t0 = tcg_const_tl(SIMM(ctx->opcode));
4111     TCGv_i32 t1 = tcg_const_i32(TO(ctx->opcode));
4112     /* Update the nip since this might generate a trap exception */
4113     gen_update_nip(ctx, ctx->nip);
4114     gen_helper_tw(cpu_env, cpu_gpr[rA(ctx->opcode)], t0, t1);
4115     tcg_temp_free(t0);
4116     tcg_temp_free_i32(t1);
4117 }
4118
4119 #if defined(TARGET_PPC64)
4120 /* td */
4121 static void gen_td(DisasContext *ctx)
4122 {
4123     TCGv_i32 t0 = tcg_const_i32(TO(ctx->opcode));
4124     /* Update the nip since this might generate a trap exception */
4125     gen_update_nip(ctx, ctx->nip);
4126     gen_helper_td(cpu_env, cpu_gpr[rA(ctx->opcode)], cpu_gpr[rB(ctx->opcode)],
4127                   t0);
4128     tcg_temp_free_i32(t0);
4129 }
4130
4131 /* tdi */
4132 static void gen_tdi(DisasContext *ctx)
4133 {
4134     TCGv t0 = tcg_const_tl(SIMM(ctx->opcode));
4135     TCGv_i32 t1 = tcg_const_i32(TO(ctx->opcode));
4136     /* Update the nip since this might generate a trap exception */
4137     gen_update_nip(ctx, ctx->nip);
4138     gen_helper_td(cpu_env, cpu_gpr[rA(ctx->opcode)], t0, t1);
4139     tcg_temp_free(t0);
4140     tcg_temp_free_i32(t1);
4141 }
4142 #endif
4143
4144 /***                          Processor control                            ***/
4145
4146 static void gen_read_xer(TCGv dst)
4147 {
4148     TCGv t0 = tcg_temp_new();
4149     TCGv t1 = tcg_temp_new();
4150     TCGv t2 = tcg_temp_new();
4151     tcg_gen_mov_tl(dst, cpu_xer);
4152     tcg_gen_shli_tl(t0, cpu_so, XER_SO);
4153     tcg_gen_shli_tl(t1, cpu_ov, XER_OV);
4154     tcg_gen_shli_tl(t2, cpu_ca, XER_CA);
4155     tcg_gen_or_tl(t0, t0, t1);
4156     tcg_gen_or_tl(dst, dst, t2);
4157     tcg_gen_or_tl(dst, dst, t0);
4158     tcg_temp_free(t0);
4159     tcg_temp_free(t1);
4160     tcg_temp_free(t2);
4161 }
4162
4163 static void gen_write_xer(TCGv src)
4164 {
4165     tcg_gen_andi_tl(cpu_xer, src,
4166                     ~((1u << XER_SO) | (1u << XER_OV) | (1u << XER_CA)));
4167     tcg_gen_shri_tl(cpu_so, src, XER_SO);
4168     tcg_gen_shri_tl(cpu_ov, src, XER_OV);
4169     tcg_gen_shri_tl(cpu_ca, src, XER_CA);
4170     tcg_gen_andi_tl(cpu_so, cpu_so, 1);
4171     tcg_gen_andi_tl(cpu_ov, cpu_ov, 1);
4172     tcg_gen_andi_tl(cpu_ca, cpu_ca, 1);
4173 }
4174
4175 /* mcrxr */
4176 static void gen_mcrxr(DisasContext *ctx)
4177 {
4178     TCGv_i32 t0 = tcg_temp_new_i32();
4179     TCGv_i32 t1 = tcg_temp_new_i32();
4180     TCGv_i32 dst = cpu_crf[crfD(ctx->opcode)];
4181
4182     tcg_gen_trunc_tl_i32(t0, cpu_so);
4183     tcg_gen_trunc_tl_i32(t1, cpu_ov);
4184     tcg_gen_trunc_tl_i32(dst, cpu_ca);
4185     tcg_gen_shli_i32(t0, t0, 3);
4186     tcg_gen_shli_i32(t1, t1, 2);
4187     tcg_gen_shli_i32(dst, dst, 1);
4188     tcg_gen_or_i32(dst, dst, t0);
4189     tcg_gen_or_i32(dst, dst, t1);
4190     tcg_temp_free_i32(t0);
4191     tcg_temp_free_i32(t1);
4192
4193     tcg_gen_movi_tl(cpu_so, 0);
4194     tcg_gen_movi_tl(cpu_ov, 0);
4195     tcg_gen_movi_tl(cpu_ca, 0);
4196 }
4197
4198 /* mfcr mfocrf */
4199 static void gen_mfcr(DisasContext *ctx)
4200 {
4201     uint32_t crm, crn;
4202
4203     if (likely(ctx->opcode & 0x00100000)) {
4204         crm = CRM(ctx->opcode);
4205         if (likely(crm && ((crm & (crm - 1)) == 0))) {
4206             crn = ctz32 (crm);
4207             tcg_gen_extu_i32_tl(cpu_gpr[rD(ctx->opcode)], cpu_crf[7 - crn]);
4208             tcg_gen_shli_tl(cpu_gpr[rD(ctx->opcode)],
4209                             cpu_gpr[rD(ctx->opcode)], crn * 4);
4210         }
4211     } else {
4212         TCGv_i32 t0 = tcg_temp_new_i32();
4213         tcg_gen_mov_i32(t0, cpu_crf[0]);
4214         tcg_gen_shli_i32(t0, t0, 4);
4215         tcg_gen_or_i32(t0, t0, cpu_crf[1]);
4216         tcg_gen_shli_i32(t0, t0, 4);
4217         tcg_gen_or_i32(t0, t0, cpu_crf[2]);
4218         tcg_gen_shli_i32(t0, t0, 4);
4219         tcg_gen_or_i32(t0, t0, cpu_crf[3]);
4220         tcg_gen_shli_i32(t0, t0, 4);
4221         tcg_gen_or_i32(t0, t0, cpu_crf[4]);
4222         tcg_gen_shli_i32(t0, t0, 4);
4223         tcg_gen_or_i32(t0, t0, cpu_crf[5]);
4224         tcg_gen_shli_i32(t0, t0, 4);
4225         tcg_gen_or_i32(t0, t0, cpu_crf[6]);
4226         tcg_gen_shli_i32(t0, t0, 4);
4227         tcg_gen_or_i32(t0, t0, cpu_crf[7]);
4228         tcg_gen_extu_i32_tl(cpu_gpr[rD(ctx->opcode)], t0);
4229         tcg_temp_free_i32(t0);
4230     }
4231 }
4232
4233 /* mfmsr */
4234 static void gen_mfmsr(DisasContext *ctx)
4235 {
4236 #if defined(CONFIG_USER_ONLY)
4237     gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
4238 #else
4239     if (unlikely(ctx->pr)) {
4240         gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
4241         return;
4242     }
4243     tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_msr);
4244 #endif
4245 }
4246
4247 static void spr_noaccess(void *opaque, int gprn, int sprn)
4248 {
4249 #if 0
4250     sprn = ((sprn >> 5) & 0x1F) | ((sprn & 0x1F) << 5);
4251     printf("ERROR: try to access SPR %d !\n", sprn);
4252 #endif
4253 }
4254 #define SPR_NOACCESS (&spr_noaccess)
4255
4256 /* mfspr */
4257 static inline void gen_op_mfspr(DisasContext *ctx)
4258 {
4259     void (*read_cb)(void *opaque, int gprn, int sprn);
4260     uint32_t sprn = SPR(ctx->opcode);
4261
4262 #if !defined(CONFIG_USER_ONLY)
4263     if (ctx->hv)
4264         read_cb = ctx->spr_cb[sprn].hea_read;
4265     else if (!ctx->pr)
4266         read_cb = ctx->spr_cb[sprn].oea_read;
4267     else
4268 #endif
4269         read_cb = ctx->spr_cb[sprn].uea_read;
4270     if (likely(read_cb != NULL)) {
4271         if (likely(read_cb != SPR_NOACCESS)) {
4272             (*read_cb)(ctx, rD(ctx->opcode), sprn);
4273         } else {
4274             /* Privilege exception */
4275             /* This is a hack to avoid warnings when running Linux:
4276              * this OS breaks the PowerPC virtualisation model,
4277              * allowing userland application to read the PVR
4278              */
4279             if (sprn != SPR_PVR) {
4280                 qemu_log("Trying to read privileged spr %d (0x%03x) at "
4281                          TARGET_FMT_lx "\n", sprn, sprn, ctx->nip - 4);
4282                 printf("Trying to read privileged spr %d (0x%03x) at "
4283                        TARGET_FMT_lx "\n", sprn, sprn, ctx->nip - 4);
4284             }
4285             gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
4286         }
4287     } else {
4288         /* Not defined */
4289         qemu_log("Trying to read invalid spr %d (0x%03x) at "
4290                  TARGET_FMT_lx "\n", sprn, sprn, ctx->nip - 4);
4291         printf("Trying to read invalid spr %d (0x%03x) at "
4292                TARGET_FMT_lx "\n", sprn, sprn, ctx->nip - 4);
4293         gen_inval_exception(ctx, POWERPC_EXCP_INVAL_SPR);
4294     }
4295 }
4296
4297 static void gen_mfspr(DisasContext *ctx)
4298 {
4299     gen_op_mfspr(ctx);
4300 }
4301
4302 /* mftb */
4303 static void gen_mftb(DisasContext *ctx)
4304 {
4305     gen_op_mfspr(ctx);
4306 }
4307
4308 /* mtcrf mtocrf*/
4309 static void gen_mtcrf(DisasContext *ctx)
4310 {
4311     uint32_t crm, crn;
4312
4313     crm = CRM(ctx->opcode);
4314     if (likely((ctx->opcode & 0x00100000))) {
4315         if (crm && ((crm & (crm - 1)) == 0)) {
4316             TCGv_i32 temp = tcg_temp_new_i32();
4317             crn = ctz32 (crm);
4318             tcg_gen_trunc_tl_i32(temp, cpu_gpr[rS(ctx->opcode)]);
4319             tcg_gen_shri_i32(temp, temp, crn * 4);
4320             tcg_gen_andi_i32(cpu_crf[7 - crn], temp, 0xf);
4321             tcg_temp_free_i32(temp);
4322         }
4323     } else {
4324         TCGv_i32 temp = tcg_temp_new_i32();
4325         tcg_gen_trunc_tl_i32(temp, cpu_gpr[rS(ctx->opcode)]);
4326         for (crn = 0 ; crn < 8 ; crn++) {
4327             if (crm & (1 << crn)) {
4328                     tcg_gen_shri_i32(cpu_crf[7 - crn], temp, crn * 4);
4329                     tcg_gen_andi_i32(cpu_crf[7 - crn], cpu_crf[7 - crn], 0xf);
4330             }
4331         }
4332         tcg_temp_free_i32(temp);
4333     }
4334 }
4335
4336 /* mtmsr */
4337 #if defined(TARGET_PPC64)
4338 static void gen_mtmsrd(DisasContext *ctx)
4339 {
4340 #if defined(CONFIG_USER_ONLY)
4341     gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
4342 #else
4343     if (unlikely(ctx->pr)) {
4344         gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
4345         return;
4346     }
4347     if (ctx->opcode & 0x00010000) {
4348         /* Special form that does not need any synchronisation */
4349         TCGv t0 = tcg_temp_new();
4350         tcg_gen_andi_tl(t0, cpu_gpr[rS(ctx->opcode)], (1 << MSR_RI) | (1 << MSR_EE));
4351         tcg_gen_andi_tl(cpu_msr, cpu_msr, ~((1 << MSR_RI) | (1 << MSR_EE)));
4352         tcg_gen_or_tl(cpu_msr, cpu_msr, t0);
4353         tcg_temp_free(t0);
4354     } else {
4355         /* XXX: we need to update nip before the store
4356          *      if we enter power saving mode, we will exit the loop
4357          *      directly from ppc_store_msr
4358          */
4359         gen_update_nip(ctx, ctx->nip);
4360         gen_helper_store_msr(cpu_env, cpu_gpr[rS(ctx->opcode)]);
4361         /* Must stop the translation as machine state (may have) changed */
4362         /* Note that mtmsr is not always defined as context-synchronizing */
4363         gen_stop_exception(ctx);
4364     }
4365 #endif
4366 }
4367 #endif
4368
4369 static void gen_mtmsr(DisasContext *ctx)
4370 {
4371 #if defined(CONFIG_USER_ONLY)
4372     gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
4373 #else
4374     if (unlikely(ctx->pr)) {
4375         gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
4376         return;
4377     }
4378     if (ctx->opcode & 0x00010000) {
4379         /* Special form that does not need any synchronisation */
4380         TCGv t0 = tcg_temp_new();
4381         tcg_gen_andi_tl(t0, cpu_gpr[rS(ctx->opcode)], (1 << MSR_RI) | (1 << MSR_EE));
4382         tcg_gen_andi_tl(cpu_msr, cpu_msr, ~((1 << MSR_RI) | (1 << MSR_EE)));
4383         tcg_gen_or_tl(cpu_msr, cpu_msr, t0);
4384         tcg_temp_free(t0);
4385     } else {
4386         TCGv msr = tcg_temp_new();
4387
4388         /* XXX: we need to update nip before the store
4389          *      if we enter power saving mode, we will exit the loop
4390          *      directly from ppc_store_msr
4391          */
4392         gen_update_nip(ctx, ctx->nip);
4393 #if defined(TARGET_PPC64)
4394         tcg_gen_deposit_tl(msr, cpu_msr, cpu_gpr[rS(ctx->opcode)], 0, 32);
4395 #else
4396         tcg_gen_mov_tl(msr, cpu_gpr[rS(ctx->opcode)]);
4397 #endif
4398         gen_helper_store_msr(cpu_env, msr);
4399         tcg_temp_free(msr);
4400         /* Must stop the translation as machine state (may have) changed */
4401         /* Note that mtmsr is not always defined as context-synchronizing */
4402         gen_stop_exception(ctx);
4403     }
4404 #endif
4405 }
4406
4407 /* mtspr */
4408 static void gen_mtspr(DisasContext *ctx)
4409 {
4410     void (*write_cb)(void *opaque, int sprn, int gprn);
4411     uint32_t sprn = SPR(ctx->opcode);
4412
4413 #if !defined(CONFIG_USER_ONLY)
4414     if (ctx->hv)
4415         write_cb = ctx->spr_cb[sprn].hea_write;
4416     else if (!ctx->pr)
4417         write_cb = ctx->spr_cb[sprn].oea_write;
4418     else
4419 #endif
4420         write_cb = ctx->spr_cb[sprn].uea_write;
4421     if (likely(write_cb != NULL)) {
4422         if (likely(write_cb != SPR_NOACCESS)) {
4423             (*write_cb)(ctx, sprn, rS(ctx->opcode));
4424         } else {
4425             /* Privilege exception */
4426             qemu_log("Trying to write privileged spr %d (0x%03x) at "
4427                      TARGET_FMT_lx "\n", sprn, sprn, ctx->nip - 4);
4428             printf("Trying to write privileged spr %d (0x%03x) at "
4429                    TARGET_FMT_lx "\n", sprn, sprn, ctx->nip - 4);
4430             gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
4431         }
4432     } else {
4433         /* Not defined */
4434         qemu_log("Trying to write invalid spr %d (0x%03x) at "
4435                  TARGET_FMT_lx "\n", sprn, sprn, ctx->nip - 4);
4436         printf("Trying to write invalid spr %d (0x%03x) at "
4437                TARGET_FMT_lx "\n", sprn, sprn, ctx->nip - 4);
4438         gen_inval_exception(ctx, POWERPC_EXCP_INVAL_SPR);
4439     }
4440 }
4441
4442 /***                         Cache management                              ***/
4443
4444 /* dcbf */
4445 static void gen_dcbf(DisasContext *ctx)
4446 {
4447     /* XXX: specification says this is treated as a load by the MMU */
4448     TCGv t0;
4449     gen_set_access_type(ctx, ACCESS_CACHE);
4450     t0 = tcg_temp_new();
4451     gen_addr_reg_index(ctx, t0);
4452     gen_qemu_ld8u(ctx, t0, t0);
4453     tcg_temp_free(t0);
4454 }
4455
4456 /* dcbi (Supervisor only) */
4457 static void gen_dcbi(DisasContext *ctx)
4458 {
4459 #if defined(CONFIG_USER_ONLY)
4460     gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
4461 #else
4462     TCGv EA, val;
4463     if (unlikely(ctx->pr)) {
4464         gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
4465         return;
4466     }
4467     EA = tcg_temp_new();
4468     gen_set_access_type(ctx, ACCESS_CACHE);
4469     gen_addr_reg_index(ctx, EA);
4470     val = tcg_temp_new();
4471     /* XXX: specification says this should be treated as a store by the MMU */
4472     gen_qemu_ld8u(ctx, val, EA);
4473     gen_qemu_st8(ctx, val, EA);
4474     tcg_temp_free(val);
4475     tcg_temp_free(EA);
4476 #endif
4477 }
4478
4479 /* dcdst */
4480 static void gen_dcbst(DisasContext *ctx)
4481 {
4482     /* XXX: specification say this is treated as a load by the MMU */
4483     TCGv t0;
4484     gen_set_access_type(ctx, ACCESS_CACHE);
4485     t0 = tcg_temp_new();
4486     gen_addr_reg_index(ctx, t0);
4487     gen_qemu_ld8u(ctx, t0, t0);
4488     tcg_temp_free(t0);
4489 }
4490
4491 /* dcbt */
4492 static void gen_dcbt(DisasContext *ctx)
4493 {
4494     /* interpreted as no-op */
4495     /* XXX: specification say this is treated as a load by the MMU
4496      *      but does not generate any exception
4497      */
4498 }
4499
4500 /* dcbtst */
4501 static void gen_dcbtst(DisasContext *ctx)
4502 {
4503     /* interpreted as no-op */
4504     /* XXX: specification say this is treated as a load by the MMU
4505      *      but does not generate any exception
4506      */
4507 }
4508
4509 /* dcbtls */
4510 static void gen_dcbtls(DisasContext *ctx)
4511 {
4512     /* Always fails locking the cache */
4513     TCGv t0 = tcg_temp_new();
4514     gen_load_spr(t0, SPR_Exxx_L1CSR0);
4515     tcg_gen_ori_tl(t0, t0, L1CSR0_CUL);
4516     gen_store_spr(SPR_Exxx_L1CSR0, t0);
4517     tcg_temp_free(t0);
4518 }
4519
4520 /* dcbz */
4521 static void gen_dcbz(DisasContext *ctx)
4522 {
4523     TCGv tcgv_addr;
4524     TCGv_i32 tcgv_is_dcbzl;
4525     int is_dcbzl = ctx->opcode & 0x00200000 ? 1 : 0;
4526
4527     gen_set_access_type(ctx, ACCESS_CACHE);
4528     /* NIP cannot be restored if the memory exception comes from an helper */
4529     gen_update_nip(ctx, ctx->nip - 4);
4530     tcgv_addr = tcg_temp_new();
4531     tcgv_is_dcbzl = tcg_const_i32(is_dcbzl);
4532
4533     gen_addr_reg_index(ctx, tcgv_addr);
4534     gen_helper_dcbz(cpu_env, tcgv_addr, tcgv_is_dcbzl);
4535
4536     tcg_temp_free(tcgv_addr);
4537     tcg_temp_free_i32(tcgv_is_dcbzl);
4538 }
4539
4540 /* dst / dstt */
4541 static void gen_dst(DisasContext *ctx)
4542 {
4543     if (rA(ctx->opcode) == 0) {
4544         gen_inval_exception(ctx, POWERPC_EXCP_INVAL_LSWX);
4545     } else {
4546         /* interpreted as no-op */
4547     }
4548 }
4549
4550 /* dstst /dststt */
4551 static void gen_dstst(DisasContext *ctx)
4552 {
4553     if (rA(ctx->opcode) == 0) {
4554         gen_inval_exception(ctx, POWERPC_EXCP_INVAL_LSWX);
4555     } else {
4556         /* interpreted as no-op */
4557     }
4558
4559 }
4560
4561 /* dss / dssall */
4562 static void gen_dss(DisasContext *ctx)
4563 {
4564     /* interpreted as no-op */
4565 }
4566
4567 /* icbi */
4568 static void gen_icbi(DisasContext *ctx)
4569 {
4570     TCGv t0;
4571     gen_set_access_type(ctx, ACCESS_CACHE);
4572     /* NIP cannot be restored if the memory exception comes from an helper */
4573     gen_update_nip(ctx, ctx->nip - 4);
4574     t0 = tcg_temp_new();
4575     gen_addr_reg_index(ctx, t0);
4576     gen_helper_icbi(cpu_env, t0);
4577     tcg_temp_free(t0);
4578 }
4579
4580 /* Optional: */
4581 /* dcba */
4582 static void gen_dcba(DisasContext *ctx)
4583 {
4584     /* interpreted as no-op */
4585     /* XXX: specification say this is treated as a store by the MMU
4586      *      but does not generate any exception
4587      */
4588 }
4589
4590 /***                    Segment register manipulation                      ***/
4591 /* Supervisor only: */
4592
4593 /* mfsr */
4594 static void gen_mfsr(DisasContext *ctx)
4595 {
4596 #if defined(CONFIG_USER_ONLY)
4597     gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
4598 #else
4599     TCGv t0;
4600     if (unlikely(ctx->pr)) {
4601         gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
4602         return;
4603     }
4604     t0 = tcg_const_tl(SR(ctx->opcode));
4605     gen_helper_load_sr(cpu_gpr[rD(ctx->opcode)], cpu_env, t0);
4606     tcg_temp_free(t0);
4607 #endif
4608 }
4609
4610 /* mfsrin */
4611 static void gen_mfsrin(DisasContext *ctx)
4612 {
4613 #if defined(CONFIG_USER_ONLY)
4614     gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
4615 #else
4616     TCGv t0;
4617     if (unlikely(ctx->pr)) {
4618         gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
4619         return;
4620     }
4621     t0 = tcg_temp_new();
4622     tcg_gen_shri_tl(t0, cpu_gpr[rB(ctx->opcode)], 28);
4623     tcg_gen_andi_tl(t0, t0, 0xF);
4624     gen_helper_load_sr(cpu_gpr[rD(ctx->opcode)], cpu_env, t0);
4625     tcg_temp_free(t0);
4626 #endif
4627 }
4628
4629 /* mtsr */
4630 static void gen_mtsr(DisasContext *ctx)
4631 {
4632 #if defined(CONFIG_USER_ONLY)
4633     gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
4634 #else
4635     TCGv t0;
4636     if (unlikely(ctx->pr)) {
4637         gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
4638         return;
4639     }
4640     t0 = tcg_const_tl(SR(ctx->opcode));
4641     gen_helper_store_sr(cpu_env, t0, cpu_gpr[rS(ctx->opcode)]);
4642     tcg_temp_free(t0);
4643 #endif
4644 }
4645
4646 /* mtsrin */
4647 static void gen_mtsrin(DisasContext *ctx)
4648 {
4649 #if defined(CONFIG_USER_ONLY)
4650     gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
4651 #else
4652     TCGv t0;
4653     if (unlikely(ctx->pr)) {
4654         gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
4655         return;
4656     }
4657     t0 = tcg_temp_new();
4658     tcg_gen_shri_tl(t0, cpu_gpr[rB(ctx->opcode)], 28);
4659     tcg_gen_andi_tl(t0, t0, 0xF);
4660     gen_helper_store_sr(cpu_env, t0, cpu_gpr[rD(ctx->opcode)]);
4661     tcg_temp_free(t0);
4662 #endif
4663 }
4664
4665 #if defined(TARGET_PPC64)
4666 /* Specific implementation for PowerPC 64 "bridge" emulation using SLB */
4667
4668 /* mfsr */
4669 static void gen_mfsr_64b(DisasContext *ctx)
4670 {
4671 #if defined(CONFIG_USER_ONLY)
4672     gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
4673 #else
4674     TCGv t0;
4675     if (unlikely(ctx->pr)) {
4676         gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
4677         return;
4678     }
4679     t0 = tcg_const_tl(SR(ctx->opcode));
4680     gen_helper_load_sr(cpu_gpr[rD(ctx->opcode)], cpu_env, t0);
4681     tcg_temp_free(t0);
4682 #endif
4683 }
4684
4685 /* mfsrin */
4686 static void gen_mfsrin_64b(DisasContext *ctx)
4687 {
4688 #if defined(CONFIG_USER_ONLY)
4689     gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
4690 #else
4691     TCGv t0;
4692     if (unlikely(ctx->pr)) {
4693         gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
4694         return;
4695     }
4696     t0 = tcg_temp_new();
4697     tcg_gen_shri_tl(t0, cpu_gpr[rB(ctx->opcode)], 28);
4698     tcg_gen_andi_tl(t0, t0, 0xF);
4699     gen_helper_load_sr(cpu_gpr[rD(ctx->opcode)], cpu_env, t0);
4700     tcg_temp_free(t0);
4701 #endif
4702 }
4703
4704 /* mtsr */
4705 static void gen_mtsr_64b(DisasContext *ctx)
4706 {
4707 #if defined(CONFIG_USER_ONLY)
4708     gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
4709 #else
4710     TCGv t0;
4711     if (unlikely(ctx->pr)) {
4712         gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
4713         return;
4714     }
4715     t0 = tcg_const_tl(SR(ctx->opcode));
4716     gen_helper_store_sr(cpu_env, t0, cpu_gpr[rS(ctx->opcode)]);
4717     tcg_temp_free(t0);
4718 #endif
4719 }
4720
4721 /* mtsrin */
4722 static void gen_mtsrin_64b(DisasContext *ctx)
4723 {
4724 #if defined(CONFIG_USER_ONLY)
4725     gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
4726 #else
4727     TCGv t0;
4728     if (unlikely(ctx->pr)) {
4729         gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
4730         return;
4731     }
4732     t0 = tcg_temp_new();
4733     tcg_gen_shri_tl(t0, cpu_gpr[rB(ctx->opcode)], 28);
4734     tcg_gen_andi_tl(t0, t0, 0xF);
4735     gen_helper_store_sr(cpu_env, t0, cpu_gpr[rS(ctx->opcode)]);
4736     tcg_temp_free(t0);
4737 #endif
4738 }
4739
4740 /* slbmte */
4741 static void gen_slbmte(DisasContext *ctx)
4742 {
4743 #if defined(CONFIG_USER_ONLY)
4744     gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
4745 #else
4746     if (unlikely(ctx->pr)) {
4747         gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
4748         return;
4749     }
4750     gen_helper_store_slb(cpu_env, cpu_gpr[rB(ctx->opcode)],
4751                          cpu_gpr[rS(ctx->opcode)]);
4752 #endif
4753 }
4754
4755 static void gen_slbmfee(DisasContext *ctx)
4756 {
4757 #if defined(CONFIG_USER_ONLY)
4758     gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
4759 #else
4760     if (unlikely(ctx->pr)) {
4761         gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
4762         return;
4763     }
4764     gen_helper_load_slb_esid(cpu_gpr[rS(ctx->opcode)], cpu_env,
4765                              cpu_gpr[rB(ctx->opcode)]);
4766 #endif
4767 }
4768
4769 static void gen_slbmfev(DisasContext *ctx)
4770 {
4771 #if defined(CONFIG_USER_ONLY)
4772     gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
4773 #else
4774     if (unlikely(ctx->pr)) {
4775         gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
4776         return;
4777     }
4778     gen_helper_load_slb_vsid(cpu_gpr[rS(ctx->opcode)], cpu_env,
4779                              cpu_gpr[rB(ctx->opcode)]);
4780 #endif
4781 }
4782 #endif /* defined(TARGET_PPC64) */
4783
4784 /***                      Lookaside buffer management                      ***/
4785 /* Optional & supervisor only: */
4786
4787 /* tlbia */
4788 static void gen_tlbia(DisasContext *ctx)
4789 {
4790 #if defined(CONFIG_USER_ONLY)
4791     gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
4792 #else
4793     if (unlikely(ctx->pr)) {
4794         gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
4795         return;
4796     }
4797     gen_helper_tlbia(cpu_env);
4798 #endif
4799 }
4800
4801 /* tlbiel */
4802 static void gen_tlbiel(DisasContext *ctx)
4803 {
4804 #if defined(CONFIG_USER_ONLY)
4805     gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
4806 #else
4807     if (unlikely(ctx->pr)) {
4808         gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
4809         return;
4810     }
4811     gen_helper_tlbie(cpu_env, cpu_gpr[rB(ctx->opcode)]);
4812 #endif
4813 }
4814
4815 /* tlbie */
4816 static void gen_tlbie(DisasContext *ctx)
4817 {
4818 #if defined(CONFIG_USER_ONLY)
4819     gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
4820 #else
4821     if (unlikely(ctx->pr)) {
4822         gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
4823         return;
4824     }
4825     if (NARROW_MODE(ctx)) {
4826         TCGv t0 = tcg_temp_new();
4827         tcg_gen_ext32u_tl(t0, cpu_gpr[rB(ctx->opcode)]);
4828         gen_helper_tlbie(cpu_env, t0);
4829         tcg_temp_free(t0);
4830     } else {
4831         gen_helper_tlbie(cpu_env, cpu_gpr[rB(ctx->opcode)]);
4832     }
4833 #endif
4834 }
4835
4836 /* tlbsync */
4837 static void gen_tlbsync(DisasContext *ctx)
4838 {
4839 #if defined(CONFIG_USER_ONLY)
4840     gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
4841 #else
4842     if (unlikely(ctx->pr)) {
4843         gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
4844         return;
4845     }
4846     /* This has no effect: it should ensure that all previous
4847      * tlbie have completed
4848      */
4849     gen_stop_exception(ctx);
4850 #endif
4851 }
4852
4853 #if defined(TARGET_PPC64)
4854 /* slbia */
4855 static void gen_slbia(DisasContext *ctx)
4856 {
4857 #if defined(CONFIG_USER_ONLY)
4858     gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
4859 #else
4860     if (unlikely(ctx->pr)) {
4861         gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
4862         return;
4863     }
4864     gen_helper_slbia(cpu_env);
4865 #endif
4866 }
4867
4868 /* slbie */
4869 static void gen_slbie(DisasContext *ctx)
4870 {
4871 #if defined(CONFIG_USER_ONLY)
4872     gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
4873 #else
4874     if (unlikely(ctx->pr)) {
4875         gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
4876         return;
4877     }
4878     gen_helper_slbie(cpu_env, cpu_gpr[rB(ctx->opcode)]);
4879 #endif
4880 }
4881 #endif
4882
4883 /***                              External control                         ***/
4884 /* Optional: */
4885
4886 /* eciwx */
4887 static void gen_eciwx(DisasContext *ctx)
4888 {
4889     TCGv t0;
4890     /* Should check EAR[E] ! */
4891     gen_set_access_type(ctx, ACCESS_EXT);
4892     t0 = tcg_temp_new();
4893     gen_addr_reg_index(ctx, t0);
4894     gen_check_align(ctx, t0, 0x03);
4895     gen_qemu_ld32u(ctx, cpu_gpr[rD(ctx->opcode)], t0);
4896     tcg_temp_free(t0);
4897 }
4898
4899 /* ecowx */
4900 static void gen_ecowx(DisasContext *ctx)
4901 {
4902     TCGv t0;
4903     /* Should check EAR[E] ! */
4904     gen_set_access_type(ctx, ACCESS_EXT);
4905     t0 = tcg_temp_new();
4906     gen_addr_reg_index(ctx, t0);
4907     gen_check_align(ctx, t0, 0x03);
4908     gen_qemu_st32(ctx, cpu_gpr[rD(ctx->opcode)], t0);
4909     tcg_temp_free(t0);
4910 }
4911
4912 /* PowerPC 601 specific instructions */
4913
4914 /* abs - abs. */
4915 static void gen_abs(DisasContext *ctx)
4916 {
4917     int l1 = gen_new_label();
4918     int l2 = gen_new_label();
4919     tcg_gen_brcondi_tl(TCG_COND_GE, cpu_gpr[rA(ctx->opcode)], 0, l1);
4920     tcg_gen_neg_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)]);
4921     tcg_gen_br(l2);
4922     gen_set_label(l1);
4923     tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)]);
4924     gen_set_label(l2);
4925     if (unlikely(Rc(ctx->opcode) != 0))
4926         gen_set_Rc0(ctx, cpu_gpr[rD(ctx->opcode)]);
4927 }
4928
4929 /* abso - abso. */
4930 static void gen_abso(DisasContext *ctx)
4931 {
4932     int l1 = gen_new_label();
4933     int l2 = gen_new_label();
4934     int l3 = gen_new_label();
4935     /* Start with XER OV disabled, the most likely case */
4936     tcg_gen_movi_tl(cpu_ov, 0);
4937     tcg_gen_brcondi_tl(TCG_COND_GE, cpu_gpr[rA(ctx->opcode)], 0, l2);
4938     tcg_gen_brcondi_tl(TCG_COND_NE, cpu_gpr[rA(ctx->opcode)], 0x80000000, l1);
4939     tcg_gen_movi_tl(cpu_ov, 1);
4940     tcg_gen_movi_tl(cpu_so, 1);
4941     tcg_gen_br(l2);
4942     gen_set_label(l1);
4943     tcg_gen_neg_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)]);
4944     tcg_gen_br(l3);
4945     gen_set_label(l2);
4946     tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)]);
4947     gen_set_label(l3);
4948     if (unlikely(Rc(ctx->opcode) != 0))
4949         gen_set_Rc0(ctx, cpu_gpr[rD(ctx->opcode)]);
4950 }
4951
4952 /* clcs */
4953 static void gen_clcs(DisasContext *ctx)
4954 {
4955     TCGv_i32 t0 = tcg_const_i32(rA(ctx->opcode));
4956     gen_helper_clcs(cpu_gpr[rD(ctx->opcode)], cpu_env, t0);
4957     tcg_temp_free_i32(t0);
4958     /* Rc=1 sets CR0 to an undefined state */
4959 }
4960
4961 /* div - div. */
4962 static void gen_div(DisasContext *ctx)
4963 {
4964     gen_helper_div(cpu_gpr[rD(ctx->opcode)], cpu_env, cpu_gpr[rA(ctx->opcode)],
4965                    cpu_gpr[rB(ctx->opcode)]);
4966     if (unlikely(Rc(ctx->opcode) != 0))
4967         gen_set_Rc0(ctx, cpu_gpr[rD(ctx->opcode)]);
4968 }
4969
4970 /* divo - divo. */
4971 static void gen_divo(DisasContext *ctx)
4972 {
4973     gen_helper_divo(cpu_gpr[rD(ctx->opcode)], cpu_env, cpu_gpr[rA(ctx->opcode)],
4974                     cpu_gpr[rB(ctx->opcode)]);
4975     if (unlikely(Rc(ctx->opcode) != 0))
4976         gen_set_Rc0(ctx, cpu_gpr[rD(ctx->opcode)]);
4977 }
4978
4979 /* divs - divs. */
4980 static void gen_divs(DisasContext *ctx)
4981 {
4982     gen_helper_divs(cpu_gpr[rD(ctx->opcode)], cpu_env, cpu_gpr[rA(ctx->opcode)],
4983                     cpu_gpr[rB(ctx->opcode)]);
4984     if (unlikely(Rc(ctx->opcode) != 0))
4985         gen_set_Rc0(ctx, cpu_gpr[rD(ctx->opcode)]);
4986 }
4987
4988 /* divso - divso. */
4989 static void gen_divso(DisasContext *ctx)
4990 {
4991     gen_helper_divso(cpu_gpr[rD(ctx->opcode)], cpu_env,
4992                      cpu_gpr[rA(ctx->opcode)], cpu_gpr[rB(ctx->opcode)]);
4993     if (unlikely(Rc(ctx->opcode) != 0))
4994         gen_set_Rc0(ctx, cpu_gpr[rD(ctx->opcode)]);
4995 }
4996
4997 /* doz - doz. */
4998 static void gen_doz(DisasContext *ctx)
4999 {
5000     int l1 = gen_new_label();
5001     int l2 = gen_new_label();
5002     tcg_gen_brcond_tl(TCG_COND_GE, cpu_gpr[rB(ctx->opcode)], cpu_gpr[rA(ctx->opcode)], l1);
5003     tcg_gen_sub_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rB(ctx->opcode)], cpu_gpr[rA(ctx->opcode)]);
5004     tcg_gen_br(l2);
5005     gen_set_label(l1);
5006     tcg_gen_movi_tl(cpu_gpr[rD(ctx->opcode)], 0);
5007     gen_set_label(l2);
5008     if (unlikely(Rc(ctx->opcode) != 0))
5009         gen_set_Rc0(ctx, cpu_gpr[rD(ctx->opcode)]);
5010 }
5011
5012 /* dozo - dozo. */
5013 static void gen_dozo(DisasContext *ctx)
5014 {
5015     int l1 = gen_new_label();
5016     int l2 = gen_new_label();
5017     TCGv t0 = tcg_temp_new();
5018     TCGv t1 = tcg_temp_new();
5019     TCGv t2 = tcg_temp_new();
5020     /* Start with XER OV disabled, the most likely case */
5021     tcg_gen_movi_tl(cpu_ov, 0);
5022     tcg_gen_brcond_tl(TCG_COND_GE, cpu_gpr[rB(ctx->opcode)], cpu_gpr[rA(ctx->opcode)], l1);
5023     tcg_gen_sub_tl(t0, cpu_gpr[rB(ctx->opcode)], cpu_gpr[rA(ctx->opcode)]);
5024     tcg_gen_xor_tl(t1, cpu_gpr[rB(ctx->opcode)], cpu_gpr[rA(ctx->opcode)]);
5025     tcg_gen_xor_tl(t2, cpu_gpr[rA(ctx->opcode)], t0);
5026     tcg_gen_andc_tl(t1, t1, t2);
5027     tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], t0);
5028     tcg_gen_brcondi_tl(TCG_COND_GE, t1, 0, l2);
5029     tcg_gen_movi_tl(cpu_ov, 1);
5030     tcg_gen_movi_tl(cpu_so, 1);
5031     tcg_gen_br(l2);
5032     gen_set_label(l1);
5033     tcg_gen_movi_tl(cpu_gpr[rD(ctx->opcode)], 0);
5034     gen_set_label(l2);
5035     tcg_temp_free(t0);
5036     tcg_temp_free(t1);
5037     tcg_temp_free(t2);
5038     if (unlikely(Rc(ctx->opcode) != 0))
5039         gen_set_Rc0(ctx, cpu_gpr[rD(ctx->opcode)]);
5040 }
5041
5042 /* dozi */
5043 static void gen_dozi(DisasContext *ctx)
5044 {
5045     target_long simm = SIMM(ctx->opcode);
5046     int l1 = gen_new_label();
5047     int l2 = gen_new_label();
5048     tcg_gen_brcondi_tl(TCG_COND_LT, cpu_gpr[rA(ctx->opcode)], simm, l1);
5049     tcg_gen_subfi_tl(cpu_gpr[rD(ctx->opcode)], simm, cpu_gpr[rA(ctx->opcode)]);
5050     tcg_gen_br(l2);
5051     gen_set_label(l1);
5052     tcg_gen_movi_tl(cpu_gpr[rD(ctx->opcode)], 0);
5053     gen_set_label(l2);
5054     if (unlikely(Rc(ctx->opcode) != 0))
5055         gen_set_Rc0(ctx, cpu_gpr[rD(ctx->opcode)]);
5056 }
5057
5058 /* lscbx - lscbx. */
5059 static void gen_lscbx(DisasContext *ctx)
5060 {
5061     TCGv t0 = tcg_temp_new();
5062     TCGv_i32 t1 = tcg_const_i32(rD(ctx->opcode));
5063     TCGv_i32 t2 = tcg_const_i32(rA(ctx->opcode));
5064     TCGv_i32 t3 = tcg_const_i32(rB(ctx->opcode));
5065
5066     gen_addr_reg_index(ctx, t0);
5067     /* NIP cannot be restored if the memory exception comes from an helper */
5068     gen_update_nip(ctx, ctx->nip - 4);
5069     gen_helper_lscbx(t0, cpu_env, t0, t1, t2, t3);
5070     tcg_temp_free_i32(t1);
5071     tcg_temp_free_i32(t2);
5072     tcg_temp_free_i32(t3);
5073     tcg_gen_andi_tl(cpu_xer, cpu_xer, ~0x7F);
5074     tcg_gen_or_tl(cpu_xer, cpu_xer, t0);
5075     if (unlikely(Rc(ctx->opcode) != 0))
5076         gen_set_Rc0(ctx, t0);
5077     tcg_temp_free(t0);
5078 }
5079
5080 /* maskg - maskg. */
5081 static void gen_maskg(DisasContext *ctx)
5082 {
5083     int l1 = gen_new_label();
5084     TCGv t0 = tcg_temp_new();
5085     TCGv t1 = tcg_temp_new();
5086     TCGv t2 = tcg_temp_new();
5087     TCGv t3 = tcg_temp_new();
5088     tcg_gen_movi_tl(t3, 0xFFFFFFFF);
5089     tcg_gen_andi_tl(t0, cpu_gpr[rB(ctx->opcode)], 0x1F);
5090     tcg_gen_andi_tl(t1, cpu_gpr[rS(ctx->opcode)], 0x1F);
5091     tcg_gen_addi_tl(t2, t0, 1);
5092     tcg_gen_shr_tl(t2, t3, t2);
5093     tcg_gen_shr_tl(t3, t3, t1);
5094     tcg_gen_xor_tl(cpu_gpr[rA(ctx->opcode)], t2, t3);
5095     tcg_gen_brcond_tl(TCG_COND_GE, t0, t1, l1);
5096     tcg_gen_neg_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rA(ctx->opcode)]);
5097     gen_set_label(l1);
5098     tcg_temp_free(t0);
5099     tcg_temp_free(t1);
5100     tcg_temp_free(t2);
5101     tcg_temp_free(t3);
5102     if (unlikely(Rc(ctx->opcode) != 0))
5103         gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
5104 }
5105
5106 /* maskir - maskir. */
5107 static void gen_maskir(DisasContext *ctx)
5108 {
5109     TCGv t0 = tcg_temp_new();
5110     TCGv t1 = tcg_temp_new();
5111     tcg_gen_and_tl(t0, cpu_gpr[rS(ctx->opcode)], cpu_gpr[rB(ctx->opcode)]);
5112     tcg_gen_andc_tl(t1, cpu_gpr[rA(ctx->opcode)], cpu_gpr[rB(ctx->opcode)]);
5113     tcg_gen_or_tl(cpu_gpr[rA(ctx->opcode)], t0, t1);
5114     tcg_temp_free(t0);
5115     tcg_temp_free(t1);
5116     if (unlikely(Rc(ctx->opcode) != 0))
5117         gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
5118 }
5119
5120 /* mul - mul. */
5121 static void gen_mul(DisasContext *ctx)
5122 {
5123     TCGv_i64 t0 = tcg_temp_new_i64();
5124     TCGv_i64 t1 = tcg_temp_new_i64();
5125     TCGv t2 = tcg_temp_new();
5126     tcg_gen_extu_tl_i64(t0, cpu_gpr[rA(ctx->opcode)]);
5127     tcg_gen_extu_tl_i64(t1, cpu_gpr[rB(ctx->opcode)]);
5128     tcg_gen_mul_i64(t0, t0, t1);
5129     tcg_gen_trunc_i64_tl(t2, t0);
5130     gen_store_spr(SPR_MQ, t2);
5131     tcg_gen_shri_i64(t1, t0, 32);
5132     tcg_gen_trunc_i64_tl(cpu_gpr[rD(ctx->opcode)], t1);
5133     tcg_temp_free_i64(t0);
5134     tcg_temp_free_i64(t1);
5135     tcg_temp_free(t2);
5136     if (unlikely(Rc(ctx->opcode) != 0))
5137         gen_set_Rc0(ctx, cpu_gpr[rD(ctx->opcode)]);
5138 }
5139
5140 /* mulo - mulo. */
5141 static void gen_mulo(DisasContext *ctx)
5142 {
5143     int l1 = gen_new_label();
5144     TCGv_i64 t0 = tcg_temp_new_i64();
5145     TCGv_i64 t1 = tcg_temp_new_i64();
5146     TCGv t2 = tcg_temp_new();
5147     /* Start with XER OV disabled, the most likely case */
5148     tcg_gen_movi_tl(cpu_ov, 0);
5149     tcg_gen_extu_tl_i64(t0, cpu_gpr[rA(ctx->opcode)]);
5150     tcg_gen_extu_tl_i64(t1, cpu_gpr[rB(ctx->opcode)]);
5151     tcg_gen_mul_i64(t0, t0, t1);
5152     tcg_gen_trunc_i64_tl(t2, t0);
5153     gen_store_spr(SPR_MQ, t2);
5154     tcg_gen_shri_i64(t1, t0, 32);
5155     tcg_gen_trunc_i64_tl(cpu_gpr[rD(ctx->opcode)], t1);
5156     tcg_gen_ext32s_i64(t1, t0);
5157     tcg_gen_brcond_i64(TCG_COND_EQ, t0, t1, l1);
5158     tcg_gen_movi_tl(cpu_ov, 1);
5159     tcg_gen_movi_tl(cpu_so, 1);
5160     gen_set_label(l1);
5161     tcg_temp_free_i64(t0);
5162     tcg_temp_free_i64(t1);
5163     tcg_temp_free(t2);
5164     if (unlikely(Rc(ctx->opcode) != 0))
5165         gen_set_Rc0(ctx, cpu_gpr[rD(ctx->opcode)]);
5166 }
5167
5168 /* nabs - nabs. */
5169 static void gen_nabs(DisasContext *ctx)
5170 {
5171     int l1 = gen_new_label();
5172     int l2 = gen_new_label();
5173     tcg_gen_brcondi_tl(TCG_COND_GT, cpu_gpr[rA(ctx->opcode)], 0, l1);
5174     tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)]);
5175     tcg_gen_br(l2);
5176     gen_set_label(l1);
5177     tcg_gen_neg_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)]);
5178     gen_set_label(l2);
5179     if (unlikely(Rc(ctx->opcode) != 0))
5180         gen_set_Rc0(ctx, cpu_gpr[rD(ctx->opcode)]);
5181 }
5182
5183 /* nabso - nabso. */
5184 static void gen_nabso(DisasContext *ctx)
5185 {
5186     int l1 = gen_new_label();
5187     int l2 = gen_new_label();
5188     tcg_gen_brcondi_tl(TCG_COND_GT, cpu_gpr[rA(ctx->opcode)], 0, l1);
5189     tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)]);
5190     tcg_gen_br(l2);
5191     gen_set_label(l1);
5192     tcg_gen_neg_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)]);
5193     gen_set_label(l2);
5194     /* nabs never overflows */
5195     tcg_gen_movi_tl(cpu_ov, 0);
5196     if (unlikely(Rc(ctx->opcode) != 0))
5197         gen_set_Rc0(ctx, cpu_gpr[rD(ctx->opcode)]);
5198 }
5199
5200 /* rlmi - rlmi. */
5201 static void gen_rlmi(DisasContext *ctx)
5202 {
5203     uint32_t mb = MB(ctx->opcode);
5204     uint32_t me = ME(ctx->opcode);
5205     TCGv t0 = tcg_temp_new();
5206     tcg_gen_andi_tl(t0, cpu_gpr[rB(ctx->opcode)], 0x1F);
5207     tcg_gen_rotl_tl(t0, cpu_gpr[rS(ctx->opcode)], t0);
5208     tcg_gen_andi_tl(t0, t0, MASK(mb, me));
5209     tcg_gen_andi_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rA(ctx->opcode)], ~MASK(mb, me));
5210     tcg_gen_or_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rA(ctx->opcode)], t0);
5211     tcg_temp_free(t0);
5212     if (unlikely(Rc(ctx->opcode) != 0))
5213         gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
5214 }
5215
5216 /* rrib - rrib. */
5217 static void gen_rrib(DisasContext *ctx)
5218 {
5219     TCGv t0 = tcg_temp_new();
5220     TCGv t1 = tcg_temp_new();
5221     tcg_gen_andi_tl(t0, cpu_gpr[rB(ctx->opcode)], 0x1F);
5222     tcg_gen_movi_tl(t1, 0x80000000);
5223     tcg_gen_shr_tl(t1, t1, t0);
5224     tcg_gen_shr_tl(t0, cpu_gpr[rS(ctx->opcode)], t0);
5225     tcg_gen_and_tl(t0, t0, t1);
5226     tcg_gen_andc_tl(t1, cpu_gpr[rA(ctx->opcode)], t1);
5227     tcg_gen_or_tl(cpu_gpr[rA(ctx->opcode)], t0, t1);
5228     tcg_temp_free(t0);
5229     tcg_temp_free(t1);
5230     if (unlikely(Rc(ctx->opcode) != 0))
5231         gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
5232 }
5233
5234 /* sle - sle. */
5235 static void gen_sle(DisasContext *ctx)
5236 {
5237     TCGv t0 = tcg_temp_new();
5238     TCGv t1 = tcg_temp_new();
5239     tcg_gen_andi_tl(t1, cpu_gpr[rB(ctx->opcode)], 0x1F);
5240     tcg_gen_shl_tl(t0, cpu_gpr[rS(ctx->opcode)], t1);
5241     tcg_gen_subfi_tl(t1, 32, t1);
5242     tcg_gen_shr_tl(t1, cpu_gpr[rS(ctx->opcode)], t1);
5243     tcg_gen_or_tl(t1, t0, t1);
5244     tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], t0);
5245     gen_store_spr(SPR_MQ, t1);
5246     tcg_temp_free(t0);
5247     tcg_temp_free(t1);
5248     if (unlikely(Rc(ctx->opcode) != 0))
5249         gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
5250 }
5251
5252 /* sleq - sleq. */
5253 static void gen_sleq(DisasContext *ctx)
5254 {
5255     TCGv t0 = tcg_temp_new();
5256     TCGv t1 = tcg_temp_new();
5257     TCGv t2 = tcg_temp_new();
5258     tcg_gen_andi_tl(t0, cpu_gpr[rB(ctx->opcode)], 0x1F);
5259     tcg_gen_movi_tl(t2, 0xFFFFFFFF);
5260     tcg_gen_shl_tl(t2, t2, t0);
5261     tcg_gen_rotl_tl(t0, cpu_gpr[rS(ctx->opcode)], t0);
5262     gen_load_spr(t1, SPR_MQ);
5263     gen_store_spr(SPR_MQ, t0);
5264     tcg_gen_and_tl(t0, t0, t2);
5265     tcg_gen_andc_tl(t1, t1, t2);
5266     tcg_gen_or_tl(cpu_gpr[rA(ctx->opcode)], t0, t1);
5267     tcg_temp_free(t0);
5268     tcg_temp_free(t1);
5269     tcg_temp_free(t2);
5270     if (unlikely(Rc(ctx->opcode) != 0))
5271         gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
5272 }
5273
5274 /* sliq - sliq. */
5275 static void gen_sliq(DisasContext *ctx)
5276 {
5277     int sh = SH(ctx->opcode);
5278     TCGv t0 = tcg_temp_new();
5279     TCGv t1 = tcg_temp_new();
5280     tcg_gen_shli_tl(t0, cpu_gpr[rS(ctx->opcode)], sh);
5281     tcg_gen_shri_tl(t1, cpu_gpr[rS(ctx->opcode)], 32 - sh);
5282     tcg_gen_or_tl(t1, t0, t1);
5283     tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], t0);
5284     gen_store_spr(SPR_MQ, t1);
5285     tcg_temp_free(t0);
5286     tcg_temp_free(t1);
5287     if (unlikely(Rc(ctx->opcode) != 0))
5288         gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
5289 }
5290
5291 /* slliq - slliq. */
5292 static void gen_slliq(DisasContext *ctx)
5293 {
5294     int sh = SH(ctx->opcode);
5295     TCGv t0 = tcg_temp_new();
5296     TCGv t1 = tcg_temp_new();
5297     tcg_gen_rotli_tl(t0, cpu_gpr[rS(ctx->opcode)], sh);
5298     gen_load_spr(t1, SPR_MQ);
5299     gen_store_spr(SPR_MQ, t0);
5300     tcg_gen_andi_tl(t0, t0,  (0xFFFFFFFFU << sh));
5301     tcg_gen_andi_tl(t1, t1, ~(0xFFFFFFFFU << sh));
5302     tcg_gen_or_tl(cpu_gpr[rA(ctx->opcode)], t0, t1);
5303     tcg_temp_free(t0);
5304     tcg_temp_free(t1);
5305     if (unlikely(Rc(ctx->opcode) != 0))
5306         gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
5307 }
5308
5309 /* sllq - sllq. */
5310 static void gen_sllq(DisasContext *ctx)
5311 {
5312     int l1 = gen_new_label();
5313     int l2 = gen_new_label();
5314     TCGv t0 = tcg_temp_local_new();
5315     TCGv t1 = tcg_temp_local_new();
5316     TCGv t2 = tcg_temp_local_new();
5317     tcg_gen_andi_tl(t2, cpu_gpr[rB(ctx->opcode)], 0x1F);
5318     tcg_gen_movi_tl(t1, 0xFFFFFFFF);
5319     tcg_gen_shl_tl(t1, t1, t2);
5320     tcg_gen_andi_tl(t0, cpu_gpr[rB(ctx->opcode)], 0x20);
5321     tcg_gen_brcondi_tl(TCG_COND_EQ, t0, 0, l1);
5322     gen_load_spr(t0, SPR_MQ);
5323     tcg_gen_and_tl(cpu_gpr[rA(ctx->opcode)], t0, t1);
5324     tcg_gen_br(l2);
5325     gen_set_label(l1);
5326     tcg_gen_shl_tl(t0, cpu_gpr[rS(ctx->opcode)], t2);
5327     gen_load_spr(t2, SPR_MQ);
5328     tcg_gen_andc_tl(t1, t2, t1);
5329     tcg_gen_or_tl(cpu_gpr[rA(ctx->opcode)], t0, t1);
5330     gen_set_label(l2);
5331     tcg_temp_free(t0);
5332     tcg_temp_free(t1);
5333     tcg_temp_free(t2);
5334     if (unlikely(Rc(ctx->opcode) != 0))
5335         gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
5336 }
5337
5338 /* slq - slq. */
5339 static void gen_slq(DisasContext *ctx)
5340 {
5341     int l1 = gen_new_label();
5342     TCGv t0 = tcg_temp_new();
5343     TCGv t1 = tcg_temp_new();
5344     tcg_gen_andi_tl(t1, cpu_gpr[rB(ctx->opcode)], 0x1F);
5345     tcg_gen_shl_tl(t0, cpu_gpr[rS(ctx->opcode)], t1);
5346     tcg_gen_subfi_tl(t1, 32, t1);
5347     tcg_gen_shr_tl(t1, cpu_gpr[rS(ctx->opcode)], t1);
5348     tcg_gen_or_tl(t1, t0, t1);
5349     gen_store_spr(SPR_MQ, t1);
5350     tcg_gen_andi_tl(t1, cpu_gpr[rB(ctx->opcode)], 0x20);
5351     tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], t0);
5352     tcg_gen_brcondi_tl(TCG_COND_EQ, t1, 0, l1);
5353     tcg_gen_movi_tl(cpu_gpr[rA(ctx->opcode)], 0);
5354     gen_set_label(l1);
5355     tcg_temp_free(t0);
5356     tcg_temp_free(t1);
5357     if (unlikely(Rc(ctx->opcode) != 0))
5358         gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
5359 }
5360
5361 /* sraiq - sraiq. */
5362 static void gen_sraiq(DisasContext *ctx)
5363 {
5364     int sh = SH(ctx->opcode);
5365     int l1 = gen_new_label();
5366     TCGv t0 = tcg_temp_new();
5367     TCGv t1 = tcg_temp_new();
5368     tcg_gen_shri_tl(t0, cpu_gpr[rS(ctx->opcode)], sh);
5369     tcg_gen_shli_tl(t1, cpu_gpr[rS(ctx->opcode)], 32 - sh);
5370     tcg_gen_or_tl(t0, t0, t1);
5371     gen_store_spr(SPR_MQ, t0);
5372     tcg_gen_movi_tl(cpu_ca, 0);
5373     tcg_gen_brcondi_tl(TCG_COND_EQ, t1, 0, l1);
5374     tcg_gen_brcondi_tl(TCG_COND_GE, cpu_gpr[rS(ctx->opcode)], 0, l1);
5375     tcg_gen_movi_tl(cpu_ca, 1);
5376     gen_set_label(l1);
5377     tcg_gen_sari_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)], sh);
5378     tcg_temp_free(t0);
5379     tcg_temp_free(t1);
5380     if (unlikely(Rc(ctx->opcode) != 0))
5381         gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
5382 }
5383
5384 /* sraq - sraq. */
5385 static void gen_sraq(DisasContext *ctx)
5386 {
5387     int l1 = gen_new_label();
5388     int l2 = gen_new_label();
5389     TCGv t0 = tcg_temp_new();
5390     TCGv t1 = tcg_temp_local_new();
5391     TCGv t2 = tcg_temp_local_new();
5392     tcg_gen_andi_tl(t2, cpu_gpr[rB(ctx->opcode)], 0x1F);
5393     tcg_gen_shr_tl(t0, cpu_gpr[rS(ctx->opcode)], t2);
5394     tcg_gen_sar_tl(t1, cpu_gpr[rS(ctx->opcode)], t2);
5395     tcg_gen_subfi_tl(t2, 32, t2);
5396     tcg_gen_shl_tl(t2, cpu_gpr[rS(ctx->opcode)], t2);
5397     tcg_gen_or_tl(t0, t0, t2);
5398     gen_store_spr(SPR_MQ, t0);
5399     tcg_gen_andi_tl(t0, cpu_gpr[rB(ctx->opcode)], 0x20);
5400     tcg_gen_brcondi_tl(TCG_COND_EQ, t2, 0, l1);
5401     tcg_gen_mov_tl(t2, cpu_gpr[rS(ctx->opcode)]);
5402     tcg_gen_sari_tl(t1, cpu_gpr[rS(ctx->opcode)], 31);
5403     gen_set_label(l1);
5404     tcg_temp_free(t0);
5405     tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], t1);
5406     tcg_gen_movi_tl(cpu_ca, 0);
5407     tcg_gen_brcondi_tl(TCG_COND_GE, t1, 0, l2);
5408     tcg_gen_brcondi_tl(TCG_COND_EQ, t2, 0, l2);
5409     tcg_gen_movi_tl(cpu_ca, 1);
5410     gen_set_label(l2);
5411     tcg_temp_free(t1);
5412     tcg_temp_free(t2);
5413     if (unlikely(Rc(ctx->opcode) != 0))
5414         gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
5415 }
5416
5417 /* sre - sre. */
5418 static void gen_sre(DisasContext *ctx)
5419 {
5420     TCGv t0 = tcg_temp_new();
5421     TCGv t1 = tcg_temp_new();
5422     tcg_gen_andi_tl(t1, cpu_gpr[rB(ctx->opcode)], 0x1F);
5423     tcg_gen_shr_tl(t0, cpu_gpr[rS(ctx->opcode)], t1);
5424     tcg_gen_subfi_tl(t1, 32, t1);
5425     tcg_gen_shl_tl(t1, cpu_gpr[rS(ctx->opcode)], t1);
5426     tcg_gen_or_tl(t1, t0, t1);
5427     tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], t0);
5428     gen_store_spr(SPR_MQ, t1);
5429     tcg_temp_free(t0);
5430     tcg_temp_free(t1);
5431     if (unlikely(Rc(ctx->opcode) != 0))
5432         gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
5433 }
5434
5435 /* srea - srea. */
5436 static void gen_srea(DisasContext *ctx)
5437 {
5438     TCGv t0 = tcg_temp_new();
5439     TCGv t1 = tcg_temp_new();
5440     tcg_gen_andi_tl(t1, cpu_gpr[rB(ctx->opcode)], 0x1F);
5441     tcg_gen_rotr_tl(t0, cpu_gpr[rS(ctx->opcode)], t1);
5442     gen_store_spr(SPR_MQ, t0);
5443     tcg_gen_sar_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)], t1);
5444     tcg_temp_free(t0);
5445     tcg_temp_free(t1);
5446     if (unlikely(Rc(ctx->opcode) != 0))
5447         gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
5448 }
5449
5450 /* sreq */
5451 static void gen_sreq(DisasContext *ctx)
5452 {
5453     TCGv t0 = tcg_temp_new();
5454     TCGv t1 = tcg_temp_new();
5455     TCGv t2 = tcg_temp_new();
5456     tcg_gen_andi_tl(t0, cpu_gpr[rB(ctx->opcode)], 0x1F);
5457     tcg_gen_movi_tl(t1, 0xFFFFFFFF);
5458     tcg_gen_shr_tl(t1, t1, t0);
5459     tcg_gen_rotr_tl(t0, cpu_gpr[rS(ctx->opcode)], t0);
5460     gen_load_spr(t2, SPR_MQ);
5461     gen_store_spr(SPR_MQ, t0);
5462     tcg_gen_and_tl(t0, t0, t1);
5463     tcg_gen_andc_tl(t2, t2, t1);
5464     tcg_gen_or_tl(cpu_gpr[rA(ctx->opcode)], t0, t2);
5465     tcg_temp_free(t0);
5466     tcg_temp_free(t1);
5467     tcg_temp_free(t2);
5468     if (unlikely(Rc(ctx->opcode) != 0))
5469         gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
5470 }
5471
5472 /* sriq */
5473 static void gen_sriq(DisasContext *ctx)
5474 {
5475     int sh = SH(ctx->opcode);
5476     TCGv t0 = tcg_temp_new();
5477     TCGv t1 = tcg_temp_new();
5478     tcg_gen_shri_tl(t0, cpu_gpr[rS(ctx->opcode)], sh);
5479     tcg_gen_shli_tl(t1, cpu_gpr[rS(ctx->opcode)], 32 - sh);
5480     tcg_gen_or_tl(t1, t0, t1);
5481     tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], t0);
5482     gen_store_spr(SPR_MQ, t1);
5483     tcg_temp_free(t0);
5484     tcg_temp_free(t1);
5485     if (unlikely(Rc(ctx->opcode) != 0))
5486         gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
5487 }
5488
5489 /* srliq */
5490 static void gen_srliq(DisasContext *ctx)
5491 {
5492     int sh = SH(ctx->opcode);
5493     TCGv t0 = tcg_temp_new();
5494     TCGv t1 = tcg_temp_new();
5495     tcg_gen_rotri_tl(t0, cpu_gpr[rS(ctx->opcode)], sh);
5496     gen_load_spr(t1, SPR_MQ);
5497     gen_store_spr(SPR_MQ, t0);
5498     tcg_gen_andi_tl(t0, t0,  (0xFFFFFFFFU >> sh));
5499     tcg_gen_andi_tl(t1, t1, ~(0xFFFFFFFFU >> sh));
5500     tcg_gen_or_tl(cpu_gpr[rA(ctx->opcode)], t0, t1);
5501     tcg_temp_free(t0);
5502     tcg_temp_free(t1);
5503     if (unlikely(Rc(ctx->opcode) != 0))
5504         gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
5505 }
5506
5507 /* srlq */
5508 static void gen_srlq(DisasContext *ctx)
5509 {
5510     int l1 = gen_new_label();
5511     int l2 = gen_new_label();
5512     TCGv t0 = tcg_temp_local_new();
5513     TCGv t1 = tcg_temp_local_new();
5514     TCGv t2 = tcg_temp_local_new();
5515     tcg_gen_andi_tl(t2, cpu_gpr[rB(ctx->opcode)], 0x1F);
5516     tcg_gen_movi_tl(t1, 0xFFFFFFFF);
5517     tcg_gen_shr_tl(t2, t1, t2);
5518     tcg_gen_andi_tl(t0, cpu_gpr[rB(ctx->opcode)], 0x20);
5519     tcg_gen_brcondi_tl(TCG_COND_EQ, t0, 0, l1);
5520     gen_load_spr(t0, SPR_MQ);
5521     tcg_gen_and_tl(cpu_gpr[rA(ctx->opcode)], t0, t2);
5522     tcg_gen_br(l2);
5523     gen_set_label(l1);
5524     tcg_gen_shr_tl(t0, cpu_gpr[rS(ctx->opcode)], t2);
5525     tcg_gen_and_tl(t0, t0, t2);
5526     gen_load_spr(t1, SPR_MQ);
5527     tcg_gen_andc_tl(t1, t1, t2);
5528     tcg_gen_or_tl(cpu_gpr[rA(ctx->opcode)], t0, t1);
5529     gen_set_label(l2);
5530     tcg_temp_free(t0);
5531     tcg_temp_free(t1);
5532     tcg_temp_free(t2);
5533     if (unlikely(Rc(ctx->opcode) != 0))
5534         gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
5535 }
5536
5537 /* srq */
5538 static void gen_srq(DisasContext *ctx)
5539 {
5540     int l1 = gen_new_label();
5541     TCGv t0 = tcg_temp_new();
5542     TCGv t1 = tcg_temp_new();
5543     tcg_gen_andi_tl(t1, cpu_gpr[rB(ctx->opcode)], 0x1F);
5544     tcg_gen_shr_tl(t0, cpu_gpr[rS(ctx->opcode)], t1);
5545     tcg_gen_subfi_tl(t1, 32, t1);
5546     tcg_gen_shl_tl(t1, cpu_gpr[rS(ctx->opcode)], t1);
5547     tcg_gen_or_tl(t1, t0, t1);
5548     gen_store_spr(SPR_MQ, t1);
5549     tcg_gen_andi_tl(t1, cpu_gpr[rB(ctx->opcode)], 0x20);
5550     tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], t0);
5551     tcg_gen_brcondi_tl(TCG_COND_EQ, t0, 0, l1);
5552     tcg_gen_movi_tl(cpu_gpr[rA(ctx->opcode)], 0);
5553     gen_set_label(l1);
5554     tcg_temp_free(t0);
5555     tcg_temp_free(t1);
5556     if (unlikely(Rc(ctx->opcode) != 0))
5557         gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
5558 }
5559
5560 /* PowerPC 602 specific instructions */
5561
5562 /* dsa  */
5563 static void gen_dsa(DisasContext *ctx)
5564 {
5565     /* XXX: TODO */
5566     gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL);
5567 }
5568
5569 /* esa */
5570 static void gen_esa(DisasContext *ctx)
5571 {
5572     /* XXX: TODO */
5573     gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL);
5574 }
5575
5576 /* mfrom */
5577 static void gen_mfrom(DisasContext *ctx)
5578 {
5579 #if defined(CONFIG_USER_ONLY)
5580     gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
5581 #else
5582     if (unlikely(ctx->pr)) {
5583         gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
5584         return;
5585     }
5586     gen_helper_602_mfrom(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)]);
5587 #endif
5588 }
5589
5590 /* 602 - 603 - G2 TLB management */
5591
5592 /* tlbld */
5593 static void gen_tlbld_6xx(DisasContext *ctx)
5594 {
5595 #if defined(CONFIG_USER_ONLY)
5596     gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
5597 #else
5598     if (unlikely(ctx->pr)) {
5599         gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
5600         return;
5601     }
5602     gen_helper_6xx_tlbd(cpu_env, cpu_gpr[rB(ctx->opcode)]);
5603 #endif
5604 }
5605
5606 /* tlbli */
5607 static void gen_tlbli_6xx(DisasContext *ctx)
5608 {
5609 #if defined(CONFIG_USER_ONLY)
5610     gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
5611 #else
5612     if (unlikely(ctx->pr)) {
5613         gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
5614         return;
5615     }
5616     gen_helper_6xx_tlbi(cpu_env, cpu_gpr[rB(ctx->opcode)]);
5617 #endif
5618 }
5619
5620 /* 74xx TLB management */
5621
5622 /* tlbld */
5623 static void gen_tlbld_74xx(DisasContext *ctx)
5624 {
5625 #if defined(CONFIG_USER_ONLY)
5626     gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
5627 #else
5628     if (unlikely(ctx->pr)) {
5629         gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
5630         return;
5631     }
5632     gen_helper_74xx_tlbd(cpu_env, cpu_gpr[rB(ctx->opcode)]);
5633 #endif
5634 }
5635
5636 /* tlbli */
5637 static void gen_tlbli_74xx(DisasContext *ctx)
5638 {
5639 #if defined(CONFIG_USER_ONLY)
5640     gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
5641 #else
5642     if (unlikely(ctx->pr)) {
5643         gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
5644         return;
5645     }
5646     gen_helper_74xx_tlbi(cpu_env, cpu_gpr[rB(ctx->opcode)]);
5647 #endif
5648 }
5649
5650 /* POWER instructions not in PowerPC 601 */
5651
5652 /* clf */
5653 static void gen_clf(DisasContext *ctx)
5654 {
5655     /* Cache line flush: implemented as no-op */
5656 }
5657
5658 /* cli */
5659 static void gen_cli(DisasContext *ctx)
5660 {
5661     /* Cache line invalidate: privileged and treated as no-op */
5662 #if defined(CONFIG_USER_ONLY)
5663     gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
5664 #else
5665     if (unlikely(ctx->pr)) {
5666         gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
5667         return;
5668     }
5669 #endif
5670 }
5671
5672 /* dclst */
5673 static void gen_dclst(DisasContext *ctx)
5674 {
5675     /* Data cache line store: treated as no-op */
5676 }
5677
5678 static void gen_mfsri(DisasContext *ctx)
5679 {
5680 #if defined(CONFIG_USER_ONLY)
5681     gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
5682 #else
5683     int ra = rA(ctx->opcode);
5684     int rd = rD(ctx->opcode);
5685     TCGv t0;
5686     if (unlikely(ctx->pr)) {
5687         gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
5688         return;
5689     }
5690     t0 = tcg_temp_new();
5691     gen_addr_reg_index(ctx, t0);
5692     tcg_gen_shri_tl(t0, t0, 28);
5693     tcg_gen_andi_tl(t0, t0, 0xF);
5694     gen_helper_load_sr(cpu_gpr[rd], cpu_env, t0);
5695     tcg_temp_free(t0);
5696     if (ra != 0 && ra != rd)
5697         tcg_gen_mov_tl(cpu_gpr[ra], cpu_gpr[rd]);
5698 #endif
5699 }
5700
5701 static void gen_rac(DisasContext *ctx)
5702 {
5703 #if defined(CONFIG_USER_ONLY)
5704     gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
5705 #else
5706     TCGv t0;
5707     if (unlikely(ctx->pr)) {
5708         gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
5709         return;
5710     }
5711     t0 = tcg_temp_new();
5712     gen_addr_reg_index(ctx, t0);
5713     gen_helper_rac(cpu_gpr[rD(ctx->opcode)], cpu_env, t0);
5714     tcg_temp_free(t0);
5715 #endif
5716 }
5717
5718 static void gen_rfsvc(DisasContext *ctx)
5719 {
5720 #if defined(CONFIG_USER_ONLY)
5721     gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
5722 #else
5723     if (unlikely(ctx->pr)) {
5724         gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
5725         return;
5726     }
5727     gen_helper_rfsvc(cpu_env);
5728     gen_sync_exception(ctx);
5729 #endif
5730 }
5731
5732 /* svc is not implemented for now */
5733
5734 /* POWER2 specific instructions */
5735 /* Quad manipulation (load/store two floats at a time) */
5736
5737 /* lfq */
5738 static void gen_lfq(DisasContext *ctx)
5739 {
5740     int rd = rD(ctx->opcode);
5741     TCGv t0;
5742     gen_set_access_type(ctx, ACCESS_FLOAT);
5743     t0 = tcg_temp_new();
5744     gen_addr_imm_index(ctx, t0, 0);
5745     gen_qemu_ld64(ctx, cpu_fpr[rd], t0);
5746     gen_addr_add(ctx, t0, t0, 8);
5747     gen_qemu_ld64(ctx, cpu_fpr[(rd + 1) % 32], t0);
5748     tcg_temp_free(t0);
5749 }
5750
5751 /* lfqu */
5752 static void gen_lfqu(DisasContext *ctx)
5753 {
5754     int ra = rA(ctx->opcode);
5755     int rd = rD(ctx->opcode);
5756     TCGv t0, t1;
5757     gen_set_access_type(ctx, ACCESS_FLOAT);
5758     t0 = tcg_temp_new();
5759     t1 = tcg_temp_new();
5760     gen_addr_imm_index(ctx, t0, 0);
5761     gen_qemu_ld64(ctx, cpu_fpr[rd], t0);
5762     gen_addr_add(ctx, t1, t0, 8);
5763     gen_qemu_ld64(ctx, cpu_fpr[(rd + 1) % 32], t1);
5764     if (ra != 0)
5765         tcg_gen_mov_tl(cpu_gpr[ra], t0);
5766     tcg_temp_free(t0);
5767     tcg_temp_free(t1);
5768 }
5769
5770 /* lfqux */
5771 static void gen_lfqux(DisasContext *ctx)
5772 {
5773     int ra = rA(ctx->opcode);
5774     int rd = rD(ctx->opcode);
5775     gen_set_access_type(ctx, ACCESS_FLOAT);
5776     TCGv t0, t1;
5777     t0 = tcg_temp_new();
5778     gen_addr_reg_index(ctx, t0);
5779     gen_qemu_ld64(ctx, cpu_fpr[rd], t0);
5780     t1 = tcg_temp_new();
5781     gen_addr_add(ctx, t1, t0, 8);
5782     gen_qemu_ld64(ctx, cpu_fpr[(rd + 1) % 32], t1);
5783     tcg_temp_free(t1);
5784     if (ra != 0)
5785         tcg_gen_mov_tl(cpu_gpr[ra], t0);
5786     tcg_temp_free(t0);
5787 }
5788
5789 /* lfqx */
5790 static void gen_lfqx(DisasContext *ctx)
5791 {
5792     int rd = rD(ctx->opcode);
5793     TCGv t0;
5794     gen_set_access_type(ctx, ACCESS_FLOAT);
5795     t0 = tcg_temp_new();
5796     gen_addr_reg_index(ctx, t0);
5797     gen_qemu_ld64(ctx, cpu_fpr[rd], t0);
5798     gen_addr_add(ctx, t0, t0, 8);
5799     gen_qemu_ld64(ctx, cpu_fpr[(rd + 1) % 32], t0);
5800     tcg_temp_free(t0);
5801 }
5802
5803 /* stfq */
5804 static void gen_stfq(DisasContext *ctx)
5805 {
5806     int rd = rD(ctx->opcode);
5807     TCGv t0;
5808     gen_set_access_type(ctx, ACCESS_FLOAT);
5809     t0 = tcg_temp_new();
5810     gen_addr_imm_index(ctx, t0, 0);
5811     gen_qemu_st64(ctx, cpu_fpr[rd], t0);
5812     gen_addr_add(ctx, t0, t0, 8);
5813     gen_qemu_st64(ctx, cpu_fpr[(rd + 1) % 32], t0);
5814     tcg_temp_free(t0);
5815 }
5816
5817 /* stfqu */
5818 static void gen_stfqu(DisasContext *ctx)
5819 {
5820     int ra = rA(ctx->opcode);
5821     int rd = rD(ctx->opcode);
5822     TCGv t0, t1;
5823     gen_set_access_type(ctx, ACCESS_FLOAT);
5824     t0 = tcg_temp_new();
5825     gen_addr_imm_index(ctx, t0, 0);
5826     gen_qemu_st64(ctx, cpu_fpr[rd], t0);
5827     t1 = tcg_temp_new();
5828     gen_addr_add(ctx, t1, t0, 8);
5829     gen_qemu_st64(ctx, cpu_fpr[(rd + 1) % 32], t1);
5830     tcg_temp_free(t1);
5831     if (ra != 0)
5832         tcg_gen_mov_tl(cpu_gpr[ra], t0);
5833     tcg_temp_free(t0);
5834 }
5835
5836 /* stfqux */
5837 static void gen_stfqux(DisasContext *ctx)
5838 {
5839     int ra = rA(ctx->opcode);
5840     int rd = rD(ctx->opcode);
5841     TCGv t0, t1;
5842     gen_set_access_type(ctx, ACCESS_FLOAT);
5843     t0 = tcg_temp_new();
5844     gen_addr_reg_index(ctx, t0);
5845     gen_qemu_st64(ctx, cpu_fpr[rd], t0);
5846     t1 = tcg_temp_new();
5847     gen_addr_add(ctx, t1, t0, 8);
5848     gen_qemu_st64(ctx, cpu_fpr[(rd + 1) % 32], t1);
5849     tcg_temp_free(t1);
5850     if (ra != 0)
5851         tcg_gen_mov_tl(cpu_gpr[ra], t0);
5852     tcg_temp_free(t0);
5853 }
5854
5855 /* stfqx */
5856 static void gen_stfqx(DisasContext *ctx)
5857 {
5858     int rd = rD(ctx->opcode);
5859     TCGv t0;
5860     gen_set_access_type(ctx, ACCESS_FLOAT);
5861     t0 = tcg_temp_new();
5862     gen_addr_reg_index(ctx, t0);
5863     gen_qemu_st64(ctx, cpu_fpr[rd], t0);
5864     gen_addr_add(ctx, t0, t0, 8);
5865     gen_qemu_st64(ctx, cpu_fpr[(rd + 1) % 32], t0);
5866     tcg_temp_free(t0);
5867 }
5868
5869 /* BookE specific instructions */
5870
5871 /* XXX: not implemented on 440 ? */
5872 static void gen_mfapidi(DisasContext *ctx)
5873 {
5874     /* XXX: TODO */
5875     gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL);
5876 }
5877
5878 /* XXX: not implemented on 440 ? */
5879 static void gen_tlbiva(DisasContext *ctx)
5880 {
5881 #if defined(CONFIG_USER_ONLY)
5882     gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
5883 #else
5884     TCGv t0;
5885     if (unlikely(ctx->pr)) {
5886         gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
5887         return;
5888     }
5889     t0 = tcg_temp_new();
5890     gen_addr_reg_index(ctx, t0);
5891     gen_helper_tlbie(cpu_env, cpu_gpr[rB(ctx->opcode)]);
5892     tcg_temp_free(t0);
5893 #endif
5894 }
5895
5896 /* All 405 MAC instructions are translated here */
5897 static inline void gen_405_mulladd_insn(DisasContext *ctx, int opc2, int opc3,
5898                                         int ra, int rb, int rt, int Rc)
5899 {
5900     TCGv t0, t1;
5901
5902     t0 = tcg_temp_local_new();
5903     t1 = tcg_temp_local_new();
5904
5905     switch (opc3 & 0x0D) {
5906     case 0x05:
5907         /* macchw    - macchw.    - macchwo   - macchwo.   */
5908         /* macchws   - macchws.   - macchwso  - macchwso.  */
5909         /* nmacchw   - nmacchw.   - nmacchwo  - nmacchwo.  */
5910         /* nmacchws  - nmacchws.  - nmacchwso - nmacchwso. */
5911         /* mulchw - mulchw. */
5912         tcg_gen_ext16s_tl(t0, cpu_gpr[ra]);
5913         tcg_gen_sari_tl(t1, cpu_gpr[rb], 16);
5914         tcg_gen_ext16s_tl(t1, t1);
5915         break;
5916     case 0x04:
5917         /* macchwu   - macchwu.   - macchwuo  - macchwuo.  */
5918         /* macchwsu  - macchwsu.  - macchwsuo - macchwsuo. */
5919         /* mulchwu - mulchwu. */
5920         tcg_gen_ext16u_tl(t0, cpu_gpr[ra]);
5921         tcg_gen_shri_tl(t1, cpu_gpr[rb], 16);
5922         tcg_gen_ext16u_tl(t1, t1);
5923         break;
5924     case 0x01:
5925         /* machhw    - machhw.    - machhwo   - machhwo.   */
5926         /* machhws   - machhws.   - machhwso  - machhwso.  */
5927         /* nmachhw   - nmachhw.   - nmachhwo  - nmachhwo.  */
5928         /* nmachhws  - nmachhws.  - nmachhwso - nmachhwso. */
5929         /* mulhhw - mulhhw. */
5930         tcg_gen_sari_tl(t0, cpu_gpr[ra], 16);
5931         tcg_gen_ext16s_tl(t0, t0);
5932         tcg_gen_sari_tl(t1, cpu_gpr[rb], 16);
5933         tcg_gen_ext16s_tl(t1, t1);
5934         break;
5935     case 0x00:
5936         /* machhwu   - machhwu.   - machhwuo  - machhwuo.  */
5937         /* machhwsu  - machhwsu.  - machhwsuo - machhwsuo. */
5938         /* mulhhwu - mulhhwu. */
5939         tcg_gen_shri_tl(t0, cpu_gpr[ra], 16);
5940         tcg_gen_ext16u_tl(t0, t0);
5941         tcg_gen_shri_tl(t1, cpu_gpr[rb], 16);
5942         tcg_gen_ext16u_tl(t1, t1);
5943         break;
5944     case 0x0D:
5945         /* maclhw    - maclhw.    - maclhwo   - maclhwo.   */
5946         /* maclhws   - maclhws.   - maclhwso  - maclhwso.  */
5947         /* nmaclhw   - nmaclhw.   - nmaclhwo  - nmaclhwo.  */
5948         /* nmaclhws  - nmaclhws.  - nmaclhwso - nmaclhwso. */
5949         /* mullhw - mullhw. */
5950         tcg_gen_ext16s_tl(t0, cpu_gpr[ra]);
5951         tcg_gen_ext16s_tl(t1, cpu_gpr[rb]);
5952         break;
5953     case 0x0C:
5954         /* maclhwu   - maclhwu.   - maclhwuo  - maclhwuo.  */
5955         /* maclhwsu  - maclhwsu.  - maclhwsuo - maclhwsuo. */
5956         /* mullhwu - mullhwu. */
5957         tcg_gen_ext16u_tl(t0, cpu_gpr[ra]);
5958         tcg_gen_ext16u_tl(t1, cpu_gpr[rb]);
5959         break;
5960     }
5961     if (opc2 & 0x04) {
5962         /* (n)multiply-and-accumulate (0x0C / 0x0E) */
5963         tcg_gen_mul_tl(t1, t0, t1);
5964         if (opc2 & 0x02) {
5965             /* nmultiply-and-accumulate (0x0E) */
5966             tcg_gen_sub_tl(t0, cpu_gpr[rt], t1);
5967         } else {
5968             /* multiply-and-accumulate (0x0C) */
5969             tcg_gen_add_tl(t0, cpu_gpr[rt], t1);
5970         }
5971
5972         if (opc3 & 0x12) {
5973             /* Check overflow and/or saturate */
5974             int l1 = gen_new_label();
5975
5976             if (opc3 & 0x10) {
5977                 /* Start with XER OV disabled, the most likely case */
5978                 tcg_gen_movi_tl(cpu_ov, 0);
5979             }
5980             if (opc3 & 0x01) {
5981                 /* Signed */
5982                 tcg_gen_xor_tl(t1, cpu_gpr[rt], t1);
5983                 tcg_gen_brcondi_tl(TCG_COND_GE, t1, 0, l1);
5984                 tcg_gen_xor_tl(t1, cpu_gpr[rt], t0);
5985                 tcg_gen_brcondi_tl(TCG_COND_LT, t1, 0, l1);
5986                 if (opc3 & 0x02) {
5987                     /* Saturate */
5988                     tcg_gen_sari_tl(t0, cpu_gpr[rt], 31);
5989                     tcg_gen_xori_tl(t0, t0, 0x7fffffff);
5990                 }
5991             } else {
5992                 /* Unsigned */
5993                 tcg_gen_brcond_tl(TCG_COND_GEU, t0, t1, l1);
5994                 if (opc3 & 0x02) {
5995                     /* Saturate */
5996                     tcg_gen_movi_tl(t0, UINT32_MAX);
5997                 }
5998             }
5999             if (opc3 & 0x10) {
6000                 /* Check overflow */
6001                 tcg_gen_movi_tl(cpu_ov, 1);
6002                 tcg_gen_movi_tl(cpu_so, 1);
6003             }
6004             gen_set_label(l1);
6005             tcg_gen_mov_tl(cpu_gpr[rt], t0);
6006         }
6007     } else {
6008         tcg_gen_mul_tl(cpu_gpr[rt], t0, t1);
6009     }
6010     tcg_temp_free(t0);
6011     tcg_temp_free(t1);
6012     if (unlikely(Rc) != 0) {
6013         /* Update Rc0 */
6014         gen_set_Rc0(ctx, cpu_gpr[rt]);
6015     }
6016 }
6017
6018 #define GEN_MAC_HANDLER(name, opc2, opc3)                                     \
6019 static void glue(gen_, name)(DisasContext *ctx)                               \
6020 {                                                                             \
6021     gen_405_mulladd_insn(ctx, opc2, opc3, rA(ctx->opcode), rB(ctx->opcode),   \
6022                          rD(ctx->opcode), Rc(ctx->opcode));                   \
6023 }
6024
6025 /* macchw    - macchw.    */
6026 GEN_MAC_HANDLER(macchw, 0x0C, 0x05);
6027 /* macchwo   - macchwo.   */
6028 GEN_MAC_HANDLER(macchwo, 0x0C, 0x15);
6029 /* macchws   - macchws.   */
6030 GEN_MAC_HANDLER(macchws, 0x0C, 0x07);
6031 /* macchwso  - macchwso.  */
6032 GEN_MAC_HANDLER(macchwso, 0x0C, 0x17);
6033 /* macchwsu  - macchwsu.  */
6034 GEN_MAC_HANDLER(macchwsu, 0x0C, 0x06);
6035 /* macchwsuo - macchwsuo. */
6036 GEN_MAC_HANDLER(macchwsuo, 0x0C, 0x16);
6037 /* macchwu   - macchwu.   */
6038 GEN_MAC_HANDLER(macchwu, 0x0C, 0x04);
6039 /* macchwuo  - macchwuo.  */
6040 GEN_MAC_HANDLER(macchwuo, 0x0C, 0x14);
6041 /* machhw    - machhw.    */
6042 GEN_MAC_HANDLER(machhw, 0x0C, 0x01);
6043 /* machhwo   - machhwo.   */
6044 GEN_MAC_HANDLER(machhwo, 0x0C, 0x11);
6045 /* machhws   - machhws.   */
6046 GEN_MAC_HANDLER(machhws, 0x0C, 0x03);
6047 /* machhwso  - machhwso.  */
6048 GEN_MAC_HANDLER(machhwso, 0x0C, 0x13);
6049 /* machhwsu  - machhwsu.  */
6050 GEN_MAC_HANDLER(machhwsu, 0x0C, 0x02);
6051 /* machhwsuo - machhwsuo. */
6052 GEN_MAC_HANDLER(machhwsuo, 0x0C, 0x12);
6053 /* machhwu   - machhwu.   */
6054 GEN_MAC_HANDLER(machhwu, 0x0C, 0x00);
6055 /* machhwuo  - machhwuo.  */
6056 GEN_MAC_HANDLER(machhwuo, 0x0C, 0x10);
6057 /* maclhw    - maclhw.    */
6058 GEN_MAC_HANDLER(maclhw, 0x0C, 0x0D);
6059 /* maclhwo   - maclhwo.   */
6060 GEN_MAC_HANDLER(maclhwo, 0x0C, 0x1D);
6061 /* maclhws   - maclhws.   */
6062 GEN_MAC_HANDLER(maclhws, 0x0C, 0x0F);
6063 /* maclhwso  - maclhwso.  */
6064 GEN_MAC_HANDLER(maclhwso, 0x0C, 0x1F);
6065 /* maclhwu   - maclhwu.   */
6066 GEN_MAC_HANDLER(maclhwu, 0x0C, 0x0C);
6067 /* maclhwuo  - maclhwuo.  */
6068 GEN_MAC_HANDLER(maclhwuo, 0x0C, 0x1C);
6069 /* maclhwsu  - maclhwsu.  */
6070 GEN_MAC_HANDLER(maclhwsu, 0x0C, 0x0E);
6071 /* maclhwsuo - maclhwsuo. */
6072 GEN_MAC_HANDLER(maclhwsuo, 0x0C, 0x1E);
6073 /* nmacchw   - nmacchw.   */
6074 GEN_MAC_HANDLER(nmacchw, 0x0E, 0x05);
6075 /* nmacchwo  - nmacchwo.  */
6076 GEN_MAC_HANDLER(nmacchwo, 0x0E, 0x15);
6077 /* nmacchws  - nmacchws.  */
6078 GEN_MAC_HANDLER(nmacchws, 0x0E, 0x07);
6079 /* nmacchwso - nmacchwso. */
6080 GEN_MAC_HANDLER(nmacchwso, 0x0E, 0x17);
6081 /* nmachhw   - nmachhw.   */
6082 GEN_MAC_HANDLER(nmachhw, 0x0E, 0x01);
6083 /* nmachhwo  - nmachhwo.  */
6084 GEN_MAC_HANDLER(nmachhwo, 0x0E, 0x11);
6085 /* nmachhws  - nmachhws.  */
6086 GEN_MAC_HANDLER(nmachhws, 0x0E, 0x03);
6087 /* nmachhwso - nmachhwso. */
6088 GEN_MAC_HANDLER(nmachhwso, 0x0E, 0x13);
6089 /* nmaclhw   - nmaclhw.   */
6090 GEN_MAC_HANDLER(nmaclhw, 0x0E, 0x0D);
6091 /* nmaclhwo  - nmaclhwo.  */
6092 GEN_MAC_HANDLER(nmaclhwo, 0x0E, 0x1D);
6093 /* nmaclhws  - nmaclhws.  */
6094 GEN_MAC_HANDLER(nmaclhws, 0x0E, 0x0F);
6095 /* nmaclhwso - nmaclhwso. */
6096 GEN_MAC_HANDLER(nmaclhwso, 0x0E, 0x1F);
6097
6098 /* mulchw  - mulchw.  */
6099 GEN_MAC_HANDLER(mulchw, 0x08, 0x05);
6100 /* mulchwu - mulchwu. */
6101 GEN_MAC_HANDLER(mulchwu, 0x08, 0x04);
6102 /* mulhhw  - mulhhw.  */
6103 GEN_MAC_HANDLER(mulhhw, 0x08, 0x01);
6104 /* mulhhwu - mulhhwu. */
6105 GEN_MAC_HANDLER(mulhhwu, 0x08, 0x00);
6106 /* mullhw  - mullhw.  */
6107 GEN_MAC_HANDLER(mullhw, 0x08, 0x0D);
6108 /* mullhwu - mullhwu. */
6109 GEN_MAC_HANDLER(mullhwu, 0x08, 0x0C);
6110
6111 /* mfdcr */
6112 static void gen_mfdcr(DisasContext *ctx)
6113 {
6114 #if defined(CONFIG_USER_ONLY)
6115     gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
6116 #else
6117     TCGv dcrn;
6118     if (unlikely(ctx->pr)) {
6119         gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
6120         return;
6121     }
6122     /* NIP cannot be restored if the memory exception comes from an helper */
6123     gen_update_nip(ctx, ctx->nip - 4);
6124     dcrn = tcg_const_tl(SPR(ctx->opcode));
6125     gen_helper_load_dcr(cpu_gpr[rD(ctx->opcode)], cpu_env, dcrn);
6126     tcg_temp_free(dcrn);
6127 #endif
6128 }
6129
6130 /* mtdcr */
6131 static void gen_mtdcr(DisasContext *ctx)
6132 {
6133 #if defined(CONFIG_USER_ONLY)
6134     gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
6135 #else
6136     TCGv dcrn;
6137     if (unlikely(ctx->pr)) {
6138         gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
6139         return;
6140     }
6141     /* NIP cannot be restored if the memory exception comes from an helper */
6142     gen_update_nip(ctx, ctx->nip - 4);
6143     dcrn = tcg_const_tl(SPR(ctx->opcode));
6144     gen_helper_store_dcr(cpu_env, dcrn, cpu_gpr[rS(ctx->opcode)]);
6145     tcg_temp_free(dcrn);
6146 #endif
6147 }
6148
6149 /* mfdcrx */
6150 /* XXX: not implemented on 440 ? */
6151 static void gen_mfdcrx(DisasContext *ctx)
6152 {
6153 #if defined(CONFIG_USER_ONLY)
6154     gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
6155 #else
6156     if (unlikely(ctx->pr)) {
6157         gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
6158         return;
6159     }
6160     /* NIP cannot be restored if the memory exception comes from an helper */
6161     gen_update_nip(ctx, ctx->nip - 4);
6162     gen_helper_load_dcr(cpu_gpr[rD(ctx->opcode)], cpu_env,
6163                         cpu_gpr[rA(ctx->opcode)]);
6164     /* Note: Rc update flag set leads to undefined state of Rc0 */
6165 #endif
6166 }
6167
6168 /* mtdcrx */
6169 /* XXX: not implemented on 440 ? */
6170 static void gen_mtdcrx(DisasContext *ctx)
6171 {
6172 #if defined(CONFIG_USER_ONLY)
6173     gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
6174 #else
6175     if (unlikely(ctx->pr)) {
6176         gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
6177         return;
6178     }
6179     /* NIP cannot be restored if the memory exception comes from an helper */
6180     gen_update_nip(ctx, ctx->nip - 4);
6181     gen_helper_store_dcr(cpu_env, cpu_gpr[rA(ctx->opcode)],
6182                          cpu_gpr[rS(ctx->opcode)]);
6183     /* Note: Rc update flag set leads to undefined state of Rc0 */
6184 #endif
6185 }
6186
6187 /* mfdcrux (PPC 460) : user-mode access to DCR */
6188 static void gen_mfdcrux(DisasContext *ctx)
6189 {
6190     /* NIP cannot be restored if the memory exception comes from an helper */
6191     gen_update_nip(ctx, ctx->nip - 4);
6192     gen_helper_load_dcr(cpu_gpr[rD(ctx->opcode)], cpu_env,
6193                         cpu_gpr[rA(ctx->opcode)]);
6194     /* Note: Rc update flag set leads to undefined state of Rc0 */
6195 }
6196
6197 /* mtdcrux (PPC 460) : user-mode access to DCR */
6198 static void gen_mtdcrux(DisasContext *ctx)
6199 {
6200     /* NIP cannot be restored if the memory exception comes from an helper */
6201     gen_update_nip(ctx, ctx->nip - 4);
6202     gen_helper_store_dcr(cpu_env, cpu_gpr[rA(ctx->opcode)],
6203                          cpu_gpr[rS(ctx->opcode)]);
6204     /* Note: Rc update flag set leads to undefined state of Rc0 */
6205 }
6206
6207 /* dccci */
6208 static void gen_dccci(DisasContext *ctx)
6209 {
6210 #if defined(CONFIG_USER_ONLY)
6211     gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
6212 #else
6213     if (unlikely(ctx->pr)) {
6214         gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
6215         return;
6216     }
6217     /* interpreted as no-op */
6218 #endif
6219 }
6220
6221 /* dcread */
6222 static void gen_dcread(DisasContext *ctx)
6223 {
6224 #if defined(CONFIG_USER_ONLY)
6225     gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
6226 #else
6227     TCGv EA, val;
6228     if (unlikely(ctx->pr)) {
6229         gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
6230         return;
6231     }
6232     gen_set_access_type(ctx, ACCESS_CACHE);
6233     EA = tcg_temp_new();
6234     gen_addr_reg_index(ctx, EA);
6235     val = tcg_temp_new();
6236     gen_qemu_ld32u(ctx, val, EA);
6237     tcg_temp_free(val);
6238     tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], EA);
6239     tcg_temp_free(EA);
6240 #endif
6241 }
6242
6243 /* icbt */
6244 static void gen_icbt_40x(DisasContext *ctx)
6245 {
6246     /* interpreted as no-op */
6247     /* XXX: specification say this is treated as a load by the MMU
6248      *      but does not generate any exception
6249      */
6250 }
6251
6252 /* iccci */
6253 static void gen_iccci(DisasContext *ctx)
6254 {
6255 #if defined(CONFIG_USER_ONLY)
6256     gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
6257 #else
6258     if (unlikely(ctx->pr)) {
6259         gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
6260         return;
6261     }
6262     /* interpreted as no-op */
6263 #endif
6264 }
6265
6266 /* icread */
6267 static void gen_icread(DisasContext *ctx)
6268 {
6269 #if defined(CONFIG_USER_ONLY)
6270     gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
6271 #else
6272     if (unlikely(ctx->pr)) {
6273         gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
6274         return;
6275     }
6276     /* interpreted as no-op */
6277 #endif
6278 }
6279
6280 /* rfci (supervisor only) */
6281 static void gen_rfci_40x(DisasContext *ctx)
6282 {
6283 #if defined(CONFIG_USER_ONLY)
6284     gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
6285 #else
6286     if (unlikely(ctx->pr)) {
6287         gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
6288         return;
6289     }
6290     /* Restore CPU state */
6291     gen_helper_40x_rfci(cpu_env);
6292     gen_sync_exception(ctx);
6293 #endif
6294 }
6295
6296 static void gen_rfci(DisasContext *ctx)
6297 {
6298 #if defined(CONFIG_USER_ONLY)
6299     gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
6300 #else
6301     if (unlikely(ctx->pr)) {
6302         gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
6303         return;
6304     }
6305     /* Restore CPU state */
6306     gen_helper_rfci(cpu_env);
6307     gen_sync_exception(ctx);
6308 #endif
6309 }
6310
6311 /* BookE specific */
6312
6313 /* XXX: not implemented on 440 ? */
6314 static void gen_rfdi(DisasContext *ctx)
6315 {
6316 #if defined(CONFIG_USER_ONLY)
6317     gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
6318 #else
6319     if (unlikely(ctx->pr)) {
6320         gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
6321         return;
6322     }
6323     /* Restore CPU state */
6324     gen_helper_rfdi(cpu_env);
6325     gen_sync_exception(ctx);
6326 #endif
6327 }
6328
6329 /* XXX: not implemented on 440 ? */
6330 static void gen_rfmci(DisasContext *ctx)
6331 {
6332 #if defined(CONFIG_USER_ONLY)
6333     gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
6334 #else
6335     if (unlikely(ctx->pr)) {
6336         gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
6337         return;
6338     }
6339     /* Restore CPU state */
6340     gen_helper_rfmci(cpu_env);
6341     gen_sync_exception(ctx);
6342 #endif
6343 }
6344
6345 /* TLB management - PowerPC 405 implementation */
6346
6347 /* tlbre */
6348 static void gen_tlbre_40x(DisasContext *ctx)
6349 {
6350 #if defined(CONFIG_USER_ONLY)
6351     gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
6352 #else
6353     if (unlikely(ctx->pr)) {
6354         gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
6355         return;
6356     }
6357     switch (rB(ctx->opcode)) {
6358     case 0:
6359         gen_helper_4xx_tlbre_hi(cpu_gpr[rD(ctx->opcode)], cpu_env,
6360                                 cpu_gpr[rA(ctx->opcode)]);
6361         break;
6362     case 1:
6363         gen_helper_4xx_tlbre_lo(cpu_gpr[rD(ctx->opcode)], cpu_env,
6364                                 cpu_gpr[rA(ctx->opcode)]);
6365         break;
6366     default:
6367         gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL);
6368         break;
6369     }
6370 #endif
6371 }
6372
6373 /* tlbsx - tlbsx. */
6374 static void gen_tlbsx_40x(DisasContext *ctx)
6375 {
6376 #if defined(CONFIG_USER_ONLY)
6377     gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
6378 #else
6379     TCGv t0;
6380     if (unlikely(ctx->pr)) {
6381         gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
6382         return;
6383     }
6384     t0 = tcg_temp_new();
6385     gen_addr_reg_index(ctx, t0);
6386     gen_helper_4xx_tlbsx(cpu_gpr[rD(ctx->opcode)], cpu_env, t0);
6387     tcg_temp_free(t0);
6388     if (Rc(ctx->opcode)) {
6389         int l1 = gen_new_label();
6390         tcg_gen_trunc_tl_i32(cpu_crf[0], cpu_so);
6391         tcg_gen_brcondi_tl(TCG_COND_EQ, cpu_gpr[rD(ctx->opcode)], -1, l1);
6392         tcg_gen_ori_i32(cpu_crf[0], cpu_crf[0], 0x02);
6393         gen_set_label(l1);
6394     }
6395 #endif
6396 }
6397
6398 /* tlbwe */
6399 static void gen_tlbwe_40x(DisasContext *ctx)
6400 {
6401 #if defined(CONFIG_USER_ONLY)
6402     gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
6403 #else
6404     if (unlikely(ctx->pr)) {
6405         gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
6406         return;
6407     }
6408     switch (rB(ctx->opcode)) {
6409     case 0:
6410         gen_helper_4xx_tlbwe_hi(cpu_env, cpu_gpr[rA(ctx->opcode)],
6411                                 cpu_gpr[rS(ctx->opcode)]);
6412         break;
6413     case 1:
6414         gen_helper_4xx_tlbwe_lo(cpu_env, cpu_gpr[rA(ctx->opcode)],
6415                                 cpu_gpr[rS(ctx->opcode)]);
6416         break;
6417     default:
6418         gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL);
6419         break;
6420     }
6421 #endif
6422 }
6423
6424 /* TLB management - PowerPC 440 implementation */
6425
6426 /* tlbre */
6427 static void gen_tlbre_440(DisasContext *ctx)
6428 {
6429 #if defined(CONFIG_USER_ONLY)
6430     gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
6431 #else
6432     if (unlikely(ctx->pr)) {
6433         gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
6434         return;
6435     }
6436     switch (rB(ctx->opcode)) {
6437     case 0:
6438     case 1:
6439     case 2:
6440         {
6441             TCGv_i32 t0 = tcg_const_i32(rB(ctx->opcode));
6442             gen_helper_440_tlbre(cpu_gpr[rD(ctx->opcode)], cpu_env,
6443                                  t0, cpu_gpr[rA(ctx->opcode)]);
6444             tcg_temp_free_i32(t0);
6445         }
6446         break;
6447     default:
6448         gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL);
6449         break;
6450     }
6451 #endif
6452 }
6453
6454 /* tlbsx - tlbsx. */
6455 static void gen_tlbsx_440(DisasContext *ctx)
6456 {
6457 #if defined(CONFIG_USER_ONLY)
6458     gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
6459 #else
6460     TCGv t0;
6461     if (unlikely(ctx->pr)) {
6462         gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
6463         return;
6464     }
6465     t0 = tcg_temp_new();
6466     gen_addr_reg_index(ctx, t0);
6467     gen_helper_440_tlbsx(cpu_gpr[rD(ctx->opcode)], cpu_env, t0);
6468     tcg_temp_free(t0);
6469     if (Rc(ctx->opcode)) {
6470         int l1 = gen_new_label();
6471         tcg_gen_trunc_tl_i32(cpu_crf[0], cpu_so);
6472         tcg_gen_brcondi_tl(TCG_COND_EQ, cpu_gpr[rD(ctx->opcode)], -1, l1);
6473         tcg_gen_ori_i32(cpu_crf[0], cpu_crf[0], 0x02);
6474         gen_set_label(l1);
6475     }
6476 #endif
6477 }
6478
6479 /* tlbwe */
6480 static void gen_tlbwe_440(DisasContext *ctx)
6481 {
6482 #if defined(CONFIG_USER_ONLY)
6483     gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
6484 #else
6485     if (unlikely(ctx->pr)) {
6486         gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
6487         return;
6488     }
6489     switch (rB(ctx->opcode)) {
6490     case 0:
6491     case 1:
6492     case 2:
6493         {
6494             TCGv_i32 t0 = tcg_const_i32(rB(ctx->opcode));
6495             gen_helper_440_tlbwe(cpu_env, t0, cpu_gpr[rA(ctx->opcode)],
6496                                  cpu_gpr[rS(ctx->opcode)]);
6497             tcg_temp_free_i32(t0);
6498         }
6499         break;
6500     default:
6501         gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL);
6502         break;
6503     }
6504 #endif
6505 }
6506
6507 /* TLB management - PowerPC BookE 2.06 implementation */
6508
6509 /* tlbre */
6510 static void gen_tlbre_booke206(DisasContext *ctx)
6511 {
6512 #if defined(CONFIG_USER_ONLY)
6513     gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
6514 #else
6515     if (unlikely(ctx->pr)) {
6516         gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
6517         return;
6518     }
6519
6520     gen_helper_booke206_tlbre(cpu_env);
6521 #endif
6522 }
6523
6524 /* tlbsx - tlbsx. */
6525 static void gen_tlbsx_booke206(DisasContext *ctx)
6526 {
6527 #if defined(CONFIG_USER_ONLY)
6528     gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
6529 #else
6530     TCGv t0;
6531     if (unlikely(ctx->pr)) {
6532         gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
6533         return;
6534     }
6535
6536     if (rA(ctx->opcode)) {
6537         t0 = tcg_temp_new();
6538         tcg_gen_mov_tl(t0, cpu_gpr[rD(ctx->opcode)]);
6539     } else {
6540         t0 = tcg_const_tl(0);
6541     }
6542
6543     tcg_gen_add_tl(t0, t0, cpu_gpr[rB(ctx->opcode)]);
6544     gen_helper_booke206_tlbsx(cpu_env, t0);
6545     tcg_temp_free(t0);
6546 #endif
6547 }
6548
6549 /* tlbwe */
6550 static void gen_tlbwe_booke206(DisasContext *ctx)
6551 {
6552 #if defined(CONFIG_USER_ONLY)
6553     gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
6554 #else
6555     if (unlikely(ctx->pr)) {
6556         gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
6557         return;
6558     }
6559     gen_update_nip(ctx, ctx->nip - 4);
6560     gen_helper_booke206_tlbwe(cpu_env);
6561 #endif
6562 }
6563
6564 static void gen_tlbivax_booke206(DisasContext *ctx)
6565 {
6566 #if defined(CONFIG_USER_ONLY)
6567     gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
6568 #else
6569     TCGv t0;
6570     if (unlikely(ctx->pr)) {
6571         gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
6572         return;
6573     }
6574
6575     t0 = tcg_temp_new();
6576     gen_addr_reg_index(ctx, t0);
6577
6578     gen_helper_booke206_tlbivax(cpu_env, t0);
6579     tcg_temp_free(t0);
6580 #endif
6581 }
6582
6583 static void gen_tlbilx_booke206(DisasContext *ctx)
6584 {
6585 #if defined(CONFIG_USER_ONLY)
6586     gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
6587 #else
6588     TCGv t0;
6589     if (unlikely(ctx->pr)) {
6590         gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
6591         return;
6592     }
6593
6594     t0 = tcg_temp_new();
6595     gen_addr_reg_index(ctx, t0);
6596
6597     switch((ctx->opcode >> 21) & 0x3) {
6598     case 0:
6599         gen_helper_booke206_tlbilx0(cpu_env, t0);
6600         break;
6601     case 1:
6602         gen_helper_booke206_tlbilx1(cpu_env, t0);
6603         break;
6604     case 3:
6605         gen_helper_booke206_tlbilx3(cpu_env, t0);
6606         break;
6607     default:
6608         gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL);
6609         break;
6610     }
6611
6612     tcg_temp_free(t0);
6613 #endif
6614 }
6615
6616
6617 /* wrtee */
6618 static void gen_wrtee(DisasContext *ctx)
6619 {
6620 #if defined(CONFIG_USER_ONLY)
6621     gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
6622 #else
6623     TCGv t0;
6624     if (unlikely(ctx->pr)) {
6625         gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
6626         return;
6627     }
6628     t0 = tcg_temp_new();
6629     tcg_gen_andi_tl(t0, cpu_gpr[rD(ctx->opcode)], (1 << MSR_EE));
6630     tcg_gen_andi_tl(cpu_msr, cpu_msr, ~(1 << MSR_EE));
6631     tcg_gen_or_tl(cpu_msr, cpu_msr, t0);
6632     tcg_temp_free(t0);
6633     /* Stop translation to have a chance to raise an exception
6634      * if we just set msr_ee to 1
6635      */
6636     gen_stop_exception(ctx);
6637 #endif
6638 }
6639
6640 /* wrteei */
6641 static void gen_wrteei(DisasContext *ctx)
6642 {
6643 #if defined(CONFIG_USER_ONLY)
6644     gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
6645 #else
6646     if (unlikely(ctx->pr)) {
6647         gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
6648         return;
6649     }
6650     if (ctx->opcode & 0x00008000) {
6651         tcg_gen_ori_tl(cpu_msr, cpu_msr, (1 << MSR_EE));
6652         /* Stop translation to have a chance to raise an exception */
6653         gen_stop_exception(ctx);
6654     } else {
6655         tcg_gen_andi_tl(cpu_msr, cpu_msr, ~(1 << MSR_EE));
6656     }
6657 #endif
6658 }
6659
6660 /* PowerPC 440 specific instructions */
6661
6662 /* dlmzb */
6663 static void gen_dlmzb(DisasContext *ctx)
6664 {
6665     TCGv_i32 t0 = tcg_const_i32(Rc(ctx->opcode));
6666     gen_helper_dlmzb(cpu_gpr[rA(ctx->opcode)], cpu_env,
6667                      cpu_gpr[rS(ctx->opcode)], cpu_gpr[rB(ctx->opcode)], t0);
6668     tcg_temp_free_i32(t0);
6669 }
6670
6671 /* mbar replaces eieio on 440 */
6672 static void gen_mbar(DisasContext *ctx)
6673 {
6674     /* interpreted as no-op */
6675 }
6676
6677 /* msync replaces sync on 440 */
6678 static void gen_msync_4xx(DisasContext *ctx)
6679 {
6680     /* interpreted as no-op */
6681 }
6682
6683 /* icbt */
6684 static void gen_icbt_440(DisasContext *ctx)
6685 {
6686     /* interpreted as no-op */
6687     /* XXX: specification say this is treated as a load by the MMU
6688      *      but does not generate any exception
6689      */
6690 }
6691
6692 /* Embedded.Processor Control */
6693
6694 static void gen_msgclr(DisasContext *ctx)
6695 {
6696 #if defined(CONFIG_USER_ONLY)
6697     gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
6698 #else
6699     if (unlikely(ctx->pr)) {
6700         gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
6701         return;
6702     }
6703
6704     gen_helper_msgclr(cpu_env, cpu_gpr[rB(ctx->opcode)]);
6705 #endif
6706 }
6707
6708 static void gen_msgsnd(DisasContext *ctx)
6709 {
6710 #if defined(CONFIG_USER_ONLY)
6711     gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
6712 #else
6713     if (unlikely(ctx->pr)) {
6714         gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
6715         return;
6716     }
6717
6718     gen_helper_msgsnd(cpu_gpr[rB(ctx->opcode)]);
6719 #endif
6720 }
6721
6722 /***                      Altivec vector extension                         ***/
6723 /* Altivec registers moves */
6724
6725 static inline TCGv_ptr gen_avr_ptr(int reg)
6726 {
6727     TCGv_ptr r = tcg_temp_new_ptr();
6728     tcg_gen_addi_ptr(r, cpu_env, offsetof(CPUPPCState, avr[reg]));
6729     return r;
6730 }
6731
6732 #define GEN_VR_LDX(name, opc2, opc3)                                          \
6733 static void glue(gen_, name)(DisasContext *ctx)                                       \
6734 {                                                                             \
6735     TCGv EA;                                                                  \
6736     if (unlikely(!ctx->altivec_enabled)) {                                    \
6737         gen_exception(ctx, POWERPC_EXCP_VPU);                                 \
6738         return;                                                               \
6739     }                                                                         \
6740     gen_set_access_type(ctx, ACCESS_INT);                                     \
6741     EA = tcg_temp_new();                                                      \
6742     gen_addr_reg_index(ctx, EA);                                              \
6743     tcg_gen_andi_tl(EA, EA, ~0xf);                                            \
6744     /* We only need to swap high and low halves. gen_qemu_ld64 does necessary \
6745        64-bit byteswap already. */                                            \
6746     if (ctx->le_mode) {                                                       \
6747         gen_qemu_ld64(ctx, cpu_avrl[rD(ctx->opcode)], EA);                    \
6748         tcg_gen_addi_tl(EA, EA, 8);                                           \
6749         gen_qemu_ld64(ctx, cpu_avrh[rD(ctx->opcode)], EA);                    \
6750     } else {                                                                  \
6751         gen_qemu_ld64(ctx, cpu_avrh[rD(ctx->opcode)], EA);                    \
6752         tcg_gen_addi_tl(EA, EA, 8);                                           \
6753         gen_qemu_ld64(ctx, cpu_avrl[rD(ctx->opcode)], EA);                    \
6754     }                                                                         \
6755     tcg_temp_free(EA);                                                        \
6756 }
6757
6758 #define GEN_VR_STX(name, opc2, opc3)                                          \
6759 static void gen_st##name(DisasContext *ctx)                                   \
6760 {                                                                             \
6761     TCGv EA;                                                                  \
6762     if (unlikely(!ctx->altivec_enabled)) {                                    \
6763         gen_exception(ctx, POWERPC_EXCP_VPU);                                 \
6764         return;                                                               \
6765     }                                                                         \
6766     gen_set_access_type(ctx, ACCESS_INT);                                     \
6767     EA = tcg_temp_new();                                                      \
6768     gen_addr_reg_index(ctx, EA);                                              \
6769     tcg_gen_andi_tl(EA, EA, ~0xf);                                            \
6770     /* We only need to swap high and low halves. gen_qemu_st64 does necessary \
6771        64-bit byteswap already. */                                            \
6772     if (ctx->le_mode) {                                                       \
6773         gen_qemu_st64(ctx, cpu_avrl[rD(ctx->opcode)], EA);                    \
6774         tcg_gen_addi_tl(EA, EA, 8);                                           \
6775         gen_qemu_st64(ctx, cpu_avrh[rD(ctx->opcode)], EA);                    \
6776     } else {                                                                  \
6777         gen_qemu_st64(ctx, cpu_avrh[rD(ctx->opcode)], EA);                    \
6778         tcg_gen_addi_tl(EA, EA, 8);                                           \
6779         gen_qemu_st64(ctx, cpu_avrl[rD(ctx->opcode)], EA);                    \
6780     }                                                                         \
6781     tcg_temp_free(EA);                                                        \
6782 }
6783
6784 #define GEN_VR_LVE(name, opc2, opc3, size)                              \
6785 static void gen_lve##name(DisasContext *ctx)                            \
6786     {                                                                   \
6787         TCGv EA;                                                        \
6788         TCGv_ptr rs;                                                    \
6789         if (unlikely(!ctx->altivec_enabled)) {                          \
6790             gen_exception(ctx, POWERPC_EXCP_VPU);                       \
6791             return;                                                     \
6792         }                                                               \
6793         gen_set_access_type(ctx, ACCESS_INT);                           \
6794         EA = tcg_temp_new();                                            \
6795         gen_addr_reg_index(ctx, EA);                                    \
6796         if (size > 1) {                                                 \
6797             tcg_gen_andi_tl(EA, EA, ~(size - 1));                       \
6798         }                                                               \
6799         rs = gen_avr_ptr(rS(ctx->opcode));                              \
6800         gen_helper_lve##name(cpu_env, rs, EA);                          \
6801         tcg_temp_free(EA);                                              \
6802         tcg_temp_free_ptr(rs);                                          \
6803     }
6804
6805 #define GEN_VR_STVE(name, opc2, opc3, size)                             \
6806 static void gen_stve##name(DisasContext *ctx)                           \
6807     {                                                                   \
6808         TCGv EA;                                                        \
6809         TCGv_ptr rs;                                                    \
6810         if (unlikely(!ctx->altivec_enabled)) {                          \
6811             gen_exception(ctx, POWERPC_EXCP_VPU);                       \
6812             return;                                                     \
6813         }                                                               \
6814         gen_set_access_type(ctx, ACCESS_INT);                           \
6815         EA = tcg_temp_new();                                            \
6816         gen_addr_reg_index(ctx, EA);                                    \
6817         if (size > 1) {                                                 \
6818             tcg_gen_andi_tl(EA, EA, ~(size - 1));                       \
6819         }                                                               \
6820         rs = gen_avr_ptr(rS(ctx->opcode));                              \
6821         gen_helper_stve##name(cpu_env, rs, EA);                         \
6822         tcg_temp_free(EA);                                              \
6823         tcg_temp_free_ptr(rs);                                          \
6824     }
6825
6826 GEN_VR_LDX(lvx, 0x07, 0x03);
6827 /* As we don't emulate the cache, lvxl is stricly equivalent to lvx */
6828 GEN_VR_LDX(lvxl, 0x07, 0x0B);
6829
6830 GEN_VR_LVE(bx, 0x07, 0x00, 1);
6831 GEN_VR_LVE(hx, 0x07, 0x01, 2);
6832 GEN_VR_LVE(wx, 0x07, 0x02, 4);
6833
6834 GEN_VR_STX(svx, 0x07, 0x07);
6835 /* As we don't emulate the cache, stvxl is stricly equivalent to stvx */
6836 GEN_VR_STX(svxl, 0x07, 0x0F);
6837
6838 GEN_VR_STVE(bx, 0x07, 0x04, 1);
6839 GEN_VR_STVE(hx, 0x07, 0x05, 2);
6840 GEN_VR_STVE(wx, 0x07, 0x06, 4);
6841
6842 static void gen_lvsl(DisasContext *ctx)
6843 {
6844     TCGv_ptr rd;
6845     TCGv EA;
6846     if (unlikely(!ctx->altivec_enabled)) {
6847         gen_exception(ctx, POWERPC_EXCP_VPU);
6848         return;
6849     }
6850     EA = tcg_temp_new();
6851     gen_addr_reg_index(ctx, EA);
6852     rd = gen_avr_ptr(rD(ctx->opcode));
6853     gen_helper_lvsl(rd, EA);
6854     tcg_temp_free(EA);
6855     tcg_temp_free_ptr(rd);
6856 }
6857
6858 static void gen_lvsr(DisasContext *ctx)
6859 {
6860     TCGv_ptr rd;
6861     TCGv EA;
6862     if (unlikely(!ctx->altivec_enabled)) {
6863         gen_exception(ctx, POWERPC_EXCP_VPU);
6864         return;
6865     }
6866     EA = tcg_temp_new();
6867     gen_addr_reg_index(ctx, EA);
6868     rd = gen_avr_ptr(rD(ctx->opcode));
6869     gen_helper_lvsr(rd, EA);
6870     tcg_temp_free(EA);
6871     tcg_temp_free_ptr(rd);
6872 }
6873
6874 static void gen_mfvscr(DisasContext *ctx)
6875 {
6876     TCGv_i32 t;
6877     if (unlikely(!ctx->altivec_enabled)) {
6878         gen_exception(ctx, POWERPC_EXCP_VPU);
6879         return;
6880     }
6881     tcg_gen_movi_i64(cpu_avrh[rD(ctx->opcode)], 0);
6882     t = tcg_temp_new_i32();
6883     tcg_gen_ld_i32(t, cpu_env, offsetof(CPUPPCState, vscr));
6884     tcg_gen_extu_i32_i64(cpu_avrl[rD(ctx->opcode)], t);
6885     tcg_temp_free_i32(t);
6886 }
6887
6888 static void gen_mtvscr(DisasContext *ctx)
6889 {
6890     TCGv_ptr p;
6891     if (unlikely(!ctx->altivec_enabled)) {
6892         gen_exception(ctx, POWERPC_EXCP_VPU);
6893         return;
6894     }
6895     p = gen_avr_ptr(rB(ctx->opcode));
6896     gen_helper_mtvscr(cpu_env, p);
6897     tcg_temp_free_ptr(p);
6898 }
6899
6900 /* Logical operations */
6901 #define GEN_VX_LOGICAL(name, tcg_op, opc2, opc3)                        \
6902 static void glue(gen_, name)(DisasContext *ctx)                                 \
6903 {                                                                       \
6904     if (unlikely(!ctx->altivec_enabled)) {                              \
6905         gen_exception(ctx, POWERPC_EXCP_VPU);                           \
6906         return;                                                         \
6907     }                                                                   \
6908     tcg_op(cpu_avrh[rD(ctx->opcode)], cpu_avrh[rA(ctx->opcode)], cpu_avrh[rB(ctx->opcode)]); \
6909     tcg_op(cpu_avrl[rD(ctx->opcode)], cpu_avrl[rA(ctx->opcode)], cpu_avrl[rB(ctx->opcode)]); \
6910 }
6911
6912 GEN_VX_LOGICAL(vand, tcg_gen_and_i64, 2, 16);
6913 GEN_VX_LOGICAL(vandc, tcg_gen_andc_i64, 2, 17);
6914 GEN_VX_LOGICAL(vor, tcg_gen_or_i64, 2, 18);
6915 GEN_VX_LOGICAL(vxor, tcg_gen_xor_i64, 2, 19);
6916 GEN_VX_LOGICAL(vnor, tcg_gen_nor_i64, 2, 20);
6917 GEN_VX_LOGICAL(veqv, tcg_gen_eqv_i64, 2, 26);
6918 GEN_VX_LOGICAL(vnand, tcg_gen_nand_i64, 2, 22);
6919 GEN_VX_LOGICAL(vorc, tcg_gen_orc_i64, 2, 21);
6920
6921 #define GEN_VXFORM(name, opc2, opc3)                                    \
6922 static void glue(gen_, name)(DisasContext *ctx)                                 \
6923 {                                                                       \
6924     TCGv_ptr ra, rb, rd;                                                \
6925     if (unlikely(!ctx->altivec_enabled)) {                              \
6926         gen_exception(ctx, POWERPC_EXCP_VPU);                           \
6927         return;                                                         \
6928     }                                                                   \
6929     ra = gen_avr_ptr(rA(ctx->opcode));                                  \
6930     rb = gen_avr_ptr(rB(ctx->opcode));                                  \
6931     rd = gen_avr_ptr(rD(ctx->opcode));                                  \
6932     gen_helper_##name (rd, ra, rb);                                     \
6933     tcg_temp_free_ptr(ra);                                              \
6934     tcg_temp_free_ptr(rb);                                              \
6935     tcg_temp_free_ptr(rd);                                              \
6936 }
6937
6938 #define GEN_VXFORM_ENV(name, opc2, opc3)                                \
6939 static void glue(gen_, name)(DisasContext *ctx)                         \
6940 {                                                                       \
6941     TCGv_ptr ra, rb, rd;                                                \
6942     if (unlikely(!ctx->altivec_enabled)) {                              \
6943         gen_exception(ctx, POWERPC_EXCP_VPU);                           \
6944         return;                                                         \
6945     }                                                                   \
6946     ra = gen_avr_ptr(rA(ctx->opcode));                                  \
6947     rb = gen_avr_ptr(rB(ctx->opcode));                                  \
6948     rd = gen_avr_ptr(rD(ctx->opcode));                                  \
6949     gen_helper_##name(cpu_env, rd, ra, rb);                             \
6950     tcg_temp_free_ptr(ra);                                              \
6951     tcg_temp_free_ptr(rb);                                              \
6952     tcg_temp_free_ptr(rd);                                              \
6953 }
6954
6955 #define GEN_VXFORM3(name, opc2, opc3)                                   \
6956 static void glue(gen_, name)(DisasContext *ctx)                         \
6957 {                                                                       \
6958     TCGv_ptr ra, rb, rc, rd;                                            \
6959     if (unlikely(!ctx->altivec_enabled)) {                              \
6960         gen_exception(ctx, POWERPC_EXCP_VPU);                           \
6961         return;                                                         \
6962     }                                                                   \
6963     ra = gen_avr_ptr(rA(ctx->opcode));                                  \
6964     rb = gen_avr_ptr(rB(ctx->opcode));                                  \
6965     rc = gen_avr_ptr(rC(ctx->opcode));                                  \
6966     rd = gen_avr_ptr(rD(ctx->opcode));                                  \
6967     gen_helper_##name(rd, ra, rb, rc);                                  \
6968     tcg_temp_free_ptr(ra);                                              \
6969     tcg_temp_free_ptr(rb);                                              \
6970     tcg_temp_free_ptr(rc);                                              \
6971     tcg_temp_free_ptr(rd);                                              \
6972 }
6973
6974 /*
6975  * Support for Altivec instruction pairs that use bit 31 (Rc) as
6976  * an opcode bit.  In general, these pairs come from different
6977  * versions of the ISA, so we must also support a pair of flags for
6978  * each instruction.
6979  */
6980 #define GEN_VXFORM_DUAL(name0, flg0, flg2_0, name1, flg1, flg2_1)          \
6981 static void glue(gen_, name0##_##name1)(DisasContext *ctx)             \
6982 {                                                                      \
6983     if ((Rc(ctx->opcode) == 0) &&                                      \
6984         ((ctx->insns_flags & flg0) || (ctx->insns_flags2 & flg2_0))) { \
6985         gen_##name0(ctx);                                              \
6986     } else if ((Rc(ctx->opcode) == 1) &&                               \
6987         ((ctx->insns_flags & flg1) || (ctx->insns_flags2 & flg2_1))) { \
6988         gen_##name1(ctx);                                              \
6989     } else {                                                           \
6990         gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL);            \
6991     }                                                                  \
6992 }
6993
6994 GEN_VXFORM(vaddubm, 0, 0);
6995 GEN_VXFORM(vadduhm, 0, 1);
6996 GEN_VXFORM(vadduwm, 0, 2);
6997 GEN_VXFORM(vaddudm, 0, 3);
6998 GEN_VXFORM(vsububm, 0, 16);
6999 GEN_VXFORM(vsubuhm, 0, 17);
7000 GEN_VXFORM(vsubuwm, 0, 18);
7001 GEN_VXFORM(vsubudm, 0, 19);
7002 GEN_VXFORM(vmaxub, 1, 0);
7003 GEN_VXFORM(vmaxuh, 1, 1);
7004 GEN_VXFORM(vmaxuw, 1, 2);
7005 GEN_VXFORM(vmaxud, 1, 3);
7006 GEN_VXFORM(vmaxsb, 1, 4);
7007 GEN_VXFORM(vmaxsh, 1, 5);
7008 GEN_VXFORM(vmaxsw, 1, 6);
7009 GEN_VXFORM(vmaxsd, 1, 7);
7010 GEN_VXFORM(vminub, 1, 8);
7011 GEN_VXFORM(vminuh, 1, 9);
7012 GEN_VXFORM(vminuw, 1, 10);
7013 GEN_VXFORM(vminud, 1, 11);
7014 GEN_VXFORM(vminsb, 1, 12);
7015 GEN_VXFORM(vminsh, 1, 13);
7016 GEN_VXFORM(vminsw, 1, 14);
7017 GEN_VXFORM(vminsd, 1, 15);
7018 GEN_VXFORM(vavgub, 1, 16);
7019 GEN_VXFORM(vavguh, 1, 17);
7020 GEN_VXFORM(vavguw, 1, 18);
7021 GEN_VXFORM(vavgsb, 1, 20);
7022 GEN_VXFORM(vavgsh, 1, 21);
7023 GEN_VXFORM(vavgsw, 1, 22);
7024 GEN_VXFORM(vmrghb, 6, 0);
7025 GEN_VXFORM(vmrghh, 6, 1);
7026 GEN_VXFORM(vmrghw, 6, 2);
7027 GEN_VXFORM(vmrglb, 6, 4);
7028 GEN_VXFORM(vmrglh, 6, 5);
7029 GEN_VXFORM(vmrglw, 6, 6);
7030
7031 static void gen_vmrgew(DisasContext *ctx)
7032 {
7033     TCGv_i64 tmp;
7034     int VT, VA, VB;
7035     if (unlikely(!ctx->altivec_enabled)) {
7036         gen_exception(ctx, POWERPC_EXCP_VPU);
7037         return;
7038     }
7039     VT = rD(ctx->opcode);
7040     VA = rA(ctx->opcode);
7041     VB = rB(ctx->opcode);
7042     tmp = tcg_temp_new_i64();
7043     tcg_gen_shri_i64(tmp, cpu_avrh[VB], 32);
7044     tcg_gen_deposit_i64(cpu_avrh[VT], cpu_avrh[VA], tmp, 0, 32);
7045     tcg_gen_shri_i64(tmp, cpu_avrl[VB], 32);
7046     tcg_gen_deposit_i64(cpu_avrl[VT], cpu_avrl[VA], tmp, 0, 32);
7047     tcg_temp_free_i64(tmp);
7048 }
7049
7050 static void gen_vmrgow(DisasContext *ctx)
7051 {
7052     int VT, VA, VB;
7053     if (unlikely(!ctx->altivec_enabled)) {
7054         gen_exception(ctx, POWERPC_EXCP_VPU);
7055         return;
7056     }
7057     VT = rD(ctx->opcode);
7058     VA = rA(ctx->opcode);
7059     VB = rB(ctx->opcode);
7060
7061     tcg_gen_deposit_i64(cpu_avrh[VT], cpu_avrh[VB], cpu_avrh[VA], 32, 32);
7062     tcg_gen_deposit_i64(cpu_avrl[VT], cpu_avrl[VB], cpu_avrl[VA], 32, 32);
7063 }
7064
7065 GEN_VXFORM(vmuloub, 4, 0);
7066 GEN_VXFORM(vmulouh, 4, 1);
7067 GEN_VXFORM(vmulouw, 4, 2);
7068 GEN_VXFORM(vmuluwm, 4, 2);
7069 GEN_VXFORM_DUAL(vmulouw, PPC_ALTIVEC, PPC_NONE,
7070                 vmuluwm, PPC_NONE, PPC2_ALTIVEC_207)
7071 GEN_VXFORM(vmulosb, 4, 4);
7072 GEN_VXFORM(vmulosh, 4, 5);
7073 GEN_VXFORM(vmulosw, 4, 6);
7074 GEN_VXFORM(vmuleub, 4, 8);
7075 GEN_VXFORM(vmuleuh, 4, 9);
7076 GEN_VXFORM(vmuleuw, 4, 10);
7077 GEN_VXFORM(vmulesb, 4, 12);
7078 GEN_VXFORM(vmulesh, 4, 13);
7079 GEN_VXFORM(vmulesw, 4, 14);
7080 GEN_VXFORM(vslb, 2, 4);
7081 GEN_VXFORM(vslh, 2, 5);
7082 GEN_VXFORM(vslw, 2, 6);
7083 GEN_VXFORM(vsld, 2, 23);
7084 GEN_VXFORM(vsrb, 2, 8);
7085 GEN_VXFORM(vsrh, 2, 9);
7086 GEN_VXFORM(vsrw, 2, 10);
7087 GEN_VXFORM(vsrd, 2, 27);
7088 GEN_VXFORM(vsrab, 2, 12);
7089 GEN_VXFORM(vsrah, 2, 13);
7090 GEN_VXFORM(vsraw, 2, 14);
7091 GEN_VXFORM(vsrad, 2, 15);
7092 GEN_VXFORM(vslo, 6, 16);
7093 GEN_VXFORM(vsro, 6, 17);
7094 GEN_VXFORM(vaddcuw, 0, 6);
7095 GEN_VXFORM(vsubcuw, 0, 22);
7096 GEN_VXFORM_ENV(vaddubs, 0, 8);
7097 GEN_VXFORM_ENV(vadduhs, 0, 9);
7098 GEN_VXFORM_ENV(vadduws, 0, 10);
7099 GEN_VXFORM_ENV(vaddsbs, 0, 12);
7100 GEN_VXFORM_ENV(vaddshs, 0, 13);
7101 GEN_VXFORM_ENV(vaddsws, 0, 14);
7102 GEN_VXFORM_ENV(vsububs, 0, 24);
7103 GEN_VXFORM_ENV(vsubuhs, 0, 25);
7104 GEN_VXFORM_ENV(vsubuws, 0, 26);
7105 GEN_VXFORM_ENV(vsubsbs, 0, 28);
7106 GEN_VXFORM_ENV(vsubshs, 0, 29);
7107 GEN_VXFORM_ENV(vsubsws, 0, 30);
7108 GEN_VXFORM(vadduqm, 0, 4);
7109 GEN_VXFORM(vaddcuq, 0, 5);
7110 GEN_VXFORM3(vaddeuqm, 30, 0);
7111 GEN_VXFORM3(vaddecuq, 30, 0);
7112 GEN_VXFORM_DUAL(vaddeuqm, PPC_NONE, PPC2_ALTIVEC_207, \
7113             vaddecuq, PPC_NONE, PPC2_ALTIVEC_207)
7114 GEN_VXFORM(vsubuqm, 0, 20);
7115 GEN_VXFORM(vsubcuq, 0, 21);
7116 GEN_VXFORM3(vsubeuqm, 31, 0);
7117 GEN_VXFORM3(vsubecuq, 31, 0);
7118 GEN_VXFORM_DUAL(vsubeuqm, PPC_NONE, PPC2_ALTIVEC_207, \
7119             vsubecuq, PPC_NONE, PPC2_ALTIVEC_207)
7120 GEN_VXFORM(vrlb, 2, 0);
7121 GEN_VXFORM(vrlh, 2, 1);
7122 GEN_VXFORM(vrlw, 2, 2);
7123 GEN_VXFORM(vrld, 2, 3);
7124 GEN_VXFORM(vsl, 2, 7);
7125 GEN_VXFORM(vsr, 2, 11);
7126 GEN_VXFORM_ENV(vpkuhum, 7, 0);
7127 GEN_VXFORM_ENV(vpkuwum, 7, 1);
7128 GEN_VXFORM_ENV(vpkudum, 7, 17);
7129 GEN_VXFORM_ENV(vpkuhus, 7, 2);
7130 GEN_VXFORM_ENV(vpkuwus, 7, 3);
7131 GEN_VXFORM_ENV(vpkudus, 7, 19);
7132 GEN_VXFORM_ENV(vpkshus, 7, 4);
7133 GEN_VXFORM_ENV(vpkswus, 7, 5);
7134 GEN_VXFORM_ENV(vpksdus, 7, 21);
7135 GEN_VXFORM_ENV(vpkshss, 7, 6);
7136 GEN_VXFORM_ENV(vpkswss, 7, 7);
7137 GEN_VXFORM_ENV(vpksdss, 7, 23);
7138 GEN_VXFORM(vpkpx, 7, 12);
7139 GEN_VXFORM_ENV(vsum4ubs, 4, 24);
7140 GEN_VXFORM_ENV(vsum4sbs, 4, 28);
7141 GEN_VXFORM_ENV(vsum4shs, 4, 25);
7142 GEN_VXFORM_ENV(vsum2sws, 4, 26);
7143 GEN_VXFORM_ENV(vsumsws, 4, 30);
7144 GEN_VXFORM_ENV(vaddfp, 5, 0);
7145 GEN_VXFORM_ENV(vsubfp, 5, 1);
7146 GEN_VXFORM_ENV(vmaxfp, 5, 16);
7147 GEN_VXFORM_ENV(vminfp, 5, 17);
7148
7149 #define GEN_VXRFORM1(opname, name, str, opc2, opc3)                     \
7150 static void glue(gen_, name)(DisasContext *ctx)                         \
7151     {                                                                   \
7152         TCGv_ptr ra, rb, rd;                                            \
7153         if (unlikely(!ctx->altivec_enabled)) {                          \
7154             gen_exception(ctx, POWERPC_EXCP_VPU);                       \
7155             return;                                                     \
7156         }                                                               \
7157         ra = gen_avr_ptr(rA(ctx->opcode));                              \
7158         rb = gen_avr_ptr(rB(ctx->opcode));                              \
7159         rd = gen_avr_ptr(rD(ctx->opcode));                              \
7160         gen_helper_##opname(cpu_env, rd, ra, rb);                       \
7161         tcg_temp_free_ptr(ra);                                          \
7162         tcg_temp_free_ptr(rb);                                          \
7163         tcg_temp_free_ptr(rd);                                          \
7164     }
7165
7166 #define GEN_VXRFORM(name, opc2, opc3)                                \
7167     GEN_VXRFORM1(name, name, #name, opc2, opc3)                      \
7168     GEN_VXRFORM1(name##_dot, name##_, #name ".", opc2, (opc3 | (0x1 << 4)))
7169
7170 /*
7171  * Support for Altivec instructions that use bit 31 (Rc) as an opcode
7172  * bit but also use bit 21 as an actual Rc bit.  In general, thse pairs
7173  * come from different versions of the ISA, so we must also support a
7174  * pair of flags for each instruction.
7175  */
7176 #define GEN_VXRFORM_DUAL(name0, flg0, flg2_0, name1, flg1, flg2_1)     \
7177 static void glue(gen_, name0##_##name1)(DisasContext *ctx)             \
7178 {                                                                      \
7179     if ((Rc(ctx->opcode) == 0) &&                                      \
7180         ((ctx->insns_flags & flg0) || (ctx->insns_flags2 & flg2_0))) { \
7181         if (Rc21(ctx->opcode) == 0) {                                  \
7182             gen_##name0(ctx);                                          \
7183         } else {                                                       \
7184             gen_##name0##_(ctx);                                       \
7185         }                                                              \
7186     } else if ((Rc(ctx->opcode) == 1) &&                               \
7187         ((ctx->insns_flags & flg1) || (ctx->insns_flags2 & flg2_1))) { \
7188         if (Rc21(ctx->opcode) == 0) {                                  \
7189             gen_##name1(ctx);                                          \
7190         } else {                                                       \
7191             gen_##name1##_(ctx);                                       \
7192         }                                                              \
7193     } else {                                                           \
7194         gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL);            \
7195     }                                                                  \
7196 }
7197
7198 GEN_VXRFORM(vcmpequb, 3, 0)
7199 GEN_VXRFORM(vcmpequh, 3, 1)
7200 GEN_VXRFORM(vcmpequw, 3, 2)
7201 GEN_VXRFORM(vcmpequd, 3, 3)
7202 GEN_VXRFORM(vcmpgtsb, 3, 12)
7203 GEN_VXRFORM(vcmpgtsh, 3, 13)
7204 GEN_VXRFORM(vcmpgtsw, 3, 14)
7205 GEN_VXRFORM(vcmpgtsd, 3, 15)
7206 GEN_VXRFORM(vcmpgtub, 3, 8)
7207 GEN_VXRFORM(vcmpgtuh, 3, 9)
7208 GEN_VXRFORM(vcmpgtuw, 3, 10)
7209 GEN_VXRFORM(vcmpgtud, 3, 11)
7210 GEN_VXRFORM(vcmpeqfp, 3, 3)
7211 GEN_VXRFORM(vcmpgefp, 3, 7)
7212 GEN_VXRFORM(vcmpgtfp, 3, 11)
7213 GEN_VXRFORM(vcmpbfp, 3, 15)
7214
7215 GEN_VXRFORM_DUAL(vcmpeqfp, PPC_ALTIVEC, PPC_NONE, \
7216                  vcmpequd, PPC_NONE, PPC2_ALTIVEC_207)
7217 GEN_VXRFORM_DUAL(vcmpbfp, PPC_ALTIVEC, PPC_NONE, \
7218                  vcmpgtsd, PPC_NONE, PPC2_ALTIVEC_207)
7219 GEN_VXRFORM_DUAL(vcmpgtfp, PPC_ALTIVEC, PPC_NONE, \
7220                  vcmpgtud, PPC_NONE, PPC2_ALTIVEC_207)
7221
7222 #define GEN_VXFORM_SIMM(name, opc2, opc3)                               \
7223 static void glue(gen_, name)(DisasContext *ctx)                         \
7224     {                                                                   \
7225         TCGv_ptr rd;                                                    \
7226         TCGv_i32 simm;                                                  \
7227         if (unlikely(!ctx->altivec_enabled)) {                          \
7228             gen_exception(ctx, POWERPC_EXCP_VPU);                       \
7229             return;                                                     \
7230         }                                                               \
7231         simm = tcg_const_i32(SIMM5(ctx->opcode));                       \
7232         rd = gen_avr_ptr(rD(ctx->opcode));                              \
7233         gen_helper_##name (rd, simm);                                   \
7234         tcg_temp_free_i32(simm);                                        \
7235         tcg_temp_free_ptr(rd);                                          \
7236     }
7237
7238 GEN_VXFORM_SIMM(vspltisb, 6, 12);
7239 GEN_VXFORM_SIMM(vspltish, 6, 13);
7240 GEN_VXFORM_SIMM(vspltisw, 6, 14);
7241
7242 #define GEN_VXFORM_NOA(name, opc2, opc3)                                \
7243 static void glue(gen_, name)(DisasContext *ctx)                                 \
7244     {                                                                   \
7245         TCGv_ptr rb, rd;                                                \
7246         if (unlikely(!ctx->altivec_enabled)) {                          \
7247             gen_exception(ctx, POWERPC_EXCP_VPU);                       \
7248             return;                                                     \
7249         }                                                               \
7250         rb = gen_avr_ptr(rB(ctx->opcode));                              \
7251         rd = gen_avr_ptr(rD(ctx->opcode));                              \
7252         gen_helper_##name (rd, rb);                                     \
7253         tcg_temp_free_ptr(rb);                                          \
7254         tcg_temp_free_ptr(rd);                                         \
7255     }
7256
7257 #define GEN_VXFORM_NOA_ENV(name, opc2, opc3)                            \
7258 static void glue(gen_, name)(DisasContext *ctx)                         \
7259     {                                                                   \
7260         TCGv_ptr rb, rd;                                                \
7261                                                                         \
7262         if (unlikely(!ctx->altivec_enabled)) {                          \
7263             gen_exception(ctx, POWERPC_EXCP_VPU);                       \
7264             return;                                                     \
7265         }                                                               \
7266         rb = gen_avr_ptr(rB(ctx->opcode));                              \
7267         rd = gen_avr_ptr(rD(ctx->opcode));                              \
7268         gen_helper_##name(cpu_env, rd, rb);                             \
7269         tcg_temp_free_ptr(rb);                                          \
7270         tcg_temp_free_ptr(rd);                                          \
7271     }
7272
7273 GEN_VXFORM_NOA(vupkhsb, 7, 8);
7274 GEN_VXFORM_NOA(vupkhsh, 7, 9);
7275 GEN_VXFORM_NOA(vupkhsw, 7, 25);
7276 GEN_VXFORM_NOA(vupklsb, 7, 10);
7277 GEN_VXFORM_NOA(vupklsh, 7, 11);
7278 GEN_VXFORM_NOA(vupklsw, 7, 27);
7279 GEN_VXFORM_NOA(vupkhpx, 7, 13);
7280 GEN_VXFORM_NOA(vupklpx, 7, 15);
7281 GEN_VXFORM_NOA_ENV(vrefp, 5, 4);
7282 GEN_VXFORM_NOA_ENV(vrsqrtefp, 5, 5);
7283 GEN_VXFORM_NOA_ENV(vexptefp, 5, 6);
7284 GEN_VXFORM_NOA_ENV(vlogefp, 5, 7);
7285 GEN_VXFORM_NOA_ENV(vrfim, 5, 11);
7286 GEN_VXFORM_NOA_ENV(vrfin, 5, 8);
7287 GEN_VXFORM_NOA_ENV(vrfip, 5, 10);
7288 GEN_VXFORM_NOA_ENV(vrfiz, 5, 9);
7289
7290 #define GEN_VXFORM_SIMM(name, opc2, opc3)                               \
7291 static void glue(gen_, name)(DisasContext *ctx)                                 \
7292     {                                                                   \
7293         TCGv_ptr rd;                                                    \
7294         TCGv_i32 simm;                                                  \
7295         if (unlikely(!ctx->altivec_enabled)) {                          \
7296             gen_exception(ctx, POWERPC_EXCP_VPU);                       \
7297             return;                                                     \
7298         }                                                               \
7299         simm = tcg_const_i32(SIMM5(ctx->opcode));                       \
7300         rd = gen_avr_ptr(rD(ctx->opcode));                              \
7301         gen_helper_##name (rd, simm);                                   \
7302         tcg_temp_free_i32(simm);                                        \
7303         tcg_temp_free_ptr(rd);                                          \
7304     }
7305
7306 #define GEN_VXFORM_UIMM(name, opc2, opc3)                               \
7307 static void glue(gen_, name)(DisasContext *ctx)                                 \
7308     {                                                                   \
7309         TCGv_ptr rb, rd;                                                \
7310         TCGv_i32 uimm;                                                  \
7311         if (unlikely(!ctx->altivec_enabled)) {                          \
7312             gen_exception(ctx, POWERPC_EXCP_VPU);                       \
7313             return;                                                     \
7314         }                                                               \
7315         uimm = tcg_const_i32(UIMM5(ctx->opcode));                       \
7316         rb = gen_avr_ptr(rB(ctx->opcode));                              \
7317         rd = gen_avr_ptr(rD(ctx->opcode));                              \
7318         gen_helper_##name (rd, rb, uimm);                               \
7319         tcg_temp_free_i32(uimm);                                        \
7320         tcg_temp_free_ptr(rb);                                          \
7321         tcg_temp_free_ptr(rd);                                          \
7322     }
7323
7324 #define GEN_VXFORM_UIMM_ENV(name, opc2, opc3)                           \
7325 static void glue(gen_, name)(DisasContext *ctx)                         \
7326     {                                                                   \
7327         TCGv_ptr rb, rd;                                                \
7328         TCGv_i32 uimm;                                                  \
7329                                                                         \
7330         if (unlikely(!ctx->altivec_enabled)) {                          \
7331             gen_exception(ctx, POWERPC_EXCP_VPU);                       \
7332             return;                                                     \
7333         }                                                               \
7334         uimm = tcg_const_i32(UIMM5(ctx->opcode));                       \
7335         rb = gen_avr_ptr(rB(ctx->opcode));                              \
7336         rd = gen_avr_ptr(rD(ctx->opcode));                              \
7337         gen_helper_##name(cpu_env, rd, rb, uimm);                       \
7338         tcg_temp_free_i32(uimm);                                        \
7339         tcg_temp_free_ptr(rb);                                          \
7340         tcg_temp_free_ptr(rd);                                          \
7341     }
7342
7343 GEN_VXFORM_UIMM(vspltb, 6, 8);
7344 GEN_VXFORM_UIMM(vsplth, 6, 9);
7345 GEN_VXFORM_UIMM(vspltw, 6, 10);
7346 GEN_VXFORM_UIMM_ENV(vcfux, 5, 12);
7347 GEN_VXFORM_UIMM_ENV(vcfsx, 5, 13);
7348 GEN_VXFORM_UIMM_ENV(vctuxs, 5, 14);
7349 GEN_VXFORM_UIMM_ENV(vctsxs, 5, 15);
7350
7351 static void gen_vsldoi(DisasContext *ctx)
7352 {
7353     TCGv_ptr ra, rb, rd;
7354     TCGv_i32 sh;
7355     if (unlikely(!ctx->altivec_enabled)) {
7356         gen_exception(ctx, POWERPC_EXCP_VPU);
7357         return;
7358     }
7359     ra = gen_avr_ptr(rA(ctx->opcode));
7360     rb = gen_avr_ptr(rB(ctx->opcode));
7361     rd = gen_avr_ptr(rD(ctx->opcode));
7362     sh = tcg_const_i32(VSH(ctx->opcode));
7363     gen_helper_vsldoi (rd, ra, rb, sh);
7364     tcg_temp_free_ptr(ra);
7365     tcg_temp_free_ptr(rb);
7366     tcg_temp_free_ptr(rd);
7367     tcg_temp_free_i32(sh);
7368 }
7369
7370 #define GEN_VAFORM_PAIRED(name0, name1, opc2)                           \
7371 static void glue(gen_, name0##_##name1)(DisasContext *ctx)              \
7372     {                                                                   \
7373         TCGv_ptr ra, rb, rc, rd;                                        \
7374         if (unlikely(!ctx->altivec_enabled)) {                          \
7375             gen_exception(ctx, POWERPC_EXCP_VPU);                       \
7376             return;                                                     \
7377         }                                                               \
7378         ra = gen_avr_ptr(rA(ctx->opcode));                              \
7379         rb = gen_avr_ptr(rB(ctx->opcode));                              \
7380         rc = gen_avr_ptr(rC(ctx->opcode));                              \
7381         rd = gen_avr_ptr(rD(ctx->opcode));                              \
7382         if (Rc(ctx->opcode)) {                                          \
7383             gen_helper_##name1(cpu_env, rd, ra, rb, rc);                \
7384         } else {                                                        \
7385             gen_helper_##name0(cpu_env, rd, ra, rb, rc);                \
7386         }                                                               \
7387         tcg_temp_free_ptr(ra);                                          \
7388         tcg_temp_free_ptr(rb);                                          \
7389         tcg_temp_free_ptr(rc);                                          \
7390         tcg_temp_free_ptr(rd);                                          \
7391     }
7392
7393 GEN_VAFORM_PAIRED(vmhaddshs, vmhraddshs, 16)
7394
7395 static void gen_vmladduhm(DisasContext *ctx)
7396 {
7397     TCGv_ptr ra, rb, rc, rd;
7398     if (unlikely(!ctx->altivec_enabled)) {
7399         gen_exception(ctx, POWERPC_EXCP_VPU);
7400         return;
7401     }
7402     ra = gen_avr_ptr(rA(ctx->opcode));
7403     rb = gen_avr_ptr(rB(ctx->opcode));
7404     rc = gen_avr_ptr(rC(ctx->opcode));
7405     rd = gen_avr_ptr(rD(ctx->opcode));
7406     gen_helper_vmladduhm(rd, ra, rb, rc);
7407     tcg_temp_free_ptr(ra);
7408     tcg_temp_free_ptr(rb);
7409     tcg_temp_free_ptr(rc);
7410     tcg_temp_free_ptr(rd);
7411 }
7412
7413 GEN_VAFORM_PAIRED(vmsumubm, vmsummbm, 18)
7414 GEN_VAFORM_PAIRED(vmsumuhm, vmsumuhs, 19)
7415 GEN_VAFORM_PAIRED(vmsumshm, vmsumshs, 20)
7416 GEN_VAFORM_PAIRED(vsel, vperm, 21)
7417 GEN_VAFORM_PAIRED(vmaddfp, vnmsubfp, 23)
7418
7419 GEN_VXFORM_NOA(vclzb, 1, 28)
7420 GEN_VXFORM_NOA(vclzh, 1, 29)
7421 GEN_VXFORM_NOA(vclzw, 1, 30)
7422 GEN_VXFORM_NOA(vclzd, 1, 31)
7423 GEN_VXFORM_NOA(vpopcntb, 1, 28)
7424 GEN_VXFORM_NOA(vpopcnth, 1, 29)
7425 GEN_VXFORM_NOA(vpopcntw, 1, 30)
7426 GEN_VXFORM_NOA(vpopcntd, 1, 31)
7427 GEN_VXFORM_DUAL(vclzb, PPC_NONE, PPC2_ALTIVEC_207, \
7428                 vpopcntb, PPC_NONE, PPC2_ALTIVEC_207)
7429 GEN_VXFORM_DUAL(vclzh, PPC_NONE, PPC2_ALTIVEC_207, \
7430                 vpopcnth, PPC_NONE, PPC2_ALTIVEC_207)
7431 GEN_VXFORM_DUAL(vclzw, PPC_NONE, PPC2_ALTIVEC_207, \
7432                 vpopcntw, PPC_NONE, PPC2_ALTIVEC_207)
7433 GEN_VXFORM_DUAL(vclzd, PPC_NONE, PPC2_ALTIVEC_207, \
7434                 vpopcntd, PPC_NONE, PPC2_ALTIVEC_207)
7435 GEN_VXFORM(vbpermq, 6, 21);
7436 GEN_VXFORM_NOA(vgbbd, 6, 20);
7437 GEN_VXFORM(vpmsumb, 4, 16)
7438 GEN_VXFORM(vpmsumh, 4, 17)
7439 GEN_VXFORM(vpmsumw, 4, 18)
7440 GEN_VXFORM(vpmsumd, 4, 19)
7441
7442 #define GEN_BCD(op)                                 \
7443 static void gen_##op(DisasContext *ctx)             \
7444 {                                                   \
7445     TCGv_ptr ra, rb, rd;                            \
7446     TCGv_i32 ps;                                    \
7447                                                     \
7448     if (unlikely(!ctx->altivec_enabled)) {          \
7449         gen_exception(ctx, POWERPC_EXCP_VPU);       \
7450         return;                                     \
7451     }                                               \
7452                                                     \
7453     ra = gen_avr_ptr(rA(ctx->opcode));              \
7454     rb = gen_avr_ptr(rB(ctx->opcode));              \
7455     rd = gen_avr_ptr(rD(ctx->opcode));              \
7456                                                     \
7457     ps = tcg_const_i32((ctx->opcode & 0x200) != 0); \
7458                                                     \
7459     gen_helper_##op(cpu_crf[6], rd, ra, rb, ps);    \
7460                                                     \
7461     tcg_temp_free_ptr(ra);                          \
7462     tcg_temp_free_ptr(rb);                          \
7463     tcg_temp_free_ptr(rd);                          \
7464     tcg_temp_free_i32(ps);                          \
7465 }
7466
7467 GEN_BCD(bcdadd)
7468 GEN_BCD(bcdsub)
7469
7470 GEN_VXFORM_DUAL(vsububm, PPC_ALTIVEC, PPC_NONE, \
7471                 bcdadd, PPC_NONE, PPC2_ALTIVEC_207)
7472 GEN_VXFORM_DUAL(vsububs, PPC_ALTIVEC, PPC_NONE, \
7473                 bcdadd, PPC_NONE, PPC2_ALTIVEC_207)
7474 GEN_VXFORM_DUAL(vsubuhm, PPC_ALTIVEC, PPC_NONE, \
7475                 bcdsub, PPC_NONE, PPC2_ALTIVEC_207)
7476 GEN_VXFORM_DUAL(vsubuhs, PPC_ALTIVEC, PPC_NONE, \
7477                 bcdsub, PPC_NONE, PPC2_ALTIVEC_207)
7478
7479 static void gen_vsbox(DisasContext *ctx)
7480 {
7481     TCGv_ptr ra, rd;
7482     if (unlikely(!ctx->altivec_enabled)) {
7483         gen_exception(ctx, POWERPC_EXCP_VPU);
7484         return;
7485     }
7486     ra = gen_avr_ptr(rA(ctx->opcode));
7487     rd = gen_avr_ptr(rD(ctx->opcode));
7488     gen_helper_vsbox(rd, ra);
7489     tcg_temp_free_ptr(ra);
7490     tcg_temp_free_ptr(rd);
7491 }
7492
7493 GEN_VXFORM(vcipher, 4, 20)
7494 GEN_VXFORM(vcipherlast, 4, 20)
7495 GEN_VXFORM(vncipher, 4, 21)
7496 GEN_VXFORM(vncipherlast, 4, 21)
7497
7498 GEN_VXFORM_DUAL(vcipher, PPC_NONE, PPC2_ALTIVEC_207,
7499                 vcipherlast, PPC_NONE, PPC2_ALTIVEC_207)
7500 GEN_VXFORM_DUAL(vncipher, PPC_NONE, PPC2_ALTIVEC_207,
7501                 vncipherlast, PPC_NONE, PPC2_ALTIVEC_207)
7502
7503 #define VSHASIGMA(op)                         \
7504 static void gen_##op(DisasContext *ctx)       \
7505 {                                             \
7506     TCGv_ptr ra, rd;                          \
7507     TCGv_i32 st_six;                          \
7508     if (unlikely(!ctx->altivec_enabled)) {    \
7509         gen_exception(ctx, POWERPC_EXCP_VPU); \
7510         return;                               \
7511     }                                         \
7512     ra = gen_avr_ptr(rA(ctx->opcode));        \
7513     rd = gen_avr_ptr(rD(ctx->opcode));        \
7514     st_six = tcg_const_i32(rB(ctx->opcode));  \
7515     gen_helper_##op(rd, ra, st_six);          \
7516     tcg_temp_free_ptr(ra);                    \
7517     tcg_temp_free_ptr(rd);                    \
7518     tcg_temp_free_i32(st_six);                \
7519 }
7520
7521 VSHASIGMA(vshasigmaw)
7522 VSHASIGMA(vshasigmad)
7523
7524 GEN_VXFORM3(vpermxor, 22, 0xFF)
7525 GEN_VXFORM_DUAL(vsldoi, PPC_ALTIVEC, PPC_NONE,
7526                 vpermxor, PPC_NONE, PPC2_ALTIVEC_207)
7527
7528 /***                           VSX extension                               ***/
7529
7530 static inline TCGv_i64 cpu_vsrh(int n)
7531 {
7532     if (n < 32) {
7533         return cpu_fpr[n];
7534     } else {
7535         return cpu_avrh[n-32];
7536     }
7537 }
7538
7539 static inline TCGv_i64 cpu_vsrl(int n)
7540 {
7541     if (n < 32) {
7542         return cpu_vsr[n];
7543     } else {
7544         return cpu_avrl[n-32];
7545     }
7546 }
7547
7548 #define VSX_LOAD_SCALAR(name, operation)                      \
7549 static void gen_##name(DisasContext *ctx)                     \
7550 {                                                             \
7551     TCGv EA;                                                  \
7552     if (unlikely(!ctx->vsx_enabled)) {                        \
7553         gen_exception(ctx, POWERPC_EXCP_VSXU);                \
7554         return;                                               \
7555     }                                                         \
7556     gen_set_access_type(ctx, ACCESS_INT);                     \
7557     EA = tcg_temp_new();                                      \
7558     gen_addr_reg_index(ctx, EA);                              \
7559     gen_qemu_##operation(ctx, cpu_vsrh(xT(ctx->opcode)), EA); \
7560     /* NOTE: cpu_vsrl is undefined */                         \
7561     tcg_temp_free(EA);                                        \
7562 }
7563
7564 VSX_LOAD_SCALAR(lxsdx, ld64)
7565 VSX_LOAD_SCALAR(lxsiwax, ld32s_i64)
7566 VSX_LOAD_SCALAR(lxsiwzx, ld32u_i64)
7567 VSX_LOAD_SCALAR(lxsspx, ld32fs)
7568
7569 static void gen_lxvd2x(DisasContext *ctx)
7570 {
7571     TCGv EA;
7572     if (unlikely(!ctx->vsx_enabled)) {
7573         gen_exception(ctx, POWERPC_EXCP_VSXU);
7574         return;
7575     }
7576     gen_set_access_type(ctx, ACCESS_INT);
7577     EA = tcg_temp_new();
7578     gen_addr_reg_index(ctx, EA);
7579     gen_qemu_ld64(ctx, cpu_vsrh(xT(ctx->opcode)), EA);
7580     tcg_gen_addi_tl(EA, EA, 8);
7581     gen_qemu_ld64(ctx, cpu_vsrl(xT(ctx->opcode)), EA);
7582     tcg_temp_free(EA);
7583 }
7584
7585 static void gen_lxvdsx(DisasContext *ctx)
7586 {
7587     TCGv EA;
7588     if (unlikely(!ctx->vsx_enabled)) {
7589         gen_exception(ctx, POWERPC_EXCP_VSXU);
7590         return;
7591     }
7592     gen_set_access_type(ctx, ACCESS_INT);
7593     EA = tcg_temp_new();
7594     gen_addr_reg_index(ctx, EA);
7595     gen_qemu_ld64(ctx, cpu_vsrh(xT(ctx->opcode)), EA);
7596     tcg_gen_mov_i64(cpu_vsrl(xT(ctx->opcode)), cpu_vsrh(xT(ctx->opcode)));
7597     tcg_temp_free(EA);
7598 }
7599
7600 static void gen_lxvw4x(DisasContext *ctx)
7601 {
7602     TCGv EA;
7603     TCGv_i64 tmp;
7604     TCGv_i64 xth = cpu_vsrh(xT(ctx->opcode));
7605     TCGv_i64 xtl = cpu_vsrl(xT(ctx->opcode));
7606     if (unlikely(!ctx->vsx_enabled)) {
7607         gen_exception(ctx, POWERPC_EXCP_VSXU);
7608         return;
7609     }
7610     gen_set_access_type(ctx, ACCESS_INT);
7611     EA = tcg_temp_new();
7612     tmp = tcg_temp_new_i64();
7613
7614     gen_addr_reg_index(ctx, EA);
7615     gen_qemu_ld32u_i64(ctx, tmp, EA);
7616     tcg_gen_addi_tl(EA, EA, 4);
7617     gen_qemu_ld32u_i64(ctx, xth, EA);
7618     tcg_gen_deposit_i64(xth, xth, tmp, 32, 32);
7619
7620     tcg_gen_addi_tl(EA, EA, 4);
7621     gen_qemu_ld32u_i64(ctx, tmp, EA);
7622     tcg_gen_addi_tl(EA, EA, 4);
7623     gen_qemu_ld32u_i64(ctx, xtl, EA);
7624     tcg_gen_deposit_i64(xtl, xtl, tmp, 32, 32);
7625
7626     tcg_temp_free(EA);
7627     tcg_temp_free_i64(tmp);
7628 }
7629
7630 #define VSX_STORE_SCALAR(name, operation)                     \
7631 static void gen_##name(DisasContext *ctx)                     \
7632 {                                                             \
7633     TCGv EA;                                                  \
7634     if (unlikely(!ctx->vsx_enabled)) {                        \
7635         gen_exception(ctx, POWERPC_EXCP_VSXU);                \
7636         return;                                               \
7637     }                                                         \
7638     gen_set_access_type(ctx, ACCESS_INT);                     \
7639     EA = tcg_temp_new();                                      \
7640     gen_addr_reg_index(ctx, EA);                              \
7641     gen_qemu_##operation(ctx, cpu_vsrh(xS(ctx->opcode)), EA); \
7642     tcg_temp_free(EA);                                        \
7643 }
7644
7645 VSX_STORE_SCALAR(stxsdx, st64)
7646 VSX_STORE_SCALAR(stxsiwx, st32_i64)
7647 VSX_STORE_SCALAR(stxsspx, st32fs)
7648
7649 static void gen_stxvd2x(DisasContext *ctx)
7650 {
7651     TCGv EA;
7652     if (unlikely(!ctx->vsx_enabled)) {
7653         gen_exception(ctx, POWERPC_EXCP_VSXU);
7654         return;
7655     }
7656     gen_set_access_type(ctx, ACCESS_INT);
7657     EA = tcg_temp_new();
7658     gen_addr_reg_index(ctx, EA);
7659     gen_qemu_st64(ctx, cpu_vsrh(xS(ctx->opcode)), EA);
7660     tcg_gen_addi_tl(EA, EA, 8);
7661     gen_qemu_st64(ctx, cpu_vsrl(xS(ctx->opcode)), EA);
7662     tcg_temp_free(EA);
7663 }
7664
7665 static void gen_stxvw4x(DisasContext *ctx)
7666 {
7667     TCGv_i64 tmp;
7668     TCGv EA;
7669     if (unlikely(!ctx->vsx_enabled)) {
7670         gen_exception(ctx, POWERPC_EXCP_VSXU);
7671         return;
7672     }
7673     gen_set_access_type(ctx, ACCESS_INT);
7674     EA = tcg_temp_new();
7675     gen_addr_reg_index(ctx, EA);
7676     tmp = tcg_temp_new_i64();
7677
7678     tcg_gen_shri_i64(tmp, cpu_vsrh(xS(ctx->opcode)), 32);
7679     gen_qemu_st32_i64(ctx, tmp, EA);
7680     tcg_gen_addi_tl(EA, EA, 4);
7681     gen_qemu_st32_i64(ctx, cpu_vsrh(xS(ctx->opcode)), EA);
7682
7683     tcg_gen_shri_i64(tmp, cpu_vsrl(xS(ctx->opcode)), 32);
7684     tcg_gen_addi_tl(EA, EA, 4);
7685     gen_qemu_st32_i64(ctx, tmp, EA);
7686     tcg_gen_addi_tl(EA, EA, 4);
7687     gen_qemu_st32_i64(ctx, cpu_vsrl(xS(ctx->opcode)), EA);
7688
7689     tcg_temp_free(EA);
7690     tcg_temp_free_i64(tmp);
7691 }
7692
7693 #define MV_VSRW(name, tcgop1, tcgop2, target, source)           \
7694 static void gen_##name(DisasContext *ctx)                       \
7695 {                                                               \
7696     if (xS(ctx->opcode) < 32) {                                 \
7697         if (unlikely(!ctx->fpu_enabled)) {                      \
7698             gen_exception(ctx, POWERPC_EXCP_FPU);               \
7699             return;                                             \
7700         }                                                       \
7701     } else {                                                    \
7702         if (unlikely(!ctx->altivec_enabled)) {                  \
7703             gen_exception(ctx, POWERPC_EXCP_VPU);               \
7704             return;                                             \
7705         }                                                       \
7706     }                                                           \
7707     TCGv_i64 tmp = tcg_temp_new_i64();                          \
7708     tcg_gen_##tcgop1(tmp, source);                              \
7709     tcg_gen_##tcgop2(target, tmp);                              \
7710     tcg_temp_free_i64(tmp);                                     \
7711 }
7712
7713
7714 MV_VSRW(mfvsrwz, ext32u_i64, trunc_i64_tl, cpu_gpr[rA(ctx->opcode)], \
7715         cpu_vsrh(xS(ctx->opcode)))
7716 MV_VSRW(mtvsrwa, extu_tl_i64, ext32s_i64, cpu_vsrh(xT(ctx->opcode)), \
7717         cpu_gpr[rA(ctx->opcode)])
7718 MV_VSRW(mtvsrwz, extu_tl_i64, ext32u_i64, cpu_vsrh(xT(ctx->opcode)), \
7719         cpu_gpr[rA(ctx->opcode)])
7720
7721 #if defined(TARGET_PPC64)
7722 #define MV_VSRD(name, target, source)                           \
7723 static void gen_##name(DisasContext *ctx)                       \
7724 {                                                               \
7725     if (xS(ctx->opcode) < 32) {                                 \
7726         if (unlikely(!ctx->fpu_enabled)) {                      \
7727             gen_exception(ctx, POWERPC_EXCP_FPU);               \
7728             return;                                             \
7729         }                                                       \
7730     } else {                                                    \
7731         if (unlikely(!ctx->altivec_enabled)) {                  \
7732             gen_exception(ctx, POWERPC_EXCP_VPU);               \
7733             return;                                             \
7734         }                                                       \
7735     }                                                           \
7736     tcg_gen_mov_i64(target, source);                            \
7737 }
7738
7739 MV_VSRD(mfvsrd, cpu_gpr[rA(ctx->opcode)], cpu_vsrh(xS(ctx->opcode)))
7740 MV_VSRD(mtvsrd, cpu_vsrh(xT(ctx->opcode)), cpu_gpr[rA(ctx->opcode)])
7741
7742 #endif
7743
7744 static void gen_xxpermdi(DisasContext *ctx)
7745 {
7746     if (unlikely(!ctx->vsx_enabled)) {
7747         gen_exception(ctx, POWERPC_EXCP_VSXU);
7748         return;
7749     }
7750
7751     if (unlikely((xT(ctx->opcode) == xA(ctx->opcode)) ||
7752                  (xT(ctx->opcode) == xB(ctx->opcode)))) {
7753         TCGv_i64 xh, xl;
7754
7755         xh = tcg_temp_new_i64();
7756         xl = tcg_temp_new_i64();
7757
7758         if ((DM(ctx->opcode) & 2) == 0) {
7759             tcg_gen_mov_i64(xh, cpu_vsrh(xA(ctx->opcode)));
7760         } else {
7761             tcg_gen_mov_i64(xh, cpu_vsrl(xA(ctx->opcode)));
7762         }
7763         if ((DM(ctx->opcode) & 1) == 0) {
7764             tcg_gen_mov_i64(xl, cpu_vsrh(xB(ctx->opcode)));
7765         } else {
7766             tcg_gen_mov_i64(xl, cpu_vsrl(xB(ctx->opcode)));
7767         }
7768
7769         tcg_gen_mov_i64(cpu_vsrh(xT(ctx->opcode)), xh);
7770         tcg_gen_mov_i64(cpu_vsrl(xT(ctx->opcode)), xl);
7771
7772         tcg_temp_free_i64(xh);
7773         tcg_temp_free_i64(xl);
7774     } else {
7775         if ((DM(ctx->opcode) & 2) == 0) {
7776             tcg_gen_mov_i64(cpu_vsrh(xT(ctx->opcode)), cpu_vsrh(xA(ctx->opcode)));
7777         } else {
7778             tcg_gen_mov_i64(cpu_vsrh(xT(ctx->opcode)), cpu_vsrl(xA(ctx->opcode)));
7779         }
7780         if ((DM(ctx->opcode) & 1) == 0) {
7781             tcg_gen_mov_i64(cpu_vsrl(xT(ctx->opcode)), cpu_vsrh(xB(ctx->opcode)));
7782         } else {
7783             tcg_gen_mov_i64(cpu_vsrl(xT(ctx->opcode)), cpu_vsrl(xB(ctx->opcode)));
7784         }
7785     }
7786 }
7787
7788 #define OP_ABS 1
7789 #define OP_NABS 2
7790 #define OP_NEG 3
7791 #define OP_CPSGN 4
7792 #define SGN_MASK_DP  0x8000000000000000ull
7793 #define SGN_MASK_SP 0x8000000080000000ull
7794
7795 #define VSX_SCALAR_MOVE(name, op, sgn_mask)                       \
7796 static void glue(gen_, name)(DisasContext * ctx)                  \
7797     {                                                             \
7798         TCGv_i64 xb, sgm;                                         \
7799         if (unlikely(!ctx->vsx_enabled)) {                        \
7800             gen_exception(ctx, POWERPC_EXCP_VSXU);                \
7801             return;                                               \
7802         }                                                         \
7803         xb = tcg_temp_new_i64();                                  \
7804         sgm = tcg_temp_new_i64();                                 \
7805         tcg_gen_mov_i64(xb, cpu_vsrh(xB(ctx->opcode)));           \
7806         tcg_gen_movi_i64(sgm, sgn_mask);                          \
7807         switch (op) {                                             \
7808             case OP_ABS: {                                        \
7809                 tcg_gen_andc_i64(xb, xb, sgm);                    \
7810                 break;                                            \
7811             }                                                     \
7812             case OP_NABS: {                                       \
7813                 tcg_gen_or_i64(xb, xb, sgm);                      \
7814                 break;                                            \
7815             }                                                     \
7816             case OP_NEG: {                                        \
7817                 tcg_gen_xor_i64(xb, xb, sgm);                     \
7818                 break;                                            \
7819             }                                                     \
7820             case OP_CPSGN: {                                      \
7821                 TCGv_i64 xa = tcg_temp_new_i64();                 \
7822                 tcg_gen_mov_i64(xa, cpu_vsrh(xA(ctx->opcode)));   \
7823                 tcg_gen_and_i64(xa, xa, sgm);                     \
7824                 tcg_gen_andc_i64(xb, xb, sgm);                    \
7825                 tcg_gen_or_i64(xb, xb, xa);                       \
7826                 tcg_temp_free_i64(xa);                            \
7827                 break;                                            \
7828             }                                                     \
7829         }                                                         \
7830         tcg_gen_mov_i64(cpu_vsrh(xT(ctx->opcode)), xb);           \
7831         tcg_temp_free_i64(xb);                                    \
7832         tcg_temp_free_i64(sgm);                                   \
7833     }
7834
7835 VSX_SCALAR_MOVE(xsabsdp, OP_ABS, SGN_MASK_DP)
7836 VSX_SCALAR_MOVE(xsnabsdp, OP_NABS, SGN_MASK_DP)
7837 VSX_SCALAR_MOVE(xsnegdp, OP_NEG, SGN_MASK_DP)
7838 VSX_SCALAR_MOVE(xscpsgndp, OP_CPSGN, SGN_MASK_DP)
7839
7840 #define VSX_VECTOR_MOVE(name, op, sgn_mask)                      \
7841 static void glue(gen_, name)(DisasContext * ctx)                 \
7842     {                                                            \
7843         TCGv_i64 xbh, xbl, sgm;                                  \
7844         if (unlikely(!ctx->vsx_enabled)) {                       \
7845             gen_exception(ctx, POWERPC_EXCP_VSXU);               \
7846             return;                                              \
7847         }                                                        \
7848         xbh = tcg_temp_new_i64();                                \
7849         xbl = tcg_temp_new_i64();                                \
7850         sgm = tcg_temp_new_i64();                                \
7851         tcg_gen_mov_i64(xbh, cpu_vsrh(xB(ctx->opcode)));         \
7852         tcg_gen_mov_i64(xbl, cpu_vsrl(xB(ctx->opcode)));         \
7853         tcg_gen_movi_i64(sgm, sgn_mask);                         \
7854         switch (op) {                                            \
7855             case OP_ABS: {                                       \
7856                 tcg_gen_andc_i64(xbh, xbh, sgm);                 \
7857                 tcg_gen_andc_i64(xbl, xbl, sgm);                 \
7858                 break;                                           \
7859             }                                                    \
7860             case OP_NABS: {                                      \
7861                 tcg_gen_or_i64(xbh, xbh, sgm);                   \
7862                 tcg_gen_or_i64(xbl, xbl, sgm);                   \
7863                 break;                                           \
7864             }                                                    \
7865             case OP_NEG: {                                       \
7866                 tcg_gen_xor_i64(xbh, xbh, sgm);                  \
7867                 tcg_gen_xor_i64(xbl, xbl, sgm);                  \
7868                 break;                                           \
7869             }                                                    \
7870             case OP_CPSGN: {                                     \
7871                 TCGv_i64 xah = tcg_temp_new_i64();               \
7872                 TCGv_i64 xal = tcg_temp_new_i64();               \
7873                 tcg_gen_mov_i64(xah, cpu_vsrh(xA(ctx->opcode))); \
7874                 tcg_gen_mov_i64(xal, cpu_vsrl(xA(ctx->opcode))); \
7875                 tcg_gen_and_i64(xah, xah, sgm);                  \
7876                 tcg_gen_and_i64(xal, xal, sgm);                  \
7877                 tcg_gen_andc_i64(xbh, xbh, sgm);                 \
7878                 tcg_gen_andc_i64(xbl, xbl, sgm);                 \
7879                 tcg_gen_or_i64(xbh, xbh, xah);                   \
7880                 tcg_gen_or_i64(xbl, xbl, xal);                   \
7881                 tcg_temp_free_i64(xah);                          \
7882                 tcg_temp_free_i64(xal);                          \
7883                 break;                                           \
7884             }                                                    \
7885         }                                                        \
7886         tcg_gen_mov_i64(cpu_vsrh(xT(ctx->opcode)), xbh);         \
7887         tcg_gen_mov_i64(cpu_vsrl(xT(ctx->opcode)), xbl);         \
7888         tcg_temp_free_i64(xbh);                                  \
7889         tcg_temp_free_i64(xbl);                                  \
7890         tcg_temp_free_i64(sgm);                                  \
7891     }
7892
7893 VSX_VECTOR_MOVE(xvabsdp, OP_ABS, SGN_MASK_DP)
7894 VSX_VECTOR_MOVE(xvnabsdp, OP_NABS, SGN_MASK_DP)
7895 VSX_VECTOR_MOVE(xvnegdp, OP_NEG, SGN_MASK_DP)
7896 VSX_VECTOR_MOVE(xvcpsgndp, OP_CPSGN, SGN_MASK_DP)
7897 VSX_VECTOR_MOVE(xvabssp, OP_ABS, SGN_MASK_SP)
7898 VSX_VECTOR_MOVE(xvnabssp, OP_NABS, SGN_MASK_SP)
7899 VSX_VECTOR_MOVE(xvnegsp, OP_NEG, SGN_MASK_SP)
7900 VSX_VECTOR_MOVE(xvcpsgnsp, OP_CPSGN, SGN_MASK_SP)
7901
7902 #define GEN_VSX_HELPER_2(name, op1, op2, inval, type)                         \
7903 static void gen_##name(DisasContext * ctx)                                    \
7904 {                                                                             \
7905     TCGv_i32 opc;                                                             \
7906     if (unlikely(!ctx->vsx_enabled)) {                                        \
7907         gen_exception(ctx, POWERPC_EXCP_VSXU);                                \
7908         return;                                                               \
7909     }                                                                         \
7910     /* NIP cannot be restored if the memory exception comes from an helper */ \
7911     gen_update_nip(ctx, ctx->nip - 4);                                        \
7912     opc = tcg_const_i32(ctx->opcode);                                         \
7913     gen_helper_##name(cpu_env, opc);                                          \
7914     tcg_temp_free_i32(opc);                                                   \
7915 }
7916
7917 #define GEN_VSX_HELPER_XT_XB_ENV(name, op1, op2, inval, type) \
7918 static void gen_##name(DisasContext * ctx)                    \
7919 {                                                             \
7920     if (unlikely(!ctx->vsx_enabled)) {                        \
7921         gen_exception(ctx, POWERPC_EXCP_VSXU);                \
7922         return;                                               \
7923     }                                                         \
7924     /* NIP cannot be restored if the exception comes */       \
7925     /* from a helper. */                                      \
7926     gen_update_nip(ctx, ctx->nip - 4);                        \
7927                                                               \
7928     gen_helper_##name(cpu_vsrh(xT(ctx->opcode)), cpu_env,     \
7929                       cpu_vsrh(xB(ctx->opcode)));             \
7930 }
7931
7932 GEN_VSX_HELPER_2(xsadddp, 0x00, 0x04, 0, PPC2_VSX)
7933 GEN_VSX_HELPER_2(xssubdp, 0x00, 0x05, 0, PPC2_VSX)
7934 GEN_VSX_HELPER_2(xsmuldp, 0x00, 0x06, 0, PPC2_VSX)
7935 GEN_VSX_HELPER_2(xsdivdp, 0x00, 0x07, 0, PPC2_VSX)
7936 GEN_VSX_HELPER_2(xsredp, 0x14, 0x05, 0, PPC2_VSX)
7937 GEN_VSX_HELPER_2(xssqrtdp, 0x16, 0x04, 0, PPC2_VSX)
7938 GEN_VSX_HELPER_2(xsrsqrtedp, 0x14, 0x04, 0, PPC2_VSX)
7939 GEN_VSX_HELPER_2(xstdivdp, 0x14, 0x07, 0, PPC2_VSX)
7940 GEN_VSX_HELPER_2(xstsqrtdp, 0x14, 0x06, 0, PPC2_VSX)
7941 GEN_VSX_HELPER_2(xsmaddadp, 0x04, 0x04, 0, PPC2_VSX)
7942 GEN_VSX_HELPER_2(xsmaddmdp, 0x04, 0x05, 0, PPC2_VSX)
7943 GEN_VSX_HELPER_2(xsmsubadp, 0x04, 0x06, 0, PPC2_VSX)
7944 GEN_VSX_HELPER_2(xsmsubmdp, 0x04, 0x07, 0, PPC2_VSX)
7945 GEN_VSX_HELPER_2(xsnmaddadp, 0x04, 0x14, 0, PPC2_VSX)
7946 GEN_VSX_HELPER_2(xsnmaddmdp, 0x04, 0x15, 0, PPC2_VSX)
7947 GEN_VSX_HELPER_2(xsnmsubadp, 0x04, 0x16, 0, PPC2_VSX)
7948 GEN_VSX_HELPER_2(xsnmsubmdp, 0x04, 0x17, 0, PPC2_VSX)
7949 GEN_VSX_HELPER_2(xscmpodp, 0x0C, 0x05, 0, PPC2_VSX)
7950 GEN_VSX_HELPER_2(xscmpudp, 0x0C, 0x04, 0, PPC2_VSX)
7951 GEN_VSX_HELPER_2(xsmaxdp, 0x00, 0x14, 0, PPC2_VSX)
7952 GEN_VSX_HELPER_2(xsmindp, 0x00, 0x15, 0, PPC2_VSX)
7953 GEN_VSX_HELPER_2(xscvdpsp, 0x12, 0x10, 0, PPC2_VSX)
7954 GEN_VSX_HELPER_XT_XB_ENV(xscvdpspn, 0x16, 0x10, 0, PPC2_VSX207)
7955 GEN_VSX_HELPER_2(xscvspdp, 0x12, 0x14, 0, PPC2_VSX)
7956 GEN_VSX_HELPER_XT_XB_ENV(xscvspdpn, 0x16, 0x14, 0, PPC2_VSX207)
7957 GEN_VSX_HELPER_2(xscvdpsxds, 0x10, 0x15, 0, PPC2_VSX)
7958 GEN_VSX_HELPER_2(xscvdpsxws, 0x10, 0x05, 0, PPC2_VSX)
7959 GEN_VSX_HELPER_2(xscvdpuxds, 0x10, 0x14, 0, PPC2_VSX)
7960 GEN_VSX_HELPER_2(xscvdpuxws, 0x10, 0x04, 0, PPC2_VSX)
7961 GEN_VSX_HELPER_2(xscvsxddp, 0x10, 0x17, 0, PPC2_VSX)
7962 GEN_VSX_HELPER_2(xscvuxddp, 0x10, 0x16, 0, PPC2_VSX)
7963 GEN_VSX_HELPER_2(xsrdpi, 0x12, 0x04, 0, PPC2_VSX)
7964 GEN_VSX_HELPER_2(xsrdpic, 0x16, 0x06, 0, PPC2_VSX)
7965 GEN_VSX_HELPER_2(xsrdpim, 0x12, 0x07, 0, PPC2_VSX)
7966 GEN_VSX_HELPER_2(xsrdpip, 0x12, 0x06, 0, PPC2_VSX)
7967 GEN_VSX_HELPER_2(xsrdpiz, 0x12, 0x05, 0, PPC2_VSX)
7968 GEN_VSX_HELPER_XT_XB_ENV(xsrsp, 0x12, 0x11, 0, PPC2_VSX207)
7969
7970 GEN_VSX_HELPER_2(xsaddsp, 0x00, 0x00, 0, PPC2_VSX207)
7971 GEN_VSX_HELPER_2(xssubsp, 0x00, 0x01, 0, PPC2_VSX207)
7972 GEN_VSX_HELPER_2(xsmulsp, 0x00, 0x02, 0, PPC2_VSX207)
7973 GEN_VSX_HELPER_2(xsdivsp, 0x00, 0x03, 0, PPC2_VSX207)
7974 GEN_VSX_HELPER_2(xsresp, 0x14, 0x01, 0, PPC2_VSX207)
7975 GEN_VSX_HELPER_2(xssqrtsp, 0x16, 0x00, 0, PPC2_VSX207)
7976 GEN_VSX_HELPER_2(xsrsqrtesp, 0x14, 0x00, 0, PPC2_VSX207)
7977 GEN_VSX_HELPER_2(xsmaddasp, 0x04, 0x00, 0, PPC2_VSX207)
7978 GEN_VSX_HELPER_2(xsmaddmsp, 0x04, 0x01, 0, PPC2_VSX207)
7979 GEN_VSX_HELPER_2(xsmsubasp, 0x04, 0x02, 0, PPC2_VSX207)
7980 GEN_VSX_HELPER_2(xsmsubmsp, 0x04, 0x03, 0, PPC2_VSX207)
7981 GEN_VSX_HELPER_2(xsnmaddasp, 0x04, 0x10, 0, PPC2_VSX207)
7982 GEN_VSX_HELPER_2(xsnmaddmsp, 0x04, 0x11, 0, PPC2_VSX207)
7983 GEN_VSX_HELPER_2(xsnmsubasp, 0x04, 0x12, 0, PPC2_VSX207)
7984 GEN_VSX_HELPER_2(xsnmsubmsp, 0x04, 0x13, 0, PPC2_VSX207)
7985 GEN_VSX_HELPER_2(xscvsxdsp, 0x10, 0x13, 0, PPC2_VSX207)
7986 GEN_VSX_HELPER_2(xscvuxdsp, 0x10, 0x12, 0, PPC2_VSX207)
7987
7988 GEN_VSX_HELPER_2(xvadddp, 0x00, 0x0C, 0, PPC2_VSX)
7989 GEN_VSX_HELPER_2(xvsubdp, 0x00, 0x0D, 0, PPC2_VSX)
7990 GEN_VSX_HELPER_2(xvmuldp, 0x00, 0x0E, 0, PPC2_VSX)
7991 GEN_VSX_HELPER_2(xvdivdp, 0x00, 0x0F, 0, PPC2_VSX)
7992 GEN_VSX_HELPER_2(xvredp, 0x14, 0x0D, 0, PPC2_VSX)
7993 GEN_VSX_HELPER_2(xvsqrtdp, 0x16, 0x0C, 0, PPC2_VSX)
7994 GEN_VSX_HELPER_2(xvrsqrtedp, 0x14, 0x0C, 0, PPC2_VSX)
7995 GEN_VSX_HELPER_2(xvtdivdp, 0x14, 0x0F, 0, PPC2_VSX)
7996 GEN_VSX_HELPER_2(xvtsqrtdp, 0x14, 0x0E, 0, PPC2_VSX)
7997 GEN_VSX_HELPER_2(xvmaddadp, 0x04, 0x0C, 0, PPC2_VSX)
7998 GEN_VSX_HELPER_2(xvmaddmdp, 0x04, 0x0D, 0, PPC2_VSX)
7999 GEN_VSX_HELPER_2(xvmsubadp, 0x04, 0x0E, 0, PPC2_VSX)
8000 GEN_VSX_HELPER_2(xvmsubmdp, 0x04, 0x0F, 0, PPC2_VSX)
8001 GEN_VSX_HELPER_2(xvnmaddadp, 0x04, 0x1C, 0, PPC2_VSX)
8002 GEN_VSX_HELPER_2(xvnmaddmdp, 0x04, 0x1D, 0, PPC2_VSX)
8003 GEN_VSX_HELPER_2(xvnmsubadp, 0x04, 0x1E, 0, PPC2_VSX)
8004 GEN_VSX_HELPER_2(xvnmsubmdp, 0x04, 0x1F, 0, PPC2_VSX)
8005 GEN_VSX_HELPER_2(xvmaxdp, 0x00, 0x1C, 0, PPC2_VSX)
8006 GEN_VSX_HELPER_2(xvmindp, 0x00, 0x1D, 0, PPC2_VSX)
8007 GEN_VSX_HELPER_2(xvcmpeqdp, 0x0C, 0x0C, 0, PPC2_VSX)
8008 GEN_VSX_HELPER_2(xvcmpgtdp, 0x0C, 0x0D, 0, PPC2_VSX)
8009 GEN_VSX_HELPER_2(xvcmpgedp, 0x0C, 0x0E, 0, PPC2_VSX)
8010 GEN_VSX_HELPER_2(xvcvdpsp, 0x12, 0x18, 0, PPC2_VSX)
8011 GEN_VSX_HELPER_2(xvcvdpsxds, 0x10, 0x1D, 0, PPC2_VSX)
8012 GEN_VSX_HELPER_2(xvcvdpsxws, 0x10, 0x0D, 0, PPC2_VSX)
8013 GEN_VSX_HELPER_2(xvcvdpuxds, 0x10, 0x1C, 0, PPC2_VSX)
8014 GEN_VSX_HELPER_2(xvcvdpuxws, 0x10, 0x0C, 0, PPC2_VSX)
8015 GEN_VSX_HELPER_2(xvcvsxddp, 0x10, 0x1F, 0, PPC2_VSX)
8016 GEN_VSX_HELPER_2(xvcvuxddp, 0x10, 0x1E, 0, PPC2_VSX)
8017 GEN_VSX_HELPER_2(xvcvsxwdp, 0x10, 0x0F, 0, PPC2_VSX)
8018 GEN_VSX_HELPER_2(xvcvuxwdp, 0x10, 0x0E, 0, PPC2_VSX)
8019 GEN_VSX_HELPER_2(xvrdpi, 0x12, 0x0C, 0, PPC2_VSX)
8020 GEN_VSX_HELPER_2(xvrdpic, 0x16, 0x0E, 0, PPC2_VSX)
8021 GEN_VSX_HELPER_2(xvrdpim, 0x12, 0x0F, 0, PPC2_VSX)
8022 GEN_VSX_HELPER_2(xvrdpip, 0x12, 0x0E, 0, PPC2_VSX)
8023 GEN_VSX_HELPER_2(xvrdpiz, 0x12, 0x0D, 0, PPC2_VSX)
8024
8025 GEN_VSX_HELPER_2(xvaddsp, 0x00, 0x08, 0, PPC2_VSX)
8026 GEN_VSX_HELPER_2(xvsubsp, 0x00, 0x09, 0, PPC2_VSX)
8027 GEN_VSX_HELPER_2(xvmulsp, 0x00, 0x0A, 0, PPC2_VSX)
8028 GEN_VSX_HELPER_2(xvdivsp, 0x00, 0x0B, 0, PPC2_VSX)
8029 GEN_VSX_HELPER_2(xvresp, 0x14, 0x09, 0, PPC2_VSX)
8030 GEN_VSX_HELPER_2(xvsqrtsp, 0x16, 0x08, 0, PPC2_VSX)
8031 GEN_VSX_HELPER_2(xvrsqrtesp, 0x14, 0x08, 0, PPC2_VSX)
8032 GEN_VSX_HELPER_2(xvtdivsp, 0x14, 0x0B, 0, PPC2_VSX)
8033 GEN_VSX_HELPER_2(xvtsqrtsp, 0x14, 0x0A, 0, PPC2_VSX)
8034 GEN_VSX_HELPER_2(xvmaddasp, 0x04, 0x08, 0, PPC2_VSX)
8035 GEN_VSX_HELPER_2(xvmaddmsp, 0x04, 0x09, 0, PPC2_VSX)
8036 GEN_VSX_HELPER_2(xvmsubasp, 0x04, 0x0A, 0, PPC2_VSX)
8037 GEN_VSX_HELPER_2(xvmsubmsp, 0x04, 0x0B, 0, PPC2_VSX)
8038 GEN_VSX_HELPER_2(xvnmaddasp, 0x04, 0x18, 0, PPC2_VSX)
8039 GEN_VSX_HELPER_2(xvnmaddmsp, 0x04, 0x19, 0, PPC2_VSX)
8040 GEN_VSX_HELPER_2(xvnmsubasp, 0x04, 0x1A, 0, PPC2_VSX)
8041 GEN_VSX_HELPER_2(xvnmsubmsp, 0x04, 0x1B, 0, PPC2_VSX)
8042 GEN_VSX_HELPER_2(xvmaxsp, 0x00, 0x18, 0, PPC2_VSX)
8043 GEN_VSX_HELPER_2(xvminsp, 0x00, 0x19, 0, PPC2_VSX)
8044 GEN_VSX_HELPER_2(xvcmpeqsp, 0x0C, 0x08, 0, PPC2_VSX)
8045 GEN_VSX_HELPER_2(xvcmpgtsp, 0x0C, 0x09, 0, PPC2_VSX)
8046 GEN_VSX_HELPER_2(xvcmpgesp, 0x0C, 0x0A, 0, PPC2_VSX)
8047 GEN_VSX_HELPER_2(xvcvspdp, 0x12, 0x1C, 0, PPC2_VSX)
8048 GEN_VSX_HELPER_2(xvcvspsxds, 0x10, 0x19, 0, PPC2_VSX)
8049 GEN_VSX_HELPER_2(xvcvspsxws, 0x10, 0x09, 0, PPC2_VSX)
8050 GEN_VSX_HELPER_2(xvcvspuxds, 0x10, 0x18, 0, PPC2_VSX)
8051 GEN_VSX_HELPER_2(xvcvspuxws, 0x10, 0x08, 0, PPC2_VSX)
8052 GEN_VSX_HELPER_2(xvcvsxdsp, 0x10, 0x1B, 0, PPC2_VSX)
8053 GEN_VSX_HELPER_2(xvcvuxdsp, 0x10, 0x1A, 0, PPC2_VSX)
8054 GEN_VSX_HELPER_2(xvcvsxwsp, 0x10, 0x0B, 0, PPC2_VSX)
8055 GEN_VSX_HELPER_2(xvcvuxwsp, 0x10, 0x0A, 0, PPC2_VSX)
8056 GEN_VSX_HELPER_2(xvrspi, 0x12, 0x08, 0, PPC2_VSX)
8057 GEN_VSX_HELPER_2(xvrspic, 0x16, 0x0A, 0, PPC2_VSX)
8058 GEN_VSX_HELPER_2(xvrspim, 0x12, 0x0B, 0, PPC2_VSX)
8059 GEN_VSX_HELPER_2(xvrspip, 0x12, 0x0A, 0, PPC2_VSX)
8060 GEN_VSX_HELPER_2(xvrspiz, 0x12, 0x09, 0, PPC2_VSX)
8061
8062 #define VSX_LOGICAL(name, tcg_op)                                    \
8063 static void glue(gen_, name)(DisasContext * ctx)                     \
8064     {                                                                \
8065         if (unlikely(!ctx->vsx_enabled)) {                           \
8066             gen_exception(ctx, POWERPC_EXCP_VSXU);                   \
8067             return;                                                  \
8068         }                                                            \
8069         tcg_op(cpu_vsrh(xT(ctx->opcode)), cpu_vsrh(xA(ctx->opcode)), \
8070             cpu_vsrh(xB(ctx->opcode)));                              \
8071         tcg_op(cpu_vsrl(xT(ctx->opcode)), cpu_vsrl(xA(ctx->opcode)), \
8072             cpu_vsrl(xB(ctx->opcode)));                              \
8073     }
8074
8075 VSX_LOGICAL(xxland, tcg_gen_and_i64)
8076 VSX_LOGICAL(xxlandc, tcg_gen_andc_i64)
8077 VSX_LOGICAL(xxlor, tcg_gen_or_i64)
8078 VSX_LOGICAL(xxlxor, tcg_gen_xor_i64)
8079 VSX_LOGICAL(xxlnor, tcg_gen_nor_i64)
8080 VSX_LOGICAL(xxleqv, tcg_gen_eqv_i64)
8081 VSX_LOGICAL(xxlnand, tcg_gen_nand_i64)
8082 VSX_LOGICAL(xxlorc, tcg_gen_orc_i64)
8083
8084 #define VSX_XXMRG(name, high)                               \
8085 static void glue(gen_, name)(DisasContext * ctx)            \
8086     {                                                       \
8087         TCGv_i64 a0, a1, b0, b1;                            \
8088         if (unlikely(!ctx->vsx_enabled)) {                  \
8089             gen_exception(ctx, POWERPC_EXCP_VSXU);          \
8090             return;                                         \
8091         }                                                   \
8092         a0 = tcg_temp_new_i64();                            \
8093         a1 = tcg_temp_new_i64();                            \
8094         b0 = tcg_temp_new_i64();                            \
8095         b1 = tcg_temp_new_i64();                            \
8096         if (high) {                                         \
8097             tcg_gen_mov_i64(a0, cpu_vsrh(xA(ctx->opcode))); \
8098             tcg_gen_mov_i64(a1, cpu_vsrh(xA(ctx->opcode))); \
8099             tcg_gen_mov_i64(b0, cpu_vsrh(xB(ctx->opcode))); \
8100             tcg_gen_mov_i64(b1, cpu_vsrh(xB(ctx->opcode))); \
8101         } else {                                            \
8102             tcg_gen_mov_i64(a0, cpu_vsrl(xA(ctx->opcode))); \
8103             tcg_gen_mov_i64(a1, cpu_vsrl(xA(ctx->opcode))); \
8104             tcg_gen_mov_i64(b0, cpu_vsrl(xB(ctx->opcode))); \
8105             tcg_gen_mov_i64(b1, cpu_vsrl(xB(ctx->opcode))); \
8106         }                                                   \
8107         tcg_gen_shri_i64(a0, a0, 32);                       \
8108         tcg_gen_shri_i64(b0, b0, 32);                       \
8109         tcg_gen_deposit_i64(cpu_vsrh(xT(ctx->opcode)),      \
8110                             b0, a0, 32, 32);                \
8111         tcg_gen_deposit_i64(cpu_vsrl(xT(ctx->opcode)),      \
8112                             b1, a1, 32, 32);                \
8113         tcg_temp_free_i64(a0);                              \
8114         tcg_temp_free_i64(a1);                              \
8115         tcg_temp_free_i64(b0);                              \
8116         tcg_temp_free_i64(b1);                              \
8117     }
8118
8119 VSX_XXMRG(xxmrghw, 1)
8120 VSX_XXMRG(xxmrglw, 0)
8121
8122 static void gen_xxsel(DisasContext * ctx)
8123 {
8124     TCGv_i64 a, b, c;
8125     if (unlikely(!ctx->vsx_enabled)) {
8126         gen_exception(ctx, POWERPC_EXCP_VSXU);
8127         return;
8128     }
8129     a = tcg_temp_new_i64();
8130     b = tcg_temp_new_i64();
8131     c = tcg_temp_new_i64();
8132
8133     tcg_gen_mov_i64(a, cpu_vsrh(xA(ctx->opcode)));
8134     tcg_gen_mov_i64(b, cpu_vsrh(xB(ctx->opcode)));
8135     tcg_gen_mov_i64(c, cpu_vsrh(xC(ctx->opcode)));
8136
8137     tcg_gen_and_i64(b, b, c);
8138     tcg_gen_andc_i64(a, a, c);
8139     tcg_gen_or_i64(cpu_vsrh(xT(ctx->opcode)), a, b);
8140
8141     tcg_gen_mov_i64(a, cpu_vsrl(xA(ctx->opcode)));
8142     tcg_gen_mov_i64(b, cpu_vsrl(xB(ctx->opcode)));
8143     tcg_gen_mov_i64(c, cpu_vsrl(xC(ctx->opcode)));
8144
8145     tcg_gen_and_i64(b, b, c);
8146     tcg_gen_andc_i64(a, a, c);
8147     tcg_gen_or_i64(cpu_vsrl(xT(ctx->opcode)), a, b);
8148
8149     tcg_temp_free_i64(a);
8150     tcg_temp_free_i64(b);
8151     tcg_temp_free_i64(c);
8152 }
8153
8154 static void gen_xxspltw(DisasContext *ctx)
8155 {
8156     TCGv_i64 b, b2;
8157     TCGv_i64 vsr = (UIM(ctx->opcode) & 2) ?
8158                    cpu_vsrl(xB(ctx->opcode)) :
8159                    cpu_vsrh(xB(ctx->opcode));
8160
8161     if (unlikely(!ctx->vsx_enabled)) {
8162         gen_exception(ctx, POWERPC_EXCP_VSXU);
8163         return;
8164     }
8165
8166     b = tcg_temp_new_i64();
8167     b2 = tcg_temp_new_i64();
8168
8169     if (UIM(ctx->opcode) & 1) {
8170         tcg_gen_ext32u_i64(b, vsr);
8171     } else {
8172         tcg_gen_shri_i64(b, vsr, 32);
8173     }
8174
8175     tcg_gen_shli_i64(b2, b, 32);
8176     tcg_gen_or_i64(cpu_vsrh(xT(ctx->opcode)), b, b2);
8177     tcg_gen_mov_i64(cpu_vsrl(xT(ctx->opcode)), cpu_vsrh(xT(ctx->opcode)));
8178
8179     tcg_temp_free_i64(b);
8180     tcg_temp_free_i64(b2);
8181 }
8182
8183 static void gen_xxsldwi(DisasContext *ctx)
8184 {
8185     TCGv_i64 xth, xtl;
8186     if (unlikely(!ctx->vsx_enabled)) {
8187         gen_exception(ctx, POWERPC_EXCP_VSXU);
8188         return;
8189     }
8190     xth = tcg_temp_new_i64();
8191     xtl = tcg_temp_new_i64();
8192
8193     switch (SHW(ctx->opcode)) {
8194         case 0: {
8195             tcg_gen_mov_i64(xth, cpu_vsrh(xA(ctx->opcode)));
8196             tcg_gen_mov_i64(xtl, cpu_vsrl(xA(ctx->opcode)));
8197             break;
8198         }
8199         case 1: {
8200             TCGv_i64 t0 = tcg_temp_new_i64();
8201             tcg_gen_mov_i64(xth, cpu_vsrh(xA(ctx->opcode)));
8202             tcg_gen_shli_i64(xth, xth, 32);
8203             tcg_gen_mov_i64(t0, cpu_vsrl(xA(ctx->opcode)));
8204             tcg_gen_shri_i64(t0, t0, 32);
8205             tcg_gen_or_i64(xth, xth, t0);
8206             tcg_gen_mov_i64(xtl, cpu_vsrl(xA(ctx->opcode)));
8207             tcg_gen_shli_i64(xtl, xtl, 32);
8208             tcg_gen_mov_i64(t0, cpu_vsrh(xB(ctx->opcode)));
8209             tcg_gen_shri_i64(t0, t0, 32);
8210             tcg_gen_or_i64(xtl, xtl, t0);
8211             tcg_temp_free_i64(t0);
8212             break;
8213         }
8214         case 2: {
8215             tcg_gen_mov_i64(xth, cpu_vsrl(xA(ctx->opcode)));
8216             tcg_gen_mov_i64(xtl, cpu_vsrh(xB(ctx->opcode)));
8217             break;
8218         }
8219         case 3: {
8220             TCGv_i64 t0 = tcg_temp_new_i64();
8221             tcg_gen_mov_i64(xth, cpu_vsrl(xA(ctx->opcode)));
8222             tcg_gen_shli_i64(xth, xth, 32);
8223             tcg_gen_mov_i64(t0, cpu_vsrh(xB(ctx->opcode)));
8224             tcg_gen_shri_i64(t0, t0, 32);
8225             tcg_gen_or_i64(xth, xth, t0);
8226             tcg_gen_mov_i64(xtl, cpu_vsrh(xB(ctx->opcode)));
8227             tcg_gen_shli_i64(xtl, xtl, 32);
8228             tcg_gen_mov_i64(t0, cpu_vsrl(xB(ctx->opcode)));
8229             tcg_gen_shri_i64(t0, t0, 32);
8230             tcg_gen_or_i64(xtl, xtl, t0);
8231             tcg_temp_free_i64(t0);
8232             break;
8233         }
8234     }
8235
8236     tcg_gen_mov_i64(cpu_vsrh(xT(ctx->opcode)), xth);
8237     tcg_gen_mov_i64(cpu_vsrl(xT(ctx->opcode)), xtl);
8238
8239     tcg_temp_free_i64(xth);
8240     tcg_temp_free_i64(xtl);
8241 }
8242
8243 /*** Decimal Floating Point ***/
8244
8245 static inline TCGv_ptr gen_fprp_ptr(int reg)
8246 {
8247     TCGv_ptr r = tcg_temp_new_ptr();
8248     tcg_gen_addi_ptr(r, cpu_env, offsetof(CPUPPCState, fpr[reg]));
8249     return r;
8250 }
8251
8252 #define GEN_DFP_T_A_B_Rc(name)                   \
8253 static void gen_##name(DisasContext *ctx)        \
8254 {                                                \
8255     TCGv_ptr rd, ra, rb;                         \
8256     if (unlikely(!ctx->fpu_enabled)) {           \
8257         gen_exception(ctx, POWERPC_EXCP_FPU);    \
8258         return;                                  \
8259     }                                            \
8260     gen_update_nip(ctx, ctx->nip - 4);           \
8261     rd = gen_fprp_ptr(rD(ctx->opcode));          \
8262     ra = gen_fprp_ptr(rA(ctx->opcode));          \
8263     rb = gen_fprp_ptr(rB(ctx->opcode));          \
8264     gen_helper_##name(cpu_env, rd, ra, rb);      \
8265     if (unlikely(Rc(ctx->opcode) != 0)) {        \
8266         gen_set_cr1_from_fpscr(ctx);             \
8267     }                                            \
8268     tcg_temp_free_ptr(rd);                       \
8269     tcg_temp_free_ptr(ra);                       \
8270     tcg_temp_free_ptr(rb);                       \
8271 }
8272
8273 #define GEN_DFP_BF_A_B(name)                      \
8274 static void gen_##name(DisasContext *ctx)         \
8275 {                                                 \
8276     TCGv_ptr ra, rb;                              \
8277     if (unlikely(!ctx->fpu_enabled)) {            \
8278         gen_exception(ctx, POWERPC_EXCP_FPU);     \
8279         return;                                   \
8280     }                                             \
8281     gen_update_nip(ctx, ctx->nip - 4);            \
8282     ra = gen_fprp_ptr(rA(ctx->opcode));           \
8283     rb = gen_fprp_ptr(rB(ctx->opcode));           \
8284     gen_helper_##name(cpu_crf[crfD(ctx->opcode)], \
8285                       cpu_env, ra, rb);           \
8286     tcg_temp_free_ptr(ra);                        \
8287     tcg_temp_free_ptr(rb);                        \
8288 }
8289
8290 #define GEN_DFP_BF_A_DCM(name)                    \
8291 static void gen_##name(DisasContext *ctx)         \
8292 {                                                 \
8293     TCGv_ptr ra;                                  \
8294     TCGv_i32 dcm;                                 \
8295     if (unlikely(!ctx->fpu_enabled)) {            \
8296         gen_exception(ctx, POWERPC_EXCP_FPU);     \
8297         return;                                   \
8298     }                                             \
8299     gen_update_nip(ctx, ctx->nip - 4);            \
8300     ra = gen_fprp_ptr(rA(ctx->opcode));           \
8301     dcm = tcg_const_i32(DCM(ctx->opcode));        \
8302     gen_helper_##name(cpu_crf[crfD(ctx->opcode)], \
8303                       cpu_env, ra, dcm);          \
8304     tcg_temp_free_ptr(ra);                        \
8305     tcg_temp_free_i32(dcm);                       \
8306 }
8307
8308 #define GEN_DFP_T_B_U32_U32_Rc(name, u32f1, u32f2)    \
8309 static void gen_##name(DisasContext *ctx)             \
8310 {                                                     \
8311     TCGv_ptr rt, rb;                                  \
8312     TCGv_i32 u32_1, u32_2;                            \
8313     if (unlikely(!ctx->fpu_enabled)) {                \
8314         gen_exception(ctx, POWERPC_EXCP_FPU);         \
8315         return;                                       \
8316     }                                                 \
8317     gen_update_nip(ctx, ctx->nip - 4);                \
8318     rt = gen_fprp_ptr(rD(ctx->opcode));               \
8319     rb = gen_fprp_ptr(rB(ctx->opcode));               \
8320     u32_1 = tcg_const_i32(u32f1(ctx->opcode));        \
8321     u32_2 = tcg_const_i32(u32f2(ctx->opcode));        \
8322     gen_helper_##name(cpu_env, rt, rb, u32_1, u32_2); \
8323     if (unlikely(Rc(ctx->opcode) != 0)) {             \
8324         gen_set_cr1_from_fpscr(ctx);                  \
8325     }                                                 \
8326     tcg_temp_free_ptr(rt);                            \
8327     tcg_temp_free_ptr(rb);                            \
8328     tcg_temp_free_i32(u32_1);                         \
8329     tcg_temp_free_i32(u32_2);                         \
8330 }
8331
8332 #define GEN_DFP_T_A_B_I32_Rc(name, i32fld)       \
8333 static void gen_##name(DisasContext *ctx)        \
8334 {                                                \
8335     TCGv_ptr rt, ra, rb;                         \
8336     TCGv_i32 i32;                                \
8337     if (unlikely(!ctx->fpu_enabled)) {           \
8338         gen_exception(ctx, POWERPC_EXCP_FPU);    \
8339         return;                                  \
8340     }                                            \
8341     gen_update_nip(ctx, ctx->nip - 4);           \
8342     rt = gen_fprp_ptr(rD(ctx->opcode));          \
8343     ra = gen_fprp_ptr(rA(ctx->opcode));          \
8344     rb = gen_fprp_ptr(rB(ctx->opcode));          \
8345     i32 = tcg_const_i32(i32fld(ctx->opcode));    \
8346     gen_helper_##name(cpu_env, rt, ra, rb, i32); \
8347     if (unlikely(Rc(ctx->opcode) != 0)) {        \
8348         gen_set_cr1_from_fpscr(ctx);             \
8349     }                                            \
8350     tcg_temp_free_ptr(rt);                       \
8351     tcg_temp_free_ptr(rb);                       \
8352     tcg_temp_free_ptr(ra);                       \
8353     tcg_temp_free_i32(i32);                      \
8354     }
8355
8356 #define GEN_DFP_T_B_Rc(name)                     \
8357 static void gen_##name(DisasContext *ctx)        \
8358 {                                                \
8359     TCGv_ptr rt, rb;                             \
8360     if (unlikely(!ctx->fpu_enabled)) {           \
8361         gen_exception(ctx, POWERPC_EXCP_FPU);    \
8362         return;                                  \
8363     }                                            \
8364     gen_update_nip(ctx, ctx->nip - 4);           \
8365     rt = gen_fprp_ptr(rD(ctx->opcode));          \
8366     rb = gen_fprp_ptr(rB(ctx->opcode));          \
8367     gen_helper_##name(cpu_env, rt, rb);          \
8368     if (unlikely(Rc(ctx->opcode) != 0)) {        \
8369         gen_set_cr1_from_fpscr(ctx);             \
8370     }                                            \
8371     tcg_temp_free_ptr(rt);                       \
8372     tcg_temp_free_ptr(rb);                       \
8373     }
8374
8375 #define GEN_DFP_T_FPR_I32_Rc(name, fprfld, i32fld) \
8376 static void gen_##name(DisasContext *ctx)          \
8377 {                                                  \
8378     TCGv_ptr rt, rs;                               \
8379     TCGv_i32 i32;                                  \
8380     if (unlikely(!ctx->fpu_enabled)) {             \
8381         gen_exception(ctx, POWERPC_EXCP_FPU);      \
8382         return;                                    \
8383     }                                              \
8384     gen_update_nip(ctx, ctx->nip - 4);             \
8385     rt = gen_fprp_ptr(rD(ctx->opcode));            \
8386     rs = gen_fprp_ptr(fprfld(ctx->opcode));        \
8387     i32 = tcg_const_i32(i32fld(ctx->opcode));      \
8388     gen_helper_##name(cpu_env, rt, rs, i32);       \
8389     if (unlikely(Rc(ctx->opcode) != 0)) {          \
8390         gen_set_cr1_from_fpscr(ctx);               \
8391     }                                              \
8392     tcg_temp_free_ptr(rt);                         \
8393     tcg_temp_free_ptr(rs);                         \
8394     tcg_temp_free_i32(i32);                        \
8395 }
8396
8397 GEN_DFP_T_A_B_Rc(dadd)
8398 GEN_DFP_T_A_B_Rc(daddq)
8399 GEN_DFP_T_A_B_Rc(dsub)
8400 GEN_DFP_T_A_B_Rc(dsubq)
8401 GEN_DFP_T_A_B_Rc(dmul)
8402 GEN_DFP_T_A_B_Rc(dmulq)
8403 GEN_DFP_T_A_B_Rc(ddiv)
8404 GEN_DFP_T_A_B_Rc(ddivq)
8405 GEN_DFP_BF_A_B(dcmpu)
8406 GEN_DFP_BF_A_B(dcmpuq)
8407 GEN_DFP_BF_A_B(dcmpo)
8408 GEN_DFP_BF_A_B(dcmpoq)
8409 GEN_DFP_BF_A_DCM(dtstdc)
8410 GEN_DFP_BF_A_DCM(dtstdcq)
8411 GEN_DFP_BF_A_DCM(dtstdg)
8412 GEN_DFP_BF_A_DCM(dtstdgq)
8413 GEN_DFP_BF_A_B(dtstex)
8414 GEN_DFP_BF_A_B(dtstexq)
8415 GEN_DFP_BF_A_B(dtstsf)
8416 GEN_DFP_BF_A_B(dtstsfq)
8417 GEN_DFP_T_B_U32_U32_Rc(dquai, SIMM5, RMC)
8418 GEN_DFP_T_B_U32_U32_Rc(dquaiq, SIMM5, RMC)
8419 GEN_DFP_T_A_B_I32_Rc(dqua, RMC)
8420 GEN_DFP_T_A_B_I32_Rc(dquaq, RMC)
8421 GEN_DFP_T_A_B_I32_Rc(drrnd, RMC)
8422 GEN_DFP_T_A_B_I32_Rc(drrndq, RMC)
8423 GEN_DFP_T_B_U32_U32_Rc(drintx, FPW, RMC)
8424 GEN_DFP_T_B_U32_U32_Rc(drintxq, FPW, RMC)
8425 GEN_DFP_T_B_U32_U32_Rc(drintn, FPW, RMC)
8426 GEN_DFP_T_B_U32_U32_Rc(drintnq, FPW, RMC)
8427 GEN_DFP_T_B_Rc(dctdp)
8428 GEN_DFP_T_B_Rc(dctqpq)
8429 GEN_DFP_T_B_Rc(drsp)
8430 GEN_DFP_T_B_Rc(drdpq)
8431 GEN_DFP_T_B_Rc(dcffix)
8432 GEN_DFP_T_B_Rc(dcffixq)
8433 GEN_DFP_T_B_Rc(dctfix)
8434 GEN_DFP_T_B_Rc(dctfixq)
8435 GEN_DFP_T_FPR_I32_Rc(ddedpd, rB, SP)
8436 GEN_DFP_T_FPR_I32_Rc(ddedpdq, rB, SP)
8437 GEN_DFP_T_FPR_I32_Rc(denbcd, rB, SP)
8438 GEN_DFP_T_FPR_I32_Rc(denbcdq, rB, SP)
8439 GEN_DFP_T_B_Rc(dxex)
8440 GEN_DFP_T_B_Rc(dxexq)
8441 GEN_DFP_T_A_B_Rc(diex)
8442 GEN_DFP_T_A_B_Rc(diexq)
8443 GEN_DFP_T_FPR_I32_Rc(dscli, rA, DCM)
8444 GEN_DFP_T_FPR_I32_Rc(dscliq, rA, DCM)
8445 GEN_DFP_T_FPR_I32_Rc(dscri, rA, DCM)
8446 GEN_DFP_T_FPR_I32_Rc(dscriq, rA, DCM)
8447
8448 /***                           SPE extension                               ***/
8449 /* Register moves */
8450
8451 static inline void gen_evmra(DisasContext *ctx)
8452 {
8453
8454     if (unlikely(!ctx->spe_enabled)) {
8455         gen_exception(ctx, POWERPC_EXCP_SPEU);
8456         return;
8457     }
8458
8459     TCGv_i64 tmp = tcg_temp_new_i64();
8460
8461     /* tmp := rA_lo + rA_hi << 32 */
8462     tcg_gen_concat_tl_i64(tmp, cpu_gpr[rA(ctx->opcode)], cpu_gprh[rA(ctx->opcode)]);
8463
8464     /* spe_acc := tmp */
8465     tcg_gen_st_i64(tmp, cpu_env, offsetof(CPUPPCState, spe_acc));
8466     tcg_temp_free_i64(tmp);
8467
8468     /* rD := rA */
8469     tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)]);
8470     tcg_gen_mov_tl(cpu_gprh[rD(ctx->opcode)], cpu_gprh[rA(ctx->opcode)]);
8471 }
8472
8473 static inline void gen_load_gpr64(TCGv_i64 t, int reg)
8474 {
8475     tcg_gen_concat_tl_i64(t, cpu_gpr[reg], cpu_gprh[reg]);
8476 }
8477
8478 static inline void gen_store_gpr64(int reg, TCGv_i64 t)
8479 {
8480     tcg_gen_extr_i64_tl(cpu_gpr[reg], cpu_gprh[reg], t);
8481 }
8482
8483 #define GEN_SPE(name0, name1, opc2, opc3, inval0, inval1, type)         \
8484 static void glue(gen_, name0##_##name1)(DisasContext *ctx)                    \
8485 {                                                                             \
8486     if (Rc(ctx->opcode))                                                      \
8487         gen_##name1(ctx);                                                     \
8488     else                                                                      \
8489         gen_##name0(ctx);                                                     \
8490 }
8491
8492 /* Handler for undefined SPE opcodes */
8493 static inline void gen_speundef(DisasContext *ctx)
8494 {
8495     gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL);
8496 }
8497
8498 /* SPE logic */
8499 #define GEN_SPEOP_LOGIC2(name, tcg_op)                                        \
8500 static inline void gen_##name(DisasContext *ctx)                              \
8501 {                                                                             \
8502     if (unlikely(!ctx->spe_enabled)) {                                        \
8503         gen_exception(ctx, POWERPC_EXCP_SPEU);                                \
8504         return;                                                               \
8505     }                                                                         \
8506     tcg_op(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)],                \
8507            cpu_gpr[rB(ctx->opcode)]);                                         \
8508     tcg_op(cpu_gprh[rD(ctx->opcode)], cpu_gprh[rA(ctx->opcode)],              \
8509            cpu_gprh[rB(ctx->opcode)]);                                        \
8510 }
8511
8512 GEN_SPEOP_LOGIC2(evand, tcg_gen_and_tl);
8513 GEN_SPEOP_LOGIC2(evandc, tcg_gen_andc_tl);
8514 GEN_SPEOP_LOGIC2(evxor, tcg_gen_xor_tl);
8515 GEN_SPEOP_LOGIC2(evor, tcg_gen_or_tl);
8516 GEN_SPEOP_LOGIC2(evnor, tcg_gen_nor_tl);
8517 GEN_SPEOP_LOGIC2(eveqv, tcg_gen_eqv_tl);
8518 GEN_SPEOP_LOGIC2(evorc, tcg_gen_orc_tl);
8519 GEN_SPEOP_LOGIC2(evnand, tcg_gen_nand_tl);
8520
8521 /* SPE logic immediate */
8522 #define GEN_SPEOP_TCG_LOGIC_IMM2(name, tcg_opi)                               \
8523 static inline void gen_##name(DisasContext *ctx)                              \
8524 {                                                                             \
8525     TCGv_i32 t0;                                                              \
8526     if (unlikely(!ctx->spe_enabled)) {                                        \
8527         gen_exception(ctx, POWERPC_EXCP_SPEU);                                \
8528         return;                                                               \
8529     }                                                                         \
8530     t0 = tcg_temp_new_i32();                                                  \
8531                                                                               \
8532     tcg_gen_trunc_tl_i32(t0, cpu_gpr[rA(ctx->opcode)]);                       \
8533     tcg_opi(t0, t0, rB(ctx->opcode));                                         \
8534     tcg_gen_extu_i32_tl(cpu_gpr[rD(ctx->opcode)], t0);                        \
8535                                                                               \
8536     tcg_gen_trunc_tl_i32(t0, cpu_gprh[rA(ctx->opcode)]);                      \
8537     tcg_opi(t0, t0, rB(ctx->opcode));                                         \
8538     tcg_gen_extu_i32_tl(cpu_gprh[rD(ctx->opcode)], t0);                       \
8539                                                                               \
8540     tcg_temp_free_i32(t0);                                                    \
8541 }
8542 GEN_SPEOP_TCG_LOGIC_IMM2(evslwi, tcg_gen_shli_i32);
8543 GEN_SPEOP_TCG_LOGIC_IMM2(evsrwiu, tcg_gen_shri_i32);
8544 GEN_SPEOP_TCG_LOGIC_IMM2(evsrwis, tcg_gen_sari_i32);
8545 GEN_SPEOP_TCG_LOGIC_IMM2(evrlwi, tcg_gen_rotli_i32);
8546
8547 /* SPE arithmetic */
8548 #define GEN_SPEOP_ARITH1(name, tcg_op)                                        \
8549 static inline void gen_##name(DisasContext *ctx)                              \
8550 {                                                                             \
8551     TCGv_i32 t0;                                                              \
8552     if (unlikely(!ctx->spe_enabled)) {                                        \
8553         gen_exception(ctx, POWERPC_EXCP_SPEU);                                \
8554         return;                                                               \
8555     }                                                                         \
8556     t0 = tcg_temp_new_i32();                                                  \
8557                                                                               \
8558     tcg_gen_trunc_tl_i32(t0, cpu_gpr[rA(ctx->opcode)]);                       \
8559     tcg_op(t0, t0);                                                           \
8560     tcg_gen_extu_i32_tl(cpu_gpr[rD(ctx->opcode)], t0);                        \
8561                                                                               \
8562     tcg_gen_trunc_tl_i32(t0, cpu_gprh[rA(ctx->opcode)]);                      \
8563     tcg_op(t0, t0);                                                           \
8564     tcg_gen_extu_i32_tl(cpu_gprh[rD(ctx->opcode)], t0);                       \
8565                                                                               \
8566     tcg_temp_free_i32(t0);                                                    \
8567 }
8568
8569 static inline void gen_op_evabs(TCGv_i32 ret, TCGv_i32 arg1)
8570 {
8571     int l1 = gen_new_label();
8572     int l2 = gen_new_label();
8573
8574     tcg_gen_brcondi_i32(TCG_COND_GE, arg1, 0, l1);
8575     tcg_gen_neg_i32(ret, arg1);
8576     tcg_gen_br(l2);
8577     gen_set_label(l1);
8578     tcg_gen_mov_i32(ret, arg1);
8579     gen_set_label(l2);
8580 }
8581 GEN_SPEOP_ARITH1(evabs, gen_op_evabs);
8582 GEN_SPEOP_ARITH1(evneg, tcg_gen_neg_i32);
8583 GEN_SPEOP_ARITH1(evextsb, tcg_gen_ext8s_i32);
8584 GEN_SPEOP_ARITH1(evextsh, tcg_gen_ext16s_i32);
8585 static inline void gen_op_evrndw(TCGv_i32 ret, TCGv_i32 arg1)
8586 {
8587     tcg_gen_addi_i32(ret, arg1, 0x8000);
8588     tcg_gen_ext16u_i32(ret, ret);
8589 }
8590 GEN_SPEOP_ARITH1(evrndw, gen_op_evrndw);
8591 GEN_SPEOP_ARITH1(evcntlsw, gen_helper_cntlsw32);
8592 GEN_SPEOP_ARITH1(evcntlzw, gen_helper_cntlzw32);
8593
8594 #define GEN_SPEOP_ARITH2(name, tcg_op)                                        \
8595 static inline void gen_##name(DisasContext *ctx)                              \
8596 {                                                                             \
8597     TCGv_i32 t0, t1;                                                          \
8598     if (unlikely(!ctx->spe_enabled)) {                                        \
8599         gen_exception(ctx, POWERPC_EXCP_SPEU);                                \
8600         return;                                                               \
8601     }                                                                         \
8602     t0 = tcg_temp_new_i32();                                                  \
8603     t1 = tcg_temp_new_i32();                                                  \
8604                                                                               \
8605     tcg_gen_trunc_tl_i32(t0, cpu_gpr[rA(ctx->opcode)]);                       \
8606     tcg_gen_trunc_tl_i32(t1, cpu_gpr[rB(ctx->opcode)]);                       \
8607     tcg_op(t0, t0, t1);                                                       \
8608     tcg_gen_extu_i32_tl(cpu_gpr[rD(ctx->opcode)], t0);                        \
8609                                                                               \
8610     tcg_gen_trunc_tl_i32(t0, cpu_gprh[rA(ctx->opcode)]);                      \
8611     tcg_gen_trunc_tl_i32(t1, cpu_gprh[rB(ctx->opcode)]);                      \
8612     tcg_op(t0, t0, t1);                                                       \
8613     tcg_gen_extu_i32_tl(cpu_gprh[rD(ctx->opcode)], t0);                       \
8614                                                                               \
8615     tcg_temp_free_i32(t0);                                                    \
8616     tcg_temp_free_i32(t1);                                                    \
8617 }
8618
8619 static inline void gen_op_evsrwu(TCGv_i32 ret, TCGv_i32 arg1, TCGv_i32 arg2)
8620 {
8621     TCGv_i32 t0;
8622     int l1, l2;
8623
8624     l1 = gen_new_label();
8625     l2 = gen_new_label();
8626     t0 = tcg_temp_local_new_i32();
8627     /* No error here: 6 bits are used */
8628     tcg_gen_andi_i32(t0, arg2, 0x3F);
8629     tcg_gen_brcondi_i32(TCG_COND_GE, t0, 32, l1);
8630     tcg_gen_shr_i32(ret, arg1, t0);
8631     tcg_gen_br(l2);
8632     gen_set_label(l1);
8633     tcg_gen_movi_i32(ret, 0);
8634     gen_set_label(l2);
8635     tcg_temp_free_i32(t0);
8636 }
8637 GEN_SPEOP_ARITH2(evsrwu, gen_op_evsrwu);
8638 static inline void gen_op_evsrws(TCGv_i32 ret, TCGv_i32 arg1, TCGv_i32 arg2)
8639 {
8640     TCGv_i32 t0;
8641     int l1, l2;
8642
8643     l1 = gen_new_label();
8644     l2 = gen_new_label();
8645     t0 = tcg_temp_local_new_i32();
8646     /* No error here: 6 bits are used */
8647     tcg_gen_andi_i32(t0, arg2, 0x3F);
8648     tcg_gen_brcondi_i32(TCG_COND_GE, t0, 32, l1);
8649     tcg_gen_sar_i32(ret, arg1, t0);
8650     tcg_gen_br(l2);
8651     gen_set_label(l1);
8652     tcg_gen_movi_i32(ret, 0);
8653     gen_set_label(l2);
8654     tcg_temp_free_i32(t0);
8655 }
8656 GEN_SPEOP_ARITH2(evsrws, gen_op_evsrws);
8657 static inline void gen_op_evslw(TCGv_i32 ret, TCGv_i32 arg1, TCGv_i32 arg2)
8658 {
8659     TCGv_i32 t0;
8660     int l1, l2;
8661
8662     l1 = gen_new_label();
8663     l2 = gen_new_label();
8664     t0 = tcg_temp_local_new_i32();
8665     /* No error here: 6 bits are used */
8666     tcg_gen_andi_i32(t0, arg2, 0x3F);
8667     tcg_gen_brcondi_i32(TCG_COND_GE, t0, 32, l1);
8668     tcg_gen_shl_i32(ret, arg1, t0);
8669     tcg_gen_br(l2);
8670     gen_set_label(l1);
8671     tcg_gen_movi_i32(ret, 0);
8672     gen_set_label(l2);
8673     tcg_temp_free_i32(t0);
8674 }
8675 GEN_SPEOP_ARITH2(evslw, gen_op_evslw);
8676 static inline void gen_op_evrlw(TCGv_i32 ret, TCGv_i32 arg1, TCGv_i32 arg2)
8677 {
8678     TCGv_i32 t0 = tcg_temp_new_i32();
8679     tcg_gen_andi_i32(t0, arg2, 0x1F);
8680     tcg_gen_rotl_i32(ret, arg1, t0);
8681     tcg_temp_free_i32(t0);
8682 }
8683 GEN_SPEOP_ARITH2(evrlw, gen_op_evrlw);
8684 static inline void gen_evmergehi(DisasContext *ctx)
8685 {
8686     if (unlikely(!ctx->spe_enabled)) {
8687         gen_exception(ctx, POWERPC_EXCP_SPEU);
8688         return;
8689     }
8690     tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_gprh[rB(ctx->opcode)]);
8691     tcg_gen_mov_tl(cpu_gprh[rD(ctx->opcode)], cpu_gprh[rA(ctx->opcode)]);
8692 }
8693 GEN_SPEOP_ARITH2(evaddw, tcg_gen_add_i32);
8694 static inline void gen_op_evsubf(TCGv_i32 ret, TCGv_i32 arg1, TCGv_i32 arg2)
8695 {
8696     tcg_gen_sub_i32(ret, arg2, arg1);
8697 }
8698 GEN_SPEOP_ARITH2(evsubfw, gen_op_evsubf);
8699
8700 /* SPE arithmetic immediate */
8701 #define GEN_SPEOP_ARITH_IMM2(name, tcg_op)                                    \
8702 static inline void gen_##name(DisasContext *ctx)                              \
8703 {                                                                             \
8704     TCGv_i32 t0;                                                              \
8705     if (unlikely(!ctx->spe_enabled)) {                                        \
8706         gen_exception(ctx, POWERPC_EXCP_SPEU);                                \
8707         return;                                                               \
8708     }                                                                         \
8709     t0 = tcg_temp_new_i32();                                                  \
8710                                                                               \
8711     tcg_gen_trunc_tl_i32(t0, cpu_gpr[rB(ctx->opcode)]);                       \
8712     tcg_op(t0, t0, rA(ctx->opcode));                                          \
8713     tcg_gen_extu_i32_tl(cpu_gpr[rD(ctx->opcode)], t0);                        \
8714                                                                               \
8715     tcg_gen_trunc_tl_i32(t0, cpu_gprh[rB(ctx->opcode)]);                      \
8716     tcg_op(t0, t0, rA(ctx->opcode));                                          \
8717     tcg_gen_extu_i32_tl(cpu_gprh[rD(ctx->opcode)], t0);                       \
8718                                                                               \
8719     tcg_temp_free_i32(t0);                                                    \
8720 }
8721 GEN_SPEOP_ARITH_IMM2(evaddiw, tcg_gen_addi_i32);
8722 GEN_SPEOP_ARITH_IMM2(evsubifw, tcg_gen_subi_i32);
8723
8724 /* SPE comparison */
8725 #define GEN_SPEOP_COMP(name, tcg_cond)                                        \
8726 static inline void gen_##name(DisasContext *ctx)                              \
8727 {                                                                             \
8728     if (unlikely(!ctx->spe_enabled)) {                                        \
8729         gen_exception(ctx, POWERPC_EXCP_SPEU);                                \
8730         return;                                                               \
8731     }                                                                         \
8732     int l1 = gen_new_label();                                                 \
8733     int l2 = gen_new_label();                                                 \
8734     int l3 = gen_new_label();                                                 \
8735     int l4 = gen_new_label();                                                 \
8736                                                                               \
8737     tcg_gen_ext32s_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rA(ctx->opcode)]);    \
8738     tcg_gen_ext32s_tl(cpu_gpr[rB(ctx->opcode)], cpu_gpr[rB(ctx->opcode)]);    \
8739     tcg_gen_ext32s_tl(cpu_gprh[rA(ctx->opcode)], cpu_gprh[rA(ctx->opcode)]);  \
8740     tcg_gen_ext32s_tl(cpu_gprh[rB(ctx->opcode)], cpu_gprh[rB(ctx->opcode)]);  \
8741                                                                               \
8742     tcg_gen_brcond_tl(tcg_cond, cpu_gpr[rA(ctx->opcode)],                     \
8743                        cpu_gpr[rB(ctx->opcode)], l1);                         \
8744     tcg_gen_movi_i32(cpu_crf[crfD(ctx->opcode)], 0);                          \
8745     tcg_gen_br(l2);                                                           \
8746     gen_set_label(l1);                                                        \
8747     tcg_gen_movi_i32(cpu_crf[crfD(ctx->opcode)],                              \
8748                      CRF_CL | CRF_CH_OR_CL | CRF_CH_AND_CL);                  \
8749     gen_set_label(l2);                                                        \
8750     tcg_gen_brcond_tl(tcg_cond, cpu_gprh[rA(ctx->opcode)],                    \
8751                        cpu_gprh[rB(ctx->opcode)], l3);                        \
8752     tcg_gen_andi_i32(cpu_crf[crfD(ctx->opcode)], cpu_crf[crfD(ctx->opcode)],  \
8753                      ~(CRF_CH | CRF_CH_AND_CL));                              \
8754     tcg_gen_br(l4);                                                           \
8755     gen_set_label(l3);                                                        \
8756     tcg_gen_ori_i32(cpu_crf[crfD(ctx->opcode)], cpu_crf[crfD(ctx->opcode)],   \
8757                     CRF_CH | CRF_CH_OR_CL);                                   \
8758     gen_set_label(l4);                                                        \
8759 }
8760 GEN_SPEOP_COMP(evcmpgtu, TCG_COND_GTU);
8761 GEN_SPEOP_COMP(evcmpgts, TCG_COND_GT);
8762 GEN_SPEOP_COMP(evcmpltu, TCG_COND_LTU);
8763 GEN_SPEOP_COMP(evcmplts, TCG_COND_LT);
8764 GEN_SPEOP_COMP(evcmpeq, TCG_COND_EQ);
8765
8766 /* SPE misc */
8767 static inline void gen_brinc(DisasContext *ctx)
8768 {
8769     /* Note: brinc is usable even if SPE is disabled */
8770     gen_helper_brinc(cpu_gpr[rD(ctx->opcode)],
8771                      cpu_gpr[rA(ctx->opcode)], cpu_gpr[rB(ctx->opcode)]);
8772 }
8773 static inline void gen_evmergelo(DisasContext *ctx)
8774 {
8775     if (unlikely(!ctx->spe_enabled)) {
8776         gen_exception(ctx, POWERPC_EXCP_SPEU);
8777         return;
8778     }
8779     tcg_gen_mov_tl(cpu_gprh[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)]);
8780     tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rB(ctx->opcode)]);
8781 }
8782 static inline void gen_evmergehilo(DisasContext *ctx)
8783 {
8784     if (unlikely(!ctx->spe_enabled)) {
8785         gen_exception(ctx, POWERPC_EXCP_SPEU);
8786         return;
8787     }
8788     tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rB(ctx->opcode)]);
8789     tcg_gen_mov_tl(cpu_gprh[rD(ctx->opcode)], cpu_gprh[rA(ctx->opcode)]);
8790 }
8791 static inline void gen_evmergelohi(DisasContext *ctx)
8792 {
8793     if (unlikely(!ctx->spe_enabled)) {
8794         gen_exception(ctx, POWERPC_EXCP_SPEU);
8795         return;
8796     }
8797     if (rD(ctx->opcode) == rA(ctx->opcode)) {
8798         TCGv tmp = tcg_temp_new();
8799         tcg_gen_mov_tl(tmp, cpu_gpr[rA(ctx->opcode)]);
8800         tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_gprh[rB(ctx->opcode)]);
8801         tcg_gen_mov_tl(cpu_gprh[rD(ctx->opcode)], tmp);
8802         tcg_temp_free(tmp);
8803     } else {
8804         tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_gprh[rB(ctx->opcode)]);
8805         tcg_gen_mov_tl(cpu_gprh[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)]);
8806     }
8807 }
8808 static inline void gen_evsplati(DisasContext *ctx)
8809 {
8810     uint64_t imm = ((int32_t)(rA(ctx->opcode) << 27)) >> 27;
8811
8812     tcg_gen_movi_tl(cpu_gpr[rD(ctx->opcode)], imm);
8813     tcg_gen_movi_tl(cpu_gprh[rD(ctx->opcode)], imm);
8814 }
8815 static inline void gen_evsplatfi(DisasContext *ctx)
8816 {
8817     uint64_t imm = rA(ctx->opcode) << 27;
8818
8819     tcg_gen_movi_tl(cpu_gpr[rD(ctx->opcode)], imm);
8820     tcg_gen_movi_tl(cpu_gprh[rD(ctx->opcode)], imm);
8821 }
8822
8823 static inline void gen_evsel(DisasContext *ctx)
8824 {
8825     int l1 = gen_new_label();
8826     int l2 = gen_new_label();
8827     int l3 = gen_new_label();
8828     int l4 = gen_new_label();
8829     TCGv_i32 t0 = tcg_temp_local_new_i32();
8830     tcg_gen_andi_i32(t0, cpu_crf[ctx->opcode & 0x07], 1 << 3);
8831     tcg_gen_brcondi_i32(TCG_COND_EQ, t0, 0, l1);
8832     tcg_gen_mov_tl(cpu_gprh[rD(ctx->opcode)], cpu_gprh[rA(ctx->opcode)]);
8833     tcg_gen_br(l2);
8834     gen_set_label(l1);
8835     tcg_gen_mov_tl(cpu_gprh[rD(ctx->opcode)], cpu_gprh[rB(ctx->opcode)]);
8836     gen_set_label(l2);
8837     tcg_gen_andi_i32(t0, cpu_crf[ctx->opcode & 0x07], 1 << 2);
8838     tcg_gen_brcondi_i32(TCG_COND_EQ, t0, 0, l3);
8839     tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)]);
8840     tcg_gen_br(l4);
8841     gen_set_label(l3);
8842     tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rB(ctx->opcode)]);
8843     gen_set_label(l4);
8844     tcg_temp_free_i32(t0);
8845 }
8846
8847 static void gen_evsel0(DisasContext *ctx)
8848 {
8849     gen_evsel(ctx);
8850 }
8851
8852 static void gen_evsel1(DisasContext *ctx)
8853 {
8854     gen_evsel(ctx);
8855 }
8856
8857 static void gen_evsel2(DisasContext *ctx)
8858 {
8859     gen_evsel(ctx);
8860 }
8861
8862 static void gen_evsel3(DisasContext *ctx)
8863 {
8864     gen_evsel(ctx);
8865 }
8866
8867 /* Multiply */
8868
8869 static inline void gen_evmwumi(DisasContext *ctx)
8870 {
8871     TCGv_i64 t0, t1;
8872
8873     if (unlikely(!ctx->spe_enabled)) {
8874         gen_exception(ctx, POWERPC_EXCP_SPEU);
8875         return;
8876     }
8877
8878     t0 = tcg_temp_new_i64();
8879     t1 = tcg_temp_new_i64();
8880
8881     /* t0 := rA; t1 := rB */
8882     tcg_gen_extu_tl_i64(t0, cpu_gpr[rA(ctx->opcode)]);
8883     tcg_gen_ext32u_i64(t0, t0);
8884     tcg_gen_extu_tl_i64(t1, cpu_gpr[rB(ctx->opcode)]);
8885     tcg_gen_ext32u_i64(t1, t1);
8886
8887     tcg_gen_mul_i64(t0, t0, t1);  /* t0 := rA * rB */
8888
8889     gen_store_gpr64(rD(ctx->opcode), t0); /* rD := t0 */
8890
8891     tcg_temp_free_i64(t0);
8892     tcg_temp_free_i64(t1);
8893 }
8894
8895 static inline void gen_evmwumia(DisasContext *ctx)
8896 {
8897     TCGv_i64 tmp;
8898
8899     if (unlikely(!ctx->spe_enabled)) {
8900         gen_exception(ctx, POWERPC_EXCP_SPEU);
8901         return;
8902     }
8903
8904     gen_evmwumi(ctx);            /* rD := rA * rB */
8905
8906     tmp = tcg_temp_new_i64();
8907
8908     /* acc := rD */
8909     gen_load_gpr64(tmp, rD(ctx->opcode));
8910     tcg_gen_st_i64(tmp, cpu_env, offsetof(CPUPPCState, spe_acc));
8911     tcg_temp_free_i64(tmp);
8912 }
8913
8914 static inline void gen_evmwumiaa(DisasContext *ctx)
8915 {
8916     TCGv_i64 acc;
8917     TCGv_i64 tmp;
8918
8919     if (unlikely(!ctx->spe_enabled)) {
8920         gen_exception(ctx, POWERPC_EXCP_SPEU);
8921         return;
8922     }
8923
8924     gen_evmwumi(ctx);           /* rD := rA * rB */
8925
8926     acc = tcg_temp_new_i64();
8927     tmp = tcg_temp_new_i64();
8928
8929     /* tmp := rD */
8930     gen_load_gpr64(tmp, rD(ctx->opcode));
8931
8932     /* Load acc */
8933     tcg_gen_ld_i64(acc, cpu_env, offsetof(CPUPPCState, spe_acc));
8934
8935     /* acc := tmp + acc */
8936     tcg_gen_add_i64(acc, acc, tmp);
8937
8938     /* Store acc */
8939     tcg_gen_st_i64(acc, cpu_env, offsetof(CPUPPCState, spe_acc));
8940
8941     /* rD := acc */
8942     gen_store_gpr64(rD(ctx->opcode), acc);
8943
8944     tcg_temp_free_i64(acc);
8945     tcg_temp_free_i64(tmp);
8946 }
8947
8948 static inline void gen_evmwsmi(DisasContext *ctx)
8949 {
8950     TCGv_i64 t0, t1;
8951
8952     if (unlikely(!ctx->spe_enabled)) {
8953         gen_exception(ctx, POWERPC_EXCP_SPEU);
8954         return;
8955     }
8956
8957     t0 = tcg_temp_new_i64();
8958     t1 = tcg_temp_new_i64();
8959
8960     /* t0 := rA; t1 := rB */
8961     tcg_gen_extu_tl_i64(t0, cpu_gpr[rA(ctx->opcode)]);
8962     tcg_gen_ext32s_i64(t0, t0);
8963     tcg_gen_extu_tl_i64(t1, cpu_gpr[rB(ctx->opcode)]);
8964     tcg_gen_ext32s_i64(t1, t1);
8965
8966     tcg_gen_mul_i64(t0, t0, t1);  /* t0 := rA * rB */
8967
8968     gen_store_gpr64(rD(ctx->opcode), t0); /* rD := t0 */
8969
8970     tcg_temp_free_i64(t0);
8971     tcg_temp_free_i64(t1);
8972 }
8973
8974 static inline void gen_evmwsmia(DisasContext *ctx)
8975 {
8976     TCGv_i64 tmp;
8977
8978     gen_evmwsmi(ctx);            /* rD := rA * rB */
8979
8980     tmp = tcg_temp_new_i64();
8981
8982     /* acc := rD */
8983     gen_load_gpr64(tmp, rD(ctx->opcode));
8984     tcg_gen_st_i64(tmp, cpu_env, offsetof(CPUPPCState, spe_acc));
8985
8986     tcg_temp_free_i64(tmp);
8987 }
8988
8989 static inline void gen_evmwsmiaa(DisasContext *ctx)
8990 {
8991     TCGv_i64 acc = tcg_temp_new_i64();
8992     TCGv_i64 tmp = tcg_temp_new_i64();
8993
8994     gen_evmwsmi(ctx);           /* rD := rA * rB */
8995
8996     acc = tcg_temp_new_i64();
8997     tmp = tcg_temp_new_i64();
8998
8999     /* tmp := rD */
9000     gen_load_gpr64(tmp, rD(ctx->opcode));
9001
9002     /* Load acc */
9003     tcg_gen_ld_i64(acc, cpu_env, offsetof(CPUPPCState, spe_acc));
9004
9005     /* acc := tmp + acc */
9006     tcg_gen_add_i64(acc, acc, tmp);
9007
9008     /* Store acc */
9009     tcg_gen_st_i64(acc, cpu_env, offsetof(CPUPPCState, spe_acc));
9010
9011     /* rD := acc */
9012     gen_store_gpr64(rD(ctx->opcode), acc);
9013
9014     tcg_temp_free_i64(acc);
9015     tcg_temp_free_i64(tmp);
9016 }
9017
9018 GEN_SPE(evaddw,      speundef,    0x00, 0x08, 0x00000000, 0xFFFFFFFF, PPC_SPE); ////
9019 GEN_SPE(evaddiw,     speundef,    0x01, 0x08, 0x00000000, 0xFFFFFFFF, PPC_SPE);
9020 GEN_SPE(evsubfw,     speundef,    0x02, 0x08, 0x00000000, 0xFFFFFFFF, PPC_SPE); ////
9021 GEN_SPE(evsubifw,    speundef,    0x03, 0x08, 0x00000000, 0xFFFFFFFF, PPC_SPE);
9022 GEN_SPE(evabs,       evneg,       0x04, 0x08, 0x0000F800, 0x0000F800, PPC_SPE); ////
9023 GEN_SPE(evextsb,     evextsh,     0x05, 0x08, 0x0000F800, 0x0000F800, PPC_SPE); ////
9024 GEN_SPE(evrndw,      evcntlzw,    0x06, 0x08, 0x0000F800, 0x0000F800, PPC_SPE); ////
9025 GEN_SPE(evcntlsw,    brinc,       0x07, 0x08, 0x0000F800, 0x00000000, PPC_SPE); //
9026 GEN_SPE(evmra,       speundef,    0x02, 0x13, 0x0000F800, 0xFFFFFFFF, PPC_SPE);
9027 GEN_SPE(speundef,    evand,       0x08, 0x08, 0xFFFFFFFF, 0x00000000, PPC_SPE); ////
9028 GEN_SPE(evandc,      speundef,    0x09, 0x08, 0x00000000, 0xFFFFFFFF, PPC_SPE); ////
9029 GEN_SPE(evxor,       evor,        0x0B, 0x08, 0x00000000, 0x00000000, PPC_SPE); ////
9030 GEN_SPE(evnor,       eveqv,       0x0C, 0x08, 0x00000000, 0x00000000, PPC_SPE); ////
9031 GEN_SPE(evmwumi,     evmwsmi,     0x0C, 0x11, 0x00000000, 0x00000000, PPC_SPE);
9032 GEN_SPE(evmwumia,    evmwsmia,    0x1C, 0x11, 0x00000000, 0x00000000, PPC_SPE);
9033 GEN_SPE(evmwumiaa,   evmwsmiaa,   0x0C, 0x15, 0x00000000, 0x00000000, PPC_SPE);
9034 GEN_SPE(speundef,    evorc,       0x0D, 0x08, 0xFFFFFFFF, 0x00000000, PPC_SPE); ////
9035 GEN_SPE(evnand,      speundef,    0x0F, 0x08, 0x00000000, 0xFFFFFFFF, PPC_SPE); ////
9036 GEN_SPE(evsrwu,      evsrws,      0x10, 0x08, 0x00000000, 0x00000000, PPC_SPE); ////
9037 GEN_SPE(evsrwiu,     evsrwis,     0x11, 0x08, 0x00000000, 0x00000000, PPC_SPE);
9038 GEN_SPE(evslw,       speundef,    0x12, 0x08, 0x00000000, 0xFFFFFFFF, PPC_SPE); ////
9039 GEN_SPE(evslwi,      speundef,    0x13, 0x08, 0x00000000, 0xFFFFFFFF, PPC_SPE);
9040 GEN_SPE(evrlw,       evsplati,    0x14, 0x08, 0x00000000, 0x0000F800, PPC_SPE); //
9041 GEN_SPE(evrlwi,      evsplatfi,   0x15, 0x08, 0x00000000, 0x0000F800, PPC_SPE);
9042 GEN_SPE(evmergehi,   evmergelo,   0x16, 0x08, 0x00000000, 0x00000000, PPC_SPE); ////
9043 GEN_SPE(evmergehilo, evmergelohi, 0x17, 0x08, 0x00000000, 0x00000000, PPC_SPE); ////
9044 GEN_SPE(evcmpgtu,    evcmpgts,    0x18, 0x08, 0x00600000, 0x00600000, PPC_SPE); ////
9045 GEN_SPE(evcmpltu,    evcmplts,    0x19, 0x08, 0x00600000, 0x00600000, PPC_SPE); ////
9046 GEN_SPE(evcmpeq,     speundef,    0x1A, 0x08, 0x00600000, 0xFFFFFFFF, PPC_SPE); ////
9047
9048 /* SPE load and stores */
9049 static inline void gen_addr_spe_imm_index(DisasContext *ctx, TCGv EA, int sh)
9050 {
9051     target_ulong uimm = rB(ctx->opcode);
9052
9053     if (rA(ctx->opcode) == 0) {
9054         tcg_gen_movi_tl(EA, uimm << sh);
9055     } else {
9056         tcg_gen_addi_tl(EA, cpu_gpr[rA(ctx->opcode)], uimm << sh);
9057         if (NARROW_MODE(ctx)) {
9058             tcg_gen_ext32u_tl(EA, EA);
9059         }
9060     }
9061 }
9062
9063 static inline void gen_op_evldd(DisasContext *ctx, TCGv addr)
9064 {
9065     TCGv_i64 t0 = tcg_temp_new_i64();
9066     gen_qemu_ld64(ctx, t0, addr);
9067     gen_store_gpr64(rD(ctx->opcode), t0);
9068     tcg_temp_free_i64(t0);
9069 }
9070
9071 static inline void gen_op_evldw(DisasContext *ctx, TCGv addr)
9072 {
9073     gen_qemu_ld32u(ctx, cpu_gprh[rD(ctx->opcode)], addr);
9074     gen_addr_add(ctx, addr, addr, 4);
9075     gen_qemu_ld32u(ctx, cpu_gpr[rD(ctx->opcode)], addr);
9076 }
9077
9078 static inline void gen_op_evldh(DisasContext *ctx, TCGv addr)
9079 {
9080     TCGv t0 = tcg_temp_new();
9081     gen_qemu_ld16u(ctx, t0, addr);
9082     tcg_gen_shli_tl(cpu_gprh[rD(ctx->opcode)], t0, 16);
9083     gen_addr_add(ctx, addr, addr, 2);
9084     gen_qemu_ld16u(ctx, t0, addr);
9085     tcg_gen_or_tl(cpu_gprh[rD(ctx->opcode)], cpu_gprh[rD(ctx->opcode)], t0);
9086     gen_addr_add(ctx, addr, addr, 2);
9087     gen_qemu_ld16u(ctx, t0, addr);
9088     tcg_gen_shli_tl(cpu_gprh[rD(ctx->opcode)], t0, 16);
9089     gen_addr_add(ctx, addr, addr, 2);
9090     gen_qemu_ld16u(ctx, t0, addr);
9091     tcg_gen_or_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rD(ctx->opcode)], t0);
9092     tcg_temp_free(t0);
9093 }
9094
9095 static inline void gen_op_evlhhesplat(DisasContext *ctx, TCGv addr)
9096 {
9097     TCGv t0 = tcg_temp_new();
9098     gen_qemu_ld16u(ctx, t0, addr);
9099     tcg_gen_shli_tl(t0, t0, 16);
9100     tcg_gen_mov_tl(cpu_gprh[rD(ctx->opcode)], t0);
9101     tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], t0);
9102     tcg_temp_free(t0);
9103 }
9104
9105 static inline void gen_op_evlhhousplat(DisasContext *ctx, TCGv addr)
9106 {
9107     TCGv t0 = tcg_temp_new();
9108     gen_qemu_ld16u(ctx, t0, addr);
9109     tcg_gen_mov_tl(cpu_gprh[rD(ctx->opcode)], t0);
9110     tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], t0);
9111     tcg_temp_free(t0);
9112 }
9113
9114 static inline void gen_op_evlhhossplat(DisasContext *ctx, TCGv addr)
9115 {
9116     TCGv t0 = tcg_temp_new();
9117     gen_qemu_ld16s(ctx, t0, addr);
9118     tcg_gen_mov_tl(cpu_gprh[rD(ctx->opcode)], t0);
9119     tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], t0);
9120     tcg_temp_free(t0);
9121 }
9122
9123 static inline void gen_op_evlwhe(DisasContext *ctx, TCGv addr)
9124 {
9125     TCGv t0 = tcg_temp_new();
9126     gen_qemu_ld16u(ctx, t0, addr);
9127     tcg_gen_shli_tl(cpu_gprh[rD(ctx->opcode)], t0, 16);
9128     gen_addr_add(ctx, addr, addr, 2);
9129     gen_qemu_ld16u(ctx, t0, addr);
9130     tcg_gen_shli_tl(cpu_gpr[rD(ctx->opcode)], t0, 16);
9131     tcg_temp_free(t0);
9132 }
9133
9134 static inline void gen_op_evlwhou(DisasContext *ctx, TCGv addr)
9135 {
9136     gen_qemu_ld16u(ctx, cpu_gprh[rD(ctx->opcode)], addr);
9137     gen_addr_add(ctx, addr, addr, 2);
9138     gen_qemu_ld16u(ctx, cpu_gpr[rD(ctx->opcode)], addr);
9139 }
9140
9141 static inline void gen_op_evlwhos(DisasContext *ctx, TCGv addr)
9142 {
9143     gen_qemu_ld16s(ctx, cpu_gprh[rD(ctx->opcode)], addr);
9144     gen_addr_add(ctx, addr, addr, 2);
9145     gen_qemu_ld16s(ctx, cpu_gpr[rD(ctx->opcode)], addr);
9146 }
9147
9148 static inline void gen_op_evlwwsplat(DisasContext *ctx, TCGv addr)
9149 {
9150     TCGv t0 = tcg_temp_new();
9151     gen_qemu_ld32u(ctx, t0, addr);
9152     tcg_gen_mov_tl(cpu_gprh[rD(ctx->opcode)], t0);
9153     tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], t0);
9154     tcg_temp_free(t0);
9155 }
9156
9157 static inline void gen_op_evlwhsplat(DisasContext *ctx, TCGv addr)
9158 {
9159     TCGv t0 = tcg_temp_new();
9160     gen_qemu_ld16u(ctx, t0, addr);
9161     tcg_gen_shli_tl(cpu_gprh[rD(ctx->opcode)], t0, 16);
9162     tcg_gen_or_tl(cpu_gprh[rD(ctx->opcode)], cpu_gprh[rD(ctx->opcode)], t0);
9163     gen_addr_add(ctx, addr, addr, 2);
9164     gen_qemu_ld16u(ctx, t0, addr);
9165     tcg_gen_shli_tl(cpu_gpr[rD(ctx->opcode)], t0, 16);
9166     tcg_gen_or_tl(cpu_gpr[rD(ctx->opcode)], cpu_gprh[rD(ctx->opcode)], t0);
9167     tcg_temp_free(t0);
9168 }
9169
9170 static inline void gen_op_evstdd(DisasContext *ctx, TCGv addr)
9171 {
9172     TCGv_i64 t0 = tcg_temp_new_i64();
9173     gen_load_gpr64(t0, rS(ctx->opcode));
9174     gen_qemu_st64(ctx, t0, addr);
9175     tcg_temp_free_i64(t0);
9176 }
9177
9178 static inline void gen_op_evstdw(DisasContext *ctx, TCGv addr)
9179 {
9180     gen_qemu_st32(ctx, cpu_gprh[rS(ctx->opcode)], addr);
9181     gen_addr_add(ctx, addr, addr, 4);
9182     gen_qemu_st32(ctx, cpu_gpr[rS(ctx->opcode)], addr);
9183 }
9184
9185 static inline void gen_op_evstdh(DisasContext *ctx, TCGv addr)
9186 {
9187     TCGv t0 = tcg_temp_new();
9188     tcg_gen_shri_tl(t0, cpu_gprh[rS(ctx->opcode)], 16);
9189     gen_qemu_st16(ctx, t0, addr);
9190     gen_addr_add(ctx, addr, addr, 2);
9191     gen_qemu_st16(ctx, cpu_gprh[rS(ctx->opcode)], addr);
9192     gen_addr_add(ctx, addr, addr, 2);
9193     tcg_gen_shri_tl(t0, cpu_gpr[rS(ctx->opcode)], 16);
9194     gen_qemu_st16(ctx, t0, addr);
9195     tcg_temp_free(t0);
9196     gen_addr_add(ctx, addr, addr, 2);
9197     gen_qemu_st16(ctx, cpu_gpr[rS(ctx->opcode)], addr);
9198 }
9199
9200 static inline void gen_op_evstwhe(DisasContext *ctx, TCGv addr)
9201 {
9202     TCGv t0 = tcg_temp_new();
9203     tcg_gen_shri_tl(t0, cpu_gprh[rS(ctx->opcode)], 16);
9204     gen_qemu_st16(ctx, t0, addr);
9205     gen_addr_add(ctx, addr, addr, 2);
9206     tcg_gen_shri_tl(t0, cpu_gpr[rS(ctx->opcode)], 16);
9207     gen_qemu_st16(ctx, t0, addr);
9208     tcg_temp_free(t0);
9209 }
9210
9211 static inline void gen_op_evstwho(DisasContext *ctx, TCGv addr)
9212 {
9213     gen_qemu_st16(ctx, cpu_gprh[rS(ctx->opcode)], addr);
9214     gen_addr_add(ctx, addr, addr, 2);
9215     gen_qemu_st16(ctx, cpu_gpr[rS(ctx->opcode)], addr);
9216 }
9217
9218 static inline void gen_op_evstwwe(DisasContext *ctx, TCGv addr)
9219 {
9220     gen_qemu_st32(ctx, cpu_gprh[rS(ctx->opcode)], addr);
9221 }
9222
9223 static inline void gen_op_evstwwo(DisasContext *ctx, TCGv addr)
9224 {
9225     gen_qemu_st32(ctx, cpu_gpr[rS(ctx->opcode)], addr);
9226 }
9227
9228 #define GEN_SPEOP_LDST(name, opc2, sh)                                        \
9229 static void glue(gen_, name)(DisasContext *ctx)                                       \
9230 {                                                                             \
9231     TCGv t0;                                                                  \
9232     if (unlikely(!ctx->spe_enabled)) {                                        \
9233         gen_exception(ctx, POWERPC_EXCP_SPEU);                                \
9234         return;                                                               \
9235     }                                                                         \
9236     gen_set_access_type(ctx, ACCESS_INT);                                     \
9237     t0 = tcg_temp_new();                                                      \
9238     if (Rc(ctx->opcode)) {                                                    \
9239         gen_addr_spe_imm_index(ctx, t0, sh);                                  \
9240     } else {                                                                  \
9241         gen_addr_reg_index(ctx, t0);                                          \
9242     }                                                                         \
9243     gen_op_##name(ctx, t0);                                                   \
9244     tcg_temp_free(t0);                                                        \
9245 }
9246
9247 GEN_SPEOP_LDST(evldd, 0x00, 3);
9248 GEN_SPEOP_LDST(evldw, 0x01, 3);
9249 GEN_SPEOP_LDST(evldh, 0x02, 3);
9250 GEN_SPEOP_LDST(evlhhesplat, 0x04, 1);
9251 GEN_SPEOP_LDST(evlhhousplat, 0x06, 1);
9252 GEN_SPEOP_LDST(evlhhossplat, 0x07, 1);
9253 GEN_SPEOP_LDST(evlwhe, 0x08, 2);
9254 GEN_SPEOP_LDST(evlwhou, 0x0A, 2);
9255 GEN_SPEOP_LDST(evlwhos, 0x0B, 2);
9256 GEN_SPEOP_LDST(evlwwsplat, 0x0C, 2);
9257 GEN_SPEOP_LDST(evlwhsplat, 0x0E, 2);
9258
9259 GEN_SPEOP_LDST(evstdd, 0x10, 3);
9260 GEN_SPEOP_LDST(evstdw, 0x11, 3);
9261 GEN_SPEOP_LDST(evstdh, 0x12, 3);
9262 GEN_SPEOP_LDST(evstwhe, 0x18, 2);
9263 GEN_SPEOP_LDST(evstwho, 0x1A, 2);
9264 GEN_SPEOP_LDST(evstwwe, 0x1C, 2);
9265 GEN_SPEOP_LDST(evstwwo, 0x1E, 2);
9266
9267 /* Multiply and add - TODO */
9268 #if 0
9269 GEN_SPE(speundef,       evmhessf,      0x01, 0x10, 0xFFFFFFFF, 0x00000000, PPC_SPE);//
9270 GEN_SPE(speundef,       evmhossf,      0x03, 0x10, 0xFFFFFFFF, 0x00000000, PPC_SPE);
9271 GEN_SPE(evmheumi,       evmhesmi,      0x04, 0x10, 0x00000000, 0x00000000, PPC_SPE);
9272 GEN_SPE(speundef,       evmhesmf,      0x05, 0x10, 0xFFFFFFFF, 0x00000000, PPC_SPE);
9273 GEN_SPE(evmhoumi,       evmhosmi,      0x06, 0x10, 0x00000000, 0x00000000, PPC_SPE);
9274 GEN_SPE(speundef,       evmhosmf,      0x07, 0x10, 0xFFFFFFFF, 0x00000000, PPC_SPE);
9275 GEN_SPE(speundef,       evmhessfa,     0x11, 0x10, 0xFFFFFFFF, 0x00000000, PPC_SPE);
9276 GEN_SPE(speundef,       evmhossfa,     0x13, 0x10, 0xFFFFFFFF, 0x00000000, PPC_SPE);
9277 GEN_SPE(evmheumia,      evmhesmia,     0x14, 0x10, 0x00000000, 0x00000000, PPC_SPE);
9278 GEN_SPE(speundef,       evmhesmfa,     0x15, 0x10, 0xFFFFFFFF, 0x00000000, PPC_SPE);
9279 GEN_SPE(evmhoumia,      evmhosmia,     0x16, 0x10, 0x00000000, 0x00000000, PPC_SPE);
9280 GEN_SPE(speundef,       evmhosmfa,     0x17, 0x10, 0xFFFFFFFF, 0x00000000, PPC_SPE);
9281
9282 GEN_SPE(speundef,       evmwhssf,      0x03, 0x11, 0xFFFFFFFF, 0x00000000, PPC_SPE);
9283 GEN_SPE(evmwlumi,       speundef,      0x04, 0x11, 0x00000000, 0xFFFFFFFF, PPC_SPE);
9284 GEN_SPE(evmwhumi,       evmwhsmi,      0x06, 0x11, 0x00000000, 0x00000000, PPC_SPE);
9285 GEN_SPE(speundef,       evmwhsmf,      0x07, 0x11, 0xFFFFFFFF, 0x00000000, PPC_SPE);
9286 GEN_SPE(speundef,       evmwssf,       0x09, 0x11, 0xFFFFFFFF, 0x00000000, PPC_SPE);
9287 GEN_SPE(speundef,       evmwsmf,       0x0D, 0x11, 0xFFFFFFFF, 0x00000000, PPC_SPE);
9288 GEN_SPE(speundef,       evmwhssfa,     0x13, 0x11, 0xFFFFFFFF, 0x00000000, PPC_SPE);
9289 GEN_SPE(evmwlumia,      speundef,      0x14, 0x11, 0x00000000, 0xFFFFFFFF, PPC_SPE);
9290 GEN_SPE(evmwhumia,      evmwhsmia,     0x16, 0x11, 0x00000000, 0x00000000, PPC_SPE);
9291 GEN_SPE(speundef,       evmwhsmfa,     0x17, 0x11, 0xFFFFFFFF, 0x00000000, PPC_SPE);
9292 GEN_SPE(speundef,       evmwssfa,      0x19, 0x11, 0xFFFFFFFF, 0x00000000, PPC_SPE);
9293 GEN_SPE(speundef,       evmwsmfa,      0x1D, 0x11, 0xFFFFFFFF, 0x00000000, PPC_SPE);
9294
9295 GEN_SPE(evadduiaaw,     evaddsiaaw,    0x00, 0x13, 0x0000F800, 0x0000F800, PPC_SPE);
9296 GEN_SPE(evsubfusiaaw,   evsubfssiaaw,  0x01, 0x13, 0x0000F800, 0x0000F800, PPC_SPE);
9297 GEN_SPE(evaddumiaaw,    evaddsmiaaw,   0x04, 0x13, 0x0000F800, 0x0000F800, PPC_SPE);
9298 GEN_SPE(evsubfumiaaw,   evsubfsmiaaw,  0x05, 0x13, 0x0000F800, 0x0000F800, PPC_SPE);
9299 GEN_SPE(evdivws,        evdivwu,       0x06, 0x13, 0x00000000, 0x00000000, PPC_SPE);
9300
9301 GEN_SPE(evmheusiaaw,    evmhessiaaw,   0x00, 0x14, 0x00000000, 0x00000000, PPC_SPE);
9302 GEN_SPE(speundef,       evmhessfaaw,   0x01, 0x14, 0xFFFFFFFF, 0x00000000, PPC_SPE);
9303 GEN_SPE(evmhousiaaw,    evmhossiaaw,   0x02, 0x14, 0x00000000, 0x00000000, PPC_SPE);
9304 GEN_SPE(speundef,       evmhossfaaw,   0x03, 0x14, 0xFFFFFFFF, 0x00000000, PPC_SPE);
9305 GEN_SPE(evmheumiaaw,    evmhesmiaaw,   0x04, 0x14, 0x00000000, 0x00000000, PPC_SPE);
9306 GEN_SPE(speundef,       evmhesmfaaw,   0x05, 0x14, 0xFFFFFFFF, 0x00000000, PPC_SPE);
9307 GEN_SPE(evmhoumiaaw,    evmhosmiaaw,   0x06, 0x14, 0x00000000, 0x00000000, PPC_SPE);
9308 GEN_SPE(speundef,       evmhosmfaaw,   0x07, 0x14, 0xFFFFFFFF, 0x00000000, PPC_SPE);
9309 GEN_SPE(evmhegumiaa,    evmhegsmiaa,   0x14, 0x14, 0x00000000, 0x00000000, PPC_SPE);
9310 GEN_SPE(speundef,       evmhegsmfaa,   0x15, 0x14, 0xFFFFFFFF, 0x00000000, PPC_SPE);
9311 GEN_SPE(evmhogumiaa,    evmhogsmiaa,   0x16, 0x14, 0x00000000, 0x00000000, PPC_SPE);
9312 GEN_SPE(speundef,       evmhogsmfaa,   0x17, 0x14, 0xFFFFFFFF, 0x00000000, PPC_SPE);
9313
9314 GEN_SPE(evmwlusiaaw,    evmwlssiaaw,   0x00, 0x15, 0x00000000, 0x00000000, PPC_SPE);
9315 GEN_SPE(evmwlumiaaw,    evmwlsmiaaw,   0x04, 0x15, 0x00000000, 0x00000000, PPC_SPE);
9316 GEN_SPE(speundef,       evmwssfaa,     0x09, 0x15, 0xFFFFFFFF, 0x00000000, PPC_SPE);
9317 GEN_SPE(speundef,       evmwsmfaa,     0x0D, 0x15, 0xFFFFFFFF, 0x00000000, PPC_SPE);
9318
9319 GEN_SPE(evmheusianw,    evmhessianw,   0x00, 0x16, 0x00000000, 0x00000000, PPC_SPE);
9320 GEN_SPE(speundef,       evmhessfanw,   0x01, 0x16, 0xFFFFFFFF, 0x00000000, PPC_SPE);
9321 GEN_SPE(evmhousianw,    evmhossianw,   0x02, 0x16, 0x00000000, 0x00000000, PPC_SPE);
9322 GEN_SPE(speundef,       evmhossfanw,   0x03, 0x16, 0xFFFFFFFF, 0x00000000, PPC_SPE);
9323 GEN_SPE(evmheumianw,    evmhesmianw,   0x04, 0x16, 0x00000000, 0x00000000, PPC_SPE);
9324 GEN_SPE(speundef,       evmhesmfanw,   0x05, 0x16, 0xFFFFFFFF, 0x00000000, PPC_SPE);
9325 GEN_SPE(evmhoumianw,    evmhosmianw,   0x06, 0x16, 0x00000000, 0x00000000, PPC_SPE);
9326 GEN_SPE(speundef,       evmhosmfanw,   0x07, 0x16, 0xFFFFFFFF, 0x00000000, PPC_SPE);
9327 GEN_SPE(evmhegumian,    evmhegsmian,   0x14, 0x16, 0x00000000, 0x00000000, PPC_SPE);
9328 GEN_SPE(speundef,       evmhegsmfan,   0x15, 0x16, 0xFFFFFFFF, 0x00000000, PPC_SPE);
9329 GEN_SPE(evmhigumian,    evmhigsmian,   0x16, 0x16, 0x00000000, 0x00000000, PPC_SPE);
9330 GEN_SPE(speundef,       evmhogsmfan,   0x17, 0x16, 0xFFFFFFFF, 0x00000000, PPC_SPE);
9331
9332 GEN_SPE(evmwlusianw,    evmwlssianw,   0x00, 0x17, 0x00000000, 0x00000000, PPC_SPE);
9333 GEN_SPE(evmwlumianw,    evmwlsmianw,   0x04, 0x17, 0x00000000, 0x00000000, PPC_SPE);
9334 GEN_SPE(speundef,       evmwssfan,     0x09, 0x17, 0xFFFFFFFF, 0x00000000, PPC_SPE);
9335 GEN_SPE(evmwumian,      evmwsmian,     0x0C, 0x17, 0x00000000, 0x00000000, PPC_SPE);
9336 GEN_SPE(speundef,       evmwsmfan,     0x0D, 0x17, 0xFFFFFFFF, 0x00000000, PPC_SPE);
9337 #endif
9338
9339 /***                      SPE floating-point extension                     ***/
9340 #define GEN_SPEFPUOP_CONV_32_32(name)                                         \
9341 static inline void gen_##name(DisasContext *ctx)                              \
9342 {                                                                             \
9343     TCGv_i32 t0 = tcg_temp_new_i32();                                         \
9344     tcg_gen_trunc_tl_i32(t0, cpu_gpr[rB(ctx->opcode)]);                       \
9345     gen_helper_##name(t0, cpu_env, t0);                                       \
9346     tcg_gen_extu_i32_tl(cpu_gpr[rD(ctx->opcode)], t0);                        \
9347     tcg_temp_free_i32(t0);                                                    \
9348 }
9349 #define GEN_SPEFPUOP_CONV_32_64(name)                                         \
9350 static inline void gen_##name(DisasContext *ctx)                              \
9351 {                                                                             \
9352     TCGv_i64 t0 = tcg_temp_new_i64();                                         \
9353     TCGv_i32 t1 = tcg_temp_new_i32();                                         \
9354     gen_load_gpr64(t0, rB(ctx->opcode));                                      \
9355     gen_helper_##name(t1, cpu_env, t0);                                       \
9356     tcg_gen_extu_i32_tl(cpu_gpr[rD(ctx->opcode)], t1);                        \
9357     tcg_temp_free_i64(t0);                                                    \
9358     tcg_temp_free_i32(t1);                                                    \
9359 }
9360 #define GEN_SPEFPUOP_CONV_64_32(name)                                         \
9361 static inline void gen_##name(DisasContext *ctx)                              \
9362 {                                                                             \
9363     TCGv_i64 t0 = tcg_temp_new_i64();                                         \
9364     TCGv_i32 t1 = tcg_temp_new_i32();                                         \
9365     tcg_gen_trunc_tl_i32(t1, cpu_gpr[rB(ctx->opcode)]);                       \
9366     gen_helper_##name(t0, cpu_env, t1);                                       \
9367     gen_store_gpr64(rD(ctx->opcode), t0);                                     \
9368     tcg_temp_free_i64(t0);                                                    \
9369     tcg_temp_free_i32(t1);                                                    \
9370 }
9371 #define GEN_SPEFPUOP_CONV_64_64(name)                                         \
9372 static inline void gen_##name(DisasContext *ctx)                              \
9373 {                                                                             \
9374     TCGv_i64 t0 = tcg_temp_new_i64();                                         \
9375     gen_load_gpr64(t0, rB(ctx->opcode));                                      \
9376     gen_helper_##name(t0, cpu_env, t0);                                       \
9377     gen_store_gpr64(rD(ctx->opcode), t0);                                     \
9378     tcg_temp_free_i64(t0);                                                    \
9379 }
9380 #define GEN_SPEFPUOP_ARITH2_32_32(name)                                       \
9381 static inline void gen_##name(DisasContext *ctx)                              \
9382 {                                                                             \
9383     TCGv_i32 t0, t1;                                                          \
9384     if (unlikely(!ctx->spe_enabled)) {                                        \
9385         gen_exception(ctx, POWERPC_EXCP_SPEU);                                \
9386         return;                                                               \
9387     }                                                                         \
9388     t0 = tcg_temp_new_i32();                                                  \
9389     t1 = tcg_temp_new_i32();                                                  \
9390     tcg_gen_trunc_tl_i32(t0, cpu_gpr[rA(ctx->opcode)]);                       \
9391     tcg_gen_trunc_tl_i32(t1, cpu_gpr[rB(ctx->opcode)]);                       \
9392     gen_helper_##name(t0, cpu_env, t0, t1);                                   \
9393     tcg_gen_extu_i32_tl(cpu_gpr[rD(ctx->opcode)], t0);                        \
9394                                                                               \
9395     tcg_temp_free_i32(t0);                                                    \
9396     tcg_temp_free_i32(t1);                                                    \
9397 }
9398 #define GEN_SPEFPUOP_ARITH2_64_64(name)                                       \
9399 static inline void gen_##name(DisasContext *ctx)                              \
9400 {                                                                             \
9401     TCGv_i64 t0, t1;                                                          \
9402     if (unlikely(!ctx->spe_enabled)) {                                        \
9403         gen_exception(ctx, POWERPC_EXCP_SPEU);                                \
9404         return;                                                               \
9405     }                                                                         \
9406     t0 = tcg_temp_new_i64();                                                  \
9407     t1 = tcg_temp_new_i64();                                                  \
9408     gen_load_gpr64(t0, rA(ctx->opcode));                                      \
9409     gen_load_gpr64(t1, rB(ctx->opcode));                                      \
9410     gen_helper_##name(t0, cpu_env, t0, t1);                                   \
9411     gen_store_gpr64(rD(ctx->opcode), t0);                                     \
9412     tcg_temp_free_i64(t0);                                                    \
9413     tcg_temp_free_i64(t1);                                                    \
9414 }
9415 #define GEN_SPEFPUOP_COMP_32(name)                                            \
9416 static inline void gen_##name(DisasContext *ctx)                              \
9417 {                                                                             \
9418     TCGv_i32 t0, t1;                                                          \
9419     if (unlikely(!ctx->spe_enabled)) {                                        \
9420         gen_exception(ctx, POWERPC_EXCP_SPEU);                                \
9421         return;                                                               \
9422     }                                                                         \
9423     t0 = tcg_temp_new_i32();                                                  \
9424     t1 = tcg_temp_new_i32();                                                  \
9425                                                                               \
9426     tcg_gen_trunc_tl_i32(t0, cpu_gpr[rA(ctx->opcode)]);                       \
9427     tcg_gen_trunc_tl_i32(t1, cpu_gpr[rB(ctx->opcode)]);                       \
9428     gen_helper_##name(cpu_crf[crfD(ctx->opcode)], cpu_env, t0, t1);           \
9429                                                                               \
9430     tcg_temp_free_i32(t0);                                                    \
9431     tcg_temp_free_i32(t1);                                                    \
9432 }
9433 #define GEN_SPEFPUOP_COMP_64(name)                                            \
9434 static inline void gen_##name(DisasContext *ctx)                              \
9435 {                                                                             \
9436     TCGv_i64 t0, t1;                                                          \
9437     if (unlikely(!ctx->spe_enabled)) {                                        \
9438         gen_exception(ctx, POWERPC_EXCP_SPEU);                                \
9439         return;                                                               \
9440     }                                                                         \
9441     t0 = tcg_temp_new_i64();                                                  \
9442     t1 = tcg_temp_new_i64();                                                  \
9443     gen_load_gpr64(t0, rA(ctx->opcode));                                      \
9444     gen_load_gpr64(t1, rB(ctx->opcode));                                      \
9445     gen_helper_##name(cpu_crf[crfD(ctx->opcode)], cpu_env, t0, t1);           \
9446     tcg_temp_free_i64(t0);                                                    \
9447     tcg_temp_free_i64(t1);                                                    \
9448 }
9449
9450 /* Single precision floating-point vectors operations */
9451 /* Arithmetic */
9452 GEN_SPEFPUOP_ARITH2_64_64(evfsadd);
9453 GEN_SPEFPUOP_ARITH2_64_64(evfssub);
9454 GEN_SPEFPUOP_ARITH2_64_64(evfsmul);
9455 GEN_SPEFPUOP_ARITH2_64_64(evfsdiv);
9456 static inline void gen_evfsabs(DisasContext *ctx)
9457 {
9458     if (unlikely(!ctx->spe_enabled)) {
9459         gen_exception(ctx, POWERPC_EXCP_SPEU);
9460         return;
9461     }
9462     tcg_gen_andi_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)],
9463                     ~0x80000000);
9464     tcg_gen_andi_tl(cpu_gprh[rD(ctx->opcode)], cpu_gprh[rA(ctx->opcode)],
9465                     ~0x80000000);
9466 }
9467 static inline void gen_evfsnabs(DisasContext *ctx)
9468 {
9469     if (unlikely(!ctx->spe_enabled)) {
9470         gen_exception(ctx, POWERPC_EXCP_SPEU);
9471         return;
9472     }
9473     tcg_gen_ori_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)],
9474                    0x80000000);
9475     tcg_gen_ori_tl(cpu_gprh[rD(ctx->opcode)], cpu_gprh[rA(ctx->opcode)],
9476                    0x80000000);
9477 }
9478 static inline void gen_evfsneg(DisasContext *ctx)
9479 {
9480     if (unlikely(!ctx->spe_enabled)) {
9481         gen_exception(ctx, POWERPC_EXCP_SPEU);
9482         return;
9483     }
9484     tcg_gen_xori_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)],
9485                     0x80000000);
9486     tcg_gen_xori_tl(cpu_gprh[rD(ctx->opcode)], cpu_gprh[rA(ctx->opcode)],
9487                     0x80000000);
9488 }
9489
9490 /* Conversion */
9491 GEN_SPEFPUOP_CONV_64_64(evfscfui);
9492 GEN_SPEFPUOP_CONV_64_64(evfscfsi);
9493 GEN_SPEFPUOP_CONV_64_64(evfscfuf);
9494 GEN_SPEFPUOP_CONV_64_64(evfscfsf);
9495 GEN_SPEFPUOP_CONV_64_64(evfsctui);
9496 GEN_SPEFPUOP_CONV_64_64(evfsctsi);
9497 GEN_SPEFPUOP_CONV_64_64(evfsctuf);
9498 GEN_SPEFPUOP_CONV_64_64(evfsctsf);
9499 GEN_SPEFPUOP_CONV_64_64(evfsctuiz);
9500 GEN_SPEFPUOP_CONV_64_64(evfsctsiz);
9501
9502 /* Comparison */
9503 GEN_SPEFPUOP_COMP_64(evfscmpgt);
9504 GEN_SPEFPUOP_COMP_64(evfscmplt);
9505 GEN_SPEFPUOP_COMP_64(evfscmpeq);
9506 GEN_SPEFPUOP_COMP_64(evfststgt);
9507 GEN_SPEFPUOP_COMP_64(evfststlt);
9508 GEN_SPEFPUOP_COMP_64(evfststeq);
9509
9510 /* Opcodes definitions */
9511 GEN_SPE(evfsadd,   evfssub,   0x00, 0x0A, 0x00000000, 0x00000000, PPC_SPE_SINGLE); //
9512 GEN_SPE(evfsabs,   evfsnabs,  0x02, 0x0A, 0x0000F800, 0x0000F800, PPC_SPE_SINGLE); //
9513 GEN_SPE(evfsneg,   speundef,  0x03, 0x0A, 0x0000F800, 0xFFFFFFFF, PPC_SPE_SINGLE); //
9514 GEN_SPE(evfsmul,   evfsdiv,   0x04, 0x0A, 0x00000000, 0x00000000, PPC_SPE_SINGLE); //
9515 GEN_SPE(evfscmpgt, evfscmplt, 0x06, 0x0A, 0x00600000, 0x00600000, PPC_SPE_SINGLE); //
9516 GEN_SPE(evfscmpeq, speundef,  0x07, 0x0A, 0x00600000, 0xFFFFFFFF, PPC_SPE_SINGLE); //
9517 GEN_SPE(evfscfui,  evfscfsi,  0x08, 0x0A, 0x00180000, 0x00180000, PPC_SPE_SINGLE); //
9518 GEN_SPE(evfscfuf,  evfscfsf,  0x09, 0x0A, 0x00180000, 0x00180000, PPC_SPE_SINGLE); //
9519 GEN_SPE(evfsctui,  evfsctsi,  0x0A, 0x0A, 0x00180000, 0x00180000, PPC_SPE_SINGLE); //
9520 GEN_SPE(evfsctuf,  evfsctsf,  0x0B, 0x0A, 0x00180000, 0x00180000, PPC_SPE_SINGLE); //
9521 GEN_SPE(evfsctuiz, speundef,  0x0C, 0x0A, 0x00180000, 0xFFFFFFFF, PPC_SPE_SINGLE); //
9522 GEN_SPE(evfsctsiz, speundef,  0x0D, 0x0A, 0x00180000, 0xFFFFFFFF, PPC_SPE_SINGLE); //
9523 GEN_SPE(evfststgt, evfststlt, 0x0E, 0x0A, 0x00600000, 0x00600000, PPC_SPE_SINGLE); //
9524 GEN_SPE(evfststeq, speundef,  0x0F, 0x0A, 0x00600000, 0xFFFFFFFF, PPC_SPE_SINGLE); //
9525
9526 /* Single precision floating-point operations */
9527 /* Arithmetic */
9528 GEN_SPEFPUOP_ARITH2_32_32(efsadd);
9529 GEN_SPEFPUOP_ARITH2_32_32(efssub);
9530 GEN_SPEFPUOP_ARITH2_32_32(efsmul);
9531 GEN_SPEFPUOP_ARITH2_32_32(efsdiv);
9532 static inline void gen_efsabs(DisasContext *ctx)
9533 {
9534     if (unlikely(!ctx->spe_enabled)) {
9535         gen_exception(ctx, POWERPC_EXCP_SPEU);
9536         return;
9537     }
9538     tcg_gen_andi_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)], (target_long)~0x80000000LL);
9539 }
9540 static inline void gen_efsnabs(DisasContext *ctx)
9541 {
9542     if (unlikely(!ctx->spe_enabled)) {
9543         gen_exception(ctx, POWERPC_EXCP_SPEU);
9544         return;
9545     }
9546     tcg_gen_ori_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)], 0x80000000);
9547 }
9548 static inline void gen_efsneg(DisasContext *ctx)
9549 {
9550     if (unlikely(!ctx->spe_enabled)) {
9551         gen_exception(ctx, POWERPC_EXCP_SPEU);
9552         return;
9553     }
9554     tcg_gen_xori_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)], 0x80000000);
9555 }
9556
9557 /* Conversion */
9558 GEN_SPEFPUOP_CONV_32_32(efscfui);
9559 GEN_SPEFPUOP_CONV_32_32(efscfsi);
9560 GEN_SPEFPUOP_CONV_32_32(efscfuf);
9561 GEN_SPEFPUOP_CONV_32_32(efscfsf);
9562 GEN_SPEFPUOP_CONV_32_32(efsctui);
9563 GEN_SPEFPUOP_CONV_32_32(efsctsi);
9564 GEN_SPEFPUOP_CONV_32_32(efsctuf);
9565 GEN_SPEFPUOP_CONV_32_32(efsctsf);
9566 GEN_SPEFPUOP_CONV_32_32(efsctuiz);
9567 GEN_SPEFPUOP_CONV_32_32(efsctsiz);
9568 GEN_SPEFPUOP_CONV_32_64(efscfd);
9569
9570 /* Comparison */
9571 GEN_SPEFPUOP_COMP_32(efscmpgt);
9572 GEN_SPEFPUOP_COMP_32(efscmplt);
9573 GEN_SPEFPUOP_COMP_32(efscmpeq);
9574 GEN_SPEFPUOP_COMP_32(efststgt);
9575 GEN_SPEFPUOP_COMP_32(efststlt);
9576 GEN_SPEFPUOP_COMP_32(efststeq);
9577
9578 /* Opcodes definitions */
9579 GEN_SPE(efsadd,   efssub,   0x00, 0x0B, 0x00000000, 0x00000000, PPC_SPE_SINGLE); //
9580 GEN_SPE(efsabs,   efsnabs,  0x02, 0x0B, 0x0000F800, 0x0000F800, PPC_SPE_SINGLE); //
9581 GEN_SPE(efsneg,   speundef, 0x03, 0x0B, 0x0000F800, 0xFFFFFFFF, PPC_SPE_SINGLE); //
9582 GEN_SPE(efsmul,   efsdiv,   0x04, 0x0B, 0x00000000, 0x00000000, PPC_SPE_SINGLE); //
9583 GEN_SPE(efscmpgt, efscmplt, 0x06, 0x0B, 0x00600000, 0x00600000, PPC_SPE_SINGLE); //
9584 GEN_SPE(efscmpeq, efscfd,   0x07, 0x0B, 0x00600000, 0x00180000, PPC_SPE_SINGLE); //
9585 GEN_SPE(efscfui,  efscfsi,  0x08, 0x0B, 0x00180000, 0x00180000, PPC_SPE_SINGLE); //
9586 GEN_SPE(efscfuf,  efscfsf,  0x09, 0x0B, 0x00180000, 0x00180000, PPC_SPE_SINGLE); //
9587 GEN_SPE(efsctui,  efsctsi,  0x0A, 0x0B, 0x00180000, 0x00180000, PPC_SPE_SINGLE); //
9588 GEN_SPE(efsctuf,  efsctsf,  0x0B, 0x0B, 0x00180000, 0x00180000, PPC_SPE_SINGLE); //
9589 GEN_SPE(efsctuiz, speundef, 0x0C, 0x0B, 0x00180000, 0xFFFFFFFF, PPC_SPE_SINGLE); //
9590 GEN_SPE(efsctsiz, speundef, 0x0D, 0x0B, 0x00180000, 0xFFFFFFFF, PPC_SPE_SINGLE); //
9591 GEN_SPE(efststgt, efststlt, 0x0E, 0x0B, 0x00600000, 0x00600000, PPC_SPE_SINGLE); //
9592 GEN_SPE(efststeq, speundef, 0x0F, 0x0B, 0x00600000, 0xFFFFFFFF, PPC_SPE_SINGLE); //
9593
9594 /* Double precision floating-point operations */
9595 /* Arithmetic */
9596 GEN_SPEFPUOP_ARITH2_64_64(efdadd);
9597 GEN_SPEFPUOP_ARITH2_64_64(efdsub);
9598 GEN_SPEFPUOP_ARITH2_64_64(efdmul);
9599 GEN_SPEFPUOP_ARITH2_64_64(efddiv);
9600 static inline void gen_efdabs(DisasContext *ctx)
9601 {
9602     if (unlikely(!ctx->spe_enabled)) {
9603         gen_exception(ctx, POWERPC_EXCP_SPEU);
9604         return;
9605     }
9606     tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)]);
9607     tcg_gen_andi_tl(cpu_gprh[rD(ctx->opcode)], cpu_gprh[rA(ctx->opcode)],
9608                     ~0x80000000);
9609 }
9610 static inline void gen_efdnabs(DisasContext *ctx)
9611 {
9612     if (unlikely(!ctx->spe_enabled)) {
9613         gen_exception(ctx, POWERPC_EXCP_SPEU);
9614         return;
9615     }
9616     tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)]);
9617     tcg_gen_ori_tl(cpu_gprh[rD(ctx->opcode)], cpu_gprh[rA(ctx->opcode)],
9618                    0x80000000);
9619 }
9620 static inline void gen_efdneg(DisasContext *ctx)
9621 {
9622     if (unlikely(!ctx->spe_enabled)) {
9623         gen_exception(ctx, POWERPC_EXCP_SPEU);
9624         return;
9625     }
9626     tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)]);
9627     tcg_gen_xori_tl(cpu_gprh[rD(ctx->opcode)], cpu_gprh[rA(ctx->opcode)],
9628                     0x80000000);
9629 }
9630
9631 /* Conversion */
9632 GEN_SPEFPUOP_CONV_64_32(efdcfui);
9633 GEN_SPEFPUOP_CONV_64_32(efdcfsi);
9634 GEN_SPEFPUOP_CONV_64_32(efdcfuf);
9635 GEN_SPEFPUOP_CONV_64_32(efdcfsf);
9636 GEN_SPEFPUOP_CONV_32_64(efdctui);
9637 GEN_SPEFPUOP_CONV_32_64(efdctsi);
9638 GEN_SPEFPUOP_CONV_32_64(efdctuf);
9639 GEN_SPEFPUOP_CONV_32_64(efdctsf);
9640 GEN_SPEFPUOP_CONV_32_64(efdctuiz);
9641 GEN_SPEFPUOP_CONV_32_64(efdctsiz);
9642 GEN_SPEFPUOP_CONV_64_32(efdcfs);
9643 GEN_SPEFPUOP_CONV_64_64(efdcfuid);
9644 GEN_SPEFPUOP_CONV_64_64(efdcfsid);
9645 GEN_SPEFPUOP_CONV_64_64(efdctuidz);
9646 GEN_SPEFPUOP_CONV_64_64(efdctsidz);
9647
9648 /* Comparison */
9649 GEN_SPEFPUOP_COMP_64(efdcmpgt);
9650 GEN_SPEFPUOP_COMP_64(efdcmplt);
9651 GEN_SPEFPUOP_COMP_64(efdcmpeq);
9652 GEN_SPEFPUOP_COMP_64(efdtstgt);
9653 GEN_SPEFPUOP_COMP_64(efdtstlt);
9654 GEN_SPEFPUOP_COMP_64(efdtsteq);
9655
9656 /* Opcodes definitions */
9657 GEN_SPE(efdadd,    efdsub,    0x10, 0x0B, 0x00000000, 0x00000000, PPC_SPE_DOUBLE); //
9658 GEN_SPE(efdcfuid,  efdcfsid,  0x11, 0x0B, 0x00180000, 0x00180000, PPC_SPE_DOUBLE); //
9659 GEN_SPE(efdabs,    efdnabs,   0x12, 0x0B, 0x0000F800, 0x0000F800, PPC_SPE_DOUBLE); //
9660 GEN_SPE(efdneg,    speundef,  0x13, 0x0B, 0x0000F800, 0xFFFFFFFF, PPC_SPE_DOUBLE); //
9661 GEN_SPE(efdmul,    efddiv,    0x14, 0x0B, 0x00000000, 0x00000000, PPC_SPE_DOUBLE); //
9662 GEN_SPE(efdctuidz, efdctsidz, 0x15, 0x0B, 0x00180000, 0x00180000, PPC_SPE_DOUBLE); //
9663 GEN_SPE(efdcmpgt,  efdcmplt,  0x16, 0x0B, 0x00600000, 0x00600000, PPC_SPE_DOUBLE); //
9664 GEN_SPE(efdcmpeq,  efdcfs,    0x17, 0x0B, 0x00600000, 0x00180000, PPC_SPE_DOUBLE); //
9665 GEN_SPE(efdcfui,   efdcfsi,   0x18, 0x0B, 0x00180000, 0x00180000, PPC_SPE_DOUBLE); //
9666 GEN_SPE(efdcfuf,   efdcfsf,   0x19, 0x0B, 0x00180000, 0x00180000, PPC_SPE_DOUBLE); //
9667 GEN_SPE(efdctui,   efdctsi,   0x1A, 0x0B, 0x00180000, 0x00180000, PPC_SPE_DOUBLE); //
9668 GEN_SPE(efdctuf,   efdctsf,   0x1B, 0x0B, 0x00180000, 0x00180000, PPC_SPE_DOUBLE); //
9669 GEN_SPE(efdctuiz,  speundef,  0x1C, 0x0B, 0x00180000, 0xFFFFFFFF, PPC_SPE_DOUBLE); //
9670 GEN_SPE(efdctsiz,  speundef,  0x1D, 0x0B, 0x00180000, 0xFFFFFFFF, PPC_SPE_DOUBLE); //
9671 GEN_SPE(efdtstgt,  efdtstlt,  0x1E, 0x0B, 0x00600000, 0x00600000, PPC_SPE_DOUBLE); //
9672 GEN_SPE(efdtsteq,  speundef,  0x1F, 0x0B, 0x00600000, 0xFFFFFFFF, PPC_SPE_DOUBLE); //
9673
9674 static opcode_t opcodes[] = {
9675 GEN_HANDLER(invalid, 0x00, 0x00, 0x00, 0xFFFFFFFF, PPC_NONE),
9676 GEN_HANDLER(cmp, 0x1F, 0x00, 0x00, 0x00400000, PPC_INTEGER),
9677 GEN_HANDLER(cmpi, 0x0B, 0xFF, 0xFF, 0x00400000, PPC_INTEGER),
9678 GEN_HANDLER(cmpl, 0x1F, 0x00, 0x01, 0x00400000, PPC_INTEGER),
9679 GEN_HANDLER(cmpli, 0x0A, 0xFF, 0xFF, 0x00400000, PPC_INTEGER),
9680 GEN_HANDLER_E(cmpb, 0x1F, 0x1C, 0x0F, 0x00000001, PPC_NONE, PPC2_ISA205),
9681 GEN_HANDLER(isel, 0x1F, 0x0F, 0xFF, 0x00000001, PPC_ISEL),
9682 GEN_HANDLER(addi, 0x0E, 0xFF, 0xFF, 0x00000000, PPC_INTEGER),
9683 GEN_HANDLER(addic, 0x0C, 0xFF, 0xFF, 0x00000000, PPC_INTEGER),
9684 GEN_HANDLER2(addic_, "addic.", 0x0D, 0xFF, 0xFF, 0x00000000, PPC_INTEGER),
9685 GEN_HANDLER(addis, 0x0F, 0xFF, 0xFF, 0x00000000, PPC_INTEGER),
9686 GEN_HANDLER(mulhw, 0x1F, 0x0B, 0x02, 0x00000400, PPC_INTEGER),
9687 GEN_HANDLER(mulhwu, 0x1F, 0x0B, 0x00, 0x00000400, PPC_INTEGER),
9688 GEN_HANDLER(mullw, 0x1F, 0x0B, 0x07, 0x00000000, PPC_INTEGER),
9689 GEN_HANDLER(mullwo, 0x1F, 0x0B, 0x17, 0x00000000, PPC_INTEGER),
9690 GEN_HANDLER(mulli, 0x07, 0xFF, 0xFF, 0x00000000, PPC_INTEGER),
9691 #if defined(TARGET_PPC64)
9692 GEN_HANDLER(mulld, 0x1F, 0x09, 0x07, 0x00000000, PPC_64B),
9693 #endif
9694 GEN_HANDLER(neg, 0x1F, 0x08, 0x03, 0x0000F800, PPC_INTEGER),
9695 GEN_HANDLER(nego, 0x1F, 0x08, 0x13, 0x0000F800, PPC_INTEGER),
9696 GEN_HANDLER(subfic, 0x08, 0xFF, 0xFF, 0x00000000, PPC_INTEGER),
9697 GEN_HANDLER2(andi_, "andi.", 0x1C, 0xFF, 0xFF, 0x00000000, PPC_INTEGER),
9698 GEN_HANDLER2(andis_, "andis.", 0x1D, 0xFF, 0xFF, 0x00000000, PPC_INTEGER),
9699 GEN_HANDLER(cntlzw, 0x1F, 0x1A, 0x00, 0x00000000, PPC_INTEGER),
9700 GEN_HANDLER(or, 0x1F, 0x1C, 0x0D, 0x00000000, PPC_INTEGER),
9701 GEN_HANDLER(xor, 0x1F, 0x1C, 0x09, 0x00000000, PPC_INTEGER),
9702 GEN_HANDLER(ori, 0x18, 0xFF, 0xFF, 0x00000000, PPC_INTEGER),
9703 GEN_HANDLER(oris, 0x19, 0xFF, 0xFF, 0x00000000, PPC_INTEGER),
9704 GEN_HANDLER(xori, 0x1A, 0xFF, 0xFF, 0x00000000, PPC_INTEGER),
9705 GEN_HANDLER(xoris, 0x1B, 0xFF, 0xFF, 0x00000000, PPC_INTEGER),
9706 GEN_HANDLER(popcntb, 0x1F, 0x1A, 0x03, 0x0000F801, PPC_POPCNTB),
9707 GEN_HANDLER(popcntw, 0x1F, 0x1A, 0x0b, 0x0000F801, PPC_POPCNTWD),
9708 GEN_HANDLER_E(prtyw, 0x1F, 0x1A, 0x04, 0x0000F801, PPC_NONE, PPC2_ISA205),
9709 #if defined(TARGET_PPC64)
9710 GEN_HANDLER(popcntd, 0x1F, 0x1A, 0x0F, 0x0000F801, PPC_POPCNTWD),
9711 GEN_HANDLER(cntlzd, 0x1F, 0x1A, 0x01, 0x00000000, PPC_64B),
9712 GEN_HANDLER_E(prtyd, 0x1F, 0x1A, 0x05, 0x0000F801, PPC_NONE, PPC2_ISA205),
9713 GEN_HANDLER_E(bpermd, 0x1F, 0x1C, 0x07, 0x00000001, PPC_NONE, PPC2_PERM_ISA206),
9714 #endif
9715 GEN_HANDLER(rlwimi, 0x14, 0xFF, 0xFF, 0x00000000, PPC_INTEGER),
9716 GEN_HANDLER(rlwinm, 0x15, 0xFF, 0xFF, 0x00000000, PPC_INTEGER),
9717 GEN_HANDLER(rlwnm, 0x17, 0xFF, 0xFF, 0x00000000, PPC_INTEGER),
9718 GEN_HANDLER(slw, 0x1F, 0x18, 0x00, 0x00000000, PPC_INTEGER),
9719 GEN_HANDLER(sraw, 0x1F, 0x18, 0x18, 0x00000000, PPC_INTEGER),
9720 GEN_HANDLER(srawi, 0x1F, 0x18, 0x19, 0x00000000, PPC_INTEGER),
9721 GEN_HANDLER(srw, 0x1F, 0x18, 0x10, 0x00000000, PPC_INTEGER),
9722 #if defined(TARGET_PPC64)
9723 GEN_HANDLER(sld, 0x1F, 0x1B, 0x00, 0x00000000, PPC_64B),
9724 GEN_HANDLER(srad, 0x1F, 0x1A, 0x18, 0x00000000, PPC_64B),
9725 GEN_HANDLER2(sradi0, "sradi", 0x1F, 0x1A, 0x19, 0x00000000, PPC_64B),
9726 GEN_HANDLER2(sradi1, "sradi", 0x1F, 0x1B, 0x19, 0x00000000, PPC_64B),
9727 GEN_HANDLER(srd, 0x1F, 0x1B, 0x10, 0x00000000, PPC_64B),
9728 #endif
9729 GEN_HANDLER(frsqrtes, 0x3B, 0x1A, 0xFF, 0x001F07C0, PPC_FLOAT_FRSQRTES),
9730 GEN_HANDLER(fsqrt, 0x3F, 0x16, 0xFF, 0x001F07C0, PPC_FLOAT_FSQRT),
9731 GEN_HANDLER(fsqrts, 0x3B, 0x16, 0xFF, 0x001F07C0, PPC_FLOAT_FSQRT),
9732 GEN_HANDLER(fcmpo, 0x3F, 0x00, 0x01, 0x00600001, PPC_FLOAT),
9733 GEN_HANDLER(fcmpu, 0x3F, 0x00, 0x00, 0x00600001, PPC_FLOAT),
9734 GEN_HANDLER(fabs, 0x3F, 0x08, 0x08, 0x001F0000, PPC_FLOAT),
9735 GEN_HANDLER(fmr, 0x3F, 0x08, 0x02, 0x001F0000, PPC_FLOAT),
9736 GEN_HANDLER(fnabs, 0x3F, 0x08, 0x04, 0x001F0000, PPC_FLOAT),
9737 GEN_HANDLER(fneg, 0x3F, 0x08, 0x01, 0x001F0000, PPC_FLOAT),
9738 GEN_HANDLER_E(fcpsgn, 0x3F, 0x08, 0x00, 0x00000000, PPC_NONE, PPC2_ISA205),
9739 GEN_HANDLER_E(fmrgew, 0x3F, 0x06, 0x1E, 0x00000001, PPC_NONE, PPC2_VSX207),
9740 GEN_HANDLER_E(fmrgow, 0x3F, 0x06, 0x1A, 0x00000001, PPC_NONE, PPC2_VSX207),
9741 GEN_HANDLER(mcrfs, 0x3F, 0x00, 0x02, 0x0063F801, PPC_FLOAT),
9742 GEN_HANDLER(mffs, 0x3F, 0x07, 0x12, 0x001FF800, PPC_FLOAT),
9743 GEN_HANDLER(mtfsb0, 0x3F, 0x06, 0x02, 0x001FF800, PPC_FLOAT),
9744 GEN_HANDLER(mtfsb1, 0x3F, 0x06, 0x01, 0x001FF800, PPC_FLOAT),
9745 GEN_HANDLER(mtfsf, 0x3F, 0x07, 0x16, 0x00000000, PPC_FLOAT),
9746 GEN_HANDLER(mtfsfi, 0x3F, 0x06, 0x04, 0x006e0800, PPC_FLOAT),
9747 #if defined(TARGET_PPC64)
9748 GEN_HANDLER(ld, 0x3A, 0xFF, 0xFF, 0x00000000, PPC_64B),
9749 GEN_HANDLER(lq, 0x38, 0xFF, 0xFF, 0x00000000, PPC_64BX),
9750 GEN_HANDLER(std, 0x3E, 0xFF, 0xFF, 0x00000000, PPC_64B),
9751 #endif
9752 GEN_HANDLER(lmw, 0x2E, 0xFF, 0xFF, 0x00000000, PPC_INTEGER),
9753 GEN_HANDLER(stmw, 0x2F, 0xFF, 0xFF, 0x00000000, PPC_INTEGER),
9754 GEN_HANDLER(lswi, 0x1F, 0x15, 0x12, 0x00000001, PPC_STRING),
9755 GEN_HANDLER(lswx, 0x1F, 0x15, 0x10, 0x00000001, PPC_STRING),
9756 GEN_HANDLER(stswi, 0x1F, 0x15, 0x16, 0x00000001, PPC_STRING),
9757 GEN_HANDLER(stswx, 0x1F, 0x15, 0x14, 0x00000001, PPC_STRING),
9758 GEN_HANDLER(eieio, 0x1F, 0x16, 0x1A, 0x03FFF801, PPC_MEM_EIEIO),
9759 GEN_HANDLER(isync, 0x13, 0x16, 0x04, 0x03FFF801, PPC_MEM),
9760 GEN_HANDLER_E(lbarx, 0x1F, 0x14, 0x01, 0, PPC_NONE, PPC2_ATOMIC_ISA206),
9761 GEN_HANDLER_E(lharx, 0x1F, 0x14, 0x03, 0, PPC_NONE, PPC2_ATOMIC_ISA206),
9762 GEN_HANDLER(lwarx, 0x1F, 0x14, 0x00, 0x00000000, PPC_RES),
9763 GEN_HANDLER_E(stbcx_, 0x1F, 0x16, 0x15, 0, PPC_NONE, PPC2_ATOMIC_ISA206),
9764 GEN_HANDLER_E(sthcx_, 0x1F, 0x16, 0x16, 0, PPC_NONE, PPC2_ATOMIC_ISA206),
9765 GEN_HANDLER2(stwcx_, "stwcx.", 0x1F, 0x16, 0x04, 0x00000000, PPC_RES),
9766 #if defined(TARGET_PPC64)
9767 GEN_HANDLER(ldarx, 0x1F, 0x14, 0x02, 0x00000000, PPC_64B),
9768 GEN_HANDLER_E(lqarx, 0x1F, 0x14, 0x08, 0, PPC_NONE, PPC2_LSQ_ISA207),
9769 GEN_HANDLER2(stdcx_, "stdcx.", 0x1F, 0x16, 0x06, 0x00000000, PPC_64B),
9770 GEN_HANDLER_E(stqcx_, 0x1F, 0x16, 0x05, 0, PPC_NONE, PPC2_LSQ_ISA207),
9771 #endif
9772 GEN_HANDLER(sync, 0x1F, 0x16, 0x12, 0x039FF801, PPC_MEM_SYNC),
9773 GEN_HANDLER(wait, 0x1F, 0x1E, 0x01, 0x03FFF801, PPC_WAIT),
9774 GEN_HANDLER(b, 0x12, 0xFF, 0xFF, 0x00000000, PPC_FLOW),
9775 GEN_HANDLER(bc, 0x10, 0xFF, 0xFF, 0x00000000, PPC_FLOW),
9776 GEN_HANDLER(bcctr, 0x13, 0x10, 0x10, 0x00000000, PPC_FLOW),
9777 GEN_HANDLER(bclr, 0x13, 0x10, 0x00, 0x00000000, PPC_FLOW),
9778 GEN_HANDLER_E(bctar, 0x13, 0x10, 0x11, 0, PPC_NONE, PPC2_BCTAR_ISA207),
9779 GEN_HANDLER(mcrf, 0x13, 0x00, 0xFF, 0x00000001, PPC_INTEGER),
9780 GEN_HANDLER(rfi, 0x13, 0x12, 0x01, 0x03FF8001, PPC_FLOW),
9781 #if defined(TARGET_PPC64)
9782 GEN_HANDLER(rfid, 0x13, 0x12, 0x00, 0x03FF8001, PPC_64B),
9783 GEN_HANDLER(hrfid, 0x13, 0x12, 0x08, 0x03FF8001, PPC_64H),
9784 #endif
9785 GEN_HANDLER(sc, 0x11, 0xFF, 0xFF, 0x03FFF01D, PPC_FLOW),
9786 GEN_HANDLER(tw, 0x1F, 0x04, 0x00, 0x00000001, PPC_FLOW),
9787 GEN_HANDLER(twi, 0x03, 0xFF, 0xFF, 0x00000000, PPC_FLOW),
9788 #if defined(TARGET_PPC64)
9789 GEN_HANDLER(td, 0x1F, 0x04, 0x02, 0x00000001, PPC_64B),
9790 GEN_HANDLER(tdi, 0x02, 0xFF, 0xFF, 0x00000000, PPC_64B),
9791 #endif
9792 GEN_HANDLER(mcrxr, 0x1F, 0x00, 0x10, 0x007FF801, PPC_MISC),
9793 GEN_HANDLER(mfcr, 0x1F, 0x13, 0x00, 0x00000801, PPC_MISC),
9794 GEN_HANDLER(mfmsr, 0x1F, 0x13, 0x02, 0x001FF801, PPC_MISC),
9795 GEN_HANDLER(mfspr, 0x1F, 0x13, 0x0A, 0x00000001, PPC_MISC),
9796 GEN_HANDLER(mftb, 0x1F, 0x13, 0x0B, 0x00000001, PPC_MFTB),
9797 GEN_HANDLER(mtcrf, 0x1F, 0x10, 0x04, 0x00000801, PPC_MISC),
9798 #if defined(TARGET_PPC64)
9799 GEN_HANDLER(mtmsrd, 0x1F, 0x12, 0x05, 0x001EF801, PPC_64B),
9800 #endif
9801 GEN_HANDLER(mtmsr, 0x1F, 0x12, 0x04, 0x001FF801, PPC_MISC),
9802 GEN_HANDLER(mtspr, 0x1F, 0x13, 0x0E, 0x00000001, PPC_MISC),
9803 GEN_HANDLER(dcbf, 0x1F, 0x16, 0x02, 0x03C00001, PPC_CACHE),
9804 GEN_HANDLER(dcbi, 0x1F, 0x16, 0x0E, 0x03E00001, PPC_CACHE),
9805 GEN_HANDLER(dcbst, 0x1F, 0x16, 0x01, 0x03E00001, PPC_CACHE),
9806 GEN_HANDLER(dcbt, 0x1F, 0x16, 0x08, 0x00000001, PPC_CACHE),
9807 GEN_HANDLER(dcbtst, 0x1F, 0x16, 0x07, 0x00000001, PPC_CACHE),
9808 GEN_HANDLER_E(dcbtls, 0x1F, 0x06, 0x05, 0x02000001, PPC_BOOKE, PPC2_BOOKE206),
9809 GEN_HANDLER(dcbz, 0x1F, 0x16, 0x1F, 0x03C00001, PPC_CACHE_DCBZ),
9810 GEN_HANDLER(dst, 0x1F, 0x16, 0x0A, 0x01800001, PPC_ALTIVEC),
9811 GEN_HANDLER(dstst, 0x1F, 0x16, 0x0B, 0x02000001, PPC_ALTIVEC),
9812 GEN_HANDLER(dss, 0x1F, 0x16, 0x19, 0x019FF801, PPC_ALTIVEC),
9813 GEN_HANDLER(icbi, 0x1F, 0x16, 0x1E, 0x03E00001, PPC_CACHE_ICBI),
9814 GEN_HANDLER(dcba, 0x1F, 0x16, 0x17, 0x03E00001, PPC_CACHE_DCBA),
9815 GEN_HANDLER(mfsr, 0x1F, 0x13, 0x12, 0x0010F801, PPC_SEGMENT),
9816 GEN_HANDLER(mfsrin, 0x1F, 0x13, 0x14, 0x001F0001, PPC_SEGMENT),
9817 GEN_HANDLER(mtsr, 0x1F, 0x12, 0x06, 0x0010F801, PPC_SEGMENT),
9818 GEN_HANDLER(mtsrin, 0x1F, 0x12, 0x07, 0x001F0001, PPC_SEGMENT),
9819 #if defined(TARGET_PPC64)
9820 GEN_HANDLER2(mfsr_64b, "mfsr", 0x1F, 0x13, 0x12, 0x0010F801, PPC_SEGMENT_64B),
9821 GEN_HANDLER2(mfsrin_64b, "mfsrin", 0x1F, 0x13, 0x14, 0x001F0001,
9822              PPC_SEGMENT_64B),
9823 GEN_HANDLER2(mtsr_64b, "mtsr", 0x1F, 0x12, 0x06, 0x0010F801, PPC_SEGMENT_64B),
9824 GEN_HANDLER2(mtsrin_64b, "mtsrin", 0x1F, 0x12, 0x07, 0x001F0001,
9825              PPC_SEGMENT_64B),
9826 GEN_HANDLER2(slbmte, "slbmte", 0x1F, 0x12, 0x0C, 0x001F0001, PPC_SEGMENT_64B),
9827 GEN_HANDLER2(slbmfee, "slbmfee", 0x1F, 0x13, 0x1C, 0x001F0001, PPC_SEGMENT_64B),
9828 GEN_HANDLER2(slbmfev, "slbmfev", 0x1F, 0x13, 0x1A, 0x001F0001, PPC_SEGMENT_64B),
9829 #endif
9830 GEN_HANDLER(tlbia, 0x1F, 0x12, 0x0B, 0x03FFFC01, PPC_MEM_TLBIA),
9831 GEN_HANDLER(tlbiel, 0x1F, 0x12, 0x08, 0x03FF0001, PPC_MEM_TLBIE),
9832 GEN_HANDLER(tlbie, 0x1F, 0x12, 0x09, 0x03FF0001, PPC_MEM_TLBIE),
9833 GEN_HANDLER(tlbsync, 0x1F, 0x16, 0x11, 0x03FFF801, PPC_MEM_TLBSYNC),
9834 #if defined(TARGET_PPC64)
9835 GEN_HANDLER(slbia, 0x1F, 0x12, 0x0F, 0x03FFFC01, PPC_SLBI),
9836 GEN_HANDLER(slbie, 0x1F, 0x12, 0x0D, 0x03FF0001, PPC_SLBI),
9837 #endif
9838 GEN_HANDLER(eciwx, 0x1F, 0x16, 0x0D, 0x00000001, PPC_EXTERN),
9839 GEN_HANDLER(ecowx, 0x1F, 0x16, 0x09, 0x00000001, PPC_EXTERN),
9840 GEN_HANDLER(abs, 0x1F, 0x08, 0x0B, 0x0000F800, PPC_POWER_BR),
9841 GEN_HANDLER(abso, 0x1F, 0x08, 0x1B, 0x0000F800, PPC_POWER_BR),
9842 GEN_HANDLER(clcs, 0x1F, 0x10, 0x13, 0x0000F800, PPC_POWER_BR),
9843 GEN_HANDLER(div, 0x1F, 0x0B, 0x0A, 0x00000000, PPC_POWER_BR),
9844 GEN_HANDLER(divo, 0x1F, 0x0B, 0x1A, 0x00000000, PPC_POWER_BR),
9845 GEN_HANDLER(divs, 0x1F, 0x0B, 0x0B, 0x00000000, PPC_POWER_BR),
9846 GEN_HANDLER(divso, 0x1F, 0x0B, 0x1B, 0x00000000, PPC_POWER_BR),
9847 GEN_HANDLER(doz, 0x1F, 0x08, 0x08, 0x00000000, PPC_POWER_BR),
9848 GEN_HANDLER(dozo, 0x1F, 0x08, 0x18, 0x00000000, PPC_POWER_BR),
9849 GEN_HANDLER(dozi, 0x09, 0xFF, 0xFF, 0x00000000, PPC_POWER_BR),
9850 GEN_HANDLER(lscbx, 0x1F, 0x15, 0x08, 0x00000000, PPC_POWER_BR),
9851 GEN_HANDLER(maskg, 0x1F, 0x1D, 0x00, 0x00000000, PPC_POWER_BR),
9852 GEN_HANDLER(maskir, 0x1F, 0x1D, 0x10, 0x00000000, PPC_POWER_BR),
9853 GEN_HANDLER(mul, 0x1F, 0x0B, 0x03, 0x00000000, PPC_POWER_BR),
9854 GEN_HANDLER(mulo, 0x1F, 0x0B, 0x13, 0x00000000, PPC_POWER_BR),
9855 GEN_HANDLER(nabs, 0x1F, 0x08, 0x0F, 0x00000000, PPC_POWER_BR),
9856 GEN_HANDLER(nabso, 0x1F, 0x08, 0x1F, 0x00000000, PPC_POWER_BR),
9857 GEN_HANDLER(rlmi, 0x16, 0xFF, 0xFF, 0x00000000, PPC_POWER_BR),
9858 GEN_HANDLER(rrib, 0x1F, 0x19, 0x10, 0x00000000, PPC_POWER_BR),
9859 GEN_HANDLER(sle, 0x1F, 0x19, 0x04, 0x00000000, PPC_POWER_BR),
9860 GEN_HANDLER(sleq, 0x1F, 0x19, 0x06, 0x00000000, PPC_POWER_BR),
9861 GEN_HANDLER(sliq, 0x1F, 0x18, 0x05, 0x00000000, PPC_POWER_BR),
9862 GEN_HANDLER(slliq, 0x1F, 0x18, 0x07, 0x00000000, PPC_POWER_BR),
9863 GEN_HANDLER(sllq, 0x1F, 0x18, 0x06, 0x00000000, PPC_POWER_BR),
9864 GEN_HANDLER(slq, 0x1F, 0x18, 0x04, 0x00000000, PPC_POWER_BR),
9865 GEN_HANDLER(sraiq, 0x1F, 0x18, 0x1D, 0x00000000, PPC_POWER_BR),
9866 GEN_HANDLER(sraq, 0x1F, 0x18, 0x1C, 0x00000000, PPC_POWER_BR),
9867 GEN_HANDLER(sre, 0x1F, 0x19, 0x14, 0x00000000, PPC_POWER_BR),
9868 GEN_HANDLER(srea, 0x1F, 0x19, 0x1C, 0x00000000, PPC_POWER_BR),
9869 GEN_HANDLER(sreq, 0x1F, 0x19, 0x16, 0x00000000, PPC_POWER_BR),
9870 GEN_HANDLER(sriq, 0x1F, 0x18, 0x15, 0x00000000, PPC_POWER_BR),
9871 GEN_HANDLER(srliq, 0x1F, 0x18, 0x17, 0x00000000, PPC_POWER_BR),
9872 GEN_HANDLER(srlq, 0x1F, 0x18, 0x16, 0x00000000, PPC_POWER_BR),
9873 GEN_HANDLER(srq, 0x1F, 0x18, 0x14, 0x00000000, PPC_POWER_BR),
9874 GEN_HANDLER(dsa, 0x1F, 0x14, 0x13, 0x03FFF801, PPC_602_SPEC),
9875 GEN_HANDLER(esa, 0x1F, 0x14, 0x12, 0x03FFF801, PPC_602_SPEC),
9876 GEN_HANDLER(mfrom, 0x1F, 0x09, 0x08, 0x03E0F801, PPC_602_SPEC),
9877 GEN_HANDLER2(tlbld_6xx, "tlbld", 0x1F, 0x12, 0x1E, 0x03FF0001, PPC_6xx_TLB),
9878 GEN_HANDLER2(tlbli_6xx, "tlbli", 0x1F, 0x12, 0x1F, 0x03FF0001, PPC_6xx_TLB),
9879 GEN_HANDLER2(tlbld_74xx, "tlbld", 0x1F, 0x12, 0x1E, 0x03FF0001, PPC_74xx_TLB),
9880 GEN_HANDLER2(tlbli_74xx, "tlbli", 0x1F, 0x12, 0x1F, 0x03FF0001, PPC_74xx_TLB),
9881 GEN_HANDLER(clf, 0x1F, 0x16, 0x03, 0x03E00000, PPC_POWER),
9882 GEN_HANDLER(cli, 0x1F, 0x16, 0x0F, 0x03E00000, PPC_POWER),
9883 GEN_HANDLER(dclst, 0x1F, 0x16, 0x13, 0x03E00000, PPC_POWER),
9884 GEN_HANDLER(mfsri, 0x1F, 0x13, 0x13, 0x00000001, PPC_POWER),
9885 GEN_HANDLER(rac, 0x1F, 0x12, 0x19, 0x00000001, PPC_POWER),
9886 GEN_HANDLER(rfsvc, 0x13, 0x12, 0x02, 0x03FFF0001, PPC_POWER),
9887 GEN_HANDLER(lfq, 0x38, 0xFF, 0xFF, 0x00000003, PPC_POWER2),
9888 GEN_HANDLER(lfqu, 0x39, 0xFF, 0xFF, 0x00000003, PPC_POWER2),
9889 GEN_HANDLER(lfqux, 0x1F, 0x17, 0x19, 0x00000001, PPC_POWER2),
9890 GEN_HANDLER(lfqx, 0x1F, 0x17, 0x18, 0x00000001, PPC_POWER2),
9891 GEN_HANDLER(stfq, 0x3C, 0xFF, 0xFF, 0x00000003, PPC_POWER2),
9892 GEN_HANDLER(stfqu, 0x3D, 0xFF, 0xFF, 0x00000003, PPC_POWER2),
9893 GEN_HANDLER(stfqux, 0x1F, 0x17, 0x1D, 0x00000001, PPC_POWER2),
9894 GEN_HANDLER(stfqx, 0x1F, 0x17, 0x1C, 0x00000001, PPC_POWER2),
9895 GEN_HANDLER(mfapidi, 0x1F, 0x13, 0x08, 0x0000F801, PPC_MFAPIDI),
9896 GEN_HANDLER(tlbiva, 0x1F, 0x12, 0x18, 0x03FFF801, PPC_TLBIVA),
9897 GEN_HANDLER(mfdcr, 0x1F, 0x03, 0x0A, 0x00000001, PPC_DCR),
9898 GEN_HANDLER(mtdcr, 0x1F, 0x03, 0x0E, 0x00000001, PPC_DCR),
9899 GEN_HANDLER(mfdcrx, 0x1F, 0x03, 0x08, 0x00000000, PPC_DCRX),
9900 GEN_HANDLER(mtdcrx, 0x1F, 0x03, 0x0C, 0x00000000, PPC_DCRX),
9901 GEN_HANDLER(mfdcrux, 0x1F, 0x03, 0x09, 0x00000000, PPC_DCRUX),
9902 GEN_HANDLER(mtdcrux, 0x1F, 0x03, 0x0D, 0x00000000, PPC_DCRUX),
9903 GEN_HANDLER(dccci, 0x1F, 0x06, 0x0E, 0x03E00001, PPC_4xx_COMMON),
9904 GEN_HANDLER(dcread, 0x1F, 0x06, 0x0F, 0x00000001, PPC_4xx_COMMON),
9905 GEN_HANDLER2(icbt_40x, "icbt", 0x1F, 0x06, 0x08, 0x03E00001, PPC_40x_ICBT),
9906 GEN_HANDLER(iccci, 0x1F, 0x06, 0x1E, 0x00000001, PPC_4xx_COMMON),
9907 GEN_HANDLER(icread, 0x1F, 0x06, 0x1F, 0x03E00001, PPC_4xx_COMMON),
9908 GEN_HANDLER2(rfci_40x, "rfci", 0x13, 0x13, 0x01, 0x03FF8001, PPC_40x_EXCP),
9909 GEN_HANDLER_E(rfci, 0x13, 0x13, 0x01, 0x03FF8001, PPC_BOOKE, PPC2_BOOKE206),
9910 GEN_HANDLER(rfdi, 0x13, 0x07, 0x01, 0x03FF8001, PPC_RFDI),
9911 GEN_HANDLER(rfmci, 0x13, 0x06, 0x01, 0x03FF8001, PPC_RFMCI),
9912 GEN_HANDLER2(tlbre_40x, "tlbre", 0x1F, 0x12, 0x1D, 0x00000001, PPC_40x_TLB),
9913 GEN_HANDLER2(tlbsx_40x, "tlbsx", 0x1F, 0x12, 0x1C, 0x00000000, PPC_40x_TLB),
9914 GEN_HANDLER2(tlbwe_40x, "tlbwe", 0x1F, 0x12, 0x1E, 0x00000001, PPC_40x_TLB),
9915 GEN_HANDLER2(tlbre_440, "tlbre", 0x1F, 0x12, 0x1D, 0x00000001, PPC_BOOKE),
9916 GEN_HANDLER2(tlbsx_440, "tlbsx", 0x1F, 0x12, 0x1C, 0x00000000, PPC_BOOKE),
9917 GEN_HANDLER2(tlbwe_440, "tlbwe", 0x1F, 0x12, 0x1E, 0x00000001, PPC_BOOKE),
9918 GEN_HANDLER2_E(tlbre_booke206, "tlbre", 0x1F, 0x12, 0x1D, 0x00000001,
9919                PPC_NONE, PPC2_BOOKE206),
9920 GEN_HANDLER2_E(tlbsx_booke206, "tlbsx", 0x1F, 0x12, 0x1C, 0x00000000,
9921                PPC_NONE, PPC2_BOOKE206),
9922 GEN_HANDLER2_E(tlbwe_booke206, "tlbwe", 0x1F, 0x12, 0x1E, 0x00000001,
9923                PPC_NONE, PPC2_BOOKE206),
9924 GEN_HANDLER2_E(tlbivax_booke206, "tlbivax", 0x1F, 0x12, 0x18, 0x00000001,
9925                PPC_NONE, PPC2_BOOKE206),
9926 GEN_HANDLER2_E(tlbilx_booke206, "tlbilx", 0x1F, 0x12, 0x00, 0x03800001,
9927                PPC_NONE, PPC2_BOOKE206),
9928 GEN_HANDLER2_E(msgsnd, "msgsnd", 0x1F, 0x0E, 0x06, 0x03ff0001,
9929                PPC_NONE, PPC2_PRCNTL),
9930 GEN_HANDLER2_E(msgclr, "msgclr", 0x1F, 0x0E, 0x07, 0x03ff0001,
9931                PPC_NONE, PPC2_PRCNTL),
9932 GEN_HANDLER(wrtee, 0x1F, 0x03, 0x04, 0x000FFC01, PPC_WRTEE),
9933 GEN_HANDLER(wrteei, 0x1F, 0x03, 0x05, 0x000E7C01, PPC_WRTEE),
9934 GEN_HANDLER(dlmzb, 0x1F, 0x0E, 0x02, 0x00000000, PPC_440_SPEC),
9935 GEN_HANDLER_E(mbar, 0x1F, 0x16, 0x1a, 0x001FF801,
9936               PPC_BOOKE, PPC2_BOOKE206),
9937 GEN_HANDLER(msync_4xx, 0x1F, 0x16, 0x12, 0x03FFF801, PPC_BOOKE),
9938 GEN_HANDLER2_E(icbt_440, "icbt", 0x1F, 0x16, 0x00, 0x03E00001,
9939                PPC_BOOKE, PPC2_BOOKE206),
9940 GEN_HANDLER(lvsl, 0x1f, 0x06, 0x00, 0x00000001, PPC_ALTIVEC),
9941 GEN_HANDLER(lvsr, 0x1f, 0x06, 0x01, 0x00000001, PPC_ALTIVEC),
9942 GEN_HANDLER(mfvscr, 0x04, 0x2, 0x18, 0x001ff800, PPC_ALTIVEC),
9943 GEN_HANDLER(mtvscr, 0x04, 0x2, 0x19, 0x03ff0000, PPC_ALTIVEC),
9944 GEN_HANDLER(vmladduhm, 0x04, 0x11, 0xFF, 0x00000000, PPC_ALTIVEC),
9945 GEN_HANDLER2(evsel0, "evsel", 0x04, 0x1c, 0x09, 0x00000000, PPC_SPE),
9946 GEN_HANDLER2(evsel1, "evsel", 0x04, 0x1d, 0x09, 0x00000000, PPC_SPE),
9947 GEN_HANDLER2(evsel2, "evsel", 0x04, 0x1e, 0x09, 0x00000000, PPC_SPE),
9948 GEN_HANDLER2(evsel3, "evsel", 0x04, 0x1f, 0x09, 0x00000000, PPC_SPE),
9949
9950 #undef GEN_INT_ARITH_ADD
9951 #undef GEN_INT_ARITH_ADD_CONST
9952 #define GEN_INT_ARITH_ADD(name, opc3, add_ca, compute_ca, compute_ov)         \
9953 GEN_HANDLER(name, 0x1F, 0x0A, opc3, 0x00000000, PPC_INTEGER),
9954 #define GEN_INT_ARITH_ADD_CONST(name, opc3, const_val,                        \
9955                                 add_ca, compute_ca, compute_ov)               \
9956 GEN_HANDLER(name, 0x1F, 0x0A, opc3, 0x0000F800, PPC_INTEGER),
9957 GEN_INT_ARITH_ADD(add, 0x08, 0, 0, 0)
9958 GEN_INT_ARITH_ADD(addo, 0x18, 0, 0, 1)
9959 GEN_INT_ARITH_ADD(addc, 0x00, 0, 1, 0)
9960 GEN_INT_ARITH_ADD(addco, 0x10, 0, 1, 1)
9961 GEN_INT_ARITH_ADD(adde, 0x04, 1, 1, 0)
9962 GEN_INT_ARITH_ADD(addeo, 0x14, 1, 1, 1)
9963 GEN_INT_ARITH_ADD_CONST(addme, 0x07, -1LL, 1, 1, 0)
9964 GEN_INT_ARITH_ADD_CONST(addmeo, 0x17, -1LL, 1, 1, 1)
9965 GEN_INT_ARITH_ADD_CONST(addze, 0x06, 0, 1, 1, 0)
9966 GEN_INT_ARITH_ADD_CONST(addzeo, 0x16, 0, 1, 1, 1)
9967
9968 #undef GEN_INT_ARITH_DIVW
9969 #define GEN_INT_ARITH_DIVW(name, opc3, sign, compute_ov)                      \
9970 GEN_HANDLER(name, 0x1F, 0x0B, opc3, 0x00000000, PPC_INTEGER)
9971 GEN_INT_ARITH_DIVW(divwu, 0x0E, 0, 0),
9972 GEN_INT_ARITH_DIVW(divwuo, 0x1E, 0, 1),
9973 GEN_INT_ARITH_DIVW(divw, 0x0F, 1, 0),
9974 GEN_INT_ARITH_DIVW(divwo, 0x1F, 1, 1),
9975 GEN_HANDLER_E(divwe, 0x1F, 0x0B, 0x0D, 0, PPC_NONE, PPC2_DIVE_ISA206),
9976 GEN_HANDLER_E(divweo, 0x1F, 0x0B, 0x1D, 0, PPC_NONE, PPC2_DIVE_ISA206),
9977 GEN_HANDLER_E(divweu, 0x1F, 0x0B, 0x0C, 0, PPC_NONE, PPC2_DIVE_ISA206),
9978 GEN_HANDLER_E(divweuo, 0x1F, 0x0B, 0x1C, 0, PPC_NONE, PPC2_DIVE_ISA206),
9979
9980 #if defined(TARGET_PPC64)
9981 #undef GEN_INT_ARITH_DIVD
9982 #define GEN_INT_ARITH_DIVD(name, opc3, sign, compute_ov)                      \
9983 GEN_HANDLER(name, 0x1F, 0x09, opc3, 0x00000000, PPC_64B)
9984 GEN_INT_ARITH_DIVD(divdu, 0x0E, 0, 0),
9985 GEN_INT_ARITH_DIVD(divduo, 0x1E, 0, 1),
9986 GEN_INT_ARITH_DIVD(divd, 0x0F, 1, 0),
9987 GEN_INT_ARITH_DIVD(divdo, 0x1F, 1, 1),
9988
9989 GEN_HANDLER_E(divdeu, 0x1F, 0x09, 0x0C, 0, PPC_NONE, PPC2_DIVE_ISA206),
9990 GEN_HANDLER_E(divdeuo, 0x1F, 0x09, 0x1C, 0, PPC_NONE, PPC2_DIVE_ISA206),
9991 GEN_HANDLER_E(divde, 0x1F, 0x09, 0x0D, 0, PPC_NONE, PPC2_DIVE_ISA206),
9992 GEN_HANDLER_E(divdeo, 0x1F, 0x09, 0x1D, 0, PPC_NONE, PPC2_DIVE_ISA206),
9993
9994 #undef GEN_INT_ARITH_MUL_HELPER
9995 #define GEN_INT_ARITH_MUL_HELPER(name, opc3)                                  \
9996 GEN_HANDLER(name, 0x1F, 0x09, opc3, 0x00000000, PPC_64B)
9997 GEN_INT_ARITH_MUL_HELPER(mulhdu, 0x00),
9998 GEN_INT_ARITH_MUL_HELPER(mulhd, 0x02),
9999 GEN_INT_ARITH_MUL_HELPER(mulldo, 0x17),
10000 #endif
10001
10002 #undef GEN_INT_ARITH_SUBF
10003 #undef GEN_INT_ARITH_SUBF_CONST
10004 #define GEN_INT_ARITH_SUBF(name, opc3, add_ca, compute_ca, compute_ov)        \
10005 GEN_HANDLER(name, 0x1F, 0x08, opc3, 0x00000000, PPC_INTEGER),
10006 #define GEN_INT_ARITH_SUBF_CONST(name, opc3, const_val,                       \
10007                                 add_ca, compute_ca, compute_ov)               \
10008 GEN_HANDLER(name, 0x1F, 0x08, opc3, 0x0000F800, PPC_INTEGER),
10009 GEN_INT_ARITH_SUBF(subf, 0x01, 0, 0, 0)
10010 GEN_INT_ARITH_SUBF(subfo, 0x11, 0, 0, 1)
10011 GEN_INT_ARITH_SUBF(subfc, 0x00, 0, 1, 0)
10012 GEN_INT_ARITH_SUBF(subfco, 0x10, 0, 1, 1)
10013 GEN_INT_ARITH_SUBF(subfe, 0x04, 1, 1, 0)
10014 GEN_INT_ARITH_SUBF(subfeo, 0x14, 1, 1, 1)
10015 GEN_INT_ARITH_SUBF_CONST(subfme, 0x07, -1LL, 1, 1, 0)
10016 GEN_INT_ARITH_SUBF_CONST(subfmeo, 0x17, -1LL, 1, 1, 1)
10017 GEN_INT_ARITH_SUBF_CONST(subfze, 0x06, 0, 1, 1, 0)
10018 GEN_INT_ARITH_SUBF_CONST(subfzeo, 0x16, 0, 1, 1, 1)
10019
10020 #undef GEN_LOGICAL1
10021 #undef GEN_LOGICAL2
10022 #define GEN_LOGICAL2(name, tcg_op, opc, type)                                 \
10023 GEN_HANDLER(name, 0x1F, 0x1C, opc, 0x00000000, type)
10024 #define GEN_LOGICAL1(name, tcg_op, opc, type)                                 \
10025 GEN_HANDLER(name, 0x1F, 0x1A, opc, 0x00000000, type)
10026 GEN_LOGICAL2(and, tcg_gen_and_tl, 0x00, PPC_INTEGER),
10027 GEN_LOGICAL2(andc, tcg_gen_andc_tl, 0x01, PPC_INTEGER),
10028 GEN_LOGICAL2(eqv, tcg_gen_eqv_tl, 0x08, PPC_INTEGER),
10029 GEN_LOGICAL1(extsb, tcg_gen_ext8s_tl, 0x1D, PPC_INTEGER),
10030 GEN_LOGICAL1(extsh, tcg_gen_ext16s_tl, 0x1C, PPC_INTEGER),
10031 GEN_LOGICAL2(nand, tcg_gen_nand_tl, 0x0E, PPC_INTEGER),
10032 GEN_LOGICAL2(nor, tcg_gen_nor_tl, 0x03, PPC_INTEGER),
10033 GEN_LOGICAL2(orc, tcg_gen_orc_tl, 0x0C, PPC_INTEGER),
10034 #if defined(TARGET_PPC64)
10035 GEN_LOGICAL1(extsw, tcg_gen_ext32s_tl, 0x1E, PPC_64B),
10036 #endif
10037
10038 #if defined(TARGET_PPC64)
10039 #undef GEN_PPC64_R2
10040 #undef GEN_PPC64_R4
10041 #define GEN_PPC64_R2(name, opc1, opc2)                                        \
10042 GEN_HANDLER2(name##0, stringify(name), opc1, opc2, 0xFF, 0x00000000, PPC_64B),\
10043 GEN_HANDLER2(name##1, stringify(name), opc1, opc2 | 0x10, 0xFF, 0x00000000,   \
10044              PPC_64B)
10045 #define GEN_PPC64_R4(name, opc1, opc2)                                        \
10046 GEN_HANDLER2(name##0, stringify(name), opc1, opc2, 0xFF, 0x00000000, PPC_64B),\
10047 GEN_HANDLER2(name##1, stringify(name), opc1, opc2 | 0x01, 0xFF, 0x00000000,   \
10048              PPC_64B),                                                        \
10049 GEN_HANDLER2(name##2, stringify(name), opc1, opc2 | 0x10, 0xFF, 0x00000000,   \
10050              PPC_64B),                                                        \
10051 GEN_HANDLER2(name##3, stringify(name), opc1, opc2 | 0x11, 0xFF, 0x00000000,   \
10052              PPC_64B)
10053 GEN_PPC64_R4(rldicl, 0x1E, 0x00),
10054 GEN_PPC64_R4(rldicr, 0x1E, 0x02),
10055 GEN_PPC64_R4(rldic, 0x1E, 0x04),
10056 GEN_PPC64_R2(rldcl, 0x1E, 0x08),
10057 GEN_PPC64_R2(rldcr, 0x1E, 0x09),
10058 GEN_PPC64_R4(rldimi, 0x1E, 0x06),
10059 #endif
10060
10061 #undef _GEN_FLOAT_ACB
10062 #undef GEN_FLOAT_ACB
10063 #undef _GEN_FLOAT_AB
10064 #undef GEN_FLOAT_AB
10065 #undef _GEN_FLOAT_AC
10066 #undef GEN_FLOAT_AC
10067 #undef GEN_FLOAT_B
10068 #undef GEN_FLOAT_BS
10069 #define _GEN_FLOAT_ACB(name, op, op1, op2, isfloat, set_fprf, type)           \
10070 GEN_HANDLER(f##name, op1, op2, 0xFF, 0x00000000, type)
10071 #define GEN_FLOAT_ACB(name, op2, set_fprf, type)                              \
10072 _GEN_FLOAT_ACB(name, name, 0x3F, op2, 0, set_fprf, type),                     \
10073 _GEN_FLOAT_ACB(name##s, name, 0x3B, op2, 1, set_fprf, type)
10074 #define _GEN_FLOAT_AB(name, op, op1, op2, inval, isfloat, set_fprf, type)     \
10075 GEN_HANDLER(f##name, op1, op2, 0xFF, inval, type)
10076 #define GEN_FLOAT_AB(name, op2, inval, set_fprf, type)                        \
10077 _GEN_FLOAT_AB(name, name, 0x3F, op2, inval, 0, set_fprf, type),               \
10078 _GEN_FLOAT_AB(name##s, name, 0x3B, op2, inval, 1, set_fprf, type)
10079 #define _GEN_FLOAT_AC(name, op, op1, op2, inval, isfloat, set_fprf, type)     \
10080 GEN_HANDLER(f##name, op1, op2, 0xFF, inval, type)
10081 #define GEN_FLOAT_AC(name, op2, inval, set_fprf, type)                        \
10082 _GEN_FLOAT_AC(name, name, 0x3F, op2, inval, 0, set_fprf, type),               \
10083 _GEN_FLOAT_AC(name##s, name, 0x3B, op2, inval, 1, set_fprf, type)
10084 #define GEN_FLOAT_B(name, op2, op3, set_fprf, type)                           \
10085 GEN_HANDLER(f##name, 0x3F, op2, op3, 0x001F0000, type)
10086 #define GEN_FLOAT_BS(name, op1, op2, set_fprf, type)                          \
10087 GEN_HANDLER(f##name, op1, op2, 0xFF, 0x001F07C0, type)
10088
10089 GEN_FLOAT_AB(add, 0x15, 0x000007C0, 1, PPC_FLOAT),
10090 GEN_FLOAT_AB(div, 0x12, 0x000007C0, 1, PPC_FLOAT),
10091 GEN_FLOAT_AC(mul, 0x19, 0x0000F800, 1, PPC_FLOAT),
10092 GEN_FLOAT_BS(re, 0x3F, 0x18, 1, PPC_FLOAT_EXT),
10093 GEN_FLOAT_BS(res, 0x3B, 0x18, 1, PPC_FLOAT_FRES),
10094 GEN_FLOAT_BS(rsqrte, 0x3F, 0x1A, 1, PPC_FLOAT_FRSQRTE),
10095 _GEN_FLOAT_ACB(sel, sel, 0x3F, 0x17, 0, 0, PPC_FLOAT_FSEL),
10096 GEN_FLOAT_AB(sub, 0x14, 0x000007C0, 1, PPC_FLOAT),
10097 GEN_FLOAT_ACB(madd, 0x1D, 1, PPC_FLOAT),
10098 GEN_FLOAT_ACB(msub, 0x1C, 1, PPC_FLOAT),
10099 GEN_FLOAT_ACB(nmadd, 0x1F, 1, PPC_FLOAT),
10100 GEN_FLOAT_ACB(nmsub, 0x1E, 1, PPC_FLOAT),
10101 GEN_HANDLER_E(ftdiv, 0x3F, 0x00, 0x04, 1, PPC_NONE, PPC2_FP_TST_ISA206),
10102 GEN_HANDLER_E(ftsqrt, 0x3F, 0x00, 0x05, 1, PPC_NONE, PPC2_FP_TST_ISA206),
10103 GEN_FLOAT_B(ctiw, 0x0E, 0x00, 0, PPC_FLOAT),
10104 GEN_HANDLER_E(fctiwu, 0x3F, 0x0E, 0x04, 0, PPC_NONE, PPC2_FP_CVT_ISA206),
10105 GEN_FLOAT_B(ctiwz, 0x0F, 0x00, 0, PPC_FLOAT),
10106 GEN_HANDLER_E(fctiwuz, 0x3F, 0x0F, 0x04, 0, PPC_NONE, PPC2_FP_CVT_ISA206),
10107 GEN_FLOAT_B(rsp, 0x0C, 0x00, 1, PPC_FLOAT),
10108 GEN_HANDLER_E(fcfid, 0x3F, 0x0E, 0x1A, 0x001F0000, PPC_NONE, PPC2_FP_CVT_S64),
10109 GEN_HANDLER_E(fcfids, 0x3B, 0x0E, 0x1A, 0, PPC_NONE, PPC2_FP_CVT_ISA206),
10110 GEN_HANDLER_E(fcfidu, 0x3F, 0x0E, 0x1E, 0, PPC_NONE, PPC2_FP_CVT_ISA206),
10111 GEN_HANDLER_E(fcfidus, 0x3B, 0x0E, 0x1E, 0, PPC_NONE, PPC2_FP_CVT_ISA206),
10112 GEN_HANDLER_E(fctid, 0x3F, 0x0E, 0x19, 0x001F0000, PPC_NONE, PPC2_FP_CVT_S64),
10113 GEN_HANDLER_E(fctidu, 0x3F, 0x0E, 0x1D, 0, PPC_NONE, PPC2_FP_CVT_ISA206),
10114 GEN_HANDLER_E(fctidz, 0x3F, 0x0F, 0x19, 0x001F0000, PPC_NONE, PPC2_FP_CVT_S64),
10115 GEN_HANDLER_E(fctiduz, 0x3F, 0x0F, 0x1D, 0, PPC_NONE, PPC2_FP_CVT_ISA206),
10116 GEN_FLOAT_B(rin, 0x08, 0x0C, 1, PPC_FLOAT_EXT),
10117 GEN_FLOAT_B(riz, 0x08, 0x0D, 1, PPC_FLOAT_EXT),
10118 GEN_FLOAT_B(rip, 0x08, 0x0E, 1, PPC_FLOAT_EXT),
10119 GEN_FLOAT_B(rim, 0x08, 0x0F, 1, PPC_FLOAT_EXT),
10120
10121 #undef GEN_LD
10122 #undef GEN_LDU
10123 #undef GEN_LDUX
10124 #undef GEN_LDX_E
10125 #undef GEN_LDS
10126 #define GEN_LD(name, ldop, opc, type)                                         \
10127 GEN_HANDLER(name, opc, 0xFF, 0xFF, 0x00000000, type),
10128 #define GEN_LDU(name, ldop, opc, type)                                        \
10129 GEN_HANDLER(name##u, opc, 0xFF, 0xFF, 0x00000000, type),
10130 #define GEN_LDUX(name, ldop, opc2, opc3, type)                                \
10131 GEN_HANDLER(name##ux, 0x1F, opc2, opc3, 0x00000001, type),
10132 #define GEN_LDX_E(name, ldop, opc2, opc3, type, type2)                        \
10133 GEN_HANDLER_E(name##x, 0x1F, opc2, opc3, 0x00000001, type, type2),
10134 #define GEN_LDS(name, ldop, op, type)                                         \
10135 GEN_LD(name, ldop, op | 0x20, type)                                           \
10136 GEN_LDU(name, ldop, op | 0x21, type)                                          \
10137 GEN_LDUX(name, ldop, 0x17, op | 0x01, type)                                   \
10138 GEN_LDX(name, ldop, 0x17, op | 0x00, type)
10139
10140 GEN_LDS(lbz, ld8u, 0x02, PPC_INTEGER)
10141 GEN_LDS(lha, ld16s, 0x0A, PPC_INTEGER)
10142 GEN_LDS(lhz, ld16u, 0x08, PPC_INTEGER)
10143 GEN_LDS(lwz, ld32u, 0x00, PPC_INTEGER)
10144 #if defined(TARGET_PPC64)
10145 GEN_LDUX(lwa, ld32s, 0x15, 0x0B, PPC_64B)
10146 GEN_LDX(lwa, ld32s, 0x15, 0x0A, PPC_64B)
10147 GEN_LDUX(ld, ld64, 0x15, 0x01, PPC_64B)
10148 GEN_LDX(ld, ld64, 0x15, 0x00, PPC_64B)
10149 GEN_LDX_E(ldbr, ld64ur, 0x14, 0x10, PPC_NONE, PPC2_DBRX)
10150 #endif
10151 GEN_LDX(lhbr, ld16ur, 0x16, 0x18, PPC_INTEGER)
10152 GEN_LDX(lwbr, ld32ur, 0x16, 0x10, PPC_INTEGER)
10153
10154 #undef GEN_ST
10155 #undef GEN_STU
10156 #undef GEN_STUX
10157 #undef GEN_STX_E
10158 #undef GEN_STS
10159 #define GEN_ST(name, stop, opc, type)                                         \
10160 GEN_HANDLER(name, opc, 0xFF, 0xFF, 0x00000000, type),
10161 #define GEN_STU(name, stop, opc, type)                                        \
10162 GEN_HANDLER(stop##u, opc, 0xFF, 0xFF, 0x00000000, type),
10163 #define GEN_STUX(name, stop, opc2, opc3, type)                                \
10164 GEN_HANDLER(name##ux, 0x1F, opc2, opc3, 0x00000001, type),
10165 #define GEN_STX_E(name, stop, opc2, opc3, type, type2)                        \
10166 GEN_HANDLER_E(name##x, 0x1F, opc2, opc3, 0x00000001, type, type2),
10167 #define GEN_STS(name, stop, op, type)                                         \
10168 GEN_ST(name, stop, op | 0x20, type)                                           \
10169 GEN_STU(name, stop, op | 0x21, type)                                          \
10170 GEN_STUX(name, stop, 0x17, op | 0x01, type)                                   \
10171 GEN_STX(name, stop, 0x17, op | 0x00, type)
10172
10173 GEN_STS(stb, st8, 0x06, PPC_INTEGER)
10174 GEN_STS(sth, st16, 0x0C, PPC_INTEGER)
10175 GEN_STS(stw, st32, 0x04, PPC_INTEGER)
10176 #if defined(TARGET_PPC64)
10177 GEN_STUX(std, st64, 0x15, 0x05, PPC_64B)
10178 GEN_STX(std, st64, 0x15, 0x04, PPC_64B)
10179 GEN_STX_E(stdbr, st64r, 0x14, 0x14, PPC_NONE, PPC2_DBRX)
10180 #endif
10181 GEN_STX(sthbr, st16r, 0x16, 0x1C, PPC_INTEGER)
10182 GEN_STX(stwbr, st32r, 0x16, 0x14, PPC_INTEGER)
10183
10184 #undef GEN_LDF
10185 #undef GEN_LDUF
10186 #undef GEN_LDUXF
10187 #undef GEN_LDXF
10188 #undef GEN_LDFS
10189 #define GEN_LDF(name, ldop, opc, type)                                        \
10190 GEN_HANDLER(name, opc, 0xFF, 0xFF, 0x00000000, type),
10191 #define GEN_LDUF(name, ldop, opc, type)                                       \
10192 GEN_HANDLER(name##u, opc, 0xFF, 0xFF, 0x00000000, type),
10193 #define GEN_LDUXF(name, ldop, opc, type)                                      \
10194 GEN_HANDLER(name##ux, 0x1F, 0x17, opc, 0x00000001, type),
10195 #define GEN_LDXF(name, ldop, opc2, opc3, type)                                \
10196 GEN_HANDLER(name##x, 0x1F, opc2, opc3, 0x00000001, type),
10197 #define GEN_LDFS(name, ldop, op, type)                                        \
10198 GEN_LDF(name, ldop, op | 0x20, type)                                          \
10199 GEN_LDUF(name, ldop, op | 0x21, type)                                         \
10200 GEN_LDUXF(name, ldop, op | 0x01, type)                                        \
10201 GEN_LDXF(name, ldop, 0x17, op | 0x00, type)
10202
10203 GEN_LDFS(lfd, ld64, 0x12, PPC_FLOAT)
10204 GEN_LDFS(lfs, ld32fs, 0x10, PPC_FLOAT)
10205 GEN_HANDLER_E(lfiwax, 0x1f, 0x17, 0x1a, 0x00000001, PPC_NONE, PPC2_ISA205),
10206 GEN_HANDLER_E(lfiwzx, 0x1f, 0x17, 0x1b, 0x1, PPC_NONE, PPC2_FP_CVT_ISA206),
10207 GEN_HANDLER_E(lfdp, 0x39, 0xFF, 0xFF, 0x00200003, PPC_NONE, PPC2_ISA205),
10208 GEN_HANDLER_E(lfdpx, 0x1F, 0x17, 0x18, 0x00200001, PPC_NONE, PPC2_ISA205),
10209
10210 #undef GEN_STF
10211 #undef GEN_STUF
10212 #undef GEN_STUXF
10213 #undef GEN_STXF
10214 #undef GEN_STFS
10215 #define GEN_STF(name, stop, opc, type)                                        \
10216 GEN_HANDLER(name, opc, 0xFF, 0xFF, 0x00000000, type),
10217 #define GEN_STUF(name, stop, opc, type)                                       \
10218 GEN_HANDLER(name##u, opc, 0xFF, 0xFF, 0x00000000, type),
10219 #define GEN_STUXF(name, stop, opc, type)                                      \
10220 GEN_HANDLER(name##ux, 0x1F, 0x17, opc, 0x00000001, type),
10221 #define GEN_STXF(name, stop, opc2, opc3, type)                                \
10222 GEN_HANDLER(name##x, 0x1F, opc2, opc3, 0x00000001, type),
10223 #define GEN_STFS(name, stop, op, type)                                        \
10224 GEN_STF(name, stop, op | 0x20, type)                                          \
10225 GEN_STUF(name, stop, op | 0x21, type)                                         \
10226 GEN_STUXF(name, stop, op | 0x01, type)                                        \
10227 GEN_STXF(name, stop, 0x17, op | 0x00, type)
10228
10229 GEN_STFS(stfd, st64, 0x16, PPC_FLOAT)
10230 GEN_STFS(stfs, st32fs, 0x14, PPC_FLOAT)
10231 GEN_STXF(stfiw, st32fiw, 0x17, 0x1E, PPC_FLOAT_STFIWX)
10232 GEN_HANDLER_E(stfdp, 0x3D, 0xFF, 0xFF, 0x00200003, PPC_NONE, PPC2_ISA205),
10233 GEN_HANDLER_E(stfdpx, 0x1F, 0x17, 0x1C, 0x00200001, PPC_NONE, PPC2_ISA205),
10234
10235 #undef GEN_CRLOGIC
10236 #define GEN_CRLOGIC(name, tcg_op, opc)                                        \
10237 GEN_HANDLER(name, 0x13, 0x01, opc, 0x00000001, PPC_INTEGER)
10238 GEN_CRLOGIC(crand, tcg_gen_and_i32, 0x08),
10239 GEN_CRLOGIC(crandc, tcg_gen_andc_i32, 0x04),
10240 GEN_CRLOGIC(creqv, tcg_gen_eqv_i32, 0x09),
10241 GEN_CRLOGIC(crnand, tcg_gen_nand_i32, 0x07),
10242 GEN_CRLOGIC(crnor, tcg_gen_nor_i32, 0x01),
10243 GEN_CRLOGIC(cror, tcg_gen_or_i32, 0x0E),
10244 GEN_CRLOGIC(crorc, tcg_gen_orc_i32, 0x0D),
10245 GEN_CRLOGIC(crxor, tcg_gen_xor_i32, 0x06),
10246
10247 #undef GEN_MAC_HANDLER
10248 #define GEN_MAC_HANDLER(name, opc2, opc3)                                     \
10249 GEN_HANDLER(name, 0x04, opc2, opc3, 0x00000000, PPC_405_MAC)
10250 GEN_MAC_HANDLER(macchw, 0x0C, 0x05),
10251 GEN_MAC_HANDLER(macchwo, 0x0C, 0x15),
10252 GEN_MAC_HANDLER(macchws, 0x0C, 0x07),
10253 GEN_MAC_HANDLER(macchwso, 0x0C, 0x17),
10254 GEN_MAC_HANDLER(macchwsu, 0x0C, 0x06),
10255 GEN_MAC_HANDLER(macchwsuo, 0x0C, 0x16),
10256 GEN_MAC_HANDLER(macchwu, 0x0C, 0x04),
10257 GEN_MAC_HANDLER(macchwuo, 0x0C, 0x14),
10258 GEN_MAC_HANDLER(machhw, 0x0C, 0x01),
10259 GEN_MAC_HANDLER(machhwo, 0x0C, 0x11),
10260 GEN_MAC_HANDLER(machhws, 0x0C, 0x03),
10261 GEN_MAC_HANDLER(machhwso, 0x0C, 0x13),
10262 GEN_MAC_HANDLER(machhwsu, 0x0C, 0x02),
10263 GEN_MAC_HANDLER(machhwsuo, 0x0C, 0x12),
10264 GEN_MAC_HANDLER(machhwu, 0x0C, 0x00),
10265 GEN_MAC_HANDLER(machhwuo, 0x0C, 0x10),
10266 GEN_MAC_HANDLER(maclhw, 0x0C, 0x0D),
10267 GEN_MAC_HANDLER(maclhwo, 0x0C, 0x1D),
10268 GEN_MAC_HANDLER(maclhws, 0x0C, 0x0F),
10269 GEN_MAC_HANDLER(maclhwso, 0x0C, 0x1F),
10270 GEN_MAC_HANDLER(maclhwu, 0x0C, 0x0C),
10271 GEN_MAC_HANDLER(maclhwuo, 0x0C, 0x1C),
10272 GEN_MAC_HANDLER(maclhwsu, 0x0C, 0x0E),
10273 GEN_MAC_HANDLER(maclhwsuo, 0x0C, 0x1E),
10274 GEN_MAC_HANDLER(nmacchw, 0x0E, 0x05),
10275 GEN_MAC_HANDLER(nmacchwo, 0x0E, 0x15),
10276 GEN_MAC_HANDLER(nmacchws, 0x0E, 0x07),
10277 GEN_MAC_HANDLER(nmacchwso, 0x0E, 0x17),
10278 GEN_MAC_HANDLER(nmachhw, 0x0E, 0x01),
10279 GEN_MAC_HANDLER(nmachhwo, 0x0E, 0x11),
10280 GEN_MAC_HANDLER(nmachhws, 0x0E, 0x03),
10281 GEN_MAC_HANDLER(nmachhwso, 0x0E, 0x13),
10282 GEN_MAC_HANDLER(nmaclhw, 0x0E, 0x0D),
10283 GEN_MAC_HANDLER(nmaclhwo, 0x0E, 0x1D),
10284 GEN_MAC_HANDLER(nmaclhws, 0x0E, 0x0F),
10285 GEN_MAC_HANDLER(nmaclhwso, 0x0E, 0x1F),
10286 GEN_MAC_HANDLER(mulchw, 0x08, 0x05),
10287 GEN_MAC_HANDLER(mulchwu, 0x08, 0x04),
10288 GEN_MAC_HANDLER(mulhhw, 0x08, 0x01),
10289 GEN_MAC_HANDLER(mulhhwu, 0x08, 0x00),
10290 GEN_MAC_HANDLER(mullhw, 0x08, 0x0D),
10291 GEN_MAC_HANDLER(mullhwu, 0x08, 0x0C),
10292
10293 #undef GEN_VR_LDX
10294 #undef GEN_VR_STX
10295 #undef GEN_VR_LVE
10296 #undef GEN_VR_STVE
10297 #define GEN_VR_LDX(name, opc2, opc3)                                          \
10298 GEN_HANDLER(name, 0x1F, opc2, opc3, 0x00000001, PPC_ALTIVEC)
10299 #define GEN_VR_STX(name, opc2, opc3)                                          \
10300 GEN_HANDLER(st##name, 0x1F, opc2, opc3, 0x00000001, PPC_ALTIVEC)
10301 #define GEN_VR_LVE(name, opc2, opc3)                                    \
10302     GEN_HANDLER(lve##name, 0x1F, opc2, opc3, 0x00000001, PPC_ALTIVEC)
10303 #define GEN_VR_STVE(name, opc2, opc3)                                   \
10304     GEN_HANDLER(stve##name, 0x1F, opc2, opc3, 0x00000001, PPC_ALTIVEC)
10305 GEN_VR_LDX(lvx, 0x07, 0x03),
10306 GEN_VR_LDX(lvxl, 0x07, 0x0B),
10307 GEN_VR_LVE(bx, 0x07, 0x00),
10308 GEN_VR_LVE(hx, 0x07, 0x01),
10309 GEN_VR_LVE(wx, 0x07, 0x02),
10310 GEN_VR_STX(svx, 0x07, 0x07),
10311 GEN_VR_STX(svxl, 0x07, 0x0F),
10312 GEN_VR_STVE(bx, 0x07, 0x04),
10313 GEN_VR_STVE(hx, 0x07, 0x05),
10314 GEN_VR_STVE(wx, 0x07, 0x06),
10315
10316 #undef GEN_VX_LOGICAL
10317 #define GEN_VX_LOGICAL(name, tcg_op, opc2, opc3)                        \
10318 GEN_HANDLER(name, 0x04, opc2, opc3, 0x00000000, PPC_ALTIVEC)
10319
10320 #undef GEN_VX_LOGICAL_207
10321 #define GEN_VX_LOGICAL_207(name, tcg_op, opc2, opc3) \
10322 GEN_HANDLER_E(name, 0x04, opc2, opc3, 0x00000000, PPC_NONE, PPC2_ALTIVEC_207)
10323
10324 GEN_VX_LOGICAL(vand, tcg_gen_and_i64, 2, 16),
10325 GEN_VX_LOGICAL(vandc, tcg_gen_andc_i64, 2, 17),
10326 GEN_VX_LOGICAL(vor, tcg_gen_or_i64, 2, 18),
10327 GEN_VX_LOGICAL(vxor, tcg_gen_xor_i64, 2, 19),
10328 GEN_VX_LOGICAL(vnor, tcg_gen_nor_i64, 2, 20),
10329 GEN_VX_LOGICAL_207(veqv, tcg_gen_eqv_i64, 2, 26),
10330 GEN_VX_LOGICAL_207(vnand, tcg_gen_nand_i64, 2, 22),
10331 GEN_VX_LOGICAL_207(vorc, tcg_gen_orc_i64, 2, 21),
10332
10333 #undef GEN_VXFORM
10334 #define GEN_VXFORM(name, opc2, opc3)                                    \
10335 GEN_HANDLER(name, 0x04, opc2, opc3, 0x00000000, PPC_ALTIVEC)
10336
10337 #undef GEN_VXFORM_207
10338 #define GEN_VXFORM_207(name, opc2, opc3) \
10339 GEN_HANDLER_E(name, 0x04, opc2, opc3, 0x00000000, PPC_NONE, PPC2_ALTIVEC_207)
10340
10341 #undef GEN_VXFORM_DUAL
10342 #define GEN_VXFORM_DUAL(name0, name1, opc2, opc3, type0, type1) \
10343 GEN_HANDLER_E(name0##_##name1, 0x4, opc2, opc3, 0x00000000, type0, type1)
10344
10345 #undef GEN_VXRFORM_DUAL
10346 #define GEN_VXRFORM_DUAL(name0, name1, opc2, opc3, tp0, tp1) \
10347 GEN_HANDLER_E(name0##_##name1, 0x4, opc2, opc3, 0x00000000, tp0, tp1), \
10348 GEN_HANDLER_E(name0##_##name1, 0x4, opc2, (opc3 | 0x10), 0x00000000, tp0, tp1),
10349
10350 GEN_VXFORM(vaddubm, 0, 0),
10351 GEN_VXFORM(vadduhm, 0, 1),
10352 GEN_VXFORM(vadduwm, 0, 2),
10353 GEN_VXFORM_207(vaddudm, 0, 3),
10354 GEN_VXFORM_DUAL(vsububm, bcdadd, 0, 16, PPC_ALTIVEC, PPC_NONE),
10355 GEN_VXFORM_DUAL(vsubuhm, bcdsub, 0, 17, PPC_ALTIVEC, PPC_NONE),
10356 GEN_VXFORM(vsubuwm, 0, 18),
10357 GEN_VXFORM_207(vsubudm, 0, 19),
10358 GEN_VXFORM(vmaxub, 1, 0),
10359 GEN_VXFORM(vmaxuh, 1, 1),
10360 GEN_VXFORM(vmaxuw, 1, 2),
10361 GEN_VXFORM_207(vmaxud, 1, 3),
10362 GEN_VXFORM(vmaxsb, 1, 4),
10363 GEN_VXFORM(vmaxsh, 1, 5),
10364 GEN_VXFORM(vmaxsw, 1, 6),
10365 GEN_VXFORM_207(vmaxsd, 1, 7),
10366 GEN_VXFORM(vminub, 1, 8),
10367 GEN_VXFORM(vminuh, 1, 9),
10368 GEN_VXFORM(vminuw, 1, 10),
10369 GEN_VXFORM_207(vminud, 1, 11),
10370 GEN_VXFORM(vminsb, 1, 12),
10371 GEN_VXFORM(vminsh, 1, 13),
10372 GEN_VXFORM(vminsw, 1, 14),
10373 GEN_VXFORM_207(vminsd, 1, 15),
10374 GEN_VXFORM(vavgub, 1, 16),
10375 GEN_VXFORM(vavguh, 1, 17),
10376 GEN_VXFORM(vavguw, 1, 18),
10377 GEN_VXFORM(vavgsb, 1, 20),
10378 GEN_VXFORM(vavgsh, 1, 21),
10379 GEN_VXFORM(vavgsw, 1, 22),
10380 GEN_VXFORM(vmrghb, 6, 0),
10381 GEN_VXFORM(vmrghh, 6, 1),
10382 GEN_VXFORM(vmrghw, 6, 2),
10383 GEN_VXFORM(vmrglb, 6, 4),
10384 GEN_VXFORM(vmrglh, 6, 5),
10385 GEN_VXFORM(vmrglw, 6, 6),
10386 GEN_VXFORM_207(vmrgew, 6, 30),
10387 GEN_VXFORM_207(vmrgow, 6, 26),
10388 GEN_VXFORM(vmuloub, 4, 0),
10389 GEN_VXFORM(vmulouh, 4, 1),
10390 GEN_VXFORM_DUAL(vmulouw, vmuluwm, 4, 2, PPC_ALTIVEC, PPC_NONE),
10391 GEN_VXFORM(vmulosb, 4, 4),
10392 GEN_VXFORM(vmulosh, 4, 5),
10393 GEN_VXFORM_207(vmulosw, 4, 6),
10394 GEN_VXFORM(vmuleub, 4, 8),
10395 GEN_VXFORM(vmuleuh, 4, 9),
10396 GEN_VXFORM_207(vmuleuw, 4, 10),
10397 GEN_VXFORM(vmulesb, 4, 12),
10398 GEN_VXFORM(vmulesh, 4, 13),
10399 GEN_VXFORM_207(vmulesw, 4, 14),
10400 GEN_VXFORM(vslb, 2, 4),
10401 GEN_VXFORM(vslh, 2, 5),
10402 GEN_VXFORM(vslw, 2, 6),
10403 GEN_VXFORM_207(vsld, 2, 23),
10404 GEN_VXFORM(vsrb, 2, 8),
10405 GEN_VXFORM(vsrh, 2, 9),
10406 GEN_VXFORM(vsrw, 2, 10),
10407 GEN_VXFORM_207(vsrd, 2, 27),
10408 GEN_VXFORM(vsrab, 2, 12),
10409 GEN_VXFORM(vsrah, 2, 13),
10410 GEN_VXFORM(vsraw, 2, 14),
10411 GEN_VXFORM_207(vsrad, 2, 15),
10412 GEN_VXFORM(vslo, 6, 16),
10413 GEN_VXFORM(vsro, 6, 17),
10414 GEN_VXFORM(vaddcuw, 0, 6),
10415 GEN_VXFORM(vsubcuw, 0, 22),
10416 GEN_VXFORM(vaddubs, 0, 8),
10417 GEN_VXFORM(vadduhs, 0, 9),
10418 GEN_VXFORM(vadduws, 0, 10),
10419 GEN_VXFORM(vaddsbs, 0, 12),
10420 GEN_VXFORM(vaddshs, 0, 13),
10421 GEN_VXFORM(vaddsws, 0, 14),
10422 GEN_VXFORM_DUAL(vsububs, bcdadd, 0, 24, PPC_ALTIVEC, PPC_NONE),
10423 GEN_VXFORM_DUAL(vsubuhs, bcdsub, 0, 25, PPC_ALTIVEC, PPC_NONE),
10424 GEN_VXFORM(vsubuws, 0, 26),
10425 GEN_VXFORM(vsubsbs, 0, 28),
10426 GEN_VXFORM(vsubshs, 0, 29),
10427 GEN_VXFORM(vsubsws, 0, 30),
10428 GEN_VXFORM_207(vadduqm, 0, 4),
10429 GEN_VXFORM_207(vaddcuq, 0, 5),
10430 GEN_VXFORM_DUAL(vaddeuqm, vaddecuq, 30, 0xFF, PPC_NONE, PPC2_ALTIVEC_207),
10431 GEN_VXFORM_207(vsubuqm, 0, 20),
10432 GEN_VXFORM_207(vsubcuq, 0, 21),
10433 GEN_VXFORM_DUAL(vsubeuqm, vsubecuq, 31, 0xFF, PPC_NONE, PPC2_ALTIVEC_207),
10434 GEN_VXFORM(vrlb, 2, 0),
10435 GEN_VXFORM(vrlh, 2, 1),
10436 GEN_VXFORM(vrlw, 2, 2),
10437 GEN_VXFORM_207(vrld, 2, 3),
10438 GEN_VXFORM(vsl, 2, 7),
10439 GEN_VXFORM(vsr, 2, 11),
10440 GEN_VXFORM(vpkuhum, 7, 0),
10441 GEN_VXFORM(vpkuwum, 7, 1),
10442 GEN_VXFORM_207(vpkudum, 7, 17),
10443 GEN_VXFORM(vpkuhus, 7, 2),
10444 GEN_VXFORM(vpkuwus, 7, 3),
10445 GEN_VXFORM_207(vpkudus, 7, 19),
10446 GEN_VXFORM(vpkshus, 7, 4),
10447 GEN_VXFORM(vpkswus, 7, 5),
10448 GEN_VXFORM_207(vpksdus, 7, 21),
10449 GEN_VXFORM(vpkshss, 7, 6),
10450 GEN_VXFORM(vpkswss, 7, 7),
10451 GEN_VXFORM_207(vpksdss, 7, 23),
10452 GEN_VXFORM(vpkpx, 7, 12),
10453 GEN_VXFORM(vsum4ubs, 4, 24),
10454 GEN_VXFORM(vsum4sbs, 4, 28),
10455 GEN_VXFORM(vsum4shs, 4, 25),
10456 GEN_VXFORM(vsum2sws, 4, 26),
10457 GEN_VXFORM(vsumsws, 4, 30),
10458 GEN_VXFORM(vaddfp, 5, 0),
10459 GEN_VXFORM(vsubfp, 5, 1),
10460 GEN_VXFORM(vmaxfp, 5, 16),
10461 GEN_VXFORM(vminfp, 5, 17),
10462
10463 #undef GEN_VXRFORM1
10464 #undef GEN_VXRFORM
10465 #define GEN_VXRFORM1(opname, name, str, opc2, opc3)                     \
10466     GEN_HANDLER2(name, str, 0x4, opc2, opc3, 0x00000000, PPC_ALTIVEC),
10467 #define GEN_VXRFORM(name, opc2, opc3)                                \
10468     GEN_VXRFORM1(name, name, #name, opc2, opc3)                      \
10469     GEN_VXRFORM1(name##_dot, name##_, #name ".", opc2, (opc3 | (0x1 << 4)))
10470 GEN_VXRFORM(vcmpequb, 3, 0)
10471 GEN_VXRFORM(vcmpequh, 3, 1)
10472 GEN_VXRFORM(vcmpequw, 3, 2)
10473 GEN_VXRFORM(vcmpgtsb, 3, 12)
10474 GEN_VXRFORM(vcmpgtsh, 3, 13)
10475 GEN_VXRFORM(vcmpgtsw, 3, 14)
10476 GEN_VXRFORM(vcmpgtub, 3, 8)
10477 GEN_VXRFORM(vcmpgtuh, 3, 9)
10478 GEN_VXRFORM(vcmpgtuw, 3, 10)
10479 GEN_VXRFORM_DUAL(vcmpeqfp, vcmpequd, 3, 3, PPC_ALTIVEC, PPC_NONE)
10480 GEN_VXRFORM(vcmpgefp, 3, 7)
10481 GEN_VXRFORM_DUAL(vcmpgtfp, vcmpgtud, 3, 11, PPC_ALTIVEC, PPC_NONE)
10482 GEN_VXRFORM_DUAL(vcmpbfp, vcmpgtsd, 3, 15, PPC_ALTIVEC, PPC_NONE)
10483
10484 #undef GEN_VXFORM_SIMM
10485 #define GEN_VXFORM_SIMM(name, opc2, opc3)                               \
10486     GEN_HANDLER(name, 0x04, opc2, opc3, 0x00000000, PPC_ALTIVEC)
10487 GEN_VXFORM_SIMM(vspltisb, 6, 12),
10488 GEN_VXFORM_SIMM(vspltish, 6, 13),
10489 GEN_VXFORM_SIMM(vspltisw, 6, 14),
10490
10491 #undef GEN_VXFORM_NOA
10492 #define GEN_VXFORM_NOA(name, opc2, opc3)                                \
10493     GEN_HANDLER(name, 0x04, opc2, opc3, 0x001f0000, PPC_ALTIVEC)
10494 GEN_VXFORM_NOA(vupkhsb, 7, 8),
10495 GEN_VXFORM_NOA(vupkhsh, 7, 9),
10496 GEN_VXFORM_207(vupkhsw, 7, 25),
10497 GEN_VXFORM_NOA(vupklsb, 7, 10),
10498 GEN_VXFORM_NOA(vupklsh, 7, 11),
10499 GEN_VXFORM_207(vupklsw, 7, 27),
10500 GEN_VXFORM_NOA(vupkhpx, 7, 13),
10501 GEN_VXFORM_NOA(vupklpx, 7, 15),
10502 GEN_VXFORM_NOA(vrefp, 5, 4),
10503 GEN_VXFORM_NOA(vrsqrtefp, 5, 5),
10504 GEN_VXFORM_NOA(vexptefp, 5, 6),
10505 GEN_VXFORM_NOA(vlogefp, 5, 7),
10506 GEN_VXFORM_NOA(vrfim, 5, 11),
10507 GEN_VXFORM_NOA(vrfin, 5, 8),
10508 GEN_VXFORM_NOA(vrfip, 5, 10),
10509 GEN_VXFORM_NOA(vrfiz, 5, 9),
10510
10511 #undef GEN_VXFORM_UIMM
10512 #define GEN_VXFORM_UIMM(name, opc2, opc3)                               \
10513     GEN_HANDLER(name, 0x04, opc2, opc3, 0x00000000, PPC_ALTIVEC)
10514 GEN_VXFORM_UIMM(vspltb, 6, 8),
10515 GEN_VXFORM_UIMM(vsplth, 6, 9),
10516 GEN_VXFORM_UIMM(vspltw, 6, 10),
10517 GEN_VXFORM_UIMM(vcfux, 5, 12),
10518 GEN_VXFORM_UIMM(vcfsx, 5, 13),
10519 GEN_VXFORM_UIMM(vctuxs, 5, 14),
10520 GEN_VXFORM_UIMM(vctsxs, 5, 15),
10521
10522 #undef GEN_VAFORM_PAIRED
10523 #define GEN_VAFORM_PAIRED(name0, name1, opc2)                           \
10524     GEN_HANDLER(name0##_##name1, 0x04, opc2, 0xFF, 0x00000000, PPC_ALTIVEC)
10525 GEN_VAFORM_PAIRED(vmhaddshs, vmhraddshs, 16),
10526 GEN_VAFORM_PAIRED(vmsumubm, vmsummbm, 18),
10527 GEN_VAFORM_PAIRED(vmsumuhm, vmsumuhs, 19),
10528 GEN_VAFORM_PAIRED(vmsumshm, vmsumshs, 20),
10529 GEN_VAFORM_PAIRED(vsel, vperm, 21),
10530 GEN_VAFORM_PAIRED(vmaddfp, vnmsubfp, 23),
10531
10532 GEN_VXFORM_DUAL(vclzb, vpopcntb, 1, 28, PPC_NONE, PPC2_ALTIVEC_207),
10533 GEN_VXFORM_DUAL(vclzh, vpopcnth, 1, 29, PPC_NONE, PPC2_ALTIVEC_207),
10534 GEN_VXFORM_DUAL(vclzw, vpopcntw, 1, 30, PPC_NONE, PPC2_ALTIVEC_207),
10535 GEN_VXFORM_DUAL(vclzd, vpopcntd, 1, 31, PPC_NONE, PPC2_ALTIVEC_207),
10536
10537 GEN_VXFORM_207(vbpermq, 6, 21),
10538 GEN_VXFORM_207(vgbbd, 6, 20),
10539 GEN_VXFORM_207(vpmsumb, 4, 16),
10540 GEN_VXFORM_207(vpmsumh, 4, 17),
10541 GEN_VXFORM_207(vpmsumw, 4, 18),
10542 GEN_VXFORM_207(vpmsumd, 4, 19),
10543
10544 GEN_VXFORM_207(vsbox, 4, 23),
10545
10546 GEN_VXFORM_DUAL(vcipher, vcipherlast, 4, 20, PPC_NONE, PPC2_ALTIVEC_207),
10547 GEN_VXFORM_DUAL(vncipher, vncipherlast, 4, 21, PPC_NONE, PPC2_ALTIVEC_207),
10548
10549 GEN_VXFORM_207(vshasigmaw, 1, 26),
10550 GEN_VXFORM_207(vshasigmad, 1, 27),
10551
10552 GEN_VXFORM_DUAL(vsldoi, vpermxor, 22, 0xFF, PPC_ALTIVEC, PPC_NONE),
10553
10554 GEN_HANDLER_E(lxsdx, 0x1F, 0x0C, 0x12, 0, PPC_NONE, PPC2_VSX),
10555 GEN_HANDLER_E(lxsiwax, 0x1F, 0x0C, 0x02, 0, PPC_NONE, PPC2_VSX207),
10556 GEN_HANDLER_E(lxsiwzx, 0x1F, 0x0C, 0x00, 0, PPC_NONE, PPC2_VSX207),
10557 GEN_HANDLER_E(lxsspx, 0x1F, 0x0C, 0x10, 0, PPC_NONE, PPC2_VSX207),
10558 GEN_HANDLER_E(lxvd2x, 0x1F, 0x0C, 0x1A, 0, PPC_NONE, PPC2_VSX),
10559 GEN_HANDLER_E(lxvdsx, 0x1F, 0x0C, 0x0A, 0, PPC_NONE, PPC2_VSX),
10560 GEN_HANDLER_E(lxvw4x, 0x1F, 0x0C, 0x18, 0, PPC_NONE, PPC2_VSX),
10561
10562 GEN_HANDLER_E(stxsdx, 0x1F, 0xC, 0x16, 0, PPC_NONE, PPC2_VSX),
10563 GEN_HANDLER_E(stxsiwx, 0x1F, 0xC, 0x04, 0, PPC_NONE, PPC2_VSX207),
10564 GEN_HANDLER_E(stxsspx, 0x1F, 0xC, 0x14, 0, PPC_NONE, PPC2_VSX207),
10565 GEN_HANDLER_E(stxvd2x, 0x1F, 0xC, 0x1E, 0, PPC_NONE, PPC2_VSX),
10566 GEN_HANDLER_E(stxvw4x, 0x1F, 0xC, 0x1C, 0, PPC_NONE, PPC2_VSX),
10567
10568 GEN_HANDLER_E(mfvsrwz, 0x1F, 0x13, 0x03, 0x0000F800, PPC_NONE, PPC2_VSX207),
10569 GEN_HANDLER_E(mtvsrwa, 0x1F, 0x13, 0x06, 0x0000F800, PPC_NONE, PPC2_VSX207),
10570 GEN_HANDLER_E(mtvsrwz, 0x1F, 0x13, 0x07, 0x0000F800, PPC_NONE, PPC2_VSX207),
10571 #if defined(TARGET_PPC64)
10572 GEN_HANDLER_E(mfvsrd, 0x1F, 0x13, 0x01, 0x0000F800, PPC_NONE, PPC2_VSX207),
10573 GEN_HANDLER_E(mtvsrd, 0x1F, 0x13, 0x05, 0x0000F800, PPC_NONE, PPC2_VSX207),
10574 #endif
10575
10576 #undef GEN_XX2FORM
10577 #define GEN_XX2FORM(name, opc2, opc3, fl2)                           \
10578 GEN_HANDLER2_E(name, #name, 0x3C, opc2 | 0, opc3, 0, PPC_NONE, fl2), \
10579 GEN_HANDLER2_E(name, #name, 0x3C, opc2 | 1, opc3, 0, PPC_NONE, fl2)
10580
10581 #undef GEN_XX3FORM
10582 #define GEN_XX3FORM(name, opc2, opc3, fl2)                           \
10583 GEN_HANDLER2_E(name, #name, 0x3C, opc2 | 0, opc3, 0, PPC_NONE, fl2), \
10584 GEN_HANDLER2_E(name, #name, 0x3C, opc2 | 1, opc3, 0, PPC_NONE, fl2), \
10585 GEN_HANDLER2_E(name, #name, 0x3C, opc2 | 2, opc3, 0, PPC_NONE, fl2), \
10586 GEN_HANDLER2_E(name, #name, 0x3C, opc2 | 3, opc3, 0, PPC_NONE, fl2)
10587
10588 #undef GEN_XX3_RC_FORM
10589 #define GEN_XX3_RC_FORM(name, opc2, opc3, fl2)                          \
10590 GEN_HANDLER2_E(name, #name, 0x3C, opc2 | 0x00, opc3 | 0x00, 0, PPC_NONE, fl2), \
10591 GEN_HANDLER2_E(name, #name, 0x3C, opc2 | 0x01, opc3 | 0x00, 0, PPC_NONE, fl2), \
10592 GEN_HANDLER2_E(name, #name, 0x3C, opc2 | 0x02, opc3 | 0x00, 0, PPC_NONE, fl2), \
10593 GEN_HANDLER2_E(name, #name, 0x3C, opc2 | 0x03, opc3 | 0x00, 0, PPC_NONE, fl2), \
10594 GEN_HANDLER2_E(name, #name, 0x3C, opc2 | 0x00, opc3 | 0x10, 0, PPC_NONE, fl2), \
10595 GEN_HANDLER2_E(name, #name, 0x3C, opc2 | 0x01, opc3 | 0x10, 0, PPC_NONE, fl2), \
10596 GEN_HANDLER2_E(name, #name, 0x3C, opc2 | 0x02, opc3 | 0x10, 0, PPC_NONE, fl2), \
10597 GEN_HANDLER2_E(name, #name, 0x3C, opc2 | 0x03, opc3 | 0x10, 0, PPC_NONE, fl2)
10598
10599 #undef GEN_XX3FORM_DM
10600 #define GEN_XX3FORM_DM(name, opc2, opc3) \
10601 GEN_HANDLER2_E(name, #name, 0x3C, opc2|0x00, opc3|0x00, 0, PPC_NONE, PPC2_VSX),\
10602 GEN_HANDLER2_E(name, #name, 0x3C, opc2|0x01, opc3|0x00, 0, PPC_NONE, PPC2_VSX),\
10603 GEN_HANDLER2_E(name, #name, 0x3C, opc2|0x02, opc3|0x00, 0, PPC_NONE, PPC2_VSX),\
10604 GEN_HANDLER2_E(name, #name, 0x3C, opc2|0x03, opc3|0x00, 0, PPC_NONE, PPC2_VSX),\
10605 GEN_HANDLER2_E(name, #name, 0x3C, opc2|0x00, opc3|0x04, 0, PPC_NONE, PPC2_VSX),\
10606 GEN_HANDLER2_E(name, #name, 0x3C, opc2|0x01, opc3|0x04, 0, PPC_NONE, PPC2_VSX),\
10607 GEN_HANDLER2_E(name, #name, 0x3C, opc2|0x02, opc3|0x04, 0, PPC_NONE, PPC2_VSX),\
10608 GEN_HANDLER2_E(name, #name, 0x3C, opc2|0x03, opc3|0x04, 0, PPC_NONE, PPC2_VSX),\
10609 GEN_HANDLER2_E(name, #name, 0x3C, opc2|0x00, opc3|0x08, 0, PPC_NONE, PPC2_VSX),\
10610 GEN_HANDLER2_E(name, #name, 0x3C, opc2|0x01, opc3|0x08, 0, PPC_NONE, PPC2_VSX),\
10611 GEN_HANDLER2_E(name, #name, 0x3C, opc2|0x02, opc3|0x08, 0, PPC_NONE, PPC2_VSX),\
10612 GEN_HANDLER2_E(name, #name, 0x3C, opc2|0x03, opc3|0x08, 0, PPC_NONE, PPC2_VSX),\
10613 GEN_HANDLER2_E(name, #name, 0x3C, opc2|0x00, opc3|0x0C, 0, PPC_NONE, PPC2_VSX),\
10614 GEN_HANDLER2_E(name, #name, 0x3C, opc2|0x01, opc3|0x0C, 0, PPC_NONE, PPC2_VSX),\
10615 GEN_HANDLER2_E(name, #name, 0x3C, opc2|0x02, opc3|0x0C, 0, PPC_NONE, PPC2_VSX),\
10616 GEN_HANDLER2_E(name, #name, 0x3C, opc2|0x03, opc3|0x0C, 0, PPC_NONE, PPC2_VSX)
10617
10618 GEN_XX2FORM(xsabsdp, 0x12, 0x15, PPC2_VSX),
10619 GEN_XX2FORM(xsnabsdp, 0x12, 0x16, PPC2_VSX),
10620 GEN_XX2FORM(xsnegdp, 0x12, 0x17, PPC2_VSX),
10621 GEN_XX3FORM(xscpsgndp, 0x00, 0x16, PPC2_VSX),
10622
10623 GEN_XX2FORM(xvabsdp, 0x12, 0x1D, PPC2_VSX),
10624 GEN_XX2FORM(xvnabsdp, 0x12, 0x1E, PPC2_VSX),
10625 GEN_XX2FORM(xvnegdp, 0x12, 0x1F, PPC2_VSX),
10626 GEN_XX3FORM(xvcpsgndp, 0x00, 0x1E, PPC2_VSX),
10627 GEN_XX2FORM(xvabssp, 0x12, 0x19, PPC2_VSX),
10628 GEN_XX2FORM(xvnabssp, 0x12, 0x1A, PPC2_VSX),
10629 GEN_XX2FORM(xvnegsp, 0x12, 0x1B, PPC2_VSX),
10630 GEN_XX3FORM(xvcpsgnsp, 0x00, 0x1A, PPC2_VSX),
10631
10632 GEN_XX3FORM(xsadddp, 0x00, 0x04, PPC2_VSX),
10633 GEN_XX3FORM(xssubdp, 0x00, 0x05, PPC2_VSX),
10634 GEN_XX3FORM(xsmuldp, 0x00, 0x06, PPC2_VSX),
10635 GEN_XX3FORM(xsdivdp, 0x00, 0x07, PPC2_VSX),
10636 GEN_XX2FORM(xsredp,  0x14, 0x05, PPC2_VSX),
10637 GEN_XX2FORM(xssqrtdp,  0x16, 0x04, PPC2_VSX),
10638 GEN_XX2FORM(xsrsqrtedp,  0x14, 0x04, PPC2_VSX),
10639 GEN_XX3FORM(xstdivdp,  0x14, 0x07, PPC2_VSX),
10640 GEN_XX2FORM(xstsqrtdp,  0x14, 0x06, PPC2_VSX),
10641 GEN_XX3FORM(xsmaddadp, 0x04, 0x04, PPC2_VSX),
10642 GEN_XX3FORM(xsmaddmdp, 0x04, 0x05, PPC2_VSX),
10643 GEN_XX3FORM(xsmsubadp, 0x04, 0x06, PPC2_VSX),
10644 GEN_XX3FORM(xsmsubmdp, 0x04, 0x07, PPC2_VSX),
10645 GEN_XX3FORM(xsnmaddadp, 0x04, 0x14, PPC2_VSX),
10646 GEN_XX3FORM(xsnmaddmdp, 0x04, 0x15, PPC2_VSX),
10647 GEN_XX3FORM(xsnmsubadp, 0x04, 0x16, PPC2_VSX),
10648 GEN_XX3FORM(xsnmsubmdp, 0x04, 0x17, PPC2_VSX),
10649 GEN_XX2FORM(xscmpodp,  0x0C, 0x05, PPC2_VSX),
10650 GEN_XX2FORM(xscmpudp,  0x0C, 0x04, PPC2_VSX),
10651 GEN_XX3FORM(xsmaxdp, 0x00, 0x14, PPC2_VSX),
10652 GEN_XX3FORM(xsmindp, 0x00, 0x15, PPC2_VSX),
10653 GEN_XX2FORM(xscvdpsp, 0x12, 0x10, PPC2_VSX),
10654 GEN_XX2FORM(xscvdpspn, 0x16, 0x10, PPC2_VSX207),
10655 GEN_XX2FORM(xscvspdp, 0x12, 0x14, PPC2_VSX),
10656 GEN_XX2FORM(xscvspdpn, 0x16, 0x14, PPC2_VSX207),
10657 GEN_XX2FORM(xscvdpsxds, 0x10, 0x15, PPC2_VSX),
10658 GEN_XX2FORM(xscvdpsxws, 0x10, 0x05, PPC2_VSX),
10659 GEN_XX2FORM(xscvdpuxds, 0x10, 0x14, PPC2_VSX),
10660 GEN_XX2FORM(xscvdpuxws, 0x10, 0x04, PPC2_VSX),
10661 GEN_XX2FORM(xscvsxddp, 0x10, 0x17, PPC2_VSX),
10662 GEN_XX2FORM(xscvuxddp, 0x10, 0x16, PPC2_VSX),
10663 GEN_XX2FORM(xsrdpi, 0x12, 0x04, PPC2_VSX),
10664 GEN_XX2FORM(xsrdpic, 0x16, 0x06, PPC2_VSX),
10665 GEN_XX2FORM(xsrdpim, 0x12, 0x07, PPC2_VSX),
10666 GEN_XX2FORM(xsrdpip, 0x12, 0x06, PPC2_VSX),
10667 GEN_XX2FORM(xsrdpiz, 0x12, 0x05, PPC2_VSX),
10668
10669 GEN_XX3FORM(xsaddsp, 0x00, 0x00, PPC2_VSX207),
10670 GEN_XX3FORM(xssubsp, 0x00, 0x01, PPC2_VSX207),
10671 GEN_XX3FORM(xsmulsp, 0x00, 0x02, PPC2_VSX207),
10672 GEN_XX3FORM(xsdivsp, 0x00, 0x03, PPC2_VSX207),
10673 GEN_XX2FORM(xsresp,  0x14, 0x01, PPC2_VSX207),
10674 GEN_XX2FORM(xsrsp, 0x12, 0x11, PPC2_VSX207),
10675 GEN_XX2FORM(xssqrtsp,  0x16, 0x00, PPC2_VSX207),
10676 GEN_XX2FORM(xsrsqrtesp,  0x14, 0x00, PPC2_VSX207),
10677 GEN_XX3FORM(xsmaddasp, 0x04, 0x00, PPC2_VSX207),
10678 GEN_XX3FORM(xsmaddmsp, 0x04, 0x01, PPC2_VSX207),
10679 GEN_XX3FORM(xsmsubasp, 0x04, 0x02, PPC2_VSX207),
10680 GEN_XX3FORM(xsmsubmsp, 0x04, 0x03, PPC2_VSX207),
10681 GEN_XX3FORM(xsnmaddasp, 0x04, 0x10, PPC2_VSX207),
10682 GEN_XX3FORM(xsnmaddmsp, 0x04, 0x11, PPC2_VSX207),
10683 GEN_XX3FORM(xsnmsubasp, 0x04, 0x12, PPC2_VSX207),
10684 GEN_XX3FORM(xsnmsubmsp, 0x04, 0x13, PPC2_VSX207),
10685 GEN_XX2FORM(xscvsxdsp, 0x10, 0x13, PPC2_VSX207),
10686 GEN_XX2FORM(xscvuxdsp, 0x10, 0x12, PPC2_VSX207),
10687
10688 GEN_XX3FORM(xvadddp, 0x00, 0x0C, PPC2_VSX),
10689 GEN_XX3FORM(xvsubdp, 0x00, 0x0D, PPC2_VSX),
10690 GEN_XX3FORM(xvmuldp, 0x00, 0x0E, PPC2_VSX),
10691 GEN_XX3FORM(xvdivdp, 0x00, 0x0F, PPC2_VSX),
10692 GEN_XX2FORM(xvredp,  0x14, 0x0D, PPC2_VSX),
10693 GEN_XX2FORM(xvsqrtdp,  0x16, 0x0C, PPC2_VSX),
10694 GEN_XX2FORM(xvrsqrtedp,  0x14, 0x0C, PPC2_VSX),
10695 GEN_XX3FORM(xvtdivdp, 0x14, 0x0F, PPC2_VSX),
10696 GEN_XX2FORM(xvtsqrtdp, 0x14, 0x0E, PPC2_VSX),
10697 GEN_XX3FORM(xvmaddadp, 0x04, 0x0C, PPC2_VSX),
10698 GEN_XX3FORM(xvmaddmdp, 0x04, 0x0D, PPC2_VSX),
10699 GEN_XX3FORM(xvmsubadp, 0x04, 0x0E, PPC2_VSX),
10700 GEN_XX3FORM(xvmsubmdp, 0x04, 0x0F, PPC2_VSX),
10701 GEN_XX3FORM(xvnmaddadp, 0x04, 0x1C, PPC2_VSX),
10702 GEN_XX3FORM(xvnmaddmdp, 0x04, 0x1D, PPC2_VSX),
10703 GEN_XX3FORM(xvnmsubadp, 0x04, 0x1E, PPC2_VSX),
10704 GEN_XX3FORM(xvnmsubmdp, 0x04, 0x1F, PPC2_VSX),
10705 GEN_XX3FORM(xvmaxdp, 0x00, 0x1C, PPC2_VSX),
10706 GEN_XX3FORM(xvmindp, 0x00, 0x1D, PPC2_VSX),
10707 GEN_XX3_RC_FORM(xvcmpeqdp, 0x0C, 0x0C, PPC2_VSX),
10708 GEN_XX3_RC_FORM(xvcmpgtdp, 0x0C, 0x0D, PPC2_VSX),
10709 GEN_XX3_RC_FORM(xvcmpgedp, 0x0C, 0x0E, PPC2_VSX),
10710 GEN_XX2FORM(xvcvdpsp, 0x12, 0x18, PPC2_VSX),
10711 GEN_XX2FORM(xvcvdpsxds, 0x10, 0x1D, PPC2_VSX),
10712 GEN_XX2FORM(xvcvdpsxws, 0x10, 0x0D, PPC2_VSX),
10713 GEN_XX2FORM(xvcvdpuxds, 0x10, 0x1C, PPC2_VSX),
10714 GEN_XX2FORM(xvcvdpuxws, 0x10, 0x0C, PPC2_VSX),
10715 GEN_XX2FORM(xvcvsxddp, 0x10, 0x1F, PPC2_VSX),
10716 GEN_XX2FORM(xvcvuxddp, 0x10, 0x1E, PPC2_VSX),
10717 GEN_XX2FORM(xvcvsxwdp, 0x10, 0x0F, PPC2_VSX),
10718 GEN_XX2FORM(xvcvuxwdp, 0x10, 0x0E, PPC2_VSX),
10719 GEN_XX2FORM(xvrdpi, 0x12, 0x0C, PPC2_VSX),
10720 GEN_XX2FORM(xvrdpic, 0x16, 0x0E, PPC2_VSX),
10721 GEN_XX2FORM(xvrdpim, 0x12, 0x0F, PPC2_VSX),
10722 GEN_XX2FORM(xvrdpip, 0x12, 0x0E, PPC2_VSX),
10723 GEN_XX2FORM(xvrdpiz, 0x12, 0x0D, PPC2_VSX),
10724
10725 GEN_XX3FORM(xvaddsp, 0x00, 0x08, PPC2_VSX),
10726 GEN_XX3FORM(xvsubsp, 0x00, 0x09, PPC2_VSX),
10727 GEN_XX3FORM(xvmulsp, 0x00, 0x0A, PPC2_VSX),
10728 GEN_XX3FORM(xvdivsp, 0x00, 0x0B, PPC2_VSX),
10729 GEN_XX2FORM(xvresp, 0x14, 0x09, PPC2_VSX),
10730 GEN_XX2FORM(xvsqrtsp, 0x16, 0x08, PPC2_VSX),
10731 GEN_XX2FORM(xvrsqrtesp, 0x14, 0x08, PPC2_VSX),
10732 GEN_XX3FORM(xvtdivsp, 0x14, 0x0B, PPC2_VSX),
10733 GEN_XX2FORM(xvtsqrtsp, 0x14, 0x0A, PPC2_VSX),
10734 GEN_XX3FORM(xvmaddasp, 0x04, 0x08, PPC2_VSX),
10735 GEN_XX3FORM(xvmaddmsp, 0x04, 0x09, PPC2_VSX),
10736 GEN_XX3FORM(xvmsubasp, 0x04, 0x0A, PPC2_VSX),
10737 GEN_XX3FORM(xvmsubmsp, 0x04, 0x0B, PPC2_VSX),
10738 GEN_XX3FORM(xvnmaddasp, 0x04, 0x18, PPC2_VSX),
10739 GEN_XX3FORM(xvnmaddmsp, 0x04, 0x19, PPC2_VSX),
10740 GEN_XX3FORM(xvnmsubasp, 0x04, 0x1A, PPC2_VSX),
10741 GEN_XX3FORM(xvnmsubmsp, 0x04, 0x1B, PPC2_VSX),
10742 GEN_XX3FORM(xvmaxsp, 0x00, 0x18, PPC2_VSX),
10743 GEN_XX3FORM(xvminsp, 0x00, 0x19, PPC2_VSX),
10744 GEN_XX3_RC_FORM(xvcmpeqsp, 0x0C, 0x08, PPC2_VSX),
10745 GEN_XX3_RC_FORM(xvcmpgtsp, 0x0C, 0x09, PPC2_VSX),
10746 GEN_XX3_RC_FORM(xvcmpgesp, 0x0C, 0x0A, PPC2_VSX),
10747 GEN_XX2FORM(xvcvspdp, 0x12, 0x1C, PPC2_VSX),
10748 GEN_XX2FORM(xvcvspsxds, 0x10, 0x19, PPC2_VSX),
10749 GEN_XX2FORM(xvcvspsxws, 0x10, 0x09, PPC2_VSX),
10750 GEN_XX2FORM(xvcvspuxds, 0x10, 0x18, PPC2_VSX),
10751 GEN_XX2FORM(xvcvspuxws, 0x10, 0x08, PPC2_VSX),
10752 GEN_XX2FORM(xvcvsxdsp, 0x10, 0x1B, PPC2_VSX),
10753 GEN_XX2FORM(xvcvuxdsp, 0x10, 0x1A, PPC2_VSX),
10754 GEN_XX2FORM(xvcvsxwsp, 0x10, 0x0B, PPC2_VSX),
10755 GEN_XX2FORM(xvcvuxwsp, 0x10, 0x0A, PPC2_VSX),
10756 GEN_XX2FORM(xvrspi, 0x12, 0x08, PPC2_VSX),
10757 GEN_XX2FORM(xvrspic, 0x16, 0x0A, PPC2_VSX),
10758 GEN_XX2FORM(xvrspim, 0x12, 0x0B, PPC2_VSX),
10759 GEN_XX2FORM(xvrspip, 0x12, 0x0A, PPC2_VSX),
10760 GEN_XX2FORM(xvrspiz, 0x12, 0x09, PPC2_VSX),
10761
10762 #undef VSX_LOGICAL
10763 #define VSX_LOGICAL(name, opc2, opc3, fl2) \
10764 GEN_XX3FORM(name, opc2, opc3, fl2)
10765
10766 VSX_LOGICAL(xxland, 0x8, 0x10, PPC2_VSX),
10767 VSX_LOGICAL(xxlandc, 0x8, 0x11, PPC2_VSX),
10768 VSX_LOGICAL(xxlor, 0x8, 0x12, PPC2_VSX),
10769 VSX_LOGICAL(xxlxor, 0x8, 0x13, PPC2_VSX),
10770 VSX_LOGICAL(xxlnor, 0x8, 0x14, PPC2_VSX),
10771 VSX_LOGICAL(xxleqv, 0x8, 0x17, PPC2_VSX207),
10772 VSX_LOGICAL(xxlnand, 0x8, 0x16, PPC2_VSX207),
10773 VSX_LOGICAL(xxlorc, 0x8, 0x15, PPC2_VSX207),
10774 GEN_XX3FORM(xxmrghw, 0x08, 0x02, PPC2_VSX),
10775 GEN_XX3FORM(xxmrglw, 0x08, 0x06, PPC2_VSX),
10776 GEN_XX2FORM(xxspltw, 0x08, 0x0A, PPC2_VSX),
10777 GEN_XX3FORM_DM(xxsldwi, 0x08, 0x00),
10778
10779 #define GEN_XXSEL_ROW(opc3) \
10780 GEN_HANDLER2_E(xxsel, "xxsel", 0x3C, 0x18, opc3, 0, PPC_NONE, PPC2_VSX), \
10781 GEN_HANDLER2_E(xxsel, "xxsel", 0x3C, 0x19, opc3, 0, PPC_NONE, PPC2_VSX), \
10782 GEN_HANDLER2_E(xxsel, "xxsel", 0x3C, 0x1A, opc3, 0, PPC_NONE, PPC2_VSX), \
10783 GEN_HANDLER2_E(xxsel, "xxsel", 0x3C, 0x1B, opc3, 0, PPC_NONE, PPC2_VSX), \
10784 GEN_HANDLER2_E(xxsel, "xxsel", 0x3C, 0x1C, opc3, 0, PPC_NONE, PPC2_VSX), \
10785 GEN_HANDLER2_E(xxsel, "xxsel", 0x3C, 0x1D, opc3, 0, PPC_NONE, PPC2_VSX), \
10786 GEN_HANDLER2_E(xxsel, "xxsel", 0x3C, 0x1E, opc3, 0, PPC_NONE, PPC2_VSX), \
10787 GEN_HANDLER2_E(xxsel, "xxsel", 0x3C, 0x1F, opc3, 0, PPC_NONE, PPC2_VSX), \
10788
10789 GEN_XXSEL_ROW(0x00)
10790 GEN_XXSEL_ROW(0x01)
10791 GEN_XXSEL_ROW(0x02)
10792 GEN_XXSEL_ROW(0x03)
10793 GEN_XXSEL_ROW(0x04)
10794 GEN_XXSEL_ROW(0x05)
10795 GEN_XXSEL_ROW(0x06)
10796 GEN_XXSEL_ROW(0x07)
10797 GEN_XXSEL_ROW(0x08)
10798 GEN_XXSEL_ROW(0x09)
10799 GEN_XXSEL_ROW(0x0A)
10800 GEN_XXSEL_ROW(0x0B)
10801 GEN_XXSEL_ROW(0x0C)
10802 GEN_XXSEL_ROW(0x0D)
10803 GEN_XXSEL_ROW(0x0E)
10804 GEN_XXSEL_ROW(0x0F)
10805 GEN_XXSEL_ROW(0x10)
10806 GEN_XXSEL_ROW(0x11)
10807 GEN_XXSEL_ROW(0x12)
10808 GEN_XXSEL_ROW(0x13)
10809 GEN_XXSEL_ROW(0x14)
10810 GEN_XXSEL_ROW(0x15)
10811 GEN_XXSEL_ROW(0x16)
10812 GEN_XXSEL_ROW(0x17)
10813 GEN_XXSEL_ROW(0x18)
10814 GEN_XXSEL_ROW(0x19)
10815 GEN_XXSEL_ROW(0x1A)
10816 GEN_XXSEL_ROW(0x1B)
10817 GEN_XXSEL_ROW(0x1C)
10818 GEN_XXSEL_ROW(0x1D)
10819 GEN_XXSEL_ROW(0x1E)
10820 GEN_XXSEL_ROW(0x1F)
10821
10822 GEN_XX3FORM_DM(xxpermdi, 0x08, 0x01),
10823
10824 #undef GEN_DFP_T_A_B_Rc
10825 #undef GEN_DFP_BF_A_B
10826 #undef GEN_DFP_BF_A_DCM
10827 #undef GEN_DFP_T_B_U32_U32_Rc
10828 #undef GEN_DFP_T_A_B_I32_Rc
10829 #undef GEN_DFP_T_B_Rc
10830 #undef GEN_DFP_T_FPR_I32_Rc
10831
10832 #define _GEN_DFP_LONG(name, op1, op2, mask) \
10833 GEN_HANDLER_E(name, 0x3B, op1, op2, mask, PPC_NONE, PPC2_DFP)
10834
10835 #define _GEN_DFP_LONGx2(name, op1, op2, mask) \
10836 GEN_HANDLER_E(name, 0x3B, op1, 0x00 | op2, mask, PPC_NONE, PPC2_DFP), \
10837 GEN_HANDLER_E(name, 0x3B, op1, 0x10 | op2, mask, PPC_NONE, PPC2_DFP)
10838
10839 #define _GEN_DFP_LONGx4(name, op1, op2, mask) \
10840 GEN_HANDLER_E(name, 0x3B, op1, 0x00 | op2, mask, PPC_NONE, PPC2_DFP), \
10841 GEN_HANDLER_E(name, 0x3B, op1, 0x08 | op2, mask, PPC_NONE, PPC2_DFP), \
10842 GEN_HANDLER_E(name, 0x3B, op1, 0x10 | op2, mask, PPC_NONE, PPC2_DFP), \
10843 GEN_HANDLER_E(name, 0x3B, op1, 0x18 | op2, mask, PPC_NONE, PPC2_DFP)
10844
10845 #define _GEN_DFP_QUAD(name, op1, op2, mask) \
10846 GEN_HANDLER_E(name, 0x3F, op1, op2, mask, PPC_NONE, PPC2_DFP)
10847
10848 #define _GEN_DFP_QUADx2(name, op1, op2, mask) \
10849 GEN_HANDLER_E(name, 0x3F, op1, 0x00 | op2, mask, PPC_NONE, PPC2_DFP), \
10850 GEN_HANDLER_E(name, 0x3F, op1, 0x10 | op2, mask, PPC_NONE, PPC2_DFP)
10851
10852 #define _GEN_DFP_QUADx4(name, op1, op2, mask)                         \
10853 GEN_HANDLER_E(name, 0x3F, op1, 0x00 | op2, mask, PPC_NONE, PPC2_DFP), \
10854 GEN_HANDLER_E(name, 0x3F, op1, 0x08 | op2, mask, PPC_NONE, PPC2_DFP), \
10855 GEN_HANDLER_E(name, 0x3F, op1, 0x10 | op2, mask, PPC_NONE, PPC2_DFP), \
10856 GEN_HANDLER_E(name, 0x3F, op1, 0x18 | op2, mask, PPC_NONE, PPC2_DFP)
10857
10858 #define GEN_DFP_T_A_B_Rc(name, op1, op2) \
10859 _GEN_DFP_LONG(name, op1, op2, 0x00000000)
10860
10861 #define GEN_DFP_Tp_Ap_Bp_Rc(name, op1, op2) \
10862 _GEN_DFP_QUAD(name, op1, op2, 0x00210800)
10863
10864 #define GEN_DFP_Tp_A_Bp_Rc(name, op1, op2) \
10865 _GEN_DFP_QUAD(name, op1, op2, 0x00200800)
10866
10867 #define GEN_DFP_T_B_Rc(name, op1, op2) \
10868 _GEN_DFP_LONG(name, op1, op2, 0x001F0000)
10869
10870 #define GEN_DFP_Tp_Bp_Rc(name, op1, op2) \
10871 _GEN_DFP_QUAD(name, op1, op2, 0x003F0800)
10872
10873 #define GEN_DFP_Tp_B_Rc(name, op1, op2) \
10874 _GEN_DFP_QUAD(name, op1, op2, 0x003F0000)
10875
10876 #define GEN_DFP_T_Bp_Rc(name, op1, op2) \
10877 _GEN_DFP_QUAD(name, op1, op2, 0x001F0800)
10878
10879 #define GEN_DFP_BF_A_B(name, op1, op2) \
10880 _GEN_DFP_LONG(name, op1, op2, 0x00000001)
10881
10882 #define GEN_DFP_BF_Ap_Bp(name, op1, op2) \
10883 _GEN_DFP_QUAD(name, op1, op2, 0x00610801)
10884
10885 #define GEN_DFP_BF_A_Bp(name, op1, op2) \
10886 _GEN_DFP_QUAD(name, op1, op2, 0x00600801)
10887
10888 #define GEN_DFP_BF_A_DCM(name, op1, op2) \
10889 _GEN_DFP_LONGx2(name, op1, op2, 0x00600001)
10890
10891 #define GEN_DFP_BF_Ap_DCM(name, op1, op2) \
10892 _GEN_DFP_QUADx2(name, op1, op2, 0x00610001)
10893
10894 #define GEN_DFP_T_A_B_RMC_Rc(name, op1, op2) \
10895 _GEN_DFP_LONGx4(name, op1, op2, 0x00000000)
10896
10897 #define GEN_DFP_Tp_Ap_Bp_RMC_Rc(name, op1, op2) \
10898 _GEN_DFP_QUADx4(name, op1, op2, 0x02010800)
10899
10900 #define GEN_DFP_Tp_A_Bp_RMC_Rc(name, op1, op2) \
10901 _GEN_DFP_QUADx4(name, op1, op2, 0x02000800)
10902
10903 #define GEN_DFP_TE_T_B_RMC_Rc(name, op1, op2) \
10904 _GEN_DFP_LONGx4(name, op1, op2, 0x00000000)
10905
10906 #define GEN_DFP_TE_Tp_Bp_RMC_Rc(name, op1, op2) \
10907 _GEN_DFP_QUADx4(name, op1, op2, 0x00200800)
10908
10909 #define GEN_DFP_R_T_B_RMC_Rc(name, op1, op2) \
10910 _GEN_DFP_LONGx4(name, op1, op2, 0x001E0000)
10911
10912 #define GEN_DFP_R_Tp_Bp_RMC_Rc(name, op1, op2) \
10913 _GEN_DFP_QUADx4(name, op1, op2, 0x003E0800)
10914
10915 #define GEN_DFP_SP_T_B_Rc(name, op1, op2) \
10916 _GEN_DFP_LONG(name, op1, op2, 0x00070000)
10917
10918 #define GEN_DFP_SP_Tp_Bp_Rc(name, op1, op2) \
10919 _GEN_DFP_QUAD(name, op1, op2, 0x00270800)
10920
10921 #define GEN_DFP_S_T_B_Rc(name, op1, op2) \
10922 _GEN_DFP_LONG(name, op1, op2, 0x000F0000)
10923
10924 #define GEN_DFP_S_Tp_Bp_Rc(name, op1, op2) \
10925 _GEN_DFP_QUAD(name, op1, op2, 0x002F0800)
10926
10927 #define GEN_DFP_T_A_SH_Rc(name, op1, op2) \
10928 _GEN_DFP_LONGx2(name, op1, op2, 0x00000000)
10929
10930 #define GEN_DFP_Tp_Ap_SH_Rc(name, op1, op2) \
10931 _GEN_DFP_QUADx2(name, op1, op2, 0x00210000)
10932
10933 GEN_DFP_T_A_B_Rc(dadd, 0x02, 0x00),
10934 GEN_DFP_Tp_Ap_Bp_Rc(daddq, 0x02, 0x00),
10935 GEN_DFP_T_A_B_Rc(dsub, 0x02, 0x10),
10936 GEN_DFP_Tp_Ap_Bp_Rc(dsubq, 0x02, 0x10),
10937 GEN_DFP_T_A_B_Rc(dmul, 0x02, 0x01),
10938 GEN_DFP_Tp_Ap_Bp_Rc(dmulq, 0x02, 0x01),
10939 GEN_DFP_T_A_B_Rc(ddiv, 0x02, 0x11),
10940 GEN_DFP_Tp_Ap_Bp_Rc(ddivq, 0x02, 0x11),
10941 GEN_DFP_BF_A_B(dcmpu, 0x02, 0x14),
10942 GEN_DFP_BF_Ap_Bp(dcmpuq, 0x02, 0x14),
10943 GEN_DFP_BF_A_B(dcmpo, 0x02, 0x04),
10944 GEN_DFP_BF_Ap_Bp(dcmpoq, 0x02, 0x04),
10945 GEN_DFP_BF_A_DCM(dtstdc, 0x02, 0x06),
10946 GEN_DFP_BF_Ap_DCM(dtstdcq, 0x02, 0x06),
10947 GEN_DFP_BF_A_DCM(dtstdg, 0x02, 0x07),
10948 GEN_DFP_BF_Ap_DCM(dtstdgq, 0x02, 0x07),
10949 GEN_DFP_BF_A_B(dtstex, 0x02, 0x05),
10950 GEN_DFP_BF_Ap_Bp(dtstexq, 0x02, 0x05),
10951 GEN_DFP_BF_A_B(dtstsf, 0x02, 0x15),
10952 GEN_DFP_BF_A_Bp(dtstsfq, 0x02, 0x15),
10953 GEN_DFP_TE_T_B_RMC_Rc(dquai, 0x03, 0x02),
10954 GEN_DFP_TE_Tp_Bp_RMC_Rc(dquaiq, 0x03, 0x02),
10955 GEN_DFP_T_A_B_RMC_Rc(dqua, 0x03, 0x00),
10956 GEN_DFP_Tp_Ap_Bp_RMC_Rc(dquaq, 0x03, 0x00),
10957 GEN_DFP_T_A_B_RMC_Rc(drrnd, 0x03, 0x01),
10958 GEN_DFP_Tp_A_Bp_RMC_Rc(drrndq, 0x03, 0x01),
10959 GEN_DFP_R_T_B_RMC_Rc(drintx, 0x03, 0x03),
10960 GEN_DFP_R_Tp_Bp_RMC_Rc(drintxq, 0x03, 0x03),
10961 GEN_DFP_R_T_B_RMC_Rc(drintn, 0x03, 0x07),
10962 GEN_DFP_R_Tp_Bp_RMC_Rc(drintnq, 0x03, 0x07),
10963 GEN_DFP_T_B_Rc(dctdp, 0x02, 0x08),
10964 GEN_DFP_Tp_B_Rc(dctqpq, 0x02, 0x08),
10965 GEN_DFP_T_B_Rc(drsp, 0x02, 0x18),
10966 GEN_DFP_Tp_Bp_Rc(drdpq, 0x02, 0x18),
10967 GEN_DFP_T_B_Rc(dcffix, 0x02, 0x19),
10968 GEN_DFP_Tp_B_Rc(dcffixq, 0x02, 0x19),
10969 GEN_DFP_T_B_Rc(dctfix, 0x02, 0x09),
10970 GEN_DFP_T_Bp_Rc(dctfixq, 0x02, 0x09),
10971 GEN_DFP_SP_T_B_Rc(ddedpd, 0x02, 0x0a),
10972 GEN_DFP_SP_Tp_Bp_Rc(ddedpdq, 0x02, 0x0a),
10973 GEN_DFP_S_T_B_Rc(denbcd, 0x02, 0x1a),
10974 GEN_DFP_S_Tp_Bp_Rc(denbcdq, 0x02, 0x1a),
10975 GEN_DFP_T_B_Rc(dxex, 0x02, 0x0b),
10976 GEN_DFP_T_Bp_Rc(dxexq, 0x02, 0x0b),
10977 GEN_DFP_T_A_B_Rc(diex, 0x02, 0x1b),
10978 GEN_DFP_Tp_A_Bp_Rc(diexq, 0x02, 0x1b),
10979 GEN_DFP_T_A_SH_Rc(dscli, 0x02, 0x02),
10980 GEN_DFP_Tp_Ap_SH_Rc(dscliq, 0x02, 0x02),
10981 GEN_DFP_T_A_SH_Rc(dscri, 0x02, 0x03),
10982 GEN_DFP_Tp_Ap_SH_Rc(dscriq, 0x02, 0x03),
10983
10984 #undef GEN_SPE
10985 #define GEN_SPE(name0, name1, opc2, opc3, inval0, inval1, type) \
10986     GEN_OPCODE_DUAL(name0##_##name1, 0x04, opc2, opc3, inval0, inval1, type, PPC_NONE)
10987 GEN_SPE(evaddw,      speundef,    0x00, 0x08, 0x00000000, 0xFFFFFFFF, PPC_SPE),
10988 GEN_SPE(evaddiw,     speundef,    0x01, 0x08, 0x00000000, 0xFFFFFFFF, PPC_SPE),
10989 GEN_SPE(evsubfw,     speundef,    0x02, 0x08, 0x00000000, 0xFFFFFFFF, PPC_SPE),
10990 GEN_SPE(evsubifw,    speundef,    0x03, 0x08, 0x00000000, 0xFFFFFFFF, PPC_SPE),
10991 GEN_SPE(evabs,       evneg,       0x04, 0x08, 0x0000F800, 0x0000F800, PPC_SPE),
10992 GEN_SPE(evextsb,     evextsh,     0x05, 0x08, 0x0000F800, 0x0000F800, PPC_SPE),
10993 GEN_SPE(evrndw,      evcntlzw,    0x06, 0x08, 0x0000F800, 0x0000F800, PPC_SPE),
10994 GEN_SPE(evcntlsw,    brinc,       0x07, 0x08, 0x0000F800, 0x00000000, PPC_SPE),
10995 GEN_SPE(evmra,       speundef,    0x02, 0x13, 0x0000F800, 0xFFFFFFFF, PPC_SPE),
10996 GEN_SPE(speundef,    evand,       0x08, 0x08, 0xFFFFFFFF, 0x00000000, PPC_SPE),
10997 GEN_SPE(evandc,      speundef,    0x09, 0x08, 0x00000000, 0xFFFFFFFF, PPC_SPE),
10998 GEN_SPE(evxor,       evor,        0x0B, 0x08, 0x00000000, 0x00000000, PPC_SPE),
10999 GEN_SPE(evnor,       eveqv,       0x0C, 0x08, 0x00000000, 0x00000000, PPC_SPE),
11000 GEN_SPE(evmwumi,     evmwsmi,     0x0C, 0x11, 0x00000000, 0x00000000, PPC_SPE),
11001 GEN_SPE(evmwumia,    evmwsmia,    0x1C, 0x11, 0x00000000, 0x00000000, PPC_SPE),
11002 GEN_SPE(evmwumiaa,   evmwsmiaa,   0x0C, 0x15, 0x00000000, 0x00000000, PPC_SPE),
11003 GEN_SPE(speundef,    evorc,       0x0D, 0x08, 0xFFFFFFFF, 0x00000000, PPC_SPE),
11004 GEN_SPE(evnand,      speundef,    0x0F, 0x08, 0x00000000, 0xFFFFFFFF, PPC_SPE),
11005 GEN_SPE(evsrwu,      evsrws,      0x10, 0x08, 0x00000000, 0x00000000, PPC_SPE),
11006 GEN_SPE(evsrwiu,     evsrwis,     0x11, 0x08, 0x00000000, 0x00000000, PPC_SPE),
11007 GEN_SPE(evslw,       speundef,    0x12, 0x08, 0x00000000, 0xFFFFFFFF, PPC_SPE),
11008 GEN_SPE(evslwi,      speundef,    0x13, 0x08, 0x00000000, 0xFFFFFFFF, PPC_SPE),
11009 GEN_SPE(evrlw,       evsplati,    0x14, 0x08, 0x00000000, 0x0000F800, PPC_SPE),
11010 GEN_SPE(evrlwi,      evsplatfi,   0x15, 0x08, 0x00000000, 0x0000F800, PPC_SPE),
11011 GEN_SPE(evmergehi,   evmergelo,   0x16, 0x08, 0x00000000, 0x00000000, PPC_SPE),
11012 GEN_SPE(evmergehilo, evmergelohi, 0x17, 0x08, 0x00000000, 0x00000000, PPC_SPE),
11013 GEN_SPE(evcmpgtu,    evcmpgts,    0x18, 0x08, 0x00600000, 0x00600000, PPC_SPE),
11014 GEN_SPE(evcmpltu,    evcmplts,    0x19, 0x08, 0x00600000, 0x00600000, PPC_SPE),
11015 GEN_SPE(evcmpeq,     speundef,    0x1A, 0x08, 0x00600000, 0xFFFFFFFF, PPC_SPE),
11016
11017 GEN_SPE(evfsadd,     evfssub,     0x00, 0x0A, 0x00000000, 0x00000000, PPC_SPE_SINGLE),
11018 GEN_SPE(evfsabs,     evfsnabs,    0x02, 0x0A, 0x0000F800, 0x0000F800, PPC_SPE_SINGLE),
11019 GEN_SPE(evfsneg,     speundef,    0x03, 0x0A, 0x0000F800, 0xFFFFFFFF, PPC_SPE_SINGLE),
11020 GEN_SPE(evfsmul,     evfsdiv,     0x04, 0x0A, 0x00000000, 0x00000000, PPC_SPE_SINGLE),
11021 GEN_SPE(evfscmpgt,   evfscmplt,   0x06, 0x0A, 0x00600000, 0x00600000, PPC_SPE_SINGLE),
11022 GEN_SPE(evfscmpeq,   speundef,    0x07, 0x0A, 0x00600000, 0xFFFFFFFF, PPC_SPE_SINGLE),
11023 GEN_SPE(evfscfui,    evfscfsi,    0x08, 0x0A, 0x00180000, 0x00180000, PPC_SPE_SINGLE),
11024 GEN_SPE(evfscfuf,    evfscfsf,    0x09, 0x0A, 0x00180000, 0x00180000, PPC_SPE_SINGLE),
11025 GEN_SPE(evfsctui,    evfsctsi,    0x0A, 0x0A, 0x00180000, 0x00180000, PPC_SPE_SINGLE),
11026 GEN_SPE(evfsctuf,    evfsctsf,    0x0B, 0x0A, 0x00180000, 0x00180000, PPC_SPE_SINGLE),
11027 GEN_SPE(evfsctuiz,   speundef,    0x0C, 0x0A, 0x00180000, 0xFFFFFFFF, PPC_SPE_SINGLE),
11028 GEN_SPE(evfsctsiz,   speundef,    0x0D, 0x0A, 0x00180000, 0xFFFFFFFF, PPC_SPE_SINGLE),
11029 GEN_SPE(evfststgt,   evfststlt,   0x0E, 0x0A, 0x00600000, 0x00600000, PPC_SPE_SINGLE),
11030 GEN_SPE(evfststeq,   speundef,    0x0F, 0x0A, 0x00600000, 0xFFFFFFFF, PPC_SPE_SINGLE),
11031
11032 GEN_SPE(efsadd,      efssub,      0x00, 0x0B, 0x00000000, 0x00000000, PPC_SPE_SINGLE),
11033 GEN_SPE(efsabs,      efsnabs,     0x02, 0x0B, 0x0000F800, 0x0000F800, PPC_SPE_SINGLE),
11034 GEN_SPE(efsneg,      speundef,    0x03, 0x0B, 0x0000F800, 0xFFFFFFFF, PPC_SPE_SINGLE),
11035 GEN_SPE(efsmul,      efsdiv,      0x04, 0x0B, 0x00000000, 0x00000000, PPC_SPE_SINGLE),
11036 GEN_SPE(efscmpgt,    efscmplt,    0x06, 0x0B, 0x00600000, 0x00600000, PPC_SPE_SINGLE),
11037 GEN_SPE(efscmpeq,    efscfd,      0x07, 0x0B, 0x00600000, 0x00180000, PPC_SPE_SINGLE),
11038 GEN_SPE(efscfui,     efscfsi,     0x08, 0x0B, 0x00180000, 0x00180000, PPC_SPE_SINGLE),
11039 GEN_SPE(efscfuf,     efscfsf,     0x09, 0x0B, 0x00180000, 0x00180000, PPC_SPE_SINGLE),
11040 GEN_SPE(efsctui,     efsctsi,     0x0A, 0x0B, 0x00180000, 0x00180000, PPC_SPE_SINGLE),
11041 GEN_SPE(efsctuf,     efsctsf,     0x0B, 0x0B, 0x00180000, 0x00180000, PPC_SPE_SINGLE),
11042 GEN_SPE(efsctuiz,    speundef,    0x0C, 0x0B, 0x00180000, 0xFFFFFFFF, PPC_SPE_SINGLE),
11043 GEN_SPE(efsctsiz,    speundef,    0x0D, 0x0B, 0x00180000, 0xFFFFFFFF, PPC_SPE_SINGLE),
11044 GEN_SPE(efststgt,    efststlt,    0x0E, 0x0B, 0x00600000, 0x00600000, PPC_SPE_SINGLE),
11045 GEN_SPE(efststeq,    speundef,    0x0F, 0x0B, 0x00600000, 0xFFFFFFFF, PPC_SPE_SINGLE),
11046
11047 GEN_SPE(efdadd,      efdsub,      0x10, 0x0B, 0x00000000, 0x00000000, PPC_SPE_DOUBLE),
11048 GEN_SPE(efdcfuid,    efdcfsid,    0x11, 0x0B, 0x00180000, 0x00180000, PPC_SPE_DOUBLE),
11049 GEN_SPE(efdabs,      efdnabs,     0x12, 0x0B, 0x0000F800, 0x0000F800, PPC_SPE_DOUBLE),
11050 GEN_SPE(efdneg,      speundef,    0x13, 0x0B, 0x0000F800, 0xFFFFFFFF, PPC_SPE_DOUBLE),
11051 GEN_SPE(efdmul,      efddiv,      0x14, 0x0B, 0x00000000, 0x00000000, PPC_SPE_DOUBLE),
11052 GEN_SPE(efdctuidz,   efdctsidz,   0x15, 0x0B, 0x00180000, 0x00180000, PPC_SPE_DOUBLE),
11053 GEN_SPE(efdcmpgt,    efdcmplt,    0x16, 0x0B, 0x00600000, 0x00600000, PPC_SPE_DOUBLE),
11054 GEN_SPE(efdcmpeq,    efdcfs,      0x17, 0x0B, 0x00600000, 0x00180000, PPC_SPE_DOUBLE),
11055 GEN_SPE(efdcfui,     efdcfsi,     0x18, 0x0B, 0x00180000, 0x00180000, PPC_SPE_DOUBLE),
11056 GEN_SPE(efdcfuf,     efdcfsf,     0x19, 0x0B, 0x00180000, 0x00180000, PPC_SPE_DOUBLE),
11057 GEN_SPE(efdctui,     efdctsi,     0x1A, 0x0B, 0x00180000, 0x00180000, PPC_SPE_DOUBLE),
11058 GEN_SPE(efdctuf,     efdctsf,     0x1B, 0x0B, 0x00180000, 0x00180000, PPC_SPE_DOUBLE),
11059 GEN_SPE(efdctuiz,    speundef,    0x1C, 0x0B, 0x00180000, 0xFFFFFFFF, PPC_SPE_DOUBLE),
11060 GEN_SPE(efdctsiz,    speundef,    0x1D, 0x0B, 0x00180000, 0xFFFFFFFF, PPC_SPE_DOUBLE),
11061 GEN_SPE(efdtstgt,    efdtstlt,    0x1E, 0x0B, 0x00600000, 0x00600000, PPC_SPE_DOUBLE),
11062 GEN_SPE(efdtsteq,    speundef,    0x1F, 0x0B, 0x00600000, 0xFFFFFFFF, PPC_SPE_DOUBLE),
11063
11064 #undef GEN_SPEOP_LDST
11065 #define GEN_SPEOP_LDST(name, opc2, sh)                                        \
11066 GEN_HANDLER(name, 0x04, opc2, 0x0C, 0x00000000, PPC_SPE)
11067 GEN_SPEOP_LDST(evldd, 0x00, 3),
11068 GEN_SPEOP_LDST(evldw, 0x01, 3),
11069 GEN_SPEOP_LDST(evldh, 0x02, 3),
11070 GEN_SPEOP_LDST(evlhhesplat, 0x04, 1),
11071 GEN_SPEOP_LDST(evlhhousplat, 0x06, 1),
11072 GEN_SPEOP_LDST(evlhhossplat, 0x07, 1),
11073 GEN_SPEOP_LDST(evlwhe, 0x08, 2),
11074 GEN_SPEOP_LDST(evlwhou, 0x0A, 2),
11075 GEN_SPEOP_LDST(evlwhos, 0x0B, 2),
11076 GEN_SPEOP_LDST(evlwwsplat, 0x0C, 2),
11077 GEN_SPEOP_LDST(evlwhsplat, 0x0E, 2),
11078
11079 GEN_SPEOP_LDST(evstdd, 0x10, 3),
11080 GEN_SPEOP_LDST(evstdw, 0x11, 3),
11081 GEN_SPEOP_LDST(evstdh, 0x12, 3),
11082 GEN_SPEOP_LDST(evstwhe, 0x18, 2),
11083 GEN_SPEOP_LDST(evstwho, 0x1A, 2),
11084 GEN_SPEOP_LDST(evstwwe, 0x1C, 2),
11085 GEN_SPEOP_LDST(evstwwo, 0x1E, 2),
11086 };
11087
11088 #include "helper_regs.h"
11089 #include "translate_init.c"
11090
11091 /*****************************************************************************/
11092 /* Misc PowerPC helpers */
11093 void ppc_cpu_dump_state(CPUState *cs, FILE *f, fprintf_function cpu_fprintf,
11094                         int flags)
11095 {
11096 #define RGPL  4
11097 #define RFPL  4
11098
11099     PowerPCCPU *cpu = POWERPC_CPU(cs);
11100     CPUPPCState *env = &cpu->env;
11101     int i;
11102
11103     cpu_fprintf(f, "NIP " TARGET_FMT_lx "   LR " TARGET_FMT_lx " CTR "
11104                 TARGET_FMT_lx " XER " TARGET_FMT_lx "\n",
11105                 env->nip, env->lr, env->ctr, cpu_read_xer(env));
11106     cpu_fprintf(f, "MSR " TARGET_FMT_lx " HID0 " TARGET_FMT_lx "  HF "
11107                 TARGET_FMT_lx " idx %d\n", env->msr, env->spr[SPR_HID0],
11108                 env->hflags, env->mmu_idx);
11109 #if !defined(NO_TIMER_DUMP)
11110     cpu_fprintf(f, "TB %08" PRIu32 " %08" PRIu64
11111 #if !defined(CONFIG_USER_ONLY)
11112                 " DECR %08" PRIu32
11113 #endif
11114                 "\n",
11115                 cpu_ppc_load_tbu(env), cpu_ppc_load_tbl(env)
11116 #if !defined(CONFIG_USER_ONLY)
11117                 , cpu_ppc_load_decr(env)
11118 #endif
11119                 );
11120 #endif
11121     for (i = 0; i < 32; i++) {
11122         if ((i & (RGPL - 1)) == 0)
11123             cpu_fprintf(f, "GPR%02d", i);
11124         cpu_fprintf(f, " %016" PRIx64, ppc_dump_gpr(env, i));
11125         if ((i & (RGPL - 1)) == (RGPL - 1))
11126             cpu_fprintf(f, "\n");
11127     }
11128     cpu_fprintf(f, "CR ");
11129     for (i = 0; i < 8; i++)
11130         cpu_fprintf(f, "%01x", env->crf[i]);
11131     cpu_fprintf(f, "  [");
11132     for (i = 0; i < 8; i++) {
11133         char a = '-';
11134         if (env->crf[i] & 0x08)
11135             a = 'L';
11136         else if (env->crf[i] & 0x04)
11137             a = 'G';
11138         else if (env->crf[i] & 0x02)
11139             a = 'E';
11140         cpu_fprintf(f, " %c%c", a, env->crf[i] & 0x01 ? 'O' : ' ');
11141     }
11142     cpu_fprintf(f, " ]             RES " TARGET_FMT_lx "\n",
11143                 env->reserve_addr);
11144     for (i = 0; i < 32; i++) {
11145         if ((i & (RFPL - 1)) == 0)
11146             cpu_fprintf(f, "FPR%02d", i);
11147         cpu_fprintf(f, " %016" PRIx64, *((uint64_t *)&env->fpr[i]));
11148         if ((i & (RFPL - 1)) == (RFPL - 1))
11149             cpu_fprintf(f, "\n");
11150     }
11151     cpu_fprintf(f, "FPSCR " TARGET_FMT_lx "\n", env->fpscr);
11152 #if !defined(CONFIG_USER_ONLY)
11153     cpu_fprintf(f, " SRR0 " TARGET_FMT_lx "  SRR1 " TARGET_FMT_lx
11154                    "    PVR " TARGET_FMT_lx " VRSAVE " TARGET_FMT_lx "\n",
11155                 env->spr[SPR_SRR0], env->spr[SPR_SRR1],
11156                 env->spr[SPR_PVR], env->spr[SPR_VRSAVE]);
11157
11158     cpu_fprintf(f, "SPRG0 " TARGET_FMT_lx " SPRG1 " TARGET_FMT_lx
11159                    "  SPRG2 " TARGET_FMT_lx "  SPRG3 " TARGET_FMT_lx "\n",
11160                 env->spr[SPR_SPRG0], env->spr[SPR_SPRG1],
11161                 env->spr[SPR_SPRG2], env->spr[SPR_SPRG3]);
11162
11163     cpu_fprintf(f, "SPRG4 " TARGET_FMT_lx " SPRG5 " TARGET_FMT_lx
11164                    "  SPRG6 " TARGET_FMT_lx "  SPRG7 " TARGET_FMT_lx "\n",
11165                 env->spr[SPR_SPRG4], env->spr[SPR_SPRG5],
11166                 env->spr[SPR_SPRG6], env->spr[SPR_SPRG7]);
11167
11168     if (env->excp_model == POWERPC_EXCP_BOOKE) {
11169         cpu_fprintf(f, "CSRR0 " TARGET_FMT_lx " CSRR1 " TARGET_FMT_lx
11170                        " MCSRR0 " TARGET_FMT_lx " MCSRR1 " TARGET_FMT_lx "\n",
11171                     env->spr[SPR_BOOKE_CSRR0], env->spr[SPR_BOOKE_CSRR1],
11172                     env->spr[SPR_BOOKE_MCSRR0], env->spr[SPR_BOOKE_MCSRR1]);
11173
11174         cpu_fprintf(f, "  TCR " TARGET_FMT_lx "   TSR " TARGET_FMT_lx
11175                        "    ESR " TARGET_FMT_lx "   DEAR " TARGET_FMT_lx "\n",
11176                     env->spr[SPR_BOOKE_TCR], env->spr[SPR_BOOKE_TSR],
11177                     env->spr[SPR_BOOKE_ESR], env->spr[SPR_BOOKE_DEAR]);
11178
11179         cpu_fprintf(f, "  PIR " TARGET_FMT_lx " DECAR " TARGET_FMT_lx
11180                        "   IVPR " TARGET_FMT_lx "   EPCR " TARGET_FMT_lx "\n",
11181                     env->spr[SPR_BOOKE_PIR], env->spr[SPR_BOOKE_DECAR],
11182                     env->spr[SPR_BOOKE_IVPR], env->spr[SPR_BOOKE_EPCR]);
11183
11184         cpu_fprintf(f, " MCSR " TARGET_FMT_lx " SPRG8 " TARGET_FMT_lx
11185                        "    EPR " TARGET_FMT_lx "\n",
11186                     env->spr[SPR_BOOKE_MCSR], env->spr[SPR_BOOKE_SPRG8],
11187                     env->spr[SPR_BOOKE_EPR]);
11188
11189         /* FSL-specific */
11190         cpu_fprintf(f, " MCAR " TARGET_FMT_lx "  PID1 " TARGET_FMT_lx
11191                        "   PID2 " TARGET_FMT_lx "    SVR " TARGET_FMT_lx "\n",
11192                     env->spr[SPR_Exxx_MCAR], env->spr[SPR_BOOKE_PID1],
11193                     env->spr[SPR_BOOKE_PID2], env->spr[SPR_E500_SVR]);
11194
11195         /*
11196          * IVORs are left out as they are large and do not change often --
11197          * they can be read with "p $ivor0", "p $ivor1", etc.
11198          */
11199     }
11200
11201 #if defined(TARGET_PPC64)
11202     if (env->flags & POWERPC_FLAG_CFAR) {
11203         cpu_fprintf(f, " CFAR " TARGET_FMT_lx"\n", env->cfar);
11204     }
11205 #endif
11206
11207     switch (env->mmu_model) {
11208     case POWERPC_MMU_32B:
11209     case POWERPC_MMU_601:
11210     case POWERPC_MMU_SOFT_6xx:
11211     case POWERPC_MMU_SOFT_74xx:
11212 #if defined(TARGET_PPC64)
11213     case POWERPC_MMU_64B:
11214     case POWERPC_MMU_2_06:
11215     case POWERPC_MMU_2_06a:
11216     case POWERPC_MMU_2_06d:
11217 #endif
11218         cpu_fprintf(f, " SDR1 " TARGET_FMT_lx "   DAR " TARGET_FMT_lx
11219                        "  DSISR " TARGET_FMT_lx "\n", env->spr[SPR_SDR1],
11220                     env->spr[SPR_DAR], env->spr[SPR_DSISR]);
11221         break;
11222     case POWERPC_MMU_BOOKE206:
11223         cpu_fprintf(f, " MAS0 " TARGET_FMT_lx "  MAS1 " TARGET_FMT_lx
11224                        "   MAS2 " TARGET_FMT_lx "   MAS3 " TARGET_FMT_lx "\n",
11225                     env->spr[SPR_BOOKE_MAS0], env->spr[SPR_BOOKE_MAS1],
11226                     env->spr[SPR_BOOKE_MAS2], env->spr[SPR_BOOKE_MAS3]);
11227
11228         cpu_fprintf(f, " MAS4 " TARGET_FMT_lx "  MAS6 " TARGET_FMT_lx
11229                        "   MAS7 " TARGET_FMT_lx "    PID " TARGET_FMT_lx "\n",
11230                     env->spr[SPR_BOOKE_MAS4], env->spr[SPR_BOOKE_MAS6],
11231                     env->spr[SPR_BOOKE_MAS7], env->spr[SPR_BOOKE_PID]);
11232
11233         cpu_fprintf(f, "MMUCFG " TARGET_FMT_lx " TLB0CFG " TARGET_FMT_lx
11234                        " TLB1CFG " TARGET_FMT_lx "\n",
11235                     env->spr[SPR_MMUCFG], env->spr[SPR_BOOKE_TLB0CFG],
11236                     env->spr[SPR_BOOKE_TLB1CFG]);
11237         break;
11238     default:
11239         break;
11240     }
11241 #endif
11242
11243 #undef RGPL
11244 #undef RFPL
11245 }
11246
11247 void ppc_cpu_dump_statistics(CPUState *cs, FILE*f,
11248                              fprintf_function cpu_fprintf, int flags)
11249 {
11250 #if defined(DO_PPC_STATISTICS)
11251     PowerPCCPU *cpu = POWERPC_CPU(cs);
11252     opc_handler_t **t1, **t2, **t3, *handler;
11253     int op1, op2, op3;
11254
11255     t1 = cpu->env.opcodes;
11256     for (op1 = 0; op1 < 64; op1++) {
11257         handler = t1[op1];
11258         if (is_indirect_opcode(handler)) {
11259             t2 = ind_table(handler);
11260             for (op2 = 0; op2 < 32; op2++) {
11261                 handler = t2[op2];
11262                 if (is_indirect_opcode(handler)) {
11263                     t3 = ind_table(handler);
11264                     for (op3 = 0; op3 < 32; op3++) {
11265                         handler = t3[op3];
11266                         if (handler->count == 0)
11267                             continue;
11268                         cpu_fprintf(f, "%02x %02x %02x (%02x %04d) %16s: "
11269                                     "%016" PRIx64 " %" PRId64 "\n",
11270                                     op1, op2, op3, op1, (op3 << 5) | op2,
11271                                     handler->oname,
11272                                     handler->count, handler->count);
11273                     }
11274                 } else {
11275                     if (handler->count == 0)
11276                         continue;
11277                     cpu_fprintf(f, "%02x %02x    (%02x %04d) %16s: "
11278                                 "%016" PRIx64 " %" PRId64 "\n",
11279                                 op1, op2, op1, op2, handler->oname,
11280                                 handler->count, handler->count);
11281                 }
11282             }
11283         } else {
11284             if (handler->count == 0)
11285                 continue;
11286             cpu_fprintf(f, "%02x       (%02x     ) %16s: %016" PRIx64
11287                         " %" PRId64 "\n",
11288                         op1, op1, handler->oname,
11289                         handler->count, handler->count);
11290         }
11291     }
11292 #endif
11293 }
11294
11295 /*****************************************************************************/
11296 static inline void gen_intermediate_code_internal(PowerPCCPU *cpu,
11297                                                   TranslationBlock *tb,
11298                                                   bool search_pc)
11299 {
11300     CPUState *cs = CPU(cpu);
11301     CPUPPCState *env = &cpu->env;
11302     DisasContext ctx, *ctxp = &ctx;
11303     opc_handler_t **table, *handler;
11304     target_ulong pc_start;
11305     uint16_t *gen_opc_end;
11306     CPUBreakpoint *bp;
11307     int j, lj = -1;
11308     int num_insns;
11309     int max_insns;
11310
11311     pc_start = tb->pc;
11312     gen_opc_end = tcg_ctx.gen_opc_buf + OPC_MAX_SIZE;
11313     ctx.nip = pc_start;
11314     ctx.tb = tb;
11315     ctx.exception = POWERPC_EXCP_NONE;
11316     ctx.spr_cb = env->spr_cb;
11317     ctx.pr = msr_pr;
11318     ctx.hv = !msr_pr && msr_hv;
11319     ctx.mem_idx = env->mmu_idx;
11320     ctx.insns_flags = env->insns_flags;
11321     ctx.insns_flags2 = env->insns_flags2;
11322     ctx.access_type = -1;
11323     ctx.le_mode = env->hflags & (1 << MSR_LE) ? 1 : 0;
11324     ctx.default_tcg_memop_mask = ctx.le_mode ? MO_LE : MO_BE;
11325 #if defined(TARGET_PPC64)
11326     ctx.sf_mode = msr_is_64bit(env, env->msr);
11327     ctx.has_cfar = !!(env->flags & POWERPC_FLAG_CFAR);
11328 #endif
11329     ctx.fpu_enabled = msr_fp;
11330     if ((env->flags & POWERPC_FLAG_SPE) && msr_spe)
11331         ctx.spe_enabled = msr_spe;
11332     else
11333         ctx.spe_enabled = 0;
11334     if ((env->flags & POWERPC_FLAG_VRE) && msr_vr)
11335         ctx.altivec_enabled = msr_vr;
11336     else
11337         ctx.altivec_enabled = 0;
11338     if ((env->flags & POWERPC_FLAG_VSX) && msr_vsx) {
11339         ctx.vsx_enabled = msr_vsx;
11340     } else {
11341         ctx.vsx_enabled = 0;
11342     }
11343     if ((env->flags & POWERPC_FLAG_SE) && msr_se)
11344         ctx.singlestep_enabled = CPU_SINGLE_STEP;
11345     else
11346         ctx.singlestep_enabled = 0;
11347     if ((env->flags & POWERPC_FLAG_BE) && msr_be)
11348         ctx.singlestep_enabled |= CPU_BRANCH_STEP;
11349     if (unlikely(cs->singlestep_enabled)) {
11350         ctx.singlestep_enabled |= GDBSTUB_SINGLE_STEP;
11351     }
11352 #if defined (DO_SINGLE_STEP) && 0
11353     /* Single step trace mode */
11354     msr_se = 1;
11355 #endif
11356     num_insns = 0;
11357     max_insns = tb->cflags & CF_COUNT_MASK;
11358     if (max_insns == 0)
11359         max_insns = CF_COUNT_MASK;
11360
11361     gen_tb_start();
11362     tcg_clear_temp_count();
11363     /* Set env in case of segfault during code fetch */
11364     while (ctx.exception == POWERPC_EXCP_NONE
11365             && tcg_ctx.gen_opc_ptr < gen_opc_end) {
11366         if (unlikely(!QTAILQ_EMPTY(&cs->breakpoints))) {
11367             QTAILQ_FOREACH(bp, &cs->breakpoints, entry) {
11368                 if (bp->pc == ctx.nip) {
11369                     gen_debug_exception(ctxp);
11370                     break;
11371                 }
11372             }
11373         }
11374         if (unlikely(search_pc)) {
11375             j = tcg_ctx.gen_opc_ptr - tcg_ctx.gen_opc_buf;
11376             if (lj < j) {
11377                 lj++;
11378                 while (lj < j)
11379                     tcg_ctx.gen_opc_instr_start[lj++] = 0;
11380             }
11381             tcg_ctx.gen_opc_pc[lj] = ctx.nip;
11382             tcg_ctx.gen_opc_instr_start[lj] = 1;
11383             tcg_ctx.gen_opc_icount[lj] = num_insns;
11384         }
11385         LOG_DISAS("----------------\n");
11386         LOG_DISAS("nip=" TARGET_FMT_lx " super=%d ir=%d\n",
11387                   ctx.nip, ctx.mem_idx, (int)msr_ir);
11388         if (num_insns + 1 == max_insns && (tb->cflags & CF_LAST_IO))
11389             gen_io_start();
11390         if (unlikely(need_byteswap(&ctx))) {
11391             ctx.opcode = bswap32(cpu_ldl_code(env, ctx.nip));
11392         } else {
11393             ctx.opcode = cpu_ldl_code(env, ctx.nip);
11394         }
11395         LOG_DISAS("translate opcode %08x (%02x %02x %02x) (%s)\n",
11396                     ctx.opcode, opc1(ctx.opcode), opc2(ctx.opcode),
11397                     opc3(ctx.opcode), ctx.le_mode ? "little" : "big");
11398         if (unlikely(qemu_loglevel_mask(CPU_LOG_TB_OP | CPU_LOG_TB_OP_OPT))) {
11399             tcg_gen_debug_insn_start(ctx.nip);
11400         }
11401         ctx.nip += 4;
11402         table = env->opcodes;
11403         num_insns++;
11404         handler = table[opc1(ctx.opcode)];
11405         if (is_indirect_opcode(handler)) {
11406             table = ind_table(handler);
11407             handler = table[opc2(ctx.opcode)];
11408             if (is_indirect_opcode(handler)) {
11409                 table = ind_table(handler);
11410                 handler = table[opc3(ctx.opcode)];
11411             }
11412         }
11413         /* Is opcode *REALLY* valid ? */
11414         if (unlikely(handler->handler == &gen_invalid)) {
11415             if (qemu_log_enabled()) {
11416                 qemu_log("invalid/unsupported opcode: "
11417                          "%02x - %02x - %02x (%08x) " TARGET_FMT_lx " %d\n",
11418                          opc1(ctx.opcode), opc2(ctx.opcode),
11419                          opc3(ctx.opcode), ctx.opcode, ctx.nip - 4, (int)msr_ir);
11420             }
11421         } else {
11422             uint32_t inval;
11423
11424             if (unlikely(handler->type & (PPC_SPE | PPC_SPE_SINGLE | PPC_SPE_DOUBLE) && Rc(ctx.opcode))) {
11425                 inval = handler->inval2;
11426             } else {
11427                 inval = handler->inval1;
11428             }
11429
11430             if (unlikely((ctx.opcode & inval) != 0)) {
11431                 if (qemu_log_enabled()) {
11432                     qemu_log("invalid bits: %08x for opcode: "
11433                              "%02x - %02x - %02x (%08x) " TARGET_FMT_lx "\n",
11434                              ctx.opcode & inval, opc1(ctx.opcode),
11435                              opc2(ctx.opcode), opc3(ctx.opcode),
11436                              ctx.opcode, ctx.nip - 4);
11437                 }
11438                 gen_inval_exception(ctxp, POWERPC_EXCP_INVAL_INVAL);
11439                 break;
11440             }
11441         }
11442         (*(handler->handler))(&ctx);
11443 #if defined(DO_PPC_STATISTICS)
11444         handler->count++;
11445 #endif
11446         /* Check trace mode exceptions */
11447         if (unlikely(ctx.singlestep_enabled & CPU_SINGLE_STEP &&
11448                      (ctx.nip <= 0x100 || ctx.nip > 0xF00) &&
11449                      ctx.exception != POWERPC_SYSCALL &&
11450                      ctx.exception != POWERPC_EXCP_TRAP &&
11451                      ctx.exception != POWERPC_EXCP_BRANCH)) {
11452             gen_exception(ctxp, POWERPC_EXCP_TRACE);
11453         } else if (unlikely(((ctx.nip & (TARGET_PAGE_SIZE - 1)) == 0) ||
11454                             (cs->singlestep_enabled) ||
11455                             singlestep ||
11456                             num_insns >= max_insns)) {
11457             /* if we reach a page boundary or are single stepping, stop
11458              * generation
11459              */
11460             break;
11461         }
11462         if (tcg_check_temp_count()) {
11463             fprintf(stderr, "Opcode %02x %02x %02x (%08x) leaked temporaries\n",
11464                     opc1(ctx.opcode), opc2(ctx.opcode), opc3(ctx.opcode),
11465                     ctx.opcode);
11466             exit(1);
11467         }
11468     }
11469     if (tb->cflags & CF_LAST_IO)
11470         gen_io_end();
11471     if (ctx.exception == POWERPC_EXCP_NONE) {
11472         gen_goto_tb(&ctx, 0, ctx.nip);
11473     } else if (ctx.exception != POWERPC_EXCP_BRANCH) {
11474         if (unlikely(cs->singlestep_enabled)) {
11475             gen_debug_exception(ctxp);
11476         }
11477         /* Generate the return instruction */
11478         tcg_gen_exit_tb(0);
11479     }
11480     gen_tb_end(tb, num_insns);
11481     *tcg_ctx.gen_opc_ptr = INDEX_op_end;
11482     if (unlikely(search_pc)) {
11483         j = tcg_ctx.gen_opc_ptr - tcg_ctx.gen_opc_buf;
11484         lj++;
11485         while (lj <= j)
11486             tcg_ctx.gen_opc_instr_start[lj++] = 0;
11487     } else {
11488         tb->size = ctx.nip - pc_start;
11489         tb->icount = num_insns;
11490     }
11491 #if defined(DEBUG_DISAS)
11492     if (qemu_loglevel_mask(CPU_LOG_TB_IN_ASM)) {
11493         int flags;
11494         flags = env->bfd_mach;
11495         flags |= ctx.le_mode << 16;
11496         qemu_log("IN: %s\n", lookup_symbol(pc_start));
11497         log_target_disas(env, pc_start, ctx.nip - pc_start, flags);
11498         qemu_log("\n");
11499     }
11500 #endif
11501 }
11502
11503 void gen_intermediate_code (CPUPPCState *env, struct TranslationBlock *tb)
11504 {
11505     gen_intermediate_code_internal(ppc_env_get_cpu(env), tb, false);
11506 }
11507
11508 void gen_intermediate_code_pc (CPUPPCState *env, struct TranslationBlock *tb)
11509 {
11510     gen_intermediate_code_internal(ppc_env_get_cpu(env), tb, true);
11511 }
11512
11513 void restore_state_to_opc(CPUPPCState *env, TranslationBlock *tb, int pc_pos)
11514 {
11515     env->nip = tcg_ctx.gen_opc_pc[pc_pos];
11516 }
This page took 0.674656 seconds and 4 git commands to generate.