]> Git Repo - qemu.git/blob - target-ppc/translate.c
PPC: Add definitions for GIVORs
[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 #define CPU_SINGLE_STEP 0x1
31 #define CPU_BRANCH_STEP 0x2
32 #define GDBSTUB_SINGLE_STEP 0x4
33
34 /* Include definitions for instructions classes and implementations flags */
35 //#define PPC_DEBUG_DISAS
36 //#define DO_PPC_STATISTICS
37
38 #ifdef PPC_DEBUG_DISAS
39 #  define LOG_DISAS(...) qemu_log_mask(CPU_LOG_TB_IN_ASM, ## __VA_ARGS__)
40 #else
41 #  define LOG_DISAS(...) do { } while (0)
42 #endif
43 /*****************************************************************************/
44 /* Code translation helpers                                                  */
45
46 /* global register indexes */
47 static TCGv_ptr cpu_env;
48 static char cpu_reg_names[10*3 + 22*4 /* GPR */
49 #if !defined(TARGET_PPC64)
50     + 10*4 + 22*5 /* SPE GPRh */
51 #endif
52     + 10*4 + 22*5 /* FPR */
53     + 2*(10*6 + 22*7) /* AVRh, AVRl */
54     + 10*5 + 22*6 /* VSR */
55     + 8*5 /* CRF */];
56 static TCGv cpu_gpr[32];
57 #if !defined(TARGET_PPC64)
58 static TCGv cpu_gprh[32];
59 #endif
60 static TCGv_i64 cpu_fpr[32];
61 static TCGv_i64 cpu_avrh[32], cpu_avrl[32];
62 static TCGv_i64 cpu_vsr[32];
63 static TCGv_i32 cpu_crf[8];
64 static TCGv cpu_nip;
65 static TCGv cpu_msr;
66 static TCGv cpu_ctr;
67 static TCGv cpu_lr;
68 #if defined(TARGET_PPC64)
69 static TCGv cpu_cfar;
70 #endif
71 static TCGv cpu_xer, cpu_so, cpu_ov, cpu_ca;
72 static TCGv cpu_reserve;
73 static TCGv cpu_fpscr;
74 static TCGv_i32 cpu_access_type;
75
76 #include "exec/gen-icount.h"
77
78 void ppc_translate_init(void)
79 {
80     int i;
81     char* p;
82     size_t cpu_reg_names_size;
83     static int done_init = 0;
84
85     if (done_init)
86         return;
87
88     cpu_env = tcg_global_reg_new_ptr(TCG_AREG0, "env");
89
90     p = cpu_reg_names;
91     cpu_reg_names_size = sizeof(cpu_reg_names);
92
93     for (i = 0; i < 8; i++) {
94         snprintf(p, cpu_reg_names_size, "crf%d", i);
95         cpu_crf[i] = tcg_global_mem_new_i32(TCG_AREG0,
96                                             offsetof(CPUPPCState, crf[i]), p);
97         p += 5;
98         cpu_reg_names_size -= 5;
99     }
100
101     for (i = 0; i < 32; i++) {
102         snprintf(p, cpu_reg_names_size, "r%d", i);
103         cpu_gpr[i] = tcg_global_mem_new(TCG_AREG0,
104                                         offsetof(CPUPPCState, gpr[i]), p);
105         p += (i < 10) ? 3 : 4;
106         cpu_reg_names_size -= (i < 10) ? 3 : 4;
107 #if !defined(TARGET_PPC64)
108         snprintf(p, cpu_reg_names_size, "r%dH", i);
109         cpu_gprh[i] = tcg_global_mem_new_i32(TCG_AREG0,
110                                              offsetof(CPUPPCState, gprh[i]), p);
111         p += (i < 10) ? 4 : 5;
112         cpu_reg_names_size -= (i < 10) ? 4 : 5;
113 #endif
114
115         snprintf(p, cpu_reg_names_size, "fp%d", i);
116         cpu_fpr[i] = tcg_global_mem_new_i64(TCG_AREG0,
117                                             offsetof(CPUPPCState, fpr[i]), p);
118         p += (i < 10) ? 4 : 5;
119         cpu_reg_names_size -= (i < 10) ? 4 : 5;
120
121         snprintf(p, cpu_reg_names_size, "avr%dH", i);
122 #ifdef HOST_WORDS_BIGENDIAN
123         cpu_avrh[i] = tcg_global_mem_new_i64(TCG_AREG0,
124                                              offsetof(CPUPPCState, avr[i].u64[0]), p);
125 #else
126         cpu_avrh[i] = tcg_global_mem_new_i64(TCG_AREG0,
127                                              offsetof(CPUPPCState, avr[i].u64[1]), p);
128 #endif
129         p += (i < 10) ? 6 : 7;
130         cpu_reg_names_size -= (i < 10) ? 6 : 7;
131
132         snprintf(p, cpu_reg_names_size, "avr%dL", i);
133 #ifdef HOST_WORDS_BIGENDIAN
134         cpu_avrl[i] = tcg_global_mem_new_i64(TCG_AREG0,
135                                              offsetof(CPUPPCState, avr[i].u64[1]), p);
136 #else
137         cpu_avrl[i] = tcg_global_mem_new_i64(TCG_AREG0,
138                                              offsetof(CPUPPCState, avr[i].u64[0]), p);
139 #endif
140         p += (i < 10) ? 6 : 7;
141         cpu_reg_names_size -= (i < 10) ? 6 : 7;
142         snprintf(p, cpu_reg_names_size, "vsr%d", i);
143         cpu_vsr[i] = tcg_global_mem_new_i64(TCG_AREG0,
144                                              offsetof(CPUPPCState, vsr[i]), p);
145         p += (i < 10) ? 5 : 6;
146         cpu_reg_names_size -= (i < 10) ? 5 : 6;
147     }
148
149     cpu_nip = tcg_global_mem_new(TCG_AREG0,
150                                  offsetof(CPUPPCState, nip), "nip");
151
152     cpu_msr = tcg_global_mem_new(TCG_AREG0,
153                                  offsetof(CPUPPCState, msr), "msr");
154
155     cpu_ctr = tcg_global_mem_new(TCG_AREG0,
156                                  offsetof(CPUPPCState, ctr), "ctr");
157
158     cpu_lr = tcg_global_mem_new(TCG_AREG0,
159                                 offsetof(CPUPPCState, lr), "lr");
160
161 #if defined(TARGET_PPC64)
162     cpu_cfar = tcg_global_mem_new(TCG_AREG0,
163                                   offsetof(CPUPPCState, cfar), "cfar");
164 #endif
165
166     cpu_xer = tcg_global_mem_new(TCG_AREG0,
167                                  offsetof(CPUPPCState, xer), "xer");
168     cpu_so = tcg_global_mem_new(TCG_AREG0,
169                                 offsetof(CPUPPCState, so), "SO");
170     cpu_ov = tcg_global_mem_new(TCG_AREG0,
171                                 offsetof(CPUPPCState, ov), "OV");
172     cpu_ca = tcg_global_mem_new(TCG_AREG0,
173                                 offsetof(CPUPPCState, ca), "CA");
174
175     cpu_reserve = tcg_global_mem_new(TCG_AREG0,
176                                      offsetof(CPUPPCState, reserve_addr),
177                                      "reserve_addr");
178
179     cpu_fpscr = tcg_global_mem_new(TCG_AREG0,
180                                    offsetof(CPUPPCState, fpscr), "fpscr");
181
182     cpu_access_type = tcg_global_mem_new_i32(TCG_AREG0,
183                                              offsetof(CPUPPCState, access_type), "access_type");
184
185     done_init = 1;
186 }
187
188 /* internal defines */
189 typedef struct DisasContext {
190     struct TranslationBlock *tb;
191     target_ulong nip;
192     uint32_t opcode;
193     uint32_t exception;
194     /* Routine used to access memory */
195     int mem_idx;
196     int access_type;
197     /* Translation flags */
198     int le_mode;
199 #if defined(TARGET_PPC64)
200     int sf_mode;
201     int has_cfar;
202 #endif
203     int fpu_enabled;
204     int altivec_enabled;
205     int vsx_enabled;
206     int spe_enabled;
207     ppc_spr_t *spr_cb; /* Needed to check rights for mfspr/mtspr */
208     int singlestep_enabled;
209     uint64_t insns_flags;
210     uint64_t insns_flags2;
211 } DisasContext;
212
213 /* True when active word size < size of target_long.  */
214 #ifdef TARGET_PPC64
215 # define NARROW_MODE(C)  (!(C)->sf_mode)
216 #else
217 # define NARROW_MODE(C)  0
218 #endif
219
220 struct opc_handler_t {
221     /* invalid bits for instruction 1 (Rc(opcode) == 0) */
222     uint32_t inval1;
223     /* invalid bits for instruction 2 (Rc(opcode) == 1) */
224     uint32_t inval2;
225     /* instruction type */
226     uint64_t type;
227     /* extended instruction type */
228     uint64_t type2;
229     /* handler */
230     void (*handler)(DisasContext *ctx);
231 #if defined(DO_PPC_STATISTICS) || defined(PPC_DUMP_CPU)
232     const char *oname;
233 #endif
234 #if defined(DO_PPC_STATISTICS)
235     uint64_t count;
236 #endif
237 };
238
239 static inline void gen_reset_fpstatus(void)
240 {
241     gen_helper_reset_fpstatus(cpu_env);
242 }
243
244 static inline void gen_compute_fprf(TCGv_i64 arg, int set_fprf, int set_rc)
245 {
246     TCGv_i32 t0 = tcg_temp_new_i32();
247
248     if (set_fprf != 0) {
249         /* This case might be optimized later */
250         tcg_gen_movi_i32(t0, 1);
251         gen_helper_compute_fprf(t0, cpu_env, arg, t0);
252         if (unlikely(set_rc)) {
253             tcg_gen_mov_i32(cpu_crf[1], t0);
254         }
255         gen_helper_float_check_status(cpu_env);
256     } else if (unlikely(set_rc)) {
257         /* We always need to compute fpcc */
258         tcg_gen_movi_i32(t0, 0);
259         gen_helper_compute_fprf(t0, cpu_env, arg, t0);
260         tcg_gen_mov_i32(cpu_crf[1], t0);
261     }
262
263     tcg_temp_free_i32(t0);
264 }
265
266 static inline void gen_set_access_type(DisasContext *ctx, int access_type)
267 {
268     if (ctx->access_type != access_type) {
269         tcg_gen_movi_i32(cpu_access_type, access_type);
270         ctx->access_type = access_type;
271     }
272 }
273
274 static inline void gen_update_nip(DisasContext *ctx, target_ulong nip)
275 {
276     if (NARROW_MODE(ctx)) {
277         nip = (uint32_t)nip;
278     }
279     tcg_gen_movi_tl(cpu_nip, nip);
280 }
281
282 static inline void gen_exception_err(DisasContext *ctx, uint32_t excp, uint32_t error)
283 {
284     TCGv_i32 t0, t1;
285     if (ctx->exception == POWERPC_EXCP_NONE) {
286         gen_update_nip(ctx, ctx->nip);
287     }
288     t0 = tcg_const_i32(excp);
289     t1 = tcg_const_i32(error);
290     gen_helper_raise_exception_err(cpu_env, t0, t1);
291     tcg_temp_free_i32(t0);
292     tcg_temp_free_i32(t1);
293     ctx->exception = (excp);
294 }
295
296 static inline void gen_exception(DisasContext *ctx, uint32_t excp)
297 {
298     TCGv_i32 t0;
299     if (ctx->exception == POWERPC_EXCP_NONE) {
300         gen_update_nip(ctx, ctx->nip);
301     }
302     t0 = tcg_const_i32(excp);
303     gen_helper_raise_exception(cpu_env, t0);
304     tcg_temp_free_i32(t0);
305     ctx->exception = (excp);
306 }
307
308 static inline void gen_debug_exception(DisasContext *ctx)
309 {
310     TCGv_i32 t0;
311
312     if ((ctx->exception != POWERPC_EXCP_BRANCH) &&
313         (ctx->exception != POWERPC_EXCP_SYNC)) {
314         gen_update_nip(ctx, ctx->nip);
315     }
316     t0 = tcg_const_i32(EXCP_DEBUG);
317     gen_helper_raise_exception(cpu_env, t0);
318     tcg_temp_free_i32(t0);
319 }
320
321 static inline void gen_inval_exception(DisasContext *ctx, uint32_t error)
322 {
323     gen_exception_err(ctx, POWERPC_EXCP_PROGRAM, POWERPC_EXCP_INVAL | error);
324 }
325
326 /* Stop translation */
327 static inline void gen_stop_exception(DisasContext *ctx)
328 {
329     gen_update_nip(ctx, ctx->nip);
330     ctx->exception = POWERPC_EXCP_STOP;
331 }
332
333 /* No need to update nip here, as execution flow will change */
334 static inline void gen_sync_exception(DisasContext *ctx)
335 {
336     ctx->exception = POWERPC_EXCP_SYNC;
337 }
338
339 #define GEN_HANDLER(name, opc1, opc2, opc3, inval, type)                      \
340 GEN_OPCODE(name, opc1, opc2, opc3, inval, type, PPC_NONE)
341
342 #define GEN_HANDLER_E(name, opc1, opc2, opc3, inval, type, type2)             \
343 GEN_OPCODE(name, opc1, opc2, opc3, inval, type, type2)
344
345 #define GEN_HANDLER2(name, onam, opc1, opc2, opc3, inval, type)               \
346 GEN_OPCODE2(name, onam, opc1, opc2, opc3, inval, type, PPC_NONE)
347
348 #define GEN_HANDLER2_E(name, onam, opc1, opc2, opc3, inval, type, type2)      \
349 GEN_OPCODE2(name, onam, opc1, opc2, opc3, inval, type, type2)
350
351 typedef struct opcode_t {
352     unsigned char opc1, opc2, opc3;
353 #if HOST_LONG_BITS == 64 /* Explicitly align to 64 bits */
354     unsigned char pad[5];
355 #else
356     unsigned char pad[1];
357 #endif
358     opc_handler_t handler;
359     const char *oname;
360 } opcode_t;
361
362 /*****************************************************************************/
363 /***                           Instruction decoding                        ***/
364 #define EXTRACT_HELPER(name, shift, nb)                                       \
365 static inline uint32_t name(uint32_t opcode)                                  \
366 {                                                                             \
367     return (opcode >> (shift)) & ((1 << (nb)) - 1);                           \
368 }
369
370 #define EXTRACT_SHELPER(name, shift, nb)                                      \
371 static inline int32_t name(uint32_t opcode)                                   \
372 {                                                                             \
373     return (int16_t)((opcode >> (shift)) & ((1 << (nb)) - 1));                \
374 }
375
376 #define EXTRACT_HELPER_SPLIT(name, shift1, nb1, shift2, nb2)                  \
377 static inline uint32_t name(uint32_t opcode)                                  \
378 {                                                                             \
379     return (((opcode >> (shift1)) & ((1 << (nb1)) - 1)) << nb2) |             \
380             ((opcode >> (shift2)) & ((1 << (nb2)) - 1));                      \
381 }
382 /* Opcode part 1 */
383 EXTRACT_HELPER(opc1, 26, 6);
384 /* Opcode part 2 */
385 EXTRACT_HELPER(opc2, 1, 5);
386 /* Opcode part 3 */
387 EXTRACT_HELPER(opc3, 6, 5);
388 /* Update Cr0 flags */
389 EXTRACT_HELPER(Rc, 0, 1);
390 /* Update Cr6 flags (Altivec) */
391 EXTRACT_HELPER(Rc21, 10, 1);
392 /* Destination */
393 EXTRACT_HELPER(rD, 21, 5);
394 /* Source */
395 EXTRACT_HELPER(rS, 21, 5);
396 /* First operand */
397 EXTRACT_HELPER(rA, 16, 5);
398 /* Second operand */
399 EXTRACT_HELPER(rB, 11, 5);
400 /* Third operand */
401 EXTRACT_HELPER(rC, 6, 5);
402 /***                               Get CRn                                 ***/
403 EXTRACT_HELPER(crfD, 23, 3);
404 EXTRACT_HELPER(crfS, 18, 3);
405 EXTRACT_HELPER(crbD, 21, 5);
406 EXTRACT_HELPER(crbA, 16, 5);
407 EXTRACT_HELPER(crbB, 11, 5);
408 /* SPR / TBL */
409 EXTRACT_HELPER(_SPR, 11, 10);
410 static inline uint32_t SPR(uint32_t opcode)
411 {
412     uint32_t sprn = _SPR(opcode);
413
414     return ((sprn >> 5) & 0x1F) | ((sprn & 0x1F) << 5);
415 }
416 /***                              Get constants                            ***/
417 EXTRACT_HELPER(IMM, 12, 8);
418 /* 16 bits signed immediate value */
419 EXTRACT_SHELPER(SIMM, 0, 16);
420 /* 16 bits unsigned immediate value */
421 EXTRACT_HELPER(UIMM, 0, 16);
422 /* 5 bits signed immediate value */
423 EXTRACT_HELPER(SIMM5, 16, 5);
424 /* 5 bits signed immediate value */
425 EXTRACT_HELPER(UIMM5, 16, 5);
426 /* Bit count */
427 EXTRACT_HELPER(NB, 11, 5);
428 /* Shift count */
429 EXTRACT_HELPER(SH, 11, 5);
430 /* Vector shift count */
431 EXTRACT_HELPER(VSH, 6, 4);
432 /* Mask start */
433 EXTRACT_HELPER(MB, 6, 5);
434 /* Mask end */
435 EXTRACT_HELPER(ME, 1, 5);
436 /* Trap operand */
437 EXTRACT_HELPER(TO, 21, 5);
438
439 EXTRACT_HELPER(CRM, 12, 8);
440 EXTRACT_HELPER(SR, 16, 4);
441
442 /* mtfsf/mtfsfi */
443 EXTRACT_HELPER(FPBF, 23, 3);
444 EXTRACT_HELPER(FPIMM, 12, 4);
445 EXTRACT_HELPER(FPL, 25, 1);
446 EXTRACT_HELPER(FPFLM, 17, 8);
447 EXTRACT_HELPER(FPW, 16, 1);
448
449 /***                            Jump target decoding                       ***/
450 /* Displacement */
451 EXTRACT_SHELPER(d, 0, 16);
452 /* Immediate address */
453 static inline target_ulong LI(uint32_t opcode)
454 {
455     return (opcode >> 0) & 0x03FFFFFC;
456 }
457
458 static inline uint32_t BD(uint32_t opcode)
459 {
460     return (opcode >> 0) & 0xFFFC;
461 }
462
463 EXTRACT_HELPER(BO, 21, 5);
464 EXTRACT_HELPER(BI, 16, 5);
465 /* Absolute/relative address */
466 EXTRACT_HELPER(AA, 1, 1);
467 /* Link */
468 EXTRACT_HELPER(LK, 0, 1);
469
470 /* DFP Z22-form */
471 EXTRACT_HELPER(DCM, 10, 6)
472
473 /* DFP Z23-form */
474 EXTRACT_HELPER(RMC, 9, 2)
475
476 /* Create a mask between <start> and <end> bits */
477 static inline target_ulong MASK(uint32_t start, uint32_t end)
478 {
479     target_ulong ret;
480
481 #if defined(TARGET_PPC64)
482     if (likely(start == 0)) {
483         ret = UINT64_MAX << (63 - end);
484     } else if (likely(end == 63)) {
485         ret = UINT64_MAX >> start;
486     }
487 #else
488     if (likely(start == 0)) {
489         ret = UINT32_MAX << (31  - end);
490     } else if (likely(end == 31)) {
491         ret = UINT32_MAX >> start;
492     }
493 #endif
494     else {
495         ret = (((target_ulong)(-1ULL)) >> (start)) ^
496             (((target_ulong)(-1ULL) >> (end)) >> 1);
497         if (unlikely(start > end))
498             return ~ret;
499     }
500
501     return ret;
502 }
503
504 EXTRACT_HELPER_SPLIT(xT, 0, 1, 21, 5);
505 EXTRACT_HELPER_SPLIT(xS, 0, 1, 21, 5);
506 EXTRACT_HELPER_SPLIT(xA, 2, 1, 16, 5);
507 EXTRACT_HELPER_SPLIT(xB, 1, 1, 11, 5);
508 EXTRACT_HELPER_SPLIT(xC, 3, 1,  6, 5);
509 EXTRACT_HELPER(DM, 8, 2);
510 EXTRACT_HELPER(UIM, 16, 2);
511 EXTRACT_HELPER(SHW, 8, 2);
512 EXTRACT_HELPER(SP, 19, 2);
513 /*****************************************************************************/
514 /* PowerPC instructions table                                                */
515
516 #if defined(DO_PPC_STATISTICS)
517 #define GEN_OPCODE(name, op1, op2, op3, invl, _typ, _typ2)                    \
518 {                                                                             \
519     .opc1 = op1,                                                              \
520     .opc2 = op2,                                                              \
521     .opc3 = op3,                                                              \
522     .pad  = { 0, },                                                           \
523     .handler = {                                                              \
524         .inval1  = invl,                                                      \
525         .type = _typ,                                                         \
526         .type2 = _typ2,                                                       \
527         .handler = &gen_##name,                                               \
528         .oname = stringify(name),                                             \
529     },                                                                        \
530     .oname = stringify(name),                                                 \
531 }
532 #define GEN_OPCODE_DUAL(name, op1, op2, op3, invl1, invl2, _typ, _typ2)       \
533 {                                                                             \
534     .opc1 = op1,                                                              \
535     .opc2 = op2,                                                              \
536     .opc3 = op3,                                                              \
537     .pad  = { 0, },                                                           \
538     .handler = {                                                              \
539         .inval1  = invl1,                                                     \
540         .inval2  = invl2,                                                     \
541         .type = _typ,                                                         \
542         .type2 = _typ2,                                                       \
543         .handler = &gen_##name,                                               \
544         .oname = stringify(name),                                             \
545     },                                                                        \
546     .oname = stringify(name),                                                 \
547 }
548 #define GEN_OPCODE2(name, onam, op1, op2, op3, invl, _typ, _typ2)             \
549 {                                                                             \
550     .opc1 = op1,                                                              \
551     .opc2 = op2,                                                              \
552     .opc3 = op3,                                                              \
553     .pad  = { 0, },                                                           \
554     .handler = {                                                              \
555         .inval1  = invl,                                                      \
556         .type = _typ,                                                         \
557         .type2 = _typ2,                                                       \
558         .handler = &gen_##name,                                               \
559         .oname = onam,                                                        \
560     },                                                                        \
561     .oname = onam,                                                            \
562 }
563 #else
564 #define GEN_OPCODE(name, op1, op2, op3, invl, _typ, _typ2)                    \
565 {                                                                             \
566     .opc1 = op1,                                                              \
567     .opc2 = op2,                                                              \
568     .opc3 = op3,                                                              \
569     .pad  = { 0, },                                                           \
570     .handler = {                                                              \
571         .inval1  = invl,                                                      \
572         .type = _typ,                                                         \
573         .type2 = _typ2,                                                       \
574         .handler = &gen_##name,                                               \
575     },                                                                        \
576     .oname = stringify(name),                                                 \
577 }
578 #define GEN_OPCODE_DUAL(name, op1, op2, op3, invl1, invl2, _typ, _typ2)       \
579 {                                                                             \
580     .opc1 = op1,                                                              \
581     .opc2 = op2,                                                              \
582     .opc3 = op3,                                                              \
583     .pad  = { 0, },                                                           \
584     .handler = {                                                              \
585         .inval1  = invl1,                                                     \
586         .inval2  = invl2,                                                     \
587         .type = _typ,                                                         \
588         .type2 = _typ2,                                                       \
589         .handler = &gen_##name,                                               \
590     },                                                                        \
591     .oname = stringify(name),                                                 \
592 }
593 #define GEN_OPCODE2(name, onam, op1, op2, op3, invl, _typ, _typ2)             \
594 {                                                                             \
595     .opc1 = op1,                                                              \
596     .opc2 = op2,                                                              \
597     .opc3 = op3,                                                              \
598     .pad  = { 0, },                                                           \
599     .handler = {                                                              \
600         .inval1  = invl,                                                      \
601         .type = _typ,                                                         \
602         .type2 = _typ2,                                                       \
603         .handler = &gen_##name,                                               \
604     },                                                                        \
605     .oname = onam,                                                            \
606 }
607 #endif
608
609 /* SPR load/store helpers */
610 static inline void gen_load_spr(TCGv t, int reg)
611 {
612     tcg_gen_ld_tl(t, cpu_env, offsetof(CPUPPCState, spr[reg]));
613 }
614
615 static inline void gen_store_spr(int reg, TCGv t)
616 {
617     tcg_gen_st_tl(t, cpu_env, offsetof(CPUPPCState, spr[reg]));
618 }
619
620 /* Invalid instruction */
621 static void gen_invalid(DisasContext *ctx)
622 {
623     gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL);
624 }
625
626 static opc_handler_t invalid_handler = {
627     .inval1  = 0xFFFFFFFF,
628     .inval2  = 0xFFFFFFFF,
629     .type    = PPC_NONE,
630     .type2   = PPC_NONE,
631     .handler = gen_invalid,
632 };
633
634 #if defined(TARGET_PPC64)
635 /* NOTE: as this time, the only use of is_user_mode() is in 64 bit code.  And */
636 /*       so the function is wrapped in the standard 64-bit ifdef in order to  */
637 /*       avoid compiler warnings in 32-bit implementations.                   */
638 static bool is_user_mode(DisasContext *ctx)
639 {
640 #if defined(CONFIG_USER_ONLY)
641     return true;
642 #else
643     return ctx->mem_idx == 0;
644 #endif
645 }
646 #endif
647
648 /***                           Integer comparison                          ***/
649
650 static inline void gen_op_cmp(TCGv arg0, TCGv arg1, int s, int crf)
651 {
652     TCGv t0 = tcg_temp_new();
653     TCGv_i32 t1 = tcg_temp_new_i32();
654
655     tcg_gen_trunc_tl_i32(cpu_crf[crf], cpu_so);
656
657     tcg_gen_setcond_tl((s ? TCG_COND_LT: TCG_COND_LTU), t0, arg0, arg1);
658     tcg_gen_trunc_tl_i32(t1, t0);
659     tcg_gen_shli_i32(t1, t1, CRF_LT);
660     tcg_gen_or_i32(cpu_crf[crf], cpu_crf[crf], t1);
661
662     tcg_gen_setcond_tl((s ? TCG_COND_GT: TCG_COND_GTU), t0, arg0, arg1);
663     tcg_gen_trunc_tl_i32(t1, t0);
664     tcg_gen_shli_i32(t1, t1, CRF_GT);
665     tcg_gen_or_i32(cpu_crf[crf], cpu_crf[crf], t1);
666
667     tcg_gen_setcond_tl(TCG_COND_EQ, t0, arg0, arg1);
668     tcg_gen_trunc_tl_i32(t1, t0);
669     tcg_gen_shli_i32(t1, t1, CRF_EQ);
670     tcg_gen_or_i32(cpu_crf[crf], cpu_crf[crf], t1);
671
672     tcg_temp_free(t0);
673     tcg_temp_free_i32(t1);
674 }
675
676 static inline void gen_op_cmpi(TCGv arg0, target_ulong arg1, int s, int crf)
677 {
678     TCGv t0 = tcg_const_tl(arg1);
679     gen_op_cmp(arg0, t0, s, crf);
680     tcg_temp_free(t0);
681 }
682
683 static inline void gen_op_cmp32(TCGv arg0, TCGv arg1, int s, int crf)
684 {
685     TCGv t0, t1;
686     t0 = tcg_temp_new();
687     t1 = tcg_temp_new();
688     if (s) {
689         tcg_gen_ext32s_tl(t0, arg0);
690         tcg_gen_ext32s_tl(t1, arg1);
691     } else {
692         tcg_gen_ext32u_tl(t0, arg0);
693         tcg_gen_ext32u_tl(t1, arg1);
694     }
695     gen_op_cmp(t0, t1, s, crf);
696     tcg_temp_free(t1);
697     tcg_temp_free(t0);
698 }
699
700 static inline void gen_op_cmpi32(TCGv arg0, target_ulong arg1, int s, int crf)
701 {
702     TCGv t0 = tcg_const_tl(arg1);
703     gen_op_cmp32(arg0, t0, s, crf);
704     tcg_temp_free(t0);
705 }
706
707 static inline void gen_set_Rc0(DisasContext *ctx, TCGv reg)
708 {
709     if (NARROW_MODE(ctx)) {
710         gen_op_cmpi32(reg, 0, 1, 0);
711     } else {
712         gen_op_cmpi(reg, 0, 1, 0);
713     }
714 }
715
716 /* cmp */
717 static void gen_cmp(DisasContext *ctx)
718 {
719     if ((ctx->opcode & 0x00200000) && (ctx->insns_flags & PPC_64B)) {
720         gen_op_cmp(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rB(ctx->opcode)],
721                    1, crfD(ctx->opcode));
722     } else {
723         gen_op_cmp32(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rB(ctx->opcode)],
724                      1, crfD(ctx->opcode));
725     }
726 }
727
728 /* cmpi */
729 static void gen_cmpi(DisasContext *ctx)
730 {
731     if ((ctx->opcode & 0x00200000) && (ctx->insns_flags & PPC_64B)) {
732         gen_op_cmpi(cpu_gpr[rA(ctx->opcode)], SIMM(ctx->opcode),
733                     1, crfD(ctx->opcode));
734     } else {
735         gen_op_cmpi32(cpu_gpr[rA(ctx->opcode)], SIMM(ctx->opcode),
736                       1, crfD(ctx->opcode));
737     }
738 }
739
740 /* cmpl */
741 static void gen_cmpl(DisasContext *ctx)
742 {
743     if ((ctx->opcode & 0x00200000) && (ctx->insns_flags & PPC_64B)) {
744         gen_op_cmp(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rB(ctx->opcode)],
745                    0, crfD(ctx->opcode));
746     } else {
747         gen_op_cmp32(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rB(ctx->opcode)],
748                      0, crfD(ctx->opcode));
749     }
750 }
751
752 /* cmpli */
753 static void gen_cmpli(DisasContext *ctx)
754 {
755     if ((ctx->opcode & 0x00200000) && (ctx->insns_flags & PPC_64B)) {
756         gen_op_cmpi(cpu_gpr[rA(ctx->opcode)], UIMM(ctx->opcode),
757                     0, crfD(ctx->opcode));
758     } else {
759         gen_op_cmpi32(cpu_gpr[rA(ctx->opcode)], UIMM(ctx->opcode),
760                       0, crfD(ctx->opcode));
761     }
762 }
763
764 /* isel (PowerPC 2.03 specification) */
765 static void gen_isel(DisasContext *ctx)
766 {
767     int l1, l2;
768     uint32_t bi = rC(ctx->opcode);
769     uint32_t mask;
770     TCGv_i32 t0;
771
772     l1 = gen_new_label();
773     l2 = gen_new_label();
774
775     mask = 1 << (3 - (bi & 0x03));
776     t0 = tcg_temp_new_i32();
777     tcg_gen_andi_i32(t0, cpu_crf[bi >> 2], mask);
778     tcg_gen_brcondi_i32(TCG_COND_EQ, t0, 0, l1);
779     if (rA(ctx->opcode) == 0)
780         tcg_gen_movi_tl(cpu_gpr[rD(ctx->opcode)], 0);
781     else
782         tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)]);
783     tcg_gen_br(l2);
784     gen_set_label(l1);
785     tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rB(ctx->opcode)]);
786     gen_set_label(l2);
787     tcg_temp_free_i32(t0);
788 }
789
790 /* cmpb: PowerPC 2.05 specification */
791 static void gen_cmpb(DisasContext *ctx)
792 {
793     gen_helper_cmpb(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)],
794                     cpu_gpr[rB(ctx->opcode)]);
795 }
796
797 /***                           Integer arithmetic                          ***/
798
799 static inline void gen_op_arith_compute_ov(DisasContext *ctx, TCGv arg0,
800                                            TCGv arg1, TCGv arg2, int sub)
801 {
802     TCGv t0 = tcg_temp_new();
803
804     tcg_gen_xor_tl(cpu_ov, arg0, arg2);
805     tcg_gen_xor_tl(t0, arg1, arg2);
806     if (sub) {
807         tcg_gen_and_tl(cpu_ov, cpu_ov, t0);
808     } else {
809         tcg_gen_andc_tl(cpu_ov, cpu_ov, t0);
810     }
811     tcg_temp_free(t0);
812     if (NARROW_MODE(ctx)) {
813         tcg_gen_ext32s_tl(cpu_ov, cpu_ov);
814     }
815     tcg_gen_shri_tl(cpu_ov, cpu_ov, TARGET_LONG_BITS - 1);
816     tcg_gen_or_tl(cpu_so, cpu_so, cpu_ov);
817 }
818
819 /* Common add function */
820 static inline void gen_op_arith_add(DisasContext *ctx, TCGv ret, TCGv arg1,
821                                     TCGv arg2, bool add_ca, bool compute_ca,
822                                     bool compute_ov, bool compute_rc0)
823 {
824     TCGv t0 = ret;
825
826     if (compute_ca || compute_ov) {
827         t0 = tcg_temp_new();
828     }
829
830     if (compute_ca) {
831         if (NARROW_MODE(ctx)) {
832             /* Caution: a non-obvious corner case of the spec is that we
833                must produce the *entire* 64-bit addition, but produce the
834                carry into bit 32.  */
835             TCGv t1 = tcg_temp_new();
836             tcg_gen_xor_tl(t1, arg1, arg2);        /* add without carry */
837             tcg_gen_add_tl(t0, arg1, arg2);
838             if (add_ca) {
839                 tcg_gen_add_tl(t0, t0, cpu_ca);
840             }
841             tcg_gen_xor_tl(cpu_ca, t0, t1);        /* bits changed w/ carry */
842             tcg_temp_free(t1);
843             tcg_gen_shri_tl(cpu_ca, cpu_ca, 32);   /* extract bit 32 */
844             tcg_gen_andi_tl(cpu_ca, cpu_ca, 1);
845         } else {
846             TCGv zero = tcg_const_tl(0);
847             if (add_ca) {
848                 tcg_gen_add2_tl(t0, cpu_ca, arg1, zero, cpu_ca, zero);
849                 tcg_gen_add2_tl(t0, cpu_ca, t0, cpu_ca, arg2, zero);
850             } else {
851                 tcg_gen_add2_tl(t0, cpu_ca, arg1, zero, arg2, zero);
852             }
853             tcg_temp_free(zero);
854         }
855     } else {
856         tcg_gen_add_tl(t0, arg1, arg2);
857         if (add_ca) {
858             tcg_gen_add_tl(t0, t0, cpu_ca);
859         }
860     }
861
862     if (compute_ov) {
863         gen_op_arith_compute_ov(ctx, t0, arg1, arg2, 0);
864     }
865     if (unlikely(compute_rc0)) {
866         gen_set_Rc0(ctx, t0);
867     }
868
869     if (!TCGV_EQUAL(t0, ret)) {
870         tcg_gen_mov_tl(ret, t0);
871         tcg_temp_free(t0);
872     }
873 }
874 /* Add functions with two operands */
875 #define GEN_INT_ARITH_ADD(name, opc3, add_ca, compute_ca, compute_ov)         \
876 static void glue(gen_, name)(DisasContext *ctx)                               \
877 {                                                                             \
878     gen_op_arith_add(ctx, cpu_gpr[rD(ctx->opcode)],                           \
879                      cpu_gpr[rA(ctx->opcode)], cpu_gpr[rB(ctx->opcode)],      \
880                      add_ca, compute_ca, compute_ov, Rc(ctx->opcode));        \
881 }
882 /* Add functions with one operand and one immediate */
883 #define GEN_INT_ARITH_ADD_CONST(name, opc3, const_val,                        \
884                                 add_ca, compute_ca, compute_ov)               \
885 static void glue(gen_, name)(DisasContext *ctx)                               \
886 {                                                                             \
887     TCGv t0 = tcg_const_tl(const_val);                                        \
888     gen_op_arith_add(ctx, cpu_gpr[rD(ctx->opcode)],                           \
889                      cpu_gpr[rA(ctx->opcode)], t0,                            \
890                      add_ca, compute_ca, compute_ov, Rc(ctx->opcode));        \
891     tcg_temp_free(t0);                                                        \
892 }
893
894 /* add  add.  addo  addo. */
895 GEN_INT_ARITH_ADD(add, 0x08, 0, 0, 0)
896 GEN_INT_ARITH_ADD(addo, 0x18, 0, 0, 1)
897 /* addc  addc.  addco  addco. */
898 GEN_INT_ARITH_ADD(addc, 0x00, 0, 1, 0)
899 GEN_INT_ARITH_ADD(addco, 0x10, 0, 1, 1)
900 /* adde  adde.  addeo  addeo. */
901 GEN_INT_ARITH_ADD(adde, 0x04, 1, 1, 0)
902 GEN_INT_ARITH_ADD(addeo, 0x14, 1, 1, 1)
903 /* addme  addme.  addmeo  addmeo.  */
904 GEN_INT_ARITH_ADD_CONST(addme, 0x07, -1LL, 1, 1, 0)
905 GEN_INT_ARITH_ADD_CONST(addmeo, 0x17, -1LL, 1, 1, 1)
906 /* addze  addze.  addzeo  addzeo.*/
907 GEN_INT_ARITH_ADD_CONST(addze, 0x06, 0, 1, 1, 0)
908 GEN_INT_ARITH_ADD_CONST(addzeo, 0x16, 0, 1, 1, 1)
909 /* addi */
910 static void gen_addi(DisasContext *ctx)
911 {
912     target_long simm = SIMM(ctx->opcode);
913
914     if (rA(ctx->opcode) == 0) {
915         /* li case */
916         tcg_gen_movi_tl(cpu_gpr[rD(ctx->opcode)], simm);
917     } else {
918         tcg_gen_addi_tl(cpu_gpr[rD(ctx->opcode)],
919                         cpu_gpr[rA(ctx->opcode)], simm);
920     }
921 }
922 /* addic  addic.*/
923 static inline void gen_op_addic(DisasContext *ctx, bool compute_rc0)
924 {
925     TCGv c = tcg_const_tl(SIMM(ctx->opcode));
926     gen_op_arith_add(ctx, cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)],
927                      c, 0, 1, 0, compute_rc0);
928     tcg_temp_free(c);
929 }
930
931 static void gen_addic(DisasContext *ctx)
932 {
933     gen_op_addic(ctx, 0);
934 }
935
936 static void gen_addic_(DisasContext *ctx)
937 {
938     gen_op_addic(ctx, 1);
939 }
940
941 /* addis */
942 static void gen_addis(DisasContext *ctx)
943 {
944     target_long simm = SIMM(ctx->opcode);
945
946     if (rA(ctx->opcode) == 0) {
947         /* lis case */
948         tcg_gen_movi_tl(cpu_gpr[rD(ctx->opcode)], simm << 16);
949     } else {
950         tcg_gen_addi_tl(cpu_gpr[rD(ctx->opcode)],
951                         cpu_gpr[rA(ctx->opcode)], simm << 16);
952     }
953 }
954
955 static inline void gen_op_arith_divw(DisasContext *ctx, TCGv ret, TCGv arg1,
956                                      TCGv arg2, int sign, int compute_ov)
957 {
958     int l1 = gen_new_label();
959     int l2 = gen_new_label();
960     TCGv_i32 t0 = tcg_temp_local_new_i32();
961     TCGv_i32 t1 = tcg_temp_local_new_i32();
962
963     tcg_gen_trunc_tl_i32(t0, arg1);
964     tcg_gen_trunc_tl_i32(t1, arg2);
965     tcg_gen_brcondi_i32(TCG_COND_EQ, t1, 0, l1);
966     if (sign) {
967         int l3 = gen_new_label();
968         tcg_gen_brcondi_i32(TCG_COND_NE, t1, -1, l3);
969         tcg_gen_brcondi_i32(TCG_COND_EQ, t0, INT32_MIN, l1);
970         gen_set_label(l3);
971         tcg_gen_div_i32(t0, t0, t1);
972     } else {
973         tcg_gen_divu_i32(t0, t0, t1);
974     }
975     if (compute_ov) {
976         tcg_gen_movi_tl(cpu_ov, 0);
977     }
978     tcg_gen_br(l2);
979     gen_set_label(l1);
980     if (sign) {
981         tcg_gen_sari_i32(t0, t0, 31);
982     } else {
983         tcg_gen_movi_i32(t0, 0);
984     }
985     if (compute_ov) {
986         tcg_gen_movi_tl(cpu_ov, 1);
987         tcg_gen_movi_tl(cpu_so, 1);
988     }
989     gen_set_label(l2);
990     tcg_gen_extu_i32_tl(ret, t0);
991     tcg_temp_free_i32(t0);
992     tcg_temp_free_i32(t1);
993     if (unlikely(Rc(ctx->opcode) != 0))
994         gen_set_Rc0(ctx, ret);
995 }
996 /* Div functions */
997 #define GEN_INT_ARITH_DIVW(name, opc3, sign, compute_ov)                      \
998 static void glue(gen_, name)(DisasContext *ctx)                                       \
999 {                                                                             \
1000     gen_op_arith_divw(ctx, cpu_gpr[rD(ctx->opcode)],                          \
1001                      cpu_gpr[rA(ctx->opcode)], cpu_gpr[rB(ctx->opcode)],      \
1002                      sign, compute_ov);                                       \
1003 }
1004 /* divwu  divwu.  divwuo  divwuo.   */
1005 GEN_INT_ARITH_DIVW(divwu, 0x0E, 0, 0);
1006 GEN_INT_ARITH_DIVW(divwuo, 0x1E, 0, 1);
1007 /* divw  divw.  divwo  divwo.   */
1008 GEN_INT_ARITH_DIVW(divw, 0x0F, 1, 0);
1009 GEN_INT_ARITH_DIVW(divwo, 0x1F, 1, 1);
1010
1011 /* div[wd]eu[o][.] */
1012 #define GEN_DIVE(name, hlpr, compute_ov)                                      \
1013 static void gen_##name(DisasContext *ctx)                                     \
1014 {                                                                             \
1015     TCGv_i32 t0 = tcg_const_i32(compute_ov);                                  \
1016     gen_helper_##hlpr(cpu_gpr[rD(ctx->opcode)], cpu_env,                      \
1017                      cpu_gpr[rA(ctx->opcode)], cpu_gpr[rB(ctx->opcode)], t0); \
1018     tcg_temp_free_i32(t0);                                                    \
1019     if (unlikely(Rc(ctx->opcode) != 0)) {                                     \
1020         gen_set_Rc0(ctx, cpu_gpr[rD(ctx->opcode)]);                           \
1021     }                                                                         \
1022 }
1023
1024 GEN_DIVE(divweu, divweu, 0);
1025 GEN_DIVE(divweuo, divweu, 1);
1026 GEN_DIVE(divwe, divwe, 0);
1027 GEN_DIVE(divweo, divwe, 1);
1028
1029 #if defined(TARGET_PPC64)
1030 static inline void gen_op_arith_divd(DisasContext *ctx, TCGv ret, TCGv arg1,
1031                                      TCGv arg2, int sign, int compute_ov)
1032 {
1033     int l1 = gen_new_label();
1034     int l2 = gen_new_label();
1035
1036     tcg_gen_brcondi_i64(TCG_COND_EQ, arg2, 0, l1);
1037     if (sign) {
1038         int l3 = gen_new_label();
1039         tcg_gen_brcondi_i64(TCG_COND_NE, arg2, -1, l3);
1040         tcg_gen_brcondi_i64(TCG_COND_EQ, arg1, INT64_MIN, l1);
1041         gen_set_label(l3);
1042         tcg_gen_div_i64(ret, arg1, arg2);
1043     } else {
1044         tcg_gen_divu_i64(ret, arg1, arg2);
1045     }
1046     if (compute_ov) {
1047         tcg_gen_movi_tl(cpu_ov, 0);
1048     }
1049     tcg_gen_br(l2);
1050     gen_set_label(l1);
1051     if (sign) {
1052         tcg_gen_sari_i64(ret, arg1, 63);
1053     } else {
1054         tcg_gen_movi_i64(ret, 0);
1055     }
1056     if (compute_ov) {
1057         tcg_gen_movi_tl(cpu_ov, 1);
1058         tcg_gen_movi_tl(cpu_so, 1);
1059     }
1060     gen_set_label(l2);
1061     if (unlikely(Rc(ctx->opcode) != 0))
1062         gen_set_Rc0(ctx, ret);
1063 }
1064 #define GEN_INT_ARITH_DIVD(name, opc3, sign, compute_ov)                      \
1065 static void glue(gen_, name)(DisasContext *ctx)                                       \
1066 {                                                                             \
1067     gen_op_arith_divd(ctx, cpu_gpr[rD(ctx->opcode)],                          \
1068                       cpu_gpr[rA(ctx->opcode)], cpu_gpr[rB(ctx->opcode)],     \
1069                       sign, compute_ov);                                      \
1070 }
1071 /* divwu  divwu.  divwuo  divwuo.   */
1072 GEN_INT_ARITH_DIVD(divdu, 0x0E, 0, 0);
1073 GEN_INT_ARITH_DIVD(divduo, 0x1E, 0, 1);
1074 /* divw  divw.  divwo  divwo.   */
1075 GEN_INT_ARITH_DIVD(divd, 0x0F, 1, 0);
1076 GEN_INT_ARITH_DIVD(divdo, 0x1F, 1, 1);
1077
1078 GEN_DIVE(divdeu, divdeu, 0);
1079 GEN_DIVE(divdeuo, divdeu, 1);
1080 GEN_DIVE(divde, divde, 0);
1081 GEN_DIVE(divdeo, divde, 1);
1082 #endif
1083
1084 /* mulhw  mulhw. */
1085 static void gen_mulhw(DisasContext *ctx)
1086 {
1087     TCGv_i32 t0 = tcg_temp_new_i32();
1088     TCGv_i32 t1 = tcg_temp_new_i32();
1089
1090     tcg_gen_trunc_tl_i32(t0, cpu_gpr[rA(ctx->opcode)]);
1091     tcg_gen_trunc_tl_i32(t1, cpu_gpr[rB(ctx->opcode)]);
1092     tcg_gen_muls2_i32(t0, t1, t0, t1);
1093     tcg_gen_extu_i32_tl(cpu_gpr[rD(ctx->opcode)], t1);
1094     tcg_temp_free_i32(t0);
1095     tcg_temp_free_i32(t1);
1096     if (unlikely(Rc(ctx->opcode) != 0))
1097         gen_set_Rc0(ctx, cpu_gpr[rD(ctx->opcode)]);
1098 }
1099
1100 /* mulhwu  mulhwu.  */
1101 static void gen_mulhwu(DisasContext *ctx)
1102 {
1103     TCGv_i32 t0 = tcg_temp_new_i32();
1104     TCGv_i32 t1 = tcg_temp_new_i32();
1105
1106     tcg_gen_trunc_tl_i32(t0, cpu_gpr[rA(ctx->opcode)]);
1107     tcg_gen_trunc_tl_i32(t1, cpu_gpr[rB(ctx->opcode)]);
1108     tcg_gen_mulu2_i32(t0, t1, t0, t1);
1109     tcg_gen_extu_i32_tl(cpu_gpr[rD(ctx->opcode)], t1);
1110     tcg_temp_free_i32(t0);
1111     tcg_temp_free_i32(t1);
1112     if (unlikely(Rc(ctx->opcode) != 0))
1113         gen_set_Rc0(ctx, cpu_gpr[rD(ctx->opcode)]);
1114 }
1115
1116 /* mullw  mullw. */
1117 static void gen_mullw(DisasContext *ctx)
1118 {
1119     tcg_gen_mul_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)],
1120                    cpu_gpr[rB(ctx->opcode)]);
1121     tcg_gen_ext32s_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rD(ctx->opcode)]);
1122     if (unlikely(Rc(ctx->opcode) != 0))
1123         gen_set_Rc0(ctx, cpu_gpr[rD(ctx->opcode)]);
1124 }
1125
1126 /* mullwo  mullwo. */
1127 static void gen_mullwo(DisasContext *ctx)
1128 {
1129     TCGv_i32 t0 = tcg_temp_new_i32();
1130     TCGv_i32 t1 = tcg_temp_new_i32();
1131
1132     tcg_gen_trunc_tl_i32(t0, cpu_gpr[rA(ctx->opcode)]);
1133     tcg_gen_trunc_tl_i32(t1, cpu_gpr[rB(ctx->opcode)]);
1134     tcg_gen_muls2_i32(t0, t1, t0, t1);
1135     tcg_gen_ext_i32_tl(cpu_gpr[rD(ctx->opcode)], t0);
1136
1137     tcg_gen_sari_i32(t0, t0, 31);
1138     tcg_gen_setcond_i32(TCG_COND_NE, t0, t0, t1);
1139     tcg_gen_extu_i32_tl(cpu_ov, t0);
1140     tcg_gen_or_tl(cpu_so, cpu_so, cpu_ov);
1141
1142     tcg_temp_free_i32(t0);
1143     tcg_temp_free_i32(t1);
1144     if (unlikely(Rc(ctx->opcode) != 0))
1145         gen_set_Rc0(ctx, cpu_gpr[rD(ctx->opcode)]);
1146 }
1147
1148 /* mulli */
1149 static void gen_mulli(DisasContext *ctx)
1150 {
1151     tcg_gen_muli_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)],
1152                     SIMM(ctx->opcode));
1153 }
1154
1155 #if defined(TARGET_PPC64)
1156 /* mulhd  mulhd. */
1157 static void gen_mulhd(DisasContext *ctx)
1158 {
1159     TCGv lo = tcg_temp_new();
1160     tcg_gen_muls2_tl(lo, cpu_gpr[rD(ctx->opcode)],
1161                      cpu_gpr[rA(ctx->opcode)], cpu_gpr[rB(ctx->opcode)]);
1162     tcg_temp_free(lo);
1163     if (unlikely(Rc(ctx->opcode) != 0)) {
1164         gen_set_Rc0(ctx, cpu_gpr[rD(ctx->opcode)]);
1165     }
1166 }
1167
1168 /* mulhdu  mulhdu. */
1169 static void gen_mulhdu(DisasContext *ctx)
1170 {
1171     TCGv lo = tcg_temp_new();
1172     tcg_gen_mulu2_tl(lo, cpu_gpr[rD(ctx->opcode)],
1173                      cpu_gpr[rA(ctx->opcode)], cpu_gpr[rB(ctx->opcode)]);
1174     tcg_temp_free(lo);
1175     if (unlikely(Rc(ctx->opcode) != 0)) {
1176         gen_set_Rc0(ctx, cpu_gpr[rD(ctx->opcode)]);
1177     }
1178 }
1179
1180 /* mulld  mulld. */
1181 static void gen_mulld(DisasContext *ctx)
1182 {
1183     tcg_gen_mul_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)],
1184                    cpu_gpr[rB(ctx->opcode)]);
1185     if (unlikely(Rc(ctx->opcode) != 0))
1186         gen_set_Rc0(ctx, cpu_gpr[rD(ctx->opcode)]);
1187 }
1188
1189 /* mulldo  mulldo. */
1190 static void gen_mulldo(DisasContext *ctx)
1191 {
1192     gen_helper_mulldo(cpu_gpr[rD(ctx->opcode)], cpu_env,
1193                       cpu_gpr[rA(ctx->opcode)], cpu_gpr[rB(ctx->opcode)]);
1194     if (unlikely(Rc(ctx->opcode) != 0)) {
1195         gen_set_Rc0(ctx, cpu_gpr[rD(ctx->opcode)]);
1196     }
1197 }
1198 #endif
1199
1200 /* Common subf function */
1201 static inline void gen_op_arith_subf(DisasContext *ctx, TCGv ret, TCGv arg1,
1202                                      TCGv arg2, bool add_ca, bool compute_ca,
1203                                      bool compute_ov, bool compute_rc0)
1204 {
1205     TCGv t0 = ret;
1206
1207     if (compute_ca || compute_ov) {
1208         t0 = tcg_temp_new();
1209     }
1210
1211     if (compute_ca) {
1212         /* dest = ~arg1 + arg2 [+ ca].  */
1213         if (NARROW_MODE(ctx)) {
1214             /* Caution: a non-obvious corner case of the spec is that we
1215                must produce the *entire* 64-bit addition, but produce the
1216                carry into bit 32.  */
1217             TCGv inv1 = tcg_temp_new();
1218             TCGv t1 = tcg_temp_new();
1219             tcg_gen_not_tl(inv1, arg1);
1220             if (add_ca) {
1221                 tcg_gen_add_tl(t0, arg2, cpu_ca);
1222             } else {
1223                 tcg_gen_addi_tl(t0, arg2, 1);
1224             }
1225             tcg_gen_xor_tl(t1, arg2, inv1);         /* add without carry */
1226             tcg_gen_add_tl(t0, t0, inv1);
1227             tcg_temp_free(inv1);
1228             tcg_gen_xor_tl(cpu_ca, t0, t1);         /* bits changes w/ carry */
1229             tcg_temp_free(t1);
1230             tcg_gen_shri_tl(cpu_ca, cpu_ca, 32);    /* extract bit 32 */
1231             tcg_gen_andi_tl(cpu_ca, cpu_ca, 1);
1232         } else if (add_ca) {
1233             TCGv zero, inv1 = tcg_temp_new();
1234             tcg_gen_not_tl(inv1, arg1);
1235             zero = tcg_const_tl(0);
1236             tcg_gen_add2_tl(t0, cpu_ca, arg2, zero, cpu_ca, zero);
1237             tcg_gen_add2_tl(t0, cpu_ca, t0, cpu_ca, inv1, zero);
1238             tcg_temp_free(zero);
1239             tcg_temp_free(inv1);
1240         } else {
1241             tcg_gen_setcond_tl(TCG_COND_GEU, cpu_ca, arg2, arg1);
1242             tcg_gen_sub_tl(t0, arg2, arg1);
1243         }
1244     } else if (add_ca) {
1245         /* Since we're ignoring carry-out, we can simplify the
1246            standard ~arg1 + arg2 + ca to arg2 - arg1 + ca - 1.  */
1247         tcg_gen_sub_tl(t0, arg2, arg1);
1248         tcg_gen_add_tl(t0, t0, cpu_ca);
1249         tcg_gen_subi_tl(t0, t0, 1);
1250     } else {
1251         tcg_gen_sub_tl(t0, arg2, arg1);
1252     }
1253
1254     if (compute_ov) {
1255         gen_op_arith_compute_ov(ctx, t0, arg1, arg2, 1);
1256     }
1257     if (unlikely(compute_rc0)) {
1258         gen_set_Rc0(ctx, t0);
1259     }
1260
1261     if (!TCGV_EQUAL(t0, ret)) {
1262         tcg_gen_mov_tl(ret, t0);
1263         tcg_temp_free(t0);
1264     }
1265 }
1266 /* Sub functions with Two operands functions */
1267 #define GEN_INT_ARITH_SUBF(name, opc3, add_ca, compute_ca, compute_ov)        \
1268 static void glue(gen_, name)(DisasContext *ctx)                               \
1269 {                                                                             \
1270     gen_op_arith_subf(ctx, cpu_gpr[rD(ctx->opcode)],                          \
1271                       cpu_gpr[rA(ctx->opcode)], cpu_gpr[rB(ctx->opcode)],     \
1272                       add_ca, compute_ca, compute_ov, Rc(ctx->opcode));       \
1273 }
1274 /* Sub functions with one operand and one immediate */
1275 #define GEN_INT_ARITH_SUBF_CONST(name, opc3, const_val,                       \
1276                                 add_ca, compute_ca, compute_ov)               \
1277 static void glue(gen_, name)(DisasContext *ctx)                               \
1278 {                                                                             \
1279     TCGv t0 = tcg_const_tl(const_val);                                        \
1280     gen_op_arith_subf(ctx, cpu_gpr[rD(ctx->opcode)],                          \
1281                       cpu_gpr[rA(ctx->opcode)], t0,                           \
1282                       add_ca, compute_ca, compute_ov, Rc(ctx->opcode));       \
1283     tcg_temp_free(t0);                                                        \
1284 }
1285 /* subf  subf.  subfo  subfo. */
1286 GEN_INT_ARITH_SUBF(subf, 0x01, 0, 0, 0)
1287 GEN_INT_ARITH_SUBF(subfo, 0x11, 0, 0, 1)
1288 /* subfc  subfc.  subfco  subfco. */
1289 GEN_INT_ARITH_SUBF(subfc, 0x00, 0, 1, 0)
1290 GEN_INT_ARITH_SUBF(subfco, 0x10, 0, 1, 1)
1291 /* subfe  subfe.  subfeo  subfo. */
1292 GEN_INT_ARITH_SUBF(subfe, 0x04, 1, 1, 0)
1293 GEN_INT_ARITH_SUBF(subfeo, 0x14, 1, 1, 1)
1294 /* subfme  subfme.  subfmeo  subfmeo.  */
1295 GEN_INT_ARITH_SUBF_CONST(subfme, 0x07, -1LL, 1, 1, 0)
1296 GEN_INT_ARITH_SUBF_CONST(subfmeo, 0x17, -1LL, 1, 1, 1)
1297 /* subfze  subfze.  subfzeo  subfzeo.*/
1298 GEN_INT_ARITH_SUBF_CONST(subfze, 0x06, 0, 1, 1, 0)
1299 GEN_INT_ARITH_SUBF_CONST(subfzeo, 0x16, 0, 1, 1, 1)
1300
1301 /* subfic */
1302 static void gen_subfic(DisasContext *ctx)
1303 {
1304     TCGv c = tcg_const_tl(SIMM(ctx->opcode));
1305     gen_op_arith_subf(ctx, cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)],
1306                       c, 0, 1, 0, 0);
1307     tcg_temp_free(c);
1308 }
1309
1310 /* neg neg. nego nego. */
1311 static inline void gen_op_arith_neg(DisasContext *ctx, bool compute_ov)
1312 {
1313     TCGv zero = tcg_const_tl(0);
1314     gen_op_arith_subf(ctx, cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)],
1315                       zero, 0, 0, compute_ov, Rc(ctx->opcode));
1316     tcg_temp_free(zero);
1317 }
1318
1319 static void gen_neg(DisasContext *ctx)
1320 {
1321     gen_op_arith_neg(ctx, 0);
1322 }
1323
1324 static void gen_nego(DisasContext *ctx)
1325 {
1326     gen_op_arith_neg(ctx, 1);
1327 }
1328
1329 /***                            Integer logical                            ***/
1330 #define GEN_LOGICAL2(name, tcg_op, opc, type)                                 \
1331 static void glue(gen_, name)(DisasContext *ctx)                                       \
1332 {                                                                             \
1333     tcg_op(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)],                \
1334        cpu_gpr[rB(ctx->opcode)]);                                             \
1335     if (unlikely(Rc(ctx->opcode) != 0))                                       \
1336         gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);                           \
1337 }
1338
1339 #define GEN_LOGICAL1(name, tcg_op, opc, type)                                 \
1340 static void glue(gen_, name)(DisasContext *ctx)                                       \
1341 {                                                                             \
1342     tcg_op(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)]);               \
1343     if (unlikely(Rc(ctx->opcode) != 0))                                       \
1344         gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);                           \
1345 }
1346
1347 /* and & and. */
1348 GEN_LOGICAL2(and, tcg_gen_and_tl, 0x00, PPC_INTEGER);
1349 /* andc & andc. */
1350 GEN_LOGICAL2(andc, tcg_gen_andc_tl, 0x01, PPC_INTEGER);
1351
1352 /* andi. */
1353 static void gen_andi_(DisasContext *ctx)
1354 {
1355     tcg_gen_andi_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)], UIMM(ctx->opcode));
1356     gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
1357 }
1358
1359 /* andis. */
1360 static void gen_andis_(DisasContext *ctx)
1361 {
1362     tcg_gen_andi_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)], UIMM(ctx->opcode) << 16);
1363     gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
1364 }
1365
1366 /* cntlzw */
1367 static void gen_cntlzw(DisasContext *ctx)
1368 {
1369     gen_helper_cntlzw(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)]);
1370     if (unlikely(Rc(ctx->opcode) != 0))
1371         gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
1372 }
1373 /* eqv & eqv. */
1374 GEN_LOGICAL2(eqv, tcg_gen_eqv_tl, 0x08, PPC_INTEGER);
1375 /* extsb & extsb. */
1376 GEN_LOGICAL1(extsb, tcg_gen_ext8s_tl, 0x1D, PPC_INTEGER);
1377 /* extsh & extsh. */
1378 GEN_LOGICAL1(extsh, tcg_gen_ext16s_tl, 0x1C, PPC_INTEGER);
1379 /* nand & nand. */
1380 GEN_LOGICAL2(nand, tcg_gen_nand_tl, 0x0E, PPC_INTEGER);
1381 /* nor & nor. */
1382 GEN_LOGICAL2(nor, tcg_gen_nor_tl, 0x03, PPC_INTEGER);
1383
1384 /* or & or. */
1385 static void gen_or(DisasContext *ctx)
1386 {
1387     int rs, ra, rb;
1388
1389     rs = rS(ctx->opcode);
1390     ra = rA(ctx->opcode);
1391     rb = rB(ctx->opcode);
1392     /* Optimisation for mr. ri case */
1393     if (rs != ra || rs != rb) {
1394         if (rs != rb)
1395             tcg_gen_or_tl(cpu_gpr[ra], cpu_gpr[rs], cpu_gpr[rb]);
1396         else
1397             tcg_gen_mov_tl(cpu_gpr[ra], cpu_gpr[rs]);
1398         if (unlikely(Rc(ctx->opcode) != 0))
1399             gen_set_Rc0(ctx, cpu_gpr[ra]);
1400     } else if (unlikely(Rc(ctx->opcode) != 0)) {
1401         gen_set_Rc0(ctx, cpu_gpr[rs]);
1402 #if defined(TARGET_PPC64)
1403     } else {
1404         int prio = 0;
1405
1406         switch (rs) {
1407         case 1:
1408             /* Set process priority to low */
1409             prio = 2;
1410             break;
1411         case 6:
1412             /* Set process priority to medium-low */
1413             prio = 3;
1414             break;
1415         case 2:
1416             /* Set process priority to normal */
1417             prio = 4;
1418             break;
1419 #if !defined(CONFIG_USER_ONLY)
1420         case 31:
1421             if (ctx->mem_idx > 0) {
1422                 /* Set process priority to very low */
1423                 prio = 1;
1424             }
1425             break;
1426         case 5:
1427             if (ctx->mem_idx > 0) {
1428                 /* Set process priority to medium-hight */
1429                 prio = 5;
1430             }
1431             break;
1432         case 3:
1433             if (ctx->mem_idx > 0) {
1434                 /* Set process priority to high */
1435                 prio = 6;
1436             }
1437             break;
1438         case 7:
1439             if (ctx->mem_idx > 1) {
1440                 /* Set process priority to very high */
1441                 prio = 7;
1442             }
1443             break;
1444 #endif
1445         default:
1446             /* nop */
1447             break;
1448         }
1449         if (prio) {
1450             TCGv t0 = tcg_temp_new();
1451             gen_load_spr(t0, SPR_PPR);
1452             tcg_gen_andi_tl(t0, t0, ~0x001C000000000000ULL);
1453             tcg_gen_ori_tl(t0, t0, ((uint64_t)prio) << 50);
1454             gen_store_spr(SPR_PPR, t0);
1455             tcg_temp_free(t0);
1456         }
1457 #endif
1458     }
1459 }
1460 /* orc & orc. */
1461 GEN_LOGICAL2(orc, tcg_gen_orc_tl, 0x0C, PPC_INTEGER);
1462
1463 /* xor & xor. */
1464 static void gen_xor(DisasContext *ctx)
1465 {
1466     /* Optimisation for "set to zero" case */
1467     if (rS(ctx->opcode) != rB(ctx->opcode))
1468         tcg_gen_xor_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)], cpu_gpr[rB(ctx->opcode)]);
1469     else
1470         tcg_gen_movi_tl(cpu_gpr[rA(ctx->opcode)], 0);
1471     if (unlikely(Rc(ctx->opcode) != 0))
1472         gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
1473 }
1474
1475 /* ori */
1476 static void gen_ori(DisasContext *ctx)
1477 {
1478     target_ulong uimm = UIMM(ctx->opcode);
1479
1480     if (rS(ctx->opcode) == rA(ctx->opcode) && uimm == 0) {
1481         /* NOP */
1482         /* XXX: should handle special NOPs for POWER series */
1483         return;
1484     }
1485     tcg_gen_ori_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)], uimm);
1486 }
1487
1488 /* oris */
1489 static void gen_oris(DisasContext *ctx)
1490 {
1491     target_ulong uimm = UIMM(ctx->opcode);
1492
1493     if (rS(ctx->opcode) == rA(ctx->opcode) && uimm == 0) {
1494         /* NOP */
1495         return;
1496     }
1497     tcg_gen_ori_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)], uimm << 16);
1498 }
1499
1500 /* xori */
1501 static void gen_xori(DisasContext *ctx)
1502 {
1503     target_ulong uimm = UIMM(ctx->opcode);
1504
1505     if (rS(ctx->opcode) == rA(ctx->opcode) && uimm == 0) {
1506         /* NOP */
1507         return;
1508     }
1509     tcg_gen_xori_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)], uimm);
1510 }
1511
1512 /* xoris */
1513 static void gen_xoris(DisasContext *ctx)
1514 {
1515     target_ulong uimm = UIMM(ctx->opcode);
1516
1517     if (rS(ctx->opcode) == rA(ctx->opcode) && uimm == 0) {
1518         /* NOP */
1519         return;
1520     }
1521     tcg_gen_xori_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)], uimm << 16);
1522 }
1523
1524 /* popcntb : PowerPC 2.03 specification */
1525 static void gen_popcntb(DisasContext *ctx)
1526 {
1527     gen_helper_popcntb(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)]);
1528 }
1529
1530 static void gen_popcntw(DisasContext *ctx)
1531 {
1532     gen_helper_popcntw(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)]);
1533 }
1534
1535 #if defined(TARGET_PPC64)
1536 /* popcntd: PowerPC 2.06 specification */
1537 static void gen_popcntd(DisasContext *ctx)
1538 {
1539     gen_helper_popcntd(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)]);
1540 }
1541 #endif
1542
1543 /* prtyw: PowerPC 2.05 specification */
1544 static void gen_prtyw(DisasContext *ctx)
1545 {
1546     TCGv ra = cpu_gpr[rA(ctx->opcode)];
1547     TCGv rs = cpu_gpr[rS(ctx->opcode)];
1548     TCGv t0 = tcg_temp_new();
1549     tcg_gen_shri_tl(t0, rs, 16);
1550     tcg_gen_xor_tl(ra, rs, t0);
1551     tcg_gen_shri_tl(t0, ra, 8);
1552     tcg_gen_xor_tl(ra, ra, t0);
1553     tcg_gen_andi_tl(ra, ra, (target_ulong)0x100000001ULL);
1554     tcg_temp_free(t0);
1555 }
1556
1557 #if defined(TARGET_PPC64)
1558 /* prtyd: PowerPC 2.05 specification */
1559 static void gen_prtyd(DisasContext *ctx)
1560 {
1561     TCGv ra = cpu_gpr[rA(ctx->opcode)];
1562     TCGv rs = cpu_gpr[rS(ctx->opcode)];
1563     TCGv t0 = tcg_temp_new();
1564     tcg_gen_shri_tl(t0, rs, 32);
1565     tcg_gen_xor_tl(ra, rs, t0);
1566     tcg_gen_shri_tl(t0, ra, 16);
1567     tcg_gen_xor_tl(ra, ra, t0);
1568     tcg_gen_shri_tl(t0, ra, 8);
1569     tcg_gen_xor_tl(ra, ra, t0);
1570     tcg_gen_andi_tl(ra, ra, 1);
1571     tcg_temp_free(t0);
1572 }
1573 #endif
1574
1575 #if defined(TARGET_PPC64)
1576 /* bpermd */
1577 static void gen_bpermd(DisasContext *ctx)
1578 {
1579     gen_helper_bpermd(cpu_gpr[rA(ctx->opcode)],
1580                       cpu_gpr[rS(ctx->opcode)], cpu_gpr[rB(ctx->opcode)]);
1581 }
1582 #endif
1583
1584 #if defined(TARGET_PPC64)
1585 /* extsw & extsw. */
1586 GEN_LOGICAL1(extsw, tcg_gen_ext32s_tl, 0x1E, PPC_64B);
1587
1588 /* cntlzd */
1589 static void gen_cntlzd(DisasContext *ctx)
1590 {
1591     gen_helper_cntlzd(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)]);
1592     if (unlikely(Rc(ctx->opcode) != 0))
1593         gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
1594 }
1595 #endif
1596
1597 /***                             Integer rotate                            ***/
1598
1599 /* rlwimi & rlwimi. */
1600 static void gen_rlwimi(DisasContext *ctx)
1601 {
1602     uint32_t mb, me, sh;
1603
1604     mb = MB(ctx->opcode);
1605     me = ME(ctx->opcode);
1606     sh = SH(ctx->opcode);
1607     if (likely(sh == 0 && mb == 0 && me == 31)) {
1608         tcg_gen_ext32u_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)]);
1609     } else {
1610         target_ulong mask;
1611         TCGv t1;
1612         TCGv t0 = tcg_temp_new();
1613 #if defined(TARGET_PPC64)
1614         TCGv_i32 t2 = tcg_temp_new_i32();
1615         tcg_gen_trunc_i64_i32(t2, cpu_gpr[rS(ctx->opcode)]);
1616         tcg_gen_rotli_i32(t2, t2, sh);
1617         tcg_gen_extu_i32_i64(t0, t2);
1618         tcg_temp_free_i32(t2);
1619 #else
1620         tcg_gen_rotli_i32(t0, cpu_gpr[rS(ctx->opcode)], sh);
1621 #endif
1622 #if defined(TARGET_PPC64)
1623         mb += 32;
1624         me += 32;
1625 #endif
1626         mask = MASK(mb, me);
1627         t1 = tcg_temp_new();
1628         tcg_gen_andi_tl(t0, t0, mask);
1629         tcg_gen_andi_tl(t1, cpu_gpr[rA(ctx->opcode)], ~mask);
1630         tcg_gen_or_tl(cpu_gpr[rA(ctx->opcode)], t0, t1);
1631         tcg_temp_free(t0);
1632         tcg_temp_free(t1);
1633     }
1634     if (unlikely(Rc(ctx->opcode) != 0))
1635         gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
1636 }
1637
1638 /* rlwinm & rlwinm. */
1639 static void gen_rlwinm(DisasContext *ctx)
1640 {
1641     uint32_t mb, me, sh;
1642
1643     sh = SH(ctx->opcode);
1644     mb = MB(ctx->opcode);
1645     me = ME(ctx->opcode);
1646
1647     if (likely(mb == 0 && me == (31 - sh))) {
1648         if (likely(sh == 0)) {
1649             tcg_gen_ext32u_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)]);
1650         } else {
1651             TCGv t0 = tcg_temp_new();
1652             tcg_gen_ext32u_tl(t0, cpu_gpr[rS(ctx->opcode)]);
1653             tcg_gen_shli_tl(t0, t0, sh);
1654             tcg_gen_ext32u_tl(cpu_gpr[rA(ctx->opcode)], t0);
1655             tcg_temp_free(t0);
1656         }
1657     } else if (likely(sh != 0 && me == 31 && sh == (32 - mb))) {
1658         TCGv t0 = tcg_temp_new();
1659         tcg_gen_ext32u_tl(t0, cpu_gpr[rS(ctx->opcode)]);
1660         tcg_gen_shri_tl(t0, t0, mb);
1661         tcg_gen_ext32u_tl(cpu_gpr[rA(ctx->opcode)], t0);
1662         tcg_temp_free(t0);
1663     } else {
1664         TCGv t0 = tcg_temp_new();
1665 #if defined(TARGET_PPC64)
1666         TCGv_i32 t1 = tcg_temp_new_i32();
1667         tcg_gen_trunc_i64_i32(t1, cpu_gpr[rS(ctx->opcode)]);
1668         tcg_gen_rotli_i32(t1, t1, sh);
1669         tcg_gen_extu_i32_i64(t0, t1);
1670         tcg_temp_free_i32(t1);
1671 #else
1672         tcg_gen_rotli_i32(t0, cpu_gpr[rS(ctx->opcode)], sh);
1673 #endif
1674 #if defined(TARGET_PPC64)
1675         mb += 32;
1676         me += 32;
1677 #endif
1678         tcg_gen_andi_tl(cpu_gpr[rA(ctx->opcode)], t0, MASK(mb, me));
1679         tcg_temp_free(t0);
1680     }
1681     if (unlikely(Rc(ctx->opcode) != 0))
1682         gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
1683 }
1684
1685 /* rlwnm & rlwnm. */
1686 static void gen_rlwnm(DisasContext *ctx)
1687 {
1688     uint32_t mb, me;
1689     TCGv t0;
1690 #if defined(TARGET_PPC64)
1691     TCGv_i32 t1, t2;
1692 #endif
1693
1694     mb = MB(ctx->opcode);
1695     me = ME(ctx->opcode);
1696     t0 = tcg_temp_new();
1697     tcg_gen_andi_tl(t0, cpu_gpr[rB(ctx->opcode)], 0x1f);
1698 #if defined(TARGET_PPC64)
1699     t1 = tcg_temp_new_i32();
1700     t2 = tcg_temp_new_i32();
1701     tcg_gen_trunc_i64_i32(t1, cpu_gpr[rS(ctx->opcode)]);
1702     tcg_gen_trunc_i64_i32(t2, t0);
1703     tcg_gen_rotl_i32(t1, t1, t2);
1704     tcg_gen_extu_i32_i64(t0, t1);
1705     tcg_temp_free_i32(t1);
1706     tcg_temp_free_i32(t2);
1707 #else
1708     tcg_gen_rotl_i32(t0, cpu_gpr[rS(ctx->opcode)], t0);
1709 #endif
1710     if (unlikely(mb != 0 || me != 31)) {
1711 #if defined(TARGET_PPC64)
1712         mb += 32;
1713         me += 32;
1714 #endif
1715         tcg_gen_andi_tl(cpu_gpr[rA(ctx->opcode)], t0, MASK(mb, me));
1716     } else {
1717         tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], t0);
1718     }
1719     tcg_temp_free(t0);
1720     if (unlikely(Rc(ctx->opcode) != 0))
1721         gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
1722 }
1723
1724 #if defined(TARGET_PPC64)
1725 #define GEN_PPC64_R2(name, opc1, opc2)                                        \
1726 static void glue(gen_, name##0)(DisasContext *ctx)                            \
1727 {                                                                             \
1728     gen_##name(ctx, 0);                                                       \
1729 }                                                                             \
1730                                                                               \
1731 static void glue(gen_, name##1)(DisasContext *ctx)                            \
1732 {                                                                             \
1733     gen_##name(ctx, 1);                                                       \
1734 }
1735 #define GEN_PPC64_R4(name, opc1, opc2)                                        \
1736 static void glue(gen_, name##0)(DisasContext *ctx)                            \
1737 {                                                                             \
1738     gen_##name(ctx, 0, 0);                                                    \
1739 }                                                                             \
1740                                                                               \
1741 static void glue(gen_, name##1)(DisasContext *ctx)                            \
1742 {                                                                             \
1743     gen_##name(ctx, 0, 1);                                                    \
1744 }                                                                             \
1745                                                                               \
1746 static void glue(gen_, name##2)(DisasContext *ctx)                            \
1747 {                                                                             \
1748     gen_##name(ctx, 1, 0);                                                    \
1749 }                                                                             \
1750                                                                               \
1751 static void glue(gen_, name##3)(DisasContext *ctx)                            \
1752 {                                                                             \
1753     gen_##name(ctx, 1, 1);                                                    \
1754 }
1755
1756 static inline void gen_rldinm(DisasContext *ctx, uint32_t mb, uint32_t me,
1757                               uint32_t sh)
1758 {
1759     if (likely(sh != 0 && mb == 0 && me == (63 - sh))) {
1760         tcg_gen_shli_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)], sh);
1761     } else if (likely(sh != 0 && me == 63 && sh == (64 - mb))) {
1762         tcg_gen_shri_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)], mb);
1763     } else {
1764         TCGv t0 = tcg_temp_new();
1765         tcg_gen_rotli_tl(t0, cpu_gpr[rS(ctx->opcode)], sh);
1766         if (likely(mb == 0 && me == 63)) {
1767             tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], t0);
1768         } else {
1769             tcg_gen_andi_tl(cpu_gpr[rA(ctx->opcode)], t0, MASK(mb, me));
1770         }
1771         tcg_temp_free(t0);
1772     }
1773     if (unlikely(Rc(ctx->opcode) != 0))
1774         gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
1775 }
1776 /* rldicl - rldicl. */
1777 static inline void gen_rldicl(DisasContext *ctx, int mbn, int shn)
1778 {
1779     uint32_t sh, mb;
1780
1781     sh = SH(ctx->opcode) | (shn << 5);
1782     mb = MB(ctx->opcode) | (mbn << 5);
1783     gen_rldinm(ctx, mb, 63, sh);
1784 }
1785 GEN_PPC64_R4(rldicl, 0x1E, 0x00);
1786 /* rldicr - rldicr. */
1787 static inline void gen_rldicr(DisasContext *ctx, int men, int shn)
1788 {
1789     uint32_t sh, me;
1790
1791     sh = SH(ctx->opcode) | (shn << 5);
1792     me = MB(ctx->opcode) | (men << 5);
1793     gen_rldinm(ctx, 0, me, sh);
1794 }
1795 GEN_PPC64_R4(rldicr, 0x1E, 0x02);
1796 /* rldic - rldic. */
1797 static inline void gen_rldic(DisasContext *ctx, int mbn, int shn)
1798 {
1799     uint32_t sh, mb;
1800
1801     sh = SH(ctx->opcode) | (shn << 5);
1802     mb = MB(ctx->opcode) | (mbn << 5);
1803     gen_rldinm(ctx, mb, 63 - sh, sh);
1804 }
1805 GEN_PPC64_R4(rldic, 0x1E, 0x04);
1806
1807 static inline void gen_rldnm(DisasContext *ctx, uint32_t mb, uint32_t me)
1808 {
1809     TCGv t0;
1810
1811     t0 = tcg_temp_new();
1812     tcg_gen_andi_tl(t0, cpu_gpr[rB(ctx->opcode)], 0x3f);
1813     tcg_gen_rotl_tl(t0, cpu_gpr[rS(ctx->opcode)], t0);
1814     if (unlikely(mb != 0 || me != 63)) {
1815         tcg_gen_andi_tl(cpu_gpr[rA(ctx->opcode)], t0, MASK(mb, me));
1816     } else {
1817         tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], t0);
1818     }
1819     tcg_temp_free(t0);
1820     if (unlikely(Rc(ctx->opcode) != 0))
1821         gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
1822 }
1823
1824 /* rldcl - rldcl. */
1825 static inline void gen_rldcl(DisasContext *ctx, int mbn)
1826 {
1827     uint32_t mb;
1828
1829     mb = MB(ctx->opcode) | (mbn << 5);
1830     gen_rldnm(ctx, mb, 63);
1831 }
1832 GEN_PPC64_R2(rldcl, 0x1E, 0x08);
1833 /* rldcr - rldcr. */
1834 static inline void gen_rldcr(DisasContext *ctx, int men)
1835 {
1836     uint32_t me;
1837
1838     me = MB(ctx->opcode) | (men << 5);
1839     gen_rldnm(ctx, 0, me);
1840 }
1841 GEN_PPC64_R2(rldcr, 0x1E, 0x09);
1842 /* rldimi - rldimi. */
1843 static inline void gen_rldimi(DisasContext *ctx, int mbn, int shn)
1844 {
1845     uint32_t sh, mb, me;
1846
1847     sh = SH(ctx->opcode) | (shn << 5);
1848     mb = MB(ctx->opcode) | (mbn << 5);
1849     me = 63 - sh;
1850     if (unlikely(sh == 0 && mb == 0)) {
1851         tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)]);
1852     } else {
1853         TCGv t0, t1;
1854         target_ulong mask;
1855
1856         t0 = tcg_temp_new();
1857         tcg_gen_rotli_tl(t0, cpu_gpr[rS(ctx->opcode)], sh);
1858         t1 = tcg_temp_new();
1859         mask = MASK(mb, me);
1860         tcg_gen_andi_tl(t0, t0, mask);
1861         tcg_gen_andi_tl(t1, cpu_gpr[rA(ctx->opcode)], ~mask);
1862         tcg_gen_or_tl(cpu_gpr[rA(ctx->opcode)], t0, t1);
1863         tcg_temp_free(t0);
1864         tcg_temp_free(t1);
1865     }
1866     if (unlikely(Rc(ctx->opcode) != 0))
1867         gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
1868 }
1869 GEN_PPC64_R4(rldimi, 0x1E, 0x06);
1870 #endif
1871
1872 /***                             Integer shift                             ***/
1873
1874 /* slw & slw. */
1875 static void gen_slw(DisasContext *ctx)
1876 {
1877     TCGv t0, t1;
1878
1879     t0 = tcg_temp_new();
1880     /* AND rS with a mask that is 0 when rB >= 0x20 */
1881 #if defined(TARGET_PPC64)
1882     tcg_gen_shli_tl(t0, cpu_gpr[rB(ctx->opcode)], 0x3a);
1883     tcg_gen_sari_tl(t0, t0, 0x3f);
1884 #else
1885     tcg_gen_shli_tl(t0, cpu_gpr[rB(ctx->opcode)], 0x1a);
1886     tcg_gen_sari_tl(t0, t0, 0x1f);
1887 #endif
1888     tcg_gen_andc_tl(t0, cpu_gpr[rS(ctx->opcode)], t0);
1889     t1 = tcg_temp_new();
1890     tcg_gen_andi_tl(t1, cpu_gpr[rB(ctx->opcode)], 0x1f);
1891     tcg_gen_shl_tl(cpu_gpr[rA(ctx->opcode)], t0, t1);
1892     tcg_temp_free(t1);
1893     tcg_temp_free(t0);
1894     tcg_gen_ext32u_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rA(ctx->opcode)]);
1895     if (unlikely(Rc(ctx->opcode) != 0))
1896         gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
1897 }
1898
1899 /* sraw & sraw. */
1900 static void gen_sraw(DisasContext *ctx)
1901 {
1902     gen_helper_sraw(cpu_gpr[rA(ctx->opcode)], cpu_env,
1903                     cpu_gpr[rS(ctx->opcode)], cpu_gpr[rB(ctx->opcode)]);
1904     if (unlikely(Rc(ctx->opcode) != 0))
1905         gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
1906 }
1907
1908 /* srawi & srawi. */
1909 static void gen_srawi(DisasContext *ctx)
1910 {
1911     int sh = SH(ctx->opcode);
1912     TCGv dst = cpu_gpr[rA(ctx->opcode)];
1913     TCGv src = cpu_gpr[rS(ctx->opcode)];
1914     if (sh == 0) {
1915         tcg_gen_mov_tl(dst, src);
1916         tcg_gen_movi_tl(cpu_ca, 0);
1917     } else {
1918         TCGv t0;
1919         tcg_gen_ext32s_tl(dst, src);
1920         tcg_gen_andi_tl(cpu_ca, dst, (1ULL << sh) - 1);
1921         t0 = tcg_temp_new();
1922         tcg_gen_sari_tl(t0, dst, TARGET_LONG_BITS - 1);
1923         tcg_gen_and_tl(cpu_ca, cpu_ca, t0);
1924         tcg_temp_free(t0);
1925         tcg_gen_setcondi_tl(TCG_COND_NE, cpu_ca, cpu_ca, 0);
1926         tcg_gen_sari_tl(dst, dst, sh);
1927     }
1928     if (unlikely(Rc(ctx->opcode) != 0)) {
1929         gen_set_Rc0(ctx, dst);
1930     }
1931 }
1932
1933 /* srw & srw. */
1934 static void gen_srw(DisasContext *ctx)
1935 {
1936     TCGv t0, t1;
1937
1938     t0 = tcg_temp_new();
1939     /* AND rS with a mask that is 0 when rB >= 0x20 */
1940 #if defined(TARGET_PPC64)
1941     tcg_gen_shli_tl(t0, cpu_gpr[rB(ctx->opcode)], 0x3a);
1942     tcg_gen_sari_tl(t0, t0, 0x3f);
1943 #else
1944     tcg_gen_shli_tl(t0, cpu_gpr[rB(ctx->opcode)], 0x1a);
1945     tcg_gen_sari_tl(t0, t0, 0x1f);
1946 #endif
1947     tcg_gen_andc_tl(t0, cpu_gpr[rS(ctx->opcode)], t0);
1948     tcg_gen_ext32u_tl(t0, t0);
1949     t1 = tcg_temp_new();
1950     tcg_gen_andi_tl(t1, cpu_gpr[rB(ctx->opcode)], 0x1f);
1951     tcg_gen_shr_tl(cpu_gpr[rA(ctx->opcode)], t0, t1);
1952     tcg_temp_free(t1);
1953     tcg_temp_free(t0);
1954     if (unlikely(Rc(ctx->opcode) != 0))
1955         gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
1956 }
1957
1958 #if defined(TARGET_PPC64)
1959 /* sld & sld. */
1960 static void gen_sld(DisasContext *ctx)
1961 {
1962     TCGv t0, t1;
1963
1964     t0 = tcg_temp_new();
1965     /* AND rS with a mask that is 0 when rB >= 0x40 */
1966     tcg_gen_shli_tl(t0, cpu_gpr[rB(ctx->opcode)], 0x39);
1967     tcg_gen_sari_tl(t0, t0, 0x3f);
1968     tcg_gen_andc_tl(t0, cpu_gpr[rS(ctx->opcode)], t0);
1969     t1 = tcg_temp_new();
1970     tcg_gen_andi_tl(t1, cpu_gpr[rB(ctx->opcode)], 0x3f);
1971     tcg_gen_shl_tl(cpu_gpr[rA(ctx->opcode)], t0, t1);
1972     tcg_temp_free(t1);
1973     tcg_temp_free(t0);
1974     if (unlikely(Rc(ctx->opcode) != 0))
1975         gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
1976 }
1977
1978 /* srad & srad. */
1979 static void gen_srad(DisasContext *ctx)
1980 {
1981     gen_helper_srad(cpu_gpr[rA(ctx->opcode)], cpu_env,
1982                     cpu_gpr[rS(ctx->opcode)], cpu_gpr[rB(ctx->opcode)]);
1983     if (unlikely(Rc(ctx->opcode) != 0))
1984         gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
1985 }
1986 /* sradi & sradi. */
1987 static inline void gen_sradi(DisasContext *ctx, int n)
1988 {
1989     int sh = SH(ctx->opcode) + (n << 5);
1990     TCGv dst = cpu_gpr[rA(ctx->opcode)];
1991     TCGv src = cpu_gpr[rS(ctx->opcode)];
1992     if (sh == 0) {
1993         tcg_gen_mov_tl(dst, src);
1994         tcg_gen_movi_tl(cpu_ca, 0);
1995     } else {
1996         TCGv t0;
1997         tcg_gen_andi_tl(cpu_ca, src, (1ULL << sh) - 1);
1998         t0 = tcg_temp_new();
1999         tcg_gen_sari_tl(t0, src, TARGET_LONG_BITS - 1);
2000         tcg_gen_and_tl(cpu_ca, cpu_ca, t0);
2001         tcg_temp_free(t0);
2002         tcg_gen_setcondi_tl(TCG_COND_NE, cpu_ca, cpu_ca, 0);
2003         tcg_gen_sari_tl(dst, src, sh);
2004     }
2005     if (unlikely(Rc(ctx->opcode) != 0)) {
2006         gen_set_Rc0(ctx, dst);
2007     }
2008 }
2009
2010 static void gen_sradi0(DisasContext *ctx)
2011 {
2012     gen_sradi(ctx, 0);
2013 }
2014
2015 static void gen_sradi1(DisasContext *ctx)
2016 {
2017     gen_sradi(ctx, 1);
2018 }
2019
2020 /* srd & srd. */
2021 static void gen_srd(DisasContext *ctx)
2022 {
2023     TCGv t0, t1;
2024
2025     t0 = tcg_temp_new();
2026     /* AND rS with a mask that is 0 when rB >= 0x40 */
2027     tcg_gen_shli_tl(t0, cpu_gpr[rB(ctx->opcode)], 0x39);
2028     tcg_gen_sari_tl(t0, t0, 0x3f);
2029     tcg_gen_andc_tl(t0, cpu_gpr[rS(ctx->opcode)], t0);
2030     t1 = tcg_temp_new();
2031     tcg_gen_andi_tl(t1, cpu_gpr[rB(ctx->opcode)], 0x3f);
2032     tcg_gen_shr_tl(cpu_gpr[rA(ctx->opcode)], t0, t1);
2033     tcg_temp_free(t1);
2034     tcg_temp_free(t0);
2035     if (unlikely(Rc(ctx->opcode) != 0))
2036         gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
2037 }
2038 #endif
2039
2040 /***                       Floating-Point arithmetic                       ***/
2041 #define _GEN_FLOAT_ACB(name, op, op1, op2, isfloat, set_fprf, type)           \
2042 static void gen_f##name(DisasContext *ctx)                                    \
2043 {                                                                             \
2044     if (unlikely(!ctx->fpu_enabled)) {                                        \
2045         gen_exception(ctx, POWERPC_EXCP_FPU);                                 \
2046         return;                                                               \
2047     }                                                                         \
2048     /* NIP cannot be restored if the memory exception comes from an helper */ \
2049     gen_update_nip(ctx, ctx->nip - 4);                                        \
2050     gen_reset_fpstatus();                                                     \
2051     gen_helper_f##op(cpu_fpr[rD(ctx->opcode)], cpu_env,                       \
2052                      cpu_fpr[rA(ctx->opcode)],                                \
2053                      cpu_fpr[rC(ctx->opcode)], cpu_fpr[rB(ctx->opcode)]);     \
2054     if (isfloat) {                                                            \
2055         gen_helper_frsp(cpu_fpr[rD(ctx->opcode)], cpu_env,                    \
2056                         cpu_fpr[rD(ctx->opcode)]);                            \
2057     }                                                                         \
2058     gen_compute_fprf(cpu_fpr[rD(ctx->opcode)], set_fprf,                      \
2059                      Rc(ctx->opcode) != 0);                                   \
2060 }
2061
2062 #define GEN_FLOAT_ACB(name, op2, set_fprf, type)                              \
2063 _GEN_FLOAT_ACB(name, name, 0x3F, op2, 0, set_fprf, type);                     \
2064 _GEN_FLOAT_ACB(name##s, name, 0x3B, op2, 1, set_fprf, type);
2065
2066 #define _GEN_FLOAT_AB(name, op, op1, op2, inval, isfloat, set_fprf, type)     \
2067 static void gen_f##name(DisasContext *ctx)                                    \
2068 {                                                                             \
2069     if (unlikely(!ctx->fpu_enabled)) {                                        \
2070         gen_exception(ctx, POWERPC_EXCP_FPU);                                 \
2071         return;                                                               \
2072     }                                                                         \
2073     /* NIP cannot be restored if the memory exception comes from an helper */ \
2074     gen_update_nip(ctx, ctx->nip - 4);                                        \
2075     gen_reset_fpstatus();                                                     \
2076     gen_helper_f##op(cpu_fpr[rD(ctx->opcode)], cpu_env,                       \
2077                      cpu_fpr[rA(ctx->opcode)],                                \
2078                      cpu_fpr[rB(ctx->opcode)]);                               \
2079     if (isfloat) {                                                            \
2080         gen_helper_frsp(cpu_fpr[rD(ctx->opcode)], cpu_env,                    \
2081                         cpu_fpr[rD(ctx->opcode)]);                            \
2082     }                                                                         \
2083     gen_compute_fprf(cpu_fpr[rD(ctx->opcode)],                                \
2084                      set_fprf, Rc(ctx->opcode) != 0);                         \
2085 }
2086 #define GEN_FLOAT_AB(name, op2, inval, set_fprf, type)                        \
2087 _GEN_FLOAT_AB(name, name, 0x3F, op2, inval, 0, set_fprf, type);               \
2088 _GEN_FLOAT_AB(name##s, name, 0x3B, op2, inval, 1, set_fprf, type);
2089
2090 #define _GEN_FLOAT_AC(name, op, op1, op2, inval, isfloat, set_fprf, type)     \
2091 static void gen_f##name(DisasContext *ctx)                                    \
2092 {                                                                             \
2093     if (unlikely(!ctx->fpu_enabled)) {                                        \
2094         gen_exception(ctx, POWERPC_EXCP_FPU);                                 \
2095         return;                                                               \
2096     }                                                                         \
2097     /* NIP cannot be restored if the memory exception comes from an helper */ \
2098     gen_update_nip(ctx, ctx->nip - 4);                                        \
2099     gen_reset_fpstatus();                                                     \
2100     gen_helper_f##op(cpu_fpr[rD(ctx->opcode)], cpu_env,                       \
2101                      cpu_fpr[rA(ctx->opcode)],                                \
2102                      cpu_fpr[rC(ctx->opcode)]);                               \
2103     if (isfloat) {                                                            \
2104         gen_helper_frsp(cpu_fpr[rD(ctx->opcode)], cpu_env,                    \
2105                         cpu_fpr[rD(ctx->opcode)]);                            \
2106     }                                                                         \
2107     gen_compute_fprf(cpu_fpr[rD(ctx->opcode)],                                \
2108                      set_fprf, Rc(ctx->opcode) != 0);                         \
2109 }
2110 #define GEN_FLOAT_AC(name, op2, inval, set_fprf, type)                        \
2111 _GEN_FLOAT_AC(name, name, 0x3F, op2, inval, 0, set_fprf, type);               \
2112 _GEN_FLOAT_AC(name##s, name, 0x3B, op2, inval, 1, set_fprf, type);
2113
2114 #define GEN_FLOAT_B(name, op2, op3, set_fprf, type)                           \
2115 static void gen_f##name(DisasContext *ctx)                                    \
2116 {                                                                             \
2117     if (unlikely(!ctx->fpu_enabled)) {                                        \
2118         gen_exception(ctx, POWERPC_EXCP_FPU);                                 \
2119         return;                                                               \
2120     }                                                                         \
2121     /* NIP cannot be restored if the memory exception comes from an helper */ \
2122     gen_update_nip(ctx, ctx->nip - 4);                                        \
2123     gen_reset_fpstatus();                                                     \
2124     gen_helper_f##name(cpu_fpr[rD(ctx->opcode)], cpu_env,                     \
2125                        cpu_fpr[rB(ctx->opcode)]);                             \
2126     gen_compute_fprf(cpu_fpr[rD(ctx->opcode)],                                \
2127                      set_fprf, Rc(ctx->opcode) != 0);                         \
2128 }
2129
2130 #define GEN_FLOAT_BS(name, op1, op2, set_fprf, type)                          \
2131 static void gen_f##name(DisasContext *ctx)                                    \
2132 {                                                                             \
2133     if (unlikely(!ctx->fpu_enabled)) {                                        \
2134         gen_exception(ctx, POWERPC_EXCP_FPU);                                 \
2135         return;                                                               \
2136     }                                                                         \
2137     /* NIP cannot be restored if the memory exception comes from an helper */ \
2138     gen_update_nip(ctx, ctx->nip - 4);                                        \
2139     gen_reset_fpstatus();                                                     \
2140     gen_helper_f##name(cpu_fpr[rD(ctx->opcode)], cpu_env,                     \
2141                        cpu_fpr[rB(ctx->opcode)]);                             \
2142     gen_compute_fprf(cpu_fpr[rD(ctx->opcode)],                                \
2143                      set_fprf, Rc(ctx->opcode) != 0);                         \
2144 }
2145
2146 /* fadd - fadds */
2147 GEN_FLOAT_AB(add, 0x15, 0x000007C0, 1, PPC_FLOAT);
2148 /* fdiv - fdivs */
2149 GEN_FLOAT_AB(div, 0x12, 0x000007C0, 1, PPC_FLOAT);
2150 /* fmul - fmuls */
2151 GEN_FLOAT_AC(mul, 0x19, 0x0000F800, 1, PPC_FLOAT);
2152
2153 /* fre */
2154 GEN_FLOAT_BS(re, 0x3F, 0x18, 1, PPC_FLOAT_EXT);
2155
2156 /* fres */
2157 GEN_FLOAT_BS(res, 0x3B, 0x18, 1, PPC_FLOAT_FRES);
2158
2159 /* frsqrte */
2160 GEN_FLOAT_BS(rsqrte, 0x3F, 0x1A, 1, PPC_FLOAT_FRSQRTE);
2161
2162 /* frsqrtes */
2163 static void gen_frsqrtes(DisasContext *ctx)
2164 {
2165     if (unlikely(!ctx->fpu_enabled)) {
2166         gen_exception(ctx, POWERPC_EXCP_FPU);
2167         return;
2168     }
2169     /* NIP cannot be restored if the memory exception comes from an helper */
2170     gen_update_nip(ctx, ctx->nip - 4);
2171     gen_reset_fpstatus();
2172     gen_helper_frsqrte(cpu_fpr[rD(ctx->opcode)], cpu_env,
2173                        cpu_fpr[rB(ctx->opcode)]);
2174     gen_helper_frsp(cpu_fpr[rD(ctx->opcode)], cpu_env,
2175                     cpu_fpr[rD(ctx->opcode)]);
2176     gen_compute_fprf(cpu_fpr[rD(ctx->opcode)], 1, Rc(ctx->opcode) != 0);
2177 }
2178
2179 /* fsel */
2180 _GEN_FLOAT_ACB(sel, sel, 0x3F, 0x17, 0, 0, PPC_FLOAT_FSEL);
2181 /* fsub - fsubs */
2182 GEN_FLOAT_AB(sub, 0x14, 0x000007C0, 1, PPC_FLOAT);
2183 /* Optional: */
2184
2185 /* fsqrt */
2186 static void gen_fsqrt(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_fsqrt(cpu_fpr[rD(ctx->opcode)], cpu_env,
2196                      cpu_fpr[rB(ctx->opcode)]);
2197     gen_compute_fprf(cpu_fpr[rD(ctx->opcode)], 1, Rc(ctx->opcode) != 0);
2198 }
2199
2200 static void gen_fsqrts(DisasContext *ctx)
2201 {
2202     if (unlikely(!ctx->fpu_enabled)) {
2203         gen_exception(ctx, POWERPC_EXCP_FPU);
2204         return;
2205     }
2206     /* NIP cannot be restored if the memory exception comes from an helper */
2207     gen_update_nip(ctx, ctx->nip - 4);
2208     gen_reset_fpstatus();
2209     gen_helper_fsqrt(cpu_fpr[rD(ctx->opcode)], cpu_env,
2210                      cpu_fpr[rB(ctx->opcode)]);
2211     gen_helper_frsp(cpu_fpr[rD(ctx->opcode)], cpu_env,
2212                     cpu_fpr[rD(ctx->opcode)]);
2213     gen_compute_fprf(cpu_fpr[rD(ctx->opcode)], 1, Rc(ctx->opcode) != 0);
2214 }
2215
2216 /***                     Floating-Point multiply-and-add                   ***/
2217 /* fmadd - fmadds */
2218 GEN_FLOAT_ACB(madd, 0x1D, 1, PPC_FLOAT);
2219 /* fmsub - fmsubs */
2220 GEN_FLOAT_ACB(msub, 0x1C, 1, PPC_FLOAT);
2221 /* fnmadd - fnmadds */
2222 GEN_FLOAT_ACB(nmadd, 0x1F, 1, PPC_FLOAT);
2223 /* fnmsub - fnmsubs */
2224 GEN_FLOAT_ACB(nmsub, 0x1E, 1, PPC_FLOAT);
2225
2226 /***                     Floating-Point round & convert                    ***/
2227 /* fctiw */
2228 GEN_FLOAT_B(ctiw, 0x0E, 0x00, 0, PPC_FLOAT);
2229 /* fctiwu */
2230 GEN_FLOAT_B(ctiwu, 0x0E, 0x04, 0, PPC2_FP_CVT_ISA206);
2231 /* fctiwz */
2232 GEN_FLOAT_B(ctiwz, 0x0F, 0x00, 0, PPC_FLOAT);
2233 /* fctiwuz */
2234 GEN_FLOAT_B(ctiwuz, 0x0F, 0x04, 0, PPC2_FP_CVT_ISA206);
2235 /* frsp */
2236 GEN_FLOAT_B(rsp, 0x0C, 0x00, 1, PPC_FLOAT);
2237 #if defined(TARGET_PPC64)
2238 /* fcfid */
2239 GEN_FLOAT_B(cfid, 0x0E, 0x1A, 1, PPC_64B);
2240 /* fcfids */
2241 GEN_FLOAT_B(cfids, 0x0E, 0x1A, 0, PPC2_FP_CVT_ISA206);
2242 /* fcfidu */
2243 GEN_FLOAT_B(cfidu, 0x0E, 0x1E, 0, PPC2_FP_CVT_ISA206);
2244 /* fcfidus */
2245 GEN_FLOAT_B(cfidus, 0x0E, 0x1E, 0, PPC2_FP_CVT_ISA206);
2246 /* fctid */
2247 GEN_FLOAT_B(ctid, 0x0E, 0x19, 0, PPC_64B);
2248 /* fctidu */
2249 GEN_FLOAT_B(ctidu, 0x0E, 0x1D, 0, PPC2_FP_CVT_ISA206);
2250 /* fctidz */
2251 GEN_FLOAT_B(ctidz, 0x0F, 0x19, 0, PPC_64B);
2252 /* fctidu */
2253 GEN_FLOAT_B(ctiduz, 0x0F, 0x1D, 0, PPC2_FP_CVT_ISA206);
2254 #endif
2255
2256 /* frin */
2257 GEN_FLOAT_B(rin, 0x08, 0x0C, 1, PPC_FLOAT_EXT);
2258 /* friz */
2259 GEN_FLOAT_B(riz, 0x08, 0x0D, 1, PPC_FLOAT_EXT);
2260 /* frip */
2261 GEN_FLOAT_B(rip, 0x08, 0x0E, 1, PPC_FLOAT_EXT);
2262 /* frim */
2263 GEN_FLOAT_B(rim, 0x08, 0x0F, 1, PPC_FLOAT_EXT);
2264
2265 static void gen_ftdiv(DisasContext *ctx)
2266 {
2267     if (unlikely(!ctx->fpu_enabled)) {
2268         gen_exception(ctx, POWERPC_EXCP_FPU);
2269         return;
2270     }
2271     gen_helper_ftdiv(cpu_crf[crfD(ctx->opcode)], cpu_fpr[rA(ctx->opcode)],
2272                      cpu_fpr[rB(ctx->opcode)]);
2273 }
2274
2275 static void gen_ftsqrt(DisasContext *ctx)
2276 {
2277     if (unlikely(!ctx->fpu_enabled)) {
2278         gen_exception(ctx, POWERPC_EXCP_FPU);
2279         return;
2280     }
2281     gen_helper_ftsqrt(cpu_crf[crfD(ctx->opcode)], cpu_fpr[rB(ctx->opcode)]);
2282 }
2283
2284
2285
2286 /***                         Floating-Point compare                        ***/
2287
2288 /* fcmpo */
2289 static void gen_fcmpo(DisasContext *ctx)
2290 {
2291     TCGv_i32 crf;
2292     if (unlikely(!ctx->fpu_enabled)) {
2293         gen_exception(ctx, POWERPC_EXCP_FPU);
2294         return;
2295     }
2296     /* NIP cannot be restored if the memory exception comes from an helper */
2297     gen_update_nip(ctx, ctx->nip - 4);
2298     gen_reset_fpstatus();
2299     crf = tcg_const_i32(crfD(ctx->opcode));
2300     gen_helper_fcmpo(cpu_env, cpu_fpr[rA(ctx->opcode)],
2301                      cpu_fpr[rB(ctx->opcode)], crf);
2302     tcg_temp_free_i32(crf);
2303     gen_helper_float_check_status(cpu_env);
2304 }
2305
2306 /* fcmpu */
2307 static void gen_fcmpu(DisasContext *ctx)
2308 {
2309     TCGv_i32 crf;
2310     if (unlikely(!ctx->fpu_enabled)) {
2311         gen_exception(ctx, POWERPC_EXCP_FPU);
2312         return;
2313     }
2314     /* NIP cannot be restored if the memory exception comes from an helper */
2315     gen_update_nip(ctx, ctx->nip - 4);
2316     gen_reset_fpstatus();
2317     crf = tcg_const_i32(crfD(ctx->opcode));
2318     gen_helper_fcmpu(cpu_env, cpu_fpr[rA(ctx->opcode)],
2319                      cpu_fpr[rB(ctx->opcode)], crf);
2320     tcg_temp_free_i32(crf);
2321     gen_helper_float_check_status(cpu_env);
2322 }
2323
2324 /***                         Floating-point move                           ***/
2325 /* fabs */
2326 /* XXX: beware that fabs never checks for NaNs nor update FPSCR */
2327 static void gen_fabs(DisasContext *ctx)
2328 {
2329     if (unlikely(!ctx->fpu_enabled)) {
2330         gen_exception(ctx, POWERPC_EXCP_FPU);
2331         return;
2332     }
2333     tcg_gen_andi_i64(cpu_fpr[rD(ctx->opcode)], cpu_fpr[rB(ctx->opcode)],
2334                      ~(1ULL << 63));
2335     gen_compute_fprf(cpu_fpr[rD(ctx->opcode)], 0, Rc(ctx->opcode) != 0);
2336 }
2337
2338 /* fmr  - fmr. */
2339 /* XXX: beware that fmr never checks for NaNs nor update FPSCR */
2340 static void gen_fmr(DisasContext *ctx)
2341 {
2342     if (unlikely(!ctx->fpu_enabled)) {
2343         gen_exception(ctx, POWERPC_EXCP_FPU);
2344         return;
2345     }
2346     tcg_gen_mov_i64(cpu_fpr[rD(ctx->opcode)], cpu_fpr[rB(ctx->opcode)]);
2347     gen_compute_fprf(cpu_fpr[rD(ctx->opcode)], 0, Rc(ctx->opcode) != 0);
2348 }
2349
2350 /* fnabs */
2351 /* XXX: beware that fnabs never checks for NaNs nor update FPSCR */
2352 static void gen_fnabs(DisasContext *ctx)
2353 {
2354     if (unlikely(!ctx->fpu_enabled)) {
2355         gen_exception(ctx, POWERPC_EXCP_FPU);
2356         return;
2357     }
2358     tcg_gen_ori_i64(cpu_fpr[rD(ctx->opcode)], cpu_fpr[rB(ctx->opcode)],
2359                     1ULL << 63);
2360     gen_compute_fprf(cpu_fpr[rD(ctx->opcode)], 0, Rc(ctx->opcode) != 0);
2361 }
2362
2363 /* fneg */
2364 /* XXX: beware that fneg never checks for NaNs nor update FPSCR */
2365 static void gen_fneg(DisasContext *ctx)
2366 {
2367     if (unlikely(!ctx->fpu_enabled)) {
2368         gen_exception(ctx, POWERPC_EXCP_FPU);
2369         return;
2370     }
2371     tcg_gen_xori_i64(cpu_fpr[rD(ctx->opcode)], cpu_fpr[rB(ctx->opcode)],
2372                      1ULL << 63);
2373     gen_compute_fprf(cpu_fpr[rD(ctx->opcode)], 0, Rc(ctx->opcode) != 0);
2374 }
2375
2376 /* fcpsgn: PowerPC 2.05 specification */
2377 /* XXX: beware that fcpsgn never checks for NaNs nor update FPSCR */
2378 static void gen_fcpsgn(DisasContext *ctx)
2379 {
2380     if (unlikely(!ctx->fpu_enabled)) {
2381         gen_exception(ctx, POWERPC_EXCP_FPU);
2382         return;
2383     }
2384     tcg_gen_deposit_i64(cpu_fpr[rD(ctx->opcode)], cpu_fpr[rA(ctx->opcode)],
2385                         cpu_fpr[rB(ctx->opcode)], 0, 63);
2386     gen_compute_fprf(cpu_fpr[rD(ctx->opcode)], 0, Rc(ctx->opcode) != 0);
2387 }
2388
2389 static void gen_fmrgew(DisasContext *ctx)
2390 {
2391     TCGv_i64 b0;
2392     if (unlikely(!ctx->fpu_enabled)) {
2393         gen_exception(ctx, POWERPC_EXCP_FPU);
2394         return;
2395     }
2396     b0 = tcg_temp_new_i64();
2397     tcg_gen_shri_i64(b0, cpu_fpr[rB(ctx->opcode)], 32);
2398     tcg_gen_deposit_i64(cpu_fpr[rD(ctx->opcode)], cpu_fpr[rA(ctx->opcode)],
2399                         b0, 0, 32);
2400     tcg_temp_free_i64(b0);
2401 }
2402
2403 static void gen_fmrgow(DisasContext *ctx)
2404 {
2405     if (unlikely(!ctx->fpu_enabled)) {
2406         gen_exception(ctx, POWERPC_EXCP_FPU);
2407         return;
2408     }
2409     tcg_gen_deposit_i64(cpu_fpr[rD(ctx->opcode)],
2410                         cpu_fpr[rB(ctx->opcode)],
2411                         cpu_fpr[rA(ctx->opcode)],
2412                         32, 32);
2413 }
2414
2415 /***                  Floating-Point status & ctrl register                ***/
2416
2417 /* mcrfs */
2418 static void gen_mcrfs(DisasContext *ctx)
2419 {
2420     TCGv tmp = tcg_temp_new();
2421     int bfa;
2422
2423     if (unlikely(!ctx->fpu_enabled)) {
2424         gen_exception(ctx, POWERPC_EXCP_FPU);
2425         return;
2426     }
2427     bfa = 4 * (7 - crfS(ctx->opcode));
2428     tcg_gen_shri_tl(tmp, cpu_fpscr, bfa);
2429     tcg_gen_trunc_tl_i32(cpu_crf[crfD(ctx->opcode)], tmp);
2430     tcg_temp_free(tmp);
2431     tcg_gen_andi_i32(cpu_crf[crfD(ctx->opcode)], cpu_crf[crfD(ctx->opcode)], 0xf);
2432     tcg_gen_andi_tl(cpu_fpscr, cpu_fpscr, ~(0xF << bfa));
2433 }
2434
2435 /* mffs */
2436 static void gen_mffs(DisasContext *ctx)
2437 {
2438     if (unlikely(!ctx->fpu_enabled)) {
2439         gen_exception(ctx, POWERPC_EXCP_FPU);
2440         return;
2441     }
2442     gen_reset_fpstatus();
2443     tcg_gen_extu_tl_i64(cpu_fpr[rD(ctx->opcode)], cpu_fpscr);
2444     gen_compute_fprf(cpu_fpr[rD(ctx->opcode)], 0, Rc(ctx->opcode) != 0);
2445 }
2446
2447 /* mtfsb0 */
2448 static void gen_mtfsb0(DisasContext *ctx)
2449 {
2450     uint8_t crb;
2451
2452     if (unlikely(!ctx->fpu_enabled)) {
2453         gen_exception(ctx, POWERPC_EXCP_FPU);
2454         return;
2455     }
2456     crb = 31 - crbD(ctx->opcode);
2457     gen_reset_fpstatus();
2458     if (likely(crb != FPSCR_FEX && crb != FPSCR_VX)) {
2459         TCGv_i32 t0;
2460         /* NIP cannot be restored if the memory exception comes from an helper */
2461         gen_update_nip(ctx, ctx->nip - 4);
2462         t0 = tcg_const_i32(crb);
2463         gen_helper_fpscr_clrbit(cpu_env, t0);
2464         tcg_temp_free_i32(t0);
2465     }
2466     if (unlikely(Rc(ctx->opcode) != 0)) {
2467         tcg_gen_trunc_tl_i32(cpu_crf[1], cpu_fpscr);
2468         tcg_gen_shri_i32(cpu_crf[1], cpu_crf[1], FPSCR_OX);
2469     }
2470 }
2471
2472 /* mtfsb1 */
2473 static void gen_mtfsb1(DisasContext *ctx)
2474 {
2475     uint8_t crb;
2476
2477     if (unlikely(!ctx->fpu_enabled)) {
2478         gen_exception(ctx, POWERPC_EXCP_FPU);
2479         return;
2480     }
2481     crb = 31 - crbD(ctx->opcode);
2482     gen_reset_fpstatus();
2483     /* XXX: we pretend we can only do IEEE floating-point computations */
2484     if (likely(crb != FPSCR_FEX && crb != FPSCR_VX && crb != FPSCR_NI)) {
2485         TCGv_i32 t0;
2486         /* NIP cannot be restored if the memory exception comes from an helper */
2487         gen_update_nip(ctx, ctx->nip - 4);
2488         t0 = tcg_const_i32(crb);
2489         gen_helper_fpscr_setbit(cpu_env, t0);
2490         tcg_temp_free_i32(t0);
2491     }
2492     if (unlikely(Rc(ctx->opcode) != 0)) {
2493         tcg_gen_trunc_tl_i32(cpu_crf[1], cpu_fpscr);
2494         tcg_gen_shri_i32(cpu_crf[1], cpu_crf[1], FPSCR_OX);
2495     }
2496     /* We can raise a differed exception */
2497     gen_helper_float_check_status(cpu_env);
2498 }
2499
2500 /* mtfsf */
2501 static void gen_mtfsf(DisasContext *ctx)
2502 {
2503     TCGv_i32 t0;
2504     int flm, l, w;
2505
2506     if (unlikely(!ctx->fpu_enabled)) {
2507         gen_exception(ctx, POWERPC_EXCP_FPU);
2508         return;
2509     }
2510     flm = FPFLM(ctx->opcode);
2511     l = FPL(ctx->opcode);
2512     w = FPW(ctx->opcode);
2513     if (unlikely(w & !(ctx->insns_flags2 & PPC2_ISA205))) {
2514         gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL);
2515         return;
2516     }
2517     /* NIP cannot be restored if the memory exception comes from an helper */
2518     gen_update_nip(ctx, ctx->nip - 4);
2519     gen_reset_fpstatus();
2520     if (l) {
2521         t0 = tcg_const_i32((ctx->insns_flags2 & PPC2_ISA205) ? 0xffff : 0xff);
2522     } else {
2523         t0 = tcg_const_i32(flm << (w * 8));
2524     }
2525     gen_helper_store_fpscr(cpu_env, cpu_fpr[rB(ctx->opcode)], t0);
2526     tcg_temp_free_i32(t0);
2527     if (unlikely(Rc(ctx->opcode) != 0)) {
2528         tcg_gen_trunc_tl_i32(cpu_crf[1], cpu_fpscr);
2529         tcg_gen_shri_i32(cpu_crf[1], cpu_crf[1], FPSCR_OX);
2530     }
2531     /* We can raise a differed exception */
2532     gen_helper_float_check_status(cpu_env);
2533 }
2534
2535 /* mtfsfi */
2536 static void gen_mtfsfi(DisasContext *ctx)
2537 {
2538     int bf, sh, w;
2539     TCGv_i64 t0;
2540     TCGv_i32 t1;
2541
2542     if (unlikely(!ctx->fpu_enabled)) {
2543         gen_exception(ctx, POWERPC_EXCP_FPU);
2544         return;
2545     }
2546     w = FPW(ctx->opcode);
2547     bf = FPBF(ctx->opcode);
2548     if (unlikely(w & !(ctx->insns_flags2 & PPC2_ISA205))) {
2549         gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL);
2550         return;
2551     }
2552     sh = (8 * w) + 7 - bf;
2553     /* NIP cannot be restored if the memory exception comes from an helper */
2554     gen_update_nip(ctx, ctx->nip - 4);
2555     gen_reset_fpstatus();
2556     t0 = tcg_const_i64(((uint64_t)FPIMM(ctx->opcode)) << (4 * sh));
2557     t1 = tcg_const_i32(1 << sh);
2558     gen_helper_store_fpscr(cpu_env, t0, t1);
2559     tcg_temp_free_i64(t0);
2560     tcg_temp_free_i32(t1);
2561     if (unlikely(Rc(ctx->opcode) != 0)) {
2562         tcg_gen_trunc_tl_i32(cpu_crf[1], cpu_fpscr);
2563         tcg_gen_shri_i32(cpu_crf[1], cpu_crf[1], FPSCR_OX);
2564     }
2565     /* We can raise a differed exception */
2566     gen_helper_float_check_status(cpu_env);
2567 }
2568
2569 /***                           Addressing modes                            ***/
2570 /* Register indirect with immediate index : EA = (rA|0) + SIMM */
2571 static inline void gen_addr_imm_index(DisasContext *ctx, TCGv EA,
2572                                       target_long maskl)
2573 {
2574     target_long simm = SIMM(ctx->opcode);
2575
2576     simm &= ~maskl;
2577     if (rA(ctx->opcode) == 0) {
2578         if (NARROW_MODE(ctx)) {
2579             simm = (uint32_t)simm;
2580         }
2581         tcg_gen_movi_tl(EA, simm);
2582     } else if (likely(simm != 0)) {
2583         tcg_gen_addi_tl(EA, cpu_gpr[rA(ctx->opcode)], simm);
2584         if (NARROW_MODE(ctx)) {
2585             tcg_gen_ext32u_tl(EA, EA);
2586         }
2587     } else {
2588         if (NARROW_MODE(ctx)) {
2589             tcg_gen_ext32u_tl(EA, cpu_gpr[rA(ctx->opcode)]);
2590         } else {
2591             tcg_gen_mov_tl(EA, cpu_gpr[rA(ctx->opcode)]);
2592         }
2593     }
2594 }
2595
2596 static inline void gen_addr_reg_index(DisasContext *ctx, TCGv EA)
2597 {
2598     if (rA(ctx->opcode) == 0) {
2599         if (NARROW_MODE(ctx)) {
2600             tcg_gen_ext32u_tl(EA, cpu_gpr[rB(ctx->opcode)]);
2601         } else {
2602             tcg_gen_mov_tl(EA, cpu_gpr[rB(ctx->opcode)]);
2603         }
2604     } else {
2605         tcg_gen_add_tl(EA, cpu_gpr[rA(ctx->opcode)], cpu_gpr[rB(ctx->opcode)]);
2606         if (NARROW_MODE(ctx)) {
2607             tcg_gen_ext32u_tl(EA, EA);
2608         }
2609     }
2610 }
2611
2612 static inline void gen_addr_register(DisasContext *ctx, TCGv EA)
2613 {
2614     if (rA(ctx->opcode) == 0) {
2615         tcg_gen_movi_tl(EA, 0);
2616     } else if (NARROW_MODE(ctx)) {
2617         tcg_gen_ext32u_tl(EA, cpu_gpr[rA(ctx->opcode)]);
2618     } else {
2619         tcg_gen_mov_tl(EA, cpu_gpr[rA(ctx->opcode)]);
2620     }
2621 }
2622
2623 static inline void gen_addr_add(DisasContext *ctx, TCGv ret, TCGv arg1,
2624                                 target_long val)
2625 {
2626     tcg_gen_addi_tl(ret, arg1, val);
2627     if (NARROW_MODE(ctx)) {
2628         tcg_gen_ext32u_tl(ret, ret);
2629     }
2630 }
2631
2632 static inline void gen_check_align(DisasContext *ctx, TCGv EA, int mask)
2633 {
2634     int l1 = gen_new_label();
2635     TCGv t0 = tcg_temp_new();
2636     TCGv_i32 t1, t2;
2637     /* NIP cannot be restored if the memory exception comes from an helper */
2638     gen_update_nip(ctx, ctx->nip - 4);
2639     tcg_gen_andi_tl(t0, EA, mask);
2640     tcg_gen_brcondi_tl(TCG_COND_EQ, t0, 0, l1);
2641     t1 = tcg_const_i32(POWERPC_EXCP_ALIGN);
2642     t2 = tcg_const_i32(0);
2643     gen_helper_raise_exception_err(cpu_env, t1, t2);
2644     tcg_temp_free_i32(t1);
2645     tcg_temp_free_i32(t2);
2646     gen_set_label(l1);
2647     tcg_temp_free(t0);
2648 }
2649
2650 /***                             Integer load                              ***/
2651 static inline void gen_qemu_ld8u(DisasContext *ctx, TCGv arg1, TCGv arg2)
2652 {
2653     tcg_gen_qemu_ld8u(arg1, arg2, ctx->mem_idx);
2654 }
2655
2656 static inline void gen_qemu_ld8s(DisasContext *ctx, TCGv arg1, TCGv arg2)
2657 {
2658     tcg_gen_qemu_ld8s(arg1, arg2, ctx->mem_idx);
2659 }
2660
2661 static inline void gen_qemu_ld16u(DisasContext *ctx, TCGv arg1, TCGv arg2)
2662 {
2663     tcg_gen_qemu_ld16u(arg1, arg2, ctx->mem_idx);
2664     if (unlikely(ctx->le_mode)) {
2665         tcg_gen_bswap16_tl(arg1, arg1);
2666     }
2667 }
2668
2669 static inline void gen_qemu_ld16s(DisasContext *ctx, TCGv arg1, TCGv arg2)
2670 {
2671     if (unlikely(ctx->le_mode)) {
2672         tcg_gen_qemu_ld16u(arg1, arg2, ctx->mem_idx);
2673         tcg_gen_bswap16_tl(arg1, arg1);
2674         tcg_gen_ext16s_tl(arg1, arg1);
2675     } else {
2676         tcg_gen_qemu_ld16s(arg1, arg2, ctx->mem_idx);
2677     }
2678 }
2679
2680 static inline void gen_qemu_ld32u(DisasContext *ctx, TCGv arg1, TCGv arg2)
2681 {
2682     tcg_gen_qemu_ld32u(arg1, arg2, ctx->mem_idx);
2683     if (unlikely(ctx->le_mode)) {
2684         tcg_gen_bswap32_tl(arg1, arg1);
2685     }
2686 }
2687
2688 static void gen_qemu_ld32u_i64(DisasContext *ctx, TCGv_i64 val, TCGv addr)
2689 {
2690     TCGv tmp = tcg_temp_new();
2691     gen_qemu_ld32u(ctx, tmp, addr);
2692     tcg_gen_extu_tl_i64(val, tmp);
2693     tcg_temp_free(tmp);
2694 }
2695
2696 static inline void gen_qemu_ld32s(DisasContext *ctx, TCGv arg1, TCGv arg2)
2697 {
2698     if (unlikely(ctx->le_mode)) {
2699         tcg_gen_qemu_ld32u(arg1, arg2, ctx->mem_idx);
2700         tcg_gen_bswap32_tl(arg1, arg1);
2701         tcg_gen_ext32s_tl(arg1, arg1);
2702     } else
2703         tcg_gen_qemu_ld32s(arg1, arg2, ctx->mem_idx);
2704 }
2705
2706 static void gen_qemu_ld32s_i64(DisasContext *ctx, TCGv_i64 val, TCGv addr)
2707 {
2708     TCGv tmp = tcg_temp_new();
2709     gen_qemu_ld32s(ctx, tmp, addr);
2710     tcg_gen_ext_tl_i64(val, tmp);
2711     tcg_temp_free(tmp);
2712 }
2713
2714 static inline void gen_qemu_ld64(DisasContext *ctx, TCGv_i64 arg1, TCGv arg2)
2715 {
2716     tcg_gen_qemu_ld64(arg1, arg2, ctx->mem_idx);
2717     if (unlikely(ctx->le_mode)) {
2718         tcg_gen_bswap64_i64(arg1, arg1);
2719     }
2720 }
2721
2722 static inline void gen_qemu_st8(DisasContext *ctx, TCGv arg1, TCGv arg2)
2723 {
2724     tcg_gen_qemu_st8(arg1, arg2, ctx->mem_idx);
2725 }
2726
2727 static inline void gen_qemu_st16(DisasContext *ctx, TCGv arg1, TCGv arg2)
2728 {
2729     if (unlikely(ctx->le_mode)) {
2730         TCGv t0 = tcg_temp_new();
2731         tcg_gen_ext16u_tl(t0, arg1);
2732         tcg_gen_bswap16_tl(t0, t0);
2733         tcg_gen_qemu_st16(t0, arg2, ctx->mem_idx);
2734         tcg_temp_free(t0);
2735     } else {
2736         tcg_gen_qemu_st16(arg1, arg2, ctx->mem_idx);
2737     }
2738 }
2739
2740 static inline void gen_qemu_st32(DisasContext *ctx, TCGv arg1, TCGv arg2)
2741 {
2742     if (unlikely(ctx->le_mode)) {
2743         TCGv t0 = tcg_temp_new();
2744         tcg_gen_ext32u_tl(t0, arg1);
2745         tcg_gen_bswap32_tl(t0, t0);
2746         tcg_gen_qemu_st32(t0, arg2, ctx->mem_idx);
2747         tcg_temp_free(t0);
2748     } else {
2749         tcg_gen_qemu_st32(arg1, arg2, ctx->mem_idx);
2750     }
2751 }
2752
2753 static void gen_qemu_st32_i64(DisasContext *ctx, TCGv_i64 val, TCGv addr)
2754 {
2755     TCGv tmp = tcg_temp_new();
2756     tcg_gen_trunc_i64_tl(tmp, val);
2757     gen_qemu_st32(ctx, tmp, addr);
2758     tcg_temp_free(tmp);
2759 }
2760
2761 static inline void gen_qemu_st64(DisasContext *ctx, TCGv_i64 arg1, TCGv arg2)
2762 {
2763     if (unlikely(ctx->le_mode)) {
2764         TCGv_i64 t0 = tcg_temp_new_i64();
2765         tcg_gen_bswap64_i64(t0, arg1);
2766         tcg_gen_qemu_st64(t0, arg2, ctx->mem_idx);
2767         tcg_temp_free_i64(t0);
2768     } else
2769         tcg_gen_qemu_st64(arg1, arg2, ctx->mem_idx);
2770 }
2771
2772 #define GEN_LD(name, ldop, opc, type)                                         \
2773 static void glue(gen_, name)(DisasContext *ctx)                                       \
2774 {                                                                             \
2775     TCGv EA;                                                                  \
2776     gen_set_access_type(ctx, ACCESS_INT);                                     \
2777     EA = tcg_temp_new();                                                      \
2778     gen_addr_imm_index(ctx, EA, 0);                                           \
2779     gen_qemu_##ldop(ctx, cpu_gpr[rD(ctx->opcode)], EA);                       \
2780     tcg_temp_free(EA);                                                        \
2781 }
2782
2783 #define GEN_LDU(name, ldop, opc, type)                                        \
2784 static void glue(gen_, name##u)(DisasContext *ctx)                                    \
2785 {                                                                             \
2786     TCGv EA;                                                                  \
2787     if (unlikely(rA(ctx->opcode) == 0 ||                                      \
2788                  rA(ctx->opcode) == rD(ctx->opcode))) {                       \
2789         gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL);                   \
2790         return;                                                               \
2791     }                                                                         \
2792     gen_set_access_type(ctx, ACCESS_INT);                                     \
2793     EA = tcg_temp_new();                                                      \
2794     if (type == PPC_64B)                                                      \
2795         gen_addr_imm_index(ctx, EA, 0x03);                                    \
2796     else                                                                      \
2797         gen_addr_imm_index(ctx, EA, 0);                                       \
2798     gen_qemu_##ldop(ctx, cpu_gpr[rD(ctx->opcode)], EA);                       \
2799     tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], EA);                             \
2800     tcg_temp_free(EA);                                                        \
2801 }
2802
2803 #define GEN_LDUX(name, ldop, opc2, opc3, type)                                \
2804 static void glue(gen_, name##ux)(DisasContext *ctx)                                   \
2805 {                                                                             \
2806     TCGv EA;                                                                  \
2807     if (unlikely(rA(ctx->opcode) == 0 ||                                      \
2808                  rA(ctx->opcode) == rD(ctx->opcode))) {                       \
2809         gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL);                   \
2810         return;                                                               \
2811     }                                                                         \
2812     gen_set_access_type(ctx, ACCESS_INT);                                     \
2813     EA = tcg_temp_new();                                                      \
2814     gen_addr_reg_index(ctx, EA);                                              \
2815     gen_qemu_##ldop(ctx, cpu_gpr[rD(ctx->opcode)], EA);                       \
2816     tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], EA);                             \
2817     tcg_temp_free(EA);                                                        \
2818 }
2819
2820 #define GEN_LDX_E(name, ldop, opc2, opc3, type, type2)                        \
2821 static void glue(gen_, name##x)(DisasContext *ctx)                            \
2822 {                                                                             \
2823     TCGv EA;                                                                  \
2824     gen_set_access_type(ctx, ACCESS_INT);                                     \
2825     EA = tcg_temp_new();                                                      \
2826     gen_addr_reg_index(ctx, EA);                                              \
2827     gen_qemu_##ldop(ctx, cpu_gpr[rD(ctx->opcode)], EA);                       \
2828     tcg_temp_free(EA);                                                        \
2829 }
2830 #define GEN_LDX(name, ldop, opc2, opc3, type)                                 \
2831     GEN_LDX_E(name, ldop, opc2, opc3, type, PPC_NONE)
2832
2833 #define GEN_LDS(name, ldop, op, type)                                         \
2834 GEN_LD(name, ldop, op | 0x20, type);                                          \
2835 GEN_LDU(name, ldop, op | 0x21, type);                                         \
2836 GEN_LDUX(name, ldop, 0x17, op | 0x01, type);                                  \
2837 GEN_LDX(name, ldop, 0x17, op | 0x00, type)
2838
2839 /* lbz lbzu lbzux lbzx */
2840 GEN_LDS(lbz, ld8u, 0x02, PPC_INTEGER);
2841 /* lha lhau lhaux lhax */
2842 GEN_LDS(lha, ld16s, 0x0A, PPC_INTEGER);
2843 /* lhz lhzu lhzux lhzx */
2844 GEN_LDS(lhz, ld16u, 0x08, PPC_INTEGER);
2845 /* lwz lwzu lwzux lwzx */
2846 GEN_LDS(lwz, ld32u, 0x00, PPC_INTEGER);
2847 #if defined(TARGET_PPC64)
2848 /* lwaux */
2849 GEN_LDUX(lwa, ld32s, 0x15, 0x0B, PPC_64B);
2850 /* lwax */
2851 GEN_LDX(lwa, ld32s, 0x15, 0x0A, PPC_64B);
2852 /* ldux */
2853 GEN_LDUX(ld, ld64, 0x15, 0x01, PPC_64B);
2854 /* ldx */
2855 GEN_LDX(ld, ld64, 0x15, 0x00, PPC_64B);
2856
2857 static void gen_ld(DisasContext *ctx)
2858 {
2859     TCGv EA;
2860     if (Rc(ctx->opcode)) {
2861         if (unlikely(rA(ctx->opcode) == 0 ||
2862                      rA(ctx->opcode) == rD(ctx->opcode))) {
2863             gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL);
2864             return;
2865         }
2866     }
2867     gen_set_access_type(ctx, ACCESS_INT);
2868     EA = tcg_temp_new();
2869     gen_addr_imm_index(ctx, EA, 0x03);
2870     if (ctx->opcode & 0x02) {
2871         /* lwa (lwau is undefined) */
2872         gen_qemu_ld32s(ctx, cpu_gpr[rD(ctx->opcode)], EA);
2873     } else {
2874         /* ld - ldu */
2875         gen_qemu_ld64(ctx, cpu_gpr[rD(ctx->opcode)], EA);
2876     }
2877     if (Rc(ctx->opcode))
2878         tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], EA);
2879     tcg_temp_free(EA);
2880 }
2881
2882 /* lq */
2883 static void gen_lq(DisasContext *ctx)
2884 {
2885     int ra, rd;
2886     TCGv EA;
2887
2888     /* lq is a legal user mode instruction starting in ISA 2.07 */
2889     bool legal_in_user_mode = (ctx->insns_flags2 & PPC2_LSQ_ISA207) != 0;
2890     bool le_is_supported = (ctx->insns_flags2 & PPC2_LSQ_ISA207) != 0;
2891
2892     if (!legal_in_user_mode && is_user_mode(ctx)) {
2893         gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
2894         return;
2895     }
2896
2897     if (!le_is_supported && ctx->le_mode) {
2898         gen_exception_err(ctx, POWERPC_EXCP_ALIGN, POWERPC_EXCP_ALIGN_LE);
2899         return;
2900     }
2901
2902     ra = rA(ctx->opcode);
2903     rd = rD(ctx->opcode);
2904     if (unlikely((rd & 1) || rd == ra)) {
2905         gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL);
2906         return;
2907     }
2908
2909     gen_set_access_type(ctx, ACCESS_INT);
2910     EA = tcg_temp_new();
2911     gen_addr_imm_index(ctx, EA, 0x0F);
2912
2913     if (unlikely(ctx->le_mode)) {
2914         gen_qemu_ld64(ctx, cpu_gpr[rd+1], EA);
2915         gen_addr_add(ctx, EA, EA, 8);
2916         gen_qemu_ld64(ctx, cpu_gpr[rd], EA);
2917     } else {
2918         gen_qemu_ld64(ctx, cpu_gpr[rd], EA);
2919         gen_addr_add(ctx, EA, EA, 8);
2920         gen_qemu_ld64(ctx, cpu_gpr[rd+1], EA);
2921     }
2922     tcg_temp_free(EA);
2923 }
2924 #endif
2925
2926 /***                              Integer store                            ***/
2927 #define GEN_ST(name, stop, opc, type)                                         \
2928 static void glue(gen_, name)(DisasContext *ctx)                                       \
2929 {                                                                             \
2930     TCGv EA;                                                                  \
2931     gen_set_access_type(ctx, ACCESS_INT);                                     \
2932     EA = tcg_temp_new();                                                      \
2933     gen_addr_imm_index(ctx, EA, 0);                                           \
2934     gen_qemu_##stop(ctx, cpu_gpr[rS(ctx->opcode)], EA);                       \
2935     tcg_temp_free(EA);                                                        \
2936 }
2937
2938 #define GEN_STU(name, stop, opc, type)                                        \
2939 static void glue(gen_, stop##u)(DisasContext *ctx)                                    \
2940 {                                                                             \
2941     TCGv EA;                                                                  \
2942     if (unlikely(rA(ctx->opcode) == 0)) {                                     \
2943         gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL);                   \
2944         return;                                                               \
2945     }                                                                         \
2946     gen_set_access_type(ctx, ACCESS_INT);                                     \
2947     EA = tcg_temp_new();                                                      \
2948     if (type == PPC_64B)                                                      \
2949         gen_addr_imm_index(ctx, EA, 0x03);                                    \
2950     else                                                                      \
2951         gen_addr_imm_index(ctx, EA, 0);                                       \
2952     gen_qemu_##stop(ctx, cpu_gpr[rS(ctx->opcode)], EA);                       \
2953     tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], EA);                             \
2954     tcg_temp_free(EA);                                                        \
2955 }
2956
2957 #define GEN_STUX(name, stop, opc2, opc3, type)                                \
2958 static void glue(gen_, name##ux)(DisasContext *ctx)                                   \
2959 {                                                                             \
2960     TCGv EA;                                                                  \
2961     if (unlikely(rA(ctx->opcode) == 0)) {                                     \
2962         gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL);                   \
2963         return;                                                               \
2964     }                                                                         \
2965     gen_set_access_type(ctx, ACCESS_INT);                                     \
2966     EA = tcg_temp_new();                                                      \
2967     gen_addr_reg_index(ctx, EA);                                              \
2968     gen_qemu_##stop(ctx, cpu_gpr[rS(ctx->opcode)], EA);                       \
2969     tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], EA);                             \
2970     tcg_temp_free(EA);                                                        \
2971 }
2972
2973 #define GEN_STX_E(name, stop, opc2, opc3, type, type2)                        \
2974 static void glue(gen_, name##x)(DisasContext *ctx)                            \
2975 {                                                                             \
2976     TCGv EA;                                                                  \
2977     gen_set_access_type(ctx, ACCESS_INT);                                     \
2978     EA = tcg_temp_new();                                                      \
2979     gen_addr_reg_index(ctx, EA);                                              \
2980     gen_qemu_##stop(ctx, cpu_gpr[rS(ctx->opcode)], EA);                       \
2981     tcg_temp_free(EA);                                                        \
2982 }
2983 #define GEN_STX(name, stop, opc2, opc3, type)                                 \
2984     GEN_STX_E(name, stop, opc2, opc3, type, PPC_NONE)
2985
2986 #define GEN_STS(name, stop, op, type)                                         \
2987 GEN_ST(name, stop, op | 0x20, type);                                          \
2988 GEN_STU(name, stop, op | 0x21, type);                                         \
2989 GEN_STUX(name, stop, 0x17, op | 0x01, type);                                  \
2990 GEN_STX(name, stop, 0x17, op | 0x00, type)
2991
2992 /* stb stbu stbux stbx */
2993 GEN_STS(stb, st8, 0x06, PPC_INTEGER);
2994 /* sth sthu sthux sthx */
2995 GEN_STS(sth, st16, 0x0C, PPC_INTEGER);
2996 /* stw stwu stwux stwx */
2997 GEN_STS(stw, st32, 0x04, PPC_INTEGER);
2998 #if defined(TARGET_PPC64)
2999 GEN_STUX(std, st64, 0x15, 0x05, PPC_64B);
3000 GEN_STX(std, st64, 0x15, 0x04, PPC_64B);
3001
3002 static void gen_std(DisasContext *ctx)
3003 {
3004     int rs;
3005     TCGv EA;
3006
3007     rs = rS(ctx->opcode);
3008     if ((ctx->opcode & 0x3) == 0x2) { /* stq */
3009
3010         bool legal_in_user_mode = (ctx->insns_flags2 & PPC2_LSQ_ISA207) != 0;
3011         bool le_is_supported = (ctx->insns_flags2 & PPC2_LSQ_ISA207) != 0;
3012
3013         if (!legal_in_user_mode && is_user_mode(ctx)) {
3014             gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
3015             return;
3016         }
3017
3018         if (!le_is_supported && ctx->le_mode) {
3019             gen_exception_err(ctx, POWERPC_EXCP_ALIGN, POWERPC_EXCP_ALIGN_LE);
3020             return;
3021         }
3022
3023         if (unlikely(rs & 1)) {
3024             gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL);
3025             return;
3026         }
3027         gen_set_access_type(ctx, ACCESS_INT);
3028         EA = tcg_temp_new();
3029         gen_addr_imm_index(ctx, EA, 0x03);
3030
3031         if (unlikely(ctx->le_mode)) {
3032             gen_qemu_st64(ctx, cpu_gpr[rs+1], EA);
3033             gen_addr_add(ctx, EA, EA, 8);
3034             gen_qemu_st64(ctx, cpu_gpr[rs], EA);
3035         } else {
3036             gen_qemu_st64(ctx, cpu_gpr[rs], EA);
3037             gen_addr_add(ctx, EA, EA, 8);
3038             gen_qemu_st64(ctx, cpu_gpr[rs+1], EA);
3039         }
3040         tcg_temp_free(EA);
3041     } else {
3042         /* std / stdu*/
3043         if (Rc(ctx->opcode)) {
3044             if (unlikely(rA(ctx->opcode) == 0)) {
3045                 gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL);
3046                 return;
3047             }
3048         }
3049         gen_set_access_type(ctx, ACCESS_INT);
3050         EA = tcg_temp_new();
3051         gen_addr_imm_index(ctx, EA, 0x03);
3052         gen_qemu_st64(ctx, cpu_gpr[rs], EA);
3053         if (Rc(ctx->opcode))
3054             tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], EA);
3055         tcg_temp_free(EA);
3056     }
3057 }
3058 #endif
3059 /***                Integer load and store with byte reverse               ***/
3060 /* lhbrx */
3061 static inline void gen_qemu_ld16ur(DisasContext *ctx, TCGv arg1, TCGv arg2)
3062 {
3063     tcg_gen_qemu_ld16u(arg1, arg2, ctx->mem_idx);
3064     if (likely(!ctx->le_mode)) {
3065         tcg_gen_bswap16_tl(arg1, arg1);
3066     }
3067 }
3068 GEN_LDX(lhbr, ld16ur, 0x16, 0x18, PPC_INTEGER);
3069
3070 /* lwbrx */
3071 static inline void gen_qemu_ld32ur(DisasContext *ctx, TCGv arg1, TCGv arg2)
3072 {
3073     tcg_gen_qemu_ld32u(arg1, arg2, ctx->mem_idx);
3074     if (likely(!ctx->le_mode)) {
3075         tcg_gen_bswap32_tl(arg1, arg1);
3076     }
3077 }
3078 GEN_LDX(lwbr, ld32ur, 0x16, 0x10, PPC_INTEGER);
3079
3080 #if defined(TARGET_PPC64)
3081 /* ldbrx */
3082 static inline void gen_qemu_ld64ur(DisasContext *ctx, TCGv arg1, TCGv arg2)
3083 {
3084     tcg_gen_qemu_ld64(arg1, arg2, ctx->mem_idx);
3085     if (likely(!ctx->le_mode)) {
3086         tcg_gen_bswap64_tl(arg1, arg1);
3087     }
3088 }
3089 GEN_LDX_E(ldbr, ld64ur, 0x14, 0x10, PPC_NONE, PPC2_DBRX);
3090 #endif  /* TARGET_PPC64 */
3091
3092 /* sthbrx */
3093 static inline void gen_qemu_st16r(DisasContext *ctx, TCGv arg1, TCGv arg2)
3094 {
3095     if (likely(!ctx->le_mode)) {
3096         TCGv t0 = tcg_temp_new();
3097         tcg_gen_ext16u_tl(t0, arg1);
3098         tcg_gen_bswap16_tl(t0, t0);
3099         tcg_gen_qemu_st16(t0, arg2, ctx->mem_idx);
3100         tcg_temp_free(t0);
3101     } else {
3102         tcg_gen_qemu_st16(arg1, arg2, ctx->mem_idx);
3103     }
3104 }
3105 GEN_STX(sthbr, st16r, 0x16, 0x1C, PPC_INTEGER);
3106
3107 /* stwbrx */
3108 static inline void gen_qemu_st32r(DisasContext *ctx, TCGv arg1, TCGv arg2)
3109 {
3110     if (likely(!ctx->le_mode)) {
3111         TCGv t0 = tcg_temp_new();
3112         tcg_gen_ext32u_tl(t0, arg1);
3113         tcg_gen_bswap32_tl(t0, t0);
3114         tcg_gen_qemu_st32(t0, arg2, ctx->mem_idx);
3115         tcg_temp_free(t0);
3116     } else {
3117         tcg_gen_qemu_st32(arg1, arg2, ctx->mem_idx);
3118     }
3119 }
3120 GEN_STX(stwbr, st32r, 0x16, 0x14, PPC_INTEGER);
3121
3122 #if defined(TARGET_PPC64)
3123 /* stdbrx */
3124 static inline void gen_qemu_st64r(DisasContext *ctx, TCGv arg1, TCGv arg2)
3125 {
3126     if (likely(!ctx->le_mode)) {
3127         TCGv t0 = tcg_temp_new();
3128         tcg_gen_bswap64_tl(t0, arg1);
3129         tcg_gen_qemu_st64(t0, arg2, ctx->mem_idx);
3130         tcg_temp_free(t0);
3131     } else {
3132         tcg_gen_qemu_st64(arg1, arg2, ctx->mem_idx);
3133     }
3134 }
3135 GEN_STX_E(stdbr, st64r, 0x14, 0x14, PPC_NONE, PPC2_DBRX);
3136 #endif  /* TARGET_PPC64 */
3137
3138 /***                    Integer load and store multiple                    ***/
3139
3140 /* lmw */
3141 static void gen_lmw(DisasContext *ctx)
3142 {
3143     TCGv t0;
3144     TCGv_i32 t1;
3145     gen_set_access_type(ctx, ACCESS_INT);
3146     /* NIP cannot be restored if the memory exception comes from an helper */
3147     gen_update_nip(ctx, ctx->nip - 4);
3148     t0 = tcg_temp_new();
3149     t1 = tcg_const_i32(rD(ctx->opcode));
3150     gen_addr_imm_index(ctx, t0, 0);
3151     gen_helper_lmw(cpu_env, t0, t1);
3152     tcg_temp_free(t0);
3153     tcg_temp_free_i32(t1);
3154 }
3155
3156 /* stmw */
3157 static void gen_stmw(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(rS(ctx->opcode));
3166     gen_addr_imm_index(ctx, t0, 0);
3167     gen_helper_stmw(cpu_env, t0, t1);
3168     tcg_temp_free(t0);
3169     tcg_temp_free_i32(t1);
3170 }
3171
3172 /***                    Integer load and store strings                     ***/
3173
3174 /* lswi */
3175 /* PowerPC32 specification says we must generate an exception if
3176  * rA is in the range of registers to be loaded.
3177  * In an other hand, IBM says this is valid, but rA won't be loaded.
3178  * For now, I'll follow the spec...
3179  */
3180 static void gen_lswi(DisasContext *ctx)
3181 {
3182     TCGv t0;
3183     TCGv_i32 t1, t2;
3184     int nb = NB(ctx->opcode);
3185     int start = rD(ctx->opcode);
3186     int ra = rA(ctx->opcode);
3187     int nr;
3188
3189     if (nb == 0)
3190         nb = 32;
3191     nr = nb / 4;
3192     if (unlikely(((start + nr) > 32  &&
3193                   start <= ra && (start + nr - 32) > ra) ||
3194                  ((start + nr) <= 32 && start <= ra && (start + nr) > ra))) {
3195         gen_inval_exception(ctx, POWERPC_EXCP_INVAL_LSWX);
3196         return;
3197     }
3198     gen_set_access_type(ctx, ACCESS_INT);
3199     /* NIP cannot be restored if the memory exception comes from an helper */
3200     gen_update_nip(ctx, ctx->nip - 4);
3201     t0 = tcg_temp_new();
3202     gen_addr_register(ctx, t0);
3203     t1 = tcg_const_i32(nb);
3204     t2 = tcg_const_i32(start);
3205     gen_helper_lsw(cpu_env, t0, t1, t2);
3206     tcg_temp_free(t0);
3207     tcg_temp_free_i32(t1);
3208     tcg_temp_free_i32(t2);
3209 }
3210
3211 /* lswx */
3212 static void gen_lswx(DisasContext *ctx)
3213 {
3214     TCGv t0;
3215     TCGv_i32 t1, t2, t3;
3216     gen_set_access_type(ctx, ACCESS_INT);
3217     /* NIP cannot be restored if the memory exception comes from an helper */
3218     gen_update_nip(ctx, ctx->nip - 4);
3219     t0 = tcg_temp_new();
3220     gen_addr_reg_index(ctx, t0);
3221     t1 = tcg_const_i32(rD(ctx->opcode));
3222     t2 = tcg_const_i32(rA(ctx->opcode));
3223     t3 = tcg_const_i32(rB(ctx->opcode));
3224     gen_helper_lswx(cpu_env, t0, t1, t2, t3);
3225     tcg_temp_free(t0);
3226     tcg_temp_free_i32(t1);
3227     tcg_temp_free_i32(t2);
3228     tcg_temp_free_i32(t3);
3229 }
3230
3231 /* stswi */
3232 static void gen_stswi(DisasContext *ctx)
3233 {
3234     TCGv t0;
3235     TCGv_i32 t1, t2;
3236     int nb = NB(ctx->opcode);
3237     gen_set_access_type(ctx, ACCESS_INT);
3238     /* NIP cannot be restored if the memory exception comes from an helper */
3239     gen_update_nip(ctx, ctx->nip - 4);
3240     t0 = tcg_temp_new();
3241     gen_addr_register(ctx, t0);
3242     if (nb == 0)
3243         nb = 32;
3244     t1 = tcg_const_i32(nb);
3245     t2 = tcg_const_i32(rS(ctx->opcode));
3246     gen_helper_stsw(cpu_env, t0, t1, t2);
3247     tcg_temp_free(t0);
3248     tcg_temp_free_i32(t1);
3249     tcg_temp_free_i32(t2);
3250 }
3251
3252 /* stswx */
3253 static void gen_stswx(DisasContext *ctx)
3254 {
3255     TCGv t0;
3256     TCGv_i32 t1, t2;
3257     gen_set_access_type(ctx, ACCESS_INT);
3258     /* NIP cannot be restored if the memory exception comes from an helper */
3259     gen_update_nip(ctx, ctx->nip - 4);
3260     t0 = tcg_temp_new();
3261     gen_addr_reg_index(ctx, t0);
3262     t1 = tcg_temp_new_i32();
3263     tcg_gen_trunc_tl_i32(t1, cpu_xer);
3264     tcg_gen_andi_i32(t1, t1, 0x7F);
3265     t2 = tcg_const_i32(rS(ctx->opcode));
3266     gen_helper_stsw(cpu_env, t0, t1, t2);
3267     tcg_temp_free(t0);
3268     tcg_temp_free_i32(t1);
3269     tcg_temp_free_i32(t2);
3270 }
3271
3272 /***                        Memory synchronisation                         ***/
3273 /* eieio */
3274 static void gen_eieio(DisasContext *ctx)
3275 {
3276 }
3277
3278 /* isync */
3279 static void gen_isync(DisasContext *ctx)
3280 {
3281     gen_stop_exception(ctx);
3282 }
3283
3284 #define LARX(name, len, loadop)                                      \
3285 static void gen_##name(DisasContext *ctx)                            \
3286 {                                                                    \
3287     TCGv t0;                                                         \
3288     TCGv gpr = cpu_gpr[rD(ctx->opcode)];                             \
3289     gen_set_access_type(ctx, ACCESS_RES);                            \
3290     t0 = tcg_temp_local_new();                                       \
3291     gen_addr_reg_index(ctx, t0);                                     \
3292     if ((len) > 1) {                                                 \
3293         gen_check_align(ctx, t0, (len)-1);                           \
3294     }                                                                \
3295     gen_qemu_##loadop(ctx, gpr, t0);                                 \
3296     tcg_gen_mov_tl(cpu_reserve, t0);                                 \
3297     tcg_gen_st_tl(gpr, cpu_env, offsetof(CPUPPCState, reserve_val)); \
3298     tcg_temp_free(t0);                                               \
3299 }
3300
3301 /* lwarx */
3302 LARX(lbarx, 1, ld8u);
3303 LARX(lharx, 2, ld16u);
3304 LARX(lwarx, 4, ld32u);
3305
3306
3307 #if defined(CONFIG_USER_ONLY)
3308 static void gen_conditional_store(DisasContext *ctx, TCGv EA,
3309                                   int reg, int size)
3310 {
3311     TCGv t0 = tcg_temp_new();
3312     uint32_t save_exception = ctx->exception;
3313
3314     tcg_gen_st_tl(EA, cpu_env, offsetof(CPUPPCState, reserve_ea));
3315     tcg_gen_movi_tl(t0, (size << 5) | reg);
3316     tcg_gen_st_tl(t0, cpu_env, offsetof(CPUPPCState, reserve_info));
3317     tcg_temp_free(t0);
3318     gen_update_nip(ctx, ctx->nip-4);
3319     ctx->exception = POWERPC_EXCP_BRANCH;
3320     gen_exception(ctx, POWERPC_EXCP_STCX);
3321     ctx->exception = save_exception;
3322 }
3323 #else
3324 static void gen_conditional_store(DisasContext *ctx, TCGv EA,
3325                                   int reg, int size)
3326 {
3327     int l1;
3328
3329     tcg_gen_trunc_tl_i32(cpu_crf[0], cpu_so);
3330     l1 = gen_new_label();
3331     tcg_gen_brcond_tl(TCG_COND_NE, EA, cpu_reserve, l1);
3332     tcg_gen_ori_i32(cpu_crf[0], cpu_crf[0], 1 << CRF_EQ);
3333 #if defined(TARGET_PPC64)
3334     if (size == 8) {
3335         gen_qemu_st64(ctx, cpu_gpr[reg], EA);
3336     } else
3337 #endif
3338     if (size == 4) {
3339         gen_qemu_st32(ctx, cpu_gpr[reg], EA);
3340     } else if (size == 2) {
3341         gen_qemu_st16(ctx, cpu_gpr[reg], EA);
3342 #if defined(TARGET_PPC64)
3343     } else if (size == 16) {
3344         TCGv gpr1, gpr2 , EA8;
3345         if (unlikely(ctx->le_mode)) {
3346             gpr1 = cpu_gpr[reg+1];
3347             gpr2 = cpu_gpr[reg];
3348         } else {
3349             gpr1 = cpu_gpr[reg];
3350             gpr2 = cpu_gpr[reg+1];
3351         }
3352         gen_qemu_st64(ctx, gpr1, EA);
3353         EA8 = tcg_temp_local_new();
3354         gen_addr_add(ctx, EA8, EA, 8);
3355         gen_qemu_st64(ctx, gpr2, EA8);
3356         tcg_temp_free(EA8);
3357 #endif
3358     } else {
3359         gen_qemu_st8(ctx, cpu_gpr[reg], EA);
3360     }
3361     gen_set_label(l1);
3362     tcg_gen_movi_tl(cpu_reserve, -1);
3363 }
3364 #endif
3365
3366 #define STCX(name, len)                                   \
3367 static void gen_##name(DisasContext *ctx)                 \
3368 {                                                         \
3369     TCGv t0;                                              \
3370     if (unlikely((len == 16) && (rD(ctx->opcode) & 1))) { \
3371         gen_inval_exception(ctx,                          \
3372                             POWERPC_EXCP_INVAL_INVAL);    \
3373         return;                                           \
3374     }                                                     \
3375     gen_set_access_type(ctx, ACCESS_RES);                 \
3376     t0 = tcg_temp_local_new();                            \
3377     gen_addr_reg_index(ctx, t0);                          \
3378     if (len > 1) {                                        \
3379         gen_check_align(ctx, t0, (len)-1);                \
3380     }                                                     \
3381     gen_conditional_store(ctx, t0, rS(ctx->opcode), len); \
3382     tcg_temp_free(t0);                                    \
3383 }
3384
3385 STCX(stbcx_, 1);
3386 STCX(sthcx_, 2);
3387 STCX(stwcx_, 4);
3388
3389 #if defined(TARGET_PPC64)
3390 /* ldarx */
3391 LARX(ldarx, 8, ld64);
3392
3393 /* lqarx */
3394 static void gen_lqarx(DisasContext *ctx)
3395 {
3396     TCGv EA;
3397     int rd = rD(ctx->opcode);
3398     TCGv gpr1, gpr2;
3399
3400     if (unlikely((rd & 1) || (rd == rA(ctx->opcode)) ||
3401                  (rd == rB(ctx->opcode)))) {
3402         gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL);
3403         return;
3404     }
3405
3406     gen_set_access_type(ctx, ACCESS_RES);
3407     EA = tcg_temp_local_new();
3408     gen_addr_reg_index(ctx, EA);
3409     gen_check_align(ctx, EA, 15);
3410     if (unlikely(ctx->le_mode)) {
3411         gpr1 = cpu_gpr[rd+1];
3412         gpr2 = cpu_gpr[rd];
3413     } else {
3414         gpr1 = cpu_gpr[rd];
3415         gpr2 = cpu_gpr[rd+1];
3416     }
3417     gen_qemu_ld64(ctx, gpr1, EA);
3418     tcg_gen_mov_tl(cpu_reserve, EA);
3419
3420     gen_addr_add(ctx, EA, EA, 8);
3421     gen_qemu_ld64(ctx, gpr2, EA);
3422
3423     tcg_gen_st_tl(gpr1, cpu_env, offsetof(CPUPPCState, reserve_val));
3424     tcg_gen_st_tl(gpr2, cpu_env, offsetof(CPUPPCState, reserve_val2));
3425
3426     tcg_temp_free(EA);
3427 }
3428
3429 /* stdcx. */
3430 STCX(stdcx_, 8);
3431 STCX(stqcx_, 16);
3432 #endif /* defined(TARGET_PPC64) */
3433
3434 /* sync */
3435 static void gen_sync(DisasContext *ctx)
3436 {
3437 }
3438
3439 /* wait */
3440 static void gen_wait(DisasContext *ctx)
3441 {
3442     TCGv_i32 t0 = tcg_temp_new_i32();
3443     tcg_gen_st_i32(t0, cpu_env,
3444                    -offsetof(PowerPCCPU, env) + offsetof(CPUState, halted));
3445     tcg_temp_free_i32(t0);
3446     /* Stop translation, as the CPU is supposed to sleep from now */
3447     gen_exception_err(ctx, EXCP_HLT, 1);
3448 }
3449
3450 /***                         Floating-point load                           ***/
3451 #define GEN_LDF(name, ldop, opc, type)                                        \
3452 static void glue(gen_, name)(DisasContext *ctx)                                       \
3453 {                                                                             \
3454     TCGv EA;                                                                  \
3455     if (unlikely(!ctx->fpu_enabled)) {                                        \
3456         gen_exception(ctx, POWERPC_EXCP_FPU);                                 \
3457         return;                                                               \
3458     }                                                                         \
3459     gen_set_access_type(ctx, ACCESS_FLOAT);                                   \
3460     EA = tcg_temp_new();                                                      \
3461     gen_addr_imm_index(ctx, EA, 0);                                           \
3462     gen_qemu_##ldop(ctx, cpu_fpr[rD(ctx->opcode)], EA);                       \
3463     tcg_temp_free(EA);                                                        \
3464 }
3465
3466 #define GEN_LDUF(name, ldop, opc, type)                                       \
3467 static void glue(gen_, name##u)(DisasContext *ctx)                                    \
3468 {                                                                             \
3469     TCGv EA;                                                                  \
3470     if (unlikely(!ctx->fpu_enabled)) {                                        \
3471         gen_exception(ctx, POWERPC_EXCP_FPU);                                 \
3472         return;                                                               \
3473     }                                                                         \
3474     if (unlikely(rA(ctx->opcode) == 0)) {                                     \
3475         gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL);                   \
3476         return;                                                               \
3477     }                                                                         \
3478     gen_set_access_type(ctx, ACCESS_FLOAT);                                   \
3479     EA = tcg_temp_new();                                                      \
3480     gen_addr_imm_index(ctx, EA, 0);                                           \
3481     gen_qemu_##ldop(ctx, cpu_fpr[rD(ctx->opcode)], EA);                       \
3482     tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], EA);                             \
3483     tcg_temp_free(EA);                                                        \
3484 }
3485
3486 #define GEN_LDUXF(name, ldop, opc, type)                                      \
3487 static void glue(gen_, name##ux)(DisasContext *ctx)                                   \
3488 {                                                                             \
3489     TCGv EA;                                                                  \
3490     if (unlikely(!ctx->fpu_enabled)) {                                        \
3491         gen_exception(ctx, POWERPC_EXCP_FPU);                                 \
3492         return;                                                               \
3493     }                                                                         \
3494     if (unlikely(rA(ctx->opcode) == 0)) {                                     \
3495         gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL);                   \
3496         return;                                                               \
3497     }                                                                         \
3498     gen_set_access_type(ctx, ACCESS_FLOAT);                                   \
3499     EA = tcg_temp_new();                                                      \
3500     gen_addr_reg_index(ctx, EA);                                              \
3501     gen_qemu_##ldop(ctx, cpu_fpr[rD(ctx->opcode)], EA);                       \
3502     tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], EA);                             \
3503     tcg_temp_free(EA);                                                        \
3504 }
3505
3506 #define GEN_LDXF(name, ldop, opc2, opc3, type)                                \
3507 static void glue(gen_, name##x)(DisasContext *ctx)                                    \
3508 {                                                                             \
3509     TCGv EA;                                                                  \
3510     if (unlikely(!ctx->fpu_enabled)) {                                        \
3511         gen_exception(ctx, POWERPC_EXCP_FPU);                                 \
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_temp_free(EA);                                                        \
3519 }
3520
3521 #define GEN_LDFS(name, ldop, op, type)                                        \
3522 GEN_LDF(name, ldop, op | 0x20, type);                                         \
3523 GEN_LDUF(name, ldop, op | 0x21, type);                                        \
3524 GEN_LDUXF(name, ldop, op | 0x01, type);                                       \
3525 GEN_LDXF(name, ldop, 0x17, op | 0x00, type)
3526
3527 static inline void gen_qemu_ld32fs(DisasContext *ctx, TCGv_i64 arg1, TCGv arg2)
3528 {
3529     TCGv t0 = tcg_temp_new();
3530     TCGv_i32 t1 = tcg_temp_new_i32();
3531     gen_qemu_ld32u(ctx, t0, arg2);
3532     tcg_gen_trunc_tl_i32(t1, t0);
3533     tcg_temp_free(t0);
3534     gen_helper_float32_to_float64(arg1, cpu_env, t1);
3535     tcg_temp_free_i32(t1);
3536 }
3537
3538  /* lfd lfdu lfdux lfdx */
3539 GEN_LDFS(lfd, ld64, 0x12, PPC_FLOAT);
3540  /* lfs lfsu lfsux lfsx */
3541 GEN_LDFS(lfs, ld32fs, 0x10, PPC_FLOAT);
3542
3543 /* lfdp */
3544 static void gen_lfdp(DisasContext *ctx)
3545 {
3546     TCGv EA;
3547     if (unlikely(!ctx->fpu_enabled)) {
3548         gen_exception(ctx, POWERPC_EXCP_FPU);
3549         return;
3550     }
3551     gen_set_access_type(ctx, ACCESS_FLOAT);
3552     EA = tcg_temp_new();
3553     gen_addr_imm_index(ctx, EA, 0);                                           \
3554     if (unlikely(ctx->le_mode)) {
3555         gen_qemu_ld64(ctx, cpu_fpr[rD(ctx->opcode) + 1], EA);
3556         tcg_gen_addi_tl(EA, EA, 8);
3557         gen_qemu_ld64(ctx, cpu_fpr[rD(ctx->opcode)], EA);
3558     } else {
3559         gen_qemu_ld64(ctx, cpu_fpr[rD(ctx->opcode)], EA);
3560         tcg_gen_addi_tl(EA, EA, 8);
3561         gen_qemu_ld64(ctx, cpu_fpr[rD(ctx->opcode) + 1], EA);
3562     }
3563     tcg_temp_free(EA);
3564 }
3565
3566 /* lfdpx */
3567 static void gen_lfdpx(DisasContext *ctx)
3568 {
3569     TCGv EA;
3570     if (unlikely(!ctx->fpu_enabled)) {
3571         gen_exception(ctx, POWERPC_EXCP_FPU);
3572         return;
3573     }
3574     gen_set_access_type(ctx, ACCESS_FLOAT);
3575     EA = tcg_temp_new();
3576     gen_addr_reg_index(ctx, EA);
3577     if (unlikely(ctx->le_mode)) {
3578         gen_qemu_ld64(ctx, cpu_fpr[rD(ctx->opcode) + 1], EA);
3579         tcg_gen_addi_tl(EA, EA, 8);
3580         gen_qemu_ld64(ctx, cpu_fpr[rD(ctx->opcode)], EA);
3581     } else {
3582         gen_qemu_ld64(ctx, cpu_fpr[rD(ctx->opcode)], EA);
3583         tcg_gen_addi_tl(EA, EA, 8);
3584         gen_qemu_ld64(ctx, cpu_fpr[rD(ctx->opcode) + 1], EA);
3585     }
3586     tcg_temp_free(EA);
3587 }
3588
3589 /* lfiwax */
3590 static void gen_lfiwax(DisasContext *ctx)
3591 {
3592     TCGv EA;
3593     TCGv t0;
3594     if (unlikely(!ctx->fpu_enabled)) {
3595         gen_exception(ctx, POWERPC_EXCP_FPU);
3596         return;
3597     }
3598     gen_set_access_type(ctx, ACCESS_FLOAT);
3599     EA = tcg_temp_new();
3600     t0 = tcg_temp_new();
3601     gen_addr_reg_index(ctx, EA);
3602     gen_qemu_ld32s(ctx, t0, EA);
3603     tcg_gen_ext_tl_i64(cpu_fpr[rD(ctx->opcode)], t0);
3604     tcg_temp_free(EA);
3605     tcg_temp_free(t0);
3606 }
3607
3608 /* lfiwzx */
3609 static void gen_lfiwzx(DisasContext *ctx)
3610 {
3611     TCGv EA;
3612     if (unlikely(!ctx->fpu_enabled)) {
3613         gen_exception(ctx, POWERPC_EXCP_FPU);
3614         return;
3615     }
3616     gen_set_access_type(ctx, ACCESS_FLOAT);
3617     EA = tcg_temp_new();
3618     gen_addr_reg_index(ctx, EA);
3619     gen_qemu_ld32u_i64(ctx, cpu_fpr[rD(ctx->opcode)], EA);
3620     tcg_temp_free(EA);
3621 }
3622 /***                         Floating-point store                          ***/
3623 #define GEN_STF(name, stop, opc, type)                                        \
3624 static void glue(gen_, name)(DisasContext *ctx)                                       \
3625 {                                                                             \
3626     TCGv EA;                                                                  \
3627     if (unlikely(!ctx->fpu_enabled)) {                                        \
3628         gen_exception(ctx, POWERPC_EXCP_FPU);                                 \
3629         return;                                                               \
3630     }                                                                         \
3631     gen_set_access_type(ctx, ACCESS_FLOAT);                                   \
3632     EA = tcg_temp_new();                                                      \
3633     gen_addr_imm_index(ctx, EA, 0);                                           \
3634     gen_qemu_##stop(ctx, cpu_fpr[rS(ctx->opcode)], EA);                       \
3635     tcg_temp_free(EA);                                                        \
3636 }
3637
3638 #define GEN_STUF(name, stop, opc, type)                                       \
3639 static void glue(gen_, name##u)(DisasContext *ctx)                                    \
3640 {                                                                             \
3641     TCGv EA;                                                                  \
3642     if (unlikely(!ctx->fpu_enabled)) {                                        \
3643         gen_exception(ctx, POWERPC_EXCP_FPU);                                 \
3644         return;                                                               \
3645     }                                                                         \
3646     if (unlikely(rA(ctx->opcode) == 0)) {                                     \
3647         gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL);                   \
3648         return;                                                               \
3649     }                                                                         \
3650     gen_set_access_type(ctx, ACCESS_FLOAT);                                   \
3651     EA = tcg_temp_new();                                                      \
3652     gen_addr_imm_index(ctx, EA, 0);                                           \
3653     gen_qemu_##stop(ctx, cpu_fpr[rS(ctx->opcode)], EA);                       \
3654     tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], EA);                             \
3655     tcg_temp_free(EA);                                                        \
3656 }
3657
3658 #define GEN_STUXF(name, stop, opc, type)                                      \
3659 static void glue(gen_, name##ux)(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_reg_index(ctx, EA);                                              \
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_STXF(name, stop, opc2, opc3, type)                                \
3679 static void glue(gen_, name##x)(DisasContext *ctx)                                    \
3680 {                                                                             \
3681     TCGv EA;                                                                  \
3682     if (unlikely(!ctx->fpu_enabled)) {                                        \
3683         gen_exception(ctx, POWERPC_EXCP_FPU);                                 \
3684         return;                                                               \
3685     }                                                                         \
3686     gen_set_access_type(ctx, ACCESS_FLOAT);                                   \
3687     EA = tcg_temp_new();                                                      \
3688     gen_addr_reg_index(ctx, EA);                                              \
3689     gen_qemu_##stop(ctx, cpu_fpr[rS(ctx->opcode)], EA);                       \
3690     tcg_temp_free(EA);                                                        \
3691 }
3692
3693 #define GEN_STFS(name, stop, op, type)                                        \
3694 GEN_STF(name, stop, op | 0x20, type);                                         \
3695 GEN_STUF(name, stop, op | 0x21, type);                                        \
3696 GEN_STUXF(name, stop, op | 0x01, type);                                       \
3697 GEN_STXF(name, stop, 0x17, op | 0x00, type)
3698
3699 static inline void gen_qemu_st32fs(DisasContext *ctx, TCGv_i64 arg1, TCGv arg2)
3700 {
3701     TCGv_i32 t0 = tcg_temp_new_i32();
3702     TCGv t1 = tcg_temp_new();
3703     gen_helper_float64_to_float32(t0, cpu_env, arg1);
3704     tcg_gen_extu_i32_tl(t1, t0);
3705     tcg_temp_free_i32(t0);
3706     gen_qemu_st32(ctx, t1, arg2);
3707     tcg_temp_free(t1);
3708 }
3709
3710 /* stfd stfdu stfdux stfdx */
3711 GEN_STFS(stfd, st64, 0x16, PPC_FLOAT);
3712 /* stfs stfsu stfsux stfsx */
3713 GEN_STFS(stfs, st32fs, 0x14, PPC_FLOAT);
3714
3715 /* stfdp */
3716 static void gen_stfdp(DisasContext *ctx)
3717 {
3718     TCGv EA;
3719     if (unlikely(!ctx->fpu_enabled)) {
3720         gen_exception(ctx, POWERPC_EXCP_FPU);
3721         return;
3722     }
3723     gen_set_access_type(ctx, ACCESS_FLOAT);
3724     EA = tcg_temp_new();
3725     gen_addr_imm_index(ctx, EA, 0);                                           \
3726     if (unlikely(ctx->le_mode)) {
3727         gen_qemu_st64(ctx, cpu_fpr[rD(ctx->opcode) + 1], EA);
3728         tcg_gen_addi_tl(EA, EA, 8);
3729         gen_qemu_st64(ctx, cpu_fpr[rD(ctx->opcode)], EA);
3730     } else {
3731         gen_qemu_st64(ctx, cpu_fpr[rD(ctx->opcode)], EA);
3732         tcg_gen_addi_tl(EA, EA, 8);
3733         gen_qemu_st64(ctx, cpu_fpr[rD(ctx->opcode) + 1], EA);
3734     }
3735     tcg_temp_free(EA);
3736 }
3737
3738 /* stfdpx */
3739 static void gen_stfdpx(DisasContext *ctx)
3740 {
3741     TCGv EA;
3742     if (unlikely(!ctx->fpu_enabled)) {
3743         gen_exception(ctx, POWERPC_EXCP_FPU);
3744         return;
3745     }
3746     gen_set_access_type(ctx, ACCESS_FLOAT);
3747     EA = tcg_temp_new();
3748     gen_addr_reg_index(ctx, EA);
3749     if (unlikely(ctx->le_mode)) {
3750         gen_qemu_st64(ctx, cpu_fpr[rD(ctx->opcode) + 1], EA);
3751         tcg_gen_addi_tl(EA, EA, 8);
3752         gen_qemu_st64(ctx, cpu_fpr[rD(ctx->opcode)], EA);
3753     } else {
3754         gen_qemu_st64(ctx, cpu_fpr[rD(ctx->opcode)], EA);
3755         tcg_gen_addi_tl(EA, EA, 8);
3756         gen_qemu_st64(ctx, cpu_fpr[rD(ctx->opcode) + 1], EA);
3757     }
3758     tcg_temp_free(EA);
3759 }
3760
3761 /* Optional: */
3762 static inline void gen_qemu_st32fiw(DisasContext *ctx, TCGv_i64 arg1, TCGv arg2)
3763 {
3764     TCGv t0 = tcg_temp_new();
3765     tcg_gen_trunc_i64_tl(t0, arg1),
3766     gen_qemu_st32(ctx, t0, arg2);
3767     tcg_temp_free(t0);
3768 }
3769 /* stfiwx */
3770 GEN_STXF(stfiw, st32fiw, 0x17, 0x1E, PPC_FLOAT_STFIWX);
3771
3772 static inline void gen_update_cfar(DisasContext *ctx, target_ulong nip)
3773 {
3774 #if defined(TARGET_PPC64)
3775     if (ctx->has_cfar)
3776         tcg_gen_movi_tl(cpu_cfar, nip);
3777 #endif
3778 }
3779
3780 /***                                Branch                                 ***/
3781 static inline void gen_goto_tb(DisasContext *ctx, int n, target_ulong dest)
3782 {
3783     TranslationBlock *tb;
3784     tb = ctx->tb;
3785     if (NARROW_MODE(ctx)) {
3786         dest = (uint32_t) dest;
3787     }
3788     if ((tb->pc & TARGET_PAGE_MASK) == (dest & TARGET_PAGE_MASK) &&
3789         likely(!ctx->singlestep_enabled)) {
3790         tcg_gen_goto_tb(n);
3791         tcg_gen_movi_tl(cpu_nip, dest & ~3);
3792         tcg_gen_exit_tb((uintptr_t)tb + n);
3793     } else {
3794         tcg_gen_movi_tl(cpu_nip, dest & ~3);
3795         if (unlikely(ctx->singlestep_enabled)) {
3796             if ((ctx->singlestep_enabled &
3797                 (CPU_BRANCH_STEP | CPU_SINGLE_STEP)) &&
3798                 (ctx->exception == POWERPC_EXCP_BRANCH ||
3799                  ctx->exception == POWERPC_EXCP_TRACE)) {
3800                 target_ulong tmp = ctx->nip;
3801                 ctx->nip = dest;
3802                 gen_exception(ctx, POWERPC_EXCP_TRACE);
3803                 ctx->nip = tmp;
3804             }
3805             if (ctx->singlestep_enabled & GDBSTUB_SINGLE_STEP) {
3806                 gen_debug_exception(ctx);
3807             }
3808         }
3809         tcg_gen_exit_tb(0);
3810     }
3811 }
3812
3813 static inline void gen_setlr(DisasContext *ctx, target_ulong nip)
3814 {
3815     if (NARROW_MODE(ctx)) {
3816         nip = (uint32_t)nip;
3817     }
3818     tcg_gen_movi_tl(cpu_lr, nip);
3819 }
3820
3821 /* b ba bl bla */
3822 static void gen_b(DisasContext *ctx)
3823 {
3824     target_ulong li, target;
3825
3826     ctx->exception = POWERPC_EXCP_BRANCH;
3827     /* sign extend LI */
3828     li = LI(ctx->opcode);
3829     li = (li ^ 0x02000000) - 0x02000000;
3830     if (likely(AA(ctx->opcode) == 0)) {
3831         target = ctx->nip + li - 4;
3832     } else {
3833         target = li;
3834     }
3835     if (LK(ctx->opcode)) {
3836         gen_setlr(ctx, ctx->nip);
3837     }
3838     gen_update_cfar(ctx, ctx->nip);
3839     gen_goto_tb(ctx, 0, target);
3840 }
3841
3842 #define BCOND_IM  0
3843 #define BCOND_LR  1
3844 #define BCOND_CTR 2
3845 #define BCOND_TAR 3
3846
3847 static inline void gen_bcond(DisasContext *ctx, int type)
3848 {
3849     uint32_t bo = BO(ctx->opcode);
3850     int l1;
3851     TCGv target;
3852
3853     ctx->exception = POWERPC_EXCP_BRANCH;
3854     if (type == BCOND_LR || type == BCOND_CTR || type == BCOND_TAR) {
3855         target = tcg_temp_local_new();
3856         if (type == BCOND_CTR)
3857             tcg_gen_mov_tl(target, cpu_ctr);
3858         else if (type == BCOND_TAR)
3859             gen_load_spr(target, SPR_TAR);
3860         else
3861             tcg_gen_mov_tl(target, cpu_lr);
3862     } else {
3863         TCGV_UNUSED(target);
3864     }
3865     if (LK(ctx->opcode))
3866         gen_setlr(ctx, ctx->nip);
3867     l1 = gen_new_label();
3868     if ((bo & 0x4) == 0) {
3869         /* Decrement and test CTR */
3870         TCGv temp = tcg_temp_new();
3871         if (unlikely(type == BCOND_CTR)) {
3872             gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL);
3873             return;
3874         }
3875         tcg_gen_subi_tl(cpu_ctr, cpu_ctr, 1);
3876         if (NARROW_MODE(ctx)) {
3877             tcg_gen_ext32u_tl(temp, cpu_ctr);
3878         } else {
3879             tcg_gen_mov_tl(temp, cpu_ctr);
3880         }
3881         if (bo & 0x2) {
3882             tcg_gen_brcondi_tl(TCG_COND_NE, temp, 0, l1);
3883         } else {
3884             tcg_gen_brcondi_tl(TCG_COND_EQ, temp, 0, l1);
3885         }
3886         tcg_temp_free(temp);
3887     }
3888     if ((bo & 0x10) == 0) {
3889         /* Test CR */
3890         uint32_t bi = BI(ctx->opcode);
3891         uint32_t mask = 1 << (3 - (bi & 0x03));
3892         TCGv_i32 temp = tcg_temp_new_i32();
3893
3894         if (bo & 0x8) {
3895             tcg_gen_andi_i32(temp, cpu_crf[bi >> 2], mask);
3896             tcg_gen_brcondi_i32(TCG_COND_EQ, temp, 0, l1);
3897         } else {
3898             tcg_gen_andi_i32(temp, cpu_crf[bi >> 2], mask);
3899             tcg_gen_brcondi_i32(TCG_COND_NE, temp, 0, l1);
3900         }
3901         tcg_temp_free_i32(temp);
3902     }
3903     gen_update_cfar(ctx, ctx->nip);
3904     if (type == BCOND_IM) {
3905         target_ulong li = (target_long)((int16_t)(BD(ctx->opcode)));
3906         if (likely(AA(ctx->opcode) == 0)) {
3907             gen_goto_tb(ctx, 0, ctx->nip + li - 4);
3908         } else {
3909             gen_goto_tb(ctx, 0, li);
3910         }
3911         gen_set_label(l1);
3912         gen_goto_tb(ctx, 1, ctx->nip);
3913     } else {
3914         if (NARROW_MODE(ctx)) {
3915             tcg_gen_andi_tl(cpu_nip, target, (uint32_t)~3);
3916         } else {
3917             tcg_gen_andi_tl(cpu_nip, target, ~3);
3918         }
3919         tcg_gen_exit_tb(0);
3920         gen_set_label(l1);
3921         gen_update_nip(ctx, ctx->nip);
3922         tcg_gen_exit_tb(0);
3923     }
3924     if (type == BCOND_LR || type == BCOND_CTR) {
3925         tcg_temp_free(target);
3926     }
3927 }
3928
3929 static void gen_bc(DisasContext *ctx)
3930 {
3931     gen_bcond(ctx, BCOND_IM);
3932 }
3933
3934 static void gen_bcctr(DisasContext *ctx)
3935 {
3936     gen_bcond(ctx, BCOND_CTR);
3937 }
3938
3939 static void gen_bclr(DisasContext *ctx)
3940 {
3941     gen_bcond(ctx, BCOND_LR);
3942 }
3943
3944 static void gen_bctar(DisasContext *ctx)
3945 {
3946     gen_bcond(ctx, BCOND_TAR);
3947 }
3948
3949 /***                      Condition register logical                       ***/
3950 #define GEN_CRLOGIC(name, tcg_op, opc)                                        \
3951 static void glue(gen_, name)(DisasContext *ctx)                                       \
3952 {                                                                             \
3953     uint8_t bitmask;                                                          \
3954     int sh;                                                                   \
3955     TCGv_i32 t0, t1;                                                          \
3956     sh = (crbD(ctx->opcode) & 0x03) - (crbA(ctx->opcode) & 0x03);             \
3957     t0 = tcg_temp_new_i32();                                                  \
3958     if (sh > 0)                                                               \
3959         tcg_gen_shri_i32(t0, cpu_crf[crbA(ctx->opcode) >> 2], sh);            \
3960     else if (sh < 0)                                                          \
3961         tcg_gen_shli_i32(t0, cpu_crf[crbA(ctx->opcode) >> 2], -sh);           \
3962     else                                                                      \
3963         tcg_gen_mov_i32(t0, cpu_crf[crbA(ctx->opcode) >> 2]);                 \
3964     t1 = tcg_temp_new_i32();                                                  \
3965     sh = (crbD(ctx->opcode) & 0x03) - (crbB(ctx->opcode) & 0x03);             \
3966     if (sh > 0)                                                               \
3967         tcg_gen_shri_i32(t1, cpu_crf[crbB(ctx->opcode) >> 2], sh);            \
3968     else if (sh < 0)                                                          \
3969         tcg_gen_shli_i32(t1, cpu_crf[crbB(ctx->opcode) >> 2], -sh);           \
3970     else                                                                      \
3971         tcg_gen_mov_i32(t1, cpu_crf[crbB(ctx->opcode) >> 2]);                 \
3972     tcg_op(t0, t0, t1);                                                       \
3973     bitmask = 1 << (3 - (crbD(ctx->opcode) & 0x03));                          \
3974     tcg_gen_andi_i32(t0, t0, bitmask);                                        \
3975     tcg_gen_andi_i32(t1, cpu_crf[crbD(ctx->opcode) >> 2], ~bitmask);          \
3976     tcg_gen_or_i32(cpu_crf[crbD(ctx->opcode) >> 2], t0, t1);                  \
3977     tcg_temp_free_i32(t0);                                                    \
3978     tcg_temp_free_i32(t1);                                                    \
3979 }
3980
3981 /* crand */
3982 GEN_CRLOGIC(crand, tcg_gen_and_i32, 0x08);
3983 /* crandc */
3984 GEN_CRLOGIC(crandc, tcg_gen_andc_i32, 0x04);
3985 /* creqv */
3986 GEN_CRLOGIC(creqv, tcg_gen_eqv_i32, 0x09);
3987 /* crnand */
3988 GEN_CRLOGIC(crnand, tcg_gen_nand_i32, 0x07);
3989 /* crnor */
3990 GEN_CRLOGIC(crnor, tcg_gen_nor_i32, 0x01);
3991 /* cror */
3992 GEN_CRLOGIC(cror, tcg_gen_or_i32, 0x0E);
3993 /* crorc */
3994 GEN_CRLOGIC(crorc, tcg_gen_orc_i32, 0x0D);
3995 /* crxor */
3996 GEN_CRLOGIC(crxor, tcg_gen_xor_i32, 0x06);
3997
3998 /* mcrf */
3999 static void gen_mcrf(DisasContext *ctx)
4000 {
4001     tcg_gen_mov_i32(cpu_crf[crfD(ctx->opcode)], cpu_crf[crfS(ctx->opcode)]);
4002 }
4003
4004 /***                           System linkage                              ***/
4005
4006 /* rfi (mem_idx only) */
4007 static void gen_rfi(DisasContext *ctx)
4008 {
4009 #if defined(CONFIG_USER_ONLY)
4010     gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
4011 #else
4012     /* Restore CPU state */
4013     if (unlikely(!ctx->mem_idx)) {
4014         gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
4015         return;
4016     }
4017     gen_update_cfar(ctx, ctx->nip);
4018     gen_helper_rfi(cpu_env);
4019     gen_sync_exception(ctx);
4020 #endif
4021 }
4022
4023 #if defined(TARGET_PPC64)
4024 static void gen_rfid(DisasContext *ctx)
4025 {
4026 #if defined(CONFIG_USER_ONLY)
4027     gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
4028 #else
4029     /* Restore CPU state */
4030     if (unlikely(!ctx->mem_idx)) {
4031         gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
4032         return;
4033     }
4034     gen_update_cfar(ctx, ctx->nip);
4035     gen_helper_rfid(cpu_env);
4036     gen_sync_exception(ctx);
4037 #endif
4038 }
4039
4040 static void gen_hrfid(DisasContext *ctx)
4041 {
4042 #if defined(CONFIG_USER_ONLY)
4043     gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
4044 #else
4045     /* Restore CPU state */
4046     if (unlikely(ctx->mem_idx <= 1)) {
4047         gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
4048         return;
4049     }
4050     gen_helper_hrfid(cpu_env);
4051     gen_sync_exception(ctx);
4052 #endif
4053 }
4054 #endif
4055
4056 /* sc */
4057 #if defined(CONFIG_USER_ONLY)
4058 #define POWERPC_SYSCALL POWERPC_EXCP_SYSCALL_USER
4059 #else
4060 #define POWERPC_SYSCALL POWERPC_EXCP_SYSCALL
4061 #endif
4062 static void gen_sc(DisasContext *ctx)
4063 {
4064     uint32_t lev;
4065
4066     lev = (ctx->opcode >> 5) & 0x7F;
4067     gen_exception_err(ctx, POWERPC_SYSCALL, lev);
4068 }
4069
4070 /***                                Trap                                   ***/
4071
4072 /* tw */
4073 static void gen_tw(DisasContext *ctx)
4074 {
4075     TCGv_i32 t0 = tcg_const_i32(TO(ctx->opcode));
4076     /* Update the nip since this might generate a trap exception */
4077     gen_update_nip(ctx, ctx->nip);
4078     gen_helper_tw(cpu_env, cpu_gpr[rA(ctx->opcode)], cpu_gpr[rB(ctx->opcode)],
4079                   t0);
4080     tcg_temp_free_i32(t0);
4081 }
4082
4083 /* twi */
4084 static void gen_twi(DisasContext *ctx)
4085 {
4086     TCGv t0 = tcg_const_tl(SIMM(ctx->opcode));
4087     TCGv_i32 t1 = tcg_const_i32(TO(ctx->opcode));
4088     /* Update the nip since this might generate a trap exception */
4089     gen_update_nip(ctx, ctx->nip);
4090     gen_helper_tw(cpu_env, cpu_gpr[rA(ctx->opcode)], t0, t1);
4091     tcg_temp_free(t0);
4092     tcg_temp_free_i32(t1);
4093 }
4094
4095 #if defined(TARGET_PPC64)
4096 /* td */
4097 static void gen_td(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_td(cpu_env, cpu_gpr[rA(ctx->opcode)], cpu_gpr[rB(ctx->opcode)],
4103                   t0);
4104     tcg_temp_free_i32(t0);
4105 }
4106
4107 /* tdi */
4108 static void gen_tdi(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_td(cpu_env, cpu_gpr[rA(ctx->opcode)], t0, t1);
4115     tcg_temp_free(t0);
4116     tcg_temp_free_i32(t1);
4117 }
4118 #endif
4119
4120 /***                          Processor control                            ***/
4121
4122 static void gen_read_xer(TCGv dst)
4123 {
4124     TCGv t0 = tcg_temp_new();
4125     TCGv t1 = tcg_temp_new();
4126     TCGv t2 = tcg_temp_new();
4127     tcg_gen_mov_tl(dst, cpu_xer);
4128     tcg_gen_shli_tl(t0, cpu_so, XER_SO);
4129     tcg_gen_shli_tl(t1, cpu_ov, XER_OV);
4130     tcg_gen_shli_tl(t2, cpu_ca, XER_CA);
4131     tcg_gen_or_tl(t0, t0, t1);
4132     tcg_gen_or_tl(dst, dst, t2);
4133     tcg_gen_or_tl(dst, dst, t0);
4134     tcg_temp_free(t0);
4135     tcg_temp_free(t1);
4136     tcg_temp_free(t2);
4137 }
4138
4139 static void gen_write_xer(TCGv src)
4140 {
4141     tcg_gen_andi_tl(cpu_xer, src,
4142                     ~((1u << XER_SO) | (1u << XER_OV) | (1u << XER_CA)));
4143     tcg_gen_shri_tl(cpu_so, src, XER_SO);
4144     tcg_gen_shri_tl(cpu_ov, src, XER_OV);
4145     tcg_gen_shri_tl(cpu_ca, src, XER_CA);
4146     tcg_gen_andi_tl(cpu_so, cpu_so, 1);
4147     tcg_gen_andi_tl(cpu_ov, cpu_ov, 1);
4148     tcg_gen_andi_tl(cpu_ca, cpu_ca, 1);
4149 }
4150
4151 /* mcrxr */
4152 static void gen_mcrxr(DisasContext *ctx)
4153 {
4154     TCGv_i32 t0 = tcg_temp_new_i32();
4155     TCGv_i32 t1 = tcg_temp_new_i32();
4156     TCGv_i32 dst = cpu_crf[crfD(ctx->opcode)];
4157
4158     tcg_gen_trunc_tl_i32(t0, cpu_so);
4159     tcg_gen_trunc_tl_i32(t1, cpu_ov);
4160     tcg_gen_trunc_tl_i32(dst, cpu_ca);
4161     tcg_gen_shri_i32(t0, t0, 2);
4162     tcg_gen_shri_i32(t1, t1, 1);
4163     tcg_gen_or_i32(dst, dst, t0);
4164     tcg_gen_or_i32(dst, dst, t1);
4165     tcg_temp_free_i32(t0);
4166     tcg_temp_free_i32(t1);
4167
4168     tcg_gen_movi_tl(cpu_so, 0);
4169     tcg_gen_movi_tl(cpu_ov, 0);
4170     tcg_gen_movi_tl(cpu_ca, 0);
4171 }
4172
4173 /* mfcr mfocrf */
4174 static void gen_mfcr(DisasContext *ctx)
4175 {
4176     uint32_t crm, crn;
4177
4178     if (likely(ctx->opcode & 0x00100000)) {
4179         crm = CRM(ctx->opcode);
4180         if (likely(crm && ((crm & (crm - 1)) == 0))) {
4181             crn = ctz32 (crm);
4182             tcg_gen_extu_i32_tl(cpu_gpr[rD(ctx->opcode)], cpu_crf[7 - crn]);
4183             tcg_gen_shli_tl(cpu_gpr[rD(ctx->opcode)],
4184                             cpu_gpr[rD(ctx->opcode)], crn * 4);
4185         }
4186     } else {
4187         TCGv_i32 t0 = tcg_temp_new_i32();
4188         tcg_gen_mov_i32(t0, cpu_crf[0]);
4189         tcg_gen_shli_i32(t0, t0, 4);
4190         tcg_gen_or_i32(t0, t0, cpu_crf[1]);
4191         tcg_gen_shli_i32(t0, t0, 4);
4192         tcg_gen_or_i32(t0, t0, cpu_crf[2]);
4193         tcg_gen_shli_i32(t0, t0, 4);
4194         tcg_gen_or_i32(t0, t0, cpu_crf[3]);
4195         tcg_gen_shli_i32(t0, t0, 4);
4196         tcg_gen_or_i32(t0, t0, cpu_crf[4]);
4197         tcg_gen_shli_i32(t0, t0, 4);
4198         tcg_gen_or_i32(t0, t0, cpu_crf[5]);
4199         tcg_gen_shli_i32(t0, t0, 4);
4200         tcg_gen_or_i32(t0, t0, cpu_crf[6]);
4201         tcg_gen_shli_i32(t0, t0, 4);
4202         tcg_gen_or_i32(t0, t0, cpu_crf[7]);
4203         tcg_gen_extu_i32_tl(cpu_gpr[rD(ctx->opcode)], t0);
4204         tcg_temp_free_i32(t0);
4205     }
4206 }
4207
4208 /* mfmsr */
4209 static void gen_mfmsr(DisasContext *ctx)
4210 {
4211 #if defined(CONFIG_USER_ONLY)
4212     gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
4213 #else
4214     if (unlikely(!ctx->mem_idx)) {
4215         gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
4216         return;
4217     }
4218     tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_msr);
4219 #endif
4220 }
4221
4222 static void spr_noaccess(void *opaque, int gprn, int sprn)
4223 {
4224 #if 0
4225     sprn = ((sprn >> 5) & 0x1F) | ((sprn & 0x1F) << 5);
4226     printf("ERROR: try to access SPR %d !\n", sprn);
4227 #endif
4228 }
4229 #define SPR_NOACCESS (&spr_noaccess)
4230
4231 /* mfspr */
4232 static inline void gen_op_mfspr(DisasContext *ctx)
4233 {
4234     void (*read_cb)(void *opaque, int gprn, int sprn);
4235     uint32_t sprn = SPR(ctx->opcode);
4236
4237 #if !defined(CONFIG_USER_ONLY)
4238     if (ctx->mem_idx == 2)
4239         read_cb = ctx->spr_cb[sprn].hea_read;
4240     else if (ctx->mem_idx)
4241         read_cb = ctx->spr_cb[sprn].oea_read;
4242     else
4243 #endif
4244         read_cb = ctx->spr_cb[sprn].uea_read;
4245     if (likely(read_cb != NULL)) {
4246         if (likely(read_cb != SPR_NOACCESS)) {
4247             (*read_cb)(ctx, rD(ctx->opcode), sprn);
4248         } else {
4249             /* Privilege exception */
4250             /* This is a hack to avoid warnings when running Linux:
4251              * this OS breaks the PowerPC virtualisation model,
4252              * allowing userland application to read the PVR
4253              */
4254             if (sprn != SPR_PVR) {
4255                 qemu_log("Trying to read privileged spr %d (0x%03x) at "
4256                          TARGET_FMT_lx "\n", sprn, sprn, ctx->nip - 4);
4257                 printf("Trying to read privileged spr %d (0x%03x) at "
4258                        TARGET_FMT_lx "\n", sprn, sprn, ctx->nip - 4);
4259             }
4260             gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
4261         }
4262     } else {
4263         /* Not defined */
4264         qemu_log("Trying to read invalid spr %d (0x%03x) at "
4265                  TARGET_FMT_lx "\n", sprn, sprn, ctx->nip - 4);
4266         printf("Trying to read invalid spr %d (0x%03x) at "
4267                TARGET_FMT_lx "\n", sprn, sprn, ctx->nip - 4);
4268         gen_inval_exception(ctx, POWERPC_EXCP_INVAL_SPR);
4269     }
4270 }
4271
4272 static void gen_mfspr(DisasContext *ctx)
4273 {
4274     gen_op_mfspr(ctx);
4275 }
4276
4277 /* mftb */
4278 static void gen_mftb(DisasContext *ctx)
4279 {
4280     gen_op_mfspr(ctx);
4281 }
4282
4283 /* mtcrf mtocrf*/
4284 static void gen_mtcrf(DisasContext *ctx)
4285 {
4286     uint32_t crm, crn;
4287
4288     crm = CRM(ctx->opcode);
4289     if (likely((ctx->opcode & 0x00100000))) {
4290         if (crm && ((crm & (crm - 1)) == 0)) {
4291             TCGv_i32 temp = tcg_temp_new_i32();
4292             crn = ctz32 (crm);
4293             tcg_gen_trunc_tl_i32(temp, cpu_gpr[rS(ctx->opcode)]);
4294             tcg_gen_shri_i32(temp, temp, crn * 4);
4295             tcg_gen_andi_i32(cpu_crf[7 - crn], temp, 0xf);
4296             tcg_temp_free_i32(temp);
4297         }
4298     } else {
4299         TCGv_i32 temp = tcg_temp_new_i32();
4300         tcg_gen_trunc_tl_i32(temp, cpu_gpr[rS(ctx->opcode)]);
4301         for (crn = 0 ; crn < 8 ; crn++) {
4302             if (crm & (1 << crn)) {
4303                     tcg_gen_shri_i32(cpu_crf[7 - crn], temp, crn * 4);
4304                     tcg_gen_andi_i32(cpu_crf[7 - crn], cpu_crf[7 - crn], 0xf);
4305             }
4306         }
4307         tcg_temp_free_i32(temp);
4308     }
4309 }
4310
4311 /* mtmsr */
4312 #if defined(TARGET_PPC64)
4313 static void gen_mtmsrd(DisasContext *ctx)
4314 {
4315 #if defined(CONFIG_USER_ONLY)
4316     gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
4317 #else
4318     if (unlikely(!ctx->mem_idx)) {
4319         gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
4320         return;
4321     }
4322     if (ctx->opcode & 0x00010000) {
4323         /* Special form that does not need any synchronisation */
4324         TCGv t0 = tcg_temp_new();
4325         tcg_gen_andi_tl(t0, cpu_gpr[rS(ctx->opcode)], (1 << MSR_RI) | (1 << MSR_EE));
4326         tcg_gen_andi_tl(cpu_msr, cpu_msr, ~((1 << MSR_RI) | (1 << MSR_EE)));
4327         tcg_gen_or_tl(cpu_msr, cpu_msr, t0);
4328         tcg_temp_free(t0);
4329     } else {
4330         /* XXX: we need to update nip before the store
4331          *      if we enter power saving mode, we will exit the loop
4332          *      directly from ppc_store_msr
4333          */
4334         gen_update_nip(ctx, ctx->nip);
4335         gen_helper_store_msr(cpu_env, cpu_gpr[rS(ctx->opcode)]);
4336         /* Must stop the translation as machine state (may have) changed */
4337         /* Note that mtmsr is not always defined as context-synchronizing */
4338         gen_stop_exception(ctx);
4339     }
4340 #endif
4341 }
4342 #endif
4343
4344 static void gen_mtmsr(DisasContext *ctx)
4345 {
4346 #if defined(CONFIG_USER_ONLY)
4347     gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
4348 #else
4349     if (unlikely(!ctx->mem_idx)) {
4350         gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
4351         return;
4352     }
4353     if (ctx->opcode & 0x00010000) {
4354         /* Special form that does not need any synchronisation */
4355         TCGv t0 = tcg_temp_new();
4356         tcg_gen_andi_tl(t0, cpu_gpr[rS(ctx->opcode)], (1 << MSR_RI) | (1 << MSR_EE));
4357         tcg_gen_andi_tl(cpu_msr, cpu_msr, ~((1 << MSR_RI) | (1 << MSR_EE)));
4358         tcg_gen_or_tl(cpu_msr, cpu_msr, t0);
4359         tcg_temp_free(t0);
4360     } else {
4361         TCGv msr = tcg_temp_new();
4362
4363         /* XXX: we need to update nip before the store
4364          *      if we enter power saving mode, we will exit the loop
4365          *      directly from ppc_store_msr
4366          */
4367         gen_update_nip(ctx, ctx->nip);
4368 #if defined(TARGET_PPC64)
4369         tcg_gen_deposit_tl(msr, cpu_msr, cpu_gpr[rS(ctx->opcode)], 0, 32);
4370 #else
4371         tcg_gen_mov_tl(msr, cpu_gpr[rS(ctx->opcode)]);
4372 #endif
4373         gen_helper_store_msr(cpu_env, msr);
4374         tcg_temp_free(msr);
4375         /* Must stop the translation as machine state (may have) changed */
4376         /* Note that mtmsr is not always defined as context-synchronizing */
4377         gen_stop_exception(ctx);
4378     }
4379 #endif
4380 }
4381
4382 /* mtspr */
4383 static void gen_mtspr(DisasContext *ctx)
4384 {
4385     void (*write_cb)(void *opaque, int sprn, int gprn);
4386     uint32_t sprn = SPR(ctx->opcode);
4387
4388 #if !defined(CONFIG_USER_ONLY)
4389     if (ctx->mem_idx == 2)
4390         write_cb = ctx->spr_cb[sprn].hea_write;
4391     else if (ctx->mem_idx)
4392         write_cb = ctx->spr_cb[sprn].oea_write;
4393     else
4394 #endif
4395         write_cb = ctx->spr_cb[sprn].uea_write;
4396     if (likely(write_cb != NULL)) {
4397         if (likely(write_cb != SPR_NOACCESS)) {
4398             (*write_cb)(ctx, sprn, rS(ctx->opcode));
4399         } else {
4400             /* Privilege exception */
4401             qemu_log("Trying to write privileged spr %d (0x%03x) at "
4402                      TARGET_FMT_lx "\n", sprn, sprn, ctx->nip - 4);
4403             printf("Trying to write privileged spr %d (0x%03x) at "
4404                    TARGET_FMT_lx "\n", sprn, sprn, ctx->nip - 4);
4405             gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
4406         }
4407     } else {
4408         /* Not defined */
4409         qemu_log("Trying to write invalid spr %d (0x%03x) at "
4410                  TARGET_FMT_lx "\n", sprn, sprn, ctx->nip - 4);
4411         printf("Trying to write invalid spr %d (0x%03x) at "
4412                TARGET_FMT_lx "\n", sprn, sprn, ctx->nip - 4);
4413         gen_inval_exception(ctx, POWERPC_EXCP_INVAL_SPR);
4414     }
4415 }
4416
4417 /***                         Cache management                              ***/
4418
4419 /* dcbf */
4420 static void gen_dcbf(DisasContext *ctx)
4421 {
4422     /* XXX: specification says this is treated as a load by the MMU */
4423     TCGv t0;
4424     gen_set_access_type(ctx, ACCESS_CACHE);
4425     t0 = tcg_temp_new();
4426     gen_addr_reg_index(ctx, t0);
4427     gen_qemu_ld8u(ctx, t0, t0);
4428     tcg_temp_free(t0);
4429 }
4430
4431 /* dcbi (Supervisor only) */
4432 static void gen_dcbi(DisasContext *ctx)
4433 {
4434 #if defined(CONFIG_USER_ONLY)
4435     gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
4436 #else
4437     TCGv EA, val;
4438     if (unlikely(!ctx->mem_idx)) {
4439         gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
4440         return;
4441     }
4442     EA = tcg_temp_new();
4443     gen_set_access_type(ctx, ACCESS_CACHE);
4444     gen_addr_reg_index(ctx, EA);
4445     val = tcg_temp_new();
4446     /* XXX: specification says this should be treated as a store by the MMU */
4447     gen_qemu_ld8u(ctx, val, EA);
4448     gen_qemu_st8(ctx, val, EA);
4449     tcg_temp_free(val);
4450     tcg_temp_free(EA);
4451 #endif
4452 }
4453
4454 /* dcdst */
4455 static void gen_dcbst(DisasContext *ctx)
4456 {
4457     /* XXX: specification say this is treated as a load by the MMU */
4458     TCGv t0;
4459     gen_set_access_type(ctx, ACCESS_CACHE);
4460     t0 = tcg_temp_new();
4461     gen_addr_reg_index(ctx, t0);
4462     gen_qemu_ld8u(ctx, t0, t0);
4463     tcg_temp_free(t0);
4464 }
4465
4466 /* dcbt */
4467 static void gen_dcbt(DisasContext *ctx)
4468 {
4469     /* interpreted as no-op */
4470     /* XXX: specification say this is treated as a load by the MMU
4471      *      but does not generate any exception
4472      */
4473 }
4474
4475 /* dcbtst */
4476 static void gen_dcbtst(DisasContext *ctx)
4477 {
4478     /* interpreted as no-op */
4479     /* XXX: specification say this is treated as a load by the MMU
4480      *      but does not generate any exception
4481      */
4482 }
4483
4484 /* dcbz */
4485 static void gen_dcbz(DisasContext *ctx)
4486 {
4487     TCGv tcgv_addr;
4488     TCGv_i32 tcgv_is_dcbzl;
4489     int is_dcbzl = ctx->opcode & 0x00200000 ? 1 : 0;
4490
4491     gen_set_access_type(ctx, ACCESS_CACHE);
4492     /* NIP cannot be restored if the memory exception comes from an helper */
4493     gen_update_nip(ctx, ctx->nip - 4);
4494     tcgv_addr = tcg_temp_new();
4495     tcgv_is_dcbzl = tcg_const_i32(is_dcbzl);
4496
4497     gen_addr_reg_index(ctx, tcgv_addr);
4498     gen_helper_dcbz(cpu_env, tcgv_addr, tcgv_is_dcbzl);
4499
4500     tcg_temp_free(tcgv_addr);
4501     tcg_temp_free_i32(tcgv_is_dcbzl);
4502 }
4503
4504 /* dst / dstt */
4505 static void gen_dst(DisasContext *ctx)
4506 {
4507     if (rA(ctx->opcode) == 0) {
4508         gen_inval_exception(ctx, POWERPC_EXCP_INVAL_LSWX);
4509     } else {
4510         /* interpreted as no-op */
4511     }
4512 }
4513
4514 /* dstst /dststt */
4515 static void gen_dstst(DisasContext *ctx)
4516 {
4517     if (rA(ctx->opcode) == 0) {
4518         gen_inval_exception(ctx, POWERPC_EXCP_INVAL_LSWX);
4519     } else {
4520         /* interpreted as no-op */
4521     }
4522
4523 }
4524
4525 /* dss / dssall */
4526 static void gen_dss(DisasContext *ctx)
4527 {
4528     /* interpreted as no-op */
4529 }
4530
4531 /* icbi */
4532 static void gen_icbi(DisasContext *ctx)
4533 {
4534     TCGv t0;
4535     gen_set_access_type(ctx, ACCESS_CACHE);
4536     /* NIP cannot be restored if the memory exception comes from an helper */
4537     gen_update_nip(ctx, ctx->nip - 4);
4538     t0 = tcg_temp_new();
4539     gen_addr_reg_index(ctx, t0);
4540     gen_helper_icbi(cpu_env, t0);
4541     tcg_temp_free(t0);
4542 }
4543
4544 /* Optional: */
4545 /* dcba */
4546 static void gen_dcba(DisasContext *ctx)
4547 {
4548     /* interpreted as no-op */
4549     /* XXX: specification say this is treated as a store by the MMU
4550      *      but does not generate any exception
4551      */
4552 }
4553
4554 /***                    Segment register manipulation                      ***/
4555 /* Supervisor only: */
4556
4557 /* mfsr */
4558 static void gen_mfsr(DisasContext *ctx)
4559 {
4560 #if defined(CONFIG_USER_ONLY)
4561     gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
4562 #else
4563     TCGv t0;
4564     if (unlikely(!ctx->mem_idx)) {
4565         gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
4566         return;
4567     }
4568     t0 = tcg_const_tl(SR(ctx->opcode));
4569     gen_helper_load_sr(cpu_gpr[rD(ctx->opcode)], cpu_env, t0);
4570     tcg_temp_free(t0);
4571 #endif
4572 }
4573
4574 /* mfsrin */
4575 static void gen_mfsrin(DisasContext *ctx)
4576 {
4577 #if defined(CONFIG_USER_ONLY)
4578     gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
4579 #else
4580     TCGv t0;
4581     if (unlikely(!ctx->mem_idx)) {
4582         gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
4583         return;
4584     }
4585     t0 = tcg_temp_new();
4586     tcg_gen_shri_tl(t0, cpu_gpr[rB(ctx->opcode)], 28);
4587     tcg_gen_andi_tl(t0, t0, 0xF);
4588     gen_helper_load_sr(cpu_gpr[rD(ctx->opcode)], cpu_env, t0);
4589     tcg_temp_free(t0);
4590 #endif
4591 }
4592
4593 /* mtsr */
4594 static void gen_mtsr(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->mem_idx)) {
4601         gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
4602         return;
4603     }
4604     t0 = tcg_const_tl(SR(ctx->opcode));
4605     gen_helper_store_sr(cpu_env, t0, cpu_gpr[rS(ctx->opcode)]);
4606     tcg_temp_free(t0);
4607 #endif
4608 }
4609
4610 /* mtsrin */
4611 static void gen_mtsrin(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->mem_idx)) {
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_store_sr(cpu_env, t0, cpu_gpr[rD(ctx->opcode)]);
4625     tcg_temp_free(t0);
4626 #endif
4627 }
4628
4629 #if defined(TARGET_PPC64)
4630 /* Specific implementation for PowerPC 64 "bridge" emulation using SLB */
4631
4632 /* mfsr */
4633 static void gen_mfsr_64b(DisasContext *ctx)
4634 {
4635 #if defined(CONFIG_USER_ONLY)
4636     gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
4637 #else
4638     TCGv t0;
4639     if (unlikely(!ctx->mem_idx)) {
4640         gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
4641         return;
4642     }
4643     t0 = tcg_const_tl(SR(ctx->opcode));
4644     gen_helper_load_sr(cpu_gpr[rD(ctx->opcode)], cpu_env, t0);
4645     tcg_temp_free(t0);
4646 #endif
4647 }
4648
4649 /* mfsrin */
4650 static void gen_mfsrin_64b(DisasContext *ctx)
4651 {
4652 #if defined(CONFIG_USER_ONLY)
4653     gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
4654 #else
4655     TCGv t0;
4656     if (unlikely(!ctx->mem_idx)) {
4657         gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
4658         return;
4659     }
4660     t0 = tcg_temp_new();
4661     tcg_gen_shri_tl(t0, cpu_gpr[rB(ctx->opcode)], 28);
4662     tcg_gen_andi_tl(t0, t0, 0xF);
4663     gen_helper_load_sr(cpu_gpr[rD(ctx->opcode)], cpu_env, t0);
4664     tcg_temp_free(t0);
4665 #endif
4666 }
4667
4668 /* mtsr */
4669 static void gen_mtsr_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->mem_idx)) {
4676         gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
4677         return;
4678     }
4679     t0 = tcg_const_tl(SR(ctx->opcode));
4680     gen_helper_store_sr(cpu_env, t0, cpu_gpr[rS(ctx->opcode)]);
4681     tcg_temp_free(t0);
4682 #endif
4683 }
4684
4685 /* mtsrin */
4686 static void gen_mtsrin_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->mem_idx)) {
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_store_sr(cpu_env, t0, cpu_gpr[rS(ctx->opcode)]);
4700     tcg_temp_free(t0);
4701 #endif
4702 }
4703
4704 /* slbmte */
4705 static void gen_slbmte(DisasContext *ctx)
4706 {
4707 #if defined(CONFIG_USER_ONLY)
4708     gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
4709 #else
4710     if (unlikely(!ctx->mem_idx)) {
4711         gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
4712         return;
4713     }
4714     gen_helper_store_slb(cpu_env, cpu_gpr[rB(ctx->opcode)],
4715                          cpu_gpr[rS(ctx->opcode)]);
4716 #endif
4717 }
4718
4719 static void gen_slbmfee(DisasContext *ctx)
4720 {
4721 #if defined(CONFIG_USER_ONLY)
4722     gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
4723 #else
4724     if (unlikely(!ctx->mem_idx)) {
4725         gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
4726         return;
4727     }
4728     gen_helper_load_slb_esid(cpu_gpr[rS(ctx->opcode)], cpu_env,
4729                              cpu_gpr[rB(ctx->opcode)]);
4730 #endif
4731 }
4732
4733 static void gen_slbmfev(DisasContext *ctx)
4734 {
4735 #if defined(CONFIG_USER_ONLY)
4736     gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
4737 #else
4738     if (unlikely(!ctx->mem_idx)) {
4739         gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
4740         return;
4741     }
4742     gen_helper_load_slb_vsid(cpu_gpr[rS(ctx->opcode)], cpu_env,
4743                              cpu_gpr[rB(ctx->opcode)]);
4744 #endif
4745 }
4746 #endif /* defined(TARGET_PPC64) */
4747
4748 /***                      Lookaside buffer management                      ***/
4749 /* Optional & mem_idx only: */
4750
4751 /* tlbia */
4752 static void gen_tlbia(DisasContext *ctx)
4753 {
4754 #if defined(CONFIG_USER_ONLY)
4755     gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
4756 #else
4757     if (unlikely(!ctx->mem_idx)) {
4758         gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
4759         return;
4760     }
4761     gen_helper_tlbia(cpu_env);
4762 #endif
4763 }
4764
4765 /* tlbiel */
4766 static void gen_tlbiel(DisasContext *ctx)
4767 {
4768 #if defined(CONFIG_USER_ONLY)
4769     gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
4770 #else
4771     if (unlikely(!ctx->mem_idx)) {
4772         gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
4773         return;
4774     }
4775     gen_helper_tlbie(cpu_env, cpu_gpr[rB(ctx->opcode)]);
4776 #endif
4777 }
4778
4779 /* tlbie */
4780 static void gen_tlbie(DisasContext *ctx)
4781 {
4782 #if defined(CONFIG_USER_ONLY)
4783     gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
4784 #else
4785     if (unlikely(!ctx->mem_idx)) {
4786         gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
4787         return;
4788     }
4789     if (NARROW_MODE(ctx)) {
4790         TCGv t0 = tcg_temp_new();
4791         tcg_gen_ext32u_tl(t0, cpu_gpr[rB(ctx->opcode)]);
4792         gen_helper_tlbie(cpu_env, t0);
4793         tcg_temp_free(t0);
4794     } else {
4795         gen_helper_tlbie(cpu_env, cpu_gpr[rB(ctx->opcode)]);
4796     }
4797 #endif
4798 }
4799
4800 /* tlbsync */
4801 static void gen_tlbsync(DisasContext *ctx)
4802 {
4803 #if defined(CONFIG_USER_ONLY)
4804     gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
4805 #else
4806     if (unlikely(!ctx->mem_idx)) {
4807         gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
4808         return;
4809     }
4810     /* This has no effect: it should ensure that all previous
4811      * tlbie have completed
4812      */
4813     gen_stop_exception(ctx);
4814 #endif
4815 }
4816
4817 #if defined(TARGET_PPC64)
4818 /* slbia */
4819 static void gen_slbia(DisasContext *ctx)
4820 {
4821 #if defined(CONFIG_USER_ONLY)
4822     gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
4823 #else
4824     if (unlikely(!ctx->mem_idx)) {
4825         gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
4826         return;
4827     }
4828     gen_helper_slbia(cpu_env);
4829 #endif
4830 }
4831
4832 /* slbie */
4833 static void gen_slbie(DisasContext *ctx)
4834 {
4835 #if defined(CONFIG_USER_ONLY)
4836     gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
4837 #else
4838     if (unlikely(!ctx->mem_idx)) {
4839         gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
4840         return;
4841     }
4842     gen_helper_slbie(cpu_env, cpu_gpr[rB(ctx->opcode)]);
4843 #endif
4844 }
4845 #endif
4846
4847 /***                              External control                         ***/
4848 /* Optional: */
4849
4850 /* eciwx */
4851 static void gen_eciwx(DisasContext *ctx)
4852 {
4853     TCGv t0;
4854     /* Should check EAR[E] ! */
4855     gen_set_access_type(ctx, ACCESS_EXT);
4856     t0 = tcg_temp_new();
4857     gen_addr_reg_index(ctx, t0);
4858     gen_check_align(ctx, t0, 0x03);
4859     gen_qemu_ld32u(ctx, cpu_gpr[rD(ctx->opcode)], t0);
4860     tcg_temp_free(t0);
4861 }
4862
4863 /* ecowx */
4864 static void gen_ecowx(DisasContext *ctx)
4865 {
4866     TCGv t0;
4867     /* Should check EAR[E] ! */
4868     gen_set_access_type(ctx, ACCESS_EXT);
4869     t0 = tcg_temp_new();
4870     gen_addr_reg_index(ctx, t0);
4871     gen_check_align(ctx, t0, 0x03);
4872     gen_qemu_st32(ctx, cpu_gpr[rD(ctx->opcode)], t0);
4873     tcg_temp_free(t0);
4874 }
4875
4876 /* PowerPC 601 specific instructions */
4877
4878 /* abs - abs. */
4879 static void gen_abs(DisasContext *ctx)
4880 {
4881     int l1 = gen_new_label();
4882     int l2 = gen_new_label();
4883     tcg_gen_brcondi_tl(TCG_COND_GE, cpu_gpr[rA(ctx->opcode)], 0, l1);
4884     tcg_gen_neg_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)]);
4885     tcg_gen_br(l2);
4886     gen_set_label(l1);
4887     tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)]);
4888     gen_set_label(l2);
4889     if (unlikely(Rc(ctx->opcode) != 0))
4890         gen_set_Rc0(ctx, cpu_gpr[rD(ctx->opcode)]);
4891 }
4892
4893 /* abso - abso. */
4894 static void gen_abso(DisasContext *ctx)
4895 {
4896     int l1 = gen_new_label();
4897     int l2 = gen_new_label();
4898     int l3 = gen_new_label();
4899     /* Start with XER OV disabled, the most likely case */
4900     tcg_gen_movi_tl(cpu_ov, 0);
4901     tcg_gen_brcondi_tl(TCG_COND_GE, cpu_gpr[rA(ctx->opcode)], 0, l2);
4902     tcg_gen_brcondi_tl(TCG_COND_NE, cpu_gpr[rA(ctx->opcode)], 0x80000000, l1);
4903     tcg_gen_movi_tl(cpu_ov, 1);
4904     tcg_gen_movi_tl(cpu_so, 1);
4905     tcg_gen_br(l2);
4906     gen_set_label(l1);
4907     tcg_gen_neg_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)]);
4908     tcg_gen_br(l3);
4909     gen_set_label(l2);
4910     tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)]);
4911     gen_set_label(l3);
4912     if (unlikely(Rc(ctx->opcode) != 0))
4913         gen_set_Rc0(ctx, cpu_gpr[rD(ctx->opcode)]);
4914 }
4915
4916 /* clcs */
4917 static void gen_clcs(DisasContext *ctx)
4918 {
4919     TCGv_i32 t0 = tcg_const_i32(rA(ctx->opcode));
4920     gen_helper_clcs(cpu_gpr[rD(ctx->opcode)], cpu_env, t0);
4921     tcg_temp_free_i32(t0);
4922     /* Rc=1 sets CR0 to an undefined state */
4923 }
4924
4925 /* div - div. */
4926 static void gen_div(DisasContext *ctx)
4927 {
4928     gen_helper_div(cpu_gpr[rD(ctx->opcode)], cpu_env, cpu_gpr[rA(ctx->opcode)],
4929                    cpu_gpr[rB(ctx->opcode)]);
4930     if (unlikely(Rc(ctx->opcode) != 0))
4931         gen_set_Rc0(ctx, cpu_gpr[rD(ctx->opcode)]);
4932 }
4933
4934 /* divo - divo. */
4935 static void gen_divo(DisasContext *ctx)
4936 {
4937     gen_helper_divo(cpu_gpr[rD(ctx->opcode)], cpu_env, cpu_gpr[rA(ctx->opcode)],
4938                     cpu_gpr[rB(ctx->opcode)]);
4939     if (unlikely(Rc(ctx->opcode) != 0))
4940         gen_set_Rc0(ctx, cpu_gpr[rD(ctx->opcode)]);
4941 }
4942
4943 /* divs - divs. */
4944 static void gen_divs(DisasContext *ctx)
4945 {
4946     gen_helper_divs(cpu_gpr[rD(ctx->opcode)], cpu_env, cpu_gpr[rA(ctx->opcode)],
4947                     cpu_gpr[rB(ctx->opcode)]);
4948     if (unlikely(Rc(ctx->opcode) != 0))
4949         gen_set_Rc0(ctx, cpu_gpr[rD(ctx->opcode)]);
4950 }
4951
4952 /* divso - divso. */
4953 static void gen_divso(DisasContext *ctx)
4954 {
4955     gen_helper_divso(cpu_gpr[rD(ctx->opcode)], cpu_env,
4956                      cpu_gpr[rA(ctx->opcode)], cpu_gpr[rB(ctx->opcode)]);
4957     if (unlikely(Rc(ctx->opcode) != 0))
4958         gen_set_Rc0(ctx, cpu_gpr[rD(ctx->opcode)]);
4959 }
4960
4961 /* doz - doz. */
4962 static void gen_doz(DisasContext *ctx)
4963 {
4964     int l1 = gen_new_label();
4965     int l2 = gen_new_label();
4966     tcg_gen_brcond_tl(TCG_COND_GE, cpu_gpr[rB(ctx->opcode)], cpu_gpr[rA(ctx->opcode)], l1);
4967     tcg_gen_sub_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rB(ctx->opcode)], cpu_gpr[rA(ctx->opcode)]);
4968     tcg_gen_br(l2);
4969     gen_set_label(l1);
4970     tcg_gen_movi_tl(cpu_gpr[rD(ctx->opcode)], 0);
4971     gen_set_label(l2);
4972     if (unlikely(Rc(ctx->opcode) != 0))
4973         gen_set_Rc0(ctx, cpu_gpr[rD(ctx->opcode)]);
4974 }
4975
4976 /* dozo - dozo. */
4977 static void gen_dozo(DisasContext *ctx)
4978 {
4979     int l1 = gen_new_label();
4980     int l2 = gen_new_label();
4981     TCGv t0 = tcg_temp_new();
4982     TCGv t1 = tcg_temp_new();
4983     TCGv t2 = tcg_temp_new();
4984     /* Start with XER OV disabled, the most likely case */
4985     tcg_gen_movi_tl(cpu_ov, 0);
4986     tcg_gen_brcond_tl(TCG_COND_GE, cpu_gpr[rB(ctx->opcode)], cpu_gpr[rA(ctx->opcode)], l1);
4987     tcg_gen_sub_tl(t0, cpu_gpr[rB(ctx->opcode)], cpu_gpr[rA(ctx->opcode)]);
4988     tcg_gen_xor_tl(t1, cpu_gpr[rB(ctx->opcode)], cpu_gpr[rA(ctx->opcode)]);
4989     tcg_gen_xor_tl(t2, cpu_gpr[rA(ctx->opcode)], t0);
4990     tcg_gen_andc_tl(t1, t1, t2);
4991     tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], t0);
4992     tcg_gen_brcondi_tl(TCG_COND_GE, t1, 0, l2);
4993     tcg_gen_movi_tl(cpu_ov, 1);
4994     tcg_gen_movi_tl(cpu_so, 1);
4995     tcg_gen_br(l2);
4996     gen_set_label(l1);
4997     tcg_gen_movi_tl(cpu_gpr[rD(ctx->opcode)], 0);
4998     gen_set_label(l2);
4999     tcg_temp_free(t0);
5000     tcg_temp_free(t1);
5001     tcg_temp_free(t2);
5002     if (unlikely(Rc(ctx->opcode) != 0))
5003         gen_set_Rc0(ctx, cpu_gpr[rD(ctx->opcode)]);
5004 }
5005
5006 /* dozi */
5007 static void gen_dozi(DisasContext *ctx)
5008 {
5009     target_long simm = SIMM(ctx->opcode);
5010     int l1 = gen_new_label();
5011     int l2 = gen_new_label();
5012     tcg_gen_brcondi_tl(TCG_COND_LT, cpu_gpr[rA(ctx->opcode)], simm, l1);
5013     tcg_gen_subfi_tl(cpu_gpr[rD(ctx->opcode)], simm, cpu_gpr[rA(ctx->opcode)]);
5014     tcg_gen_br(l2);
5015     gen_set_label(l1);
5016     tcg_gen_movi_tl(cpu_gpr[rD(ctx->opcode)], 0);
5017     gen_set_label(l2);
5018     if (unlikely(Rc(ctx->opcode) != 0))
5019         gen_set_Rc0(ctx, cpu_gpr[rD(ctx->opcode)]);
5020 }
5021
5022 /* lscbx - lscbx. */
5023 static void gen_lscbx(DisasContext *ctx)
5024 {
5025     TCGv t0 = tcg_temp_new();
5026     TCGv_i32 t1 = tcg_const_i32(rD(ctx->opcode));
5027     TCGv_i32 t2 = tcg_const_i32(rA(ctx->opcode));
5028     TCGv_i32 t3 = tcg_const_i32(rB(ctx->opcode));
5029
5030     gen_addr_reg_index(ctx, t0);
5031     /* NIP cannot be restored if the memory exception comes from an helper */
5032     gen_update_nip(ctx, ctx->nip - 4);
5033     gen_helper_lscbx(t0, cpu_env, t0, t1, t2, t3);
5034     tcg_temp_free_i32(t1);
5035     tcg_temp_free_i32(t2);
5036     tcg_temp_free_i32(t3);
5037     tcg_gen_andi_tl(cpu_xer, cpu_xer, ~0x7F);
5038     tcg_gen_or_tl(cpu_xer, cpu_xer, t0);
5039     if (unlikely(Rc(ctx->opcode) != 0))
5040         gen_set_Rc0(ctx, t0);
5041     tcg_temp_free(t0);
5042 }
5043
5044 /* maskg - maskg. */
5045 static void gen_maskg(DisasContext *ctx)
5046 {
5047     int l1 = gen_new_label();
5048     TCGv t0 = tcg_temp_new();
5049     TCGv t1 = tcg_temp_new();
5050     TCGv t2 = tcg_temp_new();
5051     TCGv t3 = tcg_temp_new();
5052     tcg_gen_movi_tl(t3, 0xFFFFFFFF);
5053     tcg_gen_andi_tl(t0, cpu_gpr[rB(ctx->opcode)], 0x1F);
5054     tcg_gen_andi_tl(t1, cpu_gpr[rS(ctx->opcode)], 0x1F);
5055     tcg_gen_addi_tl(t2, t0, 1);
5056     tcg_gen_shr_tl(t2, t3, t2);
5057     tcg_gen_shr_tl(t3, t3, t1);
5058     tcg_gen_xor_tl(cpu_gpr[rA(ctx->opcode)], t2, t3);
5059     tcg_gen_brcond_tl(TCG_COND_GE, t0, t1, l1);
5060     tcg_gen_neg_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rA(ctx->opcode)]);
5061     gen_set_label(l1);
5062     tcg_temp_free(t0);
5063     tcg_temp_free(t1);
5064     tcg_temp_free(t2);
5065     tcg_temp_free(t3);
5066     if (unlikely(Rc(ctx->opcode) != 0))
5067         gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
5068 }
5069
5070 /* maskir - maskir. */
5071 static void gen_maskir(DisasContext *ctx)
5072 {
5073     TCGv t0 = tcg_temp_new();
5074     TCGv t1 = tcg_temp_new();
5075     tcg_gen_and_tl(t0, cpu_gpr[rS(ctx->opcode)], cpu_gpr[rB(ctx->opcode)]);
5076     tcg_gen_andc_tl(t1, cpu_gpr[rA(ctx->opcode)], cpu_gpr[rB(ctx->opcode)]);
5077     tcg_gen_or_tl(cpu_gpr[rA(ctx->opcode)], t0, t1);
5078     tcg_temp_free(t0);
5079     tcg_temp_free(t1);
5080     if (unlikely(Rc(ctx->opcode) != 0))
5081         gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
5082 }
5083
5084 /* mul - mul. */
5085 static void gen_mul(DisasContext *ctx)
5086 {
5087     TCGv_i64 t0 = tcg_temp_new_i64();
5088     TCGv_i64 t1 = tcg_temp_new_i64();
5089     TCGv t2 = tcg_temp_new();
5090     tcg_gen_extu_tl_i64(t0, cpu_gpr[rA(ctx->opcode)]);
5091     tcg_gen_extu_tl_i64(t1, cpu_gpr[rB(ctx->opcode)]);
5092     tcg_gen_mul_i64(t0, t0, t1);
5093     tcg_gen_trunc_i64_tl(t2, t0);
5094     gen_store_spr(SPR_MQ, t2);
5095     tcg_gen_shri_i64(t1, t0, 32);
5096     tcg_gen_trunc_i64_tl(cpu_gpr[rD(ctx->opcode)], t1);
5097     tcg_temp_free_i64(t0);
5098     tcg_temp_free_i64(t1);
5099     tcg_temp_free(t2);
5100     if (unlikely(Rc(ctx->opcode) != 0))
5101         gen_set_Rc0(ctx, cpu_gpr[rD(ctx->opcode)]);
5102 }
5103
5104 /* mulo - mulo. */
5105 static void gen_mulo(DisasContext *ctx)
5106 {
5107     int l1 = gen_new_label();
5108     TCGv_i64 t0 = tcg_temp_new_i64();
5109     TCGv_i64 t1 = tcg_temp_new_i64();
5110     TCGv t2 = tcg_temp_new();
5111     /* Start with XER OV disabled, the most likely case */
5112     tcg_gen_movi_tl(cpu_ov, 0);
5113     tcg_gen_extu_tl_i64(t0, cpu_gpr[rA(ctx->opcode)]);
5114     tcg_gen_extu_tl_i64(t1, cpu_gpr[rB(ctx->opcode)]);
5115     tcg_gen_mul_i64(t0, t0, t1);
5116     tcg_gen_trunc_i64_tl(t2, t0);
5117     gen_store_spr(SPR_MQ, t2);
5118     tcg_gen_shri_i64(t1, t0, 32);
5119     tcg_gen_trunc_i64_tl(cpu_gpr[rD(ctx->opcode)], t1);
5120     tcg_gen_ext32s_i64(t1, t0);
5121     tcg_gen_brcond_i64(TCG_COND_EQ, t0, t1, l1);
5122     tcg_gen_movi_tl(cpu_ov, 1);
5123     tcg_gen_movi_tl(cpu_so, 1);
5124     gen_set_label(l1);
5125     tcg_temp_free_i64(t0);
5126     tcg_temp_free_i64(t1);
5127     tcg_temp_free(t2);
5128     if (unlikely(Rc(ctx->opcode) != 0))
5129         gen_set_Rc0(ctx, cpu_gpr[rD(ctx->opcode)]);
5130 }
5131
5132 /* nabs - nabs. */
5133 static void gen_nabs(DisasContext *ctx)
5134 {
5135     int l1 = gen_new_label();
5136     int l2 = gen_new_label();
5137     tcg_gen_brcondi_tl(TCG_COND_GT, cpu_gpr[rA(ctx->opcode)], 0, l1);
5138     tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)]);
5139     tcg_gen_br(l2);
5140     gen_set_label(l1);
5141     tcg_gen_neg_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)]);
5142     gen_set_label(l2);
5143     if (unlikely(Rc(ctx->opcode) != 0))
5144         gen_set_Rc0(ctx, cpu_gpr[rD(ctx->opcode)]);
5145 }
5146
5147 /* nabso - nabso. */
5148 static void gen_nabso(DisasContext *ctx)
5149 {
5150     int l1 = gen_new_label();
5151     int l2 = gen_new_label();
5152     tcg_gen_brcondi_tl(TCG_COND_GT, cpu_gpr[rA(ctx->opcode)], 0, l1);
5153     tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)]);
5154     tcg_gen_br(l2);
5155     gen_set_label(l1);
5156     tcg_gen_neg_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)]);
5157     gen_set_label(l2);
5158     /* nabs never overflows */
5159     tcg_gen_movi_tl(cpu_ov, 0);
5160     if (unlikely(Rc(ctx->opcode) != 0))
5161         gen_set_Rc0(ctx, cpu_gpr[rD(ctx->opcode)]);
5162 }
5163
5164 /* rlmi - rlmi. */
5165 static void gen_rlmi(DisasContext *ctx)
5166 {
5167     uint32_t mb = MB(ctx->opcode);
5168     uint32_t me = ME(ctx->opcode);
5169     TCGv t0 = tcg_temp_new();
5170     tcg_gen_andi_tl(t0, cpu_gpr[rB(ctx->opcode)], 0x1F);
5171     tcg_gen_rotl_tl(t0, cpu_gpr[rS(ctx->opcode)], t0);
5172     tcg_gen_andi_tl(t0, t0, MASK(mb, me));
5173     tcg_gen_andi_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rA(ctx->opcode)], ~MASK(mb, me));
5174     tcg_gen_or_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rA(ctx->opcode)], t0);
5175     tcg_temp_free(t0);
5176     if (unlikely(Rc(ctx->opcode) != 0))
5177         gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
5178 }
5179
5180 /* rrib - rrib. */
5181 static void gen_rrib(DisasContext *ctx)
5182 {
5183     TCGv t0 = tcg_temp_new();
5184     TCGv t1 = tcg_temp_new();
5185     tcg_gen_andi_tl(t0, cpu_gpr[rB(ctx->opcode)], 0x1F);
5186     tcg_gen_movi_tl(t1, 0x80000000);
5187     tcg_gen_shr_tl(t1, t1, t0);
5188     tcg_gen_shr_tl(t0, cpu_gpr[rS(ctx->opcode)], t0);
5189     tcg_gen_and_tl(t0, t0, t1);
5190     tcg_gen_andc_tl(t1, cpu_gpr[rA(ctx->opcode)], t1);
5191     tcg_gen_or_tl(cpu_gpr[rA(ctx->opcode)], t0, t1);
5192     tcg_temp_free(t0);
5193     tcg_temp_free(t1);
5194     if (unlikely(Rc(ctx->opcode) != 0))
5195         gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
5196 }
5197
5198 /* sle - sle. */
5199 static void gen_sle(DisasContext *ctx)
5200 {
5201     TCGv t0 = tcg_temp_new();
5202     TCGv t1 = tcg_temp_new();
5203     tcg_gen_andi_tl(t1, cpu_gpr[rB(ctx->opcode)], 0x1F);
5204     tcg_gen_shl_tl(t0, cpu_gpr[rS(ctx->opcode)], t1);
5205     tcg_gen_subfi_tl(t1, 32, t1);
5206     tcg_gen_shr_tl(t1, cpu_gpr[rS(ctx->opcode)], t1);
5207     tcg_gen_or_tl(t1, t0, t1);
5208     tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], t0);
5209     gen_store_spr(SPR_MQ, t1);
5210     tcg_temp_free(t0);
5211     tcg_temp_free(t1);
5212     if (unlikely(Rc(ctx->opcode) != 0))
5213         gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
5214 }
5215
5216 /* sleq - sleq. */
5217 static void gen_sleq(DisasContext *ctx)
5218 {
5219     TCGv t0 = tcg_temp_new();
5220     TCGv t1 = tcg_temp_new();
5221     TCGv t2 = tcg_temp_new();
5222     tcg_gen_andi_tl(t0, cpu_gpr[rB(ctx->opcode)], 0x1F);
5223     tcg_gen_movi_tl(t2, 0xFFFFFFFF);
5224     tcg_gen_shl_tl(t2, t2, t0);
5225     tcg_gen_rotl_tl(t0, cpu_gpr[rS(ctx->opcode)], t0);
5226     gen_load_spr(t1, SPR_MQ);
5227     gen_store_spr(SPR_MQ, t0);
5228     tcg_gen_and_tl(t0, t0, t2);
5229     tcg_gen_andc_tl(t1, t1, t2);
5230     tcg_gen_or_tl(cpu_gpr[rA(ctx->opcode)], t0, t1);
5231     tcg_temp_free(t0);
5232     tcg_temp_free(t1);
5233     tcg_temp_free(t2);
5234     if (unlikely(Rc(ctx->opcode) != 0))
5235         gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
5236 }
5237
5238 /* sliq - sliq. */
5239 static void gen_sliq(DisasContext *ctx)
5240 {
5241     int sh = SH(ctx->opcode);
5242     TCGv t0 = tcg_temp_new();
5243     TCGv t1 = tcg_temp_new();
5244     tcg_gen_shli_tl(t0, cpu_gpr[rS(ctx->opcode)], sh);
5245     tcg_gen_shri_tl(t1, cpu_gpr[rS(ctx->opcode)], 32 - sh);
5246     tcg_gen_or_tl(t1, t0, t1);
5247     tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], t0);
5248     gen_store_spr(SPR_MQ, t1);
5249     tcg_temp_free(t0);
5250     tcg_temp_free(t1);
5251     if (unlikely(Rc(ctx->opcode) != 0))
5252         gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
5253 }
5254
5255 /* slliq - slliq. */
5256 static void gen_slliq(DisasContext *ctx)
5257 {
5258     int sh = SH(ctx->opcode);
5259     TCGv t0 = tcg_temp_new();
5260     TCGv t1 = tcg_temp_new();
5261     tcg_gen_rotli_tl(t0, cpu_gpr[rS(ctx->opcode)], sh);
5262     gen_load_spr(t1, SPR_MQ);
5263     gen_store_spr(SPR_MQ, t0);
5264     tcg_gen_andi_tl(t0, t0,  (0xFFFFFFFFU << sh));
5265     tcg_gen_andi_tl(t1, t1, ~(0xFFFFFFFFU << sh));
5266     tcg_gen_or_tl(cpu_gpr[rA(ctx->opcode)], t0, t1);
5267     tcg_temp_free(t0);
5268     tcg_temp_free(t1);
5269     if (unlikely(Rc(ctx->opcode) != 0))
5270         gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
5271 }
5272
5273 /* sllq - sllq. */
5274 static void gen_sllq(DisasContext *ctx)
5275 {
5276     int l1 = gen_new_label();
5277     int l2 = gen_new_label();
5278     TCGv t0 = tcg_temp_local_new();
5279     TCGv t1 = tcg_temp_local_new();
5280     TCGv t2 = tcg_temp_local_new();
5281     tcg_gen_andi_tl(t2, cpu_gpr[rB(ctx->opcode)], 0x1F);
5282     tcg_gen_movi_tl(t1, 0xFFFFFFFF);
5283     tcg_gen_shl_tl(t1, t1, t2);
5284     tcg_gen_andi_tl(t0, cpu_gpr[rB(ctx->opcode)], 0x20);
5285     tcg_gen_brcondi_tl(TCG_COND_EQ, t0, 0, l1);
5286     gen_load_spr(t0, SPR_MQ);
5287     tcg_gen_and_tl(cpu_gpr[rA(ctx->opcode)], t0, t1);
5288     tcg_gen_br(l2);
5289     gen_set_label(l1);
5290     tcg_gen_shl_tl(t0, cpu_gpr[rS(ctx->opcode)], t2);
5291     gen_load_spr(t2, SPR_MQ);
5292     tcg_gen_andc_tl(t1, t2, t1);
5293     tcg_gen_or_tl(cpu_gpr[rA(ctx->opcode)], t0, t1);
5294     gen_set_label(l2);
5295     tcg_temp_free(t0);
5296     tcg_temp_free(t1);
5297     tcg_temp_free(t2);
5298     if (unlikely(Rc(ctx->opcode) != 0))
5299         gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
5300 }
5301
5302 /* slq - slq. */
5303 static void gen_slq(DisasContext *ctx)
5304 {
5305     int l1 = gen_new_label();
5306     TCGv t0 = tcg_temp_new();
5307     TCGv t1 = tcg_temp_new();
5308     tcg_gen_andi_tl(t1, cpu_gpr[rB(ctx->opcode)], 0x1F);
5309     tcg_gen_shl_tl(t0, cpu_gpr[rS(ctx->opcode)], t1);
5310     tcg_gen_subfi_tl(t1, 32, t1);
5311     tcg_gen_shr_tl(t1, cpu_gpr[rS(ctx->opcode)], t1);
5312     tcg_gen_or_tl(t1, t0, t1);
5313     gen_store_spr(SPR_MQ, t1);
5314     tcg_gen_andi_tl(t1, cpu_gpr[rB(ctx->opcode)], 0x20);
5315     tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], t0);
5316     tcg_gen_brcondi_tl(TCG_COND_EQ, t1, 0, l1);
5317     tcg_gen_movi_tl(cpu_gpr[rA(ctx->opcode)], 0);
5318     gen_set_label(l1);
5319     tcg_temp_free(t0);
5320     tcg_temp_free(t1);
5321     if (unlikely(Rc(ctx->opcode) != 0))
5322         gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
5323 }
5324
5325 /* sraiq - sraiq. */
5326 static void gen_sraiq(DisasContext *ctx)
5327 {
5328     int sh = SH(ctx->opcode);
5329     int l1 = gen_new_label();
5330     TCGv t0 = tcg_temp_new();
5331     TCGv t1 = tcg_temp_new();
5332     tcg_gen_shri_tl(t0, cpu_gpr[rS(ctx->opcode)], sh);
5333     tcg_gen_shli_tl(t1, cpu_gpr[rS(ctx->opcode)], 32 - sh);
5334     tcg_gen_or_tl(t0, t0, t1);
5335     gen_store_spr(SPR_MQ, t0);
5336     tcg_gen_movi_tl(cpu_ca, 0);
5337     tcg_gen_brcondi_tl(TCG_COND_EQ, t1, 0, l1);
5338     tcg_gen_brcondi_tl(TCG_COND_GE, cpu_gpr[rS(ctx->opcode)], 0, l1);
5339     tcg_gen_movi_tl(cpu_ca, 1);
5340     gen_set_label(l1);
5341     tcg_gen_sari_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)], sh);
5342     tcg_temp_free(t0);
5343     tcg_temp_free(t1);
5344     if (unlikely(Rc(ctx->opcode) != 0))
5345         gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
5346 }
5347
5348 /* sraq - sraq. */
5349 static void gen_sraq(DisasContext *ctx)
5350 {
5351     int l1 = gen_new_label();
5352     int l2 = gen_new_label();
5353     TCGv t0 = tcg_temp_new();
5354     TCGv t1 = tcg_temp_local_new();
5355     TCGv t2 = tcg_temp_local_new();
5356     tcg_gen_andi_tl(t2, cpu_gpr[rB(ctx->opcode)], 0x1F);
5357     tcg_gen_shr_tl(t0, cpu_gpr[rS(ctx->opcode)], t2);
5358     tcg_gen_sar_tl(t1, cpu_gpr[rS(ctx->opcode)], t2);
5359     tcg_gen_subfi_tl(t2, 32, t2);
5360     tcg_gen_shl_tl(t2, cpu_gpr[rS(ctx->opcode)], t2);
5361     tcg_gen_or_tl(t0, t0, t2);
5362     gen_store_spr(SPR_MQ, t0);
5363     tcg_gen_andi_tl(t0, cpu_gpr[rB(ctx->opcode)], 0x20);
5364     tcg_gen_brcondi_tl(TCG_COND_EQ, t2, 0, l1);
5365     tcg_gen_mov_tl(t2, cpu_gpr[rS(ctx->opcode)]);
5366     tcg_gen_sari_tl(t1, cpu_gpr[rS(ctx->opcode)], 31);
5367     gen_set_label(l1);
5368     tcg_temp_free(t0);
5369     tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], t1);
5370     tcg_gen_movi_tl(cpu_ca, 0);
5371     tcg_gen_brcondi_tl(TCG_COND_GE, t1, 0, l2);
5372     tcg_gen_brcondi_tl(TCG_COND_EQ, t2, 0, l2);
5373     tcg_gen_movi_tl(cpu_ca, 1);
5374     gen_set_label(l2);
5375     tcg_temp_free(t1);
5376     tcg_temp_free(t2);
5377     if (unlikely(Rc(ctx->opcode) != 0))
5378         gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
5379 }
5380
5381 /* sre - sre. */
5382 static void gen_sre(DisasContext *ctx)
5383 {
5384     TCGv t0 = tcg_temp_new();
5385     TCGv t1 = tcg_temp_new();
5386     tcg_gen_andi_tl(t1, cpu_gpr[rB(ctx->opcode)], 0x1F);
5387     tcg_gen_shr_tl(t0, cpu_gpr[rS(ctx->opcode)], t1);
5388     tcg_gen_subfi_tl(t1, 32, t1);
5389     tcg_gen_shl_tl(t1, cpu_gpr[rS(ctx->opcode)], t1);
5390     tcg_gen_or_tl(t1, t0, t1);
5391     tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], t0);
5392     gen_store_spr(SPR_MQ, t1);
5393     tcg_temp_free(t0);
5394     tcg_temp_free(t1);
5395     if (unlikely(Rc(ctx->opcode) != 0))
5396         gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
5397 }
5398
5399 /* srea - srea. */
5400 static void gen_srea(DisasContext *ctx)
5401 {
5402     TCGv t0 = tcg_temp_new();
5403     TCGv t1 = tcg_temp_new();
5404     tcg_gen_andi_tl(t1, cpu_gpr[rB(ctx->opcode)], 0x1F);
5405     tcg_gen_rotr_tl(t0, cpu_gpr[rS(ctx->opcode)], t1);
5406     gen_store_spr(SPR_MQ, t0);
5407     tcg_gen_sar_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)], t1);
5408     tcg_temp_free(t0);
5409     tcg_temp_free(t1);
5410     if (unlikely(Rc(ctx->opcode) != 0))
5411         gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
5412 }
5413
5414 /* sreq */
5415 static void gen_sreq(DisasContext *ctx)
5416 {
5417     TCGv t0 = tcg_temp_new();
5418     TCGv t1 = tcg_temp_new();
5419     TCGv t2 = tcg_temp_new();
5420     tcg_gen_andi_tl(t0, cpu_gpr[rB(ctx->opcode)], 0x1F);
5421     tcg_gen_movi_tl(t1, 0xFFFFFFFF);
5422     tcg_gen_shr_tl(t1, t1, t0);
5423     tcg_gen_rotr_tl(t0, cpu_gpr[rS(ctx->opcode)], t0);
5424     gen_load_spr(t2, SPR_MQ);
5425     gen_store_spr(SPR_MQ, t0);
5426     tcg_gen_and_tl(t0, t0, t1);
5427     tcg_gen_andc_tl(t2, t2, t1);
5428     tcg_gen_or_tl(cpu_gpr[rA(ctx->opcode)], t0, t2);
5429     tcg_temp_free(t0);
5430     tcg_temp_free(t1);
5431     tcg_temp_free(t2);
5432     if (unlikely(Rc(ctx->opcode) != 0))
5433         gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
5434 }
5435
5436 /* sriq */
5437 static void gen_sriq(DisasContext *ctx)
5438 {
5439     int sh = SH(ctx->opcode);
5440     TCGv t0 = tcg_temp_new();
5441     TCGv t1 = tcg_temp_new();
5442     tcg_gen_shri_tl(t0, cpu_gpr[rS(ctx->opcode)], sh);
5443     tcg_gen_shli_tl(t1, cpu_gpr[rS(ctx->opcode)], 32 - sh);
5444     tcg_gen_or_tl(t1, t0, t1);
5445     tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], t0);
5446     gen_store_spr(SPR_MQ, t1);
5447     tcg_temp_free(t0);
5448     tcg_temp_free(t1);
5449     if (unlikely(Rc(ctx->opcode) != 0))
5450         gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
5451 }
5452
5453 /* srliq */
5454 static void gen_srliq(DisasContext *ctx)
5455 {
5456     int sh = SH(ctx->opcode);
5457     TCGv t0 = tcg_temp_new();
5458     TCGv t1 = tcg_temp_new();
5459     tcg_gen_rotri_tl(t0, cpu_gpr[rS(ctx->opcode)], sh);
5460     gen_load_spr(t1, SPR_MQ);
5461     gen_store_spr(SPR_MQ, t0);
5462     tcg_gen_andi_tl(t0, t0,  (0xFFFFFFFFU >> sh));
5463     tcg_gen_andi_tl(t1, t1, ~(0xFFFFFFFFU >> sh));
5464     tcg_gen_or_tl(cpu_gpr[rA(ctx->opcode)], t0, t1);
5465     tcg_temp_free(t0);
5466     tcg_temp_free(t1);
5467     if (unlikely(Rc(ctx->opcode) != 0))
5468         gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
5469 }
5470
5471 /* srlq */
5472 static void gen_srlq(DisasContext *ctx)
5473 {
5474     int l1 = gen_new_label();
5475     int l2 = gen_new_label();
5476     TCGv t0 = tcg_temp_local_new();
5477     TCGv t1 = tcg_temp_local_new();
5478     TCGv t2 = tcg_temp_local_new();
5479     tcg_gen_andi_tl(t2, cpu_gpr[rB(ctx->opcode)], 0x1F);
5480     tcg_gen_movi_tl(t1, 0xFFFFFFFF);
5481     tcg_gen_shr_tl(t2, t1, t2);
5482     tcg_gen_andi_tl(t0, cpu_gpr[rB(ctx->opcode)], 0x20);
5483     tcg_gen_brcondi_tl(TCG_COND_EQ, t0, 0, l1);
5484     gen_load_spr(t0, SPR_MQ);
5485     tcg_gen_and_tl(cpu_gpr[rA(ctx->opcode)], t0, t2);
5486     tcg_gen_br(l2);
5487     gen_set_label(l1);
5488     tcg_gen_shr_tl(t0, cpu_gpr[rS(ctx->opcode)], t2);
5489     tcg_gen_and_tl(t0, t0, t2);
5490     gen_load_spr(t1, SPR_MQ);
5491     tcg_gen_andc_tl(t1, t1, t2);
5492     tcg_gen_or_tl(cpu_gpr[rA(ctx->opcode)], t0, t1);
5493     gen_set_label(l2);
5494     tcg_temp_free(t0);
5495     tcg_temp_free(t1);
5496     tcg_temp_free(t2);
5497     if (unlikely(Rc(ctx->opcode) != 0))
5498         gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
5499 }
5500
5501 /* srq */
5502 static void gen_srq(DisasContext *ctx)
5503 {
5504     int l1 = gen_new_label();
5505     TCGv t0 = tcg_temp_new();
5506     TCGv t1 = tcg_temp_new();
5507     tcg_gen_andi_tl(t1, cpu_gpr[rB(ctx->opcode)], 0x1F);
5508     tcg_gen_shr_tl(t0, cpu_gpr[rS(ctx->opcode)], t1);
5509     tcg_gen_subfi_tl(t1, 32, t1);
5510     tcg_gen_shl_tl(t1, cpu_gpr[rS(ctx->opcode)], t1);
5511     tcg_gen_or_tl(t1, t0, t1);
5512     gen_store_spr(SPR_MQ, t1);
5513     tcg_gen_andi_tl(t1, cpu_gpr[rB(ctx->opcode)], 0x20);
5514     tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], t0);
5515     tcg_gen_brcondi_tl(TCG_COND_EQ, t0, 0, l1);
5516     tcg_gen_movi_tl(cpu_gpr[rA(ctx->opcode)], 0);
5517     gen_set_label(l1);
5518     tcg_temp_free(t0);
5519     tcg_temp_free(t1);
5520     if (unlikely(Rc(ctx->opcode) != 0))
5521         gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
5522 }
5523
5524 /* PowerPC 602 specific instructions */
5525
5526 /* dsa  */
5527 static void gen_dsa(DisasContext *ctx)
5528 {
5529     /* XXX: TODO */
5530     gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL);
5531 }
5532
5533 /* esa */
5534 static void gen_esa(DisasContext *ctx)
5535 {
5536     /* XXX: TODO */
5537     gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL);
5538 }
5539
5540 /* mfrom */
5541 static void gen_mfrom(DisasContext *ctx)
5542 {
5543 #if defined(CONFIG_USER_ONLY)
5544     gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
5545 #else
5546     if (unlikely(!ctx->mem_idx)) {
5547         gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
5548         return;
5549     }
5550     gen_helper_602_mfrom(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)]);
5551 #endif
5552 }
5553
5554 /* 602 - 603 - G2 TLB management */
5555
5556 /* tlbld */
5557 static void gen_tlbld_6xx(DisasContext *ctx)
5558 {
5559 #if defined(CONFIG_USER_ONLY)
5560     gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
5561 #else
5562     if (unlikely(!ctx->mem_idx)) {
5563         gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
5564         return;
5565     }
5566     gen_helper_6xx_tlbd(cpu_env, cpu_gpr[rB(ctx->opcode)]);
5567 #endif
5568 }
5569
5570 /* tlbli */
5571 static void gen_tlbli_6xx(DisasContext *ctx)
5572 {
5573 #if defined(CONFIG_USER_ONLY)
5574     gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
5575 #else
5576     if (unlikely(!ctx->mem_idx)) {
5577         gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
5578         return;
5579     }
5580     gen_helper_6xx_tlbi(cpu_env, cpu_gpr[rB(ctx->opcode)]);
5581 #endif
5582 }
5583
5584 /* 74xx TLB management */
5585
5586 /* tlbld */
5587 static void gen_tlbld_74xx(DisasContext *ctx)
5588 {
5589 #if defined(CONFIG_USER_ONLY)
5590     gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
5591 #else
5592     if (unlikely(!ctx->mem_idx)) {
5593         gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
5594         return;
5595     }
5596     gen_helper_74xx_tlbd(cpu_env, cpu_gpr[rB(ctx->opcode)]);
5597 #endif
5598 }
5599
5600 /* tlbli */
5601 static void gen_tlbli_74xx(DisasContext *ctx)
5602 {
5603 #if defined(CONFIG_USER_ONLY)
5604     gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
5605 #else
5606     if (unlikely(!ctx->mem_idx)) {
5607         gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
5608         return;
5609     }
5610     gen_helper_74xx_tlbi(cpu_env, cpu_gpr[rB(ctx->opcode)]);
5611 #endif
5612 }
5613
5614 /* POWER instructions not in PowerPC 601 */
5615
5616 /* clf */
5617 static void gen_clf(DisasContext *ctx)
5618 {
5619     /* Cache line flush: implemented as no-op */
5620 }
5621
5622 /* cli */
5623 static void gen_cli(DisasContext *ctx)
5624 {
5625     /* Cache line invalidate: privileged and treated as no-op */
5626 #if defined(CONFIG_USER_ONLY)
5627     gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
5628 #else
5629     if (unlikely(!ctx->mem_idx)) {
5630         gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
5631         return;
5632     }
5633 #endif
5634 }
5635
5636 /* dclst */
5637 static void gen_dclst(DisasContext *ctx)
5638 {
5639     /* Data cache line store: treated as no-op */
5640 }
5641
5642 static void gen_mfsri(DisasContext *ctx)
5643 {
5644 #if defined(CONFIG_USER_ONLY)
5645     gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
5646 #else
5647     int ra = rA(ctx->opcode);
5648     int rd = rD(ctx->opcode);
5649     TCGv t0;
5650     if (unlikely(!ctx->mem_idx)) {
5651         gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
5652         return;
5653     }
5654     t0 = tcg_temp_new();
5655     gen_addr_reg_index(ctx, t0);
5656     tcg_gen_shri_tl(t0, t0, 28);
5657     tcg_gen_andi_tl(t0, t0, 0xF);
5658     gen_helper_load_sr(cpu_gpr[rd], cpu_env, t0);
5659     tcg_temp_free(t0);
5660     if (ra != 0 && ra != rd)
5661         tcg_gen_mov_tl(cpu_gpr[ra], cpu_gpr[rd]);
5662 #endif
5663 }
5664
5665 static void gen_rac(DisasContext *ctx)
5666 {
5667 #if defined(CONFIG_USER_ONLY)
5668     gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
5669 #else
5670     TCGv t0;
5671     if (unlikely(!ctx->mem_idx)) {
5672         gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
5673         return;
5674     }
5675     t0 = tcg_temp_new();
5676     gen_addr_reg_index(ctx, t0);
5677     gen_helper_rac(cpu_gpr[rD(ctx->opcode)], cpu_env, t0);
5678     tcg_temp_free(t0);
5679 #endif
5680 }
5681
5682 static void gen_rfsvc(DisasContext *ctx)
5683 {
5684 #if defined(CONFIG_USER_ONLY)
5685     gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
5686 #else
5687     if (unlikely(!ctx->mem_idx)) {
5688         gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
5689         return;
5690     }
5691     gen_helper_rfsvc(cpu_env);
5692     gen_sync_exception(ctx);
5693 #endif
5694 }
5695
5696 /* svc is not implemented for now */
5697
5698 /* POWER2 specific instructions */
5699 /* Quad manipulation (load/store two floats at a time) */
5700
5701 /* lfq */
5702 static void gen_lfq(DisasContext *ctx)
5703 {
5704     int rd = rD(ctx->opcode);
5705     TCGv t0;
5706     gen_set_access_type(ctx, ACCESS_FLOAT);
5707     t0 = tcg_temp_new();
5708     gen_addr_imm_index(ctx, t0, 0);
5709     gen_qemu_ld64(ctx, cpu_fpr[rd], t0);
5710     gen_addr_add(ctx, t0, t0, 8);
5711     gen_qemu_ld64(ctx, cpu_fpr[(rd + 1) % 32], t0);
5712     tcg_temp_free(t0);
5713 }
5714
5715 /* lfqu */
5716 static void gen_lfqu(DisasContext *ctx)
5717 {
5718     int ra = rA(ctx->opcode);
5719     int rd = rD(ctx->opcode);
5720     TCGv t0, t1;
5721     gen_set_access_type(ctx, ACCESS_FLOAT);
5722     t0 = tcg_temp_new();
5723     t1 = tcg_temp_new();
5724     gen_addr_imm_index(ctx, t0, 0);
5725     gen_qemu_ld64(ctx, cpu_fpr[rd], t0);
5726     gen_addr_add(ctx, t1, t0, 8);
5727     gen_qemu_ld64(ctx, cpu_fpr[(rd + 1) % 32], t1);
5728     if (ra != 0)
5729         tcg_gen_mov_tl(cpu_gpr[ra], t0);
5730     tcg_temp_free(t0);
5731     tcg_temp_free(t1);
5732 }
5733
5734 /* lfqux */
5735 static void gen_lfqux(DisasContext *ctx)
5736 {
5737     int ra = rA(ctx->opcode);
5738     int rd = rD(ctx->opcode);
5739     gen_set_access_type(ctx, ACCESS_FLOAT);
5740     TCGv t0, t1;
5741     t0 = tcg_temp_new();
5742     gen_addr_reg_index(ctx, t0);
5743     gen_qemu_ld64(ctx, cpu_fpr[rd], t0);
5744     t1 = tcg_temp_new();
5745     gen_addr_add(ctx, t1, t0, 8);
5746     gen_qemu_ld64(ctx, cpu_fpr[(rd + 1) % 32], t1);
5747     tcg_temp_free(t1);
5748     if (ra != 0)
5749         tcg_gen_mov_tl(cpu_gpr[ra], t0);
5750     tcg_temp_free(t0);
5751 }
5752
5753 /* lfqx */
5754 static void gen_lfqx(DisasContext *ctx)
5755 {
5756     int rd = rD(ctx->opcode);
5757     TCGv t0;
5758     gen_set_access_type(ctx, ACCESS_FLOAT);
5759     t0 = tcg_temp_new();
5760     gen_addr_reg_index(ctx, t0);
5761     gen_qemu_ld64(ctx, cpu_fpr[rd], t0);
5762     gen_addr_add(ctx, t0, t0, 8);
5763     gen_qemu_ld64(ctx, cpu_fpr[(rd + 1) % 32], t0);
5764     tcg_temp_free(t0);
5765 }
5766
5767 /* stfq */
5768 static void gen_stfq(DisasContext *ctx)
5769 {
5770     int rd = rD(ctx->opcode);
5771     TCGv t0;
5772     gen_set_access_type(ctx, ACCESS_FLOAT);
5773     t0 = tcg_temp_new();
5774     gen_addr_imm_index(ctx, t0, 0);
5775     gen_qemu_st64(ctx, cpu_fpr[rd], t0);
5776     gen_addr_add(ctx, t0, t0, 8);
5777     gen_qemu_st64(ctx, cpu_fpr[(rd + 1) % 32], t0);
5778     tcg_temp_free(t0);
5779 }
5780
5781 /* stfqu */
5782 static void gen_stfqu(DisasContext *ctx)
5783 {
5784     int ra = rA(ctx->opcode);
5785     int rd = rD(ctx->opcode);
5786     TCGv t0, t1;
5787     gen_set_access_type(ctx, ACCESS_FLOAT);
5788     t0 = tcg_temp_new();
5789     gen_addr_imm_index(ctx, t0, 0);
5790     gen_qemu_st64(ctx, cpu_fpr[rd], t0);
5791     t1 = tcg_temp_new();
5792     gen_addr_add(ctx, t1, t0, 8);
5793     gen_qemu_st64(ctx, cpu_fpr[(rd + 1) % 32], t1);
5794     tcg_temp_free(t1);
5795     if (ra != 0)
5796         tcg_gen_mov_tl(cpu_gpr[ra], t0);
5797     tcg_temp_free(t0);
5798 }
5799
5800 /* stfqux */
5801 static void gen_stfqux(DisasContext *ctx)
5802 {
5803     int ra = rA(ctx->opcode);
5804     int rd = rD(ctx->opcode);
5805     TCGv t0, t1;
5806     gen_set_access_type(ctx, ACCESS_FLOAT);
5807     t0 = tcg_temp_new();
5808     gen_addr_reg_index(ctx, t0);
5809     gen_qemu_st64(ctx, cpu_fpr[rd], t0);
5810     t1 = tcg_temp_new();
5811     gen_addr_add(ctx, t1, t0, 8);
5812     gen_qemu_st64(ctx, cpu_fpr[(rd + 1) % 32], t1);
5813     tcg_temp_free(t1);
5814     if (ra != 0)
5815         tcg_gen_mov_tl(cpu_gpr[ra], t0);
5816     tcg_temp_free(t0);
5817 }
5818
5819 /* stfqx */
5820 static void gen_stfqx(DisasContext *ctx)
5821 {
5822     int rd = rD(ctx->opcode);
5823     TCGv t0;
5824     gen_set_access_type(ctx, ACCESS_FLOAT);
5825     t0 = tcg_temp_new();
5826     gen_addr_reg_index(ctx, t0);
5827     gen_qemu_st64(ctx, cpu_fpr[rd], t0);
5828     gen_addr_add(ctx, t0, t0, 8);
5829     gen_qemu_st64(ctx, cpu_fpr[(rd + 1) % 32], t0);
5830     tcg_temp_free(t0);
5831 }
5832
5833 /* BookE specific instructions */
5834
5835 /* XXX: not implemented on 440 ? */
5836 static void gen_mfapidi(DisasContext *ctx)
5837 {
5838     /* XXX: TODO */
5839     gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL);
5840 }
5841
5842 /* XXX: not implemented on 440 ? */
5843 static void gen_tlbiva(DisasContext *ctx)
5844 {
5845 #if defined(CONFIG_USER_ONLY)
5846     gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
5847 #else
5848     TCGv t0;
5849     if (unlikely(!ctx->mem_idx)) {
5850         gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
5851         return;
5852     }
5853     t0 = tcg_temp_new();
5854     gen_addr_reg_index(ctx, t0);
5855     gen_helper_tlbie(cpu_env, cpu_gpr[rB(ctx->opcode)]);
5856     tcg_temp_free(t0);
5857 #endif
5858 }
5859
5860 /* All 405 MAC instructions are translated here */
5861 static inline void gen_405_mulladd_insn(DisasContext *ctx, int opc2, int opc3,
5862                                         int ra, int rb, int rt, int Rc)
5863 {
5864     TCGv t0, t1;
5865
5866     t0 = tcg_temp_local_new();
5867     t1 = tcg_temp_local_new();
5868
5869     switch (opc3 & 0x0D) {
5870     case 0x05:
5871         /* macchw    - macchw.    - macchwo   - macchwo.   */
5872         /* macchws   - macchws.   - macchwso  - macchwso.  */
5873         /* nmacchw   - nmacchw.   - nmacchwo  - nmacchwo.  */
5874         /* nmacchws  - nmacchws.  - nmacchwso - nmacchwso. */
5875         /* mulchw - mulchw. */
5876         tcg_gen_ext16s_tl(t0, cpu_gpr[ra]);
5877         tcg_gen_sari_tl(t1, cpu_gpr[rb], 16);
5878         tcg_gen_ext16s_tl(t1, t1);
5879         break;
5880     case 0x04:
5881         /* macchwu   - macchwu.   - macchwuo  - macchwuo.  */
5882         /* macchwsu  - macchwsu.  - macchwsuo - macchwsuo. */
5883         /* mulchwu - mulchwu. */
5884         tcg_gen_ext16u_tl(t0, cpu_gpr[ra]);
5885         tcg_gen_shri_tl(t1, cpu_gpr[rb], 16);
5886         tcg_gen_ext16u_tl(t1, t1);
5887         break;
5888     case 0x01:
5889         /* machhw    - machhw.    - machhwo   - machhwo.   */
5890         /* machhws   - machhws.   - machhwso  - machhwso.  */
5891         /* nmachhw   - nmachhw.   - nmachhwo  - nmachhwo.  */
5892         /* nmachhws  - nmachhws.  - nmachhwso - nmachhwso. */
5893         /* mulhhw - mulhhw. */
5894         tcg_gen_sari_tl(t0, cpu_gpr[ra], 16);
5895         tcg_gen_ext16s_tl(t0, t0);
5896         tcg_gen_sari_tl(t1, cpu_gpr[rb], 16);
5897         tcg_gen_ext16s_tl(t1, t1);
5898         break;
5899     case 0x00:
5900         /* machhwu   - machhwu.   - machhwuo  - machhwuo.  */
5901         /* machhwsu  - machhwsu.  - machhwsuo - machhwsuo. */
5902         /* mulhhwu - mulhhwu. */
5903         tcg_gen_shri_tl(t0, cpu_gpr[ra], 16);
5904         tcg_gen_ext16u_tl(t0, t0);
5905         tcg_gen_shri_tl(t1, cpu_gpr[rb], 16);
5906         tcg_gen_ext16u_tl(t1, t1);
5907         break;
5908     case 0x0D:
5909         /* maclhw    - maclhw.    - maclhwo   - maclhwo.   */
5910         /* maclhws   - maclhws.   - maclhwso  - maclhwso.  */
5911         /* nmaclhw   - nmaclhw.   - nmaclhwo  - nmaclhwo.  */
5912         /* nmaclhws  - nmaclhws.  - nmaclhwso - nmaclhwso. */
5913         /* mullhw - mullhw. */
5914         tcg_gen_ext16s_tl(t0, cpu_gpr[ra]);
5915         tcg_gen_ext16s_tl(t1, cpu_gpr[rb]);
5916         break;
5917     case 0x0C:
5918         /* maclhwu   - maclhwu.   - maclhwuo  - maclhwuo.  */
5919         /* maclhwsu  - maclhwsu.  - maclhwsuo - maclhwsuo. */
5920         /* mullhwu - mullhwu. */
5921         tcg_gen_ext16u_tl(t0, cpu_gpr[ra]);
5922         tcg_gen_ext16u_tl(t1, cpu_gpr[rb]);
5923         break;
5924     }
5925     if (opc2 & 0x04) {
5926         /* (n)multiply-and-accumulate (0x0C / 0x0E) */
5927         tcg_gen_mul_tl(t1, t0, t1);
5928         if (opc2 & 0x02) {
5929             /* nmultiply-and-accumulate (0x0E) */
5930             tcg_gen_sub_tl(t0, cpu_gpr[rt], t1);
5931         } else {
5932             /* multiply-and-accumulate (0x0C) */
5933             tcg_gen_add_tl(t0, cpu_gpr[rt], t1);
5934         }
5935
5936         if (opc3 & 0x12) {
5937             /* Check overflow and/or saturate */
5938             int l1 = gen_new_label();
5939
5940             if (opc3 & 0x10) {
5941                 /* Start with XER OV disabled, the most likely case */
5942                 tcg_gen_movi_tl(cpu_ov, 0);
5943             }
5944             if (opc3 & 0x01) {
5945                 /* Signed */
5946                 tcg_gen_xor_tl(t1, cpu_gpr[rt], t1);
5947                 tcg_gen_brcondi_tl(TCG_COND_GE, t1, 0, l1);
5948                 tcg_gen_xor_tl(t1, cpu_gpr[rt], t0);
5949                 tcg_gen_brcondi_tl(TCG_COND_LT, t1, 0, l1);
5950                 if (opc3 & 0x02) {
5951                     /* Saturate */
5952                     tcg_gen_sari_tl(t0, cpu_gpr[rt], 31);
5953                     tcg_gen_xori_tl(t0, t0, 0x7fffffff);
5954                 }
5955             } else {
5956                 /* Unsigned */
5957                 tcg_gen_brcond_tl(TCG_COND_GEU, t0, t1, l1);
5958                 if (opc3 & 0x02) {
5959                     /* Saturate */
5960                     tcg_gen_movi_tl(t0, UINT32_MAX);
5961                 }
5962             }
5963             if (opc3 & 0x10) {
5964                 /* Check overflow */
5965                 tcg_gen_movi_tl(cpu_ov, 1);
5966                 tcg_gen_movi_tl(cpu_so, 1);
5967             }
5968             gen_set_label(l1);
5969             tcg_gen_mov_tl(cpu_gpr[rt], t0);
5970         }
5971     } else {
5972         tcg_gen_mul_tl(cpu_gpr[rt], t0, t1);
5973     }
5974     tcg_temp_free(t0);
5975     tcg_temp_free(t1);
5976     if (unlikely(Rc) != 0) {
5977         /* Update Rc0 */
5978         gen_set_Rc0(ctx, cpu_gpr[rt]);
5979     }
5980 }
5981
5982 #define GEN_MAC_HANDLER(name, opc2, opc3)                                     \
5983 static void glue(gen_, name)(DisasContext *ctx)                               \
5984 {                                                                             \
5985     gen_405_mulladd_insn(ctx, opc2, opc3, rA(ctx->opcode), rB(ctx->opcode),   \
5986                          rD(ctx->opcode), Rc(ctx->opcode));                   \
5987 }
5988
5989 /* macchw    - macchw.    */
5990 GEN_MAC_HANDLER(macchw, 0x0C, 0x05);
5991 /* macchwo   - macchwo.   */
5992 GEN_MAC_HANDLER(macchwo, 0x0C, 0x15);
5993 /* macchws   - macchws.   */
5994 GEN_MAC_HANDLER(macchws, 0x0C, 0x07);
5995 /* macchwso  - macchwso.  */
5996 GEN_MAC_HANDLER(macchwso, 0x0C, 0x17);
5997 /* macchwsu  - macchwsu.  */
5998 GEN_MAC_HANDLER(macchwsu, 0x0C, 0x06);
5999 /* macchwsuo - macchwsuo. */
6000 GEN_MAC_HANDLER(macchwsuo, 0x0C, 0x16);
6001 /* macchwu   - macchwu.   */
6002 GEN_MAC_HANDLER(macchwu, 0x0C, 0x04);
6003 /* macchwuo  - macchwuo.  */
6004 GEN_MAC_HANDLER(macchwuo, 0x0C, 0x14);
6005 /* machhw    - machhw.    */
6006 GEN_MAC_HANDLER(machhw, 0x0C, 0x01);
6007 /* machhwo   - machhwo.   */
6008 GEN_MAC_HANDLER(machhwo, 0x0C, 0x11);
6009 /* machhws   - machhws.   */
6010 GEN_MAC_HANDLER(machhws, 0x0C, 0x03);
6011 /* machhwso  - machhwso.  */
6012 GEN_MAC_HANDLER(machhwso, 0x0C, 0x13);
6013 /* machhwsu  - machhwsu.  */
6014 GEN_MAC_HANDLER(machhwsu, 0x0C, 0x02);
6015 /* machhwsuo - machhwsuo. */
6016 GEN_MAC_HANDLER(machhwsuo, 0x0C, 0x12);
6017 /* machhwu   - machhwu.   */
6018 GEN_MAC_HANDLER(machhwu, 0x0C, 0x00);
6019 /* machhwuo  - machhwuo.  */
6020 GEN_MAC_HANDLER(machhwuo, 0x0C, 0x10);
6021 /* maclhw    - maclhw.    */
6022 GEN_MAC_HANDLER(maclhw, 0x0C, 0x0D);
6023 /* maclhwo   - maclhwo.   */
6024 GEN_MAC_HANDLER(maclhwo, 0x0C, 0x1D);
6025 /* maclhws   - maclhws.   */
6026 GEN_MAC_HANDLER(maclhws, 0x0C, 0x0F);
6027 /* maclhwso  - maclhwso.  */
6028 GEN_MAC_HANDLER(maclhwso, 0x0C, 0x1F);
6029 /* maclhwu   - maclhwu.   */
6030 GEN_MAC_HANDLER(maclhwu, 0x0C, 0x0C);
6031 /* maclhwuo  - maclhwuo.  */
6032 GEN_MAC_HANDLER(maclhwuo, 0x0C, 0x1C);
6033 /* maclhwsu  - maclhwsu.  */
6034 GEN_MAC_HANDLER(maclhwsu, 0x0C, 0x0E);
6035 /* maclhwsuo - maclhwsuo. */
6036 GEN_MAC_HANDLER(maclhwsuo, 0x0C, 0x1E);
6037 /* nmacchw   - nmacchw.   */
6038 GEN_MAC_HANDLER(nmacchw, 0x0E, 0x05);
6039 /* nmacchwo  - nmacchwo.  */
6040 GEN_MAC_HANDLER(nmacchwo, 0x0E, 0x15);
6041 /* nmacchws  - nmacchws.  */
6042 GEN_MAC_HANDLER(nmacchws, 0x0E, 0x07);
6043 /* nmacchwso - nmacchwso. */
6044 GEN_MAC_HANDLER(nmacchwso, 0x0E, 0x17);
6045 /* nmachhw   - nmachhw.   */
6046 GEN_MAC_HANDLER(nmachhw, 0x0E, 0x01);
6047 /* nmachhwo  - nmachhwo.  */
6048 GEN_MAC_HANDLER(nmachhwo, 0x0E, 0x11);
6049 /* nmachhws  - nmachhws.  */
6050 GEN_MAC_HANDLER(nmachhws, 0x0E, 0x03);
6051 /* nmachhwso - nmachhwso. */
6052 GEN_MAC_HANDLER(nmachhwso, 0x0E, 0x13);
6053 /* nmaclhw   - nmaclhw.   */
6054 GEN_MAC_HANDLER(nmaclhw, 0x0E, 0x0D);
6055 /* nmaclhwo  - nmaclhwo.  */
6056 GEN_MAC_HANDLER(nmaclhwo, 0x0E, 0x1D);
6057 /* nmaclhws  - nmaclhws.  */
6058 GEN_MAC_HANDLER(nmaclhws, 0x0E, 0x0F);
6059 /* nmaclhwso - nmaclhwso. */
6060 GEN_MAC_HANDLER(nmaclhwso, 0x0E, 0x1F);
6061
6062 /* mulchw  - mulchw.  */
6063 GEN_MAC_HANDLER(mulchw, 0x08, 0x05);
6064 /* mulchwu - mulchwu. */
6065 GEN_MAC_HANDLER(mulchwu, 0x08, 0x04);
6066 /* mulhhw  - mulhhw.  */
6067 GEN_MAC_HANDLER(mulhhw, 0x08, 0x01);
6068 /* mulhhwu - mulhhwu. */
6069 GEN_MAC_HANDLER(mulhhwu, 0x08, 0x00);
6070 /* mullhw  - mullhw.  */
6071 GEN_MAC_HANDLER(mullhw, 0x08, 0x0D);
6072 /* mullhwu - mullhwu. */
6073 GEN_MAC_HANDLER(mullhwu, 0x08, 0x0C);
6074
6075 /* mfdcr */
6076 static void gen_mfdcr(DisasContext *ctx)
6077 {
6078 #if defined(CONFIG_USER_ONLY)
6079     gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
6080 #else
6081     TCGv dcrn;
6082     if (unlikely(!ctx->mem_idx)) {
6083         gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
6084         return;
6085     }
6086     /* NIP cannot be restored if the memory exception comes from an helper */
6087     gen_update_nip(ctx, ctx->nip - 4);
6088     dcrn = tcg_const_tl(SPR(ctx->opcode));
6089     gen_helper_load_dcr(cpu_gpr[rD(ctx->opcode)], cpu_env, dcrn);
6090     tcg_temp_free(dcrn);
6091 #endif
6092 }
6093
6094 /* mtdcr */
6095 static void gen_mtdcr(DisasContext *ctx)
6096 {
6097 #if defined(CONFIG_USER_ONLY)
6098     gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
6099 #else
6100     TCGv dcrn;
6101     if (unlikely(!ctx->mem_idx)) {
6102         gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
6103         return;
6104     }
6105     /* NIP cannot be restored if the memory exception comes from an helper */
6106     gen_update_nip(ctx, ctx->nip - 4);
6107     dcrn = tcg_const_tl(SPR(ctx->opcode));
6108     gen_helper_store_dcr(cpu_env, dcrn, cpu_gpr[rS(ctx->opcode)]);
6109     tcg_temp_free(dcrn);
6110 #endif
6111 }
6112
6113 /* mfdcrx */
6114 /* XXX: not implemented on 440 ? */
6115 static void gen_mfdcrx(DisasContext *ctx)
6116 {
6117 #if defined(CONFIG_USER_ONLY)
6118     gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
6119 #else
6120     if (unlikely(!ctx->mem_idx)) {
6121         gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
6122         return;
6123     }
6124     /* NIP cannot be restored if the memory exception comes from an helper */
6125     gen_update_nip(ctx, ctx->nip - 4);
6126     gen_helper_load_dcr(cpu_gpr[rD(ctx->opcode)], cpu_env,
6127                         cpu_gpr[rA(ctx->opcode)]);
6128     /* Note: Rc update flag set leads to undefined state of Rc0 */
6129 #endif
6130 }
6131
6132 /* mtdcrx */
6133 /* XXX: not implemented on 440 ? */
6134 static void gen_mtdcrx(DisasContext *ctx)
6135 {
6136 #if defined(CONFIG_USER_ONLY)
6137     gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
6138 #else
6139     if (unlikely(!ctx->mem_idx)) {
6140         gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
6141         return;
6142     }
6143     /* NIP cannot be restored if the memory exception comes from an helper */
6144     gen_update_nip(ctx, ctx->nip - 4);
6145     gen_helper_store_dcr(cpu_env, cpu_gpr[rA(ctx->opcode)],
6146                          cpu_gpr[rS(ctx->opcode)]);
6147     /* Note: Rc update flag set leads to undefined state of Rc0 */
6148 #endif
6149 }
6150
6151 /* mfdcrux (PPC 460) : user-mode access to DCR */
6152 static void gen_mfdcrux(DisasContext *ctx)
6153 {
6154     /* NIP cannot be restored if the memory exception comes from an helper */
6155     gen_update_nip(ctx, ctx->nip - 4);
6156     gen_helper_load_dcr(cpu_gpr[rD(ctx->opcode)], cpu_env,
6157                         cpu_gpr[rA(ctx->opcode)]);
6158     /* Note: Rc update flag set leads to undefined state of Rc0 */
6159 }
6160
6161 /* mtdcrux (PPC 460) : user-mode access to DCR */
6162 static void gen_mtdcrux(DisasContext *ctx)
6163 {
6164     /* NIP cannot be restored if the memory exception comes from an helper */
6165     gen_update_nip(ctx, ctx->nip - 4);
6166     gen_helper_store_dcr(cpu_env, cpu_gpr[rA(ctx->opcode)],
6167                          cpu_gpr[rS(ctx->opcode)]);
6168     /* Note: Rc update flag set leads to undefined state of Rc0 */
6169 }
6170
6171 /* dccci */
6172 static void gen_dccci(DisasContext *ctx)
6173 {
6174 #if defined(CONFIG_USER_ONLY)
6175     gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
6176 #else
6177     if (unlikely(!ctx->mem_idx)) {
6178         gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
6179         return;
6180     }
6181     /* interpreted as no-op */
6182 #endif
6183 }
6184
6185 /* dcread */
6186 static void gen_dcread(DisasContext *ctx)
6187 {
6188 #if defined(CONFIG_USER_ONLY)
6189     gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
6190 #else
6191     TCGv EA, val;
6192     if (unlikely(!ctx->mem_idx)) {
6193         gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
6194         return;
6195     }
6196     gen_set_access_type(ctx, ACCESS_CACHE);
6197     EA = tcg_temp_new();
6198     gen_addr_reg_index(ctx, EA);
6199     val = tcg_temp_new();
6200     gen_qemu_ld32u(ctx, val, EA);
6201     tcg_temp_free(val);
6202     tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], EA);
6203     tcg_temp_free(EA);
6204 #endif
6205 }
6206
6207 /* icbt */
6208 static void gen_icbt_40x(DisasContext *ctx)
6209 {
6210     /* interpreted as no-op */
6211     /* XXX: specification say this is treated as a load by the MMU
6212      *      but does not generate any exception
6213      */
6214 }
6215
6216 /* iccci */
6217 static void gen_iccci(DisasContext *ctx)
6218 {
6219 #if defined(CONFIG_USER_ONLY)
6220     gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
6221 #else
6222     if (unlikely(!ctx->mem_idx)) {
6223         gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
6224         return;
6225     }
6226     /* interpreted as no-op */
6227 #endif
6228 }
6229
6230 /* icread */
6231 static void gen_icread(DisasContext *ctx)
6232 {
6233 #if defined(CONFIG_USER_ONLY)
6234     gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
6235 #else
6236     if (unlikely(!ctx->mem_idx)) {
6237         gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
6238         return;
6239     }
6240     /* interpreted as no-op */
6241 #endif
6242 }
6243
6244 /* rfci (mem_idx only) */
6245 static void gen_rfci_40x(DisasContext *ctx)
6246 {
6247 #if defined(CONFIG_USER_ONLY)
6248     gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
6249 #else
6250     if (unlikely(!ctx->mem_idx)) {
6251         gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
6252         return;
6253     }
6254     /* Restore CPU state */
6255     gen_helper_40x_rfci(cpu_env);
6256     gen_sync_exception(ctx);
6257 #endif
6258 }
6259
6260 static void gen_rfci(DisasContext *ctx)
6261 {
6262 #if defined(CONFIG_USER_ONLY)
6263     gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
6264 #else
6265     if (unlikely(!ctx->mem_idx)) {
6266         gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
6267         return;
6268     }
6269     /* Restore CPU state */
6270     gen_helper_rfci(cpu_env);
6271     gen_sync_exception(ctx);
6272 #endif
6273 }
6274
6275 /* BookE specific */
6276
6277 /* XXX: not implemented on 440 ? */
6278 static void gen_rfdi(DisasContext *ctx)
6279 {
6280 #if defined(CONFIG_USER_ONLY)
6281     gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
6282 #else
6283     if (unlikely(!ctx->mem_idx)) {
6284         gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
6285         return;
6286     }
6287     /* Restore CPU state */
6288     gen_helper_rfdi(cpu_env);
6289     gen_sync_exception(ctx);
6290 #endif
6291 }
6292
6293 /* XXX: not implemented on 440 ? */
6294 static void gen_rfmci(DisasContext *ctx)
6295 {
6296 #if defined(CONFIG_USER_ONLY)
6297     gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
6298 #else
6299     if (unlikely(!ctx->mem_idx)) {
6300         gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
6301         return;
6302     }
6303     /* Restore CPU state */
6304     gen_helper_rfmci(cpu_env);
6305     gen_sync_exception(ctx);
6306 #endif
6307 }
6308
6309 /* TLB management - PowerPC 405 implementation */
6310
6311 /* tlbre */
6312 static void gen_tlbre_40x(DisasContext *ctx)
6313 {
6314 #if defined(CONFIG_USER_ONLY)
6315     gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
6316 #else
6317     if (unlikely(!ctx->mem_idx)) {
6318         gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
6319         return;
6320     }
6321     switch (rB(ctx->opcode)) {
6322     case 0:
6323         gen_helper_4xx_tlbre_hi(cpu_gpr[rD(ctx->opcode)], cpu_env,
6324                                 cpu_gpr[rA(ctx->opcode)]);
6325         break;
6326     case 1:
6327         gen_helper_4xx_tlbre_lo(cpu_gpr[rD(ctx->opcode)], cpu_env,
6328                                 cpu_gpr[rA(ctx->opcode)]);
6329         break;
6330     default:
6331         gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL);
6332         break;
6333     }
6334 #endif
6335 }
6336
6337 /* tlbsx - tlbsx. */
6338 static void gen_tlbsx_40x(DisasContext *ctx)
6339 {
6340 #if defined(CONFIG_USER_ONLY)
6341     gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
6342 #else
6343     TCGv t0;
6344     if (unlikely(!ctx->mem_idx)) {
6345         gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
6346         return;
6347     }
6348     t0 = tcg_temp_new();
6349     gen_addr_reg_index(ctx, t0);
6350     gen_helper_4xx_tlbsx(cpu_gpr[rD(ctx->opcode)], cpu_env, t0);
6351     tcg_temp_free(t0);
6352     if (Rc(ctx->opcode)) {
6353         int l1 = gen_new_label();
6354         tcg_gen_trunc_tl_i32(cpu_crf[0], cpu_so);
6355         tcg_gen_brcondi_tl(TCG_COND_EQ, cpu_gpr[rD(ctx->opcode)], -1, l1);
6356         tcg_gen_ori_i32(cpu_crf[0], cpu_crf[0], 0x02);
6357         gen_set_label(l1);
6358     }
6359 #endif
6360 }
6361
6362 /* tlbwe */
6363 static void gen_tlbwe_40x(DisasContext *ctx)
6364 {
6365 #if defined(CONFIG_USER_ONLY)
6366     gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
6367 #else
6368     if (unlikely(!ctx->mem_idx)) {
6369         gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
6370         return;
6371     }
6372     switch (rB(ctx->opcode)) {
6373     case 0:
6374         gen_helper_4xx_tlbwe_hi(cpu_env, cpu_gpr[rA(ctx->opcode)],
6375                                 cpu_gpr[rS(ctx->opcode)]);
6376         break;
6377     case 1:
6378         gen_helper_4xx_tlbwe_lo(cpu_env, cpu_gpr[rA(ctx->opcode)],
6379                                 cpu_gpr[rS(ctx->opcode)]);
6380         break;
6381     default:
6382         gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL);
6383         break;
6384     }
6385 #endif
6386 }
6387
6388 /* TLB management - PowerPC 440 implementation */
6389
6390 /* tlbre */
6391 static void gen_tlbre_440(DisasContext *ctx)
6392 {
6393 #if defined(CONFIG_USER_ONLY)
6394     gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
6395 #else
6396     if (unlikely(!ctx->mem_idx)) {
6397         gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
6398         return;
6399     }
6400     switch (rB(ctx->opcode)) {
6401     case 0:
6402     case 1:
6403     case 2:
6404         {
6405             TCGv_i32 t0 = tcg_const_i32(rB(ctx->opcode));
6406             gen_helper_440_tlbre(cpu_gpr[rD(ctx->opcode)], cpu_env,
6407                                  t0, cpu_gpr[rA(ctx->opcode)]);
6408             tcg_temp_free_i32(t0);
6409         }
6410         break;
6411     default:
6412         gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL);
6413         break;
6414     }
6415 #endif
6416 }
6417
6418 /* tlbsx - tlbsx. */
6419 static void gen_tlbsx_440(DisasContext *ctx)
6420 {
6421 #if defined(CONFIG_USER_ONLY)
6422     gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
6423 #else
6424     TCGv t0;
6425     if (unlikely(!ctx->mem_idx)) {
6426         gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
6427         return;
6428     }
6429     t0 = tcg_temp_new();
6430     gen_addr_reg_index(ctx, t0);
6431     gen_helper_440_tlbsx(cpu_gpr[rD(ctx->opcode)], cpu_env, t0);
6432     tcg_temp_free(t0);
6433     if (Rc(ctx->opcode)) {
6434         int l1 = gen_new_label();
6435         tcg_gen_trunc_tl_i32(cpu_crf[0], cpu_so);
6436         tcg_gen_brcondi_tl(TCG_COND_EQ, cpu_gpr[rD(ctx->opcode)], -1, l1);
6437         tcg_gen_ori_i32(cpu_crf[0], cpu_crf[0], 0x02);
6438         gen_set_label(l1);
6439     }
6440 #endif
6441 }
6442
6443 /* tlbwe */
6444 static void gen_tlbwe_440(DisasContext *ctx)
6445 {
6446 #if defined(CONFIG_USER_ONLY)
6447     gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
6448 #else
6449     if (unlikely(!ctx->mem_idx)) {
6450         gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
6451         return;
6452     }
6453     switch (rB(ctx->opcode)) {
6454     case 0:
6455     case 1:
6456     case 2:
6457         {
6458             TCGv_i32 t0 = tcg_const_i32(rB(ctx->opcode));
6459             gen_helper_440_tlbwe(cpu_env, t0, cpu_gpr[rA(ctx->opcode)],
6460                                  cpu_gpr[rS(ctx->opcode)]);
6461             tcg_temp_free_i32(t0);
6462         }
6463         break;
6464     default:
6465         gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL);
6466         break;
6467     }
6468 #endif
6469 }
6470
6471 /* TLB management - PowerPC BookE 2.06 implementation */
6472
6473 /* tlbre */
6474 static void gen_tlbre_booke206(DisasContext *ctx)
6475 {
6476 #if defined(CONFIG_USER_ONLY)
6477     gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
6478 #else
6479     if (unlikely(!ctx->mem_idx)) {
6480         gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
6481         return;
6482     }
6483
6484     gen_helper_booke206_tlbre(cpu_env);
6485 #endif
6486 }
6487
6488 /* tlbsx - tlbsx. */
6489 static void gen_tlbsx_booke206(DisasContext *ctx)
6490 {
6491 #if defined(CONFIG_USER_ONLY)
6492     gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
6493 #else
6494     TCGv t0;
6495     if (unlikely(!ctx->mem_idx)) {
6496         gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
6497         return;
6498     }
6499
6500     if (rA(ctx->opcode)) {
6501         t0 = tcg_temp_new();
6502         tcg_gen_mov_tl(t0, cpu_gpr[rD(ctx->opcode)]);
6503     } else {
6504         t0 = tcg_const_tl(0);
6505     }
6506
6507     tcg_gen_add_tl(t0, t0, cpu_gpr[rB(ctx->opcode)]);
6508     gen_helper_booke206_tlbsx(cpu_env, t0);
6509     tcg_temp_free(t0);
6510 #endif
6511 }
6512
6513 /* tlbwe */
6514 static void gen_tlbwe_booke206(DisasContext *ctx)
6515 {
6516 #if defined(CONFIG_USER_ONLY)
6517     gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
6518 #else
6519     if (unlikely(!ctx->mem_idx)) {
6520         gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
6521         return;
6522     }
6523     gen_update_nip(ctx, ctx->nip - 4);
6524     gen_helper_booke206_tlbwe(cpu_env);
6525 #endif
6526 }
6527
6528 static void gen_tlbivax_booke206(DisasContext *ctx)
6529 {
6530 #if defined(CONFIG_USER_ONLY)
6531     gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
6532 #else
6533     TCGv t0;
6534     if (unlikely(!ctx->mem_idx)) {
6535         gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
6536         return;
6537     }
6538
6539     t0 = tcg_temp_new();
6540     gen_addr_reg_index(ctx, t0);
6541
6542     gen_helper_booke206_tlbivax(cpu_env, t0);
6543     tcg_temp_free(t0);
6544 #endif
6545 }
6546
6547 static void gen_tlbilx_booke206(DisasContext *ctx)
6548 {
6549 #if defined(CONFIG_USER_ONLY)
6550     gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
6551 #else
6552     TCGv t0;
6553     if (unlikely(!ctx->mem_idx)) {
6554         gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
6555         return;
6556     }
6557
6558     t0 = tcg_temp_new();
6559     gen_addr_reg_index(ctx, t0);
6560
6561     switch((ctx->opcode >> 21) & 0x3) {
6562     case 0:
6563         gen_helper_booke206_tlbilx0(cpu_env, t0);
6564         break;
6565     case 1:
6566         gen_helper_booke206_tlbilx1(cpu_env, t0);
6567         break;
6568     case 3:
6569         gen_helper_booke206_tlbilx3(cpu_env, t0);
6570         break;
6571     default:
6572         gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL);
6573         break;
6574     }
6575
6576     tcg_temp_free(t0);
6577 #endif
6578 }
6579
6580
6581 /* wrtee */
6582 static void gen_wrtee(DisasContext *ctx)
6583 {
6584 #if defined(CONFIG_USER_ONLY)
6585     gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
6586 #else
6587     TCGv t0;
6588     if (unlikely(!ctx->mem_idx)) {
6589         gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
6590         return;
6591     }
6592     t0 = tcg_temp_new();
6593     tcg_gen_andi_tl(t0, cpu_gpr[rD(ctx->opcode)], (1 << MSR_EE));
6594     tcg_gen_andi_tl(cpu_msr, cpu_msr, ~(1 << MSR_EE));
6595     tcg_gen_or_tl(cpu_msr, cpu_msr, t0);
6596     tcg_temp_free(t0);
6597     /* Stop translation to have a chance to raise an exception
6598      * if we just set msr_ee to 1
6599      */
6600     gen_stop_exception(ctx);
6601 #endif
6602 }
6603
6604 /* wrteei */
6605 static void gen_wrteei(DisasContext *ctx)
6606 {
6607 #if defined(CONFIG_USER_ONLY)
6608     gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
6609 #else
6610     if (unlikely(!ctx->mem_idx)) {
6611         gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
6612         return;
6613     }
6614     if (ctx->opcode & 0x00008000) {
6615         tcg_gen_ori_tl(cpu_msr, cpu_msr, (1 << MSR_EE));
6616         /* Stop translation to have a chance to raise an exception */
6617         gen_stop_exception(ctx);
6618     } else {
6619         tcg_gen_andi_tl(cpu_msr, cpu_msr, ~(1 << MSR_EE));
6620     }
6621 #endif
6622 }
6623
6624 /* PowerPC 440 specific instructions */
6625
6626 /* dlmzb */
6627 static void gen_dlmzb(DisasContext *ctx)
6628 {
6629     TCGv_i32 t0 = tcg_const_i32(Rc(ctx->opcode));
6630     gen_helper_dlmzb(cpu_gpr[rA(ctx->opcode)], cpu_env,
6631                      cpu_gpr[rS(ctx->opcode)], cpu_gpr[rB(ctx->opcode)], t0);
6632     tcg_temp_free_i32(t0);
6633 }
6634
6635 /* mbar replaces eieio on 440 */
6636 static void gen_mbar(DisasContext *ctx)
6637 {
6638     /* interpreted as no-op */
6639 }
6640
6641 /* msync replaces sync on 440 */
6642 static void gen_msync_4xx(DisasContext *ctx)
6643 {
6644     /* interpreted as no-op */
6645 }
6646
6647 /* icbt */
6648 static void gen_icbt_440(DisasContext *ctx)
6649 {
6650     /* interpreted as no-op */
6651     /* XXX: specification say this is treated as a load by the MMU
6652      *      but does not generate any exception
6653      */
6654 }
6655
6656 /* Embedded.Processor Control */
6657
6658 static void gen_msgclr(DisasContext *ctx)
6659 {
6660 #if defined(CONFIG_USER_ONLY)
6661     gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
6662 #else
6663     if (unlikely(ctx->mem_idx == 0)) {
6664         gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
6665         return;
6666     }
6667
6668     gen_helper_msgclr(cpu_env, cpu_gpr[rB(ctx->opcode)]);
6669 #endif
6670 }
6671
6672 static void gen_msgsnd(DisasContext *ctx)
6673 {
6674 #if defined(CONFIG_USER_ONLY)
6675     gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
6676 #else
6677     if (unlikely(ctx->mem_idx == 0)) {
6678         gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
6679         return;
6680     }
6681
6682     gen_helper_msgsnd(cpu_gpr[rB(ctx->opcode)]);
6683 #endif
6684 }
6685
6686 /***                      Altivec vector extension                         ***/
6687 /* Altivec registers moves */
6688
6689 static inline TCGv_ptr gen_avr_ptr(int reg)
6690 {
6691     TCGv_ptr r = tcg_temp_new_ptr();
6692     tcg_gen_addi_ptr(r, cpu_env, offsetof(CPUPPCState, avr[reg]));
6693     return r;
6694 }
6695
6696 #define GEN_VR_LDX(name, opc2, opc3)                                          \
6697 static void glue(gen_, name)(DisasContext *ctx)                                       \
6698 {                                                                             \
6699     TCGv EA;                                                                  \
6700     if (unlikely(!ctx->altivec_enabled)) {                                    \
6701         gen_exception(ctx, POWERPC_EXCP_VPU);                                 \
6702         return;                                                               \
6703     }                                                                         \
6704     gen_set_access_type(ctx, ACCESS_INT);                                     \
6705     EA = tcg_temp_new();                                                      \
6706     gen_addr_reg_index(ctx, EA);                                              \
6707     tcg_gen_andi_tl(EA, EA, ~0xf);                                            \
6708     if (ctx->le_mode) {                                                       \
6709         gen_qemu_ld64(ctx, cpu_avrl[rD(ctx->opcode)], EA);                    \
6710         tcg_gen_addi_tl(EA, EA, 8);                                           \
6711         gen_qemu_ld64(ctx, cpu_avrh[rD(ctx->opcode)], EA);                    \
6712     } else {                                                                  \
6713         gen_qemu_ld64(ctx, cpu_avrh[rD(ctx->opcode)], EA);                    \
6714         tcg_gen_addi_tl(EA, EA, 8);                                           \
6715         gen_qemu_ld64(ctx, cpu_avrl[rD(ctx->opcode)], EA);                    \
6716     }                                                                         \
6717     tcg_temp_free(EA);                                                        \
6718 }
6719
6720 #define GEN_VR_STX(name, opc2, opc3)                                          \
6721 static void gen_st##name(DisasContext *ctx)                                   \
6722 {                                                                             \
6723     TCGv EA;                                                                  \
6724     if (unlikely(!ctx->altivec_enabled)) {                                    \
6725         gen_exception(ctx, POWERPC_EXCP_VPU);                                 \
6726         return;                                                               \
6727     }                                                                         \
6728     gen_set_access_type(ctx, ACCESS_INT);                                     \
6729     EA = tcg_temp_new();                                                      \
6730     gen_addr_reg_index(ctx, EA);                                              \
6731     tcg_gen_andi_tl(EA, EA, ~0xf);                                            \
6732     if (ctx->le_mode) {                                                       \
6733         gen_qemu_st64(ctx, cpu_avrl[rD(ctx->opcode)], EA);                    \
6734         tcg_gen_addi_tl(EA, EA, 8);                                           \
6735         gen_qemu_st64(ctx, cpu_avrh[rD(ctx->opcode)], EA);                    \
6736     } else {                                                                  \
6737         gen_qemu_st64(ctx, cpu_avrh[rD(ctx->opcode)], EA);                    \
6738         tcg_gen_addi_tl(EA, EA, 8);                                           \
6739         gen_qemu_st64(ctx, cpu_avrl[rD(ctx->opcode)], EA);                    \
6740     }                                                                         \
6741     tcg_temp_free(EA);                                                        \
6742 }
6743
6744 #define GEN_VR_LVE(name, opc2, opc3)                                    \
6745 static void gen_lve##name(DisasContext *ctx)                            \
6746     {                                                                   \
6747         TCGv EA;                                                        \
6748         TCGv_ptr rs;                                                    \
6749         if (unlikely(!ctx->altivec_enabled)) {                          \
6750             gen_exception(ctx, POWERPC_EXCP_VPU);                       \
6751             return;                                                     \
6752         }                                                               \
6753         gen_set_access_type(ctx, ACCESS_INT);                           \
6754         EA = tcg_temp_new();                                            \
6755         gen_addr_reg_index(ctx, EA);                                    \
6756         rs = gen_avr_ptr(rS(ctx->opcode));                              \
6757         gen_helper_lve##name(cpu_env, rs, EA);                          \
6758         tcg_temp_free(EA);                                              \
6759         tcg_temp_free_ptr(rs);                                          \
6760     }
6761
6762 #define GEN_VR_STVE(name, opc2, opc3)                                   \
6763 static void gen_stve##name(DisasContext *ctx)                           \
6764     {                                                                   \
6765         TCGv EA;                                                        \
6766         TCGv_ptr rs;                                                    \
6767         if (unlikely(!ctx->altivec_enabled)) {                          \
6768             gen_exception(ctx, POWERPC_EXCP_VPU);                       \
6769             return;                                                     \
6770         }                                                               \
6771         gen_set_access_type(ctx, ACCESS_INT);                           \
6772         EA = tcg_temp_new();                                            \
6773         gen_addr_reg_index(ctx, EA);                                    \
6774         rs = gen_avr_ptr(rS(ctx->opcode));                              \
6775         gen_helper_stve##name(cpu_env, rs, EA);                         \
6776         tcg_temp_free(EA);                                              \
6777         tcg_temp_free_ptr(rs);                                          \
6778     }
6779
6780 GEN_VR_LDX(lvx, 0x07, 0x03);
6781 /* As we don't emulate the cache, lvxl is stricly equivalent to lvx */
6782 GEN_VR_LDX(lvxl, 0x07, 0x0B);
6783
6784 GEN_VR_LVE(bx, 0x07, 0x00);
6785 GEN_VR_LVE(hx, 0x07, 0x01);
6786 GEN_VR_LVE(wx, 0x07, 0x02);
6787
6788 GEN_VR_STX(svx, 0x07, 0x07);
6789 /* As we don't emulate the cache, stvxl is stricly equivalent to stvx */
6790 GEN_VR_STX(svxl, 0x07, 0x0F);
6791
6792 GEN_VR_STVE(bx, 0x07, 0x04);
6793 GEN_VR_STVE(hx, 0x07, 0x05);
6794 GEN_VR_STVE(wx, 0x07, 0x06);
6795
6796 static void gen_lvsl(DisasContext *ctx)
6797 {
6798     TCGv_ptr rd;
6799     TCGv EA;
6800     if (unlikely(!ctx->altivec_enabled)) {
6801         gen_exception(ctx, POWERPC_EXCP_VPU);
6802         return;
6803     }
6804     EA = tcg_temp_new();
6805     gen_addr_reg_index(ctx, EA);
6806     rd = gen_avr_ptr(rD(ctx->opcode));
6807     gen_helper_lvsl(rd, EA);
6808     tcg_temp_free(EA);
6809     tcg_temp_free_ptr(rd);
6810 }
6811
6812 static void gen_lvsr(DisasContext *ctx)
6813 {
6814     TCGv_ptr rd;
6815     TCGv EA;
6816     if (unlikely(!ctx->altivec_enabled)) {
6817         gen_exception(ctx, POWERPC_EXCP_VPU);
6818         return;
6819     }
6820     EA = tcg_temp_new();
6821     gen_addr_reg_index(ctx, EA);
6822     rd = gen_avr_ptr(rD(ctx->opcode));
6823     gen_helper_lvsr(rd, EA);
6824     tcg_temp_free(EA);
6825     tcg_temp_free_ptr(rd);
6826 }
6827
6828 static void gen_mfvscr(DisasContext *ctx)
6829 {
6830     TCGv_i32 t;
6831     if (unlikely(!ctx->altivec_enabled)) {
6832         gen_exception(ctx, POWERPC_EXCP_VPU);
6833         return;
6834     }
6835     tcg_gen_movi_i64(cpu_avrh[rD(ctx->opcode)], 0);
6836     t = tcg_temp_new_i32();
6837     tcg_gen_ld_i32(t, cpu_env, offsetof(CPUPPCState, vscr));
6838     tcg_gen_extu_i32_i64(cpu_avrl[rD(ctx->opcode)], t);
6839     tcg_temp_free_i32(t);
6840 }
6841
6842 static void gen_mtvscr(DisasContext *ctx)
6843 {
6844     TCGv_ptr p;
6845     if (unlikely(!ctx->altivec_enabled)) {
6846         gen_exception(ctx, POWERPC_EXCP_VPU);
6847         return;
6848     }
6849     p = gen_avr_ptr(rD(ctx->opcode));
6850     gen_helper_mtvscr(cpu_env, p);
6851     tcg_temp_free_ptr(p);
6852 }
6853
6854 /* Logical operations */
6855 #define GEN_VX_LOGICAL(name, tcg_op, opc2, opc3)                        \
6856 static void glue(gen_, name)(DisasContext *ctx)                                 \
6857 {                                                                       \
6858     if (unlikely(!ctx->altivec_enabled)) {                              \
6859         gen_exception(ctx, POWERPC_EXCP_VPU);                           \
6860         return;                                                         \
6861     }                                                                   \
6862     tcg_op(cpu_avrh[rD(ctx->opcode)], cpu_avrh[rA(ctx->opcode)], cpu_avrh[rB(ctx->opcode)]); \
6863     tcg_op(cpu_avrl[rD(ctx->opcode)], cpu_avrl[rA(ctx->opcode)], cpu_avrl[rB(ctx->opcode)]); \
6864 }
6865
6866 GEN_VX_LOGICAL(vand, tcg_gen_and_i64, 2, 16);
6867 GEN_VX_LOGICAL(vandc, tcg_gen_andc_i64, 2, 17);
6868 GEN_VX_LOGICAL(vor, tcg_gen_or_i64, 2, 18);
6869 GEN_VX_LOGICAL(vxor, tcg_gen_xor_i64, 2, 19);
6870 GEN_VX_LOGICAL(vnor, tcg_gen_nor_i64, 2, 20);
6871 GEN_VX_LOGICAL(veqv, tcg_gen_eqv_i64, 2, 26);
6872 GEN_VX_LOGICAL(vnand, tcg_gen_nand_i64, 2, 22);
6873 GEN_VX_LOGICAL(vorc, tcg_gen_orc_i64, 2, 21);
6874
6875 #define GEN_VXFORM(name, opc2, opc3)                                    \
6876 static void glue(gen_, name)(DisasContext *ctx)                                 \
6877 {                                                                       \
6878     TCGv_ptr ra, rb, rd;                                                \
6879     if (unlikely(!ctx->altivec_enabled)) {                              \
6880         gen_exception(ctx, POWERPC_EXCP_VPU);                           \
6881         return;                                                         \
6882     }                                                                   \
6883     ra = gen_avr_ptr(rA(ctx->opcode));                                  \
6884     rb = gen_avr_ptr(rB(ctx->opcode));                                  \
6885     rd = gen_avr_ptr(rD(ctx->opcode));                                  \
6886     gen_helper_##name (rd, ra, rb);                                     \
6887     tcg_temp_free_ptr(ra);                                              \
6888     tcg_temp_free_ptr(rb);                                              \
6889     tcg_temp_free_ptr(rd);                                              \
6890 }
6891
6892 #define GEN_VXFORM_ENV(name, opc2, opc3)                                \
6893 static void glue(gen_, name)(DisasContext *ctx)                         \
6894 {                                                                       \
6895     TCGv_ptr ra, rb, rd;                                                \
6896     if (unlikely(!ctx->altivec_enabled)) {                              \
6897         gen_exception(ctx, POWERPC_EXCP_VPU);                           \
6898         return;                                                         \
6899     }                                                                   \
6900     ra = gen_avr_ptr(rA(ctx->opcode));                                  \
6901     rb = gen_avr_ptr(rB(ctx->opcode));                                  \
6902     rd = gen_avr_ptr(rD(ctx->opcode));                                  \
6903     gen_helper_##name(cpu_env, rd, ra, rb);                             \
6904     tcg_temp_free_ptr(ra);                                              \
6905     tcg_temp_free_ptr(rb);                                              \
6906     tcg_temp_free_ptr(rd);                                              \
6907 }
6908
6909 #define GEN_VXFORM3(name, opc2, opc3)                                   \
6910 static void glue(gen_, name)(DisasContext *ctx)                         \
6911 {                                                                       \
6912     TCGv_ptr ra, rb, rc, rd;                                            \
6913     if (unlikely(!ctx->altivec_enabled)) {                              \
6914         gen_exception(ctx, POWERPC_EXCP_VPU);                           \
6915         return;                                                         \
6916     }                                                                   \
6917     ra = gen_avr_ptr(rA(ctx->opcode));                                  \
6918     rb = gen_avr_ptr(rB(ctx->opcode));                                  \
6919     rc = gen_avr_ptr(rC(ctx->opcode));                                  \
6920     rd = gen_avr_ptr(rD(ctx->opcode));                                  \
6921     gen_helper_##name(rd, ra, rb, rc);                                  \
6922     tcg_temp_free_ptr(ra);                                              \
6923     tcg_temp_free_ptr(rb);                                              \
6924     tcg_temp_free_ptr(rc);                                              \
6925     tcg_temp_free_ptr(rd);                                              \
6926 }
6927
6928 /*
6929  * Support for Altivec instruction pairs that use bit 31 (Rc) as
6930  * an opcode bit.  In general, these pairs come from different
6931  * versions of the ISA, so we must also support a pair of flags for
6932  * each instruction.
6933  */
6934 #define GEN_VXFORM_DUAL(name0, flg0, flg2_0, name1, flg1, flg2_1)          \
6935 static void glue(gen_, name0##_##name1)(DisasContext *ctx)             \
6936 {                                                                      \
6937     if ((Rc(ctx->opcode) == 0) &&                                      \
6938         ((ctx->insns_flags & flg0) || (ctx->insns_flags2 & flg2_0))) { \
6939         gen_##name0(ctx);                                              \
6940     } else if ((Rc(ctx->opcode) == 1) &&                               \
6941         ((ctx->insns_flags & flg1) || (ctx->insns_flags2 & flg2_1))) { \
6942         gen_##name1(ctx);                                              \
6943     } else {                                                           \
6944         gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL);            \
6945     }                                                                  \
6946 }
6947
6948 GEN_VXFORM(vaddubm, 0, 0);
6949 GEN_VXFORM(vadduhm, 0, 1);
6950 GEN_VXFORM(vadduwm, 0, 2);
6951 GEN_VXFORM(vaddudm, 0, 3);
6952 GEN_VXFORM(vsububm, 0, 16);
6953 GEN_VXFORM(vsubuhm, 0, 17);
6954 GEN_VXFORM(vsubuwm, 0, 18);
6955 GEN_VXFORM(vsubudm, 0, 19);
6956 GEN_VXFORM(vmaxub, 1, 0);
6957 GEN_VXFORM(vmaxuh, 1, 1);
6958 GEN_VXFORM(vmaxuw, 1, 2);
6959 GEN_VXFORM(vmaxud, 1, 3);
6960 GEN_VXFORM(vmaxsb, 1, 4);
6961 GEN_VXFORM(vmaxsh, 1, 5);
6962 GEN_VXFORM(vmaxsw, 1, 6);
6963 GEN_VXFORM(vmaxsd, 1, 7);
6964 GEN_VXFORM(vminub, 1, 8);
6965 GEN_VXFORM(vminuh, 1, 9);
6966 GEN_VXFORM(vminuw, 1, 10);
6967 GEN_VXFORM(vminud, 1, 11);
6968 GEN_VXFORM(vminsb, 1, 12);
6969 GEN_VXFORM(vminsh, 1, 13);
6970 GEN_VXFORM(vminsw, 1, 14);
6971 GEN_VXFORM(vminsd, 1, 15);
6972 GEN_VXFORM(vavgub, 1, 16);
6973 GEN_VXFORM(vavguh, 1, 17);
6974 GEN_VXFORM(vavguw, 1, 18);
6975 GEN_VXFORM(vavgsb, 1, 20);
6976 GEN_VXFORM(vavgsh, 1, 21);
6977 GEN_VXFORM(vavgsw, 1, 22);
6978 GEN_VXFORM(vmrghb, 6, 0);
6979 GEN_VXFORM(vmrghh, 6, 1);
6980 GEN_VXFORM(vmrghw, 6, 2);
6981 GEN_VXFORM(vmrglb, 6, 4);
6982 GEN_VXFORM(vmrglh, 6, 5);
6983 GEN_VXFORM(vmrglw, 6, 6);
6984
6985 static void gen_vmrgew(DisasContext *ctx)
6986 {
6987     TCGv_i64 tmp;
6988     int VT, VA, VB;
6989     if (unlikely(!ctx->altivec_enabled)) {
6990         gen_exception(ctx, POWERPC_EXCP_VPU);
6991         return;
6992     }
6993     VT = rD(ctx->opcode);
6994     VA = rA(ctx->opcode);
6995     VB = rB(ctx->opcode);
6996     tmp = tcg_temp_new_i64();
6997     tcg_gen_shri_i64(tmp, cpu_avrh[VB], 32);
6998     tcg_gen_deposit_i64(cpu_avrh[VT], cpu_avrh[VA], tmp, 0, 32);
6999     tcg_gen_shri_i64(tmp, cpu_avrl[VB], 32);
7000     tcg_gen_deposit_i64(cpu_avrl[VT], cpu_avrl[VA], tmp, 0, 32);
7001     tcg_temp_free_i64(tmp);
7002 }
7003
7004 static void gen_vmrgow(DisasContext *ctx)
7005 {
7006     int VT, VA, VB;
7007     if (unlikely(!ctx->altivec_enabled)) {
7008         gen_exception(ctx, POWERPC_EXCP_VPU);
7009         return;
7010     }
7011     VT = rD(ctx->opcode);
7012     VA = rA(ctx->opcode);
7013     VB = rB(ctx->opcode);
7014
7015     tcg_gen_deposit_i64(cpu_avrh[VT], cpu_avrh[VB], cpu_avrh[VA], 32, 32);
7016     tcg_gen_deposit_i64(cpu_avrl[VT], cpu_avrl[VB], cpu_avrl[VA], 32, 32);
7017 }
7018
7019 GEN_VXFORM(vmuloub, 4, 0);
7020 GEN_VXFORM(vmulouh, 4, 1);
7021 GEN_VXFORM(vmulouw, 4, 2);
7022 GEN_VXFORM(vmuluwm, 4, 2);
7023 GEN_VXFORM_DUAL(vmulouw, PPC_ALTIVEC, PPC_NONE,
7024                 vmuluwm, PPC_NONE, PPC2_ALTIVEC_207)
7025 GEN_VXFORM(vmulosb, 4, 4);
7026 GEN_VXFORM(vmulosh, 4, 5);
7027 GEN_VXFORM(vmulosw, 4, 6);
7028 GEN_VXFORM(vmuleub, 4, 8);
7029 GEN_VXFORM(vmuleuh, 4, 9);
7030 GEN_VXFORM(vmuleuw, 4, 10);
7031 GEN_VXFORM(vmulesb, 4, 12);
7032 GEN_VXFORM(vmulesh, 4, 13);
7033 GEN_VXFORM(vmulesw, 4, 14);
7034 GEN_VXFORM(vslb, 2, 4);
7035 GEN_VXFORM(vslh, 2, 5);
7036 GEN_VXFORM(vslw, 2, 6);
7037 GEN_VXFORM(vsld, 2, 23);
7038 GEN_VXFORM(vsrb, 2, 8);
7039 GEN_VXFORM(vsrh, 2, 9);
7040 GEN_VXFORM(vsrw, 2, 10);
7041 GEN_VXFORM(vsrd, 2, 27);
7042 GEN_VXFORM(vsrab, 2, 12);
7043 GEN_VXFORM(vsrah, 2, 13);
7044 GEN_VXFORM(vsraw, 2, 14);
7045 GEN_VXFORM(vsrad, 2, 15);
7046 GEN_VXFORM(vslo, 6, 16);
7047 GEN_VXFORM(vsro, 6, 17);
7048 GEN_VXFORM(vaddcuw, 0, 6);
7049 GEN_VXFORM(vsubcuw, 0, 22);
7050 GEN_VXFORM_ENV(vaddubs, 0, 8);
7051 GEN_VXFORM_ENV(vadduhs, 0, 9);
7052 GEN_VXFORM_ENV(vadduws, 0, 10);
7053 GEN_VXFORM_ENV(vaddsbs, 0, 12);
7054 GEN_VXFORM_ENV(vaddshs, 0, 13);
7055 GEN_VXFORM_ENV(vaddsws, 0, 14);
7056 GEN_VXFORM_ENV(vsububs, 0, 24);
7057 GEN_VXFORM_ENV(vsubuhs, 0, 25);
7058 GEN_VXFORM_ENV(vsubuws, 0, 26);
7059 GEN_VXFORM_ENV(vsubsbs, 0, 28);
7060 GEN_VXFORM_ENV(vsubshs, 0, 29);
7061 GEN_VXFORM_ENV(vsubsws, 0, 30);
7062 GEN_VXFORM(vadduqm, 0, 4);
7063 GEN_VXFORM(vaddcuq, 0, 5);
7064 GEN_VXFORM3(vaddeuqm, 30, 0);
7065 GEN_VXFORM3(vaddecuq, 30, 0);
7066 GEN_VXFORM_DUAL(vaddeuqm, PPC_NONE, PPC2_ALTIVEC_207, \
7067             vaddecuq, PPC_NONE, PPC2_ALTIVEC_207)
7068 GEN_VXFORM(vsubuqm, 0, 20);
7069 GEN_VXFORM(vsubcuq, 0, 21);
7070 GEN_VXFORM3(vsubeuqm, 31, 0);
7071 GEN_VXFORM3(vsubecuq, 31, 0);
7072 GEN_VXFORM_DUAL(vsubeuqm, PPC_NONE, PPC2_ALTIVEC_207, \
7073             vsubecuq, PPC_NONE, PPC2_ALTIVEC_207)
7074 GEN_VXFORM(vrlb, 2, 0);
7075 GEN_VXFORM(vrlh, 2, 1);
7076 GEN_VXFORM(vrlw, 2, 2);
7077 GEN_VXFORM(vrld, 2, 3);
7078 GEN_VXFORM(vsl, 2, 7);
7079 GEN_VXFORM(vsr, 2, 11);
7080 GEN_VXFORM_ENV(vpkuhum, 7, 0);
7081 GEN_VXFORM_ENV(vpkuwum, 7, 1);
7082 GEN_VXFORM_ENV(vpkudum, 7, 17);
7083 GEN_VXFORM_ENV(vpkuhus, 7, 2);
7084 GEN_VXFORM_ENV(vpkuwus, 7, 3);
7085 GEN_VXFORM_ENV(vpkudus, 7, 19);
7086 GEN_VXFORM_ENV(vpkshus, 7, 4);
7087 GEN_VXFORM_ENV(vpkswus, 7, 5);
7088 GEN_VXFORM_ENV(vpksdus, 7, 21);
7089 GEN_VXFORM_ENV(vpkshss, 7, 6);
7090 GEN_VXFORM_ENV(vpkswss, 7, 7);
7091 GEN_VXFORM_ENV(vpksdss, 7, 23);
7092 GEN_VXFORM(vpkpx, 7, 12);
7093 GEN_VXFORM_ENV(vsum4ubs, 4, 24);
7094 GEN_VXFORM_ENV(vsum4sbs, 4, 28);
7095 GEN_VXFORM_ENV(vsum4shs, 4, 25);
7096 GEN_VXFORM_ENV(vsum2sws, 4, 26);
7097 GEN_VXFORM_ENV(vsumsws, 4, 30);
7098 GEN_VXFORM_ENV(vaddfp, 5, 0);
7099 GEN_VXFORM_ENV(vsubfp, 5, 1);
7100 GEN_VXFORM_ENV(vmaxfp, 5, 16);
7101 GEN_VXFORM_ENV(vminfp, 5, 17);
7102
7103 #define GEN_VXRFORM1(opname, name, str, opc2, opc3)                     \
7104 static void glue(gen_, name)(DisasContext *ctx)                         \
7105     {                                                                   \
7106         TCGv_ptr ra, rb, rd;                                            \
7107         if (unlikely(!ctx->altivec_enabled)) {                          \
7108             gen_exception(ctx, POWERPC_EXCP_VPU);                       \
7109             return;                                                     \
7110         }                                                               \
7111         ra = gen_avr_ptr(rA(ctx->opcode));                              \
7112         rb = gen_avr_ptr(rB(ctx->opcode));                              \
7113         rd = gen_avr_ptr(rD(ctx->opcode));                              \
7114         gen_helper_##opname(cpu_env, rd, ra, rb);                       \
7115         tcg_temp_free_ptr(ra);                                          \
7116         tcg_temp_free_ptr(rb);                                          \
7117         tcg_temp_free_ptr(rd);                                          \
7118     }
7119
7120 #define GEN_VXRFORM(name, opc2, opc3)                                \
7121     GEN_VXRFORM1(name, name, #name, opc2, opc3)                      \
7122     GEN_VXRFORM1(name##_dot, name##_, #name ".", opc2, (opc3 | (0x1 << 4)))
7123
7124 /*
7125  * Support for Altivec instructions that use bit 31 (Rc) as an opcode
7126  * bit but also use bit 21 as an actual Rc bit.  In general, thse pairs
7127  * come from different versions of the ISA, so we must also support a
7128  * pair of flags for each instruction.
7129  */
7130 #define GEN_VXRFORM_DUAL(name0, flg0, flg2_0, name1, flg1, flg2_1)     \
7131 static void glue(gen_, name0##_##name1)(DisasContext *ctx)             \
7132 {                                                                      \
7133     if ((Rc(ctx->opcode) == 0) &&                                      \
7134         ((ctx->insns_flags & flg0) || (ctx->insns_flags2 & flg2_0))) { \
7135         if (Rc21(ctx->opcode) == 0) {                                  \
7136             gen_##name0(ctx);                                          \
7137         } else {                                                       \
7138             gen_##name0##_(ctx);                                       \
7139         }                                                              \
7140     } else if ((Rc(ctx->opcode) == 1) &&                               \
7141         ((ctx->insns_flags & flg1) || (ctx->insns_flags2 & flg2_1))) { \
7142         if (Rc21(ctx->opcode) == 0) {                                  \
7143             gen_##name1(ctx);                                          \
7144         } else {                                                       \
7145             gen_##name1##_(ctx);                                       \
7146         }                                                              \
7147     } else {                                                           \
7148         gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL);            \
7149     }                                                                  \
7150 }
7151
7152 GEN_VXRFORM(vcmpequb, 3, 0)
7153 GEN_VXRFORM(vcmpequh, 3, 1)
7154 GEN_VXRFORM(vcmpequw, 3, 2)
7155 GEN_VXRFORM(vcmpequd, 3, 3)
7156 GEN_VXRFORM(vcmpgtsb, 3, 12)
7157 GEN_VXRFORM(vcmpgtsh, 3, 13)
7158 GEN_VXRFORM(vcmpgtsw, 3, 14)
7159 GEN_VXRFORM(vcmpgtsd, 3, 15)
7160 GEN_VXRFORM(vcmpgtub, 3, 8)
7161 GEN_VXRFORM(vcmpgtuh, 3, 9)
7162 GEN_VXRFORM(vcmpgtuw, 3, 10)
7163 GEN_VXRFORM(vcmpgtud, 3, 11)
7164 GEN_VXRFORM(vcmpeqfp, 3, 3)
7165 GEN_VXRFORM(vcmpgefp, 3, 7)
7166 GEN_VXRFORM(vcmpgtfp, 3, 11)
7167 GEN_VXRFORM(vcmpbfp, 3, 15)
7168
7169 GEN_VXRFORM_DUAL(vcmpeqfp, PPC_ALTIVEC, PPC_NONE, \
7170                  vcmpequd, PPC_NONE, PPC2_ALTIVEC_207)
7171 GEN_VXRFORM_DUAL(vcmpbfp, PPC_ALTIVEC, PPC_NONE, \
7172                  vcmpgtsd, PPC_NONE, PPC2_ALTIVEC_207)
7173 GEN_VXRFORM_DUAL(vcmpgtfp, PPC_ALTIVEC, PPC_NONE, \
7174                  vcmpgtud, PPC_NONE, PPC2_ALTIVEC_207)
7175
7176 #define GEN_VXFORM_SIMM(name, opc2, opc3)                               \
7177 static void glue(gen_, name)(DisasContext *ctx)                         \
7178     {                                                                   \
7179         TCGv_ptr rd;                                                    \
7180         TCGv_i32 simm;                                                  \
7181         if (unlikely(!ctx->altivec_enabled)) {                          \
7182             gen_exception(ctx, POWERPC_EXCP_VPU);                       \
7183             return;                                                     \
7184         }                                                               \
7185         simm = tcg_const_i32(SIMM5(ctx->opcode));                       \
7186         rd = gen_avr_ptr(rD(ctx->opcode));                              \
7187         gen_helper_##name (rd, simm);                                   \
7188         tcg_temp_free_i32(simm);                                        \
7189         tcg_temp_free_ptr(rd);                                          \
7190     }
7191
7192 GEN_VXFORM_SIMM(vspltisb, 6, 12);
7193 GEN_VXFORM_SIMM(vspltish, 6, 13);
7194 GEN_VXFORM_SIMM(vspltisw, 6, 14);
7195
7196 #define GEN_VXFORM_NOA(name, opc2, opc3)                                \
7197 static void glue(gen_, name)(DisasContext *ctx)                                 \
7198     {                                                                   \
7199         TCGv_ptr rb, rd;                                                \
7200         if (unlikely(!ctx->altivec_enabled)) {                          \
7201             gen_exception(ctx, POWERPC_EXCP_VPU);                       \
7202             return;                                                     \
7203         }                                                               \
7204         rb = gen_avr_ptr(rB(ctx->opcode));                              \
7205         rd = gen_avr_ptr(rD(ctx->opcode));                              \
7206         gen_helper_##name (rd, rb);                                     \
7207         tcg_temp_free_ptr(rb);                                          \
7208         tcg_temp_free_ptr(rd);                                         \
7209     }
7210
7211 #define GEN_VXFORM_NOA_ENV(name, opc2, opc3)                            \
7212 static void glue(gen_, name)(DisasContext *ctx)                         \
7213     {                                                                   \
7214         TCGv_ptr rb, rd;                                                \
7215                                                                         \
7216         if (unlikely(!ctx->altivec_enabled)) {                          \
7217             gen_exception(ctx, POWERPC_EXCP_VPU);                       \
7218             return;                                                     \
7219         }                                                               \
7220         rb = gen_avr_ptr(rB(ctx->opcode));                              \
7221         rd = gen_avr_ptr(rD(ctx->opcode));                              \
7222         gen_helper_##name(cpu_env, rd, rb);                             \
7223         tcg_temp_free_ptr(rb);                                          \
7224         tcg_temp_free_ptr(rd);                                          \
7225     }
7226
7227 GEN_VXFORM_NOA(vupkhsb, 7, 8);
7228 GEN_VXFORM_NOA(vupkhsh, 7, 9);
7229 GEN_VXFORM_NOA(vupkhsw, 7, 25);
7230 GEN_VXFORM_NOA(vupklsb, 7, 10);
7231 GEN_VXFORM_NOA(vupklsh, 7, 11);
7232 GEN_VXFORM_NOA(vupklsw, 7, 27);
7233 GEN_VXFORM_NOA(vupkhpx, 7, 13);
7234 GEN_VXFORM_NOA(vupklpx, 7, 15);
7235 GEN_VXFORM_NOA_ENV(vrefp, 5, 4);
7236 GEN_VXFORM_NOA_ENV(vrsqrtefp, 5, 5);
7237 GEN_VXFORM_NOA_ENV(vexptefp, 5, 6);
7238 GEN_VXFORM_NOA_ENV(vlogefp, 5, 7);
7239 GEN_VXFORM_NOA_ENV(vrfim, 5, 8);
7240 GEN_VXFORM_NOA_ENV(vrfin, 5, 9);
7241 GEN_VXFORM_NOA_ENV(vrfip, 5, 10);
7242 GEN_VXFORM_NOA_ENV(vrfiz, 5, 11);
7243
7244 #define GEN_VXFORM_SIMM(name, opc2, opc3)                               \
7245 static void glue(gen_, name)(DisasContext *ctx)                                 \
7246     {                                                                   \
7247         TCGv_ptr rd;                                                    \
7248         TCGv_i32 simm;                                                  \
7249         if (unlikely(!ctx->altivec_enabled)) {                          \
7250             gen_exception(ctx, POWERPC_EXCP_VPU);                       \
7251             return;                                                     \
7252         }                                                               \
7253         simm = tcg_const_i32(SIMM5(ctx->opcode));                       \
7254         rd = gen_avr_ptr(rD(ctx->opcode));                              \
7255         gen_helper_##name (rd, simm);                                   \
7256         tcg_temp_free_i32(simm);                                        \
7257         tcg_temp_free_ptr(rd);                                          \
7258     }
7259
7260 #define GEN_VXFORM_UIMM(name, opc2, opc3)                               \
7261 static void glue(gen_, name)(DisasContext *ctx)                                 \
7262     {                                                                   \
7263         TCGv_ptr rb, rd;                                                \
7264         TCGv_i32 uimm;                                                  \
7265         if (unlikely(!ctx->altivec_enabled)) {                          \
7266             gen_exception(ctx, POWERPC_EXCP_VPU);                       \
7267             return;                                                     \
7268         }                                                               \
7269         uimm = tcg_const_i32(UIMM5(ctx->opcode));                       \
7270         rb = gen_avr_ptr(rB(ctx->opcode));                              \
7271         rd = gen_avr_ptr(rD(ctx->opcode));                              \
7272         gen_helper_##name (rd, rb, uimm);                               \
7273         tcg_temp_free_i32(uimm);                                        \
7274         tcg_temp_free_ptr(rb);                                          \
7275         tcg_temp_free_ptr(rd);                                          \
7276     }
7277
7278 #define GEN_VXFORM_UIMM_ENV(name, opc2, opc3)                           \
7279 static void glue(gen_, name)(DisasContext *ctx)                         \
7280     {                                                                   \
7281         TCGv_ptr rb, rd;                                                \
7282         TCGv_i32 uimm;                                                  \
7283                                                                         \
7284         if (unlikely(!ctx->altivec_enabled)) {                          \
7285             gen_exception(ctx, POWERPC_EXCP_VPU);                       \
7286             return;                                                     \
7287         }                                                               \
7288         uimm = tcg_const_i32(UIMM5(ctx->opcode));                       \
7289         rb = gen_avr_ptr(rB(ctx->opcode));                              \
7290         rd = gen_avr_ptr(rD(ctx->opcode));                              \
7291         gen_helper_##name(cpu_env, rd, rb, uimm);                       \
7292         tcg_temp_free_i32(uimm);                                        \
7293         tcg_temp_free_ptr(rb);                                          \
7294         tcg_temp_free_ptr(rd);                                          \
7295     }
7296
7297 GEN_VXFORM_UIMM(vspltb, 6, 8);
7298 GEN_VXFORM_UIMM(vsplth, 6, 9);
7299 GEN_VXFORM_UIMM(vspltw, 6, 10);
7300 GEN_VXFORM_UIMM_ENV(vcfux, 5, 12);
7301 GEN_VXFORM_UIMM_ENV(vcfsx, 5, 13);
7302 GEN_VXFORM_UIMM_ENV(vctuxs, 5, 14);
7303 GEN_VXFORM_UIMM_ENV(vctsxs, 5, 15);
7304
7305 static void gen_vsldoi(DisasContext *ctx)
7306 {
7307     TCGv_ptr ra, rb, rd;
7308     TCGv_i32 sh;
7309     if (unlikely(!ctx->altivec_enabled)) {
7310         gen_exception(ctx, POWERPC_EXCP_VPU);
7311         return;
7312     }
7313     ra = gen_avr_ptr(rA(ctx->opcode));
7314     rb = gen_avr_ptr(rB(ctx->opcode));
7315     rd = gen_avr_ptr(rD(ctx->opcode));
7316     sh = tcg_const_i32(VSH(ctx->opcode));
7317     gen_helper_vsldoi (rd, ra, rb, sh);
7318     tcg_temp_free_ptr(ra);
7319     tcg_temp_free_ptr(rb);
7320     tcg_temp_free_ptr(rd);
7321     tcg_temp_free_i32(sh);
7322 }
7323
7324 #define GEN_VAFORM_PAIRED(name0, name1, opc2)                           \
7325 static void glue(gen_, name0##_##name1)(DisasContext *ctx)              \
7326     {                                                                   \
7327         TCGv_ptr ra, rb, rc, rd;                                        \
7328         if (unlikely(!ctx->altivec_enabled)) {                          \
7329             gen_exception(ctx, POWERPC_EXCP_VPU);                       \
7330             return;                                                     \
7331         }                                                               \
7332         ra = gen_avr_ptr(rA(ctx->opcode));                              \
7333         rb = gen_avr_ptr(rB(ctx->opcode));                              \
7334         rc = gen_avr_ptr(rC(ctx->opcode));                              \
7335         rd = gen_avr_ptr(rD(ctx->opcode));                              \
7336         if (Rc(ctx->opcode)) {                                          \
7337             gen_helper_##name1(cpu_env, rd, ra, rb, rc);                \
7338         } else {                                                        \
7339             gen_helper_##name0(cpu_env, rd, ra, rb, rc);                \
7340         }                                                               \
7341         tcg_temp_free_ptr(ra);                                          \
7342         tcg_temp_free_ptr(rb);                                          \
7343         tcg_temp_free_ptr(rc);                                          \
7344         tcg_temp_free_ptr(rd);                                          \
7345     }
7346
7347 GEN_VAFORM_PAIRED(vmhaddshs, vmhraddshs, 16)
7348
7349 static void gen_vmladduhm(DisasContext *ctx)
7350 {
7351     TCGv_ptr ra, rb, rc, rd;
7352     if (unlikely(!ctx->altivec_enabled)) {
7353         gen_exception(ctx, POWERPC_EXCP_VPU);
7354         return;
7355     }
7356     ra = gen_avr_ptr(rA(ctx->opcode));
7357     rb = gen_avr_ptr(rB(ctx->opcode));
7358     rc = gen_avr_ptr(rC(ctx->opcode));
7359     rd = gen_avr_ptr(rD(ctx->opcode));
7360     gen_helper_vmladduhm(rd, ra, rb, rc);
7361     tcg_temp_free_ptr(ra);
7362     tcg_temp_free_ptr(rb);
7363     tcg_temp_free_ptr(rc);
7364     tcg_temp_free_ptr(rd);
7365 }
7366
7367 GEN_VAFORM_PAIRED(vmsumubm, vmsummbm, 18)
7368 GEN_VAFORM_PAIRED(vmsumuhm, vmsumuhs, 19)
7369 GEN_VAFORM_PAIRED(vmsumshm, vmsumshs, 20)
7370 GEN_VAFORM_PAIRED(vsel, vperm, 21)
7371 GEN_VAFORM_PAIRED(vmaddfp, vnmsubfp, 23)
7372
7373 GEN_VXFORM_NOA(vclzb, 1, 28)
7374 GEN_VXFORM_NOA(vclzh, 1, 29)
7375 GEN_VXFORM_NOA(vclzw, 1, 30)
7376 GEN_VXFORM_NOA(vclzd, 1, 31)
7377 GEN_VXFORM_NOA(vpopcntb, 1, 28)
7378 GEN_VXFORM_NOA(vpopcnth, 1, 29)
7379 GEN_VXFORM_NOA(vpopcntw, 1, 30)
7380 GEN_VXFORM_NOA(vpopcntd, 1, 31)
7381 GEN_VXFORM_DUAL(vclzb, PPC_NONE, PPC2_ALTIVEC_207, \
7382                 vpopcntb, PPC_NONE, PPC2_ALTIVEC_207)
7383 GEN_VXFORM_DUAL(vclzh, PPC_NONE, PPC2_ALTIVEC_207, \
7384                 vpopcnth, PPC_NONE, PPC2_ALTIVEC_207)
7385 GEN_VXFORM_DUAL(vclzw, PPC_NONE, PPC2_ALTIVEC_207, \
7386                 vpopcntw, PPC_NONE, PPC2_ALTIVEC_207)
7387 GEN_VXFORM_DUAL(vclzd, PPC_NONE, PPC2_ALTIVEC_207, \
7388                 vpopcntd, PPC_NONE, PPC2_ALTIVEC_207)
7389 GEN_VXFORM(vbpermq, 6, 21);
7390 GEN_VXFORM_NOA(vgbbd, 6, 20);
7391 GEN_VXFORM(vpmsumb, 4, 16)
7392 GEN_VXFORM(vpmsumh, 4, 17)
7393 GEN_VXFORM(vpmsumw, 4, 18)
7394 GEN_VXFORM(vpmsumd, 4, 19)
7395
7396 #define GEN_BCD(op)                                 \
7397 static void gen_##op(DisasContext *ctx)             \
7398 {                                                   \
7399     TCGv_ptr ra, rb, rd;                            \
7400     TCGv_i32 ps;                                    \
7401                                                     \
7402     if (unlikely(!ctx->altivec_enabled)) {          \
7403         gen_exception(ctx, POWERPC_EXCP_VPU);       \
7404         return;                                     \
7405     }                                               \
7406                                                     \
7407     ra = gen_avr_ptr(rA(ctx->opcode));              \
7408     rb = gen_avr_ptr(rB(ctx->opcode));              \
7409     rd = gen_avr_ptr(rD(ctx->opcode));              \
7410                                                     \
7411     ps = tcg_const_i32((ctx->opcode & 0x200) != 0); \
7412                                                     \
7413     gen_helper_##op(cpu_crf[6], rd, ra, rb, ps);    \
7414                                                     \
7415     tcg_temp_free_ptr(ra);                          \
7416     tcg_temp_free_ptr(rb);                          \
7417     tcg_temp_free_ptr(rd);                          \
7418     tcg_temp_free_i32(ps);                          \
7419 }
7420
7421 GEN_BCD(bcdadd)
7422 GEN_BCD(bcdsub)
7423
7424 GEN_VXFORM_DUAL(vsububm, PPC_ALTIVEC, PPC_NONE, \
7425                 bcdadd, PPC_NONE, PPC2_ALTIVEC_207)
7426 GEN_VXFORM_DUAL(vsububs, PPC_ALTIVEC, PPC_NONE, \
7427                 bcdadd, PPC_NONE, PPC2_ALTIVEC_207)
7428 GEN_VXFORM_DUAL(vsubuhm, PPC_ALTIVEC, PPC_NONE, \
7429                 bcdsub, PPC_NONE, PPC2_ALTIVEC_207)
7430 GEN_VXFORM_DUAL(vsubuhs, PPC_ALTIVEC, PPC_NONE, \
7431                 bcdsub, PPC_NONE, PPC2_ALTIVEC_207)
7432
7433 static void gen_vsbox(DisasContext *ctx)
7434 {
7435     TCGv_ptr ra, rd;
7436     if (unlikely(!ctx->altivec_enabled)) {
7437         gen_exception(ctx, POWERPC_EXCP_VPU);
7438         return;
7439     }
7440     ra = gen_avr_ptr(rA(ctx->opcode));
7441     rd = gen_avr_ptr(rD(ctx->opcode));
7442     gen_helper_vsbox(rd, ra);
7443     tcg_temp_free_ptr(ra);
7444     tcg_temp_free_ptr(rd);
7445 }
7446
7447 GEN_VXFORM(vcipher, 4, 20)
7448 GEN_VXFORM(vcipherlast, 4, 20)
7449 GEN_VXFORM(vncipher, 4, 21)
7450 GEN_VXFORM(vncipherlast, 4, 21)
7451
7452 GEN_VXFORM_DUAL(vcipher, PPC_NONE, PPC2_ALTIVEC_207,
7453                 vcipherlast, PPC_NONE, PPC2_ALTIVEC_207)
7454 GEN_VXFORM_DUAL(vncipher, PPC_NONE, PPC2_ALTIVEC_207,
7455                 vncipherlast, PPC_NONE, PPC2_ALTIVEC_207)
7456
7457 #define VSHASIGMA(op)                         \
7458 static void gen_##op(DisasContext *ctx)       \
7459 {                                             \
7460     TCGv_ptr ra, rd;                          \
7461     TCGv_i32 st_six;                          \
7462     if (unlikely(!ctx->altivec_enabled)) {    \
7463         gen_exception(ctx, POWERPC_EXCP_VPU); \
7464         return;                               \
7465     }                                         \
7466     ra = gen_avr_ptr(rA(ctx->opcode));        \
7467     rd = gen_avr_ptr(rD(ctx->opcode));        \
7468     st_six = tcg_const_i32(rB(ctx->opcode));  \
7469     gen_helper_##op(rd, ra, st_six);          \
7470     tcg_temp_free_ptr(ra);                    \
7471     tcg_temp_free_ptr(rd);                    \
7472     tcg_temp_free_i32(st_six);                \
7473 }
7474
7475 VSHASIGMA(vshasigmaw)
7476 VSHASIGMA(vshasigmad)
7477
7478 GEN_VXFORM3(vpermxor, 22, 0xFF)
7479 GEN_VXFORM_DUAL(vsldoi, PPC_ALTIVEC, PPC_NONE,
7480                 vpermxor, PPC_NONE, PPC2_ALTIVEC_207)
7481
7482 /***                           VSX extension                               ***/
7483
7484 static inline TCGv_i64 cpu_vsrh(int n)
7485 {
7486     if (n < 32) {
7487         return cpu_fpr[n];
7488     } else {
7489         return cpu_avrh[n-32];
7490     }
7491 }
7492
7493 static inline TCGv_i64 cpu_vsrl(int n)
7494 {
7495     if (n < 32) {
7496         return cpu_vsr[n];
7497     } else {
7498         return cpu_avrl[n-32];
7499     }
7500 }
7501
7502 #define VSX_LOAD_SCALAR(name, operation)                      \
7503 static void gen_##name(DisasContext *ctx)                     \
7504 {                                                             \
7505     TCGv EA;                                                  \
7506     if (unlikely(!ctx->vsx_enabled)) {                        \
7507         gen_exception(ctx, POWERPC_EXCP_VSXU);                \
7508         return;                                               \
7509     }                                                         \
7510     gen_set_access_type(ctx, ACCESS_INT);                     \
7511     EA = tcg_temp_new();                                      \
7512     gen_addr_reg_index(ctx, EA);                              \
7513     gen_qemu_##operation(ctx, cpu_vsrh(xT(ctx->opcode)), EA); \
7514     /* NOTE: cpu_vsrl is undefined */                         \
7515     tcg_temp_free(EA);                                        \
7516 }
7517
7518 VSX_LOAD_SCALAR(lxsdx, ld64)
7519 VSX_LOAD_SCALAR(lxsiwax, ld32s_i64)
7520 VSX_LOAD_SCALAR(lxsiwzx, ld32u_i64)
7521 VSX_LOAD_SCALAR(lxsspx, ld32fs)
7522
7523 static void gen_lxvd2x(DisasContext *ctx)
7524 {
7525     TCGv EA;
7526     if (unlikely(!ctx->vsx_enabled)) {
7527         gen_exception(ctx, POWERPC_EXCP_VSXU);
7528         return;
7529     }
7530     gen_set_access_type(ctx, ACCESS_INT);
7531     EA = tcg_temp_new();
7532     gen_addr_reg_index(ctx, EA);
7533     gen_qemu_ld64(ctx, cpu_vsrh(xT(ctx->opcode)), EA);
7534     tcg_gen_addi_tl(EA, EA, 8);
7535     gen_qemu_ld64(ctx, cpu_vsrl(xT(ctx->opcode)), EA);
7536     tcg_temp_free(EA);
7537 }
7538
7539 static void gen_lxvdsx(DisasContext *ctx)
7540 {
7541     TCGv EA;
7542     if (unlikely(!ctx->vsx_enabled)) {
7543         gen_exception(ctx, POWERPC_EXCP_VSXU);
7544         return;
7545     }
7546     gen_set_access_type(ctx, ACCESS_INT);
7547     EA = tcg_temp_new();
7548     gen_addr_reg_index(ctx, EA);
7549     gen_qemu_ld64(ctx, cpu_vsrh(xT(ctx->opcode)), EA);
7550     tcg_gen_mov_i64(cpu_vsrl(xT(ctx->opcode)), cpu_vsrh(xT(ctx->opcode)));
7551     tcg_temp_free(EA);
7552 }
7553
7554 static void gen_lxvw4x(DisasContext *ctx)
7555 {
7556     TCGv EA;
7557     TCGv_i64 tmp;
7558     TCGv_i64 xth = cpu_vsrh(xT(ctx->opcode));
7559     TCGv_i64 xtl = cpu_vsrl(xT(ctx->opcode));
7560     if (unlikely(!ctx->vsx_enabled)) {
7561         gen_exception(ctx, POWERPC_EXCP_VSXU);
7562         return;
7563     }
7564     gen_set_access_type(ctx, ACCESS_INT);
7565     EA = tcg_temp_new();
7566     tmp = tcg_temp_new_i64();
7567
7568     gen_addr_reg_index(ctx, EA);
7569     gen_qemu_ld32u_i64(ctx, tmp, EA);
7570     tcg_gen_addi_tl(EA, EA, 4);
7571     gen_qemu_ld32u_i64(ctx, xth, EA);
7572     tcg_gen_deposit_i64(xth, xth, tmp, 32, 32);
7573
7574     tcg_gen_addi_tl(EA, EA, 4);
7575     gen_qemu_ld32u_i64(ctx, tmp, EA);
7576     tcg_gen_addi_tl(EA, EA, 4);
7577     gen_qemu_ld32u_i64(ctx, xtl, EA);
7578     tcg_gen_deposit_i64(xtl, xtl, tmp, 32, 32);
7579
7580     tcg_temp_free(EA);
7581     tcg_temp_free_i64(tmp);
7582 }
7583
7584 #define VSX_STORE_SCALAR(name, operation)                     \
7585 static void gen_##name(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_##operation(ctx, cpu_vsrh(xS(ctx->opcode)), EA); \
7596     tcg_temp_free(EA);                                        \
7597 }
7598
7599 VSX_STORE_SCALAR(stxsdx, st64)
7600 VSX_STORE_SCALAR(stxsiwx, st32_i64)
7601 VSX_STORE_SCALAR(stxsspx, st32fs)
7602
7603 static void gen_stxvd2x(DisasContext *ctx)
7604 {
7605     TCGv EA;
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     gen_addr_reg_index(ctx, EA);
7613     gen_qemu_st64(ctx, cpu_vsrh(xS(ctx->opcode)), EA);
7614     tcg_gen_addi_tl(EA, EA, 8);
7615     gen_qemu_st64(ctx, cpu_vsrl(xS(ctx->opcode)), EA);
7616     tcg_temp_free(EA);
7617 }
7618
7619 static void gen_stxvw4x(DisasContext *ctx)
7620 {
7621     TCGv_i64 tmp;
7622     TCGv EA;
7623     if (unlikely(!ctx->vsx_enabled)) {
7624         gen_exception(ctx, POWERPC_EXCP_VSXU);
7625         return;
7626     }
7627     gen_set_access_type(ctx, ACCESS_INT);
7628     EA = tcg_temp_new();
7629     gen_addr_reg_index(ctx, EA);
7630     tmp = tcg_temp_new_i64();
7631
7632     tcg_gen_shri_i64(tmp, cpu_vsrh(xS(ctx->opcode)), 32);
7633     gen_qemu_st32_i64(ctx, tmp, EA);
7634     tcg_gen_addi_tl(EA, EA, 4);
7635     gen_qemu_st32_i64(ctx, cpu_vsrh(xS(ctx->opcode)), EA);
7636
7637     tcg_gen_shri_i64(tmp, cpu_vsrl(xS(ctx->opcode)), 32);
7638     tcg_gen_addi_tl(EA, EA, 4);
7639     gen_qemu_st32_i64(ctx, tmp, EA);
7640     tcg_gen_addi_tl(EA, EA, 4);
7641     gen_qemu_st32_i64(ctx, cpu_vsrl(xS(ctx->opcode)), EA);
7642
7643     tcg_temp_free(EA);
7644     tcg_temp_free_i64(tmp);
7645 }
7646
7647 #define MV_VSRW(name, tcgop1, tcgop2, target, source)           \
7648 static void gen_##name(DisasContext *ctx)                       \
7649 {                                                               \
7650     if (xS(ctx->opcode) < 32) {                                 \
7651         if (unlikely(!ctx->fpu_enabled)) {                      \
7652             gen_exception(ctx, POWERPC_EXCP_FPU);               \
7653             return;                                             \
7654         }                                                       \
7655     } else {                                                    \
7656         if (unlikely(!ctx->altivec_enabled)) {                  \
7657             gen_exception(ctx, POWERPC_EXCP_VPU);               \
7658             return;                                             \
7659         }                                                       \
7660     }                                                           \
7661     TCGv_i64 tmp = tcg_temp_new_i64();                          \
7662     tcg_gen_##tcgop1(tmp, source);                              \
7663     tcg_gen_##tcgop2(target, tmp);                              \
7664     tcg_temp_free_i64(tmp);                                     \
7665 }
7666
7667
7668 MV_VSRW(mfvsrwz, ext32u_i64, trunc_i64_tl, cpu_gpr[rA(ctx->opcode)], \
7669         cpu_vsrh(xS(ctx->opcode)))
7670 MV_VSRW(mtvsrwa, extu_tl_i64, ext32s_i64, cpu_vsrh(xT(ctx->opcode)), \
7671         cpu_gpr[rA(ctx->opcode)])
7672 MV_VSRW(mtvsrwz, extu_tl_i64, ext32u_i64, cpu_vsrh(xT(ctx->opcode)), \
7673         cpu_gpr[rA(ctx->opcode)])
7674
7675 #if defined(TARGET_PPC64)
7676 #define MV_VSRD(name, target, source)                           \
7677 static void gen_##name(DisasContext *ctx)                       \
7678 {                                                               \
7679     if (xS(ctx->opcode) < 32) {                                 \
7680         if (unlikely(!ctx->fpu_enabled)) {                      \
7681             gen_exception(ctx, POWERPC_EXCP_FPU);               \
7682             return;                                             \
7683         }                                                       \
7684     } else {                                                    \
7685         if (unlikely(!ctx->altivec_enabled)) {                  \
7686             gen_exception(ctx, POWERPC_EXCP_VPU);               \
7687             return;                                             \
7688         }                                                       \
7689     }                                                           \
7690     tcg_gen_mov_i64(target, source);                            \
7691 }
7692
7693 MV_VSRD(mfvsrd, cpu_gpr[rA(ctx->opcode)], cpu_vsrh(xS(ctx->opcode)))
7694 MV_VSRD(mtvsrd, cpu_vsrh(xT(ctx->opcode)), cpu_gpr[rA(ctx->opcode)])
7695
7696 #endif
7697
7698 static void gen_xxpermdi(DisasContext *ctx)
7699 {
7700     if (unlikely(!ctx->vsx_enabled)) {
7701         gen_exception(ctx, POWERPC_EXCP_VSXU);
7702         return;
7703     }
7704
7705     if (unlikely((xT(ctx->opcode) == xA(ctx->opcode)) ||
7706                  (xT(ctx->opcode) == xB(ctx->opcode)))) {
7707         TCGv_i64 xh, xl;
7708
7709         xh = tcg_temp_new_i64();
7710         xl = tcg_temp_new_i64();
7711
7712         if ((DM(ctx->opcode) & 2) == 0) {
7713             tcg_gen_mov_i64(xh, cpu_vsrh(xA(ctx->opcode)));
7714         } else {
7715             tcg_gen_mov_i64(xh, cpu_vsrl(xA(ctx->opcode)));
7716         }
7717         if ((DM(ctx->opcode) & 1) == 0) {
7718             tcg_gen_mov_i64(xl, cpu_vsrh(xB(ctx->opcode)));
7719         } else {
7720             tcg_gen_mov_i64(xl, cpu_vsrl(xB(ctx->opcode)));
7721         }
7722
7723         tcg_gen_mov_i64(cpu_vsrh(xT(ctx->opcode)), xh);
7724         tcg_gen_mov_i64(cpu_vsrl(xT(ctx->opcode)), xl);
7725
7726         tcg_temp_free_i64(xh);
7727         tcg_temp_free_i64(xl);
7728     } else {
7729         if ((DM(ctx->opcode) & 2) == 0) {
7730             tcg_gen_mov_i64(cpu_vsrh(xT(ctx->opcode)), cpu_vsrh(xA(ctx->opcode)));
7731         } else {
7732             tcg_gen_mov_i64(cpu_vsrh(xT(ctx->opcode)), cpu_vsrl(xA(ctx->opcode)));
7733         }
7734         if ((DM(ctx->opcode) & 1) == 0) {
7735             tcg_gen_mov_i64(cpu_vsrl(xT(ctx->opcode)), cpu_vsrh(xB(ctx->opcode)));
7736         } else {
7737             tcg_gen_mov_i64(cpu_vsrl(xT(ctx->opcode)), cpu_vsrl(xB(ctx->opcode)));
7738         }
7739     }
7740 }
7741
7742 #define OP_ABS 1
7743 #define OP_NABS 2
7744 #define OP_NEG 3
7745 #define OP_CPSGN 4
7746 #define SGN_MASK_DP  0x8000000000000000ull
7747 #define SGN_MASK_SP 0x8000000080000000ull
7748
7749 #define VSX_SCALAR_MOVE(name, op, sgn_mask)                       \
7750 static void glue(gen_, name)(DisasContext * ctx)                  \
7751     {                                                             \
7752         TCGv_i64 xb, sgm;                                         \
7753         if (unlikely(!ctx->vsx_enabled)) {                        \
7754             gen_exception(ctx, POWERPC_EXCP_VSXU);                \
7755             return;                                               \
7756         }                                                         \
7757         xb = tcg_temp_new_i64();                                  \
7758         sgm = tcg_temp_new_i64();                                 \
7759         tcg_gen_mov_i64(xb, cpu_vsrh(xB(ctx->opcode)));           \
7760         tcg_gen_movi_i64(sgm, sgn_mask);                          \
7761         switch (op) {                                             \
7762             case OP_ABS: {                                        \
7763                 tcg_gen_andc_i64(xb, xb, sgm);                    \
7764                 break;                                            \
7765             }                                                     \
7766             case OP_NABS: {                                       \
7767                 tcg_gen_or_i64(xb, xb, sgm);                      \
7768                 break;                                            \
7769             }                                                     \
7770             case OP_NEG: {                                        \
7771                 tcg_gen_xor_i64(xb, xb, sgm);                     \
7772                 break;                                            \
7773             }                                                     \
7774             case OP_CPSGN: {                                      \
7775                 TCGv_i64 xa = tcg_temp_new_i64();                 \
7776                 tcg_gen_mov_i64(xa, cpu_vsrh(xA(ctx->opcode)));   \
7777                 tcg_gen_and_i64(xa, xa, sgm);                     \
7778                 tcg_gen_andc_i64(xb, xb, sgm);                    \
7779                 tcg_gen_or_i64(xb, xb, xa);                       \
7780                 tcg_temp_free_i64(xa);                            \
7781                 break;                                            \
7782             }                                                     \
7783         }                                                         \
7784         tcg_gen_mov_i64(cpu_vsrh(xT(ctx->opcode)), xb);           \
7785         tcg_temp_free_i64(xb);                                    \
7786         tcg_temp_free_i64(sgm);                                   \
7787     }
7788
7789 VSX_SCALAR_MOVE(xsabsdp, OP_ABS, SGN_MASK_DP)
7790 VSX_SCALAR_MOVE(xsnabsdp, OP_NABS, SGN_MASK_DP)
7791 VSX_SCALAR_MOVE(xsnegdp, OP_NEG, SGN_MASK_DP)
7792 VSX_SCALAR_MOVE(xscpsgndp, OP_CPSGN, SGN_MASK_DP)
7793
7794 #define VSX_VECTOR_MOVE(name, op, sgn_mask)                      \
7795 static void glue(gen_, name)(DisasContext * ctx)                 \
7796     {                                                            \
7797         TCGv_i64 xbh, xbl, sgm;                                  \
7798         if (unlikely(!ctx->vsx_enabled)) {                       \
7799             gen_exception(ctx, POWERPC_EXCP_VSXU);               \
7800             return;                                              \
7801         }                                                        \
7802         xbh = tcg_temp_new_i64();                                \
7803         xbl = tcg_temp_new_i64();                                \
7804         sgm = tcg_temp_new_i64();                                \
7805         tcg_gen_mov_i64(xbh, cpu_vsrh(xB(ctx->opcode)));         \
7806         tcg_gen_mov_i64(xbl, cpu_vsrl(xB(ctx->opcode)));         \
7807         tcg_gen_movi_i64(sgm, sgn_mask);                         \
7808         switch (op) {                                            \
7809             case OP_ABS: {                                       \
7810                 tcg_gen_andc_i64(xbh, xbh, sgm);                 \
7811                 tcg_gen_andc_i64(xbl, xbl, sgm);                 \
7812                 break;                                           \
7813             }                                                    \
7814             case OP_NABS: {                                      \
7815                 tcg_gen_or_i64(xbh, xbh, sgm);                   \
7816                 tcg_gen_or_i64(xbl, xbl, sgm);                   \
7817                 break;                                           \
7818             }                                                    \
7819             case OP_NEG: {                                       \
7820                 tcg_gen_xor_i64(xbh, xbh, sgm);                  \
7821                 tcg_gen_xor_i64(xbl, xbl, sgm);                  \
7822                 break;                                           \
7823             }                                                    \
7824             case OP_CPSGN: {                                     \
7825                 TCGv_i64 xah = tcg_temp_new_i64();               \
7826                 TCGv_i64 xal = tcg_temp_new_i64();               \
7827                 tcg_gen_mov_i64(xah, cpu_vsrh(xA(ctx->opcode))); \
7828                 tcg_gen_mov_i64(xal, cpu_vsrl(xA(ctx->opcode))); \
7829                 tcg_gen_and_i64(xah, xah, sgm);                  \
7830                 tcg_gen_and_i64(xal, xal, sgm);                  \
7831                 tcg_gen_andc_i64(xbh, xbh, sgm);                 \
7832                 tcg_gen_andc_i64(xbl, xbl, sgm);                 \
7833                 tcg_gen_or_i64(xbh, xbh, xah);                   \
7834                 tcg_gen_or_i64(xbl, xbl, xal);                   \
7835                 tcg_temp_free_i64(xah);                          \
7836                 tcg_temp_free_i64(xal);                          \
7837                 break;                                           \
7838             }                                                    \
7839         }                                                        \
7840         tcg_gen_mov_i64(cpu_vsrh(xT(ctx->opcode)), xbh);         \
7841         tcg_gen_mov_i64(cpu_vsrl(xT(ctx->opcode)), xbl);         \
7842         tcg_temp_free_i64(xbh);                                  \
7843         tcg_temp_free_i64(xbl);                                  \
7844         tcg_temp_free_i64(sgm);                                  \
7845     }
7846
7847 VSX_VECTOR_MOVE(xvabsdp, OP_ABS, SGN_MASK_DP)
7848 VSX_VECTOR_MOVE(xvnabsdp, OP_NABS, SGN_MASK_DP)
7849 VSX_VECTOR_MOVE(xvnegdp, OP_NEG, SGN_MASK_DP)
7850 VSX_VECTOR_MOVE(xvcpsgndp, OP_CPSGN, SGN_MASK_DP)
7851 VSX_VECTOR_MOVE(xvabssp, OP_ABS, SGN_MASK_SP)
7852 VSX_VECTOR_MOVE(xvnabssp, OP_NABS, SGN_MASK_SP)
7853 VSX_VECTOR_MOVE(xvnegsp, OP_NEG, SGN_MASK_SP)
7854 VSX_VECTOR_MOVE(xvcpsgnsp, OP_CPSGN, SGN_MASK_SP)
7855
7856 #define GEN_VSX_HELPER_2(name, op1, op2, inval, type)                         \
7857 static void gen_##name(DisasContext * ctx)                                    \
7858 {                                                                             \
7859     TCGv_i32 opc;                                                             \
7860     if (unlikely(!ctx->vsx_enabled)) {                                        \
7861         gen_exception(ctx, POWERPC_EXCP_VSXU);                                \
7862         return;                                                               \
7863     }                                                                         \
7864     /* NIP cannot be restored if the memory exception comes from an helper */ \
7865     gen_update_nip(ctx, ctx->nip - 4);                                        \
7866     opc = tcg_const_i32(ctx->opcode);                                         \
7867     gen_helper_##name(cpu_env, opc);                                          \
7868     tcg_temp_free_i32(opc);                                                   \
7869 }
7870
7871 #define GEN_VSX_HELPER_XT_XB_ENV(name, op1, op2, inval, type) \
7872 static void gen_##name(DisasContext * ctx)                    \
7873 {                                                             \
7874     if (unlikely(!ctx->vsx_enabled)) {                        \
7875         gen_exception(ctx, POWERPC_EXCP_VSXU);                \
7876         return;                                               \
7877     }                                                         \
7878     /* NIP cannot be restored if the exception comes */       \
7879     /* from a helper. */                                      \
7880     gen_update_nip(ctx, ctx->nip - 4);                        \
7881                                                               \
7882     gen_helper_##name(cpu_vsrh(xT(ctx->opcode)), cpu_env,     \
7883                       cpu_vsrh(xB(ctx->opcode)));             \
7884 }
7885
7886 GEN_VSX_HELPER_2(xsadddp, 0x00, 0x04, 0, PPC2_VSX)
7887 GEN_VSX_HELPER_2(xssubdp, 0x00, 0x05, 0, PPC2_VSX)
7888 GEN_VSX_HELPER_2(xsmuldp, 0x00, 0x06, 0, PPC2_VSX)
7889 GEN_VSX_HELPER_2(xsdivdp, 0x00, 0x07, 0, PPC2_VSX)
7890 GEN_VSX_HELPER_2(xsredp, 0x14, 0x05, 0, PPC2_VSX)
7891 GEN_VSX_HELPER_2(xssqrtdp, 0x16, 0x04, 0, PPC2_VSX)
7892 GEN_VSX_HELPER_2(xsrsqrtedp, 0x14, 0x04, 0, PPC2_VSX)
7893 GEN_VSX_HELPER_2(xstdivdp, 0x14, 0x07, 0, PPC2_VSX)
7894 GEN_VSX_HELPER_2(xstsqrtdp, 0x14, 0x06, 0, PPC2_VSX)
7895 GEN_VSX_HELPER_2(xsmaddadp, 0x04, 0x04, 0, PPC2_VSX)
7896 GEN_VSX_HELPER_2(xsmaddmdp, 0x04, 0x05, 0, PPC2_VSX)
7897 GEN_VSX_HELPER_2(xsmsubadp, 0x04, 0x06, 0, PPC2_VSX)
7898 GEN_VSX_HELPER_2(xsmsubmdp, 0x04, 0x07, 0, PPC2_VSX)
7899 GEN_VSX_HELPER_2(xsnmaddadp, 0x04, 0x14, 0, PPC2_VSX)
7900 GEN_VSX_HELPER_2(xsnmaddmdp, 0x04, 0x15, 0, PPC2_VSX)
7901 GEN_VSX_HELPER_2(xsnmsubadp, 0x04, 0x16, 0, PPC2_VSX)
7902 GEN_VSX_HELPER_2(xsnmsubmdp, 0x04, 0x17, 0, PPC2_VSX)
7903 GEN_VSX_HELPER_2(xscmpodp, 0x0C, 0x05, 0, PPC2_VSX)
7904 GEN_VSX_HELPER_2(xscmpudp, 0x0C, 0x04, 0, PPC2_VSX)
7905 GEN_VSX_HELPER_2(xsmaxdp, 0x00, 0x14, 0, PPC2_VSX)
7906 GEN_VSX_HELPER_2(xsmindp, 0x00, 0x15, 0, PPC2_VSX)
7907 GEN_VSX_HELPER_2(xscvdpsp, 0x12, 0x10, 0, PPC2_VSX)
7908 GEN_VSX_HELPER_XT_XB_ENV(xscvdpspn, 0x16, 0x10, 0, PPC2_VSX207)
7909 GEN_VSX_HELPER_2(xscvspdp, 0x12, 0x14, 0, PPC2_VSX)
7910 GEN_VSX_HELPER_XT_XB_ENV(xscvspdpn, 0x16, 0x14, 0, PPC2_VSX207)
7911 GEN_VSX_HELPER_2(xscvdpsxds, 0x10, 0x15, 0, PPC2_VSX)
7912 GEN_VSX_HELPER_2(xscvdpsxws, 0x10, 0x05, 0, PPC2_VSX)
7913 GEN_VSX_HELPER_2(xscvdpuxds, 0x10, 0x14, 0, PPC2_VSX)
7914 GEN_VSX_HELPER_2(xscvdpuxws, 0x10, 0x04, 0, PPC2_VSX)
7915 GEN_VSX_HELPER_2(xscvsxddp, 0x10, 0x17, 0, PPC2_VSX)
7916 GEN_VSX_HELPER_2(xscvuxddp, 0x10, 0x16, 0, PPC2_VSX)
7917 GEN_VSX_HELPER_2(xsrdpi, 0x12, 0x04, 0, PPC2_VSX)
7918 GEN_VSX_HELPER_2(xsrdpic, 0x16, 0x06, 0, PPC2_VSX)
7919 GEN_VSX_HELPER_2(xsrdpim, 0x12, 0x07, 0, PPC2_VSX)
7920 GEN_VSX_HELPER_2(xsrdpip, 0x12, 0x06, 0, PPC2_VSX)
7921 GEN_VSX_HELPER_2(xsrdpiz, 0x12, 0x05, 0, PPC2_VSX)
7922 GEN_VSX_HELPER_XT_XB_ENV(xsrsp, 0x12, 0x11, 0, PPC2_VSX207)
7923
7924 GEN_VSX_HELPER_2(xsaddsp, 0x00, 0x00, 0, PPC2_VSX207)
7925 GEN_VSX_HELPER_2(xssubsp, 0x00, 0x01, 0, PPC2_VSX207)
7926 GEN_VSX_HELPER_2(xsmulsp, 0x00, 0x02, 0, PPC2_VSX207)
7927 GEN_VSX_HELPER_2(xsdivsp, 0x00, 0x03, 0, PPC2_VSX207)
7928 GEN_VSX_HELPER_2(xsresp, 0x14, 0x01, 0, PPC2_VSX207)
7929 GEN_VSX_HELPER_2(xssqrtsp, 0x16, 0x00, 0, PPC2_VSX207)
7930 GEN_VSX_HELPER_2(xsrsqrtesp, 0x14, 0x00, 0, PPC2_VSX207)
7931 GEN_VSX_HELPER_2(xsmaddasp, 0x04, 0x00, 0, PPC2_VSX207)
7932 GEN_VSX_HELPER_2(xsmaddmsp, 0x04, 0x01, 0, PPC2_VSX207)
7933 GEN_VSX_HELPER_2(xsmsubasp, 0x04, 0x02, 0, PPC2_VSX207)
7934 GEN_VSX_HELPER_2(xsmsubmsp, 0x04, 0x03, 0, PPC2_VSX207)
7935 GEN_VSX_HELPER_2(xsnmaddasp, 0x04, 0x10, 0, PPC2_VSX207)
7936 GEN_VSX_HELPER_2(xsnmaddmsp, 0x04, 0x11, 0, PPC2_VSX207)
7937 GEN_VSX_HELPER_2(xsnmsubasp, 0x04, 0x12, 0, PPC2_VSX207)
7938 GEN_VSX_HELPER_2(xsnmsubmsp, 0x04, 0x13, 0, PPC2_VSX207)
7939 GEN_VSX_HELPER_2(xscvsxdsp, 0x10, 0x13, 0, PPC2_VSX207)
7940 GEN_VSX_HELPER_2(xscvuxdsp, 0x10, 0x12, 0, PPC2_VSX207)
7941
7942 GEN_VSX_HELPER_2(xvadddp, 0x00, 0x0C, 0, PPC2_VSX)
7943 GEN_VSX_HELPER_2(xvsubdp, 0x00, 0x0D, 0, PPC2_VSX)
7944 GEN_VSX_HELPER_2(xvmuldp, 0x00, 0x0E, 0, PPC2_VSX)
7945 GEN_VSX_HELPER_2(xvdivdp, 0x00, 0x0F, 0, PPC2_VSX)
7946 GEN_VSX_HELPER_2(xvredp, 0x14, 0x0D, 0, PPC2_VSX)
7947 GEN_VSX_HELPER_2(xvsqrtdp, 0x16, 0x0C, 0, PPC2_VSX)
7948 GEN_VSX_HELPER_2(xvrsqrtedp, 0x14, 0x0C, 0, PPC2_VSX)
7949 GEN_VSX_HELPER_2(xvtdivdp, 0x14, 0x0F, 0, PPC2_VSX)
7950 GEN_VSX_HELPER_2(xvtsqrtdp, 0x14, 0x0E, 0, PPC2_VSX)
7951 GEN_VSX_HELPER_2(xvmaddadp, 0x04, 0x0C, 0, PPC2_VSX)
7952 GEN_VSX_HELPER_2(xvmaddmdp, 0x04, 0x0D, 0, PPC2_VSX)
7953 GEN_VSX_HELPER_2(xvmsubadp, 0x04, 0x0E, 0, PPC2_VSX)
7954 GEN_VSX_HELPER_2(xvmsubmdp, 0x04, 0x0F, 0, PPC2_VSX)
7955 GEN_VSX_HELPER_2(xvnmaddadp, 0x04, 0x1C, 0, PPC2_VSX)
7956 GEN_VSX_HELPER_2(xvnmaddmdp, 0x04, 0x1D, 0, PPC2_VSX)
7957 GEN_VSX_HELPER_2(xvnmsubadp, 0x04, 0x1E, 0, PPC2_VSX)
7958 GEN_VSX_HELPER_2(xvnmsubmdp, 0x04, 0x1F, 0, PPC2_VSX)
7959 GEN_VSX_HELPER_2(xvmaxdp, 0x00, 0x1C, 0, PPC2_VSX)
7960 GEN_VSX_HELPER_2(xvmindp, 0x00, 0x1D, 0, PPC2_VSX)
7961 GEN_VSX_HELPER_2(xvcmpeqdp, 0x0C, 0x0C, 0, PPC2_VSX)
7962 GEN_VSX_HELPER_2(xvcmpgtdp, 0x0C, 0x0D, 0, PPC2_VSX)
7963 GEN_VSX_HELPER_2(xvcmpgedp, 0x0C, 0x0E, 0, PPC2_VSX)
7964 GEN_VSX_HELPER_2(xvcvdpsp, 0x12, 0x18, 0, PPC2_VSX)
7965 GEN_VSX_HELPER_2(xvcvdpsxds, 0x10, 0x1D, 0, PPC2_VSX)
7966 GEN_VSX_HELPER_2(xvcvdpsxws, 0x10, 0x0D, 0, PPC2_VSX)
7967 GEN_VSX_HELPER_2(xvcvdpuxds, 0x10, 0x1C, 0, PPC2_VSX)
7968 GEN_VSX_HELPER_2(xvcvdpuxws, 0x10, 0x0C, 0, PPC2_VSX)
7969 GEN_VSX_HELPER_2(xvcvsxddp, 0x10, 0x1F, 0, PPC2_VSX)
7970 GEN_VSX_HELPER_2(xvcvuxddp, 0x10, 0x1E, 0, PPC2_VSX)
7971 GEN_VSX_HELPER_2(xvcvsxwdp, 0x10, 0x0F, 0, PPC2_VSX)
7972 GEN_VSX_HELPER_2(xvcvuxwdp, 0x10, 0x0E, 0, PPC2_VSX)
7973 GEN_VSX_HELPER_2(xvrdpi, 0x12, 0x0C, 0, PPC2_VSX)
7974 GEN_VSX_HELPER_2(xvrdpic, 0x16, 0x0E, 0, PPC2_VSX)
7975 GEN_VSX_HELPER_2(xvrdpim, 0x12, 0x0F, 0, PPC2_VSX)
7976 GEN_VSX_HELPER_2(xvrdpip, 0x12, 0x0E, 0, PPC2_VSX)
7977 GEN_VSX_HELPER_2(xvrdpiz, 0x12, 0x0D, 0, PPC2_VSX)
7978
7979 GEN_VSX_HELPER_2(xvaddsp, 0x00, 0x08, 0, PPC2_VSX)
7980 GEN_VSX_HELPER_2(xvsubsp, 0x00, 0x09, 0, PPC2_VSX)
7981 GEN_VSX_HELPER_2(xvmulsp, 0x00, 0x0A, 0, PPC2_VSX)
7982 GEN_VSX_HELPER_2(xvdivsp, 0x00, 0x0B, 0, PPC2_VSX)
7983 GEN_VSX_HELPER_2(xvresp, 0x14, 0x09, 0, PPC2_VSX)
7984 GEN_VSX_HELPER_2(xvsqrtsp, 0x16, 0x08, 0, PPC2_VSX)
7985 GEN_VSX_HELPER_2(xvrsqrtesp, 0x14, 0x08, 0, PPC2_VSX)
7986 GEN_VSX_HELPER_2(xvtdivsp, 0x14, 0x0B, 0, PPC2_VSX)
7987 GEN_VSX_HELPER_2(xvtsqrtsp, 0x14, 0x0A, 0, PPC2_VSX)
7988 GEN_VSX_HELPER_2(xvmaddasp, 0x04, 0x08, 0, PPC2_VSX)
7989 GEN_VSX_HELPER_2(xvmaddmsp, 0x04, 0x09, 0, PPC2_VSX)
7990 GEN_VSX_HELPER_2(xvmsubasp, 0x04, 0x0A, 0, PPC2_VSX)
7991 GEN_VSX_HELPER_2(xvmsubmsp, 0x04, 0x0B, 0, PPC2_VSX)
7992 GEN_VSX_HELPER_2(xvnmaddasp, 0x04, 0x18, 0, PPC2_VSX)
7993 GEN_VSX_HELPER_2(xvnmaddmsp, 0x04, 0x19, 0, PPC2_VSX)
7994 GEN_VSX_HELPER_2(xvnmsubasp, 0x04, 0x1A, 0, PPC2_VSX)
7995 GEN_VSX_HELPER_2(xvnmsubmsp, 0x04, 0x1B, 0, PPC2_VSX)
7996 GEN_VSX_HELPER_2(xvmaxsp, 0x00, 0x18, 0, PPC2_VSX)
7997 GEN_VSX_HELPER_2(xvminsp, 0x00, 0x19, 0, PPC2_VSX)
7998 GEN_VSX_HELPER_2(xvcmpeqsp, 0x0C, 0x08, 0, PPC2_VSX)
7999 GEN_VSX_HELPER_2(xvcmpgtsp, 0x0C, 0x09, 0, PPC2_VSX)
8000 GEN_VSX_HELPER_2(xvcmpgesp, 0x0C, 0x0A, 0, PPC2_VSX)
8001 GEN_VSX_HELPER_2(xvcvspdp, 0x12, 0x1C, 0, PPC2_VSX)
8002 GEN_VSX_HELPER_2(xvcvspsxds, 0x10, 0x19, 0, PPC2_VSX)
8003 GEN_VSX_HELPER_2(xvcvspsxws, 0x10, 0x09, 0, PPC2_VSX)
8004 GEN_VSX_HELPER_2(xvcvspuxds, 0x10, 0x18, 0, PPC2_VSX)
8005 GEN_VSX_HELPER_2(xvcvspuxws, 0x10, 0x08, 0, PPC2_VSX)
8006 GEN_VSX_HELPER_2(xvcvsxdsp, 0x10, 0x1B, 0, PPC2_VSX)
8007 GEN_VSX_HELPER_2(xvcvuxdsp, 0x10, 0x1A, 0, PPC2_VSX)
8008 GEN_VSX_HELPER_2(xvcvsxwsp, 0x10, 0x0B, 0, PPC2_VSX)
8009 GEN_VSX_HELPER_2(xvcvuxwsp, 0x10, 0x0A, 0, PPC2_VSX)
8010 GEN_VSX_HELPER_2(xvrspi, 0x12, 0x08, 0, PPC2_VSX)
8011 GEN_VSX_HELPER_2(xvrspic, 0x16, 0x0A, 0, PPC2_VSX)
8012 GEN_VSX_HELPER_2(xvrspim, 0x12, 0x0B, 0, PPC2_VSX)
8013 GEN_VSX_HELPER_2(xvrspip, 0x12, 0x0A, 0, PPC2_VSX)
8014 GEN_VSX_HELPER_2(xvrspiz, 0x12, 0x09, 0, PPC2_VSX)
8015
8016 #define VSX_LOGICAL(name, tcg_op)                                    \
8017 static void glue(gen_, name)(DisasContext * ctx)                     \
8018     {                                                                \
8019         if (unlikely(!ctx->vsx_enabled)) {                           \
8020             gen_exception(ctx, POWERPC_EXCP_VSXU);                   \
8021             return;                                                  \
8022         }                                                            \
8023         tcg_op(cpu_vsrh(xT(ctx->opcode)), cpu_vsrh(xA(ctx->opcode)), \
8024             cpu_vsrh(xB(ctx->opcode)));                              \
8025         tcg_op(cpu_vsrl(xT(ctx->opcode)), cpu_vsrl(xA(ctx->opcode)), \
8026             cpu_vsrl(xB(ctx->opcode)));                              \
8027     }
8028
8029 VSX_LOGICAL(xxland, tcg_gen_and_i64)
8030 VSX_LOGICAL(xxlandc, tcg_gen_andc_i64)
8031 VSX_LOGICAL(xxlor, tcg_gen_or_i64)
8032 VSX_LOGICAL(xxlxor, tcg_gen_xor_i64)
8033 VSX_LOGICAL(xxlnor, tcg_gen_nor_i64)
8034 VSX_LOGICAL(xxleqv, tcg_gen_eqv_i64)
8035 VSX_LOGICAL(xxlnand, tcg_gen_nand_i64)
8036 VSX_LOGICAL(xxlorc, tcg_gen_orc_i64)
8037
8038 #define VSX_XXMRG(name, high)                               \
8039 static void glue(gen_, name)(DisasContext * ctx)            \
8040     {                                                       \
8041         TCGv_i64 a0, a1, b0, b1;                            \
8042         if (unlikely(!ctx->vsx_enabled)) {                  \
8043             gen_exception(ctx, POWERPC_EXCP_VSXU);          \
8044             return;                                         \
8045         }                                                   \
8046         a0 = tcg_temp_new_i64();                            \
8047         a1 = tcg_temp_new_i64();                            \
8048         b0 = tcg_temp_new_i64();                            \
8049         b1 = tcg_temp_new_i64();                            \
8050         if (high) {                                         \
8051             tcg_gen_mov_i64(a0, cpu_vsrh(xA(ctx->opcode))); \
8052             tcg_gen_mov_i64(a1, cpu_vsrh(xA(ctx->opcode))); \
8053             tcg_gen_mov_i64(b0, cpu_vsrh(xB(ctx->opcode))); \
8054             tcg_gen_mov_i64(b1, cpu_vsrh(xB(ctx->opcode))); \
8055         } else {                                            \
8056             tcg_gen_mov_i64(a0, cpu_vsrl(xA(ctx->opcode))); \
8057             tcg_gen_mov_i64(a1, cpu_vsrl(xA(ctx->opcode))); \
8058             tcg_gen_mov_i64(b0, cpu_vsrl(xB(ctx->opcode))); \
8059             tcg_gen_mov_i64(b1, cpu_vsrl(xB(ctx->opcode))); \
8060         }                                                   \
8061         tcg_gen_shri_i64(a0, a0, 32);                       \
8062         tcg_gen_shri_i64(b0, b0, 32);                       \
8063         tcg_gen_deposit_i64(cpu_vsrh(xT(ctx->opcode)),      \
8064                             b0, a0, 32, 32);                \
8065         tcg_gen_deposit_i64(cpu_vsrl(xT(ctx->opcode)),      \
8066                             b1, a1, 32, 32);                \
8067         tcg_temp_free_i64(a0);                              \
8068         tcg_temp_free_i64(a1);                              \
8069         tcg_temp_free_i64(b0);                              \
8070         tcg_temp_free_i64(b1);                              \
8071     }
8072
8073 VSX_XXMRG(xxmrghw, 1)
8074 VSX_XXMRG(xxmrglw, 0)
8075
8076 static void gen_xxsel(DisasContext * ctx)
8077 {
8078     TCGv_i64 a, b, c;
8079     if (unlikely(!ctx->vsx_enabled)) {
8080         gen_exception(ctx, POWERPC_EXCP_VSXU);
8081         return;
8082     }
8083     a = tcg_temp_new_i64();
8084     b = tcg_temp_new_i64();
8085     c = tcg_temp_new_i64();
8086
8087     tcg_gen_mov_i64(a, cpu_vsrh(xA(ctx->opcode)));
8088     tcg_gen_mov_i64(b, cpu_vsrh(xB(ctx->opcode)));
8089     tcg_gen_mov_i64(c, cpu_vsrh(xC(ctx->opcode)));
8090
8091     tcg_gen_and_i64(b, b, c);
8092     tcg_gen_andc_i64(a, a, c);
8093     tcg_gen_or_i64(cpu_vsrh(xT(ctx->opcode)), a, b);
8094
8095     tcg_gen_mov_i64(a, cpu_vsrl(xA(ctx->opcode)));
8096     tcg_gen_mov_i64(b, cpu_vsrl(xB(ctx->opcode)));
8097     tcg_gen_mov_i64(c, cpu_vsrl(xC(ctx->opcode)));
8098
8099     tcg_gen_and_i64(b, b, c);
8100     tcg_gen_andc_i64(a, a, c);
8101     tcg_gen_or_i64(cpu_vsrl(xT(ctx->opcode)), a, b);
8102
8103     tcg_temp_free_i64(a);
8104     tcg_temp_free_i64(b);
8105     tcg_temp_free_i64(c);
8106 }
8107
8108 static void gen_xxspltw(DisasContext *ctx)
8109 {
8110     TCGv_i64 b, b2;
8111     TCGv_i64 vsr = (UIM(ctx->opcode) & 2) ?
8112                    cpu_vsrl(xB(ctx->opcode)) :
8113                    cpu_vsrh(xB(ctx->opcode));
8114
8115     if (unlikely(!ctx->vsx_enabled)) {
8116         gen_exception(ctx, POWERPC_EXCP_VSXU);
8117         return;
8118     }
8119
8120     b = tcg_temp_new_i64();
8121     b2 = tcg_temp_new_i64();
8122
8123     if (UIM(ctx->opcode) & 1) {
8124         tcg_gen_ext32u_i64(b, vsr);
8125     } else {
8126         tcg_gen_shri_i64(b, vsr, 32);
8127     }
8128
8129     tcg_gen_shli_i64(b2, b, 32);
8130     tcg_gen_or_i64(cpu_vsrh(xT(ctx->opcode)), b, b2);
8131     tcg_gen_mov_i64(cpu_vsrl(xT(ctx->opcode)), cpu_vsrh(xT(ctx->opcode)));
8132
8133     tcg_temp_free_i64(b);
8134     tcg_temp_free_i64(b2);
8135 }
8136
8137 static void gen_xxsldwi(DisasContext *ctx)
8138 {
8139     TCGv_i64 xth, xtl;
8140     if (unlikely(!ctx->vsx_enabled)) {
8141         gen_exception(ctx, POWERPC_EXCP_VSXU);
8142         return;
8143     }
8144     xth = tcg_temp_new_i64();
8145     xtl = tcg_temp_new_i64();
8146
8147     switch (SHW(ctx->opcode)) {
8148         case 0: {
8149             tcg_gen_mov_i64(xth, cpu_vsrh(xA(ctx->opcode)));
8150             tcg_gen_mov_i64(xtl, cpu_vsrl(xA(ctx->opcode)));
8151             break;
8152         }
8153         case 1: {
8154             TCGv_i64 t0 = tcg_temp_new_i64();
8155             tcg_gen_mov_i64(xth, cpu_vsrh(xA(ctx->opcode)));
8156             tcg_gen_shli_i64(xth, xth, 32);
8157             tcg_gen_mov_i64(t0, cpu_vsrl(xA(ctx->opcode)));
8158             tcg_gen_shri_i64(t0, t0, 32);
8159             tcg_gen_or_i64(xth, xth, t0);
8160             tcg_gen_mov_i64(xtl, cpu_vsrl(xA(ctx->opcode)));
8161             tcg_gen_shli_i64(xtl, xtl, 32);
8162             tcg_gen_mov_i64(t0, cpu_vsrh(xB(ctx->opcode)));
8163             tcg_gen_shri_i64(t0, t0, 32);
8164             tcg_gen_or_i64(xtl, xtl, t0);
8165             tcg_temp_free_i64(t0);
8166             break;
8167         }
8168         case 2: {
8169             tcg_gen_mov_i64(xth, cpu_vsrl(xA(ctx->opcode)));
8170             tcg_gen_mov_i64(xtl, cpu_vsrh(xB(ctx->opcode)));
8171             break;
8172         }
8173         case 3: {
8174             TCGv_i64 t0 = tcg_temp_new_i64();
8175             tcg_gen_mov_i64(xth, cpu_vsrl(xA(ctx->opcode)));
8176             tcg_gen_shli_i64(xth, xth, 32);
8177             tcg_gen_mov_i64(t0, cpu_vsrh(xB(ctx->opcode)));
8178             tcg_gen_shri_i64(t0, t0, 32);
8179             tcg_gen_or_i64(xth, xth, t0);
8180             tcg_gen_mov_i64(xtl, cpu_vsrh(xB(ctx->opcode)));
8181             tcg_gen_shli_i64(xtl, xtl, 32);
8182             tcg_gen_mov_i64(t0, cpu_vsrl(xB(ctx->opcode)));
8183             tcg_gen_shri_i64(t0, t0, 32);
8184             tcg_gen_or_i64(xtl, xtl, t0);
8185             tcg_temp_free_i64(t0);
8186             break;
8187         }
8188     }
8189
8190     tcg_gen_mov_i64(cpu_vsrh(xT(ctx->opcode)), xth);
8191     tcg_gen_mov_i64(cpu_vsrl(xT(ctx->opcode)), xtl);
8192
8193     tcg_temp_free_i64(xth);
8194     tcg_temp_free_i64(xtl);
8195 }
8196
8197 /*** Decimal Floating Point ***/
8198
8199 static inline TCGv_ptr gen_fprp_ptr(int reg)
8200 {
8201     TCGv_ptr r = tcg_temp_new_ptr();
8202     tcg_gen_addi_ptr(r, cpu_env, offsetof(CPUPPCState, fpr[reg]));
8203     return r;
8204 }
8205
8206 #if defined(TARGET_PPC64)
8207 static void gen_set_cr6_from_fpscr(DisasContext *ctx)
8208 {
8209     TCGv_i32 tmp = tcg_temp_new_i32();
8210     tcg_gen_trunc_tl_i32(tmp, cpu_fpscr);
8211     tcg_gen_shri_i32(cpu_crf[1], tmp, 28);
8212     tcg_temp_free_i32(tmp);
8213 }
8214 #else
8215 static void gen_set_cr6_from_fpscr(DisasContext *ctx)
8216 {
8217         tcg_gen_shri_tl(cpu_crf[1], cpu_fpscr, 28);
8218 }
8219 #endif
8220
8221 #define GEN_DFP_T_A_B_Rc(name)                   \
8222 static void gen_##name(DisasContext *ctx)        \
8223 {                                                \
8224     TCGv_ptr rd, ra, rb;                         \
8225     if (unlikely(!ctx->fpu_enabled)) {           \
8226         gen_exception(ctx, POWERPC_EXCP_FPU);    \
8227         return;                                  \
8228     }                                            \
8229     gen_update_nip(ctx, ctx->nip - 4);           \
8230     rd = gen_fprp_ptr(rD(ctx->opcode));          \
8231     ra = gen_fprp_ptr(rA(ctx->opcode));          \
8232     rb = gen_fprp_ptr(rB(ctx->opcode));          \
8233     gen_helper_##name(cpu_env, rd, ra, rb);      \
8234     if (unlikely(Rc(ctx->opcode) != 0)) {        \
8235         gen_set_cr6_from_fpscr(ctx);             \
8236     }                                            \
8237     tcg_temp_free_ptr(rd);                       \
8238     tcg_temp_free_ptr(ra);                       \
8239     tcg_temp_free_ptr(rb);                       \
8240 }
8241
8242 #define GEN_DFP_BF_A_B(name)                      \
8243 static void gen_##name(DisasContext *ctx)         \
8244 {                                                 \
8245     TCGv_ptr ra, rb;                              \
8246     if (unlikely(!ctx->fpu_enabled)) {            \
8247         gen_exception(ctx, POWERPC_EXCP_FPU);     \
8248         return;                                   \
8249     }                                             \
8250     gen_update_nip(ctx, ctx->nip - 4);            \
8251     ra = gen_fprp_ptr(rA(ctx->opcode));           \
8252     rb = gen_fprp_ptr(rB(ctx->opcode));           \
8253     gen_helper_##name(cpu_crf[crfD(ctx->opcode)], \
8254                       cpu_env, ra, rb);           \
8255     tcg_temp_free_ptr(ra);                        \
8256     tcg_temp_free_ptr(rb);                        \
8257 }
8258
8259 #define GEN_DFP_BF_A_DCM(name)                    \
8260 static void gen_##name(DisasContext *ctx)         \
8261 {                                                 \
8262     TCGv_ptr ra;                                  \
8263     TCGv_i32 dcm;                                 \
8264     if (unlikely(!ctx->fpu_enabled)) {            \
8265         gen_exception(ctx, POWERPC_EXCP_FPU);     \
8266         return;                                   \
8267     }                                             \
8268     gen_update_nip(ctx, ctx->nip - 4);            \
8269     ra = gen_fprp_ptr(rA(ctx->opcode));           \
8270     dcm = tcg_const_i32(DCM(ctx->opcode));        \
8271     gen_helper_##name(cpu_crf[crfD(ctx->opcode)], \
8272                       cpu_env, ra, dcm);          \
8273     tcg_temp_free_ptr(ra);                        \
8274     tcg_temp_free_i32(dcm);                       \
8275 }
8276
8277 #define GEN_DFP_T_B_U32_U32_Rc(name, u32f1, u32f2)    \
8278 static void gen_##name(DisasContext *ctx)             \
8279 {                                                     \
8280     TCGv_ptr rt, rb;                                  \
8281     TCGv_i32 u32_1, u32_2;                            \
8282     if (unlikely(!ctx->fpu_enabled)) {                \
8283         gen_exception(ctx, POWERPC_EXCP_FPU);         \
8284         return;                                       \
8285     }                                                 \
8286     gen_update_nip(ctx, ctx->nip - 4);                \
8287     rt = gen_fprp_ptr(rD(ctx->opcode));               \
8288     rb = gen_fprp_ptr(rB(ctx->opcode));               \
8289     u32_1 = tcg_const_i32(u32f1(ctx->opcode));        \
8290     u32_2 = tcg_const_i32(u32f2(ctx->opcode));        \
8291     gen_helper_##name(cpu_env, rt, rb, u32_1, u32_2); \
8292     if (unlikely(Rc(ctx->opcode) != 0)) {             \
8293         gen_set_cr6_from_fpscr(ctx);                  \
8294     }                                                 \
8295     tcg_temp_free_ptr(rt);                            \
8296     tcg_temp_free_ptr(rb);                            \
8297     tcg_temp_free_i32(u32_1);                         \
8298     tcg_temp_free_i32(u32_2);                         \
8299 }
8300
8301 #define GEN_DFP_T_A_B_I32_Rc(name, i32fld)       \
8302 static void gen_##name(DisasContext *ctx)        \
8303 {                                                \
8304     TCGv_ptr rt, ra, rb;                         \
8305     TCGv_i32 i32;                                \
8306     if (unlikely(!ctx->fpu_enabled)) {           \
8307         gen_exception(ctx, POWERPC_EXCP_FPU);    \
8308         return;                                  \
8309     }                                            \
8310     gen_update_nip(ctx, ctx->nip - 4);           \
8311     rt = gen_fprp_ptr(rD(ctx->opcode));          \
8312     ra = gen_fprp_ptr(rA(ctx->opcode));          \
8313     rb = gen_fprp_ptr(rB(ctx->opcode));          \
8314     i32 = tcg_const_i32(i32fld(ctx->opcode));    \
8315     gen_helper_##name(cpu_env, rt, ra, rb, i32); \
8316     if (unlikely(Rc(ctx->opcode) != 0)) {        \
8317         gen_set_cr6_from_fpscr(ctx);             \
8318     }                                            \
8319     tcg_temp_free_ptr(rt);                       \
8320     tcg_temp_free_ptr(rb);                       \
8321     tcg_temp_free_ptr(ra);                       \
8322     tcg_temp_free_i32(i32);                      \
8323     }
8324
8325 #define GEN_DFP_T_B_Rc(name)                     \
8326 static void gen_##name(DisasContext *ctx)        \
8327 {                                                \
8328     TCGv_ptr rt, rb;                             \
8329     if (unlikely(!ctx->fpu_enabled)) {           \
8330         gen_exception(ctx, POWERPC_EXCP_FPU);    \
8331         return;                                  \
8332     }                                            \
8333     gen_update_nip(ctx, ctx->nip - 4);           \
8334     rt = gen_fprp_ptr(rD(ctx->opcode));          \
8335     rb = gen_fprp_ptr(rB(ctx->opcode));          \
8336     gen_helper_##name(cpu_env, rt, rb);          \
8337     if (unlikely(Rc(ctx->opcode) != 0)) {        \
8338         gen_set_cr6_from_fpscr(ctx);             \
8339     }                                            \
8340     tcg_temp_free_ptr(rt);                       \
8341     tcg_temp_free_ptr(rb);                       \
8342     }
8343
8344 #define GEN_DFP_T_FPR_I32_Rc(name, fprfld, i32fld) \
8345 static void gen_##name(DisasContext *ctx)          \
8346 {                                                  \
8347     TCGv_ptr rt, rs;                               \
8348     TCGv_i32 i32;                                  \
8349     if (unlikely(!ctx->fpu_enabled)) {             \
8350         gen_exception(ctx, POWERPC_EXCP_FPU);      \
8351         return;                                    \
8352     }                                              \
8353     gen_update_nip(ctx, ctx->nip - 4);             \
8354     rt = gen_fprp_ptr(rD(ctx->opcode));            \
8355     rs = gen_fprp_ptr(fprfld(ctx->opcode));        \
8356     i32 = tcg_const_i32(i32fld(ctx->opcode));      \
8357     gen_helper_##name(cpu_env, rt, rs, i32);       \
8358     if (unlikely(Rc(ctx->opcode) != 0)) {          \
8359         gen_set_cr6_from_fpscr(ctx);               \
8360     }                                              \
8361     tcg_temp_free_ptr(rt);                         \
8362     tcg_temp_free_ptr(rs);                         \
8363     tcg_temp_free_i32(i32);                        \
8364 }
8365
8366 GEN_DFP_T_A_B_Rc(dadd)
8367 GEN_DFP_T_A_B_Rc(daddq)
8368 GEN_DFP_T_A_B_Rc(dsub)
8369 GEN_DFP_T_A_B_Rc(dsubq)
8370 GEN_DFP_T_A_B_Rc(dmul)
8371 GEN_DFP_T_A_B_Rc(dmulq)
8372 GEN_DFP_T_A_B_Rc(ddiv)
8373 GEN_DFP_T_A_B_Rc(ddivq)
8374 GEN_DFP_BF_A_B(dcmpu)
8375 GEN_DFP_BF_A_B(dcmpuq)
8376 GEN_DFP_BF_A_B(dcmpo)
8377 GEN_DFP_BF_A_B(dcmpoq)
8378 GEN_DFP_BF_A_DCM(dtstdc)
8379 GEN_DFP_BF_A_DCM(dtstdcq)
8380 GEN_DFP_BF_A_DCM(dtstdg)
8381 GEN_DFP_BF_A_DCM(dtstdgq)
8382 GEN_DFP_BF_A_B(dtstex)
8383 GEN_DFP_BF_A_B(dtstexq)
8384 GEN_DFP_BF_A_B(dtstsf)
8385 GEN_DFP_BF_A_B(dtstsfq)
8386 GEN_DFP_T_B_U32_U32_Rc(dquai, SIMM5, RMC)
8387 GEN_DFP_T_B_U32_U32_Rc(dquaiq, SIMM5, RMC)
8388 GEN_DFP_T_A_B_I32_Rc(dqua, RMC)
8389 GEN_DFP_T_A_B_I32_Rc(dquaq, RMC)
8390 GEN_DFP_T_A_B_I32_Rc(drrnd, RMC)
8391 GEN_DFP_T_A_B_I32_Rc(drrndq, RMC)
8392 GEN_DFP_T_B_U32_U32_Rc(drintx, FPW, RMC)
8393 GEN_DFP_T_B_U32_U32_Rc(drintxq, FPW, RMC)
8394 GEN_DFP_T_B_U32_U32_Rc(drintn, FPW, RMC)
8395 GEN_DFP_T_B_U32_U32_Rc(drintnq, FPW, RMC)
8396 GEN_DFP_T_B_Rc(dctdp)
8397 GEN_DFP_T_B_Rc(dctqpq)
8398 GEN_DFP_T_B_Rc(drsp)
8399 GEN_DFP_T_B_Rc(drdpq)
8400 GEN_DFP_T_B_Rc(dcffix)
8401 GEN_DFP_T_B_Rc(dcffixq)
8402 GEN_DFP_T_B_Rc(dctfix)
8403 GEN_DFP_T_B_Rc(dctfixq)
8404 GEN_DFP_T_FPR_I32_Rc(ddedpd, rB, SP)
8405 GEN_DFP_T_FPR_I32_Rc(ddedpdq, rB, SP)
8406 GEN_DFP_T_FPR_I32_Rc(denbcd, rB, SP)
8407 GEN_DFP_T_FPR_I32_Rc(denbcdq, rB, SP)
8408 GEN_DFP_T_B_Rc(dxex)
8409 GEN_DFP_T_B_Rc(dxexq)
8410 GEN_DFP_T_A_B_Rc(diex)
8411 GEN_DFP_T_A_B_Rc(diexq)
8412 GEN_DFP_T_FPR_I32_Rc(dscli, rA, DCM)
8413 GEN_DFP_T_FPR_I32_Rc(dscliq, rA, DCM)
8414 GEN_DFP_T_FPR_I32_Rc(dscri, rA, DCM)
8415 GEN_DFP_T_FPR_I32_Rc(dscriq, rA, DCM)
8416
8417 /***                           SPE extension                               ***/
8418 /* Register moves */
8419
8420 static inline void gen_evmra(DisasContext *ctx)
8421 {
8422
8423     if (unlikely(!ctx->spe_enabled)) {
8424         gen_exception(ctx, POWERPC_EXCP_SPEU);
8425         return;
8426     }
8427
8428 #if defined(TARGET_PPC64)
8429     /* rD := rA */
8430     tcg_gen_mov_i64(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)]);
8431
8432     /* spe_acc := rA */
8433     tcg_gen_st_i64(cpu_gpr[rA(ctx->opcode)],
8434                    cpu_env,
8435                    offsetof(CPUPPCState, spe_acc));
8436 #else
8437     TCGv_i64 tmp = tcg_temp_new_i64();
8438
8439     /* tmp := rA_lo + rA_hi << 32 */
8440     tcg_gen_concat_i32_i64(tmp, cpu_gpr[rA(ctx->opcode)], cpu_gprh[rA(ctx->opcode)]);
8441
8442     /* spe_acc := tmp */
8443     tcg_gen_st_i64(tmp, cpu_env, offsetof(CPUPPCState, spe_acc));
8444     tcg_temp_free_i64(tmp);
8445
8446     /* rD := rA */
8447     tcg_gen_mov_i32(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)]);
8448     tcg_gen_mov_i32(cpu_gprh[rD(ctx->opcode)], cpu_gprh[rA(ctx->opcode)]);
8449 #endif
8450 }
8451
8452 static inline void gen_load_gpr64(TCGv_i64 t, int reg)
8453 {
8454 #if defined(TARGET_PPC64)
8455     tcg_gen_mov_i64(t, cpu_gpr[reg]);
8456 #else
8457     tcg_gen_concat_i32_i64(t, cpu_gpr[reg], cpu_gprh[reg]);
8458 #endif
8459 }
8460
8461 static inline void gen_store_gpr64(int reg, TCGv_i64 t)
8462 {
8463 #if defined(TARGET_PPC64)
8464     tcg_gen_mov_i64(cpu_gpr[reg], t);
8465 #else
8466     TCGv_i64 tmp = tcg_temp_new_i64();
8467     tcg_gen_trunc_i64_i32(cpu_gpr[reg], t);
8468     tcg_gen_shri_i64(tmp, t, 32);
8469     tcg_gen_trunc_i64_i32(cpu_gprh[reg], tmp);
8470     tcg_temp_free_i64(tmp);
8471 #endif
8472 }
8473
8474 #define GEN_SPE(name0, name1, opc2, opc3, inval0, inval1, type)         \
8475 static void glue(gen_, name0##_##name1)(DisasContext *ctx)                    \
8476 {                                                                             \
8477     if (Rc(ctx->opcode))                                                      \
8478         gen_##name1(ctx);                                                     \
8479     else                                                                      \
8480         gen_##name0(ctx);                                                     \
8481 }
8482
8483 /* Handler for undefined SPE opcodes */
8484 static inline void gen_speundef(DisasContext *ctx)
8485 {
8486     gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL);
8487 }
8488
8489 /* SPE logic */
8490 #if defined(TARGET_PPC64)
8491 #define GEN_SPEOP_LOGIC2(name, tcg_op)                                        \
8492 static inline void gen_##name(DisasContext *ctx)                              \
8493 {                                                                             \
8494     if (unlikely(!ctx->spe_enabled)) {                                        \
8495         gen_exception(ctx, POWERPC_EXCP_SPEU);                                \
8496         return;                                                               \
8497     }                                                                         \
8498     tcg_op(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)],                \
8499            cpu_gpr[rB(ctx->opcode)]);                                         \
8500 }
8501 #else
8502 #define GEN_SPEOP_LOGIC2(name, tcg_op)                                        \
8503 static inline void gen_##name(DisasContext *ctx)                              \
8504 {                                                                             \
8505     if (unlikely(!ctx->spe_enabled)) {                                        \
8506         gen_exception(ctx, POWERPC_EXCP_SPEU);                                \
8507         return;                                                               \
8508     }                                                                         \
8509     tcg_op(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)],                \
8510            cpu_gpr[rB(ctx->opcode)]);                                         \
8511     tcg_op(cpu_gprh[rD(ctx->opcode)], cpu_gprh[rA(ctx->opcode)],              \
8512            cpu_gprh[rB(ctx->opcode)]);                                        \
8513 }
8514 #endif
8515
8516 GEN_SPEOP_LOGIC2(evand, tcg_gen_and_tl);
8517 GEN_SPEOP_LOGIC2(evandc, tcg_gen_andc_tl);
8518 GEN_SPEOP_LOGIC2(evxor, tcg_gen_xor_tl);
8519 GEN_SPEOP_LOGIC2(evor, tcg_gen_or_tl);
8520 GEN_SPEOP_LOGIC2(evnor, tcg_gen_nor_tl);
8521 GEN_SPEOP_LOGIC2(eveqv, tcg_gen_eqv_tl);
8522 GEN_SPEOP_LOGIC2(evorc, tcg_gen_orc_tl);
8523 GEN_SPEOP_LOGIC2(evnand, tcg_gen_nand_tl);
8524
8525 /* SPE logic immediate */
8526 #if defined(TARGET_PPC64)
8527 #define GEN_SPEOP_TCG_LOGIC_IMM2(name, tcg_opi)                               \
8528 static inline void gen_##name(DisasContext *ctx)                              \
8529 {                                                                             \
8530     if (unlikely(!ctx->spe_enabled)) {                                        \
8531         gen_exception(ctx, POWERPC_EXCP_SPEU);                                \
8532         return;                                                               \
8533     }                                                                         \
8534     TCGv_i32 t0 = tcg_temp_local_new_i32();                                   \
8535     TCGv_i32 t1 = tcg_temp_local_new_i32();                                   \
8536     TCGv_i64 t2 = tcg_temp_local_new_i64();                                   \
8537     tcg_gen_trunc_i64_i32(t0, cpu_gpr[rA(ctx->opcode)]);                      \
8538     tcg_opi(t0, t0, rB(ctx->opcode));                                         \
8539     tcg_gen_shri_i64(t2, cpu_gpr[rA(ctx->opcode)], 32);                       \
8540     tcg_gen_trunc_i64_i32(t1, t2);                                            \
8541     tcg_temp_free_i64(t2);                                                    \
8542     tcg_opi(t1, t1, rB(ctx->opcode));                                         \
8543     tcg_gen_concat_i32_i64(cpu_gpr[rD(ctx->opcode)], t0, t1);                 \
8544     tcg_temp_free_i32(t0);                                                    \
8545     tcg_temp_free_i32(t1);                                                    \
8546 }
8547 #else
8548 #define GEN_SPEOP_TCG_LOGIC_IMM2(name, tcg_opi)                               \
8549 static inline void gen_##name(DisasContext *ctx)                              \
8550 {                                                                             \
8551     if (unlikely(!ctx->spe_enabled)) {                                        \
8552         gen_exception(ctx, POWERPC_EXCP_SPEU);                                \
8553         return;                                                               \
8554     }                                                                         \
8555     tcg_opi(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)],               \
8556             rB(ctx->opcode));                                                 \
8557     tcg_opi(cpu_gprh[rD(ctx->opcode)], cpu_gprh[rA(ctx->opcode)],             \
8558             rB(ctx->opcode));                                                 \
8559 }
8560 #endif
8561 GEN_SPEOP_TCG_LOGIC_IMM2(evslwi, tcg_gen_shli_i32);
8562 GEN_SPEOP_TCG_LOGIC_IMM2(evsrwiu, tcg_gen_shri_i32);
8563 GEN_SPEOP_TCG_LOGIC_IMM2(evsrwis, tcg_gen_sari_i32);
8564 GEN_SPEOP_TCG_LOGIC_IMM2(evrlwi, tcg_gen_rotli_i32);
8565
8566 /* SPE arithmetic */
8567 #if defined(TARGET_PPC64)
8568 #define GEN_SPEOP_ARITH1(name, tcg_op)                                        \
8569 static inline void gen_##name(DisasContext *ctx)                              \
8570 {                                                                             \
8571     if (unlikely(!ctx->spe_enabled)) {                                        \
8572         gen_exception(ctx, POWERPC_EXCP_SPEU);                                \
8573         return;                                                               \
8574     }                                                                         \
8575     TCGv_i32 t0 = tcg_temp_local_new_i32();                                   \
8576     TCGv_i32 t1 = tcg_temp_local_new_i32();                                   \
8577     TCGv_i64 t2 = tcg_temp_local_new_i64();                                   \
8578     tcg_gen_trunc_i64_i32(t0, cpu_gpr[rA(ctx->opcode)]);                      \
8579     tcg_op(t0, t0);                                                           \
8580     tcg_gen_shri_i64(t2, cpu_gpr[rA(ctx->opcode)], 32);                       \
8581     tcg_gen_trunc_i64_i32(t1, t2);                                            \
8582     tcg_temp_free_i64(t2);                                                    \
8583     tcg_op(t1, t1);                                                           \
8584     tcg_gen_concat_i32_i64(cpu_gpr[rD(ctx->opcode)], t0, t1);                 \
8585     tcg_temp_free_i32(t0);                                                    \
8586     tcg_temp_free_i32(t1);                                                    \
8587 }
8588 #else
8589 #define GEN_SPEOP_ARITH1(name, tcg_op)                                        \
8590 static inline void gen_##name(DisasContext *ctx)                              \
8591 {                                                                             \
8592     if (unlikely(!ctx->spe_enabled)) {                                        \
8593         gen_exception(ctx, POWERPC_EXCP_SPEU);                                \
8594         return;                                                               \
8595     }                                                                         \
8596     tcg_op(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)]);               \
8597     tcg_op(cpu_gprh[rD(ctx->opcode)], cpu_gprh[rA(ctx->opcode)]);             \
8598 }
8599 #endif
8600
8601 static inline void gen_op_evabs(TCGv_i32 ret, TCGv_i32 arg1)
8602 {
8603     int l1 = gen_new_label();
8604     int l2 = gen_new_label();
8605
8606     tcg_gen_brcondi_i32(TCG_COND_GE, arg1, 0, l1);
8607     tcg_gen_neg_i32(ret, arg1);
8608     tcg_gen_br(l2);
8609     gen_set_label(l1);
8610     tcg_gen_mov_i32(ret, arg1);
8611     gen_set_label(l2);
8612 }
8613 GEN_SPEOP_ARITH1(evabs, gen_op_evabs);
8614 GEN_SPEOP_ARITH1(evneg, tcg_gen_neg_i32);
8615 GEN_SPEOP_ARITH1(evextsb, tcg_gen_ext8s_i32);
8616 GEN_SPEOP_ARITH1(evextsh, tcg_gen_ext16s_i32);
8617 static inline void gen_op_evrndw(TCGv_i32 ret, TCGv_i32 arg1)
8618 {
8619     tcg_gen_addi_i32(ret, arg1, 0x8000);
8620     tcg_gen_ext16u_i32(ret, ret);
8621 }
8622 GEN_SPEOP_ARITH1(evrndw, gen_op_evrndw);
8623 GEN_SPEOP_ARITH1(evcntlsw, gen_helper_cntlsw32);
8624 GEN_SPEOP_ARITH1(evcntlzw, gen_helper_cntlzw32);
8625
8626 #if defined(TARGET_PPC64)
8627 #define GEN_SPEOP_ARITH2(name, tcg_op)                                        \
8628 static inline void gen_##name(DisasContext *ctx)                              \
8629 {                                                                             \
8630     if (unlikely(!ctx->spe_enabled)) {                                        \
8631         gen_exception(ctx, POWERPC_EXCP_SPEU);                                \
8632         return;                                                               \
8633     }                                                                         \
8634     TCGv_i32 t0 = tcg_temp_local_new_i32();                                   \
8635     TCGv_i32 t1 = tcg_temp_local_new_i32();                                   \
8636     TCGv_i32 t2 = tcg_temp_local_new_i32();                                   \
8637     TCGv_i64 t3 = tcg_temp_local_new_i64();                                   \
8638     tcg_gen_trunc_i64_i32(t0, cpu_gpr[rA(ctx->opcode)]);                      \
8639     tcg_gen_trunc_i64_i32(t2, cpu_gpr[rB(ctx->opcode)]);                      \
8640     tcg_op(t0, t0, t2);                                                       \
8641     tcg_gen_shri_i64(t3, cpu_gpr[rA(ctx->opcode)], 32);                       \
8642     tcg_gen_trunc_i64_i32(t1, t3);                                            \
8643     tcg_gen_shri_i64(t3, cpu_gpr[rB(ctx->opcode)], 32);                       \
8644     tcg_gen_trunc_i64_i32(t2, t3);                                            \
8645     tcg_temp_free_i64(t3);                                                    \
8646     tcg_op(t1, t1, t2);                                                       \
8647     tcg_temp_free_i32(t2);                                                    \
8648     tcg_gen_concat_i32_i64(cpu_gpr[rD(ctx->opcode)], t0, t1);                 \
8649     tcg_temp_free_i32(t0);                                                    \
8650     tcg_temp_free_i32(t1);                                                    \
8651 }
8652 #else
8653 #define GEN_SPEOP_ARITH2(name, tcg_op)                                        \
8654 static inline void gen_##name(DisasContext *ctx)                              \
8655 {                                                                             \
8656     if (unlikely(!ctx->spe_enabled)) {                                        \
8657         gen_exception(ctx, POWERPC_EXCP_SPEU);                                \
8658         return;                                                               \
8659     }                                                                         \
8660     tcg_op(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)],                \
8661            cpu_gpr[rB(ctx->opcode)]);                                         \
8662     tcg_op(cpu_gprh[rD(ctx->opcode)], cpu_gprh[rA(ctx->opcode)],              \
8663            cpu_gprh[rB(ctx->opcode)]);                                        \
8664 }
8665 #endif
8666
8667 static inline void gen_op_evsrwu(TCGv_i32 ret, TCGv_i32 arg1, TCGv_i32 arg2)
8668 {
8669     TCGv_i32 t0;
8670     int l1, l2;
8671
8672     l1 = gen_new_label();
8673     l2 = gen_new_label();
8674     t0 = tcg_temp_local_new_i32();
8675     /* No error here: 6 bits are used */
8676     tcg_gen_andi_i32(t0, arg2, 0x3F);
8677     tcg_gen_brcondi_i32(TCG_COND_GE, t0, 32, l1);
8678     tcg_gen_shr_i32(ret, arg1, t0);
8679     tcg_gen_br(l2);
8680     gen_set_label(l1);
8681     tcg_gen_movi_i32(ret, 0);
8682     gen_set_label(l2);
8683     tcg_temp_free_i32(t0);
8684 }
8685 GEN_SPEOP_ARITH2(evsrwu, gen_op_evsrwu);
8686 static inline void gen_op_evsrws(TCGv_i32 ret, TCGv_i32 arg1, TCGv_i32 arg2)
8687 {
8688     TCGv_i32 t0;
8689     int l1, l2;
8690
8691     l1 = gen_new_label();
8692     l2 = gen_new_label();
8693     t0 = tcg_temp_local_new_i32();
8694     /* No error here: 6 bits are used */
8695     tcg_gen_andi_i32(t0, arg2, 0x3F);
8696     tcg_gen_brcondi_i32(TCG_COND_GE, t0, 32, l1);
8697     tcg_gen_sar_i32(ret, arg1, t0);
8698     tcg_gen_br(l2);
8699     gen_set_label(l1);
8700     tcg_gen_movi_i32(ret, 0);
8701     gen_set_label(l2);
8702     tcg_temp_free_i32(t0);
8703 }
8704 GEN_SPEOP_ARITH2(evsrws, gen_op_evsrws);
8705 static inline void gen_op_evslw(TCGv_i32 ret, TCGv_i32 arg1, TCGv_i32 arg2)
8706 {
8707     TCGv_i32 t0;
8708     int l1, l2;
8709
8710     l1 = gen_new_label();
8711     l2 = gen_new_label();
8712     t0 = tcg_temp_local_new_i32();
8713     /* No error here: 6 bits are used */
8714     tcg_gen_andi_i32(t0, arg2, 0x3F);
8715     tcg_gen_brcondi_i32(TCG_COND_GE, t0, 32, l1);
8716     tcg_gen_shl_i32(ret, arg1, t0);
8717     tcg_gen_br(l2);
8718     gen_set_label(l1);
8719     tcg_gen_movi_i32(ret, 0);
8720     gen_set_label(l2);
8721     tcg_temp_free_i32(t0);
8722 }
8723 GEN_SPEOP_ARITH2(evslw, gen_op_evslw);
8724 static inline void gen_op_evrlw(TCGv_i32 ret, TCGv_i32 arg1, TCGv_i32 arg2)
8725 {
8726     TCGv_i32 t0 = tcg_temp_new_i32();
8727     tcg_gen_andi_i32(t0, arg2, 0x1F);
8728     tcg_gen_rotl_i32(ret, arg1, t0);
8729     tcg_temp_free_i32(t0);
8730 }
8731 GEN_SPEOP_ARITH2(evrlw, gen_op_evrlw);
8732 static inline void gen_evmergehi(DisasContext *ctx)
8733 {
8734     if (unlikely(!ctx->spe_enabled)) {
8735         gen_exception(ctx, POWERPC_EXCP_SPEU);
8736         return;
8737     }
8738 #if defined(TARGET_PPC64)
8739     TCGv t0 = tcg_temp_new();
8740     TCGv t1 = tcg_temp_new();
8741     tcg_gen_shri_tl(t0, cpu_gpr[rB(ctx->opcode)], 32);
8742     tcg_gen_andi_tl(t1, cpu_gpr[rA(ctx->opcode)], 0xFFFFFFFF0000000ULL);
8743     tcg_gen_or_tl(cpu_gpr[rD(ctx->opcode)], t0, t1);
8744     tcg_temp_free(t0);
8745     tcg_temp_free(t1);
8746 #else
8747     tcg_gen_mov_i32(cpu_gpr[rD(ctx->opcode)], cpu_gprh[rB(ctx->opcode)]);
8748     tcg_gen_mov_i32(cpu_gprh[rD(ctx->opcode)], cpu_gprh[rA(ctx->opcode)]);
8749 #endif
8750 }
8751 GEN_SPEOP_ARITH2(evaddw, tcg_gen_add_i32);
8752 static inline void gen_op_evsubf(TCGv_i32 ret, TCGv_i32 arg1, TCGv_i32 arg2)
8753 {
8754     tcg_gen_sub_i32(ret, arg2, arg1);
8755 }
8756 GEN_SPEOP_ARITH2(evsubfw, gen_op_evsubf);
8757
8758 /* SPE arithmetic immediate */
8759 #if defined(TARGET_PPC64)
8760 #define GEN_SPEOP_ARITH_IMM2(name, tcg_op)                                    \
8761 static inline void gen_##name(DisasContext *ctx)                              \
8762 {                                                                             \
8763     if (unlikely(!ctx->spe_enabled)) {                                        \
8764         gen_exception(ctx, POWERPC_EXCP_SPEU);                                \
8765         return;                                                               \
8766     }                                                                         \
8767     TCGv_i32 t0 = tcg_temp_local_new_i32();                                   \
8768     TCGv_i32 t1 = tcg_temp_local_new_i32();                                   \
8769     TCGv_i64 t2 = tcg_temp_local_new_i64();                                   \
8770     tcg_gen_trunc_i64_i32(t0, cpu_gpr[rB(ctx->opcode)]);                      \
8771     tcg_op(t0, t0, rA(ctx->opcode));                                          \
8772     tcg_gen_shri_i64(t2, cpu_gpr[rB(ctx->opcode)], 32);                       \
8773     tcg_gen_trunc_i64_i32(t1, t2);                                            \
8774     tcg_temp_free_i64(t2);                                                    \
8775     tcg_op(t1, t1, rA(ctx->opcode));                                          \
8776     tcg_gen_concat_i32_i64(cpu_gpr[rD(ctx->opcode)], t0, t1);                 \
8777     tcg_temp_free_i32(t0);                                                    \
8778     tcg_temp_free_i32(t1);                                                    \
8779 }
8780 #else
8781 #define GEN_SPEOP_ARITH_IMM2(name, tcg_op)                                    \
8782 static inline void gen_##name(DisasContext *ctx)                              \
8783 {                                                                             \
8784     if (unlikely(!ctx->spe_enabled)) {                                        \
8785         gen_exception(ctx, POWERPC_EXCP_SPEU);                                \
8786         return;                                                               \
8787     }                                                                         \
8788     tcg_op(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rB(ctx->opcode)],                \
8789            rA(ctx->opcode));                                                  \
8790     tcg_op(cpu_gprh[rD(ctx->opcode)], cpu_gprh[rB(ctx->opcode)],              \
8791            rA(ctx->opcode));                                                  \
8792 }
8793 #endif
8794 GEN_SPEOP_ARITH_IMM2(evaddiw, tcg_gen_addi_i32);
8795 GEN_SPEOP_ARITH_IMM2(evsubifw, tcg_gen_subi_i32);
8796
8797 /* SPE comparison */
8798 #if defined(TARGET_PPC64)
8799 #define GEN_SPEOP_COMP(name, tcg_cond)                                        \
8800 static inline void gen_##name(DisasContext *ctx)                              \
8801 {                                                                             \
8802     if (unlikely(!ctx->spe_enabled)) {                                        \
8803         gen_exception(ctx, POWERPC_EXCP_SPEU);                                \
8804         return;                                                               \
8805     }                                                                         \
8806     int l1 = gen_new_label();                                                 \
8807     int l2 = gen_new_label();                                                 \
8808     int l3 = gen_new_label();                                                 \
8809     int l4 = gen_new_label();                                                 \
8810     TCGv_i32 t0 = tcg_temp_local_new_i32();                                   \
8811     TCGv_i32 t1 = tcg_temp_local_new_i32();                                   \
8812     TCGv_i64 t2 = tcg_temp_local_new_i64();                                   \
8813     tcg_gen_trunc_i64_i32(t0, cpu_gpr[rA(ctx->opcode)]);                      \
8814     tcg_gen_trunc_i64_i32(t1, cpu_gpr[rB(ctx->opcode)]);                      \
8815     tcg_gen_brcond_i32(tcg_cond, t0, t1, l1);                                 \
8816     tcg_gen_movi_i32(cpu_crf[crfD(ctx->opcode)], 0);                          \
8817     tcg_gen_br(l2);                                                           \
8818     gen_set_label(l1);                                                        \
8819     tcg_gen_movi_i32(cpu_crf[crfD(ctx->opcode)],                              \
8820                      CRF_CL | CRF_CH_OR_CL | CRF_CH_AND_CL);                  \
8821     gen_set_label(l2);                                                        \
8822     tcg_gen_shri_i64(t2, cpu_gpr[rA(ctx->opcode)], 32);                       \
8823     tcg_gen_trunc_i64_i32(t0, t2);                                            \
8824     tcg_gen_shri_i64(t2, cpu_gpr[rB(ctx->opcode)], 32);                       \
8825     tcg_gen_trunc_i64_i32(t1, t2);                                            \
8826     tcg_temp_free_i64(t2);                                                    \
8827     tcg_gen_brcond_i32(tcg_cond, t0, t1, l3);                                 \
8828     tcg_gen_andi_i32(cpu_crf[crfD(ctx->opcode)], cpu_crf[crfD(ctx->opcode)],  \
8829                      ~(CRF_CH | CRF_CH_AND_CL));                              \
8830     tcg_gen_br(l4);                                                           \
8831     gen_set_label(l3);                                                        \
8832     tcg_gen_ori_i32(cpu_crf[crfD(ctx->opcode)], cpu_crf[crfD(ctx->opcode)],   \
8833                     CRF_CH | CRF_CH_OR_CL);                                   \
8834     gen_set_label(l4);                                                        \
8835     tcg_temp_free_i32(t0);                                                    \
8836     tcg_temp_free_i32(t1);                                                    \
8837 }
8838 #else
8839 #define GEN_SPEOP_COMP(name, tcg_cond)                                        \
8840 static inline void gen_##name(DisasContext *ctx)                              \
8841 {                                                                             \
8842     if (unlikely(!ctx->spe_enabled)) {                                        \
8843         gen_exception(ctx, POWERPC_EXCP_SPEU);                                \
8844         return;                                                               \
8845     }                                                                         \
8846     int l1 = gen_new_label();                                                 \
8847     int l2 = gen_new_label();                                                 \
8848     int l3 = gen_new_label();                                                 \
8849     int l4 = gen_new_label();                                                 \
8850                                                                               \
8851     tcg_gen_brcond_i32(tcg_cond, cpu_gpr[rA(ctx->opcode)],                    \
8852                        cpu_gpr[rB(ctx->opcode)], l1);                         \
8853     tcg_gen_movi_tl(cpu_crf[crfD(ctx->opcode)], 0);                           \
8854     tcg_gen_br(l2);                                                           \
8855     gen_set_label(l1);                                                        \
8856     tcg_gen_movi_i32(cpu_crf[crfD(ctx->opcode)],                              \
8857                      CRF_CL | CRF_CH_OR_CL | CRF_CH_AND_CL);                  \
8858     gen_set_label(l2);                                                        \
8859     tcg_gen_brcond_i32(tcg_cond, cpu_gprh[rA(ctx->opcode)],                   \
8860                        cpu_gprh[rB(ctx->opcode)], l3);                        \
8861     tcg_gen_andi_i32(cpu_crf[crfD(ctx->opcode)], cpu_crf[crfD(ctx->opcode)],  \
8862                      ~(CRF_CH | CRF_CH_AND_CL));                              \
8863     tcg_gen_br(l4);                                                           \
8864     gen_set_label(l3);                                                        \
8865     tcg_gen_ori_i32(cpu_crf[crfD(ctx->opcode)], cpu_crf[crfD(ctx->opcode)],   \
8866                     CRF_CH | CRF_CH_OR_CL);                                   \
8867     gen_set_label(l4);                                                        \
8868 }
8869 #endif
8870 GEN_SPEOP_COMP(evcmpgtu, TCG_COND_GTU);
8871 GEN_SPEOP_COMP(evcmpgts, TCG_COND_GT);
8872 GEN_SPEOP_COMP(evcmpltu, TCG_COND_LTU);
8873 GEN_SPEOP_COMP(evcmplts, TCG_COND_LT);
8874 GEN_SPEOP_COMP(evcmpeq, TCG_COND_EQ);
8875
8876 /* SPE misc */
8877 static inline void gen_brinc(DisasContext *ctx)
8878 {
8879     /* Note: brinc is usable even if SPE is disabled */
8880     gen_helper_brinc(cpu_gpr[rD(ctx->opcode)],
8881                      cpu_gpr[rA(ctx->opcode)], cpu_gpr[rB(ctx->opcode)]);
8882 }
8883 static inline void gen_evmergelo(DisasContext *ctx)
8884 {
8885     if (unlikely(!ctx->spe_enabled)) {
8886         gen_exception(ctx, POWERPC_EXCP_SPEU);
8887         return;
8888     }
8889 #if defined(TARGET_PPC64)
8890     TCGv t0 = tcg_temp_new();
8891     TCGv t1 = tcg_temp_new();
8892     tcg_gen_ext32u_tl(t0, cpu_gpr[rB(ctx->opcode)]);
8893     tcg_gen_shli_tl(t1, cpu_gpr[rA(ctx->opcode)], 32);
8894     tcg_gen_or_tl(cpu_gpr[rD(ctx->opcode)], t0, t1);
8895     tcg_temp_free(t0);
8896     tcg_temp_free(t1);
8897 #else
8898     tcg_gen_mov_i32(cpu_gprh[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)]);
8899     tcg_gen_mov_i32(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rB(ctx->opcode)]);
8900 #endif
8901 }
8902 static inline void gen_evmergehilo(DisasContext *ctx)
8903 {
8904     if (unlikely(!ctx->spe_enabled)) {
8905         gen_exception(ctx, POWERPC_EXCP_SPEU);
8906         return;
8907     }
8908 #if defined(TARGET_PPC64)
8909     TCGv t0 = tcg_temp_new();
8910     TCGv t1 = tcg_temp_new();
8911     tcg_gen_ext32u_tl(t0, cpu_gpr[rB(ctx->opcode)]);
8912     tcg_gen_andi_tl(t1, cpu_gpr[rA(ctx->opcode)], 0xFFFFFFFF0000000ULL);
8913     tcg_gen_or_tl(cpu_gpr[rD(ctx->opcode)], t0, t1);
8914     tcg_temp_free(t0);
8915     tcg_temp_free(t1);
8916 #else
8917     tcg_gen_mov_i32(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rB(ctx->opcode)]);
8918     tcg_gen_mov_i32(cpu_gprh[rD(ctx->opcode)], cpu_gprh[rA(ctx->opcode)]);
8919 #endif
8920 }
8921 static inline void gen_evmergelohi(DisasContext *ctx)
8922 {
8923     if (unlikely(!ctx->spe_enabled)) {
8924         gen_exception(ctx, POWERPC_EXCP_SPEU);
8925         return;
8926     }
8927 #if defined(TARGET_PPC64)
8928     TCGv t0 = tcg_temp_new();
8929     TCGv t1 = tcg_temp_new();
8930     tcg_gen_shri_tl(t0, cpu_gpr[rB(ctx->opcode)], 32);
8931     tcg_gen_shli_tl(t1, cpu_gpr[rA(ctx->opcode)], 32);
8932     tcg_gen_or_tl(cpu_gpr[rD(ctx->opcode)], t0, t1);
8933     tcg_temp_free(t0);
8934     tcg_temp_free(t1);
8935 #else
8936     if (rD(ctx->opcode) == rA(ctx->opcode)) {
8937         TCGv_i32 tmp = tcg_temp_new_i32();
8938         tcg_gen_mov_i32(tmp, cpu_gpr[rA(ctx->opcode)]);
8939         tcg_gen_mov_i32(cpu_gpr[rD(ctx->opcode)], cpu_gprh[rB(ctx->opcode)]);
8940         tcg_gen_mov_i32(cpu_gprh[rD(ctx->opcode)], tmp);
8941         tcg_temp_free_i32(tmp);
8942     } else {
8943         tcg_gen_mov_i32(cpu_gpr[rD(ctx->opcode)], cpu_gprh[rB(ctx->opcode)]);
8944         tcg_gen_mov_i32(cpu_gprh[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)]);
8945     }
8946 #endif
8947 }
8948 static inline void gen_evsplati(DisasContext *ctx)
8949 {
8950     uint64_t imm = ((int32_t)(rA(ctx->opcode) << 27)) >> 27;
8951
8952 #if defined(TARGET_PPC64)
8953     tcg_gen_movi_tl(cpu_gpr[rD(ctx->opcode)], (imm << 32) | imm);
8954 #else
8955     tcg_gen_movi_i32(cpu_gpr[rD(ctx->opcode)], imm);
8956     tcg_gen_movi_i32(cpu_gprh[rD(ctx->opcode)], imm);
8957 #endif
8958 }
8959 static inline void gen_evsplatfi(DisasContext *ctx)
8960 {
8961     uint64_t imm = rA(ctx->opcode) << 27;
8962
8963 #if defined(TARGET_PPC64)
8964     tcg_gen_movi_tl(cpu_gpr[rD(ctx->opcode)], (imm << 32) | imm);
8965 #else
8966     tcg_gen_movi_i32(cpu_gpr[rD(ctx->opcode)], imm);
8967     tcg_gen_movi_i32(cpu_gprh[rD(ctx->opcode)], imm);
8968 #endif
8969 }
8970
8971 static inline void gen_evsel(DisasContext *ctx)
8972 {
8973     int l1 = gen_new_label();
8974     int l2 = gen_new_label();
8975     int l3 = gen_new_label();
8976     int l4 = gen_new_label();
8977     TCGv_i32 t0 = tcg_temp_local_new_i32();
8978 #if defined(TARGET_PPC64)
8979     TCGv t1 = tcg_temp_local_new();
8980     TCGv t2 = tcg_temp_local_new();
8981 #endif
8982     tcg_gen_andi_i32(t0, cpu_crf[ctx->opcode & 0x07], 1 << 3);
8983     tcg_gen_brcondi_i32(TCG_COND_EQ, t0, 0, l1);
8984 #if defined(TARGET_PPC64)
8985     tcg_gen_andi_tl(t1, cpu_gpr[rA(ctx->opcode)], 0xFFFFFFFF00000000ULL);
8986 #else
8987     tcg_gen_mov_tl(cpu_gprh[rD(ctx->opcode)], cpu_gprh[rA(ctx->opcode)]);
8988 #endif
8989     tcg_gen_br(l2);
8990     gen_set_label(l1);
8991 #if defined(TARGET_PPC64)
8992     tcg_gen_andi_tl(t1, cpu_gpr[rB(ctx->opcode)], 0xFFFFFFFF00000000ULL);
8993 #else
8994     tcg_gen_mov_tl(cpu_gprh[rD(ctx->opcode)], cpu_gprh[rB(ctx->opcode)]);
8995 #endif
8996     gen_set_label(l2);
8997     tcg_gen_andi_i32(t0, cpu_crf[ctx->opcode & 0x07], 1 << 2);
8998     tcg_gen_brcondi_i32(TCG_COND_EQ, t0, 0, l3);
8999 #if defined(TARGET_PPC64)
9000     tcg_gen_ext32u_tl(t2, cpu_gpr[rA(ctx->opcode)]);
9001 #else
9002     tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)]);
9003 #endif
9004     tcg_gen_br(l4);
9005     gen_set_label(l3);
9006 #if defined(TARGET_PPC64)
9007     tcg_gen_ext32u_tl(t2, cpu_gpr[rB(ctx->opcode)]);
9008 #else
9009     tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rB(ctx->opcode)]);
9010 #endif
9011     gen_set_label(l4);
9012     tcg_temp_free_i32(t0);
9013 #if defined(TARGET_PPC64)
9014     tcg_gen_or_tl(cpu_gpr[rD(ctx->opcode)], t1, t2);
9015     tcg_temp_free(t1);
9016     tcg_temp_free(t2);
9017 #endif
9018 }
9019
9020 static void gen_evsel0(DisasContext *ctx)
9021 {
9022     gen_evsel(ctx);
9023 }
9024
9025 static void gen_evsel1(DisasContext *ctx)
9026 {
9027     gen_evsel(ctx);
9028 }
9029
9030 static void gen_evsel2(DisasContext *ctx)
9031 {
9032     gen_evsel(ctx);
9033 }
9034
9035 static void gen_evsel3(DisasContext *ctx)
9036 {
9037     gen_evsel(ctx);
9038 }
9039
9040 /* Multiply */
9041
9042 static inline void gen_evmwumi(DisasContext *ctx)
9043 {
9044     TCGv_i64 t0, t1;
9045
9046     if (unlikely(!ctx->spe_enabled)) {
9047         gen_exception(ctx, POWERPC_EXCP_SPEU);
9048         return;
9049     }
9050
9051     t0 = tcg_temp_new_i64();
9052     t1 = tcg_temp_new_i64();
9053
9054     /* t0 := rA; t1 := rB */
9055 #if defined(TARGET_PPC64)
9056     tcg_gen_ext32u_tl(t0, cpu_gpr[rA(ctx->opcode)]);
9057     tcg_gen_ext32u_tl(t1, cpu_gpr[rB(ctx->opcode)]);
9058 #else
9059     tcg_gen_extu_tl_i64(t0, cpu_gpr[rA(ctx->opcode)]);
9060     tcg_gen_extu_tl_i64(t1, cpu_gpr[rB(ctx->opcode)]);
9061 #endif
9062
9063     tcg_gen_mul_i64(t0, t0, t1);  /* t0 := rA * rB */
9064
9065     gen_store_gpr64(rD(ctx->opcode), t0); /* rD := t0 */
9066
9067     tcg_temp_free_i64(t0);
9068     tcg_temp_free_i64(t1);
9069 }
9070
9071 static inline void gen_evmwumia(DisasContext *ctx)
9072 {
9073     TCGv_i64 tmp;
9074
9075     if (unlikely(!ctx->spe_enabled)) {
9076         gen_exception(ctx, POWERPC_EXCP_SPEU);
9077         return;
9078     }
9079
9080     gen_evmwumi(ctx);            /* rD := rA * rB */
9081
9082     tmp = tcg_temp_new_i64();
9083
9084     /* acc := rD */
9085     gen_load_gpr64(tmp, rD(ctx->opcode));
9086     tcg_gen_st_i64(tmp, cpu_env, offsetof(CPUPPCState, spe_acc));
9087     tcg_temp_free_i64(tmp);
9088 }
9089
9090 static inline void gen_evmwumiaa(DisasContext *ctx)
9091 {
9092     TCGv_i64 acc;
9093     TCGv_i64 tmp;
9094
9095     if (unlikely(!ctx->spe_enabled)) {
9096         gen_exception(ctx, POWERPC_EXCP_SPEU);
9097         return;
9098     }
9099
9100     gen_evmwumi(ctx);           /* rD := rA * rB */
9101
9102     acc = tcg_temp_new_i64();
9103     tmp = tcg_temp_new_i64();
9104
9105     /* tmp := rD */
9106     gen_load_gpr64(tmp, rD(ctx->opcode));
9107
9108     /* Load acc */
9109     tcg_gen_ld_i64(acc, cpu_env, offsetof(CPUPPCState, spe_acc));
9110
9111     /* acc := tmp + acc */
9112     tcg_gen_add_i64(acc, acc, tmp);
9113
9114     /* Store acc */
9115     tcg_gen_st_i64(acc, cpu_env, offsetof(CPUPPCState, spe_acc));
9116
9117     /* rD := acc */
9118     gen_store_gpr64(rD(ctx->opcode), acc);
9119
9120     tcg_temp_free_i64(acc);
9121     tcg_temp_free_i64(tmp);
9122 }
9123
9124 static inline void gen_evmwsmi(DisasContext *ctx)
9125 {
9126     TCGv_i64 t0, t1;
9127
9128     if (unlikely(!ctx->spe_enabled)) {
9129         gen_exception(ctx, POWERPC_EXCP_SPEU);
9130         return;
9131     }
9132
9133     t0 = tcg_temp_new_i64();
9134     t1 = tcg_temp_new_i64();
9135
9136     /* t0 := rA; t1 := rB */
9137 #if defined(TARGET_PPC64)
9138     tcg_gen_ext32s_tl(t0, cpu_gpr[rA(ctx->opcode)]);
9139     tcg_gen_ext32s_tl(t1, cpu_gpr[rB(ctx->opcode)]);
9140 #else
9141     tcg_gen_ext_tl_i64(t0, cpu_gpr[rA(ctx->opcode)]);
9142     tcg_gen_ext_tl_i64(t1, cpu_gpr[rB(ctx->opcode)]);
9143 #endif
9144
9145     tcg_gen_mul_i64(t0, t0, t1);  /* t0 := rA * rB */
9146
9147     gen_store_gpr64(rD(ctx->opcode), t0); /* rD := t0 */
9148
9149     tcg_temp_free_i64(t0);
9150     tcg_temp_free_i64(t1);
9151 }
9152
9153 static inline void gen_evmwsmia(DisasContext *ctx)
9154 {
9155     TCGv_i64 tmp;
9156
9157     gen_evmwsmi(ctx);            /* rD := rA * rB */
9158
9159     tmp = tcg_temp_new_i64();
9160
9161     /* acc := rD */
9162     gen_load_gpr64(tmp, rD(ctx->opcode));
9163     tcg_gen_st_i64(tmp, cpu_env, offsetof(CPUPPCState, spe_acc));
9164
9165     tcg_temp_free_i64(tmp);
9166 }
9167
9168 static inline void gen_evmwsmiaa(DisasContext *ctx)
9169 {
9170     TCGv_i64 acc = tcg_temp_new_i64();
9171     TCGv_i64 tmp = tcg_temp_new_i64();
9172
9173     gen_evmwsmi(ctx);           /* rD := rA * rB */
9174
9175     acc = tcg_temp_new_i64();
9176     tmp = tcg_temp_new_i64();
9177
9178     /* tmp := rD */
9179     gen_load_gpr64(tmp, rD(ctx->opcode));
9180
9181     /* Load acc */
9182     tcg_gen_ld_i64(acc, cpu_env, offsetof(CPUPPCState, spe_acc));
9183
9184     /* acc := tmp + acc */
9185     tcg_gen_add_i64(acc, acc, tmp);
9186
9187     /* Store acc */
9188     tcg_gen_st_i64(acc, cpu_env, offsetof(CPUPPCState, spe_acc));
9189
9190     /* rD := acc */
9191     gen_store_gpr64(rD(ctx->opcode), acc);
9192
9193     tcg_temp_free_i64(acc);
9194     tcg_temp_free_i64(tmp);
9195 }
9196
9197 GEN_SPE(evaddw,      speundef,    0x00, 0x08, 0x00000000, 0xFFFFFFFF, PPC_SPE); ////
9198 GEN_SPE(evaddiw,     speundef,    0x01, 0x08, 0x00000000, 0xFFFFFFFF, PPC_SPE);
9199 GEN_SPE(evsubfw,     speundef,    0x02, 0x08, 0x00000000, 0xFFFFFFFF, PPC_SPE); ////
9200 GEN_SPE(evsubifw,    speundef,    0x03, 0x08, 0x00000000, 0xFFFFFFFF, PPC_SPE);
9201 GEN_SPE(evabs,       evneg,       0x04, 0x08, 0x0000F800, 0x0000F800, PPC_SPE); ////
9202 GEN_SPE(evextsb,     evextsh,     0x05, 0x08, 0x0000F800, 0x0000F800, PPC_SPE); ////
9203 GEN_SPE(evrndw,      evcntlzw,    0x06, 0x08, 0x0000F800, 0x0000F800, PPC_SPE); ////
9204 GEN_SPE(evcntlsw,    brinc,       0x07, 0x08, 0x0000F800, 0x00000000, PPC_SPE); //
9205 GEN_SPE(evmra,       speundef,    0x02, 0x13, 0x0000F800, 0xFFFFFFFF, PPC_SPE);
9206 GEN_SPE(speundef,    evand,       0x08, 0x08, 0xFFFFFFFF, 0x00000000, PPC_SPE); ////
9207 GEN_SPE(evandc,      speundef,    0x09, 0x08, 0x00000000, 0xFFFFFFFF, PPC_SPE); ////
9208 GEN_SPE(evxor,       evor,        0x0B, 0x08, 0x00000000, 0x00000000, PPC_SPE); ////
9209 GEN_SPE(evnor,       eveqv,       0x0C, 0x08, 0x00000000, 0x00000000, PPC_SPE); ////
9210 GEN_SPE(evmwumi,     evmwsmi,     0x0C, 0x11, 0x00000000, 0x00000000, PPC_SPE);
9211 GEN_SPE(evmwumia,    evmwsmia,    0x1C, 0x11, 0x00000000, 0x00000000, PPC_SPE);
9212 GEN_SPE(evmwumiaa,   evmwsmiaa,   0x0C, 0x15, 0x00000000, 0x00000000, PPC_SPE);
9213 GEN_SPE(speundef,    evorc,       0x0D, 0x08, 0xFFFFFFFF, 0x00000000, PPC_SPE); ////
9214 GEN_SPE(evnand,      speundef,    0x0F, 0x08, 0x00000000, 0xFFFFFFFF, PPC_SPE); ////
9215 GEN_SPE(evsrwu,      evsrws,      0x10, 0x08, 0x00000000, 0x00000000, PPC_SPE); ////
9216 GEN_SPE(evsrwiu,     evsrwis,     0x11, 0x08, 0x00000000, 0x00000000, PPC_SPE);
9217 GEN_SPE(evslw,       speundef,    0x12, 0x08, 0x00000000, 0xFFFFFFFF, PPC_SPE); ////
9218 GEN_SPE(evslwi,      speundef,    0x13, 0x08, 0x00000000, 0xFFFFFFFF, PPC_SPE);
9219 GEN_SPE(evrlw,       evsplati,    0x14, 0x08, 0x00000000, 0x0000F800, PPC_SPE); //
9220 GEN_SPE(evrlwi,      evsplatfi,   0x15, 0x08, 0x00000000, 0x0000F800, PPC_SPE);
9221 GEN_SPE(evmergehi,   evmergelo,   0x16, 0x08, 0x00000000, 0x00000000, PPC_SPE); ////
9222 GEN_SPE(evmergehilo, evmergelohi, 0x17, 0x08, 0x00000000, 0x00000000, PPC_SPE); ////
9223 GEN_SPE(evcmpgtu,    evcmpgts,    0x18, 0x08, 0x00600000, 0x00600000, PPC_SPE); ////
9224 GEN_SPE(evcmpltu,    evcmplts,    0x19, 0x08, 0x00600000, 0x00600000, PPC_SPE); ////
9225 GEN_SPE(evcmpeq,     speundef,    0x1A, 0x08, 0x00600000, 0xFFFFFFFF, PPC_SPE); ////
9226
9227 /* SPE load and stores */
9228 static inline void gen_addr_spe_imm_index(DisasContext *ctx, TCGv EA, int sh)
9229 {
9230     target_ulong uimm = rB(ctx->opcode);
9231
9232     if (rA(ctx->opcode) == 0) {
9233         tcg_gen_movi_tl(EA, uimm << sh);
9234     } else {
9235         tcg_gen_addi_tl(EA, cpu_gpr[rA(ctx->opcode)], uimm << sh);
9236         if (NARROW_MODE(ctx)) {
9237             tcg_gen_ext32u_tl(EA, EA);
9238         }
9239     }
9240 }
9241
9242 static inline void gen_op_evldd(DisasContext *ctx, TCGv addr)
9243 {
9244 #if defined(TARGET_PPC64)
9245     gen_qemu_ld64(ctx, cpu_gpr[rD(ctx->opcode)], addr);
9246 #else
9247     TCGv_i64 t0 = tcg_temp_new_i64();
9248     gen_qemu_ld64(ctx, t0, addr);
9249     tcg_gen_trunc_i64_i32(cpu_gpr[rD(ctx->opcode)], t0);
9250     tcg_gen_shri_i64(t0, t0, 32);
9251     tcg_gen_trunc_i64_i32(cpu_gprh[rD(ctx->opcode)], t0);
9252     tcg_temp_free_i64(t0);
9253 #endif
9254 }
9255
9256 static inline void gen_op_evldw(DisasContext *ctx, TCGv addr)
9257 {
9258 #if defined(TARGET_PPC64)
9259     TCGv t0 = tcg_temp_new();
9260     gen_qemu_ld32u(ctx, t0, addr);
9261     tcg_gen_shli_tl(cpu_gpr[rD(ctx->opcode)], t0, 32);
9262     gen_addr_add(ctx, addr, addr, 4);
9263     gen_qemu_ld32u(ctx, t0, addr);
9264     tcg_gen_or_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rD(ctx->opcode)], t0);
9265     tcg_temp_free(t0);
9266 #else
9267     gen_qemu_ld32u(ctx, cpu_gprh[rD(ctx->opcode)], addr);
9268     gen_addr_add(ctx, addr, addr, 4);
9269     gen_qemu_ld32u(ctx, cpu_gpr[rD(ctx->opcode)], addr);
9270 #endif
9271 }
9272
9273 static inline void gen_op_evldh(DisasContext *ctx, TCGv addr)
9274 {
9275     TCGv t0 = tcg_temp_new();
9276 #if defined(TARGET_PPC64)
9277     gen_qemu_ld16u(ctx, t0, addr);
9278     tcg_gen_shli_tl(cpu_gpr[rD(ctx->opcode)], t0, 48);
9279     gen_addr_add(ctx, addr, addr, 2);
9280     gen_qemu_ld16u(ctx, t0, addr);
9281     tcg_gen_shli_tl(t0, t0, 32);
9282     tcg_gen_or_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rD(ctx->opcode)], t0);
9283     gen_addr_add(ctx, addr, addr, 2);
9284     gen_qemu_ld16u(ctx, t0, addr);
9285     tcg_gen_shli_tl(t0, t0, 16);
9286     tcg_gen_or_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rD(ctx->opcode)], t0);
9287     gen_addr_add(ctx, addr, addr, 2);
9288     gen_qemu_ld16u(ctx, t0, addr);
9289     tcg_gen_or_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rD(ctx->opcode)], t0);
9290 #else
9291     gen_qemu_ld16u(ctx, t0, addr);
9292     tcg_gen_shli_tl(cpu_gprh[rD(ctx->opcode)], t0, 16);
9293     gen_addr_add(ctx, addr, addr, 2);
9294     gen_qemu_ld16u(ctx, t0, addr);
9295     tcg_gen_or_tl(cpu_gprh[rD(ctx->opcode)], cpu_gprh[rD(ctx->opcode)], t0);
9296     gen_addr_add(ctx, addr, addr, 2);
9297     gen_qemu_ld16u(ctx, t0, addr);
9298     tcg_gen_shli_tl(cpu_gprh[rD(ctx->opcode)], t0, 16);
9299     gen_addr_add(ctx, addr, addr, 2);
9300     gen_qemu_ld16u(ctx, t0, addr);
9301     tcg_gen_or_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rD(ctx->opcode)], t0);
9302 #endif
9303     tcg_temp_free(t0);
9304 }
9305
9306 static inline void gen_op_evlhhesplat(DisasContext *ctx, TCGv addr)
9307 {
9308     TCGv t0 = tcg_temp_new();
9309     gen_qemu_ld16u(ctx, t0, addr);
9310 #if defined(TARGET_PPC64)
9311     tcg_gen_shli_tl(cpu_gpr[rD(ctx->opcode)], t0, 48);
9312     tcg_gen_shli_tl(t0, t0, 16);
9313     tcg_gen_or_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rD(ctx->opcode)], t0);
9314 #else
9315     tcg_gen_shli_tl(t0, t0, 16);
9316     tcg_gen_mov_tl(cpu_gprh[rD(ctx->opcode)], t0);
9317     tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], t0);
9318 #endif
9319     tcg_temp_free(t0);
9320 }
9321
9322 static inline void gen_op_evlhhousplat(DisasContext *ctx, TCGv addr)
9323 {
9324     TCGv t0 = tcg_temp_new();
9325     gen_qemu_ld16u(ctx, t0, addr);
9326 #if defined(TARGET_PPC64)
9327     tcg_gen_shli_tl(cpu_gpr[rD(ctx->opcode)], t0, 32);
9328     tcg_gen_or_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rD(ctx->opcode)], t0);
9329 #else
9330     tcg_gen_mov_tl(cpu_gprh[rD(ctx->opcode)], t0);
9331     tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], t0);
9332 #endif
9333     tcg_temp_free(t0);
9334 }
9335
9336 static inline void gen_op_evlhhossplat(DisasContext *ctx, TCGv addr)
9337 {
9338     TCGv t0 = tcg_temp_new();
9339     gen_qemu_ld16s(ctx, t0, addr);
9340 #if defined(TARGET_PPC64)
9341     tcg_gen_shli_tl(cpu_gpr[rD(ctx->opcode)], t0, 32);
9342     tcg_gen_ext32u_tl(t0, t0);
9343     tcg_gen_or_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rD(ctx->opcode)], t0);
9344 #else
9345     tcg_gen_mov_tl(cpu_gprh[rD(ctx->opcode)], t0);
9346     tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], t0);
9347 #endif
9348     tcg_temp_free(t0);
9349 }
9350
9351 static inline void gen_op_evlwhe(DisasContext *ctx, TCGv addr)
9352 {
9353     TCGv t0 = tcg_temp_new();
9354 #if defined(TARGET_PPC64)
9355     gen_qemu_ld16u(ctx, t0, addr);
9356     tcg_gen_shli_tl(cpu_gpr[rD(ctx->opcode)], t0, 48);
9357     gen_addr_add(ctx, addr, addr, 2);
9358     gen_qemu_ld16u(ctx, t0, addr);
9359     tcg_gen_shli_tl(t0, t0, 16);
9360     tcg_gen_or_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rD(ctx->opcode)], t0);
9361 #else
9362     gen_qemu_ld16u(ctx, t0, addr);
9363     tcg_gen_shli_tl(cpu_gprh[rD(ctx->opcode)], t0, 16);
9364     gen_addr_add(ctx, addr, addr, 2);
9365     gen_qemu_ld16u(ctx, t0, addr);
9366     tcg_gen_shli_tl(cpu_gpr[rD(ctx->opcode)], t0, 16);
9367 #endif
9368     tcg_temp_free(t0);
9369 }
9370
9371 static inline void gen_op_evlwhou(DisasContext *ctx, TCGv addr)
9372 {
9373 #if defined(TARGET_PPC64)
9374     TCGv t0 = tcg_temp_new();
9375     gen_qemu_ld16u(ctx, cpu_gpr[rD(ctx->opcode)], addr);
9376     gen_addr_add(ctx, addr, addr, 2);
9377     gen_qemu_ld16u(ctx, t0, addr);
9378     tcg_gen_shli_tl(t0, t0, 32);
9379     tcg_gen_or_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rD(ctx->opcode)], t0);
9380     tcg_temp_free(t0);
9381 #else
9382     gen_qemu_ld16u(ctx, cpu_gprh[rD(ctx->opcode)], addr);
9383     gen_addr_add(ctx, addr, addr, 2);
9384     gen_qemu_ld16u(ctx, cpu_gpr[rD(ctx->opcode)], addr);
9385 #endif
9386 }
9387
9388 static inline void gen_op_evlwhos(DisasContext *ctx, TCGv addr)
9389 {
9390 #if defined(TARGET_PPC64)
9391     TCGv t0 = tcg_temp_new();
9392     gen_qemu_ld16s(ctx, t0, addr);
9393     tcg_gen_ext32u_tl(cpu_gpr[rD(ctx->opcode)], t0);
9394     gen_addr_add(ctx, addr, addr, 2);
9395     gen_qemu_ld16s(ctx, t0, addr);
9396     tcg_gen_shli_tl(t0, t0, 32);
9397     tcg_gen_or_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rD(ctx->opcode)], t0);
9398     tcg_temp_free(t0);
9399 #else
9400     gen_qemu_ld16s(ctx, cpu_gprh[rD(ctx->opcode)], addr);
9401     gen_addr_add(ctx, addr, addr, 2);
9402     gen_qemu_ld16s(ctx, cpu_gpr[rD(ctx->opcode)], addr);
9403 #endif
9404 }
9405
9406 static inline void gen_op_evlwwsplat(DisasContext *ctx, TCGv addr)
9407 {
9408     TCGv t0 = tcg_temp_new();
9409     gen_qemu_ld32u(ctx, t0, addr);
9410 #if defined(TARGET_PPC64)
9411     tcg_gen_shli_tl(cpu_gpr[rD(ctx->opcode)], t0, 32);
9412     tcg_gen_or_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rD(ctx->opcode)], t0);
9413 #else
9414     tcg_gen_mov_tl(cpu_gprh[rD(ctx->opcode)], t0);
9415     tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], t0);
9416 #endif
9417     tcg_temp_free(t0);
9418 }
9419
9420 static inline void gen_op_evlwhsplat(DisasContext *ctx, TCGv addr)
9421 {
9422     TCGv t0 = tcg_temp_new();
9423 #if defined(TARGET_PPC64)
9424     gen_qemu_ld16u(ctx, t0, addr);
9425     tcg_gen_shli_tl(cpu_gpr[rD(ctx->opcode)], t0, 48);
9426     tcg_gen_shli_tl(t0, t0, 32);
9427     tcg_gen_or_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rD(ctx->opcode)], t0);
9428     gen_addr_add(ctx, addr, addr, 2);
9429     gen_qemu_ld16u(ctx, t0, addr);
9430     tcg_gen_or_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rD(ctx->opcode)], t0);
9431     tcg_gen_shli_tl(t0, t0, 16);
9432     tcg_gen_or_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rD(ctx->opcode)], t0);
9433 #else
9434     gen_qemu_ld16u(ctx, t0, addr);
9435     tcg_gen_shli_tl(cpu_gprh[rD(ctx->opcode)], t0, 16);
9436     tcg_gen_or_tl(cpu_gprh[rD(ctx->opcode)], cpu_gprh[rD(ctx->opcode)], t0);
9437     gen_addr_add(ctx, addr, addr, 2);
9438     gen_qemu_ld16u(ctx, t0, addr);
9439     tcg_gen_shli_tl(cpu_gpr[rD(ctx->opcode)], t0, 16);
9440     tcg_gen_or_tl(cpu_gpr[rD(ctx->opcode)], cpu_gprh[rD(ctx->opcode)], t0);
9441 #endif
9442     tcg_temp_free(t0);
9443 }
9444
9445 static inline void gen_op_evstdd(DisasContext *ctx, TCGv addr)
9446 {
9447 #if defined(TARGET_PPC64)
9448     gen_qemu_st64(ctx, cpu_gpr[rS(ctx->opcode)], addr);
9449 #else
9450     TCGv_i64 t0 = tcg_temp_new_i64();
9451     tcg_gen_concat_i32_i64(t0, cpu_gpr[rS(ctx->opcode)], cpu_gprh[rS(ctx->opcode)]);
9452     gen_qemu_st64(ctx, t0, addr);
9453     tcg_temp_free_i64(t0);
9454 #endif
9455 }
9456
9457 static inline void gen_op_evstdw(DisasContext *ctx, TCGv addr)
9458 {
9459 #if defined(TARGET_PPC64)
9460     TCGv t0 = tcg_temp_new();
9461     tcg_gen_shri_tl(t0, cpu_gpr[rS(ctx->opcode)], 32);
9462     gen_qemu_st32(ctx, t0, addr);
9463     tcg_temp_free(t0);
9464 #else
9465     gen_qemu_st32(ctx, cpu_gprh[rS(ctx->opcode)], addr);
9466 #endif
9467     gen_addr_add(ctx, addr, addr, 4);
9468     gen_qemu_st32(ctx, cpu_gpr[rS(ctx->opcode)], addr);
9469 }
9470
9471 static inline void gen_op_evstdh(DisasContext *ctx, TCGv addr)
9472 {
9473     TCGv t0 = tcg_temp_new();
9474 #if defined(TARGET_PPC64)
9475     tcg_gen_shri_tl(t0, cpu_gpr[rS(ctx->opcode)], 48);
9476 #else
9477     tcg_gen_shri_tl(t0, cpu_gprh[rS(ctx->opcode)], 16);
9478 #endif
9479     gen_qemu_st16(ctx, t0, addr);
9480     gen_addr_add(ctx, addr, addr, 2);
9481 #if defined(TARGET_PPC64)
9482     tcg_gen_shri_tl(t0, cpu_gpr[rS(ctx->opcode)], 32);
9483     gen_qemu_st16(ctx, t0, addr);
9484 #else
9485     gen_qemu_st16(ctx, cpu_gprh[rS(ctx->opcode)], addr);
9486 #endif
9487     gen_addr_add(ctx, addr, addr, 2);
9488     tcg_gen_shri_tl(t0, cpu_gpr[rS(ctx->opcode)], 16);
9489     gen_qemu_st16(ctx, t0, addr);
9490     tcg_temp_free(t0);
9491     gen_addr_add(ctx, addr, addr, 2);
9492     gen_qemu_st16(ctx, cpu_gpr[rS(ctx->opcode)], addr);
9493 }
9494
9495 static inline void gen_op_evstwhe(DisasContext *ctx, TCGv addr)
9496 {
9497     TCGv t0 = tcg_temp_new();
9498 #if defined(TARGET_PPC64)
9499     tcg_gen_shri_tl(t0, cpu_gpr[rS(ctx->opcode)], 48);
9500 #else
9501     tcg_gen_shri_tl(t0, cpu_gprh[rS(ctx->opcode)], 16);
9502 #endif
9503     gen_qemu_st16(ctx, t0, addr);
9504     gen_addr_add(ctx, addr, addr, 2);
9505     tcg_gen_shri_tl(t0, cpu_gpr[rS(ctx->opcode)], 16);
9506     gen_qemu_st16(ctx, t0, addr);
9507     tcg_temp_free(t0);
9508 }
9509
9510 static inline void gen_op_evstwho(DisasContext *ctx, TCGv addr)
9511 {
9512 #if defined(TARGET_PPC64)
9513     TCGv t0 = tcg_temp_new();
9514     tcg_gen_shri_tl(t0, cpu_gpr[rS(ctx->opcode)], 32);
9515     gen_qemu_st16(ctx, t0, addr);
9516     tcg_temp_free(t0);
9517 #else
9518     gen_qemu_st16(ctx, cpu_gprh[rS(ctx->opcode)], addr);
9519 #endif
9520     gen_addr_add(ctx, addr, addr, 2);
9521     gen_qemu_st16(ctx, cpu_gpr[rS(ctx->opcode)], addr);
9522 }
9523
9524 static inline void gen_op_evstwwe(DisasContext *ctx, TCGv addr)
9525 {
9526 #if defined(TARGET_PPC64)
9527     TCGv t0 = tcg_temp_new();
9528     tcg_gen_shri_tl(t0, cpu_gpr[rS(ctx->opcode)], 32);
9529     gen_qemu_st32(ctx, t0, addr);
9530     tcg_temp_free(t0);
9531 #else
9532     gen_qemu_st32(ctx, cpu_gprh[rS(ctx->opcode)], addr);
9533 #endif
9534 }
9535
9536 static inline void gen_op_evstwwo(DisasContext *ctx, TCGv addr)
9537 {
9538     gen_qemu_st32(ctx, cpu_gpr[rS(ctx->opcode)], addr);
9539 }
9540
9541 #define GEN_SPEOP_LDST(name, opc2, sh)                                        \
9542 static void glue(gen_, name)(DisasContext *ctx)                                       \
9543 {                                                                             \
9544     TCGv t0;                                                                  \
9545     if (unlikely(!ctx->spe_enabled)) {                                        \
9546         gen_exception(ctx, POWERPC_EXCP_SPEU);                                \
9547         return;                                                               \
9548     }                                                                         \
9549     gen_set_access_type(ctx, ACCESS_INT);                                     \
9550     t0 = tcg_temp_new();                                                      \
9551     if (Rc(ctx->opcode)) {                                                    \
9552         gen_addr_spe_imm_index(ctx, t0, sh);                                  \
9553     } else {                                                                  \
9554         gen_addr_reg_index(ctx, t0);                                          \
9555     }                                                                         \
9556     gen_op_##name(ctx, t0);                                                   \
9557     tcg_temp_free(t0);                                                        \
9558 }
9559
9560 GEN_SPEOP_LDST(evldd, 0x00, 3);
9561 GEN_SPEOP_LDST(evldw, 0x01, 3);
9562 GEN_SPEOP_LDST(evldh, 0x02, 3);
9563 GEN_SPEOP_LDST(evlhhesplat, 0x04, 1);
9564 GEN_SPEOP_LDST(evlhhousplat, 0x06, 1);
9565 GEN_SPEOP_LDST(evlhhossplat, 0x07, 1);
9566 GEN_SPEOP_LDST(evlwhe, 0x08, 2);
9567 GEN_SPEOP_LDST(evlwhou, 0x0A, 2);
9568 GEN_SPEOP_LDST(evlwhos, 0x0B, 2);
9569 GEN_SPEOP_LDST(evlwwsplat, 0x0C, 2);
9570 GEN_SPEOP_LDST(evlwhsplat, 0x0E, 2);
9571
9572 GEN_SPEOP_LDST(evstdd, 0x10, 3);
9573 GEN_SPEOP_LDST(evstdw, 0x11, 3);
9574 GEN_SPEOP_LDST(evstdh, 0x12, 3);
9575 GEN_SPEOP_LDST(evstwhe, 0x18, 2);
9576 GEN_SPEOP_LDST(evstwho, 0x1A, 2);
9577 GEN_SPEOP_LDST(evstwwe, 0x1C, 2);
9578 GEN_SPEOP_LDST(evstwwo, 0x1E, 2);
9579
9580 /* Multiply and add - TODO */
9581 #if 0
9582 GEN_SPE(speundef,       evmhessf,      0x01, 0x10, 0xFFFFFFFF, 0x00000000, PPC_SPE);//
9583 GEN_SPE(speundef,       evmhossf,      0x03, 0x10, 0xFFFFFFFF, 0x00000000, PPC_SPE);
9584 GEN_SPE(evmheumi,       evmhesmi,      0x04, 0x10, 0x00000000, 0x00000000, PPC_SPE);
9585 GEN_SPE(speundef,       evmhesmf,      0x05, 0x10, 0xFFFFFFFF, 0x00000000, PPC_SPE);
9586 GEN_SPE(evmhoumi,       evmhosmi,      0x06, 0x10, 0x00000000, 0x00000000, PPC_SPE);
9587 GEN_SPE(speundef,       evmhosmf,      0x07, 0x10, 0xFFFFFFFF, 0x00000000, PPC_SPE);
9588 GEN_SPE(speundef,       evmhessfa,     0x11, 0x10, 0xFFFFFFFF, 0x00000000, PPC_SPE);
9589 GEN_SPE(speundef,       evmhossfa,     0x13, 0x10, 0xFFFFFFFF, 0x00000000, PPC_SPE);
9590 GEN_SPE(evmheumia,      evmhesmia,     0x14, 0x10, 0x00000000, 0x00000000, PPC_SPE);
9591 GEN_SPE(speundef,       evmhesmfa,     0x15, 0x10, 0xFFFFFFFF, 0x00000000, PPC_SPE);
9592 GEN_SPE(evmhoumia,      evmhosmia,     0x16, 0x10, 0x00000000, 0x00000000, PPC_SPE);
9593 GEN_SPE(speundef,       evmhosmfa,     0x17, 0x10, 0xFFFFFFFF, 0x00000000, PPC_SPE);
9594
9595 GEN_SPE(speundef,       evmwhssf,      0x03, 0x11, 0xFFFFFFFF, 0x00000000, PPC_SPE);
9596 GEN_SPE(evmwlumi,       speundef,      0x04, 0x11, 0x00000000, 0xFFFFFFFF, PPC_SPE);
9597 GEN_SPE(evmwhumi,       evmwhsmi,      0x06, 0x11, 0x00000000, 0x00000000, PPC_SPE);
9598 GEN_SPE(speundef,       evmwhsmf,      0x07, 0x11, 0xFFFFFFFF, 0x00000000, PPC_SPE);
9599 GEN_SPE(speundef,       evmwssf,       0x09, 0x11, 0xFFFFFFFF, 0x00000000, PPC_SPE);
9600 GEN_SPE(speundef,       evmwsmf,       0x0D, 0x11, 0xFFFFFFFF, 0x00000000, PPC_SPE);
9601 GEN_SPE(speundef,       evmwhssfa,     0x13, 0x11, 0xFFFFFFFF, 0x00000000, PPC_SPE);
9602 GEN_SPE(evmwlumia,      speundef,      0x14, 0x11, 0x00000000, 0xFFFFFFFF, PPC_SPE);
9603 GEN_SPE(evmwhumia,      evmwhsmia,     0x16, 0x11, 0x00000000, 0x00000000, PPC_SPE);
9604 GEN_SPE(speundef,       evmwhsmfa,     0x17, 0x11, 0xFFFFFFFF, 0x00000000, PPC_SPE);
9605 GEN_SPE(speundef,       evmwssfa,      0x19, 0x11, 0xFFFFFFFF, 0x00000000, PPC_SPE);
9606 GEN_SPE(speundef,       evmwsmfa,      0x1D, 0x11, 0xFFFFFFFF, 0x00000000, PPC_SPE);
9607
9608 GEN_SPE(evadduiaaw,     evaddsiaaw,    0x00, 0x13, 0x0000F800, 0x0000F800, PPC_SPE);
9609 GEN_SPE(evsubfusiaaw,   evsubfssiaaw,  0x01, 0x13, 0x0000F800, 0x0000F800, PPC_SPE);
9610 GEN_SPE(evaddumiaaw,    evaddsmiaaw,   0x04, 0x13, 0x0000F800, 0x0000F800, PPC_SPE);
9611 GEN_SPE(evsubfumiaaw,   evsubfsmiaaw,  0x05, 0x13, 0x0000F800, 0x0000F800, PPC_SPE);
9612 GEN_SPE(evdivws,        evdivwu,       0x06, 0x13, 0x00000000, 0x00000000, PPC_SPE);
9613
9614 GEN_SPE(evmheusiaaw,    evmhessiaaw,   0x00, 0x14, 0x00000000, 0x00000000, PPC_SPE);
9615 GEN_SPE(speundef,       evmhessfaaw,   0x01, 0x14, 0xFFFFFFFF, 0x00000000, PPC_SPE);
9616 GEN_SPE(evmhousiaaw,    evmhossiaaw,   0x02, 0x14, 0x00000000, 0x00000000, PPC_SPE);
9617 GEN_SPE(speundef,       evmhossfaaw,   0x03, 0x14, 0xFFFFFFFF, 0x00000000, PPC_SPE);
9618 GEN_SPE(evmheumiaaw,    evmhesmiaaw,   0x04, 0x14, 0x00000000, 0x00000000, PPC_SPE);
9619 GEN_SPE(speundef,       evmhesmfaaw,   0x05, 0x14, 0xFFFFFFFF, 0x00000000, PPC_SPE);
9620 GEN_SPE(evmhoumiaaw,    evmhosmiaaw,   0x06, 0x14, 0x00000000, 0x00000000, PPC_SPE);
9621 GEN_SPE(speundef,       evmhosmfaaw,   0x07, 0x14, 0xFFFFFFFF, 0x00000000, PPC_SPE);
9622 GEN_SPE(evmhegumiaa,    evmhegsmiaa,   0x14, 0x14, 0x00000000, 0x00000000, PPC_SPE);
9623 GEN_SPE(speundef,       evmhegsmfaa,   0x15, 0x14, 0xFFFFFFFF, 0x00000000, PPC_SPE);
9624 GEN_SPE(evmhogumiaa,    evmhogsmiaa,   0x16, 0x14, 0x00000000, 0x00000000, PPC_SPE);
9625 GEN_SPE(speundef,       evmhogsmfaa,   0x17, 0x14, 0xFFFFFFFF, 0x00000000, PPC_SPE);
9626
9627 GEN_SPE(evmwlusiaaw,    evmwlssiaaw,   0x00, 0x15, 0x00000000, 0x00000000, PPC_SPE);
9628 GEN_SPE(evmwlumiaaw,    evmwlsmiaaw,   0x04, 0x15, 0x00000000, 0x00000000, PPC_SPE);
9629 GEN_SPE(speundef,       evmwssfaa,     0x09, 0x15, 0xFFFFFFFF, 0x00000000, PPC_SPE);
9630 GEN_SPE(speundef,       evmwsmfaa,     0x0D, 0x15, 0xFFFFFFFF, 0x00000000, PPC_SPE);
9631
9632 GEN_SPE(evmheusianw,    evmhessianw,   0x00, 0x16, 0x00000000, 0x00000000, PPC_SPE);
9633 GEN_SPE(speundef,       evmhessfanw,   0x01, 0x16, 0xFFFFFFFF, 0x00000000, PPC_SPE);
9634 GEN_SPE(evmhousianw,    evmhossianw,   0x02, 0x16, 0x00000000, 0x00000000, PPC_SPE);
9635 GEN_SPE(speundef,       evmhossfanw,   0x03, 0x16, 0xFFFFFFFF, 0x00000000, PPC_SPE);
9636 GEN_SPE(evmheumianw,    evmhesmianw,   0x04, 0x16, 0x00000000, 0x00000000, PPC_SPE);
9637 GEN_SPE(speundef,       evmhesmfanw,   0x05, 0x16, 0xFFFFFFFF, 0x00000000, PPC_SPE);
9638 GEN_SPE(evmhoumianw,    evmhosmianw,   0x06, 0x16, 0x00000000, 0x00000000, PPC_SPE);
9639 GEN_SPE(speundef,       evmhosmfanw,   0x07, 0x16, 0xFFFFFFFF, 0x00000000, PPC_SPE);
9640 GEN_SPE(evmhegumian,    evmhegsmian,   0x14, 0x16, 0x00000000, 0x00000000, PPC_SPE);
9641 GEN_SPE(speundef,       evmhegsmfan,   0x15, 0x16, 0xFFFFFFFF, 0x00000000, PPC_SPE);
9642 GEN_SPE(evmhigumian,    evmhigsmian,   0x16, 0x16, 0x00000000, 0x00000000, PPC_SPE);
9643 GEN_SPE(speundef,       evmhogsmfan,   0x17, 0x16, 0xFFFFFFFF, 0x00000000, PPC_SPE);
9644
9645 GEN_SPE(evmwlusianw,    evmwlssianw,   0x00, 0x17, 0x00000000, 0x00000000, PPC_SPE);
9646 GEN_SPE(evmwlumianw,    evmwlsmianw,   0x04, 0x17, 0x00000000, 0x00000000, PPC_SPE);
9647 GEN_SPE(speundef,       evmwssfan,     0x09, 0x17, 0xFFFFFFFF, 0x00000000, PPC_SPE);
9648 GEN_SPE(evmwumian,      evmwsmian,     0x0C, 0x17, 0x00000000, 0x00000000, PPC_SPE);
9649 GEN_SPE(speundef,       evmwsmfan,     0x0D, 0x17, 0xFFFFFFFF, 0x00000000, PPC_SPE);
9650 #endif
9651
9652 /***                      SPE floating-point extension                     ***/
9653 #if defined(TARGET_PPC64)
9654 #define GEN_SPEFPUOP_CONV_32_32(name)                                         \
9655 static inline void gen_##name(DisasContext *ctx)                              \
9656 {                                                                             \
9657     TCGv_i32 t0;                                                              \
9658     TCGv t1;                                                                  \
9659     t0 = tcg_temp_new_i32();                                                  \
9660     tcg_gen_trunc_tl_i32(t0, cpu_gpr[rB(ctx->opcode)]);                       \
9661     gen_helper_##name(t0, cpu_env, t0);                                       \
9662     t1 = tcg_temp_new();                                                      \
9663     tcg_gen_extu_i32_tl(t1, t0);                                              \
9664     tcg_temp_free_i32(t0);                                                    \
9665     tcg_gen_andi_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rD(ctx->opcode)],       \
9666                     0xFFFFFFFF00000000ULL);                                   \
9667     tcg_gen_or_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rD(ctx->opcode)], t1);    \
9668     tcg_temp_free(t1);                                                        \
9669 }
9670 #define GEN_SPEFPUOP_CONV_32_64(name)                                         \
9671 static inline void gen_##name(DisasContext *ctx)                              \
9672 {                                                                             \
9673     TCGv_i32 t0;                                                              \
9674     TCGv t1;                                                                  \
9675     t0 = tcg_temp_new_i32();                                                  \
9676     gen_helper_##name(t0, cpu_env, cpu_gpr[rB(ctx->opcode)]);                 \
9677     t1 = tcg_temp_new();                                                      \
9678     tcg_gen_extu_i32_tl(t1, t0);                                              \
9679     tcg_temp_free_i32(t0);                                                    \
9680     tcg_gen_andi_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rD(ctx->opcode)],       \
9681                     0xFFFFFFFF00000000ULL);                                   \
9682     tcg_gen_or_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rD(ctx->opcode)], t1);    \
9683     tcg_temp_free(t1);                                                        \
9684 }
9685 #define GEN_SPEFPUOP_CONV_64_32(name)                                         \
9686 static inline void gen_##name(DisasContext *ctx)                              \
9687 {                                                                             \
9688     TCGv_i32 t0 = tcg_temp_new_i32();                                         \
9689     tcg_gen_trunc_tl_i32(t0, cpu_gpr[rB(ctx->opcode)]);                       \
9690     gen_helper_##name(cpu_gpr[rD(ctx->opcode)], cpu_env, t0);                 \
9691     tcg_temp_free_i32(t0);                                                    \
9692 }
9693 #define GEN_SPEFPUOP_CONV_64_64(name)                                         \
9694 static inline void gen_##name(DisasContext *ctx)                              \
9695 {                                                                             \
9696     gen_helper_##name(cpu_gpr[rD(ctx->opcode)], cpu_env,                      \
9697                       cpu_gpr[rB(ctx->opcode)]);                              \
9698 }
9699 #define GEN_SPEFPUOP_ARITH2_32_32(name)                                       \
9700 static inline void gen_##name(DisasContext *ctx)                              \
9701 {                                                                             \
9702     TCGv_i32 t0, t1;                                                          \
9703     TCGv_i64 t2;                                                              \
9704     if (unlikely(!ctx->spe_enabled)) {                                        \
9705         gen_exception(ctx, POWERPC_EXCP_SPEU);                                \
9706         return;                                                               \
9707     }                                                                         \
9708     t0 = tcg_temp_new_i32();                                                  \
9709     t1 = tcg_temp_new_i32();                                                  \
9710     tcg_gen_trunc_tl_i32(t0, cpu_gpr[rA(ctx->opcode)]);                       \
9711     tcg_gen_trunc_tl_i32(t1, cpu_gpr[rB(ctx->opcode)]);                       \
9712     gen_helper_##name(t0, cpu_env, t0, t1);                                   \
9713     tcg_temp_free_i32(t1);                                                    \
9714     t2 = tcg_temp_new();                                                      \
9715     tcg_gen_extu_i32_tl(t2, t0);                                              \
9716     tcg_temp_free_i32(t0);                                                    \
9717     tcg_gen_andi_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rD(ctx->opcode)],       \
9718                     0xFFFFFFFF00000000ULL);                                   \
9719     tcg_gen_or_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rD(ctx->opcode)], t2);    \
9720     tcg_temp_free(t2);                                                        \
9721 }
9722 #define GEN_SPEFPUOP_ARITH2_64_64(name)                                       \
9723 static inline void gen_##name(DisasContext *ctx)                              \
9724 {                                                                             \
9725     if (unlikely(!ctx->spe_enabled)) {                                        \
9726         gen_exception(ctx, POWERPC_EXCP_SPEU);                                \
9727         return;                                                               \
9728     }                                                                         \
9729     gen_helper_##name(cpu_gpr[rD(ctx->opcode)], cpu_env,                      \
9730                       cpu_gpr[rA(ctx->opcode)], cpu_gpr[rB(ctx->opcode)]);    \
9731 }
9732 #define GEN_SPEFPUOP_COMP_32(name)                                            \
9733 static inline void gen_##name(DisasContext *ctx)                              \
9734 {                                                                             \
9735     TCGv_i32 t0, t1;                                                          \
9736     if (unlikely(!ctx->spe_enabled)) {                                        \
9737         gen_exception(ctx, POWERPC_EXCP_SPEU);                                \
9738         return;                                                               \
9739     }                                                                         \
9740     t0 = tcg_temp_new_i32();                                                  \
9741     t1 = tcg_temp_new_i32();                                                  \
9742     tcg_gen_trunc_tl_i32(t0, cpu_gpr[rA(ctx->opcode)]);                       \
9743     tcg_gen_trunc_tl_i32(t1, cpu_gpr[rB(ctx->opcode)]);                       \
9744     gen_helper_##name(cpu_crf[crfD(ctx->opcode)], cpu_env, t0, t1);           \
9745     tcg_temp_free_i32(t0);                                                    \
9746     tcg_temp_free_i32(t1);                                                    \
9747 }
9748 #define GEN_SPEFPUOP_COMP_64(name)                                            \
9749 static inline void gen_##name(DisasContext *ctx)                              \
9750 {                                                                             \
9751     if (unlikely(!ctx->spe_enabled)) {                                        \
9752         gen_exception(ctx, POWERPC_EXCP_SPEU);                                \
9753         return;                                                               \
9754     }                                                                         \
9755     gen_helper_##name(cpu_crf[crfD(ctx->opcode)], cpu_env,                    \
9756                       cpu_gpr[rA(ctx->opcode)], cpu_gpr[rB(ctx->opcode)]);    \
9757 }
9758 #else
9759 #define GEN_SPEFPUOP_CONV_32_32(name)                                         \
9760 static inline void gen_##name(DisasContext *ctx)                              \
9761 {                                                                             \
9762     gen_helper_##name(cpu_gpr[rD(ctx->opcode)], cpu_env,                      \
9763                       cpu_gpr[rB(ctx->opcode)]);                              \
9764 }
9765 #define GEN_SPEFPUOP_CONV_32_64(name)                                         \
9766 static inline void gen_##name(DisasContext *ctx)                              \
9767 {                                                                             \
9768     TCGv_i64 t0 = tcg_temp_new_i64();                                         \
9769     gen_load_gpr64(t0, rB(ctx->opcode));                                      \
9770     gen_helper_##name(cpu_gpr[rD(ctx->opcode)], cpu_env, t0);                 \
9771     tcg_temp_free_i64(t0);                                                    \
9772 }
9773 #define GEN_SPEFPUOP_CONV_64_32(name)                                         \
9774 static inline void gen_##name(DisasContext *ctx)                              \
9775 {                                                                             \
9776     TCGv_i64 t0 = tcg_temp_new_i64();                                         \
9777     gen_helper_##name(t0, cpu_env, cpu_gpr[rB(ctx->opcode)]);                 \
9778     gen_store_gpr64(rD(ctx->opcode), t0);                                     \
9779     tcg_temp_free_i64(t0);                                                    \
9780 }
9781 #define GEN_SPEFPUOP_CONV_64_64(name)                                         \
9782 static inline void gen_##name(DisasContext *ctx)                              \
9783 {                                                                             \
9784     TCGv_i64 t0 = tcg_temp_new_i64();                                         \
9785     gen_load_gpr64(t0, rB(ctx->opcode));                                      \
9786     gen_helper_##name(t0, cpu_env, t0);                                       \
9787     gen_store_gpr64(rD(ctx->opcode), t0);                                     \
9788     tcg_temp_free_i64(t0);                                                    \
9789 }
9790 #define GEN_SPEFPUOP_ARITH2_32_32(name)                                       \
9791 static inline void gen_##name(DisasContext *ctx)                              \
9792 {                                                                             \
9793     if (unlikely(!ctx->spe_enabled)) {                                        \
9794         gen_exception(ctx, POWERPC_EXCP_SPEU);                                \
9795         return;                                                               \
9796     }                                                                         \
9797     gen_helper_##name(cpu_gpr[rD(ctx->opcode)], cpu_env,                      \
9798                       cpu_gpr[rA(ctx->opcode)], cpu_gpr[rB(ctx->opcode)]);    \
9799 }
9800 #define GEN_SPEFPUOP_ARITH2_64_64(name)                                       \
9801 static inline void gen_##name(DisasContext *ctx)                              \
9802 {                                                                             \
9803     TCGv_i64 t0, t1;                                                          \
9804     if (unlikely(!ctx->spe_enabled)) {                                        \
9805         gen_exception(ctx, POWERPC_EXCP_SPEU);                                \
9806         return;                                                               \
9807     }                                                                         \
9808     t0 = tcg_temp_new_i64();                                                  \
9809     t1 = tcg_temp_new_i64();                                                  \
9810     gen_load_gpr64(t0, rA(ctx->opcode));                                      \
9811     gen_load_gpr64(t1, rB(ctx->opcode));                                      \
9812     gen_helper_##name(t0, cpu_env, t0, t1);                                   \
9813     gen_store_gpr64(rD(ctx->opcode), t0);                                     \
9814     tcg_temp_free_i64(t0);                                                    \
9815     tcg_temp_free_i64(t1);                                                    \
9816 }
9817 #define GEN_SPEFPUOP_COMP_32(name)                                            \
9818 static inline void gen_##name(DisasContext *ctx)                              \
9819 {                                                                             \
9820     if (unlikely(!ctx->spe_enabled)) {                                        \
9821         gen_exception(ctx, POWERPC_EXCP_SPEU);                                \
9822         return;                                                               \
9823     }                                                                         \
9824     gen_helper_##name(cpu_crf[crfD(ctx->opcode)], cpu_env,                    \
9825                       cpu_gpr[rA(ctx->opcode)], cpu_gpr[rB(ctx->opcode)]);    \
9826 }
9827 #define GEN_SPEFPUOP_COMP_64(name)                                            \
9828 static inline void gen_##name(DisasContext *ctx)                              \
9829 {                                                                             \
9830     TCGv_i64 t0, t1;                                                          \
9831     if (unlikely(!ctx->spe_enabled)) {                                        \
9832         gen_exception(ctx, POWERPC_EXCP_SPEU);                                \
9833         return;                                                               \
9834     }                                                                         \
9835     t0 = tcg_temp_new_i64();                                                  \
9836     t1 = tcg_temp_new_i64();                                                  \
9837     gen_load_gpr64(t0, rA(ctx->opcode));                                      \
9838     gen_load_gpr64(t1, rB(ctx->opcode));                                      \
9839     gen_helper_##name(cpu_crf[crfD(ctx->opcode)], cpu_env, t0, t1);           \
9840     tcg_temp_free_i64(t0);                                                    \
9841     tcg_temp_free_i64(t1);                                                    \
9842 }
9843 #endif
9844
9845 /* Single precision floating-point vectors operations */
9846 /* Arithmetic */
9847 GEN_SPEFPUOP_ARITH2_64_64(evfsadd);
9848 GEN_SPEFPUOP_ARITH2_64_64(evfssub);
9849 GEN_SPEFPUOP_ARITH2_64_64(evfsmul);
9850 GEN_SPEFPUOP_ARITH2_64_64(evfsdiv);
9851 static inline void gen_evfsabs(DisasContext *ctx)
9852 {
9853     if (unlikely(!ctx->spe_enabled)) {
9854         gen_exception(ctx, POWERPC_EXCP_SPEU);
9855         return;
9856     }
9857 #if defined(TARGET_PPC64)
9858     tcg_gen_andi_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)], ~0x8000000080000000LL);
9859 #else
9860     tcg_gen_andi_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)], ~0x80000000);
9861     tcg_gen_andi_tl(cpu_gprh[rD(ctx->opcode)], cpu_gprh[rA(ctx->opcode)], ~0x80000000);
9862 #endif
9863 }
9864 static inline void gen_evfsnabs(DisasContext *ctx)
9865 {
9866     if (unlikely(!ctx->spe_enabled)) {
9867         gen_exception(ctx, POWERPC_EXCP_SPEU);
9868         return;
9869     }
9870 #if defined(TARGET_PPC64)
9871     tcg_gen_ori_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)], 0x8000000080000000LL);
9872 #else
9873     tcg_gen_ori_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)], 0x80000000);
9874     tcg_gen_ori_tl(cpu_gprh[rD(ctx->opcode)], cpu_gprh[rA(ctx->opcode)], 0x80000000);
9875 #endif
9876 }
9877 static inline void gen_evfsneg(DisasContext *ctx)
9878 {
9879     if (unlikely(!ctx->spe_enabled)) {
9880         gen_exception(ctx, POWERPC_EXCP_SPEU);
9881         return;
9882     }
9883 #if defined(TARGET_PPC64)
9884     tcg_gen_xori_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)], 0x8000000080000000LL);
9885 #else
9886     tcg_gen_xori_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)], 0x80000000);
9887     tcg_gen_xori_tl(cpu_gprh[rD(ctx->opcode)], cpu_gprh[rA(ctx->opcode)], 0x80000000);
9888 #endif
9889 }
9890
9891 /* Conversion */
9892 GEN_SPEFPUOP_CONV_64_64(evfscfui);
9893 GEN_SPEFPUOP_CONV_64_64(evfscfsi);
9894 GEN_SPEFPUOP_CONV_64_64(evfscfuf);
9895 GEN_SPEFPUOP_CONV_64_64(evfscfsf);
9896 GEN_SPEFPUOP_CONV_64_64(evfsctui);
9897 GEN_SPEFPUOP_CONV_64_64(evfsctsi);
9898 GEN_SPEFPUOP_CONV_64_64(evfsctuf);
9899 GEN_SPEFPUOP_CONV_64_64(evfsctsf);
9900 GEN_SPEFPUOP_CONV_64_64(evfsctuiz);
9901 GEN_SPEFPUOP_CONV_64_64(evfsctsiz);
9902
9903 /* Comparison */
9904 GEN_SPEFPUOP_COMP_64(evfscmpgt);
9905 GEN_SPEFPUOP_COMP_64(evfscmplt);
9906 GEN_SPEFPUOP_COMP_64(evfscmpeq);
9907 GEN_SPEFPUOP_COMP_64(evfststgt);
9908 GEN_SPEFPUOP_COMP_64(evfststlt);
9909 GEN_SPEFPUOP_COMP_64(evfststeq);
9910
9911 /* Opcodes definitions */
9912 GEN_SPE(evfsadd,   evfssub,   0x00, 0x0A, 0x00000000, 0x00000000, PPC_SPE_SINGLE); //
9913 GEN_SPE(evfsabs,   evfsnabs,  0x02, 0x0A, 0x0000F800, 0x0000F800, PPC_SPE_SINGLE); //
9914 GEN_SPE(evfsneg,   speundef,  0x03, 0x0A, 0x0000F800, 0xFFFFFFFF, PPC_SPE_SINGLE); //
9915 GEN_SPE(evfsmul,   evfsdiv,   0x04, 0x0A, 0x00000000, 0x00000000, PPC_SPE_SINGLE); //
9916 GEN_SPE(evfscmpgt, evfscmplt, 0x06, 0x0A, 0x00600000, 0x00600000, PPC_SPE_SINGLE); //
9917 GEN_SPE(evfscmpeq, speundef,  0x07, 0x0A, 0x00600000, 0xFFFFFFFF, PPC_SPE_SINGLE); //
9918 GEN_SPE(evfscfui,  evfscfsi,  0x08, 0x0A, 0x00180000, 0x00180000, PPC_SPE_SINGLE); //
9919 GEN_SPE(evfscfuf,  evfscfsf,  0x09, 0x0A, 0x00180000, 0x00180000, PPC_SPE_SINGLE); //
9920 GEN_SPE(evfsctui,  evfsctsi,  0x0A, 0x0A, 0x00180000, 0x00180000, PPC_SPE_SINGLE); //
9921 GEN_SPE(evfsctuf,  evfsctsf,  0x0B, 0x0A, 0x00180000, 0x00180000, PPC_SPE_SINGLE); //
9922 GEN_SPE(evfsctuiz, speundef,  0x0C, 0x0A, 0x00180000, 0xFFFFFFFF, PPC_SPE_SINGLE); //
9923 GEN_SPE(evfsctsiz, speundef,  0x0D, 0x0A, 0x00180000, 0xFFFFFFFF, PPC_SPE_SINGLE); //
9924 GEN_SPE(evfststgt, evfststlt, 0x0E, 0x0A, 0x00600000, 0x00600000, PPC_SPE_SINGLE); //
9925 GEN_SPE(evfststeq, speundef,  0x0F, 0x0A, 0x00600000, 0xFFFFFFFF, PPC_SPE_SINGLE); //
9926
9927 /* Single precision floating-point operations */
9928 /* Arithmetic */
9929 GEN_SPEFPUOP_ARITH2_32_32(efsadd);
9930 GEN_SPEFPUOP_ARITH2_32_32(efssub);
9931 GEN_SPEFPUOP_ARITH2_32_32(efsmul);
9932 GEN_SPEFPUOP_ARITH2_32_32(efsdiv);
9933 static inline void gen_efsabs(DisasContext *ctx)
9934 {
9935     if (unlikely(!ctx->spe_enabled)) {
9936         gen_exception(ctx, POWERPC_EXCP_SPEU);
9937         return;
9938     }
9939     tcg_gen_andi_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)], (target_long)~0x80000000LL);
9940 }
9941 static inline void gen_efsnabs(DisasContext *ctx)
9942 {
9943     if (unlikely(!ctx->spe_enabled)) {
9944         gen_exception(ctx, POWERPC_EXCP_SPEU);
9945         return;
9946     }
9947     tcg_gen_ori_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)], 0x80000000);
9948 }
9949 static inline void gen_efsneg(DisasContext *ctx)
9950 {
9951     if (unlikely(!ctx->spe_enabled)) {
9952         gen_exception(ctx, POWERPC_EXCP_SPEU);
9953         return;
9954     }
9955     tcg_gen_xori_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)], 0x80000000);
9956 }
9957
9958 /* Conversion */
9959 GEN_SPEFPUOP_CONV_32_32(efscfui);
9960 GEN_SPEFPUOP_CONV_32_32(efscfsi);
9961 GEN_SPEFPUOP_CONV_32_32(efscfuf);
9962 GEN_SPEFPUOP_CONV_32_32(efscfsf);
9963 GEN_SPEFPUOP_CONV_32_32(efsctui);
9964 GEN_SPEFPUOP_CONV_32_32(efsctsi);
9965 GEN_SPEFPUOP_CONV_32_32(efsctuf);
9966 GEN_SPEFPUOP_CONV_32_32(efsctsf);
9967 GEN_SPEFPUOP_CONV_32_32(efsctuiz);
9968 GEN_SPEFPUOP_CONV_32_32(efsctsiz);
9969 GEN_SPEFPUOP_CONV_32_64(efscfd);
9970
9971 /* Comparison */
9972 GEN_SPEFPUOP_COMP_32(efscmpgt);
9973 GEN_SPEFPUOP_COMP_32(efscmplt);
9974 GEN_SPEFPUOP_COMP_32(efscmpeq);
9975 GEN_SPEFPUOP_COMP_32(efststgt);
9976 GEN_SPEFPUOP_COMP_32(efststlt);
9977 GEN_SPEFPUOP_COMP_32(efststeq);
9978
9979 /* Opcodes definitions */
9980 GEN_SPE(efsadd,   efssub,   0x00, 0x0B, 0x00000000, 0x00000000, PPC_SPE_SINGLE); //
9981 GEN_SPE(efsabs,   efsnabs,  0x02, 0x0B, 0x0000F800, 0x0000F800, PPC_SPE_SINGLE); //
9982 GEN_SPE(efsneg,   speundef, 0x03, 0x0B, 0x0000F800, 0xFFFFFFFF, PPC_SPE_SINGLE); //
9983 GEN_SPE(efsmul,   efsdiv,   0x04, 0x0B, 0x00000000, 0x00000000, PPC_SPE_SINGLE); //
9984 GEN_SPE(efscmpgt, efscmplt, 0x06, 0x0B, 0x00600000, 0x00600000, PPC_SPE_SINGLE); //
9985 GEN_SPE(efscmpeq, efscfd,   0x07, 0x0B, 0x00600000, 0x00180000, PPC_SPE_SINGLE); //
9986 GEN_SPE(efscfui,  efscfsi,  0x08, 0x0B, 0x00180000, 0x00180000, PPC_SPE_SINGLE); //
9987 GEN_SPE(efscfuf,  efscfsf,  0x09, 0x0B, 0x00180000, 0x00180000, PPC_SPE_SINGLE); //
9988 GEN_SPE(efsctui,  efsctsi,  0x0A, 0x0B, 0x00180000, 0x00180000, PPC_SPE_SINGLE); //
9989 GEN_SPE(efsctuf,  efsctsf,  0x0B, 0x0B, 0x00180000, 0x00180000, PPC_SPE_SINGLE); //
9990 GEN_SPE(efsctuiz, speundef, 0x0C, 0x0B, 0x00180000, 0xFFFFFFFF, PPC_SPE_SINGLE); //
9991 GEN_SPE(efsctsiz, speundef, 0x0D, 0x0B, 0x00180000, 0xFFFFFFFF, PPC_SPE_SINGLE); //
9992 GEN_SPE(efststgt, efststlt, 0x0E, 0x0B, 0x00600000, 0x00600000, PPC_SPE_SINGLE); //
9993 GEN_SPE(efststeq, speundef, 0x0F, 0x0B, 0x00600000, 0xFFFFFFFF, PPC_SPE_SINGLE); //
9994
9995 /* Double precision floating-point operations */
9996 /* Arithmetic */
9997 GEN_SPEFPUOP_ARITH2_64_64(efdadd);
9998 GEN_SPEFPUOP_ARITH2_64_64(efdsub);
9999 GEN_SPEFPUOP_ARITH2_64_64(efdmul);
10000 GEN_SPEFPUOP_ARITH2_64_64(efddiv);
10001 static inline void gen_efdabs(DisasContext *ctx)
10002 {
10003     if (unlikely(!ctx->spe_enabled)) {
10004         gen_exception(ctx, POWERPC_EXCP_SPEU);
10005         return;
10006     }
10007 #if defined(TARGET_PPC64)
10008     tcg_gen_andi_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)], ~0x8000000000000000LL);
10009 #else
10010     tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)]);
10011     tcg_gen_andi_tl(cpu_gprh[rD(ctx->opcode)], cpu_gprh[rA(ctx->opcode)], ~0x80000000);
10012 #endif
10013 }
10014 static inline void gen_efdnabs(DisasContext *ctx)
10015 {
10016     if (unlikely(!ctx->spe_enabled)) {
10017         gen_exception(ctx, POWERPC_EXCP_SPEU);
10018         return;
10019     }
10020 #if defined(TARGET_PPC64)
10021     tcg_gen_ori_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)], 0x8000000000000000LL);
10022 #else
10023     tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)]);
10024     tcg_gen_ori_tl(cpu_gprh[rD(ctx->opcode)], cpu_gprh[rA(ctx->opcode)], 0x80000000);
10025 #endif
10026 }
10027 static inline void gen_efdneg(DisasContext *ctx)
10028 {
10029     if (unlikely(!ctx->spe_enabled)) {
10030         gen_exception(ctx, POWERPC_EXCP_SPEU);
10031         return;
10032     }
10033 #if defined(TARGET_PPC64)
10034     tcg_gen_xori_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)], 0x8000000000000000LL);
10035 #else
10036     tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)]);
10037     tcg_gen_xori_tl(cpu_gprh[rD(ctx->opcode)], cpu_gprh[rA(ctx->opcode)], 0x80000000);
10038 #endif
10039 }
10040
10041 /* Conversion */
10042 GEN_SPEFPUOP_CONV_64_32(efdcfui);
10043 GEN_SPEFPUOP_CONV_64_32(efdcfsi);
10044 GEN_SPEFPUOP_CONV_64_32(efdcfuf);
10045 GEN_SPEFPUOP_CONV_64_32(efdcfsf);
10046 GEN_SPEFPUOP_CONV_32_64(efdctui);
10047 GEN_SPEFPUOP_CONV_32_64(efdctsi);
10048 GEN_SPEFPUOP_CONV_32_64(efdctuf);
10049 GEN_SPEFPUOP_CONV_32_64(efdctsf);
10050 GEN_SPEFPUOP_CONV_32_64(efdctuiz);
10051 GEN_SPEFPUOP_CONV_32_64(efdctsiz);
10052 GEN_SPEFPUOP_CONV_64_32(efdcfs);
10053 GEN_SPEFPUOP_CONV_64_64(efdcfuid);
10054 GEN_SPEFPUOP_CONV_64_64(efdcfsid);
10055 GEN_SPEFPUOP_CONV_64_64(efdctuidz);
10056 GEN_SPEFPUOP_CONV_64_64(efdctsidz);
10057
10058 /* Comparison */
10059 GEN_SPEFPUOP_COMP_64(efdcmpgt);
10060 GEN_SPEFPUOP_COMP_64(efdcmplt);
10061 GEN_SPEFPUOP_COMP_64(efdcmpeq);
10062 GEN_SPEFPUOP_COMP_64(efdtstgt);
10063 GEN_SPEFPUOP_COMP_64(efdtstlt);
10064 GEN_SPEFPUOP_COMP_64(efdtsteq);
10065
10066 /* Opcodes definitions */
10067 GEN_SPE(efdadd,    efdsub,    0x10, 0x0B, 0x00000000, 0x00000000, PPC_SPE_DOUBLE); //
10068 GEN_SPE(efdcfuid,  efdcfsid,  0x11, 0x0B, 0x00180000, 0x00180000, PPC_SPE_DOUBLE); //
10069 GEN_SPE(efdabs,    efdnabs,   0x12, 0x0B, 0x0000F800, 0x0000F800, PPC_SPE_DOUBLE); //
10070 GEN_SPE(efdneg,    speundef,  0x13, 0x0B, 0x0000F800, 0xFFFFFFFF, PPC_SPE_DOUBLE); //
10071 GEN_SPE(efdmul,    efddiv,    0x14, 0x0B, 0x00000000, 0x00000000, PPC_SPE_DOUBLE); //
10072 GEN_SPE(efdctuidz, efdctsidz, 0x15, 0x0B, 0x00180000, 0x00180000, PPC_SPE_DOUBLE); //
10073 GEN_SPE(efdcmpgt,  efdcmplt,  0x16, 0x0B, 0x00600000, 0x00600000, PPC_SPE_DOUBLE); //
10074 GEN_SPE(efdcmpeq,  efdcfs,    0x17, 0x0B, 0x00600000, 0x00180000, PPC_SPE_DOUBLE); //
10075 GEN_SPE(efdcfui,   efdcfsi,   0x18, 0x0B, 0x00180000, 0x00180000, PPC_SPE_DOUBLE); //
10076 GEN_SPE(efdcfuf,   efdcfsf,   0x19, 0x0B, 0x00180000, 0x00180000, PPC_SPE_DOUBLE); //
10077 GEN_SPE(efdctui,   efdctsi,   0x1A, 0x0B, 0x00180000, 0x00180000, PPC_SPE_DOUBLE); //
10078 GEN_SPE(efdctuf,   efdctsf,   0x1B, 0x0B, 0x00180000, 0x00180000, PPC_SPE_DOUBLE); //
10079 GEN_SPE(efdctuiz,  speundef,  0x1C, 0x0B, 0x00180000, 0xFFFFFFFF, PPC_SPE_DOUBLE); //
10080 GEN_SPE(efdctsiz,  speundef,  0x1D, 0x0B, 0x00180000, 0xFFFFFFFF, PPC_SPE_DOUBLE); //
10081 GEN_SPE(efdtstgt,  efdtstlt,  0x1E, 0x0B, 0x00600000, 0x00600000, PPC_SPE_DOUBLE); //
10082 GEN_SPE(efdtsteq,  speundef,  0x1F, 0x0B, 0x00600000, 0xFFFFFFFF, PPC_SPE_DOUBLE); //
10083
10084 static opcode_t opcodes[] = {
10085 GEN_HANDLER(invalid, 0x00, 0x00, 0x00, 0xFFFFFFFF, PPC_NONE),
10086 GEN_HANDLER(cmp, 0x1F, 0x00, 0x00, 0x00400000, PPC_INTEGER),
10087 GEN_HANDLER(cmpi, 0x0B, 0xFF, 0xFF, 0x00400000, PPC_INTEGER),
10088 GEN_HANDLER(cmpl, 0x1F, 0x00, 0x01, 0x00400000, PPC_INTEGER),
10089 GEN_HANDLER(cmpli, 0x0A, 0xFF, 0xFF, 0x00400000, PPC_INTEGER),
10090 GEN_HANDLER_E(cmpb, 0x1F, 0x1C, 0x0F, 0x00000001, PPC_NONE, PPC2_ISA205),
10091 GEN_HANDLER(isel, 0x1F, 0x0F, 0xFF, 0x00000001, PPC_ISEL),
10092 GEN_HANDLER(addi, 0x0E, 0xFF, 0xFF, 0x00000000, PPC_INTEGER),
10093 GEN_HANDLER(addic, 0x0C, 0xFF, 0xFF, 0x00000000, PPC_INTEGER),
10094 GEN_HANDLER2(addic_, "addic.", 0x0D, 0xFF, 0xFF, 0x00000000, PPC_INTEGER),
10095 GEN_HANDLER(addis, 0x0F, 0xFF, 0xFF, 0x00000000, PPC_INTEGER),
10096 GEN_HANDLER(mulhw, 0x1F, 0x0B, 0x02, 0x00000400, PPC_INTEGER),
10097 GEN_HANDLER(mulhwu, 0x1F, 0x0B, 0x00, 0x00000400, PPC_INTEGER),
10098 GEN_HANDLER(mullw, 0x1F, 0x0B, 0x07, 0x00000000, PPC_INTEGER),
10099 GEN_HANDLER(mullwo, 0x1F, 0x0B, 0x17, 0x00000000, PPC_INTEGER),
10100 GEN_HANDLER(mulli, 0x07, 0xFF, 0xFF, 0x00000000, PPC_INTEGER),
10101 #if defined(TARGET_PPC64)
10102 GEN_HANDLER(mulld, 0x1F, 0x09, 0x07, 0x00000000, PPC_64B),
10103 #endif
10104 GEN_HANDLER(neg, 0x1F, 0x08, 0x03, 0x0000F800, PPC_INTEGER),
10105 GEN_HANDLER(nego, 0x1F, 0x08, 0x13, 0x0000F800, PPC_INTEGER),
10106 GEN_HANDLER(subfic, 0x08, 0xFF, 0xFF, 0x00000000, PPC_INTEGER),
10107 GEN_HANDLER2(andi_, "andi.", 0x1C, 0xFF, 0xFF, 0x00000000, PPC_INTEGER),
10108 GEN_HANDLER2(andis_, "andis.", 0x1D, 0xFF, 0xFF, 0x00000000, PPC_INTEGER),
10109 GEN_HANDLER(cntlzw, 0x1F, 0x1A, 0x00, 0x00000000, PPC_INTEGER),
10110 GEN_HANDLER(or, 0x1F, 0x1C, 0x0D, 0x00000000, PPC_INTEGER),
10111 GEN_HANDLER(xor, 0x1F, 0x1C, 0x09, 0x00000000, PPC_INTEGER),
10112 GEN_HANDLER(ori, 0x18, 0xFF, 0xFF, 0x00000000, PPC_INTEGER),
10113 GEN_HANDLER(oris, 0x19, 0xFF, 0xFF, 0x00000000, PPC_INTEGER),
10114 GEN_HANDLER(xori, 0x1A, 0xFF, 0xFF, 0x00000000, PPC_INTEGER),
10115 GEN_HANDLER(xoris, 0x1B, 0xFF, 0xFF, 0x00000000, PPC_INTEGER),
10116 GEN_HANDLER(popcntb, 0x1F, 0x03, 0x03, 0x0000F801, PPC_POPCNTB),
10117 GEN_HANDLER(popcntw, 0x1F, 0x1A, 0x0b, 0x0000F801, PPC_POPCNTWD),
10118 GEN_HANDLER_E(prtyw, 0x1F, 0x1A, 0x04, 0x0000F801, PPC_NONE, PPC2_ISA205),
10119 #if defined(TARGET_PPC64)
10120 GEN_HANDLER(popcntd, 0x1F, 0x1A, 0x0F, 0x0000F801, PPC_POPCNTWD),
10121 GEN_HANDLER(cntlzd, 0x1F, 0x1A, 0x01, 0x00000000, PPC_64B),
10122 GEN_HANDLER_E(prtyd, 0x1F, 0x1A, 0x05, 0x0000F801, PPC_NONE, PPC2_ISA205),
10123 GEN_HANDLER_E(bpermd, 0x1F, 0x1C, 0x07, 0x00000001, PPC_NONE, PPC2_PERM_ISA206),
10124 #endif
10125 GEN_HANDLER(rlwimi, 0x14, 0xFF, 0xFF, 0x00000000, PPC_INTEGER),
10126 GEN_HANDLER(rlwinm, 0x15, 0xFF, 0xFF, 0x00000000, PPC_INTEGER),
10127 GEN_HANDLER(rlwnm, 0x17, 0xFF, 0xFF, 0x00000000, PPC_INTEGER),
10128 GEN_HANDLER(slw, 0x1F, 0x18, 0x00, 0x00000000, PPC_INTEGER),
10129 GEN_HANDLER(sraw, 0x1F, 0x18, 0x18, 0x00000000, PPC_INTEGER),
10130 GEN_HANDLER(srawi, 0x1F, 0x18, 0x19, 0x00000000, PPC_INTEGER),
10131 GEN_HANDLER(srw, 0x1F, 0x18, 0x10, 0x00000000, PPC_INTEGER),
10132 #if defined(TARGET_PPC64)
10133 GEN_HANDLER(sld, 0x1F, 0x1B, 0x00, 0x00000000, PPC_64B),
10134 GEN_HANDLER(srad, 0x1F, 0x1A, 0x18, 0x00000000, PPC_64B),
10135 GEN_HANDLER2(sradi0, "sradi", 0x1F, 0x1A, 0x19, 0x00000000, PPC_64B),
10136 GEN_HANDLER2(sradi1, "sradi", 0x1F, 0x1B, 0x19, 0x00000000, PPC_64B),
10137 GEN_HANDLER(srd, 0x1F, 0x1B, 0x10, 0x00000000, PPC_64B),
10138 #endif
10139 GEN_HANDLER(frsqrtes, 0x3B, 0x1A, 0xFF, 0x001F07C0, PPC_FLOAT_FRSQRTES),
10140 GEN_HANDLER(fsqrt, 0x3F, 0x16, 0xFF, 0x001F07C0, PPC_FLOAT_FSQRT),
10141 GEN_HANDLER(fsqrts, 0x3B, 0x16, 0xFF, 0x001F07C0, PPC_FLOAT_FSQRT),
10142 GEN_HANDLER(fcmpo, 0x3F, 0x00, 0x01, 0x00600001, PPC_FLOAT),
10143 GEN_HANDLER(fcmpu, 0x3F, 0x00, 0x00, 0x00600001, PPC_FLOAT),
10144 GEN_HANDLER(fabs, 0x3F, 0x08, 0x08, 0x001F0000, PPC_FLOAT),
10145 GEN_HANDLER(fmr, 0x3F, 0x08, 0x02, 0x001F0000, PPC_FLOAT),
10146 GEN_HANDLER(fnabs, 0x3F, 0x08, 0x04, 0x001F0000, PPC_FLOAT),
10147 GEN_HANDLER(fneg, 0x3F, 0x08, 0x01, 0x001F0000, PPC_FLOAT),
10148 GEN_HANDLER_E(fcpsgn, 0x3F, 0x08, 0x00, 0x00000000, PPC_NONE, PPC2_ISA205),
10149 GEN_HANDLER_E(fmrgew, 0x3F, 0x06, 0x1E, 0x00000001, PPC_NONE, PPC2_VSX207),
10150 GEN_HANDLER_E(fmrgow, 0x3F, 0x06, 0x1A, 0x00000001, PPC_NONE, PPC2_VSX207),
10151 GEN_HANDLER(mcrfs, 0x3F, 0x00, 0x02, 0x0063F801, PPC_FLOAT),
10152 GEN_HANDLER(mffs, 0x3F, 0x07, 0x12, 0x001FF800, PPC_FLOAT),
10153 GEN_HANDLER(mtfsb0, 0x3F, 0x06, 0x02, 0x001FF800, PPC_FLOAT),
10154 GEN_HANDLER(mtfsb1, 0x3F, 0x06, 0x01, 0x001FF800, PPC_FLOAT),
10155 GEN_HANDLER(mtfsf, 0x3F, 0x07, 0x16, 0x00000000, PPC_FLOAT),
10156 GEN_HANDLER(mtfsfi, 0x3F, 0x06, 0x04, 0x006e0800, PPC_FLOAT),
10157 #if defined(TARGET_PPC64)
10158 GEN_HANDLER(ld, 0x3A, 0xFF, 0xFF, 0x00000000, PPC_64B),
10159 GEN_HANDLER(lq, 0x38, 0xFF, 0xFF, 0x00000000, PPC_64BX),
10160 GEN_HANDLER(std, 0x3E, 0xFF, 0xFF, 0x00000000, PPC_64B),
10161 #endif
10162 GEN_HANDLER(lmw, 0x2E, 0xFF, 0xFF, 0x00000000, PPC_INTEGER),
10163 GEN_HANDLER(stmw, 0x2F, 0xFF, 0xFF, 0x00000000, PPC_INTEGER),
10164 GEN_HANDLER(lswi, 0x1F, 0x15, 0x12, 0x00000001, PPC_STRING),
10165 GEN_HANDLER(lswx, 0x1F, 0x15, 0x10, 0x00000001, PPC_STRING),
10166 GEN_HANDLER(stswi, 0x1F, 0x15, 0x16, 0x00000001, PPC_STRING),
10167 GEN_HANDLER(stswx, 0x1F, 0x15, 0x14, 0x00000001, PPC_STRING),
10168 GEN_HANDLER(eieio, 0x1F, 0x16, 0x1A, 0x03FFF801, PPC_MEM_EIEIO),
10169 GEN_HANDLER(isync, 0x13, 0x16, 0x04, 0x03FFF801, PPC_MEM),
10170 GEN_HANDLER_E(lbarx, 0x1F, 0x14, 0x01, 0, PPC_NONE, PPC2_ATOMIC_ISA206),
10171 GEN_HANDLER_E(lharx, 0x1F, 0x14, 0x03, 0, PPC_NONE, PPC2_ATOMIC_ISA206),
10172 GEN_HANDLER(lwarx, 0x1F, 0x14, 0x00, 0x00000000, PPC_RES),
10173 GEN_HANDLER_E(stbcx_, 0x1F, 0x16, 0x15, 0, PPC_NONE, PPC2_ATOMIC_ISA206),
10174 GEN_HANDLER_E(sthcx_, 0x1F, 0x16, 0x16, 0, PPC_NONE, PPC2_ATOMIC_ISA206),
10175 GEN_HANDLER2(stwcx_, "stwcx.", 0x1F, 0x16, 0x04, 0x00000000, PPC_RES),
10176 #if defined(TARGET_PPC64)
10177 GEN_HANDLER(ldarx, 0x1F, 0x14, 0x02, 0x00000000, PPC_64B),
10178 GEN_HANDLER_E(lqarx, 0x1F, 0x14, 0x08, 0, PPC_NONE, PPC2_LSQ_ISA207),
10179 GEN_HANDLER2(stdcx_, "stdcx.", 0x1F, 0x16, 0x06, 0x00000000, PPC_64B),
10180 GEN_HANDLER_E(stqcx_, 0x1F, 0x16, 0x05, 0, PPC_NONE, PPC2_LSQ_ISA207),
10181 #endif
10182 GEN_HANDLER(sync, 0x1F, 0x16, 0x12, 0x039FF801, PPC_MEM_SYNC),
10183 GEN_HANDLER(wait, 0x1F, 0x1E, 0x01, 0x03FFF801, PPC_WAIT),
10184 GEN_HANDLER(b, 0x12, 0xFF, 0xFF, 0x00000000, PPC_FLOW),
10185 GEN_HANDLER(bc, 0x10, 0xFF, 0xFF, 0x00000000, PPC_FLOW),
10186 GEN_HANDLER(bcctr, 0x13, 0x10, 0x10, 0x00000000, PPC_FLOW),
10187 GEN_HANDLER(bclr, 0x13, 0x10, 0x00, 0x00000000, PPC_FLOW),
10188 GEN_HANDLER_E(bctar, 0x13, 0x10, 0x11, 0, PPC_NONE, PPC2_BCTAR_ISA207),
10189 GEN_HANDLER(mcrf, 0x13, 0x00, 0xFF, 0x00000001, PPC_INTEGER),
10190 GEN_HANDLER(rfi, 0x13, 0x12, 0x01, 0x03FF8001, PPC_FLOW),
10191 #if defined(TARGET_PPC64)
10192 GEN_HANDLER(rfid, 0x13, 0x12, 0x00, 0x03FF8001, PPC_64B),
10193 GEN_HANDLER(hrfid, 0x13, 0x12, 0x08, 0x03FF8001, PPC_64H),
10194 #endif
10195 GEN_HANDLER(sc, 0x11, 0xFF, 0xFF, 0x03FFF01D, PPC_FLOW),
10196 GEN_HANDLER(tw, 0x1F, 0x04, 0x00, 0x00000001, PPC_FLOW),
10197 GEN_HANDLER(twi, 0x03, 0xFF, 0xFF, 0x00000000, PPC_FLOW),
10198 #if defined(TARGET_PPC64)
10199 GEN_HANDLER(td, 0x1F, 0x04, 0x02, 0x00000001, PPC_64B),
10200 GEN_HANDLER(tdi, 0x02, 0xFF, 0xFF, 0x00000000, PPC_64B),
10201 #endif
10202 GEN_HANDLER(mcrxr, 0x1F, 0x00, 0x10, 0x007FF801, PPC_MISC),
10203 GEN_HANDLER(mfcr, 0x1F, 0x13, 0x00, 0x00000801, PPC_MISC),
10204 GEN_HANDLER(mfmsr, 0x1F, 0x13, 0x02, 0x001FF801, PPC_MISC),
10205 GEN_HANDLER(mfspr, 0x1F, 0x13, 0x0A, 0x00000001, PPC_MISC),
10206 GEN_HANDLER(mftb, 0x1F, 0x13, 0x0B, 0x00000001, PPC_MFTB),
10207 GEN_HANDLER(mtcrf, 0x1F, 0x10, 0x04, 0x00000801, PPC_MISC),
10208 #if defined(TARGET_PPC64)
10209 GEN_HANDLER(mtmsrd, 0x1F, 0x12, 0x05, 0x001EF801, PPC_64B),
10210 #endif
10211 GEN_HANDLER(mtmsr, 0x1F, 0x12, 0x04, 0x001FF801, PPC_MISC),
10212 GEN_HANDLER(mtspr, 0x1F, 0x13, 0x0E, 0x00000001, PPC_MISC),
10213 GEN_HANDLER(dcbf, 0x1F, 0x16, 0x02, 0x03C00001, PPC_CACHE),
10214 GEN_HANDLER(dcbi, 0x1F, 0x16, 0x0E, 0x03E00001, PPC_CACHE),
10215 GEN_HANDLER(dcbst, 0x1F, 0x16, 0x01, 0x03E00001, PPC_CACHE),
10216 GEN_HANDLER(dcbt, 0x1F, 0x16, 0x08, 0x00000001, PPC_CACHE),
10217 GEN_HANDLER(dcbtst, 0x1F, 0x16, 0x07, 0x00000001, PPC_CACHE),
10218 GEN_HANDLER(dcbz, 0x1F, 0x16, 0x1F, 0x03C00001, PPC_CACHE_DCBZ),
10219 GEN_HANDLER(dst, 0x1F, 0x16, 0x0A, 0x01800001, PPC_ALTIVEC),
10220 GEN_HANDLER(dstst, 0x1F, 0x16, 0x0B, 0x02000001, PPC_ALTIVEC),
10221 GEN_HANDLER(dss, 0x1F, 0x16, 0x19, 0x019FF801, PPC_ALTIVEC),
10222 GEN_HANDLER(icbi, 0x1F, 0x16, 0x1E, 0x03E00001, PPC_CACHE_ICBI),
10223 GEN_HANDLER(dcba, 0x1F, 0x16, 0x17, 0x03E00001, PPC_CACHE_DCBA),
10224 GEN_HANDLER(mfsr, 0x1F, 0x13, 0x12, 0x0010F801, PPC_SEGMENT),
10225 GEN_HANDLER(mfsrin, 0x1F, 0x13, 0x14, 0x001F0001, PPC_SEGMENT),
10226 GEN_HANDLER(mtsr, 0x1F, 0x12, 0x06, 0x0010F801, PPC_SEGMENT),
10227 GEN_HANDLER(mtsrin, 0x1F, 0x12, 0x07, 0x001F0001, PPC_SEGMENT),
10228 #if defined(TARGET_PPC64)
10229 GEN_HANDLER2(mfsr_64b, "mfsr", 0x1F, 0x13, 0x12, 0x0010F801, PPC_SEGMENT_64B),
10230 GEN_HANDLER2(mfsrin_64b, "mfsrin", 0x1F, 0x13, 0x14, 0x001F0001,
10231              PPC_SEGMENT_64B),
10232 GEN_HANDLER2(mtsr_64b, "mtsr", 0x1F, 0x12, 0x06, 0x0010F801, PPC_SEGMENT_64B),
10233 GEN_HANDLER2(mtsrin_64b, "mtsrin", 0x1F, 0x12, 0x07, 0x001F0001,
10234              PPC_SEGMENT_64B),
10235 GEN_HANDLER2(slbmte, "slbmte", 0x1F, 0x12, 0x0C, 0x001F0001, PPC_SEGMENT_64B),
10236 GEN_HANDLER2(slbmfee, "slbmfee", 0x1F, 0x13, 0x1C, 0x001F0001, PPC_SEGMENT_64B),
10237 GEN_HANDLER2(slbmfev, "slbmfev", 0x1F, 0x13, 0x1A, 0x001F0001, PPC_SEGMENT_64B),
10238 #endif
10239 GEN_HANDLER(tlbia, 0x1F, 0x12, 0x0B, 0x03FFFC01, PPC_MEM_TLBIA),
10240 GEN_HANDLER(tlbiel, 0x1F, 0x12, 0x08, 0x03FF0001, PPC_MEM_TLBIE),
10241 GEN_HANDLER(tlbie, 0x1F, 0x12, 0x09, 0x03FF0001, PPC_MEM_TLBIE),
10242 GEN_HANDLER(tlbsync, 0x1F, 0x16, 0x11, 0x03FFF801, PPC_MEM_TLBSYNC),
10243 #if defined(TARGET_PPC64)
10244 GEN_HANDLER(slbia, 0x1F, 0x12, 0x0F, 0x03FFFC01, PPC_SLBI),
10245 GEN_HANDLER(slbie, 0x1F, 0x12, 0x0D, 0x03FF0001, PPC_SLBI),
10246 #endif
10247 GEN_HANDLER(eciwx, 0x1F, 0x16, 0x0D, 0x00000001, PPC_EXTERN),
10248 GEN_HANDLER(ecowx, 0x1F, 0x16, 0x09, 0x00000001, PPC_EXTERN),
10249 GEN_HANDLER(abs, 0x1F, 0x08, 0x0B, 0x0000F800, PPC_POWER_BR),
10250 GEN_HANDLER(abso, 0x1F, 0x08, 0x1B, 0x0000F800, PPC_POWER_BR),
10251 GEN_HANDLER(clcs, 0x1F, 0x10, 0x13, 0x0000F800, PPC_POWER_BR),
10252 GEN_HANDLER(div, 0x1F, 0x0B, 0x0A, 0x00000000, PPC_POWER_BR),
10253 GEN_HANDLER(divo, 0x1F, 0x0B, 0x1A, 0x00000000, PPC_POWER_BR),
10254 GEN_HANDLER(divs, 0x1F, 0x0B, 0x0B, 0x00000000, PPC_POWER_BR),
10255 GEN_HANDLER(divso, 0x1F, 0x0B, 0x1B, 0x00000000, PPC_POWER_BR),
10256 GEN_HANDLER(doz, 0x1F, 0x08, 0x08, 0x00000000, PPC_POWER_BR),
10257 GEN_HANDLER(dozo, 0x1F, 0x08, 0x18, 0x00000000, PPC_POWER_BR),
10258 GEN_HANDLER(dozi, 0x09, 0xFF, 0xFF, 0x00000000, PPC_POWER_BR),
10259 GEN_HANDLER(lscbx, 0x1F, 0x15, 0x08, 0x00000000, PPC_POWER_BR),
10260 GEN_HANDLER(maskg, 0x1F, 0x1D, 0x00, 0x00000000, PPC_POWER_BR),
10261 GEN_HANDLER(maskir, 0x1F, 0x1D, 0x10, 0x00000000, PPC_POWER_BR),
10262 GEN_HANDLER(mul, 0x1F, 0x0B, 0x03, 0x00000000, PPC_POWER_BR),
10263 GEN_HANDLER(mulo, 0x1F, 0x0B, 0x13, 0x00000000, PPC_POWER_BR),
10264 GEN_HANDLER(nabs, 0x1F, 0x08, 0x0F, 0x00000000, PPC_POWER_BR),
10265 GEN_HANDLER(nabso, 0x1F, 0x08, 0x1F, 0x00000000, PPC_POWER_BR),
10266 GEN_HANDLER(rlmi, 0x16, 0xFF, 0xFF, 0x00000000, PPC_POWER_BR),
10267 GEN_HANDLER(rrib, 0x1F, 0x19, 0x10, 0x00000000, PPC_POWER_BR),
10268 GEN_HANDLER(sle, 0x1F, 0x19, 0x04, 0x00000000, PPC_POWER_BR),
10269 GEN_HANDLER(sleq, 0x1F, 0x19, 0x06, 0x00000000, PPC_POWER_BR),
10270 GEN_HANDLER(sliq, 0x1F, 0x18, 0x05, 0x00000000, PPC_POWER_BR),
10271 GEN_HANDLER(slliq, 0x1F, 0x18, 0x07, 0x00000000, PPC_POWER_BR),
10272 GEN_HANDLER(sllq, 0x1F, 0x18, 0x06, 0x00000000, PPC_POWER_BR),
10273 GEN_HANDLER(slq, 0x1F, 0x18, 0x04, 0x00000000, PPC_POWER_BR),
10274 GEN_HANDLER(sraiq, 0x1F, 0x18, 0x1D, 0x00000000, PPC_POWER_BR),
10275 GEN_HANDLER(sraq, 0x1F, 0x18, 0x1C, 0x00000000, PPC_POWER_BR),
10276 GEN_HANDLER(sre, 0x1F, 0x19, 0x14, 0x00000000, PPC_POWER_BR),
10277 GEN_HANDLER(srea, 0x1F, 0x19, 0x1C, 0x00000000, PPC_POWER_BR),
10278 GEN_HANDLER(sreq, 0x1F, 0x19, 0x16, 0x00000000, PPC_POWER_BR),
10279 GEN_HANDLER(sriq, 0x1F, 0x18, 0x15, 0x00000000, PPC_POWER_BR),
10280 GEN_HANDLER(srliq, 0x1F, 0x18, 0x17, 0x00000000, PPC_POWER_BR),
10281 GEN_HANDLER(srlq, 0x1F, 0x18, 0x16, 0x00000000, PPC_POWER_BR),
10282 GEN_HANDLER(srq, 0x1F, 0x18, 0x14, 0x00000000, PPC_POWER_BR),
10283 GEN_HANDLER(dsa, 0x1F, 0x14, 0x13, 0x03FFF801, PPC_602_SPEC),
10284 GEN_HANDLER(esa, 0x1F, 0x14, 0x12, 0x03FFF801, PPC_602_SPEC),
10285 GEN_HANDLER(mfrom, 0x1F, 0x09, 0x08, 0x03E0F801, PPC_602_SPEC),
10286 GEN_HANDLER2(tlbld_6xx, "tlbld", 0x1F, 0x12, 0x1E, 0x03FF0001, PPC_6xx_TLB),
10287 GEN_HANDLER2(tlbli_6xx, "tlbli", 0x1F, 0x12, 0x1F, 0x03FF0001, PPC_6xx_TLB),
10288 GEN_HANDLER2(tlbld_74xx, "tlbld", 0x1F, 0x12, 0x1E, 0x03FF0001, PPC_74xx_TLB),
10289 GEN_HANDLER2(tlbli_74xx, "tlbli", 0x1F, 0x12, 0x1F, 0x03FF0001, PPC_74xx_TLB),
10290 GEN_HANDLER(clf, 0x1F, 0x16, 0x03, 0x03E00000, PPC_POWER),
10291 GEN_HANDLER(cli, 0x1F, 0x16, 0x0F, 0x03E00000, PPC_POWER),
10292 GEN_HANDLER(dclst, 0x1F, 0x16, 0x13, 0x03E00000, PPC_POWER),
10293 GEN_HANDLER(mfsri, 0x1F, 0x13, 0x13, 0x00000001, PPC_POWER),
10294 GEN_HANDLER(rac, 0x1F, 0x12, 0x19, 0x00000001, PPC_POWER),
10295 GEN_HANDLER(rfsvc, 0x13, 0x12, 0x02, 0x03FFF0001, PPC_POWER),
10296 GEN_HANDLER(lfq, 0x38, 0xFF, 0xFF, 0x00000003, PPC_POWER2),
10297 GEN_HANDLER(lfqu, 0x39, 0xFF, 0xFF, 0x00000003, PPC_POWER2),
10298 GEN_HANDLER(lfqux, 0x1F, 0x17, 0x19, 0x00000001, PPC_POWER2),
10299 GEN_HANDLER(lfqx, 0x1F, 0x17, 0x18, 0x00000001, PPC_POWER2),
10300 GEN_HANDLER(stfq, 0x3C, 0xFF, 0xFF, 0x00000003, PPC_POWER2),
10301 GEN_HANDLER(stfqu, 0x3D, 0xFF, 0xFF, 0x00000003, PPC_POWER2),
10302 GEN_HANDLER(stfqux, 0x1F, 0x17, 0x1D, 0x00000001, PPC_POWER2),
10303 GEN_HANDLER(stfqx, 0x1F, 0x17, 0x1C, 0x00000001, PPC_POWER2),
10304 GEN_HANDLER(mfapidi, 0x1F, 0x13, 0x08, 0x0000F801, PPC_MFAPIDI),
10305 GEN_HANDLER(tlbiva, 0x1F, 0x12, 0x18, 0x03FFF801, PPC_TLBIVA),
10306 GEN_HANDLER(mfdcr, 0x1F, 0x03, 0x0A, 0x00000001, PPC_DCR),
10307 GEN_HANDLER(mtdcr, 0x1F, 0x03, 0x0E, 0x00000001, PPC_DCR),
10308 GEN_HANDLER(mfdcrx, 0x1F, 0x03, 0x08, 0x00000000, PPC_DCRX),
10309 GEN_HANDLER(mtdcrx, 0x1F, 0x03, 0x0C, 0x00000000, PPC_DCRX),
10310 GEN_HANDLER(mfdcrux, 0x1F, 0x03, 0x09, 0x00000000, PPC_DCRUX),
10311 GEN_HANDLER(mtdcrux, 0x1F, 0x03, 0x0D, 0x00000000, PPC_DCRUX),
10312 GEN_HANDLER(dccci, 0x1F, 0x06, 0x0E, 0x03E00001, PPC_4xx_COMMON),
10313 GEN_HANDLER(dcread, 0x1F, 0x06, 0x0F, 0x00000001, PPC_4xx_COMMON),
10314 GEN_HANDLER2(icbt_40x, "icbt", 0x1F, 0x06, 0x08, 0x03E00001, PPC_40x_ICBT),
10315 GEN_HANDLER(iccci, 0x1F, 0x06, 0x1E, 0x00000001, PPC_4xx_COMMON),
10316 GEN_HANDLER(icread, 0x1F, 0x06, 0x1F, 0x03E00001, PPC_4xx_COMMON),
10317 GEN_HANDLER2(rfci_40x, "rfci", 0x13, 0x13, 0x01, 0x03FF8001, PPC_40x_EXCP),
10318 GEN_HANDLER_E(rfci, 0x13, 0x13, 0x01, 0x03FF8001, PPC_BOOKE, PPC2_BOOKE206),
10319 GEN_HANDLER(rfdi, 0x13, 0x07, 0x01, 0x03FF8001, PPC_RFDI),
10320 GEN_HANDLER(rfmci, 0x13, 0x06, 0x01, 0x03FF8001, PPC_RFMCI),
10321 GEN_HANDLER2(tlbre_40x, "tlbre", 0x1F, 0x12, 0x1D, 0x00000001, PPC_40x_TLB),
10322 GEN_HANDLER2(tlbsx_40x, "tlbsx", 0x1F, 0x12, 0x1C, 0x00000000, PPC_40x_TLB),
10323 GEN_HANDLER2(tlbwe_40x, "tlbwe", 0x1F, 0x12, 0x1E, 0x00000001, PPC_40x_TLB),
10324 GEN_HANDLER2(tlbre_440, "tlbre", 0x1F, 0x12, 0x1D, 0x00000001, PPC_BOOKE),
10325 GEN_HANDLER2(tlbsx_440, "tlbsx", 0x1F, 0x12, 0x1C, 0x00000000, PPC_BOOKE),
10326 GEN_HANDLER2(tlbwe_440, "tlbwe", 0x1F, 0x12, 0x1E, 0x00000001, PPC_BOOKE),
10327 GEN_HANDLER2_E(tlbre_booke206, "tlbre", 0x1F, 0x12, 0x1D, 0x00000001,
10328                PPC_NONE, PPC2_BOOKE206),
10329 GEN_HANDLER2_E(tlbsx_booke206, "tlbsx", 0x1F, 0x12, 0x1C, 0x00000000,
10330                PPC_NONE, PPC2_BOOKE206),
10331 GEN_HANDLER2_E(tlbwe_booke206, "tlbwe", 0x1F, 0x12, 0x1E, 0x00000001,
10332                PPC_NONE, PPC2_BOOKE206),
10333 GEN_HANDLER2_E(tlbivax_booke206, "tlbivax", 0x1F, 0x12, 0x18, 0x00000001,
10334                PPC_NONE, PPC2_BOOKE206),
10335 GEN_HANDLER2_E(tlbilx_booke206, "tlbilx", 0x1F, 0x12, 0x00, 0x03800001,
10336                PPC_NONE, PPC2_BOOKE206),
10337 GEN_HANDLER2_E(msgsnd, "msgsnd", 0x1F, 0x0E, 0x06, 0x03ff0001,
10338                PPC_NONE, PPC2_PRCNTL),
10339 GEN_HANDLER2_E(msgclr, "msgclr", 0x1F, 0x0E, 0x07, 0x03ff0001,
10340                PPC_NONE, PPC2_PRCNTL),
10341 GEN_HANDLER(wrtee, 0x1F, 0x03, 0x04, 0x000FFC01, PPC_WRTEE),
10342 GEN_HANDLER(wrteei, 0x1F, 0x03, 0x05, 0x000E7C01, PPC_WRTEE),
10343 GEN_HANDLER(dlmzb, 0x1F, 0x0E, 0x02, 0x00000000, PPC_440_SPEC),
10344 GEN_HANDLER_E(mbar, 0x1F, 0x16, 0x1a, 0x001FF801,
10345               PPC_BOOKE, PPC2_BOOKE206),
10346 GEN_HANDLER(msync_4xx, 0x1F, 0x16, 0x12, 0x03FFF801, PPC_BOOKE),
10347 GEN_HANDLER2_E(icbt_440, "icbt", 0x1F, 0x16, 0x00, 0x03E00001,
10348                PPC_BOOKE, PPC2_BOOKE206),
10349 GEN_HANDLER(lvsl, 0x1f, 0x06, 0x00, 0x00000001, PPC_ALTIVEC),
10350 GEN_HANDLER(lvsr, 0x1f, 0x06, 0x01, 0x00000001, PPC_ALTIVEC),
10351 GEN_HANDLER(mfvscr, 0x04, 0x2, 0x18, 0x001ff800, PPC_ALTIVEC),
10352 GEN_HANDLER(mtvscr, 0x04, 0x2, 0x19, 0x03ff0000, PPC_ALTIVEC),
10353 GEN_HANDLER(vmladduhm, 0x04, 0x11, 0xFF, 0x00000000, PPC_ALTIVEC),
10354 GEN_HANDLER2(evsel0, "evsel", 0x04, 0x1c, 0x09, 0x00000000, PPC_SPE),
10355 GEN_HANDLER2(evsel1, "evsel", 0x04, 0x1d, 0x09, 0x00000000, PPC_SPE),
10356 GEN_HANDLER2(evsel2, "evsel", 0x04, 0x1e, 0x09, 0x00000000, PPC_SPE),
10357 GEN_HANDLER2(evsel3, "evsel", 0x04, 0x1f, 0x09, 0x00000000, PPC_SPE),
10358
10359 #undef GEN_INT_ARITH_ADD
10360 #undef GEN_INT_ARITH_ADD_CONST
10361 #define GEN_INT_ARITH_ADD(name, opc3, add_ca, compute_ca, compute_ov)         \
10362 GEN_HANDLER(name, 0x1F, 0x0A, opc3, 0x00000000, PPC_INTEGER),
10363 #define GEN_INT_ARITH_ADD_CONST(name, opc3, const_val,                        \
10364                                 add_ca, compute_ca, compute_ov)               \
10365 GEN_HANDLER(name, 0x1F, 0x0A, opc3, 0x0000F800, PPC_INTEGER),
10366 GEN_INT_ARITH_ADD(add, 0x08, 0, 0, 0)
10367 GEN_INT_ARITH_ADD(addo, 0x18, 0, 0, 1)
10368 GEN_INT_ARITH_ADD(addc, 0x00, 0, 1, 0)
10369 GEN_INT_ARITH_ADD(addco, 0x10, 0, 1, 1)
10370 GEN_INT_ARITH_ADD(adde, 0x04, 1, 1, 0)
10371 GEN_INT_ARITH_ADD(addeo, 0x14, 1, 1, 1)
10372 GEN_INT_ARITH_ADD_CONST(addme, 0x07, -1LL, 1, 1, 0)
10373 GEN_INT_ARITH_ADD_CONST(addmeo, 0x17, -1LL, 1, 1, 1)
10374 GEN_INT_ARITH_ADD_CONST(addze, 0x06, 0, 1, 1, 0)
10375 GEN_INT_ARITH_ADD_CONST(addzeo, 0x16, 0, 1, 1, 1)
10376
10377 #undef GEN_INT_ARITH_DIVW
10378 #define GEN_INT_ARITH_DIVW(name, opc3, sign, compute_ov)                      \
10379 GEN_HANDLER(name, 0x1F, 0x0B, opc3, 0x00000000, PPC_INTEGER)
10380 GEN_INT_ARITH_DIVW(divwu, 0x0E, 0, 0),
10381 GEN_INT_ARITH_DIVW(divwuo, 0x1E, 0, 1),
10382 GEN_INT_ARITH_DIVW(divw, 0x0F, 1, 0),
10383 GEN_INT_ARITH_DIVW(divwo, 0x1F, 1, 1),
10384 GEN_HANDLER_E(divwe, 0x1F, 0x0B, 0x0D, 0, PPC_NONE, PPC2_DIVE_ISA206),
10385 GEN_HANDLER_E(divweo, 0x1F, 0x0B, 0x1D, 0, PPC_NONE, PPC2_DIVE_ISA206),
10386 GEN_HANDLER_E(divweu, 0x1F, 0x0B, 0x0C, 0, PPC_NONE, PPC2_DIVE_ISA206),
10387 GEN_HANDLER_E(divweuo, 0x1F, 0x0B, 0x1C, 0, PPC_NONE, PPC2_DIVE_ISA206),
10388
10389 #if defined(TARGET_PPC64)
10390 #undef GEN_INT_ARITH_DIVD
10391 #define GEN_INT_ARITH_DIVD(name, opc3, sign, compute_ov)                      \
10392 GEN_HANDLER(name, 0x1F, 0x09, opc3, 0x00000000, PPC_64B)
10393 GEN_INT_ARITH_DIVD(divdu, 0x0E, 0, 0),
10394 GEN_INT_ARITH_DIVD(divduo, 0x1E, 0, 1),
10395 GEN_INT_ARITH_DIVD(divd, 0x0F, 1, 0),
10396 GEN_INT_ARITH_DIVD(divdo, 0x1F, 1, 1),
10397
10398 GEN_HANDLER_E(divdeu, 0x1F, 0x09, 0x0C, 0, PPC_NONE, PPC2_DIVE_ISA206),
10399 GEN_HANDLER_E(divdeuo, 0x1F, 0x09, 0x1C, 0, PPC_NONE, PPC2_DIVE_ISA206),
10400 GEN_HANDLER_E(divde, 0x1F, 0x09, 0x0D, 0, PPC_NONE, PPC2_DIVE_ISA206),
10401 GEN_HANDLER_E(divdeo, 0x1F, 0x09, 0x1D, 0, PPC_NONE, PPC2_DIVE_ISA206),
10402
10403 #undef GEN_INT_ARITH_MUL_HELPER
10404 #define GEN_INT_ARITH_MUL_HELPER(name, opc3)                                  \
10405 GEN_HANDLER(name, 0x1F, 0x09, opc3, 0x00000000, PPC_64B)
10406 GEN_INT_ARITH_MUL_HELPER(mulhdu, 0x00),
10407 GEN_INT_ARITH_MUL_HELPER(mulhd, 0x02),
10408 GEN_INT_ARITH_MUL_HELPER(mulldo, 0x17),
10409 #endif
10410
10411 #undef GEN_INT_ARITH_SUBF
10412 #undef GEN_INT_ARITH_SUBF_CONST
10413 #define GEN_INT_ARITH_SUBF(name, opc3, add_ca, compute_ca, compute_ov)        \
10414 GEN_HANDLER(name, 0x1F, 0x08, opc3, 0x00000000, PPC_INTEGER),
10415 #define GEN_INT_ARITH_SUBF_CONST(name, opc3, const_val,                       \
10416                                 add_ca, compute_ca, compute_ov)               \
10417 GEN_HANDLER(name, 0x1F, 0x08, opc3, 0x0000F800, PPC_INTEGER),
10418 GEN_INT_ARITH_SUBF(subf, 0x01, 0, 0, 0)
10419 GEN_INT_ARITH_SUBF(subfo, 0x11, 0, 0, 1)
10420 GEN_INT_ARITH_SUBF(subfc, 0x00, 0, 1, 0)
10421 GEN_INT_ARITH_SUBF(subfco, 0x10, 0, 1, 1)
10422 GEN_INT_ARITH_SUBF(subfe, 0x04, 1, 1, 0)
10423 GEN_INT_ARITH_SUBF(subfeo, 0x14, 1, 1, 1)
10424 GEN_INT_ARITH_SUBF_CONST(subfme, 0x07, -1LL, 1, 1, 0)
10425 GEN_INT_ARITH_SUBF_CONST(subfmeo, 0x17, -1LL, 1, 1, 1)
10426 GEN_INT_ARITH_SUBF_CONST(subfze, 0x06, 0, 1, 1, 0)
10427 GEN_INT_ARITH_SUBF_CONST(subfzeo, 0x16, 0, 1, 1, 1)
10428
10429 #undef GEN_LOGICAL1
10430 #undef GEN_LOGICAL2
10431 #define GEN_LOGICAL2(name, tcg_op, opc, type)                                 \
10432 GEN_HANDLER(name, 0x1F, 0x1C, opc, 0x00000000, type)
10433 #define GEN_LOGICAL1(name, tcg_op, opc, type)                                 \
10434 GEN_HANDLER(name, 0x1F, 0x1A, opc, 0x00000000, type)
10435 GEN_LOGICAL2(and, tcg_gen_and_tl, 0x00, PPC_INTEGER),
10436 GEN_LOGICAL2(andc, tcg_gen_andc_tl, 0x01, PPC_INTEGER),
10437 GEN_LOGICAL2(eqv, tcg_gen_eqv_tl, 0x08, PPC_INTEGER),
10438 GEN_LOGICAL1(extsb, tcg_gen_ext8s_tl, 0x1D, PPC_INTEGER),
10439 GEN_LOGICAL1(extsh, tcg_gen_ext16s_tl, 0x1C, PPC_INTEGER),
10440 GEN_LOGICAL2(nand, tcg_gen_nand_tl, 0x0E, PPC_INTEGER),
10441 GEN_LOGICAL2(nor, tcg_gen_nor_tl, 0x03, PPC_INTEGER),
10442 GEN_LOGICAL2(orc, tcg_gen_orc_tl, 0x0C, PPC_INTEGER),
10443 #if defined(TARGET_PPC64)
10444 GEN_LOGICAL1(extsw, tcg_gen_ext32s_tl, 0x1E, PPC_64B),
10445 #endif
10446
10447 #if defined(TARGET_PPC64)
10448 #undef GEN_PPC64_R2
10449 #undef GEN_PPC64_R4
10450 #define GEN_PPC64_R2(name, opc1, opc2)                                        \
10451 GEN_HANDLER2(name##0, stringify(name), opc1, opc2, 0xFF, 0x00000000, PPC_64B),\
10452 GEN_HANDLER2(name##1, stringify(name), opc1, opc2 | 0x10, 0xFF, 0x00000000,   \
10453              PPC_64B)
10454 #define GEN_PPC64_R4(name, opc1, opc2)                                        \
10455 GEN_HANDLER2(name##0, stringify(name), opc1, opc2, 0xFF, 0x00000000, PPC_64B),\
10456 GEN_HANDLER2(name##1, stringify(name), opc1, opc2 | 0x01, 0xFF, 0x00000000,   \
10457              PPC_64B),                                                        \
10458 GEN_HANDLER2(name##2, stringify(name), opc1, opc2 | 0x10, 0xFF, 0x00000000,   \
10459              PPC_64B),                                                        \
10460 GEN_HANDLER2(name##3, stringify(name), opc1, opc2 | 0x11, 0xFF, 0x00000000,   \
10461              PPC_64B)
10462 GEN_PPC64_R4(rldicl, 0x1E, 0x00),
10463 GEN_PPC64_R4(rldicr, 0x1E, 0x02),
10464 GEN_PPC64_R4(rldic, 0x1E, 0x04),
10465 GEN_PPC64_R2(rldcl, 0x1E, 0x08),
10466 GEN_PPC64_R2(rldcr, 0x1E, 0x09),
10467 GEN_PPC64_R4(rldimi, 0x1E, 0x06),
10468 #endif
10469
10470 #undef _GEN_FLOAT_ACB
10471 #undef GEN_FLOAT_ACB
10472 #undef _GEN_FLOAT_AB
10473 #undef GEN_FLOAT_AB
10474 #undef _GEN_FLOAT_AC
10475 #undef GEN_FLOAT_AC
10476 #undef GEN_FLOAT_B
10477 #undef GEN_FLOAT_BS
10478 #define _GEN_FLOAT_ACB(name, op, op1, op2, isfloat, set_fprf, type)           \
10479 GEN_HANDLER(f##name, op1, op2, 0xFF, 0x00000000, type)
10480 #define GEN_FLOAT_ACB(name, op2, set_fprf, type)                              \
10481 _GEN_FLOAT_ACB(name, name, 0x3F, op2, 0, set_fprf, type),                     \
10482 _GEN_FLOAT_ACB(name##s, name, 0x3B, op2, 1, set_fprf, type)
10483 #define _GEN_FLOAT_AB(name, op, op1, op2, inval, isfloat, set_fprf, type)     \
10484 GEN_HANDLER(f##name, op1, op2, 0xFF, inval, type)
10485 #define GEN_FLOAT_AB(name, op2, inval, set_fprf, type)                        \
10486 _GEN_FLOAT_AB(name, name, 0x3F, op2, inval, 0, set_fprf, type),               \
10487 _GEN_FLOAT_AB(name##s, name, 0x3B, op2, inval, 1, set_fprf, type)
10488 #define _GEN_FLOAT_AC(name, op, op1, op2, inval, isfloat, set_fprf, type)     \
10489 GEN_HANDLER(f##name, op1, op2, 0xFF, inval, type)
10490 #define GEN_FLOAT_AC(name, op2, inval, set_fprf, type)                        \
10491 _GEN_FLOAT_AC(name, name, 0x3F, op2, inval, 0, set_fprf, type),               \
10492 _GEN_FLOAT_AC(name##s, name, 0x3B, op2, inval, 1, set_fprf, type)
10493 #define GEN_FLOAT_B(name, op2, op3, set_fprf, type)                           \
10494 GEN_HANDLER(f##name, 0x3F, op2, op3, 0x001F0000, type)
10495 #define GEN_FLOAT_BS(name, op1, op2, set_fprf, type)                          \
10496 GEN_HANDLER(f##name, op1, op2, 0xFF, 0x001F07C0, type)
10497
10498 GEN_FLOAT_AB(add, 0x15, 0x000007C0, 1, PPC_FLOAT),
10499 GEN_FLOAT_AB(div, 0x12, 0x000007C0, 1, PPC_FLOAT),
10500 GEN_FLOAT_AC(mul, 0x19, 0x0000F800, 1, PPC_FLOAT),
10501 GEN_FLOAT_BS(re, 0x3F, 0x18, 1, PPC_FLOAT_EXT),
10502 GEN_FLOAT_BS(res, 0x3B, 0x18, 1, PPC_FLOAT_FRES),
10503 GEN_FLOAT_BS(rsqrte, 0x3F, 0x1A, 1, PPC_FLOAT_FRSQRTE),
10504 _GEN_FLOAT_ACB(sel, sel, 0x3F, 0x17, 0, 0, PPC_FLOAT_FSEL),
10505 GEN_FLOAT_AB(sub, 0x14, 0x000007C0, 1, PPC_FLOAT),
10506 GEN_FLOAT_ACB(madd, 0x1D, 1, PPC_FLOAT),
10507 GEN_FLOAT_ACB(msub, 0x1C, 1, PPC_FLOAT),
10508 GEN_FLOAT_ACB(nmadd, 0x1F, 1, PPC_FLOAT),
10509 GEN_FLOAT_ACB(nmsub, 0x1E, 1, PPC_FLOAT),
10510 GEN_HANDLER_E(ftdiv, 0x3F, 0x00, 0x04, 1, PPC_NONE, PPC2_FP_TST_ISA206),
10511 GEN_HANDLER_E(ftsqrt, 0x3F, 0x00, 0x05, 1, PPC_NONE, PPC2_FP_TST_ISA206),
10512 GEN_FLOAT_B(ctiw, 0x0E, 0x00, 0, PPC_FLOAT),
10513 GEN_HANDLER_E(fctiwu, 0x3F, 0x0E, 0x04, 0, PPC_NONE, PPC2_FP_CVT_ISA206),
10514 GEN_FLOAT_B(ctiwz, 0x0F, 0x00, 0, PPC_FLOAT),
10515 GEN_HANDLER_E(fctiwuz, 0x3F, 0x0F, 0x04, 0, PPC_NONE, PPC2_FP_CVT_ISA206),
10516 GEN_FLOAT_B(rsp, 0x0C, 0x00, 1, PPC_FLOAT),
10517 #if defined(TARGET_PPC64)
10518 GEN_FLOAT_B(cfid, 0x0E, 0x1A, 1, PPC_64B),
10519 GEN_HANDLER_E(fcfids, 0x3B, 0x0E, 0x1A, 0, PPC_NONE, PPC2_FP_CVT_ISA206),
10520 GEN_HANDLER_E(fcfidu, 0x3F, 0x0E, 0x1E, 0, PPC_NONE, PPC2_FP_CVT_ISA206),
10521 GEN_HANDLER_E(fcfidus, 0x3B, 0x0E, 0x1E, 0, PPC_NONE, PPC2_FP_CVT_ISA206),
10522 GEN_FLOAT_B(ctid, 0x0E, 0x19, 0, PPC_64B),
10523 GEN_HANDLER_E(fctidu, 0x3F, 0x0E, 0x1D, 0, PPC_NONE, PPC2_FP_CVT_ISA206),
10524 GEN_FLOAT_B(ctidz, 0x0F, 0x19, 0, PPC_64B),
10525 GEN_HANDLER_E(fctiduz, 0x3F, 0x0F, 0x1D, 0, PPC_NONE, PPC2_FP_CVT_ISA206),
10526 #endif
10527 GEN_FLOAT_B(rin, 0x08, 0x0C, 1, PPC_FLOAT_EXT),
10528 GEN_FLOAT_B(riz, 0x08, 0x0D, 1, PPC_FLOAT_EXT),
10529 GEN_FLOAT_B(rip, 0x08, 0x0E, 1, PPC_FLOAT_EXT),
10530 GEN_FLOAT_B(rim, 0x08, 0x0F, 1, PPC_FLOAT_EXT),
10531
10532 #undef GEN_LD
10533 #undef GEN_LDU
10534 #undef GEN_LDUX
10535 #undef GEN_LDX_E
10536 #undef GEN_LDS
10537 #define GEN_LD(name, ldop, opc, type)                                         \
10538 GEN_HANDLER(name, opc, 0xFF, 0xFF, 0x00000000, type),
10539 #define GEN_LDU(name, ldop, opc, type)                                        \
10540 GEN_HANDLER(name##u, opc, 0xFF, 0xFF, 0x00000000, type),
10541 #define GEN_LDUX(name, ldop, opc2, opc3, type)                                \
10542 GEN_HANDLER(name##ux, 0x1F, opc2, opc3, 0x00000001, type),
10543 #define GEN_LDX_E(name, ldop, opc2, opc3, type, type2)                        \
10544 GEN_HANDLER_E(name##x, 0x1F, opc2, opc3, 0x00000001, type, type2),
10545 #define GEN_LDS(name, ldop, op, type)                                         \
10546 GEN_LD(name, ldop, op | 0x20, type)                                           \
10547 GEN_LDU(name, ldop, op | 0x21, type)                                          \
10548 GEN_LDUX(name, ldop, 0x17, op | 0x01, type)                                   \
10549 GEN_LDX(name, ldop, 0x17, op | 0x00, type)
10550
10551 GEN_LDS(lbz, ld8u, 0x02, PPC_INTEGER)
10552 GEN_LDS(lha, ld16s, 0x0A, PPC_INTEGER)
10553 GEN_LDS(lhz, ld16u, 0x08, PPC_INTEGER)
10554 GEN_LDS(lwz, ld32u, 0x00, PPC_INTEGER)
10555 #if defined(TARGET_PPC64)
10556 GEN_LDUX(lwa, ld32s, 0x15, 0x0B, PPC_64B)
10557 GEN_LDX(lwa, ld32s, 0x15, 0x0A, PPC_64B)
10558 GEN_LDUX(ld, ld64, 0x15, 0x01, PPC_64B)
10559 GEN_LDX(ld, ld64, 0x15, 0x00, PPC_64B)
10560 GEN_LDX_E(ldbr, ld64ur, 0x14, 0x10, PPC_NONE, PPC2_DBRX)
10561 #endif
10562 GEN_LDX(lhbr, ld16ur, 0x16, 0x18, PPC_INTEGER)
10563 GEN_LDX(lwbr, ld32ur, 0x16, 0x10, PPC_INTEGER)
10564
10565 #undef GEN_ST
10566 #undef GEN_STU
10567 #undef GEN_STUX
10568 #undef GEN_STX_E
10569 #undef GEN_STS
10570 #define GEN_ST(name, stop, opc, type)                                         \
10571 GEN_HANDLER(name, opc, 0xFF, 0xFF, 0x00000000, type),
10572 #define GEN_STU(name, stop, opc, type)                                        \
10573 GEN_HANDLER(stop##u, opc, 0xFF, 0xFF, 0x00000000, type),
10574 #define GEN_STUX(name, stop, opc2, opc3, type)                                \
10575 GEN_HANDLER(name##ux, 0x1F, opc2, opc3, 0x00000001, type),
10576 #define GEN_STX_E(name, stop, opc2, opc3, type, type2)                        \
10577 GEN_HANDLER_E(name##x, 0x1F, opc2, opc3, 0x00000001, type, type2),
10578 #define GEN_STS(name, stop, op, type)                                         \
10579 GEN_ST(name, stop, op | 0x20, type)                                           \
10580 GEN_STU(name, stop, op | 0x21, type)                                          \
10581 GEN_STUX(name, stop, 0x17, op | 0x01, type)                                   \
10582 GEN_STX(name, stop, 0x17, op | 0x00, type)
10583
10584 GEN_STS(stb, st8, 0x06, PPC_INTEGER)
10585 GEN_STS(sth, st16, 0x0C, PPC_INTEGER)
10586 GEN_STS(stw, st32, 0x04, PPC_INTEGER)
10587 #if defined(TARGET_PPC64)
10588 GEN_STUX(std, st64, 0x15, 0x05, PPC_64B)
10589 GEN_STX(std, st64, 0x15, 0x04, PPC_64B)
10590 GEN_STX_E(stdbr, st64r, 0x14, 0x14, PPC_NONE, PPC2_DBRX)
10591 #endif
10592 GEN_STX(sthbr, st16r, 0x16, 0x1C, PPC_INTEGER)
10593 GEN_STX(stwbr, st32r, 0x16, 0x14, PPC_INTEGER)
10594
10595 #undef GEN_LDF
10596 #undef GEN_LDUF
10597 #undef GEN_LDUXF
10598 #undef GEN_LDXF
10599 #undef GEN_LDFS
10600 #define GEN_LDF(name, ldop, opc, type)                                        \
10601 GEN_HANDLER(name, opc, 0xFF, 0xFF, 0x00000000, type),
10602 #define GEN_LDUF(name, ldop, opc, type)                                       \
10603 GEN_HANDLER(name##u, opc, 0xFF, 0xFF, 0x00000000, type),
10604 #define GEN_LDUXF(name, ldop, opc, type)                                      \
10605 GEN_HANDLER(name##ux, 0x1F, 0x17, opc, 0x00000001, type),
10606 #define GEN_LDXF(name, ldop, opc2, opc3, type)                                \
10607 GEN_HANDLER(name##x, 0x1F, opc2, opc3, 0x00000001, type),
10608 #define GEN_LDFS(name, ldop, op, type)                                        \
10609 GEN_LDF(name, ldop, op | 0x20, type)                                          \
10610 GEN_LDUF(name, ldop, op | 0x21, type)                                         \
10611 GEN_LDUXF(name, ldop, op | 0x01, type)                                        \
10612 GEN_LDXF(name, ldop, 0x17, op | 0x00, type)
10613
10614 GEN_LDFS(lfd, ld64, 0x12, PPC_FLOAT)
10615 GEN_LDFS(lfs, ld32fs, 0x10, PPC_FLOAT)
10616 GEN_HANDLER_E(lfiwax, 0x1f, 0x17, 0x1a, 0x00000001, PPC_NONE, PPC2_ISA205),
10617 GEN_HANDLER_E(lfiwzx, 0x1f, 0x17, 0x1b, 0x1, PPC_NONE, PPC2_FP_CVT_ISA206),
10618 GEN_HANDLER_E(lfdp, 0x39, 0xFF, 0xFF, 0x00200003, PPC_NONE, PPC2_ISA205),
10619 GEN_HANDLER_E(lfdpx, 0x1F, 0x17, 0x18, 0x00200001, PPC_NONE, PPC2_ISA205),
10620
10621 #undef GEN_STF
10622 #undef GEN_STUF
10623 #undef GEN_STUXF
10624 #undef GEN_STXF
10625 #undef GEN_STFS
10626 #define GEN_STF(name, stop, opc, type)                                        \
10627 GEN_HANDLER(name, opc, 0xFF, 0xFF, 0x00000000, type),
10628 #define GEN_STUF(name, stop, opc, type)                                       \
10629 GEN_HANDLER(name##u, opc, 0xFF, 0xFF, 0x00000000, type),
10630 #define GEN_STUXF(name, stop, opc, type)                                      \
10631 GEN_HANDLER(name##ux, 0x1F, 0x17, opc, 0x00000001, type),
10632 #define GEN_STXF(name, stop, opc2, opc3, type)                                \
10633 GEN_HANDLER(name##x, 0x1F, opc2, opc3, 0x00000001, type),
10634 #define GEN_STFS(name, stop, op, type)                                        \
10635 GEN_STF(name, stop, op | 0x20, type)                                          \
10636 GEN_STUF(name, stop, op | 0x21, type)                                         \
10637 GEN_STUXF(name, stop, op | 0x01, type)                                        \
10638 GEN_STXF(name, stop, 0x17, op | 0x00, type)
10639
10640 GEN_STFS(stfd, st64, 0x16, PPC_FLOAT)
10641 GEN_STFS(stfs, st32fs, 0x14, PPC_FLOAT)
10642 GEN_STXF(stfiw, st32fiw, 0x17, 0x1E, PPC_FLOAT_STFIWX)
10643 GEN_HANDLER_E(stfdp, 0x3D, 0xFF, 0xFF, 0x00200003, PPC_NONE, PPC2_ISA205),
10644 GEN_HANDLER_E(stfdpx, 0x1F, 0x17, 0x1C, 0x00200001, PPC_NONE, PPC2_ISA205),
10645
10646 #undef GEN_CRLOGIC
10647 #define GEN_CRLOGIC(name, tcg_op, opc)                                        \
10648 GEN_HANDLER(name, 0x13, 0x01, opc, 0x00000001, PPC_INTEGER)
10649 GEN_CRLOGIC(crand, tcg_gen_and_i32, 0x08),
10650 GEN_CRLOGIC(crandc, tcg_gen_andc_i32, 0x04),
10651 GEN_CRLOGIC(creqv, tcg_gen_eqv_i32, 0x09),
10652 GEN_CRLOGIC(crnand, tcg_gen_nand_i32, 0x07),
10653 GEN_CRLOGIC(crnor, tcg_gen_nor_i32, 0x01),
10654 GEN_CRLOGIC(cror, tcg_gen_or_i32, 0x0E),
10655 GEN_CRLOGIC(crorc, tcg_gen_orc_i32, 0x0D),
10656 GEN_CRLOGIC(crxor, tcg_gen_xor_i32, 0x06),
10657
10658 #undef GEN_MAC_HANDLER
10659 #define GEN_MAC_HANDLER(name, opc2, opc3)                                     \
10660 GEN_HANDLER(name, 0x04, opc2, opc3, 0x00000000, PPC_405_MAC)
10661 GEN_MAC_HANDLER(macchw, 0x0C, 0x05),
10662 GEN_MAC_HANDLER(macchwo, 0x0C, 0x15),
10663 GEN_MAC_HANDLER(macchws, 0x0C, 0x07),
10664 GEN_MAC_HANDLER(macchwso, 0x0C, 0x17),
10665 GEN_MAC_HANDLER(macchwsu, 0x0C, 0x06),
10666 GEN_MAC_HANDLER(macchwsuo, 0x0C, 0x16),
10667 GEN_MAC_HANDLER(macchwu, 0x0C, 0x04),
10668 GEN_MAC_HANDLER(macchwuo, 0x0C, 0x14),
10669 GEN_MAC_HANDLER(machhw, 0x0C, 0x01),
10670 GEN_MAC_HANDLER(machhwo, 0x0C, 0x11),
10671 GEN_MAC_HANDLER(machhws, 0x0C, 0x03),
10672 GEN_MAC_HANDLER(machhwso, 0x0C, 0x13),
10673 GEN_MAC_HANDLER(machhwsu, 0x0C, 0x02),
10674 GEN_MAC_HANDLER(machhwsuo, 0x0C, 0x12),
10675 GEN_MAC_HANDLER(machhwu, 0x0C, 0x00),
10676 GEN_MAC_HANDLER(machhwuo, 0x0C, 0x10),
10677 GEN_MAC_HANDLER(maclhw, 0x0C, 0x0D),
10678 GEN_MAC_HANDLER(maclhwo, 0x0C, 0x1D),
10679 GEN_MAC_HANDLER(maclhws, 0x0C, 0x0F),
10680 GEN_MAC_HANDLER(maclhwso, 0x0C, 0x1F),
10681 GEN_MAC_HANDLER(maclhwu, 0x0C, 0x0C),
10682 GEN_MAC_HANDLER(maclhwuo, 0x0C, 0x1C),
10683 GEN_MAC_HANDLER(maclhwsu, 0x0C, 0x0E),
10684 GEN_MAC_HANDLER(maclhwsuo, 0x0C, 0x1E),
10685 GEN_MAC_HANDLER(nmacchw, 0x0E, 0x05),
10686 GEN_MAC_HANDLER(nmacchwo, 0x0E, 0x15),
10687 GEN_MAC_HANDLER(nmacchws, 0x0E, 0x07),
10688 GEN_MAC_HANDLER(nmacchwso, 0x0E, 0x17),
10689 GEN_MAC_HANDLER(nmachhw, 0x0E, 0x01),
10690 GEN_MAC_HANDLER(nmachhwo, 0x0E, 0x11),
10691 GEN_MAC_HANDLER(nmachhws, 0x0E, 0x03),
10692 GEN_MAC_HANDLER(nmachhwso, 0x0E, 0x13),
10693 GEN_MAC_HANDLER(nmaclhw, 0x0E, 0x0D),
10694 GEN_MAC_HANDLER(nmaclhwo, 0x0E, 0x1D),
10695 GEN_MAC_HANDLER(nmaclhws, 0x0E, 0x0F),
10696 GEN_MAC_HANDLER(nmaclhwso, 0x0E, 0x1F),
10697 GEN_MAC_HANDLER(mulchw, 0x08, 0x05),
10698 GEN_MAC_HANDLER(mulchwu, 0x08, 0x04),
10699 GEN_MAC_HANDLER(mulhhw, 0x08, 0x01),
10700 GEN_MAC_HANDLER(mulhhwu, 0x08, 0x00),
10701 GEN_MAC_HANDLER(mullhw, 0x08, 0x0D),
10702 GEN_MAC_HANDLER(mullhwu, 0x08, 0x0C),
10703
10704 #undef GEN_VR_LDX
10705 #undef GEN_VR_STX
10706 #undef GEN_VR_LVE
10707 #undef GEN_VR_STVE
10708 #define GEN_VR_LDX(name, opc2, opc3)                                          \
10709 GEN_HANDLER(name, 0x1F, opc2, opc3, 0x00000001, PPC_ALTIVEC)
10710 #define GEN_VR_STX(name, opc2, opc3)                                          \
10711 GEN_HANDLER(st##name, 0x1F, opc2, opc3, 0x00000001, PPC_ALTIVEC)
10712 #define GEN_VR_LVE(name, opc2, opc3)                                    \
10713     GEN_HANDLER(lve##name, 0x1F, opc2, opc3, 0x00000001, PPC_ALTIVEC)
10714 #define GEN_VR_STVE(name, opc2, opc3)                                   \
10715     GEN_HANDLER(stve##name, 0x1F, opc2, opc3, 0x00000001, PPC_ALTIVEC)
10716 GEN_VR_LDX(lvx, 0x07, 0x03),
10717 GEN_VR_LDX(lvxl, 0x07, 0x0B),
10718 GEN_VR_LVE(bx, 0x07, 0x00),
10719 GEN_VR_LVE(hx, 0x07, 0x01),
10720 GEN_VR_LVE(wx, 0x07, 0x02),
10721 GEN_VR_STX(svx, 0x07, 0x07),
10722 GEN_VR_STX(svxl, 0x07, 0x0F),
10723 GEN_VR_STVE(bx, 0x07, 0x04),
10724 GEN_VR_STVE(hx, 0x07, 0x05),
10725 GEN_VR_STVE(wx, 0x07, 0x06),
10726
10727 #undef GEN_VX_LOGICAL
10728 #define GEN_VX_LOGICAL(name, tcg_op, opc2, opc3)                        \
10729 GEN_HANDLER(name, 0x04, opc2, opc3, 0x00000000, PPC_ALTIVEC)
10730
10731 #undef GEN_VX_LOGICAL_207
10732 #define GEN_VX_LOGICAL_207(name, tcg_op, opc2, opc3) \
10733 GEN_HANDLER_E(name, 0x04, opc2, opc3, 0x00000000, PPC_NONE, PPC2_ALTIVEC_207)
10734
10735 GEN_VX_LOGICAL(vand, tcg_gen_and_i64, 2, 16),
10736 GEN_VX_LOGICAL(vandc, tcg_gen_andc_i64, 2, 17),
10737 GEN_VX_LOGICAL(vor, tcg_gen_or_i64, 2, 18),
10738 GEN_VX_LOGICAL(vxor, tcg_gen_xor_i64, 2, 19),
10739 GEN_VX_LOGICAL(vnor, tcg_gen_nor_i64, 2, 20),
10740 GEN_VX_LOGICAL_207(veqv, tcg_gen_eqv_i64, 2, 26),
10741 GEN_VX_LOGICAL_207(vnand, tcg_gen_nand_i64, 2, 22),
10742 GEN_VX_LOGICAL_207(vorc, tcg_gen_orc_i64, 2, 21),
10743
10744 #undef GEN_VXFORM
10745 #define GEN_VXFORM(name, opc2, opc3)                                    \
10746 GEN_HANDLER(name, 0x04, opc2, opc3, 0x00000000, PPC_ALTIVEC)
10747
10748 #undef GEN_VXFORM_207
10749 #define GEN_VXFORM_207(name, opc2, opc3) \
10750 GEN_HANDLER_E(name, 0x04, opc2, opc3, 0x00000000, PPC_NONE, PPC2_ALTIVEC_207)
10751
10752 #undef GEN_VXFORM_DUAL
10753 #define GEN_VXFORM_DUAL(name0, name1, opc2, opc3, type0, type1) \
10754 GEN_HANDLER_E(name0##_##name1, 0x4, opc2, opc3, 0x00000000, type0, type1)
10755
10756 #undef GEN_VXRFORM_DUAL
10757 #define GEN_VXRFORM_DUAL(name0, name1, opc2, opc3, tp0, tp1) \
10758 GEN_HANDLER_E(name0##_##name1, 0x4, opc2, opc3, 0x00000000, tp0, tp1), \
10759 GEN_HANDLER_E(name0##_##name1, 0x4, opc2, (opc3 | 0x10), 0x00000000, tp0, tp1),
10760
10761 GEN_VXFORM(vaddubm, 0, 0),
10762 GEN_VXFORM(vadduhm, 0, 1),
10763 GEN_VXFORM(vadduwm, 0, 2),
10764 GEN_VXFORM_207(vaddudm, 0, 3),
10765 GEN_VXFORM_DUAL(vsububm, bcdadd, 0, 16, PPC_ALTIVEC, PPC_NONE),
10766 GEN_VXFORM_DUAL(vsubuhm, bcdsub, 0, 17, PPC_ALTIVEC, PPC_NONE),
10767 GEN_VXFORM(vsubuwm, 0, 18),
10768 GEN_VXFORM_207(vsubudm, 0, 19),
10769 GEN_VXFORM(vmaxub, 1, 0),
10770 GEN_VXFORM(vmaxuh, 1, 1),
10771 GEN_VXFORM(vmaxuw, 1, 2),
10772 GEN_VXFORM_207(vmaxud, 1, 3),
10773 GEN_VXFORM(vmaxsb, 1, 4),
10774 GEN_VXFORM(vmaxsh, 1, 5),
10775 GEN_VXFORM(vmaxsw, 1, 6),
10776 GEN_VXFORM_207(vmaxsd, 1, 7),
10777 GEN_VXFORM(vminub, 1, 8),
10778 GEN_VXFORM(vminuh, 1, 9),
10779 GEN_VXFORM(vminuw, 1, 10),
10780 GEN_VXFORM_207(vminud, 1, 11),
10781 GEN_VXFORM(vminsb, 1, 12),
10782 GEN_VXFORM(vminsh, 1, 13),
10783 GEN_VXFORM(vminsw, 1, 14),
10784 GEN_VXFORM_207(vminsd, 1, 15),
10785 GEN_VXFORM(vavgub, 1, 16),
10786 GEN_VXFORM(vavguh, 1, 17),
10787 GEN_VXFORM(vavguw, 1, 18),
10788 GEN_VXFORM(vavgsb, 1, 20),
10789 GEN_VXFORM(vavgsh, 1, 21),
10790 GEN_VXFORM(vavgsw, 1, 22),
10791 GEN_VXFORM(vmrghb, 6, 0),
10792 GEN_VXFORM(vmrghh, 6, 1),
10793 GEN_VXFORM(vmrghw, 6, 2),
10794 GEN_VXFORM(vmrglb, 6, 4),
10795 GEN_VXFORM(vmrglh, 6, 5),
10796 GEN_VXFORM(vmrglw, 6, 6),
10797 GEN_VXFORM_207(vmrgew, 6, 30),
10798 GEN_VXFORM_207(vmrgow, 6, 26),
10799 GEN_VXFORM(vmuloub, 4, 0),
10800 GEN_VXFORM(vmulouh, 4, 1),
10801 GEN_VXFORM_DUAL(vmulouw, vmuluwm, 4, 2, PPC_ALTIVEC, PPC_NONE),
10802 GEN_VXFORM(vmulosb, 4, 4),
10803 GEN_VXFORM(vmulosh, 4, 5),
10804 GEN_VXFORM_207(vmulosw, 4, 6),
10805 GEN_VXFORM(vmuleub, 4, 8),
10806 GEN_VXFORM(vmuleuh, 4, 9),
10807 GEN_VXFORM_207(vmuleuw, 4, 10),
10808 GEN_VXFORM(vmulesb, 4, 12),
10809 GEN_VXFORM(vmulesh, 4, 13),
10810 GEN_VXFORM_207(vmulesw, 4, 14),
10811 GEN_VXFORM(vslb, 2, 4),
10812 GEN_VXFORM(vslh, 2, 5),
10813 GEN_VXFORM(vslw, 2, 6),
10814 GEN_VXFORM_207(vsld, 2, 23),
10815 GEN_VXFORM(vsrb, 2, 8),
10816 GEN_VXFORM(vsrh, 2, 9),
10817 GEN_VXFORM(vsrw, 2, 10),
10818 GEN_VXFORM_207(vsrd, 2, 27),
10819 GEN_VXFORM(vsrab, 2, 12),
10820 GEN_VXFORM(vsrah, 2, 13),
10821 GEN_VXFORM(vsraw, 2, 14),
10822 GEN_VXFORM_207(vsrad, 2, 15),
10823 GEN_VXFORM(vslo, 6, 16),
10824 GEN_VXFORM(vsro, 6, 17),
10825 GEN_VXFORM(vaddcuw, 0, 6),
10826 GEN_VXFORM(vsubcuw, 0, 22),
10827 GEN_VXFORM(vaddubs, 0, 8),
10828 GEN_VXFORM(vadduhs, 0, 9),
10829 GEN_VXFORM(vadduws, 0, 10),
10830 GEN_VXFORM(vaddsbs, 0, 12),
10831 GEN_VXFORM(vaddshs, 0, 13),
10832 GEN_VXFORM(vaddsws, 0, 14),
10833 GEN_VXFORM_DUAL(vsububs, bcdadd, 0, 24, PPC_ALTIVEC, PPC_NONE),
10834 GEN_VXFORM_DUAL(vsubuhs, bcdsub, 0, 25, PPC_ALTIVEC, PPC_NONE),
10835 GEN_VXFORM(vsubuws, 0, 26),
10836 GEN_VXFORM(vsubsbs, 0, 28),
10837 GEN_VXFORM(vsubshs, 0, 29),
10838 GEN_VXFORM(vsubsws, 0, 30),
10839 GEN_VXFORM_207(vadduqm, 0, 4),
10840 GEN_VXFORM_207(vaddcuq, 0, 5),
10841 GEN_VXFORM_DUAL(vaddeuqm, vaddecuq, 30, 0xFF, PPC_NONE, PPC2_ALTIVEC_207),
10842 GEN_VXFORM_207(vsubuqm, 0, 20),
10843 GEN_VXFORM_207(vsubcuq, 0, 21),
10844 GEN_VXFORM_DUAL(vsubeuqm, vsubecuq, 31, 0xFF, PPC_NONE, PPC2_ALTIVEC_207),
10845 GEN_VXFORM(vrlb, 2, 0),
10846 GEN_VXFORM(vrlh, 2, 1),
10847 GEN_VXFORM(vrlw, 2, 2),
10848 GEN_VXFORM_207(vrld, 2, 3),
10849 GEN_VXFORM(vsl, 2, 7),
10850 GEN_VXFORM(vsr, 2, 11),
10851 GEN_VXFORM(vpkuhum, 7, 0),
10852 GEN_VXFORM(vpkuwum, 7, 1),
10853 GEN_VXFORM_207(vpkudum, 7, 17),
10854 GEN_VXFORM(vpkuhus, 7, 2),
10855 GEN_VXFORM(vpkuwus, 7, 3),
10856 GEN_VXFORM_207(vpkudus, 7, 19),
10857 GEN_VXFORM(vpkshus, 7, 4),
10858 GEN_VXFORM(vpkswus, 7, 5),
10859 GEN_VXFORM_207(vpksdus, 7, 21),
10860 GEN_VXFORM(vpkshss, 7, 6),
10861 GEN_VXFORM(vpkswss, 7, 7),
10862 GEN_VXFORM_207(vpksdss, 7, 23),
10863 GEN_VXFORM(vpkpx, 7, 12),
10864 GEN_VXFORM(vsum4ubs, 4, 24),
10865 GEN_VXFORM(vsum4sbs, 4, 28),
10866 GEN_VXFORM(vsum4shs, 4, 25),
10867 GEN_VXFORM(vsum2sws, 4, 26),
10868 GEN_VXFORM(vsumsws, 4, 30),
10869 GEN_VXFORM(vaddfp, 5, 0),
10870 GEN_VXFORM(vsubfp, 5, 1),
10871 GEN_VXFORM(vmaxfp, 5, 16),
10872 GEN_VXFORM(vminfp, 5, 17),
10873
10874 #undef GEN_VXRFORM1
10875 #undef GEN_VXRFORM
10876 #define GEN_VXRFORM1(opname, name, str, opc2, opc3)                     \
10877     GEN_HANDLER2(name, str, 0x4, opc2, opc3, 0x00000000, PPC_ALTIVEC),
10878 #define GEN_VXRFORM(name, opc2, opc3)                                \
10879     GEN_VXRFORM1(name, name, #name, opc2, opc3)                      \
10880     GEN_VXRFORM1(name##_dot, name##_, #name ".", opc2, (opc3 | (0x1 << 4)))
10881 GEN_VXRFORM(vcmpequb, 3, 0)
10882 GEN_VXRFORM(vcmpequh, 3, 1)
10883 GEN_VXRFORM(vcmpequw, 3, 2)
10884 GEN_VXRFORM(vcmpgtsb, 3, 12)
10885 GEN_VXRFORM(vcmpgtsh, 3, 13)
10886 GEN_VXRFORM(vcmpgtsw, 3, 14)
10887 GEN_VXRFORM(vcmpgtub, 3, 8)
10888 GEN_VXRFORM(vcmpgtuh, 3, 9)
10889 GEN_VXRFORM(vcmpgtuw, 3, 10)
10890 GEN_VXRFORM_DUAL(vcmpeqfp, vcmpequd, 3, 3, PPC_ALTIVEC, PPC_NONE)
10891 GEN_VXRFORM(vcmpgefp, 3, 7)
10892 GEN_VXRFORM_DUAL(vcmpgtfp, vcmpgtud, 3, 11, PPC_ALTIVEC, PPC_NONE)
10893 GEN_VXRFORM_DUAL(vcmpbfp, vcmpgtsd, 3, 15, PPC_ALTIVEC, PPC_NONE)
10894
10895 #undef GEN_VXFORM_SIMM
10896 #define GEN_VXFORM_SIMM(name, opc2, opc3)                               \
10897     GEN_HANDLER(name, 0x04, opc2, opc3, 0x00000000, PPC_ALTIVEC)
10898 GEN_VXFORM_SIMM(vspltisb, 6, 12),
10899 GEN_VXFORM_SIMM(vspltish, 6, 13),
10900 GEN_VXFORM_SIMM(vspltisw, 6, 14),
10901
10902 #undef GEN_VXFORM_NOA
10903 #define GEN_VXFORM_NOA(name, opc2, opc3)                                \
10904     GEN_HANDLER(name, 0x04, opc2, opc3, 0x001f0000, PPC_ALTIVEC)
10905 GEN_VXFORM_NOA(vupkhsb, 7, 8),
10906 GEN_VXFORM_NOA(vupkhsh, 7, 9),
10907 GEN_VXFORM_207(vupkhsw, 7, 25),
10908 GEN_VXFORM_NOA(vupklsb, 7, 10),
10909 GEN_VXFORM_NOA(vupklsh, 7, 11),
10910 GEN_VXFORM_207(vupklsw, 7, 27),
10911 GEN_VXFORM_NOA(vupkhpx, 7, 13),
10912 GEN_VXFORM_NOA(vupklpx, 7, 15),
10913 GEN_VXFORM_NOA(vrefp, 5, 4),
10914 GEN_VXFORM_NOA(vrsqrtefp, 5, 5),
10915 GEN_VXFORM_NOA(vexptefp, 5, 6),
10916 GEN_VXFORM_NOA(vlogefp, 5, 7),
10917 GEN_VXFORM_NOA(vrfim, 5, 8),
10918 GEN_VXFORM_NOA(vrfin, 5, 9),
10919 GEN_VXFORM_NOA(vrfip, 5, 10),
10920 GEN_VXFORM_NOA(vrfiz, 5, 11),
10921
10922 #undef GEN_VXFORM_UIMM
10923 #define GEN_VXFORM_UIMM(name, opc2, opc3)                               \
10924     GEN_HANDLER(name, 0x04, opc2, opc3, 0x00000000, PPC_ALTIVEC)
10925 GEN_VXFORM_UIMM(vspltb, 6, 8),
10926 GEN_VXFORM_UIMM(vsplth, 6, 9),
10927 GEN_VXFORM_UIMM(vspltw, 6, 10),
10928 GEN_VXFORM_UIMM(vcfux, 5, 12),
10929 GEN_VXFORM_UIMM(vcfsx, 5, 13),
10930 GEN_VXFORM_UIMM(vctuxs, 5, 14),
10931 GEN_VXFORM_UIMM(vctsxs, 5, 15),
10932
10933 #undef GEN_VAFORM_PAIRED
10934 #define GEN_VAFORM_PAIRED(name0, name1, opc2)                           \
10935     GEN_HANDLER(name0##_##name1, 0x04, opc2, 0xFF, 0x00000000, PPC_ALTIVEC)
10936 GEN_VAFORM_PAIRED(vmhaddshs, vmhraddshs, 16),
10937 GEN_VAFORM_PAIRED(vmsumubm, vmsummbm, 18),
10938 GEN_VAFORM_PAIRED(vmsumuhm, vmsumuhs, 19),
10939 GEN_VAFORM_PAIRED(vmsumshm, vmsumshs, 20),
10940 GEN_VAFORM_PAIRED(vsel, vperm, 21),
10941 GEN_VAFORM_PAIRED(vmaddfp, vnmsubfp, 23),
10942
10943 GEN_VXFORM_DUAL(vclzb, vpopcntb, 1, 28, PPC_NONE, PPC2_ALTIVEC_207),
10944 GEN_VXFORM_DUAL(vclzh, vpopcnth, 1, 29, PPC_NONE, PPC2_ALTIVEC_207),
10945 GEN_VXFORM_DUAL(vclzw, vpopcntw, 1, 30, PPC_NONE, PPC2_ALTIVEC_207),
10946 GEN_VXFORM_DUAL(vclzd, vpopcntd, 1, 31, PPC_NONE, PPC2_ALTIVEC_207),
10947
10948 GEN_VXFORM_207(vbpermq, 6, 21),
10949 GEN_VXFORM_207(vgbbd, 6, 20),
10950 GEN_VXFORM_207(vpmsumb, 4, 16),
10951 GEN_VXFORM_207(vpmsumh, 4, 17),
10952 GEN_VXFORM_207(vpmsumw, 4, 18),
10953 GEN_VXFORM_207(vpmsumd, 4, 19),
10954
10955 GEN_VXFORM_207(vsbox, 4, 23),
10956
10957 GEN_VXFORM_DUAL(vcipher, vcipherlast, 4, 20, PPC_NONE, PPC2_ALTIVEC_207),
10958 GEN_VXFORM_DUAL(vncipher, vncipherlast, 4, 21, PPC_NONE, PPC2_ALTIVEC_207),
10959
10960 GEN_VXFORM_207(vshasigmaw, 1, 26),
10961 GEN_VXFORM_207(vshasigmad, 1, 27),
10962
10963 GEN_VXFORM_DUAL(vsldoi, vpermxor, 22, 0xFF, PPC_ALTIVEC, PPC_NONE),
10964
10965 GEN_HANDLER_E(lxsdx, 0x1F, 0x0C, 0x12, 0, PPC_NONE, PPC2_VSX),
10966 GEN_HANDLER_E(lxsiwax, 0x1F, 0x0C, 0x02, 0, PPC_NONE, PPC2_VSX207),
10967 GEN_HANDLER_E(lxsiwzx, 0x1F, 0x0C, 0x00, 0, PPC_NONE, PPC2_VSX207),
10968 GEN_HANDLER_E(lxsspx, 0x1F, 0x0C, 0x10, 0, PPC_NONE, PPC2_VSX207),
10969 GEN_HANDLER_E(lxvd2x, 0x1F, 0x0C, 0x1A, 0, PPC_NONE, PPC2_VSX),
10970 GEN_HANDLER_E(lxvdsx, 0x1F, 0x0C, 0x0A, 0, PPC_NONE, PPC2_VSX),
10971 GEN_HANDLER_E(lxvw4x, 0x1F, 0x0C, 0x18, 0, PPC_NONE, PPC2_VSX),
10972
10973 GEN_HANDLER_E(stxsdx, 0x1F, 0xC, 0x16, 0, PPC_NONE, PPC2_VSX),
10974 GEN_HANDLER_E(stxsiwx, 0x1F, 0xC, 0x04, 0, PPC_NONE, PPC2_VSX207),
10975 GEN_HANDLER_E(stxsspx, 0x1F, 0xC, 0x14, 0, PPC_NONE, PPC2_VSX207),
10976 GEN_HANDLER_E(stxvd2x, 0x1F, 0xC, 0x1E, 0, PPC_NONE, PPC2_VSX),
10977 GEN_HANDLER_E(stxvw4x, 0x1F, 0xC, 0x1C, 0, PPC_NONE, PPC2_VSX),
10978
10979 GEN_HANDLER_E(mfvsrwz, 0x1F, 0x13, 0x03, 0x0000F800, PPC_NONE, PPC2_VSX207),
10980 GEN_HANDLER_E(mtvsrwa, 0x1F, 0x13, 0x06, 0x0000F800, PPC_NONE, PPC2_VSX207),
10981 GEN_HANDLER_E(mtvsrwz, 0x1F, 0x13, 0x07, 0x0000F800, PPC_NONE, PPC2_VSX207),
10982 #if defined(TARGET_PPC64)
10983 GEN_HANDLER_E(mfvsrd, 0x1F, 0x13, 0x01, 0x0000F800, PPC_NONE, PPC2_VSX207),
10984 GEN_HANDLER_E(mtvsrd, 0x1F, 0x13, 0x05, 0x0000F800, PPC_NONE, PPC2_VSX207),
10985 #endif
10986
10987 #undef GEN_XX2FORM
10988 #define GEN_XX2FORM(name, opc2, opc3, fl2)                           \
10989 GEN_HANDLER2_E(name, #name, 0x3C, opc2 | 0, opc3, 0, PPC_NONE, fl2), \
10990 GEN_HANDLER2_E(name, #name, 0x3C, opc2 | 1, opc3, 0, PPC_NONE, fl2)
10991
10992 #undef GEN_XX3FORM
10993 #define GEN_XX3FORM(name, opc2, opc3, fl2)                           \
10994 GEN_HANDLER2_E(name, #name, 0x3C, opc2 | 0, opc3, 0, PPC_NONE, fl2), \
10995 GEN_HANDLER2_E(name, #name, 0x3C, opc2 | 1, opc3, 0, PPC_NONE, fl2), \
10996 GEN_HANDLER2_E(name, #name, 0x3C, opc2 | 2, opc3, 0, PPC_NONE, fl2), \
10997 GEN_HANDLER2_E(name, #name, 0x3C, opc2 | 3, opc3, 0, PPC_NONE, fl2)
10998
10999 #undef GEN_XX3_RC_FORM
11000 #define GEN_XX3_RC_FORM(name, opc2, opc3, fl2)                          \
11001 GEN_HANDLER2_E(name, #name, 0x3C, opc2 | 0x00, opc3 | 0x00, 0, PPC_NONE, fl2), \
11002 GEN_HANDLER2_E(name, #name, 0x3C, opc2 | 0x01, opc3 | 0x00, 0, PPC_NONE, fl2), \
11003 GEN_HANDLER2_E(name, #name, 0x3C, opc2 | 0x02, opc3 | 0x00, 0, PPC_NONE, fl2), \
11004 GEN_HANDLER2_E(name, #name, 0x3C, opc2 | 0x03, opc3 | 0x00, 0, PPC_NONE, fl2), \
11005 GEN_HANDLER2_E(name, #name, 0x3C, opc2 | 0x00, opc3 | 0x10, 0, PPC_NONE, fl2), \
11006 GEN_HANDLER2_E(name, #name, 0x3C, opc2 | 0x01, opc3 | 0x10, 0, PPC_NONE, fl2), \
11007 GEN_HANDLER2_E(name, #name, 0x3C, opc2 | 0x02, opc3 | 0x10, 0, PPC_NONE, fl2), \
11008 GEN_HANDLER2_E(name, #name, 0x3C, opc2 | 0x03, opc3 | 0x10, 0, PPC_NONE, fl2)
11009
11010 #undef GEN_XX3FORM_DM
11011 #define GEN_XX3FORM_DM(name, opc2, opc3) \
11012 GEN_HANDLER2_E(name, #name, 0x3C, opc2|0x00, opc3|0x00, 0, PPC_NONE, PPC2_VSX),\
11013 GEN_HANDLER2_E(name, #name, 0x3C, opc2|0x01, opc3|0x00, 0, PPC_NONE, PPC2_VSX),\
11014 GEN_HANDLER2_E(name, #name, 0x3C, opc2|0x02, opc3|0x00, 0, PPC_NONE, PPC2_VSX),\
11015 GEN_HANDLER2_E(name, #name, 0x3C, opc2|0x03, opc3|0x00, 0, PPC_NONE, PPC2_VSX),\
11016 GEN_HANDLER2_E(name, #name, 0x3C, opc2|0x00, opc3|0x04, 0, PPC_NONE, PPC2_VSX),\
11017 GEN_HANDLER2_E(name, #name, 0x3C, opc2|0x01, opc3|0x04, 0, PPC_NONE, PPC2_VSX),\
11018 GEN_HANDLER2_E(name, #name, 0x3C, opc2|0x02, opc3|0x04, 0, PPC_NONE, PPC2_VSX),\
11019 GEN_HANDLER2_E(name, #name, 0x3C, opc2|0x03, opc3|0x04, 0, PPC_NONE, PPC2_VSX),\
11020 GEN_HANDLER2_E(name, #name, 0x3C, opc2|0x00, opc3|0x08, 0, PPC_NONE, PPC2_VSX),\
11021 GEN_HANDLER2_E(name, #name, 0x3C, opc2|0x01, opc3|0x08, 0, PPC_NONE, PPC2_VSX),\
11022 GEN_HANDLER2_E(name, #name, 0x3C, opc2|0x02, opc3|0x08, 0, PPC_NONE, PPC2_VSX),\
11023 GEN_HANDLER2_E(name, #name, 0x3C, opc2|0x03, opc3|0x08, 0, PPC_NONE, PPC2_VSX),\
11024 GEN_HANDLER2_E(name, #name, 0x3C, opc2|0x00, opc3|0x0C, 0, PPC_NONE, PPC2_VSX),\
11025 GEN_HANDLER2_E(name, #name, 0x3C, opc2|0x01, opc3|0x0C, 0, PPC_NONE, PPC2_VSX),\
11026 GEN_HANDLER2_E(name, #name, 0x3C, opc2|0x02, opc3|0x0C, 0, PPC_NONE, PPC2_VSX),\
11027 GEN_HANDLER2_E(name, #name, 0x3C, opc2|0x03, opc3|0x0C, 0, PPC_NONE, PPC2_VSX)
11028
11029 GEN_XX2FORM(xsabsdp, 0x12, 0x15, PPC2_VSX),
11030 GEN_XX2FORM(xsnabsdp, 0x12, 0x16, PPC2_VSX),
11031 GEN_XX2FORM(xsnegdp, 0x12, 0x17, PPC2_VSX),
11032 GEN_XX3FORM(xscpsgndp, 0x00, 0x16, PPC2_VSX),
11033
11034 GEN_XX2FORM(xvabsdp, 0x12, 0x1D, PPC2_VSX),
11035 GEN_XX2FORM(xvnabsdp, 0x12, 0x1E, PPC2_VSX),
11036 GEN_XX2FORM(xvnegdp, 0x12, 0x1F, PPC2_VSX),
11037 GEN_XX3FORM(xvcpsgndp, 0x00, 0x1E, PPC2_VSX),
11038 GEN_XX2FORM(xvabssp, 0x12, 0x19, PPC2_VSX),
11039 GEN_XX2FORM(xvnabssp, 0x12, 0x1A, PPC2_VSX),
11040 GEN_XX2FORM(xvnegsp, 0x12, 0x1B, PPC2_VSX),
11041 GEN_XX3FORM(xvcpsgnsp, 0x00, 0x1A, PPC2_VSX),
11042
11043 GEN_XX3FORM(xsadddp, 0x00, 0x04, PPC2_VSX),
11044 GEN_XX3FORM(xssubdp, 0x00, 0x05, PPC2_VSX),
11045 GEN_XX3FORM(xsmuldp, 0x00, 0x06, PPC2_VSX),
11046 GEN_XX3FORM(xsdivdp, 0x00, 0x07, PPC2_VSX),
11047 GEN_XX2FORM(xsredp,  0x14, 0x05, PPC2_VSX),
11048 GEN_XX2FORM(xssqrtdp,  0x16, 0x04, PPC2_VSX),
11049 GEN_XX2FORM(xsrsqrtedp,  0x14, 0x04, PPC2_VSX),
11050 GEN_XX3FORM(xstdivdp,  0x14, 0x07, PPC2_VSX),
11051 GEN_XX2FORM(xstsqrtdp,  0x14, 0x06, PPC2_VSX),
11052 GEN_XX3FORM(xsmaddadp, 0x04, 0x04, PPC2_VSX),
11053 GEN_XX3FORM(xsmaddmdp, 0x04, 0x05, PPC2_VSX),
11054 GEN_XX3FORM(xsmsubadp, 0x04, 0x06, PPC2_VSX),
11055 GEN_XX3FORM(xsmsubmdp, 0x04, 0x07, PPC2_VSX),
11056 GEN_XX3FORM(xsnmaddadp, 0x04, 0x14, PPC2_VSX),
11057 GEN_XX3FORM(xsnmaddmdp, 0x04, 0x15, PPC2_VSX),
11058 GEN_XX3FORM(xsnmsubadp, 0x04, 0x16, PPC2_VSX),
11059 GEN_XX3FORM(xsnmsubmdp, 0x04, 0x17, PPC2_VSX),
11060 GEN_XX2FORM(xscmpodp,  0x0C, 0x05, PPC2_VSX),
11061 GEN_XX2FORM(xscmpudp,  0x0C, 0x04, PPC2_VSX),
11062 GEN_XX3FORM(xsmaxdp, 0x00, 0x14, PPC2_VSX),
11063 GEN_XX3FORM(xsmindp, 0x00, 0x15, PPC2_VSX),
11064 GEN_XX2FORM(xscvdpsp, 0x12, 0x10, PPC2_VSX),
11065 GEN_XX2FORM(xscvdpspn, 0x16, 0x10, PPC2_VSX207),
11066 GEN_XX2FORM(xscvspdp, 0x12, 0x14, PPC2_VSX),
11067 GEN_XX2FORM(xscvspdpn, 0x16, 0x14, PPC2_VSX207),
11068 GEN_XX2FORM(xscvdpsxds, 0x10, 0x15, PPC2_VSX),
11069 GEN_XX2FORM(xscvdpsxws, 0x10, 0x05, PPC2_VSX),
11070 GEN_XX2FORM(xscvdpuxds, 0x10, 0x14, PPC2_VSX),
11071 GEN_XX2FORM(xscvdpuxws, 0x10, 0x04, PPC2_VSX),
11072 GEN_XX2FORM(xscvsxddp, 0x10, 0x17, PPC2_VSX),
11073 GEN_XX2FORM(xscvuxddp, 0x10, 0x16, PPC2_VSX),
11074 GEN_XX2FORM(xsrdpi, 0x12, 0x04, PPC2_VSX),
11075 GEN_XX2FORM(xsrdpic, 0x16, 0x06, PPC2_VSX),
11076 GEN_XX2FORM(xsrdpim, 0x12, 0x07, PPC2_VSX),
11077 GEN_XX2FORM(xsrdpip, 0x12, 0x06, PPC2_VSX),
11078 GEN_XX2FORM(xsrdpiz, 0x12, 0x05, PPC2_VSX),
11079
11080 GEN_XX3FORM(xsaddsp, 0x00, 0x00, PPC2_VSX207),
11081 GEN_XX3FORM(xssubsp, 0x00, 0x01, PPC2_VSX207),
11082 GEN_XX3FORM(xsmulsp, 0x00, 0x02, PPC2_VSX207),
11083 GEN_XX3FORM(xsdivsp, 0x00, 0x03, PPC2_VSX207),
11084 GEN_XX2FORM(xsresp,  0x14, 0x01, PPC2_VSX207),
11085 GEN_XX2FORM(xsrsp, 0x12, 0x11, PPC2_VSX207),
11086 GEN_XX2FORM(xssqrtsp,  0x16, 0x00, PPC2_VSX207),
11087 GEN_XX2FORM(xsrsqrtesp,  0x14, 0x00, PPC2_VSX207),
11088 GEN_XX3FORM(xsmaddasp, 0x04, 0x00, PPC2_VSX207),
11089 GEN_XX3FORM(xsmaddmsp, 0x04, 0x01, PPC2_VSX207),
11090 GEN_XX3FORM(xsmsubasp, 0x04, 0x02, PPC2_VSX207),
11091 GEN_XX3FORM(xsmsubmsp, 0x04, 0x03, PPC2_VSX207),
11092 GEN_XX3FORM(xsnmaddasp, 0x04, 0x10, PPC2_VSX207),
11093 GEN_XX3FORM(xsnmaddmsp, 0x04, 0x11, PPC2_VSX207),
11094 GEN_XX3FORM(xsnmsubasp, 0x04, 0x12, PPC2_VSX207),
11095 GEN_XX3FORM(xsnmsubmsp, 0x04, 0x13, PPC2_VSX207),
11096 GEN_XX2FORM(xscvsxdsp, 0x10, 0x13, PPC2_VSX207),
11097 GEN_XX2FORM(xscvuxdsp, 0x10, 0x12, PPC2_VSX207),
11098
11099 GEN_XX3FORM(xvadddp, 0x00, 0x0C, PPC2_VSX),
11100 GEN_XX3FORM(xvsubdp, 0x00, 0x0D, PPC2_VSX),
11101 GEN_XX3FORM(xvmuldp, 0x00, 0x0E, PPC2_VSX),
11102 GEN_XX3FORM(xvdivdp, 0x00, 0x0F, PPC2_VSX),
11103 GEN_XX2FORM(xvredp,  0x14, 0x0D, PPC2_VSX),
11104 GEN_XX2FORM(xvsqrtdp,  0x16, 0x0C, PPC2_VSX),
11105 GEN_XX2FORM(xvrsqrtedp,  0x14, 0x0C, PPC2_VSX),
11106 GEN_XX3FORM(xvtdivdp, 0x14, 0x0F, PPC2_VSX),
11107 GEN_XX2FORM(xvtsqrtdp, 0x14, 0x0E, PPC2_VSX),
11108 GEN_XX3FORM(xvmaddadp, 0x04, 0x0C, PPC2_VSX),
11109 GEN_XX3FORM(xvmaddmdp, 0x04, 0x0D, PPC2_VSX),
11110 GEN_XX3FORM(xvmsubadp, 0x04, 0x0E, PPC2_VSX),
11111 GEN_XX3FORM(xvmsubmdp, 0x04, 0x0F, PPC2_VSX),
11112 GEN_XX3FORM(xvnmaddadp, 0x04, 0x1C, PPC2_VSX),
11113 GEN_XX3FORM(xvnmaddmdp, 0x04, 0x1D, PPC2_VSX),
11114 GEN_XX3FORM(xvnmsubadp, 0x04, 0x1E, PPC2_VSX),
11115 GEN_XX3FORM(xvnmsubmdp, 0x04, 0x1F, PPC2_VSX),
11116 GEN_XX3FORM(xvmaxdp, 0x00, 0x1C, PPC2_VSX),
11117 GEN_XX3FORM(xvmindp, 0x00, 0x1D, PPC2_VSX),
11118 GEN_XX3_RC_FORM(xvcmpeqdp, 0x0C, 0x0C, PPC2_VSX),
11119 GEN_XX3_RC_FORM(xvcmpgtdp, 0x0C, 0x0D, PPC2_VSX),
11120 GEN_XX3_RC_FORM(xvcmpgedp, 0x0C, 0x0E, PPC2_VSX),
11121 GEN_XX2FORM(xvcvdpsp, 0x12, 0x18, PPC2_VSX),
11122 GEN_XX2FORM(xvcvdpsxds, 0x10, 0x1D, PPC2_VSX),
11123 GEN_XX2FORM(xvcvdpsxws, 0x10, 0x0D, PPC2_VSX),
11124 GEN_XX2FORM(xvcvdpuxds, 0x10, 0x1C, PPC2_VSX),
11125 GEN_XX2FORM(xvcvdpuxws, 0x10, 0x0C, PPC2_VSX),
11126 GEN_XX2FORM(xvcvsxddp, 0x10, 0x1F, PPC2_VSX),
11127 GEN_XX2FORM(xvcvuxddp, 0x10, 0x1E, PPC2_VSX),
11128 GEN_XX2FORM(xvcvsxwdp, 0x10, 0x0F, PPC2_VSX),
11129 GEN_XX2FORM(xvcvuxwdp, 0x10, 0x0E, PPC2_VSX),
11130 GEN_XX2FORM(xvrdpi, 0x12, 0x0C, PPC2_VSX),
11131 GEN_XX2FORM(xvrdpic, 0x16, 0x0E, PPC2_VSX),
11132 GEN_XX2FORM(xvrdpim, 0x12, 0x0F, PPC2_VSX),
11133 GEN_XX2FORM(xvrdpip, 0x12, 0x0E, PPC2_VSX),
11134 GEN_XX2FORM(xvrdpiz, 0x12, 0x0D, PPC2_VSX),
11135
11136 GEN_XX3FORM(xvaddsp, 0x00, 0x08, PPC2_VSX),
11137 GEN_XX3FORM(xvsubsp, 0x00, 0x09, PPC2_VSX),
11138 GEN_XX3FORM(xvmulsp, 0x00, 0x0A, PPC2_VSX),
11139 GEN_XX3FORM(xvdivsp, 0x00, 0x0B, PPC2_VSX),
11140 GEN_XX2FORM(xvresp, 0x14, 0x09, PPC2_VSX),
11141 GEN_XX2FORM(xvsqrtsp, 0x16, 0x08, PPC2_VSX),
11142 GEN_XX2FORM(xvrsqrtesp, 0x14, 0x08, PPC2_VSX),
11143 GEN_XX3FORM(xvtdivsp, 0x14, 0x0B, PPC2_VSX),
11144 GEN_XX2FORM(xvtsqrtsp, 0x14, 0x0A, PPC2_VSX),
11145 GEN_XX3FORM(xvmaddasp, 0x04, 0x08, PPC2_VSX),
11146 GEN_XX3FORM(xvmaddmsp, 0x04, 0x09, PPC2_VSX),
11147 GEN_XX3FORM(xvmsubasp, 0x04, 0x0A, PPC2_VSX),
11148 GEN_XX3FORM(xvmsubmsp, 0x04, 0x0B, PPC2_VSX),
11149 GEN_XX3FORM(xvnmaddasp, 0x04, 0x18, PPC2_VSX),
11150 GEN_XX3FORM(xvnmaddmsp, 0x04, 0x19, PPC2_VSX),
11151 GEN_XX3FORM(xvnmsubasp, 0x04, 0x1A, PPC2_VSX),
11152 GEN_XX3FORM(xvnmsubmsp, 0x04, 0x1B, PPC2_VSX),
11153 GEN_XX3FORM(xvmaxsp, 0x00, 0x18, PPC2_VSX),
11154 GEN_XX3FORM(xvminsp, 0x00, 0x19, PPC2_VSX),
11155 GEN_XX3_RC_FORM(xvcmpeqsp, 0x0C, 0x08, PPC2_VSX),
11156 GEN_XX3_RC_FORM(xvcmpgtsp, 0x0C, 0x09, PPC2_VSX),
11157 GEN_XX3_RC_FORM(xvcmpgesp, 0x0C, 0x0A, PPC2_VSX),
11158 GEN_XX2FORM(xvcvspdp, 0x12, 0x1C, PPC2_VSX),
11159 GEN_XX2FORM(xvcvspsxds, 0x10, 0x19, PPC2_VSX),
11160 GEN_XX2FORM(xvcvspsxws, 0x10, 0x09, PPC2_VSX),
11161 GEN_XX2FORM(xvcvspuxds, 0x10, 0x18, PPC2_VSX),
11162 GEN_XX2FORM(xvcvspuxws, 0x10, 0x08, PPC2_VSX),
11163 GEN_XX2FORM(xvcvsxdsp, 0x10, 0x1B, PPC2_VSX),
11164 GEN_XX2FORM(xvcvuxdsp, 0x10, 0x1A, PPC2_VSX),
11165 GEN_XX2FORM(xvcvsxwsp, 0x10, 0x0B, PPC2_VSX),
11166 GEN_XX2FORM(xvcvuxwsp, 0x10, 0x0A, PPC2_VSX),
11167 GEN_XX2FORM(xvrspi, 0x12, 0x08, PPC2_VSX),
11168 GEN_XX2FORM(xvrspic, 0x16, 0x0A, PPC2_VSX),
11169 GEN_XX2FORM(xvrspim, 0x12, 0x0B, PPC2_VSX),
11170 GEN_XX2FORM(xvrspip, 0x12, 0x0A, PPC2_VSX),
11171 GEN_XX2FORM(xvrspiz, 0x12, 0x09, PPC2_VSX),
11172
11173 #undef VSX_LOGICAL
11174 #define VSX_LOGICAL(name, opc2, opc3, fl2) \
11175 GEN_XX3FORM(name, opc2, opc3, fl2)
11176
11177 VSX_LOGICAL(xxland, 0x8, 0x10, PPC2_VSX),
11178 VSX_LOGICAL(xxlandc, 0x8, 0x11, PPC2_VSX),
11179 VSX_LOGICAL(xxlor, 0x8, 0x12, PPC2_VSX),
11180 VSX_LOGICAL(xxlxor, 0x8, 0x13, PPC2_VSX),
11181 VSX_LOGICAL(xxlnor, 0x8, 0x14, PPC2_VSX),
11182 VSX_LOGICAL(xxleqv, 0x8, 0x17, PPC2_VSX207),
11183 VSX_LOGICAL(xxlnand, 0x8, 0x16, PPC2_VSX207),
11184 VSX_LOGICAL(xxlorc, 0x8, 0x15, PPC2_VSX207),
11185 GEN_XX3FORM(xxmrghw, 0x08, 0x02, PPC2_VSX),
11186 GEN_XX3FORM(xxmrglw, 0x08, 0x06, PPC2_VSX),
11187 GEN_XX2FORM(xxspltw, 0x08, 0x0A, PPC2_VSX),
11188 GEN_XX3FORM_DM(xxsldwi, 0x08, 0x00),
11189
11190 #define GEN_XXSEL_ROW(opc3) \
11191 GEN_HANDLER2_E(xxsel, "xxsel", 0x3C, 0x18, opc3, 0, PPC_NONE, PPC2_VSX), \
11192 GEN_HANDLER2_E(xxsel, "xxsel", 0x3C, 0x19, opc3, 0, PPC_NONE, PPC2_VSX), \
11193 GEN_HANDLER2_E(xxsel, "xxsel", 0x3C, 0x1A, opc3, 0, PPC_NONE, PPC2_VSX), \
11194 GEN_HANDLER2_E(xxsel, "xxsel", 0x3C, 0x1B, opc3, 0, PPC_NONE, PPC2_VSX), \
11195 GEN_HANDLER2_E(xxsel, "xxsel", 0x3C, 0x1C, opc3, 0, PPC_NONE, PPC2_VSX), \
11196 GEN_HANDLER2_E(xxsel, "xxsel", 0x3C, 0x1D, opc3, 0, PPC_NONE, PPC2_VSX), \
11197 GEN_HANDLER2_E(xxsel, "xxsel", 0x3C, 0x1E, opc3, 0, PPC_NONE, PPC2_VSX), \
11198 GEN_HANDLER2_E(xxsel, "xxsel", 0x3C, 0x1F, opc3, 0, PPC_NONE, PPC2_VSX), \
11199
11200 GEN_XXSEL_ROW(0x00)
11201 GEN_XXSEL_ROW(0x01)
11202 GEN_XXSEL_ROW(0x02)
11203 GEN_XXSEL_ROW(0x03)
11204 GEN_XXSEL_ROW(0x04)
11205 GEN_XXSEL_ROW(0x05)
11206 GEN_XXSEL_ROW(0x06)
11207 GEN_XXSEL_ROW(0x07)
11208 GEN_XXSEL_ROW(0x08)
11209 GEN_XXSEL_ROW(0x09)
11210 GEN_XXSEL_ROW(0x0A)
11211 GEN_XXSEL_ROW(0x0B)
11212 GEN_XXSEL_ROW(0x0C)
11213 GEN_XXSEL_ROW(0x0D)
11214 GEN_XXSEL_ROW(0x0E)
11215 GEN_XXSEL_ROW(0x0F)
11216 GEN_XXSEL_ROW(0x10)
11217 GEN_XXSEL_ROW(0x11)
11218 GEN_XXSEL_ROW(0x12)
11219 GEN_XXSEL_ROW(0x13)
11220 GEN_XXSEL_ROW(0x14)
11221 GEN_XXSEL_ROW(0x15)
11222 GEN_XXSEL_ROW(0x16)
11223 GEN_XXSEL_ROW(0x17)
11224 GEN_XXSEL_ROW(0x18)
11225 GEN_XXSEL_ROW(0x19)
11226 GEN_XXSEL_ROW(0x1A)
11227 GEN_XXSEL_ROW(0x1B)
11228 GEN_XXSEL_ROW(0x1C)
11229 GEN_XXSEL_ROW(0x1D)
11230 GEN_XXSEL_ROW(0x1E)
11231 GEN_XXSEL_ROW(0x1F)
11232
11233 GEN_XX3FORM_DM(xxpermdi, 0x08, 0x01),
11234
11235 #undef GEN_DFP_T_A_B_Rc
11236 #undef GEN_DFP_BF_A_B
11237 #undef GEN_DFP_BF_A_DCM
11238 #undef GEN_DFP_T_B_U32_U32_Rc
11239 #undef GEN_DFP_T_A_B_I32_Rc
11240 #undef GEN_DFP_T_B_Rc
11241 #undef GEN_DFP_T_FPR_I32_Rc
11242
11243 #define _GEN_DFP_LONG(name, op1, op2, mask) \
11244 GEN_HANDLER_E(name, 0x3B, op1, op2, mask, PPC_NONE, PPC2_DFP)
11245
11246 #define _GEN_DFP_LONGx2(name, op1, op2, mask) \
11247 GEN_HANDLER_E(name, 0x3B, op1, 0x00 | op2, mask, PPC_NONE, PPC2_DFP), \
11248 GEN_HANDLER_E(name, 0x3B, op1, 0x10 | op2, mask, PPC_NONE, PPC2_DFP)
11249
11250 #define _GEN_DFP_LONGx4(name, op1, op2, mask) \
11251 GEN_HANDLER_E(name, 0x3B, op1, 0x00 | op2, mask, PPC_NONE, PPC2_DFP), \
11252 GEN_HANDLER_E(name, 0x3B, op1, 0x08 | op2, mask, PPC_NONE, PPC2_DFP), \
11253 GEN_HANDLER_E(name, 0x3B, op1, 0x10 | op2, mask, PPC_NONE, PPC2_DFP), \
11254 GEN_HANDLER_E(name, 0x3B, op1, 0x18 | op2, mask, PPC_NONE, PPC2_DFP)
11255
11256 #define _GEN_DFP_QUAD(name, op1, op2, mask) \
11257 GEN_HANDLER_E(name, 0x3F, op1, op2, mask, PPC_NONE, PPC2_DFP)
11258
11259 #define _GEN_DFP_QUADx2(name, op1, op2, mask) \
11260 GEN_HANDLER_E(name, 0x3F, op1, 0x00 | op2, mask, PPC_NONE, PPC2_DFP), \
11261 GEN_HANDLER_E(name, 0x3F, op1, 0x10 | op2, mask, PPC_NONE, PPC2_DFP)
11262
11263 #define _GEN_DFP_QUADx4(name, op1, op2, mask)                         \
11264 GEN_HANDLER_E(name, 0x3F, op1, 0x00 | op2, mask, PPC_NONE, PPC2_DFP), \
11265 GEN_HANDLER_E(name, 0x3F, op1, 0x08 | op2, mask, PPC_NONE, PPC2_DFP), \
11266 GEN_HANDLER_E(name, 0x3F, op1, 0x10 | op2, mask, PPC_NONE, PPC2_DFP), \
11267 GEN_HANDLER_E(name, 0x3F, op1, 0x18 | op2, mask, PPC_NONE, PPC2_DFP)
11268
11269 #define GEN_DFP_T_A_B_Rc(name, op1, op2) \
11270 _GEN_DFP_LONG(name, op1, op2, 0x00000000)
11271
11272 #define GEN_DFP_Tp_Ap_Bp_Rc(name, op1, op2) \
11273 _GEN_DFP_QUAD(name, op1, op2, 0x00210800)
11274
11275 #define GEN_DFP_Tp_A_Bp_Rc(name, op1, op2) \
11276 _GEN_DFP_QUAD(name, op1, op2, 0x00200800)
11277
11278 #define GEN_DFP_T_B_Rc(name, op1, op2) \
11279 _GEN_DFP_LONG(name, op1, op2, 0x001F0000)
11280
11281 #define GEN_DFP_Tp_Bp_Rc(name, op1, op2) \
11282 _GEN_DFP_QUAD(name, op1, op2, 0x003F0800)
11283
11284 #define GEN_DFP_Tp_B_Rc(name, op1, op2) \
11285 _GEN_DFP_QUAD(name, op1, op2, 0x003F0000)
11286
11287 #define GEN_DFP_T_Bp_Rc(name, op1, op2) \
11288 _GEN_DFP_QUAD(name, op1, op2, 0x001F0800)
11289
11290 #define GEN_DFP_BF_A_B(name, op1, op2) \
11291 _GEN_DFP_LONG(name, op1, op2, 0x00000001)
11292
11293 #define GEN_DFP_BF_Ap_Bp(name, op1, op2) \
11294 _GEN_DFP_QUAD(name, op1, op2, 0x00610801)
11295
11296 #define GEN_DFP_BF_A_Bp(name, op1, op2) \
11297 _GEN_DFP_QUAD(name, op1, op2, 0x00600801)
11298
11299 #define GEN_DFP_BF_A_DCM(name, op1, op2) \
11300 _GEN_DFP_LONGx2(name, op1, op2, 0x00600001)
11301
11302 #define GEN_DFP_BF_Ap_DCM(name, op1, op2) \
11303 _GEN_DFP_QUADx2(name, op1, op2, 0x00610001)
11304
11305 #define GEN_DFP_T_A_B_RMC_Rc(name, op1, op2) \
11306 _GEN_DFP_LONGx4(name, op1, op2, 0x00000000)
11307
11308 #define GEN_DFP_Tp_Ap_Bp_RMC_Rc(name, op1, op2) \
11309 _GEN_DFP_QUADx4(name, op1, op2, 0x02010800)
11310
11311 #define GEN_DFP_Tp_A_Bp_RMC_Rc(name, op1, op2) \
11312 _GEN_DFP_QUADx4(name, op1, op2, 0x02000800)
11313
11314 #define GEN_DFP_TE_T_B_RMC_Rc(name, op1, op2) \
11315 _GEN_DFP_LONGx4(name, op1, op2, 0x00000000)
11316
11317 #define GEN_DFP_TE_Tp_Bp_RMC_Rc(name, op1, op2) \
11318 _GEN_DFP_QUADx4(name, op1, op2, 0x00200800)
11319
11320 #define GEN_DFP_R_T_B_RMC_Rc(name, op1, op2) \
11321 _GEN_DFP_LONGx4(name, op1, op2, 0x001E0000)
11322
11323 #define GEN_DFP_R_Tp_Bp_RMC_Rc(name, op1, op2) \
11324 _GEN_DFP_QUADx4(name, op1, op2, 0x003E0800)
11325
11326 #define GEN_DFP_SP_T_B_Rc(name, op1, op2) \
11327 _GEN_DFP_LONG(name, op1, op2, 0x00070000)
11328
11329 #define GEN_DFP_SP_Tp_Bp_Rc(name, op1, op2) \
11330 _GEN_DFP_QUAD(name, op1, op2, 0x00270800)
11331
11332 #define GEN_DFP_S_T_B_Rc(name, op1, op2) \
11333 _GEN_DFP_LONG(name, op1, op2, 0x000F0000)
11334
11335 #define GEN_DFP_S_Tp_Bp_Rc(name, op1, op2) \
11336 _GEN_DFP_QUAD(name, op1, op2, 0x002F0800)
11337
11338 #define GEN_DFP_T_A_SH_Rc(name, op1, op2) \
11339 _GEN_DFP_LONGx2(name, op1, op2, 0x00000000)
11340
11341 #define GEN_DFP_Tp_Ap_SH_Rc(name, op1, op2) \
11342 _GEN_DFP_QUADx2(name, op1, op2, 0x00210000)
11343
11344 GEN_DFP_T_A_B_Rc(dadd, 0x02, 0x00),
11345 GEN_DFP_Tp_Ap_Bp_Rc(daddq, 0x02, 0x00),
11346 GEN_DFP_T_A_B_Rc(dsub, 0x02, 0x10),
11347 GEN_DFP_Tp_Ap_Bp_Rc(dsubq, 0x02, 0x10),
11348 GEN_DFP_T_A_B_Rc(dmul, 0x02, 0x01),
11349 GEN_DFP_Tp_Ap_Bp_Rc(dmulq, 0x02, 0x01),
11350 GEN_DFP_T_A_B_Rc(ddiv, 0x02, 0x11),
11351 GEN_DFP_Tp_Ap_Bp_Rc(ddivq, 0x02, 0x11),
11352 GEN_DFP_BF_A_B(dcmpu, 0x02, 0x14),
11353 GEN_DFP_BF_Ap_Bp(dcmpuq, 0x02, 0x14),
11354 GEN_DFP_BF_A_B(dcmpo, 0x02, 0x04),
11355 GEN_DFP_BF_Ap_Bp(dcmpoq, 0x02, 0x04),
11356 GEN_DFP_BF_A_DCM(dtstdc, 0x02, 0x06),
11357 GEN_DFP_BF_Ap_DCM(dtstdcq, 0x02, 0x06),
11358 GEN_DFP_BF_A_DCM(dtstdg, 0x02, 0x07),
11359 GEN_DFP_BF_Ap_DCM(dtstdgq, 0x02, 0x07),
11360 GEN_DFP_BF_A_B(dtstex, 0x02, 0x05),
11361 GEN_DFP_BF_Ap_Bp(dtstexq, 0x02, 0x05),
11362 GEN_DFP_BF_A_B(dtstsf, 0x02, 0x15),
11363 GEN_DFP_BF_A_Bp(dtstsfq, 0x02, 0x15),
11364 GEN_DFP_TE_T_B_RMC_Rc(dquai, 0x03, 0x02),
11365 GEN_DFP_TE_Tp_Bp_RMC_Rc(dquaiq, 0x03, 0x02),
11366 GEN_DFP_T_A_B_RMC_Rc(dqua, 0x03, 0x00),
11367 GEN_DFP_Tp_Ap_Bp_RMC_Rc(dquaq, 0x03, 0x00),
11368 GEN_DFP_T_A_B_RMC_Rc(drrnd, 0x03, 0x01),
11369 GEN_DFP_Tp_A_Bp_RMC_Rc(drrndq, 0x03, 0x01),
11370 GEN_DFP_R_T_B_RMC_Rc(drintx, 0x03, 0x03),
11371 GEN_DFP_R_Tp_Bp_RMC_Rc(drintxq, 0x03, 0x03),
11372 GEN_DFP_R_T_B_RMC_Rc(drintn, 0x03, 0x07),
11373 GEN_DFP_R_Tp_Bp_RMC_Rc(drintnq, 0x03, 0x07),
11374 GEN_DFP_T_B_Rc(dctdp, 0x02, 0x08),
11375 GEN_DFP_Tp_B_Rc(dctqpq, 0x02, 0x08),
11376 GEN_DFP_T_B_Rc(drsp, 0x02, 0x18),
11377 GEN_DFP_Tp_Bp_Rc(drdpq, 0x02, 0x18),
11378 GEN_DFP_T_B_Rc(dcffix, 0x02, 0x19),
11379 GEN_DFP_Tp_B_Rc(dcffixq, 0x02, 0x19),
11380 GEN_DFP_T_B_Rc(dctfix, 0x02, 0x09),
11381 GEN_DFP_T_Bp_Rc(dctfixq, 0x02, 0x09),
11382 GEN_DFP_SP_T_B_Rc(ddedpd, 0x02, 0x0a),
11383 GEN_DFP_SP_Tp_Bp_Rc(ddedpdq, 0x02, 0x0a),
11384 GEN_DFP_S_T_B_Rc(denbcd, 0x02, 0x1a),
11385 GEN_DFP_S_Tp_Bp_Rc(denbcdq, 0x02, 0x1a),
11386 GEN_DFP_T_B_Rc(dxex, 0x02, 0x0b),
11387 GEN_DFP_T_Bp_Rc(dxexq, 0x02, 0x0b),
11388 GEN_DFP_T_A_B_Rc(diex, 0x02, 0x1b),
11389 GEN_DFP_Tp_A_Bp_Rc(diexq, 0x02, 0x1b),
11390 GEN_DFP_T_A_SH_Rc(dscli, 0x02, 0x02),
11391 GEN_DFP_Tp_Ap_SH_Rc(dscliq, 0x02, 0x02),
11392 GEN_DFP_T_A_SH_Rc(dscri, 0x02, 0x03),
11393 GEN_DFP_Tp_Ap_SH_Rc(dscriq, 0x02, 0x03),
11394
11395 #undef GEN_SPE
11396 #define GEN_SPE(name0, name1, opc2, opc3, inval0, inval1, type) \
11397     GEN_OPCODE_DUAL(name0##_##name1, 0x04, opc2, opc3, inval0, inval1, type, PPC_NONE)
11398 GEN_SPE(evaddw,      speundef,    0x00, 0x08, 0x00000000, 0xFFFFFFFF, PPC_SPE),
11399 GEN_SPE(evaddiw,     speundef,    0x01, 0x08, 0x00000000, 0xFFFFFFFF, PPC_SPE),
11400 GEN_SPE(evsubfw,     speundef,    0x02, 0x08, 0x00000000, 0xFFFFFFFF, PPC_SPE),
11401 GEN_SPE(evsubifw,    speundef,    0x03, 0x08, 0x00000000, 0xFFFFFFFF, PPC_SPE),
11402 GEN_SPE(evabs,       evneg,       0x04, 0x08, 0x0000F800, 0x0000F800, PPC_SPE),
11403 GEN_SPE(evextsb,     evextsh,     0x05, 0x08, 0x0000F800, 0x0000F800, PPC_SPE),
11404 GEN_SPE(evrndw,      evcntlzw,    0x06, 0x08, 0x0000F800, 0x0000F800, PPC_SPE),
11405 GEN_SPE(evcntlsw,    brinc,       0x07, 0x08, 0x0000F800, 0x00000000, PPC_SPE),
11406 GEN_SPE(evmra,       speundef,    0x02, 0x13, 0x0000F800, 0xFFFFFFFF, PPC_SPE),
11407 GEN_SPE(speundef,    evand,       0x08, 0x08, 0xFFFFFFFF, 0x00000000, PPC_SPE),
11408 GEN_SPE(evandc,      speundef,    0x09, 0x08, 0x00000000, 0xFFFFFFFF, PPC_SPE),
11409 GEN_SPE(evxor,       evor,        0x0B, 0x08, 0x00000000, 0x00000000, PPC_SPE),
11410 GEN_SPE(evnor,       eveqv,       0x0C, 0x08, 0x00000000, 0x00000000, PPC_SPE),
11411 GEN_SPE(evmwumi,     evmwsmi,     0x0C, 0x11, 0x00000000, 0x00000000, PPC_SPE),
11412 GEN_SPE(evmwumia,    evmwsmia,    0x1C, 0x11, 0x00000000, 0x00000000, PPC_SPE),
11413 GEN_SPE(evmwumiaa,   evmwsmiaa,   0x0C, 0x15, 0x00000000, 0x00000000, PPC_SPE),
11414 GEN_SPE(speundef,    evorc,       0x0D, 0x08, 0xFFFFFFFF, 0x00000000, PPC_SPE),
11415 GEN_SPE(evnand,      speundef,    0x0F, 0x08, 0x00000000, 0xFFFFFFFF, PPC_SPE),
11416 GEN_SPE(evsrwu,      evsrws,      0x10, 0x08, 0x00000000, 0x00000000, PPC_SPE),
11417 GEN_SPE(evsrwiu,     evsrwis,     0x11, 0x08, 0x00000000, 0x00000000, PPC_SPE),
11418 GEN_SPE(evslw,       speundef,    0x12, 0x08, 0x00000000, 0xFFFFFFFF, PPC_SPE),
11419 GEN_SPE(evslwi,      speundef,    0x13, 0x08, 0x00000000, 0xFFFFFFFF, PPC_SPE),
11420 GEN_SPE(evrlw,       evsplati,    0x14, 0x08, 0x00000000, 0x0000F800, PPC_SPE),
11421 GEN_SPE(evrlwi,      evsplatfi,   0x15, 0x08, 0x00000000, 0x0000F800, PPC_SPE),
11422 GEN_SPE(evmergehi,   evmergelo,   0x16, 0x08, 0x00000000, 0x00000000, PPC_SPE),
11423 GEN_SPE(evmergehilo, evmergelohi, 0x17, 0x08, 0x00000000, 0x00000000, PPC_SPE),
11424 GEN_SPE(evcmpgtu,    evcmpgts,    0x18, 0x08, 0x00600000, 0x00600000, PPC_SPE),
11425 GEN_SPE(evcmpltu,    evcmplts,    0x19, 0x08, 0x00600000, 0x00600000, PPC_SPE),
11426 GEN_SPE(evcmpeq,     speundef,    0x1A, 0x08, 0x00600000, 0xFFFFFFFF, PPC_SPE),
11427
11428 GEN_SPE(evfsadd,     evfssub,     0x00, 0x0A, 0x00000000, 0x00000000, PPC_SPE_SINGLE),
11429 GEN_SPE(evfsabs,     evfsnabs,    0x02, 0x0A, 0x0000F800, 0x0000F800, PPC_SPE_SINGLE),
11430 GEN_SPE(evfsneg,     speundef,    0x03, 0x0A, 0x0000F800, 0xFFFFFFFF, PPC_SPE_SINGLE),
11431 GEN_SPE(evfsmul,     evfsdiv,     0x04, 0x0A, 0x00000000, 0x00000000, PPC_SPE_SINGLE),
11432 GEN_SPE(evfscmpgt,   evfscmplt,   0x06, 0x0A, 0x00600000, 0x00600000, PPC_SPE_SINGLE),
11433 GEN_SPE(evfscmpeq,   speundef,    0x07, 0x0A, 0x00600000, 0xFFFFFFFF, PPC_SPE_SINGLE),
11434 GEN_SPE(evfscfui,    evfscfsi,    0x08, 0x0A, 0x00180000, 0x00180000, PPC_SPE_SINGLE),
11435 GEN_SPE(evfscfuf,    evfscfsf,    0x09, 0x0A, 0x00180000, 0x00180000, PPC_SPE_SINGLE),
11436 GEN_SPE(evfsctui,    evfsctsi,    0x0A, 0x0A, 0x00180000, 0x00180000, PPC_SPE_SINGLE),
11437 GEN_SPE(evfsctuf,    evfsctsf,    0x0B, 0x0A, 0x00180000, 0x00180000, PPC_SPE_SINGLE),
11438 GEN_SPE(evfsctuiz,   speundef,    0x0C, 0x0A, 0x00180000, 0xFFFFFFFF, PPC_SPE_SINGLE),
11439 GEN_SPE(evfsctsiz,   speundef,    0x0D, 0x0A, 0x00180000, 0xFFFFFFFF, PPC_SPE_SINGLE),
11440 GEN_SPE(evfststgt,   evfststlt,   0x0E, 0x0A, 0x00600000, 0x00600000, PPC_SPE_SINGLE),
11441 GEN_SPE(evfststeq,   speundef,    0x0F, 0x0A, 0x00600000, 0xFFFFFFFF, PPC_SPE_SINGLE),
11442
11443 GEN_SPE(efsadd,      efssub,      0x00, 0x0B, 0x00000000, 0x00000000, PPC_SPE_SINGLE),
11444 GEN_SPE(efsabs,      efsnabs,     0x02, 0x0B, 0x0000F800, 0x0000F800, PPC_SPE_SINGLE),
11445 GEN_SPE(efsneg,      speundef,    0x03, 0x0B, 0x0000F800, 0xFFFFFFFF, PPC_SPE_SINGLE),
11446 GEN_SPE(efsmul,      efsdiv,      0x04, 0x0B, 0x00000000, 0x00000000, PPC_SPE_SINGLE),
11447 GEN_SPE(efscmpgt,    efscmplt,    0x06, 0x0B, 0x00600000, 0x00600000, PPC_SPE_SINGLE),
11448 GEN_SPE(efscmpeq,    efscfd,      0x07, 0x0B, 0x00600000, 0x00180000, PPC_SPE_SINGLE),
11449 GEN_SPE(efscfui,     efscfsi,     0x08, 0x0B, 0x00180000, 0x00180000, PPC_SPE_SINGLE),
11450 GEN_SPE(efscfuf,     efscfsf,     0x09, 0x0B, 0x00180000, 0x00180000, PPC_SPE_SINGLE),
11451 GEN_SPE(efsctui,     efsctsi,     0x0A, 0x0B, 0x00180000, 0x00180000, PPC_SPE_SINGLE),
11452 GEN_SPE(efsctuf,     efsctsf,     0x0B, 0x0B, 0x00180000, 0x00180000, PPC_SPE_SINGLE),
11453 GEN_SPE(efsctuiz,    speundef,    0x0C, 0x0B, 0x00180000, 0xFFFFFFFF, PPC_SPE_SINGLE),
11454 GEN_SPE(efsctsiz,    speundef,    0x0D, 0x0B, 0x00180000, 0xFFFFFFFF, PPC_SPE_SINGLE),
11455 GEN_SPE(efststgt,    efststlt,    0x0E, 0x0B, 0x00600000, 0x00600000, PPC_SPE_SINGLE),
11456 GEN_SPE(efststeq,    speundef,    0x0F, 0x0B, 0x00600000, 0xFFFFFFFF, PPC_SPE_SINGLE),
11457
11458 GEN_SPE(efdadd,      efdsub,      0x10, 0x0B, 0x00000000, 0x00000000, PPC_SPE_DOUBLE),
11459 GEN_SPE(efdcfuid,    efdcfsid,    0x11, 0x0B, 0x00180000, 0x00180000, PPC_SPE_DOUBLE),
11460 GEN_SPE(efdabs,      efdnabs,     0x12, 0x0B, 0x0000F800, 0x0000F800, PPC_SPE_DOUBLE),
11461 GEN_SPE(efdneg,      speundef,    0x13, 0x0B, 0x0000F800, 0xFFFFFFFF, PPC_SPE_DOUBLE),
11462 GEN_SPE(efdmul,      efddiv,      0x14, 0x0B, 0x00000000, 0x00000000, PPC_SPE_DOUBLE),
11463 GEN_SPE(efdctuidz,   efdctsidz,   0x15, 0x0B, 0x00180000, 0x00180000, PPC_SPE_DOUBLE),
11464 GEN_SPE(efdcmpgt,    efdcmplt,    0x16, 0x0B, 0x00600000, 0x00600000, PPC_SPE_DOUBLE),
11465 GEN_SPE(efdcmpeq,    efdcfs,      0x17, 0x0B, 0x00600000, 0x00180000, PPC_SPE_DOUBLE),
11466 GEN_SPE(efdcfui,     efdcfsi,     0x18, 0x0B, 0x00180000, 0x00180000, PPC_SPE_DOUBLE),
11467 GEN_SPE(efdcfuf,     efdcfsf,     0x19, 0x0B, 0x00180000, 0x00180000, PPC_SPE_DOUBLE),
11468 GEN_SPE(efdctui,     efdctsi,     0x1A, 0x0B, 0x00180000, 0x00180000, PPC_SPE_DOUBLE),
11469 GEN_SPE(efdctuf,     efdctsf,     0x1B, 0x0B, 0x00180000, 0x00180000, PPC_SPE_DOUBLE),
11470 GEN_SPE(efdctuiz,    speundef,    0x1C, 0x0B, 0x00180000, 0xFFFFFFFF, PPC_SPE_DOUBLE),
11471 GEN_SPE(efdctsiz,    speundef,    0x1D, 0x0B, 0x00180000, 0xFFFFFFFF, PPC_SPE_DOUBLE),
11472 GEN_SPE(efdtstgt,    efdtstlt,    0x1E, 0x0B, 0x00600000, 0x00600000, PPC_SPE_DOUBLE),
11473 GEN_SPE(efdtsteq,    speundef,    0x1F, 0x0B, 0x00600000, 0xFFFFFFFF, PPC_SPE_DOUBLE),
11474
11475 #undef GEN_SPEOP_LDST
11476 #define GEN_SPEOP_LDST(name, opc2, sh)                                        \
11477 GEN_HANDLER(name, 0x04, opc2, 0x0C, 0x00000000, PPC_SPE)
11478 GEN_SPEOP_LDST(evldd, 0x00, 3),
11479 GEN_SPEOP_LDST(evldw, 0x01, 3),
11480 GEN_SPEOP_LDST(evldh, 0x02, 3),
11481 GEN_SPEOP_LDST(evlhhesplat, 0x04, 1),
11482 GEN_SPEOP_LDST(evlhhousplat, 0x06, 1),
11483 GEN_SPEOP_LDST(evlhhossplat, 0x07, 1),
11484 GEN_SPEOP_LDST(evlwhe, 0x08, 2),
11485 GEN_SPEOP_LDST(evlwhou, 0x0A, 2),
11486 GEN_SPEOP_LDST(evlwhos, 0x0B, 2),
11487 GEN_SPEOP_LDST(evlwwsplat, 0x0C, 2),
11488 GEN_SPEOP_LDST(evlwhsplat, 0x0E, 2),
11489
11490 GEN_SPEOP_LDST(evstdd, 0x10, 3),
11491 GEN_SPEOP_LDST(evstdw, 0x11, 3),
11492 GEN_SPEOP_LDST(evstdh, 0x12, 3),
11493 GEN_SPEOP_LDST(evstwhe, 0x18, 2),
11494 GEN_SPEOP_LDST(evstwho, 0x1A, 2),
11495 GEN_SPEOP_LDST(evstwwe, 0x1C, 2),
11496 GEN_SPEOP_LDST(evstwwo, 0x1E, 2),
11497 };
11498
11499 #include "helper_regs.h"
11500 #include "translate_init.c"
11501
11502 /*****************************************************************************/
11503 /* Misc PowerPC helpers */
11504 void ppc_cpu_dump_state(CPUState *cs, FILE *f, fprintf_function cpu_fprintf,
11505                         int flags)
11506 {
11507 #define RGPL  4
11508 #define RFPL  4
11509
11510     PowerPCCPU *cpu = POWERPC_CPU(cs);
11511     CPUPPCState *env = &cpu->env;
11512     int i;
11513
11514     cpu_fprintf(f, "NIP " TARGET_FMT_lx "   LR " TARGET_FMT_lx " CTR "
11515                 TARGET_FMT_lx " XER " TARGET_FMT_lx "\n",
11516                 env->nip, env->lr, env->ctr, cpu_read_xer(env));
11517     cpu_fprintf(f, "MSR " TARGET_FMT_lx " HID0 " TARGET_FMT_lx "  HF "
11518                 TARGET_FMT_lx " idx %d\n", env->msr, env->spr[SPR_HID0],
11519                 env->hflags, env->mmu_idx);
11520 #if !defined(NO_TIMER_DUMP)
11521     cpu_fprintf(f, "TB %08" PRIu32 " %08" PRIu64
11522 #if !defined(CONFIG_USER_ONLY)
11523                 " DECR %08" PRIu32
11524 #endif
11525                 "\n",
11526                 cpu_ppc_load_tbu(env), cpu_ppc_load_tbl(env)
11527 #if !defined(CONFIG_USER_ONLY)
11528                 , cpu_ppc_load_decr(env)
11529 #endif
11530                 );
11531 #endif
11532     for (i = 0; i < 32; i++) {
11533         if ((i & (RGPL - 1)) == 0)
11534             cpu_fprintf(f, "GPR%02d", i);
11535         cpu_fprintf(f, " %016" PRIx64, ppc_dump_gpr(env, i));
11536         if ((i & (RGPL - 1)) == (RGPL - 1))
11537             cpu_fprintf(f, "\n");
11538     }
11539     cpu_fprintf(f, "CR ");
11540     for (i = 0; i < 8; i++)
11541         cpu_fprintf(f, "%01x", env->crf[i]);
11542     cpu_fprintf(f, "  [");
11543     for (i = 0; i < 8; i++) {
11544         char a = '-';
11545         if (env->crf[i] & 0x08)
11546             a = 'L';
11547         else if (env->crf[i] & 0x04)
11548             a = 'G';
11549         else if (env->crf[i] & 0x02)
11550             a = 'E';
11551         cpu_fprintf(f, " %c%c", a, env->crf[i] & 0x01 ? 'O' : ' ');
11552     }
11553     cpu_fprintf(f, " ]             RES " TARGET_FMT_lx "\n",
11554                 env->reserve_addr);
11555     for (i = 0; i < 32; i++) {
11556         if ((i & (RFPL - 1)) == 0)
11557             cpu_fprintf(f, "FPR%02d", i);
11558         cpu_fprintf(f, " %016" PRIx64, *((uint64_t *)&env->fpr[i]));
11559         if ((i & (RFPL - 1)) == (RFPL - 1))
11560             cpu_fprintf(f, "\n");
11561     }
11562     cpu_fprintf(f, "FPSCR " TARGET_FMT_lx "\n", env->fpscr);
11563 #if !defined(CONFIG_USER_ONLY)
11564     cpu_fprintf(f, " SRR0 " TARGET_FMT_lx "  SRR1 " TARGET_FMT_lx
11565                    "    PVR " TARGET_FMT_lx " VRSAVE " TARGET_FMT_lx "\n",
11566                 env->spr[SPR_SRR0], env->spr[SPR_SRR1],
11567                 env->spr[SPR_PVR], env->spr[SPR_VRSAVE]);
11568
11569     cpu_fprintf(f, "SPRG0 " TARGET_FMT_lx " SPRG1 " TARGET_FMT_lx
11570                    "  SPRG2 " TARGET_FMT_lx "  SPRG3 " TARGET_FMT_lx "\n",
11571                 env->spr[SPR_SPRG0], env->spr[SPR_SPRG1],
11572                 env->spr[SPR_SPRG2], env->spr[SPR_SPRG3]);
11573
11574     cpu_fprintf(f, "SPRG4 " TARGET_FMT_lx " SPRG5 " TARGET_FMT_lx
11575                    "  SPRG6 " TARGET_FMT_lx "  SPRG7 " TARGET_FMT_lx "\n",
11576                 env->spr[SPR_SPRG4], env->spr[SPR_SPRG5],
11577                 env->spr[SPR_SPRG6], env->spr[SPR_SPRG7]);
11578
11579     if (env->excp_model == POWERPC_EXCP_BOOKE) {
11580         cpu_fprintf(f, "CSRR0 " TARGET_FMT_lx " CSRR1 " TARGET_FMT_lx
11581                        " MCSRR0 " TARGET_FMT_lx " MCSRR1 " TARGET_FMT_lx "\n",
11582                     env->spr[SPR_BOOKE_CSRR0], env->spr[SPR_BOOKE_CSRR1],
11583                     env->spr[SPR_BOOKE_MCSRR0], env->spr[SPR_BOOKE_MCSRR1]);
11584
11585         cpu_fprintf(f, "  TCR " TARGET_FMT_lx "   TSR " TARGET_FMT_lx
11586                        "    ESR " TARGET_FMT_lx "   DEAR " TARGET_FMT_lx "\n",
11587                     env->spr[SPR_BOOKE_TCR], env->spr[SPR_BOOKE_TSR],
11588                     env->spr[SPR_BOOKE_ESR], env->spr[SPR_BOOKE_DEAR]);
11589
11590         cpu_fprintf(f, "  PIR " TARGET_FMT_lx " DECAR " TARGET_FMT_lx
11591                        "   IVPR " TARGET_FMT_lx "   EPCR " TARGET_FMT_lx "\n",
11592                     env->spr[SPR_BOOKE_PIR], env->spr[SPR_BOOKE_DECAR],
11593                     env->spr[SPR_BOOKE_IVPR], env->spr[SPR_BOOKE_EPCR]);
11594
11595         cpu_fprintf(f, " MCSR " TARGET_FMT_lx " SPRG8 " TARGET_FMT_lx
11596                        "    EPR " TARGET_FMT_lx "\n",
11597                     env->spr[SPR_BOOKE_MCSR], env->spr[SPR_BOOKE_SPRG8],
11598                     env->spr[SPR_BOOKE_EPR]);
11599
11600         /* FSL-specific */
11601         cpu_fprintf(f, " MCAR " TARGET_FMT_lx "  PID1 " TARGET_FMT_lx
11602                        "   PID2 " TARGET_FMT_lx "    SVR " TARGET_FMT_lx "\n",
11603                     env->spr[SPR_Exxx_MCAR], env->spr[SPR_BOOKE_PID1],
11604                     env->spr[SPR_BOOKE_PID2], env->spr[SPR_E500_SVR]);
11605
11606         /*
11607          * IVORs are left out as they are large and do not change often --
11608          * they can be read with "p $ivor0", "p $ivor1", etc.
11609          */
11610     }
11611
11612 #if defined(TARGET_PPC64)
11613     if (env->flags & POWERPC_FLAG_CFAR) {
11614         cpu_fprintf(f, " CFAR " TARGET_FMT_lx"\n", env->cfar);
11615     }
11616 #endif
11617
11618     switch (env->mmu_model) {
11619     case POWERPC_MMU_32B:
11620     case POWERPC_MMU_601:
11621     case POWERPC_MMU_SOFT_6xx:
11622     case POWERPC_MMU_SOFT_74xx:
11623 #if defined(TARGET_PPC64)
11624     case POWERPC_MMU_64B:
11625     case POWERPC_MMU_2_06:
11626     case POWERPC_MMU_2_06a:
11627     case POWERPC_MMU_2_06d:
11628 #endif
11629         cpu_fprintf(f, " SDR1 " TARGET_FMT_lx "   DAR " TARGET_FMT_lx
11630                        "  DSISR " TARGET_FMT_lx "\n", env->spr[SPR_SDR1],
11631                     env->spr[SPR_DAR], env->spr[SPR_DSISR]);
11632         break;
11633     case POWERPC_MMU_BOOKE206:
11634         cpu_fprintf(f, " MAS0 " TARGET_FMT_lx "  MAS1 " TARGET_FMT_lx
11635                        "   MAS2 " TARGET_FMT_lx "   MAS3 " TARGET_FMT_lx "\n",
11636                     env->spr[SPR_BOOKE_MAS0], env->spr[SPR_BOOKE_MAS1],
11637                     env->spr[SPR_BOOKE_MAS2], env->spr[SPR_BOOKE_MAS3]);
11638
11639         cpu_fprintf(f, " MAS4 " TARGET_FMT_lx "  MAS6 " TARGET_FMT_lx
11640                        "   MAS7 " TARGET_FMT_lx "    PID " TARGET_FMT_lx "\n",
11641                     env->spr[SPR_BOOKE_MAS4], env->spr[SPR_BOOKE_MAS6],
11642                     env->spr[SPR_BOOKE_MAS7], env->spr[SPR_BOOKE_PID]);
11643
11644         cpu_fprintf(f, "MMUCFG " TARGET_FMT_lx " TLB0CFG " TARGET_FMT_lx
11645                        " TLB1CFG " TARGET_FMT_lx "\n",
11646                     env->spr[SPR_MMUCFG], env->spr[SPR_BOOKE_TLB0CFG],
11647                     env->spr[SPR_BOOKE_TLB1CFG]);
11648         break;
11649     default:
11650         break;
11651     }
11652 #endif
11653
11654 #undef RGPL
11655 #undef RFPL
11656 }
11657
11658 void ppc_cpu_dump_statistics(CPUState *cs, FILE*f,
11659                              fprintf_function cpu_fprintf, int flags)
11660 {
11661 #if defined(DO_PPC_STATISTICS)
11662     PowerPCCPU *cpu = POWERPC_CPU(cs);
11663     opc_handler_t **t1, **t2, **t3, *handler;
11664     int op1, op2, op3;
11665
11666     t1 = cpu->env.opcodes;
11667     for (op1 = 0; op1 < 64; op1++) {
11668         handler = t1[op1];
11669         if (is_indirect_opcode(handler)) {
11670             t2 = ind_table(handler);
11671             for (op2 = 0; op2 < 32; op2++) {
11672                 handler = t2[op2];
11673                 if (is_indirect_opcode(handler)) {
11674                     t3 = ind_table(handler);
11675                     for (op3 = 0; op3 < 32; op3++) {
11676                         handler = t3[op3];
11677                         if (handler->count == 0)
11678                             continue;
11679                         cpu_fprintf(f, "%02x %02x %02x (%02x %04d) %16s: "
11680                                     "%016" PRIx64 " %" PRId64 "\n",
11681                                     op1, op2, op3, op1, (op3 << 5) | op2,
11682                                     handler->oname,
11683                                     handler->count, handler->count);
11684                     }
11685                 } else {
11686                     if (handler->count == 0)
11687                         continue;
11688                     cpu_fprintf(f, "%02x %02x    (%02x %04d) %16s: "
11689                                 "%016" PRIx64 " %" PRId64 "\n",
11690                                 op1, op2, op1, op2, handler->oname,
11691                                 handler->count, handler->count);
11692                 }
11693             }
11694         } else {
11695             if (handler->count == 0)
11696                 continue;
11697             cpu_fprintf(f, "%02x       (%02x     ) %16s: %016" PRIx64
11698                         " %" PRId64 "\n",
11699                         op1, op1, handler->oname,
11700                         handler->count, handler->count);
11701         }
11702     }
11703 #endif
11704 }
11705
11706 /*****************************************************************************/
11707 static inline void gen_intermediate_code_internal(PowerPCCPU *cpu,
11708                                                   TranslationBlock *tb,
11709                                                   bool search_pc)
11710 {
11711     CPUState *cs = CPU(cpu);
11712     CPUPPCState *env = &cpu->env;
11713     DisasContext ctx, *ctxp = &ctx;
11714     opc_handler_t **table, *handler;
11715     target_ulong pc_start;
11716     uint16_t *gen_opc_end;
11717     CPUBreakpoint *bp;
11718     int j, lj = -1;
11719     int num_insns;
11720     int max_insns;
11721
11722     pc_start = tb->pc;
11723     gen_opc_end = tcg_ctx.gen_opc_buf + OPC_MAX_SIZE;
11724     ctx.nip = pc_start;
11725     ctx.tb = tb;
11726     ctx.exception = POWERPC_EXCP_NONE;
11727     ctx.spr_cb = env->spr_cb;
11728     ctx.mem_idx = env->mmu_idx;
11729     ctx.insns_flags = env->insns_flags;
11730     ctx.insns_flags2 = env->insns_flags2;
11731     ctx.access_type = -1;
11732     ctx.le_mode = env->hflags & (1 << MSR_LE) ? 1 : 0;
11733 #if defined(TARGET_PPC64)
11734     ctx.sf_mode = msr_is_64bit(env, env->msr);
11735     ctx.has_cfar = !!(env->flags & POWERPC_FLAG_CFAR);
11736 #endif
11737     ctx.fpu_enabled = msr_fp;
11738     if ((env->flags & POWERPC_FLAG_SPE) && msr_spe)
11739         ctx.spe_enabled = msr_spe;
11740     else
11741         ctx.spe_enabled = 0;
11742     if ((env->flags & POWERPC_FLAG_VRE) && msr_vr)
11743         ctx.altivec_enabled = msr_vr;
11744     else
11745         ctx.altivec_enabled = 0;
11746     if ((env->flags & POWERPC_FLAG_VSX) && msr_vsx) {
11747         ctx.vsx_enabled = msr_vsx;
11748     } else {
11749         ctx.vsx_enabled = 0;
11750     }
11751     if ((env->flags & POWERPC_FLAG_SE) && msr_se)
11752         ctx.singlestep_enabled = CPU_SINGLE_STEP;
11753     else
11754         ctx.singlestep_enabled = 0;
11755     if ((env->flags & POWERPC_FLAG_BE) && msr_be)
11756         ctx.singlestep_enabled |= CPU_BRANCH_STEP;
11757     if (unlikely(cs->singlestep_enabled)) {
11758         ctx.singlestep_enabled |= GDBSTUB_SINGLE_STEP;
11759     }
11760 #if defined (DO_SINGLE_STEP) && 0
11761     /* Single step trace mode */
11762     msr_se = 1;
11763 #endif
11764     num_insns = 0;
11765     max_insns = tb->cflags & CF_COUNT_MASK;
11766     if (max_insns == 0)
11767         max_insns = CF_COUNT_MASK;
11768
11769     gen_tb_start();
11770     tcg_clear_temp_count();
11771     /* Set env in case of segfault during code fetch */
11772     while (ctx.exception == POWERPC_EXCP_NONE
11773             && tcg_ctx.gen_opc_ptr < gen_opc_end) {
11774         if (unlikely(!QTAILQ_EMPTY(&cs->breakpoints))) {
11775             QTAILQ_FOREACH(bp, &cs->breakpoints, entry) {
11776                 if (bp->pc == ctx.nip) {
11777                     gen_debug_exception(ctxp);
11778                     break;
11779                 }
11780             }
11781         }
11782         if (unlikely(search_pc)) {
11783             j = tcg_ctx.gen_opc_ptr - tcg_ctx.gen_opc_buf;
11784             if (lj < j) {
11785                 lj++;
11786                 while (lj < j)
11787                     tcg_ctx.gen_opc_instr_start[lj++] = 0;
11788             }
11789             tcg_ctx.gen_opc_pc[lj] = ctx.nip;
11790             tcg_ctx.gen_opc_instr_start[lj] = 1;
11791             tcg_ctx.gen_opc_icount[lj] = num_insns;
11792         }
11793         LOG_DISAS("----------------\n");
11794         LOG_DISAS("nip=" TARGET_FMT_lx " super=%d ir=%d\n",
11795                   ctx.nip, ctx.mem_idx, (int)msr_ir);
11796         if (num_insns + 1 == max_insns && (tb->cflags & CF_LAST_IO))
11797             gen_io_start();
11798         if (unlikely(ctx.le_mode)) {
11799             ctx.opcode = bswap32(cpu_ldl_code(env, ctx.nip));
11800         } else {
11801             ctx.opcode = cpu_ldl_code(env, ctx.nip);
11802         }
11803         LOG_DISAS("translate opcode %08x (%02x %02x %02x) (%s)\n",
11804                     ctx.opcode, opc1(ctx.opcode), opc2(ctx.opcode),
11805                     opc3(ctx.opcode), ctx.le_mode ? "little" : "big");
11806         if (unlikely(qemu_loglevel_mask(CPU_LOG_TB_OP | CPU_LOG_TB_OP_OPT))) {
11807             tcg_gen_debug_insn_start(ctx.nip);
11808         }
11809         ctx.nip += 4;
11810         table = env->opcodes;
11811         num_insns++;
11812         handler = table[opc1(ctx.opcode)];
11813         if (is_indirect_opcode(handler)) {
11814             table = ind_table(handler);
11815             handler = table[opc2(ctx.opcode)];
11816             if (is_indirect_opcode(handler)) {
11817                 table = ind_table(handler);
11818                 handler = table[opc3(ctx.opcode)];
11819             }
11820         }
11821         /* Is opcode *REALLY* valid ? */
11822         if (unlikely(handler->handler == &gen_invalid)) {
11823             if (qemu_log_enabled()) {
11824                 qemu_log("invalid/unsupported opcode: "
11825                          "%02x - %02x - %02x (%08x) " TARGET_FMT_lx " %d\n",
11826                          opc1(ctx.opcode), opc2(ctx.opcode),
11827                          opc3(ctx.opcode), ctx.opcode, ctx.nip - 4, (int)msr_ir);
11828             }
11829         } else {
11830             uint32_t inval;
11831
11832             if (unlikely(handler->type & (PPC_SPE | PPC_SPE_SINGLE | PPC_SPE_DOUBLE) && Rc(ctx.opcode))) {
11833                 inval = handler->inval2;
11834             } else {
11835                 inval = handler->inval1;
11836             }
11837
11838             if (unlikely((ctx.opcode & inval) != 0)) {
11839                 if (qemu_log_enabled()) {
11840                     qemu_log("invalid bits: %08x for opcode: "
11841                              "%02x - %02x - %02x (%08x) " TARGET_FMT_lx "\n",
11842                              ctx.opcode & inval, opc1(ctx.opcode),
11843                              opc2(ctx.opcode), opc3(ctx.opcode),
11844                              ctx.opcode, ctx.nip - 4);
11845                 }
11846                 gen_inval_exception(ctxp, POWERPC_EXCP_INVAL_INVAL);
11847                 break;
11848             }
11849         }
11850         (*(handler->handler))(&ctx);
11851 #if defined(DO_PPC_STATISTICS)
11852         handler->count++;
11853 #endif
11854         /* Check trace mode exceptions */
11855         if (unlikely(ctx.singlestep_enabled & CPU_SINGLE_STEP &&
11856                      (ctx.nip <= 0x100 || ctx.nip > 0xF00) &&
11857                      ctx.exception != POWERPC_SYSCALL &&
11858                      ctx.exception != POWERPC_EXCP_TRAP &&
11859                      ctx.exception != POWERPC_EXCP_BRANCH)) {
11860             gen_exception(ctxp, POWERPC_EXCP_TRACE);
11861         } else if (unlikely(((ctx.nip & (TARGET_PAGE_SIZE - 1)) == 0) ||
11862                             (cs->singlestep_enabled) ||
11863                             singlestep ||
11864                             num_insns >= max_insns)) {
11865             /* if we reach a page boundary or are single stepping, stop
11866              * generation
11867              */
11868             break;
11869         }
11870         if (tcg_check_temp_count()) {
11871             fprintf(stderr, "Opcode %02x %02x %02x (%08x) leaked temporaries\n",
11872                     opc1(ctx.opcode), opc2(ctx.opcode), opc3(ctx.opcode),
11873                     ctx.opcode);
11874             exit(1);
11875         }
11876     }
11877     if (tb->cflags & CF_LAST_IO)
11878         gen_io_end();
11879     if (ctx.exception == POWERPC_EXCP_NONE) {
11880         gen_goto_tb(&ctx, 0, ctx.nip);
11881     } else if (ctx.exception != POWERPC_EXCP_BRANCH) {
11882         if (unlikely(cs->singlestep_enabled)) {
11883             gen_debug_exception(ctxp);
11884         }
11885         /* Generate the return instruction */
11886         tcg_gen_exit_tb(0);
11887     }
11888     gen_tb_end(tb, num_insns);
11889     *tcg_ctx.gen_opc_ptr = INDEX_op_end;
11890     if (unlikely(search_pc)) {
11891         j = tcg_ctx.gen_opc_ptr - tcg_ctx.gen_opc_buf;
11892         lj++;
11893         while (lj <= j)
11894             tcg_ctx.gen_opc_instr_start[lj++] = 0;
11895     } else {
11896         tb->size = ctx.nip - pc_start;
11897         tb->icount = num_insns;
11898     }
11899 #if defined(DEBUG_DISAS)
11900     if (qemu_loglevel_mask(CPU_LOG_TB_IN_ASM)) {
11901         int flags;
11902         flags = env->bfd_mach;
11903         flags |= ctx.le_mode << 16;
11904         qemu_log("IN: %s\n", lookup_symbol(pc_start));
11905         log_target_disas(env, pc_start, ctx.nip - pc_start, flags);
11906         qemu_log("\n");
11907     }
11908 #endif
11909 }
11910
11911 void gen_intermediate_code (CPUPPCState *env, struct TranslationBlock *tb)
11912 {
11913     gen_intermediate_code_internal(ppc_env_get_cpu(env), tb, false);
11914 }
11915
11916 void gen_intermediate_code_pc (CPUPPCState *env, struct TranslationBlock *tb)
11917 {
11918     gen_intermediate_code_internal(ppc_env_get_cpu(env), tb, true);
11919 }
11920
11921 void restore_state_to_opc(CPUPPCState *env, TranslationBlock *tb, int pc_pos)
11922 {
11923     env->nip = tcg_ctx.gen_opc_pc[pc_pos];
11924 }
This page took 0.71036 seconds and 4 git commands to generate.